pinctrl-uclass.c 6.6 KB

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