1# Copyright 2015 ARM Limited 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15import re 16 17from devlib.module import Module 18 19class TripPoint(object): 20 def __init__(self, zone, _id): 21 self._id = _id 22 self.zone = zone 23 self.temp_node = 'trip_point_' + _id + '_temp' 24 self.type_node = 'trip_point_' + _id + '_type' 25 26 @property 27 def target(self): 28 return self.zone.target 29 30 def get_temperature(self): 31 """Returns the currently configured temperature of the trip point""" 32 temp_file = self.target.path.join(self.zone.path, self.temp_node) 33 return self.target.read_int(temp_file) 34 35 def set_temperature(self, temperature): 36 temp_file = self.target.path.join(self.zone.path, self.temp_node) 37 self.target.write_value(temp_file, temperature) 38 39 def get_type(self): 40 """Returns the type of trip point""" 41 type_file = self.target.path.join(self.zone.path, self.type_node) 42 return self.target.read_value(type_file) 43 44class ThermalZone(object): 45 def __init__(self, target, root, _id): 46 self.target = target 47 self.name = 'thermal_zone' + _id 48 self.path = target.path.join(root, self.name) 49 self.trip_points = {} 50 51 for entry in self.target.list_directory(self.path): 52 re_match = re.match('^trip_point_([0-9]+)_temp', entry) 53 if re_match is not None: 54 self.add_trip_point(re_match.group(1)) 55 56 def add_trip_point(self, _id): 57 self.trip_points[int(_id)] = TripPoint(self, _id) 58 59 def is_enabled(self): 60 """Returns a boolean representing the 'mode' of the thermal zone""" 61 value = self.target.read_value(self.target.path.join(self.path, 'mode')) 62 return value == 'enabled' 63 64 def set_mode(self, enable): 65 value = 'enabled' if enable else 'disabled' 66 self.target.write_value(self.target.path.join(self.path, 'mode'), value) 67 68 def get_temperature(self): 69 """Returns the temperature of the thermal zone""" 70 temp_file = self.target.path.join(self.path, 'temp') 71 return self.target.read_int(temp_file) 72 73class ThermalModule(Module): 74 name = 'thermal' 75 thermal_root = '/sys/class/thermal' 76 77 @staticmethod 78 def probe(target): 79 80 if target.file_exists(ThermalModule.thermal_root): 81 return True 82 83 def __init__(self, target): 84 super(ThermalModule, self).__init__(target) 85 86 self.zones = {} 87 self.cdevs = [] 88 89 for entry in target.list_directory(self.thermal_root): 90 re_match = re.match('^(thermal_zone|cooling_device)([0-9]+)', entry) 91 92 if re_match.group(1) == 'thermal_zone': 93 self.add_thermal_zone(re_match.group(2)) 94 elif re_match.group(1) == 'cooling_device': 95 # TODO 96 pass 97 98 def add_thermal_zone(self, _id): 99 self.zones[int(_id)] = ThermalZone(self.target, self.thermal_root, _id) 100 101 def disable_all_zones(self): 102 """Disables all the thermal zones in the target""" 103 for zone in self.zones: 104 zone.set_mode('disabled') 105