Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / of / property.c
blob5f9eed79a8aaf84f42e1a4cd1ce7a00f1da82cdf
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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
8 * functions.
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
19 * Grant Likely.
22 #define pr_fmt(fmt) "OF: " fmt
24 #include <linux/of.h>
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"
32 /**
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,
37 * false otherwise.
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");
44 if (ports)
45 node = ports;
47 port = of_get_child_by_name(node, "port");
48 of_node_put(ports);
49 of_node_put(port);
51 return !!port;
53 EXPORT_SYMBOL(of_graph_is_present);
55 /**
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. Returns number of elements on sucess, -EINVAL if the
64 * property does not exist or its length does not match a multiple of elem_size
65 * and -ENODATA if the property does not have a value.
67 int of_property_count_elems_of_size(const struct device_node *np,
68 const char *propname, int elem_size)
70 struct property *prop = of_find_property(np, propname, NULL);
72 if (!prop)
73 return -EINVAL;
74 if (!prop->value)
75 return -ENODATA;
77 if (prop->length % elem_size != 0) {
78 pr_err("size of %s in node %pOF is not a multiple of %d\n",
79 propname, np, elem_size);
80 return -EINVAL;
83 return prop->length / elem_size;
85 EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
87 /**
88 * of_find_property_value_of_size
90 * @np: device node from which the property value is to be read.
91 * @propname: name of the property to be searched.
92 * @min: minimum allowed length of property value
93 * @max: maximum allowed length of property value (0 means unlimited)
94 * @len: if !=NULL, actual length is written to here
96 * Search for a property in a device node and valid the requested size.
97 * Returns the property value on success, -EINVAL if the property does not
98 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
99 * property data is too small or too large.
102 static void *of_find_property_value_of_size(const struct device_node *np,
103 const char *propname, u32 min, u32 max, size_t *len)
105 struct property *prop = of_find_property(np, propname, NULL);
107 if (!prop)
108 return ERR_PTR(-EINVAL);
109 if (!prop->value)
110 return ERR_PTR(-ENODATA);
111 if (prop->length < min)
112 return ERR_PTR(-EOVERFLOW);
113 if (max && prop->length > max)
114 return ERR_PTR(-EOVERFLOW);
116 if (len)
117 *len = prop->length;
119 return prop->value;
123 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
125 * @np: device node from which the property value is to be read.
126 * @propname: name of the property to be searched.
127 * @index: index of the u32 in the list of values
128 * @out_value: pointer to return value, modified only if no error.
130 * Search for a property in a device node and read nth 32-bit value from
131 * it. Returns 0 on success, -EINVAL if the property does not exist,
132 * -ENODATA if property does not have a value, and -EOVERFLOW if the
133 * property data isn't large enough.
135 * The out_value is modified only if a valid u32 value can be decoded.
137 int of_property_read_u32_index(const struct device_node *np,
138 const char *propname,
139 u32 index, u32 *out_value)
141 const u32 *val = of_find_property_value_of_size(np, propname,
142 ((index + 1) * sizeof(*out_value)),
144 NULL);
146 if (IS_ERR(val))
147 return PTR_ERR(val);
149 *out_value = be32_to_cpup(((__be32 *)val) + index);
150 return 0;
152 EXPORT_SYMBOL_GPL(of_property_read_u32_index);
155 * of_property_read_u64_index - Find and read a u64 from a multi-value property.
157 * @np: device node from which the property value is to be read.
158 * @propname: name of the property to be searched.
159 * @index: index of the u64 in the list of values
160 * @out_value: pointer to return value, modified only if no error.
162 * Search for a property in a device node and read nth 64-bit value from
163 * it. Returns 0 on success, -EINVAL if the property does not exist,
164 * -ENODATA if property does not have a value, and -EOVERFLOW if the
165 * property data isn't large enough.
167 * The out_value is modified only if a valid u64 value can be decoded.
169 int of_property_read_u64_index(const struct device_node *np,
170 const char *propname,
171 u32 index, u64 *out_value)
173 const u64 *val = of_find_property_value_of_size(np, propname,
174 ((index + 1) * sizeof(*out_value)),
175 0, NULL);
177 if (IS_ERR(val))
178 return PTR_ERR(val);
180 *out_value = be64_to_cpup(((__be64 *)val) + index);
181 return 0;
183 EXPORT_SYMBOL_GPL(of_property_read_u64_index);
186 * of_property_read_variable_u8_array - Find and read an array of u8 from a
187 * property, with bounds on the minimum and maximum array size.
189 * @np: device node from which the property value is to be read.
190 * @propname: name of the property to be searched.
191 * @out_values: pointer to found values.
192 * @sz_min: minimum number of array elements to read
193 * @sz_max: maximum number of array elements to read, if zero there is no
194 * upper limit on the number of elements in the dts entry but only
195 * sz_min will be read.
197 * Search for a property in a device node and read 8-bit value(s) from
198 * it. Returns number of elements read on success, -EINVAL if the property
199 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
200 * if the property data is smaller than sz_min or longer than sz_max.
202 * dts entry of array should be like:
203 * property = /bits/ 8 <0x50 0x60 0x70>;
205 * The out_values is modified only if a valid u8 value can be decoded.
207 int of_property_read_variable_u8_array(const struct device_node *np,
208 const char *propname, u8 *out_values,
209 size_t sz_min, size_t sz_max)
211 size_t sz, count;
212 const u8 *val = of_find_property_value_of_size(np, propname,
213 (sz_min * sizeof(*out_values)),
214 (sz_max * sizeof(*out_values)),
215 &sz);
217 if (IS_ERR(val))
218 return PTR_ERR(val);
220 if (!sz_max)
221 sz = sz_min;
222 else
223 sz /= sizeof(*out_values);
225 count = sz;
226 while (count--)
227 *out_values++ = *val++;
229 return sz;
231 EXPORT_SYMBOL_GPL(of_property_read_variable_u8_array);
234 * of_property_read_variable_u16_array - Find and read an array of u16 from a
235 * property, with bounds on the minimum and maximum array size.
237 * @np: device node from which the property value is to be read.
238 * @propname: name of the property to be searched.
239 * @out_values: pointer to found values.
240 * @sz_min: minimum number of array elements to read
241 * @sz_max: maximum number of array elements to read, if zero there is no
242 * upper limit on the number of elements in the dts entry but only
243 * sz_min will be read.
245 * Search for a property in a device node and read 16-bit value(s) from
246 * it. Returns number of elements read on success, -EINVAL if the property
247 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
248 * if the property data is smaller than sz_min or longer than sz_max.
250 * dts entry of array should be like:
251 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
253 * The out_values is modified only if a valid u16 value can be decoded.
255 int of_property_read_variable_u16_array(const struct device_node *np,
256 const char *propname, u16 *out_values,
257 size_t sz_min, size_t sz_max)
259 size_t sz, count;
260 const __be16 *val = of_find_property_value_of_size(np, propname,
261 (sz_min * sizeof(*out_values)),
262 (sz_max * sizeof(*out_values)),
263 &sz);
265 if (IS_ERR(val))
266 return PTR_ERR(val);
268 if (!sz_max)
269 sz = sz_min;
270 else
271 sz /= sizeof(*out_values);
273 count = sz;
274 while (count--)
275 *out_values++ = be16_to_cpup(val++);
277 return sz;
279 EXPORT_SYMBOL_GPL(of_property_read_variable_u16_array);
282 * of_property_read_variable_u32_array - Find and read an array of 32 bit
283 * integers from a property, with bounds on the minimum and maximum array size.
285 * @np: device node from which the property value is to be read.
286 * @propname: name of the property to be searched.
287 * @out_values: pointer to return found values.
288 * @sz_min: minimum number of array elements to read
289 * @sz_max: maximum number of array elements to read, if zero there is no
290 * upper limit on the number of elements in the dts entry but only
291 * sz_min will be read.
293 * Search for a property in a device node and read 32-bit value(s) from
294 * it. Returns number of elements read on success, -EINVAL if the property
295 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
296 * if the property data is smaller than sz_min or longer than sz_max.
298 * The out_values is modified only if a valid u32 value can be decoded.
300 int of_property_read_variable_u32_array(const struct device_node *np,
301 const char *propname, u32 *out_values,
302 size_t sz_min, size_t sz_max)
304 size_t sz, count;
305 const __be32 *val = of_find_property_value_of_size(np, propname,
306 (sz_min * sizeof(*out_values)),
307 (sz_max * sizeof(*out_values)),
308 &sz);
310 if (IS_ERR(val))
311 return PTR_ERR(val);
313 if (!sz_max)
314 sz = sz_min;
315 else
316 sz /= sizeof(*out_values);
318 count = sz;
319 while (count--)
320 *out_values++ = be32_to_cpup(val++);
322 return sz;
324 EXPORT_SYMBOL_GPL(of_property_read_variable_u32_array);
327 * of_property_read_u64 - Find and read a 64 bit integer from a property
328 * @np: device node from which the property value is to be read.
329 * @propname: name of the property to be searched.
330 * @out_value: pointer to return value, modified only if return value is 0.
332 * Search for a property in a device node and read a 64-bit value from
333 * it. Returns 0 on success, -EINVAL if the property does not exist,
334 * -ENODATA if property does not have a value, and -EOVERFLOW if the
335 * property data isn't large enough.
337 * The out_value is modified only if a valid u64 value can be decoded.
339 int of_property_read_u64(const struct device_node *np, const char *propname,
340 u64 *out_value)
342 const __be32 *val = of_find_property_value_of_size(np, propname,
343 sizeof(*out_value),
345 NULL);
347 if (IS_ERR(val))
348 return PTR_ERR(val);
350 *out_value = of_read_number(val, 2);
351 return 0;
353 EXPORT_SYMBOL_GPL(of_property_read_u64);
356 * of_property_read_variable_u64_array - Find and read an array of 64 bit
357 * integers from a property, with bounds on the minimum and maximum array size.
359 * @np: device node from which the property value is to be read.
360 * @propname: name of the property to be searched.
361 * @out_values: pointer to found values.
362 * @sz_min: minimum number of array elements to read
363 * @sz_max: maximum number of array elements to read, if zero there is no
364 * upper limit on the number of elements in the dts entry but only
365 * sz_min will be read.
367 * Search for a property in a device node and read 64-bit value(s) from
368 * it. Returns number of elements read on success, -EINVAL if the property
369 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
370 * if the property data is smaller than sz_min or longer than sz_max.
372 * The out_values is modified only if a valid u64 value can be decoded.
374 int of_property_read_variable_u64_array(const struct device_node *np,
375 const char *propname, u64 *out_values,
376 size_t sz_min, size_t sz_max)
378 size_t sz, count;
379 const __be32 *val = of_find_property_value_of_size(np, propname,
380 (sz_min * sizeof(*out_values)),
381 (sz_max * sizeof(*out_values)),
382 &sz);
384 if (IS_ERR(val))
385 return PTR_ERR(val);
387 if (!sz_max)
388 sz = sz_min;
389 else
390 sz /= sizeof(*out_values);
392 count = sz;
393 while (count--) {
394 *out_values++ = of_read_number(val, 2);
395 val += 2;
398 return sz;
400 EXPORT_SYMBOL_GPL(of_property_read_variable_u64_array);
403 * of_property_read_string - Find and read a string from a property
404 * @np: device node from which the property value is to be read.
405 * @propname: name of the property to be searched.
406 * @out_string: pointer to null terminated return string, modified only if
407 * return value is 0.
409 * Search for a property in a device tree node and retrieve a null
410 * terminated string value (pointer to data, not a copy). Returns 0 on
411 * success, -EINVAL if the property does not exist, -ENODATA if property
412 * does not have a value, and -EILSEQ if the string is not null-terminated
413 * within the length of the property data.
415 * The out_string pointer is modified only if a valid string can be decoded.
417 int of_property_read_string(const struct device_node *np, const char *propname,
418 const char **out_string)
420 const struct property *prop = of_find_property(np, propname, NULL);
421 if (!prop)
422 return -EINVAL;
423 if (!prop->value)
424 return -ENODATA;
425 if (strnlen(prop->value, prop->length) >= prop->length)
426 return -EILSEQ;
427 *out_string = prop->value;
428 return 0;
430 EXPORT_SYMBOL_GPL(of_property_read_string);
433 * of_property_match_string() - Find string in a list and return index
434 * @np: pointer to node containing string list property
435 * @propname: string list property name
436 * @string: pointer to string to search for in string list
438 * This function searches a string list property and returns the index
439 * of a specific string value.
441 int of_property_match_string(const struct device_node *np, const char *propname,
442 const char *string)
444 const struct property *prop = of_find_property(np, propname, NULL);
445 size_t l;
446 int i;
447 const char *p, *end;
449 if (!prop)
450 return -EINVAL;
451 if (!prop->value)
452 return -ENODATA;
454 p = prop->value;
455 end = p + prop->length;
457 for (i = 0; p < end; i++, p += l) {
458 l = strnlen(p, end - p) + 1;
459 if (p + l > end)
460 return -EILSEQ;
461 pr_debug("comparing %s with %s\n", string, p);
462 if (strcmp(string, p) == 0)
463 return i; /* Found it; return index */
465 return -ENODATA;
467 EXPORT_SYMBOL_GPL(of_property_match_string);
470 * of_property_read_string_helper() - Utility helper for parsing string properties
471 * @np: device node from which the property value is to be read.
472 * @propname: name of the property to be searched.
473 * @out_strs: output array of string pointers.
474 * @sz: number of array elements to read.
475 * @skip: Number of strings to skip over at beginning of list.
477 * Don't call this function directly. It is a utility helper for the
478 * of_property_read_string*() family of functions.
480 int of_property_read_string_helper(const struct device_node *np,
481 const char *propname, const char **out_strs,
482 size_t sz, int skip)
484 const struct property *prop = of_find_property(np, propname, NULL);
485 int l = 0, i = 0;
486 const char *p, *end;
488 if (!prop)
489 return -EINVAL;
490 if (!prop->value)
491 return -ENODATA;
492 p = prop->value;
493 end = p + prop->length;
495 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
496 l = strnlen(p, end - p) + 1;
497 if (p + l > end)
498 return -EILSEQ;
499 if (out_strs && i >= skip)
500 *out_strs++ = p;
502 i -= skip;
503 return i <= 0 ? -ENODATA : i;
505 EXPORT_SYMBOL_GPL(of_property_read_string_helper);
507 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
508 u32 *pu)
510 const void *curv = cur;
512 if (!prop)
513 return NULL;
515 if (!cur) {
516 curv = prop->value;
517 goto out_val;
520 curv += sizeof(*cur);
521 if (curv >= prop->value + prop->length)
522 return NULL;
524 out_val:
525 *pu = be32_to_cpup(curv);
526 return curv;
528 EXPORT_SYMBOL_GPL(of_prop_next_u32);
530 const char *of_prop_next_string(struct property *prop, const char *cur)
532 const void *curv = cur;
534 if (!prop)
535 return NULL;
537 if (!cur)
538 return prop->value;
540 curv += strlen(cur) + 1;
541 if (curv >= prop->value + prop->length)
542 return NULL;
544 return curv;
546 EXPORT_SYMBOL_GPL(of_prop_next_string);
549 * of_graph_parse_endpoint() - parse common endpoint node properties
550 * @node: pointer to endpoint device_node
551 * @endpoint: pointer to the OF endpoint data structure
553 * The caller should hold a reference to @node.
555 int of_graph_parse_endpoint(const struct device_node *node,
556 struct of_endpoint *endpoint)
558 struct device_node *port_node = of_get_parent(node);
560 WARN_ONCE(!port_node, "%s(): endpoint %pOF has no parent node\n",
561 __func__, node);
563 memset(endpoint, 0, sizeof(*endpoint));
565 endpoint->local_node = node;
567 * It doesn't matter whether the two calls below succeed.
568 * If they don't then the default value 0 is used.
570 of_property_read_u32(port_node, "reg", &endpoint->port);
571 of_property_read_u32(node, "reg", &endpoint->id);
573 of_node_put(port_node);
575 return 0;
577 EXPORT_SYMBOL(of_graph_parse_endpoint);
580 * of_graph_get_port_by_id() - get the port matching a given id
581 * @parent: pointer to the parent device node
582 * @id: id of the port
584 * Return: A 'port' node pointer with refcount incremented. The caller
585 * has to use of_node_put() on it when done.
587 struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id)
589 struct device_node *node, *port;
591 node = of_get_child_by_name(parent, "ports");
592 if (node)
593 parent = node;
595 for_each_child_of_node(parent, port) {
596 u32 port_id = 0;
598 if (!of_node_name_eq(port, "port"))
599 continue;
600 of_property_read_u32(port, "reg", &port_id);
601 if (id == port_id)
602 break;
605 of_node_put(node);
607 return port;
609 EXPORT_SYMBOL(of_graph_get_port_by_id);
612 * of_graph_get_next_endpoint() - get next endpoint node
613 * @parent: pointer to the parent device node
614 * @prev: previous endpoint node, or NULL to get first
616 * Return: An 'endpoint' node pointer with refcount incremented. Refcount
617 * of the passed @prev node is decremented.
619 struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
620 struct device_node *prev)
622 struct device_node *endpoint;
623 struct device_node *port;
625 if (!parent)
626 return NULL;
629 * Start by locating the port node. If no previous endpoint is specified
630 * search for the first port node, otherwise get the previous endpoint
631 * parent port node.
633 if (!prev) {
634 struct device_node *node;
636 node = of_get_child_by_name(parent, "ports");
637 if (node)
638 parent = node;
640 port = of_get_child_by_name(parent, "port");
641 of_node_put(node);
643 if (!port) {
644 pr_err("graph: no port node found in %pOF\n", parent);
645 return NULL;
647 } else {
648 port = of_get_parent(prev);
649 if (WARN_ONCE(!port, "%s(): endpoint %pOF has no parent node\n",
650 __func__, prev))
651 return NULL;
654 while (1) {
656 * Now that we have a port node, get the next endpoint by
657 * getting the next child. If the previous endpoint is NULL this
658 * will return the first child.
660 endpoint = of_get_next_child(port, prev);
661 if (endpoint) {
662 of_node_put(port);
663 return endpoint;
666 /* No more endpoints under this port, try the next one. */
667 prev = NULL;
669 do {
670 port = of_get_next_child(parent, port);
671 if (!port)
672 return NULL;
673 } while (!of_node_name_eq(port, "port"));
676 EXPORT_SYMBOL(of_graph_get_next_endpoint);
679 * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers
680 * @parent: pointer to the parent device node
681 * @port_reg: identifier (value of reg property) of the parent port node
682 * @reg: identifier (value of reg property) of the endpoint node
684 * Return: An 'endpoint' node pointer which is identified by reg and at the same
685 * is the child of a port node identified by port_reg. reg and port_reg are
686 * ignored when they are -1. Use of_node_put() on the pointer when done.
688 struct device_node *of_graph_get_endpoint_by_regs(
689 const struct device_node *parent, int port_reg, int reg)
691 struct of_endpoint endpoint;
692 struct device_node *node = NULL;
694 for_each_endpoint_of_node(parent, node) {
695 of_graph_parse_endpoint(node, &endpoint);
696 if (((port_reg == -1) || (endpoint.port == port_reg)) &&
697 ((reg == -1) || (endpoint.id == reg)))
698 return node;
701 return NULL;
703 EXPORT_SYMBOL(of_graph_get_endpoint_by_regs);
706 * of_graph_get_remote_endpoint() - get remote endpoint node
707 * @node: pointer to a local endpoint device_node
709 * Return: Remote endpoint node associated with remote endpoint node linked
710 * to @node. Use of_node_put() on it when done.
712 struct device_node *of_graph_get_remote_endpoint(const struct device_node *node)
714 /* Get remote endpoint node. */
715 return of_parse_phandle(node, "remote-endpoint", 0);
717 EXPORT_SYMBOL(of_graph_get_remote_endpoint);
720 * of_graph_get_port_parent() - get port's parent node
721 * @node: pointer to a local endpoint device_node
723 * Return: device node associated with endpoint node linked
724 * to @node. Use of_node_put() on it when done.
726 struct device_node *of_graph_get_port_parent(struct device_node *node)
728 unsigned int depth;
730 if (!node)
731 return NULL;
734 * Preserve usecount for passed in node as of_get_next_parent()
735 * will do of_node_put() on it.
737 of_node_get(node);
739 /* Walk 3 levels up only if there is 'ports' node. */
740 for (depth = 3; depth && node; depth--) {
741 node = of_get_next_parent(node);
742 if (depth == 2 && !of_node_name_eq(node, "ports"))
743 break;
745 return node;
747 EXPORT_SYMBOL(of_graph_get_port_parent);
750 * of_graph_get_remote_port_parent() - get remote port's parent node
751 * @node: pointer to a local endpoint device_node
753 * Return: Remote device node associated with remote endpoint node linked
754 * to @node. Use of_node_put() on it when done.
756 struct device_node *of_graph_get_remote_port_parent(
757 const struct device_node *node)
759 struct device_node *np, *pp;
761 /* Get remote endpoint node. */
762 np = of_graph_get_remote_endpoint(node);
764 pp = of_graph_get_port_parent(np);
766 of_node_put(np);
768 return pp;
770 EXPORT_SYMBOL(of_graph_get_remote_port_parent);
773 * of_graph_get_remote_port() - get remote port node
774 * @node: pointer to a local endpoint device_node
776 * Return: Remote port 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(const struct device_node *node)
781 struct device_node *np;
783 /* Get remote endpoint node. */
784 np = of_graph_get_remote_endpoint(node);
785 if (!np)
786 return NULL;
787 return of_get_next_parent(np);
789 EXPORT_SYMBOL(of_graph_get_remote_port);
791 int of_graph_get_endpoint_count(const struct device_node *np)
793 struct device_node *endpoint;
794 int num = 0;
796 for_each_endpoint_of_node(np, endpoint)
797 num++;
799 return num;
801 EXPORT_SYMBOL(of_graph_get_endpoint_count);
804 * of_graph_get_remote_node() - get remote parent device_node for given port/endpoint
805 * @node: pointer to parent device_node containing graph port/endpoint
806 * @port: identifier (value of reg property) of the parent port node
807 * @endpoint: identifier (value of reg property) of the endpoint node
809 * Return: Remote device node associated with remote endpoint node linked
810 * to @node. Use of_node_put() on it when done.
812 struct device_node *of_graph_get_remote_node(const struct device_node *node,
813 u32 port, u32 endpoint)
815 struct device_node *endpoint_node, *remote;
817 endpoint_node = of_graph_get_endpoint_by_regs(node, port, endpoint);
818 if (!endpoint_node) {
819 pr_debug("no valid endpoint (%d, %d) for node %pOF\n",
820 port, endpoint, node);
821 return NULL;
824 remote = of_graph_get_remote_port_parent(endpoint_node);
825 of_node_put(endpoint_node);
826 if (!remote) {
827 pr_debug("no valid remote node\n");
828 return NULL;
831 if (!of_device_is_available(remote)) {
832 pr_debug("not available for remote node\n");
833 of_node_put(remote);
834 return NULL;
837 return remote;
839 EXPORT_SYMBOL(of_graph_get_remote_node);
841 static struct fwnode_handle *of_fwnode_get(struct fwnode_handle *fwnode)
843 return of_fwnode_handle(of_node_get(to_of_node(fwnode)));
846 static void of_fwnode_put(struct fwnode_handle *fwnode)
848 of_node_put(to_of_node(fwnode));
851 static bool of_fwnode_device_is_available(const struct fwnode_handle *fwnode)
853 return of_device_is_available(to_of_node(fwnode));
856 static bool of_fwnode_property_present(const struct fwnode_handle *fwnode,
857 const char *propname)
859 return of_property_read_bool(to_of_node(fwnode), propname);
862 static int of_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
863 const char *propname,
864 unsigned int elem_size, void *val,
865 size_t nval)
867 const struct device_node *node = to_of_node(fwnode);
869 if (!val)
870 return of_property_count_elems_of_size(node, propname,
871 elem_size);
873 switch (elem_size) {
874 case sizeof(u8):
875 return of_property_read_u8_array(node, propname, val, nval);
876 case sizeof(u16):
877 return of_property_read_u16_array(node, propname, val, nval);
878 case sizeof(u32):
879 return of_property_read_u32_array(node, propname, val, nval);
880 case sizeof(u64):
881 return of_property_read_u64_array(node, propname, val, nval);
884 return -ENXIO;
887 static int
888 of_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
889 const char *propname, const char **val,
890 size_t nval)
892 const struct device_node *node = to_of_node(fwnode);
894 return val ?
895 of_property_read_string_array(node, propname, val, nval) :
896 of_property_count_strings(node, propname);
899 static const char *of_fwnode_get_name(const struct fwnode_handle *fwnode)
901 return kbasename(to_of_node(fwnode)->full_name);
904 static const char *of_fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
906 /* Root needs no prefix here (its name is "/"). */
907 if (!to_of_node(fwnode)->parent)
908 return "";
910 return "/";
913 static struct fwnode_handle *
914 of_fwnode_get_parent(const struct fwnode_handle *fwnode)
916 return of_fwnode_handle(of_get_parent(to_of_node(fwnode)));
919 static struct fwnode_handle *
920 of_fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
921 struct fwnode_handle *child)
923 return of_fwnode_handle(of_get_next_available_child(to_of_node(fwnode),
924 to_of_node(child)));
927 static struct fwnode_handle *
928 of_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
929 const char *childname)
931 const struct device_node *node = to_of_node(fwnode);
932 struct device_node *child;
934 for_each_available_child_of_node(node, child)
935 if (of_node_name_eq(child, childname))
936 return of_fwnode_handle(child);
938 return NULL;
941 static int
942 of_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
943 const char *prop, const char *nargs_prop,
944 unsigned int nargs, unsigned int index,
945 struct fwnode_reference_args *args)
947 struct of_phandle_args of_args;
948 unsigned int i;
949 int ret;
951 if (nargs_prop)
952 ret = of_parse_phandle_with_args(to_of_node(fwnode), prop,
953 nargs_prop, index, &of_args);
954 else
955 ret = of_parse_phandle_with_fixed_args(to_of_node(fwnode), prop,
956 nargs, index, &of_args);
957 if (ret < 0)
958 return ret;
959 if (!args)
960 return 0;
962 args->nargs = of_args.args_count;
963 args->fwnode = of_fwnode_handle(of_args.np);
965 for (i = 0; i < NR_FWNODE_REFERENCE_ARGS; i++)
966 args->args[i] = i < of_args.args_count ? of_args.args[i] : 0;
968 return 0;
971 static struct fwnode_handle *
972 of_fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
973 struct fwnode_handle *prev)
975 return of_fwnode_handle(of_graph_get_next_endpoint(to_of_node(fwnode),
976 to_of_node(prev)));
979 static struct fwnode_handle *
980 of_fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
982 return of_fwnode_handle(
983 of_graph_get_remote_endpoint(to_of_node(fwnode)));
986 static struct fwnode_handle *
987 of_fwnode_graph_get_port_parent(struct fwnode_handle *fwnode)
989 struct device_node *np;
991 /* Get the parent of the port */
992 np = of_get_parent(to_of_node(fwnode));
993 if (!np)
994 return NULL;
996 /* Is this the "ports" node? If not, it's the port parent. */
997 if (!of_node_name_eq(np, "ports"))
998 return of_fwnode_handle(np);
1000 return of_fwnode_handle(of_get_next_parent(np));
1003 static int of_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
1004 struct fwnode_endpoint *endpoint)
1006 const struct device_node *node = to_of_node(fwnode);
1007 struct device_node *port_node = of_get_parent(node);
1009 endpoint->local_fwnode = fwnode;
1011 of_property_read_u32(port_node, "reg", &endpoint->port);
1012 of_property_read_u32(node, "reg", &endpoint->id);
1014 of_node_put(port_node);
1016 return 0;
1019 static const void *
1020 of_fwnode_device_get_match_data(const struct fwnode_handle *fwnode,
1021 const struct device *dev)
1023 return of_device_get_match_data(dev);
1026 static bool of_is_ancestor_of(struct device_node *test_ancestor,
1027 struct device_node *child)
1029 of_node_get(child);
1030 while (child) {
1031 if (child == test_ancestor) {
1032 of_node_put(child);
1033 return true;
1035 child = of_get_next_parent(child);
1037 return false;
1041 * of_link_to_phandle - Add fwnode link to supplier from supplier phandle
1042 * @con_np: consumer device tree node
1043 * @sup_np: supplier device tree node
1045 * Given a phandle to a supplier device tree node (@sup_np), this function
1046 * finds the device that owns the supplier device tree node and creates a
1047 * device link from @dev consumer device to the supplier device. This function
1048 * doesn't create device links for invalid scenarios such as trying to create a
1049 * link with a parent device as the consumer of its child device. In such
1050 * cases, it returns an error.
1052 * Returns:
1053 * - 0 if fwnode link successfully created to supplier
1054 * - -EINVAL if the supplier link is invalid and should not be created
1055 * - -ENODEV if struct device will never be create for supplier
1057 static int of_link_to_phandle(struct device_node *con_np,
1058 struct device_node *sup_np)
1060 struct device *sup_dev;
1061 struct device_node *tmp_np = sup_np;
1063 of_node_get(sup_np);
1065 * Find the device node that contains the supplier phandle. It may be
1066 * @sup_np or it may be an ancestor of @sup_np.
1068 while (sup_np) {
1070 /* Don't allow linking to a disabled supplier */
1071 if (!of_device_is_available(sup_np)) {
1072 of_node_put(sup_np);
1073 sup_np = NULL;
1076 if (of_find_property(sup_np, "compatible", NULL))
1077 break;
1079 sup_np = of_get_next_parent(sup_np);
1082 if (!sup_np) {
1083 pr_debug("Not linking %pOFP to %pOFP - No device\n",
1084 con_np, tmp_np);
1085 return -ENODEV;
1089 * Don't allow linking a device node as a consumer of one of its
1090 * descendant nodes. By definition, a child node can't be a functional
1091 * dependency for the parent node.
1093 if (of_is_ancestor_of(con_np, sup_np)) {
1094 pr_debug("Not linking %pOFP to %pOFP - is descendant\n",
1095 con_np, sup_np);
1096 of_node_put(sup_np);
1097 return -EINVAL;
1101 * Don't create links to "early devices" that won't have struct devices
1102 * created for them.
1104 sup_dev = get_dev_from_fwnode(&sup_np->fwnode);
1105 if (!sup_dev && of_node_check_flag(sup_np, OF_POPULATED)) {
1106 pr_debug("Not linking %pOFP to %pOFP - No struct device\n",
1107 con_np, sup_np);
1108 of_node_put(sup_np);
1109 return -ENODEV;
1111 put_device(sup_dev);
1113 fwnode_link_add(of_fwnode_handle(con_np), of_fwnode_handle(sup_np));
1114 of_node_put(sup_np);
1116 return 0;
1120 * parse_prop_cells - Property parsing function for suppliers
1122 * @np: Pointer to device tree node containing a list
1123 * @prop_name: Name of property to be parsed. Expected to hold phandle values
1124 * @index: For properties holding a list of phandles, this is the index
1125 * into the list.
1126 * @list_name: Property name that is known to contain list of phandle(s) to
1127 * supplier(s)
1128 * @cells_name: property name that specifies phandles' arguments count
1130 * This is a helper function to parse properties that have a known fixed name
1131 * and are a list of phandles and phandle arguments.
1133 * Returns:
1134 * - phandle node pointer with refcount incremented. Caller must of_node_put()
1135 * on it when done.
1136 * - NULL if no phandle found at index
1138 static struct device_node *parse_prop_cells(struct device_node *np,
1139 const char *prop_name, int index,
1140 const char *list_name,
1141 const char *cells_name)
1143 struct of_phandle_args sup_args;
1145 if (strcmp(prop_name, list_name))
1146 return NULL;
1148 if (of_parse_phandle_with_args(np, list_name, cells_name, index,
1149 &sup_args))
1150 return NULL;
1152 return sup_args.np;
1155 #define DEFINE_SIMPLE_PROP(fname, name, cells) \
1156 static struct device_node *parse_##fname(struct device_node *np, \
1157 const char *prop_name, int index) \
1159 return parse_prop_cells(np, prop_name, index, name, cells); \
1162 static int strcmp_suffix(const char *str, const char *suffix)
1164 unsigned int len, suffix_len;
1166 len = strlen(str);
1167 suffix_len = strlen(suffix);
1168 if (len <= suffix_len)
1169 return -1;
1170 return strcmp(str + len - suffix_len, suffix);
1174 * parse_suffix_prop_cells - Suffix property parsing function for suppliers
1176 * @np: Pointer to device tree node containing a list
1177 * @prop_name: Name of property to be parsed. Expected to hold phandle values
1178 * @index: For properties holding a list of phandles, this is the index
1179 * into the list.
1180 * @suffix: Property suffix that is known to contain list of phandle(s) to
1181 * supplier(s)
1182 * @cells_name: property name that specifies phandles' arguments count
1184 * This is a helper function to parse properties that have a known fixed suffix
1185 * and are a list of phandles and phandle arguments.
1187 * Returns:
1188 * - phandle node pointer with refcount incremented. Caller must of_node_put()
1189 * on it when done.
1190 * - NULL if no phandle found at index
1192 static struct device_node *parse_suffix_prop_cells(struct device_node *np,
1193 const char *prop_name, int index,
1194 const char *suffix,
1195 const char *cells_name)
1197 struct of_phandle_args sup_args;
1199 if (strcmp_suffix(prop_name, suffix))
1200 return NULL;
1202 if (of_parse_phandle_with_args(np, prop_name, cells_name, index,
1203 &sup_args))
1204 return NULL;
1206 return sup_args.np;
1209 #define DEFINE_SUFFIX_PROP(fname, suffix, cells) \
1210 static struct device_node *parse_##fname(struct device_node *np, \
1211 const char *prop_name, int index) \
1213 return parse_suffix_prop_cells(np, prop_name, index, suffix, cells); \
1217 * struct supplier_bindings - Property parsing functions for suppliers
1219 * @parse_prop: function name
1220 * parse_prop() finds the node corresponding to a supplier phandle
1221 * @parse_prop.np: Pointer to device node holding supplier phandle property
1222 * @parse_prop.prop_name: Name of property holding a phandle value
1223 * @parse_prop.index: For properties holding a list of phandles, this is the
1224 * index into the list
1226 * Returns:
1227 * parse_prop() return values are
1228 * - phandle node pointer with refcount incremented. Caller must of_node_put()
1229 * on it when done.
1230 * - NULL if no phandle found at index
1232 struct supplier_bindings {
1233 struct device_node *(*parse_prop)(struct device_node *np,
1234 const char *prop_name, int index);
1237 DEFINE_SIMPLE_PROP(clocks, "clocks", "#clock-cells")
1238 DEFINE_SIMPLE_PROP(interconnects, "interconnects", "#interconnect-cells")
1239 DEFINE_SIMPLE_PROP(iommus, "iommus", "#iommu-cells")
1240 DEFINE_SIMPLE_PROP(mboxes, "mboxes", "#mbox-cells")
1241 DEFINE_SIMPLE_PROP(io_channels, "io-channel", "#io-channel-cells")
1242 DEFINE_SIMPLE_PROP(interrupt_parent, "interrupt-parent", NULL)
1243 DEFINE_SIMPLE_PROP(dmas, "dmas", "#dma-cells")
1244 DEFINE_SIMPLE_PROP(power_domains, "power-domains", "#power-domain-cells")
1245 DEFINE_SIMPLE_PROP(hwlocks, "hwlocks", "#hwlock-cells")
1246 DEFINE_SIMPLE_PROP(extcon, "extcon", NULL)
1247 DEFINE_SIMPLE_PROP(interrupts_extended, "interrupts-extended",
1248 "#interrupt-cells")
1249 DEFINE_SIMPLE_PROP(nvmem_cells, "nvmem-cells", NULL)
1250 DEFINE_SIMPLE_PROP(phys, "phys", "#phy-cells")
1251 DEFINE_SIMPLE_PROP(wakeup_parent, "wakeup-parent", NULL)
1252 DEFINE_SIMPLE_PROP(pinctrl0, "pinctrl-0", NULL)
1253 DEFINE_SIMPLE_PROP(pinctrl1, "pinctrl-1", NULL)
1254 DEFINE_SIMPLE_PROP(pinctrl2, "pinctrl-2", NULL)
1255 DEFINE_SIMPLE_PROP(pinctrl3, "pinctrl-3", NULL)
1256 DEFINE_SIMPLE_PROP(pinctrl4, "pinctrl-4", NULL)
1257 DEFINE_SIMPLE_PROP(pinctrl5, "pinctrl-5", NULL)
1258 DEFINE_SIMPLE_PROP(pinctrl6, "pinctrl-6", NULL)
1259 DEFINE_SIMPLE_PROP(pinctrl7, "pinctrl-7", NULL)
1260 DEFINE_SIMPLE_PROP(pinctrl8, "pinctrl-8", NULL)
1261 DEFINE_SUFFIX_PROP(regulators, "-supply", NULL)
1262 DEFINE_SUFFIX_PROP(gpio, "-gpio", "#gpio-cells")
1263 DEFINE_SUFFIX_PROP(gpios, "-gpios", "#gpio-cells")
1265 static struct device_node *parse_iommu_maps(struct device_node *np,
1266 const char *prop_name, int index)
1268 if (strcmp(prop_name, "iommu-map"))
1269 return NULL;
1271 return of_parse_phandle(np, prop_name, (index * 4) + 1);
1274 static const struct supplier_bindings of_supplier_bindings[] = {
1275 { .parse_prop = parse_clocks, },
1276 { .parse_prop = parse_interconnects, },
1277 { .parse_prop = parse_iommus, },
1278 { .parse_prop = parse_iommu_maps, },
1279 { .parse_prop = parse_mboxes, },
1280 { .parse_prop = parse_io_channels, },
1281 { .parse_prop = parse_interrupt_parent, },
1282 { .parse_prop = parse_dmas, },
1283 { .parse_prop = parse_power_domains, },
1284 { .parse_prop = parse_hwlocks, },
1285 { .parse_prop = parse_extcon, },
1286 { .parse_prop = parse_interrupts_extended, },
1287 { .parse_prop = parse_nvmem_cells, },
1288 { .parse_prop = parse_phys, },
1289 { .parse_prop = parse_wakeup_parent, },
1290 { .parse_prop = parse_pinctrl0, },
1291 { .parse_prop = parse_pinctrl1, },
1292 { .parse_prop = parse_pinctrl2, },
1293 { .parse_prop = parse_pinctrl3, },
1294 { .parse_prop = parse_pinctrl4, },
1295 { .parse_prop = parse_pinctrl5, },
1296 { .parse_prop = parse_pinctrl6, },
1297 { .parse_prop = parse_pinctrl7, },
1298 { .parse_prop = parse_pinctrl8, },
1299 { .parse_prop = parse_regulators, },
1300 { .parse_prop = parse_gpio, },
1301 { .parse_prop = parse_gpios, },
1306 * of_link_property - Create device links to suppliers listed in a property
1307 * @dev: Consumer device
1308 * @con_np: The consumer device tree node which contains the property
1309 * @prop_name: Name of property to be parsed
1311 * This function checks if the property @prop_name that is present in the
1312 * @con_np device tree node is one of the known common device tree bindings
1313 * that list phandles to suppliers. If @prop_name isn't one, this function
1314 * doesn't do anything.
1316 * If @prop_name is one, this function attempts to create fwnode links from the
1317 * consumer device tree node @con_np to all the suppliers device tree nodes
1318 * listed in @prop_name.
1320 * Any failed attempt to create a fwnode link will NOT result in an immediate
1321 * return. of_link_property() must create links to all the available supplier
1322 * device tree nodes even when attempts to create a link to one or more
1323 * suppliers fail.
1325 static int of_link_property(struct device_node *con_np, const char *prop_name)
1327 struct device_node *phandle;
1328 const struct supplier_bindings *s = of_supplier_bindings;
1329 unsigned int i = 0;
1330 bool matched = false;
1331 int ret = 0;
1333 /* Do not stop at first failed link, link all available suppliers. */
1334 while (!matched && s->parse_prop) {
1335 while ((phandle = s->parse_prop(con_np, prop_name, i))) {
1336 matched = true;
1337 i++;
1338 of_link_to_phandle(con_np, phandle);
1339 of_node_put(phandle);
1341 s++;
1343 return ret;
1346 static int of_fwnode_add_links(struct fwnode_handle *fwnode)
1348 struct property *p;
1349 struct device_node *con_np = to_of_node(fwnode);
1351 if (!con_np)
1352 return -EINVAL;
1354 for_each_property_of_node(con_np, p)
1355 of_link_property(con_np, p->name);
1357 return 0;
1360 const struct fwnode_operations of_fwnode_ops = {
1361 .get = of_fwnode_get,
1362 .put = of_fwnode_put,
1363 .device_is_available = of_fwnode_device_is_available,
1364 .device_get_match_data = of_fwnode_device_get_match_data,
1365 .property_present = of_fwnode_property_present,
1366 .property_read_int_array = of_fwnode_property_read_int_array,
1367 .property_read_string_array = of_fwnode_property_read_string_array,
1368 .get_name = of_fwnode_get_name,
1369 .get_name_prefix = of_fwnode_get_name_prefix,
1370 .get_parent = of_fwnode_get_parent,
1371 .get_next_child_node = of_fwnode_get_next_child_node,
1372 .get_named_child_node = of_fwnode_get_named_child_node,
1373 .get_reference_args = of_fwnode_get_reference_args,
1374 .graph_get_next_endpoint = of_fwnode_graph_get_next_endpoint,
1375 .graph_get_remote_endpoint = of_fwnode_graph_get_remote_endpoint,
1376 .graph_get_port_parent = of_fwnode_graph_get_port_parent,
1377 .graph_parse_endpoint = of_fwnode_graph_parse_endpoint,
1378 .add_links = of_fwnode_add_links,
1380 EXPORT_SYMBOL_GPL(of_fwnode_ops);