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
;
265 tz
->passive_delay
= data
->passive_delay
;
267 tz
->polling_delay
= 0;
268 tz
->passive_delay
= 0;
271 mutex_unlock(&tz
->lock
);
274 thermal_zone_device_update(tz
, THERMAL_EVENT_UNSPECIFIED
);
279 static int of_thermal_get_trip_type(struct thermal_zone_device
*tz
, int trip
,
280 enum thermal_trip_type
*type
)
282 struct __thermal_zone
*data
= tz
->devdata
;
284 if (trip
>= data
->ntrips
|| trip
< 0)
287 *type
= data
->trips
[trip
].type
;
292 static int of_thermal_get_trip_temp(struct thermal_zone_device
*tz
, int trip
,
295 struct __thermal_zone
*data
= tz
->devdata
;
297 if (trip
>= data
->ntrips
|| trip
< 0)
300 *temp
= data
->trips
[trip
].temperature
;
305 static int of_thermal_set_trip_temp(struct thermal_zone_device
*tz
, int trip
,
308 struct __thermal_zone
*data
= tz
->devdata
;
310 if (trip
>= data
->ntrips
|| trip
< 0)
313 if (data
->ops
->set_trip_temp
) {
316 ret
= data
->ops
->set_trip_temp(data
->sensor_data
, trip
, temp
);
321 /* thermal framework should take care of data->mask & (1 << trip) */
322 data
->trips
[trip
].temperature
= temp
;
327 static int of_thermal_get_trip_hyst(struct thermal_zone_device
*tz
, int trip
,
330 struct __thermal_zone
*data
= tz
->devdata
;
332 if (trip
>= data
->ntrips
|| trip
< 0)
335 *hyst
= data
->trips
[trip
].hysteresis
;
340 static int of_thermal_set_trip_hyst(struct thermal_zone_device
*tz
, int trip
,
343 struct __thermal_zone
*data
= tz
->devdata
;
345 if (trip
>= data
->ntrips
|| trip
< 0)
348 /* thermal framework should take care of data->mask & (1 << trip) */
349 data
->trips
[trip
].hysteresis
= hyst
;
354 static int of_thermal_get_crit_temp(struct thermal_zone_device
*tz
,
357 struct __thermal_zone
*data
= tz
->devdata
;
360 for (i
= 0; i
< data
->ntrips
; i
++)
361 if (data
->trips
[i
].type
== THERMAL_TRIP_CRITICAL
) {
362 *temp
= data
->trips
[i
].temperature
;
369 static struct thermal_zone_device_ops of_thermal_ops
= {
370 .get_mode
= of_thermal_get_mode
,
371 .set_mode
= of_thermal_set_mode
,
373 .get_trip_type
= of_thermal_get_trip_type
,
374 .get_trip_temp
= of_thermal_get_trip_temp
,
375 .set_trip_temp
= of_thermal_set_trip_temp
,
376 .get_trip_hyst
= of_thermal_get_trip_hyst
,
377 .set_trip_hyst
= of_thermal_set_trip_hyst
,
378 .get_crit_temp
= of_thermal_get_crit_temp
,
380 .bind
= of_thermal_bind
,
381 .unbind
= of_thermal_unbind
,
386 static struct thermal_zone_device
*
387 thermal_zone_of_add_sensor(struct device_node
*zone
,
388 struct device_node
*sensor
, void *data
,
389 const struct thermal_zone_of_device_ops
*ops
)
391 struct thermal_zone_device
*tzd
;
392 struct __thermal_zone
*tz
;
394 tzd
= thermal_zone_get_zone_by_name(zone
->name
);
396 return ERR_PTR(-EPROBE_DEFER
);
401 return ERR_PTR(-EINVAL
);
403 mutex_lock(&tzd
->lock
);
405 tz
->sensor_data
= data
;
407 tzd
->ops
->get_temp
= of_thermal_get_temp
;
408 tzd
->ops
->get_trend
= of_thermal_get_trend
;
411 * The thermal zone core will calculate the window if they have set the
412 * optional set_trips pointer.
415 tzd
->ops
->set_trips
= of_thermal_set_trips
;
417 if (ops
->set_emul_temp
)
418 tzd
->ops
->set_emul_temp
= of_thermal_set_emul_temp
;
420 mutex_unlock(&tzd
->lock
);
426 * thermal_zone_of_sensor_register - registers a sensor to a DT thermal zone
427 * @dev: a valid struct device pointer of a sensor device. Must contain
428 * a valid .of_node, for the sensor node.
429 * @sensor_id: a sensor identifier, in case the sensor IP has more
431 * @data: a private pointer (owned by the caller) that will be passed
432 * back, when a temperature reading is needed.
433 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
435 * This function will search the list of thermal zones described in device
436 * tree and look for the zone that refer to the sensor device pointed by
437 * @dev->of_node as temperature providers. For the zone pointing to the
438 * sensor node, the sensor will be added to the DT thermal zone device.
440 * The thermal zone temperature is provided by the @get_temp function
441 * pointer. When called, it will have the private pointer @data back.
443 * The thermal zone temperature trend is provided by the @get_trend function
444 * pointer. When called, it will have the private pointer @data back.
447 * 01 - This function must enqueue the new sensor instead of using
448 * it as the only source of temperature values.
450 * 02 - There must be a way to match the sensor with all thermal zones
453 * Return: On success returns a valid struct thermal_zone_device,
454 * otherwise, it returns a corresponding ERR_PTR(). Caller must
455 * check the return value with help of IS_ERR() helper.
457 struct thermal_zone_device
*
458 thermal_zone_of_sensor_register(struct device
*dev
, int sensor_id
, void *data
,
459 const struct thermal_zone_of_device_ops
*ops
)
461 struct device_node
*np
, *child
, *sensor_np
;
462 struct thermal_zone_device
*tzd
= ERR_PTR(-ENODEV
);
464 np
= of_find_node_by_name(NULL
, "thermal-zones");
466 return ERR_PTR(-ENODEV
);
468 if (!dev
|| !dev
->of_node
) {
470 return ERR_PTR(-EINVAL
);
473 sensor_np
= of_node_get(dev
->of_node
);
475 for_each_available_child_of_node(np
, child
) {
476 struct of_phandle_args sensor_specs
;
479 /* For now, thermal framework supports only 1 sensor per zone */
480 ret
= of_parse_phandle_with_args(child
, "thermal-sensors",
481 "#thermal-sensor-cells",
486 if (sensor_specs
.args_count
>= 1) {
487 id
= sensor_specs
.args
[0];
488 WARN(sensor_specs
.args_count
> 1,
489 "%s: too many cells in sensor specifier %d\n",
490 sensor_specs
.np
->name
, sensor_specs
.args_count
);
495 if (sensor_specs
.np
== sensor_np
&& id
== sensor_id
) {
496 tzd
= thermal_zone_of_add_sensor(child
, sensor_np
,
499 tzd
->ops
->set_mode(tzd
, THERMAL_DEVICE_ENABLED
);
501 of_node_put(sensor_specs
.np
);
505 of_node_put(sensor_specs
.np
);
508 of_node_put(sensor_np
);
513 EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_register
);
516 * thermal_zone_of_sensor_unregister - unregisters a sensor from a DT thermal zone
517 * @dev: a valid struct device pointer of a sensor device. Must contain
518 * a valid .of_node, for the sensor node.
519 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
521 * This function removes the sensor callbacks and private data from the
522 * thermal zone device registered with thermal_zone_of_sensor_register()
523 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
524 * thermal zone device callbacks.
526 * TODO: When the support to several sensors per zone is added, this
527 * function must search the sensor list based on @dev parameter.
530 void thermal_zone_of_sensor_unregister(struct device
*dev
,
531 struct thermal_zone_device
*tzd
)
533 struct __thermal_zone
*tz
;
535 if (!dev
|| !tzd
|| !tzd
->devdata
)
540 /* no __thermal_zone, nothing to be done */
544 mutex_lock(&tzd
->lock
);
545 tzd
->ops
->get_temp
= NULL
;
546 tzd
->ops
->get_trend
= NULL
;
547 tzd
->ops
->set_emul_temp
= NULL
;
550 tz
->sensor_data
= NULL
;
551 mutex_unlock(&tzd
->lock
);
553 EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister
);
555 static void devm_thermal_zone_of_sensor_release(struct device
*dev
, void *res
)
557 thermal_zone_of_sensor_unregister(dev
,
558 *(struct thermal_zone_device
**)res
);
561 static int devm_thermal_zone_of_sensor_match(struct device
*dev
, void *res
,
564 struct thermal_zone_device
**r
= res
;
566 if (WARN_ON(!r
|| !*r
))
573 * devm_thermal_zone_of_sensor_register - Resource managed version of
574 * thermal_zone_of_sensor_register()
575 * @dev: a valid struct device pointer of a sensor device. Must contain
576 * a valid .of_node, for the sensor node.
577 * @sensor_id: a sensor identifier, in case the sensor IP has more
579 * @data: a private pointer (owned by the caller) that will be passed
580 * back, when a temperature reading is needed.
581 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
583 * Refer thermal_zone_of_sensor_register() for more details.
585 * Return: On success returns a valid struct thermal_zone_device,
586 * otherwise, it returns a corresponding ERR_PTR(). Caller must
587 * check the return value with help of IS_ERR() helper.
588 * Registered thermal_zone_device device will automatically be
589 * released when device is unbounded.
591 struct thermal_zone_device
*devm_thermal_zone_of_sensor_register(
592 struct device
*dev
, int sensor_id
,
593 void *data
, const struct thermal_zone_of_device_ops
*ops
)
595 struct thermal_zone_device
**ptr
, *tzd
;
597 ptr
= devres_alloc(devm_thermal_zone_of_sensor_release
, sizeof(*ptr
),
600 return ERR_PTR(-ENOMEM
);
602 tzd
= thermal_zone_of_sensor_register(dev
, sensor_id
, data
, ops
);
609 devres_add(dev
, ptr
);
613 EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_register
);
616 * devm_thermal_zone_of_sensor_unregister - Resource managed version of
617 * thermal_zone_of_sensor_unregister().
618 * @dev: Device for which which resource was allocated.
619 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
621 * This function removes the sensor callbacks and private data from the
622 * thermal zone device registered with devm_thermal_zone_of_sensor_register()
623 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
624 * thermal zone device callbacks.
625 * Normally this function will not need to be called and the resource
626 * management code will ensure that the resource is freed.
628 void devm_thermal_zone_of_sensor_unregister(struct device
*dev
,
629 struct thermal_zone_device
*tzd
)
631 WARN_ON(devres_release(dev
, devm_thermal_zone_of_sensor_release
,
632 devm_thermal_zone_of_sensor_match
, tzd
));
634 EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_unregister
);
636 /*** functions parsing device tree nodes ***/
639 * thermal_of_populate_bind_params - parse and fill cooling map data
640 * @np: DT node containing a cooling-map node
641 * @__tbp: data structure to be filled with cooling map info
642 * @trips: array of thermal zone trip points
643 * @ntrips: number of trip points inside trips.
645 * This function parses a cooling-map type of node represented by
646 * @np parameter and fills the read data into @__tbp data structure.
647 * It needs the already parsed array of trip points of the thermal zone
650 * Return: 0 on success, proper error code otherwise
652 static int thermal_of_populate_bind_params(struct device_node
*np
,
653 struct __thermal_bind_params
*__tbp
,
654 struct thermal_trip
*trips
,
657 struct of_phandle_args cooling_spec
;
658 struct device_node
*trip
;
662 /* Default weight. Usage is optional */
663 __tbp
->usage
= THERMAL_WEIGHT_DEFAULT
;
664 ret
= of_property_read_u32(np
, "contribution", &prop
);
668 trip
= of_parse_phandle(np
, "trip", 0);
670 pr_err("missing trip property\n");
674 /* match using device_node */
675 for (i
= 0; i
< ntrips
; i
++)
676 if (trip
== trips
[i
].np
) {
686 ret
= of_parse_phandle_with_args(np
, "cooling-device", "#cooling-cells",
689 pr_err("missing cooling_device property\n");
692 __tbp
->cooling_device
= cooling_spec
.np
;
693 if (cooling_spec
.args_count
>= 2) { /* at least min and max */
694 __tbp
->min
= cooling_spec
.args
[0];
695 __tbp
->max
= cooling_spec
.args
[1];
697 pr_err("wrong reference to cooling device, missing limits\n");
707 * It maps 'enum thermal_trip_type' found in include/linux/thermal.h
708 * into the device tree binding of 'trip', property type.
710 static const char * const trip_types
[] = {
711 [THERMAL_TRIP_ACTIVE
] = "active",
712 [THERMAL_TRIP_PASSIVE
] = "passive",
713 [THERMAL_TRIP_HOT
] = "hot",
714 [THERMAL_TRIP_CRITICAL
] = "critical",
718 * thermal_of_get_trip_type - Get phy mode for given device_node
719 * @np: Pointer to the given device_node
720 * @type: Pointer to resulting trip type
722 * The function gets trip type string from property 'type',
723 * and store its index in trip_types table in @type,
725 * Return: 0 on success, or errno in error case.
727 static int thermal_of_get_trip_type(struct device_node
*np
,
728 enum thermal_trip_type
*type
)
733 err
= of_property_read_string(np
, "type", &t
);
737 for (i
= 0; i
< ARRAY_SIZE(trip_types
); i
++)
738 if (!strcasecmp(t
, trip_types
[i
])) {
747 * thermal_of_populate_trip - parse and fill one trip point data
748 * @np: DT node containing a trip point node
749 * @trip: trip point data structure to be filled up
751 * This function parses a trip point type of node represented by
752 * @np parameter and fills the read data into @trip data structure.
754 * Return: 0 on success, proper error code otherwise
756 static int thermal_of_populate_trip(struct device_node
*np
,
757 struct thermal_trip
*trip
)
762 ret
= of_property_read_u32(np
, "temperature", &prop
);
764 pr_err("missing temperature property\n");
767 trip
->temperature
= prop
;
769 ret
= of_property_read_u32(np
, "hysteresis", &prop
);
771 pr_err("missing hysteresis property\n");
774 trip
->hysteresis
= prop
;
776 ret
= thermal_of_get_trip_type(np
, &trip
->type
);
778 pr_err("wrong trip type property\n");
782 /* Required for cooling map matching */
790 * thermal_of_build_thermal_zone - parse and fill one thermal zone data
791 * @np: DT node containing a thermal zone node
793 * This function parses a thermal zone type of node represented by
794 * @np parameter and fills the read data into a __thermal_zone data structure
795 * and return this pointer.
797 * TODO: Missing properties to parse: thermal-sensor-names
799 * Return: On success returns a valid struct __thermal_zone,
800 * otherwise, it returns a corresponding ERR_PTR(). Caller must
801 * check the return value with help of IS_ERR() helper.
803 static struct __thermal_zone
804 __init
*thermal_of_build_thermal_zone(struct device_node
*np
)
806 struct device_node
*child
= NULL
, *gchild
;
807 struct __thermal_zone
*tz
;
812 pr_err("no thermal zone np\n");
813 return ERR_PTR(-EINVAL
);
816 tz
= kzalloc(sizeof(*tz
), GFP_KERNEL
);
818 return ERR_PTR(-ENOMEM
);
820 ret
= of_property_read_u32(np
, "polling-delay-passive", &prop
);
822 pr_err("missing polling-delay-passive property\n");
825 tz
->passive_delay
= prop
;
827 ret
= of_property_read_u32(np
, "polling-delay", &prop
);
829 pr_err("missing polling-delay property\n");
832 tz
->polling_delay
= prop
;
835 * REVIST: for now, the thermal framework supports only
836 * one sensor per thermal zone. Thus, we are considering
837 * only the first two values as slope and offset.
839 ret
= of_property_read_u32_array(np
, "coefficients", coef
, 2);
842 tz
->offset
= coef
[1];
849 child
= of_get_child_by_name(np
, "trips");
851 /* No trips provided */
855 tz
->ntrips
= of_get_child_count(child
);
856 if (tz
->ntrips
== 0) /* must have at least one child */
859 tz
->trips
= kcalloc(tz
->ntrips
, sizeof(*tz
->trips
), GFP_KERNEL
);
866 for_each_child_of_node(child
, gchild
) {
867 ret
= thermal_of_populate_trip(gchild
, &tz
->trips
[i
++]);
875 child
= of_get_child_by_name(np
, "cooling-maps");
877 /* cooling-maps not provided */
881 tz
->num_tbps
= of_get_child_count(child
);
882 if (tz
->num_tbps
== 0)
885 tz
->tbps
= kcalloc(tz
->num_tbps
, sizeof(*tz
->tbps
), GFP_KERNEL
);
892 for_each_child_of_node(child
, gchild
) {
893 ret
= thermal_of_populate_bind_params(gchild
, &tz
->tbps
[i
++],
894 tz
->trips
, tz
->ntrips
);
901 tz
->mode
= THERMAL_DEVICE_DISABLED
;
906 for (i
= i
- 1; i
>= 0; i
--)
907 of_node_put(tz
->tbps
[i
].cooling_device
);
910 for (i
= 0; i
< tz
->ntrips
; i
++)
911 of_node_put(tz
->trips
[i
].np
);
921 static inline void of_thermal_free_zone(struct __thermal_zone
*tz
)
925 for (i
= 0; i
< tz
->num_tbps
; i
++)
926 of_node_put(tz
->tbps
[i
].cooling_device
);
928 for (i
= 0; i
< tz
->ntrips
; i
++)
929 of_node_put(tz
->trips
[i
].np
);
935 * of_parse_thermal_zones - parse device tree thermal data
937 * Initialization function that can be called by machine initialization
938 * code to parse thermal data and populate the thermal framework
939 * with hardware thermal zones info. This function only parses thermal zones.
940 * Cooling devices and sensor devices nodes are supposed to be parsed
941 * by their respective drivers.
943 * Return: 0 on success, proper error code otherwise
946 int __init
of_parse_thermal_zones(void)
948 struct device_node
*np
, *child
;
949 struct __thermal_zone
*tz
;
950 struct thermal_zone_device_ops
*ops
;
952 np
= of_find_node_by_name(NULL
, "thermal-zones");
954 pr_debug("unable to find thermal zones\n");
955 return 0; /* Run successfully on systems without thermal DT */
958 for_each_available_child_of_node(np
, child
) {
959 struct thermal_zone_device
*zone
;
960 struct thermal_zone_params
*tzp
;
964 tz
= thermal_of_build_thermal_zone(child
);
966 pr_err("failed to build thermal zone %s: %ld\n",
972 ops
= kmemdup(&of_thermal_ops
, sizeof(*ops
), GFP_KERNEL
);
976 tzp
= kzalloc(sizeof(*tzp
), GFP_KERNEL
);
982 /* No hwmon because there might be hwmon drivers registering */
983 tzp
->no_hwmon
= true;
985 if (!of_property_read_u32(child
, "sustainable-power", &prop
))
986 tzp
->sustainable_power
= prop
;
988 for (i
= 0; i
< tz
->ntrips
; i
++)
991 /* these two are left for temperature drivers to use */
992 tzp
->slope
= tz
->slope
;
993 tzp
->offset
= tz
->offset
;
995 zone
= thermal_zone_device_register(child
->name
, tz
->ntrips
,
1001 pr_err("Failed to build %s zone %ld\n", child
->name
,
1005 of_thermal_free_zone(tz
);
1006 /* attempting to build remaining zones still */
1016 of_thermal_free_zone(tz
);
1018 /* no memory available, so free what we have built */
1019 of_thermal_destroy_zones();
1025 * of_thermal_destroy_zones - remove all zones parsed and allocated resources
1027 * Finds all zones parsed and added to the thermal framework and remove them
1028 * from the system, together with their resources.
1031 void of_thermal_destroy_zones(void)
1033 struct device_node
*np
, *child
;
1035 np
= of_find_node_by_name(NULL
, "thermal-zones");
1037 pr_debug("unable to find thermal zones\n");
1041 for_each_available_child_of_node(np
, child
) {
1042 struct thermal_zone_device
*zone
;
1044 zone
= thermal_zone_get_zone_by_name(child
->name
);
1048 thermal_zone_device_unregister(zone
);
1051 of_thermal_free_zone(zone
->devdata
);