xref: /linux/drivers/thermal/thermal_helpers.c (revision 46e6acfe3501fa938af9c5bd730f0020235b08a2)
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 "thermal_core.h"
23 #include "thermal_trace.h"
24 
25 int get_tz_trend(struct thermal_zone_device *tz, const struct thermal_trip *trip)
26 {
27 	enum thermal_trend trend;
28 
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;
35 		else
36 			trend = THERMAL_TREND_STABLE;
37 	}
38 
39 	return trend;
40 }
41 
42 static struct thermal_instance *get_instance(struct thermal_zone_device *tz,
43 					     struct thermal_cooling_device *cdev,
44 					     const struct thermal_trip *trip)
45 {
46 	struct thermal_instance *ti;
47 
48 	list_for_each_entry(ti, &tz->thermal_instances, tz_node) {
49 		if (ti->trip == trip && ti->cdev == cdev)
50 			return ti;
51 	}
52 
53 	return NULL;
54 }
55 
56 bool thermal_trip_is_bound_to_cdev(struct thermal_zone_device *tz,
57 				   const struct thermal_trip *trip,
58 				   struct thermal_cooling_device *cdev)
59 {
60 	bool ret;
61 
62 	mutex_lock(&tz->lock);
63 	mutex_lock(&cdev->lock);
64 
65 	ret = !!get_instance(tz, cdev, trip);
66 
67 	mutex_unlock(&cdev->lock);
68 	mutex_unlock(&tz->lock);
69 
70 	return ret;
71 }
72 EXPORT_SYMBOL_GPL(thermal_trip_is_bound_to_cdev);
73 
74 struct thermal_instance *
75 get_thermal_instance(struct thermal_zone_device *tz,
76 		     struct thermal_cooling_device *cdev, int trip_index)
77 {
78 	struct thermal_instance *ti;
79 
80 	mutex_lock(&tz->lock);
81 	mutex_lock(&cdev->lock);
82 
83 	ti = get_instance(tz, cdev, &tz->trips[trip_index].trip);
84 
85 	mutex_unlock(&cdev->lock);
86 	mutex_unlock(&tz->lock);
87 
88 	return ti;
89 }
90 EXPORT_SYMBOL(get_thermal_instance);
91 
92 /**
93  * __thermal_zone_get_temp() - returns the temperature of a thermal zone
94  * @tz: a valid pointer to a struct thermal_zone_device
95  * @temp: a valid pointer to where to store the resulting temperature.
96  *
97  * When a valid thermal zone reference is passed, it will fetch its
98  * temperature and fill @temp.
99  *
100  * Both tz and tz->ops must be valid pointers when calling this function,
101  * and the tz->ops.get_temp callback must be provided.
102  * The function must be called under tz->lock.
103  *
104  * Return: On success returns 0, an error code otherwise
105  */
106 int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
107 {
108 	const struct thermal_trip_desc *td;
109 	int crit_temp = INT_MAX;
110 	int ret = -EINVAL;
111 
112 	lockdep_assert_held(&tz->lock);
113 
114 	ret = tz->ops.get_temp(tz, temp);
115 
116 	if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) {
117 		for_each_trip_desc(tz, td) {
118 			const struct thermal_trip *trip = &td->trip;
119 
120 			if (trip->type == THERMAL_TRIP_CRITICAL) {
121 				crit_temp = trip->temperature;
122 				break;
123 			}
124 		}
125 
126 		/*
127 		 * Only allow emulating a temperature when the real temperature
128 		 * is below the critical temperature so that the emulation code
129 		 * cannot hide critical conditions.
130 		 */
131 		if (!ret && *temp < crit_temp)
132 			*temp = tz->emul_temperature;
133 	}
134 
135 	if (ret)
136 		dev_dbg(&tz->device, "Failed to get temperature: %d\n", ret);
137 
138 	return ret;
139 }
140 
141 /**
142  * thermal_zone_get_temp() - returns the temperature of a thermal zone
143  * @tz: a valid pointer to a struct thermal_zone_device
144  * @temp: a valid pointer to where to store the resulting temperature.
145  *
146  * When a valid thermal zone reference is passed, it will fetch its
147  * temperature and fill @temp.
148  *
149  * Return: On success returns 0, an error code otherwise
150  */
151 int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
152 {
153 	int ret;
154 
155 	if (IS_ERR_OR_NULL(tz))
156 		return -EINVAL;
157 
158 	mutex_lock(&tz->lock);
159 
160 	if (!tz->ops.get_temp) {
161 		ret = -EINVAL;
162 		goto unlock;
163 	}
164 
165 	ret = __thermal_zone_get_temp(tz, temp);
166 	if (!ret && *temp <= THERMAL_TEMP_INVALID)
167 		ret = -ENODATA;
168 
169 unlock:
170 	mutex_unlock(&tz->lock);
171 
172 	return ret;
173 }
174 EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
175 
176 static int thermal_cdev_set_cur_state(struct thermal_cooling_device *cdev, int state)
177 {
178 	int ret;
179 
180 	/*
181 	 * No check is needed for the ops->set_cur_state as the
182 	 * registering function checked the ops are correctly set
183 	 */
184 	ret = cdev->ops->set_cur_state(cdev, state);
185 	if (ret)
186 		return ret;
187 
188 	thermal_notify_cdev_state_update(cdev, state);
189 	thermal_cooling_device_stats_update(cdev, state);
190 	thermal_debug_cdev_state_update(cdev, state);
191 
192 	return 0;
193 }
194 
195 void __thermal_cdev_update(struct thermal_cooling_device *cdev)
196 {
197 	struct thermal_instance *instance;
198 	unsigned long target = 0;
199 
200 	/* Make sure cdev enters the deepest cooling state */
201 	list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
202 		dev_dbg(&cdev->device, "zone%d->target=%lu\n",
203 			instance->tz->id, instance->target);
204 		if (instance->target == THERMAL_NO_TARGET)
205 			continue;
206 		if (instance->target > target)
207 			target = instance->target;
208 	}
209 
210 	thermal_cdev_set_cur_state(cdev, target);
211 
212 	trace_cdev_update(cdev, target);
213 	dev_dbg(&cdev->device, "set to state %lu\n", target);
214 }
215 
216 /**
217  * thermal_cdev_update - update cooling device state if needed
218  * @cdev:	pointer to struct thermal_cooling_device
219  *
220  * Update the cooling device state if there is a need.
221  */
222 void thermal_cdev_update(struct thermal_cooling_device *cdev)
223 {
224 	mutex_lock(&cdev->lock);
225 	if (!cdev->updated) {
226 		__thermal_cdev_update(cdev);
227 		cdev->updated = true;
228 	}
229 	mutex_unlock(&cdev->lock);
230 }
231 
232 /**
233  * thermal_zone_get_slope - return the slope attribute of the thermal zone
234  * @tz: thermal zone device with the slope attribute
235  *
236  * Return: If the thermal zone device has a slope attribute, return it, else
237  * return 1.
238  */
239 int thermal_zone_get_slope(struct thermal_zone_device *tz)
240 {
241 	if (tz && tz->tzp)
242 		return tz->tzp->slope;
243 	return 1;
244 }
245 EXPORT_SYMBOL_GPL(thermal_zone_get_slope);
246 
247 /**
248  * thermal_zone_get_offset - return the offset attribute of the thermal zone
249  * @tz: thermal zone device with the offset attribute
250  *
251  * Return: If the thermal zone device has a offset attribute, return it, else
252  * return 0.
253  */
254 int thermal_zone_get_offset(struct thermal_zone_device *tz)
255 {
256 	if (tz && tz->tzp)
257 		return tz->tzp->offset;
258 	return 0;
259 }
260 EXPORT_SYMBOL_GPL(thermal_zone_get_offset);
261