xref: /linux/drivers/pinctrl/devicetree.c (revision f4738f56d1dc62aaba69b33702a5ab098f1b8c63)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Device tree integration for the pin control subsystem
4  *
5  * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
6  */
7 
8 #include <linux/device.h>
9 #include <linux/of.h>
10 #include <linux/pinctrl/pinctrl.h>
11 #include <linux/slab.h>
12 
13 #include "core.h"
14 #include "devicetree.h"
15 
16 /**
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  * @map: the mapping table entries
21  * @num_maps: number of mapping table entries
22  */
23 struct pinctrl_dt_map {
24 	struct list_head node;
25 	struct pinctrl_dev *pctldev;
26 	struct pinctrl_map *map;
27 	unsigned num_maps;
28 };
29 
30 static void dt_free_map(struct pinctrl_dev *pctldev,
31 		     struct pinctrl_map *map, unsigned num_maps)
32 {
33 	int i;
34 
35 	for (i = 0; i < num_maps; ++i) {
36 		kfree_const(map[i].dev_name);
37 		map[i].dev_name = NULL;
38 	}
39 
40 	if (pctldev) {
41 		const struct pinctrl_ops *ops = pctldev->desc->pctlops;
42 		if (ops->dt_free_map)
43 			ops->dt_free_map(pctldev, map, num_maps);
44 	} else {
45 		/* There is no pctldev for PIN_MAP_TYPE_DUMMY_STATE */
46 		kfree(map);
47 	}
48 }
49 
50 void pinctrl_dt_free_maps(struct pinctrl *p)
51 {
52 	struct pinctrl_dt_map *dt_map, *n1;
53 
54 	list_for_each_entry_safe(dt_map, n1, &p->dt_maps, node) {
55 		pinctrl_unregister_mappings(dt_map->map);
56 		list_del(&dt_map->node);
57 		dt_free_map(dt_map->pctldev, dt_map->map,
58 			    dt_map->num_maps);
59 		kfree(dt_map);
60 	}
61 
62 	of_node_put(p->dev->of_node);
63 }
64 
65 static int dt_remember_or_free_map(struct pinctrl *p, const char *statename,
66 				   struct pinctrl_dev *pctldev,
67 				   struct pinctrl_map *map, unsigned num_maps)
68 {
69 	int i;
70 	struct pinctrl_dt_map *dt_map;
71 
72 	/* Initialize common mapping table entry fields */
73 	for (i = 0; i < num_maps; i++) {
74 		const char *devname;
75 
76 		devname = kstrdup_const(dev_name(p->dev), GFP_KERNEL);
77 		if (!devname)
78 			goto err_free_map;
79 
80 		map[i].dev_name = devname;
81 		map[i].name = statename;
82 		if (pctldev)
83 			map[i].ctrl_dev_name = dev_name(pctldev->dev);
84 	}
85 
86 	/* Remember the converted mapping table entries */
87 	dt_map = kzalloc(sizeof(*dt_map), GFP_KERNEL);
88 	if (!dt_map)
89 		goto err_free_map;
90 
91 	dt_map->pctldev = pctldev;
92 	dt_map->map = map;
93 	dt_map->num_maps = num_maps;
94 	list_add_tail(&dt_map->node, &p->dt_maps);
95 
96 	return pinctrl_register_mappings(map, num_maps);
97 
98 err_free_map:
99 	dt_free_map(pctldev, map, num_maps);
100 	return -ENOMEM;
101 }
102 
103 struct pinctrl_dev *of_pinctrl_get(struct device_node *np)
104 {
105 	return get_pinctrl_dev_from_of_node(np);
106 }
107 EXPORT_SYMBOL_GPL(of_pinctrl_get);
108 
109 static int dt_to_map_one_config(struct pinctrl *p,
110 				struct pinctrl_dev *hog_pctldev,
111 				const char *statename,
112 				struct device_node *np_config)
113 {
114 	struct pinctrl_dev *pctldev = NULL;
115 	struct device_node *np_pctldev;
116 	const struct pinctrl_ops *ops;
117 	int ret;
118 	struct pinctrl_map *map;
119 	unsigned num_maps;
120 	bool allow_default = false;
121 
122 	/* Find the pin controller containing np_config */
123 	np_pctldev = of_node_get(np_config);
124 	for (;;) {
125 		if (!allow_default)
126 			allow_default = of_property_read_bool(np_pctldev,
127 							      "pinctrl-use-default");
128 
129 		np_pctldev = of_get_next_parent(np_pctldev);
130 		if (!np_pctldev || of_node_is_root(np_pctldev)) {
131 			of_node_put(np_pctldev);
132 			ret = -ENODEV;
133 			/* keep deferring if modules are enabled */
134 			if (IS_ENABLED(CONFIG_MODULES) && !allow_default && ret < 0)
135 				ret = -EPROBE_DEFER;
136 			return ret;
137 		}
138 		/* If we're creating a hog we can use the passed pctldev */
139 		if (hog_pctldev && (np_pctldev == p->dev->of_node)) {
140 			pctldev = hog_pctldev;
141 			break;
142 		}
143 		pctldev = get_pinctrl_dev_from_of_node(np_pctldev);
144 		if (pctldev)
145 			break;
146 		/* Do not defer probing of hogs (circular loop) */
147 		if (np_pctldev == p->dev->of_node) {
148 			of_node_put(np_pctldev);
149 			return -ENODEV;
150 		}
151 	}
152 	of_node_put(np_pctldev);
153 
154 	/*
155 	 * Call pinctrl driver to parse device tree node, and
156 	 * generate mapping table entries
157 	 */
158 	ops = pctldev->desc->pctlops;
159 	if (!ops->dt_node_to_map) {
160 		dev_err(p->dev, "pctldev %s doesn't support DT\n",
161 			dev_name(pctldev->dev));
162 		return -ENODEV;
163 	}
164 	ret = ops->dt_node_to_map(pctldev, np_config, &map, &num_maps);
165 	if (ret < 0)
166 		return ret;
167 	else if (num_maps == 0) {
168 		/*
169 		 * If we have no valid maps (maybe caused by empty pinctrl node
170 		 * or typing error) ther is no need remember this, so just
171 		 * return.
172 		 */
173 		dev_info(p->dev,
174 			 "there is not valid maps for state %s\n", statename);
175 		return 0;
176 	}
177 
178 	/* Stash the mapping table chunk away for later use */
179 	return dt_remember_or_free_map(p, statename, pctldev, map, num_maps);
180 }
181 
182 static int dt_remember_dummy_state(struct pinctrl *p, const char *statename)
183 {
184 	struct pinctrl_map *map;
185 
186 	map = kzalloc(sizeof(*map), GFP_KERNEL);
187 	if (!map)
188 		return -ENOMEM;
189 
190 	/* There is no pctldev for PIN_MAP_TYPE_DUMMY_STATE */
191 	map->type = PIN_MAP_TYPE_DUMMY_STATE;
192 
193 	return dt_remember_or_free_map(p, statename, NULL, map, 1);
194 }
195 
196 int pinctrl_dt_to_map(struct pinctrl *p, struct pinctrl_dev *pctldev)
197 {
198 	struct device_node *np = p->dev->of_node;
199 	int state, ret;
200 	char *propname;
201 	struct property *prop;
202 	const char *statename;
203 	const __be32 *list;
204 	int size, config;
205 	phandle phandle;
206 	struct device_node *np_config;
207 
208 	/* CONFIG_OF enabled, p->dev not instantiated from DT */
209 	if (!np) {
210 		if (of_have_populated_dt())
211 			dev_dbg(p->dev,
212 				"no of_node; not parsing pinctrl DT\n");
213 		return 0;
214 	}
215 
216 	/* We may store pointers to property names within the node */
217 	of_node_get(np);
218 
219 	/* For each defined state ID */
220 	for (state = 0; ; state++) {
221 		/* Retrieve the pinctrl-* property */
222 		propname = kasprintf(GFP_KERNEL, "pinctrl-%d", state);
223 		if (!propname)
224 			return -ENOMEM;
225 		prop = of_find_property(np, propname, &size);
226 		kfree(propname);
227 		if (!prop) {
228 			if (state == 0) {
229 				of_node_put(np);
230 				return -ENODEV;
231 			}
232 			break;
233 		}
234 		list = prop->value;
235 		size /= sizeof(*list);
236 
237 		/* Determine whether pinctrl-names property names the state */
238 		ret = of_property_read_string_index(np, "pinctrl-names",
239 						    state, &statename);
240 		/*
241 		 * If not, statename is just the integer state ID. But rather
242 		 * than dynamically allocate it and have to free it later,
243 		 * just point part way into the property name for the string.
244 		 */
245 		if (ret < 0)
246 			statename = prop->name + strlen("pinctrl-");
247 
248 		/* For every referenced pin configuration node in it */
249 		for (config = 0; config < size; config++) {
250 			phandle = be32_to_cpup(list++);
251 
252 			/* Look up the pin configuration node */
253 			np_config = of_find_node_by_phandle(phandle);
254 			if (!np_config) {
255 				dev_err(p->dev,
256 					"prop %s index %i invalid phandle\n",
257 					prop->name, config);
258 				ret = -EINVAL;
259 				goto err;
260 			}
261 
262 			/* Parse the node */
263 			ret = dt_to_map_one_config(p, pctldev, statename,
264 						   np_config);
265 			of_node_put(np_config);
266 			if (ret < 0)
267 				goto err;
268 		}
269 
270 		/* No entries in DT? Generate a dummy state table entry */
271 		if (!size) {
272 			ret = dt_remember_dummy_state(p, statename);
273 			if (ret < 0)
274 				goto err;
275 		}
276 	}
277 
278 	return 0;
279 
280 err:
281 	pinctrl_dt_free_maps(p);
282 	return ret;
283 }
284 
285 /*
286  * For pinctrl binding, typically #pinctrl-cells is for the pin controller
287  * device, so either parent or grandparent. See pinctrl-bindings.txt.
288  */
289 static int pinctrl_find_cells_size(const struct device_node *np)
290 {
291 	const char *cells_name = "#pinctrl-cells";
292 	int cells_size, error;
293 
294 	error = of_property_read_u32(np->parent, cells_name, &cells_size);
295 	if (error) {
296 		error = of_property_read_u32(np->parent->parent,
297 					     cells_name, &cells_size);
298 		if (error)
299 			return -ENOENT;
300 	}
301 
302 	return cells_size;
303 }
304 
305 /**
306  * pinctrl_get_list_and_count - Gets the list and it's cell size and number
307  * @np: pointer to device node with the property
308  * @list_name: property that contains the list
309  * @list: pointer for the list found
310  * @cells_size: pointer for the cell size found
311  * @nr_elements: pointer for the number of elements found
312  *
313  * Typically np is a single pinctrl entry containing the list.
314  */
315 static int pinctrl_get_list_and_count(const struct device_node *np,
316 				      const char *list_name,
317 				      const __be32 **list,
318 				      int *cells_size,
319 				      int *nr_elements)
320 {
321 	int size;
322 
323 	*cells_size = 0;
324 	*nr_elements = 0;
325 
326 	*list = of_get_property(np, list_name, &size);
327 	if (!*list)
328 		return -ENOENT;
329 
330 	*cells_size = pinctrl_find_cells_size(np);
331 	if (*cells_size < 0)
332 		return -ENOENT;
333 
334 	/* First element is always the index within the pinctrl device */
335 	*nr_elements = (size / sizeof(**list)) / (*cells_size + 1);
336 
337 	return 0;
338 }
339 
340 /**
341  * pinctrl_count_index_with_args - Count number of elements in a pinctrl entry
342  * @np: pointer to device node with the property
343  * @list_name: property that contains the list
344  *
345  * Counts the number of elements in a pinctrl array consisting of an index
346  * within the controller and a number of u32 entries specified for each
347  * entry. Note that device_node is always for the parent pin controller device.
348  */
349 int pinctrl_count_index_with_args(const struct device_node *np,
350 				  const char *list_name)
351 {
352 	const __be32 *list;
353 	int size, nr_cells, error;
354 
355 	error = pinctrl_get_list_and_count(np, list_name, &list,
356 					   &nr_cells, &size);
357 	if (error)
358 		return error;
359 
360 	return size;
361 }
362 EXPORT_SYMBOL_GPL(pinctrl_count_index_with_args);
363 
364 /**
365  * pinctrl_copy_args - Populates of_phandle_args based on index
366  * @np: pointer to device node with the property
367  * @list: pointer to a list with the elements
368  * @index: entry within the list of elements
369  * @nr_cells: number of cells in the list
370  * @nr_elem: number of elements for each entry in the list
371  * @out_args: returned values
372  *
373  * Populates the of_phandle_args based on the index in the list.
374  */
375 static int pinctrl_copy_args(const struct device_node *np,
376 			     const __be32 *list,
377 			     int index, int nr_cells, int nr_elem,
378 			     struct of_phandle_args *out_args)
379 {
380 	int i;
381 
382 	memset(out_args, 0, sizeof(*out_args));
383 	out_args->np = (struct device_node *)np;
384 	out_args->args_count = nr_cells + 1;
385 
386 	if (index >= nr_elem)
387 		return -EINVAL;
388 
389 	list += index * (nr_cells + 1);
390 
391 	for (i = 0; i < nr_cells + 1; i++)
392 		out_args->args[i] = be32_to_cpup(list++);
393 
394 	return 0;
395 }
396 
397 /**
398  * pinctrl_parse_index_with_args - Find a node pointed by index in a list
399  * @np: pointer to device node with the property
400  * @list_name: property that contains the list
401  * @index: index within the list
402  * @out_args: entries in the list pointed by index
403  *
404  * Finds the selected element in a pinctrl array consisting of an index
405  * within the controller and a number of u32 entries specified for each
406  * entry. Note that device_node is always for the parent pin controller device.
407  */
408 int pinctrl_parse_index_with_args(const struct device_node *np,
409 				  const char *list_name, int index,
410 				  struct of_phandle_args *out_args)
411 {
412 	const __be32 *list;
413 	int nr_elem, nr_cells, error;
414 
415 	error = pinctrl_get_list_and_count(np, list_name, &list,
416 					   &nr_cells, &nr_elem);
417 	if (error || !nr_cells)
418 		return error;
419 
420 	error = pinctrl_copy_args(np, list, index, nr_cells, nr_elem,
421 				  out_args);
422 	if (error)
423 		return error;
424 
425 	return 0;
426 }
427 EXPORT_SYMBOL_GPL(pinctrl_parse_index_with_args);
428