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