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 += sysfs_emit_at(buf, count, "%s ", pos->name);
234 count += sysfs_emit_at(buf, count, "\n");
236 mutex_unlock(&thermal_governor_lock);
241 static void __init thermal_unregister_governors(void)
243 struct thermal_governor **governor;
245 for_each_governor_table(governor)
246 thermal_unregister_governor(*governor);
249 static int __init thermal_register_governors(void)
252 struct thermal_governor **governor;
254 for_each_governor_table(governor) {
255 ret = thermal_register_governor(*governor);
257 pr_err("Failed to register governor: '%s'",
262 pr_info("Registered thermal governor '%s'",
267 struct thermal_governor **gov;
269 for_each_governor_table(gov) {
272 thermal_unregister_governor(*gov);
280 * Zone update section: main control loop applied to each zone while monitoring
282 * in polling mode. The monitoring is done using a workqueue.
283 * Same update may be done on a zone by calling thermal_zone_device_update().
286 * - Non-critical trips will invoke the governor responsible for that zone;
287 * - Hot trips will produce a notification to userspace;
288 * - Critical trip point will cause a system shutdown.
290 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
294 mod_delayed_work(system_freezable_power_efficient_wq,
295 &tz->poll_queue, delay);
297 cancel_delayed_work(&tz->poll_queue);
300 static void monitor_thermal_zone(struct thermal_zone_device *tz)
302 if (tz->mode != THERMAL_DEVICE_ENABLED)
303 thermal_zone_device_set_polling(tz, 0);
304 else if (tz->passive)
305 thermal_zone_device_set_polling(tz, tz->passive_delay_jiffies);
306 else if (tz->polling_delay_jiffies)
307 thermal_zone_device_set_polling(tz, tz->polling_delay_jiffies);
310 static void handle_non_critical_trips(struct thermal_zone_device *tz, int trip)
312 tz->governor ? tz->governor->throttle(tz, trip) :
313 def_governor->throttle(tz, trip);
316 void thermal_zone_device_critical(struct thermal_zone_device *tz)
319 * poweroff_delay_ms must be a carefully profiled positive value.
320 * Its a must for forced_emergency_poweroff_work to be scheduled.
322 int poweroff_delay_ms = CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS;
324 dev_emerg(&tz->device, "%s: critical temperature reached, "
325 "shutting down\n", tz->type);
327 hw_protection_shutdown("Temperature too high", poweroff_delay_ms);
329 EXPORT_SYMBOL(thermal_zone_device_critical);
331 static void handle_critical_trips(struct thermal_zone_device *tz,
332 int trip, int trip_temp, enum thermal_trip_type trip_type)
334 /* If we have not crossed the trip_temp, we do not care. */
335 if (trip_temp <= 0 || tz->temperature < trip_temp)
338 trace_thermal_zone_trip(tz, trip, trip_type);
340 if (trip_type == THERMAL_TRIP_HOT && tz->ops->hot)
342 else if (trip_type == THERMAL_TRIP_CRITICAL)
343 tz->ops->critical(tz);
346 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip_id)
348 struct thermal_trip trip;
350 /* Ignore disabled trip points */
351 if (test_bit(trip_id, &tz->trips_disabled))
354 __thermal_zone_get_trip(tz, trip_id, &trip);
356 if (tz->last_temperature != THERMAL_TEMP_INVALID) {
357 if (tz->last_temperature < trip.temperature &&
358 tz->temperature >= trip.temperature)
359 thermal_notify_tz_trip_up(tz->id, trip_id,
361 if (tz->last_temperature >= trip.temperature &&
362 tz->temperature < (trip.temperature - trip.hysteresis))
363 thermal_notify_tz_trip_down(tz->id, trip_id,
367 if (trip.type == THERMAL_TRIP_CRITICAL || trip.type == THERMAL_TRIP_HOT)
368 handle_critical_trips(tz, trip_id, trip.temperature, trip.type);
370 handle_non_critical_trips(tz, trip_id);
373 static void update_temperature(struct thermal_zone_device *tz)
377 ret = __thermal_zone_get_temp(tz, &temp);
380 dev_warn(&tz->device,
381 "failed to read out thermal zone (%d)\n",
386 tz->last_temperature = tz->temperature;
387 tz->temperature = temp;
389 trace_thermal_temperature(tz);
391 thermal_genl_sampling_temp(tz->id, temp);
394 static void thermal_zone_device_init(struct thermal_zone_device *tz)
396 struct thermal_instance *pos;
397 tz->temperature = THERMAL_TEMP_INVALID;
398 tz->prev_low_trip = -INT_MAX;
399 tz->prev_high_trip = INT_MAX;
400 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
401 pos->initialized = false;
404 void __thermal_zone_device_update(struct thermal_zone_device *tz,
405 enum thermal_notify_event event)
409 if (atomic_read(&in_suspend))
412 if (WARN_ONCE(!tz->ops->get_temp,
413 "'%s' must not be called without 'get_temp' ops set\n",
417 if (!thermal_zone_device_is_enabled(tz))
420 update_temperature(tz);
422 __thermal_zone_set_trips(tz);
424 tz->notify_event = event;
426 for (count = 0; count < tz->num_trips; count++)
427 handle_thermal_trip(tz, count);
429 monitor_thermal_zone(tz);
432 static int thermal_zone_device_set_mode(struct thermal_zone_device *tz,
433 enum thermal_device_mode mode)
437 mutex_lock(&tz->lock);
439 /* do nothing if mode isn't changing */
440 if (mode == tz->mode) {
441 mutex_unlock(&tz->lock);
446 if (!device_is_registered(&tz->device)) {
447 mutex_unlock(&tz->lock);
452 if (tz->ops->change_mode)
453 ret = tz->ops->change_mode(tz, mode);
458 __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
460 mutex_unlock(&tz->lock);
462 if (mode == THERMAL_DEVICE_ENABLED)
463 thermal_notify_tz_enable(tz->id);
465 thermal_notify_tz_disable(tz->id);
470 int thermal_zone_device_enable(struct thermal_zone_device *tz)
472 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_ENABLED);
474 EXPORT_SYMBOL_GPL(thermal_zone_device_enable);
476 int thermal_zone_device_disable(struct thermal_zone_device *tz)
478 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED);
480 EXPORT_SYMBOL_GPL(thermal_zone_device_disable);
482 int thermal_zone_device_is_enabled(struct thermal_zone_device *tz)
484 lockdep_assert_held(&tz->lock);
486 return tz->mode == THERMAL_DEVICE_ENABLED;
489 void thermal_zone_device_update(struct thermal_zone_device *tz,
490 enum thermal_notify_event event)
492 mutex_lock(&tz->lock);
493 if (device_is_registered(&tz->device))
494 __thermal_zone_device_update(tz, event);
495 mutex_unlock(&tz->lock);
497 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
499 static void thermal_zone_device_check(struct work_struct *work)
501 struct thermal_zone_device *tz = container_of(work, struct
504 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
507 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
510 struct thermal_governor *gov;
513 mutex_lock(&thermal_governor_lock);
514 list_for_each_entry(gov, &thermal_governor_list, governor_list) {
519 mutex_unlock(&thermal_governor_lock);
524 int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
527 struct thermal_cooling_device *cdev;
530 mutex_lock(&thermal_list_lock);
531 list_for_each_entry(cdev, &thermal_cdev_list, node) {
532 ret = cb(cdev, data);
536 mutex_unlock(&thermal_list_lock);
541 int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
544 struct thermal_zone_device *tz;
547 mutex_lock(&thermal_list_lock);
548 list_for_each_entry(tz, &thermal_tz_list, node) {
553 mutex_unlock(&thermal_list_lock);
558 struct thermal_zone_device *thermal_zone_get_by_id(int id)
560 struct thermal_zone_device *tz, *match = NULL;
562 mutex_lock(&thermal_list_lock);
563 list_for_each_entry(tz, &thermal_tz_list, node) {
569 mutex_unlock(&thermal_list_lock);
575 * Device management section: cooling devices, zones devices, and binding
577 * Set of functions provided by the thermal core for:
578 * - cooling devices lifecycle: registration, unregistration,
579 * binding, and unbinding.
580 * - thermal zone devices lifecycle: registration, unregistration,
581 * binding, and unbinding.
585 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
586 * @tz: pointer to struct thermal_zone_device
587 * @trip: indicates which trip point the cooling devices is
588 * associated with in this thermal zone.
589 * @cdev: pointer to struct thermal_cooling_device
590 * @upper: the Maximum cooling state for this trip point.
591 * THERMAL_NO_LIMIT means no upper limit,
592 * and the cooling device can be in max_state.
593 * @lower: the Minimum cooling state can be used for this trip point.
594 * THERMAL_NO_LIMIT means no lower limit,
595 * and the cooling device can be in cooling state 0.
596 * @weight: The weight of the cooling device to be bound to the
597 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
600 * This interface function bind a thermal cooling device to the certain trip
601 * point of a thermal zone device.
602 * This function is usually called in the thermal zone device .bind callback.
604 * Return: 0 on success, the proper error value otherwise.
606 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
608 struct thermal_cooling_device *cdev,
609 unsigned long upper, unsigned long lower,
612 struct thermal_instance *dev;
613 struct thermal_instance *pos;
614 struct thermal_zone_device *pos1;
615 struct thermal_cooling_device *pos2;
619 if (trip >= tz->num_trips || trip < 0)
622 list_for_each_entry(pos1, &thermal_tz_list, node) {
626 list_for_each_entry(pos2, &thermal_cdev_list, node) {
631 if (tz != pos1 || cdev != pos2)
634 /* lower default 0, upper default max_state */
635 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
637 if (upper == THERMAL_NO_LIMIT) {
638 upper = cdev->max_state;
639 upper_no_limit = true;
641 upper_no_limit = false;
644 if (lower > upper || upper > cdev->max_state)
647 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
654 dev->upper_no_limit = upper_no_limit;
656 dev->target = THERMAL_NO_TARGET;
657 dev->weight = weight;
659 result = ida_alloc(&tz->ida, GFP_KERNEL);
664 sprintf(dev->name, "cdev%d", dev->id);
666 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
670 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
671 sysfs_attr_init(&dev->attr.attr);
672 dev->attr.attr.name = dev->attr_name;
673 dev->attr.attr.mode = 0444;
674 dev->attr.show = trip_point_show;
675 result = device_create_file(&tz->device, &dev->attr);
677 goto remove_symbol_link;
679 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
680 sysfs_attr_init(&dev->weight_attr.attr);
681 dev->weight_attr.attr.name = dev->weight_attr_name;
682 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
683 dev->weight_attr.show = weight_show;
684 dev->weight_attr.store = weight_store;
685 result = device_create_file(&tz->device, &dev->weight_attr);
687 goto remove_trip_file;
689 mutex_lock(&tz->lock);
690 mutex_lock(&cdev->lock);
691 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
692 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
697 list_add_tail(&dev->tz_node, &tz->thermal_instances);
698 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
699 atomic_set(&tz->need_update, 1);
701 mutex_unlock(&cdev->lock);
702 mutex_unlock(&tz->lock);
707 device_remove_file(&tz->device, &dev->weight_attr);
709 device_remove_file(&tz->device, &dev->attr);
711 sysfs_remove_link(&tz->device.kobj, dev->name);
713 ida_free(&tz->ida, dev->id);
718 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
721 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
723 * @tz: pointer to a struct thermal_zone_device.
724 * @trip: indicates which trip point the cooling devices is
725 * associated with in this thermal zone.
726 * @cdev: pointer to a struct thermal_cooling_device.
728 * This interface function unbind a thermal cooling device from the certain
729 * trip point of a thermal zone device.
730 * This function is usually called in the thermal zone device .unbind callback.
732 * Return: 0 on success, the proper error value otherwise.
734 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
736 struct thermal_cooling_device *cdev)
738 struct thermal_instance *pos, *next;
740 mutex_lock(&tz->lock);
741 mutex_lock(&cdev->lock);
742 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
743 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
744 list_del(&pos->tz_node);
745 list_del(&pos->cdev_node);
746 mutex_unlock(&cdev->lock);
747 mutex_unlock(&tz->lock);
751 mutex_unlock(&cdev->lock);
752 mutex_unlock(&tz->lock);
757 device_remove_file(&tz->device, &pos->weight_attr);
758 device_remove_file(&tz->device, &pos->attr);
759 sysfs_remove_link(&tz->device.kobj, pos->name);
760 ida_free(&tz->ida, pos->id);
764 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
766 static void thermal_release(struct device *dev)
768 struct thermal_zone_device *tz;
769 struct thermal_cooling_device *cdev;
771 if (!strncmp(dev_name(dev), "thermal_zone",
772 sizeof("thermal_zone") - 1)) {
773 tz = to_thermal_zone(dev);
774 thermal_zone_destroy_device_groups(tz);
775 mutex_destroy(&tz->lock);
777 } else if (!strncmp(dev_name(dev), "cooling_device",
778 sizeof("cooling_device") - 1)) {
779 cdev = to_cooling_device(dev);
780 thermal_cooling_device_destroy_sysfs(cdev);
782 ida_free(&thermal_cdev_ida, cdev->id);
787 static struct class *thermal_class;
790 void print_bind_err_msg(struct thermal_zone_device *tz,
791 struct thermal_cooling_device *cdev, int ret)
793 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
794 tz->type, cdev->type, ret);
797 static void __bind(struct thermal_zone_device *tz, int mask,
798 struct thermal_cooling_device *cdev,
799 unsigned long *limits,
804 for (i = 0; i < tz->num_trips; i++) {
805 if (mask & (1 << i)) {
806 unsigned long upper, lower;
808 upper = THERMAL_NO_LIMIT;
809 lower = THERMAL_NO_LIMIT;
811 lower = limits[i * 2];
812 upper = limits[i * 2 + 1];
814 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
818 print_bind_err_msg(tz, cdev, ret);
823 static void bind_cdev(struct thermal_cooling_device *cdev)
826 const struct thermal_zone_params *tzp;
827 struct thermal_zone_device *pos = NULL;
829 mutex_lock(&thermal_list_lock);
831 list_for_each_entry(pos, &thermal_tz_list, node) {
832 if (!pos->tzp && !pos->ops->bind)
835 if (pos->ops->bind) {
836 ret = pos->ops->bind(pos, cdev);
838 print_bind_err_msg(pos, cdev, ret);
843 if (!tzp || !tzp->tbp)
846 for (i = 0; i < tzp->num_tbps; i++) {
847 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
849 if (tzp->tbp[i].match(pos, cdev))
851 tzp->tbp[i].cdev = cdev;
852 __bind(pos, tzp->tbp[i].trip_mask, cdev,
853 tzp->tbp[i].binding_limits,
858 mutex_unlock(&thermal_list_lock);
862 * __thermal_cooling_device_register() - register a new thermal cooling device
863 * @np: a pointer to a device tree node.
864 * @type: the thermal cooling device type.
865 * @devdata: device private data.
866 * @ops: standard thermal cooling devices callbacks.
868 * This interface function adds a new thermal cooling device (fan/processor/...)
869 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
870 * to all the thermal zone devices registered at the same time.
871 * It also gives the opportunity to link the cooling device to a device tree
872 * node, so that it can be bound to a thermal zone created out of device tree.
874 * Return: a pointer to the created struct thermal_cooling_device or an
875 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
877 static struct thermal_cooling_device *
878 __thermal_cooling_device_register(struct device_node *np,
879 const char *type, void *devdata,
880 const struct thermal_cooling_device_ops *ops)
882 struct thermal_cooling_device *cdev;
883 struct thermal_zone_device *pos = NULL;
886 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
888 return ERR_PTR(-EINVAL);
891 return ERR_PTR(-ENODEV);
893 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
895 return ERR_PTR(-ENOMEM);
897 ret = ida_alloc(&thermal_cdev_ida, GFP_KERNEL);
903 cdev->type = kstrdup(type ? type : "", GFP_KERNEL);
909 mutex_init(&cdev->lock);
910 INIT_LIST_HEAD(&cdev->thermal_instances);
913 cdev->updated = false;
914 cdev->device.class = thermal_class;
915 cdev->devdata = devdata;
917 ret = cdev->ops->get_max_state(cdev, &cdev->max_state);
921 thermal_cooling_device_setup_sysfs(cdev);
923 ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
925 goto out_cooling_dev;
927 ret = device_register(&cdev->device);
929 /* thermal_release() handles rest of the cleanup */
930 put_device(&cdev->device);
934 /* Add 'this' new cdev to the global cdev list */
935 mutex_lock(&thermal_list_lock);
936 list_add(&cdev->node, &thermal_cdev_list);
937 mutex_unlock(&thermal_list_lock);
939 /* Update binding information for 'this' new cdev */
942 mutex_lock(&thermal_list_lock);
943 list_for_each_entry(pos, &thermal_tz_list, node)
944 if (atomic_cmpxchg(&pos->need_update, 1, 0))
945 thermal_zone_device_update(pos,
946 THERMAL_EVENT_UNSPECIFIED);
947 mutex_unlock(&thermal_list_lock);
952 thermal_cooling_device_destroy_sysfs(cdev);
956 ida_free(&thermal_cdev_ida, id);
963 * thermal_cooling_device_register() - register a new thermal cooling device
964 * @type: the thermal cooling device type.
965 * @devdata: device private data.
966 * @ops: standard thermal cooling devices callbacks.
968 * This interface function adds a new thermal cooling device (fan/processor/...)
969 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
970 * to all the thermal zone devices registered at the same time.
972 * Return: a pointer to the created struct thermal_cooling_device or an
973 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
975 struct thermal_cooling_device *
976 thermal_cooling_device_register(const char *type, void *devdata,
977 const struct thermal_cooling_device_ops *ops)
979 return __thermal_cooling_device_register(NULL, type, devdata, ops);
981 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
984 * thermal_of_cooling_device_register() - register an OF thermal cooling device
985 * @np: a pointer to a device tree node.
986 * @type: the thermal cooling device type.
987 * @devdata: device private data.
988 * @ops: standard thermal cooling devices callbacks.
990 * This function will register a cooling device with device tree node reference.
991 * This interface function adds a new thermal cooling device (fan/processor/...)
992 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
993 * to all the thermal zone devices registered at the same time.
995 * Return: a pointer to the created struct thermal_cooling_device or an
996 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
998 struct thermal_cooling_device *
999 thermal_of_cooling_device_register(struct device_node *np,
1000 const char *type, void *devdata,
1001 const struct thermal_cooling_device_ops *ops)
1003 return __thermal_cooling_device_register(np, type, devdata, ops);
1005 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1007 static void thermal_cooling_device_release(struct device *dev, void *res)
1009 thermal_cooling_device_unregister(
1010 *(struct thermal_cooling_device **)res);
1014 * devm_thermal_of_cooling_device_register() - register an OF thermal cooling
1016 * @dev: a valid struct device pointer of a sensor device.
1017 * @np: a pointer to a device tree node.
1018 * @type: the thermal cooling device type.
1019 * @devdata: device private data.
1020 * @ops: standard thermal cooling devices callbacks.
1022 * This function will register a cooling device with device tree node reference.
1023 * This interface function adds a new thermal cooling device (fan/processor/...)
1024 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1025 * to all the thermal zone devices registered at the same time.
1027 * Return: a pointer to the created struct thermal_cooling_device or an
1028 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1030 struct thermal_cooling_device *
1031 devm_thermal_of_cooling_device_register(struct device *dev,
1032 struct device_node *np,
1033 char *type, void *devdata,
1034 const struct thermal_cooling_device_ops *ops)
1036 struct thermal_cooling_device **ptr, *tcd;
1038 ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr),
1041 return ERR_PTR(-ENOMEM);
1043 tcd = __thermal_cooling_device_register(np, type, devdata, ops);
1050 devres_add(dev, ptr);
1054 EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register);
1056 static bool thermal_cooling_device_present(struct thermal_cooling_device *cdev)
1058 struct thermal_cooling_device *pos = NULL;
1060 list_for_each_entry(pos, &thermal_cdev_list, node) {
1069 * thermal_cooling_device_update - Update a cooling device object
1070 * @cdev: Target cooling device.
1072 * Update @cdev to reflect a change of the underlying hardware or platform.
1074 * Must be called when the maximum cooling state of @cdev becomes invalid and so
1075 * its .get_max_state() callback needs to be run to produce the new maximum
1076 * cooling state value.
1078 void thermal_cooling_device_update(struct thermal_cooling_device *cdev)
1080 struct thermal_instance *ti;
1081 unsigned long state;
1083 if (IS_ERR_OR_NULL(cdev))
1087 * Hold thermal_list_lock throughout the update to prevent the device
1088 * from going away while being updated.
1090 mutex_lock(&thermal_list_lock);
1092 if (!thermal_cooling_device_present(cdev))
1096 * Update under the cdev lock to prevent the state from being set beyond
1097 * the new limit concurrently.
1099 mutex_lock(&cdev->lock);
1101 if (cdev->ops->get_max_state(cdev, &cdev->max_state))
1104 thermal_cooling_device_stats_reinit(cdev);
1106 list_for_each_entry(ti, &cdev->thermal_instances, cdev_node) {
1107 if (ti->upper == cdev->max_state)
1110 if (ti->upper < cdev->max_state) {
1111 if (ti->upper_no_limit)
1112 ti->upper = cdev->max_state;
1117 ti->upper = cdev->max_state;
1118 if (ti->lower > ti->upper)
1119 ti->lower = ti->upper;
1121 if (ti->target == THERMAL_NO_TARGET)
1124 if (ti->target > ti->upper)
1125 ti->target = ti->upper;
1128 if (cdev->ops->get_cur_state(cdev, &state) || state > cdev->max_state)
1131 thermal_cooling_device_stats_update(cdev, state);
1134 mutex_unlock(&cdev->lock);
1137 mutex_unlock(&thermal_list_lock);
1139 EXPORT_SYMBOL_GPL(thermal_cooling_device_update);
1141 static void __unbind(struct thermal_zone_device *tz, int mask,
1142 struct thermal_cooling_device *cdev)
1146 for (i = 0; i < tz->num_trips; i++)
1147 if (mask & (1 << i))
1148 thermal_zone_unbind_cooling_device(tz, i, cdev);
1152 * thermal_cooling_device_unregister - removes a thermal cooling device
1153 * @cdev: the thermal cooling device to remove.
1155 * thermal_cooling_device_unregister() must be called when a registered
1156 * thermal cooling device is no longer needed.
1158 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1161 const struct thermal_zone_params *tzp;
1162 struct thermal_zone_device *tz;
1167 mutex_lock(&thermal_list_lock);
1169 if (!thermal_cooling_device_present(cdev)) {
1170 mutex_unlock(&thermal_list_lock);
1174 list_del(&cdev->node);
1176 /* Unbind all thermal zones associated with 'this' cdev */
1177 list_for_each_entry(tz, &thermal_tz_list, node) {
1178 if (tz->ops->unbind) {
1179 tz->ops->unbind(tz, cdev);
1183 if (!tz->tzp || !tz->tzp->tbp)
1187 for (i = 0; i < tzp->num_tbps; i++) {
1188 if (tzp->tbp[i].cdev == cdev) {
1189 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1190 tzp->tbp[i].cdev = NULL;
1195 mutex_unlock(&thermal_list_lock);
1197 device_unregister(&cdev->device);
1199 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1201 static void bind_tz(struct thermal_zone_device *tz)
1204 struct thermal_cooling_device *pos = NULL;
1205 const struct thermal_zone_params *tzp = tz->tzp;
1207 if (!tzp && !tz->ops->bind)
1210 mutex_lock(&thermal_list_lock);
1212 /* If there is ops->bind, try to use ops->bind */
1213 if (tz->ops->bind) {
1214 list_for_each_entry(pos, &thermal_cdev_list, node) {
1215 ret = tz->ops->bind(tz, pos);
1217 print_bind_err_msg(tz, pos, ret);
1222 if (!tzp || !tzp->tbp)
1225 list_for_each_entry(pos, &thermal_cdev_list, node) {
1226 for (i = 0; i < tzp->num_tbps; i++) {
1227 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
1229 if (tzp->tbp[i].match(tz, pos))
1231 tzp->tbp[i].cdev = pos;
1232 __bind(tz, tzp->tbp[i].trip_mask, pos,
1233 tzp->tbp[i].binding_limits,
1234 tzp->tbp[i].weight);
1238 mutex_unlock(&thermal_list_lock);
1241 static void thermal_set_delay_jiffies(unsigned long *delay_jiffies, int delay_ms)
1243 *delay_jiffies = msecs_to_jiffies(delay_ms);
1244 if (delay_ms > 1000)
1245 *delay_jiffies = round_jiffies(*delay_jiffies);
1248 int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp)
1250 int i, ret = -EINVAL;
1252 if (tz->ops->get_crit_temp)
1253 return tz->ops->get_crit_temp(tz, temp);
1258 mutex_lock(&tz->lock);
1260 for (i = 0; i < tz->num_trips; i++) {
1261 if (tz->trips[i].type == THERMAL_TRIP_CRITICAL) {
1262 *temp = tz->trips[i].temperature;
1268 mutex_unlock(&tz->lock);
1272 EXPORT_SYMBOL_GPL(thermal_zone_get_crit_temp);
1275 * thermal_zone_device_register_with_trips() - register a new thermal zone device
1276 * @type: the thermal zone device type
1277 * @trips: a pointer to an array of thermal trips
1278 * @num_trips: the number of trip points the thermal zone support
1279 * @mask: a bit string indicating the writeablility of trip points
1280 * @devdata: private device data
1281 * @ops: standard thermal zone device callbacks
1282 * @tzp: thermal zone platform parameters
1283 * @passive_delay: number of milliseconds to wait between polls when
1284 * performing passive cooling
1285 * @polling_delay: number of milliseconds to wait between polls when checking
1286 * whether trip points have been crossed (0 for interrupt
1289 * This interface function adds a new thermal zone device (sensor) to
1290 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1291 * thermal cooling devices registered at the same time.
1292 * thermal_zone_device_unregister() must be called when the device is no
1293 * longer needed. The passive cooling depends on the .get_trend() return value.
1295 * Return: a pointer to the created struct thermal_zone_device or an
1296 * in case of error, an ERR_PTR. Caller must check return value with
1297 * IS_ERR*() helpers.
1299 struct thermal_zone_device *
1300 thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *trips, int num_trips, int mask,
1301 void *devdata, struct thermal_zone_device_ops *ops,
1302 struct thermal_zone_params *tzp, int passive_delay,
1305 struct thermal_zone_device *tz;
1309 struct thermal_governor *governor;
1311 if (!type || strlen(type) == 0) {
1312 pr_err("No thermal zone type defined\n");
1313 return ERR_PTR(-EINVAL);
1316 if (strlen(type) >= THERMAL_NAME_LENGTH) {
1317 pr_err("Thermal zone name (%s) too long, should be under %d chars\n",
1318 type, THERMAL_NAME_LENGTH);
1319 return ERR_PTR(-EINVAL);
1323 * Max trip count can't exceed 31 as the "mask >> num_trips" condition.
1324 * For example, shifting by 32 will result in compiler warning:
1325 * warning: right shift count >= width of type [-Wshift-count- overflow]
1327 * Also "mask >> num_trips" will always be true with 32 bit shift.
1328 * E.g. mask = 0x80000000 for trip id 31 to be RW. Then
1329 * mask >> 32 = 0x80000000
1330 * This will result in failure for the below condition.
1332 * Check will be true when the bit 31 of the mask is set.
1333 * 32 bit shift will cause overflow of 4 byte integer.
1335 if (num_trips > (BITS_PER_TYPE(int) - 1) || num_trips < 0 || mask >> num_trips) {
1336 pr_err("Incorrect number of thermal trips\n");
1337 return ERR_PTR(-EINVAL);
1341 pr_err("Thermal zone device ops not defined\n");
1342 return ERR_PTR(-EINVAL);
1345 if (num_trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp) && !trips)
1346 return ERR_PTR(-EINVAL);
1349 return ERR_PTR(-ENODEV);
1351 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
1353 return ERR_PTR(-ENOMEM);
1355 INIT_LIST_HEAD(&tz->thermal_instances);
1357 mutex_init(&tz->lock);
1358 id = ida_alloc(&thermal_tz_ida, GFP_KERNEL);
1365 strscpy(tz->type, type, sizeof(tz->type));
1368 ops->critical = thermal_zone_device_critical;
1372 tz->device.class = thermal_class;
1373 tz->devdata = devdata;
1375 tz->num_trips = num_trips;
1377 thermal_set_delay_jiffies(&tz->passive_delay_jiffies, passive_delay);
1378 thermal_set_delay_jiffies(&tz->polling_delay_jiffies, polling_delay);
1381 /* Add nodes that are always present via .groups */
1382 result = thermal_zone_create_device_groups(tz, mask);
1386 /* A new thermal zone needs to be updated anyway. */
1387 atomic_set(&tz->need_update, 1);
1389 result = dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1391 thermal_zone_destroy_device_groups(tz);
1394 result = device_register(&tz->device);
1396 goto release_device;
1398 for (count = 0; count < num_trips; count++) {
1399 struct thermal_trip trip;
1401 result = thermal_zone_get_trip(tz, count, &trip);
1402 if (result || !trip.temperature)
1403 set_bit(count, &tz->trips_disabled);
1406 /* Update 'this' zone's governor information */
1407 mutex_lock(&thermal_governor_lock);
1410 governor = __find_governor(tz->tzp->governor_name);
1412 governor = def_governor;
1414 result = thermal_set_governor(tz, governor);
1416 mutex_unlock(&thermal_governor_lock);
1420 mutex_unlock(&thermal_governor_lock);
1422 if (!tz->tzp || !tz->tzp->no_hwmon) {
1423 result = thermal_add_hwmon_sysfs(tz);
1428 mutex_lock(&thermal_list_lock);
1429 list_add_tail(&tz->node, &thermal_tz_list);
1430 mutex_unlock(&thermal_list_lock);
1432 /* Bind cooling devices for this zone */
1435 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
1437 thermal_zone_device_init(tz);
1438 /* Update the new thermal zone and mark it as already updated. */
1439 if (atomic_cmpxchg(&tz->need_update, 1, 0))
1440 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1442 thermal_notify_tz_create(tz->id, tz->type);
1447 device_del(&tz->device);
1449 put_device(&tz->device);
1452 ida_free(&thermal_tz_ida, id);
1455 return ERR_PTR(result);
1457 EXPORT_SYMBOL_GPL(thermal_zone_device_register_with_trips);
1459 struct thermal_zone_device *thermal_zone_device_register(const char *type, int ntrips, int mask,
1460 void *devdata, struct thermal_zone_device_ops *ops,
1461 struct thermal_zone_params *tzp, int passive_delay,
1464 return thermal_zone_device_register_with_trips(type, NULL, ntrips, mask,
1466 passive_delay, polling_delay);
1468 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
1470 void *thermal_zone_device_priv(struct thermal_zone_device *tzd)
1472 return tzd->devdata;
1474 EXPORT_SYMBOL_GPL(thermal_zone_device_priv);
1476 const char *thermal_zone_device_type(struct thermal_zone_device *tzd)
1480 EXPORT_SYMBOL_GPL(thermal_zone_device_type);
1482 int thermal_zone_device_id(struct thermal_zone_device *tzd)
1486 EXPORT_SYMBOL_GPL(thermal_zone_device_id);
1489 * thermal_zone_device_unregister - removes the registered thermal zone device
1490 * @tz: the thermal zone device to remove
1492 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1495 const struct thermal_zone_params *tzp;
1496 struct thermal_cooling_device *cdev;
1497 struct thermal_zone_device *pos = NULL;
1505 mutex_lock(&thermal_list_lock);
1506 list_for_each_entry(pos, &thermal_tz_list, node)
1510 /* thermal zone device not found */
1511 mutex_unlock(&thermal_list_lock);
1514 list_del(&tz->node);
1516 /* Unbind all cdevs associated with 'this' thermal zone */
1517 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1518 if (tz->ops->unbind) {
1519 tz->ops->unbind(tz, cdev);
1523 if (!tzp || !tzp->tbp)
1526 for (i = 0; i < tzp->num_tbps; i++) {
1527 if (tzp->tbp[i].cdev == cdev) {
1528 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1529 tzp->tbp[i].cdev = NULL;
1534 mutex_unlock(&thermal_list_lock);
1536 cancel_delayed_work_sync(&tz->poll_queue);
1538 thermal_set_governor(tz, NULL);
1540 thermal_remove_hwmon_sysfs(tz);
1541 ida_free(&thermal_tz_ida, tz->id);
1542 ida_destroy(&tz->ida);
1544 mutex_lock(&tz->lock);
1545 device_del(&tz->device);
1546 mutex_unlock(&tz->lock);
1548 put_device(&tz->device);
1550 thermal_notify_tz_delete(tz_id);
1552 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1555 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1556 * @name: thermal zone name to fetch the temperature
1558 * When only one zone is found with the passed name, returns a reference to it.
1560 * Return: On success returns a reference to an unique thermal zone with
1561 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1562 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1564 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1566 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1567 unsigned int found = 0;
1572 mutex_lock(&thermal_list_lock);
1573 list_for_each_entry(pos, &thermal_tz_list, node)
1574 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1578 mutex_unlock(&thermal_list_lock);
1580 /* nothing has been found, thus an error code for it */
1582 ref = ERR_PTR(-ENODEV);
1584 /* Success only when an unique zone is found */
1585 ref = ERR_PTR(-EEXIST);
1590 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1592 static int thermal_pm_notify(struct notifier_block *nb,
1593 unsigned long mode, void *_unused)
1595 struct thermal_zone_device *tz;
1598 case PM_HIBERNATION_PREPARE:
1599 case PM_RESTORE_PREPARE:
1600 case PM_SUSPEND_PREPARE:
1601 atomic_set(&in_suspend, 1);
1603 case PM_POST_HIBERNATION:
1604 case PM_POST_RESTORE:
1605 case PM_POST_SUSPEND:
1606 atomic_set(&in_suspend, 0);
1607 list_for_each_entry(tz, &thermal_tz_list, node) {
1608 thermal_zone_device_init(tz);
1609 thermal_zone_device_update(tz,
1610 THERMAL_EVENT_UNSPECIFIED);
1619 static struct notifier_block thermal_pm_nb = {
1620 .notifier_call = thermal_pm_notify,
1623 static int __init thermal_init(void)
1627 result = thermal_netlink_init();
1631 result = thermal_register_governors();
1633 goto unregister_netlink;
1635 thermal_class = kzalloc(sizeof(*thermal_class), GFP_KERNEL);
1636 if (!thermal_class) {
1638 goto unregister_governors;
1641 thermal_class->name = "thermal";
1642 thermal_class->dev_release = thermal_release;
1644 result = class_register(thermal_class);
1646 kfree(thermal_class);
1647 thermal_class = NULL;
1648 goto unregister_governors;
1651 result = register_pm_notifier(&thermal_pm_nb);
1653 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1658 unregister_governors:
1659 thermal_unregister_governors();
1661 thermal_netlink_exit();
1663 mutex_destroy(&thermal_list_lock);
1664 mutex_destroy(&thermal_governor_lock);
1667 postcore_initcall(thermal_init);