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 #include <linux/module.h>
27 #include <linux/device.h>
28 #include <linux/err.h>
29 #include <linux/slab.h>
30 #include <linux/kdev_t.h>
31 #include <linux/idr.h>
32 #include <linux/thermal.h>
33 #include <linux/spinlock.h>
34 #include <linux/reboot.h>
35 #include <net/netlink.h>
36 #include <net/genetlink.h>
38 MODULE_AUTHOR("Zhang Rui");
39 MODULE_DESCRIPTION("Generic thermal management sysfs support");
40 MODULE_LICENSE("GPL");
42 #define PREFIX "Thermal: "
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 unsigned int thermal_event_seqnum
;
65 static int get_idr(struct idr
*idr
, struct mutex
*lock
, int *id
)
70 if (unlikely(idr_pre_get(idr
, GFP_KERNEL
) == 0))
75 err
= idr_get_new(idr
, NULL
, id
);
78 if (unlikely(err
== -EAGAIN
))
80 else if (unlikely(err
))
83 *id
= *id
& MAX_ID_MASK
;
87 static void release_idr(struct idr
*idr
, struct mutex
*lock
, int id
)
96 /* sys I/F for thermal zone */
98 #define to_thermal_zone(_dev) \
99 container_of(_dev, struct thermal_zone_device, device)
102 type_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
104 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
106 return sprintf(buf
, "%s\n", tz
->type
);
110 temp_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
112 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
116 if (!tz
->ops
->get_temp
)
119 ret
= tz
->ops
->get_temp(tz
, &temperature
);
124 return sprintf(buf
, "%ld\n", temperature
);
128 mode_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
130 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
131 enum thermal_device_mode mode
;
134 if (!tz
->ops
->get_mode
)
137 result
= tz
->ops
->get_mode(tz
, &mode
);
141 return sprintf(buf
, "%s\n", mode
== THERMAL_DEVICE_ENABLED
? "enabled"
146 mode_store(struct device
*dev
, struct device_attribute
*attr
,
147 const char *buf
, size_t count
)
149 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
152 if (!tz
->ops
->set_mode
)
155 if (!strncmp(buf
, "enabled", sizeof("enabled")))
156 result
= tz
->ops
->set_mode(tz
, THERMAL_DEVICE_ENABLED
);
157 else if (!strncmp(buf
, "disabled", sizeof("disabled")))
158 result
= tz
->ops
->set_mode(tz
, THERMAL_DEVICE_DISABLED
);
169 trip_point_type_show(struct device
*dev
, struct device_attribute
*attr
,
172 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
173 enum thermal_trip_type type
;
176 if (!tz
->ops
->get_trip_type
)
179 if (!sscanf(attr
->attr
.name
, "trip_point_%d_type", &trip
))
182 result
= tz
->ops
->get_trip_type(tz
, trip
, &type
);
187 case THERMAL_TRIP_CRITICAL
:
188 return sprintf(buf
, "critical\n");
189 case THERMAL_TRIP_HOT
:
190 return sprintf(buf
, "hot\n");
191 case THERMAL_TRIP_PASSIVE
:
192 return sprintf(buf
, "passive\n");
193 case THERMAL_TRIP_ACTIVE
:
194 return sprintf(buf
, "active\n");
196 return sprintf(buf
, "unknown\n");
201 trip_point_temp_show(struct device
*dev
, struct device_attribute
*attr
,
204 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
208 if (!tz
->ops
->get_trip_temp
)
211 if (!sscanf(attr
->attr
.name
, "trip_point_%d_temp", &trip
))
214 ret
= tz
->ops
->get_trip_temp(tz
, trip
, &temperature
);
219 return sprintf(buf
, "%ld\n", temperature
);
223 passive_store(struct device
*dev
, struct device_attribute
*attr
,
224 const char *buf
, size_t count
)
226 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
227 struct thermal_cooling_device
*cdev
= NULL
;
230 if (!sscanf(buf
, "%d\n", &state
))
233 /* sanity check: values below 1000 millicelcius don't make sense
234 * and can cause the system to go into a thermal heart attack
236 if (state
&& state
< 1000)
239 if (state
&& !tz
->forced_passive
) {
240 mutex_lock(&thermal_list_lock
);
241 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
242 if (!strncmp("Processor", cdev
->type
,
243 sizeof("Processor")))
244 thermal_zone_bind_cooling_device(tz
,
248 mutex_unlock(&thermal_list_lock
);
249 if (!tz
->passive_delay
)
250 tz
->passive_delay
= 1000;
251 } else if (!state
&& tz
->forced_passive
) {
252 mutex_lock(&thermal_list_lock
);
253 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
254 if (!strncmp("Processor", cdev
->type
,
255 sizeof("Processor")))
256 thermal_zone_unbind_cooling_device(tz
,
260 mutex_unlock(&thermal_list_lock
);
261 tz
->passive_delay
= 0;
267 tz
->forced_passive
= state
;
269 thermal_zone_device_update(tz
);
275 passive_show(struct device
*dev
, struct device_attribute
*attr
,
278 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
280 return sprintf(buf
, "%d\n", tz
->forced_passive
);
283 static DEVICE_ATTR(type
, 0444, type_show
, NULL
);
284 static DEVICE_ATTR(temp
, 0444, temp_show
, NULL
);
285 static DEVICE_ATTR(mode
, 0644, mode_show
, mode_store
);
286 static DEVICE_ATTR(passive
, S_IRUGO
| S_IWUSR
, passive_show
, \
289 static struct device_attribute trip_point_attrs
[] = {
290 __ATTR(trip_point_0_type
, 0444, trip_point_type_show
, NULL
),
291 __ATTR(trip_point_0_temp
, 0444, trip_point_temp_show
, NULL
),
292 __ATTR(trip_point_1_type
, 0444, trip_point_type_show
, NULL
),
293 __ATTR(trip_point_1_temp
, 0444, trip_point_temp_show
, NULL
),
294 __ATTR(trip_point_2_type
, 0444, trip_point_type_show
, NULL
),
295 __ATTR(trip_point_2_temp
, 0444, trip_point_temp_show
, NULL
),
296 __ATTR(trip_point_3_type
, 0444, trip_point_type_show
, NULL
),
297 __ATTR(trip_point_3_temp
, 0444, trip_point_temp_show
, NULL
),
298 __ATTR(trip_point_4_type
, 0444, trip_point_type_show
, NULL
),
299 __ATTR(trip_point_4_temp
, 0444, trip_point_temp_show
, NULL
),
300 __ATTR(trip_point_5_type
, 0444, trip_point_type_show
, NULL
),
301 __ATTR(trip_point_5_temp
, 0444, trip_point_temp_show
, NULL
),
302 __ATTR(trip_point_6_type
, 0444, trip_point_type_show
, NULL
),
303 __ATTR(trip_point_6_temp
, 0444, trip_point_temp_show
, NULL
),
304 __ATTR(trip_point_7_type
, 0444, trip_point_type_show
, NULL
),
305 __ATTR(trip_point_7_temp
, 0444, trip_point_temp_show
, NULL
),
306 __ATTR(trip_point_8_type
, 0444, trip_point_type_show
, NULL
),
307 __ATTR(trip_point_8_temp
, 0444, trip_point_temp_show
, NULL
),
308 __ATTR(trip_point_9_type
, 0444, trip_point_type_show
, NULL
),
309 __ATTR(trip_point_9_temp
, 0444, trip_point_temp_show
, NULL
),
310 __ATTR(trip_point_10_type
, 0444, trip_point_type_show
, NULL
),
311 __ATTR(trip_point_10_temp
, 0444, trip_point_temp_show
, NULL
),
312 __ATTR(trip_point_11_type
, 0444, trip_point_type_show
, NULL
),
313 __ATTR(trip_point_11_temp
, 0444, trip_point_temp_show
, NULL
),
316 #define TRIP_POINT_ATTR_ADD(_dev, _index, result) \
318 result = device_create_file(_dev, \
319 &trip_point_attrs[_index * 2]); \
322 result = device_create_file(_dev, \
323 &trip_point_attrs[_index * 2 + 1]); \
326 #define TRIP_POINT_ATTR_REMOVE(_dev, _index) \
328 device_remove_file(_dev, &trip_point_attrs[_index * 2]); \
329 device_remove_file(_dev, &trip_point_attrs[_index * 2 + 1]); \
332 /* sys I/F for cooling device */
333 #define to_cooling_device(_dev) \
334 container_of(_dev, struct thermal_cooling_device, device)
337 thermal_cooling_device_type_show(struct device
*dev
,
338 struct device_attribute
*attr
, char *buf
)
340 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
342 return sprintf(buf
, "%s\n", cdev
->type
);
346 thermal_cooling_device_max_state_show(struct device
*dev
,
347 struct device_attribute
*attr
, char *buf
)
349 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
353 ret
= cdev
->ops
->get_max_state(cdev
, &state
);
356 return sprintf(buf
, "%ld\n", state
);
360 thermal_cooling_device_cur_state_show(struct device
*dev
,
361 struct device_attribute
*attr
, char *buf
)
363 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
367 ret
= cdev
->ops
->get_cur_state(cdev
, &state
);
370 return sprintf(buf
, "%ld\n", state
);
374 thermal_cooling_device_cur_state_store(struct device
*dev
,
375 struct device_attribute
*attr
,
376 const char *buf
, size_t count
)
378 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
382 if (!sscanf(buf
, "%ld\n", &state
))
388 result
= cdev
->ops
->set_cur_state(cdev
, state
);
394 static struct device_attribute dev_attr_cdev_type
=
395 __ATTR(type
, 0444, thermal_cooling_device_type_show
, NULL
);
396 static DEVICE_ATTR(max_state
, 0444,
397 thermal_cooling_device_max_state_show
, NULL
);
398 static DEVICE_ATTR(cur_state
, 0644,
399 thermal_cooling_device_cur_state_show
,
400 thermal_cooling_device_cur_state_store
);
403 thermal_cooling_device_trip_point_show(struct device
*dev
,
404 struct device_attribute
*attr
, char *buf
)
406 struct thermal_cooling_device_instance
*instance
;
409 container_of(attr
, struct thermal_cooling_device_instance
, attr
);
411 if (instance
->trip
== THERMAL_TRIPS_NONE
)
412 return sprintf(buf
, "-1\n");
414 return sprintf(buf
, "%d\n", instance
->trip
);
417 /* Device management */
419 #if defined(CONFIG_THERMAL_HWMON)
422 #include <linux/hwmon.h>
424 /* thermal zone devices with the same type share one hwmon device */
425 struct thermal_hwmon_device
{
426 char type
[THERMAL_NAME_LENGTH
];
427 struct device
*device
;
429 struct list_head tz_list
;
430 struct list_head node
;
433 struct thermal_hwmon_attr
{
434 struct device_attribute attr
;
438 /* one temperature input for each thermal zone */
439 struct thermal_hwmon_temp
{
440 struct list_head hwmon_node
;
441 struct thermal_zone_device
*tz
;
442 struct thermal_hwmon_attr temp_input
; /* hwmon sys attr */
443 struct thermal_hwmon_attr temp_crit
; /* hwmon sys attr */
446 static LIST_HEAD(thermal_hwmon_list
);
449 name_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
451 struct thermal_hwmon_device
*hwmon
= dev_get_drvdata(dev
);
452 return sprintf(buf
, "%s\n", hwmon
->type
);
454 static DEVICE_ATTR(name
, 0444, name_show
, NULL
);
457 temp_input_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
461 struct thermal_hwmon_attr
*hwmon_attr
462 = container_of(attr
, struct thermal_hwmon_attr
, attr
);
463 struct thermal_hwmon_temp
*temp
464 = container_of(hwmon_attr
, struct thermal_hwmon_temp
,
466 struct thermal_zone_device
*tz
= temp
->tz
;
468 ret
= tz
->ops
->get_temp(tz
, &temperature
);
473 return sprintf(buf
, "%ld\n", temperature
);
477 temp_crit_show(struct device
*dev
, struct device_attribute
*attr
,
480 struct thermal_hwmon_attr
*hwmon_attr
481 = container_of(attr
, struct thermal_hwmon_attr
, attr
);
482 struct thermal_hwmon_temp
*temp
483 = container_of(hwmon_attr
, struct thermal_hwmon_temp
,
485 struct thermal_zone_device
*tz
= temp
->tz
;
489 ret
= tz
->ops
->get_trip_temp(tz
, 0, &temperature
);
493 return sprintf(buf
, "%ld\n", temperature
);
497 static struct thermal_hwmon_device
*
498 thermal_hwmon_lookup_by_type(const struct thermal_zone_device
*tz
)
500 struct thermal_hwmon_device
*hwmon
;
502 mutex_lock(&thermal_list_lock
);
503 list_for_each_entry(hwmon
, &thermal_hwmon_list
, node
)
504 if (!strcmp(hwmon
->type
, tz
->type
)) {
505 mutex_unlock(&thermal_list_lock
);
508 mutex_unlock(&thermal_list_lock
);
513 /* Find the temperature input matching a given thermal zone */
514 static struct thermal_hwmon_temp
*
515 thermal_hwmon_lookup_temp(const struct thermal_hwmon_device
*hwmon
,
516 const struct thermal_zone_device
*tz
)
518 struct thermal_hwmon_temp
*temp
;
520 mutex_lock(&thermal_list_lock
);
521 list_for_each_entry(temp
, &hwmon
->tz_list
, hwmon_node
)
522 if (temp
->tz
== tz
) {
523 mutex_unlock(&thermal_list_lock
);
526 mutex_unlock(&thermal_list_lock
);
532 thermal_add_hwmon_sysfs(struct thermal_zone_device
*tz
)
534 struct thermal_hwmon_device
*hwmon
;
535 struct thermal_hwmon_temp
*temp
;
536 int new_hwmon_device
= 1;
539 hwmon
= thermal_hwmon_lookup_by_type(tz
);
541 new_hwmon_device
= 0;
542 goto register_sys_interface
;
545 hwmon
= kzalloc(sizeof(struct thermal_hwmon_device
), GFP_KERNEL
);
549 INIT_LIST_HEAD(&hwmon
->tz_list
);
550 strlcpy(hwmon
->type
, tz
->type
, THERMAL_NAME_LENGTH
);
551 hwmon
->device
= hwmon_device_register(NULL
);
552 if (IS_ERR(hwmon
->device
)) {
553 result
= PTR_ERR(hwmon
->device
);
556 dev_set_drvdata(hwmon
->device
, hwmon
);
557 result
= device_create_file(hwmon
->device
, &dev_attr_name
);
561 register_sys_interface
:
562 temp
= kzalloc(sizeof(struct thermal_hwmon_temp
), GFP_KERNEL
);
565 goto unregister_name
;
571 snprintf(temp
->temp_input
.name
, THERMAL_NAME_LENGTH
,
572 "temp%d_input", hwmon
->count
);
573 temp
->temp_input
.attr
.attr
.name
= temp
->temp_input
.name
;
574 temp
->temp_input
.attr
.attr
.mode
= 0444;
575 temp
->temp_input
.attr
.show
= temp_input_show
;
576 sysfs_attr_init(&temp
->temp_input
.attr
.attr
);
577 result
= device_create_file(hwmon
->device
, &temp
->temp_input
.attr
);
581 if (tz
->ops
->get_crit_temp
) {
582 unsigned long temperature
;
583 if (!tz
->ops
->get_crit_temp(tz
, &temperature
)) {
584 snprintf(temp
->temp_crit
.name
, THERMAL_NAME_LENGTH
,
585 "temp%d_crit", hwmon
->count
);
586 temp
->temp_crit
.attr
.attr
.name
= temp
->temp_crit
.name
;
587 temp
->temp_crit
.attr
.attr
.mode
= 0444;
588 temp
->temp_crit
.attr
.show
= temp_crit_show
;
589 sysfs_attr_init(&temp
->temp_crit
.attr
.attr
);
590 result
= device_create_file(hwmon
->device
,
591 &temp
->temp_crit
.attr
);
593 goto unregister_input
;
597 mutex_lock(&thermal_list_lock
);
598 if (new_hwmon_device
)
599 list_add_tail(&hwmon
->node
, &thermal_hwmon_list
);
600 list_add_tail(&temp
->hwmon_node
, &hwmon
->tz_list
);
601 mutex_unlock(&thermal_list_lock
);
606 device_remove_file(hwmon
->device
, &temp
->temp_input
.attr
);
610 if (new_hwmon_device
) {
611 device_remove_file(hwmon
->device
, &dev_attr_name
);
612 hwmon_device_unregister(hwmon
->device
);
615 if (new_hwmon_device
)
622 thermal_remove_hwmon_sysfs(struct thermal_zone_device
*tz
)
624 struct thermal_hwmon_device
*hwmon
;
625 struct thermal_hwmon_temp
*temp
;
627 hwmon
= thermal_hwmon_lookup_by_type(tz
);
628 if (unlikely(!hwmon
)) {
629 /* Should never happen... */
630 dev_dbg(&tz
->device
, "hwmon device lookup failed!\n");
634 temp
= thermal_hwmon_lookup_temp(hwmon
, tz
);
635 if (unlikely(!temp
)) {
636 /* Should never happen... */
637 dev_dbg(&tz
->device
, "temperature input lookup failed!\n");
641 device_remove_file(hwmon
->device
, &temp
->temp_input
.attr
);
642 if (tz
->ops
->get_crit_temp
)
643 device_remove_file(hwmon
->device
, &temp
->temp_crit
.attr
);
645 mutex_lock(&thermal_list_lock
);
646 list_del(&temp
->hwmon_node
);
648 if (!list_empty(&hwmon
->tz_list
)) {
649 mutex_unlock(&thermal_list_lock
);
652 list_del(&hwmon
->node
);
653 mutex_unlock(&thermal_list_lock
);
655 device_remove_file(hwmon
->device
, &dev_attr_name
);
656 hwmon_device_unregister(hwmon
->device
);
661 thermal_add_hwmon_sysfs(struct thermal_zone_device
*tz
)
667 thermal_remove_hwmon_sysfs(struct thermal_zone_device
*tz
)
672 static void thermal_zone_device_set_polling(struct thermal_zone_device
*tz
,
675 cancel_delayed_work(&(tz
->poll_queue
));
681 queue_delayed_work(system_freezable_wq
, &(tz
->poll_queue
),
682 round_jiffies(msecs_to_jiffies(delay
)));
684 queue_delayed_work(system_freezable_wq
, &(tz
->poll_queue
),
685 msecs_to_jiffies(delay
));
688 static void thermal_zone_device_passive(struct thermal_zone_device
*tz
,
689 int temp
, int trip_temp
, int trip
)
692 struct thermal_cooling_device_instance
*instance
;
693 struct thermal_cooling_device
*cdev
;
694 long state
, max_state
;
699 * Calculate the thermal trend (using the passive cooling equation)
700 * and modify the performance limit for all passive cooling devices
701 * accordingly. Note that we assume symmetry.
703 if (temp
>= trip_temp
) {
706 trend
= (tz
->tc1
* (temp
- tz
->last_temperature
)) +
707 (tz
->tc2
* (temp
- trip_temp
));
711 list_for_each_entry(instance
, &tz
->cooling_devices
,
713 if (instance
->trip
!= trip
)
715 cdev
= instance
->cdev
;
716 cdev
->ops
->get_cur_state(cdev
, &state
);
717 cdev
->ops
->get_max_state(cdev
, &max_state
);
718 if (state
++ < max_state
)
719 cdev
->ops
->set_cur_state(cdev
, state
);
721 } else if (trend
< 0) { /* Cooling off? */
722 list_for_each_entry(instance
, &tz
->cooling_devices
,
724 if (instance
->trip
!= trip
)
726 cdev
= instance
->cdev
;
727 cdev
->ops
->get_cur_state(cdev
, &state
);
728 cdev
->ops
->get_max_state(cdev
, &max_state
);
730 cdev
->ops
->set_cur_state(cdev
, --state
);
739 * Implement passive cooling hysteresis to slowly increase performance
740 * and avoid thrashing around the passive trip point. Note that we
743 list_for_each_entry(instance
, &tz
->cooling_devices
, node
) {
744 if (instance
->trip
!= trip
)
746 cdev
= instance
->cdev
;
747 cdev
->ops
->get_cur_state(cdev
, &state
);
748 cdev
->ops
->get_max_state(cdev
, &max_state
);
750 cdev
->ops
->set_cur_state(cdev
, --state
);
756 static void thermal_zone_device_check(struct work_struct
*work
)
758 struct thermal_zone_device
*tz
= container_of(work
, struct
761 thermal_zone_device_update(tz
);
765 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
766 * @tz: thermal zone device
767 * @trip: indicates which trip point the cooling devices is
768 * associated with in this thermal zone.
769 * @cdev: thermal cooling device
771 * This function is usually called in the thermal zone device .bind callback.
773 int thermal_zone_bind_cooling_device(struct thermal_zone_device
*tz
,
775 struct thermal_cooling_device
*cdev
)
777 struct thermal_cooling_device_instance
*dev
;
778 struct thermal_cooling_device_instance
*pos
;
779 struct thermal_zone_device
*pos1
;
780 struct thermal_cooling_device
*pos2
;
783 if (trip
>= tz
->trips
|| (trip
< 0 && trip
!= THERMAL_TRIPS_NONE
))
786 list_for_each_entry(pos1
, &thermal_tz_list
, node
) {
790 list_for_each_entry(pos2
, &thermal_cdev_list
, node
) {
795 if (tz
!= pos1
|| cdev
!= pos2
)
799 kzalloc(sizeof(struct thermal_cooling_device_instance
), GFP_KERNEL
);
805 result
= get_idr(&tz
->idr
, &tz
->lock
, &dev
->id
);
809 sprintf(dev
->name
, "cdev%d", dev
->id
);
811 sysfs_create_link(&tz
->device
.kobj
, &cdev
->device
.kobj
, dev
->name
);
815 sprintf(dev
->attr_name
, "cdev%d_trip_point", dev
->id
);
816 sysfs_attr_init(&dev
->attr
.attr
);
817 dev
->attr
.attr
.name
= dev
->attr_name
;
818 dev
->attr
.attr
.mode
= 0444;
819 dev
->attr
.show
= thermal_cooling_device_trip_point_show
;
820 result
= device_create_file(&tz
->device
, &dev
->attr
);
822 goto remove_symbol_link
;
824 mutex_lock(&tz
->lock
);
825 list_for_each_entry(pos
, &tz
->cooling_devices
, node
)
826 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
831 list_add_tail(&dev
->node
, &tz
->cooling_devices
);
832 mutex_unlock(&tz
->lock
);
837 device_remove_file(&tz
->device
, &dev
->attr
);
839 sysfs_remove_link(&tz
->device
.kobj
, dev
->name
);
841 release_idr(&tz
->idr
, &tz
->lock
, dev
->id
);
847 EXPORT_SYMBOL(thermal_zone_bind_cooling_device
);
850 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
851 * @tz: thermal zone device
852 * @trip: indicates which trip point the cooling devices is
853 * associated with in this thermal zone.
854 * @cdev: thermal cooling device
856 * This function is usually called in the thermal zone device .unbind callback.
858 int thermal_zone_unbind_cooling_device(struct thermal_zone_device
*tz
,
860 struct thermal_cooling_device
*cdev
)
862 struct thermal_cooling_device_instance
*pos
, *next
;
864 mutex_lock(&tz
->lock
);
865 list_for_each_entry_safe(pos
, next
, &tz
->cooling_devices
, node
) {
866 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
867 list_del(&pos
->node
);
868 mutex_unlock(&tz
->lock
);
872 mutex_unlock(&tz
->lock
);
877 device_remove_file(&tz
->device
, &pos
->attr
);
878 sysfs_remove_link(&tz
->device
.kobj
, pos
->name
);
879 release_idr(&tz
->idr
, &tz
->lock
, pos
->id
);
884 EXPORT_SYMBOL(thermal_zone_unbind_cooling_device
);
886 static void thermal_release(struct device
*dev
)
888 struct thermal_zone_device
*tz
;
889 struct thermal_cooling_device
*cdev
;
891 if (!strncmp(dev_name(dev
), "thermal_zone", sizeof "thermal_zone" - 1)) {
892 tz
= to_thermal_zone(dev
);
895 cdev
= to_cooling_device(dev
);
900 static struct class thermal_class
= {
902 .dev_release
= thermal_release
,
906 * thermal_cooling_device_register - register a new thermal cooling device
907 * @type: the thermal cooling device type.
908 * @devdata: device private data.
909 * @ops: standard thermal cooling devices callbacks.
911 struct thermal_cooling_device
*thermal_cooling_device_register(
912 char *type
, void *devdata
, const struct thermal_cooling_device_ops
*ops
)
914 struct thermal_cooling_device
*cdev
;
915 struct thermal_zone_device
*pos
;
918 if (strlen(type
) >= THERMAL_NAME_LENGTH
)
919 return ERR_PTR(-EINVAL
);
921 if (!ops
|| !ops
->get_max_state
|| !ops
->get_cur_state
||
923 return ERR_PTR(-EINVAL
);
925 cdev
= kzalloc(sizeof(struct thermal_cooling_device
), GFP_KERNEL
);
927 return ERR_PTR(-ENOMEM
);
929 result
= get_idr(&thermal_cdev_idr
, &thermal_idr_lock
, &cdev
->id
);
932 return ERR_PTR(result
);
935 strcpy(cdev
->type
, type
);
937 cdev
->device
.class = &thermal_class
;
938 cdev
->devdata
= devdata
;
939 dev_set_name(&cdev
->device
, "cooling_device%d", cdev
->id
);
940 result
= device_register(&cdev
->device
);
942 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
944 return ERR_PTR(result
);
949 result
= device_create_file(&cdev
->device
, &dev_attr_cdev_type
);
954 result
= device_create_file(&cdev
->device
, &dev_attr_max_state
);
958 result
= device_create_file(&cdev
->device
, &dev_attr_cur_state
);
962 mutex_lock(&thermal_list_lock
);
963 list_add(&cdev
->node
, &thermal_cdev_list
);
964 list_for_each_entry(pos
, &thermal_tz_list
, node
) {
967 result
= pos
->ops
->bind(pos
, cdev
);
972 mutex_unlock(&thermal_list_lock
);
978 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
979 device_unregister(&cdev
->device
);
980 return ERR_PTR(result
);
983 EXPORT_SYMBOL(thermal_cooling_device_register
);
986 * thermal_cooling_device_unregister - removes the registered thermal cooling device
987 * @cdev: the thermal cooling device to remove.
989 * thermal_cooling_device_unregister() must be called when the device is no
992 void thermal_cooling_device_unregister(struct
993 thermal_cooling_device
996 struct thermal_zone_device
*tz
;
997 struct thermal_cooling_device
*pos
= NULL
;
1002 mutex_lock(&thermal_list_lock
);
1003 list_for_each_entry(pos
, &thermal_cdev_list
, node
)
1007 /* thermal cooling device not found */
1008 mutex_unlock(&thermal_list_lock
);
1011 list_del(&cdev
->node
);
1012 list_for_each_entry(tz
, &thermal_tz_list
, node
) {
1013 if (!tz
->ops
->unbind
)
1015 tz
->ops
->unbind(tz
, cdev
);
1017 mutex_unlock(&thermal_list_lock
);
1019 device_remove_file(&cdev
->device
, &dev_attr_cdev_type
);
1020 device_remove_file(&cdev
->device
, &dev_attr_max_state
);
1021 device_remove_file(&cdev
->device
, &dev_attr_cur_state
);
1023 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
1024 device_unregister(&cdev
->device
);
1028 EXPORT_SYMBOL(thermal_cooling_device_unregister
);
1031 * thermal_zone_device_update - force an update of a thermal zone's state
1032 * @ttz: the thermal zone to update
1035 void thermal_zone_device_update(struct thermal_zone_device
*tz
)
1038 long temp
, trip_temp
;
1039 enum thermal_trip_type trip_type
;
1040 struct thermal_cooling_device_instance
*instance
;
1041 struct thermal_cooling_device
*cdev
;
1043 mutex_lock(&tz
->lock
);
1045 if (tz
->ops
->get_temp(tz
, &temp
)) {
1046 /* get_temp failed - retry it later */
1047 printk(KERN_WARNING PREFIX
"failed to read out thermal zone "
1052 for (count
= 0; count
< tz
->trips
; count
++) {
1053 tz
->ops
->get_trip_type(tz
, count
, &trip_type
);
1054 tz
->ops
->get_trip_temp(tz
, count
, &trip_temp
);
1056 switch (trip_type
) {
1057 case THERMAL_TRIP_CRITICAL
:
1058 if (temp
>= trip_temp
) {
1059 if (tz
->ops
->notify
)
1060 ret
= tz
->ops
->notify(tz
, count
,
1064 "Critical temperature reached (%ld C), shutting down.\n",
1066 orderly_poweroff(true);
1070 case THERMAL_TRIP_HOT
:
1071 if (temp
>= trip_temp
)
1072 if (tz
->ops
->notify
)
1073 tz
->ops
->notify(tz
, count
, trip_type
);
1075 case THERMAL_TRIP_ACTIVE
:
1076 list_for_each_entry(instance
, &tz
->cooling_devices
,
1078 if (instance
->trip
!= count
)
1081 cdev
= instance
->cdev
;
1083 if (temp
>= trip_temp
)
1084 cdev
->ops
->set_cur_state(cdev
, 1);
1086 cdev
->ops
->set_cur_state(cdev
, 0);
1089 case THERMAL_TRIP_PASSIVE
:
1090 if (temp
>= trip_temp
|| tz
->passive
)
1091 thermal_zone_device_passive(tz
, temp
,
1097 if (tz
->forced_passive
)
1098 thermal_zone_device_passive(tz
, temp
, tz
->forced_passive
,
1099 THERMAL_TRIPS_NONE
);
1101 tz
->last_temperature
= temp
;
1105 thermal_zone_device_set_polling(tz
, tz
->passive_delay
);
1106 else if (tz
->polling_delay
)
1107 thermal_zone_device_set_polling(tz
, tz
->polling_delay
);
1109 thermal_zone_device_set_polling(tz
, 0);
1110 mutex_unlock(&tz
->lock
);
1112 EXPORT_SYMBOL(thermal_zone_device_update
);
1115 * thermal_zone_device_register - register a new thermal zone device
1116 * @type: the thermal zone device type
1117 * @trips: the number of trip points the thermal zone support
1118 * @devdata: private device data
1119 * @ops: standard thermal zone device callbacks
1120 * @tc1: thermal coefficient 1 for passive calculations
1121 * @tc2: thermal coefficient 2 for passive calculations
1122 * @passive_delay: number of milliseconds to wait between polls when
1123 * performing passive cooling
1124 * @polling_delay: number of milliseconds to wait between polls when checking
1125 * whether trip points have been crossed (0 for interrupt
1128 * thermal_zone_device_unregister() must be called when the device is no
1129 * longer needed. The passive cooling formula uses tc1 and tc2 as described in
1130 * section 11.1.5.1 of the ACPI specification 3.0.
1132 struct thermal_zone_device
*thermal_zone_device_register(char *type
,
1133 int trips
, void *devdata
,
1134 const struct thermal_zone_device_ops
*ops
,
1135 int tc1
, int tc2
, int passive_delay
, int polling_delay
)
1137 struct thermal_zone_device
*tz
;
1138 struct thermal_cooling_device
*pos
;
1139 enum thermal_trip_type trip_type
;
1144 if (strlen(type
) >= THERMAL_NAME_LENGTH
)
1145 return ERR_PTR(-EINVAL
);
1147 if (trips
> THERMAL_MAX_TRIPS
|| trips
< 0)
1148 return ERR_PTR(-EINVAL
);
1150 if (!ops
|| !ops
->get_temp
)
1151 return ERR_PTR(-EINVAL
);
1153 tz
= kzalloc(sizeof(struct thermal_zone_device
), GFP_KERNEL
);
1155 return ERR_PTR(-ENOMEM
);
1157 INIT_LIST_HEAD(&tz
->cooling_devices
);
1159 mutex_init(&tz
->lock
);
1160 result
= get_idr(&thermal_tz_idr
, &thermal_idr_lock
, &tz
->id
);
1163 return ERR_PTR(result
);
1166 strcpy(tz
->type
, type
);
1168 tz
->device
.class = &thermal_class
;
1169 tz
->devdata
= devdata
;
1173 tz
->passive_delay
= passive_delay
;
1174 tz
->polling_delay
= polling_delay
;
1176 dev_set_name(&tz
->device
, "thermal_zone%d", tz
->id
);
1177 result
= device_register(&tz
->device
);
1179 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
1181 return ERR_PTR(result
);
1186 result
= device_create_file(&tz
->device
, &dev_attr_type
);
1191 result
= device_create_file(&tz
->device
, &dev_attr_temp
);
1195 if (ops
->get_mode
) {
1196 result
= device_create_file(&tz
->device
, &dev_attr_mode
);
1201 for (count
= 0; count
< trips
; count
++) {
1202 TRIP_POINT_ATTR_ADD(&tz
->device
, count
, result
);
1205 tz
->ops
->get_trip_type(tz
, count
, &trip_type
);
1206 if (trip_type
== THERMAL_TRIP_PASSIVE
)
1211 result
= device_create_file(&tz
->device
,
1217 result
= thermal_add_hwmon_sysfs(tz
);
1221 mutex_lock(&thermal_list_lock
);
1222 list_add_tail(&tz
->node
, &thermal_tz_list
);
1224 list_for_each_entry(pos
, &thermal_cdev_list
, node
) {
1225 result
= ops
->bind(tz
, pos
);
1229 mutex_unlock(&thermal_list_lock
);
1231 INIT_DELAYED_WORK(&(tz
->poll_queue
), thermal_zone_device_check
);
1233 thermal_zone_device_update(tz
);
1239 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
1240 device_unregister(&tz
->device
);
1241 return ERR_PTR(result
);
1244 EXPORT_SYMBOL(thermal_zone_device_register
);
1247 * thermal_device_unregister - removes the registered thermal zone device
1248 * @tz: the thermal zone device to remove
1250 void thermal_zone_device_unregister(struct thermal_zone_device
*tz
)
1252 struct thermal_cooling_device
*cdev
;
1253 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
);
1269 if (tz
->ops
->unbind
)
1270 list_for_each_entry(cdev
, &thermal_cdev_list
, node
)
1271 tz
->ops
->unbind(tz
, cdev
);
1272 mutex_unlock(&thermal_list_lock
);
1274 thermal_zone_device_set_polling(tz
, 0);
1277 device_remove_file(&tz
->device
, &dev_attr_type
);
1278 device_remove_file(&tz
->device
, &dev_attr_temp
);
1279 if (tz
->ops
->get_mode
)
1280 device_remove_file(&tz
->device
, &dev_attr_mode
);
1282 for (count
= 0; count
< tz
->trips
; count
++)
1283 TRIP_POINT_ATTR_REMOVE(&tz
->device
, count
);
1285 thermal_remove_hwmon_sysfs(tz
);
1286 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
1287 idr_destroy(&tz
->idr
);
1288 mutex_destroy(&tz
->lock
);
1289 device_unregister(&tz
->device
);
1293 EXPORT_SYMBOL(thermal_zone_device_unregister
);
1296 static struct genl_family thermal_event_genl_family
= {
1297 .id
= GENL_ID_GENERATE
,
1298 .name
= THERMAL_GENL_FAMILY_NAME
,
1299 .version
= THERMAL_GENL_VERSION
,
1300 .maxattr
= THERMAL_GENL_ATTR_MAX
,
1303 static struct genl_multicast_group thermal_event_mcgrp
= {
1304 .name
= THERMAL_GENL_MCAST_GROUP_NAME
,
1307 int thermal_generate_netlink_event(u32 orig
, enum events event
)
1309 struct sk_buff
*skb
;
1310 struct nlattr
*attr
;
1311 struct thermal_genl_event
*thermal_event
;
1316 /* allocate memory */
1317 size
= nla_total_size(sizeof(struct thermal_genl_event
)) + \
1320 skb
= genlmsg_new(size
, GFP_ATOMIC
);
1324 /* add the genetlink message header */
1325 msg_header
= genlmsg_put(skb
, 0, thermal_event_seqnum
++,
1326 &thermal_event_genl_family
, 0,
1327 THERMAL_GENL_CMD_EVENT
);
1334 attr
= nla_reserve(skb
, THERMAL_GENL_ATTR_EVENT
, \
1335 sizeof(struct thermal_genl_event
));
1342 thermal_event
= nla_data(attr
);
1343 if (!thermal_event
) {
1348 memset(thermal_event
, 0, sizeof(struct thermal_genl_event
));
1350 thermal_event
->orig
= orig
;
1351 thermal_event
->event
= event
;
1353 /* send multicast genetlink message */
1354 result
= genlmsg_end(skb
, msg_header
);
1360 result
= genlmsg_multicast(skb
, 0, thermal_event_mcgrp
.id
, GFP_ATOMIC
);
1362 printk(KERN_INFO
"failed to send netlink event:%d", result
);
1366 EXPORT_SYMBOL(thermal_generate_netlink_event
);
1368 static int genetlink_init(void)
1372 result
= genl_register_family(&thermal_event_genl_family
);
1376 result
= genl_register_mc_group(&thermal_event_genl_family
,
1377 &thermal_event_mcgrp
);
1379 genl_unregister_family(&thermal_event_genl_family
);
1383 static void genetlink_exit(void)
1385 genl_unregister_family(&thermal_event_genl_family
);
1387 #else /* !CONFIG_NET */
1388 static inline int genetlink_init(void) { return 0; }
1389 static inline void genetlink_exit(void) {}
1390 #endif /* !CONFIG_NET */
1392 static int __init
thermal_init(void)
1396 result
= class_register(&thermal_class
);
1398 idr_destroy(&thermal_tz_idr
);
1399 idr_destroy(&thermal_cdev_idr
);
1400 mutex_destroy(&thermal_idr_lock
);
1401 mutex_destroy(&thermal_list_lock
);
1403 result
= genetlink_init();
1407 static void __exit
thermal_exit(void)
1409 class_unregister(&thermal_class
);
1410 idr_destroy(&thermal_tz_idr
);
1411 idr_destroy(&thermal_cdev_idr
);
1412 mutex_destroy(&thermal_idr_lock
);
1413 mutex_destroy(&thermal_list_lock
);
1417 fs_initcall(thermal_init
);
1418 module_exit(thermal_exit
);