core.c 14 KB

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