blob: c662cd783bb893b80b874bdf13dd017cc48dd9ee [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>
Ram Chandrasekard29230b2017-02-27 11:26:51 -070034#include <linux/list.h>
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040035
Ram Chandrasekar02592fa2017-05-16 17:03:02 -060036#define CREATE_TRACE_POINTS
37#include <trace/events/thermal_virtual.h>
38
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040039#include "thermal_core.h"
40
Ram Chandrasekard29230b2017-02-27 11:26:51 -070041/*** Private data structures to represent thermal device tree data ***/
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040042/**
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040043 * struct __thermal_bind_param - a match between trip and cooling device
44 * @cooling_device: a pointer to identify the referred cooling device
45 * @trip_id: the trip point index
46 * @usage: the percentage (from 0 to 100) of cooling contribution
47 * @min: minimum cooling state used at this trip point
48 * @max: maximum cooling state used at this trip point
49 */
50
51struct __thermal_bind_params {
52 struct device_node *cooling_device;
53 unsigned int trip_id;
54 unsigned int usage;
55 unsigned long min;
56 unsigned long max;
57};
58
59/**
Ram Chandrasekare34ced62017-03-03 11:22:50 -070060 * struct __sensor_param - Holds individual sensor data
61 * @sensor_data: sensor driver private data passed as input argument
62 * @ops: sensor driver ops
Lina Iyer01ffea22016-07-27 13:46:32 -060063 * @trip_high: last trip high value programmed in the sensor driver
64 * @trip_low: last trip low value programmed in the sensor driver
65 * @lock: mutex lock acquired before updating the trip temperatures
Ram Chandrasekard29230b2017-02-27 11:26:51 -070066 * @first_tz: list head pointing the first thermal zone
Ram Chandrasekare34ced62017-03-03 11:22:50 -070067 */
68struct __sensor_param {
69 void *sensor_data;
70 const struct thermal_zone_of_device_ops *ops;
Lina Iyer01ffea22016-07-27 13:46:32 -060071 int trip_high, trip_low;
72 struct mutex lock;
Ram Chandrasekard29230b2017-02-27 11:26:51 -070073 struct list_head first_tz;
Ram Chandrasekare34ced62017-03-03 11:22:50 -070074};
75
76/**
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040077 * struct __thermal_zone - internal representation of a thermal zone
78 * @mode: current thermal zone device mode (enabled/disabled)
79 * @passive_delay: polling interval while passive cooling is activated
80 * @polling_delay: zone polling interval
Eduardo Valentina46dbae2015-05-11 19:48:09 -070081 * @slope: slope of the temperature adjustment curve
82 * @offset: offset of the temperature adjustment curve
Ram Chandrasekar07479402017-08-25 14:04:42 -060083 * @default_disable: Keep the thermal zone disabled by default
Ram Chandrasekard29230b2017-02-27 11:26:51 -070084 * @tzd: thermal zone device pointer for this sensor
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040085 * @ntrips: number of trip points
86 * @trips: an array of trip points (0..ntrips - 1)
87 * @num_tbps: number of thermal bind params
88 * @tbps: an array of thermal bind params (0..num_tbps - 1)
Ram Chandrasekard29230b2017-02-27 11:26:51 -070089 * @list: sibling thermal zone pointer
Ram Chandrasekare34ced62017-03-03 11:22:50 -070090 * @senps: sensor related parameters
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040091 */
92
93struct __thermal_zone {
94 enum thermal_device_mode mode;
95 int passive_delay;
96 int polling_delay;
Eduardo Valentina46dbae2015-05-11 19:48:09 -070097 int slope;
98 int offset;
Ram Chandrasekard29230b2017-02-27 11:26:51 -070099 struct thermal_zone_device *tzd;
Ram Chandrasekar07479402017-08-25 14:04:42 -0600100 bool default_disable;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400101
102 /* trip data */
103 int ntrips;
Lukasz Majewskiad9914a2014-12-08 18:04:19 +0100104 struct thermal_trip *trips;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400105
106 /* cooling binding data */
107 int num_tbps;
108 struct __thermal_bind_params *tbps;
109
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700110 struct list_head list;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400111 /* sensor interface */
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700112 struct __sensor_param *senps;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400113};
114
Ram Chandrasekar2f2b7462017-03-11 19:35:11 -0700115/**
116 * struct virtual_sensor - internal representation of a virtual thermal zone
117 * @num_sensors - number of sensors this virtual sensor will reference to
118 * estimate temperature
119 * @tz - Array of thermal zones of the sensors this virtual sensor will use
120 * to estimate temperature
Ram Chandrasekar02592fa2017-05-16 17:03:02 -0600121 * @virt_tz - Virtual thermal zone pointer
Ram Chandrasekar2f2b7462017-03-11 19:35:11 -0700122 * @logic - aggregation logic to be used to estimate the temperature
123 * @last_reading - last estimated temperature
124 * @coefficients - array of coefficients to be used for weighted aggregation
125 * logic
126 * @avg_offset - offset value to be used for the weighted aggregation logic
127 * @avg_denominator - denominator value to be used for the weighted aggregation
128 * logic
129 */
130struct virtual_sensor {
131 int num_sensors;
132 struct thermal_zone_device *tz[THERMAL_MAX_VIRT_SENSORS];
Ram Chandrasekar02592fa2017-05-16 17:03:02 -0600133 struct thermal_zone_device *virt_tz;
Ram Chandrasekar2f2b7462017-03-11 19:35:11 -0700134 enum aggregation_logic logic;
135 int last_reading;
136 int coefficients[THERMAL_MAX_VIRT_SENSORS];
137 int avg_offset;
138 int avg_denominator;
139};
140
Lina Iyer01ffea22016-07-27 13:46:32 -0600141static int of_thermal_aggregate_trip_types(struct thermal_zone_device *tz,
142 unsigned int trip_type_mask, int *low, int *high);
143
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400144/*** DT thermal zone device callbacks ***/
145
Ram Chandrasekar2f2b7462017-03-11 19:35:11 -0700146static int virt_sensor_read_temp(void *data, int *val)
147{
148 struct virtual_sensor *sens = data;
149 int idx, temp = 0, ret = 0;
150
151 for (idx = 0; idx < sens->num_sensors; idx++) {
152 int sens_temp = 0;
153
154 ret = thermal_zone_get_temp(sens->tz[idx], &sens_temp);
155 if (ret) {
156 pr_err("virt zone: sensor[%s] read error:%d\n",
157 sens->tz[idx]->type, ret);
158 return ret;
159 }
160 switch (sens->logic) {
161 case VIRT_WEIGHTED_AVG:
162 temp += sens_temp * sens->coefficients[idx];
163 if (idx == (sens->num_sensors - 1))
164 temp = (temp + sens->avg_offset)
165 / sens->avg_denominator;
166 break;
167 case VIRT_MAXIMUM:
168 if (idx == 0)
169 temp = INT_MIN;
170 if (sens_temp > temp)
171 temp = sens_temp;
172 break;
173 case VIRT_MINIMUM:
174 if (idx == 0)
175 temp = INT_MAX;
176 if (sens_temp < temp)
177 temp = sens_temp;
178 break;
179 default:
180 break;
181 }
Ram Chandrasekar02592fa2017-05-16 17:03:02 -0600182 trace_virtual_temperature(sens->virt_tz, sens->tz[idx],
183 sens_temp, temp);
Ram Chandrasekar2f2b7462017-03-11 19:35:11 -0700184 }
185
186 sens->last_reading = *val = temp;
187
188 return 0;
189}
190
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400191static int of_thermal_get_temp(struct thermal_zone_device *tz,
Sascha Hauer17e83512015-07-24 08:12:54 +0200192 int *temp)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400193{
194 struct __thermal_zone *data = tz->devdata;
195
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700196 if (!data->senps || !data->senps->ops->get_temp)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400197 return -EINVAL;
Ram Chandrasekar37c816d2017-08-25 12:34:02 -0600198 if (data->mode == THERMAL_DEVICE_DISABLED) {
199 *temp = tz->tzp->tracks_low ?
200 THERMAL_TEMP_INVALID_LOW :
201 THERMAL_TEMP_INVALID;
202 return 0;
203 }
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400204
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700205 return data->senps->ops->get_temp(data->senps->sensor_data, temp);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400206}
207
Sascha Hauer826386e2016-06-22 16:42:02 +0800208static int of_thermal_set_trips(struct thermal_zone_device *tz,
Lina Iyer01ffea22016-07-27 13:46:32 -0600209 int inp_low, int inp_high)
Sascha Hauer826386e2016-06-22 16:42:02 +0800210{
211 struct __thermal_zone *data = tz->devdata;
Lina Iyer01ffea22016-07-27 13:46:32 -0600212 int high = INT_MAX, low = INT_MIN, ret = 0;
Sascha Hauer826386e2016-06-22 16:42:02 +0800213
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700214 if (!data->senps || !data->senps->ops->set_trips)
Sascha Hauer826386e2016-06-22 16:42:02 +0800215 return -EINVAL;
216
Lina Iyer01ffea22016-07-27 13:46:32 -0600217 mutex_lock(&data->senps->lock);
218 of_thermal_aggregate_trip_types(tz, GENMASK(THERMAL_TRIP_CRITICAL, 0),
219 &low, &high);
Lina Iyer01ffea22016-07-27 13:46:32 -0600220 data->senps->trip_low = low;
221 data->senps->trip_high = high;
222 ret = data->senps->ops->set_trips(data->senps->sensor_data,
223 low, high);
224
Lina Iyer01ffea22016-07-27 13:46:32 -0600225 mutex_unlock(&data->senps->lock);
226 return ret;
Sascha Hauer826386e2016-06-22 16:42:02 +0800227}
228
Lukasz Majewski08dab662014-12-08 18:04:17 +0100229/**
230 * of_thermal_get_ntrips - function to export number of available trip
231 * points.
232 * @tz: pointer to a thermal zone
233 *
234 * This function is a globally visible wrapper to get number of trip points
235 * stored in the local struct __thermal_zone
236 *
237 * Return: number of available trip points, -ENODEV when data not available
238 */
239int of_thermal_get_ntrips(struct thermal_zone_device *tz)
240{
241 struct __thermal_zone *data = tz->devdata;
242
243 if (!data || IS_ERR(data))
244 return -ENODEV;
245
246 return data->ntrips;
247}
248EXPORT_SYMBOL_GPL(of_thermal_get_ntrips);
249
Lukasz Majewskia9bf2cc2014-12-08 18:04:18 +0100250/**
251 * of_thermal_is_trip_valid - function to check if trip point is valid
252 *
253 * @tz: pointer to a thermal zone
254 * @trip: trip point to evaluate
255 *
256 * This function is responsible for checking if passed trip point is valid
257 *
258 * Return: true if trip point is valid, false otherwise
259 */
260bool of_thermal_is_trip_valid(struct thermal_zone_device *tz, int trip)
261{
262 struct __thermal_zone *data = tz->devdata;
263
264 if (!data || trip >= data->ntrips || trip < 0)
265 return false;
266
267 return true;
268}
269EXPORT_SYMBOL_GPL(of_thermal_is_trip_valid);
270
Lukasz Majewskice8be772014-12-08 18:04:20 +0100271/**
272 * of_thermal_get_trip_points - function to get access to a globally exported
273 * trip points
274 *
275 * @tz: pointer to a thermal zone
276 *
277 * This function provides a pointer to trip points table
278 *
279 * Return: pointer to trip points table, NULL otherwise
280 */
Geert Uytterhoevenebc31932015-01-03 22:56:56 +0100281const struct thermal_trip *
Lukasz Majewskice8be772014-12-08 18:04:20 +0100282of_thermal_get_trip_points(struct thermal_zone_device *tz)
283{
284 struct __thermal_zone *data = tz->devdata;
285
286 if (!data)
287 return NULL;
288
289 return data->trips;
290}
291EXPORT_SYMBOL_GPL(of_thermal_get_trip_points);
292
Lukasz Majewski184a4bf2014-12-08 18:04:21 +0100293/**
294 * of_thermal_set_emul_temp - function to set emulated temperature
295 *
296 * @tz: pointer to a thermal zone
297 * @temp: temperature to set
298 *
299 * This function gives the ability to set emulated value of temperature,
300 * which is handy for debugging
301 *
302 * Return: zero on success, error code otherwise
303 */
304static int of_thermal_set_emul_temp(struct thermal_zone_device *tz,
Sascha Hauer17e83512015-07-24 08:12:54 +0200305 int temp)
Lukasz Majewski184a4bf2014-12-08 18:04:21 +0100306{
307 struct __thermal_zone *data = tz->devdata;
308
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700309 if (!data->senps || !data->senps->ops->set_emul_temp)
310 return -EINVAL;
311
312 return data->senps->ops->set_emul_temp(data->senps->sensor_data, temp);
Lukasz Majewski184a4bf2014-12-08 18:04:21 +0100313}
314
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400315static int of_thermal_get_trend(struct thermal_zone_device *tz, int trip,
316 enum thermal_trend *trend)
317{
318 struct __thermal_zone *data = tz->devdata;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400319
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700320 if (!data->senps || !data->senps->ops->get_trend)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400321 return -EINVAL;
322
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700323 return data->senps->ops->get_trend(data->senps->sensor_data,
324 trip, trend);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400325}
326
327static int of_thermal_bind(struct thermal_zone_device *thermal,
328 struct thermal_cooling_device *cdev)
329{
330 struct __thermal_zone *data = thermal->devdata;
331 int i;
332
333 if (!data || IS_ERR(data))
334 return -ENODEV;
335
336 /* find where to bind */
337 for (i = 0; i < data->num_tbps; i++) {
338 struct __thermal_bind_params *tbp = data->tbps + i;
339
340 if (tbp->cooling_device == cdev->np) {
341 int ret;
342
343 ret = thermal_zone_bind_cooling_device(thermal,
344 tbp->trip_id, cdev,
Punit Agrawaldd354b82014-06-03 10:59:58 +0100345 tbp->max,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000346 tbp->min,
347 tbp->usage);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400348 if (ret)
349 return ret;
350 }
351 }
352
353 return 0;
354}
355
356static int of_thermal_unbind(struct thermal_zone_device *thermal,
357 struct thermal_cooling_device *cdev)
358{
359 struct __thermal_zone *data = thermal->devdata;
360 int i;
361
362 if (!data || IS_ERR(data))
363 return -ENODEV;
364
365 /* find where to unbind */
366 for (i = 0; i < data->num_tbps; i++) {
367 struct __thermal_bind_params *tbp = data->tbps + i;
368
369 if (tbp->cooling_device == cdev->np) {
370 int ret;
371
372 ret = thermal_zone_unbind_cooling_device(thermal,
373 tbp->trip_id, cdev);
374 if (ret)
375 return ret;
376 }
377 }
378
379 return 0;
380}
381
382static int of_thermal_get_mode(struct thermal_zone_device *tz,
383 enum thermal_device_mode *mode)
384{
385 struct __thermal_zone *data = tz->devdata;
386
387 *mode = data->mode;
388
389 return 0;
390}
391
392static int of_thermal_set_mode(struct thermal_zone_device *tz,
393 enum thermal_device_mode mode)
394{
395 struct __thermal_zone *data = tz->devdata;
396
397 mutex_lock(&tz->lock);
398
399 if (mode == THERMAL_DEVICE_ENABLED)
400 tz->polling_delay = data->polling_delay;
401 else
402 tz->polling_delay = 0;
403
404 mutex_unlock(&tz->lock);
405
406 data->mode = mode;
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700407 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400408
409 return 0;
410}
411
412static int of_thermal_get_trip_type(struct thermal_zone_device *tz, int trip,
413 enum thermal_trip_type *type)
414{
415 struct __thermal_zone *data = tz->devdata;
416
417 if (trip >= data->ntrips || trip < 0)
418 return -EDOM;
419
420 *type = data->trips[trip].type;
421
422 return 0;
423}
424
425static int of_thermal_get_trip_temp(struct thermal_zone_device *tz, int trip,
Sascha Hauer17e83512015-07-24 08:12:54 +0200426 int *temp)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400427{
428 struct __thermal_zone *data = tz->devdata;
429
430 if (trip >= data->ntrips || trip < 0)
431 return -EDOM;
432
Ram Chandrasekar32e8bfd2018-02-06 17:12:04 -0700433 if (data->senps && data->senps->ops->get_trip_temp) {
434 int ret;
435
436 ret = data->senps->ops->get_trip_temp(data->senps->sensor_data,
437 trip, temp);
438 if (ret)
439 return ret;
440 } else {
441 *temp = data->trips[trip].temperature;
442 }
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400443
444 return 0;
445}
446
447static int of_thermal_set_trip_temp(struct thermal_zone_device *tz, int trip,
Sascha Hauer17e83512015-07-24 08:12:54 +0200448 int temp)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400449{
450 struct __thermal_zone *data = tz->devdata;
451
452 if (trip >= data->ntrips || trip < 0)
453 return -EDOM;
454
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700455 if (data->senps && data->senps->ops->set_trip_temp) {
Wei Nic3509522016-03-29 18:29:17 +0800456 int ret;
457
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700458 ret = data->senps->ops->set_trip_temp(data->senps->sensor_data,
459 trip, temp);
Wei Nic3509522016-03-29 18:29:17 +0800460 if (ret)
461 return ret;
462 }
463
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400464 /* thermal framework should take care of data->mask & (1 << trip) */
465 data->trips[trip].temperature = temp;
466
467 return 0;
468}
469
470static int of_thermal_get_trip_hyst(struct thermal_zone_device *tz, int trip,
Sascha Hauer17e83512015-07-24 08:12:54 +0200471 int *hyst)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400472{
473 struct __thermal_zone *data = tz->devdata;
474
475 if (trip >= data->ntrips || trip < 0)
476 return -EDOM;
477
478 *hyst = data->trips[trip].hysteresis;
479
480 return 0;
481}
482
483static int of_thermal_set_trip_hyst(struct thermal_zone_device *tz, int trip,
Sascha Hauer17e83512015-07-24 08:12:54 +0200484 int hyst)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400485{
486 struct __thermal_zone *data = tz->devdata;
487
488 if (trip >= data->ntrips || trip < 0)
489 return -EDOM;
490
491 /* thermal framework should take care of data->mask & (1 << trip) */
492 data->trips[trip].hysteresis = hyst;
493
494 return 0;
495}
496
497static int of_thermal_get_crit_temp(struct thermal_zone_device *tz,
Sascha Hauer17e83512015-07-24 08:12:54 +0200498 int *temp)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400499{
500 struct __thermal_zone *data = tz->devdata;
501 int i;
502
503 for (i = 0; i < data->ntrips; i++)
504 if (data->trips[i].type == THERMAL_TRIP_CRITICAL) {
505 *temp = data->trips[i].temperature;
506 return 0;
507 }
508
509 return -EINVAL;
510}
511
Lina Iyer01ffea22016-07-27 13:46:32 -0600512static int of_thermal_aggregate_trip_types(struct thermal_zone_device *tz,
513 unsigned int trip_type_mask, int *low, int *high)
514{
515 int min = INT_MIN;
516 int max = INT_MAX;
517 int tt, th, trip;
518 int temp = tz->temperature;
519 struct thermal_zone_device *zone = NULL;
520 struct __thermal_zone *data = tz->devdata;
521 struct list_head *head;
522 enum thermal_trip_type type = 0;
523
524 head = &data->senps->first_tz;
Ram Chandrasekar9a1da902017-05-09 16:58:55 -0600525 list_for_each_entry(data, head, list) {
Lina Iyer01ffea22016-07-27 13:46:32 -0600526 zone = data->tzd;
Ram Chandrasekar37c816d2017-08-25 12:34:02 -0600527 if (data->mode == THERMAL_DEVICE_DISABLED)
528 continue;
Lina Iyer01ffea22016-07-27 13:46:32 -0600529 for (trip = 0; trip < data->ntrips; trip++) {
530 of_thermal_get_trip_type(zone, trip, &type);
531 if (!(BIT(type) & trip_type_mask))
532 continue;
533
534 if (!zone->tzp->tracks_low) {
535 tt = data->trips[trip].temperature;
536 if (tt > temp && tt < max)
537 max = tt;
538 th = tt - data->trips[trip].hysteresis;
539 if (th < temp && th > min)
540 min = th;
541 } else {
542 tt = data->trips[trip].temperature;
543 if (tt < temp && tt > min)
544 min = tt;
545 th = tt + data->trips[trip].hysteresis;
546 if (th > temp && th < max)
547 max = th;
548 }
549 }
550 }
551
552 *high = max;
553 *low = min;
554
555 return 0;
556}
557
558/*
559 * of_thermal_aggregate_trip - aggregate trip temperatures across sibling
560 * thermal zones.
561 * @tz: pointer to the primary thermal zone.
562 * @type: the thermal trip type to be aggregated upon
563 * @low: the low trip threshold which the most lesser than the @temp
564 * @high: the high trip threshold which is the least greater than the @temp
565 */
566int of_thermal_aggregate_trip(struct thermal_zone_device *tz,
567 enum thermal_trip_type type,
568 int *low, int *high)
569{
570 if (type <= THERMAL_TRIP_CRITICAL)
571 return of_thermal_aggregate_trip_types(tz, BIT(type), low,
572 high);
573
574 return -EINVAL;
575}
576EXPORT_SYMBOL(of_thermal_aggregate_trip);
577
Ram Chandrasekarcf543602017-03-13 22:59:01 -0600578/*
579 * of_thermal_handle_trip - Handle thermal trip from sensors
580 *
581 * @tz: pointer to the primary thermal zone.
582 */
583void of_thermal_handle_trip(struct thermal_zone_device *tz)
584{
585 struct thermal_zone_device *zone;
586 struct __thermal_zone *data = tz->devdata;
587 struct list_head *head;
588
589 head = &data->senps->first_tz;
Ram Chandrasekar9a1da902017-05-09 16:58:55 -0600590 list_for_each_entry(data, head, list) {
Ram Chandrasekarcf543602017-03-13 22:59:01 -0600591 zone = data->tzd;
Ram Chandrasekar37c816d2017-08-25 12:34:02 -0600592 if (data->mode == THERMAL_DEVICE_DISABLED)
593 continue;
Ram Chandrasekarcf543602017-03-13 22:59:01 -0600594 thermal_zone_device_update(zone, THERMAL_EVENT_UNSPECIFIED);
595 }
596}
597EXPORT_SYMBOL(of_thermal_handle_trip);
598
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400599static struct thermal_zone_device_ops of_thermal_ops = {
600 .get_mode = of_thermal_get_mode,
601 .set_mode = of_thermal_set_mode,
602
603 .get_trip_type = of_thermal_get_trip_type,
604 .get_trip_temp = of_thermal_get_trip_temp,
605 .set_trip_temp = of_thermal_set_trip_temp,
606 .get_trip_hyst = of_thermal_get_trip_hyst,
607 .set_trip_hyst = of_thermal_set_trip_hyst,
608 .get_crit_temp = of_thermal_get_crit_temp,
609
610 .bind = of_thermal_bind,
611 .unbind = of_thermal_unbind,
612};
613
Ram Chandrasekar2f2b7462017-03-11 19:35:11 -0700614static struct thermal_zone_of_device_ops of_virt_ops = {
615 .get_temp = virt_sensor_read_temp,
616};
617
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400618/*** sensor API ***/
619
620static struct thermal_zone_device *
621thermal_zone_of_add_sensor(struct device_node *zone,
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700622 struct device_node *sensor,
623 struct __sensor_param *sens_param)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400624{
625 struct thermal_zone_device *tzd;
626 struct __thermal_zone *tz;
627
628 tzd = thermal_zone_get_zone_by_name(zone->name);
629 if (IS_ERR(tzd))
630 return ERR_PTR(-EPROBE_DEFER);
631
632 tz = tzd->devdata;
633
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700634 if (!sens_param->ops)
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400635 return ERR_PTR(-EINVAL);
636
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400637 mutex_lock(&tzd->lock);
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700638 tz->senps = sens_param;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400639
640 tzd->ops->get_temp = of_thermal_get_temp;
641 tzd->ops->get_trend = of_thermal_get_trend;
Sascha Hauer826386e2016-06-22 16:42:02 +0800642
643 /*
644 * The thermal zone core will calculate the window if they have set the
645 * optional set_trips pointer.
646 */
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700647 if (sens_param->ops->set_trips)
Sascha Hauer826386e2016-06-22 16:42:02 +0800648 tzd->ops->set_trips = of_thermal_set_trips;
649
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700650 if (sens_param->ops->set_emul_temp)
Keerthye2fa7482016-06-02 14:24:50 +0530651 tzd->ops->set_emul_temp = of_thermal_set_emul_temp;
652
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700653 list_add_tail(&tz->list, &sens_param->first_tz);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400654 mutex_unlock(&tzd->lock);
655
656 return tzd;
657}
658
659/**
660 * thermal_zone_of_sensor_register - registers a sensor to a DT thermal zone
661 * @dev: a valid struct device pointer of a sensor device. Must contain
662 * a valid .of_node, for the sensor node.
663 * @sensor_id: a sensor identifier, in case the sensor IP has more
664 * than one sensors
665 * @data: a private pointer (owned by the caller) that will be passed
666 * back, when a temperature reading is needed.
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400667 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400668 *
669 * This function will search the list of thermal zones described in device
670 * tree and look for the zone that refer to the sensor device pointed by
671 * @dev->of_node as temperature providers. For the zone pointing to the
672 * sensor node, the sensor will be added to the DT thermal zone device.
673 *
674 * The thermal zone temperature is provided by the @get_temp function
675 * pointer. When called, it will have the private pointer @data back.
676 *
677 * The thermal zone temperature trend is provided by the @get_trend function
678 * pointer. When called, it will have the private pointer @data back.
679 *
680 * TODO:
681 * 01 - This function must enqueue the new sensor instead of using
682 * it as the only source of temperature values.
683 *
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400684 * Return: On success returns a valid struct thermal_zone_device,
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700685 * otherwise, it returns a corresponding ERR_PTR(). Incase there are multiple
686 * thermal zones referencing the same sensor, the return value will be
687 * thermal_zone_device pointer of the first thermal zone. Caller must
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400688 * check the return value with help of IS_ERR() helper.
689 */
690struct thermal_zone_device *
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400691thermal_zone_of_sensor_register(struct device *dev, int sensor_id, void *data,
692 const struct thermal_zone_of_device_ops *ops)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400693{
694 struct device_node *np, *child, *sensor_np;
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300695 struct thermal_zone_device *tzd = ERR_PTR(-ENODEV);
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700696 struct thermal_zone_device *first_tzd = NULL;
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700697 struct __sensor_param *sens_param = NULL;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400698
699 np = of_find_node_by_name(NULL, "thermal-zones");
700 if (!np)
701 return ERR_PTR(-ENODEV);
702
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300703 if (!dev || !dev->of_node) {
704 of_node_put(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400705 return ERR_PTR(-EINVAL);
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300706 }
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400707
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700708 sens_param = kzalloc(sizeof(*sens_param), GFP_KERNEL);
709 if (!sens_param) {
710 of_node_put(np);
711 return ERR_PTR(-ENOMEM);
712 }
713 sens_param->sensor_data = data;
714 sens_param->ops = ops;
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700715 INIT_LIST_HEAD(&sens_param->first_tz);
Lina Iyer01ffea22016-07-27 13:46:32 -0600716 sens_param->trip_high = INT_MAX;
717 sens_param->trip_low = INT_MIN;
718 mutex_init(&sens_param->lock);
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300719 sensor_np = of_node_get(dev->of_node);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400720
Laxman Dewangan42bbe402016-02-08 18:58:34 +0530721 for_each_available_child_of_node(np, child) {
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400722 struct of_phandle_args sensor_specs;
723 int ret, id;
Ram Chandrasekar07479402017-08-25 14:04:42 -0600724 struct __thermal_zone *tz;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400725
726 /* For now, thermal framework supports only 1 sensor per zone */
727 ret = of_parse_phandle_with_args(child, "thermal-sensors",
728 "#thermal-sensor-cells",
729 0, &sensor_specs);
730 if (ret)
731 continue;
732
733 if (sensor_specs.args_count >= 1) {
734 id = sensor_specs.args[0];
735 WARN(sensor_specs.args_count > 1,
736 "%s: too many cells in sensor specifier %d\n",
737 sensor_specs.np->name, sensor_specs.args_count);
738 } else {
739 id = 0;
740 }
741
742 if (sensor_specs.np == sensor_np && id == sensor_id) {
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300743 tzd = thermal_zone_of_add_sensor(child, sensor_np,
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700744 sens_param);
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700745 if (!IS_ERR(tzd)) {
746 if (!first_tzd)
747 first_tzd = tzd;
Ram Chandrasekar07479402017-08-25 14:04:42 -0600748 tz = tzd->devdata;
749 if (!tz->default_disable)
750 tzd->ops->set_mode(tzd,
751 THERMAL_DEVICE_ENABLED);
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700752 }
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400753 }
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300754 of_node_put(sensor_specs.np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400755 }
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300756 of_node_put(sensor_np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400757 of_node_put(np);
758
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700759 if (!first_tzd) {
760 first_tzd = ERR_PTR(-ENODEV);
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700761 kfree(sens_param);
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700762 }
763 return first_tzd;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400764}
765EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_register);
766
767/**
768 * thermal_zone_of_sensor_unregister - unregisters a sensor from a DT thermal zone
769 * @dev: a valid struct device pointer of a sensor device. Must contain
770 * a valid .of_node, for the sensor node.
771 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
772 *
773 * This function removes the sensor callbacks and private data from the
774 * thermal zone device registered with thermal_zone_of_sensor_register()
775 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
776 * thermal zone device callbacks.
777 *
778 * TODO: When the support to several sensors per zone is added, this
779 * function must search the sensor list based on @dev parameter.
780 *
781 */
782void thermal_zone_of_sensor_unregister(struct device *dev,
783 struct thermal_zone_device *tzd)
784{
Ram Chandrasekar9a1da902017-05-09 16:58:55 -0600785 struct __thermal_zone *tz, *next;
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700786 struct thermal_zone_device *pos;
787 struct list_head *head;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400788
789 if (!dev || !tzd || !tzd->devdata)
790 return;
791
792 tz = tzd->devdata;
793
794 /* no __thermal_zone, nothing to be done */
795 if (!tz)
796 return;
797
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700798 head = &tz->senps->first_tz;
Ram Chandrasekar9a1da902017-05-09 16:58:55 -0600799 list_for_each_entry_safe(tz, next, head, list) {
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700800 pos = tz->tzd;
801 mutex_lock(&pos->lock);
802 pos->ops->get_temp = NULL;
803 pos->ops->get_trend = NULL;
804 pos->ops->set_emul_temp = NULL;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400805
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700806 list_del(&tz->list);
807 if (list_empty(&tz->senps->first_tz))
808 kfree(tz->senps);
809 tz->senps = NULL;
810 mutex_unlock(&pos->lock);
811 }
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400812}
813EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister);
814
Laxman Dewangane498b492016-03-09 18:40:06 +0530815static void devm_thermal_zone_of_sensor_release(struct device *dev, void *res)
816{
817 thermal_zone_of_sensor_unregister(dev,
818 *(struct thermal_zone_device **)res);
819}
820
821static int devm_thermal_zone_of_sensor_match(struct device *dev, void *res,
822 void *data)
823{
824 struct thermal_zone_device **r = res;
825
826 if (WARN_ON(!r || !*r))
827 return 0;
828
829 return *r == data;
830}
831
832/**
Ram Chandrasekar2f2b7462017-03-11 19:35:11 -0700833 * devm_thermal_of_virtual_sensor_register - Register a virtual sensor.
834 * Three types of virtual sensors are supported.
835 * 1. Weighted aggregation type:
836 * Virtual sensor of this type calculates the weighted aggregation
837 * of sensor temperatures using the below formula,
838 * temp = (sensor_1_temp * coeff_1 + ... + sensor_n_temp * coeff_n)
839 * + avg_offset / avg_denominator
840 * So the sensor drivers has to specify n+2 coefficients.
841 * 2. Maximum type:
842 * Virtual sensors of this type will report the maximum of all
843 * sensor temperatures.
844 * 3. Minimum type:
845 * Virtual sensors of this type will report the minimum of all
846 * sensor temperatures.
847 *
848 * @input arguments:
849 * @dev: Virtual sensor driver device pointer.
850 * @sensor_data: Virtual sensor data supported for the device.
851 *
852 * @return: Returns a virtual thermal zone pointer. Returns error if thermal
853 * zone is not created. Returns -EAGAIN, if the sensor that is required for
854 * this virtual sensor temperature estimation is not registered yet. The
855 * sensor driver can try again later.
856 */
857struct thermal_zone_device *devm_thermal_of_virtual_sensor_register(
858 struct device *dev,
859 const struct virtual_sensor_data *sensor_data)
860{
861 int sens_idx = 0;
862 struct virtual_sensor *sens;
863 struct __thermal_zone *tz;
864 struct thermal_zone_device **ptr;
865 struct thermal_zone_device *tzd;
866 struct __sensor_param *sens_param = NULL;
867 enum thermal_device_mode mode;
868
869 if (!dev || !sensor_data)
870 return ERR_PTR(-EINVAL);
871
872 tzd = thermal_zone_get_zone_by_name(
873 sensor_data->virt_zone_name);
874 if (IS_ERR(tzd)) {
Manaf Meethalavalappu Pallikunhi3d62c3f2017-08-23 21:39:35 +0530875 dev_dbg(dev, "sens:%s not available err: %ld\n",
Ram Chandrasekar2f2b7462017-03-11 19:35:11 -0700876 sensor_data->virt_zone_name,
877 PTR_ERR(tzd));
878 return tzd;
879 }
880
881 mutex_lock(&tzd->lock);
882 /*
883 * Check if the virtual zone is registered and enabled.
884 * If so return the registered thermal zone.
885 */
886 tzd->ops->get_mode(tzd, &mode);
887 mutex_unlock(&tzd->lock);
888 if (mode == THERMAL_DEVICE_ENABLED)
889 return tzd;
890
891 sens = devm_kzalloc(dev, sizeof(*sens), GFP_KERNEL);
892 if (!sens)
893 return ERR_PTR(-ENOMEM);
894
Ram Chandrasekar02592fa2017-05-16 17:03:02 -0600895 sens->virt_tz = tzd;
Ram Chandrasekar2f2b7462017-03-11 19:35:11 -0700896 sens->logic = sensor_data->logic;
897 sens->num_sensors = sensor_data->num_sensors;
898 if (sens->logic == VIRT_WEIGHTED_AVG) {
899 int coeff_ct = sensor_data->coefficient_ct;
900
901 /*
902 * For weighted aggregation, sensor drivers has to specify
903 * n+2 coefficients.
904 */
905 if (coeff_ct != sens->num_sensors) {
906 dev_err(dev, "sens:%s Invalid coefficient\n",
907 sensor_data->virt_zone_name);
908 return ERR_PTR(-EINVAL);
909 }
910 memcpy(sens->coefficients, sensor_data->coefficients,
911 coeff_ct * sizeof(*sens->coefficients));
912 sens->avg_offset = sensor_data->avg_offset;
913 sens->avg_denominator = sensor_data->avg_denominator;
914 }
915
916 for (sens_idx = 0; sens_idx < sens->num_sensors; sens_idx++) {
917 sens->tz[sens_idx] = thermal_zone_get_zone_by_name(
918 sensor_data->sensor_names[sens_idx]);
919 if (IS_ERR(sens->tz[sens_idx])) {
920 dev_err(dev, "sens:%s sensor[%s] fetch err:%ld\n",
921 sensor_data->virt_zone_name,
922 sensor_data->sensor_names[sens_idx],
923 PTR_ERR(sens->tz[sens_idx]));
924 break;
925 }
926 }
927 if (sens->num_sensors != sens_idx)
928 return ERR_PTR(-EAGAIN);
929
930 sens_param = kzalloc(sizeof(*sens_param), GFP_KERNEL);
931 if (!sens_param)
932 return ERR_PTR(-ENOMEM);
933 sens_param->sensor_data = sens;
934 sens_param->ops = &of_virt_ops;
935 INIT_LIST_HEAD(&sens_param->first_tz);
936 sens_param->trip_high = INT_MAX;
937 sens_param->trip_low = INT_MIN;
938 mutex_init(&sens_param->lock);
939
940 mutex_lock(&tzd->lock);
941 tz = tzd->devdata;
942 tz->senps = sens_param;
943 tzd->ops->get_temp = of_thermal_get_temp;
944 list_add_tail(&tz->list, &sens_param->first_tz);
945 mutex_unlock(&tzd->lock);
946
947 ptr = devres_alloc(devm_thermal_zone_of_sensor_release, sizeof(*ptr),
948 GFP_KERNEL);
949 if (!ptr)
950 return ERR_PTR(-ENOMEM);
951
952 *ptr = tzd;
953 devres_add(dev, ptr);
954
Ram Chandrasekar07479402017-08-25 14:04:42 -0600955 if (!tz->default_disable)
956 tzd->ops->set_mode(tzd, THERMAL_DEVICE_ENABLED);
Ram Chandrasekar2f2b7462017-03-11 19:35:11 -0700957
958 return tzd;
959}
960EXPORT_SYMBOL(devm_thermal_of_virtual_sensor_register);
961
962/**
Laxman Dewangane498b492016-03-09 18:40:06 +0530963 * devm_thermal_zone_of_sensor_register - Resource managed version of
964 * thermal_zone_of_sensor_register()
965 * @dev: a valid struct device pointer of a sensor device. Must contain
966 * a valid .of_node, for the sensor node.
967 * @sensor_id: a sensor identifier, in case the sensor IP has more
968 * than one sensors
969 * @data: a private pointer (owned by the caller) that will be passed
970 * back, when a temperature reading is needed.
971 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
972 *
973 * Refer thermal_zone_of_sensor_register() for more details.
974 *
975 * Return: On success returns a valid struct thermal_zone_device,
976 * otherwise, it returns a corresponding ERR_PTR(). Caller must
977 * check the return value with help of IS_ERR() helper.
Zhang Rui7b5c4a02016-08-22 15:48:11 +0800978 * Registered thermal_zone_device device will automatically be
Laxman Dewangane498b492016-03-09 18:40:06 +0530979 * released when device is unbounded.
980 */
981struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
982 struct device *dev, int sensor_id,
983 void *data, const struct thermal_zone_of_device_ops *ops)
984{
985 struct thermal_zone_device **ptr, *tzd;
986
987 ptr = devres_alloc(devm_thermal_zone_of_sensor_release, sizeof(*ptr),
988 GFP_KERNEL);
989 if (!ptr)
990 return ERR_PTR(-ENOMEM);
991
992 tzd = thermal_zone_of_sensor_register(dev, sensor_id, data, ops);
993 if (IS_ERR(tzd)) {
994 devres_free(ptr);
995 return tzd;
996 }
997
998 *ptr = tzd;
999 devres_add(dev, ptr);
1000
1001 return tzd;
1002}
1003EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_register);
1004
1005/**
1006 * devm_thermal_zone_of_sensor_unregister - Resource managed version of
1007 * thermal_zone_of_sensor_unregister().
1008 * @dev: Device for which which resource was allocated.
1009 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
1010 *
1011 * This function removes the sensor callbacks and private data from the
1012 * thermal zone device registered with devm_thermal_zone_of_sensor_register()
1013 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
1014 * thermal zone device callbacks.
1015 * Normally this function will not need to be called and the resource
1016 * management code will ensure that the resource is freed.
1017 */
1018void devm_thermal_zone_of_sensor_unregister(struct device *dev,
1019 struct thermal_zone_device *tzd)
1020{
1021 WARN_ON(devres_release(dev, devm_thermal_zone_of_sensor_release,
1022 devm_thermal_zone_of_sensor_match, tzd));
1023}
1024EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_unregister);
1025
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001026/*** functions parsing device tree nodes ***/
1027
1028/**
1029 * thermal_of_populate_bind_params - parse and fill cooling map data
1030 * @np: DT node containing a cooling-map node
1031 * @__tbp: data structure to be filled with cooling map info
1032 * @trips: array of thermal zone trip points
1033 * @ntrips: number of trip points inside trips.
1034 *
1035 * This function parses a cooling-map type of node represented by
1036 * @np parameter and fills the read data into @__tbp data structure.
1037 * It needs the already parsed array of trip points of the thermal zone
1038 * in consideration.
1039 *
1040 * Return: 0 on success, proper error code otherwise
1041 */
1042static int thermal_of_populate_bind_params(struct device_node *np,
1043 struct __thermal_bind_params *__tbp,
Lukasz Majewskiad9914a2014-12-08 18:04:19 +01001044 struct thermal_trip *trips,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001045 int ntrips)
1046{
1047 struct of_phandle_args cooling_spec;
1048 struct device_node *trip;
1049 int ret, i;
1050 u32 prop;
1051
1052 /* Default weight. Usage is optional */
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +00001053 __tbp->usage = THERMAL_WEIGHT_DEFAULT;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001054 ret = of_property_read_u32(np, "contribution", &prop);
1055 if (ret == 0)
1056 __tbp->usage = prop;
1057
1058 trip = of_parse_phandle(np, "trip", 0);
1059 if (!trip) {
1060 pr_err("missing trip property\n");
1061 return -ENODEV;
1062 }
1063
1064 /* match using device_node */
1065 for (i = 0; i < ntrips; i++)
1066 if (trip == trips[i].np) {
1067 __tbp->trip_id = i;
1068 break;
1069 }
1070
1071 if (i == ntrips) {
1072 ret = -ENODEV;
1073 goto end;
1074 }
1075
1076 ret = of_parse_phandle_with_args(np, "cooling-device", "#cooling-cells",
1077 0, &cooling_spec);
1078 if (ret < 0) {
1079 pr_err("missing cooling_device property\n");
1080 goto end;
1081 }
1082 __tbp->cooling_device = cooling_spec.np;
1083 if (cooling_spec.args_count >= 2) { /* at least min and max */
1084 __tbp->min = cooling_spec.args[0];
1085 __tbp->max = cooling_spec.args[1];
1086 } else {
1087 pr_err("wrong reference to cooling device, missing limits\n");
1088 }
1089
1090end:
1091 of_node_put(trip);
1092
1093 return ret;
1094}
1095
1096/**
1097 * It maps 'enum thermal_trip_type' found in include/linux/thermal.h
1098 * into the device tree binding of 'trip', property type.
1099 */
1100static const char * const trip_types[] = {
1101 [THERMAL_TRIP_ACTIVE] = "active",
1102 [THERMAL_TRIP_PASSIVE] = "passive",
1103 [THERMAL_TRIP_HOT] = "hot",
1104 [THERMAL_TRIP_CRITICAL] = "critical",
1105};
1106
1107/**
1108 * thermal_of_get_trip_type - Get phy mode for given device_node
1109 * @np: Pointer to the given device_node
1110 * @type: Pointer to resulting trip type
1111 *
1112 * The function gets trip type string from property 'type',
1113 * and store its index in trip_types table in @type,
1114 *
1115 * Return: 0 on success, or errno in error case.
1116 */
1117static int thermal_of_get_trip_type(struct device_node *np,
1118 enum thermal_trip_type *type)
1119{
1120 const char *t;
1121 int err, i;
1122
1123 err = of_property_read_string(np, "type", &t);
1124 if (err < 0)
1125 return err;
1126
1127 for (i = 0; i < ARRAY_SIZE(trip_types); i++)
1128 if (!strcasecmp(t, trip_types[i])) {
1129 *type = i;
1130 return 0;
1131 }
1132
1133 return -ENODEV;
1134}
1135
1136/**
1137 * thermal_of_populate_trip - parse and fill one trip point data
1138 * @np: DT node containing a trip point node
1139 * @trip: trip point data structure to be filled up
1140 *
1141 * This function parses a trip point type of node represented by
1142 * @np parameter and fills the read data into @trip data structure.
1143 *
1144 * Return: 0 on success, proper error code otherwise
1145 */
1146static int thermal_of_populate_trip(struct device_node *np,
Lukasz Majewskiad9914a2014-12-08 18:04:19 +01001147 struct thermal_trip *trip)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001148{
1149 int prop;
1150 int ret;
1151
1152 ret = of_property_read_u32(np, "temperature", &prop);
1153 if (ret < 0) {
1154 pr_err("missing temperature property\n");
1155 return ret;
1156 }
1157 trip->temperature = prop;
1158
1159 ret = of_property_read_u32(np, "hysteresis", &prop);
1160 if (ret < 0) {
1161 pr_err("missing hysteresis property\n");
1162 return ret;
1163 }
1164 trip->hysteresis = prop;
1165
1166 ret = thermal_of_get_trip_type(np, &trip->type);
1167 if (ret < 0) {
1168 pr_err("wrong trip type property\n");
1169 return ret;
1170 }
1171
1172 /* Required for cooling map matching */
1173 trip->np = np;
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001174 of_node_get(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001175
1176 return 0;
1177}
1178
1179/**
1180 * thermal_of_build_thermal_zone - parse and fill one thermal zone data
1181 * @np: DT node containing a thermal zone node
1182 *
1183 * This function parses a thermal zone type of node represented by
1184 * @np parameter and fills the read data into a __thermal_zone data structure
1185 * and return this pointer.
1186 *
Eduardo Valentina46dbae2015-05-11 19:48:09 -07001187 * TODO: Missing properties to parse: thermal-sensor-names
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001188 *
1189 * Return: On success returns a valid struct __thermal_zone,
1190 * otherwise, it returns a corresponding ERR_PTR(). Caller must
1191 * check the return value with help of IS_ERR() helper.
1192 */
Julia Lawallc0ff8aa2016-04-19 14:33:32 +02001193static struct __thermal_zone
1194__init *thermal_of_build_thermal_zone(struct device_node *np)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001195{
1196 struct device_node *child = NULL, *gchild;
1197 struct __thermal_zone *tz;
1198 int ret, i;
Eduardo Valentina46dbae2015-05-11 19:48:09 -07001199 u32 prop, coef[2];
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001200
1201 if (!np) {
1202 pr_err("no thermal zone np\n");
1203 return ERR_PTR(-EINVAL);
1204 }
1205
1206 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
1207 if (!tz)
1208 return ERR_PTR(-ENOMEM);
1209
Ram Chandrasekard29230b2017-02-27 11:26:51 -07001210 INIT_LIST_HEAD(&tz->list);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001211 ret = of_property_read_u32(np, "polling-delay-passive", &prop);
1212 if (ret < 0) {
1213 pr_err("missing polling-delay-passive property\n");
1214 goto free_tz;
1215 }
1216 tz->passive_delay = prop;
1217
1218 ret = of_property_read_u32(np, "polling-delay", &prop);
1219 if (ret < 0) {
1220 pr_err("missing polling-delay property\n");
1221 goto free_tz;
1222 }
1223 tz->polling_delay = prop;
1224
Ram Chandrasekar07479402017-08-25 14:04:42 -06001225 tz->default_disable = of_property_read_bool(np,
1226 "disable-thermal-zone");
Eduardo Valentina46dbae2015-05-11 19:48:09 -07001227 /*
1228 * REVIST: for now, the thermal framework supports only
1229 * one sensor per thermal zone. Thus, we are considering
1230 * only the first two values as slope and offset.
1231 */
1232 ret = of_property_read_u32_array(np, "coefficients", coef, 2);
1233 if (ret == 0) {
1234 tz->slope = coef[0];
1235 tz->offset = coef[1];
1236 } else {
1237 tz->slope = 1;
1238 tz->offset = 0;
1239 }
1240
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001241 /* trips */
1242 child = of_get_child_by_name(np, "trips");
1243
1244 /* No trips provided */
1245 if (!child)
1246 goto finish;
1247
1248 tz->ntrips = of_get_child_count(child);
1249 if (tz->ntrips == 0) /* must have at least one child */
1250 goto finish;
1251
1252 tz->trips = kzalloc(tz->ntrips * sizeof(*tz->trips), GFP_KERNEL);
1253 if (!tz->trips) {
1254 ret = -ENOMEM;
1255 goto free_tz;
1256 }
1257
1258 i = 0;
1259 for_each_child_of_node(child, gchild) {
1260 ret = thermal_of_populate_trip(gchild, &tz->trips[i++]);
1261 if (ret)
1262 goto free_trips;
1263 }
1264
1265 of_node_put(child);
1266
1267 /* cooling-maps */
1268 child = of_get_child_by_name(np, "cooling-maps");
1269
1270 /* cooling-maps not provided */
1271 if (!child)
1272 goto finish;
1273
1274 tz->num_tbps = of_get_child_count(child);
1275 if (tz->num_tbps == 0)
1276 goto finish;
1277
1278 tz->tbps = kzalloc(tz->num_tbps * sizeof(*tz->tbps), GFP_KERNEL);
1279 if (!tz->tbps) {
1280 ret = -ENOMEM;
1281 goto free_trips;
1282 }
1283
1284 i = 0;
Stephen Boydca9521b2014-06-18 16:32:08 -07001285 for_each_child_of_node(child, gchild) {
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001286 ret = thermal_of_populate_bind_params(gchild, &tz->tbps[i++],
1287 tz->trips, tz->ntrips);
1288 if (ret)
1289 goto free_tbps;
Stephen Boydca9521b2014-06-18 16:32:08 -07001290 }
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001291
1292finish:
1293 of_node_put(child);
1294 tz->mode = THERMAL_DEVICE_DISABLED;
1295
1296 return tz;
1297
1298free_tbps:
Ulises Brindis1cd91c12016-03-25 12:55:41 -07001299 for (i = i - 1; i >= 0; i--)
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001300 of_node_put(tz->tbps[i].cooling_device);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001301 kfree(tz->tbps);
1302free_trips:
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001303 for (i = 0; i < tz->ntrips; i++)
1304 of_node_put(tz->trips[i].np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001305 kfree(tz->trips);
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001306 of_node_put(gchild);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001307free_tz:
1308 kfree(tz);
1309 of_node_put(child);
1310
1311 return ERR_PTR(ret);
1312}
1313
1314static inline void of_thermal_free_zone(struct __thermal_zone *tz)
1315{
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001316 int i;
1317
1318 for (i = 0; i < tz->num_tbps; i++)
1319 of_node_put(tz->tbps[i].cooling_device);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001320 kfree(tz->tbps);
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001321 for (i = 0; i < tz->ntrips; i++)
1322 of_node_put(tz->trips[i].np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001323 kfree(tz->trips);
1324 kfree(tz);
1325}
1326
1327/**
1328 * of_parse_thermal_zones - parse device tree thermal data
1329 *
1330 * Initialization function that can be called by machine initialization
1331 * code to parse thermal data and populate the thermal framework
1332 * with hardware thermal zones info. This function only parses thermal zones.
1333 * Cooling devices and sensor devices nodes are supposed to be parsed
1334 * by their respective drivers.
1335 *
1336 * Return: 0 on success, proper error code otherwise
1337 *
1338 */
1339int __init of_parse_thermal_zones(void)
1340{
1341 struct device_node *np, *child;
1342 struct __thermal_zone *tz;
1343 struct thermal_zone_device_ops *ops;
1344
1345 np = of_find_node_by_name(NULL, "thermal-zones");
1346 if (!np) {
1347 pr_debug("unable to find thermal zones\n");
1348 return 0; /* Run successfully on systems without thermal DT */
1349 }
1350
Laxman Dewangan42bbe402016-02-08 18:58:34 +05301351 for_each_available_child_of_node(np, child) {
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001352 struct thermal_zone_device *zone;
1353 struct thermal_zone_params *tzp;
Punit Agrawal76af5492015-03-03 10:43:04 +00001354 int i, mask = 0;
Punit Agrawal647f9922015-02-26 19:00:32 +00001355 u32 prop;
Ram Chandrasekarcaafe202016-07-19 11:25:46 -06001356 const char *governor_name;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001357
1358 tz = thermal_of_build_thermal_zone(child);
1359 if (IS_ERR(tz)) {
1360 pr_err("failed to build thermal zone %s: %ld\n",
1361 child->name,
1362 PTR_ERR(tz));
1363 continue;
1364 }
1365
1366 ops = kmemdup(&of_thermal_ops, sizeof(*ops), GFP_KERNEL);
1367 if (!ops)
1368 goto exit_free;
1369
1370 tzp = kzalloc(sizeof(*tzp), GFP_KERNEL);
1371 if (!tzp) {
1372 kfree(ops);
1373 goto exit_free;
1374 }
1375
1376 /* No hwmon because there might be hwmon drivers registering */
1377 tzp->no_hwmon = true;
1378
Ram Chandrasekarcaafe202016-07-19 11:25:46 -06001379 if (!of_property_read_string(child, "thermal-governor",
1380 &governor_name))
1381 strlcpy(tzp->governor_name, governor_name,
1382 THERMAL_NAME_LENGTH);
1383
Punit Agrawal647f9922015-02-26 19:00:32 +00001384 if (!of_property_read_u32(child, "sustainable-power", &prop))
1385 tzp->sustainable_power = prop;
1386
Punit Agrawal76af5492015-03-03 10:43:04 +00001387 for (i = 0; i < tz->ntrips; i++)
1388 mask |= 1 << i;
1389
Eduardo Valentina46dbae2015-05-11 19:48:09 -07001390 /* these two are left for temperature drivers to use */
1391 tzp->slope = tz->slope;
1392 tzp->offset = tz->offset;
1393
Lina Iyer159f67d2016-07-27 11:34:46 -06001394 if (of_property_read_bool(child, "tracks-low"))
1395 tzp->tracks_low = true;
1396
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001397 zone = thermal_zone_device_register(child->name, tz->ntrips,
Punit Agrawal76af5492015-03-03 10:43:04 +00001398 mask, tz,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001399 ops, tzp,
1400 tz->passive_delay,
1401 tz->polling_delay);
1402 if (IS_ERR(zone)) {
1403 pr_err("Failed to build %s zone %ld\n", child->name,
1404 PTR_ERR(zone));
1405 kfree(tzp);
1406 kfree(ops);
1407 of_thermal_free_zone(tz);
1408 /* attempting to build remaining zones still */
Ram Chandrasekard29230b2017-02-27 11:26:51 -07001409 continue;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001410 }
Ram Chandrasekard29230b2017-02-27 11:26:51 -07001411 tz->tzd = zone;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001412 }
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001413 of_node_put(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001414
1415 return 0;
1416
1417exit_free:
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001418 of_node_put(child);
1419 of_node_put(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001420 of_thermal_free_zone(tz);
1421
1422 /* no memory available, so free what we have built */
1423 of_thermal_destroy_zones();
1424
1425 return -ENOMEM;
1426}
1427
1428/**
1429 * of_thermal_destroy_zones - remove all zones parsed and allocated resources
1430 *
1431 * Finds all zones parsed and added to the thermal framework and remove them
1432 * from the system, together with their resources.
1433 *
1434 */
1435void of_thermal_destroy_zones(void)
1436{
1437 struct device_node *np, *child;
1438
1439 np = of_find_node_by_name(NULL, "thermal-zones");
1440 if (!np) {
Jiada Wang28524982015-11-16 17:10:05 +09001441 pr_debug("unable to find thermal zones\n");
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001442 return;
1443 }
1444
Laxman Dewangan42bbe402016-02-08 18:58:34 +05301445 for_each_available_child_of_node(np, child) {
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001446 struct thermal_zone_device *zone;
1447
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001448 zone = thermal_zone_get_zone_by_name(child->name);
1449 if (IS_ERR(zone))
1450 continue;
1451
1452 thermal_zone_device_unregister(zone);
1453 kfree(zone->tzp);
1454 kfree(zone->ops);
1455 of_thermal_free_zone(zone->devdata);
1456 }
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001457 of_node_put(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001458}