thermal: of: implement .set_trips for device tree thermal zones
[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  * @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 -ENOSYS;
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         tzd->ops->set_trips = of_thermal_set_trips;
442         tzd->ops->set_emul_temp = of_thermal_set_emul_temp;
443         mutex_unlock(&tzd->lock);
444
445         return tzd;
446 }
447
448 /**
449  * thermal_zone_of_sensor_register - registers a sensor to a DT thermal zone
450  * @dev: a valid struct device pointer of a sensor device. Must contain
451  *       a valid .of_node, for the sensor node.
452  * @sensor_id: a sensor identifier, in case the sensor IP has more
453  *             than one sensors
454  * @data: a private pointer (owned by the caller) that will be passed
455  *        back, when a temperature reading is needed.
456  * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
457  *
458  * This function will search the list of thermal zones described in device
459  * tree and look for the zone that refer to the sensor device pointed by
460  * @dev->of_node as temperature providers. For the zone pointing to the
461  * sensor node, the sensor will be added to the DT thermal zone device.
462  *
463  * The thermal zone temperature is provided by the @get_temp function
464  * pointer. When called, it will have the private pointer @data back.
465  *
466  * The thermal zone temperature trend is provided by the @get_trend function
467  * pointer. When called, it will have the private pointer @data back.
468  *
469  * TODO:
470  * 01 - This function must enqueue the new sensor instead of using
471  * it as the only source of temperature values.
472  *
473  * 02 - There must be a way to match the sensor with all thermal zones
474  * that refer to it.
475  *
476  * Return: On success returns a valid struct thermal_zone_device,
477  * otherwise, it returns a corresponding ERR_PTR(). Caller must
478  * check the return value with help of IS_ERR() helper.
479  */
480 struct thermal_zone_device *
481 thermal_zone_of_sensor_register(struct device *dev, int sensor_id, void *data,
482                                 const struct thermal_zone_of_device_ops *ops)
483 {
484         struct device_node *np, *child, *sensor_np;
485         struct thermal_zone_device *tzd = ERR_PTR(-ENODEV);
486
487         np = of_find_node_by_name(NULL, "thermal-zones");
488         if (!np)
489                 return ERR_PTR(-ENODEV);
490
491         if (!dev || !dev->of_node) {
492                 of_node_put(np);
493                 return ERR_PTR(-EINVAL);
494         }
495
496         sensor_np = of_node_get(dev->of_node);
497
498         for_each_child_of_node(np, child) {
499                 struct of_phandle_args sensor_specs;
500                 int ret, id;
501
502                 /* Check whether child is enabled or not */
503                 if (!of_device_is_available(child))
504                         continue;
505
506                 /* For now, thermal framework supports only 1 sensor per zone */
507                 ret = of_parse_phandle_with_args(child, "thermal-sensors",
508                                                  "#thermal-sensor-cells",
509                                                  0, &sensor_specs);
510                 if (ret)
511                         continue;
512
513                 if (sensor_specs.args_count >= 1) {
514                         id = sensor_specs.args[0];
515                         WARN(sensor_specs.args_count > 1,
516                              "%s: too many cells in sensor specifier %d\n",
517                              sensor_specs.np->name, sensor_specs.args_count);
518                 } else {
519                         id = 0;
520                 }
521
522                 if (sensor_specs.np == sensor_np && id == sensor_id) {
523                         tzd = thermal_zone_of_add_sensor(child, sensor_np,
524                                                          data, ops);
525                         if (!IS_ERR(tzd))
526                                 tzd->ops->set_mode(tzd, THERMAL_DEVICE_ENABLED);
527
528                         of_node_put(sensor_specs.np);
529                         of_node_put(child);
530                         goto exit;
531                 }
532                 of_node_put(sensor_specs.np);
533         }
534 exit:
535         of_node_put(sensor_np);
536         of_node_put(np);
537
538         return tzd;
539 }
540 EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_register);
541
542 /**
543  * thermal_zone_of_sensor_unregister - unregisters a sensor from a DT thermal zone
544  * @dev: a valid struct device pointer of a sensor device. Must contain
545  *       a valid .of_node, for the sensor node.
546  * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
547  *
548  * This function removes the sensor callbacks and private data from the
549  * thermal zone device registered with thermal_zone_of_sensor_register()
550  * API. It will also silent the zone by remove the .get_temp() and .get_trend()
551  * thermal zone device callbacks.
552  *
553  * TODO: When the support to several sensors per zone is added, this
554  * function must search the sensor list based on @dev parameter.
555  *
556  */
557 void thermal_zone_of_sensor_unregister(struct device *dev,
558                                        struct thermal_zone_device *tzd)
559 {
560         struct __thermal_zone *tz;
561
562         if (!dev || !tzd || !tzd->devdata)
563                 return;
564
565         tz = tzd->devdata;
566
567         /* no __thermal_zone, nothing to be done */
568         if (!tz)
569                 return;
570
571         mutex_lock(&tzd->lock);
572         tzd->ops->get_temp = NULL;
573         tzd->ops->get_trend = NULL;
574         tzd->ops->set_emul_temp = NULL;
575
576         tz->ops = NULL;
577         tz->sensor_data = NULL;
578         mutex_unlock(&tzd->lock);
579 }
580 EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister);
581
582 static void devm_thermal_zone_of_sensor_release(struct device *dev, void *res)
583 {
584         thermal_zone_of_sensor_unregister(dev,
585                                           *(struct thermal_zone_device **)res);
586 }
587
588 static int devm_thermal_zone_of_sensor_match(struct device *dev, void *res,
589                                              void *data)
590 {
591         struct thermal_zone_device **r = res;
592
593         if (WARN_ON(!r || !*r))
594                 return 0;
595
596         return *r == data;
597 }
598
599 /**
600  * devm_thermal_zone_of_sensor_register - Resource managed version of
601  *                              thermal_zone_of_sensor_register()
602  * @dev: a valid struct device pointer of a sensor device. Must contain
603  *       a valid .of_node, for the sensor node.
604  * @sensor_id: a sensor identifier, in case the sensor IP has more
605  *             than one sensors
606  * @data: a private pointer (owned by the caller) that will be passed
607  *        back, when a temperature reading is needed.
608  * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
609  *
610  * Refer thermal_zone_of_sensor_register() for more details.
611  *
612  * Return: On success returns a valid struct thermal_zone_device,
613  * otherwise, it returns a corresponding ERR_PTR(). Caller must
614  * check the return value with help of IS_ERR() helper.
615  * Registered hermal_zone_device device will automatically be
616  * released when device is unbounded.
617  */
618 struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
619         struct device *dev, int sensor_id,
620         void *data, const struct thermal_zone_of_device_ops *ops)
621 {
622         struct thermal_zone_device **ptr, *tzd;
623
624         ptr = devres_alloc(devm_thermal_zone_of_sensor_release, sizeof(*ptr),
625                            GFP_KERNEL);
626         if (!ptr)
627                 return ERR_PTR(-ENOMEM);
628
629         tzd = thermal_zone_of_sensor_register(dev, sensor_id, data, ops);
630         if (IS_ERR(tzd)) {
631                 devres_free(ptr);
632                 return tzd;
633         }
634
635         *ptr = tzd;
636         devres_add(dev, ptr);
637
638         return tzd;
639 }
640 EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_register);
641
642 /**
643  * devm_thermal_zone_of_sensor_unregister - Resource managed version of
644  *                              thermal_zone_of_sensor_unregister().
645  * @dev: Device for which which resource was allocated.
646  * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
647  *
648  * This function removes the sensor callbacks and private data from the
649  * thermal zone device registered with devm_thermal_zone_of_sensor_register()
650  * API. It will also silent the zone by remove the .get_temp() and .get_trend()
651  * thermal zone device callbacks.
652  * Normally this function will not need to be called and the resource
653  * management code will ensure that the resource is freed.
654  */
655 void devm_thermal_zone_of_sensor_unregister(struct device *dev,
656                                             struct thermal_zone_device *tzd)
657 {
658         WARN_ON(devres_release(dev, devm_thermal_zone_of_sensor_release,
659                                devm_thermal_zone_of_sensor_match, tzd));
660 }
661 EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_unregister);
662
663 /***   functions parsing device tree nodes   ***/
664
665 /**
666  * thermal_of_populate_bind_params - parse and fill cooling map data
667  * @np: DT node containing a cooling-map node
668  * @__tbp: data structure to be filled with cooling map info
669  * @trips: array of thermal zone trip points
670  * @ntrips: number of trip points inside trips.
671  *
672  * This function parses a cooling-map type of node represented by
673  * @np parameter and fills the read data into @__tbp data structure.
674  * It needs the already parsed array of trip points of the thermal zone
675  * in consideration.
676  *
677  * Return: 0 on success, proper error code otherwise
678  */
679 static int thermal_of_populate_bind_params(struct device_node *np,
680                                            struct __thermal_bind_params *__tbp,
681                                            struct thermal_trip *trips,
682                                            int ntrips)
683 {
684         struct of_phandle_args cooling_spec;
685         struct device_node *trip;
686         int ret, i;
687         u32 prop;
688
689         /* Default weight. Usage is optional */
690         __tbp->usage = THERMAL_WEIGHT_DEFAULT;
691         ret = of_property_read_u32(np, "contribution", &prop);
692         if (ret == 0)
693                 __tbp->usage = prop;
694
695         trip = of_parse_phandle(np, "trip", 0);
696         if (!trip) {
697                 pr_err("missing trip property\n");
698                 return -ENODEV;
699         }
700
701         /* match using device_node */
702         for (i = 0; i < ntrips; i++)
703                 if (trip == trips[i].np) {
704                         __tbp->trip_id = i;
705                         break;
706                 }
707
708         if (i == ntrips) {
709                 ret = -ENODEV;
710                 goto end;
711         }
712
713         ret = of_parse_phandle_with_args(np, "cooling-device", "#cooling-cells",
714                                          0, &cooling_spec);
715         if (ret < 0) {
716                 pr_err("missing cooling_device property\n");
717                 goto end;
718         }
719         __tbp->cooling_device = cooling_spec.np;
720         if (cooling_spec.args_count >= 2) { /* at least min and max */
721                 __tbp->min = cooling_spec.args[0];
722                 __tbp->max = cooling_spec.args[1];
723         } else {
724                 pr_err("wrong reference to cooling device, missing limits\n");
725         }
726
727 end:
728         of_node_put(trip);
729
730         return ret;
731 }
732
733 /**
734  * It maps 'enum thermal_trip_type' found in include/linux/thermal.h
735  * into the device tree binding of 'trip', property type.
736  */
737 static const char * const trip_types[] = {
738         [THERMAL_TRIP_ACTIVE]   = "active",
739         [THERMAL_TRIP_PASSIVE]  = "passive",
740         [THERMAL_TRIP_HOT]      = "hot",
741         [THERMAL_TRIP_CRITICAL] = "critical",
742 };
743
744 /**
745  * thermal_of_get_trip_type - Get phy mode for given device_node
746  * @np: Pointer to the given device_node
747  * @type: Pointer to resulting trip type
748  *
749  * The function gets trip type string from property 'type',
750  * and store its index in trip_types table in @type,
751  *
752  * Return: 0 on success, or errno in error case.
753  */
754 static int thermal_of_get_trip_type(struct device_node *np,
755                                     enum thermal_trip_type *type)
756 {
757         const char *t;
758         int err, i;
759
760         err = of_property_read_string(np, "type", &t);
761         if (err < 0)
762                 return err;
763
764         for (i = 0; i < ARRAY_SIZE(trip_types); i++)
765                 if (!strcasecmp(t, trip_types[i])) {
766                         *type = i;
767                         return 0;
768                 }
769
770         return -ENODEV;
771 }
772
773 /**
774  * thermal_of_populate_trip - parse and fill one trip point data
775  * @np: DT node containing a trip point node
776  * @trip: trip point data structure to be filled up
777  *
778  * This function parses a trip point type of node represented by
779  * @np parameter and fills the read data into @trip data structure.
780  *
781  * Return: 0 on success, proper error code otherwise
782  */
783 static int thermal_of_populate_trip(struct device_node *np,
784                                     struct thermal_trip *trip)
785 {
786         int prop;
787         int ret;
788
789         ret = of_property_read_u32(np, "temperature", &prop);
790         if (ret < 0) {
791                 pr_err("missing temperature property\n");
792                 return ret;
793         }
794         trip->temperature = prop;
795
796         ret = of_property_read_u32(np, "hysteresis", &prop);
797         if (ret < 0) {
798                 pr_err("missing hysteresis property\n");
799                 return ret;
800         }
801         trip->hysteresis = prop;
802
803         ret = thermal_of_get_trip_type(np, &trip->type);
804         if (ret < 0) {
805                 pr_err("wrong trip type property\n");
806                 return ret;
807         }
808
809         /* Required for cooling map matching */
810         trip->np = np;
811         of_node_get(np);
812
813         return 0;
814 }
815
816 /**
817  * thermal_of_build_thermal_zone - parse and fill one thermal zone data
818  * @np: DT node containing a thermal zone node
819  *
820  * This function parses a thermal zone type of node represented by
821  * @np parameter and fills the read data into a __thermal_zone data structure
822  * and return this pointer.
823  *
824  * TODO: Missing properties to parse: thermal-sensor-names
825  *
826  * Return: On success returns a valid struct __thermal_zone,
827  * otherwise, it returns a corresponding ERR_PTR(). Caller must
828  * check the return value with help of IS_ERR() helper.
829  */
830 static struct __thermal_zone *
831 thermal_of_build_thermal_zone(struct device_node *np)
832 {
833         struct device_node *child = NULL, *gchild;
834         struct __thermal_zone *tz;
835         int ret, i;
836         u32 prop, coef[2];
837
838         if (!np) {
839                 pr_err("no thermal zone np\n");
840                 return ERR_PTR(-EINVAL);
841         }
842
843         tz = kzalloc(sizeof(*tz), GFP_KERNEL);
844         if (!tz)
845                 return ERR_PTR(-ENOMEM);
846
847         ret = of_property_read_u32(np, "polling-delay-passive", &prop);
848         if (ret < 0) {
849                 pr_err("missing polling-delay-passive property\n");
850                 goto free_tz;
851         }
852         tz->passive_delay = prop;
853
854         ret = of_property_read_u32(np, "polling-delay", &prop);
855         if (ret < 0) {
856                 pr_err("missing polling-delay property\n");
857                 goto free_tz;
858         }
859         tz->polling_delay = prop;
860
861         /*
862          * REVIST: for now, the thermal framework supports only
863          * one sensor per thermal zone. Thus, we are considering
864          * only the first two values as slope and offset.
865          */
866         ret = of_property_read_u32_array(np, "coefficients", coef, 2);
867         if (ret == 0) {
868                 tz->slope = coef[0];
869                 tz->offset = coef[1];
870         } else {
871                 tz->slope = 1;
872                 tz->offset = 0;
873         }
874
875         /* trips */
876         child = of_get_child_by_name(np, "trips");
877
878         /* No trips provided */
879         if (!child)
880                 goto finish;
881
882         tz->ntrips = of_get_child_count(child);
883         if (tz->ntrips == 0) /* must have at least one child */
884                 goto finish;
885
886         tz->trips = kzalloc(tz->ntrips * sizeof(*tz->trips), GFP_KERNEL);
887         if (!tz->trips) {
888                 ret = -ENOMEM;
889                 goto free_tz;
890         }
891
892         i = 0;
893         for_each_child_of_node(child, gchild) {
894                 ret = thermal_of_populate_trip(gchild, &tz->trips[i++]);
895                 if (ret)
896                         goto free_trips;
897         }
898
899         of_node_put(child);
900
901         /* cooling-maps */
902         child = of_get_child_by_name(np, "cooling-maps");
903
904         /* cooling-maps not provided */
905         if (!child)
906                 goto finish;
907
908         tz->num_tbps = of_get_child_count(child);
909         if (tz->num_tbps == 0)
910                 goto finish;
911
912         tz->tbps = kzalloc(tz->num_tbps * sizeof(*tz->tbps), GFP_KERNEL);
913         if (!tz->tbps) {
914                 ret = -ENOMEM;
915                 goto free_trips;
916         }
917
918         i = 0;
919         for_each_child_of_node(child, gchild) {
920                 ret = thermal_of_populate_bind_params(gchild, &tz->tbps[i++],
921                                                       tz->trips, tz->ntrips);
922                 if (ret)
923                         goto free_tbps;
924         }
925
926 finish:
927         of_node_put(child);
928         tz->mode = THERMAL_DEVICE_DISABLED;
929
930         return tz;
931
932 free_tbps:
933         for (i = 0; i < tz->num_tbps; i++)
934                 of_node_put(tz->tbps[i].cooling_device);
935         kfree(tz->tbps);
936 free_trips:
937         for (i = 0; i < tz->ntrips; i++)
938                 of_node_put(tz->trips[i].np);
939         kfree(tz->trips);
940         of_node_put(gchild);
941 free_tz:
942         kfree(tz);
943         of_node_put(child);
944
945         return ERR_PTR(ret);
946 }
947
948 static inline void of_thermal_free_zone(struct __thermal_zone *tz)
949 {
950         int i;
951
952         for (i = 0; i < tz->num_tbps; i++)
953                 of_node_put(tz->tbps[i].cooling_device);
954         kfree(tz->tbps);
955         for (i = 0; i < tz->ntrips; i++)
956                 of_node_put(tz->trips[i].np);
957         kfree(tz->trips);
958         kfree(tz);
959 }
960
961 /**
962  * of_parse_thermal_zones - parse device tree thermal data
963  *
964  * Initialization function that can be called by machine initialization
965  * code to parse thermal data and populate the thermal framework
966  * with hardware thermal zones info. This function only parses thermal zones.
967  * Cooling devices and sensor devices nodes are supposed to be parsed
968  * by their respective drivers.
969  *
970  * Return: 0 on success, proper error code otherwise
971  *
972  */
973 int __init of_parse_thermal_zones(void)
974 {
975         struct device_node *np, *child;
976         struct __thermal_zone *tz;
977         struct thermal_zone_device_ops *ops;
978
979         np = of_find_node_by_name(NULL, "thermal-zones");
980         if (!np) {
981                 pr_debug("unable to find thermal zones\n");
982                 return 0; /* Run successfully on systems without thermal DT */
983         }
984
985         for_each_child_of_node(np, child) {
986                 struct thermal_zone_device *zone;
987                 struct thermal_zone_params *tzp;
988                 int i, mask = 0;
989                 u32 prop;
990
991                 /* Check whether child is enabled or not */
992                 if (!of_device_is_available(child))
993                         continue;
994
995                 tz = thermal_of_build_thermal_zone(child);
996                 if (IS_ERR(tz)) {
997                         pr_err("failed to build thermal zone %s: %ld\n",
998                                child->name,
999                                PTR_ERR(tz));
1000                         continue;
1001                 }
1002
1003                 ops = kmemdup(&of_thermal_ops, sizeof(*ops), GFP_KERNEL);
1004                 if (!ops)
1005                         goto exit_free;
1006
1007                 tzp = kzalloc(sizeof(*tzp), GFP_KERNEL);
1008                 if (!tzp) {
1009                         kfree(ops);
1010                         goto exit_free;
1011                 }
1012
1013                 /* No hwmon because there might be hwmon drivers registering */
1014                 tzp->no_hwmon = true;
1015
1016                 if (!of_property_read_u32(child, "sustainable-power", &prop))
1017                         tzp->sustainable_power = prop;
1018
1019                 for (i = 0; i < tz->ntrips; i++)
1020                         mask |= 1 << i;
1021
1022                 /* these two are left for temperature drivers to use */
1023                 tzp->slope = tz->slope;
1024                 tzp->offset = tz->offset;
1025
1026                 zone = thermal_zone_device_register(child->name, tz->ntrips,
1027                                                     mask, tz,
1028                                                     ops, tzp,
1029                                                     tz->passive_delay,
1030                                                     tz->polling_delay);
1031                 if (IS_ERR(zone)) {
1032                         pr_err("Failed to build %s zone %ld\n", child->name,
1033                                PTR_ERR(zone));
1034                         kfree(tzp);
1035                         kfree(ops);
1036                         of_thermal_free_zone(tz);
1037                         /* attempting to build remaining zones still */
1038                 }
1039         }
1040         of_node_put(np);
1041
1042         return 0;
1043
1044 exit_free:
1045         of_node_put(child);
1046         of_node_put(np);
1047         of_thermal_free_zone(tz);
1048
1049         /* no memory available, so free what we have built */
1050         of_thermal_destroy_zones();
1051
1052         return -ENOMEM;
1053 }
1054
1055 /**
1056  * of_thermal_destroy_zones - remove all zones parsed and allocated resources
1057  *
1058  * Finds all zones parsed and added to the thermal framework and remove them
1059  * from the system, together with their resources.
1060  *
1061  */
1062 void of_thermal_destroy_zones(void)
1063 {
1064         struct device_node *np, *child;
1065
1066         np = of_find_node_by_name(NULL, "thermal-zones");
1067         if (!np) {
1068                 pr_debug("unable to find thermal zones\n");
1069                 return;
1070         }
1071
1072         for_each_child_of_node(np, child) {
1073                 struct thermal_zone_device *zone;
1074
1075                 /* Check whether child is enabled or not */
1076                 if (!of_device_is_available(child))
1077                         continue;
1078
1079                 zone = thermal_zone_get_zone_by_name(child->name);
1080                 if (IS_ERR(zone))
1081                         continue;
1082
1083                 thermal_zone_device_unregister(zone);
1084                 kfree(zone->tzp);
1085                 kfree(zone->ops);
1086                 of_thermal_free_zone(zone->devdata);
1087         }
1088         of_node_put(np);
1089 }