1 // SPDX-License-Identifier: GPL-2.0
3 * of-thermal.c - Generic Thermal Management device tree support.
5 * Copyright (C) 2013 Texas Instruments
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_param - a match between trip and cooling device
39 * @tcbp: a pointer to an array of cooling devices
40 * @count: number of elements in array
41 * @trip_id: the trip point index
42 * @usage: the percentage (from 0 to 100) of cooling contribution
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_bind(struct thermal_zone_device *thermal,
207 struct thermal_cooling_device *cdev)
209 struct __thermal_zone *data = thermal->devdata;
210 struct __thermal_bind_params *tbp;
211 struct __thermal_cooling_bind_param *tcbp;
214 if (!data || IS_ERR(data))
217 /* find where to bind */
218 for (i = 0; i < data->num_tbps; i++) {
219 tbp = data->tbps + i;
221 for (j = 0; j < tbp->count; j++) {
222 tcbp = tbp->tcbp + j;
224 if (tcbp->cooling_device == cdev->np) {
227 ret = thermal_zone_bind_cooling_device(thermal,
241 static int of_thermal_unbind(struct thermal_zone_device *thermal,
242 struct thermal_cooling_device *cdev)
244 struct __thermal_zone *data = thermal->devdata;
245 struct __thermal_bind_params *tbp;
246 struct __thermal_cooling_bind_param *tcbp;
249 if (!data || IS_ERR(data))
252 /* find where to unbind */
253 for (i = 0; i < data->num_tbps; i++) {
254 tbp = data->tbps + i;
256 for (j = 0; j < tbp->count; j++) {
257 tcbp = tbp->tcbp + j;
259 if (tcbp->cooling_device == cdev->np) {
262 ret = thermal_zone_unbind_cooling_device(thermal,
273 static int of_thermal_get_trip_type(struct thermal_zone_device *tz, int trip,
274 enum thermal_trip_type *type)
276 struct __thermal_zone *data = tz->devdata;
278 if (trip >= data->ntrips || trip < 0)
281 *type = data->trips[trip].type;
286 static int of_thermal_get_trip_temp(struct thermal_zone_device *tz, int trip,
289 struct __thermal_zone *data = tz->devdata;
291 if (trip >= data->ntrips || trip < 0)
294 *temp = data->trips[trip].temperature;
299 static int of_thermal_set_trip_temp(struct thermal_zone_device *tz, int trip,
302 struct __thermal_zone *data = tz->devdata;
304 if (trip >= data->ntrips || trip < 0)
307 if (data->ops && data->ops->set_trip_temp) {
310 ret = data->ops->set_trip_temp(data->sensor_data, trip, temp);
315 /* thermal framework should take care of data->mask & (1 << trip) */
316 data->trips[trip].temperature = temp;
321 static int of_thermal_get_trip_hyst(struct thermal_zone_device *tz, int trip,
324 struct __thermal_zone *data = tz->devdata;
326 if (trip >= data->ntrips || trip < 0)
329 *hyst = data->trips[trip].hysteresis;
334 static int of_thermal_set_trip_hyst(struct thermal_zone_device *tz, int trip,
337 struct __thermal_zone *data = tz->devdata;
339 if (trip >= data->ntrips || trip < 0)
342 /* thermal framework should take care of data->mask & (1 << trip) */
343 data->trips[trip].hysteresis = hyst;
348 static int of_thermal_get_crit_temp(struct thermal_zone_device *tz,
351 struct __thermal_zone *data = tz->devdata;
354 for (i = 0; i < data->ntrips; i++)
355 if (data->trips[i].type == THERMAL_TRIP_CRITICAL) {
356 *temp = data->trips[i].temperature;
363 static struct thermal_zone_device_ops of_thermal_ops = {
364 .get_trip_type = of_thermal_get_trip_type,
365 .get_trip_temp = of_thermal_get_trip_temp,
366 .set_trip_temp = of_thermal_set_trip_temp,
367 .get_trip_hyst = of_thermal_get_trip_hyst,
368 .set_trip_hyst = of_thermal_set_trip_hyst,
369 .get_crit_temp = of_thermal_get_crit_temp,
371 .bind = of_thermal_bind,
372 .unbind = of_thermal_unbind,
377 static struct thermal_zone_device *
378 thermal_zone_of_add_sensor(struct device_node *zone,
379 struct device_node *sensor, void *data,
380 const struct thermal_zone_of_device_ops *ops)
382 struct thermal_zone_device *tzd;
383 struct __thermal_zone *tz;
385 tzd = thermal_zone_get_zone_by_name(zone->name);
387 return ERR_PTR(-EPROBE_DEFER);
392 return ERR_PTR(-EINVAL);
394 mutex_lock(&tzd->lock);
396 tz->sensor_data = data;
398 tzd->ops->get_temp = of_thermal_get_temp;
399 tzd->ops->get_trend = of_thermal_get_trend;
402 * The thermal zone core will calculate the window if they have set the
403 * optional set_trips pointer.
406 tzd->ops->set_trips = of_thermal_set_trips;
408 if (ops->set_emul_temp)
409 tzd->ops->set_emul_temp = of_thermal_set_emul_temp;
411 mutex_unlock(&tzd->lock);
417 * thermal_zone_of_get_sensor_id - get sensor ID from a DT thermal zone
418 * @tz_np: a valid thermal zone device node.
419 * @sensor_np: a sensor node of a valid sensor device.
420 * @id: the sensor ID returned if success.
422 * This function will get sensor ID from a given thermal zone node and
423 * the sensor node must match the temperature provider @sensor_np.
425 * Return: 0 on success, proper error code otherwise.
428 int thermal_zone_of_get_sensor_id(struct device_node *tz_np,
429 struct device_node *sensor_np,
432 struct of_phandle_args sensor_specs;
435 ret = of_parse_phandle_with_args(tz_np,
437 "#thermal-sensor-cells",
443 if (sensor_specs.np != sensor_np) {
444 of_node_put(sensor_specs.np);
448 if (sensor_specs.args_count > 1)
449 pr_warn("%pOFn: too many cells in sensor specifier %d\n",
450 sensor_specs.np, sensor_specs.args_count);
452 *id = sensor_specs.args_count ? sensor_specs.args[0] : 0;
454 of_node_put(sensor_specs.np);
458 EXPORT_SYMBOL_GPL(thermal_zone_of_get_sensor_id);
461 * thermal_zone_of_sensor_register - registers a sensor to a DT thermal zone
462 * @dev: a valid struct device pointer of a sensor device. Must contain
463 * a valid .of_node, for the sensor node.
464 * @sensor_id: a sensor identifier, in case the sensor IP has more
466 * @data: a private pointer (owned by the caller) that will be passed
467 * back, when a temperature reading is needed.
468 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
470 * This function will search the list of thermal zones described in device
471 * tree and look for the zone that refer to the sensor device pointed by
472 * @dev->of_node as temperature providers. For the zone pointing to the
473 * sensor node, the sensor will be added to the DT thermal zone device.
475 * The thermal zone temperature is provided by the @get_temp function
476 * pointer. When called, it will have the private pointer @data back.
478 * The thermal zone temperature trend is provided by the @get_trend function
479 * pointer. When called, it will have the private pointer @data back.
482 * 01 - This function must enqueue the new sensor instead of using
483 * it as the only source of temperature values.
485 * 02 - There must be a way to match the sensor with all thermal zones
488 * Return: On success returns a valid struct thermal_zone_device,
489 * otherwise, it returns a corresponding ERR_PTR(). Caller must
490 * check the return value with help of IS_ERR() helper.
492 struct thermal_zone_device *
493 thermal_zone_of_sensor_register(struct device *dev, int sensor_id, void *data,
494 const struct thermal_zone_of_device_ops *ops)
496 struct device_node *np, *child, *sensor_np;
497 struct thermal_zone_device *tzd = ERR_PTR(-ENODEV);
499 np = of_find_node_by_name(NULL, "thermal-zones");
501 return ERR_PTR(-ENODEV);
503 if (!dev || !dev->of_node) {
505 return ERR_PTR(-ENODEV);
508 sensor_np = of_node_get(dev->of_node);
510 for_each_available_child_of_node(np, child) {
513 /* For now, thermal framework supports only 1 sensor per zone */
514 ret = thermal_zone_of_get_sensor_id(child, sensor_np, &id);
518 if (id == sensor_id) {
519 tzd = thermal_zone_of_add_sensor(child, sensor_np,
522 thermal_zone_device_enable(tzd);
529 of_node_put(sensor_np);
534 EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_register);
537 * thermal_zone_of_sensor_unregister - unregisters a sensor from a DT thermal zone
538 * @dev: a valid struct device pointer of a sensor device. Must contain
539 * a valid .of_node, for the sensor node.
540 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
542 * This function removes the sensor callbacks and private data from the
543 * thermal zone device registered with thermal_zone_of_sensor_register()
544 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
545 * thermal zone device callbacks.
547 * TODO: When the support to several sensors per zone is added, this
548 * function must search the sensor list based on @dev parameter.
551 void thermal_zone_of_sensor_unregister(struct device *dev,
552 struct thermal_zone_device *tzd)
554 struct __thermal_zone *tz;
556 if (!dev || !tzd || !tzd->devdata)
561 /* no __thermal_zone, nothing to be done */
565 /* stop temperature polling */
566 thermal_zone_device_disable(tzd);
568 mutex_lock(&tzd->lock);
569 tzd->ops->get_temp = NULL;
570 tzd->ops->get_trend = NULL;
571 tzd->ops->set_emul_temp = NULL;
574 tz->sensor_data = NULL;
575 mutex_unlock(&tzd->lock);
577 EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister);
579 static void devm_thermal_zone_of_sensor_release(struct device *dev, void *res)
581 thermal_zone_of_sensor_unregister(dev,
582 *(struct thermal_zone_device **)res);
585 static int devm_thermal_zone_of_sensor_match(struct device *dev, void *res,
588 struct thermal_zone_device **r = res;
590 if (WARN_ON(!r || !*r))
597 * devm_thermal_zone_of_sensor_register - Resource managed version of
598 * thermal_zone_of_sensor_register()
599 * @dev: a valid struct device pointer of a sensor device. Must contain
600 * a valid .of_node, for the sensor node.
601 * @sensor_id: a sensor identifier, in case the sensor IP has more
603 * @data: a private pointer (owned by the caller) that will be passed
604 * back, when a temperature reading is needed.
605 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
607 * Refer thermal_zone_of_sensor_register() for more details.
609 * Return: On success returns a valid struct thermal_zone_device,
610 * otherwise, it returns a corresponding ERR_PTR(). Caller must
611 * check the return value with help of IS_ERR() helper.
612 * Registered thermal_zone_device device will automatically be
613 * released when device is unbounded.
615 struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
616 struct device *dev, int sensor_id,
617 void *data, const struct thermal_zone_of_device_ops *ops)
619 struct thermal_zone_device **ptr, *tzd;
621 ptr = devres_alloc(devm_thermal_zone_of_sensor_release, sizeof(*ptr),
624 return ERR_PTR(-ENOMEM);
626 tzd = thermal_zone_of_sensor_register(dev, sensor_id, data, ops);
633 devres_add(dev, ptr);
637 EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_register);
640 * devm_thermal_zone_of_sensor_unregister - Resource managed version of
641 * thermal_zone_of_sensor_unregister().
642 * @dev: Device for which which resource was allocated.
643 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
645 * This function removes the sensor callbacks and private data from the
646 * thermal zone device registered with devm_thermal_zone_of_sensor_register()
647 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
648 * thermal zone device callbacks.
649 * Normally this function will not need to be called and the resource
650 * management code will ensure that the resource is freed.
652 void devm_thermal_zone_of_sensor_unregister(struct device *dev,
653 struct thermal_zone_device *tzd)
655 WARN_ON(devres_release(dev, devm_thermal_zone_of_sensor_release,
656 devm_thermal_zone_of_sensor_match, tzd));
658 EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_unregister);
660 /*** functions parsing device tree nodes ***/
663 * thermal_of_populate_bind_params - parse and fill cooling map data
664 * @np: DT node containing a cooling-map node
665 * @__tbp: data structure to be filled with cooling map info
666 * @trips: array of thermal zone trip points
667 * @ntrips: number of trip points inside trips.
669 * This function parses a cooling-map type of node represented by
670 * @np parameter and fills the read data into @__tbp data structure.
671 * It needs the already parsed array of trip points of the thermal zone
674 * Return: 0 on success, proper error code otherwise
676 static int thermal_of_populate_bind_params(struct device_node *np,
677 struct __thermal_bind_params *__tbp,
678 struct thermal_trip *trips,
681 struct of_phandle_args cooling_spec;
682 struct __thermal_cooling_bind_param *__tcbp;
683 struct device_node *trip;
687 /* Default weight. Usage is optional */
688 __tbp->usage = THERMAL_WEIGHT_DEFAULT;
689 ret = of_property_read_u32(np, "contribution", &prop);
693 trip = of_parse_phandle(np, "trip", 0);
695 pr_err("missing trip property\n");
699 /* match using device_node */
700 for (i = 0; i < ntrips; i++)
701 if (trip == trips[i].np) {
711 count = of_count_phandle_with_args(np, "cooling-device",
714 pr_err("Add a cooling_device property with at least one device\n");
719 __tcbp = kcalloc(count, sizeof(*__tcbp), GFP_KERNEL);
725 for (i = 0; i < count; i++) {
726 ret = of_parse_phandle_with_args(np, "cooling-device",
727 "#cooling-cells", i, &cooling_spec);
729 pr_err("Invalid cooling-device entry\n");
733 __tcbp[i].cooling_device = cooling_spec.np;
735 if (cooling_spec.args_count >= 2) { /* at least min and max */
736 __tcbp[i].min = cooling_spec.args[0];
737 __tcbp[i].max = cooling_spec.args[1];
739 pr_err("wrong reference to cooling device, missing limits\n");
743 __tbp->tcbp = __tcbp;
744 __tbp->count = count;
749 for (i = i - 1; i >= 0; i--)
750 of_node_put(__tcbp[i].cooling_device);
759 * It maps 'enum thermal_trip_type' found in include/linux/thermal.h
760 * into the device tree binding of 'trip', property type.
762 static const char * const trip_types[] = {
763 [THERMAL_TRIP_ACTIVE] = "active",
764 [THERMAL_TRIP_PASSIVE] = "passive",
765 [THERMAL_TRIP_HOT] = "hot",
766 [THERMAL_TRIP_CRITICAL] = "critical",
770 * thermal_of_get_trip_type - Get phy mode for given device_node
771 * @np: Pointer to the given device_node
772 * @type: Pointer to resulting trip type
774 * The function gets trip type string from property 'type',
775 * and store its index in trip_types table in @type,
777 * Return: 0 on success, or errno in error case.
779 static int thermal_of_get_trip_type(struct device_node *np,
780 enum thermal_trip_type *type)
785 err = of_property_read_string(np, "type", &t);
789 for (i = 0; i < ARRAY_SIZE(trip_types); i++)
790 if (!strcasecmp(t, trip_types[i])) {
799 * thermal_of_populate_trip - parse and fill one trip point data
800 * @np: DT node containing a trip point node
801 * @trip: trip point data structure to be filled up
803 * This function parses a trip point type of node represented by
804 * @np parameter and fills the read data into @trip data structure.
806 * Return: 0 on success, proper error code otherwise
808 static int thermal_of_populate_trip(struct device_node *np,
809 struct thermal_trip *trip)
814 ret = of_property_read_u32(np, "temperature", &prop);
816 pr_err("missing temperature property\n");
819 trip->temperature = prop;
821 ret = of_property_read_u32(np, "hysteresis", &prop);
823 pr_err("missing hysteresis property\n");
826 trip->hysteresis = prop;
828 ret = thermal_of_get_trip_type(np, &trip->type);
830 pr_err("wrong trip type property\n");
834 /* Required for cooling map matching */
842 * thermal_of_build_thermal_zone - parse and fill one thermal zone data
843 * @np: DT node containing a thermal zone node
845 * This function parses a thermal zone type of node represented by
846 * @np parameter and fills the read data into a __thermal_zone data structure
847 * and return this pointer.
849 * TODO: Missing properties to parse: thermal-sensor-names
851 * Return: On success returns a valid struct __thermal_zone,
852 * otherwise, it returns a corresponding ERR_PTR(). Caller must
853 * check the return value with help of IS_ERR() helper.
855 static struct __thermal_zone
856 __init *thermal_of_build_thermal_zone(struct device_node *np)
858 struct device_node *child = NULL, *gchild;
859 struct __thermal_zone *tz;
864 pr_err("no thermal zone np\n");
865 return ERR_PTR(-EINVAL);
868 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
870 return ERR_PTR(-ENOMEM);
872 ret = of_property_read_u32(np, "polling-delay-passive", &prop);
874 pr_err("%pOFn: missing polling-delay-passive property\n", np);
877 tz->passive_delay = prop;
879 ret = of_property_read_u32(np, "polling-delay", &prop);
881 pr_err("%pOFn: missing polling-delay property\n", np);
884 tz->polling_delay = prop;
887 * REVIST: for now, the thermal framework supports only
888 * one sensor per thermal zone. Thus, we are considering
889 * only the first two values as slope and offset.
891 ret = of_property_read_u32_array(np, "coefficients", coef, 2);
894 tz->offset = coef[1];
901 child = of_get_child_by_name(np, "trips");
903 /* No trips provided */
907 tz->ntrips = of_get_child_count(child);
908 if (tz->ntrips == 0) /* must have at least one child */
911 tz->trips = kcalloc(tz->ntrips, sizeof(*tz->trips), GFP_KERNEL);
918 for_each_child_of_node(child, gchild) {
919 ret = thermal_of_populate_trip(gchild, &tz->trips[i++]);
927 child = of_get_child_by_name(np, "cooling-maps");
929 /* cooling-maps not provided */
933 tz->num_tbps = of_get_child_count(child);
934 if (tz->num_tbps == 0)
937 tz->tbps = kcalloc(tz->num_tbps, sizeof(*tz->tbps), GFP_KERNEL);
944 for_each_child_of_node(child, gchild) {
945 ret = thermal_of_populate_bind_params(gchild, &tz->tbps[i++],
946 tz->trips, tz->ntrips);
957 for (i = i - 1; i >= 0; i--) {
958 struct __thermal_bind_params *tbp = tz->tbps + i;
961 for (j = 0; j < tbp->count; j++)
962 of_node_put(tbp->tcbp[j].cooling_device);
969 for (i = 0; i < tz->ntrips; i++)
970 of_node_put(tz->trips[i].np);
980 static __init void of_thermal_free_zone(struct __thermal_zone *tz)
982 struct __thermal_bind_params *tbp;
985 for (i = 0; i < tz->num_tbps; i++) {
988 for (j = 0; j < tbp->count; j++)
989 of_node_put(tbp->tcbp[j].cooling_device);
995 for (i = 0; i < tz->ntrips; i++)
996 of_node_put(tz->trips[i].np);
1002 * of_thermal_destroy_zones - remove all zones parsed and allocated resources
1004 * Finds all zones parsed and added to the thermal framework and remove them
1005 * from the system, together with their resources.
1008 static __init void of_thermal_destroy_zones(void)
1010 struct device_node *np, *child;
1012 np = of_find_node_by_name(NULL, "thermal-zones");
1014 pr_debug("unable to find thermal zones\n");
1018 for_each_available_child_of_node(np, child) {
1019 struct thermal_zone_device *zone;
1021 zone = thermal_zone_get_zone_by_name(child->name);
1025 thermal_zone_device_unregister(zone);
1028 of_thermal_free_zone(zone->devdata);
1034 * of_parse_thermal_zones - parse device tree thermal data
1036 * Initialization function that can be called by machine initialization
1037 * code to parse thermal data and populate the thermal framework
1038 * with hardware thermal zones info. This function only parses thermal zones.
1039 * Cooling devices and sensor devices nodes are supposed to be parsed
1040 * by their respective drivers.
1042 * Return: 0 on success, proper error code otherwise
1045 int __init of_parse_thermal_zones(void)
1047 struct device_node *np, *child;
1048 struct __thermal_zone *tz;
1049 struct thermal_zone_device_ops *ops;
1051 np = of_find_node_by_name(NULL, "thermal-zones");
1053 pr_debug("unable to find thermal zones\n");
1054 return 0; /* Run successfully on systems without thermal DT */
1057 for_each_available_child_of_node(np, child) {
1058 struct thermal_zone_device *zone;
1059 struct thermal_zone_params *tzp;
1063 tz = thermal_of_build_thermal_zone(child);
1065 pr_err("failed to build thermal zone %pOFn: %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 %pOFn zone %ld\n", child,
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();