blob: bb5f70645a20370cff66b4ac1c257ecb084383ba [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
433 *temp = data->trips[trip].temperature;
434
435 return 0;
436}
437
438static int of_thermal_set_trip_temp(struct thermal_zone_device *tz, int trip,
Sascha Hauer17e83512015-07-24 08:12:54 +0200439 int temp)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400440{
441 struct __thermal_zone *data = tz->devdata;
442
443 if (trip >= data->ntrips || trip < 0)
444 return -EDOM;
445
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700446 if (data->senps && data->senps->ops->set_trip_temp) {
Wei Nic3509522016-03-29 18:29:17 +0800447 int ret;
448
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700449 ret = data->senps->ops->set_trip_temp(data->senps->sensor_data,
450 trip, temp);
Wei Nic3509522016-03-29 18:29:17 +0800451 if (ret)
452 return ret;
453 }
454
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400455 /* thermal framework should take care of data->mask & (1 << trip) */
456 data->trips[trip].temperature = temp;
457
458 return 0;
459}
460
461static int of_thermal_get_trip_hyst(struct thermal_zone_device *tz, int trip,
Sascha Hauer17e83512015-07-24 08:12:54 +0200462 int *hyst)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400463{
464 struct __thermal_zone *data = tz->devdata;
465
466 if (trip >= data->ntrips || trip < 0)
467 return -EDOM;
468
469 *hyst = data->trips[trip].hysteresis;
470
471 return 0;
472}
473
474static int of_thermal_set_trip_hyst(struct thermal_zone_device *tz, int trip,
Sascha Hauer17e83512015-07-24 08:12:54 +0200475 int hyst)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400476{
477 struct __thermal_zone *data = tz->devdata;
478
479 if (trip >= data->ntrips || trip < 0)
480 return -EDOM;
481
482 /* thermal framework should take care of data->mask & (1 << trip) */
483 data->trips[trip].hysteresis = hyst;
484
485 return 0;
486}
487
488static int of_thermal_get_crit_temp(struct thermal_zone_device *tz,
Sascha Hauer17e83512015-07-24 08:12:54 +0200489 int *temp)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400490{
491 struct __thermal_zone *data = tz->devdata;
492 int i;
493
494 for (i = 0; i < data->ntrips; i++)
495 if (data->trips[i].type == THERMAL_TRIP_CRITICAL) {
496 *temp = data->trips[i].temperature;
497 return 0;
498 }
499
500 return -EINVAL;
501}
502
Lina Iyer01ffea22016-07-27 13:46:32 -0600503static int of_thermal_aggregate_trip_types(struct thermal_zone_device *tz,
504 unsigned int trip_type_mask, int *low, int *high)
505{
506 int min = INT_MIN;
507 int max = INT_MAX;
508 int tt, th, trip;
509 int temp = tz->temperature;
510 struct thermal_zone_device *zone = NULL;
511 struct __thermal_zone *data = tz->devdata;
512 struct list_head *head;
513 enum thermal_trip_type type = 0;
514
515 head = &data->senps->first_tz;
Ram Chandrasekar9a1da902017-05-09 16:58:55 -0600516 list_for_each_entry(data, head, list) {
Lina Iyer01ffea22016-07-27 13:46:32 -0600517 zone = data->tzd;
Ram Chandrasekar37c816d2017-08-25 12:34:02 -0600518 if (data->mode == THERMAL_DEVICE_DISABLED)
519 continue;
Lina Iyer01ffea22016-07-27 13:46:32 -0600520 for (trip = 0; trip < data->ntrips; trip++) {
521 of_thermal_get_trip_type(zone, trip, &type);
522 if (!(BIT(type) & trip_type_mask))
523 continue;
524
525 if (!zone->tzp->tracks_low) {
526 tt = data->trips[trip].temperature;
527 if (tt > temp && tt < max)
528 max = tt;
529 th = tt - data->trips[trip].hysteresis;
530 if (th < temp && th > min)
531 min = th;
532 } else {
533 tt = data->trips[trip].temperature;
534 if (tt < temp && tt > min)
535 min = tt;
536 th = tt + data->trips[trip].hysteresis;
537 if (th > temp && th < max)
538 max = th;
539 }
540 }
541 }
542
543 *high = max;
544 *low = min;
545
546 return 0;
547}
548
549/*
550 * of_thermal_aggregate_trip - aggregate trip temperatures across sibling
551 * thermal zones.
552 * @tz: pointer to the primary thermal zone.
553 * @type: the thermal trip type to be aggregated upon
554 * @low: the low trip threshold which the most lesser than the @temp
555 * @high: the high trip threshold which is the least greater than the @temp
556 */
557int of_thermal_aggregate_trip(struct thermal_zone_device *tz,
558 enum thermal_trip_type type,
559 int *low, int *high)
560{
561 if (type <= THERMAL_TRIP_CRITICAL)
562 return of_thermal_aggregate_trip_types(tz, BIT(type), low,
563 high);
564
565 return -EINVAL;
566}
567EXPORT_SYMBOL(of_thermal_aggregate_trip);
568
Ram Chandrasekarcf543602017-03-13 22:59:01 -0600569/*
570 * of_thermal_handle_trip - Handle thermal trip from sensors
571 *
572 * @tz: pointer to the primary thermal zone.
573 */
574void of_thermal_handle_trip(struct thermal_zone_device *tz)
575{
576 struct thermal_zone_device *zone;
577 struct __thermal_zone *data = tz->devdata;
578 struct list_head *head;
579
580 head = &data->senps->first_tz;
Ram Chandrasekar9a1da902017-05-09 16:58:55 -0600581 list_for_each_entry(data, head, list) {
Ram Chandrasekarcf543602017-03-13 22:59:01 -0600582 zone = data->tzd;
Ram Chandrasekar37c816d2017-08-25 12:34:02 -0600583 if (data->mode == THERMAL_DEVICE_DISABLED)
584 continue;
Ram Chandrasekarcf543602017-03-13 22:59:01 -0600585 thermal_zone_device_update(zone, THERMAL_EVENT_UNSPECIFIED);
586 }
587}
588EXPORT_SYMBOL(of_thermal_handle_trip);
589
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400590static struct thermal_zone_device_ops of_thermal_ops = {
591 .get_mode = of_thermal_get_mode,
592 .set_mode = of_thermal_set_mode,
593
594 .get_trip_type = of_thermal_get_trip_type,
595 .get_trip_temp = of_thermal_get_trip_temp,
596 .set_trip_temp = of_thermal_set_trip_temp,
597 .get_trip_hyst = of_thermal_get_trip_hyst,
598 .set_trip_hyst = of_thermal_set_trip_hyst,
599 .get_crit_temp = of_thermal_get_crit_temp,
600
601 .bind = of_thermal_bind,
602 .unbind = of_thermal_unbind,
603};
604
Ram Chandrasekar2f2b7462017-03-11 19:35:11 -0700605static struct thermal_zone_of_device_ops of_virt_ops = {
606 .get_temp = virt_sensor_read_temp,
607};
608
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400609/*** sensor API ***/
610
611static struct thermal_zone_device *
612thermal_zone_of_add_sensor(struct device_node *zone,
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700613 struct device_node *sensor,
614 struct __sensor_param *sens_param)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400615{
616 struct thermal_zone_device *tzd;
617 struct __thermal_zone *tz;
618
619 tzd = thermal_zone_get_zone_by_name(zone->name);
620 if (IS_ERR(tzd))
621 return ERR_PTR(-EPROBE_DEFER);
622
623 tz = tzd->devdata;
624
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700625 if (!sens_param->ops)
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400626 return ERR_PTR(-EINVAL);
627
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400628 mutex_lock(&tzd->lock);
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700629 tz->senps = sens_param;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400630
631 tzd->ops->get_temp = of_thermal_get_temp;
632 tzd->ops->get_trend = of_thermal_get_trend;
Sascha Hauer826386e2016-06-22 16:42:02 +0800633
634 /*
635 * The thermal zone core will calculate the window if they have set the
636 * optional set_trips pointer.
637 */
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700638 if (sens_param->ops->set_trips)
Sascha Hauer826386e2016-06-22 16:42:02 +0800639 tzd->ops->set_trips = of_thermal_set_trips;
640
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700641 if (sens_param->ops->set_emul_temp)
Keerthye2fa7482016-06-02 14:24:50 +0530642 tzd->ops->set_emul_temp = of_thermal_set_emul_temp;
643
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700644 list_add_tail(&tz->list, &sens_param->first_tz);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400645 mutex_unlock(&tzd->lock);
646
647 return tzd;
648}
649
650/**
651 * thermal_zone_of_sensor_register - registers a sensor to a DT thermal zone
652 * @dev: a valid struct device pointer of a sensor device. Must contain
653 * a valid .of_node, for the sensor node.
654 * @sensor_id: a sensor identifier, in case the sensor IP has more
655 * than one sensors
656 * @data: a private pointer (owned by the caller) that will be passed
657 * back, when a temperature reading is needed.
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400658 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400659 *
660 * This function will search the list of thermal zones described in device
661 * tree and look for the zone that refer to the sensor device pointed by
662 * @dev->of_node as temperature providers. For the zone pointing to the
663 * sensor node, the sensor will be added to the DT thermal zone device.
664 *
665 * The thermal zone temperature is provided by the @get_temp function
666 * pointer. When called, it will have the private pointer @data back.
667 *
668 * The thermal zone temperature trend is provided by the @get_trend function
669 * pointer. When called, it will have the private pointer @data back.
670 *
671 * TODO:
672 * 01 - This function must enqueue the new sensor instead of using
673 * it as the only source of temperature values.
674 *
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400675 * Return: On success returns a valid struct thermal_zone_device,
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700676 * otherwise, it returns a corresponding ERR_PTR(). Incase there are multiple
677 * thermal zones referencing the same sensor, the return value will be
678 * thermal_zone_device pointer of the first thermal zone. Caller must
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400679 * check the return value with help of IS_ERR() helper.
680 */
681struct thermal_zone_device *
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400682thermal_zone_of_sensor_register(struct device *dev, int sensor_id, void *data,
683 const struct thermal_zone_of_device_ops *ops)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400684{
685 struct device_node *np, *child, *sensor_np;
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300686 struct thermal_zone_device *tzd = ERR_PTR(-ENODEV);
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700687 struct thermal_zone_device *first_tzd = NULL;
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700688 struct __sensor_param *sens_param = NULL;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400689
690 np = of_find_node_by_name(NULL, "thermal-zones");
691 if (!np)
692 return ERR_PTR(-ENODEV);
693
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300694 if (!dev || !dev->of_node) {
695 of_node_put(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400696 return ERR_PTR(-EINVAL);
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300697 }
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400698
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700699 sens_param = kzalloc(sizeof(*sens_param), GFP_KERNEL);
700 if (!sens_param) {
701 of_node_put(np);
702 return ERR_PTR(-ENOMEM);
703 }
704 sens_param->sensor_data = data;
705 sens_param->ops = ops;
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700706 INIT_LIST_HEAD(&sens_param->first_tz);
Lina Iyer01ffea22016-07-27 13:46:32 -0600707 sens_param->trip_high = INT_MAX;
708 sens_param->trip_low = INT_MIN;
709 mutex_init(&sens_param->lock);
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300710 sensor_np = of_node_get(dev->of_node);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400711
Laxman Dewangan42bbe402016-02-08 18:58:34 +0530712 for_each_available_child_of_node(np, child) {
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400713 struct of_phandle_args sensor_specs;
714 int ret, id;
Ram Chandrasekar07479402017-08-25 14:04:42 -0600715 struct __thermal_zone *tz;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400716
717 /* For now, thermal framework supports only 1 sensor per zone */
718 ret = of_parse_phandle_with_args(child, "thermal-sensors",
719 "#thermal-sensor-cells",
720 0, &sensor_specs);
721 if (ret)
722 continue;
723
724 if (sensor_specs.args_count >= 1) {
725 id = sensor_specs.args[0];
726 WARN(sensor_specs.args_count > 1,
727 "%s: too many cells in sensor specifier %d\n",
728 sensor_specs.np->name, sensor_specs.args_count);
729 } else {
730 id = 0;
731 }
732
733 if (sensor_specs.np == sensor_np && id == sensor_id) {
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300734 tzd = thermal_zone_of_add_sensor(child, sensor_np,
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700735 sens_param);
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700736 if (!IS_ERR(tzd)) {
737 if (!first_tzd)
738 first_tzd = tzd;
Ram Chandrasekar07479402017-08-25 14:04:42 -0600739 tz = tzd->devdata;
740 if (!tz->default_disable)
741 tzd->ops->set_mode(tzd,
742 THERMAL_DEVICE_ENABLED);
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700743 }
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400744 }
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300745 of_node_put(sensor_specs.np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400746 }
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300747 of_node_put(sensor_np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400748 of_node_put(np);
749
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700750 if (!first_tzd) {
751 first_tzd = ERR_PTR(-ENODEV);
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700752 kfree(sens_param);
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700753 }
754 return first_tzd;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400755}
756EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_register);
757
758/**
759 * thermal_zone_of_sensor_unregister - unregisters a sensor from a DT thermal zone
760 * @dev: a valid struct device pointer of a sensor device. Must contain
761 * a valid .of_node, for the sensor node.
762 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
763 *
764 * This function removes the sensor callbacks and private data from the
765 * thermal zone device registered with thermal_zone_of_sensor_register()
766 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
767 * thermal zone device callbacks.
768 *
769 * TODO: When the support to several sensors per zone is added, this
770 * function must search the sensor list based on @dev parameter.
771 *
772 */
773void thermal_zone_of_sensor_unregister(struct device *dev,
774 struct thermal_zone_device *tzd)
775{
Ram Chandrasekar9a1da902017-05-09 16:58:55 -0600776 struct __thermal_zone *tz, *next;
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700777 struct thermal_zone_device *pos;
778 struct list_head *head;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400779
780 if (!dev || !tzd || !tzd->devdata)
781 return;
782
783 tz = tzd->devdata;
784
785 /* no __thermal_zone, nothing to be done */
786 if (!tz)
787 return;
788
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700789 head = &tz->senps->first_tz;
Ram Chandrasekar9a1da902017-05-09 16:58:55 -0600790 list_for_each_entry_safe(tz, next, head, list) {
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700791 pos = tz->tzd;
792 mutex_lock(&pos->lock);
793 pos->ops->get_temp = NULL;
794 pos->ops->get_trend = NULL;
795 pos->ops->set_emul_temp = NULL;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400796
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700797 list_del(&tz->list);
798 if (list_empty(&tz->senps->first_tz))
799 kfree(tz->senps);
800 tz->senps = NULL;
801 mutex_unlock(&pos->lock);
802 }
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400803}
804EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister);
805
Laxman Dewangane498b492016-03-09 18:40:06 +0530806static void devm_thermal_zone_of_sensor_release(struct device *dev, void *res)
807{
808 thermal_zone_of_sensor_unregister(dev,
809 *(struct thermal_zone_device **)res);
810}
811
812static int devm_thermal_zone_of_sensor_match(struct device *dev, void *res,
813 void *data)
814{
815 struct thermal_zone_device **r = res;
816
817 if (WARN_ON(!r || !*r))
818 return 0;
819
820 return *r == data;
821}
822
823/**
Ram Chandrasekar2f2b7462017-03-11 19:35:11 -0700824 * devm_thermal_of_virtual_sensor_register - Register a virtual sensor.
825 * Three types of virtual sensors are supported.
826 * 1. Weighted aggregation type:
827 * Virtual sensor of this type calculates the weighted aggregation
828 * of sensor temperatures using the below formula,
829 * temp = (sensor_1_temp * coeff_1 + ... + sensor_n_temp * coeff_n)
830 * + avg_offset / avg_denominator
831 * So the sensor drivers has to specify n+2 coefficients.
832 * 2. Maximum type:
833 * Virtual sensors of this type will report the maximum of all
834 * sensor temperatures.
835 * 3. Minimum type:
836 * Virtual sensors of this type will report the minimum of all
837 * sensor temperatures.
838 *
839 * @input arguments:
840 * @dev: Virtual sensor driver device pointer.
841 * @sensor_data: Virtual sensor data supported for the device.
842 *
843 * @return: Returns a virtual thermal zone pointer. Returns error if thermal
844 * zone is not created. Returns -EAGAIN, if the sensor that is required for
845 * this virtual sensor temperature estimation is not registered yet. The
846 * sensor driver can try again later.
847 */
848struct thermal_zone_device *devm_thermal_of_virtual_sensor_register(
849 struct device *dev,
850 const struct virtual_sensor_data *sensor_data)
851{
852 int sens_idx = 0;
853 struct virtual_sensor *sens;
854 struct __thermal_zone *tz;
855 struct thermal_zone_device **ptr;
856 struct thermal_zone_device *tzd;
857 struct __sensor_param *sens_param = NULL;
858 enum thermal_device_mode mode;
859
860 if (!dev || !sensor_data)
861 return ERR_PTR(-EINVAL);
862
863 tzd = thermal_zone_get_zone_by_name(
864 sensor_data->virt_zone_name);
865 if (IS_ERR(tzd)) {
Manaf Meethalavalappu Pallikunhi3d62c3f2017-08-23 21:39:35 +0530866 dev_dbg(dev, "sens:%s not available err: %ld\n",
Ram Chandrasekar2f2b7462017-03-11 19:35:11 -0700867 sensor_data->virt_zone_name,
868 PTR_ERR(tzd));
869 return tzd;
870 }
871
872 mutex_lock(&tzd->lock);
873 /*
874 * Check if the virtual zone is registered and enabled.
875 * If so return the registered thermal zone.
876 */
877 tzd->ops->get_mode(tzd, &mode);
878 mutex_unlock(&tzd->lock);
879 if (mode == THERMAL_DEVICE_ENABLED)
880 return tzd;
881
882 sens = devm_kzalloc(dev, sizeof(*sens), GFP_KERNEL);
883 if (!sens)
884 return ERR_PTR(-ENOMEM);
885
Ram Chandrasekar02592fa2017-05-16 17:03:02 -0600886 sens->virt_tz = tzd;
Ram Chandrasekar2f2b7462017-03-11 19:35:11 -0700887 sens->logic = sensor_data->logic;
888 sens->num_sensors = sensor_data->num_sensors;
889 if (sens->logic == VIRT_WEIGHTED_AVG) {
890 int coeff_ct = sensor_data->coefficient_ct;
891
892 /*
893 * For weighted aggregation, sensor drivers has to specify
894 * n+2 coefficients.
895 */
896 if (coeff_ct != sens->num_sensors) {
897 dev_err(dev, "sens:%s Invalid coefficient\n",
898 sensor_data->virt_zone_name);
899 return ERR_PTR(-EINVAL);
900 }
901 memcpy(sens->coefficients, sensor_data->coefficients,
902 coeff_ct * sizeof(*sens->coefficients));
903 sens->avg_offset = sensor_data->avg_offset;
904 sens->avg_denominator = sensor_data->avg_denominator;
905 }
906
907 for (sens_idx = 0; sens_idx < sens->num_sensors; sens_idx++) {
908 sens->tz[sens_idx] = thermal_zone_get_zone_by_name(
909 sensor_data->sensor_names[sens_idx]);
910 if (IS_ERR(sens->tz[sens_idx])) {
911 dev_err(dev, "sens:%s sensor[%s] fetch err:%ld\n",
912 sensor_data->virt_zone_name,
913 sensor_data->sensor_names[sens_idx],
914 PTR_ERR(sens->tz[sens_idx]));
915 break;
916 }
917 }
918 if (sens->num_sensors != sens_idx)
919 return ERR_PTR(-EAGAIN);
920
921 sens_param = kzalloc(sizeof(*sens_param), GFP_KERNEL);
922 if (!sens_param)
923 return ERR_PTR(-ENOMEM);
924 sens_param->sensor_data = sens;
925 sens_param->ops = &of_virt_ops;
926 INIT_LIST_HEAD(&sens_param->first_tz);
927 sens_param->trip_high = INT_MAX;
928 sens_param->trip_low = INT_MIN;
929 mutex_init(&sens_param->lock);
930
931 mutex_lock(&tzd->lock);
932 tz = tzd->devdata;
933 tz->senps = sens_param;
934 tzd->ops->get_temp = of_thermal_get_temp;
935 list_add_tail(&tz->list, &sens_param->first_tz);
936 mutex_unlock(&tzd->lock);
937
938 ptr = devres_alloc(devm_thermal_zone_of_sensor_release, sizeof(*ptr),
939 GFP_KERNEL);
940 if (!ptr)
941 return ERR_PTR(-ENOMEM);
942
943 *ptr = tzd;
944 devres_add(dev, ptr);
945
Ram Chandrasekar07479402017-08-25 14:04:42 -0600946 if (!tz->default_disable)
947 tzd->ops->set_mode(tzd, THERMAL_DEVICE_ENABLED);
Ram Chandrasekar2f2b7462017-03-11 19:35:11 -0700948
949 return tzd;
950}
951EXPORT_SYMBOL(devm_thermal_of_virtual_sensor_register);
952
953/**
Laxman Dewangane498b492016-03-09 18:40:06 +0530954 * devm_thermal_zone_of_sensor_register - Resource managed version of
955 * thermal_zone_of_sensor_register()
956 * @dev: a valid struct device pointer of a sensor device. Must contain
957 * a valid .of_node, for the sensor node.
958 * @sensor_id: a sensor identifier, in case the sensor IP has more
959 * than one sensors
960 * @data: a private pointer (owned by the caller) that will be passed
961 * back, when a temperature reading is needed.
962 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
963 *
964 * Refer thermal_zone_of_sensor_register() for more details.
965 *
966 * Return: On success returns a valid struct thermal_zone_device,
967 * otherwise, it returns a corresponding ERR_PTR(). Caller must
968 * check the return value with help of IS_ERR() helper.
Zhang Rui7b5c4a02016-08-22 15:48:11 +0800969 * Registered thermal_zone_device device will automatically be
Laxman Dewangane498b492016-03-09 18:40:06 +0530970 * released when device is unbounded.
971 */
972struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
973 struct device *dev, int sensor_id,
974 void *data, const struct thermal_zone_of_device_ops *ops)
975{
976 struct thermal_zone_device **ptr, *tzd;
977
978 ptr = devres_alloc(devm_thermal_zone_of_sensor_release, sizeof(*ptr),
979 GFP_KERNEL);
980 if (!ptr)
981 return ERR_PTR(-ENOMEM);
982
983 tzd = thermal_zone_of_sensor_register(dev, sensor_id, data, ops);
984 if (IS_ERR(tzd)) {
985 devres_free(ptr);
986 return tzd;
987 }
988
989 *ptr = tzd;
990 devres_add(dev, ptr);
991
992 return tzd;
993}
994EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_register);
995
996/**
997 * devm_thermal_zone_of_sensor_unregister - Resource managed version of
998 * thermal_zone_of_sensor_unregister().
999 * @dev: Device for which which resource was allocated.
1000 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
1001 *
1002 * This function removes the sensor callbacks and private data from the
1003 * thermal zone device registered with devm_thermal_zone_of_sensor_register()
1004 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
1005 * thermal zone device callbacks.
1006 * Normally this function will not need to be called and the resource
1007 * management code will ensure that the resource is freed.
1008 */
1009void devm_thermal_zone_of_sensor_unregister(struct device *dev,
1010 struct thermal_zone_device *tzd)
1011{
1012 WARN_ON(devres_release(dev, devm_thermal_zone_of_sensor_release,
1013 devm_thermal_zone_of_sensor_match, tzd));
1014}
1015EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_unregister);
1016
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001017/*** functions parsing device tree nodes ***/
1018
1019/**
1020 * thermal_of_populate_bind_params - parse and fill cooling map data
1021 * @np: DT node containing a cooling-map node
1022 * @__tbp: data structure to be filled with cooling map info
1023 * @trips: array of thermal zone trip points
1024 * @ntrips: number of trip points inside trips.
1025 *
1026 * This function parses a cooling-map type of node represented by
1027 * @np parameter and fills the read data into @__tbp data structure.
1028 * It needs the already parsed array of trip points of the thermal zone
1029 * in consideration.
1030 *
1031 * Return: 0 on success, proper error code otherwise
1032 */
1033static int thermal_of_populate_bind_params(struct device_node *np,
1034 struct __thermal_bind_params *__tbp,
Lukasz Majewskiad9914a2014-12-08 18:04:19 +01001035 struct thermal_trip *trips,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001036 int ntrips)
1037{
1038 struct of_phandle_args cooling_spec;
1039 struct device_node *trip;
1040 int ret, i;
1041 u32 prop;
1042
1043 /* Default weight. Usage is optional */
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +00001044 __tbp->usage = THERMAL_WEIGHT_DEFAULT;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001045 ret = of_property_read_u32(np, "contribution", &prop);
1046 if (ret == 0)
1047 __tbp->usage = prop;
1048
1049 trip = of_parse_phandle(np, "trip", 0);
1050 if (!trip) {
1051 pr_err("missing trip property\n");
1052 return -ENODEV;
1053 }
1054
1055 /* match using device_node */
1056 for (i = 0; i < ntrips; i++)
1057 if (trip == trips[i].np) {
1058 __tbp->trip_id = i;
1059 break;
1060 }
1061
1062 if (i == ntrips) {
1063 ret = -ENODEV;
1064 goto end;
1065 }
1066
1067 ret = of_parse_phandle_with_args(np, "cooling-device", "#cooling-cells",
1068 0, &cooling_spec);
1069 if (ret < 0) {
1070 pr_err("missing cooling_device property\n");
1071 goto end;
1072 }
1073 __tbp->cooling_device = cooling_spec.np;
1074 if (cooling_spec.args_count >= 2) { /* at least min and max */
1075 __tbp->min = cooling_spec.args[0];
1076 __tbp->max = cooling_spec.args[1];
1077 } else {
1078 pr_err("wrong reference to cooling device, missing limits\n");
1079 }
1080
1081end:
1082 of_node_put(trip);
1083
1084 return ret;
1085}
1086
1087/**
1088 * It maps 'enum thermal_trip_type' found in include/linux/thermal.h
1089 * into the device tree binding of 'trip', property type.
1090 */
1091static const char * const trip_types[] = {
1092 [THERMAL_TRIP_ACTIVE] = "active",
1093 [THERMAL_TRIP_PASSIVE] = "passive",
1094 [THERMAL_TRIP_HOT] = "hot",
1095 [THERMAL_TRIP_CRITICAL] = "critical",
1096};
1097
1098/**
1099 * thermal_of_get_trip_type - Get phy mode for given device_node
1100 * @np: Pointer to the given device_node
1101 * @type: Pointer to resulting trip type
1102 *
1103 * The function gets trip type string from property 'type',
1104 * and store its index in trip_types table in @type,
1105 *
1106 * Return: 0 on success, or errno in error case.
1107 */
1108static int thermal_of_get_trip_type(struct device_node *np,
1109 enum thermal_trip_type *type)
1110{
1111 const char *t;
1112 int err, i;
1113
1114 err = of_property_read_string(np, "type", &t);
1115 if (err < 0)
1116 return err;
1117
1118 for (i = 0; i < ARRAY_SIZE(trip_types); i++)
1119 if (!strcasecmp(t, trip_types[i])) {
1120 *type = i;
1121 return 0;
1122 }
1123
1124 return -ENODEV;
1125}
1126
1127/**
1128 * thermal_of_populate_trip - parse and fill one trip point data
1129 * @np: DT node containing a trip point node
1130 * @trip: trip point data structure to be filled up
1131 *
1132 * This function parses a trip point type of node represented by
1133 * @np parameter and fills the read data into @trip data structure.
1134 *
1135 * Return: 0 on success, proper error code otherwise
1136 */
1137static int thermal_of_populate_trip(struct device_node *np,
Lukasz Majewskiad9914a2014-12-08 18:04:19 +01001138 struct thermal_trip *trip)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001139{
1140 int prop;
1141 int ret;
1142
1143 ret = of_property_read_u32(np, "temperature", &prop);
1144 if (ret < 0) {
1145 pr_err("missing temperature property\n");
1146 return ret;
1147 }
1148 trip->temperature = prop;
1149
1150 ret = of_property_read_u32(np, "hysteresis", &prop);
1151 if (ret < 0) {
1152 pr_err("missing hysteresis property\n");
1153 return ret;
1154 }
1155 trip->hysteresis = prop;
1156
1157 ret = thermal_of_get_trip_type(np, &trip->type);
1158 if (ret < 0) {
1159 pr_err("wrong trip type property\n");
1160 return ret;
1161 }
1162
1163 /* Required for cooling map matching */
1164 trip->np = np;
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001165 of_node_get(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001166
1167 return 0;
1168}
1169
1170/**
1171 * thermal_of_build_thermal_zone - parse and fill one thermal zone data
1172 * @np: DT node containing a thermal zone node
1173 *
1174 * This function parses a thermal zone type of node represented by
1175 * @np parameter and fills the read data into a __thermal_zone data structure
1176 * and return this pointer.
1177 *
Eduardo Valentina46dbae2015-05-11 19:48:09 -07001178 * TODO: Missing properties to parse: thermal-sensor-names
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001179 *
1180 * Return: On success returns a valid struct __thermal_zone,
1181 * otherwise, it returns a corresponding ERR_PTR(). Caller must
1182 * check the return value with help of IS_ERR() helper.
1183 */
Julia Lawallc0ff8aa2016-04-19 14:33:32 +02001184static struct __thermal_zone
1185__init *thermal_of_build_thermal_zone(struct device_node *np)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001186{
1187 struct device_node *child = NULL, *gchild;
1188 struct __thermal_zone *tz;
1189 int ret, i;
Eduardo Valentina46dbae2015-05-11 19:48:09 -07001190 u32 prop, coef[2];
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001191
1192 if (!np) {
1193 pr_err("no thermal zone np\n");
1194 return ERR_PTR(-EINVAL);
1195 }
1196
1197 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
1198 if (!tz)
1199 return ERR_PTR(-ENOMEM);
1200
Ram Chandrasekard29230b2017-02-27 11:26:51 -07001201 INIT_LIST_HEAD(&tz->list);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001202 ret = of_property_read_u32(np, "polling-delay-passive", &prop);
1203 if (ret < 0) {
1204 pr_err("missing polling-delay-passive property\n");
1205 goto free_tz;
1206 }
1207 tz->passive_delay = prop;
1208
1209 ret = of_property_read_u32(np, "polling-delay", &prop);
1210 if (ret < 0) {
1211 pr_err("missing polling-delay property\n");
1212 goto free_tz;
1213 }
1214 tz->polling_delay = prop;
1215
Ram Chandrasekar07479402017-08-25 14:04:42 -06001216 tz->default_disable = of_property_read_bool(np,
1217 "disable-thermal-zone");
Eduardo Valentina46dbae2015-05-11 19:48:09 -07001218 /*
1219 * REVIST: for now, the thermal framework supports only
1220 * one sensor per thermal zone. Thus, we are considering
1221 * only the first two values as slope and offset.
1222 */
1223 ret = of_property_read_u32_array(np, "coefficients", coef, 2);
1224 if (ret == 0) {
1225 tz->slope = coef[0];
1226 tz->offset = coef[1];
1227 } else {
1228 tz->slope = 1;
1229 tz->offset = 0;
1230 }
1231
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001232 /* trips */
1233 child = of_get_child_by_name(np, "trips");
1234
1235 /* No trips provided */
1236 if (!child)
1237 goto finish;
1238
1239 tz->ntrips = of_get_child_count(child);
1240 if (tz->ntrips == 0) /* must have at least one child */
1241 goto finish;
1242
1243 tz->trips = kzalloc(tz->ntrips * sizeof(*tz->trips), GFP_KERNEL);
1244 if (!tz->trips) {
1245 ret = -ENOMEM;
1246 goto free_tz;
1247 }
1248
1249 i = 0;
1250 for_each_child_of_node(child, gchild) {
1251 ret = thermal_of_populate_trip(gchild, &tz->trips[i++]);
1252 if (ret)
1253 goto free_trips;
1254 }
1255
1256 of_node_put(child);
1257
1258 /* cooling-maps */
1259 child = of_get_child_by_name(np, "cooling-maps");
1260
1261 /* cooling-maps not provided */
1262 if (!child)
1263 goto finish;
1264
1265 tz->num_tbps = of_get_child_count(child);
1266 if (tz->num_tbps == 0)
1267 goto finish;
1268
1269 tz->tbps = kzalloc(tz->num_tbps * sizeof(*tz->tbps), GFP_KERNEL);
1270 if (!tz->tbps) {
1271 ret = -ENOMEM;
1272 goto free_trips;
1273 }
1274
1275 i = 0;
Stephen Boydca9521b2014-06-18 16:32:08 -07001276 for_each_child_of_node(child, gchild) {
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001277 ret = thermal_of_populate_bind_params(gchild, &tz->tbps[i++],
1278 tz->trips, tz->ntrips);
1279 if (ret)
1280 goto free_tbps;
Stephen Boydca9521b2014-06-18 16:32:08 -07001281 }
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001282
1283finish:
1284 of_node_put(child);
1285 tz->mode = THERMAL_DEVICE_DISABLED;
1286
1287 return tz;
1288
1289free_tbps:
Ulises Brindis1cd91c12016-03-25 12:55:41 -07001290 for (i = i - 1; i >= 0; i--)
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001291 of_node_put(tz->tbps[i].cooling_device);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001292 kfree(tz->tbps);
1293free_trips:
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001294 for (i = 0; i < tz->ntrips; i++)
1295 of_node_put(tz->trips[i].np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001296 kfree(tz->trips);
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001297 of_node_put(gchild);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001298free_tz:
1299 kfree(tz);
1300 of_node_put(child);
1301
1302 return ERR_PTR(ret);
1303}
1304
1305static inline void of_thermal_free_zone(struct __thermal_zone *tz)
1306{
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001307 int i;
1308
1309 for (i = 0; i < tz->num_tbps; i++)
1310 of_node_put(tz->tbps[i].cooling_device);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001311 kfree(tz->tbps);
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001312 for (i = 0; i < tz->ntrips; i++)
1313 of_node_put(tz->trips[i].np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001314 kfree(tz->trips);
1315 kfree(tz);
1316}
1317
1318/**
1319 * of_parse_thermal_zones - parse device tree thermal data
1320 *
1321 * Initialization function that can be called by machine initialization
1322 * code to parse thermal data and populate the thermal framework
1323 * with hardware thermal zones info. This function only parses thermal zones.
1324 * Cooling devices and sensor devices nodes are supposed to be parsed
1325 * by their respective drivers.
1326 *
1327 * Return: 0 on success, proper error code otherwise
1328 *
1329 */
1330int __init of_parse_thermal_zones(void)
1331{
1332 struct device_node *np, *child;
1333 struct __thermal_zone *tz;
1334 struct thermal_zone_device_ops *ops;
1335
1336 np = of_find_node_by_name(NULL, "thermal-zones");
1337 if (!np) {
1338 pr_debug("unable to find thermal zones\n");
1339 return 0; /* Run successfully on systems without thermal DT */
1340 }
1341
Laxman Dewangan42bbe402016-02-08 18:58:34 +05301342 for_each_available_child_of_node(np, child) {
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001343 struct thermal_zone_device *zone;
1344 struct thermal_zone_params *tzp;
Punit Agrawal76af5492015-03-03 10:43:04 +00001345 int i, mask = 0;
Punit Agrawal647f9922015-02-26 19:00:32 +00001346 u32 prop;
Ram Chandrasekarcaafe202016-07-19 11:25:46 -06001347 const char *governor_name;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001348
1349 tz = thermal_of_build_thermal_zone(child);
1350 if (IS_ERR(tz)) {
1351 pr_err("failed to build thermal zone %s: %ld\n",
1352 child->name,
1353 PTR_ERR(tz));
1354 continue;
1355 }
1356
1357 ops = kmemdup(&of_thermal_ops, sizeof(*ops), GFP_KERNEL);
1358 if (!ops)
1359 goto exit_free;
1360
1361 tzp = kzalloc(sizeof(*tzp), GFP_KERNEL);
1362 if (!tzp) {
1363 kfree(ops);
1364 goto exit_free;
1365 }
1366
1367 /* No hwmon because there might be hwmon drivers registering */
1368 tzp->no_hwmon = true;
1369
Ram Chandrasekarcaafe202016-07-19 11:25:46 -06001370 if (!of_property_read_string(child, "thermal-governor",
1371 &governor_name))
1372 strlcpy(tzp->governor_name, governor_name,
1373 THERMAL_NAME_LENGTH);
1374
Punit Agrawal647f9922015-02-26 19:00:32 +00001375 if (!of_property_read_u32(child, "sustainable-power", &prop))
1376 tzp->sustainable_power = prop;
1377
Punit Agrawal76af5492015-03-03 10:43:04 +00001378 for (i = 0; i < tz->ntrips; i++)
1379 mask |= 1 << i;
1380
Eduardo Valentina46dbae2015-05-11 19:48:09 -07001381 /* these two are left for temperature drivers to use */
1382 tzp->slope = tz->slope;
1383 tzp->offset = tz->offset;
1384
Lina Iyer159f67d2016-07-27 11:34:46 -06001385 if (of_property_read_bool(child, "tracks-low"))
1386 tzp->tracks_low = true;
1387
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001388 zone = thermal_zone_device_register(child->name, tz->ntrips,
Punit Agrawal76af5492015-03-03 10:43:04 +00001389 mask, tz,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001390 ops, tzp,
1391 tz->passive_delay,
1392 tz->polling_delay);
1393 if (IS_ERR(zone)) {
1394 pr_err("Failed to build %s zone %ld\n", child->name,
1395 PTR_ERR(zone));
1396 kfree(tzp);
1397 kfree(ops);
1398 of_thermal_free_zone(tz);
1399 /* attempting to build remaining zones still */
Ram Chandrasekard29230b2017-02-27 11:26:51 -07001400 continue;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001401 }
Ram Chandrasekard29230b2017-02-27 11:26:51 -07001402 tz->tzd = zone;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001403 }
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001404 of_node_put(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001405
1406 return 0;
1407
1408exit_free:
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001409 of_node_put(child);
1410 of_node_put(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001411 of_thermal_free_zone(tz);
1412
1413 /* no memory available, so free what we have built */
1414 of_thermal_destroy_zones();
1415
1416 return -ENOMEM;
1417}
1418
1419/**
1420 * of_thermal_destroy_zones - remove all zones parsed and allocated resources
1421 *
1422 * Finds all zones parsed and added to the thermal framework and remove them
1423 * from the system, together with their resources.
1424 *
1425 */
1426void of_thermal_destroy_zones(void)
1427{
1428 struct device_node *np, *child;
1429
1430 np = of_find_node_by_name(NULL, "thermal-zones");
1431 if (!np) {
Jiada Wang28524982015-11-16 17:10:05 +09001432 pr_debug("unable to find thermal zones\n");
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001433 return;
1434 }
1435
Laxman Dewangan42bbe402016-02-08 18:58:34 +05301436 for_each_available_child_of_node(np, child) {
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001437 struct thermal_zone_device *zone;
1438
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001439 zone = thermal_zone_get_zone_by_name(child->name);
1440 if (IS_ERR(zone))
1441 continue;
1442
1443 thermal_zone_device_unregister(zone);
1444 kfree(zone->tzp);
1445 kfree(zone->ops);
1446 of_thermal_free_zone(zone->devdata);
1447 }
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001448 of_node_put(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001449}