devicetree.c 6.5 KB

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