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