Merge tag 'nios2-v4.19-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/lftan...
[linux/fpc-iii.git] / drivers / of / property.c
blobf46828e3b082b87966d5c5be3d7df1637f0391a9
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 return NULL;
812 return remote;
814 EXPORT_SYMBOL(of_graph_get_remote_node);
816 static struct fwnode_handle *of_fwnode_get(struct fwnode_handle *fwnode)
818 return of_fwnode_handle(of_node_get(to_of_node(fwnode)));
821 static void of_fwnode_put(struct fwnode_handle *fwnode)
823 of_node_put(to_of_node(fwnode));
826 static bool of_fwnode_device_is_available(const struct fwnode_handle *fwnode)
828 return of_device_is_available(to_of_node(fwnode));
831 static bool of_fwnode_property_present(const struct fwnode_handle *fwnode,
832 const char *propname)
834 return of_property_read_bool(to_of_node(fwnode), propname);
837 static int of_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
838 const char *propname,
839 unsigned int elem_size, void *val,
840 size_t nval)
842 const struct device_node *node = to_of_node(fwnode);
844 if (!val)
845 return of_property_count_elems_of_size(node, propname,
846 elem_size);
848 switch (elem_size) {
849 case sizeof(u8):
850 return of_property_read_u8_array(node, propname, val, nval);
851 case sizeof(u16):
852 return of_property_read_u16_array(node, propname, val, nval);
853 case sizeof(u32):
854 return of_property_read_u32_array(node, propname, val, nval);
855 case sizeof(u64):
856 return of_property_read_u64_array(node, propname, val, nval);
859 return -ENXIO;
862 static int
863 of_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
864 const char *propname, const char **val,
865 size_t nval)
867 const struct device_node *node = to_of_node(fwnode);
869 return val ?
870 of_property_read_string_array(node, propname, val, nval) :
871 of_property_count_strings(node, propname);
874 static struct fwnode_handle *
875 of_fwnode_get_parent(const struct fwnode_handle *fwnode)
877 return of_fwnode_handle(of_get_parent(to_of_node(fwnode)));
880 static struct fwnode_handle *
881 of_fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
882 struct fwnode_handle *child)
884 return of_fwnode_handle(of_get_next_available_child(to_of_node(fwnode),
885 to_of_node(child)));
888 static struct fwnode_handle *
889 of_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
890 const char *childname)
892 const struct device_node *node = to_of_node(fwnode);
893 struct device_node *child;
895 for_each_available_child_of_node(node, child)
896 if (!of_node_cmp(child->name, childname))
897 return of_fwnode_handle(child);
899 return NULL;
902 static int
903 of_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
904 const char *prop, const char *nargs_prop,
905 unsigned int nargs, unsigned int index,
906 struct fwnode_reference_args *args)
908 struct of_phandle_args of_args;
909 unsigned int i;
910 int ret;
912 if (nargs_prop)
913 ret = of_parse_phandle_with_args(to_of_node(fwnode), prop,
914 nargs_prop, index, &of_args);
915 else
916 ret = of_parse_phandle_with_fixed_args(to_of_node(fwnode), prop,
917 nargs, index, &of_args);
918 if (ret < 0)
919 return ret;
920 if (!args)
921 return 0;
923 args->nargs = of_args.args_count;
924 args->fwnode = of_fwnode_handle(of_args.np);
926 for (i = 0; i < NR_FWNODE_REFERENCE_ARGS; i++)
927 args->args[i] = i < of_args.args_count ? of_args.args[i] : 0;
929 return 0;
932 static struct fwnode_handle *
933 of_fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
934 struct fwnode_handle *prev)
936 return of_fwnode_handle(of_graph_get_next_endpoint(to_of_node(fwnode),
937 to_of_node(prev)));
940 static struct fwnode_handle *
941 of_fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
943 return of_fwnode_handle(
944 of_graph_get_remote_endpoint(to_of_node(fwnode)));
947 static struct fwnode_handle *
948 of_fwnode_graph_get_port_parent(struct fwnode_handle *fwnode)
950 struct device_node *np;
952 /* Get the parent of the port */
953 np = of_get_parent(to_of_node(fwnode));
954 if (!np)
955 return NULL;
957 /* Is this the "ports" node? If not, it's the port parent. */
958 if (of_node_cmp(np->name, "ports"))
959 return of_fwnode_handle(np);
961 return of_fwnode_handle(of_get_next_parent(np));
964 static int of_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
965 struct fwnode_endpoint *endpoint)
967 const struct device_node *node = to_of_node(fwnode);
968 struct device_node *port_node = of_get_parent(node);
970 endpoint->local_fwnode = fwnode;
972 of_property_read_u32(port_node, "reg", &endpoint->port);
973 of_property_read_u32(node, "reg", &endpoint->id);
975 of_node_put(port_node);
977 return 0;
980 static const void *
981 of_fwnode_device_get_match_data(const struct fwnode_handle *fwnode,
982 const struct device *dev)
984 return of_device_get_match_data(dev);
987 const struct fwnode_operations of_fwnode_ops = {
988 .get = of_fwnode_get,
989 .put = of_fwnode_put,
990 .device_is_available = of_fwnode_device_is_available,
991 .device_get_match_data = of_fwnode_device_get_match_data,
992 .property_present = of_fwnode_property_present,
993 .property_read_int_array = of_fwnode_property_read_int_array,
994 .property_read_string_array = of_fwnode_property_read_string_array,
995 .get_parent = of_fwnode_get_parent,
996 .get_next_child_node = of_fwnode_get_next_child_node,
997 .get_named_child_node = of_fwnode_get_named_child_node,
998 .get_reference_args = of_fwnode_get_reference_args,
999 .graph_get_next_endpoint = of_fwnode_graph_get_next_endpoint,
1000 .graph_get_remote_endpoint = of_fwnode_graph_get_remote_endpoint,
1001 .graph_get_port_parent = of_fwnode_graph_get_port_parent,
1002 .graph_parse_endpoint = of_fwnode_graph_parse_endpoint,
1004 EXPORT_SYMBOL_GPL(of_fwnode_ops);