Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * thermal_helpers.c - helper functions to handle thermal devices
4 *
5 * Copyright (C) 2016 Eduardo Valentin <edubezval@gmail.com>
6 *
7 * Highly based on original thermal_core.c
8 * Copyright (C) 2008 Intel Corp
9 * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
10 * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
11 */
12
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
15#include <linux/device.h>
16#include <linux/err.h>
17#include <linux/export.h>
18#include <linux/slab.h>
19#include <linux/string.h>
20#include <linux/sysfs.h>
21
22#include <trace/events/thermal.h>
23
24#include "thermal_core.h"
25
26int get_tz_trend(struct thermal_zone_device *tz, int trip)
27{
28 enum thermal_trend trend;
29
30 if (tz->emul_temperature || !tz->ops->get_trend ||
31 tz->ops->get_trend(tz, trip, &trend)) {
32 if (tz->temperature > tz->last_temperature)
33 trend = THERMAL_TREND_RAISING;
34 else if (tz->temperature < tz->last_temperature)
35 trend = THERMAL_TREND_DROPPING;
36 else
37 trend = THERMAL_TREND_STABLE;
38 }
39
40 return trend;
41}
42
43struct thermal_instance *
44get_thermal_instance(struct thermal_zone_device *tz,
45 struct thermal_cooling_device *cdev, int trip)
46{
47 struct thermal_instance *pos = NULL;
48 struct thermal_instance *target_instance = NULL;
49
50 mutex_lock(&tz->lock);
51 mutex_lock(&cdev->lock);
52
53 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
54 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
55 target_instance = pos;
56 break;
57 }
58 }
59
60 mutex_unlock(&cdev->lock);
61 mutex_unlock(&tz->lock);
62
63 return target_instance;
64}
65EXPORT_SYMBOL(get_thermal_instance);
66
67/**
68 * __thermal_zone_get_temp() - returns the temperature of a thermal zone
69 * @tz: a valid pointer to a struct thermal_zone_device
70 * @temp: a valid pointer to where to store the resulting temperature.
71 *
72 * When a valid thermal zone reference is passed, it will fetch its
73 * temperature and fill @temp.
74 *
75 * Both tz and tz->ops must be valid pointers when calling this function,
76 * and the tz->ops->get_temp callback must be provided.
77 * The function must be called under tz->lock.
78 *
79 * Return: On success returns 0, an error code otherwise
80 */
81int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
82{
83 int ret = -EINVAL;
84 int count;
85 int crit_temp = INT_MAX;
86 enum thermal_trip_type type;
87
88 lockdep_assert_held(&tz->lock);
89
90 ret = tz->ops->get_temp(tz, temp);
91
92 if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) {
93 for (count = 0; count < tz->num_trips; count++) {
94 ret = tz->ops->get_trip_type(tz, count, &type);
95 if (!ret && type == THERMAL_TRIP_CRITICAL) {
96 ret = tz->ops->get_trip_temp(tz, count,
97 &crit_temp);
98 break;
99 }
100 }
101
102 /*
103 * Only allow emulating a temperature when the real temperature
104 * is below the critical temperature so that the emulation code
105 * cannot hide critical conditions.
106 */
107 if (!ret && *temp < crit_temp)
108 *temp = tz->emul_temperature;
109 }
110
111 return ret;
112}
113
114/**
115 * thermal_zone_get_temp() - returns the temperature of a thermal zone
116 * @tz: a valid pointer to a struct thermal_zone_device
117 * @temp: a valid pointer to where to store the resulting temperature.
118 *
119 * When a valid thermal zone reference is passed, it will fetch its
120 * temperature and fill @temp.
121 *
122 * Return: On success returns 0, an error code otherwise
123 */
124int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
125{
126 int ret;
127
128 if (IS_ERR_OR_NULL(tz))
129 return -EINVAL;
130
131 mutex_lock(&tz->lock);
132
133 if (!tz->ops->get_temp) {
134 ret = -EINVAL;
135 goto unlock;
136 }
137
138 if (device_is_registered(&tz->device))
139 ret = __thermal_zone_get_temp(tz, temp);
140 else
141 ret = -ENODEV;
142
143unlock:
144 mutex_unlock(&tz->lock);
145
146 return ret;
147}
148EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
149
150/**
151 * __thermal_zone_set_trips - Computes the next trip points for the driver
152 * @tz: a pointer to a thermal zone device structure
153 *
154 * The function computes the next temperature boundaries by browsing
155 * the trip points. The result is the closer low and high trip points
156 * to the current temperature. These values are passed to the backend
157 * driver to let it set its own notification mechanism (usually an
158 * interrupt).
159 *
160 * This function must be called with tz->lock held. Both tz and tz->ops
161 * must be valid pointers.
162 *
163 * It does not return a value
164 */
165void __thermal_zone_set_trips(struct thermal_zone_device *tz)
166{
167 int low = -INT_MAX;
168 int high = INT_MAX;
169 int trip_temp, hysteresis;
170 int i, ret;
171
172 lockdep_assert_held(&tz->lock);
173
174 if (!tz->ops->set_trips || !tz->ops->get_trip_hyst)
175 return;
176
177 for (i = 0; i < tz->num_trips; i++) {
178 int trip_low;
179
180 tz->ops->get_trip_temp(tz, i, &trip_temp);
181 tz->ops->get_trip_hyst(tz, i, &hysteresis);
182
183 trip_low = trip_temp - hysteresis;
184
185 if (trip_low < tz->temperature && trip_low > low)
186 low = trip_low;
187
188 if (trip_temp > tz->temperature && trip_temp < high)
189 high = trip_temp;
190 }
191
192 /* No need to change trip points */
193 if (tz->prev_low_trip == low && tz->prev_high_trip == high)
194 return;
195
196 tz->prev_low_trip = low;
197 tz->prev_high_trip = high;
198
199 dev_dbg(&tz->device,
200 "new temperature boundaries: %d < x < %d\n", low, high);
201
202 /*
203 * Set a temperature window. When this window is left the driver
204 * must inform the thermal core via thermal_zone_device_update.
205 */
206 ret = tz->ops->set_trips(tz, low, high);
207 if (ret)
208 dev_err(&tz->device, "Failed to set trips: %d\n", ret);
209}
210
211static void thermal_cdev_set_cur_state(struct thermal_cooling_device *cdev,
212 int target)
213{
214 if (cdev->ops->set_cur_state(cdev, target))
215 return;
216
217 thermal_notify_cdev_state_update(cdev->id, target);
218 thermal_cooling_device_stats_update(cdev, target);
219}
220
221void __thermal_cdev_update(struct thermal_cooling_device *cdev)
222{
223 struct thermal_instance *instance;
224 unsigned long target = 0;
225
226 /* Make sure cdev enters the deepest cooling state */
227 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
228 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
229 instance->tz->id, instance->target);
230 if (instance->target == THERMAL_NO_TARGET)
231 continue;
232 if (instance->target > target)
233 target = instance->target;
234 }
235
236 thermal_cdev_set_cur_state(cdev, target);
237
238 trace_cdev_update(cdev, target);
239 dev_dbg(&cdev->device, "set to state %lu\n", target);
240}
241
242/**
243 * thermal_cdev_update - update cooling device state if needed
244 * @cdev: pointer to struct thermal_cooling_device
245 *
246 * Update the cooling device state if there is a need.
247 */
248void thermal_cdev_update(struct thermal_cooling_device *cdev)
249{
250 mutex_lock(&cdev->lock);
251 if (!cdev->updated) {
252 __thermal_cdev_update(cdev);
253 cdev->updated = true;
254 }
255 mutex_unlock(&cdev->lock);
256}
257
258/**
259 * thermal_zone_get_slope - return the slope attribute of the thermal zone
260 * @tz: thermal zone device with the slope attribute
261 *
262 * Return: If the thermal zone device has a slope attribute, return it, else
263 * return 1.
264 */
265int thermal_zone_get_slope(struct thermal_zone_device *tz)
266{
267 if (tz && tz->tzp)
268 return tz->tzp->slope;
269 return 1;
270}
271EXPORT_SYMBOL_GPL(thermal_zone_get_slope);
272
273/**
274 * thermal_zone_get_offset - return the offset attribute of the thermal zone
275 * @tz: thermal zone device with the offset attribute
276 *
277 * Return: If the thermal zone device has a offset attribute, return it, else
278 * return 0.
279 */
280int thermal_zone_get_offset(struct thermal_zone_device *tz)
281{
282 if (tz && tz->tzp)
283 return tz->tzp->offset;
284 return 0;
285}
286EXPORT_SYMBOL_GPL(thermal_zone_get_offset);
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * thermal_helpers.c - helper functions to handle thermal devices
4 *
5 * Copyright (C) 2016 Eduardo Valentin <edubezval@gmail.com>
6 *
7 * Highly based on original thermal_core.c
8 * Copyright (C) 2008 Intel Corp
9 * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
10 * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
11 */
12
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
15#include <linux/device.h>
16#include <linux/err.h>
17#include <linux/export.h>
18#include <linux/slab.h>
19#include <linux/string.h>
20#include <linux/sysfs.h>
21
22#include "thermal_core.h"
23#include "thermal_trace.h"
24
25int get_tz_trend(struct thermal_zone_device *tz, const struct thermal_trip *trip)
26{
27 enum thermal_trend trend;
28
29 if (tz->emul_temperature || !tz->ops.get_trend ||
30 tz->ops.get_trend(tz, trip, &trend)) {
31 if (tz->temperature > tz->last_temperature)
32 trend = THERMAL_TREND_RAISING;
33 else if (tz->temperature < tz->last_temperature)
34 trend = THERMAL_TREND_DROPPING;
35 else
36 trend = THERMAL_TREND_STABLE;
37 }
38
39 return trend;
40}
41
42static bool thermal_instance_present(struct thermal_zone_device *tz,
43 struct thermal_cooling_device *cdev,
44 const struct thermal_trip *trip)
45{
46 const struct thermal_trip_desc *td = trip_to_trip_desc(trip);
47 struct thermal_instance *ti;
48
49 list_for_each_entry(ti, &td->thermal_instances, trip_node) {
50 if (ti->cdev == cdev)
51 return true;
52 }
53
54 return false;
55}
56
57bool thermal_trip_is_bound_to_cdev(struct thermal_zone_device *tz,
58 const struct thermal_trip *trip,
59 struct thermal_cooling_device *cdev)
60{
61 guard(thermal_zone)(tz);
62 guard(cooling_dev)(cdev);
63
64 return thermal_instance_present(tz, cdev, trip);
65}
66EXPORT_SYMBOL_GPL(thermal_trip_is_bound_to_cdev);
67
68/**
69 * __thermal_zone_get_temp() - returns the temperature of a thermal zone
70 * @tz: a valid pointer to a struct thermal_zone_device
71 * @temp: a valid pointer to where to store the resulting temperature.
72 *
73 * When a valid thermal zone reference is passed, it will fetch its
74 * temperature and fill @temp.
75 *
76 * Both tz and tz->ops must be valid pointers when calling this function,
77 * and the tz->ops.get_temp callback must be provided.
78 * The function must be called under tz->lock.
79 *
80 * Return: On success returns 0, an error code otherwise
81 */
82int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
83{
84 const struct thermal_trip_desc *td;
85 int crit_temp = INT_MAX;
86 int ret = -EINVAL;
87
88 lockdep_assert_held(&tz->lock);
89
90 ret = tz->ops.get_temp(tz, temp);
91
92 if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) {
93 for_each_trip_desc(tz, td) {
94 const struct thermal_trip *trip = &td->trip;
95
96 if (trip->type == THERMAL_TRIP_CRITICAL) {
97 crit_temp = trip->temperature;
98 break;
99 }
100 }
101
102 /*
103 * Only allow emulating a temperature when the real temperature
104 * is below the critical temperature so that the emulation code
105 * cannot hide critical conditions.
106 */
107 if (!ret && *temp < crit_temp)
108 *temp = tz->emul_temperature;
109 }
110
111 if (ret)
112 dev_dbg(&tz->device, "Failed to get temperature: %d\n", ret);
113
114 return ret;
115}
116
117/**
118 * thermal_zone_get_temp() - returns the temperature of a thermal zone
119 * @tz: a valid pointer to a struct thermal_zone_device
120 * @temp: a valid pointer to where to store the resulting temperature.
121 *
122 * When a valid thermal zone reference is passed, it will fetch its
123 * temperature and fill @temp.
124 *
125 * Return: On success returns 0, an error code otherwise
126 */
127int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
128{
129 int ret;
130
131 if (IS_ERR_OR_NULL(tz))
132 return -EINVAL;
133
134 guard(thermal_zone)(tz);
135
136 if (!tz->ops.get_temp)
137 return -EINVAL;
138
139 ret = __thermal_zone_get_temp(tz, temp);
140 if (!ret && *temp <= THERMAL_TEMP_INVALID)
141 return -ENODATA;
142
143 return ret;
144}
145EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
146
147static int thermal_cdev_set_cur_state(struct thermal_cooling_device *cdev, int state)
148{
149 int ret;
150
151 /*
152 * No check is needed for the ops->set_cur_state as the
153 * registering function checked the ops are correctly set
154 */
155 ret = cdev->ops->set_cur_state(cdev, state);
156 if (ret)
157 return ret;
158
159 thermal_notify_cdev_state_update(cdev, state);
160 thermal_cooling_device_stats_update(cdev, state);
161 thermal_debug_cdev_state_update(cdev, state);
162
163 return 0;
164}
165
166void __thermal_cdev_update(struct thermal_cooling_device *cdev)
167{
168 struct thermal_instance *instance;
169 unsigned long target = 0;
170
171 /* Make sure cdev enters the deepest cooling state */
172 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
173 if (instance->target == THERMAL_NO_TARGET)
174 continue;
175 if (instance->target > target)
176 target = instance->target;
177 }
178
179 thermal_cdev_set_cur_state(cdev, target);
180
181 trace_cdev_update(cdev, target);
182 dev_dbg(&cdev->device, "set to state %lu\n", target);
183}
184
185/**
186 * thermal_cdev_update - update cooling device state if needed
187 * @cdev: pointer to struct thermal_cooling_device
188 *
189 * Update the cooling device state if there is a need.
190 */
191void thermal_cdev_update(struct thermal_cooling_device *cdev)
192{
193 guard(cooling_dev)(cdev);
194
195 if (!cdev->updated) {
196 __thermal_cdev_update(cdev);
197 cdev->updated = true;
198 }
199}
200
201/**
202 * thermal_cdev_update_nocheck() - Unconditionally update cooling device state
203 * @cdev: Target cooling device.
204 */
205void thermal_cdev_update_nocheck(struct thermal_cooling_device *cdev)
206{
207 guard(cooling_dev)(cdev);
208
209 __thermal_cdev_update(cdev);
210}
211
212/**
213 * thermal_zone_get_slope - return the slope attribute of the thermal zone
214 * @tz: thermal zone device with the slope attribute
215 *
216 * Return: If the thermal zone device has a slope attribute, return it, else
217 * return 1.
218 */
219int thermal_zone_get_slope(struct thermal_zone_device *tz)
220{
221 if (tz && tz->tzp)
222 return tz->tzp->slope;
223 return 1;
224}
225EXPORT_SYMBOL_GPL(thermal_zone_get_slope);
226
227/**
228 * thermal_zone_get_offset - return the offset attribute of the thermal zone
229 * @tz: thermal zone device with the offset attribute
230 *
231 * Return: If the thermal zone device has a offset attribute, return it, else
232 * return 0.
233 */
234int thermal_zone_get_offset(struct thermal_zone_device *tz)
235{
236 if (tz && tz->tzp)
237 return tz->tzp->offset;
238 return 0;
239}
240EXPORT_SYMBOL_GPL(thermal_zone_get_offset);