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/string.h>
28 #include <linux/moduleparam.h>
30 #include "of_private.h"
33 * of_graph_is_present() - check graph's presence
34 * @node: pointer to device_node containing graph port
36 * Return: True if @node has a port or ports (with a port) sub-node,
39 bool of_graph_is_present(const struct device_node *node)
41 struct device_node *ports, *port;
43 ports = of_get_child_by_name(node, "ports");
47 port = of_get_child_by_name(node, "port");
53 EXPORT_SYMBOL(of_graph_is_present);
56 * of_property_count_elems_of_size - Count the number of elements in a property
58 * @np: device node from which the property value is to be read.
59 * @propname: name of the property to be searched.
60 * @elem_size: size of the individual element
62 * Search for a property in a device node and count the number of elements of
63 * size elem_size in it.
65 * Return: The number of elements on sucess, -EINVAL if the property does not
66 * exist or its length does not match a multiple of elem_size and -ENODATA if
67 * the property does not have a value.
69 int of_property_count_elems_of_size(const struct device_node *np,
70 const char *propname, int elem_size)
72 struct property *prop = of_find_property(np, propname, NULL);
79 if (prop->length % elem_size != 0) {
80 pr_err("size of %s in node %pOF is not a multiple of %d\n",
81 propname, np, elem_size);
85 return prop->length / elem_size;
87 EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
90 * of_find_property_value_of_size
92 * @np: device node from which the property value is to be read.
93 * @propname: name of the property to be searched.
94 * @min: minimum allowed length of property value
95 * @max: maximum allowed length of property value (0 means unlimited)
96 * @len: if !=NULL, actual length is written to here
98 * Search for a property in a device node and valid the requested size.
100 * Return: The property value on success, -EINVAL if the property does not
101 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
102 * property data is too small or too large.
105 static void *of_find_property_value_of_size(const struct device_node *np,
106 const char *propname, u32 min, u32 max, size_t *len)
108 struct property *prop = of_find_property(np, propname, NULL);
111 return ERR_PTR(-EINVAL);
113 return ERR_PTR(-ENODATA);
114 if (prop->length < min)
115 return ERR_PTR(-EOVERFLOW);
116 if (max && prop->length > max)
117 return ERR_PTR(-EOVERFLOW);
126 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
128 * @np: device node from which the property value is to be read.
129 * @propname: name of the property to be searched.
130 * @index: index of the u32 in the list of values
131 * @out_value: pointer to return value, modified only if no error.
133 * Search for a property in a device node and read nth 32-bit value from
136 * Return: 0 on success, -EINVAL if the property does not exist,
137 * -ENODATA if property does not have a value, and -EOVERFLOW if the
138 * property data isn't large enough.
140 * The out_value is modified only if a valid u32 value can be decoded.
142 int of_property_read_u32_index(const struct device_node *np,
143 const char *propname,
144 u32 index, u32 *out_value)
146 const u32 *val = of_find_property_value_of_size(np, propname,
147 ((index + 1) * sizeof(*out_value)),
154 *out_value = be32_to_cpup(((__be32 *)val) + index);
157 EXPORT_SYMBOL_GPL(of_property_read_u32_index);
160 * of_property_read_u64_index - Find and read a u64 from a multi-value property.
162 * @np: device node from which the property value is to be read.
163 * @propname: name of the property to be searched.
164 * @index: index of the u64 in the list of values
165 * @out_value: pointer to return value, modified only if no error.
167 * Search for a property in a device node and read nth 64-bit value from
170 * Return: 0 on success, -EINVAL if the property does not exist,
171 * -ENODATA if property does not have a value, and -EOVERFLOW if the
172 * property data isn't large enough.
174 * The out_value is modified only if a valid u64 value can be decoded.
176 int of_property_read_u64_index(const struct device_node *np,
177 const char *propname,
178 u32 index, u64 *out_value)
180 const u64 *val = of_find_property_value_of_size(np, propname,
181 ((index + 1) * sizeof(*out_value)),
187 *out_value = be64_to_cpup(((__be64 *)val) + index);
190 EXPORT_SYMBOL_GPL(of_property_read_u64_index);
193 * of_property_read_variable_u8_array - Find and read an array of u8 from a
194 * property, with bounds on the minimum and maximum array size.
196 * @np: device node from which the property value is to be read.
197 * @propname: name of the property to be searched.
198 * @out_values: pointer to found values.
199 * @sz_min: minimum number of array elements to read
200 * @sz_max: maximum number of array elements to read, if zero there is no
201 * upper limit on the number of elements in the dts entry but only
202 * sz_min will be read.
204 * Search for a property in a device node and read 8-bit value(s) from
207 * dts entry of array should be like:
208 * ``property = /bits/ 8 <0x50 0x60 0x70>;``
210 * Return: The number of elements read on success, -EINVAL if the property
211 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
212 * if the property data is smaller than sz_min or longer than sz_max.
214 * The out_values is modified only if a valid u8 value can be decoded.
216 int of_property_read_variable_u8_array(const struct device_node *np,
217 const char *propname, u8 *out_values,
218 size_t sz_min, size_t sz_max)
221 const u8 *val = of_find_property_value_of_size(np, propname,
222 (sz_min * sizeof(*out_values)),
223 (sz_max * sizeof(*out_values)),
232 sz /= sizeof(*out_values);
236 *out_values++ = *val++;
240 EXPORT_SYMBOL_GPL(of_property_read_variable_u8_array);
243 * of_property_read_variable_u16_array - Find and read an array of u16 from a
244 * property, with bounds on the minimum and maximum array size.
246 * @np: device node from which the property value is to be read.
247 * @propname: name of the property to be searched.
248 * @out_values: pointer to found values.
249 * @sz_min: minimum number of array elements to read
250 * @sz_max: maximum number of array elements to read, if zero there is no
251 * upper limit on the number of elements in the dts entry but only
252 * sz_min will be read.
254 * Search for a property in a device node and read 16-bit value(s) from
257 * dts entry of array should be like:
258 * ``property = /bits/ 16 <0x5000 0x6000 0x7000>;``
260 * Return: The number of elements read on success, -EINVAL if the property
261 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
262 * if the property data is smaller than sz_min or longer than sz_max.
264 * The out_values is modified only if a valid u16 value can be decoded.
266 int of_property_read_variable_u16_array(const struct device_node *np,
267 const char *propname, u16 *out_values,
268 size_t sz_min, size_t sz_max)
271 const __be16 *val = of_find_property_value_of_size(np, propname,
272 (sz_min * sizeof(*out_values)),
273 (sz_max * sizeof(*out_values)),
282 sz /= sizeof(*out_values);
286 *out_values++ = be16_to_cpup(val++);
290 EXPORT_SYMBOL_GPL(of_property_read_variable_u16_array);
293 * of_property_read_variable_u32_array - Find and read an array of 32 bit
294 * integers from a property, with bounds on the minimum and maximum array size.
296 * @np: device node from which the property value is to be read.
297 * @propname: name of the property to be searched.
298 * @out_values: pointer to return found values.
299 * @sz_min: minimum number of array elements to read
300 * @sz_max: maximum number of array elements to read, if zero there is no
301 * upper limit on the number of elements in the dts entry but only
302 * sz_min will be read.
304 * Search for a property in a device node and read 32-bit value(s) from
307 * Return: The number of elements read on success, -EINVAL if the property
308 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
309 * if the property data is smaller than sz_min or longer than sz_max.
311 * The out_values is modified only if a valid u32 value can be decoded.
313 int of_property_read_variable_u32_array(const struct device_node *np,
314 const char *propname, u32 *out_values,
315 size_t sz_min, size_t sz_max)
318 const __be32 *val = of_find_property_value_of_size(np, propname,
319 (sz_min * sizeof(*out_values)),
320 (sz_max * sizeof(*out_values)),
329 sz /= sizeof(*out_values);
333 *out_values++ = be32_to_cpup(val++);
337 EXPORT_SYMBOL_GPL(of_property_read_variable_u32_array);
340 * of_property_read_u64 - Find and read a 64 bit integer from a property
341 * @np: device node from which the property value is to be read.
342 * @propname: name of the property to be searched.
343 * @out_value: pointer to return value, modified only if return value is 0.
345 * Search for a property in a device node and read a 64-bit value from
348 * Return: 0 on success, -EINVAL if the property does not exist,
349 * -ENODATA if property does not have a value, and -EOVERFLOW if the
350 * property data isn't large enough.
352 * The out_value is modified only if a valid u64 value can be decoded.
354 int of_property_read_u64(const struct device_node *np, const char *propname,
357 const __be32 *val = of_find_property_value_of_size(np, propname,
365 *out_value = of_read_number(val, 2);
368 EXPORT_SYMBOL_GPL(of_property_read_u64);
371 * of_property_read_variable_u64_array - Find and read an array of 64 bit
372 * integers from a property, with bounds on the minimum and maximum array size.
374 * @np: device node from which the property value is to be read.
375 * @propname: name of the property to be searched.
376 * @out_values: pointer to found values.
377 * @sz_min: minimum number of array elements to read
378 * @sz_max: maximum number of array elements to read, if zero there is no
379 * upper limit on the number of elements in the dts entry but only
380 * sz_min will be read.
382 * Search for a property in a device node and read 64-bit value(s) from
385 * Return: The number of elements read on success, -EINVAL if the property
386 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
387 * if the property data is smaller than sz_min or longer than sz_max.
389 * The out_values is modified only if a valid u64 value can be decoded.
391 int of_property_read_variable_u64_array(const struct device_node *np,
392 const char *propname, u64 *out_values,
393 size_t sz_min, size_t sz_max)
396 const __be32 *val = of_find_property_value_of_size(np, propname,
397 (sz_min * sizeof(*out_values)),
398 (sz_max * sizeof(*out_values)),
407 sz /= sizeof(*out_values);
411 *out_values++ = of_read_number(val, 2);
417 EXPORT_SYMBOL_GPL(of_property_read_variable_u64_array);
420 * of_property_read_string - Find and read a string from a property
421 * @np: device node from which the property value is to be read.
422 * @propname: name of the property to be searched.
423 * @out_string: pointer to null terminated return string, modified only if
426 * Search for a property in a device tree node and retrieve a null
427 * terminated string value (pointer to data, not a copy).
429 * Return: 0 on success, -EINVAL if the property does not exist, -ENODATA if
430 * property does not have a value, and -EILSEQ if the string is not
431 * null-terminated within the length of the property data.
433 * The out_string pointer is modified only if a valid string can be decoded.
435 int of_property_read_string(const struct device_node *np, const char *propname,
436 const char **out_string)
438 const struct property *prop = of_find_property(np, propname, NULL);
443 if (strnlen(prop->value, prop->length) >= prop->length)
445 *out_string = prop->value;
448 EXPORT_SYMBOL_GPL(of_property_read_string);
451 * of_property_match_string() - Find string in a list and return index
452 * @np: pointer to node containing string list property
453 * @propname: string list property name
454 * @string: pointer to string to search for in string list
456 * This function searches a string list property and returns the index
457 * of a specific string value.
459 int of_property_match_string(const struct device_node *np, const char *propname,
462 const struct property *prop = of_find_property(np, propname, NULL);
473 end = p + prop->length;
475 for (i = 0; p < end; i++, p += l) {
476 l = strnlen(p, end - p) + 1;
479 pr_debug("comparing %s with %s\n", string, p);
480 if (strcmp(string, p) == 0)
481 return i; /* Found it; return index */
485 EXPORT_SYMBOL_GPL(of_property_match_string);
488 * of_property_read_string_helper() - Utility helper for parsing string properties
489 * @np: device node from which the property value is to be read.
490 * @propname: name of the property to be searched.
491 * @out_strs: output array of string pointers.
492 * @sz: number of array elements to read.
493 * @skip: Number of strings to skip over at beginning of list.
495 * Don't call this function directly. It is a utility helper for the
496 * of_property_read_string*() family of functions.
498 int of_property_read_string_helper(const struct device_node *np,
499 const char *propname, const char **out_strs,
502 const struct property *prop = of_find_property(np, propname, NULL);
511 end = p + prop->length;
513 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
514 l = strnlen(p, end - p) + 1;
517 if (out_strs && i >= skip)
521 return i <= 0 ? -ENODATA : i;
523 EXPORT_SYMBOL_GPL(of_property_read_string_helper);
525 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
528 const void *curv = cur;
538 curv += sizeof(*cur);
539 if (curv >= prop->value + prop->length)
543 *pu = be32_to_cpup(curv);
546 EXPORT_SYMBOL_GPL(of_prop_next_u32);
548 const char *of_prop_next_string(struct property *prop, const char *cur)
550 const void *curv = cur;
558 curv += strlen(cur) + 1;
559 if (curv >= prop->value + prop->length)
564 EXPORT_SYMBOL_GPL(of_prop_next_string);
567 * of_graph_parse_endpoint() - parse common endpoint node properties
568 * @node: pointer to endpoint device_node
569 * @endpoint: pointer to the OF endpoint data structure
571 * The caller should hold a reference to @node.
573 int of_graph_parse_endpoint(const struct device_node *node,
574 struct of_endpoint *endpoint)
576 struct device_node *port_node = of_get_parent(node);
578 WARN_ONCE(!port_node, "%s(): endpoint %pOF has no parent node\n",
581 memset(endpoint, 0, sizeof(*endpoint));
583 endpoint->local_node = node;
585 * It doesn't matter whether the two calls below succeed.
586 * If they don't then the default value 0 is used.
588 of_property_read_u32(port_node, "reg", &endpoint->port);
589 of_property_read_u32(node, "reg", &endpoint->id);
591 of_node_put(port_node);
595 EXPORT_SYMBOL(of_graph_parse_endpoint);
598 * of_graph_get_port_by_id() - get the port matching a given id
599 * @parent: pointer to the parent device node
600 * @id: id of the port
602 * Return: A 'port' node pointer with refcount incremented. The caller
603 * has to use of_node_put() on it when done.
605 struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id)
607 struct device_node *node, *port;
609 node = of_get_child_by_name(parent, "ports");
613 for_each_child_of_node(parent, port) {
616 if (!of_node_name_eq(port, "port"))
618 of_property_read_u32(port, "reg", &port_id);
627 EXPORT_SYMBOL(of_graph_get_port_by_id);
630 * of_graph_get_next_endpoint() - get next endpoint node
631 * @parent: pointer to the parent device node
632 * @prev: previous endpoint node, or NULL to get first
634 * Return: An 'endpoint' node pointer with refcount incremented. Refcount
635 * of the passed @prev node is decremented.
637 struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
638 struct device_node *prev)
640 struct device_node *endpoint;
641 struct device_node *port;
647 * Start by locating the port node. If no previous endpoint is specified
648 * search for the first port node, otherwise get the previous endpoint
652 struct device_node *node;
654 node = of_get_child_by_name(parent, "ports");
658 port = of_get_child_by_name(parent, "port");
662 pr_err("graph: no port node found in %pOF\n", parent);
666 port = of_get_parent(prev);
667 if (WARN_ONCE(!port, "%s(): endpoint %pOF has no parent node\n",
674 * Now that we have a port node, get the next endpoint by
675 * getting the next child. If the previous endpoint is NULL this
676 * will return the first child.
678 endpoint = of_get_next_child(port, prev);
684 /* No more endpoints under this port, try the next one. */
688 port = of_get_next_child(parent, port);
691 } while (!of_node_name_eq(port, "port"));
694 EXPORT_SYMBOL(of_graph_get_next_endpoint);
697 * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers
698 * @parent: pointer to the parent device node
699 * @port_reg: identifier (value of reg property) of the parent port node
700 * @reg: identifier (value of reg property) of the endpoint node
702 * Return: An 'endpoint' node pointer which is identified by reg and at the same
703 * is the child of a port node identified by port_reg. reg and port_reg are
704 * ignored when they are -1. Use of_node_put() on the pointer when done.
706 struct device_node *of_graph_get_endpoint_by_regs(
707 const struct device_node *parent, int port_reg, int reg)
709 struct of_endpoint endpoint;
710 struct device_node *node = NULL;
712 for_each_endpoint_of_node(parent, node) {
713 of_graph_parse_endpoint(node, &endpoint);
714 if (((port_reg == -1) || (endpoint.port == port_reg)) &&
715 ((reg == -1) || (endpoint.id == reg)))
721 EXPORT_SYMBOL(of_graph_get_endpoint_by_regs);
724 * of_graph_get_remote_endpoint() - get remote endpoint node
725 * @node: pointer to a local endpoint device_node
727 * Return: Remote endpoint node associated with remote endpoint node linked
728 * to @node. Use of_node_put() on it when done.
730 struct device_node *of_graph_get_remote_endpoint(const struct device_node *node)
732 /* Get remote endpoint node. */
733 return of_parse_phandle(node, "remote-endpoint", 0);
735 EXPORT_SYMBOL(of_graph_get_remote_endpoint);
738 * of_graph_get_port_parent() - get port's parent node
739 * @node: pointer to a local endpoint device_node
741 * Return: device node associated with endpoint node linked
742 * to @node. Use of_node_put() on it when done.
744 struct device_node *of_graph_get_port_parent(struct device_node *node)
752 * Preserve usecount for passed in node as of_get_next_parent()
753 * will do of_node_put() on it.
757 /* Walk 3 levels up only if there is 'ports' node. */
758 for (depth = 3; depth && node; depth--) {
759 node = of_get_next_parent(node);
760 if (depth == 2 && !of_node_name_eq(node, "ports"))
765 EXPORT_SYMBOL(of_graph_get_port_parent);
768 * of_graph_get_remote_port_parent() - get remote port's parent node
769 * @node: pointer to a local endpoint device_node
771 * Return: Remote device node associated with remote endpoint node linked
772 * to @node. Use of_node_put() on it when done.
774 struct device_node *of_graph_get_remote_port_parent(
775 const struct device_node *node)
777 struct device_node *np, *pp;
779 /* Get remote endpoint node. */
780 np = of_graph_get_remote_endpoint(node);
782 pp = of_graph_get_port_parent(np);
788 EXPORT_SYMBOL(of_graph_get_remote_port_parent);
791 * of_graph_get_remote_port() - get remote port node
792 * @node: pointer to a local endpoint device_node
794 * Return: Remote port node associated with remote endpoint node linked
795 * to @node. Use of_node_put() on it when done.
797 struct device_node *of_graph_get_remote_port(const struct device_node *node)
799 struct device_node *np;
801 /* Get remote endpoint node. */
802 np = of_graph_get_remote_endpoint(node);
805 return of_get_next_parent(np);
807 EXPORT_SYMBOL(of_graph_get_remote_port);
809 int of_graph_get_endpoint_count(const struct device_node *np)
811 struct device_node *endpoint;
814 for_each_endpoint_of_node(np, endpoint)
819 EXPORT_SYMBOL(of_graph_get_endpoint_count);
822 * of_graph_get_remote_node() - get remote parent device_node for given port/endpoint
823 * @node: pointer to parent device_node containing graph port/endpoint
824 * @port: identifier (value of reg property) of the parent port node
825 * @endpoint: identifier (value of reg property) of the endpoint node
827 * Return: Remote device node associated with remote endpoint node linked
828 * to @node. Use of_node_put() on it when done.
830 struct device_node *of_graph_get_remote_node(const struct device_node *node,
831 u32 port, u32 endpoint)
833 struct device_node *endpoint_node, *remote;
835 endpoint_node = of_graph_get_endpoint_by_regs(node, port, endpoint);
836 if (!endpoint_node) {
837 pr_debug("no valid endpoint (%d, %d) for node %pOF\n",
838 port, endpoint, node);
842 remote = of_graph_get_remote_port_parent(endpoint_node);
843 of_node_put(endpoint_node);
845 pr_debug("no valid remote node\n");
849 if (!of_device_is_available(remote)) {
850 pr_debug("not available for remote node\n");
857 EXPORT_SYMBOL(of_graph_get_remote_node);
859 static struct fwnode_handle *of_fwnode_get(struct fwnode_handle *fwnode)
861 return of_fwnode_handle(of_node_get(to_of_node(fwnode)));
864 static void of_fwnode_put(struct fwnode_handle *fwnode)
866 of_node_put(to_of_node(fwnode));
869 static bool of_fwnode_device_is_available(const struct fwnode_handle *fwnode)
871 return of_device_is_available(to_of_node(fwnode));
874 static bool of_fwnode_property_present(const struct fwnode_handle *fwnode,
875 const char *propname)
877 return of_property_read_bool(to_of_node(fwnode), propname);
880 static int of_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
881 const char *propname,
882 unsigned int elem_size, void *val,
885 const struct device_node *node = to_of_node(fwnode);
888 return of_property_count_elems_of_size(node, propname,
893 return of_property_read_u8_array(node, propname, val, nval);
895 return of_property_read_u16_array(node, propname, val, nval);
897 return of_property_read_u32_array(node, propname, val, nval);
899 return of_property_read_u64_array(node, propname, val, nval);
906 of_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
907 const char *propname, const char **val,
910 const struct device_node *node = to_of_node(fwnode);
913 of_property_read_string_array(node, propname, val, nval) :
914 of_property_count_strings(node, propname);
917 static const char *of_fwnode_get_name(const struct fwnode_handle *fwnode)
919 return kbasename(to_of_node(fwnode)->full_name);
922 static const char *of_fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
924 /* Root needs no prefix here (its name is "/"). */
925 if (!to_of_node(fwnode)->parent)
931 static struct fwnode_handle *
932 of_fwnode_get_parent(const struct fwnode_handle *fwnode)
934 return of_fwnode_handle(of_get_parent(to_of_node(fwnode)));
937 static struct fwnode_handle *
938 of_fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
939 struct fwnode_handle *child)
941 return of_fwnode_handle(of_get_next_available_child(to_of_node(fwnode),
945 static struct fwnode_handle *
946 of_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
947 const char *childname)
949 const struct device_node *node = to_of_node(fwnode);
950 struct device_node *child;
952 for_each_available_child_of_node(node, child)
953 if (of_node_name_eq(child, childname))
954 return of_fwnode_handle(child);
960 of_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
961 const char *prop, const char *nargs_prop,
962 unsigned int nargs, unsigned int index,
963 struct fwnode_reference_args *args)
965 struct of_phandle_args of_args;
970 ret = of_parse_phandle_with_args(to_of_node(fwnode), prop,
971 nargs_prop, index, &of_args);
973 ret = of_parse_phandle_with_fixed_args(to_of_node(fwnode), prop,
974 nargs, index, &of_args);
978 of_node_put(of_args.np);
982 args->nargs = of_args.args_count;
983 args->fwnode = of_fwnode_handle(of_args.np);
985 for (i = 0; i < NR_FWNODE_REFERENCE_ARGS; i++)
986 args->args[i] = i < of_args.args_count ? of_args.args[i] : 0;
991 static struct fwnode_handle *
992 of_fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
993 struct fwnode_handle *prev)
995 return of_fwnode_handle(of_graph_get_next_endpoint(to_of_node(fwnode),
999 static struct fwnode_handle *
1000 of_fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
1002 return of_fwnode_handle(
1003 of_graph_get_remote_endpoint(to_of_node(fwnode)));
1006 static struct fwnode_handle *
1007 of_fwnode_graph_get_port_parent(struct fwnode_handle *fwnode)
1009 struct device_node *np;
1011 /* Get the parent of the port */
1012 np = of_get_parent(to_of_node(fwnode));
1016 /* Is this the "ports" node? If not, it's the port parent. */
1017 if (!of_node_name_eq(np, "ports"))
1018 return of_fwnode_handle(np);
1020 return of_fwnode_handle(of_get_next_parent(np));
1023 static int of_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
1024 struct fwnode_endpoint *endpoint)
1026 const struct device_node *node = to_of_node(fwnode);
1027 struct device_node *port_node = of_get_parent(node);
1029 endpoint->local_fwnode = fwnode;
1031 of_property_read_u32(port_node, "reg", &endpoint->port);
1032 of_property_read_u32(node, "reg", &endpoint->id);
1034 of_node_put(port_node);
1040 of_fwnode_device_get_match_data(const struct fwnode_handle *fwnode,
1041 const struct device *dev)
1043 return of_device_get_match_data(dev);
1046 static bool of_is_ancestor_of(struct device_node *test_ancestor,
1047 struct device_node *child)
1051 if (child == test_ancestor) {
1055 child = of_get_next_parent(child);
1061 * of_get_next_parent_dev - Add device link to supplier from supplier phandle
1062 * @np: device tree node
1064 * Given a device tree node (@np), this function finds its closest ancestor
1065 * device tree node that has a corresponding struct device.
1067 * The caller of this function is expected to call put_device() on the returned
1068 * device when they are done.
1070 static struct device *of_get_next_parent_dev(struct device_node *np)
1072 struct device *dev = NULL;
1076 np = of_get_next_parent(np);
1078 dev = get_dev_from_fwnode(&np->fwnode);
1079 } while (np && !dev);
1085 * of_link_to_phandle - Add device link to supplier from supplier phandle
1086 * @dev: consumer device
1087 * @sup_np: phandle to supplier device tree node
1089 * Given a phandle to a supplier device tree node (@sup_np), this function
1090 * finds the device that owns the supplier device tree node and creates a
1091 * device link from @dev consumer device to the supplier device. This function
1092 * doesn't create device links for invalid scenarios such as trying to create a
1093 * link with a parent device as the consumer of its child device. In such
1094 * cases, it returns an error.
1097 * - 0 if link successfully created to supplier
1098 * - -EAGAIN if linking to the supplier should be reattempted
1099 * - -EINVAL if the supplier link is invalid and should not be created
1100 * - -ENODEV if there is no device that corresponds to the supplier phandle
1102 static int of_link_to_phandle(struct device *dev, struct device_node *sup_np,
1105 struct device *sup_dev, *sup_par_dev;
1107 struct device_node *tmp_np = sup_np;
1109 of_node_get(sup_np);
1111 * Find the device node that contains the supplier phandle. It may be
1112 * @sup_np or it may be an ancestor of @sup_np.
1116 /* Don't allow linking to a disabled supplier */
1117 if (!of_device_is_available(sup_np)) {
1118 of_node_put(sup_np);
1122 if (of_find_property(sup_np, "compatible", NULL))
1125 sup_np = of_get_next_parent(sup_np);
1129 dev_dbg(dev, "Not linking to %pOFP - No device\n", tmp_np);
1134 * Don't allow linking a device node as a consumer of one of its
1135 * descendant nodes. By definition, a child node can't be a functional
1136 * dependency for the parent node.
1138 if (of_is_ancestor_of(dev->of_node, sup_np)) {
1139 dev_dbg(dev, "Not linking to %pOFP - is descendant\n", sup_np);
1140 of_node_put(sup_np);
1143 sup_dev = get_dev_from_fwnode(&sup_np->fwnode);
1144 if (!sup_dev && of_node_check_flag(sup_np, OF_POPULATED)) {
1145 /* Early device without struct device. */
1146 dev_dbg(dev, "Not linking to %pOFP - No struct device\n",
1148 of_node_put(sup_np);
1150 } else if (!sup_dev) {
1152 * DL_FLAG_SYNC_STATE_ONLY doesn't block probing and supports
1153 * cycles. So cycle detection isn't necessary and shouldn't be
1156 if (dl_flags & DL_FLAG_SYNC_STATE_ONLY) {
1157 of_node_put(sup_np);
1161 sup_par_dev = of_get_next_parent_dev(sup_np);
1163 if (sup_par_dev && device_is_dependent(dev, sup_par_dev)) {
1164 /* Cyclic dependency detected, don't try to link */
1165 dev_dbg(dev, "Not linking to %pOFP - cycle detected\n",
1170 * Can't check for cycles or no cycles. So let's try
1176 of_node_put(sup_np);
1177 put_device(sup_par_dev);
1180 of_node_put(sup_np);
1181 if (!device_link_add(dev, sup_dev, dl_flags))
1183 put_device(sup_dev);
1188 * parse_prop_cells - Property parsing function for suppliers
1190 * @np: Pointer to device tree node containing a list
1191 * @prop_name: Name of property to be parsed. Expected to hold phandle values
1192 * @index: For properties holding a list of phandles, this is the index
1194 * @list_name: Property name that is known to contain list of phandle(s) to
1196 * @cells_name: property name that specifies phandles' arguments count
1198 * This is a helper function to parse properties that have a known fixed name
1199 * and are a list of phandles and phandle arguments.
1202 * - phandle node pointer with refcount incremented. Caller must of_node_put()
1204 * - NULL if no phandle found at index
1206 static struct device_node *parse_prop_cells(struct device_node *np,
1207 const char *prop_name, int index,
1208 const char *list_name,
1209 const char *cells_name)
1211 struct of_phandle_args sup_args;
1213 if (strcmp(prop_name, list_name))
1216 if (of_parse_phandle_with_args(np, list_name, cells_name, index,
1223 #define DEFINE_SIMPLE_PROP(fname, name, cells) \
1224 static struct device_node *parse_##fname(struct device_node *np, \
1225 const char *prop_name, int index) \
1227 return parse_prop_cells(np, prop_name, index, name, cells); \
1230 static int strcmp_suffix(const char *str, const char *suffix)
1232 unsigned int len, suffix_len;
1235 suffix_len = strlen(suffix);
1236 if (len <= suffix_len)
1238 return strcmp(str + len - suffix_len, suffix);
1242 * parse_suffix_prop_cells - Suffix property parsing function for suppliers
1244 * @np: Pointer to device tree node containing a list
1245 * @prop_name: Name of property to be parsed. Expected to hold phandle values
1246 * @index: For properties holding a list of phandles, this is the index
1248 * @suffix: Property suffix that is known to contain list of phandle(s) to
1250 * @cells_name: property name that specifies phandles' arguments count
1252 * This is a helper function to parse properties that have a known fixed suffix
1253 * and are a list of phandles and phandle arguments.
1256 * - phandle node pointer with refcount incremented. Caller must of_node_put()
1258 * - NULL if no phandle found at index
1260 static struct device_node *parse_suffix_prop_cells(struct device_node *np,
1261 const char *prop_name, int index,
1263 const char *cells_name)
1265 struct of_phandle_args sup_args;
1267 if (strcmp_suffix(prop_name, suffix))
1270 if (of_parse_phandle_with_args(np, prop_name, cells_name, index,
1277 #define DEFINE_SUFFIX_PROP(fname, suffix, cells) \
1278 static struct device_node *parse_##fname(struct device_node *np, \
1279 const char *prop_name, int index) \
1281 return parse_suffix_prop_cells(np, prop_name, index, suffix, cells); \
1285 * struct supplier_bindings - Property parsing functions for suppliers
1287 * @parse_prop: function name
1288 * parse_prop() finds the node corresponding to a supplier phandle
1289 * @parse_prop.np: Pointer to device node holding supplier phandle property
1290 * @parse_prop.prop_name: Name of property holding a phandle value
1291 * @parse_prop.index: For properties holding a list of phandles, this is the
1292 * index into the list
1295 * parse_prop() return values are
1296 * - phandle node pointer with refcount incremented. Caller must of_node_put()
1298 * - NULL if no phandle found at index
1300 struct supplier_bindings {
1301 struct device_node *(*parse_prop)(struct device_node *np,
1302 const char *prop_name, int index);
1305 DEFINE_SIMPLE_PROP(clocks, "clocks", "#clock-cells")
1306 DEFINE_SIMPLE_PROP(interconnects, "interconnects", "#interconnect-cells")
1307 DEFINE_SIMPLE_PROP(iommus, "iommus", "#iommu-cells")
1308 DEFINE_SIMPLE_PROP(mboxes, "mboxes", "#mbox-cells")
1309 DEFINE_SIMPLE_PROP(io_channels, "io-channels", "#io-channel-cells")
1310 DEFINE_SIMPLE_PROP(interrupt_parent, "interrupt-parent", NULL)
1311 DEFINE_SIMPLE_PROP(dmas, "dmas", "#dma-cells")
1312 DEFINE_SIMPLE_PROP(power_domains, "power-domains", "#power-domain-cells")
1313 DEFINE_SIMPLE_PROP(hwlocks, "hwlocks", "#hwlock-cells")
1314 DEFINE_SIMPLE_PROP(extcon, "extcon", NULL)
1315 DEFINE_SIMPLE_PROP(interrupts_extended, "interrupts-extended",
1317 DEFINE_SIMPLE_PROP(nvmem_cells, "nvmem-cells", NULL)
1318 DEFINE_SIMPLE_PROP(phys, "phys", "#phy-cells")
1319 DEFINE_SIMPLE_PROP(wakeup_parent, "wakeup-parent", NULL)
1320 DEFINE_SIMPLE_PROP(pinctrl0, "pinctrl-0", NULL)
1321 DEFINE_SIMPLE_PROP(pinctrl1, "pinctrl-1", NULL)
1322 DEFINE_SIMPLE_PROP(pinctrl2, "pinctrl-2", NULL)
1323 DEFINE_SIMPLE_PROP(pinctrl3, "pinctrl-3", NULL)
1324 DEFINE_SIMPLE_PROP(pinctrl4, "pinctrl-4", NULL)
1325 DEFINE_SIMPLE_PROP(pinctrl5, "pinctrl-5", NULL)
1326 DEFINE_SIMPLE_PROP(pinctrl6, "pinctrl-6", NULL)
1327 DEFINE_SIMPLE_PROP(pinctrl7, "pinctrl-7", NULL)
1328 DEFINE_SIMPLE_PROP(pinctrl8, "pinctrl-8", NULL)
1329 DEFINE_SUFFIX_PROP(regulators, "-supply", NULL)
1330 DEFINE_SUFFIX_PROP(gpio, "-gpio", "#gpio-cells")
1332 static struct device_node *parse_gpios(struct device_node *np,
1333 const char *prop_name, int index)
1335 if (!strcmp_suffix(prop_name, ",nr-gpios"))
1338 return parse_suffix_prop_cells(np, prop_name, index, "-gpios",
1342 static struct device_node *parse_iommu_maps(struct device_node *np,
1343 const char *prop_name, int index)
1345 if (strcmp(prop_name, "iommu-map"))
1348 return of_parse_phandle(np, prop_name, (index * 4) + 1);
1351 static const struct supplier_bindings of_supplier_bindings[] = {
1352 { .parse_prop = parse_clocks, },
1353 { .parse_prop = parse_interconnects, },
1354 { .parse_prop = parse_iommus, },
1355 { .parse_prop = parse_iommu_maps, },
1356 { .parse_prop = parse_mboxes, },
1357 { .parse_prop = parse_io_channels, },
1358 { .parse_prop = parse_interrupt_parent, },
1359 { .parse_prop = parse_dmas, },
1360 { .parse_prop = parse_power_domains, },
1361 { .parse_prop = parse_hwlocks, },
1362 { .parse_prop = parse_extcon, },
1363 { .parse_prop = parse_interrupts_extended, },
1364 { .parse_prop = parse_nvmem_cells, },
1365 { .parse_prop = parse_phys, },
1366 { .parse_prop = parse_wakeup_parent, },
1367 { .parse_prop = parse_pinctrl0, },
1368 { .parse_prop = parse_pinctrl1, },
1369 { .parse_prop = parse_pinctrl2, },
1370 { .parse_prop = parse_pinctrl3, },
1371 { .parse_prop = parse_pinctrl4, },
1372 { .parse_prop = parse_pinctrl5, },
1373 { .parse_prop = parse_pinctrl6, },
1374 { .parse_prop = parse_pinctrl7, },
1375 { .parse_prop = parse_pinctrl8, },
1376 { .parse_prop = parse_regulators, },
1377 { .parse_prop = parse_gpio, },
1378 { .parse_prop = parse_gpios, },
1383 * of_link_property - Create device links to suppliers listed in a property
1384 * @dev: Consumer device
1385 * @con_np: The consumer device tree node which contains the property
1386 * @prop_name: Name of property to be parsed
1388 * This function checks if the property @prop_name that is present in the
1389 * @con_np device tree node is one of the known common device tree bindings
1390 * that list phandles to suppliers. If @prop_name isn't one, this function
1391 * doesn't do anything.
1393 * If @prop_name is one, this function attempts to create device links from the
1394 * consumer device @dev to all the devices of the suppliers listed in
1397 * Any failed attempt to create a device link will NOT result in an immediate
1398 * return. of_link_property() must create links to all the available supplier
1399 * devices even when attempts to create a link to one or more suppliers fail.
1401 static int of_link_property(struct device *dev, struct device_node *con_np,
1402 const char *prop_name)
1404 struct device_node *phandle;
1405 const struct supplier_bindings *s = of_supplier_bindings;
1407 bool matched = false;
1411 if (dev->of_node == con_np)
1412 dl_flags = fw_devlink_get_flags();
1414 dl_flags = DL_FLAG_SYNC_STATE_ONLY;
1416 /* Do not stop at first failed link, link all available suppliers. */
1417 while (!matched && s->parse_prop) {
1418 while ((phandle = s->parse_prop(con_np, prop_name, i))) {
1421 if (of_link_to_phandle(dev, phandle, dl_flags)
1424 of_node_put(phandle);
1431 static int of_link_to_suppliers(struct device *dev,
1432 struct device_node *con_np)
1434 struct device_node *child;
1438 for_each_property_of_node(con_np, p)
1439 if (of_link_property(dev, con_np, p->name))
1442 for_each_available_child_of_node(con_np, child)
1443 if (of_link_to_suppliers(dev, child) && !ret)
1449 static int of_fwnode_add_links(const struct fwnode_handle *fwnode,
1452 if (unlikely(!is_of_node(fwnode)))
1455 return of_link_to_suppliers(dev, to_of_node(fwnode));
1458 const struct fwnode_operations of_fwnode_ops = {
1459 .get = of_fwnode_get,
1460 .put = of_fwnode_put,
1461 .device_is_available = of_fwnode_device_is_available,
1462 .device_get_match_data = of_fwnode_device_get_match_data,
1463 .property_present = of_fwnode_property_present,
1464 .property_read_int_array = of_fwnode_property_read_int_array,
1465 .property_read_string_array = of_fwnode_property_read_string_array,
1466 .get_name = of_fwnode_get_name,
1467 .get_name_prefix = of_fwnode_get_name_prefix,
1468 .get_parent = of_fwnode_get_parent,
1469 .get_next_child_node = of_fwnode_get_next_child_node,
1470 .get_named_child_node = of_fwnode_get_named_child_node,
1471 .get_reference_args = of_fwnode_get_reference_args,
1472 .graph_get_next_endpoint = of_fwnode_graph_get_next_endpoint,
1473 .graph_get_remote_endpoint = of_fwnode_graph_get_remote_endpoint,
1474 .graph_get_port_parent = of_fwnode_graph_get_port_parent,
1475 .graph_parse_endpoint = of_fwnode_graph_parse_endpoint,
1476 .add_links = of_fwnode_add_links,
1478 EXPORT_SYMBOL_GPL(of_fwnode_ops);