]>
Commit | Line | Data |
---|---|---|
7e3c0381 | 1 | // SPDX-License-Identifier: GPL-2.0 |
cd221c7b EV |
2 | /* |
3 | * thermal_helpers.c - helper functions to handle thermal devices | |
4 | * | |
5 | * Copyright (C) 2016 Eduardo Valentin <[email protected]> | |
6 | * | |
7 | * Highly based on original thermal_core.c | |
8 | * Copyright (C) 2008 Intel Corp | |
9 | * Copyright (C) 2008 Zhang Rui <[email protected]> | |
10 | * Copyright (C) 2008 Sujith Thomas <[email protected]> | |
cd221c7b EV |
11 | */ |
12 | ||
13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
14 | ||
cd221c7b EV |
15 | #include <linux/device.h> |
16 | #include <linux/err.h> | |
3a74c882 | 17 | #include <linux/export.h> |
cd221c7b EV |
18 | #include <linux/slab.h> |
19 | #include <linux/string.h> | |
231b98af | 20 | #include <linux/sysfs.h> |
cd221c7b | 21 | |
cd221c7b | 22 | #include "thermal_core.h" |
32a7a021 | 23 | #include "thermal_trace.h" |
cd221c7b | 24 | |
8c35b1f4 | 25 | int get_tz_trend(struct thermal_zone_device *tz, const struct thermal_trip *trip) |
cd221c7b EV |
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 | } | |
cd221c7b EV |
41 | |
42 | struct thermal_instance * | |
43 | get_thermal_instance(struct thermal_zone_device *tz, | |
2c7b4bfa | 44 | struct thermal_cooling_device *cdev, int trip_index) |
cd221c7b EV |
45 | { |
46 | struct thermal_instance *pos = NULL; | |
47 | struct thermal_instance *target_instance = NULL; | |
2c7b4bfa | 48 | const struct thermal_trip *trip; |
cd221c7b EV |
49 | |
50 | mutex_lock(&tz->lock); | |
51 | mutex_lock(&cdev->lock); | |
52 | ||
2c7b4bfa RW |
53 | trip = &tz->trips[trip_index]; |
54 | ||
cd221c7b EV |
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; | |
58 | break; | |
59 | } | |
60 | } | |
61 | ||
62 | mutex_unlock(&cdev->lock); | |
63 | mutex_unlock(&tz->lock); | |
64 | ||
65 | return target_instance; | |
66 | } | |
67 | EXPORT_SYMBOL(get_thermal_instance); | |
68 | ||
ed97d10a GR |
69 | /** |
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. | |
73 | * | |
74 | * When a valid thermal zone reference is passed, it will fetch its | |
75 | * temperature and fill @temp. | |
76 | * | |
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. | |
80 | * | |
81 | * Return: On success returns 0, an error code otherwise | |
82 | */ | |
a930da9b | 83 | int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp) |
cd221c7b | 84 | { |
2e3e7dad | 85 | const struct thermal_trip *trip; |
cd221c7b | 86 | int crit_temp = INT_MAX; |
2e3e7dad | 87 | int ret = -EINVAL; |
cd221c7b | 88 | |
a930da9b | 89 | lockdep_assert_held(&tz->lock); |
cd221c7b | 90 | |
cd221c7b EV |
91 | ret = tz->ops->get_temp(tz, temp); |
92 | ||
93 | if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) { | |
2e3e7dad RW |
94 | for_each_trip(tz, trip) { |
95 | if (trip->type == THERMAL_TRIP_CRITICAL) { | |
96 | crit_temp = trip->temperature; | |
cd221c7b EV |
97 | break; |
98 | } | |
99 | } | |
100 | ||
101 | /* | |
102 | * Only allow emulating a temperature when the real temperature | |
103 | * is below the critical temperature so that the emulation code | |
104 | * cannot hide critical conditions. | |
105 | */ | |
106 | if (!ret && *temp < crit_temp) | |
107 | *temp = tz->emul_temperature; | |
108 | } | |
109 | ||
4216e815 DL |
110 | if (ret) |
111 | dev_dbg(&tz->device, "Failed to get temperature: %d\n", ret); | |
112 | ||
cd221c7b EV |
113 | return ret; |
114 | } | |
cd221c7b | 115 | |
bceb5646 | 116 | /** |
a930da9b DL |
117 | * thermal_zone_get_temp() - returns the temperature of a thermal zone |
118 | * @tz: a valid pointer to a struct thermal_zone_device | |
119 | * @temp: a valid pointer to where to store the resulting temperature. | |
bceb5646 | 120 | * |
a930da9b DL |
121 | * When a valid thermal zone reference is passed, it will fetch its |
122 | * temperature and fill @temp. | |
bceb5646 | 123 | * |
a930da9b | 124 | * Return: On success returns 0, an error code otherwise |
bceb5646 | 125 | */ |
a930da9b DL |
126 | int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp) |
127 | { | |
128 | int ret; | |
129 | ||
ed97d10a GR |
130 | if (IS_ERR_OR_NULL(tz)) |
131 | return -EINVAL; | |
132 | ||
a930da9b | 133 | mutex_lock(&tz->lock); |
1c6b3006 | 134 | |
ed97d10a GR |
135 | if (!tz->ops->get_temp) { |
136 | ret = -EINVAL; | |
137 | goto unlock; | |
138 | } | |
139 | ||
c3ffdfff | 140 | ret = __thermal_zone_get_temp(tz, temp); |
1c6b3006 | 141 | |
ed97d10a | 142 | unlock: |
a930da9b DL |
143 | mutex_unlock(&tz->lock); |
144 | ||
145 | return ret; | |
146 | } | |
147 | EXPORT_SYMBOL_GPL(thermal_zone_get_temp); | |
148 | ||
55cdf0a2 DL |
149 | static void thermal_cdev_set_cur_state(struct thermal_cooling_device *cdev, |
150 | int target) | |
151 | { | |
152 | if (cdev->ops->set_cur_state(cdev, target)) | |
153 | return; | |
154 | ||
155 | thermal_notify_cdev_state_update(cdev->id, target); | |
156 | thermal_cooling_device_stats_update(cdev, target); | |
157 | } | |
158 | ||
b70dbf40 | 159 | void __thermal_cdev_update(struct thermal_cooling_device *cdev) |
cd221c7b EV |
160 | { |
161 | struct thermal_instance *instance; | |
162 | unsigned long target = 0; | |
163 | ||
cd221c7b EV |
164 | /* Make sure cdev enters the deepest cooling state */ |
165 | list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) { | |
166 | dev_dbg(&cdev->device, "zone%d->target=%lu\n", | |
167 | instance->tz->id, instance->target); | |
168 | if (instance->target == THERMAL_NO_TARGET) | |
169 | continue; | |
170 | if (instance->target > target) | |
171 | target = instance->target; | |
172 | } | |
8ea22951 | 173 | |
55cdf0a2 | 174 | thermal_cdev_set_cur_state(cdev, target); |
8ea22951 | 175 | |
cd221c7b EV |
176 | trace_cdev_update(cdev, target); |
177 | dev_dbg(&cdev->device, "set to state %lu\n", target); | |
178 | } | |
b70dbf40 LL |
179 | |
180 | /** | |
181 | * thermal_cdev_update - update cooling device state if needed | |
182 | * @cdev: pointer to struct thermal_cooling_device | |
183 | * | |
184 | * Update the cooling device state if there is a need. | |
185 | */ | |
186 | void thermal_cdev_update(struct thermal_cooling_device *cdev) | |
187 | { | |
188 | mutex_lock(&cdev->lock); | |
189 | if (!cdev->updated) { | |
190 | __thermal_cdev_update(cdev); | |
191 | cdev->updated = true; | |
192 | } | |
193 | mutex_unlock(&cdev->lock); | |
194 | } | |
373f91d1 EV |
195 | |
196 | /** | |
197 | * thermal_zone_get_slope - return the slope attribute of the thermal zone | |
198 | * @tz: thermal zone device with the slope attribute | |
199 | * | |
200 | * Return: If the thermal zone device has a slope attribute, return it, else | |
201 | * return 1. | |
202 | */ | |
203 | int thermal_zone_get_slope(struct thermal_zone_device *tz) | |
204 | { | |
205 | if (tz && tz->tzp) | |
206 | return tz->tzp->slope; | |
207 | return 1; | |
208 | } | |
209 | EXPORT_SYMBOL_GPL(thermal_zone_get_slope); | |
210 | ||
211 | /** | |
212 | * thermal_zone_get_offset - return the offset attribute of the thermal zone | |
213 | * @tz: thermal zone device with the offset attribute | |
214 | * | |
215 | * Return: If the thermal zone device has a offset attribute, return it, else | |
216 | * return 0. | |
217 | */ | |
218 | int thermal_zone_get_offset(struct thermal_zone_device *tz) | |
219 | { | |
220 | if (tz && tz->tzp) | |
221 | return tz->tzp->offset; | |
222 | return 0; | |
223 | } | |
224 | EXPORT_SYMBOL_GPL(thermal_zone_get_offset); |