Merge branch 'next' into for-linus
[linux-2.6-microblaze.git] / drivers / of / property.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * drivers/of/property.c - Procedures for accessing and interpreting
4  *                         Devicetree properties and graphs.
5  *
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.
9  *
10  * Paul Mackerras       August 1996.
11  * Copyright (C) 1996-2005 Paul Mackerras.
12  *
13  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
14  *    {engebret|bergner}@us.ibm.com
15  *
16  *  Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
17  *
18  *  Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
19  *  Grant Likely.
20  */
21
22 #define pr_fmt(fmt)     "OF: " fmt
23
24 #include <linux/of.h>
25 #include <linux/of_device.h>
26 #include <linux/of_graph.h>
27 #include <linux/of_irq.h>
28 #include <linux/string.h>
29 #include <linux/moduleparam.h>
30
31 #include "of_private.h"
32
33 /**
34  * of_graph_is_present() - check graph's presence
35  * @node: pointer to device_node containing graph port
36  *
37  * Return: True if @node has a port or ports (with a port) sub-node,
38  * false otherwise.
39  */
40 bool of_graph_is_present(const struct device_node *node)
41 {
42         struct device_node *ports, *port;
43
44         ports = of_get_child_by_name(node, "ports");
45         if (ports)
46                 node = ports;
47
48         port = of_get_child_by_name(node, "port");
49         of_node_put(ports);
50         of_node_put(port);
51
52         return !!port;
53 }
54 EXPORT_SYMBOL(of_graph_is_present);
55
56 /**
57  * of_property_count_elems_of_size - Count the number of elements in a property
58  *
59  * @np:         device node from which the property value is to be read.
60  * @propname:   name of the property to be searched.
61  * @elem_size:  size of the individual element
62  *
63  * Search for a property in a device node and count the number of elements of
64  * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
65  * property does not exist or its length does not match a multiple of elem_size
66  * and -ENODATA if the property does not have a value.
67  */
68 int of_property_count_elems_of_size(const struct device_node *np,
69                                 const char *propname, int elem_size)
70 {
71         struct property *prop = of_find_property(np, propname, NULL);
72
73         if (!prop)
74                 return -EINVAL;
75         if (!prop->value)
76                 return -ENODATA;
77
78         if (prop->length % elem_size != 0) {
79                 pr_err("size of %s in node %pOF is not a multiple of %d\n",
80                        propname, np, elem_size);
81                 return -EINVAL;
82         }
83
84         return prop->length / elem_size;
85 }
86 EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
87
88 /**
89  * of_find_property_value_of_size
90  *
91  * @np:         device node from which the property value is to be read.
92  * @propname:   name of the property to be searched.
93  * @min:        minimum allowed length of property value
94  * @max:        maximum allowed length of property value (0 means unlimited)
95  * @len:        if !=NULL, actual length is written to here
96  *
97  * Search for a property in a device node and valid the requested size.
98  * Returns the property value on success, -EINVAL if the property does not
99  *  exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
100  * property data is too small or too large.
101  *
102  */
103 static void *of_find_property_value_of_size(const struct device_node *np,
104                         const char *propname, u32 min, u32 max, size_t *len)
105 {
106         struct property *prop = of_find_property(np, propname, NULL);
107
108         if (!prop)
109                 return ERR_PTR(-EINVAL);
110         if (!prop->value)
111                 return ERR_PTR(-ENODATA);
112         if (prop->length < min)
113                 return ERR_PTR(-EOVERFLOW);
114         if (max && prop->length > max)
115                 return ERR_PTR(-EOVERFLOW);
116
117         if (len)
118                 *len = prop->length;
119
120         return prop->value;
121 }
122
123 /**
124  * of_property_read_u32_index - Find and read a u32 from a multi-value property.
125  *
126  * @np:         device node from which the property value is to be read.
127  * @propname:   name of the property to be searched.
128  * @index:      index of the u32 in the list of values
129  * @out_value:  pointer to return value, modified only if no error.
130  *
131  * Search for a property in a device node and read nth 32-bit value from
132  * it. Returns 0 on success, -EINVAL if the property does not exist,
133  * -ENODATA if property does not have a value, and -EOVERFLOW if the
134  * property data isn't large enough.
135  *
136  * The out_value is modified only if a valid u32 value can be decoded.
137  */
138 int of_property_read_u32_index(const struct device_node *np,
139                                        const char *propname,
140                                        u32 index, u32 *out_value)
141 {
142         const u32 *val = of_find_property_value_of_size(np, propname,
143                                         ((index + 1) * sizeof(*out_value)),
144                                         0,
145                                         NULL);
146
147         if (IS_ERR(val))
148                 return PTR_ERR(val);
149
150         *out_value = be32_to_cpup(((__be32 *)val) + index);
151         return 0;
152 }
153 EXPORT_SYMBOL_GPL(of_property_read_u32_index);
154
155 /**
156  * of_property_read_u64_index - Find and read a u64 from a multi-value property.
157  *
158  * @np:         device node from which the property value is to be read.
159  * @propname:   name of the property to be searched.
160  * @index:      index of the u64 in the list of values
161  * @out_value:  pointer to return value, modified only if no error.
162  *
163  * Search for a property in a device node and read nth 64-bit value from
164  * it. Returns 0 on success, -EINVAL if the property does not exist,
165  * -ENODATA if property does not have a value, and -EOVERFLOW if the
166  * property data isn't large enough.
167  *
168  * The out_value is modified only if a valid u64 value can be decoded.
169  */
170 int of_property_read_u64_index(const struct device_node *np,
171                                        const char *propname,
172                                        u32 index, u64 *out_value)
173 {
174         const u64 *val = of_find_property_value_of_size(np, propname,
175                                         ((index + 1) * sizeof(*out_value)),
176                                         0, NULL);
177
178         if (IS_ERR(val))
179                 return PTR_ERR(val);
180
181         *out_value = be64_to_cpup(((__be64 *)val) + index);
182         return 0;
183 }
184 EXPORT_SYMBOL_GPL(of_property_read_u64_index);
185
186 /**
187  * of_property_read_variable_u8_array - Find and read an array of u8 from a
188  * property, with bounds on the minimum and maximum array size.
189  *
190  * @np:         device node from which the property value is to be read.
191  * @propname:   name of the property to be searched.
192  * @out_values: pointer to found values.
193  * @sz_min:     minimum number of array elements to read
194  * @sz_max:     maximum number of array elements to read, if zero there is no
195  *              upper limit on the number of elements in the dts entry but only
196  *              sz_min will be read.
197  *
198  * Search for a property in a device node and read 8-bit value(s) from
199  * it. Returns number of elements read on success, -EINVAL if the property
200  * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
201  * if the property data is smaller than sz_min or longer than sz_max.
202  *
203  * dts entry of array should be like:
204  *      property = /bits/ 8 <0x50 0x60 0x70>;
205  *
206  * The out_values is modified only if a valid u8 value can be decoded.
207  */
208 int of_property_read_variable_u8_array(const struct device_node *np,
209                                         const char *propname, u8 *out_values,
210                                         size_t sz_min, size_t sz_max)
211 {
212         size_t sz, count;
213         const u8 *val = of_find_property_value_of_size(np, propname,
214                                                 (sz_min * sizeof(*out_values)),
215                                                 (sz_max * sizeof(*out_values)),
216                                                 &sz);
217
218         if (IS_ERR(val))
219                 return PTR_ERR(val);
220
221         if (!sz_max)
222                 sz = sz_min;
223         else
224                 sz /= sizeof(*out_values);
225
226         count = sz;
227         while (count--)
228                 *out_values++ = *val++;
229
230         return sz;
231 }
232 EXPORT_SYMBOL_GPL(of_property_read_variable_u8_array);
233
234 /**
235  * of_property_read_variable_u16_array - Find and read an array of u16 from a
236  * property, with bounds on the minimum and maximum array size.
237  *
238  * @np:         device node from which the property value is to be read.
239  * @propname:   name of the property to be searched.
240  * @out_values: pointer to found values.
241  * @sz_min:     minimum number of array elements to read
242  * @sz_max:     maximum number of array elements to read, if zero there is no
243  *              upper limit on the number of elements in the dts entry but only
244  *              sz_min will be read.
245  *
246  * Search for a property in a device node and read 16-bit value(s) from
247  * it. Returns number of elements read on success, -EINVAL if the property
248  * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
249  * if the property data is smaller than sz_min or longer than sz_max.
250  *
251  * dts entry of array should be like:
252  *      property = /bits/ 16 <0x5000 0x6000 0x7000>;
253  *
254  * The out_values is modified only if a valid u16 value can be decoded.
255  */
256 int of_property_read_variable_u16_array(const struct device_node *np,
257                                         const char *propname, u16 *out_values,
258                                         size_t sz_min, size_t sz_max)
259 {
260         size_t sz, count;
261         const __be16 *val = of_find_property_value_of_size(np, propname,
262                                                 (sz_min * sizeof(*out_values)),
263                                                 (sz_max * sizeof(*out_values)),
264                                                 &sz);
265
266         if (IS_ERR(val))
267                 return PTR_ERR(val);
268
269         if (!sz_max)
270                 sz = sz_min;
271         else
272                 sz /= sizeof(*out_values);
273
274         count = sz;
275         while (count--)
276                 *out_values++ = be16_to_cpup(val++);
277
278         return sz;
279 }
280 EXPORT_SYMBOL_GPL(of_property_read_variable_u16_array);
281
282 /**
283  * of_property_read_variable_u32_array - Find and read an array of 32 bit
284  * integers from a property, with bounds on the minimum and maximum array size.
285  *
286  * @np:         device node from which the property value is to be read.
287  * @propname:   name of the property to be searched.
288  * @out_values: pointer to return found values.
289  * @sz_min:     minimum number of array elements to read
290  * @sz_max:     maximum number of array elements to read, if zero there is no
291  *              upper limit on the number of elements in the dts entry but only
292  *              sz_min will be read.
293  *
294  * Search for a property in a device node and read 32-bit value(s) from
295  * it. Returns number of elements read on success, -EINVAL if the property
296  * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
297  * if the property data is smaller than sz_min or longer than sz_max.
298  *
299  * The out_values is modified only if a valid u32 value can be decoded.
300  */
301 int of_property_read_variable_u32_array(const struct device_node *np,
302                                const char *propname, u32 *out_values,
303                                size_t sz_min, size_t sz_max)
304 {
305         size_t sz, count;
306         const __be32 *val = of_find_property_value_of_size(np, propname,
307                                                 (sz_min * sizeof(*out_values)),
308                                                 (sz_max * sizeof(*out_values)),
309                                                 &sz);
310
311         if (IS_ERR(val))
312                 return PTR_ERR(val);
313
314         if (!sz_max)
315                 sz = sz_min;
316         else
317                 sz /= sizeof(*out_values);
318
319         count = sz;
320         while (count--)
321                 *out_values++ = be32_to_cpup(val++);
322
323         return sz;
324 }
325 EXPORT_SYMBOL_GPL(of_property_read_variable_u32_array);
326
327 /**
328  * of_property_read_u64 - Find and read a 64 bit integer from a property
329  * @np:         device node from which the property value is to be read.
330  * @propname:   name of the property to be searched.
331  * @out_value:  pointer to return value, modified only if return value is 0.
332  *
333  * Search for a property in a device node and read a 64-bit value from
334  * it. Returns 0 on success, -EINVAL if the property does not exist,
335  * -ENODATA if property does not have a value, and -EOVERFLOW if the
336  * property data isn't large enough.
337  *
338  * The out_value is modified only if a valid u64 value can be decoded.
339  */
340 int of_property_read_u64(const struct device_node *np, const char *propname,
341                          u64 *out_value)
342 {
343         const __be32 *val = of_find_property_value_of_size(np, propname,
344                                                 sizeof(*out_value),
345                                                 0,
346                                                 NULL);
347
348         if (IS_ERR(val))
349                 return PTR_ERR(val);
350
351         *out_value = of_read_number(val, 2);
352         return 0;
353 }
354 EXPORT_SYMBOL_GPL(of_property_read_u64);
355
356 /**
357  * of_property_read_variable_u64_array - Find and read an array of 64 bit
358  * integers from a property, with bounds on the minimum and maximum array size.
359  *
360  * @np:         device node from which the property value is to be read.
361  * @propname:   name of the property to be searched.
362  * @out_values: pointer to found values.
363  * @sz_min:     minimum number of array elements to read
364  * @sz_max:     maximum number of array elements to read, if zero there is no
365  *              upper limit on the number of elements in the dts entry but only
366  *              sz_min will be read.
367  *
368  * Search for a property in a device node and read 64-bit value(s) from
369  * it. Returns number of elements read on success, -EINVAL if the property
370  * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
371  * if the property data is smaller than sz_min or longer than sz_max.
372  *
373  * The out_values is modified only if a valid u64 value can be decoded.
374  */
375 int of_property_read_variable_u64_array(const struct device_node *np,
376                                const char *propname, u64 *out_values,
377                                size_t sz_min, size_t sz_max)
378 {
379         size_t sz, count;
380         const __be32 *val = of_find_property_value_of_size(np, propname,
381                                                 (sz_min * sizeof(*out_values)),
382                                                 (sz_max * sizeof(*out_values)),
383                                                 &sz);
384
385         if (IS_ERR(val))
386                 return PTR_ERR(val);
387
388         if (!sz_max)
389                 sz = sz_min;
390         else
391                 sz /= sizeof(*out_values);
392
393         count = sz;
394         while (count--) {
395                 *out_values++ = of_read_number(val, 2);
396                 val += 2;
397         }
398
399         return sz;
400 }
401 EXPORT_SYMBOL_GPL(of_property_read_variable_u64_array);
402
403 /**
404  * of_property_read_string - Find and read a string from a property
405  * @np:         device node from which the property value is to be read.
406  * @propname:   name of the property to be searched.
407  * @out_string: pointer to null terminated return string, modified only if
408  *              return value is 0.
409  *
410  * Search for a property in a device tree node and retrieve a null
411  * terminated string value (pointer to data, not a copy). Returns 0 on
412  * success, -EINVAL if the property does not exist, -ENODATA if property
413  * does not have a value, and -EILSEQ if the string is not null-terminated
414  * within the length of the property data.
415  *
416  * The out_string pointer is modified only if a valid string can be decoded.
417  */
418 int of_property_read_string(const struct device_node *np, const char *propname,
419                                 const char **out_string)
420 {
421         const struct property *prop = of_find_property(np, propname, NULL);
422         if (!prop)
423                 return -EINVAL;
424         if (!prop->value)
425                 return -ENODATA;
426         if (strnlen(prop->value, prop->length) >= prop->length)
427                 return -EILSEQ;
428         *out_string = prop->value;
429         return 0;
430 }
431 EXPORT_SYMBOL_GPL(of_property_read_string);
432
433 /**
434  * of_property_match_string() - Find string in a list and return index
435  * @np: pointer to node containing string list property
436  * @propname: string list property name
437  * @string: pointer to string to search for in string list
438  *
439  * This function searches a string list property and returns the index
440  * of a specific string value.
441  */
442 int of_property_match_string(const struct device_node *np, const char *propname,
443                              const char *string)
444 {
445         const struct property *prop = of_find_property(np, propname, NULL);
446         size_t l;
447         int i;
448         const char *p, *end;
449
450         if (!prop)
451                 return -EINVAL;
452         if (!prop->value)
453                 return -ENODATA;
454
455         p = prop->value;
456         end = p + prop->length;
457
458         for (i = 0; p < end; i++, p += l) {
459                 l = strnlen(p, end - p) + 1;
460                 if (p + l > end)
461                         return -EILSEQ;
462                 pr_debug("comparing %s with %s\n", string, p);
463                 if (strcmp(string, p) == 0)
464                         return i; /* Found it; return index */
465         }
466         return -ENODATA;
467 }
468 EXPORT_SYMBOL_GPL(of_property_match_string);
469
470 /**
471  * of_property_read_string_helper() - Utility helper for parsing string properties
472  * @np:         device node from which the property value is to be read.
473  * @propname:   name of the property to be searched.
474  * @out_strs:   output array of string pointers.
475  * @sz:         number of array elements to read.
476  * @skip:       Number of strings to skip over at beginning of list.
477  *
478  * Don't call this function directly. It is a utility helper for the
479  * of_property_read_string*() family of functions.
480  */
481 int of_property_read_string_helper(const struct device_node *np,
482                                    const char *propname, const char **out_strs,
483                                    size_t sz, int skip)
484 {
485         const struct property *prop = of_find_property(np, propname, NULL);
486         int l = 0, i = 0;
487         const char *p, *end;
488
489         if (!prop)
490                 return -EINVAL;
491         if (!prop->value)
492                 return -ENODATA;
493         p = prop->value;
494         end = p + prop->length;
495
496         for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
497                 l = strnlen(p, end - p) + 1;
498                 if (p + l > end)
499                         return -EILSEQ;
500                 if (out_strs && i >= skip)
501                         *out_strs++ = p;
502         }
503         i -= skip;
504         return i <= 0 ? -ENODATA : i;
505 }
506 EXPORT_SYMBOL_GPL(of_property_read_string_helper);
507
508 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
509                                u32 *pu)
510 {
511         const void *curv = cur;
512
513         if (!prop)
514                 return NULL;
515
516         if (!cur) {
517                 curv = prop->value;
518                 goto out_val;
519         }
520
521         curv += sizeof(*cur);
522         if (curv >= prop->value + prop->length)
523                 return NULL;
524
525 out_val:
526         *pu = be32_to_cpup(curv);
527         return curv;
528 }
529 EXPORT_SYMBOL_GPL(of_prop_next_u32);
530
531 const char *of_prop_next_string(struct property *prop, const char *cur)
532 {
533         const void *curv = cur;
534
535         if (!prop)
536                 return NULL;
537
538         if (!cur)
539                 return prop->value;
540
541         curv += strlen(cur) + 1;
542         if (curv >= prop->value + prop->length)
543                 return NULL;
544
545         return curv;
546 }
547 EXPORT_SYMBOL_GPL(of_prop_next_string);
548
549 /**
550  * of_graph_parse_endpoint() - parse common endpoint node properties
551  * @node: pointer to endpoint device_node
552  * @endpoint: pointer to the OF endpoint data structure
553  *
554  * The caller should hold a reference to @node.
555  */
556 int of_graph_parse_endpoint(const struct device_node *node,
557                             struct of_endpoint *endpoint)
558 {
559         struct device_node *port_node = of_get_parent(node);
560
561         WARN_ONCE(!port_node, "%s(): endpoint %pOF has no parent node\n",
562                   __func__, node);
563
564         memset(endpoint, 0, sizeof(*endpoint));
565
566         endpoint->local_node = node;
567         /*
568          * It doesn't matter whether the two calls below succeed.
569          * If they don't then the default value 0 is used.
570          */
571         of_property_read_u32(port_node, "reg", &endpoint->port);
572         of_property_read_u32(node, "reg", &endpoint->id);
573
574         of_node_put(port_node);
575
576         return 0;
577 }
578 EXPORT_SYMBOL(of_graph_parse_endpoint);
579
580 /**
581  * of_graph_get_port_by_id() - get the port matching a given id
582  * @parent: pointer to the parent device node
583  * @id: id of the port
584  *
585  * Return: A 'port' node pointer with refcount incremented. The caller
586  * has to use of_node_put() on it when done.
587  */
588 struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id)
589 {
590         struct device_node *node, *port;
591
592         node = of_get_child_by_name(parent, "ports");
593         if (node)
594                 parent = node;
595
596         for_each_child_of_node(parent, port) {
597                 u32 port_id = 0;
598
599                 if (!of_node_name_eq(port, "port"))
600                         continue;
601                 of_property_read_u32(port, "reg", &port_id);
602                 if (id == port_id)
603                         break;
604         }
605
606         of_node_put(node);
607
608         return port;
609 }
610 EXPORT_SYMBOL(of_graph_get_port_by_id);
611
612 /**
613  * of_graph_get_next_endpoint() - get next endpoint node
614  * @parent: pointer to the parent device node
615  * @prev: previous endpoint node, or NULL to get first
616  *
617  * Return: An 'endpoint' node pointer with refcount incremented. Refcount
618  * of the passed @prev node is decremented.
619  */
620 struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
621                                         struct device_node *prev)
622 {
623         struct device_node *endpoint;
624         struct device_node *port;
625
626         if (!parent)
627                 return NULL;
628
629         /*
630          * Start by locating the port node. If no previous endpoint is specified
631          * search for the first port node, otherwise get the previous endpoint
632          * parent port node.
633          */
634         if (!prev) {
635                 struct device_node *node;
636
637                 node = of_get_child_by_name(parent, "ports");
638                 if (node)
639                         parent = node;
640
641                 port = of_get_child_by_name(parent, "port");
642                 of_node_put(node);
643
644                 if (!port) {
645                         pr_err("graph: no port node found in %pOF\n", parent);
646                         return NULL;
647                 }
648         } else {
649                 port = of_get_parent(prev);
650                 if (WARN_ONCE(!port, "%s(): endpoint %pOF has no parent node\n",
651                               __func__, prev))
652                         return NULL;
653         }
654
655         while (1) {
656                 /*
657                  * Now that we have a port node, get the next endpoint by
658                  * getting the next child. If the previous endpoint is NULL this
659                  * will return the first child.
660                  */
661                 endpoint = of_get_next_child(port, prev);
662                 if (endpoint) {
663                         of_node_put(port);
664                         return endpoint;
665                 }
666
667                 /* No more endpoints under this port, try the next one. */
668                 prev = NULL;
669
670                 do {
671                         port = of_get_next_child(parent, port);
672                         if (!port)
673                                 return NULL;
674                 } while (!of_node_name_eq(port, "port"));
675         }
676 }
677 EXPORT_SYMBOL(of_graph_get_next_endpoint);
678
679 /**
680  * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers
681  * @parent: pointer to the parent device node
682  * @port_reg: identifier (value of reg property) of the parent port node
683  * @reg: identifier (value of reg property) of the endpoint node
684  *
685  * Return: An 'endpoint' node pointer which is identified by reg and at the same
686  * is the child of a port node identified by port_reg. reg and port_reg are
687  * ignored when they are -1. Use of_node_put() on the pointer when done.
688  */
689 struct device_node *of_graph_get_endpoint_by_regs(
690         const struct device_node *parent, int port_reg, int reg)
691 {
692         struct of_endpoint endpoint;
693         struct device_node *node = NULL;
694
695         for_each_endpoint_of_node(parent, node) {
696                 of_graph_parse_endpoint(node, &endpoint);
697                 if (((port_reg == -1) || (endpoint.port == port_reg)) &&
698                         ((reg == -1) || (endpoint.id == reg)))
699                         return node;
700         }
701
702         return NULL;
703 }
704 EXPORT_SYMBOL(of_graph_get_endpoint_by_regs);
705
706 /**
707  * of_graph_get_remote_endpoint() - get remote endpoint node
708  * @node: pointer to a local endpoint device_node
709  *
710  * Return: Remote endpoint node associated with remote endpoint node linked
711  *         to @node. Use of_node_put() on it when done.
712  */
713 struct device_node *of_graph_get_remote_endpoint(const struct device_node *node)
714 {
715         /* Get remote endpoint node. */
716         return of_parse_phandle(node, "remote-endpoint", 0);
717 }
718 EXPORT_SYMBOL(of_graph_get_remote_endpoint);
719
720 /**
721  * of_graph_get_port_parent() - get port's parent node
722  * @node: pointer to a local endpoint device_node
723  *
724  * Return: device node associated with endpoint node linked
725  *         to @node. Use of_node_put() on it when done.
726  */
727 struct device_node *of_graph_get_port_parent(struct device_node *node)
728 {
729         unsigned int depth;
730
731         if (!node)
732                 return NULL;
733
734         /*
735          * Preserve usecount for passed in node as of_get_next_parent()
736          * will do of_node_put() on it.
737          */
738         of_node_get(node);
739
740         /* Walk 3 levels up only if there is 'ports' node. */
741         for (depth = 3; depth && node; depth--) {
742                 node = of_get_next_parent(node);
743                 if (depth == 2 && !of_node_name_eq(node, "ports"))
744                         break;
745         }
746         return node;
747 }
748 EXPORT_SYMBOL(of_graph_get_port_parent);
749
750 /**
751  * of_graph_get_remote_port_parent() - get remote port's parent node
752  * @node: pointer to a local endpoint device_node
753  *
754  * Return: Remote device node associated with remote endpoint node linked
755  *         to @node. Use of_node_put() on it when done.
756  */
757 struct device_node *of_graph_get_remote_port_parent(
758                                const struct device_node *node)
759 {
760         struct device_node *np, *pp;
761
762         /* Get remote endpoint node. */
763         np = of_graph_get_remote_endpoint(node);
764
765         pp = of_graph_get_port_parent(np);
766
767         of_node_put(np);
768
769         return pp;
770 }
771 EXPORT_SYMBOL(of_graph_get_remote_port_parent);
772
773 /**
774  * of_graph_get_remote_port() - get remote port node
775  * @node: pointer to a local endpoint device_node
776  *
777  * Return: Remote port node associated with remote endpoint node linked
778  *         to @node. Use of_node_put() on it when done.
779  */
780 struct device_node *of_graph_get_remote_port(const struct device_node *node)
781 {
782         struct device_node *np;
783
784         /* Get remote endpoint node. */
785         np = of_graph_get_remote_endpoint(node);
786         if (!np)
787                 return NULL;
788         return of_get_next_parent(np);
789 }
790 EXPORT_SYMBOL(of_graph_get_remote_port);
791
792 int of_graph_get_endpoint_count(const struct device_node *np)
793 {
794         struct device_node *endpoint;
795         int num = 0;
796
797         for_each_endpoint_of_node(np, endpoint)
798                 num++;
799
800         return num;
801 }
802 EXPORT_SYMBOL(of_graph_get_endpoint_count);
803
804 /**
805  * of_graph_get_remote_node() - get remote parent device_node for given port/endpoint
806  * @node: pointer to parent device_node containing graph port/endpoint
807  * @port: identifier (value of reg property) of the parent port node
808  * @endpoint: identifier (value of reg property) of the endpoint node
809  *
810  * Return: Remote device node associated with remote endpoint node linked
811  *         to @node. Use of_node_put() on it when done.
812  */
813 struct device_node *of_graph_get_remote_node(const struct device_node *node,
814                                              u32 port, u32 endpoint)
815 {
816         struct device_node *endpoint_node, *remote;
817
818         endpoint_node = of_graph_get_endpoint_by_regs(node, port, endpoint);
819         if (!endpoint_node) {
820                 pr_debug("no valid endpoint (%d, %d) for node %pOF\n",
821                          port, endpoint, node);
822                 return NULL;
823         }
824
825         remote = of_graph_get_remote_port_parent(endpoint_node);
826         of_node_put(endpoint_node);
827         if (!remote) {
828                 pr_debug("no valid remote node\n");
829                 return NULL;
830         }
831
832         if (!of_device_is_available(remote)) {
833                 pr_debug("not available for remote node\n");
834                 of_node_put(remote);
835                 return NULL;
836         }
837
838         return remote;
839 }
840 EXPORT_SYMBOL(of_graph_get_remote_node);
841
842 static struct fwnode_handle *of_fwnode_get(struct fwnode_handle *fwnode)
843 {
844         return of_fwnode_handle(of_node_get(to_of_node(fwnode)));
845 }
846
847 static void of_fwnode_put(struct fwnode_handle *fwnode)
848 {
849         of_node_put(to_of_node(fwnode));
850 }
851
852 static bool of_fwnode_device_is_available(const struct fwnode_handle *fwnode)
853 {
854         return of_device_is_available(to_of_node(fwnode));
855 }
856
857 static bool of_fwnode_property_present(const struct fwnode_handle *fwnode,
858                                        const char *propname)
859 {
860         return of_property_read_bool(to_of_node(fwnode), propname);
861 }
862
863 static int of_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
864                                              const char *propname,
865                                              unsigned int elem_size, void *val,
866                                              size_t nval)
867 {
868         const struct device_node *node = to_of_node(fwnode);
869
870         if (!val)
871                 return of_property_count_elems_of_size(node, propname,
872                                                        elem_size);
873
874         switch (elem_size) {
875         case sizeof(u8):
876                 return of_property_read_u8_array(node, propname, val, nval);
877         case sizeof(u16):
878                 return of_property_read_u16_array(node, propname, val, nval);
879         case sizeof(u32):
880                 return of_property_read_u32_array(node, propname, val, nval);
881         case sizeof(u64):
882                 return of_property_read_u64_array(node, propname, val, nval);
883         }
884
885         return -ENXIO;
886 }
887
888 static int
889 of_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
890                                      const char *propname, const char **val,
891                                      size_t nval)
892 {
893         const struct device_node *node = to_of_node(fwnode);
894
895         return val ?
896                 of_property_read_string_array(node, propname, val, nval) :
897                 of_property_count_strings(node, propname);
898 }
899
900 static const char *of_fwnode_get_name(const struct fwnode_handle *fwnode)
901 {
902         return kbasename(to_of_node(fwnode)->full_name);
903 }
904
905 static const char *of_fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
906 {
907         /* Root needs no prefix here (its name is "/"). */
908         if (!to_of_node(fwnode)->parent)
909                 return "";
910
911         return "/";
912 }
913
914 static struct fwnode_handle *
915 of_fwnode_get_parent(const struct fwnode_handle *fwnode)
916 {
917         return of_fwnode_handle(of_get_parent(to_of_node(fwnode)));
918 }
919
920 static struct fwnode_handle *
921 of_fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
922                               struct fwnode_handle *child)
923 {
924         return of_fwnode_handle(of_get_next_available_child(to_of_node(fwnode),
925                                                             to_of_node(child)));
926 }
927
928 static struct fwnode_handle *
929 of_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
930                                const char *childname)
931 {
932         const struct device_node *node = to_of_node(fwnode);
933         struct device_node *child;
934
935         for_each_available_child_of_node(node, child)
936                 if (of_node_name_eq(child, childname))
937                         return of_fwnode_handle(child);
938
939         return NULL;
940 }
941
942 static int
943 of_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
944                              const char *prop, const char *nargs_prop,
945                              unsigned int nargs, unsigned int index,
946                              struct fwnode_reference_args *args)
947 {
948         struct of_phandle_args of_args;
949         unsigned int i;
950         int ret;
951
952         if (nargs_prop)
953                 ret = of_parse_phandle_with_args(to_of_node(fwnode), prop,
954                                                  nargs_prop, index, &of_args);
955         else
956                 ret = of_parse_phandle_with_fixed_args(to_of_node(fwnode), prop,
957                                                        nargs, index, &of_args);
958         if (ret < 0)
959                 return ret;
960         if (!args)
961                 return 0;
962
963         args->nargs = of_args.args_count;
964         args->fwnode = of_fwnode_handle(of_args.np);
965
966         for (i = 0; i < NR_FWNODE_REFERENCE_ARGS; i++)
967                 args->args[i] = i < of_args.args_count ? of_args.args[i] : 0;
968
969         return 0;
970 }
971
972 static struct fwnode_handle *
973 of_fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
974                                   struct fwnode_handle *prev)
975 {
976         return of_fwnode_handle(of_graph_get_next_endpoint(to_of_node(fwnode),
977                                                            to_of_node(prev)));
978 }
979
980 static struct fwnode_handle *
981 of_fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
982 {
983         return of_fwnode_handle(
984                 of_graph_get_remote_endpoint(to_of_node(fwnode)));
985 }
986
987 static struct fwnode_handle *
988 of_fwnode_graph_get_port_parent(struct fwnode_handle *fwnode)
989 {
990         struct device_node *np;
991
992         /* Get the parent of the port */
993         np = of_get_parent(to_of_node(fwnode));
994         if (!np)
995                 return NULL;
996
997         /* Is this the "ports" node? If not, it's the port parent. */
998         if (!of_node_name_eq(np, "ports"))
999                 return of_fwnode_handle(np);
1000
1001         return of_fwnode_handle(of_get_next_parent(np));
1002 }
1003
1004 static int of_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
1005                                           struct fwnode_endpoint *endpoint)
1006 {
1007         const struct device_node *node = to_of_node(fwnode);
1008         struct device_node *port_node = of_get_parent(node);
1009
1010         endpoint->local_fwnode = fwnode;
1011
1012         of_property_read_u32(port_node, "reg", &endpoint->port);
1013         of_property_read_u32(node, "reg", &endpoint->id);
1014
1015         of_node_put(port_node);
1016
1017         return 0;
1018 }
1019
1020 static const void *
1021 of_fwnode_device_get_match_data(const struct fwnode_handle *fwnode,
1022                                 const struct device *dev)
1023 {
1024         return of_device_get_match_data(dev);
1025 }
1026
1027 static bool of_is_ancestor_of(struct device_node *test_ancestor,
1028                               struct device_node *child)
1029 {
1030         of_node_get(child);
1031         while (child) {
1032                 if (child == test_ancestor) {
1033                         of_node_put(child);
1034                         return true;
1035                 }
1036                 child = of_get_next_parent(child);
1037         }
1038         return false;
1039 }
1040
1041 /**
1042  * of_link_to_phandle - Add fwnode link to supplier from supplier phandle
1043  * @con_np: consumer device tree node
1044  * @sup_np: supplier device tree node
1045  *
1046  * Given a phandle to a supplier device tree node (@sup_np), this function
1047  * finds the device that owns the supplier device tree node and creates a
1048  * device link from @dev consumer device to the supplier device. This function
1049  * doesn't create device links for invalid scenarios such as trying to create a
1050  * link with a parent device as the consumer of its child device. In such
1051  * cases, it returns an error.
1052  *
1053  * Returns:
1054  * - 0 if fwnode link successfully created to supplier
1055  * - -EINVAL if the supplier link is invalid and should not be created
1056  * - -ENODEV if struct device will never be create for supplier
1057  */
1058 static int of_link_to_phandle(struct device_node *con_np,
1059                               struct device_node *sup_np)
1060 {
1061         struct device *sup_dev;
1062         struct device_node *tmp_np = sup_np;
1063
1064         of_node_get(sup_np);
1065         /*
1066          * Find the device node that contains the supplier phandle.  It may be
1067          * @sup_np or it may be an ancestor of @sup_np.
1068          */
1069         while (sup_np) {
1070
1071                 /* Don't allow linking to a disabled supplier */
1072                 if (!of_device_is_available(sup_np)) {
1073                         of_node_put(sup_np);
1074                         sup_np = NULL;
1075                 }
1076
1077                 if (of_find_property(sup_np, "compatible", NULL))
1078                         break;
1079
1080                 sup_np = of_get_next_parent(sup_np);
1081         }
1082
1083         if (!sup_np) {
1084                 pr_debug("Not linking %pOFP to %pOFP - No device\n",
1085                          con_np, tmp_np);
1086                 return -ENODEV;
1087         }
1088
1089         /*
1090          * Don't allow linking a device node as a consumer of one of its
1091          * descendant nodes. By definition, a child node can't be a functional
1092          * dependency for the parent node.
1093          */
1094         if (of_is_ancestor_of(con_np, sup_np)) {
1095                 pr_debug("Not linking %pOFP to %pOFP - is descendant\n",
1096                          con_np, sup_np);
1097                 of_node_put(sup_np);
1098                 return -EINVAL;
1099         }
1100
1101         /*
1102          * Don't create links to "early devices" that won't have struct devices
1103          * created for them.
1104          */
1105         sup_dev = get_dev_from_fwnode(&sup_np->fwnode);
1106         if (!sup_dev &&
1107             (of_node_check_flag(sup_np, OF_POPULATED) ||
1108              sup_np->fwnode.flags & FWNODE_FLAG_NOT_DEVICE)) {
1109                 pr_debug("Not linking %pOFP to %pOFP - No struct device\n",
1110                          con_np, sup_np);
1111                 of_node_put(sup_np);
1112                 return -ENODEV;
1113         }
1114         put_device(sup_dev);
1115
1116         fwnode_link_add(of_fwnode_handle(con_np), of_fwnode_handle(sup_np));
1117         of_node_put(sup_np);
1118
1119         return 0;
1120 }
1121
1122 /**
1123  * parse_prop_cells - Property parsing function for suppliers
1124  *
1125  * @np:         Pointer to device tree node containing a list
1126  * @prop_name:  Name of property to be parsed. Expected to hold phandle values
1127  * @index:      For properties holding a list of phandles, this is the index
1128  *              into the list.
1129  * @list_name:  Property name that is known to contain list of phandle(s) to
1130  *              supplier(s)
1131  * @cells_name: property name that specifies phandles' arguments count
1132  *
1133  * This is a helper function to parse properties that have a known fixed name
1134  * and are a list of phandles and phandle arguments.
1135  *
1136  * Returns:
1137  * - phandle node pointer with refcount incremented. Caller must of_node_put()
1138  *   on it when done.
1139  * - NULL if no phandle found at index
1140  */
1141 static struct device_node *parse_prop_cells(struct device_node *np,
1142                                             const char *prop_name, int index,
1143                                             const char *list_name,
1144                                             const char *cells_name)
1145 {
1146         struct of_phandle_args sup_args;
1147
1148         if (strcmp(prop_name, list_name))
1149                 return NULL;
1150
1151         if (of_parse_phandle_with_args(np, list_name, cells_name, index,
1152                                        &sup_args))
1153                 return NULL;
1154
1155         return sup_args.np;
1156 }
1157
1158 #define DEFINE_SIMPLE_PROP(fname, name, cells)                            \
1159 static struct device_node *parse_##fname(struct device_node *np,          \
1160                                         const char *prop_name, int index) \
1161 {                                                                         \
1162         return parse_prop_cells(np, prop_name, index, name, cells);       \
1163 }
1164
1165 static int strcmp_suffix(const char *str, const char *suffix)
1166 {
1167         unsigned int len, suffix_len;
1168
1169         len = strlen(str);
1170         suffix_len = strlen(suffix);
1171         if (len <= suffix_len)
1172                 return -1;
1173         return strcmp(str + len - suffix_len, suffix);
1174 }
1175
1176 /**
1177  * parse_suffix_prop_cells - Suffix property parsing function for suppliers
1178  *
1179  * @np:         Pointer to device tree node containing a list
1180  * @prop_name:  Name of property to be parsed. Expected to hold phandle values
1181  * @index:      For properties holding a list of phandles, this is the index
1182  *              into the list.
1183  * @suffix:     Property suffix that is known to contain list of phandle(s) to
1184  *              supplier(s)
1185  * @cells_name: property name that specifies phandles' arguments count
1186  *
1187  * This is a helper function to parse properties that have a known fixed suffix
1188  * and are a list of phandles and phandle arguments.
1189  *
1190  * Returns:
1191  * - phandle node pointer with refcount incremented. Caller must of_node_put()
1192  *   on it when done.
1193  * - NULL if no phandle found at index
1194  */
1195 static struct device_node *parse_suffix_prop_cells(struct device_node *np,
1196                                             const char *prop_name, int index,
1197                                             const char *suffix,
1198                                             const char *cells_name)
1199 {
1200         struct of_phandle_args sup_args;
1201
1202         if (strcmp_suffix(prop_name, suffix))
1203                 return NULL;
1204
1205         if (of_parse_phandle_with_args(np, prop_name, cells_name, index,
1206                                        &sup_args))
1207                 return NULL;
1208
1209         return sup_args.np;
1210 }
1211
1212 #define DEFINE_SUFFIX_PROP(fname, suffix, cells)                             \
1213 static struct device_node *parse_##fname(struct device_node *np,             \
1214                                         const char *prop_name, int index)    \
1215 {                                                                            \
1216         return parse_suffix_prop_cells(np, prop_name, index, suffix, cells); \
1217 }
1218
1219 /**
1220  * struct supplier_bindings - Property parsing functions for suppliers
1221  *
1222  * @parse_prop: function name
1223  *      parse_prop() finds the node corresponding to a supplier phandle
1224  * @parse_prop.np: Pointer to device node holding supplier phandle property
1225  * @parse_prop.prop_name: Name of property holding a phandle value
1226  * @parse_prop.index: For properties holding a list of phandles, this is the
1227  *                    index into the list
1228  *
1229  * Returns:
1230  * parse_prop() return values are
1231  * - phandle node pointer with refcount incremented. Caller must of_node_put()
1232  *   on it when done.
1233  * - NULL if no phandle found at index
1234  */
1235 struct supplier_bindings {
1236         struct device_node *(*parse_prop)(struct device_node *np,
1237                                           const char *prop_name, int index);
1238         bool optional;
1239 };
1240
1241 DEFINE_SIMPLE_PROP(clocks, "clocks", "#clock-cells")
1242 DEFINE_SIMPLE_PROP(interconnects, "interconnects", "#interconnect-cells")
1243 DEFINE_SIMPLE_PROP(iommus, "iommus", "#iommu-cells")
1244 DEFINE_SIMPLE_PROP(mboxes, "mboxes", "#mbox-cells")
1245 DEFINE_SIMPLE_PROP(io_channels, "io-channel", "#io-channel-cells")
1246 DEFINE_SIMPLE_PROP(interrupt_parent, "interrupt-parent", NULL)
1247 DEFINE_SIMPLE_PROP(dmas, "dmas", "#dma-cells")
1248 DEFINE_SIMPLE_PROP(power_domains, "power-domains", "#power-domain-cells")
1249 DEFINE_SIMPLE_PROP(hwlocks, "hwlocks", "#hwlock-cells")
1250 DEFINE_SIMPLE_PROP(extcon, "extcon", NULL)
1251 DEFINE_SIMPLE_PROP(nvmem_cells, "nvmem-cells", NULL)
1252 DEFINE_SIMPLE_PROP(phys, "phys", "#phy-cells")
1253 DEFINE_SIMPLE_PROP(wakeup_parent, "wakeup-parent", NULL)
1254 DEFINE_SIMPLE_PROP(pinctrl0, "pinctrl-0", NULL)
1255 DEFINE_SIMPLE_PROP(pinctrl1, "pinctrl-1", NULL)
1256 DEFINE_SIMPLE_PROP(pinctrl2, "pinctrl-2", NULL)
1257 DEFINE_SIMPLE_PROP(pinctrl3, "pinctrl-3", NULL)
1258 DEFINE_SIMPLE_PROP(pinctrl4, "pinctrl-4", NULL)
1259 DEFINE_SIMPLE_PROP(pinctrl5, "pinctrl-5", NULL)
1260 DEFINE_SIMPLE_PROP(pinctrl6, "pinctrl-6", NULL)
1261 DEFINE_SIMPLE_PROP(pinctrl7, "pinctrl-7", NULL)
1262 DEFINE_SIMPLE_PROP(pinctrl8, "pinctrl-8", NULL)
1263 DEFINE_SUFFIX_PROP(regulators, "-supply", NULL)
1264 DEFINE_SUFFIX_PROP(gpio, "-gpio", "#gpio-cells")
1265
1266 static struct device_node *parse_gpios(struct device_node *np,
1267                                        const char *prop_name, int index)
1268 {
1269         if (!strcmp_suffix(prop_name, ",nr-gpios"))
1270                 return NULL;
1271
1272         return parse_suffix_prop_cells(np, prop_name, index, "-gpios",
1273                                        "#gpio-cells");
1274 }
1275
1276 static struct device_node *parse_iommu_maps(struct device_node *np,
1277                                             const char *prop_name, int index)
1278 {
1279         if (strcmp(prop_name, "iommu-map"))
1280                 return NULL;
1281
1282         return of_parse_phandle(np, prop_name, (index * 4) + 1);
1283 }
1284
1285 static struct device_node *parse_gpio_compat(struct device_node *np,
1286                                              const char *prop_name, int index)
1287 {
1288         struct of_phandle_args sup_args;
1289
1290         if (strcmp(prop_name, "gpio") && strcmp(prop_name, "gpios"))
1291                 return NULL;
1292
1293         /*
1294          * Ignore node with gpio-hog property since its gpios are all provided
1295          * by its parent.
1296          */
1297         if (of_find_property(np, "gpio-hog", NULL))
1298                 return NULL;
1299
1300         if (of_parse_phandle_with_args(np, prop_name, "#gpio-cells", index,
1301                                        &sup_args))
1302                 return NULL;
1303
1304         return sup_args.np;
1305 }
1306
1307 static struct device_node *parse_interrupts(struct device_node *np,
1308                                             const char *prop_name, int index)
1309 {
1310         struct of_phandle_args sup_args;
1311
1312         if (!IS_ENABLED(CONFIG_OF_IRQ) || IS_ENABLED(CONFIG_PPC))
1313                 return NULL;
1314
1315         if (strcmp(prop_name, "interrupts") &&
1316             strcmp(prop_name, "interrupts-extended"))
1317                 return NULL;
1318
1319         return of_irq_parse_one(np, index, &sup_args) ? NULL : sup_args.np;
1320 }
1321
1322 static const struct supplier_bindings of_supplier_bindings[] = {
1323         { .parse_prop = parse_clocks, },
1324         { .parse_prop = parse_interconnects, },
1325         { .parse_prop = parse_iommus, .optional = true, },
1326         { .parse_prop = parse_iommu_maps, .optional = true, },
1327         { .parse_prop = parse_mboxes, },
1328         { .parse_prop = parse_io_channels, },
1329         { .parse_prop = parse_interrupt_parent, },
1330         { .parse_prop = parse_dmas, .optional = true, },
1331         { .parse_prop = parse_power_domains, },
1332         { .parse_prop = parse_hwlocks, },
1333         { .parse_prop = parse_extcon, },
1334         { .parse_prop = parse_nvmem_cells, },
1335         { .parse_prop = parse_phys, },
1336         { .parse_prop = parse_wakeup_parent, },
1337         { .parse_prop = parse_pinctrl0, },
1338         { .parse_prop = parse_pinctrl1, },
1339         { .parse_prop = parse_pinctrl2, },
1340         { .parse_prop = parse_pinctrl3, },
1341         { .parse_prop = parse_pinctrl4, },
1342         { .parse_prop = parse_pinctrl5, },
1343         { .parse_prop = parse_pinctrl6, },
1344         { .parse_prop = parse_pinctrl7, },
1345         { .parse_prop = parse_pinctrl8, },
1346         { .parse_prop = parse_gpio_compat, },
1347         { .parse_prop = parse_interrupts, },
1348         { .parse_prop = parse_regulators, },
1349         { .parse_prop = parse_gpio, },
1350         { .parse_prop = parse_gpios, },
1351         {}
1352 };
1353
1354 /**
1355  * of_link_property - Create device links to suppliers listed in a property
1356  * @dev: Consumer device
1357  * @con_np: The consumer device tree node which contains the property
1358  * @prop_name: Name of property to be parsed
1359  *
1360  * This function checks if the property @prop_name that is present in the
1361  * @con_np device tree node is one of the known common device tree bindings
1362  * that list phandles to suppliers. If @prop_name isn't one, this function
1363  * doesn't do anything.
1364  *
1365  * If @prop_name is one, this function attempts to create fwnode links from the
1366  * consumer device tree node @con_np to all the suppliers device tree nodes
1367  * listed in @prop_name.
1368  *
1369  * Any failed attempt to create a fwnode link will NOT result in an immediate
1370  * return.  of_link_property() must create links to all the available supplier
1371  * device tree nodes even when attempts to create a link to one or more
1372  * suppliers fail.
1373  */
1374 static int of_link_property(struct device_node *con_np, const char *prop_name)
1375 {
1376         struct device_node *phandle;
1377         const struct supplier_bindings *s = of_supplier_bindings;
1378         unsigned int i = 0;
1379         bool matched = false;
1380         int ret = 0;
1381
1382         /* Do not stop at first failed link, link all available suppliers. */
1383         while (!matched && s->parse_prop) {
1384                 if (s->optional && !fw_devlink_is_strict()) {
1385                         s++;
1386                         continue;
1387                 }
1388
1389                 while ((phandle = s->parse_prop(con_np, prop_name, i))) {
1390                         matched = true;
1391                         i++;
1392                         of_link_to_phandle(con_np, phandle);
1393                         of_node_put(phandle);
1394                 }
1395                 s++;
1396         }
1397         return ret;
1398 }
1399
1400 static int of_fwnode_add_links(struct fwnode_handle *fwnode)
1401 {
1402         struct property *p;
1403         struct device_node *con_np = to_of_node(fwnode);
1404
1405         if (!con_np)
1406                 return -EINVAL;
1407
1408         for_each_property_of_node(con_np, p)
1409                 of_link_property(con_np, p->name);
1410
1411         return 0;
1412 }
1413
1414 const struct fwnode_operations of_fwnode_ops = {
1415         .get = of_fwnode_get,
1416         .put = of_fwnode_put,
1417         .device_is_available = of_fwnode_device_is_available,
1418         .device_get_match_data = of_fwnode_device_get_match_data,
1419         .property_present = of_fwnode_property_present,
1420         .property_read_int_array = of_fwnode_property_read_int_array,
1421         .property_read_string_array = of_fwnode_property_read_string_array,
1422         .get_name = of_fwnode_get_name,
1423         .get_name_prefix = of_fwnode_get_name_prefix,
1424         .get_parent = of_fwnode_get_parent,
1425         .get_next_child_node = of_fwnode_get_next_child_node,
1426         .get_named_child_node = of_fwnode_get_named_child_node,
1427         .get_reference_args = of_fwnode_get_reference_args,
1428         .graph_get_next_endpoint = of_fwnode_graph_get_next_endpoint,
1429         .graph_get_remote_endpoint = of_fwnode_graph_get_remote_endpoint,
1430         .graph_get_port_parent = of_fwnode_graph_get_port_parent,
1431         .graph_parse_endpoint = of_fwnode_graph_parse_endpoint,
1432         .add_links = of_fwnode_add_links,
1433 };
1434 EXPORT_SYMBOL_GPL(of_fwnode_ops);