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_cmp(port->name, "port") != 0)
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_cmp(port->name, "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_cmp(node->name, "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 return NULL;
810 }
811
812 return remote;
813}
814EXPORT_SYMBOL(of_graph_get_remote_node);
815
816static struct fwnode_handle *of_fwnode_get(struct fwnode_handle *fwnode)
817{
818 return of_fwnode_handle(of_node_get(to_of_node(fwnode)));
819}
820
821static void of_fwnode_put(struct fwnode_handle *fwnode)
822{
823 of_node_put(to_of_node(fwnode));
824}
825
826static bool of_fwnode_device_is_available(const struct fwnode_handle *fwnode)
827{
828 return of_device_is_available(to_of_node(fwnode));
829}
830
831static bool of_fwnode_property_present(const struct fwnode_handle *fwnode,
832 const char *propname)
833{
834 return of_property_read_bool(to_of_node(fwnode), propname);
835}
836
837static int of_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
838 const char *propname,
839 unsigned int elem_size, void *val,
840 size_t nval)
841{
842 const struct device_node *node = to_of_node(fwnode);
843
844 if (!val)
845 return of_property_count_elems_of_size(node, propname,
846 elem_size);
847
848 switch (elem_size) {
849 case sizeof(u8):
850 return of_property_read_u8_array(node, propname, val, nval);
851 case sizeof(u16):
852 return of_property_read_u16_array(node, propname, val, nval);
853 case sizeof(u32):
854 return of_property_read_u32_array(node, propname, val, nval);
855 case sizeof(u64):
856 return of_property_read_u64_array(node, propname, val, nval);
857 }
858
859 return -ENXIO;
860}
861
862static int
863of_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
864 const char *propname, const char **val,
865 size_t nval)
866{
867 const struct device_node *node = to_of_node(fwnode);
868
869 return val ?
870 of_property_read_string_array(node, propname, val, nval) :
871 of_property_count_strings(node, propname);
872}
873
874static struct fwnode_handle *
875of_fwnode_get_parent(const struct fwnode_handle *fwnode)
876{
877 return of_fwnode_handle(of_get_parent(to_of_node(fwnode)));
878}
879
880static struct fwnode_handle *
881of_fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
882 struct fwnode_handle *child)
883{
884 return of_fwnode_handle(of_get_next_available_child(to_of_node(fwnode),
885 to_of_node(child)));
886}
887
888static struct fwnode_handle *
889of_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
890 const char *childname)
891{
892 const struct device_node *node = to_of_node(fwnode);
893 struct device_node *child;
894
895 for_each_available_child_of_node(node, child)
896 if (!of_node_cmp(child->name, childname))
897 return of_fwnode_handle(child);
898
899 return NULL;
900}
901
902static int
903of_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
904 const char *prop, const char *nargs_prop,
905 unsigned int nargs, unsigned int index,
906 struct fwnode_reference_args *args)
907{
908 struct of_phandle_args of_args;
909 unsigned int i;
910 int ret;
911
912 if (nargs_prop)
913 ret = of_parse_phandle_with_args(to_of_node(fwnode), prop,
914 nargs_prop, index, &of_args);
915 else
916 ret = of_parse_phandle_with_fixed_args(to_of_node(fwnode), prop,
917 nargs, index, &of_args);
918 if (ret < 0)
919 return ret;
920 if (!args)
921 return 0;
922
923 args->nargs = of_args.args_count;
924 args->fwnode = of_fwnode_handle(of_args.np);
925
926 for (i = 0; i < NR_FWNODE_REFERENCE_ARGS; i++)
927 args->args[i] = i < of_args.args_count ? of_args.args[i] : 0;
928
929 return 0;
930}
931
932static struct fwnode_handle *
933of_fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
934 struct fwnode_handle *prev)
935{
936 return of_fwnode_handle(of_graph_get_next_endpoint(to_of_node(fwnode),
937 to_of_node(prev)));
938}
939
940static struct fwnode_handle *
941of_fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
942{
943 return of_fwnode_handle(
944 of_graph_get_remote_endpoint(to_of_node(fwnode)));
945}
946
947static struct fwnode_handle *
948of_fwnode_graph_get_port_parent(struct fwnode_handle *fwnode)
949{
950 struct device_node *np;
951
952
953 np = of_get_parent(to_of_node(fwnode));
954 if (!np)
955 return NULL;
956
957
958 if (of_node_cmp(np->name, "ports"))
959 return of_fwnode_handle(np);
960
961 return of_fwnode_handle(of_get_next_parent(np));
962}
963
964static int of_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
965 struct fwnode_endpoint *endpoint)
966{
967 const struct device_node *node = to_of_node(fwnode);
968 struct device_node *port_node = of_get_parent(node);
969
970 endpoint->local_fwnode = fwnode;
971
972 of_property_read_u32(port_node, "reg", &endpoint->port);
973 of_property_read_u32(node, "reg", &endpoint->id);
974
975 of_node_put(port_node);
976
977 return 0;
978}
979
980static const void *
981of_fwnode_device_get_match_data(const struct fwnode_handle *fwnode,
982 const struct device *dev)
983{
984 return of_device_get_match_data(dev);
985}
986
987const struct fwnode_operations of_fwnode_ops = {
988 .get = of_fwnode_get,
989 .put = of_fwnode_put,
990 .device_is_available = of_fwnode_device_is_available,
991 .device_get_match_data = of_fwnode_device_get_match_data,
992 .property_present = of_fwnode_property_present,
993 .property_read_int_array = of_fwnode_property_read_int_array,
994 .property_read_string_array = of_fwnode_property_read_string_array,
995 .get_parent = of_fwnode_get_parent,
996 .get_next_child_node = of_fwnode_get_next_child_node,
997 .get_named_child_node = of_fwnode_get_named_child_node,
998 .get_reference_args = of_fwnode_get_reference_args,
999 .graph_get_next_endpoint = of_fwnode_graph_get_next_endpoint,
1000 .graph_get_remote_endpoint = of_fwnode_graph_get_remote_endpoint,
1001 .graph_get_port_parent = of_fwnode_graph_get_port_parent,
1002 .graph_parse_endpoint = of_fwnode_graph_parse_endpoint,
1003};
1004EXPORT_SYMBOL_GPL(of_fwnode_ops);
1005