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_device.h>
26 #include <linux/of_graph.h>
27 #include <linux/of_irq.h>
28 #include <linux/string.h>
29 #include <linux/moduleparam.h>
31 #include "of_private.h"
34 * of_graph_is_present() - check graph's presence
35 * @node: pointer to device_node containing graph port
37 * Return: True if @node has a port or ports (with a port) sub-node,
40 bool of_graph_is_present(const struct device_node *node)
42 struct device_node *ports, *port;
44 ports = of_get_child_by_name(node, "ports");
48 port = of_get_child_by_name(node, "port");
54 EXPORT_SYMBOL(of_graph_is_present);
57 * of_property_count_elems_of_size - Count the number of elements in a property
59 * @np: device node from which the property value is to be read.
60 * @propname: name of the property to be searched.
61 * @elem_size: size of the individual element
63 * Search for a property in a device node and count the number of elements of
64 * size elem_size in it.
66 * Return: The number of elements on sucess, -EINVAL if the property does not
67 * exist or its length does not match a multiple of elem_size and -ENODATA if
68 * the property does not have a value.
70 int of_property_count_elems_of_size(const struct device_node *np,
71 const char *propname, int elem_size)
73 struct property *prop = of_find_property(np, propname, NULL);
80 if (prop->length % elem_size != 0) {
81 pr_err("size of %s in node %pOF is not a multiple of %d\n",
82 propname, np, elem_size);
86 return prop->length / elem_size;
88 EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
91 * of_find_property_value_of_size
93 * @np: device node from which the property value is to be read.
94 * @propname: name of the property to be searched.
95 * @min: minimum allowed length of property value
96 * @max: maximum allowed length of property value (0 means unlimited)
97 * @len: if !=NULL, actual length is written to here
99 * Search for a property in a device node and valid the requested size.
101 * Return: The property value on success, -EINVAL if the property does not
102 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
103 * property data is too small or too large.
106 static void *of_find_property_value_of_size(const struct device_node *np,
107 const char *propname, u32 min, u32 max, size_t *len)
109 struct property *prop = of_find_property(np, propname, NULL);
112 return ERR_PTR(-EINVAL);
114 return ERR_PTR(-ENODATA);
115 if (prop->length < min)
116 return ERR_PTR(-EOVERFLOW);
117 if (max && prop->length > max)
118 return ERR_PTR(-EOVERFLOW);
127 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
129 * @np: device node from which the property value is to be read.
130 * @propname: name of the property to be searched.
131 * @index: index of the u32 in the list of values
132 * @out_value: pointer to return value, modified only if no error.
134 * Search for a property in a device node and read nth 32-bit value from
137 * Return: 0 on success, -EINVAL if the property does not exist,
138 * -ENODATA if property does not have a value, and -EOVERFLOW if the
139 * property data isn't large enough.
141 * The out_value is modified only if a valid u32 value can be decoded.
143 int of_property_read_u32_index(const struct device_node *np,
144 const char *propname,
145 u32 index, u32 *out_value)
147 const u32 *val = of_find_property_value_of_size(np, propname,
148 ((index + 1) * sizeof(*out_value)),
155 *out_value = be32_to_cpup(((__be32 *)val) + index);
158 EXPORT_SYMBOL_GPL(of_property_read_u32_index);
161 * of_property_read_u64_index - Find and read a u64 from a multi-value property.
163 * @np: device node from which the property value is to be read.
164 * @propname: name of the property to be searched.
165 * @index: index of the u64 in the list of values
166 * @out_value: pointer to return value, modified only if no error.
168 * Search for a property in a device node and read nth 64-bit value from
171 * Return: 0 on success, -EINVAL if the property does not exist,
172 * -ENODATA if property does not have a value, and -EOVERFLOW if the
173 * property data isn't large enough.
175 * The out_value is modified only if a valid u64 value can be decoded.
177 int of_property_read_u64_index(const struct device_node *np,
178 const char *propname,
179 u32 index, u64 *out_value)
181 const u64 *val = of_find_property_value_of_size(np, propname,
182 ((index + 1) * sizeof(*out_value)),
188 *out_value = be64_to_cpup(((__be64 *)val) + index);
191 EXPORT_SYMBOL_GPL(of_property_read_u64_index);
194 * of_property_read_variable_u8_array - Find and read an array of u8 from a
195 * property, with bounds on the minimum and maximum array size.
197 * @np: device node from which the property value is to be read.
198 * @propname: name of the property to be searched.
199 * @out_values: pointer to found values.
200 * @sz_min: minimum number of array elements to read
201 * @sz_max: maximum number of array elements to read, if zero there is no
202 * upper limit on the number of elements in the dts entry but only
203 * sz_min will be read.
205 * Search for a property in a device node and read 8-bit value(s) from
208 * dts entry of array should be like:
209 * ``property = /bits/ 8 <0x50 0x60 0x70>;``
211 * Return: The number of elements read on success, -EINVAL if the property
212 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
213 * if the property data is smaller than sz_min or longer than sz_max.
215 * The out_values is modified only if a valid u8 value can be decoded.
217 int of_property_read_variable_u8_array(const struct device_node *np,
218 const char *propname, u8 *out_values,
219 size_t sz_min, size_t sz_max)
222 const u8 *val = of_find_property_value_of_size(np, propname,
223 (sz_min * sizeof(*out_values)),
224 (sz_max * sizeof(*out_values)),
233 sz /= sizeof(*out_values);
237 *out_values++ = *val++;
241 EXPORT_SYMBOL_GPL(of_property_read_variable_u8_array);
244 * of_property_read_variable_u16_array - Find and read an array of u16 from a
245 * property, with bounds on the minimum and maximum array size.
247 * @np: device node from which the property value is to be read.
248 * @propname: name of the property to be searched.
249 * @out_values: pointer to found values.
250 * @sz_min: minimum number of array elements to read
251 * @sz_max: maximum number of array elements to read, if zero there is no
252 * upper limit on the number of elements in the dts entry but only
253 * sz_min will be read.
255 * Search for a property in a device node and read 16-bit value(s) from
258 * dts entry of array should be like:
259 * ``property = /bits/ 16 <0x5000 0x6000 0x7000>;``
261 * Return: The number of elements read on success, -EINVAL if the property
262 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
263 * if the property data is smaller than sz_min or longer than sz_max.
265 * The out_values is modified only if a valid u16 value can be decoded.
267 int of_property_read_variable_u16_array(const struct device_node *np,
268 const char *propname, u16 *out_values,
269 size_t sz_min, size_t sz_max)
272 const __be16 *val = of_find_property_value_of_size(np, propname,
273 (sz_min * sizeof(*out_values)),
274 (sz_max * sizeof(*out_values)),
283 sz /= sizeof(*out_values);
287 *out_values++ = be16_to_cpup(val++);
291 EXPORT_SYMBOL_GPL(of_property_read_variable_u16_array);
294 * of_property_read_variable_u32_array - Find and read an array of 32 bit
295 * integers from a property, with bounds on the minimum and maximum array size.
297 * @np: device node from which the property value is to be read.
298 * @propname: name of the property to be searched.
299 * @out_values: pointer to return found values.
300 * @sz_min: minimum number of array elements to read
301 * @sz_max: maximum number of array elements to read, if zero there is no
302 * upper limit on the number of elements in the dts entry but only
303 * sz_min will be read.
305 * Search for a property in a device node and read 32-bit value(s) from
308 * Return: The number of elements read on success, -EINVAL if the property
309 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
310 * if the property data is smaller than sz_min or longer than sz_max.
312 * The out_values is modified only if a valid u32 value can be decoded.
314 int of_property_read_variable_u32_array(const struct device_node *np,
315 const char *propname, u32 *out_values,
316 size_t sz_min, size_t sz_max)
319 const __be32 *val = of_find_property_value_of_size(np, propname,
320 (sz_min * sizeof(*out_values)),
321 (sz_max * sizeof(*out_values)),
330 sz /= sizeof(*out_values);
334 *out_values++ = be32_to_cpup(val++);
338 EXPORT_SYMBOL_GPL(of_property_read_variable_u32_array);
341 * of_property_read_u64 - Find and read a 64 bit integer from a property
342 * @np: device node from which the property value is to be read.
343 * @propname: name of the property to be searched.
344 * @out_value: pointer to return value, modified only if return value is 0.
346 * Search for a property in a device node and read a 64-bit value from
349 * Return: 0 on success, -EINVAL if the property does not exist,
350 * -ENODATA if property does not have a value, and -EOVERFLOW if the
351 * property data isn't large enough.
353 * The out_value is modified only if a valid u64 value can be decoded.
355 int of_property_read_u64(const struct device_node *np, const char *propname,
358 const __be32 *val = of_find_property_value_of_size(np, propname,
366 *out_value = of_read_number(val, 2);
369 EXPORT_SYMBOL_GPL(of_property_read_u64);
372 * of_property_read_variable_u64_array - Find and read an array of 64 bit
373 * integers from a property, with bounds on the minimum and maximum array size.
375 * @np: device node from which the property value is to be read.
376 * @propname: name of the property to be searched.
377 * @out_values: pointer to found values.
378 * @sz_min: minimum number of array elements to read
379 * @sz_max: maximum number of array elements to read, if zero there is no
380 * upper limit on the number of elements in the dts entry but only
381 * sz_min will be read.
383 * Search for a property in a device node and read 64-bit value(s) from
386 * Return: The number of elements read on success, -EINVAL if the property
387 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
388 * if the property data is smaller than sz_min or longer than sz_max.
390 * The out_values is modified only if a valid u64 value can be decoded.
392 int of_property_read_variable_u64_array(const struct device_node *np,
393 const char *propname, u64 *out_values,
394 size_t sz_min, size_t sz_max)
397 const __be32 *val = of_find_property_value_of_size(np, propname,
398 (sz_min * sizeof(*out_values)),
399 (sz_max * sizeof(*out_values)),
408 sz /= sizeof(*out_values);
412 *out_values++ = of_read_number(val, 2);
418 EXPORT_SYMBOL_GPL(of_property_read_variable_u64_array);
421 * of_property_read_string - Find and read a string from a property
422 * @np: device node from which the property value is to be read.
423 * @propname: name of the property to be searched.
424 * @out_string: pointer to null terminated return string, modified only if
427 * Search for a property in a device tree node and retrieve a null
428 * terminated string value (pointer to data, not a copy).
430 * Return: 0 on success, -EINVAL if the property does not exist, -ENODATA if
431 * property does not have a value, and -EILSEQ if the string is not
432 * null-terminated within the length of the property data.
434 * The out_string pointer is modified only if a valid string can be decoded.
436 int of_property_read_string(const struct device_node *np, const char *propname,
437 const char **out_string)
439 const struct property *prop = of_find_property(np, propname, NULL);
444 if (strnlen(prop->value, prop->length) >= prop->length)
446 *out_string = prop->value;
449 EXPORT_SYMBOL_GPL(of_property_read_string);
452 * of_property_match_string() - Find string in a list and return index
453 * @np: pointer to node containing string list property
454 * @propname: string list property name
455 * @string: pointer to string to search for in string list
457 * This function searches a string list property and returns the index
458 * of a specific string value.
460 int of_property_match_string(const struct device_node *np, const char *propname,
463 const struct property *prop = of_find_property(np, propname, NULL);
474 end = p + prop->length;
476 for (i = 0; p < end; i++, p += l) {
477 l = strnlen(p, end - p) + 1;
480 pr_debug("comparing %s with %s\n", string, p);
481 if (strcmp(string, p) == 0)
482 return i; /* Found it; return index */
486 EXPORT_SYMBOL_GPL(of_property_match_string);
489 * of_property_read_string_helper() - Utility helper for parsing string properties
490 * @np: device node from which the property value is to be read.
491 * @propname: name of the property to be searched.
492 * @out_strs: output array of string pointers.
493 * @sz: number of array elements to read.
494 * @skip: Number of strings to skip over at beginning of list.
496 * Don't call this function directly. It is a utility helper for the
497 * of_property_read_string*() family of functions.
499 int of_property_read_string_helper(const struct device_node *np,
500 const char *propname, const char **out_strs,
503 const struct property *prop = of_find_property(np, propname, NULL);
512 end = p + prop->length;
514 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
515 l = strnlen(p, end - p) + 1;
518 if (out_strs && i >= skip)
522 return i <= 0 ? -ENODATA : i;
524 EXPORT_SYMBOL_GPL(of_property_read_string_helper);
526 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
529 const void *curv = cur;
539 curv += sizeof(*cur);
540 if (curv >= prop->value + prop->length)
544 *pu = be32_to_cpup(curv);
547 EXPORT_SYMBOL_GPL(of_prop_next_u32);
549 const char *of_prop_next_string(struct property *prop, const char *cur)
551 const void *curv = cur;
559 curv += strlen(cur) + 1;
560 if (curv >= prop->value + prop->length)
565 EXPORT_SYMBOL_GPL(of_prop_next_string);
568 * of_graph_parse_endpoint() - parse common endpoint node properties
569 * @node: pointer to endpoint device_node
570 * @endpoint: pointer to the OF endpoint data structure
572 * The caller should hold a reference to @node.
574 int of_graph_parse_endpoint(const struct device_node *node,
575 struct of_endpoint *endpoint)
577 struct device_node *port_node = of_get_parent(node);
579 WARN_ONCE(!port_node, "%s(): endpoint %pOF has no parent node\n",
582 memset(endpoint, 0, sizeof(*endpoint));
584 endpoint->local_node = node;
586 * It doesn't matter whether the two calls below succeed.
587 * If they don't then the default value 0 is used.
589 of_property_read_u32(port_node, "reg", &endpoint->port);
590 of_property_read_u32(node, "reg", &endpoint->id);
592 of_node_put(port_node);
596 EXPORT_SYMBOL(of_graph_parse_endpoint);
599 * of_graph_get_port_by_id() - get the port matching a given id
600 * @parent: pointer to the parent device node
601 * @id: id of the port
603 * Return: A 'port' node pointer with refcount incremented. The caller
604 * has to use of_node_put() on it when done.
606 struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id)
608 struct device_node *node, *port;
610 node = of_get_child_by_name(parent, "ports");
614 for_each_child_of_node(parent, port) {
617 if (!of_node_name_eq(port, "port"))
619 of_property_read_u32(port, "reg", &port_id);
628 EXPORT_SYMBOL(of_graph_get_port_by_id);
631 * of_graph_get_next_endpoint() - get next endpoint node
632 * @parent: pointer to the parent device node
633 * @prev: previous endpoint node, or NULL to get first
635 * Return: An 'endpoint' node pointer with refcount incremented. Refcount
636 * of the passed @prev node is decremented.
638 struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
639 struct device_node *prev)
641 struct device_node *endpoint;
642 struct device_node *port;
648 * Start by locating the port node. If no previous endpoint is specified
649 * search for the first port node, otherwise get the previous endpoint
653 struct device_node *node;
655 node = of_get_child_by_name(parent, "ports");
659 port = of_get_child_by_name(parent, "port");
663 pr_err("graph: no port node found in %pOF\n", parent);
667 port = of_get_parent(prev);
668 if (WARN_ONCE(!port, "%s(): endpoint %pOF has no parent node\n",
675 * Now that we have a port node, get the next endpoint by
676 * getting the next child. If the previous endpoint is NULL this
677 * will return the first child.
679 endpoint = of_get_next_child(port, prev);
685 /* No more endpoints under this port, try the next one. */
689 port = of_get_next_child(parent, port);
692 } while (!of_node_name_eq(port, "port"));
695 EXPORT_SYMBOL(of_graph_get_next_endpoint);
698 * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers
699 * @parent: pointer to the parent device node
700 * @port_reg: identifier (value of reg property) of the parent port node
701 * @reg: identifier (value of reg property) of the endpoint node
703 * Return: An 'endpoint' node pointer which is identified by reg and at the same
704 * is the child of a port node identified by port_reg. reg and port_reg are
705 * ignored when they are -1. Use of_node_put() on the pointer when done.
707 struct device_node *of_graph_get_endpoint_by_regs(
708 const struct device_node *parent, int port_reg, int reg)
710 struct of_endpoint endpoint;
711 struct device_node *node = NULL;
713 for_each_endpoint_of_node(parent, node) {
714 of_graph_parse_endpoint(node, &endpoint);
715 if (((port_reg == -1) || (endpoint.port == port_reg)) &&
716 ((reg == -1) || (endpoint.id == reg)))
722 EXPORT_SYMBOL(of_graph_get_endpoint_by_regs);
725 * of_graph_get_remote_endpoint() - get remote endpoint node
726 * @node: pointer to a local endpoint device_node
728 * Return: Remote endpoint node associated with remote endpoint node linked
729 * to @node. Use of_node_put() on it when done.
731 struct device_node *of_graph_get_remote_endpoint(const struct device_node *node)
733 /* Get remote endpoint node. */
734 return of_parse_phandle(node, "remote-endpoint", 0);
736 EXPORT_SYMBOL(of_graph_get_remote_endpoint);
739 * of_graph_get_port_parent() - get port's parent node
740 * @node: pointer to a local endpoint device_node
742 * Return: device node associated with endpoint node linked
743 * to @node. Use of_node_put() on it when done.
745 struct device_node *of_graph_get_port_parent(struct device_node *node)
753 * Preserve usecount for passed in node as of_get_next_parent()
754 * will do of_node_put() on it.
758 /* Walk 3 levels up only if there is 'ports' node. */
759 for (depth = 3; depth && node; depth--) {
760 node = of_get_next_parent(node);
761 if (depth == 2 && !of_node_name_eq(node, "ports"))
766 EXPORT_SYMBOL(of_graph_get_port_parent);
769 * of_graph_get_remote_port_parent() - get remote port's parent node
770 * @node: pointer to a local endpoint device_node
772 * Return: Remote device node associated with remote endpoint node linked
773 * to @node. Use of_node_put() on it when done.
775 struct device_node *of_graph_get_remote_port_parent(
776 const struct device_node *node)
778 struct device_node *np, *pp;
780 /* Get remote endpoint node. */
781 np = of_graph_get_remote_endpoint(node);
783 pp = of_graph_get_port_parent(np);
789 EXPORT_SYMBOL(of_graph_get_remote_port_parent);
792 * of_graph_get_remote_port() - get remote port node
793 * @node: pointer to a local endpoint device_node
795 * Return: Remote port node associated with remote endpoint node linked
796 * to @node. Use of_node_put() on it when done.
798 struct device_node *of_graph_get_remote_port(const struct device_node *node)
800 struct device_node *np;
802 /* Get remote endpoint node. */
803 np = of_graph_get_remote_endpoint(node);
806 return of_get_next_parent(np);
808 EXPORT_SYMBOL(of_graph_get_remote_port);
810 int of_graph_get_endpoint_count(const struct device_node *np)
812 struct device_node *endpoint;
815 for_each_endpoint_of_node(np, endpoint)
820 EXPORT_SYMBOL(of_graph_get_endpoint_count);
823 * of_graph_get_remote_node() - get remote parent device_node for given port/endpoint
824 * @node: pointer to parent device_node containing graph port/endpoint
825 * @port: identifier (value of reg property) of the parent port node
826 * @endpoint: identifier (value of reg property) of the endpoint node
828 * Return: Remote device node associated with remote endpoint node linked
829 * to @node. Use of_node_put() on it when done.
831 struct device_node *of_graph_get_remote_node(const struct device_node *node,
832 u32 port, u32 endpoint)
834 struct device_node *endpoint_node, *remote;
836 endpoint_node = of_graph_get_endpoint_by_regs(node, port, endpoint);
837 if (!endpoint_node) {
838 pr_debug("no valid endpoint (%d, %d) for node %pOF\n",
839 port, endpoint, node);
843 remote = of_graph_get_remote_port_parent(endpoint_node);
844 of_node_put(endpoint_node);
846 pr_debug("no valid remote node\n");
850 if (!of_device_is_available(remote)) {
851 pr_debug("not available for remote node\n");
858 EXPORT_SYMBOL(of_graph_get_remote_node);
860 static struct fwnode_handle *of_fwnode_get(struct fwnode_handle *fwnode)
862 return of_fwnode_handle(of_node_get(to_of_node(fwnode)));
865 static void of_fwnode_put(struct fwnode_handle *fwnode)
867 of_node_put(to_of_node(fwnode));
870 static bool of_fwnode_device_is_available(const struct fwnode_handle *fwnode)
872 return of_device_is_available(to_of_node(fwnode));
875 static bool of_fwnode_property_present(const struct fwnode_handle *fwnode,
876 const char *propname)
878 return of_property_read_bool(to_of_node(fwnode), propname);
881 static int of_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
882 const char *propname,
883 unsigned int elem_size, void *val,
886 const struct device_node *node = to_of_node(fwnode);
889 return of_property_count_elems_of_size(node, propname,
894 return of_property_read_u8_array(node, propname, val, nval);
896 return of_property_read_u16_array(node, propname, val, nval);
898 return of_property_read_u32_array(node, propname, val, nval);
900 return of_property_read_u64_array(node, propname, val, nval);
907 of_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
908 const char *propname, const char **val,
911 const struct device_node *node = to_of_node(fwnode);
914 of_property_read_string_array(node, propname, val, nval) :
915 of_property_count_strings(node, propname);
918 static const char *of_fwnode_get_name(const struct fwnode_handle *fwnode)
920 return kbasename(to_of_node(fwnode)->full_name);
923 static const char *of_fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
925 /* Root needs no prefix here (its name is "/"). */
926 if (!to_of_node(fwnode)->parent)
932 static struct fwnode_handle *
933 of_fwnode_get_parent(const struct fwnode_handle *fwnode)
935 return of_fwnode_handle(of_get_parent(to_of_node(fwnode)));
938 static struct fwnode_handle *
939 of_fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
940 struct fwnode_handle *child)
942 return of_fwnode_handle(of_get_next_available_child(to_of_node(fwnode),
946 static struct fwnode_handle *
947 of_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
948 const char *childname)
950 const struct device_node *node = to_of_node(fwnode);
951 struct device_node *child;
953 for_each_available_child_of_node(node, child)
954 if (of_node_name_eq(child, childname))
955 return of_fwnode_handle(child);
961 of_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
962 const char *prop, const char *nargs_prop,
963 unsigned int nargs, unsigned int index,
964 struct fwnode_reference_args *args)
966 struct of_phandle_args of_args;
971 ret = of_parse_phandle_with_args(to_of_node(fwnode), prop,
972 nargs_prop, index, &of_args);
974 ret = of_parse_phandle_with_fixed_args(to_of_node(fwnode), prop,
975 nargs, index, &of_args);
979 of_node_put(of_args.np);
983 args->nargs = of_args.args_count;
984 args->fwnode = of_fwnode_handle(of_args.np);
986 for (i = 0; i < NR_FWNODE_REFERENCE_ARGS; i++)
987 args->args[i] = i < of_args.args_count ? of_args.args[i] : 0;
992 static struct fwnode_handle *
993 of_fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
994 struct fwnode_handle *prev)
996 return of_fwnode_handle(of_graph_get_next_endpoint(to_of_node(fwnode),
1000 static struct fwnode_handle *
1001 of_fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
1003 return of_fwnode_handle(
1004 of_graph_get_remote_endpoint(to_of_node(fwnode)));
1007 static struct fwnode_handle *
1008 of_fwnode_graph_get_port_parent(struct fwnode_handle *fwnode)
1010 struct device_node *np;
1012 /* Get the parent of the port */
1013 np = of_get_parent(to_of_node(fwnode));
1017 /* Is this the "ports" node? If not, it's the port parent. */
1018 if (!of_node_name_eq(np, "ports"))
1019 return of_fwnode_handle(np);
1021 return of_fwnode_handle(of_get_next_parent(np));
1024 static int of_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
1025 struct fwnode_endpoint *endpoint)
1027 const struct device_node *node = to_of_node(fwnode);
1028 struct device_node *port_node = of_get_parent(node);
1030 endpoint->local_fwnode = fwnode;
1032 of_property_read_u32(port_node, "reg", &endpoint->port);
1033 of_property_read_u32(node, "reg", &endpoint->id);
1035 of_node_put(port_node);
1041 of_fwnode_device_get_match_data(const struct fwnode_handle *fwnode,
1042 const struct device *dev)
1044 return of_device_get_match_data(dev);
1047 static bool of_is_ancestor_of(struct device_node *test_ancestor,
1048 struct device_node *child)
1052 if (child == test_ancestor) {
1056 child = of_get_next_parent(child);
1061 static struct device_node *of_get_compat_node(struct device_node *np)
1066 if (!of_device_is_available(np)) {
1071 if (of_find_property(np, "compatible", NULL))
1074 np = of_get_next_parent(np);
1081 * of_link_to_phandle - Add fwnode link to supplier from supplier phandle
1082 * @con_np: consumer device tree node
1083 * @sup_np: supplier device tree node
1085 * Given a phandle to a supplier device tree node (@sup_np), this function
1086 * finds the device that owns the supplier device tree node and creates a
1087 * device link from @dev consumer device to the supplier device. This function
1088 * doesn't create device links for invalid scenarios such as trying to create a
1089 * link with a parent device as the consumer of its child device. In such
1090 * cases, it returns an error.
1093 * - 0 if fwnode link successfully created to supplier
1094 * - -EINVAL if the supplier link is invalid and should not be created
1095 * - -ENODEV if struct device will never be create for supplier
1097 static int of_link_to_phandle(struct device_node *con_np,
1098 struct device_node *sup_np)
1100 struct device *sup_dev;
1101 struct device_node *tmp_np = sup_np;
1104 * Find the device node that contains the supplier phandle. It may be
1105 * @sup_np or it may be an ancestor of @sup_np.
1107 sup_np = of_get_compat_node(sup_np);
1109 pr_debug("Not linking %pOFP to %pOFP - No device\n",
1115 * Don't allow linking a device node as a consumer of one of its
1116 * descendant nodes. By definition, a child node can't be a functional
1117 * dependency for the parent node.
1119 if (of_is_ancestor_of(con_np, sup_np)) {
1120 pr_debug("Not linking %pOFP to %pOFP - is descendant\n",
1122 of_node_put(sup_np);
1127 * Don't create links to "early devices" that won't have struct devices
1130 sup_dev = get_dev_from_fwnode(&sup_np->fwnode);
1132 (of_node_check_flag(sup_np, OF_POPULATED) ||
1133 sup_np->fwnode.flags & FWNODE_FLAG_NOT_DEVICE)) {
1134 pr_debug("Not linking %pOFP to %pOFP - No struct device\n",
1136 of_node_put(sup_np);
1139 put_device(sup_dev);
1141 fwnode_link_add(of_fwnode_handle(con_np), of_fwnode_handle(sup_np));
1142 of_node_put(sup_np);
1148 * parse_prop_cells - Property parsing function for suppliers
1150 * @np: Pointer to device tree node containing a list
1151 * @prop_name: Name of property to be parsed. Expected to hold phandle values
1152 * @index: For properties holding a list of phandles, this is the index
1154 * @list_name: Property name that is known to contain list of phandle(s) to
1156 * @cells_name: property name that specifies phandles' arguments count
1158 * This is a helper function to parse properties that have a known fixed name
1159 * and are a list of phandles and phandle arguments.
1162 * - phandle node pointer with refcount incremented. Caller must of_node_put()
1164 * - NULL if no phandle found at index
1166 static struct device_node *parse_prop_cells(struct device_node *np,
1167 const char *prop_name, int index,
1168 const char *list_name,
1169 const char *cells_name)
1171 struct of_phandle_args sup_args;
1173 if (strcmp(prop_name, list_name))
1176 if (of_parse_phandle_with_args(np, list_name, cells_name, index,
1183 #define DEFINE_SIMPLE_PROP(fname, name, cells) \
1184 static struct device_node *parse_##fname(struct device_node *np, \
1185 const char *prop_name, int index) \
1187 return parse_prop_cells(np, prop_name, index, name, cells); \
1190 static int strcmp_suffix(const char *str, const char *suffix)
1192 unsigned int len, suffix_len;
1195 suffix_len = strlen(suffix);
1196 if (len <= suffix_len)
1198 return strcmp(str + len - suffix_len, suffix);
1202 * parse_suffix_prop_cells - Suffix property parsing function for suppliers
1204 * @np: Pointer to device tree node containing a list
1205 * @prop_name: Name of property to be parsed. Expected to hold phandle values
1206 * @index: For properties holding a list of phandles, this is the index
1208 * @suffix: Property suffix that is known to contain list of phandle(s) to
1210 * @cells_name: property name that specifies phandles' arguments count
1212 * This is a helper function to parse properties that have a known fixed suffix
1213 * and are a list of phandles and phandle arguments.
1216 * - phandle node pointer with refcount incremented. Caller must of_node_put()
1218 * - NULL if no phandle found at index
1220 static struct device_node *parse_suffix_prop_cells(struct device_node *np,
1221 const char *prop_name, int index,
1223 const char *cells_name)
1225 struct of_phandle_args sup_args;
1227 if (strcmp_suffix(prop_name, suffix))
1230 if (of_parse_phandle_with_args(np, prop_name, cells_name, index,
1237 #define DEFINE_SUFFIX_PROP(fname, suffix, cells) \
1238 static struct device_node *parse_##fname(struct device_node *np, \
1239 const char *prop_name, int index) \
1241 return parse_suffix_prop_cells(np, prop_name, index, suffix, cells); \
1245 * struct supplier_bindings - Property parsing functions for suppliers
1247 * @parse_prop: function name
1248 * parse_prop() finds the node corresponding to a supplier phandle
1249 * @parse_prop.np: Pointer to device node holding supplier phandle property
1250 * @parse_prop.prop_name: Name of property holding a phandle value
1251 * @parse_prop.index: For properties holding a list of phandles, this is the
1252 * index into the list
1253 * @optional: Describes whether a supplier is mandatory or not
1254 * @node_not_dev: The consumer node containing the property is never a device.
1257 * parse_prop() return values are
1258 * - phandle node pointer with refcount incremented. Caller must of_node_put()
1260 * - NULL if no phandle found at index
1262 struct supplier_bindings {
1263 struct device_node *(*parse_prop)(struct device_node *np,
1264 const char *prop_name, int index);
1269 DEFINE_SIMPLE_PROP(clocks, "clocks", "#clock-cells")
1270 DEFINE_SIMPLE_PROP(interconnects, "interconnects", "#interconnect-cells")
1271 DEFINE_SIMPLE_PROP(iommus, "iommus", "#iommu-cells")
1272 DEFINE_SIMPLE_PROP(mboxes, "mboxes", "#mbox-cells")
1273 DEFINE_SIMPLE_PROP(io_channels, "io-channel", "#io-channel-cells")
1274 DEFINE_SIMPLE_PROP(interrupt_parent, "interrupt-parent", NULL)
1275 DEFINE_SIMPLE_PROP(dmas, "dmas", "#dma-cells")
1276 DEFINE_SIMPLE_PROP(power_domains, "power-domains", "#power-domain-cells")
1277 DEFINE_SIMPLE_PROP(hwlocks, "hwlocks", "#hwlock-cells")
1278 DEFINE_SIMPLE_PROP(extcon, "extcon", NULL)
1279 DEFINE_SIMPLE_PROP(nvmem_cells, "nvmem-cells", NULL)
1280 DEFINE_SIMPLE_PROP(phys, "phys", "#phy-cells")
1281 DEFINE_SIMPLE_PROP(wakeup_parent, "wakeup-parent", NULL)
1282 DEFINE_SIMPLE_PROP(pinctrl0, "pinctrl-0", NULL)
1283 DEFINE_SIMPLE_PROP(pinctrl1, "pinctrl-1", NULL)
1284 DEFINE_SIMPLE_PROP(pinctrl2, "pinctrl-2", NULL)
1285 DEFINE_SIMPLE_PROP(pinctrl3, "pinctrl-3", NULL)
1286 DEFINE_SIMPLE_PROP(pinctrl4, "pinctrl-4", NULL)
1287 DEFINE_SIMPLE_PROP(pinctrl5, "pinctrl-5", NULL)
1288 DEFINE_SIMPLE_PROP(pinctrl6, "pinctrl-6", NULL)
1289 DEFINE_SIMPLE_PROP(pinctrl7, "pinctrl-7", NULL)
1290 DEFINE_SIMPLE_PROP(pinctrl8, "pinctrl-8", NULL)
1291 DEFINE_SIMPLE_PROP(remote_endpoint, "remote-endpoint", NULL)
1292 DEFINE_SIMPLE_PROP(pwms, "pwms", "#pwm-cells")
1293 DEFINE_SIMPLE_PROP(resets, "resets", "#reset-cells")
1294 DEFINE_SIMPLE_PROP(leds, "leds", NULL)
1295 DEFINE_SIMPLE_PROP(backlight, "backlight", NULL)
1296 DEFINE_SUFFIX_PROP(regulators, "-supply", NULL)
1297 DEFINE_SUFFIX_PROP(gpio, "-gpio", "#gpio-cells")
1299 static struct device_node *parse_gpios(struct device_node *np,
1300 const char *prop_name, int index)
1302 if (!strcmp_suffix(prop_name, ",nr-gpios"))
1305 return parse_suffix_prop_cells(np, prop_name, index, "-gpios",
1309 static struct device_node *parse_iommu_maps(struct device_node *np,
1310 const char *prop_name, int index)
1312 if (strcmp(prop_name, "iommu-map"))
1315 return of_parse_phandle(np, prop_name, (index * 4) + 1);
1318 static struct device_node *parse_gpio_compat(struct device_node *np,
1319 const char *prop_name, int index)
1321 struct of_phandle_args sup_args;
1323 if (strcmp(prop_name, "gpio") && strcmp(prop_name, "gpios"))
1327 * Ignore node with gpio-hog property since its gpios are all provided
1330 if (of_find_property(np, "gpio-hog", NULL))
1333 if (of_parse_phandle_with_args(np, prop_name, "#gpio-cells", index,
1340 static struct device_node *parse_interrupts(struct device_node *np,
1341 const char *prop_name, int index)
1343 struct of_phandle_args sup_args;
1345 if (!IS_ENABLED(CONFIG_OF_IRQ) || IS_ENABLED(CONFIG_PPC))
1348 if (strcmp(prop_name, "interrupts") &&
1349 strcmp(prop_name, "interrupts-extended"))
1352 return of_irq_parse_one(np, index, &sup_args) ? NULL : sup_args.np;
1355 static const struct supplier_bindings of_supplier_bindings[] = {
1356 { .parse_prop = parse_clocks, },
1357 { .parse_prop = parse_interconnects, },
1358 { .parse_prop = parse_iommus, .optional = true, },
1359 { .parse_prop = parse_iommu_maps, .optional = true, },
1360 { .parse_prop = parse_mboxes, },
1361 { .parse_prop = parse_io_channels, },
1362 { .parse_prop = parse_interrupt_parent, },
1363 { .parse_prop = parse_dmas, .optional = true, },
1364 { .parse_prop = parse_power_domains, },
1365 { .parse_prop = parse_hwlocks, },
1366 { .parse_prop = parse_extcon, },
1367 { .parse_prop = parse_nvmem_cells, },
1368 { .parse_prop = parse_phys, },
1369 { .parse_prop = parse_wakeup_parent, },
1370 { .parse_prop = parse_pinctrl0, },
1371 { .parse_prop = parse_pinctrl1, },
1372 { .parse_prop = parse_pinctrl2, },
1373 { .parse_prop = parse_pinctrl3, },
1374 { .parse_prop = parse_pinctrl4, },
1375 { .parse_prop = parse_pinctrl5, },
1376 { .parse_prop = parse_pinctrl6, },
1377 { .parse_prop = parse_pinctrl7, },
1378 { .parse_prop = parse_pinctrl8, },
1379 { .parse_prop = parse_remote_endpoint, .node_not_dev = true, },
1380 { .parse_prop = parse_pwms, },
1381 { .parse_prop = parse_resets, },
1382 { .parse_prop = parse_leds, },
1383 { .parse_prop = parse_backlight, },
1384 { .parse_prop = parse_gpio_compat, },
1385 { .parse_prop = parse_interrupts, },
1386 { .parse_prop = parse_regulators, },
1387 { .parse_prop = parse_gpio, },
1388 { .parse_prop = parse_gpios, },
1393 * of_link_property - Create device links to suppliers listed in a property
1394 * @con_np: The consumer device tree node which contains the property
1395 * @prop_name: Name of property to be parsed
1397 * This function checks if the property @prop_name that is present in the
1398 * @con_np device tree node is one of the known common device tree bindings
1399 * that list phandles to suppliers. If @prop_name isn't one, this function
1400 * doesn't do anything.
1402 * If @prop_name is one, this function attempts to create fwnode links from the
1403 * consumer device tree node @con_np to all the suppliers device tree nodes
1404 * listed in @prop_name.
1406 * Any failed attempt to create a fwnode link will NOT result in an immediate
1407 * return. of_link_property() must create links to all the available supplier
1408 * device tree nodes even when attempts to create a link to one or more
1411 static int of_link_property(struct device_node *con_np, const char *prop_name)
1413 struct device_node *phandle;
1414 const struct supplier_bindings *s = of_supplier_bindings;
1416 bool matched = false;
1418 /* Do not stop at first failed link, link all available suppliers. */
1419 while (!matched && s->parse_prop) {
1420 if (s->optional && !fw_devlink_is_strict()) {
1425 while ((phandle = s->parse_prop(con_np, prop_name, i))) {
1426 struct device_node *con_dev_np;
1428 con_dev_np = s->node_not_dev
1429 ? of_get_compat_node(con_np)
1430 : of_node_get(con_np);
1433 of_link_to_phandle(con_dev_np, phandle);
1434 of_node_put(phandle);
1435 of_node_put(con_dev_np);
1442 static int of_fwnode_add_links(struct fwnode_handle *fwnode)
1445 struct device_node *con_np = to_of_node(fwnode);
1447 if (IS_ENABLED(CONFIG_X86))
1453 for_each_property_of_node(con_np, p)
1454 of_link_property(con_np, p->name);
1459 const struct fwnode_operations of_fwnode_ops = {
1460 .get = of_fwnode_get,
1461 .put = of_fwnode_put,
1462 .device_is_available = of_fwnode_device_is_available,
1463 .device_get_match_data = of_fwnode_device_get_match_data,
1464 .property_present = of_fwnode_property_present,
1465 .property_read_int_array = of_fwnode_property_read_int_array,
1466 .property_read_string_array = of_fwnode_property_read_string_array,
1467 .get_name = of_fwnode_get_name,
1468 .get_name_prefix = of_fwnode_get_name_prefix,
1469 .get_parent = of_fwnode_get_parent,
1470 .get_next_child_node = of_fwnode_get_next_child_node,
1471 .get_named_child_node = of_fwnode_get_named_child_node,
1472 .get_reference_args = of_fwnode_get_reference_args,
1473 .graph_get_next_endpoint = of_fwnode_graph_get_next_endpoint,
1474 .graph_get_remote_endpoint = of_fwnode_graph_get_remote_endpoint,
1475 .graph_get_port_parent = of_fwnode_graph_get_port_parent,
1476 .graph_parse_endpoint = of_fwnode_graph_parse_endpoint,
1477 .add_links = of_fwnode_add_links,
1479 EXPORT_SYMBOL_GPL(of_fwnode_ops);