• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
get_tz_trend(struct thermal_zone_device * tz,int trip)26 int 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 
43 struct thermal_instance *
get_thermal_instance(struct thermal_zone_device * tz,struct thermal_cooling_device * cdev,int trip)44 get_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 }
65 EXPORT_SYMBOL(get_thermal_instance);
66 
__thermal_zone_get_temp(struct thermal_zone_device * tz,int * temp)67 int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
68 {
69 	int ret = -EINVAL;
70 	int count;
71 	int crit_temp = INT_MAX;
72 	enum thermal_trip_type type;
73 
74 	lockdep_assert_held(&tz->lock);
75 
76 	if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
77 		return -EINVAL;
78 
79 	ret = tz->ops->get_temp(tz, temp);
80 
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,
86 						&crit_temp);
87 				break;
88 			}
89 		}
90 
91 		/*
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.
95 		 */
96 		if (!ret && *temp < crit_temp)
97 			*temp = tz->emul_temperature;
98 	}
99 
100 	return ret;
101 }
102 
103 /**
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.
107  *
108  * When a valid thermal zone reference is passed, it will fetch its
109  * temperature and fill @temp.
110  *
111  * Return: On success returns 0, an error code otherwise
112  */
thermal_zone_get_temp(struct thermal_zone_device * tz,int * temp)113 int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
114 {
115 	int ret;
116 
117 	mutex_lock(&tz->lock);
118 
119 	if (device_is_registered(&tz->device))
120 		ret = __thermal_zone_get_temp(tz, temp);
121 	else
122 		ret = -ENODEV;
123 
124 	mutex_unlock(&tz->lock);
125 
126 	return ret;
127 }
128 EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
129 
__thermal_zone_set_trips(struct thermal_zone_device * tz)130 void __thermal_zone_set_trips(struct thermal_zone_device *tz)
131 {
132 	int low = -INT_MAX;
133 	int high = INT_MAX;
134 	int trip_temp, hysteresis;
135 	int i, ret;
136 
137 	lockdep_assert_held(&tz->lock);
138 
139 	if (!tz->ops->set_trips || !tz->ops->get_trip_hyst)
140 		return;
141 
142 	for (i = 0; i < tz->num_trips; i++) {
143 		int trip_low;
144 
145 		tz->ops->get_trip_temp(tz, i, &trip_temp);
146 		tz->ops->get_trip_hyst(tz, i, &hysteresis);
147 
148 		trip_low = trip_temp - hysteresis;
149 
150 		if (trip_low < tz->temperature && trip_low > low)
151 			low = trip_low;
152 
153 		if (trip_temp > tz->temperature && trip_temp < high)
154 			high = trip_temp;
155 	}
156 
157 	/* No need to change trip points */
158 	if (tz->prev_low_trip == low && tz->prev_high_trip == high)
159 		return;
160 
161 	tz->prev_low_trip = low;
162 	tz->prev_high_trip = high;
163 
164 	dev_dbg(&tz->device,
165 		"new temperature boundaries: %d < x < %d\n", low, high);
166 
167 	/*
168 	 * Set a temperature window. When this window is left the driver
169 	 * must inform the thermal core via thermal_zone_device_update.
170 	 */
171 	ret = tz->ops->set_trips(tz, low, high);
172 	if (ret)
173 		dev_err(&tz->device, "Failed to set trips: %d\n", ret);
174 }
175 
176 /**
177  * thermal_zone_set_trips - Computes the next trip points for the driver
178  * @tz: a pointer to a thermal zone device structure
179  *
180  * The function computes the next temperature boundaries by browsing
181  * the trip points. The result is the closer low and high trip points
182  * to the current temperature. These values are passed to the backend
183  * driver to let it set its own notification mechanism (usually an
184  * interrupt).
185  *
186  * It does not return a value
187  */
thermal_zone_set_trips(struct thermal_zone_device * tz)188 void thermal_zone_set_trips(struct thermal_zone_device *tz)
189 {
190 	mutex_lock(&tz->lock);
191 	__thermal_zone_set_trips(tz);
192 	mutex_unlock(&tz->lock);
193 }
194 
thermal_cdev_set_cur_state(struct thermal_cooling_device * cdev,int target)195 static void thermal_cdev_set_cur_state(struct thermal_cooling_device *cdev,
196 				       int target)
197 {
198 	if (cdev->ops->set_cur_state(cdev, target))
199 		return;
200 
201 	thermal_notify_cdev_state_update(cdev->id, target);
202 	thermal_cooling_device_stats_update(cdev, target);
203 }
204 
__thermal_cdev_update(struct thermal_cooling_device * cdev)205 void __thermal_cdev_update(struct thermal_cooling_device *cdev)
206 {
207 	struct thermal_instance *instance;
208 	unsigned long target = 0;
209 
210 	/* Make sure cdev enters the deepest cooling state */
211 	list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
212 		dev_dbg(&cdev->device, "zone%d->target=%lu\n",
213 			instance->tz->id, instance->target);
214 		if (instance->target == THERMAL_NO_TARGET)
215 			continue;
216 		if (instance->target > target)
217 			target = instance->target;
218 	}
219 
220 	thermal_cdev_set_cur_state(cdev, target);
221 
222 	trace_cdev_update(cdev, target);
223 	dev_dbg(&cdev->device, "set to state %lu\n", target);
224 }
225 
226 /**
227  * thermal_cdev_update - update cooling device state if needed
228  * @cdev:	pointer to struct thermal_cooling_device
229  *
230  * Update the cooling device state if there is a need.
231  */
thermal_cdev_update(struct thermal_cooling_device * cdev)232 void thermal_cdev_update(struct thermal_cooling_device *cdev)
233 {
234 	mutex_lock(&cdev->lock);
235 	if (!cdev->updated) {
236 		__thermal_cdev_update(cdev);
237 		cdev->updated = true;
238 	}
239 	mutex_unlock(&cdev->lock);
240 }
241 EXPORT_SYMBOL_GPL(thermal_cdev_update);
242 
243 /**
244  * thermal_zone_get_slope - return the slope attribute of the thermal zone
245  * @tz: thermal zone device with the slope attribute
246  *
247  * Return: If the thermal zone device has a slope attribute, return it, else
248  * return 1.
249  */
thermal_zone_get_slope(struct thermal_zone_device * tz)250 int thermal_zone_get_slope(struct thermal_zone_device *tz)
251 {
252 	if (tz && tz->tzp)
253 		return tz->tzp->slope;
254 	return 1;
255 }
256 EXPORT_SYMBOL_GPL(thermal_zone_get_slope);
257 
258 /**
259  * thermal_zone_get_offset - return the offset attribute of the thermal zone
260  * @tz: thermal zone device with the offset attribute
261  *
262  * Return: If the thermal zone device has a offset attribute, return it, else
263  * return 0.
264  */
thermal_zone_get_offset(struct thermal_zone_device * tz)265 int thermal_zone_get_offset(struct thermal_zone_device *tz)
266 {
267 	if (tz && tz->tzp)
268 		return tz->tzp->offset;
269 	return 0;
270 }
271 EXPORT_SYMBOL_GPL(thermal_zone_get_offset);
272