core.c 20 KB

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