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