blob: fee89727a6134e2e3c2ed6296e4cb81803ede7fc [file] [log] [blame]
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -07001/*
2 * Copyright (c) 2020, The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 *
18 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
28 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
Bavyasritha Alahari0a344f42023-04-26 17:16:51 -070031/* Changes from Qualcomm Innovation Center are provided under the following license:
32
33Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
34SPDX-License-Identifier: BSD-3-Clause-Clear */
35
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -070036#include <android-base/file.h>
37#include <android-base/logging.h>
38#include <android-base/properties.h>
39#include <android-base/stringprintf.h>
40#include <android-base/strings.h>
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -070041
42#include "thermalConfig.h"
43#include "thermalUtils.h"
44
Bavyasritha Alahari0a344f42023-04-26 17:16:51 -070045namespace aidl {
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -070046namespace android {
47namespace hardware {
48namespace thermal {
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -070049
50ThermalUtils::ThermalUtils(const ueventCB &inp_cb):
51 cfg(),
52 cmnInst(),
53 monitor(std::bind(&ThermalUtils::ueventParse, this,
54 std::placeholders::_1,
55 std::placeholders::_2)),
56 cb(inp_cb)
57{
58 int ret = 0;
59 std::vector<struct therm_sensor> sensorList;
Ram Chandrasekardbc52fc2020-11-17 13:57:40 -080060 std::vector<struct target_therm_cfg> therm_cfg = cfg.fetchConfig();
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -070061
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -070062 is_cdev_init = false;
Ram Chandrasekardbc52fc2020-11-17 13:57:40 -080063 ret = cmnInst.initThermalZones(therm_cfg);
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -070064 if (ret > 0) {
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -070065 sensorList = cmnInst.fetch_sensor_list();
66 std::lock_guard<std::mutex> _lock(sens_cb_mutex);
Ram Chandrasekardbc52fc2020-11-17 13:57:40 -080067 for (struct therm_sensor sens: sensorList) {
68 thermalConfig[sens.sensor_name] = sens;
69 cmnInst.read_temperature(sens);
Ram Chandrasekar56329992020-12-28 16:44:40 -080070 cmnInst.estimateSeverity(sens);
Ram Chandrasekardbc52fc2020-11-17 13:57:40 -080071 cmnInst.initThreshold(sens);
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -070072 }
73 monitor.start();
74 }
75 ret = cmnInst.initCdev();
76 if (ret > 0) {
77 is_cdev_init = true;
78 cdevList = cmnInst.fetch_cdev_list();
79 }
80}
81
Michael Bestas04aeca62025-07-06 23:44:50 +030082bool ThermalUtils::isSensorInitialized()
83{
84 std::lock_guard<std::mutex> _lock(sens_cb_mutex);
85
86 if (thermalConfig.begin() == thermalConfig.end())
87 return false;
88
89 return true;
90}
91
92bool ThermalUtils::isSensorInitialized(TemperatureType type)
93{
94 std::unordered_map<std::string, struct therm_sensor>::iterator it;
95 std::lock_guard<std::mutex> _lock(sens_cb_mutex);
96
97 if (thermalConfig.begin() == thermalConfig.end())
98 return false;
99
100 for (it = thermalConfig.begin(); it != thermalConfig.end();
101 it++) {
102 struct therm_sensor& sens = it->second;
103 if (sens.t.type == type)
104 return true;
105 }
106
107 return false;
108}
109
Ram Chandrasekar56329992020-12-28 16:44:40 -0800110void ThermalUtils::Notify(struct therm_sensor& sens)
111{
112 int severity = cmnInst.estimateSeverity(sens);
113 if (severity != -1) {
114 LOG(INFO) << "sensor: " << sens.sensor_name <<" temperature: "
115 << sens.t.value << " old: " <<
116 (int)sens.lastThrottleStatus << " new: " <<
117 (int)sens.t.throttlingStatus << std::endl;
118 cb(sens.t);
119 cmnInst.initThreshold(sens);
120 }
121}
122
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700123void ThermalUtils::ueventParse(std::string sensor_name, int temp)
124{
Rashid Zafare10611c2023-06-21 12:09:36 -0700125 LOG(DEBUG) << "uevent triggered for sensor: " << sensor_name
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700126 << std::endl;
Ram Chandrasekardbc52fc2020-11-17 13:57:40 -0800127 if (thermalConfig.find(sensor_name) == thermalConfig.end()) {
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700128 LOG(DEBUG) << "sensor is not monitored:" << sensor_name
129 << std::endl;
130 return;
131 }
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700132 std::lock_guard<std::mutex> _lock(sens_cb_mutex);
Ram Chandrasekardbc52fc2020-11-17 13:57:40 -0800133 struct therm_sensor& sens = thermalConfig[sensor_name];
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700134 sens.t.value = (float)temp / (float)sens.mulFactor;
Ram Chandrasekar56329992020-12-28 16:44:40 -0800135 return Notify(sens);
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700136}
137
Bavyasritha Alahari0a344f42023-04-26 17:16:51 -0700138int ThermalUtils::readTemperatures(std::vector<Temperature>& temp)
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700139{
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700140 std::unordered_map<std::string, struct therm_sensor>::iterator it;
141 int ret = 0;
Ram Chandrasekarf020a1d2020-08-25 14:25:14 -0700142 std::vector<Temperature> _temp;
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700143
Ram Chandrasekar56329992020-12-28 16:44:40 -0800144 std::lock_guard<std::mutex> _lock(sens_cb_mutex);
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700145 for (it = thermalConfig.begin(); it != thermalConfig.end(); it++) {
Ram Chandrasekardbc52fc2020-11-17 13:57:40 -0800146 struct therm_sensor& sens = it->second;
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700147
Bavyasritha Alahari0a344f42023-04-26 17:16:51 -0700148 ret = cmnInst.read_temperature(sens);
149 if (ret < 0)
150 return ret;
151 Notify(sens);
152 _temp.push_back(sens.t);
153 }
154
155 temp = _temp;
156 return temp.size();
157}
158
159int ThermalUtils::readTemperatures(TemperatureType type,
160 std::vector<Temperature>& temp)
161{
162 std::unordered_map<std::string, struct therm_sensor>::iterator it;
163 int ret = 0;
164 std::vector<Temperature> _temp;
165
166 std::lock_guard<std::mutex> _lock(sens_cb_mutex);
167 for (it = thermalConfig.begin(); it != thermalConfig.end(); it++) {
168 struct therm_sensor& sens = it->second;
169
170 if (sens.t.type != type)
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700171 continue;
Ram Chandrasekardbc52fc2020-11-17 13:57:40 -0800172 ret = cmnInst.read_temperature(sens);
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700173 if (ret < 0)
174 return ret;
Ram Chandrasekar56329992020-12-28 16:44:40 -0800175 Notify(sens);
Ram Chandrasekarf020a1d2020-08-25 14:25:14 -0700176 _temp.push_back(sens.t);
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700177 }
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700178
Ram Chandrasekarf020a1d2020-08-25 14:25:14 -0700179 temp = _temp;
180 return temp.size();
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700181}
182
Bavyasritha Alahari0a344f42023-04-26 17:16:51 -0700183int ThermalUtils::readTemperatureThreshold(std::vector<TemperatureThreshold>& thresh)
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700184{
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700185 std::unordered_map<std::string, struct therm_sensor>::iterator it;
Ram Chandrasekarf020a1d2020-08-25 14:25:14 -0700186 std::vector<TemperatureThreshold> _thresh;
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700187
188 for (it = thermalConfig.begin(); it != thermalConfig.end(); it++) {
Ram Chandrasekardbc52fc2020-11-17 13:57:40 -0800189 struct therm_sensor& sens = it->second;
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700190
Bavyasritha Alahari0a344f42023-04-26 17:16:51 -0700191 _thresh.push_back(sens.thresh);
192 }
193
194 thresh = _thresh;
195 return thresh.size();
196}
197
198int ThermalUtils::readTemperatureThreshold(TemperatureType type,
199 std::vector<TemperatureThreshold>& thresh)
200{
201 std::unordered_map<std::string, struct therm_sensor>::iterator it;
202 std::vector<TemperatureThreshold> _thresh;
203
204 for (it = thermalConfig.begin(); it != thermalConfig.end(); it++) {
205 struct therm_sensor& sens = it->second;
206
207 if (sens.t.type != type)
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700208 continue;
Ram Chandrasekarf020a1d2020-08-25 14:25:14 -0700209 _thresh.push_back(sens.thresh);
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700210 }
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700211
Ram Chandrasekarf020a1d2020-08-25 14:25:14 -0700212 thresh = _thresh;
213 return thresh.size();
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700214}
215
Bavyasritha Alahari0a344f42023-04-26 17:16:51 -0700216int ThermalUtils::readCdevStates(std::vector<CoolingDevice>& cdev_out)
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700217{
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700218 int ret = 0;
Ram Chandrasekarf020a1d2020-08-25 14:25:14 -0700219 std::vector<CoolingDevice> _cdev;
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700220
Ram Chandrasekarf020a1d2020-08-25 14:25:14 -0700221 for (struct therm_cdev cdev: cdevList) {
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700222
Bavyasritha Alahari0a344f42023-04-26 17:16:51 -0700223 ret = cmnInst.read_cdev_state(cdev);
224 if (ret < 0)
225 return ret;
226 _cdev.push_back(cdev.c);
227 }
228
229 cdev_out = _cdev;
230
231 return cdev_out.size();
232}
233
234int ThermalUtils::readCdevStates(cdevType type,
235 std::vector<CoolingDevice>& cdev_out)
236{
237 int ret = 0;
238 std::vector<CoolingDevice> _cdev;
239
240 for (struct therm_cdev cdev: cdevList) {
241
242 if (cdev.c.type != type)
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700243 continue;
Ram Chandrasekardbc52fc2020-11-17 13:57:40 -0800244 ret = cmnInst.read_cdev_state(cdev);
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700245 if (ret < 0)
246 return ret;
Ram Chandrasekarf020a1d2020-08-25 14:25:14 -0700247 _cdev.push_back(cdev.c);
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700248 }
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700249
Ram Chandrasekarf020a1d2020-08-25 14:25:14 -0700250 cdev_out = _cdev;
251
252 return cdev_out.size();
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700253}
254
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700255} // namespace thermal
256} // namespace hardware
257} // namespace android
Rashid Zafare10611c2023-06-21 12:09:36 -0700258} // namespace aidl