blob: 87b9cfe28eb81334fc07411efef628f5cde950ce [file] [log] [blame]
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001/*
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>
Eduardo Valentin2251aef2014-11-07 21:24:39 -040033#include <linux/thermal.h>
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040034
35#include "thermal_core.h"
36
37/*** Private data structures to represent thermal device tree data ***/
38
39/**
40 * struct __thermal_trip - representation of a point in temperature domain
41 * @np: pointer to struct device_node that this trip point was created from
42 * @temperature: temperature value in miliCelsius
43 * @hysteresis: relative hysteresis in miliCelsius
44 * @type: trip point type
45 */
46
47struct __thermal_trip {
48 struct device_node *np;
49 unsigned long int temperature;
50 unsigned long int hysteresis;
51 enum thermal_trip_type type;
52};
53
54/**
55 * struct __thermal_bind_param - a match between trip and cooling device
56 * @cooling_device: a pointer to identify the referred cooling device
57 * @trip_id: the trip point index
58 * @usage: the percentage (from 0 to 100) of cooling contribution
59 * @min: minimum cooling state used at this trip point
60 * @max: maximum cooling state used at this trip point
61 */
62
63struct __thermal_bind_params {
64 struct device_node *cooling_device;
65 unsigned int trip_id;
66 unsigned int usage;
67 unsigned long min;
68 unsigned long max;
69};
70
71/**
72 * struct __thermal_zone - internal representation of a thermal zone
73 * @mode: current thermal zone device mode (enabled/disabled)
74 * @passive_delay: polling interval while passive cooling is activated
75 * @polling_delay: zone polling interval
76 * @ntrips: number of trip points
77 * @trips: an array of trip points (0..ntrips - 1)
78 * @num_tbps: number of thermal bind params
79 * @tbps: an array of thermal bind params (0..num_tbps - 1)
80 * @sensor_data: sensor private data used while reading temperature and trend
Eduardo Valentin2251aef2014-11-07 21:24:39 -040081 * @ops: set of callbacks to handle the thermal zone based on DT
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040082 */
83
84struct __thermal_zone {
85 enum thermal_device_mode mode;
86 int passive_delay;
87 int polling_delay;
88
89 /* trip data */
90 int ntrips;
91 struct __thermal_trip *trips;
92
93 /* cooling binding data */
94 int num_tbps;
95 struct __thermal_bind_params *tbps;
96
97 /* sensor interface */
98 void *sensor_data;
Eduardo Valentin2251aef2014-11-07 21:24:39 -040099 const struct thermal_zone_of_device_ops *ops;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400100};
101
102/*** DT thermal zone device callbacks ***/
103
104static int of_thermal_get_temp(struct thermal_zone_device *tz,
105 unsigned long *temp)
106{
107 struct __thermal_zone *data = tz->devdata;
108
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400109 if (!data->ops->get_temp)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400110 return -EINVAL;
111
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400112 return data->ops->get_temp(data->sensor_data, temp);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400113}
114
Lukasz Majewski08dab662014-12-08 18:04:17 +0100115/**
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 */
125int 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}
134EXPORT_SYMBOL_GPL(of_thermal_get_ntrips);
135
Lukasz Majewskia9bf2cc2014-12-08 18:04:18 +0100136/**
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 */
146bool 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}
155EXPORT_SYMBOL_GPL(of_thermal_is_trip_valid);
156
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400157static int of_thermal_get_trend(struct thermal_zone_device *tz, int trip,
158 enum thermal_trend *trend)
159{
160 struct __thermal_zone *data = tz->devdata;
161 long dev_trend;
162 int r;
163
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400164 if (!data->ops->get_trend)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400165 return -EINVAL;
166
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400167 r = data->ops->get_trend(data->sensor_data, &dev_trend);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400168 if (r)
169 return r;
170
171 /* TODO: These intervals might have some thresholds, but in core code */
172 if (dev_trend > 0)
173 *trend = THERMAL_TREND_RAISING;
174 else if (dev_trend < 0)
175 *trend = THERMAL_TREND_DROPPING;
176 else
177 *trend = THERMAL_TREND_STABLE;
178
179 return 0;
180}
181
182static int of_thermal_bind(struct thermal_zone_device *thermal,
183 struct thermal_cooling_device *cdev)
184{
185 struct __thermal_zone *data = thermal->devdata;
186 int i;
187
188 if (!data || IS_ERR(data))
189 return -ENODEV;
190
191 /* find where to bind */
192 for (i = 0; i < data->num_tbps; i++) {
193 struct __thermal_bind_params *tbp = data->tbps + i;
194
195 if (tbp->cooling_device == cdev->np) {
196 int ret;
197
198 ret = thermal_zone_bind_cooling_device(thermal,
199 tbp->trip_id, cdev,
Punit Agrawaldd354b82014-06-03 10:59:58 +0100200 tbp->max,
201 tbp->min);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400202 if (ret)
203 return ret;
204 }
205 }
206
207 return 0;
208}
209
210static int of_thermal_unbind(struct thermal_zone_device *thermal,
211 struct thermal_cooling_device *cdev)
212{
213 struct __thermal_zone *data = thermal->devdata;
214 int i;
215
216 if (!data || IS_ERR(data))
217 return -ENODEV;
218
219 /* find where to unbind */
220 for (i = 0; i < data->num_tbps; i++) {
221 struct __thermal_bind_params *tbp = data->tbps + i;
222
223 if (tbp->cooling_device == cdev->np) {
224 int ret;
225
226 ret = thermal_zone_unbind_cooling_device(thermal,
227 tbp->trip_id, cdev);
228 if (ret)
229 return ret;
230 }
231 }
232
233 return 0;
234}
235
236static int of_thermal_get_mode(struct thermal_zone_device *tz,
237 enum thermal_device_mode *mode)
238{
239 struct __thermal_zone *data = tz->devdata;
240
241 *mode = data->mode;
242
243 return 0;
244}
245
246static int of_thermal_set_mode(struct thermal_zone_device *tz,
247 enum thermal_device_mode mode)
248{
249 struct __thermal_zone *data = tz->devdata;
250
251 mutex_lock(&tz->lock);
252
253 if (mode == THERMAL_DEVICE_ENABLED)
254 tz->polling_delay = data->polling_delay;
255 else
256 tz->polling_delay = 0;
257
258 mutex_unlock(&tz->lock);
259
260 data->mode = mode;
261 thermal_zone_device_update(tz);
262
263 return 0;
264}
265
266static int of_thermal_get_trip_type(struct thermal_zone_device *tz, int trip,
267 enum thermal_trip_type *type)
268{
269 struct __thermal_zone *data = tz->devdata;
270
271 if (trip >= data->ntrips || trip < 0)
272 return -EDOM;
273
274 *type = data->trips[trip].type;
275
276 return 0;
277}
278
279static int of_thermal_get_trip_temp(struct thermal_zone_device *tz, int trip,
280 unsigned long *temp)
281{
282 struct __thermal_zone *data = tz->devdata;
283
284 if (trip >= data->ntrips || trip < 0)
285 return -EDOM;
286
287 *temp = data->trips[trip].temperature;
288
289 return 0;
290}
291
292static int of_thermal_set_trip_temp(struct thermal_zone_device *tz, int trip,
293 unsigned long temp)
294{
295 struct __thermal_zone *data = tz->devdata;
296
297 if (trip >= data->ntrips || trip < 0)
298 return -EDOM;
299
300 /* thermal framework should take care of data->mask & (1 << trip) */
301 data->trips[trip].temperature = temp;
302
303 return 0;
304}
305
306static int of_thermal_get_trip_hyst(struct thermal_zone_device *tz, int trip,
307 unsigned long *hyst)
308{
309 struct __thermal_zone *data = tz->devdata;
310
311 if (trip >= data->ntrips || trip < 0)
312 return -EDOM;
313
314 *hyst = data->trips[trip].hysteresis;
315
316 return 0;
317}
318
319static int of_thermal_set_trip_hyst(struct thermal_zone_device *tz, int trip,
320 unsigned long hyst)
321{
322 struct __thermal_zone *data = tz->devdata;
323
324 if (trip >= data->ntrips || trip < 0)
325 return -EDOM;
326
327 /* thermal framework should take care of data->mask & (1 << trip) */
328 data->trips[trip].hysteresis = hyst;
329
330 return 0;
331}
332
333static int of_thermal_get_crit_temp(struct thermal_zone_device *tz,
334 unsigned long *temp)
335{
336 struct __thermal_zone *data = tz->devdata;
337 int i;
338
339 for (i = 0; i < data->ntrips; i++)
340 if (data->trips[i].type == THERMAL_TRIP_CRITICAL) {
341 *temp = data->trips[i].temperature;
342 return 0;
343 }
344
345 return -EINVAL;
346}
347
348static struct thermal_zone_device_ops of_thermal_ops = {
349 .get_mode = of_thermal_get_mode,
350 .set_mode = of_thermal_set_mode,
351
352 .get_trip_type = of_thermal_get_trip_type,
353 .get_trip_temp = of_thermal_get_trip_temp,
354 .set_trip_temp = of_thermal_set_trip_temp,
355 .get_trip_hyst = of_thermal_get_trip_hyst,
356 .set_trip_hyst = of_thermal_set_trip_hyst,
357 .get_crit_temp = of_thermal_get_crit_temp,
358
359 .bind = of_thermal_bind,
360 .unbind = of_thermal_unbind,
361};
362
363/*** sensor API ***/
364
365static struct thermal_zone_device *
366thermal_zone_of_add_sensor(struct device_node *zone,
367 struct device_node *sensor, void *data,
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400368 const struct thermal_zone_of_device_ops *ops)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400369{
370 struct thermal_zone_device *tzd;
371 struct __thermal_zone *tz;
372
373 tzd = thermal_zone_get_zone_by_name(zone->name);
374 if (IS_ERR(tzd))
375 return ERR_PTR(-EPROBE_DEFER);
376
377 tz = tzd->devdata;
378
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400379 if (!ops)
380 return ERR_PTR(-EINVAL);
381
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400382 mutex_lock(&tzd->lock);
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400383 tz->ops = ops;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400384 tz->sensor_data = data;
385
386 tzd->ops->get_temp = of_thermal_get_temp;
387 tzd->ops->get_trend = of_thermal_get_trend;
388 mutex_unlock(&tzd->lock);
389
390 return tzd;
391}
392
393/**
394 * thermal_zone_of_sensor_register - registers a sensor to a DT thermal zone
395 * @dev: a valid struct device pointer of a sensor device. Must contain
396 * a valid .of_node, for the sensor node.
397 * @sensor_id: a sensor identifier, in case the sensor IP has more
398 * than one sensors
399 * @data: a private pointer (owned by the caller) that will be passed
400 * back, when a temperature reading is needed.
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400401 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400402 *
403 * This function will search the list of thermal zones described in device
404 * tree and look for the zone that refer to the sensor device pointed by
405 * @dev->of_node as temperature providers. For the zone pointing to the
406 * sensor node, the sensor will be added to the DT thermal zone device.
407 *
408 * The thermal zone temperature is provided by the @get_temp function
409 * pointer. When called, it will have the private pointer @data back.
410 *
411 * The thermal zone temperature trend is provided by the @get_trend function
412 * pointer. When called, it will have the private pointer @data back.
413 *
414 * TODO:
415 * 01 - This function must enqueue the new sensor instead of using
416 * it as the only source of temperature values.
417 *
418 * 02 - There must be a way to match the sensor with all thermal zones
419 * that refer to it.
420 *
421 * Return: On success returns a valid struct thermal_zone_device,
422 * otherwise, it returns a corresponding ERR_PTR(). Caller must
423 * check the return value with help of IS_ERR() helper.
424 */
425struct thermal_zone_device *
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400426thermal_zone_of_sensor_register(struct device *dev, int sensor_id, void *data,
427 const struct thermal_zone_of_device_ops *ops)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400428{
429 struct device_node *np, *child, *sensor_np;
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300430 struct thermal_zone_device *tzd = ERR_PTR(-ENODEV);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400431
432 np = of_find_node_by_name(NULL, "thermal-zones");
433 if (!np)
434 return ERR_PTR(-ENODEV);
435
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300436 if (!dev || !dev->of_node) {
437 of_node_put(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400438 return ERR_PTR(-EINVAL);
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300439 }
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400440
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300441 sensor_np = of_node_get(dev->of_node);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400442
443 for_each_child_of_node(np, child) {
444 struct of_phandle_args sensor_specs;
445 int ret, id;
446
Laxman Dewangana0202792014-07-25 15:31:58 +0530447 /* Check whether child is enabled or not */
448 if (!of_device_is_available(child))
449 continue;
450
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400451 /* For now, thermal framework supports only 1 sensor per zone */
452 ret = of_parse_phandle_with_args(child, "thermal-sensors",
453 "#thermal-sensor-cells",
454 0, &sensor_specs);
455 if (ret)
456 continue;
457
458 if (sensor_specs.args_count >= 1) {
459 id = sensor_specs.args[0];
460 WARN(sensor_specs.args_count > 1,
461 "%s: too many cells in sensor specifier %d\n",
462 sensor_specs.np->name, sensor_specs.args_count);
463 } else {
464 id = 0;
465 }
466
467 if (sensor_specs.np == sensor_np && id == sensor_id) {
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300468 tzd = thermal_zone_of_add_sensor(child, sensor_np,
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400469 data, ops);
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300470 of_node_put(sensor_specs.np);
471 of_node_put(child);
472 goto exit;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400473 }
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300474 of_node_put(sensor_specs.np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400475 }
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300476exit:
477 of_node_put(sensor_np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400478 of_node_put(np);
479
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300480 return tzd;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400481}
482EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_register);
483
484/**
485 * thermal_zone_of_sensor_unregister - unregisters a sensor from a DT thermal zone
486 * @dev: a valid struct device pointer of a sensor device. Must contain
487 * a valid .of_node, for the sensor node.
488 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
489 *
490 * This function removes the sensor callbacks and private data from the
491 * thermal zone device registered with thermal_zone_of_sensor_register()
492 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
493 * thermal zone device callbacks.
494 *
495 * TODO: When the support to several sensors per zone is added, this
496 * function must search the sensor list based on @dev parameter.
497 *
498 */
499void thermal_zone_of_sensor_unregister(struct device *dev,
500 struct thermal_zone_device *tzd)
501{
502 struct __thermal_zone *tz;
503
504 if (!dev || !tzd || !tzd->devdata)
505 return;
506
507 tz = tzd->devdata;
508
509 /* no __thermal_zone, nothing to be done */
510 if (!tz)
511 return;
512
513 mutex_lock(&tzd->lock);
514 tzd->ops->get_temp = NULL;
515 tzd->ops->get_trend = NULL;
516
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400517 tz->ops = NULL;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400518 tz->sensor_data = NULL;
519 mutex_unlock(&tzd->lock);
520}
521EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister);
522
523/*** functions parsing device tree nodes ***/
524
525/**
526 * thermal_of_populate_bind_params - parse and fill cooling map data
527 * @np: DT node containing a cooling-map node
528 * @__tbp: data structure to be filled with cooling map info
529 * @trips: array of thermal zone trip points
530 * @ntrips: number of trip points inside trips.
531 *
532 * This function parses a cooling-map type of node represented by
533 * @np parameter and fills the read data into @__tbp data structure.
534 * It needs the already parsed array of trip points of the thermal zone
535 * in consideration.
536 *
537 * Return: 0 on success, proper error code otherwise
538 */
539static int thermal_of_populate_bind_params(struct device_node *np,
540 struct __thermal_bind_params *__tbp,
541 struct __thermal_trip *trips,
542 int ntrips)
543{
544 struct of_phandle_args cooling_spec;
545 struct device_node *trip;
546 int ret, i;
547 u32 prop;
548
549 /* Default weight. Usage is optional */
550 __tbp->usage = 0;
551 ret = of_property_read_u32(np, "contribution", &prop);
552 if (ret == 0)
553 __tbp->usage = prop;
554
555 trip = of_parse_phandle(np, "trip", 0);
556 if (!trip) {
557 pr_err("missing trip property\n");
558 return -ENODEV;
559 }
560
561 /* match using device_node */
562 for (i = 0; i < ntrips; i++)
563 if (trip == trips[i].np) {
564 __tbp->trip_id = i;
565 break;
566 }
567
568 if (i == ntrips) {
569 ret = -ENODEV;
570 goto end;
571 }
572
573 ret = of_parse_phandle_with_args(np, "cooling-device", "#cooling-cells",
574 0, &cooling_spec);
575 if (ret < 0) {
576 pr_err("missing cooling_device property\n");
577 goto end;
578 }
579 __tbp->cooling_device = cooling_spec.np;
580 if (cooling_spec.args_count >= 2) { /* at least min and max */
581 __tbp->min = cooling_spec.args[0];
582 __tbp->max = cooling_spec.args[1];
583 } else {
584 pr_err("wrong reference to cooling device, missing limits\n");
585 }
586
587end:
588 of_node_put(trip);
589
590 return ret;
591}
592
593/**
594 * It maps 'enum thermal_trip_type' found in include/linux/thermal.h
595 * into the device tree binding of 'trip', property type.
596 */
597static const char * const trip_types[] = {
598 [THERMAL_TRIP_ACTIVE] = "active",
599 [THERMAL_TRIP_PASSIVE] = "passive",
600 [THERMAL_TRIP_HOT] = "hot",
601 [THERMAL_TRIP_CRITICAL] = "critical",
602};
603
604/**
605 * thermal_of_get_trip_type - Get phy mode for given device_node
606 * @np: Pointer to the given device_node
607 * @type: Pointer to resulting trip type
608 *
609 * The function gets trip type string from property 'type',
610 * and store its index in trip_types table in @type,
611 *
612 * Return: 0 on success, or errno in error case.
613 */
614static int thermal_of_get_trip_type(struct device_node *np,
615 enum thermal_trip_type *type)
616{
617 const char *t;
618 int err, i;
619
620 err = of_property_read_string(np, "type", &t);
621 if (err < 0)
622 return err;
623
624 for (i = 0; i < ARRAY_SIZE(trip_types); i++)
625 if (!strcasecmp(t, trip_types[i])) {
626 *type = i;
627 return 0;
628 }
629
630 return -ENODEV;
631}
632
633/**
634 * thermal_of_populate_trip - parse and fill one trip point data
635 * @np: DT node containing a trip point node
636 * @trip: trip point data structure to be filled up
637 *
638 * This function parses a trip point type of node represented by
639 * @np parameter and fills the read data into @trip data structure.
640 *
641 * Return: 0 on success, proper error code otherwise
642 */
643static int thermal_of_populate_trip(struct device_node *np,
644 struct __thermal_trip *trip)
645{
646 int prop;
647 int ret;
648
649 ret = of_property_read_u32(np, "temperature", &prop);
650 if (ret < 0) {
651 pr_err("missing temperature property\n");
652 return ret;
653 }
654 trip->temperature = prop;
655
656 ret = of_property_read_u32(np, "hysteresis", &prop);
657 if (ret < 0) {
658 pr_err("missing hysteresis property\n");
659 return ret;
660 }
661 trip->hysteresis = prop;
662
663 ret = thermal_of_get_trip_type(np, &trip->type);
664 if (ret < 0) {
665 pr_err("wrong trip type property\n");
666 return ret;
667 }
668
669 /* Required for cooling map matching */
670 trip->np = np;
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300671 of_node_get(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400672
673 return 0;
674}
675
676/**
677 * thermal_of_build_thermal_zone - parse and fill one thermal zone data
678 * @np: DT node containing a thermal zone node
679 *
680 * This function parses a thermal zone type of node represented by
681 * @np parameter and fills the read data into a __thermal_zone data structure
682 * and return this pointer.
683 *
684 * TODO: Missing properties to parse: thermal-sensor-names and coefficients
685 *
686 * Return: On success returns a valid struct __thermal_zone,
687 * otherwise, it returns a corresponding ERR_PTR(). Caller must
688 * check the return value with help of IS_ERR() helper.
689 */
690static struct __thermal_zone *
691thermal_of_build_thermal_zone(struct device_node *np)
692{
693 struct device_node *child = NULL, *gchild;
694 struct __thermal_zone *tz;
695 int ret, i;
696 u32 prop;
697
698 if (!np) {
699 pr_err("no thermal zone np\n");
700 return ERR_PTR(-EINVAL);
701 }
702
703 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
704 if (!tz)
705 return ERR_PTR(-ENOMEM);
706
707 ret = of_property_read_u32(np, "polling-delay-passive", &prop);
708 if (ret < 0) {
709 pr_err("missing polling-delay-passive property\n");
710 goto free_tz;
711 }
712 tz->passive_delay = prop;
713
714 ret = of_property_read_u32(np, "polling-delay", &prop);
715 if (ret < 0) {
716 pr_err("missing polling-delay property\n");
717 goto free_tz;
718 }
719 tz->polling_delay = prop;
720
721 /* trips */
722 child = of_get_child_by_name(np, "trips");
723
724 /* No trips provided */
725 if (!child)
726 goto finish;
727
728 tz->ntrips = of_get_child_count(child);
729 if (tz->ntrips == 0) /* must have at least one child */
730 goto finish;
731
732 tz->trips = kzalloc(tz->ntrips * sizeof(*tz->trips), GFP_KERNEL);
733 if (!tz->trips) {
734 ret = -ENOMEM;
735 goto free_tz;
736 }
737
738 i = 0;
739 for_each_child_of_node(child, gchild) {
740 ret = thermal_of_populate_trip(gchild, &tz->trips[i++]);
741 if (ret)
742 goto free_trips;
743 }
744
745 of_node_put(child);
746
747 /* cooling-maps */
748 child = of_get_child_by_name(np, "cooling-maps");
749
750 /* cooling-maps not provided */
751 if (!child)
752 goto finish;
753
754 tz->num_tbps = of_get_child_count(child);
755 if (tz->num_tbps == 0)
756 goto finish;
757
758 tz->tbps = kzalloc(tz->num_tbps * sizeof(*tz->tbps), GFP_KERNEL);
759 if (!tz->tbps) {
760 ret = -ENOMEM;
761 goto free_trips;
762 }
763
764 i = 0;
Stephen Boydca9521b2014-06-18 16:32:08 -0700765 for_each_child_of_node(child, gchild) {
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400766 ret = thermal_of_populate_bind_params(gchild, &tz->tbps[i++],
767 tz->trips, tz->ntrips);
768 if (ret)
769 goto free_tbps;
Stephen Boydca9521b2014-06-18 16:32:08 -0700770 }
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400771
772finish:
773 of_node_put(child);
774 tz->mode = THERMAL_DEVICE_DISABLED;
775
776 return tz;
777
778free_tbps:
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300779 for (i = 0; i < tz->num_tbps; i++)
780 of_node_put(tz->tbps[i].cooling_device);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400781 kfree(tz->tbps);
782free_trips:
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300783 for (i = 0; i < tz->ntrips; i++)
784 of_node_put(tz->trips[i].np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400785 kfree(tz->trips);
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300786 of_node_put(gchild);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400787free_tz:
788 kfree(tz);
789 of_node_put(child);
790
791 return ERR_PTR(ret);
792}
793
794static inline void of_thermal_free_zone(struct __thermal_zone *tz)
795{
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300796 int i;
797
798 for (i = 0; i < tz->num_tbps; i++)
799 of_node_put(tz->tbps[i].cooling_device);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400800 kfree(tz->tbps);
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300801 for (i = 0; i < tz->ntrips; i++)
802 of_node_put(tz->trips[i].np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400803 kfree(tz->trips);
804 kfree(tz);
805}
806
807/**
808 * of_parse_thermal_zones - parse device tree thermal data
809 *
810 * Initialization function that can be called by machine initialization
811 * code to parse thermal data and populate the thermal framework
812 * with hardware thermal zones info. This function only parses thermal zones.
813 * Cooling devices and sensor devices nodes are supposed to be parsed
814 * by their respective drivers.
815 *
816 * Return: 0 on success, proper error code otherwise
817 *
818 */
819int __init of_parse_thermal_zones(void)
820{
821 struct device_node *np, *child;
822 struct __thermal_zone *tz;
823 struct thermal_zone_device_ops *ops;
824
825 np = of_find_node_by_name(NULL, "thermal-zones");
826 if (!np) {
827 pr_debug("unable to find thermal zones\n");
828 return 0; /* Run successfully on systems without thermal DT */
829 }
830
831 for_each_child_of_node(np, child) {
832 struct thermal_zone_device *zone;
833 struct thermal_zone_params *tzp;
834
Laxman Dewangana0202792014-07-25 15:31:58 +0530835 /* Check whether child is enabled or not */
836 if (!of_device_is_available(child))
837 continue;
838
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400839 tz = thermal_of_build_thermal_zone(child);
840 if (IS_ERR(tz)) {
841 pr_err("failed to build thermal zone %s: %ld\n",
842 child->name,
843 PTR_ERR(tz));
844 continue;
845 }
846
847 ops = kmemdup(&of_thermal_ops, sizeof(*ops), GFP_KERNEL);
848 if (!ops)
849 goto exit_free;
850
851 tzp = kzalloc(sizeof(*tzp), GFP_KERNEL);
852 if (!tzp) {
853 kfree(ops);
854 goto exit_free;
855 }
856
857 /* No hwmon because there might be hwmon drivers registering */
858 tzp->no_hwmon = true;
859
860 zone = thermal_zone_device_register(child->name, tz->ntrips,
861 0, tz,
862 ops, tzp,
863 tz->passive_delay,
864 tz->polling_delay);
865 if (IS_ERR(zone)) {
866 pr_err("Failed to build %s zone %ld\n", child->name,
867 PTR_ERR(zone));
868 kfree(tzp);
869 kfree(ops);
870 of_thermal_free_zone(tz);
871 /* attempting to build remaining zones still */
872 }
873 }
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300874 of_node_put(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400875
876 return 0;
877
878exit_free:
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300879 of_node_put(child);
880 of_node_put(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400881 of_thermal_free_zone(tz);
882
883 /* no memory available, so free what we have built */
884 of_thermal_destroy_zones();
885
886 return -ENOMEM;
887}
888
889/**
890 * of_thermal_destroy_zones - remove all zones parsed and allocated resources
891 *
892 * Finds all zones parsed and added to the thermal framework and remove them
893 * from the system, together with their resources.
894 *
895 */
896void of_thermal_destroy_zones(void)
897{
898 struct device_node *np, *child;
899
900 np = of_find_node_by_name(NULL, "thermal-zones");
901 if (!np) {
902 pr_err("unable to find thermal zones\n");
903 return;
904 }
905
906 for_each_child_of_node(np, child) {
907 struct thermal_zone_device *zone;
908
Laxman Dewangana0202792014-07-25 15:31:58 +0530909 /* Check whether child is enabled or not */
910 if (!of_device_is_available(child))
911 continue;
912
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400913 zone = thermal_zone_get_zone_by_name(child->name);
914 if (IS_ERR(zone))
915 continue;
916
917 thermal_zone_device_unregister(zone);
918 kfree(zone->tzp);
919 kfree(zone->ops);
920 of_thermal_free_zone(zone->devdata);
921 }
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300922 of_node_put(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400923}