mux-uclass.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Multiplexer subsystem
  4. *
  5. * Based on the linux multiplexer framework
  6. *
  7. * Copyright (C) 2017 Axentia Technologies AB
  8. * Author: Peter Rosin <peda@axentia.se>
  9. *
  10. * Copyright (C) 2017-2018 Texas Instruments Incorporated - http://www.ti.com/
  11. * Jean-Jacques Hiblot <jjhiblot@ti.com>
  12. */
  13. #define LOG_CATEGORY UCLASS_MUX
  14. #include <common.h>
  15. #include <dm.h>
  16. #include <mux-internal.h>
  17. #include <dm/device-internal.h>
  18. #include <dm/device_compat.h>
  19. #include <dm/devres.h>
  20. #include <dt-bindings/mux/mux.h>
  21. #include <linux/bug.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. * mux_control_ops() - Get the mux_control ops.
  30. * @dev: The client device.
  31. *
  32. * Return: A pointer to the 'mux_control_ops' of the device.
  33. */
  34. static inline const struct mux_control_ops *mux_dev_ops(struct udevice *dev)
  35. {
  36. return (const struct mux_control_ops *)dev->driver->ops;
  37. }
  38. /**
  39. * mux_control_set() - Set the state of the given mux controller.
  40. * @mux: A multiplexer control
  41. * @state: The new requested state.
  42. *
  43. * Return: 0 if OK, or a negative error code.
  44. */
  45. static int mux_control_set(struct mux_control *mux, int state)
  46. {
  47. int ret = mux_dev_ops(mux->dev)->set(mux, state);
  48. mux->cached_state = ret < 0 ? MUX_CACHE_UNKNOWN : state;
  49. return ret;
  50. }
  51. unsigned int mux_control_states(struct mux_control *mux)
  52. {
  53. return mux->states;
  54. }
  55. /**
  56. * __mux_control_select() - Select the given multiplexer state.
  57. * @mux: The mux-control to request a change of state from.
  58. * @state: The new requested state.
  59. *
  60. * Try to set the mux to the requested state. If not, try to revert if
  61. * appropriate.
  62. */
  63. static int __mux_control_select(struct mux_control *mux, int state)
  64. {
  65. int ret;
  66. if (WARN_ON(state < 0 || state >= mux->states))
  67. return -EINVAL;
  68. if (mux->cached_state == state)
  69. return 0;
  70. ret = mux_control_set(mux, state);
  71. if (ret >= 0)
  72. return 0;
  73. /* The mux update failed, try to revert if appropriate... */
  74. if (mux->idle_state != MUX_IDLE_AS_IS)
  75. mux_control_set(mux, mux->idle_state);
  76. return ret;
  77. }
  78. int mux_control_select(struct mux_control *mux, unsigned int state)
  79. {
  80. int ret;
  81. if (mux->in_use)
  82. return -EBUSY;
  83. ret = __mux_control_select(mux, state);
  84. if (ret < 0)
  85. return ret;
  86. mux->in_use = true;
  87. return 0;
  88. }
  89. int mux_control_deselect(struct mux_control *mux)
  90. {
  91. int ret = 0;
  92. if (mux->idle_state != MUX_IDLE_AS_IS &&
  93. mux->idle_state != mux->cached_state)
  94. ret = mux_control_set(mux, mux->idle_state);
  95. mux->in_use = false;
  96. return ret;
  97. }
  98. static int mux_of_xlate_default(struct mux_chip *mux_chip,
  99. struct ofnode_phandle_args *args,
  100. struct mux_control **muxp)
  101. {
  102. struct mux_control *mux;
  103. int id;
  104. log_debug("%s(muxp=%p)\n", __func__, muxp);
  105. if (args->args_count > 1) {
  106. debug("Invaild args_count: %d\n", args->args_count);
  107. return -EINVAL;
  108. }
  109. if (args->args_count)
  110. id = args->args[0];
  111. else
  112. id = 0;
  113. if (id >= mux_chip->controllers) {
  114. pr_err("bad mux controller %u specified in %s\n",
  115. id, ofnode_get_name(args->node));
  116. return -ERANGE;
  117. }
  118. mux = &mux_chip->mux[id];
  119. mux->id = id;
  120. *muxp = mux;
  121. return 0;
  122. }
  123. /**
  124. * mux_get_by_indexed_prop() - Get a mux control by integer index
  125. * @dev: The client device.
  126. * @prop_name: Name of the device tree property.
  127. * @index: The index of the mux to get
  128. * @mux: A pointer to the 'mux_control' struct to initialize.
  129. *
  130. * Return: 0 of OK, -errno otherwise.
  131. */
  132. static int mux_get_by_indexed_prop(struct udevice *dev, const char *prop_name,
  133. int index, struct mux_control **mux)
  134. {
  135. int ret;
  136. struct ofnode_phandle_args args;
  137. struct udevice *dev_mux;
  138. const struct mux_control_ops *ops;
  139. struct mux_chip *mux_chip;
  140. log_debug("%s(dev=%p, index=%d, mux=%p)\n", __func__, dev, index, mux);
  141. ret = dev_read_phandle_with_args(dev, prop_name, "#mux-control-cells",
  142. 0, index, &args);
  143. if (ret) {
  144. debug("%s: fdtdec_parse_phandle_with_args failed: err=%d\n",
  145. __func__, ret);
  146. return ret;
  147. }
  148. ret = uclass_get_device_by_ofnode(UCLASS_MUX, args.node, &dev_mux);
  149. if (ret) {
  150. debug("%s: uclass_get_device_by_ofnode failed: err=%d\n",
  151. __func__, ret);
  152. return ret;
  153. }
  154. mux_chip = dev_get_uclass_priv(dev_mux);
  155. ops = mux_dev_ops(dev_mux);
  156. if (ops->of_xlate)
  157. ret = ops->of_xlate(mux_chip, &args, mux);
  158. else
  159. ret = mux_of_xlate_default(mux_chip, &args, mux);
  160. if (ret) {
  161. debug("of_xlate() failed: %d\n", ret);
  162. return ret;
  163. }
  164. (*mux)->dev = dev_mux;
  165. return 0;
  166. }
  167. int mux_get_by_index(struct udevice *dev, int index, struct mux_control **mux)
  168. {
  169. return mux_get_by_indexed_prop(dev, "mux-controls", index, mux);
  170. }
  171. int mux_control_get(struct udevice *dev, const char *name,
  172. struct mux_control **mux)
  173. {
  174. int index;
  175. debug("%s(dev=%p, name=%s, mux=%p)\n", __func__, dev, name, mux);
  176. index = dev_read_stringlist_search(dev, "mux-control-names", name);
  177. if (index < 0) {
  178. debug("fdt_stringlist_search() failed: %d\n", index);
  179. return index;
  180. }
  181. return mux_get_by_index(dev, index, mux);
  182. }
  183. void mux_control_put(struct mux_control *mux)
  184. {
  185. mux_control_deselect(mux);
  186. }
  187. /**
  188. * devm_mux_control_release() - Release the given managed mux.
  189. * @dev: The client device.
  190. * @res: Pointer to the mux to be released.
  191. *
  192. * This function is called by devres to release the mux. It reverses the
  193. * effects of mux_control_get().
  194. */
  195. static void devm_mux_control_release(struct udevice *dev, void *res)
  196. {
  197. mux_control_put(*(struct mux_control **)res);
  198. }
  199. struct mux_control *devm_mux_control_get(struct udevice *dev, const char *id)
  200. {
  201. int rc;
  202. struct mux_control **mux;
  203. mux = devres_alloc(devm_mux_control_release,
  204. sizeof(struct mux_control *), __GFP_ZERO);
  205. if (unlikely(!mux))
  206. return ERR_PTR(-ENOMEM);
  207. rc = mux_control_get(dev, id, mux);
  208. if (rc)
  209. return ERR_PTR(rc);
  210. devres_add(dev, mux);
  211. return *mux;
  212. }
  213. int mux_alloc_controllers(struct udevice *dev, unsigned int controllers)
  214. {
  215. int i;
  216. struct mux_chip *mux_chip = dev_get_uclass_priv(dev);
  217. mux_chip->mux = devm_kmalloc(dev,
  218. sizeof(struct mux_control) * controllers,
  219. __GFP_ZERO);
  220. if (!mux_chip->mux)
  221. return -ENOMEM;
  222. mux_chip->controllers = controllers;
  223. for (i = 0; i < mux_chip->controllers; ++i) {
  224. struct mux_control *mux = &mux_chip->mux[i];
  225. mux->dev = dev;
  226. mux->cached_state = MUX_CACHE_UNKNOWN;
  227. mux->idle_state = MUX_IDLE_AS_IS;
  228. mux->in_use = false;
  229. mux->id = i;
  230. }
  231. return 0;
  232. }
  233. static int mux_uclass_post_probe(struct udevice *dev)
  234. {
  235. int i, ret;
  236. struct mux_chip *mux_chip = dev_get_uclass_priv(dev);
  237. /* Set all mux controllers to their idle state. */
  238. for (i = 0; i < mux_chip->controllers; ++i) {
  239. struct mux_control *mux = &mux_chip->mux[i];
  240. if (mux->idle_state == mux->cached_state)
  241. continue;
  242. ret = mux_control_set(mux, mux->idle_state);
  243. if (ret < 0) {
  244. dev_err(dev, "unable to set idle state\n");
  245. return ret;
  246. }
  247. }
  248. return 0;
  249. }
  250. int dm_mux_init(void)
  251. {
  252. struct uclass *uc;
  253. struct udevice *dev;
  254. int ret;
  255. ret = uclass_get(UCLASS_MUX, &uc);
  256. if (ret < 0) {
  257. log_debug("unable to get MUX uclass\n");
  258. return ret;
  259. }
  260. uclass_foreach_dev(dev, uc) {
  261. if (dev_read_bool(dev, "u-boot,mux-autoprobe")) {
  262. ret = device_probe(dev);
  263. if (ret)
  264. log_debug("unable to probe device %s\n",
  265. dev->name);
  266. }
  267. }
  268. return 0;
  269. }
  270. UCLASS_DRIVER(mux) = {
  271. .id = UCLASS_MUX,
  272. .name = "mux",
  273. .post_probe = mux_uclass_post_probe,
  274. .per_device_auto = sizeof(struct mux_chip),
  275. };