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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 #include <linux/module.h>
29 #include <linux/device.h>
30 #include <linux/err.h>
31 #include <linux/slab.h>
32 #include <linux/kdev_t.h>
33 #include <linux/idr.h>
34 #include <linux/thermal.h>
35 #include <linux/spinlock.h>
36 #include <linux/reboot.h>
37 #include <net/netlink.h>
38 #include <net/genetlink.h>
40 MODULE_AUTHOR("Zhang Rui");
41 MODULE_DESCRIPTION("Generic thermal management sysfs support");
42 MODULE_LICENSE("GPL");
44 struct thermal_cooling_device_instance
{
46 char name
[THERMAL_NAME_LENGTH
];
47 struct thermal_zone_device
*tz
;
48 struct thermal_cooling_device
*cdev
;
50 char attr_name
[THERMAL_NAME_LENGTH
];
51 struct device_attribute attr
;
52 struct list_head node
;
55 static DEFINE_IDR(thermal_tz_idr
);
56 static DEFINE_IDR(thermal_cdev_idr
);
57 static DEFINE_MUTEX(thermal_idr_lock
);
59 static LIST_HEAD(thermal_tz_list
);
60 static LIST_HEAD(thermal_cdev_list
);
61 static DEFINE_MUTEX(thermal_list_lock
);
63 static int get_idr(struct idr
*idr
, struct mutex
*lock
, int *id
)
68 if (unlikely(idr_pre_get(idr
, GFP_KERNEL
) == 0))
73 err
= idr_get_new(idr
, NULL
, id
);
76 if (unlikely(err
== -EAGAIN
))
78 else if (unlikely(err
))
81 *id
= *id
& MAX_ID_MASK
;
85 static void release_idr(struct idr
*idr
, struct mutex
*lock
, int id
)
94 /* sys I/F for thermal zone */
96 #define to_thermal_zone(_dev) \
97 container_of(_dev, struct thermal_zone_device, device)
100 type_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
102 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
104 return sprintf(buf
, "%s\n", tz
->type
);
108 temp_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
110 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
114 if (!tz
->ops
->get_temp
)
117 ret
= tz
->ops
->get_temp(tz
, &temperature
);
122 return sprintf(buf
, "%ld\n", temperature
);
126 mode_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
128 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
129 enum thermal_device_mode mode
;
132 if (!tz
->ops
->get_mode
)
135 result
= tz
->ops
->get_mode(tz
, &mode
);
139 return sprintf(buf
, "%s\n", mode
== THERMAL_DEVICE_ENABLED
? "enabled"
144 mode_store(struct device
*dev
, struct device_attribute
*attr
,
145 const char *buf
, size_t count
)
147 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
150 if (!tz
->ops
->set_mode
)
153 if (!strncmp(buf
, "enabled", sizeof("enabled") - 1))
154 result
= tz
->ops
->set_mode(tz
, THERMAL_DEVICE_ENABLED
);
155 else if (!strncmp(buf
, "disabled", sizeof("disabled") - 1))
156 result
= tz
->ops
->set_mode(tz
, THERMAL_DEVICE_DISABLED
);
167 trip_point_type_show(struct device
*dev
, struct device_attribute
*attr
,
170 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
171 enum thermal_trip_type type
;
174 if (!tz
->ops
->get_trip_type
)
177 if (!sscanf(attr
->attr
.name
, "trip_point_%d_type", &trip
))
180 result
= tz
->ops
->get_trip_type(tz
, trip
, &type
);
185 case THERMAL_TRIP_CRITICAL
:
186 return sprintf(buf
, "critical\n");
187 case THERMAL_TRIP_HOT
:
188 return sprintf(buf
, "hot\n");
189 case THERMAL_TRIP_PASSIVE
:
190 return sprintf(buf
, "passive\n");
191 case THERMAL_TRIP_ACTIVE
:
192 return sprintf(buf
, "active\n");
194 return sprintf(buf
, "unknown\n");
199 trip_point_temp_store(struct device
*dev
, struct device_attribute
*attr
,
200 const char *buf
, size_t count
)
202 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
204 unsigned long temperature
;
206 if (!tz
->ops
->set_trip_temp
)
209 if (!sscanf(attr
->attr
.name
, "trip_point_%d_temp", &trip
))
212 if (kstrtoul(buf
, 10, &temperature
))
215 ret
= tz
->ops
->set_trip_temp(tz
, trip
, temperature
);
217 return ret
? ret
: count
;
221 trip_point_temp_show(struct device
*dev
, struct device_attribute
*attr
,
224 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
228 if (!tz
->ops
->get_trip_temp
)
231 if (!sscanf(attr
->attr
.name
, "trip_point_%d_temp", &trip
))
234 ret
= tz
->ops
->get_trip_temp(tz
, trip
, &temperature
);
239 return sprintf(buf
, "%ld\n", temperature
);
243 trip_point_hyst_store(struct device
*dev
, struct device_attribute
*attr
,
244 const char *buf
, size_t count
)
246 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
248 unsigned long temperature
;
250 if (!tz
->ops
->set_trip_hyst
)
253 if (!sscanf(attr
->attr
.name
, "trip_point_%d_hyst", &trip
))
256 if (kstrtoul(buf
, 10, &temperature
))
260 * We are not doing any check on the 'temperature' value
261 * here. The driver implementing 'set_trip_hyst' has to
264 ret
= tz
->ops
->set_trip_hyst(tz
, trip
, temperature
);
266 return ret
? ret
: count
;
270 trip_point_hyst_show(struct device
*dev
, struct device_attribute
*attr
,
273 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
275 unsigned long temperature
;
277 if (!tz
->ops
->get_trip_hyst
)
280 if (!sscanf(attr
->attr
.name
, "trip_point_%d_hyst", &trip
))
283 ret
= tz
->ops
->get_trip_hyst(tz
, trip
, &temperature
);
285 return ret
? ret
: sprintf(buf
, "%ld\n", temperature
);
289 passive_store(struct device
*dev
, struct device_attribute
*attr
,
290 const char *buf
, size_t count
)
292 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
293 struct thermal_cooling_device
*cdev
= NULL
;
296 if (!sscanf(buf
, "%d\n", &state
))
299 /* sanity check: values below 1000 millicelcius don't make sense
300 * and can cause the system to go into a thermal heart attack
302 if (state
&& state
< 1000)
305 if (state
&& !tz
->forced_passive
) {
306 mutex_lock(&thermal_list_lock
);
307 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
308 if (!strncmp("Processor", cdev
->type
,
309 sizeof("Processor")))
310 thermal_zone_bind_cooling_device(tz
,
314 mutex_unlock(&thermal_list_lock
);
315 if (!tz
->passive_delay
)
316 tz
->passive_delay
= 1000;
317 } else if (!state
&& tz
->forced_passive
) {
318 mutex_lock(&thermal_list_lock
);
319 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
320 if (!strncmp("Processor", cdev
->type
,
321 sizeof("Processor")))
322 thermal_zone_unbind_cooling_device(tz
,
326 mutex_unlock(&thermal_list_lock
);
327 tz
->passive_delay
= 0;
333 tz
->forced_passive
= state
;
335 thermal_zone_device_update(tz
);
341 passive_show(struct device
*dev
, struct device_attribute
*attr
,
344 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
346 return sprintf(buf
, "%d\n", tz
->forced_passive
);
349 static DEVICE_ATTR(type
, 0444, type_show
, NULL
);
350 static DEVICE_ATTR(temp
, 0444, temp_show
, NULL
);
351 static DEVICE_ATTR(mode
, 0644, mode_show
, mode_store
);
352 static DEVICE_ATTR(passive
, S_IRUGO
| S_IWUSR
, passive_show
, passive_store
);
354 /* sys I/F for cooling device */
355 #define to_cooling_device(_dev) \
356 container_of(_dev, struct thermal_cooling_device, device)
359 thermal_cooling_device_type_show(struct device
*dev
,
360 struct device_attribute
*attr
, char *buf
)
362 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
364 return sprintf(buf
, "%s\n", cdev
->type
);
368 thermal_cooling_device_max_state_show(struct device
*dev
,
369 struct device_attribute
*attr
, char *buf
)
371 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
375 ret
= cdev
->ops
->get_max_state(cdev
, &state
);
378 return sprintf(buf
, "%ld\n", state
);
382 thermal_cooling_device_cur_state_show(struct device
*dev
,
383 struct device_attribute
*attr
, char *buf
)
385 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
389 ret
= cdev
->ops
->get_cur_state(cdev
, &state
);
392 return sprintf(buf
, "%ld\n", state
);
396 thermal_cooling_device_cur_state_store(struct device
*dev
,
397 struct device_attribute
*attr
,
398 const char *buf
, size_t count
)
400 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
404 if (!sscanf(buf
, "%ld\n", &state
))
410 result
= cdev
->ops
->set_cur_state(cdev
, state
);
416 static struct device_attribute dev_attr_cdev_type
=
417 __ATTR(type
, 0444, thermal_cooling_device_type_show
, NULL
);
418 static DEVICE_ATTR(max_state
, 0444,
419 thermal_cooling_device_max_state_show
, NULL
);
420 static DEVICE_ATTR(cur_state
, 0644,
421 thermal_cooling_device_cur_state_show
,
422 thermal_cooling_device_cur_state_store
);
425 thermal_cooling_device_trip_point_show(struct device
*dev
,
426 struct device_attribute
*attr
, char *buf
)
428 struct thermal_cooling_device_instance
*instance
;
431 container_of(attr
, struct thermal_cooling_device_instance
, attr
);
433 if (instance
->trip
== THERMAL_TRIPS_NONE
)
434 return sprintf(buf
, "-1\n");
436 return sprintf(buf
, "%d\n", instance
->trip
);
439 /* Device management */
441 #if defined(CONFIG_THERMAL_HWMON)
444 #include <linux/hwmon.h>
446 /* thermal zone devices with the same type share one hwmon device */
447 struct thermal_hwmon_device
{
448 char type
[THERMAL_NAME_LENGTH
];
449 struct device
*device
;
451 struct list_head tz_list
;
452 struct list_head node
;
455 struct thermal_hwmon_attr
{
456 struct device_attribute attr
;
460 /* one temperature input for each thermal zone */
461 struct thermal_hwmon_temp
{
462 struct list_head hwmon_node
;
463 struct thermal_zone_device
*tz
;
464 struct thermal_hwmon_attr temp_input
; /* hwmon sys attr */
465 struct thermal_hwmon_attr temp_crit
; /* hwmon sys attr */
468 static LIST_HEAD(thermal_hwmon_list
);
471 name_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
473 struct thermal_hwmon_device
*hwmon
= dev_get_drvdata(dev
);
474 return sprintf(buf
, "%s\n", hwmon
->type
);
476 static DEVICE_ATTR(name
, 0444, name_show
, NULL
);
479 temp_input_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
483 struct thermal_hwmon_attr
*hwmon_attr
484 = container_of(attr
, struct thermal_hwmon_attr
, attr
);
485 struct thermal_hwmon_temp
*temp
486 = container_of(hwmon_attr
, struct thermal_hwmon_temp
,
488 struct thermal_zone_device
*tz
= temp
->tz
;
490 ret
= tz
->ops
->get_temp(tz
, &temperature
);
495 return sprintf(buf
, "%ld\n", temperature
);
499 temp_crit_show(struct device
*dev
, struct device_attribute
*attr
,
502 struct thermal_hwmon_attr
*hwmon_attr
503 = container_of(attr
, struct thermal_hwmon_attr
, attr
);
504 struct thermal_hwmon_temp
*temp
505 = container_of(hwmon_attr
, struct thermal_hwmon_temp
,
507 struct thermal_zone_device
*tz
= temp
->tz
;
511 ret
= tz
->ops
->get_trip_temp(tz
, 0, &temperature
);
515 return sprintf(buf
, "%ld\n", temperature
);
519 static struct thermal_hwmon_device
*
520 thermal_hwmon_lookup_by_type(const struct thermal_zone_device
*tz
)
522 struct thermal_hwmon_device
*hwmon
;
524 mutex_lock(&thermal_list_lock
);
525 list_for_each_entry(hwmon
, &thermal_hwmon_list
, node
)
526 if (!strcmp(hwmon
->type
, tz
->type
)) {
527 mutex_unlock(&thermal_list_lock
);
530 mutex_unlock(&thermal_list_lock
);
535 /* Find the temperature input matching a given thermal zone */
536 static struct thermal_hwmon_temp
*
537 thermal_hwmon_lookup_temp(const struct thermal_hwmon_device
*hwmon
,
538 const struct thermal_zone_device
*tz
)
540 struct thermal_hwmon_temp
*temp
;
542 mutex_lock(&thermal_list_lock
);
543 list_for_each_entry(temp
, &hwmon
->tz_list
, hwmon_node
)
544 if (temp
->tz
== tz
) {
545 mutex_unlock(&thermal_list_lock
);
548 mutex_unlock(&thermal_list_lock
);
554 thermal_add_hwmon_sysfs(struct thermal_zone_device
*tz
)
556 struct thermal_hwmon_device
*hwmon
;
557 struct thermal_hwmon_temp
*temp
;
558 int new_hwmon_device
= 1;
561 hwmon
= thermal_hwmon_lookup_by_type(tz
);
563 new_hwmon_device
= 0;
564 goto register_sys_interface
;
567 hwmon
= kzalloc(sizeof(struct thermal_hwmon_device
), GFP_KERNEL
);
571 INIT_LIST_HEAD(&hwmon
->tz_list
);
572 strlcpy(hwmon
->type
, tz
->type
, THERMAL_NAME_LENGTH
);
573 hwmon
->device
= hwmon_device_register(NULL
);
574 if (IS_ERR(hwmon
->device
)) {
575 result
= PTR_ERR(hwmon
->device
);
578 dev_set_drvdata(hwmon
->device
, hwmon
);
579 result
= device_create_file(hwmon
->device
, &dev_attr_name
);
583 register_sys_interface
:
584 temp
= kzalloc(sizeof(struct thermal_hwmon_temp
), GFP_KERNEL
);
587 goto unregister_name
;
593 snprintf(temp
->temp_input
.name
, THERMAL_NAME_LENGTH
,
594 "temp%d_input", hwmon
->count
);
595 temp
->temp_input
.attr
.attr
.name
= temp
->temp_input
.name
;
596 temp
->temp_input
.attr
.attr
.mode
= 0444;
597 temp
->temp_input
.attr
.show
= temp_input_show
;
598 sysfs_attr_init(&temp
->temp_input
.attr
.attr
);
599 result
= device_create_file(hwmon
->device
, &temp
->temp_input
.attr
);
603 if (tz
->ops
->get_crit_temp
) {
604 unsigned long temperature
;
605 if (!tz
->ops
->get_crit_temp(tz
, &temperature
)) {
606 snprintf(temp
->temp_crit
.name
, THERMAL_NAME_LENGTH
,
607 "temp%d_crit", hwmon
->count
);
608 temp
->temp_crit
.attr
.attr
.name
= temp
->temp_crit
.name
;
609 temp
->temp_crit
.attr
.attr
.mode
= 0444;
610 temp
->temp_crit
.attr
.show
= temp_crit_show
;
611 sysfs_attr_init(&temp
->temp_crit
.attr
.attr
);
612 result
= device_create_file(hwmon
->device
,
613 &temp
->temp_crit
.attr
);
615 goto unregister_input
;
619 mutex_lock(&thermal_list_lock
);
620 if (new_hwmon_device
)
621 list_add_tail(&hwmon
->node
, &thermal_hwmon_list
);
622 list_add_tail(&temp
->hwmon_node
, &hwmon
->tz_list
);
623 mutex_unlock(&thermal_list_lock
);
628 device_remove_file(hwmon
->device
, &temp
->temp_input
.attr
);
632 if (new_hwmon_device
) {
633 device_remove_file(hwmon
->device
, &dev_attr_name
);
634 hwmon_device_unregister(hwmon
->device
);
637 if (new_hwmon_device
)
644 thermal_remove_hwmon_sysfs(struct thermal_zone_device
*tz
)
646 struct thermal_hwmon_device
*hwmon
;
647 struct thermal_hwmon_temp
*temp
;
649 hwmon
= thermal_hwmon_lookup_by_type(tz
);
650 if (unlikely(!hwmon
)) {
651 /* Should never happen... */
652 dev_dbg(&tz
->device
, "hwmon device lookup failed!\n");
656 temp
= thermal_hwmon_lookup_temp(hwmon
, tz
);
657 if (unlikely(!temp
)) {
658 /* Should never happen... */
659 dev_dbg(&tz
->device
, "temperature input lookup failed!\n");
663 device_remove_file(hwmon
->device
, &temp
->temp_input
.attr
);
664 if (tz
->ops
->get_crit_temp
)
665 device_remove_file(hwmon
->device
, &temp
->temp_crit
.attr
);
667 mutex_lock(&thermal_list_lock
);
668 list_del(&temp
->hwmon_node
);
670 if (!list_empty(&hwmon
->tz_list
)) {
671 mutex_unlock(&thermal_list_lock
);
674 list_del(&hwmon
->node
);
675 mutex_unlock(&thermal_list_lock
);
677 device_remove_file(hwmon
->device
, &dev_attr_name
);
678 hwmon_device_unregister(hwmon
->device
);
683 thermal_add_hwmon_sysfs(struct thermal_zone_device
*tz
)
689 thermal_remove_hwmon_sysfs(struct thermal_zone_device
*tz
)
694 static void thermal_zone_device_set_polling(struct thermal_zone_device
*tz
,
697 cancel_delayed_work(&(tz
->poll_queue
));
703 queue_delayed_work(system_freezable_wq
, &(tz
->poll_queue
),
704 round_jiffies(msecs_to_jiffies(delay
)));
706 queue_delayed_work(system_freezable_wq
, &(tz
->poll_queue
),
707 msecs_to_jiffies(delay
));
710 static void thermal_zone_device_passive(struct thermal_zone_device
*tz
,
711 int temp
, int trip_temp
, int trip
)
714 struct thermal_cooling_device_instance
*instance
;
715 struct thermal_cooling_device
*cdev
;
716 long state
, max_state
;
721 * Calculate the thermal trend (using the passive cooling equation)
722 * and modify the performance limit for all passive cooling devices
723 * accordingly. Note that we assume symmetry.
725 if (temp
>= trip_temp
) {
728 trend
= (tz
->tc1
* (temp
- tz
->last_temperature
)) +
729 (tz
->tc2
* (temp
- trip_temp
));
733 list_for_each_entry(instance
, &tz
->cooling_devices
,
735 if (instance
->trip
!= trip
)
737 cdev
= instance
->cdev
;
738 cdev
->ops
->get_cur_state(cdev
, &state
);
739 cdev
->ops
->get_max_state(cdev
, &max_state
);
740 if (state
++ < max_state
)
741 cdev
->ops
->set_cur_state(cdev
, state
);
743 } else if (trend
< 0) { /* Cooling off? */
744 list_for_each_entry(instance
, &tz
->cooling_devices
,
746 if (instance
->trip
!= trip
)
748 cdev
= instance
->cdev
;
749 cdev
->ops
->get_cur_state(cdev
, &state
);
750 cdev
->ops
->get_max_state(cdev
, &max_state
);
752 cdev
->ops
->set_cur_state(cdev
, --state
);
761 * Implement passive cooling hysteresis to slowly increase performance
762 * and avoid thrashing around the passive trip point. Note that we
765 list_for_each_entry(instance
, &tz
->cooling_devices
, node
) {
766 if (instance
->trip
!= trip
)
768 cdev
= instance
->cdev
;
769 cdev
->ops
->get_cur_state(cdev
, &state
);
770 cdev
->ops
->get_max_state(cdev
, &max_state
);
772 cdev
->ops
->set_cur_state(cdev
, --state
);
778 static void thermal_zone_device_check(struct work_struct
*work
)
780 struct thermal_zone_device
*tz
= container_of(work
, struct
783 thermal_zone_device_update(tz
);
787 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
788 * @tz: thermal zone device
789 * @trip: indicates which trip point the cooling devices is
790 * associated with in this thermal zone.
791 * @cdev: thermal cooling device
793 * This function is usually called in the thermal zone device .bind callback.
795 int thermal_zone_bind_cooling_device(struct thermal_zone_device
*tz
,
797 struct thermal_cooling_device
*cdev
)
799 struct thermal_cooling_device_instance
*dev
;
800 struct thermal_cooling_device_instance
*pos
;
801 struct thermal_zone_device
*pos1
;
802 struct thermal_cooling_device
*pos2
;
805 if (trip
>= tz
->trips
|| (trip
< 0 && trip
!= THERMAL_TRIPS_NONE
))
808 list_for_each_entry(pos1
, &thermal_tz_list
, node
) {
812 list_for_each_entry(pos2
, &thermal_cdev_list
, node
) {
817 if (tz
!= pos1
|| cdev
!= pos2
)
821 kzalloc(sizeof(struct thermal_cooling_device_instance
), GFP_KERNEL
);
827 result
= get_idr(&tz
->idr
, &tz
->lock
, &dev
->id
);
831 sprintf(dev
->name
, "cdev%d", dev
->id
);
833 sysfs_create_link(&tz
->device
.kobj
, &cdev
->device
.kobj
, dev
->name
);
837 sprintf(dev
->attr_name
, "cdev%d_trip_point", dev
->id
);
838 sysfs_attr_init(&dev
->attr
.attr
);
839 dev
->attr
.attr
.name
= dev
->attr_name
;
840 dev
->attr
.attr
.mode
= 0444;
841 dev
->attr
.show
= thermal_cooling_device_trip_point_show
;
842 result
= device_create_file(&tz
->device
, &dev
->attr
);
844 goto remove_symbol_link
;
846 mutex_lock(&tz
->lock
);
847 list_for_each_entry(pos
, &tz
->cooling_devices
, node
)
848 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
853 list_add_tail(&dev
->node
, &tz
->cooling_devices
);
854 mutex_unlock(&tz
->lock
);
859 device_remove_file(&tz
->device
, &dev
->attr
);
861 sysfs_remove_link(&tz
->device
.kobj
, dev
->name
);
863 release_idr(&tz
->idr
, &tz
->lock
, dev
->id
);
868 EXPORT_SYMBOL(thermal_zone_bind_cooling_device
);
871 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
872 * @tz: thermal zone device
873 * @trip: indicates which trip point the cooling devices is
874 * associated with in this thermal zone.
875 * @cdev: thermal cooling device
877 * This function is usually called in the thermal zone device .unbind callback.
879 int thermal_zone_unbind_cooling_device(struct thermal_zone_device
*tz
,
881 struct thermal_cooling_device
*cdev
)
883 struct thermal_cooling_device_instance
*pos
, *next
;
885 mutex_lock(&tz
->lock
);
886 list_for_each_entry_safe(pos
, next
, &tz
->cooling_devices
, node
) {
887 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
888 list_del(&pos
->node
);
889 mutex_unlock(&tz
->lock
);
893 mutex_unlock(&tz
->lock
);
898 device_remove_file(&tz
->device
, &pos
->attr
);
899 sysfs_remove_link(&tz
->device
.kobj
, pos
->name
);
900 release_idr(&tz
->idr
, &tz
->lock
, pos
->id
);
904 EXPORT_SYMBOL(thermal_zone_unbind_cooling_device
);
906 static void thermal_release(struct device
*dev
)
908 struct thermal_zone_device
*tz
;
909 struct thermal_cooling_device
*cdev
;
911 if (!strncmp(dev_name(dev
), "thermal_zone",
912 sizeof("thermal_zone") - 1)) {
913 tz
= to_thermal_zone(dev
);
916 cdev
= to_cooling_device(dev
);
921 static struct class thermal_class
= {
923 .dev_release
= thermal_release
,
927 * thermal_cooling_device_register - register a new thermal cooling device
928 * @type: the thermal cooling device type.
929 * @devdata: device private data.
930 * @ops: standard thermal cooling devices callbacks.
932 struct thermal_cooling_device
*
933 thermal_cooling_device_register(char *type
, void *devdata
,
934 const struct thermal_cooling_device_ops
*ops
)
936 struct thermal_cooling_device
*cdev
;
937 struct thermal_zone_device
*pos
;
940 if (strlen(type
) >= THERMAL_NAME_LENGTH
)
941 return ERR_PTR(-EINVAL
);
943 if (!ops
|| !ops
->get_max_state
|| !ops
->get_cur_state
||
945 return ERR_PTR(-EINVAL
);
947 cdev
= kzalloc(sizeof(struct thermal_cooling_device
), GFP_KERNEL
);
949 return ERR_PTR(-ENOMEM
);
951 result
= get_idr(&thermal_cdev_idr
, &thermal_idr_lock
, &cdev
->id
);
954 return ERR_PTR(result
);
957 strcpy(cdev
->type
, type
);
959 cdev
->device
.class = &thermal_class
;
960 cdev
->devdata
= devdata
;
961 dev_set_name(&cdev
->device
, "cooling_device%d", cdev
->id
);
962 result
= device_register(&cdev
->device
);
964 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
966 return ERR_PTR(result
);
971 result
= device_create_file(&cdev
->device
, &dev_attr_cdev_type
);
976 result
= device_create_file(&cdev
->device
, &dev_attr_max_state
);
980 result
= device_create_file(&cdev
->device
, &dev_attr_cur_state
);
984 mutex_lock(&thermal_list_lock
);
985 list_add(&cdev
->node
, &thermal_cdev_list
);
986 list_for_each_entry(pos
, &thermal_tz_list
, node
) {
989 result
= pos
->ops
->bind(pos
, cdev
);
994 mutex_unlock(&thermal_list_lock
);
1000 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
1001 device_unregister(&cdev
->device
);
1002 return ERR_PTR(result
);
1004 EXPORT_SYMBOL(thermal_cooling_device_register
);
1007 * thermal_cooling_device_unregister - removes the registered thermal cooling device
1008 * @cdev: the thermal cooling device to remove.
1010 * thermal_cooling_device_unregister() must be called when the device is no
1013 void thermal_cooling_device_unregister(struct
1014 thermal_cooling_device
1017 struct thermal_zone_device
*tz
;
1018 struct thermal_cooling_device
*pos
= NULL
;
1023 mutex_lock(&thermal_list_lock
);
1024 list_for_each_entry(pos
, &thermal_cdev_list
, node
)
1028 /* thermal cooling device not found */
1029 mutex_unlock(&thermal_list_lock
);
1032 list_del(&cdev
->node
);
1033 list_for_each_entry(tz
, &thermal_tz_list
, node
) {
1034 if (!tz
->ops
->unbind
)
1036 tz
->ops
->unbind(tz
, cdev
);
1038 mutex_unlock(&thermal_list_lock
);
1040 device_remove_file(&cdev
->device
, &dev_attr_cdev_type
);
1041 device_remove_file(&cdev
->device
, &dev_attr_max_state
);
1042 device_remove_file(&cdev
->device
, &dev_attr_cur_state
);
1044 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
1045 device_unregister(&cdev
->device
);
1048 EXPORT_SYMBOL(thermal_cooling_device_unregister
);
1051 * thermal_zone_device_update - force an update of a thermal zone's state
1052 * @ttz: the thermal zone to update
1055 void thermal_zone_device_update(struct thermal_zone_device
*tz
)
1058 long temp
, trip_temp
;
1059 enum thermal_trip_type trip_type
;
1060 struct thermal_cooling_device_instance
*instance
;
1061 struct thermal_cooling_device
*cdev
;
1063 mutex_lock(&tz
->lock
);
1065 if (tz
->ops
->get_temp(tz
, &temp
)) {
1066 /* get_temp failed - retry it later */
1067 pr_warn("failed to read out thermal zone %d\n", tz
->id
);
1071 for (count
= 0; count
< tz
->trips
; count
++) {
1072 tz
->ops
->get_trip_type(tz
, count
, &trip_type
);
1073 tz
->ops
->get_trip_temp(tz
, count
, &trip_temp
);
1075 switch (trip_type
) {
1076 case THERMAL_TRIP_CRITICAL
:
1077 if (temp
>= trip_temp
) {
1078 if (tz
->ops
->notify
)
1079 ret
= tz
->ops
->notify(tz
, count
,
1082 pr_emerg("Critical temperature reached (%ld C), shutting down\n",
1084 orderly_poweroff(true);
1088 case THERMAL_TRIP_HOT
:
1089 if (temp
>= trip_temp
)
1090 if (tz
->ops
->notify
)
1091 tz
->ops
->notify(tz
, count
, trip_type
);
1093 case THERMAL_TRIP_ACTIVE
:
1094 list_for_each_entry(instance
, &tz
->cooling_devices
,
1096 if (instance
->trip
!= count
)
1099 cdev
= instance
->cdev
;
1101 if (temp
>= trip_temp
)
1102 cdev
->ops
->set_cur_state(cdev
, 1);
1104 cdev
->ops
->set_cur_state(cdev
, 0);
1107 case THERMAL_TRIP_PASSIVE
:
1108 if (temp
>= trip_temp
|| tz
->passive
)
1109 thermal_zone_device_passive(tz
, temp
,
1115 if (tz
->forced_passive
)
1116 thermal_zone_device_passive(tz
, temp
, tz
->forced_passive
,
1117 THERMAL_TRIPS_NONE
);
1119 tz
->last_temperature
= temp
;
1123 thermal_zone_device_set_polling(tz
, tz
->passive_delay
);
1124 else if (tz
->polling_delay
)
1125 thermal_zone_device_set_polling(tz
, tz
->polling_delay
);
1127 thermal_zone_device_set_polling(tz
, 0);
1128 mutex_unlock(&tz
->lock
);
1130 EXPORT_SYMBOL(thermal_zone_device_update
);
1133 * create_trip_attrs - create attributes for trip points
1134 * @tz: the thermal zone device
1135 * @mask: Writeable trip point bitmap.
1137 static int create_trip_attrs(struct thermal_zone_device
*tz
, int mask
)
1140 int size
= sizeof(struct thermal_attr
) * tz
->trips
;
1142 tz
->trip_type_attrs
= kzalloc(size
, GFP_KERNEL
);
1143 if (!tz
->trip_type_attrs
)
1146 tz
->trip_temp_attrs
= kzalloc(size
, GFP_KERNEL
);
1147 if (!tz
->trip_temp_attrs
) {
1148 kfree(tz
->trip_type_attrs
);
1152 if (tz
->ops
->get_trip_hyst
) {
1153 tz
->trip_hyst_attrs
= kzalloc(size
, GFP_KERNEL
);
1154 if (!tz
->trip_hyst_attrs
) {
1155 kfree(tz
->trip_type_attrs
);
1156 kfree(tz
->trip_temp_attrs
);
1162 for (indx
= 0; indx
< tz
->trips
; indx
++) {
1163 /* create trip type attribute */
1164 snprintf(tz
->trip_type_attrs
[indx
].name
, THERMAL_NAME_LENGTH
,
1165 "trip_point_%d_type", indx
);
1167 sysfs_attr_init(&tz
->trip_type_attrs
[indx
].attr
.attr
);
1168 tz
->trip_type_attrs
[indx
].attr
.attr
.name
=
1169 tz
->trip_type_attrs
[indx
].name
;
1170 tz
->trip_type_attrs
[indx
].attr
.attr
.mode
= S_IRUGO
;
1171 tz
->trip_type_attrs
[indx
].attr
.show
= trip_point_type_show
;
1173 device_create_file(&tz
->device
,
1174 &tz
->trip_type_attrs
[indx
].attr
);
1176 /* create trip temp attribute */
1177 snprintf(tz
->trip_temp_attrs
[indx
].name
, THERMAL_NAME_LENGTH
,
1178 "trip_point_%d_temp", indx
);
1180 sysfs_attr_init(&tz
->trip_temp_attrs
[indx
].attr
.attr
);
1181 tz
->trip_temp_attrs
[indx
].attr
.attr
.name
=
1182 tz
->trip_temp_attrs
[indx
].name
;
1183 tz
->trip_temp_attrs
[indx
].attr
.attr
.mode
= S_IRUGO
;
1184 tz
->trip_temp_attrs
[indx
].attr
.show
= trip_point_temp_show
;
1185 if (mask
& (1 << indx
)) {
1186 tz
->trip_temp_attrs
[indx
].attr
.attr
.mode
|= S_IWUSR
;
1187 tz
->trip_temp_attrs
[indx
].attr
.store
=
1188 trip_point_temp_store
;
1191 device_create_file(&tz
->device
,
1192 &tz
->trip_temp_attrs
[indx
].attr
);
1194 /* create Optional trip hyst attribute */
1195 if (!tz
->ops
->get_trip_hyst
)
1197 snprintf(tz
->trip_hyst_attrs
[indx
].name
, THERMAL_NAME_LENGTH
,
1198 "trip_point_%d_hyst", indx
);
1200 sysfs_attr_init(&tz
->trip_hyst_attrs
[indx
].attr
.attr
);
1201 tz
->trip_hyst_attrs
[indx
].attr
.attr
.name
=
1202 tz
->trip_hyst_attrs
[indx
].name
;
1203 tz
->trip_hyst_attrs
[indx
].attr
.attr
.mode
= S_IRUGO
;
1204 tz
->trip_hyst_attrs
[indx
].attr
.show
= trip_point_hyst_show
;
1205 if (tz
->ops
->set_trip_hyst
) {
1206 tz
->trip_hyst_attrs
[indx
].attr
.attr
.mode
|= S_IWUSR
;
1207 tz
->trip_hyst_attrs
[indx
].attr
.store
=
1208 trip_point_hyst_store
;
1211 device_create_file(&tz
->device
,
1212 &tz
->trip_hyst_attrs
[indx
].attr
);
1217 static void remove_trip_attrs(struct thermal_zone_device
*tz
)
1221 for (indx
= 0; indx
< tz
->trips
; indx
++) {
1222 device_remove_file(&tz
->device
,
1223 &tz
->trip_type_attrs
[indx
].attr
);
1224 device_remove_file(&tz
->device
,
1225 &tz
->trip_temp_attrs
[indx
].attr
);
1226 if (tz
->ops
->get_trip_hyst
)
1227 device_remove_file(&tz
->device
,
1228 &tz
->trip_hyst_attrs
[indx
].attr
);
1230 kfree(tz
->trip_type_attrs
);
1231 kfree(tz
->trip_temp_attrs
);
1232 kfree(tz
->trip_hyst_attrs
);
1236 * thermal_zone_device_register - register a new thermal zone device
1237 * @type: the thermal zone device type
1238 * @trips: the number of trip points the thermal zone support
1239 * @mask: a bit string indicating the writeablility of trip points
1240 * @devdata: private device data
1241 * @ops: standard thermal zone device callbacks
1242 * @tc1: thermal coefficient 1 for passive calculations
1243 * @tc2: thermal coefficient 2 for passive calculations
1244 * @passive_delay: number of milliseconds to wait between polls when
1245 * performing passive cooling
1246 * @polling_delay: number of milliseconds to wait between polls when checking
1247 * whether trip points have been crossed (0 for interrupt
1250 * thermal_zone_device_unregister() must be called when the device is no
1251 * longer needed. The passive cooling formula uses tc1 and tc2 as described in
1252 * section 11.1.5.1 of the ACPI specification 3.0.
1254 struct thermal_zone_device
*thermal_zone_device_register(char *type
,
1255 int trips
, int mask
, void *devdata
,
1256 const struct thermal_zone_device_ops
*ops
,
1257 int tc1
, int tc2
, int passive_delay
, int polling_delay
)
1259 struct thermal_zone_device
*tz
;
1260 struct thermal_cooling_device
*pos
;
1261 enum thermal_trip_type trip_type
;
1266 if (strlen(type
) >= THERMAL_NAME_LENGTH
)
1267 return ERR_PTR(-EINVAL
);
1269 if (trips
> THERMAL_MAX_TRIPS
|| trips
< 0 || mask
>> trips
)
1270 return ERR_PTR(-EINVAL
);
1272 if (!ops
|| !ops
->get_temp
)
1273 return ERR_PTR(-EINVAL
);
1275 tz
= kzalloc(sizeof(struct thermal_zone_device
), GFP_KERNEL
);
1277 return ERR_PTR(-ENOMEM
);
1279 INIT_LIST_HEAD(&tz
->cooling_devices
);
1281 mutex_init(&tz
->lock
);
1282 result
= get_idr(&thermal_tz_idr
, &thermal_idr_lock
, &tz
->id
);
1285 return ERR_PTR(result
);
1288 strcpy(tz
->type
, type
);
1290 tz
->device
.class = &thermal_class
;
1291 tz
->devdata
= devdata
;
1295 tz
->passive_delay
= passive_delay
;
1296 tz
->polling_delay
= polling_delay
;
1298 dev_set_name(&tz
->device
, "thermal_zone%d", tz
->id
);
1299 result
= device_register(&tz
->device
);
1301 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
1303 return ERR_PTR(result
);
1308 result
= device_create_file(&tz
->device
, &dev_attr_type
);
1313 result
= device_create_file(&tz
->device
, &dev_attr_temp
);
1317 if (ops
->get_mode
) {
1318 result
= device_create_file(&tz
->device
, &dev_attr_mode
);
1323 result
= create_trip_attrs(tz
, mask
);
1327 for (count
= 0; count
< trips
; count
++) {
1328 tz
->ops
->get_trip_type(tz
, count
, &trip_type
);
1329 if (trip_type
== THERMAL_TRIP_PASSIVE
)
1334 result
= device_create_file(&tz
->device
,
1340 result
= thermal_add_hwmon_sysfs(tz
);
1344 mutex_lock(&thermal_list_lock
);
1345 list_add_tail(&tz
->node
, &thermal_tz_list
);
1347 list_for_each_entry(pos
, &thermal_cdev_list
, node
) {
1348 result
= ops
->bind(tz
, pos
);
1352 mutex_unlock(&thermal_list_lock
);
1354 INIT_DELAYED_WORK(&(tz
->poll_queue
), thermal_zone_device_check
);
1356 thermal_zone_device_update(tz
);
1362 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
1363 device_unregister(&tz
->device
);
1364 return ERR_PTR(result
);
1366 EXPORT_SYMBOL(thermal_zone_device_register
);
1369 * thermal_device_unregister - removes the registered thermal zone device
1370 * @tz: the thermal zone device to remove
1372 void thermal_zone_device_unregister(struct thermal_zone_device
*tz
)
1374 struct thermal_cooling_device
*cdev
;
1375 struct thermal_zone_device
*pos
= NULL
;
1380 mutex_lock(&thermal_list_lock
);
1381 list_for_each_entry(pos
, &thermal_tz_list
, node
)
1385 /* thermal zone device not found */
1386 mutex_unlock(&thermal_list_lock
);
1389 list_del(&tz
->node
);
1390 if (tz
->ops
->unbind
)
1391 list_for_each_entry(cdev
, &thermal_cdev_list
, node
)
1392 tz
->ops
->unbind(tz
, cdev
);
1393 mutex_unlock(&thermal_list_lock
);
1395 thermal_zone_device_set_polling(tz
, 0);
1398 device_remove_file(&tz
->device
, &dev_attr_type
);
1399 device_remove_file(&tz
->device
, &dev_attr_temp
);
1400 if (tz
->ops
->get_mode
)
1401 device_remove_file(&tz
->device
, &dev_attr_mode
);
1402 remove_trip_attrs(tz
);
1404 thermal_remove_hwmon_sysfs(tz
);
1405 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
1406 idr_destroy(&tz
->idr
);
1407 mutex_destroy(&tz
->lock
);
1408 device_unregister(&tz
->device
);
1411 EXPORT_SYMBOL(thermal_zone_device_unregister
);
1414 static struct genl_family thermal_event_genl_family
= {
1415 .id
= GENL_ID_GENERATE
,
1416 .name
= THERMAL_GENL_FAMILY_NAME
,
1417 .version
= THERMAL_GENL_VERSION
,
1418 .maxattr
= THERMAL_GENL_ATTR_MAX
,
1421 static struct genl_multicast_group thermal_event_mcgrp
= {
1422 .name
= THERMAL_GENL_MCAST_GROUP_NAME
,
1425 int thermal_generate_netlink_event(u32 orig
, enum events event
)
1427 struct sk_buff
*skb
;
1428 struct nlattr
*attr
;
1429 struct thermal_genl_event
*thermal_event
;
1433 static unsigned int thermal_event_seqnum
;
1435 /* allocate memory */
1436 size
= nla_total_size(sizeof(struct thermal_genl_event
)) +
1439 skb
= genlmsg_new(size
, GFP_ATOMIC
);
1443 /* add the genetlink message header */
1444 msg_header
= genlmsg_put(skb
, 0, thermal_event_seqnum
++,
1445 &thermal_event_genl_family
, 0,
1446 THERMAL_GENL_CMD_EVENT
);
1453 attr
= nla_reserve(skb
, THERMAL_GENL_ATTR_EVENT
,
1454 sizeof(struct thermal_genl_event
));
1461 thermal_event
= nla_data(attr
);
1462 if (!thermal_event
) {
1467 memset(thermal_event
, 0, sizeof(struct thermal_genl_event
));
1469 thermal_event
->orig
= orig
;
1470 thermal_event
->event
= event
;
1472 /* send multicast genetlink message */
1473 result
= genlmsg_end(skb
, msg_header
);
1479 result
= genlmsg_multicast(skb
, 0, thermal_event_mcgrp
.id
, GFP_ATOMIC
);
1481 pr_info("failed to send netlink event:%d\n", result
);
1485 EXPORT_SYMBOL(thermal_generate_netlink_event
);
1487 static int genetlink_init(void)
1491 result
= genl_register_family(&thermal_event_genl_family
);
1495 result
= genl_register_mc_group(&thermal_event_genl_family
,
1496 &thermal_event_mcgrp
);
1498 genl_unregister_family(&thermal_event_genl_family
);
1502 static void genetlink_exit(void)
1504 genl_unregister_family(&thermal_event_genl_family
);
1506 #else /* !CONFIG_NET */
1507 static inline int genetlink_init(void) { return 0; }
1508 static inline void genetlink_exit(void) {}
1509 #endif /* !CONFIG_NET */
1511 static int __init
thermal_init(void)
1515 result
= class_register(&thermal_class
);
1517 idr_destroy(&thermal_tz_idr
);
1518 idr_destroy(&thermal_cdev_idr
);
1519 mutex_destroy(&thermal_idr_lock
);
1520 mutex_destroy(&thermal_list_lock
);
1522 result
= genetlink_init();
1526 static void __exit
thermal_exit(void)
1528 class_unregister(&thermal_class
);
1529 idr_destroy(&thermal_tz_idr
);
1530 idr_destroy(&thermal_cdev_idr
);
1531 mutex_destroy(&thermal_idr_lock
);
1532 mutex_destroy(&thermal_list_lock
);
1536 fs_initcall(thermal_init
);
1537 module_exit(thermal_exit
);