diff --git a/scripts/dts/edtlib.py b/scripts/dts/edtlib.py index 19dbffe1687..d0841b65dd8 100644 --- a/scripts/dts/edtlib.py +++ b/scripts/dts/edtlib.py @@ -789,6 +789,11 @@ class Node: flash_controller: The flash controller for the node. Only meaningful for nodes representing flash partitions. + + spi_cs_gpio: + The device's SPI GPIO chip select as a ControllerAndData instance, if it + exists, and None otherwise. See + Documentation/devicetree/bindings/spi/spi-controller.yaml in the Linux kernel. """ @property def name(self): @@ -927,6 +932,28 @@ class Node: return controller.parent return controller + @property + def spi_cs_gpio(self): + "See the class docstring" + + if not (self.on_bus == "spi" and "cs-gpios" in self.bus_node.props): + return None + + if not self.regs: + _err("{!r} needs a 'reg' property, to look up the chip select index " + "for SPI".format(self)) + + parent_cs_lst = self.bus_node.props["cs-gpios"].val + + # cs-gpios is indexed by the unit address + cs_index = self.regs[0].addr + if cs_index >= len(parent_cs_lst): + _err("index from 'regs' in {!r} ({}) is >= number of cs-gpios " + "in {!r} ({})".format( + self, cs_index, self.bus_node, len(parent_cs_lst))) + + return parent_cs_lst[cs_index] + def __repr__(self): return "".format( self.path, self.edt.dts_path, @@ -1504,35 +1531,6 @@ class EDTError(Exception): "Exception raised for devicetree- and binding-related errors" -# -# Public global functions -# - - -def spi_dev_cs_gpio(node): - # Returns an SPI device's GPIO chip select if it exists, as a - # ControllerAndData instance, and None otherwise. See - # Documentation/devicetree/bindings/spi/spi-bus.txt in the Linux kernel. - - if not (node.on_bus == "spi" and "cs-gpios" in node.bus_node.props): - return None - - if not node.regs: - _err("{!r} needs a 'reg' property, to look up the chip select index " - "for SPI".format(node)) - - parent_cs_lst = node.bus_node.props["cs-gpios"].val - - # cs-gpios is indexed by the unit address - cs_index = node.regs[0].addr - if cs_index >= len(parent_cs_lst): - _err("index from 'regs' in {!r} ({}) is >= number of cs-gpios " - "in {!r} ({})".format( - node, cs_index, node.bus_node, len(parent_cs_lst))) - - return parent_cs_lst[cs_index] - - # # Private global functions # diff --git a/scripts/dts/gen_defines.py b/scripts/dts/gen_defines.py index 428f9886746..7ed55ab3bf1 100755 --- a/scripts/dts/gen_defines.py +++ b/scripts/dts/gen_defines.py @@ -549,7 +549,7 @@ def write_irqs(node): def write_spi_dev(node): # Writes SPI device GPIO chip select data if there is any - cs_gpio = edtlib.spi_dev_cs_gpio(node) + cs_gpio = node.spi_cs_gpio if cs_gpio is not None: write_phandle_val_list_entry(node, cs_gpio, None, "CS_GPIOS")