]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/alias.py
PicoMini changed from RV1106 to RV1103
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / alias.py
1 # SPDX-FileCopyrightText: 2023 Steve Jeong for Hardkernel
2 #
3 # SPDX-License-Identifier: MIT
4
5 """
6 Functions to find aliases for all hardware,
7 including those defined in the device tree.
8 """
9
10 from typing import Optional
11 import os
12 import re
13
14
15 def get_dts_alias(device: str) -> Optional[str]:
16     """Get the Device Tree Alias"""
17     uevent_path = "/sys/bus/platform/devices/" + device + "/uevent"
18     if os.path.exists(uevent_path):
19         with open(uevent_path, "r", encoding="utf-8") as fd:
20             pattern = r"^OF_ALIAS_0=(.*)$"
21             uevent = fd.read().split("\n")
22             for line in uevent:
23                 match = re.search(pattern, line)
24                 if match:
25                     return match.group(1).upper()
26     return None
27
28
29 def get_pwm_chipid(device: str):
30     """Get the PWM Chip ID"""
31     for chipid in range(32):
32         pwm_sys_path = "/sys/class/pwm/pwmchip{}".format(chipid)
33         if not os.path.exists(pwm_sys_path):
34             continue
35         if device in os.path.realpath(pwm_sys_path):
36             alias = "PWM" + str(chipid)
37             return alias
38     return None