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 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);
379 if (tz->last_temperature >= trip_temp &&
380 tz->temperature < (trip_temp - hyst))
381 thermal_notify_tz_trip_down(tz->id, trip);
384 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
385 handle_critical_trips(tz, trip, type);
387 handle_non_critical_trips(tz, trip);
389 * Alright, we handled this trip successfully.
390 * So, start monitoring again.
392 monitor_thermal_zone(tz);
395 static void update_temperature(struct thermal_zone_device *tz)
399 ret = thermal_zone_get_temp(tz, &temp);
402 dev_warn(&tz->device,
403 "failed to read out thermal zone (%d)\n",
408 mutex_lock(&tz->lock);
409 tz->last_temperature = tz->temperature;
410 tz->temperature = temp;
411 mutex_unlock(&tz->lock);
413 trace_thermal_temperature(tz);
415 thermal_genl_sampling_temp(tz->id, temp);
418 static void thermal_zone_device_init(struct thermal_zone_device *tz)
420 struct thermal_instance *pos;
421 tz->temperature = THERMAL_TEMP_INVALID;
422 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
423 pos->initialized = false;
426 static int thermal_zone_device_set_mode(struct thermal_zone_device *tz,
427 enum thermal_device_mode mode)
431 mutex_lock(&tz->lock);
433 /* do nothing if mode isn't changing */
434 if (mode == tz->mode) {
435 mutex_unlock(&tz->lock);
440 if (tz->ops->change_mode)
441 ret = tz->ops->change_mode(tz, mode);
446 mutex_unlock(&tz->lock);
448 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
450 if (mode == THERMAL_DEVICE_ENABLED)
451 thermal_notify_tz_enable(tz->id);
453 thermal_notify_tz_disable(tz->id);
458 int thermal_zone_device_enable(struct thermal_zone_device *tz)
460 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_ENABLED);
462 EXPORT_SYMBOL_GPL(thermal_zone_device_enable);
464 int thermal_zone_device_disable(struct thermal_zone_device *tz)
466 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED);
468 EXPORT_SYMBOL_GPL(thermal_zone_device_disable);
470 int thermal_zone_device_is_enabled(struct thermal_zone_device *tz)
472 enum thermal_device_mode mode;
474 mutex_lock(&tz->lock);
478 mutex_unlock(&tz->lock);
480 return mode == THERMAL_DEVICE_ENABLED;
483 void thermal_zone_device_update(struct thermal_zone_device *tz,
484 enum thermal_notify_event event)
488 if (should_stop_polling(tz))
491 if (atomic_read(&in_suspend))
494 if (WARN_ONCE(!tz->ops->get_temp, "'%s' must not be called without "
495 "'get_temp' ops set\n", __func__))
498 update_temperature(tz);
500 thermal_zone_set_trips(tz);
502 tz->notify_event = event;
504 for (count = 0; count < tz->trips; count++)
505 handle_thermal_trip(tz, count);
507 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
509 static void thermal_zone_device_check(struct work_struct *work)
511 struct thermal_zone_device *tz = container_of(work, struct
514 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
517 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
520 struct thermal_governor *gov;
523 mutex_lock(&thermal_governor_lock);
524 list_for_each_entry(gov, &thermal_governor_list, governor_list) {
529 mutex_unlock(&thermal_governor_lock);
534 int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
537 struct thermal_cooling_device *cdev;
540 mutex_lock(&thermal_list_lock);
541 list_for_each_entry(cdev, &thermal_cdev_list, node) {
542 ret = cb(cdev, data);
546 mutex_unlock(&thermal_list_lock);
551 int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
554 struct thermal_zone_device *tz;
557 mutex_lock(&thermal_list_lock);
558 list_for_each_entry(tz, &thermal_tz_list, node) {
563 mutex_unlock(&thermal_list_lock);
568 struct thermal_zone_device *thermal_zone_get_by_id(int id)
570 struct thermal_zone_device *tz, *match = NULL;
572 mutex_lock(&thermal_list_lock);
573 list_for_each_entry(tz, &thermal_tz_list, node) {
579 mutex_unlock(&thermal_list_lock);
585 * Device management section: cooling devices, zones devices, and binding
587 * Set of functions provided by the thermal core for:
588 * - cooling devices lifecycle: registration, unregistration,
589 * binding, and unbinding.
590 * - thermal zone devices lifecycle: registration, unregistration,
591 * binding, and unbinding.
595 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
596 * @tz: pointer to struct thermal_zone_device
597 * @trip: indicates which trip point the cooling devices is
598 * associated with in this thermal zone.
599 * @cdev: pointer to struct thermal_cooling_device
600 * @upper: the Maximum cooling state for this trip point.
601 * THERMAL_NO_LIMIT means no upper limit,
602 * and the cooling device can be in max_state.
603 * @lower: the Minimum cooling state can be used for this trip point.
604 * THERMAL_NO_LIMIT means no lower limit,
605 * and the cooling device can be in cooling state 0.
606 * @weight: The weight of the cooling device to be bound to the
607 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
610 * This interface function bind a thermal cooling device to the certain trip
611 * point of a thermal zone device.
612 * This function is usually called in the thermal zone device .bind callback.
614 * Return: 0 on success, the proper error value otherwise.
616 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
618 struct thermal_cooling_device *cdev,
619 unsigned long upper, unsigned long lower,
622 struct thermal_instance *dev;
623 struct thermal_instance *pos;
624 struct thermal_zone_device *pos1;
625 struct thermal_cooling_device *pos2;
626 unsigned long max_state;
629 if (trip >= tz->trips || trip < 0)
632 list_for_each_entry(pos1, &thermal_tz_list, node) {
636 list_for_each_entry(pos2, &thermal_cdev_list, node) {
641 if (tz != pos1 || cdev != pos2)
644 ret = cdev->ops->get_max_state(cdev, &max_state);
648 /* lower default 0, upper default max_state */
649 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
650 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
652 if (lower > upper || upper > max_state)
655 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
663 dev->target = THERMAL_NO_TARGET;
664 dev->weight = weight;
666 result = ida_simple_get(&tz->ida, 0, 0, GFP_KERNEL);
671 sprintf(dev->name, "cdev%d", dev->id);
673 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
677 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
678 sysfs_attr_init(&dev->attr.attr);
679 dev->attr.attr.name = dev->attr_name;
680 dev->attr.attr.mode = 0444;
681 dev->attr.show = trip_point_show;
682 result = device_create_file(&tz->device, &dev->attr);
684 goto remove_symbol_link;
686 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
687 sysfs_attr_init(&dev->weight_attr.attr);
688 dev->weight_attr.attr.name = dev->weight_attr_name;
689 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
690 dev->weight_attr.show = weight_show;
691 dev->weight_attr.store = weight_store;
692 result = device_create_file(&tz->device, &dev->weight_attr);
694 goto remove_trip_file;
696 mutex_lock(&tz->lock);
697 mutex_lock(&cdev->lock);
698 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
699 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
704 list_add_tail(&dev->tz_node, &tz->thermal_instances);
705 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
706 atomic_set(&tz->need_update, 1);
708 mutex_unlock(&cdev->lock);
709 mutex_unlock(&tz->lock);
714 device_remove_file(&tz->device, &dev->weight_attr);
716 device_remove_file(&tz->device, &dev->attr);
718 sysfs_remove_link(&tz->device.kobj, dev->name);
720 ida_simple_remove(&tz->ida, dev->id);
725 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
728 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
730 * @tz: pointer to a struct thermal_zone_device.
731 * @trip: indicates which trip point the cooling devices is
732 * associated with in this thermal zone.
733 * @cdev: pointer to a struct thermal_cooling_device.
735 * This interface function unbind a thermal cooling device from the certain
736 * trip point of a thermal zone device.
737 * This function is usually called in the thermal zone device .unbind callback.
739 * Return: 0 on success, the proper error value otherwise.
741 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
743 struct thermal_cooling_device *cdev)
745 struct thermal_instance *pos, *next;
747 mutex_lock(&tz->lock);
748 mutex_lock(&cdev->lock);
749 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
750 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
751 list_del(&pos->tz_node);
752 list_del(&pos->cdev_node);
753 mutex_unlock(&cdev->lock);
754 mutex_unlock(&tz->lock);
758 mutex_unlock(&cdev->lock);
759 mutex_unlock(&tz->lock);
764 device_remove_file(&tz->device, &pos->weight_attr);
765 device_remove_file(&tz->device, &pos->attr);
766 sysfs_remove_link(&tz->device.kobj, pos->name);
767 ida_simple_remove(&tz->ida, pos->id);
771 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
773 static void thermal_release(struct device *dev)
775 struct thermal_zone_device *tz;
776 struct thermal_cooling_device *cdev;
778 if (!strncmp(dev_name(dev), "thermal_zone",
779 sizeof("thermal_zone") - 1)) {
780 tz = to_thermal_zone(dev);
781 thermal_zone_destroy_device_groups(tz);
783 } else if (!strncmp(dev_name(dev), "cooling_device",
784 sizeof("cooling_device") - 1)) {
785 cdev = to_cooling_device(dev);
790 static struct class thermal_class = {
792 .dev_release = thermal_release,
796 void print_bind_err_msg(struct thermal_zone_device *tz,
797 struct thermal_cooling_device *cdev, int ret)
799 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
800 tz->type, cdev->type, ret);
803 static void __bind(struct thermal_zone_device *tz, int mask,
804 struct thermal_cooling_device *cdev,
805 unsigned long *limits,
810 for (i = 0; i < tz->trips; i++) {
811 if (mask & (1 << i)) {
812 unsigned long upper, lower;
814 upper = THERMAL_NO_LIMIT;
815 lower = THERMAL_NO_LIMIT;
817 lower = limits[i * 2];
818 upper = limits[i * 2 + 1];
820 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
824 print_bind_err_msg(tz, cdev, ret);
829 static void bind_cdev(struct thermal_cooling_device *cdev)
832 const struct thermal_zone_params *tzp;
833 struct thermal_zone_device *pos = NULL;
835 mutex_lock(&thermal_list_lock);
837 list_for_each_entry(pos, &thermal_tz_list, node) {
838 if (!pos->tzp && !pos->ops->bind)
841 if (pos->ops->bind) {
842 ret = pos->ops->bind(pos, cdev);
844 print_bind_err_msg(pos, cdev, ret);
849 if (!tzp || !tzp->tbp)
852 for (i = 0; i < tzp->num_tbps; i++) {
853 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
855 if (tzp->tbp[i].match(pos, cdev))
857 tzp->tbp[i].cdev = cdev;
858 __bind(pos, tzp->tbp[i].trip_mask, cdev,
859 tzp->tbp[i].binding_limits,
864 mutex_unlock(&thermal_list_lock);
868 * __thermal_cooling_device_register() - register a new thermal cooling device
869 * @np: a pointer to a device tree node.
870 * @type: the thermal cooling device type.
871 * @devdata: device private data.
872 * @ops: standard thermal cooling devices callbacks.
874 * This interface function adds a new thermal cooling device (fan/processor/...)
875 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
876 * to all the thermal zone devices registered at the same time.
877 * It also gives the opportunity to link the cooling device to a device tree
878 * node, so that it can be bound to a thermal zone created out of device tree.
880 * Return: a pointer to the created struct thermal_cooling_device or an
881 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
883 static struct thermal_cooling_device *
884 __thermal_cooling_device_register(struct device_node *np,
885 const char *type, void *devdata,
886 const struct thermal_cooling_device_ops *ops)
888 struct thermal_cooling_device *cdev;
889 struct thermal_zone_device *pos = NULL;
892 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
894 return ERR_PTR(-EINVAL);
896 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
898 return ERR_PTR(-ENOMEM);
900 ret = ida_simple_get(&thermal_cdev_ida, 0, 0, GFP_KERNEL);
906 ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
910 cdev->type = kstrdup(type ? type : "", GFP_KERNEL);
916 mutex_init(&cdev->lock);
917 INIT_LIST_HEAD(&cdev->thermal_instances);
920 cdev->updated = false;
921 cdev->device.class = &thermal_class;
922 cdev->devdata = devdata;
923 thermal_cooling_device_setup_sysfs(cdev);
924 ret = device_register(&cdev->device);
928 /* Add 'this' new cdev to the global cdev list */
929 mutex_lock(&thermal_list_lock);
930 list_add(&cdev->node, &thermal_cdev_list);
931 mutex_unlock(&thermal_list_lock);
933 /* Update binding information for 'this' new cdev */
936 mutex_lock(&thermal_list_lock);
937 list_for_each_entry(pos, &thermal_tz_list, node)
938 if (atomic_cmpxchg(&pos->need_update, 1, 0))
939 thermal_zone_device_update(pos,
940 THERMAL_EVENT_UNSPECIFIED);
941 mutex_unlock(&thermal_list_lock);
947 put_device(&cdev->device);
950 ida_simple_remove(&thermal_cdev_ida, id);
957 * thermal_cooling_device_register() - register a new thermal cooling device
958 * @type: the thermal cooling device type.
959 * @devdata: device private data.
960 * @ops: standard thermal cooling devices callbacks.
962 * This interface function adds a new thermal cooling device (fan/processor/...)
963 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
964 * to all the thermal zone devices registered at the same time.
966 * Return: a pointer to the created struct thermal_cooling_device or an
967 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
969 struct thermal_cooling_device *
970 thermal_cooling_device_register(const char *type, void *devdata,
971 const struct thermal_cooling_device_ops *ops)
973 return __thermal_cooling_device_register(NULL, type, devdata, ops);
975 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
978 * thermal_of_cooling_device_register() - register an OF thermal cooling device
979 * @np: a pointer to a device tree node.
980 * @type: the thermal cooling device type.
981 * @devdata: device private data.
982 * @ops: standard thermal cooling devices callbacks.
984 * This function will register a cooling device with device tree node reference.
985 * This interface function adds a new thermal cooling device (fan/processor/...)
986 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
987 * to all the thermal zone devices registered at the same time.
989 * Return: a pointer to the created struct thermal_cooling_device or an
990 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
992 struct thermal_cooling_device *
993 thermal_of_cooling_device_register(struct device_node *np,
994 const char *type, void *devdata,
995 const struct thermal_cooling_device_ops *ops)
997 return __thermal_cooling_device_register(np, type, devdata, ops);
999 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1001 static void thermal_cooling_device_release(struct device *dev, void *res)
1003 thermal_cooling_device_unregister(
1004 *(struct thermal_cooling_device **)res);
1008 * devm_thermal_of_cooling_device_register() - register an OF thermal cooling
1010 * @dev: a valid struct device pointer of a sensor device.
1011 * @np: a pointer to a device tree node.
1012 * @type: the thermal cooling device type.
1013 * @devdata: device private data.
1014 * @ops: standard thermal cooling devices callbacks.
1016 * This function will register a cooling device with device tree node reference.
1017 * This interface function adds a new thermal cooling device (fan/processor/...)
1018 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1019 * to all the thermal zone devices registered at the same time.
1021 * Return: a pointer to the created struct thermal_cooling_device or an
1022 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1024 struct thermal_cooling_device *
1025 devm_thermal_of_cooling_device_register(struct device *dev,
1026 struct device_node *np,
1027 char *type, void *devdata,
1028 const struct thermal_cooling_device_ops *ops)
1030 struct thermal_cooling_device **ptr, *tcd;
1032 ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr),
1035 return ERR_PTR(-ENOMEM);
1037 tcd = __thermal_cooling_device_register(np, type, devdata, ops);
1044 devres_add(dev, ptr);
1048 EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register);
1050 static void __unbind(struct thermal_zone_device *tz, int mask,
1051 struct thermal_cooling_device *cdev)
1055 for (i = 0; i < tz->trips; i++)
1056 if (mask & (1 << i))
1057 thermal_zone_unbind_cooling_device(tz, i, cdev);
1061 * thermal_cooling_device_unregister - removes a thermal cooling device
1062 * @cdev: the thermal cooling device to remove.
1064 * thermal_cooling_device_unregister() must be called when a registered
1065 * thermal cooling device is no longer needed.
1067 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1070 const struct thermal_zone_params *tzp;
1071 struct thermal_zone_device *tz;
1072 struct thermal_cooling_device *pos = NULL;
1077 mutex_lock(&thermal_list_lock);
1078 list_for_each_entry(pos, &thermal_cdev_list, node)
1082 /* thermal cooling device not found */
1083 mutex_unlock(&thermal_list_lock);
1086 list_del(&cdev->node);
1088 /* Unbind all thermal zones associated with 'this' cdev */
1089 list_for_each_entry(tz, &thermal_tz_list, node) {
1090 if (tz->ops->unbind) {
1091 tz->ops->unbind(tz, cdev);
1095 if (!tz->tzp || !tz->tzp->tbp)
1099 for (i = 0; i < tzp->num_tbps; i++) {
1100 if (tzp->tbp[i].cdev == cdev) {
1101 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1102 tzp->tbp[i].cdev = NULL;
1107 mutex_unlock(&thermal_list_lock);
1109 ida_simple_remove(&thermal_cdev_ida, cdev->id);
1110 device_del(&cdev->device);
1111 thermal_cooling_device_destroy_sysfs(cdev);
1113 put_device(&cdev->device);
1115 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1117 static void bind_tz(struct thermal_zone_device *tz)
1120 struct thermal_cooling_device *pos = NULL;
1121 const struct thermal_zone_params *tzp = tz->tzp;
1123 if (!tzp && !tz->ops->bind)
1126 mutex_lock(&thermal_list_lock);
1128 /* If there is ops->bind, try to use ops->bind */
1129 if (tz->ops->bind) {
1130 list_for_each_entry(pos, &thermal_cdev_list, node) {
1131 ret = tz->ops->bind(tz, pos);
1133 print_bind_err_msg(tz, pos, ret);
1138 if (!tzp || !tzp->tbp)
1141 list_for_each_entry(pos, &thermal_cdev_list, node) {
1142 for (i = 0; i < tzp->num_tbps; i++) {
1143 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
1145 if (tzp->tbp[i].match(tz, pos))
1147 tzp->tbp[i].cdev = pos;
1148 __bind(tz, tzp->tbp[i].trip_mask, pos,
1149 tzp->tbp[i].binding_limits,
1150 tzp->tbp[i].weight);
1154 mutex_unlock(&thermal_list_lock);
1158 * thermal_zone_device_register() - register a new thermal zone device
1159 * @type: the thermal zone device type
1160 * @trips: the number of trip points the thermal zone support
1161 * @mask: a bit string indicating the writeablility of trip points
1162 * @devdata: private device data
1163 * @ops: standard thermal zone device callbacks
1164 * @tzp: thermal zone platform parameters
1165 * @passive_delay: number of milliseconds to wait between polls when
1166 * performing passive cooling
1167 * @polling_delay: number of milliseconds to wait between polls when checking
1168 * whether trip points have been crossed (0 for interrupt
1171 * This interface function adds a new thermal zone device (sensor) to
1172 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1173 * thermal cooling devices registered at the same time.
1174 * thermal_zone_device_unregister() must be called when the device is no
1175 * longer needed. The passive cooling depends on the .get_trend() return value.
1177 * Return: a pointer to the created struct thermal_zone_device or an
1178 * in case of error, an ERR_PTR. Caller must check return value with
1179 * IS_ERR*() helpers.
1181 struct thermal_zone_device *
1182 thermal_zone_device_register(const char *type, int trips, int mask,
1183 void *devdata, struct thermal_zone_device_ops *ops,
1184 struct thermal_zone_params *tzp, int passive_delay,
1187 struct thermal_zone_device *tz;
1188 enum thermal_trip_type trip_type;
1193 struct thermal_governor *governor;
1195 if (!type || strlen(type) == 0) {
1196 pr_err("Error: No thermal zone type defined\n");
1197 return ERR_PTR(-EINVAL);
1200 if (type && strlen(type) >= THERMAL_NAME_LENGTH) {
1201 pr_err("Error: Thermal zone name (%s) too long, should be under %d chars\n",
1202 type, THERMAL_NAME_LENGTH);
1203 return ERR_PTR(-EINVAL);
1206 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips) {
1207 pr_err("Error: Incorrect number of thermal trips\n");
1208 return ERR_PTR(-EINVAL);
1212 pr_err("Error: Thermal zone device ops not defined\n");
1213 return ERR_PTR(-EINVAL);
1216 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
1217 return ERR_PTR(-EINVAL);
1219 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
1221 return ERR_PTR(-ENOMEM);
1223 INIT_LIST_HEAD(&tz->thermal_instances);
1225 mutex_init(&tz->lock);
1226 id = ida_simple_get(&thermal_tz_ida, 0, 0, GFP_KERNEL);
1233 strlcpy(tz->type, type, sizeof(tz->type));
1235 result = dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1240 ops->critical = thermal_zone_device_critical;
1244 tz->device.class = &thermal_class;
1245 tz->devdata = devdata;
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 = device_register(&tz->device);
1262 goto release_device;
1264 for (count = 0; count < trips; count++) {
1265 if (tz->ops->get_trip_type(tz, count, &trip_type) ||
1266 tz->ops->get_trip_temp(tz, count, &trip_temp) ||
1268 set_bit(count, &tz->trips_disabled);
1271 /* Update 'this' zone's governor information */
1272 mutex_lock(&thermal_governor_lock);
1275 governor = __find_governor(tz->tzp->governor_name);
1277 governor = def_governor;
1279 result = thermal_set_governor(tz, governor);
1281 mutex_unlock(&thermal_governor_lock);
1285 mutex_unlock(&thermal_governor_lock);
1287 if (!tz->tzp || !tz->tzp->no_hwmon) {
1288 result = thermal_add_hwmon_sysfs(tz);
1293 mutex_lock(&thermal_list_lock);
1294 list_add_tail(&tz->node, &thermal_tz_list);
1295 mutex_unlock(&thermal_list_lock);
1297 /* Bind cooling devices for this zone */
1300 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
1302 thermal_zone_device_init(tz);
1303 /* Update the new thermal zone and mark it as already updated. */
1304 if (atomic_cmpxchg(&tz->need_update, 1, 0))
1305 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1307 thermal_notify_tz_create(tz->id, tz->type);
1312 device_del(&tz->device);
1314 put_device(&tz->device);
1317 ida_simple_remove(&thermal_tz_ida, id);
1320 return ERR_PTR(result);
1322 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
1325 * thermal_zone_device_unregister - removes the registered thermal zone device
1326 * @tz: the thermal zone device to remove
1328 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1331 const struct thermal_zone_params *tzp;
1332 struct thermal_cooling_device *cdev;
1333 struct thermal_zone_device *pos = NULL;
1341 mutex_lock(&thermal_list_lock);
1342 list_for_each_entry(pos, &thermal_tz_list, node)
1346 /* thermal zone device not found */
1347 mutex_unlock(&thermal_list_lock);
1350 list_del(&tz->node);
1352 /* Unbind all cdevs associated with 'this' thermal zone */
1353 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1354 if (tz->ops->unbind) {
1355 tz->ops->unbind(tz, cdev);
1359 if (!tzp || !tzp->tbp)
1362 for (i = 0; i < tzp->num_tbps; i++) {
1363 if (tzp->tbp[i].cdev == cdev) {
1364 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1365 tzp->tbp[i].cdev = NULL;
1370 mutex_unlock(&thermal_list_lock);
1372 cancel_delayed_work_sync(&tz->poll_queue);
1374 thermal_set_governor(tz, NULL);
1376 thermal_remove_hwmon_sysfs(tz);
1377 ida_simple_remove(&thermal_tz_ida, tz->id);
1378 ida_destroy(&tz->ida);
1379 mutex_destroy(&tz->lock);
1380 device_unregister(&tz->device);
1382 thermal_notify_tz_delete(tz_id);
1384 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1387 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1388 * @name: thermal zone name to fetch the temperature
1390 * When only one zone is found with the passed name, returns a reference to it.
1392 * Return: On success returns a reference to an unique thermal zone with
1393 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1394 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1396 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1398 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1399 unsigned int found = 0;
1404 mutex_lock(&thermal_list_lock);
1405 list_for_each_entry(pos, &thermal_tz_list, node)
1406 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1410 mutex_unlock(&thermal_list_lock);
1412 /* nothing has been found, thus an error code for it */
1414 ref = ERR_PTR(-ENODEV);
1416 /* Success only when an unique zone is found */
1417 ref = ERR_PTR(-EEXIST);
1422 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1424 static int thermal_pm_notify(struct notifier_block *nb,
1425 unsigned long mode, void *_unused)
1427 struct thermal_zone_device *tz;
1430 case PM_HIBERNATION_PREPARE:
1431 case PM_RESTORE_PREPARE:
1432 case PM_SUSPEND_PREPARE:
1433 atomic_set(&in_suspend, 1);
1435 case PM_POST_HIBERNATION:
1436 case PM_POST_RESTORE:
1437 case PM_POST_SUSPEND:
1438 atomic_set(&in_suspend, 0);
1439 list_for_each_entry(tz, &thermal_tz_list, node) {
1440 if (!thermal_zone_device_is_enabled(tz))
1443 thermal_zone_device_init(tz);
1444 thermal_zone_device_update(tz,
1445 THERMAL_EVENT_UNSPECIFIED);
1454 static struct notifier_block thermal_pm_nb = {
1455 .notifier_call = thermal_pm_notify,
1458 static int __init thermal_init(void)
1462 result = thermal_netlink_init();
1466 result = thermal_register_governors();
1470 result = class_register(&thermal_class);
1472 goto unregister_governors;
1474 result = of_parse_thermal_zones();
1476 goto unregister_class;
1478 result = register_pm_notifier(&thermal_pm_nb);
1480 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1486 class_unregister(&thermal_class);
1487 unregister_governors:
1488 thermal_unregister_governors();
1490 ida_destroy(&thermal_tz_ida);
1491 ida_destroy(&thermal_cdev_ida);
1492 mutex_destroy(&thermal_list_lock);
1493 mutex_destroy(&thermal_governor_lock);
1496 postcore_initcall(thermal_init);