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 if (!device_is_registered(&tz->device))
209 gov = __find_governor(strim(policy));
213 ret = thermal_set_governor(tz, gov);
216 mutex_unlock(&tz->lock);
217 mutex_unlock(&thermal_governor_lock);
219 thermal_notify_tz_gov_change(tz->id, policy);
224 int thermal_build_list_of_policies(char *buf)
226 struct thermal_governor *pos;
229 mutex_lock(&thermal_governor_lock);
231 list_for_each_entry(pos, &thermal_governor_list, governor_list) {
232 count += scnprintf(buf + count, PAGE_SIZE - count, "%s ",
235 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
237 mutex_unlock(&thermal_governor_lock);
242 static void __init thermal_unregister_governors(void)
244 struct thermal_governor **governor;
246 for_each_governor_table(governor)
247 thermal_unregister_governor(*governor);
250 static int __init thermal_register_governors(void)
253 struct thermal_governor **governor;
255 for_each_governor_table(governor) {
256 ret = thermal_register_governor(*governor);
258 pr_err("Failed to register governor: '%s'",
263 pr_info("Registered thermal governor '%s'",
268 struct thermal_governor **gov;
270 for_each_governor_table(gov) {
273 thermal_unregister_governor(*gov);
281 * Zone update section: main control loop applied to each zone while monitoring
283 * in polling mode. The monitoring is done using a workqueue.
284 * Same update may be done on a zone by calling thermal_zone_device_update().
287 * - Non-critical trips will invoke the governor responsible for that zone;
288 * - Hot trips will produce a notification to userspace;
289 * - Critical trip point will cause a system shutdown.
291 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
295 mod_delayed_work(system_freezable_power_efficient_wq,
296 &tz->poll_queue, delay);
298 cancel_delayed_work(&tz->poll_queue);
301 static void monitor_thermal_zone(struct thermal_zone_device *tz)
303 if (tz->mode != THERMAL_DEVICE_ENABLED)
304 thermal_zone_device_set_polling(tz, 0);
305 else if (tz->passive)
306 thermal_zone_device_set_polling(tz, tz->passive_delay_jiffies);
307 else if (tz->polling_delay_jiffies)
308 thermal_zone_device_set_polling(tz, tz->polling_delay_jiffies);
311 static void handle_non_critical_trips(struct thermal_zone_device *tz, int trip)
313 tz->governor ? tz->governor->throttle(tz, trip) :
314 def_governor->throttle(tz, trip);
317 void thermal_zone_device_critical(struct thermal_zone_device *tz)
320 * poweroff_delay_ms must be a carefully profiled positive value.
321 * Its a must for forced_emergency_poweroff_work to be scheduled.
323 int poweroff_delay_ms = CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS;
325 dev_emerg(&tz->device, "%s: critical temperature reached, "
326 "shutting down\n", tz->type);
328 hw_protection_shutdown("Temperature too high", poweroff_delay_ms);
330 EXPORT_SYMBOL(thermal_zone_device_critical);
332 static void handle_critical_trips(struct thermal_zone_device *tz,
333 int trip, int trip_temp, enum thermal_trip_type trip_type)
335 /* If we have not crossed the trip_temp, we do not care. */
336 if (trip_temp <= 0 || tz->temperature < trip_temp)
339 trace_thermal_zone_trip(tz, trip, trip_type);
341 if (trip_type == THERMAL_TRIP_HOT && tz->ops->hot)
343 else if (trip_type == THERMAL_TRIP_CRITICAL)
344 tz->ops->critical(tz);
347 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
349 enum thermal_trip_type type;
350 int trip_temp, hyst = 0;
352 /* Ignore disabled trip points */
353 if (test_bit(trip, &tz->trips_disabled))
356 tz->ops->get_trip_temp(tz, trip, &trip_temp);
357 tz->ops->get_trip_type(tz, trip, &type);
358 if (tz->ops->get_trip_hyst)
359 tz->ops->get_trip_hyst(tz, trip, &hyst);
361 if (tz->last_temperature != THERMAL_TEMP_INVALID) {
362 if (tz->last_temperature < trip_temp &&
363 tz->temperature >= trip_temp)
364 thermal_notify_tz_trip_up(tz->id, trip,
366 if (tz->last_temperature >= trip_temp &&
367 tz->temperature < (trip_temp - hyst))
368 thermal_notify_tz_trip_down(tz->id, trip,
372 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
373 handle_critical_trips(tz, trip, trip_temp, type);
375 handle_non_critical_trips(tz, trip);
378 static void update_temperature(struct thermal_zone_device *tz)
382 ret = __thermal_zone_get_temp(tz, &temp);
385 dev_warn(&tz->device,
386 "failed to read out thermal zone (%d)\n",
391 tz->last_temperature = tz->temperature;
392 tz->temperature = temp;
394 trace_thermal_temperature(tz);
396 thermal_genl_sampling_temp(tz->id, temp);
399 static void thermal_zone_device_init(struct thermal_zone_device *tz)
401 struct thermal_instance *pos;
402 tz->temperature = THERMAL_TEMP_INVALID;
403 tz->prev_low_trip = -INT_MAX;
404 tz->prev_high_trip = INT_MAX;
405 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
406 pos->initialized = false;
409 void __thermal_zone_device_update(struct thermal_zone_device *tz,
410 enum thermal_notify_event event)
414 if (atomic_read(&in_suspend))
417 if (WARN_ONCE(!tz->ops->get_temp,
418 "'%s' must not be called without 'get_temp' ops set\n",
422 if (!thermal_zone_device_is_enabled(tz))
425 update_temperature(tz);
427 __thermal_zone_set_trips(tz);
429 tz->notify_event = event;
431 for (count = 0; count < tz->num_trips; count++)
432 handle_thermal_trip(tz, count);
434 monitor_thermal_zone(tz);
437 static int thermal_zone_device_set_mode(struct thermal_zone_device *tz,
438 enum thermal_device_mode mode)
442 mutex_lock(&tz->lock);
444 /* do nothing if mode isn't changing */
445 if (mode == tz->mode) {
446 mutex_unlock(&tz->lock);
451 if (!device_is_registered(&tz->device)) {
452 mutex_unlock(&tz->lock);
457 if (tz->ops->change_mode)
458 ret = tz->ops->change_mode(tz, mode);
463 __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
465 mutex_unlock(&tz->lock);
467 if (mode == THERMAL_DEVICE_ENABLED)
468 thermal_notify_tz_enable(tz->id);
470 thermal_notify_tz_disable(tz->id);
475 int thermal_zone_device_enable(struct thermal_zone_device *tz)
477 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_ENABLED);
479 EXPORT_SYMBOL_GPL(thermal_zone_device_enable);
481 int thermal_zone_device_disable(struct thermal_zone_device *tz)
483 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED);
485 EXPORT_SYMBOL_GPL(thermal_zone_device_disable);
487 int thermal_zone_device_is_enabled(struct thermal_zone_device *tz)
489 lockdep_assert_held(&tz->lock);
491 return tz->mode == THERMAL_DEVICE_ENABLED;
494 void thermal_zone_device_update(struct thermal_zone_device *tz,
495 enum thermal_notify_event event)
497 mutex_lock(&tz->lock);
498 if (device_is_registered(&tz->device))
499 __thermal_zone_device_update(tz, event);
500 mutex_unlock(&tz->lock);
502 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
504 static void thermal_zone_device_check(struct work_struct *work)
506 struct thermal_zone_device *tz = container_of(work, struct
509 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
512 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
515 struct thermal_governor *gov;
518 mutex_lock(&thermal_governor_lock);
519 list_for_each_entry(gov, &thermal_governor_list, governor_list) {
524 mutex_unlock(&thermal_governor_lock);
529 int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
532 struct thermal_cooling_device *cdev;
535 mutex_lock(&thermal_list_lock);
536 list_for_each_entry(cdev, &thermal_cdev_list, node) {
537 ret = cb(cdev, data);
541 mutex_unlock(&thermal_list_lock);
546 int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
549 struct thermal_zone_device *tz;
552 mutex_lock(&thermal_list_lock);
553 list_for_each_entry(tz, &thermal_tz_list, node) {
558 mutex_unlock(&thermal_list_lock);
563 struct thermal_zone_device *thermal_zone_get_by_id(int id)
565 struct thermal_zone_device *tz, *match = NULL;
567 mutex_lock(&thermal_list_lock);
568 list_for_each_entry(tz, &thermal_tz_list, node) {
574 mutex_unlock(&thermal_list_lock);
580 * Device management section: cooling devices, zones devices, and binding
582 * Set of functions provided by the thermal core for:
583 * - cooling devices lifecycle: registration, unregistration,
584 * binding, and unbinding.
585 * - thermal zone devices lifecycle: registration, unregistration,
586 * binding, and unbinding.
590 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
591 * @tz: pointer to struct thermal_zone_device
592 * @trip: indicates which trip point the cooling devices is
593 * associated with in this thermal zone.
594 * @cdev: pointer to struct thermal_cooling_device
595 * @upper: the Maximum cooling state for this trip point.
596 * THERMAL_NO_LIMIT means no upper limit,
597 * and the cooling device can be in max_state.
598 * @lower: the Minimum cooling state can be used for this trip point.
599 * THERMAL_NO_LIMIT means no lower limit,
600 * and the cooling device can be in cooling state 0.
601 * @weight: The weight of the cooling device to be bound to the
602 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
605 * This interface function bind a thermal cooling device to the certain trip
606 * point of a thermal zone device.
607 * This function is usually called in the thermal zone device .bind callback.
609 * Return: 0 on success, the proper error value otherwise.
611 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
613 struct thermal_cooling_device *cdev,
614 unsigned long upper, unsigned long lower,
617 struct thermal_instance *dev;
618 struct thermal_instance *pos;
619 struct thermal_zone_device *pos1;
620 struct thermal_cooling_device *pos2;
623 if (trip >= tz->num_trips || trip < 0)
626 list_for_each_entry(pos1, &thermal_tz_list, node) {
630 list_for_each_entry(pos2, &thermal_cdev_list, node) {
635 if (tz != pos1 || cdev != pos2)
638 /* lower default 0, upper default max_state */
639 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
640 upper = upper == THERMAL_NO_LIMIT ? cdev->max_state : upper;
642 if (lower > upper || upper > cdev->max_state)
645 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
653 dev->target = THERMAL_NO_TARGET;
654 dev->weight = weight;
656 result = ida_alloc(&tz->ida, GFP_KERNEL);
661 sprintf(dev->name, "cdev%d", dev->id);
663 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
667 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
668 sysfs_attr_init(&dev->attr.attr);
669 dev->attr.attr.name = dev->attr_name;
670 dev->attr.attr.mode = 0444;
671 dev->attr.show = trip_point_show;
672 result = device_create_file(&tz->device, &dev->attr);
674 goto remove_symbol_link;
676 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
677 sysfs_attr_init(&dev->weight_attr.attr);
678 dev->weight_attr.attr.name = dev->weight_attr_name;
679 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
680 dev->weight_attr.show = weight_show;
681 dev->weight_attr.store = weight_store;
682 result = device_create_file(&tz->device, &dev->weight_attr);
684 goto remove_trip_file;
686 mutex_lock(&tz->lock);
687 mutex_lock(&cdev->lock);
688 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
689 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
694 list_add_tail(&dev->tz_node, &tz->thermal_instances);
695 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
696 atomic_set(&tz->need_update, 1);
698 mutex_unlock(&cdev->lock);
699 mutex_unlock(&tz->lock);
704 device_remove_file(&tz->device, &dev->weight_attr);
706 device_remove_file(&tz->device, &dev->attr);
708 sysfs_remove_link(&tz->device.kobj, dev->name);
710 ida_free(&tz->ida, dev->id);
715 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
718 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
720 * @tz: pointer to a struct thermal_zone_device.
721 * @trip: indicates which trip point the cooling devices is
722 * associated with in this thermal zone.
723 * @cdev: pointer to a struct thermal_cooling_device.
725 * This interface function unbind a thermal cooling device from the certain
726 * trip point of a thermal zone device.
727 * This function is usually called in the thermal zone device .unbind callback.
729 * Return: 0 on success, the proper error value otherwise.
731 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
733 struct thermal_cooling_device *cdev)
735 struct thermal_instance *pos, *next;
737 mutex_lock(&tz->lock);
738 mutex_lock(&cdev->lock);
739 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
740 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
741 list_del(&pos->tz_node);
742 list_del(&pos->cdev_node);
743 mutex_unlock(&cdev->lock);
744 mutex_unlock(&tz->lock);
748 mutex_unlock(&cdev->lock);
749 mutex_unlock(&tz->lock);
754 device_remove_file(&tz->device, &pos->weight_attr);
755 device_remove_file(&tz->device, &pos->attr);
756 sysfs_remove_link(&tz->device.kobj, pos->name);
757 ida_free(&tz->ida, pos->id);
761 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
763 static void thermal_release(struct device *dev)
765 struct thermal_zone_device *tz;
766 struct thermal_cooling_device *cdev;
768 if (!strncmp(dev_name(dev), "thermal_zone",
769 sizeof("thermal_zone") - 1)) {
770 tz = to_thermal_zone(dev);
771 thermal_zone_destroy_device_groups(tz);
772 mutex_destroy(&tz->lock);
774 } else if (!strncmp(dev_name(dev), "cooling_device",
775 sizeof("cooling_device") - 1)) {
776 cdev = to_cooling_device(dev);
781 static struct class thermal_class = {
783 .dev_release = thermal_release,
787 void print_bind_err_msg(struct thermal_zone_device *tz,
788 struct thermal_cooling_device *cdev, int ret)
790 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
791 tz->type, cdev->type, ret);
794 static void __bind(struct thermal_zone_device *tz, int mask,
795 struct thermal_cooling_device *cdev,
796 unsigned long *limits,
801 for (i = 0; i < tz->num_trips; i++) {
802 if (mask & (1 << i)) {
803 unsigned long upper, lower;
805 upper = THERMAL_NO_LIMIT;
806 lower = THERMAL_NO_LIMIT;
808 lower = limits[i * 2];
809 upper = limits[i * 2 + 1];
811 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
815 print_bind_err_msg(tz, cdev, ret);
820 static void bind_cdev(struct thermal_cooling_device *cdev)
823 const struct thermal_zone_params *tzp;
824 struct thermal_zone_device *pos = NULL;
826 mutex_lock(&thermal_list_lock);
828 list_for_each_entry(pos, &thermal_tz_list, node) {
829 if (!pos->tzp && !pos->ops->bind)
832 if (pos->ops->bind) {
833 ret = pos->ops->bind(pos, cdev);
835 print_bind_err_msg(pos, cdev, ret);
840 if (!tzp || !tzp->tbp)
843 for (i = 0; i < tzp->num_tbps; i++) {
844 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
846 if (tzp->tbp[i].match(pos, cdev))
848 tzp->tbp[i].cdev = cdev;
849 __bind(pos, tzp->tbp[i].trip_mask, cdev,
850 tzp->tbp[i].binding_limits,
855 mutex_unlock(&thermal_list_lock);
859 * __thermal_cooling_device_register() - register a new thermal cooling device
860 * @np: a pointer to a device tree node.
861 * @type: the thermal cooling device type.
862 * @devdata: device private data.
863 * @ops: standard thermal cooling devices callbacks.
865 * This interface function adds a new thermal cooling device (fan/processor/...)
866 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
867 * to all the thermal zone devices registered at the same time.
868 * It also gives the opportunity to link the cooling device to a device tree
869 * node, so that it can be bound to a thermal zone created out of device tree.
871 * Return: a pointer to the created struct thermal_cooling_device or an
872 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
874 static struct thermal_cooling_device *
875 __thermal_cooling_device_register(struct device_node *np,
876 const char *type, void *devdata,
877 const struct thermal_cooling_device_ops *ops)
879 struct thermal_cooling_device *cdev;
880 struct thermal_zone_device *pos = NULL;
883 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
885 return ERR_PTR(-EINVAL);
887 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
889 return ERR_PTR(-ENOMEM);
891 ret = ida_alloc(&thermal_cdev_ida, GFP_KERNEL);
897 cdev->type = kstrdup(type ? type : "", GFP_KERNEL);
903 mutex_init(&cdev->lock);
904 INIT_LIST_HEAD(&cdev->thermal_instances);
907 cdev->updated = false;
908 cdev->device.class = &thermal_class;
909 cdev->devdata = devdata;
911 ret = cdev->ops->get_max_state(cdev, &cdev->max_state);
915 thermal_cooling_device_setup_sysfs(cdev);
916 ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
918 thermal_cooling_device_destroy_sysfs(cdev);
921 ret = device_register(&cdev->device);
925 /* Add 'this' new cdev to the global cdev list */
926 mutex_lock(&thermal_list_lock);
927 list_add(&cdev->node, &thermal_cdev_list);
928 mutex_unlock(&thermal_list_lock);
930 /* Update binding information for 'this' new cdev */
933 mutex_lock(&thermal_list_lock);
934 list_for_each_entry(pos, &thermal_tz_list, node)
935 if (atomic_cmpxchg(&pos->need_update, 1, 0))
936 thermal_zone_device_update(pos,
937 THERMAL_EVENT_UNSPECIFIED);
938 mutex_unlock(&thermal_list_lock);
943 thermal_cooling_device_destroy_sysfs(cdev);
945 put_device(&cdev->device);
948 ida_free(&thermal_cdev_ida, id);
955 * thermal_cooling_device_register() - register a new thermal cooling device
956 * @type: the thermal cooling device type.
957 * @devdata: device private data.
958 * @ops: standard thermal cooling devices callbacks.
960 * This interface function adds a new thermal cooling device (fan/processor/...)
961 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
962 * to all the thermal zone devices registered at the same time.
964 * Return: a pointer to the created struct thermal_cooling_device or an
965 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
967 struct thermal_cooling_device *
968 thermal_cooling_device_register(const char *type, void *devdata,
969 const struct thermal_cooling_device_ops *ops)
971 return __thermal_cooling_device_register(NULL, type, devdata, ops);
973 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
976 * thermal_of_cooling_device_register() - register an OF thermal cooling device
977 * @np: a pointer to a device tree node.
978 * @type: the thermal cooling device type.
979 * @devdata: device private data.
980 * @ops: standard thermal cooling devices callbacks.
982 * This function will register a cooling device with device tree node reference.
983 * This interface function adds a new thermal cooling device (fan/processor/...)
984 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
985 * to all the thermal zone devices registered at the same time.
987 * Return: a pointer to the created struct thermal_cooling_device or an
988 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
990 struct thermal_cooling_device *
991 thermal_of_cooling_device_register(struct device_node *np,
992 const char *type, void *devdata,
993 const struct thermal_cooling_device_ops *ops)
995 return __thermal_cooling_device_register(np, type, devdata, ops);
997 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
999 static void thermal_cooling_device_release(struct device *dev, void *res)
1001 thermal_cooling_device_unregister(
1002 *(struct thermal_cooling_device **)res);
1006 * devm_thermal_of_cooling_device_register() - register an OF thermal cooling
1008 * @dev: a valid struct device pointer of a sensor device.
1009 * @np: a pointer to a device tree node.
1010 * @type: the thermal cooling device type.
1011 * @devdata: device private data.
1012 * @ops: standard thermal cooling devices callbacks.
1014 * This function will register a cooling device with device tree node reference.
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.
1019 * Return: a pointer to the created struct thermal_cooling_device or an
1020 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1022 struct thermal_cooling_device *
1023 devm_thermal_of_cooling_device_register(struct device *dev,
1024 struct device_node *np,
1025 char *type, void *devdata,
1026 const struct thermal_cooling_device_ops *ops)
1028 struct thermal_cooling_device **ptr, *tcd;
1030 ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr),
1033 return ERR_PTR(-ENOMEM);
1035 tcd = __thermal_cooling_device_register(np, type, devdata, ops);
1042 devres_add(dev, ptr);
1046 EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register);
1048 static void __unbind(struct thermal_zone_device *tz, int mask,
1049 struct thermal_cooling_device *cdev)
1053 for (i = 0; i < tz->num_trips; i++)
1054 if (mask & (1 << i))
1055 thermal_zone_unbind_cooling_device(tz, i, cdev);
1059 * thermal_cooling_device_unregister - removes a thermal cooling device
1060 * @cdev: the thermal cooling device to remove.
1062 * thermal_cooling_device_unregister() must be called when a registered
1063 * thermal cooling device is no longer needed.
1065 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1068 const struct thermal_zone_params *tzp;
1069 struct thermal_zone_device *tz;
1070 struct thermal_cooling_device *pos = NULL;
1075 mutex_lock(&thermal_list_lock);
1076 list_for_each_entry(pos, &thermal_cdev_list, node)
1080 /* thermal cooling device not found */
1081 mutex_unlock(&thermal_list_lock);
1084 list_del(&cdev->node);
1086 /* Unbind all thermal zones associated with 'this' cdev */
1087 list_for_each_entry(tz, &thermal_tz_list, node) {
1088 if (tz->ops->unbind) {
1089 tz->ops->unbind(tz, cdev);
1093 if (!tz->tzp || !tz->tzp->tbp)
1097 for (i = 0; i < tzp->num_tbps; i++) {
1098 if (tzp->tbp[i].cdev == cdev) {
1099 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1100 tzp->tbp[i].cdev = NULL;
1105 mutex_unlock(&thermal_list_lock);
1107 ida_free(&thermal_cdev_ida, cdev->id);
1108 device_del(&cdev->device);
1109 thermal_cooling_device_destroy_sysfs(cdev);
1111 put_device(&cdev->device);
1113 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1115 static void bind_tz(struct thermal_zone_device *tz)
1118 struct thermal_cooling_device *pos = NULL;
1119 const struct thermal_zone_params *tzp = tz->tzp;
1121 if (!tzp && !tz->ops->bind)
1124 mutex_lock(&thermal_list_lock);
1126 /* If there is ops->bind, try to use ops->bind */
1127 if (tz->ops->bind) {
1128 list_for_each_entry(pos, &thermal_cdev_list, node) {
1129 ret = tz->ops->bind(tz, pos);
1131 print_bind_err_msg(tz, pos, ret);
1136 if (!tzp || !tzp->tbp)
1139 list_for_each_entry(pos, &thermal_cdev_list, node) {
1140 for (i = 0; i < tzp->num_tbps; i++) {
1141 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
1143 if (tzp->tbp[i].match(tz, pos))
1145 tzp->tbp[i].cdev = pos;
1146 __bind(tz, tzp->tbp[i].trip_mask, pos,
1147 tzp->tbp[i].binding_limits,
1148 tzp->tbp[i].weight);
1152 mutex_unlock(&thermal_list_lock);
1155 static void thermal_set_delay_jiffies(unsigned long *delay_jiffies, int delay_ms)
1157 *delay_jiffies = msecs_to_jiffies(delay_ms);
1158 if (delay_ms > 1000)
1159 *delay_jiffies = round_jiffies(*delay_jiffies);
1163 * thermal_zone_device_register_with_trips() - register a new thermal zone device
1164 * @type: the thermal zone device type
1165 * @trips: a pointer to an array of thermal trips
1166 * @num_trips: the number of trip points the thermal zone support
1167 * @mask: a bit string indicating the writeablility of trip points
1168 * @devdata: private device data
1169 * @ops: standard thermal zone device callbacks
1170 * @tzp: thermal zone platform parameters
1171 * @passive_delay: number of milliseconds to wait between polls when
1172 * performing passive cooling
1173 * @polling_delay: number of milliseconds to wait between polls when checking
1174 * whether trip points have been crossed (0 for interrupt
1177 * This interface function adds a new thermal zone device (sensor) to
1178 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1179 * thermal cooling devices registered at the same time.
1180 * thermal_zone_device_unregister() must be called when the device is no
1181 * longer needed. The passive cooling depends on the .get_trend() return value.
1183 * Return: a pointer to the created struct thermal_zone_device or an
1184 * in case of error, an ERR_PTR. Caller must check return value with
1185 * IS_ERR*() helpers.
1187 struct thermal_zone_device *
1188 thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *trips, int num_trips, int mask,
1189 void *devdata, struct thermal_zone_device_ops *ops,
1190 struct thermal_zone_params *tzp, int passive_delay,
1193 struct thermal_zone_device *tz;
1194 enum thermal_trip_type trip_type;
1199 struct thermal_governor *governor;
1201 if (!type || strlen(type) == 0) {
1202 pr_err("No thermal zone type defined\n");
1203 return ERR_PTR(-EINVAL);
1206 if (strlen(type) >= THERMAL_NAME_LENGTH) {
1207 pr_err("Thermal zone name (%s) too long, should be under %d chars\n",
1208 type, THERMAL_NAME_LENGTH);
1209 return ERR_PTR(-EINVAL);
1213 * Max trip count can't exceed 31 as the "mask >> num_trips" condition.
1214 * For example, shifting by 32 will result in compiler warning:
1215 * warning: right shift count >= width of type [-Wshift-count- overflow]
1217 * Also "mask >> num_trips" will always be true with 32 bit shift.
1218 * E.g. mask = 0x80000000 for trip id 31 to be RW. Then
1219 * mask >> 32 = 0x80000000
1220 * This will result in failure for the below condition.
1222 * Check will be true when the bit 31 of the mask is set.
1223 * 32 bit shift will cause overflow of 4 byte integer.
1225 if (num_trips > (BITS_PER_TYPE(int) - 1) || num_trips < 0 || mask >> num_trips) {
1226 pr_err("Incorrect number of thermal trips\n");
1227 return ERR_PTR(-EINVAL);
1231 pr_err("Thermal zone device ops not defined\n");
1232 return ERR_PTR(-EINVAL);
1235 if (num_trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
1236 return ERR_PTR(-EINVAL);
1238 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
1240 return ERR_PTR(-ENOMEM);
1242 INIT_LIST_HEAD(&tz->thermal_instances);
1244 mutex_init(&tz->lock);
1245 id = ida_alloc(&thermal_tz_ida, GFP_KERNEL);
1252 strscpy(tz->type, type, sizeof(tz->type));
1255 ops->critical = thermal_zone_device_critical;
1259 tz->device.class = &thermal_class;
1260 tz->devdata = devdata;
1262 tz->num_trips = num_trips;
1264 thermal_set_delay_jiffies(&tz->passive_delay_jiffies, passive_delay);
1265 thermal_set_delay_jiffies(&tz->polling_delay_jiffies, polling_delay);
1268 /* Add nodes that are always present via .groups */
1269 result = thermal_zone_create_device_groups(tz, mask);
1273 /* A new thermal zone needs to be updated anyway. */
1274 atomic_set(&tz->need_update, 1);
1276 result = dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1278 thermal_zone_destroy_device_groups(tz);
1281 result = device_register(&tz->device);
1283 goto release_device;
1285 for (count = 0; count < num_trips; count++) {
1286 if (tz->ops->get_trip_type(tz, count, &trip_type) ||
1287 tz->ops->get_trip_temp(tz, count, &trip_temp) ||
1289 set_bit(count, &tz->trips_disabled);
1292 /* Update 'this' zone's governor information */
1293 mutex_lock(&thermal_governor_lock);
1296 governor = __find_governor(tz->tzp->governor_name);
1298 governor = def_governor;
1300 result = thermal_set_governor(tz, governor);
1302 mutex_unlock(&thermal_governor_lock);
1306 mutex_unlock(&thermal_governor_lock);
1308 if (!tz->tzp || !tz->tzp->no_hwmon) {
1309 result = thermal_add_hwmon_sysfs(tz);
1314 mutex_lock(&thermal_list_lock);
1315 list_add_tail(&tz->node, &thermal_tz_list);
1316 mutex_unlock(&thermal_list_lock);
1318 /* Bind cooling devices for this zone */
1321 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
1323 thermal_zone_device_init(tz);
1324 /* Update the new thermal zone and mark it as already updated. */
1325 if (atomic_cmpxchg(&tz->need_update, 1, 0))
1326 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1328 thermal_notify_tz_create(tz->id, tz->type);
1333 device_del(&tz->device);
1335 put_device(&tz->device);
1338 ida_free(&thermal_tz_ida, id);
1341 return ERR_PTR(result);
1343 EXPORT_SYMBOL_GPL(thermal_zone_device_register_with_trips);
1345 struct thermal_zone_device *thermal_zone_device_register(const char *type, int ntrips, int mask,
1346 void *devdata, struct thermal_zone_device_ops *ops,
1347 struct thermal_zone_params *tzp, int passive_delay,
1350 return thermal_zone_device_register_with_trips(type, NULL, ntrips, mask,
1352 passive_delay, polling_delay);
1354 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
1357 * thermal_zone_device_unregister - removes the registered thermal zone device
1358 * @tz: the thermal zone device to remove
1360 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1363 const struct thermal_zone_params *tzp;
1364 struct thermal_cooling_device *cdev;
1365 struct thermal_zone_device *pos = NULL;
1373 mutex_lock(&thermal_list_lock);
1374 list_for_each_entry(pos, &thermal_tz_list, node)
1378 /* thermal zone device not found */
1379 mutex_unlock(&thermal_list_lock);
1382 list_del(&tz->node);
1384 /* Unbind all cdevs associated with 'this' thermal zone */
1385 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1386 if (tz->ops->unbind) {
1387 tz->ops->unbind(tz, cdev);
1391 if (!tzp || !tzp->tbp)
1394 for (i = 0; i < tzp->num_tbps; i++) {
1395 if (tzp->tbp[i].cdev == cdev) {
1396 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1397 tzp->tbp[i].cdev = NULL;
1402 mutex_unlock(&thermal_list_lock);
1404 cancel_delayed_work_sync(&tz->poll_queue);
1406 thermal_set_governor(tz, NULL);
1408 thermal_remove_hwmon_sysfs(tz);
1409 ida_free(&thermal_tz_ida, tz->id);
1410 ida_destroy(&tz->ida);
1412 mutex_lock(&tz->lock);
1413 device_del(&tz->device);
1414 mutex_unlock(&tz->lock);
1416 put_device(&tz->device);
1418 thermal_notify_tz_delete(tz_id);
1420 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1423 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1424 * @name: thermal zone name to fetch the temperature
1426 * When only one zone is found with the passed name, returns a reference to it.
1428 * Return: On success returns a reference to an unique thermal zone with
1429 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1430 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1432 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1434 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1435 unsigned int found = 0;
1440 mutex_lock(&thermal_list_lock);
1441 list_for_each_entry(pos, &thermal_tz_list, node)
1442 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1446 mutex_unlock(&thermal_list_lock);
1448 /* nothing has been found, thus an error code for it */
1450 ref = ERR_PTR(-ENODEV);
1452 /* Success only when an unique zone is found */
1453 ref = ERR_PTR(-EEXIST);
1458 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1460 static int thermal_pm_notify(struct notifier_block *nb,
1461 unsigned long mode, void *_unused)
1463 struct thermal_zone_device *tz;
1466 case PM_HIBERNATION_PREPARE:
1467 case PM_RESTORE_PREPARE:
1468 case PM_SUSPEND_PREPARE:
1469 atomic_set(&in_suspend, 1);
1471 case PM_POST_HIBERNATION:
1472 case PM_POST_RESTORE:
1473 case PM_POST_SUSPEND:
1474 atomic_set(&in_suspend, 0);
1475 list_for_each_entry(tz, &thermal_tz_list, node) {
1476 thermal_zone_device_init(tz);
1477 thermal_zone_device_update(tz,
1478 THERMAL_EVENT_UNSPECIFIED);
1487 static struct notifier_block thermal_pm_nb = {
1488 .notifier_call = thermal_pm_notify,
1491 static int __init thermal_init(void)
1495 result = thermal_netlink_init();
1499 result = thermal_register_governors();
1503 result = class_register(&thermal_class);
1505 goto unregister_governors;
1507 result = register_pm_notifier(&thermal_pm_nb);
1509 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1514 unregister_governors:
1515 thermal_unregister_governors();
1517 ida_destroy(&thermal_tz_ida);
1518 ida_destroy(&thermal_cdev_ida);
1519 mutex_destroy(&thermal_list_lock);
1520 mutex_destroy(&thermal_governor_lock);
1523 postcore_initcall(thermal_init);