1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * drivers/of/property.c - Procedures for accessing and interpreting
4 * Devicetree properties and graphs.
5 *
6 * Initially created by copying procedures from drivers/of/base.c. This
7 * file contains the OF property as well as the OF graph interface
8 * functions.
9 *
10 * Paul Mackerras August 1996.
11 * Copyright (C) 1996-2005 Paul Mackerras.
12 *
13 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
14 * {engebret|bergner}@us.ibm.com
15 *
16 * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
17 *
18 * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
19 * Grant Likely.
20 */
21
22 #define pr_fmt(fmt) "OF: " fmt
23
24 #include <linux/ctype.h>
25 #include <linux/of.h>
26 #include <linux/of_address.h>
27 #include <linux/of_device.h>
28 #include <linux/of_graph.h>
29 #include <linux/of_irq.h>
30 #include <linux/string.h>
31 #include <linux/moduleparam.h>
32
33 #include "of_private.h"
34
35 /**
36 * of_property_read_bool - Find a property
37 * @np: device node from which the property value is to be read.
38 * @propname: name of the property to be searched.
39 *
40 * Search for a boolean property in a device node. Usage on non-boolean
41 * property types is deprecated.
42 *
43 * Return: true if the property exists false otherwise.
44 */
of_property_read_bool(const struct device_node * np,const char * propname)45 bool of_property_read_bool(const struct device_node *np, const char *propname)
46 {
47 struct property *prop = of_find_property(np, propname, NULL);
48
49 /*
50 * Boolean properties should not have a value. Testing for property
51 * presence should either use of_property_present() or just read the
52 * property value and check the returned error code.
53 */
54 if (prop && prop->length)
55 pr_warn("%pOF: Read of boolean property '%s' with a value.\n", np, propname);
56
57 return prop ? true : false;
58 }
59 EXPORT_SYMBOL(of_property_read_bool);
60
61 /**
62 * of_graph_is_present() - check graph's presence
63 * @node: pointer to device_node containing graph port
64 *
65 * Return: True if @node has a port or ports (with a port) sub-node,
66 * false otherwise.
67 */
of_graph_is_present(const struct device_node * node)68 bool of_graph_is_present(const struct device_node *node)
69 {
70 struct device_node *ports __free(device_node) = of_get_child_by_name(node, "ports");
71
72 if (ports)
73 node = ports;
74
75 struct device_node *port __free(device_node) = of_get_child_by_name(node, "port");
76
77 return !!port;
78 }
79 EXPORT_SYMBOL(of_graph_is_present);
80
81 /**
82 * of_property_count_elems_of_size - Count the number of elements in a property
83 *
84 * @np: device node from which the property value is to be read.
85 * @propname: name of the property to be searched.
86 * @elem_size: size of the individual element
87 *
88 * Search for a property in a device node and count the number of elements of
89 * size elem_size in it.
90 *
91 * Return: The number of elements on sucess, -EINVAL if the property does not
92 * exist or its length does not match a multiple of elem_size and -ENODATA if
93 * the property does not have a value.
94 */
of_property_count_elems_of_size(const struct device_node * np,const char * propname,int elem_size)95 int of_property_count_elems_of_size(const struct device_node *np,
96 const char *propname, int elem_size)
97 {
98 const struct property *prop = of_find_property(np, propname, NULL);
99
100 if (!prop)
101 return -EINVAL;
102 if (!prop->value)
103 return -ENODATA;
104
105 if (prop->length % elem_size != 0) {
106 pr_err("size of %s in node %pOF is not a multiple of %d\n",
107 propname, np, elem_size);
108 return -EINVAL;
109 }
110
111 return prop->length / elem_size;
112 }
113 EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
114
115 /**
116 * of_find_property_value_of_size
117 *
118 * @np: device node from which the property value is to be read.
119 * @propname: name of the property to be searched.
120 * @min: minimum allowed length of property value
121 * @max: maximum allowed length of property value (0 means unlimited)
122 * @len: if !=NULL, actual length is written to here
123 *
124 * Search for a property in a device node and valid the requested size.
125 *
126 * Return: The property value on success, -EINVAL if the property does not
127 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
128 * property data is too small or too large.
129 *
130 */
of_find_property_value_of_size(const struct device_node * np,const char * propname,u32 min,u32 max,size_t * len)131 static void *of_find_property_value_of_size(const struct device_node *np,
132 const char *propname, u32 min, u32 max, size_t *len)
133 {
134 const struct property *prop = of_find_property(np, propname, NULL);
135
136 if (!prop)
137 return ERR_PTR(-EINVAL);
138 if (!prop->value)
139 return ERR_PTR(-ENODATA);
140 if (prop->length < min)
141 return ERR_PTR(-EOVERFLOW);
142 if (max && prop->length > max)
143 return ERR_PTR(-EOVERFLOW);
144
145 if (len)
146 *len = prop->length;
147
148 return prop->value;
149 }
150
151 /**
152 * of_property_read_u8_index - Find and read a u8 from a multi-value property.
153 *
154 * @np: device node from which the property value is to be read.
155 * @propname: name of the property to be searched.
156 * @index: index of the u8 in the list of values
157 * @out_value: pointer to return value, modified only if no error.
158 *
159 * Search for a property in a device node and read nth 8-bit value from
160 * it.
161 *
162 * Return: 0 on success, -EINVAL if the property does not exist,
163 * -ENODATA if property does not have a value, and -EOVERFLOW if the
164 * property data isn't large enough.
165 *
166 * The out_value is modified only if a valid u8 value can be decoded.
167 */
of_property_read_u8_index(const struct device_node * np,const char * propname,u32 index,u8 * out_value)168 int of_property_read_u8_index(const struct device_node *np,
169 const char *propname,
170 u32 index, u8 *out_value)
171 {
172 const u8 *val = of_find_property_value_of_size(np, propname,
173 ((index + 1) * sizeof(*out_value)),
174 0, NULL);
175
176 if (IS_ERR(val))
177 return PTR_ERR(val);
178
179 *out_value = val[index];
180 return 0;
181 }
182 EXPORT_SYMBOL_GPL(of_property_read_u8_index);
183
184 /**
185 * of_property_read_u16_index - Find and read a u16 from a multi-value property.
186 *
187 * @np: device node from which the property value is to be read.
188 * @propname: name of the property to be searched.
189 * @index: index of the u16 in the list of values
190 * @out_value: pointer to return value, modified only if no error.
191 *
192 * Search for a property in a device node and read nth 16-bit value from
193 * it.
194 *
195 * Return: 0 on success, -EINVAL if the property does not exist,
196 * -ENODATA if property does not have a value, and -EOVERFLOW if the
197 * property data isn't large enough.
198 *
199 * The out_value is modified only if a valid u16 value can be decoded.
200 */
of_property_read_u16_index(const struct device_node * np,const char * propname,u32 index,u16 * out_value)201 int of_property_read_u16_index(const struct device_node *np,
202 const char *propname,
203 u32 index, u16 *out_value)
204 {
205 const u16 *val = of_find_property_value_of_size(np, propname,
206 ((index + 1) * sizeof(*out_value)),
207 0, NULL);
208
209 if (IS_ERR(val))
210 return PTR_ERR(val);
211
212 *out_value = be16_to_cpup(((__be16 *)val) + index);
213 return 0;
214 }
215 EXPORT_SYMBOL_GPL(of_property_read_u16_index);
216
217 /**
218 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
219 *
220 * @np: device node from which the property value is to be read.
221 * @propname: name of the property to be searched.
222 * @index: index of the u32 in the list of values
223 * @out_value: pointer to return value, modified only if no error.
224 *
225 * Search for a property in a device node and read nth 32-bit value from
226 * it.
227 *
228 * Return: 0 on success, -EINVAL if the property does not exist,
229 * -ENODATA if property does not have a value, and -EOVERFLOW if the
230 * property data isn't large enough.
231 *
232 * The out_value is modified only if a valid u32 value can be decoded.
233 */
of_property_read_u32_index(const struct device_node * np,const char * propname,u32 index,u32 * out_value)234 int of_property_read_u32_index(const struct device_node *np,
235 const char *propname,
236 u32 index, u32 *out_value)
237 {
238 const u32 *val = of_find_property_value_of_size(np, propname,
239 ((index + 1) * sizeof(*out_value)),
240 0,
241 NULL);
242
243 if (IS_ERR(val))
244 return PTR_ERR(val);
245
246 *out_value = be32_to_cpup(((__be32 *)val) + index);
247 return 0;
248 }
249 EXPORT_SYMBOL_GPL(of_property_read_u32_index);
250
251 /**
252 * of_property_read_u64_index - Find and read a u64 from a multi-value property.
253 *
254 * @np: device node from which the property value is to be read.
255 * @propname: name of the property to be searched.
256 * @index: index of the u64 in the list of values
257 * @out_value: pointer to return value, modified only if no error.
258 *
259 * Search for a property in a device node and read nth 64-bit value from
260 * it.
261 *
262 * Return: 0 on success, -EINVAL if the property does not exist,
263 * -ENODATA if property does not have a value, and -EOVERFLOW if the
264 * property data isn't large enough.
265 *
266 * The out_value is modified only if a valid u64 value can be decoded.
267 */
of_property_read_u64_index(const struct device_node * np,const char * propname,u32 index,u64 * out_value)268 int of_property_read_u64_index(const struct device_node *np,
269 const char *propname,
270 u32 index, u64 *out_value)
271 {
272 const u64 *val = of_find_property_value_of_size(np, propname,
273 ((index + 1) * sizeof(*out_value)),
274 0, NULL);
275
276 if (IS_ERR(val))
277 return PTR_ERR(val);
278
279 *out_value = be64_to_cpup(((__be64 *)val) + index);
280 return 0;
281 }
282 EXPORT_SYMBOL_GPL(of_property_read_u64_index);
283
284 /**
285 * of_property_read_variable_u8_array - Find and read an array of u8 from a
286 * property, with bounds on the minimum and maximum array size.
287 *
288 * @np: device node from which the property value is to be read.
289 * @propname: name of the property to be searched.
290 * @out_values: pointer to found values.
291 * @sz_min: minimum number of array elements to read
292 * @sz_max: maximum number of array elements to read, if zero there is no
293 * upper limit on the number of elements in the dts entry but only
294 * sz_min will be read.
295 *
296 * Search for a property in a device node and read 8-bit value(s) from
297 * it.
298 *
299 * dts entry of array should be like:
300 * ``property = /bits/ 8 <0x50 0x60 0x70>;``
301 *
302 * Return: The number of elements read on success, -EINVAL if the property
303 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
304 * if the property data is smaller than sz_min or longer than sz_max.
305 *
306 * The out_values is modified only if a valid u8 value can be decoded.
307 */
of_property_read_variable_u8_array(const struct device_node * np,const char * propname,u8 * out_values,size_t sz_min,size_t sz_max)308 int of_property_read_variable_u8_array(const struct device_node *np,
309 const char *propname, u8 *out_values,
310 size_t sz_min, size_t sz_max)
311 {
312 size_t sz, count;
313 const u8 *val = of_find_property_value_of_size(np, propname,
314 (sz_min * sizeof(*out_values)),
315 (sz_max * sizeof(*out_values)),
316 &sz);
317
318 if (IS_ERR(val))
319 return PTR_ERR(val);
320
321 if (!sz_max)
322 sz = sz_min;
323 else
324 sz /= sizeof(*out_values);
325
326 count = sz;
327 while (count--)
328 *out_values++ = *val++;
329
330 return sz;
331 }
332 EXPORT_SYMBOL_GPL(of_property_read_variable_u8_array);
333
334 /**
335 * of_property_read_variable_u16_array - Find and read an array of u16 from a
336 * property, with bounds on the minimum and maximum array size.
337 *
338 * @np: device node from which the property value is to be read.
339 * @propname: name of the property to be searched.
340 * @out_values: pointer to found values.
341 * @sz_min: minimum number of array elements to read
342 * @sz_max: maximum number of array elements to read, if zero there is no
343 * upper limit on the number of elements in the dts entry but only
344 * sz_min will be read.
345 *
346 * Search for a property in a device node and read 16-bit value(s) from
347 * it.
348 *
349 * dts entry of array should be like:
350 * ``property = /bits/ 16 <0x5000 0x6000 0x7000>;``
351 *
352 * Return: The number of elements read on success, -EINVAL if the property
353 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
354 * if the property data is smaller than sz_min or longer than sz_max.
355 *
356 * The out_values is modified only if a valid u16 value can be decoded.
357 */
of_property_read_variable_u16_array(const struct device_node * np,const char * propname,u16 * out_values,size_t sz_min,size_t sz_max)358 int of_property_read_variable_u16_array(const struct device_node *np,
359 const char *propname, u16 *out_values,
360 size_t sz_min, size_t sz_max)
361 {
362 size_t sz, count;
363 const __be16 *val = of_find_property_value_of_size(np, propname,
364 (sz_min * sizeof(*out_values)),
365 (sz_max * sizeof(*out_values)),
366 &sz);
367
368 if (IS_ERR(val))
369 return PTR_ERR(val);
370
371 if (!sz_max)
372 sz = sz_min;
373 else
374 sz /= sizeof(*out_values);
375
376 count = sz;
377 while (count--)
378 *out_values++ = be16_to_cpup(val++);
379
380 return sz;
381 }
382 EXPORT_SYMBOL_GPL(of_property_read_variable_u16_array);
383
384 /**
385 * of_property_read_variable_u32_array - Find and read an array of 32 bit
386 * integers from a property, with bounds on the minimum and maximum array size.
387 *
388 * @np: device node from which the property value is to be read.
389 * @propname: name of the property to be searched.
390 * @out_values: pointer to return found values.
391 * @sz_min: minimum number of array elements to read
392 * @sz_max: maximum number of array elements to read, if zero there is no
393 * upper limit on the number of elements in the dts entry but only
394 * sz_min will be read.
395 *
396 * Search for a property in a device node and read 32-bit value(s) from
397 * it.
398 *
399 * Return: The number of elements read on success, -EINVAL if the property
400 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
401 * if the property data is smaller than sz_min or longer than sz_max.
402 *
403 * The out_values is modified only if a valid u32 value can be decoded.
404 */
of_property_read_variable_u32_array(const struct device_node * np,const char * propname,u32 * out_values,size_t sz_min,size_t sz_max)405 int of_property_read_variable_u32_array(const struct device_node *np,
406 const char *propname, u32 *out_values,
407 size_t sz_min, size_t sz_max)
408 {
409 size_t sz, count;
410 const __be32 *val = of_find_property_value_of_size(np, propname,
411 (sz_min * sizeof(*out_values)),
412 (sz_max * sizeof(*out_values)),
413 &sz);
414
415 if (IS_ERR(val))
416 return PTR_ERR(val);
417
418 if (!sz_max)
419 sz = sz_min;
420 else
421 sz /= sizeof(*out_values);
422
423 count = sz;
424 while (count--)
425 *out_values++ = be32_to_cpup(val++);
426
427 return sz;
428 }
429 EXPORT_SYMBOL_GPL(of_property_read_variable_u32_array);
430
431 /**
432 * of_property_read_u64 - Find and read a 64 bit integer from a property
433 * @np: device node from which the property value is to be read.
434 * @propname: name of the property to be searched.
435 * @out_value: pointer to return value, modified only if return value is 0.
436 *
437 * Search for a property in a device node and read a 64-bit value from
438 * it.
439 *
440 * Return: 0 on success, -EINVAL if the property does not exist,
441 * -ENODATA if property does not have a value, and -EOVERFLOW if the
442 * property data isn't large enough.
443 *
444 * The out_value is modified only if a valid u64 value can be decoded.
445 */
of_property_read_u64(const struct device_node * np,const char * propname,u64 * out_value)446 int of_property_read_u64(const struct device_node *np, const char *propname,
447 u64 *out_value)
448 {
449 const __be32 *val = of_find_property_value_of_size(np, propname,
450 sizeof(*out_value),
451 0,
452 NULL);
453
454 if (IS_ERR(val))
455 return PTR_ERR(val);
456
457 *out_value = of_read_number(val, 2);
458 return 0;
459 }
460 EXPORT_SYMBOL_GPL(of_property_read_u64);
461
462 /**
463 * of_property_read_variable_u64_array - Find and read an array of 64 bit
464 * integers from a property, with bounds on the minimum and maximum array size.
465 *
466 * @np: device node from which the property value is to be read.
467 * @propname: name of the property to be searched.
468 * @out_values: pointer to found values.
469 * @sz_min: minimum number of array elements to read
470 * @sz_max: maximum number of array elements to read, if zero there is no
471 * upper limit on the number of elements in the dts entry but only
472 * sz_min will be read.
473 *
474 * Search for a property in a device node and read 64-bit value(s) from
475 * it.
476 *
477 * Return: The number of elements read on success, -EINVAL if the property
478 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
479 * if the property data is smaller than sz_min or longer than sz_max.
480 *
481 * The out_values is modified only if a valid u64 value can be decoded.
482 */
of_property_read_variable_u64_array(const struct device_node * np,const char * propname,u64 * out_values,size_t sz_min,size_t sz_max)483 int of_property_read_variable_u64_array(const struct device_node *np,
484 const char *propname, u64 *out_values,
485 size_t sz_min, size_t sz_max)
486 {
487 size_t sz, count;
488 const __be32 *val = of_find_property_value_of_size(np, propname,
489 (sz_min * sizeof(*out_values)),
490 (sz_max * sizeof(*out_values)),
491 &sz);
492
493 if (IS_ERR(val))
494 return PTR_ERR(val);
495
496 if (!sz_max)
497 sz = sz_min;
498 else
499 sz /= sizeof(*out_values);
500
501 count = sz;
502 while (count--) {
503 *out_values++ = of_read_number(val, 2);
504 val += 2;
505 }
506
507 return sz;
508 }
509 EXPORT_SYMBOL_GPL(of_property_read_variable_u64_array);
510
511 /**
512 * of_property_read_string - Find and read a string from a property
513 * @np: device node from which the property value is to be read.
514 * @propname: name of the property to be searched.
515 * @out_string: pointer to null terminated return string, modified only if
516 * return value is 0.
517 *
518 * Search for a property in a device tree node and retrieve a null
519 * terminated string value (pointer to data, not a copy).
520 *
521 * Return: 0 on success, -EINVAL if the property does not exist, -ENODATA if
522 * property does not have a value, and -EILSEQ if the string is not
523 * null-terminated within the length of the property data.
524 *
525 * Note that the empty string "" has length of 1, thus -ENODATA cannot
526 * be interpreted as an empty string.
527 *
528 * The out_string pointer is modified only if a valid string can be decoded.
529 */
of_property_read_string(const struct device_node * np,const char * propname,const char ** out_string)530 int of_property_read_string(const struct device_node *np, const char *propname,
531 const char **out_string)
532 {
533 const struct property *prop = of_find_property(np, propname, NULL);
534
535 if (!prop)
536 return -EINVAL;
537 if (!prop->length)
538 return -ENODATA;
539 if (strnlen(prop->value, prop->length) >= prop->length)
540 return -EILSEQ;
541 *out_string = prop->value;
542 return 0;
543 }
544 EXPORT_SYMBOL_GPL(of_property_read_string);
545
546 /**
547 * of_property_match_string() - Find string in a list and return index
548 * @np: pointer to the node containing the string list property
549 * @propname: string list property name
550 * @string: pointer to the string to search for in the string list
551 *
552 * Search for an exact match of string in a device node property which is a
553 * string of lists.
554 *
555 * Return: the index of the first occurrence of the string on success, -EINVAL
556 * if the property does not exist, -ENODATA if the property does not have a
557 * value, and -EILSEQ if the string is not null-terminated within the length of
558 * the property data.
559 */
of_property_match_string(const struct device_node * np,const char * propname,const char * string)560 int of_property_match_string(const struct device_node *np, const char *propname,
561 const char *string)
562 {
563 const struct property *prop = of_find_property(np, propname, NULL);
564 size_t l;
565 int i;
566 const char *p, *end;
567
568 if (!prop)
569 return -EINVAL;
570 if (!prop->value)
571 return -ENODATA;
572
573 p = prop->value;
574 end = p + prop->length;
575
576 for (i = 0; p < end; i++, p += l) {
577 l = strnlen(p, end - p) + 1;
578 if (p + l > end)
579 return -EILSEQ;
580 pr_debug("comparing %s with %s\n", string, p);
581 if (strcmp(string, p) == 0)
582 return i; /* Found it; return index */
583 }
584 return -ENODATA;
585 }
586 EXPORT_SYMBOL_GPL(of_property_match_string);
587
588 /**
589 * of_property_read_string_helper() - Utility helper for parsing string properties
590 * @np: device node from which the property value is to be read.
591 * @propname: name of the property to be searched.
592 * @out_strs: output array of string pointers.
593 * @sz: number of array elements to read.
594 * @skip: Number of strings to skip over at beginning of list.
595 *
596 * Don't call this function directly. It is a utility helper for the
597 * of_property_read_string*() family of functions.
598 */
of_property_read_string_helper(const struct device_node * np,const char * propname,const char ** out_strs,size_t sz,int skip)599 int of_property_read_string_helper(const struct device_node *np,
600 const char *propname, const char **out_strs,
601 size_t sz, int skip)
602 {
603 const struct property *prop = of_find_property(np, propname, NULL);
604 int l = 0, i = 0;
605 const char *p, *end;
606
607 if (!prop)
608 return -EINVAL;
609 if (!prop->value)
610 return -ENODATA;
611 p = prop->value;
612 end = p + prop->length;
613
614 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
615 l = strnlen(p, end - p) + 1;
616 if (p + l > end)
617 return -EILSEQ;
618 if (out_strs && i >= skip)
619 *out_strs++ = p;
620 }
621 i -= skip;
622 return i <= 0 ? -ENODATA : i;
623 }
624 EXPORT_SYMBOL_GPL(of_property_read_string_helper);
625
of_prop_next_u32(const struct property * prop,const __be32 * cur,u32 * pu)626 const __be32 *of_prop_next_u32(const struct property *prop, const __be32 *cur,
627 u32 *pu)
628 {
629 const void *curv = cur;
630
631 if (!prop)
632 return NULL;
633
634 if (!cur) {
635 curv = prop->value;
636 goto out_val;
637 }
638
639 curv += sizeof(*cur);
640 if (curv >= prop->value + prop->length)
641 return NULL;
642
643 out_val:
644 *pu = be32_to_cpup(curv);
645 return curv;
646 }
647 EXPORT_SYMBOL_GPL(of_prop_next_u32);
648
of_prop_next_string(const struct property * prop,const char * cur)649 const char *of_prop_next_string(const struct property *prop, const char *cur)
650 {
651 const void *curv = cur;
652
653 if (!prop)
654 return NULL;
655
656 if (!cur)
657 return prop->value;
658
659 curv += strlen(cur) + 1;
660 if (curv >= prop->value + prop->length)
661 return NULL;
662
663 return curv;
664 }
665 EXPORT_SYMBOL_GPL(of_prop_next_string);
666
667 /**
668 * of_graph_parse_endpoint() - parse common endpoint node properties
669 * @node: pointer to endpoint device_node
670 * @endpoint: pointer to the OF endpoint data structure
671 *
672 * The caller should hold a reference to @node.
673 */
of_graph_parse_endpoint(const struct device_node * node,struct of_endpoint * endpoint)674 int of_graph_parse_endpoint(const struct device_node *node,
675 struct of_endpoint *endpoint)
676 {
677 struct device_node *port_node __free(device_node) =
678 of_get_parent(node);
679
680 WARN_ONCE(!port_node, "%s(): endpoint %pOF has no parent node\n",
681 __func__, node);
682
683 memset(endpoint, 0, sizeof(*endpoint));
684
685 endpoint->local_node = node;
686 /*
687 * It doesn't matter whether the two calls below succeed.
688 * If they don't then the default value 0 is used.
689 */
690 of_property_read_u32(port_node, "reg", &endpoint->port);
691 of_property_read_u32(node, "reg", &endpoint->id);
692
693 return 0;
694 }
695 EXPORT_SYMBOL(of_graph_parse_endpoint);
696
697 /**
698 * of_graph_get_port_by_id() - get the port matching a given id
699 * @parent: pointer to the parent device node
700 * @id: id of the port
701 *
702 * Return: A 'port' node pointer with refcount incremented. The caller
703 * has to use of_node_put() on it when done.
704 */
of_graph_get_port_by_id(struct device_node * parent,u32 id)705 struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id)
706 {
707 struct device_node *node __free(device_node) = of_get_child_by_name(parent, "ports");
708
709 if (node)
710 parent = node;
711
712 for_each_child_of_node_scoped(parent, port) {
713 u32 port_id = 0;
714
715 if (!of_node_name_eq(port, "port"))
716 continue;
717 of_property_read_u32(port, "reg", &port_id);
718 if (id == port_id)
719 return_ptr(port);
720 }
721
722 return NULL;
723 }
724 EXPORT_SYMBOL(of_graph_get_port_by_id);
725
726 /**
727 * of_graph_get_next_port() - get next port node.
728 * @parent: pointer to the parent device node, or parent ports node
729 * @prev: previous port node, or NULL to get first
730 *
731 * Parent device node can be used as @parent whether device node has ports node
732 * or not. It will work same as ports@0 node.
733 *
734 * Return: A 'port' node pointer with refcount incremented. Refcount
735 * of the passed @prev node is decremented.
736 */
of_graph_get_next_port(const struct device_node * parent,struct device_node * prev)737 struct device_node *of_graph_get_next_port(const struct device_node *parent,
738 struct device_node *prev)
739 {
740 if (!parent)
741 return NULL;
742
743 if (!prev) {
744 struct device_node *node __free(device_node) =
745 of_get_child_by_name(parent, "ports");
746
747 if (node)
748 parent = node;
749
750 return of_get_child_by_name(parent, "port");
751 }
752
753 do {
754 prev = of_get_next_child(parent, prev);
755 if (!prev)
756 break;
757 } while (!of_node_name_eq(prev, "port"));
758
759 return prev;
760 }
761 EXPORT_SYMBOL(of_graph_get_next_port);
762
763 /**
764 * of_graph_get_next_port_endpoint() - get next endpoint node in port.
765 * If it reached to end of the port, it will return NULL.
766 * @port: pointer to the target port node
767 * @prev: previous endpoint node, or NULL to get first
768 *
769 * Return: An 'endpoint' node pointer with refcount incremented. Refcount
770 * of the passed @prev node is decremented.
771 */
of_graph_get_next_port_endpoint(const struct device_node * port,struct device_node * prev)772 struct device_node *of_graph_get_next_port_endpoint(const struct device_node *port,
773 struct device_node *prev)
774 {
775 while (1) {
776 prev = of_get_next_child(port, prev);
777 if (!prev)
778 break;
779 if (WARN(!of_node_name_eq(prev, "endpoint"),
780 "non endpoint node is used (%pOF)", prev))
781 continue;
782
783 break;
784 }
785
786 return prev;
787 }
788 EXPORT_SYMBOL(of_graph_get_next_port_endpoint);
789
790 /**
791 * of_graph_get_next_endpoint() - get next endpoint node
792 * @parent: pointer to the parent device node
793 * @prev: previous endpoint node, or NULL to get first
794 *
795 * Return: An 'endpoint' node pointer with refcount incremented. Refcount
796 * of the passed @prev node is decremented.
797 */
of_graph_get_next_endpoint(const struct device_node * parent,struct device_node * prev)798 struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
799 struct device_node *prev)
800 {
801 struct device_node *endpoint;
802 struct device_node *port;
803
804 if (!parent)
805 return NULL;
806
807 /*
808 * Start by locating the port node. If no previous endpoint is specified
809 * search for the first port node, otherwise get the previous endpoint
810 * parent port node.
811 */
812 if (!prev) {
813 port = of_graph_get_next_port(parent, NULL);
814 if (!port) {
815 pr_debug("graph: no port node found in %pOF\n", parent);
816 return NULL;
817 }
818 } else {
819 port = of_get_parent(prev);
820 if (WARN_ONCE(!port, "%s(): endpoint %pOF has no parent node\n",
821 __func__, prev))
822 return NULL;
823 }
824
825 while (1) {
826 /*
827 * Now that we have a port node, get the next endpoint by
828 * getting the next child. If the previous endpoint is NULL this
829 * will return the first child.
830 */
831 endpoint = of_graph_get_next_port_endpoint(port, prev);
832 if (endpoint) {
833 of_node_put(port);
834 return endpoint;
835 }
836
837 /* No more endpoints under this port, try the next one. */
838 prev = NULL;
839
840 port = of_graph_get_next_port(parent, port);
841 if (!port)
842 return NULL;
843 }
844 }
845 EXPORT_SYMBOL(of_graph_get_next_endpoint);
846
847 /**
848 * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers
849 * @parent: pointer to the parent device node
850 * @port_reg: identifier (value of reg property) of the parent port node
851 * @reg: identifier (value of reg property) of the endpoint node
852 *
853 * Return: An 'endpoint' node pointer which is identified by reg and at the same
854 * is the child of a port node identified by port_reg. reg and port_reg are
855 * ignored when they are -1. Use of_node_put() on the pointer when done.
856 */
of_graph_get_endpoint_by_regs(const struct device_node * parent,int port_reg,int reg)857 struct device_node *of_graph_get_endpoint_by_regs(
858 const struct device_node *parent, int port_reg, int reg)
859 {
860 struct of_endpoint endpoint;
861 struct device_node *node = NULL;
862
863 for_each_endpoint_of_node(parent, node) {
864 of_graph_parse_endpoint(node, &endpoint);
865 if (((port_reg == -1) || (endpoint.port == port_reg)) &&
866 ((reg == -1) || (endpoint.id == reg)))
867 return node;
868 }
869
870 return NULL;
871 }
872 EXPORT_SYMBOL(of_graph_get_endpoint_by_regs);
873
874 /**
875 * of_graph_get_remote_endpoint() - get remote endpoint node
876 * @node: pointer to a local endpoint device_node
877 *
878 * Return: Remote endpoint node associated with remote endpoint node linked
879 * to @node. Use of_node_put() on it when done.
880 */
of_graph_get_remote_endpoint(const struct device_node * node)881 struct device_node *of_graph_get_remote_endpoint(const struct device_node *node)
882 {
883 /* Get remote endpoint node. */
884 return of_parse_phandle(node, "remote-endpoint", 0);
885 }
886 EXPORT_SYMBOL(of_graph_get_remote_endpoint);
887
888 /**
889 * of_graph_get_port_parent() - get port's parent node
890 * @node: pointer to a local endpoint device_node
891 *
892 * Return: device node associated with endpoint node linked
893 * to @node. Use of_node_put() on it when done.
894 */
of_graph_get_port_parent(struct device_node * node)895 struct device_node *of_graph_get_port_parent(struct device_node *node)
896 {
897 unsigned int depth;
898
899 if (!node)
900 return NULL;
901
902 /*
903 * Preserve usecount for passed in node as of_get_next_parent()
904 * will do of_node_put() on it.
905 */
906 of_node_get(node);
907
908 /* Walk 3 levels up only if there is 'ports' node. */
909 for (depth = 3; depth && node; depth--) {
910 node = of_get_next_parent(node);
911 if (depth == 2 && !of_node_name_eq(node, "ports") &&
912 !of_node_name_eq(node, "in-ports") &&
913 !of_node_name_eq(node, "out-ports"))
914 break;
915 }
916 return node;
917 }
918 EXPORT_SYMBOL(of_graph_get_port_parent);
919
920 /**
921 * of_graph_get_remote_port_parent() - get remote port's parent node
922 * @node: pointer to a local endpoint device_node
923 *
924 * Return: Remote device node associated with remote endpoint node linked
925 * to @node. Use of_node_put() on it when done.
926 */
of_graph_get_remote_port_parent(const struct device_node * node)927 struct device_node *of_graph_get_remote_port_parent(
928 const struct device_node *node)
929 {
930 /* Get remote endpoint node. */
931 struct device_node *np __free(device_node) =
932 of_graph_get_remote_endpoint(node);
933
934 return of_graph_get_port_parent(np);
935 }
936 EXPORT_SYMBOL(of_graph_get_remote_port_parent);
937
938 /**
939 * of_graph_get_remote_port() - get remote port node
940 * @node: pointer to a local endpoint device_node
941 *
942 * Return: Remote port node associated with remote endpoint node linked
943 * to @node. Use of_node_put() on it when done.
944 */
of_graph_get_remote_port(const struct device_node * node)945 struct device_node *of_graph_get_remote_port(const struct device_node *node)
946 {
947 struct device_node *np;
948
949 /* Get remote endpoint node. */
950 np = of_graph_get_remote_endpoint(node);
951 if (!np)
952 return NULL;
953 return of_get_next_parent(np);
954 }
955 EXPORT_SYMBOL(of_graph_get_remote_port);
956
957 /**
958 * of_graph_get_endpoint_count() - get the number of endpoints in a device node
959 * @np: parent device node containing ports and endpoints
960 *
961 * Return: count of endpoint of this device node
962 */
of_graph_get_endpoint_count(const struct device_node * np)963 unsigned int of_graph_get_endpoint_count(const struct device_node *np)
964 {
965 struct device_node *endpoint;
966 unsigned int num = 0;
967
968 for_each_endpoint_of_node(np, endpoint)
969 num++;
970
971 return num;
972 }
973 EXPORT_SYMBOL(of_graph_get_endpoint_count);
974
975 /**
976 * of_graph_get_port_count() - get the number of port in a device or ports node
977 * @np: pointer to the device or ports node
978 *
979 * Return: count of port of this device or ports node
980 */
of_graph_get_port_count(struct device_node * np)981 unsigned int of_graph_get_port_count(struct device_node *np)
982 {
983 unsigned int num = 0;
984
985 for_each_of_graph_port(np, port)
986 num++;
987
988 return num;
989 }
990 EXPORT_SYMBOL(of_graph_get_port_count);
991
992 /**
993 * of_graph_get_remote_node() - get remote parent device_node for given port/endpoint
994 * @node: pointer to parent device_node containing graph port/endpoint
995 * @port: identifier (value of reg property) of the parent port node
996 * @endpoint: identifier (value of reg property) of the endpoint node
997 *
998 * Return: Remote device node associated with remote endpoint node linked
999 * to @node. Use of_node_put() on it when done.
1000 */
of_graph_get_remote_node(const struct device_node * node,u32 port,u32 endpoint)1001 struct device_node *of_graph_get_remote_node(const struct device_node *node,
1002 u32 port, u32 endpoint)
1003 {
1004 struct device_node *endpoint_node, *remote;
1005
1006 endpoint_node = of_graph_get_endpoint_by_regs(node, port, endpoint);
1007 if (!endpoint_node) {
1008 pr_debug("no valid endpoint (%d, %d) for node %pOF\n",
1009 port, endpoint, node);
1010 return NULL;
1011 }
1012
1013 remote = of_graph_get_remote_port_parent(endpoint_node);
1014 of_node_put(endpoint_node);
1015 if (!remote) {
1016 pr_debug("no valid remote node\n");
1017 return NULL;
1018 }
1019
1020 if (!of_device_is_available(remote)) {
1021 pr_debug("not available for remote node\n");
1022 of_node_put(remote);
1023 return NULL;
1024 }
1025
1026 return remote;
1027 }
1028 EXPORT_SYMBOL(of_graph_get_remote_node);
1029
of_fwnode_get(struct fwnode_handle * fwnode)1030 static struct fwnode_handle *of_fwnode_get(struct fwnode_handle *fwnode)
1031 {
1032 return of_fwnode_handle(of_node_get(to_of_node(fwnode)));
1033 }
1034
of_fwnode_put(struct fwnode_handle * fwnode)1035 static void of_fwnode_put(struct fwnode_handle *fwnode)
1036 {
1037 of_node_put(to_of_node(fwnode));
1038 }
1039
of_fwnode_device_is_available(const struct fwnode_handle * fwnode)1040 static bool of_fwnode_device_is_available(const struct fwnode_handle *fwnode)
1041 {
1042 return of_device_is_available(to_of_node(fwnode));
1043 }
1044
of_fwnode_device_dma_supported(const struct fwnode_handle * fwnode)1045 static bool of_fwnode_device_dma_supported(const struct fwnode_handle *fwnode)
1046 {
1047 return true;
1048 }
1049
1050 static enum dev_dma_attr
of_fwnode_device_get_dma_attr(const struct fwnode_handle * fwnode)1051 of_fwnode_device_get_dma_attr(const struct fwnode_handle *fwnode)
1052 {
1053 if (of_dma_is_coherent(to_of_node(fwnode)))
1054 return DEV_DMA_COHERENT;
1055 else
1056 return DEV_DMA_NON_COHERENT;
1057 }
1058
of_fwnode_property_present(const struct fwnode_handle * fwnode,const char * propname)1059 static bool of_fwnode_property_present(const struct fwnode_handle *fwnode,
1060 const char *propname)
1061 {
1062 return of_property_present(to_of_node(fwnode), propname);
1063 }
1064
of_fwnode_property_read_bool(const struct fwnode_handle * fwnode,const char * propname)1065 static bool of_fwnode_property_read_bool(const struct fwnode_handle *fwnode,
1066 const char *propname)
1067 {
1068 return of_property_read_bool(to_of_node(fwnode), propname);
1069 }
1070
of_fwnode_property_read_int_array(const struct fwnode_handle * fwnode,const char * propname,unsigned int elem_size,void * val,size_t nval)1071 static int of_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
1072 const char *propname,
1073 unsigned int elem_size, void *val,
1074 size_t nval)
1075 {
1076 const struct device_node *node = to_of_node(fwnode);
1077
1078 if (!val)
1079 return of_property_count_elems_of_size(node, propname,
1080 elem_size);
1081
1082 switch (elem_size) {
1083 case sizeof(u8):
1084 return of_property_read_u8_array(node, propname, val, nval);
1085 case sizeof(u16):
1086 return of_property_read_u16_array(node, propname, val, nval);
1087 case sizeof(u32):
1088 return of_property_read_u32_array(node, propname, val, nval);
1089 case sizeof(u64):
1090 return of_property_read_u64_array(node, propname, val, nval);
1091 }
1092
1093 return -ENXIO;
1094 }
1095
1096 static int
of_fwnode_property_read_string_array(const struct fwnode_handle * fwnode,const char * propname,const char ** val,size_t nval)1097 of_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
1098 const char *propname, const char **val,
1099 size_t nval)
1100 {
1101 const struct device_node *node = to_of_node(fwnode);
1102
1103 return val ?
1104 of_property_read_string_array(node, propname, val, nval) :
1105 of_property_count_strings(node, propname);
1106 }
1107
of_fwnode_get_name(const struct fwnode_handle * fwnode)1108 static const char *of_fwnode_get_name(const struct fwnode_handle *fwnode)
1109 {
1110 return kbasename(to_of_node(fwnode)->full_name);
1111 }
1112
of_fwnode_get_name_prefix(const struct fwnode_handle * fwnode)1113 static const char *of_fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
1114 {
1115 /* Root needs no prefix here (its name is "/"). */
1116 if (!to_of_node(fwnode)->parent)
1117 return "";
1118
1119 return "/";
1120 }
1121
1122 static struct fwnode_handle *
of_fwnode_get_parent(const struct fwnode_handle * fwnode)1123 of_fwnode_get_parent(const struct fwnode_handle *fwnode)
1124 {
1125 return of_fwnode_handle(of_get_parent(to_of_node(fwnode)));
1126 }
1127
1128 static struct fwnode_handle *
of_fwnode_get_next_child_node(const struct fwnode_handle * fwnode,struct fwnode_handle * child)1129 of_fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
1130 struct fwnode_handle *child)
1131 {
1132 return of_fwnode_handle(of_get_next_available_child(to_of_node(fwnode),
1133 to_of_node(child)));
1134 }
1135
1136 static struct fwnode_handle *
of_fwnode_get_named_child_node(const struct fwnode_handle * fwnode,const char * childname)1137 of_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
1138 const char *childname)
1139 {
1140 const struct device_node *node = to_of_node(fwnode);
1141 struct device_node *child;
1142
1143 for_each_available_child_of_node(node, child)
1144 if (of_node_name_eq(child, childname))
1145 return of_fwnode_handle(child);
1146
1147 return NULL;
1148 }
1149
1150 static int
of_fwnode_get_reference_args(const struct fwnode_handle * fwnode,const char * prop,const char * nargs_prop,unsigned int nargs,unsigned int index,struct fwnode_reference_args * args)1151 of_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
1152 const char *prop, const char *nargs_prop,
1153 unsigned int nargs, unsigned int index,
1154 struct fwnode_reference_args *args)
1155 {
1156 struct of_phandle_args of_args;
1157 unsigned int i;
1158 int ret;
1159
1160 if (nargs_prop)
1161 ret = of_parse_phandle_with_args(to_of_node(fwnode), prop,
1162 nargs_prop, index, &of_args);
1163 else
1164 ret = of_parse_phandle_with_fixed_args(to_of_node(fwnode), prop,
1165 nargs, index, &of_args);
1166 if (ret < 0)
1167 return ret;
1168 if (!args) {
1169 of_node_put(of_args.np);
1170 return 0;
1171 }
1172
1173 args->nargs = of_args.args_count;
1174 args->fwnode = of_fwnode_handle(of_args.np);
1175
1176 for (i = 0; i < NR_FWNODE_REFERENCE_ARGS; i++)
1177 args->args[i] = i < of_args.args_count ? of_args.args[i] : 0;
1178
1179 return 0;
1180 }
1181
1182 static struct fwnode_handle *
of_fwnode_graph_get_next_endpoint(const struct fwnode_handle * fwnode,struct fwnode_handle * prev)1183 of_fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
1184 struct fwnode_handle *prev)
1185 {
1186 return of_fwnode_handle(of_graph_get_next_endpoint(to_of_node(fwnode),
1187 to_of_node(prev)));
1188 }
1189
1190 static struct fwnode_handle *
of_fwnode_graph_get_remote_endpoint(const struct fwnode_handle * fwnode)1191 of_fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
1192 {
1193 return of_fwnode_handle(
1194 of_graph_get_remote_endpoint(to_of_node(fwnode)));
1195 }
1196
1197 static struct fwnode_handle *
of_fwnode_graph_get_port_parent(struct fwnode_handle * fwnode)1198 of_fwnode_graph_get_port_parent(struct fwnode_handle *fwnode)
1199 {
1200 struct device_node *np;
1201
1202 /* Get the parent of the port */
1203 np = of_get_parent(to_of_node(fwnode));
1204 if (!np)
1205 return NULL;
1206
1207 /* Is this the "ports" node? If not, it's the port parent. */
1208 if (!of_node_name_eq(np, "ports"))
1209 return of_fwnode_handle(np);
1210
1211 return of_fwnode_handle(of_get_next_parent(np));
1212 }
1213
of_fwnode_graph_parse_endpoint(const struct fwnode_handle * fwnode,struct fwnode_endpoint * endpoint)1214 static int of_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
1215 struct fwnode_endpoint *endpoint)
1216 {
1217 const struct device_node *node = to_of_node(fwnode);
1218 struct device_node *port_node __free(device_node) = of_get_parent(node);
1219
1220 endpoint->local_fwnode = fwnode;
1221
1222 of_property_read_u32(port_node, "reg", &endpoint->port);
1223 of_property_read_u32(node, "reg", &endpoint->id);
1224
1225 return 0;
1226 }
1227
1228 static const void *
of_fwnode_device_get_match_data(const struct fwnode_handle * fwnode,const struct device * dev)1229 of_fwnode_device_get_match_data(const struct fwnode_handle *fwnode,
1230 const struct device *dev)
1231 {
1232 return of_device_get_match_data(dev);
1233 }
1234
of_link_to_phandle(struct device_node * con_np,struct device_node * sup_np,u8 flags)1235 static void of_link_to_phandle(struct device_node *con_np,
1236 struct device_node *sup_np,
1237 u8 flags)
1238 {
1239 struct device_node *tmp_np __free(device_node) = of_node_get(sup_np);
1240
1241 /* Check that sup_np and its ancestors are available. */
1242 while (tmp_np) {
1243 if (of_fwnode_handle(tmp_np)->dev)
1244 break;
1245
1246 if (!of_device_is_available(tmp_np))
1247 return;
1248
1249 tmp_np = of_get_next_parent(tmp_np);
1250 }
1251
1252 fwnode_link_add(of_fwnode_handle(con_np), of_fwnode_handle(sup_np), flags);
1253 }
1254
1255 /**
1256 * parse_prop_cells - Property parsing function for suppliers
1257 *
1258 * @np: Pointer to device tree node containing a list
1259 * @prop_name: Name of property to be parsed. Expected to hold phandle values
1260 * @index: For properties holding a list of phandles, this is the index
1261 * into the list.
1262 * @list_name: Property name that is known to contain list of phandle(s) to
1263 * supplier(s)
1264 * @cells_name: property name that specifies phandles' arguments count
1265 *
1266 * This is a helper function to parse properties that have a known fixed name
1267 * and are a list of phandles and phandle arguments.
1268 *
1269 * Returns:
1270 * - phandle node pointer with refcount incremented. Caller must of_node_put()
1271 * on it when done.
1272 * - NULL if no phandle found at index
1273 */
parse_prop_cells(struct device_node * np,const char * prop_name,int index,const char * list_name,const char * cells_name)1274 static struct device_node *parse_prop_cells(struct device_node *np,
1275 const char *prop_name, int index,
1276 const char *list_name,
1277 const char *cells_name)
1278 {
1279 struct of_phandle_args sup_args;
1280
1281 if (strcmp(prop_name, list_name))
1282 return NULL;
1283
1284 if (__of_parse_phandle_with_args(np, list_name, cells_name, 0, index,
1285 &sup_args))
1286 return NULL;
1287
1288 return sup_args.np;
1289 }
1290
1291 #define DEFINE_SIMPLE_PROP(fname, name, cells) \
1292 static struct device_node *parse_##fname(struct device_node *np, \
1293 const char *prop_name, int index) \
1294 { \
1295 return parse_prop_cells(np, prop_name, index, name, cells); \
1296 }
1297
1298 /**
1299 * parse_suffix_prop_cells - Suffix property parsing function for suppliers
1300 *
1301 * @np: Pointer to device tree node containing a list
1302 * @prop_name: Name of property to be parsed. Expected to hold phandle values
1303 * @index: For properties holding a list of phandles, this is the index
1304 * into the list.
1305 * @suffix: Property suffix that is known to contain list of phandle(s) to
1306 * supplier(s)
1307 * @cells_name: property name that specifies phandles' arguments count
1308 *
1309 * This is a helper function to parse properties that have a known fixed suffix
1310 * and are a list of phandles and phandle arguments.
1311 *
1312 * Returns:
1313 * - phandle node pointer with refcount incremented. Caller must of_node_put()
1314 * on it when done.
1315 * - NULL if no phandle found at index
1316 */
parse_suffix_prop_cells(struct device_node * np,const char * prop_name,int index,const char * suffix,const char * cells_name)1317 static struct device_node *parse_suffix_prop_cells(struct device_node *np,
1318 const char *prop_name, int index,
1319 const char *suffix,
1320 const char *cells_name)
1321 {
1322 struct of_phandle_args sup_args;
1323
1324 if (!strends(prop_name, suffix))
1325 return NULL;
1326
1327 if (of_parse_phandle_with_args(np, prop_name, cells_name, index,
1328 &sup_args))
1329 return NULL;
1330
1331 return sup_args.np;
1332 }
1333
1334 #define DEFINE_SUFFIX_PROP(fname, suffix, cells) \
1335 static struct device_node *parse_##fname(struct device_node *np, \
1336 const char *prop_name, int index) \
1337 { \
1338 return parse_suffix_prop_cells(np, prop_name, index, suffix, cells); \
1339 }
1340
1341 /**
1342 * struct supplier_bindings - Property parsing functions for suppliers
1343 *
1344 * @parse_prop: function name
1345 * parse_prop() finds the node corresponding to a supplier phandle
1346 * parse_prop.np: Pointer to device node holding supplier phandle property
1347 * parse_prop.prop_name: Name of property holding a phandle value
1348 * parse_prop.index: For properties holding a list of phandles, this is the
1349 * index into the list
1350 * @get_con_dev: If the consumer node containing the property is never converted
1351 * to a struct device, implement this ops so fw_devlink can use it
1352 * to find the true consumer.
1353 * @optional: Describes whether a supplier is mandatory or not
1354 * @fwlink_flags: Optional fwnode link flags to use when creating a fwnode link
1355 * for this property.
1356 *
1357 * Returns:
1358 * parse_prop() return values are
1359 * - phandle node pointer with refcount incremented. Caller must of_node_put()
1360 * on it when done.
1361 * - NULL if no phandle found at index
1362 */
1363 struct supplier_bindings {
1364 struct device_node *(*parse_prop)(struct device_node *np,
1365 const char *prop_name, int index);
1366 struct device_node *(*get_con_dev)(struct device_node *np);
1367 bool optional;
1368 u8 fwlink_flags;
1369 };
1370
1371 DEFINE_SIMPLE_PROP(clocks, "clocks", "#clock-cells")
1372 DEFINE_SIMPLE_PROP(interconnects, "interconnects", "#interconnect-cells")
1373 DEFINE_SIMPLE_PROP(iommus, "iommus", "#iommu-cells")
1374 DEFINE_SIMPLE_PROP(mboxes, "mboxes", "#mbox-cells")
1375 DEFINE_SIMPLE_PROP(io_channels, "io-channels", "#io-channel-cells")
1376 DEFINE_SIMPLE_PROP(io_backends, "io-backends", "#io-backend-cells")
1377 DEFINE_SIMPLE_PROP(dmas, "dmas", "#dma-cells")
1378 DEFINE_SIMPLE_PROP(power_domains, "power-domains", "#power-domain-cells")
1379 DEFINE_SIMPLE_PROP(hwlocks, "hwlocks", "#hwlock-cells")
1380 DEFINE_SIMPLE_PROP(extcon, "extcon", NULL)
1381 DEFINE_SIMPLE_PROP(nvmem_cells, "nvmem-cells", "#nvmem-cell-cells")
1382 DEFINE_SIMPLE_PROP(phys, "phys", "#phy-cells")
1383 DEFINE_SIMPLE_PROP(wakeup_parent, "wakeup-parent", NULL)
1384 DEFINE_SIMPLE_PROP(pwms, "pwms", "#pwm-cells")
1385 DEFINE_SIMPLE_PROP(resets, "resets", "#reset-cells")
1386 DEFINE_SIMPLE_PROP(leds, "leds", NULL)
1387 DEFINE_SIMPLE_PROP(backlight, "backlight", NULL)
1388 DEFINE_SIMPLE_PROP(panel, "panel", NULL)
1389 DEFINE_SIMPLE_PROP(msi_parent, "msi-parent", "#msi-cells")
1390 DEFINE_SIMPLE_PROP(post_init_providers, "post-init-providers", NULL)
1391 DEFINE_SIMPLE_PROP(access_controllers, "access-controllers", "#access-controller-cells")
1392 DEFINE_SIMPLE_PROP(pses, "pses", "#pse-cells")
1393 DEFINE_SIMPLE_PROP(power_supplies, "power-supplies", NULL)
1394 DEFINE_SIMPLE_PROP(mmc_pwrseq, "mmc-pwrseq", NULL)
1395 DEFINE_SUFFIX_PROP(regulators, "-supply", NULL)
1396 DEFINE_SUFFIX_PROP(gpio, "-gpio", "#gpio-cells")
1397
parse_pinctrl_n(struct device_node * np,const char * prop_name,int index)1398 static struct device_node *parse_pinctrl_n(struct device_node *np,
1399 const char *prop_name, int index)
1400 {
1401 if (!strstarts(prop_name, "pinctrl-"))
1402 return NULL;
1403
1404 if (!isdigit(prop_name[strlen("pinctrl-")]))
1405 return NULL;
1406
1407 return of_parse_phandle(np, prop_name, index);
1408 }
1409
parse_gpios(struct device_node * np,const char * prop_name,int index)1410 static struct device_node *parse_gpios(struct device_node *np,
1411 const char *prop_name, int index)
1412 {
1413 if (strends(prop_name, ",nr-gpios"))
1414 return NULL;
1415
1416 return parse_suffix_prop_cells(np, prop_name, index, "-gpios",
1417 "#gpio-cells");
1418 }
1419
parse_iommu_maps(struct device_node * np,const char * prop_name,int index)1420 static struct device_node *parse_iommu_maps(struct device_node *np,
1421 const char *prop_name, int index)
1422 {
1423 if (strcmp(prop_name, "iommu-map"))
1424 return NULL;
1425
1426 return of_parse_phandle(np, prop_name, (index * 4) + 1);
1427 }
1428
parse_gpio_compat(struct device_node * np,const char * prop_name,int index)1429 static struct device_node *parse_gpio_compat(struct device_node *np,
1430 const char *prop_name, int index)
1431 {
1432 struct of_phandle_args sup_args;
1433
1434 if (strcmp(prop_name, "gpio") && strcmp(prop_name, "gpios"))
1435 return NULL;
1436
1437 /*
1438 * Ignore node with gpio-hog property since its gpios are all provided
1439 * by its parent.
1440 */
1441 if (of_property_read_bool(np, "gpio-hog"))
1442 return NULL;
1443
1444 if (of_parse_phandle_with_args(np, prop_name, "#gpio-cells", index,
1445 &sup_args))
1446 return NULL;
1447
1448 return sup_args.np;
1449 }
1450
parse_interrupts(struct device_node * np,const char * prop_name,int index)1451 static struct device_node *parse_interrupts(struct device_node *np,
1452 const char *prop_name, int index)
1453 {
1454 struct of_phandle_args sup_args;
1455
1456 if (!IS_ENABLED(CONFIG_OF_IRQ) || IS_ENABLED(CONFIG_PPC))
1457 return NULL;
1458
1459 if (strcmp(prop_name, "interrupts") &&
1460 strcmp(prop_name, "interrupts-extended"))
1461 return NULL;
1462
1463 return of_irq_parse_one(np, index, &sup_args) ? NULL : sup_args.np;
1464 }
1465
parse_interrupt_map(struct device_node * np,const char * prop_name,int index)1466 static struct device_node *parse_interrupt_map(struct device_node *np,
1467 const char *prop_name, int index)
1468 {
1469 const __be32 *imap, *imap_end;
1470 struct of_phandle_args sup_args;
1471 u32 addrcells, intcells;
1472 int imaplen;
1473
1474 if (!IS_ENABLED(CONFIG_OF_IRQ))
1475 return NULL;
1476
1477 if (strcmp(prop_name, "interrupt-map"))
1478 return NULL;
1479
1480 if (of_property_read_u32(np, "#interrupt-cells", &intcells))
1481 return NULL;
1482 addrcells = of_bus_n_addr_cells(np);
1483
1484 imap = of_get_property(np, "interrupt-map", &imaplen);
1485 if (!imap)
1486 return NULL;
1487 imaplen /= sizeof(*imap);
1488
1489 imap_end = imap + imaplen;
1490
1491 for (int i = 0; imap + addrcells + intcells + 1 < imap_end; i++) {
1492 imap += addrcells + intcells;
1493
1494 imap = of_irq_parse_imap_parent(imap, imap_end - imap, &sup_args);
1495 if (!imap)
1496 return NULL;
1497
1498 if (i == index)
1499 return sup_args.np;
1500
1501 of_node_put(sup_args.np);
1502 }
1503
1504 return NULL;
1505 }
1506
parse_remote_endpoint(struct device_node * np,const char * prop_name,int index)1507 static struct device_node *parse_remote_endpoint(struct device_node *np,
1508 const char *prop_name,
1509 int index)
1510 {
1511 /* Return NULL for index > 0 to signify end of remote-endpoints. */
1512 if (index > 0 || strcmp(prop_name, "remote-endpoint"))
1513 return NULL;
1514
1515 return of_graph_get_remote_port_parent(np);
1516 }
1517
1518 static const struct supplier_bindings of_supplier_bindings[] = {
1519 { .parse_prop = parse_clocks, },
1520 { .parse_prop = parse_interconnects, },
1521 { .parse_prop = parse_iommus, .optional = true, },
1522 { .parse_prop = parse_iommu_maps, .optional = true, },
1523 { .parse_prop = parse_mboxes, },
1524 { .parse_prop = parse_io_channels, },
1525 { .parse_prop = parse_io_backends, },
1526 { .parse_prop = parse_dmas, .optional = true, },
1527 { .parse_prop = parse_power_domains, },
1528 { .parse_prop = parse_hwlocks, },
1529 { .parse_prop = parse_extcon, },
1530 { .parse_prop = parse_nvmem_cells, },
1531 { .parse_prop = parse_phys, },
1532 { .parse_prop = parse_wakeup_parent, },
1533 { .parse_prop = parse_pinctrl_n, },
1534 {
1535 .parse_prop = parse_remote_endpoint,
1536 .get_con_dev = of_graph_get_port_parent,
1537 },
1538 { .parse_prop = parse_pwms, },
1539 { .parse_prop = parse_resets, },
1540 { .parse_prop = parse_leds, },
1541 { .parse_prop = parse_backlight, },
1542 { .parse_prop = parse_panel, },
1543 { .parse_prop = parse_msi_parent, },
1544 { .parse_prop = parse_pses, },
1545 { .parse_prop = parse_power_supplies, },
1546 { .parse_prop = parse_mmc_pwrseq, },
1547 { .parse_prop = parse_gpio_compat, },
1548 { .parse_prop = parse_interrupts, },
1549 { .parse_prop = parse_interrupt_map, },
1550 { .parse_prop = parse_access_controllers, },
1551 { .parse_prop = parse_regulators, },
1552 { .parse_prop = parse_gpio, },
1553 { .parse_prop = parse_gpios, },
1554 {
1555 .parse_prop = parse_post_init_providers,
1556 .fwlink_flags = FWLINK_FLAG_IGNORE,
1557 },
1558 {}
1559 };
1560
1561 /**
1562 * of_link_property - Create device links to suppliers listed in a property
1563 * @con_np: The consumer device tree node which contains the property
1564 * @prop_name: Name of property to be parsed
1565 *
1566 * This function checks if the property @prop_name that is present in the
1567 * @con_np device tree node is one of the known common device tree bindings
1568 * that list phandles to suppliers. If @prop_name isn't one, this function
1569 * doesn't do anything.
1570 *
1571 * If @prop_name is one, this function attempts to create fwnode links from the
1572 * consumer device tree node @con_np to all the suppliers device tree nodes
1573 * listed in @prop_name.
1574 *
1575 * Any failed attempt to create a fwnode link will NOT result in an immediate
1576 * return. of_link_property() must create links to all the available supplier
1577 * device tree nodes even when attempts to create a link to one or more
1578 * suppliers fail.
1579 */
of_link_property(struct device_node * con_np,const char * prop_name)1580 static int of_link_property(struct device_node *con_np, const char *prop_name)
1581 {
1582 struct device_node *phandle;
1583 const struct supplier_bindings *s = of_supplier_bindings;
1584 unsigned int i = 0;
1585 bool matched = false;
1586
1587 /* Do not stop at first failed link, link all available suppliers. */
1588 while (!matched && s->parse_prop) {
1589 if (s->optional && !fw_devlink_is_strict()) {
1590 s++;
1591 continue;
1592 }
1593
1594 while ((phandle = s->parse_prop(con_np, prop_name, i))) {
1595 struct device_node *con_dev_np __free(device_node) =
1596 s->get_con_dev ? s->get_con_dev(con_np) : of_node_get(con_np);
1597
1598 matched = true;
1599 i++;
1600 of_link_to_phandle(con_dev_np, phandle, s->fwlink_flags);
1601 of_node_put(phandle);
1602 }
1603 s++;
1604 }
1605 return 0;
1606 }
1607
of_fwnode_iomap(struct fwnode_handle * fwnode,int index)1608 static void __iomem *of_fwnode_iomap(struct fwnode_handle *fwnode, int index)
1609 {
1610 #ifdef CONFIG_OF_ADDRESS
1611 return of_iomap(to_of_node(fwnode), index);
1612 #else
1613 return NULL;
1614 #endif
1615 }
1616
of_fwnode_irq_get(const struct fwnode_handle * fwnode,unsigned int index)1617 static int of_fwnode_irq_get(const struct fwnode_handle *fwnode,
1618 unsigned int index)
1619 {
1620 return of_irq_get(to_of_node(fwnode), index);
1621 }
1622
of_fwnode_add_links(struct fwnode_handle * fwnode)1623 static int of_fwnode_add_links(struct fwnode_handle *fwnode)
1624 {
1625 const struct property *p;
1626 struct device_node *con_np = to_of_node(fwnode);
1627
1628 if (IS_ENABLED(CONFIG_X86))
1629 return 0;
1630
1631 if (!con_np)
1632 return -EINVAL;
1633
1634 for_each_property_of_node(con_np, p)
1635 of_link_property(con_np, p->name);
1636
1637 return 0;
1638 }
1639
1640 const struct fwnode_operations of_fwnode_ops = {
1641 .get = of_fwnode_get,
1642 .put = of_fwnode_put,
1643 .device_is_available = of_fwnode_device_is_available,
1644 .device_get_match_data = of_fwnode_device_get_match_data,
1645 .device_dma_supported = of_fwnode_device_dma_supported,
1646 .device_get_dma_attr = of_fwnode_device_get_dma_attr,
1647 .property_present = of_fwnode_property_present,
1648 .property_read_bool = of_fwnode_property_read_bool,
1649 .property_read_int_array = of_fwnode_property_read_int_array,
1650 .property_read_string_array = of_fwnode_property_read_string_array,
1651 .get_name = of_fwnode_get_name,
1652 .get_name_prefix = of_fwnode_get_name_prefix,
1653 .get_parent = of_fwnode_get_parent,
1654 .get_next_child_node = of_fwnode_get_next_child_node,
1655 .get_named_child_node = of_fwnode_get_named_child_node,
1656 .get_reference_args = of_fwnode_get_reference_args,
1657 .graph_get_next_endpoint = of_fwnode_graph_get_next_endpoint,
1658 .graph_get_remote_endpoint = of_fwnode_graph_get_remote_endpoint,
1659 .graph_get_port_parent = of_fwnode_graph_get_port_parent,
1660 .graph_parse_endpoint = of_fwnode_graph_parse_endpoint,
1661 .iomap = of_fwnode_iomap,
1662 .irq_get = of_fwnode_irq_get,
1663 .add_links = of_fwnode_add_links,
1664 };
1665 EXPORT_SYMBOL_GPL(of_fwnode_ops);
1666