From: Jan Volckaert Date: Sun, 4 Feb 2024 19:48:53 +0000 (+0100) Subject: libgpio 2.x support for odroid c4 X-Git-Tag: 8.32.0~1^2 X-Git-Url: https://git.ayoreis.com/Adafruit_Blinka-hackapet.git/commitdiff_plain/7f33856a3f37c3ef6c82628996699f4420ea58cd libgpio 2.x support for odroid c4 --- diff --git a/src/adafruit_blinka/microcontroller/amlogic/meson_g12_common/pin.py b/src/adafruit_blinka/microcontroller/amlogic/meson_g12_common/pin.py index 3491ce9..ea01a8e 100644 --- a/src/adafruit_blinka/microcontroller/amlogic/meson_g12_common/pin.py +++ b/src/adafruit_blinka/microcontroller/amlogic/meson_g12_common/pin.py @@ -15,30 +15,13 @@ Linux kernel 5.4.y (mainline) from adafruit_blinka.agnostic import detector from adafruit_blinka.microcontroller.alias import get_dts_alias, get_pwm_chipid from adafruit_blinka.microcontroller.generic_linux.libgpiod_pin import Pin +from adafruit_blinka.microcontroller.generic_linux.libgpiod_chip import Chip -try: - import gpiod -except ImportError: - raise ImportError( - "libgpiod Python bindings not found, please install and try again!" - ) from ImportError +chip0 = Chip("0") +chip1 = Chip("1") -if hasattr(gpiod, "Chip"): - chip0 = gpiod.Chip("0") - chip1 = gpiod.Chip("1") -else: - chip0 = gpiod.chip("0") - chip1 = gpiod.chip("1") - -if callable(chip0.num_lines): - chip0lines = chip0.num_lines() -else: - chip0lines = chip0.num_lines - -if callable(chip1.num_lines): - chip1lines = chip1.num_lines() -else: - chip1lines = chip1.num_lines +chip0lines = chip0.num_lines +chip1lines = chip1.num_lines if chip0lines < 20: aobus = 0 diff --git a/src/adafruit_blinka/microcontroller/generic_linux/libgpiod/libgpiod_chip_1_x.py b/src/adafruit_blinka/microcontroller/generic_linux/libgpiod/libgpiod_chip_1_x.py new file mode 100644 index 0000000..532fb44 --- /dev/null +++ b/src/adafruit_blinka/microcontroller/generic_linux/libgpiod/libgpiod_chip_1_x.py @@ -0,0 +1,33 @@ +# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries +# +# SPDX-License-Identifier: MIT +"""A Chip class for use with libgpiod 1.x.""" +import gpiod + + +# pylint: disable=too-many-branches,too-many-statements +class Chip: + """Abstraction for handling all breaking changes over the lifecycle of gpiod""" + + _CONSUMER = "adafruit_blinka" + + id: str = None + num_lines: int + + def __init__(self, chip_id: str): + self.id = chip_id + if hasattr(gpiod, "Chip"): + self._chip = gpiod.Chip(self.id) + else: + self._chip = gpiod.chip(self.id) + + if callable(self._chip.num_lines): + self.num_lines = self._chip.num_lines() + else: + self.num_lines = self.num_lines + + def __repr__(self): + return self.id + + def __eq__(self, other): + return self.id == other diff --git a/src/adafruit_blinka/microcontroller/generic_linux/libgpiod/libgpiod_chip_2_x.py b/src/adafruit_blinka/microcontroller/generic_linux/libgpiod/libgpiod_chip_2_x.py new file mode 100644 index 0000000..8fd75e4 --- /dev/null +++ b/src/adafruit_blinka/microcontroller/generic_linux/libgpiod/libgpiod_chip_2_x.py @@ -0,0 +1,29 @@ +# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries +# +# SPDX-License-Identifier: MIT +"""A Chip class for use with libgpiod 2.x.""" +import gpiod + + +# pylint: disable=too-many-branches,too-many-statements +class Chip: + """Abstraction for handling all breaking changes over the lifecycle of gpiod""" + + _CONSUMER = "adafruit_blinka" + + id: str = None + num_lines: int + + def __init__(self, chip_id: str): + self.id = chip_id + path = f"/dev/gpiochip{self.id}" + self._chip = gpiod.Chip(path) + + info = self._chip.get_info() + self.num_lines = info.num_lines + + def __repr__(self): + return self.id + + def __eq__(self, other): + return self.id == other diff --git a/src/adafruit_blinka/microcontroller/generic_linux/libgpiod_chip.py b/src/adafruit_blinka/microcontroller/generic_linux/libgpiod_chip.py new file mode 100644 index 0000000..66d2376 --- /dev/null +++ b/src/adafruit_blinka/microcontroller/generic_linux/libgpiod_chip.py @@ -0,0 +1,22 @@ +# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries +# +# SPDX-License-Identifier: MIT +"""A Chip class for use with libgpiod.""" +try: + import gpiod +except ImportError: + raise ImportError( + "libgpiod Python bindings not found, please install and try again! See " + "https://github.com/adafruit/Raspberry-Pi-Installer-Scripts/blob/main/libgpiod.py" + ) from ImportError + +# Versions 1.5.4 and earlier have no __version__ attribute +if hasattr(gpiod, "__version__"): + version = gpiod.__version__ +else: + version = "1.x" + +if version.startswith("1."): + from .libgpiod.libgpiod_chip_1_x import Chip # pylint: disable=unused-import +else: + from .libgpiod.libgpiod_chip_2_x import Chip # pylint: disable=unused-import