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 "thermal_trace.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,
311 const struct thermal_trip *trip)
313 tz->governor ? tz->governor->throttle(tz, trip) :
314 def_governor->throttle(tz, trip);
317 void thermal_zone_device_critical(struct thermal_zone_device *tz)
320 * poweroff_delay_ms must be a carefully profiled positive value.
321 * Its a must for forced_emergency_poweroff_work to be scheduled.
323 int poweroff_delay_ms = CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS;
325 dev_emerg(&tz->device, "%s: critical temperature reached, "
326 "shutting down\n", tz->type);
328 hw_protection_shutdown("Temperature too high", poweroff_delay_ms);
330 EXPORT_SYMBOL(thermal_zone_device_critical);
332 static void handle_critical_trips(struct thermal_zone_device *tz,
333 const struct thermal_trip *trip)
335 /* If we have not crossed the trip_temp, we do not care. */
336 if (trip->temperature <= 0 || tz->temperature < trip->temperature)
339 trace_thermal_zone_trip(tz, thermal_zone_trip_id(tz, trip), trip->type);
341 if (trip->type == THERMAL_TRIP_CRITICAL)
342 tz->ops->critical(tz);
343 else if (tz->ops->hot)
347 static void handle_thermal_trip(struct thermal_zone_device *tz,
348 const struct thermal_trip *trip)
350 if (trip->temperature == THERMAL_TEMP_INVALID)
353 if (tz->last_temperature != THERMAL_TEMP_INVALID) {
354 if (tz->last_temperature < trip->temperature &&
355 tz->temperature >= trip->temperature)
356 thermal_notify_tz_trip_up(tz->id,
357 thermal_zone_trip_id(tz, trip),
359 if (tz->last_temperature >= trip->temperature &&
360 tz->temperature < trip->temperature - trip->hysteresis)
361 thermal_notify_tz_trip_down(tz->id,
362 thermal_zone_trip_id(tz, trip),
366 if (trip->type == THERMAL_TRIP_CRITICAL || trip->type == THERMAL_TRIP_HOT)
367 handle_critical_trips(tz, trip);
369 handle_non_critical_trips(tz, trip);
372 static void update_temperature(struct thermal_zone_device *tz)
376 ret = __thermal_zone_get_temp(tz, &temp);
379 dev_warn(&tz->device,
380 "failed to read out thermal zone (%d)\n",
385 tz->last_temperature = tz->temperature;
386 tz->temperature = temp;
388 trace_thermal_temperature(tz);
390 thermal_genl_sampling_temp(tz->id, temp);
393 static void thermal_zone_device_init(struct thermal_zone_device *tz)
395 struct thermal_instance *pos;
396 tz->temperature = THERMAL_TEMP_INVALID;
397 tz->prev_low_trip = -INT_MAX;
398 tz->prev_high_trip = INT_MAX;
399 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
400 pos->initialized = false;
403 void __thermal_zone_device_update(struct thermal_zone_device *tz,
404 enum thermal_notify_event event)
406 const struct thermal_trip *trip;
408 if (atomic_read(&in_suspend))
411 if (WARN_ONCE(!tz->ops->get_temp,
412 "'%s' must not be called without 'get_temp' ops set\n",
416 if (!thermal_zone_device_is_enabled(tz))
419 update_temperature(tz);
421 __thermal_zone_set_trips(tz);
423 tz->notify_event = event;
425 for_each_trip(tz, trip)
426 handle_thermal_trip(tz, trip);
428 monitor_thermal_zone(tz);
431 static int thermal_zone_device_set_mode(struct thermal_zone_device *tz,
432 enum thermal_device_mode mode)
436 mutex_lock(&tz->lock);
438 /* do nothing if mode isn't changing */
439 if (mode == tz->mode) {
440 mutex_unlock(&tz->lock);
445 if (!device_is_registered(&tz->device)) {
446 mutex_unlock(&tz->lock);
451 if (tz->ops->change_mode)
452 ret = tz->ops->change_mode(tz, mode);
457 __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
459 mutex_unlock(&tz->lock);
461 if (mode == THERMAL_DEVICE_ENABLED)
462 thermal_notify_tz_enable(tz->id);
464 thermal_notify_tz_disable(tz->id);
469 int thermal_zone_device_enable(struct thermal_zone_device *tz)
471 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_ENABLED);
473 EXPORT_SYMBOL_GPL(thermal_zone_device_enable);
475 int thermal_zone_device_disable(struct thermal_zone_device *tz)
477 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED);
479 EXPORT_SYMBOL_GPL(thermal_zone_device_disable);
481 int thermal_zone_device_is_enabled(struct thermal_zone_device *tz)
483 lockdep_assert_held(&tz->lock);
485 return tz->mode == THERMAL_DEVICE_ENABLED;
488 void thermal_zone_device_update(struct thermal_zone_device *tz,
489 enum thermal_notify_event event)
491 mutex_lock(&tz->lock);
492 if (device_is_registered(&tz->device))
493 __thermal_zone_device_update(tz, event);
494 mutex_unlock(&tz->lock);
496 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
498 static void thermal_zone_device_check(struct work_struct *work)
500 struct thermal_zone_device *tz = container_of(work, struct
503 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
506 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
509 struct thermal_governor *gov;
512 mutex_lock(&thermal_governor_lock);
513 list_for_each_entry(gov, &thermal_governor_list, governor_list) {
518 mutex_unlock(&thermal_governor_lock);
523 int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
526 struct thermal_cooling_device *cdev;
529 mutex_lock(&thermal_list_lock);
530 list_for_each_entry(cdev, &thermal_cdev_list, node) {
531 ret = cb(cdev, data);
535 mutex_unlock(&thermal_list_lock);
540 int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
543 struct thermal_zone_device *tz;
546 mutex_lock(&thermal_list_lock);
547 list_for_each_entry(tz, &thermal_tz_list, node) {
552 mutex_unlock(&thermal_list_lock);
557 struct thermal_zone_device *thermal_zone_get_by_id(int id)
559 struct thermal_zone_device *tz, *match = NULL;
561 mutex_lock(&thermal_list_lock);
562 list_for_each_entry(tz, &thermal_tz_list, node) {
568 mutex_unlock(&thermal_list_lock);
574 * Device management section: cooling devices, zones devices, and binding
576 * Set of functions provided by the thermal core for:
577 * - cooling devices lifecycle: registration, unregistration,
578 * binding, and unbinding.
579 * - thermal zone devices lifecycle: registration, unregistration,
580 * binding, and unbinding.
584 * thermal_bind_cdev_to_trip - bind a cooling device to a thermal zone
585 * @tz: pointer to struct thermal_zone_device
586 * @trip: trip point the cooling devices is associated with in this zone.
587 * @cdev: pointer to struct thermal_cooling_device
588 * @upper: the Maximum cooling state for this trip point.
589 * THERMAL_NO_LIMIT means no upper limit,
590 * and the cooling device can be in max_state.
591 * @lower: the Minimum cooling state can be used for this trip point.
592 * THERMAL_NO_LIMIT means no lower limit,
593 * and the cooling device can be in cooling state 0.
594 * @weight: The weight of the cooling device to be bound to the
595 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
598 * This interface function bind a thermal cooling device to the certain trip
599 * point of a thermal zone device.
600 * This function is usually called in the thermal zone device .bind callback.
602 * Return: 0 on success, the proper error value otherwise.
604 int thermal_bind_cdev_to_trip(struct thermal_zone_device *tz,
605 const struct thermal_trip *trip,
606 struct thermal_cooling_device *cdev,
607 unsigned long upper, unsigned long lower,
610 struct thermal_instance *dev;
611 struct thermal_instance *pos;
612 struct thermal_zone_device *pos1;
613 struct thermal_cooling_device *pos2;
617 list_for_each_entry(pos1, &thermal_tz_list, node) {
621 list_for_each_entry(pos2, &thermal_cdev_list, node) {
626 if (tz != pos1 || cdev != pos2)
629 /* lower default 0, upper default max_state */
630 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
632 if (upper == THERMAL_NO_LIMIT) {
633 upper = cdev->max_state;
634 upper_no_limit = true;
636 upper_no_limit = false;
639 if (lower > upper || upper > cdev->max_state)
642 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
649 dev->upper_no_limit = upper_no_limit;
651 dev->target = THERMAL_NO_TARGET;
652 dev->weight = weight;
654 result = ida_alloc(&tz->ida, GFP_KERNEL);
659 sprintf(dev->name, "cdev%d", dev->id);
661 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
665 snprintf(dev->attr_name, sizeof(dev->attr_name), "cdev%d_trip_point",
667 sysfs_attr_init(&dev->attr.attr);
668 dev->attr.attr.name = dev->attr_name;
669 dev->attr.attr.mode = 0444;
670 dev->attr.show = trip_point_show;
671 result = device_create_file(&tz->device, &dev->attr);
673 goto remove_symbol_link;
675 snprintf(dev->weight_attr_name, sizeof(dev->weight_attr_name),
676 "cdev%d_weight", dev->id);
677 sysfs_attr_init(&dev->weight_attr.attr);
678 dev->weight_attr.attr.name = dev->weight_attr_name;
679 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
680 dev->weight_attr.show = weight_show;
681 dev->weight_attr.store = weight_store;
682 result = device_create_file(&tz->device, &dev->weight_attr);
684 goto remove_trip_file;
686 mutex_lock(&tz->lock);
687 mutex_lock(&cdev->lock);
688 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
689 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
694 list_add_tail(&dev->tz_node, &tz->thermal_instances);
695 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
696 atomic_set(&tz->need_update, 1);
698 mutex_unlock(&cdev->lock);
699 mutex_unlock(&tz->lock);
704 device_remove_file(&tz->device, &dev->weight_attr);
706 device_remove_file(&tz->device, &dev->attr);
708 sysfs_remove_link(&tz->device.kobj, dev->name);
710 ida_free(&tz->ida, dev->id);
715 EXPORT_SYMBOL_GPL(thermal_bind_cdev_to_trip);
717 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
719 struct thermal_cooling_device *cdev,
720 unsigned long upper, unsigned long lower,
723 if (trip_index < 0 || trip_index >= tz->num_trips)
726 return thermal_bind_cdev_to_trip(tz, &tz->trips[trip_index], cdev,
727 upper, lower, weight);
729 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
732 * thermal_unbind_cdev_from_trip - unbind a cooling device from a thermal zone.
733 * @tz: pointer to a struct thermal_zone_device.
734 * @trip: trip point the cooling devices is associated with in this 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_unbind_cdev_from_trip(struct thermal_zone_device *tz,
744 const struct thermal_trip *trip,
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_free(&tz->ida, pos->id);
773 EXPORT_SYMBOL_GPL(thermal_unbind_cdev_from_trip);
775 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
777 struct thermal_cooling_device *cdev)
779 if (trip_index < 0 || trip_index >= tz->num_trips)
782 return thermal_unbind_cdev_from_trip(tz, &tz->trips[trip_index], cdev);
784 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
786 static void thermal_release(struct device *dev)
788 struct thermal_zone_device *tz;
789 struct thermal_cooling_device *cdev;
791 if (!strncmp(dev_name(dev), "thermal_zone",
792 sizeof("thermal_zone") - 1)) {
793 tz = to_thermal_zone(dev);
794 thermal_zone_destroy_device_groups(tz);
795 mutex_destroy(&tz->lock);
797 } else if (!strncmp(dev_name(dev), "cooling_device",
798 sizeof("cooling_device") - 1)) {
799 cdev = to_cooling_device(dev);
800 thermal_cooling_device_destroy_sysfs(cdev);
802 ida_free(&thermal_cdev_ida, cdev->id);
807 static struct class *thermal_class;
810 void print_bind_err_msg(struct thermal_zone_device *tz,
811 struct thermal_cooling_device *cdev, int ret)
813 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
814 tz->type, cdev->type, ret);
817 static void bind_cdev(struct thermal_cooling_device *cdev)
820 struct thermal_zone_device *pos = NULL;
822 list_for_each_entry(pos, &thermal_tz_list, node) {
823 if (pos->ops->bind) {
824 ret = pos->ops->bind(pos, cdev);
826 print_bind_err_msg(pos, cdev, ret);
832 * __thermal_cooling_device_register() - register a new thermal cooling device
833 * @np: a pointer to a device tree node.
834 * @type: the thermal cooling device type.
835 * @devdata: device private data.
836 * @ops: standard thermal cooling devices callbacks.
838 * This interface function adds a new thermal cooling device (fan/processor/...)
839 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
840 * to all the thermal zone devices registered at the same time.
841 * It also gives the opportunity to link the cooling device to a device tree
842 * node, so that it can be bound to a thermal zone created out of device tree.
844 * Return: a pointer to the created struct thermal_cooling_device or an
845 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
847 static struct thermal_cooling_device *
848 __thermal_cooling_device_register(struct device_node *np,
849 const char *type, void *devdata,
850 const struct thermal_cooling_device_ops *ops)
852 struct thermal_cooling_device *cdev;
853 struct thermal_zone_device *pos = NULL;
856 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
858 return ERR_PTR(-EINVAL);
861 return ERR_PTR(-ENODEV);
863 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
865 return ERR_PTR(-ENOMEM);
867 ret = ida_alloc(&thermal_cdev_ida, GFP_KERNEL);
873 cdev->type = kstrdup(type ? type : "", GFP_KERNEL);
879 mutex_init(&cdev->lock);
880 INIT_LIST_HEAD(&cdev->thermal_instances);
883 cdev->updated = false;
884 cdev->device.class = thermal_class;
885 cdev->devdata = devdata;
887 ret = cdev->ops->get_max_state(cdev, &cdev->max_state);
891 thermal_cooling_device_setup_sysfs(cdev);
893 ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
895 goto out_cooling_dev;
897 ret = device_register(&cdev->device);
899 /* thermal_release() handles rest of the cleanup */
900 put_device(&cdev->device);
904 /* Add 'this' new cdev to the global cdev list */
905 mutex_lock(&thermal_list_lock);
907 list_add(&cdev->node, &thermal_cdev_list);
909 /* Update binding information for 'this' new cdev */
912 list_for_each_entry(pos, &thermal_tz_list, node)
913 if (atomic_cmpxchg(&pos->need_update, 1, 0))
914 thermal_zone_device_update(pos,
915 THERMAL_EVENT_UNSPECIFIED);
917 mutex_unlock(&thermal_list_lock);
922 thermal_cooling_device_destroy_sysfs(cdev);
926 ida_free(&thermal_cdev_ida, id);
933 * thermal_cooling_device_register() - register a new thermal cooling device
934 * @type: the thermal cooling device type.
935 * @devdata: device private data.
936 * @ops: standard thermal cooling devices callbacks.
938 * This interface function adds a new thermal cooling device (fan/processor/...)
939 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
940 * to all the thermal zone devices registered at the same time.
942 * Return: a pointer to the created struct thermal_cooling_device or an
943 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
945 struct thermal_cooling_device *
946 thermal_cooling_device_register(const char *type, void *devdata,
947 const struct thermal_cooling_device_ops *ops)
949 return __thermal_cooling_device_register(NULL, type, devdata, ops);
951 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
954 * thermal_of_cooling_device_register() - register an OF thermal cooling device
955 * @np: a pointer to a device tree node.
956 * @type: the thermal cooling device type.
957 * @devdata: device private data.
958 * @ops: standard thermal cooling devices callbacks.
960 * This function will register a cooling device with device tree node reference.
961 * This interface function adds a new thermal cooling device (fan/processor/...)
962 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
963 * to all the thermal zone devices registered at the same time.
965 * Return: a pointer to the created struct thermal_cooling_device or an
966 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
968 struct thermal_cooling_device *
969 thermal_of_cooling_device_register(struct device_node *np,
970 const char *type, void *devdata,
971 const struct thermal_cooling_device_ops *ops)
973 return __thermal_cooling_device_register(np, type, devdata, ops);
975 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
977 static void thermal_cooling_device_release(struct device *dev, void *res)
979 thermal_cooling_device_unregister(
980 *(struct thermal_cooling_device **)res);
984 * devm_thermal_of_cooling_device_register() - register an OF thermal cooling
986 * @dev: a valid struct device pointer of a sensor device.
987 * @np: a pointer to a device tree node.
988 * @type: the thermal cooling device type.
989 * @devdata: device private data.
990 * @ops: standard thermal cooling devices callbacks.
992 * This function will register a cooling device with device tree node reference.
993 * This interface function adds a new thermal cooling device (fan/processor/...)
994 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
995 * to all the thermal zone devices registered at the same time.
997 * Return: a pointer to the created struct thermal_cooling_device or an
998 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1000 struct thermal_cooling_device *
1001 devm_thermal_of_cooling_device_register(struct device *dev,
1002 struct device_node *np,
1003 char *type, void *devdata,
1004 const struct thermal_cooling_device_ops *ops)
1006 struct thermal_cooling_device **ptr, *tcd;
1008 ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr),
1011 return ERR_PTR(-ENOMEM);
1013 tcd = __thermal_cooling_device_register(np, type, devdata, ops);
1020 devres_add(dev, ptr);
1024 EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register);
1026 static bool thermal_cooling_device_present(struct thermal_cooling_device *cdev)
1028 struct thermal_cooling_device *pos = NULL;
1030 list_for_each_entry(pos, &thermal_cdev_list, node) {
1039 * thermal_cooling_device_update - Update a cooling device object
1040 * @cdev: Target cooling device.
1042 * Update @cdev to reflect a change of the underlying hardware or platform.
1044 * Must be called when the maximum cooling state of @cdev becomes invalid and so
1045 * its .get_max_state() callback needs to be run to produce the new maximum
1046 * cooling state value.
1048 void thermal_cooling_device_update(struct thermal_cooling_device *cdev)
1050 struct thermal_instance *ti;
1051 unsigned long state;
1053 if (IS_ERR_OR_NULL(cdev))
1057 * Hold thermal_list_lock throughout the update to prevent the device
1058 * from going away while being updated.
1060 mutex_lock(&thermal_list_lock);
1062 if (!thermal_cooling_device_present(cdev))
1066 * Update under the cdev lock to prevent the state from being set beyond
1067 * the new limit concurrently.
1069 mutex_lock(&cdev->lock);
1071 if (cdev->ops->get_max_state(cdev, &cdev->max_state))
1074 thermal_cooling_device_stats_reinit(cdev);
1076 list_for_each_entry(ti, &cdev->thermal_instances, cdev_node) {
1077 if (ti->upper == cdev->max_state)
1080 if (ti->upper < cdev->max_state) {
1081 if (ti->upper_no_limit)
1082 ti->upper = cdev->max_state;
1087 ti->upper = cdev->max_state;
1088 if (ti->lower > ti->upper)
1089 ti->lower = ti->upper;
1091 if (ti->target == THERMAL_NO_TARGET)
1094 if (ti->target > ti->upper)
1095 ti->target = ti->upper;
1098 if (cdev->ops->get_cur_state(cdev, &state) || state > cdev->max_state)
1101 thermal_cooling_device_stats_update(cdev, state);
1104 mutex_unlock(&cdev->lock);
1107 mutex_unlock(&thermal_list_lock);
1109 EXPORT_SYMBOL_GPL(thermal_cooling_device_update);
1112 * thermal_cooling_device_unregister - removes a thermal cooling device
1113 * @cdev: the thermal cooling device to remove.
1115 * thermal_cooling_device_unregister() must be called when a registered
1116 * thermal cooling device is no longer needed.
1118 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1120 struct thermal_zone_device *tz;
1125 mutex_lock(&thermal_list_lock);
1127 if (!thermal_cooling_device_present(cdev)) {
1128 mutex_unlock(&thermal_list_lock);
1132 list_del(&cdev->node);
1134 /* Unbind all thermal zones associated with 'this' cdev */
1135 list_for_each_entry(tz, &thermal_tz_list, node) {
1136 if (tz->ops->unbind)
1137 tz->ops->unbind(tz, cdev);
1140 mutex_unlock(&thermal_list_lock);
1142 device_unregister(&cdev->device);
1144 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1146 static void bind_tz(struct thermal_zone_device *tz)
1149 struct thermal_cooling_device *pos = NULL;
1154 mutex_lock(&thermal_list_lock);
1156 list_for_each_entry(pos, &thermal_cdev_list, node) {
1157 ret = tz->ops->bind(tz, pos);
1159 print_bind_err_msg(tz, pos, ret);
1162 mutex_unlock(&thermal_list_lock);
1165 static void thermal_set_delay_jiffies(unsigned long *delay_jiffies, int delay_ms)
1167 *delay_jiffies = msecs_to_jiffies(delay_ms);
1168 if (delay_ms > 1000)
1169 *delay_jiffies = round_jiffies(*delay_jiffies);
1172 int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp)
1174 int i, ret = -EINVAL;
1176 if (tz->ops->get_crit_temp)
1177 return tz->ops->get_crit_temp(tz, temp);
1182 mutex_lock(&tz->lock);
1184 for (i = 0; i < tz->num_trips; i++) {
1185 if (tz->trips[i].type == THERMAL_TRIP_CRITICAL) {
1186 *temp = tz->trips[i].temperature;
1192 mutex_unlock(&tz->lock);
1196 EXPORT_SYMBOL_GPL(thermal_zone_get_crit_temp);
1199 * thermal_zone_device_register_with_trips() - register a new thermal zone device
1200 * @type: the thermal zone device type
1201 * @trips: a pointer to an array of thermal trips
1202 * @num_trips: the number of trip points the thermal zone support
1203 * @mask: a bit string indicating the writeablility of trip points
1204 * @devdata: private device data
1205 * @ops: standard thermal zone device callbacks
1206 * @tzp: thermal zone platform parameters
1207 * @passive_delay: number of milliseconds to wait between polls when
1208 * performing passive cooling
1209 * @polling_delay: number of milliseconds to wait between polls when checking
1210 * whether trip points have been crossed (0 for interrupt
1213 * This interface function adds a new thermal zone device (sensor) to
1214 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1215 * thermal cooling devices registered at the same time.
1216 * thermal_zone_device_unregister() must be called when the device is no
1217 * longer needed. The passive cooling depends on the .get_trend() return value.
1219 * Return: a pointer to the created struct thermal_zone_device or an
1220 * in case of error, an ERR_PTR. Caller must check return value with
1221 * IS_ERR*() helpers.
1223 struct thermal_zone_device *
1224 thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *trips, int num_trips, int mask,
1225 void *devdata, struct thermal_zone_device_ops *ops,
1226 const struct thermal_zone_params *tzp, int passive_delay,
1229 struct thermal_zone_device *tz;
1232 struct thermal_governor *governor;
1234 if (!type || strlen(type) == 0) {
1235 pr_err("No thermal zone type defined\n");
1236 return ERR_PTR(-EINVAL);
1239 if (strlen(type) >= THERMAL_NAME_LENGTH) {
1240 pr_err("Thermal zone name (%s) too long, should be under %d chars\n",
1241 type, THERMAL_NAME_LENGTH);
1242 return ERR_PTR(-EINVAL);
1246 * Max trip count can't exceed 31 as the "mask >> num_trips" condition.
1247 * For example, shifting by 32 will result in compiler warning:
1248 * warning: right shift count >= width of type [-Wshift-count- overflow]
1250 * Also "mask >> num_trips" will always be true with 32 bit shift.
1251 * E.g. mask = 0x80000000 for trip id 31 to be RW. Then
1252 * mask >> 32 = 0x80000000
1253 * This will result in failure for the below condition.
1255 * Check will be true when the bit 31 of the mask is set.
1256 * 32 bit shift will cause overflow of 4 byte integer.
1258 if (num_trips > (BITS_PER_TYPE(int) - 1) || num_trips < 0 || mask >> num_trips) {
1259 pr_err("Incorrect number of thermal trips\n");
1260 return ERR_PTR(-EINVAL);
1264 pr_err("Thermal zone device ops not defined\n");
1265 return ERR_PTR(-EINVAL);
1268 if (num_trips > 0 && !trips)
1269 return ERR_PTR(-EINVAL);
1272 return ERR_PTR(-ENODEV);
1274 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
1276 return ERR_PTR(-ENOMEM);
1279 tz->tzp = kmemdup(tzp, sizeof(*tzp), GFP_KERNEL);
1286 INIT_LIST_HEAD(&tz->thermal_instances);
1288 mutex_init(&tz->lock);
1289 id = ida_alloc(&thermal_tz_ida, GFP_KERNEL);
1296 strscpy(tz->type, type, sizeof(tz->type));
1299 ops->critical = thermal_zone_device_critical;
1302 tz->device.class = thermal_class;
1303 tz->devdata = devdata;
1305 tz->num_trips = num_trips;
1307 thermal_set_delay_jiffies(&tz->passive_delay_jiffies, passive_delay);
1308 thermal_set_delay_jiffies(&tz->polling_delay_jiffies, polling_delay);
1311 /* Add nodes that are always present via .groups */
1312 result = thermal_zone_create_device_groups(tz, mask);
1316 /* A new thermal zone needs to be updated anyway. */
1317 atomic_set(&tz->need_update, 1);
1319 result = dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1321 thermal_zone_destroy_device_groups(tz);
1324 result = device_register(&tz->device);
1326 goto release_device;
1328 /* Update 'this' zone's governor information */
1329 mutex_lock(&thermal_governor_lock);
1332 governor = __find_governor(tz->tzp->governor_name);
1334 governor = def_governor;
1336 result = thermal_set_governor(tz, governor);
1338 mutex_unlock(&thermal_governor_lock);
1342 mutex_unlock(&thermal_governor_lock);
1344 if (!tz->tzp || !tz->tzp->no_hwmon) {
1345 result = thermal_add_hwmon_sysfs(tz);
1350 mutex_lock(&thermal_list_lock);
1351 list_add_tail(&tz->node, &thermal_tz_list);
1352 mutex_unlock(&thermal_list_lock);
1354 /* Bind cooling devices for this zone */
1357 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
1359 thermal_zone_device_init(tz);
1360 /* Update the new thermal zone and mark it as already updated. */
1361 if (atomic_cmpxchg(&tz->need_update, 1, 0))
1362 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1364 thermal_notify_tz_create(tz->id, tz->type);
1369 device_del(&tz->device);
1371 put_device(&tz->device);
1374 ida_free(&thermal_tz_ida, id);
1379 return ERR_PTR(result);
1381 EXPORT_SYMBOL_GPL(thermal_zone_device_register_with_trips);
1383 struct thermal_zone_device *thermal_tripless_zone_device_register(
1386 struct thermal_zone_device_ops *ops,
1387 const struct thermal_zone_params *tzp)
1389 return thermal_zone_device_register_with_trips(type, NULL, 0, 0, devdata,
1392 EXPORT_SYMBOL_GPL(thermal_tripless_zone_device_register);
1394 void *thermal_zone_device_priv(struct thermal_zone_device *tzd)
1396 return tzd->devdata;
1398 EXPORT_SYMBOL_GPL(thermal_zone_device_priv);
1400 const char *thermal_zone_device_type(struct thermal_zone_device *tzd)
1404 EXPORT_SYMBOL_GPL(thermal_zone_device_type);
1406 int thermal_zone_device_id(struct thermal_zone_device *tzd)
1410 EXPORT_SYMBOL_GPL(thermal_zone_device_id);
1412 struct device *thermal_zone_device(struct thermal_zone_device *tzd)
1414 return &tzd->device;
1416 EXPORT_SYMBOL_GPL(thermal_zone_device);
1419 * thermal_zone_device_unregister - removes the registered thermal zone device
1420 * @tz: the thermal zone device to remove
1422 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1425 struct thermal_cooling_device *cdev;
1426 struct thermal_zone_device *pos = NULL;
1433 mutex_lock(&thermal_list_lock);
1434 list_for_each_entry(pos, &thermal_tz_list, node)
1438 /* thermal zone device not found */
1439 mutex_unlock(&thermal_list_lock);
1442 list_del(&tz->node);
1444 /* Unbind all cdevs associated with 'this' thermal zone */
1445 list_for_each_entry(cdev, &thermal_cdev_list, node)
1446 if (tz->ops->unbind)
1447 tz->ops->unbind(tz, cdev);
1449 mutex_unlock(&thermal_list_lock);
1451 cancel_delayed_work_sync(&tz->poll_queue);
1453 thermal_set_governor(tz, NULL);
1455 thermal_remove_hwmon_sysfs(tz);
1456 ida_free(&thermal_tz_ida, tz->id);
1457 ida_destroy(&tz->ida);
1459 mutex_lock(&tz->lock);
1460 device_del(&tz->device);
1461 mutex_unlock(&tz->lock);
1465 put_device(&tz->device);
1467 thermal_notify_tz_delete(tz_id);
1469 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1472 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1473 * @name: thermal zone name to fetch the temperature
1475 * When only one zone is found with the passed name, returns a reference to it.
1477 * Return: On success returns a reference to an unique thermal zone with
1478 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1479 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1481 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1483 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1484 unsigned int found = 0;
1489 mutex_lock(&thermal_list_lock);
1490 list_for_each_entry(pos, &thermal_tz_list, node)
1491 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1495 mutex_unlock(&thermal_list_lock);
1497 /* nothing has been found, thus an error code for it */
1499 ref = ERR_PTR(-ENODEV);
1501 /* Success only when an unique zone is found */
1502 ref = ERR_PTR(-EEXIST);
1507 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1509 static int thermal_pm_notify(struct notifier_block *nb,
1510 unsigned long mode, void *_unused)
1512 struct thermal_zone_device *tz;
1515 case PM_HIBERNATION_PREPARE:
1516 case PM_RESTORE_PREPARE:
1517 case PM_SUSPEND_PREPARE:
1518 atomic_set(&in_suspend, 1);
1520 case PM_POST_HIBERNATION:
1521 case PM_POST_RESTORE:
1522 case PM_POST_SUSPEND:
1523 atomic_set(&in_suspend, 0);
1524 list_for_each_entry(tz, &thermal_tz_list, node) {
1525 thermal_zone_device_init(tz);
1526 thermal_zone_device_update(tz,
1527 THERMAL_EVENT_UNSPECIFIED);
1536 static struct notifier_block thermal_pm_nb = {
1537 .notifier_call = thermal_pm_notify,
1540 static int __init thermal_init(void)
1544 result = thermal_netlink_init();
1548 result = thermal_register_governors();
1550 goto unregister_netlink;
1552 thermal_class = kzalloc(sizeof(*thermal_class), GFP_KERNEL);
1553 if (!thermal_class) {
1555 goto unregister_governors;
1558 thermal_class->name = "thermal";
1559 thermal_class->dev_release = thermal_release;
1561 result = class_register(thermal_class);
1563 kfree(thermal_class);
1564 thermal_class = NULL;
1565 goto unregister_governors;
1568 result = register_pm_notifier(&thermal_pm_nb);
1570 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1575 unregister_governors:
1576 thermal_unregister_governors();
1578 thermal_netlink_exit();
1580 mutex_destroy(&thermal_list_lock);
1581 mutex_destroy(&thermal_governor_lock);
1584 postcore_initcall(thermal_init);