2 * thermal.c - Generic Thermal Management Sysfs support.
4 * Copyright (C) 2008 Intel Corp
5 * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
6 * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/module.h>
16 #include <linux/device.h>
17 #include <linux/err.h>
18 #include <linux/slab.h>
19 #include <linux/kdev_t.h>
20 #include <linux/idr.h>
21 #include <linux/thermal.h>
22 #include <linux/reboot.h>
23 #include <linux/string.h>
25 #include <net/netlink.h>
26 #include <net/genetlink.h>
27 #include <linux/suspend.h>
29 #define CREATE_TRACE_POINTS
30 #include <trace/events/thermal.h>
32 #include "thermal_core.h"
33 #include "thermal_hwmon.h"
35 MODULE_AUTHOR("Zhang Rui");
36 MODULE_DESCRIPTION("Generic thermal management sysfs support");
37 MODULE_LICENSE("GPL v2");
39 static DEFINE_IDR(thermal_tz_idr
);
40 static DEFINE_IDR(thermal_cdev_idr
);
41 static DEFINE_MUTEX(thermal_idr_lock
);
43 static LIST_HEAD(thermal_tz_list
);
44 static LIST_HEAD(thermal_cdev_list
);
45 static LIST_HEAD(thermal_governor_list
);
47 static DEFINE_MUTEX(thermal_list_lock
);
48 static DEFINE_MUTEX(thermal_governor_lock
);
50 static atomic_t in_suspend
;
52 static struct thermal_governor
*def_governor
;
55 * Governor section: set of functions to handle thermal governors
57 * Functions to help in the life cycle of thermal governors within
58 * the thermal core and by the thermal governor code.
61 static struct thermal_governor
*__find_governor(const char *name
)
63 struct thermal_governor
*pos
;
65 if (!name
|| !name
[0])
68 list_for_each_entry(pos
, &thermal_governor_list
, governor_list
)
69 if (!strncasecmp(name
, pos
->name
, THERMAL_NAME_LENGTH
))
76 * bind_previous_governor() - bind the previous governor of the thermal zone
77 * @tz: a valid pointer to a struct thermal_zone_device
78 * @failed_gov_name: the name of the governor that failed to register
80 * Register the previous governor of the thermal zone after a new
81 * governor has failed to be bound.
83 static void bind_previous_governor(struct thermal_zone_device
*tz
,
84 const char *failed_gov_name
)
86 if (tz
->governor
&& tz
->governor
->bind_to_tz
) {
87 if (tz
->governor
->bind_to_tz(tz
)) {
89 "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n",
90 failed_gov_name
, tz
->governor
->name
, tz
->type
);
97 * thermal_set_governor() - Switch to another governor
98 * @tz: a valid pointer to a struct thermal_zone_device
99 * @new_gov: pointer to the new governor
101 * Change the governor of thermal zone @tz.
103 * Return: 0 on success, an error if the new governor's bind_to_tz() failed.
105 static int thermal_set_governor(struct thermal_zone_device
*tz
,
106 struct thermal_governor
*new_gov
)
110 if (tz
->governor
&& tz
->governor
->unbind_from_tz
)
111 tz
->governor
->unbind_from_tz(tz
);
113 if (new_gov
&& new_gov
->bind_to_tz
) {
114 ret
= new_gov
->bind_to_tz(tz
);
116 bind_previous_governor(tz
, new_gov
->name
);
122 tz
->governor
= new_gov
;
127 int thermal_register_governor(struct thermal_governor
*governor
)
131 struct thermal_zone_device
*pos
;
136 mutex_lock(&thermal_governor_lock
);
139 if (!__find_governor(governor
->name
)) {
143 list_add(&governor
->governor_list
, &thermal_governor_list
);
144 match_default
= !strncmp(governor
->name
,
145 DEFAULT_THERMAL_GOVERNOR
,
146 THERMAL_NAME_LENGTH
);
148 if (!def_governor
&& match_default
)
149 def_governor
= governor
;
152 mutex_lock(&thermal_list_lock
);
154 list_for_each_entry(pos
, &thermal_tz_list
, node
) {
156 * only thermal zones with specified tz->tzp->governor_name
157 * may run with tz->govenor unset
162 name
= pos
->tzp
->governor_name
;
164 if (!strncasecmp(name
, governor
->name
, THERMAL_NAME_LENGTH
)) {
167 ret
= thermal_set_governor(pos
, governor
);
169 dev_err(&pos
->device
,
170 "Failed to set governor %s for thermal zone %s: %d\n",
171 governor
->name
, pos
->type
, ret
);
175 mutex_unlock(&thermal_list_lock
);
176 mutex_unlock(&thermal_governor_lock
);
181 void thermal_unregister_governor(struct thermal_governor
*governor
)
183 struct thermal_zone_device
*pos
;
188 mutex_lock(&thermal_governor_lock
);
190 if (!__find_governor(governor
->name
))
193 mutex_lock(&thermal_list_lock
);
195 list_for_each_entry(pos
, &thermal_tz_list
, node
) {
196 if (!strncasecmp(pos
->governor
->name
, governor
->name
,
197 THERMAL_NAME_LENGTH
))
198 thermal_set_governor(pos
, NULL
);
201 mutex_unlock(&thermal_list_lock
);
202 list_del(&governor
->governor_list
);
204 mutex_unlock(&thermal_governor_lock
);
207 int thermal_zone_device_set_policy(struct thermal_zone_device
*tz
,
210 struct thermal_governor
*gov
;
213 mutex_lock(&thermal_governor_lock
);
214 mutex_lock(&tz
->lock
);
216 gov
= __find_governor(strim(policy
));
220 ret
= thermal_set_governor(tz
, gov
);
223 mutex_unlock(&tz
->lock
);
224 mutex_unlock(&thermal_governor_lock
);
229 int thermal_build_list_of_policies(char *buf
)
231 struct thermal_governor
*pos
;
233 ssize_t size
= PAGE_SIZE
;
235 mutex_lock(&thermal_governor_lock
);
237 list_for_each_entry(pos
, &thermal_governor_list
, governor_list
) {
238 size
= PAGE_SIZE
- count
;
239 count
+= scnprintf(buf
+ count
, size
, "%s ", pos
->name
);
241 count
+= scnprintf(buf
+ count
, size
, "\n");
243 mutex_unlock(&thermal_governor_lock
);
248 static int __init
thermal_register_governors(void)
252 result
= thermal_gov_step_wise_register();
256 result
= thermal_gov_fair_share_register();
260 result
= thermal_gov_bang_bang_register();
264 result
= thermal_gov_user_space_register();
268 return thermal_gov_power_allocator_register();
271 static void thermal_unregister_governors(void)
273 thermal_gov_step_wise_unregister();
274 thermal_gov_fair_share_unregister();
275 thermal_gov_bang_bang_unregister();
276 thermal_gov_user_space_unregister();
277 thermal_gov_power_allocator_unregister();
281 * Zone update section: main control loop applied to each zone while monitoring
283 * in polling mode. The monitoring is done using a workqueue.
284 * Same update may be done on a zone by calling thermal_zone_device_update().
287 * - Non-critical trips will invoke the governor responsible for that zone;
288 * - Hot trips will produce a notification to userspace;
289 * - Critical trip point will cause a system shutdown.
291 static void thermal_zone_device_set_polling(struct thermal_zone_device
*tz
,
295 mod_delayed_work(system_freezable_wq
, &tz
->poll_queue
,
296 round_jiffies(msecs_to_jiffies(delay
)));
298 mod_delayed_work(system_freezable_wq
, &tz
->poll_queue
,
299 msecs_to_jiffies(delay
));
301 cancel_delayed_work(&tz
->poll_queue
);
304 static void monitor_thermal_zone(struct thermal_zone_device
*tz
)
306 mutex_lock(&tz
->lock
);
309 thermal_zone_device_set_polling(tz
, tz
->passive_delay
);
310 else if (tz
->polling_delay
)
311 thermal_zone_device_set_polling(tz
, tz
->polling_delay
);
313 thermal_zone_device_set_polling(tz
, 0);
315 mutex_unlock(&tz
->lock
);
318 static void handle_non_critical_trips(struct thermal_zone_device
*tz
,
320 enum thermal_trip_type trip_type
)
322 tz
->governor
? tz
->governor
->throttle(tz
, trip
) :
323 def_governor
->throttle(tz
, trip
);
326 static void handle_critical_trips(struct thermal_zone_device
*tz
,
327 int trip
, enum thermal_trip_type trip_type
)
331 tz
->ops
->get_trip_temp(tz
, trip
, &trip_temp
);
333 /* If we have not crossed the trip_temp, we do not care. */
334 if (trip_temp
<= 0 || tz
->temperature
< trip_temp
)
337 trace_thermal_zone_trip(tz
, trip
, trip_type
);
340 tz
->ops
->notify(tz
, trip
, trip_type
);
342 if (trip_type
== THERMAL_TRIP_CRITICAL
) {
343 dev_emerg(&tz
->device
,
344 "critical temperature reached(%d C),shutting down\n",
345 tz
->temperature
/ 1000);
346 orderly_poweroff(true);
350 static void handle_thermal_trip(struct thermal_zone_device
*tz
, int trip
)
352 enum thermal_trip_type type
;
354 /* Ignore disabled trip points */
355 if (test_bit(trip
, &tz
->trips_disabled
))
358 tz
->ops
->get_trip_type(tz
, trip
, &type
);
360 if (type
== THERMAL_TRIP_CRITICAL
|| type
== THERMAL_TRIP_HOT
)
361 handle_critical_trips(tz
, trip
, type
);
363 handle_non_critical_trips(tz
, trip
, type
);
365 * Alright, we handled this trip successfully.
366 * So, start monitoring again.
368 monitor_thermal_zone(tz
);
371 static void update_temperature(struct thermal_zone_device
*tz
)
375 ret
= thermal_zone_get_temp(tz
, &temp
);
378 dev_warn(&tz
->device
,
379 "failed to read out thermal zone (%d)\n",
384 mutex_lock(&tz
->lock
);
385 tz
->last_temperature
= tz
->temperature
;
386 tz
->temperature
= temp
;
387 mutex_unlock(&tz
->lock
);
389 trace_thermal_temperature(tz
);
390 if (tz
->last_temperature
== THERMAL_TEMP_INVALID
)
391 dev_dbg(&tz
->device
, "last_temperature N/A, current_temperature=%d\n",
394 dev_dbg(&tz
->device
, "last_temperature=%d, current_temperature=%d\n",
395 tz
->last_temperature
, tz
->temperature
);
398 static void thermal_zone_device_reset(struct thermal_zone_device
*tz
)
400 struct thermal_instance
*pos
;
402 tz
->temperature
= THERMAL_TEMP_INVALID
;
404 list_for_each_entry(pos
, &tz
->thermal_instances
, tz_node
)
405 pos
->initialized
= false;
408 void thermal_zone_device_update(struct thermal_zone_device
*tz
,
409 enum thermal_notify_event event
)
413 if (atomic_read(&in_suspend
))
416 if (!tz
->ops
->get_temp
)
419 update_temperature(tz
);
421 thermal_zone_set_trips(tz
);
423 tz
->notify_event
= event
;
425 for (count
= 0; count
< tz
->trips
; count
++)
426 handle_thermal_trip(tz
, count
);
428 EXPORT_SYMBOL_GPL(thermal_zone_device_update
);
431 * thermal_notify_framework - Sensor drivers use this API to notify framework
432 * @tz: thermal zone device
433 * @trip: indicates which trip point has been crossed
435 * This function handles the trip events from sensor drivers. It starts
436 * throttling the cooling devices according to the policy configured.
437 * For CRITICAL and HOT trip points, this notifies the respective drivers,
438 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
439 * The throttling policy is based on the configured platform data; if no
440 * platform data is provided, this uses the step_wise throttling policy.
442 void thermal_notify_framework(struct thermal_zone_device
*tz
, int trip
)
444 handle_thermal_trip(tz
, trip
);
446 EXPORT_SYMBOL_GPL(thermal_notify_framework
);
448 static void thermal_zone_device_check(struct work_struct
*work
)
450 struct thermal_zone_device
*tz
= container_of(work
, struct
453 thermal_zone_device_update(tz
, THERMAL_EVENT_UNSPECIFIED
);
457 * Power actor section: interface to power actors to estimate power
459 * Set of functions used to interact to cooling devices that know
460 * how to estimate their devices power consumption.
464 * power_actor_get_max_power() - get the maximum power that a cdev can consume
465 * @cdev: pointer to &thermal_cooling_device
466 * @tz: a valid thermal zone device pointer
467 * @max_power: pointer in which to store the maximum power
469 * Calculate the maximum power consumption in milliwats that the
470 * cooling device can currently consume and store it in @max_power.
472 * Return: 0 on success, -EINVAL if @cdev doesn't support the
473 * power_actor API or -E* on other error.
475 int power_actor_get_max_power(struct thermal_cooling_device
*cdev
,
476 struct thermal_zone_device
*tz
, u32
*max_power
)
478 if (!cdev_is_power_actor(cdev
))
481 return cdev
->ops
->state2power(cdev
, tz
, 0, max_power
);
485 * power_actor_get_min_power() - get the mainimum power that a cdev can consume
486 * @cdev: pointer to &thermal_cooling_device
487 * @tz: a valid thermal zone device pointer
488 * @min_power: pointer in which to store the minimum power
490 * Calculate the minimum power consumption in milliwatts that the
491 * cooling device can currently consume and store it in @min_power.
493 * Return: 0 on success, -EINVAL if @cdev doesn't support the
494 * power_actor API or -E* on other error.
496 int power_actor_get_min_power(struct thermal_cooling_device
*cdev
,
497 struct thermal_zone_device
*tz
, u32
*min_power
)
499 unsigned long max_state
;
502 if (!cdev_is_power_actor(cdev
))
505 ret
= cdev
->ops
->get_max_state(cdev
, &max_state
);
509 return cdev
->ops
->state2power(cdev
, tz
, max_state
, min_power
);
513 * power_actor_set_power() - limit the maximum power a cooling device consumes
514 * @cdev: pointer to &thermal_cooling_device
515 * @instance: thermal instance to update
516 * @power: the power in milliwatts
518 * Set the cooling device to consume at most @power milliwatts. The limit is
519 * expected to be a cap at the maximum power consumption.
521 * Return: 0 on success, -EINVAL if the cooling device does not
522 * implement the power actor API or -E* for other failures.
524 int power_actor_set_power(struct thermal_cooling_device
*cdev
,
525 struct thermal_instance
*instance
, u32 power
)
530 if (!cdev_is_power_actor(cdev
))
533 ret
= cdev
->ops
->power2state(cdev
, instance
->tz
, power
, &state
);
537 instance
->target
= state
;
538 mutex_lock(&cdev
->lock
);
539 cdev
->updated
= false;
540 mutex_unlock(&cdev
->lock
);
541 thermal_cdev_update(cdev
);
546 void thermal_zone_device_rebind_exception(struct thermal_zone_device
*tz
,
547 const char *cdev_type
, size_t size
)
549 struct thermal_cooling_device
*cdev
= NULL
;
551 mutex_lock(&thermal_list_lock
);
552 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
553 /* skip non matching cdevs */
554 if (strncmp(cdev_type
, cdev
->type
, size
))
557 /* re binding the exception matching the type pattern */
558 thermal_zone_bind_cooling_device(tz
, THERMAL_TRIPS_NONE
, cdev
,
561 THERMAL_WEIGHT_DEFAULT
);
563 mutex_unlock(&thermal_list_lock
);
566 void thermal_zone_device_unbind_exception(struct thermal_zone_device
*tz
,
567 const char *cdev_type
, size_t size
)
569 struct thermal_cooling_device
*cdev
= NULL
;
571 mutex_lock(&thermal_list_lock
);
572 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
573 /* skip non matching cdevs */
574 if (strncmp(cdev_type
, cdev
->type
, size
))
576 /* unbinding the exception matching the type pattern */
577 thermal_zone_unbind_cooling_device(tz
, THERMAL_TRIPS_NONE
,
580 mutex_unlock(&thermal_list_lock
);
584 * Device management section: cooling devices, zones devices, and binding
586 * Set of functions provided by the thermal core for:
587 * - cooling devices lifecycle: registration, unregistration,
588 * binding, and unbinding.
589 * - thermal zone devices lifecycle: registration, unregistration,
590 * binding, and unbinding.
592 static int get_idr(struct idr
*idr
, struct mutex
*lock
, int *id
)
598 ret
= idr_alloc(idr
, NULL
, 0, 0, GFP_KERNEL
);
601 if (unlikely(ret
< 0))
607 static void release_idr(struct idr
*idr
, struct mutex
*lock
, int id
)
617 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
618 * @tz: pointer to struct thermal_zone_device
619 * @trip: indicates which trip point the cooling devices is
620 * associated with in this thermal zone.
621 * @cdev: pointer to struct thermal_cooling_device
622 * @upper: the Maximum cooling state for this trip point.
623 * THERMAL_NO_LIMIT means no upper limit,
624 * and the cooling device can be in max_state.
625 * @lower: the Minimum cooling state can be used for this trip point.
626 * THERMAL_NO_LIMIT means no lower limit,
627 * and the cooling device can be in cooling state 0.
628 * @weight: The weight of the cooling device to be bound to the
629 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
632 * This interface function bind a thermal cooling device to the certain trip
633 * point of a thermal zone device.
634 * This function is usually called in the thermal zone device .bind callback.
636 * Return: 0 on success, the proper error value otherwise.
638 int thermal_zone_bind_cooling_device(struct thermal_zone_device
*tz
,
640 struct thermal_cooling_device
*cdev
,
641 unsigned long upper
, unsigned long lower
,
644 struct thermal_instance
*dev
;
645 struct thermal_instance
*pos
;
646 struct thermal_zone_device
*pos1
;
647 struct thermal_cooling_device
*pos2
;
648 unsigned long max_state
;
651 if (trip
>= tz
->trips
|| (trip
< 0 && trip
!= THERMAL_TRIPS_NONE
))
654 list_for_each_entry(pos1
, &thermal_tz_list
, node
) {
658 list_for_each_entry(pos2
, &thermal_cdev_list
, node
) {
663 if (tz
!= pos1
|| cdev
!= pos2
)
666 ret
= cdev
->ops
->get_max_state(cdev
, &max_state
);
670 /* lower default 0, upper default max_state */
671 lower
= lower
== THERMAL_NO_LIMIT
? 0 : lower
;
672 upper
= upper
== THERMAL_NO_LIMIT
? max_state
: upper
;
674 if (lower
> upper
|| upper
> max_state
)
677 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
685 dev
->target
= THERMAL_NO_TARGET
;
686 dev
->weight
= weight
;
688 result
= get_idr(&tz
->idr
, &tz
->lock
, &dev
->id
);
692 sprintf(dev
->name
, "cdev%d", dev
->id
);
694 sysfs_create_link(&tz
->device
.kobj
, &cdev
->device
.kobj
, dev
->name
);
698 sprintf(dev
->attr_name
, "cdev%d_trip_point", dev
->id
);
699 sysfs_attr_init(&dev
->attr
.attr
);
700 dev
->attr
.attr
.name
= dev
->attr_name
;
701 dev
->attr
.attr
.mode
= 0444;
702 dev
->attr
.show
= thermal_cooling_device_trip_point_show
;
703 result
= device_create_file(&tz
->device
, &dev
->attr
);
705 goto remove_symbol_link
;
707 sprintf(dev
->weight_attr_name
, "cdev%d_weight", dev
->id
);
708 sysfs_attr_init(&dev
->weight_attr
.attr
);
709 dev
->weight_attr
.attr
.name
= dev
->weight_attr_name
;
710 dev
->weight_attr
.attr
.mode
= S_IWUSR
| S_IRUGO
;
711 dev
->weight_attr
.show
= thermal_cooling_device_weight_show
;
712 dev
->weight_attr
.store
= thermal_cooling_device_weight_store
;
713 result
= device_create_file(&tz
->device
, &dev
->weight_attr
);
715 goto remove_trip_file
;
717 mutex_lock(&tz
->lock
);
718 mutex_lock(&cdev
->lock
);
719 list_for_each_entry(pos
, &tz
->thermal_instances
, tz_node
)
720 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
725 list_add_tail(&dev
->tz_node
, &tz
->thermal_instances
);
726 list_add_tail(&dev
->cdev_node
, &cdev
->thermal_instances
);
727 atomic_set(&tz
->need_update
, 1);
729 mutex_unlock(&cdev
->lock
);
730 mutex_unlock(&tz
->lock
);
735 device_remove_file(&tz
->device
, &dev
->weight_attr
);
737 device_remove_file(&tz
->device
, &dev
->attr
);
739 sysfs_remove_link(&tz
->device
.kobj
, dev
->name
);
741 release_idr(&tz
->idr
, &tz
->lock
, dev
->id
);
746 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device
);
749 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
751 * @tz: pointer to a struct thermal_zone_device.
752 * @trip: indicates which trip point the cooling devices is
753 * associated with in this thermal zone.
754 * @cdev: pointer to a struct thermal_cooling_device.
756 * This interface function unbind a thermal cooling device from the certain
757 * trip point of a thermal zone device.
758 * This function is usually called in the thermal zone device .unbind callback.
760 * Return: 0 on success, the proper error value otherwise.
762 int thermal_zone_unbind_cooling_device(struct thermal_zone_device
*tz
,
764 struct thermal_cooling_device
*cdev
)
766 struct thermal_instance
*pos
, *next
;
768 mutex_lock(&tz
->lock
);
769 mutex_lock(&cdev
->lock
);
770 list_for_each_entry_safe(pos
, next
, &tz
->thermal_instances
, tz_node
) {
771 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
772 list_del(&pos
->tz_node
);
773 list_del(&pos
->cdev_node
);
774 mutex_unlock(&cdev
->lock
);
775 mutex_unlock(&tz
->lock
);
779 mutex_unlock(&cdev
->lock
);
780 mutex_unlock(&tz
->lock
);
785 device_remove_file(&tz
->device
, &pos
->weight_attr
);
786 device_remove_file(&tz
->device
, &pos
->attr
);
787 sysfs_remove_link(&tz
->device
.kobj
, pos
->name
);
788 release_idr(&tz
->idr
, &tz
->lock
, pos
->id
);
792 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device
);
794 static void thermal_release(struct device
*dev
)
796 struct thermal_zone_device
*tz
;
797 struct thermal_cooling_device
*cdev
;
799 if (!strncmp(dev_name(dev
), "thermal_zone",
800 sizeof("thermal_zone") - 1)) {
801 tz
= to_thermal_zone(dev
);
802 kfree(tz
->trip_type_attrs
);
803 kfree(tz
->trip_temp_attrs
);
804 kfree(tz
->trip_hyst_attrs
);
805 kfree(tz
->trips_attribute_group
.attrs
);
806 kfree(tz
->device
.groups
);
808 } else if (!strncmp(dev_name(dev
), "cooling_device",
809 sizeof("cooling_device") - 1)) {
810 cdev
= to_cooling_device(dev
);
815 static struct class thermal_class
= {
817 .dev_release
= thermal_release
,
821 void print_bind_err_msg(struct thermal_zone_device
*tz
,
822 struct thermal_cooling_device
*cdev
, int ret
)
824 dev_err(&tz
->device
, "binding zone %s with cdev %s failed:%d\n",
825 tz
->type
, cdev
->type
, ret
);
828 static void __bind(struct thermal_zone_device
*tz
, int mask
,
829 struct thermal_cooling_device
*cdev
,
830 unsigned long *limits
,
835 for (i
= 0; i
< tz
->trips
; i
++) {
836 if (mask
& (1 << i
)) {
837 unsigned long upper
, lower
;
839 upper
= THERMAL_NO_LIMIT
;
840 lower
= THERMAL_NO_LIMIT
;
842 lower
= limits
[i
* 2];
843 upper
= limits
[i
* 2 + 1];
845 ret
= thermal_zone_bind_cooling_device(tz
, i
, cdev
,
849 print_bind_err_msg(tz
, cdev
, ret
);
854 static void bind_cdev(struct thermal_cooling_device
*cdev
)
857 const struct thermal_zone_params
*tzp
;
858 struct thermal_zone_device
*pos
= NULL
;
860 mutex_lock(&thermal_list_lock
);
862 list_for_each_entry(pos
, &thermal_tz_list
, node
) {
863 if (!pos
->tzp
&& !pos
->ops
->bind
)
866 if (pos
->ops
->bind
) {
867 ret
= pos
->ops
->bind(pos
, cdev
);
869 print_bind_err_msg(pos
, cdev
, ret
);
874 if (!tzp
|| !tzp
->tbp
)
877 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
878 if (tzp
->tbp
[i
].cdev
|| !tzp
->tbp
[i
].match
)
880 if (tzp
->tbp
[i
].match(pos
, cdev
))
882 tzp
->tbp
[i
].cdev
= cdev
;
883 __bind(pos
, tzp
->tbp
[i
].trip_mask
, cdev
,
884 tzp
->tbp
[i
].binding_limits
,
889 mutex_unlock(&thermal_list_lock
);
893 * __thermal_cooling_device_register() - register a new thermal cooling device
894 * @np: a pointer to a device tree node.
895 * @type: the thermal cooling device type.
896 * @devdata: device private data.
897 * @ops: standard thermal cooling devices callbacks.
899 * This interface function adds a new thermal cooling device (fan/processor/...)
900 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
901 * to all the thermal zone devices registered at the same time.
902 * It also gives the opportunity to link the cooling device to a device tree
903 * node, so that it can be bound to a thermal zone created out of device tree.
905 * Return: a pointer to the created struct thermal_cooling_device or an
906 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
908 static struct thermal_cooling_device
*
909 __thermal_cooling_device_register(struct device_node
*np
,
910 char *type
, void *devdata
,
911 const struct thermal_cooling_device_ops
*ops
)
913 struct thermal_cooling_device
*cdev
;
914 struct thermal_zone_device
*pos
= NULL
;
917 if (type
&& strlen(type
) >= THERMAL_NAME_LENGTH
)
918 return ERR_PTR(-EINVAL
);
920 if (!ops
|| !ops
->get_max_state
|| !ops
->get_cur_state
||
922 return ERR_PTR(-EINVAL
);
924 cdev
= kzalloc(sizeof(*cdev
), GFP_KERNEL
);
926 return ERR_PTR(-ENOMEM
);
928 result
= get_idr(&thermal_cdev_idr
, &thermal_idr_lock
, &cdev
->id
);
931 return ERR_PTR(result
);
934 strlcpy(cdev
->type
, type
? : "", sizeof(cdev
->type
));
935 mutex_init(&cdev
->lock
);
936 INIT_LIST_HEAD(&cdev
->thermal_instances
);
939 cdev
->updated
= false;
940 cdev
->device
.class = &thermal_class
;
941 thermal_cooling_device_setup_sysfs(cdev
);
942 cdev
->devdata
= devdata
;
943 dev_set_name(&cdev
->device
, "cooling_device%d", cdev
->id
);
944 result
= device_register(&cdev
->device
);
946 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
948 return ERR_PTR(result
);
951 /* Add 'this' new cdev to the global cdev list */
952 mutex_lock(&thermal_list_lock
);
953 list_add(&cdev
->node
, &thermal_cdev_list
);
954 mutex_unlock(&thermal_list_lock
);
956 /* Update binding information for 'this' new cdev */
959 mutex_lock(&thermal_list_lock
);
960 list_for_each_entry(pos
, &thermal_tz_list
, node
)
961 if (atomic_cmpxchg(&pos
->need_update
, 1, 0))
962 thermal_zone_device_update(pos
,
963 THERMAL_EVENT_UNSPECIFIED
);
964 mutex_unlock(&thermal_list_lock
);
970 * thermal_cooling_device_register() - register a new thermal cooling device
971 * @type: the thermal cooling device type.
972 * @devdata: device private data.
973 * @ops: standard thermal cooling devices callbacks.
975 * This interface function adds a new thermal cooling device (fan/processor/...)
976 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
977 * to all the thermal zone devices registered at the same time.
979 * Return: a pointer to the created struct thermal_cooling_device or an
980 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
982 struct thermal_cooling_device
*
983 thermal_cooling_device_register(char *type
, void *devdata
,
984 const struct thermal_cooling_device_ops
*ops
)
986 return __thermal_cooling_device_register(NULL
, type
, devdata
, ops
);
988 EXPORT_SYMBOL_GPL(thermal_cooling_device_register
);
991 * thermal_of_cooling_device_register() - register an OF thermal cooling device
992 * @np: a pointer to a device tree node.
993 * @type: the thermal cooling device type.
994 * @devdata: device private data.
995 * @ops: standard thermal cooling devices callbacks.
997 * This function will register a cooling device with device tree node reference.
998 * This interface function adds a new thermal cooling device (fan/processor/...)
999 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1000 * to all the thermal zone devices registered at the same time.
1002 * Return: a pointer to the created struct thermal_cooling_device or an
1003 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1005 struct thermal_cooling_device
*
1006 thermal_of_cooling_device_register(struct device_node
*np
,
1007 char *type
, void *devdata
,
1008 const struct thermal_cooling_device_ops
*ops
)
1010 return __thermal_cooling_device_register(np
, type
, devdata
, ops
);
1012 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register
);
1014 static void __unbind(struct thermal_zone_device
*tz
, int mask
,
1015 struct thermal_cooling_device
*cdev
)
1019 for (i
= 0; i
< tz
->trips
; i
++)
1020 if (mask
& (1 << i
))
1021 thermal_zone_unbind_cooling_device(tz
, i
, cdev
);
1025 * thermal_cooling_device_unregister - removes a thermal cooling device
1026 * @cdev: the thermal cooling device to remove.
1028 * thermal_cooling_device_unregister() must be called when a registered
1029 * thermal cooling device is no longer needed.
1031 void thermal_cooling_device_unregister(struct thermal_cooling_device
*cdev
)
1034 const struct thermal_zone_params
*tzp
;
1035 struct thermal_zone_device
*tz
;
1036 struct thermal_cooling_device
*pos
= NULL
;
1041 mutex_lock(&thermal_list_lock
);
1042 list_for_each_entry(pos
, &thermal_cdev_list
, node
)
1046 /* thermal cooling device not found */
1047 mutex_unlock(&thermal_list_lock
);
1050 list_del(&cdev
->node
);
1052 /* Unbind all thermal zones associated with 'this' cdev */
1053 list_for_each_entry(tz
, &thermal_tz_list
, node
) {
1054 if (tz
->ops
->unbind
) {
1055 tz
->ops
->unbind(tz
, cdev
);
1059 if (!tz
->tzp
|| !tz
->tzp
->tbp
)
1063 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
1064 if (tzp
->tbp
[i
].cdev
== cdev
) {
1065 __unbind(tz
, tzp
->tbp
[i
].trip_mask
, cdev
);
1066 tzp
->tbp
[i
].cdev
= NULL
;
1071 mutex_unlock(&thermal_list_lock
);
1073 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
1074 device_unregister(&cdev
->device
);
1076 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister
);
1078 static void bind_tz(struct thermal_zone_device
*tz
)
1081 struct thermal_cooling_device
*pos
= NULL
;
1082 const struct thermal_zone_params
*tzp
= tz
->tzp
;
1084 if (!tzp
&& !tz
->ops
->bind
)
1087 mutex_lock(&thermal_list_lock
);
1089 /* If there is ops->bind, try to use ops->bind */
1090 if (tz
->ops
->bind
) {
1091 list_for_each_entry(pos
, &thermal_cdev_list
, node
) {
1092 ret
= tz
->ops
->bind(tz
, pos
);
1094 print_bind_err_msg(tz
, pos
, ret
);
1099 if (!tzp
|| !tzp
->tbp
)
1102 list_for_each_entry(pos
, &thermal_cdev_list
, node
) {
1103 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
1104 if (tzp
->tbp
[i
].cdev
|| !tzp
->tbp
[i
].match
)
1106 if (tzp
->tbp
[i
].match(tz
, pos
))
1108 tzp
->tbp
[i
].cdev
= pos
;
1109 __bind(tz
, tzp
->tbp
[i
].trip_mask
, pos
,
1110 tzp
->tbp
[i
].binding_limits
,
1111 tzp
->tbp
[i
].weight
);
1115 mutex_unlock(&thermal_list_lock
);
1119 * thermal_zone_device_register() - register a new thermal zone device
1120 * @type: the thermal zone device type
1121 * @trips: the number of trip points the thermal zone support
1122 * @mask: a bit string indicating the writeablility of trip points
1123 * @devdata: private device data
1124 * @ops: standard thermal zone device callbacks
1125 * @tzp: thermal zone platform parameters
1126 * @passive_delay: number of milliseconds to wait between polls when
1127 * performing passive cooling
1128 * @polling_delay: number of milliseconds to wait between polls when checking
1129 * whether trip points have been crossed (0 for interrupt
1132 * This interface function adds a new thermal zone device (sensor) to
1133 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1134 * thermal cooling devices registered at the same time.
1135 * thermal_zone_device_unregister() must be called when the device is no
1136 * longer needed. The passive cooling depends on the .get_trend() return value.
1138 * Return: a pointer to the created struct thermal_zone_device or an
1139 * in case of error, an ERR_PTR. Caller must check return value with
1140 * IS_ERR*() helpers.
1142 struct thermal_zone_device
*
1143 thermal_zone_device_register(const char *type
, int trips
, int mask
,
1144 void *devdata
, struct thermal_zone_device_ops
*ops
,
1145 struct thermal_zone_params
*tzp
, int passive_delay
,
1148 struct thermal_zone_device
*tz
;
1149 enum thermal_trip_type trip_type
;
1153 struct thermal_governor
*governor
;
1155 if (!type
|| strlen(type
) == 0)
1156 return ERR_PTR(-EINVAL
);
1158 if (type
&& strlen(type
) >= THERMAL_NAME_LENGTH
)
1159 return ERR_PTR(-EINVAL
);
1161 if (trips
> THERMAL_MAX_TRIPS
|| trips
< 0 || mask
>> trips
)
1162 return ERR_PTR(-EINVAL
);
1165 return ERR_PTR(-EINVAL
);
1167 if (trips
> 0 && (!ops
->get_trip_type
|| !ops
->get_trip_temp
))
1168 return ERR_PTR(-EINVAL
);
1170 tz
= kzalloc(sizeof(*tz
), GFP_KERNEL
);
1172 return ERR_PTR(-ENOMEM
);
1174 INIT_LIST_HEAD(&tz
->thermal_instances
);
1176 mutex_init(&tz
->lock
);
1177 result
= get_idr(&thermal_tz_idr
, &thermal_idr_lock
, &tz
->id
);
1180 return ERR_PTR(result
);
1183 strlcpy(tz
->type
, type
, sizeof(tz
->type
));
1186 tz
->device
.class = &thermal_class
;
1187 tz
->devdata
= devdata
;
1189 tz
->passive_delay
= passive_delay
;
1190 tz
->polling_delay
= polling_delay
;
1193 /* Add nodes that are always present via .groups */
1194 result
= thermal_zone_create_device_groups(tz
, mask
);
1198 /* A new thermal zone needs to be updated anyway. */
1199 atomic_set(&tz
->need_update
, 1);
1201 dev_set_name(&tz
->device
, "thermal_zone%d", tz
->id
);
1202 result
= device_register(&tz
->device
);
1204 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
1206 return ERR_PTR(result
);
1209 for (count
= 0; count
< trips
; count
++) {
1210 if (tz
->ops
->get_trip_type(tz
, count
, &trip_type
))
1211 set_bit(count
, &tz
->trips_disabled
);
1212 if (tz
->ops
->get_trip_temp(tz
, count
, &trip_temp
))
1213 set_bit(count
, &tz
->trips_disabled
);
1214 /* Check for bogus trip points */
1216 set_bit(count
, &tz
->trips_disabled
);
1219 /* Update 'this' zone's governor information */
1220 mutex_lock(&thermal_governor_lock
);
1223 governor
= __find_governor(tz
->tzp
->governor_name
);
1225 governor
= def_governor
;
1227 result
= thermal_set_governor(tz
, governor
);
1229 mutex_unlock(&thermal_governor_lock
);
1233 mutex_unlock(&thermal_governor_lock
);
1235 if (!tz
->tzp
|| !tz
->tzp
->no_hwmon
) {
1236 result
= thermal_add_hwmon_sysfs(tz
);
1241 mutex_lock(&thermal_list_lock
);
1242 list_add_tail(&tz
->node
, &thermal_tz_list
);
1243 mutex_unlock(&thermal_list_lock
);
1245 /* Bind cooling devices for this zone */
1248 INIT_DELAYED_WORK(&tz
->poll_queue
, thermal_zone_device_check
);
1250 thermal_zone_device_reset(tz
);
1251 /* Update the new thermal zone and mark it as already updated. */
1252 if (atomic_cmpxchg(&tz
->need_update
, 1, 0))
1253 thermal_zone_device_update(tz
, THERMAL_EVENT_UNSPECIFIED
);
1258 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
1259 device_unregister(&tz
->device
);
1260 return ERR_PTR(result
);
1262 EXPORT_SYMBOL_GPL(thermal_zone_device_register
);
1265 * thermal_device_unregister - removes the registered thermal zone device
1266 * @tz: the thermal zone device to remove
1268 void thermal_zone_device_unregister(struct thermal_zone_device
*tz
)
1271 const struct thermal_zone_params
*tzp
;
1272 struct thermal_cooling_device
*cdev
;
1273 struct thermal_zone_device
*pos
= NULL
;
1280 mutex_lock(&thermal_list_lock
);
1281 list_for_each_entry(pos
, &thermal_tz_list
, node
)
1285 /* thermal zone device not found */
1286 mutex_unlock(&thermal_list_lock
);
1289 list_del(&tz
->node
);
1291 /* Unbind all cdevs associated with 'this' thermal zone */
1292 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
1293 if (tz
->ops
->unbind
) {
1294 tz
->ops
->unbind(tz
, cdev
);
1298 if (!tzp
|| !tzp
->tbp
)
1301 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
1302 if (tzp
->tbp
[i
].cdev
== cdev
) {
1303 __unbind(tz
, tzp
->tbp
[i
].trip_mask
, cdev
);
1304 tzp
->tbp
[i
].cdev
= NULL
;
1309 mutex_unlock(&thermal_list_lock
);
1311 thermal_zone_device_set_polling(tz
, 0);
1313 thermal_set_governor(tz
, NULL
);
1315 thermal_remove_hwmon_sysfs(tz
);
1316 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
1317 idr_destroy(&tz
->idr
);
1318 mutex_destroy(&tz
->lock
);
1319 device_unregister(&tz
->device
);
1321 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister
);
1324 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1325 * @name: thermal zone name to fetch the temperature
1327 * When only one zone is found with the passed name, returns a reference to it.
1329 * Return: On success returns a reference to an unique thermal zone with
1330 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1331 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1333 struct thermal_zone_device
*thermal_zone_get_zone_by_name(const char *name
)
1335 struct thermal_zone_device
*pos
= NULL
, *ref
= ERR_PTR(-EINVAL
);
1336 unsigned int found
= 0;
1341 mutex_lock(&thermal_list_lock
);
1342 list_for_each_entry(pos
, &thermal_tz_list
, node
)
1343 if (!strncasecmp(name
, pos
->type
, THERMAL_NAME_LENGTH
)) {
1347 mutex_unlock(&thermal_list_lock
);
1349 /* nothing has been found, thus an error code for it */
1351 ref
= ERR_PTR(-ENODEV
);
1353 /* Success only when an unique zone is found */
1354 ref
= ERR_PTR(-EEXIST
);
1359 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name
);
1362 static const struct genl_multicast_group thermal_event_mcgrps
[] = {
1363 { .name
= THERMAL_GENL_MCAST_GROUP_NAME
, },
1366 static struct genl_family thermal_event_genl_family __ro_after_init
= {
1367 .module
= THIS_MODULE
,
1368 .name
= THERMAL_GENL_FAMILY_NAME
,
1369 .version
= THERMAL_GENL_VERSION
,
1370 .maxattr
= THERMAL_GENL_ATTR_MAX
,
1371 .mcgrps
= thermal_event_mcgrps
,
1372 .n_mcgrps
= ARRAY_SIZE(thermal_event_mcgrps
),
1375 int thermal_generate_netlink_event(struct thermal_zone_device
*tz
,
1378 struct sk_buff
*skb
;
1379 struct nlattr
*attr
;
1380 struct thermal_genl_event
*thermal_event
;
1384 static unsigned int thermal_event_seqnum
;
1389 /* allocate memory */
1390 size
= nla_total_size(sizeof(struct thermal_genl_event
)) +
1393 skb
= genlmsg_new(size
, GFP_ATOMIC
);
1397 /* add the genetlink message header */
1398 msg_header
= genlmsg_put(skb
, 0, thermal_event_seqnum
++,
1399 &thermal_event_genl_family
, 0,
1400 THERMAL_GENL_CMD_EVENT
);
1407 attr
= nla_reserve(skb
, THERMAL_GENL_ATTR_EVENT
,
1408 sizeof(struct thermal_genl_event
));
1415 thermal_event
= nla_data(attr
);
1416 if (!thermal_event
) {
1421 memset(thermal_event
, 0, sizeof(struct thermal_genl_event
));
1423 thermal_event
->orig
= tz
->id
;
1424 thermal_event
->event
= event
;
1426 /* send multicast genetlink message */
1427 genlmsg_end(skb
, msg_header
);
1429 result
= genlmsg_multicast(&thermal_event_genl_family
, skb
, 0,
1432 dev_err(&tz
->device
, "Failed to send netlink event:%d", result
);
1436 EXPORT_SYMBOL_GPL(thermal_generate_netlink_event
);
1438 static int __init
genetlink_init(void)
1440 return genl_register_family(&thermal_event_genl_family
);
1443 static void genetlink_exit(void)
1445 genl_unregister_family(&thermal_event_genl_family
);
1447 #else /* !CONFIG_NET */
1448 static inline int genetlink_init(void) { return 0; }
1449 static inline void genetlink_exit(void) {}
1450 #endif /* !CONFIG_NET */
1452 static int thermal_pm_notify(struct notifier_block
*nb
,
1453 unsigned long mode
, void *_unused
)
1455 struct thermal_zone_device
*tz
;
1458 case PM_HIBERNATION_PREPARE
:
1459 case PM_RESTORE_PREPARE
:
1460 case PM_SUSPEND_PREPARE
:
1461 atomic_set(&in_suspend
, 1);
1463 case PM_POST_HIBERNATION
:
1464 case PM_POST_RESTORE
:
1465 case PM_POST_SUSPEND
:
1466 atomic_set(&in_suspend
, 0);
1467 list_for_each_entry(tz
, &thermal_tz_list
, node
) {
1468 thermal_zone_device_reset(tz
);
1469 thermal_zone_device_update(tz
,
1470 THERMAL_EVENT_UNSPECIFIED
);
1479 static struct notifier_block thermal_pm_nb
= {
1480 .notifier_call
= thermal_pm_notify
,
1483 static int __init
thermal_init(void)
1487 result
= thermal_register_governors();
1491 result
= class_register(&thermal_class
);
1493 goto unregister_governors
;
1495 result
= genetlink_init();
1497 goto unregister_class
;
1499 result
= of_parse_thermal_zones();
1503 result
= register_pm_notifier(&thermal_pm_nb
);
1505 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1513 class_unregister(&thermal_class
);
1514 unregister_governors
:
1515 thermal_unregister_governors();
1517 idr_destroy(&thermal_tz_idr
);
1518 idr_destroy(&thermal_cdev_idr
);
1519 mutex_destroy(&thermal_idr_lock
);
1520 mutex_destroy(&thermal_list_lock
);
1521 mutex_destroy(&thermal_governor_lock
);
1525 static void __exit
thermal_exit(void)
1527 unregister_pm_notifier(&thermal_pm_nb
);
1528 of_thermal_destroy_zones();
1530 class_unregister(&thermal_class
);
1531 thermal_unregister_governors();
1532 idr_destroy(&thermal_tz_idr
);
1533 idr_destroy(&thermal_cdev_idr
);
1534 mutex_destroy(&thermal_idr_lock
);
1535 mutex_destroy(&thermal_list_lock
);
1536 mutex_destroy(&thermal_governor_lock
);
1539 fs_initcall(thermal_init
);
1540 module_exit(thermal_exit
);