1 // SPDX-License-Identifier: GPL-2.0
3 * thermal.c - Generic Thermal Management Sysfs support.
5 * Copyright (C) 2008 Intel Corp
6 * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
7 * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/export.h>
15 #include <linux/slab.h>
16 #include <linux/kdev_t.h>
17 #include <linux/idr.h>
18 #include <linux/thermal.h>
19 #include <linux/reboot.h>
20 #include <linux/string.h>
22 #include <linux/suspend.h>
24 #define CREATE_TRACE_POINTS
25 #include <trace/events/thermal.h>
27 #include "thermal_core.h"
28 #include "thermal_hwmon.h"
30 static DEFINE_IDA(thermal_tz_ida);
31 static DEFINE_IDA(thermal_cdev_ida);
33 static LIST_HEAD(thermal_tz_list);
34 static LIST_HEAD(thermal_cdev_list);
35 static LIST_HEAD(thermal_governor_list);
37 static DEFINE_MUTEX(thermal_list_lock);
38 static DEFINE_MUTEX(thermal_governor_lock);
40 static atomic_t in_suspend;
42 static struct thermal_governor *def_governor;
45 * Governor section: set of functions to handle thermal governors
47 * Functions to help in the life cycle of thermal governors within
48 * the thermal core and by the thermal governor code.
51 static struct thermal_governor *__find_governor(const char *name)
53 struct thermal_governor *pos;
55 if (!name || !name[0])
58 list_for_each_entry(pos, &thermal_governor_list, governor_list)
59 if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH))
66 * bind_previous_governor() - bind the previous governor of the thermal zone
67 * @tz: a valid pointer to a struct thermal_zone_device
68 * @failed_gov_name: the name of the governor that failed to register
70 * Register the previous governor of the thermal zone after a new
71 * governor has failed to be bound.
73 static void bind_previous_governor(struct thermal_zone_device *tz,
74 const char *failed_gov_name)
76 if (tz->governor && tz->governor->bind_to_tz) {
77 if (tz->governor->bind_to_tz(tz)) {
79 "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n",
80 failed_gov_name, tz->governor->name, tz->type);
87 * thermal_set_governor() - Switch to another governor
88 * @tz: a valid pointer to a struct thermal_zone_device
89 * @new_gov: pointer to the new governor
91 * Change the governor of thermal zone @tz.
93 * Return: 0 on success, an error if the new governor's bind_to_tz() failed.
95 static int thermal_set_governor(struct thermal_zone_device *tz,
96 struct thermal_governor *new_gov)
100 if (tz->governor && tz->governor->unbind_from_tz)
101 tz->governor->unbind_from_tz(tz);
103 if (new_gov && new_gov->bind_to_tz) {
104 ret = new_gov->bind_to_tz(tz);
106 bind_previous_governor(tz, new_gov->name);
112 tz->governor = new_gov;
117 int thermal_register_governor(struct thermal_governor *governor)
121 struct thermal_zone_device *pos;
126 mutex_lock(&thermal_governor_lock);
129 if (!__find_governor(governor->name)) {
133 list_add(&governor->governor_list, &thermal_governor_list);
134 match_default = !strncmp(governor->name,
135 DEFAULT_THERMAL_GOVERNOR,
136 THERMAL_NAME_LENGTH);
138 if (!def_governor && match_default)
139 def_governor = governor;
142 mutex_lock(&thermal_list_lock);
144 list_for_each_entry(pos, &thermal_tz_list, node) {
146 * only thermal zones with specified tz->tzp->governor_name
147 * may run with tz->govenor unset
152 name = pos->tzp->governor_name;
154 if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) {
157 ret = thermal_set_governor(pos, governor);
159 dev_err(&pos->device,
160 "Failed to set governor %s for thermal zone %s: %d\n",
161 governor->name, pos->type, ret);
165 mutex_unlock(&thermal_list_lock);
166 mutex_unlock(&thermal_governor_lock);
171 void thermal_unregister_governor(struct thermal_governor *governor)
173 struct thermal_zone_device *pos;
178 mutex_lock(&thermal_governor_lock);
180 if (!__find_governor(governor->name))
183 mutex_lock(&thermal_list_lock);
185 list_for_each_entry(pos, &thermal_tz_list, node) {
186 if (!strncasecmp(pos->governor->name, governor->name,
187 THERMAL_NAME_LENGTH))
188 thermal_set_governor(pos, NULL);
191 mutex_unlock(&thermal_list_lock);
192 list_del(&governor->governor_list);
194 mutex_unlock(&thermal_governor_lock);
197 int thermal_zone_device_set_policy(struct thermal_zone_device *tz,
200 struct thermal_governor *gov;
203 mutex_lock(&thermal_governor_lock);
204 mutex_lock(&tz->lock);
206 gov = __find_governor(strim(policy));
210 ret = thermal_set_governor(tz, gov);
213 mutex_unlock(&tz->lock);
214 mutex_unlock(&thermal_governor_lock);
216 thermal_notify_tz_gov_change(tz->id, policy);
221 int thermal_build_list_of_policies(char *buf)
223 struct thermal_governor *pos;
225 ssize_t size = PAGE_SIZE;
227 mutex_lock(&thermal_governor_lock);
229 list_for_each_entry(pos, &thermal_governor_list, governor_list) {
230 size = PAGE_SIZE - count;
231 count += scnprintf(buf + count, size, "%s ", pos->name);
233 count += scnprintf(buf + count, size, "\n");
235 mutex_unlock(&thermal_governor_lock);
240 static void __init thermal_unregister_governors(void)
242 struct thermal_governor **governor;
244 for_each_governor_table(governor)
245 thermal_unregister_governor(*governor);
248 static int __init thermal_register_governors(void)
251 struct thermal_governor **governor;
253 for_each_governor_table(governor) {
254 ret = thermal_register_governor(*governor);
256 pr_err("Failed to register governor: '%s'",
261 pr_info("Registered thermal governor '%s'",
266 struct thermal_governor **gov;
268 for_each_governor_table(gov) {
271 thermal_unregister_governor(*gov);
279 * Zone update section: main control loop applied to each zone while monitoring
281 * in polling mode. The monitoring is done using a workqueue.
282 * Same update may be done on a zone by calling thermal_zone_device_update().
285 * - Non-critical trips will invoke the governor responsible for that zone;
286 * - Hot trips will produce a notification to userspace;
287 * - Critical trip point will cause a system shutdown.
289 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
293 mod_delayed_work(system_freezable_power_efficient_wq,
294 &tz->poll_queue, delay);
296 cancel_delayed_work(&tz->poll_queue);
299 static inline bool should_stop_polling(struct thermal_zone_device *tz)
301 return !thermal_zone_device_is_enabled(tz);
304 static void monitor_thermal_zone(struct thermal_zone_device *tz)
308 stop = should_stop_polling(tz);
310 mutex_lock(&tz->lock);
312 if (!stop && tz->passive)
313 thermal_zone_device_set_polling(tz, tz->passive_delay_jiffies);
314 else if (!stop && tz->polling_delay_jiffies)
315 thermal_zone_device_set_polling(tz, tz->polling_delay_jiffies);
317 thermal_zone_device_set_polling(tz, 0);
319 mutex_unlock(&tz->lock);
322 static void handle_non_critical_trips(struct thermal_zone_device *tz, int trip)
324 tz->governor ? tz->governor->throttle(tz, trip) :
325 def_governor->throttle(tz, trip);
328 void thermal_zone_device_critical(struct thermal_zone_device *tz)
331 * poweroff_delay_ms must be a carefully profiled positive value.
332 * Its a must for forced_emergency_poweroff_work to be scheduled.
334 int poweroff_delay_ms = CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS;
336 dev_emerg(&tz->device, "%s: critical temperature reached, "
337 "shutting down\n", tz->type);
339 hw_protection_shutdown("Temperature too high", poweroff_delay_ms);
341 EXPORT_SYMBOL(thermal_zone_device_critical);
343 static void handle_critical_trips(struct thermal_zone_device *tz,
344 int trip, enum thermal_trip_type trip_type)
348 tz->ops->get_trip_temp(tz, trip, &trip_temp);
350 /* If we have not crossed the trip_temp, we do not care. */
351 if (trip_temp <= 0 || tz->temperature < trip_temp)
354 trace_thermal_zone_trip(tz, trip, trip_type);
356 if (trip_type == THERMAL_TRIP_HOT && tz->ops->hot)
358 else if (trip_type == THERMAL_TRIP_CRITICAL)
359 tz->ops->critical(tz);
362 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
364 enum thermal_trip_type type;
365 int trip_temp, hyst = 0;
367 /* Ignore disabled trip points */
368 if (test_bit(trip, &tz->trips_disabled))
371 tz->ops->get_trip_temp(tz, trip, &trip_temp);
372 tz->ops->get_trip_type(tz, trip, &type);
373 if (tz->ops->get_trip_hyst)
374 tz->ops->get_trip_hyst(tz, trip, &hyst);
376 if (tz->last_temperature != THERMAL_TEMP_INVALID) {
377 if (tz->last_temperature < trip_temp &&
378 tz->temperature >= trip_temp)
379 thermal_notify_tz_trip_up(tz->id, trip);
380 if (tz->last_temperature >= trip_temp &&
381 tz->temperature < (trip_temp - hyst))
382 thermal_notify_tz_trip_down(tz->id, trip);
385 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
386 handle_critical_trips(tz, trip, type);
388 handle_non_critical_trips(tz, trip);
390 * Alright, we handled this trip successfully.
391 * So, start monitoring again.
393 monitor_thermal_zone(tz);
396 static void update_temperature(struct thermal_zone_device *tz)
400 ret = thermal_zone_get_temp(tz, &temp);
403 dev_warn(&tz->device,
404 "failed to read out thermal zone (%d)\n",
409 mutex_lock(&tz->lock);
410 tz->last_temperature = tz->temperature;
411 tz->temperature = temp;
412 mutex_unlock(&tz->lock);
414 trace_thermal_temperature(tz);
416 thermal_genl_sampling_temp(tz->id, temp);
419 static void thermal_zone_device_init(struct thermal_zone_device *tz)
421 struct thermal_instance *pos;
422 tz->temperature = THERMAL_TEMP_INVALID;
423 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
424 pos->initialized = false;
427 static int thermal_zone_device_set_mode(struct thermal_zone_device *tz,
428 enum thermal_device_mode mode)
432 mutex_lock(&tz->lock);
434 /* do nothing if mode isn't changing */
435 if (mode == tz->mode) {
436 mutex_unlock(&tz->lock);
441 if (tz->ops->change_mode)
442 ret = tz->ops->change_mode(tz, mode);
447 mutex_unlock(&tz->lock);
449 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
451 if (mode == THERMAL_DEVICE_ENABLED)
452 thermal_notify_tz_enable(tz->id);
454 thermal_notify_tz_disable(tz->id);
459 int thermal_zone_device_enable(struct thermal_zone_device *tz)
461 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_ENABLED);
463 EXPORT_SYMBOL_GPL(thermal_zone_device_enable);
465 int thermal_zone_device_disable(struct thermal_zone_device *tz)
467 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED);
469 EXPORT_SYMBOL_GPL(thermal_zone_device_disable);
471 int thermal_zone_device_is_enabled(struct thermal_zone_device *tz)
473 enum thermal_device_mode mode;
475 mutex_lock(&tz->lock);
479 mutex_unlock(&tz->lock);
481 return mode == THERMAL_DEVICE_ENABLED;
484 void thermal_zone_device_update(struct thermal_zone_device *tz,
485 enum thermal_notify_event event)
489 if (should_stop_polling(tz))
492 if (atomic_read(&in_suspend))
495 if (WARN_ONCE(!tz->ops->get_temp, "'%s' must not be called without "
496 "'get_temp' ops set\n", __func__))
499 update_temperature(tz);
501 thermal_zone_set_trips(tz);
503 tz->notify_event = event;
505 for (count = 0; count < tz->trips; count++)
506 handle_thermal_trip(tz, count);
508 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
510 static void thermal_zone_device_check(struct work_struct *work)
512 struct thermal_zone_device *tz = container_of(work, struct
515 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
518 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
521 struct thermal_governor *gov;
524 mutex_lock(&thermal_governor_lock);
525 list_for_each_entry(gov, &thermal_governor_list, governor_list) {
530 mutex_unlock(&thermal_governor_lock);
535 int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
538 struct thermal_cooling_device *cdev;
541 mutex_lock(&thermal_list_lock);
542 list_for_each_entry(cdev, &thermal_cdev_list, node) {
543 ret = cb(cdev, data);
547 mutex_unlock(&thermal_list_lock);
552 int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
555 struct thermal_zone_device *tz;
558 mutex_lock(&thermal_list_lock);
559 list_for_each_entry(tz, &thermal_tz_list, node) {
564 mutex_unlock(&thermal_list_lock);
569 struct thermal_zone_device *thermal_zone_get_by_id(int id)
571 struct thermal_zone_device *tz, *match = NULL;
573 mutex_lock(&thermal_list_lock);
574 list_for_each_entry(tz, &thermal_tz_list, node) {
580 mutex_unlock(&thermal_list_lock);
586 * Device management section: cooling devices, zones devices, and binding
588 * Set of functions provided by the thermal core for:
589 * - cooling devices lifecycle: registration, unregistration,
590 * binding, and unbinding.
591 * - thermal zone devices lifecycle: registration, unregistration,
592 * binding, and unbinding.
596 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
597 * @tz: pointer to struct thermal_zone_device
598 * @trip: indicates which trip point the cooling devices is
599 * associated with in this thermal zone.
600 * @cdev: pointer to struct thermal_cooling_device
601 * @upper: the Maximum cooling state for this trip point.
602 * THERMAL_NO_LIMIT means no upper limit,
603 * and the cooling device can be in max_state.
604 * @lower: the Minimum cooling state can be used for this trip point.
605 * THERMAL_NO_LIMIT means no lower limit,
606 * and the cooling device can be in cooling state 0.
607 * @weight: The weight of the cooling device to be bound to the
608 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
611 * This interface function bind a thermal cooling device to the certain trip
612 * point of a thermal zone device.
613 * This function is usually called in the thermal zone device .bind callback.
615 * Return: 0 on success, the proper error value otherwise.
617 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
619 struct thermal_cooling_device *cdev,
620 unsigned long upper, unsigned long lower,
623 struct thermal_instance *dev;
624 struct thermal_instance *pos;
625 struct thermal_zone_device *pos1;
626 struct thermal_cooling_device *pos2;
627 unsigned long max_state;
630 if (trip >= tz->trips || trip < 0)
633 list_for_each_entry(pos1, &thermal_tz_list, node) {
637 list_for_each_entry(pos2, &thermal_cdev_list, node) {
642 if (tz != pos1 || cdev != pos2)
645 ret = cdev->ops->get_max_state(cdev, &max_state);
649 /* lower default 0, upper default max_state */
650 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
651 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
653 if (lower > upper || upper > max_state)
656 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
664 dev->target = THERMAL_NO_TARGET;
665 dev->weight = weight;
667 result = ida_simple_get(&tz->ida, 0, 0, GFP_KERNEL);
672 sprintf(dev->name, "cdev%d", dev->id);
674 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
678 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
679 sysfs_attr_init(&dev->attr.attr);
680 dev->attr.attr.name = dev->attr_name;
681 dev->attr.attr.mode = 0444;
682 dev->attr.show = trip_point_show;
683 result = device_create_file(&tz->device, &dev->attr);
685 goto remove_symbol_link;
687 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
688 sysfs_attr_init(&dev->weight_attr.attr);
689 dev->weight_attr.attr.name = dev->weight_attr_name;
690 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
691 dev->weight_attr.show = weight_show;
692 dev->weight_attr.store = weight_store;
693 result = device_create_file(&tz->device, &dev->weight_attr);
695 goto remove_trip_file;
697 mutex_lock(&tz->lock);
698 mutex_lock(&cdev->lock);
699 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
700 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
705 list_add_tail(&dev->tz_node, &tz->thermal_instances);
706 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
707 atomic_set(&tz->need_update, 1);
709 mutex_unlock(&cdev->lock);
710 mutex_unlock(&tz->lock);
715 device_remove_file(&tz->device, &dev->weight_attr);
717 device_remove_file(&tz->device, &dev->attr);
719 sysfs_remove_link(&tz->device.kobj, dev->name);
721 ida_simple_remove(&tz->ida, dev->id);
726 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
729 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
731 * @tz: pointer to a struct thermal_zone_device.
732 * @trip: indicates which trip point the cooling devices is
733 * associated with in this thermal zone.
734 * @cdev: pointer to a struct thermal_cooling_device.
736 * This interface function unbind a thermal cooling device from the certain
737 * trip point of a thermal zone device.
738 * This function is usually called in the thermal zone device .unbind callback.
740 * Return: 0 on success, the proper error value otherwise.
742 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
744 struct thermal_cooling_device *cdev)
746 struct thermal_instance *pos, *next;
748 mutex_lock(&tz->lock);
749 mutex_lock(&cdev->lock);
750 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
751 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
752 list_del(&pos->tz_node);
753 list_del(&pos->cdev_node);
754 mutex_unlock(&cdev->lock);
755 mutex_unlock(&tz->lock);
759 mutex_unlock(&cdev->lock);
760 mutex_unlock(&tz->lock);
765 device_remove_file(&tz->device, &pos->weight_attr);
766 device_remove_file(&tz->device, &pos->attr);
767 sysfs_remove_link(&tz->device.kobj, pos->name);
768 ida_simple_remove(&tz->ida, pos->id);
772 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
774 static void thermal_release(struct device *dev)
776 struct thermal_zone_device *tz;
777 struct thermal_cooling_device *cdev;
779 if (!strncmp(dev_name(dev), "thermal_zone",
780 sizeof("thermal_zone") - 1)) {
781 tz = to_thermal_zone(dev);
782 thermal_zone_destroy_device_groups(tz);
784 } else if (!strncmp(dev_name(dev), "cooling_device",
785 sizeof("cooling_device") - 1)) {
786 cdev = to_cooling_device(dev);
791 static struct class thermal_class = {
793 .dev_release = thermal_release,
797 void print_bind_err_msg(struct thermal_zone_device *tz,
798 struct thermal_cooling_device *cdev, int ret)
800 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
801 tz->type, cdev->type, ret);
804 static void __bind(struct thermal_zone_device *tz, int mask,
805 struct thermal_cooling_device *cdev,
806 unsigned long *limits,
811 for (i = 0; i < tz->trips; i++) {
812 if (mask & (1 << i)) {
813 unsigned long upper, lower;
815 upper = THERMAL_NO_LIMIT;
816 lower = THERMAL_NO_LIMIT;
818 lower = limits[i * 2];
819 upper = limits[i * 2 + 1];
821 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
825 print_bind_err_msg(tz, cdev, ret);
830 static void bind_cdev(struct thermal_cooling_device *cdev)
833 const struct thermal_zone_params *tzp;
834 struct thermal_zone_device *pos = NULL;
836 mutex_lock(&thermal_list_lock);
838 list_for_each_entry(pos, &thermal_tz_list, node) {
839 if (!pos->tzp && !pos->ops->bind)
842 if (pos->ops->bind) {
843 ret = pos->ops->bind(pos, cdev);
845 print_bind_err_msg(pos, cdev, ret);
850 if (!tzp || !tzp->tbp)
853 for (i = 0; i < tzp->num_tbps; i++) {
854 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
856 if (tzp->tbp[i].match(pos, cdev))
858 tzp->tbp[i].cdev = cdev;
859 __bind(pos, tzp->tbp[i].trip_mask, cdev,
860 tzp->tbp[i].binding_limits,
865 mutex_unlock(&thermal_list_lock);
869 * __thermal_cooling_device_register() - register a new thermal cooling device
870 * @np: a pointer to a device tree node.
871 * @type: the thermal cooling device type.
872 * @devdata: device private data.
873 * @ops: standard thermal cooling devices callbacks.
875 * This interface function adds a new thermal cooling device (fan/processor/...)
876 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
877 * to all the thermal zone devices registered at the same time.
878 * It also gives the opportunity to link the cooling device to a device tree
879 * node, so that it can be bound to a thermal zone created out of device tree.
881 * Return: a pointer to the created struct thermal_cooling_device or an
882 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
884 static struct thermal_cooling_device *
885 __thermal_cooling_device_register(struct device_node *np,
886 const char *type, void *devdata,
887 const struct thermal_cooling_device_ops *ops)
889 struct thermal_cooling_device *cdev;
890 struct thermal_zone_device *pos = NULL;
893 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
895 return ERR_PTR(-EINVAL);
897 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
899 return ERR_PTR(-ENOMEM);
901 ret = ida_simple_get(&thermal_cdev_ida, 0, 0, GFP_KERNEL);
906 cdev->type = kstrdup(type ? type : "", GFP_KERNEL);
912 mutex_init(&cdev->lock);
913 INIT_LIST_HEAD(&cdev->thermal_instances);
916 cdev->updated = false;
917 cdev->device.class = &thermal_class;
918 cdev->devdata = devdata;
919 thermal_cooling_device_setup_sysfs(cdev);
920 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
921 ret = device_register(&cdev->device);
925 /* Add 'this' new cdev to the global cdev list */
926 mutex_lock(&thermal_list_lock);
927 list_add(&cdev->node, &thermal_cdev_list);
928 mutex_unlock(&thermal_list_lock);
930 /* Update binding information for 'this' new cdev */
933 mutex_lock(&thermal_list_lock);
934 list_for_each_entry(pos, &thermal_tz_list, node)
935 if (atomic_cmpxchg(&pos->need_update, 1, 0))
936 thermal_zone_device_update(pos,
937 THERMAL_EVENT_UNSPECIFIED);
938 mutex_unlock(&thermal_list_lock);
944 put_device(&cdev->device);
946 ida_simple_remove(&thermal_cdev_ida, cdev->id);
953 * thermal_cooling_device_register() - register a new thermal cooling device
954 * @type: the thermal cooling device type.
955 * @devdata: device private data.
956 * @ops: standard thermal cooling devices callbacks.
958 * This interface function adds a new thermal cooling device (fan/processor/...)
959 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
960 * to all the thermal zone devices registered at the same time.
962 * Return: a pointer to the created struct thermal_cooling_device or an
963 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
965 struct thermal_cooling_device *
966 thermal_cooling_device_register(const char *type, void *devdata,
967 const struct thermal_cooling_device_ops *ops)
969 return __thermal_cooling_device_register(NULL, type, devdata, ops);
971 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
974 * thermal_of_cooling_device_register() - register an OF thermal cooling device
975 * @np: a pointer to a device tree node.
976 * @type: the thermal cooling device type.
977 * @devdata: device private data.
978 * @ops: standard thermal cooling devices callbacks.
980 * This function will register a cooling device with device tree node reference.
981 * This interface function adds a new thermal cooling device (fan/processor/...)
982 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
983 * to all the thermal zone devices registered at the same time.
985 * Return: a pointer to the created struct thermal_cooling_device or an
986 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
988 struct thermal_cooling_device *
989 thermal_of_cooling_device_register(struct device_node *np,
990 const char *type, void *devdata,
991 const struct thermal_cooling_device_ops *ops)
993 return __thermal_cooling_device_register(np, type, devdata, ops);
995 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
997 static void thermal_cooling_device_release(struct device *dev, void *res)
999 thermal_cooling_device_unregister(
1000 *(struct thermal_cooling_device **)res);
1004 * devm_thermal_of_cooling_device_register() - register an OF thermal cooling
1006 * @dev: a valid struct device pointer of a sensor device.
1007 * @np: a pointer to a device tree node.
1008 * @type: the thermal cooling device type.
1009 * @devdata: device private data.
1010 * @ops: standard thermal cooling devices callbacks.
1012 * This function will register a cooling device with device tree node reference.
1013 * This interface function adds a new thermal cooling device (fan/processor/...)
1014 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1015 * to all the thermal zone devices registered at the same time.
1017 * Return: a pointer to the created struct thermal_cooling_device or an
1018 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1020 struct thermal_cooling_device *
1021 devm_thermal_of_cooling_device_register(struct device *dev,
1022 struct device_node *np,
1023 char *type, void *devdata,
1024 const struct thermal_cooling_device_ops *ops)
1026 struct thermal_cooling_device **ptr, *tcd;
1028 ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr),
1031 return ERR_PTR(-ENOMEM);
1033 tcd = __thermal_cooling_device_register(np, type, devdata, ops);
1040 devres_add(dev, ptr);
1044 EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register);
1046 static void __unbind(struct thermal_zone_device *tz, int mask,
1047 struct thermal_cooling_device *cdev)
1051 for (i = 0; i < tz->trips; i++)
1052 if (mask & (1 << i))
1053 thermal_zone_unbind_cooling_device(tz, i, cdev);
1057 * thermal_cooling_device_unregister - removes a thermal cooling device
1058 * @cdev: the thermal cooling device to remove.
1060 * thermal_cooling_device_unregister() must be called when a registered
1061 * thermal cooling device is no longer needed.
1063 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1066 const struct thermal_zone_params *tzp;
1067 struct thermal_zone_device *tz;
1068 struct thermal_cooling_device *pos = NULL;
1073 mutex_lock(&thermal_list_lock);
1074 list_for_each_entry(pos, &thermal_cdev_list, node)
1078 /* thermal cooling device not found */
1079 mutex_unlock(&thermal_list_lock);
1082 list_del(&cdev->node);
1084 /* Unbind all thermal zones associated with 'this' cdev */
1085 list_for_each_entry(tz, &thermal_tz_list, node) {
1086 if (tz->ops->unbind) {
1087 tz->ops->unbind(tz, cdev);
1091 if (!tz->tzp || !tz->tzp->tbp)
1095 for (i = 0; i < tzp->num_tbps; i++) {
1096 if (tzp->tbp[i].cdev == cdev) {
1097 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1098 tzp->tbp[i].cdev = NULL;
1103 mutex_unlock(&thermal_list_lock);
1105 ida_simple_remove(&thermal_cdev_ida, cdev->id);
1106 device_del(&cdev->device);
1107 thermal_cooling_device_destroy_sysfs(cdev);
1109 put_device(&cdev->device);
1111 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1113 static void bind_tz(struct thermal_zone_device *tz)
1116 struct thermal_cooling_device *pos = NULL;
1117 const struct thermal_zone_params *tzp = tz->tzp;
1119 if (!tzp && !tz->ops->bind)
1122 mutex_lock(&thermal_list_lock);
1124 /* If there is ops->bind, try to use ops->bind */
1125 if (tz->ops->bind) {
1126 list_for_each_entry(pos, &thermal_cdev_list, node) {
1127 ret = tz->ops->bind(tz, pos);
1129 print_bind_err_msg(tz, pos, ret);
1134 if (!tzp || !tzp->tbp)
1137 list_for_each_entry(pos, &thermal_cdev_list, node) {
1138 for (i = 0; i < tzp->num_tbps; i++) {
1139 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
1141 if (tzp->tbp[i].match(tz, pos))
1143 tzp->tbp[i].cdev = pos;
1144 __bind(tz, tzp->tbp[i].trip_mask, pos,
1145 tzp->tbp[i].binding_limits,
1146 tzp->tbp[i].weight);
1150 mutex_unlock(&thermal_list_lock);
1154 * thermal_zone_device_register() - register a new thermal zone device
1155 * @type: the thermal zone device type
1156 * @trips: the number of trip points the thermal zone support
1157 * @mask: a bit string indicating the writeablility of trip points
1158 * @devdata: private device data
1159 * @ops: standard thermal zone device callbacks
1160 * @tzp: thermal zone platform parameters
1161 * @passive_delay: number of milliseconds to wait between polls when
1162 * performing passive cooling
1163 * @polling_delay: number of milliseconds to wait between polls when checking
1164 * whether trip points have been crossed (0 for interrupt
1167 * This interface function adds a new thermal zone device (sensor) to
1168 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1169 * thermal cooling devices registered at the same time.
1170 * thermal_zone_device_unregister() must be called when the device is no
1171 * longer needed. The passive cooling depends on the .get_trend() return value.
1173 * Return: a pointer to the created struct thermal_zone_device or an
1174 * in case of error, an ERR_PTR. Caller must check return value with
1175 * IS_ERR*() helpers.
1177 struct thermal_zone_device *
1178 thermal_zone_device_register(const char *type, int trips, int mask,
1179 void *devdata, struct thermal_zone_device_ops *ops,
1180 struct thermal_zone_params *tzp, int passive_delay,
1183 struct thermal_zone_device *tz;
1184 enum thermal_trip_type trip_type;
1189 struct thermal_governor *governor;
1191 if (!type || strlen(type) == 0) {
1192 pr_err("Error: No thermal zone type defined\n");
1193 return ERR_PTR(-EINVAL);
1196 if (type && strlen(type) >= THERMAL_NAME_LENGTH) {
1197 pr_err("Error: Thermal zone name (%s) too long, should be under %d chars\n",
1198 type, THERMAL_NAME_LENGTH);
1199 return ERR_PTR(-EINVAL);
1202 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips) {
1203 pr_err("Error: Incorrect number of thermal trips\n");
1204 return ERR_PTR(-EINVAL);
1208 pr_err("Error: Thermal zone device ops not defined\n");
1209 return ERR_PTR(-EINVAL);
1212 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
1213 return ERR_PTR(-EINVAL);
1215 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
1217 return ERR_PTR(-ENOMEM);
1219 INIT_LIST_HEAD(&tz->thermal_instances);
1221 mutex_init(&tz->lock);
1222 id = ida_simple_get(&thermal_tz_ida, 0, 0, GFP_KERNEL);
1229 strlcpy(tz->type, type, sizeof(tz->type));
1232 ops->critical = thermal_zone_device_critical;
1236 tz->device.class = &thermal_class;
1237 tz->devdata = devdata;
1240 thermal_set_delay_jiffies(&tz->passive_delay_jiffies, passive_delay);
1241 thermal_set_delay_jiffies(&tz->polling_delay_jiffies, polling_delay);
1244 /* Add nodes that are always present via .groups */
1245 result = thermal_zone_create_device_groups(tz, mask);
1249 /* A new thermal zone needs to be updated anyway. */
1250 atomic_set(&tz->need_update, 1);
1252 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1253 result = device_register(&tz->device);
1255 goto release_device;
1257 for (count = 0; count < trips; count++) {
1258 if (tz->ops->get_trip_type(tz, count, &trip_type) ||
1259 tz->ops->get_trip_temp(tz, count, &trip_temp) ||
1261 set_bit(count, &tz->trips_disabled);
1264 /* Update 'this' zone's governor information */
1265 mutex_lock(&thermal_governor_lock);
1268 governor = __find_governor(tz->tzp->governor_name);
1270 governor = def_governor;
1272 result = thermal_set_governor(tz, governor);
1274 mutex_unlock(&thermal_governor_lock);
1278 mutex_unlock(&thermal_governor_lock);
1280 if (!tz->tzp || !tz->tzp->no_hwmon) {
1281 result = thermal_add_hwmon_sysfs(tz);
1286 mutex_lock(&thermal_list_lock);
1287 list_add_tail(&tz->node, &thermal_tz_list);
1288 mutex_unlock(&thermal_list_lock);
1290 /* Bind cooling devices for this zone */
1293 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
1295 thermal_zone_device_init(tz);
1296 /* Update the new thermal zone and mark it as already updated. */
1297 if (atomic_cmpxchg(&tz->need_update, 1, 0))
1298 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1300 thermal_notify_tz_create(tz->id, tz->type);
1305 device_del(&tz->device);
1307 put_device(&tz->device);
1310 ida_simple_remove(&thermal_tz_ida, id);
1313 return ERR_PTR(result);
1315 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
1318 * thermal_device_unregister - removes the registered thermal zone device
1319 * @tz: the thermal zone device to remove
1321 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1324 const struct thermal_zone_params *tzp;
1325 struct thermal_cooling_device *cdev;
1326 struct thermal_zone_device *pos = NULL;
1334 mutex_lock(&thermal_list_lock);
1335 list_for_each_entry(pos, &thermal_tz_list, node)
1339 /* thermal zone device not found */
1340 mutex_unlock(&thermal_list_lock);
1343 list_del(&tz->node);
1345 /* Unbind all cdevs associated with 'this' thermal zone */
1346 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1347 if (tz->ops->unbind) {
1348 tz->ops->unbind(tz, cdev);
1352 if (!tzp || !tzp->tbp)
1355 for (i = 0; i < tzp->num_tbps; i++) {
1356 if (tzp->tbp[i].cdev == cdev) {
1357 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1358 tzp->tbp[i].cdev = NULL;
1363 mutex_unlock(&thermal_list_lock);
1365 cancel_delayed_work_sync(&tz->poll_queue);
1367 thermal_set_governor(tz, NULL);
1369 thermal_remove_hwmon_sysfs(tz);
1370 ida_simple_remove(&thermal_tz_ida, tz->id);
1371 ida_destroy(&tz->ida);
1372 mutex_destroy(&tz->lock);
1373 device_unregister(&tz->device);
1375 thermal_notify_tz_delete(tz_id);
1377 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1380 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1381 * @name: thermal zone name to fetch the temperature
1383 * When only one zone is found with the passed name, returns a reference to it.
1385 * Return: On success returns a reference to an unique thermal zone with
1386 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1387 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1389 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1391 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1392 unsigned int found = 0;
1397 mutex_lock(&thermal_list_lock);
1398 list_for_each_entry(pos, &thermal_tz_list, node)
1399 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1403 mutex_unlock(&thermal_list_lock);
1405 /* nothing has been found, thus an error code for it */
1407 ref = ERR_PTR(-ENODEV);
1409 /* Success only when an unique zone is found */
1410 ref = ERR_PTR(-EEXIST);
1415 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1417 static int thermal_pm_notify(struct notifier_block *nb,
1418 unsigned long mode, void *_unused)
1420 struct thermal_zone_device *tz;
1423 case PM_HIBERNATION_PREPARE:
1424 case PM_RESTORE_PREPARE:
1425 case PM_SUSPEND_PREPARE:
1426 atomic_set(&in_suspend, 1);
1428 case PM_POST_HIBERNATION:
1429 case PM_POST_RESTORE:
1430 case PM_POST_SUSPEND:
1431 atomic_set(&in_suspend, 0);
1432 list_for_each_entry(tz, &thermal_tz_list, node) {
1433 if (!thermal_zone_device_is_enabled(tz))
1436 thermal_zone_device_init(tz);
1437 thermal_zone_device_update(tz,
1438 THERMAL_EVENT_UNSPECIFIED);
1447 static struct notifier_block thermal_pm_nb = {
1448 .notifier_call = thermal_pm_notify,
1451 static int __init thermal_init(void)
1455 result = thermal_netlink_init();
1459 result = thermal_register_governors();
1463 result = class_register(&thermal_class);
1465 goto unregister_governors;
1467 result = of_parse_thermal_zones();
1469 goto unregister_class;
1471 result = register_pm_notifier(&thermal_pm_nb);
1473 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1479 class_unregister(&thermal_class);
1480 unregister_governors:
1481 thermal_unregister_governors();
1483 ida_destroy(&thermal_tz_ida);
1484 ida_destroy(&thermal_cdev_ida);
1485 mutex_destroy(&thermal_list_lock);
1486 mutex_destroy(&thermal_governor_lock);
1489 postcore_initcall(thermal_init);