devicetree.c 11 KB

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