1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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/string.h>
28#include <linux/moduleparam.h>
29
30#include "of_private.h"
31
32
33
34
35
36
37
38
39
40
41
42
43
44int of_property_count_elems_of_size(const struct device_node *np,
45 const char *propname, int elem_size)
46{
47 struct property *prop = of_find_property(np, propname, NULL);
48
49 if (!prop)
50 return -EINVAL;
51 if (!prop->value)
52 return -ENODATA;
53
54 if (prop->length % elem_size != 0) {
55 pr_err("size of %s in node %pOF is not a multiple of %d\n",
56 propname, np, elem_size);
57 return -EINVAL;
58 }
59
60 return prop->length / elem_size;
61}
62EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79static void *of_find_property_value_of_size(const struct device_node *np,
80 const char *propname, u32 min, u32 max, size_t *len)
81{
82 struct property *prop = of_find_property(np, propname, NULL);
83
84 if (!prop)
85 return ERR_PTR(-EINVAL);
86 if (!prop->value)
87 return ERR_PTR(-ENODATA);
88 if (prop->length < min)
89 return ERR_PTR(-EOVERFLOW);
90 if (max && prop->length > max)
91 return ERR_PTR(-EOVERFLOW);
92
93 if (len)
94 *len = prop->length;
95
96 return prop->value;
97}
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114int of_property_read_u32_index(const struct device_node *np,
115 const char *propname,
116 u32 index, u32 *out_value)
117{
118 const u32 *val = of_find_property_value_of_size(np, propname,
119 ((index + 1) * sizeof(*out_value)),
120 0,
121 NULL);
122
123 if (IS_ERR(val))
124 return PTR_ERR(val);
125
126 *out_value = be32_to_cpup(((__be32 *)val) + index);
127 return 0;
128}
129EXPORT_SYMBOL_GPL(of_property_read_u32_index);
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146int of_property_read_u64_index(const struct device_node *np,
147 const char *propname,
148 u32 index, u64 *out_value)
149{
150 const u64 *val = of_find_property_value_of_size(np, propname,
151 ((index + 1) * sizeof(*out_value)),
152 0, NULL);
153
154 if (IS_ERR(val))
155 return PTR_ERR(val);
156
157 *out_value = be64_to_cpup(((__be64 *)val) + index);
158 return 0;
159}
160EXPORT_SYMBOL_GPL(of_property_read_u64_index);
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184int of_property_read_variable_u8_array(const struct device_node *np,
185 const char *propname, u8 *out_values,
186 size_t sz_min, size_t sz_max)
187{
188 size_t sz, count;
189 const u8 *val = of_find_property_value_of_size(np, propname,
190 (sz_min * sizeof(*out_values)),
191 (sz_max * sizeof(*out_values)),
192 &sz);
193
194 if (IS_ERR(val))
195 return PTR_ERR(val);
196
197 if (!sz_max)
198 sz = sz_min;
199 else
200 sz /= sizeof(*out_values);
201
202 count = sz;
203 while (count--)
204 *out_values++ = *val++;
205
206 return sz;
207}
208EXPORT_SYMBOL_GPL(of_property_read_variable_u8_array);
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232int of_property_read_variable_u16_array(const struct device_node *np,
233 const char *propname, u16 *out_values,
234 size_t sz_min, size_t sz_max)
235{
236 size_t sz, count;
237 const __be16 *val = of_find_property_value_of_size(np, propname,
238 (sz_min * sizeof(*out_values)),
239 (sz_max * sizeof(*out_values)),
240 &sz);
241
242 if (IS_ERR(val))
243 return PTR_ERR(val);
244
245 if (!sz_max)
246 sz = sz_min;
247 else
248 sz /= sizeof(*out_values);
249
250 count = sz;
251 while (count--)
252 *out_values++ = be16_to_cpup(val++);
253
254 return sz;
255}
256EXPORT_SYMBOL_GPL(of_property_read_variable_u16_array);
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277int of_property_read_variable_u32_array(const struct device_node *np,
278 const char *propname, u32 *out_values,
279 size_t sz_min, size_t sz_max)
280{
281 size_t sz, count;
282 const __be32 *val = of_find_property_value_of_size(np, propname,
283 (sz_min * sizeof(*out_values)),
284 (sz_max * sizeof(*out_values)),
285 &sz);
286
287 if (IS_ERR(val))
288 return PTR_ERR(val);
289
290 if (!sz_max)
291 sz = sz_min;
292 else
293 sz /= sizeof(*out_values);
294
295 count = sz;
296 while (count--)
297 *out_values++ = be32_to_cpup(val++);
298
299 return sz;
300}
301EXPORT_SYMBOL_GPL(of_property_read_variable_u32_array);
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316int of_property_read_u64(const struct device_node *np, const char *propname,
317 u64 *out_value)
318{
319 const __be32 *val = of_find_property_value_of_size(np, propname,
320 sizeof(*out_value),
321 0,
322 NULL);
323
324 if (IS_ERR(val))
325 return PTR_ERR(val);
326
327 *out_value = of_read_number(val, 2);
328 return 0;
329}
330EXPORT_SYMBOL_GPL(of_property_read_u64);
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351int of_property_read_variable_u64_array(const struct device_node *np,
352 const char *propname, u64 *out_values,
353 size_t sz_min, size_t sz_max)
354{
355 size_t sz, count;
356 const __be32 *val = of_find_property_value_of_size(np, propname,
357 (sz_min * sizeof(*out_values)),
358 (sz_max * sizeof(*out_values)),
359 &sz);
360
361 if (IS_ERR(val))
362 return PTR_ERR(val);
363
364 if (!sz_max)
365 sz = sz_min;
366 else
367 sz /= sizeof(*out_values);
368
369 count = sz;
370 while (count--) {
371 *out_values++ = of_read_number(val, 2);
372 val += 2;
373 }
374
375 return sz;
376}
377EXPORT_SYMBOL_GPL(of_property_read_variable_u64_array);
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394int of_property_read_string(const struct device_node *np, const char *propname,
395 const char **out_string)
396{
397 const struct property *prop = of_find_property(np, propname, NULL);
398 if (!prop)
399 return -EINVAL;
400 if (!prop->value)
401 return -ENODATA;
402 if (strnlen(prop->value, prop->length) >= prop->length)
403 return -EILSEQ;
404 *out_string = prop->value;
405 return 0;
406}
407EXPORT_SYMBOL_GPL(of_property_read_string);
408
409
410
411
412
413
414
415
416
417
418int of_property_match_string(const struct device_node *np, const char *propname,
419 const char *string)
420{
421 const struct property *prop = of_find_property(np, propname, NULL);
422 size_t l;
423 int i;
424 const char *p, *end;
425
426 if (!prop)
427 return -EINVAL;
428 if (!prop->value)
429 return -ENODATA;
430
431 p = prop->value;
432 end = p + prop->length;
433
434 for (i = 0; p < end; i++, p += l) {
435 l = strnlen(p, end - p) + 1;
436 if (p + l > end)
437 return -EILSEQ;
438 pr_debug("comparing %s with %s\n", string, p);
439 if (strcmp(string, p) == 0)
440 return i;
441 }
442 return -ENODATA;
443}
444EXPORT_SYMBOL_GPL(of_property_match_string);
445
446
447
448
449
450
451
452
453
454
455
456
457int of_property_read_string_helper(const struct device_node *np,
458 const char *propname, const char **out_strs,
459 size_t sz, int skip)
460{
461 const struct property *prop = of_find_property(np, propname, NULL);
462 int l = 0, i = 0;
463 const char *p, *end;
464
465 if (!prop)
466 return -EINVAL;
467 if (!prop->value)
468 return -ENODATA;
469 p = prop->value;
470 end = p + prop->length;
471
472 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
473 l = strnlen(p, end - p) + 1;
474 if (p + l > end)
475 return -EILSEQ;
476 if (out_strs && i >= skip)
477 *out_strs++ = p;
478 }
479 i -= skip;
480 return i <= 0 ? -ENODATA : i;
481}
482EXPORT_SYMBOL_GPL(of_property_read_string_helper);
483
484const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
485 u32 *pu)
486{
487 const void *curv = cur;
488
489 if (!prop)
490 return NULL;
491
492 if (!cur) {
493 curv = prop->value;
494 goto out_val;
495 }
496
497 curv += sizeof(*cur);
498 if (curv >= prop->value + prop->length)
499 return NULL;
500
501out_val:
502 *pu = be32_to_cpup(curv);
503 return curv;
504}
505EXPORT_SYMBOL_GPL(of_prop_next_u32);
506
507const char *of_prop_next_string(struct property *prop, const char *cur)
508{
509 const void *curv = cur;
510
511 if (!prop)
512 return NULL;
513
514 if (!cur)
515 return prop->value;
516
517 curv += strlen(cur) + 1;
518 if (curv >= prop->value + prop->length)
519 return NULL;
520
521 return curv;
522}
523EXPORT_SYMBOL_GPL(of_prop_next_string);
524
525
526
527
528
529
530
531
532int of_graph_parse_endpoint(const struct device_node *node,
533 struct of_endpoint *endpoint)
534{
535 struct device_node *port_node = of_get_parent(node);
536
537 WARN_ONCE(!port_node, "%s(): endpoint %pOF has no parent node\n",
538 __func__, node);
539
540 memset(endpoint, 0, sizeof(*endpoint));
541
542 endpoint->local_node = node;
543
544
545
546
547 of_property_read_u32(port_node, "reg", &endpoint->port);
548 of_property_read_u32(node, "reg", &endpoint->id);
549
550 of_node_put(port_node);
551
552 return 0;
553}
554EXPORT_SYMBOL(of_graph_parse_endpoint);
555
556
557
558
559
560
561
562
563
564struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id)
565{
566 struct device_node *node, *port;
567
568 node = of_get_child_by_name(parent, "ports");
569 if (node)
570 parent = node;
571
572 for_each_child_of_node(parent, port) {
573 u32 port_id = 0;
574
575 if (!of_node_name_eq(port, "port"))
576 continue;
577 of_property_read_u32(port, "reg", &port_id);
578 if (id == port_id)
579 break;
580 }
581
582 of_node_put(node);
583
584 return port;
585}
586EXPORT_SYMBOL(of_graph_get_port_by_id);
587
588
589
590
591
592
593
594
595
596struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
597 struct device_node *prev)
598{
599 struct device_node *endpoint;
600 struct device_node *port;
601
602 if (!parent)
603 return NULL;
604
605
606
607
608
609
610 if (!prev) {
611 struct device_node *node;
612
613 node = of_get_child_by_name(parent, "ports");
614 if (node)
615 parent = node;
616
617 port = of_get_child_by_name(parent, "port");
618 of_node_put(node);
619
620 if (!port) {
621 pr_err("graph: no port node found in %pOF\n", parent);
622 return NULL;
623 }
624 } else {
625 port = of_get_parent(prev);
626 if (WARN_ONCE(!port, "%s(): endpoint %pOF has no parent node\n",
627 __func__, prev))
628 return NULL;
629 }
630
631 while (1) {
632
633
634
635
636
637 endpoint = of_get_next_child(port, prev);
638 if (endpoint) {
639 of_node_put(port);
640 return endpoint;
641 }
642
643
644 prev = NULL;
645
646 do {
647 port = of_get_next_child(parent, port);
648 if (!port)
649 return NULL;
650 } while (!of_node_name_eq(port, "port"));
651 }
652}
653EXPORT_SYMBOL(of_graph_get_next_endpoint);
654
655
656
657
658
659
660
661
662
663
664
665struct device_node *of_graph_get_endpoint_by_regs(
666 const struct device_node *parent, int port_reg, int reg)
667{
668 struct of_endpoint endpoint;
669 struct device_node *node = NULL;
670
671 for_each_endpoint_of_node(parent, node) {
672 of_graph_parse_endpoint(node, &endpoint);
673 if (((port_reg == -1) || (endpoint.port == port_reg)) &&
674 ((reg == -1) || (endpoint.id == reg)))
675 return node;
676 }
677
678 return NULL;
679}
680EXPORT_SYMBOL(of_graph_get_endpoint_by_regs);
681
682
683
684
685
686
687
688
689struct device_node *of_graph_get_remote_endpoint(const struct device_node *node)
690{
691
692 return of_parse_phandle(node, "remote-endpoint", 0);
693}
694EXPORT_SYMBOL(of_graph_get_remote_endpoint);
695
696
697
698
699
700
701
702
703struct device_node *of_graph_get_port_parent(struct device_node *node)
704{
705 unsigned int depth;
706
707 if (!node)
708 return NULL;
709
710
711
712
713
714 of_node_get(node);
715
716
717 for (depth = 3; depth && node; depth--) {
718 node = of_get_next_parent(node);
719 if (depth == 2 && !of_node_name_eq(node, "ports"))
720 break;
721 }
722 return node;
723}
724EXPORT_SYMBOL(of_graph_get_port_parent);
725
726
727
728
729
730
731
732
733struct device_node *of_graph_get_remote_port_parent(
734 const struct device_node *node)
735{
736 struct device_node *np, *pp;
737
738
739 np = of_graph_get_remote_endpoint(node);
740
741 pp = of_graph_get_port_parent(np);
742
743 of_node_put(np);
744
745 return pp;
746}
747EXPORT_SYMBOL(of_graph_get_remote_port_parent);
748
749
750
751
752
753
754
755
756struct device_node *of_graph_get_remote_port(const struct device_node *node)
757{
758 struct device_node *np;
759
760
761 np = of_graph_get_remote_endpoint(node);
762 if (!np)
763 return NULL;
764 return of_get_next_parent(np);
765}
766EXPORT_SYMBOL(of_graph_get_remote_port);
767
768int of_graph_get_endpoint_count(const struct device_node *np)
769{
770 struct device_node *endpoint;
771 int num = 0;
772
773 for_each_endpoint_of_node(np, endpoint)
774 num++;
775
776 return num;
777}
778EXPORT_SYMBOL(of_graph_get_endpoint_count);
779
780
781
782
783
784
785
786
787
788
789struct device_node *of_graph_get_remote_node(const struct device_node *node,
790 u32 port, u32 endpoint)
791{
792 struct device_node *endpoint_node, *remote;
793
794 endpoint_node = of_graph_get_endpoint_by_regs(node, port, endpoint);
795 if (!endpoint_node) {
796 pr_debug("no valid endpoint (%d, %d) for node %pOF\n",
797 port, endpoint, node);
798 return NULL;
799 }
800
801 remote = of_graph_get_remote_port_parent(endpoint_node);
802 of_node_put(endpoint_node);
803 if (!remote) {
804 pr_debug("no valid remote node\n");
805 return NULL;
806 }
807
808 if (!of_device_is_available(remote)) {
809 pr_debug("not available for remote node\n");
810 of_node_put(remote);
811 return NULL;
812 }
813
814 return remote;
815}
816EXPORT_SYMBOL(of_graph_get_remote_node);
817
818static struct fwnode_handle *of_fwnode_get(struct fwnode_handle *fwnode)
819{
820 return of_fwnode_handle(of_node_get(to_of_node(fwnode)));
821}
822
823static void of_fwnode_put(struct fwnode_handle *fwnode)
824{
825 of_node_put(to_of_node(fwnode));
826}
827
828static bool of_fwnode_device_is_available(const struct fwnode_handle *fwnode)
829{
830 return of_device_is_available(to_of_node(fwnode));
831}
832
833static bool of_fwnode_property_present(const struct fwnode_handle *fwnode,
834 const char *propname)
835{
836 return of_property_read_bool(to_of_node(fwnode), propname);
837}
838
839static int of_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
840 const char *propname,
841 unsigned int elem_size, void *val,
842 size_t nval)
843{
844 const struct device_node *node = to_of_node(fwnode);
845
846 if (!val)
847 return of_property_count_elems_of_size(node, propname,
848 elem_size);
849
850 switch (elem_size) {
851 case sizeof(u8):
852 return of_property_read_u8_array(node, propname, val, nval);
853 case sizeof(u16):
854 return of_property_read_u16_array(node, propname, val, nval);
855 case sizeof(u32):
856 return of_property_read_u32_array(node, propname, val, nval);
857 case sizeof(u64):
858 return of_property_read_u64_array(node, propname, val, nval);
859 }
860
861 return -ENXIO;
862}
863
864static int
865of_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
866 const char *propname, const char **val,
867 size_t nval)
868{
869 const struct device_node *node = to_of_node(fwnode);
870
871 return val ?
872 of_property_read_string_array(node, propname, val, nval) :
873 of_property_count_strings(node, propname);
874}
875
876static const char *of_fwnode_get_name(const struct fwnode_handle *fwnode)
877{
878 return kbasename(to_of_node(fwnode)->full_name);
879}
880
881static const char *of_fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
882{
883
884 if (!to_of_node(fwnode)->parent)
885 return "";
886
887 return "/";
888}
889
890static struct fwnode_handle *
891of_fwnode_get_parent(const struct fwnode_handle *fwnode)
892{
893 return of_fwnode_handle(of_get_parent(to_of_node(fwnode)));
894}
895
896static struct fwnode_handle *
897of_fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
898 struct fwnode_handle *child)
899{
900 return of_fwnode_handle(of_get_next_available_child(to_of_node(fwnode),
901 to_of_node(child)));
902}
903
904static struct fwnode_handle *
905of_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
906 const char *childname)
907{
908 const struct device_node *node = to_of_node(fwnode);
909 struct device_node *child;
910
911 for_each_available_child_of_node(node, child)
912 if (of_node_name_eq(child, childname))
913 return of_fwnode_handle(child);
914
915 return NULL;
916}
917
918static int
919of_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
920 const char *prop, const char *nargs_prop,
921 unsigned int nargs, unsigned int index,
922 struct fwnode_reference_args *args)
923{
924 struct of_phandle_args of_args;
925 unsigned int i;
926 int ret;
927
928 if (nargs_prop)
929 ret = of_parse_phandle_with_args(to_of_node(fwnode), prop,
930 nargs_prop, index, &of_args);
931 else
932 ret = of_parse_phandle_with_fixed_args(to_of_node(fwnode), prop,
933 nargs, index, &of_args);
934 if (ret < 0)
935 return ret;
936 if (!args)
937 return 0;
938
939 args->nargs = of_args.args_count;
940 args->fwnode = of_fwnode_handle(of_args.np);
941
942 for (i = 0; i < NR_FWNODE_REFERENCE_ARGS; i++)
943 args->args[i] = i < of_args.args_count ? of_args.args[i] : 0;
944
945 return 0;
946}
947
948static struct fwnode_handle *
949of_fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
950 struct fwnode_handle *prev)
951{
952 return of_fwnode_handle(of_graph_get_next_endpoint(to_of_node(fwnode),
953 to_of_node(prev)));
954}
955
956static struct fwnode_handle *
957of_fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
958{
959 return of_fwnode_handle(
960 of_graph_get_remote_endpoint(to_of_node(fwnode)));
961}
962
963static struct fwnode_handle *
964of_fwnode_graph_get_port_parent(struct fwnode_handle *fwnode)
965{
966 struct device_node *np;
967
968
969 np = of_get_parent(to_of_node(fwnode));
970 if (!np)
971 return NULL;
972
973
974 if (!of_node_name_eq(np, "ports"))
975 return of_fwnode_handle(np);
976
977 return of_fwnode_handle(of_get_next_parent(np));
978}
979
980static int of_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
981 struct fwnode_endpoint *endpoint)
982{
983 const struct device_node *node = to_of_node(fwnode);
984 struct device_node *port_node = of_get_parent(node);
985
986 endpoint->local_fwnode = fwnode;
987
988 of_property_read_u32(port_node, "reg", &endpoint->port);
989 of_property_read_u32(node, "reg", &endpoint->id);
990
991 of_node_put(port_node);
992
993 return 0;
994}
995
996static const void *
997of_fwnode_device_get_match_data(const struct fwnode_handle *fwnode,
998 const struct device *dev)
999{
1000 return of_device_get_match_data(dev);
1001}
1002
1003static bool of_is_ancestor_of(struct device_node *test_ancestor,
1004 struct device_node *child)
1005{
1006 of_node_get(child);
1007 while (child) {
1008 if (child == test_ancestor) {
1009 of_node_put(child);
1010 return true;
1011 }
1012 child = of_get_next_parent(child);
1013 }
1014 return false;
1015}
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035static int of_link_to_phandle(struct device *dev, struct device_node *sup_np,
1036 u32 dl_flags)
1037{
1038 struct device *sup_dev;
1039 int ret = 0;
1040 struct device_node *tmp_np = sup_np;
1041 int is_populated;
1042
1043 of_node_get(sup_np);
1044
1045
1046
1047
1048 while (sup_np) {
1049
1050
1051 if (!of_device_is_available(sup_np)) {
1052 of_node_put(sup_np);
1053 sup_np = NULL;
1054 }
1055
1056 if (of_find_property(sup_np, "compatible", NULL))
1057 break;
1058
1059 sup_np = of_get_next_parent(sup_np);
1060 }
1061
1062 if (!sup_np) {
1063 dev_dbg(dev, "Not linking to %pOFP - No device\n", tmp_np);
1064 return -ENODEV;
1065 }
1066
1067
1068
1069
1070
1071
1072 if (of_is_ancestor_of(dev->of_node, sup_np)) {
1073 dev_dbg(dev, "Not linking to %pOFP - is descendant\n", sup_np);
1074 of_node_put(sup_np);
1075 return -EINVAL;
1076 }
1077 sup_dev = get_dev_from_fwnode(&sup_np->fwnode);
1078 is_populated = of_node_check_flag(sup_np, OF_POPULATED);
1079 of_node_put(sup_np);
1080 if (!sup_dev && is_populated) {
1081
1082 dev_dbg(dev, "Not linking to %pOFP - No struct device\n",
1083 sup_np);
1084 return -ENODEV;
1085 } else if (!sup_dev) {
1086 return -EAGAIN;
1087 }
1088 if (!device_link_add(dev, sup_dev, dl_flags))
1089 ret = -EINVAL;
1090 put_device(sup_dev);
1091 return ret;
1092}
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113static struct device_node *parse_prop_cells(struct device_node *np,
1114 const char *prop_name, int index,
1115 const char *list_name,
1116 const char *cells_name)
1117{
1118 struct of_phandle_args sup_args;
1119
1120 if (strcmp(prop_name, list_name))
1121 return NULL;
1122
1123 if (of_parse_phandle_with_args(np, list_name, cells_name, index,
1124 &sup_args))
1125 return NULL;
1126
1127 return sup_args.np;
1128}
1129
1130#define DEFINE_SIMPLE_PROP(fname, name, cells) \
1131static struct device_node *parse_##fname(struct device_node *np, \
1132 const char *prop_name, int index) \
1133{ \
1134 return parse_prop_cells(np, prop_name, index, name, cells); \
1135}
1136
1137static int strcmp_suffix(const char *str, const char *suffix)
1138{
1139 unsigned int len, suffix_len;
1140
1141 len = strlen(str);
1142 suffix_len = strlen(suffix);
1143 if (len <= suffix_len)
1144 return -1;
1145 return strcmp(str + len - suffix_len, suffix);
1146}
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167static struct device_node *parse_suffix_prop_cells(struct device_node *np,
1168 const char *prop_name, int index,
1169 const char *suffix,
1170 const char *cells_name)
1171{
1172 struct of_phandle_args sup_args;
1173
1174 if (strcmp_suffix(prop_name, suffix))
1175 return NULL;
1176
1177 if (of_parse_phandle_with_args(np, prop_name, cells_name, index,
1178 &sup_args))
1179 return NULL;
1180
1181 return sup_args.np;
1182}
1183
1184#define DEFINE_SUFFIX_PROP(fname, suffix, cells) \
1185static struct device_node *parse_##fname(struct device_node *np, \
1186 const char *prop_name, int index) \
1187{ \
1188 return parse_suffix_prop_cells(np, prop_name, index, suffix, cells); \
1189}
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207struct supplier_bindings {
1208 struct device_node *(*parse_prop)(struct device_node *np,
1209 const char *prop_name, int index);
1210};
1211
1212DEFINE_SIMPLE_PROP(clocks, "clocks", "#clock-cells")
1213DEFINE_SIMPLE_PROP(interconnects, "interconnects", "#interconnect-cells")
1214DEFINE_SIMPLE_PROP(iommus, "iommus", "#iommu-cells")
1215DEFINE_SIMPLE_PROP(mboxes, "mboxes", "#mbox-cells")
1216DEFINE_SIMPLE_PROP(io_channels, "io-channel", "#io-channel-cells")
1217DEFINE_SIMPLE_PROP(interrupt_parent, "interrupt-parent", NULL)
1218DEFINE_SIMPLE_PROP(dmas, "dmas", "#dma-cells")
1219DEFINE_SIMPLE_PROP(power_domains, "power-domains", "#power-domain-cells")
1220DEFINE_SIMPLE_PROP(hwlocks, "hwlocks", "#hwlock-cells")
1221DEFINE_SIMPLE_PROP(extcon, "extcon", NULL)
1222DEFINE_SUFFIX_PROP(regulators, "-supply", NULL)
1223DEFINE_SUFFIX_PROP(gpio, "-gpio", "#gpio-cells")
1224DEFINE_SUFFIX_PROP(gpios, "-gpios", "#gpio-cells")
1225
1226static struct device_node *parse_iommu_maps(struct device_node *np,
1227 const char *prop_name, int index)
1228{
1229 if (strcmp(prop_name, "iommu-map"))
1230 return NULL;
1231
1232 return of_parse_phandle(np, prop_name, (index * 4) + 1);
1233}
1234
1235static const struct supplier_bindings of_supplier_bindings[] = {
1236 { .parse_prop = parse_clocks, },
1237 { .parse_prop = parse_interconnects, },
1238 { .parse_prop = parse_iommus, },
1239 { .parse_prop = parse_iommu_maps, },
1240 { .parse_prop = parse_mboxes, },
1241 { .parse_prop = parse_io_channels, },
1242 { .parse_prop = parse_interrupt_parent, },
1243 { .parse_prop = parse_dmas, },
1244 { .parse_prop = parse_power_domains, },
1245 { .parse_prop = parse_hwlocks, },
1246 { .parse_prop = parse_extcon, },
1247 { .parse_prop = parse_regulators, },
1248 { .parse_prop = parse_gpio, },
1249 { .parse_prop = parse_gpios, },
1250 {}
1251};
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272static int of_link_property(struct device *dev, struct device_node *con_np,
1273 const char *prop_name)
1274{
1275 struct device_node *phandle;
1276 const struct supplier_bindings *s = of_supplier_bindings;
1277 unsigned int i = 0;
1278 bool matched = false;
1279 int ret = 0;
1280 u32 dl_flags;
1281
1282 if (dev->of_node == con_np)
1283 dl_flags = fw_devlink_get_flags();
1284 else
1285 dl_flags = DL_FLAG_SYNC_STATE_ONLY;
1286
1287
1288 while (!matched && s->parse_prop) {
1289 while ((phandle = s->parse_prop(con_np, prop_name, i))) {
1290 matched = true;
1291 i++;
1292 if (of_link_to_phandle(dev, phandle, dl_flags)
1293 == -EAGAIN)
1294 ret = -EAGAIN;
1295 of_node_put(phandle);
1296 }
1297 s++;
1298 }
1299 return ret;
1300}
1301
1302static int of_link_to_suppliers(struct device *dev,
1303 struct device_node *con_np)
1304{
1305 struct device_node *child;
1306 struct property *p;
1307 int ret = 0;
1308
1309 for_each_property_of_node(con_np, p)
1310 if (of_link_property(dev, con_np, p->name))
1311 ret = -ENODEV;
1312
1313 for_each_available_child_of_node(con_np, child)
1314 if (of_link_to_suppliers(dev, child) && !ret)
1315 ret = -EAGAIN;
1316
1317 return ret;
1318}
1319
1320static int of_fwnode_add_links(const struct fwnode_handle *fwnode,
1321 struct device *dev)
1322{
1323 if (unlikely(!is_of_node(fwnode)))
1324 return 0;
1325
1326 return of_link_to_suppliers(dev, to_of_node(fwnode));
1327}
1328
1329const struct fwnode_operations of_fwnode_ops = {
1330 .get = of_fwnode_get,
1331 .put = of_fwnode_put,
1332 .device_is_available = of_fwnode_device_is_available,
1333 .device_get_match_data = of_fwnode_device_get_match_data,
1334 .property_present = of_fwnode_property_present,
1335 .property_read_int_array = of_fwnode_property_read_int_array,
1336 .property_read_string_array = of_fwnode_property_read_string_array,
1337 .get_name = of_fwnode_get_name,
1338 .get_name_prefix = of_fwnode_get_name_prefix,
1339 .get_parent = of_fwnode_get_parent,
1340 .get_next_child_node = of_fwnode_get_next_child_node,
1341 .get_named_child_node = of_fwnode_get_named_child_node,
1342 .get_reference_args = of_fwnode_get_reference_args,
1343 .graph_get_next_endpoint = of_fwnode_graph_get_next_endpoint,
1344 .graph_get_remote_endpoint = of_fwnode_graph_get_remote_endpoint,
1345 .graph_get_port_parent = of_fwnode_graph_get_port_parent,
1346 .graph_parse_endpoint = of_fwnode_graph_parse_endpoint,
1347 .add_links = of_fwnode_add_links,
1348};
1349EXPORT_SYMBOL_GPL(of_fwnode_ops);
1350