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