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>
34 #include "thermal_core.h"
36 /*** Private data structures to represent thermal device tree data ***/
39 * struct __thermal_bind_param - a match between trip and cooling device
40 * @cooling_device: a pointer to identify the referred cooling device
41 * @trip_id: the trip point index
42 * @usage: the percentage (from 0 to 100) of cooling contribution
43 * @min: minimum cooling state used at this trip point
44 * @max: maximum cooling state used at this trip point
47 struct __thermal_bind_params
{
48 struct device_node
*cooling_device
;
56 * struct __thermal_zone - internal representation of a thermal zone
57 * @mode: current thermal zone device mode (enabled/disabled)
58 * @passive_delay: polling interval while passive cooling is activated
59 * @polling_delay: zone polling interval
60 * @slope: slope of the temperature adjustment curve
61 * @offset: offset of the temperature adjustment curve
62 * @ntrips: number of trip points
63 * @trips: an array of trip points (0..ntrips - 1)
64 * @num_tbps: number of thermal bind params
65 * @tbps: an array of thermal bind params (0..num_tbps - 1)
66 * @sensor_data: sensor private data used while reading temperature and trend
67 * @ops: set of callbacks to handle the thermal zone based on DT
70 struct __thermal_zone
{
71 enum thermal_device_mode mode
;
79 struct thermal_trip
*trips
;
81 /* cooling binding data */
83 struct __thermal_bind_params
*tbps
;
85 /* sensor interface */
87 const struct thermal_zone_of_device_ops
*ops
;
90 /*** DT thermal zone device callbacks ***/
92 static int of_thermal_get_temp(struct thermal_zone_device
*tz
,
95 struct __thermal_zone
*data
= tz
->devdata
;
97 if (!data
->ops
->get_temp
)
100 return data
->ops
->get_temp(data
->sensor_data
, temp
);
103 static int of_thermal_set_trips(struct thermal_zone_device
*tz
,
106 struct __thermal_zone
*data
= tz
->devdata
;
108 if (!data
->ops
|| !data
->ops
->set_trips
)
111 return data
->ops
->set_trips(data
->sensor_data
, low
, high
);
115 * of_thermal_get_ntrips - function to export number of available trip
117 * @tz: pointer to a thermal zone
119 * This function is a globally visible wrapper to get number of trip points
120 * stored in the local struct __thermal_zone
122 * Return: number of available trip points, -ENODEV when data not available
124 int of_thermal_get_ntrips(struct thermal_zone_device
*tz
)
126 struct __thermal_zone
*data
= tz
->devdata
;
128 if (!data
|| IS_ERR(data
))
133 EXPORT_SYMBOL_GPL(of_thermal_get_ntrips
);
136 * of_thermal_is_trip_valid - function to check if trip point is valid
138 * @tz: pointer to a thermal zone
139 * @trip: trip point to evaluate
141 * This function is responsible for checking if passed trip point is valid
143 * Return: true if trip point is valid, false otherwise
145 bool of_thermal_is_trip_valid(struct thermal_zone_device
*tz
, int trip
)
147 struct __thermal_zone
*data
= tz
->devdata
;
149 if (!data
|| trip
>= data
->ntrips
|| trip
< 0)
154 EXPORT_SYMBOL_GPL(of_thermal_is_trip_valid
);
157 * of_thermal_get_trip_points - function to get access to a globally exported
160 * @tz: pointer to a thermal zone
162 * This function provides a pointer to trip points table
164 * Return: pointer to trip points table, NULL otherwise
166 const struct thermal_trip
*
167 of_thermal_get_trip_points(struct thermal_zone_device
*tz
)
169 struct __thermal_zone
*data
= tz
->devdata
;
176 EXPORT_SYMBOL_GPL(of_thermal_get_trip_points
);
179 * of_thermal_set_emul_temp - function to set emulated temperature
181 * @tz: pointer to a thermal zone
182 * @temp: temperature to set
184 * This function gives the ability to set emulated value of temperature,
185 * which is handy for debugging
187 * Return: zero on success, error code otherwise
189 static int of_thermal_set_emul_temp(struct thermal_zone_device
*tz
,
192 struct __thermal_zone
*data
= tz
->devdata
;
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
->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
;
214 if (!data
|| IS_ERR(data
))
217 /* find where to bind */
218 for (i
= 0; i
< data
->num_tbps
; i
++) {
219 struct __thermal_bind_params
*tbp
= data
->tbps
+ i
;
221 if (tbp
->cooling_device
== cdev
->np
) {
224 ret
= thermal_zone_bind_cooling_device(thermal
,
237 static int of_thermal_unbind(struct thermal_zone_device
*thermal
,
238 struct thermal_cooling_device
*cdev
)
240 struct __thermal_zone
*data
= thermal
->devdata
;
243 if (!data
|| IS_ERR(data
))
246 /* find where to unbind */
247 for (i
= 0; i
< data
->num_tbps
; i
++) {
248 struct __thermal_bind_params
*tbp
= data
->tbps
+ i
;
250 if (tbp
->cooling_device
== cdev
->np
) {
253 ret
= thermal_zone_unbind_cooling_device(thermal
,
263 static int of_thermal_get_mode(struct thermal_zone_device
*tz
,
264 enum thermal_device_mode
*mode
)
266 struct __thermal_zone
*data
= tz
->devdata
;
273 static int of_thermal_set_mode(struct thermal_zone_device
*tz
,
274 enum thermal_device_mode mode
)
276 struct __thermal_zone
*data
= tz
->devdata
;
278 mutex_lock(&tz
->lock
);
280 if (mode
== THERMAL_DEVICE_ENABLED
)
281 tz
->polling_delay
= data
->polling_delay
;
283 tz
->polling_delay
= 0;
285 mutex_unlock(&tz
->lock
);
288 thermal_zone_device_update(tz
, THERMAL_EVENT_UNSPECIFIED
);
293 static int of_thermal_get_trip_type(struct thermal_zone_device
*tz
, int trip
,
294 enum thermal_trip_type
*type
)
296 struct __thermal_zone
*data
= tz
->devdata
;
298 if (trip
>= data
->ntrips
|| trip
< 0)
301 *type
= data
->trips
[trip
].type
;
306 static int of_thermal_get_trip_temp(struct thermal_zone_device
*tz
, int trip
,
309 struct __thermal_zone
*data
= tz
->devdata
;
311 if (trip
>= data
->ntrips
|| trip
< 0)
314 *temp
= data
->trips
[trip
].temperature
;
319 static int of_thermal_set_trip_temp(struct thermal_zone_device
*tz
, int trip
,
322 struct __thermal_zone
*data
= tz
->devdata
;
324 if (trip
>= data
->ntrips
|| trip
< 0)
327 if (data
->ops
->set_trip_temp
) {
330 ret
= data
->ops
->set_trip_temp(data
->sensor_data
, trip
, temp
);
335 /* thermal framework should take care of data->mask & (1 << trip) */
336 data
->trips
[trip
].temperature
= temp
;
341 static int of_thermal_get_trip_hyst(struct thermal_zone_device
*tz
, int trip
,
344 struct __thermal_zone
*data
= tz
->devdata
;
346 if (trip
>= data
->ntrips
|| trip
< 0)
349 *hyst
= data
->trips
[trip
].hysteresis
;
354 static int of_thermal_set_trip_hyst(struct thermal_zone_device
*tz
, int trip
,
357 struct __thermal_zone
*data
= tz
->devdata
;
359 if (trip
>= data
->ntrips
|| trip
< 0)
362 /* thermal framework should take care of data->mask & (1 << trip) */
363 data
->trips
[trip
].hysteresis
= hyst
;
368 static int of_thermal_get_crit_temp(struct thermal_zone_device
*tz
,
371 struct __thermal_zone
*data
= tz
->devdata
;
374 for (i
= 0; i
< data
->ntrips
; i
++)
375 if (data
->trips
[i
].type
== THERMAL_TRIP_CRITICAL
) {
376 *temp
= data
->trips
[i
].temperature
;
383 static struct thermal_zone_device_ops of_thermal_ops
= {
384 .get_mode
= of_thermal_get_mode
,
385 .set_mode
= of_thermal_set_mode
,
387 .get_trip_type
= of_thermal_get_trip_type
,
388 .get_trip_temp
= of_thermal_get_trip_temp
,
389 .set_trip_temp
= of_thermal_set_trip_temp
,
390 .get_trip_hyst
= of_thermal_get_trip_hyst
,
391 .set_trip_hyst
= of_thermal_set_trip_hyst
,
392 .get_crit_temp
= of_thermal_get_crit_temp
,
394 .bind
= of_thermal_bind
,
395 .unbind
= of_thermal_unbind
,
400 static struct thermal_zone_device
*
401 thermal_zone_of_add_sensor(struct device_node
*zone
,
402 struct device_node
*sensor
, void *data
,
403 const struct thermal_zone_of_device_ops
*ops
)
405 struct thermal_zone_device
*tzd
;
406 struct __thermal_zone
*tz
;
408 tzd
= thermal_zone_get_zone_by_name(zone
->name
);
410 return ERR_PTR(-EPROBE_DEFER
);
415 return ERR_PTR(-EINVAL
);
417 mutex_lock(&tzd
->lock
);
419 tz
->sensor_data
= data
;
421 tzd
->ops
->get_temp
= of_thermal_get_temp
;
422 tzd
->ops
->get_trend
= of_thermal_get_trend
;
425 * The thermal zone core will calculate the window if they have set the
426 * optional set_trips pointer.
429 tzd
->ops
->set_trips
= of_thermal_set_trips
;
431 if (ops
->set_emul_temp
)
432 tzd
->ops
->set_emul_temp
= of_thermal_set_emul_temp
;
434 mutex_unlock(&tzd
->lock
);
440 * thermal_zone_of_sensor_register - registers a sensor to a DT thermal zone
441 * @dev: a valid struct device pointer of a sensor device. Must contain
442 * a valid .of_node, for the sensor node.
443 * @sensor_id: a sensor identifier, in case the sensor IP has more
445 * @data: a private pointer (owned by the caller) that will be passed
446 * back, when a temperature reading is needed.
447 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
449 * This function will search the list of thermal zones described in device
450 * tree and look for the zone that refer to the sensor device pointed by
451 * @dev->of_node as temperature providers. For the zone pointing to the
452 * sensor node, the sensor will be added to the DT thermal zone device.
454 * The thermal zone temperature is provided by the @get_temp function
455 * pointer. When called, it will have the private pointer @data back.
457 * The thermal zone temperature trend is provided by the @get_trend function
458 * pointer. When called, it will have the private pointer @data back.
461 * 01 - This function must enqueue the new sensor instead of using
462 * it as the only source of temperature values.
464 * 02 - There must be a way to match the sensor with all thermal zones
467 * Return: On success returns a valid struct thermal_zone_device,
468 * otherwise, it returns a corresponding ERR_PTR(). Caller must
469 * check the return value with help of IS_ERR() helper.
471 struct thermal_zone_device
*
472 thermal_zone_of_sensor_register(struct device
*dev
, int sensor_id
, void *data
,
473 const struct thermal_zone_of_device_ops
*ops
)
475 struct device_node
*np
, *child
, *sensor_np
;
476 struct thermal_zone_device
*tzd
= ERR_PTR(-ENODEV
);
478 np
= of_find_node_by_name(NULL
, "thermal-zones");
480 return ERR_PTR(-ENODEV
);
482 if (!dev
|| !dev
->of_node
) {
484 return ERR_PTR(-EINVAL
);
487 sensor_np
= of_node_get(dev
->of_node
);
489 for_each_available_child_of_node(np
, child
) {
490 struct of_phandle_args sensor_specs
;
493 /* For now, thermal framework supports only 1 sensor per zone */
494 ret
= of_parse_phandle_with_args(child
, "thermal-sensors",
495 "#thermal-sensor-cells",
500 if (sensor_specs
.args_count
>= 1) {
501 id
= sensor_specs
.args
[0];
502 WARN(sensor_specs
.args_count
> 1,
503 "%s: too many cells in sensor specifier %d\n",
504 sensor_specs
.np
->name
, sensor_specs
.args_count
);
509 if (sensor_specs
.np
== sensor_np
&& id
== sensor_id
) {
510 tzd
= thermal_zone_of_add_sensor(child
, sensor_np
,
513 tzd
->ops
->set_mode(tzd
, THERMAL_DEVICE_ENABLED
);
515 of_node_put(sensor_specs
.np
);
519 of_node_put(sensor_specs
.np
);
522 of_node_put(sensor_np
);
527 EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_register
);
530 * thermal_zone_of_sensor_unregister - unregisters a sensor from a DT thermal zone
531 * @dev: a valid struct device pointer of a sensor device. Must contain
532 * a valid .of_node, for the sensor node.
533 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
535 * This function removes the sensor callbacks and private data from the
536 * thermal zone device registered with thermal_zone_of_sensor_register()
537 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
538 * thermal zone device callbacks.
540 * TODO: When the support to several sensors per zone is added, this
541 * function must search the sensor list based on @dev parameter.
544 void thermal_zone_of_sensor_unregister(struct device
*dev
,
545 struct thermal_zone_device
*tzd
)
547 struct __thermal_zone
*tz
;
549 if (!dev
|| !tzd
|| !tzd
->devdata
)
554 /* no __thermal_zone, nothing to be done */
558 mutex_lock(&tzd
->lock
);
559 tzd
->ops
->get_temp
= NULL
;
560 tzd
->ops
->get_trend
= NULL
;
561 tzd
->ops
->set_emul_temp
= NULL
;
564 tz
->sensor_data
= NULL
;
565 mutex_unlock(&tzd
->lock
);
567 EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister
);
569 static void devm_thermal_zone_of_sensor_release(struct device
*dev
, void *res
)
571 thermal_zone_of_sensor_unregister(dev
,
572 *(struct thermal_zone_device
**)res
);
575 static int devm_thermal_zone_of_sensor_match(struct device
*dev
, void *res
,
578 struct thermal_zone_device
**r
= res
;
580 if (WARN_ON(!r
|| !*r
))
587 * devm_thermal_zone_of_sensor_register - Resource managed version of
588 * thermal_zone_of_sensor_register()
589 * @dev: a valid struct device pointer of a sensor device. Must contain
590 * a valid .of_node, for the sensor node.
591 * @sensor_id: a sensor identifier, in case the sensor IP has more
593 * @data: a private pointer (owned by the caller) that will be passed
594 * back, when a temperature reading is needed.
595 * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
597 * Refer thermal_zone_of_sensor_register() for more details.
599 * Return: On success returns a valid struct thermal_zone_device,
600 * otherwise, it returns a corresponding ERR_PTR(). Caller must
601 * check the return value with help of IS_ERR() helper.
602 * Registered thermal_zone_device device will automatically be
603 * released when device is unbounded.
605 struct thermal_zone_device
*devm_thermal_zone_of_sensor_register(
606 struct device
*dev
, int sensor_id
,
607 void *data
, const struct thermal_zone_of_device_ops
*ops
)
609 struct thermal_zone_device
**ptr
, *tzd
;
611 ptr
= devres_alloc(devm_thermal_zone_of_sensor_release
, sizeof(*ptr
),
614 return ERR_PTR(-ENOMEM
);
616 tzd
= thermal_zone_of_sensor_register(dev
, sensor_id
, data
, ops
);
623 devres_add(dev
, ptr
);
627 EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_register
);
630 * devm_thermal_zone_of_sensor_unregister - Resource managed version of
631 * thermal_zone_of_sensor_unregister().
632 * @dev: Device for which which resource was allocated.
633 * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
635 * This function removes the sensor callbacks and private data from the
636 * thermal zone device registered with devm_thermal_zone_of_sensor_register()
637 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
638 * thermal zone device callbacks.
639 * Normally this function will not need to be called and the resource
640 * management code will ensure that the resource is freed.
642 void devm_thermal_zone_of_sensor_unregister(struct device
*dev
,
643 struct thermal_zone_device
*tzd
)
645 WARN_ON(devres_release(dev
, devm_thermal_zone_of_sensor_release
,
646 devm_thermal_zone_of_sensor_match
, tzd
));
648 EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_unregister
);
650 /*** functions parsing device tree nodes ***/
653 * thermal_of_populate_bind_params - parse and fill cooling map data
654 * @np: DT node containing a cooling-map node
655 * @__tbp: data structure to be filled with cooling map info
656 * @trips: array of thermal zone trip points
657 * @ntrips: number of trip points inside trips.
659 * This function parses a cooling-map type of node represented by
660 * @np parameter and fills the read data into @__tbp data structure.
661 * It needs the already parsed array of trip points of the thermal zone
664 * Return: 0 on success, proper error code otherwise
666 static int thermal_of_populate_bind_params(struct device_node
*np
,
667 struct __thermal_bind_params
*__tbp
,
668 struct thermal_trip
*trips
,
671 struct of_phandle_args cooling_spec
;
672 struct device_node
*trip
;
676 /* Default weight. Usage is optional */
677 __tbp
->usage
= THERMAL_WEIGHT_DEFAULT
;
678 ret
= of_property_read_u32(np
, "contribution", &prop
);
682 trip
= of_parse_phandle(np
, "trip", 0);
684 pr_err("missing trip property\n");
688 /* match using device_node */
689 for (i
= 0; i
< ntrips
; i
++)
690 if (trip
== trips
[i
].np
) {
700 ret
= of_parse_phandle_with_args(np
, "cooling-device", "#cooling-cells",
703 pr_err("missing cooling_device property\n");
706 __tbp
->cooling_device
= cooling_spec
.np
;
707 if (cooling_spec
.args_count
>= 2) { /* at least min and max */
708 __tbp
->min
= cooling_spec
.args
[0];
709 __tbp
->max
= cooling_spec
.args
[1];
711 pr_err("wrong reference to cooling device, missing limits\n");
721 * It maps 'enum thermal_trip_type' found in include/linux/thermal.h
722 * into the device tree binding of 'trip', property type.
724 static const char * const trip_types
[] = {
725 [THERMAL_TRIP_ACTIVE
] = "active",
726 [THERMAL_TRIP_PASSIVE
] = "passive",
727 [THERMAL_TRIP_HOT
] = "hot",
728 [THERMAL_TRIP_CRITICAL
] = "critical",
732 * thermal_of_get_trip_type - Get phy mode for given device_node
733 * @np: Pointer to the given device_node
734 * @type: Pointer to resulting trip type
736 * The function gets trip type string from property 'type',
737 * and store its index in trip_types table in @type,
739 * Return: 0 on success, or errno in error case.
741 static int thermal_of_get_trip_type(struct device_node
*np
,
742 enum thermal_trip_type
*type
)
747 err
= of_property_read_string(np
, "type", &t
);
751 for (i
= 0; i
< ARRAY_SIZE(trip_types
); i
++)
752 if (!strcasecmp(t
, trip_types
[i
])) {
761 * thermal_of_populate_trip - parse and fill one trip point data
762 * @np: DT node containing a trip point node
763 * @trip: trip point data structure to be filled up
765 * This function parses a trip point type of node represented by
766 * @np parameter and fills the read data into @trip data structure.
768 * Return: 0 on success, proper error code otherwise
770 static int thermal_of_populate_trip(struct device_node
*np
,
771 struct thermal_trip
*trip
)
776 ret
= of_property_read_u32(np
, "temperature", &prop
);
778 pr_err("missing temperature property\n");
781 trip
->temperature
= prop
;
783 ret
= of_property_read_u32(np
, "hysteresis", &prop
);
785 pr_err("missing hysteresis property\n");
788 trip
->hysteresis
= prop
;
790 ret
= thermal_of_get_trip_type(np
, &trip
->type
);
792 pr_err("wrong trip type property\n");
796 /* Required for cooling map matching */
804 * thermal_of_build_thermal_zone - parse and fill one thermal zone data
805 * @np: DT node containing a thermal zone node
807 * This function parses a thermal zone type of node represented by
808 * @np parameter and fills the read data into a __thermal_zone data structure
809 * and return this pointer.
811 * TODO: Missing properties to parse: thermal-sensor-names
813 * Return: On success returns a valid struct __thermal_zone,
814 * otherwise, it returns a corresponding ERR_PTR(). Caller must
815 * check the return value with help of IS_ERR() helper.
817 static struct __thermal_zone
818 __init
*thermal_of_build_thermal_zone(struct device_node
*np
)
820 struct device_node
*child
= NULL
, *gchild
;
821 struct __thermal_zone
*tz
;
826 pr_err("no thermal zone np\n");
827 return ERR_PTR(-EINVAL
);
830 tz
= kzalloc(sizeof(*tz
), GFP_KERNEL
);
832 return ERR_PTR(-ENOMEM
);
834 ret
= of_property_read_u32(np
, "polling-delay-passive", &prop
);
836 pr_err("missing polling-delay-passive property\n");
839 tz
->passive_delay
= prop
;
841 ret
= of_property_read_u32(np
, "polling-delay", &prop
);
843 pr_err("missing polling-delay property\n");
846 tz
->polling_delay
= prop
;
849 * REVIST: for now, the thermal framework supports only
850 * one sensor per thermal zone. Thus, we are considering
851 * only the first two values as slope and offset.
853 ret
= of_property_read_u32_array(np
, "coefficients", coef
, 2);
856 tz
->offset
= coef
[1];
863 child
= of_get_child_by_name(np
, "trips");
865 /* No trips provided */
869 tz
->ntrips
= of_get_child_count(child
);
870 if (tz
->ntrips
== 0) /* must have at least one child */
873 tz
->trips
= kzalloc(tz
->ntrips
* sizeof(*tz
->trips
), GFP_KERNEL
);
880 for_each_child_of_node(child
, gchild
) {
881 ret
= thermal_of_populate_trip(gchild
, &tz
->trips
[i
++]);
889 child
= of_get_child_by_name(np
, "cooling-maps");
891 /* cooling-maps not provided */
895 tz
->num_tbps
= of_get_child_count(child
);
896 if (tz
->num_tbps
== 0)
899 tz
->tbps
= kzalloc(tz
->num_tbps
* sizeof(*tz
->tbps
), GFP_KERNEL
);
906 for_each_child_of_node(child
, gchild
) {
907 ret
= thermal_of_populate_bind_params(gchild
, &tz
->tbps
[i
++],
908 tz
->trips
, tz
->ntrips
);
915 tz
->mode
= THERMAL_DEVICE_DISABLED
;
920 for (i
= i
- 1; i
>= 0; i
--)
921 of_node_put(tz
->tbps
[i
].cooling_device
);
924 for (i
= 0; i
< tz
->ntrips
; i
++)
925 of_node_put(tz
->trips
[i
].np
);
935 static inline void of_thermal_free_zone(struct __thermal_zone
*tz
)
939 for (i
= 0; i
< tz
->num_tbps
; i
++)
940 of_node_put(tz
->tbps
[i
].cooling_device
);
942 for (i
= 0; i
< tz
->ntrips
; i
++)
943 of_node_put(tz
->trips
[i
].np
);
949 * of_parse_thermal_zones - parse device tree thermal data
951 * Initialization function that can be called by machine initialization
952 * code to parse thermal data and populate the thermal framework
953 * with hardware thermal zones info. This function only parses thermal zones.
954 * Cooling devices and sensor devices nodes are supposed to be parsed
955 * by their respective drivers.
957 * Return: 0 on success, proper error code otherwise
960 int __init
of_parse_thermal_zones(void)
962 struct device_node
*np
, *child
;
963 struct __thermal_zone
*tz
;
964 struct thermal_zone_device_ops
*ops
;
966 np
= of_find_node_by_name(NULL
, "thermal-zones");
968 pr_debug("unable to find thermal zones\n");
969 return 0; /* Run successfully on systems without thermal DT */
972 for_each_available_child_of_node(np
, child
) {
973 struct thermal_zone_device
*zone
;
974 struct thermal_zone_params
*tzp
;
978 tz
= thermal_of_build_thermal_zone(child
);
980 pr_err("failed to build thermal zone %s: %ld\n",
986 ops
= kmemdup(&of_thermal_ops
, sizeof(*ops
), GFP_KERNEL
);
990 tzp
= kzalloc(sizeof(*tzp
), GFP_KERNEL
);
996 /* No hwmon because there might be hwmon drivers registering */
997 tzp
->no_hwmon
= true;
999 if (!of_property_read_u32(child
, "sustainable-power", &prop
))
1000 tzp
->sustainable_power
= prop
;
1002 for (i
= 0; i
< tz
->ntrips
; i
++)
1005 /* these two are left for temperature drivers to use */
1006 tzp
->slope
= tz
->slope
;
1007 tzp
->offset
= tz
->offset
;
1009 zone
= thermal_zone_device_register(child
->name
, tz
->ntrips
,
1015 pr_err("Failed to build %s zone %ld\n", child
->name
,
1019 of_thermal_free_zone(tz
);
1020 /* attempting to build remaining zones still */
1030 of_thermal_free_zone(tz
);
1032 /* no memory available, so free what we have built */
1033 of_thermal_destroy_zones();
1039 * of_thermal_destroy_zones - remove all zones parsed and allocated resources
1041 * Finds all zones parsed and added to the thermal framework and remove them
1042 * from the system, together with their resources.
1045 void of_thermal_destroy_zones(void)
1047 struct device_node
*np
, *child
;
1049 np
= of_find_node_by_name(NULL
, "thermal-zones");
1051 pr_debug("unable to find thermal zones\n");
1055 for_each_available_child_of_node(np
, child
) {
1056 struct thermal_zone_device
*zone
;
1058 zone
= thermal_zone_get_zone_by_name(child
->name
);
1062 thermal_zone_device_unregister(zone
);
1065 of_thermal_free_zone(zone
->devdata
);