1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * of-thermal.c - Generic Thermal Management device tree support.
4 *
5 * Copyright (C) 2013 Texas Instruments
6 * Copyright (C) 2013 Eduardo Valentin <eduardo.valentin@ti.com>
7 */
8
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
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>
18
19 #include "thermal_core.h"
20
21 /*** functions parsing device tree nodes ***/
22
23 /*
24 * It maps 'enum thermal_trip_type' found in include/linux/thermal.h
25 * into the device tree binding of 'trip', property type.
26 */
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",
32 };
33
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
38 *
39 * The function gets trip type string from property 'type',
40 * and store its index in trip_types table in @type,
41 *
42 * Return: 0 on success, or errno in error case.
43 */
thermal_of_get_trip_type(struct device_node * np,enum thermal_trip_type * type)44 static int thermal_of_get_trip_type(struct device_node *np,
45 enum thermal_trip_type *type)
46 {
47 const char *t;
48 int err, i;
49
50 err = of_property_read_string(np, "type", &t);
51 if (err < 0)
52 return err;
53
54 for (i = 0; i < ARRAY_SIZE(trip_types); i++)
55 if (!strcasecmp(t, trip_types[i])) {
56 *type = i;
57 return 0;
58 }
59
60 return -ENODEV;
61 }
62
thermal_of_populate_trip(struct device_node * np,struct thermal_trip * trip)63 static int thermal_of_populate_trip(struct device_node *np,
64 struct thermal_trip *trip)
65 {
66 int prop;
67 int ret;
68
69 ret = of_property_read_u32(np, "temperature", &prop);
70 if (ret < 0) {
71 pr_err("missing temperature property\n");
72 return ret;
73 }
74 trip->temperature = prop;
75
76 ret = of_property_read_u32(np, "hysteresis", &prop);
77 if (ret < 0) {
78 pr_err("missing hysteresis property\n");
79 return ret;
80 }
81 trip->hysteresis = prop;
82
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;
87 }
88
89 trip->flags = THERMAL_TRIP_FLAG_RW_TEMP;
90
91 trip->priv = np;
92
93 return 0;
94 }
95
thermal_of_trips_init(struct device_node * np,int * ntrips)96 static struct thermal_trip *thermal_of_trips_init(struct device_node *np, int *ntrips)
97 {
98 int ret, count;
99
100 *ntrips = 0;
101
102 struct device_node *trips __free(device_node) = of_get_child_by_name(np, "trips");
103 if (!trips)
104 return NULL;
105
106 count = of_get_child_count(trips);
107 if (!count)
108 return NULL;
109
110 struct thermal_trip *tt __free(kfree) = kzalloc_objs(*tt, count);
111 if (!tt)
112 return ERR_PTR(-ENOMEM);
113
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);
119 }
120
121 *ntrips = count;
122
123 return no_free_ptr(tt);
124 }
125
of_thermal_zone_find(struct device_node * sensor,int id)126 static struct device_node *of_thermal_zone_find(struct device_node *sensor, int id)
127 {
128 struct of_phandle_args sensor_specs;
129
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);
134 }
135
136 /*
137 * Search for each thermal zone, a defined sensor
138 * corresponding to the one passed as parameter
139 */
140 for_each_available_child_of_node_scoped(np, child) {
141
142 int count, i;
143
144 count = of_count_phandle_with_args(child, "thermal-sensors",
145 "#thermal-sensor-cells");
146 if (count <= 0) {
147 pr_err("%pOFP: missing thermal sensor\n", child);
148 return ERR_PTR(-EINVAL);
149 }
150
151 for (i = 0; i < count; i++) {
152
153 int ret;
154
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("%pOFP: Failed to read thermal-sensors cells: %d\n", child, ret);
160 return ERR_PTR(ret);
161 }
162
163 of_node_put(sensor_specs.np);
164 if ((sensor == sensor_specs.np) && id == (sensor_specs.args_count ?
165 sensor_specs.args[0] : 0)) {
166 pr_debug("sensor %pOFP id=%d belongs to %pOFP\n", sensor, id, child);
167 return no_free_ptr(child);
168 }
169 }
170 }
171
172 return ERR_PTR(-ENODEV);
173 }
174
thermal_of_monitor_init(struct device_node * np,int * delay,int * pdelay)175 static int thermal_of_monitor_init(struct device_node *np, int *delay, int *pdelay)
176 {
177 int ret;
178
179 ret = of_property_read_u32(np, "polling-delay-passive", pdelay);
180 if (ret == -EINVAL) {
181 *pdelay = 0;
182 } else if (ret < 0) {
183 pr_err("%pOFP: Couldn't get polling-delay-passive: %d\n", np, ret);
184 return ret;
185 }
186
187 ret = of_property_read_u32(np, "polling-delay", delay);
188 if (ret == -EINVAL) {
189 *delay = 0;
190 } else if (ret < 0) {
191 pr_err("%pOFP: Couldn't get polling-delay: %d\n", np, ret);
192 return ret;
193 }
194
195 return 0;
196 }
197
thermal_of_parameters_init(struct device_node * np,struct thermal_zone_params * tzp)198 static void thermal_of_parameters_init(struct device_node *np,
199 struct thermal_zone_params *tzp)
200 {
201 int coef[2];
202 int ncoef = ARRAY_SIZE(coef);
203 int prop, ret;
204
205 tzp->no_hwmon = true;
206
207 if (!of_property_read_u32(np, "sustainable-power", &prop))
208 tzp->sustainable_power = prop;
209
210 /*
211 * For now, the thermal framework supports only one sensor per
212 * thermal zone. Thus, we are considering only the first two
213 * values as slope and offset.
214 */
215 ret = of_property_read_u32_array(np, "coefficients", coef, ncoef);
216 if (ret) {
217 coef[0] = 1;
218 coef[1] = 0;
219 }
220
221 tzp->slope = coef[0];
222 tzp->offset = coef[1];
223 }
224
thermal_of_zone_get_by_name(struct thermal_zone_device * tz)225 static struct device_node *thermal_of_zone_get_by_name(struct thermal_zone_device *tz)
226 {
227 struct device_node *np, *tz_np;
228
229 np = of_find_node_by_name(NULL, "thermal-zones");
230 if (!np)
231 return ERR_PTR(-ENODEV);
232
233 tz_np = of_get_child_by_name(np, tz->type);
234
235 of_node_put(np);
236
237 if (!tz_np)
238 return ERR_PTR(-ENODEV);
239
240 return tz_np;
241 }
242
thermal_of_get_cooling_spec(struct device_node * map_np,int index,struct thermal_cooling_device * cdev,struct cooling_spec * c)243 static bool thermal_of_get_cooling_spec(struct device_node *map_np, int index,
244 struct thermal_cooling_device *cdev,
245 struct cooling_spec *c)
246 {
247 struct of_phandle_args cooling_spec;
248 int ret, weight = THERMAL_WEIGHT_DEFAULT;
249
250 of_property_read_u32(map_np, "contribution", &weight);
251
252 ret = of_parse_phandle_with_args(map_np, "cooling-device", "#cooling-cells",
253 index, &cooling_spec);
254
255 if (ret < 0) {
256 pr_err("Invalid cooling-device entry\n");
257 return false;
258 }
259
260 of_node_put(cooling_spec.np);
261
262 /*
263 * There are two formats:
264 * - Legacy format : <&cdev lower upper>
265 * - New format : <&cdev cdev_id lower upper>
266 *
267 * With the new format, along with the device node pointer,
268 * the cdev_id must match with the cooling device cdev_id in
269 * order to bind
270 */
271 if (cooling_spec.args_count < 2 || cooling_spec.args_count > 3) {
272 pr_err("Invalid number of cooling device parameters\n");
273 return false;
274 }
275
276 if (cooling_spec.np != cdev->np)
277 return false;
278
279 if (cooling_spec.args_count == 3 &&
280 cooling_spec.args[0] != cdev->cdev_id)
281 return false;
282
283 if (cooling_spec.args_count != 3) {
284 c->lower = cooling_spec.args[0];
285 c->upper = cooling_spec.args[1];
286 } else {
287 c->lower = cooling_spec.args[1];
288 c->upper = cooling_spec.args[2];
289 }
290 c->weight = weight;
291
292 return true;
293 }
294
thermal_of_cm_lookup(struct device_node * cm_np,const struct thermal_trip * trip,struct thermal_cooling_device * cdev,struct cooling_spec * c)295 static bool thermal_of_cm_lookup(struct device_node *cm_np,
296 const struct thermal_trip *trip,
297 struct thermal_cooling_device *cdev,
298 struct cooling_spec *c)
299 {
300 for_each_child_of_node_scoped(cm_np, child) {
301 int count, i;
302
303 struct device_node *tr_np __free(device_node) =
304 of_parse_phandle(child, "trip", 0);
305 if (tr_np != trip->priv)
306 continue;
307
308 /* The trip has been found, look up the cdev. */
309 count = of_count_phandle_with_args(child, "cooling-device",
310 "#cooling-cells");
311 if (count <= 0)
312 pr_err("Add a cooling_device property with at least one device\n");
313
314 for (i = 0; i < count; i++) {
315 if (thermal_of_get_cooling_spec(child, i, cdev, c))
316 return true;
317 }
318 }
319
320 return false;
321 }
322
thermal_of_should_bind(struct thermal_zone_device * tz,const struct thermal_trip * trip,struct thermal_cooling_device * cdev,struct cooling_spec * c)323 static bool thermal_of_should_bind(struct thermal_zone_device *tz,
324 const struct thermal_trip *trip,
325 struct thermal_cooling_device *cdev,
326 struct cooling_spec *c)
327 {
328 struct device_node *tz_np, *cm_np;
329 bool result = false;
330
331 tz_np = thermal_of_zone_get_by_name(tz);
332 if (IS_ERR(tz_np)) {
333 pr_err("Failed to get node tz by name\n");
334 return false;
335 }
336
337 cm_np = of_get_child_by_name(tz_np, "cooling-maps");
338 if (!cm_np)
339 goto out;
340
341 /* Look up the trip and the cdev in the cooling maps. */
342 result = thermal_of_cm_lookup(cm_np, trip, cdev, c);
343
344 of_node_put(cm_np);
345 out:
346 of_node_put(tz_np);
347
348 return result;
349 }
350
351 /**
352 * thermal_of_zone_unregister - Cleanup the specific allocated ressources
353 *
354 * This function disables the thermal zone and frees the different
355 * ressources allocated specific to the thermal OF.
356 *
357 * @tz: a pointer to the thermal zone structure
358 */
thermal_of_zone_unregister(struct thermal_zone_device * tz)359 static void thermal_of_zone_unregister(struct thermal_zone_device *tz)
360 {
361 thermal_zone_device_disable(tz);
362 thermal_zone_device_unregister(tz);
363 }
364
365 /**
366 * thermal_of_zone_register - Register a thermal zone with device node
367 * sensor
368 *
369 * The thermal_of_zone_register() parses a device tree given a device
370 * node sensor and identifier. It searches for the thermal zone
371 * associated to the couple sensor/id and retrieves all the thermal
372 * zone properties and registers new thermal zone with those
373 * properties.
374 *
375 * @sensor: A device node pointer corresponding to the sensor in the device tree
376 * @id: An integer as sensor identifier
377 * @data: A private data to be stored in the thermal zone dedicated private area
378 * @ops: A set of thermal sensor ops
379 *
380 * Return: a valid thermal zone structure pointer on success.
381 * - EINVAL: if the device tree thermal description is malformed
382 * - ENOMEM: if one structure can not be allocated
383 * - Other negative errors are returned by the underlying called functions
384 */
thermal_of_zone_register(struct device_node * sensor,int id,void * data,const struct thermal_zone_device_ops * ops)385 static struct thermal_zone_device *thermal_of_zone_register(struct device_node *sensor, int id, void *data,
386 const struct thermal_zone_device_ops *ops)
387 {
388 struct thermal_zone_device_ops of_ops = *ops;
389 struct thermal_zone_device *tz;
390 struct thermal_trip *trips;
391 struct thermal_zone_params tzp = {};
392 struct device_node *np;
393 const char *action;
394 int delay, pdelay;
395 int ntrips;
396 int ret;
397
398 np = of_thermal_zone_find(sensor, id);
399 if (IS_ERR(np)) {
400 if (PTR_ERR(np) != -ENODEV)
401 pr_err("Failed to find thermal zone for %pOFP id=%d\n", sensor, id);
402 return ERR_CAST(np);
403 }
404
405 trips = thermal_of_trips_init(np, &ntrips);
406 if (IS_ERR(trips)) {
407 pr_err("Failed to parse trip points for %pOFP id=%d\n", sensor, id);
408 ret = PTR_ERR(trips);
409 goto out_of_node_put;
410 }
411
412 if (!trips)
413 pr_info("No trip points found for %pOFP id=%d\n", sensor, id);
414
415 ret = thermal_of_monitor_init(np, &delay, &pdelay);
416 if (ret) {
417 pr_err("Failed to initialize monitoring delays from %pOFP\n", np);
418 goto out_kfree_trips;
419 }
420
421 thermal_of_parameters_init(np, &tzp);
422
423 of_ops.should_bind = thermal_of_should_bind;
424
425 ret = of_property_read_string(np, "critical-action", &action);
426 if (!ret && !of_ops.critical) {
427 if (!strcasecmp(action, "reboot"))
428 of_ops.critical = thermal_zone_device_critical_reboot;
429 else if (!strcasecmp(action, "shutdown"))
430 of_ops.critical = thermal_zone_device_critical_shutdown;
431 }
432
433 tz = thermal_zone_device_register_with_trips(np->name, trips, ntrips,
434 data, &of_ops, &tzp,
435 pdelay, delay);
436 if (IS_ERR(tz)) {
437 ret = PTR_ERR(tz);
438 pr_err("Failed to register thermal zone %pOFP: %d\n", np, ret);
439 goto out_kfree_trips;
440 }
441
442 of_node_put(np);
443 kfree(trips);
444
445 ret = thermal_zone_device_enable(tz);
446 if (ret) {
447 pr_err("Failed to enable thermal zone '%s', id=%d: %d\n",
448 tz->type, tz->id, ret);
449 thermal_of_zone_unregister(tz);
450 return ERR_PTR(ret);
451 }
452
453 return tz;
454
455 out_kfree_trips:
456 kfree(trips);
457 out_of_node_put:
458 of_node_put(np);
459
460 return ERR_PTR(ret);
461 }
462
devm_thermal_of_zone_release(struct device * dev,void * res)463 static void devm_thermal_of_zone_release(struct device *dev, void *res)
464 {
465 thermal_of_zone_unregister(*(struct thermal_zone_device **)res);
466 }
467
devm_thermal_of_zone_match(struct device * dev,void * res,void * data)468 static int devm_thermal_of_zone_match(struct device *dev, void *res,
469 void *data)
470 {
471 struct thermal_zone_device **r = res;
472
473 if (WARN_ON(!r || !*r))
474 return 0;
475
476 return *r == data;
477 }
478
479 /**
480 * devm_thermal_of_zone_register - register a thermal tied with the sensor life cycle
481 *
482 * This function is the device version of the thermal_of_zone_register() function.
483 *
484 * @dev: a device structure pointer to sensor to be tied with the thermal zone OF life cycle
485 * @sensor_id: the sensor identifier
486 * @data: a pointer to a private data to be stored in the thermal zone 'devdata' field
487 * @ops: a pointer to the ops structure associated with the sensor
488 */
devm_thermal_of_zone_register(struct device * dev,int sensor_id,void * data,const struct thermal_zone_device_ops * ops)489 struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int sensor_id, void *data,
490 const struct thermal_zone_device_ops *ops)
491 {
492 struct thermal_zone_device **ptr, *tzd;
493
494 ptr = devres_alloc(devm_thermal_of_zone_release, sizeof(*ptr),
495 GFP_KERNEL);
496 if (!ptr)
497 return ERR_PTR(-ENOMEM);
498
499 tzd = thermal_of_zone_register(dev->of_node, sensor_id, data, ops);
500 if (IS_ERR(tzd)) {
501 devres_free(ptr);
502 return tzd;
503 }
504
505 *ptr = tzd;
506 devres_add(dev, ptr);
507
508 return tzd;
509 }
510 EXPORT_SYMBOL_GPL(devm_thermal_of_zone_register);
511
512 /**
513 * devm_thermal_of_zone_unregister - Resource managed version of
514 * thermal_of_zone_unregister().
515 * @dev: Device for which resource was allocated.
516 * @tz: a pointer to struct thermal_zone where the sensor is registered.
517 *
518 * This function removes the sensor callbacks and private data from the
519 * thermal zone device registered with devm_thermal_zone_of_sensor_register()
520 * API. It will also silent the zone by remove the .get_temp() and .get_trend()
521 * thermal zone device callbacks.
522 * Normally this function will not need to be called and the resource
523 * management code will ensure that the resource is freed.
524 */
devm_thermal_of_zone_unregister(struct device * dev,struct thermal_zone_device * tz)525 void devm_thermal_of_zone_unregister(struct device *dev, struct thermal_zone_device *tz)
526 {
527 WARN_ON(devres_release(dev, devm_thermal_of_zone_release,
528 devm_thermal_of_zone_match, tz));
529 }
530 EXPORT_SYMBOL_GPL(devm_thermal_of_zone_unregister);
531
532 /**
533 * thermal_of_cooling_device_register() - register an OF thermal cooling device
534 * @np: a pointer to a device tree node.
535 * @cdev_id: a cooling device id in the cooling controller
536 * @type: the thermal cooling device type.
537 * @devdata: device private data.
538 * @ops: standard thermal cooling devices callbacks.
539 *
540 * This interface function adds a new thermal cooling device (fan/processor/...)
541 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
542 * to all the thermal zone devices registered at the same time.
543 * It also gives the opportunity to link the cooling device to a device tree
544 * node, so that it can be bound to a thermal zone created out of device tree.
545 *
546 * Return: a pointer to the created struct thermal_cooling_device or an
547 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
548 */
549 struct thermal_cooling_device *
thermal_of_cooling_device_register(struct device_node * np,u32 cdev_id,const char * type,void * devdata,const struct thermal_cooling_device_ops * ops)550 thermal_of_cooling_device_register(struct device_node *np, u32 cdev_id,
551 const char *type, void *devdata,
552 const struct thermal_cooling_device_ops *ops)
553 {
554 struct thermal_cooling_device *cdev;
555 int ret;
556
557 cdev = thermal_cooling_device_alloc(type, ops);
558 if (IS_ERR(cdev))
559 return cdev;
560
561 cdev->np = np;
562 cdev->cdev_id = cdev_id;
563
564 ret = thermal_cooling_device_add(cdev, devdata);
565 if (ret)
566 return ERR_PTR(ret);
567
568 return cdev;
569 }
570 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
571
thermal_of_cooling_device_release(void * data)572 static void thermal_of_cooling_device_release(void *data)
573 {
574 struct thermal_cooling_device *cdev = data;
575
576 thermal_cooling_device_unregister(cdev);
577 }
578
579 static struct thermal_cooling_device *
__devm_thermal_of_cooling_device_register(struct device * dev,struct device_node * np,u32 cdev_id,const char * type,void * devdata,const struct thermal_cooling_device_ops * ops)580 __devm_thermal_of_cooling_device_register(struct device *dev, struct device_node *np,
581 u32 cdev_id, const char *type, void *devdata,
582 const struct thermal_cooling_device_ops *ops)
583 {
584 struct thermal_cooling_device *cdev;
585 int ret;
586
587 cdev = thermal_of_cooling_device_register(np, cdev_id, type, devdata, ops);
588 if (IS_ERR(cdev))
589 return cdev;
590
591 ret = devm_add_action_or_reset(dev, thermal_of_cooling_device_release, cdev);
592 if (ret)
593 return ERR_PTR(ret);
594
595 return cdev;
596 }
597
598 /**
599 * devm_thermal_of_cooling_device_register() - register an OF thermal cooling device
600 * @dev: a valid struct device pointer of a sensor device.
601 * @cdev_id: a cooling device index in the cooling controller
602 * @type: the thermal cooling device type.
603 * @devdata: device private data.
604 * @ops: standard thermal cooling devices callbacks.
605 *
606 * This function will register a cooling device with device tree node reference.
607 * This interface function adds a new thermal cooling device (fan/processor/...)
608 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
609 * to all the thermal zone devices registered at the same time.
610 *
611 * Return: a pointer to the created struct thermal_cooling_device or an
612 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
613 */
614 struct thermal_cooling_device *
devm_thermal_of_cooling_device_register(struct device * dev,u32 cdev_id,const char * type,void * devdata,const struct thermal_cooling_device_ops * ops)615 devm_thermal_of_cooling_device_register(struct device *dev, u32 cdev_id,
616 const char *type, void *devdata,
617 const struct thermal_cooling_device_ops *ops)
618 {
619 return __devm_thermal_of_cooling_device_register(dev, dev->of_node, cdev_id,
620 type, devdata, ops);
621 }
622 EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register);
623
624 /**
625 * devm_thermal_of_child_cooling_device_register() - register an OF thermal cooling
626 * device
627 * @dev: a valid struct device pointer of a sensor device.
628 * @np: a pointer to a device tree node.
629 * @type: the thermal cooling device type.
630 * @devdata: device private data.
631 * @ops: standard thermal cooling devices callbacks.
632 *
633 * This function will register a cooling device with device tree node reference.
634 * This interface function adds a new thermal cooling device (fan/processor/...)
635 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
636 * to all the thermal zone devices registered at the same time.
637 *
638 * This function should be used when a cooling controller has child
639 * nodes which are referenced in the thermal zone cooling map.
640 *
641 * Return: a pointer to the created struct thermal_cooling_device or an
642 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
643 */
644 struct thermal_cooling_device *
devm_thermal_of_child_cooling_device_register(struct device * dev,struct device_node * np,const char * type,void * devdata,const struct thermal_cooling_device_ops * ops)645 devm_thermal_of_child_cooling_device_register(struct device *dev,
646 struct device_node *np,
647 const char *type, void *devdata,
648 const struct thermal_cooling_device_ops *ops)
649 {
650 return __devm_thermal_of_cooling_device_register(dev, np, 0, type, devdata, ops);
651 }
652 EXPORT_SYMBOL_GPL(devm_thermal_of_child_cooling_device_register);
653