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);
39 static DEFINE_MUTEX(poweroff_lock);
41 static atomic_t in_suspend;
42 static bool power_off_triggered;
44 static struct thermal_governor *def_governor;
47 * Governor section: set of functions to handle thermal governors
49 * Functions to help in the life cycle of thermal governors within
50 * the thermal core and by the thermal governor code.
53 static struct thermal_governor *__find_governor(const char *name)
55 struct thermal_governor *pos;
57 if (!name || !name[0])
60 list_for_each_entry(pos, &thermal_governor_list, governor_list)
61 if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH))
68 * bind_previous_governor() - bind the previous governor of the thermal zone
69 * @tz: a valid pointer to a struct thermal_zone_device
70 * @failed_gov_name: the name of the governor that failed to register
72 * Register the previous governor of the thermal zone after a new
73 * governor has failed to be bound.
75 static void bind_previous_governor(struct thermal_zone_device *tz,
76 const char *failed_gov_name)
78 if (tz->governor && tz->governor->bind_to_tz) {
79 if (tz->governor->bind_to_tz(tz)) {
81 "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n",
82 failed_gov_name, tz->governor->name, tz->type);
89 * thermal_set_governor() - Switch to another governor
90 * @tz: a valid pointer to a struct thermal_zone_device
91 * @new_gov: pointer to the new governor
93 * Change the governor of thermal zone @tz.
95 * Return: 0 on success, an error if the new governor's bind_to_tz() failed.
97 static int thermal_set_governor(struct thermal_zone_device *tz,
98 struct thermal_governor *new_gov)
102 if (tz->governor && tz->governor->unbind_from_tz)
103 tz->governor->unbind_from_tz(tz);
105 if (new_gov && new_gov->bind_to_tz) {
106 ret = new_gov->bind_to_tz(tz);
108 bind_previous_governor(tz, new_gov->name);
114 tz->governor = new_gov;
119 int thermal_register_governor(struct thermal_governor *governor)
123 struct thermal_zone_device *pos;
128 mutex_lock(&thermal_governor_lock);
131 if (!__find_governor(governor->name)) {
135 list_add(&governor->governor_list, &thermal_governor_list);
136 match_default = !strncmp(governor->name,
137 DEFAULT_THERMAL_GOVERNOR,
138 THERMAL_NAME_LENGTH);
140 if (!def_governor && match_default)
141 def_governor = governor;
144 mutex_lock(&thermal_list_lock);
146 list_for_each_entry(pos, &thermal_tz_list, node) {
148 * only thermal zones with specified tz->tzp->governor_name
149 * may run with tz->govenor unset
154 name = pos->tzp->governor_name;
156 if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) {
159 ret = thermal_set_governor(pos, governor);
161 dev_err(&pos->device,
162 "Failed to set governor %s for thermal zone %s: %d\n",
163 governor->name, pos->type, ret);
167 mutex_unlock(&thermal_list_lock);
168 mutex_unlock(&thermal_governor_lock);
173 void thermal_unregister_governor(struct thermal_governor *governor)
175 struct thermal_zone_device *pos;
180 mutex_lock(&thermal_governor_lock);
182 if (!__find_governor(governor->name))
185 mutex_lock(&thermal_list_lock);
187 list_for_each_entry(pos, &thermal_tz_list, node) {
188 if (!strncasecmp(pos->governor->name, governor->name,
189 THERMAL_NAME_LENGTH))
190 thermal_set_governor(pos, NULL);
193 mutex_unlock(&thermal_list_lock);
194 list_del(&governor->governor_list);
196 mutex_unlock(&thermal_governor_lock);
199 int thermal_zone_device_set_policy(struct thermal_zone_device *tz,
202 struct thermal_governor *gov;
205 mutex_lock(&thermal_governor_lock);
206 mutex_lock(&tz->lock);
208 gov = __find_governor(strim(policy));
212 ret = thermal_set_governor(tz, gov);
215 mutex_unlock(&tz->lock);
216 mutex_unlock(&thermal_governor_lock);
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,
295 round_jiffies(msecs_to_jiffies(delay)));
297 mod_delayed_work(system_freezable_power_efficient_wq,
299 msecs_to_jiffies(delay));
301 cancel_delayed_work(&tz->poll_queue);
304 static inline bool should_stop_polling(struct thermal_zone_device *tz)
306 return !thermal_zone_device_is_enabled(tz);
309 static void monitor_thermal_zone(struct thermal_zone_device *tz)
313 stop = should_stop_polling(tz);
315 mutex_lock(&tz->lock);
317 if (!stop && tz->passive)
318 thermal_zone_device_set_polling(tz, tz->passive_delay);
319 else if (!stop && tz->polling_delay)
320 thermal_zone_device_set_polling(tz, tz->polling_delay);
322 thermal_zone_device_set_polling(tz, 0);
324 mutex_unlock(&tz->lock);
327 static void handle_non_critical_trips(struct thermal_zone_device *tz, int trip)
329 tz->governor ? tz->governor->throttle(tz, trip) :
330 def_governor->throttle(tz, trip);
334 * thermal_emergency_poweroff_func - emergency poweroff work after a known delay
335 * @work: work_struct associated with the emergency poweroff function
337 * This function is called in very critical situations to force
338 * a kernel poweroff after a configurable timeout value.
340 static void thermal_emergency_poweroff_func(struct work_struct *work)
343 * We have reached here after the emergency thermal shutdown
344 * Waiting period has expired. This means orderly_poweroff has
345 * not been able to shut off the system for some reason.
346 * Try to shut down the system immediately using kernel_power_off
349 WARN(1, "Attempting kernel_power_off: Temperature too high\n");
353 * Worst of the worst case trigger emergency restart
355 WARN(1, "Attempting emergency_restart: Temperature too high\n");
359 static DECLARE_DELAYED_WORK(thermal_emergency_poweroff_work,
360 thermal_emergency_poweroff_func);
363 * thermal_emergency_poweroff - Trigger an emergency system poweroff
365 * This may be called from any critical situation to trigger a system shutdown
366 * after a known period of time. By default this is not scheduled.
368 static void thermal_emergency_poweroff(void)
370 int poweroff_delay_ms = CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS;
372 * poweroff_delay_ms must be a carefully profiled positive value.
373 * Its a must for thermal_emergency_poweroff_work to be scheduled
375 if (poweroff_delay_ms <= 0)
377 schedule_delayed_work(&thermal_emergency_poweroff_work,
378 msecs_to_jiffies(poweroff_delay_ms));
381 static void handle_critical_trips(struct thermal_zone_device *tz,
382 int trip, enum thermal_trip_type trip_type)
386 tz->ops->get_trip_temp(tz, trip, &trip_temp);
388 /* If we have not crossed the trip_temp, we do not care. */
389 if (trip_temp <= 0 || tz->temperature < trip_temp)
392 trace_thermal_zone_trip(tz, trip, trip_type);
395 tz->ops->notify(tz, trip, trip_type);
397 if (trip_type == THERMAL_TRIP_CRITICAL) {
398 dev_emerg(&tz->device,
399 "critical temperature reached (%d C), shutting down\n",
400 tz->temperature / 1000);
401 mutex_lock(&poweroff_lock);
402 if (!power_off_triggered) {
404 * Queue a backup emergency shutdown in the event of
405 * orderly_poweroff failure
407 thermal_emergency_poweroff();
408 orderly_poweroff(true);
409 power_off_triggered = true;
411 mutex_unlock(&poweroff_lock);
415 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
417 enum thermal_trip_type type;
419 /* Ignore disabled trip points */
420 if (test_bit(trip, &tz->trips_disabled))
423 tz->ops->get_trip_type(tz, trip, &type);
425 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
426 handle_critical_trips(tz, trip, type);
428 handle_non_critical_trips(tz, trip);
430 * Alright, we handled this trip successfully.
431 * So, start monitoring again.
433 monitor_thermal_zone(tz);
436 static void update_temperature(struct thermal_zone_device *tz)
440 ret = thermal_zone_get_temp(tz, &temp);
443 dev_warn(&tz->device,
444 "failed to read out thermal zone (%d)\n",
449 mutex_lock(&tz->lock);
450 tz->last_temperature = tz->temperature;
451 tz->temperature = temp;
452 mutex_unlock(&tz->lock);
454 trace_thermal_temperature(tz);
457 static void thermal_zone_device_init(struct thermal_zone_device *tz)
459 struct thermal_instance *pos;
460 tz->temperature = THERMAL_TEMP_INVALID;
461 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
462 pos->initialized = false;
465 static void thermal_zone_device_reset(struct thermal_zone_device *tz)
468 thermal_zone_device_init(tz);
471 static int thermal_zone_device_set_mode(struct thermal_zone_device *tz,
472 enum thermal_device_mode mode)
476 mutex_lock(&tz->lock);
478 /* do nothing if mode isn't changing */
479 if (mode == tz->mode) {
480 mutex_unlock(&tz->lock);
485 if (tz->ops->set_mode)
486 ret = tz->ops->set_mode(tz, mode);
491 mutex_unlock(&tz->lock);
493 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
498 int thermal_zone_device_enable(struct thermal_zone_device *tz)
500 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_ENABLED);
502 EXPORT_SYMBOL_GPL(thermal_zone_device_enable);
504 int thermal_zone_device_disable(struct thermal_zone_device *tz)
506 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED);
508 EXPORT_SYMBOL_GPL(thermal_zone_device_disable);
510 int thermal_zone_device_is_enabled(struct thermal_zone_device *tz)
512 enum thermal_device_mode mode;
514 mutex_lock(&tz->lock);
518 mutex_unlock(&tz->lock);
520 return mode == THERMAL_DEVICE_ENABLED;
522 EXPORT_SYMBOL_GPL(thermal_zone_device_is_enabled);
524 void thermal_zone_device_update(struct thermal_zone_device *tz,
525 enum thermal_notify_event event)
529 if (should_stop_polling(tz))
532 if (atomic_read(&in_suspend))
535 if (!tz->ops->get_temp)
538 update_temperature(tz);
540 thermal_zone_set_trips(tz);
542 tz->notify_event = event;
544 for (count = 0; count < tz->trips; count++)
545 handle_thermal_trip(tz, count);
547 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
550 * thermal_notify_framework - Sensor drivers use this API to notify framework
551 * @tz: thermal zone device
552 * @trip: indicates which trip point has been crossed
554 * This function handles the trip events from sensor drivers. It starts
555 * throttling the cooling devices according to the policy configured.
556 * For CRITICAL and HOT trip points, this notifies the respective drivers,
557 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
558 * The throttling policy is based on the configured platform data; if no
559 * platform data is provided, this uses the step_wise throttling policy.
561 void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
563 handle_thermal_trip(tz, trip);
565 EXPORT_SYMBOL_GPL(thermal_notify_framework);
567 static void thermal_zone_device_check(struct work_struct *work)
569 struct thermal_zone_device *tz = container_of(work, struct
572 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
576 * Power actor section: interface to power actors to estimate power
578 * Set of functions used to interact to cooling devices that know
579 * how to estimate their devices power consumption.
583 * power_actor_get_max_power() - get the maximum power that a cdev can consume
584 * @cdev: pointer to &thermal_cooling_device
585 * @tz: a valid thermal zone device pointer
586 * @max_power: pointer in which to store the maximum power
588 * Calculate the maximum power consumption in milliwats that the
589 * cooling device can currently consume and store it in @max_power.
591 * Return: 0 on success, -EINVAL if @cdev doesn't support the
592 * power_actor API or -E* on other error.
594 int power_actor_get_max_power(struct thermal_cooling_device *cdev,
595 struct thermal_zone_device *tz, u32 *max_power)
597 if (!cdev_is_power_actor(cdev))
600 return cdev->ops->state2power(cdev, tz, 0, max_power);
604 * power_actor_get_min_power() - get the mainimum power that a cdev can consume
605 * @cdev: pointer to &thermal_cooling_device
606 * @tz: a valid thermal zone device pointer
607 * @min_power: pointer in which to store the minimum power
609 * Calculate the minimum power consumption in milliwatts that the
610 * cooling device can currently consume and store it in @min_power.
612 * Return: 0 on success, -EINVAL if @cdev doesn't support the
613 * power_actor API or -E* on other error.
615 int power_actor_get_min_power(struct thermal_cooling_device *cdev,
616 struct thermal_zone_device *tz, u32 *min_power)
618 unsigned long max_state;
621 if (!cdev_is_power_actor(cdev))
624 ret = cdev->ops->get_max_state(cdev, &max_state);
628 return cdev->ops->state2power(cdev, tz, max_state, min_power);
632 * power_actor_set_power() - limit the maximum power a cooling device consumes
633 * @cdev: pointer to &thermal_cooling_device
634 * @instance: thermal instance to update
635 * @power: the power in milliwatts
637 * Set the cooling device to consume at most @power milliwatts. The limit is
638 * expected to be a cap at the maximum power consumption.
640 * Return: 0 on success, -EINVAL if the cooling device does not
641 * implement the power actor API or -E* for other failures.
643 int power_actor_set_power(struct thermal_cooling_device *cdev,
644 struct thermal_instance *instance, u32 power)
649 if (!cdev_is_power_actor(cdev))
652 ret = cdev->ops->power2state(cdev, instance->tz, power, &state);
656 instance->target = state;
657 mutex_lock(&cdev->lock);
658 cdev->updated = false;
659 mutex_unlock(&cdev->lock);
660 thermal_cdev_update(cdev);
665 void thermal_zone_device_rebind_exception(struct thermal_zone_device *tz,
666 const char *cdev_type, size_t size)
668 struct thermal_cooling_device *cdev = NULL;
670 mutex_lock(&thermal_list_lock);
671 list_for_each_entry(cdev, &thermal_cdev_list, node) {
672 /* skip non matching cdevs */
673 if (strncmp(cdev_type, cdev->type, size))
676 /* re binding the exception matching the type pattern */
677 thermal_zone_bind_cooling_device(tz, THERMAL_TRIPS_NONE, cdev,
680 THERMAL_WEIGHT_DEFAULT);
682 mutex_unlock(&thermal_list_lock);
685 void thermal_zone_device_unbind_exception(struct thermal_zone_device *tz,
686 const char *cdev_type, size_t size)
688 struct thermal_cooling_device *cdev = NULL;
690 mutex_lock(&thermal_list_lock);
691 list_for_each_entry(cdev, &thermal_cdev_list, node) {
692 /* skip non matching cdevs */
693 if (strncmp(cdev_type, cdev->type, size))
695 /* unbinding the exception matching the type pattern */
696 thermal_zone_unbind_cooling_device(tz, THERMAL_TRIPS_NONE,
699 mutex_unlock(&thermal_list_lock);
703 * Device management section: cooling devices, zones devices, and binding
705 * Set of functions provided by the thermal core for:
706 * - cooling devices lifecycle: registration, unregistration,
707 * binding, and unbinding.
708 * - thermal zone devices lifecycle: registration, unregistration,
709 * binding, and unbinding.
713 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
714 * @tz: pointer to struct thermal_zone_device
715 * @trip: indicates which trip point the cooling devices is
716 * associated with in this thermal zone.
717 * @cdev: pointer to struct thermal_cooling_device
718 * @upper: the Maximum cooling state for this trip point.
719 * THERMAL_NO_LIMIT means no upper limit,
720 * and the cooling device can be in max_state.
721 * @lower: the Minimum cooling state can be used for this trip point.
722 * THERMAL_NO_LIMIT means no lower limit,
723 * and the cooling device can be in cooling state 0.
724 * @weight: The weight of the cooling device to be bound to the
725 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
728 * This interface function bind a thermal cooling device to the certain trip
729 * point of a thermal zone device.
730 * This function is usually called in the thermal zone device .bind callback.
732 * Return: 0 on success, the proper error value otherwise.
734 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
736 struct thermal_cooling_device *cdev,
737 unsigned long upper, unsigned long lower,
740 struct thermal_instance *dev;
741 struct thermal_instance *pos;
742 struct thermal_zone_device *pos1;
743 struct thermal_cooling_device *pos2;
744 unsigned long max_state;
747 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
750 list_for_each_entry(pos1, &thermal_tz_list, node) {
754 list_for_each_entry(pos2, &thermal_cdev_list, node) {
759 if (tz != pos1 || cdev != pos2)
762 ret = cdev->ops->get_max_state(cdev, &max_state);
766 /* lower default 0, upper default max_state */
767 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
768 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
770 if (lower > upper || upper > max_state)
773 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
781 dev->target = THERMAL_NO_TARGET;
782 dev->weight = weight;
784 result = ida_simple_get(&tz->ida, 0, 0, GFP_KERNEL);
789 sprintf(dev->name, "cdev%d", dev->id);
791 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
795 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
796 sysfs_attr_init(&dev->attr.attr);
797 dev->attr.attr.name = dev->attr_name;
798 dev->attr.attr.mode = 0444;
799 dev->attr.show = trip_point_show;
800 result = device_create_file(&tz->device, &dev->attr);
802 goto remove_symbol_link;
804 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
805 sysfs_attr_init(&dev->weight_attr.attr);
806 dev->weight_attr.attr.name = dev->weight_attr_name;
807 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
808 dev->weight_attr.show = weight_show;
809 dev->weight_attr.store = weight_store;
810 result = device_create_file(&tz->device, &dev->weight_attr);
812 goto remove_trip_file;
814 mutex_lock(&tz->lock);
815 mutex_lock(&cdev->lock);
816 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
817 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
822 list_add_tail(&dev->tz_node, &tz->thermal_instances);
823 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
824 atomic_set(&tz->need_update, 1);
826 mutex_unlock(&cdev->lock);
827 mutex_unlock(&tz->lock);
832 device_remove_file(&tz->device, &dev->weight_attr);
834 device_remove_file(&tz->device, &dev->attr);
836 sysfs_remove_link(&tz->device.kobj, dev->name);
838 ida_simple_remove(&tz->ida, dev->id);
843 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
846 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
848 * @tz: pointer to a struct thermal_zone_device.
849 * @trip: indicates which trip point the cooling devices is
850 * associated with in this thermal zone.
851 * @cdev: pointer to a struct thermal_cooling_device.
853 * This interface function unbind a thermal cooling device from the certain
854 * trip point of a thermal zone device.
855 * This function is usually called in the thermal zone device .unbind callback.
857 * Return: 0 on success, the proper error value otherwise.
859 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
861 struct thermal_cooling_device *cdev)
863 struct thermal_instance *pos, *next;
865 mutex_lock(&tz->lock);
866 mutex_lock(&cdev->lock);
867 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
868 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
869 list_del(&pos->tz_node);
870 list_del(&pos->cdev_node);
871 mutex_unlock(&cdev->lock);
872 mutex_unlock(&tz->lock);
876 mutex_unlock(&cdev->lock);
877 mutex_unlock(&tz->lock);
882 device_remove_file(&tz->device, &pos->weight_attr);
883 device_remove_file(&tz->device, &pos->attr);
884 sysfs_remove_link(&tz->device.kobj, pos->name);
885 ida_simple_remove(&tz->ida, pos->id);
889 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
891 static void thermal_release(struct device *dev)
893 struct thermal_zone_device *tz;
894 struct thermal_cooling_device *cdev;
896 if (!strncmp(dev_name(dev), "thermal_zone",
897 sizeof("thermal_zone") - 1)) {
898 tz = to_thermal_zone(dev);
899 thermal_zone_destroy_device_groups(tz);
901 } else if (!strncmp(dev_name(dev), "cooling_device",
902 sizeof("cooling_device") - 1)) {
903 cdev = to_cooling_device(dev);
908 static struct class thermal_class = {
910 .dev_release = thermal_release,
914 void print_bind_err_msg(struct thermal_zone_device *tz,
915 struct thermal_cooling_device *cdev, int ret)
917 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
918 tz->type, cdev->type, ret);
921 static void __bind(struct thermal_zone_device *tz, int mask,
922 struct thermal_cooling_device *cdev,
923 unsigned long *limits,
928 for (i = 0; i < tz->trips; i++) {
929 if (mask & (1 << i)) {
930 unsigned long upper, lower;
932 upper = THERMAL_NO_LIMIT;
933 lower = THERMAL_NO_LIMIT;
935 lower = limits[i * 2];
936 upper = limits[i * 2 + 1];
938 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
942 print_bind_err_msg(tz, cdev, ret);
947 static void bind_cdev(struct thermal_cooling_device *cdev)
950 const struct thermal_zone_params *tzp;
951 struct thermal_zone_device *pos = NULL;
953 mutex_lock(&thermal_list_lock);
955 list_for_each_entry(pos, &thermal_tz_list, node) {
956 if (!pos->tzp && !pos->ops->bind)
959 if (pos->ops->bind) {
960 ret = pos->ops->bind(pos, cdev);
962 print_bind_err_msg(pos, cdev, ret);
967 if (!tzp || !tzp->tbp)
970 for (i = 0; i < tzp->num_tbps; i++) {
971 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
973 if (tzp->tbp[i].match(pos, cdev))
975 tzp->tbp[i].cdev = cdev;
976 __bind(pos, tzp->tbp[i].trip_mask, cdev,
977 tzp->tbp[i].binding_limits,
982 mutex_unlock(&thermal_list_lock);
986 * __thermal_cooling_device_register() - register a new thermal cooling device
987 * @np: a pointer to a device tree node.
988 * @type: the thermal cooling device type.
989 * @devdata: device private data.
990 * @ops: standard thermal cooling devices callbacks.
992 * This interface function adds a new thermal cooling device (fan/processor/...)
993 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
994 * to all the thermal zone devices registered at the same time.
995 * It also gives the opportunity to link the cooling device to a device tree
996 * node, so that it can be bound to a thermal zone created out of device tree.
998 * Return: a pointer to the created struct thermal_cooling_device or an
999 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1001 static struct thermal_cooling_device *
1002 __thermal_cooling_device_register(struct device_node *np,
1003 const char *type, void *devdata,
1004 const struct thermal_cooling_device_ops *ops)
1006 struct thermal_cooling_device *cdev;
1007 struct thermal_zone_device *pos = NULL;
1010 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1011 return ERR_PTR(-EINVAL);
1013 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
1014 !ops->set_cur_state)
1015 return ERR_PTR(-EINVAL);
1017 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
1019 return ERR_PTR(-ENOMEM);
1021 result = ida_simple_get(&thermal_cdev_ida, 0, 0, GFP_KERNEL);
1024 return ERR_PTR(result);
1028 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
1029 mutex_init(&cdev->lock);
1030 INIT_LIST_HEAD(&cdev->thermal_instances);
1033 cdev->updated = false;
1034 cdev->device.class = &thermal_class;
1035 cdev->devdata = devdata;
1036 thermal_cooling_device_setup_sysfs(cdev);
1037 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
1038 result = device_register(&cdev->device);
1040 ida_simple_remove(&thermal_cdev_ida, cdev->id);
1041 put_device(&cdev->device);
1042 return ERR_PTR(result);
1045 /* Add 'this' new cdev to the global cdev list */
1046 mutex_lock(&thermal_list_lock);
1047 list_add(&cdev->node, &thermal_cdev_list);
1048 mutex_unlock(&thermal_list_lock);
1050 /* Update binding information for 'this' new cdev */
1053 mutex_lock(&thermal_list_lock);
1054 list_for_each_entry(pos, &thermal_tz_list, node)
1055 if (atomic_cmpxchg(&pos->need_update, 1, 0))
1056 thermal_zone_device_update(pos,
1057 THERMAL_EVENT_UNSPECIFIED);
1058 mutex_unlock(&thermal_list_lock);
1064 * thermal_cooling_device_register() - register a new thermal cooling device
1065 * @type: the thermal cooling device type.
1066 * @devdata: device private data.
1067 * @ops: standard thermal cooling devices callbacks.
1069 * This interface function adds a new thermal cooling device (fan/processor/...)
1070 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1071 * to all the thermal zone devices registered at the same time.
1073 * Return: a pointer to the created struct thermal_cooling_device or an
1074 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1076 struct thermal_cooling_device *
1077 thermal_cooling_device_register(const char *type, void *devdata,
1078 const struct thermal_cooling_device_ops *ops)
1080 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1082 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
1085 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1086 * @np: a pointer to a device tree node.
1087 * @type: the thermal cooling device type.
1088 * @devdata: device private data.
1089 * @ops: standard thermal cooling devices callbacks.
1091 * This function will register a cooling device with device tree node reference.
1092 * This interface function adds a new thermal cooling device (fan/processor/...)
1093 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1094 * to all the thermal zone devices registered at the same time.
1096 * Return: a pointer to the created struct thermal_cooling_device or an
1097 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1099 struct thermal_cooling_device *
1100 thermal_of_cooling_device_register(struct device_node *np,
1101 const char *type, void *devdata,
1102 const struct thermal_cooling_device_ops *ops)
1104 return __thermal_cooling_device_register(np, type, devdata, ops);
1106 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1108 static void thermal_cooling_device_release(struct device *dev, void *res)
1110 thermal_cooling_device_unregister(
1111 *(struct thermal_cooling_device **)res);
1115 * devm_thermal_of_cooling_device_register() - register an OF thermal cooling
1117 * @dev: a valid struct device pointer of a sensor device.
1118 * @np: a pointer to a device tree node.
1119 * @type: the thermal cooling device type.
1120 * @devdata: device private data.
1121 * @ops: standard thermal cooling devices callbacks.
1123 * This function will register a cooling device with device tree node reference.
1124 * This interface function adds a new thermal cooling device (fan/processor/...)
1125 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1126 * to all the thermal zone devices registered at the same time.
1128 * Return: a pointer to the created struct thermal_cooling_device or an
1129 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1131 struct thermal_cooling_device *
1132 devm_thermal_of_cooling_device_register(struct device *dev,
1133 struct device_node *np,
1134 char *type, void *devdata,
1135 const struct thermal_cooling_device_ops *ops)
1137 struct thermal_cooling_device **ptr, *tcd;
1139 ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr),
1142 return ERR_PTR(-ENOMEM);
1144 tcd = __thermal_cooling_device_register(np, type, devdata, ops);
1151 devres_add(dev, ptr);
1155 EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register);
1157 static void __unbind(struct thermal_zone_device *tz, int mask,
1158 struct thermal_cooling_device *cdev)
1162 for (i = 0; i < tz->trips; i++)
1163 if (mask & (1 << i))
1164 thermal_zone_unbind_cooling_device(tz, i, cdev);
1168 * thermal_cooling_device_unregister - removes a thermal cooling device
1169 * @cdev: the thermal cooling device to remove.
1171 * thermal_cooling_device_unregister() must be called when a registered
1172 * thermal cooling device is no longer needed.
1174 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1177 const struct thermal_zone_params *tzp;
1178 struct thermal_zone_device *tz;
1179 struct thermal_cooling_device *pos = NULL;
1184 mutex_lock(&thermal_list_lock);
1185 list_for_each_entry(pos, &thermal_cdev_list, node)
1189 /* thermal cooling device not found */
1190 mutex_unlock(&thermal_list_lock);
1193 list_del(&cdev->node);
1195 /* Unbind all thermal zones associated with 'this' cdev */
1196 list_for_each_entry(tz, &thermal_tz_list, node) {
1197 if (tz->ops->unbind) {
1198 tz->ops->unbind(tz, cdev);
1202 if (!tz->tzp || !tz->tzp->tbp)
1206 for (i = 0; i < tzp->num_tbps; i++) {
1207 if (tzp->tbp[i].cdev == cdev) {
1208 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1209 tzp->tbp[i].cdev = NULL;
1214 mutex_unlock(&thermal_list_lock);
1216 ida_simple_remove(&thermal_cdev_ida, cdev->id);
1217 device_del(&cdev->device);
1218 thermal_cooling_device_destroy_sysfs(cdev);
1219 put_device(&cdev->device);
1221 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1223 static void bind_tz(struct thermal_zone_device *tz)
1226 struct thermal_cooling_device *pos = NULL;
1227 const struct thermal_zone_params *tzp = tz->tzp;
1229 if (!tzp && !tz->ops->bind)
1232 mutex_lock(&thermal_list_lock);
1234 /* If there is ops->bind, try to use ops->bind */
1235 if (tz->ops->bind) {
1236 list_for_each_entry(pos, &thermal_cdev_list, node) {
1237 ret = tz->ops->bind(tz, pos);
1239 print_bind_err_msg(tz, pos, ret);
1244 if (!tzp || !tzp->tbp)
1247 list_for_each_entry(pos, &thermal_cdev_list, node) {
1248 for (i = 0; i < tzp->num_tbps; i++) {
1249 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
1251 if (tzp->tbp[i].match(tz, pos))
1253 tzp->tbp[i].cdev = pos;
1254 __bind(tz, tzp->tbp[i].trip_mask, pos,
1255 tzp->tbp[i].binding_limits,
1256 tzp->tbp[i].weight);
1260 mutex_unlock(&thermal_list_lock);
1264 * thermal_zone_device_register() - register a new thermal zone device
1265 * @type: the thermal zone device type
1266 * @trips: the number of trip points the thermal zone support
1267 * @mask: a bit string indicating the writeablility of trip points
1268 * @devdata: private device data
1269 * @ops: standard thermal zone device callbacks
1270 * @tzp: thermal zone platform parameters
1271 * @passive_delay: number of milliseconds to wait between polls when
1272 * performing passive cooling
1273 * @polling_delay: number of milliseconds to wait between polls when checking
1274 * whether trip points have been crossed (0 for interrupt
1277 * This interface function adds a new thermal zone device (sensor) to
1278 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1279 * thermal cooling devices registered at the same time.
1280 * thermal_zone_device_unregister() must be called when the device is no
1281 * longer needed. The passive cooling depends on the .get_trend() return value.
1283 * Return: a pointer to the created struct thermal_zone_device or an
1284 * in case of error, an ERR_PTR. Caller must check return value with
1285 * IS_ERR*() helpers.
1287 struct thermal_zone_device *
1288 thermal_zone_device_register(const char *type, int trips, int mask,
1289 void *devdata, struct thermal_zone_device_ops *ops,
1290 struct thermal_zone_params *tzp, int passive_delay,
1293 struct thermal_zone_device *tz;
1294 enum thermal_trip_type trip_type;
1299 struct thermal_governor *governor;
1301 if (!type || strlen(type) == 0) {
1302 pr_err("Error: No thermal zone type defined\n");
1303 return ERR_PTR(-EINVAL);
1306 if (type && strlen(type) >= THERMAL_NAME_LENGTH) {
1307 pr_err("Error: Thermal zone name (%s) too long, should be under %d chars\n",
1308 type, THERMAL_NAME_LENGTH);
1309 return ERR_PTR(-EINVAL);
1312 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips) {
1313 pr_err("Error: Incorrect number of thermal trips\n");
1314 return ERR_PTR(-EINVAL);
1318 pr_err("Error: Thermal zone device ops not defined\n");
1319 return ERR_PTR(-EINVAL);
1322 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
1323 return ERR_PTR(-EINVAL);
1325 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
1327 return ERR_PTR(-ENOMEM);
1329 INIT_LIST_HEAD(&tz->thermal_instances);
1331 mutex_init(&tz->lock);
1332 id = ida_simple_get(&thermal_tz_ida, 0, 0, GFP_KERNEL);
1339 strlcpy(tz->type, type, sizeof(tz->type));
1342 tz->device.class = &thermal_class;
1343 tz->devdata = devdata;
1345 tz->passive_delay = passive_delay;
1346 tz->polling_delay = polling_delay;
1349 /* Add nodes that are always present via .groups */
1350 result = thermal_zone_create_device_groups(tz, mask);
1354 /* A new thermal zone needs to be updated anyway. */
1355 atomic_set(&tz->need_update, 1);
1357 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1358 result = device_register(&tz->device);
1360 goto release_device;
1362 for (count = 0; count < trips; count++) {
1363 if (tz->ops->get_trip_type(tz, count, &trip_type))
1364 set_bit(count, &tz->trips_disabled);
1365 if (tz->ops->get_trip_temp(tz, count, &trip_temp))
1366 set_bit(count, &tz->trips_disabled);
1367 /* Check for bogus trip points */
1369 set_bit(count, &tz->trips_disabled);
1372 /* Update 'this' zone's governor information */
1373 mutex_lock(&thermal_governor_lock);
1376 governor = __find_governor(tz->tzp->governor_name);
1378 governor = def_governor;
1380 result = thermal_set_governor(tz, governor);
1382 mutex_unlock(&thermal_governor_lock);
1386 mutex_unlock(&thermal_governor_lock);
1388 if (!tz->tzp || !tz->tzp->no_hwmon) {
1389 result = thermal_add_hwmon_sysfs(tz);
1394 mutex_lock(&thermal_list_lock);
1395 list_add_tail(&tz->node, &thermal_tz_list);
1396 mutex_unlock(&thermal_list_lock);
1398 /* Bind cooling devices for this zone */
1401 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
1403 thermal_zone_device_reset(tz);
1404 /* Update the new thermal zone and mark it as already updated. */
1405 if (atomic_cmpxchg(&tz->need_update, 1, 0))
1406 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1411 device_del(&tz->device);
1413 put_device(&tz->device);
1416 ida_simple_remove(&thermal_tz_ida, id);
1419 return ERR_PTR(result);
1421 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
1424 * thermal_device_unregister - removes the registered thermal zone device
1425 * @tz: the thermal zone device to remove
1427 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1430 const struct thermal_zone_params *tzp;
1431 struct thermal_cooling_device *cdev;
1432 struct thermal_zone_device *pos = NULL;
1439 mutex_lock(&thermal_list_lock);
1440 list_for_each_entry(pos, &thermal_tz_list, node)
1444 /* thermal zone device not found */
1445 mutex_unlock(&thermal_list_lock);
1448 list_del(&tz->node);
1450 /* Unbind all cdevs associated with 'this' thermal zone */
1451 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1452 if (tz->ops->unbind) {
1453 tz->ops->unbind(tz, cdev);
1457 if (!tzp || !tzp->tbp)
1460 for (i = 0; i < tzp->num_tbps; i++) {
1461 if (tzp->tbp[i].cdev == cdev) {
1462 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1463 tzp->tbp[i].cdev = NULL;
1468 mutex_unlock(&thermal_list_lock);
1470 cancel_delayed_work_sync(&tz->poll_queue);
1472 thermal_set_governor(tz, NULL);
1474 thermal_remove_hwmon_sysfs(tz);
1475 ida_simple_remove(&thermal_tz_ida, tz->id);
1476 ida_destroy(&tz->ida);
1477 mutex_destroy(&tz->lock);
1478 device_unregister(&tz->device);
1480 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1483 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1484 * @name: thermal zone name to fetch the temperature
1486 * When only one zone is found with the passed name, returns a reference to it.
1488 * Return: On success returns a reference to an unique thermal zone with
1489 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1490 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1492 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1494 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1495 unsigned int found = 0;
1500 mutex_lock(&thermal_list_lock);
1501 list_for_each_entry(pos, &thermal_tz_list, node)
1502 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1506 mutex_unlock(&thermal_list_lock);
1508 /* nothing has been found, thus an error code for it */
1510 ref = ERR_PTR(-ENODEV);
1512 /* Success only when an unique zone is found */
1513 ref = ERR_PTR(-EEXIST);
1518 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1520 static int thermal_pm_notify(struct notifier_block *nb,
1521 unsigned long mode, void *_unused)
1523 struct thermal_zone_device *tz;
1526 case PM_HIBERNATION_PREPARE:
1527 case PM_RESTORE_PREPARE:
1528 case PM_SUSPEND_PREPARE:
1529 atomic_set(&in_suspend, 1);
1531 case PM_POST_HIBERNATION:
1532 case PM_POST_RESTORE:
1533 case PM_POST_SUSPEND:
1534 atomic_set(&in_suspend, 0);
1535 list_for_each_entry(tz, &thermal_tz_list, node) {
1536 if (!thermal_zone_device_is_enabled(tz))
1539 thermal_zone_device_init(tz);
1540 thermal_zone_device_update(tz,
1541 THERMAL_EVENT_UNSPECIFIED);
1550 static struct notifier_block thermal_pm_nb = {
1551 .notifier_call = thermal_pm_notify,
1554 static int __init thermal_init(void)
1558 mutex_init(&poweroff_lock);
1559 result = thermal_register_governors();
1563 result = class_register(&thermal_class);
1565 goto unregister_governors;
1567 result = of_parse_thermal_zones();
1569 goto unregister_class;
1571 result = register_pm_notifier(&thermal_pm_nb);
1573 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1579 class_unregister(&thermal_class);
1580 unregister_governors:
1581 thermal_unregister_governors();
1583 ida_destroy(&thermal_tz_ida);
1584 ida_destroy(&thermal_cdev_ida);
1585 mutex_destroy(&thermal_list_lock);
1586 mutex_destroy(&thermal_governor_lock);
1587 mutex_destroy(&poweroff_lock);
1590 core_initcall(thermal_init);