pinctrl-uclass.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
  4. */
  5. #define LOG_CATEGORY UCLASS_PINCTRL
  6. #include <common.h>
  7. #include <malloc.h>
  8. #include <asm/global_data.h>
  9. #include <dm/device_compat.h>
  10. #include <linux/libfdt.h>
  11. #include <linux/err.h>
  12. #include <linux/list.h>
  13. #include <dm.h>
  14. #include <dm/lists.h>
  15. #include <dm/pinctrl.h>
  16. #include <dm/util.h>
  17. #include <dm/of_access.h>
  18. DECLARE_GLOBAL_DATA_PTR;
  19. /**
  20. * pinctrl_config_one() - apply pinctrl settings for a single node
  21. *
  22. * @config: pin configuration node
  23. * @return: 0 on success, or negative error code on failure
  24. */
  25. static int pinctrl_config_one(struct udevice *config)
  26. {
  27. struct udevice *pctldev;
  28. const struct pinctrl_ops *ops;
  29. pctldev = config;
  30. for (;;) {
  31. pctldev = dev_get_parent(pctldev);
  32. if (!pctldev) {
  33. dev_err(config, "could not find pctldev\n");
  34. return -EINVAL;
  35. }
  36. if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL)
  37. break;
  38. }
  39. ops = pinctrl_get_ops(pctldev);
  40. return ops->set_state(pctldev, config);
  41. }
  42. /**
  43. * pinctrl_select_state_full() - full implementation of pinctrl_select_state
  44. *
  45. * @dev: peripheral device
  46. * @statename: state name, like "default"
  47. * @return: 0 on success, or negative error code on failure
  48. */
  49. static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
  50. {
  51. char propname[32]; /* long enough */
  52. const fdt32_t *list;
  53. uint32_t phandle;
  54. struct udevice *config;
  55. int state, size, i, ret;
  56. state = dev_read_stringlist_search(dev, "pinctrl-names", statename);
  57. if (state < 0) {
  58. char *end;
  59. /*
  60. * If statename is not found in "pinctrl-names",
  61. * assume statename is just the integer state ID.
  62. */
  63. state = dectoul(statename, &end);
  64. if (*end)
  65. return -ENOSYS;
  66. }
  67. snprintf(propname, sizeof(propname), "pinctrl-%d", state);
  68. list = dev_read_prop(dev, propname, &size);
  69. if (!list)
  70. return -ENOSYS;
  71. size /= sizeof(*list);
  72. for (i = 0; i < size; i++) {
  73. phandle = fdt32_to_cpu(*list++);
  74. ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle,
  75. &config);
  76. if (ret) {
  77. dev_warn(dev, "%s: uclass_get_device_by_phandle_id: err=%d\n",
  78. __func__, ret);
  79. continue;
  80. }
  81. ret = pinctrl_config_one(config);
  82. if (ret) {
  83. dev_warn(dev, "%s: pinctrl_config_one: err=%d\n",
  84. __func__, ret);
  85. continue;
  86. }
  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. if (!dev_has_ofnode(dev))
  104. return 0;
  105. dev_for_each_subnode(node, dev) {
  106. if (pre_reloc_only &&
  107. !ofnode_pre_reloc(node))
  108. continue;
  109. /*
  110. * If this node has "compatible" property, this is not
  111. * a pin configuration node, but a normal device. skip.
  112. */
  113. ofnode_get_property(node, "compatible", &ret);
  114. if (ret >= 0)
  115. continue;
  116. /* If this node has "gpio-controller" property, skip */
  117. if (ofnode_read_bool(node, "gpio-controller"))
  118. continue;
  119. if (ret != -FDT_ERR_NOTFOUND)
  120. return ret;
  121. name = ofnode_get_name(node);
  122. if (!name)
  123. return -EINVAL;
  124. ret = device_bind_driver_to_node(dev, "pinconfig", name,
  125. node, NULL);
  126. if (ret)
  127. return ret;
  128. }
  129. return 0;
  130. }
  131. #if CONFIG_IS_ENABLED(PINCTRL_FULL)
  132. UCLASS_DRIVER(pinconfig) = {
  133. .id = UCLASS_PINCONFIG,
  134. #if CONFIG_IS_ENABLED(PINCONF_RECURSIVE)
  135. .post_bind = pinconfig_post_bind,
  136. #endif
  137. .name = "pinconfig",
  138. };
  139. U_BOOT_DRIVER(pinconfig_generic) = {
  140. .name = "pinconfig",
  141. .id = UCLASS_PINCONFIG,
  142. };
  143. #endif
  144. static int
  145. pinctrl_gpio_get_pinctrl_and_offset(struct udevice *dev, unsigned offset,
  146. struct udevice **pctldev,
  147. unsigned int *pin_selector)
  148. {
  149. struct ofnode_phandle_args args;
  150. unsigned gpio_offset, pfc_base, pfc_pins;
  151. int ret = 0;
  152. int i = 0;
  153. while (ret == 0) {
  154. ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3,
  155. i++, &args);
  156. if (ret) {
  157. dev_dbg(dev, "%s: dev_read_phandle_with_args: err=%d\n",
  158. __func__, ret);
  159. return ret;
  160. }
  161. ret = uclass_get_device_by_ofnode(UCLASS_PINCTRL,
  162. args.node, pctldev);
  163. if (ret) {
  164. dev_dbg(dev,
  165. "%s: uclass_get_device_by_of_offset failed: err=%d\n",
  166. __func__, ret);
  167. return ret;
  168. }
  169. gpio_offset = args.args[0];
  170. pfc_base = args.args[1];
  171. pfc_pins = args.args[2];
  172. if (offset >= gpio_offset && offset <= gpio_offset + pfc_pins)
  173. break;
  174. }
  175. offset -= gpio_offset;
  176. offset += pfc_base;
  177. *pin_selector = offset;
  178. return 0;
  179. }
  180. /**
  181. * pinctrl_gpio_request() - request a single pin to be used as GPIO
  182. *
  183. * @dev: GPIO peripheral device
  184. * @offset: the GPIO pin offset from the GPIO controller
  185. * @label: the GPIO pin label
  186. * @return: 0 on success, or negative error code on failure
  187. */
  188. int pinctrl_gpio_request(struct udevice *dev, unsigned offset, const char *label)
  189. {
  190. const struct pinctrl_ops *ops;
  191. struct udevice *pctldev;
  192. unsigned int pin_selector;
  193. int ret;
  194. ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
  195. &pctldev, &pin_selector);
  196. if (ret)
  197. return ret;
  198. ops = pinctrl_get_ops(pctldev);
  199. assert(ops);
  200. if (!ops->gpio_request_enable)
  201. return -ENOSYS;
  202. return ops->gpio_request_enable(pctldev, pin_selector);
  203. }
  204. /**
  205. * pinctrl_gpio_free() - free a single pin used as GPIO
  206. *
  207. * @dev: GPIO peripheral device
  208. * @offset: the GPIO pin offset from the GPIO controller
  209. * @return: 0 on success, or negative error code on failure
  210. */
  211. int pinctrl_gpio_free(struct udevice *dev, unsigned offset)
  212. {
  213. const struct pinctrl_ops *ops;
  214. struct udevice *pctldev;
  215. unsigned int pin_selector;
  216. int ret;
  217. ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
  218. &pctldev, &pin_selector);
  219. if (ret)
  220. return ret;
  221. ops = pinctrl_get_ops(pctldev);
  222. assert(ops);
  223. if (!ops->gpio_disable_free)
  224. return -ENOSYS;
  225. return ops->gpio_disable_free(pctldev, pin_selector);
  226. }
  227. /**
  228. * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
  229. *
  230. * @dev: peripheral device
  231. * @return: 0 on success, or negative error code on failure
  232. */
  233. static int pinctrl_select_state_simple(struct udevice *dev)
  234. {
  235. struct udevice *pctldev;
  236. struct pinctrl_ops *ops;
  237. int ret;
  238. /*
  239. * For most system, there is only one pincontroller device. But in
  240. * case of multiple pincontroller devices, probe the one with sequence
  241. * number 0 (defined by alias) to avoid race condition.
  242. */
  243. ret = uclass_get_device_by_seq(UCLASS_PINCTRL, 0, &pctldev);
  244. if (ret)
  245. /* if not found, get the first one */
  246. ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
  247. if (ret)
  248. return ret;
  249. ops = pinctrl_get_ops(pctldev);
  250. if (!ops->set_state_simple) {
  251. dev_dbg(dev, "set_state_simple op missing\n");
  252. return -ENOSYS;
  253. }
  254. return ops->set_state_simple(pctldev, dev);
  255. }
  256. int pinctrl_select_state(struct udevice *dev, const char *statename)
  257. {
  258. /*
  259. * Some device which is logical like mmc.blk, do not have
  260. * a valid ofnode.
  261. */
  262. if (!dev_has_ofnode(dev))
  263. return 0;
  264. /*
  265. * Try full-implemented pinctrl first.
  266. * If it fails or is not implemented, try simple one.
  267. */
  268. if (CONFIG_IS_ENABLED(PINCTRL_FULL))
  269. return pinctrl_select_state_full(dev, statename);
  270. return pinctrl_select_state_simple(dev);
  271. }
  272. int pinctrl_request(struct udevice *dev, int func, int flags)
  273. {
  274. struct pinctrl_ops *ops = pinctrl_get_ops(dev);
  275. if (!ops->request)
  276. return -ENOSYS;
  277. return ops->request(dev, func, flags);
  278. }
  279. int pinctrl_request_noflags(struct udevice *dev, int func)
  280. {
  281. return pinctrl_request(dev, func, 0);
  282. }
  283. int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
  284. {
  285. struct pinctrl_ops *ops = pinctrl_get_ops(dev);
  286. if (!ops->get_periph_id)
  287. return -ENOSYS;
  288. return ops->get_periph_id(dev, periph);
  289. }
  290. int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
  291. {
  292. struct pinctrl_ops *ops = pinctrl_get_ops(dev);
  293. if (!ops->get_gpio_mux)
  294. return -ENOSYS;
  295. return ops->get_gpio_mux(dev, banknum, index);
  296. }
  297. int pinctrl_get_pins_count(struct udevice *dev)
  298. {
  299. struct pinctrl_ops *ops = pinctrl_get_ops(dev);
  300. if (!ops->get_pins_count)
  301. return -ENOSYS;
  302. return ops->get_pins_count(dev);
  303. }
  304. int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf,
  305. int size)
  306. {
  307. struct pinctrl_ops *ops = pinctrl_get_ops(dev);
  308. if (!ops->get_pin_name)
  309. return -ENOSYS;
  310. snprintf(buf, size, ops->get_pin_name(dev, selector));
  311. return 0;
  312. }
  313. int pinctrl_get_pin_muxing(struct udevice *dev, int selector, char *buf,
  314. int size)
  315. {
  316. struct pinctrl_ops *ops = pinctrl_get_ops(dev);
  317. if (!ops->get_pin_muxing)
  318. return -ENOSYS;
  319. return ops->get_pin_muxing(dev, selector, buf, size);
  320. }
  321. /**
  322. * pinctrl_post_bind() - post binding for PINCTRL uclass
  323. * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
  324. *
  325. * @dev: pinctrl device
  326. * @return: 0 on success, or negative error code on failure
  327. */
  328. static int __maybe_unused pinctrl_post_bind(struct udevice *dev)
  329. {
  330. const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
  331. if (!ops) {
  332. dev_dbg(dev, "ops is not set. Do not bind.\n");
  333. return -EINVAL;
  334. }
  335. /*
  336. * If the pinctrl driver has the full implementation, its child nodes
  337. * should be bound so that peripheral devices can easily search in
  338. * parent devices during later DT-parsing.
  339. */
  340. if (CONFIG_IS_ENABLED(PINCTRL_FULL))
  341. return pinconfig_post_bind(dev);
  342. return 0;
  343. }
  344. UCLASS_DRIVER(pinctrl) = {
  345. .id = UCLASS_PINCTRL,
  346. #if CONFIG_IS_ENABLED(OF_REAL)
  347. .post_bind = pinctrl_post_bind,
  348. #endif
  349. .flags = DM_UC_FLAG_SEQ_ALIAS,
  350. .name = "pinctrl",
  351. };