blob: c5abfa45318347d8714cff03c3c6f50987b7e98a [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/**
Ram Chandrasekare34ced62017-03-03 11:22:50 -070057 * struct __sensor_param - Holds individual sensor data
58 * @sensor_data: sensor driver private data passed as input argument
59 * @ops: sensor driver ops
60 */
61struct __sensor_param {
62 void *sensor_data;
63 const struct thermal_zone_of_device_ops *ops;
64};
65
66/**
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040067 * struct __thermal_zone - internal representation of a thermal zone
68 * @mode: current thermal zone device mode (enabled/disabled)
69 * @passive_delay: polling interval while passive cooling is activated
70 * @polling_delay: zone polling interval
Eduardo Valentina46dbae2015-05-11 19:48:09 -070071 * @slope: slope of the temperature adjustment curve
72 * @offset: offset of the temperature adjustment curve
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040073 * @ntrips: number of trip points
74 * @trips: an array of trip points (0..ntrips - 1)
75 * @num_tbps: number of thermal bind params
76 * @tbps: an array of thermal bind params (0..num_tbps - 1)
Ram Chandrasekare34ced62017-03-03 11:22:50 -070077 * @senps: sensor related parameters
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040078 */
79
80struct __thermal_zone {
81 enum thermal_device_mode mode;
82 int passive_delay;
83 int polling_delay;
Eduardo Valentina46dbae2015-05-11 19:48:09 -070084 int slope;
85 int offset;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040086
87 /* trip data */
88 int ntrips;
Lukasz Majewskiad9914a2014-12-08 18:04:19 +010089 struct thermal_trip *trips;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040090
91 /* cooling binding data */
92 int num_tbps;
93 struct __thermal_bind_params *tbps;
94
95 /* sensor interface */
Ram Chandrasekare34ced62017-03-03 11:22:50 -070096 struct __sensor_param *senps;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040097};
98
99/*** DT thermal zone device callbacks ***/
100
101static int of_thermal_get_temp(struct thermal_zone_device *tz,
Sascha Hauer17e83512015-07-24 08:12:54 +0200102 int *temp)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400103{
104 struct __thermal_zone *data = tz->devdata;
105
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700106 if (!data->senps || !data->senps->ops->get_temp)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400107 return -EINVAL;
108
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700109 return data->senps->ops->get_temp(data->senps->sensor_data, temp);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400110}
111
Sascha Hauer826386e2016-06-22 16:42:02 +0800112static int of_thermal_set_trips(struct thermal_zone_device *tz,
113 int low, int high)
114{
115 struct __thermal_zone *data = tz->devdata;
116
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700117 if (!data->senps || !data->senps->ops->set_trips)
Sascha Hauer826386e2016-06-22 16:42:02 +0800118 return -EINVAL;
119
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700120 return data->senps->ops->set_trips(data->senps->sensor_data, low, high);
Sascha Hauer826386e2016-06-22 16:42:02 +0800121}
122
Lukasz Majewski08dab662014-12-08 18:04:17 +0100123/**
124 * of_thermal_get_ntrips - function to export number of available trip
125 * points.
126 * @tz: pointer to a thermal zone
127 *
128 * This function is a globally visible wrapper to get number of trip points
129 * stored in the local struct __thermal_zone
130 *
131 * Return: number of available trip points, -ENODEV when data not available
132 */
133int of_thermal_get_ntrips(struct thermal_zone_device *tz)
134{
135 struct __thermal_zone *data = tz->devdata;
136
137 if (!data || IS_ERR(data))
138 return -ENODEV;
139
140 return data->ntrips;
141}
142EXPORT_SYMBOL_GPL(of_thermal_get_ntrips);
143
Lukasz Majewskia9bf2cc2014-12-08 18:04:18 +0100144/**
145 * of_thermal_is_trip_valid - function to check if trip point is valid
146 *
147 * @tz: pointer to a thermal zone
148 * @trip: trip point to evaluate
149 *
150 * This function is responsible for checking if passed trip point is valid
151 *
152 * Return: true if trip point is valid, false otherwise
153 */
154bool of_thermal_is_trip_valid(struct thermal_zone_device *tz, int trip)
155{
156 struct __thermal_zone *data = tz->devdata;
157
158 if (!data || trip >= data->ntrips || trip < 0)
159 return false;
160
161 return true;
162}
163EXPORT_SYMBOL_GPL(of_thermal_is_trip_valid);
164
Lukasz Majewskice8be772014-12-08 18:04:20 +0100165/**
166 * of_thermal_get_trip_points - function to get access to a globally exported
167 * trip points
168 *
169 * @tz: pointer to a thermal zone
170 *
171 * This function provides a pointer to trip points table
172 *
173 * Return: pointer to trip points table, NULL otherwise
174 */
Geert Uytterhoevenebc31932015-01-03 22:56:56 +0100175const struct thermal_trip *
Lukasz Majewskice8be772014-12-08 18:04:20 +0100176of_thermal_get_trip_points(struct thermal_zone_device *tz)
177{
178 struct __thermal_zone *data = tz->devdata;
179
180 if (!data)
181 return NULL;
182
183 return data->trips;
184}
185EXPORT_SYMBOL_GPL(of_thermal_get_trip_points);
186
Lukasz Majewski184a4bf2014-12-08 18:04:21 +0100187/**
188 * of_thermal_set_emul_temp - function to set emulated temperature
189 *
190 * @tz: pointer to a thermal zone
191 * @temp: temperature to set
192 *
193 * This function gives the ability to set emulated value of temperature,
194 * which is handy for debugging
195 *
196 * Return: zero on success, error code otherwise
197 */
198static int of_thermal_set_emul_temp(struct thermal_zone_device *tz,
Sascha Hauer17e83512015-07-24 08:12:54 +0200199 int temp)
Lukasz Majewski184a4bf2014-12-08 18:04:21 +0100200{
201 struct __thermal_zone *data = tz->devdata;
202
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700203 if (!data->senps || !data->senps->ops->set_emul_temp)
204 return -EINVAL;
205
206 return data->senps->ops->set_emul_temp(data->senps->sensor_data, temp);
Lukasz Majewski184a4bf2014-12-08 18:04:21 +0100207}
208
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400209static int of_thermal_get_trend(struct thermal_zone_device *tz, int trip,
210 enum thermal_trend *trend)
211{
212 struct __thermal_zone *data = tz->devdata;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400213
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700214 if (!data->senps || !data->senps->ops->get_trend)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400215 return -EINVAL;
216
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700217 return data->senps->ops->get_trend(data->senps->sensor_data,
218 trip, trend);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400219}
220
221static int of_thermal_bind(struct thermal_zone_device *thermal,
222 struct thermal_cooling_device *cdev)
223{
224 struct __thermal_zone *data = thermal->devdata;
225 int i;
226
227 if (!data || IS_ERR(data))
228 return -ENODEV;
229
230 /* find where to bind */
231 for (i = 0; i < data->num_tbps; i++) {
232 struct __thermal_bind_params *tbp = data->tbps + i;
233
234 if (tbp->cooling_device == cdev->np) {
235 int ret;
236
237 ret = thermal_zone_bind_cooling_device(thermal,
238 tbp->trip_id, cdev,
Punit Agrawaldd354b82014-06-03 10:59:58 +0100239 tbp->max,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000240 tbp->min,
241 tbp->usage);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400242 if (ret)
243 return ret;
244 }
245 }
246
247 return 0;
248}
249
250static int of_thermal_unbind(struct thermal_zone_device *thermal,
251 struct thermal_cooling_device *cdev)
252{
253 struct __thermal_zone *data = thermal->devdata;
254 int i;
255
256 if (!data || IS_ERR(data))
257 return -ENODEV;
258
259 /* find where to unbind */
260 for (i = 0; i < data->num_tbps; i++) {
261 struct __thermal_bind_params *tbp = data->tbps + i;
262
263 if (tbp->cooling_device == cdev->np) {
264 int ret;
265
266 ret = thermal_zone_unbind_cooling_device(thermal,
267 tbp->trip_id, cdev);
268 if (ret)
269 return ret;
270 }
271 }
272
273 return 0;
274}
275
276static int of_thermal_get_mode(struct thermal_zone_device *tz,
277 enum thermal_device_mode *mode)
278{
279 struct __thermal_zone *data = tz->devdata;
280
281 *mode = data->mode;
282
283 return 0;
284}
285
286static int of_thermal_set_mode(struct thermal_zone_device *tz,
287 enum thermal_device_mode mode)
288{
289 struct __thermal_zone *data = tz->devdata;
290
291 mutex_lock(&tz->lock);
292
293 if (mode == THERMAL_DEVICE_ENABLED)
294 tz->polling_delay = data->polling_delay;
295 else
296 tz->polling_delay = 0;
297
298 mutex_unlock(&tz->lock);
299
300 data->mode = mode;
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700301 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400302
303 return 0;
304}
305
306static int of_thermal_get_trip_type(struct thermal_zone_device *tz, int trip,
307 enum thermal_trip_type *type)
308{
309 struct __thermal_zone *data = tz->devdata;
310
311 if (trip >= data->ntrips || trip < 0)
312 return -EDOM;
313
314 *type = data->trips[trip].type;
315
316 return 0;
317}
318
319static int of_thermal_get_trip_temp(struct thermal_zone_device *tz, int trip,
Sascha Hauer17e83512015-07-24 08:12:54 +0200320 int *temp)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400321{
322 struct __thermal_zone *data = tz->devdata;
323
324 if (trip >= data->ntrips || trip < 0)
325 return -EDOM;
326
327 *temp = data->trips[trip].temperature;
328
329 return 0;
330}
331
332static int of_thermal_set_trip_temp(struct thermal_zone_device *tz, int trip,
Sascha Hauer17e83512015-07-24 08:12:54 +0200333 int temp)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400334{
335 struct __thermal_zone *data = tz->devdata;
336
337 if (trip >= data->ntrips || trip < 0)
338 return -EDOM;
339
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700340 if (data->senps && data->senps->ops->set_trip_temp) {
Wei Nic3509522016-03-29 18:29:17 +0800341 int ret;
342
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700343 ret = data->senps->ops->set_trip_temp(data->senps->sensor_data,
344 trip, temp);
Wei Nic3509522016-03-29 18:29:17 +0800345 if (ret)
346 return ret;
347 }
348
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400349 /* thermal framework should take care of data->mask & (1 << trip) */
350 data->trips[trip].temperature = temp;
351
352 return 0;
353}
354
355static int of_thermal_get_trip_hyst(struct thermal_zone_device *tz, int trip,
Sascha Hauer17e83512015-07-24 08:12:54 +0200356 int *hyst)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400357{
358 struct __thermal_zone *data = tz->devdata;
359
360 if (trip >= data->ntrips || trip < 0)
361 return -EDOM;
362
363 *hyst = data->trips[trip].hysteresis;
364
365 return 0;
366}
367
368static int of_thermal_set_trip_hyst(struct thermal_zone_device *tz, int trip,
Sascha Hauer17e83512015-07-24 08:12:54 +0200369 int hyst)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400370{
371 struct __thermal_zone *data = tz->devdata;
372
373 if (trip >= data->ntrips || trip < 0)
374 return -EDOM;
375
376 /* thermal framework should take care of data->mask & (1 << trip) */
377 data->trips[trip].hysteresis = hyst;
378
379 return 0;
380}
381
382static int of_thermal_get_crit_temp(struct thermal_zone_device *tz,
Sascha Hauer17e83512015-07-24 08:12:54 +0200383 int *temp)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400384{
385 struct __thermal_zone *data = tz->devdata;
386 int i;
387
388 for (i = 0; i < data->ntrips; i++)
389 if (data->trips[i].type == THERMAL_TRIP_CRITICAL) {
390 *temp = data->trips[i].temperature;
391 return 0;
392 }
393
394 return -EINVAL;
395}
396
397static struct thermal_zone_device_ops of_thermal_ops = {
398 .get_mode = of_thermal_get_mode,
399 .set_mode = of_thermal_set_mode,
400
401 .get_trip_type = of_thermal_get_trip_type,
402 .get_trip_temp = of_thermal_get_trip_temp,
403 .set_trip_temp = of_thermal_set_trip_temp,
404 .get_trip_hyst = of_thermal_get_trip_hyst,
405 .set_trip_hyst = of_thermal_set_trip_hyst,
406 .get_crit_temp = of_thermal_get_crit_temp,
407
408 .bind = of_thermal_bind,
409 .unbind = of_thermal_unbind,
410};
411
412/*** sensor API ***/
413
414static struct thermal_zone_device *
415thermal_zone_of_add_sensor(struct device_node *zone,
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700416 struct device_node *sensor,
417 struct __sensor_param *sens_param)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400418{
419 struct thermal_zone_device *tzd;
420 struct __thermal_zone *tz;
421
422 tzd = thermal_zone_get_zone_by_name(zone->name);
423 if (IS_ERR(tzd))
424 return ERR_PTR(-EPROBE_DEFER);
425
426 tz = tzd->devdata;
427
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700428 if (!sens_param->ops)
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400429 return ERR_PTR(-EINVAL);
430
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400431 mutex_lock(&tzd->lock);
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700432 tz->senps = sens_param;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400433
434 tzd->ops->get_temp = of_thermal_get_temp;
435 tzd->ops->get_trend = of_thermal_get_trend;
Sascha Hauer826386e2016-06-22 16:42:02 +0800436
437 /*
438 * The thermal zone core will calculate the window if they have set the
439 * optional set_trips pointer.
440 */
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700441 if (sens_param->ops->set_trips)
Sascha Hauer826386e2016-06-22 16:42:02 +0800442 tzd->ops->set_trips = of_thermal_set_trips;
443
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700444 if (sens_param->ops->set_emul_temp)
Keerthye2fa7482016-06-02 14:24:50 +0530445 tzd->ops->set_emul_temp = of_thermal_set_emul_temp;
446
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400447 mutex_unlock(&tzd->lock);
448
449 return tzd;
450}
451
452/**
453 * thermal_zone_of_sensor_register - registers a sensor to a DT thermal zone
454 * @dev: a valid struct device pointer of a sensor device. Must contain
455 * a valid .of_node, for the sensor node.
456 * @sensor_id: a sensor identifier, in case the sensor IP has more
457 * than one sensors
458 * @data: a private pointer (owned by the caller) that will be passed
459 * back, when a temperature reading is needed.
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400460 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400461 *
462 * This function will search the list of thermal zones described in device
463 * tree and look for the zone that refer to the sensor device pointed by
464 * @dev->of_node as temperature providers. For the zone pointing to the
465 * sensor node, the sensor will be added to the DT thermal zone device.
466 *
467 * The thermal zone temperature is provided by the @get_temp function
468 * pointer. When called, it will have the private pointer @data back.
469 *
470 * The thermal zone temperature trend is provided by the @get_trend function
471 * pointer. When called, it will have the private pointer @data back.
472 *
473 * TODO:
474 * 01 - This function must enqueue the new sensor instead of using
475 * it as the only source of temperature values.
476 *
477 * 02 - There must be a way to match the sensor with all thermal zones
478 * that refer to it.
479 *
480 * Return: On success returns a valid struct thermal_zone_device,
481 * otherwise, it returns a corresponding ERR_PTR(). Caller must
482 * check the return value with help of IS_ERR() helper.
483 */
484struct thermal_zone_device *
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400485thermal_zone_of_sensor_register(struct device *dev, int sensor_id, void *data,
486 const struct thermal_zone_of_device_ops *ops)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400487{
488 struct device_node *np, *child, *sensor_np;
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300489 struct thermal_zone_device *tzd = ERR_PTR(-ENODEV);
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700490 struct __sensor_param *sens_param = NULL;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400491
492 np = of_find_node_by_name(NULL, "thermal-zones");
493 if (!np)
494 return ERR_PTR(-ENODEV);
495
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300496 if (!dev || !dev->of_node) {
497 of_node_put(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400498 return ERR_PTR(-EINVAL);
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300499 }
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400500
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700501 sens_param = kzalloc(sizeof(*sens_param), GFP_KERNEL);
502 if (!sens_param) {
503 of_node_put(np);
504 return ERR_PTR(-ENOMEM);
505 }
506 sens_param->sensor_data = data;
507 sens_param->ops = ops;
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300508 sensor_np = of_node_get(dev->of_node);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400509
Laxman Dewangan42bbe402016-02-08 18:58:34 +0530510 for_each_available_child_of_node(np, child) {
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400511 struct of_phandle_args sensor_specs;
512 int ret, id;
513
514 /* For now, thermal framework supports only 1 sensor per zone */
515 ret = of_parse_phandle_with_args(child, "thermal-sensors",
516 "#thermal-sensor-cells",
517 0, &sensor_specs);
518 if (ret)
519 continue;
520
521 if (sensor_specs.args_count >= 1) {
522 id = sensor_specs.args[0];
523 WARN(sensor_specs.args_count > 1,
524 "%s: too many cells in sensor specifier %d\n",
525 sensor_specs.np->name, sensor_specs.args_count);
526 } else {
527 id = 0;
528 }
529
530 if (sensor_specs.np == sensor_np && id == sensor_id) {
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300531 tzd = thermal_zone_of_add_sensor(child, sensor_np,
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700532 sens_param);
Lukasz Majewski528012c12015-01-19 12:44:04 +0100533 if (!IS_ERR(tzd))
534 tzd->ops->set_mode(tzd, THERMAL_DEVICE_ENABLED);
535
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300536 of_node_put(sensor_specs.np);
537 of_node_put(child);
538 goto exit;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400539 }
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300540 of_node_put(sensor_specs.np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400541 }
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300542exit:
543 of_node_put(sensor_np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400544 of_node_put(np);
545
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700546 if (tzd == ERR_PTR(-ENODEV))
547 kfree(sens_param);
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300548 return tzd;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400549}
550EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_register);
551
552/**
553 * thermal_zone_of_sensor_unregister - unregisters a sensor from a DT thermal zone
554 * @dev: a valid struct device pointer of a sensor device. Must contain
555 * a valid .of_node, for the sensor node.
556 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
557 *
558 * This function removes the sensor callbacks and private data from the
559 * thermal zone device registered with thermal_zone_of_sensor_register()
560 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
561 * thermal zone device callbacks.
562 *
563 * TODO: When the support to several sensors per zone is added, this
564 * function must search the sensor list based on @dev parameter.
565 *
566 */
567void thermal_zone_of_sensor_unregister(struct device *dev,
568 struct thermal_zone_device *tzd)
569{
570 struct __thermal_zone *tz;
571
572 if (!dev || !tzd || !tzd->devdata)
573 return;
574
575 tz = tzd->devdata;
576
577 /* no __thermal_zone, nothing to be done */
578 if (!tz)
579 return;
580
581 mutex_lock(&tzd->lock);
582 tzd->ops->get_temp = NULL;
583 tzd->ops->get_trend = NULL;
Lukasz Majewski184a4bf2014-12-08 18:04:21 +0100584 tzd->ops->set_emul_temp = NULL;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400585
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700586 kfree(tz->senps);
587 tz->senps = NULL;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400588 mutex_unlock(&tzd->lock);
589}
590EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister);
591
Laxman Dewangane498b492016-03-09 18:40:06 +0530592static void devm_thermal_zone_of_sensor_release(struct device *dev, void *res)
593{
594 thermal_zone_of_sensor_unregister(dev,
595 *(struct thermal_zone_device **)res);
596}
597
598static int devm_thermal_zone_of_sensor_match(struct device *dev, void *res,
599 void *data)
600{
601 struct thermal_zone_device **r = res;
602
603 if (WARN_ON(!r || !*r))
604 return 0;
605
606 return *r == data;
607}
608
609/**
610 * devm_thermal_zone_of_sensor_register - Resource managed version of
611 * thermal_zone_of_sensor_register()
612 * @dev: a valid struct device pointer of a sensor device. Must contain
613 * a valid .of_node, for the sensor node.
614 * @sensor_id: a sensor identifier, in case the sensor IP has more
615 * than one sensors
616 * @data: a private pointer (owned by the caller) that will be passed
617 * back, when a temperature reading is needed.
618 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
619 *
620 * Refer thermal_zone_of_sensor_register() for more details.
621 *
622 * Return: On success returns a valid struct thermal_zone_device,
623 * otherwise, it returns a corresponding ERR_PTR(). Caller must
624 * check the return value with help of IS_ERR() helper.
Zhang Rui7b5c4a02016-08-22 15:48:11 +0800625 * Registered thermal_zone_device device will automatically be
Laxman Dewangane498b492016-03-09 18:40:06 +0530626 * released when device is unbounded.
627 */
628struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
629 struct device *dev, int sensor_id,
630 void *data, const struct thermal_zone_of_device_ops *ops)
631{
632 struct thermal_zone_device **ptr, *tzd;
633
634 ptr = devres_alloc(devm_thermal_zone_of_sensor_release, sizeof(*ptr),
635 GFP_KERNEL);
636 if (!ptr)
637 return ERR_PTR(-ENOMEM);
638
639 tzd = thermal_zone_of_sensor_register(dev, sensor_id, data, ops);
640 if (IS_ERR(tzd)) {
641 devres_free(ptr);
642 return tzd;
643 }
644
645 *ptr = tzd;
646 devres_add(dev, ptr);
647
648 return tzd;
649}
650EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_register);
651
652/**
653 * devm_thermal_zone_of_sensor_unregister - Resource managed version of
654 * thermal_zone_of_sensor_unregister().
655 * @dev: Device for which which resource was allocated.
656 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
657 *
658 * This function removes the sensor callbacks and private data from the
659 * thermal zone device registered with devm_thermal_zone_of_sensor_register()
660 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
661 * thermal zone device callbacks.
662 * Normally this function will not need to be called and the resource
663 * management code will ensure that the resource is freed.
664 */
665void devm_thermal_zone_of_sensor_unregister(struct device *dev,
666 struct thermal_zone_device *tzd)
667{
668 WARN_ON(devres_release(dev, devm_thermal_zone_of_sensor_release,
669 devm_thermal_zone_of_sensor_match, tzd));
670}
671EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_unregister);
672
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400673/*** functions parsing device tree nodes ***/
674
675/**
676 * thermal_of_populate_bind_params - parse and fill cooling map data
677 * @np: DT node containing a cooling-map node
678 * @__tbp: data structure to be filled with cooling map info
679 * @trips: array of thermal zone trip points
680 * @ntrips: number of trip points inside trips.
681 *
682 * This function parses a cooling-map type of node represented by
683 * @np parameter and fills the read data into @__tbp data structure.
684 * It needs the already parsed array of trip points of the thermal zone
685 * in consideration.
686 *
687 * Return: 0 on success, proper error code otherwise
688 */
689static int thermal_of_populate_bind_params(struct device_node *np,
690 struct __thermal_bind_params *__tbp,
Lukasz Majewskiad9914a2014-12-08 18:04:19 +0100691 struct thermal_trip *trips,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400692 int ntrips)
693{
694 struct of_phandle_args cooling_spec;
695 struct device_node *trip;
696 int ret, i;
697 u32 prop;
698
699 /* Default weight. Usage is optional */
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000700 __tbp->usage = THERMAL_WEIGHT_DEFAULT;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400701 ret = of_property_read_u32(np, "contribution", &prop);
702 if (ret == 0)
703 __tbp->usage = prop;
704
705 trip = of_parse_phandle(np, "trip", 0);
706 if (!trip) {
707 pr_err("missing trip property\n");
708 return -ENODEV;
709 }
710
711 /* match using device_node */
712 for (i = 0; i < ntrips; i++)
713 if (trip == trips[i].np) {
714 __tbp->trip_id = i;
715 break;
716 }
717
718 if (i == ntrips) {
719 ret = -ENODEV;
720 goto end;
721 }
722
723 ret = of_parse_phandle_with_args(np, "cooling-device", "#cooling-cells",
724 0, &cooling_spec);
725 if (ret < 0) {
726 pr_err("missing cooling_device property\n");
727 goto end;
728 }
729 __tbp->cooling_device = cooling_spec.np;
730 if (cooling_spec.args_count >= 2) { /* at least min and max */
731 __tbp->min = cooling_spec.args[0];
732 __tbp->max = cooling_spec.args[1];
733 } else {
734 pr_err("wrong reference to cooling device, missing limits\n");
735 }
736
737end:
738 of_node_put(trip);
739
740 return ret;
741}
742
743/**
744 * It maps 'enum thermal_trip_type' found in include/linux/thermal.h
745 * into the device tree binding of 'trip', property type.
746 */
747static const char * const trip_types[] = {
748 [THERMAL_TRIP_ACTIVE] = "active",
749 [THERMAL_TRIP_PASSIVE] = "passive",
750 [THERMAL_TRIP_HOT] = "hot",
751 [THERMAL_TRIP_CRITICAL] = "critical",
752};
753
754/**
755 * thermal_of_get_trip_type - Get phy mode for given device_node
756 * @np: Pointer to the given device_node
757 * @type: Pointer to resulting trip type
758 *
759 * The function gets trip type string from property 'type',
760 * and store its index in trip_types table in @type,
761 *
762 * Return: 0 on success, or errno in error case.
763 */
764static int thermal_of_get_trip_type(struct device_node *np,
765 enum thermal_trip_type *type)
766{
767 const char *t;
768 int err, i;
769
770 err = of_property_read_string(np, "type", &t);
771 if (err < 0)
772 return err;
773
774 for (i = 0; i < ARRAY_SIZE(trip_types); i++)
775 if (!strcasecmp(t, trip_types[i])) {
776 *type = i;
777 return 0;
778 }
779
780 return -ENODEV;
781}
782
783/**
784 * thermal_of_populate_trip - parse and fill one trip point data
785 * @np: DT node containing a trip point node
786 * @trip: trip point data structure to be filled up
787 *
788 * This function parses a trip point type of node represented by
789 * @np parameter and fills the read data into @trip data structure.
790 *
791 * Return: 0 on success, proper error code otherwise
792 */
793static int thermal_of_populate_trip(struct device_node *np,
Lukasz Majewskiad9914a2014-12-08 18:04:19 +0100794 struct thermal_trip *trip)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400795{
796 int prop;
797 int ret;
798
799 ret = of_property_read_u32(np, "temperature", &prop);
800 if (ret < 0) {
801 pr_err("missing temperature property\n");
802 return ret;
803 }
804 trip->temperature = prop;
805
806 ret = of_property_read_u32(np, "hysteresis", &prop);
807 if (ret < 0) {
808 pr_err("missing hysteresis property\n");
809 return ret;
810 }
811 trip->hysteresis = prop;
812
813 ret = thermal_of_get_trip_type(np, &trip->type);
814 if (ret < 0) {
815 pr_err("wrong trip type property\n");
816 return ret;
817 }
818
819 /* Required for cooling map matching */
820 trip->np = np;
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300821 of_node_get(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400822
823 return 0;
824}
825
826/**
827 * thermal_of_build_thermal_zone - parse and fill one thermal zone data
828 * @np: DT node containing a thermal zone node
829 *
830 * This function parses a thermal zone type of node represented by
831 * @np parameter and fills the read data into a __thermal_zone data structure
832 * and return this pointer.
833 *
Eduardo Valentina46dbae2015-05-11 19:48:09 -0700834 * TODO: Missing properties to parse: thermal-sensor-names
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400835 *
836 * Return: On success returns a valid struct __thermal_zone,
837 * otherwise, it returns a corresponding ERR_PTR(). Caller must
838 * check the return value with help of IS_ERR() helper.
839 */
Julia Lawallc0ff8aa2016-04-19 14:33:32 +0200840static struct __thermal_zone
841__init *thermal_of_build_thermal_zone(struct device_node *np)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400842{
843 struct device_node *child = NULL, *gchild;
844 struct __thermal_zone *tz;
845 int ret, i;
Eduardo Valentina46dbae2015-05-11 19:48:09 -0700846 u32 prop, coef[2];
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400847
848 if (!np) {
849 pr_err("no thermal zone np\n");
850 return ERR_PTR(-EINVAL);
851 }
852
853 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
854 if (!tz)
855 return ERR_PTR(-ENOMEM);
856
857 ret = of_property_read_u32(np, "polling-delay-passive", &prop);
858 if (ret < 0) {
859 pr_err("missing polling-delay-passive property\n");
860 goto free_tz;
861 }
862 tz->passive_delay = prop;
863
864 ret = of_property_read_u32(np, "polling-delay", &prop);
865 if (ret < 0) {
866 pr_err("missing polling-delay property\n");
867 goto free_tz;
868 }
869 tz->polling_delay = prop;
870
Eduardo Valentina46dbae2015-05-11 19:48:09 -0700871 /*
872 * REVIST: for now, the thermal framework supports only
873 * one sensor per thermal zone. Thus, we are considering
874 * only the first two values as slope and offset.
875 */
876 ret = of_property_read_u32_array(np, "coefficients", coef, 2);
877 if (ret == 0) {
878 tz->slope = coef[0];
879 tz->offset = coef[1];
880 } else {
881 tz->slope = 1;
882 tz->offset = 0;
883 }
884
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400885 /* trips */
886 child = of_get_child_by_name(np, "trips");
887
888 /* No trips provided */
889 if (!child)
890 goto finish;
891
892 tz->ntrips = of_get_child_count(child);
893 if (tz->ntrips == 0) /* must have at least one child */
894 goto finish;
895
896 tz->trips = kzalloc(tz->ntrips * sizeof(*tz->trips), GFP_KERNEL);
897 if (!tz->trips) {
898 ret = -ENOMEM;
899 goto free_tz;
900 }
901
902 i = 0;
903 for_each_child_of_node(child, gchild) {
904 ret = thermal_of_populate_trip(gchild, &tz->trips[i++]);
905 if (ret)
906 goto free_trips;
907 }
908
909 of_node_put(child);
910
911 /* cooling-maps */
912 child = of_get_child_by_name(np, "cooling-maps");
913
914 /* cooling-maps not provided */
915 if (!child)
916 goto finish;
917
918 tz->num_tbps = of_get_child_count(child);
919 if (tz->num_tbps == 0)
920 goto finish;
921
922 tz->tbps = kzalloc(tz->num_tbps * sizeof(*tz->tbps), GFP_KERNEL);
923 if (!tz->tbps) {
924 ret = -ENOMEM;
925 goto free_trips;
926 }
927
928 i = 0;
Stephen Boydca9521b2014-06-18 16:32:08 -0700929 for_each_child_of_node(child, gchild) {
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400930 ret = thermal_of_populate_bind_params(gchild, &tz->tbps[i++],
931 tz->trips, tz->ntrips);
932 if (ret)
933 goto free_tbps;
Stephen Boydca9521b2014-06-18 16:32:08 -0700934 }
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400935
936finish:
937 of_node_put(child);
938 tz->mode = THERMAL_DEVICE_DISABLED;
939
940 return tz;
941
942free_tbps:
Ulises Brindis1cd91c12016-03-25 12:55:41 -0700943 for (i = i - 1; i >= 0; i--)
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300944 of_node_put(tz->tbps[i].cooling_device);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400945 kfree(tz->tbps);
946free_trips:
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300947 for (i = 0; i < tz->ntrips; i++)
948 of_node_put(tz->trips[i].np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400949 kfree(tz->trips);
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300950 of_node_put(gchild);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400951free_tz:
952 kfree(tz);
953 of_node_put(child);
954
955 return ERR_PTR(ret);
956}
957
958static inline void of_thermal_free_zone(struct __thermal_zone *tz)
959{
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300960 int i;
961
962 for (i = 0; i < tz->num_tbps; i++)
963 of_node_put(tz->tbps[i].cooling_device);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400964 kfree(tz->tbps);
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300965 for (i = 0; i < tz->ntrips; i++)
966 of_node_put(tz->trips[i].np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400967 kfree(tz->trips);
968 kfree(tz);
969}
970
971/**
972 * of_parse_thermal_zones - parse device tree thermal data
973 *
974 * Initialization function that can be called by machine initialization
975 * code to parse thermal data and populate the thermal framework
976 * with hardware thermal zones info. This function only parses thermal zones.
977 * Cooling devices and sensor devices nodes are supposed to be parsed
978 * by their respective drivers.
979 *
980 * Return: 0 on success, proper error code otherwise
981 *
982 */
983int __init of_parse_thermal_zones(void)
984{
985 struct device_node *np, *child;
986 struct __thermal_zone *tz;
987 struct thermal_zone_device_ops *ops;
988
989 np = of_find_node_by_name(NULL, "thermal-zones");
990 if (!np) {
991 pr_debug("unable to find thermal zones\n");
992 return 0; /* Run successfully on systems without thermal DT */
993 }
994
Laxman Dewangan42bbe402016-02-08 18:58:34 +0530995 for_each_available_child_of_node(np, child) {
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400996 struct thermal_zone_device *zone;
997 struct thermal_zone_params *tzp;
Punit Agrawal76af5492015-03-03 10:43:04 +0000998 int i, mask = 0;
Punit Agrawal647f9922015-02-26 19:00:32 +0000999 u32 prop;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001000
1001 tz = thermal_of_build_thermal_zone(child);
1002 if (IS_ERR(tz)) {
1003 pr_err("failed to build thermal zone %s: %ld\n",
1004 child->name,
1005 PTR_ERR(tz));
1006 continue;
1007 }
1008
1009 ops = kmemdup(&of_thermal_ops, sizeof(*ops), GFP_KERNEL);
1010 if (!ops)
1011 goto exit_free;
1012
1013 tzp = kzalloc(sizeof(*tzp), GFP_KERNEL);
1014 if (!tzp) {
1015 kfree(ops);
1016 goto exit_free;
1017 }
1018
1019 /* No hwmon because there might be hwmon drivers registering */
1020 tzp->no_hwmon = true;
1021
Punit Agrawal647f9922015-02-26 19:00:32 +00001022 if (!of_property_read_u32(child, "sustainable-power", &prop))
1023 tzp->sustainable_power = prop;
1024
Punit Agrawal76af5492015-03-03 10:43:04 +00001025 for (i = 0; i < tz->ntrips; i++)
1026 mask |= 1 << i;
1027
Eduardo Valentina46dbae2015-05-11 19:48:09 -07001028 /* these two are left for temperature drivers to use */
1029 tzp->slope = tz->slope;
1030 tzp->offset = tz->offset;
1031
Lina Iyer159f67d2016-07-27 11:34:46 -06001032 if (of_property_read_bool(child, "tracks-low"))
1033 tzp->tracks_low = true;
1034
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001035 zone = thermal_zone_device_register(child->name, tz->ntrips,
Punit Agrawal76af5492015-03-03 10:43:04 +00001036 mask, tz,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001037 ops, tzp,
1038 tz->passive_delay,
1039 tz->polling_delay);
1040 if (IS_ERR(zone)) {
1041 pr_err("Failed to build %s zone %ld\n", child->name,
1042 PTR_ERR(zone));
1043 kfree(tzp);
1044 kfree(ops);
1045 of_thermal_free_zone(tz);
1046 /* attempting to build remaining zones still */
1047 }
1048 }
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001049 of_node_put(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001050
1051 return 0;
1052
1053exit_free:
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001054 of_node_put(child);
1055 of_node_put(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001056 of_thermal_free_zone(tz);
1057
1058 /* no memory available, so free what we have built */
1059 of_thermal_destroy_zones();
1060
1061 return -ENOMEM;
1062}
1063
1064/**
1065 * of_thermal_destroy_zones - remove all zones parsed and allocated resources
1066 *
1067 * Finds all zones parsed and added to the thermal framework and remove them
1068 * from the system, together with their resources.
1069 *
1070 */
1071void of_thermal_destroy_zones(void)
1072{
1073 struct device_node *np, *child;
1074
1075 np = of_find_node_by_name(NULL, "thermal-zones");
1076 if (!np) {
Jiada Wang28524982015-11-16 17:10:05 +09001077 pr_debug("unable to find thermal zones\n");
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001078 return;
1079 }
1080
Laxman Dewangan42bbe402016-02-08 18:58:34 +05301081 for_each_available_child_of_node(np, child) {
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001082 struct thermal_zone_device *zone;
1083
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001084 zone = thermal_zone_get_zone_by_name(child->name);
1085 if (IS_ERR(zone))
1086 continue;
1087
1088 thermal_zone_device_unregister(zone);
1089 kfree(zone->tzp);
1090 kfree(zone->ops);
1091 of_thermal_free_zone(zone->devdata);
1092 }
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001093 of_node_put(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001094}