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/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 /*** functions parsing device tree nodes ***/
24 static int of_find_trip_id(struct device_node *np, struct device_node *trip)
26 struct device_node *trips;
27 struct device_node *t;
30 trips = of_get_child_by_name(np, "trips");
32 pr_err("Failed to find 'trips' node\n");
37 * Find the trip id point associated with the cooling device map
39 for_each_child_of_node(trips, t) {
54 * It maps 'enum thermal_trip_type' found in include/linux/thermal.h
55 * into the device tree binding of 'trip', property type.
57 static const char * const trip_types[] = {
58 [THERMAL_TRIP_ACTIVE] = "active",
59 [THERMAL_TRIP_PASSIVE] = "passive",
60 [THERMAL_TRIP_HOT] = "hot",
61 [THERMAL_TRIP_CRITICAL] = "critical",
65 * thermal_of_get_trip_type - Get phy mode for given device_node
66 * @np: Pointer to the given device_node
67 * @type: Pointer to resulting trip type
69 * The function gets trip type string from property 'type',
70 * and store its index in trip_types table in @type,
72 * Return: 0 on success, or errno in error case.
74 static int thermal_of_get_trip_type(struct device_node *np,
75 enum thermal_trip_type *type)
80 err = of_property_read_string(np, "type", &t);
84 for (i = 0; i < ARRAY_SIZE(trip_types); i++)
85 if (!strcasecmp(t, trip_types[i])) {
93 static int thermal_of_populate_trip(struct device_node *np,
94 struct thermal_trip *trip)
99 ret = of_property_read_u32(np, "temperature", &prop);
101 pr_err("missing temperature property\n");
104 trip->temperature = prop;
106 ret = of_property_read_u32(np, "hysteresis", &prop);
108 pr_err("missing hysteresis property\n");
111 trip->hysteresis = prop;
113 ret = thermal_of_get_trip_type(np, &trip->type);
115 pr_err("wrong trip type property\n");
122 static struct thermal_trip *thermal_of_trips_init(struct device_node *np, int *ntrips)
124 struct thermal_trip *tt;
125 struct device_node *trips, *trip;
128 trips = of_get_child_by_name(np, "trips");
130 pr_err("Failed to find 'trips' node\n");
131 return ERR_PTR(-EINVAL);
134 count = of_get_child_count(trips);
136 pr_err("No trip point defined\n");
138 goto out_of_node_put;
141 tt = kzalloc(sizeof(*tt) * count, GFP_KERNEL);
144 goto out_of_node_put;
150 for_each_child_of_node(trips, trip) {
151 ret = thermal_of_populate_trip(trip, &tt[count++]);
169 static struct device_node *of_thermal_zone_find(struct device_node *sensor, int id)
171 struct device_node *np, *tz;
172 struct of_phandle_args sensor_specs;
174 np = of_find_node_by_name(NULL, "thermal-zones");
176 pr_debug("No thermal zones description\n");
177 return ERR_PTR(-ENODEV);
181 * Search for each thermal zone, a defined sensor
182 * corresponding to the one passed as parameter
184 for_each_available_child_of_node(np, tz) {
188 count = of_count_phandle_with_args(tz, "thermal-sensors",
189 "#thermal-sensor-cells");
191 pr_err("%pOFn: missing thermal sensor\n", tz);
192 tz = ERR_PTR(-EINVAL);
196 for (i = 0; i < count; i++) {
200 ret = of_parse_phandle_with_args(tz, "thermal-sensors",
201 "#thermal-sensor-cells",
204 pr_err("%pOFn: Failed to read thermal-sensors cells: %d\n", tz, ret);
209 if ((sensor == sensor_specs.np) && id == (sensor_specs.args_count ?
210 sensor_specs.args[0] : 0)) {
211 pr_debug("sensor %pOFn id=%d belongs to %pOFn\n", sensor, id, tz);
216 tz = ERR_PTR(-ENODEV);
222 static int thermal_of_monitor_init(struct device_node *np, int *delay, int *pdelay)
226 ret = of_property_read_u32(np, "polling-delay-passive", pdelay);
228 pr_err("%pOFn: missing polling-delay-passive property\n", np);
232 ret = of_property_read_u32(np, "polling-delay", delay);
234 pr_err("%pOFn: missing polling-delay property\n", np);
241 static struct thermal_zone_params *thermal_of_parameters_init(struct device_node *np)
243 struct thermal_zone_params *tzp;
245 int ncoef = ARRAY_SIZE(coef);
248 tzp = kzalloc(sizeof(*tzp), GFP_KERNEL);
250 return ERR_PTR(-ENOMEM);
252 tzp->no_hwmon = true;
254 if (!of_property_read_u32(np, "sustainable-power", &prop))
255 tzp->sustainable_power = prop;
258 * For now, the thermal framework supports only one sensor per
259 * thermal zone. Thus, we are considering only the first two
260 * values as slope and offset.
262 ret = of_property_read_u32_array(np, "coefficients", coef, ncoef);
268 tzp->slope = coef[0];
269 tzp->offset = coef[1];
274 static struct device_node *thermal_of_zone_get_by_name(struct thermal_zone_device *tz)
276 struct device_node *np, *tz_np;
278 np = of_find_node_by_name(NULL, "thermal-zones");
280 return ERR_PTR(-ENODEV);
282 tz_np = of_get_child_by_name(np, tz->type);
287 return ERR_PTR(-ENODEV);
292 static int __thermal_of_unbind(struct device_node *map_np, int index, int trip_id,
293 struct thermal_zone_device *tz, struct thermal_cooling_device *cdev)
295 struct of_phandle_args cooling_spec;
298 ret = of_parse_phandle_with_args(map_np, "cooling-device", "#cooling-cells",
299 index, &cooling_spec);
301 of_node_put(cooling_spec.np);
304 pr_err("Invalid cooling-device entry\n");
308 if (cooling_spec.args_count < 2) {
309 pr_err("wrong reference to cooling device, missing limits\n");
313 if (cooling_spec.np != cdev->np)
316 ret = thermal_zone_unbind_cooling_device(tz, trip_id, cdev);
318 pr_err("Failed to unbind '%s' with '%s': %d\n", tz->type, cdev->type, ret);
323 static int __thermal_of_bind(struct device_node *map_np, int index, int trip_id,
324 struct thermal_zone_device *tz, struct thermal_cooling_device *cdev)
326 struct of_phandle_args cooling_spec;
327 int ret, weight = THERMAL_WEIGHT_DEFAULT;
329 of_property_read_u32(map_np, "contribution", &weight);
331 ret = of_parse_phandle_with_args(map_np, "cooling-device", "#cooling-cells",
332 index, &cooling_spec);
334 of_node_put(cooling_spec.np);
337 pr_err("Invalid cooling-device entry\n");
341 if (cooling_spec.args_count < 2) {
342 pr_err("wrong reference to cooling device, missing limits\n");
346 if (cooling_spec.np != cdev->np)
349 ret = thermal_zone_bind_cooling_device(tz, trip_id, cdev, cooling_spec.args[1],
350 cooling_spec.args[0],
353 pr_err("Failed to bind '%s' with '%s': %d\n", tz->type, cdev->type, ret);
358 static int thermal_of_for_each_cooling_device(struct device_node *tz_np, struct device_node *map_np,
359 struct thermal_zone_device *tz, struct thermal_cooling_device *cdev,
360 int (*action)(struct device_node *, int, int,
361 struct thermal_zone_device *, struct thermal_cooling_device *))
363 struct device_node *tr_np;
364 int count, i, trip_id;
366 tr_np = of_parse_phandle(map_np, "trip", 0);
370 trip_id = of_find_trip_id(tz_np, tr_np);
374 count = of_count_phandle_with_args(map_np, "cooling-device", "#cooling-cells");
376 pr_err("Add a cooling_device property with at least one device\n");
381 * At this point, we don't want to bail out when there is an
382 * error, we will try to bind/unbind as many as possible
385 for (i = 0; i < count; i++)
386 action(map_np, i, trip_id, tz, cdev);
391 static int thermal_of_for_each_cooling_maps(struct thermal_zone_device *tz,
392 struct thermal_cooling_device *cdev,
393 int (*action)(struct device_node *, int, int,
394 struct thermal_zone_device *, struct thermal_cooling_device *))
396 struct device_node *tz_np, *cm_np, *child;
399 tz_np = thermal_of_zone_get_by_name(tz);
401 pr_err("Failed to get node tz by name\n");
402 return PTR_ERR(tz_np);
405 cm_np = of_get_child_by_name(tz_np, "cooling-maps");
409 for_each_child_of_node(cm_np, child) {
410 ret = thermal_of_for_each_cooling_device(tz_np, child, tz, cdev, action);
422 static int thermal_of_bind(struct thermal_zone_device *tz,
423 struct thermal_cooling_device *cdev)
425 return thermal_of_for_each_cooling_maps(tz, cdev, __thermal_of_bind);
428 static int thermal_of_unbind(struct thermal_zone_device *tz,
429 struct thermal_cooling_device *cdev)
431 return thermal_of_for_each_cooling_maps(tz, cdev, __thermal_of_unbind);
435 * thermal_of_zone_unregister - Cleanup the specific allocated ressources
437 * This function disables the thermal zone and frees the different
438 * ressources allocated specific to the thermal OF.
440 * @tz: a pointer to the thermal zone structure
442 static void thermal_of_zone_unregister(struct thermal_zone_device *tz)
444 struct thermal_trip *trips = tz->trips;
445 struct thermal_zone_params *tzp = tz->tzp;
446 struct thermal_zone_device_ops *ops = tz->ops;
448 thermal_zone_device_disable(tz);
449 thermal_zone_device_unregister(tz);
456 * thermal_of_zone_register - Register a thermal zone with device node
459 * The thermal_of_zone_register() parses a device tree given a device
460 * node sensor and identifier. It searches for the thermal zone
461 * associated to the couple sensor/id and retrieves all the thermal
462 * zone properties and registers new thermal zone with those
465 * @sensor: A device node pointer corresponding to the sensor in the device tree
466 * @id: An integer as sensor identifier
467 * @data: A private data to be stored in the thermal zone dedicated private area
468 * @ops: A set of thermal sensor ops
470 * Return: a valid thermal zone structure pointer on success.
471 * - EINVAL: if the device tree thermal description is malformed
472 * - ENOMEM: if one structure can not be allocated
473 * - Other negative errors are returned by the underlying called functions
475 static struct thermal_zone_device *thermal_of_zone_register(struct device_node *sensor, int id, void *data,
476 const struct thermal_zone_device_ops *ops)
478 struct thermal_zone_device *tz;
479 struct thermal_trip *trips;
480 struct thermal_zone_params *tzp;
481 struct thermal_zone_device_ops *of_ops;
482 struct device_node *np;
487 of_ops = kmemdup(ops, sizeof(*ops), GFP_KERNEL);
489 return ERR_PTR(-ENOMEM);
491 np = of_thermal_zone_find(sensor, id);
493 if (PTR_ERR(np) != -ENODEV)
494 pr_err("Failed to find thermal zone for %pOFn id=%d\n", sensor, id);
496 goto out_kfree_of_ops;
499 trips = thermal_of_trips_init(np, &ntrips);
501 pr_err("Failed to find trip points for %pOFn id=%d\n", sensor, id);
502 ret = PTR_ERR(trips);
503 goto out_kfree_of_ops;
506 ret = thermal_of_monitor_init(np, &delay, &pdelay);
508 pr_err("Failed to initialize monitoring delays from %pOFn\n", np);
509 goto out_kfree_trips;
512 tzp = thermal_of_parameters_init(np);
515 pr_err("Failed to initialize parameter from %pOFn: %d\n", np, ret);
516 goto out_kfree_trips;
519 of_ops->bind = thermal_of_bind;
520 of_ops->unbind = thermal_of_unbind;
522 mask = GENMASK_ULL((ntrips) - 1, 0);
524 tz = thermal_zone_device_register_with_trips(np->name, trips, ntrips,
525 mask, data, of_ops, tzp,
529 pr_err("Failed to register thermal zone %pOFn: %d\n", np, ret);
533 ret = thermal_zone_device_enable(tz);
535 pr_err("Failed to enabled thermal zone '%s', id=%d: %d\n",
536 tz->type, tz->id, ret);
537 thermal_of_zone_unregister(tz);
553 static void devm_thermal_of_zone_release(struct device *dev, void *res)
555 thermal_of_zone_unregister(*(struct thermal_zone_device **)res);
558 static int devm_thermal_of_zone_match(struct device *dev, void *res,
561 struct thermal_zone_device **r = res;
563 if (WARN_ON(!r || !*r))
570 * devm_thermal_of_zone_register - register a thermal tied with the sensor life cycle
572 * This function is the device version of the thermal_of_zone_register() function.
574 * @dev: a device structure pointer to sensor to be tied with the thermal zone OF life cycle
575 * @sensor_id: the sensor identifier
576 * @data: a pointer to a private data to be stored in the thermal zone 'devdata' field
577 * @ops: a pointer to the ops structure associated with the sensor
579 struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int sensor_id, void *data,
580 const struct thermal_zone_device_ops *ops)
582 struct thermal_zone_device **ptr, *tzd;
584 ptr = devres_alloc(devm_thermal_of_zone_release, sizeof(*ptr),
587 return ERR_PTR(-ENOMEM);
589 tzd = thermal_of_zone_register(dev->of_node, sensor_id, data, ops);
596 devres_add(dev, ptr);
600 EXPORT_SYMBOL_GPL(devm_thermal_of_zone_register);
603 * devm_thermal_of_zone_unregister - Resource managed version of
604 * thermal_of_zone_unregister().
605 * @dev: Device for which which resource was allocated.
606 * @tz: a pointer to struct thermal_zone where the sensor is registered.
608 * This function removes the sensor callbacks and private data from the
609 * thermal zone device registered with devm_thermal_zone_of_sensor_register()
610 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
611 * thermal zone device callbacks.
612 * Normally this function will not need to be called and the resource
613 * management code will ensure that the resource is freed.
615 void devm_thermal_of_zone_unregister(struct device *dev, struct thermal_zone_device *tz)
617 WARN_ON(devres_release(dev, devm_thermal_of_zone_release,
618 devm_thermal_of_zone_match, tz));
620 EXPORT_SYMBOL_GPL(devm_thermal_of_zone_unregister);