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_IDA(thermal_tz_ida
);
40 static DEFINE_IDA(thermal_cdev_ida
);
42 static LIST_HEAD(thermal_tz_list
);
43 static LIST_HEAD(thermal_cdev_list
);
44 static LIST_HEAD(thermal_governor_list
);
46 static DEFINE_MUTEX(thermal_list_lock
);
47 static DEFINE_MUTEX(thermal_governor_lock
);
49 static atomic_t in_suspend
;
51 static struct thermal_governor
*def_governor
;
54 * Governor section: set of functions to handle thermal governors
56 * Functions to help in the life cycle of thermal governors within
57 * the thermal core and by the thermal governor code.
60 static struct thermal_governor
*__find_governor(const char *name
)
62 struct thermal_governor
*pos
;
64 if (!name
|| !name
[0])
67 list_for_each_entry(pos
, &thermal_governor_list
, governor_list
)
68 if (!strncasecmp(name
, pos
->name
, THERMAL_NAME_LENGTH
))
75 * bind_previous_governor() - bind the previous governor of the thermal zone
76 * @tz: a valid pointer to a struct thermal_zone_device
77 * @failed_gov_name: the name of the governor that failed to register
79 * Register the previous governor of the thermal zone after a new
80 * governor has failed to be bound.
82 static void bind_previous_governor(struct thermal_zone_device
*tz
,
83 const char *failed_gov_name
)
85 if (tz
->governor
&& tz
->governor
->bind_to_tz
) {
86 if (tz
->governor
->bind_to_tz(tz
)) {
88 "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n",
89 failed_gov_name
, tz
->governor
->name
, tz
->type
);
96 * thermal_set_governor() - Switch to another governor
97 * @tz: a valid pointer to a struct thermal_zone_device
98 * @new_gov: pointer to the new governor
100 * Change the governor of thermal zone @tz.
102 * Return: 0 on success, an error if the new governor's bind_to_tz() failed.
104 static int thermal_set_governor(struct thermal_zone_device
*tz
,
105 struct thermal_governor
*new_gov
)
109 if (tz
->governor
&& tz
->governor
->unbind_from_tz
)
110 tz
->governor
->unbind_from_tz(tz
);
112 if (new_gov
&& new_gov
->bind_to_tz
) {
113 ret
= new_gov
->bind_to_tz(tz
);
115 bind_previous_governor(tz
, new_gov
->name
);
121 tz
->governor
= new_gov
;
126 int thermal_register_governor(struct thermal_governor
*governor
)
130 struct thermal_zone_device
*pos
;
135 mutex_lock(&thermal_governor_lock
);
138 if (!__find_governor(governor
->name
)) {
142 list_add(&governor
->governor_list
, &thermal_governor_list
);
143 match_default
= !strncmp(governor
->name
,
144 DEFAULT_THERMAL_GOVERNOR
,
145 THERMAL_NAME_LENGTH
);
147 if (!def_governor
&& match_default
)
148 def_governor
= governor
;
151 mutex_lock(&thermal_list_lock
);
153 list_for_each_entry(pos
, &thermal_tz_list
, node
) {
155 * only thermal zones with specified tz->tzp->governor_name
156 * may run with tz->govenor unset
161 name
= pos
->tzp
->governor_name
;
163 if (!strncasecmp(name
, governor
->name
, THERMAL_NAME_LENGTH
)) {
166 ret
= thermal_set_governor(pos
, governor
);
168 dev_err(&pos
->device
,
169 "Failed to set governor %s for thermal zone %s: %d\n",
170 governor
->name
, pos
->type
, ret
);
174 mutex_unlock(&thermal_list_lock
);
175 mutex_unlock(&thermal_governor_lock
);
180 void thermal_unregister_governor(struct thermal_governor
*governor
)
182 struct thermal_zone_device
*pos
;
187 mutex_lock(&thermal_governor_lock
);
189 if (!__find_governor(governor
->name
))
192 mutex_lock(&thermal_list_lock
);
194 list_for_each_entry(pos
, &thermal_tz_list
, node
) {
195 if (!strncasecmp(pos
->governor
->name
, governor
->name
,
196 THERMAL_NAME_LENGTH
))
197 thermal_set_governor(pos
, NULL
);
200 mutex_unlock(&thermal_list_lock
);
201 list_del(&governor
->governor_list
);
203 mutex_unlock(&thermal_governor_lock
);
206 int thermal_zone_device_set_policy(struct thermal_zone_device
*tz
,
209 struct thermal_governor
*gov
;
212 mutex_lock(&thermal_governor_lock
);
213 mutex_lock(&tz
->lock
);
215 gov
= __find_governor(strim(policy
));
219 ret
= thermal_set_governor(tz
, gov
);
222 mutex_unlock(&tz
->lock
);
223 mutex_unlock(&thermal_governor_lock
);
228 int thermal_build_list_of_policies(char *buf
)
230 struct thermal_governor
*pos
;
232 ssize_t size
= PAGE_SIZE
;
234 mutex_lock(&thermal_governor_lock
);
236 list_for_each_entry(pos
, &thermal_governor_list
, governor_list
) {
237 size
= PAGE_SIZE
- count
;
238 count
+= scnprintf(buf
+ count
, size
, "%s ", pos
->name
);
240 count
+= scnprintf(buf
+ count
, size
, "\n");
242 mutex_unlock(&thermal_governor_lock
);
247 static int __init
thermal_register_governors(void)
251 result
= thermal_gov_step_wise_register();
255 result
= thermal_gov_fair_share_register();
259 result
= thermal_gov_bang_bang_register();
263 result
= thermal_gov_user_space_register();
267 return thermal_gov_power_allocator_register();
270 static void thermal_unregister_governors(void)
272 thermal_gov_step_wise_unregister();
273 thermal_gov_fair_share_unregister();
274 thermal_gov_bang_bang_unregister();
275 thermal_gov_user_space_unregister();
276 thermal_gov_power_allocator_unregister();
280 * Zone update section: main control loop applied to each zone while monitoring
282 * in polling mode. The monitoring is done using a workqueue.
283 * Same update may be done on a zone by calling thermal_zone_device_update().
286 * - Non-critical trips will invoke the governor responsible for that zone;
287 * - Hot trips will produce a notification to userspace;
288 * - Critical trip point will cause a system shutdown.
290 static void thermal_zone_device_set_polling(struct thermal_zone_device
*tz
,
294 mod_delayed_work(system_freezable_wq
, &tz
->poll_queue
,
295 round_jiffies(msecs_to_jiffies(delay
)));
297 mod_delayed_work(system_freezable_wq
, &tz
->poll_queue
,
298 msecs_to_jiffies(delay
));
300 cancel_delayed_work(&tz
->poll_queue
);
303 static void monitor_thermal_zone(struct thermal_zone_device
*tz
)
305 mutex_lock(&tz
->lock
);
308 thermal_zone_device_set_polling(tz
, tz
->passive_delay
);
309 else if (tz
->polling_delay
)
310 thermal_zone_device_set_polling(tz
, tz
->polling_delay
);
312 thermal_zone_device_set_polling(tz
, 0);
314 mutex_unlock(&tz
->lock
);
317 static void handle_non_critical_trips(struct thermal_zone_device
*tz
,
319 enum thermal_trip_type trip_type
)
321 tz
->governor
? tz
->governor
->throttle(tz
, trip
) :
322 def_governor
->throttle(tz
, trip
);
325 static void handle_critical_trips(struct thermal_zone_device
*tz
,
326 int trip
, enum thermal_trip_type trip_type
)
330 tz
->ops
->get_trip_temp(tz
, trip
, &trip_temp
);
332 /* If we have not crossed the trip_temp, we do not care. */
333 if (trip_temp
<= 0 || tz
->temperature
< trip_temp
)
336 trace_thermal_zone_trip(tz
, trip
, trip_type
);
339 tz
->ops
->notify(tz
, trip
, trip_type
);
341 if (trip_type
== THERMAL_TRIP_CRITICAL
) {
342 dev_emerg(&tz
->device
,
343 "critical temperature reached(%d C),shutting down\n",
344 tz
->temperature
/ 1000);
345 orderly_poweroff(true);
349 static void handle_thermal_trip(struct thermal_zone_device
*tz
, int trip
)
351 enum thermal_trip_type type
;
353 /* Ignore disabled trip points */
354 if (test_bit(trip
, &tz
->trips_disabled
))
357 tz
->ops
->get_trip_type(tz
, trip
, &type
);
359 if (type
== THERMAL_TRIP_CRITICAL
|| type
== THERMAL_TRIP_HOT
)
360 handle_critical_trips(tz
, trip
, type
);
362 handle_non_critical_trips(tz
, trip
, type
);
364 * Alright, we handled this trip successfully.
365 * So, start monitoring again.
367 monitor_thermal_zone(tz
);
370 static void update_temperature(struct thermal_zone_device
*tz
)
374 ret
= thermal_zone_get_temp(tz
, &temp
);
377 dev_warn(&tz
->device
,
378 "failed to read out thermal zone (%d)\n",
383 mutex_lock(&tz
->lock
);
384 tz
->last_temperature
= tz
->temperature
;
385 tz
->temperature
= temp
;
386 mutex_unlock(&tz
->lock
);
388 trace_thermal_temperature(tz
);
389 if (tz
->last_temperature
== THERMAL_TEMP_INVALID
)
390 dev_dbg(&tz
->device
, "last_temperature N/A, current_temperature=%d\n",
393 dev_dbg(&tz
->device
, "last_temperature=%d, current_temperature=%d\n",
394 tz
->last_temperature
, tz
->temperature
);
397 static void thermal_zone_device_reset(struct thermal_zone_device
*tz
)
399 struct thermal_instance
*pos
;
401 tz
->temperature
= THERMAL_TEMP_INVALID
;
403 list_for_each_entry(pos
, &tz
->thermal_instances
, tz_node
)
404 pos
->initialized
= false;
407 void thermal_zone_device_update(struct thermal_zone_device
*tz
,
408 enum thermal_notify_event event
)
412 if (atomic_read(&in_suspend
))
415 if (!tz
->ops
->get_temp
)
418 update_temperature(tz
);
420 thermal_zone_set_trips(tz
);
422 tz
->notify_event
= event
;
424 for (count
= 0; count
< tz
->trips
; count
++)
425 handle_thermal_trip(tz
, count
);
427 EXPORT_SYMBOL_GPL(thermal_zone_device_update
);
430 * thermal_notify_framework - Sensor drivers use this API to notify framework
431 * @tz: thermal zone device
432 * @trip: indicates which trip point has been crossed
434 * This function handles the trip events from sensor drivers. It starts
435 * throttling the cooling devices according to the policy configured.
436 * For CRITICAL and HOT trip points, this notifies the respective drivers,
437 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
438 * The throttling policy is based on the configured platform data; if no
439 * platform data is provided, this uses the step_wise throttling policy.
441 void thermal_notify_framework(struct thermal_zone_device
*tz
, int trip
)
443 handle_thermal_trip(tz
, trip
);
445 EXPORT_SYMBOL_GPL(thermal_notify_framework
);
447 static void thermal_zone_device_check(struct work_struct
*work
)
449 struct thermal_zone_device
*tz
= container_of(work
, struct
452 thermal_zone_device_update(tz
, THERMAL_EVENT_UNSPECIFIED
);
456 * Power actor section: interface to power actors to estimate power
458 * Set of functions used to interact to cooling devices that know
459 * how to estimate their devices power consumption.
463 * power_actor_get_max_power() - get the maximum power that a cdev can consume
464 * @cdev: pointer to &thermal_cooling_device
465 * @tz: a valid thermal zone device pointer
466 * @max_power: pointer in which to store the maximum power
468 * Calculate the maximum power consumption in milliwats that the
469 * cooling device can currently consume and store it in @max_power.
471 * Return: 0 on success, -EINVAL if @cdev doesn't support the
472 * power_actor API or -E* on other error.
474 int power_actor_get_max_power(struct thermal_cooling_device
*cdev
,
475 struct thermal_zone_device
*tz
, u32
*max_power
)
477 if (!cdev_is_power_actor(cdev
))
480 return cdev
->ops
->state2power(cdev
, tz
, 0, max_power
);
484 * power_actor_get_min_power() - get the mainimum power that a cdev can consume
485 * @cdev: pointer to &thermal_cooling_device
486 * @tz: a valid thermal zone device pointer
487 * @min_power: pointer in which to store the minimum power
489 * Calculate the minimum power consumption in milliwatts that the
490 * cooling device can currently consume and store it in @min_power.
492 * Return: 0 on success, -EINVAL if @cdev doesn't support the
493 * power_actor API or -E* on other error.
495 int power_actor_get_min_power(struct thermal_cooling_device
*cdev
,
496 struct thermal_zone_device
*tz
, u32
*min_power
)
498 unsigned long max_state
;
501 if (!cdev_is_power_actor(cdev
))
504 ret
= cdev
->ops
->get_max_state(cdev
, &max_state
);
508 return cdev
->ops
->state2power(cdev
, tz
, max_state
, min_power
);
512 * power_actor_set_power() - limit the maximum power a cooling device consumes
513 * @cdev: pointer to &thermal_cooling_device
514 * @instance: thermal instance to update
515 * @power: the power in milliwatts
517 * Set the cooling device to consume at most @power milliwatts. The limit is
518 * expected to be a cap at the maximum power consumption.
520 * Return: 0 on success, -EINVAL if the cooling device does not
521 * implement the power actor API or -E* for other failures.
523 int power_actor_set_power(struct thermal_cooling_device
*cdev
,
524 struct thermal_instance
*instance
, u32 power
)
529 if (!cdev_is_power_actor(cdev
))
532 ret
= cdev
->ops
->power2state(cdev
, instance
->tz
, power
, &state
);
536 instance
->target
= state
;
537 mutex_lock(&cdev
->lock
);
538 cdev
->updated
= false;
539 mutex_unlock(&cdev
->lock
);
540 thermal_cdev_update(cdev
);
545 void thermal_zone_device_rebind_exception(struct thermal_zone_device
*tz
,
546 const char *cdev_type
, size_t size
)
548 struct thermal_cooling_device
*cdev
= NULL
;
550 mutex_lock(&thermal_list_lock
);
551 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
552 /* skip non matching cdevs */
553 if (strncmp(cdev_type
, cdev
->type
, size
))
556 /* re binding the exception matching the type pattern */
557 thermal_zone_bind_cooling_device(tz
, THERMAL_TRIPS_NONE
, cdev
,
560 THERMAL_WEIGHT_DEFAULT
);
562 mutex_unlock(&thermal_list_lock
);
565 void thermal_zone_device_unbind_exception(struct thermal_zone_device
*tz
,
566 const char *cdev_type
, size_t size
)
568 struct thermal_cooling_device
*cdev
= NULL
;
570 mutex_lock(&thermal_list_lock
);
571 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
572 /* skip non matching cdevs */
573 if (strncmp(cdev_type
, cdev
->type
, size
))
575 /* unbinding the exception matching the type pattern */
576 thermal_zone_unbind_cooling_device(tz
, THERMAL_TRIPS_NONE
,
579 mutex_unlock(&thermal_list_lock
);
583 * Device management section: cooling devices, zones devices, and binding
585 * Set of functions provided by the thermal core for:
586 * - cooling devices lifecycle: registration, unregistration,
587 * binding, and unbinding.
588 * - thermal zone devices lifecycle: registration, unregistration,
589 * binding, and unbinding.
593 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
594 * @tz: pointer to struct thermal_zone_device
595 * @trip: indicates which trip point the cooling devices is
596 * associated with in this thermal zone.
597 * @cdev: pointer to struct thermal_cooling_device
598 * @upper: the Maximum cooling state for this trip point.
599 * THERMAL_NO_LIMIT means no upper limit,
600 * and the cooling device can be in max_state.
601 * @lower: the Minimum cooling state can be used for this trip point.
602 * THERMAL_NO_LIMIT means no lower limit,
603 * and the cooling device can be in cooling state 0.
604 * @weight: The weight of the cooling device to be bound to the
605 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
608 * This interface function bind a thermal cooling device to the certain trip
609 * point of a thermal zone device.
610 * This function is usually called in the thermal zone device .bind callback.
612 * Return: 0 on success, the proper error value otherwise.
614 int thermal_zone_bind_cooling_device(struct thermal_zone_device
*tz
,
616 struct thermal_cooling_device
*cdev
,
617 unsigned long upper
, unsigned long lower
,
620 struct thermal_instance
*dev
;
621 struct thermal_instance
*pos
;
622 struct thermal_zone_device
*pos1
;
623 struct thermal_cooling_device
*pos2
;
624 unsigned long max_state
;
627 if (trip
>= tz
->trips
|| (trip
< 0 && trip
!= THERMAL_TRIPS_NONE
))
630 list_for_each_entry(pos1
, &thermal_tz_list
, node
) {
634 list_for_each_entry(pos2
, &thermal_cdev_list
, node
) {
639 if (tz
!= pos1
|| cdev
!= pos2
)
642 ret
= cdev
->ops
->get_max_state(cdev
, &max_state
);
646 /* lower default 0, upper default max_state */
647 lower
= lower
== THERMAL_NO_LIMIT
? 0 : lower
;
648 upper
= upper
== THERMAL_NO_LIMIT
? max_state
: upper
;
650 if (lower
> upper
|| upper
> max_state
)
653 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
661 dev
->target
= THERMAL_NO_TARGET
;
662 dev
->weight
= weight
;
664 result
= ida_simple_get(&tz
->ida
, 0, 0, GFP_KERNEL
);
669 sprintf(dev
->name
, "cdev%d", dev
->id
);
671 sysfs_create_link(&tz
->device
.kobj
, &cdev
->device
.kobj
, dev
->name
);
675 sprintf(dev
->attr_name
, "cdev%d_trip_point", dev
->id
);
676 sysfs_attr_init(&dev
->attr
.attr
);
677 dev
->attr
.attr
.name
= dev
->attr_name
;
678 dev
->attr
.attr
.mode
= 0444;
679 dev
->attr
.show
= thermal_cooling_device_trip_point_show
;
680 result
= device_create_file(&tz
->device
, &dev
->attr
);
682 goto remove_symbol_link
;
684 sprintf(dev
->weight_attr_name
, "cdev%d_weight", dev
->id
);
685 sysfs_attr_init(&dev
->weight_attr
.attr
);
686 dev
->weight_attr
.attr
.name
= dev
->weight_attr_name
;
687 dev
->weight_attr
.attr
.mode
= S_IWUSR
| S_IRUGO
;
688 dev
->weight_attr
.show
= thermal_cooling_device_weight_show
;
689 dev
->weight_attr
.store
= thermal_cooling_device_weight_store
;
690 result
= device_create_file(&tz
->device
, &dev
->weight_attr
);
692 goto remove_trip_file
;
694 mutex_lock(&tz
->lock
);
695 mutex_lock(&cdev
->lock
);
696 list_for_each_entry(pos
, &tz
->thermal_instances
, tz_node
)
697 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
702 list_add_tail(&dev
->tz_node
, &tz
->thermal_instances
);
703 list_add_tail(&dev
->cdev_node
, &cdev
->thermal_instances
);
704 atomic_set(&tz
->need_update
, 1);
706 mutex_unlock(&cdev
->lock
);
707 mutex_unlock(&tz
->lock
);
712 device_remove_file(&tz
->device
, &dev
->weight_attr
);
714 device_remove_file(&tz
->device
, &dev
->attr
);
716 sysfs_remove_link(&tz
->device
.kobj
, dev
->name
);
718 ida_simple_remove(&tz
->ida
, dev
->id
);
723 EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device
);
726 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
728 * @tz: pointer to a struct thermal_zone_device.
729 * @trip: indicates which trip point the cooling devices is
730 * associated with in this thermal zone.
731 * @cdev: pointer to a struct thermal_cooling_device.
733 * This interface function unbind a thermal cooling device from the certain
734 * trip point of a thermal zone device.
735 * This function is usually called in the thermal zone device .unbind callback.
737 * Return: 0 on success, the proper error value otherwise.
739 int thermal_zone_unbind_cooling_device(struct thermal_zone_device
*tz
,
741 struct thermal_cooling_device
*cdev
)
743 struct thermal_instance
*pos
, *next
;
745 mutex_lock(&tz
->lock
);
746 mutex_lock(&cdev
->lock
);
747 list_for_each_entry_safe(pos
, next
, &tz
->thermal_instances
, tz_node
) {
748 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
749 list_del(&pos
->tz_node
);
750 list_del(&pos
->cdev_node
);
751 mutex_unlock(&cdev
->lock
);
752 mutex_unlock(&tz
->lock
);
756 mutex_unlock(&cdev
->lock
);
757 mutex_unlock(&tz
->lock
);
762 device_remove_file(&tz
->device
, &pos
->weight_attr
);
763 device_remove_file(&tz
->device
, &pos
->attr
);
764 sysfs_remove_link(&tz
->device
.kobj
, pos
->name
);
765 ida_simple_remove(&tz
->ida
, pos
->id
);
769 EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device
);
771 static void thermal_release(struct device
*dev
)
773 struct thermal_zone_device
*tz
;
774 struct thermal_cooling_device
*cdev
;
776 if (!strncmp(dev_name(dev
), "thermal_zone",
777 sizeof("thermal_zone") - 1)) {
778 tz
= to_thermal_zone(dev
);
779 kfree(tz
->trip_type_attrs
);
780 kfree(tz
->trip_temp_attrs
);
781 kfree(tz
->trip_hyst_attrs
);
782 kfree(tz
->trips_attribute_group
.attrs
);
783 kfree(tz
->device
.groups
);
785 } else if (!strncmp(dev_name(dev
), "cooling_device",
786 sizeof("cooling_device") - 1)) {
787 cdev
= to_cooling_device(dev
);
792 static struct class thermal_class
= {
794 .dev_release
= thermal_release
,
798 void print_bind_err_msg(struct thermal_zone_device
*tz
,
799 struct thermal_cooling_device
*cdev
, int ret
)
801 dev_err(&tz
->device
, "binding zone %s with cdev %s failed:%d\n",
802 tz
->type
, cdev
->type
, ret
);
805 static void __bind(struct thermal_zone_device
*tz
, int mask
,
806 struct thermal_cooling_device
*cdev
,
807 unsigned long *limits
,
812 for (i
= 0; i
< tz
->trips
; i
++) {
813 if (mask
& (1 << i
)) {
814 unsigned long upper
, lower
;
816 upper
= THERMAL_NO_LIMIT
;
817 lower
= THERMAL_NO_LIMIT
;
819 lower
= limits
[i
* 2];
820 upper
= limits
[i
* 2 + 1];
822 ret
= thermal_zone_bind_cooling_device(tz
, i
, cdev
,
826 print_bind_err_msg(tz
, cdev
, ret
);
831 static void bind_cdev(struct thermal_cooling_device
*cdev
)
834 const struct thermal_zone_params
*tzp
;
835 struct thermal_zone_device
*pos
= NULL
;
837 mutex_lock(&thermal_list_lock
);
839 list_for_each_entry(pos
, &thermal_tz_list
, node
) {
840 if (!pos
->tzp
&& !pos
->ops
->bind
)
843 if (pos
->ops
->bind
) {
844 ret
= pos
->ops
->bind(pos
, cdev
);
846 print_bind_err_msg(pos
, cdev
, ret
);
851 if (!tzp
|| !tzp
->tbp
)
854 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
855 if (tzp
->tbp
[i
].cdev
|| !tzp
->tbp
[i
].match
)
857 if (tzp
->tbp
[i
].match(pos
, cdev
))
859 tzp
->tbp
[i
].cdev
= cdev
;
860 __bind(pos
, tzp
->tbp
[i
].trip_mask
, cdev
,
861 tzp
->tbp
[i
].binding_limits
,
866 mutex_unlock(&thermal_list_lock
);
870 * __thermal_cooling_device_register() - register a new thermal cooling device
871 * @np: a pointer to a device tree node.
872 * @type: the thermal cooling device type.
873 * @devdata: device private data.
874 * @ops: standard thermal cooling devices callbacks.
876 * This interface function adds a new thermal cooling device (fan/processor/...)
877 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
878 * to all the thermal zone devices registered at the same time.
879 * It also gives the opportunity to link the cooling device to a device tree
880 * node, so that it can be bound to a thermal zone created out of device tree.
882 * Return: a pointer to the created struct thermal_cooling_device or an
883 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
885 static struct thermal_cooling_device
*
886 __thermal_cooling_device_register(struct device_node
*np
,
887 char *type
, void *devdata
,
888 const struct thermal_cooling_device_ops
*ops
)
890 struct thermal_cooling_device
*cdev
;
891 struct thermal_zone_device
*pos
= NULL
;
894 if (type
&& strlen(type
) >= THERMAL_NAME_LENGTH
)
895 return ERR_PTR(-EINVAL
);
897 if (!ops
|| !ops
->get_max_state
|| !ops
->get_cur_state
||
899 return ERR_PTR(-EINVAL
);
901 cdev
= kzalloc(sizeof(*cdev
), GFP_KERNEL
);
903 return ERR_PTR(-ENOMEM
);
905 result
= ida_simple_get(&thermal_cdev_ida
, 0, 0, GFP_KERNEL
);
908 return ERR_PTR(result
);
912 strlcpy(cdev
->type
, type
? : "", sizeof(cdev
->type
));
913 mutex_init(&cdev
->lock
);
914 INIT_LIST_HEAD(&cdev
->thermal_instances
);
917 cdev
->updated
= false;
918 cdev
->device
.class = &thermal_class
;
919 thermal_cooling_device_setup_sysfs(cdev
);
920 cdev
->devdata
= devdata
;
921 dev_set_name(&cdev
->device
, "cooling_device%d", cdev
->id
);
922 result
= device_register(&cdev
->device
);
924 ida_simple_remove(&thermal_cdev_ida
, cdev
->id
);
926 return ERR_PTR(result
);
929 /* Add 'this' new cdev to the global cdev list */
930 mutex_lock(&thermal_list_lock
);
931 list_add(&cdev
->node
, &thermal_cdev_list
);
932 mutex_unlock(&thermal_list_lock
);
934 /* Update binding information for 'this' new cdev */
937 mutex_lock(&thermal_list_lock
);
938 list_for_each_entry(pos
, &thermal_tz_list
, node
)
939 if (atomic_cmpxchg(&pos
->need_update
, 1, 0))
940 thermal_zone_device_update(pos
,
941 THERMAL_EVENT_UNSPECIFIED
);
942 mutex_unlock(&thermal_list_lock
);
948 * thermal_cooling_device_register() - register a new thermal cooling device
949 * @type: the thermal cooling device type.
950 * @devdata: device private data.
951 * @ops: standard thermal cooling devices callbacks.
953 * This interface function adds a new thermal cooling device (fan/processor/...)
954 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
955 * to all the thermal zone devices registered at the same time.
957 * Return: a pointer to the created struct thermal_cooling_device or an
958 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
960 struct thermal_cooling_device
*
961 thermal_cooling_device_register(char *type
, void *devdata
,
962 const struct thermal_cooling_device_ops
*ops
)
964 return __thermal_cooling_device_register(NULL
, type
, devdata
, ops
);
966 EXPORT_SYMBOL_GPL(thermal_cooling_device_register
);
969 * thermal_of_cooling_device_register() - register an OF thermal cooling device
970 * @np: a pointer to a device tree node.
971 * @type: the thermal cooling device type.
972 * @devdata: device private data.
973 * @ops: standard thermal cooling devices callbacks.
975 * This function will register a cooling device with device tree node reference.
976 * This interface function adds a new thermal cooling device (fan/processor/...)
977 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
978 * to all the thermal zone devices registered at the same time.
980 * Return: a pointer to the created struct thermal_cooling_device or an
981 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
983 struct thermal_cooling_device
*
984 thermal_of_cooling_device_register(struct device_node
*np
,
985 char *type
, void *devdata
,
986 const struct thermal_cooling_device_ops
*ops
)
988 return __thermal_cooling_device_register(np
, type
, devdata
, ops
);
990 EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register
);
992 static void __unbind(struct thermal_zone_device
*tz
, int mask
,
993 struct thermal_cooling_device
*cdev
)
997 for (i
= 0; i
< tz
->trips
; i
++)
999 thermal_zone_unbind_cooling_device(tz
, i
, cdev
);
1003 * thermal_cooling_device_unregister - removes a thermal cooling device
1004 * @cdev: the thermal cooling device to remove.
1006 * thermal_cooling_device_unregister() must be called when a registered
1007 * thermal cooling device is no longer needed.
1009 void thermal_cooling_device_unregister(struct thermal_cooling_device
*cdev
)
1012 const struct thermal_zone_params
*tzp
;
1013 struct thermal_zone_device
*tz
;
1014 struct thermal_cooling_device
*pos
= NULL
;
1019 mutex_lock(&thermal_list_lock
);
1020 list_for_each_entry(pos
, &thermal_cdev_list
, node
)
1024 /* thermal cooling device not found */
1025 mutex_unlock(&thermal_list_lock
);
1028 list_del(&cdev
->node
);
1030 /* Unbind all thermal zones associated with 'this' cdev */
1031 list_for_each_entry(tz
, &thermal_tz_list
, node
) {
1032 if (tz
->ops
->unbind
) {
1033 tz
->ops
->unbind(tz
, cdev
);
1037 if (!tz
->tzp
|| !tz
->tzp
->tbp
)
1041 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
1042 if (tzp
->tbp
[i
].cdev
== cdev
) {
1043 __unbind(tz
, tzp
->tbp
[i
].trip_mask
, cdev
);
1044 tzp
->tbp
[i
].cdev
= NULL
;
1049 mutex_unlock(&thermal_list_lock
);
1051 ida_simple_remove(&thermal_cdev_ida
, cdev
->id
);
1052 device_unregister(&cdev
->device
);
1054 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister
);
1056 static void bind_tz(struct thermal_zone_device
*tz
)
1059 struct thermal_cooling_device
*pos
= NULL
;
1060 const struct thermal_zone_params
*tzp
= tz
->tzp
;
1062 if (!tzp
&& !tz
->ops
->bind
)
1065 mutex_lock(&thermal_list_lock
);
1067 /* If there is ops->bind, try to use ops->bind */
1068 if (tz
->ops
->bind
) {
1069 list_for_each_entry(pos
, &thermal_cdev_list
, node
) {
1070 ret
= tz
->ops
->bind(tz
, pos
);
1072 print_bind_err_msg(tz
, pos
, ret
);
1077 if (!tzp
|| !tzp
->tbp
)
1080 list_for_each_entry(pos
, &thermal_cdev_list
, node
) {
1081 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
1082 if (tzp
->tbp
[i
].cdev
|| !tzp
->tbp
[i
].match
)
1084 if (tzp
->tbp
[i
].match(tz
, pos
))
1086 tzp
->tbp
[i
].cdev
= pos
;
1087 __bind(tz
, tzp
->tbp
[i
].trip_mask
, pos
,
1088 tzp
->tbp
[i
].binding_limits
,
1089 tzp
->tbp
[i
].weight
);
1093 mutex_unlock(&thermal_list_lock
);
1097 * thermal_zone_device_register() - register a new thermal zone device
1098 * @type: the thermal zone device type
1099 * @trips: the number of trip points the thermal zone support
1100 * @mask: a bit string indicating the writeablility of trip points
1101 * @devdata: private device data
1102 * @ops: standard thermal zone device callbacks
1103 * @tzp: thermal zone platform parameters
1104 * @passive_delay: number of milliseconds to wait between polls when
1105 * performing passive cooling
1106 * @polling_delay: number of milliseconds to wait between polls when checking
1107 * whether trip points have been crossed (0 for interrupt
1110 * This interface function adds a new thermal zone device (sensor) to
1111 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1112 * thermal cooling devices registered at the same time.
1113 * thermal_zone_device_unregister() must be called when the device is no
1114 * longer needed. The passive cooling depends on the .get_trend() return value.
1116 * Return: a pointer to the created struct thermal_zone_device or an
1117 * in case of error, an ERR_PTR. Caller must check return value with
1118 * IS_ERR*() helpers.
1120 struct thermal_zone_device
*
1121 thermal_zone_device_register(const char *type
, int trips
, int mask
,
1122 void *devdata
, struct thermal_zone_device_ops
*ops
,
1123 struct thermal_zone_params
*tzp
, int passive_delay
,
1126 struct thermal_zone_device
*tz
;
1127 enum thermal_trip_type trip_type
;
1131 struct thermal_governor
*governor
;
1133 if (!type
|| strlen(type
) == 0)
1134 return ERR_PTR(-EINVAL
);
1136 if (type
&& strlen(type
) >= THERMAL_NAME_LENGTH
)
1137 return ERR_PTR(-EINVAL
);
1139 if (trips
> THERMAL_MAX_TRIPS
|| trips
< 0 || mask
>> trips
)
1140 return ERR_PTR(-EINVAL
);
1143 return ERR_PTR(-EINVAL
);
1145 if (trips
> 0 && (!ops
->get_trip_type
|| !ops
->get_trip_temp
))
1146 return ERR_PTR(-EINVAL
);
1148 tz
= kzalloc(sizeof(*tz
), GFP_KERNEL
);
1150 return ERR_PTR(-ENOMEM
);
1152 INIT_LIST_HEAD(&tz
->thermal_instances
);
1154 mutex_init(&tz
->lock
);
1155 result
= ida_simple_get(&thermal_tz_ida
, 0, 0, GFP_KERNEL
);
1158 return ERR_PTR(result
);
1162 strlcpy(tz
->type
, type
, sizeof(tz
->type
));
1165 tz
->device
.class = &thermal_class
;
1166 tz
->devdata
= devdata
;
1168 tz
->passive_delay
= passive_delay
;
1169 tz
->polling_delay
= polling_delay
;
1172 /* Add nodes that are always present via .groups */
1173 result
= thermal_zone_create_device_groups(tz
, mask
);
1177 /* A new thermal zone needs to be updated anyway. */
1178 atomic_set(&tz
->need_update
, 1);
1180 dev_set_name(&tz
->device
, "thermal_zone%d", tz
->id
);
1181 result
= device_register(&tz
->device
);
1183 ida_simple_remove(&thermal_tz_ida
, tz
->id
);
1185 return ERR_PTR(result
);
1188 for (count
= 0; count
< trips
; count
++) {
1189 if (tz
->ops
->get_trip_type(tz
, count
, &trip_type
))
1190 set_bit(count
, &tz
->trips_disabled
);
1191 if (tz
->ops
->get_trip_temp(tz
, count
, &trip_temp
))
1192 set_bit(count
, &tz
->trips_disabled
);
1193 /* Check for bogus trip points */
1195 set_bit(count
, &tz
->trips_disabled
);
1198 /* Update 'this' zone's governor information */
1199 mutex_lock(&thermal_governor_lock
);
1202 governor
= __find_governor(tz
->tzp
->governor_name
);
1204 governor
= def_governor
;
1206 result
= thermal_set_governor(tz
, governor
);
1208 mutex_unlock(&thermal_governor_lock
);
1212 mutex_unlock(&thermal_governor_lock
);
1214 if (!tz
->tzp
|| !tz
->tzp
->no_hwmon
) {
1215 result
= thermal_add_hwmon_sysfs(tz
);
1220 mutex_lock(&thermal_list_lock
);
1221 list_add_tail(&tz
->node
, &thermal_tz_list
);
1222 mutex_unlock(&thermal_list_lock
);
1224 /* Bind cooling devices for this zone */
1227 INIT_DELAYED_WORK(&tz
->poll_queue
, thermal_zone_device_check
);
1229 thermal_zone_device_reset(tz
);
1230 /* Update the new thermal zone and mark it as already updated. */
1231 if (atomic_cmpxchg(&tz
->need_update
, 1, 0))
1232 thermal_zone_device_update(tz
, THERMAL_EVENT_UNSPECIFIED
);
1237 ida_simple_remove(&thermal_tz_ida
, tz
->id
);
1238 device_unregister(&tz
->device
);
1239 return ERR_PTR(result
);
1241 EXPORT_SYMBOL_GPL(thermal_zone_device_register
);
1244 * thermal_device_unregister - removes the registered thermal zone device
1245 * @tz: the thermal zone device to remove
1247 void thermal_zone_device_unregister(struct thermal_zone_device
*tz
)
1250 const struct thermal_zone_params
*tzp
;
1251 struct thermal_cooling_device
*cdev
;
1252 struct thermal_zone_device
*pos
= NULL
;
1259 mutex_lock(&thermal_list_lock
);
1260 list_for_each_entry(pos
, &thermal_tz_list
, node
)
1264 /* thermal zone device not found */
1265 mutex_unlock(&thermal_list_lock
);
1268 list_del(&tz
->node
);
1270 /* Unbind all cdevs associated with 'this' thermal zone */
1271 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
1272 if (tz
->ops
->unbind
) {
1273 tz
->ops
->unbind(tz
, cdev
);
1277 if (!tzp
|| !tzp
->tbp
)
1280 for (i
= 0; i
< tzp
->num_tbps
; i
++) {
1281 if (tzp
->tbp
[i
].cdev
== cdev
) {
1282 __unbind(tz
, tzp
->tbp
[i
].trip_mask
, cdev
);
1283 tzp
->tbp
[i
].cdev
= NULL
;
1288 mutex_unlock(&thermal_list_lock
);
1290 thermal_zone_device_set_polling(tz
, 0);
1292 thermal_set_governor(tz
, NULL
);
1294 thermal_remove_hwmon_sysfs(tz
);
1295 ida_simple_remove(&thermal_tz_ida
, tz
->id
);
1296 ida_destroy(&tz
->ida
);
1297 mutex_destroy(&tz
->lock
);
1298 device_unregister(&tz
->device
);
1300 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister
);
1303 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1304 * @name: thermal zone name to fetch the temperature
1306 * When only one zone is found with the passed name, returns a reference to it.
1308 * Return: On success returns a reference to an unique thermal zone with
1309 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1310 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1312 struct thermal_zone_device
*thermal_zone_get_zone_by_name(const char *name
)
1314 struct thermal_zone_device
*pos
= NULL
, *ref
= ERR_PTR(-EINVAL
);
1315 unsigned int found
= 0;
1320 mutex_lock(&thermal_list_lock
);
1321 list_for_each_entry(pos
, &thermal_tz_list
, node
)
1322 if (!strncasecmp(name
, pos
->type
, THERMAL_NAME_LENGTH
)) {
1326 mutex_unlock(&thermal_list_lock
);
1328 /* nothing has been found, thus an error code for it */
1330 ref
= ERR_PTR(-ENODEV
);
1332 /* Success only when an unique zone is found */
1333 ref
= ERR_PTR(-EEXIST
);
1338 EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name
);
1341 static const struct genl_multicast_group thermal_event_mcgrps
[] = {
1342 { .name
= THERMAL_GENL_MCAST_GROUP_NAME
, },
1345 static struct genl_family thermal_event_genl_family __ro_after_init
= {
1346 .module
= THIS_MODULE
,
1347 .name
= THERMAL_GENL_FAMILY_NAME
,
1348 .version
= THERMAL_GENL_VERSION
,
1349 .maxattr
= THERMAL_GENL_ATTR_MAX
,
1350 .mcgrps
= thermal_event_mcgrps
,
1351 .n_mcgrps
= ARRAY_SIZE(thermal_event_mcgrps
),
1354 int thermal_generate_netlink_event(struct thermal_zone_device
*tz
,
1357 struct sk_buff
*skb
;
1358 struct nlattr
*attr
;
1359 struct thermal_genl_event
*thermal_event
;
1363 static unsigned int thermal_event_seqnum
;
1368 /* allocate memory */
1369 size
= nla_total_size(sizeof(struct thermal_genl_event
)) +
1372 skb
= genlmsg_new(size
, GFP_ATOMIC
);
1376 /* add the genetlink message header */
1377 msg_header
= genlmsg_put(skb
, 0, thermal_event_seqnum
++,
1378 &thermal_event_genl_family
, 0,
1379 THERMAL_GENL_CMD_EVENT
);
1386 attr
= nla_reserve(skb
, THERMAL_GENL_ATTR_EVENT
,
1387 sizeof(struct thermal_genl_event
));
1394 thermal_event
= nla_data(attr
);
1395 if (!thermal_event
) {
1400 memset(thermal_event
, 0, sizeof(struct thermal_genl_event
));
1402 thermal_event
->orig
= tz
->id
;
1403 thermal_event
->event
= event
;
1405 /* send multicast genetlink message */
1406 genlmsg_end(skb
, msg_header
);
1408 result
= genlmsg_multicast(&thermal_event_genl_family
, skb
, 0,
1411 dev_err(&tz
->device
, "Failed to send netlink event:%d", result
);
1415 EXPORT_SYMBOL_GPL(thermal_generate_netlink_event
);
1417 static int __init
genetlink_init(void)
1419 return genl_register_family(&thermal_event_genl_family
);
1422 static void genetlink_exit(void)
1424 genl_unregister_family(&thermal_event_genl_family
);
1426 #else /* !CONFIG_NET */
1427 static inline int genetlink_init(void) { return 0; }
1428 static inline void genetlink_exit(void) {}
1429 #endif /* !CONFIG_NET */
1431 static int thermal_pm_notify(struct notifier_block
*nb
,
1432 unsigned long mode
, void *_unused
)
1434 struct thermal_zone_device
*tz
;
1437 case PM_HIBERNATION_PREPARE
:
1438 case PM_RESTORE_PREPARE
:
1439 case PM_SUSPEND_PREPARE
:
1440 atomic_set(&in_suspend
, 1);
1442 case PM_POST_HIBERNATION
:
1443 case PM_POST_RESTORE
:
1444 case PM_POST_SUSPEND
:
1445 atomic_set(&in_suspend
, 0);
1446 list_for_each_entry(tz
, &thermal_tz_list
, node
) {
1447 thermal_zone_device_reset(tz
);
1448 thermal_zone_device_update(tz
,
1449 THERMAL_EVENT_UNSPECIFIED
);
1458 static struct notifier_block thermal_pm_nb
= {
1459 .notifier_call
= thermal_pm_notify
,
1462 static int __init
thermal_init(void)
1466 result
= thermal_register_governors();
1470 result
= class_register(&thermal_class
);
1472 goto unregister_governors
;
1474 result
= genetlink_init();
1476 goto unregister_class
;
1478 result
= of_parse_thermal_zones();
1482 result
= register_pm_notifier(&thermal_pm_nb
);
1484 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1492 class_unregister(&thermal_class
);
1493 unregister_governors
:
1494 thermal_unregister_governors();
1496 ida_destroy(&thermal_tz_ida
);
1497 ida_destroy(&thermal_cdev_ida
);
1498 mutex_destroy(&thermal_list_lock
);
1499 mutex_destroy(&thermal_governor_lock
);
1503 static void __exit
thermal_exit(void)
1505 unregister_pm_notifier(&thermal_pm_nb
);
1506 of_thermal_destroy_zones();
1508 class_unregister(&thermal_class
);
1509 thermal_unregister_governors();
1510 ida_destroy(&thermal_tz_ida
);
1511 ida_destroy(&thermal_cdev_ida
);
1512 mutex_destroy(&thermal_list_lock
);
1513 mutex_destroy(&thermal_governor_lock
);
1516 fs_initcall(thermal_init
);
1517 module_exit(thermal_exit
);