1 // SPDX-License-Identifier: GPL-2.0
3 * thermal.c - Generic Thermal Management Sysfs support.
5 * Copyright (C) 2008 Intel Corp
6 * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
7 * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
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/thermal.h>
19 #include <linux/reboot.h>
20 #include <linux/string.h>
22 #include <linux/suspend.h>
24 #define CREATE_TRACE_POINTS
25 #include <trace/events/thermal.h>
27 #include "thermal_core.h"
28 #include "thermal_hwmon.h"
30 static DEFINE_IDA(thermal_tz_ida);
31 static DEFINE_IDA(thermal_cdev_ida);
33 static LIST_HEAD(thermal_tz_list);
34 static LIST_HEAD(thermal_cdev_list);
35 static LIST_HEAD(thermal_governor_list);
37 static DEFINE_MUTEX(thermal_list_lock);
38 static DEFINE_MUTEX(thermal_governor_lock);
40 static atomic_t in_suspend;
42 static struct thermal_governor *def_governor;
45 * Governor section: set of functions to handle thermal governors
47 * Functions to help in the life cycle of thermal governors within
48 * the thermal core and by the thermal governor code.
51 static struct thermal_governor *__find_governor(const char *name)
53 struct thermal_governor *pos;
55 if (!name || !name[0])
58 list_for_each_entry(pos, &thermal_governor_list, governor_list)
59 if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH))
66 * bind_previous_governor() - bind the previous governor of the thermal zone
67 * @tz: a valid pointer to a struct thermal_zone_device
68 * @failed_gov_name: the name of the governor that failed to register
70 * Register the previous governor of the thermal zone after a new
71 * governor has failed to be bound.
73 static void bind_previous_governor(struct thermal_zone_device *tz,
74 const char *failed_gov_name)
76 if (tz->governor && tz->governor->bind_to_tz) {
77 if (tz->governor->bind_to_tz(tz)) {
79 "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n",
80 failed_gov_name, tz->governor->name, tz->type);
87 * thermal_set_governor() - Switch to another governor
88 * @tz: a valid pointer to a struct thermal_zone_device
89 * @new_gov: pointer to the new governor
91 * Change the governor of thermal zone @tz.
93 * Return: 0 on success, an error if the new governor's bind_to_tz() failed.
95 static int thermal_set_governor(struct thermal_zone_device *tz,
96 struct thermal_governor *new_gov)
100 if (tz->governor && tz->governor->unbind_from_tz)
101 tz->governor->unbind_from_tz(tz);
103 if (new_gov && new_gov->bind_to_tz) {
104 ret = new_gov->bind_to_tz(tz);
106 bind_previous_governor(tz, new_gov->name);
112 tz->governor = new_gov;
117 int thermal_register_governor(struct thermal_governor *governor)
121 struct thermal_zone_device *pos;
126 mutex_lock(&thermal_governor_lock);
129 if (!__find_governor(governor->name)) {
133 list_add(&governor->governor_list, &thermal_governor_list);
134 match_default = !strncmp(governor->name,
135 DEFAULT_THERMAL_GOVERNOR,
136 THERMAL_NAME_LENGTH);
138 if (!def_governor && match_default)
139 def_governor = governor;
142 mutex_lock(&thermal_list_lock);
144 list_for_each_entry(pos, &thermal_tz_list, node) {
146 * only thermal zones with specified tz->tzp->governor_name
147 * may run with tz->govenor unset
152 name = pos->tzp->governor_name;
154 if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) {
157 ret = thermal_set_governor(pos, governor);
159 dev_err(&pos->device,
160 "Failed to set governor %s for thermal zone %s: %d\n",
161 governor->name, pos->type, ret);
165 mutex_unlock(&thermal_list_lock);
166 mutex_unlock(&thermal_governor_lock);
171 void thermal_unregister_governor(struct thermal_governor *governor)
173 struct thermal_zone_device *pos;
178 mutex_lock(&thermal_governor_lock);
180 if (!__find_governor(governor->name))
183 mutex_lock(&thermal_list_lock);
185 list_for_each_entry(pos, &thermal_tz_list, node) {
186 if (!strncasecmp(pos->governor->name, governor->name,
187 THERMAL_NAME_LENGTH))
188 thermal_set_governor(pos, NULL);
191 mutex_unlock(&thermal_list_lock);
192 list_del(&governor->governor_list);
194 mutex_unlock(&thermal_governor_lock);
197 int thermal_zone_device_set_policy(struct thermal_zone_device *tz,
200 struct thermal_governor *gov;
203 mutex_lock(&thermal_governor_lock);
204 mutex_lock(&tz->lock);
206 gov = __find_governor(strim(policy));
210 ret = thermal_set_governor(tz, gov);
213 mutex_unlock(&tz->lock);
214 mutex_unlock(&thermal_governor_lock);
216 thermal_notify_tz_gov_change(tz->id, policy);
221 int thermal_build_list_of_policies(char *buf)
223 struct thermal_governor *pos;
226 mutex_lock(&thermal_governor_lock);
228 list_for_each_entry(pos, &thermal_governor_list, governor_list) {
229 count += scnprintf(buf + count, PAGE_SIZE - count, "%s ",
232 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
234 mutex_unlock(&thermal_governor_lock);
239 static void __init thermal_unregister_governors(void)
241 struct thermal_governor **governor;
243 for_each_governor_table(governor)
244 thermal_unregister_governor(*governor);
247 static int __init thermal_register_governors(void)
250 struct thermal_governor **governor;
252 for_each_governor_table(governor) {
253 ret = thermal_register_governor(*governor);
255 pr_err("Failed to register governor: '%s'",
260 pr_info("Registered thermal governor '%s'",
265 struct thermal_governor **gov;
267 for_each_governor_table(gov) {
270 thermal_unregister_governor(*gov);
278 * Zone update section: main control loop applied to each zone while monitoring
280 * in polling mode. The monitoring is done using a workqueue.
281 * Same update may be done on a zone by calling thermal_zone_device_update().
284 * - Non-critical trips will invoke the governor responsible for that zone;
285 * - Hot trips will produce a notification to userspace;
286 * - Critical trip point will cause a system shutdown.
288 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
292 mod_delayed_work(system_freezable_power_efficient_wq,
293 &tz->poll_queue, delay);
295 cancel_delayed_work(&tz->poll_queue);
298 static void monitor_thermal_zone(struct thermal_zone_device *tz)
300 if (tz->mode != THERMAL_DEVICE_ENABLED)
301 thermal_zone_device_set_polling(tz, 0);
302 else if (tz->passive)
303 thermal_zone_device_set_polling(tz, tz->passive_delay_jiffies);
304 else if (tz->polling_delay_jiffies)
305 thermal_zone_device_set_polling(tz, tz->polling_delay_jiffies);
308 static void handle_non_critical_trips(struct thermal_zone_device *tz, int trip)
310 tz->governor ? tz->governor->throttle(tz, trip) :
311 def_governor->throttle(tz, trip);
314 void thermal_zone_device_critical(struct thermal_zone_device *tz)
317 * poweroff_delay_ms must be a carefully profiled positive value.
318 * Its a must for forced_emergency_poweroff_work to be scheduled.
320 int poweroff_delay_ms = CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS;
322 dev_emerg(&tz->device, "%s: critical temperature reached, "
323 "shutting down\n", tz->type);
325 hw_protection_shutdown("Temperature too high", poweroff_delay_ms);
327 EXPORT_SYMBOL(thermal_zone_device_critical);
329 static void handle_critical_trips(struct thermal_zone_device *tz,
330 int trip, int trip_temp, enum thermal_trip_type trip_type)
332 /* If we have not crossed the trip_temp, we do not care. */
333 if (trip_temp <= 0 || tz->temperature < trip_temp)
336 trace_thermal_zone_trip(tz, trip, trip_type);
338 if (trip_type == THERMAL_TRIP_HOT && tz->ops->hot)
340 else if (trip_type == THERMAL_TRIP_CRITICAL)
341 tz->ops->critical(tz);
344 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
346 enum thermal_trip_type type;
347 int trip_temp, hyst = 0;
349 /* Ignore disabled trip points */
350 if (test_bit(trip, &tz->trips_disabled))
353 tz->ops->get_trip_temp(tz, trip, &trip_temp);
354 tz->ops->get_trip_type(tz, trip, &type);
355 if (tz->ops->get_trip_hyst)
356 tz->ops->get_trip_hyst(tz, trip, &hyst);
358 if (tz->last_temperature != THERMAL_TEMP_INVALID) {
359 if (tz->last_temperature < trip_temp &&
360 tz->temperature >= trip_temp)
361 thermal_notify_tz_trip_up(tz->id, trip,
363 if (tz->last_temperature >= trip_temp &&
364 tz->temperature < (trip_temp - hyst))
365 thermal_notify_tz_trip_down(tz->id, trip,
369 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
370 handle_critical_trips(tz, trip, trip_temp, type);
372 handle_non_critical_trips(tz, trip);
375 static void update_temperature(struct thermal_zone_device *tz)
379 ret = __thermal_zone_get_temp(tz, &temp);
382 dev_warn(&tz->device,
383 "failed to read out thermal zone (%d)\n",
388 tz->last_temperature = tz->temperature;
389 tz->temperature = temp;
391 trace_thermal_temperature(tz);
393 thermal_genl_sampling_temp(tz->id, temp);
396 static void thermal_zone_device_init(struct thermal_zone_device *tz)
398 struct thermal_instance *pos;
399 tz->temperature = THERMAL_TEMP_INVALID;
400 tz->prev_low_trip = -INT_MAX;
401 tz->prev_high_trip = INT_MAX;
402 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
403 pos->initialized = false;
406 static int thermal_zone_device_set_mode(struct thermal_zone_device *tz,
407 enum thermal_device_mode mode)
411 mutex_lock(&tz->lock);
413 /* do nothing if mode isn't changing */
414 if (mode == tz->mode) {
415 mutex_unlock(&tz->lock);
420 if (tz->ops->change_mode)
421 ret = tz->ops->change_mode(tz, mode);
426 mutex_unlock(&tz->lock);
428 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
430 if (mode == THERMAL_DEVICE_ENABLED)
431 thermal_notify_tz_enable(tz->id);
433 thermal_notify_tz_disable(tz->id);
438 int thermal_zone_device_enable(struct thermal_zone_device *tz)
440 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_ENABLED);
442 EXPORT_SYMBOL_GPL(thermal_zone_device_enable);
444 int thermal_zone_device_disable(struct thermal_zone_device *tz)
446 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED);
448 EXPORT_SYMBOL_GPL(thermal_zone_device_disable);
450 int thermal_zone_device_is_enabled(struct thermal_zone_device *tz)
452 lockdep_assert_held(&tz->lock);
454 return tz->mode == THERMAL_DEVICE_ENABLED;
457 void thermal_zone_device_update(struct thermal_zone_device *tz,
458 enum thermal_notify_event event)
462 if (atomic_read(&in_suspend))
465 if (WARN_ONCE(!tz->ops->get_temp, "'%s' must not be called without "
466 "'get_temp' ops set\n", __func__))
469 mutex_lock(&tz->lock);
471 if (!thermal_zone_device_is_enabled(tz))
474 update_temperature(tz);
476 __thermal_zone_set_trips(tz);
478 tz->notify_event = event;
480 for (count = 0; count < tz->num_trips; count++)
481 handle_thermal_trip(tz, count);
483 monitor_thermal_zone(tz);
485 mutex_unlock(&tz->lock);
487 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
489 static void thermal_zone_device_check(struct work_struct *work)
491 struct thermal_zone_device *tz = container_of(work, struct
494 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
497 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
500 struct thermal_governor *gov;
503 mutex_lock(&thermal_governor_lock);
504 list_for_each_entry(gov, &thermal_governor_list, governor_list) {
509 mutex_unlock(&thermal_governor_lock);
514 int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
517 struct thermal_cooling_device *cdev;
520 mutex_lock(&thermal_list_lock);
521 list_for_each_entry(cdev, &thermal_cdev_list, node) {
522 ret = cb(cdev, data);
526 mutex_unlock(&thermal_list_lock);
531 int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
534 struct thermal_zone_device *tz;
537 mutex_lock(&thermal_list_lock);
538 list_for_each_entry(tz, &thermal_tz_list, node) {
543 mutex_unlock(&thermal_list_lock);
548 struct thermal_zone_device *thermal_zone_get_by_id(int id)
550 struct thermal_zone_device *tz, *match = NULL;
552 mutex_lock(&thermal_list_lock);
553 list_for_each_entry(tz, &thermal_tz_list, node) {
559 mutex_unlock(&thermal_list_lock);
565 * Device management section: cooling devices, zones devices, and binding
567 * Set of functions provided by the thermal core for:
568 * - cooling devices lifecycle: registration, unregistration,
569 * binding, and unbinding.
570 * - thermal zone devices lifecycle: registration, unregistration,
571 * binding, and unbinding.
575 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
576 * @tz: pointer to struct thermal_zone_device
577 * @trip: indicates which trip point the cooling devices is
578 * associated with in this thermal zone.
579 * @cdev: pointer to struct thermal_cooling_device
580 * @upper: the Maximum cooling state for this trip point.
581 * THERMAL_NO_LIMIT means no upper limit,
582 * and the cooling device can be in max_state.
583 * @lower: the Minimum cooling state can be used for this trip point.
584 * THERMAL_NO_LIMIT means no lower limit,
585 * and the cooling device can be in cooling state 0.
586 * @weight: The weight of the cooling device to be bound to the
587 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
590 * This interface function bind a thermal cooling device to the certain trip
591 * point of a thermal zone device.
592 * This function is usually called in the thermal zone device .bind callback.
594 * Return: 0 on success, the proper error value otherwise.
596 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
598 struct thermal_cooling_device *cdev,
599 unsigned long upper, unsigned long lower,
602 struct thermal_instance *dev;
603 struct thermal_instance *pos;
604 struct thermal_zone_device *pos1;
605 struct thermal_cooling_device *pos2;
606 unsigned long max_state;
609 if (trip >= tz->num_trips || trip < 0)
612 list_for_each_entry(pos1, &thermal_tz_list, node) {
616 list_for_each_entry(pos2, &thermal_cdev_list, node) {
621 if (tz != pos1 || cdev != pos2)
624 ret = cdev->ops->get_max_state(cdev, &max_state);
628 /* lower default 0, upper default max_state */
629 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
630 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
632 if (lower > upper || upper > max_state)
635 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
643 dev->target = THERMAL_NO_TARGET;
644 dev->weight = weight;
646 result = ida_alloc(&tz->ida, GFP_KERNEL);
651 sprintf(dev->name, "cdev%d", dev->id);
653 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
657 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
658 sysfs_attr_init(&dev->attr.attr);
659 dev->attr.attr.name = dev->attr_name;
660 dev->attr.attr.mode = 0444;
661 dev->attr.show = trip_point_show;
662 result = device_create_file(&tz->device, &dev->attr);
664 goto remove_symbol_link;
666 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
667 sysfs_attr_init(&dev->weight_attr.attr);
668 dev->weight_attr.attr.name = dev->weight_attr_name;
669 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
670 dev->weight_attr.show = weight_show;
671 dev->weight_attr.store = weight_store;
672 result = device_create_file(&tz->device, &dev->weight_attr);
674 goto remove_trip_file;
676 mutex_lock(&tz->lock);
677 mutex_lock(&cdev->lock);
678 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
679 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
684 list_add_tail(&dev->tz_node, &tz->thermal_instances);
685 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
686 atomic_set(&tz->need_update, 1);
688 mutex_unlock(&cdev->lock);
689 mutex_unlock(&tz->lock);
694 device_remove_file(&tz->device, &dev->weight_attr);
696 device_remove_file(&tz->device, &dev->attr);
698 sysfs_remove_link(&tz->device.kobj, dev->name);
700 ida_free(&tz->ida, dev->id);
705 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
708 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
710 * @tz: pointer to a struct thermal_zone_device.
711 * @trip: indicates which trip point the cooling devices is
712 * associated with in this thermal zone.
713 * @cdev: pointer to a struct thermal_cooling_device.
715 * This interface function unbind a thermal cooling device from the certain
716 * trip point of a thermal zone device.
717 * This function is usually called in the thermal zone device .unbind callback.
719 * Return: 0 on success, the proper error value otherwise.
721 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
723 struct thermal_cooling_device *cdev)
725 struct thermal_instance *pos, *next;
727 mutex_lock(&tz->lock);
728 mutex_lock(&cdev->lock);
729 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
730 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
731 list_del(&pos->tz_node);
732 list_del(&pos->cdev_node);
733 mutex_unlock(&cdev->lock);
734 mutex_unlock(&tz->lock);
738 mutex_unlock(&cdev->lock);
739 mutex_unlock(&tz->lock);
744 device_remove_file(&tz->device, &pos->weight_attr);
745 device_remove_file(&tz->device, &pos->attr);
746 sysfs_remove_link(&tz->device.kobj, pos->name);
747 ida_free(&tz->ida, pos->id);
751 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
753 static void thermal_release(struct device *dev)
755 struct thermal_zone_device *tz;
756 struct thermal_cooling_device *cdev;
758 if (!strncmp(dev_name(dev), "thermal_zone",
759 sizeof("thermal_zone") - 1)) {
760 tz = to_thermal_zone(dev);
761 thermal_zone_destroy_device_groups(tz);
763 } else if (!strncmp(dev_name(dev), "cooling_device",
764 sizeof("cooling_device") - 1)) {
765 cdev = to_cooling_device(dev);
770 static struct class thermal_class = {
772 .dev_release = thermal_release,
776 void print_bind_err_msg(struct thermal_zone_device *tz,
777 struct thermal_cooling_device *cdev, int ret)
779 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
780 tz->type, cdev->type, ret);
783 static void __bind(struct thermal_zone_device *tz, int mask,
784 struct thermal_cooling_device *cdev,
785 unsigned long *limits,
790 for (i = 0; i < tz->num_trips; i++) {
791 if (mask & (1 << i)) {
792 unsigned long upper, lower;
794 upper = THERMAL_NO_LIMIT;
795 lower = THERMAL_NO_LIMIT;
797 lower = limits[i * 2];
798 upper = limits[i * 2 + 1];
800 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
804 print_bind_err_msg(tz, cdev, ret);
809 static void bind_cdev(struct thermal_cooling_device *cdev)
812 const struct thermal_zone_params *tzp;
813 struct thermal_zone_device *pos = NULL;
815 mutex_lock(&thermal_list_lock);
817 list_for_each_entry(pos, &thermal_tz_list, node) {
818 if (!pos->tzp && !pos->ops->bind)
821 if (pos->ops->bind) {
822 ret = pos->ops->bind(pos, cdev);
824 print_bind_err_msg(pos, cdev, ret);
829 if (!tzp || !tzp->tbp)
832 for (i = 0; i < tzp->num_tbps; i++) {
833 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
835 if (tzp->tbp[i].match(pos, cdev))
837 tzp->tbp[i].cdev = cdev;
838 __bind(pos, tzp->tbp[i].trip_mask, cdev,
839 tzp->tbp[i].binding_limits,
844 mutex_unlock(&thermal_list_lock);
848 * __thermal_cooling_device_register() - register a new thermal cooling device
849 * @np: a pointer to a device tree node.
850 * @type: the thermal cooling device type.
851 * @devdata: device private data.
852 * @ops: standard thermal cooling devices callbacks.
854 * This interface function adds a new thermal cooling device (fan/processor/...)
855 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
856 * to all the thermal zone devices registered at the same time.
857 * It also gives the opportunity to link the cooling device to a device tree
858 * node, so that it can be bound to a thermal zone created out of device tree.
860 * Return: a pointer to the created struct thermal_cooling_device or an
861 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
863 static struct thermal_cooling_device *
864 __thermal_cooling_device_register(struct device_node *np,
865 const char *type, void *devdata,
866 const struct thermal_cooling_device_ops *ops)
868 struct thermal_cooling_device *cdev;
869 struct thermal_zone_device *pos = NULL;
872 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
874 return ERR_PTR(-EINVAL);
876 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
878 return ERR_PTR(-ENOMEM);
880 ret = ida_alloc(&thermal_cdev_ida, GFP_KERNEL);
886 cdev->type = kstrdup(type ? type : "", GFP_KERNEL);
892 mutex_init(&cdev->lock);
893 INIT_LIST_HEAD(&cdev->thermal_instances);
896 cdev->updated = false;
897 cdev->device.class = &thermal_class;
898 cdev->devdata = devdata;
899 thermal_cooling_device_setup_sysfs(cdev);
900 ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
902 thermal_cooling_device_destroy_sysfs(cdev);
905 ret = device_register(&cdev->device);
909 /* Add 'this' new cdev to the global cdev list */
910 mutex_lock(&thermal_list_lock);
911 list_add(&cdev->node, &thermal_cdev_list);
912 mutex_unlock(&thermal_list_lock);
914 /* Update binding information for 'this' new cdev */
917 mutex_lock(&thermal_list_lock);
918 list_for_each_entry(pos, &thermal_tz_list, node)
919 if (atomic_cmpxchg(&pos->need_update, 1, 0))
920 thermal_zone_device_update(pos,
921 THERMAL_EVENT_UNSPECIFIED);
922 mutex_unlock(&thermal_list_lock);
927 thermal_cooling_device_destroy_sysfs(cdev);
929 put_device(&cdev->device);
932 ida_free(&thermal_cdev_ida, id);
939 * thermal_cooling_device_register() - register a new thermal cooling device
940 * @type: the thermal cooling device type.
941 * @devdata: device private data.
942 * @ops: standard thermal cooling devices callbacks.
944 * This interface function adds a new thermal cooling device (fan/processor/...)
945 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
946 * to all the thermal zone devices registered at the same time.
948 * Return: a pointer to the created struct thermal_cooling_device or an
949 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
951 struct thermal_cooling_device *
952 thermal_cooling_device_register(const char *type, void *devdata,
953 const struct thermal_cooling_device_ops *ops)
955 return __thermal_cooling_device_register(NULL, type, devdata, ops);
957 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
960 * thermal_of_cooling_device_register() - register an OF thermal cooling device
961 * @np: a pointer to a device tree node.
962 * @type: the thermal cooling device type.
963 * @devdata: device private data.
964 * @ops: standard thermal cooling devices callbacks.
966 * This function will register a cooling device with device tree node reference.
967 * This interface function adds a new thermal cooling device (fan/processor/...)
968 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
969 * to all the thermal zone devices registered at the same time.
971 * Return: a pointer to the created struct thermal_cooling_device or an
972 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
974 struct thermal_cooling_device *
975 thermal_of_cooling_device_register(struct device_node *np,
976 const char *type, void *devdata,
977 const struct thermal_cooling_device_ops *ops)
979 return __thermal_cooling_device_register(np, type, devdata, ops);
981 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
983 static void thermal_cooling_device_release(struct device *dev, void *res)
985 thermal_cooling_device_unregister(
986 *(struct thermal_cooling_device **)res);
990 * devm_thermal_of_cooling_device_register() - register an OF thermal cooling
992 * @dev: a valid struct device pointer of a sensor device.
993 * @np: a pointer to a device tree node.
994 * @type: the thermal cooling device type.
995 * @devdata: device private data.
996 * @ops: standard thermal cooling devices callbacks.
998 * This function will register a cooling device with device tree node reference.
999 * This interface function adds a new thermal cooling device (fan/processor/...)
1000 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1001 * to all the thermal zone devices registered at the same time.
1003 * Return: a pointer to the created struct thermal_cooling_device or an
1004 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1006 struct thermal_cooling_device *
1007 devm_thermal_of_cooling_device_register(struct device *dev,
1008 struct device_node *np,
1009 char *type, void *devdata,
1010 const struct thermal_cooling_device_ops *ops)
1012 struct thermal_cooling_device **ptr, *tcd;
1014 ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr),
1017 return ERR_PTR(-ENOMEM);
1019 tcd = __thermal_cooling_device_register(np, type, devdata, ops);
1026 devres_add(dev, ptr);
1030 EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register);
1032 static void __unbind(struct thermal_zone_device *tz, int mask,
1033 struct thermal_cooling_device *cdev)
1037 for (i = 0; i < tz->num_trips; i++)
1038 if (mask & (1 << i))
1039 thermal_zone_unbind_cooling_device(tz, i, cdev);
1043 * thermal_cooling_device_unregister - removes a thermal cooling device
1044 * @cdev: the thermal cooling device to remove.
1046 * thermal_cooling_device_unregister() must be called when a registered
1047 * thermal cooling device is no longer needed.
1049 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1052 const struct thermal_zone_params *tzp;
1053 struct thermal_zone_device *tz;
1054 struct thermal_cooling_device *pos = NULL;
1059 mutex_lock(&thermal_list_lock);
1060 list_for_each_entry(pos, &thermal_cdev_list, node)
1064 /* thermal cooling device not found */
1065 mutex_unlock(&thermal_list_lock);
1068 list_del(&cdev->node);
1070 /* Unbind all thermal zones associated with 'this' cdev */
1071 list_for_each_entry(tz, &thermal_tz_list, node) {
1072 if (tz->ops->unbind) {
1073 tz->ops->unbind(tz, cdev);
1077 if (!tz->tzp || !tz->tzp->tbp)
1081 for (i = 0; i < tzp->num_tbps; i++) {
1082 if (tzp->tbp[i].cdev == cdev) {
1083 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1084 tzp->tbp[i].cdev = NULL;
1089 mutex_unlock(&thermal_list_lock);
1091 ida_free(&thermal_cdev_ida, cdev->id);
1092 device_del(&cdev->device);
1093 thermal_cooling_device_destroy_sysfs(cdev);
1095 put_device(&cdev->device);
1097 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1099 static void bind_tz(struct thermal_zone_device *tz)
1102 struct thermal_cooling_device *pos = NULL;
1103 const struct thermal_zone_params *tzp = tz->tzp;
1105 if (!tzp && !tz->ops->bind)
1108 mutex_lock(&thermal_list_lock);
1110 /* If there is ops->bind, try to use ops->bind */
1111 if (tz->ops->bind) {
1112 list_for_each_entry(pos, &thermal_cdev_list, node) {
1113 ret = tz->ops->bind(tz, pos);
1115 print_bind_err_msg(tz, pos, ret);
1120 if (!tzp || !tzp->tbp)
1123 list_for_each_entry(pos, &thermal_cdev_list, node) {
1124 for (i = 0; i < tzp->num_tbps; i++) {
1125 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
1127 if (tzp->tbp[i].match(tz, pos))
1129 tzp->tbp[i].cdev = pos;
1130 __bind(tz, tzp->tbp[i].trip_mask, pos,
1131 tzp->tbp[i].binding_limits,
1132 tzp->tbp[i].weight);
1136 mutex_unlock(&thermal_list_lock);
1139 static void thermal_set_delay_jiffies(unsigned long *delay_jiffies, int delay_ms)
1141 *delay_jiffies = msecs_to_jiffies(delay_ms);
1142 if (delay_ms > 1000)
1143 *delay_jiffies = round_jiffies(*delay_jiffies);
1147 * thermal_zone_device_register_with_trips() - register a new thermal zone device
1148 * @type: the thermal zone device type
1149 * @trips: a pointer to an array of thermal trips
1150 * @num_trips: the number of trip points the thermal zone support
1151 * @mask: a bit string indicating the writeablility of trip points
1152 * @devdata: private device data
1153 * @ops: standard thermal zone device callbacks
1154 * @tzp: thermal zone platform parameters
1155 * @passive_delay: number of milliseconds to wait between polls when
1156 * performing passive cooling
1157 * @polling_delay: number of milliseconds to wait between polls when checking
1158 * whether trip points have been crossed (0 for interrupt
1161 * This interface function adds a new thermal zone device (sensor) to
1162 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1163 * thermal cooling devices registered at the same time.
1164 * thermal_zone_device_unregister() must be called when the device is no
1165 * longer needed. The passive cooling depends on the .get_trend() return value.
1167 * Return: a pointer to the created struct thermal_zone_device or an
1168 * in case of error, an ERR_PTR. Caller must check return value with
1169 * IS_ERR*() helpers.
1171 struct thermal_zone_device *
1172 thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *trips, int num_trips, int mask,
1173 void *devdata, struct thermal_zone_device_ops *ops,
1174 struct thermal_zone_params *tzp, int passive_delay,
1177 struct thermal_zone_device *tz;
1178 enum thermal_trip_type trip_type;
1183 struct thermal_governor *governor;
1185 if (!type || strlen(type) == 0) {
1186 pr_err("No thermal zone type defined\n");
1187 return ERR_PTR(-EINVAL);
1190 if (strlen(type) >= THERMAL_NAME_LENGTH) {
1191 pr_err("Thermal zone name (%s) too long, should be under %d chars\n",
1192 type, THERMAL_NAME_LENGTH);
1193 return ERR_PTR(-EINVAL);
1197 * Max trip count can't exceed 31 as the "mask >> num_trips" condition.
1198 * For example, shifting by 32 will result in compiler warning:
1199 * warning: right shift count >= width of type [-Wshift-count- overflow]
1201 * Also "mask >> num_trips" will always be true with 32 bit shift.
1202 * E.g. mask = 0x80000000 for trip id 31 to be RW. Then
1203 * mask >> 32 = 0x80000000
1204 * This will result in failure for the below condition.
1206 * Check will be true when the bit 31 of the mask is set.
1207 * 32 bit shift will cause overflow of 4 byte integer.
1209 if (num_trips > (BITS_PER_TYPE(int) - 1) || num_trips < 0 || mask >> num_trips) {
1210 pr_err("Incorrect number of thermal trips\n");
1211 return ERR_PTR(-EINVAL);
1215 pr_err("Thermal zone device ops not defined\n");
1216 return ERR_PTR(-EINVAL);
1219 if (num_trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
1220 return ERR_PTR(-EINVAL);
1222 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
1224 return ERR_PTR(-ENOMEM);
1226 INIT_LIST_HEAD(&tz->thermal_instances);
1228 mutex_init(&tz->lock);
1229 id = ida_alloc(&thermal_tz_ida, GFP_KERNEL);
1236 strscpy(tz->type, type, sizeof(tz->type));
1239 ops->critical = thermal_zone_device_critical;
1243 tz->device.class = &thermal_class;
1244 tz->devdata = devdata;
1246 tz->num_trips = num_trips;
1248 thermal_set_delay_jiffies(&tz->passive_delay_jiffies, passive_delay);
1249 thermal_set_delay_jiffies(&tz->polling_delay_jiffies, polling_delay);
1252 /* Add nodes that are always present via .groups */
1253 result = thermal_zone_create_device_groups(tz, mask);
1257 /* A new thermal zone needs to be updated anyway. */
1258 atomic_set(&tz->need_update, 1);
1260 result = dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1262 thermal_zone_destroy_device_groups(tz);
1265 result = device_register(&tz->device);
1267 goto release_device;
1269 for (count = 0; count < num_trips; count++) {
1270 if (tz->ops->get_trip_type(tz, count, &trip_type) ||
1271 tz->ops->get_trip_temp(tz, count, &trip_temp) ||
1273 set_bit(count, &tz->trips_disabled);
1276 /* Update 'this' zone's governor information */
1277 mutex_lock(&thermal_governor_lock);
1280 governor = __find_governor(tz->tzp->governor_name);
1282 governor = def_governor;
1284 result = thermal_set_governor(tz, governor);
1286 mutex_unlock(&thermal_governor_lock);
1290 mutex_unlock(&thermal_governor_lock);
1292 if (!tz->tzp || !tz->tzp->no_hwmon) {
1293 result = thermal_add_hwmon_sysfs(tz);
1298 mutex_lock(&thermal_list_lock);
1299 list_add_tail(&tz->node, &thermal_tz_list);
1300 mutex_unlock(&thermal_list_lock);
1302 /* Bind cooling devices for this zone */
1305 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
1307 thermal_zone_device_init(tz);
1308 /* Update the new thermal zone and mark it as already updated. */
1309 if (atomic_cmpxchg(&tz->need_update, 1, 0))
1310 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1312 thermal_notify_tz_create(tz->id, tz->type);
1317 device_del(&tz->device);
1319 put_device(&tz->device);
1322 ida_free(&thermal_tz_ida, id);
1325 return ERR_PTR(result);
1327 EXPORT_SYMBOL_GPL(thermal_zone_device_register_with_trips);
1329 struct thermal_zone_device *thermal_zone_device_register(const char *type, int ntrips, int mask,
1330 void *devdata, struct thermal_zone_device_ops *ops,
1331 struct thermal_zone_params *tzp, int passive_delay,
1334 return thermal_zone_device_register_with_trips(type, NULL, ntrips, mask,
1336 passive_delay, polling_delay);
1338 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
1341 * thermal_zone_device_unregister - removes the registered thermal zone device
1342 * @tz: the thermal zone device to remove
1344 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1347 const struct thermal_zone_params *tzp;
1348 struct thermal_cooling_device *cdev;
1349 struct thermal_zone_device *pos = NULL;
1357 mutex_lock(&thermal_list_lock);
1358 list_for_each_entry(pos, &thermal_tz_list, node)
1362 /* thermal zone device not found */
1363 mutex_unlock(&thermal_list_lock);
1366 list_del(&tz->node);
1368 /* Unbind all cdevs associated with 'this' thermal zone */
1369 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1370 if (tz->ops->unbind) {
1371 tz->ops->unbind(tz, cdev);
1375 if (!tzp || !tzp->tbp)
1378 for (i = 0; i < tzp->num_tbps; i++) {
1379 if (tzp->tbp[i].cdev == cdev) {
1380 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1381 tzp->tbp[i].cdev = NULL;
1386 mutex_unlock(&thermal_list_lock);
1388 cancel_delayed_work_sync(&tz->poll_queue);
1390 thermal_set_governor(tz, NULL);
1392 thermal_remove_hwmon_sysfs(tz);
1393 ida_free(&thermal_tz_ida, tz->id);
1394 ida_destroy(&tz->ida);
1395 mutex_destroy(&tz->lock);
1396 device_unregister(&tz->device);
1398 thermal_notify_tz_delete(tz_id);
1400 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1403 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1404 * @name: thermal zone name to fetch the temperature
1406 * When only one zone is found with the passed name, returns a reference to it.
1408 * Return: On success returns a reference to an unique thermal zone with
1409 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1410 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1412 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1414 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1415 unsigned int found = 0;
1420 mutex_lock(&thermal_list_lock);
1421 list_for_each_entry(pos, &thermal_tz_list, node)
1422 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1426 mutex_unlock(&thermal_list_lock);
1428 /* nothing has been found, thus an error code for it */
1430 ref = ERR_PTR(-ENODEV);
1432 /* Success only when an unique zone is found */
1433 ref = ERR_PTR(-EEXIST);
1438 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1440 static int thermal_pm_notify(struct notifier_block *nb,
1441 unsigned long mode, void *_unused)
1443 struct thermal_zone_device *tz;
1446 case PM_HIBERNATION_PREPARE:
1447 case PM_RESTORE_PREPARE:
1448 case PM_SUSPEND_PREPARE:
1449 atomic_set(&in_suspend, 1);
1451 case PM_POST_HIBERNATION:
1452 case PM_POST_RESTORE:
1453 case PM_POST_SUSPEND:
1454 atomic_set(&in_suspend, 0);
1455 list_for_each_entry(tz, &thermal_tz_list, node) {
1456 thermal_zone_device_init(tz);
1457 thermal_zone_device_update(tz,
1458 THERMAL_EVENT_UNSPECIFIED);
1467 static struct notifier_block thermal_pm_nb = {
1468 .notifier_call = thermal_pm_notify,
1471 static int __init thermal_init(void)
1475 result = thermal_netlink_init();
1479 result = thermal_register_governors();
1483 result = class_register(&thermal_class);
1485 goto unregister_governors;
1487 result = register_pm_notifier(&thermal_pm_nb);
1489 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1494 unregister_governors:
1495 thermal_unregister_governors();
1497 ida_destroy(&thermal_tz_ida);
1498 ida_destroy(&thermal_cdev_ida);
1499 mutex_destroy(&thermal_list_lock);
1500 mutex_destroy(&thermal_governor_lock);
1503 postcore_initcall(thermal_init);