core.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Multiplexer subsystem
  4. *
  5. * Copyright (C) 2017 Axentia Technologies AB
  6. *
  7. * Author: Peter Rosin <peda@axentia.se>
  8. */
  9. #define pr_fmt(fmt) "mux-core: " fmt
  10. #include <linux/delay.h>
  11. #include <linux/device.h>
  12. #include <linux/err.h>
  13. #include <linux/export.h>
  14. #include <linux/idr.h>
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/mux/consumer.h>
  18. #include <linux/mux/driver.h>
  19. #include <linux/of.h>
  20. #include <linux/slab.h>
  21. /*
  22. * The idle-as-is "state" is not an actual state that may be selected, it
  23. * only implies that the state should not be changed. So, use that state
  24. * as indication that the cached state of the multiplexer is unknown.
  25. */
  26. #define MUX_CACHE_UNKNOWN MUX_IDLE_AS_IS
  27. /**
  28. * struct mux_state - Represents a mux controller state specific to a given
  29. * consumer.
  30. * @mux: Pointer to a mux controller.
  31. * @state: State of the mux to be selected.
  32. *
  33. * This structure is specific to the consumer that acquires it and has
  34. * information specific to that consumer.
  35. */
  36. struct mux_state {
  37. struct mux_control *mux;
  38. unsigned int state;
  39. };
  40. static const struct class mux_class = {
  41. .name = "mux",
  42. };
  43. static DEFINE_IDA(mux_ida);
  44. static int __init mux_init(void)
  45. {
  46. ida_init(&mux_ida);
  47. return class_register(&mux_class);
  48. }
  49. static void __exit mux_exit(void)
  50. {
  51. class_unregister(&mux_class);
  52. ida_destroy(&mux_ida);
  53. }
  54. static void mux_chip_release(struct device *dev)
  55. {
  56. struct mux_chip *mux_chip = to_mux_chip(dev);
  57. ida_free(&mux_ida, mux_chip->id);
  58. kfree(mux_chip);
  59. }
  60. static const struct device_type mux_type = {
  61. .name = "mux-chip",
  62. .release = mux_chip_release,
  63. };
  64. /**
  65. * mux_chip_alloc() - Allocate a mux-chip.
  66. * @dev: The parent device implementing the mux interface.
  67. * @controllers: The number of mux controllers to allocate for this chip.
  68. * @sizeof_priv: Size of extra memory area for private use by the caller.
  69. *
  70. * After allocating the mux-chip with the desired number of mux controllers
  71. * but before registering the chip, the mux driver is required to configure
  72. * the number of valid mux states in the mux_chip->mux[N].states members and
  73. * the desired idle state in the returned mux_chip->mux[N].idle_state members.
  74. * The default idle state is MUX_IDLE_AS_IS. The mux driver also needs to
  75. * provide a pointer to the operations struct in the mux_chip->ops member
  76. * before registering the mux-chip with mux_chip_register.
  77. *
  78. * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
  79. */
  80. struct mux_chip *mux_chip_alloc(struct device *dev,
  81. unsigned int controllers, size_t sizeof_priv)
  82. {
  83. struct mux_chip *mux_chip;
  84. int i;
  85. if (WARN_ON(!dev || !controllers))
  86. return ERR_PTR(-EINVAL);
  87. mux_chip = kzalloc(size_add(struct_size(mux_chip, mux, controllers),
  88. sizeof_priv),
  89. GFP_KERNEL);
  90. if (!mux_chip)
  91. return ERR_PTR(-ENOMEM);
  92. mux_chip->dev.class = &mux_class;
  93. mux_chip->dev.type = &mux_type;
  94. mux_chip->dev.parent = dev;
  95. mux_chip->dev.of_node = dev->of_node;
  96. dev_set_drvdata(&mux_chip->dev, mux_chip);
  97. mux_chip->id = ida_alloc(&mux_ida, GFP_KERNEL);
  98. if (mux_chip->id < 0) {
  99. int err = mux_chip->id;
  100. pr_err("muxchipX failed to get a device id\n");
  101. kfree(mux_chip);
  102. return ERR_PTR(err);
  103. }
  104. dev_set_name(&mux_chip->dev, "muxchip%d", mux_chip->id);
  105. mux_chip->controllers = controllers;
  106. for (i = 0; i < controllers; ++i) {
  107. struct mux_control *mux = &mux_chip->mux[i];
  108. mux->chip = mux_chip;
  109. sema_init(&mux->lock, 1);
  110. mux->cached_state = MUX_CACHE_UNKNOWN;
  111. mux->idle_state = MUX_IDLE_AS_IS;
  112. mux->last_change = ktime_get();
  113. }
  114. device_initialize(&mux_chip->dev);
  115. return mux_chip;
  116. }
  117. EXPORT_SYMBOL_GPL(mux_chip_alloc);
  118. static int mux_control_set(struct mux_control *mux, int state)
  119. {
  120. int ret = mux->chip->ops->set(mux, state);
  121. mux->cached_state = ret < 0 ? MUX_CACHE_UNKNOWN : state;
  122. if (ret >= 0)
  123. mux->last_change = ktime_get();
  124. return ret;
  125. }
  126. /**
  127. * mux_chip_register() - Register a mux-chip, thus readying the controllers
  128. * for use.
  129. * @mux_chip: The mux-chip to register.
  130. *
  131. * Do not retry registration of the same mux-chip on failure. You should
  132. * instead put it away with mux_chip_free() and allocate a new one, if you
  133. * for some reason would like to retry registration.
  134. *
  135. * Return: Zero on success or a negative errno on error.
  136. */
  137. int mux_chip_register(struct mux_chip *mux_chip)
  138. {
  139. int i;
  140. int ret;
  141. for (i = 0; i < mux_chip->controllers; ++i) {
  142. struct mux_control *mux = &mux_chip->mux[i];
  143. if (mux->idle_state == mux->cached_state)
  144. continue;
  145. ret = mux_control_set(mux, mux->idle_state);
  146. if (ret < 0) {
  147. dev_err(&mux_chip->dev, "unable to set idle state\n");
  148. return ret;
  149. }
  150. }
  151. ret = device_add(&mux_chip->dev);
  152. if (ret < 0)
  153. dev_err(&mux_chip->dev,
  154. "device_add failed in %s: %d\n", __func__, ret);
  155. return ret;
  156. }
  157. EXPORT_SYMBOL_GPL(mux_chip_register);
  158. /**
  159. * mux_chip_unregister() - Take the mux-chip off-line.
  160. * @mux_chip: The mux-chip to unregister.
  161. *
  162. * mux_chip_unregister() reverses the effects of mux_chip_register().
  163. * But not completely, you should not try to call mux_chip_register()
  164. * on a mux-chip that has been registered before.
  165. */
  166. void mux_chip_unregister(struct mux_chip *mux_chip)
  167. {
  168. device_del(&mux_chip->dev);
  169. }
  170. EXPORT_SYMBOL_GPL(mux_chip_unregister);
  171. /**
  172. * mux_chip_free() - Free the mux-chip for good.
  173. * @mux_chip: The mux-chip to free.
  174. *
  175. * mux_chip_free() reverses the effects of mux_chip_alloc().
  176. */
  177. void mux_chip_free(struct mux_chip *mux_chip)
  178. {
  179. if (!mux_chip)
  180. return;
  181. put_device(&mux_chip->dev);
  182. }
  183. EXPORT_SYMBOL_GPL(mux_chip_free);
  184. static void devm_mux_chip_release(struct device *dev, void *res)
  185. {
  186. struct mux_chip *mux_chip = *(struct mux_chip **)res;
  187. mux_chip_free(mux_chip);
  188. }
  189. /**
  190. * devm_mux_chip_alloc() - Resource-managed version of mux_chip_alloc().
  191. * @dev: The parent device implementing the mux interface.
  192. * @controllers: The number of mux controllers to allocate for this chip.
  193. * @sizeof_priv: Size of extra memory area for private use by the caller.
  194. *
  195. * See mux_chip_alloc() for more details.
  196. *
  197. * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
  198. */
  199. struct mux_chip *devm_mux_chip_alloc(struct device *dev,
  200. unsigned int controllers,
  201. size_t sizeof_priv)
  202. {
  203. struct mux_chip **ptr, *mux_chip;
  204. ptr = devres_alloc(devm_mux_chip_release, sizeof(*ptr), GFP_KERNEL);
  205. if (!ptr)
  206. return ERR_PTR(-ENOMEM);
  207. mux_chip = mux_chip_alloc(dev, controllers, sizeof_priv);
  208. if (IS_ERR(mux_chip)) {
  209. devres_free(ptr);
  210. return mux_chip;
  211. }
  212. *ptr = mux_chip;
  213. devres_add(dev, ptr);
  214. return mux_chip;
  215. }
  216. EXPORT_SYMBOL_GPL(devm_mux_chip_alloc);
  217. static void devm_mux_chip_reg_release(struct device *dev, void *res)
  218. {
  219. struct mux_chip *mux_chip = *(struct mux_chip **)res;
  220. mux_chip_unregister(mux_chip);
  221. }
  222. /**
  223. * devm_mux_chip_register() - Resource-managed version mux_chip_register().
  224. * @dev: The parent device implementing the mux interface.
  225. * @mux_chip: The mux-chip to register.
  226. *
  227. * See mux_chip_register() for more details.
  228. *
  229. * Return: Zero on success or a negative errno on error.
  230. */
  231. int devm_mux_chip_register(struct device *dev,
  232. struct mux_chip *mux_chip)
  233. {
  234. struct mux_chip **ptr;
  235. int res;
  236. ptr = devres_alloc(devm_mux_chip_reg_release, sizeof(*ptr), GFP_KERNEL);
  237. if (!ptr)
  238. return -ENOMEM;
  239. res = mux_chip_register(mux_chip);
  240. if (res) {
  241. devres_free(ptr);
  242. return res;
  243. }
  244. *ptr = mux_chip;
  245. devres_add(dev, ptr);
  246. return res;
  247. }
  248. EXPORT_SYMBOL_GPL(devm_mux_chip_register);
  249. /**
  250. * mux_control_states() - Query the number of multiplexer states.
  251. * @mux: The mux-control to query.
  252. *
  253. * Return: The number of multiplexer states.
  254. */
  255. unsigned int mux_control_states(struct mux_control *mux)
  256. {
  257. return mux->states;
  258. }
  259. EXPORT_SYMBOL_GPL(mux_control_states);
  260. /*
  261. * The mux->lock must be down when calling this function.
  262. */
  263. static int __mux_control_select(struct mux_control *mux, int state)
  264. {
  265. int ret;
  266. if (WARN_ON(state < 0 || state >= mux->states))
  267. return -EINVAL;
  268. if (mux->cached_state == state)
  269. return 0;
  270. ret = mux_control_set(mux, state);
  271. if (ret >= 0)
  272. return 0;
  273. /* The mux update failed, try to revert if appropriate... */
  274. if (mux->idle_state != MUX_IDLE_AS_IS)
  275. mux_control_set(mux, mux->idle_state);
  276. return ret;
  277. }
  278. static void mux_control_delay(struct mux_control *mux, unsigned int delay_us)
  279. {
  280. ktime_t delayend;
  281. s64 remaining;
  282. if (!delay_us)
  283. return;
  284. delayend = ktime_add_us(mux->last_change, delay_us);
  285. remaining = ktime_us_delta(delayend, ktime_get());
  286. if (remaining > 0)
  287. fsleep(remaining);
  288. }
  289. /**
  290. * mux_control_select_delay() - Select the given multiplexer state.
  291. * @mux: The mux-control to request a change of state from.
  292. * @state: The new requested state.
  293. * @delay_us: The time to delay (in microseconds) if the mux state is changed.
  294. *
  295. * On successfully selecting the mux-control state, it will be locked until
  296. * there is a call to mux_control_deselect(). If the mux-control is already
  297. * selected when mux_control_select() is called, the caller will be blocked
  298. * until mux_control_deselect() or mux_state_deselect() is called (by someone
  299. * else).
  300. *
  301. * Therefore, make sure to call mux_control_deselect() when the operation is
  302. * complete and the mux-control is free for others to use, but do not call
  303. * mux_control_deselect() if mux_control_select() fails.
  304. *
  305. * Return: 0 when the mux-control state has the requested state or a negative
  306. * errno on error.
  307. */
  308. int mux_control_select_delay(struct mux_control *mux, unsigned int state,
  309. unsigned int delay_us)
  310. {
  311. int ret;
  312. ret = down_killable(&mux->lock);
  313. if (ret < 0)
  314. return ret;
  315. ret = __mux_control_select(mux, state);
  316. if (ret >= 0)
  317. mux_control_delay(mux, delay_us);
  318. if (ret < 0)
  319. up(&mux->lock);
  320. return ret;
  321. }
  322. EXPORT_SYMBOL_GPL(mux_control_select_delay);
  323. /**
  324. * mux_state_select_delay() - Select the given multiplexer state.
  325. * @mstate: The mux-state to select.
  326. * @delay_us: The time to delay (in microseconds) if the mux state is changed.
  327. *
  328. * On successfully selecting the mux-state, its mux-control will be locked
  329. * until there is a call to mux_state_deselect(). If the mux-control is already
  330. * selected when mux_state_select() is called, the caller will be blocked
  331. * until mux_state_deselect() or mux_control_deselect() is called (by someone
  332. * else).
  333. *
  334. * Therefore, make sure to call mux_state_deselect() when the operation is
  335. * complete and the mux-control is free for others to use, but do not call
  336. * mux_state_deselect() if mux_state_select() fails.
  337. *
  338. * Return: 0 when the mux-state has been selected or a negative
  339. * errno on error.
  340. */
  341. int mux_state_select_delay(struct mux_state *mstate, unsigned int delay_us)
  342. {
  343. return mux_control_select_delay(mstate->mux, mstate->state, delay_us);
  344. }
  345. EXPORT_SYMBOL_GPL(mux_state_select_delay);
  346. /**
  347. * mux_control_try_select_delay() - Try to select the given multiplexer state.
  348. * @mux: The mux-control to request a change of state from.
  349. * @state: The new requested state.
  350. * @delay_us: The time to delay (in microseconds) if the mux state is changed.
  351. *
  352. * On successfully selecting the mux-control state, it will be locked until
  353. * mux_control_deselect() is called.
  354. *
  355. * Therefore, make sure to call mux_control_deselect() when the operation is
  356. * complete and the mux-control is free for others to use, but do not call
  357. * mux_control_deselect() if mux_control_try_select() fails.
  358. *
  359. * Return: 0 when the mux-control state has the requested state or a negative
  360. * errno on error. Specifically -EBUSY if the mux-control is contended.
  361. */
  362. int mux_control_try_select_delay(struct mux_control *mux, unsigned int state,
  363. unsigned int delay_us)
  364. {
  365. int ret;
  366. if (down_trylock(&mux->lock))
  367. return -EBUSY;
  368. ret = __mux_control_select(mux, state);
  369. if (ret >= 0)
  370. mux_control_delay(mux, delay_us);
  371. if (ret < 0)
  372. up(&mux->lock);
  373. return ret;
  374. }
  375. EXPORT_SYMBOL_GPL(mux_control_try_select_delay);
  376. /**
  377. * mux_state_try_select_delay() - Try to select the given multiplexer state.
  378. * @mstate: The mux-state to select.
  379. * @delay_us: The time to delay (in microseconds) if the mux state is changed.
  380. *
  381. * On successfully selecting the mux-state, its mux-control will be locked
  382. * until mux_state_deselect() is called.
  383. *
  384. * Therefore, make sure to call mux_state_deselect() when the operation is
  385. * complete and the mux-control is free for others to use, but do not call
  386. * mux_state_deselect() if mux_state_try_select() fails.
  387. *
  388. * Return: 0 when the mux-state has been selected or a negative errno on
  389. * error. Specifically -EBUSY if the mux-control is contended.
  390. */
  391. int mux_state_try_select_delay(struct mux_state *mstate, unsigned int delay_us)
  392. {
  393. return mux_control_try_select_delay(mstate->mux, mstate->state, delay_us);
  394. }
  395. EXPORT_SYMBOL_GPL(mux_state_try_select_delay);
  396. /**
  397. * mux_control_deselect() - Deselect the previously selected multiplexer state.
  398. * @mux: The mux-control to deselect.
  399. *
  400. * It is required that a single call is made to mux_control_deselect() for
  401. * each and every successful call made to either of mux_control_select() or
  402. * mux_control_try_select().
  403. *
  404. * Return: 0 on success and a negative errno on error. An error can only
  405. * occur if the mux has an idle state. Note that even if an error occurs, the
  406. * mux-control is unlocked and is thus free for the next access.
  407. */
  408. int mux_control_deselect(struct mux_control *mux)
  409. {
  410. int ret = 0;
  411. if (mux->idle_state != MUX_IDLE_AS_IS &&
  412. mux->idle_state != mux->cached_state)
  413. ret = mux_control_set(mux, mux->idle_state);
  414. up(&mux->lock);
  415. return ret;
  416. }
  417. EXPORT_SYMBOL_GPL(mux_control_deselect);
  418. /**
  419. * mux_state_deselect() - Deselect the previously selected multiplexer state.
  420. * @mstate: The mux-state to deselect.
  421. *
  422. * It is required that a single call is made to mux_state_deselect() for
  423. * each and every successful call made to either of mux_state_select() or
  424. * mux_state_try_select().
  425. *
  426. * Return: 0 on success and a negative errno on error. An error can only
  427. * occur if the mux has an idle state. Note that even if an error occurs, the
  428. * mux-control is unlocked and is thus free for the next access.
  429. */
  430. int mux_state_deselect(struct mux_state *mstate)
  431. {
  432. return mux_control_deselect(mstate->mux);
  433. }
  434. EXPORT_SYMBOL_GPL(mux_state_deselect);
  435. /* Note this function returns a reference to the mux_chip dev. */
  436. static struct mux_chip *of_find_mux_chip_by_node(struct device_node *np)
  437. {
  438. struct device *dev;
  439. dev = class_find_device_by_of_node(&mux_class, np);
  440. return dev ? to_mux_chip(dev) : NULL;
  441. }
  442. /*
  443. * mux_get() - Get the mux-control for a device.
  444. * @dev: The device that needs a mux-control.
  445. * @mux_name: The name identifying the mux-control.
  446. * @state: Pointer to where the requested state is returned, or NULL when
  447. * the required multiplexer states are handled by other means.
  448. *
  449. * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno.
  450. */
  451. static struct mux_control *mux_get(struct device *dev, const char *mux_name,
  452. unsigned int *state)
  453. {
  454. struct device_node *np = dev->of_node;
  455. struct of_phandle_args args;
  456. struct mux_chip *mux_chip;
  457. unsigned int controller;
  458. int index = 0;
  459. int ret;
  460. if (mux_name) {
  461. if (state)
  462. index = of_property_match_string(np, "mux-state-names",
  463. mux_name);
  464. else
  465. index = of_property_match_string(np, "mux-control-names",
  466. mux_name);
  467. if (index < 0) {
  468. dev_err(dev, "mux controller '%s' not found\n",
  469. mux_name);
  470. return ERR_PTR(index);
  471. }
  472. }
  473. if (state)
  474. ret = of_parse_phandle_with_args(np,
  475. "mux-states", "#mux-state-cells",
  476. index, &args);
  477. else
  478. ret = of_parse_phandle_with_args(np,
  479. "mux-controls", "#mux-control-cells",
  480. index, &args);
  481. if (ret) {
  482. dev_err(dev, "%pOF: failed to get mux-%s %s(%i)\n",
  483. np, state ? "state" : "control", mux_name ?: "", index);
  484. return ERR_PTR(ret);
  485. }
  486. mux_chip = of_find_mux_chip_by_node(args.np);
  487. of_node_put(args.np);
  488. if (!mux_chip)
  489. return ERR_PTR(-EPROBE_DEFER);
  490. controller = 0;
  491. if (state) {
  492. if (args.args_count > 2 || args.args_count == 0 ||
  493. (args.args_count < 2 && mux_chip->controllers > 1)) {
  494. dev_err(dev, "%pOF: wrong #mux-state-cells for %pOF\n",
  495. np, args.np);
  496. put_device(&mux_chip->dev);
  497. return ERR_PTR(-EINVAL);
  498. }
  499. if (args.args_count == 2) {
  500. controller = args.args[0];
  501. *state = args.args[1];
  502. } else {
  503. *state = args.args[0];
  504. }
  505. } else {
  506. if (args.args_count > 1 ||
  507. (!args.args_count && mux_chip->controllers > 1)) {
  508. dev_err(dev, "%pOF: wrong #mux-control-cells for %pOF\n",
  509. np, args.np);
  510. put_device(&mux_chip->dev);
  511. return ERR_PTR(-EINVAL);
  512. }
  513. if (args.args_count)
  514. controller = args.args[0];
  515. }
  516. if (controller >= mux_chip->controllers) {
  517. dev_err(dev, "%pOF: bad mux controller %u specified in %pOF\n",
  518. np, controller, args.np);
  519. put_device(&mux_chip->dev);
  520. return ERR_PTR(-EINVAL);
  521. }
  522. return &mux_chip->mux[controller];
  523. }
  524. /**
  525. * mux_control_get() - Get the mux-control for a device.
  526. * @dev: The device that needs a mux-control.
  527. * @mux_name: The name identifying the mux-control.
  528. *
  529. * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno.
  530. */
  531. struct mux_control *mux_control_get(struct device *dev, const char *mux_name)
  532. {
  533. return mux_get(dev, mux_name, NULL);
  534. }
  535. EXPORT_SYMBOL_GPL(mux_control_get);
  536. /**
  537. * mux_control_put() - Put away the mux-control for good.
  538. * @mux: The mux-control to put away.
  539. *
  540. * mux_control_put() reverses the effects of mux_control_get().
  541. */
  542. void mux_control_put(struct mux_control *mux)
  543. {
  544. put_device(&mux->chip->dev);
  545. }
  546. EXPORT_SYMBOL_GPL(mux_control_put);
  547. static void devm_mux_control_release(struct device *dev, void *res)
  548. {
  549. struct mux_control *mux = *(struct mux_control **)res;
  550. mux_control_put(mux);
  551. }
  552. /**
  553. * devm_mux_control_get() - Get the mux-control for a device, with resource
  554. * management.
  555. * @dev: The device that needs a mux-control.
  556. * @mux_name: The name identifying the mux-control.
  557. *
  558. * Return: Pointer to the mux-control, or an ERR_PTR with a negative errno.
  559. */
  560. struct mux_control *devm_mux_control_get(struct device *dev,
  561. const char *mux_name)
  562. {
  563. struct mux_control **ptr, *mux;
  564. ptr = devres_alloc(devm_mux_control_release, sizeof(*ptr), GFP_KERNEL);
  565. if (!ptr)
  566. return ERR_PTR(-ENOMEM);
  567. mux = mux_control_get(dev, mux_name);
  568. if (IS_ERR(mux)) {
  569. devres_free(ptr);
  570. return mux;
  571. }
  572. *ptr = mux;
  573. devres_add(dev, ptr);
  574. return mux;
  575. }
  576. EXPORT_SYMBOL_GPL(devm_mux_control_get);
  577. /*
  578. * mux_state_get() - Get the mux-state for a device.
  579. * @dev: The device that needs a mux-state.
  580. * @mux_name: The name identifying the mux-state.
  581. *
  582. * Return: A pointer to the mux-state, or an ERR_PTR with a negative errno.
  583. */
  584. static struct mux_state *mux_state_get(struct device *dev, const char *mux_name)
  585. {
  586. struct mux_state *mstate;
  587. mstate = kzalloc_obj(*mstate);
  588. if (!mstate)
  589. return ERR_PTR(-ENOMEM);
  590. mstate->mux = mux_get(dev, mux_name, &mstate->state);
  591. if (IS_ERR(mstate->mux)) {
  592. int err = PTR_ERR(mstate->mux);
  593. kfree(mstate);
  594. return ERR_PTR(err);
  595. }
  596. return mstate;
  597. }
  598. /*
  599. * mux_state_put() - Put away the mux-state for good.
  600. * @mstate: The mux-state to put away.
  601. *
  602. * mux_state_put() reverses the effects of mux_state_get().
  603. */
  604. static void mux_state_put(struct mux_state *mstate)
  605. {
  606. mux_control_put(mstate->mux);
  607. kfree(mstate);
  608. }
  609. static void devm_mux_state_release(struct device *dev, void *res)
  610. {
  611. struct mux_state *mstate = *(struct mux_state **)res;
  612. mux_state_put(mstate);
  613. }
  614. /**
  615. * devm_mux_state_get() - Get the mux-state for a device, with resource
  616. * management.
  617. * @dev: The device that needs a mux-control.
  618. * @mux_name: The name identifying the mux-control.
  619. *
  620. * Return: Pointer to the mux-state, or an ERR_PTR with a negative errno.
  621. */
  622. struct mux_state *devm_mux_state_get(struct device *dev,
  623. const char *mux_name)
  624. {
  625. struct mux_state **ptr, *mstate;
  626. ptr = devres_alloc(devm_mux_state_release, sizeof(*ptr), GFP_KERNEL);
  627. if (!ptr)
  628. return ERR_PTR(-ENOMEM);
  629. mstate = mux_state_get(dev, mux_name);
  630. if (IS_ERR(mstate)) {
  631. devres_free(ptr);
  632. return mstate;
  633. }
  634. *ptr = mstate;
  635. devres_add(dev, ptr);
  636. return mstate;
  637. }
  638. EXPORT_SYMBOL_GPL(devm_mux_state_get);
  639. /*
  640. * Using subsys_initcall instead of module_init here to try to ensure - for
  641. * the non-modular case - that the subsystem is initialized when mux consumers
  642. * and mux controllers start to use it.
  643. * For the modular case, the ordering is ensured with module dependencies.
  644. */
  645. subsys_initcall(mux_init);
  646. module_exit(mux_exit);
  647. MODULE_DESCRIPTION("Multiplexer subsystem");
  648. MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
  649. MODULE_LICENSE("GPL v2");