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 if (!device_is_registered(&tz->device))
207 gov = __find_governor(strim(policy));
211 ret = thermal_set_governor(tz, gov);
214 mutex_unlock(&tz->lock);
215 mutex_unlock(&thermal_governor_lock);
217 thermal_notify_tz_gov_change(tz->id, policy);
222 int thermal_build_list_of_policies(char *buf)
224 struct thermal_governor *pos;
227 mutex_lock(&thermal_governor_lock);
229 list_for_each_entry(pos, &thermal_governor_list, governor_list) {
230 count += sysfs_emit_at(buf, count, "%s ", pos->name);
232 count += sysfs_emit_at(buf, count, "\n");
234 mutex_unlock(&thermal_governor_lock);
239 static void __init thermal_unregister_governors(void)
241 struct thermal_governor **governor;
243 for_each_governor_table(governor)
244 thermal_unregister_governor(*governor);
247 static int __init thermal_register_governors(void)
250 struct thermal_governor **governor;
252 for_each_governor_table(governor) {
253 ret = thermal_register_governor(*governor);
255 pr_err("Failed to register governor: '%s'",
260 pr_info("Registered thermal governor '%s'",
265 struct thermal_governor **gov;
267 for_each_governor_table(gov) {
270 thermal_unregister_governor(*gov);
278 * Zone update section: main control loop applied to each zone while monitoring
280 * in polling mode. The monitoring is done using a workqueue.
281 * Same update may be done on a zone by calling thermal_zone_device_update().
284 * - Non-critical trips will invoke the governor responsible for that zone;
285 * - Hot trips will produce a notification to userspace;
286 * - Critical trip point will cause a system shutdown.
288 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
292 mod_delayed_work(system_freezable_power_efficient_wq,
293 &tz->poll_queue, delay);
295 cancel_delayed_work(&tz->poll_queue);
298 static void monitor_thermal_zone(struct thermal_zone_device *tz)
300 if (tz->mode != THERMAL_DEVICE_ENABLED)
301 thermal_zone_device_set_polling(tz, 0);
302 else if (tz->passive)
303 thermal_zone_device_set_polling(tz, tz->passive_delay_jiffies);
304 else if (tz->polling_delay_jiffies)
305 thermal_zone_device_set_polling(tz, tz->polling_delay_jiffies);
308 static void handle_non_critical_trips(struct thermal_zone_device *tz,
309 const struct thermal_trip *trip)
311 tz->governor ? tz->governor->throttle(tz, trip) :
312 def_governor->throttle(tz, trip);
315 void thermal_zone_device_critical(struct thermal_zone_device *tz)
318 * poweroff_delay_ms must be a carefully profiled positive value.
319 * Its a must for forced_emergency_poweroff_work to be scheduled.
321 int poweroff_delay_ms = CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS;
323 dev_emerg(&tz->device, "%s: critical temperature reached, "
324 "shutting down\n", tz->type);
326 hw_protection_shutdown("Temperature too high", poweroff_delay_ms);
328 EXPORT_SYMBOL(thermal_zone_device_critical);
330 static void handle_critical_trips(struct thermal_zone_device *tz,
331 const struct thermal_trip *trip)
333 /* If we have not crossed the trip_temp, we do not care. */
334 if (trip->temperature <= 0 || tz->temperature < trip->temperature)
337 trace_thermal_zone_trip(tz, thermal_zone_trip_id(tz, trip), trip->type);
339 if (trip->type == THERMAL_TRIP_CRITICAL)
340 tz->ops->critical(tz);
341 else if (tz->ops->hot)
345 static void handle_thermal_trip(struct thermal_zone_device *tz,
346 const struct thermal_trip *trip)
348 if (trip->temperature == THERMAL_TEMP_INVALID)
351 if (tz->last_temperature != THERMAL_TEMP_INVALID) {
352 if (tz->last_temperature < trip->temperature &&
353 tz->temperature >= trip->temperature)
354 thermal_notify_tz_trip_up(tz->id,
355 thermal_zone_trip_id(tz, trip),
357 if (tz->last_temperature >= trip->temperature &&
358 tz->temperature < trip->temperature - trip->hysteresis)
359 thermal_notify_tz_trip_down(tz->id,
360 thermal_zone_trip_id(tz, trip),
364 if (trip->type == THERMAL_TRIP_CRITICAL || trip->type == THERMAL_TRIP_HOT)
365 handle_critical_trips(tz, trip);
367 handle_non_critical_trips(tz, trip);
370 static void update_temperature(struct thermal_zone_device *tz)
374 ret = __thermal_zone_get_temp(tz, &temp);
377 dev_warn(&tz->device,
378 "failed to read out thermal zone (%d)\n",
383 tz->last_temperature = tz->temperature;
384 tz->temperature = temp;
386 trace_thermal_temperature(tz);
388 thermal_genl_sampling_temp(tz->id, temp);
391 static void thermal_zone_device_init(struct thermal_zone_device *tz)
393 struct thermal_instance *pos;
394 tz->temperature = THERMAL_TEMP_INVALID;
395 tz->prev_low_trip = -INT_MAX;
396 tz->prev_high_trip = INT_MAX;
397 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
398 pos->initialized = false;
401 void __thermal_zone_device_update(struct thermal_zone_device *tz,
402 enum thermal_notify_event event)
404 const struct thermal_trip *trip;
409 if (WARN_ONCE(!tz->ops->get_temp,
410 "'%s' must not be called without 'get_temp' ops set\n",
414 if (!thermal_zone_device_is_enabled(tz))
417 update_temperature(tz);
419 __thermal_zone_set_trips(tz);
421 tz->notify_event = event;
423 for_each_trip(tz, trip)
424 handle_thermal_trip(tz, trip);
426 monitor_thermal_zone(tz);
429 static int thermal_zone_device_set_mode(struct thermal_zone_device *tz,
430 enum thermal_device_mode mode)
434 mutex_lock(&tz->lock);
436 /* do nothing if mode isn't changing */
437 if (mode == tz->mode) {
438 mutex_unlock(&tz->lock);
443 if (!device_is_registered(&tz->device)) {
444 mutex_unlock(&tz->lock);
449 if (tz->ops->change_mode)
450 ret = tz->ops->change_mode(tz, mode);
455 __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
457 mutex_unlock(&tz->lock);
459 if (mode == THERMAL_DEVICE_ENABLED)
460 thermal_notify_tz_enable(tz->id);
462 thermal_notify_tz_disable(tz->id);
467 int thermal_zone_device_enable(struct thermal_zone_device *tz)
469 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_ENABLED);
471 EXPORT_SYMBOL_GPL(thermal_zone_device_enable);
473 int thermal_zone_device_disable(struct thermal_zone_device *tz)
475 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED);
477 EXPORT_SYMBOL_GPL(thermal_zone_device_disable);
479 int thermal_zone_device_is_enabled(struct thermal_zone_device *tz)
481 lockdep_assert_held(&tz->lock);
483 return tz->mode == THERMAL_DEVICE_ENABLED;
486 void thermal_zone_device_update(struct thermal_zone_device *tz,
487 enum thermal_notify_event event)
489 mutex_lock(&tz->lock);
490 if (device_is_registered(&tz->device))
491 __thermal_zone_device_update(tz, event);
492 mutex_unlock(&tz->lock);
494 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
496 static void thermal_zone_device_check(struct work_struct *work)
498 struct thermal_zone_device *tz = container_of(work, struct
501 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
504 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
507 struct thermal_governor *gov;
510 mutex_lock(&thermal_governor_lock);
511 list_for_each_entry(gov, &thermal_governor_list, governor_list) {
516 mutex_unlock(&thermal_governor_lock);
521 int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
524 struct thermal_cooling_device *cdev;
527 mutex_lock(&thermal_list_lock);
528 list_for_each_entry(cdev, &thermal_cdev_list, node) {
529 ret = cb(cdev, data);
533 mutex_unlock(&thermal_list_lock);
538 int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
541 struct thermal_zone_device *tz;
544 mutex_lock(&thermal_list_lock);
545 list_for_each_entry(tz, &thermal_tz_list, node) {
550 mutex_unlock(&thermal_list_lock);
555 struct thermal_zone_device *thermal_zone_get_by_id(int id)
557 struct thermal_zone_device *tz, *match = NULL;
559 mutex_lock(&thermal_list_lock);
560 list_for_each_entry(tz, &thermal_tz_list, node) {
566 mutex_unlock(&thermal_list_lock);
572 * Device management section: cooling devices, zones devices, and binding
574 * Set of functions provided by the thermal core for:
575 * - cooling devices lifecycle: registration, unregistration,
576 * binding, and unbinding.
577 * - thermal zone devices lifecycle: registration, unregistration,
578 * binding, and unbinding.
582 * thermal_bind_cdev_to_trip - bind a cooling device to a thermal zone
583 * @tz: pointer to struct thermal_zone_device
584 * @trip: trip point the cooling devices is associated with in this zone.
585 * @cdev: pointer to struct thermal_cooling_device
586 * @upper: the Maximum cooling state for this trip point.
587 * THERMAL_NO_LIMIT means no upper limit,
588 * and the cooling device can be in max_state.
589 * @lower: the Minimum cooling state can be used for this trip point.
590 * THERMAL_NO_LIMIT means no lower limit,
591 * and the cooling device can be in cooling state 0.
592 * @weight: The weight of the cooling device to be bound to the
593 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
596 * This interface function bind a thermal cooling device to the certain trip
597 * point of a thermal zone device.
598 * This function is usually called in the thermal zone device .bind callback.
600 * Return: 0 on success, the proper error value otherwise.
602 int thermal_bind_cdev_to_trip(struct thermal_zone_device *tz,
603 const struct thermal_trip *trip,
604 struct thermal_cooling_device *cdev,
605 unsigned long upper, unsigned long lower,
608 struct thermal_instance *dev;
609 struct thermal_instance *pos;
610 struct thermal_zone_device *pos1;
611 struct thermal_cooling_device *pos2;
615 list_for_each_entry(pos1, &thermal_tz_list, node) {
619 list_for_each_entry(pos2, &thermal_cdev_list, node) {
624 if (tz != pos1 || cdev != pos2)
627 /* lower default 0, upper default max_state */
628 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
630 if (upper == THERMAL_NO_LIMIT) {
631 upper = cdev->max_state;
632 upper_no_limit = true;
634 upper_no_limit = false;
637 if (lower > upper || upper > cdev->max_state)
640 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
647 dev->upper_no_limit = upper_no_limit;
649 dev->target = THERMAL_NO_TARGET;
650 dev->weight = weight;
652 result = ida_alloc(&tz->ida, GFP_KERNEL);
657 sprintf(dev->name, "cdev%d", dev->id);
659 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
663 snprintf(dev->attr_name, sizeof(dev->attr_name), "cdev%d_trip_point",
665 sysfs_attr_init(&dev->attr.attr);
666 dev->attr.attr.name = dev->attr_name;
667 dev->attr.attr.mode = 0444;
668 dev->attr.show = trip_point_show;
669 result = device_create_file(&tz->device, &dev->attr);
671 goto remove_symbol_link;
673 snprintf(dev->weight_attr_name, sizeof(dev->weight_attr_name),
674 "cdev%d_weight", dev->id);
675 sysfs_attr_init(&dev->weight_attr.attr);
676 dev->weight_attr.attr.name = dev->weight_attr_name;
677 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
678 dev->weight_attr.show = weight_show;
679 dev->weight_attr.store = weight_store;
680 result = device_create_file(&tz->device, &dev->weight_attr);
682 goto remove_trip_file;
684 mutex_lock(&tz->lock);
685 mutex_lock(&cdev->lock);
686 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
687 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
692 list_add_tail(&dev->tz_node, &tz->thermal_instances);
693 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
694 atomic_set(&tz->need_update, 1);
696 mutex_unlock(&cdev->lock);
697 mutex_unlock(&tz->lock);
702 device_remove_file(&tz->device, &dev->weight_attr);
704 device_remove_file(&tz->device, &dev->attr);
706 sysfs_remove_link(&tz->device.kobj, dev->name);
708 ida_free(&tz->ida, dev->id);
713 EXPORT_SYMBOL_GPL(thermal_bind_cdev_to_trip);
715 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
717 struct thermal_cooling_device *cdev,
718 unsigned long upper, unsigned long lower,
721 if (trip_index < 0 || trip_index >= tz->num_trips)
724 return thermal_bind_cdev_to_trip(tz, &tz->trips[trip_index], cdev,
725 upper, lower, weight);
727 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
730 * thermal_unbind_cdev_from_trip - unbind a cooling device from a thermal zone.
731 * @tz: pointer to a struct thermal_zone_device.
732 * @trip: trip point the cooling devices is associated with in this zone.
733 * @cdev: pointer to a struct thermal_cooling_device.
735 * This interface function unbind a thermal cooling device from the certain
736 * trip point of a thermal zone device.
737 * This function is usually called in the thermal zone device .unbind callback.
739 * Return: 0 on success, the proper error value otherwise.
741 int thermal_unbind_cdev_from_trip(struct thermal_zone_device *tz,
742 const struct thermal_trip *trip,
743 struct thermal_cooling_device *cdev)
745 struct thermal_instance *pos, *next;
747 mutex_lock(&tz->lock);
748 mutex_lock(&cdev->lock);
749 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
750 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
751 list_del(&pos->tz_node);
752 list_del(&pos->cdev_node);
753 mutex_unlock(&cdev->lock);
754 mutex_unlock(&tz->lock);
758 mutex_unlock(&cdev->lock);
759 mutex_unlock(&tz->lock);
764 device_remove_file(&tz->device, &pos->weight_attr);
765 device_remove_file(&tz->device, &pos->attr);
766 sysfs_remove_link(&tz->device.kobj, pos->name);
767 ida_free(&tz->ida, pos->id);
771 EXPORT_SYMBOL_GPL(thermal_unbind_cdev_from_trip);
773 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
775 struct thermal_cooling_device *cdev)
777 if (trip_index < 0 || trip_index >= tz->num_trips)
780 return thermal_unbind_cdev_from_trip(tz, &tz->trips[trip_index], cdev);
782 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
784 static void thermal_release(struct device *dev)
786 struct thermal_zone_device *tz;
787 struct thermal_cooling_device *cdev;
789 if (!strncmp(dev_name(dev), "thermal_zone",
790 sizeof("thermal_zone") - 1)) {
791 tz = to_thermal_zone(dev);
792 thermal_zone_destroy_device_groups(tz);
793 mutex_destroy(&tz->lock);
795 } else if (!strncmp(dev_name(dev), "cooling_device",
796 sizeof("cooling_device") - 1)) {
797 cdev = to_cooling_device(dev);
798 thermal_cooling_device_destroy_sysfs(cdev);
800 ida_free(&thermal_cdev_ida, cdev->id);
805 static struct class *thermal_class;
808 void print_bind_err_msg(struct thermal_zone_device *tz,
809 struct thermal_cooling_device *cdev, int ret)
811 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
812 tz->type, cdev->type, ret);
815 static void bind_cdev(struct thermal_cooling_device *cdev)
818 struct thermal_zone_device *pos = NULL;
820 list_for_each_entry(pos, &thermal_tz_list, node) {
821 if (pos->ops->bind) {
822 ret = pos->ops->bind(pos, cdev);
824 print_bind_err_msg(pos, cdev, ret);
830 * __thermal_cooling_device_register() - register a new thermal cooling device
831 * @np: a pointer to a device tree node.
832 * @type: the thermal cooling device type.
833 * @devdata: device private data.
834 * @ops: standard thermal cooling devices callbacks.
836 * This interface function adds a new thermal cooling device (fan/processor/...)
837 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
838 * to all the thermal zone devices registered at the same time.
839 * It also gives the opportunity to link the cooling device to a device tree
840 * node, so that it can be bound to a thermal zone created out of device tree.
842 * Return: a pointer to the created struct thermal_cooling_device or an
843 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
845 static struct thermal_cooling_device *
846 __thermal_cooling_device_register(struct device_node *np,
847 const char *type, void *devdata,
848 const struct thermal_cooling_device_ops *ops)
850 struct thermal_cooling_device *cdev;
851 struct thermal_zone_device *pos = NULL;
854 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
856 return ERR_PTR(-EINVAL);
859 return ERR_PTR(-ENODEV);
861 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
863 return ERR_PTR(-ENOMEM);
865 ret = ida_alloc(&thermal_cdev_ida, GFP_KERNEL);
871 cdev->type = kstrdup(type ? type : "", GFP_KERNEL);
877 mutex_init(&cdev->lock);
878 INIT_LIST_HEAD(&cdev->thermal_instances);
881 cdev->updated = false;
882 cdev->device.class = thermal_class;
883 cdev->devdata = devdata;
885 ret = cdev->ops->get_max_state(cdev, &cdev->max_state);
889 thermal_cooling_device_setup_sysfs(cdev);
891 ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
893 goto out_cooling_dev;
895 ret = device_register(&cdev->device);
897 /* thermal_release() handles rest of the cleanup */
898 put_device(&cdev->device);
902 /* Add 'this' new cdev to the global cdev list */
903 mutex_lock(&thermal_list_lock);
905 list_add(&cdev->node, &thermal_cdev_list);
907 /* Update binding information for 'this' new cdev */
910 list_for_each_entry(pos, &thermal_tz_list, node)
911 if (atomic_cmpxchg(&pos->need_update, 1, 0))
912 thermal_zone_device_update(pos,
913 THERMAL_EVENT_UNSPECIFIED);
915 mutex_unlock(&thermal_list_lock);
920 thermal_cooling_device_destroy_sysfs(cdev);
924 ida_free(&thermal_cdev_ida, id);
931 * thermal_cooling_device_register() - register a new thermal cooling device
932 * @type: the thermal cooling device type.
933 * @devdata: device private data.
934 * @ops: standard thermal cooling devices callbacks.
936 * This interface function adds a new thermal cooling device (fan/processor/...)
937 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
938 * to all the thermal zone devices registered at the same time.
940 * Return: a pointer to the created struct thermal_cooling_device or an
941 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
943 struct thermal_cooling_device *
944 thermal_cooling_device_register(const char *type, void *devdata,
945 const struct thermal_cooling_device_ops *ops)
947 return __thermal_cooling_device_register(NULL, type, devdata, ops);
949 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
952 * thermal_of_cooling_device_register() - register an OF thermal cooling device
953 * @np: a pointer to a device tree node.
954 * @type: the thermal cooling device type.
955 * @devdata: device private data.
956 * @ops: standard thermal cooling devices callbacks.
958 * This function will register a cooling device with device tree node reference.
959 * This interface function adds a new thermal cooling device (fan/processor/...)
960 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
961 * to all the thermal zone devices registered at the same time.
963 * Return: a pointer to the created struct thermal_cooling_device or an
964 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
966 struct thermal_cooling_device *
967 thermal_of_cooling_device_register(struct device_node *np,
968 const char *type, void *devdata,
969 const struct thermal_cooling_device_ops *ops)
971 return __thermal_cooling_device_register(np, type, devdata, ops);
973 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
975 static void thermal_cooling_device_release(struct device *dev, void *res)
977 thermal_cooling_device_unregister(
978 *(struct thermal_cooling_device **)res);
982 * devm_thermal_of_cooling_device_register() - register an OF thermal cooling
984 * @dev: a valid struct device pointer of a sensor 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 devm_thermal_of_cooling_device_register(struct device *dev,
1000 struct device_node *np,
1001 char *type, void *devdata,
1002 const struct thermal_cooling_device_ops *ops)
1004 struct thermal_cooling_device **ptr, *tcd;
1006 ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr),
1009 return ERR_PTR(-ENOMEM);
1011 tcd = __thermal_cooling_device_register(np, type, devdata, ops);
1018 devres_add(dev, ptr);
1022 EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register);
1024 static bool thermal_cooling_device_present(struct thermal_cooling_device *cdev)
1026 struct thermal_cooling_device *pos = NULL;
1028 list_for_each_entry(pos, &thermal_cdev_list, node) {
1037 * thermal_cooling_device_update - Update a cooling device object
1038 * @cdev: Target cooling device.
1040 * Update @cdev to reflect a change of the underlying hardware or platform.
1042 * Must be called when the maximum cooling state of @cdev becomes invalid and so
1043 * its .get_max_state() callback needs to be run to produce the new maximum
1044 * cooling state value.
1046 void thermal_cooling_device_update(struct thermal_cooling_device *cdev)
1048 struct thermal_instance *ti;
1049 unsigned long state;
1051 if (IS_ERR_OR_NULL(cdev))
1055 * Hold thermal_list_lock throughout the update to prevent the device
1056 * from going away while being updated.
1058 mutex_lock(&thermal_list_lock);
1060 if (!thermal_cooling_device_present(cdev))
1064 * Update under the cdev lock to prevent the state from being set beyond
1065 * the new limit concurrently.
1067 mutex_lock(&cdev->lock);
1069 if (cdev->ops->get_max_state(cdev, &cdev->max_state))
1072 thermal_cooling_device_stats_reinit(cdev);
1074 list_for_each_entry(ti, &cdev->thermal_instances, cdev_node) {
1075 if (ti->upper == cdev->max_state)
1078 if (ti->upper < cdev->max_state) {
1079 if (ti->upper_no_limit)
1080 ti->upper = cdev->max_state;
1085 ti->upper = cdev->max_state;
1086 if (ti->lower > ti->upper)
1087 ti->lower = ti->upper;
1089 if (ti->target == THERMAL_NO_TARGET)
1092 if (ti->target > ti->upper)
1093 ti->target = ti->upper;
1096 if (cdev->ops->get_cur_state(cdev, &state) || state > cdev->max_state)
1099 thermal_cooling_device_stats_update(cdev, state);
1102 mutex_unlock(&cdev->lock);
1105 mutex_unlock(&thermal_list_lock);
1107 EXPORT_SYMBOL_GPL(thermal_cooling_device_update);
1110 * thermal_cooling_device_unregister - removes a thermal cooling device
1111 * @cdev: the thermal cooling device to remove.
1113 * thermal_cooling_device_unregister() must be called when a registered
1114 * thermal cooling device is no longer needed.
1116 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1118 struct thermal_zone_device *tz;
1123 mutex_lock(&thermal_list_lock);
1125 if (!thermal_cooling_device_present(cdev)) {
1126 mutex_unlock(&thermal_list_lock);
1130 list_del(&cdev->node);
1132 /* Unbind all thermal zones associated with 'this' cdev */
1133 list_for_each_entry(tz, &thermal_tz_list, node) {
1134 if (tz->ops->unbind)
1135 tz->ops->unbind(tz, cdev);
1138 mutex_unlock(&thermal_list_lock);
1140 device_unregister(&cdev->device);
1142 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1144 static void bind_tz(struct thermal_zone_device *tz)
1147 struct thermal_cooling_device *pos = NULL;
1152 mutex_lock(&thermal_list_lock);
1154 list_for_each_entry(pos, &thermal_cdev_list, node) {
1155 ret = tz->ops->bind(tz, pos);
1157 print_bind_err_msg(tz, pos, ret);
1160 mutex_unlock(&thermal_list_lock);
1163 static void thermal_set_delay_jiffies(unsigned long *delay_jiffies, int delay_ms)
1165 *delay_jiffies = msecs_to_jiffies(delay_ms);
1166 if (delay_ms > 1000)
1167 *delay_jiffies = round_jiffies(*delay_jiffies);
1170 int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp)
1172 int i, ret = -EINVAL;
1174 if (tz->ops->get_crit_temp)
1175 return tz->ops->get_crit_temp(tz, temp);
1180 mutex_lock(&tz->lock);
1182 for (i = 0; i < tz->num_trips; i++) {
1183 if (tz->trips[i].type == THERMAL_TRIP_CRITICAL) {
1184 *temp = tz->trips[i].temperature;
1190 mutex_unlock(&tz->lock);
1194 EXPORT_SYMBOL_GPL(thermal_zone_get_crit_temp);
1197 * thermal_zone_device_register_with_trips() - register a new thermal zone device
1198 * @type: the thermal zone device type
1199 * @trips: a pointer to an array of thermal trips
1200 * @num_trips: the number of trip points the thermal zone support
1201 * @mask: a bit string indicating the writeablility of trip points
1202 * @devdata: private device data
1203 * @ops: standard thermal zone device callbacks
1204 * @tzp: thermal zone platform parameters
1205 * @passive_delay: number of milliseconds to wait between polls when
1206 * performing passive cooling
1207 * @polling_delay: number of milliseconds to wait between polls when checking
1208 * whether trip points have been crossed (0 for interrupt
1211 * This interface function adds a new thermal zone device (sensor) to
1212 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1213 * thermal cooling devices registered at the same time.
1214 * thermal_zone_device_unregister() must be called when the device is no
1215 * longer needed. The passive cooling depends on the .get_trend() return value.
1217 * Return: a pointer to the created struct thermal_zone_device or an
1218 * in case of error, an ERR_PTR. Caller must check return value with
1219 * IS_ERR*() helpers.
1221 struct thermal_zone_device *
1222 thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *trips, int num_trips, int mask,
1223 void *devdata, struct thermal_zone_device_ops *ops,
1224 const struct thermal_zone_params *tzp, int passive_delay,
1227 struct thermal_zone_device *tz;
1230 struct thermal_governor *governor;
1232 if (!type || strlen(type) == 0) {
1233 pr_err("No thermal zone type defined\n");
1234 return ERR_PTR(-EINVAL);
1237 if (strlen(type) >= THERMAL_NAME_LENGTH) {
1238 pr_err("Thermal zone name (%s) too long, should be under %d chars\n",
1239 type, THERMAL_NAME_LENGTH);
1240 return ERR_PTR(-EINVAL);
1244 * Max trip count can't exceed 31 as the "mask >> num_trips" condition.
1245 * For example, shifting by 32 will result in compiler warning:
1246 * warning: right shift count >= width of type [-Wshift-count- overflow]
1248 * Also "mask >> num_trips" will always be true with 32 bit shift.
1249 * E.g. mask = 0x80000000 for trip id 31 to be RW. Then
1250 * mask >> 32 = 0x80000000
1251 * This will result in failure for the below condition.
1253 * Check will be true when the bit 31 of the mask is set.
1254 * 32 bit shift will cause overflow of 4 byte integer.
1256 if (num_trips > (BITS_PER_TYPE(int) - 1) || num_trips < 0 || mask >> num_trips) {
1257 pr_err("Incorrect number of thermal trips\n");
1258 return ERR_PTR(-EINVAL);
1262 pr_err("Thermal zone device ops not defined\n");
1263 return ERR_PTR(-EINVAL);
1266 if (num_trips > 0 && !trips)
1267 return ERR_PTR(-EINVAL);
1270 return ERR_PTR(-ENODEV);
1272 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
1274 return ERR_PTR(-ENOMEM);
1277 tz->tzp = kmemdup(tzp, sizeof(*tzp), GFP_KERNEL);
1284 INIT_LIST_HEAD(&tz->thermal_instances);
1286 mutex_init(&tz->lock);
1287 id = ida_alloc(&thermal_tz_ida, GFP_KERNEL);
1294 strscpy(tz->type, type, sizeof(tz->type));
1297 ops->critical = thermal_zone_device_critical;
1300 tz->device.class = thermal_class;
1301 tz->devdata = devdata;
1303 tz->num_trips = num_trips;
1305 thermal_set_delay_jiffies(&tz->passive_delay_jiffies, passive_delay);
1306 thermal_set_delay_jiffies(&tz->polling_delay_jiffies, polling_delay);
1309 /* Add nodes that are always present via .groups */
1310 result = thermal_zone_create_device_groups(tz, mask);
1314 /* A new thermal zone needs to be updated anyway. */
1315 atomic_set(&tz->need_update, 1);
1317 result = dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1319 thermal_zone_destroy_device_groups(tz);
1322 result = device_register(&tz->device);
1324 goto release_device;
1326 /* Update 'this' zone's governor information */
1327 mutex_lock(&thermal_governor_lock);
1330 governor = __find_governor(tz->tzp->governor_name);
1332 governor = def_governor;
1334 result = thermal_set_governor(tz, governor);
1336 mutex_unlock(&thermal_governor_lock);
1340 mutex_unlock(&thermal_governor_lock);
1342 if (!tz->tzp || !tz->tzp->no_hwmon) {
1343 result = thermal_add_hwmon_sysfs(tz);
1348 mutex_lock(&thermal_list_lock);
1349 list_add_tail(&tz->node, &thermal_tz_list);
1350 mutex_unlock(&thermal_list_lock);
1352 /* Bind cooling devices for this zone */
1355 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
1357 thermal_zone_device_init(tz);
1358 /* Update the new thermal zone and mark it as already updated. */
1359 if (atomic_cmpxchg(&tz->need_update, 1, 0))
1360 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1362 thermal_notify_tz_create(tz->id, tz->type);
1367 device_del(&tz->device);
1369 put_device(&tz->device);
1371 ida_free(&thermal_tz_ida, id);
1376 return ERR_PTR(result);
1378 EXPORT_SYMBOL_GPL(thermal_zone_device_register_with_trips);
1380 struct thermal_zone_device *thermal_tripless_zone_device_register(
1383 struct thermal_zone_device_ops *ops,
1384 const struct thermal_zone_params *tzp)
1386 return thermal_zone_device_register_with_trips(type, NULL, 0, 0, devdata,
1389 EXPORT_SYMBOL_GPL(thermal_tripless_zone_device_register);
1391 void *thermal_zone_device_priv(struct thermal_zone_device *tzd)
1393 return tzd->devdata;
1395 EXPORT_SYMBOL_GPL(thermal_zone_device_priv);
1397 const char *thermal_zone_device_type(struct thermal_zone_device *tzd)
1401 EXPORT_SYMBOL_GPL(thermal_zone_device_type);
1403 int thermal_zone_device_id(struct thermal_zone_device *tzd)
1407 EXPORT_SYMBOL_GPL(thermal_zone_device_id);
1409 struct device *thermal_zone_device(struct thermal_zone_device *tzd)
1411 return &tzd->device;
1413 EXPORT_SYMBOL_GPL(thermal_zone_device);
1416 * thermal_zone_device_unregister - removes the registered thermal zone device
1417 * @tz: the thermal zone device to remove
1419 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1422 struct thermal_cooling_device *cdev;
1423 struct thermal_zone_device *pos = NULL;
1430 mutex_lock(&thermal_list_lock);
1431 list_for_each_entry(pos, &thermal_tz_list, node)
1435 /* thermal zone device not found */
1436 mutex_unlock(&thermal_list_lock);
1439 list_del(&tz->node);
1441 /* Unbind all cdevs associated with 'this' thermal zone */
1442 list_for_each_entry(cdev, &thermal_cdev_list, node)
1443 if (tz->ops->unbind)
1444 tz->ops->unbind(tz, cdev);
1446 mutex_unlock(&thermal_list_lock);
1448 cancel_delayed_work_sync(&tz->poll_queue);
1450 thermal_set_governor(tz, NULL);
1452 thermal_remove_hwmon_sysfs(tz);
1453 ida_free(&thermal_tz_ida, tz->id);
1454 ida_destroy(&tz->ida);
1456 mutex_lock(&tz->lock);
1457 device_del(&tz->device);
1458 mutex_unlock(&tz->lock);
1462 put_device(&tz->device);
1464 thermal_notify_tz_delete(tz_id);
1466 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1469 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1470 * @name: thermal zone name to fetch the temperature
1472 * When only one zone is found with the passed name, returns a reference to it.
1474 * Return: On success returns a reference to an unique thermal zone with
1475 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1476 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1478 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1480 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1481 unsigned int found = 0;
1486 mutex_lock(&thermal_list_lock);
1487 list_for_each_entry(pos, &thermal_tz_list, node)
1488 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1492 mutex_unlock(&thermal_list_lock);
1494 /* nothing has been found, thus an error code for it */
1496 ref = ERR_PTR(-ENODEV);
1498 /* Success only when an unique zone is found */
1499 ref = ERR_PTR(-EEXIST);
1504 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1506 static int thermal_pm_notify(struct notifier_block *nb,
1507 unsigned long mode, void *_unused)
1509 struct thermal_zone_device *tz;
1512 case PM_HIBERNATION_PREPARE:
1513 case PM_RESTORE_PREPARE:
1514 case PM_SUSPEND_PREPARE:
1515 mutex_lock(&thermal_list_lock);
1517 list_for_each_entry(tz, &thermal_tz_list, node) {
1518 mutex_lock(&tz->lock);
1520 tz->suspended = true;
1522 mutex_unlock(&tz->lock);
1525 mutex_unlock(&thermal_list_lock);
1527 case PM_POST_HIBERNATION:
1528 case PM_POST_RESTORE:
1529 case PM_POST_SUSPEND:
1530 mutex_lock(&thermal_list_lock);
1532 list_for_each_entry(tz, &thermal_tz_list, node) {
1533 mutex_lock(&tz->lock);
1535 tz->suspended = false;
1537 thermal_zone_device_init(tz);
1538 __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1540 mutex_unlock(&tz->lock);
1543 mutex_unlock(&thermal_list_lock);
1551 static struct notifier_block thermal_pm_nb = {
1552 .notifier_call = thermal_pm_notify,
1555 static int __init thermal_init(void)
1559 result = thermal_netlink_init();
1563 result = thermal_register_governors();
1565 goto unregister_netlink;
1567 thermal_class = kzalloc(sizeof(*thermal_class), GFP_KERNEL);
1568 if (!thermal_class) {
1570 goto unregister_governors;
1573 thermal_class->name = "thermal";
1574 thermal_class->dev_release = thermal_release;
1576 result = class_register(thermal_class);
1578 kfree(thermal_class);
1579 thermal_class = NULL;
1580 goto unregister_governors;
1583 result = register_pm_notifier(&thermal_pm_nb);
1585 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1590 unregister_governors:
1591 thermal_unregister_governors();
1593 thermal_netlink_exit();
1595 mutex_destroy(&thermal_list_lock);
1596 mutex_destroy(&thermal_governor_lock);
1599 postcore_initcall(thermal_init);