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 | ||
39 | /** | |
40 | * struct __thermal_trip - representation of a point in temperature domain | |
41 | * @np: pointer to struct device_node that this trip point was created from | |
42 | * @temperature: temperature value in miliCelsius | |
43 | * @hysteresis: relative hysteresis in miliCelsius | |
44 | * @type: trip point type | |
45 | */ | |
46 | ||
47 | struct __thermal_trip { | |
48 | struct device_node *np; | |
49 | unsigned long int temperature; | |
50 | unsigned long int hysteresis; | |
51 | enum thermal_trip_type type; | |
52 | }; | |
53 | ||
54 | /** | |
55 | * struct __thermal_bind_param - a match between trip and cooling device | |
56 | * @cooling_device: a pointer to identify the referred cooling device | |
57 | * @trip_id: the trip point index | |
58 | * @usage: the percentage (from 0 to 100) of cooling contribution | |
59 | * @min: minimum cooling state used at this trip point | |
60 | * @max: maximum cooling state used at this trip point | |
61 | */ | |
62 | ||
63 | struct __thermal_bind_params { | |
64 | struct device_node *cooling_device; | |
65 | unsigned int trip_id; | |
66 | unsigned int usage; | |
67 | unsigned long min; | |
68 | unsigned long max; | |
69 | }; | |
70 | ||
71 | /** | |
72 | * struct __thermal_zone - internal representation of a thermal zone | |
73 | * @mode: current thermal zone device mode (enabled/disabled) | |
74 | * @passive_delay: polling interval while passive cooling is activated | |
75 | * @polling_delay: zone polling interval | |
76 | * @ntrips: number of trip points | |
77 | * @trips: an array of trip points (0..ntrips - 1) | |
78 | * @num_tbps: number of thermal bind params | |
79 | * @tbps: an array of thermal bind params (0..num_tbps - 1) | |
80 | * @sensor_data: sensor private data used while reading temperature and trend | |
2251aef6 | 81 | * @ops: set of callbacks to handle the thermal zone based on DT |
4e5e4705 EV |
82 | */ |
83 | ||
84 | struct __thermal_zone { | |
85 | enum thermal_device_mode mode; | |
86 | int passive_delay; | |
87 | int polling_delay; | |
88 | ||
89 | /* trip data */ | |
90 | int ntrips; | |
91 | struct __thermal_trip *trips; | |
92 | ||
93 | /* cooling binding data */ | |
94 | int num_tbps; | |
95 | struct __thermal_bind_params *tbps; | |
96 | ||
97 | /* sensor interface */ | |
98 | void *sensor_data; | |
2251aef6 | 99 | const struct thermal_zone_of_device_ops *ops; |
4e5e4705 EV |
100 | }; |
101 | ||
102 | /*** DT thermal zone device callbacks ***/ | |
103 | ||
104 | static int of_thermal_get_temp(struct thermal_zone_device *tz, | |
105 | unsigned long *temp) | |
106 | { | |
107 | struct __thermal_zone *data = tz->devdata; | |
108 | ||
2251aef6 | 109 | if (!data->ops->get_temp) |
4e5e4705 EV |
110 | return -EINVAL; |
111 | ||
2251aef6 | 112 | return data->ops->get_temp(data->sensor_data, temp); |
4e5e4705 EV |
113 | } |
114 | ||
115 | static int of_thermal_get_trend(struct thermal_zone_device *tz, int trip, | |
116 | enum thermal_trend *trend) | |
117 | { | |
118 | struct __thermal_zone *data = tz->devdata; | |
119 | long dev_trend; | |
120 | int r; | |
121 | ||
2251aef6 | 122 | if (!data->ops->get_trend) |
4e5e4705 EV |
123 | return -EINVAL; |
124 | ||
2251aef6 | 125 | r = data->ops->get_trend(data->sensor_data, &dev_trend); |
4e5e4705 EV |
126 | if (r) |
127 | return r; | |
128 | ||
129 | /* TODO: These intervals might have some thresholds, but in core code */ | |
130 | if (dev_trend > 0) | |
131 | *trend = THERMAL_TREND_RAISING; | |
132 | else if (dev_trend < 0) | |
133 | *trend = THERMAL_TREND_DROPPING; | |
134 | else | |
135 | *trend = THERMAL_TREND_STABLE; | |
136 | ||
137 | return 0; | |
138 | } | |
139 | ||
140 | static int of_thermal_bind(struct thermal_zone_device *thermal, | |
141 | struct thermal_cooling_device *cdev) | |
142 | { | |
143 | struct __thermal_zone *data = thermal->devdata; | |
144 | int i; | |
145 | ||
146 | if (!data || IS_ERR(data)) | |
147 | return -ENODEV; | |
148 | ||
149 | /* find where to bind */ | |
150 | for (i = 0; i < data->num_tbps; i++) { | |
151 | struct __thermal_bind_params *tbp = data->tbps + i; | |
152 | ||
153 | if (tbp->cooling_device == cdev->np) { | |
154 | int ret; | |
155 | ||
156 | ret = thermal_zone_bind_cooling_device(thermal, | |
157 | tbp->trip_id, cdev, | |
dd354b84 PA |
158 | tbp->max, |
159 | tbp->min); | |
4e5e4705 EV |
160 | if (ret) |
161 | return ret; | |
162 | } | |
163 | } | |
164 | ||
165 | return 0; | |
166 | } | |
167 | ||
168 | static int of_thermal_unbind(struct thermal_zone_device *thermal, | |
169 | struct thermal_cooling_device *cdev) | |
170 | { | |
171 | struct __thermal_zone *data = thermal->devdata; | |
172 | int i; | |
173 | ||
174 | if (!data || IS_ERR(data)) | |
175 | return -ENODEV; | |
176 | ||
177 | /* find where to unbind */ | |
178 | for (i = 0; i < data->num_tbps; i++) { | |
179 | struct __thermal_bind_params *tbp = data->tbps + i; | |
180 | ||
181 | if (tbp->cooling_device == cdev->np) { | |
182 | int ret; | |
183 | ||
184 | ret = thermal_zone_unbind_cooling_device(thermal, | |
185 | tbp->trip_id, cdev); | |
186 | if (ret) | |
187 | return ret; | |
188 | } | |
189 | } | |
190 | ||
191 | return 0; | |
192 | } | |
193 | ||
194 | static int of_thermal_get_mode(struct thermal_zone_device *tz, | |
195 | enum thermal_device_mode *mode) | |
196 | { | |
197 | struct __thermal_zone *data = tz->devdata; | |
198 | ||
199 | *mode = data->mode; | |
200 | ||
201 | return 0; | |
202 | } | |
203 | ||
204 | static int of_thermal_set_mode(struct thermal_zone_device *tz, | |
205 | enum thermal_device_mode mode) | |
206 | { | |
207 | struct __thermal_zone *data = tz->devdata; | |
208 | ||
209 | mutex_lock(&tz->lock); | |
210 | ||
211 | if (mode == THERMAL_DEVICE_ENABLED) | |
212 | tz->polling_delay = data->polling_delay; | |
213 | else | |
214 | tz->polling_delay = 0; | |
215 | ||
216 | mutex_unlock(&tz->lock); | |
217 | ||
218 | data->mode = mode; | |
219 | thermal_zone_device_update(tz); | |
220 | ||
221 | return 0; | |
222 | } | |
223 | ||
224 | static int of_thermal_get_trip_type(struct thermal_zone_device *tz, int trip, | |
225 | enum thermal_trip_type *type) | |
226 | { | |
227 | struct __thermal_zone *data = tz->devdata; | |
228 | ||
229 | if (trip >= data->ntrips || trip < 0) | |
230 | return -EDOM; | |
231 | ||
232 | *type = data->trips[trip].type; | |
233 | ||
234 | return 0; | |
235 | } | |
236 | ||
237 | static int of_thermal_get_trip_temp(struct thermal_zone_device *tz, int trip, | |
238 | unsigned long *temp) | |
239 | { | |
240 | struct __thermal_zone *data = tz->devdata; | |
241 | ||
242 | if (trip >= data->ntrips || trip < 0) | |
243 | return -EDOM; | |
244 | ||
245 | *temp = data->trips[trip].temperature; | |
246 | ||
247 | return 0; | |
248 | } | |
249 | ||
250 | static int of_thermal_set_trip_temp(struct thermal_zone_device *tz, int trip, | |
251 | unsigned long temp) | |
252 | { | |
253 | struct __thermal_zone *data = tz->devdata; | |
254 | ||
255 | if (trip >= data->ntrips || trip < 0) | |
256 | return -EDOM; | |
257 | ||
258 | /* thermal framework should take care of data->mask & (1 << trip) */ | |
259 | data->trips[trip].temperature = temp; | |
260 | ||
261 | return 0; | |
262 | } | |
263 | ||
264 | static int of_thermal_get_trip_hyst(struct thermal_zone_device *tz, int trip, | |
265 | unsigned long *hyst) | |
266 | { | |
267 | struct __thermal_zone *data = tz->devdata; | |
268 | ||
269 | if (trip >= data->ntrips || trip < 0) | |
270 | return -EDOM; | |
271 | ||
272 | *hyst = data->trips[trip].hysteresis; | |
273 | ||
274 | return 0; | |
275 | } | |
276 | ||
277 | static int of_thermal_set_trip_hyst(struct thermal_zone_device *tz, int trip, | |
278 | unsigned long hyst) | |
279 | { | |
280 | struct __thermal_zone *data = tz->devdata; | |
281 | ||
282 | if (trip >= data->ntrips || trip < 0) | |
283 | return -EDOM; | |
284 | ||
285 | /* thermal framework should take care of data->mask & (1 << trip) */ | |
286 | data->trips[trip].hysteresis = hyst; | |
287 | ||
288 | return 0; | |
289 | } | |
290 | ||
291 | static int of_thermal_get_crit_temp(struct thermal_zone_device *tz, | |
292 | unsigned long *temp) | |
293 | { | |
294 | struct __thermal_zone *data = tz->devdata; | |
295 | int i; | |
296 | ||
297 | for (i = 0; i < data->ntrips; i++) | |
298 | if (data->trips[i].type == THERMAL_TRIP_CRITICAL) { | |
299 | *temp = data->trips[i].temperature; | |
300 | return 0; | |
301 | } | |
302 | ||
303 | return -EINVAL; | |
304 | } | |
305 | ||
306 | static struct thermal_zone_device_ops of_thermal_ops = { | |
307 | .get_mode = of_thermal_get_mode, | |
308 | .set_mode = of_thermal_set_mode, | |
309 | ||
310 | .get_trip_type = of_thermal_get_trip_type, | |
311 | .get_trip_temp = of_thermal_get_trip_temp, | |
312 | .set_trip_temp = of_thermal_set_trip_temp, | |
313 | .get_trip_hyst = of_thermal_get_trip_hyst, | |
314 | .set_trip_hyst = of_thermal_set_trip_hyst, | |
315 | .get_crit_temp = of_thermal_get_crit_temp, | |
316 | ||
317 | .bind = of_thermal_bind, | |
318 | .unbind = of_thermal_unbind, | |
319 | }; | |
320 | ||
321 | /*** sensor API ***/ | |
322 | ||
323 | static struct thermal_zone_device * | |
324 | thermal_zone_of_add_sensor(struct device_node *zone, | |
325 | struct device_node *sensor, void *data, | |
2251aef6 | 326 | const struct thermal_zone_of_device_ops *ops) |
4e5e4705 EV |
327 | { |
328 | struct thermal_zone_device *tzd; | |
329 | struct __thermal_zone *tz; | |
330 | ||
331 | tzd = thermal_zone_get_zone_by_name(zone->name); | |
332 | if (IS_ERR(tzd)) | |
333 | return ERR_PTR(-EPROBE_DEFER); | |
334 | ||
335 | tz = tzd->devdata; | |
336 | ||
2251aef6 EV |
337 | if (!ops) |
338 | return ERR_PTR(-EINVAL); | |
339 | ||
4e5e4705 | 340 | mutex_lock(&tzd->lock); |
2251aef6 | 341 | tz->ops = ops; |
4e5e4705 EV |
342 | tz->sensor_data = data; |
343 | ||
344 | tzd->ops->get_temp = of_thermal_get_temp; | |
345 | tzd->ops->get_trend = of_thermal_get_trend; | |
346 | mutex_unlock(&tzd->lock); | |
347 | ||
348 | return tzd; | |
349 | } | |
350 | ||
351 | /** | |
352 | * thermal_zone_of_sensor_register - registers a sensor to a DT thermal zone | |
353 | * @dev: a valid struct device pointer of a sensor device. Must contain | |
354 | * a valid .of_node, for the sensor node. | |
355 | * @sensor_id: a sensor identifier, in case the sensor IP has more | |
356 | * than one sensors | |
357 | * @data: a private pointer (owned by the caller) that will be passed | |
358 | * back, when a temperature reading is needed. | |
2251aef6 | 359 | * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp. |
4e5e4705 EV |
360 | * |
361 | * This function will search the list of thermal zones described in device | |
362 | * tree and look for the zone that refer to the sensor device pointed by | |
363 | * @dev->of_node as temperature providers. For the zone pointing to the | |
364 | * sensor node, the sensor will be added to the DT thermal zone device. | |
365 | * | |
366 | * The thermal zone temperature is provided by the @get_temp function | |
367 | * pointer. When called, it will have the private pointer @data back. | |
368 | * | |
369 | * The thermal zone temperature trend is provided by the @get_trend function | |
370 | * pointer. When called, it will have the private pointer @data back. | |
371 | * | |
372 | * TODO: | |
373 | * 01 - This function must enqueue the new sensor instead of using | |
374 | * it as the only source of temperature values. | |
375 | * | |
376 | * 02 - There must be a way to match the sensor with all thermal zones | |
377 | * that refer to it. | |
378 | * | |
379 | * Return: On success returns a valid struct thermal_zone_device, | |
380 | * otherwise, it returns a corresponding ERR_PTR(). Caller must | |
381 | * check the return value with help of IS_ERR() helper. | |
382 | */ | |
383 | struct thermal_zone_device * | |
2251aef6 EV |
384 | thermal_zone_of_sensor_register(struct device *dev, int sensor_id, void *data, |
385 | const struct thermal_zone_of_device_ops *ops) | |
4e5e4705 EV |
386 | { |
387 | struct device_node *np, *child, *sensor_np; | |
c2aad93c | 388 | struct thermal_zone_device *tzd = ERR_PTR(-ENODEV); |
4e5e4705 EV |
389 | |
390 | np = of_find_node_by_name(NULL, "thermal-zones"); | |
391 | if (!np) | |
392 | return ERR_PTR(-ENODEV); | |
393 | ||
c2aad93c VZ |
394 | if (!dev || !dev->of_node) { |
395 | of_node_put(np); | |
4e5e4705 | 396 | return ERR_PTR(-EINVAL); |
c2aad93c | 397 | } |
4e5e4705 | 398 | |
c2aad93c | 399 | sensor_np = of_node_get(dev->of_node); |
4e5e4705 EV |
400 | |
401 | for_each_child_of_node(np, child) { | |
402 | struct of_phandle_args sensor_specs; | |
403 | int ret, id; | |
404 | ||
a020279e LD |
405 | /* Check whether child is enabled or not */ |
406 | if (!of_device_is_available(child)) | |
407 | continue; | |
408 | ||
4e5e4705 EV |
409 | /* For now, thermal framework supports only 1 sensor per zone */ |
410 | ret = of_parse_phandle_with_args(child, "thermal-sensors", | |
411 | "#thermal-sensor-cells", | |
412 | 0, &sensor_specs); | |
413 | if (ret) | |
414 | continue; | |
415 | ||
416 | if (sensor_specs.args_count >= 1) { | |
417 | id = sensor_specs.args[0]; | |
418 | WARN(sensor_specs.args_count > 1, | |
419 | "%s: too many cells in sensor specifier %d\n", | |
420 | sensor_specs.np->name, sensor_specs.args_count); | |
421 | } else { | |
422 | id = 0; | |
423 | } | |
424 | ||
425 | if (sensor_specs.np == sensor_np && id == sensor_id) { | |
c2aad93c | 426 | tzd = thermal_zone_of_add_sensor(child, sensor_np, |
2251aef6 | 427 | data, ops); |
c2aad93c VZ |
428 | of_node_put(sensor_specs.np); |
429 | of_node_put(child); | |
430 | goto exit; | |
4e5e4705 | 431 | } |
c2aad93c | 432 | of_node_put(sensor_specs.np); |
4e5e4705 | 433 | } |
c2aad93c VZ |
434 | exit: |
435 | of_node_put(sensor_np); | |
4e5e4705 EV |
436 | of_node_put(np); |
437 | ||
c2aad93c | 438 | return tzd; |
4e5e4705 EV |
439 | } |
440 | EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_register); | |
441 | ||
442 | /** | |
443 | * thermal_zone_of_sensor_unregister - unregisters a sensor from a DT thermal zone | |
444 | * @dev: a valid struct device pointer of a sensor device. Must contain | |
445 | * a valid .of_node, for the sensor node. | |
446 | * @tzd: a pointer to struct thermal_zone_device where the sensor is registered. | |
447 | * | |
448 | * This function removes the sensor callbacks and private data from the | |
449 | * thermal zone device registered with thermal_zone_of_sensor_register() | |
450 | * API. It will also silent the zone by remove the .get_temp() and .get_trend() | |
451 | * thermal zone device callbacks. | |
452 | * | |
453 | * TODO: When the support to several sensors per zone is added, this | |
454 | * function must search the sensor list based on @dev parameter. | |
455 | * | |
456 | */ | |
457 | void thermal_zone_of_sensor_unregister(struct device *dev, | |
458 | struct thermal_zone_device *tzd) | |
459 | { | |
460 | struct __thermal_zone *tz; | |
461 | ||
462 | if (!dev || !tzd || !tzd->devdata) | |
463 | return; | |
464 | ||
465 | tz = tzd->devdata; | |
466 | ||
467 | /* no __thermal_zone, nothing to be done */ | |
468 | if (!tz) | |
469 | return; | |
470 | ||
471 | mutex_lock(&tzd->lock); | |
472 | tzd->ops->get_temp = NULL; | |
473 | tzd->ops->get_trend = NULL; | |
474 | ||
2251aef6 | 475 | tz->ops = NULL; |
4e5e4705 EV |
476 | tz->sensor_data = NULL; |
477 | mutex_unlock(&tzd->lock); | |
478 | } | |
479 | EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister); | |
480 | ||
481 | /*** functions parsing device tree nodes ***/ | |
482 | ||
483 | /** | |
484 | * thermal_of_populate_bind_params - parse and fill cooling map data | |
485 | * @np: DT node containing a cooling-map node | |
486 | * @__tbp: data structure to be filled with cooling map info | |
487 | * @trips: array of thermal zone trip points | |
488 | * @ntrips: number of trip points inside trips. | |
489 | * | |
490 | * This function parses a cooling-map type of node represented by | |
491 | * @np parameter and fills the read data into @__tbp data structure. | |
492 | * It needs the already parsed array of trip points of the thermal zone | |
493 | * in consideration. | |
494 | * | |
495 | * Return: 0 on success, proper error code otherwise | |
496 | */ | |
497 | static int thermal_of_populate_bind_params(struct device_node *np, | |
498 | struct __thermal_bind_params *__tbp, | |
499 | struct __thermal_trip *trips, | |
500 | int ntrips) | |
501 | { | |
502 | struct of_phandle_args cooling_spec; | |
503 | struct device_node *trip; | |
504 | int ret, i; | |
505 | u32 prop; | |
506 | ||
507 | /* Default weight. Usage is optional */ | |
508 | __tbp->usage = 0; | |
509 | ret = of_property_read_u32(np, "contribution", &prop); | |
510 | if (ret == 0) | |
511 | __tbp->usage = prop; | |
512 | ||
513 | trip = of_parse_phandle(np, "trip", 0); | |
514 | if (!trip) { | |
515 | pr_err("missing trip property\n"); | |
516 | return -ENODEV; | |
517 | } | |
518 | ||
519 | /* match using device_node */ | |
520 | for (i = 0; i < ntrips; i++) | |
521 | if (trip == trips[i].np) { | |
522 | __tbp->trip_id = i; | |
523 | break; | |
524 | } | |
525 | ||
526 | if (i == ntrips) { | |
527 | ret = -ENODEV; | |
528 | goto end; | |
529 | } | |
530 | ||
531 | ret = of_parse_phandle_with_args(np, "cooling-device", "#cooling-cells", | |
532 | 0, &cooling_spec); | |
533 | if (ret < 0) { | |
534 | pr_err("missing cooling_device property\n"); | |
535 | goto end; | |
536 | } | |
537 | __tbp->cooling_device = cooling_spec.np; | |
538 | if (cooling_spec.args_count >= 2) { /* at least min and max */ | |
539 | __tbp->min = cooling_spec.args[0]; | |
540 | __tbp->max = cooling_spec.args[1]; | |
541 | } else { | |
542 | pr_err("wrong reference to cooling device, missing limits\n"); | |
543 | } | |
544 | ||
545 | end: | |
546 | of_node_put(trip); | |
547 | ||
548 | return ret; | |
549 | } | |
550 | ||
551 | /** | |
552 | * It maps 'enum thermal_trip_type' found in include/linux/thermal.h | |
553 | * into the device tree binding of 'trip', property type. | |
554 | */ | |
555 | static const char * const trip_types[] = { | |
556 | [THERMAL_TRIP_ACTIVE] = "active", | |
557 | [THERMAL_TRIP_PASSIVE] = "passive", | |
558 | [THERMAL_TRIP_HOT] = "hot", | |
559 | [THERMAL_TRIP_CRITICAL] = "critical", | |
560 | }; | |
561 | ||
562 | /** | |
563 | * thermal_of_get_trip_type - Get phy mode for given device_node | |
564 | * @np: Pointer to the given device_node | |
565 | * @type: Pointer to resulting trip type | |
566 | * | |
567 | * The function gets trip type string from property 'type', | |
568 | * and store its index in trip_types table in @type, | |
569 | * | |
570 | * Return: 0 on success, or errno in error case. | |
571 | */ | |
572 | static int thermal_of_get_trip_type(struct device_node *np, | |
573 | enum thermal_trip_type *type) | |
574 | { | |
575 | const char *t; | |
576 | int err, i; | |
577 | ||
578 | err = of_property_read_string(np, "type", &t); | |
579 | if (err < 0) | |
580 | return err; | |
581 | ||
582 | for (i = 0; i < ARRAY_SIZE(trip_types); i++) | |
583 | if (!strcasecmp(t, trip_types[i])) { | |
584 | *type = i; | |
585 | return 0; | |
586 | } | |
587 | ||
588 | return -ENODEV; | |
589 | } | |
590 | ||
591 | /** | |
592 | * thermal_of_populate_trip - parse and fill one trip point data | |
593 | * @np: DT node containing a trip point node | |
594 | * @trip: trip point data structure to be filled up | |
595 | * | |
596 | * This function parses a trip point type of node represented by | |
597 | * @np parameter and fills the read data into @trip data structure. | |
598 | * | |
599 | * Return: 0 on success, proper error code otherwise | |
600 | */ | |
601 | static int thermal_of_populate_trip(struct device_node *np, | |
602 | struct __thermal_trip *trip) | |
603 | { | |
604 | int prop; | |
605 | int ret; | |
606 | ||
607 | ret = of_property_read_u32(np, "temperature", &prop); | |
608 | if (ret < 0) { | |
609 | pr_err("missing temperature property\n"); | |
610 | return ret; | |
611 | } | |
612 | trip->temperature = prop; | |
613 | ||
614 | ret = of_property_read_u32(np, "hysteresis", &prop); | |
615 | if (ret < 0) { | |
616 | pr_err("missing hysteresis property\n"); | |
617 | return ret; | |
618 | } | |
619 | trip->hysteresis = prop; | |
620 | ||
621 | ret = thermal_of_get_trip_type(np, &trip->type); | |
622 | if (ret < 0) { | |
623 | pr_err("wrong trip type property\n"); | |
624 | return ret; | |
625 | } | |
626 | ||
627 | /* Required for cooling map matching */ | |
628 | trip->np = np; | |
c2aad93c | 629 | of_node_get(np); |
4e5e4705 EV |
630 | |
631 | return 0; | |
632 | } | |
633 | ||
634 | /** | |
635 | * thermal_of_build_thermal_zone - parse and fill one thermal zone data | |
636 | * @np: DT node containing a thermal zone node | |
637 | * | |
638 | * This function parses a thermal zone type of node represented by | |
639 | * @np parameter and fills the read data into a __thermal_zone data structure | |
640 | * and return this pointer. | |
641 | * | |
642 | * TODO: Missing properties to parse: thermal-sensor-names and coefficients | |
643 | * | |
644 | * Return: On success returns a valid struct __thermal_zone, | |
645 | * otherwise, it returns a corresponding ERR_PTR(). Caller must | |
646 | * check the return value with help of IS_ERR() helper. | |
647 | */ | |
648 | static struct __thermal_zone * | |
649 | thermal_of_build_thermal_zone(struct device_node *np) | |
650 | { | |
651 | struct device_node *child = NULL, *gchild; | |
652 | struct __thermal_zone *tz; | |
653 | int ret, i; | |
654 | u32 prop; | |
655 | ||
656 | if (!np) { | |
657 | pr_err("no thermal zone np\n"); | |
658 | return ERR_PTR(-EINVAL); | |
659 | } | |
660 | ||
661 | tz = kzalloc(sizeof(*tz), GFP_KERNEL); | |
662 | if (!tz) | |
663 | return ERR_PTR(-ENOMEM); | |
664 | ||
665 | ret = of_property_read_u32(np, "polling-delay-passive", &prop); | |
666 | if (ret < 0) { | |
667 | pr_err("missing polling-delay-passive property\n"); | |
668 | goto free_tz; | |
669 | } | |
670 | tz->passive_delay = prop; | |
671 | ||
672 | ret = of_property_read_u32(np, "polling-delay", &prop); | |
673 | if (ret < 0) { | |
674 | pr_err("missing polling-delay property\n"); | |
675 | goto free_tz; | |
676 | } | |
677 | tz->polling_delay = prop; | |
678 | ||
679 | /* trips */ | |
680 | child = of_get_child_by_name(np, "trips"); | |
681 | ||
682 | /* No trips provided */ | |
683 | if (!child) | |
684 | goto finish; | |
685 | ||
686 | tz->ntrips = of_get_child_count(child); | |
687 | if (tz->ntrips == 0) /* must have at least one child */ | |
688 | goto finish; | |
689 | ||
690 | tz->trips = kzalloc(tz->ntrips * sizeof(*tz->trips), GFP_KERNEL); | |
691 | if (!tz->trips) { | |
692 | ret = -ENOMEM; | |
693 | goto free_tz; | |
694 | } | |
695 | ||
696 | i = 0; | |
697 | for_each_child_of_node(child, gchild) { | |
698 | ret = thermal_of_populate_trip(gchild, &tz->trips[i++]); | |
699 | if (ret) | |
700 | goto free_trips; | |
701 | } | |
702 | ||
703 | of_node_put(child); | |
704 | ||
705 | /* cooling-maps */ | |
706 | child = of_get_child_by_name(np, "cooling-maps"); | |
707 | ||
708 | /* cooling-maps not provided */ | |
709 | if (!child) | |
710 | goto finish; | |
711 | ||
712 | tz->num_tbps = of_get_child_count(child); | |
713 | if (tz->num_tbps == 0) | |
714 | goto finish; | |
715 | ||
716 | tz->tbps = kzalloc(tz->num_tbps * sizeof(*tz->tbps), GFP_KERNEL); | |
717 | if (!tz->tbps) { | |
718 | ret = -ENOMEM; | |
719 | goto free_trips; | |
720 | } | |
721 | ||
722 | i = 0; | |
ca9521b7 | 723 | for_each_child_of_node(child, gchild) { |
4e5e4705 EV |
724 | ret = thermal_of_populate_bind_params(gchild, &tz->tbps[i++], |
725 | tz->trips, tz->ntrips); | |
726 | if (ret) | |
727 | goto free_tbps; | |
ca9521b7 | 728 | } |
4e5e4705 EV |
729 | |
730 | finish: | |
731 | of_node_put(child); | |
732 | tz->mode = THERMAL_DEVICE_DISABLED; | |
733 | ||
734 | return tz; | |
735 | ||
736 | free_tbps: | |
c2aad93c VZ |
737 | for (i = 0; i < tz->num_tbps; i++) |
738 | of_node_put(tz->tbps[i].cooling_device); | |
4e5e4705 EV |
739 | kfree(tz->tbps); |
740 | free_trips: | |
c2aad93c VZ |
741 | for (i = 0; i < tz->ntrips; i++) |
742 | of_node_put(tz->trips[i].np); | |
4e5e4705 | 743 | kfree(tz->trips); |
c2aad93c | 744 | of_node_put(gchild); |
4e5e4705 EV |
745 | free_tz: |
746 | kfree(tz); | |
747 | of_node_put(child); | |
748 | ||
749 | return ERR_PTR(ret); | |
750 | } | |
751 | ||
752 | static inline void of_thermal_free_zone(struct __thermal_zone *tz) | |
753 | { | |
c2aad93c VZ |
754 | int i; |
755 | ||
756 | for (i = 0; i < tz->num_tbps; i++) | |
757 | of_node_put(tz->tbps[i].cooling_device); | |
4e5e4705 | 758 | kfree(tz->tbps); |
c2aad93c VZ |
759 | for (i = 0; i < tz->ntrips; i++) |
760 | of_node_put(tz->trips[i].np); | |
4e5e4705 EV |
761 | kfree(tz->trips); |
762 | kfree(tz); | |
763 | } | |
764 | ||
765 | /** | |
766 | * of_parse_thermal_zones - parse device tree thermal data | |
767 | * | |
768 | * Initialization function that can be called by machine initialization | |
769 | * code to parse thermal data and populate the thermal framework | |
770 | * with hardware thermal zones info. This function only parses thermal zones. | |
771 | * Cooling devices and sensor devices nodes are supposed to be parsed | |
772 | * by their respective drivers. | |
773 | * | |
774 | * Return: 0 on success, proper error code otherwise | |
775 | * | |
776 | */ | |
777 | int __init of_parse_thermal_zones(void) | |
778 | { | |
779 | struct device_node *np, *child; | |
780 | struct __thermal_zone *tz; | |
781 | struct thermal_zone_device_ops *ops; | |
782 | ||
783 | np = of_find_node_by_name(NULL, "thermal-zones"); | |
784 | if (!np) { | |
785 | pr_debug("unable to find thermal zones\n"); | |
786 | return 0; /* Run successfully on systems without thermal DT */ | |
787 | } | |
788 | ||
789 | for_each_child_of_node(np, child) { | |
790 | struct thermal_zone_device *zone; | |
791 | struct thermal_zone_params *tzp; | |
792 | ||
a020279e LD |
793 | /* Check whether child is enabled or not */ |
794 | if (!of_device_is_available(child)) | |
795 | continue; | |
796 | ||
4e5e4705 EV |
797 | tz = thermal_of_build_thermal_zone(child); |
798 | if (IS_ERR(tz)) { | |
799 | pr_err("failed to build thermal zone %s: %ld\n", | |
800 | child->name, | |
801 | PTR_ERR(tz)); | |
802 | continue; | |
803 | } | |
804 | ||
805 | ops = kmemdup(&of_thermal_ops, sizeof(*ops), GFP_KERNEL); | |
806 | if (!ops) | |
807 | goto exit_free; | |
808 | ||
809 | tzp = kzalloc(sizeof(*tzp), GFP_KERNEL); | |
810 | if (!tzp) { | |
811 | kfree(ops); | |
812 | goto exit_free; | |
813 | } | |
814 | ||
815 | /* No hwmon because there might be hwmon drivers registering */ | |
816 | tzp->no_hwmon = true; | |
817 | ||
818 | zone = thermal_zone_device_register(child->name, tz->ntrips, | |
819 | 0, tz, | |
820 | ops, tzp, | |
821 | tz->passive_delay, | |
822 | tz->polling_delay); | |
823 | if (IS_ERR(zone)) { | |
824 | pr_err("Failed to build %s zone %ld\n", child->name, | |
825 | PTR_ERR(zone)); | |
826 | kfree(tzp); | |
827 | kfree(ops); | |
828 | of_thermal_free_zone(tz); | |
829 | /* attempting to build remaining zones still */ | |
830 | } | |
831 | } | |
c2aad93c | 832 | of_node_put(np); |
4e5e4705 EV |
833 | |
834 | return 0; | |
835 | ||
836 | exit_free: | |
c2aad93c VZ |
837 | of_node_put(child); |
838 | of_node_put(np); | |
4e5e4705 EV |
839 | of_thermal_free_zone(tz); |
840 | ||
841 | /* no memory available, so free what we have built */ | |
842 | of_thermal_destroy_zones(); | |
843 | ||
844 | return -ENOMEM; | |
845 | } | |
846 | ||
847 | /** | |
848 | * of_thermal_destroy_zones - remove all zones parsed and allocated resources | |
849 | * | |
850 | * Finds all zones parsed and added to the thermal framework and remove them | |
851 | * from the system, together with their resources. | |
852 | * | |
853 | */ | |
854 | void of_thermal_destroy_zones(void) | |
855 | { | |
856 | struct device_node *np, *child; | |
857 | ||
858 | np = of_find_node_by_name(NULL, "thermal-zones"); | |
859 | if (!np) { | |
860 | pr_err("unable to find thermal zones\n"); | |
861 | return; | |
862 | } | |
863 | ||
864 | for_each_child_of_node(np, child) { | |
865 | struct thermal_zone_device *zone; | |
866 | ||
a020279e LD |
867 | /* Check whether child is enabled or not */ |
868 | if (!of_device_is_available(child)) | |
869 | continue; | |
870 | ||
4e5e4705 EV |
871 | zone = thermal_zone_get_zone_by_name(child->name); |
872 | if (IS_ERR(zone)) | |
873 | continue; | |
874 | ||
875 | thermal_zone_device_unregister(zone); | |
876 | kfree(zone->tzp); | |
877 | kfree(zone->ops); | |
878 | of_thermal_free_zone(zone->devdata); | |
879 | } | |
c2aad93c | 880 | of_node_put(np); |
4e5e4705 | 881 | } |