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/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 inline bool should_stop_polling(struct thermal_zone_device *tz)
300 return !thermal_zone_device_is_enabled(tz);
303 static void monitor_thermal_zone(struct thermal_zone_device *tz)
307 stop = should_stop_polling(tz);
309 mutex_lock(&tz->lock);
311 if (!stop && tz->passive)
312 thermal_zone_device_set_polling(tz, tz->passive_delay_jiffies);
313 else if (!stop && tz->polling_delay_jiffies)
314 thermal_zone_device_set_polling(tz, tz->polling_delay_jiffies);
316 thermal_zone_device_set_polling(tz, 0);
318 mutex_unlock(&tz->lock);
321 static void handle_non_critical_trips(struct thermal_zone_device *tz, int trip)
323 tz->governor ? tz->governor->throttle(tz, trip) :
324 def_governor->throttle(tz, trip);
327 void thermal_zone_device_critical(struct thermal_zone_device *tz)
330 * poweroff_delay_ms must be a carefully profiled positive value.
331 * Its a must for forced_emergency_poweroff_work to be scheduled.
333 int poweroff_delay_ms = CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS;
335 dev_emerg(&tz->device, "%s: critical temperature reached, "
336 "shutting down\n", tz->type);
338 hw_protection_shutdown("Temperature too high", poweroff_delay_ms);
340 EXPORT_SYMBOL(thermal_zone_device_critical);
342 static void handle_critical_trips(struct thermal_zone_device *tz,
343 int trip, enum thermal_trip_type trip_type)
347 tz->ops->get_trip_temp(tz, trip, &trip_temp);
349 /* If we have not crossed the trip_temp, we do not care. */
350 if (trip_temp <= 0 || tz->temperature < trip_temp)
353 trace_thermal_zone_trip(tz, trip, trip_type);
355 if (trip_type == THERMAL_TRIP_HOT && tz->ops->hot)
357 else if (trip_type == THERMAL_TRIP_CRITICAL)
358 tz->ops->critical(tz);
361 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
363 enum thermal_trip_type type;
364 int trip_temp, hyst = 0;
366 /* Ignore disabled trip points */
367 if (test_bit(trip, &tz->trips_disabled))
370 tz->ops->get_trip_temp(tz, trip, &trip_temp);
371 tz->ops->get_trip_type(tz, trip, &type);
372 if (tz->ops->get_trip_hyst)
373 tz->ops->get_trip_hyst(tz, trip, &hyst);
375 if (tz->last_temperature != THERMAL_TEMP_INVALID) {
376 if (tz->last_temperature < trip_temp &&
377 tz->temperature >= trip_temp)
378 thermal_notify_tz_trip_up(tz->id, trip,
380 if (tz->last_temperature >= trip_temp &&
381 tz->temperature < (trip_temp - hyst))
382 thermal_notify_tz_trip_down(tz->id, trip,
386 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
387 handle_critical_trips(tz, trip, type);
389 handle_non_critical_trips(tz, trip);
391 * Alright, we handled this trip successfully.
392 * So, start monitoring again.
394 monitor_thermal_zone(tz);
397 static void update_temperature(struct thermal_zone_device *tz)
401 ret = thermal_zone_get_temp(tz, &temp);
404 dev_warn(&tz->device,
405 "failed to read out thermal zone (%d)\n",
410 mutex_lock(&tz->lock);
411 tz->last_temperature = tz->temperature;
412 tz->temperature = temp;
413 mutex_unlock(&tz->lock);
415 trace_thermal_temperature(tz);
417 thermal_genl_sampling_temp(tz->id, temp);
420 static void thermal_zone_device_init(struct thermal_zone_device *tz)
422 struct thermal_instance *pos;
423 tz->temperature = THERMAL_TEMP_INVALID;
424 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
425 pos->initialized = false;
428 static int thermal_zone_device_set_mode(struct thermal_zone_device *tz,
429 enum thermal_device_mode mode)
433 mutex_lock(&tz->lock);
435 /* do nothing if mode isn't changing */
436 if (mode == tz->mode) {
437 mutex_unlock(&tz->lock);
442 if (tz->ops->change_mode)
443 ret = tz->ops->change_mode(tz, mode);
448 mutex_unlock(&tz->lock);
450 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
452 if (mode == THERMAL_DEVICE_ENABLED)
453 thermal_notify_tz_enable(tz->id);
455 thermal_notify_tz_disable(tz->id);
460 int thermal_zone_device_enable(struct thermal_zone_device *tz)
462 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_ENABLED);
464 EXPORT_SYMBOL_GPL(thermal_zone_device_enable);
466 int thermal_zone_device_disable(struct thermal_zone_device *tz)
468 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED);
470 EXPORT_SYMBOL_GPL(thermal_zone_device_disable);
472 int thermal_zone_device_is_enabled(struct thermal_zone_device *tz)
474 enum thermal_device_mode mode;
476 mutex_lock(&tz->lock);
480 mutex_unlock(&tz->lock);
482 return mode == THERMAL_DEVICE_ENABLED;
485 void thermal_zone_device_update(struct thermal_zone_device *tz,
486 enum thermal_notify_event event)
490 if (should_stop_polling(tz))
493 if (atomic_read(&in_suspend))
496 if (WARN_ONCE(!tz->ops->get_temp, "'%s' must not be called without "
497 "'get_temp' ops set\n", __func__))
500 update_temperature(tz);
502 thermal_zone_set_trips(tz);
504 tz->notify_event = event;
506 for (count = 0; count < tz->trips; count++)
507 handle_thermal_trip(tz, count);
509 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
511 static void thermal_zone_device_check(struct work_struct *work)
513 struct thermal_zone_device *tz = container_of(work, struct
516 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
519 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
522 struct thermal_governor *gov;
525 mutex_lock(&thermal_governor_lock);
526 list_for_each_entry(gov, &thermal_governor_list, governor_list) {
531 mutex_unlock(&thermal_governor_lock);
536 int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
539 struct thermal_cooling_device *cdev;
542 mutex_lock(&thermal_list_lock);
543 list_for_each_entry(cdev, &thermal_cdev_list, node) {
544 ret = cb(cdev, data);
548 mutex_unlock(&thermal_list_lock);
553 int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
556 struct thermal_zone_device *tz;
559 mutex_lock(&thermal_list_lock);
560 list_for_each_entry(tz, &thermal_tz_list, node) {
565 mutex_unlock(&thermal_list_lock);
570 struct thermal_zone_device *thermal_zone_get_by_id(int id)
572 struct thermal_zone_device *tz, *match = NULL;
574 mutex_lock(&thermal_list_lock);
575 list_for_each_entry(tz, &thermal_tz_list, node) {
581 mutex_unlock(&thermal_list_lock);
587 * Device management section: cooling devices, zones devices, and binding
589 * Set of functions provided by the thermal core for:
590 * - cooling devices lifecycle: registration, unregistration,
591 * binding, and unbinding.
592 * - thermal zone devices lifecycle: registration, unregistration,
593 * binding, and unbinding.
597 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
598 * @tz: pointer to struct thermal_zone_device
599 * @trip: indicates which trip point the cooling devices is
600 * associated with in this thermal zone.
601 * @cdev: pointer to struct thermal_cooling_device
602 * @upper: the Maximum cooling state for this trip point.
603 * THERMAL_NO_LIMIT means no upper limit,
604 * and the cooling device can be in max_state.
605 * @lower: the Minimum cooling state can be used for this trip point.
606 * THERMAL_NO_LIMIT means no lower limit,
607 * and the cooling device can be in cooling state 0.
608 * @weight: The weight of the cooling device to be bound to the
609 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
612 * This interface function bind a thermal cooling device to the certain trip
613 * point of a thermal zone device.
614 * This function is usually called in the thermal zone device .bind callback.
616 * Return: 0 on success, the proper error value otherwise.
618 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
620 struct thermal_cooling_device *cdev,
621 unsigned long upper, unsigned long lower,
624 struct thermal_instance *dev;
625 struct thermal_instance *pos;
626 struct thermal_zone_device *pos1;
627 struct thermal_cooling_device *pos2;
628 unsigned long max_state;
631 if (trip >= tz->trips || trip < 0)
634 list_for_each_entry(pos1, &thermal_tz_list, node) {
638 list_for_each_entry(pos2, &thermal_cdev_list, node) {
643 if (tz != pos1 || cdev != pos2)
646 ret = cdev->ops->get_max_state(cdev, &max_state);
650 /* lower default 0, upper default max_state */
651 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
652 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
654 if (lower > upper || upper > max_state)
657 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
665 dev->target = THERMAL_NO_TARGET;
666 dev->weight = weight;
668 result = ida_simple_get(&tz->ida, 0, 0, GFP_KERNEL);
673 sprintf(dev->name, "cdev%d", dev->id);
675 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
679 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
680 sysfs_attr_init(&dev->attr.attr);
681 dev->attr.attr.name = dev->attr_name;
682 dev->attr.attr.mode = 0444;
683 dev->attr.show = trip_point_show;
684 result = device_create_file(&tz->device, &dev->attr);
686 goto remove_symbol_link;
688 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
689 sysfs_attr_init(&dev->weight_attr.attr);
690 dev->weight_attr.attr.name = dev->weight_attr_name;
691 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
692 dev->weight_attr.show = weight_show;
693 dev->weight_attr.store = weight_store;
694 result = device_create_file(&tz->device, &dev->weight_attr);
696 goto remove_trip_file;
698 mutex_lock(&tz->lock);
699 mutex_lock(&cdev->lock);
700 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
701 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
706 list_add_tail(&dev->tz_node, &tz->thermal_instances);
707 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
708 atomic_set(&tz->need_update, 1);
710 mutex_unlock(&cdev->lock);
711 mutex_unlock(&tz->lock);
716 device_remove_file(&tz->device, &dev->weight_attr);
718 device_remove_file(&tz->device, &dev->attr);
720 sysfs_remove_link(&tz->device.kobj, dev->name);
722 ida_simple_remove(&tz->ida, dev->id);
727 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
730 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
732 * @tz: pointer to a struct thermal_zone_device.
733 * @trip: indicates which trip point the cooling devices is
734 * associated with in this thermal zone.
735 * @cdev: pointer to a struct thermal_cooling_device.
737 * This interface function unbind a thermal cooling device from the certain
738 * trip point of a thermal zone device.
739 * This function is usually called in the thermal zone device .unbind callback.
741 * Return: 0 on success, the proper error value otherwise.
743 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
745 struct thermal_cooling_device *cdev)
747 struct thermal_instance *pos, *next;
749 mutex_lock(&tz->lock);
750 mutex_lock(&cdev->lock);
751 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
752 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
753 list_del(&pos->tz_node);
754 list_del(&pos->cdev_node);
755 mutex_unlock(&cdev->lock);
756 mutex_unlock(&tz->lock);
760 mutex_unlock(&cdev->lock);
761 mutex_unlock(&tz->lock);
766 device_remove_file(&tz->device, &pos->weight_attr);
767 device_remove_file(&tz->device, &pos->attr);
768 sysfs_remove_link(&tz->device.kobj, pos->name);
769 ida_simple_remove(&tz->ida, pos->id);
773 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
775 static void thermal_release(struct device *dev)
777 struct thermal_zone_device *tz;
778 struct thermal_cooling_device *cdev;
780 if (!strncmp(dev_name(dev), "thermal_zone",
781 sizeof("thermal_zone") - 1)) {
782 tz = to_thermal_zone(dev);
783 thermal_zone_destroy_device_groups(tz);
785 } else if (!strncmp(dev_name(dev), "cooling_device",
786 sizeof("cooling_device") - 1)) {
787 cdev = to_cooling_device(dev);
792 static struct class thermal_class = {
794 .dev_release = thermal_release,
798 void print_bind_err_msg(struct thermal_zone_device *tz,
799 struct thermal_cooling_device *cdev, int ret)
801 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
802 tz->type, cdev->type, ret);
805 static void __bind(struct thermal_zone_device *tz, int mask,
806 struct thermal_cooling_device *cdev,
807 unsigned long *limits,
812 for (i = 0; i < tz->trips; i++) {
813 if (mask & (1 << i)) {
814 unsigned long upper, lower;
816 upper = THERMAL_NO_LIMIT;
817 lower = THERMAL_NO_LIMIT;
819 lower = limits[i * 2];
820 upper = limits[i * 2 + 1];
822 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
826 print_bind_err_msg(tz, cdev, ret);
831 static void bind_cdev(struct thermal_cooling_device *cdev)
834 const struct thermal_zone_params *tzp;
835 struct thermal_zone_device *pos = NULL;
837 mutex_lock(&thermal_list_lock);
839 list_for_each_entry(pos, &thermal_tz_list, node) {
840 if (!pos->tzp && !pos->ops->bind)
843 if (pos->ops->bind) {
844 ret = pos->ops->bind(pos, cdev);
846 print_bind_err_msg(pos, cdev, ret);
851 if (!tzp || !tzp->tbp)
854 for (i = 0; i < tzp->num_tbps; i++) {
855 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
857 if (tzp->tbp[i].match(pos, cdev))
859 tzp->tbp[i].cdev = cdev;
860 __bind(pos, tzp->tbp[i].trip_mask, cdev,
861 tzp->tbp[i].binding_limits,
866 mutex_unlock(&thermal_list_lock);
870 * __thermal_cooling_device_register() - register a new thermal cooling device
871 * @np: a pointer to a device tree node.
872 * @type: the thermal cooling device type.
873 * @devdata: device private data.
874 * @ops: standard thermal cooling devices callbacks.
876 * This interface function adds a new thermal cooling device (fan/processor/...)
877 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
878 * to all the thermal zone devices registered at the same time.
879 * It also gives the opportunity to link the cooling device to a device tree
880 * node, so that it can be bound to a thermal zone created out of device tree.
882 * Return: a pointer to the created struct thermal_cooling_device or an
883 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
885 static struct thermal_cooling_device *
886 __thermal_cooling_device_register(struct device_node *np,
887 const char *type, void *devdata,
888 const struct thermal_cooling_device_ops *ops)
890 struct thermal_cooling_device *cdev;
891 struct thermal_zone_device *pos = NULL;
894 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
896 return ERR_PTR(-EINVAL);
898 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
900 return ERR_PTR(-ENOMEM);
902 ret = ida_simple_get(&thermal_cdev_ida, 0, 0, GFP_KERNEL);
908 ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
912 cdev->type = kstrdup(type ? type : "", GFP_KERNEL);
918 mutex_init(&cdev->lock);
919 INIT_LIST_HEAD(&cdev->thermal_instances);
922 cdev->updated = false;
923 cdev->device.class = &thermal_class;
924 cdev->devdata = devdata;
925 thermal_cooling_device_setup_sysfs(cdev);
926 ret = device_register(&cdev->device);
930 /* Add 'this' new cdev to the global cdev list */
931 mutex_lock(&thermal_list_lock);
932 list_add(&cdev->node, &thermal_cdev_list);
933 mutex_unlock(&thermal_list_lock);
935 /* Update binding information for 'this' new cdev */
938 mutex_lock(&thermal_list_lock);
939 list_for_each_entry(pos, &thermal_tz_list, node)
940 if (atomic_cmpxchg(&pos->need_update, 1, 0))
941 thermal_zone_device_update(pos,
942 THERMAL_EVENT_UNSPECIFIED);
943 mutex_unlock(&thermal_list_lock);
949 put_device(&cdev->device);
952 ida_simple_remove(&thermal_cdev_ida, id);
959 * thermal_cooling_device_register() - register a new thermal cooling device
960 * @type: the thermal cooling device type.
961 * @devdata: device private data.
962 * @ops: standard thermal cooling devices callbacks.
964 * This interface function adds a new thermal cooling device (fan/processor/...)
965 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
966 * to all the thermal zone devices registered at the same time.
968 * Return: a pointer to the created struct thermal_cooling_device or an
969 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
971 struct thermal_cooling_device *
972 thermal_cooling_device_register(const char *type, void *devdata,
973 const struct thermal_cooling_device_ops *ops)
975 return __thermal_cooling_device_register(NULL, type, devdata, ops);
977 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
980 * thermal_of_cooling_device_register() - register an OF thermal cooling device
981 * @np: a pointer to a device tree node.
982 * @type: the thermal cooling device type.
983 * @devdata: device private data.
984 * @ops: standard thermal cooling devices callbacks.
986 * This function will register a cooling device with device tree node reference.
987 * This interface function adds a new thermal cooling device (fan/processor/...)
988 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
989 * to all the thermal zone devices registered at the same time.
991 * Return: a pointer to the created struct thermal_cooling_device or an
992 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
994 struct thermal_cooling_device *
995 thermal_of_cooling_device_register(struct device_node *np,
996 const char *type, void *devdata,
997 const struct thermal_cooling_device_ops *ops)
999 return __thermal_cooling_device_register(np, type, devdata, ops);
1001 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1003 static void thermal_cooling_device_release(struct device *dev, void *res)
1005 thermal_cooling_device_unregister(
1006 *(struct thermal_cooling_device **)res);
1010 * devm_thermal_of_cooling_device_register() - register an OF thermal cooling
1012 * @dev: a valid struct device pointer of a sensor device.
1013 * @np: a pointer to a device tree node.
1014 * @type: the thermal cooling device type.
1015 * @devdata: device private data.
1016 * @ops: standard thermal cooling devices callbacks.
1018 * This function will register a cooling device with device tree node reference.
1019 * This interface function adds a new thermal cooling device (fan/processor/...)
1020 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1021 * to all the thermal zone devices registered at the same time.
1023 * Return: a pointer to the created struct thermal_cooling_device or an
1024 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1026 struct thermal_cooling_device *
1027 devm_thermal_of_cooling_device_register(struct device *dev,
1028 struct device_node *np,
1029 char *type, void *devdata,
1030 const struct thermal_cooling_device_ops *ops)
1032 struct thermal_cooling_device **ptr, *tcd;
1034 ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr),
1037 return ERR_PTR(-ENOMEM);
1039 tcd = __thermal_cooling_device_register(np, type, devdata, ops);
1046 devres_add(dev, ptr);
1050 EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register);
1052 static void __unbind(struct thermal_zone_device *tz, int mask,
1053 struct thermal_cooling_device *cdev)
1057 for (i = 0; i < tz->trips; i++)
1058 if (mask & (1 << i))
1059 thermal_zone_unbind_cooling_device(tz, i, cdev);
1063 * thermal_cooling_device_unregister - removes a thermal cooling device
1064 * @cdev: the thermal cooling device to remove.
1066 * thermal_cooling_device_unregister() must be called when a registered
1067 * thermal cooling device is no longer needed.
1069 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1072 const struct thermal_zone_params *tzp;
1073 struct thermal_zone_device *tz;
1074 struct thermal_cooling_device *pos = NULL;
1079 mutex_lock(&thermal_list_lock);
1080 list_for_each_entry(pos, &thermal_cdev_list, node)
1084 /* thermal cooling device not found */
1085 mutex_unlock(&thermal_list_lock);
1088 list_del(&cdev->node);
1090 /* Unbind all thermal zones associated with 'this' cdev */
1091 list_for_each_entry(tz, &thermal_tz_list, node) {
1092 if (tz->ops->unbind) {
1093 tz->ops->unbind(tz, cdev);
1097 if (!tz->tzp || !tz->tzp->tbp)
1101 for (i = 0; i < tzp->num_tbps; i++) {
1102 if (tzp->tbp[i].cdev == cdev) {
1103 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1104 tzp->tbp[i].cdev = NULL;
1109 mutex_unlock(&thermal_list_lock);
1111 ida_simple_remove(&thermal_cdev_ida, cdev->id);
1112 device_del(&cdev->device);
1113 thermal_cooling_device_destroy_sysfs(cdev);
1115 put_device(&cdev->device);
1117 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1119 static void bind_tz(struct thermal_zone_device *tz)
1122 struct thermal_cooling_device *pos = NULL;
1123 const struct thermal_zone_params *tzp = tz->tzp;
1125 if (!tzp && !tz->ops->bind)
1128 mutex_lock(&thermal_list_lock);
1130 /* If there is ops->bind, try to use ops->bind */
1131 if (tz->ops->bind) {
1132 list_for_each_entry(pos, &thermal_cdev_list, node) {
1133 ret = tz->ops->bind(tz, pos);
1135 print_bind_err_msg(tz, pos, ret);
1140 if (!tzp || !tzp->tbp)
1143 list_for_each_entry(pos, &thermal_cdev_list, node) {
1144 for (i = 0; i < tzp->num_tbps; i++) {
1145 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
1147 if (tzp->tbp[i].match(tz, pos))
1149 tzp->tbp[i].cdev = pos;
1150 __bind(tz, tzp->tbp[i].trip_mask, pos,
1151 tzp->tbp[i].binding_limits,
1152 tzp->tbp[i].weight);
1156 mutex_unlock(&thermal_list_lock);
1160 * thermal_zone_device_register() - register a new thermal zone device
1161 * @type: the thermal zone device type
1162 * @trips: the number of trip points the thermal zone support
1163 * @mask: a bit string indicating the writeablility of trip points
1164 * @devdata: private device data
1165 * @ops: standard thermal zone device callbacks
1166 * @tzp: thermal zone platform parameters
1167 * @passive_delay: number of milliseconds to wait between polls when
1168 * performing passive cooling
1169 * @polling_delay: number of milliseconds to wait between polls when checking
1170 * whether trip points have been crossed (0 for interrupt
1173 * This interface function adds a new thermal zone device (sensor) to
1174 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1175 * thermal cooling devices registered at the same time.
1176 * thermal_zone_device_unregister() must be called when the device is no
1177 * longer needed. The passive cooling depends on the .get_trend() return value.
1179 * Return: a pointer to the created struct thermal_zone_device or an
1180 * in case of error, an ERR_PTR. Caller must check return value with
1181 * IS_ERR*() helpers.
1183 struct thermal_zone_device *
1184 thermal_zone_device_register(const char *type, int trips, int mask,
1185 void *devdata, struct thermal_zone_device_ops *ops,
1186 struct thermal_zone_params *tzp, int passive_delay,
1189 struct thermal_zone_device *tz;
1190 enum thermal_trip_type trip_type;
1195 struct thermal_governor *governor;
1197 if (!type || strlen(type) == 0) {
1198 pr_err("Error: No thermal zone type defined\n");
1199 return ERR_PTR(-EINVAL);
1202 if (type && strlen(type) >= THERMAL_NAME_LENGTH) {
1203 pr_err("Error: Thermal zone name (%s) too long, should be under %d chars\n",
1204 type, THERMAL_NAME_LENGTH);
1205 return ERR_PTR(-EINVAL);
1208 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips) {
1209 pr_err("Error: Incorrect number of thermal trips\n");
1210 return ERR_PTR(-EINVAL);
1214 pr_err("Error: Thermal zone device ops not defined\n");
1215 return ERR_PTR(-EINVAL);
1218 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
1219 return ERR_PTR(-EINVAL);
1221 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
1223 return ERR_PTR(-ENOMEM);
1225 INIT_LIST_HEAD(&tz->thermal_instances);
1227 mutex_init(&tz->lock);
1228 id = ida_simple_get(&thermal_tz_ida, 0, 0, GFP_KERNEL);
1235 strlcpy(tz->type, type, sizeof(tz->type));
1237 result = dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1242 ops->critical = thermal_zone_device_critical;
1246 tz->device.class = &thermal_class;
1247 tz->devdata = devdata;
1250 thermal_set_delay_jiffies(&tz->passive_delay_jiffies, passive_delay);
1251 thermal_set_delay_jiffies(&tz->polling_delay_jiffies, polling_delay);
1254 /* Add nodes that are always present via .groups */
1255 result = thermal_zone_create_device_groups(tz, mask);
1259 /* A new thermal zone needs to be updated anyway. */
1260 atomic_set(&tz->need_update, 1);
1262 result = device_register(&tz->device);
1264 goto release_device;
1266 for (count = 0; count < trips; count++) {
1267 if (tz->ops->get_trip_type(tz, count, &trip_type) ||
1268 tz->ops->get_trip_temp(tz, count, &trip_temp) ||
1270 set_bit(count, &tz->trips_disabled);
1273 /* Update 'this' zone's governor information */
1274 mutex_lock(&thermal_governor_lock);
1277 governor = __find_governor(tz->tzp->governor_name);
1279 governor = def_governor;
1281 result = thermal_set_governor(tz, governor);
1283 mutex_unlock(&thermal_governor_lock);
1287 mutex_unlock(&thermal_governor_lock);
1289 if (!tz->tzp || !tz->tzp->no_hwmon) {
1290 result = thermal_add_hwmon_sysfs(tz);
1295 mutex_lock(&thermal_list_lock);
1296 list_add_tail(&tz->node, &thermal_tz_list);
1297 mutex_unlock(&thermal_list_lock);
1299 /* Bind cooling devices for this zone */
1302 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
1304 thermal_zone_device_init(tz);
1305 /* Update the new thermal zone and mark it as already updated. */
1306 if (atomic_cmpxchg(&tz->need_update, 1, 0))
1307 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1309 thermal_notify_tz_create(tz->id, tz->type);
1314 device_del(&tz->device);
1316 put_device(&tz->device);
1319 ida_simple_remove(&thermal_tz_ida, id);
1322 return ERR_PTR(result);
1324 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
1327 * thermal_zone_device_unregister - removes the registered thermal zone device
1328 * @tz: the thermal zone device to remove
1330 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1333 const struct thermal_zone_params *tzp;
1334 struct thermal_cooling_device *cdev;
1335 struct thermal_zone_device *pos = NULL;
1343 mutex_lock(&thermal_list_lock);
1344 list_for_each_entry(pos, &thermal_tz_list, node)
1348 /* thermal zone device not found */
1349 mutex_unlock(&thermal_list_lock);
1352 list_del(&tz->node);
1354 /* Unbind all cdevs associated with 'this' thermal zone */
1355 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1356 if (tz->ops->unbind) {
1357 tz->ops->unbind(tz, cdev);
1361 if (!tzp || !tzp->tbp)
1364 for (i = 0; i < tzp->num_tbps; i++) {
1365 if (tzp->tbp[i].cdev == cdev) {
1366 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1367 tzp->tbp[i].cdev = NULL;
1372 mutex_unlock(&thermal_list_lock);
1374 cancel_delayed_work_sync(&tz->poll_queue);
1376 thermal_set_governor(tz, NULL);
1378 thermal_remove_hwmon_sysfs(tz);
1379 ida_simple_remove(&thermal_tz_ida, tz->id);
1380 ida_destroy(&tz->ida);
1381 mutex_destroy(&tz->lock);
1382 device_unregister(&tz->device);
1384 thermal_notify_tz_delete(tz_id);
1386 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1389 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1390 * @name: thermal zone name to fetch the temperature
1392 * When only one zone is found with the passed name, returns a reference to it.
1394 * Return: On success returns a reference to an unique thermal zone with
1395 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1396 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1398 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1400 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1401 unsigned int found = 0;
1406 mutex_lock(&thermal_list_lock);
1407 list_for_each_entry(pos, &thermal_tz_list, node)
1408 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1412 mutex_unlock(&thermal_list_lock);
1414 /* nothing has been found, thus an error code for it */
1416 ref = ERR_PTR(-ENODEV);
1418 /* Success only when an unique zone is found */
1419 ref = ERR_PTR(-EEXIST);
1424 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1426 static int thermal_pm_notify(struct notifier_block *nb,
1427 unsigned long mode, void *_unused)
1429 struct thermal_zone_device *tz;
1432 case PM_HIBERNATION_PREPARE:
1433 case PM_RESTORE_PREPARE:
1434 case PM_SUSPEND_PREPARE:
1435 atomic_set(&in_suspend, 1);
1437 case PM_POST_HIBERNATION:
1438 case PM_POST_RESTORE:
1439 case PM_POST_SUSPEND:
1440 atomic_set(&in_suspend, 0);
1441 list_for_each_entry(tz, &thermal_tz_list, node) {
1442 if (!thermal_zone_device_is_enabled(tz))
1445 thermal_zone_device_init(tz);
1446 thermal_zone_device_update(tz,
1447 THERMAL_EVENT_UNSPECIFIED);
1456 static struct notifier_block thermal_pm_nb = {
1457 .notifier_call = thermal_pm_notify,
1460 static int __init thermal_init(void)
1464 result = thermal_netlink_init();
1468 result = thermal_register_governors();
1472 result = class_register(&thermal_class);
1474 goto unregister_governors;
1476 result = of_parse_thermal_zones();
1478 goto unregister_class;
1480 result = register_pm_notifier(&thermal_pm_nb);
1482 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1488 class_unregister(&thermal_class);
1489 unregister_governors:
1490 thermal_unregister_governors();
1492 ida_destroy(&thermal_tz_ida);
1493 ida_destroy(&thermal_cdev_ida);
1494 mutex_destroy(&thermal_list_lock);
1495 mutex_destroy(&thermal_governor_lock);
1498 postcore_initcall(thermal_init);