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>
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/err.h>
12 #include <linux/export.h>
13 #include <linux/of_device.h>
14 #include <linux/of_platform.h>
15 #include <linux/slab.h>
16 #include <linux/thermal.h>
17 #include <linux/types.h>
18 #include <linux/string.h>
20 #include "thermal_core.h"
22 /*** Private data structures to represent thermal device tree data ***/
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
31 struct __thermal_cooling_bind_param {
32 struct device_node *cooling_device;
38 * struct __thermal_bind_params - 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
45 struct __thermal_bind_params {
46 struct __thermal_cooling_bind_param *tcbp;
53 * struct __thermal_zone - internal representation of a thermal zone
54 * @passive_delay: polling interval while passive cooling is activated
55 * @polling_delay: zone polling interval
56 * @slope: slope of the temperature adjustment curve
57 * @offset: offset of the temperature adjustment curve
58 * @ntrips: number of trip points
59 * @trips: an array of trip points (0..ntrips - 1)
60 * @num_tbps: number of thermal bind params
61 * @tbps: an array of thermal bind params (0..num_tbps - 1)
62 * @sensor_data: sensor private data used while reading temperature and trend
63 * @ops: set of callbacks to handle the thermal zone based on DT
66 struct __thermal_zone {
74 struct thermal_trip *trips;
76 /* cooling binding data */
78 struct __thermal_bind_params *tbps;
80 /* sensor interface */
82 const struct thermal_zone_of_device_ops *ops;
85 /*** DT thermal zone device callbacks ***/
87 static int of_thermal_get_temp(struct thermal_zone_device *tz,
90 struct __thermal_zone *data = tz->devdata;
92 if (!data->ops || !data->ops->get_temp)
95 return data->ops->get_temp(data->sensor_data, temp);
98 static int of_thermal_set_trips(struct thermal_zone_device *tz,
101 struct __thermal_zone *data = tz->devdata;
103 if (!data->ops || !data->ops->set_trips)
106 return data->ops->set_trips(data->sensor_data, low, high);
110 * of_thermal_get_ntrips - function to export number of available trip
112 * @tz: pointer to a thermal zone
114 * This function is a globally visible wrapper to get number of trip points
115 * stored in the local struct __thermal_zone
117 * Return: number of available trip points, -ENODEV when data not available
119 int of_thermal_get_ntrips(struct thermal_zone_device *tz)
121 struct __thermal_zone *data = tz->devdata;
123 if (!data || IS_ERR(data))
128 EXPORT_SYMBOL_GPL(of_thermal_get_ntrips);
131 * of_thermal_is_trip_valid - function to check if trip point is valid
133 * @tz: pointer to a thermal zone
134 * @trip: trip point to evaluate
136 * This function is responsible for checking if passed trip point is valid
138 * Return: true if trip point is valid, false otherwise
140 bool of_thermal_is_trip_valid(struct thermal_zone_device *tz, int trip)
142 struct __thermal_zone *data = tz->devdata;
144 if (!data || trip >= data->ntrips || trip < 0)
149 EXPORT_SYMBOL_GPL(of_thermal_is_trip_valid);
152 * of_thermal_get_trip_points - function to get access to a globally exported
155 * @tz: pointer to a thermal zone
157 * This function provides a pointer to trip points table
159 * Return: pointer to trip points table, NULL otherwise
161 const struct thermal_trip *
162 of_thermal_get_trip_points(struct thermal_zone_device *tz)
164 struct __thermal_zone *data = tz->devdata;
171 EXPORT_SYMBOL_GPL(of_thermal_get_trip_points);
174 * of_thermal_set_emul_temp - function to set emulated temperature
176 * @tz: pointer to a thermal zone
177 * @temp: temperature to set
179 * This function gives the ability to set emulated value of temperature,
180 * which is handy for debugging
182 * Return: zero on success, error code otherwise
184 static int of_thermal_set_emul_temp(struct thermal_zone_device *tz,
187 struct __thermal_zone *data = tz->devdata;
189 if (!data->ops || !data->ops->set_emul_temp)
192 return data->ops->set_emul_temp(data->sensor_data, temp);
195 static int of_thermal_get_trend(struct thermal_zone_device *tz, int trip,
196 enum thermal_trend *trend)
198 struct __thermal_zone *data = tz->devdata;
200 if (!data->ops || !data->ops->get_trend)
203 return data->ops->get_trend(data->sensor_data, trip, trend);
206 static int of_thermal_change_mode(struct thermal_zone_device *tz,
207 enum thermal_device_mode mode)
209 struct __thermal_zone *data = tz->devdata;
211 return data->ops->change_mode(data->sensor_data, mode);
214 static int of_thermal_bind(struct thermal_zone_device *thermal,
215 struct thermal_cooling_device *cdev)
217 struct __thermal_zone *data = thermal->devdata;
218 struct __thermal_bind_params *tbp;
219 struct __thermal_cooling_bind_param *tcbp;
222 if (!data || IS_ERR(data))
225 /* find where to bind */
226 for (i = 0; i < data->num_tbps; i++) {
227 tbp = data->tbps + i;
229 for (j = 0; j < tbp->count; j++) {
230 tcbp = tbp->tcbp + j;
232 if (tcbp->cooling_device == cdev->np) {
235 ret = thermal_zone_bind_cooling_device(thermal,
249 static int of_thermal_unbind(struct thermal_zone_device *thermal,
250 struct thermal_cooling_device *cdev)
252 struct __thermal_zone *data = thermal->devdata;
253 struct __thermal_bind_params *tbp;
254 struct __thermal_cooling_bind_param *tcbp;
257 if (!data || IS_ERR(data))
260 /* find where to unbind */
261 for (i = 0; i < data->num_tbps; i++) {
262 tbp = data->tbps + i;
264 for (j = 0; j < tbp->count; j++) {
265 tcbp = tbp->tcbp + j;
267 if (tcbp->cooling_device == cdev->np) {
270 ret = thermal_zone_unbind_cooling_device(thermal,
281 static int of_thermal_get_trip_type(struct thermal_zone_device *tz, int trip,
282 enum thermal_trip_type *type)
284 struct __thermal_zone *data = tz->devdata;
286 if (trip >= data->ntrips || trip < 0)
289 *type = data->trips[trip].type;
294 static int of_thermal_get_trip_temp(struct thermal_zone_device *tz, int trip,
297 struct __thermal_zone *data = tz->devdata;
299 if (trip >= data->ntrips || trip < 0)
302 *temp = data->trips[trip].temperature;
307 static int of_thermal_set_trip_temp(struct thermal_zone_device *tz, int trip,
310 struct __thermal_zone *data = tz->devdata;
312 if (trip >= data->ntrips || trip < 0)
315 if (data->ops && data->ops->set_trip_temp) {
318 ret = data->ops->set_trip_temp(data->sensor_data, trip, temp);
323 /* thermal framework should take care of data->mask & (1 << trip) */
324 data->trips[trip].temperature = temp;
329 static int of_thermal_get_trip_hyst(struct thermal_zone_device *tz, int trip,
332 struct __thermal_zone *data = tz->devdata;
334 if (trip >= data->ntrips || trip < 0)
337 *hyst = data->trips[trip].hysteresis;
342 static int of_thermal_set_trip_hyst(struct thermal_zone_device *tz, int trip,
345 struct __thermal_zone *data = tz->devdata;
347 if (trip >= data->ntrips || trip < 0)
350 /* thermal framework should take care of data->mask & (1 << trip) */
351 data->trips[trip].hysteresis = hyst;
356 static int of_thermal_get_crit_temp(struct thermal_zone_device *tz,
359 struct __thermal_zone *data = tz->devdata;
362 for (i = 0; i < data->ntrips; i++)
363 if (data->trips[i].type == THERMAL_TRIP_CRITICAL) {
364 *temp = data->trips[i].temperature;
371 static struct thermal_zone_device_ops of_thermal_ops = {
372 .get_trip_type = of_thermal_get_trip_type,
373 .get_trip_temp = of_thermal_get_trip_temp,
374 .set_trip_temp = of_thermal_set_trip_temp,
375 .get_trip_hyst = of_thermal_get_trip_hyst,
376 .set_trip_hyst = of_thermal_set_trip_hyst,
377 .get_crit_temp = of_thermal_get_crit_temp,
379 .bind = of_thermal_bind,
380 .unbind = of_thermal_unbind,
385 static struct thermal_zone_device *
386 thermal_zone_of_add_sensor(struct device_node *zone,
387 struct device_node *sensor, void *data,
388 const struct thermal_zone_of_device_ops *ops)
390 struct thermal_zone_device *tzd;
391 struct __thermal_zone *tz;
393 tzd = thermal_zone_get_zone_by_name(zone->name);
395 return ERR_PTR(-EPROBE_DEFER);
400 return ERR_PTR(-EINVAL);
402 mutex_lock(&tzd->lock);
404 tz->sensor_data = data;
406 tzd->ops->get_temp = of_thermal_get_temp;
407 tzd->ops->get_trend = of_thermal_get_trend;
410 * The thermal zone core will calculate the window if they have set the
411 * optional set_trips pointer.
414 tzd->ops->set_trips = of_thermal_set_trips;
416 if (ops->set_emul_temp)
417 tzd->ops->set_emul_temp = of_thermal_set_emul_temp;
419 if (ops->change_mode)
420 tzd->ops->change_mode = of_thermal_change_mode;
422 mutex_unlock(&tzd->lock);
428 * thermal_zone_of_get_sensor_id - get sensor ID from a DT thermal zone
429 * @tz_np: a valid thermal zone device node.
430 * @sensor_np: a sensor node of a valid sensor device.
431 * @id: the sensor ID returned if success.
433 * This function will get sensor ID from a given thermal zone node and
434 * the sensor node must match the temperature provider @sensor_np.
436 * Return: 0 on success, proper error code otherwise.
439 int thermal_zone_of_get_sensor_id(struct device_node *tz_np,
440 struct device_node *sensor_np,
443 struct of_phandle_args sensor_specs;
446 ret = of_parse_phandle_with_args(tz_np,
448 "#thermal-sensor-cells",
454 if (sensor_specs.np != sensor_np) {
455 of_node_put(sensor_specs.np);
459 if (sensor_specs.args_count > 1)
460 pr_warn("%pOFn: too many cells in sensor specifier %d\n",
461 sensor_specs.np, sensor_specs.args_count);
463 *id = sensor_specs.args_count ? sensor_specs.args[0] : 0;
465 of_node_put(sensor_specs.np);
469 EXPORT_SYMBOL_GPL(thermal_zone_of_get_sensor_id);
472 * thermal_zone_of_sensor_register - registers a sensor to a DT thermal zone
473 * @dev: a valid struct device pointer of a sensor device. Must contain
474 * a valid .of_node, for the sensor node.
475 * @sensor_id: a sensor identifier, in case the sensor IP has more
477 * @data: a private pointer (owned by the caller) that will be passed
478 * back, when a temperature reading is needed.
479 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
481 * This function will search the list of thermal zones described in device
482 * tree and look for the zone that refer to the sensor device pointed by
483 * @dev->of_node as temperature providers. For the zone pointing to the
484 * sensor node, the sensor will be added to the DT thermal zone device.
486 * The thermal zone temperature is provided by the @get_temp function
487 * pointer. When called, it will have the private pointer @data back.
489 * The thermal zone temperature trend is provided by the @get_trend function
490 * pointer. When called, it will have the private pointer @data back.
493 * 01 - This function must enqueue the new sensor instead of using
494 * it as the only source of temperature values.
496 * 02 - There must be a way to match the sensor with all thermal zones
499 * Return: On success returns a valid struct thermal_zone_device,
500 * otherwise, it returns a corresponding ERR_PTR(). Caller must
501 * check the return value with help of IS_ERR() helper.
503 struct thermal_zone_device *
504 thermal_zone_of_sensor_register(struct device *dev, int sensor_id, void *data,
505 const struct thermal_zone_of_device_ops *ops)
507 struct device_node *np, *child, *sensor_np;
508 struct thermal_zone_device *tzd = ERR_PTR(-ENODEV);
510 np = of_find_node_by_name(NULL, "thermal-zones");
512 return ERR_PTR(-ENODEV);
514 if (!dev || !dev->of_node) {
516 return ERR_PTR(-ENODEV);
519 sensor_np = of_node_get(dev->of_node);
521 for_each_available_child_of_node(np, child) {
524 /* For now, thermal framework supports only 1 sensor per zone */
525 ret = thermal_zone_of_get_sensor_id(child, sensor_np, &id);
529 if (id == sensor_id) {
530 tzd = thermal_zone_of_add_sensor(child, sensor_np,
533 thermal_zone_device_enable(tzd);
540 of_node_put(sensor_np);
545 EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_register);
548 * thermal_zone_of_sensor_unregister - unregisters a sensor from a DT thermal zone
549 * @dev: a valid struct device pointer of a sensor device. Must contain
550 * a valid .of_node, for the sensor node.
551 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
553 * This function removes the sensor callbacks and private data from the
554 * thermal zone device registered with thermal_zone_of_sensor_register()
555 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
556 * thermal zone device callbacks.
558 * TODO: When the support to several sensors per zone is added, this
559 * function must search the sensor list based on @dev parameter.
562 void thermal_zone_of_sensor_unregister(struct device *dev,
563 struct thermal_zone_device *tzd)
565 struct __thermal_zone *tz;
567 if (!dev || !tzd || !tzd->devdata)
572 /* no __thermal_zone, nothing to be done */
576 /* stop temperature polling */
577 thermal_zone_device_disable(tzd);
579 mutex_lock(&tzd->lock);
580 tzd->ops->get_temp = NULL;
581 tzd->ops->get_trend = NULL;
582 tzd->ops->set_emul_temp = NULL;
583 tzd->ops->change_mode = NULL;
586 tz->sensor_data = NULL;
587 mutex_unlock(&tzd->lock);
589 EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister);
591 static void devm_thermal_zone_of_sensor_release(struct device *dev, void *res)
593 thermal_zone_of_sensor_unregister(dev,
594 *(struct thermal_zone_device **)res);
597 static int devm_thermal_zone_of_sensor_match(struct device *dev, void *res,
600 struct thermal_zone_device **r = res;
602 if (WARN_ON(!r || !*r))
609 * devm_thermal_zone_of_sensor_register - Resource managed version of
610 * thermal_zone_of_sensor_register()
611 * @dev: a valid struct device pointer of a sensor device. Must contain
612 * a valid .of_node, for the sensor node.
613 * @sensor_id: a sensor identifier, in case the sensor IP has more
615 * @data: a private pointer (owned by the caller) that will be passed
616 * back, when a temperature reading is needed.
617 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
619 * Refer thermal_zone_of_sensor_register() for more details.
621 * Return: On success returns a valid struct thermal_zone_device,
622 * otherwise, it returns a corresponding ERR_PTR(). Caller must
623 * check the return value with help of IS_ERR() helper.
624 * Registered thermal_zone_device device will automatically be
625 * released when device is unbounded.
627 struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
628 struct device *dev, int sensor_id,
629 void *data, const struct thermal_zone_of_device_ops *ops)
631 struct thermal_zone_device **ptr, *tzd;
633 ptr = devres_alloc(devm_thermal_zone_of_sensor_release, sizeof(*ptr),
636 return ERR_PTR(-ENOMEM);
638 tzd = thermal_zone_of_sensor_register(dev, sensor_id, data, ops);
645 devres_add(dev, ptr);
649 EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_register);
652 * devm_thermal_zone_of_sensor_unregister - Resource managed version of
653 * thermal_zone_of_sensor_unregister().
654 * @dev: Device for which which resource was allocated.
655 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
657 * This function removes the sensor callbacks and private data from the
658 * thermal zone device registered with devm_thermal_zone_of_sensor_register()
659 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
660 * thermal zone device callbacks.
661 * Normally this function will not need to be called and the resource
662 * management code will ensure that the resource is freed.
664 void devm_thermal_zone_of_sensor_unregister(struct device *dev,
665 struct thermal_zone_device *tzd)
667 WARN_ON(devres_release(dev, devm_thermal_zone_of_sensor_release,
668 devm_thermal_zone_of_sensor_match, tzd));
670 EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_unregister);
672 /*** functions parsing device tree nodes ***/
675 * thermal_of_populate_bind_params - parse and fill cooling map data
676 * @np: DT node containing a cooling-map node
677 * @__tbp: data structure to be filled with cooling map info
678 * @trips: array of thermal zone trip points
679 * @ntrips: number of trip points inside trips.
681 * This function parses a cooling-map type of node represented by
682 * @np parameter and fills the read data into @__tbp data structure.
683 * It needs the already parsed array of trip points of the thermal zone
686 * Return: 0 on success, proper error code otherwise
688 static int thermal_of_populate_bind_params(struct device_node *np,
689 struct __thermal_bind_params *__tbp,
690 struct thermal_trip *trips,
693 struct of_phandle_args cooling_spec;
694 struct __thermal_cooling_bind_param *__tcbp;
695 struct device_node *trip;
699 /* Default weight. Usage is optional */
700 __tbp->usage = THERMAL_WEIGHT_DEFAULT;
701 ret = of_property_read_u32(np, "contribution", &prop);
705 trip = of_parse_phandle(np, "trip", 0);
707 pr_err("missing trip property\n");
711 /* match using device_node */
712 for (i = 0; i < ntrips; i++)
713 if (trip == trips[i].np) {
723 count = of_count_phandle_with_args(np, "cooling-device",
726 pr_err("Add a cooling_device property with at least one device\n");
731 __tcbp = kcalloc(count, sizeof(*__tcbp), GFP_KERNEL);
737 for (i = 0; i < count; i++) {
738 ret = of_parse_phandle_with_args(np, "cooling-device",
739 "#cooling-cells", i, &cooling_spec);
741 pr_err("Invalid cooling-device entry\n");
745 __tcbp[i].cooling_device = cooling_spec.np;
747 if (cooling_spec.args_count >= 2) { /* at least min and max */
748 __tcbp[i].min = cooling_spec.args[0];
749 __tcbp[i].max = cooling_spec.args[1];
751 pr_err("wrong reference to cooling device, missing limits\n");
755 __tbp->tcbp = __tcbp;
756 __tbp->count = count;
761 for (i = i - 1; i >= 0; i--)
762 of_node_put(__tcbp[i].cooling_device);
771 * It maps 'enum thermal_trip_type' found in include/linux/thermal.h
772 * into the device tree binding of 'trip', property type.
774 static const char * const trip_types[] = {
775 [THERMAL_TRIP_ACTIVE] = "active",
776 [THERMAL_TRIP_PASSIVE] = "passive",
777 [THERMAL_TRIP_HOT] = "hot",
778 [THERMAL_TRIP_CRITICAL] = "critical",
782 * thermal_of_get_trip_type - Get phy mode for given device_node
783 * @np: Pointer to the given device_node
784 * @type: Pointer to resulting trip type
786 * The function gets trip type string from property 'type',
787 * and store its index in trip_types table in @type,
789 * Return: 0 on success, or errno in error case.
791 static int thermal_of_get_trip_type(struct device_node *np,
792 enum thermal_trip_type *type)
797 err = of_property_read_string(np, "type", &t);
801 for (i = 0; i < ARRAY_SIZE(trip_types); i++)
802 if (!strcasecmp(t, trip_types[i])) {
811 * thermal_of_populate_trip - parse and fill one trip point data
812 * @np: DT node containing a trip point node
813 * @trip: trip point data structure to be filled up
815 * This function parses a trip point type of node represented by
816 * @np parameter and fills the read data into @trip data structure.
818 * Return: 0 on success, proper error code otherwise
820 static int thermal_of_populate_trip(struct device_node *np,
821 struct thermal_trip *trip)
826 ret = of_property_read_u32(np, "temperature", &prop);
828 pr_err("missing temperature property\n");
831 trip->temperature = prop;
833 ret = of_property_read_u32(np, "hysteresis", &prop);
835 pr_err("missing hysteresis property\n");
838 trip->hysteresis = prop;
840 ret = thermal_of_get_trip_type(np, &trip->type);
842 pr_err("wrong trip type property\n");
846 /* Required for cooling map matching */
854 * thermal_of_build_thermal_zone - parse and fill one thermal zone data
855 * @np: DT node containing a thermal zone node
857 * This function parses a thermal zone type of node represented by
858 * @np parameter and fills the read data into a __thermal_zone data structure
859 * and return this pointer.
861 * TODO: Missing properties to parse: thermal-sensor-names
863 * Return: On success returns a valid struct __thermal_zone,
864 * otherwise, it returns a corresponding ERR_PTR(). Caller must
865 * check the return value with help of IS_ERR() helper.
867 static struct __thermal_zone
868 __init *thermal_of_build_thermal_zone(struct device_node *np)
870 struct device_node *child = NULL, *gchild;
871 struct __thermal_zone *tz;
876 pr_err("no thermal zone np\n");
877 return ERR_PTR(-EINVAL);
880 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
882 return ERR_PTR(-ENOMEM);
884 ret = of_property_read_u32(np, "polling-delay-passive", &prop);
886 pr_err("%pOFn: missing polling-delay-passive property\n", np);
889 tz->passive_delay = prop;
891 ret = of_property_read_u32(np, "polling-delay", &prop);
893 pr_err("%pOFn: missing polling-delay property\n", np);
896 tz->polling_delay = prop;
899 * REVIST: for now, the thermal framework supports only
900 * one sensor per thermal zone. Thus, we are considering
901 * only the first two values as slope and offset.
903 ret = of_property_read_u32_array(np, "coefficients", coef, 2);
906 tz->offset = coef[1];
913 child = of_get_child_by_name(np, "trips");
915 /* No trips provided */
919 tz->ntrips = of_get_child_count(child);
920 if (tz->ntrips == 0) /* must have at least one child */
923 tz->trips = kcalloc(tz->ntrips, sizeof(*tz->trips), GFP_KERNEL);
930 for_each_child_of_node(child, gchild) {
931 ret = thermal_of_populate_trip(gchild, &tz->trips[i++]);
939 child = of_get_child_by_name(np, "cooling-maps");
941 /* cooling-maps not provided */
945 tz->num_tbps = of_get_child_count(child);
946 if (tz->num_tbps == 0)
949 tz->tbps = kcalloc(tz->num_tbps, sizeof(*tz->tbps), GFP_KERNEL);
956 for_each_child_of_node(child, gchild) {
957 ret = thermal_of_populate_bind_params(gchild, &tz->tbps[i++],
958 tz->trips, tz->ntrips);
969 for (i = i - 1; i >= 0; i--) {
970 struct __thermal_bind_params *tbp = tz->tbps + i;
973 for (j = 0; j < tbp->count; j++)
974 of_node_put(tbp->tcbp[j].cooling_device);
981 for (i = 0; i < tz->ntrips; i++)
982 of_node_put(tz->trips[i].np);
992 static __init void of_thermal_free_zone(struct __thermal_zone *tz)
994 struct __thermal_bind_params *tbp;
997 for (i = 0; i < tz->num_tbps; i++) {
1000 for (j = 0; j < tbp->count; j++)
1001 of_node_put(tbp->tcbp[j].cooling_device);
1007 for (i = 0; i < tz->ntrips; i++)
1008 of_node_put(tz->trips[i].np);
1014 * of_thermal_destroy_zones - remove all zones parsed and allocated resources
1016 * Finds all zones parsed and added to the thermal framework and remove them
1017 * from the system, together with their resources.
1020 static __init void of_thermal_destroy_zones(void)
1022 struct device_node *np, *child;
1024 np = of_find_node_by_name(NULL, "thermal-zones");
1026 pr_debug("unable to find thermal zones\n");
1030 for_each_available_child_of_node(np, child) {
1031 struct thermal_zone_device *zone;
1033 zone = thermal_zone_get_zone_by_name(child->name);
1037 thermal_zone_device_unregister(zone);
1040 of_thermal_free_zone(zone->devdata);
1046 * of_parse_thermal_zones - parse device tree thermal data
1048 * Initialization function that can be called by machine initialization
1049 * code to parse thermal data and populate the thermal framework
1050 * with hardware thermal zones info. This function only parses thermal zones.
1051 * Cooling devices and sensor devices nodes are supposed to be parsed
1052 * by their respective drivers.
1054 * Return: 0 on success, proper error code otherwise
1057 int __init of_parse_thermal_zones(void)
1059 struct device_node *np, *child;
1060 struct __thermal_zone *tz;
1061 struct thermal_zone_device_ops *ops;
1063 np = of_find_node_by_name(NULL, "thermal-zones");
1065 pr_debug("unable to find thermal zones\n");
1066 return 0; /* Run successfully on systems without thermal DT */
1069 for_each_available_child_of_node(np, child) {
1070 struct thermal_zone_device *zone;
1071 struct thermal_zone_params *tzp;
1075 tz = thermal_of_build_thermal_zone(child);
1077 pr_err("failed to build thermal zone %pOFn: %ld\n",
1083 ops = kmemdup(&of_thermal_ops, sizeof(*ops), GFP_KERNEL);
1087 tzp = kzalloc(sizeof(*tzp), GFP_KERNEL);
1093 /* No hwmon because there might be hwmon drivers registering */
1094 tzp->no_hwmon = true;
1096 if (!of_property_read_u32(child, "sustainable-power", &prop))
1097 tzp->sustainable_power = prop;
1099 for (i = 0; i < tz->ntrips; i++)
1102 /* these two are left for temperature drivers to use */
1103 tzp->slope = tz->slope;
1104 tzp->offset = tz->offset;
1106 zone = thermal_zone_device_register(child->name, tz->ntrips,
1112 pr_err("Failed to build %pOFn zone %ld\n", child,
1116 of_thermal_free_zone(tz);
1117 /* attempting to build remaining zones still */
1127 of_thermal_free_zone(tz);
1129 /* no memory available, so free what we have built */
1130 of_thermal_destroy_zones();