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 struct thermal_governor *def_governor;
43 * Governor section: set of functions to handle thermal governors
45 * Functions to help in the life cycle of thermal governors within
46 * the thermal core and by the thermal governor code.
49 static struct thermal_governor *__find_governor(const char *name)
51 struct thermal_governor *pos;
53 if (!name || !name[0])
56 list_for_each_entry(pos, &thermal_governor_list, governor_list)
57 if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH))
64 * bind_previous_governor() - bind the previous governor of the thermal zone
65 * @tz: a valid pointer to a struct thermal_zone_device
66 * @failed_gov_name: the name of the governor that failed to register
68 * Register the previous governor of the thermal zone after a new
69 * governor has failed to be bound.
71 static void bind_previous_governor(struct thermal_zone_device *tz,
72 const char *failed_gov_name)
74 if (tz->governor && tz->governor->bind_to_tz) {
75 if (tz->governor->bind_to_tz(tz)) {
77 "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n",
78 failed_gov_name, tz->governor->name, tz->type);
85 * thermal_set_governor() - Switch to another governor
86 * @tz: a valid pointer to a struct thermal_zone_device
87 * @new_gov: pointer to the new governor
89 * Change the governor of thermal zone @tz.
91 * Return: 0 on success, an error if the new governor's bind_to_tz() failed.
93 static int thermal_set_governor(struct thermal_zone_device *tz,
94 struct thermal_governor *new_gov)
98 if (tz->governor && tz->governor->unbind_from_tz)
99 tz->governor->unbind_from_tz(tz);
101 if (new_gov && new_gov->bind_to_tz) {
102 ret = new_gov->bind_to_tz(tz);
104 bind_previous_governor(tz, new_gov->name);
110 tz->governor = new_gov;
115 int thermal_register_governor(struct thermal_governor *governor)
119 struct thermal_zone_device *pos;
124 mutex_lock(&thermal_governor_lock);
127 if (!__find_governor(governor->name)) {
131 list_add(&governor->governor_list, &thermal_governor_list);
132 match_default = !strncmp(governor->name,
133 DEFAULT_THERMAL_GOVERNOR,
134 THERMAL_NAME_LENGTH);
136 if (!def_governor && match_default)
137 def_governor = governor;
140 mutex_lock(&thermal_list_lock);
142 list_for_each_entry(pos, &thermal_tz_list, node) {
144 * only thermal zones with specified tz->tzp->governor_name
145 * may run with tz->govenor unset
150 name = pos->tzp->governor_name;
152 if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) {
155 ret = thermal_set_governor(pos, governor);
157 dev_err(&pos->device,
158 "Failed to set governor %s for thermal zone %s: %d\n",
159 governor->name, pos->type, ret);
163 mutex_unlock(&thermal_list_lock);
164 mutex_unlock(&thermal_governor_lock);
169 void thermal_unregister_governor(struct thermal_governor *governor)
171 struct thermal_zone_device *pos;
176 mutex_lock(&thermal_governor_lock);
178 if (!__find_governor(governor->name))
181 mutex_lock(&thermal_list_lock);
183 list_for_each_entry(pos, &thermal_tz_list, node) {
184 if (!strncasecmp(pos->governor->name, governor->name,
185 THERMAL_NAME_LENGTH))
186 thermal_set_governor(pos, NULL);
189 mutex_unlock(&thermal_list_lock);
190 list_del(&governor->governor_list);
192 mutex_unlock(&thermal_governor_lock);
195 int thermal_zone_device_set_policy(struct thermal_zone_device *tz,
198 struct thermal_governor *gov;
201 mutex_lock(&thermal_governor_lock);
202 mutex_lock(&tz->lock);
204 gov = __find_governor(strim(policy));
208 ret = thermal_set_governor(tz, gov);
211 mutex_unlock(&tz->lock);
212 mutex_unlock(&thermal_governor_lock);
214 thermal_notify_tz_gov_change(tz, policy);
219 int thermal_build_list_of_policies(char *buf)
221 struct thermal_governor *pos;
224 mutex_lock(&thermal_governor_lock);
226 list_for_each_entry(pos, &thermal_governor_list, governor_list) {
227 count += sysfs_emit_at(buf, count, "%s ", pos->name);
229 count += sysfs_emit_at(buf, count, "\n");
231 mutex_unlock(&thermal_governor_lock);
236 static void __init thermal_unregister_governors(void)
238 struct thermal_governor **governor;
240 for_each_governor_table(governor)
241 thermal_unregister_governor(*governor);
244 static int __init thermal_register_governors(void)
247 struct thermal_governor **governor;
249 for_each_governor_table(governor) {
250 ret = thermal_register_governor(*governor);
252 pr_err("Failed to register governor: '%s'",
257 pr_info("Registered thermal governor '%s'",
262 struct thermal_governor **gov;
264 for_each_governor_table(gov) {
267 thermal_unregister_governor(*gov);
275 * Zone update section: main control loop applied to each zone while monitoring
276 * in polling mode. The monitoring is done using a workqueue.
277 * Same update may be done on a zone by calling thermal_zone_device_update().
280 * - Non-critical trips will invoke the governor responsible for that zone;
281 * - Hot trips will produce a notification to userspace;
282 * - Critical trip point will cause a system shutdown.
284 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
288 mod_delayed_work(system_freezable_power_efficient_wq,
289 &tz->poll_queue, delay);
291 cancel_delayed_work(&tz->poll_queue);
294 static void monitor_thermal_zone(struct thermal_zone_device *tz)
296 if (tz->mode != THERMAL_DEVICE_ENABLED)
297 thermal_zone_device_set_polling(tz, 0);
298 else if (tz->passive)
299 thermal_zone_device_set_polling(tz, tz->passive_delay_jiffies);
300 else if (tz->polling_delay_jiffies)
301 thermal_zone_device_set_polling(tz, tz->polling_delay_jiffies);
304 static void handle_non_critical_trips(struct thermal_zone_device *tz,
305 const struct thermal_trip *trip)
307 tz->governor ? tz->governor->throttle(tz, trip) :
308 def_governor->throttle(tz, trip);
311 void thermal_governor_update_tz(struct thermal_zone_device *tz,
312 enum thermal_notify_event reason)
314 if (!tz->governor || !tz->governor->update_tz)
317 tz->governor->update_tz(tz, reason);
320 static void thermal_zone_device_halt(struct thermal_zone_device *tz, bool shutdown)
323 * poweroff_delay_ms must be a carefully profiled positive value.
324 * Its a must for forced_emergency_poweroff_work to be scheduled.
326 int poweroff_delay_ms = CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS;
327 const char *msg = "Temperature too high";
329 dev_emerg(&tz->device, "%s: critical temperature reached\n", tz->type);
332 hw_protection_shutdown(msg, poweroff_delay_ms);
334 hw_protection_reboot(msg, poweroff_delay_ms);
337 void thermal_zone_device_critical(struct thermal_zone_device *tz)
339 thermal_zone_device_halt(tz, true);
341 EXPORT_SYMBOL(thermal_zone_device_critical);
343 void thermal_zone_device_critical_reboot(struct thermal_zone_device *tz)
345 thermal_zone_device_halt(tz, false);
348 static void handle_critical_trips(struct thermal_zone_device *tz,
349 const struct thermal_trip *trip)
351 /* If we have not crossed the trip_temp, we do not care. */
352 if (trip->temperature <= 0 || tz->temperature < trip->temperature)
355 trace_thermal_zone_trip(tz, thermal_zone_trip_id(tz, trip), trip->type);
357 if (trip->type == THERMAL_TRIP_CRITICAL)
358 tz->ops.critical(tz);
359 else if (tz->ops.hot)
363 static void handle_thermal_trip(struct thermal_zone_device *tz,
364 struct thermal_trip *trip)
366 if (trip->temperature == THERMAL_TEMP_INVALID)
369 if (tz->last_temperature == THERMAL_TEMP_INVALID) {
370 /* Initialization. */
371 trip->threshold = trip->temperature;
372 if (tz->temperature >= trip->threshold)
373 trip->threshold -= trip->hysteresis;
374 } else if (tz->last_temperature < trip->threshold) {
376 * The trip threshold is equal to the trip temperature, unless
377 * the latter has changed in the meantime. In either case,
378 * the trip is crossed if the current zone temperature is at
379 * least equal to its temperature, but otherwise ensure that
380 * the threshold and the trip temperature will be equal.
382 if (tz->temperature >= trip->temperature) {
383 thermal_notify_tz_trip_up(tz, trip);
384 thermal_debug_tz_trip_up(tz, trip);
385 trip->threshold = trip->temperature - trip->hysteresis;
387 trip->threshold = trip->temperature;
391 * The previous zone temperature was above or equal to the trip
392 * threshold, which would be equal to the "low temperature" of
393 * the trip (its temperature minus its hysteresis), unless the
394 * trip temperature or hysteresis had changed. In either case,
395 * the trip is crossed if the current zone temperature is below
396 * the low temperature of the trip, but otherwise ensure that
397 * the trip threshold will be equal to the low temperature of
400 if (tz->temperature < trip->temperature - trip->hysteresis) {
401 thermal_notify_tz_trip_down(tz, trip);
402 thermal_debug_tz_trip_down(tz, trip);
403 trip->threshold = trip->temperature;
405 trip->threshold = trip->temperature - trip->hysteresis;
409 if (trip->type == THERMAL_TRIP_CRITICAL || trip->type == THERMAL_TRIP_HOT)
410 handle_critical_trips(tz, trip);
412 handle_non_critical_trips(tz, trip);
415 static void update_temperature(struct thermal_zone_device *tz)
419 ret = __thermal_zone_get_temp(tz, &temp);
422 dev_warn(&tz->device,
423 "failed to read out thermal zone (%d)\n",
428 tz->last_temperature = tz->temperature;
429 tz->temperature = temp;
431 trace_thermal_temperature(tz);
433 thermal_genl_sampling_temp(tz->id, temp);
434 thermal_debug_update_temp(tz);
437 static void thermal_zone_device_check(struct work_struct *work)
439 struct thermal_zone_device *tz = container_of(work, struct
442 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
445 static void thermal_zone_device_init(struct thermal_zone_device *tz)
447 struct thermal_instance *pos;
449 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
451 tz->temperature = THERMAL_TEMP_INVALID;
452 tz->prev_low_trip = -INT_MAX;
453 tz->prev_high_trip = INT_MAX;
454 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
455 pos->initialized = false;
458 void __thermal_zone_device_update(struct thermal_zone_device *tz,
459 enum thermal_notify_event event)
461 struct thermal_trip *trip;
466 if (!thermal_zone_device_is_enabled(tz))
469 update_temperature(tz);
471 __thermal_zone_set_trips(tz);
473 tz->notify_event = event;
475 for_each_trip(tz, trip)
476 handle_thermal_trip(tz, trip);
478 monitor_thermal_zone(tz);
481 static int thermal_zone_device_set_mode(struct thermal_zone_device *tz,
482 enum thermal_device_mode mode)
486 mutex_lock(&tz->lock);
488 /* do nothing if mode isn't changing */
489 if (mode == tz->mode) {
490 mutex_unlock(&tz->lock);
495 if (tz->ops.change_mode)
496 ret = tz->ops.change_mode(tz, mode);
501 __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
503 mutex_unlock(&tz->lock);
505 if (mode == THERMAL_DEVICE_ENABLED)
506 thermal_notify_tz_enable(tz);
508 thermal_notify_tz_disable(tz);
513 int thermal_zone_device_enable(struct thermal_zone_device *tz)
515 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_ENABLED);
517 EXPORT_SYMBOL_GPL(thermal_zone_device_enable);
519 int thermal_zone_device_disable(struct thermal_zone_device *tz)
521 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED);
523 EXPORT_SYMBOL_GPL(thermal_zone_device_disable);
525 int thermal_zone_device_is_enabled(struct thermal_zone_device *tz)
527 lockdep_assert_held(&tz->lock);
529 return tz->mode == THERMAL_DEVICE_ENABLED;
532 static bool thermal_zone_is_present(struct thermal_zone_device *tz)
534 return !list_empty(&tz->node);
537 void thermal_zone_device_update(struct thermal_zone_device *tz,
538 enum thermal_notify_event event)
540 mutex_lock(&tz->lock);
541 if (thermal_zone_is_present(tz))
542 __thermal_zone_device_update(tz, event);
543 mutex_unlock(&tz->lock);
545 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
547 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
550 struct thermal_governor *gov;
553 mutex_lock(&thermal_governor_lock);
554 list_for_each_entry(gov, &thermal_governor_list, governor_list) {
559 mutex_unlock(&thermal_governor_lock);
564 int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
567 struct thermal_cooling_device *cdev;
570 mutex_lock(&thermal_list_lock);
571 list_for_each_entry(cdev, &thermal_cdev_list, node) {
572 ret = cb(cdev, data);
576 mutex_unlock(&thermal_list_lock);
581 int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
584 struct thermal_zone_device *tz;
587 mutex_lock(&thermal_list_lock);
588 list_for_each_entry(tz, &thermal_tz_list, node) {
593 mutex_unlock(&thermal_list_lock);
598 struct thermal_zone_device *thermal_zone_get_by_id(int id)
600 struct thermal_zone_device *tz, *match = NULL;
602 mutex_lock(&thermal_list_lock);
603 list_for_each_entry(tz, &thermal_tz_list, node) {
609 mutex_unlock(&thermal_list_lock);
615 * Device management section: cooling devices, zones devices, and binding
617 * Set of functions provided by the thermal core for:
618 * - cooling devices lifecycle: registration, unregistration,
619 * binding, and unbinding.
620 * - thermal zone devices lifecycle: registration, unregistration,
621 * binding, and unbinding.
625 * thermal_bind_cdev_to_trip - bind a cooling device to a thermal zone
626 * @tz: pointer to struct thermal_zone_device
627 * @trip: trip point the cooling devices is associated with in this zone.
628 * @cdev: pointer to struct thermal_cooling_device
629 * @upper: the Maximum cooling state for this trip point.
630 * THERMAL_NO_LIMIT means no upper limit,
631 * and the cooling device can be in max_state.
632 * @lower: the Minimum cooling state can be used for this trip point.
633 * THERMAL_NO_LIMIT means no lower limit,
634 * and the cooling device can be in cooling state 0.
635 * @weight: The weight of the cooling device to be bound to the
636 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
639 * This interface function bind a thermal cooling device to the certain trip
640 * point of a thermal zone device.
641 * This function is usually called in the thermal zone device .bind callback.
643 * Return: 0 on success, the proper error value otherwise.
645 int thermal_bind_cdev_to_trip(struct thermal_zone_device *tz,
646 const struct thermal_trip *trip,
647 struct thermal_cooling_device *cdev,
648 unsigned long upper, unsigned long lower,
651 struct thermal_instance *dev;
652 struct thermal_instance *pos;
653 struct thermal_zone_device *pos1;
654 struct thermal_cooling_device *pos2;
658 list_for_each_entry(pos1, &thermal_tz_list, node) {
662 list_for_each_entry(pos2, &thermal_cdev_list, node) {
667 if (tz != pos1 || cdev != pos2)
670 /* lower default 0, upper default max_state */
671 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
673 if (upper == THERMAL_NO_LIMIT) {
674 upper = cdev->max_state;
675 upper_no_limit = true;
677 upper_no_limit = false;
680 if (lower > upper || upper > cdev->max_state)
683 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
690 dev->upper_no_limit = upper_no_limit;
692 dev->target = THERMAL_NO_TARGET;
693 dev->weight = weight;
695 result = ida_alloc(&tz->ida, GFP_KERNEL);
700 sprintf(dev->name, "cdev%d", dev->id);
702 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
706 snprintf(dev->attr_name, sizeof(dev->attr_name), "cdev%d_trip_point",
708 sysfs_attr_init(&dev->attr.attr);
709 dev->attr.attr.name = dev->attr_name;
710 dev->attr.attr.mode = 0444;
711 dev->attr.show = trip_point_show;
712 result = device_create_file(&tz->device, &dev->attr);
714 goto remove_symbol_link;
716 snprintf(dev->weight_attr_name, sizeof(dev->weight_attr_name),
717 "cdev%d_weight", dev->id);
718 sysfs_attr_init(&dev->weight_attr.attr);
719 dev->weight_attr.attr.name = dev->weight_attr_name;
720 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
721 dev->weight_attr.show = weight_show;
722 dev->weight_attr.store = weight_store;
723 result = device_create_file(&tz->device, &dev->weight_attr);
725 goto remove_trip_file;
727 mutex_lock(&tz->lock);
728 mutex_lock(&cdev->lock);
729 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
730 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
735 list_add_tail(&dev->tz_node, &tz->thermal_instances);
736 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
737 atomic_set(&tz->need_update, 1);
739 thermal_governor_update_tz(tz, THERMAL_TZ_BIND_CDEV);
741 mutex_unlock(&cdev->lock);
742 mutex_unlock(&tz->lock);
747 device_remove_file(&tz->device, &dev->weight_attr);
749 device_remove_file(&tz->device, &dev->attr);
751 sysfs_remove_link(&tz->device.kobj, dev->name);
753 ida_free(&tz->ida, dev->id);
758 EXPORT_SYMBOL_GPL(thermal_bind_cdev_to_trip);
760 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
762 struct thermal_cooling_device *cdev,
763 unsigned long upper, unsigned long lower,
766 if (trip_index < 0 || trip_index >= tz->num_trips)
769 return thermal_bind_cdev_to_trip(tz, &tz->trips[trip_index], cdev,
770 upper, lower, weight);
772 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
775 * thermal_unbind_cdev_from_trip - unbind a cooling device from a thermal zone.
776 * @tz: pointer to a struct thermal_zone_device.
777 * @trip: trip point the cooling devices is associated with in this zone.
778 * @cdev: pointer to a struct thermal_cooling_device.
780 * This interface function unbind a thermal cooling device from the certain
781 * trip point of a thermal zone device.
782 * This function is usually called in the thermal zone device .unbind callback.
784 * Return: 0 on success, the proper error value otherwise.
786 int thermal_unbind_cdev_from_trip(struct thermal_zone_device *tz,
787 const struct thermal_trip *trip,
788 struct thermal_cooling_device *cdev)
790 struct thermal_instance *pos, *next;
792 mutex_lock(&tz->lock);
793 mutex_lock(&cdev->lock);
794 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
795 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
796 list_del(&pos->tz_node);
797 list_del(&pos->cdev_node);
799 thermal_governor_update_tz(tz, THERMAL_TZ_UNBIND_CDEV);
801 mutex_unlock(&cdev->lock);
802 mutex_unlock(&tz->lock);
806 mutex_unlock(&cdev->lock);
807 mutex_unlock(&tz->lock);
812 device_remove_file(&tz->device, &pos->weight_attr);
813 device_remove_file(&tz->device, &pos->attr);
814 sysfs_remove_link(&tz->device.kobj, pos->name);
815 ida_free(&tz->ida, pos->id);
819 EXPORT_SYMBOL_GPL(thermal_unbind_cdev_from_trip);
821 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
823 struct thermal_cooling_device *cdev)
825 if (trip_index < 0 || trip_index >= tz->num_trips)
828 return thermal_unbind_cdev_from_trip(tz, &tz->trips[trip_index], cdev);
830 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
832 static void thermal_release(struct device *dev)
834 struct thermal_zone_device *tz;
835 struct thermal_cooling_device *cdev;
837 if (!strncmp(dev_name(dev), "thermal_zone",
838 sizeof("thermal_zone") - 1)) {
839 tz = to_thermal_zone(dev);
840 thermal_zone_destroy_device_groups(tz);
841 mutex_destroy(&tz->lock);
842 complete(&tz->removal);
843 } else if (!strncmp(dev_name(dev), "cooling_device",
844 sizeof("cooling_device") - 1)) {
845 cdev = to_cooling_device(dev);
846 thermal_cooling_device_destroy_sysfs(cdev);
847 kfree_const(cdev->type);
848 ida_free(&thermal_cdev_ida, cdev->id);
853 static struct class *thermal_class;
856 void print_bind_err_msg(struct thermal_zone_device *tz,
857 struct thermal_cooling_device *cdev, int ret)
859 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
860 tz->type, cdev->type, ret);
863 static void bind_cdev(struct thermal_cooling_device *cdev)
866 struct thermal_zone_device *pos = NULL;
868 list_for_each_entry(pos, &thermal_tz_list, node) {
870 ret = pos->ops.bind(pos, cdev);
872 print_bind_err_msg(pos, cdev, ret);
878 * __thermal_cooling_device_register() - register a new thermal cooling device
879 * @np: a pointer to a device tree node.
880 * @type: the thermal cooling device type.
881 * @devdata: device private data.
882 * @ops: standard thermal cooling devices callbacks.
884 * This interface function adds a new thermal cooling device (fan/processor/...)
885 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
886 * to all the thermal zone devices registered at the same time.
887 * It also gives the opportunity to link the cooling device to a device tree
888 * node, so that it can be bound to a thermal zone created out of device tree.
890 * Return: a pointer to the created struct thermal_cooling_device or an
891 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
893 static struct thermal_cooling_device *
894 __thermal_cooling_device_register(struct device_node *np,
895 const char *type, void *devdata,
896 const struct thermal_cooling_device_ops *ops)
898 struct thermal_cooling_device *cdev;
899 struct thermal_zone_device *pos = NULL;
902 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
904 return ERR_PTR(-EINVAL);
907 return ERR_PTR(-ENODEV);
909 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
911 return ERR_PTR(-ENOMEM);
913 ret = ida_alloc(&thermal_cdev_ida, GFP_KERNEL);
919 cdev->type = kstrdup_const(type ? type : "", GFP_KERNEL);
925 mutex_init(&cdev->lock);
926 INIT_LIST_HEAD(&cdev->thermal_instances);
929 cdev->updated = false;
930 cdev->device.class = thermal_class;
931 cdev->devdata = devdata;
933 ret = cdev->ops->get_max_state(cdev, &cdev->max_state);
937 thermal_cooling_device_setup_sysfs(cdev);
939 ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
941 goto out_cooling_dev;
943 ret = device_register(&cdev->device);
945 /* thermal_release() handles rest of the cleanup */
946 put_device(&cdev->device);
950 /* Add 'this' new cdev to the global cdev list */
951 mutex_lock(&thermal_list_lock);
953 list_add(&cdev->node, &thermal_cdev_list);
955 /* Update binding information for 'this' new cdev */
958 list_for_each_entry(pos, &thermal_tz_list, node)
959 if (atomic_cmpxchg(&pos->need_update, 1, 0))
960 thermal_zone_device_update(pos,
961 THERMAL_EVENT_UNSPECIFIED);
963 mutex_unlock(&thermal_list_lock);
965 thermal_debug_cdev_add(cdev);
970 thermal_cooling_device_destroy_sysfs(cdev);
972 kfree_const(cdev->type);
974 ida_free(&thermal_cdev_ida, id);
981 * thermal_cooling_device_register() - register a new thermal cooling device
982 * @type: the thermal cooling device type.
983 * @devdata: device private data.
984 * @ops: standard thermal cooling devices callbacks.
986 * This interface function adds a new thermal cooling device (fan/processor/...)
987 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
988 * to all the thermal zone devices registered at the same time.
990 * Return: a pointer to the created struct thermal_cooling_device or an
991 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
993 struct thermal_cooling_device *
994 thermal_cooling_device_register(const char *type, void *devdata,
995 const struct thermal_cooling_device_ops *ops)
997 return __thermal_cooling_device_register(NULL, type, devdata, ops);
999 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
1002 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1003 * @np: a pointer to a device tree node.
1004 * @type: the thermal cooling device type.
1005 * @devdata: device private data.
1006 * @ops: standard thermal cooling devices callbacks.
1008 * This function will register a cooling device with device tree node reference.
1009 * This interface function adds a new thermal cooling device (fan/processor/...)
1010 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1011 * to all the thermal zone devices registered at the same time.
1013 * Return: a pointer to the created struct thermal_cooling_device or an
1014 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1016 struct thermal_cooling_device *
1017 thermal_of_cooling_device_register(struct device_node *np,
1018 const char *type, void *devdata,
1019 const struct thermal_cooling_device_ops *ops)
1021 return __thermal_cooling_device_register(np, type, devdata, ops);
1023 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1025 static void thermal_cooling_device_release(struct device *dev, void *res)
1027 thermal_cooling_device_unregister(
1028 *(struct thermal_cooling_device **)res);
1032 * devm_thermal_of_cooling_device_register() - register an OF thermal cooling
1034 * @dev: a valid struct device pointer of a sensor device.
1035 * @np: a pointer to a device tree node.
1036 * @type: the thermal cooling device type.
1037 * @devdata: device private data.
1038 * @ops: standard thermal cooling devices callbacks.
1040 * This function will register a cooling device with device tree node reference.
1041 * This interface function adds a new thermal cooling device (fan/processor/...)
1042 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1043 * to all the thermal zone devices registered at the same time.
1045 * Return: a pointer to the created struct thermal_cooling_device or an
1046 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1048 struct thermal_cooling_device *
1049 devm_thermal_of_cooling_device_register(struct device *dev,
1050 struct device_node *np,
1051 char *type, void *devdata,
1052 const struct thermal_cooling_device_ops *ops)
1054 struct thermal_cooling_device **ptr, *tcd;
1056 ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr),
1059 return ERR_PTR(-ENOMEM);
1061 tcd = __thermal_cooling_device_register(np, type, devdata, ops);
1068 devres_add(dev, ptr);
1072 EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register);
1074 static bool thermal_cooling_device_present(struct thermal_cooling_device *cdev)
1076 struct thermal_cooling_device *pos = NULL;
1078 list_for_each_entry(pos, &thermal_cdev_list, node) {
1087 * thermal_cooling_device_update - Update a cooling device object
1088 * @cdev: Target cooling device.
1090 * Update @cdev to reflect a change of the underlying hardware or platform.
1092 * Must be called when the maximum cooling state of @cdev becomes invalid and so
1093 * its .get_max_state() callback needs to be run to produce the new maximum
1094 * cooling state value.
1096 void thermal_cooling_device_update(struct thermal_cooling_device *cdev)
1098 struct thermal_instance *ti;
1099 unsigned long state;
1101 if (IS_ERR_OR_NULL(cdev))
1105 * Hold thermal_list_lock throughout the update to prevent the device
1106 * from going away while being updated.
1108 mutex_lock(&thermal_list_lock);
1110 if (!thermal_cooling_device_present(cdev))
1114 * Update under the cdev lock to prevent the state from being set beyond
1115 * the new limit concurrently.
1117 mutex_lock(&cdev->lock);
1119 if (cdev->ops->get_max_state(cdev, &cdev->max_state))
1122 thermal_cooling_device_stats_reinit(cdev);
1124 list_for_each_entry(ti, &cdev->thermal_instances, cdev_node) {
1125 if (ti->upper == cdev->max_state)
1128 if (ti->upper < cdev->max_state) {
1129 if (ti->upper_no_limit)
1130 ti->upper = cdev->max_state;
1135 ti->upper = cdev->max_state;
1136 if (ti->lower > ti->upper)
1137 ti->lower = ti->upper;
1139 if (ti->target == THERMAL_NO_TARGET)
1142 if (ti->target > ti->upper)
1143 ti->target = ti->upper;
1146 if (cdev->ops->get_cur_state(cdev, &state) || state > cdev->max_state)
1149 thermal_cooling_device_stats_update(cdev, state);
1152 mutex_unlock(&cdev->lock);
1155 mutex_unlock(&thermal_list_lock);
1157 EXPORT_SYMBOL_GPL(thermal_cooling_device_update);
1160 * thermal_cooling_device_unregister - removes a thermal cooling device
1161 * @cdev: the thermal cooling device to remove.
1163 * thermal_cooling_device_unregister() must be called when a registered
1164 * thermal cooling device is no longer needed.
1166 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1168 struct thermal_zone_device *tz;
1173 thermal_debug_cdev_remove(cdev);
1175 mutex_lock(&thermal_list_lock);
1177 if (!thermal_cooling_device_present(cdev)) {
1178 mutex_unlock(&thermal_list_lock);
1182 list_del(&cdev->node);
1184 /* Unbind all thermal zones associated with 'this' cdev */
1185 list_for_each_entry(tz, &thermal_tz_list, node) {
1187 tz->ops.unbind(tz, cdev);
1190 mutex_unlock(&thermal_list_lock);
1192 device_unregister(&cdev->device);
1194 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1196 static void bind_tz(struct thermal_zone_device *tz)
1199 struct thermal_cooling_device *pos = NULL;
1204 mutex_lock(&thermal_list_lock);
1206 list_for_each_entry(pos, &thermal_cdev_list, node) {
1207 ret = tz->ops.bind(tz, pos);
1209 print_bind_err_msg(tz, pos, ret);
1212 mutex_unlock(&thermal_list_lock);
1215 static void thermal_set_delay_jiffies(unsigned long *delay_jiffies, int delay_ms)
1217 *delay_jiffies = msecs_to_jiffies(delay_ms);
1218 if (delay_ms > 1000)
1219 *delay_jiffies = round_jiffies(*delay_jiffies);
1222 int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp)
1224 int i, ret = -EINVAL;
1226 if (tz->ops.get_crit_temp)
1227 return tz->ops.get_crit_temp(tz, temp);
1229 mutex_lock(&tz->lock);
1231 for (i = 0; i < tz->num_trips; i++) {
1232 if (tz->trips[i].type == THERMAL_TRIP_CRITICAL) {
1233 *temp = tz->trips[i].temperature;
1239 mutex_unlock(&tz->lock);
1243 EXPORT_SYMBOL_GPL(thermal_zone_get_crit_temp);
1246 * thermal_zone_device_register_with_trips() - register a new thermal zone device
1247 * @type: the thermal zone device type
1248 * @trips: a pointer to an array of thermal trips
1249 * @num_trips: the number of trip points the thermal zone support
1250 * @devdata: private device data
1251 * @ops: standard thermal zone device callbacks
1252 * @tzp: thermal zone platform parameters
1253 * @passive_delay: number of milliseconds to wait between polls when
1254 * performing passive cooling
1255 * @polling_delay: number of milliseconds to wait between polls when checking
1256 * whether trip points have been crossed (0 for interrupt
1259 * This interface function adds a new thermal zone device (sensor) to
1260 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1261 * thermal cooling devices registered at the same time.
1262 * thermal_zone_device_unregister() must be called when the device is no
1263 * longer needed. The passive cooling depends on the .get_trend() return value.
1265 * Return: a pointer to the created struct thermal_zone_device or an
1266 * in case of error, an ERR_PTR. Caller must check return value with
1267 * IS_ERR*() helpers.
1269 struct thermal_zone_device *
1270 thermal_zone_device_register_with_trips(const char *type,
1271 const struct thermal_trip *trips,
1272 int num_trips, void *devdata,
1273 const struct thermal_zone_device_ops *ops,
1274 const struct thermal_zone_params *tzp,
1275 int passive_delay, int polling_delay)
1277 struct thermal_zone_device *tz;
1280 struct thermal_governor *governor;
1282 if (!type || strlen(type) == 0) {
1283 pr_err("No thermal zone type defined\n");
1284 return ERR_PTR(-EINVAL);
1287 if (strlen(type) >= THERMAL_NAME_LENGTH) {
1288 pr_err("Thermal zone name (%s) too long, should be under %d chars\n",
1289 type, THERMAL_NAME_LENGTH);
1290 return ERR_PTR(-EINVAL);
1293 if (num_trips < 0) {
1294 pr_err("Incorrect number of thermal trips\n");
1295 return ERR_PTR(-EINVAL);
1298 if (!ops || !ops->get_temp) {
1299 pr_err("Thermal zone device ops not defined\n");
1300 return ERR_PTR(-EINVAL);
1303 if (num_trips > 0 && !trips)
1304 return ERR_PTR(-EINVAL);
1307 return ERR_PTR(-ENODEV);
1309 tz = kzalloc(struct_size(tz, trips, num_trips), GFP_KERNEL);
1311 return ERR_PTR(-ENOMEM);
1314 tz->tzp = kmemdup(tzp, sizeof(*tzp), GFP_KERNEL);
1321 INIT_LIST_HEAD(&tz->thermal_instances);
1322 INIT_LIST_HEAD(&tz->node);
1324 mutex_init(&tz->lock);
1325 init_completion(&tz->removal);
1326 id = ida_alloc(&thermal_tz_ida, GFP_KERNEL);
1333 strscpy(tz->type, type, sizeof(tz->type));
1336 if (!tz->ops.critical)
1337 tz->ops.critical = thermal_zone_device_critical;
1339 tz->device.class = thermal_class;
1340 tz->devdata = devdata;
1341 tz->num_trips = num_trips;
1342 memcpy(tz->trips, trips, num_trips * sizeof(*trips));
1344 thermal_set_delay_jiffies(&tz->passive_delay_jiffies, passive_delay);
1345 thermal_set_delay_jiffies(&tz->polling_delay_jiffies, polling_delay);
1348 /* Add nodes that are always present via .groups */
1349 result = thermal_zone_create_device_groups(tz);
1353 /* A new thermal zone needs to be updated anyway. */
1354 atomic_set(&tz->need_update, 1);
1356 result = dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1358 thermal_zone_destroy_device_groups(tz);
1361 result = device_register(&tz->device);
1363 goto release_device;
1365 /* Update 'this' zone's governor information */
1366 mutex_lock(&thermal_governor_lock);
1369 governor = __find_governor(tz->tzp->governor_name);
1371 governor = def_governor;
1373 result = thermal_set_governor(tz, governor);
1375 mutex_unlock(&thermal_governor_lock);
1379 mutex_unlock(&thermal_governor_lock);
1381 if (!tz->tzp || !tz->tzp->no_hwmon) {
1382 result = thermal_add_hwmon_sysfs(tz);
1387 mutex_lock(&thermal_list_lock);
1388 mutex_lock(&tz->lock);
1389 list_add_tail(&tz->node, &thermal_tz_list);
1390 mutex_unlock(&tz->lock);
1391 mutex_unlock(&thermal_list_lock);
1393 /* Bind cooling devices for this zone */
1396 thermal_zone_device_init(tz);
1397 /* Update the new thermal zone and mark it as already updated. */
1398 if (atomic_cmpxchg(&tz->need_update, 1, 0))
1399 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1401 thermal_notify_tz_create(tz);
1403 thermal_debug_tz_add(tz);
1408 device_del(&tz->device);
1410 put_device(&tz->device);
1412 ida_free(&thermal_tz_ida, id);
1417 return ERR_PTR(result);
1419 EXPORT_SYMBOL_GPL(thermal_zone_device_register_with_trips);
1421 struct thermal_zone_device *thermal_tripless_zone_device_register(
1424 const struct thermal_zone_device_ops *ops,
1425 const struct thermal_zone_params *tzp)
1427 return thermal_zone_device_register_with_trips(type, NULL, 0, devdata,
1430 EXPORT_SYMBOL_GPL(thermal_tripless_zone_device_register);
1432 void *thermal_zone_device_priv(struct thermal_zone_device *tzd)
1434 return tzd->devdata;
1436 EXPORT_SYMBOL_GPL(thermal_zone_device_priv);
1438 const char *thermal_zone_device_type(struct thermal_zone_device *tzd)
1442 EXPORT_SYMBOL_GPL(thermal_zone_device_type);
1444 int thermal_zone_device_id(struct thermal_zone_device *tzd)
1448 EXPORT_SYMBOL_GPL(thermal_zone_device_id);
1450 struct device *thermal_zone_device(struct thermal_zone_device *tzd)
1452 return &tzd->device;
1454 EXPORT_SYMBOL_GPL(thermal_zone_device);
1457 * thermal_zone_device_unregister - removes the registered thermal zone device
1458 * @tz: the thermal zone device to remove
1460 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1462 struct thermal_cooling_device *cdev;
1463 struct thermal_zone_device *pos = NULL;
1468 thermal_debug_tz_remove(tz);
1470 mutex_lock(&thermal_list_lock);
1471 list_for_each_entry(pos, &thermal_tz_list, node)
1475 /* thermal zone device not found */
1476 mutex_unlock(&thermal_list_lock);
1480 mutex_lock(&tz->lock);
1481 list_del(&tz->node);
1482 mutex_unlock(&tz->lock);
1484 /* Unbind all cdevs associated with 'this' thermal zone */
1485 list_for_each_entry(cdev, &thermal_cdev_list, node)
1487 tz->ops.unbind(tz, cdev);
1489 mutex_unlock(&thermal_list_lock);
1491 cancel_delayed_work_sync(&tz->poll_queue);
1493 thermal_set_governor(tz, NULL);
1495 thermal_remove_hwmon_sysfs(tz);
1496 ida_free(&thermal_tz_ida, tz->id);
1497 ida_destroy(&tz->ida);
1499 device_del(&tz->device);
1503 put_device(&tz->device);
1505 thermal_notify_tz_delete(tz);
1507 wait_for_completion(&tz->removal);
1510 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1513 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1514 * @name: thermal zone name to fetch the temperature
1516 * When only one zone is found with the passed name, returns a reference to it.
1518 * Return: On success returns a reference to an unique thermal zone with
1519 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1520 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1522 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1524 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1525 unsigned int found = 0;
1530 mutex_lock(&thermal_list_lock);
1531 list_for_each_entry(pos, &thermal_tz_list, node)
1532 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1536 mutex_unlock(&thermal_list_lock);
1538 /* nothing has been found, thus an error code for it */
1540 ref = ERR_PTR(-ENODEV);
1542 /* Success only when an unique zone is found */
1543 ref = ERR_PTR(-EEXIST);
1548 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1550 static void thermal_zone_device_resume(struct work_struct *work)
1552 struct thermal_zone_device *tz;
1554 tz = container_of(work, struct thermal_zone_device, poll_queue.work);
1556 mutex_lock(&tz->lock);
1558 tz->suspended = false;
1560 thermal_zone_device_init(tz);
1561 __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1563 mutex_unlock(&tz->lock);
1566 static int thermal_pm_notify(struct notifier_block *nb,
1567 unsigned long mode, void *_unused)
1569 struct thermal_zone_device *tz;
1572 case PM_HIBERNATION_PREPARE:
1573 case PM_RESTORE_PREPARE:
1574 case PM_SUSPEND_PREPARE:
1575 mutex_lock(&thermal_list_lock);
1577 list_for_each_entry(tz, &thermal_tz_list, node) {
1578 mutex_lock(&tz->lock);
1580 tz->suspended = true;
1582 mutex_unlock(&tz->lock);
1585 mutex_unlock(&thermal_list_lock);
1587 case PM_POST_HIBERNATION:
1588 case PM_POST_RESTORE:
1589 case PM_POST_SUSPEND:
1590 mutex_lock(&thermal_list_lock);
1592 list_for_each_entry(tz, &thermal_tz_list, node) {
1593 mutex_lock(&tz->lock);
1595 cancel_delayed_work(&tz->poll_queue);
1598 * Replace the work function with the resume one, which
1599 * will restore the original work function and schedule
1600 * the polling work if needed.
1602 INIT_DELAYED_WORK(&tz->poll_queue,
1603 thermal_zone_device_resume);
1604 /* Queue up the work without a delay. */
1605 mod_delayed_work(system_freezable_power_efficient_wq,
1606 &tz->poll_queue, 0);
1608 mutex_unlock(&tz->lock);
1611 mutex_unlock(&thermal_list_lock);
1619 static struct notifier_block thermal_pm_nb = {
1620 .notifier_call = thermal_pm_notify,
1623 static int __init thermal_init(void)
1627 thermal_debug_init();
1629 result = thermal_netlink_init();
1633 result = thermal_register_governors();
1635 goto unregister_netlink;
1637 thermal_class = kzalloc(sizeof(*thermal_class), GFP_KERNEL);
1638 if (!thermal_class) {
1640 goto unregister_governors;
1643 thermal_class->name = "thermal";
1644 thermal_class->dev_release = thermal_release;
1646 result = class_register(thermal_class);
1648 kfree(thermal_class);
1649 thermal_class = NULL;
1650 goto unregister_governors;
1653 result = register_pm_notifier(&thermal_pm_nb);
1655 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1660 unregister_governors:
1661 thermal_unregister_governors();
1663 thermal_netlink_exit();
1665 mutex_destroy(&thermal_list_lock);
1666 mutex_destroy(&thermal_governor_lock);
1669 postcore_initcall(thermal_init);