1 // SPDX-License-Identifier: GPL-2.0
3 * thermal.c - Generic Thermal Management Sysfs support.
5 * Copyright (C) 2008 Intel Corp
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/export.h>
15 #include <linux/slab.h>
16 #include <linux/kdev_t.h>
17 #include <linux/idr.h>
18 #include <linux/list_sort.h>
19 #include <linux/thermal.h>
20 #include <linux/reboot.h>
21 #include <linux/string.h>
23 #include <linux/suspend.h>
25 #define CREATE_TRACE_POINTS
26 #include "thermal_trace.h"
28 #include "thermal_core.h"
29 #include "thermal_hwmon.h"
31 static DEFINE_IDA(thermal_tz_ida);
32 static DEFINE_IDA(thermal_cdev_ida);
34 static LIST_HEAD(thermal_tz_list);
35 static LIST_HEAD(thermal_cdev_list);
36 static LIST_HEAD(thermal_governor_list);
38 static DEFINE_MUTEX(thermal_list_lock);
39 static DEFINE_MUTEX(thermal_governor_lock);
41 static struct thermal_governor *def_governor;
44 * Governor section: set of functions to handle thermal governors
46 * Functions to help in the life cycle of thermal governors within
47 * the thermal core and by the thermal governor code.
50 static struct thermal_governor *__find_governor(const char *name)
52 struct thermal_governor *pos;
54 if (!name || !name[0])
57 list_for_each_entry(pos, &thermal_governor_list, governor_list)
58 if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH))
65 * bind_previous_governor() - bind the previous governor of the thermal zone
66 * @tz: a valid pointer to a struct thermal_zone_device
67 * @failed_gov_name: the name of the governor that failed to register
69 * Register the previous governor of the thermal zone after a new
70 * governor has failed to be bound.
72 static void bind_previous_governor(struct thermal_zone_device *tz,
73 const char *failed_gov_name)
75 if (tz->governor && tz->governor->bind_to_tz) {
76 if (tz->governor->bind_to_tz(tz)) {
78 "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n",
79 failed_gov_name, tz->governor->name, tz->type);
86 * thermal_set_governor() - Switch to another governor
87 * @tz: a valid pointer to a struct thermal_zone_device
88 * @new_gov: pointer to the new governor
90 * Change the governor of thermal zone @tz.
92 * Return: 0 on success, an error if the new governor's bind_to_tz() failed.
94 static int thermal_set_governor(struct thermal_zone_device *tz,
95 struct thermal_governor *new_gov)
99 if (tz->governor && tz->governor->unbind_from_tz)
100 tz->governor->unbind_from_tz(tz);
102 if (new_gov && new_gov->bind_to_tz) {
103 ret = new_gov->bind_to_tz(tz);
105 bind_previous_governor(tz, new_gov->name);
111 tz->governor = new_gov;
116 int thermal_register_governor(struct thermal_governor *governor)
120 struct thermal_zone_device *pos;
125 mutex_lock(&thermal_governor_lock);
128 if (!__find_governor(governor->name)) {
132 list_add(&governor->governor_list, &thermal_governor_list);
133 match_default = !strncmp(governor->name,
134 DEFAULT_THERMAL_GOVERNOR,
135 THERMAL_NAME_LENGTH);
137 if (!def_governor && match_default)
138 def_governor = governor;
141 mutex_lock(&thermal_list_lock);
143 list_for_each_entry(pos, &thermal_tz_list, node) {
145 * only thermal zones with specified tz->tzp->governor_name
146 * may run with tz->govenor unset
151 name = pos->tzp->governor_name;
153 if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) {
156 ret = thermal_set_governor(pos, governor);
158 dev_err(&pos->device,
159 "Failed to set governor %s for thermal zone %s: %d\n",
160 governor->name, pos->type, ret);
164 mutex_unlock(&thermal_list_lock);
165 mutex_unlock(&thermal_governor_lock);
170 void thermal_unregister_governor(struct thermal_governor *governor)
172 struct thermal_zone_device *pos;
177 mutex_lock(&thermal_governor_lock);
179 if (!__find_governor(governor->name))
182 mutex_lock(&thermal_list_lock);
184 list_for_each_entry(pos, &thermal_tz_list, node) {
185 if (!strncasecmp(pos->governor->name, governor->name,
186 THERMAL_NAME_LENGTH))
187 thermal_set_governor(pos, NULL);
190 mutex_unlock(&thermal_list_lock);
191 list_del(&governor->governor_list);
193 mutex_unlock(&thermal_governor_lock);
196 int thermal_zone_device_set_policy(struct thermal_zone_device *tz,
199 struct thermal_governor *gov;
202 mutex_lock(&thermal_governor_lock);
203 mutex_lock(&tz->lock);
205 gov = __find_governor(strim(policy));
209 ret = thermal_set_governor(tz, gov);
212 mutex_unlock(&tz->lock);
213 mutex_unlock(&thermal_governor_lock);
215 thermal_notify_tz_gov_change(tz, policy);
220 int thermal_build_list_of_policies(char *buf)
222 struct thermal_governor *pos;
225 mutex_lock(&thermal_governor_lock);
227 list_for_each_entry(pos, &thermal_governor_list, governor_list) {
228 count += sysfs_emit_at(buf, count, "%s ", pos->name);
230 count += sysfs_emit_at(buf, count, "\n");
232 mutex_unlock(&thermal_governor_lock);
237 static void __init thermal_unregister_governors(void)
239 struct thermal_governor **governor;
241 for_each_governor_table(governor)
242 thermal_unregister_governor(*governor);
245 static int __init thermal_register_governors(void)
248 struct thermal_governor **governor;
250 for_each_governor_table(governor) {
251 ret = thermal_register_governor(*governor);
253 pr_err("Failed to register governor: '%s'",
258 pr_info("Registered thermal governor '%s'",
263 struct thermal_governor **gov;
265 for_each_governor_table(gov) {
268 thermal_unregister_governor(*gov);
276 * Zone update section: main control loop applied to each zone while monitoring
277 * in polling mode. The monitoring is done using a workqueue.
278 * Same update may be done on a zone by calling thermal_zone_device_update().
281 * - Non-critical trips will invoke the governor responsible for that zone;
282 * - Hot trips will produce a notification to userspace;
283 * - Critical trip point will cause a system shutdown.
285 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
289 mod_delayed_work(system_freezable_power_efficient_wq,
290 &tz->poll_queue, delay);
292 cancel_delayed_work(&tz->poll_queue);
295 static void monitor_thermal_zone(struct thermal_zone_device *tz)
297 if (tz->mode != THERMAL_DEVICE_ENABLED)
298 thermal_zone_device_set_polling(tz, 0);
299 else if (tz->passive > 0)
300 thermal_zone_device_set_polling(tz, tz->passive_delay_jiffies);
301 else if (tz->polling_delay_jiffies)
302 thermal_zone_device_set_polling(tz, tz->polling_delay_jiffies);
305 static struct thermal_governor *thermal_get_tz_governor(struct thermal_zone_device *tz)
313 void thermal_governor_update_tz(struct thermal_zone_device *tz,
314 enum thermal_notify_event reason)
316 if (!tz->governor || !tz->governor->update_tz)
319 tz->governor->update_tz(tz, reason);
322 static void thermal_zone_device_halt(struct thermal_zone_device *tz, bool shutdown)
325 * poweroff_delay_ms must be a carefully profiled positive value.
326 * Its a must for forced_emergency_poweroff_work to be scheduled.
328 int poweroff_delay_ms = CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS;
329 const char *msg = "Temperature too high";
331 dev_emerg(&tz->device, "%s: critical temperature reached\n", tz->type);
334 hw_protection_shutdown(msg, poweroff_delay_ms);
336 hw_protection_reboot(msg, poweroff_delay_ms);
339 void thermal_zone_device_critical(struct thermal_zone_device *tz)
341 thermal_zone_device_halt(tz, true);
343 EXPORT_SYMBOL(thermal_zone_device_critical);
345 void thermal_zone_device_critical_reboot(struct thermal_zone_device *tz)
347 thermal_zone_device_halt(tz, false);
350 static void handle_critical_trips(struct thermal_zone_device *tz,
351 const struct thermal_trip *trip)
353 trace_thermal_zone_trip(tz, thermal_zone_trip_id(tz, trip), trip->type);
355 if (trip->type == THERMAL_TRIP_CRITICAL)
356 tz->ops.critical(tz);
357 else if (tz->ops.hot)
361 static void handle_thermal_trip(struct thermal_zone_device *tz,
362 struct thermal_trip_desc *td,
363 struct list_head *way_up_list,
364 struct list_head *way_down_list)
366 const struct thermal_trip *trip = &td->trip;
369 if (trip->temperature == THERMAL_TEMP_INVALID)
373 * If the trip temperature or hysteresis has been updated recently,
374 * the threshold needs to be computed again using the new values.
375 * However, its initial value still reflects the old ones and that
376 * is what needs to be compared with the previous zone temperature
377 * to decide which action to take.
379 old_threshold = td->threshold;
380 td->threshold = trip->temperature;
382 if (tz->last_temperature >= old_threshold &&
383 tz->last_temperature != THERMAL_TEMP_INVALID) {
385 * Mitigation is under way, so it needs to stop if the zone
386 * temperature falls below the low temperature of the trip.
387 * In that case, the trip temperature becomes the new threshold.
389 if (tz->temperature < trip->temperature - trip->hysteresis) {
390 list_add(&td->notify_list_node, way_down_list);
391 td->notify_temp = trip->temperature - trip->hysteresis;
393 if (trip->type == THERMAL_TRIP_PASSIVE) {
395 WARN_ON(tz->passive < 0);
398 td->threshold -= trip->hysteresis;
400 } else if (tz->temperature >= trip->temperature) {
402 * There is no mitigation under way, so it needs to be started
403 * if the zone temperature exceeds the trip one. The new
404 * threshold is then set to the low temperature of the trip.
406 list_add_tail(&td->notify_list_node, way_up_list);
407 td->notify_temp = trip->temperature;
408 td->threshold -= trip->hysteresis;
410 if (trip->type == THERMAL_TRIP_PASSIVE)
412 else if (trip->type == THERMAL_TRIP_CRITICAL ||
413 trip->type == THERMAL_TRIP_HOT)
414 handle_critical_trips(tz, trip);
418 static void update_temperature(struct thermal_zone_device *tz)
422 ret = __thermal_zone_get_temp(tz, &temp);
425 dev_warn(&tz->device,
426 "failed to read out thermal zone (%d)\n",
431 tz->last_temperature = tz->temperature;
432 tz->temperature = temp;
434 trace_thermal_temperature(tz);
436 thermal_genl_sampling_temp(tz->id, temp);
439 static void thermal_zone_device_check(struct work_struct *work)
441 struct thermal_zone_device *tz = container_of(work, struct
444 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
447 static void thermal_zone_device_init(struct thermal_zone_device *tz)
449 struct thermal_instance *pos;
451 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
453 tz->temperature = THERMAL_TEMP_INVALID;
455 tz->prev_low_trip = -INT_MAX;
456 tz->prev_high_trip = INT_MAX;
457 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
458 pos->initialized = false;
461 static void thermal_governor_trip_crossed(struct thermal_governor *governor,
462 struct thermal_zone_device *tz,
463 const struct thermal_trip *trip,
466 if (governor->trip_crossed)
467 governor->trip_crossed(tz, trip, crossed_up);
470 static int thermal_trip_notify_cmp(void *ascending, const struct list_head *a,
471 const struct list_head *b)
473 struct thermal_trip_desc *tda = container_of(a, struct thermal_trip_desc,
475 struct thermal_trip_desc *tdb = container_of(b, struct thermal_trip_desc,
477 int ret = tdb->notify_temp - tda->notify_temp;
479 return ascending ? ret : -ret;
482 void __thermal_zone_device_update(struct thermal_zone_device *tz,
483 enum thermal_notify_event event)
485 struct thermal_governor *governor = thermal_get_tz_governor(tz);
486 struct thermal_trip_desc *td;
487 LIST_HEAD(way_down_list);
488 LIST_HEAD(way_up_list);
493 if (!thermal_zone_device_is_enabled(tz))
496 update_temperature(tz);
498 if (tz->temperature == THERMAL_TEMP_INVALID)
501 __thermal_zone_set_trips(tz);
503 tz->notify_event = event;
505 for_each_trip_desc(tz, td)
506 handle_thermal_trip(tz, td, &way_up_list, &way_down_list);
508 list_sort(&way_up_list, &way_up_list, thermal_trip_notify_cmp);
509 list_for_each_entry(td, &way_up_list, notify_list_node) {
510 thermal_notify_tz_trip_up(tz, &td->trip);
511 thermal_debug_tz_trip_up(tz, &td->trip);
512 thermal_governor_trip_crossed(governor, tz, &td->trip, true);
515 list_sort(NULL, &way_down_list, thermal_trip_notify_cmp);
516 list_for_each_entry(td, &way_down_list, notify_list_node) {
517 thermal_notify_tz_trip_down(tz, &td->trip);
518 thermal_debug_tz_trip_down(tz, &td->trip);
519 thermal_governor_trip_crossed(governor, tz, &td->trip, false);
522 if (governor->manage)
523 governor->manage(tz);
525 thermal_debug_update_trip_stats(tz);
527 monitor_thermal_zone(tz);
530 static int thermal_zone_device_set_mode(struct thermal_zone_device *tz,
531 enum thermal_device_mode mode)
535 mutex_lock(&tz->lock);
537 /* do nothing if mode isn't changing */
538 if (mode == tz->mode) {
539 mutex_unlock(&tz->lock);
544 if (tz->ops.change_mode)
545 ret = tz->ops.change_mode(tz, mode);
550 __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
552 mutex_unlock(&tz->lock);
554 if (mode == THERMAL_DEVICE_ENABLED)
555 thermal_notify_tz_enable(tz);
557 thermal_notify_tz_disable(tz);
562 int thermal_zone_device_enable(struct thermal_zone_device *tz)
564 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_ENABLED);
566 EXPORT_SYMBOL_GPL(thermal_zone_device_enable);
568 int thermal_zone_device_disable(struct thermal_zone_device *tz)
570 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED);
572 EXPORT_SYMBOL_GPL(thermal_zone_device_disable);
574 int thermal_zone_device_is_enabled(struct thermal_zone_device *tz)
576 lockdep_assert_held(&tz->lock);
578 return tz->mode == THERMAL_DEVICE_ENABLED;
581 static bool thermal_zone_is_present(struct thermal_zone_device *tz)
583 return !list_empty(&tz->node);
586 void thermal_zone_device_update(struct thermal_zone_device *tz,
587 enum thermal_notify_event event)
589 mutex_lock(&tz->lock);
590 if (thermal_zone_is_present(tz))
591 __thermal_zone_device_update(tz, event);
592 mutex_unlock(&tz->lock);
594 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
596 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
599 struct thermal_governor *gov;
602 mutex_lock(&thermal_governor_lock);
603 list_for_each_entry(gov, &thermal_governor_list, governor_list) {
608 mutex_unlock(&thermal_governor_lock);
613 int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
616 struct thermal_cooling_device *cdev;
619 mutex_lock(&thermal_list_lock);
620 list_for_each_entry(cdev, &thermal_cdev_list, node) {
621 ret = cb(cdev, data);
625 mutex_unlock(&thermal_list_lock);
630 int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
633 struct thermal_zone_device *tz;
636 mutex_lock(&thermal_list_lock);
637 list_for_each_entry(tz, &thermal_tz_list, node) {
642 mutex_unlock(&thermal_list_lock);
647 struct thermal_zone_device *thermal_zone_get_by_id(int id)
649 struct thermal_zone_device *tz, *match = NULL;
651 mutex_lock(&thermal_list_lock);
652 list_for_each_entry(tz, &thermal_tz_list, node) {
658 mutex_unlock(&thermal_list_lock);
664 * Device management section: cooling devices, zones devices, and binding
666 * Set of functions provided by the thermal core for:
667 * - cooling devices lifecycle: registration, unregistration,
668 * binding, and unbinding.
669 * - thermal zone devices lifecycle: registration, unregistration,
670 * binding, and unbinding.
674 * thermal_bind_cdev_to_trip - bind a cooling device to a thermal zone
675 * @tz: pointer to struct thermal_zone_device
676 * @trip: trip point the cooling devices is associated with in this zone.
677 * @cdev: pointer to struct thermal_cooling_device
678 * @upper: the Maximum cooling state for this trip point.
679 * THERMAL_NO_LIMIT means no upper limit,
680 * and the cooling device can be in max_state.
681 * @lower: the Minimum cooling state can be used for this trip point.
682 * THERMAL_NO_LIMIT means no lower limit,
683 * and the cooling device can be in cooling state 0.
684 * @weight: The weight of the cooling device to be bound to the
685 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
688 * This interface function bind a thermal cooling device to the certain trip
689 * point of a thermal zone device.
690 * This function is usually called in the thermal zone device .bind callback.
692 * Return: 0 on success, the proper error value otherwise.
694 int thermal_bind_cdev_to_trip(struct thermal_zone_device *tz,
695 const struct thermal_trip *trip,
696 struct thermal_cooling_device *cdev,
697 unsigned long upper, unsigned long lower,
700 struct thermal_instance *dev;
701 struct thermal_instance *pos;
702 struct thermal_zone_device *pos1;
703 struct thermal_cooling_device *pos2;
707 list_for_each_entry(pos1, &thermal_tz_list, node) {
711 list_for_each_entry(pos2, &thermal_cdev_list, node) {
716 if (tz != pos1 || cdev != pos2)
719 /* lower default 0, upper default max_state */
720 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
722 if (upper == THERMAL_NO_LIMIT) {
723 upper = cdev->max_state;
724 upper_no_limit = true;
726 upper_no_limit = false;
729 if (lower > upper || upper > cdev->max_state)
732 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
739 dev->upper_no_limit = upper_no_limit;
741 dev->target = THERMAL_NO_TARGET;
742 dev->weight = weight;
744 result = ida_alloc(&tz->ida, GFP_KERNEL);
749 sprintf(dev->name, "cdev%d", dev->id);
751 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
755 snprintf(dev->attr_name, sizeof(dev->attr_name), "cdev%d_trip_point",
757 sysfs_attr_init(&dev->attr.attr);
758 dev->attr.attr.name = dev->attr_name;
759 dev->attr.attr.mode = 0444;
760 dev->attr.show = trip_point_show;
761 result = device_create_file(&tz->device, &dev->attr);
763 goto remove_symbol_link;
765 snprintf(dev->weight_attr_name, sizeof(dev->weight_attr_name),
766 "cdev%d_weight", dev->id);
767 sysfs_attr_init(&dev->weight_attr.attr);
768 dev->weight_attr.attr.name = dev->weight_attr_name;
769 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
770 dev->weight_attr.show = weight_show;
771 dev->weight_attr.store = weight_store;
772 result = device_create_file(&tz->device, &dev->weight_attr);
774 goto remove_trip_file;
776 mutex_lock(&tz->lock);
777 mutex_lock(&cdev->lock);
778 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
779 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
784 list_add_tail(&dev->tz_node, &tz->thermal_instances);
785 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
786 atomic_set(&tz->need_update, 1);
788 thermal_governor_update_tz(tz, THERMAL_TZ_BIND_CDEV);
790 mutex_unlock(&cdev->lock);
791 mutex_unlock(&tz->lock);
796 device_remove_file(&tz->device, &dev->weight_attr);
798 device_remove_file(&tz->device, &dev->attr);
800 sysfs_remove_link(&tz->device.kobj, dev->name);
802 ida_free(&tz->ida, dev->id);
807 EXPORT_SYMBOL_GPL(thermal_bind_cdev_to_trip);
809 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
811 struct thermal_cooling_device *cdev,
812 unsigned long upper, unsigned long lower,
815 if (trip_index < 0 || trip_index >= tz->num_trips)
818 return thermal_bind_cdev_to_trip(tz, &tz->trips[trip_index].trip, cdev,
819 upper, lower, weight);
821 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
824 * thermal_unbind_cdev_from_trip - unbind a cooling device from a thermal zone.
825 * @tz: pointer to a struct thermal_zone_device.
826 * @trip: trip point the cooling devices is associated with in this zone.
827 * @cdev: pointer to a struct thermal_cooling_device.
829 * This interface function unbind a thermal cooling device from the certain
830 * trip point of a thermal zone device.
831 * This function is usually called in the thermal zone device .unbind callback.
833 * Return: 0 on success, the proper error value otherwise.
835 int thermal_unbind_cdev_from_trip(struct thermal_zone_device *tz,
836 const struct thermal_trip *trip,
837 struct thermal_cooling_device *cdev)
839 struct thermal_instance *pos, *next;
841 mutex_lock(&tz->lock);
842 mutex_lock(&cdev->lock);
843 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
844 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
845 list_del(&pos->tz_node);
846 list_del(&pos->cdev_node);
848 thermal_governor_update_tz(tz, THERMAL_TZ_UNBIND_CDEV);
850 mutex_unlock(&cdev->lock);
851 mutex_unlock(&tz->lock);
855 mutex_unlock(&cdev->lock);
856 mutex_unlock(&tz->lock);
861 device_remove_file(&tz->device, &pos->weight_attr);
862 device_remove_file(&tz->device, &pos->attr);
863 sysfs_remove_link(&tz->device.kobj, pos->name);
864 ida_free(&tz->ida, pos->id);
868 EXPORT_SYMBOL_GPL(thermal_unbind_cdev_from_trip);
870 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
872 struct thermal_cooling_device *cdev)
874 if (trip_index < 0 || trip_index >= tz->num_trips)
877 return thermal_unbind_cdev_from_trip(tz, &tz->trips[trip_index].trip, cdev);
879 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
881 static void thermal_release(struct device *dev)
883 struct thermal_zone_device *tz;
884 struct thermal_cooling_device *cdev;
886 if (!strncmp(dev_name(dev), "thermal_zone",
887 sizeof("thermal_zone") - 1)) {
888 tz = to_thermal_zone(dev);
889 thermal_zone_destroy_device_groups(tz);
890 mutex_destroy(&tz->lock);
891 complete(&tz->removal);
892 } else if (!strncmp(dev_name(dev), "cooling_device",
893 sizeof("cooling_device") - 1)) {
894 cdev = to_cooling_device(dev);
895 thermal_cooling_device_destroy_sysfs(cdev);
896 kfree_const(cdev->type);
897 ida_free(&thermal_cdev_ida, cdev->id);
902 static struct class *thermal_class;
905 void print_bind_err_msg(struct thermal_zone_device *tz,
906 struct thermal_cooling_device *cdev, int ret)
908 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
909 tz->type, cdev->type, ret);
912 static void bind_cdev(struct thermal_cooling_device *cdev)
915 struct thermal_zone_device *pos = NULL;
917 list_for_each_entry(pos, &thermal_tz_list, node) {
919 ret = pos->ops.bind(pos, cdev);
921 print_bind_err_msg(pos, cdev, ret);
927 * __thermal_cooling_device_register() - register a new thermal cooling device
928 * @np: a pointer to a device tree node.
929 * @type: the thermal cooling device type.
930 * @devdata: device private data.
931 * @ops: standard thermal cooling devices callbacks.
933 * This interface function adds a new thermal cooling device (fan/processor/...)
934 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
935 * to all the thermal zone devices registered at the same time.
936 * It also gives the opportunity to link the cooling device to a device tree
937 * node, so that it can be bound to a thermal zone created out of device tree.
939 * Return: a pointer to the created struct thermal_cooling_device or an
940 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
942 static struct thermal_cooling_device *
943 __thermal_cooling_device_register(struct device_node *np,
944 const char *type, void *devdata,
945 const struct thermal_cooling_device_ops *ops)
947 struct thermal_cooling_device *cdev;
948 struct thermal_zone_device *pos = NULL;
949 unsigned long current_state;
952 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
954 return ERR_PTR(-EINVAL);
957 return ERR_PTR(-ENODEV);
959 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
961 return ERR_PTR(-ENOMEM);
963 ret = ida_alloc(&thermal_cdev_ida, GFP_KERNEL);
969 cdev->type = kstrdup_const(type ? type : "", GFP_KERNEL);
975 mutex_init(&cdev->lock);
976 INIT_LIST_HEAD(&cdev->thermal_instances);
979 cdev->updated = false;
980 cdev->device.class = thermal_class;
981 cdev->devdata = devdata;
983 ret = cdev->ops->get_max_state(cdev, &cdev->max_state);
987 ret = cdev->ops->get_cur_state(cdev, ¤t_state);
991 thermal_cooling_device_setup_sysfs(cdev);
993 ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
995 goto out_cooling_dev;
997 ret = device_register(&cdev->device);
999 /* thermal_release() handles rest of the cleanup */
1000 put_device(&cdev->device);
1001 return ERR_PTR(ret);
1004 thermal_debug_cdev_add(cdev, current_state);
1006 /* Add 'this' new cdev to the global cdev list */
1007 mutex_lock(&thermal_list_lock);
1009 list_add(&cdev->node, &thermal_cdev_list);
1011 /* Update binding information for 'this' new cdev */
1014 list_for_each_entry(pos, &thermal_tz_list, node)
1015 if (atomic_cmpxchg(&pos->need_update, 1, 0))
1016 thermal_zone_device_update(pos,
1017 THERMAL_EVENT_UNSPECIFIED);
1019 mutex_unlock(&thermal_list_lock);
1024 thermal_cooling_device_destroy_sysfs(cdev);
1026 kfree_const(cdev->type);
1028 ida_free(&thermal_cdev_ida, id);
1031 return ERR_PTR(ret);
1035 * thermal_cooling_device_register() - register a new thermal cooling device
1036 * @type: the thermal cooling device type.
1037 * @devdata: device private data.
1038 * @ops: standard thermal cooling devices callbacks.
1040 * This interface function adds a new thermal cooling device (fan/processor/...)
1041 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1042 * to all the thermal zone devices registered at the same time.
1044 * Return: a pointer to the created struct thermal_cooling_device or an
1045 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1047 struct thermal_cooling_device *
1048 thermal_cooling_device_register(const char *type, void *devdata,
1049 const struct thermal_cooling_device_ops *ops)
1051 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1053 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
1056 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1057 * @np: a pointer to a device tree node.
1058 * @type: the thermal cooling device type.
1059 * @devdata: device private data.
1060 * @ops: standard thermal cooling devices callbacks.
1062 * This function will register a cooling device with device tree node reference.
1063 * This interface function adds a new thermal cooling device (fan/processor/...)
1064 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1065 * to all the thermal zone devices registered at the same time.
1067 * Return: a pointer to the created struct thermal_cooling_device or an
1068 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1070 struct thermal_cooling_device *
1071 thermal_of_cooling_device_register(struct device_node *np,
1072 const char *type, void *devdata,
1073 const struct thermal_cooling_device_ops *ops)
1075 return __thermal_cooling_device_register(np, type, devdata, ops);
1077 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1079 static void thermal_cooling_device_release(struct device *dev, void *res)
1081 thermal_cooling_device_unregister(
1082 *(struct thermal_cooling_device **)res);
1086 * devm_thermal_of_cooling_device_register() - register an OF thermal cooling
1088 * @dev: a valid struct device pointer of a sensor device.
1089 * @np: a pointer to a device tree node.
1090 * @type: the thermal cooling device type.
1091 * @devdata: device private data.
1092 * @ops: standard thermal cooling devices callbacks.
1094 * This function will register a cooling device with device tree node reference.
1095 * This interface function adds a new thermal cooling device (fan/processor/...)
1096 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1097 * to all the thermal zone devices registered at the same time.
1099 * Return: a pointer to the created struct thermal_cooling_device or an
1100 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1102 struct thermal_cooling_device *
1103 devm_thermal_of_cooling_device_register(struct device *dev,
1104 struct device_node *np,
1105 char *type, void *devdata,
1106 const struct thermal_cooling_device_ops *ops)
1108 struct thermal_cooling_device **ptr, *tcd;
1110 ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr),
1113 return ERR_PTR(-ENOMEM);
1115 tcd = __thermal_cooling_device_register(np, type, devdata, ops);
1122 devres_add(dev, ptr);
1126 EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register);
1128 static bool thermal_cooling_device_present(struct thermal_cooling_device *cdev)
1130 struct thermal_cooling_device *pos = NULL;
1132 list_for_each_entry(pos, &thermal_cdev_list, node) {
1141 * thermal_cooling_device_update - Update a cooling device object
1142 * @cdev: Target cooling device.
1144 * Update @cdev to reflect a change of the underlying hardware or platform.
1146 * Must be called when the maximum cooling state of @cdev becomes invalid and so
1147 * its .get_max_state() callback needs to be run to produce the new maximum
1148 * cooling state value.
1150 void thermal_cooling_device_update(struct thermal_cooling_device *cdev)
1152 struct thermal_instance *ti;
1153 unsigned long state;
1155 if (IS_ERR_OR_NULL(cdev))
1159 * Hold thermal_list_lock throughout the update to prevent the device
1160 * from going away while being updated.
1162 mutex_lock(&thermal_list_lock);
1164 if (!thermal_cooling_device_present(cdev))
1168 * Update under the cdev lock to prevent the state from being set beyond
1169 * the new limit concurrently.
1171 mutex_lock(&cdev->lock);
1173 if (cdev->ops->get_max_state(cdev, &cdev->max_state))
1176 thermal_cooling_device_stats_reinit(cdev);
1178 list_for_each_entry(ti, &cdev->thermal_instances, cdev_node) {
1179 if (ti->upper == cdev->max_state)
1182 if (ti->upper < cdev->max_state) {
1183 if (ti->upper_no_limit)
1184 ti->upper = cdev->max_state;
1189 ti->upper = cdev->max_state;
1190 if (ti->lower > ti->upper)
1191 ti->lower = ti->upper;
1193 if (ti->target == THERMAL_NO_TARGET)
1196 if (ti->target > ti->upper)
1197 ti->target = ti->upper;
1200 if (cdev->ops->get_cur_state(cdev, &state) || state > cdev->max_state)
1203 thermal_cooling_device_stats_update(cdev, state);
1206 mutex_unlock(&cdev->lock);
1209 mutex_unlock(&thermal_list_lock);
1211 EXPORT_SYMBOL_GPL(thermal_cooling_device_update);
1214 * thermal_cooling_device_unregister - removes a thermal cooling device
1215 * @cdev: the thermal cooling device to remove.
1217 * thermal_cooling_device_unregister() must be called when a registered
1218 * thermal cooling device is no longer needed.
1220 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1222 struct thermal_zone_device *tz;
1227 thermal_debug_cdev_remove(cdev);
1229 mutex_lock(&thermal_list_lock);
1231 if (!thermal_cooling_device_present(cdev)) {
1232 mutex_unlock(&thermal_list_lock);
1236 list_del(&cdev->node);
1238 /* Unbind all thermal zones associated with 'this' cdev */
1239 list_for_each_entry(tz, &thermal_tz_list, node) {
1241 tz->ops.unbind(tz, cdev);
1244 mutex_unlock(&thermal_list_lock);
1246 device_unregister(&cdev->device);
1248 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1250 static void bind_tz(struct thermal_zone_device *tz)
1253 struct thermal_cooling_device *pos = NULL;
1258 mutex_lock(&thermal_list_lock);
1260 list_for_each_entry(pos, &thermal_cdev_list, node) {
1261 ret = tz->ops.bind(tz, pos);
1263 print_bind_err_msg(tz, pos, ret);
1266 mutex_unlock(&thermal_list_lock);
1269 static void thermal_set_delay_jiffies(unsigned long *delay_jiffies, int delay_ms)
1271 *delay_jiffies = msecs_to_jiffies(delay_ms);
1272 if (delay_ms > 1000)
1273 *delay_jiffies = round_jiffies(*delay_jiffies);
1276 int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp)
1278 const struct thermal_trip_desc *td;
1281 if (tz->ops.get_crit_temp)
1282 return tz->ops.get_crit_temp(tz, temp);
1284 mutex_lock(&tz->lock);
1286 for_each_trip_desc(tz, td) {
1287 const struct thermal_trip *trip = &td->trip;
1289 if (trip->type == THERMAL_TRIP_CRITICAL) {
1290 *temp = trip->temperature;
1296 mutex_unlock(&tz->lock);
1300 EXPORT_SYMBOL_GPL(thermal_zone_get_crit_temp);
1303 * thermal_zone_device_register_with_trips() - register a new thermal zone device
1304 * @type: the thermal zone device type
1305 * @trips: a pointer to an array of thermal trips
1306 * @num_trips: the number of trip points the thermal zone support
1307 * @devdata: private device data
1308 * @ops: standard thermal zone device callbacks
1309 * @tzp: thermal zone platform parameters
1310 * @passive_delay: number of milliseconds to wait between polls when
1311 * performing passive cooling
1312 * @polling_delay: number of milliseconds to wait between polls when checking
1313 * whether trip points have been crossed (0 for interrupt
1316 * This interface function adds a new thermal zone device (sensor) to
1317 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1318 * thermal cooling devices registered at the same time.
1319 * thermal_zone_device_unregister() must be called when the device is no
1320 * longer needed. The passive cooling depends on the .get_trend() return value.
1322 * Return: a pointer to the created struct thermal_zone_device or an
1323 * in case of error, an ERR_PTR. Caller must check return value with
1324 * IS_ERR*() helpers.
1326 struct thermal_zone_device *
1327 thermal_zone_device_register_with_trips(const char *type,
1328 const struct thermal_trip *trips,
1329 int num_trips, void *devdata,
1330 const struct thermal_zone_device_ops *ops,
1331 const struct thermal_zone_params *tzp,
1332 int passive_delay, int polling_delay)
1334 const struct thermal_trip *trip = trips;
1335 struct thermal_zone_device *tz;
1336 struct thermal_trip_desc *td;
1339 struct thermal_governor *governor;
1341 if (!type || strlen(type) == 0) {
1342 pr_err("No thermal zone type defined\n");
1343 return ERR_PTR(-EINVAL);
1346 if (strlen(type) >= THERMAL_NAME_LENGTH) {
1347 pr_err("Thermal zone name (%s) too long, should be under %d chars\n",
1348 type, THERMAL_NAME_LENGTH);
1349 return ERR_PTR(-EINVAL);
1352 if (num_trips < 0) {
1353 pr_err("Incorrect number of thermal trips\n");
1354 return ERR_PTR(-EINVAL);
1357 if (!ops || !ops->get_temp) {
1358 pr_err("Thermal zone device ops not defined\n");
1359 return ERR_PTR(-EINVAL);
1362 if (num_trips > 0 && !trips)
1363 return ERR_PTR(-EINVAL);
1366 return ERR_PTR(-ENODEV);
1368 tz = kzalloc(struct_size(tz, trips, num_trips), GFP_KERNEL);
1370 return ERR_PTR(-ENOMEM);
1373 tz->tzp = kmemdup(tzp, sizeof(*tzp), GFP_KERNEL);
1380 INIT_LIST_HEAD(&tz->thermal_instances);
1381 INIT_LIST_HEAD(&tz->node);
1383 mutex_init(&tz->lock);
1384 init_completion(&tz->removal);
1385 id = ida_alloc(&thermal_tz_ida, GFP_KERNEL);
1392 strscpy(tz->type, type, sizeof(tz->type));
1395 if (!tz->ops.critical)
1396 tz->ops.critical = thermal_zone_device_critical;
1398 tz->device.class = thermal_class;
1399 tz->devdata = devdata;
1400 tz->num_trips = num_trips;
1401 for_each_trip_desc(tz, td) {
1404 * Mark all thresholds as invalid to start with even though
1405 * this only matters for the trips that start as invalid and
1406 * become valid later.
1408 td->threshold = INT_MAX;
1411 thermal_set_delay_jiffies(&tz->passive_delay_jiffies, passive_delay);
1412 thermal_set_delay_jiffies(&tz->polling_delay_jiffies, polling_delay);
1415 /* Add nodes that are always present via .groups */
1416 result = thermal_zone_create_device_groups(tz);
1420 /* A new thermal zone needs to be updated anyway. */
1421 atomic_set(&tz->need_update, 1);
1423 result = dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1425 thermal_zone_destroy_device_groups(tz);
1428 result = device_register(&tz->device);
1430 goto release_device;
1432 /* Update 'this' zone's governor information */
1433 mutex_lock(&thermal_governor_lock);
1436 governor = __find_governor(tz->tzp->governor_name);
1438 governor = def_governor;
1440 result = thermal_set_governor(tz, governor);
1442 mutex_unlock(&thermal_governor_lock);
1446 mutex_unlock(&thermal_governor_lock);
1448 if (!tz->tzp || !tz->tzp->no_hwmon) {
1449 result = thermal_add_hwmon_sysfs(tz);
1454 mutex_lock(&thermal_list_lock);
1455 mutex_lock(&tz->lock);
1456 list_add_tail(&tz->node, &thermal_tz_list);
1457 mutex_unlock(&tz->lock);
1458 mutex_unlock(&thermal_list_lock);
1460 /* Bind cooling devices for this zone */
1463 thermal_zone_device_init(tz);
1464 /* Update the new thermal zone and mark it as already updated. */
1465 if (atomic_cmpxchg(&tz->need_update, 1, 0))
1466 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1468 thermal_notify_tz_create(tz);
1470 thermal_debug_tz_add(tz);
1475 device_del(&tz->device);
1477 put_device(&tz->device);
1479 ida_free(&thermal_tz_ida, id);
1484 return ERR_PTR(result);
1486 EXPORT_SYMBOL_GPL(thermal_zone_device_register_with_trips);
1488 struct thermal_zone_device *thermal_tripless_zone_device_register(
1491 const struct thermal_zone_device_ops *ops,
1492 const struct thermal_zone_params *tzp)
1494 return thermal_zone_device_register_with_trips(type, NULL, 0, devdata,
1497 EXPORT_SYMBOL_GPL(thermal_tripless_zone_device_register);
1499 void *thermal_zone_device_priv(struct thermal_zone_device *tzd)
1501 return tzd->devdata;
1503 EXPORT_SYMBOL_GPL(thermal_zone_device_priv);
1505 const char *thermal_zone_device_type(struct thermal_zone_device *tzd)
1509 EXPORT_SYMBOL_GPL(thermal_zone_device_type);
1511 int thermal_zone_device_id(struct thermal_zone_device *tzd)
1515 EXPORT_SYMBOL_GPL(thermal_zone_device_id);
1517 struct device *thermal_zone_device(struct thermal_zone_device *tzd)
1519 return &tzd->device;
1521 EXPORT_SYMBOL_GPL(thermal_zone_device);
1524 * thermal_zone_device_unregister - removes the registered thermal zone device
1525 * @tz: the thermal zone device to remove
1527 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1529 struct thermal_cooling_device *cdev;
1530 struct thermal_zone_device *pos = NULL;
1535 thermal_debug_tz_remove(tz);
1537 mutex_lock(&thermal_list_lock);
1538 list_for_each_entry(pos, &thermal_tz_list, node)
1542 /* thermal zone device not found */
1543 mutex_unlock(&thermal_list_lock);
1547 mutex_lock(&tz->lock);
1548 list_del(&tz->node);
1549 mutex_unlock(&tz->lock);
1551 /* Unbind all cdevs associated with 'this' thermal zone */
1552 list_for_each_entry(cdev, &thermal_cdev_list, node)
1554 tz->ops.unbind(tz, cdev);
1556 mutex_unlock(&thermal_list_lock);
1558 cancel_delayed_work_sync(&tz->poll_queue);
1560 thermal_set_governor(tz, NULL);
1562 thermal_remove_hwmon_sysfs(tz);
1563 ida_free(&thermal_tz_ida, tz->id);
1564 ida_destroy(&tz->ida);
1566 device_del(&tz->device);
1570 put_device(&tz->device);
1572 thermal_notify_tz_delete(tz);
1574 wait_for_completion(&tz->removal);
1577 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1580 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1581 * @name: thermal zone name to fetch the temperature
1583 * When only one zone is found with the passed name, returns a reference to it.
1585 * Return: On success returns a reference to an unique thermal zone with
1586 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1587 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1589 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1591 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1592 unsigned int found = 0;
1597 mutex_lock(&thermal_list_lock);
1598 list_for_each_entry(pos, &thermal_tz_list, node)
1599 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1603 mutex_unlock(&thermal_list_lock);
1605 /* nothing has been found, thus an error code for it */
1607 ref = ERR_PTR(-ENODEV);
1609 /* Success only when an unique zone is found */
1610 ref = ERR_PTR(-EEXIST);
1615 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1617 static void thermal_zone_device_resume(struct work_struct *work)
1619 struct thermal_zone_device *tz;
1621 tz = container_of(work, struct thermal_zone_device, poll_queue.work);
1623 mutex_lock(&tz->lock);
1625 tz->suspended = false;
1627 thermal_zone_device_init(tz);
1628 __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1630 mutex_unlock(&tz->lock);
1633 static int thermal_pm_notify(struct notifier_block *nb,
1634 unsigned long mode, void *_unused)
1636 struct thermal_zone_device *tz;
1639 case PM_HIBERNATION_PREPARE:
1640 case PM_RESTORE_PREPARE:
1641 case PM_SUSPEND_PREPARE:
1642 mutex_lock(&thermal_list_lock);
1644 list_for_each_entry(tz, &thermal_tz_list, node) {
1645 mutex_lock(&tz->lock);
1647 tz->suspended = true;
1649 mutex_unlock(&tz->lock);
1652 mutex_unlock(&thermal_list_lock);
1654 case PM_POST_HIBERNATION:
1655 case PM_POST_RESTORE:
1656 case PM_POST_SUSPEND:
1657 mutex_lock(&thermal_list_lock);
1659 list_for_each_entry(tz, &thermal_tz_list, node) {
1660 mutex_lock(&tz->lock);
1662 cancel_delayed_work(&tz->poll_queue);
1665 * Replace the work function with the resume one, which
1666 * will restore the original work function and schedule
1667 * the polling work if needed.
1669 INIT_DELAYED_WORK(&tz->poll_queue,
1670 thermal_zone_device_resume);
1671 /* Queue up the work without a delay. */
1672 mod_delayed_work(system_freezable_power_efficient_wq,
1673 &tz->poll_queue, 0);
1675 mutex_unlock(&tz->lock);
1678 mutex_unlock(&thermal_list_lock);
1686 static struct notifier_block thermal_pm_nb = {
1687 .notifier_call = thermal_pm_notify,
1690 static int __init thermal_init(void)
1694 thermal_debug_init();
1696 result = thermal_netlink_init();
1700 result = thermal_register_governors();
1702 goto unregister_netlink;
1704 thermal_class = kzalloc(sizeof(*thermal_class), GFP_KERNEL);
1705 if (!thermal_class) {
1707 goto unregister_governors;
1710 thermal_class->name = "thermal";
1711 thermal_class->dev_release = thermal_release;
1713 result = class_register(thermal_class);
1715 kfree(thermal_class);
1716 thermal_class = NULL;
1717 goto unregister_governors;
1720 result = register_pm_notifier(&thermal_pm_nb);
1722 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1727 unregister_governors:
1728 thermal_unregister_governors();
1730 thermal_netlink_exit();
1732 mutex_destroy(&thermal_list_lock);
1733 mutex_destroy(&thermal_governor_lock);
1736 postcore_initcall(thermal_init);