Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / thermal / thermal_of.c
blobfab11b98ca4952d23d0232998433bd0650b53d24
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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>
7 */
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/err.h>
12 #include <linux/export.h>
13 #include <linux/of.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 ***/
24 * It maps 'enum thermal_trip_type' found in include/linux/thermal.h
25 * into the device tree binding of 'trip', property type.
27 static const char * const trip_types[] = {
28 [THERMAL_TRIP_ACTIVE] = "active",
29 [THERMAL_TRIP_PASSIVE] = "passive",
30 [THERMAL_TRIP_HOT] = "hot",
31 [THERMAL_TRIP_CRITICAL] = "critical",
34 /**
35 * thermal_of_get_trip_type - Get phy mode for given device_node
36 * @np: Pointer to the given device_node
37 * @type: Pointer to resulting trip type
39 * The function gets trip type string from property 'type',
40 * and store its index in trip_types table in @type,
42 * Return: 0 on success, or errno in error case.
44 static int thermal_of_get_trip_type(struct device_node *np,
45 enum thermal_trip_type *type)
47 const char *t;
48 int err, i;
50 err = of_property_read_string(np, "type", &t);
51 if (err < 0)
52 return err;
54 for (i = 0; i < ARRAY_SIZE(trip_types); i++)
55 if (!strcasecmp(t, trip_types[i])) {
56 *type = i;
57 return 0;
60 return -ENODEV;
63 static int thermal_of_populate_trip(struct device_node *np,
64 struct thermal_trip *trip)
66 int prop;
67 int ret;
69 ret = of_property_read_u32(np, "temperature", &prop);
70 if (ret < 0) {
71 pr_err("missing temperature property\n");
72 return ret;
74 trip->temperature = prop;
76 ret = of_property_read_u32(np, "hysteresis", &prop);
77 if (ret < 0) {
78 pr_err("missing hysteresis property\n");
79 return ret;
81 trip->hysteresis = prop;
83 ret = thermal_of_get_trip_type(np, &trip->type);
84 if (ret < 0) {
85 pr_err("wrong trip type property\n");
86 return ret;
89 trip->flags = THERMAL_TRIP_FLAG_RW_TEMP;
91 trip->priv = np;
93 return 0;
96 static struct thermal_trip *thermal_of_trips_init(struct device_node *np, int *ntrips)
98 int ret, count;
100 *ntrips = 0;
102 struct device_node *trips __free(device_node) = of_get_child_by_name(np, "trips");
103 if (!trips)
104 return NULL;
106 count = of_get_child_count(trips);
107 if (!count)
108 return NULL;
110 struct thermal_trip *tt __free(kfree) = kzalloc(sizeof(*tt) * count, GFP_KERNEL);
111 if (!tt)
112 return ERR_PTR(-ENOMEM);
114 count = 0;
115 for_each_child_of_node_scoped(trips, trip) {
116 ret = thermal_of_populate_trip(trip, &tt[count++]);
117 if (ret)
118 return ERR_PTR(ret);
121 *ntrips = count;
123 return no_free_ptr(tt);
126 static struct device_node *of_thermal_zone_find(struct device_node *sensor, int id)
128 struct of_phandle_args sensor_specs;
130 struct device_node *np __free(device_node) = of_find_node_by_name(NULL, "thermal-zones");
131 if (!np) {
132 pr_debug("No thermal zones description\n");
133 return ERR_PTR(-ENODEV);
137 * Search for each thermal zone, a defined sensor
138 * corresponding to the one passed as parameter
140 for_each_available_child_of_node_scoped(np, child) {
142 int count, i;
144 count = of_count_phandle_with_args(child, "thermal-sensors",
145 "#thermal-sensor-cells");
146 if (count <= 0) {
147 pr_err("%pOFn: missing thermal sensor\n", child);
148 return ERR_PTR(-EINVAL);
151 for (i = 0; i < count; i++) {
153 int ret;
155 ret = of_parse_phandle_with_args(child, "thermal-sensors",
156 "#thermal-sensor-cells",
157 i, &sensor_specs);
158 if (ret < 0) {
159 pr_err("%pOFn: Failed to read thermal-sensors cells: %d\n", child, ret);
160 return ERR_PTR(ret);
163 if ((sensor == sensor_specs.np) && id == (sensor_specs.args_count ?
164 sensor_specs.args[0] : 0)) {
165 pr_debug("sensor %pOFn id=%d belongs to %pOFn\n", sensor, id, child);
166 return no_free_ptr(child);
171 return ERR_PTR(-ENODEV);
174 static int thermal_of_monitor_init(struct device_node *np, int *delay, int *pdelay)
176 int ret;
178 ret = of_property_read_u32(np, "polling-delay-passive", pdelay);
179 if (ret == -EINVAL) {
180 *pdelay = 0;
181 } else if (ret < 0) {
182 pr_err("%pOFn: Couldn't get polling-delay-passive: %d\n", np, ret);
183 return ret;
186 ret = of_property_read_u32(np, "polling-delay", delay);
187 if (ret == -EINVAL) {
188 *delay = 0;
189 } else if (ret < 0) {
190 pr_err("%pOFn: Couldn't get polling-delay: %d\n", np, ret);
191 return ret;
194 return 0;
197 static void thermal_of_parameters_init(struct device_node *np,
198 struct thermal_zone_params *tzp)
200 int coef[2];
201 int ncoef = ARRAY_SIZE(coef);
202 int prop, ret;
204 tzp->no_hwmon = true;
206 if (!of_property_read_u32(np, "sustainable-power", &prop))
207 tzp->sustainable_power = prop;
210 * For now, the thermal framework supports only one sensor per
211 * thermal zone. Thus, we are considering only the first two
212 * values as slope and offset.
214 ret = of_property_read_u32_array(np, "coefficients", coef, ncoef);
215 if (ret) {
216 coef[0] = 1;
217 coef[1] = 0;
220 tzp->slope = coef[0];
221 tzp->offset = coef[1];
224 static struct device_node *thermal_of_zone_get_by_name(struct thermal_zone_device *tz)
226 struct device_node *np, *tz_np;
228 np = of_find_node_by_name(NULL, "thermal-zones");
229 if (!np)
230 return ERR_PTR(-ENODEV);
232 tz_np = of_get_child_by_name(np, tz->type);
234 of_node_put(np);
236 if (!tz_np)
237 return ERR_PTR(-ENODEV);
239 return tz_np;
242 static bool thermal_of_get_cooling_spec(struct device_node *map_np, int index,
243 struct thermal_cooling_device *cdev,
244 struct cooling_spec *c)
246 struct of_phandle_args cooling_spec;
247 int ret, weight = THERMAL_WEIGHT_DEFAULT;
249 of_property_read_u32(map_np, "contribution", &weight);
251 ret = of_parse_phandle_with_args(map_np, "cooling-device", "#cooling-cells",
252 index, &cooling_spec);
254 if (ret < 0) {
255 pr_err("Invalid cooling-device entry\n");
256 return false;
259 of_node_put(cooling_spec.np);
261 if (cooling_spec.args_count < 2) {
262 pr_err("wrong reference to cooling device, missing limits\n");
263 return false;
266 if (cooling_spec.np != cdev->np)
267 return false;
269 c->lower = cooling_spec.args[0];
270 c->upper = cooling_spec.args[1];
271 c->weight = weight;
273 return true;
276 static bool thermal_of_should_bind(struct thermal_zone_device *tz,
277 const struct thermal_trip *trip,
278 struct thermal_cooling_device *cdev,
279 struct cooling_spec *c)
281 struct device_node *tz_np, *cm_np;
282 bool result = false;
284 tz_np = thermal_of_zone_get_by_name(tz);
285 if (IS_ERR(tz_np)) {
286 pr_err("Failed to get node tz by name\n");
287 return false;
290 cm_np = of_get_child_by_name(tz_np, "cooling-maps");
291 if (!cm_np)
292 goto out;
294 /* Look up the trip and the cdev in the cooling maps. */
295 for_each_child_of_node_scoped(cm_np, child) {
296 struct device_node *tr_np;
297 int count, i;
299 tr_np = of_parse_phandle(child, "trip", 0);
300 if (tr_np != trip->priv)
301 continue;
303 /* The trip has been found, look up the cdev. */
304 count = of_count_phandle_with_args(child, "cooling-device", "#cooling-cells");
305 if (count <= 0)
306 pr_err("Add a cooling_device property with at least one device\n");
308 for (i = 0; i < count; i++) {
309 result = thermal_of_get_cooling_spec(child, i, cdev, c);
310 if (result)
311 break;
314 break;
317 of_node_put(cm_np);
318 out:
319 of_node_put(tz_np);
321 return result;
325 * thermal_of_zone_unregister - Cleanup the specific allocated ressources
327 * This function disables the thermal zone and frees the different
328 * ressources allocated specific to the thermal OF.
330 * @tz: a pointer to the thermal zone structure
332 static void thermal_of_zone_unregister(struct thermal_zone_device *tz)
334 thermal_zone_device_disable(tz);
335 thermal_zone_device_unregister(tz);
339 * thermal_of_zone_register - Register a thermal zone with device node
340 * sensor
342 * The thermal_of_zone_register() parses a device tree given a device
343 * node sensor and identifier. It searches for the thermal zone
344 * associated to the couple sensor/id and retrieves all the thermal
345 * zone properties and registers new thermal zone with those
346 * properties.
348 * @sensor: A device node pointer corresponding to the sensor in the device tree
349 * @id: An integer as sensor identifier
350 * @data: A private data to be stored in the thermal zone dedicated private area
351 * @ops: A set of thermal sensor ops
353 * Return: a valid thermal zone structure pointer on success.
354 * - EINVAL: if the device tree thermal description is malformed
355 * - ENOMEM: if one structure can not be allocated
356 * - Other negative errors are returned by the underlying called functions
358 static struct thermal_zone_device *thermal_of_zone_register(struct device_node *sensor, int id, void *data,
359 const struct thermal_zone_device_ops *ops)
361 struct thermal_zone_device_ops of_ops = *ops;
362 struct thermal_zone_device *tz;
363 struct thermal_trip *trips;
364 struct thermal_zone_params tzp = {};
365 struct device_node *np;
366 const char *action;
367 int delay, pdelay;
368 int ntrips;
369 int ret;
371 np = of_thermal_zone_find(sensor, id);
372 if (IS_ERR(np)) {
373 if (PTR_ERR(np) != -ENODEV)
374 pr_err("Failed to find thermal zone for %pOFn id=%d\n", sensor, id);
375 return ERR_CAST(np);
378 trips = thermal_of_trips_init(np, &ntrips);
379 if (IS_ERR(trips)) {
380 pr_err("Failed to parse trip points for %pOFn id=%d\n", sensor, id);
381 ret = PTR_ERR(trips);
382 goto out_of_node_put;
385 if (!trips)
386 pr_info("No trip points found for %pOFn id=%d\n", sensor, id);
388 ret = thermal_of_monitor_init(np, &delay, &pdelay);
389 if (ret) {
390 pr_err("Failed to initialize monitoring delays from %pOFn\n", np);
391 goto out_kfree_trips;
394 thermal_of_parameters_init(np, &tzp);
396 of_ops.should_bind = thermal_of_should_bind;
398 ret = of_property_read_string(np, "critical-action", &action);
399 if (!ret)
400 if (!of_ops.critical && !strcasecmp(action, "reboot"))
401 of_ops.critical = thermal_zone_device_critical_reboot;
403 tz = thermal_zone_device_register_with_trips(np->name, trips, ntrips,
404 data, &of_ops, &tzp,
405 pdelay, delay);
406 if (IS_ERR(tz)) {
407 ret = PTR_ERR(tz);
408 pr_err("Failed to register thermal zone %pOFn: %d\n", np, ret);
409 goto out_kfree_trips;
412 of_node_put(np);
413 kfree(trips);
415 ret = thermal_zone_device_enable(tz);
416 if (ret) {
417 pr_err("Failed to enabled thermal zone '%s', id=%d: %d\n",
418 tz->type, tz->id, ret);
419 thermal_of_zone_unregister(tz);
420 return ERR_PTR(ret);
423 return tz;
425 out_kfree_trips:
426 kfree(trips);
427 out_of_node_put:
428 of_node_put(np);
430 return ERR_PTR(ret);
433 static void devm_thermal_of_zone_release(struct device *dev, void *res)
435 thermal_of_zone_unregister(*(struct thermal_zone_device **)res);
438 static int devm_thermal_of_zone_match(struct device *dev, void *res,
439 void *data)
441 struct thermal_zone_device **r = res;
443 if (WARN_ON(!r || !*r))
444 return 0;
446 return *r == data;
450 * devm_thermal_of_zone_register - register a thermal tied with the sensor life cycle
452 * This function is the device version of the thermal_of_zone_register() function.
454 * @dev: a device structure pointer to sensor to be tied with the thermal zone OF life cycle
455 * @sensor_id: the sensor identifier
456 * @data: a pointer to a private data to be stored in the thermal zone 'devdata' field
457 * @ops: a pointer to the ops structure associated with the sensor
459 struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int sensor_id, void *data,
460 const struct thermal_zone_device_ops *ops)
462 struct thermal_zone_device **ptr, *tzd;
464 ptr = devres_alloc(devm_thermal_of_zone_release, sizeof(*ptr),
465 GFP_KERNEL);
466 if (!ptr)
467 return ERR_PTR(-ENOMEM);
469 tzd = thermal_of_zone_register(dev->of_node, sensor_id, data, ops);
470 if (IS_ERR(tzd)) {
471 devres_free(ptr);
472 return tzd;
475 *ptr = tzd;
476 devres_add(dev, ptr);
478 return tzd;
480 EXPORT_SYMBOL_GPL(devm_thermal_of_zone_register);
483 * devm_thermal_of_zone_unregister - Resource managed version of
484 * thermal_of_zone_unregister().
485 * @dev: Device for which which resource was allocated.
486 * @tz: a pointer to struct thermal_zone where the sensor is registered.
488 * This function removes the sensor callbacks and private data from the
489 * thermal zone device registered with devm_thermal_zone_of_sensor_register()
490 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
491 * thermal zone device callbacks.
492 * Normally this function will not need to be called and the resource
493 * management code will ensure that the resource is freed.
495 void devm_thermal_of_zone_unregister(struct device *dev, struct thermal_zone_device *tz)
497 WARN_ON(devres_release(dev, devm_thermal_of_zone_release,
498 devm_thermal_of_zone_match, tz));
500 EXPORT_SYMBOL_GPL(devm_thermal_of_zone_unregister);