pinctrl-uclass.c 9.5 KB

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