dm integrity: use kzalloc() instead of kmalloc() + memset()
[linux-block.git] / drivers / pinctrl / devicetree.c
CommitLineData
9952f691 1// SPDX-License-Identifier: GPL-2.0-only
57291ce2
SW
2/*
3 * Device tree integration for the pin control subsystem
4 *
5 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
57291ce2
SW
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 * @maps: the mapping table entries
21 */
22struct pinctrl_dt_map {
23 struct list_head node;
24 struct pinctrl_dev *pctldev;
25 struct pinctrl_map *map;
26 unsigned num_maps;
27};
28
29static void dt_free_map(struct pinctrl_dev *pctldev,
30 struct pinctrl_map *map, unsigned num_maps)
31{
32 if (pctldev) {
022ab148 33 const struct pinctrl_ops *ops = pctldev->desc->pctlops;
92c2b671
TL
34 if (ops->dt_free_map)
35 ops->dt_free_map(pctldev, map, num_maps);
57291ce2
SW
36 } else {
37 /* There is no pctldev for PIN_MAP_TYPE_DUMMY_STATE */
38 kfree(map);
39 }
40}
41
42void pinctrl_dt_free_maps(struct pinctrl *p)
43{
44 struct pinctrl_dt_map *dt_map, *n1;
45
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,
50 dt_map->num_maps);
51 kfree(dt_map);
52 }
53
54 of_node_put(p->dev->of_node);
55}
56
57static 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)
60{
61 int i;
62 struct pinctrl_dt_map *dt_map;
63
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;
68 if (pctldev)
69 map[i].ctrl_dev_name = dev_name(pctldev->dev);
70 }
71
72 /* Remember the converted mapping table entries */
73 dt_map = kzalloc(sizeof(*dt_map), GFP_KERNEL);
74 if (!dt_map) {
57291ce2
SW
75 dt_free_map(pctldev, map, num_maps);
76 return -ENOMEM;
77 }
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
c5272a28 84 return pinctrl_register_map(map, num_maps, false);
57291ce2
SW
85}
86
1e63d7b9 87struct pinctrl_dev *of_pinctrl_get(struct device_node *np)
f23f1516 88{
fb00de77 89 return get_pinctrl_dev_from_of_node(np);
f23f1516
SH
90}
91
99e4f675 92static int dt_to_map_one_config(struct pinctrl *p,
bc3322bc 93 struct pinctrl_dev *hog_pctldev,
99e4f675 94 const char *statename,
57291ce2
SW
95 struct device_node *np_config)
96{
bc3322bc 97 struct pinctrl_dev *pctldev = NULL;
57291ce2 98 struct device_node *np_pctldev;
022ab148 99 const struct pinctrl_ops *ops;
57291ce2
SW
100 int ret;
101 struct pinctrl_map *map;
102 unsigned num_maps;
d19c5e79 103 bool allow_default = false;
57291ce2
SW
104
105 /* Find the pin controller containing np_config */
106 np_pctldev = of_node_get(np_config);
107 for (;;) {
d19c5e79
RH
108 if (!allow_default)
109 allow_default = of_property_read_bool(np_pctldev,
110 "pinctrl-use-default");
111
57291ce2
SW
112 np_pctldev = of_get_next_parent(np_pctldev);
113 if (!np_pctldev || of_node_is_root(np_pctldev)) {
57291ce2 114 of_node_put(np_pctldev);
d19c5e79
RH
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)
118 ret = -EPROBE_DEFER;
119
120 return ret;
57291ce2 121 }
b89405b6 122 /* If we're creating a hog we can use the passed pctldev */
bc3322bc
FE
123 if (hog_pctldev && (np_pctldev == p->dev->of_node)) {
124 pctldev = hog_pctldev;
b89405b6 125 break;
bc3322bc 126 }
b89405b6 127 pctldev = get_pinctrl_dev_from_of_node(np_pctldev);
57291ce2
SW
128 if (pctldev)
129 break;
b2083062
LW
130 /* Do not defer probing of hogs (circular loop) */
131 if (np_pctldev == p->dev->of_node) {
132 of_node_put(np_pctldev);
133 return -ENODEV;
134 }
57291ce2
SW
135 }
136 of_node_put(np_pctldev);
137
138 /*
139 * Call pinctrl driver to parse device tree node, and
140 * generate mapping table entries
141 */
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));
146 return -ENODEV;
147 }
148 ret = ops->dt_node_to_map(pctldev, np_config, &map, &num_maps);
149 if (ret < 0)
150 return ret;
151
152 /* Stash the mapping table chunk away for later use */
153 return dt_remember_or_free_map(p, statename, pctldev, map, num_maps);
154}
155
156static int dt_remember_dummy_state(struct pinctrl *p, const char *statename)
157{
158 struct pinctrl_map *map;
159
160 map = kzalloc(sizeof(*map), GFP_KERNEL);
9b21e72e 161 if (!map)
57291ce2 162 return -ENOMEM;
57291ce2
SW
163
164 /* There is no pctldev for PIN_MAP_TYPE_DUMMY_STATE */
165 map->type = PIN_MAP_TYPE_DUMMY_STATE;
166
167 return dt_remember_or_free_map(p, statename, NULL, map, 1);
168}
169
99e4f675
TL
170bool pinctrl_dt_has_hogs(struct pinctrl_dev *pctldev)
171{
172 struct device_node *np;
173 struct property *prop;
174 int size;
175
176 np = pctldev->dev->of_node;
177 if (!np)
178 return false;
179
180 prop = of_find_property(np, "pinctrl-0", &size);
181
182 return prop ? true : false;
183}
184
185int pinctrl_dt_to_map(struct pinctrl *p, struct pinctrl_dev *pctldev)
57291ce2
SW
186{
187 struct device_node *np = p->dev->of_node;
188 int state, ret;
189 char *propname;
190 struct property *prop;
191 const char *statename;
192 const __be32 *list;
193 int size, config;
194 phandle phandle;
195 struct device_node *np_config;
196
197 /* CONFIG_OF enabled, p->dev not instantiated from DT */
198 if (!np) {
5d88dcea
MB
199 if (of_have_populated_dt())
200 dev_dbg(p->dev,
201 "no of_node; not parsing pinctrl DT\n");
57291ce2
SW
202 return 0;
203 }
204
205 /* We may store pointers to property names within the node */
206 of_node_get(np);
207
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);
213 kfree(propname);
98849fa0
JH
214 if (!prop) {
215 if (state == 0) {
216 of_node_put(np);
217 return -ENODEV;
218 }
57291ce2 219 break;
98849fa0 220 }
57291ce2
SW
221 list = prop->value;
222 size /= sizeof(*list);
223
224 /* Determine whether pinctrl-names property names the state */
225 ret = of_property_read_string_index(np, "pinctrl-names",
226 state, &statename);
227 /*
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.
231 */
232 if (ret < 0) {
233 /* strlen("pinctrl-") == 8 */
234 statename = prop->name + 8;
235 }
236
237 /* For every referenced pin configuration node in it */
238 for (config = 0; config < size; config++) {
239 phandle = be32_to_cpup(list++);
240
241 /* Look up the pin configuration node */
242 np_config = of_find_node_by_phandle(phandle);
243 if (!np_config) {
244 dev_err(p->dev,
245 "prop %s index %i invalid phandle\n",
246 prop->name, config);
247 ret = -EINVAL;
248 goto err;
249 }
250
251 /* Parse the node */
99e4f675
TL
252 ret = dt_to_map_one_config(p, pctldev, statename,
253 np_config);
57291ce2
SW
254 of_node_put(np_config);
255 if (ret < 0)
256 goto err;
257 }
258
259 /* No entries in DT? Generate a dummy state table entry */
260 if (!size) {
261 ret = dt_remember_dummy_state(p, statename);
262 if (ret < 0)
263 goto err;
264 }
265 }
266
267 return 0;
268
269err:
270 pinctrl_dt_free_maps(p);
271 return ret;
272}
42124bc5
TL
273
274/*
275 * For pinctrl binding, typically #pinctrl-cells is for the pin controller
276 * device, so either parent or grandparent. See pinctrl-bindings.txt.
277 */
278static int pinctrl_find_cells_size(const struct device_node *np)
279{
280 const char *cells_name = "#pinctrl-cells";
281 int cells_size, error;
282
283 error = of_property_read_u32(np->parent, cells_name, &cells_size);
284 if (error) {
285 error = of_property_read_u32(np->parent->parent,
286 cells_name, &cells_size);
287 if (error)
288 return -ENOENT;
289 }
290
291 return cells_size;
292}
293
294/**
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
301 *
302 * Typically np is a single pinctrl entry containing the list.
303 */
304static int pinctrl_get_list_and_count(const struct device_node *np,
305 const char *list_name,
306 const __be32 **list,
307 int *cells_size,
308 int *nr_elements)
309{
310 int size;
311
312 *cells_size = 0;
313 *nr_elements = 0;
314
315 *list = of_get_property(np, list_name, &size);
316 if (!*list)
317 return -ENOENT;
318
319 *cells_size = pinctrl_find_cells_size(np);
320 if (*cells_size < 0)
321 return -ENOENT;
322
323 /* First element is always the index within the pinctrl device */
324 *nr_elements = (size / sizeof(**list)) / (*cells_size + 1);
325
326 return 0;
327}
328
329/**
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
333 *
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.
337 */
338int pinctrl_count_index_with_args(const struct device_node *np,
339 const char *list_name)
340{
341 const __be32 *list;
342 int size, nr_cells, error;
343
344 error = pinctrl_get_list_and_count(np, list_name, &list,
345 &nr_cells, &size);
346 if (error)
347 return error;
348
349 return size;
350}
351EXPORT_SYMBOL_GPL(pinctrl_count_index_with_args);
352
353/**
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
361 *
362 * Populates the of_phandle_args based on the index in the list.
363 */
364static int pinctrl_copy_args(const struct device_node *np,
365 const __be32 *list,
366 int index, int nr_cells, int nr_elem,
367 struct of_phandle_args *out_args)
368{
369 int i;
370
371 memset(out_args, 0, sizeof(*out_args));
372 out_args->np = (struct device_node *)np;
373 out_args->args_count = nr_cells + 1;
374
375 if (index >= nr_elem)
376 return -EINVAL;
377
378 list += index * (nr_cells + 1);
379
380 for (i = 0; i < nr_cells + 1; i++)
381 out_args->args[i] = be32_to_cpup(list++);
382
383 return 0;
384}
385
386/**
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
392 *
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.
396 */
397int pinctrl_parse_index_with_args(const struct device_node *np,
398 const char *list_name, int index,
399 struct of_phandle_args *out_args)
400{
401 const __be32 *list;
402 int nr_elem, nr_cells, error;
403
404 error = pinctrl_get_list_and_count(np, list_name, &list,
405 &nr_cells, &nr_elem);
406 if (error || !nr_cells)
407 return error;
408
409 error = pinctrl_copy_args(np, list, index, nr_cells, nr_elem,
410 out_args);
411 if (error)
412 return error;
413
414 return 0;
415}
416EXPORT_SYMBOL_GPL(pinctrl_parse_index_with_args);