pinctrl-uclass.c 9.6 KB

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