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