#!/usr/bin/env python3 # # SPDX-FileCopyrightText: Copyright (c) 2020-2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: LicenseRef-NvidiaProprietary # # NVIDIA CORPORATION, its affiliates and licensors retain all intellectual # property and proprietary rights in and to this material, related # documentation and any modifications thereto. Any use, reproduction, # disclosure or distribution of this material and related documentation # without an express license agreement from NVIDIA CORPORATION or # its affiliates is strictly prohibited. import time from pathlib import Path class SocthermIRQ(object): def __init__(self, path): self.path = path self.irq_cnt_prev = self.irq_cnt() def get_new_event(self): new = self.irq_cnt() - self.irq_cnt_prev self.irq_cnt_prev += new return new def irq_cnt(self): return int(self.path.read_text().strip()) def oc_event_t194(): soctherm_oc = None hwmons = [p for p in Path('/sys/class/hwmon').glob('*')] for hwmon in hwmons: name = hwmon.joinpath('name').read_text().strip() if name == 'soctherm_oc': soctherm_oc = hwmon if soctherm_oc: soctherm_irqs = [SocthermIRQ(p) for p in soctherm_oc.glob('oc*irq_cnt')] else: return None while True: for irq in soctherm_irqs: if irq.get_new_event() > 0: return ('throttle-alert', 'Over-current') time.sleep(1) def oc_event_t234(): soctherm_oc = None hwmons = [p for p in Path('/sys/class/hwmon').glob('*')] for hwmon in hwmons: oc_hwmon_name = 'soctherm_oc' name = hwmon.joinpath('name').read_text().strip() if oc_hwmon_name == name: soctherm_oc = hwmon if soctherm_oc: evt_pattern = 'oc*_event_cnt' soctherm_irqs = [SocthermIRQ(p) for p in soctherm_oc.glob(evt_pattern)] else: return None while True: for irq in soctherm_irqs: if irq.get_new_event() > 0: return ('throttle-alert', 'Over-current') time.sleep(1) oc_event_func = {} oc_event_func['tegra194'] = oc_event_t194 oc_event_func['tegra234'] = oc_event_t234 def soc_family(): try: with open("/proc/device-tree/compatible") as f: soc_family = None compatible = f.read().split('\x00') for s in compatible: for soc in oc_event_func.keys(): if soc in s: soc_family = soc return soc_family except IOError: return 'unknown' class OcEvent(object): def __init__(self): if soc_family(): self.wait_event = oc_event_func[soc_family()] def wait_event(): return self.wait_event() if __name__ == '__main__': print(f"SoC family: {soc_family()}") print("Waiting for OC event..") oc = OcEvent() while True: oc.wait_event() print("OC event occurred.")