2 * of-thermal.c - Generic Thermal Management device tree support.
4 * Copyright (C) 2013 Texas Instruments
5 * Copyright (C) 2013 Eduardo Valentin <eduardo.valentin@ti.com>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 #include <linux/thermal.h>
26 #include <linux/slab.h>
27 #include <linux/types.h>
28 #include <linux/of_device.h>
29 #include <linux/of_platform.h>
30 #include <linux/err.h>
31 #include <linux/export.h>
32 #include <linux/string.h>
33 #include <linux/thermal.h>
35 #include "thermal_core.h"
37 /*** Private data structures to represent thermal device tree data ***/
40 * struct __thermal_bind_param - a match between trip and cooling device
41 * @cooling_device: a pointer to identify the referred cooling device
42 * @trip_id: the trip point index
43 * @usage: the percentage (from 0 to 100) of cooling contribution
44 * @min: minimum cooling state used at this trip point
45 * @max: maximum cooling state used at this trip point
48 struct __thermal_bind_params {
49 struct device_node *cooling_device;
57 * struct __thermal_zone - internal representation of a thermal zone
58 * @mode: current thermal zone device mode (enabled/disabled)
59 * @passive_delay: polling interval while passive cooling is activated
60 * @polling_delay: zone polling interval
61 * @slope: slope of the temperature adjustment curve
62 * @offset: offset of the temperature adjustment curve
63 * @prev_low_trip: previous low trip point temperature
64 * @prev_high_trip: previous high trip point temperature
65 * @ntrips: number of trip points
66 * @trips: an array of trip points (0..ntrips - 1)
67 * @num_tbps: number of thermal bind params
68 * @tbps: an array of thermal bind params (0..num_tbps - 1)
69 * @sensor_data: sensor private data used while reading temperature and trend
70 * @ops: set of callbacks to handle the thermal zone based on DT
73 struct __thermal_zone {
74 enum thermal_device_mode mode;
79 int prev_low_trip, prev_high_trip;
83 struct thermal_trip *trips;
85 /* cooling binding data */
87 struct __thermal_bind_params *tbps;
89 /* sensor interface */
91 const struct thermal_zone_of_device_ops *ops;
94 /*** Automatic trip handling ***/
96 static int of_thermal_set_trips(struct thermal_zone_device *tz, int temp)
98 struct __thermal_zone *data = tz->devdata;
99 int low = INT_MIN, high = INT_MAX;
102 /* Hardware trip points not supported */
103 if (!data->ops->set_trips)
106 /* No need to change trip points */
107 if (temp > data->prev_low_trip && temp < data->prev_high_trip)
110 for (i = 0; i < data->ntrips; ++i) {
111 struct thermal_trip *trip = data->trips + i;
112 int trip_low = trip->temperature - trip->hysteresis;
114 if (trip_low < temp && trip_low > low)
117 if (trip->temperature > temp && trip->temperature < high)
118 high = trip->temperature;
122 "temperature %d, updating trip points to %d, %d\n",
125 data->prev_low_trip = low;
126 data->prev_high_trip = high;
128 return data->ops->set_trips(data->sensor_data, low, high);
131 /*** DT thermal zone device callbacks ***/
133 static int of_thermal_get_temp(struct thermal_zone_device *tz,
136 struct __thermal_zone *data = tz->devdata;
139 if (!data->ops->get_temp)
142 err = data->ops->get_temp(data->sensor_data, temp);
146 err = of_thermal_set_trips(tz, *temp);
154 * of_thermal_get_ntrips - function to export number of available trip
156 * @tz: pointer to a thermal zone
158 * This function is a globally visible wrapper to get number of trip points
159 * stored in the local struct __thermal_zone
161 * Return: number of available trip points, -ENODEV when data not available
163 int of_thermal_get_ntrips(struct thermal_zone_device *tz)
165 struct __thermal_zone *data = tz->devdata;
167 if (!data || IS_ERR(data))
172 EXPORT_SYMBOL_GPL(of_thermal_get_ntrips);
175 * of_thermal_is_trip_valid - function to check if trip point is valid
177 * @tz: pointer to a thermal zone
178 * @trip: trip point to evaluate
180 * This function is responsible for checking if passed trip point is valid
182 * Return: true if trip point is valid, false otherwise
184 bool of_thermal_is_trip_valid(struct thermal_zone_device *tz, int trip)
186 struct __thermal_zone *data = tz->devdata;
188 if (!data || trip >= data->ntrips || trip < 0)
193 EXPORT_SYMBOL_GPL(of_thermal_is_trip_valid);
196 * of_thermal_get_trip_points - function to get access to a globally exported
199 * @tz: pointer to a thermal zone
201 * This function provides a pointer to trip points table
203 * Return: pointer to trip points table, NULL otherwise
205 const struct thermal_trip *
206 of_thermal_get_trip_points(struct thermal_zone_device *tz)
208 struct __thermal_zone *data = tz->devdata;
215 EXPORT_SYMBOL_GPL(of_thermal_get_trip_points);
218 * of_thermal_set_emul_temp - function to set emulated temperature
220 * @tz: pointer to a thermal zone
221 * @temp: temperature to set
223 * This function gives the ability to set emulated value of temperature,
224 * which is handy for debugging
226 * Return: zero on success, error code otherwise
228 static int of_thermal_set_emul_temp(struct thermal_zone_device *tz,
231 struct __thermal_zone *data = tz->devdata;
233 if (!data->ops || !data->ops->set_emul_temp)
236 return data->ops->set_emul_temp(data->sensor_data, temp);
239 static int of_thermal_get_trend(struct thermal_zone_device *tz, int trip,
240 enum thermal_trend *trend)
242 struct __thermal_zone *data = tz->devdata;
246 if (!data->ops->get_trend)
249 r = data->ops->get_trend(data->sensor_data, &dev_trend);
253 /* TODO: These intervals might have some thresholds, but in core code */
255 *trend = THERMAL_TREND_RAISING;
256 else if (dev_trend < 0)
257 *trend = THERMAL_TREND_DROPPING;
259 *trend = THERMAL_TREND_STABLE;
264 static int of_thermal_bind(struct thermal_zone_device *thermal,
265 struct thermal_cooling_device *cdev)
267 struct __thermal_zone *data = thermal->devdata;
270 if (!data || IS_ERR(data))
273 /* find where to bind */
274 for (i = 0; i < data->num_tbps; i++) {
275 struct __thermal_bind_params *tbp = data->tbps + i;
277 if (tbp->cooling_device == cdev->np) {
280 ret = thermal_zone_bind_cooling_device(thermal,
293 static int of_thermal_unbind(struct thermal_zone_device *thermal,
294 struct thermal_cooling_device *cdev)
296 struct __thermal_zone *data = thermal->devdata;
299 if (!data || IS_ERR(data))
302 /* find where to unbind */
303 for (i = 0; i < data->num_tbps; i++) {
304 struct __thermal_bind_params *tbp = data->tbps + i;
306 if (tbp->cooling_device == cdev->np) {
309 ret = thermal_zone_unbind_cooling_device(thermal,
319 static int of_thermal_get_mode(struct thermal_zone_device *tz,
320 enum thermal_device_mode *mode)
322 struct __thermal_zone *data = tz->devdata;
329 static int of_thermal_set_mode(struct thermal_zone_device *tz,
330 enum thermal_device_mode mode)
332 struct __thermal_zone *data = tz->devdata;
334 mutex_lock(&tz->lock);
336 if (mode == THERMAL_DEVICE_ENABLED)
337 tz->polling_delay = data->polling_delay;
339 tz->polling_delay = 0;
341 mutex_unlock(&tz->lock);
344 thermal_zone_device_update(tz);
349 static int of_thermal_update_trips(struct thermal_zone_device *tz)
354 err = of_thermal_get_temp(tz, &temp);
358 err = of_thermal_set_trips(tz, temp);
365 static int of_thermal_get_trip_type(struct thermal_zone_device *tz, int trip,
366 enum thermal_trip_type *type)
368 struct __thermal_zone *data = tz->devdata;
370 if (trip >= data->ntrips || trip < 0)
373 *type = data->trips[trip].type;
378 static int of_thermal_get_trip_temp(struct thermal_zone_device *tz, int trip,
381 struct __thermal_zone *data = tz->devdata;
383 if (trip >= data->ntrips || trip < 0)
386 *temp = data->trips[trip].temperature;
391 static int of_thermal_set_trip_temp(struct thermal_zone_device *tz, int trip,
394 struct __thermal_zone *data = tz->devdata;
397 if (trip >= data->ntrips || trip < 0)
400 if (data->ops->set_trip_temp) {
403 ret = data->ops->set_trip_temp(data->sensor_data, trip, temp);
408 /* thermal framework should take care of data->mask & (1 << trip) */
409 data->trips[trip].temperature = temp;
411 err = of_thermal_update_trips(tz);
418 static int of_thermal_get_trip_hyst(struct thermal_zone_device *tz, int trip,
421 struct __thermal_zone *data = tz->devdata;
423 if (trip >= data->ntrips || trip < 0)
426 *hyst = data->trips[trip].hysteresis;
431 static int of_thermal_set_trip_hyst(struct thermal_zone_device *tz, int trip,
434 struct __thermal_zone *data = tz->devdata;
437 if (trip >= data->ntrips || trip < 0)
440 /* thermal framework should take care of data->mask & (1 << trip) */
441 data->trips[trip].hysteresis = hyst;
443 err = of_thermal_update_trips(tz);
450 static int of_thermal_get_crit_temp(struct thermal_zone_device *tz,
453 struct __thermal_zone *data = tz->devdata;
456 for (i = 0; i < data->ntrips; i++)
457 if (data->trips[i].type == THERMAL_TRIP_CRITICAL) {
458 *temp = data->trips[i].temperature;
465 static struct thermal_zone_device_ops of_thermal_ops = {
466 .get_mode = of_thermal_get_mode,
467 .set_mode = of_thermal_set_mode,
469 .get_trip_type = of_thermal_get_trip_type,
470 .get_trip_temp = of_thermal_get_trip_temp,
471 .set_trip_temp = of_thermal_set_trip_temp,
472 .get_trip_hyst = of_thermal_get_trip_hyst,
473 .set_trip_hyst = of_thermal_set_trip_hyst,
474 .get_crit_temp = of_thermal_get_crit_temp,
476 .bind = of_thermal_bind,
477 .unbind = of_thermal_unbind,
482 static struct thermal_zone_device *
483 thermal_zone_of_add_sensor(struct device_node *zone,
484 struct device_node *sensor, void *data,
485 const struct thermal_zone_of_device_ops *ops)
487 struct thermal_zone_device *tzd;
488 struct __thermal_zone *tz;
490 tzd = thermal_zone_get_zone_by_name(zone->name);
492 return ERR_PTR(-EPROBE_DEFER);
497 return ERR_PTR(-EINVAL);
499 mutex_lock(&tzd->lock);
501 tz->sensor_data = data;
503 of_thermal_update_trips(tzd);
505 tzd->ops->get_temp = of_thermal_get_temp;
506 tzd->ops->get_trend = of_thermal_get_trend;
507 tzd->ops->set_emul_temp = of_thermal_set_emul_temp;
508 mutex_unlock(&tzd->lock);
514 * thermal_zone_of_sensor_register - registers a sensor to a DT thermal zone
515 * @dev: a valid struct device pointer of a sensor device. Must contain
516 * a valid .of_node, for the sensor node.
517 * @sensor_id: a sensor identifier, in case the sensor IP has more
519 * @data: a private pointer (owned by the caller) that will be passed
520 * back, when a temperature reading is needed.
521 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
523 * This function will search the list of thermal zones described in device
524 * tree and look for the zone that refer to the sensor device pointed by
525 * @dev->of_node as temperature providers. For the zone pointing to the
526 * sensor node, the sensor will be added to the DT thermal zone device.
528 * The thermal zone temperature is provided by the @get_temp function
529 * pointer. When called, it will have the private pointer @data back.
531 * The thermal zone temperature trend is provided by the @get_trend function
532 * pointer. When called, it will have the private pointer @data back.
535 * 01 - This function must enqueue the new sensor instead of using
536 * it as the only source of temperature values.
538 * 02 - There must be a way to match the sensor with all thermal zones
541 * Return: On success returns a valid struct thermal_zone_device,
542 * otherwise, it returns a corresponding ERR_PTR(). Caller must
543 * check the return value with help of IS_ERR() helper.
545 struct thermal_zone_device *
546 thermal_zone_of_sensor_register(struct device *dev, int sensor_id, void *data,
547 const struct thermal_zone_of_device_ops *ops)
549 struct device_node *np, *child, *sensor_np;
550 struct thermal_zone_device *tzd = ERR_PTR(-ENODEV);
552 np = of_find_node_by_name(NULL, "thermal-zones");
554 return ERR_PTR(-ENODEV);
556 if (!dev || !dev->of_node) {
558 return ERR_PTR(-EINVAL);
561 sensor_np = of_node_get(dev->of_node);
563 for_each_child_of_node(np, child) {
564 struct of_phandle_args sensor_specs;
567 /* Check whether child is enabled or not */
568 if (!of_device_is_available(child))
571 /* For now, thermal framework supports only 1 sensor per zone */
572 ret = of_parse_phandle_with_args(child, "thermal-sensors",
573 "#thermal-sensor-cells",
578 if (sensor_specs.args_count >= 1) {
579 id = sensor_specs.args[0];
580 WARN(sensor_specs.args_count > 1,
581 "%s: too many cells in sensor specifier %d\n",
582 sensor_specs.np->name, sensor_specs.args_count);
587 if (sensor_specs.np == sensor_np && id == sensor_id) {
588 tzd = thermal_zone_of_add_sensor(child, sensor_np,
591 tzd->ops->set_mode(tzd, THERMAL_DEVICE_ENABLED);
593 of_node_put(sensor_specs.np);
597 of_node_put(sensor_specs.np);
600 of_node_put(sensor_np);
605 EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_register);
608 * thermal_zone_of_sensor_unregister - unregisters a sensor from a DT thermal zone
609 * @dev: a valid struct device pointer of a sensor device. Must contain
610 * a valid .of_node, for the sensor node.
611 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
613 * This function removes the sensor callbacks and private data from the
614 * thermal zone device registered with thermal_zone_of_sensor_register()
615 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
616 * thermal zone device callbacks.
618 * TODO: When the support to several sensors per zone is added, this
619 * function must search the sensor list based on @dev parameter.
622 void thermal_zone_of_sensor_unregister(struct device *dev,
623 struct thermal_zone_device *tzd)
625 struct __thermal_zone *tz;
627 if (!dev || !tzd || !tzd->devdata)
632 /* no __thermal_zone, nothing to be done */
636 mutex_lock(&tzd->lock);
637 tzd->ops->get_temp = NULL;
638 tzd->ops->get_trend = NULL;
639 tzd->ops->set_emul_temp = NULL;
642 tz->sensor_data = NULL;
643 mutex_unlock(&tzd->lock);
645 EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister);
647 static void devm_thermal_zone_of_sensor_release(struct device *dev, void *res)
649 thermal_zone_of_sensor_unregister(dev,
650 *(struct thermal_zone_device **)res);
653 static int devm_thermal_zone_of_sensor_match(struct device *dev, void *res,
656 struct thermal_zone_device **r = res;
658 if (WARN_ON(!r || !*r))
665 * devm_thermal_zone_of_sensor_register - Resource managed version of
666 * thermal_zone_of_sensor_register()
667 * @dev: a valid struct device pointer of a sensor device. Must contain
668 * a valid .of_node, for the sensor node.
669 * @sensor_id: a sensor identifier, in case the sensor IP has more
671 * @data: a private pointer (owned by the caller) that will be passed
672 * back, when a temperature reading is needed.
673 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
675 * Refer thermal_zone_of_sensor_register() for more details.
677 * Return: On success returns a valid struct thermal_zone_device,
678 * otherwise, it returns a corresponding ERR_PTR(). Caller must
679 * check the return value with help of IS_ERR() helper.
680 * Registered hermal_zone_device device will automatically be
681 * released when device is unbounded.
683 struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
684 struct device *dev, int sensor_id,
685 void *data, const struct thermal_zone_of_device_ops *ops)
687 struct thermal_zone_device **ptr, *tzd;
689 ptr = devres_alloc(devm_thermal_zone_of_sensor_release, sizeof(*ptr),
692 return ERR_PTR(-ENOMEM);
694 tzd = thermal_zone_of_sensor_register(dev, sensor_id, data, ops);
701 devres_add(dev, ptr);
705 EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_register);
708 * devm_thermal_zone_of_sensor_unregister - Resource managed version of
709 * thermal_zone_of_sensor_unregister().
710 * @dev: Device for which which resource was allocated.
711 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
713 * This function removes the sensor callbacks and private data from the
714 * thermal zone device registered with devm_thermal_zone_of_sensor_register()
715 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
716 * thermal zone device callbacks.
717 * Normally this function will not need to be called and the resource
718 * management code will ensure that the resource is freed.
720 void devm_thermal_zone_of_sensor_unregister(struct device *dev,
721 struct thermal_zone_device *tzd)
723 WARN_ON(devres_release(dev, devm_thermal_zone_of_sensor_release,
724 devm_thermal_zone_of_sensor_match, tzd));
726 EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_unregister);
728 /*** functions parsing device tree nodes ***/
731 * thermal_of_populate_bind_params - parse and fill cooling map data
732 * @np: DT node containing a cooling-map node
733 * @__tbp: data structure to be filled with cooling map info
734 * @trips: array of thermal zone trip points
735 * @ntrips: number of trip points inside trips.
737 * This function parses a cooling-map type of node represented by
738 * @np parameter and fills the read data into @__tbp data structure.
739 * It needs the already parsed array of trip points of the thermal zone
742 * Return: 0 on success, proper error code otherwise
744 static int thermal_of_populate_bind_params(struct device_node *np,
745 struct __thermal_bind_params *__tbp,
746 struct thermal_trip *trips,
749 struct of_phandle_args cooling_spec;
750 struct device_node *trip;
754 /* Default weight. Usage is optional */
755 __tbp->usage = THERMAL_WEIGHT_DEFAULT;
756 ret = of_property_read_u32(np, "contribution", &prop);
760 trip = of_parse_phandle(np, "trip", 0);
762 pr_err("missing trip property\n");
766 /* match using device_node */
767 for (i = 0; i < ntrips; i++)
768 if (trip == trips[i].np) {
778 ret = of_parse_phandle_with_args(np, "cooling-device", "#cooling-cells",
781 pr_err("missing cooling_device property\n");
784 __tbp->cooling_device = cooling_spec.np;
785 if (cooling_spec.args_count >= 2) { /* at least min and max */
786 __tbp->min = cooling_spec.args[0];
787 __tbp->max = cooling_spec.args[1];
789 pr_err("wrong reference to cooling device, missing limits\n");
799 * It maps 'enum thermal_trip_type' found in include/linux/thermal.h
800 * into the device tree binding of 'trip', property type.
802 static const char * const trip_types[] = {
803 [THERMAL_TRIP_ACTIVE] = "active",
804 [THERMAL_TRIP_PASSIVE] = "passive",
805 [THERMAL_TRIP_HOT] = "hot",
806 [THERMAL_TRIP_CRITICAL] = "critical",
810 * thermal_of_get_trip_type - Get phy mode for given device_node
811 * @np: Pointer to the given device_node
812 * @type: Pointer to resulting trip type
814 * The function gets trip type string from property 'type',
815 * and store its index in trip_types table in @type,
817 * Return: 0 on success, or errno in error case.
819 static int thermal_of_get_trip_type(struct device_node *np,
820 enum thermal_trip_type *type)
825 err = of_property_read_string(np, "type", &t);
829 for (i = 0; i < ARRAY_SIZE(trip_types); i++)
830 if (!strcasecmp(t, trip_types[i])) {
839 * thermal_of_populate_trip - parse and fill one trip point data
840 * @np: DT node containing a trip point node
841 * @trip: trip point data structure to be filled up
843 * This function parses a trip point type of node represented by
844 * @np parameter and fills the read data into @trip data structure.
846 * Return: 0 on success, proper error code otherwise
848 static int thermal_of_populate_trip(struct device_node *np,
849 struct thermal_trip *trip)
854 ret = of_property_read_u32(np, "temperature", &prop);
856 pr_err("missing temperature property\n");
859 trip->temperature = prop;
861 ret = of_property_read_u32(np, "hysteresis", &prop);
863 pr_err("missing hysteresis property\n");
866 trip->hysteresis = prop;
868 ret = thermal_of_get_trip_type(np, &trip->type);
870 pr_err("wrong trip type property\n");
874 /* Required for cooling map matching */
882 * thermal_of_build_thermal_zone - parse and fill one thermal zone data
883 * @np: DT node containing a thermal zone node
885 * This function parses a thermal zone type of node represented by
886 * @np parameter and fills the read data into a __thermal_zone data structure
887 * and return this pointer.
889 * TODO: Missing properties to parse: thermal-sensor-names
891 * Return: On success returns a valid struct __thermal_zone,
892 * otherwise, it returns a corresponding ERR_PTR(). Caller must
893 * check the return value with help of IS_ERR() helper.
895 static struct __thermal_zone *
896 thermal_of_build_thermal_zone(struct device_node *np)
898 struct device_node *child = NULL, *gchild;
899 struct __thermal_zone *tz;
904 pr_err("no thermal zone np\n");
905 return ERR_PTR(-EINVAL);
908 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
910 return ERR_PTR(-ENOMEM);
912 ret = of_property_read_u32(np, "polling-delay-passive", &prop);
914 pr_err("missing polling-delay-passive property\n");
917 tz->passive_delay = prop;
919 ret = of_property_read_u32(np, "polling-delay", &prop);
921 pr_err("missing polling-delay property\n");
924 tz->polling_delay = prop;
927 * REVIST: for now, the thermal framework supports only
928 * one sensor per thermal zone. Thus, we are considering
929 * only the first two values as slope and offset.
931 ret = of_property_read_u32_array(np, "coefficients", coef, 2);
934 tz->offset = coef[1];
941 child = of_get_child_by_name(np, "trips");
943 tz->prev_high_trip = INT_MIN;
944 tz->prev_low_trip = INT_MAX;
946 /* No trips provided */
950 tz->ntrips = of_get_child_count(child);
951 if (tz->ntrips == 0) /* must have at least one child */
954 tz->trips = kzalloc(tz->ntrips * sizeof(*tz->trips), GFP_KERNEL);
961 for_each_child_of_node(child, gchild) {
962 ret = thermal_of_populate_trip(gchild, &tz->trips[i++]);
970 child = of_get_child_by_name(np, "cooling-maps");
972 /* cooling-maps not provided */
976 tz->num_tbps = of_get_child_count(child);
977 if (tz->num_tbps == 0)
980 tz->tbps = kzalloc(tz->num_tbps * sizeof(*tz->tbps), GFP_KERNEL);
987 for_each_child_of_node(child, gchild) {
988 ret = thermal_of_populate_bind_params(gchild, &tz->tbps[i++],
989 tz->trips, tz->ntrips);
996 tz->mode = THERMAL_DEVICE_DISABLED;
1001 for (i = 0; i < tz->num_tbps; i++)
1002 of_node_put(tz->tbps[i].cooling_device);
1005 for (i = 0; i < tz->ntrips; i++)
1006 of_node_put(tz->trips[i].np);
1008 of_node_put(gchild);
1013 return ERR_PTR(ret);
1016 static inline void of_thermal_free_zone(struct __thermal_zone *tz)
1020 for (i = 0; i < tz->num_tbps; i++)
1021 of_node_put(tz->tbps[i].cooling_device);
1023 for (i = 0; i < tz->ntrips; i++)
1024 of_node_put(tz->trips[i].np);
1030 * of_parse_thermal_zones - parse device tree thermal data
1032 * Initialization function that can be called by machine initialization
1033 * code to parse thermal data and populate the thermal framework
1034 * with hardware thermal zones info. This function only parses thermal zones.
1035 * Cooling devices and sensor devices nodes are supposed to be parsed
1036 * by their respective drivers.
1038 * Return: 0 on success, proper error code otherwise
1041 int __init of_parse_thermal_zones(void)
1043 struct device_node *np, *child;
1044 struct __thermal_zone *tz;
1045 struct thermal_zone_device_ops *ops;
1047 np = of_find_node_by_name(NULL, "thermal-zones");
1049 pr_debug("unable to find thermal zones\n");
1050 return 0; /* Run successfully on systems without thermal DT */
1053 for_each_child_of_node(np, child) {
1054 struct thermal_zone_device *zone;
1055 struct thermal_zone_params *tzp;
1059 /* Check whether child is enabled or not */
1060 if (!of_device_is_available(child))
1063 tz = thermal_of_build_thermal_zone(child);
1065 pr_err("failed to build thermal zone %s: %ld\n",
1071 ops = kmemdup(&of_thermal_ops, sizeof(*ops), GFP_KERNEL);
1075 tzp = kzalloc(sizeof(*tzp), GFP_KERNEL);
1081 /* No hwmon because there might be hwmon drivers registering */
1082 tzp->no_hwmon = true;
1084 if (!of_property_read_u32(child, "sustainable-power", &prop))
1085 tzp->sustainable_power = prop;
1087 for (i = 0; i < tz->ntrips; i++)
1090 /* these two are left for temperature drivers to use */
1091 tzp->slope = tz->slope;
1092 tzp->offset = tz->offset;
1094 zone = thermal_zone_device_register(child->name, tz->ntrips,
1100 pr_err("Failed to build %s zone %ld\n", child->name,
1104 of_thermal_free_zone(tz);
1105 /* attempting to build remaining zones still */
1115 of_thermal_free_zone(tz);
1117 /* no memory available, so free what we have built */
1118 of_thermal_destroy_zones();
1124 * of_thermal_destroy_zones - remove all zones parsed and allocated resources
1126 * Finds all zones parsed and added to the thermal framework and remove them
1127 * from the system, together with their resources.
1130 void of_thermal_destroy_zones(void)
1132 struct device_node *np, *child;
1134 np = of_find_node_by_name(NULL, "thermal-zones");
1136 pr_debug("unable to find thermal zones\n");
1140 for_each_child_of_node(np, child) {
1141 struct thermal_zone_device *zone;
1143 /* Check whether child is enabled or not */
1144 if (!of_device_is_available(child))
1147 zone = thermal_zone_get_zone_by_name(child->name);
1151 thermal_zone_device_unregister(zone);
1154 of_thermal_free_zone(zone->devdata);