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 delay = round_jiffies_relative(delay);
329 mod_delayed_work(system_freezable_power_efficient_wq, &tz->poll_queue, delay);
332 static void thermal_zone_recheck(struct thermal_zone_device *tz, int error)
334 if (error == -EAGAIN) {
335 thermal_zone_device_set_polling(tz, THERMAL_RECHECK_DELAY);
340 * Print the message once to reduce log noise. It will be followed by
341 * another one if the temperature cannot be determined after multiple
344 if (tz->recheck_delay_jiffies == THERMAL_RECHECK_DELAY)
345 dev_info(&tz->device, "Temperature check failed (%d)\n", error);
347 thermal_zone_device_set_polling(tz, tz->recheck_delay_jiffies);
349 tz->recheck_delay_jiffies += max(tz->recheck_delay_jiffies >> 1, 1ULL);
350 if (tz->recheck_delay_jiffies > THERMAL_MAX_RECHECK_DELAY) {
351 thermal_zone_broken_disable(tz);
353 * Restore the original recheck delay value to allow the thermal
354 * zone to try to recover when it is reenabled by user space.
356 tz->recheck_delay_jiffies = THERMAL_RECHECK_DELAY;
360 static void monitor_thermal_zone(struct thermal_zone_device *tz)
362 if (tz->passive > 0 && tz->passive_delay_jiffies)
363 thermal_zone_device_set_polling(tz, tz->passive_delay_jiffies);
364 else if (tz->polling_delay_jiffies)
365 thermal_zone_device_set_polling(tz, tz->polling_delay_jiffies);
368 static struct thermal_governor *thermal_get_tz_governor(struct thermal_zone_device *tz)
376 void thermal_governor_update_tz(struct thermal_zone_device *tz,
377 enum thermal_notify_event reason)
379 if (!tz->governor || !tz->governor->update_tz)
382 tz->governor->update_tz(tz, reason);
385 static void thermal_zone_device_halt(struct thermal_zone_device *tz, bool shutdown)
388 * poweroff_delay_ms must be a carefully profiled positive value.
389 * Its a must for forced_emergency_poweroff_work to be scheduled.
391 int poweroff_delay_ms = CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS;
392 const char *msg = "Temperature too high";
394 dev_emerg(&tz->device, "%s: critical temperature reached\n", tz->type);
397 hw_protection_shutdown(msg, poweroff_delay_ms);
399 hw_protection_reboot(msg, poweroff_delay_ms);
402 void thermal_zone_device_critical(struct thermal_zone_device *tz)
404 thermal_zone_device_halt(tz, true);
406 EXPORT_SYMBOL(thermal_zone_device_critical);
408 void thermal_zone_device_critical_reboot(struct thermal_zone_device *tz)
410 thermal_zone_device_halt(tz, false);
413 static void handle_critical_trips(struct thermal_zone_device *tz,
414 const struct thermal_trip *trip)
416 trace_thermal_zone_trip(tz, thermal_zone_trip_id(tz, trip), trip->type);
418 if (trip->type == THERMAL_TRIP_CRITICAL)
419 tz->ops.critical(tz);
420 else if (tz->ops.hot)
424 static void handle_thermal_trip(struct thermal_zone_device *tz,
425 struct thermal_trip_desc *td,
426 struct list_head *way_up_list,
427 struct list_head *way_down_list)
429 const struct thermal_trip *trip = &td->trip;
432 if (trip->temperature == THERMAL_TEMP_INVALID)
436 * If the trip temperature or hysteresis has been updated recently,
437 * the threshold needs to be computed again using the new values.
438 * However, its initial value still reflects the old ones and that
439 * is what needs to be compared with the previous zone temperature
440 * to decide which action to take.
442 old_threshold = td->threshold;
443 td->threshold = trip->temperature;
445 if (tz->last_temperature >= old_threshold &&
446 tz->last_temperature != THERMAL_TEMP_INIT) {
448 * Mitigation is under way, so it needs to stop if the zone
449 * temperature falls below the low temperature of the trip.
450 * In that case, the trip temperature becomes the new threshold.
452 if (tz->temperature < trip->temperature - trip->hysteresis) {
453 list_add(&td->notify_list_node, way_down_list);
454 td->notify_temp = trip->temperature - trip->hysteresis;
456 if (trip->type == THERMAL_TRIP_PASSIVE) {
458 WARN_ON(tz->passive < 0);
461 td->threshold -= trip->hysteresis;
463 } else if (tz->temperature >= trip->temperature) {
465 * There is no mitigation under way, so it needs to be started
466 * if the zone temperature exceeds the trip one. The new
467 * threshold is then set to the low temperature of the trip.
469 list_add_tail(&td->notify_list_node, way_up_list);
470 td->notify_temp = trip->temperature;
471 td->threshold -= trip->hysteresis;
473 if (trip->type == THERMAL_TRIP_PASSIVE)
475 else if (trip->type == THERMAL_TRIP_CRITICAL ||
476 trip->type == THERMAL_TRIP_HOT)
477 handle_critical_trips(tz, trip);
481 static void thermal_zone_device_check(struct work_struct *work)
483 struct thermal_zone_device *tz = container_of(work, struct
486 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
489 static void thermal_zone_device_init(struct thermal_zone_device *tz)
491 struct thermal_instance *pos;
493 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
495 tz->temperature = THERMAL_TEMP_INIT;
497 tz->prev_low_trip = -INT_MAX;
498 tz->prev_high_trip = INT_MAX;
499 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
500 pos->initialized = false;
503 static void thermal_governor_trip_crossed(struct thermal_governor *governor,
504 struct thermal_zone_device *tz,
505 const struct thermal_trip *trip,
508 if (trip->type == THERMAL_TRIP_HOT || trip->type == THERMAL_TRIP_CRITICAL)
511 if (governor->trip_crossed)
512 governor->trip_crossed(tz, trip, crossed_up);
515 static void thermal_trip_crossed(struct thermal_zone_device *tz,
516 const struct thermal_trip *trip,
517 struct thermal_governor *governor,
521 thermal_notify_tz_trip_up(tz, trip);
522 thermal_debug_tz_trip_up(tz, trip);
524 thermal_notify_tz_trip_down(tz, trip);
525 thermal_debug_tz_trip_down(tz, trip);
527 thermal_governor_trip_crossed(governor, tz, trip, crossed_up);
530 static int thermal_trip_notify_cmp(void *not_used, const struct list_head *a,
531 const struct list_head *b)
533 struct thermal_trip_desc *tda = container_of(a, struct thermal_trip_desc,
535 struct thermal_trip_desc *tdb = container_of(b, struct thermal_trip_desc,
537 return tda->notify_temp - tdb->notify_temp;
540 void __thermal_zone_device_update(struct thermal_zone_device *tz,
541 enum thermal_notify_event event)
543 struct thermal_governor *governor = thermal_get_tz_governor(tz);
544 struct thermal_trip_desc *td;
545 LIST_HEAD(way_down_list);
546 LIST_HEAD(way_up_list);
547 int low = -INT_MAX, high = INT_MAX;
550 if (tz->suspended || tz->mode != THERMAL_DEVICE_ENABLED)
553 ret = __thermal_zone_get_temp(tz, &temp);
555 thermal_zone_recheck(tz, ret);
557 } else if (temp <= THERMAL_TEMP_INVALID) {
559 * Special case: No valid temperature value is available, but
560 * the zone owner does not want the core to do anything about
561 * it. Continue regular zone polling if needed, so that this
562 * function can be called again, but skip everything else.
567 tz->recheck_delay_jiffies = THERMAL_RECHECK_DELAY;
569 tz->last_temperature = tz->temperature;
570 tz->temperature = temp;
572 trace_thermal_temperature(tz);
574 thermal_genl_sampling_temp(tz->id, temp);
576 tz->notify_event = event;
578 for_each_trip_desc(tz, td) {
579 handle_thermal_trip(tz, td, &way_up_list, &way_down_list);
581 if (td->threshold <= tz->temperature && td->threshold > low)
584 if (td->threshold >= tz->temperature && td->threshold < high)
585 high = td->threshold;
588 thermal_zone_set_trips(tz, low, high);
590 list_sort(NULL, &way_up_list, thermal_trip_notify_cmp);
591 list_for_each_entry(td, &way_up_list, notify_list_node)
592 thermal_trip_crossed(tz, &td->trip, governor, true);
594 list_sort(NULL, &way_down_list, thermal_trip_notify_cmp);
595 list_for_each_entry_reverse(td, &way_down_list, notify_list_node)
596 thermal_trip_crossed(tz, &td->trip, governor, false);
598 if (governor->manage)
599 governor->manage(tz);
601 thermal_debug_update_trip_stats(tz);
604 monitor_thermal_zone(tz);
607 static int thermal_zone_device_set_mode(struct thermal_zone_device *tz,
608 enum thermal_device_mode mode)
612 mutex_lock(&tz->lock);
614 /* do nothing if mode isn't changing */
615 if (mode == tz->mode) {
616 mutex_unlock(&tz->lock);
621 ret = __thermal_zone_device_set_mode(tz, mode);
623 mutex_unlock(&tz->lock);
628 __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
630 mutex_unlock(&tz->lock);
632 if (mode == THERMAL_DEVICE_ENABLED)
633 thermal_notify_tz_enable(tz);
635 thermal_notify_tz_disable(tz);
640 int thermal_zone_device_enable(struct thermal_zone_device *tz)
642 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_ENABLED);
644 EXPORT_SYMBOL_GPL(thermal_zone_device_enable);
646 int thermal_zone_device_disable(struct thermal_zone_device *tz)
648 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED);
650 EXPORT_SYMBOL_GPL(thermal_zone_device_disable);
652 static bool thermal_zone_is_present(struct thermal_zone_device *tz)
654 return !list_empty(&tz->node);
657 void thermal_zone_device_update(struct thermal_zone_device *tz,
658 enum thermal_notify_event event)
660 mutex_lock(&tz->lock);
661 if (thermal_zone_is_present(tz))
662 __thermal_zone_device_update(tz, event);
663 mutex_unlock(&tz->lock);
665 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
667 void thermal_zone_trip_down(struct thermal_zone_device *tz,
668 const struct thermal_trip *trip)
670 thermal_trip_crossed(tz, trip, thermal_get_tz_governor(tz), false);
673 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
676 struct thermal_governor *gov;
679 mutex_lock(&thermal_governor_lock);
680 list_for_each_entry(gov, &thermal_governor_list, governor_list) {
685 mutex_unlock(&thermal_governor_lock);
690 int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
693 struct thermal_cooling_device *cdev;
696 mutex_lock(&thermal_list_lock);
697 list_for_each_entry(cdev, &thermal_cdev_list, node) {
698 ret = cb(cdev, data);
702 mutex_unlock(&thermal_list_lock);
707 int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
710 struct thermal_zone_device *tz;
713 mutex_lock(&thermal_list_lock);
714 list_for_each_entry(tz, &thermal_tz_list, node) {
719 mutex_unlock(&thermal_list_lock);
724 struct thermal_zone_device *thermal_zone_get_by_id(int id)
726 struct thermal_zone_device *tz, *match = NULL;
728 mutex_lock(&thermal_list_lock);
729 list_for_each_entry(tz, &thermal_tz_list, node) {
735 mutex_unlock(&thermal_list_lock);
741 * Device management section: cooling devices, zones devices, and binding
743 * Set of functions provided by the thermal core for:
744 * - cooling devices lifecycle: registration, unregistration,
745 * binding, and unbinding.
746 * - thermal zone devices lifecycle: registration, unregistration,
747 * binding, and unbinding.
751 * thermal_bind_cdev_to_trip - bind a cooling device to a thermal zone
752 * @tz: pointer to struct thermal_zone_device
753 * @trip: trip point the cooling devices is associated with in this zone.
754 * @cdev: pointer to struct thermal_cooling_device
755 * @cool_spec: cooling specification for @trip and @cdev
757 * This interface function bind a thermal cooling device to the certain trip
758 * point of a thermal zone device.
759 * This function is usually called in the thermal zone device .bind callback.
761 * Return: 0 on success, the proper error value otherwise.
763 static int thermal_bind_cdev_to_trip(struct thermal_zone_device *tz,
764 const struct thermal_trip *trip,
765 struct thermal_cooling_device *cdev,
766 struct cooling_spec *cool_spec)
768 struct thermal_instance *dev;
769 struct thermal_instance *pos;
773 /* lower default 0, upper default max_state */
774 if (cool_spec->lower == THERMAL_NO_LIMIT)
775 cool_spec->lower = 0;
777 if (cool_spec->upper == THERMAL_NO_LIMIT) {
778 cool_spec->upper = cdev->max_state;
779 upper_no_limit = true;
781 upper_no_limit = false;
784 if (cool_spec->lower > cool_spec->upper || cool_spec->upper > cdev->max_state)
787 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
793 dev->upper = cool_spec->upper;
794 dev->upper_no_limit = upper_no_limit;
795 dev->lower = cool_spec->lower;
796 dev->target = THERMAL_NO_TARGET;
797 dev->weight = cool_spec->weight;
799 result = ida_alloc(&tz->ida, GFP_KERNEL);
804 sprintf(dev->name, "cdev%d", dev->id);
806 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
810 snprintf(dev->attr_name, sizeof(dev->attr_name), "cdev%d_trip_point",
812 sysfs_attr_init(&dev->attr.attr);
813 dev->attr.attr.name = dev->attr_name;
814 dev->attr.attr.mode = 0444;
815 dev->attr.show = trip_point_show;
816 result = device_create_file(&tz->device, &dev->attr);
818 goto remove_symbol_link;
820 snprintf(dev->weight_attr_name, sizeof(dev->weight_attr_name),
821 "cdev%d_weight", dev->id);
822 sysfs_attr_init(&dev->weight_attr.attr);
823 dev->weight_attr.attr.name = dev->weight_attr_name;
824 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
825 dev->weight_attr.show = weight_show;
826 dev->weight_attr.store = weight_store;
827 result = device_create_file(&tz->device, &dev->weight_attr);
829 goto remove_trip_file;
831 mutex_lock(&cdev->lock);
832 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
833 if (pos->trip == trip && pos->cdev == cdev) {
838 list_add_tail(&dev->tz_node, &tz->thermal_instances);
839 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
840 atomic_set(&tz->need_update, 1);
842 thermal_governor_update_tz(tz, THERMAL_TZ_BIND_CDEV);
844 mutex_unlock(&cdev->lock);
849 device_remove_file(&tz->device, &dev->weight_attr);
851 device_remove_file(&tz->device, &dev->attr);
853 sysfs_remove_link(&tz->device.kobj, dev->name);
855 ida_free(&tz->ida, dev->id);
862 * thermal_unbind_cdev_from_trip - unbind a cooling device from a thermal zone.
863 * @tz: pointer to a struct thermal_zone_device.
864 * @trip: trip point the cooling devices is associated with in this zone.
865 * @cdev: pointer to a struct thermal_cooling_device.
867 * This interface function unbind a thermal cooling device from the certain
868 * trip point of a thermal zone device.
869 * This function is usually called in the thermal zone device .unbind callback.
871 static void thermal_unbind_cdev_from_trip(struct thermal_zone_device *tz,
872 const struct thermal_trip *trip,
873 struct thermal_cooling_device *cdev)
875 struct thermal_instance *pos, *next;
877 mutex_lock(&cdev->lock);
878 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
879 if (pos->trip == trip && pos->cdev == cdev) {
880 list_del(&pos->tz_node);
881 list_del(&pos->cdev_node);
883 thermal_governor_update_tz(tz, THERMAL_TZ_UNBIND_CDEV);
885 mutex_unlock(&cdev->lock);
889 mutex_unlock(&cdev->lock);
894 device_remove_file(&tz->device, &pos->weight_attr);
895 device_remove_file(&tz->device, &pos->attr);
896 sysfs_remove_link(&tz->device.kobj, pos->name);
897 ida_free(&tz->ida, pos->id);
901 static void thermal_release(struct device *dev)
903 struct thermal_zone_device *tz;
904 struct thermal_cooling_device *cdev;
906 if (!strncmp(dev_name(dev), "thermal_zone",
907 sizeof("thermal_zone") - 1)) {
908 tz = to_thermal_zone(dev);
909 thermal_zone_destroy_device_groups(tz);
910 mutex_destroy(&tz->lock);
911 complete(&tz->removal);
912 } else if (!strncmp(dev_name(dev), "cooling_device",
913 sizeof("cooling_device") - 1)) {
914 cdev = to_cooling_device(dev);
915 thermal_cooling_device_destroy_sysfs(cdev);
916 kfree_const(cdev->type);
917 ida_free(&thermal_cdev_ida, cdev->id);
922 static struct class *thermal_class;
925 void print_bind_err_msg(struct thermal_zone_device *tz,
926 const struct thermal_trip *trip,
927 struct thermal_cooling_device *cdev, int ret)
929 dev_err(&tz->device, "binding cdev %s to trip %d failed: %d\n",
930 cdev->type, thermal_zone_trip_id(tz, trip), ret);
933 static void thermal_zone_cdev_bind(struct thermal_zone_device *tz,
934 struct thermal_cooling_device *cdev)
936 struct thermal_trip_desc *td;
938 if (!tz->ops.should_bind)
941 mutex_lock(&tz->lock);
943 for_each_trip_desc(tz, td) {
944 struct thermal_trip *trip = &td->trip;
945 struct cooling_spec c = {
946 .upper = THERMAL_NO_LIMIT,
947 .lower = THERMAL_NO_LIMIT,
948 .weight = THERMAL_WEIGHT_DEFAULT
952 if (!tz->ops.should_bind(tz, trip, cdev, &c))
955 ret = thermal_bind_cdev_to_trip(tz, trip, cdev, &c);
957 print_bind_err_msg(tz, trip, cdev, ret);
960 mutex_unlock(&tz->lock);
964 * __thermal_cooling_device_register() - register a new thermal cooling device
965 * @np: a pointer to a device tree node.
966 * @type: the thermal cooling device type.
967 * @devdata: device private data.
968 * @ops: standard thermal cooling devices callbacks.
970 * This interface function adds a new thermal cooling device (fan/processor/...)
971 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
972 * to all the thermal zone devices registered at the same time.
973 * It also gives the opportunity to link the cooling device to a device tree
974 * node, so that it can be bound to a thermal zone created out of device tree.
976 * Return: a pointer to the created struct thermal_cooling_device or an
977 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
979 static struct thermal_cooling_device *
980 __thermal_cooling_device_register(struct device_node *np,
981 const char *type, void *devdata,
982 const struct thermal_cooling_device_ops *ops)
984 struct thermal_cooling_device *cdev;
985 struct thermal_zone_device *pos = NULL;
986 unsigned long current_state;
989 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
991 return ERR_PTR(-EINVAL);
994 return ERR_PTR(-ENODEV);
996 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
998 return ERR_PTR(-ENOMEM);
1000 ret = ida_alloc(&thermal_cdev_ida, GFP_KERNEL);
1002 goto out_kfree_cdev;
1006 cdev->type = kstrdup_const(type ? type : "", GFP_KERNEL);
1009 goto out_ida_remove;
1012 mutex_init(&cdev->lock);
1013 INIT_LIST_HEAD(&cdev->thermal_instances);
1016 cdev->updated = false;
1017 cdev->device.class = thermal_class;
1018 cdev->devdata = devdata;
1020 ret = cdev->ops->get_max_state(cdev, &cdev->max_state);
1025 * The cooling device's current state is only needed for debug
1026 * initialization below, so a failure to get it does not cause
1027 * the entire cooling device initialization to fail. However,
1028 * the debug will not work for the device if its initial state
1029 * cannot be determined and drivers are responsible for ensuring
1030 * that this will not happen.
1032 ret = cdev->ops->get_cur_state(cdev, ¤t_state);
1034 current_state = ULONG_MAX;
1036 thermal_cooling_device_setup_sysfs(cdev);
1038 ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
1040 goto out_cooling_dev;
1042 ret = device_register(&cdev->device);
1044 /* thermal_release() handles rest of the cleanup */
1045 put_device(&cdev->device);
1046 return ERR_PTR(ret);
1049 if (current_state <= cdev->max_state)
1050 thermal_debug_cdev_add(cdev, current_state);
1052 /* Add 'this' new cdev to the global cdev list */
1053 mutex_lock(&thermal_list_lock);
1055 list_add(&cdev->node, &thermal_cdev_list);
1057 /* Update binding information for 'this' new cdev */
1058 list_for_each_entry(pos, &thermal_tz_list, node)
1059 thermal_zone_cdev_bind(pos, cdev);
1061 list_for_each_entry(pos, &thermal_tz_list, node)
1062 if (atomic_cmpxchg(&pos->need_update, 1, 0))
1063 thermal_zone_device_update(pos,
1064 THERMAL_EVENT_UNSPECIFIED);
1066 mutex_unlock(&thermal_list_lock);
1071 thermal_cooling_device_destroy_sysfs(cdev);
1073 kfree_const(cdev->type);
1075 ida_free(&thermal_cdev_ida, id);
1078 return ERR_PTR(ret);
1082 * thermal_cooling_device_register() - register a new thermal cooling device
1083 * @type: the thermal cooling device type.
1084 * @devdata: device private data.
1085 * @ops: standard thermal cooling devices callbacks.
1087 * This interface function adds a new thermal cooling device (fan/processor/...)
1088 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1089 * to all the thermal zone devices registered at the same time.
1091 * Return: a pointer to the created struct thermal_cooling_device or an
1092 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1094 struct thermal_cooling_device *
1095 thermal_cooling_device_register(const char *type, void *devdata,
1096 const struct thermal_cooling_device_ops *ops)
1098 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1100 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
1103 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1104 * @np: a pointer to a device tree node.
1105 * @type: the thermal cooling device type.
1106 * @devdata: device private data.
1107 * @ops: standard thermal cooling devices callbacks.
1109 * This function will register a cooling device with device tree node reference.
1110 * This interface function adds a new thermal cooling device (fan/processor/...)
1111 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1112 * to all the thermal zone devices registered at the same time.
1114 * Return: a pointer to the created struct thermal_cooling_device or an
1115 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1117 struct thermal_cooling_device *
1118 thermal_of_cooling_device_register(struct device_node *np,
1119 const char *type, void *devdata,
1120 const struct thermal_cooling_device_ops *ops)
1122 return __thermal_cooling_device_register(np, type, devdata, ops);
1124 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1126 static void thermal_cooling_device_release(struct device *dev, void *res)
1128 thermal_cooling_device_unregister(
1129 *(struct thermal_cooling_device **)res);
1133 * devm_thermal_of_cooling_device_register() - register an OF thermal cooling
1135 * @dev: a valid struct device pointer of a sensor device.
1136 * @np: a pointer to a device tree node.
1137 * @type: the thermal cooling device type.
1138 * @devdata: device private data.
1139 * @ops: standard thermal cooling devices callbacks.
1141 * This function will register a cooling device with device tree node reference.
1142 * This interface function adds a new thermal cooling device (fan/processor/...)
1143 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1144 * to all the thermal zone devices registered at the same time.
1146 * Return: a pointer to the created struct thermal_cooling_device or an
1147 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1149 struct thermal_cooling_device *
1150 devm_thermal_of_cooling_device_register(struct device *dev,
1151 struct device_node *np,
1152 const char *type, void *devdata,
1153 const struct thermal_cooling_device_ops *ops)
1155 struct thermal_cooling_device **ptr, *tcd;
1157 ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr),
1160 return ERR_PTR(-ENOMEM);
1162 tcd = __thermal_cooling_device_register(np, type, devdata, ops);
1169 devres_add(dev, ptr);
1173 EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register);
1175 static bool thermal_cooling_device_present(struct thermal_cooling_device *cdev)
1177 struct thermal_cooling_device *pos = NULL;
1179 list_for_each_entry(pos, &thermal_cdev_list, node) {
1188 * thermal_cooling_device_update - Update a cooling device object
1189 * @cdev: Target cooling device.
1191 * Update @cdev to reflect a change of the underlying hardware or platform.
1193 * Must be called when the maximum cooling state of @cdev becomes invalid and so
1194 * its .get_max_state() callback needs to be run to produce the new maximum
1195 * cooling state value.
1197 void thermal_cooling_device_update(struct thermal_cooling_device *cdev)
1199 struct thermal_instance *ti;
1200 unsigned long state;
1202 if (IS_ERR_OR_NULL(cdev))
1206 * Hold thermal_list_lock throughout the update to prevent the device
1207 * from going away while being updated.
1209 mutex_lock(&thermal_list_lock);
1211 if (!thermal_cooling_device_present(cdev))
1215 * Update under the cdev lock to prevent the state from being set beyond
1216 * the new limit concurrently.
1218 mutex_lock(&cdev->lock);
1220 if (cdev->ops->get_max_state(cdev, &cdev->max_state))
1223 thermal_cooling_device_stats_reinit(cdev);
1225 list_for_each_entry(ti, &cdev->thermal_instances, cdev_node) {
1226 if (ti->upper == cdev->max_state)
1229 if (ti->upper < cdev->max_state) {
1230 if (ti->upper_no_limit)
1231 ti->upper = cdev->max_state;
1236 ti->upper = cdev->max_state;
1237 if (ti->lower > ti->upper)
1238 ti->lower = ti->upper;
1240 if (ti->target == THERMAL_NO_TARGET)
1243 if (ti->target > ti->upper)
1244 ti->target = ti->upper;
1247 if (cdev->ops->get_cur_state(cdev, &state) || state > cdev->max_state)
1250 thermal_cooling_device_stats_update(cdev, state);
1253 mutex_unlock(&cdev->lock);
1256 mutex_unlock(&thermal_list_lock);
1258 EXPORT_SYMBOL_GPL(thermal_cooling_device_update);
1260 static void thermal_zone_cdev_unbind(struct thermal_zone_device *tz,
1261 struct thermal_cooling_device *cdev)
1263 struct thermal_trip_desc *td;
1265 mutex_lock(&tz->lock);
1267 for_each_trip_desc(tz, td)
1268 thermal_unbind_cdev_from_trip(tz, &td->trip, cdev);
1270 mutex_unlock(&tz->lock);
1274 * thermal_cooling_device_unregister - removes a thermal cooling device
1275 * @cdev: the thermal cooling device to remove.
1277 * thermal_cooling_device_unregister() must be called when a registered
1278 * thermal cooling device is no longer needed.
1280 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1282 struct thermal_zone_device *tz;
1287 thermal_debug_cdev_remove(cdev);
1289 mutex_lock(&thermal_list_lock);
1291 if (!thermal_cooling_device_present(cdev)) {
1292 mutex_unlock(&thermal_list_lock);
1296 list_del(&cdev->node);
1298 /* Unbind all thermal zones associated with 'this' cdev */
1299 list_for_each_entry(tz, &thermal_tz_list, node)
1300 thermal_zone_cdev_unbind(tz, cdev);
1302 mutex_unlock(&thermal_list_lock);
1304 device_unregister(&cdev->device);
1306 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1308 int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp)
1310 const struct thermal_trip_desc *td;
1313 if (tz->ops.get_crit_temp)
1314 return tz->ops.get_crit_temp(tz, temp);
1316 mutex_lock(&tz->lock);
1318 for_each_trip_desc(tz, td) {
1319 const struct thermal_trip *trip = &td->trip;
1321 if (trip->type == THERMAL_TRIP_CRITICAL) {
1322 *temp = trip->temperature;
1328 mutex_unlock(&tz->lock);
1332 EXPORT_SYMBOL_GPL(thermal_zone_get_crit_temp);
1335 * thermal_zone_device_register_with_trips() - register a new thermal zone device
1336 * @type: the thermal zone device type
1337 * @trips: a pointer to an array of thermal trips
1338 * @num_trips: the number of trip points the thermal zone support
1339 * @devdata: private device data
1340 * @ops: standard thermal zone device callbacks
1341 * @tzp: thermal zone platform parameters
1342 * @passive_delay: number of milliseconds to wait between polls when
1343 * performing passive cooling
1344 * @polling_delay: number of milliseconds to wait between polls when checking
1345 * whether trip points have been crossed (0 for interrupt
1348 * This interface function adds a new thermal zone device (sensor) to
1349 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1350 * thermal cooling devices registered at the same time.
1351 * thermal_zone_device_unregister() must be called when the device is no
1352 * longer needed. The passive cooling depends on the .get_trend() return value.
1354 * Return: a pointer to the created struct thermal_zone_device or an
1355 * in case of error, an ERR_PTR. Caller must check return value with
1356 * IS_ERR*() helpers.
1358 struct thermal_zone_device *
1359 thermal_zone_device_register_with_trips(const char *type,
1360 const struct thermal_trip *trips,
1361 int num_trips, void *devdata,
1362 const struct thermal_zone_device_ops *ops,
1363 const struct thermal_zone_params *tzp,
1364 unsigned int passive_delay,
1365 unsigned int polling_delay)
1367 const struct thermal_trip *trip = trips;
1368 struct thermal_cooling_device *cdev;
1369 struct thermal_zone_device *tz;
1370 struct thermal_trip_desc *td;
1373 struct thermal_governor *governor;
1375 if (!type || strlen(type) == 0) {
1376 pr_err("No thermal zone type defined\n");
1377 return ERR_PTR(-EINVAL);
1380 if (strlen(type) >= THERMAL_NAME_LENGTH) {
1381 pr_err("Thermal zone name (%s) too long, should be under %d chars\n",
1382 type, THERMAL_NAME_LENGTH);
1383 return ERR_PTR(-EINVAL);
1386 if (num_trips < 0) {
1387 pr_err("Incorrect number of thermal trips\n");
1388 return ERR_PTR(-EINVAL);
1391 if (!ops || !ops->get_temp) {
1392 pr_err("Thermal zone device ops not defined or invalid\n");
1393 return ERR_PTR(-EINVAL);
1396 if (num_trips > 0 && !trips)
1397 return ERR_PTR(-EINVAL);
1399 if (polling_delay && passive_delay > polling_delay)
1400 return ERR_PTR(-EINVAL);
1403 return ERR_PTR(-ENODEV);
1405 tz = kzalloc(struct_size(tz, trips, num_trips), GFP_KERNEL);
1407 return ERR_PTR(-ENOMEM);
1410 tz->tzp = kmemdup(tzp, sizeof(*tzp), GFP_KERNEL);
1417 INIT_LIST_HEAD(&tz->thermal_instances);
1418 INIT_LIST_HEAD(&tz->node);
1420 mutex_init(&tz->lock);
1421 init_completion(&tz->removal);
1422 init_completion(&tz->resume);
1423 id = ida_alloc(&thermal_tz_ida, GFP_KERNEL);
1430 strscpy(tz->type, type, sizeof(tz->type));
1433 if (!tz->ops.critical)
1434 tz->ops.critical = thermal_zone_device_critical;
1436 tz->device.class = thermal_class;
1437 tz->devdata = devdata;
1438 tz->num_trips = num_trips;
1439 for_each_trip_desc(tz, td) {
1442 * Mark all thresholds as invalid to start with even though
1443 * this only matters for the trips that start as invalid and
1444 * become valid later.
1446 td->threshold = INT_MAX;
1449 tz->polling_delay_jiffies = msecs_to_jiffies(polling_delay);
1450 tz->passive_delay_jiffies = msecs_to_jiffies(passive_delay);
1451 tz->recheck_delay_jiffies = THERMAL_RECHECK_DELAY;
1454 /* Add nodes that are always present via .groups */
1455 result = thermal_zone_create_device_groups(tz);
1459 /* A new thermal zone needs to be updated anyway. */
1460 atomic_set(&tz->need_update, 1);
1462 result = dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1464 thermal_zone_destroy_device_groups(tz);
1467 result = device_register(&tz->device);
1469 goto release_device;
1471 /* Update 'this' zone's governor information */
1472 mutex_lock(&thermal_governor_lock);
1475 governor = __find_governor(tz->tzp->governor_name);
1477 governor = def_governor;
1479 result = thermal_set_governor(tz, governor);
1481 mutex_unlock(&thermal_governor_lock);
1485 mutex_unlock(&thermal_governor_lock);
1487 if (!tz->tzp || !tz->tzp->no_hwmon) {
1488 result = thermal_add_hwmon_sysfs(tz);
1493 mutex_lock(&thermal_list_lock);
1495 mutex_lock(&tz->lock);
1496 list_add_tail(&tz->node, &thermal_tz_list);
1497 mutex_unlock(&tz->lock);
1499 /* Bind cooling devices for this zone */
1500 list_for_each_entry(cdev, &thermal_cdev_list, node)
1501 thermal_zone_cdev_bind(tz, cdev);
1503 mutex_unlock(&thermal_list_lock);
1505 thermal_zone_device_init(tz);
1506 /* Update the new thermal zone and mark it as already updated. */
1507 if (atomic_cmpxchg(&tz->need_update, 1, 0))
1508 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1510 thermal_notify_tz_create(tz);
1512 thermal_debug_tz_add(tz);
1517 device_del(&tz->device);
1519 put_device(&tz->device);
1521 ida_free(&thermal_tz_ida, id);
1526 return ERR_PTR(result);
1528 EXPORT_SYMBOL_GPL(thermal_zone_device_register_with_trips);
1530 struct thermal_zone_device *thermal_tripless_zone_device_register(
1533 const struct thermal_zone_device_ops *ops,
1534 const struct thermal_zone_params *tzp)
1536 return thermal_zone_device_register_with_trips(type, NULL, 0, devdata,
1539 EXPORT_SYMBOL_GPL(thermal_tripless_zone_device_register);
1541 void *thermal_zone_device_priv(struct thermal_zone_device *tzd)
1543 return tzd->devdata;
1545 EXPORT_SYMBOL_GPL(thermal_zone_device_priv);
1547 const char *thermal_zone_device_type(struct thermal_zone_device *tzd)
1551 EXPORT_SYMBOL_GPL(thermal_zone_device_type);
1553 int thermal_zone_device_id(struct thermal_zone_device *tzd)
1557 EXPORT_SYMBOL_GPL(thermal_zone_device_id);
1559 struct device *thermal_zone_device(struct thermal_zone_device *tzd)
1561 return &tzd->device;
1563 EXPORT_SYMBOL_GPL(thermal_zone_device);
1566 * thermal_zone_device_unregister - removes the registered thermal zone device
1567 * @tz: the thermal zone device to remove
1569 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1571 struct thermal_cooling_device *cdev;
1572 struct thermal_zone_device *pos = NULL;
1577 thermal_debug_tz_remove(tz);
1579 mutex_lock(&thermal_list_lock);
1580 list_for_each_entry(pos, &thermal_tz_list, node)
1584 /* thermal zone device not found */
1585 mutex_unlock(&thermal_list_lock);
1589 mutex_lock(&tz->lock);
1590 list_del(&tz->node);
1591 mutex_unlock(&tz->lock);
1593 /* Unbind all cdevs associated with 'this' thermal zone */
1594 list_for_each_entry(cdev, &thermal_cdev_list, node)
1595 thermal_zone_cdev_unbind(tz, cdev);
1597 mutex_unlock(&thermal_list_lock);
1599 cancel_delayed_work_sync(&tz->poll_queue);
1601 thermal_set_governor(tz, NULL);
1603 thermal_remove_hwmon_sysfs(tz);
1604 ida_free(&thermal_tz_ida, tz->id);
1605 ida_destroy(&tz->ida);
1607 device_del(&tz->device);
1611 put_device(&tz->device);
1613 thermal_notify_tz_delete(tz);
1615 wait_for_completion(&tz->removal);
1618 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1621 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1622 * @name: thermal zone name to fetch the temperature
1624 * When only one zone is found with the passed name, returns a reference to it.
1626 * Return: On success returns a reference to an unique thermal zone with
1627 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1628 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1630 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1632 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1633 unsigned int found = 0;
1638 mutex_lock(&thermal_list_lock);
1639 list_for_each_entry(pos, &thermal_tz_list, node)
1640 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1644 mutex_unlock(&thermal_list_lock);
1646 /* nothing has been found, thus an error code for it */
1648 ref = ERR_PTR(-ENODEV);
1650 /* Success only when an unique zone is found */
1651 ref = ERR_PTR(-EEXIST);
1656 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1658 static void thermal_zone_device_resume(struct work_struct *work)
1660 struct thermal_zone_device *tz;
1662 tz = container_of(work, struct thermal_zone_device, poll_queue.work);
1664 mutex_lock(&tz->lock);
1666 tz->suspended = false;
1668 thermal_debug_tz_resume(tz);
1669 thermal_zone_device_init(tz);
1670 thermal_governor_update_tz(tz, THERMAL_TZ_RESUME);
1671 __thermal_zone_device_update(tz, THERMAL_TZ_RESUME);
1673 complete(&tz->resume);
1674 tz->resuming = false;
1676 mutex_unlock(&tz->lock);
1679 static int thermal_pm_notify(struct notifier_block *nb,
1680 unsigned long mode, void *_unused)
1682 struct thermal_zone_device *tz;
1685 case PM_HIBERNATION_PREPARE:
1686 case PM_RESTORE_PREPARE:
1687 case PM_SUSPEND_PREPARE:
1688 mutex_lock(&thermal_list_lock);
1690 list_for_each_entry(tz, &thermal_tz_list, node) {
1691 mutex_lock(&tz->lock);
1695 * thermal_zone_device_resume() queued up for
1696 * this zone has not acquired the lock yet, so
1697 * release it to let the function run and wait
1698 * util it has done the work.
1700 mutex_unlock(&tz->lock);
1702 wait_for_completion(&tz->resume);
1704 mutex_lock(&tz->lock);
1707 tz->suspended = true;
1709 mutex_unlock(&tz->lock);
1712 mutex_unlock(&thermal_list_lock);
1714 case PM_POST_HIBERNATION:
1715 case PM_POST_RESTORE:
1716 case PM_POST_SUSPEND:
1717 mutex_lock(&thermal_list_lock);
1719 list_for_each_entry(tz, &thermal_tz_list, node) {
1720 mutex_lock(&tz->lock);
1722 cancel_delayed_work(&tz->poll_queue);
1724 reinit_completion(&tz->resume);
1725 tz->resuming = true;
1728 * Replace the work function with the resume one, which
1729 * will restore the original work function and schedule
1730 * the polling work if needed.
1732 INIT_DELAYED_WORK(&tz->poll_queue,
1733 thermal_zone_device_resume);
1734 /* Queue up the work without a delay. */
1735 mod_delayed_work(system_freezable_power_efficient_wq,
1736 &tz->poll_queue, 0);
1738 mutex_unlock(&tz->lock);
1741 mutex_unlock(&thermal_list_lock);
1749 static struct notifier_block thermal_pm_nb = {
1750 .notifier_call = thermal_pm_notify,
1752 * Run at the lowest priority to avoid interference between the thermal
1753 * zone resume work items spawned by thermal_pm_notify() and the other
1756 .priority = INT_MIN,
1759 static int __init thermal_init(void)
1763 thermal_debug_init();
1765 result = thermal_netlink_init();
1769 result = thermal_register_governors();
1771 goto unregister_netlink;
1773 thermal_class = kzalloc(sizeof(*thermal_class), GFP_KERNEL);
1774 if (!thermal_class) {
1776 goto unregister_governors;
1779 thermal_class->name = "thermal";
1780 thermal_class->dev_release = thermal_release;
1782 result = class_register(thermal_class);
1784 kfree(thermal_class);
1785 thermal_class = NULL;
1786 goto unregister_governors;
1789 result = register_pm_notifier(&thermal_pm_nb);
1791 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1796 unregister_governors:
1797 thermal_unregister_governors();
1799 thermal_netlink_exit();
1801 mutex_destroy(&thermal_list_lock);
1802 mutex_destroy(&thermal_governor_lock);
1805 postcore_initcall(thermal_init);