pinctrl-uclass.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. 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. const void *fdt = gd->fdt_blob;
  58. int node = dev->of_offset;
  59. char propname[32]; /* long enough */
  60. const fdt32_t *list;
  61. uint32_t phandle;
  62. int config_node;
  63. struct udevice *config;
  64. int state, size, i, ret;
  65. state = fdt_find_string(fdt, node, "pinctrl-names", statename);
  66. if (state < 0) {
  67. char *end;
  68. /*
  69. * If statename is not found in "pinctrl-names",
  70. * assume statename is just the integer state ID.
  71. */
  72. state = simple_strtoul(statename, &end, 10);
  73. if (*end)
  74. return -EINVAL;
  75. }
  76. snprintf(propname, sizeof(propname), "pinctrl-%d", state);
  77. list = fdt_getprop(fdt, node, propname, &size);
  78. if (!list)
  79. return -EINVAL;
  80. size /= sizeof(*list);
  81. for (i = 0; i < size; i++) {
  82. phandle = fdt32_to_cpu(*list++);
  83. config_node = fdt_node_offset_by_phandle(fdt, phandle);
  84. if (config_node < 0) {
  85. dev_err(dev, "prop %s index %d invalid phandle\n",
  86. propname, i);
  87. return -EINVAL;
  88. }
  89. ret = uclass_get_device_by_of_offset(UCLASS_PINCONFIG,
  90. config_node, &config);
  91. if (ret)
  92. return ret;
  93. ret = pinctrl_config_one(config);
  94. if (ret)
  95. return ret;
  96. }
  97. return 0;
  98. }
  99. /**
  100. * pinconfig_post_bind() - post binding for PINCONFIG uclass
  101. * Recursively bind its children as pinconfig devices.
  102. *
  103. * @dev: pinconfig device
  104. * @return: 0 on success, or negative error code on failure
  105. */
  106. static int pinconfig_post_bind(struct udevice *dev)
  107. {
  108. const void *fdt = gd->fdt_blob;
  109. int offset = dev->of_offset;
  110. bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
  111. const char *name;
  112. int ret;
  113. for (offset = fdt_first_subnode(fdt, offset);
  114. offset > 0;
  115. offset = fdt_next_subnode(fdt, offset)) {
  116. if (pre_reloc_only &&
  117. !fdt_getprop(fdt, offset, "u-boot,dm-pre-reloc", NULL))
  118. continue;
  119. /*
  120. * If this node has "compatible" property, this is not
  121. * a pin configuration node, but a normal device. skip.
  122. */
  123. fdt_get_property(fdt, offset, "compatible", &ret);
  124. if (ret >= 0)
  125. continue;
  126. if (ret != -FDT_ERR_NOTFOUND)
  127. return ret;
  128. name = fdt_get_name(fdt, offset, NULL);
  129. if (!name)
  130. return -EINVAL;
  131. ret = device_bind_driver_to_node(dev, "pinconfig", name,
  132. offset, NULL);
  133. if (ret)
  134. return ret;
  135. }
  136. return 0;
  137. }
  138. UCLASS_DRIVER(pinconfig) = {
  139. .id = UCLASS_PINCONFIG,
  140. .post_bind = pinconfig_post_bind,
  141. .name = "pinconfig",
  142. };
  143. U_BOOT_DRIVER(pinconfig_generic) = {
  144. .name = "pinconfig",
  145. .id = UCLASS_PINCONFIG,
  146. };
  147. #else
  148. static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
  149. {
  150. return -ENODEV;
  151. }
  152. static int pinconfig_post_bind(struct udevice *dev)
  153. {
  154. return 0;
  155. }
  156. #endif
  157. /**
  158. * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
  159. *
  160. * @dev: peripheral device
  161. * @return: 0 on success, or negative error code on failure
  162. */
  163. static int pinctrl_select_state_simple(struct udevice *dev)
  164. {
  165. struct udevice *pctldev;
  166. struct pinctrl_ops *ops;
  167. int ret;
  168. /*
  169. * For simplicity, assume the first device of PINCTRL uclass
  170. * is the correct one. This is most likely OK as there is
  171. * usually only one pinctrl device on the system.
  172. */
  173. ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
  174. if (ret)
  175. return ret;
  176. ops = pinctrl_get_ops(pctldev);
  177. if (!ops->set_state_simple) {
  178. dev_dbg(dev, "set_state_simple op missing\n");
  179. return -ENOSYS;
  180. }
  181. return ops->set_state_simple(pctldev, dev);
  182. }
  183. int pinctrl_select_state(struct udevice *dev, const char *statename)
  184. {
  185. /*
  186. * Try full-implemented pinctrl first.
  187. * If it fails or is not implemented, try simple one.
  188. */
  189. if (pinctrl_select_state_full(dev, statename))
  190. return pinctrl_select_state_simple(dev);
  191. return 0;
  192. }
  193. int pinctrl_request(struct udevice *dev, int func, int flags)
  194. {
  195. struct pinctrl_ops *ops = pinctrl_get_ops(dev);
  196. if (!ops->request)
  197. return -ENOSYS;
  198. return ops->request(dev, func, flags);
  199. }
  200. int pinctrl_request_noflags(struct udevice *dev, int func)
  201. {
  202. return pinctrl_request(dev, func, 0);
  203. }
  204. int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
  205. {
  206. struct pinctrl_ops *ops = pinctrl_get_ops(dev);
  207. if (!ops->get_periph_id)
  208. return -ENOSYS;
  209. return ops->get_periph_id(dev, periph);
  210. }
  211. int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
  212. {
  213. struct pinctrl_ops *ops = pinctrl_get_ops(dev);
  214. if (!ops->get_gpio_mux)
  215. return -ENOSYS;
  216. return ops->get_gpio_mux(dev, banknum, index);
  217. }
  218. /**
  219. * pinconfig_post_bind() - post binding for PINCTRL uclass
  220. * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
  221. *
  222. * @dev: pinctrl device
  223. * @return: 0 on success, or negative error code on failure
  224. */
  225. static int pinctrl_post_bind(struct udevice *dev)
  226. {
  227. const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
  228. if (!ops) {
  229. dev_dbg(dev, "ops is not set. Do not bind.\n");
  230. return -EINVAL;
  231. }
  232. /*
  233. * If set_state callback is set, we assume this pinctrl driver is the
  234. * full implementation. In this case, its child nodes should be bound
  235. * so that peripheral devices can easily search in parent devices
  236. * during later DT-parsing.
  237. */
  238. if (ops->set_state)
  239. return pinconfig_post_bind(dev);
  240. return 0;
  241. }
  242. UCLASS_DRIVER(pinctrl) = {
  243. .id = UCLASS_PINCTRL,
  244. .post_bind = pinctrl_post_bind,
  245. .flags = DM_UC_FLAG_SEQ_ALIAS,
  246. .name = "pinctrl",
  247. };