1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * of-thermal.c - Generic Thermal Management device tree support.
4 *
5 * Copyright (C) 2013 Texas Instruments
6 * Copyright (C) 2013 Eduardo Valentin <eduardo.valentin@ti.com>
7 */
8
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11 #include <linux/thermal.h>
12 #include <linux/slab.h>
13 #include <linux/types.h>
14 #include <linux/of_device.h>
15 #include <linux/of_platform.h>
16 #include <linux/err.h>
17 #include <linux/export.h>
18 #include <linux/string.h>
19
20 #include "thermal_core.h"
21
22 /*** Private data structures to represent thermal device tree data ***/
23
24 /**
25 * struct __thermal_cooling_bind_param - a cooling device for a trip point
26 * @cooling_device: a pointer to identify the referred cooling device
27 * @min: minimum cooling state used at this trip point
28 * @max: maximum cooling state used at this trip point
29 */
30
31 struct __thermal_cooling_bind_param {
32 struct device_node *cooling_device;
33 unsigned long min;
34 unsigned long max;
35 };
36
37 /**
38 * struct __thermal_bind_param - a match between trip and cooling device
39 * @tcbp: a pointer to an array of cooling devices
40 * @count: number of elements in array
41 * @trip_id: the trip point index
42 * @usage: the percentage (from 0 to 100) of cooling contribution
43 */
44
45 struct __thermal_bind_params {
46 struct __thermal_cooling_bind_param *tcbp;
47 unsigned int count;
48 unsigned int trip_id;
49 unsigned int usage;
50 };
51
52 /**
53 * struct __thermal_zone - internal representation of a thermal zone
54 * @mode: current thermal zone device mode (enabled/disabled)
55 * @passive_delay: polling interval while passive cooling is activated
56 * @polling_delay: zone polling interval
57 * @slope: slope of the temperature adjustment curve
58 * @offset: offset of the temperature adjustment curve
59 * @ntrips: number of trip points
60 * @trips: an array of trip points (0..ntrips - 1)
61 * @num_tbps: number of thermal bind params
62 * @tbps: an array of thermal bind params (0..num_tbps - 1)
63 * @sensor_data: sensor private data used while reading temperature and trend
64 * @ops: set of callbacks to handle the thermal zone based on DT
65 */
66
67 struct __thermal_zone {
68 enum thermal_device_mode mode;
69 int passive_delay;
70 int polling_delay;
71 int slope;
72 int offset;
73
74 /* trip data */
75 int ntrips;
76 struct thermal_trip *trips;
77
78 /* cooling binding data */
79 int num_tbps;
80 struct __thermal_bind_params *tbps;
81
82 /* sensor interface */
83 void *sensor_data;
84 const struct thermal_zone_of_device_ops *ops;
85 };
86
87 /*** DT thermal zone device callbacks ***/
88
of_thermal_get_temp(struct thermal_zone_device * tz,int * temp)89 static int of_thermal_get_temp(struct thermal_zone_device *tz,
90 int *temp)
91 {
92 struct __thermal_zone *data = tz->devdata;
93
94 if (!data->ops || !data->ops->get_temp)
95 return -EINVAL;
96
97 return data->ops->get_temp(data->sensor_data, temp);
98 }
99
of_thermal_set_trips(struct thermal_zone_device * tz,int low,int high)100 static int of_thermal_set_trips(struct thermal_zone_device *tz,
101 int low, int high)
102 {
103 struct __thermal_zone *data = tz->devdata;
104
105 if (!data->ops || !data->ops->set_trips)
106 return -EINVAL;
107
108 return data->ops->set_trips(data->sensor_data, low, high);
109 }
110
111 /**
112 * of_thermal_get_ntrips - function to export number of available trip
113 * points.
114 * @tz: pointer to a thermal zone
115 *
116 * This function is a globally visible wrapper to get number of trip points
117 * stored in the local struct __thermal_zone
118 *
119 * Return: number of available trip points, -ENODEV when data not available
120 */
of_thermal_get_ntrips(struct thermal_zone_device * tz)121 int of_thermal_get_ntrips(struct thermal_zone_device *tz)
122 {
123 struct __thermal_zone *data = tz->devdata;
124
125 if (!data || IS_ERR(data))
126 return -ENODEV;
127
128 return data->ntrips;
129 }
130 EXPORT_SYMBOL_GPL(of_thermal_get_ntrips);
131
132 /**
133 * of_thermal_is_trip_valid - function to check if trip point is valid
134 *
135 * @tz: pointer to a thermal zone
136 * @trip: trip point to evaluate
137 *
138 * This function is responsible for checking if passed trip point is valid
139 *
140 * Return: true if trip point is valid, false otherwise
141 */
of_thermal_is_trip_valid(struct thermal_zone_device * tz,int trip)142 bool of_thermal_is_trip_valid(struct thermal_zone_device *tz, int trip)
143 {
144 struct __thermal_zone *data = tz->devdata;
145
146 if (!data || trip >= data->ntrips || trip < 0)
147 return false;
148
149 return true;
150 }
151 EXPORT_SYMBOL_GPL(of_thermal_is_trip_valid);
152
153 /**
154 * of_thermal_get_trip_points - function to get access to a globally exported
155 * trip points
156 *
157 * @tz: pointer to a thermal zone
158 *
159 * This function provides a pointer to trip points table
160 *
161 * Return: pointer to trip points table, NULL otherwise
162 */
163 const struct thermal_trip *
of_thermal_get_trip_points(struct thermal_zone_device * tz)164 of_thermal_get_trip_points(struct thermal_zone_device *tz)
165 {
166 struct __thermal_zone *data = tz->devdata;
167
168 if (!data)
169 return NULL;
170
171 return data->trips;
172 }
173 EXPORT_SYMBOL_GPL(of_thermal_get_trip_points);
174
175 /**
176 * of_thermal_set_emul_temp - function to set emulated temperature
177 *
178 * @tz: pointer to a thermal zone
179 * @temp: temperature to set
180 *
181 * This function gives the ability to set emulated value of temperature,
182 * which is handy for debugging
183 *
184 * Return: zero on success, error code otherwise
185 */
of_thermal_set_emul_temp(struct thermal_zone_device * tz,int temp)186 static int of_thermal_set_emul_temp(struct thermal_zone_device *tz,
187 int temp)
188 {
189 struct __thermal_zone *data = tz->devdata;
190
191 if (!data->ops || !data->ops->set_emul_temp)
192 return -EINVAL;
193
194 return data->ops->set_emul_temp(data->sensor_data, temp);
195 }
196
of_thermal_get_trend(struct thermal_zone_device * tz,int trip,enum thermal_trend * trend)197 static int of_thermal_get_trend(struct thermal_zone_device *tz, int trip,
198 enum thermal_trend *trend)
199 {
200 struct __thermal_zone *data = tz->devdata;
201
202 if (!data->ops || !data->ops->get_trend)
203 return -EINVAL;
204
205 return data->ops->get_trend(data->sensor_data, trip, trend);
206 }
207
of_thermal_bind(struct thermal_zone_device * thermal,struct thermal_cooling_device * cdev)208 static int of_thermal_bind(struct thermal_zone_device *thermal,
209 struct thermal_cooling_device *cdev)
210 {
211 struct __thermal_zone *data = thermal->devdata;
212 struct __thermal_bind_params *tbp;
213 struct __thermal_cooling_bind_param *tcbp;
214 int i, j;
215
216 if (!data || IS_ERR(data))
217 return -ENODEV;
218
219 /* find where to bind */
220 for (i = 0; i < data->num_tbps; i++) {
221 tbp = data->tbps + i;
222
223 for (j = 0; j < tbp->count; j++) {
224 tcbp = tbp->tcbp + j;
225
226 if (tcbp->cooling_device == cdev->np) {
227 int ret;
228
229 ret = thermal_zone_bind_cooling_device(thermal,
230 tbp->trip_id, cdev,
231 tcbp->max,
232 tcbp->min,
233 tbp->usage);
234 if (ret)
235 return ret;
236 }
237 }
238 }
239
240 return 0;
241 }
242
of_thermal_unbind(struct thermal_zone_device * thermal,struct thermal_cooling_device * cdev)243 static int of_thermal_unbind(struct thermal_zone_device *thermal,
244 struct thermal_cooling_device *cdev)
245 {
246 struct __thermal_zone *data = thermal->devdata;
247 struct __thermal_bind_params *tbp;
248 struct __thermal_cooling_bind_param *tcbp;
249 int i, j;
250
251 if (!data || IS_ERR(data))
252 return -ENODEV;
253
254 /* find where to unbind */
255 for (i = 0; i < data->num_tbps; i++) {
256 tbp = data->tbps + i;
257
258 for (j = 0; j < tbp->count; j++) {
259 tcbp = tbp->tcbp + j;
260
261 if (tcbp->cooling_device == cdev->np) {
262 int ret;
263
264 ret = thermal_zone_unbind_cooling_device(thermal,
265 tbp->trip_id, cdev);
266 if (ret)
267 return ret;
268 }
269 }
270 }
271
272 return 0;
273 }
274
of_thermal_get_mode(struct thermal_zone_device * tz,enum thermal_device_mode * mode)275 static int of_thermal_get_mode(struct thermal_zone_device *tz,
276 enum thermal_device_mode *mode)
277 {
278 struct __thermal_zone *data = tz->devdata;
279
280 *mode = data->mode;
281
282 return 0;
283 }
284
of_thermal_set_mode(struct thermal_zone_device * tz,enum thermal_device_mode mode)285 static int of_thermal_set_mode(struct thermal_zone_device *tz,
286 enum thermal_device_mode mode)
287 {
288 struct __thermal_zone *data = tz->devdata;
289
290 mutex_lock(&tz->lock);
291
292 if (mode == THERMAL_DEVICE_ENABLED) {
293 tz->polling_delay = data->polling_delay;
294 tz->passive_delay = data->passive_delay;
295 } else {
296 tz->polling_delay = 0;
297 tz->passive_delay = 0;
298 }
299
300 mutex_unlock(&tz->lock);
301
302 data->mode = mode;
303 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
304
305 return 0;
306 }
307
of_thermal_get_trip_type(struct thermal_zone_device * tz,int trip,enum thermal_trip_type * type)308 static int of_thermal_get_trip_type(struct thermal_zone_device *tz, int trip,
309 enum thermal_trip_type *type)
310 {
311 struct __thermal_zone *data = tz->devdata;
312
313 if (trip >= data->ntrips || trip < 0)
314 return -EDOM;
315
316 *type = data->trips[trip].type;
317
318 return 0;
319 }
320
of_thermal_get_trip_temp(struct thermal_zone_device * tz,int trip,int * temp)321 static int of_thermal_get_trip_temp(struct thermal_zone_device *tz, int trip,
322 int *temp)
323 {
324 struct __thermal_zone *data = tz->devdata;
325
326 if (trip >= data->ntrips || trip < 0)
327 return -EDOM;
328
329 *temp = data->trips[trip].temperature;
330
331 return 0;
332 }
333
of_thermal_set_trip_temp(struct thermal_zone_device * tz,int trip,int temp)334 static int of_thermal_set_trip_temp(struct thermal_zone_device *tz, int trip,
335 int temp)
336 {
337 struct __thermal_zone *data = tz->devdata;
338
339 if (trip >= data->ntrips || trip < 0)
340 return -EDOM;
341
342 if (data->ops && data->ops->set_trip_temp) {
343 int ret;
344
345 ret = data->ops->set_trip_temp(data->sensor_data, trip, temp);
346 if (ret)
347 return ret;
348 }
349
350 /* thermal framework should take care of data->mask & (1 << trip) */
351 data->trips[trip].temperature = temp;
352
353 return 0;
354 }
355
of_thermal_get_trip_hyst(struct thermal_zone_device * tz,int trip,int * hyst)356 static int of_thermal_get_trip_hyst(struct thermal_zone_device *tz, int trip,
357 int *hyst)
358 {
359 struct __thermal_zone *data = tz->devdata;
360
361 if (trip >= data->ntrips || trip < 0)
362 return -EDOM;
363
364 *hyst = data->trips[trip].hysteresis;
365
366 return 0;
367 }
368
of_thermal_set_trip_hyst(struct thermal_zone_device * tz,int trip,int hyst)369 static int of_thermal_set_trip_hyst(struct thermal_zone_device *tz, int trip,
370 int hyst)
371 {
372 struct __thermal_zone *data = tz->devdata;
373
374 if (trip >= data->ntrips || trip < 0)
375 return -EDOM;
376
377 /* thermal framework should take care of data->mask & (1 << trip) */
378 data->trips[trip].hysteresis = hyst;
379
380 return 0;
381 }
382
of_thermal_get_crit_temp(struct thermal_zone_device * tz,int * temp)383 static int of_thermal_get_crit_temp(struct thermal_zone_device *tz,
384 int *temp)
385 {
386 struct __thermal_zone *data = tz->devdata;
387 int i;
388
389 for (i = 0; i < data->ntrips; i++)
390 if (data->trips[i].type == THERMAL_TRIP_CRITICAL) {
391 *temp = data->trips[i].temperature;
392 return 0;
393 }
394
395 return -EINVAL;
396 }
397
398 static struct thermal_zone_device_ops of_thermal_ops = {
399 .get_mode = of_thermal_get_mode,
400 .set_mode = of_thermal_set_mode,
401
402 .get_trip_type = of_thermal_get_trip_type,
403 .get_trip_temp = of_thermal_get_trip_temp,
404 .set_trip_temp = of_thermal_set_trip_temp,
405 .get_trip_hyst = of_thermal_get_trip_hyst,
406 .set_trip_hyst = of_thermal_set_trip_hyst,
407 .get_crit_temp = of_thermal_get_crit_temp,
408
409 .bind = of_thermal_bind,
410 .unbind = of_thermal_unbind,
411 };
412
413 /*** sensor API ***/
414
415 static struct thermal_zone_device *
thermal_zone_of_add_sensor(struct device_node * zone,struct device_node * sensor,void * data,const struct thermal_zone_of_device_ops * ops)416 thermal_zone_of_add_sensor(struct device_node *zone,
417 struct device_node *sensor, void *data,
418 const struct thermal_zone_of_device_ops *ops)
419 {
420 struct thermal_zone_device *tzd;
421 struct __thermal_zone *tz;
422
423 tzd = thermal_zone_get_zone_by_name(zone->name);
424 if (IS_ERR(tzd))
425 return ERR_PTR(-EPROBE_DEFER);
426
427 tz = tzd->devdata;
428
429 if (!ops)
430 return ERR_PTR(-EINVAL);
431
432 mutex_lock(&tzd->lock);
433 tz->ops = ops;
434 tz->sensor_data = data;
435
436 tzd->ops->get_temp = of_thermal_get_temp;
437 tzd->ops->get_trend = of_thermal_get_trend;
438
439 /*
440 * The thermal zone core will calculate the window if they have set the
441 * optional set_trips pointer.
442 */
443 if (ops->set_trips)
444 tzd->ops->set_trips = of_thermal_set_trips;
445
446 if (ops->set_emul_temp)
447 tzd->ops->set_emul_temp = of_thermal_set_emul_temp;
448
449 mutex_unlock(&tzd->lock);
450
451 return tzd;
452 }
453
454 /**
455 * thermal_zone_of_sensor_register - registers a sensor to a DT thermal zone
456 * @dev: a valid struct device pointer of a sensor device. Must contain
457 * a valid .of_node, for the sensor node.
458 * @sensor_id: a sensor identifier, in case the sensor IP has more
459 * than one sensors
460 * @data: a private pointer (owned by the caller) that will be passed
461 * back, when a temperature reading is needed.
462 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
463 *
464 * This function will search the list of thermal zones described in device
465 * tree and look for the zone that refer to the sensor device pointed by
466 * @dev->of_node as temperature providers. For the zone pointing to the
467 * sensor node, the sensor will be added to the DT thermal zone device.
468 *
469 * The thermal zone temperature is provided by the @get_temp function
470 * pointer. When called, it will have the private pointer @data back.
471 *
472 * The thermal zone temperature trend is provided by the @get_trend function
473 * pointer. When called, it will have the private pointer @data back.
474 *
475 * TODO:
476 * 01 - This function must enqueue the new sensor instead of using
477 * it as the only source of temperature values.
478 *
479 * 02 - There must be a way to match the sensor with all thermal zones
480 * that refer to it.
481 *
482 * Return: On success returns a valid struct thermal_zone_device,
483 * otherwise, it returns a corresponding ERR_PTR(). Caller must
484 * check the return value with help of IS_ERR() helper.
485 */
486 struct thermal_zone_device *
thermal_zone_of_sensor_register(struct device * dev,int sensor_id,void * data,const struct thermal_zone_of_device_ops * ops)487 thermal_zone_of_sensor_register(struct device *dev, int sensor_id, void *data,
488 const struct thermal_zone_of_device_ops *ops)
489 {
490 struct device_node *np, *child, *sensor_np;
491 struct thermal_zone_device *tzd = ERR_PTR(-ENODEV);
492
493 np = of_find_node_by_name(NULL, "thermal-zones");
494 if (!np)
495 return ERR_PTR(-ENODEV);
496
497 if (!dev || !dev->of_node) {
498 of_node_put(np);
499 return ERR_PTR(-EINVAL);
500 }
501
502 sensor_np = of_node_get(dev->of_node);
503
504 for_each_available_child_of_node(np, child) {
505 struct of_phandle_args sensor_specs;
506 int ret, id;
507
508 /* For now, thermal framework supports only 1 sensor per zone */
509 ret = of_parse_phandle_with_args(child, "thermal-sensors",
510 "#thermal-sensor-cells",
511 0, &sensor_specs);
512 if (ret)
513 continue;
514
515 if (sensor_specs.args_count >= 1) {
516 id = sensor_specs.args[0];
517 WARN(sensor_specs.args_count > 1,
518 "%pOFn: too many cells in sensor specifier %d\n",
519 sensor_specs.np, sensor_specs.args_count);
520 } else {
521 id = 0;
522 }
523
524 if (sensor_specs.np == sensor_np && id == sensor_id) {
525 tzd = thermal_zone_of_add_sensor(child, sensor_np,
526 data, ops);
527 if (!IS_ERR(tzd))
528 tzd->ops->set_mode(tzd, THERMAL_DEVICE_ENABLED);
529
530 of_node_put(sensor_specs.np);
531 of_node_put(child);
532 goto exit;
533 }
534 of_node_put(sensor_specs.np);
535 }
536 exit:
537 of_node_put(sensor_np);
538 of_node_put(np);
539
540 return tzd;
541 }
542 EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_register);
543
544 /**
545 * thermal_zone_of_sensor_unregister - unregisters a sensor from a DT thermal zone
546 * @dev: a valid struct device pointer of a sensor device. Must contain
547 * a valid .of_node, for the sensor node.
548 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
549 *
550 * This function removes the sensor callbacks and private data from the
551 * thermal zone device registered with thermal_zone_of_sensor_register()
552 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
553 * thermal zone device callbacks.
554 *
555 * TODO: When the support to several sensors per zone is added, this
556 * function must search the sensor list based on @dev parameter.
557 *
558 */
thermal_zone_of_sensor_unregister(struct device * dev,struct thermal_zone_device * tzd)559 void thermal_zone_of_sensor_unregister(struct device *dev,
560 struct thermal_zone_device *tzd)
561 {
562 struct __thermal_zone *tz;
563
564 if (!dev || !tzd || !tzd->devdata)
565 return;
566
567 tz = tzd->devdata;
568
569 /* no __thermal_zone, nothing to be done */
570 if (!tz)
571 return;
572
573 mutex_lock(&tzd->lock);
574 tzd->ops->get_temp = NULL;
575 tzd->ops->get_trend = NULL;
576 tzd->ops->set_emul_temp = NULL;
577
578 tz->ops = NULL;
579 tz->sensor_data = NULL;
580 mutex_unlock(&tzd->lock);
581 }
582 EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister);
583
devm_thermal_zone_of_sensor_release(struct device * dev,void * res)584 static void devm_thermal_zone_of_sensor_release(struct device *dev, void *res)
585 {
586 thermal_zone_of_sensor_unregister(dev,
587 *(struct thermal_zone_device **)res);
588 }
589
devm_thermal_zone_of_sensor_match(struct device * dev,void * res,void * data)590 static int devm_thermal_zone_of_sensor_match(struct device *dev, void *res,
591 void *data)
592 {
593 struct thermal_zone_device **r = res;
594
595 if (WARN_ON(!r || !*r))
596 return 0;
597
598 return *r == data;
599 }
600
601 /**
602 * devm_thermal_zone_of_sensor_register - Resource managed version of
603 * thermal_zone_of_sensor_register()
604 * @dev: a valid struct device pointer of a sensor device. Must contain
605 * a valid .of_node, for the sensor node.
606 * @sensor_id: a sensor identifier, in case the sensor IP has more
607 * than one sensors
608 * @data: a private pointer (owned by the caller) that will be passed
609 * back, when a temperature reading is needed.
610 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
611 *
612 * Refer thermal_zone_of_sensor_register() for more details.
613 *
614 * Return: On success returns a valid struct thermal_zone_device,
615 * otherwise, it returns a corresponding ERR_PTR(). Caller must
616 * check the return value with help of IS_ERR() helper.
617 * Registered thermal_zone_device device will automatically be
618 * released when device is unbounded.
619 */
devm_thermal_zone_of_sensor_register(struct device * dev,int sensor_id,void * data,const struct thermal_zone_of_device_ops * ops)620 struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
621 struct device *dev, int sensor_id,
622 void *data, const struct thermal_zone_of_device_ops *ops)
623 {
624 struct thermal_zone_device **ptr, *tzd;
625
626 ptr = devres_alloc(devm_thermal_zone_of_sensor_release, sizeof(*ptr),
627 GFP_KERNEL);
628 if (!ptr)
629 return ERR_PTR(-ENOMEM);
630
631 tzd = thermal_zone_of_sensor_register(dev, sensor_id, data, ops);
632 if (IS_ERR(tzd)) {
633 devres_free(ptr);
634 return tzd;
635 }
636
637 *ptr = tzd;
638 devres_add(dev, ptr);
639
640 return tzd;
641 }
642 EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_register);
643
644 /**
645 * devm_thermal_zone_of_sensor_unregister - Resource managed version of
646 * thermal_zone_of_sensor_unregister().
647 * @dev: Device for which which resource was allocated.
648 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
649 *
650 * This function removes the sensor callbacks and private data from the
651 * thermal zone device registered with devm_thermal_zone_of_sensor_register()
652 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
653 * thermal zone device callbacks.
654 * Normally this function will not need to be called and the resource
655 * management code will ensure that the resource is freed.
656 */
devm_thermal_zone_of_sensor_unregister(struct device * dev,struct thermal_zone_device * tzd)657 void devm_thermal_zone_of_sensor_unregister(struct device *dev,
658 struct thermal_zone_device *tzd)
659 {
660 WARN_ON(devres_release(dev, devm_thermal_zone_of_sensor_release,
661 devm_thermal_zone_of_sensor_match, tzd));
662 }
663 EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_unregister);
664
665 /*** functions parsing device tree nodes ***/
666
667 /**
668 * thermal_of_populate_bind_params - parse and fill cooling map data
669 * @np: DT node containing a cooling-map node
670 * @__tbp: data structure to be filled with cooling map info
671 * @trips: array of thermal zone trip points
672 * @ntrips: number of trip points inside trips.
673 *
674 * This function parses a cooling-map type of node represented by
675 * @np parameter and fills the read data into @__tbp data structure.
676 * It needs the already parsed array of trip points of the thermal zone
677 * in consideration.
678 *
679 * Return: 0 on success, proper error code otherwise
680 */
thermal_of_populate_bind_params(struct device_node * np,struct __thermal_bind_params * __tbp,struct thermal_trip * trips,int ntrips)681 static int thermal_of_populate_bind_params(struct device_node *np,
682 struct __thermal_bind_params *__tbp,
683 struct thermal_trip *trips,
684 int ntrips)
685 {
686 struct of_phandle_args cooling_spec;
687 struct __thermal_cooling_bind_param *__tcbp;
688 struct device_node *trip;
689 int ret, i, count;
690 u32 prop;
691
692 /* Default weight. Usage is optional */
693 __tbp->usage = THERMAL_WEIGHT_DEFAULT;
694 ret = of_property_read_u32(np, "contribution", &prop);
695 if (ret == 0)
696 __tbp->usage = prop;
697
698 trip = of_parse_phandle(np, "trip", 0);
699 if (!trip) {
700 pr_err("missing trip property\n");
701 return -ENODEV;
702 }
703
704 /* match using device_node */
705 for (i = 0; i < ntrips; i++)
706 if (trip == trips[i].np) {
707 __tbp->trip_id = i;
708 break;
709 }
710
711 if (i == ntrips) {
712 ret = -ENODEV;
713 goto end;
714 }
715
716 count = of_count_phandle_with_args(np, "cooling-device",
717 "#cooling-cells");
718 if (count <= 0) {
719 pr_err("Add a cooling_device property with at least one device\n");
720 ret = -ENOENT;
721 goto end;
722 }
723
724 __tcbp = kcalloc(count, sizeof(*__tcbp), GFP_KERNEL);
725 if (!__tcbp) {
726 ret = -ENOMEM;
727 goto end;
728 }
729
730 for (i = 0; i < count; i++) {
731 ret = of_parse_phandle_with_args(np, "cooling-device",
732 "#cooling-cells", i, &cooling_spec);
733 if (ret < 0) {
734 pr_err("Invalid cooling-device entry\n");
735 goto free_tcbp;
736 }
737
738 __tcbp[i].cooling_device = cooling_spec.np;
739
740 if (cooling_spec.args_count >= 2) { /* at least min and max */
741 __tcbp[i].min = cooling_spec.args[0];
742 __tcbp[i].max = cooling_spec.args[1];
743 } else {
744 pr_err("wrong reference to cooling device, missing limits\n");
745 }
746 }
747
748 __tbp->tcbp = __tcbp;
749 __tbp->count = count;
750
751 goto end;
752
753 free_tcbp:
754 for (i = i - 1; i >= 0; i--)
755 of_node_put(__tcbp[i].cooling_device);
756 kfree(__tcbp);
757 end:
758 of_node_put(trip);
759
760 return ret;
761 }
762
763 /**
764 * It maps 'enum thermal_trip_type' found in include/linux/thermal.h
765 * into the device tree binding of 'trip', property type.
766 */
767 static const char * const trip_types[] = {
768 [THERMAL_TRIP_ACTIVE] = "active",
769 [THERMAL_TRIP_PASSIVE] = "passive",
770 [THERMAL_TRIP_HOT] = "hot",
771 [THERMAL_TRIP_CRITICAL] = "critical",
772 };
773
774 /**
775 * thermal_of_get_trip_type - Get phy mode for given device_node
776 * @np: Pointer to the given device_node
777 * @type: Pointer to resulting trip type
778 *
779 * The function gets trip type string from property 'type',
780 * and store its index in trip_types table in @type,
781 *
782 * Return: 0 on success, or errno in error case.
783 */
thermal_of_get_trip_type(struct device_node * np,enum thermal_trip_type * type)784 static int thermal_of_get_trip_type(struct device_node *np,
785 enum thermal_trip_type *type)
786 {
787 const char *t;
788 int err, i;
789
790 err = of_property_read_string(np, "type", &t);
791 if (err < 0)
792 return err;
793
794 for (i = 0; i < ARRAY_SIZE(trip_types); i++)
795 if (!strcasecmp(t, trip_types[i])) {
796 *type = i;
797 return 0;
798 }
799
800 return -ENODEV;
801 }
802
803 /**
804 * thermal_of_populate_trip - parse and fill one trip point data
805 * @np: DT node containing a trip point node
806 * @trip: trip point data structure to be filled up
807 *
808 * This function parses a trip point type of node represented by
809 * @np parameter and fills the read data into @trip data structure.
810 *
811 * Return: 0 on success, proper error code otherwise
812 */
thermal_of_populate_trip(struct device_node * np,struct thermal_trip * trip)813 static int thermal_of_populate_trip(struct device_node *np,
814 struct thermal_trip *trip)
815 {
816 int prop;
817 int ret;
818
819 ret = of_property_read_u32(np, "temperature", &prop);
820 if (ret < 0) {
821 pr_err("missing temperature property\n");
822 return ret;
823 }
824 trip->temperature = prop;
825
826 ret = of_property_read_u32(np, "hysteresis", &prop);
827 if (ret < 0) {
828 pr_err("missing hysteresis property\n");
829 return ret;
830 }
831 trip->hysteresis = prop;
832
833 ret = thermal_of_get_trip_type(np, &trip->type);
834 if (ret < 0) {
835 pr_err("wrong trip type property\n");
836 return ret;
837 }
838
839 /* Required for cooling map matching */
840 trip->np = np;
841 of_node_get(np);
842
843 return 0;
844 }
845
846 /**
847 * thermal_of_build_thermal_zone - parse and fill one thermal zone data
848 * @np: DT node containing a thermal zone node
849 *
850 * This function parses a thermal zone type of node represented by
851 * @np parameter and fills the read data into a __thermal_zone data structure
852 * and return this pointer.
853 *
854 * TODO: Missing properties to parse: thermal-sensor-names
855 *
856 * Return: On success returns a valid struct __thermal_zone,
857 * otherwise, it returns a corresponding ERR_PTR(). Caller must
858 * check the return value with help of IS_ERR() helper.
859 */
860 static struct __thermal_zone
thermal_of_build_thermal_zone(struct device_node * np)861 __init *thermal_of_build_thermal_zone(struct device_node *np)
862 {
863 struct device_node *child = NULL, *gchild;
864 struct __thermal_zone *tz;
865 int ret, i;
866 u32 prop, coef[2];
867
868 if (!np) {
869 pr_err("no thermal zone np\n");
870 return ERR_PTR(-EINVAL);
871 }
872
873 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
874 if (!tz)
875 return ERR_PTR(-ENOMEM);
876
877 ret = of_property_read_u32(np, "polling-delay-passive", &prop);
878 if (ret < 0) {
879 pr_err("%pOFn: missing polling-delay-passive property\n", np);
880 goto free_tz;
881 }
882 tz->passive_delay = prop;
883
884 ret = of_property_read_u32(np, "polling-delay", &prop);
885 if (ret < 0) {
886 pr_err("%pOFn: missing polling-delay property\n", np);
887 goto free_tz;
888 }
889 tz->polling_delay = prop;
890
891 /*
892 * REVIST: for now, the thermal framework supports only
893 * one sensor per thermal zone. Thus, we are considering
894 * only the first two values as slope and offset.
895 */
896 ret = of_property_read_u32_array(np, "coefficients", coef, 2);
897 if (ret == 0) {
898 tz->slope = coef[0];
899 tz->offset = coef[1];
900 } else {
901 tz->slope = 1;
902 tz->offset = 0;
903 }
904
905 /* trips */
906 child = of_get_child_by_name(np, "trips");
907
908 /* No trips provided */
909 if (!child)
910 goto finish;
911
912 tz->ntrips = of_get_child_count(child);
913 if (tz->ntrips == 0) /* must have at least one child */
914 goto finish;
915
916 tz->trips = kcalloc(tz->ntrips, sizeof(*tz->trips), GFP_KERNEL);
917 if (!tz->trips) {
918 ret = -ENOMEM;
919 goto free_tz;
920 }
921
922 i = 0;
923 for_each_child_of_node(child, gchild) {
924 ret = thermal_of_populate_trip(gchild, &tz->trips[i++]);
925 if (ret)
926 goto free_trips;
927 }
928
929 of_node_put(child);
930
931 /* cooling-maps */
932 child = of_get_child_by_name(np, "cooling-maps");
933
934 /* cooling-maps not provided */
935 if (!child)
936 goto finish;
937
938 tz->num_tbps = of_get_child_count(child);
939 if (tz->num_tbps == 0)
940 goto finish;
941
942 tz->tbps = kcalloc(tz->num_tbps, sizeof(*tz->tbps), GFP_KERNEL);
943 if (!tz->tbps) {
944 ret = -ENOMEM;
945 goto free_trips;
946 }
947
948 i = 0;
949 for_each_child_of_node(child, gchild) {
950 ret = thermal_of_populate_bind_params(gchild, &tz->tbps[i++],
951 tz->trips, tz->ntrips);
952 if (ret)
953 goto free_tbps;
954 }
955
956 finish:
957 of_node_put(child);
958 tz->mode = THERMAL_DEVICE_DISABLED;
959
960 return tz;
961
962 free_tbps:
963 for (i = i - 1; i >= 0; i--) {
964 struct __thermal_bind_params *tbp = tz->tbps + i;
965 int j;
966
967 for (j = 0; j < tbp->count; j++)
968 of_node_put(tbp->tcbp[j].cooling_device);
969
970 kfree(tbp->tcbp);
971 }
972
973 kfree(tz->tbps);
974 free_trips:
975 for (i = 0; i < tz->ntrips; i++)
976 of_node_put(tz->trips[i].np);
977 kfree(tz->trips);
978 of_node_put(gchild);
979 free_tz:
980 kfree(tz);
981 of_node_put(child);
982
983 return ERR_PTR(ret);
984 }
985
of_thermal_free_zone(struct __thermal_zone * tz)986 static inline void of_thermal_free_zone(struct __thermal_zone *tz)
987 {
988 struct __thermal_bind_params *tbp;
989 int i, j;
990
991 for (i = 0; i < tz->num_tbps; i++) {
992 tbp = tz->tbps + i;
993
994 for (j = 0; j < tbp->count; j++)
995 of_node_put(tbp->tcbp[j].cooling_device);
996
997 kfree(tbp->tcbp);
998 }
999
1000 kfree(tz->tbps);
1001 for (i = 0; i < tz->ntrips; i++)
1002 of_node_put(tz->trips[i].np);
1003 kfree(tz->trips);
1004 kfree(tz);
1005 }
1006
1007 /**
1008 * of_parse_thermal_zones - parse device tree thermal data
1009 *
1010 * Initialization function that can be called by machine initialization
1011 * code to parse thermal data and populate the thermal framework
1012 * with hardware thermal zones info. This function only parses thermal zones.
1013 * Cooling devices and sensor devices nodes are supposed to be parsed
1014 * by their respective drivers.
1015 *
1016 * Return: 0 on success, proper error code otherwise
1017 *
1018 */
of_parse_thermal_zones(void)1019 int __init of_parse_thermal_zones(void)
1020 {
1021 struct device_node *np, *child;
1022 struct __thermal_zone *tz;
1023 struct thermal_zone_device_ops *ops;
1024
1025 np = of_find_node_by_name(NULL, "thermal-zones");
1026 if (!np) {
1027 pr_debug("unable to find thermal zones\n");
1028 return 0; /* Run successfully on systems without thermal DT */
1029 }
1030
1031 for_each_available_child_of_node(np, child) {
1032 struct thermal_zone_device *zone;
1033 struct thermal_zone_params *tzp;
1034 int i, mask = 0;
1035 u32 prop;
1036
1037 tz = thermal_of_build_thermal_zone(child);
1038 if (IS_ERR(tz)) {
1039 pr_err("failed to build thermal zone %pOFn: %ld\n",
1040 child,
1041 PTR_ERR(tz));
1042 continue;
1043 }
1044
1045 ops = kmemdup(&of_thermal_ops, sizeof(*ops), GFP_KERNEL);
1046 if (!ops)
1047 goto exit_free;
1048
1049 tzp = kzalloc(sizeof(*tzp), GFP_KERNEL);
1050 if (!tzp) {
1051 kfree(ops);
1052 goto exit_free;
1053 }
1054
1055 /* No hwmon because there might be hwmon drivers registering */
1056 tzp->no_hwmon = true;
1057
1058 if (!of_property_read_u32(child, "sustainable-power", &prop))
1059 tzp->sustainable_power = prop;
1060
1061 for (i = 0; i < tz->ntrips; i++)
1062 mask |= 1 << i;
1063
1064 /* these two are left for temperature drivers to use */
1065 tzp->slope = tz->slope;
1066 tzp->offset = tz->offset;
1067
1068 zone = thermal_zone_device_register(child->name, tz->ntrips,
1069 mask, tz,
1070 ops, tzp,
1071 tz->passive_delay,
1072 tz->polling_delay);
1073 if (IS_ERR(zone)) {
1074 pr_err("Failed to build %pOFn zone %ld\n", child,
1075 PTR_ERR(zone));
1076 kfree(tzp);
1077 kfree(ops);
1078 of_thermal_free_zone(tz);
1079 /* attempting to build remaining zones still */
1080 }
1081 }
1082 of_node_put(np);
1083
1084 return 0;
1085
1086 exit_free:
1087 of_node_put(child);
1088 of_node_put(np);
1089 of_thermal_free_zone(tz);
1090
1091 /* no memory available, so free what we have built */
1092 of_thermal_destroy_zones();
1093
1094 return -ENOMEM;
1095 }
1096
1097 /**
1098 * of_thermal_destroy_zones - remove all zones parsed and allocated resources
1099 *
1100 * Finds all zones parsed and added to the thermal framework and remove them
1101 * from the system, together with their resources.
1102 *
1103 */
of_thermal_destroy_zones(void)1104 void of_thermal_destroy_zones(void)
1105 {
1106 struct device_node *np, *child;
1107
1108 np = of_find_node_by_name(NULL, "thermal-zones");
1109 if (!np) {
1110 pr_debug("unable to find thermal zones\n");
1111 return;
1112 }
1113
1114 for_each_available_child_of_node(np, child) {
1115 struct thermal_zone_device *zone;
1116
1117 zone = thermal_zone_get_zone_by_name(child->name);
1118 if (IS_ERR(zone))
1119 continue;
1120
1121 thermal_zone_device_unregister(zone);
1122 kfree(zone->tzp);
1123 kfree(zone->ops);
1124 of_thermal_free_zone(zone->devdata);
1125 }
1126 of_node_put(np);
1127 }
1128