Blame view

drivers/pinctrl/devicetree.c 10.8 KB
9952f6918   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
57291ce29   Stephen Warren   pinctrl: core dev...
2
3
4
5
  /*
   * Device tree integration for the pin control subsystem
   *
   * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
57291ce29   Stephen Warren   pinctrl: core dev...
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
   */
  
  #include <linux/device.h>
  #include <linux/of.h>
  #include <linux/pinctrl/pinctrl.h>
  #include <linux/slab.h>
  
  #include "core.h"
  #include "devicetree.h"
  
  /**
   * struct pinctrl_dt_map - mapping table chunk parsed from device tree
   * @node: list node for struct pinctrl's @dt_maps field
   * @pctldev: the pin controller that allocated this struct, and will free it
   * @maps: the mapping table entries
   */
  struct pinctrl_dt_map {
  	struct list_head node;
  	struct pinctrl_dev *pctldev;
  	struct pinctrl_map *map;
  	unsigned num_maps;
  };
  
  static void dt_free_map(struct pinctrl_dev *pctldev,
  		     struct pinctrl_map *map, unsigned num_maps)
  {
f739a699d   Will Deacon   pinctrl: devicetr...
32
33
34
35
36
37
  	int i;
  
  	for (i = 0; i < num_maps; ++i) {
  		kfree_const(map[i].dev_name);
  		map[i].dev_name = NULL;
  	}
57291ce29   Stephen Warren   pinctrl: core dev...
38
  	if (pctldev) {
022ab148d   Laurent Pinchart   pinctrl: Declare ...
39
  		const struct pinctrl_ops *ops = pctldev->desc->pctlops;
92c2b6718   Tony Lindgren   pinctrl: core: Ma...
40
41
  		if (ops->dt_free_map)
  			ops->dt_free_map(pctldev, map, num_maps);
57291ce29   Stephen Warren   pinctrl: core dev...
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  	} else {
  		/* There is no pctldev for PIN_MAP_TYPE_DUMMY_STATE */
  		kfree(map);
  	}
  }
  
  void pinctrl_dt_free_maps(struct pinctrl *p)
  {
  	struct pinctrl_dt_map *dt_map, *n1;
  
  	list_for_each_entry_safe(dt_map, n1, &p->dt_maps, node) {
  		pinctrl_unregister_map(dt_map->map);
  		list_del(&dt_map->node);
  		dt_free_map(dt_map->pctldev, dt_map->map,
  			    dt_map->num_maps);
  		kfree(dt_map);
  	}
  
  	of_node_put(p->dev->of_node);
  }
  
  static int dt_remember_or_free_map(struct pinctrl *p, const char *statename,
  				   struct pinctrl_dev *pctldev,
  				   struct pinctrl_map *map, unsigned num_maps)
  {
  	int i;
  	struct pinctrl_dt_map *dt_map;
  
  	/* Initialize common mapping table entry fields */
  	for (i = 0; i < num_maps; i++) {
f739a699d   Will Deacon   pinctrl: devicetr...
72
73
74
75
76
77
78
  		const char *devname;
  
  		devname = kstrdup_const(dev_name(p->dev), GFP_KERNEL);
  		if (!devname)
  			goto err_free_map;
  
  		map[i].dev_name = devname;
57291ce29   Stephen Warren   pinctrl: core dev...
79
80
81
82
83
84
85
  		map[i].name = statename;
  		if (pctldev)
  			map[i].ctrl_dev_name = dev_name(pctldev->dev);
  	}
  
  	/* Remember the converted mapping table entries */
  	dt_map = kzalloc(sizeof(*dt_map), GFP_KERNEL);
f739a699d   Will Deacon   pinctrl: devicetr...
86
87
  	if (!dt_map)
  		goto err_free_map;
57291ce29   Stephen Warren   pinctrl: core dev...
88
89
90
91
92
  
  	dt_map->pctldev = pctldev;
  	dt_map->map = map;
  	dt_map->num_maps = num_maps;
  	list_add_tail(&dt_map->node, &p->dt_maps);
c5272a285   Doug Anderson   pinctrl: Don't ju...
93
  	return pinctrl_register_map(map, num_maps, false);
f739a699d   Will Deacon   pinctrl: devicetr...
94
95
96
97
  
  err_free_map:
  	dt_free_map(pctldev, map, num_maps);
  	return -ENOMEM;
57291ce29   Stephen Warren   pinctrl: core dev...
98
  }
1e63d7b93   Linus Walleij   gpiolib: separati...
99
  struct pinctrl_dev *of_pinctrl_get(struct device_node *np)
