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"
23 * of_thermal_get_ntrips - function to export number of available trip
25 * @tz: pointer to a thermal zone
27 * This function is a globally visible wrapper to get number of trip points
28 * stored in the local struct __thermal_zone
30 * Return: number of available trip points, -ENODEV when data not available
32 int of_thermal_get_ntrips(struct thermal_zone_device *tz)
36 EXPORT_SYMBOL_GPL(of_thermal_get_ntrips);
39 * of_thermal_is_trip_valid - function to check if trip point is valid
41 * @tz: pointer to a thermal zone
42 * @trip: trip point to evaluate
44 * This function is responsible for checking if passed trip point is valid
46 * Return: true if trip point is valid, false otherwise
48 bool of_thermal_is_trip_valid(struct thermal_zone_device *tz, int trip)
50 if (trip >= tz->num_trips || trip < 0)
55 EXPORT_SYMBOL_GPL(of_thermal_is_trip_valid);
58 * of_thermal_get_trip_points - function to get access to a globally exported
61 * @tz: pointer to a thermal zone
63 * This function provides a pointer to trip points table
65 * Return: pointer to trip points table, NULL otherwise
67 const struct thermal_trip *
68 of_thermal_get_trip_points(struct thermal_zone_device *tz)
72 EXPORT_SYMBOL_GPL(of_thermal_get_trip_points);
74 static int of_thermal_get_trip_type(struct thermal_zone_device *tz, int trip,
75 enum thermal_trip_type *type)
77 if (trip >= tz->num_trips || trip < 0)
80 *type = tz->trips[trip].type;
85 static int of_thermal_get_trip_temp(struct thermal_zone_device *tz, int trip,
88 if (trip >= tz->num_trips || trip < 0)
91 *temp = tz->trips[trip].temperature;
96 static int of_thermal_get_trip_hyst(struct thermal_zone_device *tz, int trip,
99 if (trip >= tz->num_trips || trip < 0)
102 *hyst = tz->trips[trip].hysteresis;
107 static int of_thermal_set_trip_hyst(struct thermal_zone_device *tz, int trip,
110 if (trip >= tz->num_trips || trip < 0)
113 /* thermal framework should take care of data->mask & (1 << trip) */
114 tz->trips[trip].hysteresis = hyst;
119 static int of_thermal_get_crit_temp(struct thermal_zone_device *tz,
124 for (i = 0; i < tz->num_trips; i++)
125 if (tz->trips[i].type == THERMAL_TRIP_CRITICAL) {
126 *temp = tz->trips[i].temperature;
134 * thermal_zone_of_get_sensor_id - get sensor ID from a DT thermal zone
135 * @tz_np: a valid thermal zone device node.
136 * @sensor_np: a sensor node of a valid sensor device.
137 * @id: the sensor ID returned if success.
139 * This function will get sensor ID from a given thermal zone node and
140 * the sensor node must match the temperature provider @sensor_np.
142 * Return: 0 on success, proper error code otherwise.
145 int thermal_zone_of_get_sensor_id(struct device_node *tz_np,
146 struct device_node *sensor_np,
149 struct of_phandle_args sensor_specs;
152 ret = of_parse_phandle_with_args(tz_np,
154 "#thermal-sensor-cells",
160 if (sensor_specs.np != sensor_np) {
161 of_node_put(sensor_specs.np);
165 if (sensor_specs.args_count > 1)
166 pr_warn("%pOFn: too many cells in sensor specifier %d\n",
167 sensor_specs.np, sensor_specs.args_count);
169 *id = sensor_specs.args_count ? sensor_specs.args[0] : 0;
171 of_node_put(sensor_specs.np);
175 EXPORT_SYMBOL_GPL(thermal_zone_of_get_sensor_id);
177 /*** functions parsing device tree nodes ***/
179 static int of_find_trip_id(struct device_node *np, struct device_node *trip)
181 struct device_node *trips;
182 struct device_node *t;
185 trips = of_get_child_by_name(np, "trips");
187 pr_err("Failed to find 'trips' node\n");
192 * Find the trip id point associated with the cooling device map
194 for_each_child_of_node(trips, t) {
209 * It maps 'enum thermal_trip_type' found in include/linux/thermal.h
210 * into the device tree binding of 'trip', property type.
212 static const char * const trip_types[] = {
213 [THERMAL_TRIP_ACTIVE] = "active",
214 [THERMAL_TRIP_PASSIVE] = "passive",
215 [THERMAL_TRIP_HOT] = "hot",
216 [THERMAL_TRIP_CRITICAL] = "critical",
220 * thermal_of_get_trip_type - Get phy mode for given device_node
221 * @np: Pointer to the given device_node
222 * @type: Pointer to resulting trip type
224 * The function gets trip type string from property 'type',
225 * and store its index in trip_types table in @type,
227 * Return: 0 on success, or errno in error case.
229 static int thermal_of_get_trip_type(struct device_node *np,
230 enum thermal_trip_type *type)
235 err = of_property_read_string(np, "type", &t);
239 for (i = 0; i < ARRAY_SIZE(trip_types); i++)
240 if (!strcasecmp(t, trip_types[i])) {
248 static int thermal_of_populate_trip(struct device_node *np,
249 struct thermal_trip *trip)
254 ret = of_property_read_u32(np, "temperature", &prop);
256 pr_err("missing temperature property\n");
259 trip->temperature = prop;
261 ret = of_property_read_u32(np, "hysteresis", &prop);
263 pr_err("missing hysteresis property\n");
266 trip->hysteresis = prop;
268 ret = thermal_of_get_trip_type(np, &trip->type);
270 pr_err("wrong trip type property\n");
277 static struct thermal_trip *thermal_of_trips_init(struct device_node *np, int *ntrips)
279 struct thermal_trip *tt;
280 struct device_node *trips, *trip;
283 trips = of_get_child_by_name(np, "trips");
285 pr_err("Failed to find 'trips' node\n");
286 return ERR_PTR(-EINVAL);
289 count = of_get_child_count(trips);
291 pr_err("No trip point defined\n");
293 goto out_of_node_put;
296 tt = kzalloc(sizeof(*tt) * count, GFP_KERNEL);
299 goto out_of_node_put;
305 for_each_child_of_node(trips, trip) {
306 ret = thermal_of_populate_trip(trip, &tt[count++]);
324 static struct device_node *of_thermal_zone_find(struct device_node *sensor, int id)
326 struct device_node *np, *tz;
327 struct of_phandle_args sensor_specs;
329 np = of_find_node_by_name(NULL, "thermal-zones");
331 pr_debug("No thermal zones description\n");
332 return ERR_PTR(-ENODEV);
336 * Search for each thermal zone, a defined sensor
337 * corresponding to the one passed as parameter
339 for_each_available_child_of_node(np, tz) {
343 count = of_count_phandle_with_args(tz, "thermal-sensors",
344 "#thermal-sensor-cells");
346 pr_err("%pOFn: missing thermal sensor\n", tz);
347 tz = ERR_PTR(-EINVAL);
351 for (i = 0; i < count; i++) {
355 ret = of_parse_phandle_with_args(tz, "thermal-sensors",
356 "#thermal-sensor-cells",
359 pr_err("%pOFn: Failed to read thermal-sensors cells: %d\n", tz, ret);
364 if ((sensor == sensor_specs.np) && id == (sensor_specs.args_count ?
365 sensor_specs.args[0] : 0)) {
366 pr_debug("sensor %pOFn id=%d belongs to %pOFn\n", sensor, id, tz);
371 tz = ERR_PTR(-ENODEV);
377 static int thermal_of_monitor_init(struct device_node *np, int *delay, int *pdelay)
381 ret = of_property_read_u32(np, "polling-delay-passive", pdelay);
383 pr_err("%pOFn: missing polling-delay-passive property\n", np);
387 ret = of_property_read_u32(np, "polling-delay", delay);
389 pr_err("%pOFn: missing polling-delay property\n", np);
396 static struct thermal_zone_params *thermal_of_parameters_init(struct device_node *np)
398 struct thermal_zone_params *tzp;
400 int ncoef = ARRAY_SIZE(coef);
403 tzp = kzalloc(sizeof(*tzp), GFP_KERNEL);
405 return ERR_PTR(-ENOMEM);
407 tzp->no_hwmon = true;
409 if (!of_property_read_u32(np, "sustainable-power", &prop))
410 tzp->sustainable_power = prop;
413 * For now, the thermal framework supports only one sensor per
414 * thermal zone. Thus, we are considering only the first two
415 * values as slope and offset.
417 ret = of_property_read_u32_array(np, "coefficients", coef, ncoef);
423 tzp->slope = coef[0];
424 tzp->offset = coef[1];
429 static struct device_node *thermal_of_zone_get_by_name(struct thermal_zone_device *tz)
431 struct device_node *np, *tz_np;
433 np = of_find_node_by_name(NULL, "thermal-zones");
435 return ERR_PTR(-ENODEV);
437 tz_np = of_get_child_by_name(np, tz->type);
442 return ERR_PTR(-ENODEV);
447 static int __thermal_of_unbind(struct device_node *map_np, int index, int trip_id,
448 struct thermal_zone_device *tz, struct thermal_cooling_device *cdev)
450 struct of_phandle_args cooling_spec;
453 ret = of_parse_phandle_with_args(map_np, "cooling-device", "#cooling-cells",
454 index, &cooling_spec);
456 of_node_put(cooling_spec.np);
459 pr_err("Invalid cooling-device entry\n");
463 if (cooling_spec.args_count < 2) {
464 pr_err("wrong reference to cooling device, missing limits\n");
468 if (cooling_spec.np != cdev->np)
471 ret = thermal_zone_unbind_cooling_device(tz, trip_id, cdev);
473 pr_err("Failed to unbind '%s' with '%s': %d\n", tz->type, cdev->type, ret);
478 static int __thermal_of_bind(struct device_node *map_np, int index, int trip_id,
479 struct thermal_zone_device *tz, struct thermal_cooling_device *cdev)
481 struct of_phandle_args cooling_spec;
482 int ret, weight = THERMAL_WEIGHT_DEFAULT;
484 of_property_read_u32(map_np, "contribution", &weight);
486 ret = of_parse_phandle_with_args(map_np, "cooling-device", "#cooling-cells",
487 index, &cooling_spec);
489 of_node_put(cooling_spec.np);
492 pr_err("Invalid cooling-device entry\n");
496 if (cooling_spec.args_count < 2) {
497 pr_err("wrong reference to cooling device, missing limits\n");
501 if (cooling_spec.np != cdev->np)
504 ret = thermal_zone_bind_cooling_device(tz, trip_id, cdev, cooling_spec.args[1],
505 cooling_spec.args[0],
508 pr_err("Failed to bind '%s' with '%s': %d\n", tz->type, cdev->type, ret);
513 static int thermal_of_for_each_cooling_device(struct device_node *tz_np, struct device_node *map_np,
514 struct thermal_zone_device *tz, struct thermal_cooling_device *cdev,
515 int (*action)(struct device_node *, int, int,
516 struct thermal_zone_device *, struct thermal_cooling_device *))
518 struct device_node *tr_np;
519 int count, i, trip_id;
521 tr_np = of_parse_phandle(map_np, "trip", 0);
525 trip_id = of_find_trip_id(tz_np, tr_np);
529 count = of_count_phandle_with_args(map_np, "cooling-device", "#cooling-cells");
531 pr_err("Add a cooling_device property with at least one device\n");
536 * At this point, we don't want to bail out when there is an
537 * error, we will try to bind/unbind as many as possible
540 for (i = 0; i < count; i++)
541 action(map_np, i, trip_id, tz, cdev);
546 static int thermal_of_for_each_cooling_maps(struct thermal_zone_device *tz,
547 struct thermal_cooling_device *cdev,
548 int (*action)(struct device_node *, int, int,
549 struct thermal_zone_device *, struct thermal_cooling_device *))
551 struct device_node *tz_np, *cm_np, *child;
554 tz_np = thermal_of_zone_get_by_name(tz);
556 pr_err("Failed to get node tz by name\n");
557 return PTR_ERR(tz_np);
560 cm_np = of_get_child_by_name(tz_np, "cooling-maps");
564 for_each_child_of_node(cm_np, child) {
565 ret = thermal_of_for_each_cooling_device(tz_np, child, tz, cdev, action);
577 static int thermal_of_bind(struct thermal_zone_device *tz,
578 struct thermal_cooling_device *cdev)
580 return thermal_of_for_each_cooling_maps(tz, cdev, __thermal_of_bind);
583 static int thermal_of_unbind(struct thermal_zone_device *tz,
584 struct thermal_cooling_device *cdev)
586 return thermal_of_for_each_cooling_maps(tz, cdev, __thermal_of_unbind);
590 * thermal_of_zone_unregister - Cleanup the specific allocated ressources
592 * This function disables the thermal zone and frees the different
593 * ressources allocated specific to the thermal OF.
595 * @tz: a pointer to the thermal zone structure
597 void thermal_of_zone_unregister(struct thermal_zone_device *tz)
599 struct thermal_trip *trips = tz->trips;
600 struct thermal_zone_params *tzp = tz->tzp;
601 struct thermal_zone_device_ops *ops = tz->ops;
603 thermal_zone_device_disable(tz);
604 thermal_zone_device_unregister(tz);
609 EXPORT_SYMBOL_GPL(thermal_of_zone_unregister);
612 * thermal_of_zone_register - Register a thermal zone with device node
615 * The thermal_of_zone_register() parses a device tree given a device
616 * node sensor and identifier. It searches for the thermal zone
617 * associated to the couple sensor/id and retrieves all the thermal
618 * zone properties and registers new thermal zone with those
621 * @sensor: A device node pointer corresponding to the sensor in the device tree
622 * @id: An integer as sensor identifier
623 * @data: A private data to be stored in the thermal zone dedicated private area
624 * @ops: A set of thermal sensor ops
626 * Return: a valid thermal zone structure pointer on success.
627 * - EINVAL: if the device tree thermal description is malformed
628 * - ENOMEM: if one structure can not be allocated
629 * - Other negative errors are returned by the underlying called functions
631 struct thermal_zone_device *thermal_of_zone_register(struct device_node *sensor, int id, void *data,
632 const struct thermal_zone_device_ops *ops)
634 struct thermal_zone_device *tz;
635 struct thermal_trip *trips;
636 struct thermal_zone_params *tzp;
637 struct thermal_zone_device_ops *of_ops;
638 struct device_node *np;
643 of_ops = kmemdup(ops, sizeof(*ops), GFP_KERNEL);
645 return ERR_PTR(-ENOMEM);
647 np = of_thermal_zone_find(sensor, id);
649 if (PTR_ERR(np) != -ENODEV)
650 pr_err("Failed to find thermal zone for %pOFn id=%d\n", sensor, id);
654 trips = thermal_of_trips_init(np, &ntrips);
656 pr_err("Failed to find trip points for %pOFn id=%d\n", sensor, id);
657 return ERR_CAST(trips);
660 ret = thermal_of_monitor_init(np, &delay, &pdelay);
662 pr_err("Failed to initialize monitoring delays from %pOFn\n", np);
663 goto out_kfree_trips;
666 tzp = thermal_of_parameters_init(np);
669 pr_err("Failed to initialize parameter from %pOFn: %d\n", np, ret);
670 goto out_kfree_trips;
673 of_ops->get_trip_type = of_ops->get_trip_type ? : of_thermal_get_trip_type;
674 of_ops->get_trip_temp = of_ops->get_trip_temp ? : of_thermal_get_trip_temp;
675 of_ops->get_trip_hyst = of_ops->get_trip_hyst ? : of_thermal_get_trip_hyst;
676 of_ops->set_trip_hyst = of_ops->set_trip_hyst ? : of_thermal_set_trip_hyst;
677 of_ops->get_crit_temp = of_ops->get_crit_temp ? : of_thermal_get_crit_temp;
678 of_ops->bind = thermal_of_bind;
679 of_ops->unbind = thermal_of_unbind;
681 mask = GENMASK_ULL((ntrips) - 1, 0);
683 tz = thermal_zone_device_register_with_trips(np->name, trips, ntrips,
684 mask, data, of_ops, tzp,
688 pr_err("Failed to register thermal zone %pOFn: %d\n", np, ret);
692 ret = thermal_zone_device_enable(tz);
694 pr_err("Failed to enabled thermal zone '%s', id=%d: %d\n",
695 tz->type, tz->id, ret);
696 thermal_of_zone_unregister(tz);
709 EXPORT_SYMBOL_GPL(thermal_of_zone_register);
711 static void devm_thermal_of_zone_release(struct device *dev, void *res)
713 thermal_of_zone_unregister(*(struct thermal_zone_device **)res);
716 static int devm_thermal_of_zone_match(struct device *dev, void *res,
719 struct thermal_zone_device **r = res;
721 if (WARN_ON(!r || !*r))
728 * devm_thermal_of_zone_register - register a thermal tied with the sensor life cycle
730 * This function is the device version of the thermal_of_zone_register() function.
732 * @dev: a device structure pointer to sensor to be tied with the thermal zone OF life cycle
733 * @sensor_id: the sensor identifier
734 * @data: a pointer to a private data to be stored in the thermal zone 'devdata' field
735 * @ops: a pointer to the ops structure associated with the sensor
737 struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int sensor_id, void *data,
738 const struct thermal_zone_device_ops *ops)
740 struct thermal_zone_device **ptr, *tzd;
742 ptr = devres_alloc(devm_thermal_of_zone_release, sizeof(*ptr),
745 return ERR_PTR(-ENOMEM);
747 tzd = thermal_of_zone_register(dev->of_node, sensor_id, data, ops);
754 devres_add(dev, ptr);
758 EXPORT_SYMBOL_GPL(devm_thermal_of_zone_register);
761 * devm_thermal_of_zone_unregister - Resource managed version of
762 * thermal_of_zone_unregister().
763 * @dev: Device for which which resource was allocated.
764 * @tz: a pointer to struct thermal_zone where the sensor is registered.
766 * This function removes the sensor callbacks and private data from the
767 * thermal zone device registered with devm_thermal_zone_of_sensor_register()
768 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
769 * thermal zone device callbacks.
770 * Normally this function will not need to be called and the resource
771 * management code will ensure that the resource is freed.
773 void devm_thermal_of_zone_unregister(struct device *dev, struct thermal_zone_device *tz)
775 WARN_ON(devres_release(dev, devm_thermal_of_zone_release,
776 devm_thermal_of_zone_match, tz));
778 EXPORT_SYMBOL_GPL(devm_thermal_of_zone_unregister);