|
1 | 1 | #!/usr/bin/env python |
| 2 | +import glob |
2 | 3 | import os |
3 | 4 | import signal |
4 | 5 | import time |
|
9 | 10 |
|
10 | 11 | ALERT_VOLTAGE_THRESHOLD_mV = 4000 |
11 | 12 |
|
12 | | -POWER_ALERT_GPIO_PIN = 1264 |
13 | | -INA231_BUS = 0 |
| 13 | +def get_tlmm_base(): |
| 14 | + bases = glob.glob("/sys/bus/platform/devices/3400000.pinctrl/gpio/*/base") |
| 15 | + if bases: |
| 16 | + with open(bases[0]) as f: |
| 17 | + return int(f.read().strip()) |
| 18 | + return 0 |
| 19 | + |
| 20 | +TLMM_BASE = get_tlmm_base() |
| 21 | + |
| 22 | +def get_pm8998_base(): |
| 23 | + """Find PM8998 PMIC GPIO chip base (SPMI USID 0, GPIO @ 0xc000)""" |
| 24 | + for chip in sorted(glob.glob("/sys/class/gpio/gpiochip*")): |
| 25 | + try: |
| 26 | + with open(os.path.join(chip, "label")) as f: |
| 27 | + label = f.read().strip() |
| 28 | + if ("spmi" in label and "pmic@0" in label and "gpio" in label) or ("pm8998" in label and "gpio" in label): |
| 29 | + with open(os.path.join(chip, "base")) as f: |
| 30 | + return int(f.read().strip()) |
| 31 | + except (IOError, ValueError): |
| 32 | + continue |
| 33 | + return 0 |
| 34 | + |
| 35 | +PM8998_BASE = get_pm8998_base() |
| 36 | +POWER_ALERT_GPIO_PIN = PM8998_BASE + 3 # PM8998_GPIO4 (0-indexed offset 3) |
14 | 37 | INA231_ADDRESS = 0x40 |
15 | 38 | INA231_MASK_REG = 0x06 |
16 | 39 | INA231_LIMIT_REG = 0x07 |
17 | 40 | INA231_MASK_CONFIG = (1 << 12) # Bus undervoltage, not latching |
18 | 41 | INA231_BUS_VOLTAGE_LSB_mV = 1.25 |
19 | | -VOLTAGE_FILE = "/sys/class/hwmon/hwmon1/in1_input" |
| 42 | + |
| 43 | +def find_ina231_hwmon(): |
| 44 | + for hwmon in sorted(glob.glob("/sys/class/hwmon/hwmon*")): |
| 45 | + try: |
| 46 | + with open(os.path.join(hwmon, "name")) as f: |
| 47 | + if f.read().strip() == "ina231": |
| 48 | + return hwmon |
| 49 | + except (IOError, ValueError): |
| 50 | + continue |
| 51 | + return None |
| 52 | + |
| 53 | +def find_ina231_i2c_bus(): |
| 54 | + for dev in sorted(glob.glob("/sys/bus/i2c/devices/*-0040")): |
| 55 | + try: |
| 56 | + with open(os.path.join(dev, "name")) as f: |
| 57 | + if f.read().strip() == "ina231": |
| 58 | + return int(os.path.basename(dev).split("-")[0]) |
| 59 | + except (IOError, ValueError): |
| 60 | + continue |
| 61 | + return 0 |
| 62 | + |
| 63 | +INA231_BUS = find_ina231_i2c_bus() |
| 64 | +INA231_HWMON = find_ina231_hwmon() |
| 65 | +VOLTAGE_FILE = os.path.join(INA231_HWMON, "in1_input") if INA231_HWMON else "/sys/class/hwmon/hwmon1/in1_input" |
20 | 66 | PARAM_FILE = "/data/params/d/LastPowerDropDetected" |
21 | 67 | COMMA_CGROUP_PROCS = "/sys/fs/cgroup/comma/cgroup.procs" |
22 | 68 | COMMA_SV_CONTROL = "/run/runit/service/comma/supervise/control" |
@@ -51,8 +97,10 @@ def read_voltage_mV(): |
51 | 97 | with open(VOLTAGE_FILE, "r") as f: |
52 | 98 | return int(f.read().strip()) |
53 | 99 |
|
| 100 | +CURRENT_FILE = os.path.join(INA231_HWMON, "curr1_input") if INA231_HWMON else "/sys/class/hwmon/hwmon1/curr1_input" |
| 101 | + |
54 | 102 | def read_current_mA(): |
55 | | - with open("/sys/class/hwmon/hwmon1/curr1_input", "r") as f: |
| 103 | + with open(CURRENT_FILE, "r") as f: |
56 | 104 | return int(f.read().strip()) |
57 | 105 |
|
58 | 106 | def update_param(stage, v_initial, i_initial, v_final, i_final): |
@@ -107,8 +155,9 @@ def perform_controlled_shutdown(): |
107 | 155 | # SIGKILL all processes in the comma cgroup (comma/run puts all openpilot procs in this cgroup) |
108 | 156 | [os.kill(int(p), signal.SIGKILL) for p in open(COMMA_CGROUP_PROCS).read().split() if p.strip()] |
109 | 157 | # Tell panda SoC is off so it doesn't spin up the fan |
110 | | - write_once("/sys/class/gpio/gpio49/direction", "out") |
111 | | - write_once("/sys/class/gpio/gpio49/value", "0") |
| 158 | + som_st_io = TLMM_BASE + 49 |
| 159 | + write_once(f"/sys/class/gpio/gpio{som_st_io}/direction", "out") |
| 160 | + write_once(f"/sys/class/gpio/gpio{som_st_io}/value", "0") |
112 | 161 | # Wait for all processes to fully exit before syncing (kernel resource cleanup takes ~150-400ms) |
113 | 162 | while open(COMMA_CGROUP_PROCS).read().strip(): |
114 | 163 | time.sleep(0.001) |
|
0 commit comments