blob: 6287c6d60bb7f116d2d80d700082d1e06f9d1cc6 [file] [log] [blame]
Rob Herringaf6074f2017-12-27 12:55:14 -06001// SPDX-License-Identifier: GPL-2.0+
Sakari Ailus1df09bc2017-05-24 17:53:53 +03002/*
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.
Sakari Ailus1df09bc2017-05-24 17:53:53 +030020 */
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>
Saravana Kannan4104ca72021-01-21 14:57:12 -080027#include <linux/of_irq.h>
Sakari Ailus1df09bc2017-05-24 17:53:53 +030028#include <linux/string.h>
Saravana Kannana3e1d1a2019-09-04 14:11:22 -070029#include <linux/moduleparam.h>
Sakari Ailus1df09bc2017-05-24 17:53:53 +030030
31#include "of_private.h"
32
33/**
Dmitry Osipenko4ec0a442020-07-01 10:42:31 +030034 * 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 */
40bool 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}
54EXPORT_SYMBOL(of_graph_is_present);
55
56/**
Sakari Ailus1df09bc2017-05-24 17:53:53 +030057 * 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 */
68int 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) {
Rob Herring0d638a02017-06-01 15:50:55 -050079 pr_err("size of %s in node %pOF is not a multiple of %d\n",
80 propname, np, elem_size);
Sakari Ailus1df09bc2017-05-24 17:53:53 +030081 return -EINVAL;
82 }
83
84 return prop->length / elem_size;
85}
86EXPORT_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 */
103static 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 */
138int 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}
153EXPORT_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 */
170int 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}
184EXPORT_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.
Matti Vaittinen7f3fefe2019-11-13 08:43:38 +0200192 * @out_values: pointer to found values.
Sakari Ailus1df09bc2017-05-24 17:53:53 +0300193 * @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 */
208int 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}
232EXPORT_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.
Matti Vaittinen7f3fefe2019-11-13 08:43:38 +0200240 * @out_values: pointer to found values.
Sakari Ailus1df09bc2017-05-24 17:53:53 +0300241 * @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 */
256int 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}
280EXPORT_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.
Matti Vaittinen7f3fefe2019-11-13 08:43:38 +0200288 * @out_values: pointer to return found values.
Sakari Ailus1df09bc2017-05-24 17:53:53 +0300289 * @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 */
301int 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}
325EXPORT_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 */
340int 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}
354EXPORT_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.
Matti Vaittinen7f3fefe2019-11-13 08:43:38 +0200362 * @out_values: pointer to found values.
Sakari Ailus1df09bc2017-05-24 17:53:53 +0300363 * @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 */
375int 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}
401EXPORT_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 */
418int 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}
431EXPORT_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 */
442int 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}
468EXPORT_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 */
481int 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}
506EXPORT_SYMBOL_GPL(of_property_read_string_helper);
507
508const __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
525out_val:
526 *pu = be32_to_cpup(curv);
527 return curv;
528}
529EXPORT_SYMBOL_GPL(of_prop_next_u32);
530
531const 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}
547EXPORT_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 */
556int 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
Rob Herring0d638a02017-06-01 15:50:55 -0500561 WARN_ONCE(!port_node, "%s(): endpoint %pOF has no parent node\n",
562 __func__, node);
Sakari Ailus1df09bc2017-05-24 17:53:53 +0300563
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}
578EXPORT_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 */
588struct 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
Rob Herringb3e46d12018-08-27 08:37:06 -0500599 if (!of_node_name_eq(port, "port"))
Sakari Ailus1df09bc2017-05-24 17:53:53 +0300600 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}
610EXPORT_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 */
620struct 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) {
Rob Herring0d638a02017-06-01 15:50:55 -0500645 pr_err("graph: no port node found in %pOF\n", parent);
Sakari Ailus1df09bc2017-05-24 17:53:53 +0300646 return NULL;
647 }
648 } else {
649 port = of_get_parent(prev);
Rob Herring0d638a02017-06-01 15:50:55 -0500650 if (WARN_ONCE(!port, "%s(): endpoint %pOF has no parent node\n",
651 __func__, prev))
Sakari Ailus1df09bc2017-05-24 17:53:53 +0300652 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;
Rob Herringb3e46d12018-08-27 08:37:06 -0500674 } while (!of_node_name_eq(port, "port"));
Sakari Ailus1df09bc2017-05-24 17:53:53 +0300675 }
676}
677EXPORT_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
Maxime Riparddeb387d2019-03-15 10:22:47 +0100687 * ignored when they are -1. Use of_node_put() on the pointer when done.
Sakari Ailus1df09bc2017-05-24 17:53:53 +0300688 */
689struct 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}
704EXPORT_SYMBOL(of_graph_get_endpoint_by_regs);
705
706/**
Rob Herringb8ba92b2017-07-05 08:24:05 -0500707 * 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 */
713struct 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}
718EXPORT_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 */
727struct device_node *of_graph_get_port_parent(struct device_node *node)
728{
729 unsigned int depth;
730
Tony Lindgrenc0a480d2017-07-28 01:23:15 -0700731 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
Rob Herringb8ba92b2017-07-05 08:24:05 -0500740 /* Walk 3 levels up only if there is 'ports' node. */
741 for (depth = 3; depth && node; depth--) {
742 node = of_get_next_parent(node);
Rob Herringb3e46d12018-08-27 08:37:06 -0500743 if (depth == 2 && !of_node_name_eq(node, "ports"))
Rob Herringb8ba92b2017-07-05 08:24:05 -0500744 break;
745 }
746 return node;
747}
748EXPORT_SYMBOL(of_graph_get_port_parent);
749
750/**
Sakari Ailus1df09bc2017-05-24 17:53:53 +0300751 * 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 */
757struct device_node *of_graph_get_remote_port_parent(
758 const struct device_node *node)
759{
Tony Lindgrenc0a480d2017-07-28 01:23:15 -0700760 struct device_node *np, *pp;
Sakari Ailus1df09bc2017-05-24 17:53:53 +0300761
762 /* Get remote endpoint node. */
Rob Herringb8ba92b2017-07-05 08:24:05 -0500763 np = of_graph_get_remote_endpoint(node);
Sakari Ailus1df09bc2017-05-24 17:53:53 +0300764
Tony Lindgrenc0a480d2017-07-28 01:23:15 -0700765 pp = of_graph_get_port_parent(np);
766
767 of_node_put(np);
768
769 return pp;
Sakari Ailus1df09bc2017-05-24 17:53:53 +0300770}
771EXPORT_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 */
780struct device_node *of_graph_get_remote_port(const struct device_node *node)
781{
782 struct device_node *np;
783
784 /* Get remote endpoint node. */
Rob Herringb8ba92b2017-07-05 08:24:05 -0500785 np = of_graph_get_remote_endpoint(node);
Sakari Ailus1df09bc2017-05-24 17:53:53 +0300786 if (!np)
787 return NULL;
788 return of_get_next_parent(np);
789}
790EXPORT_SYMBOL(of_graph_get_remote_port);
791
Rob Herringb8ba92b2017-07-05 08:24:05 -0500792int 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}
802EXPORT_SYMBOL(of_graph_get_endpoint_count);
803
Sakari Ailus1df09bc2017-05-24 17:53:53 +0300804/**
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 */
813struct 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) {
Rob Herring0d638a02017-06-01 15:50:55 -0500820 pr_debug("no valid endpoint (%d, %d) for node %pOF\n",
821 port, endpoint, node);
Sakari Ailus1df09bc2017-05-24 17:53:53 +0300822 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");
Julia Lawall28b170e2019-01-13 10:44:50 +0100834 of_node_put(remote);
Sakari Ailus1df09bc2017-05-24 17:53:53 +0300835 return NULL;
836 }
837
838 return remote;
839}
840EXPORT_SYMBOL(of_graph_get_remote_node);
Sakari Ailus37081842017-06-06 12:37:37 +0300841
Sakari Ailuscf89a312017-09-19 12:39:11 +0300842static struct fwnode_handle *of_fwnode_get(struct fwnode_handle *fwnode)
Sakari Ailus37081842017-06-06 12:37:37 +0300843{
Sakari Ailuscf89a312017-09-19 12:39:11 +0300844 return of_fwnode_handle(of_node_get(to_of_node(fwnode)));
Sakari Ailus37081842017-06-06 12:37:37 +0300845}
846
847static void of_fwnode_put(struct fwnode_handle *fwnode)
848{
849 of_node_put(to_of_node(fwnode));
850}
851
Sakari Ailus37ba9832017-07-21 14:39:36 +0300852static bool of_fwnode_device_is_available(const struct fwnode_handle *fwnode)
Sakari Ailus2294b3a2017-06-06 12:37:39 +0300853{
854 return of_device_is_available(to_of_node(fwnode));
855}
856
Sakari Ailus37ba9832017-07-21 14:39:36 +0300857static bool of_fwnode_property_present(const struct fwnode_handle *fwnode,
Sakari Ailus37081842017-06-06 12:37:37 +0300858 const char *propname)
859{
860 return of_property_read_bool(to_of_node(fwnode), propname);
861}
862
Sakari Ailus37ba9832017-07-21 14:39:36 +0300863static int of_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
Sakari Ailus37081842017-06-06 12:37:37 +0300864 const char *propname,
865 unsigned int elem_size, void *val,
866 size_t nval)
867{
Sakari Ailus37ba9832017-07-21 14:39:36 +0300868 const struct device_node *node = to_of_node(fwnode);
Sakari Ailus37081842017-06-06 12:37:37 +0300869
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
Sakari Ailus37ba9832017-07-21 14:39:36 +0300888static int
889of_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
890 const char *propname, const char **val,
891 size_t nval)
Sakari Ailus37081842017-06-06 12:37:37 +0300892{
Sakari Ailus37ba9832017-07-21 14:39:36 +0300893 const struct device_node *node = to_of_node(fwnode);
Sakari Ailus37081842017-06-06 12:37:37 +0300894
895 return val ?
896 of_property_read_string_array(node, propname, val, nval) :
897 of_property_count_strings(node, propname);
898}
899
Sakari Ailusbc0500c2019-10-03 15:32:12 +0300900static const char *of_fwnode_get_name(const struct fwnode_handle *fwnode)
901{
902 return kbasename(to_of_node(fwnode)->full_name);
903}
904
Sakari Ailuse7e242b2019-10-03 15:32:13 +0300905static 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
Sakari Ailus37ba9832017-07-21 14:39:36 +0300914static struct fwnode_handle *
915of_fwnode_get_parent(const struct fwnode_handle *fwnode)
Sakari Ailus37081842017-06-06 12:37:37 +0300916{
917 return of_fwnode_handle(of_get_parent(to_of_node(fwnode)));
918}
919
920static struct fwnode_handle *
Sakari Ailus37ba9832017-07-21 14:39:36 +0300921of_fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
Sakari Ailus37081842017-06-06 12:37:37 +0300922 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
928static struct fwnode_handle *
Sakari Ailus37ba9832017-07-21 14:39:36 +0300929of_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
Sakari Ailus37081842017-06-06 12:37:37 +0300930 const char *childname)
931{
Sakari Ailus37ba9832017-07-21 14:39:36 +0300932 const struct device_node *node = to_of_node(fwnode);
Sakari Ailus37081842017-06-06 12:37:37 +0300933 struct device_node *child;
934
935 for_each_available_child_of_node(node, child)
Rob Herringb3e46d12018-08-27 08:37:06 -0500936 if (of_node_name_eq(child, childname))
Sakari Ailus37081842017-06-06 12:37:37 +0300937 return of_fwnode_handle(child);
938
939 return NULL;
940}
941
Sakari Ailus3e3119d2017-07-21 15:11:49 +0300942static int
943of_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
Sakari Ailus3b27d002017-06-06 12:37:38 +0300972static struct fwnode_handle *
Sakari Ailus37ba9832017-07-21 14:39:36 +0300973of_fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
Sakari Ailus3b27d002017-06-06 12:37:38 +0300974 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
980static struct fwnode_handle *
Sakari Ailus37ba9832017-07-21 14:39:36 +0300981of_fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
Sakari Ailus3b27d002017-06-06 12:37:38 +0300982{
Kuninori Morimoto358155e2017-08-10 04:38:16 +0000983 return of_fwnode_handle(
984 of_graph_get_remote_endpoint(to_of_node(fwnode)));
Sakari Ailus3b27d002017-06-06 12:37:38 +0300985}
986
987static struct fwnode_handle *
988of_fwnode_graph_get_port_parent(struct fwnode_handle *fwnode)
989{
990 struct device_node *np;
991
992 /* Get the parent of the port */
Niklas Söderlund3314c6b2017-08-22 02:19:12 +0200993 np = of_get_parent(to_of_node(fwnode));
Sakari Ailus3b27d002017-06-06 12:37:38 +0300994 if (!np)
995 return NULL;
996
997 /* Is this the "ports" node? If not, it's the port parent. */
Rob Herringb3e46d12018-08-27 08:37:06 -0500998 if (!of_node_name_eq(np, "ports"))
Sakari Ailus3b27d002017-06-06 12:37:38 +0300999 return of_fwnode_handle(np);
1000
1001 return of_fwnode_handle(of_get_next_parent(np));
1002}
1003
Sakari Ailus37ba9832017-07-21 14:39:36 +03001004static int of_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
Sakari Ailus3b27d002017-06-06 12:37:38 +03001005 struct fwnode_endpoint *endpoint)
1006{
Sakari Ailus37ba9832017-07-21 14:39:36 +03001007 const struct device_node *node = to_of_node(fwnode);
Sakari Ailus3b27d002017-06-06 12:37:38 +03001008 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
Andy Shevchenko67dcc262018-02-09 17:38:36 +02001020static const void *
Sinan Kaya1c2c82e2017-12-13 02:20:50 -05001021of_fwnode_device_get_match_data(const struct fwnode_handle *fwnode,
1022 const struct device *dev)
1023{
Andy Shevchenko67dcc262018-02-09 17:38:36 +02001024 return of_device_get_match_data(dev);
Sinan Kaya1c2c82e2017-12-13 02:20:50 -05001025}
1026
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001027static 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);
Saravana Kannan38835392019-11-20 00:02:29 -08001034 return true;
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001035 }
1036 child = of_get_next_parent(child);
1037 }
Saravana Kannan38835392019-11-20 00:02:29 -08001038 return false;
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001039}
1040
1041/**
Saravana Kannan8a06d1e2020-11-20 18:02:29 -08001042 * 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
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001045 *
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:
Saravana Kannan8a06d1e2020-11-20 18:02:29 -08001054 * - 0 if fwnode link successfully created to supplier
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001055 * - -EINVAL if the supplier link is invalid and should not be created
Saravana Kannan8a06d1e2020-11-20 18:02:29 -08001056 * - -ENODEV if struct device will never be create for supplier
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001057 */
Saravana Kannan8a06d1e2020-11-20 18:02:29 -08001058static int of_link_to_phandle(struct device_node *con_np,
1059 struct device_node *sup_np)
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001060{
Saravana Kannan8a06d1e2020-11-20 18:02:29 -08001061 struct device *sup_dev;
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001062 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 */
Nicolas Saenz Julienne74564272020-04-20 14:01:02 +02001069 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
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001080 sup_np = of_get_next_parent(sup_np);
Nicolas Saenz Julienne74564272020-04-20 14:01:02 +02001081 }
1082
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001083 if (!sup_np) {
Saravana Kannan8a06d1e2020-11-20 18:02:29 -08001084 pr_debug("Not linking %pOFP to %pOFP - No device\n",
1085 con_np, tmp_np);
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001086 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 */
Saravana Kannan8a06d1e2020-11-20 18:02:29 -08001094 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);
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001097 of_node_put(sup_np);
1098 return -EINVAL;
1099 }
Saravana Kannan8a06d1e2020-11-20 18:02:29 -08001100
1101 /*
1102 * Don't create links to "early devices" that won't have struct devices
1103 * created for them.
1104 */
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001105 sup_dev = get_dev_from_fwnode(&sup_np->fwnode);
Saravana Kannanbb278b12020-06-09 18:19:34 -07001106 if (!sup_dev && of_node_check_flag(sup_np, OF_POPULATED)) {
Saravana Kannan8a06d1e2020-11-20 18:02:29 -08001107 pr_debug("Not linking %pOFP to %pOFP - No struct device\n",
1108 con_np, sup_np);
Saravana Kannanbb278b12020-06-09 18:19:34 -07001109 of_node_put(sup_np);
Saravana Kannanba861f82019-11-04 22:49:58 -08001110 return -ENODEV;
Saravana Kannanba861f82019-11-04 22:49:58 -08001111 }
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001112 put_device(sup_dev);
Saravana Kannan8a06d1e2020-11-20 18:02:29 -08001113
1114 fwnode_link_add(of_fwnode_handle(con_np), of_fwnode_handle(sup_np));
1115 of_node_put(sup_np);
1116
1117 return 0;
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001118}
1119
1120/**
1121 * parse_prop_cells - Property parsing function for suppliers
1122 *
1123 * @np: Pointer to device tree node containing a list
1124 * @prop_name: Name of property to be parsed. Expected to hold phandle values
1125 * @index: For properties holding a list of phandles, this is the index
1126 * into the list.
1127 * @list_name: Property name that is known to contain list of phandle(s) to
1128 * supplier(s)
1129 * @cells_name: property name that specifies phandles' arguments count
1130 *
1131 * This is a helper function to parse properties that have a known fixed name
1132 * and are a list of phandles and phandle arguments.
1133 *
1134 * Returns:
1135 * - phandle node pointer with refcount incremented. Caller must of_node_put()
1136 * on it when done.
1137 * - NULL if no phandle found at index
1138 */
1139static struct device_node *parse_prop_cells(struct device_node *np,
1140 const char *prop_name, int index,
1141 const char *list_name,
1142 const char *cells_name)
1143{
1144 struct of_phandle_args sup_args;
1145
1146 if (strcmp(prop_name, list_name))
1147 return NULL;
1148
1149 if (of_parse_phandle_with_args(np, list_name, cells_name, index,
1150 &sup_args))
1151 return NULL;
1152
1153 return sup_args.np;
1154}
1155
Saravana Kannana436ef42019-11-04 22:49:59 -08001156#define DEFINE_SIMPLE_PROP(fname, name, cells) \
1157static struct device_node *parse_##fname(struct device_node *np, \
1158 const char *prop_name, int index) \
1159{ \
1160 return parse_prop_cells(np, prop_name, index, name, cells); \
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001161}
1162
1163static int strcmp_suffix(const char *str, const char *suffix)
1164{
1165 unsigned int len, suffix_len;
1166
1167 len = strlen(str);
1168 suffix_len = strlen(suffix);
1169 if (len <= suffix_len)
1170 return -1;
1171 return strcmp(str + len - suffix_len, suffix);
1172}
1173
Saravana Kannana436ef42019-11-04 22:49:59 -08001174/**
1175 * parse_suffix_prop_cells - Suffix property parsing function for suppliers
1176 *
1177 * @np: Pointer to device tree node containing a list
1178 * @prop_name: Name of property to be parsed. Expected to hold phandle values
1179 * @index: For properties holding a list of phandles, this is the index
1180 * into the list.
1181 * @suffix: Property suffix that is known to contain list of phandle(s) to
1182 * supplier(s)
1183 * @cells_name: property name that specifies phandles' arguments count
1184 *
1185 * This is a helper function to parse properties that have a known fixed suffix
1186 * and are a list of phandles and phandle arguments.
1187 *
1188 * Returns:
1189 * - phandle node pointer with refcount incremented. Caller must of_node_put()
1190 * on it when done.
1191 * - NULL if no phandle found at index
1192 */
1193static struct device_node *parse_suffix_prop_cells(struct device_node *np,
1194 const char *prop_name, int index,
1195 const char *suffix,
1196 const char *cells_name)
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001197{
Saravana Kannana436ef42019-11-04 22:49:59 -08001198 struct of_phandle_args sup_args;
1199
1200 if (strcmp_suffix(prop_name, suffix))
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001201 return NULL;
1202
Saravana Kannana436ef42019-11-04 22:49:59 -08001203 if (of_parse_phandle_with_args(np, prop_name, cells_name, index,
1204 &sup_args))
1205 return NULL;
1206
1207 return sup_args.np;
1208}
1209
1210#define DEFINE_SUFFIX_PROP(fname, suffix, cells) \
1211static struct device_node *parse_##fname(struct device_node *np, \
1212 const char *prop_name, int index) \
1213{ \
1214 return parse_suffix_prop_cells(np, prop_name, index, suffix, cells); \
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001215}
1216
1217/**
1218 * struct supplier_bindings - Property parsing functions for suppliers
1219 *
1220 * @parse_prop: function name
1221 * parse_prop() finds the node corresponding to a supplier phandle
1222 * @parse_prop.np: Pointer to device node holding supplier phandle property
1223 * @parse_prop.prop_name: Name of property holding a phandle value
1224 * @parse_prop.index: For properties holding a list of phandles, this is the
1225 * index into the list
1226 *
1227 * Returns:
1228 * parse_prop() return values are
1229 * - phandle node pointer with refcount incremented. Caller must of_node_put()
1230 * on it when done.
1231 * - NULL if no phandle found at index
1232 */
1233struct supplier_bindings {
1234 struct device_node *(*parse_prop)(struct device_node *np,
1235 const char *prop_name, int index);
1236};
1237
Saravana Kannana436ef42019-11-04 22:49:59 -08001238DEFINE_SIMPLE_PROP(clocks, "clocks", "#clock-cells")
1239DEFINE_SIMPLE_PROP(interconnects, "interconnects", "#interconnect-cells")
Saravana Kannan8e122572019-11-04 22:50:00 -08001240DEFINE_SIMPLE_PROP(iommus, "iommus", "#iommu-cells")
1241DEFINE_SIMPLE_PROP(mboxes, "mboxes", "#mbox-cells")
1242DEFINE_SIMPLE_PROP(io_channels, "io-channel", "#io-channel-cells")
Saravana Kannan7f00be92019-11-19 23:13:01 -08001243DEFINE_SIMPLE_PROP(interrupt_parent, "interrupt-parent", NULL)
1244DEFINE_SIMPLE_PROP(dmas, "dmas", "#dma-cells")
Saravana Kannan2f7afc32020-02-19 21:52:50 -08001245DEFINE_SIMPLE_PROP(power_domains, "power-domains", "#power-domain-cells")
1246DEFINE_SIMPLE_PROP(hwlocks, "hwlocks", "#hwlock-cells")
Saravana Kannan78056e72020-04-01 15:52:03 -07001247DEFINE_SIMPLE_PROP(extcon, "extcon", NULL)
Saravana Kannan53e6a672020-07-24 16:44:14 -07001248DEFINE_SIMPLE_PROP(interrupts_extended, "interrupts-extended",
1249 "#interrupt-cells")
1250DEFINE_SIMPLE_PROP(nvmem_cells, "nvmem-cells", NULL)
1251DEFINE_SIMPLE_PROP(phys, "phys", "#phy-cells")
1252DEFINE_SIMPLE_PROP(wakeup_parent, "wakeup-parent", NULL)
Saravana Kannanfb820b42020-07-24 16:44:15 -07001253DEFINE_SIMPLE_PROP(pinctrl0, "pinctrl-0", NULL)
1254DEFINE_SIMPLE_PROP(pinctrl1, "pinctrl-1", NULL)
1255DEFINE_SIMPLE_PROP(pinctrl2, "pinctrl-2", NULL)
1256DEFINE_SIMPLE_PROP(pinctrl3, "pinctrl-3", NULL)
1257DEFINE_SIMPLE_PROP(pinctrl4, "pinctrl-4", NULL)
1258DEFINE_SIMPLE_PROP(pinctrl5, "pinctrl-5", NULL)
1259DEFINE_SIMPLE_PROP(pinctrl6, "pinctrl-6", NULL)
1260DEFINE_SIMPLE_PROP(pinctrl7, "pinctrl-7", NULL)
1261DEFINE_SIMPLE_PROP(pinctrl8, "pinctrl-8", NULL)
Saravana Kannana436ef42019-11-04 22:49:59 -08001262DEFINE_SUFFIX_PROP(regulators, "-supply", NULL)
Saravana Kannan7f00be92019-11-19 23:13:01 -08001263DEFINE_SUFFIX_PROP(gpio, "-gpio", "#gpio-cells")
1264DEFINE_SUFFIX_PROP(gpios, "-gpios", "#gpio-cells")
Saravana Kannana436ef42019-11-04 22:49:59 -08001265
Will Deacone1495732019-11-20 19:00:28 +00001266static struct device_node *parse_iommu_maps(struct device_node *np,
1267 const char *prop_name, int index)
1268{
1269 if (strcmp(prop_name, "iommu-map"))
1270 return NULL;
1271
1272 return of_parse_phandle(np, prop_name, (index * 4) + 1);
1273}
1274
Saravana Kannane13f5b72021-01-21 14:57:11 -08001275static struct device_node *parse_gpio_compat(struct device_node *np,
1276 const char *prop_name, int index)
1277{
1278 struct of_phandle_args sup_args;
1279
1280 if (strcmp(prop_name, "gpio") && strcmp(prop_name, "gpios"))
1281 return NULL;
1282
1283 /*
1284 * Ignore node with gpio-hog property since its gpios are all provided
1285 * by its parent.
1286 */
1287 if (of_find_property(np, "gpio-hog", NULL))
1288 return NULL;
1289
1290 if (of_parse_phandle_with_args(np, prop_name, "#gpio-cells", index,
1291 &sup_args))
1292 return NULL;
1293
1294 return sup_args.np;
1295}
1296
Saravana Kannan4104ca72021-01-21 14:57:12 -08001297static struct device_node *parse_interrupts(struct device_node *np,
1298 const char *prop_name, int index)
1299{
1300 if (strcmp(prop_name, "interrupts") || index)
1301 return NULL;
1302
1303 return of_irq_find_parent(np);
1304}
1305
Saravana Kannanaf1b9672019-10-11 12:15:19 -07001306static const struct supplier_bindings of_supplier_bindings[] = {
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001307 { .parse_prop = parse_clocks, },
1308 { .parse_prop = parse_interconnects, },
Saravana Kannan8e122572019-11-04 22:50:00 -08001309 { .parse_prop = parse_iommus, },
Will Deacone1495732019-11-20 19:00:28 +00001310 { .parse_prop = parse_iommu_maps, },
Saravana Kannan8e122572019-11-04 22:50:00 -08001311 { .parse_prop = parse_mboxes, },
1312 { .parse_prop = parse_io_channels, },
Saravana Kannan7f00be92019-11-19 23:13:01 -08001313 { .parse_prop = parse_interrupt_parent, },
1314 { .parse_prop = parse_dmas, },
Saravana Kannan2f7afc32020-02-19 21:52:50 -08001315 { .parse_prop = parse_power_domains, },
1316 { .parse_prop = parse_hwlocks, },
Saravana Kannan78056e72020-04-01 15:52:03 -07001317 { .parse_prop = parse_extcon, },
Saravana Kannan53e6a672020-07-24 16:44:14 -07001318 { .parse_prop = parse_interrupts_extended, },
1319 { .parse_prop = parse_nvmem_cells, },
1320 { .parse_prop = parse_phys, },
1321 { .parse_prop = parse_wakeup_parent, },
Saravana Kannanfb820b42020-07-24 16:44:15 -07001322 { .parse_prop = parse_pinctrl0, },
1323 { .parse_prop = parse_pinctrl1, },
1324 { .parse_prop = parse_pinctrl2, },
1325 { .parse_prop = parse_pinctrl3, },
1326 { .parse_prop = parse_pinctrl4, },
1327 { .parse_prop = parse_pinctrl5, },
1328 { .parse_prop = parse_pinctrl6, },
1329 { .parse_prop = parse_pinctrl7, },
1330 { .parse_prop = parse_pinctrl8, },
Saravana Kannane13f5b72021-01-21 14:57:11 -08001331 { .parse_prop = parse_gpio_compat, },
Saravana Kannan4104ca72021-01-21 14:57:12 -08001332 { .parse_prop = parse_interrupts, },
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001333 { .parse_prop = parse_regulators, },
Saravana Kannan7f00be92019-11-19 23:13:01 -08001334 { .parse_prop = parse_gpio, },
1335 { .parse_prop = parse_gpios, },
Saravana Kannanaf1b9672019-10-11 12:15:19 -07001336 {}
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001337};
1338
1339/**
1340 * of_link_property - Create device links to suppliers listed in a property
1341 * @dev: Consumer device
1342 * @con_np: The consumer device tree node which contains the property
1343 * @prop_name: Name of property to be parsed
1344 *
1345 * This function checks if the property @prop_name that is present in the
1346 * @con_np device tree node is one of the known common device tree bindings
1347 * that list phandles to suppliers. If @prop_name isn't one, this function
1348 * doesn't do anything.
1349 *
Saravana Kannan8a06d1e2020-11-20 18:02:29 -08001350 * If @prop_name is one, this function attempts to create fwnode links from the
1351 * consumer device tree node @con_np to all the suppliers device tree nodes
1352 * listed in @prop_name.
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001353 *
Saravana Kannan8a06d1e2020-11-20 18:02:29 -08001354 * Any failed attempt to create a fwnode link will NOT result in an immediate
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001355 * return. of_link_property() must create links to all the available supplier
Saravana Kannan8a06d1e2020-11-20 18:02:29 -08001356 * device tree nodes even when attempts to create a link to one or more
1357 * suppliers fail.
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001358 */
Saravana Kannan8a06d1e2020-11-20 18:02:29 -08001359static int of_link_property(struct device_node *con_np, const char *prop_name)
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001360{
1361 struct device_node *phandle;
Saravana Kannanaf1b9672019-10-11 12:15:19 -07001362 const struct supplier_bindings *s = of_supplier_bindings;
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001363 unsigned int i = 0;
1364 bool matched = false;
1365 int ret = 0;
1366
1367 /* Do not stop at first failed link, link all available suppliers. */
1368 while (!matched && s->parse_prop) {
1369 while ((phandle = s->parse_prop(con_np, prop_name, i))) {
1370 matched = true;
1371 i++;
Saravana Kannan8a06d1e2020-11-20 18:02:29 -08001372 of_link_to_phandle(con_np, phandle);
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001373 of_node_put(phandle);
1374 }
1375 s++;
1376 }
1377 return ret;
1378}
1379
Saravana Kannan2d09e6e2020-11-20 18:02:32 -08001380static int of_fwnode_add_links(struct fwnode_handle *fwnode)
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001381{
Saravana Kannan8a06d1e2020-11-20 18:02:29 -08001382 struct property *p;
1383 struct device_node *con_np = to_of_node(fwnode);
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001384
Saravana Kannan8a06d1e2020-11-20 18:02:29 -08001385 if (!con_np)
1386 return -EINVAL;
1387
1388 for_each_property_of_node(con_np, p)
1389 of_link_property(con_np, p->name);
1390
1391 return 0;
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001392}
1393
Sakari Ailus37081842017-06-06 12:37:37 +03001394const struct fwnode_operations of_fwnode_ops = {
1395 .get = of_fwnode_get,
1396 .put = of_fwnode_put,
Sakari Ailus2294b3a2017-06-06 12:37:39 +03001397 .device_is_available = of_fwnode_device_is_available,
Sinan Kaya1c2c82e2017-12-13 02:20:50 -05001398 .device_get_match_data = of_fwnode_device_get_match_data,
Sakari Ailus37081842017-06-06 12:37:37 +03001399 .property_present = of_fwnode_property_present,
1400 .property_read_int_array = of_fwnode_property_read_int_array,
1401 .property_read_string_array = of_fwnode_property_read_string_array,
Sakari Ailusbc0500c2019-10-03 15:32:12 +03001402 .get_name = of_fwnode_get_name,
Sakari Ailuse7e242b2019-10-03 15:32:13 +03001403 .get_name_prefix = of_fwnode_get_name_prefix,
Sakari Ailus37081842017-06-06 12:37:37 +03001404 .get_parent = of_fwnode_get_parent,
1405 .get_next_child_node = of_fwnode_get_next_child_node,
1406 .get_named_child_node = of_fwnode_get_named_child_node,
Sakari Ailus3e3119d2017-07-21 15:11:49 +03001407 .get_reference_args = of_fwnode_get_reference_args,
Sakari Ailus3b27d002017-06-06 12:37:38 +03001408 .graph_get_next_endpoint = of_fwnode_graph_get_next_endpoint,
1409 .graph_get_remote_endpoint = of_fwnode_graph_get_remote_endpoint,
1410 .graph_get_port_parent = of_fwnode_graph_get_port_parent,
1411 .graph_parse_endpoint = of_fwnode_graph_parse_endpoint,
Saravana Kannana3e1d1a2019-09-04 14:11:22 -07001412 .add_links = of_fwnode_add_links,
Sakari Ailus37081842017-06-06 12:37:37 +03001413};
Sakari Ailusdb3e50f2017-07-21 14:39:31 +03001414EXPORT_SYMBOL_GPL(of_fwnode_ops);