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
);
450 if (tz
->last_temperature
== THERMAL_TEMP_INVALID
)
451 dev_dbg(&tz
->device
, "last_temperature N/A, current_temperature=%d\n",
454 dev_dbg(&tz
->device
, "last_temperature=%d, current_temperature=%d\n",
455 tz
->last_temperature
, tz
->temperature
);
458 static void thermal_zone_device_init(struct thermal_zone_device
*tz
)
460 struct thermal_instance
*pos
;
461 tz
->temperature
= THERMAL_TEMP_INVALID
;
462 list_for_each_entry(pos
, &tz
->thermal_instances
, tz_node
)
463 pos
->initialized
= false;
466 static void thermal_zone_device_reset(struct thermal_zone_device
*tz
)
469 thermal_zone_device_init(tz
);
472 void thermal_zone_device_update(struct thermal_zone_device
*tz
,
473 enum thermal_notify_event event
)
477 if (atomic_read(&in_suspend
))
480 if (!tz
->ops
->get_temp
)
483 update_temperature(tz
);
485 thermal_zone_set_trips(tz
);
487 tz
->notify_event
= event
;
489 for (count
= 0; count
< tz
->trips
; count
++)
490 handle_thermal_trip(tz
, count
);
492 EXPORT_SYMBOL_GPL(thermal_zone_device_update
);
495 * thermal_notify_framework - Sensor drivers use this API to notify framework
496 * @tz: thermal zone device
497 * @trip: indicates which trip point has been crossed
499 * This function handles the trip events from sensor drivers. It starts
500 * throttling the cooling devices according to the policy configured.
501 * For CRITICAL and HOT trip points, this notifies the respective drivers,
502 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
503 * The throttling policy is based on the configured platform data; if no
504 * platform data is provided, this uses the step_wise throttling policy.
506 void thermal_notify_framework(struct thermal_zone_device
*tz
, int trip
)
508 handle_thermal_trip(tz
, trip
);
510 EXPORT_SYMBOL_GPL(thermal_notify_framework
);
512 static void thermal_zone_device_check(struct work_struct
*work
)
514 struct thermal_zone_device
*tz
= container_of(work
, struct
517 thermal_zone_device_update(tz
, THERMAL_EVENT_UNSPECIFIED
);
521 * Power actor section: interface to power actors to estimate power
523 * Set of functions used to interact to cooling devices that know
524 * how to estimate their devices power consumption.
528 * power_actor_get_max_power() - get the maximum power that a cdev can consume
529 * @cdev: pointer to &thermal_cooling_device
530 * @tz: a valid thermal zone device pointer
531 * @max_power: pointer in which to store the maximum power
533 * Calculate the maximum power consumption in milliwats that the
534 * cooling device can currently consume and store it in @max_power.
536 * Return: 0 on success, -EINVAL if @cdev doesn't support the
537 * power_actor API or -E* on other error.
539 int power_actor_get_max_power(struct thermal_cooling_device
*cdev
,
540 struct thermal_zone_device
*tz
, u32
*max_power
)
542 if (!cdev_is_power_actor(cdev
))
545 return cdev
->ops
->state2power(cdev
, tz
, 0, max_power
);
549 * power_actor_get_min_power() - get the mainimum power that a cdev can consume
550 * @cdev: pointer to &thermal_cooling_device
551 * @tz: a valid thermal zone device pointer
552 * @min_power: pointer in which to store the minimum power
554 * Calculate the minimum power consumption in milliwatts that the
555 * cooling device can currently consume and store it in @min_power.
557 * Return: 0 on success, -EINVAL if @cdev doesn't support the
558 * power_actor API or -E* on other error.
560 int power_actor_get_min_power(struct thermal_cooling_device
*cdev
,
561 struct thermal_zone_device
*tz
, u32
*min_power
)
563 unsigned long max_state
;
566 if (!cdev_is_power_actor(cdev
))
569 ret
= cdev
->ops
->get_max_state(cdev
, &max_state
);
573 return cdev
->ops
->state2power(cdev
, tz
, max_state
, min_power
);
577 * power_actor_set_power() - limit the maximum power a cooling device consumes
578 * @cdev: pointer to &thermal_cooling_device
579 * @instance: thermal instance to update
580 * @power: the power in milliwatts
582 * Set the cooling device to consume at most @power milliwatts. The limit is
583 * expected to be a cap at the maximum power consumption.
585 * Return: 0 on success, -EINVAL if the cooling device does not
586 * implement the power actor API or -E* for other failures.
588 int power_actor_set_power(struct thermal_cooling_device
*cdev
,
589 struct thermal_instance
*instance
, u32 power
)
594 if (!cdev_is_power_actor(cdev
))
597 ret
= cdev
->ops
->power2state(cdev
, instance
->tz
, power
, &state
);
601 instance
->target
= state
;
602 mutex_lock(&cdev
->lock
);
603 cdev
->updated
= false;
604 mutex_unlock(&cdev
->lock
);
605 thermal_cdev_update(cdev
);
610 void thermal_zone_device_rebind_exception(struct thermal_zone_device
*tz
,
611 const char *cdev_type
, size_t size
)
613 struct thermal_cooling_device
*cdev
= NULL
;
615 mutex_lock(&thermal_list_lock
);
616 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
617 /* skip non matching cdevs */
618 if (strncmp(cdev_type
, cdev
->type
, size
))
621 /* re binding the exception matching the type pattern */
622 thermal_zone_bind_cooling_device(tz
, THERMAL_TRIPS_NONE
, cdev
,
625 THERMAL_WEIGHT_DEFAULT
);
627 mutex_unlock(&thermal_list_lock
);
630 void thermal_zone_device_unbind_exception(struct thermal_zone_device
*tz
,
631 const char *cdev_type
, size_t size
)
633 struct thermal_cooling_device
*cdev
= NULL
;
635 mutex_lock(&thermal_list_lock
);
636 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
637 /* skip non matching cdevs */
638 if (strncmp(cdev_type
, cdev
->type
, size
))
640 /* unbinding the exception matching the type pattern */
641 thermal_zone_unbind_cooling_device(tz
, THERMAL_TRIPS_NONE
,
644 mutex_unlock(&thermal_list_lock
);
648 * Device management section: cooling devices, zones devices, and binding
650 * Set of functions provided by the thermal core for:
651 * - cooling devices lifecycle: registration, unregistration,
652 * binding, and unbinding.
653 * - thermal zone devices lifecycle: registration, unregistration,
654 * binding, and unbinding.
658 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
659 * @tz: pointer to struct thermal_zone_device
660 * @trip: indicates which trip point the cooling devices is
661 * associated with in this thermal zone.
662 * @cdev: pointer to struct thermal_cooling_device
663 * @upper: the Maximum cooling state for this trip point.
664 * THERMAL_NO_LIMIT means no upper limit,
665 * and the cooling device can be in max_state.
666 * @lower: the Minimum cooling state can be used for this trip point.
667 * THERMAL_NO_LIMIT means no lower limit,
668 * and the cooling device can be in cooling state 0.
669 * @weight: The weight of the cooling device to be bound to the
670 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
673 * This interface function bind a thermal cooling device to the certain trip
674 * point of a thermal zone device.
675 * This function is usually called in the thermal zone device .bind callback.
677 * Return: 0 on success, the proper error value otherwise.
679 int thermal_zone_bind_cooling_device(struct thermal_zone_device
*tz
,
681 struct thermal_cooling_device
*cdev
,
682 unsigned long upper
, unsigned long lower
,
685 struct thermal_instance
*dev
;
686 struct thermal_instance
*pos
;
687 struct thermal_zone_device
*pos1
;
688 struct thermal_cooling_device
*pos2
;
689 unsigned long max_state
;
692 if (trip
>= tz
->trips
|| (trip
< 0 && trip
!= THERMAL_TRIPS_NONE
))
695 list_for_each_entry(pos1
, &thermal_tz_list
, node
) {
699 list_for_each_entry(pos2
, &thermal_cdev_list
, node
) {
704 if (tz
!= pos1
|| cdev
!= pos2
)
707 ret
= cdev
->ops
->get_max_state(cdev
, &max_state
);
711 /* lower default 0, upper default max_state */
712 lower
= lower
== THERMAL_NO_LIMIT
? 0 : lower
;
713 upper
= upper
== THERMAL_NO_LIMIT
? max_state
: upper
;
715 if (lower
> upper
|| upper
> max_state
)
718 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
726 dev
->target
= THERMAL_NO_TARGET
;
727 dev
->weight
= weight
;
729 result
= ida_simple_get(&tz
->ida
, 0, 0, GFP_KERNEL
);
734 sprintf(dev
->name
, "cdev%d", dev
->id
);
736 sysfs_create_link(&tz
->device
.kobj
, &cdev
->device
.kobj
, dev
->name
);
740 sprintf(dev
->attr_name
, "cdev%d_trip_point", dev
->id
);
741 sysfs_attr_init(&dev
->attr
.attr
);
742 dev
->attr
.attr
.name
= dev
->attr_name
;
743 dev
->attr
.attr
.mode
= 0444;
744 dev
->attr
.show
= trip_point_show
;
745 result
= device_create_file(&tz
->device
, &dev
->attr
);
747 goto remove_symbol_link
;
749 sprintf(dev
->weight_attr_name
, "cdev%d_weight", dev
->id
);
750 sysfs_attr_init(&dev
->weight_attr
.attr
);
751 dev
->weight_attr
.attr
.name
= dev
->weight_attr_name
;
752 dev
->weight_attr
.attr
.mode
= S_IWUSR
| S_IRUGO
;
753 dev
->weight_attr
.show
= weight_show
;
754 dev
->weight_attr
.store
= weight_store
;
755 result
= device_create_file(&tz
->device
, &dev
->weight_attr
);
757 goto remove_trip_file
;
759 mutex_lock(&tz
->lock
);
760 mutex_lock(&cdev
->lock
);
761 list_for_each_entry(pos
, &tz
->thermal_instances
, tz_node
)
762 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
767 list_add_tail(&dev
->tz_node
, &tz
->thermal_instances
);
768 list_add_tail(&dev
->cdev_node
, &cdev
->thermal_instances
);
769 atomic_set(&tz
->need_update
, 1);
771 mutex_unlock(&cdev
->lock
);
772 mutex_unlock(&tz
->lock
);
777 device_remove_file(&tz
->device
, &dev
->weight_attr
);
779 device_remove_file(&tz
->device
, &dev
->attr
);
781 sysfs_remove_link(&tz
->device
.kobj
, dev
->name
);
783 ida_simple_remove(&tz
->ida
, dev
->id
);
788 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device
);
791 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
793 * @tz: pointer to a struct thermal_zone_device.
794 * @trip: indicates which trip point the cooling devices is
795 * associated with in this thermal zone.
796 * @cdev: pointer to a struct thermal_cooling_device.
798 * This interface function unbind a thermal cooling device from the certain
799 * trip point of a thermal zone device.
800 * This function is usually called in the thermal zone device .unbind callback.
802 * Return: 0 on success, the proper error value otherwise.
804 int thermal_zone_unbind_cooling_device(struct thermal_zone_device
*tz
,
806 struct thermal_cooling_device
*cdev
)
808 struct thermal_instance
*pos
, *next
;
810 mutex_lock(&tz
->lock
);
811 mutex_lock(&cdev
->lock
);
812 list_for_each_entry_safe(pos
, next
, &tz
->thermal_instances
, tz_node
) {
813 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
814 list_del(&pos
->tz_node
);
815 list_del(&pos
->cdev_node
);
816 mutex_unlock(&cdev
->lock
);
817 mutex_unlock(&tz
->lock
);
821 mutex_unlock(&cdev
->lock
);
822 mutex_unlock(&tz
->lock
);
827 device_remove_file(&tz
->device
, &pos
->weight_attr
);
828 device_remove_file(&tz
->device
, &pos
->attr
);
829 sysfs_remove_link(&tz
->device
.kobj
, pos
->name
);
830 ida_simple_remove(&tz
->ida
, pos
->id
);
834 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device
);
836 static void thermal_release(struct device
*dev
)
838 struct thermal_zone_device
*tz
;
839 struct thermal_cooling_device
*cdev
;
841 if (!strncmp(dev_name(dev
), "thermal_zone",
842 sizeof("thermal_zone") - 1)) {
843 tz
= to_thermal_zone(dev
);
844 thermal_zone_destroy_device_groups(tz
);
846 } else if (!strncmp(dev_name(dev
), "cooling_device",
847 sizeof("cooling_device") - 1)) {
848 cdev
= to_cooling_device(dev
);
853 static struct class thermal_class
= {
855 .dev_release
= thermal_release
,
859 void print_bind_err_msg(struct thermal_zone_device
*tz
,
860 struct thermal_cooling_device
*cdev
, int ret
)
862 dev_err(&tz
->device
, "binding zone %s with cdev %s failed:%d\n",
863 tz
->type
, cdev
->type
, ret
);
866 static void __bind(struct thermal_zone_device
*tz
, int mask
,
867 struct thermal_cooling_device
*cdev
,
868 unsigned long *limits
,
873 for (i
= 0; i
< tz
->trips
; i
++) {
874 if (mask
& (1 << i
)) {
875 unsigned long upper
, lower
;
877 upper
= THERMAL_NO_LIMIT
;
878 lower
= THERMAL_NO_LIMIT
;
880 lower
= limits
[i
* 2];
881 upper
= limits
[i
* 2 + 1];
883 ret
= thermal_zone_bind_cooling_device(tz
, i
, cdev
,
887 print_bind_err_msg(tz
, cdev
, ret
);
892 static void bind_cdev(struct thermal_cooling_device
*cdev
)
895 const struct thermal_zone_params
*tzp
;
896 struct thermal_zone_device
*pos
= NULL
;
898 mutex_lock(&thermal_list_lock
);
900 list_for_each_entry(pos
, &thermal_tz_list
, node
) {
901 if (!pos
->tzp
&& !pos
->ops
->bind
)
904 if (pos
->ops
->bind
) {
905 ret
= pos
->ops
->bind(pos
, cdev
);
907 print_bind_err_msg(pos
, cdev
, ret
);
912 if (!tzp
|| !tzp
->tbp
)
915 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
916 if (tzp
->tbp
[i
].cdev
|| !tzp
->tbp
[i
].match
)
918 if (tzp
->tbp
[i
].match(pos
, cdev
))
920 tzp
->tbp
[i
].cdev
= cdev
;
921 __bind(pos
, tzp
->tbp
[i
].trip_mask
, cdev
,
922 tzp
->tbp
[i
].binding_limits
,
927 mutex_unlock(&thermal_list_lock
);
931 * __thermal_cooling_device_register() - register a new thermal cooling device
932 * @np: a pointer to a device tree node.
933 * @type: the thermal cooling device type.
934 * @devdata: device private data.
935 * @ops: standard thermal cooling devices callbacks.
937 * This interface function adds a new thermal cooling device (fan/processor/...)
938 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
939 * to all the thermal zone devices registered at the same time.
940 * It also gives the opportunity to link the cooling device to a device tree
941 * node, so that it can be bound to a thermal zone created out of device tree.
943 * Return: a pointer to the created struct thermal_cooling_device or an
944 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
946 static struct thermal_cooling_device
*
947 __thermal_cooling_device_register(struct device_node
*np
,
948 const char *type
, void *devdata
,
949 const struct thermal_cooling_device_ops
*ops
)
951 struct thermal_cooling_device
*cdev
;
952 struct thermal_zone_device
*pos
= NULL
;
955 if (type
&& strlen(type
) >= THERMAL_NAME_LENGTH
)
956 return ERR_PTR(-EINVAL
);
958 if (!ops
|| !ops
->get_max_state
|| !ops
->get_cur_state
||
960 return ERR_PTR(-EINVAL
);
962 cdev
= kzalloc(sizeof(*cdev
), GFP_KERNEL
);
964 return ERR_PTR(-ENOMEM
);
966 result
= ida_simple_get(&thermal_cdev_ida
, 0, 0, GFP_KERNEL
);
969 return ERR_PTR(result
);
973 strlcpy(cdev
->type
, type
? : "", sizeof(cdev
->type
));
974 mutex_init(&cdev
->lock
);
975 INIT_LIST_HEAD(&cdev
->thermal_instances
);
978 cdev
->updated
= false;
979 cdev
->device
.class = &thermal_class
;
980 cdev
->devdata
= devdata
;
981 thermal_cooling_device_setup_sysfs(cdev
);
982 dev_set_name(&cdev
->device
, "cooling_device%d", cdev
->id
);
983 result
= device_register(&cdev
->device
);
985 ida_simple_remove(&thermal_cdev_ida
, cdev
->id
);
986 put_device(&cdev
->device
);
987 return ERR_PTR(result
);
990 /* Add 'this' new cdev to the global cdev list */
991 mutex_lock(&thermal_list_lock
);
992 list_add(&cdev
->node
, &thermal_cdev_list
);
993 mutex_unlock(&thermal_list_lock
);
995 /* Update binding information for 'this' new cdev */
998 mutex_lock(&thermal_list_lock
);
999 list_for_each_entry(pos
, &thermal_tz_list
, node
)
1000 if (atomic_cmpxchg(&pos
->need_update
, 1, 0))
1001 thermal_zone_device_update(pos
,
1002 THERMAL_EVENT_UNSPECIFIED
);
1003 mutex_unlock(&thermal_list_lock
);
1009 * thermal_cooling_device_register() - register a new thermal cooling device
1010 * @type: the thermal cooling device type.
1011 * @devdata: device private data.
1012 * @ops: standard thermal cooling devices callbacks.
1014 * This interface function adds a new thermal cooling device (fan/processor/...)
1015 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1016 * to all the thermal zone devices registered at the same time.
1018 * Return: a pointer to the created struct thermal_cooling_device or an
1019 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1021 struct thermal_cooling_device
*
1022 thermal_cooling_device_register(const char *type
, void *devdata
,
1023 const struct thermal_cooling_device_ops
*ops
)
1025 return __thermal_cooling_device_register(NULL
, type
, devdata
, ops
);
1027 EXPORT_SYMBOL_GPL(thermal_cooling_device_register
);
1030 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1031 * @np: a pointer to a device tree node.
1032 * @type: the thermal cooling device type.
1033 * @devdata: device private data.
1034 * @ops: standard thermal cooling devices callbacks.
1036 * This function will register a cooling device with device tree node reference.
1037 * This interface function adds a new thermal cooling device (fan/processor/...)
1038 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1039 * to all the thermal zone devices registered at the same time.
1041 * Return: a pointer to the created struct thermal_cooling_device or an
1042 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1044 struct thermal_cooling_device
*
1045 thermal_of_cooling_device_register(struct device_node
*np
,
1046 const char *type
, void *devdata
,
1047 const struct thermal_cooling_device_ops
*ops
)
1049 return __thermal_cooling_device_register(np
, type
, devdata
, ops
);
1051 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register
);
1053 static void thermal_cooling_device_release(struct device
*dev
, void *res
)
1055 thermal_cooling_device_unregister(
1056 *(struct thermal_cooling_device
**)res
);
1060 * devm_thermal_of_cooling_device_register() - register an OF thermal cooling
1062 * @dev: a valid struct device pointer of a sensor device.
1063 * @np: a pointer to a device tree node.
1064 * @type: the thermal cooling device type.
1065 * @devdata: device private data.
1066 * @ops: standard thermal cooling devices callbacks.
1068 * This function will register a cooling device with device tree node reference.
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 devm_thermal_of_cooling_device_register(struct device
*dev
,
1078 struct device_node
*np
,
1079 char *type
, void *devdata
,
1080 const struct thermal_cooling_device_ops
*ops
)
1082 struct thermal_cooling_device
**ptr
, *tcd
;
1084 ptr
= devres_alloc(thermal_cooling_device_release
, sizeof(*ptr
),
1087 return ERR_PTR(-ENOMEM
);
1089 tcd
= __thermal_cooling_device_register(np
, type
, devdata
, ops
);
1096 devres_add(dev
, ptr
);
1100 EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register
);
1102 static void __unbind(struct thermal_zone_device
*tz
, int mask
,
1103 struct thermal_cooling_device
*cdev
)
1107 for (i
= 0; i
< tz
->trips
; i
++)
1108 if (mask
& (1 << i
))
1109 thermal_zone_unbind_cooling_device(tz
, i
, cdev
);
1113 * thermal_cooling_device_unregister - removes a thermal cooling device
1114 * @cdev: the thermal cooling device to remove.
1116 * thermal_cooling_device_unregister() must be called when a registered
1117 * thermal cooling device is no longer needed.
1119 void thermal_cooling_device_unregister(struct thermal_cooling_device
*cdev
)
1122 const struct thermal_zone_params
*tzp
;
1123 struct thermal_zone_device
*tz
;
1124 struct thermal_cooling_device
*pos
= NULL
;
1129 mutex_lock(&thermal_list_lock
);
1130 list_for_each_entry(pos
, &thermal_cdev_list
, node
)
1134 /* thermal cooling device not found */
1135 mutex_unlock(&thermal_list_lock
);
1138 list_del(&cdev
->node
);
1140 /* Unbind all thermal zones associated with 'this' cdev */
1141 list_for_each_entry(tz
, &thermal_tz_list
, node
) {
1142 if (tz
->ops
->unbind
) {
1143 tz
->ops
->unbind(tz
, cdev
);
1147 if (!tz
->tzp
|| !tz
->tzp
->tbp
)
1151 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
1152 if (tzp
->tbp
[i
].cdev
== cdev
) {
1153 __unbind(tz
, tzp
->tbp
[i
].trip_mask
, cdev
);
1154 tzp
->tbp
[i
].cdev
= NULL
;
1159 mutex_unlock(&thermal_list_lock
);
1161 ida_simple_remove(&thermal_cdev_ida
, cdev
->id
);
1162 device_del(&cdev
->device
);
1163 thermal_cooling_device_destroy_sysfs(cdev
);
1164 put_device(&cdev
->device
);
1166 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister
);
1168 static void bind_tz(struct thermal_zone_device
*tz
)
1171 struct thermal_cooling_device
*pos
= NULL
;
1172 const struct thermal_zone_params
*tzp
= tz
->tzp
;
1174 if (!tzp
&& !tz
->ops
->bind
)
1177 mutex_lock(&thermal_list_lock
);
1179 /* If there is ops->bind, try to use ops->bind */
1180 if (tz
->ops
->bind
) {
1181 list_for_each_entry(pos
, &thermal_cdev_list
, node
) {
1182 ret
= tz
->ops
->bind(tz
, pos
);
1184 print_bind_err_msg(tz
, pos
, ret
);
1189 if (!tzp
|| !tzp
->tbp
)
1192 list_for_each_entry(pos
, &thermal_cdev_list
, node
) {
1193 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
1194 if (tzp
->tbp
[i
].cdev
|| !tzp
->tbp
[i
].match
)
1196 if (tzp
->tbp
[i
].match(tz
, pos
))
1198 tzp
->tbp
[i
].cdev
= pos
;
1199 __bind(tz
, tzp
->tbp
[i
].trip_mask
, pos
,
1200 tzp
->tbp
[i
].binding_limits
,
1201 tzp
->tbp
[i
].weight
);
1205 mutex_unlock(&thermal_list_lock
);
1209 * thermal_zone_device_register() - register a new thermal zone device
1210 * @type: the thermal zone device type
1211 * @trips: the number of trip points the thermal zone support
1212 * @mask: a bit string indicating the writeablility of trip points
1213 * @devdata: private device data
1214 * @ops: standard thermal zone device callbacks
1215 * @tzp: thermal zone platform parameters
1216 * @passive_delay: number of milliseconds to wait between polls when
1217 * performing passive cooling
1218 * @polling_delay: number of milliseconds to wait between polls when checking
1219 * whether trip points have been crossed (0 for interrupt
1222 * This interface function adds a new thermal zone device (sensor) to
1223 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1224 * thermal cooling devices registered at the same time.
1225 * thermal_zone_device_unregister() must be called when the device is no
1226 * longer needed. The passive cooling depends on the .get_trend() return value.
1228 * Return: a pointer to the created struct thermal_zone_device or an
1229 * in case of error, an ERR_PTR. Caller must check return value with
1230 * IS_ERR*() helpers.
1232 struct thermal_zone_device
*
1233 thermal_zone_device_register(const char *type
, int trips
, int mask
,
1234 void *devdata
, struct thermal_zone_device_ops
*ops
,
1235 struct thermal_zone_params
*tzp
, int passive_delay
,
1238 struct thermal_zone_device
*tz
;
1239 enum thermal_trip_type trip_type
;
1244 struct thermal_governor
*governor
;
1246 if (!type
|| strlen(type
) == 0) {
1247 pr_err("Error: No thermal zone type defined\n");
1248 return ERR_PTR(-EINVAL
);
1251 if (type
&& strlen(type
) >= THERMAL_NAME_LENGTH
) {
1252 pr_err("Error: Thermal zone name (%s) too long, should be under %d chars\n",
1253 type
, THERMAL_NAME_LENGTH
);
1254 return ERR_PTR(-EINVAL
);
1257 if (trips
> THERMAL_MAX_TRIPS
|| trips
< 0 || mask
>> trips
) {
1258 pr_err("Error: Incorrect number of thermal trips\n");
1259 return ERR_PTR(-EINVAL
);
1263 pr_err("Error: Thermal zone device ops not defined\n");
1264 return ERR_PTR(-EINVAL
);
1267 if (trips
> 0 && (!ops
->get_trip_type
|| !ops
->get_trip_temp
))
1268 return ERR_PTR(-EINVAL
);
1270 tz
= kzalloc(sizeof(*tz
), GFP_KERNEL
);
1272 return ERR_PTR(-ENOMEM
);
1274 INIT_LIST_HEAD(&tz
->thermal_instances
);
1276 mutex_init(&tz
->lock
);
1277 id
= ida_simple_get(&thermal_tz_ida
, 0, 0, GFP_KERNEL
);
1284 strlcpy(tz
->type
, type
, sizeof(tz
->type
));
1287 tz
->device
.class = &thermal_class
;
1288 tz
->devdata
= devdata
;
1290 tz
->passive_delay
= passive_delay
;
1291 tz
->polling_delay
= polling_delay
;
1294 /* Add nodes that are always present via .groups */
1295 result
= thermal_zone_create_device_groups(tz
, mask
);
1299 /* A new thermal zone needs to be updated anyway. */
1300 atomic_set(&tz
->need_update
, 1);
1302 dev_set_name(&tz
->device
, "thermal_zone%d", tz
->id
);
1303 result
= device_register(&tz
->device
);
1305 goto release_device
;
1307 for (count
= 0; count
< trips
; count
++) {
1308 if (tz
->ops
->get_trip_type(tz
, count
, &trip_type
))
1309 set_bit(count
, &tz
->trips_disabled
);
1310 if (tz
->ops
->get_trip_temp(tz
, count
, &trip_temp
))
1311 set_bit(count
, &tz
->trips_disabled
);
1312 /* Check for bogus trip points */
1314 set_bit(count
, &tz
->trips_disabled
);
1317 /* Update 'this' zone's governor information */
1318 mutex_lock(&thermal_governor_lock
);
1321 governor
= __find_governor(tz
->tzp
->governor_name
);
1323 governor
= def_governor
;
1325 result
= thermal_set_governor(tz
, governor
);
1327 mutex_unlock(&thermal_governor_lock
);
1331 mutex_unlock(&thermal_governor_lock
);
1333 if (!tz
->tzp
|| !tz
->tzp
->no_hwmon
) {
1334 result
= thermal_add_hwmon_sysfs(tz
);
1339 mutex_lock(&thermal_list_lock
);
1340 list_add_tail(&tz
->node
, &thermal_tz_list
);
1341 mutex_unlock(&thermal_list_lock
);
1343 /* Bind cooling devices for this zone */
1346 INIT_DELAYED_WORK(&tz
->poll_queue
, thermal_zone_device_check
);
1348 thermal_zone_device_reset(tz
);
1349 /* Update the new thermal zone and mark it as already updated. */
1350 if (atomic_cmpxchg(&tz
->need_update
, 1, 0))
1351 thermal_zone_device_update(tz
, THERMAL_EVENT_UNSPECIFIED
);
1356 device_del(&tz
->device
);
1358 put_device(&tz
->device
);
1361 ida_simple_remove(&thermal_tz_ida
, id
);
1364 return ERR_PTR(result
);
1366 EXPORT_SYMBOL_GPL(thermal_zone_device_register
);
1369 * thermal_device_unregister - removes the registered thermal zone device
1370 * @tz: the thermal zone device to remove
1372 void thermal_zone_device_unregister(struct thermal_zone_device
*tz
)
1375 const struct thermal_zone_params
*tzp
;
1376 struct thermal_cooling_device
*cdev
;
1377 struct thermal_zone_device
*pos
= NULL
;
1384 mutex_lock(&thermal_list_lock
);
1385 list_for_each_entry(pos
, &thermal_tz_list
, node
)
1389 /* thermal zone device not found */
1390 mutex_unlock(&thermal_list_lock
);
1393 list_del(&tz
->node
);
1395 /* Unbind all cdevs associated with 'this' thermal zone */
1396 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
1397 if (tz
->ops
->unbind
) {
1398 tz
->ops
->unbind(tz
, cdev
);
1402 if (!tzp
|| !tzp
->tbp
)
1405 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
1406 if (tzp
->tbp
[i
].cdev
== cdev
) {
1407 __unbind(tz
, tzp
->tbp
[i
].trip_mask
, cdev
);
1408 tzp
->tbp
[i
].cdev
= NULL
;
1413 mutex_unlock(&thermal_list_lock
);
1415 cancel_delayed_work_sync(&tz
->poll_queue
);
1417 thermal_set_governor(tz
, NULL
);
1419 thermal_remove_hwmon_sysfs(tz
);
1420 ida_simple_remove(&thermal_tz_ida
, tz
->id
);
1421 ida_destroy(&tz
->ida
);
1422 mutex_destroy(&tz
->lock
);
1423 device_unregister(&tz
->device
);
1425 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister
);
1428 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1429 * @name: thermal zone name to fetch the temperature
1431 * When only one zone is found with the passed name, returns a reference to it.
1433 * Return: On success returns a reference to an unique thermal zone with
1434 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1435 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1437 struct thermal_zone_device
*thermal_zone_get_zone_by_name(const char *name
)
1439 struct thermal_zone_device
*pos
= NULL
, *ref
= ERR_PTR(-EINVAL
);
1440 unsigned int found
= 0;
1445 mutex_lock(&thermal_list_lock
);
1446 list_for_each_entry(pos
, &thermal_tz_list
, node
)
1447 if (!strncasecmp(name
, pos
->type
, THERMAL_NAME_LENGTH
)) {
1451 mutex_unlock(&thermal_list_lock
);
1453 /* nothing has been found, thus an error code for it */
1455 ref
= ERR_PTR(-ENODEV
);
1457 /* Success only when an unique zone is found */
1458 ref
= ERR_PTR(-EEXIST
);
1463 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name
);
1465 static int thermal_pm_notify(struct notifier_block
*nb
,
1466 unsigned long mode
, void *_unused
)
1468 struct thermal_zone_device
*tz
;
1469 enum thermal_device_mode tz_mode
;
1472 case PM_HIBERNATION_PREPARE
:
1473 case PM_RESTORE_PREPARE
:
1474 case PM_SUSPEND_PREPARE
:
1475 atomic_set(&in_suspend
, 1);
1477 case PM_POST_HIBERNATION
:
1478 case PM_POST_RESTORE
:
1479 case PM_POST_SUSPEND
:
1480 atomic_set(&in_suspend
, 0);
1481 list_for_each_entry(tz
, &thermal_tz_list
, node
) {
1482 tz_mode
= THERMAL_DEVICE_ENABLED
;
1483 if (tz
->ops
->get_mode
)
1484 tz
->ops
->get_mode(tz
, &tz_mode
);
1486 if (tz_mode
== THERMAL_DEVICE_DISABLED
)
1489 thermal_zone_device_init(tz
);
1490 thermal_zone_device_update(tz
,
1491 THERMAL_EVENT_UNSPECIFIED
);
1500 static struct notifier_block thermal_pm_nb
= {
1501 .notifier_call
= thermal_pm_notify
,
1504 static int __init
thermal_init(void)
1508 mutex_init(&poweroff_lock
);
1509 result
= thermal_register_governors();
1513 result
= class_register(&thermal_class
);
1515 goto unregister_governors
;
1517 result
= of_parse_thermal_zones();
1519 goto unregister_class
;
1521 result
= register_pm_notifier(&thermal_pm_nb
);
1523 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1529 class_unregister(&thermal_class
);
1530 unregister_governors
:
1531 thermal_unregister_governors();
1533 ida_destroy(&thermal_tz_ida
);
1534 ida_destroy(&thermal_cdev_ida
);
1535 mutex_destroy(&thermal_list_lock
);
1536 mutex_destroy(&thermal_governor_lock
);
1537 mutex_destroy(&poweroff_lock
);
1540 core_initcall(thermal_init
);