forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkconfigfunctions.py
1093 lines (886 loc) · 35 KB
/
kconfigfunctions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 2018-2019 Linaro
# Copyright (c) 2019 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: Apache-2.0
import functools
import inspect
import operator
import os
import pickle
import re
import sys
from pathlib import Path
ZEPHYR_BASE = str(Path(__file__).resolve().parents[2])
sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts", "dts",
"python-devicetree", "src"))
# Types we support
# 'string', 'int', 'hex', 'bool'
doc_mode = os.environ.get('KCONFIG_DOC_MODE') == "1"
if not doc_mode:
EDT_PICKLE = os.environ.get("EDT_PICKLE")
# The "if" handles a missing dts.
if EDT_PICKLE is not None and os.path.isfile(EDT_PICKLE):
with open(EDT_PICKLE, 'rb') as f:
edt = pickle.load(f)
edtlib = inspect.getmodule(edt)
else:
edt = None
edtlib = None
def _warn(kconf, msg):
print("{}:{}: WARNING: {}".format(kconf.filename, kconf.linenr, msg))
def _dt_units_to_scale(unit):
if not unit:
return 0
if unit in {'k', 'K'}:
return 10
if unit in {'m', 'M'}:
return 20
if unit in {'g', 'G'}:
return 30
if unit in {'kb', 'Kb'}:
return 13
if unit in {'mb', 'Mb'}:
return 23
if unit in {'gb', 'Gb'}:
return 33
def dt_chosen_label(kconf, _, chosen):
"""
This function takes a 'chosen' property and treats that property as a path
to an EDT node. If it finds an EDT node, it will look to see if that node
has a "label" property and return the value of that "label". If not, we
return the node's name in the devicetree.
"""
if doc_mode or edt is None:
return ""
node = edt.chosen_node(chosen)
if not node:
return ""
if "label" not in node.props:
return node.name
return node.props["label"].val
def dt_chosen_enabled(kconf, _, chosen):
"""
This function returns "y" if /chosen contains a property named 'chosen'
that points to an enabled node, and "n" otherwise
"""
if doc_mode or edt is None:
return "n"
node = edt.chosen_node(chosen)
return "y" if node and node.status == "okay" else "n"
def dt_chosen_path(kconf, _, chosen):
"""
This function takes a /chosen node property and returns the path
to the node in the property value, or the empty string.
"""
if doc_mode or edt is None:
return "n"
node = edt.chosen_node(chosen)
return node.path if node else ""
def dt_chosen_has_compat(kconf, _, chosen, compat):
"""
This function takes a /chosen node property and returns 'y' if the
chosen node has the provided compatible string 'compat'
"""
if doc_mode or edt is None:
return "n"
node = edt.chosen_node(chosen)
if node is None:
return "n"
if compat in node.compats:
return "y"
return "n"
def dt_node_enabled(kconf, name, node):
"""
This function is used to test if a node is enabled (has status
'okay') or not.
The 'node' argument is a string which is either a path or an
alias, or both, depending on 'name'.
If 'name' is 'dt_path_enabled', 'node' is an alias or a path. If
'name' is 'dt_alias_enabled, 'node' is an alias.
"""
if doc_mode or edt is None:
return "n"
if name == "dt_alias_enabled":
if node.startswith("/"):
# EDT.get_node() works with either aliases or paths. If we
# are specifically being asked about an alias, reject paths.
return "n"
else:
# Make sure this is being called appropriately.
assert name == "dt_path_enabled"
try:
node = edt.get_node(node)
except edtlib.EDTError:
return "n"
return "y" if node and node.status == "okay" else "n"
def dt_nodelabel_exists(kconf, _, label):
"""
This function returns "y" if a nodelabel exists and "n" otherwise.
"""
if doc_mode or edt is None:
return "n"
node = edt.label2node.get(label)
return "y" if node else "n"
def dt_nodelabel_enabled(kconf, _, label):
"""
This function is like dt_node_enabled(), but the 'label' argument
should be a node label, like "foo" is here:
foo: some-node { ... };
"""
if doc_mode or edt is None:
return "n"
node = edt.label2node.get(label)
return "y" if node and node.status == "okay" else "n"
def _node_reg_addr(node, index, unit):
if not node:
return 0
if not node.regs:
return 0
if int(index) >= len(node.regs):
return 0
if node.regs[int(index)].addr is None:
return 0
return node.regs[int(index)].addr >> _dt_units_to_scale(unit)
def _node_reg_size(node, index, unit):
if not node:
return 0
if not node.regs:
return 0
if int(index) >= len(node.regs):
return 0
if node.regs[int(index)].size is None:
return 0
return node.regs[int(index)].size >> _dt_units_to_scale(unit)
def _node_int_prop(node, prop, unit=None):
"""
This function takes a 'node' and will look to see if that 'node' has a
property called 'prop' and if that 'prop' is an integer type will return
the value of the property 'prop' as either a string int or string hex
value, if not we return 0.
The function will divide the value based on 'unit':
None No division
'k' or 'K' divide by 1024 (1 << 10)
'm' or 'M' divide by 1,048,576 (1 << 20)
'g' or 'G' divide by 1,073,741,824 (1 << 30)
'kb' or 'Kb' divide by 8192 (1 << 13)
'mb' or 'Mb' divide by 8,388,608 (1 << 23)
'gb' or 'Gb' divide by 8,589,934,592 (1 << 33)
"""
if not node:
return 0
if prop not in node.props:
return 0
if node.props[prop].type != "int":
return 0
return node.props[prop].val >> _dt_units_to_scale(unit)
def _node_array_prop(node, prop, index=0, unit=None):
"""
This function takes a 'node' and will look to see if that 'node' has a
property called 'prop' and if that 'prop' is an array type will return
the value of the property 'prop' at the given 'index' as either a string int
or string hex value. If the property 'prop' is not found or the given 'index'
is out of range it will return 0.
The function will divide the value based on 'unit':
None No division
'k' or 'K' divide by 1024 (1 << 10)
'm' or 'M' divide by 1,048,576 (1 << 20)
'g' or 'G' divide by 1,073,741,824 (1 << 30)
"""
if not node:
return 0
if prop not in node.props:
return 0
if node.props[prop].type != "array":
return 0
if int(index) >= len(node.props[prop].val):
return 0
return node.props[prop].val[int(index)] >> _dt_units_to_scale(unit)
def _node_ph_array_prop(node, prop, index, cell, unit=None):
"""
This function takes a 'node', a property name ('prop'), index ('index') and
a cell ('cell') and it will look to see if that node has a property
called 'prop' and if that 'prop' is an phandle-array type.
Then it will check if that phandle array has a cell matching the given index
and then return the value of the cell named 'cell' in this array index.
If not found it will return 0.
The function will divide the value based on 'unit':
None No division
'k' or 'K' divide by 1024 (1 << 10)
'm' or 'M' divide by 1,048,576 (1 << 20)
'g' or 'G' divide by 1,073,741,824 (1 << 30)
"""
if not node:
return 0
if prop not in node.props:
return 0
if node.props[prop].type != "phandle-array":
return 0
if int(index) >= len(node.props[prop].val):
return 0
if cell not in node.props[prop].val[int(index)].data.keys():
return 0
return node.props[prop].val[int(index)].data[cell] >> _dt_units_to_scale(unit)
def _dt_chosen_reg_addr(kconf, chosen, index=0, unit=None):
"""
This function takes a 'chosen' property and treats that property as a path
to an EDT node. If it finds an EDT node, it will look to see if that
node has a register at the given 'index' and return the address value of
that reg, if not we return 0.
The function will divide the value based on 'unit':
None No division
'k' or 'K' divide by 1024 (1 << 10)
'm' or 'M' divide by 1,048,576 (1 << 20)
'g' or 'G' divide by 1,073,741,824 (1 << 30)
'kb' or 'Kb' divide by 8192 (1 << 13)
'mb' or 'Mb' divide by 8,388,608 (1 << 23)
'gb' or 'Gb' divide by 8,589,934,592 (1 << 33)
"""
if doc_mode or edt is None:
return 0
node = edt.chosen_node(chosen)
return _node_reg_addr(node, index, unit)
def _dt_chosen_reg_size(kconf, chosen, index=0, unit=None):
"""
This function takes a 'chosen' property and treats that property as a path
to an EDT node. If it finds an EDT node, it will look to see if that node
has a register at the given 'index' and return the size value of that reg,
if not we return 0.
The function will divide the value based on 'unit':
None No division
'k' or 'K' divide by 1024 (1 << 10)
'm' or 'M' divide by 1,048,576 (1 << 20)
'g' or 'G' divide by 1,073,741,824 (1 << 30)
'kb' or 'Kb' divide by 8192 (1 << 13)
'mb' or 'Mb' divide by 8,388,608 (1 << 23)
'gb' or 'Gb' divide by 8,589,934,592 (1 << 33)
"""
if doc_mode or edt is None:
return 0
node = edt.chosen_node(chosen)
return _node_reg_size(node, index, unit)
def dt_chosen_reg(kconf, name, chosen, index=0, unit=None):
"""
This function just routes to the proper function and converts
the result to either a string int or string hex value.
"""
if name == "dt_chosen_reg_size_int":
return str(_dt_chosen_reg_size(kconf, chosen, index, unit))
if name == "dt_chosen_reg_size_hex":
return hex(_dt_chosen_reg_size(kconf, chosen, index, unit))
if name == "dt_chosen_reg_addr_int":
return str(_dt_chosen_reg_addr(kconf, chosen, index, unit))
if name == "dt_chosen_reg_addr_hex":
return hex(_dt_chosen_reg_addr(kconf, chosen, index, unit))
def _dt_chosen_partition_addr(kconf, chosen, index=0, unit=None):
"""
This function takes a 'chosen' property and treats that property as a path
to an EDT node. If it finds an EDT node, it will look to see if that
node has a register, and if that node has a grandparent that has a register
at the given 'index'. The addition of both addresses will be returned, if
not, we return 0.
The function will divide the value based on 'unit':
None No division
'k' or 'K' divide by 1024 (1 << 10)
'm' or 'M' divide by 1,048,576 (1 << 20)
'g' or 'G' divide by 1,073,741,824 (1 << 30)
'kb' or 'Kb' divide by 8192 (1 << 13)
'mb' or 'Mb' divide by 8,388,608 (1 << 23)
'gb' or 'Gb' divide by 8,589,934,592 (1 << 33)
"""
if doc_mode or edt is None:
return 0
node = edt.chosen_node(chosen)
if not node:
return 0
p_node = node.parent
if not p_node:
return 0
return _node_reg_addr(p_node.parent, index, unit) + _node_reg_addr(node, 0, unit)
def dt_chosen_partition_addr(kconf, name, chosen, index=0, unit=None):
"""
This function just routes to the proper function and converts
the result to either a string int or string hex value.
"""
if name == "dt_chosen_partition_addr_int":
return str(_dt_chosen_partition_addr(kconf, chosen, index, unit))
if name == "dt_chosen_partition_addr_hex":
return hex(_dt_chosen_partition_addr(kconf, chosen, index, unit))
def _dt_node_reg_addr(kconf, path, index=0, unit=None):
"""
This function takes a 'path' and looks for an EDT node at that path. If it
finds an EDT node, it will look to see if that node has a register at the
given 'index' and return the address value of that reg, if not we return 0.
The function will divide the value based on 'unit':
None No division
'k' or 'K' divide by 1024 (1 << 10)
'm' or 'M' divide by 1,048,576 (1 << 20)
'g' or 'G' divide by 1,073,741,824 (1 << 30)
'kb' or 'Kb' divide by 8192 (1 << 13)
'mb' or 'Mb' divide by 8,388,608 (1 << 23)
'gb' or 'Gb' divide by 8,589,934,592 (1 << 33)
"""
if doc_mode or edt is None:
return 0
try:
node = edt.get_node(path)
except edtlib.EDTError:
return 0
return _node_reg_addr(node, index, unit)
def _dt_node_reg_size(kconf, path, index=0, unit=None):
"""
This function takes a 'path' and looks for an EDT node at that path. If it
finds an EDT node, it will look to see if that node has a register at the
given 'index' and return the size value of that reg, if not we return 0.
The function will divide the value based on 'unit':
None No division
'k' or 'K' divide by 1024 (1 << 10)
'm' or 'M' divide by 1,048,576 (1 << 20)
'g' or 'G' divide by 1,073,741,824 (1 << 30)
'kb' or 'Kb' divide by 8192 (1 << 13)
'mb' or 'Mb' divide by 8,388,608 (1 << 23)
'gb' or 'Gb' divide by 8,589,934,592 (1 << 33)
"""
if doc_mode or edt is None:
return 0
try:
node = edt.get_node(path)
except edtlib.EDTError:
return 0
return _node_reg_size(node, index, unit)
def dt_node_reg(kconf, name, path, index=0, unit=None):
"""
This function just routes to the proper function and converts
the result to either a string int or string hex value.
"""
if name == "dt_node_reg_size_int":
return str(_dt_node_reg_size(kconf, path, index, unit))
if name == "dt_node_reg_size_hex":
return hex(_dt_node_reg_size(kconf, path, index, unit))
if name == "dt_node_reg_addr_int":
return str(_dt_node_reg_addr(kconf, path, index, unit))
if name == "dt_node_reg_addr_hex":
return hex(_dt_node_reg_addr(kconf, path, index, unit))
def dt_nodelabel_reg(kconf, name, label, index=0, unit=None):
"""
This function is like dt_node_reg(), but the 'label' argument
should be a node label, like "foo" is here:
foo: some-node { ... };
"""
if doc_mode or edt is None:
node = None
else:
node = edt.label2node.get(label)
if name == "dt_nodelabel_reg_size_int":
return str(_dt_node_reg_size(kconf, node.path, index, unit)) if node else "0"
if name == "dt_nodelabel_reg_size_hex":
return hex(_dt_node_reg_size(kconf, node.path, index, unit)) if node else "0x0"
if name == "dt_nodelabel_reg_addr_int":
return str(_dt_node_reg_addr(kconf, node.path, index, unit)) if node else "0"
if name == "dt_nodelabel_reg_addr_hex":
return hex(_dt_node_reg_addr(kconf, node.path, index, unit)) if node else "0x0"
def _dt_node_bool_prop_generic(node_search_function, search_arg, prop):
"""
This function takes the 'node_search_function' and uses it to search for
a node with 'search_arg' and if node exists, checks if 'prop' exists
inside the node and is a boolean, if it is true, returns "y".
Otherwise, it returns "n".
"""
try:
node = node_search_function(search_arg)
except edtlib.EDTError:
return "n"
if node is None:
return "n"
if prop not in node.props:
return "n"
if node.props[prop].type != "boolean":
return "n"
if node.props[prop].val:
return "y"
return "n"
def dt_node_bool_prop(kconf, _, path, prop):
"""
This function takes a 'path' and looks for an EDT node at that path. If it
finds an EDT node, it will look to see if that node has a boolean property
by the name of 'prop'. If the 'prop' exists it will return "y" otherwise
we return "n".
"""
if doc_mode or edt is None:
return "n"
return _dt_node_bool_prop_generic(edt.get_node, path, prop)
def dt_nodelabel_bool_prop(kconf, _, label, prop):
"""
This function takes a 'label' and looks for an EDT node with that label.
If it finds an EDT node, it will look to see if that node has a boolean
property by the name of 'prop'. If the 'prop' exists it will return "y"
otherwise we return "n".
"""
if doc_mode or edt is None:
return "n"
return _dt_node_bool_prop_generic(edt.label2node.get, label, prop)
def dt_chosen_bool_prop(kconf, _, chosen, prop):
"""
This function takes a /chosen node property named 'chosen', and
looks for the chosen node. If that node exists and has a boolean
property 'prop', it returns "y". Otherwise, it returns "n".
"""
if doc_mode or edt is None:
return "n"
return _dt_node_bool_prop_generic(edt.chosen_node, chosen, prop)
def _dt_node_has_prop_generic(node_search_function, search_arg, prop):
"""
This function takes the 'node_search_function' and uses it to search for
a node with 'search_arg' and if node exists, then checks if 'prop'
exists inside the node and returns "y". Otherwise, it returns "n".
"""
try:
node = node_search_function(search_arg)
except edtlib.EDTError:
return "n"
if node is None:
return "n"
if prop in node.props:
return "y"
return "n"
def dt_node_has_prop(kconf, _, path, prop):
"""
This function takes a 'path' and looks for an EDT node at that path. If it
finds an EDT node, it will look to see if that node has a property
by the name of 'prop'. If the 'prop' exists it will return "y" otherwise
it returns "n".
"""
if doc_mode or edt is None:
return "n"
return _dt_node_has_prop_generic(edt.get_node, path, prop)
def dt_nodelabel_has_prop(kconf, _, label, prop):
"""
This function takes a 'label' and looks for an EDT node with that label.
If it finds an EDT node, it will look to see if that node has a property
by the name of 'prop'. If the 'prop' exists it will return "y" otherwise
it returns "n".
"""
if doc_mode or edt is None:
return "n"
return _dt_node_has_prop_generic(edt.label2node.get, label, prop)
def dt_node_int_prop(kconf, name, path, prop, unit=None):
"""
This function takes a 'path' and property name ('prop') looks for an EDT
node at that path. If it finds an EDT node, it will look to see if that
node has a property called 'prop' and if that 'prop' is an integer type
will return the value of the property 'prop' as either a string int or
string hex value, if not we return 0.
The function will divide the value based on 'unit':
None No division
'k' or 'K' divide by 1024 (1 << 10)
'm' or 'M' divide by 1,048,576 (1 << 20)
'g' or 'G' divide by 1,073,741,824 (1 << 30)
'kb' or 'Kb' divide by 8192 (1 << 13)
'mb' or 'Mb' divide by 8,388,608 (1 << 23)
'gb' or 'Gb' divide by 8,589,934,592 (1 << 33)
"""
if doc_mode or edt is None:
return "0"
try:
node = edt.get_node(path)
except edtlib.EDTError:
return "0"
if name == "dt_node_int_prop_int":
return str(_node_int_prop(node, prop, unit))
if name == "dt_node_int_prop_hex":
return hex(_node_int_prop(node, prop, unit))
def dt_node_array_prop(kconf, name, path, prop, index, unit=None):
"""
This function takes a 'path', property name ('prop') and index ('index')
and looks for an EDT node at that path. If it finds an EDT node, it will
look to see if that node has a property called 'prop' and if that 'prop'
is an array type will return the value of the property 'prop' at the given
'index' as either a string int or string hex value. If not found we return 0.
The function will divide the value based on 'unit':
None No division
'k' or 'K' divide by 1024 (1 << 10)
'm' or 'M' divide by 1,048,576 (1 << 20)
'g' or 'G' divide by 1,073,741,824 (1 << 30)
"""
if doc_mode or edt is None:
return "0"
try:
node = edt.get_node(path)
except edtlib.EDTError:
return "0"
if name == "dt_node_array_prop_int":
return str(_node_array_prop(node, prop, index, unit))
if name == "dt_node_array_prop_hex":
return hex(_node_array_prop(node, prop, index, unit))
def dt_node_ph_array_prop(kconf, name, path, prop, index, cell, unit=None):
"""
This function takes a 'path', property name ('prop'), index ('index') and
a cell ('cell') and looks for an EDT node at that path.
If it finds an EDT node, it will look to see if that node has a property
called 'prop' and if that 'prop' is an phandle-array type.
Then it will check if that phandle array has a cell matching the given index
and ten return the value of the cell named 'cell' in this array index as
either a string int or string hex value. If not found we return 0.
The function will divide the value based on 'unit':
None No division
'k' or 'K' divide by 1024 (1 << 10)
'm' or 'M' divide by 1,048,576 (1 << 20)
'g' or 'G' divide by 1,073,741,824 (1 << 30)
"""
if doc_mode or edt is None:
return "0"
try:
node = edt.get_node(path)
except edtlib.EDTError:
return "0"
if name == "dt_node_ph_array_prop_int":
return str(_node_ph_array_prop(node, prop, index, cell, unit))
if name == "dt_node_ph_array_prop_hex":
return hex(_node_ph_array_prop(node, prop, index, cell, unit))
def dt_node_ph_prop_path(kconf, name, path, prop):
"""
This function takes a 'path' and a property name ('prop') and
looks for an EDT node at that path. If it finds an EDT node,
it will look to see if that node has a property called 'prop'
and if that 'prop' is an phandle type. Then it will return the
path to the pointed-to node, or an empty string if there is
no such node.
"""
if doc_mode or edt is None:
return ""
try:
node = edt.get_node(path)
except edtlib.EDTError:
return ""
if prop not in node.props:
return ""
if node.props[prop].type != "phandle":
return ""
phandle = node.props[prop].val
return phandle.path if phandle else ""
def dt_node_str_prop_equals(kconf, _, path, prop, val):
"""
This function takes a 'path' and property name ('prop') looks for an EDT
node at that path. If it finds an EDT node, it will look to see if that
node has a property 'prop' of type string. If that 'prop' is equal to 'val'
it will return "y" otherwise return "n".
"""
if doc_mode or edt is None:
return "n"
try:
node = edt.get_node(path)
except edtlib.EDTError:
return "n"
if prop not in node.props:
return "n"
if node.props[prop].type != "string":
return "n"
if node.props[prop].val == val:
return "y"
return "n"
def dt_has_compat(kconf, _, compat):
"""
This function takes a 'compat' and returns "y" if any compatible node
can be found in the EDT, otherwise it returns "n".
"""
if doc_mode or edt is None:
return "n"
return "y" if compat in edt.compat2nodes else "n"
def dt_compat_enabled(kconf, _, compat):
"""
This function takes a 'compat' and returns "y" if we find a status "okay"
compatible node in the EDT otherwise we return "n"
"""
if doc_mode or edt is None:
return "n"
return "y" if compat in edt.compat2okay else "n"
def dt_compat_on_bus(kconf, _, compat, bus):
"""
This function takes a 'compat' and returns "y" if we find an enabled
compatible node in the EDT which is on bus 'bus'. It returns "n" otherwise.
"""
if doc_mode or edt is None:
return "n"
if compat in edt.compat2okay:
for node in edt.compat2okay[compat]:
if node.on_buses is not None and bus in node.on_buses:
return "y"
return "n"
def dt_compat_any_has_prop(kconf, _, compat, prop, value=None):
"""
This function takes a 'compat', a 'prop', and a 'value'.
If value=None, the function returns "y" if any
enabled node with compatible 'compat' also has a valid property 'prop'.
If value is given, the function returns "y" if any enabled node with compatible 'compat'
also has a valid property 'prop' with value 'value'.
It returns "n" otherwise.
"""
if doc_mode or edt is None:
return "n"
if compat in edt.compat2okay:
for node in edt.compat2okay[compat]:
if prop in node.props:
if value is None:
return "y"
elif str(node.props[prop].val) == value:
return "y"
return "n"
def dt_compat_any_not_has_prop(kconf, _, compat, prop):
"""
This function takes a 'compat', and a 'prop'.
The function returns "y" if any enabled node with compatible 'compat'
does NOT contain the property 'prop'.
It returns "n" otherwise.
"""
if doc_mode or edt is None:
return "n"
if compat in edt.compat2okay:
for node in edt.compat2okay[compat]:
if prop not in node.props:
return "y"
return "n"
def dt_nodelabel_has_compat(kconf, _, label, compat):
"""
This function takes a 'label' and looks for an EDT node with that label.
If it finds such node, it returns "y" if this node is compatible with
the provided 'compat'. Otherwise, it return "n" .
"""
if doc_mode or edt is None:
return "n"
node = edt.label2node.get(label)
if node and compat in node.compats:
return "y"
return "n"
def dt_node_has_compat(kconf, _, path, compat):
"""
This function takes a 'path' and looks for an EDT node at that path. If it
finds an EDT node, it returns "y" if this node is compatible with
the provided 'compat'. Otherwise, it return "n" .
"""
if doc_mode or edt is None:
return "n"
try:
node = edt.get_node(path)
except edtlib.EDTError:
return "n"
if node and compat in node.compats:
return "y"
return "n"
def dt_nodelabel_enabled_with_compat(kconf, _, label, compat):
"""
This function takes a 'label' and returns "y" if an enabled node with
such label can be found in the EDT and that node is compatible with the
provided 'compat', otherwise it returns "n".
"""
if doc_mode or edt is None:
return "n"
if compat in edt.compat2okay:
for node in edt.compat2okay[compat]:
if label in node.labels:
return "y"
return "n"
def dt_nodelabel_array_prop_has_val(kconf, _, label, prop, val):
"""
This function looks for a node with node label 'label'.
If the node exists, it checks if the node node has a property
'prop' with type "array". If so, and the property contains
an element equal to the integer 'val', it returns "y".
Otherwise, it returns "n".
"""
if doc_mode or edt is None:
return "n"
node = edt.label2node.get(label)
if not node or (prop not in node.props) or (node.props[prop].type != "array"):
return "n"
else:
return "y" if int(val, base=0) in node.props[prop].val else "n"
def dt_nodelabel_path(kconf, _, label):
"""
This function takes a node label (not a label property) and
returns the path to the node which has that label, or an empty
string if there is no such node.
"""
if doc_mode or edt is None:
return ""
node = edt.label2node.get(label)
return node.path if node else ""
def dt_node_parent(kconf, _, path):
"""
This function takes a 'path' and looks for an EDT node at that path. If it
finds an EDT node, it will look for the parent of that node. If the parent
exists, it will return the path to that parent. Otherwise, an empty string
will be returned.
"""
if doc_mode or edt is None:
return ""
try:
node = edt.get_node(path)
except edtlib.EDTError:
return ""
if node is None:
return ""
return node.parent.path if node.parent else ""
def dt_gpio_hogs_enabled(kconf, _):
"""
Return "y" if any GPIO hog node is enabled. Otherwise, return "n".
"""
if doc_mode or edt is None:
return "n"
for node in edt.nodes:
if node.gpio_hogs and node.status == "okay":
return "y"
return "n"
def normalize_upper(kconf, _, string):
"""
Normalize the string, so that the string only contains alpha-numeric
characters or underscores. All non-alpha-numeric characters are replaced
with an underscore, '_'.
When string has been normalized it will be converted into upper case.
"""
return re.sub(r'[^a-zA-Z0-9_]', '_', string).upper()
def shields_list_contains(kconf, _, shield):
"""
Return "n" if cmake environment variable 'SHIELD_AS_LIST' doesn't exist.
Return "y" if 'shield' is present list obtained after 'SHIELD_AS_LIST'
has been split using ";" as a separator and "n" otherwise.
"""
try:
list = os.environ['SHIELD_AS_LIST']
except KeyError:
return "n"
return "y" if shield in list.split(";") else "n"
def substring(kconf, _, string, start, stop=None):
"""
Extracts a portion of the string, removing characters from the front, back or both.
"""
if stop is not None:
return string[int(start):int(stop)]
else:
return string[int(start):]
def arith(kconf, name, *args):
"""
The arithmetic operations on integers.
If three or more arguments are given, it returns the result of performing
the operation on the first two arguments and operates the same operation as
the result and the following argument.
For interoperability with inc and dec,
if there is only one argument, it will be split with a comma and processed
as a sequence of numbers.
Examples in Kconfig:
$(add, 10, 3) # -> 13
$(add, 10, 3, 2) # -> 15
$(sub, 10, 3) # -> 7
$(sub, 10, 3, 2) # -> 5
$(mul, 10, 3) # -> 30
$(mul, 10, 3, 2) # -> 60
$(div, 10, 3) # -> 3
$(div, 10, 3, 2) # -> 1
$(mod, 10, 3) # -> 1
$(mod, 10, 3, 2) # -> 1
$(inc, 1) # -> 2
$(inc, 1, 1) # -> "2,2"
$(inc, $(inc, 1, 1)) # -> "3,3"
$(dec, 1) # -> 0
$(dec, 1, 1) # -> "0,0"
$(dec, $(dec, 1, 1)) # -> "-1,-1"
$(add, $(inc, 1, 1)) # -> 4
$(div, $(dec, 1, 1)) # Error (0 div 0)
"""
intarray = map(int, args if len(args) > 1 else args[0].split(","))
if name == "add":
return str(int(functools.reduce(operator.add, intarray)))
elif name == "sub":
return str(int(functools.reduce(operator.sub, intarray)))
elif name == "mul":
return str(int(functools.reduce(operator.mul, intarray)))
elif name == "div":
return str(int(functools.reduce(operator.truediv, intarray)))
elif name == "mod":
return str(int(functools.reduce(operator.mod, intarray)))
elif name == "max":