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