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. Returns number of elements on sucess, -EINVAL if the
65 * property does not exist or its length does not match a multiple of elem_size
66 * and -ENODATA if 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 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.
98 * Returns the property value on success, -EINVAL if the property does not
99 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
100 * property data is too small or too large.
103 static void *of_find_property_value_of_size(const struct device_node *np,
104 const char *propname, u32 min, u32 max, size_t *len)
106 struct property *prop = of_find_property(np, propname, NULL);
109 return ERR_PTR(-EINVAL);
111 return ERR_PTR(-ENODATA);
112 if (prop->length < min)
113 return ERR_PTR(-EOVERFLOW);
114 if (max && prop->length > max)
115 return ERR_PTR(-EOVERFLOW);
124 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
126 * @np: device node from which the property value is to be read.
127 * @propname: name of the property to be searched.
128 * @index: index of the u32 in the list of values
129 * @out_value: pointer to return value, modified only if no error.
131 * Search for a property in a device node and read nth 32-bit value from
132 * it. Returns 0 on success, -EINVAL if the property does not exist,
133 * -ENODATA if property does not have a value, and -EOVERFLOW if the
134 * property data isn't large enough.
136 * The out_value is modified only if a valid u32 value can be decoded.
138 int of_property_read_u32_index(const struct device_node *np,
139 const char *propname,
140 u32 index, u32 *out_value)
142 const u32 *val = of_find_property_value_of_size(np, propname,
143 ((index + 1) * sizeof(*out_value)),
150 *out_value = be32_to_cpup(((__be32 *)val) + index);
153 EXPORT_SYMBOL_GPL(of_property_read_u32_index);
156 * of_property_read_u64_index - Find and read a u64 from a multi-value property.
158 * @np: device node from which the property value is to be read.
159 * @propname: name of the property to be searched.
160 * @index: index of the u64 in the list of values
161 * @out_value: pointer to return value, modified only if no error.
163 * Search for a property in a device node and read nth 64-bit value from
164 * it. Returns 0 on success, -EINVAL if the property does not exist,
165 * -ENODATA if property does not have a value, and -EOVERFLOW if the
166 * property data isn't large enough.
168 * The out_value is modified only if a valid u64 value can be decoded.
170 int of_property_read_u64_index(const struct device_node *np,
171 const char *propname,
172 u32 index, u64 *out_value)
174 const u64 *val = of_find_property_value_of_size(np, propname,
175 ((index + 1) * sizeof(*out_value)),
181 *out_value = be64_to_cpup(((__be64 *)val) + index);
184 EXPORT_SYMBOL_GPL(of_property_read_u64_index);
187 * of_property_read_variable_u8_array - Find and read an array of u8 from a
188 * property, with bounds on the minimum and maximum array size.
190 * @np: device node from which the property value is to be read.
191 * @propname: name of the property to be searched.
192 * @out_values: pointer to found values.
193 * @sz_min: minimum number of array elements to read
194 * @sz_max: maximum number of array elements to read, if zero there is no
195 * upper limit on the number of elements in the dts entry but only
196 * sz_min will be read.
198 * Search for a property in a device node and read 8-bit value(s) from
199 * it. Returns number of elements read on success, -EINVAL if the property
200 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
201 * if the property data is smaller than sz_min or longer than sz_max.
203 * dts entry of array should be like:
204 * property = /bits/ 8 <0x50 0x60 0x70>;
206 * The out_values is modified only if a valid u8 value can be decoded.
208 int of_property_read_variable_u8_array(const struct device_node *np,
209 const char *propname, u8 *out_values,
210 size_t sz_min, size_t sz_max)
213 const u8 *val = of_find_property_value_of_size(np, propname,
214 (sz_min * sizeof(*out_values)),
215 (sz_max * sizeof(*out_values)),
224 sz /= sizeof(*out_values);
228 *out_values++ = *val++;
232 EXPORT_SYMBOL_GPL(of_property_read_variable_u8_array);
235 * of_property_read_variable_u16_array - Find and read an array of u16 from a
236 * property, with bounds on the minimum and maximum array size.
238 * @np: device node from which the property value is to be read.
239 * @propname: name of the property to be searched.
240 * @out_values: pointer to found values.
241 * @sz_min: minimum number of array elements to read
242 * @sz_max: maximum number of array elements to read, if zero there is no
243 * upper limit on the number of elements in the dts entry but only
244 * sz_min will be read.
246 * Search for a property in a device node and read 16-bit value(s) from
247 * it. Returns number of elements read on success, -EINVAL if the property
248 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
249 * if the property data is smaller than sz_min or longer than sz_max.
251 * dts entry of array should be like:
252 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
254 * The out_values is modified only if a valid u16 value can be decoded.
256 int of_property_read_variable_u16_array(const struct device_node *np,
257 const char *propname, u16 *out_values,
258 size_t sz_min, size_t sz_max)
261 const __be16 *val = of_find_property_value_of_size(np, propname,
262 (sz_min * sizeof(*out_values)),
263 (sz_max * sizeof(*out_values)),
272 sz /= sizeof(*out_values);
276 *out_values++ = be16_to_cpup(val++);
280 EXPORT_SYMBOL_GPL(of_property_read_variable_u16_array);
283 * of_property_read_variable_u32_array - Find and read an array of 32 bit
284 * integers from a property, with bounds on the minimum and maximum array size.
286 * @np: device node from which the property value is to be read.
287 * @propname: name of the property to be searched.
288 * @out_values: pointer to return found values.
289 * @sz_min: minimum number of array elements to read
290 * @sz_max: maximum number of array elements to read, if zero there is no
291 * upper limit on the number of elements in the dts entry but only
292 * sz_min will be read.
294 * Search for a property in a device node and read 32-bit value(s) from
295 * it. Returns number of elements read on success, -EINVAL if the property
296 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
297 * if the property data is smaller than sz_min or longer than sz_max.
299 * The out_values is modified only if a valid u32 value can be decoded.
301 int of_property_read_variable_u32_array(const struct device_node *np,
302 const char *propname, u32 *out_values,
303 size_t sz_min, size_t sz_max)
306 const __be32 *val = of_find_property_value_of_size(np, propname,
307 (sz_min * sizeof(*out_values)),
308 (sz_max * sizeof(*out_values)),
317 sz /= sizeof(*out_values);
321 *out_values++ = be32_to_cpup(val++);
325 EXPORT_SYMBOL_GPL(of_property_read_variable_u32_array);
328 * of_property_read_u64 - Find and read a 64 bit integer from a property
329 * @np: device node from which the property value is to be read.
330 * @propname: name of the property to be searched.
331 * @out_value: pointer to return value, modified only if return value is 0.
333 * Search for a property in a device node and read a 64-bit value from
334 * it. Returns 0 on success, -EINVAL if the property does not exist,
335 * -ENODATA if property does not have a value, and -EOVERFLOW if the
336 * property data isn't large enough.
338 * The out_value is modified only if a valid u64 value can be decoded.
340 int of_property_read_u64(const struct device_node *np, const char *propname,
343 const __be32 *val = of_find_property_value_of_size(np, propname,
351 *out_value = of_read_number(val, 2);
354 EXPORT_SYMBOL_GPL(of_property_read_u64);
357 * of_property_read_variable_u64_array - Find and read an array of 64 bit
358 * integers from a property, with bounds on the minimum and maximum array size.
360 * @np: device node from which the property value is to be read.
361 * @propname: name of the property to be searched.
362 * @out_values: pointer to found values.
363 * @sz_min: minimum number of array elements to read
364 * @sz_max: maximum number of array elements to read, if zero there is no
365 * upper limit on the number of elements in the dts entry but only
366 * sz_min will be read.
368 * Search for a property in a device node and read 64-bit value(s) from
369 * it. Returns number of elements read on success, -EINVAL if the property
370 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
371 * if the property data is smaller than sz_min or longer than sz_max.
373 * The out_values is modified only if a valid u64 value can be decoded.
375 int of_property_read_variable_u64_array(const struct device_node *np,
376 const char *propname, u64 *out_values,
377 size_t sz_min, size_t sz_max)
380 const __be32 *val = of_find_property_value_of_size(np, propname,
381 (sz_min * sizeof(*out_values)),
382 (sz_max * sizeof(*out_values)),
391 sz /= sizeof(*out_values);
395 *out_values++ = of_read_number(val, 2);
401 EXPORT_SYMBOL_GPL(of_property_read_variable_u64_array);
404 * of_property_read_string - Find and read a string from a property
405 * @np: device node from which the property value is to be read.
406 * @propname: name of the property to be searched.
407 * @out_string: pointer to null terminated return string, modified only if
410 * Search for a property in a device tree node and retrieve a null
411 * terminated string value (pointer to data, not a copy). Returns 0 on
412 * success, -EINVAL if the property does not exist, -ENODATA if property
413 * does not have a value, and -EILSEQ if the string is not null-terminated
414 * within the length of the property data.
416 * The out_string pointer is modified only if a valid string can be decoded.
418 int of_property_read_string(const struct device_node *np, const char *propname,
419 const char **out_string)
421 const struct property *prop = of_find_property(np, propname, NULL);
426 if (strnlen(prop->value, prop->length) >= prop->length)
428 *out_string = prop->value;
431 EXPORT_SYMBOL_GPL(of_property_read_string);
434 * of_property_match_string() - Find string in a list and return index
435 * @np: pointer to node containing string list property
436 * @propname: string list property name
437 * @string: pointer to string to search for in string list
439 * This function searches a string list property and returns the index
440 * of a specific string value.
442 int of_property_match_string(const struct device_node *np, const char *propname,
445 const struct property *prop = of_find_property(np, propname, NULL);
456 end = p + prop->length;
458 for (i = 0; p < end; i++, p += l) {
459 l = strnlen(p, end - p) + 1;
462 pr_debug("comparing %s with %s\n", string, p);
463 if (strcmp(string, p) == 0)
464 return i; /* Found it; return index */
468 EXPORT_SYMBOL_GPL(of_property_match_string);
471 * of_property_read_string_helper() - Utility helper for parsing string properties
472 * @np: device node from which the property value is to be read.
473 * @propname: name of the property to be searched.
474 * @out_strs: output array of string pointers.
475 * @sz: number of array elements to read.
476 * @skip: Number of strings to skip over at beginning of list.
478 * Don't call this function directly. It is a utility helper for the
479 * of_property_read_string*() family of functions.
481 int of_property_read_string_helper(const struct device_node *np,
482 const char *propname, const char **out_strs,
485 const struct property *prop = of_find_property(np, propname, NULL);
494 end = p + prop->length;
496 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
497 l = strnlen(p, end - p) + 1;
500 if (out_strs && i >= skip)
504 return i <= 0 ? -ENODATA : i;
506 EXPORT_SYMBOL_GPL(of_property_read_string_helper);
508 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
511 const void *curv = cur;
521 curv += sizeof(*cur);
522 if (curv >= prop->value + prop->length)
526 *pu = be32_to_cpup(curv);
529 EXPORT_SYMBOL_GPL(of_prop_next_u32);
531 const char *of_prop_next_string(struct property *prop, const char *cur)
533 const void *curv = cur;
541 curv += strlen(cur) + 1;
542 if (curv >= prop->value + prop->length)
547 EXPORT_SYMBOL_GPL(of_prop_next_string);
550 * of_graph_parse_endpoint() - parse common endpoint node properties
551 * @node: pointer to endpoint device_node
552 * @endpoint: pointer to the OF endpoint data structure
554 * The caller should hold a reference to @node.
556 int of_graph_parse_endpoint(const struct device_node *node,
557 struct of_endpoint *endpoint)
559 struct device_node *port_node = of_get_parent(node);
561 WARN_ONCE(!port_node, "%s(): endpoint %pOF has no parent node\n",
564 memset(endpoint, 0, sizeof(*endpoint));
566 endpoint->local_node = node;
568 * It doesn't matter whether the two calls below succeed.
569 * If they don't then the default value 0 is used.
571 of_property_read_u32(port_node, "reg", &endpoint->port);
572 of_property_read_u32(node, "reg", &endpoint->id);
574 of_node_put(port_node);
578 EXPORT_SYMBOL(of_graph_parse_endpoint);
581 * of_graph_get_port_by_id() - get the port matching a given id
582 * @parent: pointer to the parent device node
583 * @id: id of the port
585 * Return: A 'port' node pointer with refcount incremented. The caller
586 * has to use of_node_put() on it when done.
588 struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id)
590 struct device_node *node, *port;
592 node = of_get_child_by_name(parent, "ports");
596 for_each_child_of_node(parent, port) {
599 if (!of_node_name_eq(port, "port"))
601 of_property_read_u32(port, "reg", &port_id);
610 EXPORT_SYMBOL(of_graph_get_port_by_id);
613 * of_graph_get_next_endpoint() - get next endpoint node
614 * @parent: pointer to the parent device node
615 * @prev: previous endpoint node, or NULL to get first
617 * Return: An 'endpoint' node pointer with refcount incremented. Refcount
618 * of the passed @prev node is decremented.
620 struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
621 struct device_node *prev)
623 struct device_node *endpoint;
624 struct device_node *port;
630 * Start by locating the port node. If no previous endpoint is specified
631 * search for the first port node, otherwise get the previous endpoint
635 struct device_node *node;
637 node = of_get_child_by_name(parent, "ports");
641 port = of_get_child_by_name(parent, "port");
645 pr_err("graph: no port node found in %pOF\n", parent);
649 port = of_get_parent(prev);
650 if (WARN_ONCE(!port, "%s(): endpoint %pOF has no parent node\n",
657 * Now that we have a port node, get the next endpoint by
658 * getting the next child. If the previous endpoint is NULL this
659 * will return the first child.
661 endpoint = of_get_next_child(port, prev);
667 /* No more endpoints under this port, try the next one. */
671 port = of_get_next_child(parent, port);
674 } while (!of_node_name_eq(port, "port"));
677 EXPORT_SYMBOL(of_graph_get_next_endpoint);
680 * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers
681 * @parent: pointer to the parent device node
682 * @port_reg: identifier (value of reg property) of the parent port node
683 * @reg: identifier (value of reg property) of the endpoint node
685 * Return: An 'endpoint' node pointer which is identified by reg and at the same
686 * is the child of a port node identified by port_reg. reg and port_reg are
687 * ignored when they are -1. Use of_node_put() on the pointer when done.
689 struct device_node *of_graph_get_endpoint_by_regs(
690 const struct device_node *parent, int port_reg, int reg)
692 struct of_endpoint endpoint;
693 struct device_node *node = NULL;
695 for_each_endpoint_of_node(parent, node) {
696 of_graph_parse_endpoint(node, &endpoint);
697 if (((port_reg == -1) || (endpoint.port == port_reg)) &&
698 ((reg == -1) || (endpoint.id == reg)))
704 EXPORT_SYMBOL(of_graph_get_endpoint_by_regs);
707 * of_graph_get_remote_endpoint() - get remote endpoint node
708 * @node: pointer to a local endpoint device_node
710 * Return: Remote endpoint node associated with remote endpoint node linked
711 * to @node. Use of_node_put() on it when done.
713 struct device_node *of_graph_get_remote_endpoint(const struct device_node *node)
715 /* Get remote endpoint node. */
716 return of_parse_phandle(node, "remote-endpoint", 0);
718 EXPORT_SYMBOL(of_graph_get_remote_endpoint);
721 * of_graph_get_port_parent() - get port's parent node
722 * @node: pointer to a local endpoint device_node
724 * Return: device node associated with endpoint node linked
725 * to @node. Use of_node_put() on it when done.
727 struct device_node *of_graph_get_port_parent(struct device_node *node)
735 * Preserve usecount for passed in node as of_get_next_parent()
736 * will do of_node_put() on it.
740 /* Walk 3 levels up only if there is 'ports' node. */
741 for (depth = 3; depth && node; depth--) {
742 node = of_get_next_parent(node);
743 if (depth == 2 && !of_node_name_eq(node, "ports"))
748 EXPORT_SYMBOL(of_graph_get_port_parent);
751 * of_graph_get_remote_port_parent() - get remote port's parent node
752 * @node: pointer to a local endpoint device_node
754 * Return: Remote device node associated with remote endpoint node linked
755 * to @node. Use of_node_put() on it when done.
757 struct device_node *of_graph_get_remote_port_parent(
758 const struct device_node *node)
760 struct device_node *np, *pp;
762 /* Get remote endpoint node. */
763 np = of_graph_get_remote_endpoint(node);
765 pp = of_graph_get_port_parent(np);
771 EXPORT_SYMBOL(of_graph_get_remote_port_parent);
774 * of_graph_get_remote_port() - get remote port node
775 * @node: pointer to a local endpoint device_node
777 * Return: Remote port node associated with remote endpoint node linked
778 * to @node. Use of_node_put() on it when done.
780 struct device_node *of_graph_get_remote_port(const struct device_node *node)
782 struct device_node *np;
784 /* Get remote endpoint node. */
785 np = of_graph_get_remote_endpoint(node);
788 return of_get_next_parent(np);
790 EXPORT_SYMBOL(of_graph_get_remote_port);
792 int of_graph_get_endpoint_count(const struct device_node *np)
794 struct device_node *endpoint;
797 for_each_endpoint_of_node(np, endpoint)
802 EXPORT_SYMBOL(of_graph_get_endpoint_count);
805 * of_graph_get_remote_node() - get remote parent device_node for given port/endpoint
806 * @node: pointer to parent device_node containing graph port/endpoint
807 * @port: identifier (value of reg property) of the parent port node
808 * @endpoint: identifier (value of reg property) of the endpoint node
810 * Return: Remote device node associated with remote endpoint node linked
811 * to @node. Use of_node_put() on it when done.
813 struct device_node *of_graph_get_remote_node(const struct device_node *node,
814 u32 port, u32 endpoint)
816 struct device_node *endpoint_node, *remote;
818 endpoint_node = of_graph_get_endpoint_by_regs(node, port, endpoint);
819 if (!endpoint_node) {
820 pr_debug("no valid endpoint (%d, %d) for node %pOF\n",
821 port, endpoint, node);
825 remote = of_graph_get_remote_port_parent(endpoint_node);
826 of_node_put(endpoint_node);
828 pr_debug("no valid remote node\n");
832 if (!of_device_is_available(remote)) {
833 pr_debug("not available for remote node\n");
840 EXPORT_SYMBOL(of_graph_get_remote_node);
842 static struct fwnode_handle *of_fwnode_get(struct fwnode_handle *fwnode)
844 return of_fwnode_handle(of_node_get(to_of_node(fwnode)));
847 static void of_fwnode_put(struct fwnode_handle *fwnode)
849 of_node_put(to_of_node(fwnode));
852 static bool of_fwnode_device_is_available(const struct fwnode_handle *fwnode)
854 return of_device_is_available(to_of_node(fwnode));
857 static bool of_fwnode_property_present(const struct fwnode_handle *fwnode,
858 const char *propname)
860 return of_property_read_bool(to_of_node(fwnode), propname);
863 static int of_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
864 const char *propname,
865 unsigned int elem_size, void *val,
868 const struct device_node *node = to_of_node(fwnode);
871 return of_property_count_elems_of_size(node, propname,
876 return of_property_read_u8_array(node, propname, val, nval);
878 return of_property_read_u16_array(node, propname, val, nval);
880 return of_property_read_u32_array(node, propname, val, nval);
882 return of_property_read_u64_array(node, propname, val, nval);
889 of_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
890 const char *propname, const char **val,
893 const struct device_node *node = to_of_node(fwnode);
896 of_property_read_string_array(node, propname, val, nval) :
897 of_property_count_strings(node, propname);
900 static const char *of_fwnode_get_name(const struct fwnode_handle *fwnode)
902 return kbasename(to_of_node(fwnode)->full_name);
905 static const char *of_fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
907 /* Root needs no prefix here (its name is "/"). */
908 if (!to_of_node(fwnode)->parent)
914 static struct fwnode_handle *
915 of_fwnode_get_parent(const struct fwnode_handle *fwnode)
917 return of_fwnode_handle(of_get_parent(to_of_node(fwnode)));
920 static struct fwnode_handle *
921 of_fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
922 struct fwnode_handle *child)
924 return of_fwnode_handle(of_get_next_available_child(to_of_node(fwnode),
928 static struct fwnode_handle *
929 of_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
930 const char *childname)
932 const struct device_node *node = to_of_node(fwnode);
933 struct device_node *child;
935 for_each_available_child_of_node(node, child)
936 if (of_node_name_eq(child, childname))
937 return of_fwnode_handle(child);
943 of_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
944 const char *prop, const char *nargs_prop,
945 unsigned int nargs, unsigned int index,
946 struct fwnode_reference_args *args)
948 struct of_phandle_args of_args;
953 ret = of_parse_phandle_with_args(to_of_node(fwnode), prop,
954 nargs_prop, index, &of_args);
956 ret = of_parse_phandle_with_fixed_args(to_of_node(fwnode), prop,
957 nargs, index, &of_args);
963 args->nargs = of_args.args_count;
964 args->fwnode = of_fwnode_handle(of_args.np);
966 for (i = 0; i < NR_FWNODE_REFERENCE_ARGS; i++)
967 args->args[i] = i < of_args.args_count ? of_args.args[i] : 0;
972 static struct fwnode_handle *
973 of_fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
974 struct fwnode_handle *prev)
976 return of_fwnode_handle(of_graph_get_next_endpoint(to_of_node(fwnode),
980 static struct fwnode_handle *
981 of_fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
983 return of_fwnode_handle(
984 of_graph_get_remote_endpoint(to_of_node(fwnode)));
987 static struct fwnode_handle *
988 of_fwnode_graph_get_port_parent(struct fwnode_handle *fwnode)
990 struct device_node *np;
992 /* Get the parent of the port */
993 np = of_get_parent(to_of_node(fwnode));
997 /* Is this the "ports" node? If not, it's the port parent. */
998 if (!of_node_name_eq(np, "ports"))
999 return of_fwnode_handle(np);
1001 return of_fwnode_handle(of_get_next_parent(np));
1004 static int of_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
1005 struct fwnode_endpoint *endpoint)
1007 const struct device_node *node = to_of_node(fwnode);
1008 struct device_node *port_node = of_get_parent(node);
1010 endpoint->local_fwnode = fwnode;
1012 of_property_read_u32(port_node, "reg", &endpoint->port);
1013 of_property_read_u32(node, "reg", &endpoint->id);
1015 of_node_put(port_node);
1021 of_fwnode_device_get_match_data(const struct fwnode_handle *fwnode,
1022 const struct device *dev)
1024 return of_device_get_match_data(dev);
1027 static bool of_is_ancestor_of(struct device_node *test_ancestor,
1028 struct device_node *child)
1032 if (child == test_ancestor) {
1036 child = of_get_next_parent(child);
1041 static struct device_node *of_get_compat_node(struct device_node *np)
1046 if (!of_device_is_available(np)) {
1051 if (of_find_property(np, "compatible", NULL))
1054 np = of_get_next_parent(np);
1061 * of_link_to_phandle - Add fwnode link to supplier from supplier phandle
1062 * @con_np: consumer device tree node
1063 * @sup_np: supplier device tree node
1065 * Given a phandle to a supplier device tree node (@sup_np), this function
1066 * finds the device that owns the supplier device tree node and creates a
1067 * device link from @dev consumer device to the supplier device. This function
1068 * doesn't create device links for invalid scenarios such as trying to create a
1069 * link with a parent device as the consumer of its child device. In such
1070 * cases, it returns an error.
1073 * - 0 if fwnode link successfully created to supplier
1074 * - -EINVAL if the supplier link is invalid and should not be created
1075 * - -ENODEV if struct device will never be create for supplier
1077 static int of_link_to_phandle(struct device_node *con_np,
1078 struct device_node *sup_np)
1080 struct device *sup_dev;
1081 struct device_node *tmp_np = sup_np;
1084 * Find the device node that contains the supplier phandle. It may be
1085 * @sup_np or it may be an ancestor of @sup_np.
1087 sup_np = of_get_compat_node(sup_np);
1089 pr_debug("Not linking %pOFP to %pOFP - No device\n",
1095 * Don't allow linking a device node as a consumer of one of its
1096 * descendant nodes. By definition, a child node can't be a functional
1097 * dependency for the parent node.
1099 if (of_is_ancestor_of(con_np, sup_np)) {
1100 pr_debug("Not linking %pOFP to %pOFP - is descendant\n",
1102 of_node_put(sup_np);
1107 * Don't create links to "early devices" that won't have struct devices
1110 sup_dev = get_dev_from_fwnode(&sup_np->fwnode);
1112 (of_node_check_flag(sup_np, OF_POPULATED) ||
1113 sup_np->fwnode.flags & FWNODE_FLAG_NOT_DEVICE)) {
1114 pr_debug("Not linking %pOFP to %pOFP - No struct device\n",
1116 of_node_put(sup_np);
1119 put_device(sup_dev);
1121 fwnode_link_add(of_fwnode_handle(con_np), of_fwnode_handle(sup_np));
1122 of_node_put(sup_np);
1128 * parse_prop_cells - Property parsing function for suppliers
1130 * @np: Pointer to device tree node containing a list
1131 * @prop_name: Name of property to be parsed. Expected to hold phandle values
1132 * @index: For properties holding a list of phandles, this is the index
1134 * @list_name: Property name that is known to contain list of phandle(s) to
1136 * @cells_name: property name that specifies phandles' arguments count
1138 * This is a helper function to parse properties that have a known fixed name
1139 * and are a list of phandles and phandle arguments.
1142 * - phandle node pointer with refcount incremented. Caller must of_node_put()
1144 * - NULL if no phandle found at index
1146 static struct device_node *parse_prop_cells(struct device_node *np,
1147 const char *prop_name, int index,
1148 const char *list_name,
1149 const char *cells_name)
1151 struct of_phandle_args sup_args;
1153 if (strcmp(prop_name, list_name))
1156 if (of_parse_phandle_with_args(np, list_name, cells_name, index,
1163 #define DEFINE_SIMPLE_PROP(fname, name, cells) \
1164 static struct device_node *parse_##fname(struct device_node *np, \
1165 const char *prop_name, int index) \
1167 return parse_prop_cells(np, prop_name, index, name, cells); \
1170 static int strcmp_suffix(const char *str, const char *suffix)
1172 unsigned int len, suffix_len;
1175 suffix_len = strlen(suffix);
1176 if (len <= suffix_len)
1178 return strcmp(str + len - suffix_len, suffix);
1182 * parse_suffix_prop_cells - Suffix property parsing function for suppliers
1184 * @np: Pointer to device tree node containing a list
1185 * @prop_name: Name of property to be parsed. Expected to hold phandle values
1186 * @index: For properties holding a list of phandles, this is the index
1188 * @suffix: Property suffix that is known to contain list of phandle(s) to
1190 * @cells_name: property name that specifies phandles' arguments count
1192 * This is a helper function to parse properties that have a known fixed suffix
1193 * and are a list of phandles and phandle arguments.
1196 * - phandle node pointer with refcount incremented. Caller must of_node_put()
1198 * - NULL if no phandle found at index
1200 static struct device_node *parse_suffix_prop_cells(struct device_node *np,
1201 const char *prop_name, int index,
1203 const char *cells_name)
1205 struct of_phandle_args sup_args;
1207 if (strcmp_suffix(prop_name, suffix))
1210 if (of_parse_phandle_with_args(np, prop_name, cells_name, index,
1217 #define DEFINE_SUFFIX_PROP(fname, suffix, cells) \
1218 static struct device_node *parse_##fname(struct device_node *np, \
1219 const char *prop_name, int index) \
1221 return parse_suffix_prop_cells(np, prop_name, index, suffix, cells); \
1225 * struct supplier_bindings - Property parsing functions for suppliers
1227 * @parse_prop: function name
1228 * parse_prop() finds the node corresponding to a supplier phandle
1229 * @parse_prop.np: Pointer to device node holding supplier phandle property
1230 * @parse_prop.prop_name: Name of property holding a phandle value
1231 * @parse_prop.index: For properties holding a list of phandles, this is the
1232 * index into the list
1233 * @optional: The property can be an optional dependency.
1234 * @node_not_dev: The consumer node containing the property is never a device.
1237 * parse_prop() return values are
1238 * - phandle node pointer with refcount incremented. Caller must of_node_put()
1240 * - NULL if no phandle found at index
1242 struct supplier_bindings {
1243 struct device_node *(*parse_prop)(struct device_node *np,
1244 const char *prop_name, int index);
1249 DEFINE_SIMPLE_PROP(clocks, "clocks", "#clock-cells")
1250 DEFINE_SIMPLE_PROP(interconnects, "interconnects", "#interconnect-cells")
1251 DEFINE_SIMPLE_PROP(iommus, "iommus", "#iommu-cells")
1252 DEFINE_SIMPLE_PROP(mboxes, "mboxes", "#mbox-cells")
1253 DEFINE_SIMPLE_PROP(io_channels, "io-channel", "#io-channel-cells")
1254 DEFINE_SIMPLE_PROP(interrupt_parent, "interrupt-parent", NULL)
1255 DEFINE_SIMPLE_PROP(dmas, "dmas", "#dma-cells")
1256 DEFINE_SIMPLE_PROP(power_domains, "power-domains", "#power-domain-cells")
1257 DEFINE_SIMPLE_PROP(hwlocks, "hwlocks", "#hwlock-cells")
1258 DEFINE_SIMPLE_PROP(extcon, "extcon", NULL)
1259 DEFINE_SIMPLE_PROP(nvmem_cells, "nvmem-cells", NULL)
1260 DEFINE_SIMPLE_PROP(phys, "phys", "#phy-cells")
1261 DEFINE_SIMPLE_PROP(wakeup_parent, "wakeup-parent", NULL)
1262 DEFINE_SIMPLE_PROP(pinctrl0, "pinctrl-0", NULL)
1263 DEFINE_SIMPLE_PROP(pinctrl1, "pinctrl-1", NULL)
1264 DEFINE_SIMPLE_PROP(pinctrl2, "pinctrl-2", NULL)
1265 DEFINE_SIMPLE_PROP(pinctrl3, "pinctrl-3", NULL)
1266 DEFINE_SIMPLE_PROP(pinctrl4, "pinctrl-4", NULL)
1267 DEFINE_SIMPLE_PROP(pinctrl5, "pinctrl-5", NULL)
1268 DEFINE_SIMPLE_PROP(pinctrl6, "pinctrl-6", NULL)
1269 DEFINE_SIMPLE_PROP(pinctrl7, "pinctrl-7", NULL)
1270 DEFINE_SIMPLE_PROP(pinctrl8, "pinctrl-8", NULL)
1271 DEFINE_SIMPLE_PROP(remote_endpoint, "remote-endpoint", NULL)
1272 DEFINE_SUFFIX_PROP(regulators, "-supply", NULL)
1273 DEFINE_SUFFIX_PROP(gpio, "-gpio", "#gpio-cells")
1275 static struct device_node *parse_gpios(struct device_node *np,
1276 const char *prop_name, int index)
1278 if (!strcmp_suffix(prop_name, ",nr-gpios"))
1281 return parse_suffix_prop_cells(np, prop_name, index, "-gpios",
1285 static struct device_node *parse_iommu_maps(struct device_node *np,
1286 const char *prop_name, int index)
1288 if (strcmp(prop_name, "iommu-map"))
1291 return of_parse_phandle(np, prop_name, (index * 4) + 1);
1294 static struct device_node *parse_gpio_compat(struct device_node *np,
1295 const char *prop_name, int index)
1297 struct of_phandle_args sup_args;
1299 if (strcmp(prop_name, "gpio") && strcmp(prop_name, "gpios"))
1303 * Ignore node with gpio-hog property since its gpios are all provided
1306 if (of_find_property(np, "gpio-hog", NULL))
1309 if (of_parse_phandle_with_args(np, prop_name, "#gpio-cells", index,
1316 static struct device_node *parse_interrupts(struct device_node *np,
1317 const char *prop_name, int index)
1319 struct of_phandle_args sup_args;
1321 if (!IS_ENABLED(CONFIG_OF_IRQ) || IS_ENABLED(CONFIG_PPC))
1324 if (strcmp(prop_name, "interrupts") &&
1325 strcmp(prop_name, "interrupts-extended"))
1328 return of_irq_parse_one(np, index, &sup_args) ? NULL : sup_args.np;
1331 static const struct supplier_bindings of_supplier_bindings[] = {
1332 { .parse_prop = parse_clocks, },
1333 { .parse_prop = parse_interconnects, },
1334 { .parse_prop = parse_iommus, .optional = true, },
1335 { .parse_prop = parse_iommu_maps, .optional = true, },
1336 { .parse_prop = parse_mboxes, },
1337 { .parse_prop = parse_io_channels, },
1338 { .parse_prop = parse_interrupt_parent, },
1339 { .parse_prop = parse_dmas, .optional = true, },
1340 { .parse_prop = parse_power_domains, },
1341 { .parse_prop = parse_hwlocks, },
1342 { .parse_prop = parse_extcon, },
1343 { .parse_prop = parse_nvmem_cells, },
1344 { .parse_prop = parse_phys, },
1345 { .parse_prop = parse_wakeup_parent, },
1346 { .parse_prop = parse_pinctrl0, },
1347 { .parse_prop = parse_pinctrl1, },
1348 { .parse_prop = parse_pinctrl2, },
1349 { .parse_prop = parse_pinctrl3, },
1350 { .parse_prop = parse_pinctrl4, },
1351 { .parse_prop = parse_pinctrl5, },
1352 { .parse_prop = parse_pinctrl6, },
1353 { .parse_prop = parse_pinctrl7, },
1354 { .parse_prop = parse_pinctrl8, },
1355 { .parse_prop = parse_remote_endpoint, .node_not_dev = true, },
1356 { .parse_prop = parse_gpio_compat, },
1357 { .parse_prop = parse_interrupts, },
1358 { .parse_prop = parse_regulators, },
1359 { .parse_prop = parse_gpio, },
1360 { .parse_prop = parse_gpios, },
1365 * of_link_property - Create device links to suppliers listed in a property
1366 * @dev: Consumer device
1367 * @con_np: The consumer device tree node which contains the property
1368 * @prop_name: Name of property to be parsed
1370 * This function checks if the property @prop_name that is present in the
1371 * @con_np device tree node is one of the known common device tree bindings
1372 * that list phandles to suppliers. If @prop_name isn't one, this function
1373 * doesn't do anything.
1375 * If @prop_name is one, this function attempts to create fwnode links from the
1376 * consumer device tree node @con_np to all the suppliers device tree nodes
1377 * listed in @prop_name.
1379 * Any failed attempt to create a fwnode link will NOT result in an immediate
1380 * return. of_link_property() must create links to all the available supplier
1381 * device tree nodes even when attempts to create a link to one or more
1384 static int of_link_property(struct device_node *con_np, const char *prop_name)
1386 struct device_node *phandle;
1387 const struct supplier_bindings *s = of_supplier_bindings;
1389 bool matched = false;
1392 /* Do not stop at first failed link, link all available suppliers. */
1393 while (!matched && s->parse_prop) {
1394 if (s->optional && !fw_devlink_is_strict()) {
1399 while ((phandle = s->parse_prop(con_np, prop_name, i))) {
1400 struct device_node *con_dev_np;
1402 con_dev_np = s->node_not_dev
1403 ? of_get_compat_node(con_np)
1404 : of_node_get(con_np);
1407 of_link_to_phandle(con_dev_np, phandle);
1408 of_node_put(phandle);
1409 of_node_put(con_dev_np);
1416 static int of_fwnode_add_links(struct fwnode_handle *fwnode)
1419 struct device_node *con_np = to_of_node(fwnode);
1424 for_each_property_of_node(con_np, p)
1425 of_link_property(con_np, p->name);
1430 const struct fwnode_operations of_fwnode_ops = {
1431 .get = of_fwnode_get,
1432 .put = of_fwnode_put,
1433 .device_is_available = of_fwnode_device_is_available,
1434 .device_get_match_data = of_fwnode_device_get_match_data,
1435 .property_present = of_fwnode_property_present,
1436 .property_read_int_array = of_fwnode_property_read_int_array,
1437 .property_read_string_array = of_fwnode_property_read_string_array,
1438 .get_name = of_fwnode_get_name,
1439 .get_name_prefix = of_fwnode_get_name_prefix,
1440 .get_parent = of_fwnode_get_parent,
1441 .get_next_child_node = of_fwnode_get_next_child_node,
1442 .get_named_child_node = of_fwnode_get_named_child_node,
1443 .get_reference_args = of_fwnode_get_reference_args,
1444 .graph_get_next_endpoint = of_fwnode_graph_get_next_endpoint,
1445 .graph_get_remote_endpoint = of_fwnode_graph_get_remote_endpoint,
1446 .graph_get_port_parent = of_fwnode_graph_get_port_parent,
1447 .graph_parse_endpoint = of_fwnode_graph_parse_endpoint,
1448 .add_links = of_fwnode_add_links,
1450 EXPORT_SYMBOL_GPL(of_fwnode_ops);