devicetree.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. 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);
  84. }
  85. struct pinctrl_dev *of_pinctrl_get(struct device_node *np)
  86. {
  87. struct pinctrl_dev *pctldev;
  88. pctldev = get_pinctrl_dev_from_of_node(np);
  89. if (!pctldev)
  90. return NULL;
  91. return pctldev;
  92. }
  93. static int dt_to_map_one_config(struct pinctrl *p, const char *statename,
  94. struct device_node *np_config)
  95. {
  96. struct device_node *np_pctldev;
  97. struct pinctrl_dev *pctldev;
  98. const struct pinctrl_ops *ops;
  99. int ret;
  100. struct pinctrl_map *map;
  101. unsigned num_maps;
  102. /* Find the pin controller containing np_config */
  103. np_pctldev = of_node_get(np_config);
  104. for (;;) {
  105. np_pctldev = of_get_next_parent(np_pctldev);
  106. if (!np_pctldev || of_node_is_root(np_pctldev)) {
  107. dev_info(p->dev, "could not find pctldev for node %s, deferring probe\n",
  108. np_config->full_name);
  109. of_node_put(np_pctldev);
  110. /* OK let's just assume this will appear later then */
  111. return -EPROBE_DEFER;
  112. }
  113. pctldev = get_pinctrl_dev_from_of_node(np_pctldev);
  114. if (pctldev)
  115. break;
  116. /* Do not defer probing of hogs (circular loop) */
  117. if (np_pctldev == p->dev->of_node) {
  118. of_node_put(np_pctldev);
  119. return -ENODEV;
  120. }
  121. }
  122. of_node_put(np_pctldev);
  123. /*
  124. * Call pinctrl driver to parse device tree node, and
  125. * generate mapping table entries
  126. */
  127. ops = pctldev->desc->pctlops;
  128. if (!ops->dt_node_to_map) {
  129. dev_err(p->dev, "pctldev %s doesn't support DT\n",
  130. dev_name(pctldev->dev));
  131. return -ENODEV;
  132. }
  133. ret = ops->dt_node_to_map(pctldev, np_config, &map, &num_maps);
  134. if (ret < 0)
  135. return ret;
  136. /* Stash the mapping table chunk away for later use */
  137. return dt_remember_or_free_map(p, statename, pctldev, map, num_maps);
  138. }
  139. static int dt_remember_dummy_state(struct pinctrl *p, const char *statename)
  140. {
  141. struct pinctrl_map *map;
  142. map = kzalloc(sizeof(*map), GFP_KERNEL);
  143. if (!map) {
  144. dev_err(p->dev, "failed to alloc struct pinctrl_map\n");
  145. return -ENOMEM;
  146. }
  147. /* There is no pctldev for PIN_MAP_TYPE_DUMMY_STATE */
  148. map->type = PIN_MAP_TYPE_DUMMY_STATE;
  149. return dt_remember_or_free_map(p, statename, NULL, map, 1);
  150. }
  151. int pinctrl_dt_to_map(struct pinctrl *p)
  152. {
  153. struct device_node *np = p->dev->of_node;
  154. int state, ret;
  155. char *propname;
  156. struct property *prop;
  157. const char *statename;
  158. const __be32 *list;
  159. int size, config;
  160. phandle phandle;
  161. struct device_node *np_config;
  162. /* CONFIG_OF enabled, p->dev not instantiated from DT */
  163. if (!np) {
  164. if (of_have_populated_dt())
  165. dev_dbg(p->dev,
  166. "no of_node; not parsing pinctrl DT\n");
  167. return 0;
  168. }
  169. /* We may store pointers to property names within the node */
  170. of_node_get(np);
  171. /* For each defined state ID */
  172. for (state = 0; ; state++) {
  173. /* Retrieve the pinctrl-* property */
  174. propname = kasprintf(GFP_KERNEL, "pinctrl-%d", state);
  175. prop = of_find_property(np, propname, &size);
  176. kfree(propname);
  177. if (!prop)
  178. break;
  179. list = prop->value;
  180. size /= sizeof(*list);
  181. /* Determine whether pinctrl-names property names the state */
  182. ret = of_property_read_string_index(np, "pinctrl-names",
  183. state, &statename);
  184. /*
  185. * If not, statename is just the integer state ID. But rather
  186. * than dynamically allocate it and have to free it later,
  187. * just point part way into the property name for the string.
  188. */
  189. if (ret < 0) {
  190. /* strlen("pinctrl-") == 8 */
  191. statename = prop->name + 8;
  192. }
  193. /* For every referenced pin configuration node in it */
  194. for (config = 0; config < size; config++) {
  195. phandle = be32_to_cpup(list++);
  196. /* Look up the pin configuration node */
  197. np_config = of_find_node_by_phandle(phandle);
  198. if (!np_config) {
  199. dev_err(p->dev,
  200. "prop %s index %i invalid phandle\n",
  201. prop->name, config);
  202. ret = -EINVAL;
  203. goto err;
  204. }
  205. /* Parse the node */
  206. ret = dt_to_map_one_config(p, statename, np_config);
  207. of_node_put(np_config);
  208. if (ret < 0)
  209. goto err;
  210. }
  211. /* No entries in DT? Generate a dummy state table entry */
  212. if (!size) {
  213. ret = dt_remember_dummy_state(p, statename);
  214. if (ret < 0)
  215. goto err;
  216. }
  217. }
  218. return 0;
  219. err:
  220. pinctrl_dt_free_maps(p);
  221. return ret;
  222. }