Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / thermal / thermal_helpers.c
blob8cdf75adcce16a56654a1036272008fa6b7a675d
1 /*
2 * thermal_helpers.c - helper functions to handle thermal devices
4 * Copyright (C) 2016 Eduardo Valentin <edubezval@gmail.com>
6 * Highly based on original thermal_core.c
7 * Copyright (C) 2008 Intel Corp
8 * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
9 * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License.
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/sysfs.h>
19 #include <linux/device.h>
20 #include <linux/err.h>
21 #include <linux/slab.h>
22 #include <linux/string.h>
24 #include <trace/events/thermal.h>
26 #include "thermal_core.h"
28 int get_tz_trend(struct thermal_zone_device *tz, int trip)
30 enum thermal_trend trend;
32 if (tz->emul_temperature || !tz->ops->get_trend ||
33 tz->ops->get_trend(tz, trip, &trend)) {
34 if (tz->temperature > tz->last_temperature)
35 trend = THERMAL_TREND_RAISING;
36 else if (tz->temperature < tz->last_temperature)
37 trend = THERMAL_TREND_DROPPING;
38 else
39 trend = THERMAL_TREND_STABLE;
42 return trend;
44 EXPORT_SYMBOL(get_tz_trend);
46 struct thermal_instance *
47 get_thermal_instance(struct thermal_zone_device *tz,
48 struct thermal_cooling_device *cdev, int trip)
50 struct thermal_instance *pos = NULL;
51 struct thermal_instance *target_instance = NULL;
53 mutex_lock(&tz->lock);
54 mutex_lock(&cdev->lock);
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;
59 break;
63 mutex_unlock(&cdev->lock);
64 mutex_unlock(&tz->lock);
66 return target_instance;
68 EXPORT_SYMBOL(get_thermal_instance);
70 /**
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 * Return: On success returns 0, an error code otherwise
80 int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
82 int ret = -EINVAL;
83 int count;
84 int crit_temp = INT_MAX;
85 enum thermal_trip_type type;
87 if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
88 goto exit;
90 mutex_lock(&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->trips; count++) {
96 ret = tz->ops->get_trip_type(tz, count, &type);
97 if (!ret && type == THERMAL_TRIP_CRITICAL) {
98 ret = tz->ops->get_trip_temp(tz, count,
99 &crit_temp);
100 break;
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;
113 mutex_unlock(&tz->lock);
114 exit:
115 return ret;
117 EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
119 void thermal_zone_set_trips(struct thermal_zone_device *tz)
121 int low = -INT_MAX;
122 int high = INT_MAX;
123 int trip_temp, hysteresis;
124 int i, ret;
126 mutex_lock(&tz->lock);
128 if (!tz->ops->set_trips || !tz->ops->get_trip_hyst)
129 goto exit;
131 for (i = 0; i < tz->trips; i++) {
132 int trip_low;
134 tz->ops->get_trip_temp(tz, i, &trip_temp);
135 tz->ops->get_trip_hyst(tz, i, &hysteresis);
137 trip_low = trip_temp - hysteresis;
139 if (trip_low < tz->temperature && trip_low > low)
140 low = trip_low;
142 if (trip_temp > tz->temperature && trip_temp < high)
143 high = trip_temp;
146 /* No need to change trip points */
147 if (tz->prev_low_trip == low && tz->prev_high_trip == high)
148 goto exit;
150 tz->prev_low_trip = low;
151 tz->prev_high_trip = high;
153 dev_dbg(&tz->device,
154 "new temperature boundaries: %d < x < %d\n", low, high);
157 * Set a temperature window. When this window is left the driver
158 * must inform the thermal core via thermal_zone_device_update.
160 ret = tz->ops->set_trips(tz, low, high);
161 if (ret)
162 dev_err(&tz->device, "Failed to set trips: %d\n", ret);
164 exit:
165 mutex_unlock(&tz->lock);
167 EXPORT_SYMBOL_GPL(thermal_zone_set_trips);
169 void thermal_cdev_update(struct thermal_cooling_device *cdev)
171 struct thermal_instance *instance;
172 unsigned long target = 0;
174 mutex_lock(&cdev->lock);
175 /* cooling device is updated*/
176 if (cdev->updated) {
177 mutex_unlock(&cdev->lock);
178 return;
181 /* Make sure cdev enters the deepest cooling state */
182 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
183 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
184 instance->tz->id, instance->target);
185 if (instance->target == THERMAL_NO_TARGET)
186 continue;
187 if (instance->target > target)
188 target = instance->target;
190 cdev->ops->set_cur_state(cdev, target);
191 cdev->updated = true;
192 mutex_unlock(&cdev->lock);
193 trace_cdev_update(cdev, target);
194 dev_dbg(&cdev->device, "set to state %lu\n", target);
196 EXPORT_SYMBOL(thermal_cdev_update);
199 * thermal_zone_get_slope - return the slope attribute of the thermal zone
200 * @tz: thermal zone device with the slope attribute
202 * Return: If the thermal zone device has a slope attribute, return it, else
203 * return 1.
205 int thermal_zone_get_slope(struct thermal_zone_device *tz)
207 if (tz && tz->tzp)
208 return tz->tzp->slope;
209 return 1;
211 EXPORT_SYMBOL_GPL(thermal_zone_get_slope);
214 * thermal_zone_get_offset - return the offset attribute of the thermal zone
215 * @tz: thermal zone device with the offset attribute
217 * Return: If the thermal zone device has a offset attribute, return it, else
218 * return 0.
220 int thermal_zone_get_offset(struct thermal_zone_device *tz)
222 if (tz && tz->tzp)
223 return tz->tzp->offset;
224 return 0;
226 EXPORT_SYMBOL_GPL(thermal_zone_get_offset);