2 * Device tree integration for the pin control subsystem
4 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
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.
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
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/>.
19 #include <linux/device.h>
21 #include <linux/pinctrl/pinctrl.h>
22 #include <linux/slab.h>
25 #include "devicetree.h"
28 * struct pinctrl_dt_map - mapping table chunk parsed from device tree
29 * @node: list node for struct pinctrl's @dt_maps field
30 * @pctldev: the pin controller that allocated this struct, and will free it
31 * @maps: the mapping table entries
33 struct pinctrl_dt_map {
34 struct list_head node;
35 struct pinctrl_dev *pctldev;
36 struct pinctrl_map *map;
40 static void dt_free_map(struct pinctrl_dev *pctldev,
41 struct pinctrl_map *map, unsigned num_maps)
45 for (i = 0; i < num_maps; ++i) {
46 kfree_const(map[i].dev_name);
47 map[i].dev_name = NULL;
51 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
52 ops->dt_free_map(pctldev, map, num_maps);
54 /* There is no pctldev for PIN_MAP_TYPE_DUMMY_STATE */
59 void pinctrl_dt_free_maps(struct pinctrl *p)
61 struct pinctrl_dt_map *dt_map, *n1;
63 list_for_each_entry_safe(dt_map, n1, &p->dt_maps, node) {
64 pinctrl_unregister_map(dt_map->map);
65 list_del(&dt_map->node);
66 dt_free_map(dt_map->pctldev, dt_map->map,
71 of_node_put(p->dev->of_node);
74 static int dt_remember_or_free_map(struct pinctrl *p, const char *statename,
75 struct pinctrl_dev *pctldev,
76 struct pinctrl_map *map, unsigned num_maps)
79 struct pinctrl_dt_map *dt_map;
81 /* Initialize common mapping table entry fields */
82 for (i = 0; i < num_maps; i++) {
85 devname = kstrdup_const(dev_name(p->dev), GFP_KERNEL);
89 map[i].dev_name = devname;
90 map[i].name = statename;
92 map[i].ctrl_dev_name = dev_name(pctldev->dev);
95 /* Remember the converted mapping table entries */
96 dt_map = kzalloc(sizeof(*dt_map), GFP_KERNEL);
100 dt_map->pctldev = pctldev;
102 dt_map->num_maps = num_maps;
103 list_add_tail(&dt_map->node, &p->dt_maps);
105 return pinctrl_register_map(map, num_maps, false);
108 dt_free_map(pctldev, map, num_maps);
112 struct pinctrl_dev *of_pinctrl_get(struct device_node *np)
114 return get_pinctrl_dev_from_of_node(np);
117 static int dt_to_map_one_config(struct pinctrl *p, const char *statename,
118 struct device_node *np_config)
120 struct device_node *np_pctldev;
121 struct pinctrl_dev *pctldev;
122 const struct pinctrl_ops *ops;
124 struct pinctrl_map *map;
127 /* Find the pin controller containing np_config */
128 np_pctldev = of_node_get(np_config);
130 np_pctldev = of_get_next_parent(np_pctldev);
131 if (!np_pctldev || of_node_is_root(np_pctldev)) {
132 dev_info(p->dev, "could not find pctldev for node %s, deferring probe\n",
133 np_config->full_name);
134 of_node_put(np_pctldev);
135 /* OK let's just assume this will appear later then */
136 return -EPROBE_DEFER;
138 pctldev = get_pinctrl_dev_from_of_node(np_pctldev);
141 /* Do not defer probing of hogs (circular loop) */
142 if (np_pctldev == p->dev->of_node) {
143 of_node_put(np_pctldev);
147 of_node_put(np_pctldev);
150 * Call pinctrl driver to parse device tree node, and
151 * generate mapping table entries
153 ops = pctldev->desc->pctlops;
154 if (!ops->dt_node_to_map) {
155 dev_err(p->dev, "pctldev %s doesn't support DT\n",
156 dev_name(pctldev->dev));
159 ret = ops->dt_node_to_map(pctldev, np_config, &map, &num_maps);
163 /* Stash the mapping table chunk away for later use */
164 return dt_remember_or_free_map(p, statename, pctldev, map, num_maps);
167 static int dt_remember_dummy_state(struct pinctrl *p, const char *statename)
169 struct pinctrl_map *map;
171 map = kzalloc(sizeof(*map), GFP_KERNEL);
173 dev_err(p->dev, "failed to alloc struct pinctrl_map\n");
177 /* There is no pctldev for PIN_MAP_TYPE_DUMMY_STATE */
178 map->type = PIN_MAP_TYPE_DUMMY_STATE;
180 return dt_remember_or_free_map(p, statename, NULL, map, 1);
183 int pinctrl_dt_to_map(struct pinctrl *p)
185 struct device_node *np = p->dev->of_node;
188 struct property *prop;
189 const char *statename;
193 struct device_node *np_config;
195 /* CONFIG_OF enabled, p->dev not instantiated from DT */
197 if (of_have_populated_dt())
199 "no of_node; not parsing pinctrl DT\n");
203 /* We may store pointers to property names within the node */
206 /* For each defined state ID */
207 for (state = 0; ; state++) {
208 /* Retrieve the pinctrl-* property */
209 propname = kasprintf(GFP_KERNEL, "pinctrl-%d", state);
210 prop = of_find_property(np, propname, &size);
220 size /= sizeof(*list);
222 /* Determine whether pinctrl-names property names the state */
223 ret = of_property_read_string_index(np, "pinctrl-names",
226 * If not, statename is just the integer state ID. But rather
227 * than dynamically allocate it and have to free it later,
228 * just point part way into the property name for the string.
231 /* strlen("pinctrl-") == 8 */
232 statename = prop->name + 8;
235 /* For every referenced pin configuration node in it */
236 for (config = 0; config < size; config++) {
237 phandle = be32_to_cpup(list++);
239 /* Look up the pin configuration node */
240 np_config = of_find_node_by_phandle(phandle);
243 "prop %s index %i invalid phandle\n",
250 ret = dt_to_map_one_config(p, statename, np_config);
251 of_node_put(np_config);
256 /* No entries in DT? Generate a dummy state table entry */
258 ret = dt_remember_dummy_state(p, statename);
267 pinctrl_dt_free_maps(p);