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