f23f1516b   Shiraz Hashim   gpiolib: provide ...
100
  {
fb00de771   Masahiro Yamada   pinctrl: simplify...
101
  	return get_pinctrl_dev_from_of_node(np);
f23f1516b   Shiraz Hashim   gpiolib: provide ...
102
  }
99e4f6750   Tony Lindgren   pinctrl: core: Us...
103
  static int dt_to_map_one_config(struct pinctrl *p,
bc3322bc1   Fabio Estevam   pinctrl: devicetr...
104
  				struct pinctrl_dev *hog_pctldev,
99e4f6750   Tony Lindgren   pinctrl: core: Us...
105
  				const char *statename,
57291ce29   Stephen Warren   pinctrl: core dev...
106
107
  				struct device_node *np_config)
  {
bc3322bc1   Fabio Estevam   pinctrl: devicetr...
108
  	struct pinctrl_dev *pctldev = NULL;
57291ce29   Stephen Warren   pinctrl: core dev...
109
  	struct device_node *np_pctldev;
022ab148d   Laurent Pinchart   pinctrl: Declare ...
110
  	const struct pinctrl_ops *ops;
57291ce29   Stephen Warren   pinctrl: core dev...
111
112
113
  	int ret;
  	struct pinctrl_map *map;
  	unsigned num_maps;
d19c5e79d   Rob Herring   pinctrl: Support ...
114
  	bool allow_default = false;
57291ce29   Stephen Warren   pinctrl: core dev...
115
116
117
118
  
  	/* Find the pin controller containing np_config */
  	np_pctldev = of_node_get(np_config);
  	for (;;) {
d19c5e79d   Rob Herring   pinctrl: Support ...
119
120
121
  		if (!allow_default)
  			allow_default = of_property_read_bool(np_pctldev,
  							      "pinctrl-use-default");
57291ce29   Stephen Warren   pinctrl: core dev...
122
123
  		np_pctldev = of_get_next_parent(np_pctldev);
  		if (!np_pctldev || of_node_is_root(np_pctldev)) {
57291ce29   Stephen Warren   pinctrl: core dev...
124
  			of_node_put(np_pctldev);
d19c5e79d   Rob Herring   pinctrl: Support ...
125
  			/* keep deferring if modules are enabled unless we've timed out */
62a6bc3a1   Thierry Reding   driver: core: All...
126
127
  			if (IS_ENABLED(CONFIG_MODULES) && !allow_default)
  				return driver_deferred_probe_check_state_continue(p->dev);
d19c5e79d   Rob Herring   pinctrl: Support ...
128

62a6bc3a1   Thierry Reding   driver: core: All...
129
  			return driver_deferred_probe_check_state(p->dev);
57291ce29   Stephen Warren   pinctrl: core dev...
130
  		}
b89405b61   Richard Fitzgerald   pinctrl: devicetr...
131
  		/* If we're creating a hog we can use the passed pctldev */
bc3322bc1   Fabio Estevam   pinctrl: devicetr...
132
133
  		if (hog_pctldev && (np_pctldev == p->dev->of_node)) {
  			pctldev = hog_pctldev;
b89405b61   Richard Fitzgerald   pinctrl: devicetr...
134
  			break;
bc3322bc1   Fabio Estevam   pinctrl: devicetr...
135
  		}
b89405b61   Richard Fitzgerald   pinctrl: devicetr...
136
  		pctldev = get_pinctrl_dev_from_of_node(np_pctldev);
57291ce29   Stephen Warren   pinctrl: core dev...
137
138
  		if (pctldev)
  			break;
b2083062a   Linus Walleij   pinctrl: do not d...
139
140
141
142
143
  		/* Do not defer probing of hogs (circular loop) */
  		if (np_pctldev == p->dev->of_node) {
  			of_node_put(np_pctldev);
  			return -ENODEV;
  		}
57291ce29   Stephen Warren   pinctrl: core dev...
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
  	}
  	of_node_put(np_pctldev);
  
  	/*
  	 * Call pinctrl driver to parse device tree node, and
  	 * generate mapping table entries
  	 */
  	ops = pctldev->desc->pctlops;
  	if (!ops->dt_node_to_map) {
  		dev_err(p->dev, "pctldev %s doesn't support DT
  ",
  			dev_name(pctldev->dev));
  		return -ENODEV;
  	}
  	ret = ops->dt_node_to_map(pctldev, np_config, &map, &num_maps);
  	if (ret < 0)
  		return ret;
  
  	/* Stash the mapping table chunk away for later use */
  	return dt_remember_or_free_map(p, statename, pctldev, map, num_maps);
  }
  
  static int dt_remember_dummy_state(struct pinctrl *p, const char *statename)
  {
  	struct pinctrl_map *map;
  
  	map = kzalloc(sizeof(*map), GFP_KERNEL);
9b21e72e8   Markus Elfring   pinctrl: Delete a...
171
  	if (!map)
57291ce29   Stephen Warren   pinctrl: core dev...
172
  		return -ENOMEM;
57291ce29   Stephen Warren   pinctrl: core dev...
173
174
175
176
177
178
  
  	/* There is no pctldev for PIN_MAP_TYPE_DUMMY_STATE */
  	map->type = PIN_MAP_TYPE_DUMMY_STATE;
  
  	return dt_remember_or_free_map(p, statename, NULL, map, 1);
  }
