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/thermal.h>
12 #include <linux/slab.h>
13 #include <linux/types.h>
14 #include <linux/of_device.h>
15 #include <linux/of_platform.h>
16 #include <linux/err.h>
17 #include <linux/export.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 * @mode: current thermal zone device mode (enabled/disabled)
55 * @passive_delay: polling interval while passive cooling is activated
56 * @polling_delay: zone polling interval
57 * @slope: slope of the temperature adjustment curve
58 * @offset: offset of the temperature adjustment curve
59 * @ntrips: number of trip points
60 * @trips: an array of trip points (0..ntrips - 1)
61 * @num_tbps: number of thermal bind params
62 * @tbps: an array of thermal bind params (0..num_tbps - 1)
63 * @sensor_data: sensor private data used while reading temperature and trend
64 * @ops: set of callbacks to handle the thermal zone based on DT
67 struct __thermal_zone {
68 enum thermal_device_mode mode;
76 struct thermal_trip *trips;
78 /* cooling binding data */
80 struct __thermal_bind_params *tbps;
82 /* sensor interface */
84 const struct thermal_zone_of_device_ops *ops;
87 /*** DT thermal zone device callbacks ***/
89 static int of_thermal_get_temp(struct thermal_zone_device *tz,
92 struct __thermal_zone *data = tz->devdata;
94 if (!data->ops || !data->ops->get_temp)
97 return data->ops->get_temp(data->sensor_data, temp);
100 static int of_thermal_set_trips(struct thermal_zone_device *tz,
103 struct __thermal_zone *data = tz->devdata;
105 if (!data->ops || !data->ops->set_trips)
108 return data->ops->set_trips(data->sensor_data, low, high);
112 * of_thermal_get_ntrips - function to export number of available trip
114 * @tz: pointer to a thermal zone
116 * This function is a globally visible wrapper to get number of trip points
117 * stored in the local struct __thermal_zone
119 * Return: number of available trip points, -ENODEV when data not available
121 int of_thermal_get_ntrips(struct thermal_zone_device *tz)
123 struct __thermal_zone *data = tz->devdata;
125 if (!data || IS_ERR(data))
130 EXPORT_SYMBOL_GPL(of_thermal_get_ntrips);
133 * of_thermal_is_trip_valid - function to check if trip point is valid
135 * @tz: pointer to a thermal zone
136 * @trip: trip point to evaluate
138 * This function is responsible for checking if passed trip point is valid
140 * Return: true if trip point is valid, false otherwise
142 bool of_thermal_is_trip_valid(struct thermal_zone_device *tz, int trip)
144 struct __thermal_zone *data = tz->devdata;
146 if (!data || trip >= data->ntrips || trip < 0)
151 EXPORT_SYMBOL_GPL(of_thermal_is_trip_valid);
154 * of_thermal_get_trip_points - function to get access to a globally exported
157 * @tz: pointer to a thermal zone
159 * This function provides a pointer to trip points table
161 * Return: pointer to trip points table, NULL otherwise
163 const struct thermal_trip *
164 of_thermal_get_trip_points(struct thermal_zone_device *tz)
166 struct __thermal_zone *data = tz->devdata;
173 EXPORT_SYMBOL_GPL(of_thermal_get_trip_points);
176 * of_thermal_set_emul_temp - function to set emulated temperature
178 * @tz: pointer to a thermal zone
179 * @temp: temperature to set
181 * This function gives the ability to set emulated value of temperature,
182 * which is handy for debugging
184 * Return: zero on success, error code otherwise
186 static int of_thermal_set_emul_temp(struct thermal_zone_device *tz,
189 struct __thermal_zone *data = tz->devdata;
191 if (!data->ops || !data->ops->set_emul_temp)
194 return data->ops->set_emul_temp(data->sensor_data, temp);
197 static int of_thermal_get_trend(struct thermal_zone_device *tz, int trip,
198 enum thermal_trend *trend)
200 struct __thermal_zone *data = tz->devdata;
202 if (!data->ops || !data->ops->get_trend)
205 return data->ops->get_trend(data->sensor_data, trip, trend);
208 static int of_thermal_bind(struct thermal_zone_device *thermal,
209 struct thermal_cooling_device *cdev)
211 struct __thermal_zone *data = thermal->devdata;
212 struct __thermal_bind_params *tbp;
213 struct __thermal_cooling_bind_param *tcbp;
216 if (!data || IS_ERR(data))
219 /* find where to bind */
220 for (i = 0; i < data->num_tbps; i++) {
221 tbp = data->tbps + i;
223 for (j = 0; j < tbp->count; j++) {
224 tcbp = tbp->tcbp + j;
226 if (tcbp->cooling_device == cdev->np) {
229 ret = thermal_zone_bind_cooling_device(thermal,
243 static int of_thermal_unbind(struct thermal_zone_device *thermal,
244 struct thermal_cooling_device *cdev)
246 struct __thermal_zone *data = thermal->devdata;
247 struct __thermal_bind_params *tbp;
248 struct __thermal_cooling_bind_param *tcbp;
251 if (!data || IS_ERR(data))
254 /* find where to unbind */
255 for (i = 0; i < data->num_tbps; i++) {
256 tbp = data->tbps + i;
258 for (j = 0; j < tbp->count; j++) {
259 tcbp = tbp->tcbp + j;
261 if (tcbp->cooling_device == cdev->np) {
264 ret = thermal_zone_unbind_cooling_device(thermal,
275 static int of_thermal_get_mode(struct thermal_zone_device *tz,
276 enum thermal_device_mode *mode)
278 struct __thermal_zone *data = tz->devdata;
285 static int of_thermal_set_mode(struct thermal_zone_device *tz,
286 enum thermal_device_mode mode)
288 struct __thermal_zone *data = tz->devdata;
290 mutex_lock(&tz->lock);
292 if (mode == THERMAL_DEVICE_ENABLED) {
293 tz->polling_delay = data->polling_delay;
294 tz->passive_delay = data->passive_delay;
296 tz->polling_delay = 0;
297 tz->passive_delay = 0;
300 mutex_unlock(&tz->lock);
303 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
308 static int of_thermal_get_trip_type(struct thermal_zone_device *tz, int trip,
309 enum thermal_trip_type *type)
311 struct __thermal_zone *data = tz->devdata;
313 if (trip >= data->ntrips || trip < 0)
316 *type = data->trips[trip].type;
321 static int of_thermal_get_trip_temp(struct thermal_zone_device *tz, int trip,
324 struct __thermal_zone *data = tz->devdata;
326 if (trip >= data->ntrips || trip < 0)
329 *temp = data->trips[trip].temperature;
334 static int of_thermal_set_trip_temp(struct thermal_zone_device *tz, int trip,
337 struct __thermal_zone *data = tz->devdata;
339 if (trip >= data->ntrips || trip < 0)
342 if (data->ops && data->ops->set_trip_temp) {
345 ret = data->ops->set_trip_temp(data->sensor_data, trip, temp);
350 /* thermal framework should take care of data->mask & (1 << trip) */
351 data->trips[trip].temperature = temp;
356 static int of_thermal_get_trip_hyst(struct thermal_zone_device *tz, int trip,
359 struct __thermal_zone *data = tz->devdata;
361 if (trip >= data->ntrips || trip < 0)
364 *hyst = data->trips[trip].hysteresis;
369 static int of_thermal_set_trip_hyst(struct thermal_zone_device *tz, int trip,
372 struct __thermal_zone *data = tz->devdata;
374 if (trip >= data->ntrips || trip < 0)
377 /* thermal framework should take care of data->mask & (1 << trip) */
378 data->trips[trip].hysteresis = hyst;
383 static int of_thermal_get_crit_temp(struct thermal_zone_device *tz,
386 struct __thermal_zone *data = tz->devdata;
389 for (i = 0; i < data->ntrips; i++)
390 if (data->trips[i].type == THERMAL_TRIP_CRITICAL) {
391 *temp = data->trips[i].temperature;
398 static struct thermal_zone_device_ops of_thermal_ops = {
399 .get_mode = of_thermal_get_mode,
400 .set_mode = of_thermal_set_mode,
402 .get_trip_type = of_thermal_get_trip_type,
403 .get_trip_temp = of_thermal_get_trip_temp,
404 .set_trip_temp = of_thermal_set_trip_temp,
405 .get_trip_hyst = of_thermal_get_trip_hyst,
406 .set_trip_hyst = of_thermal_set_trip_hyst,
407 .get_crit_temp = of_thermal_get_crit_temp,
409 .bind = of_thermal_bind,
410 .unbind = of_thermal_unbind,
415 static struct thermal_zone_device *
416 thermal_zone_of_add_sensor(struct device_node *zone,
417 struct device_node *sensor, void *data,
418 const struct thermal_zone_of_device_ops *ops)
420 struct thermal_zone_device *tzd;
421 struct __thermal_zone *tz;
423 tzd = thermal_zone_get_zone_by_name(zone->name);
425 return ERR_PTR(-EPROBE_DEFER);
430 return ERR_PTR(-EINVAL);
432 mutex_lock(&tzd->lock);
434 tz->sensor_data = data;
436 tzd->ops->get_temp = of_thermal_get_temp;
437 tzd->ops->get_trend = of_thermal_get_trend;
440 * The thermal zone core will calculate the window if they have set the
441 * optional set_trips pointer.
444 tzd->ops->set_trips = of_thermal_set_trips;
446 if (ops->set_emul_temp)
447 tzd->ops->set_emul_temp = of_thermal_set_emul_temp;
449 mutex_unlock(&tzd->lock);
455 * thermal_zone_of_sensor_register - registers a sensor to a DT thermal zone
456 * @dev: a valid struct device pointer of a sensor device. Must contain
457 * a valid .of_node, for the sensor node.
458 * @sensor_id: a sensor identifier, in case the sensor IP has more
460 * @data: a private pointer (owned by the caller) that will be passed
461 * back, when a temperature reading is needed.
462 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
464 * This function will search the list of thermal zones described in device
465 * tree and look for the zone that refer to the sensor device pointed by
466 * @dev->of_node as temperature providers. For the zone pointing to the
467 * sensor node, the sensor will be added to the DT thermal zone device.
469 * The thermal zone temperature is provided by the @get_temp function
470 * pointer. When called, it will have the private pointer @data back.
472 * The thermal zone temperature trend is provided by the @get_trend function
473 * pointer. When called, it will have the private pointer @data back.
476 * 01 - This function must enqueue the new sensor instead of using
477 * it as the only source of temperature values.
479 * 02 - There must be a way to match the sensor with all thermal zones
482 * Return: On success returns a valid struct thermal_zone_device,
483 * otherwise, it returns a corresponding ERR_PTR(). Caller must
484 * check the return value with help of IS_ERR() helper.
486 struct thermal_zone_device *
487 thermal_zone_of_sensor_register(struct device *dev, int sensor_id, void *data,
488 const struct thermal_zone_of_device_ops *ops)
490 struct device_node *np, *child, *sensor_np;
491 struct thermal_zone_device *tzd = ERR_PTR(-ENODEV);
493 np = of_find_node_by_name(NULL, "thermal-zones");
495 return ERR_PTR(-ENODEV);
497 if (!dev || !dev->of_node) {
499 return ERR_PTR(-EINVAL);
502 sensor_np = of_node_get(dev->of_node);
504 for_each_available_child_of_node(np, child) {
505 struct of_phandle_args sensor_specs;
508 /* For now, thermal framework supports only 1 sensor per zone */
509 ret = of_parse_phandle_with_args(child, "thermal-sensors",
510 "#thermal-sensor-cells",
515 if (sensor_specs.args_count >= 1) {
516 id = sensor_specs.args[0];
517 WARN(sensor_specs.args_count > 1,
518 "%pOFn: too many cells in sensor specifier %d\n",
519 sensor_specs.np, sensor_specs.args_count);
524 if (sensor_specs.np == sensor_np && id == sensor_id) {
525 tzd = thermal_zone_of_add_sensor(child, sensor_np,
528 tzd->ops->set_mode(tzd, THERMAL_DEVICE_ENABLED);
530 of_node_put(sensor_specs.np);
534 of_node_put(sensor_specs.np);
537 of_node_put(sensor_np);
542 EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_register);
545 * thermal_zone_of_sensor_unregister - unregisters a sensor from a DT thermal zone
546 * @dev: a valid struct device pointer of a sensor device. Must contain
547 * a valid .of_node, for the sensor node.
548 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
550 * This function removes the sensor callbacks and private data from the
551 * thermal zone device registered with thermal_zone_of_sensor_register()
552 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
553 * thermal zone device callbacks.
555 * TODO: When the support to several sensors per zone is added, this
556 * function must search the sensor list based on @dev parameter.
559 void thermal_zone_of_sensor_unregister(struct device *dev,
560 struct thermal_zone_device *tzd)
562 struct __thermal_zone *tz;
564 if (!dev || !tzd || !tzd->devdata)
569 /* no __thermal_zone, nothing to be done */
573 mutex_lock(&tzd->lock);
574 tzd->ops->get_temp = NULL;
575 tzd->ops->get_trend = NULL;
576 tzd->ops->set_emul_temp = NULL;
579 tz->sensor_data = NULL;
580 mutex_unlock(&tzd->lock);
582 EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister);
584 static void devm_thermal_zone_of_sensor_release(struct device *dev, void *res)
586 thermal_zone_of_sensor_unregister(dev,
587 *(struct thermal_zone_device **)res);
590 static int devm_thermal_zone_of_sensor_match(struct device *dev, void *res,
593 struct thermal_zone_device **r = res;
595 if (WARN_ON(!r || !*r))
602 * devm_thermal_zone_of_sensor_register - Resource managed version of
603 * thermal_zone_of_sensor_register()
604 * @dev: a valid struct device pointer of a sensor device. Must contain
605 * a valid .of_node, for the sensor node.
606 * @sensor_id: a sensor identifier, in case the sensor IP has more
608 * @data: a private pointer (owned by the caller) that will be passed
609 * back, when a temperature reading is needed.
610 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
612 * Refer thermal_zone_of_sensor_register() for more details.
614 * Return: On success returns a valid struct thermal_zone_device,
615 * otherwise, it returns a corresponding ERR_PTR(). Caller must
616 * check the return value with help of IS_ERR() helper.
617 * Registered thermal_zone_device device will automatically be
618 * released when device is unbounded.
620 struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
621 struct device *dev, int sensor_id,
622 void *data, const struct thermal_zone_of_device_ops *ops)
624 struct thermal_zone_device **ptr, *tzd;
626 ptr = devres_alloc(devm_thermal_zone_of_sensor_release, sizeof(*ptr),
629 return ERR_PTR(-ENOMEM);
631 tzd = thermal_zone_of_sensor_register(dev, sensor_id, data, ops);
638 devres_add(dev, ptr);
642 EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_register);
645 * devm_thermal_zone_of_sensor_unregister - Resource managed version of
646 * thermal_zone_of_sensor_unregister().
647 * @dev: Device for which which resource was allocated.
648 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
650 * This function removes the sensor callbacks and private data from the
651 * thermal zone device registered with devm_thermal_zone_of_sensor_register()
652 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
653 * thermal zone device callbacks.
654 * Normally this function will not need to be called and the resource
655 * management code will ensure that the resource is freed.
657 void devm_thermal_zone_of_sensor_unregister(struct device *dev,
658 struct thermal_zone_device *tzd)
660 WARN_ON(devres_release(dev, devm_thermal_zone_of_sensor_release,
661 devm_thermal_zone_of_sensor_match, tzd));
663 EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_unregister);
665 /*** functions parsing device tree nodes ***/
668 * thermal_of_populate_bind_params - parse and fill cooling map data
669 * @np: DT node containing a cooling-map node
670 * @__tbp: data structure to be filled with cooling map info
671 * @trips: array of thermal zone trip points
672 * @ntrips: number of trip points inside trips.
674 * This function parses a cooling-map type of node represented by
675 * @np parameter and fills the read data into @__tbp data structure.
676 * It needs the already parsed array of trip points of the thermal zone
679 * Return: 0 on success, proper error code otherwise
681 static int thermal_of_populate_bind_params(struct device_node *np,
682 struct __thermal_bind_params *__tbp,
683 struct thermal_trip *trips,
686 struct of_phandle_args cooling_spec;
687 struct __thermal_cooling_bind_param *__tcbp;
688 struct device_node *trip;
692 /* Default weight. Usage is optional */
693 __tbp->usage = THERMAL_WEIGHT_DEFAULT;
694 ret = of_property_read_u32(np, "contribution", &prop);
698 trip = of_parse_phandle(np, "trip", 0);
700 pr_err("missing trip property\n");
704 /* match using device_node */
705 for (i = 0; i < ntrips; i++)
706 if (trip == trips[i].np) {
716 count = of_count_phandle_with_args(np, "cooling-device",
719 pr_err("Add a cooling_device property with at least one device\n");
724 __tcbp = kcalloc(count, sizeof(*__tcbp), GFP_KERNEL);
730 for (i = 0; i < count; i++) {
731 ret = of_parse_phandle_with_args(np, "cooling-device",
732 "#cooling-cells", i, &cooling_spec);
734 pr_err("Invalid cooling-device entry\n");
738 __tcbp[i].cooling_device = cooling_spec.np;
740 if (cooling_spec.args_count >= 2) { /* at least min and max */
741 __tcbp[i].min = cooling_spec.args[0];
742 __tcbp[i].max = cooling_spec.args[1];
744 pr_err("wrong reference to cooling device, missing limits\n");
748 __tbp->tcbp = __tcbp;
749 __tbp->count = count;
754 for (i = i - 1; i >= 0; i--)
755 of_node_put(__tcbp[i].cooling_device);
764 * It maps 'enum thermal_trip_type' found in include/linux/thermal.h
765 * into the device tree binding of 'trip', property type.
767 static const char * const trip_types[] = {
768 [THERMAL_TRIP_ACTIVE] = "active",
769 [THERMAL_TRIP_PASSIVE] = "passive",
770 [THERMAL_TRIP_HOT] = "hot",
771 [THERMAL_TRIP_CRITICAL] = "critical",
775 * thermal_of_get_trip_type - Get phy mode for given device_node
776 * @np: Pointer to the given device_node
777 * @type: Pointer to resulting trip type
779 * The function gets trip type string from property 'type',
780 * and store its index in trip_types table in @type,
782 * Return: 0 on success, or errno in error case.
784 static int thermal_of_get_trip_type(struct device_node *np,
785 enum thermal_trip_type *type)
790 err = of_property_read_string(np, "type", &t);
794 for (i = 0; i < ARRAY_SIZE(trip_types); i++)
795 if (!strcasecmp(t, trip_types[i])) {
804 * thermal_of_populate_trip - parse and fill one trip point data
805 * @np: DT node containing a trip point node
806 * @trip: trip point data structure to be filled up
808 * This function parses a trip point type of node represented by
809 * @np parameter and fills the read data into @trip data structure.
811 * Return: 0 on success, proper error code otherwise
813 static int thermal_of_populate_trip(struct device_node *np,
814 struct thermal_trip *trip)
819 ret = of_property_read_u32(np, "temperature", &prop);
821 pr_err("missing temperature property\n");
824 trip->temperature = prop;
826 ret = of_property_read_u32(np, "hysteresis", &prop);
828 pr_err("missing hysteresis property\n");
831 trip->hysteresis = prop;
833 ret = thermal_of_get_trip_type(np, &trip->type);
835 pr_err("wrong trip type property\n");
839 /* Required for cooling map matching */
847 * thermal_of_build_thermal_zone - parse and fill one thermal zone data
848 * @np: DT node containing a thermal zone node
850 * This function parses a thermal zone type of node represented by
851 * @np parameter and fills the read data into a __thermal_zone data structure
852 * and return this pointer.
854 * TODO: Missing properties to parse: thermal-sensor-names
856 * Return: On success returns a valid struct __thermal_zone,
857 * otherwise, it returns a corresponding ERR_PTR(). Caller must
858 * check the return value with help of IS_ERR() helper.
860 static struct __thermal_zone
861 __init *thermal_of_build_thermal_zone(struct device_node *np)
863 struct device_node *child = NULL, *gchild;
864 struct __thermal_zone *tz;
869 pr_err("no thermal zone np\n");
870 return ERR_PTR(-EINVAL);
873 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
875 return ERR_PTR(-ENOMEM);
877 ret = of_property_read_u32(np, "polling-delay-passive", &prop);
879 pr_err("%pOFn: missing polling-delay-passive property\n", np);
882 tz->passive_delay = prop;
884 ret = of_property_read_u32(np, "polling-delay", &prop);
886 pr_err("%pOFn: missing polling-delay property\n", np);
889 tz->polling_delay = prop;
892 * REVIST: for now, the thermal framework supports only
893 * one sensor per thermal zone. Thus, we are considering
894 * only the first two values as slope and offset.
896 ret = of_property_read_u32_array(np, "coefficients", coef, 2);
899 tz->offset = coef[1];
906 child = of_get_child_by_name(np, "trips");
908 /* No trips provided */
912 tz->ntrips = of_get_child_count(child);
913 if (tz->ntrips == 0) /* must have at least one child */
916 tz->trips = kcalloc(tz->ntrips, sizeof(*tz->trips), GFP_KERNEL);
923 for_each_child_of_node(child, gchild) {
924 ret = thermal_of_populate_trip(gchild, &tz->trips[i++]);
932 child = of_get_child_by_name(np, "cooling-maps");
934 /* cooling-maps not provided */
938 tz->num_tbps = of_get_child_count(child);
939 if (tz->num_tbps == 0)
942 tz->tbps = kcalloc(tz->num_tbps, sizeof(*tz->tbps), GFP_KERNEL);
949 for_each_child_of_node(child, gchild) {
950 ret = thermal_of_populate_bind_params(gchild, &tz->tbps[i++],
951 tz->trips, tz->ntrips);
958 tz->mode = THERMAL_DEVICE_DISABLED;
963 for (i = i - 1; i >= 0; i--) {
964 struct __thermal_bind_params *tbp = tz->tbps + i;
967 for (j = 0; j < tbp->count; j++)
968 of_node_put(tbp->tcbp[j].cooling_device);
975 for (i = 0; i < tz->ntrips; i++)
976 of_node_put(tz->trips[i].np);
986 static inline void of_thermal_free_zone(struct __thermal_zone *tz)
988 struct __thermal_bind_params *tbp;
991 for (i = 0; i < tz->num_tbps; i++) {
994 for (j = 0; j < tbp->count; j++)
995 of_node_put(tbp->tcbp[j].cooling_device);
1001 for (i = 0; i < tz->ntrips; i++)
1002 of_node_put(tz->trips[i].np);
1008 * of_parse_thermal_zones - parse device tree thermal data
1010 * Initialization function that can be called by machine initialization
1011 * code to parse thermal data and populate the thermal framework
1012 * with hardware thermal zones info. This function only parses thermal zones.
1013 * Cooling devices and sensor devices nodes are supposed to be parsed
1014 * by their respective drivers.
1016 * Return: 0 on success, proper error code otherwise
1019 int __init of_parse_thermal_zones(void)
1021 struct device_node *np, *child;
1022 struct __thermal_zone *tz;
1023 struct thermal_zone_device_ops *ops;
1025 np = of_find_node_by_name(NULL, "thermal-zones");
1027 pr_debug("unable to find thermal zones\n");
1028 return 0; /* Run successfully on systems without thermal DT */
1031 for_each_available_child_of_node(np, child) {
1032 struct thermal_zone_device *zone;
1033 struct thermal_zone_params *tzp;
1037 tz = thermal_of_build_thermal_zone(child);
1039 pr_err("failed to build thermal zone %pOFn: %ld\n",
1045 ops = kmemdup(&of_thermal_ops, sizeof(*ops), GFP_KERNEL);
1049 tzp = kzalloc(sizeof(*tzp), GFP_KERNEL);
1055 /* No hwmon because there might be hwmon drivers registering */
1056 tzp->no_hwmon = true;
1058 if (!of_property_read_u32(child, "sustainable-power", &prop))
1059 tzp->sustainable_power = prop;
1061 for (i = 0; i < tz->ntrips; i++)
1064 /* these two are left for temperature drivers to use */
1065 tzp->slope = tz->slope;
1066 tzp->offset = tz->offset;
1068 zone = thermal_zone_device_register(child->name, tz->ntrips,
1074 pr_err("Failed to build %pOFn zone %ld\n", child,
1078 of_thermal_free_zone(tz);
1079 /* attempting to build remaining zones still */
1089 of_thermal_free_zone(tz);
1091 /* no memory available, so free what we have built */
1092 of_thermal_destroy_zones();
1098 * of_thermal_destroy_zones - remove all zones parsed and allocated resources
1100 * Finds all zones parsed and added to the thermal framework and remove them
1101 * from the system, together with their resources.
1104 void of_thermal_destroy_zones(void)
1106 struct device_node *np, *child;
1108 np = of_find_node_by_name(NULL, "thermal-zones");
1110 pr_debug("unable to find thermal zones\n");
1114 for_each_available_child_of_node(np, child) {
1115 struct thermal_zone_device *zone;
1117 zone = thermal_zone_get_zone_by_name(child->name);
1121 thermal_zone_device_unregister(zone);
1124 of_thermal_free_zone(zone->devdata);