Linux 4.19.133
[linux/fpc-iii.git] / drivers / of / property.c
blob43720c2de138bf1ed13ca988ab794f0b9cdca92d
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>
29 #include "of_private.h"
31 /**
32 * of_property_count_elems_of_size - Count the number of elements in a property
34 * @np: device node from which the property value is to be read.
35 * @propname: name of the property to be searched.
36 * @elem_size: size of the individual element
38 * Search for a property in a device node and count the number of elements of
39 * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
40 * property does not exist or its length does not match a multiple of elem_size
41 * and -ENODATA if the property does not have a value.
43 int of_property_count_elems_of_size(const struct device_node *np,
44 const char *propname, int elem_size)
46 struct property *prop = of_find_property(np, propname, NULL);
48 if (!prop)
49 return -EINVAL;
50 if (!prop->value)
51 return -ENODATA;
53 if (prop->length % elem_size != 0) {
54 pr_err("size of %s in node %pOF is not a multiple of %d\n",
55 propname, np, elem_size);
56 return -EINVAL;
59 return prop->length / elem_size;
61 EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
63 /**
64 * of_find_property_value_of_size
66 * @np: device node from which the property value is to be read.
67 * @propname: name of the property to be searched.
68 * @min: minimum allowed length of property value
69 * @max: maximum allowed length of property value (0 means unlimited)
70 * @len: if !=NULL, actual length is written to here
72 * Search for a property in a device node and valid the requested size.
73 * Returns the property value on success, -EINVAL if the property does not
74 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
75 * property data is too small or too large.
78 static void *of_find_property_value_of_size(const struct device_node *np,
79 const char *propname, u32 min, u32 max, size_t *len)
81 struct property *prop = of_find_property(np, propname, NULL);
83 if (!prop)
84 return ERR_PTR(-EINVAL);
85 if (!prop->value)
86 return ERR_PTR(-ENODATA);
87 if (prop->length < min)
88 return ERR_PTR(-EOVERFLOW);
89 if (max && prop->length > max)
90 return ERR_PTR(-EOVERFLOW);
92 if (len)
93 *len = prop->length;
95 return prop->value;
98 /**
99 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
101 * @np: device node from which the property value is to be read.
102 * @propname: name of the property to be searched.
103 * @index: index of the u32 in the list of values
104 * @out_value: pointer to return value, modified only if no error.
106 * Search for a property in a device node and read nth 32-bit value from
107 * it. Returns 0 on success, -EINVAL if the property does not exist,
108 * -ENODATA if property does not have a value, and -EOVERFLOW if the
109 * property data isn't large enough.
111 * The out_value is modified only if a valid u32 value can be decoded.
113 int of_property_read_u32_index(const struct device_node *np,
114 const char *propname,
115 u32 index, u32 *out_value)
117 const u32 *val = of_find_property_value_of_size(np, propname,
118 ((index + 1) * sizeof(*out_value)),
120 NULL);
122 if (IS_ERR(val))
123 return PTR_ERR(val);
125 *out_value = be32_to_cpup(((__be32 *)val) + index);
126 return 0;
128 EXPORT_SYMBOL_GPL(of_property_read_u32_index);
131 * of_property_read_u64_index - Find and read a u64 from a multi-value property.
133 * @np: device node from which the property value is to be read.
134 * @propname: name of the property to be searched.
135 * @index: index of the u64 in the list of values
136 * @out_value: pointer to return value, modified only if no error.
138 * Search for a property in a device node and read nth 64-bit value from
139 * it. Returns 0 on success, -EINVAL if the property does not exist,
140 * -ENODATA if property does not have a value, and -EOVERFLOW if the
141 * property data isn't large enough.
143 * The out_value is modified only if a valid u64 value can be decoded.
145 int of_property_read_u64_index(const struct device_node *np,
146 const char *propname,
147 u32 index, u64 *out_value)
149 const u64 *val = of_find_property_value_of_size(np, propname,
150 ((index + 1) * sizeof(*out_value)),
151 0, NULL);
153 if (IS_ERR(val))
154 return PTR_ERR(val);
156 *out_value = be64_to_cpup(((__be64 *)val) + index);
157 return 0;
159 EXPORT_SYMBOL_GPL(of_property_read_u64_index);
162 * of_property_read_variable_u8_array - Find and read an array of u8 from a
163 * property, with bounds on the minimum and maximum array size.
165 * @np: device node from which the property value is to be read.
166 * @propname: name of the property to be searched.
167 * @out_values: pointer to return value, modified only if return value is 0.
168 * @sz_min: minimum number of array elements to read
169 * @sz_max: maximum number of array elements to read, if zero there is no
170 * upper limit on the number of elements in the dts entry but only
171 * sz_min will be read.
173 * Search for a property in a device node and read 8-bit value(s) from
174 * it. Returns number of elements read on success, -EINVAL if the property
175 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
176 * if the property data is smaller than sz_min or longer than sz_max.
178 * dts entry of array should be like:
179 * property = /bits/ 8 <0x50 0x60 0x70>;
181 * The out_values is modified only if a valid u8 value can be decoded.
183 int of_property_read_variable_u8_array(const struct device_node *np,
184 const char *propname, u8 *out_values,
185 size_t sz_min, size_t sz_max)
187 size_t sz, count;
188 const u8 *val = of_find_property_value_of_size(np, propname,
189 (sz_min * sizeof(*out_values)),
190 (sz_max * sizeof(*out_values)),
191 &sz);
193 if (IS_ERR(val))
194 return PTR_ERR(val);
196 if (!sz_max)
197 sz = sz_min;
198 else
199 sz /= sizeof(*out_values);
201 count = sz;
202 while (count--)
203 *out_values++ = *val++;
205 return sz;
207 EXPORT_SYMBOL_GPL(of_property_read_variable_u8_array);
210 * of_property_read_variable_u16_array - Find and read an array of u16 from a
211 * property, with bounds on the minimum and maximum array size.
213 * @np: device node from which the property value is to be read.
214 * @propname: name of the property to be searched.
215 * @out_values: pointer to return value, modified only if return value is 0.
216 * @sz_min: minimum number of array elements to read
217 * @sz_max: maximum number of array elements to read, if zero there is no
218 * upper limit on the number of elements in the dts entry but only
219 * sz_min will be read.
221 * Search for a property in a device node and read 16-bit value(s) from
222 * it. Returns number of elements read on success, -EINVAL if the property
223 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
224 * if the property data is smaller than sz_min or longer than sz_max.
226 * dts entry of array should be like:
227 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
229 * The out_values is modified only if a valid u16 value can be decoded.
231 int of_property_read_variable_u16_array(const struct device_node *np,
232 const char *propname, u16 *out_values,
233 size_t sz_min, size_t sz_max)
235 size_t sz, count;
236 const __be16 *val = of_find_property_value_of_size(np, propname,
237 (sz_min * sizeof(*out_values)),
238 (sz_max * sizeof(*out_values)),
239 &sz);
241 if (IS_ERR(val))
242 return PTR_ERR(val);
244 if (!sz_max)
245 sz = sz_min;
246 else
247 sz /= sizeof(*out_values);
249 count = sz;
250 while (count--)
251 *out_values++ = be16_to_cpup(val++);
253 return sz;
255 EXPORT_SYMBOL_GPL(of_property_read_variable_u16_array);
258 * of_property_read_variable_u32_array - Find and read an array of 32 bit
259 * integers from a property, with bounds on the minimum and maximum array size.
261 * @np: device node from which the property value is to be read.
262 * @propname: name of the property to be searched.
263 * @out_values: pointer to return value, modified only if return value is 0.
264 * @sz_min: minimum number of array elements to read
265 * @sz_max: maximum number of array elements to read, if zero there is no
266 * upper limit on the number of elements in the dts entry but only
267 * sz_min will be read.
269 * Search for a property in a device node and read 32-bit value(s) from
270 * it. Returns number of elements read on success, -EINVAL if the property
271 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
272 * if the property data is smaller than sz_min or longer than sz_max.
274 * The out_values is modified only if a valid u32 value can be decoded.
276 int of_property_read_variable_u32_array(const struct device_node *np,
277 const char *propname, u32 *out_values,
278 size_t sz_min, size_t sz_max)
280 size_t sz, count;
281 const __be32 *val = of_find_property_value_of_size(np, propname,
282 (sz_min * sizeof(*out_values)),
283 (sz_max * sizeof(*out_values)),
284 &sz);
286 if (IS_ERR(val))
287 return PTR_ERR(val);
289 if (!sz_max)
290 sz = sz_min;
291 else
292 sz /= sizeof(*out_values);
294 count = sz;
295 while (count--)
296 *out_values++ = be32_to_cpup(val++);
298 return sz;
300 EXPORT_SYMBOL_GPL(of_property_read_variable_u32_array);
303 * of_property_read_u64 - Find and read a 64 bit integer from a property
304 * @np: device node from which the property value is to be read.
305 * @propname: name of the property to be searched.
306 * @out_value: pointer to return value, modified only if return value is 0.
308 * Search for a property in a device node and read a 64-bit value from
309 * it. Returns 0 on success, -EINVAL if the property does not exist,
310 * -ENODATA if property does not have a value, and -EOVERFLOW if the
311 * property data isn't large enough.
313 * The out_value is modified only if a valid u64 value can be decoded.
315 int of_property_read_u64(const struct device_node *np, const char *propname,
316 u64 *out_value)
318 const __be32 *val = of_find_property_value_of_size(np, propname,
319 sizeof(*out_value),
321 NULL);
323 if (IS_ERR(val))
324 return PTR_ERR(val);
326 *out_value = of_read_number(val, 2);
327 return 0;
329 EXPORT_SYMBOL_GPL(of_property_read_u64);
332 * of_property_read_variable_u64_array - Find and read an array of 64 bit
333 * integers from a property, with bounds on the minimum and maximum array size.
335 * @np: device node from which the property value is to be read.
336 * @propname: name of the property to be searched.
337 * @out_values: pointer to return value, modified only if return value is 0.
338 * @sz_min: minimum number of array elements to read
339 * @sz_max: maximum number of array elements to read, if zero there is no
340 * upper limit on the number of elements in the dts entry but only
341 * sz_min will be read.
343 * Search for a property in a device node and read 64-bit value(s) from
344 * it. Returns number of elements read on success, -EINVAL if the property
345 * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
346 * if the property data is smaller than sz_min or longer than sz_max.
348 * The out_values is modified only if a valid u64 value can be decoded.
350 int of_property_read_variable_u64_array(const struct device_node *np,
351 const char *propname, u64 *out_values,
352 size_t sz_min, size_t sz_max)
354 size_t sz, count;
355 const __be32 *val = of_find_property_value_of_size(np, propname,
356 (sz_min * sizeof(*out_values)),
357 (sz_max * sizeof(*out_values)),
358 &sz);
360 if (IS_ERR(val))
361 return PTR_ERR(val);
363 if (!sz_max)
364 sz = sz_min;
365 else
366 sz /= sizeof(*out_values);
368 count = sz;
369 while (count--) {
370 *out_values++ = of_read_number(val, 2);
371 val += 2;
374 return sz;
376 EXPORT_SYMBOL_GPL(of_property_read_variable_u64_array);
379 * of_property_read_string - Find and read a string from a property
380 * @np: device node from which the property value is to be read.
381 * @propname: name of the property to be searched.
382 * @out_string: pointer to null terminated return string, modified only if
383 * return value is 0.
385 * Search for a property in a device tree node and retrieve a null
386 * terminated string value (pointer to data, not a copy). Returns 0 on
387 * success, -EINVAL if the property does not exist, -ENODATA if property
388 * does not have a value, and -EILSEQ if the string is not null-terminated
389 * within the length of the property data.
391 * The out_string pointer is modified only if a valid string can be decoded.
393 int of_property_read_string(const struct device_node *np, const char *propname,
394 const char **out_string)
396 const struct property *prop = of_find_property(np, propname, NULL);
397 if (!prop)
398 return -EINVAL;
399 if (!prop->value)
400 return -ENODATA;
401 if (strnlen(prop->value, prop->length) >= prop->length)
402 return -EILSEQ;
403 *out_string = prop->value;
404 return 0;
406 EXPORT_SYMBOL_GPL(of_property_read_string);
409 * of_property_match_string() - Find string in a list and return index
410 * @np: pointer to node containing string list property
411 * @propname: string list property name
412 * @string: pointer to string to search for in string list
414 * This function searches a string list property and returns the index
415 * of a specific string value.
417 int of_property_match_string(const struct device_node *np, const char *propname,
418 const char *string)
420 const struct property *prop = of_find_property(np, propname, NULL);
421 size_t l;
422 int i;
423 const char *p, *end;
425 if (!prop)
426 return -EINVAL;
427 if (!prop->value)
428 return -ENODATA;
430 p = prop->value;
431 end = p + prop->length;
433 for (i = 0; p < end; i++, p += l) {
434 l = strnlen(p, end - p) + 1;
435 if (p + l > end)
436 return -EILSEQ;
437 pr_debug("comparing %s with %s\n", string, p);
438 if (strcmp(string, p) == 0)
439 return i; /* Found it; return index */
441 return -ENODATA;
443 EXPORT_SYMBOL_GPL(of_property_match_string);
446 * of_property_read_string_helper() - Utility helper for parsing string properties
447 * @np: device node from which the property value is to be read.
448 * @propname: name of the property to be searched.
449 * @out_strs: output array of string pointers.
450 * @sz: number of array elements to read.
451 * @skip: Number of strings to skip over at beginning of list.
453 * Don't call this function directly. It is a utility helper for the
454 * of_property_read_string*() family of functions.
456 int of_property_read_string_helper(const struct device_node *np,
457 const char *propname, const char **out_strs,
458 size_t sz, int skip)
460 const struct property *prop = of_find_property(np, propname, NULL);
461 int l = 0, i = 0;
462 const char *p, *end;
464 if (!prop)
465 return -EINVAL;
466 if (!prop->value)
467 return -ENODATA;
468 p = prop->value;
469 end = p + prop->length;
471 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
472 l = strnlen(p, end - p) + 1;
473 if (p + l > end)
474 return -EILSEQ;
475 if (out_strs && i >= skip)
476 *out_strs++ = p;
478 i -= skip;
479 return i <= 0 ? -ENODATA : i;
481 EXPORT_SYMBOL_GPL(of_property_read_string_helper);
483 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
484 u32 *pu)
486 const void *curv = cur;
488 if (!prop)
489 return NULL;
491 if (!cur) {
492 curv = prop->value;
493 goto out_val;
496 curv += sizeof(*cur);
497 if (curv >= prop->value + prop->length)
498 return NULL;
500 out_val:
501 *pu = be32_to_cpup(curv);
502 return curv;
504 EXPORT_SYMBOL_GPL(of_prop_next_u32);
506 const char *of_prop_next_string(struct property *prop, const char *cur)
508 const void *curv = cur;
510 if (!prop)
511 return NULL;
513 if (!cur)
514 return prop->value;
516 curv += strlen(cur) + 1;
517 if (curv >= prop->value + prop->length)
518 return NULL;
520 return curv;
522 EXPORT_SYMBOL_GPL(of_prop_next_string);
525 * of_graph_parse_endpoint() - parse common endpoint node properties
526 * @node: pointer to endpoint device_node
527 * @endpoint: pointer to the OF endpoint data structure
529 * The caller should hold a reference to @node.
531 int of_graph_parse_endpoint(const struct device_node *node,
532 struct of_endpoint *endpoint)
534 struct device_node *port_node = of_get_parent(node);
536 WARN_ONCE(!port_node, "%s(): endpoint %pOF has no parent node\n",
537 __func__, node);
539 memset(endpoint, 0, sizeof(*endpoint));
541 endpoint->local_node = node;
543 * It doesn't matter whether the two calls below succeed.
544 * If they don't then the default value 0 is used.
546 of_property_read_u32(port_node, "reg", &endpoint->port);
547 of_property_read_u32(node, "reg", &endpoint->id);
549 of_node_put(port_node);
551 return 0;
553 EXPORT_SYMBOL(of_graph_parse_endpoint);
556 * of_graph_get_port_by_id() - get the port matching a given id
557 * @parent: pointer to the parent device node
558 * @id: id of the port
560 * Return: A 'port' node pointer with refcount incremented. The caller
561 * has to use of_node_put() on it when done.
563 struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id)
565 struct device_node *node, *port;
567 node = of_get_child_by_name(parent, "ports");
568 if (node)
569 parent = node;
571 for_each_child_of_node(parent, port) {
572 u32 port_id = 0;
574 if (of_node_cmp(port->name, "port") != 0)
575 continue;
576 of_property_read_u32(port, "reg", &port_id);
577 if (id == port_id)
578 break;
581 of_node_put(node);
583 return port;
585 EXPORT_SYMBOL(of_graph_get_port_by_id);
588 * of_graph_get_next_endpoint() - get next endpoint node
589 * @parent: pointer to the parent device node
590 * @prev: previous endpoint node, or NULL to get first
592 * Return: An 'endpoint' node pointer with refcount incremented. Refcount
593 * of the passed @prev node is decremented.
595 struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
596 struct device_node *prev)
598 struct device_node *endpoint;
599 struct device_node *port;
601 if (!parent)
602 return NULL;
605 * Start by locating the port node. If no previous endpoint is specified
606 * search for the first port node, otherwise get the previous endpoint
607 * parent port node.
609 if (!prev) {
610 struct device_node *node;
612 node = of_get_child_by_name(parent, "ports");
613 if (node)
614 parent = node;
616 port = of_get_child_by_name(parent, "port");
617 of_node_put(node);
619 if (!port) {
620 pr_err("graph: no port node found in %pOF\n", parent);
621 return NULL;
623 } else {
624 port = of_get_parent(prev);
625 if (WARN_ONCE(!port, "%s(): endpoint %pOF has no parent node\n",
626 __func__, prev))
627 return NULL;
630 while (1) {
632 * Now that we have a port node, get the next endpoint by
633 * getting the next child. If the previous endpoint is NULL this
634 * will return the first child.
636 endpoint = of_get_next_child(port, prev);
637 if (endpoint) {
638 of_node_put(port);
639 return endpoint;
642 /* No more endpoints under this port, try the next one. */
643 prev = NULL;
645 do {
646 port = of_get_next_child(parent, port);
647 if (!port)
648 return NULL;
649 } while (of_node_cmp(port->name, "port"));
652 EXPORT_SYMBOL(of_graph_get_next_endpoint);
655 * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers
656 * @parent: pointer to the parent device node
657 * @port_reg: identifier (value of reg property) of the parent port node
658 * @reg: identifier (value of reg property) of the endpoint node
660 * Return: An 'endpoint' node pointer which is identified by reg and at the same
661 * is the child of a port node identified by port_reg. reg and port_reg are
662 * ignored when they are -1.
664 struct device_node *of_graph_get_endpoint_by_regs(
665 const struct device_node *parent, int port_reg, int reg)
667 struct of_endpoint endpoint;
668 struct device_node *node = NULL;
670 for_each_endpoint_of_node(parent, node) {
671 of_graph_parse_endpoint(node, &endpoint);
672 if (((port_reg == -1) || (endpoint.port == port_reg)) &&
673 ((reg == -1) || (endpoint.id == reg)))
674 return node;
677 return NULL;
679 EXPORT_SYMBOL(of_graph_get_endpoint_by_regs);
682 * of_graph_get_remote_endpoint() - get remote endpoint node
683 * @node: pointer to a local endpoint device_node
685 * Return: Remote endpoint node associated with remote endpoint node linked
686 * to @node. Use of_node_put() on it when done.
688 struct device_node *of_graph_get_remote_endpoint(const struct device_node *node)
690 /* Get remote endpoint node. */
691 return of_parse_phandle(node, "remote-endpoint", 0);
693 EXPORT_SYMBOL(of_graph_get_remote_endpoint);
696 * of_graph_get_port_parent() - get port's parent node
697 * @node: pointer to a local endpoint device_node
699 * Return: device node associated with endpoint node linked
700 * to @node. Use of_node_put() on it when done.
702 struct device_node *of_graph_get_port_parent(struct device_node *node)
704 unsigned int depth;
706 if (!node)
707 return NULL;
710 * Preserve usecount for passed in node as of_get_next_parent()
711 * will do of_node_put() on it.
713 of_node_get(node);
715 /* Walk 3 levels up only if there is 'ports' node. */
716 for (depth = 3; depth && node; depth--) {
717 node = of_get_next_parent(node);
718 if (depth == 2 && of_node_cmp(node->name, "ports"))
719 break;
721 return node;
723 EXPORT_SYMBOL(of_graph_get_port_parent);
726 * of_graph_get_remote_port_parent() - get remote port's parent node
727 * @node: pointer to a local endpoint device_node
729 * Return: Remote device node associated with remote endpoint node linked
730 * to @node. Use of_node_put() on it when done.
732 struct device_node *of_graph_get_remote_port_parent(
733 const struct device_node *node)
735 struct device_node *np, *pp;
737 /* Get remote endpoint node. */
738 np = of_graph_get_remote_endpoint(node);
740 pp = of_graph_get_port_parent(np);
742 of_node_put(np);
744 return pp;
746 EXPORT_SYMBOL(of_graph_get_remote_port_parent);
749 * of_graph_get_remote_port() - get remote port node
750 * @node: pointer to a local endpoint device_node
752 * Return: Remote port node associated with remote endpoint node linked
753 * to @node. Use of_node_put() on it when done.
755 struct device_node *of_graph_get_remote_port(const struct device_node *node)
757 struct device_node *np;
759 /* Get remote endpoint node. */
760 np = of_graph_get_remote_endpoint(node);
761 if (!np)
762 return NULL;
763 return of_get_next_parent(np);
765 EXPORT_SYMBOL(of_graph_get_remote_port);
767 int of_graph_get_endpoint_count(const struct device_node *np)
769 struct device_node *endpoint;
770 int num = 0;
772 for_each_endpoint_of_node(np, endpoint)
773 num++;
775 return num;
777 EXPORT_SYMBOL(of_graph_get_endpoint_count);
780 * of_graph_get_remote_node() - get remote parent device_node for given port/endpoint
781 * @node: pointer to parent device_node containing graph port/endpoint
782 * @port: identifier (value of reg property) of the parent port node
783 * @endpoint: identifier (value of reg property) of the endpoint node
785 * Return: Remote device node associated with remote endpoint node linked
786 * to @node. Use of_node_put() on it when done.
788 struct device_node *of_graph_get_remote_node(const struct device_node *node,
789 u32 port, u32 endpoint)
791 struct device_node *endpoint_node, *remote;
793 endpoint_node = of_graph_get_endpoint_by_regs(node, port, endpoint);
794 if (!endpoint_node) {
795 pr_debug("no valid endpoint (%d, %d) for node %pOF\n",
796 port, endpoint, node);
797 return NULL;
800 remote = of_graph_get_remote_port_parent(endpoint_node);
801 of_node_put(endpoint_node);
802 if (!remote) {
803 pr_debug("no valid remote node\n");
804 return NULL;
807 if (!of_device_is_available(remote)) {
808 pr_debug("not available for remote node\n");
809 of_node_put(remote);
810 return NULL;
813 return remote;
815 EXPORT_SYMBOL(of_graph_get_remote_node);
817 static struct fwnode_handle *of_fwnode_get(struct fwnode_handle *fwnode)
819 return of_fwnode_handle(of_node_get(to_of_node(fwnode)));
822 static void of_fwnode_put(struct fwnode_handle *fwnode)
824 of_node_put(to_of_node(fwnode));
827 static bool of_fwnode_device_is_available(const struct fwnode_handle *fwnode)
829 return of_device_is_available(to_of_node(fwnode));
832 static bool of_fwnode_property_present(const struct fwnode_handle *fwnode,
833 const char *propname)
835 return of_property_read_bool(to_of_node(fwnode), propname);
838 static int of_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
839 const char *propname,
840 unsigned int elem_size, void *val,
841 size_t nval)
843 const struct device_node *node = to_of_node(fwnode);
845 if (!val)
846 return of_property_count_elems_of_size(node, propname,
847 elem_size);
849 switch (elem_size) {
850 case sizeof(u8):
851 return of_property_read_u8_array(node, propname, val, nval);
852 case sizeof(u16):
853 return of_property_read_u16_array(node, propname, val, nval);
854 case sizeof(u32):
855 return of_property_read_u32_array(node, propname, val, nval);
856 case sizeof(u64):
857 return of_property_read_u64_array(node, propname, val, nval);
860 return -ENXIO;
863 static int
864 of_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
865 const char *propname, const char **val,
866 size_t nval)
868 const struct device_node *node = to_of_node(fwnode);
870 return val ?
871 of_property_read_string_array(node, propname, val, nval) :
872 of_property_count_strings(node, propname);
875 static struct fwnode_handle *
876 of_fwnode_get_parent(const struct fwnode_handle *fwnode)
878 return of_fwnode_handle(of_get_parent(to_of_node(fwnode)));
881 static struct fwnode_handle *
882 of_fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
883 struct fwnode_handle *child)
885 return of_fwnode_handle(of_get_next_available_child(to_of_node(fwnode),
886 to_of_node(child)));
889 static struct fwnode_handle *
890 of_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
891 const char *childname)
893 const struct device_node *node = to_of_node(fwnode);
894 struct device_node *child;
896 for_each_available_child_of_node(node, child)
897 if (!of_node_cmp(child->name, childname))
898 return of_fwnode_handle(child);
900 return NULL;
903 static int
904 of_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
905 const char *prop, const char *nargs_prop,
906 unsigned int nargs, unsigned int index,
907 struct fwnode_reference_args *args)
909 struct of_phandle_args of_args;
910 unsigned int i;
911 int ret;
913 if (nargs_prop)
914 ret = of_parse_phandle_with_args(to_of_node(fwnode), prop,
915 nargs_prop, index, &of_args);
916 else
917 ret = of_parse_phandle_with_fixed_args(to_of_node(fwnode), prop,
918 nargs, index, &of_args);
919 if (ret < 0)
920 return ret;
921 if (!args)
922 return 0;
924 args->nargs = of_args.args_count;
925 args->fwnode = of_fwnode_handle(of_args.np);
927 for (i = 0; i < NR_FWNODE_REFERENCE_ARGS; i++)
928 args->args[i] = i < of_args.args_count ? of_args.args[i] : 0;
930 return 0;
933 static struct fwnode_handle *
934 of_fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
935 struct fwnode_handle *prev)
937 return of_fwnode_handle(of_graph_get_next_endpoint(to_of_node(fwnode),
938 to_of_node(prev)));
941 static struct fwnode_handle *
942 of_fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
944 return of_fwnode_handle(
945 of_graph_get_remote_endpoint(to_of_node(fwnode)));
948 static struct fwnode_handle *
949 of_fwnode_graph_get_port_parent(struct fwnode_handle *fwnode)
951 struct device_node *np;
953 /* Get the parent of the port */
954 np = of_get_parent(to_of_node(fwnode));
955 if (!np)
956 return NULL;
958 /* Is this the "ports" node? If not, it's the port parent. */
959 if (of_node_cmp(np->name, "ports"))
960 return of_fwnode_handle(np);
962 return of_fwnode_handle(of_get_next_parent(np));
965 static int of_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
966 struct fwnode_endpoint *endpoint)
968 const struct device_node *node = to_of_node(fwnode);
969 struct device_node *port_node = of_get_parent(node);
971 endpoint->local_fwnode = fwnode;
973 of_property_read_u32(port_node, "reg", &endpoint->port);
974 of_property_read_u32(node, "reg", &endpoint->id);
976 of_node_put(port_node);
978 return 0;
981 static const void *
982 of_fwnode_device_get_match_data(const struct fwnode_handle *fwnode,
983 const struct device *dev)
985 return of_device_get_match_data(dev);
988 const struct fwnode_operations of_fwnode_ops = {
989 .get = of_fwnode_get,
990 .put = of_fwnode_put,
991 .device_is_available = of_fwnode_device_is_available,
992 .device_get_match_data = of_fwnode_device_get_match_data,
993 .property_present = of_fwnode_property_present,
994 .property_read_int_array = of_fwnode_property_read_int_array,
995 .property_read_string_array = of_fwnode_property_read_string_array,
996 .get_parent = of_fwnode_get_parent,
997 .get_next_child_node = of_fwnode_get_next_child_node,
998 .get_named_child_node = of_fwnode_get_named_child_node,
999 .get_reference_args = of_fwnode_get_reference_args,
1000 .graph_get_next_endpoint = of_fwnode_graph_get_next_endpoint,
1001 .graph_get_remote_endpoint = of_fwnode_graph_get_remote_endpoint,
1002 .graph_get_port_parent = of_fwnode_graph_get_port_parent,
1003 .graph_parse_endpoint = of_fwnode_graph_parse_endpoint,
1005 EXPORT_SYMBOL_GPL(of_fwnode_ops);