1 // SPDX-License-Identifier: GPL-2.0+
3 * drivers/of/property.c - Procedures for accessing and interpreting
4 * Devicetree properties and graphs.
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
10 * Paul Mackerras August 1996.
11 * Copyright (C) 1996-2005 Paul Mackerras.
13 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
14 * {engebret|bergner}@us.ibm.com
16 * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
18 * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
22 #define pr_fmt(fmt) "OF: " fmt
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>
32 #include "of_private.h"
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.
39 * Search for a boolean property in a device node. Usage on non-boolean
40 * property types is deprecated.
42 * Return: true if the property exists false otherwise.
44 bool of_property_read_bool(const struct device_node
*np
, const char *propname
)
46 struct property
*prop
= of_find_property(np
, propname
, NULL
);
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.
53 if (prop
&& prop
->length
)
54 pr_warn("%pOF: Read of boolean property '%s' with a value.\n", np
, propname
);
56 return prop
? true : false;
58 EXPORT_SYMBOL(of_property_read_bool
);
61 * of_graph_is_present() - check graph's presence
62 * @node: pointer to device_node containing graph port
64 * Return: True if @node has a port or ports (with a port) sub-node,
67 bool of_graph_is_present(const struct device_node
*node
)
69 struct device_node
*ports
__free(device_node
) = of_get_child_by_name(node
, "ports");
74 struct device_node
*port
__free(device_node
) = of_get_child_by_name(node
, "port");
78 EXPORT_SYMBOL(of_graph_is_present
);
81 * of_property_count_elems_of_size - Count the number of elements in a property
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
87 * Search for a property in a device node and count the number of elements of
88 * size elem_size in it.
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.
94 int of_property_count_elems_of_size(const struct device_node
*np
,
95 const char *propname
, int elem_size
)
97 const struct property
*prop
= of_find_property(np
, propname
, NULL
);
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
);
110 return prop
->length
/ elem_size
;
112 EXPORT_SYMBOL_GPL(of_property_count_elems_of_size
);
115 * of_find_property_value_of_size
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
123 * Search for a property in a device node and valid the requested size.
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.
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
)
133 const struct property
*prop
= of_find_property(np
, propname
, NULL
);
136 return ERR_PTR(-EINVAL
);
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
);
151 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
153 * @np: device node from which the property value is to be read.
154 * @propname: name of the property to be searched.
155 * @index: index of the u32 in the list of values
156 * @out_value: pointer to return value, modified only if no error.
158 * Search for a property in a device node and read nth 32-bit value from
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.
165 * The out_value is modified only if a valid u32 value can be decoded.
167 int of_property_read_u32_index(const struct device_node
*np
,
168 const char *propname
,
169 u32 index
, u32
*out_value
)
171 const u32
*val
= of_find_property_value_of_size(np
, propname
,
172 ((index
+ 1) * sizeof(*out_value
)),
179 *out_value
= be32_to_cpup(((__be32
*)val
) + index
);
182 EXPORT_SYMBOL_GPL(of_property_read_u32_index
);
185 * of_property_read_u64_index - Find and read a u64 from a multi-value property.
187 * @np: device node from which the property value is to be read.
188 * @propname: name of the property to be searched.
189 * @index: index of the u64 in the list of values
190 * @out_value: pointer to return value, modified only if no error.
192 * Search for a property in a device node and read nth 64-bit value from
195 * Return: 0 on success, -EINVAL if the property does not exist,
196 * -ENODATA if property does not have a value, and -EOVERFLOW if the
197 * property data isn't large enough.
199 * The out_value is modified only if a valid u64 value can be decoded.
201 int of_property_read_u64_index(const struct device_node
*np
,
202 const char *propname
,
203 u32 index
, u64
*out_value
)
205 const u64
*val
= of_find_property_value_of_size(np
, propname
,
206 ((index
+ 1) * sizeof(*out_value
)),
212 *out_value
= be64_to_cpup(((__be64
*)val
) + index
);
215 EXPORT_SYMBOL_GPL(of_property_read_u64_index
);
218 * of_property_read_variable_u8_array - Find and read an array of u8 from a
219 * property, with bounds on the minimum and maximum array size.
221 * @np: device node from which the property value is to be read.
222 * @propname: name of the property to be searched.
223 * @out_values: pointer to found values.
224 * @sz_min: minimum number of array elements to read
225 * @sz_max: maximum number of array elements to read, if zero there is no
226 * upper limit on the number of elements in the dts entry but only
227 * sz_min will be read.
229 * Search for a property in a device node and read 8-bit value(s) from
232 * dts entry of array should be like:
233 * ``property = /bits/ 8 <0x50 0x60 0x70>;``
235 * Return: The number of elements read on success, -EINVAL if the property
236 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
237 * if the property data is smaller than sz_min or longer than sz_max.
239 * The out_values is modified only if a valid u8 value can be decoded.
241 int of_property_read_variable_u8_array(const struct device_node
*np
,
242 const char *propname
, u8
*out_values
,
243 size_t sz_min
, size_t sz_max
)
246 const u8
*val
= of_find_property_value_of_size(np
, propname
,
247 (sz_min
* sizeof(*out_values
)),
248 (sz_max
* sizeof(*out_values
)),
257 sz
/= sizeof(*out_values
);
261 *out_values
++ = *val
++;
265 EXPORT_SYMBOL_GPL(of_property_read_variable_u8_array
);
268 * of_property_read_variable_u16_array - Find and read an array of u16 from a
269 * property, with bounds on the minimum and maximum array size.
271 * @np: device node from which the property value is to be read.
272 * @propname: name of the property to be searched.
273 * @out_values: pointer to found values.
274 * @sz_min: minimum number of array elements to read
275 * @sz_max: maximum number of array elements to read, if zero there is no
276 * upper limit on the number of elements in the dts entry but only
277 * sz_min will be read.
279 * Search for a property in a device node and read 16-bit value(s) from
282 * dts entry of array should be like:
283 * ``property = /bits/ 16 <0x5000 0x6000 0x7000>;``
285 * Return: The number of elements read on success, -EINVAL if the property
286 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
287 * if the property data is smaller than sz_min or longer than sz_max.
289 * The out_values is modified only if a valid u16 value can be decoded.
291 int of_property_read_variable_u16_array(const struct device_node
*np
,
292 const char *propname
, u16
*out_values
,
293 size_t sz_min
, size_t sz_max
)
296 const __be16
*val
= of_find_property_value_of_size(np
, propname
,
297 (sz_min
* sizeof(*out_values
)),
298 (sz_max
* sizeof(*out_values
)),
307 sz
/= sizeof(*out_values
);
311 *out_values
++ = be16_to_cpup(val
++);
315 EXPORT_SYMBOL_GPL(of_property_read_variable_u16_array
);
318 * of_property_read_variable_u32_array - Find and read an array of 32 bit
319 * integers from a property, with bounds on the minimum and maximum array size.
321 * @np: device node from which the property value is to be read.
322 * @propname: name of the property to be searched.
323 * @out_values: pointer to return found values.
324 * @sz_min: minimum number of array elements to read
325 * @sz_max: maximum number of array elements to read, if zero there is no
326 * upper limit on the number of elements in the dts entry but only
327 * sz_min will be read.
329 * Search for a property in a device node and read 32-bit value(s) from
332 * Return: The number of elements read on success, -EINVAL if the property
333 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
334 * if the property data is smaller than sz_min or longer than sz_max.
336 * The out_values is modified only if a valid u32 value can be decoded.
338 int of_property_read_variable_u32_array(const struct device_node
*np
,
339 const char *propname
, u32
*out_values
,
340 size_t sz_min
, size_t sz_max
)
343 const __be32
*val
= of_find_property_value_of_size(np
, propname
,
344 (sz_min
* sizeof(*out_values
)),
345 (sz_max
* sizeof(*out_values
)),
354 sz
/= sizeof(*out_values
);
358 *out_values
++ = be32_to_cpup(val
++);
362 EXPORT_SYMBOL_GPL(of_property_read_variable_u32_array
);
365 * of_property_read_u64 - Find and read a 64 bit integer from a property
366 * @np: device node from which the property value is to be read.
367 * @propname: name of the property to be searched.
368 * @out_value: pointer to return value, modified only if return value is 0.
370 * Search for a property in a device node and read a 64-bit value from
373 * Return: 0 on success, -EINVAL if the property does not exist,
374 * -ENODATA if property does not have a value, and -EOVERFLOW if the
375 * property data isn't large enough.
377 * The out_value is modified only if a valid u64 value can be decoded.
379 int of_property_read_u64(const struct device_node
*np
, const char *propname
,
382 const __be32
*val
= of_find_property_value_of_size(np
, propname
,
390 *out_value
= of_read_number(val
, 2);
393 EXPORT_SYMBOL_GPL(of_property_read_u64
);
396 * of_property_read_variable_u64_array - Find and read an array of 64 bit
397 * integers from a property, with bounds on the minimum and maximum array size.
399 * @np: device node from which the property value is to be read.
400 * @propname: name of the property to be searched.
401 * @out_values: pointer to found values.
402 * @sz_min: minimum number of array elements to read
403 * @sz_max: maximum number of array elements to read, if zero there is no
404 * upper limit on the number of elements in the dts entry but only
405 * sz_min will be read.
407 * Search for a property in a device node and read 64-bit value(s) from
410 * Return: The number of elements read on success, -EINVAL if the property
411 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
412 * if the property data is smaller than sz_min or longer than sz_max.
414 * The out_values is modified only if a valid u64 value can be decoded.
416 int of_property_read_variable_u64_array(const struct device_node
*np
,
417 const char *propname
, u64
*out_values
,
418 size_t sz_min
, size_t sz_max
)
421 const __be32
*val
= of_find_property_value_of_size(np
, propname
,
422 (sz_min
* sizeof(*out_values
)),
423 (sz_max
* sizeof(*out_values
)),
432 sz
/= sizeof(*out_values
);
436 *out_values
++ = of_read_number(val
, 2);
442 EXPORT_SYMBOL_GPL(of_property_read_variable_u64_array
);
445 * of_property_read_string - Find and read a string from a property
446 * @np: device node from which the property value is to be read.
447 * @propname: name of the property to be searched.
448 * @out_string: pointer to null terminated return string, modified only if
451 * Search for a property in a device tree node and retrieve a null
452 * terminated string value (pointer to data, not a copy).
454 * Return: 0 on success, -EINVAL if the property does not exist, -ENODATA if
455 * property does not have a value, and -EILSEQ if the string is not
456 * null-terminated within the length of the property data.
458 * Note that the empty string "" has length of 1, thus -ENODATA cannot
459 * be interpreted as an empty string.
461 * The out_string pointer is modified only if a valid string can be decoded.
463 int of_property_read_string(const struct device_node
*np
, const char *propname
,
464 const char **out_string
)
466 const struct property
*prop
= of_find_property(np
, propname
, NULL
);
472 if (strnlen(prop
->value
, prop
->length
) >= prop
->length
)
474 *out_string
= prop
->value
;
477 EXPORT_SYMBOL_GPL(of_property_read_string
);
480 * of_property_match_string() - Find string in a list and return index
481 * @np: pointer to the node containing the string list property
482 * @propname: string list property name
483 * @string: pointer to the string to search for in the string list
485 * Search for an exact match of string in a device node property which is a
488 * Return: the index of the first occurrence of the string on success, -EINVAL
489 * if the property does not exist, -ENODATA if the property does not have a
490 * value, and -EILSEQ if the string is not null-terminated within the length of
493 int of_property_match_string(const struct device_node
*np
, const char *propname
,
496 const struct property
*prop
= of_find_property(np
, propname
, NULL
);
507 end
= p
+ prop
->length
;
509 for (i
= 0; p
< end
; i
++, p
+= l
) {
510 l
= strnlen(p
, end
- p
) + 1;
513 pr_debug("comparing %s with %s\n", string
, p
);
514 if (strcmp(string
, p
) == 0)
515 return i
; /* Found it; return index */
519 EXPORT_SYMBOL_GPL(of_property_match_string
);
522 * of_property_read_string_helper() - Utility helper for parsing string properties
523 * @np: device node from which the property value is to be read.
524 * @propname: name of the property to be searched.
525 * @out_strs: output array of string pointers.
526 * @sz: number of array elements to read.
527 * @skip: Number of strings to skip over at beginning of list.
529 * Don't call this function directly. It is a utility helper for the
530 * of_property_read_string*() family of functions.
532 int of_property_read_string_helper(const struct device_node
*np
,
533 const char *propname
, const char **out_strs
,
536 const struct property
*prop
= of_find_property(np
, propname
, NULL
);
545 end
= p
+ prop
->length
;
547 for (i
= 0; p
< end
&& (!out_strs
|| i
< skip
+ sz
); i
++, p
+= l
) {
548 l
= strnlen(p
, end
- p
) + 1;
551 if (out_strs
&& i
>= skip
)
555 return i
<= 0 ? -ENODATA
: i
;
557 EXPORT_SYMBOL_GPL(of_property_read_string_helper
);
559 const __be32
*of_prop_next_u32(const struct property
*prop
, const __be32
*cur
,
562 const void *curv
= cur
;
572 curv
+= sizeof(*cur
);
573 if (curv
>= prop
->value
+ prop
->length
)
577 *pu
= be32_to_cpup(curv
);
580 EXPORT_SYMBOL_GPL(of_prop_next_u32
);
582 const char *of_prop_next_string(const struct property
*prop
, const char *cur
)
584 const void *curv
= cur
;
592 curv
+= strlen(cur
) + 1;
593 if (curv
>= prop
->value
+ prop
->length
)
598 EXPORT_SYMBOL_GPL(of_prop_next_string
);
601 * of_graph_parse_endpoint() - parse common endpoint node properties
602 * @node: pointer to endpoint device_node
603 * @endpoint: pointer to the OF endpoint data structure
605 * The caller should hold a reference to @node.
607 int of_graph_parse_endpoint(const struct device_node
*node
,
608 struct of_endpoint
*endpoint
)
610 struct device_node
*port_node
__free(device_node
) =
613 WARN_ONCE(!port_node
, "%s(): endpoint %pOF has no parent node\n",
616 memset(endpoint
, 0, sizeof(*endpoint
));
618 endpoint
->local_node
= node
;
620 * It doesn't matter whether the two calls below succeed.
621 * If they don't then the default value 0 is used.
623 of_property_read_u32(port_node
, "reg", &endpoint
->port
);
624 of_property_read_u32(node
, "reg", &endpoint
->id
);
628 EXPORT_SYMBOL(of_graph_parse_endpoint
);
631 * of_graph_get_port_by_id() - get the port matching a given id
632 * @parent: pointer to the parent device node
633 * @id: id of the port
635 * Return: A 'port' node pointer with refcount incremented. The caller
636 * has to use of_node_put() on it when done.
638 struct device_node
*of_graph_get_port_by_id(struct device_node
*parent
, u32 id
)
640 struct device_node
*node
__free(device_node
) = of_get_child_by_name(parent
, "ports");
645 for_each_child_of_node_scoped(parent
, port
) {
648 if (!of_node_name_eq(port
, "port"))
650 of_property_read_u32(port
, "reg", &port_id
);
657 EXPORT_SYMBOL(of_graph_get_port_by_id
);
660 * of_graph_get_next_port() - get next port node.
661 * @parent: pointer to the parent device node, or parent ports node
662 * @prev: previous port node, or NULL to get first
664 * Parent device node can be used as @parent whether device node has ports node
665 * or not. It will work same as ports@0 node.
667 * Return: A 'port' node pointer with refcount incremented. Refcount
668 * of the passed @prev node is decremented.
670 struct device_node
*of_graph_get_next_port(const struct device_node
*parent
,
671 struct device_node
*prev
)
677 struct device_node
*node
__free(device_node
) =
678 of_get_child_by_name(parent
, "ports");
683 return of_get_child_by_name(parent
, "port");
687 prev
= of_get_next_child(parent
, prev
);
690 } while (!of_node_name_eq(prev
, "port"));
694 EXPORT_SYMBOL(of_graph_get_next_port
);
697 * of_graph_get_next_port_endpoint() - get next endpoint node in port.
698 * If it reached to end of the port, it will return NULL.
699 * @port: pointer to the target port node
700 * @prev: previous endpoint node, or NULL to get first
702 * Return: An 'endpoint' node pointer with refcount incremented. Refcount
703 * of the passed @prev node is decremented.
705 struct device_node
*of_graph_get_next_port_endpoint(const struct device_node
*port
,
706 struct device_node
*prev
)
709 prev
= of_get_next_child(port
, prev
);
712 if (WARN(!of_node_name_eq(prev
, "endpoint"),
713 "non endpoint node is used (%pOF)", prev
))
721 EXPORT_SYMBOL(of_graph_get_next_port_endpoint
);
724 * of_graph_get_next_endpoint() - get next endpoint node
725 * @parent: pointer to the parent device node
726 * @prev: previous endpoint node, or NULL to get first
728 * Return: An 'endpoint' node pointer with refcount incremented. Refcount
729 * of the passed @prev node is decremented.
731 struct device_node
*of_graph_get_next_endpoint(const struct device_node
*parent
,
732 struct device_node
*prev
)
734 struct device_node
*endpoint
;
735 struct device_node
*port
;
741 * Start by locating the port node. If no previous endpoint is specified
742 * search for the first port node, otherwise get the previous endpoint
746 port
= of_graph_get_next_port(parent
, NULL
);
748 pr_debug("graph: no port node found in %pOF\n", parent
);
752 port
= of_get_parent(prev
);
753 if (WARN_ONCE(!port
, "%s(): endpoint %pOF has no parent node\n",
760 * Now that we have a port node, get the next endpoint by
761 * getting the next child. If the previous endpoint is NULL this
762 * will return the first child.
764 endpoint
= of_graph_get_next_port_endpoint(port
, prev
);
770 /* No more endpoints under this port, try the next one. */
773 port
= of_graph_get_next_port(parent
, port
);
778 EXPORT_SYMBOL(of_graph_get_next_endpoint
);
781 * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers
782 * @parent: pointer to the parent device node
783 * @port_reg: identifier (value of reg property) of the parent port node
784 * @reg: identifier (value of reg property) of the endpoint node
786 * Return: An 'endpoint' node pointer which is identified by reg and at the same
787 * is the child of a port node identified by port_reg. reg and port_reg are
788 * ignored when they are -1. Use of_node_put() on the pointer when done.
790 struct device_node
*of_graph_get_endpoint_by_regs(
791 const struct device_node
*parent
, int port_reg
, int reg
)
793 struct of_endpoint endpoint
;
794 struct device_node
*node
= NULL
;
796 for_each_endpoint_of_node(parent
, node
) {
797 of_graph_parse_endpoint(node
, &endpoint
);
798 if (((port_reg
== -1) || (endpoint
.port
== port_reg
)) &&
799 ((reg
== -1) || (endpoint
.id
== reg
)))
805 EXPORT_SYMBOL(of_graph_get_endpoint_by_regs
);
808 * of_graph_get_remote_endpoint() - get remote endpoint node
809 * @node: pointer to a local endpoint device_node
811 * Return: Remote endpoint node associated with remote endpoint node linked
812 * to @node. Use of_node_put() on it when done.
814 struct device_node
*of_graph_get_remote_endpoint(const struct device_node
*node
)
816 /* Get remote endpoint node. */
817 return of_parse_phandle(node
, "remote-endpoint", 0);
819 EXPORT_SYMBOL(of_graph_get_remote_endpoint
);
822 * of_graph_get_port_parent() - get port's parent node
823 * @node: pointer to a local endpoint device_node
825 * Return: device node associated with endpoint node linked
826 * to @node. Use of_node_put() on it when done.
828 struct device_node
*of_graph_get_port_parent(struct device_node
*node
)
836 * Preserve usecount for passed in node as of_get_next_parent()
837 * will do of_node_put() on it.
841 /* Walk 3 levels up only if there is 'ports' node. */
842 for (depth
= 3; depth
&& node
; depth
--) {
843 node
= of_get_next_parent(node
);
844 if (depth
== 2 && !of_node_name_eq(node
, "ports") &&
845 !of_node_name_eq(node
, "in-ports") &&
846 !of_node_name_eq(node
, "out-ports"))
851 EXPORT_SYMBOL(of_graph_get_port_parent
);
854 * of_graph_get_remote_port_parent() - get remote port's parent node
855 * @node: pointer to a local endpoint device_node
857 * Return: Remote device node associated with remote endpoint node linked
858 * to @node. Use of_node_put() on it when done.
860 struct device_node
*of_graph_get_remote_port_parent(
861 const struct device_node
*node
)
863 /* Get remote endpoint node. */
864 struct device_node
*np
__free(device_node
) =
865 of_graph_get_remote_endpoint(node
);
867 return of_graph_get_port_parent(np
);
869 EXPORT_SYMBOL(of_graph_get_remote_port_parent
);
872 * of_graph_get_remote_port() - get remote port node
873 * @node: pointer to a local endpoint device_node
875 * Return: Remote port node associated with remote endpoint node linked
876 * to @node. Use of_node_put() on it when done.
878 struct device_node
*of_graph_get_remote_port(const struct device_node
*node
)
880 struct device_node
*np
;
882 /* Get remote endpoint node. */
883 np
= of_graph_get_remote_endpoint(node
);
886 return of_get_next_parent(np
);
888 EXPORT_SYMBOL(of_graph_get_remote_port
);
891 * of_graph_get_endpoint_count() - get the number of endpoints in a device node
892 * @np: parent device node containing ports and endpoints
894 * Return: count of endpoint of this device node
896 unsigned int of_graph_get_endpoint_count(const struct device_node
*np
)
898 struct device_node
*endpoint
;
899 unsigned int num
= 0;
901 for_each_endpoint_of_node(np
, endpoint
)
906 EXPORT_SYMBOL(of_graph_get_endpoint_count
);
909 * of_graph_get_port_count() - get the number of port in a device or ports node
910 * @np: pointer to the device or ports node
912 * Return: count of port of this device or ports node
914 unsigned int of_graph_get_port_count(struct device_node
*np
)
916 unsigned int num
= 0;
918 for_each_of_graph_port(np
, port
)
923 EXPORT_SYMBOL(of_graph_get_port_count
);
926 * of_graph_get_remote_node() - get remote parent device_node for given port/endpoint
927 * @node: pointer to parent device_node containing graph port/endpoint
928 * @port: identifier (value of reg property) of the parent port node
929 * @endpoint: identifier (value of reg property) of the endpoint node
931 * Return: Remote device node associated with remote endpoint node linked
932 * to @node. Use of_node_put() on it when done.
934 struct device_node
*of_graph_get_remote_node(const struct device_node
*node
,
935 u32 port
, u32 endpoint
)
937 struct device_node
*endpoint_node
, *remote
;
939 endpoint_node
= of_graph_get_endpoint_by_regs(node
, port
, endpoint
);
940 if (!endpoint_node
) {
941 pr_debug("no valid endpoint (%d, %d) for node %pOF\n",
942 port
, endpoint
, node
);
946 remote
= of_graph_get_remote_port_parent(endpoint_node
);
947 of_node_put(endpoint_node
);
949 pr_debug("no valid remote node\n");
953 if (!of_device_is_available(remote
)) {
954 pr_debug("not available for remote node\n");
961 EXPORT_SYMBOL(of_graph_get_remote_node
);
963 static struct fwnode_handle
*of_fwnode_get(struct fwnode_handle
*fwnode
)
965 return of_fwnode_handle(of_node_get(to_of_node(fwnode
)));
968 static void of_fwnode_put(struct fwnode_handle
*fwnode
)
970 of_node_put(to_of_node(fwnode
));
973 static bool of_fwnode_device_is_available(const struct fwnode_handle
*fwnode
)
975 return of_device_is_available(to_of_node(fwnode
));
978 static bool of_fwnode_device_dma_supported(const struct fwnode_handle
*fwnode
)
983 static enum dev_dma_attr
984 of_fwnode_device_get_dma_attr(const struct fwnode_handle
*fwnode
)
986 if (of_dma_is_coherent(to_of_node(fwnode
)))
987 return DEV_DMA_COHERENT
;
989 return DEV_DMA_NON_COHERENT
;
992 static bool of_fwnode_property_present(const struct fwnode_handle
*fwnode
,
993 const char *propname
)
995 return of_property_present(to_of_node(fwnode
), propname
);
998 static bool of_fwnode_property_read_bool(const struct fwnode_handle
*fwnode
,
999 const char *propname
)
1001 return of_property_read_bool(to_of_node(fwnode
), propname
);
1004 static int of_fwnode_property_read_int_array(const struct fwnode_handle
*fwnode
,
1005 const char *propname
,
1006 unsigned int elem_size
, void *val
,
1009 const struct device_node
*node
= to_of_node(fwnode
);
1012 return of_property_count_elems_of_size(node
, propname
,
1015 switch (elem_size
) {
1017 return of_property_read_u8_array(node
, propname
, val
, nval
);
1019 return of_property_read_u16_array(node
, propname
, val
, nval
);
1021 return of_property_read_u32_array(node
, propname
, val
, nval
);
1023 return of_property_read_u64_array(node
, propname
, val
, nval
);
1030 of_fwnode_property_read_string_array(const struct fwnode_handle
*fwnode
,
1031 const char *propname
, const char **val
,
1034 const struct device_node
*node
= to_of_node(fwnode
);
1037 of_property_read_string_array(node
, propname
, val
, nval
) :
1038 of_property_count_strings(node
, propname
);
1041 static const char *of_fwnode_get_name(const struct fwnode_handle
*fwnode
)
1043 return kbasename(to_of_node(fwnode
)->full_name
);
1046 static const char *of_fwnode_get_name_prefix(const struct fwnode_handle
*fwnode
)
1048 /* Root needs no prefix here (its name is "/"). */
1049 if (!to_of_node(fwnode
)->parent
)
1055 static struct fwnode_handle
*
1056 of_fwnode_get_parent(const struct fwnode_handle
*fwnode
)
1058 return of_fwnode_handle(of_get_parent(to_of_node(fwnode
)));
1061 static struct fwnode_handle
*
1062 of_fwnode_get_next_child_node(const struct fwnode_handle
*fwnode
,
1063 struct fwnode_handle
*child
)
1065 return of_fwnode_handle(of_get_next_available_child(to_of_node(fwnode
),
1066 to_of_node(child
)));
1069 static struct fwnode_handle
*
1070 of_fwnode_get_named_child_node(const struct fwnode_handle
*fwnode
,
1071 const char *childname
)
1073 const struct device_node
*node
= to_of_node(fwnode
);
1074 struct device_node
*child
;
1076 for_each_available_child_of_node(node
, child
)
1077 if (of_node_name_eq(child
, childname
))
1078 return of_fwnode_handle(child
);
1084 of_fwnode_get_reference_args(const struct fwnode_handle
*fwnode
,
1085 const char *prop
, const char *nargs_prop
,
1086 unsigned int nargs
, unsigned int index
,
1087 struct fwnode_reference_args
*args
)
1089 struct of_phandle_args of_args
;
1094 ret
= of_parse_phandle_with_args(to_of_node(fwnode
), prop
,
1095 nargs_prop
, index
, &of_args
);
1097 ret
= of_parse_phandle_with_fixed_args(to_of_node(fwnode
), prop
,
1098 nargs
, index
, &of_args
);
1102 of_node_put(of_args
.np
);
1106 args
->nargs
= of_args
.args_count
;
1107 args
->fwnode
= of_fwnode_handle(of_args
.np
);
1109 for (i
= 0; i
< NR_FWNODE_REFERENCE_ARGS
; i
++)
1110 args
->args
[i
] = i
< of_args
.args_count
? of_args
.args
[i
] : 0;
1115 static struct fwnode_handle
*
1116 of_fwnode_graph_get_next_endpoint(const struct fwnode_handle
*fwnode
,
1117 struct fwnode_handle
*prev
)
1119 return of_fwnode_handle(of_graph_get_next_endpoint(to_of_node(fwnode
),
1123 static struct fwnode_handle
*
1124 of_fwnode_graph_get_remote_endpoint(const struct fwnode_handle
*fwnode
)
1126 return of_fwnode_handle(
1127 of_graph_get_remote_endpoint(to_of_node(fwnode
)));
1130 static struct fwnode_handle
*
1131 of_fwnode_graph_get_port_parent(struct fwnode_handle
*fwnode
)
1133 struct device_node
*np
;
1135 /* Get the parent of the port */
1136 np
= of_get_parent(to_of_node(fwnode
));
1140 /* Is this the "ports" node? If not, it's the port parent. */
1141 if (!of_node_name_eq(np
, "ports"))
1142 return of_fwnode_handle(np
);
1144 return of_fwnode_handle(of_get_next_parent(np
));
1147 static int of_fwnode_graph_parse_endpoint(const struct fwnode_handle
*fwnode
,
1148 struct fwnode_endpoint
*endpoint
)
1150 const struct device_node
*node
= to_of_node(fwnode
);
1151 struct device_node
*port_node
__free(device_node
) = of_get_parent(node
);
1153 endpoint
->local_fwnode
= fwnode
;
1155 of_property_read_u32(port_node
, "reg", &endpoint
->port
);
1156 of_property_read_u32(node
, "reg", &endpoint
->id
);
1162 of_fwnode_device_get_match_data(const struct fwnode_handle
*fwnode
,
1163 const struct device
*dev
)
1165 return of_device_get_match_data(dev
);
1168 static void of_link_to_phandle(struct device_node
*con_np
,
1169 struct device_node
*sup_np
,
1172 struct device_node
*tmp_np
__free(device_node
) = of_node_get(sup_np
);
1174 /* Check that sup_np and its ancestors are available. */
1176 if (of_fwnode_handle(tmp_np
)->dev
)
1179 if (!of_device_is_available(tmp_np
))
1182 tmp_np
= of_get_next_parent(tmp_np
);
1185 fwnode_link_add(of_fwnode_handle(con_np
), of_fwnode_handle(sup_np
), flags
);
1189 * parse_prop_cells - Property parsing function for suppliers
1191 * @np: Pointer to device tree node containing a list
1192 * @prop_name: Name of property to be parsed. Expected to hold phandle values
1193 * @index: For properties holding a list of phandles, this is the index
1195 * @list_name: Property name that is known to contain list of phandle(s) to
1197 * @cells_name: property name that specifies phandles' arguments count
1199 * This is a helper function to parse properties that have a known fixed name
1200 * and are a list of phandles and phandle arguments.
1203 * - phandle node pointer with refcount incremented. Caller must of_node_put()
1205 * - NULL if no phandle found at index
1207 static struct device_node
*parse_prop_cells(struct device_node
*np
,
1208 const char *prop_name
, int index
,
1209 const char *list_name
,
1210 const char *cells_name
)
1212 struct of_phandle_args sup_args
;
1214 if (strcmp(prop_name
, list_name
))
1217 if (__of_parse_phandle_with_args(np
, list_name
, cells_name
, 0, index
,
1224 #define DEFINE_SIMPLE_PROP(fname, name, cells) \
1225 static struct device_node *parse_##fname(struct device_node *np, \
1226 const char *prop_name, int index) \
1228 return parse_prop_cells(np, prop_name, index, name, cells); \
1231 static int strcmp_suffix(const char *str
, const char *suffix
)
1233 unsigned int len
, suffix_len
;
1236 suffix_len
= strlen(suffix
);
1237 if (len
<= suffix_len
)
1239 return strcmp(str
+ len
- suffix_len
, suffix
);
1243 * parse_suffix_prop_cells - Suffix property parsing function for suppliers
1245 * @np: Pointer to device tree node containing a list
1246 * @prop_name: Name of property to be parsed. Expected to hold phandle values
1247 * @index: For properties holding a list of phandles, this is the index
1249 * @suffix: Property suffix that is known to contain list of phandle(s) to
1251 * @cells_name: property name that specifies phandles' arguments count
1253 * This is a helper function to parse properties that have a known fixed suffix
1254 * and are a list of phandles and phandle arguments.
1257 * - phandle node pointer with refcount incremented. Caller must of_node_put()
1259 * - NULL if no phandle found at index
1261 static struct device_node
*parse_suffix_prop_cells(struct device_node
*np
,
1262 const char *prop_name
, int index
,
1264 const char *cells_name
)
1266 struct of_phandle_args sup_args
;
1268 if (strcmp_suffix(prop_name
, suffix
))
1271 if (of_parse_phandle_with_args(np
, prop_name
, cells_name
, index
,
1278 #define DEFINE_SUFFIX_PROP(fname, suffix, cells) \
1279 static struct device_node *parse_##fname(struct device_node *np, \
1280 const char *prop_name, int index) \
1282 return parse_suffix_prop_cells(np, prop_name, index, suffix, cells); \
1286 * struct supplier_bindings - Property parsing functions for suppliers
1288 * @parse_prop: function name
1289 * parse_prop() finds the node corresponding to a supplier phandle
1290 * parse_prop.np: Pointer to device node holding supplier phandle property
1291 * parse_prop.prop_name: Name of property holding a phandle value
1292 * parse_prop.index: For properties holding a list of phandles, this is the
1293 * index into the list
1294 * @get_con_dev: If the consumer node containing the property is never converted
1295 * to a struct device, implement this ops so fw_devlink can use it
1296 * to find the true consumer.
1297 * @optional: Describes whether a supplier is mandatory or not
1298 * @fwlink_flags: Optional fwnode link flags to use when creating a fwnode link
1299 * for this property.
1302 * parse_prop() return values are
1303 * - phandle node pointer with refcount incremented. Caller must of_node_put()
1305 * - NULL if no phandle found at index
1307 struct supplier_bindings
{
1308 struct device_node
*(*parse_prop
)(struct device_node
*np
,
1309 const char *prop_name
, int index
);
1310 struct device_node
*(*get_con_dev
)(struct device_node
*np
);
1315 DEFINE_SIMPLE_PROP(clocks
, "clocks", "#clock-cells")
1316 DEFINE_SIMPLE_PROP(interconnects
, "interconnects", "#interconnect-cells")
1317 DEFINE_SIMPLE_PROP(iommus
, "iommus", "#iommu-cells")
1318 DEFINE_SIMPLE_PROP(mboxes
, "mboxes", "#mbox-cells")
1319 DEFINE_SIMPLE_PROP(io_channels
, "io-channels", "#io-channel-cells")
1320 DEFINE_SIMPLE_PROP(io_backends
, "io-backends", "#io-backend-cells")
1321 DEFINE_SIMPLE_PROP(dmas
, "dmas", "#dma-cells")
1322 DEFINE_SIMPLE_PROP(power_domains
, "power-domains", "#power-domain-cells")
1323 DEFINE_SIMPLE_PROP(hwlocks
, "hwlocks", "#hwlock-cells")
1324 DEFINE_SIMPLE_PROP(extcon
, "extcon", NULL
)
1325 DEFINE_SIMPLE_PROP(nvmem_cells
, "nvmem-cells", "#nvmem-cell-cells")
1326 DEFINE_SIMPLE_PROP(phys
, "phys", "#phy-cells")
1327 DEFINE_SIMPLE_PROP(wakeup_parent
, "wakeup-parent", NULL
)
1328 DEFINE_SIMPLE_PROP(pinctrl0
, "pinctrl-0", NULL
)
1329 DEFINE_SIMPLE_PROP(pinctrl1
, "pinctrl-1", NULL
)
1330 DEFINE_SIMPLE_PROP(pinctrl2
, "pinctrl-2", NULL
)
1331 DEFINE_SIMPLE_PROP(pinctrl3
, "pinctrl-3", NULL
)
1332 DEFINE_SIMPLE_PROP(pinctrl4
, "pinctrl-4", NULL
)
1333 DEFINE_SIMPLE_PROP(pinctrl5
, "pinctrl-5", NULL
)
1334 DEFINE_SIMPLE_PROP(pinctrl6
, "pinctrl-6", NULL
)
1335 DEFINE_SIMPLE_PROP(pinctrl7
, "pinctrl-7", NULL
)
1336 DEFINE_SIMPLE_PROP(pinctrl8
, "pinctrl-8", NULL
)
1337 DEFINE_SIMPLE_PROP(pwms
, "pwms", "#pwm-cells")
1338 DEFINE_SIMPLE_PROP(resets
, "resets", "#reset-cells")
1339 DEFINE_SIMPLE_PROP(leds
, "leds", NULL
)
1340 DEFINE_SIMPLE_PROP(backlight
, "backlight", NULL
)
1341 DEFINE_SIMPLE_PROP(panel
, "panel", NULL
)
1342 DEFINE_SIMPLE_PROP(msi_parent
, "msi-parent", "#msi-cells")
1343 DEFINE_SIMPLE_PROP(post_init_providers
, "post-init-providers", NULL
)
1344 DEFINE_SIMPLE_PROP(access_controllers
, "access-controllers", "#access-controller-cells")
1345 DEFINE_SIMPLE_PROP(pses
, "pses", "#pse-cells")
1346 DEFINE_SIMPLE_PROP(power_supplies
, "power-supplies", NULL
)
1347 DEFINE_SUFFIX_PROP(regulators
, "-supply", NULL
)
1348 DEFINE_SUFFIX_PROP(gpio
, "-gpio", "#gpio-cells")
1350 static struct device_node
*parse_gpios(struct device_node
*np
,
1351 const char *prop_name
, int index
)
1353 if (!strcmp_suffix(prop_name
, ",nr-gpios"))
1356 return parse_suffix_prop_cells(np
, prop_name
, index
, "-gpios",
1360 static struct device_node
*parse_iommu_maps(struct device_node
*np
,
1361 const char *prop_name
, int index
)
1363 if (strcmp(prop_name
, "iommu-map"))
1366 return of_parse_phandle(np
, prop_name
, (index
* 4) + 1);
1369 static struct device_node
*parse_gpio_compat(struct device_node
*np
,
1370 const char *prop_name
, int index
)
1372 struct of_phandle_args sup_args
;
1374 if (strcmp(prop_name
, "gpio") && strcmp(prop_name
, "gpios"))
1378 * Ignore node with gpio-hog property since its gpios are all provided
1381 if (of_property_read_bool(np
, "gpio-hog"))
1384 if (of_parse_phandle_with_args(np
, prop_name
, "#gpio-cells", index
,
1391 static struct device_node
*parse_interrupts(struct device_node
*np
,
1392 const char *prop_name
, int index
)
1394 struct of_phandle_args sup_args
;
1396 if (!IS_ENABLED(CONFIG_OF_IRQ
) || IS_ENABLED(CONFIG_PPC
))
1399 if (strcmp(prop_name
, "interrupts") &&
1400 strcmp(prop_name
, "interrupts-extended"))
1403 return of_irq_parse_one(np
, index
, &sup_args
) ? NULL
: sup_args
.np
;
1406 static struct device_node
*parse_interrupt_map(struct device_node
*np
,
1407 const char *prop_name
, int index
)
1409 const __be32
*imap
, *imap_end
;
1410 struct of_phandle_args sup_args
;
1411 u32 addrcells
, intcells
;
1414 if (!IS_ENABLED(CONFIG_OF_IRQ
))
1417 if (strcmp(prop_name
, "interrupt-map"))
1420 if (of_property_read_u32(np
, "#interrupt-cells", &intcells
))
1422 addrcells
= of_bus_n_addr_cells(np
);
1424 imap
= of_get_property(np
, "interrupt-map", &imaplen
);
1427 imaplen
/= sizeof(*imap
);
1429 imap_end
= imap
+ imaplen
;
1431 for (int i
= 0; imap
+ addrcells
+ intcells
+ 1 < imap_end
; i
++) {
1432 imap
+= addrcells
+ intcells
;
1434 imap
= of_irq_parse_imap_parent(imap
, imap_end
- imap
, &sup_args
);
1441 of_node_put(sup_args
.np
);
1447 static struct device_node
*parse_remote_endpoint(struct device_node
*np
,
1448 const char *prop_name
,
1451 /* Return NULL for index > 0 to signify end of remote-endpoints. */
1452 if (index
> 0 || strcmp(prop_name
, "remote-endpoint"))
1455 return of_graph_get_remote_port_parent(np
);
1458 static const struct supplier_bindings of_supplier_bindings
[] = {
1459 { .parse_prop
= parse_clocks
, },
1460 { .parse_prop
= parse_interconnects
, },
1461 { .parse_prop
= parse_iommus
, .optional
= true, },
1462 { .parse_prop
= parse_iommu_maps
, .optional
= true, },
1463 { .parse_prop
= parse_mboxes
, },
1464 { .parse_prop
= parse_io_channels
, },
1465 { .parse_prop
= parse_io_backends
, },
1466 { .parse_prop
= parse_dmas
, .optional
= true, },
1467 { .parse_prop
= parse_power_domains
, },
1468 { .parse_prop
= parse_hwlocks
, },
1469 { .parse_prop
= parse_extcon
, },
1470 { .parse_prop
= parse_nvmem_cells
, },
1471 { .parse_prop
= parse_phys
, },
1472 { .parse_prop
= parse_wakeup_parent
, },
1473 { .parse_prop
= parse_pinctrl0
, },
1474 { .parse_prop
= parse_pinctrl1
, },
1475 { .parse_prop
= parse_pinctrl2
, },
1476 { .parse_prop
= parse_pinctrl3
, },
1477 { .parse_prop
= parse_pinctrl4
, },
1478 { .parse_prop
= parse_pinctrl5
, },
1479 { .parse_prop
= parse_pinctrl6
, },
1480 { .parse_prop
= parse_pinctrl7
, },
1481 { .parse_prop
= parse_pinctrl8
, },
1483 .parse_prop
= parse_remote_endpoint
,
1484 .get_con_dev
= of_graph_get_port_parent
,
1486 { .parse_prop
= parse_pwms
, },
1487 { .parse_prop
= parse_resets
, },
1488 { .parse_prop
= parse_leds
, },
1489 { .parse_prop
= parse_backlight
, },
1490 { .parse_prop
= parse_panel
, },
1491 { .parse_prop
= parse_msi_parent
, },
1492 { .parse_prop
= parse_pses
, },
1493 { .parse_prop
= parse_power_supplies
, },
1494 { .parse_prop
= parse_gpio_compat
, },
1495 { .parse_prop
= parse_interrupts
, },
1496 { .parse_prop
= parse_interrupt_map
, },
1497 { .parse_prop
= parse_access_controllers
, },
1498 { .parse_prop
= parse_regulators
, },
1499 { .parse_prop
= parse_gpio
, },
1500 { .parse_prop
= parse_gpios
, },
1502 .parse_prop
= parse_post_init_providers
,
1503 .fwlink_flags
= FWLINK_FLAG_IGNORE
,
1509 * of_link_property - Create device links to suppliers listed in a property
1510 * @con_np: The consumer device tree node which contains the property
1511 * @prop_name: Name of property to be parsed
1513 * This function checks if the property @prop_name that is present in the
1514 * @con_np device tree node is one of the known common device tree bindings
1515 * that list phandles to suppliers. If @prop_name isn't one, this function
1516 * doesn't do anything.
1518 * If @prop_name is one, this function attempts to create fwnode links from the
1519 * consumer device tree node @con_np to all the suppliers device tree nodes
1520 * listed in @prop_name.
1522 * Any failed attempt to create a fwnode link will NOT result in an immediate
1523 * return. of_link_property() must create links to all the available supplier
1524 * device tree nodes even when attempts to create a link to one or more
1527 static int of_link_property(struct device_node
*con_np
, const char *prop_name
)
1529 struct device_node
*phandle
;
1530 const struct supplier_bindings
*s
= of_supplier_bindings
;
1532 bool matched
= false;
1534 /* Do not stop at first failed link, link all available suppliers. */
1535 while (!matched
&& s
->parse_prop
) {
1536 if (s
->optional
&& !fw_devlink_is_strict()) {
1541 while ((phandle
= s
->parse_prop(con_np
, prop_name
, i
))) {
1542 struct device_node
*con_dev_np
__free(device_node
) =
1543 s
->get_con_dev
? s
->get_con_dev(con_np
) : of_node_get(con_np
);
1547 of_link_to_phandle(con_dev_np
, phandle
, s
->fwlink_flags
);
1548 of_node_put(phandle
);
1555 static void __iomem
*of_fwnode_iomap(struct fwnode_handle
*fwnode
, int index
)
1557 #ifdef CONFIG_OF_ADDRESS
1558 return of_iomap(to_of_node(fwnode
), index
);
1564 static int of_fwnode_irq_get(const struct fwnode_handle
*fwnode
,
1567 return of_irq_get(to_of_node(fwnode
), index
);
1570 static int of_fwnode_add_links(struct fwnode_handle
*fwnode
)
1572 const struct property
*p
;
1573 struct device_node
*con_np
= to_of_node(fwnode
);
1575 if (IS_ENABLED(CONFIG_X86
))
1581 for_each_property_of_node(con_np
, p
)
1582 of_link_property(con_np
, p
->name
);
1587 const struct fwnode_operations of_fwnode_ops
= {
1588 .get
= of_fwnode_get
,
1589 .put
= of_fwnode_put
,
1590 .device_is_available
= of_fwnode_device_is_available
,
1591 .device_get_match_data
= of_fwnode_device_get_match_data
,
1592 .device_dma_supported
= of_fwnode_device_dma_supported
,
1593 .device_get_dma_attr
= of_fwnode_device_get_dma_attr
,
1594 .property_present
= of_fwnode_property_present
,
1595 .property_read_bool
= of_fwnode_property_read_bool
,
1596 .property_read_int_array
= of_fwnode_property_read_int_array
,
1597 .property_read_string_array
= of_fwnode_property_read_string_array
,
1598 .get_name
= of_fwnode_get_name
,
1599 .get_name_prefix
= of_fwnode_get_name_prefix
,
1600 .get_parent
= of_fwnode_get_parent
,
1601 .get_next_child_node
= of_fwnode_get_next_child_node
,
1602 .get_named_child_node
= of_fwnode_get_named_child_node
,
1603 .get_reference_args
= of_fwnode_get_reference_args
,
1604 .graph_get_next_endpoint
= of_fwnode_graph_get_next_endpoint
,
1605 .graph_get_remote_endpoint
= of_fwnode_graph_get_remote_endpoint
,
1606 .graph_get_port_parent
= of_fwnode_graph_get_port_parent
,
1607 .graph_parse_endpoint
= of_fwnode_graph_parse_endpoint
,
1608 .iomap
= of_fwnode_iomap
,
1609 .irq_get
= of_fwnode_irq_get
,
1610 .add_links
= of_fwnode_add_links
,
1612 EXPORT_SYMBOL_GPL(of_fwnode_ops
);