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_graph_is_present() - check graph's presence
36 * @node: pointer to device_node containing graph port
38 * Return: True if @node has a port or ports (with a port) sub-node,
41 bool of_graph_is_present(const struct device_node
*node
)
43 struct device_node
*ports
__free(device_node
) = of_get_child_by_name(node
, "ports");
48 struct device_node
*port
__free(device_node
) = of_get_child_by_name(node
, "port");
52 EXPORT_SYMBOL(of_graph_is_present
);
55 * of_property_count_elems_of_size - Count the number of elements in a property
57 * @np: device node from which the property value is to be read.
58 * @propname: name of the property to be searched.
59 * @elem_size: size of the individual element
61 * Search for a property in a device node and count the number of elements of
62 * size elem_size in it.
64 * Return: The number of elements on sucess, -EINVAL if the property does not
65 * exist or its length does not match a multiple of elem_size and -ENODATA if
66 * the property does not have a value.
68 int of_property_count_elems_of_size(const struct device_node
*np
,
69 const char *propname
, int elem_size
)
71 const struct property
*prop
= of_find_property(np
, propname
, NULL
);
78 if (prop
->length
% elem_size
!= 0) {
79 pr_err("size of %s in node %pOF is not a multiple of %d\n",
80 propname
, np
, elem_size
);
84 return prop
->length
/ elem_size
;
86 EXPORT_SYMBOL_GPL(of_property_count_elems_of_size
);
89 * of_find_property_value_of_size
91 * @np: device node from which the property value is to be read.
92 * @propname: name of the property to be searched.
93 * @min: minimum allowed length of property value
94 * @max: maximum allowed length of property value (0 means unlimited)
95 * @len: if !=NULL, actual length is written to here
97 * Search for a property in a device node and valid the requested size.
99 * Return: The property value on success, -EINVAL if the property does not
100 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
101 * property data is too small or too large.
104 static void *of_find_property_value_of_size(const struct device_node
*np
,
105 const char *propname
, u32 min
, u32 max
, size_t *len
)
107 const struct property
*prop
= of_find_property(np
, propname
, NULL
);
110 return ERR_PTR(-EINVAL
);
112 return ERR_PTR(-ENODATA
);
113 if (prop
->length
< min
)
114 return ERR_PTR(-EOVERFLOW
);
115 if (max
&& prop
->length
> max
)
116 return ERR_PTR(-EOVERFLOW
);
125 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
127 * @np: device node from which the property value is to be read.
128 * @propname: name of the property to be searched.
129 * @index: index of the u32 in the list of values
130 * @out_value: pointer to return value, modified only if no error.
132 * Search for a property in a device node and read nth 32-bit value from
135 * Return: 0 on success, -EINVAL if the property does not exist,
136 * -ENODATA if property does not have a value, and -EOVERFLOW if the
137 * property data isn't large enough.
139 * The out_value is modified only if a valid u32 value can be decoded.
141 int of_property_read_u32_index(const struct device_node
*np
,
142 const char *propname
,
143 u32 index
, u32
*out_value
)
145 const u32
*val
= of_find_property_value_of_size(np
, propname
,
146 ((index
+ 1) * sizeof(*out_value
)),
153 *out_value
= be32_to_cpup(((__be32
*)val
) + index
);
156 EXPORT_SYMBOL_GPL(of_property_read_u32_index
);
159 * of_property_read_u64_index - Find and read a u64 from a multi-value property.
161 * @np: device node from which the property value is to be read.
162 * @propname: name of the property to be searched.
163 * @index: index of the u64 in the list of values
164 * @out_value: pointer to return value, modified only if no error.
166 * Search for a property in a device node and read nth 64-bit value from
169 * Return: 0 on success, -EINVAL if the property does not exist,
170 * -ENODATA if property does not have a value, and -EOVERFLOW if the
171 * property data isn't large enough.
173 * The out_value is modified only if a valid u64 value can be decoded.
175 int of_property_read_u64_index(const struct device_node
*np
,
176 const char *propname
,
177 u32 index
, u64
*out_value
)
179 const u64
*val
= of_find_property_value_of_size(np
, propname
,
180 ((index
+ 1) * sizeof(*out_value
)),
186 *out_value
= be64_to_cpup(((__be64
*)val
) + index
);
189 EXPORT_SYMBOL_GPL(of_property_read_u64_index
);
192 * of_property_read_variable_u8_array - Find and read an array of u8 from a
193 * property, with bounds on the minimum and maximum array size.
195 * @np: device node from which the property value is to be read.
196 * @propname: name of the property to be searched.
197 * @out_values: pointer to found values.
198 * @sz_min: minimum number of array elements to read
199 * @sz_max: maximum number of array elements to read, if zero there is no
200 * upper limit on the number of elements in the dts entry but only
201 * sz_min will be read.
203 * Search for a property in a device node and read 8-bit value(s) from
206 * dts entry of array should be like:
207 * ``property = /bits/ 8 <0x50 0x60 0x70>;``
209 * Return: The number of elements read on success, -EINVAL if the property
210 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
211 * if the property data is smaller than sz_min or longer than sz_max.
213 * The out_values is modified only if a valid u8 value can be decoded.
215 int of_property_read_variable_u8_array(const struct device_node
*np
,
216 const char *propname
, u8
*out_values
,
217 size_t sz_min
, size_t sz_max
)
220 const u8
*val
= of_find_property_value_of_size(np
, propname
,
221 (sz_min
* sizeof(*out_values
)),
222 (sz_max
* sizeof(*out_values
)),
231 sz
/= sizeof(*out_values
);
235 *out_values
++ = *val
++;
239 EXPORT_SYMBOL_GPL(of_property_read_variable_u8_array
);
242 * of_property_read_variable_u16_array - Find and read an array of u16 from a
243 * property, with bounds on the minimum and maximum array size.
245 * @np: device node from which the property value is to be read.
246 * @propname: name of the property to be searched.
247 * @out_values: pointer to found values.
248 * @sz_min: minimum number of array elements to read
249 * @sz_max: maximum number of array elements to read, if zero there is no
250 * upper limit on the number of elements in the dts entry but only
251 * sz_min will be read.
253 * Search for a property in a device node and read 16-bit value(s) from
256 * dts entry of array should be like:
257 * ``property = /bits/ 16 <0x5000 0x6000 0x7000>;``
259 * Return: The number of elements read on success, -EINVAL if the property
260 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
261 * if the property data is smaller than sz_min or longer than sz_max.
263 * The out_values is modified only if a valid u16 value can be decoded.
265 int of_property_read_variable_u16_array(const struct device_node
*np
,
266 const char *propname
, u16
*out_values
,
267 size_t sz_min
, size_t sz_max
)
270 const __be16
*val
= of_find_property_value_of_size(np
, propname
,
271 (sz_min
* sizeof(*out_values
)),
272 (sz_max
* sizeof(*out_values
)),
281 sz
/= sizeof(*out_values
);
285 *out_values
++ = be16_to_cpup(val
++);
289 EXPORT_SYMBOL_GPL(of_property_read_variable_u16_array
);
292 * of_property_read_variable_u32_array - Find and read an array of 32 bit
293 * integers from a property, with bounds on the minimum and maximum array size.
295 * @np: device node from which the property value is to be read.
296 * @propname: name of the property to be searched.
297 * @out_values: pointer to return found values.
298 * @sz_min: minimum number of array elements to read
299 * @sz_max: maximum number of array elements to read, if zero there is no
300 * upper limit on the number of elements in the dts entry but only
301 * sz_min will be read.
303 * Search for a property in a device node and read 32-bit value(s) from
306 * Return: The number of elements read on success, -EINVAL if the property
307 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
308 * if the property data is smaller than sz_min or longer than sz_max.
310 * The out_values is modified only if a valid u32 value can be decoded.
312 int of_property_read_variable_u32_array(const struct device_node
*np
,
313 const char *propname
, u32
*out_values
,
314 size_t sz_min
, size_t sz_max
)
317 const __be32
*val
= of_find_property_value_of_size(np
, propname
,
318 (sz_min
* sizeof(*out_values
)),
319 (sz_max
* sizeof(*out_values
)),
328 sz
/= sizeof(*out_values
);
332 *out_values
++ = be32_to_cpup(val
++);
336 EXPORT_SYMBOL_GPL(of_property_read_variable_u32_array
);
339 * of_property_read_u64 - Find and read a 64 bit integer from a property
340 * @np: device node from which the property value is to be read.
341 * @propname: name of the property to be searched.
342 * @out_value: pointer to return value, modified only if return value is 0.
344 * Search for a property in a device node and read a 64-bit value from
347 * Return: 0 on success, -EINVAL if the property does not exist,
348 * -ENODATA if property does not have a value, and -EOVERFLOW if the
349 * property data isn't large enough.
351 * The out_value is modified only if a valid u64 value can be decoded.
353 int of_property_read_u64(const struct device_node
*np
, const char *propname
,
356 const __be32
*val
= of_find_property_value_of_size(np
, propname
,
364 *out_value
= of_read_number(val
, 2);
367 EXPORT_SYMBOL_GPL(of_property_read_u64
);
370 * of_property_read_variable_u64_array - Find and read an array of 64 bit
371 * integers from a property, with bounds on the minimum and maximum array size.
373 * @np: device node from which the property value is to be read.
374 * @propname: name of the property to be searched.
375 * @out_values: pointer to found values.
376 * @sz_min: minimum number of array elements to read
377 * @sz_max: maximum number of array elements to read, if zero there is no
378 * upper limit on the number of elements in the dts entry but only
379 * sz_min will be read.
381 * Search for a property in a device node and read 64-bit value(s) from
384 * Return: The number of elements read on success, -EINVAL if the property
385 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
386 * if the property data is smaller than sz_min or longer than sz_max.
388 * The out_values is modified only if a valid u64 value can be decoded.
390 int of_property_read_variable_u64_array(const struct device_node
*np
,
391 const char *propname
, u64
*out_values
,
392 size_t sz_min
, size_t sz_max
)
395 const __be32
*val
= of_find_property_value_of_size(np
, propname
,
396 (sz_min
* sizeof(*out_values
)),
397 (sz_max
* sizeof(*out_values
)),
406 sz
/= sizeof(*out_values
);
410 *out_values
++ = of_read_number(val
, 2);
416 EXPORT_SYMBOL_GPL(of_property_read_variable_u64_array
);
419 * of_property_read_string - Find and read a string from a property
420 * @np: device node from which the property value is to be read.
421 * @propname: name of the property to be searched.
422 * @out_string: pointer to null terminated return string, modified only if
425 * Search for a property in a device tree node and retrieve a null
426 * terminated string value (pointer to data, not a copy).
428 * Return: 0 on success, -EINVAL if the property does not exist, -ENODATA if
429 * property does not have a value, and -EILSEQ if the string is not
430 * null-terminated within the length of the property data.
432 * Note that the empty string "" has length of 1, thus -ENODATA cannot
433 * be interpreted as an empty string.
435 * The out_string pointer is modified only if a valid string can be decoded.
437 int of_property_read_string(const struct device_node
*np
, const char *propname
,
438 const char **out_string
)
440 const struct property
*prop
= of_find_property(np
, propname
, NULL
);
446 if (strnlen(prop
->value
, prop
->length
) >= prop
->length
)
448 *out_string
= prop
->value
;
451 EXPORT_SYMBOL_GPL(of_property_read_string
);
454 * of_property_match_string() - Find string in a list and return index
455 * @np: pointer to the node containing the string list property
456 * @propname: string list property name
457 * @string: pointer to the string to search for in the string list
459 * Search for an exact match of string in a device node property which is a
462 * Return: the index of the first occurrence of the string on success, -EINVAL
463 * if the property does not exist, -ENODATA if the property does not have a
464 * value, and -EILSEQ if the string is not null-terminated within the length of
467 int of_property_match_string(const struct device_node
*np
, const char *propname
,
470 const struct property
*prop
= of_find_property(np
, propname
, NULL
);
481 end
= p
+ prop
->length
;
483 for (i
= 0; p
< end
; i
++, p
+= l
) {
484 l
= strnlen(p
, end
- p
) + 1;
487 pr_debug("comparing %s with %s\n", string
, p
);
488 if (strcmp(string
, p
) == 0)
489 return i
; /* Found it; return index */
493 EXPORT_SYMBOL_GPL(of_property_match_string
);
496 * of_property_read_string_helper() - Utility helper for parsing string properties
497 * @np: device node from which the property value is to be read.
498 * @propname: name of the property to be searched.
499 * @out_strs: output array of string pointers.
500 * @sz: number of array elements to read.
501 * @skip: Number of strings to skip over at beginning of list.
503 * Don't call this function directly. It is a utility helper for the
504 * of_property_read_string*() family of functions.
506 int of_property_read_string_helper(const struct device_node
*np
,
507 const char *propname
, const char **out_strs
,
510 const struct property
*prop
= of_find_property(np
, propname
, NULL
);
519 end
= p
+ prop
->length
;
521 for (i
= 0; p
< end
&& (!out_strs
|| i
< skip
+ sz
); i
++, p
+= l
) {
522 l
= strnlen(p
, end
- p
) + 1;
525 if (out_strs
&& i
>= skip
)
529 return i
<= 0 ? -ENODATA
: i
;
531 EXPORT_SYMBOL_GPL(of_property_read_string_helper
);
533 const __be32
*of_prop_next_u32(const struct property
*prop
, const __be32
*cur
,
536 const void *curv
= cur
;
546 curv
+= sizeof(*cur
);
547 if (curv
>= prop
->value
+ prop
->length
)
551 *pu
= be32_to_cpup(curv
);
554 EXPORT_SYMBOL_GPL(of_prop_next_u32
);
556 const char *of_prop_next_string(const struct property
*prop
, const char *cur
)
558 const void *curv
= cur
;
566 curv
+= strlen(cur
) + 1;
567 if (curv
>= prop
->value
+ prop
->length
)
572 EXPORT_SYMBOL_GPL(of_prop_next_string
);
575 * of_graph_parse_endpoint() - parse common endpoint node properties
576 * @node: pointer to endpoint device_node
577 * @endpoint: pointer to the OF endpoint data structure
579 * The caller should hold a reference to @node.
581 int of_graph_parse_endpoint(const struct device_node
*node
,
582 struct of_endpoint
*endpoint
)
584 struct device_node
*port_node
__free(device_node
) =
587 WARN_ONCE(!port_node
, "%s(): endpoint %pOF has no parent node\n",
590 memset(endpoint
, 0, sizeof(*endpoint
));
592 endpoint
->local_node
= node
;
594 * It doesn't matter whether the two calls below succeed.
595 * If they don't then the default value 0 is used.
597 of_property_read_u32(port_node
, "reg", &endpoint
->port
);
598 of_property_read_u32(node
, "reg", &endpoint
->id
);
602 EXPORT_SYMBOL(of_graph_parse_endpoint
);
605 * of_graph_get_port_by_id() - get the port matching a given id
606 * @parent: pointer to the parent device node
607 * @id: id of the port
609 * Return: A 'port' node pointer with refcount incremented. The caller
610 * has to use of_node_put() on it when done.
612 struct device_node
*of_graph_get_port_by_id(struct device_node
*parent
, u32 id
)
614 struct device_node
*node
__free(device_node
) = of_get_child_by_name(parent
, "ports");
619 for_each_child_of_node_scoped(parent
, port
) {
622 if (!of_node_name_eq(port
, "port"))
624 of_property_read_u32(port
, "reg", &port_id
);
631 EXPORT_SYMBOL(of_graph_get_port_by_id
);
634 * of_graph_get_next_port() - get next port node.
635 * @parent: pointer to the parent device node, or parent ports node
636 * @prev: previous port node, or NULL to get first
638 * Parent device node can be used as @parent whether device node has ports node
639 * or not. It will work same as ports@0 node.
641 * Return: A 'port' node pointer with refcount incremented. Refcount
642 * of the passed @prev node is decremented.
644 struct device_node
*of_graph_get_next_port(const struct device_node
*parent
,
645 struct device_node
*prev
)
651 struct device_node
*node
__free(device_node
) =
652 of_get_child_by_name(parent
, "ports");
657 return of_get_child_by_name(parent
, "port");
661 prev
= of_get_next_child(parent
, prev
);
664 } while (!of_node_name_eq(prev
, "port"));
668 EXPORT_SYMBOL(of_graph_get_next_port
);
671 * of_graph_get_next_port_endpoint() - get next endpoint node in port.
672 * If it reached to end of the port, it will return NULL.
673 * @port: pointer to the target port node
674 * @prev: previous endpoint node, or NULL to get first
676 * Return: An 'endpoint' node pointer with refcount incremented. Refcount
677 * of the passed @prev node is decremented.
679 struct device_node
*of_graph_get_next_port_endpoint(const struct device_node
*port
,
680 struct device_node
*prev
)
683 prev
= of_get_next_child(port
, prev
);
686 if (WARN(!of_node_name_eq(prev
, "endpoint"),
687 "non endpoint node is used (%pOF)", prev
))
695 EXPORT_SYMBOL(of_graph_get_next_port_endpoint
);
698 * of_graph_get_next_endpoint() - get next endpoint node
699 * @parent: pointer to the parent device 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_endpoint(const struct device_node
*parent
,
706 struct device_node
*prev
)
708 struct device_node
*endpoint
;
709 struct device_node
*port
;
715 * Start by locating the port node. If no previous endpoint is specified
716 * search for the first port node, otherwise get the previous endpoint
720 port
= of_graph_get_next_port(parent
, NULL
);
722 pr_debug("graph: no port node found in %pOF\n", parent
);
726 port
= of_get_parent(prev
);
727 if (WARN_ONCE(!port
, "%s(): endpoint %pOF has no parent node\n",
734 * Now that we have a port node, get the next endpoint by
735 * getting the next child. If the previous endpoint is NULL this
736 * will return the first child.
738 endpoint
= of_graph_get_next_port_endpoint(port
, prev
);
744 /* No more endpoints under this port, try the next one. */
747 port
= of_graph_get_next_port(parent
, port
);
752 EXPORT_SYMBOL(of_graph_get_next_endpoint
);
755 * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers
756 * @parent: pointer to the parent device node
757 * @port_reg: identifier (value of reg property) of the parent port node
758 * @reg: identifier (value of reg property) of the endpoint node
760 * Return: An 'endpoint' node pointer which is identified by reg and at the same
761 * is the child of a port node identified by port_reg. reg and port_reg are
762 * ignored when they are -1. Use of_node_put() on the pointer when done.
764 struct device_node
*of_graph_get_endpoint_by_regs(
765 const struct device_node
*parent
, int port_reg
, int reg
)
767 struct of_endpoint endpoint
;
768 struct device_node
*node
= NULL
;
770 for_each_endpoint_of_node(parent
, node
) {
771 of_graph_parse_endpoint(node
, &endpoint
);
772 if (((port_reg
== -1) || (endpoint
.port
== port_reg
)) &&
773 ((reg
== -1) || (endpoint
.id
== reg
)))
779 EXPORT_SYMBOL(of_graph_get_endpoint_by_regs
);
782 * of_graph_get_remote_endpoint() - get remote endpoint node
783 * @node: pointer to a local endpoint device_node
785 * Return: Remote endpoint node associated with remote endpoint node linked
786 * to @node. Use of_node_put() on it when done.
788 struct device_node
*of_graph_get_remote_endpoint(const struct device_node
*node
)
790 /* Get remote endpoint node. */
791 return of_parse_phandle(node
, "remote-endpoint", 0);
793 EXPORT_SYMBOL(of_graph_get_remote_endpoint
);
796 * of_graph_get_port_parent() - get port's parent node
797 * @node: pointer to a local endpoint device_node
799 * Return: device node associated with endpoint node linked
800 * to @node. Use of_node_put() on it when done.
802 struct device_node
*of_graph_get_port_parent(struct device_node
*node
)
810 * Preserve usecount for passed in node as of_get_next_parent()
811 * will do of_node_put() on it.
815 /* Walk 3 levels up only if there is 'ports' node. */
816 for (depth
= 3; depth
&& node
; depth
--) {
817 node
= of_get_next_parent(node
);
818 if (depth
== 2 && !of_node_name_eq(node
, "ports") &&
819 !of_node_name_eq(node
, "in-ports") &&
820 !of_node_name_eq(node
, "out-ports"))
825 EXPORT_SYMBOL(of_graph_get_port_parent
);
828 * of_graph_get_remote_port_parent() - get remote port's parent node
829 * @node: pointer to a local endpoint device_node
831 * Return: Remote device node associated with remote endpoint node linked
832 * to @node. Use of_node_put() on it when done.
834 struct device_node
*of_graph_get_remote_port_parent(
835 const struct device_node
*node
)
837 /* Get remote endpoint node. */
838 struct device_node
*np
__free(device_node
) =
839 of_graph_get_remote_endpoint(node
);
841 return of_graph_get_port_parent(np
);
843 EXPORT_SYMBOL(of_graph_get_remote_port_parent
);
846 * of_graph_get_remote_port() - get remote port node
847 * @node: pointer to a local endpoint device_node
849 * Return: Remote port node associated with remote endpoint node linked
850 * to @node. Use of_node_put() on it when done.
852 struct device_node
*of_graph_get_remote_port(const struct device_node
*node
)
854 struct device_node
*np
;
856 /* Get remote endpoint node. */
857 np
= of_graph_get_remote_endpoint(node
);
860 return of_get_next_parent(np
);
862 EXPORT_SYMBOL(of_graph_get_remote_port
);
865 * of_graph_get_endpoint_count() - get the number of endpoints in a device node
866 * @np: parent device node containing ports and endpoints
868 * Return: count of endpoint of this device node
870 unsigned int of_graph_get_endpoint_count(const struct device_node
*np
)
872 struct device_node
*endpoint
;
873 unsigned int num
= 0;
875 for_each_endpoint_of_node(np
, endpoint
)
880 EXPORT_SYMBOL(of_graph_get_endpoint_count
);
883 * of_graph_get_port_count() - get the number of port in a device or ports node
884 * @np: pointer to the device or ports node
886 * Return: count of port of this device or ports node
888 unsigned int of_graph_get_port_count(struct device_node
*np
)
890 unsigned int num
= 0;
892 for_each_of_graph_port(np
, port
)
897 EXPORT_SYMBOL(of_graph_get_port_count
);
900 * of_graph_get_remote_node() - get remote parent device_node for given port/endpoint
901 * @node: pointer to parent device_node containing graph port/endpoint
902 * @port: identifier (value of reg property) of the parent port node
903 * @endpoint: identifier (value of reg property) of the endpoint node
905 * Return: Remote device node associated with remote endpoint node linked
906 * to @node. Use of_node_put() on it when done.
908 struct device_node
*of_graph_get_remote_node(const struct device_node
*node
,
909 u32 port
, u32 endpoint
)
911 struct device_node
*endpoint_node
, *remote
;
913 endpoint_node
= of_graph_get_endpoint_by_regs(node
, port
, endpoint
);
914 if (!endpoint_node
) {
915 pr_debug("no valid endpoint (%d, %d) for node %pOF\n",
916 port
, endpoint
, node
);
920 remote
= of_graph_get_remote_port_parent(endpoint_node
);
921 of_node_put(endpoint_node
);
923 pr_debug("no valid remote node\n");
927 if (!of_device_is_available(remote
)) {
928 pr_debug("not available for remote node\n");
935 EXPORT_SYMBOL(of_graph_get_remote_node
);
937 static struct fwnode_handle
*of_fwnode_get(struct fwnode_handle
*fwnode
)
939 return of_fwnode_handle(of_node_get(to_of_node(fwnode
)));
942 static void of_fwnode_put(struct fwnode_handle
*fwnode
)
944 of_node_put(to_of_node(fwnode
));
947 static bool of_fwnode_device_is_available(const struct fwnode_handle
*fwnode
)
949 return of_device_is_available(to_of_node(fwnode
));
952 static bool of_fwnode_device_dma_supported(const struct fwnode_handle
*fwnode
)
957 static enum dev_dma_attr
958 of_fwnode_device_get_dma_attr(const struct fwnode_handle
*fwnode
)
960 if (of_dma_is_coherent(to_of_node(fwnode
)))
961 return DEV_DMA_COHERENT
;
963 return DEV_DMA_NON_COHERENT
;
966 static bool of_fwnode_property_present(const struct fwnode_handle
*fwnode
,
967 const char *propname
)
969 return of_property_read_bool(to_of_node(fwnode
), propname
);
972 static int of_fwnode_property_read_int_array(const struct fwnode_handle
*fwnode
,
973 const char *propname
,
974 unsigned int elem_size
, void *val
,
977 const struct device_node
*node
= to_of_node(fwnode
);
980 return of_property_count_elems_of_size(node
, propname
,
985 return of_property_read_u8_array(node
, propname
, val
, nval
);
987 return of_property_read_u16_array(node
, propname
, val
, nval
);
989 return of_property_read_u32_array(node
, propname
, val
, nval
);
991 return of_property_read_u64_array(node
, propname
, val
, nval
);
998 of_fwnode_property_read_string_array(const struct fwnode_handle
*fwnode
,
999 const char *propname
, const char **val
,
1002 const struct device_node
*node
= to_of_node(fwnode
);
1005 of_property_read_string_array(node
, propname
, val
, nval
) :
1006 of_property_count_strings(node
, propname
);
1009 static const char *of_fwnode_get_name(const struct fwnode_handle
*fwnode
)
1011 return kbasename(to_of_node(fwnode
)->full_name
);
1014 static const char *of_fwnode_get_name_prefix(const struct fwnode_handle
*fwnode
)
1016 /* Root needs no prefix here (its name is "/"). */
1017 if (!to_of_node(fwnode
)->parent
)
1023 static struct fwnode_handle
*
1024 of_fwnode_get_parent(const struct fwnode_handle
*fwnode
)
1026 return of_fwnode_handle(of_get_parent(to_of_node(fwnode
)));
1029 static struct fwnode_handle
*
1030 of_fwnode_get_next_child_node(const struct fwnode_handle
*fwnode
,
1031 struct fwnode_handle
*child
)
1033 return of_fwnode_handle(of_get_next_available_child(to_of_node(fwnode
),
1034 to_of_node(child
)));
1037 static struct fwnode_handle
*
1038 of_fwnode_get_named_child_node(const struct fwnode_handle
*fwnode
,
1039 const char *childname
)
1041 const struct device_node
*node
= to_of_node(fwnode
);
1042 struct device_node
*child
;
1044 for_each_available_child_of_node(node
, child
)
1045 if (of_node_name_eq(child
, childname
))
1046 return of_fwnode_handle(child
);
1052 of_fwnode_get_reference_args(const struct fwnode_handle
*fwnode
,
1053 const char *prop
, const char *nargs_prop
,
1054 unsigned int nargs
, unsigned int index
,
1055 struct fwnode_reference_args
*args
)
1057 struct of_phandle_args of_args
;
1062 ret
= of_parse_phandle_with_args(to_of_node(fwnode
), prop
,
1063 nargs_prop
, index
, &of_args
);
1065 ret
= of_parse_phandle_with_fixed_args(to_of_node(fwnode
), prop
,
1066 nargs
, index
, &of_args
);
1070 of_node_put(of_args
.np
);
1074 args
->nargs
= of_args
.args_count
;
1075 args
->fwnode
= of_fwnode_handle(of_args
.np
);
1077 for (i
= 0; i
< NR_FWNODE_REFERENCE_ARGS
; i
++)
1078 args
->args
[i
] = i
< of_args
.args_count
? of_args
.args
[i
] : 0;
1083 static struct fwnode_handle
*
1084 of_fwnode_graph_get_next_endpoint(const struct fwnode_handle
*fwnode
,
1085 struct fwnode_handle
*prev
)
1087 return of_fwnode_handle(of_graph_get_next_endpoint(to_of_node(fwnode
),
1091 static struct fwnode_handle
*
1092 of_fwnode_graph_get_remote_endpoint(const struct fwnode_handle
*fwnode
)
1094 return of_fwnode_handle(
1095 of_graph_get_remote_endpoint(to_of_node(fwnode
)));
1098 static struct fwnode_handle
*
1099 of_fwnode_graph_get_port_parent(struct fwnode_handle
*fwnode
)
1101 struct device_node
*np
;
1103 /* Get the parent of the port */
1104 np
= of_get_parent(to_of_node(fwnode
));
1108 /* Is this the "ports" node? If not, it's the port parent. */
1109 if (!of_node_name_eq(np
, "ports"))
1110 return of_fwnode_handle(np
);
1112 return of_fwnode_handle(of_get_next_parent(np
));
1115 static int of_fwnode_graph_parse_endpoint(const struct fwnode_handle
*fwnode
,
1116 struct fwnode_endpoint
*endpoint
)
1118 const struct device_node
*node
= to_of_node(fwnode
);
1119 struct device_node
*port_node
__free(device_node
) = of_get_parent(node
);
1121 endpoint
->local_fwnode
= fwnode
;
1123 of_property_read_u32(port_node
, "reg", &endpoint
->port
);
1124 of_property_read_u32(node
, "reg", &endpoint
->id
);
1130 of_fwnode_device_get_match_data(const struct fwnode_handle
*fwnode
,
1131 const struct device
*dev
)
1133 return of_device_get_match_data(dev
);
1136 static void of_link_to_phandle(struct device_node
*con_np
,
1137 struct device_node
*sup_np
,
1140 struct device_node
*tmp_np
__free(device_node
) = of_node_get(sup_np
);
1142 /* Check that sup_np and its ancestors are available. */
1144 if (of_fwnode_handle(tmp_np
)->dev
)
1147 if (!of_device_is_available(tmp_np
))
1150 tmp_np
= of_get_next_parent(tmp_np
);
1153 fwnode_link_add(of_fwnode_handle(con_np
), of_fwnode_handle(sup_np
), flags
);
1157 * parse_prop_cells - Property parsing function for suppliers
1159 * @np: Pointer to device tree node containing a list
1160 * @prop_name: Name of property to be parsed. Expected to hold phandle values
1161 * @index: For properties holding a list of phandles, this is the index
1163 * @list_name: Property name that is known to contain list of phandle(s) to
1165 * @cells_name: property name that specifies phandles' arguments count
1167 * This is a helper function to parse properties that have a known fixed name
1168 * and are a list of phandles and phandle arguments.
1171 * - phandle node pointer with refcount incremented. Caller must of_node_put()
1173 * - NULL if no phandle found at index
1175 static struct device_node
*parse_prop_cells(struct device_node
*np
,
1176 const char *prop_name
, int index
,
1177 const char *list_name
,
1178 const char *cells_name
)
1180 struct of_phandle_args sup_args
;
1182 if (strcmp(prop_name
, list_name
))
1185 if (__of_parse_phandle_with_args(np
, list_name
, cells_name
, 0, index
,
1192 #define DEFINE_SIMPLE_PROP(fname, name, cells) \
1193 static struct device_node *parse_##fname(struct device_node *np, \
1194 const char *prop_name, int index) \
1196 return parse_prop_cells(np, prop_name, index, name, cells); \
1199 static int strcmp_suffix(const char *str
, const char *suffix
)
1201 unsigned int len
, suffix_len
;
1204 suffix_len
= strlen(suffix
);
1205 if (len
<= suffix_len
)
1207 return strcmp(str
+ len
- suffix_len
, suffix
);
1211 * parse_suffix_prop_cells - Suffix property parsing function for suppliers
1213 * @np: Pointer to device tree node containing a list
1214 * @prop_name: Name of property to be parsed. Expected to hold phandle values
1215 * @index: For properties holding a list of phandles, this is the index
1217 * @suffix: Property suffix that is known to contain list of phandle(s) to
1219 * @cells_name: property name that specifies phandles' arguments count
1221 * This is a helper function to parse properties that have a known fixed suffix
1222 * and are a list of phandles and phandle arguments.
1225 * - phandle node pointer with refcount incremented. Caller must of_node_put()
1227 * - NULL if no phandle found at index
1229 static struct device_node
*parse_suffix_prop_cells(struct device_node
*np
,
1230 const char *prop_name
, int index
,
1232 const char *cells_name
)
1234 struct of_phandle_args sup_args
;
1236 if (strcmp_suffix(prop_name
, suffix
))
1239 if (of_parse_phandle_with_args(np
, prop_name
, cells_name
, index
,
1246 #define DEFINE_SUFFIX_PROP(fname, suffix, cells) \
1247 static struct device_node *parse_##fname(struct device_node *np, \
1248 const char *prop_name, int index) \
1250 return parse_suffix_prop_cells(np, prop_name, index, suffix, cells); \
1254 * struct supplier_bindings - Property parsing functions for suppliers
1256 * @parse_prop: function name
1257 * parse_prop() finds the node corresponding to a supplier phandle
1258 * parse_prop.np: Pointer to device node holding supplier phandle property
1259 * parse_prop.prop_name: Name of property holding a phandle value
1260 * parse_prop.index: For properties holding a list of phandles, this is the
1261 * index into the list
1262 * @get_con_dev: If the consumer node containing the property is never converted
1263 * to a struct device, implement this ops so fw_devlink can use it
1264 * to find the true consumer.
1265 * @optional: Describes whether a supplier is mandatory or not
1266 * @fwlink_flags: Optional fwnode link flags to use when creating a fwnode link
1267 * for this property.
1270 * parse_prop() return values are
1271 * - phandle node pointer with refcount incremented. Caller must of_node_put()
1273 * - NULL if no phandle found at index
1275 struct supplier_bindings
{
1276 struct device_node
*(*parse_prop
)(struct device_node
*np
,
1277 const char *prop_name
, int index
);
1278 struct device_node
*(*get_con_dev
)(struct device_node
*np
);
1283 DEFINE_SIMPLE_PROP(clocks
, "clocks", "#clock-cells")
1284 DEFINE_SIMPLE_PROP(interconnects
, "interconnects", "#interconnect-cells")
1285 DEFINE_SIMPLE_PROP(iommus
, "iommus", "#iommu-cells")
1286 DEFINE_SIMPLE_PROP(mboxes
, "mboxes", "#mbox-cells")
1287 DEFINE_SIMPLE_PROP(io_channels
, "io-channels", "#io-channel-cells")
1288 DEFINE_SIMPLE_PROP(io_backends
, "io-backends", "#io-backend-cells")
1289 DEFINE_SIMPLE_PROP(interrupt_parent
, "interrupt-parent", NULL
)
1290 DEFINE_SIMPLE_PROP(dmas
, "dmas", "#dma-cells")
1291 DEFINE_SIMPLE_PROP(power_domains
, "power-domains", "#power-domain-cells")
1292 DEFINE_SIMPLE_PROP(hwlocks
, "hwlocks", "#hwlock-cells")
1293 DEFINE_SIMPLE_PROP(extcon
, "extcon", NULL
)
1294 DEFINE_SIMPLE_PROP(nvmem_cells
, "nvmem-cells", "#nvmem-cell-cells")
1295 DEFINE_SIMPLE_PROP(phys
, "phys", "#phy-cells")
1296 DEFINE_SIMPLE_PROP(wakeup_parent
, "wakeup-parent", NULL
)
1297 DEFINE_SIMPLE_PROP(pinctrl0
, "pinctrl-0", NULL
)
1298 DEFINE_SIMPLE_PROP(pinctrl1
, "pinctrl-1", NULL
)
1299 DEFINE_SIMPLE_PROP(pinctrl2
, "pinctrl-2", NULL
)
1300 DEFINE_SIMPLE_PROP(pinctrl3
, "pinctrl-3", NULL
)
1301 DEFINE_SIMPLE_PROP(pinctrl4
, "pinctrl-4", NULL
)
1302 DEFINE_SIMPLE_PROP(pinctrl5
, "pinctrl-5", NULL
)
1303 DEFINE_SIMPLE_PROP(pinctrl6
, "pinctrl-6", NULL
)
1304 DEFINE_SIMPLE_PROP(pinctrl7
, "pinctrl-7", NULL
)
1305 DEFINE_SIMPLE_PROP(pinctrl8
, "pinctrl-8", NULL
)
1306 DEFINE_SIMPLE_PROP(pwms
, "pwms", "#pwm-cells")
1307 DEFINE_SIMPLE_PROP(resets
, "resets", "#reset-cells")
1308 DEFINE_SIMPLE_PROP(leds
, "leds", NULL
)
1309 DEFINE_SIMPLE_PROP(backlight
, "backlight", NULL
)
1310 DEFINE_SIMPLE_PROP(panel
, "panel", NULL
)
1311 DEFINE_SIMPLE_PROP(msi_parent
, "msi-parent", "#msi-cells")
1312 DEFINE_SIMPLE_PROP(post_init_providers
, "post-init-providers", NULL
)
1313 DEFINE_SIMPLE_PROP(access_controllers
, "access-controllers", "#access-controller-cells")
1314 DEFINE_SIMPLE_PROP(pses
, "pses", "#pse-cells")
1315 DEFINE_SIMPLE_PROP(power_supplies
, "power-supplies", NULL
)
1316 DEFINE_SUFFIX_PROP(regulators
, "-supply", NULL
)
1317 DEFINE_SUFFIX_PROP(gpio
, "-gpio", "#gpio-cells")
1319 static struct device_node
*parse_gpios(struct device_node
*np
,
1320 const char *prop_name
, int index
)
1322 if (!strcmp_suffix(prop_name
, ",nr-gpios"))
1325 return parse_suffix_prop_cells(np
, prop_name
, index
, "-gpios",
1329 static struct device_node
*parse_iommu_maps(struct device_node
*np
,
1330 const char *prop_name
, int index
)
1332 if (strcmp(prop_name
, "iommu-map"))
1335 return of_parse_phandle(np
, prop_name
, (index
* 4) + 1);
1338 static struct device_node
*parse_gpio_compat(struct device_node
*np
,
1339 const char *prop_name
, int index
)
1341 struct of_phandle_args sup_args
;
1343 if (strcmp(prop_name
, "gpio") && strcmp(prop_name
, "gpios"))
1347 * Ignore node with gpio-hog property since its gpios are all provided
1350 if (of_property_read_bool(np
, "gpio-hog"))
1353 if (of_parse_phandle_with_args(np
, prop_name
, "#gpio-cells", index
,
1360 static struct device_node
*parse_interrupts(struct device_node
*np
,
1361 const char *prop_name
, int index
)
1363 struct of_phandle_args sup_args
;
1365 if (!IS_ENABLED(CONFIG_OF_IRQ
) || IS_ENABLED(CONFIG_PPC
))
1368 if (strcmp(prop_name
, "interrupts") &&
1369 strcmp(prop_name
, "interrupts-extended"))
1372 return of_irq_parse_one(np
, index
, &sup_args
) ? NULL
: sup_args
.np
;
1375 static struct device_node
*parse_interrupt_map(struct device_node
*np
,
1376 const char *prop_name
, int index
)
1378 const __be32
*imap
, *imap_end
;
1379 struct of_phandle_args sup_args
;
1380 u32 addrcells
, intcells
;
1383 if (!IS_ENABLED(CONFIG_OF_IRQ
))
1386 if (strcmp(prop_name
, "interrupt-map"))
1389 if (of_property_read_u32(np
, "#interrupt-cells", &intcells
))
1391 addrcells
= of_bus_n_addr_cells(np
);
1393 imap
= of_get_property(np
, "interrupt-map", &imaplen
);
1394 imaplen
/= sizeof(*imap
);
1398 imap_end
= imap
+ imaplen
;
1400 for (int i
= 0; imap
+ addrcells
+ intcells
+ 1 < imap_end
; i
++) {
1401 imap
+= addrcells
+ intcells
;
1403 imap
= of_irq_parse_imap_parent(imap
, imap_end
- imap
, &sup_args
);
1410 of_node_put(sup_args
.np
);
1416 static struct device_node
*parse_remote_endpoint(struct device_node
*np
,
1417 const char *prop_name
,
1420 /* Return NULL for index > 0 to signify end of remote-endpoints. */
1421 if (index
> 0 || strcmp(prop_name
, "remote-endpoint"))
1424 return of_graph_get_remote_port_parent(np
);
1427 static const struct supplier_bindings of_supplier_bindings
[] = {
1428 { .parse_prop
= parse_clocks
, },
1429 { .parse_prop
= parse_interconnects
, },
1430 { .parse_prop
= parse_iommus
, .optional
= true, },
1431 { .parse_prop
= parse_iommu_maps
, .optional
= true, },
1432 { .parse_prop
= parse_mboxes
, },
1433 { .parse_prop
= parse_io_channels
, },
1434 { .parse_prop
= parse_io_backends
, },
1435 { .parse_prop
= parse_interrupt_parent
, },
1436 { .parse_prop
= parse_dmas
, .optional
= true, },
1437 { .parse_prop
= parse_power_domains
, },
1438 { .parse_prop
= parse_hwlocks
, },
1439 { .parse_prop
= parse_extcon
, },
1440 { .parse_prop
= parse_nvmem_cells
, },
1441 { .parse_prop
= parse_phys
, },
1442 { .parse_prop
= parse_wakeup_parent
, },
1443 { .parse_prop
= parse_pinctrl0
, },
1444 { .parse_prop
= parse_pinctrl1
, },
1445 { .parse_prop
= parse_pinctrl2
, },
1446 { .parse_prop
= parse_pinctrl3
, },
1447 { .parse_prop
= parse_pinctrl4
, },
1448 { .parse_prop
= parse_pinctrl5
, },
1449 { .parse_prop
= parse_pinctrl6
, },
1450 { .parse_prop
= parse_pinctrl7
, },
1451 { .parse_prop
= parse_pinctrl8
, },
1453 .parse_prop
= parse_remote_endpoint
,
1454 .get_con_dev
= of_graph_get_port_parent
,
1456 { .parse_prop
= parse_pwms
, },
1457 { .parse_prop
= parse_resets
, },
1458 { .parse_prop
= parse_leds
, },
1459 { .parse_prop
= parse_backlight
, },
1460 { .parse_prop
= parse_panel
, },
1461 { .parse_prop
= parse_msi_parent
, },
1462 { .parse_prop
= parse_pses
, },
1463 { .parse_prop
= parse_power_supplies
, },
1464 { .parse_prop
= parse_gpio_compat
, },
1465 { .parse_prop
= parse_interrupts
, },
1466 { .parse_prop
= parse_interrupt_map
, },
1467 { .parse_prop
= parse_access_controllers
, },
1468 { .parse_prop
= parse_regulators
, },
1469 { .parse_prop
= parse_gpio
, },
1470 { .parse_prop
= parse_gpios
, },
1472 .parse_prop
= parse_post_init_providers
,
1473 .fwlink_flags
= FWLINK_FLAG_IGNORE
,
1479 * of_link_property - Create device links to suppliers listed in a property
1480 * @con_np: The consumer device tree node which contains the property
1481 * @prop_name: Name of property to be parsed
1483 * This function checks if the property @prop_name that is present in the
1484 * @con_np device tree node is one of the known common device tree bindings
1485 * that list phandles to suppliers. If @prop_name isn't one, this function
1486 * doesn't do anything.
1488 * If @prop_name is one, this function attempts to create fwnode links from the
1489 * consumer device tree node @con_np to all the suppliers device tree nodes
1490 * listed in @prop_name.
1492 * Any failed attempt to create a fwnode link will NOT result in an immediate
1493 * return. of_link_property() must create links to all the available supplier
1494 * device tree nodes even when attempts to create a link to one or more
1497 static int of_link_property(struct device_node
*con_np
, const char *prop_name
)
1499 struct device_node
*phandle
;
1500 const struct supplier_bindings
*s
= of_supplier_bindings
;
1502 bool matched
= false;
1504 /* Do not stop at first failed link, link all available suppliers. */
1505 while (!matched
&& s
->parse_prop
) {
1506 if (s
->optional
&& !fw_devlink_is_strict()) {
1511 while ((phandle
= s
->parse_prop(con_np
, prop_name
, i
))) {
1512 struct device_node
*con_dev_np
__free(device_node
) =
1513 s
->get_con_dev
? s
->get_con_dev(con_np
) : of_node_get(con_np
);
1517 of_link_to_phandle(con_dev_np
, phandle
, s
->fwlink_flags
);
1518 of_node_put(phandle
);
1525 static void __iomem
*of_fwnode_iomap(struct fwnode_handle
*fwnode
, int index
)
1527 #ifdef CONFIG_OF_ADDRESS
1528 return of_iomap(to_of_node(fwnode
), index
);
1534 static int of_fwnode_irq_get(const struct fwnode_handle
*fwnode
,
1537 return of_irq_get(to_of_node(fwnode
), index
);
1540 static int of_fwnode_add_links(struct fwnode_handle
*fwnode
)
1542 const struct property
*p
;
1543 struct device_node
*con_np
= to_of_node(fwnode
);
1545 if (IS_ENABLED(CONFIG_X86
))
1551 for_each_property_of_node(con_np
, p
)
1552 of_link_property(con_np
, p
->name
);
1557 const struct fwnode_operations of_fwnode_ops
= {
1558 .get
= of_fwnode_get
,
1559 .put
= of_fwnode_put
,
1560 .device_is_available
= of_fwnode_device_is_available
,
1561 .device_get_match_data
= of_fwnode_device_get_match_data
,
1562 .device_dma_supported
= of_fwnode_device_dma_supported
,
1563 .device_get_dma_attr
= of_fwnode_device_get_dma_attr
,
1564 .property_present
= of_fwnode_property_present
,
1565 .property_read_int_array
= of_fwnode_property_read_int_array
,
1566 .property_read_string_array
= of_fwnode_property_read_string_array
,
1567 .get_name
= of_fwnode_get_name
,
1568 .get_name_prefix
= of_fwnode_get_name_prefix
,
1569 .get_parent
= of_fwnode_get_parent
,
1570 .get_next_child_node
= of_fwnode_get_next_child_node
,
1571 .get_named_child_node
= of_fwnode_get_named_child_node
,
1572 .get_reference_args
= of_fwnode_get_reference_args
,
1573 .graph_get_next_endpoint
= of_fwnode_graph_get_next_endpoint
,
1574 .graph_get_remote_endpoint
= of_fwnode_graph_get_remote_endpoint
,
1575 .graph_get_port_parent
= of_fwnode_graph_get_port_parent
,
1576 .graph_parse_endpoint
= of_fwnode_graph_parse_endpoint
,
1577 .iomap
= of_fwnode_iomap
,
1578 .irq_get
= of_fwnode_irq_get
,
1579 .add_links
= of_fwnode_add_links
,
1581 EXPORT_SYMBOL_GPL(of_fwnode_ops
);