blob: 0afc4481ffa8bbd0ecec1065bfc1b146d03b6bc8 [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
36#include "thermal_core.h"
37
Ram Chandrasekard29230b2017-02-27 11:26:51 -070038/*** Private data structures to represent thermal device tree data ***/
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040039/**
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040040 * struct __thermal_bind_param - a match between trip and cooling device
41 * @cooling_device: a pointer to identify the referred cooling device
42 * @trip_id: the trip point index
43 * @usage: the percentage (from 0 to 100) of cooling contribution
44 * @min: minimum cooling state used at this trip point
45 * @max: maximum cooling state used at this trip point
46 */
47
48struct __thermal_bind_params {
49 struct device_node *cooling_device;
50 unsigned int trip_id;
51 unsigned int usage;
52 unsigned long min;
53 unsigned long max;
54};
55
56/**
Ram Chandrasekare34ced62017-03-03 11:22:50 -070057 * struct __sensor_param - Holds individual sensor data
58 * @sensor_data: sensor driver private data passed as input argument
59 * @ops: sensor driver ops
Lina Iyer01ffea22016-07-27 13:46:32 -060060 * @trip_high: last trip high value programmed in the sensor driver
61 * @trip_low: last trip low value programmed in the sensor driver
62 * @lock: mutex lock acquired before updating the trip temperatures
Ram Chandrasekard29230b2017-02-27 11:26:51 -070063 * @first_tz: list head pointing the first thermal zone
Ram Chandrasekare34ced62017-03-03 11:22:50 -070064 */
65struct __sensor_param {
66 void *sensor_data;
67 const struct thermal_zone_of_device_ops *ops;
Lina Iyer01ffea22016-07-27 13:46:32 -060068 int trip_high, trip_low;
69 struct mutex lock;
Ram Chandrasekard29230b2017-02-27 11:26:51 -070070 struct list_head first_tz;
Ram Chandrasekare34ced62017-03-03 11:22:50 -070071};
72
73/**
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040074 * struct __thermal_zone - internal representation of a thermal zone
75 * @mode: current thermal zone device mode (enabled/disabled)
76 * @passive_delay: polling interval while passive cooling is activated
77 * @polling_delay: zone polling interval
Eduardo Valentina46dbae2015-05-11 19:48:09 -070078 * @slope: slope of the temperature adjustment curve
79 * @offset: offset of the temperature adjustment curve
Ram Chandrasekard29230b2017-02-27 11:26:51 -070080 * @tzd: thermal zone device pointer for this sensor
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040081 * @ntrips: number of trip points
82 * @trips: an array of trip points (0..ntrips - 1)
83 * @num_tbps: number of thermal bind params
84 * @tbps: an array of thermal bind params (0..num_tbps - 1)
Ram Chandrasekard29230b2017-02-27 11:26:51 -070085 * @list: sibling thermal zone pointer
Ram Chandrasekare34ced62017-03-03 11:22:50 -070086 * @senps: sensor related parameters
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040087 */
88
89struct __thermal_zone {
90 enum thermal_device_mode mode;
91 int passive_delay;
92 int polling_delay;
Eduardo Valentina46dbae2015-05-11 19:48:09 -070093 int slope;
94 int offset;
Ram Chandrasekard29230b2017-02-27 11:26:51 -070095 struct thermal_zone_device *tzd;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -040096
97 /* trip data */
98 int ntrips;
Lukasz Majewskiad9914a2014-12-08 18:04:19 +010099 struct thermal_trip *trips;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400100
101 /* cooling binding data */
102 int num_tbps;
103 struct __thermal_bind_params *tbps;
104
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700105 struct list_head list;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400106 /* sensor interface */
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700107 struct __sensor_param *senps;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400108};
109
Ram Chandrasekar2f2b7462017-03-11 19:35:11 -0700110/**
111 * struct virtual_sensor - internal representation of a virtual thermal zone
112 * @num_sensors - number of sensors this virtual sensor will reference to
113 * estimate temperature
114 * @tz - Array of thermal zones of the sensors this virtual sensor will use
115 * to estimate temperature
116 * @logic - aggregation logic to be used to estimate the temperature
117 * @last_reading - last estimated temperature
118 * @coefficients - array of coefficients to be used for weighted aggregation
119 * logic
120 * @avg_offset - offset value to be used for the weighted aggregation logic
121 * @avg_denominator - denominator value to be used for the weighted aggregation
122 * logic
123 */
124struct virtual_sensor {
125 int num_sensors;
126 struct thermal_zone_device *tz[THERMAL_MAX_VIRT_SENSORS];
127 enum aggregation_logic logic;
128 int last_reading;
129 int coefficients[THERMAL_MAX_VIRT_SENSORS];
130 int avg_offset;
131 int avg_denominator;
132};
133
Lina Iyer01ffea22016-07-27 13:46:32 -0600134static int of_thermal_aggregate_trip_types(struct thermal_zone_device *tz,
135 unsigned int trip_type_mask, int *low, int *high);
136
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400137/*** DT thermal zone device callbacks ***/
138
Ram Chandrasekar2f2b7462017-03-11 19:35:11 -0700139static int virt_sensor_read_temp(void *data, int *val)
140{
141 struct virtual_sensor *sens = data;
142 int idx, temp = 0, ret = 0;
143
144 for (idx = 0; idx < sens->num_sensors; idx++) {
145 int sens_temp = 0;
146
147 ret = thermal_zone_get_temp(sens->tz[idx], &sens_temp);
148 if (ret) {
149 pr_err("virt zone: sensor[%s] read error:%d\n",
150 sens->tz[idx]->type, ret);
151 return ret;
152 }
153 switch (sens->logic) {
154 case VIRT_WEIGHTED_AVG:
155 temp += sens_temp * sens->coefficients[idx];
156 if (idx == (sens->num_sensors - 1))
157 temp = (temp + sens->avg_offset)
158 / sens->avg_denominator;
159 break;
160 case VIRT_MAXIMUM:
161 if (idx == 0)
162 temp = INT_MIN;
163 if (sens_temp > temp)
164 temp = sens_temp;
165 break;
166 case VIRT_MINIMUM:
167 if (idx == 0)
168 temp = INT_MAX;
169 if (sens_temp < temp)
170 temp = sens_temp;
171 break;
172 default:
173 break;
174 }
175 }
176
177 sens->last_reading = *val = temp;
178
179 return 0;
180}
181
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400182static int of_thermal_get_temp(struct thermal_zone_device *tz,
Sascha Hauer17e83512015-07-24 08:12:54 +0200183 int *temp)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400184{
185 struct __thermal_zone *data = tz->devdata;
186
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700187 if (!data->senps || !data->senps->ops->get_temp)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400188 return -EINVAL;
189
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700190 return data->senps->ops->get_temp(data->senps->sensor_data, temp);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400191}
192
Sascha Hauer826386e2016-06-22 16:42:02 +0800193static int of_thermal_set_trips(struct thermal_zone_device *tz,
Lina Iyer01ffea22016-07-27 13:46:32 -0600194 int inp_low, int inp_high)
Sascha Hauer826386e2016-06-22 16:42:02 +0800195{
196 struct __thermal_zone *data = tz->devdata;
Lina Iyer01ffea22016-07-27 13:46:32 -0600197 int high = INT_MAX, low = INT_MIN, ret = 0;
Sascha Hauer826386e2016-06-22 16:42:02 +0800198
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700199 if (!data->senps || !data->senps->ops->set_trips)
Sascha Hauer826386e2016-06-22 16:42:02 +0800200 return -EINVAL;
201
Lina Iyer01ffea22016-07-27 13:46:32 -0600202 mutex_lock(&data->senps->lock);
203 of_thermal_aggregate_trip_types(tz, GENMASK(THERMAL_TRIP_CRITICAL, 0),
204 &low, &high);
Lina Iyer01ffea22016-07-27 13:46:32 -0600205 data->senps->trip_low = low;
206 data->senps->trip_high = high;
207 ret = data->senps->ops->set_trips(data->senps->sensor_data,
208 low, high);
209
Lina Iyer01ffea22016-07-27 13:46:32 -0600210 mutex_unlock(&data->senps->lock);
211 return ret;
Sascha Hauer826386e2016-06-22 16:42:02 +0800212}
213
Lukasz Majewski08dab662014-12-08 18:04:17 +0100214/**
215 * of_thermal_get_ntrips - function to export number of available trip
216 * points.
217 * @tz: pointer to a thermal zone
218 *
219 * This function is a globally visible wrapper to get number of trip points
220 * stored in the local struct __thermal_zone
221 *
222 * Return: number of available trip points, -ENODEV when data not available
223 */
224int of_thermal_get_ntrips(struct thermal_zone_device *tz)
225{
226 struct __thermal_zone *data = tz->devdata;
227
228 if (!data || IS_ERR(data))
229 return -ENODEV;
230
231 return data->ntrips;
232}
233EXPORT_SYMBOL_GPL(of_thermal_get_ntrips);
234
Lukasz Majewskia9bf2cc2014-12-08 18:04:18 +0100235/**
236 * of_thermal_is_trip_valid - function to check if trip point is valid
237 *
238 * @tz: pointer to a thermal zone
239 * @trip: trip point to evaluate
240 *
241 * This function is responsible for checking if passed trip point is valid
242 *
243 * Return: true if trip point is valid, false otherwise
244 */
245bool of_thermal_is_trip_valid(struct thermal_zone_device *tz, int trip)
246{
247 struct __thermal_zone *data = tz->devdata;
248
249 if (!data || trip >= data->ntrips || trip < 0)
250 return false;
251
252 return true;
253}
254EXPORT_SYMBOL_GPL(of_thermal_is_trip_valid);
255
Lukasz Majewskice8be772014-12-08 18:04:20 +0100256/**
257 * of_thermal_get_trip_points - function to get access to a globally exported
258 * trip points
259 *
260 * @tz: pointer to a thermal zone
261 *
262 * This function provides a pointer to trip points table
263 *
264 * Return: pointer to trip points table, NULL otherwise
265 */
Geert Uytterhoevenebc31932015-01-03 22:56:56 +0100266const struct thermal_trip *
Lukasz Majewskice8be772014-12-08 18:04:20 +0100267of_thermal_get_trip_points(struct thermal_zone_device *tz)
268{
269 struct __thermal_zone *data = tz->devdata;
270
271 if (!data)
272 return NULL;
273
274 return data->trips;
275}
276EXPORT_SYMBOL_GPL(of_thermal_get_trip_points);
277
Lukasz Majewski184a4bf2014-12-08 18:04:21 +0100278/**
279 * of_thermal_set_emul_temp - function to set emulated temperature
280 *
281 * @tz: pointer to a thermal zone
282 * @temp: temperature to set
283 *
284 * This function gives the ability to set emulated value of temperature,
285 * which is handy for debugging
286 *
287 * Return: zero on success, error code otherwise
288 */
289static int of_thermal_set_emul_temp(struct thermal_zone_device *tz,
Sascha Hauer17e83512015-07-24 08:12:54 +0200290 int temp)
Lukasz Majewski184a4bf2014-12-08 18:04:21 +0100291{
292 struct __thermal_zone *data = tz->devdata;
293
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700294 if (!data->senps || !data->senps->ops->set_emul_temp)
295 return -EINVAL;
296
297 return data->senps->ops->set_emul_temp(data->senps->sensor_data, temp);
Lukasz Majewski184a4bf2014-12-08 18:04:21 +0100298}
299
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400300static int of_thermal_get_trend(struct thermal_zone_device *tz, int trip,
301 enum thermal_trend *trend)
302{
303 struct __thermal_zone *data = tz->devdata;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400304
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700305 if (!data->senps || !data->senps->ops->get_trend)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400306 return -EINVAL;
307
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700308 return data->senps->ops->get_trend(data->senps->sensor_data,
309 trip, trend);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400310}
311
312static int of_thermal_bind(struct thermal_zone_device *thermal,
313 struct thermal_cooling_device *cdev)
314{
315 struct __thermal_zone *data = thermal->devdata;
316 int i;
317
318 if (!data || IS_ERR(data))
319 return -ENODEV;
320
321 /* find where to bind */
322 for (i = 0; i < data->num_tbps; i++) {
323 struct __thermal_bind_params *tbp = data->tbps + i;
324
325 if (tbp->cooling_device == cdev->np) {
326 int ret;
327
328 ret = thermal_zone_bind_cooling_device(thermal,
329 tbp->trip_id, cdev,
Punit Agrawaldd354b82014-06-03 10:59:58 +0100330 tbp->max,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000331 tbp->min,
332 tbp->usage);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400333 if (ret)
334 return ret;
335 }
336 }
337
338 return 0;
339}
340
341static int of_thermal_unbind(struct thermal_zone_device *thermal,
342 struct thermal_cooling_device *cdev)
343{
344 struct __thermal_zone *data = thermal->devdata;
345 int i;
346
347 if (!data || IS_ERR(data))
348 return -ENODEV;
349
350 /* find where to unbind */
351 for (i = 0; i < data->num_tbps; i++) {
352 struct __thermal_bind_params *tbp = data->tbps + i;
353
354 if (tbp->cooling_device == cdev->np) {
355 int ret;
356
357 ret = thermal_zone_unbind_cooling_device(thermal,
358 tbp->trip_id, cdev);
359 if (ret)
360 return ret;
361 }
362 }
363
364 return 0;
365}
366
367static int of_thermal_get_mode(struct thermal_zone_device *tz,
368 enum thermal_device_mode *mode)
369{
370 struct __thermal_zone *data = tz->devdata;
371
372 *mode = data->mode;
373
374 return 0;
375}
376
377static int of_thermal_set_mode(struct thermal_zone_device *tz,
378 enum thermal_device_mode mode)
379{
380 struct __thermal_zone *data = tz->devdata;
381
382 mutex_lock(&tz->lock);
383
384 if (mode == THERMAL_DEVICE_ENABLED)
385 tz->polling_delay = data->polling_delay;
386 else
387 tz->polling_delay = 0;
388
389 mutex_unlock(&tz->lock);
390
391 data->mode = mode;
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700392 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400393
394 return 0;
395}
396
397static int of_thermal_get_trip_type(struct thermal_zone_device *tz, int trip,
398 enum thermal_trip_type *type)
399{
400 struct __thermal_zone *data = tz->devdata;
401
402 if (trip >= data->ntrips || trip < 0)
403 return -EDOM;
404
405 *type = data->trips[trip].type;
406
407 return 0;
408}
409
410static int of_thermal_get_trip_temp(struct thermal_zone_device *tz, int trip,
Sascha Hauer17e83512015-07-24 08:12:54 +0200411 int *temp)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400412{
413 struct __thermal_zone *data = tz->devdata;
414
415 if (trip >= data->ntrips || trip < 0)
416 return -EDOM;
417
418 *temp = data->trips[trip].temperature;
419
420 return 0;
421}
422
423static int of_thermal_set_trip_temp(struct thermal_zone_device *tz, int trip,
Sascha Hauer17e83512015-07-24 08:12:54 +0200424 int temp)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400425{
426 struct __thermal_zone *data = tz->devdata;
427
428 if (trip >= data->ntrips || trip < 0)
429 return -EDOM;
430
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700431 if (data->senps && data->senps->ops->set_trip_temp) {
Wei Nic3509522016-03-29 18:29:17 +0800432 int ret;
433
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700434 ret = data->senps->ops->set_trip_temp(data->senps->sensor_data,
435 trip, temp);
Wei Nic3509522016-03-29 18:29:17 +0800436 if (ret)
437 return ret;
438 }
439
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400440 /* thermal framework should take care of data->mask & (1 << trip) */
441 data->trips[trip].temperature = temp;
442
443 return 0;
444}
445
446static int of_thermal_get_trip_hyst(struct thermal_zone_device *tz, int trip,
Sascha Hauer17e83512015-07-24 08:12:54 +0200447 int *hyst)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400448{
449 struct __thermal_zone *data = tz->devdata;
450
451 if (trip >= data->ntrips || trip < 0)
452 return -EDOM;
453
454 *hyst = data->trips[trip].hysteresis;
455
456 return 0;
457}
458
459static int of_thermal_set_trip_hyst(struct thermal_zone_device *tz, int trip,
Sascha Hauer17e83512015-07-24 08:12:54 +0200460 int hyst)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400461{
462 struct __thermal_zone *data = tz->devdata;
463
464 if (trip >= data->ntrips || trip < 0)
465 return -EDOM;
466
467 /* thermal framework should take care of data->mask & (1 << trip) */
468 data->trips[trip].hysteresis = hyst;
469
470 return 0;
471}
472
473static int of_thermal_get_crit_temp(struct thermal_zone_device *tz,
Sascha Hauer17e83512015-07-24 08:12:54 +0200474 int *temp)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400475{
476 struct __thermal_zone *data = tz->devdata;
477 int i;
478
479 for (i = 0; i < data->ntrips; i++)
480 if (data->trips[i].type == THERMAL_TRIP_CRITICAL) {
481 *temp = data->trips[i].temperature;
482 return 0;
483 }
484
485 return -EINVAL;
486}
487
Lina Iyer01ffea22016-07-27 13:46:32 -0600488static int of_thermal_aggregate_trip_types(struct thermal_zone_device *tz,
489 unsigned int trip_type_mask, int *low, int *high)
490{
491 int min = INT_MIN;
492 int max = INT_MAX;
493 int tt, th, trip;
494 int temp = tz->temperature;
495 struct thermal_zone_device *zone = NULL;
496 struct __thermal_zone *data = tz->devdata;
497 struct list_head *head;
498 enum thermal_trip_type type = 0;
499
500 head = &data->senps->first_tz;
Ram Chandrasekar9a1da902017-05-09 16:58:55 -0600501 list_for_each_entry(data, head, list) {
Lina Iyer01ffea22016-07-27 13:46:32 -0600502 zone = data->tzd;
503 for (trip = 0; trip < data->ntrips; trip++) {
504 of_thermal_get_trip_type(zone, trip, &type);
505 if (!(BIT(type) & trip_type_mask))
506 continue;
507
508 if (!zone->tzp->tracks_low) {
509 tt = data->trips[trip].temperature;
510 if (tt > temp && tt < max)
511 max = tt;
512 th = tt - data->trips[trip].hysteresis;
513 if (th < temp && th > min)
514 min = th;
515 } else {
516 tt = data->trips[trip].temperature;
517 if (tt < temp && tt > min)
518 min = tt;
519 th = tt + data->trips[trip].hysteresis;
520 if (th > temp && th < max)
521 max = th;
522 }
523 }
524 }
525
526 *high = max;
527 *low = min;
528
529 return 0;
530}
531
532/*
533 * of_thermal_aggregate_trip - aggregate trip temperatures across sibling
534 * thermal zones.
535 * @tz: pointer to the primary thermal zone.
536 * @type: the thermal trip type to be aggregated upon
537 * @low: the low trip threshold which the most lesser than the @temp
538 * @high: the high trip threshold which is the least greater than the @temp
539 */
540int of_thermal_aggregate_trip(struct thermal_zone_device *tz,
541 enum thermal_trip_type type,
542 int *low, int *high)
543{
544 if (type <= THERMAL_TRIP_CRITICAL)
545 return of_thermal_aggregate_trip_types(tz, BIT(type), low,
546 high);
547
548 return -EINVAL;
549}
550EXPORT_SYMBOL(of_thermal_aggregate_trip);
551
Ram Chandrasekarcf543602017-03-13 22:59:01 -0600552/*
553 * of_thermal_handle_trip - Handle thermal trip from sensors
554 *
555 * @tz: pointer to the primary thermal zone.
556 */
557void of_thermal_handle_trip(struct thermal_zone_device *tz)
558{
559 struct thermal_zone_device *zone;
560 struct __thermal_zone *data = tz->devdata;
561 struct list_head *head;
562
563 head = &data->senps->first_tz;
Ram Chandrasekar9a1da902017-05-09 16:58:55 -0600564 list_for_each_entry(data, head, list) {
Ram Chandrasekarcf543602017-03-13 22:59:01 -0600565 zone = data->tzd;
566 thermal_zone_device_update(zone, THERMAL_EVENT_UNSPECIFIED);
567 }
568}
569EXPORT_SYMBOL(of_thermal_handle_trip);
570
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400571static struct thermal_zone_device_ops of_thermal_ops = {
572 .get_mode = of_thermal_get_mode,
573 .set_mode = of_thermal_set_mode,
574
575 .get_trip_type = of_thermal_get_trip_type,
576 .get_trip_temp = of_thermal_get_trip_temp,
577 .set_trip_temp = of_thermal_set_trip_temp,
578 .get_trip_hyst = of_thermal_get_trip_hyst,
579 .set_trip_hyst = of_thermal_set_trip_hyst,
580 .get_crit_temp = of_thermal_get_crit_temp,
581
582 .bind = of_thermal_bind,
583 .unbind = of_thermal_unbind,
584};
585
Ram Chandrasekar2f2b7462017-03-11 19:35:11 -0700586static struct thermal_zone_of_device_ops of_virt_ops = {
587 .get_temp = virt_sensor_read_temp,
588};
589
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400590/*** sensor API ***/
591
592static struct thermal_zone_device *
593thermal_zone_of_add_sensor(struct device_node *zone,
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700594 struct device_node *sensor,
595 struct __sensor_param *sens_param)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400596{
597 struct thermal_zone_device *tzd;
598 struct __thermal_zone *tz;
599
600 tzd = thermal_zone_get_zone_by_name(zone->name);
601 if (IS_ERR(tzd))
602 return ERR_PTR(-EPROBE_DEFER);
603
604 tz = tzd->devdata;
605
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700606 if (!sens_param->ops)
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400607 return ERR_PTR(-EINVAL);
608
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400609 mutex_lock(&tzd->lock);
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700610 tz->senps = sens_param;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400611
612 tzd->ops->get_temp = of_thermal_get_temp;
613 tzd->ops->get_trend = of_thermal_get_trend;
Sascha Hauer826386e2016-06-22 16:42:02 +0800614
615 /*
616 * The thermal zone core will calculate the window if they have set the
617 * optional set_trips pointer.
618 */
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700619 if (sens_param->ops->set_trips)
Sascha Hauer826386e2016-06-22 16:42:02 +0800620 tzd->ops->set_trips = of_thermal_set_trips;
621
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700622 if (sens_param->ops->set_emul_temp)
Keerthye2fa7482016-06-02 14:24:50 +0530623 tzd->ops->set_emul_temp = of_thermal_set_emul_temp;
624
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700625 list_add_tail(&tz->list, &sens_param->first_tz);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400626 mutex_unlock(&tzd->lock);
627
628 return tzd;
629}
630
631/**
632 * thermal_zone_of_sensor_register - registers a sensor to a DT thermal zone
633 * @dev: a valid struct device pointer of a sensor device. Must contain
634 * a valid .of_node, for the sensor node.
635 * @sensor_id: a sensor identifier, in case the sensor IP has more
636 * than one sensors
637 * @data: a private pointer (owned by the caller) that will be passed
638 * back, when a temperature reading is needed.
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400639 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400640 *
641 * This function will search the list of thermal zones described in device
642 * tree and look for the zone that refer to the sensor device pointed by
643 * @dev->of_node as temperature providers. For the zone pointing to the
644 * sensor node, the sensor will be added to the DT thermal zone device.
645 *
646 * The thermal zone temperature is provided by the @get_temp function
647 * pointer. When called, it will have the private pointer @data back.
648 *
649 * The thermal zone temperature trend is provided by the @get_trend function
650 * pointer. When called, it will have the private pointer @data back.
651 *
652 * TODO:
653 * 01 - This function must enqueue the new sensor instead of using
654 * it as the only source of temperature values.
655 *
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400656 * Return: On success returns a valid struct thermal_zone_device,
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700657 * otherwise, it returns a corresponding ERR_PTR(). Incase there are multiple
658 * thermal zones referencing the same sensor, the return value will be
659 * thermal_zone_device pointer of the first thermal zone. Caller must
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400660 * check the return value with help of IS_ERR() helper.
661 */
662struct thermal_zone_device *
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400663thermal_zone_of_sensor_register(struct device *dev, int sensor_id, void *data,
664 const struct thermal_zone_of_device_ops *ops)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400665{
666 struct device_node *np, *child, *sensor_np;
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300667 struct thermal_zone_device *tzd = ERR_PTR(-ENODEV);
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700668 struct thermal_zone_device *first_tzd = NULL;
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700669 struct __sensor_param *sens_param = NULL;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400670
671 np = of_find_node_by_name(NULL, "thermal-zones");
672 if (!np)
673 return ERR_PTR(-ENODEV);
674
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300675 if (!dev || !dev->of_node) {
676 of_node_put(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400677 return ERR_PTR(-EINVAL);
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300678 }
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400679
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700680 sens_param = kzalloc(sizeof(*sens_param), GFP_KERNEL);
681 if (!sens_param) {
682 of_node_put(np);
683 return ERR_PTR(-ENOMEM);
684 }
685 sens_param->sensor_data = data;
686 sens_param->ops = ops;
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700687 INIT_LIST_HEAD(&sens_param->first_tz);
Lina Iyer01ffea22016-07-27 13:46:32 -0600688 sens_param->trip_high = INT_MAX;
689 sens_param->trip_low = INT_MIN;
690 mutex_init(&sens_param->lock);
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300691 sensor_np = of_node_get(dev->of_node);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400692
Laxman Dewangan42bbe402016-02-08 18:58:34 +0530693 for_each_available_child_of_node(np, child) {
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400694 struct of_phandle_args sensor_specs;
695 int ret, id;
696
697 /* For now, thermal framework supports only 1 sensor per zone */
698 ret = of_parse_phandle_with_args(child, "thermal-sensors",
699 "#thermal-sensor-cells",
700 0, &sensor_specs);
701 if (ret)
702 continue;
703
704 if (sensor_specs.args_count >= 1) {
705 id = sensor_specs.args[0];
706 WARN(sensor_specs.args_count > 1,
707 "%s: too many cells in sensor specifier %d\n",
708 sensor_specs.np->name, sensor_specs.args_count);
709 } else {
710 id = 0;
711 }
712
713 if (sensor_specs.np == sensor_np && id == sensor_id) {
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300714 tzd = thermal_zone_of_add_sensor(child, sensor_np,
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700715 sens_param);
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700716 if (!IS_ERR(tzd)) {
717 if (!first_tzd)
718 first_tzd = tzd;
Lukasz Majewski528012c12015-01-19 12:44:04 +0100719 tzd->ops->set_mode(tzd, THERMAL_DEVICE_ENABLED);
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700720 }
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400721 }
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300722 of_node_put(sensor_specs.np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400723 }
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +0300724 of_node_put(sensor_np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400725 of_node_put(np);
726
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700727 if (!first_tzd) {
728 first_tzd = ERR_PTR(-ENODEV);
Ram Chandrasekare34ced62017-03-03 11:22:50 -0700729 kfree(sens_param);
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700730 }
731 return first_tzd;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400732}
733EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_register);
734
735/**
736 * thermal_zone_of_sensor_unregister - unregisters a sensor from a DT thermal zone
737 * @dev: a valid struct device pointer of a sensor device. Must contain
738 * a valid .of_node, for the sensor node.
739 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
740 *
741 * This function removes the sensor callbacks and private data from the
742 * thermal zone device registered with thermal_zone_of_sensor_register()
743 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
744 * thermal zone device callbacks.
745 *
746 * TODO: When the support to several sensors per zone is added, this
747 * function must search the sensor list based on @dev parameter.
748 *
749 */
750void thermal_zone_of_sensor_unregister(struct device *dev,
751 struct thermal_zone_device *tzd)
752{
Ram Chandrasekar9a1da902017-05-09 16:58:55 -0600753 struct __thermal_zone *tz, *next;
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700754 struct thermal_zone_device *pos;
755 struct list_head *head;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400756
757 if (!dev || !tzd || !tzd->devdata)
758 return;
759
760 tz = tzd->devdata;
761
762 /* no __thermal_zone, nothing to be done */
763 if (!tz)
764 return;
765
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700766 head = &tz->senps->first_tz;
Ram Chandrasekar9a1da902017-05-09 16:58:55 -0600767 list_for_each_entry_safe(tz, next, head, list) {
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700768 pos = tz->tzd;
769 mutex_lock(&pos->lock);
770 pos->ops->get_temp = NULL;
771 pos->ops->get_trend = NULL;
772 pos->ops->set_emul_temp = NULL;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400773
Ram Chandrasekard29230b2017-02-27 11:26:51 -0700774 list_del(&tz->list);
775 if (list_empty(&tz->senps->first_tz))
776 kfree(tz->senps);
777 tz->senps = NULL;
778 mutex_unlock(&pos->lock);
779 }
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400780}
781EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister);
782
Laxman Dewangane498b492016-03-09 18:40:06 +0530783static void devm_thermal_zone_of_sensor_release(struct device *dev, void *res)
784{
785 thermal_zone_of_sensor_unregister(dev,
786 *(struct thermal_zone_device **)res);
787}
788
789static int devm_thermal_zone_of_sensor_match(struct device *dev, void *res,
790 void *data)
791{
792 struct thermal_zone_device **r = res;
793
794 if (WARN_ON(!r || !*r))
795 return 0;
796
797 return *r == data;
798}
799
800/**
Ram Chandrasekar2f2b7462017-03-11 19:35:11 -0700801 * devm_thermal_of_virtual_sensor_register - Register a virtual sensor.
802 * Three types of virtual sensors are supported.
803 * 1. Weighted aggregation type:
804 * Virtual sensor of this type calculates the weighted aggregation
805 * of sensor temperatures using the below formula,
806 * temp = (sensor_1_temp * coeff_1 + ... + sensor_n_temp * coeff_n)
807 * + avg_offset / avg_denominator
808 * So the sensor drivers has to specify n+2 coefficients.
809 * 2. Maximum type:
810 * Virtual sensors of this type will report the maximum of all
811 * sensor temperatures.
812 * 3. Minimum type:
813 * Virtual sensors of this type will report the minimum of all
814 * sensor temperatures.
815 *
816 * @input arguments:
817 * @dev: Virtual sensor driver device pointer.
818 * @sensor_data: Virtual sensor data supported for the device.
819 *
820 * @return: Returns a virtual thermal zone pointer. Returns error if thermal
821 * zone is not created. Returns -EAGAIN, if the sensor that is required for
822 * this virtual sensor temperature estimation is not registered yet. The
823 * sensor driver can try again later.
824 */
825struct thermal_zone_device *devm_thermal_of_virtual_sensor_register(
826 struct device *dev,
827 const struct virtual_sensor_data *sensor_data)
828{
829 int sens_idx = 0;
830 struct virtual_sensor *sens;
831 struct __thermal_zone *tz;
832 struct thermal_zone_device **ptr;
833 struct thermal_zone_device *tzd;
834 struct __sensor_param *sens_param = NULL;
835 enum thermal_device_mode mode;
836
837 if (!dev || !sensor_data)
838 return ERR_PTR(-EINVAL);
839
840 tzd = thermal_zone_get_zone_by_name(
841 sensor_data->virt_zone_name);
842 if (IS_ERR(tzd)) {
843 dev_err(dev, "sens:%s not available err: %ld\n",
844 sensor_data->virt_zone_name,
845 PTR_ERR(tzd));
846 return tzd;
847 }
848
849 mutex_lock(&tzd->lock);
850 /*
851 * Check if the virtual zone is registered and enabled.
852 * If so return the registered thermal zone.
853 */
854 tzd->ops->get_mode(tzd, &mode);
855 mutex_unlock(&tzd->lock);
856 if (mode == THERMAL_DEVICE_ENABLED)
857 return tzd;
858
859 sens = devm_kzalloc(dev, sizeof(*sens), GFP_KERNEL);
860 if (!sens)
861 return ERR_PTR(-ENOMEM);
862
863 sens->logic = sensor_data->logic;
864 sens->num_sensors = sensor_data->num_sensors;
865 if (sens->logic == VIRT_WEIGHTED_AVG) {
866 int coeff_ct = sensor_data->coefficient_ct;
867
868 /*
869 * For weighted aggregation, sensor drivers has to specify
870 * n+2 coefficients.
871 */
872 if (coeff_ct != sens->num_sensors) {
873 dev_err(dev, "sens:%s Invalid coefficient\n",
874 sensor_data->virt_zone_name);
875 return ERR_PTR(-EINVAL);
876 }
877 memcpy(sens->coefficients, sensor_data->coefficients,
878 coeff_ct * sizeof(*sens->coefficients));
879 sens->avg_offset = sensor_data->avg_offset;
880 sens->avg_denominator = sensor_data->avg_denominator;
881 }
882
883 for (sens_idx = 0; sens_idx < sens->num_sensors; sens_idx++) {
884 sens->tz[sens_idx] = thermal_zone_get_zone_by_name(
885 sensor_data->sensor_names[sens_idx]);
886 if (IS_ERR(sens->tz[sens_idx])) {
887 dev_err(dev, "sens:%s sensor[%s] fetch err:%ld\n",
888 sensor_data->virt_zone_name,
889 sensor_data->sensor_names[sens_idx],
890 PTR_ERR(sens->tz[sens_idx]));
891 break;
892 }
893 }
894 if (sens->num_sensors != sens_idx)
895 return ERR_PTR(-EAGAIN);
896
897 sens_param = kzalloc(sizeof(*sens_param), GFP_KERNEL);
898 if (!sens_param)
899 return ERR_PTR(-ENOMEM);
900 sens_param->sensor_data = sens;
901 sens_param->ops = &of_virt_ops;
902 INIT_LIST_HEAD(&sens_param->first_tz);
903 sens_param->trip_high = INT_MAX;
904 sens_param->trip_low = INT_MIN;
905 mutex_init(&sens_param->lock);
906
907 mutex_lock(&tzd->lock);
908 tz = tzd->devdata;
909 tz->senps = sens_param;
910 tzd->ops->get_temp = of_thermal_get_temp;
911 list_add_tail(&tz->list, &sens_param->first_tz);
912 mutex_unlock(&tzd->lock);
913
914 ptr = devres_alloc(devm_thermal_zone_of_sensor_release, sizeof(*ptr),
915 GFP_KERNEL);
916 if (!ptr)
917 return ERR_PTR(-ENOMEM);
918
919 *ptr = tzd;
920 devres_add(dev, ptr);
921
922 tzd->ops->set_mode(tzd, THERMAL_DEVICE_ENABLED);
923
924 return tzd;
925}
926EXPORT_SYMBOL(devm_thermal_of_virtual_sensor_register);
927
928/**
Laxman Dewangane498b492016-03-09 18:40:06 +0530929 * devm_thermal_zone_of_sensor_register - Resource managed version of
930 * thermal_zone_of_sensor_register()
931 * @dev: a valid struct device pointer of a sensor device. Must contain
932 * a valid .of_node, for the sensor node.
933 * @sensor_id: a sensor identifier, in case the sensor IP has more
934 * than one sensors
935 * @data: a private pointer (owned by the caller) that will be passed
936 * back, when a temperature reading is needed.
937 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
938 *
939 * Refer thermal_zone_of_sensor_register() for more details.
940 *
941 * Return: On success returns a valid struct thermal_zone_device,
942 * otherwise, it returns a corresponding ERR_PTR(). Caller must
943 * check the return value with help of IS_ERR() helper.
Zhang Rui7b5c4a02016-08-22 15:48:11 +0800944 * Registered thermal_zone_device device will automatically be
Laxman Dewangane498b492016-03-09 18:40:06 +0530945 * released when device is unbounded.
946 */
947struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
948 struct device *dev, int sensor_id,
949 void *data, const struct thermal_zone_of_device_ops *ops)
950{
951 struct thermal_zone_device **ptr, *tzd;
952
953 ptr = devres_alloc(devm_thermal_zone_of_sensor_release, sizeof(*ptr),
954 GFP_KERNEL);
955 if (!ptr)
956 return ERR_PTR(-ENOMEM);
957
958 tzd = thermal_zone_of_sensor_register(dev, sensor_id, data, ops);
959 if (IS_ERR(tzd)) {
960 devres_free(ptr);
961 return tzd;
962 }
963
964 *ptr = tzd;
965 devres_add(dev, ptr);
966
967 return tzd;
968}
969EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_register);
970
971/**
972 * devm_thermal_zone_of_sensor_unregister - Resource managed version of
973 * thermal_zone_of_sensor_unregister().
974 * @dev: Device for which which resource was allocated.
975 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
976 *
977 * This function removes the sensor callbacks and private data from the
978 * thermal zone device registered with devm_thermal_zone_of_sensor_register()
979 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
980 * thermal zone device callbacks.
981 * Normally this function will not need to be called and the resource
982 * management code will ensure that the resource is freed.
983 */
984void devm_thermal_zone_of_sensor_unregister(struct device *dev,
985 struct thermal_zone_device *tzd)
986{
987 WARN_ON(devres_release(dev, devm_thermal_zone_of_sensor_release,
988 devm_thermal_zone_of_sensor_match, tzd));
989}
990EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_unregister);
991
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400992/*** functions parsing device tree nodes ***/
993
994/**
995 * thermal_of_populate_bind_params - parse and fill cooling map data
996 * @np: DT node containing a cooling-map node
997 * @__tbp: data structure to be filled with cooling map info
998 * @trips: array of thermal zone trip points
999 * @ntrips: number of trip points inside trips.
1000 *
1001 * This function parses a cooling-map type of node represented by
1002 * @np parameter and fills the read data into @__tbp data structure.
1003 * It needs the already parsed array of trip points of the thermal zone
1004 * in consideration.
1005 *
1006 * Return: 0 on success, proper error code otherwise
1007 */
1008static int thermal_of_populate_bind_params(struct device_node *np,
1009 struct __thermal_bind_params *__tbp,
Lukasz Majewskiad9914a2014-12-08 18:04:19 +01001010 struct thermal_trip *trips,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001011 int ntrips)
1012{
1013 struct of_phandle_args cooling_spec;
1014 struct device_node *trip;
1015 int ret, i;
1016 u32 prop;
1017
1018 /* Default weight. Usage is optional */
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +00001019 __tbp->usage = THERMAL_WEIGHT_DEFAULT;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001020 ret = of_property_read_u32(np, "contribution", &prop);
1021 if (ret == 0)
1022 __tbp->usage = prop;
1023
1024 trip = of_parse_phandle(np, "trip", 0);
1025 if (!trip) {
1026 pr_err("missing trip property\n");
1027 return -ENODEV;
1028 }
1029
1030 /* match using device_node */
1031 for (i = 0; i < ntrips; i++)
1032 if (trip == trips[i].np) {
1033 __tbp->trip_id = i;
1034 break;
1035 }
1036
1037 if (i == ntrips) {
1038 ret = -ENODEV;
1039 goto end;
1040 }
1041
1042 ret = of_parse_phandle_with_args(np, "cooling-device", "#cooling-cells",
1043 0, &cooling_spec);
1044 if (ret < 0) {
1045 pr_err("missing cooling_device property\n");
1046 goto end;
1047 }
1048 __tbp->cooling_device = cooling_spec.np;
1049 if (cooling_spec.args_count >= 2) { /* at least min and max */
1050 __tbp->min = cooling_spec.args[0];
1051 __tbp->max = cooling_spec.args[1];
1052 } else {
1053 pr_err("wrong reference to cooling device, missing limits\n");
1054 }
1055
1056end:
1057 of_node_put(trip);
1058
1059 return ret;
1060}
1061
1062/**
1063 * It maps 'enum thermal_trip_type' found in include/linux/thermal.h
1064 * into the device tree binding of 'trip', property type.
1065 */
1066static const char * const trip_types[] = {
1067 [THERMAL_TRIP_ACTIVE] = "active",
1068 [THERMAL_TRIP_PASSIVE] = "passive",
1069 [THERMAL_TRIP_HOT] = "hot",
1070 [THERMAL_TRIP_CRITICAL] = "critical",
1071};
1072
1073/**
1074 * thermal_of_get_trip_type - Get phy mode for given device_node
1075 * @np: Pointer to the given device_node
1076 * @type: Pointer to resulting trip type
1077 *
1078 * The function gets trip type string from property 'type',
1079 * and store its index in trip_types table in @type,
1080 *
1081 * Return: 0 on success, or errno in error case.
1082 */
1083static int thermal_of_get_trip_type(struct device_node *np,
1084 enum thermal_trip_type *type)
1085{
1086 const char *t;
1087 int err, i;
1088
1089 err = of_property_read_string(np, "type", &t);
1090 if (err < 0)
1091 return err;
1092
1093 for (i = 0; i < ARRAY_SIZE(trip_types); i++)
1094 if (!strcasecmp(t, trip_types[i])) {
1095 *type = i;
1096 return 0;
1097 }
1098
1099 return -ENODEV;
1100}
1101
1102/**
1103 * thermal_of_populate_trip - parse and fill one trip point data
1104 * @np: DT node containing a trip point node
1105 * @trip: trip point data structure to be filled up
1106 *
1107 * This function parses a trip point type of node represented by
1108 * @np parameter and fills the read data into @trip data structure.
1109 *
1110 * Return: 0 on success, proper error code otherwise
1111 */
1112static int thermal_of_populate_trip(struct device_node *np,
Lukasz Majewskiad9914a2014-12-08 18:04:19 +01001113 struct thermal_trip *trip)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001114{
1115 int prop;
1116 int ret;
1117
1118 ret = of_property_read_u32(np, "temperature", &prop);
1119 if (ret < 0) {
1120 pr_err("missing temperature property\n");
1121 return ret;
1122 }
1123 trip->temperature = prop;
1124
1125 ret = of_property_read_u32(np, "hysteresis", &prop);
1126 if (ret < 0) {
1127 pr_err("missing hysteresis property\n");
1128 return ret;
1129 }
1130 trip->hysteresis = prop;
1131
1132 ret = thermal_of_get_trip_type(np, &trip->type);
1133 if (ret < 0) {
1134 pr_err("wrong trip type property\n");
1135 return ret;
1136 }
1137
1138 /* Required for cooling map matching */
1139 trip->np = np;
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001140 of_node_get(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001141
1142 return 0;
1143}
1144
1145/**
1146 * thermal_of_build_thermal_zone - parse and fill one thermal zone data
1147 * @np: DT node containing a thermal zone node
1148 *
1149 * This function parses a thermal zone type of node represented by
1150 * @np parameter and fills the read data into a __thermal_zone data structure
1151 * and return this pointer.
1152 *
Eduardo Valentina46dbae2015-05-11 19:48:09 -07001153 * TODO: Missing properties to parse: thermal-sensor-names
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001154 *
1155 * Return: On success returns a valid struct __thermal_zone,
1156 * otherwise, it returns a corresponding ERR_PTR(). Caller must
1157 * check the return value with help of IS_ERR() helper.
1158 */
Julia Lawallc0ff8aa2016-04-19 14:33:32 +02001159static struct __thermal_zone
1160__init *thermal_of_build_thermal_zone(struct device_node *np)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001161{
1162 struct device_node *child = NULL, *gchild;
1163 struct __thermal_zone *tz;
1164 int ret, i;
Eduardo Valentina46dbae2015-05-11 19:48:09 -07001165 u32 prop, coef[2];
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001166
1167 if (!np) {
1168 pr_err("no thermal zone np\n");
1169 return ERR_PTR(-EINVAL);
1170 }
1171
1172 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
1173 if (!tz)
1174 return ERR_PTR(-ENOMEM);
1175
Ram Chandrasekard29230b2017-02-27 11:26:51 -07001176 INIT_LIST_HEAD(&tz->list);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001177 ret = of_property_read_u32(np, "polling-delay-passive", &prop);
1178 if (ret < 0) {
1179 pr_err("missing polling-delay-passive property\n");
1180 goto free_tz;
1181 }
1182 tz->passive_delay = prop;
1183
1184 ret = of_property_read_u32(np, "polling-delay", &prop);
1185 if (ret < 0) {
1186 pr_err("missing polling-delay property\n");
1187 goto free_tz;
1188 }
1189 tz->polling_delay = prop;
1190
Eduardo Valentina46dbae2015-05-11 19:48:09 -07001191 /*
1192 * REVIST: for now, the thermal framework supports only
1193 * one sensor per thermal zone. Thus, we are considering
1194 * only the first two values as slope and offset.
1195 */
1196 ret = of_property_read_u32_array(np, "coefficients", coef, 2);
1197 if (ret == 0) {
1198 tz->slope = coef[0];
1199 tz->offset = coef[1];
1200 } else {
1201 tz->slope = 1;
1202 tz->offset = 0;
1203 }
1204
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001205 /* trips */
1206 child = of_get_child_by_name(np, "trips");
1207
1208 /* No trips provided */
1209 if (!child)
1210 goto finish;
1211
1212 tz->ntrips = of_get_child_count(child);
1213 if (tz->ntrips == 0) /* must have at least one child */
1214 goto finish;
1215
1216 tz->trips = kzalloc(tz->ntrips * sizeof(*tz->trips), GFP_KERNEL);
1217 if (!tz->trips) {
1218 ret = -ENOMEM;
1219 goto free_tz;
1220 }
1221
1222 i = 0;
1223 for_each_child_of_node(child, gchild) {
1224 ret = thermal_of_populate_trip(gchild, &tz->trips[i++]);
1225 if (ret)
1226 goto free_trips;
1227 }
1228
1229 of_node_put(child);
1230
1231 /* cooling-maps */
1232 child = of_get_child_by_name(np, "cooling-maps");
1233
1234 /* cooling-maps not provided */
1235 if (!child)
1236 goto finish;
1237
1238 tz->num_tbps = of_get_child_count(child);
1239 if (tz->num_tbps == 0)
1240 goto finish;
1241
1242 tz->tbps = kzalloc(tz->num_tbps * sizeof(*tz->tbps), GFP_KERNEL);
1243 if (!tz->tbps) {
1244 ret = -ENOMEM;
1245 goto free_trips;
1246 }
1247
1248 i = 0;
Stephen Boydca9521b2014-06-18 16:32:08 -07001249 for_each_child_of_node(child, gchild) {
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001250 ret = thermal_of_populate_bind_params(gchild, &tz->tbps[i++],
1251 tz->trips, tz->ntrips);
1252 if (ret)
1253 goto free_tbps;
Stephen Boydca9521b2014-06-18 16:32:08 -07001254 }
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001255
1256finish:
1257 of_node_put(child);
1258 tz->mode = THERMAL_DEVICE_DISABLED;
1259
1260 return tz;
1261
1262free_tbps:
Ulises Brindis1cd91c12016-03-25 12:55:41 -07001263 for (i = i - 1; i >= 0; i--)
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001264 of_node_put(tz->tbps[i].cooling_device);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001265 kfree(tz->tbps);
1266free_trips:
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001267 for (i = 0; i < tz->ntrips; i++)
1268 of_node_put(tz->trips[i].np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001269 kfree(tz->trips);
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001270 of_node_put(gchild);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001271free_tz:
1272 kfree(tz);
1273 of_node_put(child);
1274
1275 return ERR_PTR(ret);
1276}
1277
1278static inline void of_thermal_free_zone(struct __thermal_zone *tz)
1279{
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001280 int i;
1281
1282 for (i = 0; i < tz->num_tbps; i++)
1283 of_node_put(tz->tbps[i].cooling_device);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001284 kfree(tz->tbps);
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001285 for (i = 0; i < tz->ntrips; i++)
1286 of_node_put(tz->trips[i].np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001287 kfree(tz->trips);
1288 kfree(tz);
1289}
1290
1291/**
1292 * of_parse_thermal_zones - parse device tree thermal data
1293 *
1294 * Initialization function that can be called by machine initialization
1295 * code to parse thermal data and populate the thermal framework
1296 * with hardware thermal zones info. This function only parses thermal zones.
1297 * Cooling devices and sensor devices nodes are supposed to be parsed
1298 * by their respective drivers.
1299 *
1300 * Return: 0 on success, proper error code otherwise
1301 *
1302 */
1303int __init of_parse_thermal_zones(void)
1304{
1305 struct device_node *np, *child;
1306 struct __thermal_zone *tz;
1307 struct thermal_zone_device_ops *ops;
1308
1309 np = of_find_node_by_name(NULL, "thermal-zones");
1310 if (!np) {
1311 pr_debug("unable to find thermal zones\n");
1312 return 0; /* Run successfully on systems without thermal DT */
1313 }
1314
Laxman Dewangan42bbe402016-02-08 18:58:34 +05301315 for_each_available_child_of_node(np, child) {
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001316 struct thermal_zone_device *zone;
1317 struct thermal_zone_params *tzp;
Punit Agrawal76af5492015-03-03 10:43:04 +00001318 int i, mask = 0;
Punit Agrawal647f9922015-02-26 19:00:32 +00001319 u32 prop;
Ram Chandrasekarcaafe202016-07-19 11:25:46 -06001320 const char *governor_name;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001321
1322 tz = thermal_of_build_thermal_zone(child);
1323 if (IS_ERR(tz)) {
1324 pr_err("failed to build thermal zone %s: %ld\n",
1325 child->name,
1326 PTR_ERR(tz));
1327 continue;
1328 }
1329
1330 ops = kmemdup(&of_thermal_ops, sizeof(*ops), GFP_KERNEL);
1331 if (!ops)
1332 goto exit_free;
1333
1334 tzp = kzalloc(sizeof(*tzp), GFP_KERNEL);
1335 if (!tzp) {
1336 kfree(ops);
1337 goto exit_free;
1338 }
1339
1340 /* No hwmon because there might be hwmon drivers registering */
1341 tzp->no_hwmon = true;
1342
Ram Chandrasekarcaafe202016-07-19 11:25:46 -06001343 if (!of_property_read_string(child, "thermal-governor",
1344 &governor_name))
1345 strlcpy(tzp->governor_name, governor_name,
1346 THERMAL_NAME_LENGTH);
1347
Punit Agrawal647f9922015-02-26 19:00:32 +00001348 if (!of_property_read_u32(child, "sustainable-power", &prop))
1349 tzp->sustainable_power = prop;
1350
Punit Agrawal76af5492015-03-03 10:43:04 +00001351 for (i = 0; i < tz->ntrips; i++)
1352 mask |= 1 << i;
1353
Eduardo Valentina46dbae2015-05-11 19:48:09 -07001354 /* these two are left for temperature drivers to use */
1355 tzp->slope = tz->slope;
1356 tzp->offset = tz->offset;
1357
Lina Iyer159f67d2016-07-27 11:34:46 -06001358 if (of_property_read_bool(child, "tracks-low"))
1359 tzp->tracks_low = true;
1360
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001361 zone = thermal_zone_device_register(child->name, tz->ntrips,
Punit Agrawal76af5492015-03-03 10:43:04 +00001362 mask, tz,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001363 ops, tzp,
1364 tz->passive_delay,
1365 tz->polling_delay);
1366 if (IS_ERR(zone)) {
1367 pr_err("Failed to build %s zone %ld\n", child->name,
1368 PTR_ERR(zone));
1369 kfree(tzp);
1370 kfree(ops);
1371 of_thermal_free_zone(tz);
1372 /* attempting to build remaining zones still */
Ram Chandrasekard29230b2017-02-27 11:26:51 -07001373 continue;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001374 }
Ram Chandrasekard29230b2017-02-27 11:26:51 -07001375 tz->tzd = zone;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001376 }
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001377 of_node_put(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001378
1379 return 0;
1380
1381exit_free:
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001382 of_node_put(child);
1383 of_node_put(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001384 of_thermal_free_zone(tz);
1385
1386 /* no memory available, so free what we have built */
1387 of_thermal_destroy_zones();
1388
1389 return -ENOMEM;
1390}
1391
1392/**
1393 * of_thermal_destroy_zones - remove all zones parsed and allocated resources
1394 *
1395 * Finds all zones parsed and added to the thermal framework and remove them
1396 * from the system, together with their resources.
1397 *
1398 */
1399void of_thermal_destroy_zones(void)
1400{
1401 struct device_node *np, *child;
1402
1403 np = of_find_node_by_name(NULL, "thermal-zones");
1404 if (!np) {
Jiada Wang28524982015-11-16 17:10:05 +09001405 pr_debug("unable to find thermal zones\n");
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001406 return;
1407 }
1408
Laxman Dewangan42bbe402016-02-08 18:58:34 +05301409 for_each_available_child_of_node(np, child) {
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001410 struct thermal_zone_device *zone;
1411
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001412 zone = thermal_zone_get_zone_by_name(child->name);
1413 if (IS_ERR(zone))
1414 continue;
1415
1416 thermal_zone_device_unregister(zone);
1417 kfree(zone->tzp);
1418 kfree(zone->ops);
1419 of_thermal_free_zone(zone->devdata);
1420 }
Vladimir Zapolskiyc2aad93c2014-09-29 02:47:46 +03001421 of_node_put(np);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001422}