1 // SPDX-License-Identifier: GPL-2.0+
3 * drivers/of/property.c - Procedures for accessing and interpreting
4 * Devicetree properties and graphs.
6 * Initially created by copying procedures from drivers/of/base.c. This
7 * file contains the OF property as well as the OF graph interface
10 * Paul Mackerras August 1996.
11 * Copyright (C) 1996-2005 Paul Mackerras.
13 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
14 * {engebret|bergner}@us.ibm.com
16 * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
18 * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
22 #define pr_fmt(fmt) "OF: " fmt
25 #include <linux/of_address.h>
26 #include <linux/of_device.h>
27 #include <linux/of_graph.h>
28 #include <linux/of_irq.h>
29 #include <linux/string.h>
30 #include <linux/moduleparam.h>
32 #include "of_private.h"
35 * of_graph_is_present() - check graph's presence
36 * @node: pointer to device_node containing graph port
38 * Return: True if @node has a port or ports (with a port) sub-node,
41 bool of_graph_is_present(const struct device_node *node)
43 struct device_node *ports, *port;
45 ports = of_get_child_by_name(node, "ports");
49 port = of_get_child_by_name(node, "port");
55 EXPORT_SYMBOL(of_graph_is_present);
58 * of_property_count_elems_of_size - Count the number of elements in a property
60 * @np: device node from which the property value is to be read.
61 * @propname: name of the property to be searched.
62 * @elem_size: size of the individual element
64 * Search for a property in a device node and count the number of elements of
65 * size elem_size in it.
67 * Return: The number of elements on sucess, -EINVAL if the property does not
68 * exist or its length does not match a multiple of elem_size and -ENODATA if
69 * the property does not have a value.
71 int of_property_count_elems_of_size(const struct device_node *np,
72 const char *propname, int elem_size)
74 struct property *prop = of_find_property(np, propname, NULL);
81 if (prop->length % elem_size != 0) {
82 pr_err("size of %s in node %pOF is not a multiple of %d\n",
83 propname, np, elem_size);
87 return prop->length / elem_size;
89 EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
92 * of_find_property_value_of_size
94 * @np: device node from which the property value is to be read.
95 * @propname: name of the property to be searched.
96 * @min: minimum allowed length of property value
97 * @max: maximum allowed length of property value (0 means unlimited)
98 * @len: if !=NULL, actual length is written to here
100 * Search for a property in a device node and valid the requested size.
102 * Return: The property value on success, -EINVAL if the property does not
103 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
104 * property data is too small or too large.
107 static void *of_find_property_value_of_size(const struct device_node *np,
108 const char *propname, u32 min, u32 max, size_t *len)
110 struct property *prop = of_find_property(np, propname, NULL);
113 return ERR_PTR(-EINVAL);
115 return ERR_PTR(-ENODATA);
116 if (prop->length < min)
117 return ERR_PTR(-EOVERFLOW);
118 if (max && prop->length > max)
119 return ERR_PTR(-EOVERFLOW);
128 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
130 * @np: device node from which the property value is to be read.
131 * @propname: name of the property to be searched.
132 * @index: index of the u32 in the list of values
133 * @out_value: pointer to return value, modified only if no error.
135 * Search for a property in a device node and read nth 32-bit value from
138 * Return: 0 on success, -EINVAL if the property does not exist,
139 * -ENODATA if property does not have a value, and -EOVERFLOW if the
140 * property data isn't large enough.
142 * The out_value is modified only if a valid u32 value can be decoded.
144 int of_property_read_u32_index(const struct device_node *np,
145 const char *propname,
146 u32 index, u32 *out_value)
148 const u32 *val = of_find_property_value_of_size(np, propname,
149 ((index + 1) * sizeof(*out_value)),
156 *out_value = be32_to_cpup(((__be32 *)val) + index);
159 EXPORT_SYMBOL_GPL(of_property_read_u32_index);
162 * of_property_read_u64_index - Find and read a u64 from a multi-value property.
164 * @np: device node from which the property value is to be read.
165 * @propname: name of the property to be searched.
166 * @index: index of the u64 in the list of values
167 * @out_value: pointer to return value, modified only if no error.
169 * Search for a property in a device node and read nth 64-bit value from
172 * Return: 0 on success, -EINVAL if the property does not exist,
173 * -ENODATA if property does not have a value, and -EOVERFLOW if the
174 * property data isn't large enough.
176 * The out_value is modified only if a valid u64 value can be decoded.
178 int of_property_read_u64_index(const struct device_node *np,
179 const char *propname,
180 u32 index, u64 *out_value)
182 const u64 *val = of_find_property_value_of_size(np, propname,
183 ((index + 1) * sizeof(*out_value)),
189 *out_value = be64_to_cpup(((__be64 *)val) + index);
192 EXPORT_SYMBOL_GPL(of_property_read_u64_index);
195 * of_property_read_variable_u8_array - Find and read an array of u8 from a
196 * property, with bounds on the minimum and maximum array size.
198 * @np: device node from which the property value is to be read.
199 * @propname: name of the property to be searched.
200 * @out_values: pointer to found values.
201 * @sz_min: minimum number of array elements to read
202 * @sz_max: maximum number of array elements to read, if zero there is no
203 * upper limit on the number of elements in the dts entry but only
204 * sz_min will be read.
206 * Search for a property in a device node and read 8-bit value(s) from
209 * dts entry of array should be like:
210 * ``property = /bits/ 8 <0x50 0x60 0x70>;``
212 * Return: The number of elements read on success, -EINVAL if the property
213 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
214 * if the property data is smaller than sz_min or longer than sz_max.
216 * The out_values is modified only if a valid u8 value can be decoded.
218 int of_property_read_variable_u8_array(const struct device_node *np,
219 const char *propname, u8 *out_values,
220 size_t sz_min, size_t sz_max)
223 const u8 *val = of_find_property_value_of_size(np, propname,
224 (sz_min * sizeof(*out_values)),
225 (sz_max * sizeof(*out_values)),
234 sz /= sizeof(*out_values);
238 *out_values++ = *val++;
242 EXPORT_SYMBOL_GPL(of_property_read_variable_u8_array);
245 * of_property_read_variable_u16_array - Find and read an array of u16 from a
246 * property, with bounds on the minimum and maximum array size.
248 * @np: device node from which the property value is to be read.
249 * @propname: name of the property to be searched.
250 * @out_values: pointer to found values.
251 * @sz_min: minimum number of array elements to read
252 * @sz_max: maximum number of array elements to read, if zero there is no
253 * upper limit on the number of elements in the dts entry but only
254 * sz_min will be read.
256 * Search for a property in a device node and read 16-bit value(s) from
259 * dts entry of array should be like:
260 * ``property = /bits/ 16 <0x5000 0x6000 0x7000>;``
262 * Return: The number of elements read on success, -EINVAL if the property
263 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
264 * if the property data is smaller than sz_min or longer than sz_max.
266 * The out_values is modified only if a valid u16 value can be decoded.
268 int of_property_read_variable_u16_array(const struct device_node *np,
269 const char *propname, u16 *out_values,
270 size_t sz_min, size_t sz_max)
273 const __be16 *val = of_find_property_value_of_size(np, propname,
274 (sz_min * sizeof(*out_values)),
275 (sz_max * sizeof(*out_values)),
284 sz /= sizeof(*out_values);
288 *out_values++ = be16_to_cpup(val++);
292 EXPORT_SYMBOL_GPL(of_property_read_variable_u16_array);
295 * of_property_read_variable_u32_array - Find and read an array of 32 bit
296 * integers from a property, with bounds on the minimum and maximum array size.
298 * @np: device node from which the property value is to be read.
299 * @propname: name of the property to be searched.
300 * @out_values: pointer to return found values.
301 * @sz_min: minimum number of array elements to read
302 * @sz_max: maximum number of array elements to read, if zero there is no
303 * upper limit on the number of elements in the dts entry but only
304 * sz_min will be read.
306 * Search for a property in a device node and read 32-bit value(s) from
309 * Return: The number of elements read on success, -EINVAL if the property
310 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
311 * if the property data is smaller than sz_min or longer than sz_max.
313 * The out_values is modified only if a valid u32 value can be decoded.
315 int of_property_read_variable_u32_array(const struct device_node *np,
316 const char *propname, u32 *out_values,
317 size_t sz_min, size_t sz_max)
320 const __be32 *val = of_find_property_value_of_size(np, propname,
321 (sz_min * sizeof(*out_values)),
322 (sz_max * sizeof(*out_values)),
331 sz /= sizeof(*out_values);
335 *out_values++ = be32_to_cpup(val++);
339 EXPORT_SYMBOL_GPL(of_property_read_variable_u32_array);
342 * of_property_read_u64 - Find and read a 64 bit integer from a property
343 * @np: device node from which the property value is to be read.
344 * @propname: name of the property to be searched.
345 * @out_value: pointer to return value, modified only if return value is 0.
347 * Search for a property in a device node and read a 64-bit value from
350 * Return: 0 on success, -EINVAL if the property does not exist,
351 * -ENODATA if property does not have a value, and -EOVERFLOW if the
352 * property data isn't large enough.
354 * The out_value is modified only if a valid u64 value can be decoded.
356 int of_property_read_u64(const struct device_node *np, const char *propname,
359 const __be32 *val = of_find_property_value_of_size(np, propname,
367 *out_value = of_read_number(val, 2);
370 EXPORT_SYMBOL_GPL(of_property_read_u64);
373 * of_property_read_variable_u64_array - Find and read an array of 64 bit
374 * integers from a property, with bounds on the minimum and maximum array size.
376 * @np: device node from which the property value is to be read.
377 * @propname: name of the property to be searched.
378 * @out_values: pointer to found values.
379 * @sz_min: minimum number of array elements to read
380 * @sz_max: maximum number of array elements to read, if zero there is no
381 * upper limit on the number of elements in the dts entry but only
382 * sz_min will be read.
384 * Search for a property in a device node and read 64-bit value(s) from
387 * Return: The number of elements read on success, -EINVAL if the property
388 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
389 * if the property data is smaller than sz_min or longer than sz_max.
391 * The out_values is modified only if a valid u64 value can be decoded.
393 int of_property_read_variable_u64_array(const struct device_node *np,
394 const char *propname, u64 *out_values,
395 size_t sz_min, size_t sz_max)
398 const __be32 *val = of_find_property_value_of_size(np, propname,
399 (sz_min * sizeof(*out_values)),
400 (sz_max * sizeof(*out_values)),
409 sz /= sizeof(*out_values);
413 *out_values++ = of_read_number(val, 2);
419 EXPORT_SYMBOL_GPL(of_property_read_variable_u64_array);
422 * of_property_read_string - Find and read a string from a property
423 * @np: device node from which the property value is to be read.
424 * @propname: name of the property to be searched.
425 * @out_string: pointer to null terminated return string, modified only if
428 * Search for a property in a device tree node and retrieve a null
429 * terminated string value (pointer to data, not a copy).
431 * Return: 0 on success, -EINVAL if the property does not exist, -ENODATA if
432 * property does not have a value, and -EILSEQ if the string is not
433 * null-terminated within the length of the property data.
435 * Note that the empty string "" has length of 1, thus -ENODATA cannot
436 * be interpreted as an empty string.
438 * The out_string pointer is modified only if a valid string can be decoded.
440 int of_property_read_string(const struct device_node *np, const char *propname,
441 const char **out_string)
443 const struct property *prop = of_find_property(np, propname, NULL);
448 if (strnlen(prop->value, prop->length) >= prop->length)
450 *out_string = prop->value;
453 EXPORT_SYMBOL_GPL(of_property_read_string);
456 * of_property_match_string() - Find string in a list and return index
457 * @np: pointer to node containing string list property
458 * @propname: string list property name
459 * @string: pointer to string to search for in string list
461 * This function searches a string list property and returns the index
462 * of a specific string value.
464 int of_property_match_string(const struct device_node *np, const char *propname,
467 const struct property *prop = of_find_property(np, propname, NULL);
478 end = p + prop->length;
480 for (i = 0; p < end; i++, p += l) {
481 l = strnlen(p, end - p) + 1;
484 pr_debug("comparing %s with %s\n", string, p);
485 if (strcmp(string, p) == 0)
486 return i; /* Found it; return index */
490 EXPORT_SYMBOL_GPL(of_property_match_string);
493 * of_property_read_string_helper() - Utility helper for parsing string properties
494 * @np: device node from which the property value is to be read.
495 * @propname: name of the property to be searched.
496 * @out_strs: output array of string pointers.
497 * @sz: number of array elements to read.
498 * @skip: Number of strings to skip over at beginning of list.
500 * Don't call this function directly. It is a utility helper for the
501 * of_property_read_string*() family of functions.
503 int of_property_read_string_helper(const struct device_node *np,
504 const char *propname, const char **out_strs,
507 const struct property *prop = of_find_property(np, propname, NULL);
516 end = p + prop->length;
518 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
519 l = strnlen(p, end - p) + 1;
522 if (out_strs && i >= skip)
526 return i <= 0 ? -ENODATA : i;
528 EXPORT_SYMBOL_GPL(of_property_read_string_helper);
530 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
533 const void *curv = cur;
543 curv += sizeof(*cur);
544 if (curv >= prop->value + prop->length)
548 *pu = be32_to_cpup(curv);
551 EXPORT_SYMBOL_GPL(of_prop_next_u32);
553 const char *of_prop_next_string(struct property *prop, const char *cur)
555 const void *curv = cur;
563 curv += strlen(cur) + 1;
564 if (curv >= prop->value + prop->length)
569 EXPORT_SYMBOL_GPL(of_prop_next_string);
572 * of_graph_parse_endpoint() - parse common endpoint node properties
573 * @node: pointer to endpoint device_node
574 * @endpoint: pointer to the OF endpoint data structure
576 * The caller should hold a reference to @node.
578 int of_graph_parse_endpoint(const struct device_node *node,
579 struct of_endpoint *endpoint)
581 struct device_node *port_node = of_get_parent(node);
583 WARN_ONCE(!port_node, "%s(): endpoint %pOF has no parent node\n",
586 memset(endpoint, 0, sizeof(*endpoint));
588 endpoint->local_node = node;
590 * It doesn't matter whether the two calls below succeed.
591 * If they don't then the default value 0 is used.
593 of_property_read_u32(port_node, "reg", &endpoint->port);
594 of_property_read_u32(node, "reg", &endpoint->id);
596 of_node_put(port_node);
600 EXPORT_SYMBOL(of_graph_parse_endpoint);
603 * of_graph_get_port_by_id() - get the port matching a given id
604 * @parent: pointer to the parent device node
605 * @id: id of the port
607 * Return: A 'port' node pointer with refcount incremented. The caller
608 * has to use of_node_put() on it when done.
610 struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id)
612 struct device_node *node, *port;
614 node = of_get_child_by_name(parent, "ports");
618 for_each_child_of_node(parent, port) {
621 if (!of_node_name_eq(port, "port"))
623 of_property_read_u32(port, "reg", &port_id);
632 EXPORT_SYMBOL(of_graph_get_port_by_id);
635 * of_graph_get_next_endpoint() - get next endpoint node
636 * @parent: pointer to the parent device node
637 * @prev: previous endpoint node, or NULL to get first
639 * Return: An 'endpoint' node pointer with refcount incremented. Refcount
640 * of the passed @prev node is decremented.
642 struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
643 struct device_node *prev)
645 struct device_node *endpoint;
646 struct device_node *port;
652 * Start by locating the port node. If no previous endpoint is specified
653 * search for the first port node, otherwise get the previous endpoint
657 struct device_node *node;
659 node = of_get_child_by_name(parent, "ports");
663 port = of_get_child_by_name(parent, "port");
667 pr_err("graph: no port node found in %pOF\n", parent);
671 port = of_get_parent(prev);
672 if (WARN_ONCE(!port, "%s(): endpoint %pOF has no parent node\n",
679 * Now that we have a port node, get the next endpoint by
680 * getting the next child. If the previous endpoint is NULL this
681 * will return the first child.
683 endpoint = of_get_next_child(port, prev);
689 /* No more endpoints under this port, try the next one. */
693 port = of_get_next_child(parent, port);
696 } while (!of_node_name_eq(port, "port"));
699 EXPORT_SYMBOL(of_graph_get_next_endpoint);
702 * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers
703 * @parent: pointer to the parent device node
704 * @port_reg: identifier (value of reg property) of the parent port node
705 * @reg: identifier (value of reg property) of the endpoint node
707 * Return: An 'endpoint' node pointer which is identified by reg and at the same
708 * is the child of a port node identified by port_reg. reg and port_reg are
709 * ignored when they are -1. Use of_node_put() on the pointer when done.
711 struct device_node *of_graph_get_endpoint_by_regs(
712 const struct device_node *parent, int port_reg, int reg)
714 struct of_endpoint endpoint;
715 struct device_node *node = NULL;
717 for_each_endpoint_of_node(parent, node) {
718 of_graph_parse_endpoint(node, &endpoint);
719 if (((port_reg == -1) || (endpoint.port == port_reg)) &&
720 ((reg == -1) || (endpoint.id == reg)))
726 EXPORT_SYMBOL(of_graph_get_endpoint_by_regs);
729 * of_graph_get_remote_endpoint() - get remote endpoint node
730 * @node: pointer to a local endpoint device_node
732 * Return: Remote endpoint node associated with remote endpoint node linked
733 * to @node. Use of_node_put() on it when done.
735 struct device_node *of_graph_get_remote_endpoint(const struct device_node *node)
737 /* Get remote endpoint node. */
738 return of_parse_phandle(node, "remote-endpoint", 0);
740 EXPORT_SYMBOL(of_graph_get_remote_endpoint);
743 * of_graph_get_port_parent() - get port's parent node
744 * @node: pointer to a local endpoint device_node
746 * Return: device node associated with endpoint node linked
747 * to @node. Use of_node_put() on it when done.
749 struct device_node *of_graph_get_port_parent(struct device_node *node)
757 * Preserve usecount for passed in node as of_get_next_parent()
758 * will do of_node_put() on it.
762 /* Walk 3 levels up only if there is 'ports' node. */
763 for (depth = 3; depth && node; depth--) {
764 node = of_get_next_parent(node);
765 if (depth == 2 && !of_node_name_eq(node, "ports"))
770 EXPORT_SYMBOL(of_graph_get_port_parent);
773 * of_graph_get_remote_port_parent() - get remote port's parent node
774 * @node: pointer to a local endpoint device_node
776 * Return: Remote device node associated with remote endpoint node linked
777 * to @node. Use of_node_put() on it when done.
779 struct device_node *of_graph_get_remote_port_parent(
780 const struct device_node *node)
782 struct device_node *np, *pp;
784 /* Get remote endpoint node. */
785 np = of_graph_get_remote_endpoint(node);
787 pp = of_graph_get_port_parent(np);
793 EXPORT_SYMBOL(of_graph_get_remote_port_parent);
796 * of_graph_get_remote_port() - get remote port node
797 * @node: pointer to a local endpoint device_node
799 * Return: Remote port node associated with remote endpoint node linked
800 * to @node. Use of_node_put() on it when done.
802 struct device_node *of_graph_get_remote_port(const struct device_node *node)
804 struct device_node *np;
806 /* Get remote endpoint node. */
807 np = of_graph_get_remote_endpoint(node);
810 return of_get_next_parent(np);
812 EXPORT_SYMBOL(of_graph_get_remote_port);
814 int of_graph_get_endpoint_count(const struct device_node *np)
816 struct device_node *endpoint;
819 for_each_endpoint_of_node(np, endpoint)
824 EXPORT_SYMBOL(of_graph_get_endpoint_count);
827 * of_graph_get_remote_node() - get remote parent device_node for given port/endpoint
828 * @node: pointer to parent device_node containing graph port/endpoint
829 * @port: identifier (value of reg property) of the parent port node
830 * @endpoint: identifier (value of reg property) of the endpoint node
832 * Return: Remote device node associated with remote endpoint node linked
833 * to @node. Use of_node_put() on it when done.
835 struct device_node *of_graph_get_remote_node(const struct device_node *node,
836 u32 port, u32 endpoint)
838 struct device_node *endpoint_node, *remote;
840 endpoint_node = of_graph_get_endpoint_by_regs(node, port, endpoint);
841 if (!endpoint_node) {
842 pr_debug("no valid endpoint (%d, %d) for node %pOF\n",
843 port, endpoint, node);
847 remote = of_graph_get_remote_port_parent(endpoint_node);
848 of_node_put(endpoint_node);
850 pr_debug("no valid remote node\n");
854 if (!of_device_is_available(remote)) {
855 pr_debug("not available for remote node\n");
862 EXPORT_SYMBOL(of_graph_get_remote_node);
864 static struct fwnode_handle *of_fwnode_get(struct fwnode_handle *fwnode)
866 return of_fwnode_handle(of_node_get(to_of_node(fwnode)));
869 static void of_fwnode_put(struct fwnode_handle *fwnode)
871 of_node_put(to_of_node(fwnode));
874 static bool of_fwnode_device_is_available(const struct fwnode_handle *fwnode)
876 return of_device_is_available(to_of_node(fwnode));
879 static bool of_fwnode_device_dma_supported(const struct fwnode_handle *fwnode)
884 static enum dev_dma_attr
885 of_fwnode_device_get_dma_attr(const struct fwnode_handle *fwnode)
887 if (of_dma_is_coherent(to_of_node(fwnode)))
888 return DEV_DMA_COHERENT;
890 return DEV_DMA_NON_COHERENT;
893 static bool of_fwnode_property_present(const struct fwnode_handle *fwnode,
894 const char *propname)
896 return of_property_read_bool(to_of_node(fwnode), propname);
899 static int of_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
900 const char *propname,
901 unsigned int elem_size, void *val,
904 const struct device_node *node = to_of_node(fwnode);
907 return of_property_count_elems_of_size(node, propname,
912 return of_property_read_u8_array(node, propname, val, nval);
914 return of_property_read_u16_array(node, propname, val, nval);
916 return of_property_read_u32_array(node, propname, val, nval);
918 return of_property_read_u64_array(node, propname, val, nval);
925 of_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
926 const char *propname, const char **val,
929 const struct device_node *node = to_of_node(fwnode);
932 of_property_read_string_array(node, propname, val, nval) :
933 of_property_count_strings(node, propname);
936 static const char *of_fwnode_get_name(const struct fwnode_handle *fwnode)
938 return kbasename(to_of_node(fwnode)->full_name);
941 static const char *of_fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
943 /* Root needs no prefix here (its name is "/"). */
944 if (!to_of_node(fwnode)->parent)
950 static struct fwnode_handle *
951 of_fwnode_get_parent(const struct fwnode_handle *fwnode)
953 return of_fwnode_handle(of_get_parent(to_of_node(fwnode)));
956 static struct fwnode_handle *
957 of_fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
958 struct fwnode_handle *child)
960 return of_fwnode_handle(of_get_next_available_child(to_of_node(fwnode),
964 static struct fwnode_handle *
965 of_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
966 const char *childname)
968 const struct device_node *node = to_of_node(fwnode);
969 struct device_node *child;
971 for_each_available_child_of_node(node, child)
972 if (of_node_name_eq(child, childname))
973 return of_fwnode_handle(child);
979 of_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
980 const char *prop, const char *nargs_prop,
981 unsigned int nargs, unsigned int index,
982 struct fwnode_reference_args *args)
984 struct of_phandle_args of_args;
989 ret = of_parse_phandle_with_args(to_of_node(fwnode), prop,
990 nargs_prop, index, &of_args);
992 ret = of_parse_phandle_with_fixed_args(to_of_node(fwnode), prop,
993 nargs, index, &of_args);
997 of_node_put(of_args.np);
1001 args->nargs = of_args.args_count;
1002 args->fwnode = of_fwnode_handle(of_args.np);
1004 for (i = 0; i < NR_FWNODE_REFERENCE_ARGS; i++)
1005 args->args[i] = i < of_args.args_count ? of_args.args[i] : 0;
1010 static struct fwnode_handle *
1011 of_fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
1012 struct fwnode_handle *prev)
1014 return of_fwnode_handle(of_graph_get_next_endpoint(to_of_node(fwnode),
1018 static struct fwnode_handle *
1019 of_fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
1021 return of_fwnode_handle(
1022 of_graph_get_remote_endpoint(to_of_node(fwnode)));
1025 static struct fwnode_handle *
1026 of_fwnode_graph_get_port_parent(struct fwnode_handle *fwnode)
1028 struct device_node *np;
1030 /* Get the parent of the port */
1031 np = of_get_parent(to_of_node(fwnode));
1035 /* Is this the "ports" node? If not, it's the port parent. */
1036 if (!of_node_name_eq(np, "ports"))
1037 return of_fwnode_handle(np);
1039 return of_fwnode_handle(of_get_next_parent(np));
1042 static int of_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
1043 struct fwnode_endpoint *endpoint)
1045 const struct device_node *node = to_of_node(fwnode);
1046 struct device_node *port_node = of_get_parent(node);
1048 endpoint->local_fwnode = fwnode;
1050 of_property_read_u32(port_node, "reg", &endpoint->port);
1051 of_property_read_u32(node, "reg", &endpoint->id);
1053 of_node_put(port_node);
1059 of_fwnode_device_get_match_data(const struct fwnode_handle *fwnode,
1060 const struct device *dev)
1062 return of_device_get_match_data(dev);
1065 static bool of_is_ancestor_of(struct device_node *test_ancestor,
1066 struct device_node *child)
1070 if (child == test_ancestor) {
1074 child = of_get_next_parent(child);
1079 static struct device_node *of_get_compat_node(struct device_node *np)
1084 if (!of_device_is_available(np)) {
1089 if (of_find_property(np, "compatible", NULL))
1092 np = of_get_next_parent(np);
1098 static struct device_node *of_get_compat_node_parent(struct device_node *np)
1100 struct device_node *parent, *node;
1102 parent = of_get_parent(np);
1103 node = of_get_compat_node(parent);
1104 of_node_put(parent);
1110 * of_link_to_phandle - Add fwnode link to supplier from supplier phandle
1111 * @con_np: consumer device tree node
1112 * @sup_np: supplier device tree node
1114 * Given a phandle to a supplier device tree node (@sup_np), this function
1115 * finds the device that owns the supplier device tree node and creates a
1116 * device link from @dev consumer device to the supplier device. This function
1117 * doesn't create device links for invalid scenarios such as trying to create a
1118 * link with a parent device as the consumer of its child device. In such
1119 * cases, it returns an error.
1122 * - 0 if fwnode link successfully created to supplier
1123 * - -EINVAL if the supplier link is invalid and should not be created
1124 * - -ENODEV if struct device will never be create for supplier
1126 static int of_link_to_phandle(struct device_node *con_np,
1127 struct device_node *sup_np)
1129 struct device *sup_dev;
1130 struct device_node *tmp_np = sup_np;
1133 * Find the device node that contains the supplier phandle. It may be
1134 * @sup_np or it may be an ancestor of @sup_np.
1136 sup_np = of_get_compat_node(sup_np);
1138 pr_debug("Not linking %pOFP to %pOFP - No device\n",
1144 * Don't allow linking a device node as a consumer of one of its
1145 * descendant nodes. By definition, a child node can't be a functional
1146 * dependency for the parent node.
1148 if (of_is_ancestor_of(con_np, sup_np)) {
1149 pr_debug("Not linking %pOFP to %pOFP - is descendant\n",
1151 of_node_put(sup_np);
1156 * Don't create links to "early devices" that won't have struct devices
1159 sup_dev = get_dev_from_fwnode(&sup_np->fwnode);
1161 (of_node_check_flag(sup_np, OF_POPULATED) ||
1162 sup_np->fwnode.flags & FWNODE_FLAG_NOT_DEVICE)) {
1163 pr_debug("Not linking %pOFP to %pOFP - No struct device\n",
1165 of_node_put(sup_np);
1168 put_device(sup_dev);
1170 fwnode_link_add(of_fwnode_handle(con_np), of_fwnode_handle(sup_np));
1171 of_node_put(sup_np);
1177 * parse_prop_cells - Property parsing function for suppliers
1179 * @np: Pointer to device tree node containing a list
1180 * @prop_name: Name of property to be parsed. Expected to hold phandle values
1181 * @index: For properties holding a list of phandles, this is the index
1183 * @list_name: Property name that is known to contain list of phandle(s) to
1185 * @cells_name: property name that specifies phandles' arguments count
1187 * This is a helper function to parse properties that have a known fixed name
1188 * and are a list of phandles and phandle arguments.
1191 * - phandle node pointer with refcount incremented. Caller must of_node_put()
1193 * - NULL if no phandle found at index
1195 static struct device_node *parse_prop_cells(struct device_node *np,
1196 const char *prop_name, int index,
1197 const char *list_name,
1198 const char *cells_name)
1200 struct of_phandle_args sup_args;
1202 if (strcmp(prop_name, list_name))
1205 if (of_parse_phandle_with_args(np, list_name, cells_name, index,
1212 #define DEFINE_SIMPLE_PROP(fname, name, cells) \
1213 static struct device_node *parse_##fname(struct device_node *np, \
1214 const char *prop_name, int index) \
1216 return parse_prop_cells(np, prop_name, index, name, cells); \
1219 static int strcmp_suffix(const char *str, const char *suffix)
1221 unsigned int len, suffix_len;
1224 suffix_len = strlen(suffix);
1225 if (len <= suffix_len)
1227 return strcmp(str + len - suffix_len, suffix);
1231 * parse_suffix_prop_cells - Suffix property parsing function for suppliers
1233 * @np: Pointer to device tree node containing a list
1234 * @prop_name: Name of property to be parsed. Expected to hold phandle values
1235 * @index: For properties holding a list of phandles, this is the index
1237 * @suffix: Property suffix that is known to contain list of phandle(s) to
1239 * @cells_name: property name that specifies phandles' arguments count
1241 * This is a helper function to parse properties that have a known fixed suffix
1242 * and are a list of phandles and phandle arguments.
1245 * - phandle node pointer with refcount incremented. Caller must of_node_put()
1247 * - NULL if no phandle found at index
1249 static struct device_node *parse_suffix_prop_cells(struct device_node *np,
1250 const char *prop_name, int index,
1252 const char *cells_name)
1254 struct of_phandle_args sup_args;
1256 if (strcmp_suffix(prop_name, suffix))
1259 if (of_parse_phandle_with_args(np, prop_name, cells_name, index,
1266 #define DEFINE_SUFFIX_PROP(fname, suffix, cells) \
1267 static struct device_node *parse_##fname(struct device_node *np, \
1268 const char *prop_name, int index) \
1270 return parse_suffix_prop_cells(np, prop_name, index, suffix, cells); \
1274 * struct supplier_bindings - Property parsing functions for suppliers
1276 * @parse_prop: function name
1277 * parse_prop() finds the node corresponding to a supplier phandle
1278 * @parse_prop.np: Pointer to device node holding supplier phandle property
1279 * @parse_prop.prop_name: Name of property holding a phandle value
1280 * @parse_prop.index: For properties holding a list of phandles, this is the
1281 * index into the list
1282 * @optional: Describes whether a supplier is mandatory or not
1283 * @node_not_dev: The consumer node containing the property is never converted
1284 * to a struct device. Instead, parse ancestor nodes for the
1285 * compatible property to find a node corresponding to a device.
1288 * parse_prop() return values are
1289 * - phandle node pointer with refcount incremented. Caller must of_node_put()
1291 * - NULL if no phandle found at index
1293 struct supplier_bindings {
1294 struct device_node *(*parse_prop)(struct device_node *np,
1295 const char *prop_name, int index);
1300 DEFINE_SIMPLE_PROP(clocks, "clocks", "#clock-cells")
1301 DEFINE_SIMPLE_PROP(interconnects, "interconnects", "#interconnect-cells")
1302 DEFINE_SIMPLE_PROP(iommus, "iommus", "#iommu-cells")
1303 DEFINE_SIMPLE_PROP(mboxes, "mboxes", "#mbox-cells")
1304 DEFINE_SIMPLE_PROP(io_channels, "io-channel", "#io-channel-cells")
1305 DEFINE_SIMPLE_PROP(interrupt_parent, "interrupt-parent", NULL)
1306 DEFINE_SIMPLE_PROP(dmas, "dmas", "#dma-cells")
1307 DEFINE_SIMPLE_PROP(power_domains, "power-domains", "#power-domain-cells")
1308 DEFINE_SIMPLE_PROP(hwlocks, "hwlocks", "#hwlock-cells")
1309 DEFINE_SIMPLE_PROP(extcon, "extcon", NULL)
1310 DEFINE_SIMPLE_PROP(nvmem_cells, "nvmem-cells", NULL)
1311 DEFINE_SIMPLE_PROP(phys, "phys", "#phy-cells")
1312 DEFINE_SIMPLE_PROP(wakeup_parent, "wakeup-parent", NULL)
1313 DEFINE_SIMPLE_PROP(pinctrl0, "pinctrl-0", NULL)
1314 DEFINE_SIMPLE_PROP(pinctrl1, "pinctrl-1", NULL)
1315 DEFINE_SIMPLE_PROP(pinctrl2, "pinctrl-2", NULL)
1316 DEFINE_SIMPLE_PROP(pinctrl3, "pinctrl-3", NULL)
1317 DEFINE_SIMPLE_PROP(pinctrl4, "pinctrl-4", NULL)
1318 DEFINE_SIMPLE_PROP(pinctrl5, "pinctrl-5", NULL)
1319 DEFINE_SIMPLE_PROP(pinctrl6, "pinctrl-6", NULL)
1320 DEFINE_SIMPLE_PROP(pinctrl7, "pinctrl-7", NULL)
1321 DEFINE_SIMPLE_PROP(pinctrl8, "pinctrl-8", NULL)
1322 DEFINE_SIMPLE_PROP(remote_endpoint, "remote-endpoint", NULL)
1323 DEFINE_SIMPLE_PROP(pwms, "pwms", "#pwm-cells")
1324 DEFINE_SIMPLE_PROP(resets, "resets", "#reset-cells")
1325 DEFINE_SIMPLE_PROP(leds, "leds", NULL)
1326 DEFINE_SIMPLE_PROP(backlight, "backlight", NULL)
1327 DEFINE_SUFFIX_PROP(regulators, "-supply", NULL)
1328 DEFINE_SUFFIX_PROP(gpio, "-gpio", "#gpio-cells")
1330 static struct device_node *parse_gpios(struct device_node *np,
1331 const char *prop_name, int index)
1333 if (!strcmp_suffix(prop_name, ",nr-gpios"))
1336 return parse_suffix_prop_cells(np, prop_name, index, "-gpios",
1340 static struct device_node *parse_iommu_maps(struct device_node *np,
1341 const char *prop_name, int index)
1343 if (strcmp(prop_name, "iommu-map"))
1346 return of_parse_phandle(np, prop_name, (index * 4) + 1);
1349 static struct device_node *parse_gpio_compat(struct device_node *np,
1350 const char *prop_name, int index)
1352 struct of_phandle_args sup_args;
1354 if (strcmp(prop_name, "gpio") && strcmp(prop_name, "gpios"))
1358 * Ignore node with gpio-hog property since its gpios are all provided
1361 if (of_find_property(np, "gpio-hog", NULL))
1364 if (of_parse_phandle_with_args(np, prop_name, "#gpio-cells", index,
1371 static struct device_node *parse_interrupts(struct device_node *np,
1372 const char *prop_name, int index)
1374 struct of_phandle_args sup_args;
1376 if (!IS_ENABLED(CONFIG_OF_IRQ) || IS_ENABLED(CONFIG_PPC))
1379 if (strcmp(prop_name, "interrupts") &&
1380 strcmp(prop_name, "interrupts-extended"))
1383 return of_irq_parse_one(np, index, &sup_args) ? NULL : sup_args.np;
1386 static const struct supplier_bindings of_supplier_bindings[] = {
1387 { .parse_prop = parse_clocks, },
1388 { .parse_prop = parse_interconnects, },
1389 { .parse_prop = parse_iommus, .optional = true, },
1390 { .parse_prop = parse_iommu_maps, .optional = true, },
1391 { .parse_prop = parse_mboxes, },
1392 { .parse_prop = parse_io_channels, },
1393 { .parse_prop = parse_interrupt_parent, },
1394 { .parse_prop = parse_dmas, .optional = true, },
1395 { .parse_prop = parse_power_domains, },
1396 { .parse_prop = parse_hwlocks, },
1397 { .parse_prop = parse_extcon, },
1398 { .parse_prop = parse_nvmem_cells, },
1399 { .parse_prop = parse_phys, },
1400 { .parse_prop = parse_wakeup_parent, },
1401 { .parse_prop = parse_pinctrl0, },
1402 { .parse_prop = parse_pinctrl1, },
1403 { .parse_prop = parse_pinctrl2, },
1404 { .parse_prop = parse_pinctrl3, },
1405 { .parse_prop = parse_pinctrl4, },
1406 { .parse_prop = parse_pinctrl5, },
1407 { .parse_prop = parse_pinctrl6, },
1408 { .parse_prop = parse_pinctrl7, },
1409 { .parse_prop = parse_pinctrl8, },
1410 { .parse_prop = parse_remote_endpoint, .node_not_dev = true, },
1411 { .parse_prop = parse_pwms, },
1412 { .parse_prop = parse_resets, },
1413 { .parse_prop = parse_leds, },
1414 { .parse_prop = parse_backlight, },
1415 { .parse_prop = parse_gpio_compat, },
1416 { .parse_prop = parse_interrupts, },
1417 { .parse_prop = parse_regulators, },
1418 { .parse_prop = parse_gpio, },
1419 { .parse_prop = parse_gpios, },
1424 * of_link_property - Create device links to suppliers listed in a property
1425 * @con_np: The consumer device tree node which contains the property
1426 * @prop_name: Name of property to be parsed
1428 * This function checks if the property @prop_name that is present in the
1429 * @con_np device tree node is one of the known common device tree bindings
1430 * that list phandles to suppliers. If @prop_name isn't one, this function
1431 * doesn't do anything.
1433 * If @prop_name is one, this function attempts to create fwnode links from the
1434 * consumer device tree node @con_np to all the suppliers device tree nodes
1435 * listed in @prop_name.
1437 * Any failed attempt to create a fwnode link will NOT result in an immediate
1438 * return. of_link_property() must create links to all the available supplier
1439 * device tree nodes even when attempts to create a link to one or more
1442 static int of_link_property(struct device_node *con_np, const char *prop_name)
1444 struct device_node *phandle;
1445 const struct supplier_bindings *s = of_supplier_bindings;
1447 bool matched = false;
1449 /* Do not stop at first failed link, link all available suppliers. */
1450 while (!matched && s->parse_prop) {
1451 if (s->optional && !fw_devlink_is_strict()) {
1456 while ((phandle = s->parse_prop(con_np, prop_name, i))) {
1457 struct device_node *con_dev_np;
1459 con_dev_np = s->node_not_dev
1460 ? of_get_compat_node_parent(con_np)
1461 : of_node_get(con_np);
1464 of_link_to_phandle(con_dev_np, phandle);
1465 of_node_put(phandle);
1466 of_node_put(con_dev_np);
1473 static void __iomem *of_fwnode_iomap(struct fwnode_handle *fwnode, int index)
1475 #ifdef CONFIG_OF_ADDRESS
1476 return of_iomap(to_of_node(fwnode), index);
1482 static int of_fwnode_irq_get(const struct fwnode_handle *fwnode,
1485 return of_irq_get(to_of_node(fwnode), index);
1488 static int of_fwnode_add_links(struct fwnode_handle *fwnode)
1491 struct device_node *con_np = to_of_node(fwnode);
1493 if (IS_ENABLED(CONFIG_X86))
1499 for_each_property_of_node(con_np, p)
1500 of_link_property(con_np, p->name);
1505 const struct fwnode_operations of_fwnode_ops = {
1506 .get = of_fwnode_get,
1507 .put = of_fwnode_put,
1508 .device_is_available = of_fwnode_device_is_available,
1509 .device_get_match_data = of_fwnode_device_get_match_data,
1510 .device_dma_supported = of_fwnode_device_dma_supported,
1511 .device_get_dma_attr = of_fwnode_device_get_dma_attr,
1512 .property_present = of_fwnode_property_present,
1513 .property_read_int_array = of_fwnode_property_read_int_array,
1514 .property_read_string_array = of_fwnode_property_read_string_array,
1515 .get_name = of_fwnode_get_name,
1516 .get_name_prefix = of_fwnode_get_name_prefix,
1517 .get_parent = of_fwnode_get_parent,
1518 .get_next_child_node = of_fwnode_get_next_child_node,
1519 .get_named_child_node = of_fwnode_get_named_child_node,
1520 .get_reference_args = of_fwnode_get_reference_args,
1521 .graph_get_next_endpoint = of_fwnode_graph_get_next_endpoint,
1522 .graph_get_remote_endpoint = of_fwnode_graph_get_remote_endpoint,
1523 .graph_get_port_parent = of_fwnode_graph_get_port_parent,
1524 .graph_parse_endpoint = of_fwnode_graph_parse_endpoint,
1525 .iomap = of_fwnode_iomap,
1526 .irq_get = of_fwnode_irq_get,
1527 .add_links = of_fwnode_add_links,
1529 EXPORT_SYMBOL_GPL(of_fwnode_ops);