core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /*
  2. * Multiplexer subsystem
  3. *
  4. * Copyright (C) 2017 Axentia Technologies AB
  5. *
  6. * Author: Peter Rosin <peda@axentia.se>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #define pr_fmt(fmt) "mux-core: " fmt
  13. #include <linux/device.h>
  14. #include <linux/err.h>
  15. #include <linux/export.h>
  16. #include <linux/idr.h>
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/mux/consumer.h>
  20. #include <linux/mux/driver.h>
  21. #include <linux/of.h>
  22. #include <linux/of_platform.h>
  23. #include <linux/slab.h>
  24. /*
  25. * The idle-as-is "state" is not an actual state that may be selected, it
  26. * only implies that the state should not be changed. So, use that state
  27. * as indication that the cached state of the multiplexer is unknown.
  28. */
  29. #define MUX_CACHE_UNKNOWN MUX_IDLE_AS_IS
  30. static struct class mux_class = {
  31. .name = "mux",
  32. .owner = THIS_MODULE,
  33. };
  34. static DEFINE_IDA(mux_ida);
  35. static int __init mux_init(void)
  36. {
  37. ida_init(&mux_ida);
  38. return class_register(&mux_class);
  39. }
  40. static void __exit mux_exit(void)
  41. {
  42. class_unregister(&mux_class);
  43. ida_destroy(&mux_ida);
  44. }
  45. static void mux_chip_release(struct device *dev)
  46. {
  47. struct mux_chip *mux_chip = to_mux_chip(dev);
  48. ida_simple_remove(&mux_ida, mux_chip->id);
  49. kfree(mux_chip);
  50. }
  51. static const struct device_type mux_type = {
  52. .name = "mux-chip",
  53. .release = mux_chip_release,
  54. };
  55. /**
  56. * mux_chip_alloc() - Allocate a mux-chip.
  57. * @dev: The parent device implementing the mux interface.
  58. * @controllers: The number of mux controllers to allocate for this chip.
  59. * @sizeof_priv: Size of extra memory area for private use by the caller.
  60. *
  61. * After allocating the mux-chip with the desired number of mux controllers
  62. * but before registering the chip, the mux driver is required to configure
  63. * the number of valid mux states in the mux_chip->mux[N].states members and
  64. * the desired idle state in the returned mux_chip->mux[N].idle_state members.
  65. * The default idle state is MUX_IDLE_AS_IS. The mux driver also needs to
  66. * provide a pointer to the operations struct in the mux_chip->ops member
  67. * before registering the mux-chip with mux_chip_register.
  68. *
  69. * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
  70. */
  71. struct mux_chip *mux_chip_alloc(struct device *dev,
  72. unsigned int controllers, size_t sizeof_priv)
  73. {
  74. struct mux_chip *mux_chip;
  75. int i;
  76. if (WARN_ON(!dev || !controllers))
  77. return ERR_PTR(-EINVAL);
  78. mux_chip = kzalloc(sizeof(*mux_chip) +
  79. controllers * sizeof(*mux_chip->mux) +
  80. sizeof_priv, GFP_KERNEL);
  81. if (!mux_chip)
  82. return ERR_PTR(-ENOMEM);
  83. mux_chip->mux = (struct mux_control *)(mux_chip + 1);
  84. mux_chip->dev.class = &mux_class;
  85. mux_chip->dev.type = &mux_type;
  86. mux_chip->dev.parent = dev;
  87. mux_chip->dev.of_node = dev->of_node;
  88. dev_set_drvdata(&mux_chip->dev, mux_chip);
  89. mux_chip->id = ida_simple_get(&mux_ida, 0, 0, GFP_KERNEL);
  90. if (mux_chip->id < 0) {
  91. int err = mux_chip->id;
  92. pr_err("muxchipX failed to get a device id\n");
  93. kfree(mux_chip);
  94. return ERR_PTR(err);
  95. }
  96. dev_set_name(&mux_chip->dev, "muxchip%d", mux_chip->id);
  97. mux_chip->controllers = controllers;
  98. for (i = 0; i < controllers; ++i) {
  99. struct mux_control *mux = &mux_chip->mux[i];
  100. mux->chip = mux_chip;
  101. sema_init(&mux->lock, 1);
  102. mux->cached_state = MUX_CACHE_UNKNOWN;
  103. mux->idle_state = MUX_IDLE_AS_IS;
  104. }
  105. device_initialize(&mux_chip->dev);
  106. return mux_chip;
  107. }
  108. EXPORT_SYMBOL_GPL(mux_chip_alloc);
  109. static int mux_control_set(struct mux_control *mux, int state)
  110. {
  111. int ret = mux->chip->ops->set(mux, state);
  112. mux->cached_state = ret < 0 ? MUX_CACHE_UNKNOWN : state;
  113. return ret;
  114. }
  115. /**
  116. * mux_chip_register() - Register a mux-chip, thus readying the controllers
  117. * for use.
  118. * @mux_chip: The mux-chip to register.
  119. *
  120. * Do not retry registration of the same mux-chip on failure. You should
  121. * instead put it away with mux_chip_free() and allocate a new one, if you
  122. * for some reason would like to retry registration.
  123. *
  124. * Return: Zero on success or a negative errno on error.
  125. */
  126. int mux_chip_register(struct mux_chip *mux_chip)
  127. {
  128. int i;
  129. int ret;
  130. for (i = 0; i < mux_chip->controllers; ++i) {
  131. struct mux_control *mux = &mux_chip->mux[i];
  132. if (mux->idle_state == mux->cached_state)
  133. continue;
  134. ret = mux_control_set(mux, mux->idle_state);
  135. if (ret < 0) {
  136. dev_err(&mux_chip->dev, "unable to set idle state\n");
  137. return ret;
  138. }
  139. }
  140. ret = device_add(&mux_chip->dev);
  141. if (ret < 0)
  142. dev_err(&mux_chip->dev,
  143. "device_add failed in %s: %d\n", __func__, ret);
  144. return ret;
  145. }
  146. EXPORT_SYMBOL_GPL(mux_chip_register);
  147. /**
  148. * mux_chip_unregister() - Take the mux-chip off-line.
  149. * @mux_chip: The mux-chip to unregister.
  150. *
  151. * mux_chip_unregister() reverses the effects of mux_chip_register().
  152. * But not completely, you should not try to call mux_chip_register()
  153. * on a mux-chip that has been registered before.
  154. */
  155. void mux_chip_unregister(struct mux_chip *mux_chip)
  156. {
  157. device_del(&mux_chip->dev);
  158. }
  159. EXPORT_SYMBOL_GPL(mux_chip_unregister);
  160. /**
  161. * mux_chip_free() - Free the mux-chip for good.
  162. * @mux_chip: The mux-chip to free.
  163. *
  164. * mux_chip_free() reverses the effects of mux_chip_alloc().
  165. */
  166. void mux_chip_free(struct mux_chip *mux_chip)
  167. {
  168. if (!mux_chip)
  169. return;
  170. put_device(&mux_chip->dev);
  171. }
  172. EXPORT_SYMBOL_GPL(mux_chip_free);
  173. static void devm_mux_chip_release(struct device *dev, void *res)
  174. {
  175. struct mux_chip *mux_chip = *(struct mux_chip **)res;
  176. mux_chip_free(mux_chip);
  177. }
  178. /**
  179. * devm_mux_chip_alloc() - Resource-managed version of mux_chip_alloc().
  180. * @dev: The parent device implementing the mux interface.
  181. * @controllers: The number of mux controllers to allocate for this chip.
  182. * @sizeof_priv: Size of extra memory area for private use by the caller.
  183. *
  184. * See mux_chip_alloc() for more details.
  185. *
  186. * Return: A pointer to the new mux-chip, or an ERR_PTR with a negative errno.
  187. */
  188. struct mux_chip *devm_mux_chip_alloc(struct device *dev,
  189. unsigned int controllers,
  190. size_t sizeof_priv)
  191. {
  192. struct mux_chip **ptr, *mux_chip;
  193. ptr = devres_alloc(devm_mux_chip_release, sizeof(*ptr), GFP_KERNEL);
  194. if (!ptr)
  195. return ERR_PTR(-ENOMEM);
  196. mux_chip = mux_chip_alloc(dev, controllers, sizeof_priv);
  197. if (IS_ERR(mux_chip)) {
  198. devres_free(ptr);
  199. return mux_chip;
  200. }
  201. *ptr = mux_chip;
  202. devres_add(dev, ptr);
  203. return mux_chip;
  204. }
  205. EXPORT_SYMBOL_GPL(devm_mux_chip_alloc);
  206. static void devm_mux_chip_reg_release(struct device *dev, void *res)
  207. {
  208. struct mux_chip *mux_chip = *(struct mux_chip **)res;
  209. mux_chip_unregister(mux_chip);
  210. }
  211. /**
  212. * devm_mux_chip_register() - Resource-managed version mux_chip_register().
  213. * @dev: The parent device implementing the mux interface.
  214. * @mux_chip: The mux-chip to register.
  215. *
  216. * See mux_chip_register() for more details.
  217. *
  218. * Return: Zero on success or a negative errno on error.
  219. */
  220. int devm_mux_chip_register(struct device *dev,
  221. struct mux_chip *mux_chip)
  222. {
  223. struct mux_chip **ptr;
  224. int res;
  225. ptr = devres_alloc(devm_mux_chip_reg_release, sizeof(*ptr), GFP_KERNEL);
  226. if (!ptr)
  227. return -ENOMEM;
  228. res = mux_chip_register(mux_chip);
  229. if (res) {
  230. devres_free(ptr);
  231. return res;
  232. }
  233. *ptr = mux_chip;
  234. devres_add(dev, ptr);
  235. return res;
  236. }
  237. EXPORT_SYMBOL_GPL(devm_mux_chip_register);
  238. /**
  239. * mux_control_states() - Query the number of multiplexer states.
  240. * @mux: The mux-control to query.
  241. *
  242. * Return: The number of multiplexer states.
  243. */
  244. unsigned int mux_control_states(struct mux_control *mux)
  245. {
  246. return mux->states;
  247. }
  248. EXPORT_SYMBOL_GPL(mux_control_states);
  249. /*
  250. * The mux->lock must be down when calling this function.
  251. */
  252. static int __mux_control_select(struct mux_control *mux, int state)
  253. {
  254. int ret;
  255. if (WARN_ON(state < 0 || state >= mux->states))
  256. return -EINVAL;
  257. if (mux->cached_state == state)
  258. return 0;
  259. ret = mux_control_set(mux, state);
  260. if (ret >= 0)
  261. return 0;
  262. /* The mux update failed, try to revert if appropriate... */
  263. if (mux->idle_state != MUX_IDLE_AS_IS)
  264. mux_control_set(mux, mux->idle_state);
  265. return ret;
  266. }
  267. /**
  268. * mux_control_select() - Select the given multiplexer state.
  269. * @mux: The mux-control to request a change of state from.
  270. * @state: The new requested state.
  271. *
  272. * On successfully selecting the mux-control state, it will be locked until
  273. * there is a call to mux_control_deselect(). If the mux-control is already
  274. * selected when mux_control_select() is called, the caller will be blocked
  275. * until mux_control_deselect() is called (by someone else).
  276. *
  277. * Therefore, make sure to call mux_control_deselect() when the operation is
  278. * complete and the mux-control is free for others to use, but do not call
  279. * mux_control_deselect() if mux_control_select() fails.
  280. *
  281. * Return: 0 when the mux-control state has the requested state or a negative
  282. * errno on error.
  283. */
  284. int mux_control_select(struct mux_control *mux, unsigned int state)
  285. {
  286. int ret;
  287. ret = down_killable(&mux->lock);
  288. if (ret < 0)
  289. return ret;
  290. ret = __mux_control_select(mux, state);
  291. if (ret < 0)
  292. up(&mux->lock);
  293. return ret;
  294. }
  295. EXPORT_SYMBOL_GPL(mux_control_select);
  296. /**
  297. * mux_control_try_select() - Try to select the given multiplexer state.
  298. * @mux: The mux-control to request a change of state from.
  299. * @state: The new requested state.
  300. *
  301. * On successfully selecting the mux-control state, it will be locked until
  302. * mux_control_deselect() called.
  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_try_select() fails.
  307. *
  308. * Return: 0 when the mux-control state has the requested state or a negative
  309. * errno on error. Specifically -EBUSY if the mux-control is contended.
  310. */
  311. int mux_control_try_select(struct mux_control *mux, unsigned int state)
  312. {
  313. int ret;
  314. if (down_trylock(&mux->lock))
  315. return -EBUSY;
  316. ret = __mux_control_select(mux, state);
  317. if (ret < 0)
  318. up(&mux->lock);
  319. return ret;
  320. }
  321. EXPORT_SYMBOL_GPL(mux_control_try_select);
  322. /**
  323. * mux_control_deselect() - Deselect the previously selected multiplexer state.
  324. * @mux: The mux-control to deselect.
  325. *
  326. * It is required that a single call is made to mux_control_deselect() for
  327. * each and every successful call made to either of mux_control_select() or
  328. * mux_control_try_select().
  329. *
  330. * Return: 0 on success and a negative errno on error. An error can only
  331. * occur if the mux has an idle state. Note that even if an error occurs, the
  332. * mux-control is unlocked and is thus free for the next access.
  333. */
  334. int mux_control_deselect(struct mux_control *mux)
  335. {
  336. int ret = 0;
  337. if (mux->idle_state != MUX_IDLE_AS_IS &&
  338. mux->idle_state != mux->cached_state)
  339. ret = mux_control_set(mux, mux->idle_state);
  340. up(&mux->lock);
  341. return ret;
  342. }
  343. EXPORT_SYMBOL_GPL(mux_control_deselect);
  344. static int of_dev_node_match(struct device *dev, const void *data)
  345. {
  346. return dev->of_node == data;
  347. }
  348. static struct mux_chip *of_find_mux_chip_by_node(struct device_node *np)
  349. {
  350. struct device *dev;
  351. dev = class_find_device(&mux_class, NULL, np, of_dev_node_match);
  352. return dev ? to_mux_chip(dev) : NULL;
  353. }
  354. /**
  355. * mux_control_get() - Get the mux-control for a device.
  356. * @dev: The device that needs a mux-control.
  357. * @mux_name: The name identifying the mux-control.
  358. *
  359. * Return: A pointer to the mux-control, or an ERR_PTR with a negative errno.
  360. */
  361. struct mux_control *mux_control_get(struct device *dev, const char *mux_name)
  362. {
  363. struct device_node *np = dev->of_node;
  364. struct of_phandle_args args;
  365. struct mux_chip *mux_chip;
  366. unsigned int controller;
  367. int index = 0;
  368. int ret;
  369. if (mux_name) {
  370. index = of_property_match_string(np, "mux-control-names",
  371. mux_name);
  372. if (index < 0) {
  373. dev_err(dev, "mux controller '%s' not found\n",
  374. mux_name);
  375. return ERR_PTR(index);
  376. }
  377. }
  378. ret = of_parse_phandle_with_args(np,
  379. "mux-controls", "#mux-control-cells",
  380. index, &args);
  381. if (ret) {
  382. dev_err(dev, "%pOF: failed to get mux-control %s(%i)\n",
  383. np, mux_name ?: "", index);
  384. return ERR_PTR(ret);
  385. }
  386. mux_chip = of_find_mux_chip_by_node(args.np);
  387. of_node_put(args.np);
  388. if (!mux_chip)
  389. return ERR_PTR(-EPROBE_DEFER);
  390. if (args.args_count > 1 ||
  391. (!args.args_count && (mux_chip->controllers > 1))) {
  392. dev_err(dev, "%pOF: wrong #mux-control-cells for %pOF\n",
  393. np, args.np);
  394. return ERR_PTR(-EINVAL);
  395. }
  396. controller = 0;
  397. if (args.args_count)
  398. controller = args.args[0];
  399. if (controller >= mux_chip->controllers) {
  400. dev_err(dev, "%pOF: bad mux controller %u specified in %pOF\n",
  401. np, controller, args.np);
  402. return ERR_PTR(-EINVAL);
  403. }
  404. get_device(&mux_chip->dev);
  405. return &mux_chip->mux[controller];
  406. }
  407. EXPORT_SYMBOL_GPL(mux_control_get);
  408. /**
  409. * mux_control_put() - Put away the mux-control for good.
  410. * @mux: The mux-control to put away.
  411. *
  412. * mux_control_put() reverses the effects of mux_control_get().
  413. */
  414. void mux_control_put(struct mux_control *mux)
  415. {
  416. put_device(&mux->chip->dev);
  417. }
  418. EXPORT_SYMBOL_GPL(mux_control_put);
  419. static void devm_mux_control_release(struct device *dev, void *res)
  420. {
  421. struct mux_control *mux = *(struct mux_control **)res;
  422. mux_control_put(mux);
  423. }
  424. /**
  425. * devm_mux_control_get() - Get the mux-control for a device, with resource
  426. * management.
  427. * @dev: The device that needs a mux-control.
  428. * @mux_name: The name identifying the mux-control.
  429. *
  430. * Return: Pointer to the mux-control, or an ERR_PTR with a negative errno.
  431. */
  432. struct mux_control *devm_mux_control_get(struct device *dev,
  433. const char *mux_name)
  434. {
  435. struct mux_control **ptr, *mux;
  436. ptr = devres_alloc(devm_mux_control_release, sizeof(*ptr), GFP_KERNEL);
  437. if (!ptr)
  438. return ERR_PTR(-ENOMEM);
  439. mux = mux_control_get(dev, mux_name);
  440. if (IS_ERR(mux)) {
  441. devres_free(ptr);
  442. return mux;
  443. }
  444. *ptr = mux;
  445. devres_add(dev, ptr);
  446. return mux;
  447. }
  448. EXPORT_SYMBOL_GPL(devm_mux_control_get);
  449. /*
  450. * Using subsys_initcall instead of module_init here to try to ensure - for
  451. * the non-modular case - that the subsystem is initialized when mux consumers
  452. * and mux controllers start to use it.
  453. * For the modular case, the ordering is ensured with module dependencies.
  454. */
  455. subsys_initcall(mux_init);
  456. module_exit(mux_exit);
  457. MODULE_DESCRIPTION("Multiplexer subsystem");
  458. MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
  459. MODULE_LICENSE("GPL v2");