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