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/module.h>
13 #include <linux/device.h>
14 #include <linux/err.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 MODULE_AUTHOR("Zhang Rui");
31 MODULE_DESCRIPTION("Generic thermal management sysfs support");
32 MODULE_LICENSE("GPL v2");
34 static DEFINE_IDA(thermal_tz_ida);
35 static DEFINE_IDA(thermal_cdev_ida);
37 static LIST_HEAD(thermal_tz_list);
38 static LIST_HEAD(thermal_cdev_list);
39 static LIST_HEAD(thermal_governor_list);
41 static DEFINE_MUTEX(thermal_list_lock);
42 static DEFINE_MUTEX(thermal_governor_lock);
43 static DEFINE_MUTEX(poweroff_lock);
45 static atomic_t in_suspend;
46 static bool power_off_triggered;
48 static struct thermal_governor *def_governor;
51 * Governor section: set of functions to handle thermal governors
53 * Functions to help in the life cycle of thermal governors within
54 * the thermal core and by the thermal governor code.
57 static struct thermal_governor *__find_governor(const char *name)
59 struct thermal_governor *pos;
61 if (!name || !name[0])
64 list_for_each_entry(pos, &thermal_governor_list, governor_list)
65 if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH))
72 * bind_previous_governor() - bind the previous governor of the thermal zone
73 * @tz: a valid pointer to a struct thermal_zone_device
74 * @failed_gov_name: the name of the governor that failed to register
76 * Register the previous governor of the thermal zone after a new
77 * governor has failed to be bound.
79 static void bind_previous_governor(struct thermal_zone_device *tz,
80 const char *failed_gov_name)
82 if (tz->governor && tz->governor->bind_to_tz) {
83 if (tz->governor->bind_to_tz(tz)) {
85 "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n",
86 failed_gov_name, tz->governor->name, tz->type);
93 * thermal_set_governor() - Switch to another governor
94 * @tz: a valid pointer to a struct thermal_zone_device
95 * @new_gov: pointer to the new governor
97 * Change the governor of thermal zone @tz.
99 * Return: 0 on success, an error if the new governor's bind_to_tz() failed.
101 static int thermal_set_governor(struct thermal_zone_device *tz,
102 struct thermal_governor *new_gov)
106 if (tz->governor && tz->governor->unbind_from_tz)
107 tz->governor->unbind_from_tz(tz);
109 if (new_gov && new_gov->bind_to_tz) {
110 ret = new_gov->bind_to_tz(tz);
112 bind_previous_governor(tz, new_gov->name);
118 tz->governor = new_gov;
123 int thermal_register_governor(struct thermal_governor *governor)
127 struct thermal_zone_device *pos;
132 mutex_lock(&thermal_governor_lock);
135 if (!__find_governor(governor->name)) {
139 list_add(&governor->governor_list, &thermal_governor_list);
140 match_default = !strncmp(governor->name,
141 DEFAULT_THERMAL_GOVERNOR,
142 THERMAL_NAME_LENGTH);
144 if (!def_governor && match_default)
145 def_governor = governor;
148 mutex_lock(&thermal_list_lock);
150 list_for_each_entry(pos, &thermal_tz_list, node) {
152 * only thermal zones with specified tz->tzp->governor_name
153 * may run with tz->govenor unset
158 name = pos->tzp->governor_name;
160 if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) {
163 ret = thermal_set_governor(pos, governor);
165 dev_err(&pos->device,
166 "Failed to set governor %s for thermal zone %s: %d\n",
167 governor->name, pos->type, ret);
171 mutex_unlock(&thermal_list_lock);
172 mutex_unlock(&thermal_governor_lock);
177 void thermal_unregister_governor(struct thermal_governor *governor)
179 struct thermal_zone_device *pos;
184 mutex_lock(&thermal_governor_lock);
186 if (!__find_governor(governor->name))
189 mutex_lock(&thermal_list_lock);
191 list_for_each_entry(pos, &thermal_tz_list, node) {
192 if (!strncasecmp(pos->governor->name, governor->name,
193 THERMAL_NAME_LENGTH))
194 thermal_set_governor(pos, NULL);
197 mutex_unlock(&thermal_list_lock);
198 list_del(&governor->governor_list);
200 mutex_unlock(&thermal_governor_lock);
203 int thermal_zone_device_set_policy(struct thermal_zone_device *tz,
206 struct thermal_governor *gov;
209 mutex_lock(&thermal_governor_lock);
210 mutex_lock(&tz->lock);
212 gov = __find_governor(strim(policy));
216 ret = thermal_set_governor(tz, gov);
219 mutex_unlock(&tz->lock);
220 mutex_unlock(&thermal_governor_lock);
225 int thermal_build_list_of_policies(char *buf)
227 struct thermal_governor *pos;
229 ssize_t size = PAGE_SIZE;
231 mutex_lock(&thermal_governor_lock);
233 list_for_each_entry(pos, &thermal_governor_list, governor_list) {
234 size = PAGE_SIZE - count;
235 count += scnprintf(buf + count, size, "%s ", pos->name);
237 count += scnprintf(buf + count, size, "\n");
239 mutex_unlock(&thermal_governor_lock);
244 static void __init thermal_unregister_governors(void)
246 struct thermal_governor **governor;
248 for_each_governor_table(governor)
249 thermal_unregister_governor(*governor);
252 static int __init thermal_register_governors(void)
255 struct thermal_governor **governor;
257 for_each_governor_table(governor) {
258 ret = thermal_register_governor(*governor);
260 pr_err("Failed to register governor: '%s'",
265 pr_info("Registered thermal governor '%s'",
270 struct thermal_governor **gov;
272 for_each_governor_table(gov) {
275 thermal_unregister_governor(*gov);
283 * Zone update section: main control loop applied to each zone while monitoring
285 * in polling mode. The monitoring is done using a workqueue.
286 * Same update may be done on a zone by calling thermal_zone_device_update().
289 * - Non-critical trips will invoke the governor responsible for that zone;
290 * - Hot trips will produce a notification to userspace;
291 * - Critical trip point will cause a system shutdown.
293 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
297 mod_delayed_work(system_freezable_power_efficient_wq,
299 round_jiffies(msecs_to_jiffies(delay)));
301 mod_delayed_work(system_freezable_power_efficient_wq,
303 msecs_to_jiffies(delay));
305 cancel_delayed_work(&tz->poll_queue);
308 static void monitor_thermal_zone(struct thermal_zone_device *tz)
310 mutex_lock(&tz->lock);
313 thermal_zone_device_set_polling(tz, tz->passive_delay);
314 else if (tz->polling_delay)
315 thermal_zone_device_set_polling(tz, tz->polling_delay);
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);
329 * thermal_emergency_poweroff_func - emergency poweroff work after a known delay
330 * @work: work_struct associated with the emergency poweroff function
332 * This function is called in very critical situations to force
333 * a kernel poweroff after a configurable timeout value.
335 static void thermal_emergency_poweroff_func(struct work_struct *work)
338 * We have reached here after the emergency thermal shutdown
339 * Waiting period has expired. This means orderly_poweroff has
340 * not been able to shut off the system for some reason.
341 * Try to shut down the system immediately using kernel_power_off
344 WARN(1, "Attempting kernel_power_off: Temperature too high\n");
348 * Worst of the worst case trigger emergency restart
350 WARN(1, "Attempting emergency_restart: Temperature too high\n");
354 static DECLARE_DELAYED_WORK(thermal_emergency_poweroff_work,
355 thermal_emergency_poweroff_func);
358 * thermal_emergency_poweroff - Trigger an emergency system poweroff
360 * This may be called from any critical situation to trigger a system shutdown
361 * after a known period of time. By default this is not scheduled.
363 static void thermal_emergency_poweroff(void)
365 int poweroff_delay_ms = CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS;
367 * poweroff_delay_ms must be a carefully profiled positive value.
368 * Its a must for thermal_emergency_poweroff_work to be scheduled
370 if (poweroff_delay_ms <= 0)
372 schedule_delayed_work(&thermal_emergency_poweroff_work,
373 msecs_to_jiffies(poweroff_delay_ms));
376 static void handle_critical_trips(struct thermal_zone_device *tz,
377 int trip, enum thermal_trip_type trip_type)
381 tz->ops->get_trip_temp(tz, trip, &trip_temp);
383 /* If we have not crossed the trip_temp, we do not care. */
384 if (trip_temp <= 0 || tz->temperature < trip_temp)
387 trace_thermal_zone_trip(tz, trip, trip_type);
390 tz->ops->notify(tz, trip, trip_type);
392 if (trip_type == THERMAL_TRIP_CRITICAL) {
393 dev_emerg(&tz->device,
394 "critical temperature reached (%d C), shutting down\n",
395 tz->temperature / 1000);
396 mutex_lock(&poweroff_lock);
397 if (!power_off_triggered) {
399 * Queue a backup emergency shutdown in the event of
400 * orderly_poweroff failure
402 thermal_emergency_poweroff();
403 orderly_poweroff(true);
404 power_off_triggered = true;
406 mutex_unlock(&poweroff_lock);
410 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
412 enum thermal_trip_type type;
414 /* Ignore disabled trip points */
415 if (test_bit(trip, &tz->trips_disabled))
418 tz->ops->get_trip_type(tz, trip, &type);
420 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
421 handle_critical_trips(tz, trip, type);
423 handle_non_critical_trips(tz, trip);
425 * Alright, we handled this trip successfully.
426 * So, start monitoring again.
428 monitor_thermal_zone(tz);
431 static void update_temperature(struct thermal_zone_device *tz)
435 ret = thermal_zone_get_temp(tz, &temp);
438 dev_warn(&tz->device,
439 "failed to read out thermal zone (%d)\n",
444 mutex_lock(&tz->lock);
445 tz->last_temperature = tz->temperature;
446 tz->temperature = temp;
447 mutex_unlock(&tz->lock);
449 trace_thermal_temperature(tz);
452 static void thermal_zone_device_init(struct thermal_zone_device *tz)
454 struct thermal_instance *pos;
455 tz->temperature = THERMAL_TEMP_INVALID;
456 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
457 pos->initialized = false;
460 static void thermal_zone_device_reset(struct thermal_zone_device *tz)
463 thermal_zone_device_init(tz);
466 void thermal_zone_device_update(struct thermal_zone_device *tz,
467 enum thermal_notify_event event)
471 if (atomic_read(&in_suspend))
474 if (!tz->ops->get_temp)
477 update_temperature(tz);
479 thermal_zone_set_trips(tz);
481 tz->notify_event = event;
483 for (count = 0; count < tz->trips; count++)
484 handle_thermal_trip(tz, count);
486 EXPORT_SYMBOL_GPL(thermal_zone_device_update);
489 * thermal_notify_framework - Sensor drivers use this API to notify framework
490 * @tz: thermal zone device
491 * @trip: indicates which trip point has been crossed
493 * This function handles the trip events from sensor drivers. It starts
494 * throttling the cooling devices according to the policy configured.
495 * For CRITICAL and HOT trip points, this notifies the respective drivers,
496 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
497 * The throttling policy is based on the configured platform data; if no
498 * platform data is provided, this uses the step_wise throttling policy.
500 void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
502 handle_thermal_trip(tz, trip);
504 EXPORT_SYMBOL_GPL(thermal_notify_framework);
506 static void thermal_zone_device_check(struct work_struct *work)
508 struct thermal_zone_device *tz = container_of(work, struct
511 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
515 * Power actor section: interface to power actors to estimate power
517 * Set of functions used to interact to cooling devices that know
518 * how to estimate their devices power consumption.
522 * power_actor_get_max_power() - get the maximum power that a cdev can consume
523 * @cdev: pointer to &thermal_cooling_device
524 * @tz: a valid thermal zone device pointer
525 * @max_power: pointer in which to store the maximum power
527 * Calculate the maximum power consumption in milliwats that the
528 * cooling device can currently consume and store it in @max_power.
530 * Return: 0 on success, -EINVAL if @cdev doesn't support the
531 * power_actor API or -E* on other error.
533 int power_actor_get_max_power(struct thermal_cooling_device *cdev,
534 struct thermal_zone_device *tz, u32 *max_power)
536 if (!cdev_is_power_actor(cdev))
539 return cdev->ops->state2power(cdev, tz, 0, max_power);
543 * power_actor_get_min_power() - get the mainimum power that a cdev can consume
544 * @cdev: pointer to &thermal_cooling_device
545 * @tz: a valid thermal zone device pointer
546 * @min_power: pointer in which to store the minimum power
548 * Calculate the minimum power consumption in milliwatts that the
549 * cooling device can currently consume and store it in @min_power.
551 * Return: 0 on success, -EINVAL if @cdev doesn't support the
552 * power_actor API or -E* on other error.
554 int power_actor_get_min_power(struct thermal_cooling_device *cdev,
555 struct thermal_zone_device *tz, u32 *min_power)
557 unsigned long max_state;
560 if (!cdev_is_power_actor(cdev))
563 ret = cdev->ops->get_max_state(cdev, &max_state);
567 return cdev->ops->state2power(cdev, tz, max_state, min_power);
571 * power_actor_set_power() - limit the maximum power a cooling device consumes
572 * @cdev: pointer to &thermal_cooling_device
573 * @instance: thermal instance to update
574 * @power: the power in milliwatts
576 * Set the cooling device to consume at most @power milliwatts. The limit is
577 * expected to be a cap at the maximum power consumption.
579 * Return: 0 on success, -EINVAL if the cooling device does not
580 * implement the power actor API or -E* for other failures.
582 int power_actor_set_power(struct thermal_cooling_device *cdev,
583 struct thermal_instance *instance, u32 power)
588 if (!cdev_is_power_actor(cdev))
591 ret = cdev->ops->power2state(cdev, instance->tz, power, &state);
595 instance->target = state;
596 mutex_lock(&cdev->lock);
597 cdev->updated = false;
598 mutex_unlock(&cdev->lock);
599 thermal_cdev_update(cdev);
604 void thermal_zone_device_rebind_exception(struct thermal_zone_device *tz,
605 const char *cdev_type, size_t size)
607 struct thermal_cooling_device *cdev = NULL;
609 mutex_lock(&thermal_list_lock);
610 list_for_each_entry(cdev, &thermal_cdev_list, node) {
611 /* skip non matching cdevs */
612 if (strncmp(cdev_type, cdev->type, size))
615 /* re binding the exception matching the type pattern */
616 thermal_zone_bind_cooling_device(tz, THERMAL_TRIPS_NONE, cdev,
619 THERMAL_WEIGHT_DEFAULT);
621 mutex_unlock(&thermal_list_lock);
624 void thermal_zone_device_unbind_exception(struct thermal_zone_device *tz,
625 const char *cdev_type, size_t size)
627 struct thermal_cooling_device *cdev = NULL;
629 mutex_lock(&thermal_list_lock);
630 list_for_each_entry(cdev, &thermal_cdev_list, node) {
631 /* skip non matching cdevs */
632 if (strncmp(cdev_type, cdev->type, size))
634 /* unbinding the exception matching the type pattern */
635 thermal_zone_unbind_cooling_device(tz, THERMAL_TRIPS_NONE,
638 mutex_unlock(&thermal_list_lock);
642 * Device management section: cooling devices, zones devices, and binding
644 * Set of functions provided by the thermal core for:
645 * - cooling devices lifecycle: registration, unregistration,
646 * binding, and unbinding.
647 * - thermal zone devices lifecycle: registration, unregistration,
648 * binding, and unbinding.
652 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
653 * @tz: pointer to struct thermal_zone_device
654 * @trip: indicates which trip point the cooling devices is
655 * associated with in this thermal zone.
656 * @cdev: pointer to struct thermal_cooling_device
657 * @upper: the Maximum cooling state for this trip point.
658 * THERMAL_NO_LIMIT means no upper limit,
659 * and the cooling device can be in max_state.
660 * @lower: the Minimum cooling state can be used for this trip point.
661 * THERMAL_NO_LIMIT means no lower limit,
662 * and the cooling device can be in cooling state 0.
663 * @weight: The weight of the cooling device to be bound to the
664 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
667 * This interface function bind a thermal cooling device to the certain trip
668 * point of a thermal zone device.
669 * This function is usually called in the thermal zone device .bind callback.
671 * Return: 0 on success, the proper error value otherwise.
673 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
675 struct thermal_cooling_device *cdev,
676 unsigned long upper, unsigned long lower,
679 struct thermal_instance *dev;
680 struct thermal_instance *pos;
681 struct thermal_zone_device *pos1;
682 struct thermal_cooling_device *pos2;
683 unsigned long max_state;
686 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
689 list_for_each_entry(pos1, &thermal_tz_list, node) {
693 list_for_each_entry(pos2, &thermal_cdev_list, node) {
698 if (tz != pos1 || cdev != pos2)
701 ret = cdev->ops->get_max_state(cdev, &max_state);
705 /* lower default 0, upper default max_state */
706 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
707 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
709 if (lower > upper || upper > max_state)
712 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
720 dev->target = THERMAL_NO_TARGET;
721 dev->weight = weight;
723 result = ida_simple_get(&tz->ida, 0, 0, GFP_KERNEL);
728 sprintf(dev->name, "cdev%d", dev->id);
730 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
734 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
735 sysfs_attr_init(&dev->attr.attr);
736 dev->attr.attr.name = dev->attr_name;
737 dev->attr.attr.mode = 0444;
738 dev->attr.show = trip_point_show;
739 result = device_create_file(&tz->device, &dev->attr);
741 goto remove_symbol_link;
743 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
744 sysfs_attr_init(&dev->weight_attr.attr);
745 dev->weight_attr.attr.name = dev->weight_attr_name;
746 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
747 dev->weight_attr.show = weight_show;
748 dev->weight_attr.store = weight_store;
749 result = device_create_file(&tz->device, &dev->weight_attr);
751 goto remove_trip_file;
753 mutex_lock(&tz->lock);
754 mutex_lock(&cdev->lock);
755 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
756 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
761 list_add_tail(&dev->tz_node, &tz->thermal_instances);
762 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
763 atomic_set(&tz->need_update, 1);
765 mutex_unlock(&cdev->lock);
766 mutex_unlock(&tz->lock);
771 device_remove_file(&tz->device, &dev->weight_attr);
773 device_remove_file(&tz->device, &dev->attr);
775 sysfs_remove_link(&tz->device.kobj, dev->name);
777 ida_simple_remove(&tz->ida, dev->id);
782 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
785 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
787 * @tz: pointer to a struct thermal_zone_device.
788 * @trip: indicates which trip point the cooling devices is
789 * associated with in this thermal zone.
790 * @cdev: pointer to a struct thermal_cooling_device.
792 * This interface function unbind a thermal cooling device from the certain
793 * trip point of a thermal zone device.
794 * This function is usually called in the thermal zone device .unbind callback.
796 * Return: 0 on success, the proper error value otherwise.
798 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
800 struct thermal_cooling_device *cdev)
802 struct thermal_instance *pos, *next;
804 mutex_lock(&tz->lock);
805 mutex_lock(&cdev->lock);
806 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
807 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
808 list_del(&pos->tz_node);
809 list_del(&pos->cdev_node);
810 mutex_unlock(&cdev->lock);
811 mutex_unlock(&tz->lock);
815 mutex_unlock(&cdev->lock);
816 mutex_unlock(&tz->lock);
821 device_remove_file(&tz->device, &pos->weight_attr);
822 device_remove_file(&tz->device, &pos->attr);
823 sysfs_remove_link(&tz->device.kobj, pos->name);
824 ida_simple_remove(&tz->ida, pos->id);
828 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
830 static void thermal_release(struct device *dev)
832 struct thermal_zone_device *tz;
833 struct thermal_cooling_device *cdev;
835 if (!strncmp(dev_name(dev), "thermal_zone",
836 sizeof("thermal_zone") - 1)) {
837 tz = to_thermal_zone(dev);
838 thermal_zone_destroy_device_groups(tz);
840 } else if (!strncmp(dev_name(dev), "cooling_device",
841 sizeof("cooling_device") - 1)) {
842 cdev = to_cooling_device(dev);
847 static struct class thermal_class = {
849 .dev_release = thermal_release,
853 void print_bind_err_msg(struct thermal_zone_device *tz,
854 struct thermal_cooling_device *cdev, int ret)
856 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
857 tz->type, cdev->type, ret);
860 static void __bind(struct thermal_zone_device *tz, int mask,
861 struct thermal_cooling_device *cdev,
862 unsigned long *limits,
867 for (i = 0; i < tz->trips; i++) {
868 if (mask & (1 << i)) {
869 unsigned long upper, lower;
871 upper = THERMAL_NO_LIMIT;
872 lower = THERMAL_NO_LIMIT;
874 lower = limits[i * 2];
875 upper = limits[i * 2 + 1];
877 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
881 print_bind_err_msg(tz, cdev, ret);
886 static void bind_cdev(struct thermal_cooling_device *cdev)
889 const struct thermal_zone_params *tzp;
890 struct thermal_zone_device *pos = NULL;
892 mutex_lock(&thermal_list_lock);
894 list_for_each_entry(pos, &thermal_tz_list, node) {
895 if (!pos->tzp && !pos->ops->bind)
898 if (pos->ops->bind) {
899 ret = pos->ops->bind(pos, cdev);
901 print_bind_err_msg(pos, cdev, ret);
906 if (!tzp || !tzp->tbp)
909 for (i = 0; i < tzp->num_tbps; i++) {
910 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
912 if (tzp->tbp[i].match(pos, cdev))
914 tzp->tbp[i].cdev = cdev;
915 __bind(pos, tzp->tbp[i].trip_mask, cdev,
916 tzp->tbp[i].binding_limits,
921 mutex_unlock(&thermal_list_lock);
925 * __thermal_cooling_device_register() - register a new thermal cooling device
926 * @np: a pointer to a device tree node.
927 * @type: the thermal cooling device type.
928 * @devdata: device private data.
929 * @ops: standard thermal cooling devices callbacks.
931 * This interface function adds a new thermal cooling device (fan/processor/...)
932 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
933 * to all the thermal zone devices registered at the same time.
934 * It also gives the opportunity to link the cooling device to a device tree
935 * node, so that it can be bound to a thermal zone created out of device tree.
937 * Return: a pointer to the created struct thermal_cooling_device or an
938 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
940 static struct thermal_cooling_device *
941 __thermal_cooling_device_register(struct device_node *np,
942 const char *type, void *devdata,
943 const struct thermal_cooling_device_ops *ops)
945 struct thermal_cooling_device *cdev;
946 struct thermal_zone_device *pos = NULL;
949 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
950 return ERR_PTR(-EINVAL);
952 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
954 return ERR_PTR(-EINVAL);
956 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
958 return ERR_PTR(-ENOMEM);
960 result = ida_simple_get(&thermal_cdev_ida, 0, 0, GFP_KERNEL);
963 return ERR_PTR(result);
967 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
968 mutex_init(&cdev->lock);
969 INIT_LIST_HEAD(&cdev->thermal_instances);
972 cdev->updated = false;
973 cdev->device.class = &thermal_class;
974 cdev->devdata = devdata;
975 thermal_cooling_device_setup_sysfs(cdev);
976 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
977 result = device_register(&cdev->device);
979 ida_simple_remove(&thermal_cdev_ida, cdev->id);
980 put_device(&cdev->device);
981 return ERR_PTR(result);
984 /* Add 'this' new cdev to the global cdev list */
985 mutex_lock(&thermal_list_lock);
986 list_add(&cdev->node, &thermal_cdev_list);
987 mutex_unlock(&thermal_list_lock);
989 /* Update binding information for 'this' new cdev */
992 mutex_lock(&thermal_list_lock);
993 list_for_each_entry(pos, &thermal_tz_list, node)
994 if (atomic_cmpxchg(&pos->need_update, 1, 0))
995 thermal_zone_device_update(pos,
996 THERMAL_EVENT_UNSPECIFIED);
997 mutex_unlock(&thermal_list_lock);
1003 * thermal_cooling_device_register() - register a new thermal cooling device
1004 * @type: the thermal cooling device type.
1005 * @devdata: device private data.
1006 * @ops: standard thermal cooling devices callbacks.
1008 * This interface function adds a new thermal cooling device (fan/processor/...)
1009 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1010 * to all the thermal zone devices registered at the same time.
1012 * Return: a pointer to the created struct thermal_cooling_device or an
1013 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1015 struct thermal_cooling_device *
1016 thermal_cooling_device_register(const char *type, void *devdata,
1017 const struct thermal_cooling_device_ops *ops)
1019 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1021 EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
1024 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1025 * @np: a pointer to a device tree node.
1026 * @type: the thermal cooling device type.
1027 * @devdata: device private data.
1028 * @ops: standard thermal cooling devices callbacks.
1030 * This function will register a cooling device with device tree node reference.
1031 * This interface function adds a new thermal cooling device (fan/processor/...)
1032 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1033 * to all the thermal zone devices registered at the same time.
1035 * Return: a pointer to the created struct thermal_cooling_device or an
1036 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1038 struct thermal_cooling_device *
1039 thermal_of_cooling_device_register(struct device_node *np,
1040 const char *type, void *devdata,
1041 const struct thermal_cooling_device_ops *ops)
1043 return __thermal_cooling_device_register(np, type, devdata, ops);
1045 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1047 static void thermal_cooling_device_release(struct device *dev, void *res)
1049 thermal_cooling_device_unregister(
1050 *(struct thermal_cooling_device **)res);
1054 * devm_thermal_of_cooling_device_register() - register an OF thermal cooling
1056 * @dev: a valid struct device pointer of a sensor device.
1057 * @np: a pointer to a device tree node.
1058 * @type: the thermal cooling device type.
1059 * @devdata: device private data.
1060 * @ops: standard thermal cooling devices callbacks.
1062 * This function will register a cooling device with device tree node reference.
1063 * This interface function adds a new thermal cooling device (fan/processor/...)
1064 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1065 * to all the thermal zone devices registered at the same time.
1067 * Return: a pointer to the created struct thermal_cooling_device or an
1068 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1070 struct thermal_cooling_device *
1071 devm_thermal_of_cooling_device_register(struct device *dev,
1072 struct device_node *np,
1073 char *type, void *devdata,
1074 const struct thermal_cooling_device_ops *ops)
1076 struct thermal_cooling_device **ptr, *tcd;
1078 ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr),
1081 return ERR_PTR(-ENOMEM);
1083 tcd = __thermal_cooling_device_register(np, type, devdata, ops);
1090 devres_add(dev, ptr);
1094 EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register);
1096 static void __unbind(struct thermal_zone_device *tz, int mask,
1097 struct thermal_cooling_device *cdev)
1101 for (i = 0; i < tz->trips; i++)
1102 if (mask & (1 << i))
1103 thermal_zone_unbind_cooling_device(tz, i, cdev);
1107 * thermal_cooling_device_unregister - removes a thermal cooling device
1108 * @cdev: the thermal cooling device to remove.
1110 * thermal_cooling_device_unregister() must be called when a registered
1111 * thermal cooling device is no longer needed.
1113 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1116 const struct thermal_zone_params *tzp;
1117 struct thermal_zone_device *tz;
1118 struct thermal_cooling_device *pos = NULL;
1123 mutex_lock(&thermal_list_lock);
1124 list_for_each_entry(pos, &thermal_cdev_list, node)
1128 /* thermal cooling device not found */
1129 mutex_unlock(&thermal_list_lock);
1132 list_del(&cdev->node);
1134 /* Unbind all thermal zones associated with 'this' cdev */
1135 list_for_each_entry(tz, &thermal_tz_list, node) {
1136 if (tz->ops->unbind) {
1137 tz->ops->unbind(tz, cdev);
1141 if (!tz->tzp || !tz->tzp->tbp)
1145 for (i = 0; i < tzp->num_tbps; i++) {
1146 if (tzp->tbp[i].cdev == cdev) {
1147 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1148 tzp->tbp[i].cdev = NULL;
1153 mutex_unlock(&thermal_list_lock);
1155 ida_simple_remove(&thermal_cdev_ida, cdev->id);
1156 device_del(&cdev->device);
1157 thermal_cooling_device_destroy_sysfs(cdev);
1158 put_device(&cdev->device);
1160 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
1162 static void bind_tz(struct thermal_zone_device *tz)
1165 struct thermal_cooling_device *pos = NULL;
1166 const struct thermal_zone_params *tzp = tz->tzp;
1168 if (!tzp && !tz->ops->bind)
1171 mutex_lock(&thermal_list_lock);
1173 /* If there is ops->bind, try to use ops->bind */
1174 if (tz->ops->bind) {
1175 list_for_each_entry(pos, &thermal_cdev_list, node) {
1176 ret = tz->ops->bind(tz, pos);
1178 print_bind_err_msg(tz, pos, ret);
1183 if (!tzp || !tzp->tbp)
1186 list_for_each_entry(pos, &thermal_cdev_list, node) {
1187 for (i = 0; i < tzp->num_tbps; i++) {
1188 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
1190 if (tzp->tbp[i].match(tz, pos))
1192 tzp->tbp[i].cdev = pos;
1193 __bind(tz, tzp->tbp[i].trip_mask, pos,
1194 tzp->tbp[i].binding_limits,
1195 tzp->tbp[i].weight);
1199 mutex_unlock(&thermal_list_lock);
1203 * thermal_zone_device_register() - register a new thermal zone device
1204 * @type: the thermal zone device type
1205 * @trips: the number of trip points the thermal zone support
1206 * @mask: a bit string indicating the writeablility of trip points
1207 * @devdata: private device data
1208 * @ops: standard thermal zone device callbacks
1209 * @tzp: thermal zone platform parameters
1210 * @passive_delay: number of milliseconds to wait between polls when
1211 * performing passive cooling
1212 * @polling_delay: number of milliseconds to wait between polls when checking
1213 * whether trip points have been crossed (0 for interrupt
1216 * This interface function adds a new thermal zone device (sensor) to
1217 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1218 * thermal cooling devices registered at the same time.
1219 * thermal_zone_device_unregister() must be called when the device is no
1220 * longer needed. The passive cooling depends on the .get_trend() return value.
1222 * Return: a pointer to the created struct thermal_zone_device or an
1223 * in case of error, an ERR_PTR. Caller must check return value with
1224 * IS_ERR*() helpers.
1226 struct thermal_zone_device *
1227 thermal_zone_device_register(const char *type, int trips, int mask,
1228 void *devdata, struct thermal_zone_device_ops *ops,
1229 struct thermal_zone_params *tzp, int passive_delay,
1232 struct thermal_zone_device *tz;
1233 enum thermal_trip_type trip_type;
1238 struct thermal_governor *governor;
1240 if (!type || strlen(type) == 0) {
1241 pr_err("Error: No thermal zone type defined\n");
1242 return ERR_PTR(-EINVAL);
1245 if (type && strlen(type) >= THERMAL_NAME_LENGTH) {
1246 pr_err("Error: Thermal zone name (%s) too long, should be under %d chars\n",
1247 type, THERMAL_NAME_LENGTH);
1248 return ERR_PTR(-EINVAL);
1251 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips) {
1252 pr_err("Error: Incorrect number of thermal trips\n");
1253 return ERR_PTR(-EINVAL);
1257 pr_err("Error: Thermal zone device ops not defined\n");
1258 return ERR_PTR(-EINVAL);
1261 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
1262 return ERR_PTR(-EINVAL);
1264 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
1266 return ERR_PTR(-ENOMEM);
1268 INIT_LIST_HEAD(&tz->thermal_instances);
1270 mutex_init(&tz->lock);
1271 id = ida_simple_get(&thermal_tz_ida, 0, 0, GFP_KERNEL);
1278 strlcpy(tz->type, type, sizeof(tz->type));
1281 tz->device.class = &thermal_class;
1282 tz->devdata = devdata;
1284 tz->passive_delay = passive_delay;
1285 tz->polling_delay = polling_delay;
1288 /* Add nodes that are always present via .groups */
1289 result = thermal_zone_create_device_groups(tz, mask);
1293 /* A new thermal zone needs to be updated anyway. */
1294 atomic_set(&tz->need_update, 1);
1296 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1297 result = device_register(&tz->device);
1299 goto release_device;
1301 for (count = 0; count < trips; count++) {
1302 if (tz->ops->get_trip_type(tz, count, &trip_type))
1303 set_bit(count, &tz->trips_disabled);
1304 if (tz->ops->get_trip_temp(tz, count, &trip_temp))
1305 set_bit(count, &tz->trips_disabled);
1306 /* Check for bogus trip points */
1308 set_bit(count, &tz->trips_disabled);
1311 /* Update 'this' zone's governor information */
1312 mutex_lock(&thermal_governor_lock);
1315 governor = __find_governor(tz->tzp->governor_name);
1317 governor = def_governor;
1319 result = thermal_set_governor(tz, governor);
1321 mutex_unlock(&thermal_governor_lock);
1325 mutex_unlock(&thermal_governor_lock);
1327 if (!tz->tzp || !tz->tzp->no_hwmon) {
1328 result = thermal_add_hwmon_sysfs(tz);
1333 mutex_lock(&thermal_list_lock);
1334 list_add_tail(&tz->node, &thermal_tz_list);
1335 mutex_unlock(&thermal_list_lock);
1337 /* Bind cooling devices for this zone */
1340 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
1342 thermal_zone_device_reset(tz);
1343 /* Update the new thermal zone and mark it as already updated. */
1344 if (atomic_cmpxchg(&tz->need_update, 1, 0))
1345 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1350 device_del(&tz->device);
1352 put_device(&tz->device);
1355 ida_simple_remove(&thermal_tz_ida, id);
1358 return ERR_PTR(result);
1360 EXPORT_SYMBOL_GPL(thermal_zone_device_register);
1363 * thermal_device_unregister - removes the registered thermal zone device
1364 * @tz: the thermal zone device to remove
1366 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1369 const struct thermal_zone_params *tzp;
1370 struct thermal_cooling_device *cdev;
1371 struct thermal_zone_device *pos = NULL;
1378 mutex_lock(&thermal_list_lock);
1379 list_for_each_entry(pos, &thermal_tz_list, node)
1383 /* thermal zone device not found */
1384 mutex_unlock(&thermal_list_lock);
1387 list_del(&tz->node);
1389 /* Unbind all cdevs associated with 'this' thermal zone */
1390 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1391 if (tz->ops->unbind) {
1392 tz->ops->unbind(tz, cdev);
1396 if (!tzp || !tzp->tbp)
1399 for (i = 0; i < tzp->num_tbps; i++) {
1400 if (tzp->tbp[i].cdev == cdev) {
1401 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1402 tzp->tbp[i].cdev = NULL;
1407 mutex_unlock(&thermal_list_lock);
1409 cancel_delayed_work_sync(&tz->poll_queue);
1411 thermal_set_governor(tz, NULL);
1413 thermal_remove_hwmon_sysfs(tz);
1414 ida_simple_remove(&thermal_tz_ida, tz->id);
1415 ida_destroy(&tz->ida);
1416 mutex_destroy(&tz->lock);
1417 device_unregister(&tz->device);
1419 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
1422 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1423 * @name: thermal zone name to fetch the temperature
1425 * When only one zone is found with the passed name, returns a reference to it.
1427 * Return: On success returns a reference to an unique thermal zone with
1428 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1429 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1431 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1433 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1434 unsigned int found = 0;
1439 mutex_lock(&thermal_list_lock);
1440 list_for_each_entry(pos, &thermal_tz_list, node)
1441 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1445 mutex_unlock(&thermal_list_lock);
1447 /* nothing has been found, thus an error code for it */
1449 ref = ERR_PTR(-ENODEV);
1451 /* Success only when an unique zone is found */
1452 ref = ERR_PTR(-EEXIST);
1457 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1459 static int thermal_pm_notify(struct notifier_block *nb,
1460 unsigned long mode, void *_unused)
1462 struct thermal_zone_device *tz;
1463 enum thermal_device_mode tz_mode;
1466 case PM_HIBERNATION_PREPARE:
1467 case PM_RESTORE_PREPARE:
1468 case PM_SUSPEND_PREPARE:
1469 atomic_set(&in_suspend, 1);
1471 case PM_POST_HIBERNATION:
1472 case PM_POST_RESTORE:
1473 case PM_POST_SUSPEND:
1474 atomic_set(&in_suspend, 0);
1475 list_for_each_entry(tz, &thermal_tz_list, node) {
1476 tz_mode = THERMAL_DEVICE_ENABLED;
1477 if (tz->ops->get_mode)
1478 tz->ops->get_mode(tz, &tz_mode);
1480 if (tz_mode == THERMAL_DEVICE_DISABLED)
1483 thermal_zone_device_init(tz);
1484 thermal_zone_device_update(tz,
1485 THERMAL_EVENT_UNSPECIFIED);
1494 static struct notifier_block thermal_pm_nb = {
1495 .notifier_call = thermal_pm_notify,
1498 static int __init thermal_init(void)
1502 mutex_init(&poweroff_lock);
1503 result = thermal_register_governors();
1507 result = class_register(&thermal_class);
1509 goto unregister_governors;
1511 result = of_parse_thermal_zones();
1513 goto unregister_class;
1515 result = register_pm_notifier(&thermal_pm_nb);
1517 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1523 class_unregister(&thermal_class);
1524 unregister_governors:
1525 thermal_unregister_governors();
1527 ida_destroy(&thermal_tz_ida);
1528 ida_destroy(&thermal_cdev_ida);
1529 mutex_destroy(&thermal_list_lock);
1530 mutex_destroy(&thermal_governor_lock);
1531 mutex_destroy(&poweroff_lock);
1534 core_initcall(thermal_init);