blob: e062bf59ab6c05de49126291235dfa8d9cf87295 [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/**
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040040 * 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
48struct __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 * @ntrips: number of trip points
62 * @trips: an array of trip points (0..ntrips - 1)
63 * @num_tbps: number of thermal bind params
64 * @tbps: an array of thermal bind params (0..num_tbps - 1)
65 * @sensor_data: sensor private data used while reading temperature and trend
Eduardo Valentin2251aef2014-11-07 21:24:39 -040066 * @ops: set of callbacks to handle the thermal zone based on DT
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040067 */
68
69struct __thermal_zone {
70 enum thermal_device_mode mode;
71 int passive_delay;
72 int polling_delay;
73
74 /* trip data */
75 int ntrips;
Lukasz Majewskiad9914a2014-12-08 18:04:19 +010076 struct thermal_trip *trips;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040077
78 /* cooling binding data */
79 int num_tbps;
80 struct __thermal_bind_params *tbps;
81
82 /* sensor interface */
83 void *sensor_data;
Eduardo Valentin2251aef2014-11-07 21:24:39 -040084 const struct thermal_zone_of_device_ops *ops;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040085};
86
87/*** DT thermal zone device callbacks ***/
88
89static int of_thermal_get_temp(struct thermal_zone_device *tz,
90 unsigned long *temp)
91{
92 struct __thermal_zone *data = tz->devdata;
93
Eduardo Valentin2251aef2014-11-07 21:24:39 -040094 if (!data->ops->get_temp)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040095 return -EINVAL;
96
Eduardo Valentin2251aef2014-11-07 21:24:39 -040097 return data->ops->get_temp(data->sensor_data, temp);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040098}
99
Lukasz Majewski08dab662014-12-08 18:04:17 +0100100/**
101 * of_thermal_get_ntrips - function to export number of available trip
102 * points.
103 * @tz: pointer to a thermal zone
104 *
105 * This function is a globally visible wrapper to get number of trip points
106 * stored in the local struct __thermal_zone
107 *
108 * Return: number of available trip points, -ENODEV when data not available
109 */
110int of_thermal_get_ntrips(struct thermal_zone_device *tz)
111{
112 struct __thermal_zone *data = tz->devdata;
113
114 if (!data || IS_ERR(data))
115 return -ENODEV;
116
117 return data->ntrips;
118}
119EXPORT_SYMBOL_GPL(of_thermal_get_ntrips);
120
Lukasz Majewskia9bf2cc2014-12-08 18:04:18 +0100121/**
122 * of_thermal_is_trip_valid - function to check if trip point is valid
123 *
124 * @tz: pointer to a thermal zone
125 * @trip: trip point to evaluate
126 *
127 * This function is responsible for checking if passed trip point is valid
128 *
129 * Return: true if trip point is valid, false otherwise
130 */
131bool of_thermal_is_trip_valid(struct thermal_zone_device *tz, int trip)
132{
133 struct __thermal_zone *data = tz->devdata;
134
135 if (!data || trip >= data->ntrips || trip < 0)
136 return false;
137
138 return true;
139}
140EXPORT_SYMBOL_GPL(of_thermal_is_trip_valid);
141
Lukasz Majewskice8be772014-12-08 18:04:20 +0100142/**
143 * of_thermal_get_trip_points - function to get access to a globally exported
144 * trip points
145 *
146 * @tz: pointer to a thermal zone
147 *
148 * This function provides a pointer to trip points table
149 *
150 * Return: pointer to trip points table, NULL otherwise
151 */
152const struct thermal_trip * const
153of_thermal_get_trip_points(struct thermal_zone_device *tz)
154{
155 struct __thermal_zone *data = tz->devdata;
156
157 if (!data)
158 return NULL;
159
160 return data->trips;
161}
162EXPORT_SYMBOL_GPL(of_thermal_get_trip_points);
163
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400164static int of_thermal_get_trend(struct thermal_zone_device *tz, int trip,
165 enum thermal_trend *trend)
166{
167 struct __thermal_zone *data = tz->devdata;
168 long dev_trend;
169 int r;
170
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400171 if (!data->ops->get_trend)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400172 return -EINVAL;
173
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400174 r = data->ops->get_trend(data->sensor_data, &dev_trend);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400175 if (r)
176 return r;
177
178 /* TODO: These intervals might have some thresholds, but in core code */
179 if (dev_trend > 0)
180 *trend = THERMAL_TREND_RAISING;
181 else if (dev_trend < 0)
182 *trend = THERMAL_TREND_DROPPING;
183 else
184 *trend = THERMAL_TREND_STABLE;
185
186 return 0;
187}
188
189static int of_thermal_bind(struct thermal_zone_device *thermal,
190 struct thermal_cooling_device *cdev)
191{
192 struct __thermal_zone *data = thermal->devdata;
193 int i;
194
195 if (!data || IS_ERR(data))
196 return -ENODEV;
197
198 /* find where to bind */
199 for (i = 0; i < data->num_tbps; i++) {
200 struct __thermal_bind_params *tbp = data->tbps + i;
201
202 if (tbp->cooling_device == cdev->np) {
203 int ret;
204
205 ret = thermal_zone_bind_cooling_device(thermal,
206 tbp->trip_id, cdev,
Punit Agrawaldd354b82014-06-03 10:59:58 +0100207 tbp->max,
208 tbp->min);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400209 if (ret)
210 return ret;
211 }
212 }
213
214 return 0;
215}
216
217static int of_thermal_unbind(struct thermal_zone_device *thermal,
218 struct thermal_cooling_device *cdev)
219{
220 struct __thermal_zone *data = thermal->devdata;
221 int i;
222
223 if (!data || IS_ERR(data))
224 return -ENODEV;
225
226 /* find where to unbind */
227 for (i = 0; i < data->num_tbps; i++) {
228 struct __thermal_bind_params *tbp = data->tbps + i;
229
230 if (tbp->cooling_device == cdev->np) {
231 int ret;
232
233 ret = thermal_zone_unbind_cooling_device(thermal,
234 tbp->trip_id, cdev);
235 if (ret)
236 return ret;
237 }
238 }
239
240 return 0;
241}
242
243static int of_thermal_get_mode(struct thermal_zone_device *tz,
244 enum thermal_device_mode *mode)
245{
246 struct __thermal_zone *data = tz->devdata;
247
248 *mode = data->mode;
249
250 return 0;
251}
252
253static int of_thermal_set_mode(struct thermal_zone_device *tz,
254 enum thermal_device_mode mode)
255{
256 struct __thermal_zone *data = tz->devdata;
257
258 mutex_lock(&tz->lock);
259
260 if (mode == THERMAL_DEVICE_ENABLED)
261 tz->polling_delay = data->polling_delay;
262 else
263 tz->polling_delay = 0;
264
265 mutex_unlock(&tz->lock);
266
267 data->mode = mode;
268 thermal_zone_device_update(tz);
269
270 return 0;
271}
272
273static int of_thermal_get_trip_type(struct thermal_zone_device *tz, int trip,
274 enum thermal_trip_type *type)
275{
276 struct __thermal_zone *data = tz->devdata;
277
278 if (trip >= data->ntrips || trip < 0)
279 return -EDOM;
280
281 *type = data->trips[trip].type;
282
283 return 0;
284}
285
286static int of_thermal_get_trip_temp(struct thermal_zone_device *tz, int trip,
287 unsigned long *temp)
288{
289 struct __thermal_zone *data = tz->devdata;
290
291 if (trip >= data->ntrips || trip < 0)
292 return -EDOM;
293
294 *temp = data->trips[trip].temperature;
295
296 return 0;
297}
298
299static int of_thermal_set_trip_temp(struct thermal_zone_device *tz, int trip,
300 unsigned long temp)
301{
302 struct __thermal_zone *data = tz->devdata;
303
304 if (trip >= data->ntrips || trip < 0)
305 return -EDOM;
306
307 /* thermal framework should take care of data->mask & (1 << trip) */
308 data->trips[trip].temperature = temp;
309
310 return 0;
311}
312
313static int of_thermal_get_trip_hyst(struct thermal_zone_device *tz, int trip,
314 unsigned long *hyst)
315{
316 struct __thermal_zone *data = tz->devdata;
317
318 if (trip >= data->ntrips || trip < 0)
319 return -EDOM;
320
321 *hyst = data->trips[trip].hysteresis;
322
323 return 0;
324}
325
326static int of_thermal_set_trip_hyst(struct thermal_zone_device *tz, int trip,
327 unsigned long hyst)
328{
329 struct __thermal_zone *data = tz->devdata;
330
331 if (trip >= data->ntrips || trip < 0)
332 return -EDOM;
333
334 /* thermal framework should take care of data->mask & (1 << trip) */
335 data->trips[trip].hysteresis = hyst;
336
337 return 0;
338}
339
340static int of_thermal_get_crit_temp(struct thermal_zone_device *tz,
341 unsigned long *temp)
342{
343 struct __thermal_zone *data = tz->devdata;
344 int i;
345
346 for (i = 0; i < data->ntrips; i++)
347 if (data->trips[i].type == THERMAL_TRIP_CRITICAL) {
348 *temp = data->trips[i].temperature;
349 return 0;
350 }
351
352 return -EINVAL;
353}
354
355static struct thermal_zone_device_ops of_thermal_ops = {
356 .get_mode = of_thermal_get_mode,
357 .set_mode = of_thermal_set_mode,
358
359 .get_trip_type = of_thermal_get_trip_type,
360 .get_trip_temp = of_thermal_get_trip_temp,
361 .set_trip_temp = of_thermal_set_trip_temp,
362 .get_trip_hyst = of_thermal_get_trip_hyst,
363 .set_trip_hyst = of_thermal_set_trip_hyst,
364 .get_crit_temp = of_thermal_get_crit_temp,
365
366 .bind = of_thermal_bind,
367 .unbind = of_thermal_unbind,
368};
369
370/*** sensor API ***/
371
372static struct thermal_zone_device *
373thermal_zone_of_add_sensor(struct device_node *zone,
374 struct device_node *sensor, void *data,
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400375 const struct thermal_zone_of_device_ops *ops)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400376{
377 struct thermal_zone_device *tzd;
378 struct __thermal_zone *tz;
379
380 tzd = thermal_zone_get_zone_by_name(zone->name);
381 if (IS_ERR(tzd))
382 return ERR_PTR(-EPROBE_DEFER);
383
384 tz = tzd->devdata;
385
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400386 if (!ops)
387 return ERR_PTR(-EINVAL);
388
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400389 mutex_lock(&tzd->lock);
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400390 tz->ops = ops;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400391 tz->sensor_data = data;
392
393 tzd->ops->get_temp = of_thermal_get_temp;
394 tzd->ops->get_trend = of_thermal_get_trend;
395 mutex_unlock(&tzd->lock);
396
397 return tzd;
398}
399
400/**
401 * thermal_zone_of_sensor_register - registers a sensor to a DT thermal zone
402 * @dev: a valid struct device pointer of a sensor device. Must contain
403 * a valid .of_node, for the sensor node.
404 * @sensor_id: a sensor identifier, in case the sensor IP has more
405 * than one sensors
406 * @data: a private pointer (owned by the caller) that will be passed
407 * back, when a temperature reading is needed.
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400408 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400409 *
410 * This function will search the list of thermal zones described in device
411 * tree and look for the zone that refer to the sensor device pointed by
412 * @dev->of_node as temperature providers. For the zone pointing to the
413 * sensor node, the sensor will be added to the DT thermal zone device.
414 *
415 * The thermal zone temperature is provided by the @get_temp function
416 * pointer. When called, it will have the private pointer @data back.
417 *
418 * The thermal zone temperature trend is provided by the @get_trend function
419 * pointer. When called, it will have the private pointer @data back.
420 *
421 * TODO:
422 * 01 - This function must enqueue the new sensor instead of using
423 * it as the only source of temperature values.
424 *
425 * 02 - There must be a way to match the sensor with all thermal zones
426 * that refer to it.
427 *
428 * Return: On success returns a valid struct thermal_zone_device,
429 * otherwise, it returns a corresponding ERR_PTR(). Caller must
430 * check the return value with help of IS_ERR() helper.
431 */
432struct thermal_zone_device *
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400433thermal_zone_of_sensor_register(struct device *dev, int sensor_id, void *data,
434 const struct thermal_zone_of_device_ops *ops)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400435{
436 struct device_node *np, *child, *sensor_np;
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300437 struct thermal_zone_device *tzd = ERR_PTR(-ENODEV);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400438
439 np = of_find_node_by_name(NULL, "thermal-zones");
440 if (!np)
441 return ERR_PTR(-ENODEV);
442
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300443 if (!dev || !dev->of_node) {
444 of_node_put(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400445 return ERR_PTR(-EINVAL);
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300446 }
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400447
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300448 sensor_np = of_node_get(dev->of_node);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400449
450 for_each_child_of_node(np, child) {
451 struct of_phandle_args sensor_specs;
452 int ret, id;
453
Laxman Dewangana0202792014-07-25 15:31:58 +0530454 /* Check whether child is enabled or not */
455 if (!of_device_is_available(child))
456 continue;
457
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400458 /* For now, thermal framework supports only 1 sensor per zone */
459 ret = of_parse_phandle_with_args(child, "thermal-sensors",
460 "#thermal-sensor-cells",
461 0, &sensor_specs);
462 if (ret)
463 continue;
464
465 if (sensor_specs.args_count >= 1) {
466 id = sensor_specs.args[0];
467 WARN(sensor_specs.args_count > 1,
468 "%s: too many cells in sensor specifier %d\n",
469 sensor_specs.np->name, sensor_specs.args_count);
470 } else {
471 id = 0;
472 }
473
474 if (sensor_specs.np == sensor_np && id == sensor_id) {
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300475 tzd = thermal_zone_of_add_sensor(child, sensor_np,
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400476 data, ops);
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300477 of_node_put(sensor_specs.np);
478 of_node_put(child);
479 goto exit;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400480 }
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300481 of_node_put(sensor_specs.np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400482 }
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300483exit:
484 of_node_put(sensor_np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400485 of_node_put(np);
486
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300487 return tzd;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400488}
489EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_register);
490
491/**
492 * thermal_zone_of_sensor_unregister - unregisters a sensor from a DT thermal zone
493 * @dev: a valid struct device pointer of a sensor device. Must contain
494 * a valid .of_node, for the sensor node.
495 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
496 *
497 * This function removes the sensor callbacks and private data from the
498 * thermal zone device registered with thermal_zone_of_sensor_register()
499 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
500 * thermal zone device callbacks.
501 *
502 * TODO: When the support to several sensors per zone is added, this
503 * function must search the sensor list based on @dev parameter.
504 *
505 */
506void thermal_zone_of_sensor_unregister(struct device *dev,
507 struct thermal_zone_device *tzd)
508{
509 struct __thermal_zone *tz;
510
511 if (!dev || !tzd || !tzd->devdata)
512 return;
513
514 tz = tzd->devdata;
515
516 /* no __thermal_zone, nothing to be done */
517 if (!tz)
518 return;
519
520 mutex_lock(&tzd->lock);
521 tzd->ops->get_temp = NULL;
522 tzd->ops->get_trend = NULL;
523
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400524 tz->ops = NULL;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400525 tz->sensor_data = NULL;
526 mutex_unlock(&tzd->lock);
527}
528EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister);
529
530/*** functions parsing device tree nodes ***/
531
532/**
533 * thermal_of_populate_bind_params - parse and fill cooling map data
534 * @np: DT node containing a cooling-map node
535 * @__tbp: data structure to be filled with cooling map info
536 * @trips: array of thermal zone trip points
537 * @ntrips: number of trip points inside trips.
538 *
539 * This function parses a cooling-map type of node represented by
540 * @np parameter and fills the read data into @__tbp data structure.
541 * It needs the already parsed array of trip points of the thermal zone
542 * in consideration.
543 *
544 * Return: 0 on success, proper error code otherwise
545 */
546static int thermal_of_populate_bind_params(struct device_node *np,
547 struct __thermal_bind_params *__tbp,
Lukasz Majewskiad9914a2014-12-08 18:04:19 +0100548 struct thermal_trip *trips,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400549 int ntrips)
550{
551 struct of_phandle_args cooling_spec;
552 struct device_node *trip;
553 int ret, i;
554 u32 prop;
555
556 /* Default weight. Usage is optional */
557 __tbp->usage = 0;
558 ret = of_property_read_u32(np, "contribution", &prop);
559 if (ret == 0)
560 __tbp->usage = prop;
561
562 trip = of_parse_phandle(np, "trip", 0);
563 if (!trip) {
564 pr_err("missing trip property\n");
565 return -ENODEV;
566 }
567
568 /* match using device_node */
569 for (i = 0; i < ntrips; i++)
570 if (trip == trips[i].np) {
571 __tbp->trip_id = i;
572 break;
573 }
574
575 if (i == ntrips) {
576 ret = -ENODEV;
577 goto end;
578 }
579
580 ret = of_parse_phandle_with_args(np, "cooling-device", "#cooling-cells",
581 0, &cooling_spec);
582 if (ret < 0) {
583 pr_err("missing cooling_device property\n");
584 goto end;
585 }
586 __tbp->cooling_device = cooling_spec.np;
587 if (cooling_spec.args_count >= 2) { /* at least min and max */
588 __tbp->min = cooling_spec.args[0];
589 __tbp->max = cooling_spec.args[1];
590 } else {
591 pr_err("wrong reference to cooling device, missing limits\n");
592 }
593
594end:
595 of_node_put(trip);
596
597 return ret;
598}
599
600/**
601 * It maps 'enum thermal_trip_type' found in include/linux/thermal.h
602 * into the device tree binding of 'trip', property type.
603 */
604static const char * const trip_types[] = {
605 [THERMAL_TRIP_ACTIVE] = "active",
606 [THERMAL_TRIP_PASSIVE] = "passive",
607 [THERMAL_TRIP_HOT] = "hot",
608 [THERMAL_TRIP_CRITICAL] = "critical",
609};
610
611/**
612 * thermal_of_get_trip_type - Get phy mode for given device_node
613 * @np: Pointer to the given device_node
614 * @type: Pointer to resulting trip type
615 *
616 * The function gets trip type string from property 'type',
617 * and store its index in trip_types table in @type,
618 *
619 * Return: 0 on success, or errno in error case.
620 */
621static int thermal_of_get_trip_type(struct device_node *np,
622 enum thermal_trip_type *type)
623{
624 const char *t;
625 int err, i;
626
627 err = of_property_read_string(np, "type", &t);
628 if (err < 0)
629 return err;
630
631 for (i = 0; i < ARRAY_SIZE(trip_types); i++)
632 if (!strcasecmp(t, trip_types[i])) {
633 *type = i;
634 return 0;
635 }
636
637 return -ENODEV;
638}
639
640/**
641 * thermal_of_populate_trip - parse and fill one trip point data
642 * @np: DT node containing a trip point node
643 * @trip: trip point data structure to be filled up
644 *
645 * This function parses a trip point type of node represented by
646 * @np parameter and fills the read data into @trip data structure.
647 *
648 * Return: 0 on success, proper error code otherwise
649 */
650static int thermal_of_populate_trip(struct device_node *np,
Lukasz Majewskiad9914a2014-12-08 18:04:19 +0100651 struct thermal_trip *trip)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400652{
653 int prop;
654 int ret;
655
656 ret = of_property_read_u32(np, "temperature", &prop);
657 if (ret < 0) {
658 pr_err("missing temperature property\n");
659 return ret;
660 }
661 trip->temperature = prop;
662
663 ret = of_property_read_u32(np, "hysteresis", &prop);
664 if (ret < 0) {
665 pr_err("missing hysteresis property\n");
666 return ret;
667 }
668 trip->hysteresis = prop;
669
670 ret = thermal_of_get_trip_type(np, &trip->type);
671 if (ret < 0) {
672 pr_err("wrong trip type property\n");
673 return ret;
674 }
675
676 /* Required for cooling map matching */
677 trip->np = np;
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300678 of_node_get(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400679
680 return 0;
681}
682
683/**
684 * thermal_of_build_thermal_zone - parse and fill one thermal zone data
685 * @np: DT node containing a thermal zone node
686 *
687 * This function parses a thermal zone type of node represented by
688 * @np parameter and fills the read data into a __thermal_zone data structure
689 * and return this pointer.
690 *
691 * TODO: Missing properties to parse: thermal-sensor-names and coefficients
692 *
693 * Return: On success returns a valid struct __thermal_zone,
694 * otherwise, it returns a corresponding ERR_PTR(). Caller must
695 * check the return value with help of IS_ERR() helper.
696 */
697static struct __thermal_zone *
698thermal_of_build_thermal_zone(struct device_node *np)
699{
700 struct device_node *child = NULL, *gchild;
701 struct __thermal_zone *tz;
702 int ret, i;
703 u32 prop;
704
705 if (!np) {
706 pr_err("no thermal zone np\n");
707 return ERR_PTR(-EINVAL);
708 }
709
710 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
711 if (!tz)
712 return ERR_PTR(-ENOMEM);
713
714 ret = of_property_read_u32(np, "polling-delay-passive", &prop);
715 if (ret < 0) {
716 pr_err("missing polling-delay-passive property\n");
717 goto free_tz;
718 }
719 tz->passive_delay = prop;
720
721 ret = of_property_read_u32(np, "polling-delay", &prop);
722 if (ret < 0) {
723 pr_err("missing polling-delay property\n");
724 goto free_tz;
725 }
726 tz->polling_delay = prop;
727
728 /* trips */
729 child = of_get_child_by_name(np, "trips");
730
731 /* No trips provided */
732 if (!child)
733 goto finish;
734
735 tz->ntrips = of_get_child_count(child);
736 if (tz->ntrips == 0) /* must have at least one child */
737 goto finish;
738
739 tz->trips = kzalloc(tz->ntrips * sizeof(*tz->trips), GFP_KERNEL);
740 if (!tz->trips) {
741 ret = -ENOMEM;
742 goto free_tz;
743 }
744
745 i = 0;
746 for_each_child_of_node(child, gchild) {
747 ret = thermal_of_populate_trip(gchild, &tz->trips[i++]);
748 if (ret)
749 goto free_trips;
750 }
751
752 of_node_put(child);
753
754 /* cooling-maps */
755 child = of_get_child_by_name(np, "cooling-maps");
756
757 /* cooling-maps not provided */
758 if (!child)
759 goto finish;
760
761 tz->num_tbps = of_get_child_count(child);
762 if (tz->num_tbps == 0)
763 goto finish;
764
765 tz->tbps = kzalloc(tz->num_tbps * sizeof(*tz->tbps), GFP_KERNEL);
766 if (!tz->tbps) {
767 ret = -ENOMEM;
768 goto free_trips;
769 }
770
771 i = 0;
Stephen Boydca9521b2014-06-18 16:32:08 -0700772 for_each_child_of_node(child, gchild) {
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400773 ret = thermal_of_populate_bind_params(gchild, &tz->tbps[i++],
774 tz->trips, tz->ntrips);
775 if (ret)
776 goto free_tbps;
Stephen Boydca9521b2014-06-18 16:32:08 -0700777 }
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400778
779finish:
780 of_node_put(child);
781 tz->mode = THERMAL_DEVICE_DISABLED;
782
783 return tz;
784
785free_tbps:
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300786 for (i = 0; i < tz->num_tbps; i++)
787 of_node_put(tz->tbps[i].cooling_device);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400788 kfree(tz->tbps);
789free_trips:
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300790 for (i = 0; i < tz->ntrips; i++)
791 of_node_put(tz->trips[i].np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400792 kfree(tz->trips);
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300793 of_node_put(gchild);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400794free_tz:
795 kfree(tz);
796 of_node_put(child);
797
798 return ERR_PTR(ret);
799}
800
801static inline void of_thermal_free_zone(struct __thermal_zone *tz)
802{
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300803 int i;
804
805 for (i = 0; i < tz->num_tbps; i++)
806 of_node_put(tz->tbps[i].cooling_device);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400807 kfree(tz->tbps);
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300808 for (i = 0; i < tz->ntrips; i++)
809 of_node_put(tz->trips[i].np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400810 kfree(tz->trips);
811 kfree(tz);
812}
813
814/**
815 * of_parse_thermal_zones - parse device tree thermal data
816 *
817 * Initialization function that can be called by machine initialization
818 * code to parse thermal data and populate the thermal framework
819 * with hardware thermal zones info. This function only parses thermal zones.
820 * Cooling devices and sensor devices nodes are supposed to be parsed
821 * by their respective drivers.
822 *
823 * Return: 0 on success, proper error code otherwise
824 *
825 */
826int __init of_parse_thermal_zones(void)
827{
828 struct device_node *np, *child;
829 struct __thermal_zone *tz;
830 struct thermal_zone_device_ops *ops;
831
832 np = of_find_node_by_name(NULL, "thermal-zones");
833 if (!np) {
834 pr_debug("unable to find thermal zones\n");
835 return 0; /* Run successfully on systems without thermal DT */
836 }
837
838 for_each_child_of_node(np, child) {
839 struct thermal_zone_device *zone;
840 struct thermal_zone_params *tzp;
841
Laxman Dewangana0202792014-07-25 15:31:58 +0530842 /* Check whether child is enabled or not */
843 if (!of_device_is_available(child))
844 continue;
845
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400846 tz = thermal_of_build_thermal_zone(child);
847 if (IS_ERR(tz)) {
848 pr_err("failed to build thermal zone %s: %ld\n",
849 child->name,
850 PTR_ERR(tz));
851 continue;
852 }
853
854 ops = kmemdup(&of_thermal_ops, sizeof(*ops), GFP_KERNEL);
855 if (!ops)
856 goto exit_free;
857
858 tzp = kzalloc(sizeof(*tzp), GFP_KERNEL);
859 if (!tzp) {
860 kfree(ops);
861 goto exit_free;
862 }
863
864 /* No hwmon because there might be hwmon drivers registering */
865 tzp->no_hwmon = true;
866
867 zone = thermal_zone_device_register(child->name, tz->ntrips,
868 0, tz,
869 ops, tzp,
870 tz->passive_delay,
871 tz->polling_delay);
872 if (IS_ERR(zone)) {
873 pr_err("Failed to build %s zone %ld\n", child->name,
874 PTR_ERR(zone));
875 kfree(tzp);
876 kfree(ops);
877 of_thermal_free_zone(tz);
878 /* attempting to build remaining zones still */
879 }
880 }
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300881 of_node_put(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400882
883 return 0;
884
885exit_free:
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300886 of_node_put(child);
887 of_node_put(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400888 of_thermal_free_zone(tz);
889
890 /* no memory available, so free what we have built */
891 of_thermal_destroy_zones();
892
893 return -ENOMEM;
894}
895
896/**
897 * of_thermal_destroy_zones - remove all zones parsed and allocated resources
898 *
899 * Finds all zones parsed and added to the thermal framework and remove them
900 * from the system, together with their resources.
901 *
902 */
903void of_thermal_destroy_zones(void)
904{
905 struct device_node *np, *child;
906
907 np = of_find_node_by_name(NULL, "thermal-zones");
908 if (!np) {
909 pr_err("unable to find thermal zones\n");
910 return;
911 }
912
913 for_each_child_of_node(np, child) {
914 struct thermal_zone_device *zone;
915
Laxman Dewangana0202792014-07-25 15:31:58 +0530916 /* Check whether child is enabled or not */
917 if (!of_device_is_available(child))
918 continue;
919
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400920 zone = thermal_zone_get_zone_by_name(child->name);
921 if (IS_ERR(zone))
922 continue;
923
924 thermal_zone_device_unregister(zone);
925 kfree(zone->tzp);
926 kfree(zone->ops);
927 of_thermal_free_zone(zone->devdata);
928 }
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300929 of_node_put(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400930}