Commit | Line | Data |
---|---|---|
4e5e4705 EV |
1 | /* |
2 | * of-thermal.c - Generic Thermal Management device tree support. | |
3 | * | |
4 | * Copyright (C) 2013 Texas Instruments | |
5 | * Copyright (C) 2013 Eduardo Valentin <eduardo.valentin@ti.com> | |
6 | * | |
7 | * | |
8 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; version 2 of the License. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, but | |
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | * General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License along | |
20 | * with this program; if not, write to the Free Software Foundation, Inc., | |
21 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
22 | * | |
23 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
24 | */ | |
25 | #include <linux/thermal.h> | |
26 | #include <linux/slab.h> | |
27 | #include <linux/types.h> | |
28 | #include <linux/of_device.h> | |
29 | #include <linux/of_platform.h> | |
30 | #include <linux/err.h> | |
31 | #include <linux/export.h> | |
32 | #include <linux/string.h> | |
2251aef6 | 33 | #include <linux/thermal.h> |
4e5e4705 EV |
34 | |
35 | #include "thermal_core.h" | |
36 | ||
37 | /*** Private data structures to represent thermal device tree data ***/ | |
38 | ||
4e5e4705 EV |
39 | /** |
40 | * struct __thermal_bind_param - a match between trip and cooling device | |
41 | * @cooling_device: a pointer to identify the referred cooling device | |
42 | * @trip_id: the trip point index | |
43 | * @usage: the percentage (from 0 to 100) of cooling contribution | |
44 | * @min: minimum cooling state used at this trip point | |
45 | * @max: maximum cooling state used at this trip point | |
46 | */ | |
47 | ||
48 | struct __thermal_bind_params { | |
49 | struct device_node *cooling_device; | |
50 | unsigned int trip_id; | |
51 | unsigned int usage; | |
52 | unsigned long min; | |
53 | unsigned long max; | |
54 | }; | |
55 | ||
56 | /** | |
57 | * struct __thermal_zone - internal representation of a thermal zone | |
58 | * @mode: current thermal zone device mode (enabled/disabled) | |
59 | * @passive_delay: polling interval while passive cooling is activated | |
60 | * @polling_delay: zone polling interval | |
a46dbae8 EV |
61 | * @slope: slope of the temperature adjustment curve |
62 | * @offset: offset of the temperature adjustment curve | |
4e5e4705 EV |
63 | * @ntrips: number of trip points |
64 | * @trips: an array of trip points (0..ntrips - 1) | |
65 | * @num_tbps: number of thermal bind params | |
66 | * @tbps: an array of thermal bind params (0..num_tbps - 1) | |
67 | * @sensor_data: sensor private data used while reading temperature and trend | |
2251aef6 | 68 | * @ops: set of callbacks to handle the thermal zone based on DT |
4e5e4705 EV |
69 | */ |
70 | ||
71 | struct __thermal_zone { | |
72 | enum thermal_device_mode mode; | |
73 | int passive_delay; | |
74 | int polling_delay; | |
a46dbae8 EV |
75 | int slope; |
76 | int offset; | |
4e5e4705 EV |
77 | |
78 | /* trip data */ | |
79 | int ntrips; | |
ad9914ac | 80 | struct thermal_trip *trips; |
4e5e4705 EV |
81 | |
82 | /* cooling binding data */ | |
83 | int num_tbps; | |
84 | struct __thermal_bind_params *tbps; | |
85 | ||
86 | /* sensor interface */ | |
87 | void *sensor_data; | |
2251aef6 | 88 | const struct thermal_zone_of_device_ops *ops; |
4e5e4705 EV |
89 | }; |
90 | ||
91 | /*** DT thermal zone device callbacks ***/ | |
92 | ||
93 | static int of_thermal_get_temp(struct thermal_zone_device *tz, | |
17e8351a | 94 | int *temp) |
4e5e4705 EV |
95 | { |
96 | struct __thermal_zone *data = tz->devdata; | |
97 | ||
2251aef6 | 98 | if (!data->ops->get_temp) |
4e5e4705 EV |
99 | return -EINVAL; |
100 | ||
2251aef6 | 101 | return data->ops->get_temp(data->sensor_data, temp); |
4e5e4705 EV |
102 | } |
103 | ||
252b7879 SH |
104 | static int of_thermal_set_trips(struct thermal_zone_device *tz, |
105 | int low, int high) | |
106 | { | |
107 | struct __thermal_zone *data = tz->devdata; | |
108 | ||
109 | if (!data->ops || !data->ops->set_trips) | |
110 | return -EINVAL; | |
111 | ||
112 | return data->ops->set_trips(data->sensor_data, low, high); | |
113 | } | |
114 | ||
08dab66e LM |
115 | /** |
116 | * of_thermal_get_ntrips - function to export number of available trip | |
117 | * points. | |
118 | * @tz: pointer to a thermal zone | |
119 | * | |
120 | * This function is a globally visible wrapper to get number of trip points | |
121 | * stored in the local struct __thermal_zone | |
122 | * | |
123 | * Return: number of available trip points, -ENODEV when data not available | |
124 | */ | |
125 | int of_thermal_get_ntrips(struct thermal_zone_device *tz) | |
126 | { | |
127 | struct __thermal_zone *data = tz->devdata; | |
128 | ||
129 | if (!data || IS_ERR(data)) | |
130 | return -ENODEV; | |
131 | ||
132 | return data->ntrips; | |
133 | } | |
134 | EXPORT_SYMBOL_GPL(of_thermal_get_ntrips); | |
135 | ||
a9bf2cc4 LM |
136 | /** |
137 | * of_thermal_is_trip_valid - function to check if trip point is valid | |
138 | * | |
139 | * @tz: pointer to a thermal zone | |
140 | * @trip: trip point to evaluate | |
141 | * | |
142 | * This function is responsible for checking if passed trip point is valid | |
143 | * | |
144 | * Return: true if trip point is valid, false otherwise | |
145 | */ | |
146 | bool of_thermal_is_trip_valid(struct thermal_zone_device *tz, int trip) | |
147 | { | |
148 | struct __thermal_zone *data = tz->devdata; | |
149 | ||
150 | if (!data || trip >= data->ntrips || trip < 0) | |
151 | return false; | |
152 | ||
153 | return true; | |
154 | } | |
155 | EXPORT_SYMBOL_GPL(of_thermal_is_trip_valid); | |
156 | ||
ce8be778 LM |
157 | /** |
158 | * of_thermal_get_trip_points - function to get access to a globally exported | |
159 | * trip points | |
160 | * | |
161 | * @tz: pointer to a thermal zone | |
162 | * | |
163 | * This function provides a pointer to trip points table | |
164 | * | |
165 | * Return: pointer to trip points table, NULL otherwise | |
166 | */ | |
ebc3193a | 167 | const struct thermal_trip * |
ce8be778 LM |
168 | of_thermal_get_trip_points(struct thermal_zone_device *tz) |
169 | { | |
170 | struct __thermal_zone *data = tz->devdata; | |
171 | ||
172 | if (!data) | |
173 | return NULL; | |
174 | ||
175 | return data->trips; | |
176 | } | |
177 | EXPORT_SYMBOL_GPL(of_thermal_get_trip_points); | |
178 | ||
184a4bf6 LM |
179 | /** |
180 | * of_thermal_set_emul_temp - function to set emulated temperature | |
181 | * | |
182 | * @tz: pointer to a thermal zone | |
183 | * @temp: temperature to set | |
184 | * | |
185 | * This function gives the ability to set emulated value of temperature, | |
186 | * which is handy for debugging | |
187 | * | |
188 | * Return: zero on success, error code otherwise | |
189 | */ | |
190 | static int of_thermal_set_emul_temp(struct thermal_zone_device *tz, | |
17e8351a | 191 | int temp) |
184a4bf6 LM |
192 | { |
193 | struct __thermal_zone *data = tz->devdata; | |
194 | ||
195 | if (!data->ops || !data->ops->set_emul_temp) | |
196 | return -EINVAL; | |
197 | ||
198 | return data->ops->set_emul_temp(data->sensor_data, temp); | |
199 | } | |
200 | ||
4e5e4705 EV |
201 | static int of_thermal_get_trend(struct thermal_zone_device *tz, int trip, |
202 | enum thermal_trend *trend) | |
203 | { | |
204 | struct __thermal_zone *data = tz->devdata; | |
4e5e4705 | 205 | |
2251aef6 | 206 | if (!data->ops->get_trend) |
4e5e4705 EV |
207 | return -EINVAL; |
208 | ||
94f94ab7 | 209 | return data->ops->get_trend(data->sensor_data, trip, trend); |
4e5e4705 EV |
210 | } |
211 | ||
212 | static int of_thermal_bind(struct thermal_zone_device *thermal, | |
213 | struct thermal_cooling_device *cdev) | |
214 | { | |
215 | struct __thermal_zone *data = thermal->devdata; | |
216 | int i; | |
217 | ||
218 | if (!data || IS_ERR(data)) | |
219 | return -ENODEV; | |
220 | ||
221 | /* find where to bind */ | |
222 | for (i = 0; i < data->num_tbps; i++) { | |
223 | struct __thermal_bind_params *tbp = data->tbps + i; | |
224 | ||
225 | if (tbp->cooling_device == cdev->np) { | |
226 | int ret; | |
227 | ||
228 | ret = thermal_zone_bind_cooling_device(thermal, | |
229 | tbp->trip_id, cdev, | |
dd354b84 | 230 | tbp->max, |
6cd9e9f6 KS |
231 | tbp->min, |
232 | tbp->usage); | |
4e5e4705 EV |
233 | if (ret) |
234 | return ret; | |
235 | } | |
236 | } | |
237 | ||
238 | return 0; | |
239 | } | |
240 | ||
241 | static int of_thermal_unbind(struct thermal_zone_device *thermal, | |
242 | struct thermal_cooling_device *cdev) | |
243 | { | |
244 | struct __thermal_zone *data = thermal->devdata; | |
245 | int i; | |
246 | ||
247 | if (!data || IS_ERR(data)) | |
248 | return -ENODEV; | |
249 | ||
250 | /* find where to unbind */ | |
251 | for (i = 0; i < data->num_tbps; i++) { | |
252 | struct __thermal_bind_params *tbp = data->tbps + i; | |
253 | ||
254 | if (tbp->cooling_device == cdev->np) { | |
255 | int ret; | |
256 | ||
257 | ret = thermal_zone_unbind_cooling_device(thermal, | |
258 | tbp->trip_id, cdev); | |
259 | if (ret) | |
260 | return ret; | |
261 | } | |
262 | } | |
263 | ||
264 | return 0; | |
265 | } | |
266 | ||
267 | static int of_thermal_get_mode(struct thermal_zone_device *tz, | |
268 | enum thermal_device_mode *mode) | |
269 | { | |
270 | struct __thermal_zone *data = tz->devdata; | |
271 | ||
272 | *mode = data->mode; | |
273 | ||
274 | return 0; | |
275 | } | |
276 | ||
277 | static int of_thermal_set_mode(struct thermal_zone_device *tz, | |
278 | enum thermal_device_mode mode) | |
279 | { | |
280 | struct __thermal_zone *data = tz->devdata; | |
281 | ||
282 | mutex_lock(&tz->lock); | |
283 | ||
284 | if (mode == THERMAL_DEVICE_ENABLED) | |
285 | tz->polling_delay = data->polling_delay; | |
286 | else | |
287 | tz->polling_delay = 0; | |
288 | ||
289 | mutex_unlock(&tz->lock); | |
290 | ||
291 | data->mode = mode; | |
292 | thermal_zone_device_update(tz); | |
293 | ||
294 | return 0; | |
295 | } | |
296 | ||
297 | static int of_thermal_get_trip_type(struct thermal_zone_device *tz, int trip, | |
298 | enum thermal_trip_type *type) | |
299 | { | |
300 | struct __thermal_zone *data = tz->devdata; | |
301 | ||
302 | if (trip >= data->ntrips || trip < 0) | |
303 | return -EDOM; | |
304 | ||
305 | *type = data->trips[trip].type; | |
306 | ||
307 | return 0; | |
308 | } | |
309 | ||
310 | static int of_thermal_get_trip_temp(struct thermal_zone_device *tz, int trip, | |
17e8351a | 311 | int *temp) |
4e5e4705 EV |
312 | { |
313 | struct __thermal_zone *data = tz->devdata; | |
314 | ||
315 | if (trip >= data->ntrips || trip < 0) | |
316 | return -EDOM; | |
317 | ||
318 | *temp = data->trips[trip].temperature; | |
319 | ||
320 | return 0; | |
321 | } | |
322 | ||
323 | static int of_thermal_set_trip_temp(struct thermal_zone_device *tz, int trip, | |
17e8351a | 324 | int temp) |
4e5e4705 EV |
325 | { |
326 | struct __thermal_zone *data = tz->devdata; | |
327 | ||
328 | if (trip >= data->ntrips || trip < 0) | |
329 | return -EDOM; | |
330 | ||
c3509521 WN |
331 | if (data->ops->set_trip_temp) { |
332 | int ret; | |
333 | ||
334 | ret = data->ops->set_trip_temp(data->sensor_data, trip, temp); | |
335 | if (ret) | |
336 | return ret; | |
337 | } | |
338 | ||
4e5e4705 EV |
339 | /* thermal framework should take care of data->mask & (1 << trip) */ |
340 | data->trips[trip].temperature = temp; | |
341 | ||
342 | return 0; | |
343 | } | |
344 | ||
345 | static int of_thermal_get_trip_hyst(struct thermal_zone_device *tz, int trip, | |
17e8351a | 346 | int *hyst) |
4e5e4705 EV |
347 | { |
348 | struct __thermal_zone *data = tz->devdata; | |
349 | ||
350 | if (trip >= data->ntrips || trip < 0) | |
351 | return -EDOM; | |
352 | ||
353 | *hyst = data->trips[trip].hysteresis; | |
354 | ||
355 | return 0; | |
356 | } | |
357 | ||
358 | static int of_thermal_set_trip_hyst(struct thermal_zone_device *tz, int trip, | |
17e8351a | 359 | int hyst) |
4e5e4705 EV |
360 | { |
361 | struct __thermal_zone *data = tz->devdata; | |
362 | ||
363 | if (trip >= data->ntrips || trip < 0) | |
364 | return -EDOM; | |
365 | ||
366 | /* thermal framework should take care of data->mask & (1 << trip) */ | |
367 | data->trips[trip].hysteresis = hyst; | |
368 | ||
369 | return 0; | |
370 | } | |
371 | ||
372 | static int of_thermal_get_crit_temp(struct thermal_zone_device *tz, | |
17e8351a | 373 | int *temp) |
4e5e4705 EV |
374 | { |
375 | struct __thermal_zone *data = tz->devdata; | |
376 | int i; | |
377 | ||
378 | for (i = 0; i < data->ntrips; i++) | |
379 | if (data->trips[i].type == THERMAL_TRIP_CRITICAL) { | |
380 | *temp = data->trips[i].temperature; | |
381 | return 0; | |
382 | } | |
383 | ||
384 | return -EINVAL; | |
385 | } | |
386 | ||
387 | static struct thermal_zone_device_ops of_thermal_ops = { | |
388 | .get_mode = of_thermal_get_mode, | |
389 | .set_mode = of_thermal_set_mode, | |
390 | ||
391 | .get_trip_type = of_thermal_get_trip_type, | |
392 | .get_trip_temp = of_thermal_get_trip_temp, | |
393 | .set_trip_temp = of_thermal_set_trip_temp, | |
394 | .get_trip_hyst = of_thermal_get_trip_hyst, | |
395 | .set_trip_hyst = of_thermal_set_trip_hyst, | |
396 | .get_crit_temp = of_thermal_get_crit_temp, | |
397 | ||
398 | .bind = of_thermal_bind, | |
399 | .unbind = of_thermal_unbind, | |
400 | }; | |
401 | ||
402 | /*** sensor API ***/ | |
403 | ||
404 | static struct thermal_zone_device * | |
405 | thermal_zone_of_add_sensor(struct device_node *zone, | |
406 | struct device_node *sensor, void *data, | |
2251aef6 | 407 | const struct thermal_zone_of_device_ops *ops) |
4e5e4705 EV |
408 | { |
409 | struct thermal_zone_device *tzd; | |
410 | struct __thermal_zone *tz; | |
411 | ||
412 | tzd = thermal_zone_get_zone_by_name(zone->name); | |
413 | if (IS_ERR(tzd)) | |
414 | return ERR_PTR(-EPROBE_DEFER); | |
415 | ||
416 | tz = tzd->devdata; | |
417 | ||
2251aef6 EV |
418 | if (!ops) |
419 | return ERR_PTR(-EINVAL); | |
420 | ||
4e5e4705 | 421 | mutex_lock(&tzd->lock); |
2251aef6 | 422 | tz->ops = ops; |
4e5e4705 EV |
423 | tz->sensor_data = data; |
424 | ||
425 | tzd->ops->get_temp = of_thermal_get_temp; | |
426 | tzd->ops->get_trend = of_thermal_get_trend; | |
252b7879 SH |
427 | |
428 | /* | |
429 | * The thermal zone core will calculate the window if they have set the | |
430 | * optional set_trips pointer. | |
431 | */ | |
432 | if (ops->set_trips) | |
433 | tzd->ops->set_trips = of_thermal_set_trips; | |
434 | ||
184a4bf6 | 435 | tzd->ops->set_emul_temp = of_thermal_set_emul_temp; |
4e5e4705 EV |
436 | mutex_unlock(&tzd->lock); |
437 | ||
438 | return tzd; | |
439 | } | |
440 | ||
441 | /** | |
442 | * thermal_zone_of_sensor_register - registers a sensor to a DT thermal zone | |
443 | * @dev: a valid struct device pointer of a sensor device. Must contain | |
444 | * a valid .of_node, for the sensor node. | |
445 | * @sensor_id: a sensor identifier, in case the sensor IP has more | |
446 | * than one sensors | |
447 | * @data: a private pointer (owned by the caller) that will be passed | |
448 | * back, when a temperature reading is needed. | |
2251aef6 | 449 | * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp. |
4e5e4705 EV |
450 | * |
451 | * This function will search the list of thermal zones described in device | |
452 | * tree and look for the zone that refer to the sensor device pointed by | |
453 | * @dev->of_node as temperature providers. For the zone pointing to the | |
454 | * sensor node, the sensor will be added to the DT thermal zone device. | |
455 | * | |
456 | * The thermal zone temperature is provided by the @get_temp function | |
457 | * pointer. When called, it will have the private pointer @data back. | |
458 | * | |
459 | * The thermal zone temperature trend is provided by the @get_trend function | |
460 | * pointer. When called, it will have the private pointer @data back. | |
461 | * | |
462 | * TODO: | |
463 | * 01 - This function must enqueue the new sensor instead of using | |
464 | * it as the only source of temperature values. | |
465 | * | |
466 | * 02 - There must be a way to match the sensor with all thermal zones | |
467 | * that refer to it. | |
468 | * | |
469 | * Return: On success returns a valid struct thermal_zone_device, | |
470 | * otherwise, it returns a corresponding ERR_PTR(). Caller must | |
471 | * check the return value with help of IS_ERR() helper. | |
472 | */ | |
473 | struct thermal_zone_device * | |
2251aef6 EV |
474 | thermal_zone_of_sensor_register(struct device *dev, int sensor_id, void *data, |
475 | const struct thermal_zone_of_device_ops *ops) | |
4e5e4705 EV |
476 | { |
477 | struct device_node *np, *child, *sensor_np; | |
c2aad93c | 478 | struct thermal_zone_device *tzd = ERR_PTR(-ENODEV); |
4e5e4705 EV |
479 | |
480 | np = of_find_node_by_name(NULL, "thermal-zones"); | |
481 | if (!np) | |
482 | return ERR_PTR(-ENODEV); | |
483 | ||
c2aad93c VZ |
484 | if (!dev || !dev->of_node) { |
485 | of_node_put(np); | |
4e5e4705 | 486 | return ERR_PTR(-EINVAL); |
c2aad93c | 487 | } |
4e5e4705 | 488 | |
c2aad93c | 489 | sensor_np = of_node_get(dev->of_node); |
4e5e4705 | 490 | |
42bbe400 | 491 | for_each_available_child_of_node(np, child) { |
4e5e4705 EV |
492 | struct of_phandle_args sensor_specs; |
493 | int ret, id; | |
494 | ||
495 | /* For now, thermal framework supports only 1 sensor per zone */ | |
496 | ret = of_parse_phandle_with_args(child, "thermal-sensors", | |
497 | "#thermal-sensor-cells", | |
498 | 0, &sensor_specs); | |
499 | if (ret) | |
500 | continue; | |
501 | ||
502 | if (sensor_specs.args_count >= 1) { | |
503 | id = sensor_specs.args[0]; | |
504 | WARN(sensor_specs.args_count > 1, | |
505 | "%s: too many cells in sensor specifier %d\n", | |
506 | sensor_specs.np->name, sensor_specs.args_count); | |
507 | } else { | |
508 | id = 0; | |
509 | } | |
510 | ||
511 | if (sensor_specs.np == sensor_np && id == sensor_id) { | |
c2aad93c | 512 | tzd = thermal_zone_of_add_sensor(child, sensor_np, |
2251aef6 | 513 | data, ops); |
528012c1 LM |
514 | if (!IS_ERR(tzd)) |
515 | tzd->ops->set_mode(tzd, THERMAL_DEVICE_ENABLED); | |
516 | ||
c2aad93c VZ |
517 | of_node_put(sensor_specs.np); |
518 | of_node_put(child); | |
519 | goto exit; | |
4e5e4705 | 520 | } |
c2aad93c | 521 | of_node_put(sensor_specs.np); |
4e5e4705 | 522 | } |
c2aad93c VZ |
523 | exit: |
524 | of_node_put(sensor_np); | |
4e5e4705 EV |
525 | of_node_put(np); |
526 | ||
c2aad93c | 527 | return tzd; |
4e5e4705 EV |
528 | } |
529 | EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_register); | |
530 | ||
531 | /** | |
532 | * thermal_zone_of_sensor_unregister - unregisters a sensor from a DT thermal zone | |
533 | * @dev: a valid struct device pointer of a sensor device. Must contain | |
534 | * a valid .of_node, for the sensor node. | |
535 | * @tzd: a pointer to struct thermal_zone_device where the sensor is registered. | |
536 | * | |
537 | * This function removes the sensor callbacks and private data from the | |
538 | * thermal zone device registered with thermal_zone_of_sensor_register() | |
539 | * API. It will also silent the zone by remove the .get_temp() and .get_trend() | |
540 | * thermal zone device callbacks. | |
541 | * | |
542 | * TODO: When the support to several sensors per zone is added, this | |
543 | * function must search the sensor list based on @dev parameter. | |
544 | * | |
545 | */ | |
546 | void thermal_zone_of_sensor_unregister(struct device *dev, | |
547 | struct thermal_zone_device *tzd) | |
548 | { | |
549 | struct __thermal_zone *tz; | |
550 | ||
551 | if (!dev || !tzd || !tzd->devdata) | |
552 | return; | |
553 | ||
554 | tz = tzd->devdata; | |
555 | ||
556 | /* no __thermal_zone, nothing to be done */ | |
557 | if (!tz) | |
558 | return; | |
559 | ||
560 | mutex_lock(&tzd->lock); | |
561 | tzd->ops->get_temp = NULL; | |
562 | tzd->ops->get_trend = NULL; | |
184a4bf6 | 563 | tzd->ops->set_emul_temp = NULL; |
4e5e4705 | 564 | |
2251aef6 | 565 | tz->ops = NULL; |
4e5e4705 EV |
566 | tz->sensor_data = NULL; |
567 | mutex_unlock(&tzd->lock); | |
568 | } | |
569 | EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister); | |
570 | ||
e498b498 LD |
571 | static void devm_thermal_zone_of_sensor_release(struct device *dev, void *res) |
572 | { | |
573 | thermal_zone_of_sensor_unregister(dev, | |
574 | *(struct thermal_zone_device **)res); | |
575 | } | |
576 | ||
577 | static int devm_thermal_zone_of_sensor_match(struct device *dev, void *res, | |
578 | void *data) | |
579 | { | |
580 | struct thermal_zone_device **r = res; | |
581 | ||
582 | if (WARN_ON(!r || !*r)) | |
583 | return 0; | |
584 | ||
585 | return *r == data; | |
586 | } | |
587 | ||
588 | /** | |
589 | * devm_thermal_zone_of_sensor_register - Resource managed version of | |
590 | * thermal_zone_of_sensor_register() | |
591 | * @dev: a valid struct device pointer of a sensor device. Must contain | |
592 | * a valid .of_node, for the sensor node. | |
593 | * @sensor_id: a sensor identifier, in case the sensor IP has more | |
594 | * than one sensors | |
595 | * @data: a private pointer (owned by the caller) that will be passed | |
596 | * back, when a temperature reading is needed. | |
597 | * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp. | |
598 | * | |
599 | * Refer thermal_zone_of_sensor_register() for more details. | |
600 | * | |
601 | * Return: On success returns a valid struct thermal_zone_device, | |
602 | * otherwise, it returns a corresponding ERR_PTR(). Caller must | |
603 | * check the return value with help of IS_ERR() helper. | |
604 | * Registered hermal_zone_device device will automatically be | |
605 | * released when device is unbounded. | |
606 | */ | |
607 | struct thermal_zone_device *devm_thermal_zone_of_sensor_register( | |
608 | struct device *dev, int sensor_id, | |
609 | void *data, const struct thermal_zone_of_device_ops *ops) | |
610 | { | |
611 | struct thermal_zone_device **ptr, *tzd; | |
612 | ||
613 | ptr = devres_alloc(devm_thermal_zone_of_sensor_release, sizeof(*ptr), | |
614 | GFP_KERNEL); | |
615 | if (!ptr) | |
616 | return ERR_PTR(-ENOMEM); | |
617 | ||
618 | tzd = thermal_zone_of_sensor_register(dev, sensor_id, data, ops); | |
619 | if (IS_ERR(tzd)) { | |
620 | devres_free(ptr); | |
621 | return tzd; | |
622 | } | |
623 | ||
624 | *ptr = tzd; | |
625 | devres_add(dev, ptr); | |
626 | ||
627 | return tzd; | |
628 | } | |
629 | EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_register); | |
630 | ||
631 | /** | |
632 | * devm_thermal_zone_of_sensor_unregister - Resource managed version of | |
633 | * thermal_zone_of_sensor_unregister(). | |
634 | * @dev: Device for which which resource was allocated. | |
635 | * @tzd: a pointer to struct thermal_zone_device where the sensor is registered. | |
636 | * | |
637 | * This function removes the sensor callbacks and private data from the | |
638 | * thermal zone device registered with devm_thermal_zone_of_sensor_register() | |
639 | * API. It will also silent the zone by remove the .get_temp() and .get_trend() | |
640 | * thermal zone device callbacks. | |
641 | * Normally this function will not need to be called and the resource | |
642 | * management code will ensure that the resource is freed. | |
643 | */ | |
644 | void devm_thermal_zone_of_sensor_unregister(struct device *dev, | |
645 | struct thermal_zone_device *tzd) | |
646 | { | |
647 | WARN_ON(devres_release(dev, devm_thermal_zone_of_sensor_release, | |
648 | devm_thermal_zone_of_sensor_match, tzd)); | |
649 | } | |
650 | EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_unregister); | |
651 | ||
4e5e4705 EV |
652 | /*** functions parsing device tree nodes ***/ |
653 | ||
654 | /** | |
655 | * thermal_of_populate_bind_params - parse and fill cooling map data | |
656 | * @np: DT node containing a cooling-map node | |
657 | * @__tbp: data structure to be filled with cooling map info | |
658 | * @trips: array of thermal zone trip points | |
659 | * @ntrips: number of trip points inside trips. | |
660 | * | |
661 | * This function parses a cooling-map type of node represented by | |
662 | * @np parameter and fills the read data into @__tbp data structure. | |
663 | * It needs the already parsed array of trip points of the thermal zone | |
664 | * in consideration. | |
665 | * | |
666 | * Return: 0 on success, proper error code otherwise | |
667 | */ | |
668 | static int thermal_of_populate_bind_params(struct device_node *np, | |
669 | struct __thermal_bind_params *__tbp, | |
ad9914ac | 670 | struct thermal_trip *trips, |
4e5e4705 EV |
671 | int ntrips) |
672 | { | |
673 | struct of_phandle_args cooling_spec; | |
674 | struct device_node *trip; | |
675 | int ret, i; | |
676 | u32 prop; | |
677 | ||
678 | /* Default weight. Usage is optional */ | |
6cd9e9f6 | 679 | __tbp->usage = THERMAL_WEIGHT_DEFAULT; |
4e5e4705 EV |
680 | ret = of_property_read_u32(np, "contribution", &prop); |
681 | if (ret == 0) | |
682 | __tbp->usage = prop; | |
683 | ||
684 | trip = of_parse_phandle(np, "trip", 0); | |
685 | if (!trip) { | |
686 | pr_err("missing trip property\n"); | |
687 | return -ENODEV; | |
688 | } | |
689 | ||
690 | /* match using device_node */ | |
691 | for (i = 0; i < ntrips; i++) | |
692 | if (trip == trips[i].np) { | |
693 | __tbp->trip_id = i; | |
694 | break; | |
695 | } | |
696 | ||
697 | if (i == ntrips) { | |
698 | ret = -ENODEV; | |
699 | goto end; | |
700 | } | |
701 | ||
702 | ret = of_parse_phandle_with_args(np, "cooling-device", "#cooling-cells", | |
703 | 0, &cooling_spec); | |
704 | if (ret < 0) { | |
705 | pr_err("missing cooling_device property\n"); | |
706 | goto end; | |
707 | } | |
708 | __tbp->cooling_device = cooling_spec.np; | |
709 | if (cooling_spec.args_count >= 2) { /* at least min and max */ | |
710 | __tbp->min = cooling_spec.args[0]; | |
711 | __tbp->max = cooling_spec.args[1]; | |
712 | } else { | |
713 | pr_err("wrong reference to cooling device, missing limits\n"); | |
714 | } | |
715 | ||
716 | end: | |
717 | of_node_put(trip); | |
718 | ||
719 | return ret; | |
720 | } | |
721 | ||
722 | /** | |
723 | * It maps 'enum thermal_trip_type' found in include/linux/thermal.h | |
724 | * into the device tree binding of 'trip', property type. | |
725 | */ | |
726 | static const char * const trip_types[] = { | |
727 | [THERMAL_TRIP_ACTIVE] = "active", | |
728 | [THERMAL_TRIP_PASSIVE] = "passive", | |
729 | [THERMAL_TRIP_HOT] = "hot", | |
730 | [THERMAL_TRIP_CRITICAL] = "critical", | |
731 | }; | |
732 | ||
733 | /** | |
734 | * thermal_of_get_trip_type - Get phy mode for given device_node | |
735 | * @np: Pointer to the given device_node | |
736 | * @type: Pointer to resulting trip type | |
737 | * | |
738 | * The function gets trip type string from property 'type', | |
739 | * and store its index in trip_types table in @type, | |
740 | * | |
741 | * Return: 0 on success, or errno in error case. | |
742 | */ | |
743 | static int thermal_of_get_trip_type(struct device_node *np, | |
744 | enum thermal_trip_type *type) | |
745 | { | |
746 | const char *t; | |
747 | int err, i; | |
748 | ||
749 | err = of_property_read_string(np, "type", &t); | |
750 | if (err < 0) | |
751 | return err; | |
752 | ||
753 | for (i = 0; i < ARRAY_SIZE(trip_types); i++) | |
754 | if (!strcasecmp(t, trip_types[i])) { | |
755 | *type = i; | |
756 | return 0; | |
757 | } | |
758 | ||
759 | return -ENODEV; | |
760 | } | |
761 | ||
762 | /** | |
763 | * thermal_of_populate_trip - parse and fill one trip point data | |
764 | * @np: DT node containing a trip point node | |
765 | * @trip: trip point data structure to be filled up | |
766 | * | |
767 | * This function parses a trip point type of node represented by | |
768 | * @np parameter and fills the read data into @trip data structure. | |
769 | * | |
770 | * Return: 0 on success, proper error code otherwise | |
771 | */ | |
772 | static int thermal_of_populate_trip(struct device_node *np, | |
ad9914ac | 773 | struct thermal_trip *trip) |
4e5e4705 EV |
774 | { |
775 | int prop; | |
776 | int ret; | |
777 | ||
778 | ret = of_property_read_u32(np, "temperature", &prop); | |
779 | if (ret < 0) { | |
780 | pr_err("missing temperature property\n"); | |
781 | return ret; | |
782 | } | |
783 | trip->temperature = prop; | |
784 | ||
785 | ret = of_property_read_u32(np, "hysteresis", &prop); | |
786 | if (ret < 0) { | |
787 | pr_err("missing hysteresis property\n"); | |
788 | return ret; | |
789 | } | |
790 | trip->hysteresis = prop; | |
791 | ||
792 | ret = thermal_of_get_trip_type(np, &trip->type); | |
793 | if (ret < 0) { | |
794 | pr_err("wrong trip type property\n"); | |
795 | return ret; | |
796 | } | |
797 | ||
798 | /* Required for cooling map matching */ | |
799 | trip->np = np; | |
c2aad93c | 800 | of_node_get(np); |
4e5e4705 EV |
801 | |
802 | return 0; | |
803 | } | |
804 | ||
805 | /** | |
806 | * thermal_of_build_thermal_zone - parse and fill one thermal zone data | |
807 | * @np: DT node containing a thermal zone node | |
808 | * | |
809 | * This function parses a thermal zone type of node represented by | |
810 | * @np parameter and fills the read data into a __thermal_zone data structure | |
811 | * and return this pointer. | |
812 | * | |
a46dbae8 | 813 | * TODO: Missing properties to parse: thermal-sensor-names |
4e5e4705 EV |
814 | * |
815 | * Return: On success returns a valid struct __thermal_zone, | |
816 | * otherwise, it returns a corresponding ERR_PTR(). Caller must | |
817 | * check the return value with help of IS_ERR() helper. | |
818 | */ | |
c0ff8aaa JL |
819 | static struct __thermal_zone |
820 | __init *thermal_of_build_thermal_zone(struct device_node *np) | |
4e5e4705 EV |
821 | { |
822 | struct device_node *child = NULL, *gchild; | |
823 | struct __thermal_zone *tz; | |
824 | int ret, i; | |
a46dbae8 | 825 | u32 prop, coef[2]; |
4e5e4705 EV |
826 | |
827 | if (!np) { | |
828 | pr_err("no thermal zone np\n"); | |
829 | return ERR_PTR(-EINVAL); | |
830 | } | |
831 | ||
832 | tz = kzalloc(sizeof(*tz), GFP_KERNEL); | |
833 | if (!tz) | |
834 | return ERR_PTR(-ENOMEM); | |
835 | ||
836 | ret = of_property_read_u32(np, "polling-delay-passive", &prop); | |
837 | if (ret < 0) { | |
838 | pr_err("missing polling-delay-passive property\n"); | |
839 | goto free_tz; | |
840 | } | |
841 | tz->passive_delay = prop; | |
842 | ||
843 | ret = of_property_read_u32(np, "polling-delay", &prop); | |
844 | if (ret < 0) { | |
845 | pr_err("missing polling-delay property\n"); | |
846 | goto free_tz; | |
847 | } | |
848 | tz->polling_delay = prop; | |
849 | ||
a46dbae8 EV |
850 | /* |
851 | * REVIST: for now, the thermal framework supports only | |
852 | * one sensor per thermal zone. Thus, we are considering | |
853 | * only the first two values as slope and offset. | |
854 | */ | |
855 | ret = of_property_read_u32_array(np, "coefficients", coef, 2); | |
856 | if (ret == 0) { | |
857 | tz->slope = coef[0]; | |
858 | tz->offset = coef[1]; | |
859 | } else { | |
860 | tz->slope = 1; | |
861 | tz->offset = 0; | |
862 | } | |
863 | ||
4e5e4705 EV |
864 | /* trips */ |
865 | child = of_get_child_by_name(np, "trips"); | |
866 | ||
867 | /* No trips provided */ | |
868 | if (!child) | |
869 | goto finish; | |
870 | ||
871 | tz->ntrips = of_get_child_count(child); | |
872 | if (tz->ntrips == 0) /* must have at least one child */ | |
873 | goto finish; | |
874 | ||
875 | tz->trips = kzalloc(tz->ntrips * sizeof(*tz->trips), GFP_KERNEL); | |
876 | if (!tz->trips) { | |
877 | ret = -ENOMEM; | |
878 | goto free_tz; | |
879 | } | |
880 | ||
881 | i = 0; | |
882 | for_each_child_of_node(child, gchild) { | |
883 | ret = thermal_of_populate_trip(gchild, &tz->trips[i++]); | |
884 | if (ret) | |
885 | goto free_trips; | |
886 | } | |
887 | ||
888 | of_node_put(child); | |
889 | ||
890 | /* cooling-maps */ | |
891 | child = of_get_child_by_name(np, "cooling-maps"); | |
892 | ||
893 | /* cooling-maps not provided */ | |
894 | if (!child) | |
895 | goto finish; | |
896 | ||
897 | tz->num_tbps = of_get_child_count(child); | |
898 | if (tz->num_tbps == 0) | |
899 | goto finish; | |
900 | ||
901 | tz->tbps = kzalloc(tz->num_tbps * sizeof(*tz->tbps), GFP_KERNEL); | |
902 | if (!tz->tbps) { | |
903 | ret = -ENOMEM; | |
904 | goto free_trips; | |
905 | } | |
906 | ||
907 | i = 0; | |
ca9521b7 | 908 | for_each_child_of_node(child, gchild) { |
4e5e4705 EV |
909 | ret = thermal_of_populate_bind_params(gchild, &tz->tbps[i++], |
910 | tz->trips, tz->ntrips); | |
911 | if (ret) | |
912 | goto free_tbps; | |
ca9521b7 | 913 | } |
4e5e4705 EV |
914 | |
915 | finish: | |
916 | of_node_put(child); | |
917 | tz->mode = THERMAL_DEVICE_DISABLED; | |
918 | ||
919 | return tz; | |
920 | ||
921 | free_tbps: | |
1cd91c18 | 922 | for (i = i - 1; i >= 0; i--) |
c2aad93c | 923 | of_node_put(tz->tbps[i].cooling_device); |
4e5e4705 EV |
924 | kfree(tz->tbps); |
925 | free_trips: | |
c2aad93c VZ |
926 | for (i = 0; i < tz->ntrips; i++) |
927 | of_node_put(tz->trips[i].np); | |
4e5e4705 | 928 | kfree(tz->trips); |
c2aad93c | 929 | of_node_put(gchild); |
4e5e4705 EV |
930 | free_tz: |
931 | kfree(tz); | |
932 | of_node_put(child); | |
933 | ||
934 | return ERR_PTR(ret); | |
935 | } | |
936 | ||
937 | static inline void of_thermal_free_zone(struct __thermal_zone *tz) | |
938 | { | |
c2aad93c VZ |
939 | int i; |
940 | ||
941 | for (i = 0; i < tz->num_tbps; i++) | |
942 | of_node_put(tz->tbps[i].cooling_device); | |
4e5e4705 | 943 | kfree(tz->tbps); |
c2aad93c VZ |
944 | for (i = 0; i < tz->ntrips; i++) |
945 | of_node_put(tz->trips[i].np); | |
4e5e4705 EV |
946 | kfree(tz->trips); |
947 | kfree(tz); | |
948 | } | |
949 | ||
950 | /** | |
951 | * of_parse_thermal_zones - parse device tree thermal data | |
952 | * | |
953 | * Initialization function that can be called by machine initialization | |
954 | * code to parse thermal data and populate the thermal framework | |
955 | * with hardware thermal zones info. This function only parses thermal zones. | |
956 | * Cooling devices and sensor devices nodes are supposed to be parsed | |
957 | * by their respective drivers. | |
958 | * | |
959 | * Return: 0 on success, proper error code otherwise | |
960 | * | |
961 | */ | |
962 | int __init of_parse_thermal_zones(void) | |
963 | { | |
964 | struct device_node *np, *child; | |
965 | struct __thermal_zone *tz; | |
966 | struct thermal_zone_device_ops *ops; | |
967 | ||
968 | np = of_find_node_by_name(NULL, "thermal-zones"); | |
969 | if (!np) { | |
970 | pr_debug("unable to find thermal zones\n"); | |
971 | return 0; /* Run successfully on systems without thermal DT */ | |
972 | } | |
973 | ||
42bbe400 | 974 | for_each_available_child_of_node(np, child) { |
4e5e4705 EV |
975 | struct thermal_zone_device *zone; |
976 | struct thermal_zone_params *tzp; | |
76af5495 | 977 | int i, mask = 0; |
647f9925 | 978 | u32 prop; |
4e5e4705 EV |
979 | |
980 | tz = thermal_of_build_thermal_zone(child); | |
981 | if (IS_ERR(tz)) { | |
982 | pr_err("failed to build thermal zone %s: %ld\n", | |
983 | child->name, | |
984 | PTR_ERR(tz)); | |
985 | continue; | |
986 | } | |
987 | ||
988 | ops = kmemdup(&of_thermal_ops, sizeof(*ops), GFP_KERNEL); | |
989 | if (!ops) | |
990 | goto exit_free; | |
991 | ||
992 | tzp = kzalloc(sizeof(*tzp), GFP_KERNEL); | |
993 | if (!tzp) { | |
994 | kfree(ops); | |
995 | goto exit_free; | |
996 | } | |
997 | ||
998 | /* No hwmon because there might be hwmon drivers registering */ | |
999 | tzp->no_hwmon = true; | |
1000 | ||
647f9925 PA |
1001 | if (!of_property_read_u32(child, "sustainable-power", &prop)) |
1002 | tzp->sustainable_power = prop; | |
1003 | ||
76af5495 PA |
1004 | for (i = 0; i < tz->ntrips; i++) |
1005 | mask |= 1 << i; | |
1006 | ||
a46dbae8 EV |
1007 | /* these two are left for temperature drivers to use */ |
1008 | tzp->slope = tz->slope; | |
1009 | tzp->offset = tz->offset; | |
1010 | ||
4e5e4705 | 1011 | zone = thermal_zone_device_register(child->name, tz->ntrips, |
76af5495 | 1012 | mask, tz, |
4e5e4705 EV |
1013 | ops, tzp, |
1014 | tz->passive_delay, | |
1015 | tz->polling_delay); | |
1016 | if (IS_ERR(zone)) { | |
1017 | pr_err("Failed to build %s zone %ld\n", child->name, | |
1018 | PTR_ERR(zone)); | |
1019 | kfree(tzp); | |
1020 | kfree(ops); | |
1021 | of_thermal_free_zone(tz); | |
1022 | /* attempting to build remaining zones still */ | |
1023 | } | |
1024 | } | |
c2aad93c | 1025 | of_node_put(np); |
4e5e4705 EV |
1026 | |
1027 | return 0; | |
1028 | ||
1029 | exit_free: | |
c2aad93c VZ |
1030 | of_node_put(child); |
1031 | of_node_put(np); | |
4e5e4705 EV |
1032 | of_thermal_free_zone(tz); |
1033 | ||
1034 | /* no memory available, so free what we have built */ | |
1035 | of_thermal_destroy_zones(); | |
1036 | ||
1037 | return -ENOMEM; | |
1038 | } | |
1039 | ||
1040 | /** | |
1041 | * of_thermal_destroy_zones - remove all zones parsed and allocated resources | |
1042 | * | |
1043 | * Finds all zones parsed and added to the thermal framework and remove them | |
1044 | * from the system, together with their resources. | |
1045 | * | |
1046 | */ | |
1047 | void of_thermal_destroy_zones(void) | |
1048 | { | |
1049 | struct device_node *np, *child; | |
1050 | ||
1051 | np = of_find_node_by_name(NULL, "thermal-zones"); | |
1052 | if (!np) { | |
28524988 | 1053 | pr_debug("unable to find thermal zones\n"); |
4e5e4705 EV |
1054 | return; |
1055 | } | |
1056 | ||
42bbe400 | 1057 | for_each_available_child_of_node(np, child) { |
4e5e4705 EV |
1058 | struct thermal_zone_device *zone; |
1059 | ||
1060 | zone = thermal_zone_get_zone_by_name(child->name); | |
1061 | if (IS_ERR(zone)) | |
1062 | continue; | |
1063 | ||
1064 | thermal_zone_device_unregister(zone); | |
1065 | kfree(zone->tzp); | |
1066 | kfree(zone->ops); | |
1067 | of_thermal_free_zone(zone->devdata); | |
1068 | } | |
c2aad93c | 1069 | of_node_put(np); |
4e5e4705 | 1070 | } |