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