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