pinctrl-uclass.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
  4. */
  5. #include <common.h>
  6. #include <linux/libfdt.h>
  7. #include <linux/err.h>
  8. #include <linux/list.h>
  9. #include <dm.h>
  10. #include <dm/lists.h>
  11. #include <dm/pinctrl.h>
  12. #include <dm/util.h>
  13. #include <dm/of_access.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. int pinctrl_decode_pin_config(const void *blob, int node)
  16. {
  17. int flags = 0;
  18. if (fdtdec_get_bool(blob, node, "bias-pull-up"))
  19. flags |= 1 << PIN_CONFIG_BIAS_PULL_UP;
  20. else if (fdtdec_get_bool(blob, node, "bias-pull-down"))
  21. flags |= 1 << PIN_CONFIG_BIAS_PULL_DOWN;
  22. return flags;
  23. }
  24. #if CONFIG_IS_ENABLED(PINCTRL_FULL)
  25. /**
  26. * pinctrl_config_one() - apply pinctrl settings for a single node
  27. *
  28. * @config: pin configuration node
  29. * @return: 0 on success, or negative error code on failure
  30. */
  31. static int pinctrl_config_one(struct udevice *config)
  32. {
  33. struct udevice *pctldev;
  34. const struct pinctrl_ops *ops;
  35. pctldev = config;
  36. for (;;) {
  37. pctldev = dev_get_parent(pctldev);
  38. if (!pctldev) {
  39. dev_err(config, "could not find pctldev\n");
  40. return -EINVAL;
  41. }
  42. if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL)
  43. break;
  44. }
  45. ops = pinctrl_get_ops(pctldev);
  46. return ops->set_state(pctldev, config);
  47. }
  48. /**
  49. * pinctrl_select_state_full() - full implementation of pinctrl_select_state
  50. *
  51. * @dev: peripheral device
  52. * @statename: state name, like "default"
  53. * @return: 0 on success, or negative error code on failure
  54. */
  55. static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
  56. {
  57. char propname[32]; /* long enough */
  58. const fdt32_t *list;
  59. uint32_t phandle;
  60. struct udevice *config;
  61. int state, size, i, ret;
  62. state = dev_read_stringlist_search(dev, "pinctrl-names", statename);
  63. if (state < 0) {
  64. char *end;
  65. /*
  66. * If statename is not found in "pinctrl-names",
  67. * assume statename is just the integer state ID.
  68. */
  69. state = simple_strtoul(statename, &end, 10);
  70. if (*end)
  71. return -EINVAL;
  72. }
  73. snprintf(propname, sizeof(propname), "pinctrl-%d", state);
  74. list = dev_read_prop(dev, propname, &size);
  75. if (!list)
  76. return -EINVAL;
  77. size /= sizeof(*list);
  78. for (i = 0; i < size; i++) {
  79. phandle = fdt32_to_cpu(*list++);
  80. ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle,
  81. &config);
  82. if (ret)
  83. return ret;
  84. ret = pinctrl_config_one(config);
  85. if (ret)
  86. return ret;
  87. }
  88. return 0;
  89. }
  90. /**
  91. * pinconfig_post_bind() - post binding for PINCONFIG uclass
  92. * Recursively bind its children as pinconfig devices.
  93. *
  94. * @dev: pinconfig device
  95. * @return: 0 on success, or negative error code on failure
  96. */
  97. static int pinconfig_post_bind(struct udevice *dev)
  98. {
  99. bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
  100. const char *name;
  101. ofnode node;
  102. int ret;
  103. dev_for_each_subnode(node, dev) {
  104. if (pre_reloc_only &&
  105. !ofnode_pre_reloc(node))
  106. continue;
  107. /*
  108. * If this node has "compatible" property, this is not
  109. * a pin configuration node, but a normal device. skip.
  110. */
  111. ofnode_get_property(node, "compatible", &ret);
  112. if (ret >= 0)
  113. continue;
  114. if (ret != -FDT_ERR_NOTFOUND)
  115. return ret;
  116. name = ofnode_get_name(node);
  117. if (!name)
  118. return -EINVAL;
  119. ret = device_bind_driver_to_node(dev, "pinconfig", name,
  120. node, NULL);
  121. if (ret)
  122. return ret;
  123. }
  124. return 0;
  125. }
  126. UCLASS_DRIVER(pinconfig) = {
  127. .id = UCLASS_PINCONFIG,
  128. .post_bind = pinconfig_post_bind,
  129. .name = "pinconfig",
  130. };
  131. U_BOOT_DRIVER(pinconfig_generic) = {
  132. .name = "pinconfig",
  133. .id = UCLASS_PINCONFIG,
  134. };
  135. #else
  136. static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
  137. {
  138. return -ENODEV;
  139. }
  140. static int pinconfig_post_bind(struct udevice *dev)
  141. {
  142. return 0;
  143. }
  144. #endif
  145. /**
  146. * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
  147. *
  148. * @dev: peripheral device
  149. * @return: 0 on success, or negative error code on failure
  150. */
  151. static int pinctrl_select_state_simple(struct udevice *dev)
  152. {
  153. struct udevice *pctldev;
  154. struct pinctrl_ops *ops;
  155. int ret;
  156. /*
  157. * For simplicity, assume the first device of PINCTRL uclass
  158. * is the correct one. This is most likely OK as there is
  159. * usually only one pinctrl device on the system.
  160. */
  161. ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
  162. if (ret)
  163. return ret;
  164. ops = pinctrl_get_ops(pctldev);
  165. if (!ops->set_state_simple) {
  166. dev_dbg(dev, "set_state_simple op missing\n");
  167. return -ENOSYS;
  168. }
  169. return ops->set_state_simple(pctldev, dev);
  170. }
  171. int pinctrl_select_state(struct udevice *dev, const char *statename)
  172. {
  173. /*
  174. * Some device which is logical like mmc.blk, do not have
  175. * a valid ofnode.
  176. */
  177. if (!ofnode_valid(dev->node))
  178. return 0;
  179. /*
  180. * Try full-implemented pinctrl first.
  181. * If it fails or is not implemented, try simple one.
  182. */
  183. if (pinctrl_select_state_full(dev, statename))
  184. return pinctrl_select_state_simple(dev);
  185. return 0;
  186. }
  187. int pinctrl_request(struct udevice *dev, int func, int flags)
  188. {
  189. struct pinctrl_ops *ops = pinctrl_get_ops(dev);
  190. if (!ops->request)
  191. return -ENOSYS;
  192. return ops->request(dev, func, flags);
  193. }
  194. int pinctrl_request_noflags(struct udevice *dev, int func)
  195. {
  196. return pinctrl_request(dev, func, 0);
  197. }
  198. int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
  199. {
  200. struct pinctrl_ops *ops = pinctrl_get_ops(dev);
  201. if (!ops->get_periph_id)
  202. return -ENOSYS;
  203. return ops->get_periph_id(dev, periph);
  204. }
  205. int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
  206. {
  207. struct pinctrl_ops *ops = pinctrl_get_ops(dev);
  208. if (!ops->get_gpio_mux)
  209. return -ENOSYS;
  210. return ops->get_gpio_mux(dev, banknum, index);
  211. }
  212. /**
  213. * pinconfig_post_bind() - post binding for PINCTRL uclass
  214. * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
  215. *
  216. * @dev: pinctrl device
  217. * @return: 0 on success, or negative error code on failure
  218. */
  219. static int pinctrl_post_bind(struct udevice *dev)
  220. {
  221. const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
  222. if (!ops) {
  223. dev_dbg(dev, "ops is not set. Do not bind.\n");
  224. return -EINVAL;
  225. }
  226. /*
  227. * If set_state callback is set, we assume this pinctrl driver is the
  228. * full implementation. In this case, its child nodes should be bound
  229. * so that peripheral devices can easily search in parent devices
  230. * during later DT-parsing.
  231. */
  232. if (ops->set_state)
  233. return pinconfig_post_bind(dev);
  234. return 0;
  235. }
  236. UCLASS_DRIVER(pinctrl) = {
  237. .id = UCLASS_PINCTRL,
  238. .post_bind = pinctrl_post_bind,
  239. .flags = DM_UC_FLAG_SEQ_ALIAS,
  240. .name = "pinctrl",
  241. };