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/of.h>
25 #include <linux/of_device.h>
26 #include <linux/of_graph.h>
27 #include <linux/of_irq.h>
28 #include <linux/string.h>
29 #include <linux/moduleparam.h>
30
31 #include "of_private.h"
32
33 /**
34 * of_graph_is_present() - check graph's presence
35 * @node: pointer to device_node containing graph port
36 *
37 * Return: True if @node has a port or ports (with a port) sub-node,
38 * false otherwise.
39 */
of_graph_is_present(const struct device_node * node)40 bool of_graph_is_present(const struct device_node *node)
41 {
42 struct device_node *ports, *port;
43
44 ports = of_get_child_by_name(node, "ports");
45 if (ports)
46 node = ports;
47
48 port = of_get_child_by_name(node, "port");
49 of_node_put(ports);
50 of_node_put(port);
51
52 return !!port;
53 }
54 EXPORT_SYMBOL(of_graph_is_present);
55
56 /**
57 * of_property_count_elems_of_size - Count the number of elements in a property
58 *
59 * @np: device node from which the property value is to be read.
60 * @propname: name of the property to be searched.
61 * @elem_size: size of the individual element
62 *
63 * Search for a property in a device node and count the number of elements of
64 * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
65 * property does not exist or its length does not match a multiple of elem_size
66 * and -ENODATA if the property does not have a value.
67 */
of_property_count_elems_of_size(const struct device_node * np,const char * propname,int elem_size)68 int of_property_count_elems_of_size(const struct device_node *np,
69 const char *propname, int elem_size)
70 {
71 struct property *prop = of_find_property(np, propname, NULL);
72
73 if (!prop)
74 return -EINVAL;
75 if (!prop->value)
76 return -ENODATA;
77
78 if (prop->length % elem_size != 0) {
79 pr_err("size of %s in node %pOF is not a multiple of %d\n",
80 propname, np, elem_size);
81 return -EINVAL;
82 }
83
84 return prop->length / elem_size;
85 }
86 EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
87
88 /**
89 * of_find_property_value_of_size
90 *
91 * @np: device node from which the property value is to be read.
92 * @propname: name of the property to be searched.
93 * @min: minimum allowed length of property value
94 * @max: maximum allowed length of property value (0 means unlimited)
95 * @len: if !=NULL, actual length is written to here
96 *
97 * Search for a property in a device node and valid the requested size.
98 * Returns the property value on success, -EINVAL if the property does not
99 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
100 * property data is too small or too large.
101 *
102 */
of_find_property_value_of_size(const struct device_node * np,const char * propname,u32 min,u32 max,size_t * len)103 static void *of_find_property_value_of_size(const struct device_node *np,
104 const char *propname, u32 min, u32 max, size_t *len)
105 {
106 struct property *prop = of_find_property(np, propname, NULL);
107
108 if (!prop)
109 return ERR_PTR(-EINVAL);
110 if (!prop->value)
111 return ERR_PTR(-ENODATA);
112 if (prop->length < min)
113 return ERR_PTR(-EOVERFLOW);
114 if (max && prop->length > max)
115 return ERR_PTR(-EOVERFLOW);
116
117 if (len)
118 *len = prop->length;
119
120 return prop->value;
121 }
122
123 /**
124 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
125 *
126 * @np: device node from which the property value is to be read.
127 * @propname: name of the property to be searched.
128 * @index: index of the u32 in the list of values
129 * @out_value: pointer to return value, modified only if no error.
130 *
131 * Search for a property in a device node and read nth 32-bit value from
132 * it. Returns 0 on success, -EINVAL if the property does not exist,
133 * -ENODATA if property does not have a value, and -EOVERFLOW if the
134 * property data isn't large enough.
135 *
136 * The out_value is modified only if a valid u32 value can be decoded.
137 */
of_property_read_u32_index(const struct device_node * np,const char * propname,u32 index,u32 * out_value)138 int of_property_read_u32_index(const struct device_node *np,
139 const char *propname,
140 u32 index, u32 *out_value)
141 {
142 const u32 *val = of_find_property_value_of_size(np, propname,
143 ((index + 1) * sizeof(*out_value)),
144 0,
145 NULL);
146
147 if (IS_ERR(val))
148 return PTR_ERR(val);
149
150 *out_value = be32_to_cpup(((__be32 *)val) + index);
151 return 0;
152 }
153 EXPORT_SYMBOL_GPL(of_property_read_u32_index);
154
155 /**
156 * of_property_read_u64_index - Find and read a u64 from a multi-value property.
157 *
158 * @np: device node from which the property value is to be read.
159 * @propname: name of the property to be searched.
160 * @index: index of the u64 in the list of values
161 * @out_value: pointer to return value, modified only if no error.
162 *
163 * Search for a property in a device node and read nth 64-bit value from
164 * it. Returns 0 on success, -EINVAL if the property does not exist,
165 * -ENODATA if property does not have a value, and -EOVERFLOW if the
166 * property data isn't large enough.
167 *
168 * The out_value is modified only if a valid u64 value can be decoded.
169 */
of_property_read_u64_index(const struct device_node * np,const char * propname,u32 index,u64 * out_value)170 int of_property_read_u64_index(const struct device_node *np,
171 const char *propname,
172 u32 index, u64 *out_value)
173 {
174 const u64 *val = of_find_property_value_of_size(np, propname,
175 ((index + 1) * sizeof(*out_value)),
176 0, NULL);
177
178 if (IS_ERR(val))
179 return PTR_ERR(val);
180
181 *out_value = be64_to_cpup(((__be64 *)val) + index);
182 return 0;
183 }
184 EXPORT_SYMBOL_GPL(of_property_read_u64_index);
185
186 /**
187 * of_property_read_variable_u8_array - Find and read an array of u8 from a
188 * property, with bounds on the minimum and maximum array size.
189 *
190 * @np: device node from which the property value is to be read.
191 * @propname: name of the property to be searched.
192 * @out_values: pointer to found values.
193 * @sz_min: minimum number of array elements to read
194 * @sz_max: maximum number of array elements to read, if zero there is no
195 * upper limit on the number of elements in the dts entry but only
196 * sz_min will be read.
197 *
198 * Search for a property in a device node and read 8-bit value(s) from
199 * it. Returns number of elements read on success, -EINVAL if the property
200 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
201 * if the property data is smaller than sz_min or longer than sz_max.
202 *
203 * dts entry of array should be like:
204 * property = /bits/ 8 <0x50 0x60 0x70>;
205 *
206 * The out_values is modified only if a valid u8 value can be decoded.
207 */
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)208 int of_property_read_variable_u8_array(const struct device_node *np,
209 const char *propname, u8 *out_values,
210 size_t sz_min, size_t sz_max)
211 {
212 size_t sz, count;
213 const u8 *val = of_find_property_value_of_size(np, propname,
214 (sz_min * sizeof(*out_values)),
215 (sz_max * sizeof(*out_values)),
216 &sz);
217
218 if (IS_ERR(val))
219 return PTR_ERR(val);
220
221 if (!sz_max)
222 sz = sz_min;
223 else
224 sz /= sizeof(*out_values);
225
226 count = sz;
227 while (count--)
228 *out_values++ = *val++;
229
230 return sz;
231 }
232 EXPORT_SYMBOL_GPL(of_property_read_variable_u8_array);
233
234 /**
235 * of_property_read_variable_u16_array - Find and read an array of u16 from a
236 * property, with bounds on the minimum and maximum array size.
237 *
238 * @np: device node from which the property value is to be read.
239 * @propname: name of the property to be searched.
240 * @out_values: pointer to found values.
241 * @sz_min: minimum number of array elements to read
242 * @sz_max: maximum number of array elements to read, if zero there is no
243 * upper limit on the number of elements in the dts entry but only
244 * sz_min will be read.
245 *
246 * Search for a property in a device node and read 16-bit value(s) from
247 * it. Returns number of elements read on success, -EINVAL if the property
248 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
249 * if the property data is smaller than sz_min or longer than sz_max.
250 *
251 * dts entry of array should be like:
252 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
253 *
254 * The out_values is modified only if a valid u16 value can be decoded.
255 */
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)256 int of_property_read_variable_u16_array(const struct device_node *np,
257 const char *propname, u16 *out_values,
258 size_t sz_min, size_t sz_max)
259 {
260 size_t sz, count;
261 const __be16 *val = of_find_property_value_of_size(np, propname,
262 (sz_min * sizeof(*out_values)),
263 (sz_max * sizeof(*out_values)),
264 &sz);
265
266 if (IS_ERR(val))
267 return PTR_ERR(val);
268
269 if (!sz_max)
270 sz = sz_min;
271 else
272 sz /= sizeof(*out_values);
273
274 count = sz;
275 while (count--)
276 *out_values++ = be16_to_cpup(val++);
277
278 return sz;
279 }
280 EXPORT_SYMBOL_GPL(of_property_read_variable_u16_array);
281
282 /**
283 * of_property_read_variable_u32_array - Find and read an array of 32 bit
284 * integers from a property, with bounds on the minimum and maximum array size.
285 *
286 * @np: device node from which the property value is to be read.
287 * @propname: name of the property to be searched.
288 * @out_values: pointer to return found values.
289 * @sz_min: minimum number of array elements to read
290 * @sz_max: maximum number of array elements to read, if zero there is no
291 * upper limit on the number of elements in the dts entry but only
292 * sz_min will be read.
293 *
294 * Search for a property in a device node and read 32-bit value(s) from
295 * it. Returns number of elements read on success, -EINVAL if the property
296 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
297 * if the property data is smaller than sz_min or longer than sz_max.
298 *
299 * The out_values is modified only if a valid u32 value can be decoded.
300 */
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)301 int of_property_read_variable_u32_array(const struct device_node *np,
302 const char *propname, u32 *out_values,
303 size_t sz_min, size_t sz_max)
304 {
305 size_t sz, count;
306 const __be32 *val = of_find_property_value_of_size(np, propname,
307 (sz_min * sizeof(*out_values)),
308 (sz_max * sizeof(*out_values)),
309 &sz);
310
311 if (IS_ERR(val))
312 return PTR_ERR(val);
313
314 if (!sz_max)
315 sz = sz_min;
316 else
317 sz /= sizeof(*out_values);
318
319 count = sz;
320 while (count--)
321 *out_values++ = be32_to_cpup(val++);
322
323 return sz;
324 }
325 EXPORT_SYMBOL_GPL(of_property_read_variable_u32_array);
326
327 /**
328 * of_property_read_u64 - Find and read a 64 bit integer from a property
329 * @np: device node from which the property value is to be read.
330 * @propname: name of the property to be searched.
331 * @out_value: pointer to return value, modified only if return value is 0.
332 *
333 * Search for a property in a device node and read a 64-bit value from
334 * it. Returns 0 on success, -EINVAL if the property does not exist,
335 * -ENODATA if property does not have a value, and -EOVERFLOW if the
336 * property data isn't large enough.
337 *
338 * The out_value is modified only if a valid u64 value can be decoded.
339 */
of_property_read_u64(const struct device_node * np,const char * propname,u64 * out_value)340 int of_property_read_u64(const struct device_node *np, const char *propname,
341 u64 *out_value)
342 {
343 const __be32 *val = of_find_property_value_of_size(np, propname,
344 sizeof(*out_value),
345 0,
346 NULL);
347
348 if (IS_ERR(val))
349 return PTR_ERR(val);
350
351 *out_value = of_read_number(val, 2);
352 return 0;
353 }
354 EXPORT_SYMBOL_GPL(of_property_read_u64);
355
356 /**
357 * of_property_read_variable_u64_array - Find and read an array of 64 bit
358 * integers from a property, with bounds on the minimum and maximum array size.
359 *
360 * @np: device node from which the property value is to be read.
361 * @propname: name of the property to be searched.
362 * @out_values: pointer to found values.
363 * @sz_min: minimum number of array elements to read
364 * @sz_max: maximum number of array elements to read, if zero there is no
365 * upper limit on the number of elements in the dts entry but only
366 * sz_min will be read.
367 *
368 * Search for a property in a device node and read 64-bit value(s) from
369 * it. Returns number of elements read on success, -EINVAL if the property
370 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
371 * if the property data is smaller than sz_min or longer than sz_max.
372 *
373 * The out_values is modified only if a valid u64 value can be decoded.
374 */
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)375 int of_property_read_variable_u64_array(const struct device_node *np,
376 const char *propname, u64 *out_values,
377 size_t sz_min, size_t sz_max)
378 {
379 size_t sz, count;
380 const __be32 *val = of_find_property_value_of_size(np, propname,
381 (sz_min * sizeof(*out_values)),
382 (sz_max * sizeof(*out_values)),
383 &sz);
384
385 if (IS_ERR(val))
386 return PTR_ERR(val);
387
388 if (!sz_max)
389 sz = sz_min;
390 else
391 sz /= sizeof(*out_values);
392
393 count = sz;
394 while (count--) {
395 *out_values++ = of_read_number(val, 2);
396 val += 2;
397 }
398
399 return sz;
400 }
401 EXPORT_SYMBOL_GPL(of_property_read_variable_u64_array);
402
403 /**
404 * of_property_read_string - Find and read a string from a property
405 * @np: device node from which the property value is to be read.
406 * @propname: name of the property to be searched.
407 * @out_string: pointer to null terminated return string, modified only if
408 * return value is 0.
409 *
410 * Search for a property in a device tree node and retrieve a null
411 * terminated string value (pointer to data, not a copy). Returns 0 on
412 * success, -EINVAL if the property does not exist, -ENODATA if property
413 * does not have a value, and -EILSEQ if the string is not null-terminated
414 * within the length of the property data.
415 *
416 * The out_string pointer is modified only if a valid string can be decoded.
417 */
of_property_read_string(const struct device_node * np,const char * propname,const char ** out_string)418 int of_property_read_string(const struct device_node *np, const char *propname,
419 const char **out_string)
420 {
421 const struct property *prop = of_find_property(np, propname, NULL);
422 if (!prop)
423 return -EINVAL;
424 if (!prop->value)
425 return -ENODATA;
426 if (strnlen(prop->value, prop->length) >= prop->length)
427 return -EILSEQ;
428 *out_string = prop->value;
429 return 0;
430 }
431 EXPORT_SYMBOL_GPL(of_property_read_string);
432
433 /**
434 * of_property_match_string() - Find string in a list and return index
435 * @np: pointer to node containing string list property
436 * @propname: string list property name
437 * @string: pointer to string to search for in string list
438 *
439 * This function searches a string list property and returns the index
440 * of a specific string value.
441 */
of_property_match_string(const struct device_node * np,const char * propname,const char * string)442 int of_property_match_string(const struct device_node *np, const char *propname,
443 const char *string)
444 {
445 const struct property *prop = of_find_property(np, propname, NULL);
446 size_t l;
447 int i;
448 const char *p, *end;
449
450 if (!prop)
451 return -EINVAL;
452 if (!prop->value)
453 return -ENODATA;
454
455 p = prop->value;
456 end = p + prop->length;
457
458 for (i = 0; p < end; i++, p += l) {
459 l = strnlen(p, end - p) + 1;
460 if (p + l > end)
461 return -EILSEQ;
462 pr_debug("comparing %s with %s\n", string, p);
463 if (strcmp(string, p) == 0)
464 return i; /* Found it; return index */
465 }
466 return -ENODATA;
467 }
468 EXPORT_SYMBOL_GPL(of_property_match_string);
469
470 /**
471 * of_property_read_string_helper() - Utility helper for parsing string properties
472 * @np: device node from which the property value is to be read.
473 * @propname: name of the property to be searched.
474 * @out_strs: output array of string pointers.
475 * @sz: number of array elements to read.
476 * @skip: Number of strings to skip over at beginning of list.
477 *
478 * Don't call this function directly. It is a utility helper for the
479 * of_property_read_string*() family of functions.
480 */
of_property_read_string_helper(const struct device_node * np,const char * propname,const char ** out_strs,size_t sz,int skip)481 int of_property_read_string_helper(const struct device_node *np,
482 const char *propname, const char **out_strs,
483 size_t sz, int skip)
484 {
485 const struct property *prop = of_find_property(np, propname, NULL);
486 int l = 0, i = 0;
487 const char *p, *end;
488
489 if (!prop)
490 return -EINVAL;
491 if (!prop->value)
492 return -ENODATA;
493 p = prop->value;
494 end = p + prop->length;
495
496 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
497 l = strnlen(p, end - p) + 1;
498 if (p + l > end)
499 return -EILSEQ;
500 if (out_strs && i >= skip)
501 *out_strs++ = p;
502 }
503 i -= skip;
504 return i <= 0 ? -ENODATA : i;
505 }
506 EXPORT_SYMBOL_GPL(of_property_read_string_helper);
507
of_prop_next_u32(struct property * prop,const __be32 * cur,u32 * pu)508 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
509 u32 *pu)
510 {
511 const void *curv = cur;
512
513 if (!prop)
514 return NULL;
515
516 if (!cur) {
517 curv = prop->value;
518 goto out_val;
519 }
520
521 curv += sizeof(*cur);
522 if (curv >= prop->value + prop->length)
523 return NULL;
524
525 out_val:
526 *pu = be32_to_cpup(curv);
527 return curv;
528 }
529 EXPORT_SYMBOL_GPL(of_prop_next_u32);
530
of_prop_next_string(struct property * prop,const char * cur)531 const char *of_prop_next_string(struct property *prop, const char *cur)
532 {
533 const void *curv = cur;
534
535 if (!prop)
536 return NULL;
537
538 if (!cur)
539 return prop->value;
540
541 curv += strlen(cur) + 1;
542 if (curv >= prop->value + prop->length)
543 return NULL;
544
545 return curv;
546 }
547 EXPORT_SYMBOL_GPL(of_prop_next_string);
548
549 /**
550 * of_graph_parse_endpoint() - parse common endpoint node properties
551 * @node: pointer to endpoint device_node
552 * @endpoint: pointer to the OF endpoint data structure
553 *
554 * The caller should hold a reference to @node.
555 */
of_graph_parse_endpoint(const struct device_node * node,struct of_endpoint * endpoint)556 int of_graph_parse_endpoint(const struct device_node *node,
557 struct of_endpoint *endpoint)
558 {
559 struct device_node *port_node = of_get_parent(node);
560
561 WARN_ONCE(!port_node, "%s(): endpoint %pOF has no parent node\n",
562 __func__, node);
563
564 memset(endpoint, 0, sizeof(*endpoint));
565
566 endpoint->local_node = node;
567 /*
568 * It doesn't matter whether the two calls below succeed.
569 * If they don't then the default value 0 is used.
570 */
571 of_property_read_u32(port_node, "reg", &endpoint->port);
572 of_property_read_u32(node, "reg", &endpoint->id);
573
574 of_node_put(port_node);
575
576 return 0;
577 }
578 EXPORT_SYMBOL(of_graph_parse_endpoint);
579
580 /**
581 * of_graph_get_port_by_id() - get the port matching a given id
582 * @parent: pointer to the parent device node
583 * @id: id of the port
584 *
585 * Return: A 'port' node pointer with refcount incremented. The caller
586 * has to use of_node_put() on it when done.
587 */
of_graph_get_port_by_id(struct device_node * parent,u32 id)588 struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id)
589 {
590 struct device_node *node, *port;
591
592 node = of_get_child_by_name(parent, "ports");
593 if (node)
594 parent = node;
595
596 for_each_child_of_node(parent, port) {
597 u32 port_id = 0;
598
599 if (!of_node_name_eq(port, "port"))
600 continue;
601 of_property_read_u32(port, "reg", &port_id);
602 if (id == port_id)
603 break;
604 }
605
606 of_node_put(node);
607
608 return port;
609 }
610 EXPORT_SYMBOL(of_graph_get_port_by_id);
611
612 /**
613 * of_graph_get_next_endpoint() - get next endpoint node
614 * @parent: pointer to the parent device node
615 * @prev: previous endpoint node, or NULL to get first
616 *
617 * Return: An 'endpoint' node pointer with refcount incremented. Refcount
618 * of the passed @prev node is decremented.
619 */
of_graph_get_next_endpoint(const struct device_node * parent,struct device_node * prev)620 struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
621 struct device_node *prev)
622 {
623 struct device_node *endpoint;
624 struct device_node *port;
625
626 if (!parent)
627 return NULL;
628
629 /*
630 * Start by locating the port node. If no previous endpoint is specified
631 * search for the first port node, otherwise get the previous endpoint
632 * parent port node.
633 */
634 if (!prev) {
635 struct device_node *node;
636
637 node = of_get_child_by_name(parent, "ports");
638 if (node)
639 parent = node;
640
641 port = of_get_child_by_name(parent, "port");
642 of_node_put(node);
643
644 if (!port) {
645 pr_err("graph: no port node found in %pOF\n", parent);
646 return NULL;
647 }
648 } else {
649 port = of_get_parent(prev);
650 if (WARN_ONCE(!port, "%s(): endpoint %pOF has no parent node\n",
651 __func__, prev))
652 return NULL;
653 }
654
655 while (1) {
656 /*
657 * Now that we have a port node, get the next endpoint by
658 * getting the next child. If the previous endpoint is NULL this
659 * will return the first child.
660 */
661 endpoint = of_get_next_child(port, prev);
662 if (endpoint) {
663 of_node_put(port);
664 return endpoint;
665 }
666
667 /* No more endpoints under this port, try the next one. */
668 prev = NULL;
669
670 do {
671 port = of_get_next_child(parent, port);
672 if (!port)
673 return NULL;
674 } while (!of_node_name_eq(port, "port"));
675 }
676 }
677 EXPORT_SYMBOL(of_graph_get_next_endpoint);
678
679 /**
680 * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers
681 * @parent: pointer to the parent device node
682 * @port_reg: identifier (value of reg property) of the parent port node
683 * @reg: identifier (value of reg property) of the endpoint node
684 *
685 * Return: An 'endpoint' node pointer which is identified by reg and at the same
686 * is the child of a port node identified by port_reg. reg and port_reg are
687 * ignored when they are -1. Use of_node_put() on the pointer when done.
688 */
of_graph_get_endpoint_by_regs(const struct device_node * parent,int port_reg,int reg)689 struct device_node *of_graph_get_endpoint_by_regs(
690 const struct device_node *parent, int port_reg, int reg)
691 {
692 struct of_endpoint endpoint;
693 struct device_node *node = NULL;
694
695 for_each_endpoint_of_node(parent, node) {
696 of_graph_parse_endpoint(node, &endpoint);
697 if (((port_reg == -1) || (endpoint.port == port_reg)) &&
698 ((reg == -1) || (endpoint.id == reg)))
699 return node;
700 }
701
702 return NULL;
703 }
704 EXPORT_SYMBOL(of_graph_get_endpoint_by_regs);
705
706 /**
707 * of_graph_get_remote_endpoint() - get remote endpoint node
708 * @node: pointer to a local endpoint device_node
709 *
710 * Return: Remote endpoint node associated with remote endpoint node linked
711 * to @node. Use of_node_put() on it when done.
712 */
of_graph_get_remote_endpoint(const struct device_node * node)713 struct device_node *of_graph_get_remote_endpoint(const struct device_node *node)
714 {
715 /* Get remote endpoint node. */
716 return of_parse_phandle(node, "remote-endpoint", 0);
717 }
718 EXPORT_SYMBOL(of_graph_get_remote_endpoint);
719
720 /**
721 * of_graph_get_port_parent() - get port's parent node
722 * @node: pointer to a local endpoint device_node
723 *
724 * Return: device node associated with endpoint node linked
725 * to @node. Use of_node_put() on it when done.
726 */
of_graph_get_port_parent(struct device_node * node)727 struct device_node *of_graph_get_port_parent(struct device_node *node)
728 {
729 unsigned int depth;
730
731 if (!node)
732 return NULL;
733
734 /*
735 * Preserve usecount for passed in node as of_get_next_parent()
736 * will do of_node_put() on it.
737 */
738 of_node_get(node);
739
740 /* Walk 3 levels up only if there is 'ports' node. */
741 for (depth = 3; depth && node; depth--) {
742 node = of_get_next_parent(node);
743 if (depth == 2 && !of_node_name_eq(node, "ports"))
744 break;
745 }
746 return node;
747 }
748 EXPORT_SYMBOL(of_graph_get_port_parent);
749
750 /**
751 * of_graph_get_remote_port_parent() - get remote port's parent node
752 * @node: pointer to a local endpoint device_node
753 *
754 * Return: Remote device node associated with remote endpoint node linked
755 * to @node. Use of_node_put() on it when done.
756 */
of_graph_get_remote_port_parent(const struct device_node * node)757 struct device_node *of_graph_get_remote_port_parent(
758 const struct device_node *node)
759 {
760 struct device_node *np, *pp;
761
762 /* Get remote endpoint node. */
763 np = of_graph_get_remote_endpoint(node);
764
765 pp = of_graph_get_port_parent(np);
766
767 of_node_put(np);
768
769 return pp;
770 }
771 EXPORT_SYMBOL(of_graph_get_remote_port_parent);
772
773 /**
774 * of_graph_get_remote_port() - get remote port node
775 * @node: pointer to a local endpoint device_node
776 *
777 * Return: Remote port node associated with remote endpoint node linked
778 * to @node. Use of_node_put() on it when done.
779 */
of_graph_get_remote_port(const struct device_node * node)780 struct device_node *of_graph_get_remote_port(const struct device_node *node)
781 {
782 struct device_node *np;
783
784 /* Get remote endpoint node. */
785 np = of_graph_get_remote_endpoint(node);
786 if (!np)
787 return NULL;
788 return of_get_next_parent(np);
789 }
790 EXPORT_SYMBOL(of_graph_get_remote_port);
791
of_graph_get_endpoint_count(const struct device_node * np)792 int of_graph_get_endpoint_count(const struct device_node *np)
793 {
794 struct device_node *endpoint;
795 int num = 0;
796
797 for_each_endpoint_of_node(np, endpoint)
798 num++;
799
800 return num;
801 }
802 EXPORT_SYMBOL(of_graph_get_endpoint_count);
803
804 /**
805 * of_graph_get_remote_node() - get remote parent device_node for given port/endpoint
806 * @node: pointer to parent device_node containing graph port/endpoint
807 * @port: identifier (value of reg property) of the parent port node
808 * @endpoint: identifier (value of reg property) of the endpoint node
809 *
810 * Return: Remote device node associated with remote endpoint node linked
811 * to @node. Use of_node_put() on it when done.
812 */
of_graph_get_remote_node(const struct device_node * node,u32 port,u32 endpoint)813 struct device_node *of_graph_get_remote_node(const struct device_node *node,
814 u32 port, u32 endpoint)
815 {
816 struct device_node *endpoint_node, *remote;
817
818 endpoint_node = of_graph_get_endpoint_by_regs(node, port, endpoint);
819 if (!endpoint_node) {
820 pr_debug("no valid endpoint (%d, %d) for node %pOF\n",
821 port, endpoint, node);
822 return NULL;
823 }
824
825 remote = of_graph_get_remote_port_parent(endpoint_node);
826 of_node_put(endpoint_node);
827 if (!remote) {
828 pr_debug("no valid remote node\n");
829 return NULL;
830 }
831
832 if (!of_device_is_available(remote)) {
833 pr_debug("not available for remote node\n");
834 of_node_put(remote);
835 return NULL;
836 }
837
838 return remote;
839 }
840 EXPORT_SYMBOL(of_graph_get_remote_node);
841
of_fwnode_get(struct fwnode_handle * fwnode)842 static struct fwnode_handle *of_fwnode_get(struct fwnode_handle *fwnode)
843 {
844 return of_fwnode_handle(of_node_get(to_of_node(fwnode)));
845 }
846
of_fwnode_put(struct fwnode_handle * fwnode)847 static void of_fwnode_put(struct fwnode_handle *fwnode)
848 {
849 of_node_put(to_of_node(fwnode));
850 }
851
of_fwnode_device_is_available(const struct fwnode_handle * fwnode)852 static bool of_fwnode_device_is_available(const struct fwnode_handle *fwnode)
853 {
854 return of_device_is_available(to_of_node(fwnode));
855 }
856
of_fwnode_property_present(const struct fwnode_handle * fwnode,const char * propname)857 static bool of_fwnode_property_present(const struct fwnode_handle *fwnode,
858 const char *propname)
859 {
860 return of_property_read_bool(to_of_node(fwnode), propname);
861 }
862
of_fwnode_property_read_int_array(const struct fwnode_handle * fwnode,const char * propname,unsigned int elem_size,void * val,size_t nval)863 static int of_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
864 const char *propname,
865 unsigned int elem_size, void *val,
866 size_t nval)
867 {
868 const struct device_node *node = to_of_node(fwnode);
869
870 if (!val)
871 return of_property_count_elems_of_size(node, propname,
872 elem_size);
873
874 switch (elem_size) {
875 case sizeof(u8):
876 return of_property_read_u8_array(node, propname, val, nval);
877 case sizeof(u16):
878 return of_property_read_u16_array(node, propname, val, nval);
879 case sizeof(u32):
880 return of_property_read_u32_array(node, propname, val, nval);
881 case sizeof(u64):
882 return of_property_read_u64_array(node, propname, val, nval);
883 }
884
885 return -ENXIO;
886 }
887
888 static int
of_fwnode_property_read_string_array(const struct fwnode_handle * fwnode,const char * propname,const char ** val,size_t nval)889 of_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
890 const char *propname, const char **val,
891 size_t nval)
892 {
893 const struct device_node *node = to_of_node(fwnode);
894
895 return val ?
896 of_property_read_string_array(node, propname, val, nval) :
897 of_property_count_strings(node, propname);
898 }
899
of_fwnode_get_name(const struct fwnode_handle * fwnode)900 static const char *of_fwnode_get_name(const struct fwnode_handle *fwnode)
901 {
902 return kbasename(to_of_node(fwnode)->full_name);
903 }
904
of_fwnode_get_name_prefix(const struct fwnode_handle * fwnode)905 static const char *of_fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
906 {
907 /* Root needs no prefix here (its name is "/"). */
908 if (!to_of_node(fwnode)->parent)
909 return "";
910
911 return "/";
912 }
913
914 static struct fwnode_handle *
of_fwnode_get_parent(const struct fwnode_handle * fwnode)915 of_fwnode_get_parent(const struct fwnode_handle *fwnode)
916 {
917 return of_fwnode_handle(of_get_parent(to_of_node(fwnode)));
918 }
919
920 static struct fwnode_handle *
of_fwnode_get_next_child_node(const struct fwnode_handle * fwnode,struct fwnode_handle * child)921 of_fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
922 struct fwnode_handle *child)
923 {
924 return of_fwnode_handle(of_get_next_available_child(to_of_node(fwnode),
925 to_of_node(child)));
926 }
927
928 static struct fwnode_handle *
of_fwnode_get_named_child_node(const struct fwnode_handle * fwnode,const char * childname)929 of_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
930 const char *childname)
931 {
932 const struct device_node *node = to_of_node(fwnode);
933 struct device_node *child;
934
935 for_each_available_child_of_node(node, child)
936 if (of_node_name_eq(child, childname))
937 return of_fwnode_handle(child);
938
939 return NULL;
940 }
941
942 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)943 of_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
944 const char *prop, const char *nargs_prop,
945 unsigned int nargs, unsigned int index,
946 struct fwnode_reference_args *args)
947 {
948 struct of_phandle_args of_args;
949 unsigned int i;
950 int ret;
951
952 if (nargs_prop)
953 ret = of_parse_phandle_with_args(to_of_node(fwnode), prop,
954 nargs_prop, index, &of_args);
955 else
956 ret = of_parse_phandle_with_fixed_args(to_of_node(fwnode), prop,
957 nargs, index, &of_args);
958 if (ret < 0)
959 return ret;
960 if (!args) {
961 of_node_put(of_args.np);
962 return 0;
963 }
964
965 args->nargs = of_args.args_count;
966 args->fwnode = of_fwnode_handle(of_args.np);
967
968 for (i = 0; i < NR_FWNODE_REFERENCE_ARGS; i++)
969 args->args[i] = i < of_args.args_count ? of_args.args[i] : 0;
970
971 return 0;
972 }
973
974 static struct fwnode_handle *
of_fwnode_graph_get_next_endpoint(const struct fwnode_handle * fwnode,struct fwnode_handle * prev)975 of_fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
976 struct fwnode_handle *prev)
977 {
978 return of_fwnode_handle(of_graph_get_next_endpoint(to_of_node(fwnode),
979 to_of_node(prev)));
980 }
981
982 static struct fwnode_handle *
of_fwnode_graph_get_remote_endpoint(const struct fwnode_handle * fwnode)983 of_fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
984 {
985 return of_fwnode_handle(
986 of_graph_get_remote_endpoint(to_of_node(fwnode)));
987 }
988
989 static struct fwnode_handle *
of_fwnode_graph_get_port_parent(struct fwnode_handle * fwnode)990 of_fwnode_graph_get_port_parent(struct fwnode_handle *fwnode)
991 {
992 struct device_node *np;
993
994 /* Get the parent of the port */
995 np = of_get_parent(to_of_node(fwnode));
996 if (!np)
997 return NULL;
998
999 /* Is this the "ports" node? If not, it's the port parent. */
1000 if (!of_node_name_eq(np, "ports"))
1001 return of_fwnode_handle(np);
1002
1003 return of_fwnode_handle(of_get_next_parent(np));
1004 }
1005
of_fwnode_graph_parse_endpoint(const struct fwnode_handle * fwnode,struct fwnode_endpoint * endpoint)1006 static int of_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
1007 struct fwnode_endpoint *endpoint)
1008 {
1009 const struct device_node *node = to_of_node(fwnode);
1010 struct device_node *port_node = of_get_parent(node);
1011
1012 endpoint->local_fwnode = fwnode;
1013
1014 of_property_read_u32(port_node, "reg", &endpoint->port);
1015 of_property_read_u32(node, "reg", &endpoint->id);
1016
1017 of_node_put(port_node);
1018
1019 return 0;
1020 }
1021
1022 static const void *
of_fwnode_device_get_match_data(const struct fwnode_handle * fwnode,const struct device * dev)1023 of_fwnode_device_get_match_data(const struct fwnode_handle *fwnode,
1024 const struct device *dev)
1025 {
1026 return of_device_get_match_data(dev);
1027 }
1028
of_is_ancestor_of(struct device_node * test_ancestor,struct device_node * child)1029 static bool of_is_ancestor_of(struct device_node *test_ancestor,
1030 struct device_node *child)
1031 {
1032 of_node_get(child);
1033 while (child) {
1034 if (child == test_ancestor) {
1035 of_node_put(child);
1036 return true;
1037 }
1038 child = of_get_next_parent(child);
1039 }
1040 return false;
1041 }
1042
1043 /**
1044 * of_link_to_phandle - Add fwnode link to supplier from supplier phandle
1045 * @con_np: consumer device tree node
1046 * @sup_np: supplier device tree node
1047 *
1048 * Given a phandle to a supplier device tree node (@sup_np), this function
1049 * finds the device that owns the supplier device tree node and creates a
1050 * device link from @dev consumer device to the supplier device. This function
1051 * doesn't create device links for invalid scenarios such as trying to create a
1052 * link with a parent device as the consumer of its child device. In such
1053 * cases, it returns an error.
1054 *
1055 * Returns:
1056 * - 0 if fwnode link successfully created to supplier
1057 * - -EINVAL if the supplier link is invalid and should not be created
1058 * - -ENODEV if struct device will never be create for supplier
1059 */
of_link_to_phandle(struct device_node * con_np,struct device_node * sup_np)1060 static int of_link_to_phandle(struct device_node *con_np,
1061 struct device_node *sup_np)
1062 {
1063 struct device *sup_dev;
1064 struct device_node *tmp_np = sup_np;
1065
1066 of_node_get(sup_np);
1067 /*
1068 * Find the device node that contains the supplier phandle. It may be
1069 * @sup_np or it may be an ancestor of @sup_np.
1070 */
1071 while (sup_np) {
1072
1073 /* Don't allow linking to a disabled supplier */
1074 if (!of_device_is_available(sup_np)) {
1075 of_node_put(sup_np);
1076 sup_np = NULL;
1077 }
1078
1079 if (of_find_property(sup_np, "compatible", NULL))
1080 break;
1081
1082 sup_np = of_get_next_parent(sup_np);
1083 }
1084
1085 if (!sup_np) {
1086 pr_debug("Not linking %pOFP to %pOFP - No device\n",
1087 con_np, tmp_np);
1088 return -ENODEV;
1089 }
1090
1091 /*
1092 * Don't allow linking a device node as a consumer of one of its
1093 * descendant nodes. By definition, a child node can't be a functional
1094 * dependency for the parent node.
1095 */
1096 if (of_is_ancestor_of(con_np, sup_np)) {
1097 pr_debug("Not linking %pOFP to %pOFP - is descendant\n",
1098 con_np, sup_np);
1099 of_node_put(sup_np);
1100 return -EINVAL;
1101 }
1102
1103 /*
1104 * Don't create links to "early devices" that won't have struct devices
1105 * created for them.
1106 */
1107 sup_dev = get_dev_from_fwnode(&sup_np->fwnode);
1108 if (!sup_dev &&
1109 (of_node_check_flag(sup_np, OF_POPULATED) ||
1110 sup_np->fwnode.flags & FWNODE_FLAG_NOT_DEVICE)) {
1111 pr_debug("Not linking %pOFP to %pOFP - No struct device\n",
1112 con_np, sup_np);
1113 of_node_put(sup_np);
1114 return -ENODEV;
1115 }
1116 put_device(sup_dev);
1117
1118 fwnode_link_add(of_fwnode_handle(con_np), of_fwnode_handle(sup_np));
1119 of_node_put(sup_np);
1120
1121 return 0;
1122 }
1123
1124 /**
1125 * parse_prop_cells - Property parsing function for suppliers
1126 *
1127 * @np: Pointer to device tree node containing a list
1128 * @prop_name: Name of property to be parsed. Expected to hold phandle values
1129 * @index: For properties holding a list of phandles, this is the index
1130 * into the list.
1131 * @list_name: Property name that is known to contain list of phandle(s) to
1132 * supplier(s)
1133 * @cells_name: property name that specifies phandles' arguments count
1134 *
1135 * This is a helper function to parse properties that have a known fixed name
1136 * and are a list of phandles and phandle arguments.
1137 *
1138 * Returns:
1139 * - phandle node pointer with refcount incremented. Caller must of_node_put()
1140 * on it when done.
1141 * - NULL if no phandle found at index
1142 */
parse_prop_cells(struct device_node * np,const char * prop_name,int index,const char * list_name,const char * cells_name)1143 static struct device_node *parse_prop_cells(struct device_node *np,
1144 const char *prop_name, int index,
1145 const char *list_name,
1146 const char *cells_name)
1147 {
1148 struct of_phandle_args sup_args;
1149
1150 if (strcmp(prop_name, list_name))
1151 return NULL;
1152
1153 if (of_parse_phandle_with_args(np, list_name, cells_name, index,
1154 &sup_args))
1155 return NULL;
1156
1157 return sup_args.np;
1158 }
1159
1160 #define DEFINE_SIMPLE_PROP(fname, name, cells) \
1161 static struct device_node *parse_##fname(struct device_node *np, \
1162 const char *prop_name, int index) \
1163 { \
1164 return parse_prop_cells(np, prop_name, index, name, cells); \
1165 }
1166
strcmp_suffix(const char * str,const char * suffix)1167 static int strcmp_suffix(const char *str, const char *suffix)
1168 {
1169 unsigned int len, suffix_len;
1170
1171 len = strlen(str);
1172 suffix_len = strlen(suffix);
1173 if (len <= suffix_len)
1174 return -1;
1175 return strcmp(str + len - suffix_len, suffix);
1176 }
1177
1178 /**
1179 * parse_suffix_prop_cells - Suffix property parsing function for suppliers
1180 *
1181 * @np: Pointer to device tree node containing a list
1182 * @prop_name: Name of property to be parsed. Expected to hold phandle values
1183 * @index: For properties holding a list of phandles, this is the index
1184 * into the list.
1185 * @suffix: Property suffix that is known to contain list of phandle(s) to
1186 * supplier(s)
1187 * @cells_name: property name that specifies phandles' arguments count
1188 *
1189 * This is a helper function to parse properties that have a known fixed suffix
1190 * and are a list of phandles and phandle arguments.
1191 *
1192 * Returns:
1193 * - phandle node pointer with refcount incremented. Caller must of_node_put()
1194 * on it when done.
1195 * - NULL if no phandle found at index
1196 */
parse_suffix_prop_cells(struct device_node * np,const char * prop_name,int index,const char * suffix,const char * cells_name)1197 static struct device_node *parse_suffix_prop_cells(struct device_node *np,
1198 const char *prop_name, int index,
1199 const char *suffix,
1200 const char *cells_name)
1201 {
1202 struct of_phandle_args sup_args;
1203
1204 if (strcmp_suffix(prop_name, suffix))
1205 return NULL;
1206
1207 if (of_parse_phandle_with_args(np, prop_name, cells_name, index,
1208 &sup_args))
1209 return NULL;
1210
1211 return sup_args.np;
1212 }
1213
1214 #define DEFINE_SUFFIX_PROP(fname, suffix, cells) \
1215 static struct device_node *parse_##fname(struct device_node *np, \
1216 const char *prop_name, int index) \
1217 { \
1218 return parse_suffix_prop_cells(np, prop_name, index, suffix, cells); \
1219 }
1220
1221 /**
1222 * struct supplier_bindings - Property parsing functions for suppliers
1223 *
1224 * @parse_prop: function name
1225 * parse_prop() finds the node corresponding to a supplier phandle
1226 * @parse_prop.np: Pointer to device node holding supplier phandle property
1227 * @parse_prop.prop_name: Name of property holding a phandle value
1228 * @parse_prop.index: For properties holding a list of phandles, this is the
1229 * index into the list
1230 *
1231 * Returns:
1232 * parse_prop() return values are
1233 * - phandle node pointer with refcount incremented. Caller must of_node_put()
1234 * on it when done.
1235 * - NULL if no phandle found at index
1236 */
1237 struct supplier_bindings {
1238 struct device_node *(*parse_prop)(struct device_node *np,
1239 const char *prop_name, int index);
1240 bool optional;
1241 };
1242
1243 DEFINE_SIMPLE_PROP(clocks, "clocks", "#clock-cells")
1244 DEFINE_SIMPLE_PROP(interconnects, "interconnects", "#interconnect-cells")
1245 DEFINE_SIMPLE_PROP(iommus, "iommus", "#iommu-cells")
1246 DEFINE_SIMPLE_PROP(mboxes, "mboxes", "#mbox-cells")
1247 DEFINE_SIMPLE_PROP(io_channels, "io-channel", "#io-channel-cells")
1248 DEFINE_SIMPLE_PROP(interrupt_parent, "interrupt-parent", NULL)
1249 DEFINE_SIMPLE_PROP(dmas, "dmas", "#dma-cells")
1250 DEFINE_SIMPLE_PROP(power_domains, "power-domains", "#power-domain-cells")
1251 DEFINE_SIMPLE_PROP(hwlocks, "hwlocks", "#hwlock-cells")
1252 DEFINE_SIMPLE_PROP(extcon, "extcon", NULL)
1253 DEFINE_SIMPLE_PROP(nvmem_cells, "nvmem-cells", NULL)
1254 DEFINE_SIMPLE_PROP(phys, "phys", "#phy-cells")
1255 DEFINE_SIMPLE_PROP(wakeup_parent, "wakeup-parent", NULL)
1256 DEFINE_SIMPLE_PROP(pinctrl0, "pinctrl-0", NULL)
1257 DEFINE_SIMPLE_PROP(pinctrl1, "pinctrl-1", NULL)
1258 DEFINE_SIMPLE_PROP(pinctrl2, "pinctrl-2", NULL)
1259 DEFINE_SIMPLE_PROP(pinctrl3, "pinctrl-3", NULL)
1260 DEFINE_SIMPLE_PROP(pinctrl4, "pinctrl-4", NULL)
1261 DEFINE_SIMPLE_PROP(pinctrl5, "pinctrl-5", NULL)
1262 DEFINE_SIMPLE_PROP(pinctrl6, "pinctrl-6", NULL)
1263 DEFINE_SIMPLE_PROP(pinctrl7, "pinctrl-7", NULL)
1264 DEFINE_SIMPLE_PROP(pinctrl8, "pinctrl-8", NULL)
1265 DEFINE_SUFFIX_PROP(regulators, "-supply", NULL)
1266 DEFINE_SUFFIX_PROP(gpio, "-gpio", "#gpio-cells")
1267
parse_gpios(struct device_node * np,const char * prop_name,int index)1268 static struct device_node *parse_gpios(struct device_node *np,
1269 const char *prop_name, int index)
1270 {
1271 if (!strcmp_suffix(prop_name, ",nr-gpios"))
1272 return NULL;
1273
1274 return parse_suffix_prop_cells(np, prop_name, index, "-gpios",
1275 "#gpio-cells");
1276 }
1277
parse_iommu_maps(struct device_node * np,const char * prop_name,int index)1278 static struct device_node *parse_iommu_maps(struct device_node *np,
1279 const char *prop_name, int index)
1280 {
1281 if (strcmp(prop_name, "iommu-map"))
1282 return NULL;
1283
1284 return of_parse_phandle(np, prop_name, (index * 4) + 1);
1285 }
1286
parse_gpio_compat(struct device_node * np,const char * prop_name,int index)1287 static struct device_node *parse_gpio_compat(struct device_node *np,
1288 const char *prop_name, int index)
1289 {
1290 struct of_phandle_args sup_args;
1291
1292 if (strcmp(prop_name, "gpio") && strcmp(prop_name, "gpios"))
1293 return NULL;
1294
1295 /*
1296 * Ignore node with gpio-hog property since its gpios are all provided
1297 * by its parent.
1298 */
1299 if (of_find_property(np, "gpio-hog", NULL))
1300 return NULL;
1301
1302 if (of_parse_phandle_with_args(np, prop_name, "#gpio-cells", index,
1303 &sup_args))
1304 return NULL;
1305
1306 return sup_args.np;
1307 }
1308
parse_interrupts(struct device_node * np,const char * prop_name,int index)1309 static struct device_node *parse_interrupts(struct device_node *np,
1310 const char *prop_name, int index)
1311 {
1312 struct of_phandle_args sup_args;
1313
1314 if (!IS_ENABLED(CONFIG_OF_IRQ) || IS_ENABLED(CONFIG_PPC))
1315 return NULL;
1316
1317 if (strcmp(prop_name, "interrupts") &&
1318 strcmp(prop_name, "interrupts-extended"))
1319 return NULL;
1320
1321 return of_irq_parse_one(np, index, &sup_args) ? NULL : sup_args.np;
1322 }
1323
1324 static const struct supplier_bindings of_supplier_bindings[] = {
1325 { .parse_prop = parse_clocks, },
1326 { .parse_prop = parse_interconnects, },
1327 { .parse_prop = parse_iommus, .optional = true, },
1328 { .parse_prop = parse_iommu_maps, .optional = true, },
1329 { .parse_prop = parse_mboxes, },
1330 { .parse_prop = parse_io_channels, },
1331 { .parse_prop = parse_interrupt_parent, },
1332 { .parse_prop = parse_dmas, .optional = true, },
1333 { .parse_prop = parse_power_domains, },
1334 { .parse_prop = parse_hwlocks, },
1335 { .parse_prop = parse_extcon, },
1336 { .parse_prop = parse_nvmem_cells, },
1337 { .parse_prop = parse_phys, },
1338 { .parse_prop = parse_wakeup_parent, },
1339 { .parse_prop = parse_pinctrl0, },
1340 { .parse_prop = parse_pinctrl1, },
1341 { .parse_prop = parse_pinctrl2, },
1342 { .parse_prop = parse_pinctrl3, },
1343 { .parse_prop = parse_pinctrl4, },
1344 { .parse_prop = parse_pinctrl5, },
1345 { .parse_prop = parse_pinctrl6, },
1346 { .parse_prop = parse_pinctrl7, },
1347 { .parse_prop = parse_pinctrl8, },
1348 { .parse_prop = parse_gpio_compat, },
1349 { .parse_prop = parse_interrupts, },
1350 { .parse_prop = parse_regulators, },
1351 { .parse_prop = parse_gpio, },
1352 { .parse_prop = parse_gpios, },
1353 {}
1354 };
1355
1356 /**
1357 * of_link_property - Create device links to suppliers listed in a property
1358 * @dev: Consumer device
1359 * @con_np: The consumer device tree node which contains the property
1360 * @prop_name: Name of property to be parsed
1361 *
1362 * This function checks if the property @prop_name that is present in the
1363 * @con_np device tree node is one of the known common device tree bindings
1364 * that list phandles to suppliers. If @prop_name isn't one, this function
1365 * doesn't do anything.
1366 *
1367 * If @prop_name is one, this function attempts to create fwnode links from the
1368 * consumer device tree node @con_np to all the suppliers device tree nodes
1369 * listed in @prop_name.
1370 *
1371 * Any failed attempt to create a fwnode link will NOT result in an immediate
1372 * return. of_link_property() must create links to all the available supplier
1373 * device tree nodes even when attempts to create a link to one or more
1374 * suppliers fail.
1375 */
of_link_property(struct device_node * con_np,const char * prop_name)1376 static int of_link_property(struct device_node *con_np, const char *prop_name)
1377 {
1378 struct device_node *phandle;
1379 const struct supplier_bindings *s = of_supplier_bindings;
1380 unsigned int i = 0;
1381 bool matched = false;
1382 int ret = 0;
1383
1384 /* Do not stop at first failed link, link all available suppliers. */
1385 while (!matched && s->parse_prop) {
1386 if (s->optional && !fw_devlink_is_strict()) {
1387 s++;
1388 continue;
1389 }
1390
1391 while ((phandle = s->parse_prop(con_np, prop_name, i))) {
1392 matched = true;
1393 i++;
1394 of_link_to_phandle(con_np, phandle);
1395 of_node_put(phandle);
1396 }
1397 s++;
1398 }
1399 return ret;
1400 }
1401
of_fwnode_add_links(struct fwnode_handle * fwnode)1402 static int of_fwnode_add_links(struct fwnode_handle *fwnode)
1403 {
1404 struct property *p;
1405 struct device_node *con_np = to_of_node(fwnode);
1406
1407 if (!con_np)
1408 return -EINVAL;
1409
1410 for_each_property_of_node(con_np, p)
1411 of_link_property(con_np, p->name);
1412
1413 return 0;
1414 }
1415
1416 const struct fwnode_operations of_fwnode_ops = {
1417 .get = of_fwnode_get,
1418 .put = of_fwnode_put,
1419 .device_is_available = of_fwnode_device_is_available,
1420 .device_get_match_data = of_fwnode_device_get_match_data,
1421 .property_present = of_fwnode_property_present,
1422 .property_read_int_array = of_fwnode_property_read_int_array,
1423 .property_read_string_array = of_fwnode_property_read_string_array,
1424 .get_name = of_fwnode_get_name,
1425 .get_name_prefix = of_fwnode_get_name_prefix,
1426 .get_parent = of_fwnode_get_parent,
1427 .get_next_child_node = of_fwnode_get_next_child_node,
1428 .get_named_child_node = of_fwnode_get_named_child_node,
1429 .get_reference_args = of_fwnode_get_reference_args,
1430 .graph_get_next_endpoint = of_fwnode_graph_get_next_endpoint,
1431 .graph_get_remote_endpoint = of_fwnode_graph_get_remote_endpoint,
1432 .graph_get_port_parent = of_fwnode_graph_get_port_parent,
1433 .graph_parse_endpoint = of_fwnode_graph_parse_endpoint,
1434 .add_links = of_fwnode_add_links,
1435 };
1436 EXPORT_SYMBOL_GPL(of_fwnode_ops);
1437