1 // SPDX-License-Identifier: GPL-2.0
3 * thermal_helpers.c - helper functions to handle thermal devices
5 * Copyright (C) 2016 Eduardo Valentin <edubezval@gmail.com>
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>
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, int trip_index)
27 struct thermal_trip *trip = tz->trips ? &tz->trips[trip_index] : NULL;
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_index)
47 struct thermal_instance *pos = NULL;
48 struct thermal_instance *target_instance = NULL;
49 const struct thermal_trip *trip;
51 mutex_lock(&tz->lock);
52 mutex_lock(&cdev->lock);
54 trip = &tz->trips[trip_index];
56 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
57 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
58 target_instance = pos;
63 mutex_unlock(&cdev->lock);
64 mutex_unlock(&tz->lock);
66 return target_instance;
68 EXPORT_SYMBOL(get_thermal_instance);
71 * __thermal_zone_get_temp() - returns the temperature of a thermal zone
72 * @tz: a valid pointer to a struct thermal_zone_device
73 * @temp: a valid pointer to where to store the resulting temperature.
75 * When a valid thermal zone reference is passed, it will fetch its
76 * temperature and fill @temp.
78 * Both tz and tz->ops must be valid pointers when calling this function,
79 * and the tz->ops->get_temp callback must be provided.
80 * The function must be called under tz->lock.
82 * Return: On success returns 0, an error code otherwise
84 int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
88 int crit_temp = INT_MAX;
89 struct thermal_trip trip;
91 lockdep_assert_held(&tz->lock);
93 ret = tz->ops->get_temp(tz, temp);
95 if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) {
96 for (count = 0; count < tz->num_trips; count++) {
97 ret = __thermal_zone_get_trip(tz, count, &trip);
98 if (!ret && trip.type == THERMAL_TRIP_CRITICAL) {
99 crit_temp = trip.temperature;
105 * Only allow emulating a temperature when the real temperature
106 * is below the critical temperature so that the emulation code
107 * cannot hide critical conditions.
109 if (!ret && *temp < crit_temp)
110 *temp = tz->emul_temperature;
114 dev_dbg(&tz->device, "Failed to get temperature: %d\n", ret);
120 * thermal_zone_get_temp() - returns the temperature of a thermal zone
121 * @tz: a valid pointer to a struct thermal_zone_device
122 * @temp: a valid pointer to where to store the resulting temperature.
124 * When a valid thermal zone reference is passed, it will fetch its
125 * temperature and fill @temp.
127 * Return: On success returns 0, an error code otherwise
129 int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
133 if (IS_ERR_OR_NULL(tz))
136 mutex_lock(&tz->lock);
138 if (!tz->ops->get_temp) {
143 if (device_is_registered(&tz->device))
144 ret = __thermal_zone_get_temp(tz, temp);
149 mutex_unlock(&tz->lock);
153 EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
155 static void thermal_cdev_set_cur_state(struct thermal_cooling_device *cdev,
158 if (cdev->ops->set_cur_state(cdev, target))
161 thermal_notify_cdev_state_update(cdev->id, target);
162 thermal_cooling_device_stats_update(cdev, target);
165 void __thermal_cdev_update(struct thermal_cooling_device *cdev)
167 struct thermal_instance *instance;
168 unsigned long target = 0;
170 /* Make sure cdev enters the deepest cooling state */
171 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
172 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
173 instance->tz->id, instance->target);
174 if (instance->target == THERMAL_NO_TARGET)
176 if (instance->target > target)
177 target = instance->target;
180 thermal_cdev_set_cur_state(cdev, target);
182 trace_cdev_update(cdev, target);
183 dev_dbg(&cdev->device, "set to state %lu\n", target);
187 * thermal_cdev_update - update cooling device state if needed
188 * @cdev: pointer to struct thermal_cooling_device
190 * Update the cooling device state if there is a need.
192 void thermal_cdev_update(struct thermal_cooling_device *cdev)
194 mutex_lock(&cdev->lock);
195 if (!cdev->updated) {
196 __thermal_cdev_update(cdev);
197 cdev->updated = true;
199 mutex_unlock(&cdev->lock);
203 * thermal_zone_get_slope - return the slope attribute of the thermal zone
204 * @tz: thermal zone device with the slope attribute
206 * Return: If the thermal zone device has a slope attribute, return it, else
209 int thermal_zone_get_slope(struct thermal_zone_device *tz)
212 return tz->tzp->slope;
215 EXPORT_SYMBOL_GPL(thermal_zone_get_slope);
218 * thermal_zone_get_offset - return the offset attribute of the thermal zone
219 * @tz: thermal zone device with the offset attribute
221 * Return: If the thermal zone device has a offset attribute, return it, else
224 int thermal_zone_get_offset(struct thermal_zone_device *tz)
227 return tz->tzp->offset;
230 EXPORT_SYMBOL_GPL(thermal_zone_get_offset);