2 * acpi_thermal.c - ACPI Thermal Zone Driver ($Revision: 41 $)
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 * This driver fully implements the ACPI thermal policy as described in the
26 * ACPI 2.0 Specification.
28 * TBD: 1. Implement passive cooling hysteresis.
29 * 2. Enhance passive cooling (CPU) states/limit interface to support
30 * concepts of 'multiple limiters', upper/lower limits, etc.
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/dmi.h>
37 #include <linux/init.h>
38 #include <linux/slab.h>
39 #include <linux/types.h>
40 #include <linux/proc_fs.h>
41 #include <linux/jiffies.h>
42 #include <linux/kmod.h>
43 #include <linux/seq_file.h>
44 #include <linux/reboot.h>
45 #include <linux/device.h>
46 #include <asm/uaccess.h>
47 #include <linux/thermal.h>
48 #include <acpi/acpi_bus.h>
49 #include <acpi/acpi_drivers.h>
51 #define PREFIX "ACPI: "
53 #define ACPI_THERMAL_CLASS "thermal_zone"
54 #define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
55 #define ACPI_THERMAL_FILE_STATE "state"
56 #define ACPI_THERMAL_FILE_TEMPERATURE "temperature"
57 #define ACPI_THERMAL_FILE_TRIP_POINTS "trip_points"
58 #define ACPI_THERMAL_FILE_COOLING_MODE "cooling_mode"
59 #define ACPI_THERMAL_FILE_POLLING_FREQ "polling_frequency"
60 #define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
61 #define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
62 #define ACPI_THERMAL_NOTIFY_DEVICES 0x82
63 #define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
64 #define ACPI_THERMAL_NOTIFY_HOT 0xF1
65 #define ACPI_THERMAL_MODE_ACTIVE 0x00
67 #define ACPI_THERMAL_MAX_ACTIVE 10
68 #define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
70 #define _COMPONENT ACPI_THERMAL_COMPONENT
71 ACPI_MODULE_NAME("thermal");
73 MODULE_AUTHOR("Paul Diefenbaugh");
74 MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
75 MODULE_LICENSE("GPL");
78 module_param(act, int, 0644);
79 MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.");
82 module_param(crt, int, 0644);
83 MODULE_PARM_DESC(crt, "Disable or lower all critical trip points.");
86 module_param(tzp, int, 0444);
87 MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.");
90 module_param(nocrt, int, 0);
91 MODULE_PARM_DESC(nocrt, "Set to take no action upon ACPI thermal zone critical trips points.");
94 module_param(off, int, 0);
95 MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
98 module_param(psv, int, 0644);
99 MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
101 static int acpi_thermal_add(struct acpi_device *device);
102 static int acpi_thermal_remove(struct acpi_device *device, int type);
103 static int acpi_thermal_resume(struct acpi_device *device);
104 static void acpi_thermal_notify(struct acpi_device *device, u32 event);
105 static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
106 static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
107 static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
108 static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
109 static ssize_t acpi_thermal_write_cooling_mode(struct file *,
110 const char __user *, size_t,
112 static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
113 static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
116 static const struct acpi_device_id thermal_device_ids[] = {
117 {ACPI_THERMAL_HID, 0},
120 MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
122 static struct acpi_driver acpi_thermal_driver = {
124 .class = ACPI_THERMAL_CLASS,
125 .ids = thermal_device_ids,
127 .add = acpi_thermal_add,
128 .remove = acpi_thermal_remove,
129 .resume = acpi_thermal_resume,
130 .notify = acpi_thermal_notify,
134 struct acpi_thermal_state {
143 struct acpi_thermal_state_flags {
149 struct acpi_thermal_critical {
150 struct acpi_thermal_state_flags flags;
151 unsigned long temperature;
154 struct acpi_thermal_hot {
155 struct acpi_thermal_state_flags flags;
156 unsigned long temperature;
159 struct acpi_thermal_passive {
160 struct acpi_thermal_state_flags flags;
161 unsigned long temperature;
165 struct acpi_handle_list devices;
168 struct acpi_thermal_active {
169 struct acpi_thermal_state_flags flags;
170 unsigned long temperature;
171 struct acpi_handle_list devices;
174 struct acpi_thermal_trips {
175 struct acpi_thermal_critical critical;
176 struct acpi_thermal_hot hot;
177 struct acpi_thermal_passive passive;
178 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
181 struct acpi_thermal_flags {
182 u8 cooling_mode:1; /* _SCP */
183 u8 devices:1; /* _TZD */
187 struct acpi_thermal {
188 struct acpi_device * device;
190 unsigned long temperature;
191 unsigned long last_temperature;
192 unsigned long polling_frequency;
194 struct acpi_thermal_flags flags;
195 struct acpi_thermal_state state;
196 struct acpi_thermal_trips trips;
197 struct acpi_handle_list devices;
198 struct thermal_zone_device *thermal_zone;
204 static const struct file_operations acpi_thermal_state_fops = {
205 .owner = THIS_MODULE,
206 .open = acpi_thermal_state_open_fs,
209 .release = single_release,
212 static const struct file_operations acpi_thermal_temp_fops = {
213 .owner = THIS_MODULE,
214 .open = acpi_thermal_temp_open_fs,
217 .release = single_release,
220 static const struct file_operations acpi_thermal_trip_fops = {
221 .owner = THIS_MODULE,
222 .open = acpi_thermal_trip_open_fs,
225 .release = single_release,
228 static const struct file_operations acpi_thermal_cooling_fops = {
229 .owner = THIS_MODULE,
230 .open = acpi_thermal_cooling_open_fs,
232 .write = acpi_thermal_write_cooling_mode,
234 .release = single_release,
237 static const struct file_operations acpi_thermal_polling_fops = {
238 .owner = THIS_MODULE,
239 .open = acpi_thermal_polling_open_fs,
241 .write = acpi_thermal_write_polling,
243 .release = single_release,
246 /* --------------------------------------------------------------------------
247 Thermal Zone Management
248 -------------------------------------------------------------------------- */
250 static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
252 acpi_status status = AE_OK;
253 unsigned long long tmp;
258 tz->last_temperature = tz->temperature;
260 status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp);
261 if (ACPI_FAILURE(status))
264 tz->temperature = tmp;
265 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
271 static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
273 acpi_status status = AE_OK;
274 unsigned long long tmp;
279 status = acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, &tmp);
280 if (ACPI_FAILURE(status))
283 tz->polling_frequency = tmp;
284 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
285 tz->polling_frequency));
290 static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
296 tz->polling_frequency = seconds * 10; /* Convert value to deci-seconds */
298 tz->thermal_zone->polling_delay = seconds * 1000;
301 thermal_zone_device_update(tz->thermal_zone);
303 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
304 "Polling frequency set to %lu seconds\n",
305 tz->polling_frequency/10));
310 static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
312 acpi_status status = AE_OK;
313 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
314 struct acpi_object_list arg_list = { 1, &arg0 };
315 acpi_handle handle = NULL;
321 status = acpi_get_handle(tz->device->handle, "_SCP", &handle);
322 if (ACPI_FAILURE(status)) {
323 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
327 arg0.integer.value = mode;
329 status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
330 if (ACPI_FAILURE(status))
336 #define ACPI_TRIPS_CRITICAL 0x01
337 #define ACPI_TRIPS_HOT 0x02
338 #define ACPI_TRIPS_PASSIVE 0x04
339 #define ACPI_TRIPS_ACTIVE 0x08
340 #define ACPI_TRIPS_DEVICES 0x10
342 #define ACPI_TRIPS_REFRESH_THRESHOLDS (ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE)
343 #define ACPI_TRIPS_REFRESH_DEVICES ACPI_TRIPS_DEVICES
345 #define ACPI_TRIPS_INIT (ACPI_TRIPS_CRITICAL | ACPI_TRIPS_HOT | \
346 ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE | \
350 * This exception is thrown out in two cases:
351 * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
352 * when re-evaluating the AML code.
353 * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change.
354 * We need to re-bind the cooling devices of a thermal zone when this occurs.
356 #define ACPI_THERMAL_TRIPS_EXCEPTION(flags, str) \
358 if (flags != ACPI_TRIPS_INIT) \
359 ACPI_EXCEPTION((AE_INFO, AE_ERROR, \
360 "ACPI thermal trip point %s changed\n" \
361 "Please send acpidump to linux-acpi@vger.kernel.org\n", str)); \
364 static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
366 acpi_status status = AE_OK;
367 unsigned long long tmp;
368 struct acpi_handle_list devices;
372 /* Critical Shutdown */
373 if (flag & ACPI_TRIPS_CRITICAL) {
374 status = acpi_evaluate_integer(tz->device->handle,
376 tz->trips.critical.temperature = tmp;
378 * Treat freezing temperatures as invalid as well; some
379 * BIOSes return really low values and cause reboots at startup.
380 * Below zero (Celsius) values clearly aren't right for sure..
381 * ... so lets discard those as invalid.
383 if (ACPI_FAILURE(status)) {
384 tz->trips.critical.flags.valid = 0;
385 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
386 "No critical threshold\n"));
387 } else if (tmp <= 2732) {
388 printk(KERN_WARNING FW_BUG "Invalid critical threshold "
390 tz->trips.critical.flags.valid = 0;
392 tz->trips.critical.flags.valid = 1;
393 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
394 "Found critical threshold [%lu]\n",
395 tz->trips.critical.temperature));
397 if (tz->trips.critical.flags.valid == 1) {
399 tz->trips.critical.flags.valid = 0;
400 } else if (crt > 0) {
401 unsigned long crt_k = CELSIUS_TO_KELVIN(crt);
403 * Allow override critical threshold
405 if (crt_k > tz->trips.critical.temperature)
406 printk(KERN_WARNING PREFIX
407 "Critical threshold %d C\n", crt);
408 tz->trips.critical.temperature = crt_k;
413 /* Critical Sleep (optional) */
414 if (flag & ACPI_TRIPS_HOT) {
415 status = acpi_evaluate_integer(tz->device->handle,
417 if (ACPI_FAILURE(status)) {
418 tz->trips.hot.flags.valid = 0;
419 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
420 "No hot threshold\n"));
422 tz->trips.hot.temperature = tmp;
423 tz->trips.hot.flags.valid = 1;
424 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
425 "Found hot threshold [%lu]\n",
426 tz->trips.critical.temperature));
430 /* Passive (optional) */
431 if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.flags.valid) ||
432 (flag == ACPI_TRIPS_INIT)) {
433 valid = tz->trips.passive.flags.valid;
436 } else if (psv > 0) {
437 tmp = CELSIUS_TO_KELVIN(psv);
440 status = acpi_evaluate_integer(tz->device->handle,
444 if (ACPI_FAILURE(status))
445 tz->trips.passive.flags.valid = 0;
447 tz->trips.passive.temperature = tmp;
448 tz->trips.passive.flags.valid = 1;
449 if (flag == ACPI_TRIPS_INIT) {
450 status = acpi_evaluate_integer(
451 tz->device->handle, "_TC1",
453 if (ACPI_FAILURE(status))
454 tz->trips.passive.flags.valid = 0;
456 tz->trips.passive.tc1 = tmp;
457 status = acpi_evaluate_integer(
458 tz->device->handle, "_TC2",
460 if (ACPI_FAILURE(status))
461 tz->trips.passive.flags.valid = 0;
463 tz->trips.passive.tc2 = tmp;
464 status = acpi_evaluate_integer(
465 tz->device->handle, "_TSP",
467 if (ACPI_FAILURE(status))
468 tz->trips.passive.flags.valid = 0;
470 tz->trips.passive.tsp = tmp;
474 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.flags.valid) {
475 memset(&devices, 0, sizeof(struct acpi_handle_list));
476 status = acpi_evaluate_reference(tz->device->handle, "_PSL",
478 if (ACPI_FAILURE(status)) {
479 printk(KERN_WARNING PREFIX
480 "Invalid passive threshold\n");
481 tz->trips.passive.flags.valid = 0;
484 tz->trips.passive.flags.valid = 1;
486 if (memcmp(&tz->trips.passive.devices, &devices,
487 sizeof(struct acpi_handle_list))) {
488 memcpy(&tz->trips.passive.devices, &devices,
489 sizeof(struct acpi_handle_list));
490 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
493 if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
494 if (valid != tz->trips.passive.flags.valid)
495 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
498 /* Active (optional) */
499 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
500 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
501 valid = tz->trips.active[i].flags.valid;
504 break; /* disable all active trip points */
506 if ((flag == ACPI_TRIPS_INIT) || ((flag & ACPI_TRIPS_ACTIVE) &&
507 tz->trips.active[i].flags.valid)) {
508 status = acpi_evaluate_integer(tz->device->handle,
510 if (ACPI_FAILURE(status)) {
511 tz->trips.active[i].flags.valid = 0;
517 tz->trips.active[0].temperature =
518 CELSIUS_TO_KELVIN(act);
521 * Don't allow override higher than
522 * the next higher trip point
524 tz->trips.active[i - 1].temperature =
525 (tz->trips.active[i - 2].temperature <
526 CELSIUS_TO_KELVIN(act) ?
527 tz->trips.active[i - 2].temperature :
528 CELSIUS_TO_KELVIN(act));
531 tz->trips.active[i].temperature = tmp;
532 tz->trips.active[i].flags.valid = 1;
537 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].flags.valid ) {
538 memset(&devices, 0, sizeof(struct acpi_handle_list));
539 status = acpi_evaluate_reference(tz->device->handle,
540 name, NULL, &devices);
541 if (ACPI_FAILURE(status)) {
542 printk(KERN_WARNING PREFIX
543 "Invalid active%d threshold\n", i);
544 tz->trips.active[i].flags.valid = 0;
547 tz->trips.active[i].flags.valid = 1;
549 if (memcmp(&tz->trips.active[i].devices, &devices,
550 sizeof(struct acpi_handle_list))) {
551 memcpy(&tz->trips.active[i].devices, &devices,
552 sizeof(struct acpi_handle_list));
553 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
556 if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
557 if (valid != tz->trips.active[i].flags.valid)
558 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
560 if (!tz->trips.active[i].flags.valid)
564 if (flag & ACPI_TRIPS_DEVICES) {
565 memset(&devices, 0, sizeof(struct acpi_handle_list));
566 status = acpi_evaluate_reference(tz->device->handle, "_TZD",
568 if (memcmp(&tz->devices, &devices,
569 sizeof(struct acpi_handle_list))) {
570 memcpy(&tz->devices, &devices,
571 sizeof(struct acpi_handle_list));
572 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
579 static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
581 int i, valid, ret = acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
586 valid = tz->trips.critical.flags.valid |
587 tz->trips.hot.flags.valid |
588 tz->trips.passive.flags.valid;
590 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
591 valid |= tz->trips.active[i].flags.valid;
594 printk(KERN_WARNING FW_BUG "No valid trip found\n");
600 static void acpi_thermal_check(void *data)
602 struct acpi_thermal *tz = data;
604 thermal_zone_device_update(tz->thermal_zone);
607 /* sys I/F for generic thermal sysfs support */
608 #define KELVIN_TO_MILLICELSIUS(t, off) (((t) - (off)) * 100)
610 static int thermal_get_temp(struct thermal_zone_device *thermal,
613 struct acpi_thermal *tz = thermal->devdata;
619 result = acpi_thermal_get_temperature(tz);
623 *temp = KELVIN_TO_MILLICELSIUS(tz->temperature, tz->kelvin_offset);
627 static const char enabled[] = "kernel";
628 static const char disabled[] = "user";
629 static int thermal_get_mode(struct thermal_zone_device *thermal,
630 enum thermal_device_mode *mode)
632 struct acpi_thermal *tz = thermal->devdata;
637 *mode = tz->tz_enabled ? THERMAL_DEVICE_ENABLED :
638 THERMAL_DEVICE_DISABLED;
643 static int thermal_set_mode(struct thermal_zone_device *thermal,
644 enum thermal_device_mode mode)
646 struct acpi_thermal *tz = thermal->devdata;
653 * enable/disable thermal management from ACPI thermal driver
655 if (mode == THERMAL_DEVICE_ENABLED)
657 else if (mode == THERMAL_DEVICE_DISABLED)
662 if (enable != tz->tz_enabled) {
663 tz->tz_enabled = enable;
664 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
665 "%s ACPI thermal control\n",
666 tz->tz_enabled ? enabled : disabled));
667 acpi_thermal_check(tz);
672 static int thermal_get_trip_type(struct thermal_zone_device *thermal,
673 int trip, enum thermal_trip_type *type)
675 struct acpi_thermal *tz = thermal->devdata;
681 if (tz->trips.critical.flags.valid) {
683 *type = THERMAL_TRIP_CRITICAL;
689 if (tz->trips.hot.flags.valid) {
691 *type = THERMAL_TRIP_HOT;
697 if (tz->trips.passive.flags.valid) {
699 *type = THERMAL_TRIP_PASSIVE;
705 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
706 tz->trips.active[i].flags.valid; i++) {
708 *type = THERMAL_TRIP_ACTIVE;
717 static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
718 int trip, unsigned long *temp)
720 struct acpi_thermal *tz = thermal->devdata;
726 if (tz->trips.critical.flags.valid) {
728 *temp = KELVIN_TO_MILLICELSIUS(
729 tz->trips.critical.temperature,
736 if (tz->trips.hot.flags.valid) {
738 *temp = KELVIN_TO_MILLICELSIUS(
739 tz->trips.hot.temperature,
746 if (tz->trips.passive.flags.valid) {
748 *temp = KELVIN_TO_MILLICELSIUS(
749 tz->trips.passive.temperature,
756 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
757 tz->trips.active[i].flags.valid; i++) {
759 *temp = KELVIN_TO_MILLICELSIUS(
760 tz->trips.active[i].temperature,
770 static int thermal_get_crit_temp(struct thermal_zone_device *thermal,
771 unsigned long *temperature) {
772 struct acpi_thermal *tz = thermal->devdata;
774 if (tz->trips.critical.flags.valid) {
775 *temperature = KELVIN_TO_MILLICELSIUS(
776 tz->trips.critical.temperature,
783 static int thermal_notify(struct thermal_zone_device *thermal, int trip,
784 enum thermal_trip_type trip_type)
787 struct acpi_thermal *tz = thermal->devdata;
789 if (trip_type == THERMAL_TRIP_CRITICAL)
790 type = ACPI_THERMAL_NOTIFY_CRITICAL;
791 else if (trip_type == THERMAL_TRIP_HOT)
792 type = ACPI_THERMAL_NOTIFY_HOT;
796 acpi_bus_generate_proc_event(tz->device, type, 1);
797 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
798 dev_name(&tz->device->dev), type, 1);
800 if (trip_type == THERMAL_TRIP_CRITICAL && nocrt)
806 typedef int (*cb)(struct thermal_zone_device *, int,
807 struct thermal_cooling_device *);
808 static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
809 struct thermal_cooling_device *cdev,
812 struct acpi_device *device = cdev->devdata;
813 struct acpi_thermal *tz = thermal->devdata;
814 struct acpi_device *dev;
822 if (tz->trips.critical.flags.valid)
825 if (tz->trips.hot.flags.valid)
828 if (tz->trips.passive.flags.valid) {
830 for (i = 0; i < tz->trips.passive.devices.count;
832 handle = tz->trips.passive.devices.handles[i];
833 status = acpi_bus_get_device(handle, &dev);
834 if (ACPI_SUCCESS(status) && (dev == device)) {
835 result = action(thermal, trip, cdev);
842 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
843 if (!tz->trips.active[i].flags.valid)
847 j < tz->trips.active[i].devices.count;
849 handle = tz->trips.active[i].devices.handles[j];
850 status = acpi_bus_get_device(handle, &dev);
851 if (ACPI_SUCCESS(status) && (dev == device)) {
852 result = action(thermal, trip, cdev);
859 for (i = 0; i < tz->devices.count; i++) {
860 handle = tz->devices.handles[i];
861 status = acpi_bus_get_device(handle, &dev);
862 if (ACPI_SUCCESS(status) && (dev == device)) {
863 result = action(thermal, -1, cdev);
874 acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
875 struct thermal_cooling_device *cdev)
877 return acpi_thermal_cooling_device_cb(thermal, cdev,
878 thermal_zone_bind_cooling_device);
882 acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
883 struct thermal_cooling_device *cdev)
885 return acpi_thermal_cooling_device_cb(thermal, cdev,
886 thermal_zone_unbind_cooling_device);
889 static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
890 .bind = acpi_thermal_bind_cooling_device,
891 .unbind = acpi_thermal_unbind_cooling_device,
892 .get_temp = thermal_get_temp,
893 .get_mode = thermal_get_mode,
894 .set_mode = thermal_set_mode,
895 .get_trip_type = thermal_get_trip_type,
896 .get_trip_temp = thermal_get_trip_temp,
897 .get_crit_temp = thermal_get_crit_temp,
898 .notify = thermal_notify,
901 static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
908 if (tz->trips.critical.flags.valid)
911 if (tz->trips.hot.flags.valid)
914 if (tz->trips.passive.flags.valid)
917 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
918 tz->trips.active[i].flags.valid; i++, trips++);
920 if (tz->trips.passive.flags.valid)
922 thermal_zone_device_register("acpitz", trips, tz,
923 &acpi_thermal_zone_ops,
924 tz->trips.passive.tc1,
925 tz->trips.passive.tc2,
926 tz->trips.passive.tsp*100,
927 tz->polling_frequency*100);
930 thermal_zone_device_register("acpitz", trips, tz,
931 &acpi_thermal_zone_ops,
933 tz->polling_frequency*100);
934 if (IS_ERR(tz->thermal_zone))
937 result = sysfs_create_link(&tz->device->dev.kobj,
938 &tz->thermal_zone->device.kobj, "thermal_zone");
942 result = sysfs_create_link(&tz->thermal_zone->device.kobj,
943 &tz->device->dev.kobj, "device");
947 status = acpi_attach_data(tz->device->handle,
948 acpi_bus_private_data_handler,
950 if (ACPI_FAILURE(status)) {
951 printk(KERN_ERR PREFIX
952 "Error attaching device data\n");
958 dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
959 tz->thermal_zone->id);
963 static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
965 sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
966 sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
967 thermal_zone_device_unregister(tz->thermal_zone);
968 tz->thermal_zone = NULL;
969 acpi_detach_data(tz->device->handle, acpi_bus_private_data_handler);
973 /* --------------------------------------------------------------------------
975 -------------------------------------------------------------------------- */
977 static struct proc_dir_entry *acpi_thermal_dir;
979 static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
981 struct acpi_thermal *tz = seq->private;
987 seq_puts(seq, "state: ");
989 if (!tz->state.critical && !tz->state.hot && !tz->state.passive
990 && !tz->state.active)
991 seq_puts(seq, "ok\n");
993 if (tz->state.critical)
994 seq_puts(seq, "critical ");
996 seq_puts(seq, "hot ");
997 if (tz->state.passive)
998 seq_puts(seq, "passive ");
999 if (tz->state.active)
1000 seq_printf(seq, "active[%d]", tz->state.active_index);
1001 seq_puts(seq, "\n");
1008 static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
1010 return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
1013 static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
1016 struct acpi_thermal *tz = seq->private;
1022 result = acpi_thermal_get_temperature(tz);
1026 seq_printf(seq, "temperature: %ld C\n",
1027 KELVIN_TO_CELSIUS(tz->temperature));
1033 static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
1035 return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
1038 static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
1040 struct acpi_thermal *tz = seq->private;
1041 struct acpi_device *device;
1051 if (tz->trips.critical.flags.valid)
1052 seq_printf(seq, "critical (S5): %ld C%s",
1053 KELVIN_TO_CELSIUS(tz->trips.critical.temperature),
1054 nocrt ? " <disabled>\n" : "\n");
1056 if (tz->trips.hot.flags.valid)
1057 seq_printf(seq, "hot (S4): %ld C%s",
1058 KELVIN_TO_CELSIUS(tz->trips.hot.temperature),
1059 nocrt ? " <disabled>\n" : "\n");
1061 if (tz->trips.passive.flags.valid) {
1063 "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
1064 KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
1065 tz->trips.passive.tc1, tz->trips.passive.tc2,
1066 tz->trips.passive.tsp);
1067 for (j = 0; j < tz->trips.passive.devices.count; j++) {
1068 status = acpi_bus_get_device(tz->trips.passive.devices.
1069 handles[j], &device);
1070 seq_printf(seq, "%4.4s ", status ? "" :
1071 acpi_device_bid(device));
1073 seq_puts(seq, "\n");
1075 seq_printf(seq, "passive (forced):");
1076 if (tz->thermal_zone->forced_passive)
1077 seq_printf(seq, " %i C\n",
1078 tz->thermal_zone->forced_passive / 1000);
1080 seq_printf(seq, "<not set>\n");
1083 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1084 if (!(tz->trips.active[i].flags.valid))
1086 seq_printf(seq, "active[%d]: %ld C: devices=",
1088 KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
1089 for (j = 0; j < tz->trips.active[i].devices.count; j++){
1090 status = acpi_bus_get_device(tz->trips.active[i].
1093 seq_printf(seq, "%4.4s ", status ? "" :
1094 acpi_device_bid(device));
1096 seq_puts(seq, "\n");
1103 static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
1105 return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
1108 static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
1110 struct acpi_thermal *tz = seq->private;
1116 if (!tz->flags.cooling_mode)
1117 seq_puts(seq, "<setting not supported>\n");
1119 seq_puts(seq, "0 - Active; 1 - Passive\n");
1125 static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
1127 return single_open(file, acpi_thermal_cooling_seq_show,
1132 acpi_thermal_write_cooling_mode(struct file *file,
1133 const char __user * buffer,
1134 size_t count, loff_t * ppos)
1136 struct seq_file *m = file->private_data;
1137 struct acpi_thermal *tz = m->private;
1139 char mode_string[12] = { '\0' };
1142 if (!tz || (count > sizeof(mode_string) - 1))
1145 if (!tz->flags.cooling_mode)
1148 if (copy_from_user(mode_string, buffer, count))
1151 mode_string[count] = '\0';
1153 result = acpi_thermal_set_cooling_mode(tz,
1154 simple_strtoul(mode_string, NULL,
1159 acpi_thermal_check(tz);
1164 static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
1166 struct acpi_thermal *tz = seq->private;
1172 if (!tz->thermal_zone->polling_delay) {
1173 seq_puts(seq, "<polling disabled>\n");
1177 seq_printf(seq, "polling frequency: %d seconds\n",
1178 (tz->thermal_zone->polling_delay / 1000));
1184 static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
1186 return single_open(file, acpi_thermal_polling_seq_show,
1191 acpi_thermal_write_polling(struct file *file,
1192 const char __user * buffer,
1193 size_t count, loff_t * ppos)
1195 struct seq_file *m = file->private_data;
1196 struct acpi_thermal *tz = m->private;
1198 char polling_string[12] = { '\0' };
1202 if (!tz || (count > sizeof(polling_string) - 1))
1205 if (copy_from_user(polling_string, buffer, count))
1208 polling_string[count] = '\0';
1210 seconds = simple_strtoul(polling_string, NULL, 0);
1212 result = acpi_thermal_set_polling(tz, seconds);
1216 acpi_thermal_check(tz);
1221 static int acpi_thermal_add_fs(struct acpi_device *device)
1223 struct proc_dir_entry *entry = NULL;
1226 if (!acpi_device_dir(device)) {
1227 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
1229 if (!acpi_device_dir(device))
1234 entry = proc_create_data(ACPI_THERMAL_FILE_STATE,
1235 S_IRUGO, acpi_device_dir(device),
1236 &acpi_thermal_state_fops,
1237 acpi_driver_data(device));
1241 /* 'temperature' [R] */
1242 entry = proc_create_data(ACPI_THERMAL_FILE_TEMPERATURE,
1243 S_IRUGO, acpi_device_dir(device),
1244 &acpi_thermal_temp_fops,
1245 acpi_driver_data(device));
1249 /* 'trip_points' [R] */
1250 entry = proc_create_data(ACPI_THERMAL_FILE_TRIP_POINTS,
1252 acpi_device_dir(device),
1253 &acpi_thermal_trip_fops,
1254 acpi_driver_data(device));
1258 /* 'cooling_mode' [R/W] */
1259 entry = proc_create_data(ACPI_THERMAL_FILE_COOLING_MODE,
1260 S_IFREG | S_IRUGO | S_IWUSR,
1261 acpi_device_dir(device),
1262 &acpi_thermal_cooling_fops,
1263 acpi_driver_data(device));
1267 /* 'polling_frequency' [R/W] */
1268 entry = proc_create_data(ACPI_THERMAL_FILE_POLLING_FREQ,
1269 S_IFREG | S_IRUGO | S_IWUSR,
1270 acpi_device_dir(device),
1271 &acpi_thermal_polling_fops,
1272 acpi_driver_data(device));
1278 static int acpi_thermal_remove_fs(struct acpi_device *device)
1281 if (acpi_device_dir(device)) {
1282 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1283 acpi_device_dir(device));
1284 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1285 acpi_device_dir(device));
1286 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1287 acpi_device_dir(device));
1288 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1289 acpi_device_dir(device));
1290 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1291 acpi_device_dir(device));
1292 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1293 acpi_device_dir(device) = NULL;
1299 /* --------------------------------------------------------------------------
1301 -------------------------------------------------------------------------- */
1303 static void acpi_thermal_notify(struct acpi_device *device, u32 event)
1305 struct acpi_thermal *tz = acpi_driver_data(device);
1312 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1313 acpi_thermal_check(tz);
1315 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
1316 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
1317 acpi_thermal_check(tz);
1318 acpi_bus_generate_proc_event(device, event, 0);
1319 acpi_bus_generate_netlink_event(device->pnp.device_class,
1320 dev_name(&device->dev), event, 0);
1322 case ACPI_THERMAL_NOTIFY_DEVICES:
1323 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
1324 acpi_thermal_check(tz);
1325 acpi_bus_generate_proc_event(device, event, 0);
1326 acpi_bus_generate_netlink_event(device->pnp.device_class,
1327 dev_name(&device->dev), event, 0);
1330 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1331 "Unsupported event [0x%x]\n", event));
1336 static int acpi_thermal_get_info(struct acpi_thermal *tz)
1344 /* Get temperature [_TMP] (required) */
1345 result = acpi_thermal_get_temperature(tz);
1349 /* Get trip points [_CRT, _PSV, etc.] (required) */
1350 result = acpi_thermal_get_trip_points(tz);
1354 /* Set the cooling mode [_SCP] to active cooling (default) */
1355 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
1357 tz->flags.cooling_mode = 1;
1359 /* Get default polling frequency [_TZP] (optional) */
1361 tz->polling_frequency = tzp;
1363 acpi_thermal_get_polling_frequency(tz);
1369 * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI
1370 * handles temperature values with a single decimal place. As a consequence,
1371 * some implementations use an offset of 273.1 and others use an offset of
1372 * 273.2. Try to find out which one is being used, to present the most
1373 * accurate and visually appealing number.
1375 * The heuristic below should work for all ACPI thermal zones which have a
1376 * critical trip point with a value being a multiple of 0.5 degree Celsius.
1378 static void acpi_thermal_guess_offset(struct acpi_thermal *tz)
1380 if (tz->trips.critical.flags.valid &&
1381 (tz->trips.critical.temperature % 5) == 1)
1382 tz->kelvin_offset = 2731;
1384 tz->kelvin_offset = 2732;
1387 static int acpi_thermal_add(struct acpi_device *device)
1390 struct acpi_thermal *tz = NULL;
1396 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
1400 tz->device = device;
1401 strcpy(tz->name, device->pnp.bus_id);
1402 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1403 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1404 device->driver_data = tz;
1405 mutex_init(&tz->lock);
1408 result = acpi_thermal_get_info(tz);
1412 acpi_thermal_guess_offset(tz);
1414 result = acpi_thermal_register_thermal_zone(tz);
1418 result = acpi_thermal_add_fs(device);
1420 goto unregister_thermal_zone;
1422 printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
1423 acpi_device_name(device), acpi_device_bid(device),
1424 KELVIN_TO_CELSIUS(tz->temperature));
1427 unregister_thermal_zone:
1428 thermal_zone_device_unregister(tz->thermal_zone);
1435 static int acpi_thermal_remove(struct acpi_device *device, int type)
1437 struct acpi_thermal *tz = NULL;
1439 if (!device || !acpi_driver_data(device))
1442 tz = acpi_driver_data(device);
1444 acpi_thermal_remove_fs(device);
1445 acpi_thermal_unregister_thermal_zone(tz);
1446 mutex_destroy(&tz->lock);
1451 static int acpi_thermal_resume(struct acpi_device *device)
1453 struct acpi_thermal *tz = NULL;
1454 int i, j, power_state, result;
1457 if (!device || !acpi_driver_data(device))
1460 tz = acpi_driver_data(device);
1462 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1463 if (!(&tz->trips.active[i]))
1465 if (!tz->trips.active[i].flags.valid)
1467 tz->trips.active[i].flags.enabled = 1;
1468 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1469 result = acpi_bus_get_power(tz->trips.active[i].devices.
1470 handles[j], &power_state);
1471 if (result || (power_state != ACPI_STATE_D0)) {
1472 tz->trips.active[i].flags.enabled = 0;
1476 tz->state.active |= tz->trips.active[i].flags.enabled;
1479 acpi_thermal_check(tz);
1484 static int thermal_act(const struct dmi_system_id *d) {
1487 printk(KERN_NOTICE "ACPI: %s detected: "
1488 "disabling all active thermal trip points\n", d->ident);
1493 static int thermal_nocrt(const struct dmi_system_id *d) {
1495 printk(KERN_NOTICE "ACPI: %s detected: "
1496 "disabling all critical thermal trip point actions.\n", d->ident);
1500 static int thermal_tzp(const struct dmi_system_id *d) {
1503 printk(KERN_NOTICE "ACPI: %s detected: "
1504 "enabling thermal zone polling\n", d->ident);
1505 tzp = 300; /* 300 dS = 30 Seconds */
1509 static int thermal_psv(const struct dmi_system_id *d) {
1512 printk(KERN_NOTICE "ACPI: %s detected: "
1513 "disabling all passive thermal trip points\n", d->ident);
1519 static struct dmi_system_id thermal_dmi_table[] __initdata = {
1521 * Award BIOS on this AOpen makes thermal control almost worthless.
1522 * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1525 .callback = thermal_act,
1526 .ident = "AOpen i915GMm-HFS",
1528 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1529 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1533 .callback = thermal_psv,
1534 .ident = "AOpen i915GMm-HFS",
1536 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1537 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1541 .callback = thermal_tzp,
1542 .ident = "AOpen i915GMm-HFS",
1544 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1545 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1549 .callback = thermal_nocrt,
1550 .ident = "Gigabyte GA-7ZX",
1552 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
1553 DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
1559 static int __init acpi_thermal_init(void)
1563 dmi_check_system(thermal_dmi_table);
1566 printk(KERN_NOTICE "ACPI: thermal control disabled\n");
1569 acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1570 if (!acpi_thermal_dir)
1573 result = acpi_bus_register_driver(&acpi_thermal_driver);
1575 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1582 static void __exit acpi_thermal_exit(void)
1585 acpi_bus_unregister_driver(&acpi_thermal_driver);
1587 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1592 module_init(acpi_thermal_init);
1593 module_exit(acpi_thermal_exit);