Skip to content

Commit

Permalink
soc/cores/gpio: add dts support
Browse files Browse the repository at this point in the history
gpio will require target integration, for example:

    self.gpio = gpio = GPIOTristate(pads = pads)
    self.add_dts_node("gpio", gpio)

NOTE: GPIOIn and GPIOOut will generate DTS compatible with the current
Linux driver, but the existing DTS nodes for litex GPIO depart from
DTS conventions are are worth changing if there are no compatibility
concerns.
  • Loading branch information
Andrew Dennison committed Jul 21, 2024
1 parent 177c6c1 commit f989254
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions litex/soc/cores/gpio.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from migen import *
from migen.genlib.cdc import MultiReg

from litex.gen import *
from litex.gen import dts_property, LiteXModule

from litex.soc.interconnect.csr import *
from litex.soc.interconnect.csr_eventmanager import *
Expand Down Expand Up @@ -46,21 +46,48 @@ def add_irq(self, in_pads):
# GPIO Input ---------------------------------------------------------------------------------------

class GPIOIn(_GPIOIRQ):
def __init__(self, pads, with_irq=False):
dts_compatible = "litex,gpioin"
dts_node = "gpio"
dts_properties = dts_property("gpio-controller")
dts_properties += dts_property("#gpio-cells", 2)

def __init__(self, pads, with_irq=False, deprecated_dts=True):
pads = _to_signal(pads)
self._in = CSRStatus(len(pads), description="GPIO Input(s) Status.")
self.specials += MultiReg(pads, self._in.status)

self.dts_properties += dts_property("ngpio", len(pads))
if deprecated_dts: # deprecated: for linux driver
# the values below are based on json2dts_linux "switches"
# should be compatible with the linux driver
self.dts_compatible = ["litex,gpioin", "litex,gpio"]
self.dts_properties += dts_property("litex,direction", "in")
self.dts_properties += dts_property("litex,ngpio", len(pads))

if with_irq:
self.add_irq(self._in.status)

# GPIO Output --------------------------------------------------------------------------------------

class GPIOOut(LiteXModule):
def __init__(self, pads, reset=0):
dts_compatible = "litex,gpioout"
dts_node = "gpio"
dts_properties = dts_property("gpio-controller")
dts_properties += dts_property("#gpio-cells", 2)

def __init__(self, pads, reset=0, deprecated_dts=True):
pads = _to_signal(pads)
self.out = CSRStorage(len(pads), reset=reset, description="GPIO Output(s) Control.")
self.comb += pads.eq(self.out.storage)

self.dts_properties += dts_property("ngpio", len(pads))
if deprecated_dts: # deprecated: for linux driver
# the values below are based on json2dts_linux "leds"
# should be compatible with the linux driver
self.dts_compatible = ["litex,gpioout", "litex,gpio"]
self.dts_properties += dts_property("litex,direction", "out")
self.dts_properties += dts_property("litex,ngpio", len(pads))

# GPIO Input/Output --------------------------------------------------------------------------------

class GPIOInOut(LiteXModule):
Expand All @@ -74,6 +101,11 @@ def get_csrs(self):
# GPIO Tristate ------------------------------------------------------------------------------------

class GPIOTristate(_GPIOIRQ):
dts_compatible = "litex,gpiotristate"
dts_node = "gpio"
dts_properties = dts_property("gpio-controller")
dts_properties += dts_property("#gpio-cells", 2)

def __init__(self, pads, with_irq=False):
internal = not (hasattr(pads, "o") and hasattr(pads, "oe") and hasattr(pads, "i"))
nbits = len(pads) if internal else len(pads.o)
Expand Down Expand Up @@ -104,5 +136,7 @@ def __init__(self, pads, with_irq=False):
self.comb += pads.o[i].eq(self._out.storage[i])
self.specials += MultiReg(pads.i[i], self._in.status[i])

self.dts_properties += dts_property("ngpio", nbits)

if with_irq:
self.add_irq(self._in.status)

0 comments on commit f989254

Please sign in to comment.