1 // SPDX-License-Identifier: GPL-2.0
3 * of-thermal.c - Generic Thermal Management device tree support.
5 * Copyright (C) 2013 Texas Instruments
6 * Copyright (C) 2013 Eduardo Valentin <eduardo.valentin@ti.com>
8 #include <linux/thermal.h>
9 #include <linux/slab.h>
10 #include <linux/types.h>
11 #include <linux/of_device.h>
12 #include <linux/of_platform.h>
13 #include <linux/err.h>
14 #include <linux/export.h>
15 #include <linux/string.h>
17 #include "thermal_core.h"
19 /*** Private data structures to represent thermal device tree data ***/
22 * struct __thermal_bind_param - a match between trip and cooling device
23 * @cooling_device: a pointer to identify the referred cooling device
24 * @trip_id: the trip point index
25 * @usage: the percentage (from 0 to 100) of cooling contribution
26 * @min: minimum cooling state used at this trip point
27 * @max: maximum cooling state used at this trip point
30 struct __thermal_bind_params
{
31 struct device_node
*cooling_device
;
39 * struct __thermal_zone - internal representation of a thermal zone
40 * @mode: current thermal zone device mode (enabled/disabled)
41 * @passive_delay: polling interval while passive cooling is activated
42 * @polling_delay: zone polling interval
43 * @slope: slope of the temperature adjustment curve
44 * @offset: offset of the temperature adjustment curve
45 * @ntrips: number of trip points
46 * @trips: an array of trip points (0..ntrips - 1)
47 * @num_tbps: number of thermal bind params
48 * @tbps: an array of thermal bind params (0..num_tbps - 1)
49 * @sensor_data: sensor private data used while reading temperature and trend
50 * @ops: set of callbacks to handle the thermal zone based on DT
53 struct __thermal_zone
{
54 enum thermal_device_mode mode
;
62 struct thermal_trip
*trips
;
64 /* cooling binding data */
66 struct __thermal_bind_params
*tbps
;
68 /* sensor interface */
70 const struct thermal_zone_of_device_ops
*ops
;
73 /*** DT thermal zone device callbacks ***/
75 static int of_thermal_get_temp(struct thermal_zone_device
*tz
,
78 struct __thermal_zone
*data
= tz
->devdata
;
80 if (!data
->ops
->get_temp
)
83 return data
->ops
->get_temp(data
->sensor_data
, temp
);
86 static int of_thermal_set_trips(struct thermal_zone_device
*tz
,
89 struct __thermal_zone
*data
= tz
->devdata
;
91 if (!data
->ops
|| !data
->ops
->set_trips
)
94 return data
->ops
->set_trips(data
->sensor_data
, low
, high
);
98 * of_thermal_get_ntrips - function to export number of available trip
100 * @tz: pointer to a thermal zone
102 * This function is a globally visible wrapper to get number of trip points
103 * stored in the local struct __thermal_zone
105 * Return: number of available trip points, -ENODEV when data not available
107 int of_thermal_get_ntrips(struct thermal_zone_device
*tz
)
109 struct __thermal_zone
*data
= tz
->devdata
;
111 if (!data
|| IS_ERR(data
))
116 EXPORT_SYMBOL_GPL(of_thermal_get_ntrips
);
119 * of_thermal_is_trip_valid - function to check if trip point is valid
121 * @tz: pointer to a thermal zone
122 * @trip: trip point to evaluate
124 * This function is responsible for checking if passed trip point is valid
126 * Return: true if trip point is valid, false otherwise
128 bool of_thermal_is_trip_valid(struct thermal_zone_device
*tz
, int trip
)
130 struct __thermal_zone
*data
= tz
->devdata
;
132 if (!data
|| trip
>= data
->ntrips
|| trip
< 0)
137 EXPORT_SYMBOL_GPL(of_thermal_is_trip_valid
);
140 * of_thermal_get_trip_points - function to get access to a globally exported
143 * @tz: pointer to a thermal zone
145 * This function provides a pointer to trip points table
147 * Return: pointer to trip points table, NULL otherwise
149 const struct thermal_trip
*
150 of_thermal_get_trip_points(struct thermal_zone_device
*tz
)
152 struct __thermal_zone
*data
= tz
->devdata
;
159 EXPORT_SYMBOL_GPL(of_thermal_get_trip_points
);
162 * of_thermal_set_emul_temp - function to set emulated temperature
164 * @tz: pointer to a thermal zone
165 * @temp: temperature to set
167 * This function gives the ability to set emulated value of temperature,
168 * which is handy for debugging
170 * Return: zero on success, error code otherwise
172 static int of_thermal_set_emul_temp(struct thermal_zone_device
*tz
,
175 struct __thermal_zone
*data
= tz
->devdata
;
177 return data
->ops
->set_emul_temp(data
->sensor_data
, temp
);
180 static int of_thermal_get_trend(struct thermal_zone_device
*tz
, int trip
,
181 enum thermal_trend
*trend
)
183 struct __thermal_zone
*data
= tz
->devdata
;
185 if (!data
->ops
->get_trend
)
188 return data
->ops
->get_trend(data
->sensor_data
, trip
, trend
);
191 static int of_thermal_bind(struct thermal_zone_device
*thermal
,
192 struct thermal_cooling_device
*cdev
)
194 struct __thermal_zone
*data
= thermal
->devdata
;
197 if (!data
|| IS_ERR(data
))
200 /* find where to bind */
201 for (i
= 0; i
< data
->num_tbps
; i
++) {
202 struct __thermal_bind_params
*tbp
= data
->tbps
+ i
;
204 if (tbp
->cooling_device
== cdev
->np
) {
207 ret
= thermal_zone_bind_cooling_device(thermal
,
220 static int of_thermal_unbind(struct thermal_zone_device
*thermal
,
221 struct thermal_cooling_device
*cdev
)
223 struct __thermal_zone
*data
= thermal
->devdata
;
226 if (!data
|| IS_ERR(data
))
229 /* find where to unbind */
230 for (i
= 0; i
< data
->num_tbps
; i
++) {
231 struct __thermal_bind_params
*tbp
= data
->tbps
+ i
;
233 if (tbp
->cooling_device
== cdev
->np
) {
236 ret
= thermal_zone_unbind_cooling_device(thermal
,
246 static int of_thermal_get_mode(struct thermal_zone_device
*tz
,
247 enum thermal_device_mode
*mode
)
249 struct __thermal_zone
*data
= tz
->devdata
;
256 static int of_thermal_set_mode(struct thermal_zone_device
*tz
,
257 enum thermal_device_mode mode
)
259 struct __thermal_zone
*data
= tz
->devdata
;
261 mutex_lock(&tz
->lock
);
263 if (mode
== THERMAL_DEVICE_ENABLED
)
264 tz
->polling_delay
= data
->polling_delay
;
266 tz
->polling_delay
= 0;
268 mutex_unlock(&tz
->lock
);
271 thermal_zone_device_update(tz
, THERMAL_EVENT_UNSPECIFIED
);
276 static int of_thermal_get_trip_type(struct thermal_zone_device
*tz
, int trip
,
277 enum thermal_trip_type
*type
)
279 struct __thermal_zone
*data
= tz
->devdata
;
281 if (trip
>= data
->ntrips
|| trip
< 0)
284 *type
= data
->trips
[trip
].type
;
289 static int of_thermal_get_trip_temp(struct thermal_zone_device
*tz
, int trip
,
292 struct __thermal_zone
*data
= tz
->devdata
;
294 if (trip
>= data
->ntrips
|| trip
< 0)
297 *temp
= data
->trips
[trip
].temperature
;
302 static int of_thermal_set_trip_temp(struct thermal_zone_device
*tz
, int trip
,
305 struct __thermal_zone
*data
= tz
->devdata
;
307 if (trip
>= data
->ntrips
|| trip
< 0)
310 if (data
->ops
->set_trip_temp
) {
313 ret
= data
->ops
->set_trip_temp(data
->sensor_data
, trip
, temp
);
318 /* thermal framework should take care of data->mask & (1 << trip) */
319 data
->trips
[trip
].temperature
= temp
;
324 static int of_thermal_get_trip_hyst(struct thermal_zone_device
*tz
, int trip
,
327 struct __thermal_zone
*data
= tz
->devdata
;
329 if (trip
>= data
->ntrips
|| trip
< 0)
332 *hyst
= data
->trips
[trip
].hysteresis
;
337 static int of_thermal_set_trip_hyst(struct thermal_zone_device
*tz
, int trip
,
340 struct __thermal_zone
*data
= tz
->devdata
;
342 if (trip
>= data
->ntrips
|| trip
< 0)
345 /* thermal framework should take care of data->mask & (1 << trip) */
346 data
->trips
[trip
].hysteresis
= hyst
;
351 static int of_thermal_get_crit_temp(struct thermal_zone_device
*tz
,
354 struct __thermal_zone
*data
= tz
->devdata
;
357 for (i
= 0; i
< data
->ntrips
; i
++)
358 if (data
->trips
[i
].type
== THERMAL_TRIP_CRITICAL
) {
359 *temp
= data
->trips
[i
].temperature
;
366 static struct thermal_zone_device_ops of_thermal_ops
= {
367 .get_mode
= of_thermal_get_mode
,
368 .set_mode
= of_thermal_set_mode
,
370 .get_trip_type
= of_thermal_get_trip_type
,
371 .get_trip_temp
= of_thermal_get_trip_temp
,
372 .set_trip_temp
= of_thermal_set_trip_temp
,
373 .get_trip_hyst
= of_thermal_get_trip_hyst
,
374 .set_trip_hyst
= of_thermal_set_trip_hyst
,
375 .get_crit_temp
= of_thermal_get_crit_temp
,
377 .bind
= of_thermal_bind
,
378 .unbind
= of_thermal_unbind
,
383 static struct thermal_zone_device
*
384 thermal_zone_of_add_sensor(struct device_node
*zone
,
385 struct device_node
*sensor
, void *data
,
386 const struct thermal_zone_of_device_ops
*ops
)
388 struct thermal_zone_device
*tzd
;
389 struct __thermal_zone
*tz
;
391 tzd
= thermal_zone_get_zone_by_name(zone
->name
);
393 return ERR_PTR(-EPROBE_DEFER
);
398 return ERR_PTR(-EINVAL
);
400 mutex_lock(&tzd
->lock
);
402 tz
->sensor_data
= data
;
404 tzd
->ops
->get_temp
= of_thermal_get_temp
;
405 tzd
->ops
->get_trend
= of_thermal_get_trend
;
408 * The thermal zone core will calculate the window if they have set the
409 * optional set_trips pointer.
412 tzd
->ops
->set_trips
= of_thermal_set_trips
;
414 if (ops
->set_emul_temp
)
415 tzd
->ops
->set_emul_temp
= of_thermal_set_emul_temp
;
417 mutex_unlock(&tzd
->lock
);
423 * thermal_zone_of_sensor_register - registers a sensor to a DT thermal zone
424 * @dev: a valid struct device pointer of a sensor device. Must contain
425 * a valid .of_node, for the sensor node.
426 * @sensor_id: a sensor identifier, in case the sensor IP has more
428 * @data: a private pointer (owned by the caller) that will be passed
429 * back, when a temperature reading is needed.
430 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
432 * This function will search the list of thermal zones described in device
433 * tree and look for the zone that refer to the sensor device pointed by
434 * @dev->of_node as temperature providers. For the zone pointing to the
435 * sensor node, the sensor will be added to the DT thermal zone device.
437 * The thermal zone temperature is provided by the @get_temp function
438 * pointer. When called, it will have the private pointer @data back.
440 * The thermal zone temperature trend is provided by the @get_trend function
441 * pointer. When called, it will have the private pointer @data back.
444 * 01 - This function must enqueue the new sensor instead of using
445 * it as the only source of temperature values.
447 * 02 - There must be a way to match the sensor with all thermal zones
450 * Return: On success returns a valid struct thermal_zone_device,
451 * otherwise, it returns a corresponding ERR_PTR(). Caller must
452 * check the return value with help of IS_ERR() helper.
454 struct thermal_zone_device
*
455 thermal_zone_of_sensor_register(struct device
*dev
, int sensor_id
, void *data
,
456 const struct thermal_zone_of_device_ops
*ops
)
458 struct device_node
*np
, *child
, *sensor_np
;
459 struct thermal_zone_device
*tzd
= ERR_PTR(-ENODEV
);
461 np
= of_find_node_by_name(NULL
, "thermal-zones");
463 return ERR_PTR(-ENODEV
);
465 if (!dev
|| !dev
->of_node
) {
467 return ERR_PTR(-EINVAL
);
470 sensor_np
= of_node_get(dev
->of_node
);
472 for_each_available_child_of_node(np
, child
) {
473 struct of_phandle_args sensor_specs
;
476 /* For now, thermal framework supports only 1 sensor per zone */
477 ret
= of_parse_phandle_with_args(child
, "thermal-sensors",
478 "#thermal-sensor-cells",
483 if (sensor_specs
.args_count
>= 1) {
484 id
= sensor_specs
.args
[0];
485 WARN(sensor_specs
.args_count
> 1,
486 "%s: too many cells in sensor specifier %d\n",
487 sensor_specs
.np
->name
, sensor_specs
.args_count
);
492 if (sensor_specs
.np
== sensor_np
&& id
== sensor_id
) {
493 tzd
= thermal_zone_of_add_sensor(child
, sensor_np
,
496 tzd
->ops
->set_mode(tzd
, THERMAL_DEVICE_ENABLED
);
498 of_node_put(sensor_specs
.np
);
502 of_node_put(sensor_specs
.np
);
505 of_node_put(sensor_np
);
510 EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_register
);
513 * thermal_zone_of_sensor_unregister - unregisters a sensor from a DT thermal zone
514 * @dev: a valid struct device pointer of a sensor device. Must contain
515 * a valid .of_node, for the sensor node.
516 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
518 * This function removes the sensor callbacks and private data from the
519 * thermal zone device registered with thermal_zone_of_sensor_register()
520 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
521 * thermal zone device callbacks.
523 * TODO: When the support to several sensors per zone is added, this
524 * function must search the sensor list based on @dev parameter.
527 void thermal_zone_of_sensor_unregister(struct device
*dev
,
528 struct thermal_zone_device
*tzd
)
530 struct __thermal_zone
*tz
;
532 if (!dev
|| !tzd
|| !tzd
->devdata
)
537 /* no __thermal_zone, nothing to be done */
541 mutex_lock(&tzd
->lock
);
542 tzd
->ops
->get_temp
= NULL
;
543 tzd
->ops
->get_trend
= NULL
;
544 tzd
->ops
->set_emul_temp
= NULL
;
547 tz
->sensor_data
= NULL
;
548 mutex_unlock(&tzd
->lock
);
550 EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister
);
552 static void devm_thermal_zone_of_sensor_release(struct device
*dev
, void *res
)
554 thermal_zone_of_sensor_unregister(dev
,
555 *(struct thermal_zone_device
**)res
);
558 static int devm_thermal_zone_of_sensor_match(struct device
*dev
, void *res
,
561 struct thermal_zone_device
**r
= res
;
563 if (WARN_ON(!r
|| !*r
))
570 * devm_thermal_zone_of_sensor_register - Resource managed version of
571 * thermal_zone_of_sensor_register()
572 * @dev: a valid struct device pointer of a sensor device. Must contain
573 * a valid .of_node, for the sensor node.
574 * @sensor_id: a sensor identifier, in case the sensor IP has more
576 * @data: a private pointer (owned by the caller) that will be passed
577 * back, when a temperature reading is needed.
578 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
580 * Refer thermal_zone_of_sensor_register() for more details.
582 * Return: On success returns a valid struct thermal_zone_device,
583 * otherwise, it returns a corresponding ERR_PTR(). Caller must
584 * check the return value with help of IS_ERR() helper.
585 * Registered thermal_zone_device device will automatically be
586 * released when device is unbounded.
588 struct thermal_zone_device
*devm_thermal_zone_of_sensor_register(
589 struct device
*dev
, int sensor_id
,
590 void *data
, const struct thermal_zone_of_device_ops
*ops
)
592 struct thermal_zone_device
**ptr
, *tzd
;
594 ptr
= devres_alloc(devm_thermal_zone_of_sensor_release
, sizeof(*ptr
),
597 return ERR_PTR(-ENOMEM
);
599 tzd
= thermal_zone_of_sensor_register(dev
, sensor_id
, data
, ops
);
606 devres_add(dev
, ptr
);
610 EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_register
);
613 * devm_thermal_zone_of_sensor_unregister - Resource managed version of
614 * thermal_zone_of_sensor_unregister().
615 * @dev: Device for which which resource was allocated.
616 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
618 * This function removes the sensor callbacks and private data from the
619 * thermal zone device registered with devm_thermal_zone_of_sensor_register()
620 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
621 * thermal zone device callbacks.
622 * Normally this function will not need to be called and the resource
623 * management code will ensure that the resource is freed.
625 void devm_thermal_zone_of_sensor_unregister(struct device
*dev
,
626 struct thermal_zone_device
*tzd
)
628 WARN_ON(devres_release(dev
, devm_thermal_zone_of_sensor_release
,
629 devm_thermal_zone_of_sensor_match
, tzd
));
631 EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_unregister
);
633 /*** functions parsing device tree nodes ***/
636 * thermal_of_populate_bind_params - parse and fill cooling map data
637 * @np: DT node containing a cooling-map node
638 * @__tbp: data structure to be filled with cooling map info
639 * @trips: array of thermal zone trip points
640 * @ntrips: number of trip points inside trips.
642 * This function parses a cooling-map type of node represented by
643 * @np parameter and fills the read data into @__tbp data structure.
644 * It needs the already parsed array of trip points of the thermal zone
647 * Return: 0 on success, proper error code otherwise
649 static int thermal_of_populate_bind_params(struct device_node
*np
,
650 struct __thermal_bind_params
*__tbp
,
651 struct thermal_trip
*trips
,
654 struct of_phandle_args cooling_spec
;
655 struct device_node
*trip
;
659 /* Default weight. Usage is optional */
660 __tbp
->usage
= THERMAL_WEIGHT_DEFAULT
;
661 ret
= of_property_read_u32(np
, "contribution", &prop
);
665 trip
= of_parse_phandle(np
, "trip", 0);
667 pr_err("missing trip property\n");
671 /* match using device_node */
672 for (i
= 0; i
< ntrips
; i
++)
673 if (trip
== trips
[i
].np
) {
683 ret
= of_parse_phandle_with_args(np
, "cooling-device", "#cooling-cells",
686 pr_err("missing cooling_device property\n");
689 __tbp
->cooling_device
= cooling_spec
.np
;
690 if (cooling_spec
.args_count
>= 2) { /* at least min and max */
691 __tbp
->min
= cooling_spec
.args
[0];
692 __tbp
->max
= cooling_spec
.args
[1];
694 pr_err("wrong reference to cooling device, missing limits\n");
704 * It maps 'enum thermal_trip_type' found in include/linux/thermal.h
705 * into the device tree binding of 'trip', property type.
707 static const char * const trip_types
[] = {
708 [THERMAL_TRIP_ACTIVE
] = "active",
709 [THERMAL_TRIP_PASSIVE
] = "passive",
710 [THERMAL_TRIP_HOT
] = "hot",
711 [THERMAL_TRIP_CRITICAL
] = "critical",
715 * thermal_of_get_trip_type - Get phy mode for given device_node
716 * @np: Pointer to the given device_node
717 * @type: Pointer to resulting trip type
719 * The function gets trip type string from property 'type',
720 * and store its index in trip_types table in @type,
722 * Return: 0 on success, or errno in error case.
724 static int thermal_of_get_trip_type(struct device_node
*np
,
725 enum thermal_trip_type
*type
)
730 err
= of_property_read_string(np
, "type", &t
);
734 for (i
= 0; i
< ARRAY_SIZE(trip_types
); i
++)
735 if (!strcasecmp(t
, trip_types
[i
])) {
744 * thermal_of_populate_trip - parse and fill one trip point data
745 * @np: DT node containing a trip point node
746 * @trip: trip point data structure to be filled up
748 * This function parses a trip point type of node represented by
749 * @np parameter and fills the read data into @trip data structure.
751 * Return: 0 on success, proper error code otherwise
753 static int thermal_of_populate_trip(struct device_node
*np
,
754 struct thermal_trip
*trip
)
759 ret
= of_property_read_u32(np
, "temperature", &prop
);
761 pr_err("missing temperature property\n");
764 trip
->temperature
= prop
;
766 ret
= of_property_read_u32(np
, "hysteresis", &prop
);
768 pr_err("missing hysteresis property\n");
771 trip
->hysteresis
= prop
;
773 ret
= thermal_of_get_trip_type(np
, &trip
->type
);
775 pr_err("wrong trip type property\n");
779 /* Required for cooling map matching */
787 * thermal_of_build_thermal_zone - parse and fill one thermal zone data
788 * @np: DT node containing a thermal zone node
790 * This function parses a thermal zone type of node represented by
791 * @np parameter and fills the read data into a __thermal_zone data structure
792 * and return this pointer.
794 * TODO: Missing properties to parse: thermal-sensor-names
796 * Return: On success returns a valid struct __thermal_zone,
797 * otherwise, it returns a corresponding ERR_PTR(). Caller must
798 * check the return value with help of IS_ERR() helper.
800 static struct __thermal_zone
801 __init
*thermal_of_build_thermal_zone(struct device_node
*np
)
803 struct device_node
*child
= NULL
, *gchild
;
804 struct __thermal_zone
*tz
;
809 pr_err("no thermal zone np\n");
810 return ERR_PTR(-EINVAL
);
813 tz
= kzalloc(sizeof(*tz
), GFP_KERNEL
);
815 return ERR_PTR(-ENOMEM
);
817 ret
= of_property_read_u32(np
, "polling-delay-passive", &prop
);
819 pr_err("missing polling-delay-passive property\n");
822 tz
->passive_delay
= prop
;
824 ret
= of_property_read_u32(np
, "polling-delay", &prop
);
826 pr_err("missing polling-delay property\n");
829 tz
->polling_delay
= prop
;
832 * REVIST: for now, the thermal framework supports only
833 * one sensor per thermal zone. Thus, we are considering
834 * only the first two values as slope and offset.
836 ret
= of_property_read_u32_array(np
, "coefficients", coef
, 2);
839 tz
->offset
= coef
[1];
846 child
= of_get_child_by_name(np
, "trips");
848 /* No trips provided */
852 tz
->ntrips
= of_get_child_count(child
);
853 if (tz
->ntrips
== 0) /* must have at least one child */
856 tz
->trips
= kcalloc(tz
->ntrips
, sizeof(*tz
->trips
), GFP_KERNEL
);
863 for_each_child_of_node(child
, gchild
) {
864 ret
= thermal_of_populate_trip(gchild
, &tz
->trips
[i
++]);
872 child
= of_get_child_by_name(np
, "cooling-maps");
874 /* cooling-maps not provided */
878 tz
->num_tbps
= of_get_child_count(child
);
879 if (tz
->num_tbps
== 0)
882 tz
->tbps
= kcalloc(tz
->num_tbps
, sizeof(*tz
->tbps
), GFP_KERNEL
);
889 for_each_child_of_node(child
, gchild
) {
890 ret
= thermal_of_populate_bind_params(gchild
, &tz
->tbps
[i
++],
891 tz
->trips
, tz
->ntrips
);
898 tz
->mode
= THERMAL_DEVICE_DISABLED
;
903 for (i
= i
- 1; i
>= 0; i
--)
904 of_node_put(tz
->tbps
[i
].cooling_device
);
907 for (i
= 0; i
< tz
->ntrips
; i
++)
908 of_node_put(tz
->trips
[i
].np
);
918 static inline void of_thermal_free_zone(struct __thermal_zone
*tz
)
922 for (i
= 0; i
< tz
->num_tbps
; i
++)
923 of_node_put(tz
->tbps
[i
].cooling_device
);
925 for (i
= 0; i
< tz
->ntrips
; i
++)
926 of_node_put(tz
->trips
[i
].np
);
932 * of_parse_thermal_zones - parse device tree thermal data
934 * Initialization function that can be called by machine initialization
935 * code to parse thermal data and populate the thermal framework
936 * with hardware thermal zones info. This function only parses thermal zones.
937 * Cooling devices and sensor devices nodes are supposed to be parsed
938 * by their respective drivers.
940 * Return: 0 on success, proper error code otherwise
943 int __init
of_parse_thermal_zones(void)
945 struct device_node
*np
, *child
;
946 struct __thermal_zone
*tz
;
947 struct thermal_zone_device_ops
*ops
;
949 np
= of_find_node_by_name(NULL
, "thermal-zones");
951 pr_debug("unable to find thermal zones\n");
952 return 0; /* Run successfully on systems without thermal DT */
955 for_each_available_child_of_node(np
, child
) {
956 struct thermal_zone_device
*zone
;
957 struct thermal_zone_params
*tzp
;
961 tz
= thermal_of_build_thermal_zone(child
);
963 pr_err("failed to build thermal zone %s: %ld\n",
969 ops
= kmemdup(&of_thermal_ops
, sizeof(*ops
), GFP_KERNEL
);
973 tzp
= kzalloc(sizeof(*tzp
), GFP_KERNEL
);
979 /* No hwmon because there might be hwmon drivers registering */
980 tzp
->no_hwmon
= true;
982 if (!of_property_read_u32(child
, "sustainable-power", &prop
))
983 tzp
->sustainable_power
= prop
;
985 for (i
= 0; i
< tz
->ntrips
; i
++)
988 /* these two are left for temperature drivers to use */
989 tzp
->slope
= tz
->slope
;
990 tzp
->offset
= tz
->offset
;
992 zone
= thermal_zone_device_register(child
->name
, tz
->ntrips
,
998 pr_err("Failed to build %s zone %ld\n", child
->name
,
1002 of_thermal_free_zone(tz
);
1003 /* attempting to build remaining zones still */
1013 of_thermal_free_zone(tz
);
1015 /* no memory available, so free what we have built */
1016 of_thermal_destroy_zones();
1022 * of_thermal_destroy_zones - remove all zones parsed and allocated resources
1024 * Finds all zones parsed and added to the thermal framework and remove them
1025 * from the system, together with their resources.
1028 void of_thermal_destroy_zones(void)
1030 struct device_node
*np
, *child
;
1032 np
= of_find_node_by_name(NULL
, "thermal-zones");
1034 pr_debug("unable to find thermal zones\n");
1038 for_each_available_child_of_node(np
, child
) {
1039 struct thermal_zone_device
*zone
;
1041 zone
= thermal_zone_get_zone_by_name(child
->name
);
1045 thermal_zone_device_unregister(zone
);
1048 of_thermal_free_zone(zone
->devdata
);