1 // SPDX-License-Identifier: GPL-2.0-only
3 * Device tree integration for the pin control subsystem
5 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
8 #include <linux/device.h>
10 #include <linux/pinctrl/pinctrl.h>
11 #include <linux/slab.h>
14 #include "devicetree.h"
17 * struct pinctrl_dt_map - mapping table chunk parsed from device tree
18 * @node: list node for struct pinctrl's @dt_maps field
19 * @pctldev: the pin controller that allocated this struct, and will free it
20 * @maps: the mapping table entries
22 struct pinctrl_dt_map {
23 struct list_head node;
24 struct pinctrl_dev *pctldev;
25 struct pinctrl_map *map;
29 static void dt_free_map(struct pinctrl_dev *pctldev,
30 struct pinctrl_map *map, unsigned num_maps)
33 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
35 ops->dt_free_map(pctldev, map, num_maps);
37 /* There is no pctldev for PIN_MAP_TYPE_DUMMY_STATE */
42 void pinctrl_dt_free_maps(struct pinctrl *p)
44 struct pinctrl_dt_map *dt_map, *n1;
46 list_for_each_entry_safe(dt_map, n1, &p->dt_maps, node) {
47 pinctrl_unregister_map(dt_map->map);
48 list_del(&dt_map->node);
49 dt_free_map(dt_map->pctldev, dt_map->map,
54 of_node_put(p->dev->of_node);
57 static int dt_remember_or_free_map(struct pinctrl *p, const char *statename,
58 struct pinctrl_dev *pctldev,
59 struct pinctrl_map *map, unsigned num_maps)
62 struct pinctrl_dt_map *dt_map;
64 /* Initialize common mapping table entry fields */
65 for (i = 0; i < num_maps; i++) {
66 map[i].dev_name = dev_name(p->dev);
67 map[i].name = statename;
69 map[i].ctrl_dev_name = dev_name(pctldev->dev);
72 /* Remember the converted mapping table entries */
73 dt_map = kzalloc(sizeof(*dt_map), GFP_KERNEL);
75 dt_free_map(pctldev, map, num_maps);
79 dt_map->pctldev = pctldev;
81 dt_map->num_maps = num_maps;
82 list_add_tail(&dt_map->node, &p->dt_maps);
84 return pinctrl_register_map(map, num_maps, false);
87 struct pinctrl_dev *of_pinctrl_get(struct device_node *np)
89 return get_pinctrl_dev_from_of_node(np);
92 static int dt_to_map_one_config(struct pinctrl *p,
93 struct pinctrl_dev *hog_pctldev,
94 const char *statename,
95 struct device_node *np_config)
97 struct pinctrl_dev *pctldev = NULL;
98 struct device_node *np_pctldev;
99 const struct pinctrl_ops *ops;
101 struct pinctrl_map *map;
103 bool allow_default = false;
105 /* Find the pin controller containing np_config */
106 np_pctldev = of_node_get(np_config);
109 allow_default = of_property_read_bool(np_pctldev,
110 "pinctrl-use-default");
112 np_pctldev = of_get_next_parent(np_pctldev);
113 if (!np_pctldev || of_node_is_root(np_pctldev)) {
114 of_node_put(np_pctldev);
115 ret = driver_deferred_probe_check_state(p->dev);
116 /* keep deferring if modules are enabled unless we've timed out */
117 if (IS_ENABLED(CONFIG_MODULES) && !allow_default && ret == -ENODEV)
122 /* If we're creating a hog we can use the passed pctldev */
123 if (hog_pctldev && (np_pctldev == p->dev->of_node)) {
124 pctldev = hog_pctldev;
127 pctldev = get_pinctrl_dev_from_of_node(np_pctldev);
130 /* Do not defer probing of hogs (circular loop) */
131 if (np_pctldev == p->dev->of_node) {
132 of_node_put(np_pctldev);
136 of_node_put(np_pctldev);
139 * Call pinctrl driver to parse device tree node, and
140 * generate mapping table entries
142 ops = pctldev->desc->pctlops;
143 if (!ops->dt_node_to_map) {
144 dev_err(p->dev, "pctldev %s doesn't support DT\n",
145 dev_name(pctldev->dev));
148 ret = ops->dt_node_to_map(pctldev, np_config, &map, &num_maps);
152 /* Stash the mapping table chunk away for later use */
153 return dt_remember_or_free_map(p, statename, pctldev, map, num_maps);
156 static int dt_remember_dummy_state(struct pinctrl *p, const char *statename)
158 struct pinctrl_map *map;
160 map = kzalloc(sizeof(*map), GFP_KERNEL);
164 /* There is no pctldev for PIN_MAP_TYPE_DUMMY_STATE */
165 map->type = PIN_MAP_TYPE_DUMMY_STATE;
167 return dt_remember_or_free_map(p, statename, NULL, map, 1);
170 bool pinctrl_dt_has_hogs(struct pinctrl_dev *pctldev)
172 struct device_node *np;
173 struct property *prop;
176 np = pctldev->dev->of_node;
180 prop = of_find_property(np, "pinctrl-0", &size);
182 return prop ? true : false;
185 int pinctrl_dt_to_map(struct pinctrl *p, struct pinctrl_dev *pctldev)
187 struct device_node *np = p->dev->of_node;
190 struct property *prop;
191 const char *statename;
195 struct device_node *np_config;
197 /* CONFIG_OF enabled, p->dev not instantiated from DT */
199 if (of_have_populated_dt())
201 "no of_node; not parsing pinctrl DT\n");
205 /* We may store pointers to property names within the node */
208 /* For each defined state ID */
209 for (state = 0; ; state++) {
210 /* Retrieve the pinctrl-* property */
211 propname = kasprintf(GFP_KERNEL, "pinctrl-%d", state);
212 prop = of_find_property(np, propname, &size);
222 size /= sizeof(*list);
224 /* Determine whether pinctrl-names property names the state */
225 ret = of_property_read_string_index(np, "pinctrl-names",
228 * If not, statename is just the integer state ID. But rather
229 * than dynamically allocate it and have to free it later,
230 * just point part way into the property name for the string.
233 /* strlen("pinctrl-") == 8 */
234 statename = prop->name + 8;
237 /* For every referenced pin configuration node in it */
238 for (config = 0; config < size; config++) {
239 phandle = be32_to_cpup(list++);
241 /* Look up the pin configuration node */
242 np_config = of_find_node_by_phandle(phandle);
245 "prop %s index %i invalid phandle\n",
252 ret = dt_to_map_one_config(p, pctldev, statename,
254 of_node_put(np_config);
259 /* No entries in DT? Generate a dummy state table entry */
261 ret = dt_remember_dummy_state(p, statename);
270 pinctrl_dt_free_maps(p);
275 * For pinctrl binding, typically #pinctrl-cells is for the pin controller
276 * device, so either parent or grandparent. See pinctrl-bindings.txt.
278 static int pinctrl_find_cells_size(const struct device_node *np)
280 const char *cells_name = "#pinctrl-cells";
281 int cells_size, error;
283 error = of_property_read_u32(np->parent, cells_name, &cells_size);
285 error = of_property_read_u32(np->parent->parent,
286 cells_name, &cells_size);
295 * pinctrl_get_list_and_count - Gets the list and it's cell size and number
296 * @np: pointer to device node with the property
297 * @list_name: property that contains the list
298 * @list: pointer for the list found
299 * @cells_size: pointer for the cell size found
300 * @nr_elements: pointer for the number of elements found
302 * Typically np is a single pinctrl entry containing the list.
304 static int pinctrl_get_list_and_count(const struct device_node *np,
305 const char *list_name,
315 *list = of_get_property(np, list_name, &size);
319 *cells_size = pinctrl_find_cells_size(np);
323 /* First element is always the index within the pinctrl device */
324 *nr_elements = (size / sizeof(**list)) / (*cells_size + 1);
330 * pinctrl_count_index_with_args - Count number of elements in a pinctrl entry
331 * @np: pointer to device node with the property
332 * @list_name: property that contains the list
334 * Counts the number of elements in a pinctrl array consisting of an index
335 * within the controller and a number of u32 entries specified for each
336 * entry. Note that device_node is always for the parent pin controller device.
338 int pinctrl_count_index_with_args(const struct device_node *np,
339 const char *list_name)
342 int size, nr_cells, error;
344 error = pinctrl_get_list_and_count(np, list_name, &list,
351 EXPORT_SYMBOL_GPL(pinctrl_count_index_with_args);
354 * pinctrl_copy_args - Populates of_phandle_args based on index
355 * @np: pointer to device node with the property
356 * @list: pointer to a list with the elements
357 * @index: entry within the list of elements
358 * @nr_cells: number of cells in the list
359 * @nr_elem: number of elements for each entry in the list
360 * @out_args: returned values
362 * Populates the of_phandle_args based on the index in the list.
364 static int pinctrl_copy_args(const struct device_node *np,
366 int index, int nr_cells, int nr_elem,
367 struct of_phandle_args *out_args)
371 memset(out_args, 0, sizeof(*out_args));
372 out_args->np = (struct device_node *)np;
373 out_args->args_count = nr_cells + 1;
375 if (index >= nr_elem)
378 list += index * (nr_cells + 1);
380 for (i = 0; i < nr_cells + 1; i++)
381 out_args->args[i] = be32_to_cpup(list++);
387 * pinctrl_parse_index_with_args - Find a node pointed by index in a list
388 * @np: pointer to device node with the property
389 * @list_name: property that contains the list
390 * @index: index within the list
391 * @out_arts: entries in the list pointed by index
393 * Finds the selected element in a pinctrl array consisting of an index
394 * within the controller and a number of u32 entries specified for each
395 * entry. Note that device_node is always for the parent pin controller device.
397 int pinctrl_parse_index_with_args(const struct device_node *np,
398 const char *list_name, int index,
399 struct of_phandle_args *out_args)
402 int nr_elem, nr_cells, error;
404 error = pinctrl_get_list_and_count(np, list_name, &list,
405 &nr_cells, &nr_elem);
406 if (error || !nr_cells)
409 error = pinctrl_copy_args(np, list, index, nr_cells, nr_elem,
416 EXPORT_SYMBOL_GPL(pinctrl_parse_index_with_args);