aboutsummaryrefslogtreecommitdiff
path: root/devlib/module/thermal.py
blob: 4fa8e1534eadb13ddb2abcfaf0bfbeaf106ace54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#    Copyright 2015 ARM Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import re

from devlib.module import Module

class TripPoint(object):
    def __init__(self, zone, _id):
        self._id = _id
        self.zone = zone
        self.temp_node = 'trip_point_' + _id + '_temp'
        self.type_node = 'trip_point_' + _id + '_type'

    @property
    def target(self):
        return self.zone.target

    def get_temperature(self):
        """Returns the currently configured temperature of the trip point"""
        temp_file = self.target.path.join(self.zone.path, self.temp_node)
        return self.target.read_int(temp_file)

    def set_temperature(self, temperature):
        temp_file = self.target.path.join(self.zone.path, self.temp_node)
        self.target.write_value(temp_file, temperature)

    def get_type(self):
        """Returns the type of trip point"""
        type_file = self.target.path.join(self.zone.path, self.type_node)
        return self.target.read_value(type_file)

class ThermalZone(object):
    def __init__(self, target, root, _id):
        self.target = target
        self.name = 'thermal_zone' + _id
        self.path = target.path.join(root, self.name)
        self.trip_points = {}

        for entry in self.target.list_directory(self.path):
            re_match = re.match('^trip_point_([0-9]+)_temp', entry)
            if re_match is not None:
                self.add_trip_point(re_match.group(1))

    def add_trip_point(self, _id):
        self.trip_points[int(_id)] = TripPoint(self, _id)

    def is_enabled(self):
        """Returns a boolean representing the 'mode' of the thermal zone"""
        value = self.target.read_value(self.target.path.join(self.path, 'mode'))
        return value == 'enabled'

    def set_mode(self, enable):
        value = 'enabled' if enable else 'disabled'
        self.target.write_value(self.target.path.join(self.path, 'mode'), value)

    def get_temperature(self):
        """Returns the temperature of the thermal zone"""
        temp_file = self.target.path.join(self.path, 'temp')
        return self.target.read_int(temp_file)

class ThermalModule(Module):
    name = 'thermal'
    thermal_root = '/sys/class/thermal'

    @staticmethod
    def probe(target):

        if target.file_exists(ThermalModule.thermal_root):
            return True

    def __init__(self, target):
        super(ThermalModule, self).__init__(target)

        self.zones = {}
        self.cdevs = []

        for entry in target.list_directory(self.thermal_root):
            re_match = re.match('^(thermal_zone|cooling_device)([0-9]+)', entry)

            if re_match.group(1) == 'thermal_zone':
                self.add_thermal_zone(re_match.group(2))
            elif re_match.group(1) == 'cooling_device':
                # TODO
                pass

    def add_thermal_zone(self, _id):
        self.zones[int(_id)] = ThermalZone(self.target, self.thermal_root, _id)

    def disable_all_zones(self):
        """Disables all the thermal zones in the target"""
        for zone in self.zones:
            zone.set_mode('disabled')