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);
275 static int __thermal_zone_device_set_mode(struct thermal_zone_device *tz,
276 enum thermal_device_mode mode)
278 if (tz->ops.change_mode) {
281 ret = tz->ops.change_mode(tz, mode);
291 static void thermal_zone_broken_disable(struct thermal_zone_device *tz)
293 struct thermal_trip_desc *td;
295 dev_err(&tz->device, "Unable to get temperature, disabling!\n");
297 * This function only runs for enabled thermal zones, so no need to
298 * check for the current mode.
300 __thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED);
301 thermal_notify_tz_disable(tz);
303 for_each_trip_desc(tz, td) {
304 if (td->trip.type == THERMAL_TRIP_CRITICAL &&
305 td->trip.temperature > THERMAL_TEMP_INVALID) {
306 dev_crit(&tz->device,
307 "Disabled thermal zone with critical trip point\n");
314 * Zone update section: main control loop applied to each zone while monitoring
315 * in polling mode. The monitoring is done using a workqueue.
316 * Same update may be done on a zone by calling thermal_zone_device_update().
319 * - Non-critical trips will invoke the governor responsible for that zone;
320 * - Hot trips will produce a notification to userspace;
321 * - Critical trip point will cause a system shutdown.
323 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
327 mod_delayed_work(system_freezable_power_efficient_wq,
328 &tz->poll_queue, delay);
330 cancel_delayed_work(&tz->poll_queue);
333 static void thermal_zone_recheck(struct thermal_zone_device *tz, int error)
335 if (error == -EAGAIN) {
336 thermal_zone_device_set_polling(tz, THERMAL_RECHECK_DELAY);
341 * Print the message once to reduce log noise. It will be followed by
342 * another one if the temperature cannot be determined after multiple
345 if (tz->recheck_delay_jiffies == THERMAL_RECHECK_DELAY)
346 dev_info(&tz->device, "Temperature check failed (%d)\n", error);
348 thermal_zone_device_set_polling(tz, tz->recheck_delay_jiffies);
350 tz->recheck_delay_jiffies += max(tz->recheck_delay_jiffies >> 1, 1ULL);
351 if (tz->recheck_delay_jiffies > THERMAL_MAX_RECHECK_DELAY) {
352 thermal_zone_broken_disable(tz);
354 * Restore the original recheck delay value to allow the thermal
355 * zone to try to recover when it is reenabled by user space.
357 tz->recheck_delay_jiffies = THERMAL_RECHECK_DELAY;
361 static void monitor_thermal_zone(struct thermal_zone_device *tz)
363 if (tz->mode != THERMAL_DEVICE_ENABLED)
364 thermal_zone_device_set_polling(tz, 0);
365 else if (tz->passive > 0)
366 thermal_zone_device_set_polling(tz, tz->passive_delay_jiffies);
367 else if (tz->polling_delay_jiffies)
368 thermal_zone_device_set_polling(tz, tz->polling_delay_jiffies);
371 static struct thermal_governor *thermal_get_tz_governor(struct thermal_zone_device *tz)
379 void thermal_governor_update_tz(struct thermal_zone_device *tz,
380 enum thermal_notify_event reason)
382 if (!tz->governor || !tz->governor->update_tz)
385 tz->governor->update_tz(tz, reason);
388 static void thermal_zone_device_halt(struct thermal_zone_device *tz, bool shutdown)
391 * poweroff_delay_ms must be a carefully profiled positive value.
392 * Its a must for forced_emergency_poweroff_work to be scheduled.
394 int poweroff_delay_ms = CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS;
395 const char *msg = "Temperature too high";
397 dev_emerg(&tz->device, "%s: critical temperature reached\n", tz->type);
400 hw_protection_shutdown(msg, poweroff_delay_ms);
402 hw_protection_reboot(msg, poweroff_delay_ms);
405 void thermal_zone_device_critical(struct thermal_zone_device *tz)
407 thermal_zone_device_halt(tz, true);
409 EXPORT_SYMBOL(thermal_zone_device_critical);
411 void thermal_zone_device_critical_reboot(struct thermal_zone_device *tz)
413 thermal_zone_device_halt(tz, false);
416 static void handle_critical_trips(struct thermal_zone_device *tz,
417 const struct thermal_trip *trip)
419 trace_thermal_zone_trip(tz, thermal_zone_trip_id(tz, trip), trip->type);
421 if (trip->type == THERMAL_TRIP_CRITICAL)
422 tz->ops.critical(tz);
423 else if (tz->ops.hot)
427 static void handle_thermal_trip(struct thermal_zone_device *tz,
428 struct thermal_trip_desc *td,
429 struct list_head *way_up_list,
430 struct list_head *way_down_list)
432 const struct thermal_trip *trip = &td->trip;
435 if (trip->temperature == THERMAL_TEMP_INVALID)
439 * If the trip temperature or hysteresis has been updated recently,
440 * the threshold needs to be computed again using the new values.
441 * However, its initial value still reflects the old ones and that
442 * is what needs to be compared with the previous zone temperature
443 * to decide which action to take.
445 old_threshold = td->threshold;
446 td->threshold = trip->temperature;
448 if (tz->last_temperature >= old_threshold &&
449 tz->last_temperature != THERMAL_TEMP_INIT) {
451 * Mitigation is under way, so it needs to stop if the zone
452 * temperature falls below the low temperature of the trip.
453 * In that case, the trip temperature becomes the new threshold.
455 if (tz->temperature < trip->temperature - trip->hysteresis) {
456 list_add(&td->notify_list_node, way_down_list);
457 td->notify_temp = trip->temperature - trip->hysteresis;
459 if (trip->type == THERMAL_TRIP_PASSIVE) {
461 WARN_ON(tz->passive < 0);
464 td->threshold -= trip->hysteresis;
466 } else if (tz->temperature >= trip->temperature) {
468 * There is no mitigation under way, so it needs to be started
469 * if the zone temperature exceeds the trip one. The new
470 * threshold is then set to the low temperature of the trip.
472 list_add_tail(&td->notify_list_node, way_up_list);
473 td->notify_temp = trip->temperature;
474 td->threshold -= trip->hysteresis;
476 if (trip->type == THERMAL_TRIP_PASSIVE)
478 else if (trip->type == THERMAL_TRIP_CRITICAL ||
479 trip->type == THERMAL_TRIP_HOT)
480 handle_critical_trips(tz, trip);
484 static void thermal_zone_device_check(struct work_struct *work)
486 struct thermal_zone_device *tz = container_of(work, struct
489 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
492 static void thermal_zone_device_init(struct thermal_zone_device *tz)
494 struct thermal_instance *pos;
496 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
498 tz->temperature = THERMAL_TEMP_INIT;
500 tz->prev_low_trip = -INT_MAX;
501 tz->prev_high_trip = INT_MAX;
502 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
503 pos->initialized = false;
506 static void thermal_governor_trip_crossed(struct thermal_governor *governor,
507 struct thermal_zone_device *tz,
508 const struct thermal_trip *trip,
511 if (trip->type == THERMAL_TRIP_HOT || trip->type == THERMAL_TRIP_CRITICAL)
514 if (governor->trip_crossed)
515 governor->trip_crossed(tz, trip, crossed_up);
518 static void thermal_trip_crossed(struct thermal_zone_device *tz,
519 const struct thermal_trip *trip,
520 struct thermal_governor *governor,
524 thermal_notify_tz_trip_up(tz, trip);
525 thermal_debug_tz_trip_up(tz, trip);
527 thermal_notify_tz_trip_down(tz, trip);
528 thermal_debug_tz_trip_down(tz, trip);
530 thermal_governor_trip_crossed(governor, tz, trip, crossed_up);
533 static int thermal_trip_notify_cmp(void *not_used, const struct list_head *a,
534 const struct list_head *b)
536 struct thermal_trip_desc *tda = container_of(a, struct thermal_trip_desc,
538 struct thermal_trip_desc *tdb = container_of(b, struct thermal_trip_desc,
540 return tda->notify_temp - tdb->notify_temp;
543 void __thermal_zone_device_update(struct thermal_zone_device *tz,
544 enum thermal_notify_event event)
546 struct thermal_governor *governor = thermal_get_tz_governor(tz);
547 struct thermal_trip_desc *td;
548 LIST_HEAD(way_down_list);
549 LIST_HEAD(way_up_list);
555 if (!thermal_zone_device_is_enabled(tz))
558 ret = __thermal_zone_get_temp(tz, &temp);
560 thermal_zone_recheck(tz, ret);
562 } else if (temp <= THERMAL_TEMP_INVALID) {
564 * Special case: No valid temperature value is available, but
565 * the zone owner does not want the core to do anything about
566 * it. Continue regular zone polling if needed, so that this
567 * function can be called again, but skip everything else.
572 tz->recheck_delay_jiffies = THERMAL_RECHECK_DELAY;
574 tz->last_temperature = tz->temperature;
575 tz->temperature = temp;
577 trace_thermal_temperature(tz);
579 thermal_genl_sampling_temp(tz->id, temp);
581 tz->notify_event = event;
583 for_each_trip_desc(tz, td)
584 handle_thermal_trip(tz, td, &way_up_list, &way_down_list);
586 thermal_zone_set_trips(tz);
588 list_sort(NULL, &way_up_list, thermal_trip_notify_cmp);
589 list_for_each_entry(td, &way_up_list, notify_list_node)
590 thermal_trip_crossed(tz, &td->trip, governor, true);
592 list_sort(NULL, &way_down_list, thermal_trip_notify_cmp);
593 list_for_each_entry_reverse(td, &way_down_list, notify_list_node)
594 thermal_trip_crossed(tz, &td->trip, governor, false);
596 if (governor->manage)
597 governor->manage(tz);
599 thermal_debug_update_trip_stats(tz);
602 monitor_thermal_zone(tz);
605 static int thermal_zone_device_set_mode(struct thermal_zone_device *tz,
606 enum thermal_device_mode mode)
610 mutex_lock(&tz->lock);
612 /* do nothing if mode isn't changing */
613 if (mode == tz->mode) {
614 mutex_unlock(&tz->lock);
619 ret = __thermal_zone_device_set_mode(tz, mode);
621 mutex_unlock(&tz->lock);
626 __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
628 mutex_unlock(&tz->lock);
630 if (mode == THERMAL_DEVICE_ENABLED)
631 thermal_notify_tz_enable(tz);
633 thermal_notify_tz_disable(tz);
638 int thermal_zone_device_enable(struct thermal_zone_device *tz)
640 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_ENABLED);
642 EXPORT_SYMBOL_GPL(thermal_zone_device_enable);
644 int thermal_zone_device_disable(struct thermal_zone_device *tz)
646 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED);
648 EXPORT_SYMBOL_GPL(thermal_zone_device_disable);
650 int thermal_zone_device_is_enabled(struct thermal_zone_device *tz)
652 lockdep_assert_held(&tz->lock);
654 return tz->mode == THERMAL_DEVICE_ENABLED;
657 static bool thermal_zone_is_present(struct thermal_zone_device *tz)
659 return !list_empty(&tz->node);
662 void thermal_zone_device_update(struct thermal_zone_device *tz,
663 enum thermal_notify_event event)
665 mutex_lock(&tz->lock);
666 if (thermal_zone_is_present(tz))
667 __thermal_zone_device_update(tz, event);
668 mutex_unlock(&tz->lock);
670 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
672 void thermal_zone_trip_down(struct thermal_zone_device *tz,
673 const struct thermal_trip *trip)
675 thermal_trip_crossed(tz, trip, thermal_get_tz_governor(tz), false);
678 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
681 struct thermal_governor *gov;
684 mutex_lock(&thermal_governor_lock);
685 list_for_each_entry(gov, &thermal_governor_list, governor_list) {
690 mutex_unlock(&thermal_governor_lock);
695 int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
698 struct thermal_cooling_device *cdev;
701 mutex_lock(&thermal_list_lock);
702 list_for_each_entry(cdev, &thermal_cdev_list, node) {
703 ret = cb(cdev, data);
707 mutex_unlock(&thermal_list_lock);
712 int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
715 struct thermal_zone_device *tz;
718 mutex_lock(&thermal_list_lock);
719 list_for_each_entry(tz, &thermal_tz_list, node) {
724 mutex_unlock(&thermal_list_lock);
729 struct thermal_zone_device *thermal_zone_get_by_id(int id)
731 struct thermal_zone_device *tz, *match = NULL;
733 mutex_lock(&thermal_list_lock);
734 list_for_each_entry(tz, &thermal_tz_list, node) {
740 mutex_unlock(&thermal_list_lock);
746 * Device management section: cooling devices, zones devices, and binding
748 * Set of functions provided by the thermal core for:
749 * - cooling devices lifecycle: registration, unregistration,
750 * binding, and unbinding.
751 * - thermal zone devices lifecycle: registration, unregistration,
752 * binding, and unbinding.
756 * thermal_bind_cdev_to_trip - bind a cooling device to a thermal zone
757 * @tz: pointer to struct thermal_zone_device
758 * @trip: trip point the cooling devices is associated with in this zone.
759 * @cdev: pointer to struct thermal_cooling_device
760 * @upper: the Maximum cooling state for this trip point.
761 * THERMAL_NO_LIMIT means no upper limit,
762 * and the cooling device can be in max_state.
763 * @lower: the Minimum cooling state can be used for this trip point.
764 * THERMAL_NO_LIMIT means no lower limit,
765 * and the cooling device can be in cooling state 0.
766 * @weight: The weight of the cooling device to be bound to the
767 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
770 * This interface function bind a thermal cooling device to the certain trip
771 * point of a thermal zone device.
772 * This function is usually called in the thermal zone device .bind callback.
774 * Return: 0 on success, the proper error value otherwise.
776 int thermal_bind_cdev_to_trip(struct thermal_zone_device *tz,
777 const struct thermal_trip *trip,
778 struct thermal_cooling_device *cdev,
779 unsigned long upper, unsigned long lower,
782 struct thermal_instance *dev;
783 struct thermal_instance *pos;
784 struct thermal_zone_device *pos1;
785 struct thermal_cooling_device *pos2;
789 list_for_each_entry(pos1, &thermal_tz_list, node) {
793 list_for_each_entry(pos2, &thermal_cdev_list, node) {
798 if (tz != pos1 || cdev != pos2)
801 /* lower default 0, upper default max_state */
802 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
804 if (upper == THERMAL_NO_LIMIT) {
805 upper = cdev->max_state;
806 upper_no_limit = true;
808 upper_no_limit = false;
811 if (lower > upper || upper > cdev->max_state)
814 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
821 dev->upper_no_limit = upper_no_limit;
823 dev->target = THERMAL_NO_TARGET;
824 dev->weight = weight;
826 result = ida_alloc(&tz->ida, GFP_KERNEL);
831 sprintf(dev->name, "cdev%d", dev->id);
833 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
837 snprintf(dev->attr_name, sizeof(dev->attr_name), "cdev%d_trip_point",
839 sysfs_attr_init(&dev->attr.attr);
840 dev->attr.attr.name = dev->attr_name;
841 dev->attr.attr.mode = 0444;
842 dev->attr.show = trip_point_show;
843 result = device_create_file(&tz->device, &dev->attr);
845 goto remove_symbol_link;
847 snprintf(dev->weight_attr_name, sizeof(dev->weight_attr_name),
848 "cdev%d_weight", dev->id);
849 sysfs_attr_init(&dev->weight_attr.attr);
850 dev->weight_attr.attr.name = dev->weight_attr_name;
851 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
852 dev->weight_attr.show = weight_show;
853 dev->weight_attr.store = weight_store;
854 result = device_create_file(&tz->device, &dev->weight_attr);
856 goto remove_trip_file;
858 mutex_lock(&tz->lock);
859 mutex_lock(&cdev->lock);
860 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
861 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
866 list_add_tail(&dev->tz_node, &tz->thermal_instances);
867 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
868 atomic_set(&tz->need_update, 1);
870 thermal_governor_update_tz(tz, THERMAL_TZ_BIND_CDEV);
872 mutex_unlock(&cdev->lock);
873 mutex_unlock(&tz->lock);
878 device_remove_file(&tz->device, &dev->weight_attr);
880 device_remove_file(&tz->device, &dev->attr);
882 sysfs_remove_link(&tz->device.kobj, dev->name);
884 ida_free(&tz->ida, dev->id);
889 EXPORT_SYMBOL_GPL(thermal_bind_cdev_to_trip);
891 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
893 struct thermal_cooling_device *cdev,
894 unsigned long upper, unsigned long lower,
897 if (trip_index < 0 || trip_index >= tz->num_trips)
900 return thermal_bind_cdev_to_trip(tz, &tz->trips[trip_index].trip, cdev,
901 upper, lower, weight);
903 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
906 * thermal_unbind_cdev_from_trip - unbind a cooling device from a thermal zone.
907 * @tz: pointer to a struct thermal_zone_device.
908 * @trip: trip point the cooling devices is associated with in this zone.
909 * @cdev: pointer to a struct thermal_cooling_device.
911 * This interface function unbind a thermal cooling device from the certain
912 * trip point of a thermal zone device.
913 * This function is usually called in the thermal zone device .unbind callback.
915 * Return: 0 on success, the proper error value otherwise.
917 int thermal_unbind_cdev_from_trip(struct thermal_zone_device *tz,
918 const struct thermal_trip *trip,
919 struct thermal_cooling_device *cdev)
921 struct thermal_instance *pos, *next;
923 mutex_lock(&tz->lock);
924 mutex_lock(&cdev->lock);
925 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
926 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
927 list_del(&pos->tz_node);
928 list_del(&pos->cdev_node);
930 thermal_governor_update_tz(tz, THERMAL_TZ_UNBIND_CDEV);
932 mutex_unlock(&cdev->lock);
933 mutex_unlock(&tz->lock);
937 mutex_unlock(&cdev->lock);
938 mutex_unlock(&tz->lock);
943 device_remove_file(&tz->device, &pos->weight_attr);
944 device_remove_file(&tz->device, &pos->attr);
945 sysfs_remove_link(&tz->device.kobj, pos->name);
946 ida_free(&tz->ida, pos->id);
950 EXPORT_SYMBOL_GPL(thermal_unbind_cdev_from_trip);
952 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
954 struct thermal_cooling_device *cdev)
956 if (trip_index < 0 || trip_index >= tz->num_trips)
959 return thermal_unbind_cdev_from_trip(tz, &tz->trips[trip_index].trip, cdev);
961 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
963 static void thermal_release(struct device *dev)
965 struct thermal_zone_device *tz;
966 struct thermal_cooling_device *cdev;
968 if (!strncmp(dev_name(dev), "thermal_zone",
969 sizeof("thermal_zone") - 1)) {
970 tz = to_thermal_zone(dev);
971 thermal_zone_destroy_device_groups(tz);
972 mutex_destroy(&tz->lock);
973 complete(&tz->removal);
974 } else if (!strncmp(dev_name(dev), "cooling_device",
975 sizeof("cooling_device") - 1)) {
976 cdev = to_cooling_device(dev);
977 thermal_cooling_device_destroy_sysfs(cdev);
978 kfree_const(cdev->type);
979 ida_free(&thermal_cdev_ida, cdev->id);
984 static struct class *thermal_class;
987 void print_bind_err_msg(struct thermal_zone_device *tz,
988 struct thermal_cooling_device *cdev, int ret)
990 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
991 tz->type, cdev->type, ret);
994 static void bind_cdev(struct thermal_cooling_device *cdev)
997 struct thermal_zone_device *pos = NULL;
999 list_for_each_entry(pos, &thermal_tz_list, node) {
1000 if (pos->ops.bind) {
1001 ret = pos->ops.bind(pos, cdev);
1003 print_bind_err_msg(pos, cdev, ret);
1009 * __thermal_cooling_device_register() - register a new thermal cooling device
1010 * @np: a pointer to a device tree node.
1011 * @type: the thermal cooling device type.
1012 * @devdata: device private data.
1013 * @ops: standard thermal cooling devices callbacks.
1015 * This interface function adds a new thermal cooling device (fan/processor/...)
1016 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1017 * to all the thermal zone devices registered at the same time.
1018 * It also gives the opportunity to link the cooling device to a device tree
1019 * node, so that it can be bound to a thermal zone created out of device tree.
1021 * Return: a pointer to the created struct thermal_cooling_device or an
1022 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1024 static struct thermal_cooling_device *
1025 __thermal_cooling_device_register(struct device_node *np,
1026 const char *type, void *devdata,
1027 const struct thermal_cooling_device_ops *ops)
1029 struct thermal_cooling_device *cdev;
1030 struct thermal_zone_device *pos = NULL;
1031 unsigned long current_state;
1034 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
1035 !ops->set_cur_state)
1036 return ERR_PTR(-EINVAL);
1039 return ERR_PTR(-ENODEV);
1041 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
1043 return ERR_PTR(-ENOMEM);
1045 ret = ida_alloc(&thermal_cdev_ida, GFP_KERNEL);
1047 goto out_kfree_cdev;
1051 cdev->type = kstrdup_const(type ? type : "", GFP_KERNEL);
1054 goto out_ida_remove;
1057 mutex_init(&cdev->lock);
1058 INIT_LIST_HEAD(&cdev->thermal_instances);
1061 cdev->updated = false;
1062 cdev->device.class = thermal_class;
1063 cdev->devdata = devdata;
1065 ret = cdev->ops->get_max_state(cdev, &cdev->max_state);
1070 * The cooling device's current state is only needed for debug
1071 * initialization below, so a failure to get it does not cause
1072 * the entire cooling device initialization to fail. However,
1073 * the debug will not work for the device if its initial state
1074 * cannot be determined and drivers are responsible for ensuring
1075 * that this will not happen.
1077 ret = cdev->ops->get_cur_state(cdev, ¤t_state);
1079 current_state = ULONG_MAX;
1081 thermal_cooling_device_setup_sysfs(cdev);
1083 ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
1085 goto out_cooling_dev;
1087 ret = device_register(&cdev->device);
1089 /* thermal_release() handles rest of the cleanup */
1090 put_device(&cdev->device);
1091 return ERR_PTR(ret);
1094 if (current_state <= cdev->max_state)
1095 thermal_debug_cdev_add(cdev, current_state);
1097 /* Add 'this' new cdev to the global cdev list */
1098 mutex_lock(&thermal_list_lock);
1100 list_add(&cdev->node, &thermal_cdev_list);
1102 /* Update binding information for 'this' new cdev */
1105 list_for_each_entry(pos, &thermal_tz_list, node)
1106 if (atomic_cmpxchg(&pos->need_update, 1, 0))
1107 thermal_zone_device_update(pos,
1108 THERMAL_EVENT_UNSPECIFIED);
1110 mutex_unlock(&thermal_list_lock);
1115 thermal_cooling_device_destroy_sysfs(cdev);
1117 kfree_const(cdev->type);
1119 ida_free(&thermal_cdev_ida, id);
1122 return ERR_PTR(ret);
1126 * thermal_cooling_device_register() - register a new thermal cooling device
1127 * @type: the thermal cooling device type.
1128 * @devdata: device private data.
1129 * @ops: standard thermal cooling devices callbacks.
1131 * This interface function adds a new thermal cooling device (fan/processor/...)
1132 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1133 * to all the thermal zone devices registered at the same time.
1135 * Return: a pointer to the created struct thermal_cooling_device or an
1136 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1138 struct thermal_cooling_device *
1139 thermal_cooling_device_register(const char *type, void *devdata,
1140 const struct thermal_cooling_device_ops *ops)
1142 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1144 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
1147 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1148 * @np: a pointer to a device tree node.
1149 * @type: the thermal cooling device type.
1150 * @devdata: device private data.
1151 * @ops: standard thermal cooling devices callbacks.
1153 * This function will register a cooling device with device tree node reference.
1154 * This interface function adds a new thermal cooling device (fan/processor/...)
1155 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1156 * to all the thermal zone devices registered at the same time.
1158 * Return: a pointer to the created struct thermal_cooling_device or an
1159 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1161 struct thermal_cooling_device *
1162 thermal_of_cooling_device_register(struct device_node *np,
1163 const char *type, void *devdata,
1164 const struct thermal_cooling_device_ops *ops)
1166 return __thermal_cooling_device_register(np, type, devdata, ops);
1168 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1170 static void thermal_cooling_device_release(struct device *dev, void *res)
1172 thermal_cooling_device_unregister(
1173 *(struct thermal_cooling_device **)res);
1177 * devm_thermal_of_cooling_device_register() - register an OF thermal cooling
1179 * @dev: a valid struct device pointer of a sensor device.
1180 * @np: a pointer to a device tree node.
1181 * @type: the thermal cooling device type.
1182 * @devdata: device private data.
1183 * @ops: standard thermal cooling devices callbacks.
1185 * This function will register a cooling device with device tree node reference.
1186 * This interface function adds a new thermal cooling device (fan/processor/...)
1187 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1188 * to all the thermal zone devices registered at the same time.
1190 * Return: a pointer to the created struct thermal_cooling_device or an
1191 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1193 struct thermal_cooling_device *
1194 devm_thermal_of_cooling_device_register(struct device *dev,
1195 struct device_node *np,
1196 const char *type, void *devdata,
1197 const struct thermal_cooling_device_ops *ops)
1199 struct thermal_cooling_device **ptr, *tcd;
1201 ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr),
1204 return ERR_PTR(-ENOMEM);
1206 tcd = __thermal_cooling_device_register(np, type, devdata, ops);
1213 devres_add(dev, ptr);
1217 EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register);
1219 static bool thermal_cooling_device_present(struct thermal_cooling_device *cdev)
1221 struct thermal_cooling_device *pos = NULL;
1223 list_for_each_entry(pos, &thermal_cdev_list, node) {
1232 * thermal_cooling_device_update - Update a cooling device object
1233 * @cdev: Target cooling device.
1235 * Update @cdev to reflect a change of the underlying hardware or platform.
1237 * Must be called when the maximum cooling state of @cdev becomes invalid and so
1238 * its .get_max_state() callback needs to be run to produce the new maximum
1239 * cooling state value.
1241 void thermal_cooling_device_update(struct thermal_cooling_device *cdev)
1243 struct thermal_instance *ti;
1244 unsigned long state;
1246 if (IS_ERR_OR_NULL(cdev))
1250 * Hold thermal_list_lock throughout the update to prevent the device
1251 * from going away while being updated.
1253 mutex_lock(&thermal_list_lock);
1255 if (!thermal_cooling_device_present(cdev))
1259 * Update under the cdev lock to prevent the state from being set beyond
1260 * the new limit concurrently.
1262 mutex_lock(&cdev->lock);
1264 if (cdev->ops->get_max_state(cdev, &cdev->max_state))
1267 thermal_cooling_device_stats_reinit(cdev);
1269 list_for_each_entry(ti, &cdev->thermal_instances, cdev_node) {
1270 if (ti->upper == cdev->max_state)
1273 if (ti->upper < cdev->max_state) {
1274 if (ti->upper_no_limit)
1275 ti->upper = cdev->max_state;
1280 ti->upper = cdev->max_state;
1281 if (ti->lower > ti->upper)
1282 ti->lower = ti->upper;
1284 if (ti->target == THERMAL_NO_TARGET)
1287 if (ti->target > ti->upper)
1288 ti->target = ti->upper;
1291 if (cdev->ops->get_cur_state(cdev, &state) || state > cdev->max_state)
1294 thermal_cooling_device_stats_update(cdev, state);
1297 mutex_unlock(&cdev->lock);
1300 mutex_unlock(&thermal_list_lock);
1302 EXPORT_SYMBOL_GPL(thermal_cooling_device_update);
1305 * thermal_cooling_device_unregister - removes a thermal cooling device
1306 * @cdev: the thermal cooling device to remove.
1308 * thermal_cooling_device_unregister() must be called when a registered
1309 * thermal cooling device is no longer needed.
1311 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1313 struct thermal_zone_device *tz;
1318 thermal_debug_cdev_remove(cdev);
1320 mutex_lock(&thermal_list_lock);
1322 if (!thermal_cooling_device_present(cdev)) {
1323 mutex_unlock(&thermal_list_lock);
1327 list_del(&cdev->node);
1329 /* Unbind all thermal zones associated with 'this' cdev */
1330 list_for_each_entry(tz, &thermal_tz_list, node) {
1332 tz->ops.unbind(tz, cdev);
1335 mutex_unlock(&thermal_list_lock);
1337 device_unregister(&cdev->device);
1339 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1341 static void bind_tz(struct thermal_zone_device *tz)
1344 struct thermal_cooling_device *pos = NULL;
1349 mutex_lock(&thermal_list_lock);
1351 list_for_each_entry(pos, &thermal_cdev_list, node) {
1352 ret = tz->ops.bind(tz, pos);
1354 print_bind_err_msg(tz, pos, ret);
1357 mutex_unlock(&thermal_list_lock);
1360 static void thermal_set_delay_jiffies(unsigned long *delay_jiffies, int delay_ms)
1362 *delay_jiffies = msecs_to_jiffies(delay_ms);
1363 if (delay_ms > 1000)
1364 *delay_jiffies = round_jiffies(*delay_jiffies);
1367 int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp)
1369 const struct thermal_trip_desc *td;
1372 if (tz->ops.get_crit_temp)
1373 return tz->ops.get_crit_temp(tz, temp);
1375 mutex_lock(&tz->lock);
1377 for_each_trip_desc(tz, td) {
1378 const struct thermal_trip *trip = &td->trip;
1380 if (trip->type == THERMAL_TRIP_CRITICAL) {
1381 *temp = trip->temperature;
1387 mutex_unlock(&tz->lock);
1391 EXPORT_SYMBOL_GPL(thermal_zone_get_crit_temp);
1394 * thermal_zone_device_register_with_trips() - register a new thermal zone device
1395 * @type: the thermal zone device type
1396 * @trips: a pointer to an array of thermal trips
1397 * @num_trips: the number of trip points the thermal zone support
1398 * @devdata: private device data
1399 * @ops: standard thermal zone device callbacks
1400 * @tzp: thermal zone platform parameters
1401 * @passive_delay: number of milliseconds to wait between polls when
1402 * performing passive cooling
1403 * @polling_delay: number of milliseconds to wait between polls when checking
1404 * whether trip points have been crossed (0 for interrupt
1407 * This interface function adds a new thermal zone device (sensor) to
1408 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1409 * thermal cooling devices registered at the same time.
1410 * thermal_zone_device_unregister() must be called when the device is no
1411 * longer needed. The passive cooling depends on the .get_trend() return value.
1413 * Return: a pointer to the created struct thermal_zone_device or an
1414 * in case of error, an ERR_PTR. Caller must check return value with
1415 * IS_ERR*() helpers.
1417 struct thermal_zone_device *
1418 thermal_zone_device_register_with_trips(const char *type,
1419 const struct thermal_trip *trips,
1420 int num_trips, void *devdata,
1421 const struct thermal_zone_device_ops *ops,
1422 const struct thermal_zone_params *tzp,
1423 unsigned int passive_delay,
1424 unsigned int polling_delay)
1426 const struct thermal_trip *trip = trips;
1427 struct thermal_zone_device *tz;
1428 struct thermal_trip_desc *td;
1431 struct thermal_governor *governor;
1433 if (!type || strlen(type) == 0) {
1434 pr_err("No thermal zone type defined\n");
1435 return ERR_PTR(-EINVAL);
1438 if (strlen(type) >= THERMAL_NAME_LENGTH) {
1439 pr_err("Thermal zone name (%s) too long, should be under %d chars\n",
1440 type, THERMAL_NAME_LENGTH);
1441 return ERR_PTR(-EINVAL);
1444 if (num_trips < 0) {
1445 pr_err("Incorrect number of thermal trips\n");
1446 return ERR_PTR(-EINVAL);
1449 if (!ops || !ops->get_temp) {
1450 pr_err("Thermal zone device ops not defined\n");
1451 return ERR_PTR(-EINVAL);
1454 if (num_trips > 0 && !trips)
1455 return ERR_PTR(-EINVAL);
1457 if (polling_delay) {
1458 if (passive_delay > polling_delay)
1459 return ERR_PTR(-EINVAL);
1462 passive_delay = polling_delay;
1466 return ERR_PTR(-ENODEV);
1468 tz = kzalloc(struct_size(tz, trips, num_trips), GFP_KERNEL);
1470 return ERR_PTR(-ENOMEM);
1473 tz->tzp = kmemdup(tzp, sizeof(*tzp), GFP_KERNEL);
1480 INIT_LIST_HEAD(&tz->thermal_instances);
1481 INIT_LIST_HEAD(&tz->node);
1483 mutex_init(&tz->lock);
1484 init_completion(&tz->removal);
1485 init_completion(&tz->resume);
1486 id = ida_alloc(&thermal_tz_ida, GFP_KERNEL);
1493 strscpy(tz->type, type, sizeof(tz->type));
1496 if (!tz->ops.critical)
1497 tz->ops.critical = thermal_zone_device_critical;
1499 tz->device.class = thermal_class;
1500 tz->devdata = devdata;
1501 tz->num_trips = num_trips;
1502 for_each_trip_desc(tz, td) {
1505 * Mark all thresholds as invalid to start with even though
1506 * this only matters for the trips that start as invalid and
1507 * become valid later.
1509 td->threshold = INT_MAX;
1512 thermal_set_delay_jiffies(&tz->passive_delay_jiffies, passive_delay);
1513 thermal_set_delay_jiffies(&tz->polling_delay_jiffies, polling_delay);
1514 tz->recheck_delay_jiffies = THERMAL_RECHECK_DELAY;
1517 /* Add nodes that are always present via .groups */
1518 result = thermal_zone_create_device_groups(tz);
1522 /* A new thermal zone needs to be updated anyway. */
1523 atomic_set(&tz->need_update, 1);
1525 result = dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1527 thermal_zone_destroy_device_groups(tz);
1530 result = device_register(&tz->device);
1532 goto release_device;
1534 /* Update 'this' zone's governor information */
1535 mutex_lock(&thermal_governor_lock);
1538 governor = __find_governor(tz->tzp->governor_name);
1540 governor = def_governor;
1542 result = thermal_set_governor(tz, governor);
1544 mutex_unlock(&thermal_governor_lock);
1548 mutex_unlock(&thermal_governor_lock);
1550 if (!tz->tzp || !tz->tzp->no_hwmon) {
1551 result = thermal_add_hwmon_sysfs(tz);
1556 mutex_lock(&thermal_list_lock);
1557 mutex_lock(&tz->lock);
1558 list_add_tail(&tz->node, &thermal_tz_list);
1559 mutex_unlock(&tz->lock);
1560 mutex_unlock(&thermal_list_lock);
1562 /* Bind cooling devices for this zone */
1565 thermal_zone_device_init(tz);
1566 /* Update the new thermal zone and mark it as already updated. */
1567 if (atomic_cmpxchg(&tz->need_update, 1, 0))
1568 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1570 thermal_notify_tz_create(tz);
1572 thermal_debug_tz_add(tz);
1577 device_del(&tz->device);
1579 put_device(&tz->device);
1581 ida_free(&thermal_tz_ida, id);
1586 return ERR_PTR(result);
1588 EXPORT_SYMBOL_GPL(thermal_zone_device_register_with_trips);
1590 struct thermal_zone_device *thermal_tripless_zone_device_register(
1593 const struct thermal_zone_device_ops *ops,
1594 const struct thermal_zone_params *tzp)
1596 return thermal_zone_device_register_with_trips(type, NULL, 0, devdata,
1599 EXPORT_SYMBOL_GPL(thermal_tripless_zone_device_register);
1601 void *thermal_zone_device_priv(struct thermal_zone_device *tzd)
1603 return tzd->devdata;
1605 EXPORT_SYMBOL_GPL(thermal_zone_device_priv);
1607 const char *thermal_zone_device_type(struct thermal_zone_device *tzd)
1611 EXPORT_SYMBOL_GPL(thermal_zone_device_type);
1613 int thermal_zone_device_id(struct thermal_zone_device *tzd)
1617 EXPORT_SYMBOL_GPL(thermal_zone_device_id);
1619 struct device *thermal_zone_device(struct thermal_zone_device *tzd)
1621 return &tzd->device;
1623 EXPORT_SYMBOL_GPL(thermal_zone_device);
1626 * thermal_zone_device_unregister - removes the registered thermal zone device
1627 * @tz: the thermal zone device to remove
1629 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1631 struct thermal_cooling_device *cdev;
1632 struct thermal_zone_device *pos = NULL;
1637 thermal_debug_tz_remove(tz);
1639 mutex_lock(&thermal_list_lock);
1640 list_for_each_entry(pos, &thermal_tz_list, node)
1644 /* thermal zone device not found */
1645 mutex_unlock(&thermal_list_lock);
1649 mutex_lock(&tz->lock);
1650 list_del(&tz->node);
1651 mutex_unlock(&tz->lock);
1653 /* Unbind all cdevs associated with 'this' thermal zone */
1654 list_for_each_entry(cdev, &thermal_cdev_list, node)
1656 tz->ops.unbind(tz, cdev);
1658 mutex_unlock(&thermal_list_lock);
1660 cancel_delayed_work_sync(&tz->poll_queue);
1662 thermal_set_governor(tz, NULL);
1664 thermal_remove_hwmon_sysfs(tz);
1665 ida_free(&thermal_tz_ida, tz->id);
1666 ida_destroy(&tz->ida);
1668 device_del(&tz->device);
1672 put_device(&tz->device);
1674 thermal_notify_tz_delete(tz);
1676 wait_for_completion(&tz->removal);
1679 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1682 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1683 * @name: thermal zone name to fetch the temperature
1685 * When only one zone is found with the passed name, returns a reference to it.
1687 * Return: On success returns a reference to an unique thermal zone with
1688 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1689 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1691 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1693 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1694 unsigned int found = 0;
1699 mutex_lock(&thermal_list_lock);
1700 list_for_each_entry(pos, &thermal_tz_list, node)
1701 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1705 mutex_unlock(&thermal_list_lock);
1707 /* nothing has been found, thus an error code for it */
1709 ref = ERR_PTR(-ENODEV);
1711 /* Success only when an unique zone is found */
1712 ref = ERR_PTR(-EEXIST);
1717 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1719 static void thermal_zone_device_resume(struct work_struct *work)
1721 struct thermal_zone_device *tz;
1723 tz = container_of(work, struct thermal_zone_device, poll_queue.work);
1725 mutex_lock(&tz->lock);
1727 tz->suspended = false;
1729 thermal_debug_tz_resume(tz);
1730 thermal_zone_device_init(tz);
1731 thermal_governor_update_tz(tz, THERMAL_TZ_RESUME);
1732 __thermal_zone_device_update(tz, THERMAL_TZ_RESUME);
1734 complete(&tz->resume);
1735 tz->resuming = false;
1737 mutex_unlock(&tz->lock);
1740 static int thermal_pm_notify(struct notifier_block *nb,
1741 unsigned long mode, void *_unused)
1743 struct thermal_zone_device *tz;
1746 case PM_HIBERNATION_PREPARE:
1747 case PM_RESTORE_PREPARE:
1748 case PM_SUSPEND_PREPARE:
1749 mutex_lock(&thermal_list_lock);
1751 list_for_each_entry(tz, &thermal_tz_list, node) {
1752 mutex_lock(&tz->lock);
1756 * thermal_zone_device_resume() queued up for
1757 * this zone has not acquired the lock yet, so
1758 * release it to let the function run and wait
1759 * util it has done the work.
1761 mutex_unlock(&tz->lock);
1763 wait_for_completion(&tz->resume);
1765 mutex_lock(&tz->lock);
1768 tz->suspended = true;
1770 mutex_unlock(&tz->lock);
1773 mutex_unlock(&thermal_list_lock);
1775 case PM_POST_HIBERNATION:
1776 case PM_POST_RESTORE:
1777 case PM_POST_SUSPEND:
1778 mutex_lock(&thermal_list_lock);
1780 list_for_each_entry(tz, &thermal_tz_list, node) {
1781 mutex_lock(&tz->lock);
1783 cancel_delayed_work(&tz->poll_queue);
1785 reinit_completion(&tz->resume);
1786 tz->resuming = true;
1789 * Replace the work function with the resume one, which
1790 * will restore the original work function and schedule
1791 * the polling work if needed.
1793 INIT_DELAYED_WORK(&tz->poll_queue,
1794 thermal_zone_device_resume);
1795 /* Queue up the work without a delay. */
1796 mod_delayed_work(system_freezable_power_efficient_wq,
1797 &tz->poll_queue, 0);
1799 mutex_unlock(&tz->lock);
1802 mutex_unlock(&thermal_list_lock);
1810 static struct notifier_block thermal_pm_nb = {
1811 .notifier_call = thermal_pm_notify,
1813 * Run at the lowest priority to avoid interference between the thermal
1814 * zone resume work items spawned by thermal_pm_notify() and the other
1817 .priority = INT_MIN,
1820 static int __init thermal_init(void)
1824 thermal_debug_init();
1826 result = thermal_netlink_init();
1830 result = thermal_register_governors();
1832 goto unregister_netlink;
1834 thermal_class = kzalloc(sizeof(*thermal_class), GFP_KERNEL);
1835 if (!thermal_class) {
1837 goto unregister_governors;
1840 thermal_class->name = "thermal";
1841 thermal_class->dev_release = thermal_release;
1843 result = class_register(thermal_class);
1845 kfree(thermal_class);
1846 thermal_class = NULL;
1847 goto unregister_governors;
1850 result = register_pm_notifier(&thermal_pm_nb);
1852 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1857 unregister_governors:
1858 thermal_unregister_governors();
1860 thermal_netlink_exit();
1862 mutex_destroy(&thermal_list_lock);
1863 mutex_destroy(&thermal_governor_lock);
1866 postcore_initcall(thermal_init);