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>
14 #include <linux/slab.h>
15 #include <linux/thermal.h>
16 #include <linux/types.h>
17 #include <linux/string.h>
19 #include "thermal_core.h"
21 /*** functions parsing device tree nodes ***/
23 static int of_find_trip_id(struct device_node *np, struct device_node *trip)
25 struct device_node *trips;
26 struct device_node *t;
29 trips = of_get_child_by_name(np, "trips");
31 pr_err("Failed to find 'trips' node\n");
36 * Find the trip id point associated with the cooling device map
38 for_each_child_of_node(trips, t) {
55 * It maps 'enum thermal_trip_type' found in include/linux/thermal.h
56 * into the device tree binding of 'trip', property type.
58 static const char * const trip_types[] = {
59 [THERMAL_TRIP_ACTIVE] = "active",
60 [THERMAL_TRIP_PASSIVE] = "passive",
61 [THERMAL_TRIP_HOT] = "hot",
62 [THERMAL_TRIP_CRITICAL] = "critical",
66 * thermal_of_get_trip_type - Get phy mode for given device_node
67 * @np: Pointer to the given device_node
68 * @type: Pointer to resulting trip type
70 * The function gets trip type string from property 'type',
71 * and store its index in trip_types table in @type,
73 * Return: 0 on success, or errno in error case.
75 static int thermal_of_get_trip_type(struct device_node *np,
76 enum thermal_trip_type *type)
81 err = of_property_read_string(np, "type", &t);
85 for (i = 0; i < ARRAY_SIZE(trip_types); i++)
86 if (!strcasecmp(t, trip_types[i])) {
94 static int thermal_of_populate_trip(struct device_node *np,
95 struct thermal_trip *trip)
100 ret = of_property_read_u32(np, "temperature", &prop);
102 pr_err("missing temperature property\n");
105 trip->temperature = prop;
107 ret = of_property_read_u32(np, "hysteresis", &prop);
109 pr_err("missing hysteresis property\n");
112 trip->hysteresis = prop;
114 ret = thermal_of_get_trip_type(np, &trip->type);
116 pr_err("wrong trip type property\n");
123 static struct thermal_trip *thermal_of_trips_init(struct device_node *np, int *ntrips)
125 struct thermal_trip *tt;
126 struct device_node *trips, *trip;
129 trips = of_get_child_by_name(np, "trips");
131 pr_err("Failed to find 'trips' node\n");
132 return ERR_PTR(-EINVAL);
135 count = of_get_child_count(trips);
137 pr_err("No trip point defined\n");
139 goto out_of_node_put;
142 tt = kzalloc(sizeof(*tt) * count, GFP_KERNEL);
145 goto out_of_node_put;
151 for_each_child_of_node(trips, trip) {
152 ret = thermal_of_populate_trip(trip, &tt[count++]);
170 static struct device_node *of_thermal_zone_find(struct device_node *sensor, int id)
172 struct device_node *np, *tz;
173 struct of_phandle_args sensor_specs;
175 np = of_find_node_by_name(NULL, "thermal-zones");
177 pr_debug("No thermal zones description\n");
178 return ERR_PTR(-ENODEV);
182 * Search for each thermal zone, a defined sensor
183 * corresponding to the one passed as parameter
185 for_each_available_child_of_node(np, tz) {
189 count = of_count_phandle_with_args(tz, "thermal-sensors",
190 "#thermal-sensor-cells");
192 pr_err("%pOFn: missing thermal sensor\n", tz);
193 tz = ERR_PTR(-EINVAL);
197 for (i = 0; i < count; i++) {
201 ret = of_parse_phandle_with_args(tz, "thermal-sensors",
202 "#thermal-sensor-cells",
205 pr_err("%pOFn: Failed to read thermal-sensors cells: %d\n", tz, ret);
210 if ((sensor == sensor_specs.np) && id == (sensor_specs.args_count ?
211 sensor_specs.args[0] : 0)) {
212 pr_debug("sensor %pOFn id=%d belongs to %pOFn\n", sensor, id, tz);
217 tz = ERR_PTR(-ENODEV);
223 static int thermal_of_monitor_init(struct device_node *np, int *delay, int *pdelay)
227 ret = of_property_read_u32(np, "polling-delay-passive", pdelay);
229 pr_err("%pOFn: missing polling-delay-passive property\n", np);
233 ret = of_property_read_u32(np, "polling-delay", delay);
235 pr_err("%pOFn: missing polling-delay property\n", np);
242 static void thermal_of_parameters_init(struct device_node *np,
243 struct thermal_zone_params *tzp)
246 int ncoef = ARRAY_SIZE(coef);
249 tzp->no_hwmon = true;
251 if (!of_property_read_u32(np, "sustainable-power", &prop))
252 tzp->sustainable_power = prop;
255 * For now, the thermal framework supports only one sensor per
256 * thermal zone. Thus, we are considering only the first two
257 * values as slope and offset.
259 ret = of_property_read_u32_array(np, "coefficients", coef, ncoef);
265 tzp->slope = coef[0];
266 tzp->offset = coef[1];
269 static struct device_node *thermal_of_zone_get_by_name(struct thermal_zone_device *tz)
271 struct device_node *np, *tz_np;
273 np = of_find_node_by_name(NULL, "thermal-zones");
275 return ERR_PTR(-ENODEV);
277 tz_np = of_get_child_by_name(np, tz->type);
282 return ERR_PTR(-ENODEV);
287 static int __thermal_of_unbind(struct device_node *map_np, int index, int trip_id,
288 struct thermal_zone_device *tz, struct thermal_cooling_device *cdev)
290 struct of_phandle_args cooling_spec;
293 ret = of_parse_phandle_with_args(map_np, "cooling-device", "#cooling-cells",
294 index, &cooling_spec);
297 pr_err("Invalid cooling-device entry\n");
301 of_node_put(cooling_spec.np);
303 if (cooling_spec.args_count < 2) {
304 pr_err("wrong reference to cooling device, missing limits\n");
308 if (cooling_spec.np != cdev->np)
311 ret = thermal_zone_unbind_cooling_device(tz, trip_id, cdev);
313 pr_err("Failed to unbind '%s' with '%s': %d\n", tz->type, cdev->type, ret);
318 static int __thermal_of_bind(struct device_node *map_np, int index, int trip_id,
319 struct thermal_zone_device *tz, struct thermal_cooling_device *cdev)
321 struct of_phandle_args cooling_spec;
322 int ret, weight = THERMAL_WEIGHT_DEFAULT;
324 of_property_read_u32(map_np, "contribution", &weight);
326 ret = of_parse_phandle_with_args(map_np, "cooling-device", "#cooling-cells",
327 index, &cooling_spec);
330 pr_err("Invalid cooling-device entry\n");
334 of_node_put(cooling_spec.np);
336 if (cooling_spec.args_count < 2) {
337 pr_err("wrong reference to cooling device, missing limits\n");
341 if (cooling_spec.np != cdev->np)
344 ret = thermal_zone_bind_cooling_device(tz, trip_id, cdev, cooling_spec.args[1],
345 cooling_spec.args[0],
348 pr_err("Failed to bind '%s' with '%s': %d\n", tz->type, cdev->type, ret);
353 static int thermal_of_for_each_cooling_device(struct device_node *tz_np, struct device_node *map_np,
354 struct thermal_zone_device *tz, struct thermal_cooling_device *cdev,
355 int (*action)(struct device_node *, int, int,
356 struct thermal_zone_device *, struct thermal_cooling_device *))
358 struct device_node *tr_np;
359 int count, i, trip_id;
361 tr_np = of_parse_phandle(map_np, "trip", 0);
365 trip_id = of_find_trip_id(tz_np, tr_np);
369 count = of_count_phandle_with_args(map_np, "cooling-device", "#cooling-cells");
371 pr_err("Add a cooling_device property with at least one device\n");
376 * At this point, we don't want to bail out when there is an
377 * error, we will try to bind/unbind as many as possible
380 for (i = 0; i < count; i++)
381 action(map_np, i, trip_id, tz, cdev);
386 static int thermal_of_for_each_cooling_maps(struct thermal_zone_device *tz,
387 struct thermal_cooling_device *cdev,
388 int (*action)(struct device_node *, int, int,
389 struct thermal_zone_device *, struct thermal_cooling_device *))
391 struct device_node *tz_np, *cm_np, *child;
394 tz_np = thermal_of_zone_get_by_name(tz);
396 pr_err("Failed to get node tz by name\n");
397 return PTR_ERR(tz_np);
400 cm_np = of_get_child_by_name(tz_np, "cooling-maps");
404 for_each_child_of_node(cm_np, child) {
405 ret = thermal_of_for_each_cooling_device(tz_np, child, tz, cdev, action);
419 static int thermal_of_bind(struct thermal_zone_device *tz,
420 struct thermal_cooling_device *cdev)
422 return thermal_of_for_each_cooling_maps(tz, cdev, __thermal_of_bind);
425 static int thermal_of_unbind(struct thermal_zone_device *tz,
426 struct thermal_cooling_device *cdev)
428 return thermal_of_for_each_cooling_maps(tz, cdev, __thermal_of_unbind);
432 * thermal_of_zone_unregister - Cleanup the specific allocated ressources
434 * This function disables the thermal zone and frees the different
435 * ressources allocated specific to the thermal OF.
437 * @tz: a pointer to the thermal zone structure
439 static void thermal_of_zone_unregister(struct thermal_zone_device *tz)
441 struct thermal_trip *trips = tz->trips;
442 struct thermal_zone_device_ops *ops = tz->ops;
444 thermal_zone_device_disable(tz);
445 thermal_zone_device_unregister(tz);
451 * thermal_of_zone_register - Register a thermal zone with device node
454 * The thermal_of_zone_register() parses a device tree given a device
455 * node sensor and identifier. It searches for the thermal zone
456 * associated to the couple sensor/id and retrieves all the thermal
457 * zone properties and registers new thermal zone with those
460 * @sensor: A device node pointer corresponding to the sensor in the device tree
461 * @id: An integer as sensor identifier
462 * @data: A private data to be stored in the thermal zone dedicated private area
463 * @ops: A set of thermal sensor ops
465 * Return: a valid thermal zone structure pointer on success.
466 * - EINVAL: if the device tree thermal description is malformed
467 * - ENOMEM: if one structure can not be allocated
468 * - Other negative errors are returned by the underlying called functions
470 static struct thermal_zone_device *thermal_of_zone_register(struct device_node *sensor, int id, void *data,
471 const struct thermal_zone_device_ops *ops)
473 struct thermal_zone_device *tz;
474 struct thermal_trip *trips;
475 struct thermal_zone_params tzp = {};
476 struct thermal_zone_device_ops *of_ops;
477 struct device_node *np;
482 of_ops = kmemdup(ops, sizeof(*ops), GFP_KERNEL);
484 return ERR_PTR(-ENOMEM);
486 np = of_thermal_zone_find(sensor, id);
488 if (PTR_ERR(np) != -ENODEV)
489 pr_err("Failed to find thermal zone for %pOFn id=%d\n", sensor, id);
491 goto out_kfree_of_ops;
494 trips = thermal_of_trips_init(np, &ntrips);
496 pr_err("Failed to find trip points for %pOFn id=%d\n", sensor, id);
497 ret = PTR_ERR(trips);
498 goto out_kfree_of_ops;
501 ret = thermal_of_monitor_init(np, &delay, &pdelay);
503 pr_err("Failed to initialize monitoring delays from %pOFn\n", np);
504 goto out_kfree_trips;
507 thermal_of_parameters_init(np, &tzp);
509 of_ops->bind = thermal_of_bind;
510 of_ops->unbind = thermal_of_unbind;
512 mask = GENMASK_ULL((ntrips) - 1, 0);
514 tz = thermal_zone_device_register_with_trips(np->name, trips, ntrips,
515 mask, data, of_ops, &tzp,
519 pr_err("Failed to register thermal zone %pOFn: %d\n", np, ret);
520 goto out_kfree_trips;
523 ret = thermal_zone_device_enable(tz);
525 pr_err("Failed to enabled thermal zone '%s', id=%d: %d\n",
526 tz->type, tz->id, ret);
527 thermal_of_zone_unregister(tz);
541 static void devm_thermal_of_zone_release(struct device *dev, void *res)
543 thermal_of_zone_unregister(*(struct thermal_zone_device **)res);
546 static int devm_thermal_of_zone_match(struct device *dev, void *res,
549 struct thermal_zone_device **r = res;
551 if (WARN_ON(!r || !*r))
558 * devm_thermal_of_zone_register - register a thermal tied with the sensor life cycle
560 * This function is the device version of the thermal_of_zone_register() function.
562 * @dev: a device structure pointer to sensor to be tied with the thermal zone OF life cycle
563 * @sensor_id: the sensor identifier
564 * @data: a pointer to a private data to be stored in the thermal zone 'devdata' field
565 * @ops: a pointer to the ops structure associated with the sensor
567 struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int sensor_id, void *data,
568 const struct thermal_zone_device_ops *ops)
570 struct thermal_zone_device **ptr, *tzd;
572 ptr = devres_alloc(devm_thermal_of_zone_release, sizeof(*ptr),
575 return ERR_PTR(-ENOMEM);
577 tzd = thermal_of_zone_register(dev->of_node, sensor_id, data, ops);
584 devres_add(dev, ptr);
588 EXPORT_SYMBOL_GPL(devm_thermal_of_zone_register);
591 * devm_thermal_of_zone_unregister - Resource managed version of
592 * thermal_of_zone_unregister().
593 * @dev: Device for which which resource was allocated.
594 * @tz: a pointer to struct thermal_zone where the sensor is registered.
596 * This function removes the sensor callbacks and private data from the
597 * thermal zone device registered with devm_thermal_zone_of_sensor_register()
598 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
599 * thermal zone device callbacks.
600 * Normally this function will not need to be called and the resource
601 * management code will ensure that the resource is freed.
603 void devm_thermal_of_zone_unregister(struct device *dev, struct thermal_zone_device *tz)
605 WARN_ON(devres_release(dev, devm_thermal_of_zone_release,
606 devm_thermal_of_zone_match, tz));
608 EXPORT_SYMBOL_GPL(devm_thermal_of_zone_unregister);