core.c 20 KB

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