Skip to content

Commit bd434e1

Browse files
scripts: rt_cfg_utils: Generate SOC level pinctrl definition list
Generate soc level pinctrl DTSI with all pinmux options, and use those phandles in pin groups Signed-off-by: Daniel DeGrasse <[email protected]>
1 parent cda5917 commit bd434e1

File tree

2 files changed

+51
-41
lines changed

2 files changed

+51
-41
lines changed

tools/rt_cfg_utils.py

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -523,21 +523,22 @@ def write_pinctrl_nodes(self, outputfile):
523523
soc_dtsi.write("};\n\n")
524524
soc_dtsi.close()
525525

526-
def write_pinctrl_header(self, outfile):
526+
def write_pinctrl_defs(self, outputfile):
527527
"""
528-
Write pinctrl header to disk in a format suitable for inclusion into
529-
DTSI file
530-
@param outfile: output file destination
528+
Writes a pinctrl dtsi file that defines all pinmux options. The board
529+
level pin groups will include the pinctrl definitions here, and define
530+
the properties to be set on each pin.
531+
@param outputfile file to write pinctrl dtsi file to
531532
"""
532533
try:
533-
soc_header = open(outfile, "w", encoding='utf8')
534+
soc_dtsi = open(outputfile, "w", encoding='utf8')
534535
except IOError:
535-
print(f"Could not write to file {outfile}")
536+
print(f"Could not write to file {outputfile}")
536537
return
537538
# Start by writing header
538-
soc_header.write(self._header)
539+
soc_dtsi.write(self._header)
539540
# Write documentation block
540-
soc_header.write("/*\n"
541+
soc_dtsi.write("/*\n"
541542
" * SOC level pinctrl defintions\n"
542543
" * These definitions define SOC level defaults for each pin,\n"
543544
" * and select the pinmux for the pin. Pinmux entries are a tuple of:\n"
@@ -547,25 +548,25 @@ def write_pinctrl_header(self, outfile):
547548
" * each register, respectively. The config_register is used to configure\n"
548549
" * the pin based on the devicetree properties set\n"
549550
" */\n\n")
551+
soc_dtsi.write("&iomuxc {\n")
550552
for pin in sorted(self._signal_map.values()):
551553
for iomux_opt in sorted(pin.get_mux_options()):
552-
header_block = ""
553554
# Get iomuxc constant values
554555
iomuxc_name = iomux_opt.get_name()
555556
register = iomux_opt.get_mux_reg()
556-
header_block += f"#define {iomuxc_name}_MUX 0x{register:x}\n"
557557
mode = iomux_opt.get_mux_val()
558-
header_block += f"#define {iomuxc_name}_MODE {mode:d}\n"
559558
input_reg = iomux_opt.get_daisy_reg()
560-
header_block += f"#define {iomuxc_name}_INPUT 0x{input_reg:x}\n"
561559
input_daisy = iomux_opt.get_daisy_val()
562-
header_block += f"#define {iomuxc_name}_DAISY {input_daisy:d}\n"
563560
config_reg = iomux_opt.get_cfg_reg()
564-
header_block += f"#define {iomuxc_name}_CFG 0x{config_reg:x}\n"
565-
soc_header.write(header_block)
566-
soc_header.write("\n")
567-
soc_header.close()
568-
561+
# build DTS node
562+
dts_node = f"\t{iomuxc_name.lower()}: {iomuxc_name} {{\n"
563+
dts_node += (f"\t\tpinmux = <0x{register:x} {mode:d} 0x{input_reg:x} "
564+
f"{input_daisy:d} 0x{config_reg:x}>;\n")
565+
dts_node += "\t};\n"
566+
# Write iomuxc dts node to file
567+
soc_dtsi.write(dts_node)
568+
soc_dtsi.write("};\n\n")
569+
soc_dtsi.close()
569570

570571
def write_pinctrl_overrides(self, mexfile, outputfile):
571572
"""
@@ -634,7 +635,7 @@ def write_pinctrl_groups(self, mexfile, outputfile):
634635
pin_groups = self._parse_mex_cfg(config_tree, self._signal_map)
635636
# Start by writing header
636637
dts_file.write(self._header)
637-
dts_file.write(f"#include <nxp/nxp_imx/rt/{self.get_soc().lower()}-iomuxc.h>\n\n")
638+
dts_file.write(f"#include <nxp/nxp_imx/rt/{self.get_soc().lower()}-iomuxc.dtsi>\n\n")
638639
dts_file.write("&pinctrl {\n")
639640
for pin_group in sorted(pin_groups.keys()):
640641
pins = pin_groups[pin_group]
@@ -650,31 +651,21 @@ def write_pinctrl_groups(self, mexfile, outputfile):
650651
overrides = props['overrides']
651652
defaults = props['defaults']
652653
dts_strings = self._props_to_dts(overrides, defaults)
653-
cfg_dict[dts_strings].append(iomux.get_name())
654+
cfg_dict[dts_strings].append(iomux.get_name().lower())
654655
sorted_keys = sorted(cfg_dict.keys())
655656
# Second step- take each dictionary, and write out all pinmuxes
656657
# as well as their shared configuration into a group
657658
for idx in range(len(cfg_dict.keys())):
658659
pinmuxes = cfg_dict[sorted_keys[idx]]
659-
# MUX REG, MUX MODE, DAISY REG, DAISY, CFG_REG
660-
mux_props = ["", "" ,"", "", ""]
661-
# Generate each named pinmux property
660+
pinmux_str = "<"
661+
# Select each DTS node for mux properties
662662
for mux in pinmuxes:
663-
mux_props[0] += f"<{mux + '_MUX'}>,\n\t\t\t\t"
664-
mux_props[1] += f"<{mux + '_MODE'}>,\n\t\t\t\t"
665-
mux_props[2] += f"<{mux + '_INPUT'}>,\n\t\t\t\t"
666-
mux_props[3] += f"<{mux + '_DAISY'}>,\n\t\t\t\t"
667-
mux_props[4] += f"<{mux + '_CFG'}>,\n\t\t\t\t"
668-
for i in range(len(mux_props)):
669-
mux_props[i] = re.sub(r',\n\t\t\t\t$','', mux_props[i])
663+
pinmux_str += f"&{mux}\n\t\t\t\t"
664+
pinmux_str = re.sub(r'\n\t\t\t\t$','>', pinmux_str)
670665
# Name of group
671666
dts_entry = f"\t\tgroup{idx} {{\n"
672667
# Pinmuxes for group
673-
dts_entry += f"\t\t\tmux = {mux_props[0]};\n"
674-
dts_entry += f"\t\t\tmode = {mux_props[1]};\n"
675-
dts_entry += f"\t\t\tinput = {mux_props[2]};\n"
676-
dts_entry += f"\t\t\tdaisy = {mux_props[3]};\n"
677-
dts_entry += f"\t\t\tcfg = {mux_props[4]};\n"
668+
dts_entry += f"\t\t\tpins = {pinmux_str};\n"
678669
# Properties of group
679670
for prop in sorted_keys[idx]:
680671
dts_entry += f"\t\t\t{prop};\n"
@@ -737,8 +728,27 @@ def _props_to_dts(self, props, defaults):
737728
@return array of strings suitable for writing to DTS
738729
"""
739730
zephyr_props = []
731+
prop_mapping = {
732+
'MHZ_50': '50-mhz',
733+
'MHZ_100': '100-mhz',
734+
'MHZ_150': '150-mhz',
735+
'MHZ_200': '200-mhz',
736+
'R0': 'r0',
737+
'R0_2': 'r0-2',
738+
'R0_3': 'r0-3',
739+
'R0_4': 'r0-4',
740+
'R0_5': 'r0-5',
741+
'R0_6': 'r0-6',
742+
'R0_7': 'r0-7',
743+
'Pull_Down_100K_Ohm': '100k',
744+
'Pull_Up_47K_Ohm': '47k',
745+
'Pull_Up_100K_Ohm': '100k',
746+
'Pull_Up_22K_Ohm': '22k',
747+
'Fast': 'fast',
748+
'Slow': 'slow',
749+
}
740750
# Lambda to convert property names to zephyr formatted strings
741-
sanitize = lambda x : "\"" + re.sub(r'[\W_]', '-', x).lower() + "\""
751+
sanitize = lambda x: "\"" + prop_mapping[x] + "\"" if (x in prop_mapping) else ""
742752
# Check pin defaults and overrides to see if the pin will have a pull or keeper
743753
if 'pull_keeper_enable' in props:
744754
pull_keeper = props['pull_keeper_enable'] == 'Enable'
@@ -751,8 +761,8 @@ def _props_to_dts(self, props, defaults):
751761
keeper = props['pull_keeper_select'] == 'Keeper'
752762
else:
753763
keeper = defaults['pull_keeper_select'] == 'Keeper'
754-
if keeper:
755-
zephyr_props.append('bias-bus-hold')
764+
if not keeper:
765+
zephyr_props.append('bias-disable')
756766
# For each property, append the provided override or the default
757767
if 'drive_strength' in props:
758768
zephyr_props.append(f"drive-strength = {sanitize(props['drive_strength'])}")

tools/rt_dts_gen.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262

6363
if not args.soc_output:
6464
if args.pin_groups:
65-
args.soc_output = f"{soc_name.lower()}-iomuxc.h"
65+
args.soc_output = f"{soc_name.lower()}-iomuxc.dtsi"
6666
else:
6767
args.soc_output = f"{soc_name.lower()}-pinctrl.dtsi"
6868

@@ -111,7 +111,7 @@
111111
print(f"Wrote pinctrl dts file to {args.board_output}")
112112
else:
113113
print(f"Generating configuration for {board_name}")
114-
util.write_pinctrl_header(args.soc_output)
115-
print(f"Wrote pinctrl header file to {args.soc_output}")
114+
util.write_pinctrl_defs(args.soc_output)
115+
print(f"Wrote pinctrl header defs to {args.soc_output}")
116116
util.write_pinctrl_groups(args.mex_file, args.board_output)
117117
print(f"Wrote pinctrl dts file to {args.board_output}")

0 commit comments

Comments
 (0)