xref: /openbmc/linux/drivers/of/property.c (revision d8bcaabe)
1 /*
2  * drivers/of/property.c - Procedures for accessing and interpreting
3  *			   Devicetree properties and graphs.
4  *
5  * Initially created by copying procedures from drivers/of/base.c. This
6  * file contains the OF property as well as the OF graph interface
7  * functions.
8  *
9  * Paul Mackerras	August 1996.
10  * Copyright (C) 1996-2005 Paul Mackerras.
11  *
12  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
13  *    {engebret|bergner}@us.ibm.com
14  *
15  *  Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
16  *
17  *  Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
18  *  Grant Likely.
19  *
20  *      This program is free software; you can redistribute it and/or
21  *      modify it under the terms of the GNU General Public License
22  *      as published by the Free Software Foundation; either version
23  *      2 of the License, or (at your option) any later version.
24  */
25 
26 #define pr_fmt(fmt)	"OF: " fmt
27 
28 #include <linux/of.h>
29 #include <linux/of_device.h>
30 #include <linux/of_graph.h>
31 #include <linux/string.h>
32 
33 #include "of_private.h"
34 
35 /**
36  * of_property_count_elems_of_size - Count the number of elements in a property
37  *
38  * @np:		device node from which the property value is to be read.
39  * @propname:	name of the property to be searched.
40  * @elem_size:	size of the individual element
41  *
42  * Search for a property in a device node and count the number of elements of
43  * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
44  * property does not exist or its length does not match a multiple of elem_size
45  * and -ENODATA if the property does not have a value.
46  */
47 int of_property_count_elems_of_size(const struct device_node *np,
48 				const char *propname, int elem_size)
49 {
50 	struct property *prop = of_find_property(np, propname, NULL);
51 
52 	if (!prop)
53 		return -EINVAL;
54 	if (!prop->value)
55 		return -ENODATA;
56 
57 	if (prop->length % elem_size != 0) {
58 		pr_err("size of %s in node %pOF is not a multiple of %d\n",
59 		       propname, np, elem_size);
60 		return -EINVAL;
61 	}
62 
63 	return prop->length / elem_size;
64 }
65 EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
66 
67 /**
68  * of_find_property_value_of_size
69  *
70  * @np:		device node from which the property value is to be read.
71  * @propname:	name of the property to be searched.
72  * @min:	minimum allowed length of property value
73  * @max:	maximum allowed length of property value (0 means unlimited)
74  * @len:	if !=NULL, actual length is written to here
75  *
76  * Search for a property in a device node and valid the requested size.
77  * Returns the property value on success, -EINVAL if the property does not
78  *  exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
79  * property data is too small or too large.
80  *
81  */
82 static void *of_find_property_value_of_size(const struct device_node *np,
83 			const char *propname, u32 min, u32 max, size_t *len)
84 {
85 	struct property *prop = of_find_property(np, propname, NULL);
86 
87 	if (!prop)
88 		return ERR_PTR(-EINVAL);
89 	if (!prop->value)
90 		return ERR_PTR(-ENODATA);
91 	if (prop->length < min)
92 		return ERR_PTR(-EOVERFLOW);
93 	if (max && prop->length > max)
94 		return ERR_PTR(-EOVERFLOW);
95 
96 	if (len)
97 		*len = prop->length;
98 
99 	return prop->value;
100 }
101 
102 /**
103  * of_property_read_u32_index - Find and read a u32 from a multi-value property.
104  *
105  * @np:		device node from which the property value is to be read.
106  * @propname:	name of the property to be searched.
107  * @index:	index of the u32 in the list of values
108  * @out_value:	pointer to return value, modified only if no error.
109  *
110  * Search for a property in a device node and read nth 32-bit value from
111  * it. Returns 0 on success, -EINVAL if the property does not exist,
112  * -ENODATA if property does not have a value, and -EOVERFLOW if the
113  * property data isn't large enough.
114  *
115  * The out_value is modified only if a valid u32 value can be decoded.
116  */
117 int of_property_read_u32_index(const struct device_node *np,
118 				       const char *propname,
119 				       u32 index, u32 *out_value)
120 {
121 	const u32 *val = of_find_property_value_of_size(np, propname,
122 					((index + 1) * sizeof(*out_value)),
123 					0,
124 					NULL);
125 
126 	if (IS_ERR(val))
127 		return PTR_ERR(val);
128 
129 	*out_value = be32_to_cpup(((__be32 *)val) + index);
130 	return 0;
131 }
132 EXPORT_SYMBOL_GPL(of_property_read_u32_index);
133 
134 /**
135  * of_property_read_u64_index - Find and read a u64 from a multi-value property.
136  *
137  * @np:		device node from which the property value is to be read.
138  * @propname:	name of the property to be searched.
139  * @index:	index of the u64 in the list of values
140  * @out_value:	pointer to return value, modified only if no error.
141  *
142  * Search for a property in a device node and read nth 64-bit value from
143  * it. Returns 0 on success, -EINVAL if the property does not exist,
144  * -ENODATA if property does not have a value, and -EOVERFLOW if the
145  * property data isn't large enough.
146  *
147  * The out_value is modified only if a valid u64 value can be decoded.
148  */
149 int of_property_read_u64_index(const struct device_node *np,
150 				       const char *propname,
151 				       u32 index, u64 *out_value)
152 {
153 	const u64 *val = of_find_property_value_of_size(np, propname,
154 					((index + 1) * sizeof(*out_value)),
155 					0, NULL);
156 
157 	if (IS_ERR(val))
158 		return PTR_ERR(val);
159 
160 	*out_value = be64_to_cpup(((__be64 *)val) + index);
161 	return 0;
162 }
163 EXPORT_SYMBOL_GPL(of_property_read_u64_index);
164 
165 /**
166  * of_property_read_variable_u8_array - Find and read an array of u8 from a
167  * property, with bounds on the minimum and maximum array size.
168  *
169  * @np:		device node from which the property value is to be read.
170  * @propname:	name of the property to be searched.
171  * @out_values:	pointer to return value, modified only if return value is 0.
172  * @sz_min:	minimum number of array elements to read
173  * @sz_max:	maximum number of array elements to read, if zero there is no
174  *		upper limit on the number of elements in the dts entry but only
175  *		sz_min will be read.
176  *
177  * Search for a property in a device node and read 8-bit value(s) from
178  * it. Returns number of elements read on success, -EINVAL if the property
179  * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
180  * if the property data is smaller than sz_min or longer than sz_max.
181  *
182  * dts entry of array should be like:
183  *	property = /bits/ 8 <0x50 0x60 0x70>;
184  *
185  * The out_values is modified only if a valid u8 value can be decoded.
186  */
187 int of_property_read_variable_u8_array(const struct device_node *np,
188 					const char *propname, u8 *out_values,
189 					size_t sz_min, size_t sz_max)
190 {
191 	size_t sz, count;
192 	const u8 *val = of_find_property_value_of_size(np, propname,
193 						(sz_min * sizeof(*out_values)),
194 						(sz_max * sizeof(*out_values)),
195 						&sz);
196 
197 	if (IS_ERR(val))
198 		return PTR_ERR(val);
199 
200 	if (!sz_max)
201 		sz = sz_min;
202 	else
203 		sz /= sizeof(*out_values);
204 
205 	count = sz;
206 	while (count--)
207 		*out_values++ = *val++;
208 
209 	return sz;
210 }
211 EXPORT_SYMBOL_GPL(of_property_read_variable_u8_array);
212 
213 /**
214  * of_property_read_variable_u16_array - Find and read an array of u16 from a
215  * property, with bounds on the minimum and maximum array size.
216  *
217  * @np:		device node from which the property value is to be read.
218  * @propname:	name of the property to be searched.
219  * @out_values:	pointer to return value, modified only if return value is 0.
220  * @sz_min:	minimum number of array elements to read
221  * @sz_max:	maximum number of array elements to read, if zero there is no
222  *		upper limit on the number of elements in the dts entry but only
223  *		sz_min will be read.
224  *
225  * Search for a property in a device node and read 16-bit value(s) from
226  * it. Returns number of elements read on success, -EINVAL if the property
227  * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
228  * if the property data is smaller than sz_min or longer than sz_max.
229  *
230  * dts entry of array should be like:
231  *	property = /bits/ 16 <0x5000 0x6000 0x7000>;
232  *
233  * The out_values is modified only if a valid u16 value can be decoded.
234  */
235 int of_property_read_variable_u16_array(const struct device_node *np,
236 					const char *propname, u16 *out_values,
237 					size_t sz_min, size_t sz_max)
238 {
239 	size_t sz, count;
240 	const __be16 *val = of_find_property_value_of_size(np, propname,
241 						(sz_min * sizeof(*out_values)),
242 						(sz_max * sizeof(*out_values)),
243 						&sz);
244 
245 	if (IS_ERR(val))
246 		return PTR_ERR(val);
247 
248 	if (!sz_max)
249 		sz = sz_min;
250 	else
251 		sz /= sizeof(*out_values);
252 
253 	count = sz;
254 	while (count--)
255 		*out_values++ = be16_to_cpup(val++);
256 
257 	return sz;
258 }
259 EXPORT_SYMBOL_GPL(of_property_read_variable_u16_array);
260 
261 /**
262  * of_property_read_variable_u32_array - Find and read an array of 32 bit
263  * integers from a property, with bounds on the minimum and maximum array size.
264  *
265  * @np:		device node from which the property value is to be read.
266  * @propname:	name of the property to be searched.
267  * @out_values:	pointer to return value, modified only if return value is 0.
268  * @sz_min:	minimum number of array elements to read
269  * @sz_max:	maximum number of array elements to read, if zero there is no
270  *		upper limit on the number of elements in the dts entry but only
271  *		sz_min will be read.
272  *
273  * Search for a property in a device node and read 32-bit value(s) from
274  * it. Returns number of elements read on success, -EINVAL if the property
275  * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
276  * if the property data is smaller than sz_min or longer than sz_max.
277  *
278  * The out_values is modified only if a valid u32 value can be decoded.
279  */
280 int of_property_read_variable_u32_array(const struct device_node *np,
281 			       const char *propname, u32 *out_values,
282 			       size_t sz_min, size_t sz_max)
283 {
284 	size_t sz, count;
285 	const __be32 *val = of_find_property_value_of_size(np, propname,
286 						(sz_min * sizeof(*out_values)),
287 						(sz_max * sizeof(*out_values)),
288 						&sz);
289 
290 	if (IS_ERR(val))
291 		return PTR_ERR(val);
292 
293 	if (!sz_max)
294 		sz = sz_min;
295 	else
296 		sz /= sizeof(*out_values);
297 
298 	count = sz;
299 	while (count--)
300 		*out_values++ = be32_to_cpup(val++);
301 
302 	return sz;
303 }
304 EXPORT_SYMBOL_GPL(of_property_read_variable_u32_array);
305 
306 /**
307  * of_property_read_u64 - Find and read a 64 bit integer from a property
308  * @np:		device node from which the property value is to be read.
309  * @propname:	name of the property to be searched.
310  * @out_value:	pointer to return value, modified only if return value is 0.
311  *
312  * Search for a property in a device node and read a 64-bit value from
313  * it. Returns 0 on success, -EINVAL if the property does not exist,
314  * -ENODATA if property does not have a value, and -EOVERFLOW if the
315  * property data isn't large enough.
316  *
317  * The out_value is modified only if a valid u64 value can be decoded.
318  */
319 int of_property_read_u64(const struct device_node *np, const char *propname,
320 			 u64 *out_value)
321 {
322 	const __be32 *val = of_find_property_value_of_size(np, propname,
323 						sizeof(*out_value),
324 						0,
325 						NULL);
326 
327 	if (IS_ERR(val))
328 		return PTR_ERR(val);
329 
330 	*out_value = of_read_number(val, 2);
331 	return 0;
332 }
333 EXPORT_SYMBOL_GPL(of_property_read_u64);
334 
335 /**
336  * of_property_read_variable_u64_array - Find and read an array of 64 bit
337  * integers from a property, with bounds on the minimum and maximum array size.
338  *
339  * @np:		device node from which the property value is to be read.
340  * @propname:	name of the property to be searched.
341  * @out_values:	pointer to return value, modified only if return value is 0.
342  * @sz_min:	minimum number of array elements to read
343  * @sz_max:	maximum number of array elements to read, if zero there is no
344  *		upper limit on the number of elements in the dts entry but only
345  *		sz_min will be read.
346  *
347  * Search for a property in a device node and read 64-bit value(s) from
348  * it. Returns number of elements read on success, -EINVAL if the property
349  * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
350  * if the property data is smaller than sz_min or longer than sz_max.
351  *
352  * The out_values is modified only if a valid u64 value can be decoded.
353  */
354 int of_property_read_variable_u64_array(const struct device_node *np,
355 			       const char *propname, u64 *out_values,
356 			       size_t sz_min, size_t sz_max)
357 {
358 	size_t sz, count;
359 	const __be32 *val = of_find_property_value_of_size(np, propname,
360 						(sz_min * sizeof(*out_values)),
361 						(sz_max * sizeof(*out_values)),
362 						&sz);
363 
364 	if (IS_ERR(val))
365 		return PTR_ERR(val);
366 
367 	if (!sz_max)
368 		sz = sz_min;
369 	else
370 		sz /= sizeof(*out_values);
371 
372 	count = sz;
373 	while (count--) {
374 		*out_values++ = of_read_number(val, 2);
375 		val += 2;
376 	}
377 
378 	return sz;
379 }
380 EXPORT_SYMBOL_GPL(of_property_read_variable_u64_array);
381 
382 /**
383  * of_property_read_string - Find and read a string from a property
384  * @np:		device node from which the property value is to be read.
385  * @propname:	name of the property to be searched.
386  * @out_string:	pointer to null terminated return string, modified only if
387  *		return value is 0.
388  *
389  * Search for a property in a device tree node and retrieve a null
390  * terminated string value (pointer to data, not a copy). Returns 0 on
391  * success, -EINVAL if the property does not exist, -ENODATA if property
392  * does not have a value, and -EILSEQ if the string is not null-terminated
393  * within the length of the property data.
394  *
395  * The out_string pointer is modified only if a valid string can be decoded.
396  */
397 int of_property_read_string(const struct device_node *np, const char *propname,
398 				const char **out_string)
399 {
400 	const struct property *prop = of_find_property(np, propname, NULL);
401 	if (!prop)
402 		return -EINVAL;
403 	if (!prop->value)
404 		return -ENODATA;
405 	if (strnlen(prop->value, prop->length) >= prop->length)
406 		return -EILSEQ;
407 	*out_string = prop->value;
408 	return 0;
409 }
410 EXPORT_SYMBOL_GPL(of_property_read_string);
411 
412 /**
413  * of_property_match_string() - Find string in a list and return index
414  * @np: pointer to node containing string list property
415  * @propname: string list property name
416  * @string: pointer to string to search for in string list
417  *
418  * This function searches a string list property and returns the index
419  * of a specific string value.
420  */
421 int of_property_match_string(const struct device_node *np, const char *propname,
422 			     const char *string)
423 {
424 	const struct property *prop = of_find_property(np, propname, NULL);
425 	size_t l;
426 	int i;
427 	const char *p, *end;
428 
429 	if (!prop)
430 		return -EINVAL;
431 	if (!prop->value)
432 		return -ENODATA;
433 
434 	p = prop->value;
435 	end = p + prop->length;
436 
437 	for (i = 0; p < end; i++, p += l) {
438 		l = strnlen(p, end - p) + 1;
439 		if (p + l > end)
440 			return -EILSEQ;
441 		pr_debug("comparing %s with %s\n", string, p);
442 		if (strcmp(string, p) == 0)
443 			return i; /* Found it; return index */
444 	}
445 	return -ENODATA;
446 }
447 EXPORT_SYMBOL_GPL(of_property_match_string);
448 
449 /**
450  * of_property_read_string_helper() - Utility helper for parsing string properties
451  * @np:		device node from which the property value is to be read.
452  * @propname:	name of the property to be searched.
453  * @out_strs:	output array of string pointers.
454  * @sz:		number of array elements to read.
455  * @skip:	Number of strings to skip over at beginning of list.
456  *
457  * Don't call this function directly. It is a utility helper for the
458  * of_property_read_string*() family of functions.
459  */
460 int of_property_read_string_helper(const struct device_node *np,
461 				   const char *propname, const char **out_strs,
462 				   size_t sz, int skip)
463 {
464 	const struct property *prop = of_find_property(np, propname, NULL);
465 	int l = 0, i = 0;
466 	const char *p, *end;
467 
468 	if (!prop)
469 		return -EINVAL;
470 	if (!prop->value)
471 		return -ENODATA;
472 	p = prop->value;
473 	end = p + prop->length;
474 
475 	for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
476 		l = strnlen(p, end - p) + 1;
477 		if (p + l > end)
478 			return -EILSEQ;
479 		if (out_strs && i >= skip)
480 			*out_strs++ = p;
481 	}
482 	i -= skip;
483 	return i <= 0 ? -ENODATA : i;
484 }
485 EXPORT_SYMBOL_GPL(of_property_read_string_helper);
486 
487 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
488 			       u32 *pu)
489 {
490 	const void *curv = cur;
491 
492 	if (!prop)
493 		return NULL;
494 
495 	if (!cur) {
496 		curv = prop->value;
497 		goto out_val;
498 	}
499 
500 	curv += sizeof(*cur);
501 	if (curv >= prop->value + prop->length)
502 		return NULL;
503 
504 out_val:
505 	*pu = be32_to_cpup(curv);
506 	return curv;
507 }
508 EXPORT_SYMBOL_GPL(of_prop_next_u32);
509 
510 const char *of_prop_next_string(struct property *prop, const char *cur)
511 {
512 	const void *curv = cur;
513 
514 	if (!prop)
515 		return NULL;
516 
517 	if (!cur)
518 		return prop->value;
519 
520 	curv += strlen(cur) + 1;
521 	if (curv >= prop->value + prop->length)
522 		return NULL;
523 
524 	return curv;
525 }
526 EXPORT_SYMBOL_GPL(of_prop_next_string);
527 
528 /**
529  * of_graph_parse_endpoint() - parse common endpoint node properties
530  * @node: pointer to endpoint device_node
531  * @endpoint: pointer to the OF endpoint data structure
532  *
533  * The caller should hold a reference to @node.
534  */
535 int of_graph_parse_endpoint(const struct device_node *node,
536 			    struct of_endpoint *endpoint)
537 {
538 	struct device_node *port_node = of_get_parent(node);
539 
540 	WARN_ONCE(!port_node, "%s(): endpoint %pOF has no parent node\n",
541 		  __func__, node);
542 
543 	memset(endpoint, 0, sizeof(*endpoint));
544 
545 	endpoint->local_node = node;
546 	/*
547 	 * It doesn't matter whether the two calls below succeed.
548 	 * If they don't then the default value 0 is used.
549 	 */
550 	of_property_read_u32(port_node, "reg", &endpoint->port);
551 	of_property_read_u32(node, "reg", &endpoint->id);
552 
553 	of_node_put(port_node);
554 
555 	return 0;
556 }
557 EXPORT_SYMBOL(of_graph_parse_endpoint);
558 
559 /**
560  * of_graph_get_port_by_id() - get the port matching a given id
561  * @parent: pointer to the parent device node
562  * @id: id of the port
563  *
564  * Return: A 'port' node pointer with refcount incremented. The caller
565  * has to use of_node_put() on it when done.
566  */
567 struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id)
568 {
569 	struct device_node *node, *port;
570 
571 	node = of_get_child_by_name(parent, "ports");
572 	if (node)
573 		parent = node;
574 
575 	for_each_child_of_node(parent, port) {
576 		u32 port_id = 0;
577 
578 		if (of_node_cmp(port->name, "port") != 0)
579 			continue;
580 		of_property_read_u32(port, "reg", &port_id);
581 		if (id == port_id)
582 			break;
583 	}
584 
585 	of_node_put(node);
586 
587 	return port;
588 }
589 EXPORT_SYMBOL(of_graph_get_port_by_id);
590 
591 /**
592  * of_graph_get_next_endpoint() - get next endpoint node
593  * @parent: pointer to the parent device node
594  * @prev: previous endpoint node, or NULL to get first
595  *
596  * Return: An 'endpoint' node pointer with refcount incremented. Refcount
597  * of the passed @prev node is decremented.
598  */
599 struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
600 					struct device_node *prev)
601 {
602 	struct device_node *endpoint;
603 	struct device_node *port;
604 
605 	if (!parent)
606 		return NULL;
607 
608 	/*
609 	 * Start by locating the port node. If no previous endpoint is specified
610 	 * search for the first port node, otherwise get the previous endpoint
611 	 * parent port node.
612 	 */
613 	if (!prev) {
614 		struct device_node *node;
615 
616 		node = of_get_child_by_name(parent, "ports");
617 		if (node)
618 			parent = node;
619 
620 		port = of_get_child_by_name(parent, "port");
621 		of_node_put(node);
622 
623 		if (!port) {
624 			pr_err("graph: no port node found in %pOF\n", parent);
625 			return NULL;
626 		}
627 	} else {
628 		port = of_get_parent(prev);
629 		if (WARN_ONCE(!port, "%s(): endpoint %pOF has no parent node\n",
630 			      __func__, prev))
631 			return NULL;
632 	}
633 
634 	while (1) {
635 		/*
636 		 * Now that we have a port node, get the next endpoint by
637 		 * getting the next child. If the previous endpoint is NULL this
638 		 * will return the first child.
639 		 */
640 		endpoint = of_get_next_child(port, prev);
641 		if (endpoint) {
642 			of_node_put(port);
643 			return endpoint;
644 		}
645 
646 		/* No more endpoints under this port, try the next one. */
647 		prev = NULL;
648 
649 		do {
650 			port = of_get_next_child(parent, port);
651 			if (!port)
652 				return NULL;
653 		} while (of_node_cmp(port->name, "port"));
654 	}
655 }
656 EXPORT_SYMBOL(of_graph_get_next_endpoint);
657 
658 /**
659  * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers
660  * @parent: pointer to the parent device node
661  * @port_reg: identifier (value of reg property) of the parent port node
662  * @reg: identifier (value of reg property) of the endpoint node
663  *
664  * Return: An 'endpoint' node pointer which is identified by reg and at the same
665  * is the child of a port node identified by port_reg. reg and port_reg are
666  * ignored when they are -1.
667  */
668 struct device_node *of_graph_get_endpoint_by_regs(
669 	const struct device_node *parent, int port_reg, int reg)
670 {
671 	struct of_endpoint endpoint;
672 	struct device_node *node = NULL;
673 
674 	for_each_endpoint_of_node(parent, node) {
675 		of_graph_parse_endpoint(node, &endpoint);
676 		if (((port_reg == -1) || (endpoint.port == port_reg)) &&
677 			((reg == -1) || (endpoint.id == reg)))
678 			return node;
679 	}
680 
681 	return NULL;
682 }
683 EXPORT_SYMBOL(of_graph_get_endpoint_by_regs);
684 
685 /**
686  * of_graph_get_remote_endpoint() - get remote endpoint node
687  * @node: pointer to a local endpoint device_node
688  *
689  * Return: Remote endpoint node associated with remote endpoint node linked
690  *	   to @node. Use of_node_put() on it when done.
691  */
692 struct device_node *of_graph_get_remote_endpoint(const struct device_node *node)
693 {
694 	/* Get remote endpoint node. */
695 	return of_parse_phandle(node, "remote-endpoint", 0);
696 }
697 EXPORT_SYMBOL(of_graph_get_remote_endpoint);
698 
699 /**
700  * of_graph_get_port_parent() - get port's parent node
701  * @node: pointer to a local endpoint device_node
702  *
703  * Return: device node associated with endpoint node linked
704  *	   to @node. Use of_node_put() on it when done.
705  */
706 struct device_node *of_graph_get_port_parent(struct device_node *node)
707 {
708 	unsigned int depth;
709 
710 	if (!node)
711 		return NULL;
712 
713 	/*
714 	 * Preserve usecount for passed in node as of_get_next_parent()
715 	 * will do of_node_put() on it.
716 	 */
717 	of_node_get(node);
718 
719 	/* Walk 3 levels up only if there is 'ports' node. */
720 	for (depth = 3; depth && node; depth--) {
721 		node = of_get_next_parent(node);
722 		if (depth == 2 && of_node_cmp(node->name, "ports"))
723 			break;
724 	}
725 	return node;
726 }
727 EXPORT_SYMBOL(of_graph_get_port_parent);
728 
729 /**
730  * of_graph_get_remote_port_parent() - get remote port's parent node
731  * @node: pointer to a local endpoint device_node
732  *
733  * Return: Remote device node associated with remote endpoint node linked
734  *	   to @node. Use of_node_put() on it when done.
735  */
736 struct device_node *of_graph_get_remote_port_parent(
737 			       const struct device_node *node)
738 {
739 	struct device_node *np, *pp;
740 
741 	/* Get remote endpoint node. */
742 	np = of_graph_get_remote_endpoint(node);
743 
744 	pp = of_graph_get_port_parent(np);
745 
746 	of_node_put(np);
747 
748 	return pp;
749 }
750 EXPORT_SYMBOL(of_graph_get_remote_port_parent);
751 
752 /**
753  * of_graph_get_remote_port() - get remote port node
754  * @node: pointer to a local endpoint device_node
755  *
756  * Return: Remote port node associated with remote endpoint node linked
757  *	   to @node. Use of_node_put() on it when done.
758  */
759 struct device_node *of_graph_get_remote_port(const struct device_node *node)
760 {
761 	struct device_node *np;
762 
763 	/* Get remote endpoint node. */
764 	np = of_graph_get_remote_endpoint(node);
765 	if (!np)
766 		return NULL;
767 	return of_get_next_parent(np);
768 }
769 EXPORT_SYMBOL(of_graph_get_remote_port);
770 
771 int of_graph_get_endpoint_count(const struct device_node *np)
772 {
773 	struct device_node *endpoint;
774 	int num = 0;
775 
776 	for_each_endpoint_of_node(np, endpoint)
777 		num++;
778 
779 	return num;
780 }
781 EXPORT_SYMBOL(of_graph_get_endpoint_count);
782 
783 /**
784  * of_graph_get_remote_node() - get remote parent device_node for given port/endpoint
785  * @node: pointer to parent device_node containing graph port/endpoint
786  * @port: identifier (value of reg property) of the parent port node
787  * @endpoint: identifier (value of reg property) of the endpoint node
788  *
789  * Return: Remote device node associated with remote endpoint node linked
790  *	   to @node. Use of_node_put() on it when done.
791  */
792 struct device_node *of_graph_get_remote_node(const struct device_node *node,
793 					     u32 port, u32 endpoint)
794 {
795 	struct device_node *endpoint_node, *remote;
796 
797 	endpoint_node = of_graph_get_endpoint_by_regs(node, port, endpoint);
798 	if (!endpoint_node) {
799 		pr_debug("no valid endpoint (%d, %d) for node %pOF\n",
800 			 port, endpoint, node);
801 		return NULL;
802 	}
803 
804 	remote = of_graph_get_remote_port_parent(endpoint_node);
805 	of_node_put(endpoint_node);
806 	if (!remote) {
807 		pr_debug("no valid remote node\n");
808 		return NULL;
809 	}
810 
811 	if (!of_device_is_available(remote)) {
812 		pr_debug("not available for remote node\n");
813 		return NULL;
814 	}
815 
816 	return remote;
817 }
818 EXPORT_SYMBOL(of_graph_get_remote_node);
819 
820 static void of_fwnode_get(struct fwnode_handle *fwnode)
821 {
822 	of_node_get(to_of_node(fwnode));
823 }
824 
825 static void of_fwnode_put(struct fwnode_handle *fwnode)
826 {
827 	of_node_put(to_of_node(fwnode));
828 }
829 
830 static bool of_fwnode_device_is_available(const struct fwnode_handle *fwnode)
831 {
832 	return of_device_is_available(to_of_node(fwnode));
833 }
834 
835 static bool of_fwnode_property_present(const struct fwnode_handle *fwnode,
836 				       const char *propname)
837 {
838 	return of_property_read_bool(to_of_node(fwnode), propname);
839 }
840 
841 static int of_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
842 					     const char *propname,
843 					     unsigned int elem_size, void *val,
844 					     size_t nval)
845 {
846 	const struct device_node *node = to_of_node(fwnode);
847 
848 	if (!val)
849 		return of_property_count_elems_of_size(node, propname,
850 						       elem_size);
851 
852 	switch (elem_size) {
853 	case sizeof(u8):
854 		return of_property_read_u8_array(node, propname, val, nval);
855 	case sizeof(u16):
856 		return of_property_read_u16_array(node, propname, val, nval);
857 	case sizeof(u32):
858 		return of_property_read_u32_array(node, propname, val, nval);
859 	case sizeof(u64):
860 		return of_property_read_u64_array(node, propname, val, nval);
861 	}
862 
863 	return -ENXIO;
864 }
865 
866 static int
867 of_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
868 				     const char *propname, const char **val,
869 				     size_t nval)
870 {
871 	const struct device_node *node = to_of_node(fwnode);
872 
873 	return val ?
874 		of_property_read_string_array(node, propname, val, nval) :
875 		of_property_count_strings(node, propname);
876 }
877 
878 static struct fwnode_handle *
879 of_fwnode_get_parent(const struct fwnode_handle *fwnode)
880 {
881 	return of_fwnode_handle(of_get_parent(to_of_node(fwnode)));
882 }
883 
884 static struct fwnode_handle *
885 of_fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
886 			      struct fwnode_handle *child)
887 {
888 	return of_fwnode_handle(of_get_next_available_child(to_of_node(fwnode),
889 							    to_of_node(child)));
890 }
891 
892 static struct fwnode_handle *
893 of_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
894 			       const char *childname)
895 {
896 	const struct device_node *node = to_of_node(fwnode);
897 	struct device_node *child;
898 
899 	for_each_available_child_of_node(node, child)
900 		if (!of_node_cmp(child->name, childname))
901 			return of_fwnode_handle(child);
902 
903 	return NULL;
904 }
905 
906 static int
907 of_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
908 			     const char *prop, const char *nargs_prop,
909 			     unsigned int nargs, unsigned int index,
910 			     struct fwnode_reference_args *args)
911 {
912 	struct of_phandle_args of_args;
913 	unsigned int i;
914 	int ret;
915 
916 	if (nargs_prop)
917 		ret = of_parse_phandle_with_args(to_of_node(fwnode), prop,
918 						 nargs_prop, index, &of_args);
919 	else
920 		ret = of_parse_phandle_with_fixed_args(to_of_node(fwnode), prop,
921 						       nargs, index, &of_args);
922 	if (ret < 0)
923 		return ret;
924 	if (!args)
925 		return 0;
926 
927 	args->nargs = of_args.args_count;
928 	args->fwnode = of_fwnode_handle(of_args.np);
929 
930 	for (i = 0; i < NR_FWNODE_REFERENCE_ARGS; i++)
931 		args->args[i] = i < of_args.args_count ? of_args.args[i] : 0;
932 
933 	return 0;
934 }
935 
936 static struct fwnode_handle *
937 of_fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
938 				  struct fwnode_handle *prev)
939 {
940 	return of_fwnode_handle(of_graph_get_next_endpoint(to_of_node(fwnode),
941 							   to_of_node(prev)));
942 }
943 
944 static struct fwnode_handle *
945 of_fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
946 {
947 	return of_fwnode_handle(
948 		of_graph_get_remote_endpoint(to_of_node(fwnode)));
949 }
950 
951 static struct fwnode_handle *
952 of_fwnode_graph_get_port_parent(struct fwnode_handle *fwnode)
953 {
954 	struct device_node *np;
955 
956 	/* Get the parent of the port */
957 	np = of_get_next_parent(to_of_node(fwnode));
958 	if (!np)
959 		return NULL;
960 
961 	/* Is this the "ports" node? If not, it's the port parent. */
962 	if (of_node_cmp(np->name, "ports"))
963 		return of_fwnode_handle(np);
964 
965 	return of_fwnode_handle(of_get_next_parent(np));
966 }
967 
968 static int of_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
969 					  struct fwnode_endpoint *endpoint)
970 {
971 	const struct device_node *node = to_of_node(fwnode);
972 	struct device_node *port_node = of_get_parent(node);
973 
974 	endpoint->local_fwnode = fwnode;
975 
976 	of_property_read_u32(port_node, "reg", &endpoint->port);
977 	of_property_read_u32(node, "reg", &endpoint->id);
978 
979 	of_node_put(port_node);
980 
981 	return 0;
982 }
983 
984 const struct fwnode_operations of_fwnode_ops = {
985 	.get = of_fwnode_get,
986 	.put = of_fwnode_put,
987 	.device_is_available = of_fwnode_device_is_available,
988 	.property_present = of_fwnode_property_present,
989 	.property_read_int_array = of_fwnode_property_read_int_array,
990 	.property_read_string_array = of_fwnode_property_read_string_array,
991 	.get_parent = of_fwnode_get_parent,
992 	.get_next_child_node = of_fwnode_get_next_child_node,
993 	.get_named_child_node = of_fwnode_get_named_child_node,
994 	.get_reference_args = of_fwnode_get_reference_args,
995 	.graph_get_next_endpoint = of_fwnode_graph_get_next_endpoint,
996 	.graph_get_remote_endpoint = of_fwnode_graph_get_remote_endpoint,
997 	.graph_get_port_parent = of_fwnode_graph_get_port_parent,
998 	.graph_parse_endpoint = of_fwnode_graph_parse_endpoint,
999 };
1000 EXPORT_SYMBOL_GPL(of_fwnode_ops);
1001