devicetree.c 6.6 KB

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