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 "thermal_core.h"
23 #include "thermal_trace.h"
25 int get_tz_trend(struct thermal_zone_device *tz, const struct thermal_trip *trip)
27 enum thermal_trend trend;
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;
36 trend = THERMAL_TREND_STABLE;
42 struct thermal_instance *
43 get_thermal_instance(struct thermal_zone_device *tz,
44 struct thermal_cooling_device *cdev, int trip_index)
46 struct thermal_instance *pos = NULL;
47 struct thermal_instance *target_instance = NULL;
48 const struct thermal_trip *trip;
50 mutex_lock(&tz->lock);
51 mutex_lock(&cdev->lock);
53 trip = &tz->trips[trip_index];
55 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
56 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
57 target_instance = pos;
62 mutex_unlock(&cdev->lock);
63 mutex_unlock(&tz->lock);
65 return target_instance;
67 EXPORT_SYMBOL(get_thermal_instance);
70 * __thermal_zone_get_temp() - returns the temperature of a thermal zone
71 * @tz: a valid pointer to a struct thermal_zone_device
72 * @temp: a valid pointer to where to store the resulting temperature.
74 * When a valid thermal zone reference is passed, it will fetch its
75 * temperature and fill @temp.
77 * Both tz and tz->ops must be valid pointers when calling this function,
78 * and the tz->ops->get_temp callback must be provided.
79 * The function must be called under tz->lock.
81 * Return: On success returns 0, an error code otherwise
83 int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
87 int crit_temp = INT_MAX;
88 struct thermal_trip trip;
90 lockdep_assert_held(&tz->lock);
92 ret = tz->ops->get_temp(tz, temp);
94 if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) {
95 for (count = 0; count < tz->num_trips; count++) {
96 ret = __thermal_zone_get_trip(tz, count, &trip);
97 if (!ret && trip.type == THERMAL_TRIP_CRITICAL) {
98 crit_temp = trip.temperature;
104 * Only allow emulating a temperature when the real temperature
105 * is below the critical temperature so that the emulation code
106 * cannot hide critical conditions.
108 if (!ret && *temp < crit_temp)
109 *temp = tz->emul_temperature;
113 dev_dbg(&tz->device, "Failed to get temperature: %d\n", ret);
119 * thermal_zone_get_temp() - returns the temperature of a thermal zone
120 * @tz: a valid pointer to a struct thermal_zone_device
121 * @temp: a valid pointer to where to store the resulting temperature.
123 * When a valid thermal zone reference is passed, it will fetch its
124 * temperature and fill @temp.
126 * Return: On success returns 0, an error code otherwise
128 int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
132 if (IS_ERR_OR_NULL(tz))
135 mutex_lock(&tz->lock);
137 if (!tz->ops->get_temp) {
142 if (device_is_registered(&tz->device))
143 ret = __thermal_zone_get_temp(tz, temp);
148 mutex_unlock(&tz->lock);
152 EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
154 static void thermal_cdev_set_cur_state(struct thermal_cooling_device *cdev,
157 if (cdev->ops->set_cur_state(cdev, target))
160 thermal_notify_cdev_state_update(cdev->id, target);
161 thermal_cooling_device_stats_update(cdev, target);
164 void __thermal_cdev_update(struct thermal_cooling_device *cdev)
166 struct thermal_instance *instance;
167 unsigned long target = 0;
169 /* Make sure cdev enters the deepest cooling state */
170 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
171 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
172 instance->tz->id, instance->target);
173 if (instance->target == THERMAL_NO_TARGET)
175 if (instance->target > target)
176 target = instance->target;
179 thermal_cdev_set_cur_state(cdev, target);
181 trace_cdev_update(cdev, target);
182 dev_dbg(&cdev->device, "set to state %lu\n", target);
186 * thermal_cdev_update - update cooling device state if needed
187 * @cdev: pointer to struct thermal_cooling_device
189 * Update the cooling device state if there is a need.
191 void thermal_cdev_update(struct thermal_cooling_device *cdev)
193 mutex_lock(&cdev->lock);
194 if (!cdev->updated) {
195 __thermal_cdev_update(cdev);
196 cdev->updated = true;
198 mutex_unlock(&cdev->lock);
202 * thermal_zone_get_slope - return the slope attribute of the thermal zone
203 * @tz: thermal zone device with the slope attribute
205 * Return: If the thermal zone device has a slope attribute, return it, else
208 int thermal_zone_get_slope(struct thermal_zone_device *tz)
211 return tz->tzp->slope;
214 EXPORT_SYMBOL_GPL(thermal_zone_get_slope);
217 * thermal_zone_get_offset - return the offset attribute of the thermal zone
218 * @tz: thermal zone device with the offset attribute
220 * Return: If the thermal zone device has a offset attribute, return it, else
223 int thermal_zone_get_offset(struct thermal_zone_device *tz)
226 return tz->tzp->offset;
229 EXPORT_SYMBOL_GPL(thermal_zone_get_offset);