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/types.h>
39 #include <linux/proc_fs.h>
40 #include <linux/jiffies.h>
41 #include <linux/kmod.h>
42 #include <linux/seq_file.h>
43 #include <linux/reboot.h>
44 #include <linux/device.h>
45 #include <asm/uaccess.h>
46 #include <linux/thermal.h>
47 #include <acpi/acpi_bus.h>
48 #include <acpi/acpi_drivers.h>
50 #define PREFIX "ACPI: "
52 #define ACPI_THERMAL_CLASS "thermal_zone"
53 #define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
54 #define ACPI_THERMAL_FILE_STATE "state"
55 #define ACPI_THERMAL_FILE_TEMPERATURE "temperature"
56 #define ACPI_THERMAL_FILE_TRIP_POINTS "trip_points"
57 #define ACPI_THERMAL_FILE_COOLING_MODE "cooling_mode"
58 #define ACPI_THERMAL_FILE_POLLING_FREQ "polling_frequency"
59 #define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
60 #define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
61 #define ACPI_THERMAL_NOTIFY_DEVICES 0x82
62 #define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
63 #define ACPI_THERMAL_NOTIFY_HOT 0xF1
64 #define ACPI_THERMAL_MODE_ACTIVE 0x00
66 #define ACPI_THERMAL_MAX_ACTIVE 10
67 #define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
69 #define _COMPONENT ACPI_THERMAL_COMPONENT
70 ACPI_MODULE_NAME("thermal");
72 MODULE_AUTHOR("Paul Diefenbaugh");
73 MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
74 MODULE_LICENSE("GPL");
77 module_param(act, int, 0644);
78 MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.");
81 module_param(crt, int, 0644);
82 MODULE_PARM_DESC(crt, "Disable or lower all critical trip points.");
85 module_param(tzp, int, 0444);
86 MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.");
89 module_param(nocrt, int, 0);
90 MODULE_PARM_DESC(nocrt, "Set to take no action upon ACPI thermal zone critical trips points.");
93 module_param(off, int, 0);
94 MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
97 module_param(psv, int, 0644);
98 MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
100 static int acpi_thermal_add(struct acpi_device *device);
101 static int acpi_thermal_remove(struct acpi_device *device, int type);
102 static int acpi_thermal_resume(struct acpi_device *device);
103 static void acpi_thermal_notify(struct acpi_device *device, u32 event);
104 static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
105 static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
106 static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
107 static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
108 static ssize_t acpi_thermal_write_cooling_mode(struct file *,
109 const char __user *, size_t,
111 static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
112 static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
115 static const struct acpi_device_id thermal_device_ids[] = {
116 {ACPI_THERMAL_HID, 0},
119 MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
121 static struct acpi_driver acpi_thermal_driver = {
123 .class = ACPI_THERMAL_CLASS,
124 .ids = thermal_device_ids,
126 .add = acpi_thermal_add,
127 .remove = acpi_thermal_remove,
128 .resume = acpi_thermal_resume,
129 .notify = acpi_thermal_notify,
133 struct acpi_thermal_state {
142 struct acpi_thermal_state_flags {
148 struct acpi_thermal_critical {
149 struct acpi_thermal_state_flags flags;
150 unsigned long temperature;
153 struct acpi_thermal_hot {
154 struct acpi_thermal_state_flags flags;
155 unsigned long temperature;
158 struct acpi_thermal_passive {
159 struct acpi_thermal_state_flags flags;
160 unsigned long temperature;
164 struct acpi_handle_list devices;
167 struct acpi_thermal_active {
168 struct acpi_thermal_state_flags flags;
169 unsigned long temperature;
170 struct acpi_handle_list devices;
173 struct acpi_thermal_trips {
174 struct acpi_thermal_critical critical;
175 struct acpi_thermal_hot hot;
176 struct acpi_thermal_passive passive;
177 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
180 struct acpi_thermal_flags {
181 u8 cooling_mode:1; /* _SCP */
182 u8 devices:1; /* _TZD */
186 struct acpi_thermal {
187 struct acpi_device * device;
189 unsigned long temperature;
190 unsigned long last_temperature;
191 unsigned long polling_frequency;
193 struct acpi_thermal_flags flags;
194 struct acpi_thermal_state state;
195 struct acpi_thermal_trips trips;
196 struct acpi_handle_list devices;
197 struct thermal_zone_device *thermal_zone;
203 static const struct file_operations acpi_thermal_state_fops = {
204 .owner = THIS_MODULE,
205 .open = acpi_thermal_state_open_fs,
208 .release = single_release,
211 static const struct file_operations acpi_thermal_temp_fops = {
212 .owner = THIS_MODULE,
213 .open = acpi_thermal_temp_open_fs,
216 .release = single_release,
219 static const struct file_operations acpi_thermal_trip_fops = {
220 .owner = THIS_MODULE,
221 .open = acpi_thermal_trip_open_fs,
224 .release = single_release,
227 static const struct file_operations acpi_thermal_cooling_fops = {
228 .owner = THIS_MODULE,
229 .open = acpi_thermal_cooling_open_fs,
231 .write = acpi_thermal_write_cooling_mode,
233 .release = single_release,
236 static const struct file_operations acpi_thermal_polling_fops = {
237 .owner = THIS_MODULE,
238 .open = acpi_thermal_polling_open_fs,
240 .write = acpi_thermal_write_polling,
242 .release = single_release,
245 /* --------------------------------------------------------------------------
246 Thermal Zone Management
247 -------------------------------------------------------------------------- */
249 static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
251 acpi_status status = AE_OK;
252 unsigned long long tmp;
257 tz->last_temperature = tz->temperature;
259 status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp);
260 if (ACPI_FAILURE(status))
263 tz->temperature = tmp;
264 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
270 static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
272 acpi_status status = AE_OK;
273 unsigned long long tmp;
278 status = acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, &tmp);
279 if (ACPI_FAILURE(status))
282 tz->polling_frequency = tmp;
283 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
284 tz->polling_frequency));
289 static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
295 tz->polling_frequency = seconds * 10; /* Convert value to deci-seconds */
297 tz->thermal_zone->polling_delay = seconds * 1000;
300 thermal_zone_device_update(tz->thermal_zone);
302 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
303 "Polling frequency set to %lu seconds\n",
304 tz->polling_frequency/10));
309 static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
311 acpi_status status = AE_OK;
312 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
313 struct acpi_object_list arg_list = { 1, &arg0 };
314 acpi_handle handle = NULL;
320 status = acpi_get_handle(tz->device->handle, "_SCP", &handle);
321 if (ACPI_FAILURE(status)) {
322 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
326 arg0.integer.value = mode;
328 status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
329 if (ACPI_FAILURE(status))
335 #define ACPI_TRIPS_CRITICAL 0x01
336 #define ACPI_TRIPS_HOT 0x02
337 #define ACPI_TRIPS_PASSIVE 0x04
338 #define ACPI_TRIPS_ACTIVE 0x08
339 #define ACPI_TRIPS_DEVICES 0x10
341 #define ACPI_TRIPS_REFRESH_THRESHOLDS (ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE)
342 #define ACPI_TRIPS_REFRESH_DEVICES ACPI_TRIPS_DEVICES
344 #define ACPI_TRIPS_INIT (ACPI_TRIPS_CRITICAL | ACPI_TRIPS_HOT | \
345 ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE | \
349 * This exception is thrown out in two cases:
350 * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
351 * when re-evaluating the AML code.
352 * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change.
353 * We need to re-bind the cooling devices of a thermal zone when this occurs.
355 #define ACPI_THERMAL_TRIPS_EXCEPTION(flags, str) \
357 if (flags != ACPI_TRIPS_INIT) \
358 ACPI_EXCEPTION((AE_INFO, AE_ERROR, \
359 "ACPI thermal trip point %s changed\n" \
360 "Please send acpidump to linux-acpi@vger.kernel.org\n", str)); \
363 static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
365 acpi_status status = AE_OK;
366 unsigned long long tmp;
367 struct acpi_handle_list devices;
371 /* Critical Shutdown */
372 if (flag & ACPI_TRIPS_CRITICAL) {
373 status = acpi_evaluate_integer(tz->device->handle,
375 tz->trips.critical.temperature = tmp;
377 * Treat freezing temperatures as invalid as well; some
378 * BIOSes return really low values and cause reboots at startup.
379 * Below zero (Celsius) values clearly aren't right for sure..
380 * ... so lets discard those as invalid.
382 if (ACPI_FAILURE(status)) {
383 tz->trips.critical.flags.valid = 0;
384 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
385 "No critical threshold\n"));
386 } else if (tmp <= 2732) {
387 printk(KERN_WARNING FW_BUG "Invalid critical threshold "
389 tz->trips.critical.flags.valid = 0;
391 tz->trips.critical.flags.valid = 1;
392 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
393 "Found critical threshold [%lu]\n",
394 tz->trips.critical.temperature));
396 if (tz->trips.critical.flags.valid == 1) {
398 tz->trips.critical.flags.valid = 0;
399 } else if (crt > 0) {
400 unsigned long crt_k = CELSIUS_TO_KELVIN(crt);
402 * Allow override critical threshold
404 if (crt_k > tz->trips.critical.temperature)
405 printk(KERN_WARNING PREFIX
406 "Critical threshold %d C\n", crt);
407 tz->trips.critical.temperature = crt_k;
412 /* Critical Sleep (optional) */
413 if (flag & ACPI_TRIPS_HOT) {
414 status = acpi_evaluate_integer(tz->device->handle,
416 if (ACPI_FAILURE(status)) {
417 tz->trips.hot.flags.valid = 0;
418 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
419 "No hot threshold\n"));
421 tz->trips.hot.temperature = tmp;
422 tz->trips.hot.flags.valid = 1;
423 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
424 "Found hot threshold [%lu]\n",
425 tz->trips.critical.temperature));
429 /* Passive (optional) */
430 if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.flags.valid) ||
431 (flag == ACPI_TRIPS_INIT)) {
432 valid = tz->trips.passive.flags.valid;
435 } else if (psv > 0) {
436 tmp = CELSIUS_TO_KELVIN(psv);
439 status = acpi_evaluate_integer(tz->device->handle,
443 if (ACPI_FAILURE(status))
444 tz->trips.passive.flags.valid = 0;
446 tz->trips.passive.temperature = tmp;
447 tz->trips.passive.flags.valid = 1;
448 if (flag == ACPI_TRIPS_INIT) {
449 status = acpi_evaluate_integer(
450 tz->device->handle, "_TC1",
452 if (ACPI_FAILURE(status))
453 tz->trips.passive.flags.valid = 0;
455 tz->trips.passive.tc1 = tmp;
456 status = acpi_evaluate_integer(
457 tz->device->handle, "_TC2",
459 if (ACPI_FAILURE(status))
460 tz->trips.passive.flags.valid = 0;
462 tz->trips.passive.tc2 = tmp;
463 status = acpi_evaluate_integer(
464 tz->device->handle, "_TSP",
466 if (ACPI_FAILURE(status))
467 tz->trips.passive.flags.valid = 0;
469 tz->trips.passive.tsp = tmp;
473 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.flags.valid) {
474 memset(&devices, 0, sizeof(struct acpi_handle_list));
475 status = acpi_evaluate_reference(tz->device->handle, "_PSL",
477 if (ACPI_FAILURE(status)) {
478 printk(KERN_WARNING PREFIX
479 "Invalid passive threshold\n");
480 tz->trips.passive.flags.valid = 0;
483 tz->trips.passive.flags.valid = 1;
485 if (memcmp(&tz->trips.passive.devices, &devices,
486 sizeof(struct acpi_handle_list))) {
487 memcpy(&tz->trips.passive.devices, &devices,
488 sizeof(struct acpi_handle_list));
489 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
492 if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
493 if (valid != tz->trips.passive.flags.valid)
494 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
497 /* Active (optional) */
498 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
499 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
500 valid = tz->trips.active[i].flags.valid;
503 break; /* disable all active trip points */
505 if ((flag == ACPI_TRIPS_INIT) || ((flag & ACPI_TRIPS_ACTIVE) &&
506 tz->trips.active[i].flags.valid)) {
507 status = acpi_evaluate_integer(tz->device->handle,
509 if (ACPI_FAILURE(status)) {
510 tz->trips.active[i].flags.valid = 0;
516 tz->trips.active[0].temperature =
517 CELSIUS_TO_KELVIN(act);
520 * Don't allow override higher than
521 * the next higher trip point
523 tz->trips.active[i - 1].temperature =
524 (tz->trips.active[i - 2].temperature <
525 CELSIUS_TO_KELVIN(act) ?
526 tz->trips.active[i - 2].temperature :
527 CELSIUS_TO_KELVIN(act));
530 tz->trips.active[i].temperature = tmp;
531 tz->trips.active[i].flags.valid = 1;
536 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].flags.valid ) {
537 memset(&devices, 0, sizeof(struct acpi_handle_list));
538 status = acpi_evaluate_reference(tz->device->handle,
539 name, NULL, &devices);
540 if (ACPI_FAILURE(status)) {
541 printk(KERN_WARNING PREFIX
542 "Invalid active%d threshold\n", i);
543 tz->trips.active[i].flags.valid = 0;
546 tz->trips.active[i].flags.valid = 1;
548 if (memcmp(&tz->trips.active[i].devices, &devices,
549 sizeof(struct acpi_handle_list))) {
550 memcpy(&tz->trips.active[i].devices, &devices,
551 sizeof(struct acpi_handle_list));
552 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
555 if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
556 if (valid != tz->trips.active[i].flags.valid)
557 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
559 if (!tz->trips.active[i].flags.valid)
563 if (flag & ACPI_TRIPS_DEVICES) {
564 memset(&devices, 0, sizeof(struct acpi_handle_list));
565 status = acpi_evaluate_reference(tz->device->handle, "_TZD",
567 if (memcmp(&tz->devices, &devices,
568 sizeof(struct acpi_handle_list))) {
569 memcpy(&tz->devices, &devices,
570 sizeof(struct acpi_handle_list));
571 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
578 static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
580 return acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
583 static void acpi_thermal_check(void *data)
585 struct acpi_thermal *tz = data;
587 thermal_zone_device_update(tz->thermal_zone);
590 /* sys I/F for generic thermal sysfs support */
591 #define KELVIN_TO_MILLICELSIUS(t, off) (((t) - (off)) * 100)
593 static int thermal_get_temp(struct thermal_zone_device *thermal,
596 struct acpi_thermal *tz = thermal->devdata;
602 result = acpi_thermal_get_temperature(tz);
606 *temp = KELVIN_TO_MILLICELSIUS(tz->temperature, tz->kelvin_offset);
610 static const char enabled[] = "kernel";
611 static const char disabled[] = "user";
612 static int thermal_get_mode(struct thermal_zone_device *thermal,
613 enum thermal_device_mode *mode)
615 struct acpi_thermal *tz = thermal->devdata;
620 *mode = tz->tz_enabled ? THERMAL_DEVICE_ENABLED :
621 THERMAL_DEVICE_DISABLED;
626 static int thermal_set_mode(struct thermal_zone_device *thermal,
627 enum thermal_device_mode mode)
629 struct acpi_thermal *tz = thermal->devdata;
636 * enable/disable thermal management from ACPI thermal driver
638 if (mode == THERMAL_DEVICE_ENABLED)
640 else if (mode == THERMAL_DEVICE_DISABLED)
645 if (enable != tz->tz_enabled) {
646 tz->tz_enabled = enable;
647 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
648 "%s ACPI thermal control\n",
649 tz->tz_enabled ? enabled : disabled));
650 acpi_thermal_check(tz);
655 static int thermal_get_trip_type(struct thermal_zone_device *thermal,
656 int trip, enum thermal_trip_type *type)
658 struct acpi_thermal *tz = thermal->devdata;
664 if (tz->trips.critical.flags.valid) {
666 *type = THERMAL_TRIP_CRITICAL;
672 if (tz->trips.hot.flags.valid) {
674 *type = THERMAL_TRIP_HOT;
680 if (tz->trips.passive.flags.valid) {
682 *type = THERMAL_TRIP_PASSIVE;
688 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
689 tz->trips.active[i].flags.valid; i++) {
691 *type = THERMAL_TRIP_ACTIVE;
700 static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
701 int trip, unsigned long *temp)
703 struct acpi_thermal *tz = thermal->devdata;
709 if (tz->trips.critical.flags.valid) {
711 *temp = KELVIN_TO_MILLICELSIUS(
712 tz->trips.critical.temperature,
719 if (tz->trips.hot.flags.valid) {
721 *temp = KELVIN_TO_MILLICELSIUS(
722 tz->trips.hot.temperature,
729 if (tz->trips.passive.flags.valid) {
731 *temp = KELVIN_TO_MILLICELSIUS(
732 tz->trips.passive.temperature,
739 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
740 tz->trips.active[i].flags.valid; i++) {
742 *temp = KELVIN_TO_MILLICELSIUS(
743 tz->trips.active[i].temperature,
753 static int thermal_get_crit_temp(struct thermal_zone_device *thermal,
754 unsigned long *temperature) {
755 struct acpi_thermal *tz = thermal->devdata;
757 if (tz->trips.critical.flags.valid) {
758 *temperature = KELVIN_TO_MILLICELSIUS(
759 tz->trips.critical.temperature,
766 static int thermal_notify(struct thermal_zone_device *thermal, int trip,
767 enum thermal_trip_type trip_type)
770 struct acpi_thermal *tz = thermal->devdata;
772 if (trip_type == THERMAL_TRIP_CRITICAL)
773 type = ACPI_THERMAL_NOTIFY_CRITICAL;
774 else if (trip_type == THERMAL_TRIP_HOT)
775 type = ACPI_THERMAL_NOTIFY_HOT;
779 acpi_bus_generate_proc_event(tz->device, type, 1);
780 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
781 dev_name(&tz->device->dev), type, 1);
783 if (trip_type == THERMAL_TRIP_CRITICAL && nocrt)
789 typedef int (*cb)(struct thermal_zone_device *, int,
790 struct thermal_cooling_device *);
791 static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
792 struct thermal_cooling_device *cdev,
795 struct acpi_device *device = cdev->devdata;
796 struct acpi_thermal *tz = thermal->devdata;
797 struct acpi_device *dev;
805 if (tz->trips.critical.flags.valid)
808 if (tz->trips.hot.flags.valid)
811 if (tz->trips.passive.flags.valid) {
813 for (i = 0; i < tz->trips.passive.devices.count;
815 handle = tz->trips.passive.devices.handles[i];
816 status = acpi_bus_get_device(handle, &dev);
817 if (ACPI_SUCCESS(status) && (dev == device)) {
818 result = action(thermal, trip, cdev);
825 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
826 if (!tz->trips.active[i].flags.valid)
830 j < tz->trips.active[i].devices.count;
832 handle = tz->trips.active[i].devices.handles[j];
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 < tz->devices.count; i++) {
843 handle = tz->devices.handles[i];
844 status = acpi_bus_get_device(handle, &dev);
845 if (ACPI_SUCCESS(status) && (dev == device)) {
846 result = action(thermal, -1, cdev);
857 acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
858 struct thermal_cooling_device *cdev)
860 return acpi_thermal_cooling_device_cb(thermal, cdev,
861 thermal_zone_bind_cooling_device);
865 acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
866 struct thermal_cooling_device *cdev)
868 return acpi_thermal_cooling_device_cb(thermal, cdev,
869 thermal_zone_unbind_cooling_device);
872 static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
873 .bind = acpi_thermal_bind_cooling_device,
874 .unbind = acpi_thermal_unbind_cooling_device,
875 .get_temp = thermal_get_temp,
876 .get_mode = thermal_get_mode,
877 .set_mode = thermal_set_mode,
878 .get_trip_type = thermal_get_trip_type,
879 .get_trip_temp = thermal_get_trip_temp,
880 .get_crit_temp = thermal_get_crit_temp,
881 .notify = thermal_notify,
884 static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
891 if (tz->trips.critical.flags.valid)
894 if (tz->trips.hot.flags.valid)
897 if (tz->trips.passive.flags.valid)
900 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
901 tz->trips.active[i].flags.valid; i++, trips++);
903 if (tz->trips.passive.flags.valid)
905 thermal_zone_device_register("acpitz", trips, tz,
906 &acpi_thermal_zone_ops,
907 tz->trips.passive.tc1,
908 tz->trips.passive.tc2,
909 tz->trips.passive.tsp*100,
910 tz->polling_frequency*100);
913 thermal_zone_device_register("acpitz", trips, tz,
914 &acpi_thermal_zone_ops,
916 tz->polling_frequency*100);
917 if (IS_ERR(tz->thermal_zone))
920 result = sysfs_create_link(&tz->device->dev.kobj,
921 &tz->thermal_zone->device.kobj, "thermal_zone");
925 result = sysfs_create_link(&tz->thermal_zone->device.kobj,
926 &tz->device->dev.kobj, "device");
930 status = acpi_attach_data(tz->device->handle,
931 acpi_bus_private_data_handler,
933 if (ACPI_FAILURE(status)) {
934 printk(KERN_ERR PREFIX
935 "Error attaching device data\n");
941 dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
942 tz->thermal_zone->id);
946 static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
948 sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
949 sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
950 thermal_zone_device_unregister(tz->thermal_zone);
951 tz->thermal_zone = NULL;
952 acpi_detach_data(tz->device->handle, acpi_bus_private_data_handler);
956 /* --------------------------------------------------------------------------
958 -------------------------------------------------------------------------- */
960 static struct proc_dir_entry *acpi_thermal_dir;
962 static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
964 struct acpi_thermal *tz = seq->private;
970 seq_puts(seq, "state: ");
972 if (!tz->state.critical && !tz->state.hot && !tz->state.passive
973 && !tz->state.active)
974 seq_puts(seq, "ok\n");
976 if (tz->state.critical)
977 seq_puts(seq, "critical ");
979 seq_puts(seq, "hot ");
980 if (tz->state.passive)
981 seq_puts(seq, "passive ");
982 if (tz->state.active)
983 seq_printf(seq, "active[%d]", tz->state.active_index);
991 static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
993 return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
996 static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
999 struct acpi_thermal *tz = seq->private;
1005 result = acpi_thermal_get_temperature(tz);
1009 seq_printf(seq, "temperature: %ld C\n",
1010 KELVIN_TO_CELSIUS(tz->temperature));
1016 static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
1018 return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
1021 static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
1023 struct acpi_thermal *tz = seq->private;
1024 struct acpi_device *device;
1034 if (tz->trips.critical.flags.valid)
1035 seq_printf(seq, "critical (S5): %ld C%s",
1036 KELVIN_TO_CELSIUS(tz->trips.critical.temperature),
1037 nocrt ? " <disabled>\n" : "\n");
1039 if (tz->trips.hot.flags.valid)
1040 seq_printf(seq, "hot (S4): %ld C%s",
1041 KELVIN_TO_CELSIUS(tz->trips.hot.temperature),
1042 nocrt ? " <disabled>\n" : "\n");
1044 if (tz->trips.passive.flags.valid) {
1046 "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
1047 KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
1048 tz->trips.passive.tc1, tz->trips.passive.tc2,
1049 tz->trips.passive.tsp);
1050 for (j = 0; j < tz->trips.passive.devices.count; j++) {
1051 status = acpi_bus_get_device(tz->trips.passive.devices.
1052 handles[j], &device);
1053 seq_printf(seq, "%4.4s ", status ? "" :
1054 acpi_device_bid(device));
1056 seq_puts(seq, "\n");
1058 seq_printf(seq, "passive (forced):");
1059 if (tz->thermal_zone->forced_passive)
1060 seq_printf(seq, " %i C\n",
1061 tz->thermal_zone->forced_passive / 1000);
1063 seq_printf(seq, "<not set>\n");
1066 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1067 if (!(tz->trips.active[i].flags.valid))
1069 seq_printf(seq, "active[%d]: %ld C: devices=",
1071 KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
1072 for (j = 0; j < tz->trips.active[i].devices.count; j++){
1073 status = acpi_bus_get_device(tz->trips.active[i].
1076 seq_printf(seq, "%4.4s ", status ? "" :
1077 acpi_device_bid(device));
1079 seq_puts(seq, "\n");
1086 static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
1088 return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
1091 static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
1093 struct acpi_thermal *tz = seq->private;
1099 if (!tz->flags.cooling_mode)
1100 seq_puts(seq, "<setting not supported>\n");
1102 seq_puts(seq, "0 - Active; 1 - Passive\n");
1108 static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
1110 return single_open(file, acpi_thermal_cooling_seq_show,
1115 acpi_thermal_write_cooling_mode(struct file *file,
1116 const char __user * buffer,
1117 size_t count, loff_t * ppos)
1119 struct seq_file *m = file->private_data;
1120 struct acpi_thermal *tz = m->private;
1122 char mode_string[12] = { '\0' };
1125 if (!tz || (count > sizeof(mode_string) - 1))
1128 if (!tz->flags.cooling_mode)
1131 if (copy_from_user(mode_string, buffer, count))
1134 mode_string[count] = '\0';
1136 result = acpi_thermal_set_cooling_mode(tz,
1137 simple_strtoul(mode_string, NULL,
1142 acpi_thermal_check(tz);
1147 static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
1149 struct acpi_thermal *tz = seq->private;
1155 if (!tz->thermal_zone->polling_delay) {
1156 seq_puts(seq, "<polling disabled>\n");
1160 seq_printf(seq, "polling frequency: %d seconds\n",
1161 (tz->thermal_zone->polling_delay / 1000));
1167 static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
1169 return single_open(file, acpi_thermal_polling_seq_show,
1174 acpi_thermal_write_polling(struct file *file,
1175 const char __user * buffer,
1176 size_t count, loff_t * ppos)
1178 struct seq_file *m = file->private_data;
1179 struct acpi_thermal *tz = m->private;
1181 char polling_string[12] = { '\0' };
1185 if (!tz || (count > sizeof(polling_string) - 1))
1188 if (copy_from_user(polling_string, buffer, count))
1191 polling_string[count] = '\0';
1193 seconds = simple_strtoul(polling_string, NULL, 0);
1195 result = acpi_thermal_set_polling(tz, seconds);
1199 acpi_thermal_check(tz);
1204 static int acpi_thermal_add_fs(struct acpi_device *device)
1206 struct proc_dir_entry *entry = NULL;
1209 if (!acpi_device_dir(device)) {
1210 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
1212 if (!acpi_device_dir(device))
1217 entry = proc_create_data(ACPI_THERMAL_FILE_STATE,
1218 S_IRUGO, acpi_device_dir(device),
1219 &acpi_thermal_state_fops,
1220 acpi_driver_data(device));
1224 /* 'temperature' [R] */
1225 entry = proc_create_data(ACPI_THERMAL_FILE_TEMPERATURE,
1226 S_IRUGO, acpi_device_dir(device),
1227 &acpi_thermal_temp_fops,
1228 acpi_driver_data(device));
1232 /* 'trip_points' [R] */
1233 entry = proc_create_data(ACPI_THERMAL_FILE_TRIP_POINTS,
1235 acpi_device_dir(device),
1236 &acpi_thermal_trip_fops,
1237 acpi_driver_data(device));
1241 /* 'cooling_mode' [R/W] */
1242 entry = proc_create_data(ACPI_THERMAL_FILE_COOLING_MODE,
1243 S_IFREG | S_IRUGO | S_IWUSR,
1244 acpi_device_dir(device),
1245 &acpi_thermal_cooling_fops,
1246 acpi_driver_data(device));
1250 /* 'polling_frequency' [R/W] */
1251 entry = proc_create_data(ACPI_THERMAL_FILE_POLLING_FREQ,
1252 S_IFREG | S_IRUGO | S_IWUSR,
1253 acpi_device_dir(device),
1254 &acpi_thermal_polling_fops,
1255 acpi_driver_data(device));
1261 static int acpi_thermal_remove_fs(struct acpi_device *device)
1264 if (acpi_device_dir(device)) {
1265 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1266 acpi_device_dir(device));
1267 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1268 acpi_device_dir(device));
1269 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1270 acpi_device_dir(device));
1271 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1272 acpi_device_dir(device));
1273 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1274 acpi_device_dir(device));
1275 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1276 acpi_device_dir(device) = NULL;
1282 /* --------------------------------------------------------------------------
1284 -------------------------------------------------------------------------- */
1286 static void acpi_thermal_notify(struct acpi_device *device, u32 event)
1288 struct acpi_thermal *tz = acpi_driver_data(device);
1295 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1296 acpi_thermal_check(tz);
1298 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
1299 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
1300 acpi_thermal_check(tz);
1301 acpi_bus_generate_proc_event(device, event, 0);
1302 acpi_bus_generate_netlink_event(device->pnp.device_class,
1303 dev_name(&device->dev), event, 0);
1305 case ACPI_THERMAL_NOTIFY_DEVICES:
1306 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
1307 acpi_thermal_check(tz);
1308 acpi_bus_generate_proc_event(device, event, 0);
1309 acpi_bus_generate_netlink_event(device->pnp.device_class,
1310 dev_name(&device->dev), event, 0);
1313 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1314 "Unsupported event [0x%x]\n", event));
1319 static int acpi_thermal_get_info(struct acpi_thermal *tz)
1327 /* Get temperature [_TMP] (required) */
1328 result = acpi_thermal_get_temperature(tz);
1332 /* Get trip points [_CRT, _PSV, etc.] (required) */
1333 result = acpi_thermal_get_trip_points(tz);
1337 /* Set the cooling mode [_SCP] to active cooling (default) */
1338 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
1340 tz->flags.cooling_mode = 1;
1342 /* Get default polling frequency [_TZP] (optional) */
1344 tz->polling_frequency = tzp;
1346 acpi_thermal_get_polling_frequency(tz);
1352 * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI
1353 * handles temperature values with a single decimal place. As a consequence,
1354 * some implementations use an offset of 273.1 and others use an offset of
1355 * 273.2. Try to find out which one is being used, to present the most
1356 * accurate and visually appealing number.
1358 * The heuristic below should work for all ACPI thermal zones which have a
1359 * critical trip point with a value being a multiple of 0.5 degree Celsius.
1361 static void acpi_thermal_guess_offset(struct acpi_thermal *tz)
1363 if (tz->trips.critical.flags.valid &&
1364 (tz->trips.critical.temperature % 5) == 1)
1365 tz->kelvin_offset = 2731;
1367 tz->kelvin_offset = 2732;
1370 static int acpi_thermal_add(struct acpi_device *device)
1373 struct acpi_thermal *tz = NULL;
1379 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
1383 tz->device = device;
1384 strcpy(tz->name, device->pnp.bus_id);
1385 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1386 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1387 device->driver_data = tz;
1388 mutex_init(&tz->lock);
1391 result = acpi_thermal_get_info(tz);
1395 acpi_thermal_guess_offset(tz);
1397 result = acpi_thermal_register_thermal_zone(tz);
1401 result = acpi_thermal_add_fs(device);
1403 goto unregister_thermal_zone;
1405 printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
1406 acpi_device_name(device), acpi_device_bid(device),
1407 KELVIN_TO_CELSIUS(tz->temperature));
1410 unregister_thermal_zone:
1411 thermal_zone_device_unregister(tz->thermal_zone);
1418 static int acpi_thermal_remove(struct acpi_device *device, int type)
1420 struct acpi_thermal *tz = NULL;
1422 if (!device || !acpi_driver_data(device))
1425 tz = acpi_driver_data(device);
1427 acpi_thermal_remove_fs(device);
1428 acpi_thermal_unregister_thermal_zone(tz);
1429 mutex_destroy(&tz->lock);
1434 static int acpi_thermal_resume(struct acpi_device *device)
1436 struct acpi_thermal *tz = NULL;
1437 int i, j, power_state, result;
1440 if (!device || !acpi_driver_data(device))
1443 tz = acpi_driver_data(device);
1445 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1446 if (!(&tz->trips.active[i]))
1448 if (!tz->trips.active[i].flags.valid)
1450 tz->trips.active[i].flags.enabled = 1;
1451 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1452 result = acpi_bus_get_power(tz->trips.active[i].devices.
1453 handles[j], &power_state);
1454 if (result || (power_state != ACPI_STATE_D0)) {
1455 tz->trips.active[i].flags.enabled = 0;
1459 tz->state.active |= tz->trips.active[i].flags.enabled;
1462 acpi_thermal_check(tz);
1467 static int thermal_act(const struct dmi_system_id *d) {
1470 printk(KERN_NOTICE "ACPI: %s detected: "
1471 "disabling all active thermal trip points\n", d->ident);
1476 static int thermal_nocrt(const struct dmi_system_id *d) {
1478 printk(KERN_NOTICE "ACPI: %s detected: "
1479 "disabling all critical thermal trip point actions.\n", d->ident);
1483 static int thermal_tzp(const struct dmi_system_id *d) {
1486 printk(KERN_NOTICE "ACPI: %s detected: "
1487 "enabling thermal zone polling\n", d->ident);
1488 tzp = 300; /* 300 dS = 30 Seconds */
1492 static int thermal_psv(const struct dmi_system_id *d) {
1495 printk(KERN_NOTICE "ACPI: %s detected: "
1496 "disabling all passive thermal trip points\n", d->ident);
1502 static struct dmi_system_id thermal_dmi_table[] __initdata = {
1504 * Award BIOS on this AOpen makes thermal control almost worthless.
1505 * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1508 .callback = thermal_act,
1509 .ident = "AOpen i915GMm-HFS",
1511 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1512 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1516 .callback = thermal_psv,
1517 .ident = "AOpen i915GMm-HFS",
1519 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1520 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1524 .callback = thermal_tzp,
1525 .ident = "AOpen i915GMm-HFS",
1527 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1528 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1532 .callback = thermal_nocrt,
1533 .ident = "Gigabyte GA-7ZX",
1535 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
1536 DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
1542 static int __init acpi_thermal_init(void)
1546 dmi_check_system(thermal_dmi_table);
1549 printk(KERN_NOTICE "ACPI: thermal control disabled\n");
1552 acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1553 if (!acpi_thermal_dir)
1556 result = acpi_bus_register_driver(&acpi_thermal_driver);
1558 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1565 static void __exit acpi_thermal_exit(void)
1568 acpi_bus_unregister_driver(&acpi_thermal_driver);
1570 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1575 module_init(acpi_thermal_init);
1576 module_exit(acpi_thermal_exit);