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