#
 # SPDX-License-Identifier: MIT
 """Allwinner H618 Pin Names"""
+import re
 from adafruit_blinka.microcontroller.generic_linux.libgpiod_pin import Pin
 
-# gpiochip select
-__chip_num = 0
-with open("/sys/class/gpio/gpiochip0/label", "r") as f:
-    label = f.read().strip()
-    if label == "300b000.pinctrl":
-        __chip_num = 1
+
+def find_gpiochip_number(target_label):
+    """Get correct gpiochip number, legacy kernel and mainline kernel are different"""
+    try:
+        with open("/sys/kernel/debug/gpio", "r") as f:
+            lines = f.readlines()
+    except FileNotFoundError:
+        print("The file /sys/kernel/debug/gpio does not exist.")
+        return None
+
+    gpiochip_number = None
+    for line in lines:
+        if target_label in line:
+            match = re.search(r"gpiochip(\d+)", line)
+            if match:
+                gpiochip_number = match.group(1)
+                break
+
+    return gpiochip_number
+
+
+if find_gpiochip_number("300b000.pinctrl"):
+    __chip_num = 1
+else:
+    __chip_num = 0
 
 PC0 = Pin((__chip_num, 64))
 SPI0_SCLK = PC0
 PG18 = Pin((__chip_num, 210))
 TWI3_SDA = PG18
 PG19 = Pin((__chip_num, 211))
+PWM1 = PG19
 
 PH0 = Pin((__chip_num, 224))
 PH1 = Pin((__chip_num, 225))
 UART3_RX = PI10
 PI11 = Pin((__chip_num, 267))
 PI12 = Pin((__chip_num, 268))
+PWM2 = PI12
 PI13 = Pin((__chip_num, 269))
 UART4_TX = PI13
 PI14 = Pin((__chip_num, 270))
     (4, UART4_TX, UART4_RX),
     (5, UART5_TX, UART5_RX),
 )
+
+# SysFS pwm outputs, pwm channel and pin in first tuple
+pwmOuts = [
+    ((0, 1), PWM1),
+    ((0, 2), PWM2),
+]