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 struct genl_family thermal_event_genl_family
= {
66 .id
= GENL_ID_GENERATE
,
67 .name
= THERMAL_GENL_FAMILY_NAME
,
68 .version
= THERMAL_GENL_VERSION
,
69 .maxattr
= THERMAL_GENL_ATTR_MAX
,
72 static struct genl_multicast_group thermal_event_mcgrp
= {
73 .name
= THERMAL_GENL_MCAST_GROUP_NAME
,
76 static int genetlink_init(void);
77 static void genetlink_exit(void);
79 static int get_idr(struct idr
*idr
, struct mutex
*lock
, int *id
)
84 if (unlikely(idr_pre_get(idr
, GFP_KERNEL
) == 0))
89 err
= idr_get_new(idr
, NULL
, id
);
92 if (unlikely(err
== -EAGAIN
))
94 else if (unlikely(err
))
97 *id
= *id
& MAX_ID_MASK
;
101 static void release_idr(struct idr
*idr
, struct mutex
*lock
, int id
)
110 /* sys I/F for thermal zone */
112 #define to_thermal_zone(_dev) \
113 container_of(_dev, struct thermal_zone_device, device)
116 type_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
118 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
120 return sprintf(buf
, "%s\n", tz
->type
);
124 temp_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
126 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
130 if (!tz
->ops
->get_temp
)
133 ret
= tz
->ops
->get_temp(tz
, &temperature
);
138 return sprintf(buf
, "%ld\n", temperature
);
142 mode_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
144 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
145 enum thermal_device_mode mode
;
148 if (!tz
->ops
->get_mode
)
151 result
= tz
->ops
->get_mode(tz
, &mode
);
155 return sprintf(buf
, "%s\n", mode
== THERMAL_DEVICE_ENABLED
? "enabled"
160 mode_store(struct device
*dev
, struct device_attribute
*attr
,
161 const char *buf
, size_t count
)
163 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
166 if (!tz
->ops
->set_mode
)
169 if (!strncmp(buf
, "enabled", sizeof("enabled")))
170 result
= tz
->ops
->set_mode(tz
, THERMAL_DEVICE_ENABLED
);
171 else if (!strncmp(buf
, "disabled", sizeof("disabled")))
172 result
= tz
->ops
->set_mode(tz
, THERMAL_DEVICE_DISABLED
);
183 trip_point_type_show(struct device
*dev
, struct device_attribute
*attr
,
186 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
187 enum thermal_trip_type type
;
190 if (!tz
->ops
->get_trip_type
)
193 if (!sscanf(attr
->attr
.name
, "trip_point_%d_type", &trip
))
196 result
= tz
->ops
->get_trip_type(tz
, trip
, &type
);
201 case THERMAL_TRIP_CRITICAL
:
202 return sprintf(buf
, "critical\n");
203 case THERMAL_TRIP_HOT
:
204 return sprintf(buf
, "hot\n");
205 case THERMAL_TRIP_PASSIVE
:
206 return sprintf(buf
, "passive\n");
207 case THERMAL_TRIP_ACTIVE
:
208 return sprintf(buf
, "active\n");
210 return sprintf(buf
, "unknown\n");
215 trip_point_temp_show(struct device
*dev
, struct device_attribute
*attr
,
218 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
222 if (!tz
->ops
->get_trip_temp
)
225 if (!sscanf(attr
->attr
.name
, "trip_point_%d_temp", &trip
))
228 ret
= tz
->ops
->get_trip_temp(tz
, trip
, &temperature
);
233 return sprintf(buf
, "%ld\n", temperature
);
237 passive_store(struct device
*dev
, struct device_attribute
*attr
,
238 const char *buf
, size_t count
)
240 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
241 struct thermal_cooling_device
*cdev
= NULL
;
244 if (!sscanf(buf
, "%d\n", &state
))
247 /* sanity check: values below 1000 millicelcius don't make sense
248 * and can cause the system to go into a thermal heart attack
250 if (state
&& state
< 1000)
253 if (state
&& !tz
->forced_passive
) {
254 mutex_lock(&thermal_list_lock
);
255 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
256 if (!strncmp("Processor", cdev
->type
,
257 sizeof("Processor")))
258 thermal_zone_bind_cooling_device(tz
,
262 mutex_unlock(&thermal_list_lock
);
263 if (!tz
->passive_delay
)
264 tz
->passive_delay
= 1000;
265 } else if (!state
&& tz
->forced_passive
) {
266 mutex_lock(&thermal_list_lock
);
267 list_for_each_entry(cdev
, &thermal_cdev_list
, node
) {
268 if (!strncmp("Processor", cdev
->type
,
269 sizeof("Processor")))
270 thermal_zone_unbind_cooling_device(tz
,
274 mutex_unlock(&thermal_list_lock
);
275 tz
->passive_delay
= 0;
281 tz
->forced_passive
= state
;
283 thermal_zone_device_update(tz
);
289 passive_show(struct device
*dev
, struct device_attribute
*attr
,
292 struct thermal_zone_device
*tz
= to_thermal_zone(dev
);
294 return sprintf(buf
, "%d\n", tz
->forced_passive
);
297 static DEVICE_ATTR(type
, 0444, type_show
, NULL
);
298 static DEVICE_ATTR(temp
, 0444, temp_show
, NULL
);
299 static DEVICE_ATTR(mode
, 0644, mode_show
, mode_store
);
300 static DEVICE_ATTR(passive
, S_IRUGO
| S_IWUSR
, passive_show
, \
303 static struct device_attribute trip_point_attrs
[] = {
304 __ATTR(trip_point_0_type
, 0444, trip_point_type_show
, NULL
),
305 __ATTR(trip_point_0_temp
, 0444, trip_point_temp_show
, NULL
),
306 __ATTR(trip_point_1_type
, 0444, trip_point_type_show
, NULL
),
307 __ATTR(trip_point_1_temp
, 0444, trip_point_temp_show
, NULL
),
308 __ATTR(trip_point_2_type
, 0444, trip_point_type_show
, NULL
),
309 __ATTR(trip_point_2_temp
, 0444, trip_point_temp_show
, NULL
),
310 __ATTR(trip_point_3_type
, 0444, trip_point_type_show
, NULL
),
311 __ATTR(trip_point_3_temp
, 0444, trip_point_temp_show
, NULL
),
312 __ATTR(trip_point_4_type
, 0444, trip_point_type_show
, NULL
),
313 __ATTR(trip_point_4_temp
, 0444, trip_point_temp_show
, NULL
),
314 __ATTR(trip_point_5_type
, 0444, trip_point_type_show
, NULL
),
315 __ATTR(trip_point_5_temp
, 0444, trip_point_temp_show
, NULL
),
316 __ATTR(trip_point_6_type
, 0444, trip_point_type_show
, NULL
),
317 __ATTR(trip_point_6_temp
, 0444, trip_point_temp_show
, NULL
),
318 __ATTR(trip_point_7_type
, 0444, trip_point_type_show
, NULL
),
319 __ATTR(trip_point_7_temp
, 0444, trip_point_temp_show
, NULL
),
320 __ATTR(trip_point_8_type
, 0444, trip_point_type_show
, NULL
),
321 __ATTR(trip_point_8_temp
, 0444, trip_point_temp_show
, NULL
),
322 __ATTR(trip_point_9_type
, 0444, trip_point_type_show
, NULL
),
323 __ATTR(trip_point_9_temp
, 0444, trip_point_temp_show
, NULL
),
324 __ATTR(trip_point_10_type
, 0444, trip_point_type_show
, NULL
),
325 __ATTR(trip_point_10_temp
, 0444, trip_point_temp_show
, NULL
),
326 __ATTR(trip_point_11_type
, 0444, trip_point_type_show
, NULL
),
327 __ATTR(trip_point_11_temp
, 0444, trip_point_temp_show
, NULL
),
330 #define TRIP_POINT_ATTR_ADD(_dev, _index, result) \
332 result = device_create_file(_dev, \
333 &trip_point_attrs[_index * 2]); \
336 result = device_create_file(_dev, \
337 &trip_point_attrs[_index * 2 + 1]); \
340 #define TRIP_POINT_ATTR_REMOVE(_dev, _index) \
342 device_remove_file(_dev, &trip_point_attrs[_index * 2]); \
343 device_remove_file(_dev, &trip_point_attrs[_index * 2 + 1]); \
346 /* sys I/F for cooling device */
347 #define to_cooling_device(_dev) \
348 container_of(_dev, struct thermal_cooling_device, device)
351 thermal_cooling_device_type_show(struct device
*dev
,
352 struct device_attribute
*attr
, char *buf
)
354 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
356 return sprintf(buf
, "%s\n", cdev
->type
);
360 thermal_cooling_device_max_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_max_state(cdev
, &state
);
370 return sprintf(buf
, "%ld\n", state
);
374 thermal_cooling_device_cur_state_show(struct device
*dev
,
375 struct device_attribute
*attr
, char *buf
)
377 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
381 ret
= cdev
->ops
->get_cur_state(cdev
, &state
);
384 return sprintf(buf
, "%ld\n", state
);
388 thermal_cooling_device_cur_state_store(struct device
*dev
,
389 struct device_attribute
*attr
,
390 const char *buf
, size_t count
)
392 struct thermal_cooling_device
*cdev
= to_cooling_device(dev
);
396 if (!sscanf(buf
, "%ld\n", &state
))
402 result
= cdev
->ops
->set_cur_state(cdev
, state
);
408 static struct device_attribute dev_attr_cdev_type
=
409 __ATTR(type
, 0444, thermal_cooling_device_type_show
, NULL
);
410 static DEVICE_ATTR(max_state
, 0444,
411 thermal_cooling_device_max_state_show
, NULL
);
412 static DEVICE_ATTR(cur_state
, 0644,
413 thermal_cooling_device_cur_state_show
,
414 thermal_cooling_device_cur_state_store
);
417 thermal_cooling_device_trip_point_show(struct device
*dev
,
418 struct device_attribute
*attr
, char *buf
)
420 struct thermal_cooling_device_instance
*instance
;
423 container_of(attr
, struct thermal_cooling_device_instance
, attr
);
425 if (instance
->trip
== THERMAL_TRIPS_NONE
)
426 return sprintf(buf
, "-1\n");
428 return sprintf(buf
, "%d\n", instance
->trip
);
431 /* Device management */
433 #if defined(CONFIG_THERMAL_HWMON)
436 #include <linux/hwmon.h>
437 static LIST_HEAD(thermal_hwmon_list
);
440 name_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
442 struct thermal_hwmon_device
*hwmon
= dev_get_drvdata(dev
);
443 return sprintf(buf
, "%s\n", hwmon
->type
);
445 static DEVICE_ATTR(name
, 0444, name_show
, NULL
);
448 temp_input_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
452 struct thermal_hwmon_attr
*hwmon_attr
453 = container_of(attr
, struct thermal_hwmon_attr
, attr
);
454 struct thermal_zone_device
*tz
455 = container_of(hwmon_attr
, struct thermal_zone_device
,
458 ret
= tz
->ops
->get_temp(tz
, &temperature
);
463 return sprintf(buf
, "%ld\n", temperature
);
467 temp_crit_show(struct device
*dev
, struct device_attribute
*attr
,
470 struct thermal_hwmon_attr
*hwmon_attr
471 = container_of(attr
, struct thermal_hwmon_attr
, attr
);
472 struct thermal_zone_device
*tz
473 = container_of(hwmon_attr
, struct thermal_zone_device
,
478 ret
= tz
->ops
->get_trip_temp(tz
, 0, &temperature
);
482 return sprintf(buf
, "%ld\n", temperature
);
487 thermal_add_hwmon_sysfs(struct thermal_zone_device
*tz
)
489 struct thermal_hwmon_device
*hwmon
;
490 int new_hwmon_device
= 1;
493 mutex_lock(&thermal_list_lock
);
494 list_for_each_entry(hwmon
, &thermal_hwmon_list
, node
)
495 if (!strcmp(hwmon
->type
, tz
->type
)) {
496 new_hwmon_device
= 0;
497 mutex_unlock(&thermal_list_lock
);
498 goto register_sys_interface
;
500 mutex_unlock(&thermal_list_lock
);
502 hwmon
= kzalloc(sizeof(struct thermal_hwmon_device
), GFP_KERNEL
);
506 INIT_LIST_HEAD(&hwmon
->tz_list
);
507 strlcpy(hwmon
->type
, tz
->type
, THERMAL_NAME_LENGTH
);
508 hwmon
->device
= hwmon_device_register(NULL
);
509 if (IS_ERR(hwmon
->device
)) {
510 result
= PTR_ERR(hwmon
->device
);
513 dev_set_drvdata(hwmon
->device
, hwmon
);
514 result
= device_create_file(hwmon
->device
, &dev_attr_name
);
516 goto unregister_hwmon_device
;
518 register_sys_interface
:
522 snprintf(tz
->temp_input
.name
, THERMAL_NAME_LENGTH
,
523 "temp%d_input", hwmon
->count
);
524 tz
->temp_input
.attr
.attr
.name
= tz
->temp_input
.name
;
525 tz
->temp_input
.attr
.attr
.mode
= 0444;
526 tz
->temp_input
.attr
.show
= temp_input_show
;
527 sysfs_attr_init(&tz
->temp_input
.attr
.attr
);
528 result
= device_create_file(hwmon
->device
, &tz
->temp_input
.attr
);
530 goto unregister_hwmon_device
;
532 if (tz
->ops
->get_crit_temp
) {
533 unsigned long temperature
;
534 if (!tz
->ops
->get_crit_temp(tz
, &temperature
)) {
535 snprintf(tz
->temp_crit
.name
, THERMAL_NAME_LENGTH
,
536 "temp%d_crit", hwmon
->count
);
537 tz
->temp_crit
.attr
.attr
.name
= tz
->temp_crit
.name
;
538 tz
->temp_crit
.attr
.attr
.mode
= 0444;
539 tz
->temp_crit
.attr
.show
= temp_crit_show
;
540 sysfs_attr_init(&tz
->temp_crit
.attr
.attr
);
541 result
= device_create_file(hwmon
->device
,
542 &tz
->temp_crit
.attr
);
544 goto unregister_hwmon_device
;
548 mutex_lock(&thermal_list_lock
);
549 if (new_hwmon_device
)
550 list_add_tail(&hwmon
->node
, &thermal_hwmon_list
);
551 list_add_tail(&tz
->hwmon_node
, &hwmon
->tz_list
);
552 mutex_unlock(&thermal_list_lock
);
556 unregister_hwmon_device
:
557 device_remove_file(hwmon
->device
, &tz
->temp_crit
.attr
);
558 device_remove_file(hwmon
->device
, &tz
->temp_input
.attr
);
559 if (new_hwmon_device
) {
560 device_remove_file(hwmon
->device
, &dev_attr_name
);
561 hwmon_device_unregister(hwmon
->device
);
564 if (new_hwmon_device
)
571 thermal_remove_hwmon_sysfs(struct thermal_zone_device
*tz
)
573 struct thermal_hwmon_device
*hwmon
= tz
->hwmon
;
576 device_remove_file(hwmon
->device
, &tz
->temp_input
.attr
);
577 device_remove_file(hwmon
->device
, &tz
->temp_crit
.attr
);
579 mutex_lock(&thermal_list_lock
);
580 list_del(&tz
->hwmon_node
);
581 if (!list_empty(&hwmon
->tz_list
)) {
582 mutex_unlock(&thermal_list_lock
);
585 list_del(&hwmon
->node
);
586 mutex_unlock(&thermal_list_lock
);
588 device_remove_file(hwmon
->device
, &dev_attr_name
);
589 hwmon_device_unregister(hwmon
->device
);
594 thermal_add_hwmon_sysfs(struct thermal_zone_device
*tz
)
600 thermal_remove_hwmon_sysfs(struct thermal_zone_device
*tz
)
605 static void thermal_zone_device_set_polling(struct thermal_zone_device
*tz
,
608 cancel_delayed_work(&(tz
->poll_queue
));
614 schedule_delayed_work(&(tz
->poll_queue
),
615 round_jiffies(msecs_to_jiffies(delay
)));
617 schedule_delayed_work(&(tz
->poll_queue
),
618 msecs_to_jiffies(delay
));
621 static void thermal_zone_device_passive(struct thermal_zone_device
*tz
,
622 int temp
, int trip_temp
, int trip
)
625 struct thermal_cooling_device_instance
*instance
;
626 struct thermal_cooling_device
*cdev
;
627 long state
, max_state
;
632 * Calculate the thermal trend (using the passive cooling equation)
633 * and modify the performance limit for all passive cooling devices
634 * accordingly. Note that we assume symmetry.
636 if (temp
>= trip_temp
) {
639 trend
= (tz
->tc1
* (temp
- tz
->last_temperature
)) +
640 (tz
->tc2
* (temp
- trip_temp
));
644 list_for_each_entry(instance
, &tz
->cooling_devices
,
646 if (instance
->trip
!= trip
)
648 cdev
= instance
->cdev
;
649 cdev
->ops
->get_cur_state(cdev
, &state
);
650 cdev
->ops
->get_max_state(cdev
, &max_state
);
651 if (state
++ < max_state
)
652 cdev
->ops
->set_cur_state(cdev
, state
);
654 } else if (trend
< 0) { /* Cooling off? */
655 list_for_each_entry(instance
, &tz
->cooling_devices
,
657 if (instance
->trip
!= trip
)
659 cdev
= instance
->cdev
;
660 cdev
->ops
->get_cur_state(cdev
, &state
);
661 cdev
->ops
->get_max_state(cdev
, &max_state
);
663 cdev
->ops
->set_cur_state(cdev
, --state
);
672 * Implement passive cooling hysteresis to slowly increase performance
673 * and avoid thrashing around the passive trip point. Note that we
676 list_for_each_entry(instance
, &tz
->cooling_devices
, node
) {
677 if (instance
->trip
!= trip
)
679 cdev
= instance
->cdev
;
680 cdev
->ops
->get_cur_state(cdev
, &state
);
681 cdev
->ops
->get_max_state(cdev
, &max_state
);
683 cdev
->ops
->set_cur_state(cdev
, --state
);
689 static void thermal_zone_device_check(struct work_struct
*work
)
691 struct thermal_zone_device
*tz
= container_of(work
, struct
694 thermal_zone_device_update(tz
);
698 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
699 * @tz: thermal zone device
700 * @trip: indicates which trip point the cooling devices is
701 * associated with in this thermal zone.
702 * @cdev: thermal cooling device
704 * This function is usually called in the thermal zone device .bind callback.
706 int thermal_zone_bind_cooling_device(struct thermal_zone_device
*tz
,
708 struct thermal_cooling_device
*cdev
)
710 struct thermal_cooling_device_instance
*dev
;
711 struct thermal_cooling_device_instance
*pos
;
712 struct thermal_zone_device
*pos1
;
713 struct thermal_cooling_device
*pos2
;
716 if (trip
>= tz
->trips
|| (trip
< 0 && trip
!= THERMAL_TRIPS_NONE
))
719 list_for_each_entry(pos1
, &thermal_tz_list
, node
) {
723 list_for_each_entry(pos2
, &thermal_cdev_list
, node
) {
728 if (tz
!= pos1
|| cdev
!= pos2
)
732 kzalloc(sizeof(struct thermal_cooling_device_instance
), GFP_KERNEL
);
738 result
= get_idr(&tz
->idr
, &tz
->lock
, &dev
->id
);
742 sprintf(dev
->name
, "cdev%d", dev
->id
);
744 sysfs_create_link(&tz
->device
.kobj
, &cdev
->device
.kobj
, dev
->name
);
748 sprintf(dev
->attr_name
, "cdev%d_trip_point", dev
->id
);
749 sysfs_attr_init(&dev
->attr
.attr
);
750 dev
->attr
.attr
.name
= dev
->attr_name
;
751 dev
->attr
.attr
.mode
= 0444;
752 dev
->attr
.show
= thermal_cooling_device_trip_point_show
;
753 result
= device_create_file(&tz
->device
, &dev
->attr
);
755 goto remove_symbol_link
;
757 mutex_lock(&tz
->lock
);
758 list_for_each_entry(pos
, &tz
->cooling_devices
, node
)
759 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
764 list_add_tail(&dev
->node
, &tz
->cooling_devices
);
765 mutex_unlock(&tz
->lock
);
770 device_remove_file(&tz
->device
, &dev
->attr
);
772 sysfs_remove_link(&tz
->device
.kobj
, dev
->name
);
774 release_idr(&tz
->idr
, &tz
->lock
, dev
->id
);
780 EXPORT_SYMBOL(thermal_zone_bind_cooling_device
);
783 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
784 * @tz: thermal zone device
785 * @trip: indicates which trip point the cooling devices is
786 * associated with in this thermal zone.
787 * @cdev: thermal cooling device
789 * This function is usually called in the thermal zone device .unbind callback.
791 int thermal_zone_unbind_cooling_device(struct thermal_zone_device
*tz
,
793 struct thermal_cooling_device
*cdev
)
795 struct thermal_cooling_device_instance
*pos
, *next
;
797 mutex_lock(&tz
->lock
);
798 list_for_each_entry_safe(pos
, next
, &tz
->cooling_devices
, node
) {
799 if (pos
->tz
== tz
&& pos
->trip
== trip
&& pos
->cdev
== cdev
) {
800 list_del(&pos
->node
);
801 mutex_unlock(&tz
->lock
);
805 mutex_unlock(&tz
->lock
);
810 device_remove_file(&tz
->device
, &pos
->attr
);
811 sysfs_remove_link(&tz
->device
.kobj
, pos
->name
);
812 release_idr(&tz
->idr
, &tz
->lock
, pos
->id
);
817 EXPORT_SYMBOL(thermal_zone_unbind_cooling_device
);
819 static void thermal_release(struct device
*dev
)
821 struct thermal_zone_device
*tz
;
822 struct thermal_cooling_device
*cdev
;
824 if (!strncmp(dev_name(dev
), "thermal_zone", sizeof "thermal_zone" - 1)) {
825 tz
= to_thermal_zone(dev
);
828 cdev
= to_cooling_device(dev
);
833 static struct class thermal_class
= {
835 .dev_release
= thermal_release
,
839 * thermal_cooling_device_register - register a new thermal cooling device
840 * @type: the thermal cooling device type.
841 * @devdata: device private data.
842 * @ops: standard thermal cooling devices callbacks.
844 struct thermal_cooling_device
*thermal_cooling_device_register(
845 char *type
, void *devdata
, const struct thermal_cooling_device_ops
*ops
)
847 struct thermal_cooling_device
*cdev
;
848 struct thermal_zone_device
*pos
;
851 if (strlen(type
) >= THERMAL_NAME_LENGTH
)
852 return ERR_PTR(-EINVAL
);
854 if (!ops
|| !ops
->get_max_state
|| !ops
->get_cur_state
||
856 return ERR_PTR(-EINVAL
);
858 cdev
= kzalloc(sizeof(struct thermal_cooling_device
), GFP_KERNEL
);
860 return ERR_PTR(-ENOMEM
);
862 result
= get_idr(&thermal_cdev_idr
, &thermal_idr_lock
, &cdev
->id
);
865 return ERR_PTR(result
);
868 strcpy(cdev
->type
, type
);
870 cdev
->device
.class = &thermal_class
;
871 cdev
->devdata
= devdata
;
872 dev_set_name(&cdev
->device
, "cooling_device%d", cdev
->id
);
873 result
= device_register(&cdev
->device
);
875 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
877 return ERR_PTR(result
);
882 result
= device_create_file(&cdev
->device
, &dev_attr_cdev_type
);
887 result
= device_create_file(&cdev
->device
, &dev_attr_max_state
);
891 result
= device_create_file(&cdev
->device
, &dev_attr_cur_state
);
895 mutex_lock(&thermal_list_lock
);
896 list_add(&cdev
->node
, &thermal_cdev_list
);
897 list_for_each_entry(pos
, &thermal_tz_list
, node
) {
900 result
= pos
->ops
->bind(pos
, cdev
);
905 mutex_unlock(&thermal_list_lock
);
911 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
912 device_unregister(&cdev
->device
);
913 return ERR_PTR(result
);
916 EXPORT_SYMBOL(thermal_cooling_device_register
);
919 * thermal_cooling_device_unregister - removes the registered thermal cooling device
920 * @cdev: the thermal cooling device to remove.
922 * thermal_cooling_device_unregister() must be called when the device is no
925 void thermal_cooling_device_unregister(struct
926 thermal_cooling_device
929 struct thermal_zone_device
*tz
;
930 struct thermal_cooling_device
*pos
= NULL
;
935 mutex_lock(&thermal_list_lock
);
936 list_for_each_entry(pos
, &thermal_cdev_list
, node
)
940 /* thermal cooling device not found */
941 mutex_unlock(&thermal_list_lock
);
944 list_del(&cdev
->node
);
945 list_for_each_entry(tz
, &thermal_tz_list
, node
) {
946 if (!tz
->ops
->unbind
)
948 tz
->ops
->unbind(tz
, cdev
);
950 mutex_unlock(&thermal_list_lock
);
952 device_remove_file(&cdev
->device
, &dev_attr_cdev_type
);
953 device_remove_file(&cdev
->device
, &dev_attr_max_state
);
954 device_remove_file(&cdev
->device
, &dev_attr_cur_state
);
956 release_idr(&thermal_cdev_idr
, &thermal_idr_lock
, cdev
->id
);
957 device_unregister(&cdev
->device
);
961 EXPORT_SYMBOL(thermal_cooling_device_unregister
);
964 * thermal_zone_device_update - force an update of a thermal zone's state
965 * @ttz: the thermal zone to update
968 void thermal_zone_device_update(struct thermal_zone_device
*tz
)
971 long temp
, trip_temp
;
972 enum thermal_trip_type trip_type
;
973 struct thermal_cooling_device_instance
*instance
;
974 struct thermal_cooling_device
*cdev
;
976 mutex_lock(&tz
->lock
);
978 if (tz
->ops
->get_temp(tz
, &temp
)) {
979 /* get_temp failed - retry it later */
980 printk(KERN_WARNING PREFIX
"failed to read out thermal zone "
985 for (count
= 0; count
< tz
->trips
; count
++) {
986 tz
->ops
->get_trip_type(tz
, count
, &trip_type
);
987 tz
->ops
->get_trip_temp(tz
, count
, &trip_temp
);
990 case THERMAL_TRIP_CRITICAL
:
991 if (temp
>= trip_temp
) {
993 ret
= tz
->ops
->notify(tz
, count
,
997 "Critical temperature reached (%ld C), shutting down.\n",
999 orderly_poweroff(true);
1003 case THERMAL_TRIP_HOT
:
1004 if (temp
>= trip_temp
)
1005 if (tz
->ops
->notify
)
1006 tz
->ops
->notify(tz
, count
, trip_type
);
1008 case THERMAL_TRIP_ACTIVE
:
1009 list_for_each_entry(instance
, &tz
->cooling_devices
,
1011 if (instance
->trip
!= count
)
1014 cdev
= instance
->cdev
;
1016 if (temp
>= trip_temp
)
1017 cdev
->ops
->set_cur_state(cdev
, 1);
1019 cdev
->ops
->set_cur_state(cdev
, 0);
1022 case THERMAL_TRIP_PASSIVE
:
1023 if (temp
>= trip_temp
|| tz
->passive
)
1024 thermal_zone_device_passive(tz
, temp
,
1030 if (tz
->forced_passive
)
1031 thermal_zone_device_passive(tz
, temp
, tz
->forced_passive
,
1032 THERMAL_TRIPS_NONE
);
1034 tz
->last_temperature
= temp
;
1038 thermal_zone_device_set_polling(tz
, tz
->passive_delay
);
1039 else if (tz
->polling_delay
)
1040 thermal_zone_device_set_polling(tz
, tz
->polling_delay
);
1042 thermal_zone_device_set_polling(tz
, 0);
1043 mutex_unlock(&tz
->lock
);
1045 EXPORT_SYMBOL(thermal_zone_device_update
);
1048 * thermal_zone_device_register - register a new thermal zone device
1049 * @type: the thermal zone device type
1050 * @trips: the number of trip points the thermal zone support
1051 * @devdata: private device data
1052 * @ops: standard thermal zone device callbacks
1053 * @tc1: thermal coefficient 1 for passive calculations
1054 * @tc2: thermal coefficient 2 for passive calculations
1055 * @passive_delay: number of milliseconds to wait between polls when
1056 * performing passive cooling
1057 * @polling_delay: number of milliseconds to wait between polls when checking
1058 * whether trip points have been crossed (0 for interrupt
1061 * thermal_zone_device_unregister() must be called when the device is no
1062 * longer needed. The passive cooling formula uses tc1 and tc2 as described in
1063 * section 11.1.5.1 of the ACPI specification 3.0.
1065 struct thermal_zone_device
*thermal_zone_device_register(char *type
,
1066 int trips
, void *devdata
,
1067 const struct thermal_zone_device_ops
*ops
,
1068 int tc1
, int tc2
, int passive_delay
, int polling_delay
)
1070 struct thermal_zone_device
*tz
;
1071 struct thermal_cooling_device
*pos
;
1072 enum thermal_trip_type trip_type
;
1077 if (strlen(type
) >= THERMAL_NAME_LENGTH
)
1078 return ERR_PTR(-EINVAL
);
1080 if (trips
> THERMAL_MAX_TRIPS
|| trips
< 0)
1081 return ERR_PTR(-EINVAL
);
1083 if (!ops
|| !ops
->get_temp
)
1084 return ERR_PTR(-EINVAL
);
1086 tz
= kzalloc(sizeof(struct thermal_zone_device
), GFP_KERNEL
);
1088 return ERR_PTR(-ENOMEM
);
1090 INIT_LIST_HEAD(&tz
->cooling_devices
);
1092 mutex_init(&tz
->lock
);
1093 result
= get_idr(&thermal_tz_idr
, &thermal_idr_lock
, &tz
->id
);
1096 return ERR_PTR(result
);
1099 strcpy(tz
->type
, type
);
1101 tz
->device
.class = &thermal_class
;
1102 tz
->devdata
= devdata
;
1106 tz
->passive_delay
= passive_delay
;
1107 tz
->polling_delay
= polling_delay
;
1109 dev_set_name(&tz
->device
, "thermal_zone%d", tz
->id
);
1110 result
= device_register(&tz
->device
);
1112 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
1114 return ERR_PTR(result
);
1119 result
= device_create_file(&tz
->device
, &dev_attr_type
);
1124 result
= device_create_file(&tz
->device
, &dev_attr_temp
);
1128 if (ops
->get_mode
) {
1129 result
= device_create_file(&tz
->device
, &dev_attr_mode
);
1134 for (count
= 0; count
< trips
; count
++) {
1135 TRIP_POINT_ATTR_ADD(&tz
->device
, count
, result
);
1138 tz
->ops
->get_trip_type(tz
, count
, &trip_type
);
1139 if (trip_type
== THERMAL_TRIP_PASSIVE
)
1144 result
= device_create_file(&tz
->device
,
1150 result
= thermal_add_hwmon_sysfs(tz
);
1154 mutex_lock(&thermal_list_lock
);
1155 list_add_tail(&tz
->node
, &thermal_tz_list
);
1157 list_for_each_entry(pos
, &thermal_cdev_list
, node
) {
1158 result
= ops
->bind(tz
, pos
);
1162 mutex_unlock(&thermal_list_lock
);
1164 INIT_DELAYED_WORK(&(tz
->poll_queue
), thermal_zone_device_check
);
1166 thermal_zone_device_update(tz
);
1172 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
1173 device_unregister(&tz
->device
);
1174 return ERR_PTR(result
);
1177 EXPORT_SYMBOL(thermal_zone_device_register
);
1180 * thermal_device_unregister - removes the registered thermal zone device
1181 * @tz: the thermal zone device to remove
1183 void thermal_zone_device_unregister(struct thermal_zone_device
*tz
)
1185 struct thermal_cooling_device
*cdev
;
1186 struct thermal_zone_device
*pos
= NULL
;
1192 mutex_lock(&thermal_list_lock
);
1193 list_for_each_entry(pos
, &thermal_tz_list
, node
)
1197 /* thermal zone device not found */
1198 mutex_unlock(&thermal_list_lock
);
1201 list_del(&tz
->node
);
1202 if (tz
->ops
->unbind
)
1203 list_for_each_entry(cdev
, &thermal_cdev_list
, node
)
1204 tz
->ops
->unbind(tz
, cdev
);
1205 mutex_unlock(&thermal_list_lock
);
1207 thermal_zone_device_set_polling(tz
, 0);
1210 device_remove_file(&tz
->device
, &dev_attr_type
);
1211 device_remove_file(&tz
->device
, &dev_attr_temp
);
1212 if (tz
->ops
->get_mode
)
1213 device_remove_file(&tz
->device
, &dev_attr_mode
);
1215 for (count
= 0; count
< tz
->trips
; count
++)
1216 TRIP_POINT_ATTR_REMOVE(&tz
->device
, count
);
1218 thermal_remove_hwmon_sysfs(tz
);
1219 release_idr(&thermal_tz_idr
, &thermal_idr_lock
, tz
->id
);
1220 idr_destroy(&tz
->idr
);
1221 mutex_destroy(&tz
->lock
);
1222 device_unregister(&tz
->device
);
1226 EXPORT_SYMBOL(thermal_zone_device_unregister
);
1228 int generate_netlink_event(u32 orig
, enum events event
)
1230 struct sk_buff
*skb
;
1231 struct nlattr
*attr
;
1232 struct thermal_genl_event
*thermal_event
;
1237 /* allocate memory */
1238 size
= nla_total_size(sizeof(struct thermal_genl_event
)) + \
1241 skb
= genlmsg_new(size
, GFP_ATOMIC
);
1245 /* add the genetlink message header */
1246 msg_header
= genlmsg_put(skb
, 0, thermal_event_seqnum
++,
1247 &thermal_event_genl_family
, 0,
1248 THERMAL_GENL_CMD_EVENT
);
1255 attr
= nla_reserve(skb
, THERMAL_GENL_ATTR_EVENT
, \
1256 sizeof(struct thermal_genl_event
));
1263 thermal_event
= nla_data(attr
);
1264 if (!thermal_event
) {
1269 memset(thermal_event
, 0, sizeof(struct thermal_genl_event
));
1271 thermal_event
->orig
= orig
;
1272 thermal_event
->event
= event
;
1274 /* send multicast genetlink message */
1275 result
= genlmsg_end(skb
, msg_header
);
1281 result
= genlmsg_multicast(skb
, 0, thermal_event_mcgrp
.id
, GFP_ATOMIC
);
1283 printk(KERN_INFO
"failed to send netlink event:%d", result
);
1287 EXPORT_SYMBOL(generate_netlink_event
);
1289 static int genetlink_init(void)
1293 result
= genl_register_family(&thermal_event_genl_family
);
1297 result
= genl_register_mc_group(&thermal_event_genl_family
,
1298 &thermal_event_mcgrp
);
1300 genl_unregister_family(&thermal_event_genl_family
);
1304 static int __init
thermal_init(void)
1308 result
= class_register(&thermal_class
);
1310 idr_destroy(&thermal_tz_idr
);
1311 idr_destroy(&thermal_cdev_idr
);
1312 mutex_destroy(&thermal_idr_lock
);
1313 mutex_destroy(&thermal_list_lock
);
1315 result
= genetlink_init();
1319 static void genetlink_exit(void)
1321 genl_unregister_family(&thermal_event_genl_family
);
1324 static void __exit
thermal_exit(void)
1326 class_unregister(&thermal_class
);
1327 idr_destroy(&thermal_tz_idr
);
1328 idr_destroy(&thermal_cdev_idr
);
1329 mutex_destroy(&thermal_idr_lock
);
1330 mutex_destroy(&thermal_list_lock
);
1334 fs_initcall(thermal_init
);
1335 module_exit(thermal_exit
);