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);
218 thermal_notify_tz_gov_change(tz->id, policy);
223 int thermal_build_list_of_policies(char *buf)
225 struct thermal_governor *pos;
228 mutex_lock(&thermal_governor_lock);
230 list_for_each_entry(pos, &thermal_governor_list, governor_list) {
231 count += scnprintf(buf + count, PAGE_SIZE - count, "%s ",
234 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
236 mutex_unlock(&thermal_governor_lock);
241 static void __init thermal_unregister_governors(void)
243 struct thermal_governor **governor;
245 for_each_governor_table(governor)
246 thermal_unregister_governor(*governor);
249 static int __init thermal_register_governors(void)
252 struct thermal_governor **governor;
254 for_each_governor_table(governor) {
255 ret = thermal_register_governor(*governor);
257 pr_err("Failed to register governor: '%s'",
262 pr_info("Registered thermal governor '%s'",
267 struct thermal_governor **gov;
269 for_each_governor_table(gov) {
272 thermal_unregister_governor(*gov);
280 * Zone update section: main control loop applied to each zone while monitoring
282 * in polling mode. The monitoring is done using a workqueue.
283 * Same update may be done on a zone by calling thermal_zone_device_update().
286 * - Non-critical trips will invoke the governor responsible for that zone;
287 * - Hot trips will produce a notification to userspace;
288 * - Critical trip point will cause a system shutdown.
290 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
294 mod_delayed_work(system_freezable_power_efficient_wq,
296 round_jiffies(msecs_to_jiffies(delay)));
298 mod_delayed_work(system_freezable_power_efficient_wq,
300 msecs_to_jiffies(delay));
302 cancel_delayed_work(&tz->poll_queue);
305 static inline bool should_stop_polling(struct thermal_zone_device *tz)
307 return !thermal_zone_device_is_enabled(tz);
310 static void monitor_thermal_zone(struct thermal_zone_device *tz)
314 stop = should_stop_polling(tz);
316 mutex_lock(&tz->lock);
318 if (!stop && tz->passive)
319 thermal_zone_device_set_polling(tz, tz->passive_delay);
320 else if (!stop && tz->polling_delay)
321 thermal_zone_device_set_polling(tz, tz->polling_delay);
323 thermal_zone_device_set_polling(tz, 0);
325 mutex_unlock(&tz->lock);
328 static void handle_non_critical_trips(struct thermal_zone_device *tz, int trip)
330 tz->governor ? tz->governor->throttle(tz, trip) :
331 def_governor->throttle(tz, trip);
335 * thermal_emergency_poweroff_func - emergency poweroff work after a known delay
336 * @work: work_struct associated with the emergency poweroff function
338 * This function is called in very critical situations to force
339 * a kernel poweroff after a configurable timeout value.
341 static void thermal_emergency_poweroff_func(struct work_struct *work)
344 * We have reached here after the emergency thermal shutdown
345 * Waiting period has expired. This means orderly_poweroff has
346 * not been able to shut off the system for some reason.
347 * Try to shut down the system immediately using kernel_power_off
350 WARN(1, "Attempting kernel_power_off: Temperature too high\n");
354 * Worst of the worst case trigger emergency restart
356 WARN(1, "Attempting emergency_restart: Temperature too high\n");
360 static DECLARE_DELAYED_WORK(thermal_emergency_poweroff_work,
361 thermal_emergency_poweroff_func);
364 * thermal_emergency_poweroff - Trigger an emergency system poweroff
366 * This may be called from any critical situation to trigger a system shutdown
367 * after a known period of time. By default this is not scheduled.
369 static void thermal_emergency_poweroff(void)
371 int poweroff_delay_ms = CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS;
373 * poweroff_delay_ms must be a carefully profiled positive value.
374 * Its a must for thermal_emergency_poweroff_work to be scheduled
376 if (poweroff_delay_ms <= 0)
378 schedule_delayed_work(&thermal_emergency_poweroff_work,
379 msecs_to_jiffies(poweroff_delay_ms));
382 static void handle_critical_trips(struct thermal_zone_device *tz,
383 int trip, enum thermal_trip_type trip_type)
387 tz->ops->get_trip_temp(tz, trip, &trip_temp);
389 /* If we have not crossed the trip_temp, we do not care. */
390 if (trip_temp <= 0 || tz->temperature < trip_temp)
393 trace_thermal_zone_trip(tz, trip, trip_type);
396 tz->ops->notify(tz, trip, trip_type);
398 if (trip_type == THERMAL_TRIP_CRITICAL) {
399 dev_emerg(&tz->device,
400 "critical temperature reached (%d C), shutting down\n",
401 tz->temperature / 1000);
402 mutex_lock(&poweroff_lock);
403 if (!power_off_triggered) {
405 * Queue a backup emergency shutdown in the event of
406 * orderly_poweroff failure
408 thermal_emergency_poweroff();
409 orderly_poweroff(true);
410 power_off_triggered = true;
412 mutex_unlock(&poweroff_lock);
416 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
418 enum thermal_trip_type type;
419 int trip_temp, hyst = 0;
421 /* Ignore disabled trip points */
422 if (test_bit(trip, &tz->trips_disabled))
425 tz->ops->get_trip_temp(tz, trip, &trip_temp);
426 tz->ops->get_trip_type(tz, trip, &type);
427 if (tz->ops->get_trip_hyst)
428 tz->ops->get_trip_hyst(tz, trip, &hyst);
430 if (tz->last_temperature != THERMAL_TEMP_INVALID) {
431 if (tz->last_temperature < trip_temp &&
432 tz->temperature >= trip_temp)
433 thermal_notify_tz_trip_up(tz->id, trip);
434 if (tz->last_temperature >= trip_temp &&
435 tz->temperature < (trip_temp - hyst))
436 thermal_notify_tz_trip_down(tz->id, trip);
439 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
440 handle_critical_trips(tz, trip, type);
442 handle_non_critical_trips(tz, trip);
444 * Alright, we handled this trip successfully.
445 * So, start monitoring again.
447 monitor_thermal_zone(tz);
450 static void update_temperature(struct thermal_zone_device *tz)
454 ret = thermal_zone_get_temp(tz, &temp);
457 dev_warn(&tz->device,
458 "failed to read out thermal zone (%d)\n",
463 mutex_lock(&tz->lock);
464 tz->last_temperature = tz->temperature;
465 tz->temperature = temp;
466 mutex_unlock(&tz->lock);
468 trace_thermal_temperature(tz);
470 thermal_genl_sampling_temp(tz->id, temp);
473 static void thermal_zone_device_init(struct thermal_zone_device *tz)
475 struct thermal_instance *pos;
476 tz->temperature = THERMAL_TEMP_INVALID;
477 tz->prev_low_trip = -INT_MAX;
478 tz->prev_high_trip = INT_MAX;
479 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
480 pos->initialized = false;
483 static void thermal_zone_device_reset(struct thermal_zone_device *tz)
486 thermal_zone_device_init(tz);
489 static int thermal_zone_device_set_mode(struct thermal_zone_device *tz,
490 enum thermal_device_mode mode)
494 mutex_lock(&tz->lock);
496 /* do nothing if mode isn't changing */
497 if (mode == tz->mode) {
498 mutex_unlock(&tz->lock);
503 if (tz->ops->change_mode)
504 ret = tz->ops->change_mode(tz, mode);
509 mutex_unlock(&tz->lock);
511 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
513 if (mode == THERMAL_DEVICE_ENABLED)
514 thermal_notify_tz_enable(tz->id);
516 thermal_notify_tz_disable(tz->id);
521 int thermal_zone_device_enable(struct thermal_zone_device *tz)
523 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_ENABLED);
525 EXPORT_SYMBOL_GPL(thermal_zone_device_enable);
527 int thermal_zone_device_disable(struct thermal_zone_device *tz)
529 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED);
531 EXPORT_SYMBOL_GPL(thermal_zone_device_disable);
533 int thermal_zone_device_is_enabled(struct thermal_zone_device *tz)
535 enum thermal_device_mode mode;
537 mutex_lock(&tz->lock);
541 mutex_unlock(&tz->lock);
543 return mode == THERMAL_DEVICE_ENABLED;
546 void thermal_zone_device_update(struct thermal_zone_device *tz,
547 enum thermal_notify_event event)
551 if (should_stop_polling(tz))
554 if (atomic_read(&in_suspend))
557 if (!tz->ops->get_temp)
560 update_temperature(tz);
562 thermal_zone_set_trips(tz);
564 tz->notify_event = event;
566 for (count = 0; count < tz->trips; count++)
567 handle_thermal_trip(tz, count);
569 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
572 * thermal_notify_framework - Sensor drivers use this API to notify framework
573 * @tz: thermal zone device
574 * @trip: indicates which trip point has been crossed
576 * This function handles the trip events from sensor drivers. It starts
577 * throttling the cooling devices according to the policy configured.
578 * For CRITICAL and HOT trip points, this notifies the respective drivers,
579 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
580 * The throttling policy is based on the configured platform data; if no
581 * platform data is provided, this uses the step_wise throttling policy.
583 void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
585 handle_thermal_trip(tz, trip);
587 EXPORT_SYMBOL_GPL(thermal_notify_framework);
589 static void thermal_zone_device_check(struct work_struct *work)
591 struct thermal_zone_device *tz = container_of(work, struct
594 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
598 * Power actor section: interface to power actors to estimate power
600 * Set of functions used to interact to cooling devices that know
601 * how to estimate their devices power consumption.
605 * power_actor_get_max_power() - get the maximum power that a cdev can consume
606 * @cdev: pointer to &thermal_cooling_device
607 * @max_power: pointer in which to store the maximum power
609 * Calculate the maximum power consumption in milliwats that the
610 * cooling device can currently consume and store it in @max_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_max_power(struct thermal_cooling_device *cdev,
618 if (!cdev_is_power_actor(cdev))
621 return cdev->ops->state2power(cdev, 0, max_power);
625 * power_actor_get_min_power() - get the mainimum power that a cdev can consume
626 * @cdev: pointer to &thermal_cooling_device
627 * @min_power: pointer in which to store the minimum power
629 * Calculate the minimum power consumption in milliwatts that the
630 * cooling device can currently consume and store it in @min_power.
632 * Return: 0 on success, -EINVAL if @cdev doesn't support the
633 * power_actor API or -E* on other error.
635 int power_actor_get_min_power(struct thermal_cooling_device *cdev,
638 unsigned long max_state;
641 if (!cdev_is_power_actor(cdev))
644 ret = cdev->ops->get_max_state(cdev, &max_state);
648 return cdev->ops->state2power(cdev, max_state, min_power);
652 * power_actor_set_power() - limit the maximum power a cooling device consumes
653 * @cdev: pointer to &thermal_cooling_device
654 * @instance: thermal instance to update
655 * @power: the power in milliwatts
657 * Set the cooling device to consume at most @power milliwatts. The limit is
658 * expected to be a cap at the maximum power consumption.
660 * Return: 0 on success, -EINVAL if the cooling device does not
661 * implement the power actor API or -E* for other failures.
663 int power_actor_set_power(struct thermal_cooling_device *cdev,
664 struct thermal_instance *instance, u32 power)
669 if (!cdev_is_power_actor(cdev))
672 ret = cdev->ops->power2state(cdev, power, &state);
676 instance->target = state;
677 mutex_lock(&cdev->lock);
678 cdev->updated = false;
679 mutex_unlock(&cdev->lock);
680 thermal_cdev_update(cdev);
685 void thermal_zone_device_rebind_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))
696 /* re binding the exception matching the type pattern */
697 thermal_zone_bind_cooling_device(tz, THERMAL_TRIPS_NONE, cdev,
700 THERMAL_WEIGHT_DEFAULT);
702 mutex_unlock(&thermal_list_lock);
705 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
708 struct thermal_governor *gov;
711 mutex_lock(&thermal_governor_lock);
712 list_for_each_entry(gov, &thermal_governor_list, governor_list) {
717 mutex_unlock(&thermal_governor_lock);
722 int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
725 struct thermal_cooling_device *cdev;
728 mutex_lock(&thermal_list_lock);
729 list_for_each_entry(cdev, &thermal_cdev_list, node) {
730 ret = cb(cdev, data);
734 mutex_unlock(&thermal_list_lock);
739 int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
742 struct thermal_zone_device *tz;
745 mutex_lock(&thermal_list_lock);
746 list_for_each_entry(tz, &thermal_tz_list, node) {
751 mutex_unlock(&thermal_list_lock);
756 struct thermal_zone_device *thermal_zone_get_by_id(int id)
758 struct thermal_zone_device *tz, *match = NULL;
760 mutex_lock(&thermal_list_lock);
761 list_for_each_entry(tz, &thermal_tz_list, node) {
767 mutex_unlock(&thermal_list_lock);
772 void thermal_zone_device_unbind_exception(struct thermal_zone_device *tz,
773 const char *cdev_type, size_t size)
775 struct thermal_cooling_device *cdev = NULL;
777 mutex_lock(&thermal_list_lock);
778 list_for_each_entry(cdev, &thermal_cdev_list, node) {
779 /* skip non matching cdevs */
780 if (strncmp(cdev_type, cdev->type, size))
782 /* unbinding the exception matching the type pattern */
783 thermal_zone_unbind_cooling_device(tz, THERMAL_TRIPS_NONE,
786 mutex_unlock(&thermal_list_lock);
790 * Device management section: cooling devices, zones devices, and binding
792 * Set of functions provided by the thermal core for:
793 * - cooling devices lifecycle: registration, unregistration,
794 * binding, and unbinding.
795 * - thermal zone devices lifecycle: registration, unregistration,
796 * binding, and unbinding.
800 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
801 * @tz: pointer to struct thermal_zone_device
802 * @trip: indicates which trip point the cooling devices is
803 * associated with in this thermal zone.
804 * @cdev: pointer to struct thermal_cooling_device
805 * @upper: the Maximum cooling state for this trip point.
806 * THERMAL_NO_LIMIT means no upper limit,
807 * and the cooling device can be in max_state.
808 * @lower: the Minimum cooling state can be used for this trip point.
809 * THERMAL_NO_LIMIT means no lower limit,
810 * and the cooling device can be in cooling state 0.
811 * @weight: The weight of the cooling device to be bound to the
812 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
815 * This interface function bind a thermal cooling device to the certain trip
816 * point of a thermal zone device.
817 * This function is usually called in the thermal zone device .bind callback.
819 * Return: 0 on success, the proper error value otherwise.
821 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
823 struct thermal_cooling_device *cdev,
824 unsigned long upper, unsigned long lower,
827 struct thermal_instance *dev;
828 struct thermal_instance *pos;
829 struct thermal_zone_device *pos1;
830 struct thermal_cooling_device *pos2;
831 unsigned long max_state;
834 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
837 list_for_each_entry(pos1, &thermal_tz_list, node) {
841 list_for_each_entry(pos2, &thermal_cdev_list, node) {
846 if (tz != pos1 || cdev != pos2)
849 ret = cdev->ops->get_max_state(cdev, &max_state);
853 /* lower default 0, upper default max_state */
854 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
855 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
857 if (lower > upper || upper > max_state)
860 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
868 dev->target = THERMAL_NO_TARGET;
869 dev->weight = weight;
871 result = ida_simple_get(&tz->ida, 0, 0, GFP_KERNEL);
876 sprintf(dev->name, "cdev%d", dev->id);
878 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
882 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
883 sysfs_attr_init(&dev->attr.attr);
884 dev->attr.attr.name = dev->attr_name;
885 dev->attr.attr.mode = 0444;
886 dev->attr.show = trip_point_show;
887 result = device_create_file(&tz->device, &dev->attr);
889 goto remove_symbol_link;
891 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
892 sysfs_attr_init(&dev->weight_attr.attr);
893 dev->weight_attr.attr.name = dev->weight_attr_name;
894 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
895 dev->weight_attr.show = weight_show;
896 dev->weight_attr.store = weight_store;
897 result = device_create_file(&tz->device, &dev->weight_attr);
899 goto remove_trip_file;
901 mutex_lock(&tz->lock);
902 mutex_lock(&cdev->lock);
903 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
904 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
909 list_add_tail(&dev->tz_node, &tz->thermal_instances);
910 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
911 atomic_set(&tz->need_update, 1);
913 mutex_unlock(&cdev->lock);
914 mutex_unlock(&tz->lock);
919 device_remove_file(&tz->device, &dev->weight_attr);
921 device_remove_file(&tz->device, &dev->attr);
923 sysfs_remove_link(&tz->device.kobj, dev->name);
925 ida_simple_remove(&tz->ida, dev->id);
930 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
933 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
935 * @tz: pointer to a struct thermal_zone_device.
936 * @trip: indicates which trip point the cooling devices is
937 * associated with in this thermal zone.
938 * @cdev: pointer to a struct thermal_cooling_device.
940 * This interface function unbind a thermal cooling device from the certain
941 * trip point of a thermal zone device.
942 * This function is usually called in the thermal zone device .unbind callback.
944 * Return: 0 on success, the proper error value otherwise.
946 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
948 struct thermal_cooling_device *cdev)
950 struct thermal_instance *pos, *next;
952 mutex_lock(&tz->lock);
953 mutex_lock(&cdev->lock);
954 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
955 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
956 list_del(&pos->tz_node);
957 list_del(&pos->cdev_node);
958 mutex_unlock(&cdev->lock);
959 mutex_unlock(&tz->lock);
963 mutex_unlock(&cdev->lock);
964 mutex_unlock(&tz->lock);
969 device_remove_file(&tz->device, &pos->weight_attr);
970 device_remove_file(&tz->device, &pos->attr);
971 sysfs_remove_link(&tz->device.kobj, pos->name);
972 ida_simple_remove(&tz->ida, pos->id);
976 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
978 static void thermal_release(struct device *dev)
980 struct thermal_zone_device *tz;
981 struct thermal_cooling_device *cdev;
983 if (!strncmp(dev_name(dev), "thermal_zone",
984 sizeof("thermal_zone") - 1)) {
985 tz = to_thermal_zone(dev);
986 thermal_zone_destroy_device_groups(tz);
988 } else if (!strncmp(dev_name(dev), "cooling_device",
989 sizeof("cooling_device") - 1)) {
990 cdev = to_cooling_device(dev);
995 static struct class thermal_class = {
997 .dev_release = thermal_release,
1001 void print_bind_err_msg(struct thermal_zone_device *tz,
1002 struct thermal_cooling_device *cdev, int ret)
1004 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
1005 tz->type, cdev->type, ret);
1008 static void __bind(struct thermal_zone_device *tz, int mask,
1009 struct thermal_cooling_device *cdev,
1010 unsigned long *limits,
1011 unsigned int weight)
1015 for (i = 0; i < tz->trips; i++) {
1016 if (mask & (1 << i)) {
1017 unsigned long upper, lower;
1019 upper = THERMAL_NO_LIMIT;
1020 lower = THERMAL_NO_LIMIT;
1022 lower = limits[i * 2];
1023 upper = limits[i * 2 + 1];
1025 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
1029 print_bind_err_msg(tz, cdev, ret);
1034 static void bind_cdev(struct thermal_cooling_device *cdev)
1037 const struct thermal_zone_params *tzp;
1038 struct thermal_zone_device *pos = NULL;
1040 mutex_lock(&thermal_list_lock);
1042 list_for_each_entry(pos, &thermal_tz_list, node) {
1043 if (!pos->tzp && !pos->ops->bind)
1046 if (pos->ops->bind) {
1047 ret = pos->ops->bind(pos, cdev);
1049 print_bind_err_msg(pos, cdev, ret);
1054 if (!tzp || !tzp->tbp)
1057 for (i = 0; i < tzp->num_tbps; i++) {
1058 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
1060 if (tzp->tbp[i].match(pos, cdev))
1062 tzp->tbp[i].cdev = cdev;
1063 __bind(pos, tzp->tbp[i].trip_mask, cdev,
1064 tzp->tbp[i].binding_limits,
1065 tzp->tbp[i].weight);
1069 mutex_unlock(&thermal_list_lock);
1073 * __thermal_cooling_device_register() - register a new thermal cooling device
1074 * @np: a pointer to a device tree node.
1075 * @type: the thermal cooling device type.
1076 * @devdata: device private data.
1077 * @ops: standard thermal cooling devices callbacks.
1079 * This interface function adds a new thermal cooling device (fan/processor/...)
1080 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1081 * to all the thermal zone devices registered at the same time.
1082 * It also gives the opportunity to link the cooling device to a device tree
1083 * node, so that it can be bound to a thermal zone created out of device tree.
1085 * Return: a pointer to the created struct thermal_cooling_device or an
1086 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1088 static struct thermal_cooling_device *
1089 __thermal_cooling_device_register(struct device_node *np,
1090 const char *type, void *devdata,
1091 const struct thermal_cooling_device_ops *ops)
1093 struct thermal_cooling_device *cdev;
1094 struct thermal_zone_device *pos = NULL;
1097 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
1098 !ops->set_cur_state)
1099 return ERR_PTR(-EINVAL);
1101 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
1103 return ERR_PTR(-ENOMEM);
1105 ret = ida_simple_get(&thermal_cdev_ida, 0, 0, GFP_KERNEL);
1107 goto out_kfree_cdev;
1111 cdev->type = kstrdup(type ? type : "", GFP_KERNEL);
1114 goto out_ida_remove;
1117 mutex_init(&cdev->lock);
1118 INIT_LIST_HEAD(&cdev->thermal_instances);
1121 cdev->updated = false;
1122 cdev->device.class = &thermal_class;
1123 cdev->devdata = devdata;
1124 thermal_cooling_device_setup_sysfs(cdev);
1125 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
1126 ret = device_register(&cdev->device);
1128 goto out_kfree_type;
1130 /* Add 'this' new cdev to the global cdev list */
1131 mutex_lock(&thermal_list_lock);
1132 list_add(&cdev->node, &thermal_cdev_list);
1133 mutex_unlock(&thermal_list_lock);
1135 /* Update binding information for 'this' new cdev */
1138 mutex_lock(&thermal_list_lock);
1139 list_for_each_entry(pos, &thermal_tz_list, node)
1140 if (atomic_cmpxchg(&pos->need_update, 1, 0))
1141 thermal_zone_device_update(pos,
1142 THERMAL_EVENT_UNSPECIFIED);
1143 mutex_unlock(&thermal_list_lock);
1148 thermal_cooling_device_destroy_sysfs(cdev);
1150 put_device(&cdev->device);
1153 ida_simple_remove(&thermal_cdev_ida, id);
1156 return ERR_PTR(ret);
1160 * thermal_cooling_device_register() - register a new thermal cooling device
1161 * @type: the thermal cooling device type.
1162 * @devdata: device private data.
1163 * @ops: standard thermal cooling devices callbacks.
1165 * This interface function adds a new thermal cooling device (fan/processor/...)
1166 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1167 * to all the thermal zone devices registered at the same time.
1169 * Return: a pointer to the created struct thermal_cooling_device or an
1170 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1172 struct thermal_cooling_device *
1173 thermal_cooling_device_register(const char *type, void *devdata,
1174 const struct thermal_cooling_device_ops *ops)
1176 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1178 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
1181 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1182 * @np: a pointer to a device tree node.
1183 * @type: the thermal cooling device type.
1184 * @devdata: device private data.
1185 * @ops: standard thermal cooling devices callbacks.
1187 * This function will register a cooling device with device tree node reference.
1188 * This interface function adds a new thermal cooling device (fan/processor/...)
1189 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1190 * to all the thermal zone devices registered at the same time.
1192 * Return: a pointer to the created struct thermal_cooling_device or an
1193 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1195 struct thermal_cooling_device *
1196 thermal_of_cooling_device_register(struct device_node *np,
1197 const char *type, void *devdata,
1198 const struct thermal_cooling_device_ops *ops)
1200 return __thermal_cooling_device_register(np, type, devdata, ops);
1202 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1204 static void thermal_cooling_device_release(struct device *dev, void *res)
1206 thermal_cooling_device_unregister(
1207 *(struct thermal_cooling_device **)res);
1211 * devm_thermal_of_cooling_device_register() - register an OF thermal cooling
1213 * @dev: a valid struct device pointer of a sensor device.
1214 * @np: a pointer to a device tree node.
1215 * @type: the thermal cooling device type.
1216 * @devdata: device private data.
1217 * @ops: standard thermal cooling devices callbacks.
1219 * This function will register a cooling device with device tree node reference.
1220 * This interface function adds a new thermal cooling device (fan/processor/...)
1221 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1222 * to all the thermal zone devices registered at the same time.
1224 * Return: a pointer to the created struct thermal_cooling_device or an
1225 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1227 struct thermal_cooling_device *
1228 devm_thermal_of_cooling_device_register(struct device *dev,
1229 struct device_node *np,
1230 char *type, void *devdata,
1231 const struct thermal_cooling_device_ops *ops)
1233 struct thermal_cooling_device **ptr, *tcd;
1235 ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr),
1238 return ERR_PTR(-ENOMEM);
1240 tcd = __thermal_cooling_device_register(np, type, devdata, ops);
1247 devres_add(dev, ptr);
1251 EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register);
1253 static void __unbind(struct thermal_zone_device *tz, int mask,
1254 struct thermal_cooling_device *cdev)
1258 for (i = 0; i < tz->trips; i++)
1259 if (mask & (1 << i))
1260 thermal_zone_unbind_cooling_device(tz, i, cdev);
1264 * thermal_cooling_device_unregister - removes a thermal cooling device
1265 * @cdev: the thermal cooling device to remove.
1267 * thermal_cooling_device_unregister() must be called when a registered
1268 * thermal cooling device is no longer needed.
1270 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1273 const struct thermal_zone_params *tzp;
1274 struct thermal_zone_device *tz;
1275 struct thermal_cooling_device *pos = NULL;
1280 mutex_lock(&thermal_list_lock);
1281 list_for_each_entry(pos, &thermal_cdev_list, node)
1285 /* thermal cooling device not found */
1286 mutex_unlock(&thermal_list_lock);
1289 list_del(&cdev->node);
1291 /* Unbind all thermal zones associated with 'this' cdev */
1292 list_for_each_entry(tz, &thermal_tz_list, node) {
1293 if (tz->ops->unbind) {
1294 tz->ops->unbind(tz, cdev);
1298 if (!tz->tzp || !tz->tzp->tbp)
1302 for (i = 0; i < tzp->num_tbps; i++) {
1303 if (tzp->tbp[i].cdev == cdev) {
1304 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1305 tzp->tbp[i].cdev = NULL;
1310 mutex_unlock(&thermal_list_lock);
1312 ida_simple_remove(&thermal_cdev_ida, cdev->id);
1313 device_del(&cdev->device);
1314 thermal_cooling_device_destroy_sysfs(cdev);
1316 put_device(&cdev->device);
1318 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1320 static void bind_tz(struct thermal_zone_device *tz)
1323 struct thermal_cooling_device *pos = NULL;
1324 const struct thermal_zone_params *tzp = tz->tzp;
1326 if (!tzp && !tz->ops->bind)
1329 mutex_lock(&thermal_list_lock);
1331 /* If there is ops->bind, try to use ops->bind */
1332 if (tz->ops->bind) {
1333 list_for_each_entry(pos, &thermal_cdev_list, node) {
1334 ret = tz->ops->bind(tz, pos);
1336 print_bind_err_msg(tz, pos, ret);
1341 if (!tzp || !tzp->tbp)
1344 list_for_each_entry(pos, &thermal_cdev_list, node) {
1345 for (i = 0; i < tzp->num_tbps; i++) {
1346 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
1348 if (tzp->tbp[i].match(tz, pos))
1350 tzp->tbp[i].cdev = pos;
1351 __bind(tz, tzp->tbp[i].trip_mask, pos,
1352 tzp->tbp[i].binding_limits,
1353 tzp->tbp[i].weight);
1357 mutex_unlock(&thermal_list_lock);
1361 * thermal_zone_device_register() - register a new thermal zone device
1362 * @type: the thermal zone device type
1363 * @trips: the number of trip points the thermal zone support
1364 * @mask: a bit string indicating the writeablility of trip points
1365 * @devdata: private device data
1366 * @ops: standard thermal zone device callbacks
1367 * @tzp: thermal zone platform parameters
1368 * @passive_delay: number of milliseconds to wait between polls when
1369 * performing passive cooling
1370 * @polling_delay: number of milliseconds to wait between polls when checking
1371 * whether trip points have been crossed (0 for interrupt
1374 * This interface function adds a new thermal zone device (sensor) to
1375 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1376 * thermal cooling devices registered at the same time.
1377 * thermal_zone_device_unregister() must be called when the device is no
1378 * longer needed. The passive cooling depends on the .get_trend() return value.
1380 * Return: a pointer to the created struct thermal_zone_device or an
1381 * in case of error, an ERR_PTR. Caller must check return value with
1382 * IS_ERR*() helpers.
1384 struct thermal_zone_device *
1385 thermal_zone_device_register(const char *type, int trips, int mask,
1386 void *devdata, struct thermal_zone_device_ops *ops,
1387 struct thermal_zone_params *tzp, int passive_delay,
1390 struct thermal_zone_device *tz;
1391 enum thermal_trip_type trip_type;
1396 struct thermal_governor *governor;
1398 if (!type || strlen(type) == 0) {
1399 pr_err("Error: No thermal zone type defined\n");
1400 return ERR_PTR(-EINVAL);
1403 if (type && strlen(type) >= THERMAL_NAME_LENGTH) {
1404 pr_err("Error: Thermal zone name (%s) too long, should be under %d chars\n",
1405 type, THERMAL_NAME_LENGTH);
1406 return ERR_PTR(-EINVAL);
1409 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips) {
1410 pr_err("Error: Incorrect number of thermal trips\n");
1411 return ERR_PTR(-EINVAL);
1415 pr_err("Error: Thermal zone device ops not defined\n");
1416 return ERR_PTR(-EINVAL);
1419 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
1420 return ERR_PTR(-EINVAL);
1422 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
1424 return ERR_PTR(-ENOMEM);
1426 INIT_LIST_HEAD(&tz->thermal_instances);
1428 mutex_init(&tz->lock);
1429 id = ida_simple_get(&thermal_tz_ida, 0, 0, GFP_KERNEL);
1436 strlcpy(tz->type, type, sizeof(tz->type));
1439 tz->device.class = &thermal_class;
1440 tz->devdata = devdata;
1442 tz->passive_delay = passive_delay;
1443 tz->polling_delay = polling_delay;
1446 /* Add nodes that are always present via .groups */
1447 result = thermal_zone_create_device_groups(tz, mask);
1451 /* A new thermal zone needs to be updated anyway. */
1452 atomic_set(&tz->need_update, 1);
1454 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1455 result = device_register(&tz->device);
1457 goto release_device;
1459 for (count = 0; count < trips; count++) {
1460 if (tz->ops->get_trip_type(tz, count, &trip_type))
1461 set_bit(count, &tz->trips_disabled);
1462 if (tz->ops->get_trip_temp(tz, count, &trip_temp))
1463 set_bit(count, &tz->trips_disabled);
1464 /* Check for bogus trip points */
1466 set_bit(count, &tz->trips_disabled);
1469 /* Update 'this' zone's governor information */
1470 mutex_lock(&thermal_governor_lock);
1473 governor = __find_governor(tz->tzp->governor_name);
1475 governor = def_governor;
1477 result = thermal_set_governor(tz, governor);
1479 mutex_unlock(&thermal_governor_lock);
1483 mutex_unlock(&thermal_governor_lock);
1485 if (!tz->tzp || !tz->tzp->no_hwmon) {
1486 result = thermal_add_hwmon_sysfs(tz);
1491 mutex_lock(&thermal_list_lock);
1492 list_add_tail(&tz->node, &thermal_tz_list);
1493 mutex_unlock(&thermal_list_lock);
1495 /* Bind cooling devices for this zone */
1498 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
1500 thermal_zone_device_reset(tz);
1501 /* Update the new thermal zone and mark it as already updated. */
1502 if (atomic_cmpxchg(&tz->need_update, 1, 0))
1503 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1505 thermal_notify_tz_create(tz->id, tz->type);
1510 device_del(&tz->device);
1512 put_device(&tz->device);
1515 ida_simple_remove(&thermal_tz_ida, id);
1518 return ERR_PTR(result);
1520 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
1523 * thermal_zone_device_unregister - removes the registered thermal zone device
1524 * @tz: the thermal zone device to remove
1526 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1529 const struct thermal_zone_params *tzp;
1530 struct thermal_cooling_device *cdev;
1531 struct thermal_zone_device *pos = NULL;
1539 mutex_lock(&thermal_list_lock);
1540 list_for_each_entry(pos, &thermal_tz_list, node)
1544 /* thermal zone device not found */
1545 mutex_unlock(&thermal_list_lock);
1548 list_del(&tz->node);
1550 /* Unbind all cdevs associated with 'this' thermal zone */
1551 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1552 if (tz->ops->unbind) {
1553 tz->ops->unbind(tz, cdev);
1557 if (!tzp || !tzp->tbp)
1560 for (i = 0; i < tzp->num_tbps; i++) {
1561 if (tzp->tbp[i].cdev == cdev) {
1562 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1563 tzp->tbp[i].cdev = NULL;
1568 mutex_unlock(&thermal_list_lock);
1570 cancel_delayed_work_sync(&tz->poll_queue);
1572 thermal_set_governor(tz, NULL);
1574 thermal_remove_hwmon_sysfs(tz);
1575 ida_simple_remove(&thermal_tz_ida, tz->id);
1576 ida_destroy(&tz->ida);
1577 mutex_destroy(&tz->lock);
1578 device_unregister(&tz->device);
1580 thermal_notify_tz_delete(tz_id);
1582 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1585 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1586 * @name: thermal zone name to fetch the temperature
1588 * When only one zone is found with the passed name, returns a reference to it.
1590 * Return: On success returns a reference to an unique thermal zone with
1591 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1592 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1594 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1596 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1597 unsigned int found = 0;
1602 mutex_lock(&thermal_list_lock);
1603 list_for_each_entry(pos, &thermal_tz_list, node)
1604 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1608 mutex_unlock(&thermal_list_lock);
1610 /* nothing has been found, thus an error code for it */
1612 ref = ERR_PTR(-ENODEV);
1614 /* Success only when an unique zone is found */
1615 ref = ERR_PTR(-EEXIST);
1620 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1622 static int thermal_pm_notify(struct notifier_block *nb,
1623 unsigned long mode, void *_unused)
1625 struct thermal_zone_device *tz;
1628 case PM_HIBERNATION_PREPARE:
1629 case PM_RESTORE_PREPARE:
1630 case PM_SUSPEND_PREPARE:
1631 atomic_set(&in_suspend, 1);
1633 case PM_POST_HIBERNATION:
1634 case PM_POST_RESTORE:
1635 case PM_POST_SUSPEND:
1636 atomic_set(&in_suspend, 0);
1637 list_for_each_entry(tz, &thermal_tz_list, node) {
1638 if (!thermal_zone_device_is_enabled(tz))
1641 thermal_zone_device_init(tz);
1642 thermal_zone_device_update(tz,
1643 THERMAL_EVENT_UNSPECIFIED);
1652 static struct notifier_block thermal_pm_nb = {
1653 .notifier_call = thermal_pm_notify,
1656 static int __init thermal_init(void)
1660 result = thermal_netlink_init();
1664 result = thermal_register_governors();
1668 result = class_register(&thermal_class);
1670 goto unregister_governors;
1672 result = of_parse_thermal_zones();
1674 goto unregister_class;
1676 result = register_pm_notifier(&thermal_pm_nb);
1678 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1684 class_unregister(&thermal_class);
1685 unregister_governors:
1686 thermal_unregister_governors();
1688 ida_destroy(&thermal_tz_ida);
1689 ida_destroy(&thermal_cdev_ida);
1690 mutex_destroy(&thermal_list_lock);
1691 mutex_destroy(&thermal_governor_lock);
1692 mutex_destroy(&poweroff_lock);
1695 postcore_initcall(thermal_init);