1 # SPDX-FileCopyrightText: 2023 Steve Jeong for Hardkernel
 
   3 # SPDX-License-Identifier: MIT
 
   6 Functions to find aliases for all hardware,
 
   7 including those defined in the device tree.
 
  10 from typing import Optional
 
  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")
 
  23                 match = re.search(pattern, line)
 
  25                     return match.group(1).upper()
 
  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):
 
  35         if device in os.path.realpath(pwm_sys_path):
 
  36             alias = "PWM" + str(chipid)