blob: 2d2a06f155e2a08593369e1f76642f8fb49e783e [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
Eduardo Valentina46dbae2015-05-11 19:48:09 -070061 * @slope: slope of the temperature adjustment curve
62 * @offset: offset of the temperature adjustment curve
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040063 * @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
Eduardo Valentin2251aef2014-11-07 21:24:39 -040068 * @ops: set of callbacks to handle the thermal zone based on DT
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040069 */
70
71struct __thermal_zone {
72 enum thermal_device_mode mode;
73 int passive_delay;
74 int polling_delay;
Eduardo Valentina46dbae2015-05-11 19:48:09 -070075 int slope;
76 int offset;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040077
78 /* trip data */
79 int ntrips;
Lukasz Majewskiad9914a2014-12-08 18:04:19 +010080 struct thermal_trip *trips;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040081
82 /* cooling binding data */
83 int num_tbps;
84 struct __thermal_bind_params *tbps;
85
86 /* sensor interface */
87 void *sensor_data;
Eduardo Valentin2251aef2014-11-07 21:24:39 -040088 const struct thermal_zone_of_device_ops *ops;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040089};
90
91/*** DT thermal zone device callbacks ***/
92
93static int of_thermal_get_temp(struct thermal_zone_device *tz,
Sascha Hauer17e83512015-07-24 08:12:54 +020094 int *temp)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040095{
96 struct __thermal_zone *data = tz->devdata;
97
Eduardo Valentin2251aef2014-11-07 21:24:39 -040098 if (!data->ops->get_temp)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040099 return -EINVAL;
100
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400101 return data->ops->get_temp(data->sensor_data, temp);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400102}
103
Sascha Hauer826386e2016-06-22 16:42:02 +0800104static int of_thermal_set_trips(struct thermal_zone_device *tz,
105 int low, int high)
106{
107 struct __thermal_zone *data = tz->devdata;
108
109 if (!data->ops || !data->ops->set_trips)
110 return -EINVAL;
111
112 return data->ops->set_trips(data->sensor_data, low, high);
113}
114
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
Lukasz Majewskice8be772014-12-08 18:04:20 +0100157/**
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 */
Geert Uytterhoevenebc31932015-01-03 22:56:56 +0100167const struct thermal_trip *
Lukasz Majewskice8be772014-12-08 18:04:20 +0100168of_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}
177EXPORT_SYMBOL_GPL(of_thermal_get_trip_points);
178
Lukasz Majewski184a4bf2014-12-08 18:04:21 +0100179/**
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 */
190static int of_thermal_set_emul_temp(struct thermal_zone_device *tz,
Sascha Hauer17e83512015-07-24 08:12:54 +0200191 int temp)
Lukasz Majewski184a4bf2014-12-08 18:04:21 +0100192{
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
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400201static 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
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400208 if (!data->ops->get_trend)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400209 return -EINVAL;
210
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400211 r = data->ops->get_trend(data->sensor_data, &dev_trend);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400212 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
226static 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,
Punit Agrawaldd354b82014-06-03 10:59:58 +0100244 tbp->max,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000245 tbp->min,
246 tbp->usage);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400247 if (ret)
248 return ret;
249 }
250 }
251
252 return 0;
253}
254
255static 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
281static 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
291static 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
311static 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
324static int of_thermal_get_trip_temp(struct thermal_zone_device *tz, int trip,
Sascha Hauer17e83512015-07-24 08:12:54 +0200325 int *temp)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400326{
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
337static int of_thermal_set_trip_temp(struct thermal_zone_device *tz, int trip,
Sascha Hauer17e83512015-07-24 08:12:54 +0200338 int temp)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400339{
340 struct __thermal_zone *data = tz->devdata;
341
342 if (trip >= data->ntrips || trip < 0)
343 return -EDOM;
344
Wei Nic3509522016-03-29 18:29:17 +0800345 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
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400353 /* thermal framework should take care of data->mask & (1 << trip) */
354 data->trips[trip].temperature = temp;
355
356 return 0;
357}
358
359static int of_thermal_get_trip_hyst(struct thermal_zone_device *tz, int trip,
Sascha Hauer17e83512015-07-24 08:12:54 +0200360 int *hyst)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400361{
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
372static int of_thermal_set_trip_hyst(struct thermal_zone_device *tz, int trip,
Sascha Hauer17e83512015-07-24 08:12:54 +0200373 int hyst)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400374{
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
386static int of_thermal_get_crit_temp(struct thermal_zone_device *tz,
Sascha Hauer17e83512015-07-24 08:12:54 +0200387 int *temp)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400388{
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
401static 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
418static struct thermal_zone_device *
419thermal_zone_of_add_sensor(struct device_node *zone,
420 struct device_node *sensor, void *data,
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400421 const struct thermal_zone_of_device_ops *ops)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400422{
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
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400432 if (!ops)
433 return ERR_PTR(-EINVAL);
434
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400435 mutex_lock(&tzd->lock);
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400436 tz->ops = ops;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400437 tz->sensor_data = data;
438
439 tzd->ops->get_temp = of_thermal_get_temp;
440 tzd->ops->get_trend = of_thermal_get_trend;
Sascha Hauer826386e2016-06-22 16:42:02 +0800441
442 /*
443 * The thermal zone core will calculate the window if they have set the
444 * optional set_trips pointer.
445 */
446 if (ops->set_trips)
447 tzd->ops->set_trips = of_thermal_set_trips;
448
Lukasz Majewski184a4bf2014-12-08 18:04:21 +0100449 tzd->ops->set_emul_temp = of_thermal_set_emul_temp;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400450 mutex_unlock(&tzd->lock);
451
452 return tzd;
453}
454
455/**
456 * thermal_zone_of_sensor_register - registers a sensor to a DT thermal zone
457 * @dev: a valid struct device pointer of a sensor device. Must contain
458 * a valid .of_node, for the sensor node.
459 * @sensor_id: a sensor identifier, in case the sensor IP has more
460 * than one sensors
461 * @data: a private pointer (owned by the caller) that will be passed
462 * back, when a temperature reading is needed.
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400463 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400464 *
465 * This function will search the list of thermal zones described in device
466 * tree and look for the zone that refer to the sensor device pointed by
467 * @dev->of_node as temperature providers. For the zone pointing to the
468 * sensor node, the sensor will be added to the DT thermal zone device.
469 *
470 * The thermal zone temperature is provided by the @get_temp function
471 * pointer. When called, it will have the private pointer @data back.
472 *
473 * The thermal zone temperature trend is provided by the @get_trend function
474 * pointer. When called, it will have the private pointer @data back.
475 *
476 * TODO:
477 * 01 - This function must enqueue the new sensor instead of using
478 * it as the only source of temperature values.
479 *
480 * 02 - There must be a way to match the sensor with all thermal zones
481 * that refer to it.
482 *
483 * Return: On success returns a valid struct thermal_zone_device,
484 * otherwise, it returns a corresponding ERR_PTR(). Caller must
485 * check the return value with help of IS_ERR() helper.
486 */
487struct thermal_zone_device *
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400488thermal_zone_of_sensor_register(struct device *dev, int sensor_id, void *data,
489 const struct thermal_zone_of_device_ops *ops)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400490{
491 struct device_node *np, *child, *sensor_np;
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300492 struct thermal_zone_device *tzd = ERR_PTR(-ENODEV);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400493
494 np = of_find_node_by_name(NULL, "thermal-zones");
495 if (!np)
496 return ERR_PTR(-ENODEV);
497
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300498 if (!dev || !dev->of_node) {
499 of_node_put(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400500 return ERR_PTR(-EINVAL);
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300501 }
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400502
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300503 sensor_np = of_node_get(dev->of_node);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400504
Laxman Dewangan42bbe402016-02-08 18:58:34 +0530505 for_each_available_child_of_node(np, child) {
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400506 struct of_phandle_args sensor_specs;
507 int ret, id;
508
509 /* For now, thermal framework supports only 1 sensor per zone */
510 ret = of_parse_phandle_with_args(child, "thermal-sensors",
511 "#thermal-sensor-cells",
512 0, &sensor_specs);
513 if (ret)
514 continue;
515
516 if (sensor_specs.args_count >= 1) {
517 id = sensor_specs.args[0];
518 WARN(sensor_specs.args_count > 1,
519 "%s: too many cells in sensor specifier %d\n",
520 sensor_specs.np->name, sensor_specs.args_count);
521 } else {
522 id = 0;
523 }
524
525 if (sensor_specs.np == sensor_np && id == sensor_id) {
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300526 tzd = thermal_zone_of_add_sensor(child, sensor_np,
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400527 data, ops);
Lukasz Majewski528012c12015-01-19 12:44:04 +0100528 if (!IS_ERR(tzd))
529 tzd->ops->set_mode(tzd, THERMAL_DEVICE_ENABLED);
530
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300531 of_node_put(sensor_specs.np);
532 of_node_put(child);
533 goto exit;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400534 }
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300535 of_node_put(sensor_specs.np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400536 }
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300537exit:
538 of_node_put(sensor_np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400539 of_node_put(np);
540
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300541 return tzd;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400542}
543EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_register);
544
545/**
546 * thermal_zone_of_sensor_unregister - unregisters a sensor from a DT thermal zone
547 * @dev: a valid struct device pointer of a sensor device. Must contain
548 * a valid .of_node, for the sensor node.
549 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
550 *
551 * This function removes the sensor callbacks and private data from the
552 * thermal zone device registered with thermal_zone_of_sensor_register()
553 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
554 * thermal zone device callbacks.
555 *
556 * TODO: When the support to several sensors per zone is added, this
557 * function must search the sensor list based on @dev parameter.
558 *
559 */
560void thermal_zone_of_sensor_unregister(struct device *dev,
561 struct thermal_zone_device *tzd)
562{
563 struct __thermal_zone *tz;
564
565 if (!dev || !tzd || !tzd->devdata)
566 return;
567
568 tz = tzd->devdata;
569
570 /* no __thermal_zone, nothing to be done */
571 if (!tz)
572 return;
573
574 mutex_lock(&tzd->lock);
575 tzd->ops->get_temp = NULL;
576 tzd->ops->get_trend = NULL;
Lukasz Majewski184a4bf2014-12-08 18:04:21 +0100577 tzd->ops->set_emul_temp = NULL;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400578
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400579 tz->ops = NULL;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400580 tz->sensor_data = NULL;
581 mutex_unlock(&tzd->lock);
582}
583EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister);
584
Laxman Dewangane498b492016-03-09 18:40:06 +0530585static void devm_thermal_zone_of_sensor_release(struct device *dev, void *res)
586{
587 thermal_zone_of_sensor_unregister(dev,
588 *(struct thermal_zone_device **)res);
589}
590
591static int devm_thermal_zone_of_sensor_match(struct device *dev, void *res,
592 void *data)
593{
594 struct thermal_zone_device **r = res;
595
596 if (WARN_ON(!r || !*r))
597 return 0;
598
599 return *r == data;
600}
601
602/**
603 * devm_thermal_zone_of_sensor_register - Resource managed version of
604 * thermal_zone_of_sensor_register()
605 * @dev: a valid struct device pointer of a sensor device. Must contain
606 * a valid .of_node, for the sensor node.
607 * @sensor_id: a sensor identifier, in case the sensor IP has more
608 * than one sensors
609 * @data: a private pointer (owned by the caller) that will be passed
610 * back, when a temperature reading is needed.
611 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
612 *
613 * Refer thermal_zone_of_sensor_register() for more details.
614 *
615 * Return: On success returns a valid struct thermal_zone_device,
616 * otherwise, it returns a corresponding ERR_PTR(). Caller must
617 * check the return value with help of IS_ERR() helper.
618 * Registered hermal_zone_device device will automatically be
619 * released when device is unbounded.
620 */
621struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
622 struct device *dev, int sensor_id,
623 void *data, const struct thermal_zone_of_device_ops *ops)
624{
625 struct thermal_zone_device **ptr, *tzd;
626
627 ptr = devres_alloc(devm_thermal_zone_of_sensor_release, sizeof(*ptr),
628 GFP_KERNEL);
629 if (!ptr)
630 return ERR_PTR(-ENOMEM);
631
632 tzd = thermal_zone_of_sensor_register(dev, sensor_id, data, ops);
633 if (IS_ERR(tzd)) {
634 devres_free(ptr);
635 return tzd;
636 }
637
638 *ptr = tzd;
639 devres_add(dev, ptr);
640
641 return tzd;
642}
643EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_register);
644
645/**
646 * devm_thermal_zone_of_sensor_unregister - Resource managed version of
647 * thermal_zone_of_sensor_unregister().
648 * @dev: Device for which which resource was allocated.
649 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
650 *
651 * This function removes the sensor callbacks and private data from the
652 * thermal zone device registered with devm_thermal_zone_of_sensor_register()
653 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
654 * thermal zone device callbacks.
655 * Normally this function will not need to be called and the resource
656 * management code will ensure that the resource is freed.
657 */
658void devm_thermal_zone_of_sensor_unregister(struct device *dev,
659 struct thermal_zone_device *tzd)
660{
661 WARN_ON(devres_release(dev, devm_thermal_zone_of_sensor_release,
662 devm_thermal_zone_of_sensor_match, tzd));
663}
664EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_unregister);
665
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400666/*** functions parsing device tree nodes ***/
667
668/**
669 * thermal_of_populate_bind_params - parse and fill cooling map data
670 * @np: DT node containing a cooling-map node
671 * @__tbp: data structure to be filled with cooling map info
672 * @trips: array of thermal zone trip points
673 * @ntrips: number of trip points inside trips.
674 *
675 * This function parses a cooling-map type of node represented by
676 * @np parameter and fills the read data into @__tbp data structure.
677 * It needs the already parsed array of trip points of the thermal zone
678 * in consideration.
679 *
680 * Return: 0 on success, proper error code otherwise
681 */
682static int thermal_of_populate_bind_params(struct device_node *np,
683 struct __thermal_bind_params *__tbp,
Lukasz Majewskiad9914a2014-12-08 18:04:19 +0100684 struct thermal_trip *trips,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400685 int ntrips)
686{
687 struct of_phandle_args cooling_spec;
688 struct device_node *trip;
689 int ret, i;
690 u32 prop;
691
692 /* Default weight. Usage is optional */
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000693 __tbp->usage = THERMAL_WEIGHT_DEFAULT;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400694 ret = of_property_read_u32(np, "contribution", &prop);
695 if (ret == 0)
696 __tbp->usage = prop;
697
698 trip = of_parse_phandle(np, "trip", 0);
699 if (!trip) {
700 pr_err("missing trip property\n");
701 return -ENODEV;
702 }
703
704 /* match using device_node */
705 for (i = 0; i < ntrips; i++)
706 if (trip == trips[i].np) {
707 __tbp->trip_id = i;
708 break;
709 }
710
711 if (i == ntrips) {
712 ret = -ENODEV;
713 goto end;
714 }
715
716 ret = of_parse_phandle_with_args(np, "cooling-device", "#cooling-cells",
717 0, &cooling_spec);
718 if (ret < 0) {
719 pr_err("missing cooling_device property\n");
720 goto end;
721 }
722 __tbp->cooling_device = cooling_spec.np;
723 if (cooling_spec.args_count >= 2) { /* at least min and max */
724 __tbp->min = cooling_spec.args[0];
725 __tbp->max = cooling_spec.args[1];
726 } else {
727 pr_err("wrong reference to cooling device, missing limits\n");
728 }
729
730end:
731 of_node_put(trip);
732
733 return ret;
734}
735
736/**
737 * It maps 'enum thermal_trip_type' found in include/linux/thermal.h
738 * into the device tree binding of 'trip', property type.
739 */
740static const char * const trip_types[] = {
741 [THERMAL_TRIP_ACTIVE] = "active",
742 [THERMAL_TRIP_PASSIVE] = "passive",
743 [THERMAL_TRIP_HOT] = "hot",
744 [THERMAL_TRIP_CRITICAL] = "critical",
745};
746
747/**
748 * thermal_of_get_trip_type - Get phy mode for given device_node
749 * @np: Pointer to the given device_node
750 * @type: Pointer to resulting trip type
751 *
752 * The function gets trip type string from property 'type',
753 * and store its index in trip_types table in @type,
754 *
755 * Return: 0 on success, or errno in error case.
756 */
757static int thermal_of_get_trip_type(struct device_node *np,
758 enum thermal_trip_type *type)
759{
760 const char *t;
761 int err, i;
762
763 err = of_property_read_string(np, "type", &t);
764 if (err < 0)
765 return err;
766
767 for (i = 0; i < ARRAY_SIZE(trip_types); i++)
768 if (!strcasecmp(t, trip_types[i])) {
769 *type = i;
770 return 0;
771 }
772
773 return -ENODEV;
774}
775
776/**
777 * thermal_of_populate_trip - parse and fill one trip point data
778 * @np: DT node containing a trip point node
779 * @trip: trip point data structure to be filled up
780 *
781 * This function parses a trip point type of node represented by
782 * @np parameter and fills the read data into @trip data structure.
783 *
784 * Return: 0 on success, proper error code otherwise
785 */
786static int thermal_of_populate_trip(struct device_node *np,
Lukasz Majewskiad9914a2014-12-08 18:04:19 +0100787 struct thermal_trip *trip)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400788{
789 int prop;
790 int ret;
791
792 ret = of_property_read_u32(np, "temperature", &prop);
793 if (ret < 0) {
794 pr_err("missing temperature property\n");
795 return ret;
796 }
797 trip->temperature = prop;
798
799 ret = of_property_read_u32(np, "hysteresis", &prop);
800 if (ret < 0) {
801 pr_err("missing hysteresis property\n");
802 return ret;
803 }
804 trip->hysteresis = prop;
805
806 ret = thermal_of_get_trip_type(np, &trip->type);
807 if (ret < 0) {
808 pr_err("wrong trip type property\n");
809 return ret;
810 }
811
812 /* Required for cooling map matching */
813 trip->np = np;
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300814 of_node_get(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400815
816 return 0;
817}
818
819/**
820 * thermal_of_build_thermal_zone - parse and fill one thermal zone data
821 * @np: DT node containing a thermal zone node
822 *
823 * This function parses a thermal zone type of node represented by
824 * @np parameter and fills the read data into a __thermal_zone data structure
825 * and return this pointer.
826 *
Eduardo Valentina46dbae2015-05-11 19:48:09 -0700827 * TODO: Missing properties to parse: thermal-sensor-names
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400828 *
829 * Return: On success returns a valid struct __thermal_zone,
830 * otherwise, it returns a corresponding ERR_PTR(). Caller must
831 * check the return value with help of IS_ERR() helper.
832 */
Julia Lawallc0ff8aa2016-04-19 14:33:32 +0200833static struct __thermal_zone
834__init *thermal_of_build_thermal_zone(struct device_node *np)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400835{
836 struct device_node *child = NULL, *gchild;
837 struct __thermal_zone *tz;
838 int ret, i;
Eduardo Valentina46dbae2015-05-11 19:48:09 -0700839 u32 prop, coef[2];
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400840
841 if (!np) {
842 pr_err("no thermal zone np\n");
843 return ERR_PTR(-EINVAL);
844 }
845
846 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
847 if (!tz)
848 return ERR_PTR(-ENOMEM);
849
850 ret = of_property_read_u32(np, "polling-delay-passive", &prop);
851 if (ret < 0) {
852 pr_err("missing polling-delay-passive property\n");
853 goto free_tz;
854 }
855 tz->passive_delay = prop;
856
857 ret = of_property_read_u32(np, "polling-delay", &prop);
858 if (ret < 0) {
859 pr_err("missing polling-delay property\n");
860 goto free_tz;
861 }
862 tz->polling_delay = prop;
863
Eduardo Valentina46dbae2015-05-11 19:48:09 -0700864 /*
865 * REVIST: for now, the thermal framework supports only
866 * one sensor per thermal zone. Thus, we are considering
867 * only the first two values as slope and offset.
868 */
869 ret = of_property_read_u32_array(np, "coefficients", coef, 2);
870 if (ret == 0) {
871 tz->slope = coef[0];
872 tz->offset = coef[1];
873 } else {
874 tz->slope = 1;
875 tz->offset = 0;
876 }
877
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400878 /* trips */
879 child = of_get_child_by_name(np, "trips");
880
881 /* No trips provided */
882 if (!child)
883 goto finish;
884
885 tz->ntrips = of_get_child_count(child);
886 if (tz->ntrips == 0) /* must have at least one child */
887 goto finish;
888
889 tz->trips = kzalloc(tz->ntrips * sizeof(*tz->trips), GFP_KERNEL);
890 if (!tz->trips) {
891 ret = -ENOMEM;
892 goto free_tz;
893 }
894
895 i = 0;
896 for_each_child_of_node(child, gchild) {
897 ret = thermal_of_populate_trip(gchild, &tz->trips[i++]);
898 if (ret)
899 goto free_trips;
900 }
901
902 of_node_put(child);
903
904 /* cooling-maps */
905 child = of_get_child_by_name(np, "cooling-maps");
906
907 /* cooling-maps not provided */
908 if (!child)
909 goto finish;
910
911 tz->num_tbps = of_get_child_count(child);
912 if (tz->num_tbps == 0)
913 goto finish;
914
915 tz->tbps = kzalloc(tz->num_tbps * sizeof(*tz->tbps), GFP_KERNEL);
916 if (!tz->tbps) {
917 ret = -ENOMEM;
918 goto free_trips;
919 }
920
921 i = 0;
Stephen Boydca9521b2014-06-18 16:32:08 -0700922 for_each_child_of_node(child, gchild) {
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400923 ret = thermal_of_populate_bind_params(gchild, &tz->tbps[i++],
924 tz->trips, tz->ntrips);
925 if (ret)
926 goto free_tbps;
Stephen Boydca9521b2014-06-18 16:32:08 -0700927 }
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400928
929finish:
930 of_node_put(child);
931 tz->mode = THERMAL_DEVICE_DISABLED;
932
933 return tz;
934
935free_tbps:
Ulises Brindis1cd91c12016-03-25 12:55:41 -0700936 for (i = i - 1; i >= 0; i--)
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300937 of_node_put(tz->tbps[i].cooling_device);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400938 kfree(tz->tbps);
939free_trips:
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300940 for (i = 0; i < tz->ntrips; i++)
941 of_node_put(tz->trips[i].np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400942 kfree(tz->trips);
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300943 of_node_put(gchild);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400944free_tz:
945 kfree(tz);
946 of_node_put(child);
947
948 return ERR_PTR(ret);
949}
950
951static inline void of_thermal_free_zone(struct __thermal_zone *tz)
952{
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300953 int i;
954
955 for (i = 0; i < tz->num_tbps; i++)
956 of_node_put(tz->tbps[i].cooling_device);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400957 kfree(tz->tbps);
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300958 for (i = 0; i < tz->ntrips; i++)
959 of_node_put(tz->trips[i].np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400960 kfree(tz->trips);
961 kfree(tz);
962}
963
964/**
965 * of_parse_thermal_zones - parse device tree thermal data
966 *
967 * Initialization function that can be called by machine initialization
968 * code to parse thermal data and populate the thermal framework
969 * with hardware thermal zones info. This function only parses thermal zones.
970 * Cooling devices and sensor devices nodes are supposed to be parsed
971 * by their respective drivers.
972 *
973 * Return: 0 on success, proper error code otherwise
974 *
975 */
976int __init of_parse_thermal_zones(void)
977{
978 struct device_node *np, *child;
979 struct __thermal_zone *tz;
980 struct thermal_zone_device_ops *ops;
981
982 np = of_find_node_by_name(NULL, "thermal-zones");
983 if (!np) {
984 pr_debug("unable to find thermal zones\n");
985 return 0; /* Run successfully on systems without thermal DT */
986 }
987
Laxman Dewangan42bbe402016-02-08 18:58:34 +0530988 for_each_available_child_of_node(np, child) {
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400989 struct thermal_zone_device *zone;
990 struct thermal_zone_params *tzp;
Punit Agrawal76af5492015-03-03 10:43:04 +0000991 int i, mask = 0;
Punit Agrawal647f9922015-02-26 19:00:32 +0000992 u32 prop;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400993
994 tz = thermal_of_build_thermal_zone(child);
995 if (IS_ERR(tz)) {
996 pr_err("failed to build thermal zone %s: %ld\n",
997 child->name,
998 PTR_ERR(tz));
999 continue;
1000 }
1001
1002 ops = kmemdup(&of_thermal_ops, sizeof(*ops), GFP_KERNEL);
1003 if (!ops)
1004 goto exit_free;
1005
1006 tzp = kzalloc(sizeof(*tzp), GFP_KERNEL);
1007 if (!tzp) {
1008 kfree(ops);
1009 goto exit_free;
1010 }
1011
1012 /* No hwmon because there might be hwmon drivers registering */
1013 tzp->no_hwmon = true;
1014
Punit Agrawal647f9922015-02-26 19:00:32 +00001015 if (!of_property_read_u32(child, "sustainable-power", &prop))
1016 tzp->sustainable_power = prop;
1017
Punit Agrawal76af5492015-03-03 10:43:04 +00001018 for (i = 0; i < tz->ntrips; i++)
1019 mask |= 1 << i;
1020
Eduardo Valentina46dbae2015-05-11 19:48:09 -07001021 /* these two are left for temperature drivers to use */
1022 tzp->slope = tz->slope;
1023 tzp->offset = tz->offset;
1024
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001025 zone = thermal_zone_device_register(child->name, tz->ntrips,
Punit Agrawal76af5492015-03-03 10:43:04 +00001026 mask, tz,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001027 ops, tzp,
1028 tz->passive_delay,
1029 tz->polling_delay);
1030 if (IS_ERR(zone)) {
1031 pr_err("Failed to build %s zone %ld\n", child->name,
1032 PTR_ERR(zone));
1033 kfree(tzp);
1034 kfree(ops);
1035 of_thermal_free_zone(tz);
1036 /* attempting to build remaining zones still */
1037 }
1038 }
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001039 of_node_put(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001040
1041 return 0;
1042
1043exit_free:
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001044 of_node_put(child);
1045 of_node_put(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001046 of_thermal_free_zone(tz);
1047
1048 /* no memory available, so free what we have built */
1049 of_thermal_destroy_zones();
1050
1051 return -ENOMEM;
1052}
1053
1054/**
1055 * of_thermal_destroy_zones - remove all zones parsed and allocated resources
1056 *
1057 * Finds all zones parsed and added to the thermal framework and remove them
1058 * from the system, together with their resources.
1059 *
1060 */
1061void of_thermal_destroy_zones(void)
1062{
1063 struct device_node *np, *child;
1064
1065 np = of_find_node_by_name(NULL, "thermal-zones");
1066 if (!np) {
Jiada Wang28524982015-11-16 17:10:05 +09001067 pr_debug("unable to find thermal zones\n");
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001068 return;
1069 }
1070
Laxman Dewangan42bbe402016-02-08 18:58:34 +05301071 for_each_available_child_of_node(np, child) {
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001072 struct thermal_zone_device *zone;
1073
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001074 zone = thermal_zone_get_zone_by_name(child->name);
1075 if (IS_ERR(zone))
1076 continue;
1077
1078 thermal_zone_device_unregister(zone);
1079 kfree(zone->tzp);
1080 kfree(zone->ops);
1081 of_thermal_free_zone(zone->devdata);
1082 }
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001083 of_node_put(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001084}