99e4f6750   Tony Lindgren   pinctrl: core: Us...
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
  bool pinctrl_dt_has_hogs(struct pinctrl_dev *pctldev)
  {
  	struct device_node *np;
  	struct property *prop;
  	int size;
  
  	np = pctldev->dev->of_node;
  	if (!np)
  		return false;
  
  	prop = of_find_property(np, "pinctrl-0", &size);
  
  	return prop ? true : false;
  }
  
  int pinctrl_dt_to_map(struct pinctrl *p, struct pinctrl_dev *pctldev)
57291ce29   Stephen Warren   pinctrl: core dev...
195
196
197
198
199
200
201
202
203
204
205
206
207
  {
  	struct device_node *np = p->dev->of_node;
  	int state, ret;
  	char *propname;
  	struct property *prop;
  	const char *statename;
  	const __be32 *list;
  	int size, config;
  	phandle phandle;
  	struct device_node *np_config;
  
  	/* CONFIG_OF enabled, p->dev not instantiated from DT */
  	if (!np) {
5d88dceac   Mark Brown   pinctrl: Quiet lo...
208
209
210
211
  		if (of_have_populated_dt())
  			dev_dbg(p->dev,
  				"no of_node; not parsing pinctrl DT
  ");
57291ce29   Stephen Warren   pinctrl: core dev...
212
213
214
215
216
217
218
219
220
221
222
223
  		return 0;
  	}
  
  	/* We may store pointers to property names within the node */
  	of_node_get(np);
  
  	/* For each defined state ID */
  	for (state = 0; ; state++) {
  		/* Retrieve the pinctrl-* property */
  		propname = kasprintf(GFP_KERNEL, "pinctrl-%d", state);
  		prop = of_find_property(np, propname, &size);
  		kfree(propname);
98849fa01   Jon Hunter   pinctrl: OF: Don'...
224
225
226
227
228
  		if (!prop) {
  			if (state == 0) {
  				of_node_put(np);
  				return -ENODEV;
  			}
57291ce29   Stephen Warren   pinctrl: core dev...
229
  			break;
98849fa01   Jon Hunter   pinctrl: OF: Don'...
230
  		}
57291ce29   Stephen Warren   pinctrl: core dev...
231
232
233
234
235
236
237
238
239
240
241
  		list = prop->value;
  		size /= sizeof(*list);
  
  		/* Determine whether pinctrl-names property names the state */
  		ret = of_property_read_string_index(np, "pinctrl-names",
  						    state, &statename);
  		/*
  		 * If not, statename is just the integer state ID. But rather
  		 * than dynamically allocate it and have to free it later,
  		 * just point part way into the property name for the string.
  		 */
f0b0e923e   Geert Uytterhoeven   pinctrl: devicetr...
242
243
  		if (ret < 0)
  			statename = prop->name + strlen("pinctrl-");
57291ce29   Stephen Warren   pinctrl: core dev...
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
  
  		/* For every referenced pin configuration node in it */
  		for (config = 0; config < size; config++) {
  			phandle = be32_to_cpup(list++);
  
  			/* Look up the pin configuration node */
  			np_config = of_find_node_by_phandle(phandle);
  			if (!np_config) {
  				dev_err(p->dev,
  					"prop %s index %i invalid phandle
  ",
  					prop->name, config);
  				ret = -EINVAL;
  				goto err;
  			}
  
  			/* Parse the node */
99e4f6750   Tony Lindgren   pinctrl: core: Us...
261
262
  			ret = dt_to_map_one_config(p, pctldev, statename,
  						   np_config);
57291ce29   Stephen Warren   pinctrl: core dev...
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
  			of_node_put(np_config);
  			if (ret < 0)
  				goto err;
  		}
  
  		/* No entries in DT? Generate a dummy state table entry */
  		if (!size) {
  			ret = dt_remember_dummy_state(p, statename);
  			if (ret < 0)
  				goto err;
  		}
  	}
  
  	return 0;
  
  err:
  	pinctrl_dt_free_maps(p);
  	return ret;
  }
42124bc59   Tony Lindgren   pinctrl: Introduc...
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
  
  /*
   * For pinctrl binding, typically #pinctrl-cells is for the pin controller
   * device, so either parent or grandparent. See pinctrl-bindings.txt.
   */
  static int pinctrl_find_cells_size(const struct device_node *np)
  {
  	const char *cells_name = "#pinctrl-cells";
  	int cells_size, error;
  
  	error = of_property_read_u32(np->parent, cells_name, &cells_size);
  	if (error) {
  		error = of_property_read_u32(np->parent->parent,
  					     cells_name, &cells_size);
  		if (error)
  			return -ENOENT;
  	}
  
  	return cells_size;
  }
  
  /**
   * pinctrl_get_list_and_count - Gets the list and it's cell size and number
   * @np: pointer to device node with the property
   * @list_name: property that contains the list
   * @list: pointer for the list found
   * @cells_size: pointer for the cell size found
   * @nr_elements: pointer for the number of elements found
   *
   * Typically np is a single pinctrl entry containing the list.
   */
  static int pinctrl_get_list_and_count(const struct device_node *np,
  				      const char *list_name,
  				      const __be32 **list,
  				      int *cells_size,
  				      int *nr_elements)
  {
  	int size;
  
  	*cells_size = 0;
  	*nr_elements = 0;
  
  	*list = of_get_property(np, list_name, &size);
  	if (!*list)
  		return -ENOENT;
  
  	*cells_size = pinctrl_find_cells_size(np);
  	if (*cells_size < 0)
  		return -ENOENT;
  
  	/* First element is always the index within the pinctrl device */
  	*nr_elements = (size / sizeof(**list)) / (*cells_size + 1);
  
  	return 0;
  }
  
  /**
   * pinctrl_count_index_with_args - Count number of elements in a pinctrl entry
   * @np: pointer to device node with the property
   * @list_name: property that contains the list
   *
   * Counts the number of elements in a pinctrl array consisting of an index
   * within the controller and a number of u32 entries specified for each
   * entry. Note that device_node is always for the parent pin controller device.
   */
  int pinctrl_count_index_with_args(const struct device_node *np,
  				  const char *list_name)
  {
  	const __be32 *list;
  	int size, nr_cells, error;
  
  	error = pinctrl_get_list_and_count(np, list_name, &list,
  					   &nr_cells, &size);
  	if (error)
  		return error;
  
  	return size;
  }
  EXPORT_SYMBOL_GPL(pinctrl_count_index_with_args);
  
  /**
   * pinctrl_copy_args - Populates of_phandle_args based on index
   * @np: pointer to device node with the property
   * @list: pointer to a list with the elements
   * @index: entry within the list of elements
   * @nr_cells: number of cells in the list
   * @nr_elem: number of elements for each entry in the list
   * @out_args: returned values
   *
   * Populates the of_phandle_args based on the index in the list.
   */
  static int pinctrl_copy_args(const struct device_node *np,
  			     const __be32 *list,
  			     int index, int nr_cells, int nr_elem,
  			     struct of_phandle_args *out_args)
  {
  	int i;
  
  	memset(out_args, 0, sizeof(*out_args));
  	out_args->np = (struct device_node *)np;
  	out_args->args_count = nr_cells + 1;
  
  	if (index >= nr_elem)
  		return -EINVAL;
  
  	list += index * (nr_cells + 1);
  
  	for (i = 0; i < nr_cells + 1; i++)
  		out_args->args[i] = be32_to_cpup(list++);
  
  	return 0;
  }
  
  /**
   * pinctrl_parse_index_with_args - Find a node pointed by index in a list
   * @np: pointer to device node with the property
   * @list_name: property that contains the list
   * @index: index within the list
   * @out_arts: entries in the list pointed by index
   *
   * Finds the selected element in a pinctrl array consisting of an index
   * within the controller and a number of u32 entries specified for each
   * entry. Note that device_node is always for the parent pin controller device.
   */
  int pinctrl_parse_index_with_args(const struct device_node *np,
  				  const char *list_name, int index,
  				  struct of_phandle_args *out_args)
  {
  	const __be32 *list;
  	int nr_elem, nr_cells, error;
  
  	error = pinctrl_get_list_and_count(np, list_name, &list,
  					   &nr_cells, &nr_elem);
  	if (error || !nr_cells)
  		return error;
  
  	error = pinctrl_copy_args(np, list, index, nr_cells, nr_elem,
  				  out_args);
  	if (error)
  		return error;
  
  	return 0;
  }
  EXPORT_SYMBOL_GPL(pinctrl_parse_index_with_args);