+# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
+#
+# SPDX-License-Identifier: MIT
+"""Broadcom BCM283x pin names"""
+from pathlib import Path
+import lgpio
+
+
+def _get_gpiochip():
+ """
+ Determines the handle of the GPIO chip device to access.
+
+ iterate through sysfs to find a GPIO chip device with a driver known to be
+ used for userspace GPIO access.
+ """
+ for dev in Path("/sys/bus/gpio/devices").glob("gpiochip*"):
+ drivers = set((dev / "of_node/compatible").read_text().split("\0"))
+ # check if driver names are intended for userspace control
+ if drivers & {
+ "raspberrypi,rp1-gpio",
+ "raspberrypi,bcm2835-gpio",
+ "raspberrypi,bcm2711-gpio",
+ }:
+ return lgpio.gpiochip_open(int(dev.name[-1]))
+ # return chip0 as a fallback
+ return lgpio.gpiochip_open(0)
+
+
+CHIP = _get_gpiochip()
+