devicetree.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Device tree integration for the pin control subsystem
  4. *
  5. * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
  6. */
  7. #include <linux/device.h>
  8. #include <linux/of.h>
  9. #include <linux/pinctrl/pinctrl.h>
  10. #include <linux/slab.h>
  11. #include "core.h"
  12. #include "devicetree.h"
  13. /**
  14. * struct pinctrl_dt_map - mapping table chunk parsed from device tree
  15. * @node: list node for struct pinctrl's @dt_maps field
  16. * @pctldev: the pin controller that allocated this struct, and will free it
  17. * @map: the mapping table entries
  18. * @num_maps: number of mapping table entries
  19. */
  20. struct pinctrl_dt_map {
  21. struct list_head node;
  22. struct pinctrl_dev *pctldev;
  23. struct pinctrl_map *map;
  24. unsigned int num_maps;
  25. };
  26. static void dt_free_map(struct pinctrl_dev *pctldev,
  27. struct pinctrl_map *map, unsigned int num_maps)
  28. {
  29. int i;
  30. for (i = 0; i < num_maps; ++i) {
  31. kfree_const(map[i].dev_name);
  32. map[i].dev_name = NULL;
  33. }
  34. if (pctldev) {
  35. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  36. if (ops->dt_free_map)
  37. ops->dt_free_map(pctldev, map, num_maps);
  38. } else {
  39. /* There is no pctldev for PIN_MAP_TYPE_DUMMY_STATE */
  40. kfree(map);
  41. }
  42. }
  43. void pinctrl_dt_free_maps(struct pinctrl *p)
  44. {
  45. struct pinctrl_dt_map *dt_map, *n1;
  46. list_for_each_entry_safe(dt_map, n1, &p->dt_maps, node) {
  47. pinctrl_unregister_mappings(dt_map->map);
  48. list_del(&dt_map->node);
  49. dt_free_map(dt_map->pctldev, dt_map->map,
  50. dt_map->num_maps);
  51. kfree(dt_map);
  52. }
  53. of_node_put(p->dev->of_node);
  54. }
  55. static int dt_remember_or_free_map(struct pinctrl *p, const char *statename,
  56. struct pinctrl_dev *pctldev,
  57. struct pinctrl_map *map, unsigned int num_maps)
  58. {
  59. int i;
  60. struct pinctrl_dt_map *dt_map;
  61. /* Initialize common mapping table entry fields */
  62. for (i = 0; i < num_maps; i++) {
  63. const char *devname;
  64. devname = kstrdup_const(dev_name(p->dev), GFP_KERNEL);
  65. if (!devname)
  66. goto err_free_map;
  67. map[i].dev_name = devname;
  68. map[i].name = statename;
  69. if (pctldev)
  70. map[i].ctrl_dev_name = dev_name(pctldev->dev);
  71. }
  72. /* Remember the converted mapping table entries */
  73. dt_map = kzalloc(sizeof(*dt_map), GFP_KERNEL);
  74. if (!dt_map)
  75. goto err_free_map;
  76. dt_map->pctldev = pctldev;
  77. dt_map->map = map;
  78. dt_map->num_maps = num_maps;
  79. list_add_tail(&dt_map->node, &p->dt_maps);
  80. return pinctrl_register_mappings(map, num_maps);
  81. err_free_map:
  82. dt_free_map(pctldev, map, num_maps);
  83. return -ENOMEM;
  84. }
  85. struct pinctrl_dev *of_pinctrl_get(struct device_node *np)
  86. {
  87. return get_pinctrl_dev_from_of_node(np);
  88. }
  89. EXPORT_SYMBOL_GPL(of_pinctrl_get);
  90. static int dt_to_map_one_config(struct pinctrl *p,
  91. struct pinctrl_dev *hog_pctldev,
  92. const char *statename,
  93. struct device_node *np_config)
  94. {
  95. struct pinctrl_dev *pctldev = NULL;
  96. struct device_node *np_pctldev;
  97. const struct pinctrl_ops *ops;
  98. int ret;
  99. struct pinctrl_map *map;
  100. unsigned int num_maps;
  101. bool allow_default = false;
  102. /* Find the pin controller containing np_config */
  103. np_pctldev = of_node_get(np_config);
  104. for (;;) {
  105. if (!allow_default)
  106. allow_default = of_property_read_bool(np_pctldev,
  107. "pinctrl-use-default");
  108. np_pctldev = of_get_next_parent(np_pctldev);
  109. if (!np_pctldev || of_node_is_root(np_pctldev)) {
  110. of_node_put(np_pctldev);
  111. ret = -ENODEV;
  112. /* keep deferring if modules are enabled */
  113. if (IS_ENABLED(CONFIG_MODULES) && !allow_default && ret < 0)
  114. ret = -EPROBE_DEFER;
  115. return ret;
  116. }
  117. /* If we're creating a hog we can use the passed pctldev */
  118. if (hog_pctldev && (np_pctldev == p->dev->of_node)) {
  119. pctldev = hog_pctldev;
  120. break;
  121. }
  122. pctldev = get_pinctrl_dev_from_of_node(np_pctldev);
  123. if (pctldev)
  124. break;
  125. /*
  126. * Do not defer probing of hogs (circular loop)
  127. *
  128. * Return 1 to let the caller catch the case.
  129. */
  130. if (np_pctldev == p->dev->of_node) {
  131. of_node_put(np_pctldev);
  132. return 1;
  133. }
  134. }
  135. of_node_put(np_pctldev);
  136. /*
  137. * Call pinctrl driver to parse device tree node, and
  138. * generate mapping table entries
  139. */
  140. ops = pctldev->desc->pctlops;
  141. if (!ops->dt_node_to_map) {
  142. dev_err(p->dev, "pctldev %s doesn't support DT\n",
  143. dev_name(pctldev->dev));
  144. return -ENODEV;
  145. }
  146. ret = ops->dt_node_to_map(pctldev, np_config, &map, &num_maps);
  147. if (ret < 0)
  148. return ret;
  149. else if (num_maps == 0) {
  150. /*
  151. * If we have no valid maps (maybe caused by empty pinctrl node
  152. * or typing error) ther is no need remember this, so just
  153. * return.
  154. */
  155. dev_info(p->dev,
  156. "there is not valid maps for state %s\n", statename);
  157. return 0;
  158. }
  159. /* Stash the mapping table chunk away for later use */
  160. return dt_remember_or_free_map(p, statename, pctldev, map, num_maps);
  161. }
  162. static int dt_remember_dummy_state(struct pinctrl *p, const char *statename)
  163. {
  164. struct pinctrl_map *map;
  165. map = kzalloc(sizeof(*map), GFP_KERNEL);
  166. if (!map)
  167. return -ENOMEM;
  168. /* There is no pctldev for PIN_MAP_TYPE_DUMMY_STATE */
  169. map->type = PIN_MAP_TYPE_DUMMY_STATE;
  170. return dt_remember_or_free_map(p, statename, NULL, map, 1);
  171. }
  172. int pinctrl_dt_to_map(struct pinctrl *p, struct pinctrl_dev *pctldev)
  173. {
  174. struct device_node *np = p->dev->of_node;
  175. int state, ret;
  176. char *propname;
  177. struct property *prop;
  178. const char *statename;
  179. const __be32 *list;
  180. int size, config;
  181. phandle phandle;
  182. struct device_node *np_config;
  183. /* CONFIG_OF enabled, p->dev not instantiated from DT */
  184. if (!np) {
  185. if (of_have_populated_dt())
  186. dev_dbg(p->dev,
  187. "no of_node; not parsing pinctrl DT\n");
  188. return 0;
  189. }
  190. /* We may store pointers to property names within the node */
  191. of_node_get(np);
  192. /* For each defined state ID */
  193. for (state = 0; ; state++) {
  194. /* Retrieve the pinctrl-* property */
  195. propname = kasprintf(GFP_KERNEL, "pinctrl-%d", state);
  196. if (!propname) {
  197. ret = -ENOMEM;
  198. goto err;
  199. }
  200. prop = of_find_property(np, propname, &size);
  201. kfree(propname);
  202. if (!prop) {
  203. if (state == 0) {
  204. ret = -ENODEV;
  205. goto err;
  206. }
  207. break;
  208. }
  209. list = prop->value;
  210. size /= sizeof(*list);
  211. /* Determine whether pinctrl-names property names the state */
  212. ret = of_property_read_string_index(np, "pinctrl-names",
  213. state, &statename);
  214. /*
  215. * If not, statename is just the integer state ID. But rather
  216. * than dynamically allocate it and have to free it later,
  217. * just point part way into the property name for the string.
  218. */
  219. if (ret < 0)
  220. statename = prop->name + strlen("pinctrl-");
  221. /* For every referenced pin configuration node in it */
  222. for (config = 0; config < size; config++) {
  223. phandle = be32_to_cpup(list++);
  224. /* Look up the pin configuration node */
  225. np_config = of_find_node_by_phandle(phandle);
  226. if (!np_config) {
  227. dev_err(p->dev,
  228. "prop %s index %i invalid phandle\n",
  229. prop->name, config);
  230. ret = -EINVAL;
  231. goto err;
  232. }
  233. /* Parse the node */
  234. ret = dt_to_map_one_config(p, pctldev, statename,
  235. np_config);
  236. of_node_put(np_config);
  237. if (ret == 1)
  238. continue;
  239. if (ret < 0)
  240. goto err;
  241. }
  242. /* No entries in DT? Generate a dummy state table entry */
  243. if (!size) {
  244. ret = dt_remember_dummy_state(p, statename);
  245. if (ret < 0)
  246. goto err;
  247. }
  248. }
  249. return 0;
  250. err:
  251. pinctrl_dt_free_maps(p);
  252. return ret;
  253. }
  254. /*
  255. * For pinctrl binding, typically #pinctrl-cells is for the pin controller
  256. * device, so either parent or grandparent. See pinctrl-bindings.txt.
  257. */
  258. static int pinctrl_find_cells_size(const struct device_node *np)
  259. {
  260. const char *cells_name = "#pinctrl-cells";
  261. int cells_size, error;
  262. error = of_property_read_u32(np->parent, cells_name, &cells_size);
  263. if (error) {
  264. error = of_property_read_u32(np->parent->parent,
  265. cells_name, &cells_size);
  266. if (error)
  267. return -ENOENT;
  268. }
  269. return cells_size;
  270. }
  271. /**
  272. * pinctrl_get_list_and_count - Gets the list and it's cell size and number
  273. * @np: pointer to device node with the property
  274. * @list_name: property that contains the list
  275. * @list: pointer for the list found
  276. * @cells_size: pointer for the cell size found
  277. * @nr_elements: pointer for the number of elements found
  278. *
  279. * Typically np is a single pinctrl entry containing the list.
  280. */
  281. static int pinctrl_get_list_and_count(const struct device_node *np,
  282. const char *list_name,
  283. const __be32 **list,
  284. int *cells_size,
  285. int *nr_elements)
  286. {
  287. int size;
  288. *cells_size = 0;
  289. *nr_elements = 0;
  290. *list = of_get_property(np, list_name, &size);
  291. if (!*list)
  292. return -ENOENT;
  293. *cells_size = pinctrl_find_cells_size(np);
  294. if (*cells_size < 0)
  295. return -ENOENT;
  296. /* First element is always the index within the pinctrl device */
  297. *nr_elements = (size / sizeof(**list)) / (*cells_size + 1);
  298. return 0;
  299. }
  300. /**
  301. * pinctrl_count_index_with_args - Count number of elements in a pinctrl entry
  302. * @np: pointer to device node with the property
  303. * @list_name: property that contains the list
  304. *
  305. * Counts the number of elements in a pinctrl array consisting of an index
  306. * within the controller and a number of u32 entries specified for each
  307. * entry. Note that device_node is always for the parent pin controller device.
  308. */
  309. int pinctrl_count_index_with_args(const struct device_node *np,
  310. const char *list_name)
  311. {
  312. const __be32 *list;
  313. int size, nr_cells, error;
  314. error = pinctrl_get_list_and_count(np, list_name, &list,
  315. &nr_cells, &size);
  316. if (error)
  317. return error;
  318. return size;
  319. }
  320. EXPORT_SYMBOL_GPL(pinctrl_count_index_with_args);
  321. /**
  322. * pinctrl_copy_args - Populates of_phandle_args based on index
  323. * @np: pointer to device node with the property
  324. * @list: pointer to a list with the elements
  325. * @index: entry within the list of elements
  326. * @nr_cells: number of cells in the list
  327. * @nr_elem: number of elements for each entry in the list
  328. * @out_args: returned values
  329. *
  330. * Populates the of_phandle_args based on the index in the list.
  331. */
  332. static int pinctrl_copy_args(const struct device_node *np,
  333. const __be32 *list,
  334. int index, int nr_cells, int nr_elem,
  335. struct of_phandle_args *out_args)
  336. {
  337. int i;
  338. memset(out_args, 0, sizeof(*out_args));
  339. out_args->np = (struct device_node *)np;
  340. out_args->args_count = nr_cells + 1;
  341. if (index >= nr_elem)
  342. return -EINVAL;
  343. list += index * (nr_cells + 1);
  344. for (i = 0; i < nr_cells + 1; i++)
  345. out_args->args[i] = be32_to_cpup(list++);
  346. return 0;
  347. }
  348. /**
  349. * pinctrl_parse_index_with_args - Find a node pointed by index in a list
  350. * @np: pointer to device node with the property
  351. * @list_name: property that contains the list
  352. * @index: index within the list
  353. * @out_args: entries in the list pointed by index
  354. *
  355. * Finds the selected element in a pinctrl array consisting of an index
  356. * within the controller and a number of u32 entries specified for each
  357. * entry. Note that device_node is always for the parent pin controller device.
  358. */
  359. int pinctrl_parse_index_with_args(const struct device_node *np,
  360. const char *list_name, int index,
  361. struct of_phandle_args *out_args)
  362. {
  363. const __be32 *list;
  364. int nr_elem, nr_cells, error;
  365. error = pinctrl_get_list_and_count(np, list_name, &list,
  366. &nr_cells, &nr_elem);
  367. if (error || !nr_cells)
  368. return error;
  369. error = pinctrl_copy_args(np, list, index, nr_cells, nr_elem,
  370. out_args);
  371. if (error)
  372. return error;
  373. return 0;
  374. }
  375. EXPORT_SYMBOL_GPL(pinctrl_parse_index_with_args);