1 // SPDX-License-Identifier: GPL-2.0
3 * thermal_helpers.c - helper functions to handle thermal devices
7 * Highly based on original thermal_core.c
8 * Copyright (C) 2008 Intel Corp
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
22 #include <trace/events/thermal.h>
24 #include "thermal_core.h"
26 int get_tz_trend(struct thermal_zone_device *tz, int trip)
28 enum thermal_trend trend;
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;
37 trend = THERMAL_TREND_STABLE;
43 struct thermal_instance *
44 get_thermal_instance(struct thermal_zone_device *tz,
45 struct thermal_cooling_device *cdev, int trip)
47 struct thermal_instance *pos = NULL;
48 struct thermal_instance *target_instance = NULL;
50 mutex_lock(&tz->lock);
51 mutex_lock(&cdev->lock);
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;
60 mutex_unlock(&cdev->lock);
61 mutex_unlock(&tz->lock);
63 return target_instance;
65 EXPORT_SYMBOL(get_thermal_instance);
67 int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
71 int crit_temp = INT_MAX;
72 enum thermal_trip_type type;
74 lockdep_assert_held(&tz->lock);
76 if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
79 ret = tz->ops->get_temp(tz, temp);
81 if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) {
82 for (count = 0; count < tz->num_trips; count++) {
83 ret = tz->ops->get_trip_type(tz, count, &type);
84 if (!ret && type == THERMAL_TRIP_CRITICAL) {
85 ret = tz->ops->get_trip_temp(tz, count,
92 * Only allow emulating a temperature when the real temperature
93 * is below the critical temperature so that the emulation code
94 * cannot hide critical conditions.
96 if (!ret && *temp < crit_temp)
97 *temp = tz->emul_temperature;
104 * thermal_zone_get_temp() - returns the temperature of a thermal zone
105 * @tz: a valid pointer to a struct thermal_zone_device
106 * @temp: a valid pointer to where to store the resulting temperature.
108 * When a valid thermal zone reference is passed, it will fetch its
109 * temperature and fill @temp.
111 * Return: On success returns 0, an error code otherwise
113 int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
117 mutex_lock(&tz->lock);
118 ret = __thermal_zone_get_temp(tz, temp);
119 mutex_unlock(&tz->lock);
123 EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
125 void __thermal_zone_set_trips(struct thermal_zone_device *tz)
129 int trip_temp, hysteresis;
132 lockdep_assert_held(&tz->lock);
134 if (!tz->ops->set_trips || !tz->ops->get_trip_hyst)
137 for (i = 0; i < tz->num_trips; i++) {
140 tz->ops->get_trip_temp(tz, i, &trip_temp);
141 tz->ops->get_trip_hyst(tz, i, &hysteresis);
143 trip_low = trip_temp - hysteresis;
145 if (trip_low < tz->temperature && trip_low > low)
148 if (trip_temp > tz->temperature && trip_temp < high)
152 /* No need to change trip points */
153 if (tz->prev_low_trip == low && tz->prev_high_trip == high)
156 tz->prev_low_trip = low;
157 tz->prev_high_trip = high;
160 "new temperature boundaries: %d < x < %d\n", low, high);
163 * Set a temperature window. When this window is left the driver
164 * must inform the thermal core via thermal_zone_device_update.
166 ret = tz->ops->set_trips(tz, low, high);
168 dev_err(&tz->device, "Failed to set trips: %d\n", ret);
172 * thermal_zone_set_trips - Computes the next trip points for the driver
173 * @tz: a pointer to a thermal zone device structure
175 * The function computes the next temperature boundaries by browsing
176 * the trip points. The result is the closer low and high trip points
177 * to the current temperature. These values are passed to the backend
178 * driver to let it set its own notification mechanism (usually an
181 * It does not return a value
183 void thermal_zone_set_trips(struct thermal_zone_device *tz)
185 mutex_lock(&tz->lock);
186 __thermal_zone_set_trips(tz);
187 mutex_unlock(&tz->lock);
190 static void thermal_cdev_set_cur_state(struct thermal_cooling_device *cdev,
193 if (cdev->ops->set_cur_state(cdev, target))
196 thermal_notify_cdev_state_update(cdev->id, target);
197 thermal_cooling_device_stats_update(cdev, target);
200 void __thermal_cdev_update(struct thermal_cooling_device *cdev)
202 struct thermal_instance *instance;
203 unsigned long target = 0;
205 /* Make sure cdev enters the deepest cooling state */
206 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
207 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
208 instance->tz->id, instance->target);
209 if (instance->target == THERMAL_NO_TARGET)
211 if (instance->target > target)
212 target = instance->target;
215 thermal_cdev_set_cur_state(cdev, target);
217 trace_cdev_update(cdev, target);
218 dev_dbg(&cdev->device, "set to state %lu\n", target);
222 * thermal_cdev_update - update cooling device state if needed
223 * @cdev: pointer to struct thermal_cooling_device
225 * Update the cooling device state if there is a need.
227 void thermal_cdev_update(struct thermal_cooling_device *cdev)
229 mutex_lock(&cdev->lock);
230 if (!cdev->updated) {
231 __thermal_cdev_update(cdev);
232 cdev->updated = true;
234 mutex_unlock(&cdev->lock);
238 * thermal_zone_get_slope - return the slope attribute of the thermal zone
239 * @tz: thermal zone device with the slope attribute
241 * Return: If the thermal zone device has a slope attribute, return it, else
244 int thermal_zone_get_slope(struct thermal_zone_device *tz)
247 return tz->tzp->slope;
250 EXPORT_SYMBOL_GPL(thermal_zone_get_slope);
253 * thermal_zone_get_offset - return the offset attribute of the thermal zone
254 * @tz: thermal zone device with the offset attribute
256 * Return: If the thermal zone device has a offset attribute, return it, else
259 int thermal_zone_get_offset(struct thermal_zone_device *tz)
262 return tz->tzp->offset;
265 EXPORT_SYMBOL_GPL(thermal_zone_get_offset);