devicetree.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. struct pinctrl_ops *ops = pctldev->desc->pctlops;
  41. ops->dt_free_map(pctldev, map, num_maps);
  42. } else {
  43. /* There is no pctldev for PIN_MAP_TYPE_DUMMY_STATE */
  44. kfree(map);
  45. }
  46. }
  47. void pinctrl_dt_free_maps(struct pinctrl *p)
  48. {
  49. struct pinctrl_dt_map *dt_map, *n1;
  50. list_for_each_entry_safe(dt_map, n1, &p->dt_maps, node) {
  51. pinctrl_unregister_map(dt_map->map);
  52. list_del(&dt_map->node);
  53. dt_free_map(dt_map->pctldev, dt_map->map,
  54. dt_map->num_maps);
  55. kfree(dt_map);
  56. }
  57. of_node_put(p->dev->of_node);
  58. }
  59. static int dt_remember_or_free_map(struct pinctrl *p, const char *statename,
  60. struct pinctrl_dev *pctldev,
  61. struct pinctrl_map *map, unsigned num_maps)
  62. {
  63. int i;
  64. struct pinctrl_dt_map *dt_map;
  65. /* Initialize common mapping table entry fields */
  66. for (i = 0; i < num_maps; i++) {
  67. map[i].dev_name = dev_name(p->dev);
  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. dev_err(p->dev, "failed to alloc struct pinctrl_dt_map\n");
  76. dt_free_map(pctldev, map, num_maps);
  77. return -ENOMEM;
  78. }
  79. dt_map->pctldev = pctldev;
  80. dt_map->map = map;
  81. dt_map->num_maps = num_maps;
  82. list_add_tail(&dt_map->node, &p->dt_maps);
  83. return pinctrl_register_map(map, num_maps, false, true);
  84. }
  85. static struct pinctrl_dev *find_pinctrl_by_of_node(struct device_node *np)
  86. {
  87. struct pinctrl_dev *pctldev;
  88. list_for_each_entry(pctldev, &pinctrldev_list, node)
  89. if (pctldev->dev->of_node == np)
  90. return pctldev;
  91. return NULL;
  92. }
  93. struct pinctrl_dev *of_pinctrl_get(struct device_node *np)
  94. {
  95. struct pinctrl_dev *pctldev;
  96. pctldev = find_pinctrl_by_of_node(np);
  97. if (!pctldev)
  98. return NULL;
  99. return pctldev;
  100. }
  101. static int dt_to_map_one_config(struct pinctrl *p, const char *statename,
  102. struct device_node *np_config)
  103. {
  104. struct device_node *np_pctldev;
  105. struct pinctrl_dev *pctldev;
  106. 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 %s, deferring probe\n",
  116. np_config->full_name);
  117. of_node_put(np_pctldev);
  118. /* OK let's just assume this will appear later then */
  119. return -EPROBE_DEFER;
  120. }
  121. pctldev = find_pinctrl_by_of_node(np_pctldev);
  122. if (pctldev)
  123. break;
  124. /* Do not defer probing of hogs (circular loop) */
  125. if (np_pctldev == p->dev->of_node) {
  126. of_node_put(np_pctldev);
  127. return -ENODEV;
  128. }
  129. }
  130. of_node_put(np_pctldev);
  131. /*
  132. * Call pinctrl driver to parse device tree node, and
  133. * generate mapping table entries
  134. */
  135. ops = pctldev->desc->pctlops;
  136. if (!ops->dt_node_to_map) {
  137. dev_err(p->dev, "pctldev %s doesn't support DT\n",
  138. dev_name(pctldev->dev));
  139. return -ENODEV;
  140. }
  141. ret = ops->dt_node_to_map(pctldev, np_config, &map, &num_maps);
  142. if (ret < 0)
  143. return ret;
  144. /* Stash the mapping table chunk away for later use */
  145. return dt_remember_or_free_map(p, statename, pctldev, map, num_maps);
  146. }
  147. static int dt_remember_dummy_state(struct pinctrl *p, const char *statename)
  148. {
  149. struct pinctrl_map *map;
  150. map = kzalloc(sizeof(*map), GFP_KERNEL);
  151. if (!map) {
  152. dev_err(p->dev, "failed to alloc struct pinctrl_map\n");
  153. return -ENOMEM;
  154. }
  155. /* There is no pctldev for PIN_MAP_TYPE_DUMMY_STATE */
  156. map->type = PIN_MAP_TYPE_DUMMY_STATE;
  157. return dt_remember_or_free_map(p, statename, NULL, map, 1);
  158. }
  159. int pinctrl_dt_to_map(struct pinctrl *p)
  160. {
  161. struct device_node *np = p->dev->of_node;
  162. int state, ret;
  163. char *propname;
  164. struct property *prop;
  165. const char *statename;
  166. const __be32 *list;
  167. int size, config;
  168. phandle phandle;
  169. struct device_node *np_config;
  170. /* CONFIG_OF enabled, p->dev not instantiated from DT */
  171. if (!np) {
  172. dev_dbg(p->dev, "no of_node; not parsing pinctrl DT\n");
  173. return 0;
  174. }
  175. /* We may store pointers to property names within the node */
  176. of_node_get(np);
  177. /* For each defined state ID */
  178. for (state = 0; ; state++) {
  179. /* Retrieve the pinctrl-* property */
  180. propname = kasprintf(GFP_KERNEL, "pinctrl-%d", state);
  181. prop = of_find_property(np, propname, &size);
  182. kfree(propname);
  183. if (!prop)
  184. break;
  185. list = prop->value;
  186. size /= sizeof(*list);
  187. /* Determine whether pinctrl-names property names the state */
  188. ret = of_property_read_string_index(np, "pinctrl-names",
  189. state, &statename);
  190. /*
  191. * If not, statename is just the integer state ID. But rather
  192. * than dynamically allocate it and have to free it later,
  193. * just point part way into the property name for the string.
  194. */
  195. if (ret < 0) {
  196. /* strlen("pinctrl-") == 8 */
  197. statename = prop->name + 8;
  198. }
  199. /* For every referenced pin configuration node in it */
  200. for (config = 0; config < size; config++) {
  201. phandle = be32_to_cpup(list++);
  202. /* Look up the pin configuration node */
  203. np_config = of_find_node_by_phandle(phandle);
  204. if (!np_config) {
  205. dev_err(p->dev,
  206. "prop %s index %i invalid phandle\n",
  207. prop->name, config);
  208. ret = -EINVAL;
  209. goto err;
  210. }
  211. /* Parse the node */
  212. ret = dt_to_map_one_config(p, statename, np_config);
  213. of_node_put(np_config);
  214. if (ret < 0)
  215. goto err;
  216. }
  217. /* No entries in DT? Generate a dummy state table entry */
  218. if (!size) {
  219. ret = dt_remember_dummy_state(p, statename);
  220. if (ret < 0)
  221. goto err;
  222. }
  223. }
  224. return 0;
  225. err:
  226. pinctrl_dt_free_maps(p);
  227. return ret;
  228. }