devicetree.c 11 KB

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