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
29#include "of_private.h"
30
31
32
33
34
35
36
37
38
39
40
41
42
43int of_property_count_elems_of_size(const struct device_node *np,
44 const char *propname, int elem_size)
45{
46 struct property *prop = of_find_property(np, propname, NULL);
47
48 if (!prop)
49 return -EINVAL;
50 if (!prop->value)
51 return -ENODATA;
52
53 if (prop->length % elem_size != 0) {
54 pr_err("size of %s in node %pOF is not a multiple of %d\n",
55 propname, np, elem_size);
56 return -EINVAL;
57 }
58
59 return prop->length / elem_size;
60}
61EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78static void *of_find_property_value_of_size(const struct device_node *np,
79 const char *propname, u32 min, u32 max, size_t *len)
80{
81 struct property *prop = of_find_property(np, propname, NULL);
82
83 if (!prop)
84 return ERR_PTR(-EINVAL);
85 if (!prop->value)
86 return ERR_PTR(-ENODATA);
87 if (prop->length < min)
88 return ERR_PTR(-EOVERFLOW);
89 if (max && prop->length > max)
90 return ERR_PTR(-EOVERFLOW);
91
92 if (len)
93 *len = prop->length;
94
95 return prop->value;
96}
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113int of_property_read_u32_index(const struct device_node *np,
114 const char *propname,
115 u32 index, u32 *out_value)
116{
117 const u32 *val = of_find_property_value_of_size(np, propname,
118 ((index + 1) * sizeof(*out_value)),
119 0,
120 NULL);
121
122 if (IS_ERR(val))
123 return PTR_ERR(val);
124
125 *out_value = be32_to_cpup(((__be32 *)val) + index);
126 return 0;
127}
128EXPORT_SYMBOL_GPL(of_property_read_u32_index);
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145int of_property_read_u64_index(const struct device_node *np,
146 const char *propname,
147 u32 index, u64 *out_value)
148{
149 const u64 *val = of_find_property_value_of_size(np, propname,
150 ((index + 1) * sizeof(*out_value)),
151 0, NULL);
152
153 if (IS_ERR(val))
154 return PTR_ERR(val);
155
156 *out_value = be64_to_cpup(((__be64 *)val) + index);
157 return 0;
158}
159EXPORT_SYMBOL_GPL(of_property_read_u64_index);
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183int of_property_read_variable_u8_array(const struct device_node *np,
184 const char *propname, u8 *out_values,
185 size_t sz_min, size_t sz_max)
186{
187 size_t sz, count;
188 const u8 *val = of_find_property_value_of_size(np, propname,
189 (sz_min * sizeof(*out_values)),
190 (sz_max * sizeof(*out_values)),
191 &sz);
192
193 if (IS_ERR(val))
194 return PTR_ERR(val);
195
196 if (!sz_max)
197 sz = sz_min;
198 else
199 sz /= sizeof(*out_values);
200
201 count = sz;
202 while (count--)
203 *out_values++ = *val++;
204
205 return sz;
206}
207EXPORT_SYMBOL_GPL(of_property_read_variable_u8_array);
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231int of_property_read_variable_u16_array(const struct device_node *np,
232 const char *propname, u16 *out_values,
233 size_t sz_min, size_t sz_max)
234{
235 size_t sz, count;
236 const __be16 *val = of_find_property_value_of_size(np, propname,
237 (sz_min * sizeof(*out_values)),
238 (sz_max * sizeof(*out_values)),
239 &sz);
240
241 if (IS_ERR(val))
242 return PTR_ERR(val);
243
244 if (!sz_max)
245 sz = sz_min;
246 else
247 sz /= sizeof(*out_values);
248
249 count = sz;
250 while (count--)
251 *out_values++ = be16_to_cpup(val++);
252
253 return sz;
254}
255EXPORT_SYMBOL_GPL(of_property_read_variable_u16_array);
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276int of_property_read_variable_u32_array(const struct device_node *np,
277 const char *propname, u32 *out_values,
278 size_t sz_min, size_t sz_max)
279{
280 size_t sz, count;
281 const __be32 *val = of_find_property_value_of_size(np, propname,
282 (sz_min * sizeof(*out_values)),
283 (sz_max * sizeof(*out_values)),
284 &sz);
285
286 if (IS_ERR(val))
287 return PTR_ERR(val);
288
289 if (!sz_max)
290 sz = sz_min;
291 else
292 sz /= sizeof(*out_values);
293
294 count = sz;
295 while (count--)
296 *out_values++ = be32_to_cpup(val++);
297
298 return sz;
299}
300EXPORT_SYMBOL_GPL(of_property_read_variable_u32_array);
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315int of_property_read_u64(const struct device_node *np, const char *propname,
316 u64 *out_value)
317{
318 const __be32 *val = of_find_property_value_of_size(np, propname,
319 sizeof(*out_value),
320 0,
321 NULL);
322
323 if (IS_ERR(val))
324 return PTR_ERR(val);
325
326 *out_value = of_read_number(val, 2);
327 return 0;
328}
329EXPORT_SYMBOL_GPL(of_property_read_u64);
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350int of_property_read_variable_u64_array(const struct device_node *np,
351 const char *propname, u64 *out_values,
352 size_t sz_min, size_t sz_max)
353{
354 size_t sz, count;
355 const __be32 *val = of_find_property_value_of_size(np, propname,
356 (sz_min * sizeof(*out_values)),
357 (sz_max * sizeof(*out_values)),
358 &sz);
359
360 if (IS_ERR(val))
361 return PTR_ERR(val);
362
363 if (!sz_max)
364 sz = sz_min;
365 else
366 sz /= sizeof(*out_values);
367
368 count = sz;
369 while (count--) {
370 *out_values++ = of_read_number(val, 2);
371 val += 2;
372 }
373
374 return sz;
375}
376EXPORT_SYMBOL_GPL(of_property_read_variable_u64_array);
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393int of_property_read_string(const struct device_node *np, const char *propname,
394 const char **out_string)
395{
396 const struct property *prop = of_find_property(np, propname, NULL);
397 if (!prop)
398 return -EINVAL;
399 if (!prop->value)
400 return -ENODATA;
401 if (strnlen(prop->value, prop->length) >= prop->length)
402 return -EILSEQ;
403 *out_string = prop->value;
404 return 0;
405}
406EXPORT_SYMBOL_GPL(of_property_read_string);
407
408
409
410
411
412
413
414
415
416
417int of_property_match_string(const struct device_node *np, const char *propname,
418 const char *string)
419{
420 const struct property *prop = of_find_property(np, propname, NULL);
421 size_t l;
422 int i;
423 const char *p, *end;
424
425 if (!prop)
426 return -EINVAL;
427 if (!prop->value)
428 return -ENODATA;
429
430 p = prop->value;
431 end = p + prop->length;
432
433 for (i = 0; p < end; i++, p += l) {
434 l = strnlen(p, end - p) + 1;
435 if (p + l > end)
436 return -EILSEQ;
437 pr_debug("comparing %s with %s\n", string, p);
438 if (strcmp(string, p) == 0)
439 return i;
440 }
441 return -ENODATA;
442}
443EXPORT_SYMBOL_GPL(of_property_match_string);
444
445
446
447
448
449
450
451
452
453
454
455
456int of_property_read_string_helper(const struct device_node *np,
457 const char *propname, const char **out_strs,
458 size_t sz, int skip)
459{
460 const struct property *prop = of_find_property(np, propname, NULL);
461 int l = 0, i = 0;
462 const char *p, *end;
463
464 if (!prop)
465 return -EINVAL;
466 if (!prop->value)
467 return -ENODATA;
468 p = prop->value;
469 end = p + prop->length;
470
471 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
472 l = strnlen(p, end - p) + 1;
473 if (p + l > end)
474 return -EILSEQ;
475 if (out_strs && i >= skip)
476 *out_strs++ = p;
477 }
478 i -= skip;
479 return i <= 0 ? -ENODATA : i;
480}
481EXPORT_SYMBOL_GPL(of_property_read_string_helper);
482
483const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
484 u32 *pu)
485{
486 const void *curv = cur;
487
488 if (!prop)
489 return NULL;
490
491 if (!cur) {
492 curv = prop->value;
493 goto out_val;
494 }
495
496 curv += sizeof(*cur);
497 if (curv >= prop->value + prop->length)
498 return NULL;
499
500out_val:
501 *pu = be32_to_cpup(curv);
502 return curv;
503}
504EXPORT_SYMBOL_GPL(of_prop_next_u32);
505
506const char *of_prop_next_string(struct property *prop, const char *cur)
507{
508 const void *curv = cur;
509
510 if (!prop)
511 return NULL;
512
513 if (!cur)
514 return prop->value;
515
516 curv += strlen(cur) + 1;
517 if (curv >= prop->value + prop->length)
518 return NULL;
519
520 return curv;
521}
522EXPORT_SYMBOL_GPL(of_prop_next_string);
523
524
525
526
527
528
529
530
531int of_graph_parse_endpoint(const struct device_node *node,
532 struct of_endpoint *endpoint)
533{
534 struct device_node *port_node = of_get_parent(node);
535
536 WARN_ONCE(!port_node, "%s(): endpoint %pOF has no parent node\n",
537 __func__, node);
538
539 memset(endpoint, 0, sizeof(*endpoint));
540
541 endpoint->local_node = node;
542
543
544
545
546 of_property_read_u32(port_node, "reg", &endpoint->port);
547 of_property_read_u32(node, "reg", &endpoint->id);
548
549 of_node_put(port_node);
550
551 return 0;
552}
553EXPORT_SYMBOL(of_graph_parse_endpoint);
554
555
556
557
558
559
560
561
562
563struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id)
564{
565 struct device_node *node, *port;
566
567 node = of_get_child_by_name(parent, "ports");
568 if (node)
569 parent = node;
570
571 for_each_child_of_node(parent, port) {
572 u32 port_id = 0;
573
574 if (!of_node_name_eq(port, "port"))
575 continue;
576 of_property_read_u32(port, "reg", &port_id);
577 if (id == port_id)
578 break;
579 }
580
581 of_node_put(node);
582
583 return port;
584}
585EXPORT_SYMBOL(of_graph_get_port_by_id);
586
587
588
589
590
591
592
593
594
595struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
596 struct device_node *prev)
597{
598 struct device_node *endpoint;
599 struct device_node *port;
600
601 if (!parent)
602 return NULL;
603
604
605
606
607
608
609 if (!prev) {
610 struct device_node *node;
611
612 node = of_get_child_by_name(parent, "ports");
613 if (node)
614 parent = node;
615
616 port = of_get_child_by_name(parent, "port");
617 of_node_put(node);
618
619 if (!port) {
620 pr_err("graph: no port node found in %pOF\n", parent);
621 return NULL;
622 }
623 } else {
624 port = of_get_parent(prev);
625 if (WARN_ONCE(!port, "%s(): endpoint %pOF has no parent node\n",
626 __func__, prev))
627 return NULL;
628 }
629
630 while (1) {
631
632
633
634
635
636 endpoint = of_get_next_child(port, prev);
637 if (endpoint) {
638 of_node_put(port);
639 return endpoint;
640 }
641
642
643 prev = NULL;
644
645 do {
646 port = of_get_next_child(parent, port);
647 if (!port)
648 return NULL;
649 } while (!of_node_name_eq(port, "port"));
650 }
651}
652EXPORT_SYMBOL(of_graph_get_next_endpoint);
653
654
655
656
657
658
659
660
661
662
663
664struct device_node *of_graph_get_endpoint_by_regs(
665 const struct device_node *parent, int port_reg, int reg)
666{
667 struct of_endpoint endpoint;
668 struct device_node *node = NULL;
669
670 for_each_endpoint_of_node(parent, node) {
671 of_graph_parse_endpoint(node, &endpoint);
672 if (((port_reg == -1) || (endpoint.port == port_reg)) &&
673 ((reg == -1) || (endpoint.id == reg)))
674 return node;
675 }
676
677 return NULL;
678}
679EXPORT_SYMBOL(of_graph_get_endpoint_by_regs);
680
681
682
683
684
685
686
687
688struct device_node *of_graph_get_remote_endpoint(const struct device_node *node)
689{
690
691 return of_parse_phandle(node, "remote-endpoint", 0);
692}
693EXPORT_SYMBOL(of_graph_get_remote_endpoint);
694
695
696
697
698
699
700
701
702struct device_node *of_graph_get_port_parent(struct device_node *node)
703{
704 unsigned int depth;
705
706 if (!node)
707 return NULL;
708
709
710
711
712
713 of_node_get(node);
714
715
716 for (depth = 3; depth && node; depth--) {
717 node = of_get_next_parent(node);
718 if (depth == 2 && !of_node_name_eq(node, "ports"))
719 break;
720 }
721 return node;
722}
723EXPORT_SYMBOL(of_graph_get_port_parent);
724
725
726
727
728
729
730
731
732struct device_node *of_graph_get_remote_port_parent(
733 const struct device_node *node)
734{
735 struct device_node *np, *pp;
736
737
738 np = of_graph_get_remote_endpoint(node);
739
740 pp = of_graph_get_port_parent(np);
741
742 of_node_put(np);
743
744 return pp;
745}
746EXPORT_SYMBOL(of_graph_get_remote_port_parent);
747
748
749
750
751
752
753
754
755struct device_node *of_graph_get_remote_port(const struct device_node *node)
756{
757 struct device_node *np;
758
759
760 np = of_graph_get_remote_endpoint(node);
761 if (!np)
762 return NULL;
763 return of_get_next_parent(np);
764}
765EXPORT_SYMBOL(of_graph_get_remote_port);
766
767int of_graph_get_endpoint_count(const struct device_node *np)
768{
769 struct device_node *endpoint;
770 int num = 0;
771
772 for_each_endpoint_of_node(np, endpoint)
773 num++;
774
775 return num;
776}
777EXPORT_SYMBOL(of_graph_get_endpoint_count);
778
779
780
781
782
783
784
785
786
787
788struct device_node *of_graph_get_remote_node(const struct device_node *node,
789 u32 port, u32 endpoint)
790{
791 struct device_node *endpoint_node, *remote;
792
793 endpoint_node = of_graph_get_endpoint_by_regs(node, port, endpoint);
794 if (!endpoint_node) {
795 pr_debug("no valid endpoint (%d, %d) for node %pOF\n",
796 port, endpoint, node);
797 return NULL;
798 }
799
800 remote = of_graph_get_remote_port_parent(endpoint_node);
801 of_node_put(endpoint_node);
802 if (!remote) {
803 pr_debug("no valid remote node\n");
804 return NULL;
805 }
806
807 if (!of_device_is_available(remote)) {
808 pr_debug("not available for remote node\n");
809 of_node_put(remote);
810 return NULL;
811 }
812
813 return remote;
814}
815EXPORT_SYMBOL(of_graph_get_remote_node);
816
817static struct fwnode_handle *of_fwnode_get(struct fwnode_handle *fwnode)
818{
819 return of_fwnode_handle(of_node_get(to_of_node(fwnode)));
820}
821
822static void of_fwnode_put(struct fwnode_handle *fwnode)
823{
824 of_node_put(to_of_node(fwnode));
825}
826
827static bool of_fwnode_device_is_available(const struct fwnode_handle *fwnode)
828{
829 return of_device_is_available(to_of_node(fwnode));
830}
831
832static bool of_fwnode_property_present(const struct fwnode_handle *fwnode,
833 const char *propname)
834{
835 return of_property_read_bool(to_of_node(fwnode), propname);
836}
837
838static int of_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
839 const char *propname,
840 unsigned int elem_size, void *val,
841 size_t nval)
842{
843 const struct device_node *node = to_of_node(fwnode);
844
845 if (!val)
846 return of_property_count_elems_of_size(node, propname,
847 elem_size);
848
849 switch (elem_size) {
850 case sizeof(u8):
851 return of_property_read_u8_array(node, propname, val, nval);
852 case sizeof(u16):
853 return of_property_read_u16_array(node, propname, val, nval);
854 case sizeof(u32):
855 return of_property_read_u32_array(node, propname, val, nval);
856 case sizeof(u64):
857 return of_property_read_u64_array(node, propname, val, nval);
858 }
859
860 return -ENXIO;
861}
862
863static int
864of_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
865 const char *propname, const char **val,
866 size_t nval)
867{
868 const struct device_node *node = to_of_node(fwnode);
869
870 return val ?
871 of_property_read_string_array(node, propname, val, nval) :
872 of_property_count_strings(node, propname);
873}
874
875static struct fwnode_handle *
876of_fwnode_get_parent(const struct fwnode_handle *fwnode)
877{
878 return of_fwnode_handle(of_get_parent(to_of_node(fwnode)));
879}
880
881static struct fwnode_handle *
882of_fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
883 struct fwnode_handle *child)
884{
885 return of_fwnode_handle(of_get_next_available_child(to_of_node(fwnode),
886 to_of_node(child)));
887}
888
889static struct fwnode_handle *
890of_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
891 const char *childname)
892{
893 const struct device_node *node = to_of_node(fwnode);
894 struct device_node *child;
895
896 for_each_available_child_of_node(node, child)
897 if (of_node_name_eq(child, childname))
898 return of_fwnode_handle(child);
899
900 return NULL;
901}
902
903static int
904of_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
905 const char *prop, const char *nargs_prop,
906 unsigned int nargs, unsigned int index,
907 struct fwnode_reference_args *args)
908{
909 struct of_phandle_args of_args;
910 unsigned int i;
911 int ret;
912
913 if (nargs_prop)
914 ret = of_parse_phandle_with_args(to_of_node(fwnode), prop,
915 nargs_prop, index, &of_args);
916 else
917 ret = of_parse_phandle_with_fixed_args(to_of_node(fwnode), prop,
918 nargs, index, &of_args);
919 if (ret < 0)
920 return ret;
921 if (!args)
922 return 0;
923
924 args->nargs = of_args.args_count;
925 args->fwnode = of_fwnode_handle(of_args.np);
926
927 for (i = 0; i < NR_FWNODE_REFERENCE_ARGS; i++)
928 args->args[i] = i < of_args.args_count ? of_args.args[i] : 0;
929
930 return 0;
931}
932
933static struct fwnode_handle *
934of_fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
935 struct fwnode_handle *prev)
936{
937 return of_fwnode_handle(of_graph_get_next_endpoint(to_of_node(fwnode),
938 to_of_node(prev)));
939}
940
941static struct fwnode_handle *
942of_fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
943{
944 return of_fwnode_handle(
945 of_graph_get_remote_endpoint(to_of_node(fwnode)));
946}
947
948static struct fwnode_handle *
949of_fwnode_graph_get_port_parent(struct fwnode_handle *fwnode)
950{
951 struct device_node *np;
952
953
954 np = of_get_parent(to_of_node(fwnode));
955 if (!np)
956 return NULL;
957
958
959 if (!of_node_name_eq(np, "ports"))
960 return of_fwnode_handle(np);
961
962 return of_fwnode_handle(of_get_next_parent(np));
963}
964
965static int of_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
966 struct fwnode_endpoint *endpoint)
967{
968 const struct device_node *node = to_of_node(fwnode);
969 struct device_node *port_node = of_get_parent(node);
970
971 endpoint->local_fwnode = fwnode;
972
973 of_property_read_u32(port_node, "reg", &endpoint->port);
974 of_property_read_u32(node, "reg", &endpoint->id);
975
976 of_node_put(port_node);
977
978 return 0;
979}
980
981static const void *
982of_fwnode_device_get_match_data(const struct fwnode_handle *fwnode,
983 const struct device *dev)
984{
985 return of_device_get_match_data(dev);
986}
987
988const struct fwnode_operations of_fwnode_ops = {
989 .get = of_fwnode_get,
990 .put = of_fwnode_put,
991 .device_is_available = of_fwnode_device_is_available,
992 .device_get_match_data = of_fwnode_device_get_match_data,
993 .property_present = of_fwnode_property_present,
994 .property_read_int_array = of_fwnode_property_read_int_array,
995 .property_read_string_array = of_fwnode_property_read_string_array,
996 .get_parent = of_fwnode_get_parent,
997 .get_next_child_node = of_fwnode_get_next_child_node,
998 .get_named_child_node = of_fwnode_get_named_child_node,
999 .get_reference_args = of_fwnode_get_reference_args,
1000 .graph_get_next_endpoint = of_fwnode_graph_get_next_endpoint,
1001 .graph_get_remote_endpoint = of_fwnode_graph_get_remote_endpoint,
1002 .graph_get_port_parent = of_fwnode_graph_get_port_parent,
1003 .graph_parse_endpoint = of_fwnode_graph_parse_endpoint,
1004};
1005EXPORT_SYMBOL_GPL(of_fwnode_ops);
1006