Commit | Line | Data |
---|---|---|
445eaf87 EV |
1 | /* |
2 | * OMAP thermal driver interface | |
3 | * | |
4 | * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/ | |
5 | * Contact: | |
6 | * Eduardo Valentin <eduardo.valentin@ti.com> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public License | |
10 | * version 2 as published by the Free Software Foundation. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
20 | * 02110-1301 USA | |
21 | * | |
22 | */ | |
23 | ||
24 | #include <linux/device.h> | |
25 | #include <linux/err.h> | |
26 | #include <linux/mutex.h> | |
27 | #include <linux/gfp.h> | |
28 | #include <linux/kernel.h> | |
29 | #include <linux/workqueue.h> | |
30 | #include <linux/thermal.h> | |
5035d48d | 31 | #include <linux/cpumask.h> |
445eaf87 | 32 | #include <linux/cpu_cooling.h> |
26d9cc65 | 33 | #include <linux/of.h> |
445eaf87 | 34 | |
7372add4 EV |
35 | #include "ti-thermal.h" |
36 | #include "ti-bandgap.h" | |
445eaf87 EV |
37 | |
38 | /* common data structures */ | |
03e859d3 EV |
39 | struct ti_thermal_data { |
40 | struct thermal_zone_device *ti_thermal; | |
359836e1 | 41 | struct thermal_zone_device *pcb_tz; |
445eaf87 | 42 | struct thermal_cooling_device *cool_dev; |
03e859d3 | 43 | struct ti_bandgap *bgp; |
445eaf87 EV |
44 | enum thermal_device_mode mode; |
45 | struct work_struct thermal_wq; | |
46 | int sensor_id; | |
26d9cc65 | 47 | bool our_zone; |
445eaf87 EV |
48 | }; |
49 | ||
03e859d3 | 50 | static void ti_thermal_work(struct work_struct *work) |
445eaf87 | 51 | { |
03e859d3 EV |
52 | struct ti_thermal_data *data = container_of(work, |
53 | struct ti_thermal_data, thermal_wq); | |
445eaf87 | 54 | |
03e859d3 | 55 | thermal_zone_device_update(data->ti_thermal); |
445eaf87 | 56 | |
03e859d3 EV |
57 | dev_dbg(&data->ti_thermal->device, "updated thermal zone %s\n", |
58 | data->ti_thermal->type); | |
445eaf87 EV |
59 | } |
60 | ||
61 | /** | |
03e859d3 | 62 | * ti_thermal_hotspot_temperature - returns sensor extrapolated temperature |
445eaf87 EV |
63 | * @t: omap sensor temperature |
64 | * @s: omap sensor slope value | |
65 | * @c: omap sensor const value | |
66 | */ | |
03e859d3 | 67 | static inline int ti_thermal_hotspot_temperature(int t, int s, int c) |
445eaf87 EV |
68 | { |
69 | int delta = t * s / 1000 + c; | |
70 | ||
71 | if (delta < 0) | |
72 | delta = 0; | |
73 | ||
74 | return t + delta; | |
75 | } | |
76 | ||
77 | /* thermal zone ops */ | |
e34238bf | 78 | /* Get temperature callback function for thermal zone */ |
17e8351a | 79 | static inline int __ti_thermal_get_temp(void *devdata, int *temp) |
445eaf87 | 80 | { |
359836e1 | 81 | struct thermal_zone_device *pcb_tz = NULL; |
26d9cc65 | 82 | struct ti_thermal_data *data = devdata; |
03e859d3 | 83 | struct ti_bandgap *bgp; |
9879b2c4 | 84 | const struct ti_temp_sensor *s; |
359836e1 | 85 | int ret, tmp, slope, constant; |
17e8351a | 86 | int pcb_temp; |
445eaf87 | 87 | |
04a4d10d EV |
88 | if (!data) |
89 | return 0; | |
90 | ||
d7f080e6 EV |
91 | bgp = data->bgp; |
92 | s = &bgp->conf->sensors[data->sensor_id]; | |
04a4d10d | 93 | |
03e859d3 | 94 | ret = ti_bandgap_read_temperature(bgp, data->sensor_id, &tmp); |
445eaf87 EV |
95 | if (ret) |
96 | return ret; | |
97 | ||
359836e1 EV |
98 | /* Default constants */ |
99 | slope = s->slope; | |
100 | constant = s->constant; | |
101 | ||
102 | pcb_tz = data->pcb_tz; | |
445eaf87 | 103 | /* In case pcb zone is available, use the extrapolation rule with it */ |
0c12b5ac | 104 | if (!IS_ERR(pcb_tz)) { |
359836e1 EV |
105 | ret = thermal_zone_get_temp(pcb_tz, &pcb_temp); |
106 | if (!ret) { | |
107 | tmp -= pcb_temp; /* got a valid PCB temp */ | |
108 | slope = s->slope_pcb; | |
109 | constant = s->constant_pcb; | |
110 | } else { | |
111 | dev_err(bgp->dev, | |
112 | "Failed to read PCB state. Using defaults\n"); | |
df8f1347 | 113 | ret = 0; |
359836e1 | 114 | } |
445eaf87 | 115 | } |
03e859d3 | 116 | *temp = ti_thermal_hotspot_temperature(tmp, slope, constant); |
445eaf87 EV |
117 | |
118 | return ret; | |
119 | } | |
120 | ||
26d9cc65 | 121 | static inline int ti_thermal_get_temp(struct thermal_zone_device *thermal, |
17e8351a | 122 | int *temp) |
26d9cc65 EV |
123 | { |
124 | struct ti_thermal_data *data = thermal->devdata; | |
125 | ||
126 | return __ti_thermal_get_temp(data, temp); | |
127 | } | |
128 | ||
445eaf87 | 129 | /* Bind callback functions for thermal zone */ |
03e859d3 EV |
130 | static int ti_thermal_bind(struct thermal_zone_device *thermal, |
131 | struct thermal_cooling_device *cdev) | |
445eaf87 | 132 | { |
03e859d3 | 133 | struct ti_thermal_data *data = thermal->devdata; |
5035d48d | 134 | int id; |
445eaf87 | 135 | |
0c12b5ac | 136 | if (!data || IS_ERR(data)) |
445eaf87 EV |
137 | return -ENODEV; |
138 | ||
139 | /* check if this is the cooling device we registered */ | |
140 | if (data->cool_dev != cdev) | |
141 | return 0; | |
142 | ||
143 | id = data->sensor_id; | |
445eaf87 | 144 | |
445eaf87 | 145 | /* Simple thing, two trips, one passive another critical */ |
a7a3b8c8 | 146 | return thermal_zone_bind_cooling_device(thermal, 0, cdev, |
67b4c474 | 147 | /* bind with min and max states defined by cpu_cooling */ |
a7a3b8c8 | 148 | THERMAL_NO_LIMIT, |
6cd9e9f6 KS |
149 | THERMAL_NO_LIMIT, |
150 | THERMAL_WEIGHT_DEFAULT); | |
445eaf87 EV |
151 | } |
152 | ||
153 | /* Unbind callback functions for thermal zone */ | |
03e859d3 EV |
154 | static int ti_thermal_unbind(struct thermal_zone_device *thermal, |
155 | struct thermal_cooling_device *cdev) | |
445eaf87 | 156 | { |
03e859d3 | 157 | struct ti_thermal_data *data = thermal->devdata; |
445eaf87 | 158 | |
0c12b5ac | 159 | if (!data || IS_ERR(data)) |
445eaf87 EV |
160 | return -ENODEV; |
161 | ||
162 | /* check if this is the cooling device we registered */ | |
163 | if (data->cool_dev != cdev) | |
164 | return 0; | |
165 | ||
166 | /* Simple thing, two trips, one passive another critical */ | |
167 | return thermal_zone_unbind_cooling_device(thermal, 0, cdev); | |
168 | } | |
169 | ||
170 | /* Get mode callback functions for thermal zone */ | |
03e859d3 EV |
171 | static int ti_thermal_get_mode(struct thermal_zone_device *thermal, |
172 | enum thermal_device_mode *mode) | |
445eaf87 | 173 | { |
03e859d3 | 174 | struct ti_thermal_data *data = thermal->devdata; |
445eaf87 EV |
175 | |
176 | if (data) | |
177 | *mode = data->mode; | |
178 | ||
179 | return 0; | |
180 | } | |
181 | ||
182 | /* Set mode callback functions for thermal zone */ | |
03e859d3 EV |
183 | static int ti_thermal_set_mode(struct thermal_zone_device *thermal, |
184 | enum thermal_device_mode mode) | |
445eaf87 | 185 | { |
03e859d3 | 186 | struct ti_thermal_data *data = thermal->devdata; |
10ccff1b RK |
187 | struct ti_bandgap *bgp; |
188 | ||
189 | bgp = data->bgp; | |
445eaf87 | 190 | |
03e859d3 | 191 | if (!data->ti_thermal) { |
445eaf87 EV |
192 | dev_notice(&thermal->device, "thermal zone not registered\n"); |
193 | return 0; | |
194 | } | |
195 | ||
03e859d3 | 196 | mutex_lock(&data->ti_thermal->lock); |
445eaf87 EV |
197 | |
198 | if (mode == THERMAL_DEVICE_ENABLED) | |
03e859d3 | 199 | data->ti_thermal->polling_delay = FAST_TEMP_MONITORING_RATE; |
445eaf87 | 200 | else |
03e859d3 | 201 | data->ti_thermal->polling_delay = 0; |
445eaf87 | 202 | |
03e859d3 | 203 | mutex_unlock(&data->ti_thermal->lock); |
445eaf87 EV |
204 | |
205 | data->mode = mode; | |
10ccff1b RK |
206 | ti_bandgap_write_update_interval(bgp, data->sensor_id, |
207 | data->ti_thermal->polling_delay); | |
03e859d3 | 208 | thermal_zone_device_update(data->ti_thermal); |
445eaf87 | 209 | dev_dbg(&thermal->device, "thermal polling set for duration=%d msec\n", |
03e859d3 | 210 | data->ti_thermal->polling_delay); |
445eaf87 EV |
211 | |
212 | return 0; | |
213 | } | |
214 | ||
215 | /* Get trip type callback functions for thermal zone */ | |
03e859d3 EV |
216 | static int ti_thermal_get_trip_type(struct thermal_zone_device *thermal, |
217 | int trip, enum thermal_trip_type *type) | |
445eaf87 | 218 | { |
03e859d3 | 219 | if (!ti_thermal_is_valid_trip(trip)) |
445eaf87 EV |
220 | return -EINVAL; |
221 | ||
222 | if (trip + 1 == OMAP_TRIP_NUMBER) | |
223 | *type = THERMAL_TRIP_CRITICAL; | |
224 | else | |
225 | *type = THERMAL_TRIP_PASSIVE; | |
226 | ||
227 | return 0; | |
228 | } | |
229 | ||
230 | /* Get trip temperature callback functions for thermal zone */ | |
03e859d3 | 231 | static int ti_thermal_get_trip_temp(struct thermal_zone_device *thermal, |
17e8351a | 232 | int trip, int *temp) |
445eaf87 | 233 | { |
03e859d3 | 234 | if (!ti_thermal_is_valid_trip(trip)) |
445eaf87 EV |
235 | return -EINVAL; |
236 | ||
03e859d3 | 237 | *temp = ti_thermal_get_trip_value(trip); |
445eaf87 EV |
238 | |
239 | return 0; | |
240 | } | |
241 | ||
94f94ab7 | 242 | static int __ti_thermal_get_trend(void *p, int trip, enum thermal_trend *trend) |
03b7f67b | 243 | { |
26d9cc65 | 244 | struct ti_thermal_data *data = p; |
03b7f67b K |
245 | struct ti_bandgap *bgp; |
246 | int id, tr, ret = 0; | |
247 | ||
248 | bgp = data->bgp; | |
249 | id = data->sensor_id; | |
250 | ||
251 | ret = ti_bandgap_get_trend(bgp, id, &tr); | |
252 | if (ret) | |
253 | return ret; | |
254 | ||
255 | if (tr > 0) | |
256 | *trend = THERMAL_TREND_RAISING; | |
257 | else if (tr < 0) | |
258 | *trend = THERMAL_TREND_DROPPING; | |
259 | else | |
260 | *trend = THERMAL_TREND_STABLE; | |
261 | ||
262 | return 0; | |
263 | } | |
264 | ||
94f94ab7 SH |
265 | /* Get the temperature trend callback functions for thermal zone */ |
266 | static int ti_thermal_get_trend(struct thermal_zone_device *thermal, | |
267 | int trip, enum thermal_trend *trend) | |
268 | { | |
269 | return __ti_thermal_get_trend(thermal->devdata, trip, trend); | |
270 | } | |
271 | ||
445eaf87 | 272 | /* Get critical temperature callback functions for thermal zone */ |
03e859d3 | 273 | static int ti_thermal_get_crit_temp(struct thermal_zone_device *thermal, |
17e8351a | 274 | int *temp) |
445eaf87 EV |
275 | { |
276 | /* shutdown zone */ | |
03e859d3 | 277 | return ti_thermal_get_trip_temp(thermal, OMAP_TRIP_NUMBER - 1, temp); |
445eaf87 EV |
278 | } |
279 | ||
2251aef6 EV |
280 | static const struct thermal_zone_of_device_ops ti_of_thermal_ops = { |
281 | .get_temp = __ti_thermal_get_temp, | |
282 | .get_trend = __ti_thermal_get_trend, | |
283 | }; | |
284 | ||
03e859d3 EV |
285 | static struct thermal_zone_device_ops ti_thermal_ops = { |
286 | .get_temp = ti_thermal_get_temp, | |
03b7f67b | 287 | .get_trend = ti_thermal_get_trend, |
03e859d3 EV |
288 | .bind = ti_thermal_bind, |
289 | .unbind = ti_thermal_unbind, | |
290 | .get_mode = ti_thermal_get_mode, | |
291 | .set_mode = ti_thermal_set_mode, | |
292 | .get_trip_type = ti_thermal_get_trip_type, | |
293 | .get_trip_temp = ti_thermal_get_trip_temp, | |
294 | .get_crit_temp = ti_thermal_get_crit_temp, | |
445eaf87 EV |
295 | }; |
296 | ||
03e859d3 EV |
297 | static struct ti_thermal_data |
298 | *ti_thermal_build_data(struct ti_bandgap *bgp, int id) | |
445eaf87 | 299 | { |
03e859d3 | 300 | struct ti_thermal_data *data; |
445eaf87 | 301 | |
d7f080e6 | 302 | data = devm_kzalloc(bgp->dev, sizeof(*data), GFP_KERNEL); |
445eaf87 | 303 | if (!data) { |
d7f080e6 | 304 | dev_err(bgp->dev, "kzalloc fail\n"); |
04a4d10d | 305 | return NULL; |
445eaf87 EV |
306 | } |
307 | data->sensor_id = id; | |
d7f080e6 | 308 | data->bgp = bgp; |
445eaf87 | 309 | data->mode = THERMAL_DEVICE_ENABLED; |
0c12b5ac | 310 | /* pcb_tz will be either valid or PTR_ERR() */ |
359836e1 | 311 | data->pcb_tz = thermal_zone_get_zone_by_name("pcb"); |
03e859d3 | 312 | INIT_WORK(&data->thermal_wq, ti_thermal_work); |
445eaf87 | 313 | |
04a4d10d EV |
314 | return data; |
315 | } | |
316 | ||
03e859d3 EV |
317 | int ti_thermal_expose_sensor(struct ti_bandgap *bgp, int id, |
318 | char *domain) | |
04a4d10d | 319 | { |
03e859d3 | 320 | struct ti_thermal_data *data; |
04a4d10d | 321 | |
03e859d3 | 322 | data = ti_bandgap_get_sensor_data(bgp, id); |
04a4d10d | 323 | |
0c12b5ac | 324 | if (!data || IS_ERR(data)) |
03e859d3 | 325 | data = ti_thermal_build_data(bgp, id); |
04a4d10d EV |
326 | |
327 | if (!data) | |
328 | return -EINVAL; | |
329 | ||
26d9cc65 | 330 | /* in case this is specified by DT */ |
3982204c | 331 | data->ti_thermal = devm_thermal_zone_of_sensor_register(bgp->dev, id, |
2251aef6 | 332 | data, &ti_of_thermal_ops); |
26d9cc65 EV |
333 | if (IS_ERR(data->ti_thermal)) { |
334 | /* Create thermal zone */ | |
335 | data->ti_thermal = thermal_zone_device_register(domain, | |
03e859d3 | 336 | OMAP_TRIP_NUMBER, 0, data, &ti_thermal_ops, |
50125a9b | 337 | NULL, FAST_TEMP_MONITORING_RATE, |
765a1939 | 338 | FAST_TEMP_MONITORING_RATE); |
26d9cc65 EV |
339 | if (IS_ERR(data->ti_thermal)) { |
340 | dev_err(bgp->dev, "thermal zone device is NULL\n"); | |
341 | return PTR_ERR(data->ti_thermal); | |
342 | } | |
343 | data->ti_thermal->polling_delay = FAST_TEMP_MONITORING_RATE; | |
344 | data->our_zone = true; | |
445eaf87 | 345 | } |
03e859d3 | 346 | ti_bandgap_set_sensor_data(bgp, id, data); |
10ccff1b RK |
347 | ti_bandgap_write_update_interval(bgp, data->sensor_id, |
348 | data->ti_thermal->polling_delay); | |
445eaf87 EV |
349 | |
350 | return 0; | |
351 | } | |
352 | ||
03e859d3 | 353 | int ti_thermal_remove_sensor(struct ti_bandgap *bgp, int id) |
445eaf87 | 354 | { |
03e859d3 | 355 | struct ti_thermal_data *data; |
445eaf87 | 356 | |
03e859d3 | 357 | data = ti_bandgap_get_sensor_data(bgp, id); |
445eaf87 | 358 | |
26d9cc65 EV |
359 | if (data && data->ti_thermal) { |
360 | if (data->our_zone) | |
361 | thermal_zone_device_unregister(data->ti_thermal); | |
26d9cc65 | 362 | } |
445eaf87 EV |
363 | |
364 | return 0; | |
365 | } | |
366 | ||
03e859d3 | 367 | int ti_thermal_report_sensor_temperature(struct ti_bandgap *bgp, int id) |
445eaf87 | 368 | { |
03e859d3 | 369 | struct ti_thermal_data *data; |
445eaf87 | 370 | |
03e859d3 | 371 | data = ti_bandgap_get_sensor_data(bgp, id); |
445eaf87 EV |
372 | |
373 | schedule_work(&data->thermal_wq); | |
374 | ||
375 | return 0; | |
376 | } | |
377 | ||
03e859d3 | 378 | int ti_thermal_register_cpu_cooling(struct ti_bandgap *bgp, int id) |
445eaf87 | 379 | { |
03e859d3 | 380 | struct ti_thermal_data *data; |
26d9cc65 EV |
381 | struct device_node *np = bgp->dev->of_node; |
382 | ||
383 | /* | |
384 | * We are assuming here that if one deploys the zone | |
385 | * using DT, then it must be aware that the cooling device | |
386 | * loading has to happen via cpufreq driver. | |
387 | */ | |
388 | if (of_find_property(np, "#thermal-sensor-cells", NULL)) | |
389 | return 0; | |
445eaf87 | 390 | |
03e859d3 | 391 | data = ti_bandgap_get_sensor_data(bgp, id); |
0c12b5ac | 392 | if (!data || IS_ERR(data)) |
03e859d3 | 393 | data = ti_thermal_build_data(bgp, id); |
04a4d10d EV |
394 | |
395 | if (!data) | |
396 | return -EINVAL; | |
445eaf87 | 397 | |
445eaf87 | 398 | /* Register cooling device */ |
5035d48d | 399 | data->cool_dev = cpufreq_cooling_register(cpu_present_mask); |
0c12b5ac | 400 | if (IS_ERR(data->cool_dev)) { |
cffafc32 EV |
401 | int ret = PTR_ERR(data->cool_dev); |
402 | ||
403 | if (ret != -EPROBE_DEFER) | |
404 | dev_err(bgp->dev, | |
405 | "Failed to register cpu cooling device %d\n", | |
406 | ret); | |
407 | ||
408 | return ret; | |
445eaf87 | 409 | } |
03e859d3 | 410 | ti_bandgap_set_sensor_data(bgp, id, data); |
445eaf87 EV |
411 | |
412 | return 0; | |
413 | } | |
414 | ||
03e859d3 | 415 | int ti_thermal_unregister_cpu_cooling(struct ti_bandgap *bgp, int id) |
445eaf87 | 416 | { |
03e859d3 | 417 | struct ti_thermal_data *data; |
445eaf87 | 418 | |
03e859d3 | 419 | data = ti_bandgap_get_sensor_data(bgp, id); |
26d9cc65 | 420 | |
9ca9be2b | 421 | if (data) |
26d9cc65 | 422 | cpufreq_cooling_unregister(data->cool_dev); |
445eaf87 EV |
423 | |
424 | return 0; | |
425 | } |