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 <net/netlink.h>
23 #include <net/genetlink.h>
24 #include <linux/suspend.h>
26 #define CREATE_TRACE_POINTS
27 #include <trace/events/thermal.h>
29 #include "thermal_core.h"
30 #include "thermal_hwmon.h"
32 MODULE_AUTHOR("Zhang Rui");
33 MODULE_DESCRIPTION("Generic thermal management sysfs support");
34 MODULE_LICENSE("GPL v2");
36 static DEFINE_IDA(thermal_tz_ida
);
37 static DEFINE_IDA(thermal_cdev_ida
);
39 static LIST_HEAD(thermal_tz_list
);
40 static LIST_HEAD(thermal_cdev_list
);
41 static LIST_HEAD(thermal_governor_list
);
43 static DEFINE_MUTEX(thermal_list_lock
);
44 static DEFINE_MUTEX(thermal_governor_lock
);
45 static DEFINE_MUTEX(poweroff_lock
);
47 static atomic_t in_suspend
;
48 static bool power_off_triggered
;
50 static struct thermal_governor
*def_governor
;
53 * Governor section: set of functions to handle thermal governors
55 * Functions to help in the life cycle of thermal governors within
56 * the thermal core and by the thermal governor code.
59 static struct thermal_governor
*__find_governor(const char *name
)
61 struct thermal_governor
*pos
;
63 if (!name
|| !name
[0])
66 list_for_each_entry(pos
, &thermal_governor_list
, governor_list
)
67 if (!strncasecmp(name
, pos
->name
, THERMAL_NAME_LENGTH
))
74 * bind_previous_governor() - bind the previous governor of the thermal zone
75 * @tz: a valid pointer to a struct thermal_zone_device
76 * @failed_gov_name: the name of the governor that failed to register
78 * Register the previous governor of the thermal zone after a new
79 * governor has failed to be bound.
81 static void bind_previous_governor(struct thermal_zone_device
*tz
,
82 const char *failed_gov_name
)
84 if (tz
->governor
&& tz
->governor
->bind_to_tz
) {
85 if (tz
->governor
->bind_to_tz(tz
)) {
87 "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n",
88 failed_gov_name
, tz
->governor
->name
, tz
->type
);
95 * thermal_set_governor() - Switch to another governor
96 * @tz: a valid pointer to a struct thermal_zone_device
97 * @new_gov: pointer to the new governor
99 * Change the governor of thermal zone @tz.
101 * Return: 0 on success, an error if the new governor's bind_to_tz() failed.
103 static int thermal_set_governor(struct thermal_zone_device
*tz
,
104 struct thermal_governor
*new_gov
)
108 if (tz
->governor
&& tz
->governor
->unbind_from_tz
)
109 tz
->governor
->unbind_from_tz(tz
);
111 if (new_gov
&& new_gov
->bind_to_tz
) {
112 ret
= new_gov
->bind_to_tz(tz
);
114 bind_previous_governor(tz
, new_gov
->name
);
120 tz
->governor
= new_gov
;
125 int thermal_register_governor(struct thermal_governor
*governor
)
129 struct thermal_zone_device
*pos
;
134 mutex_lock(&thermal_governor_lock
);
137 if (!__find_governor(governor
->name
)) {
141 list_add(&governor
->governor_list
, &thermal_governor_list
);
142 match_default
= !strncmp(governor
->name
,
143 DEFAULT_THERMAL_GOVERNOR
,
144 THERMAL_NAME_LENGTH
);
146 if (!def_governor
&& match_default
)
147 def_governor
= governor
;
150 mutex_lock(&thermal_list_lock
);
152 list_for_each_entry(pos
, &thermal_tz_list
, node
) {
154 * only thermal zones with specified tz->tzp->governor_name
155 * may run with tz->govenor unset
160 name
= pos
->tzp
->governor_name
;
162 if (!strncasecmp(name
, governor
->name
, THERMAL_NAME_LENGTH
)) {
165 ret
= thermal_set_governor(pos
, governor
);
167 dev_err(&pos
->device
,
168 "Failed to set governor %s for thermal zone %s: %d\n",
169 governor
->name
, pos
->type
, ret
);
173 mutex_unlock(&thermal_list_lock
);
174 mutex_unlock(&thermal_governor_lock
);
179 void thermal_unregister_governor(struct thermal_governor
*governor
)
181 struct thermal_zone_device
*pos
;
186 mutex_lock(&thermal_governor_lock
);
188 if (!__find_governor(governor
->name
))
191 mutex_lock(&thermal_list_lock
);
193 list_for_each_entry(pos
, &thermal_tz_list
, node
) {
194 if (!strncasecmp(pos
->governor
->name
, governor
->name
,
195 THERMAL_NAME_LENGTH
))
196 thermal_set_governor(pos
, NULL
);
199 mutex_unlock(&thermal_list_lock
);
200 list_del(&governor
->governor_list
);
202 mutex_unlock(&thermal_governor_lock
);
205 int thermal_zone_device_set_policy(struct thermal_zone_device
*tz
,
208 struct thermal_governor
*gov
;
211 mutex_lock(&thermal_governor_lock
);
212 mutex_lock(&tz
->lock
);
214 gov
= __find_governor(strim(policy
));
218 ret
= thermal_set_governor(tz
, gov
);
221 mutex_unlock(&tz
->lock
);
222 mutex_unlock(&thermal_governor_lock
);
227 int thermal_build_list_of_policies(char *buf
)
229 struct thermal_governor
*pos
;
231 ssize_t size
= PAGE_SIZE
;
233 mutex_lock(&thermal_governor_lock
);
235 list_for_each_entry(pos
, &thermal_governor_list
, governor_list
) {
236 size
= PAGE_SIZE
- count
;
237 count
+= scnprintf(buf
+ count
, size
, "%s ", pos
->name
);
239 count
+= scnprintf(buf
+ count
, size
, "\n");
241 mutex_unlock(&thermal_governor_lock
);
246 static int __init
thermal_register_governors(void)
250 result
= thermal_gov_step_wise_register();
254 result
= thermal_gov_fair_share_register();
258 result
= thermal_gov_bang_bang_register();
262 result
= thermal_gov_user_space_register();
266 return thermal_gov_power_allocator_register();
269 static void thermal_unregister_governors(void)
271 thermal_gov_step_wise_unregister();
272 thermal_gov_fair_share_unregister();
273 thermal_gov_bang_bang_unregister();
274 thermal_gov_user_space_unregister();
275 thermal_gov_power_allocator_unregister();
279 * Zone update section: main control loop applied to each zone while monitoring
281 * in polling mode. The monitoring is done using a workqueue.
282 * Same update may be done on a zone by calling thermal_zone_device_update().
285 * - Non-critical trips will invoke the governor responsible for that zone;
286 * - Hot trips will produce a notification to userspace;
287 * - Critical trip point will cause a system shutdown.
289 static void thermal_zone_device_set_polling(struct thermal_zone_device
*tz
,
293 mod_delayed_work(system_freezable_wq
, &tz
->poll_queue
,
294 round_jiffies(msecs_to_jiffies(delay
)));
296 mod_delayed_work(system_freezable_wq
, &tz
->poll_queue
,
297 msecs_to_jiffies(delay
));
299 cancel_delayed_work(&tz
->poll_queue
);
302 static void monitor_thermal_zone(struct thermal_zone_device
*tz
)
304 mutex_lock(&tz
->lock
);
307 thermal_zone_device_set_polling(tz
, tz
->passive_delay
);
308 else if (tz
->polling_delay
)
309 thermal_zone_device_set_polling(tz
, tz
->polling_delay
);
311 thermal_zone_device_set_polling(tz
, 0);
313 mutex_unlock(&tz
->lock
);
316 static void handle_non_critical_trips(struct thermal_zone_device
*tz
,
318 enum thermal_trip_type trip_type
)
320 tz
->governor
? tz
->governor
->throttle(tz
, trip
) :
321 def_governor
->throttle(tz
, trip
);
325 * thermal_emergency_poweroff_func - emergency poweroff work after a known delay
326 * @work: work_struct associated with the emergency poweroff function
328 * This function is called in very critical situations to force
329 * a kernel poweroff after a configurable timeout value.
331 static void thermal_emergency_poweroff_func(struct work_struct
*work
)
334 * We have reached here after the emergency thermal shutdown
335 * Waiting period has expired. This means orderly_poweroff has
336 * not been able to shut off the system for some reason.
337 * Try to shut down the system immediately using kernel_power_off
340 WARN(1, "Attempting kernel_power_off: Temperature too high\n");
344 * Worst of the worst case trigger emergency restart
346 WARN(1, "Attempting emergency_restart: Temperature too high\n");
350 static DECLARE_DELAYED_WORK(thermal_emergency_poweroff_work
,
351 thermal_emergency_poweroff_func
);
354 * thermal_emergency_poweroff - Trigger an emergency system poweroff
356 * This may be called from any critical situation to trigger a system shutdown
357 * after a known period of time. By default this is not scheduled.
359 static void thermal_emergency_poweroff(void)
361 int poweroff_delay_ms
= CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS
;
363 * poweroff_delay_ms must be a carefully profiled positive value.
364 * Its a must for thermal_emergency_poweroff_work to be scheduled
366 if (poweroff_delay_ms
<= 0)
368 schedule_delayed_work(&thermal_emergency_poweroff_work
,
369 msecs_to_jiffies(poweroff_delay_ms
));
372 static void handle_critical_trips(struct thermal_zone_device
*tz
,
373 int trip
, enum thermal_trip_type trip_type
)
377 tz
->ops
->get_trip_temp(tz
, trip
, &trip_temp
);
379 /* If we have not crossed the trip_temp, we do not care. */
380 if (trip_temp
<= 0 || tz
->temperature
< trip_temp
)
383 trace_thermal_zone_trip(tz
, trip
, trip_type
);
386 tz
->ops
->notify(tz
, trip
, trip_type
);
388 if (trip_type
== THERMAL_TRIP_CRITICAL
) {
389 dev_emerg(&tz
->device
,
390 "critical temperature reached (%d C), shutting down\n",
391 tz
->temperature
/ 1000);
392 mutex_lock(&poweroff_lock
);
393 if (!power_off_triggered
) {
395 * Queue a backup emergency shutdown in the event of
396 * orderly_poweroff failure
398 thermal_emergency_poweroff();
399 orderly_poweroff(true);
400 power_off_triggered
= true;
402 mutex_unlock(&poweroff_lock
);
406 static void handle_thermal_trip(struct thermal_zone_device
*tz
, int trip
)
408 enum thermal_trip_type type
;
410 /* Ignore disabled trip points */
411 if (test_bit(trip
, &tz
->trips_disabled
))
414 tz
->ops
->get_trip_type(tz
, trip
, &type
);
416 if (type
== THERMAL_TRIP_CRITICAL
|| type
== THERMAL_TRIP_HOT
)
417 handle_critical_trips(tz
, trip
, type
);
419 handle_non_critical_trips(tz
, trip
, type
);
421 * Alright, we handled this trip successfully.
422 * So, start monitoring again.
424 monitor_thermal_zone(tz
);
427 static void update_temperature(struct thermal_zone_device
*tz
)
431 ret
= thermal_zone_get_temp(tz
, &temp
);
434 dev_warn(&tz
->device
,
435 "failed to read out thermal zone (%d)\n",
440 mutex_lock(&tz
->lock
);
441 tz
->last_temperature
= tz
->temperature
;
442 tz
->temperature
= temp
;
443 mutex_unlock(&tz
->lock
);
445 trace_thermal_temperature(tz
);
446 if (tz
->last_temperature
== THERMAL_TEMP_INVALID
)
447 dev_dbg(&tz
->device
, "last_temperature N/A, current_temperature=%d\n",
450 dev_dbg(&tz
->device
, "last_temperature=%d, current_temperature=%d\n",
451 tz
->last_temperature
, tz
->temperature
);
454 static void thermal_zone_device_reset(struct thermal_zone_device
*tz
)
456 struct thermal_instance
*pos
;
458 tz
->temperature
= THERMAL_TEMP_INVALID
;
460 list_for_each_entry(pos
, &tz
->thermal_instances
, tz_node
)
461 pos
->initialized
= false;
464 void thermal_zone_device_update(struct thermal_zone_device
*tz
,
465 enum thermal_notify_event event
)
469 if (atomic_read(&in_suspend
))
472 if (!tz
->ops
->get_temp
)
475 update_temperature(tz
);
477 thermal_zone_set_trips(tz
);
479 tz
->notify_event
= event
;
481 for (count
= 0; count
< tz
->trips
; count
++)
482 handle_thermal_trip(tz
, count
);
484 EXPORT_SYMBOL_GPL(thermal_zone_device_update
);
487 * thermal_notify_framework - Sensor drivers use this API to notify framework
488 * @tz: thermal zone device
489 * @trip: indicates which trip point has been crossed
491 * This function handles the trip events from sensor drivers. It starts
492 * throttling the cooling devices according to the policy configured.
493 * For CRITICAL and HOT trip points, this notifies the respective drivers,
494 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
495 * The throttling policy is based on the configured platform data; if no
496 * platform data is provided, this uses the step_wise throttling policy.
498 void thermal_notify_framework(struct thermal_zone_device
*tz
, int trip
)
500 handle_thermal_trip(tz
, trip
);
502 EXPORT_SYMBOL_GPL(thermal_notify_framework
);
504 static void thermal_zone_device_check(struct work_struct
*work
)
506 struct thermal_zone_device
*tz
= container_of(work
, struct
509 thermal_zone_device_update(tz
, THERMAL_EVENT_UNSPECIFIED
);
513 * Power actor section: interface to power actors to estimate power
515 * Set of functions used to interact to cooling devices that know
516 * how to estimate their devices power consumption.
520 * power_actor_get_max_power() - get the maximum power that a cdev can consume
521 * @cdev: pointer to &thermal_cooling_device
522 * @tz: a valid thermal zone device pointer
523 * @max_power: pointer in which to store the maximum power
525 * Calculate the maximum power consumption in milliwats that the
526 * cooling device can currently consume and store it in @max_power.
528 * Return: 0 on success, -EINVAL if @cdev doesn't support the
529 * power_actor API or -E* on other error.
531 int power_actor_get_max_power(struct thermal_cooling_device
*cdev
,
532 struct thermal_zone_device
*tz
, u32
*max_power
)
534 if (!cdev_is_power_actor(cdev
))
537 return cdev
->ops
->state2power(cdev
, tz
, 0, max_power
);
541 * power_actor_get_min_power() - get the mainimum power that a cdev can consume
542 * @cdev: pointer to &thermal_cooling_device
543 * @tz: a valid thermal zone device pointer
544 * @min_power: pointer in which to store the minimum power
546 * Calculate the minimum power consumption in milliwatts that the
547 * cooling device can currently consume and store it in @min_power.
549 * Return: 0 on success, -EINVAL if @cdev doesn't support the
550 * power_actor API or -E* on other error.
552 int power_actor_get_min_power(struct thermal_cooling_device
*cdev
,
553 struct thermal_zone_device
*tz
, u32
*min_power
)
555 unsigned long max_state
;
558 if (!cdev_is_power_actor(cdev
))
561 ret
= cdev
->ops
->get_max_state(cdev
, &max_state
);
565 return cdev
->ops
->state2power(cdev
, tz
, max_state
, min_power
);
569 * power_actor_set_power() - limit the maximum power a cooling device consumes
570 * @cdev: pointer to &thermal_cooling_device
571 * @instance: thermal instance to update
572 * @power: the power in milliwatts
574 * Set the cooling device to consume at most @power milliwatts. The limit is
575 * expected to be a cap at the maximum power consumption.
577 * Return: 0 on success, -EINVAL if the cooling device does not
578 * implement the power actor API or -E* for other failures.
580 int power_actor_set_power(struct thermal_cooling_device
*cdev
,
581 struct thermal_instance
*instance
, u32 power
)
586 if (!cdev_is_power_actor(cdev
))
589 ret
= cdev
->ops
->power2state(cdev
, instance
->tz
, power
, &state
);
593 instance
->target
= state
;
594 mutex_lock(&cdev
->lock
);
595 cdev
->updated
= false;
596 mutex_unlock(&cdev
->lock
);
597 thermal_cdev_update(cdev
);
602 void thermal_zone_device_rebind_exception(struct thermal_zone_device
*tz
,
603 const char *cdev_type
, size_t size
)
605 struct thermal_cooling_device
*cdev
= NULL
;
607 mutex_lock(&thermal_list_lock
);
608 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
609 /* skip non matching cdevs */
610 if (strncmp(cdev_type
, cdev
->type
, size
))
613 /* re binding the exception matching the type pattern */
614 thermal_zone_bind_cooling_device(tz
, THERMAL_TRIPS_NONE
, cdev
,
617 THERMAL_WEIGHT_DEFAULT
);
619 mutex_unlock(&thermal_list_lock
);
622 void thermal_zone_device_unbind_exception(struct thermal_zone_device
*tz
,
623 const char *cdev_type
, size_t size
)
625 struct thermal_cooling_device
*cdev
= NULL
;
627 mutex_lock(&thermal_list_lock
);
628 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
629 /* skip non matching cdevs */
630 if (strncmp(cdev_type
, cdev
->type
, size
))
632 /* unbinding the exception matching the type pattern */
633 thermal_zone_unbind_cooling_device(tz
, THERMAL_TRIPS_NONE
,
636 mutex_unlock(&thermal_list_lock
);
640 * Device management section: cooling devices, zones devices, and binding
642 * Set of functions provided by the thermal core for:
643 * - cooling devices lifecycle: registration, unregistration,
644 * binding, and unbinding.
645 * - thermal zone devices lifecycle: registration, unregistration,
646 * binding, and unbinding.
650 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
651 * @tz: pointer to struct thermal_zone_device
652 * @trip: indicates which trip point the cooling devices is
653 * associated with in this thermal zone.
654 * @cdev: pointer to struct thermal_cooling_device
655 * @upper: the Maximum cooling state for this trip point.
656 * THERMAL_NO_LIMIT means no upper limit,
657 * and the cooling device can be in max_state.
658 * @lower: the Minimum cooling state can be used for this trip point.
659 * THERMAL_NO_LIMIT means no lower limit,
660 * and the cooling device can be in cooling state 0.
661 * @weight: The weight of the cooling device to be bound to the
662 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
665 * This interface function bind a thermal cooling device to the certain trip
666 * point of a thermal zone device.
667 * This function is usually called in the thermal zone device .bind callback.
669 * Return: 0 on success, the proper error value otherwise.
671 int thermal_zone_bind_cooling_device(struct thermal_zone_device
*tz
,
673 struct thermal_cooling_device
*cdev
,
674 unsigned long upper
, unsigned long lower
,
677 struct thermal_instance
*dev
;
678 struct thermal_instance
*pos
;
679 struct thermal_zone_device
*pos1
;
680 struct thermal_cooling_device
*pos2
;
681 unsigned long max_state
;
684 if (trip
>= tz
->trips
|| (trip
< 0 && trip
!= THERMAL_TRIPS_NONE
))
687 list_for_each_entry(pos1
, &thermal_tz_list
, node
) {
691 list_for_each_entry(pos2
, &thermal_cdev_list
, node
) {
696 if (tz
!= pos1
|| cdev
!= pos2
)
699 ret
= cdev
->ops
->get_max_state(cdev
, &max_state
);
703 /* lower default 0, upper default max_state */
704 lower
= lower
== THERMAL_NO_LIMIT
? 0 : lower
;
705 upper
= upper
== THERMAL_NO_LIMIT
? max_state
: upper
;
707 if (lower
> upper
|| upper
> max_state
)
710 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
718 dev
->target
= THERMAL_NO_TARGET
;
719 dev
->weight
= weight
;
721 result
= ida_simple_get(&tz
->ida
, 0, 0, GFP_KERNEL
);
726 sprintf(dev
->name
, "cdev%d", dev
->id
);
728 sysfs_create_link(&tz
->device
.kobj
, &cdev
->device
.kobj
, dev
->name
);
732 sprintf(dev
->attr_name
, "cdev%d_trip_point", dev
->id
);
733 sysfs_attr_init(&dev
->attr
.attr
);
734 dev
->attr
.attr
.name
= dev
->attr_name
;
735 dev
->attr
.attr
.mode
= 0444;
736 dev
->attr
.show
= trip_point_show
;
737 result
= device_create_file(&tz
->device
, &dev
->attr
);
739 goto remove_symbol_link
;
741 sprintf(dev
->weight_attr_name
, "cdev%d_weight", dev
->id
);
742 sysfs_attr_init(&dev
->weight_attr
.attr
);
743 dev
->weight_attr
.attr
.name
= dev
->weight_attr_name
;
744 dev
->weight_attr
.attr
.mode
= S_IWUSR
| S_IRUGO
;
745 dev
->weight_attr
.show
= weight_show
;
746 dev
->weight_attr
.store
= weight_store
;
747 result
= device_create_file(&tz
->device
, &dev
->weight_attr
);
749 goto remove_trip_file
;
751 mutex_lock(&tz
->lock
);
752 mutex_lock(&cdev
->lock
);
753 list_for_each_entry(pos
, &tz
->thermal_instances
, tz_node
)
754 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
759 list_add_tail(&dev
->tz_node
, &tz
->thermal_instances
);
760 list_add_tail(&dev
->cdev_node
, &cdev
->thermal_instances
);
761 atomic_set(&tz
->need_update
, 1);
763 mutex_unlock(&cdev
->lock
);
764 mutex_unlock(&tz
->lock
);
769 device_remove_file(&tz
->device
, &dev
->weight_attr
);
771 device_remove_file(&tz
->device
, &dev
->attr
);
773 sysfs_remove_link(&tz
->device
.kobj
, dev
->name
);
775 ida_simple_remove(&tz
->ida
, dev
->id
);
780 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device
);
783 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
785 * @tz: pointer to a struct thermal_zone_device.
786 * @trip: indicates which trip point the cooling devices is
787 * associated with in this thermal zone.
788 * @cdev: pointer to a struct thermal_cooling_device.
790 * This interface function unbind a thermal cooling device from the certain
791 * trip point of a thermal zone device.
792 * This function is usually called in the thermal zone device .unbind callback.
794 * Return: 0 on success, the proper error value otherwise.
796 int thermal_zone_unbind_cooling_device(struct thermal_zone_device
*tz
,
798 struct thermal_cooling_device
*cdev
)
800 struct thermal_instance
*pos
, *next
;
802 mutex_lock(&tz
->lock
);
803 mutex_lock(&cdev
->lock
);
804 list_for_each_entry_safe(pos
, next
, &tz
->thermal_instances
, tz_node
) {
805 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
806 list_del(&pos
->tz_node
);
807 list_del(&pos
->cdev_node
);
808 mutex_unlock(&cdev
->lock
);
809 mutex_unlock(&tz
->lock
);
813 mutex_unlock(&cdev
->lock
);
814 mutex_unlock(&tz
->lock
);
819 device_remove_file(&tz
->device
, &pos
->weight_attr
);
820 device_remove_file(&tz
->device
, &pos
->attr
);
821 sysfs_remove_link(&tz
->device
.kobj
, pos
->name
);
822 ida_simple_remove(&tz
->ida
, pos
->id
);
826 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device
);
828 static void thermal_release(struct device
*dev
)
830 struct thermal_zone_device
*tz
;
831 struct thermal_cooling_device
*cdev
;
833 if (!strncmp(dev_name(dev
), "thermal_zone",
834 sizeof("thermal_zone") - 1)) {
835 tz
= to_thermal_zone(dev
);
836 thermal_zone_destroy_device_groups(tz
);
838 } else if (!strncmp(dev_name(dev
), "cooling_device",
839 sizeof("cooling_device") - 1)) {
840 cdev
= to_cooling_device(dev
);
845 static struct class thermal_class
= {
847 .dev_release
= thermal_release
,
851 void print_bind_err_msg(struct thermal_zone_device
*tz
,
852 struct thermal_cooling_device
*cdev
, int ret
)
854 dev_err(&tz
->device
, "binding zone %s with cdev %s failed:%d\n",
855 tz
->type
, cdev
->type
, ret
);
858 static void __bind(struct thermal_zone_device
*tz
, int mask
,
859 struct thermal_cooling_device
*cdev
,
860 unsigned long *limits
,
865 for (i
= 0; i
< tz
->trips
; i
++) {
866 if (mask
& (1 << i
)) {
867 unsigned long upper
, lower
;
869 upper
= THERMAL_NO_LIMIT
;
870 lower
= THERMAL_NO_LIMIT
;
872 lower
= limits
[i
* 2];
873 upper
= limits
[i
* 2 + 1];
875 ret
= thermal_zone_bind_cooling_device(tz
, i
, cdev
,
879 print_bind_err_msg(tz
, cdev
, ret
);
884 static void bind_cdev(struct thermal_cooling_device
*cdev
)
887 const struct thermal_zone_params
*tzp
;
888 struct thermal_zone_device
*pos
= NULL
;
890 mutex_lock(&thermal_list_lock
);
892 list_for_each_entry(pos
, &thermal_tz_list
, node
) {
893 if (!pos
->tzp
&& !pos
->ops
->bind
)
896 if (pos
->ops
->bind
) {
897 ret
= pos
->ops
->bind(pos
, cdev
);
899 print_bind_err_msg(pos
, cdev
, ret
);
904 if (!tzp
|| !tzp
->tbp
)
907 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
908 if (tzp
->tbp
[i
].cdev
|| !tzp
->tbp
[i
].match
)
910 if (tzp
->tbp
[i
].match(pos
, cdev
))
912 tzp
->tbp
[i
].cdev
= cdev
;
913 __bind(pos
, tzp
->tbp
[i
].trip_mask
, cdev
,
914 tzp
->tbp
[i
].binding_limits
,
919 mutex_unlock(&thermal_list_lock
);
923 * __thermal_cooling_device_register() - register a new thermal cooling device
924 * @np: a pointer to a device tree node.
925 * @type: the thermal cooling device type.
926 * @devdata: device private data.
927 * @ops: standard thermal cooling devices callbacks.
929 * This interface function adds a new thermal cooling device (fan/processor/...)
930 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
931 * to all the thermal zone devices registered at the same time.
932 * It also gives the opportunity to link the cooling device to a device tree
933 * node, so that it can be bound to a thermal zone created out of device tree.
935 * Return: a pointer to the created struct thermal_cooling_device or an
936 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
938 static struct thermal_cooling_device
*
939 __thermal_cooling_device_register(struct device_node
*np
,
940 char *type
, void *devdata
,
941 const struct thermal_cooling_device_ops
*ops
)
943 struct thermal_cooling_device
*cdev
;
944 struct thermal_zone_device
*pos
= NULL
;
947 if (type
&& strlen(type
) >= THERMAL_NAME_LENGTH
)
948 return ERR_PTR(-EINVAL
);
950 if (!ops
|| !ops
->get_max_state
|| !ops
->get_cur_state
||
952 return ERR_PTR(-EINVAL
);
954 cdev
= kzalloc(sizeof(*cdev
), GFP_KERNEL
);
956 return ERR_PTR(-ENOMEM
);
958 result
= ida_simple_get(&thermal_cdev_ida
, 0, 0, GFP_KERNEL
);
961 return ERR_PTR(result
);
965 strlcpy(cdev
->type
, type
? : "", sizeof(cdev
->type
));
966 mutex_init(&cdev
->lock
);
967 INIT_LIST_HEAD(&cdev
->thermal_instances
);
970 cdev
->updated
= false;
971 cdev
->device
.class = &thermal_class
;
972 cdev
->devdata
= devdata
;
973 thermal_cooling_device_setup_sysfs(cdev
);
974 dev_set_name(&cdev
->device
, "cooling_device%d", cdev
->id
);
975 result
= device_register(&cdev
->device
);
977 ida_simple_remove(&thermal_cdev_ida
, cdev
->id
);
979 return ERR_PTR(result
);
982 /* Add 'this' new cdev to the global cdev list */
983 mutex_lock(&thermal_list_lock
);
984 list_add(&cdev
->node
, &thermal_cdev_list
);
985 mutex_unlock(&thermal_list_lock
);
987 /* Update binding information for 'this' new cdev */
990 mutex_lock(&thermal_list_lock
);
991 list_for_each_entry(pos
, &thermal_tz_list
, node
)
992 if (atomic_cmpxchg(&pos
->need_update
, 1, 0))
993 thermal_zone_device_update(pos
,
994 THERMAL_EVENT_UNSPECIFIED
);
995 mutex_unlock(&thermal_list_lock
);
1001 * thermal_cooling_device_register() - register a new thermal cooling device
1002 * @type: the thermal cooling device type.
1003 * @devdata: device private data.
1004 * @ops: standard thermal cooling devices callbacks.
1006 * This interface function adds a new thermal cooling device (fan/processor/...)
1007 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1008 * to all the thermal zone devices registered at the same time.
1010 * Return: a pointer to the created struct thermal_cooling_device or an
1011 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1013 struct thermal_cooling_device
*
1014 thermal_cooling_device_register(char *type
, void *devdata
,
1015 const struct thermal_cooling_device_ops
*ops
)
1017 return __thermal_cooling_device_register(NULL
, type
, devdata
, ops
);
1019 EXPORT_SYMBOL_GPL(thermal_cooling_device_register
);
1022 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1023 * @np: a pointer to a device tree node.
1024 * @type: the thermal cooling device type.
1025 * @devdata: device private data.
1026 * @ops: standard thermal cooling devices callbacks.
1028 * This function will register a cooling device with device tree node reference.
1029 * This interface function adds a new thermal cooling device (fan/processor/...)
1030 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1031 * to all the thermal zone devices registered at the same time.
1033 * Return: a pointer to the created struct thermal_cooling_device or an
1034 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1036 struct thermal_cooling_device
*
1037 thermal_of_cooling_device_register(struct device_node
*np
,
1038 char *type
, void *devdata
,
1039 const struct thermal_cooling_device_ops
*ops
)
1041 return __thermal_cooling_device_register(np
, type
, devdata
, ops
);
1043 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register
);
1045 static void __unbind(struct thermal_zone_device
*tz
, int mask
,
1046 struct thermal_cooling_device
*cdev
)
1050 for (i
= 0; i
< tz
->trips
; i
++)
1051 if (mask
& (1 << i
))
1052 thermal_zone_unbind_cooling_device(tz
, i
, cdev
);
1056 * thermal_cooling_device_unregister - removes a thermal cooling device
1057 * @cdev: the thermal cooling device to remove.
1059 * thermal_cooling_device_unregister() must be called when a registered
1060 * thermal cooling device is no longer needed.
1062 void thermal_cooling_device_unregister(struct thermal_cooling_device
*cdev
)
1065 const struct thermal_zone_params
*tzp
;
1066 struct thermal_zone_device
*tz
;
1067 struct thermal_cooling_device
*pos
= NULL
;
1072 mutex_lock(&thermal_list_lock
);
1073 list_for_each_entry(pos
, &thermal_cdev_list
, node
)
1077 /* thermal cooling device not found */
1078 mutex_unlock(&thermal_list_lock
);
1081 list_del(&cdev
->node
);
1083 /* Unbind all thermal zones associated with 'this' cdev */
1084 list_for_each_entry(tz
, &thermal_tz_list
, node
) {
1085 if (tz
->ops
->unbind
) {
1086 tz
->ops
->unbind(tz
, cdev
);
1090 if (!tz
->tzp
|| !tz
->tzp
->tbp
)
1094 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
1095 if (tzp
->tbp
[i
].cdev
== cdev
) {
1096 __unbind(tz
, tzp
->tbp
[i
].trip_mask
, cdev
);
1097 tzp
->tbp
[i
].cdev
= NULL
;
1102 mutex_unlock(&thermal_list_lock
);
1104 ida_simple_remove(&thermal_cdev_ida
, cdev
->id
);
1105 device_unregister(&cdev
->device
);
1106 thermal_cooling_device_destroy_sysfs(cdev
);
1108 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister
);
1110 static void bind_tz(struct thermal_zone_device
*tz
)
1113 struct thermal_cooling_device
*pos
= NULL
;
1114 const struct thermal_zone_params
*tzp
= tz
->tzp
;
1116 if (!tzp
&& !tz
->ops
->bind
)
1119 mutex_lock(&thermal_list_lock
);
1121 /* If there is ops->bind, try to use ops->bind */
1122 if (tz
->ops
->bind
) {
1123 list_for_each_entry(pos
, &thermal_cdev_list
, node
) {
1124 ret
= tz
->ops
->bind(tz
, pos
);
1126 print_bind_err_msg(tz
, pos
, ret
);
1131 if (!tzp
|| !tzp
->tbp
)
1134 list_for_each_entry(pos
, &thermal_cdev_list
, node
) {
1135 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
1136 if (tzp
->tbp
[i
].cdev
|| !tzp
->tbp
[i
].match
)
1138 if (tzp
->tbp
[i
].match(tz
, pos
))
1140 tzp
->tbp
[i
].cdev
= pos
;
1141 __bind(tz
, tzp
->tbp
[i
].trip_mask
, pos
,
1142 tzp
->tbp
[i
].binding_limits
,
1143 tzp
->tbp
[i
].weight
);
1147 mutex_unlock(&thermal_list_lock
);
1151 * thermal_zone_device_register() - register a new thermal zone device
1152 * @type: the thermal zone device type
1153 * @trips: the number of trip points the thermal zone support
1154 * @mask: a bit string indicating the writeablility of trip points
1155 * @devdata: private device data
1156 * @ops: standard thermal zone device callbacks
1157 * @tzp: thermal zone platform parameters
1158 * @passive_delay: number of milliseconds to wait between polls when
1159 * performing passive cooling
1160 * @polling_delay: number of milliseconds to wait between polls when checking
1161 * whether trip points have been crossed (0 for interrupt
1164 * This interface function adds a new thermal zone device (sensor) to
1165 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1166 * thermal cooling devices registered at the same time.
1167 * thermal_zone_device_unregister() must be called when the device is no
1168 * longer needed. The passive cooling depends on the .get_trend() return value.
1170 * Return: a pointer to the created struct thermal_zone_device or an
1171 * in case of error, an ERR_PTR. Caller must check return value with
1172 * IS_ERR*() helpers.
1174 struct thermal_zone_device
*
1175 thermal_zone_device_register(const char *type
, int trips
, int mask
,
1176 void *devdata
, struct thermal_zone_device_ops
*ops
,
1177 struct thermal_zone_params
*tzp
, int passive_delay
,
1180 struct thermal_zone_device
*tz
;
1181 enum thermal_trip_type trip_type
;
1185 struct thermal_governor
*governor
;
1187 if (!type
|| strlen(type
) == 0)
1188 return ERR_PTR(-EINVAL
);
1190 if (type
&& strlen(type
) >= THERMAL_NAME_LENGTH
)
1191 return ERR_PTR(-EINVAL
);
1193 if (trips
> THERMAL_MAX_TRIPS
|| trips
< 0 || mask
>> trips
)
1194 return ERR_PTR(-EINVAL
);
1197 return ERR_PTR(-EINVAL
);
1199 if (trips
> 0 && (!ops
->get_trip_type
|| !ops
->get_trip_temp
))
1200 return ERR_PTR(-EINVAL
);
1202 tz
= kzalloc(sizeof(*tz
), GFP_KERNEL
);
1204 return ERR_PTR(-ENOMEM
);
1206 INIT_LIST_HEAD(&tz
->thermal_instances
);
1208 mutex_init(&tz
->lock
);
1209 result
= ida_simple_get(&thermal_tz_ida
, 0, 0, GFP_KERNEL
);
1214 strlcpy(tz
->type
, type
, sizeof(tz
->type
));
1217 tz
->device
.class = &thermal_class
;
1218 tz
->devdata
= devdata
;
1220 tz
->passive_delay
= passive_delay
;
1221 tz
->polling_delay
= polling_delay
;
1224 /* Add nodes that are always present via .groups */
1225 result
= thermal_zone_create_device_groups(tz
, mask
);
1229 /* A new thermal zone needs to be updated anyway. */
1230 atomic_set(&tz
->need_update
, 1);
1232 dev_set_name(&tz
->device
, "thermal_zone%d", tz
->id
);
1233 result
= device_register(&tz
->device
);
1235 goto remove_device_groups
;
1237 for (count
= 0; count
< trips
; count
++) {
1238 if (tz
->ops
->get_trip_type(tz
, count
, &trip_type
))
1239 set_bit(count
, &tz
->trips_disabled
);
1240 if (tz
->ops
->get_trip_temp(tz
, count
, &trip_temp
))
1241 set_bit(count
, &tz
->trips_disabled
);
1242 /* Check for bogus trip points */
1244 set_bit(count
, &tz
->trips_disabled
);
1247 /* Update 'this' zone's governor information */
1248 mutex_lock(&thermal_governor_lock
);
1251 governor
= __find_governor(tz
->tzp
->governor_name
);
1253 governor
= def_governor
;
1255 result
= thermal_set_governor(tz
, governor
);
1257 mutex_unlock(&thermal_governor_lock
);
1261 mutex_unlock(&thermal_governor_lock
);
1263 if (!tz
->tzp
|| !tz
->tzp
->no_hwmon
) {
1264 result
= thermal_add_hwmon_sysfs(tz
);
1269 mutex_lock(&thermal_list_lock
);
1270 list_add_tail(&tz
->node
, &thermal_tz_list
);
1271 mutex_unlock(&thermal_list_lock
);
1273 /* Bind cooling devices for this zone */
1276 INIT_DELAYED_WORK(&tz
->poll_queue
, thermal_zone_device_check
);
1278 thermal_zone_device_reset(tz
);
1279 /* Update the new thermal zone and mark it as already updated. */
1280 if (atomic_cmpxchg(&tz
->need_update
, 1, 0))
1281 thermal_zone_device_update(tz
, THERMAL_EVENT_UNSPECIFIED
);
1286 ida_simple_remove(&thermal_tz_ida
, tz
->id
);
1287 device_unregister(&tz
->device
);
1288 return ERR_PTR(result
);
1290 remove_device_groups
:
1291 thermal_zone_destroy_device_groups(tz
);
1293 ida_simple_remove(&thermal_tz_ida
, tz
->id
);
1296 return ERR_PTR(result
);
1298 EXPORT_SYMBOL_GPL(thermal_zone_device_register
);
1301 * thermal_device_unregister - removes the registered thermal zone device
1302 * @tz: the thermal zone device to remove
1304 void thermal_zone_device_unregister(struct thermal_zone_device
*tz
)
1307 const struct thermal_zone_params
*tzp
;
1308 struct thermal_cooling_device
*cdev
;
1309 struct thermal_zone_device
*pos
= NULL
;
1316 mutex_lock(&thermal_list_lock
);
1317 list_for_each_entry(pos
, &thermal_tz_list
, node
)
1321 /* thermal zone device not found */
1322 mutex_unlock(&thermal_list_lock
);
1325 list_del(&tz
->node
);
1327 /* Unbind all cdevs associated with 'this' thermal zone */
1328 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
1329 if (tz
->ops
->unbind
) {
1330 tz
->ops
->unbind(tz
, cdev
);
1334 if (!tzp
|| !tzp
->tbp
)
1337 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
1338 if (tzp
->tbp
[i
].cdev
== cdev
) {
1339 __unbind(tz
, tzp
->tbp
[i
].trip_mask
, cdev
);
1340 tzp
->tbp
[i
].cdev
= NULL
;
1345 mutex_unlock(&thermal_list_lock
);
1347 thermal_zone_device_set_polling(tz
, 0);
1349 thermal_set_governor(tz
, NULL
);
1351 thermal_remove_hwmon_sysfs(tz
);
1352 ida_simple_remove(&thermal_tz_ida
, tz
->id
);
1353 ida_destroy(&tz
->ida
);
1354 mutex_destroy(&tz
->lock
);
1355 device_unregister(&tz
->device
);
1357 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister
);
1360 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1361 * @name: thermal zone name to fetch the temperature
1363 * When only one zone is found with the passed name, returns a reference to it.
1365 * Return: On success returns a reference to an unique thermal zone with
1366 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1367 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1369 struct thermal_zone_device
*thermal_zone_get_zone_by_name(const char *name
)
1371 struct thermal_zone_device
*pos
= NULL
, *ref
= ERR_PTR(-EINVAL
);
1372 unsigned int found
= 0;
1377 mutex_lock(&thermal_list_lock
);
1378 list_for_each_entry(pos
, &thermal_tz_list
, node
)
1379 if (!strncasecmp(name
, pos
->type
, THERMAL_NAME_LENGTH
)) {
1383 mutex_unlock(&thermal_list_lock
);
1385 /* nothing has been found, thus an error code for it */
1387 ref
= ERR_PTR(-ENODEV
);
1389 /* Success only when an unique zone is found */
1390 ref
= ERR_PTR(-EEXIST
);
1395 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name
);
1398 static const struct genl_multicast_group thermal_event_mcgrps
[] = {
1399 { .name
= THERMAL_GENL_MCAST_GROUP_NAME
, },
1402 static struct genl_family thermal_event_genl_family __ro_after_init
= {
1403 .module
= THIS_MODULE
,
1404 .name
= THERMAL_GENL_FAMILY_NAME
,
1405 .version
= THERMAL_GENL_VERSION
,
1406 .maxattr
= THERMAL_GENL_ATTR_MAX
,
1407 .mcgrps
= thermal_event_mcgrps
,
1408 .n_mcgrps
= ARRAY_SIZE(thermal_event_mcgrps
),
1411 int thermal_generate_netlink_event(struct thermal_zone_device
*tz
,
1414 struct sk_buff
*skb
;
1415 struct nlattr
*attr
;
1416 struct thermal_genl_event
*thermal_event
;
1420 static unsigned int thermal_event_seqnum
;
1425 /* allocate memory */
1426 size
= nla_total_size(sizeof(struct thermal_genl_event
)) +
1429 skb
= genlmsg_new(size
, GFP_ATOMIC
);
1433 /* add the genetlink message header */
1434 msg_header
= genlmsg_put(skb
, 0, thermal_event_seqnum
++,
1435 &thermal_event_genl_family
, 0,
1436 THERMAL_GENL_CMD_EVENT
);
1443 attr
= nla_reserve(skb
, THERMAL_GENL_ATTR_EVENT
,
1444 sizeof(struct thermal_genl_event
));
1451 thermal_event
= nla_data(attr
);
1452 if (!thermal_event
) {
1457 memset(thermal_event
, 0, sizeof(struct thermal_genl_event
));
1459 thermal_event
->orig
= tz
->id
;
1460 thermal_event
->event
= event
;
1462 /* send multicast genetlink message */
1463 genlmsg_end(skb
, msg_header
);
1465 result
= genlmsg_multicast(&thermal_event_genl_family
, skb
, 0,
1468 dev_err(&tz
->device
, "Failed to send netlink event:%d", result
);
1472 EXPORT_SYMBOL_GPL(thermal_generate_netlink_event
);
1474 static int __init
genetlink_init(void)
1476 return genl_register_family(&thermal_event_genl_family
);
1479 static void genetlink_exit(void)
1481 genl_unregister_family(&thermal_event_genl_family
);
1483 #else /* !CONFIG_NET */
1484 static inline int genetlink_init(void) { return 0; }
1485 static inline void genetlink_exit(void) {}
1486 #endif /* !CONFIG_NET */
1488 static int thermal_pm_notify(struct notifier_block
*nb
,
1489 unsigned long mode
, void *_unused
)
1491 struct thermal_zone_device
*tz
;
1494 case PM_HIBERNATION_PREPARE
:
1495 case PM_RESTORE_PREPARE
:
1496 case PM_SUSPEND_PREPARE
:
1497 atomic_set(&in_suspend
, 1);
1499 case PM_POST_HIBERNATION
:
1500 case PM_POST_RESTORE
:
1501 case PM_POST_SUSPEND
:
1502 atomic_set(&in_suspend
, 0);
1503 list_for_each_entry(tz
, &thermal_tz_list
, node
) {
1504 thermal_zone_device_reset(tz
);
1505 thermal_zone_device_update(tz
,
1506 THERMAL_EVENT_UNSPECIFIED
);
1515 static struct notifier_block thermal_pm_nb
= {
1516 .notifier_call
= thermal_pm_notify
,
1519 static int __init
thermal_init(void)
1523 mutex_init(&poweroff_lock
);
1524 result
= thermal_register_governors();
1528 result
= class_register(&thermal_class
);
1530 goto unregister_governors
;
1532 result
= genetlink_init();
1534 goto unregister_class
;
1536 result
= of_parse_thermal_zones();
1540 result
= register_pm_notifier(&thermal_pm_nb
);
1542 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1550 class_unregister(&thermal_class
);
1551 unregister_governors
:
1552 thermal_unregister_governors();
1554 ida_destroy(&thermal_tz_ida
);
1555 ida_destroy(&thermal_cdev_ida
);
1556 mutex_destroy(&thermal_list_lock
);
1557 mutex_destroy(&thermal_governor_lock
);
1558 mutex_destroy(&poweroff_lock
);
1562 static void __exit
thermal_exit(void)
1564 unregister_pm_notifier(&thermal_pm_nb
);
1565 of_thermal_destroy_zones();
1567 class_unregister(&thermal_class
);
1568 thermal_unregister_governors();
1569 ida_destroy(&thermal_tz_ida
);
1570 ida_destroy(&thermal_cdev_ida
);
1571 mutex_destroy(&thermal_list_lock
);
1572 mutex_destroy(&thermal_governor_lock
);
1575 fs_initcall(thermal_init
);
1576 module_exit(thermal_exit
);