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>
41 #ifdef CONFIG_ACPI_PROCFS
42 #include <linux/proc_fs.h>
43 #include <linux/seq_file.h>
46 #include <linux/jiffies.h>
47 #include <linux/kmod.h>
48 #include <linux/reboot.h>
49 #include <linux/device.h>
50 #include <asm/uaccess.h>
51 #include <linux/thermal.h>
52 #include <acpi/acpi_bus.h>
53 #include <acpi/acpi_drivers.h>
55 #define PREFIX "ACPI: "
57 #define ACPI_THERMAL_CLASS "thermal_zone"
58 #define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
59 #define ACPI_THERMAL_FILE_STATE "state"
60 #define ACPI_THERMAL_FILE_TEMPERATURE "temperature"
61 #define ACPI_THERMAL_FILE_TRIP_POINTS "trip_points"
62 #define ACPI_THERMAL_FILE_COOLING_MODE "cooling_mode"
63 #define ACPI_THERMAL_FILE_POLLING_FREQ "polling_frequency"
64 #define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
65 #define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
66 #define ACPI_THERMAL_NOTIFY_DEVICES 0x82
67 #define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
68 #define ACPI_THERMAL_NOTIFY_HOT 0xF1
69 #define ACPI_THERMAL_MODE_ACTIVE 0x00
71 #define ACPI_THERMAL_MAX_ACTIVE 10
72 #define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
74 #define _COMPONENT ACPI_THERMAL_COMPONENT
75 ACPI_MODULE_NAME("thermal");
77 MODULE_AUTHOR("Paul Diefenbaugh");
78 MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
79 MODULE_LICENSE("GPL");
82 module_param(act, int, 0644);
83 MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.");
86 module_param(crt, int, 0644);
87 MODULE_PARM_DESC(crt, "Disable or lower all critical trip points.");
90 module_param(tzp, int, 0444);
91 MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.");
94 module_param(nocrt, int, 0);
95 MODULE_PARM_DESC(nocrt, "Set to take no action upon ACPI thermal zone critical trips points.");
98 module_param(off, int, 0);
99 MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
102 module_param(psv, int, 0644);
103 MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
105 static int acpi_thermal_add(struct acpi_device *device);
106 static int acpi_thermal_remove(struct acpi_device *device, int type);
107 static int acpi_thermal_resume(struct acpi_device *device);
108 static void acpi_thermal_notify(struct acpi_device *device, u32 event);
110 static const struct acpi_device_id thermal_device_ids[] = {
111 {ACPI_THERMAL_HID, 0},
114 MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
116 static struct acpi_driver acpi_thermal_driver = {
118 .class = ACPI_THERMAL_CLASS,
119 .ids = thermal_device_ids,
121 .add = acpi_thermal_add,
122 .remove = acpi_thermal_remove,
123 .resume = acpi_thermal_resume,
124 .notify = acpi_thermal_notify,
128 struct acpi_thermal_state {
137 struct acpi_thermal_state_flags {
143 struct acpi_thermal_critical {
144 struct acpi_thermal_state_flags flags;
145 unsigned long temperature;
148 struct acpi_thermal_hot {
149 struct acpi_thermal_state_flags flags;
150 unsigned long temperature;
153 struct acpi_thermal_passive {
154 struct acpi_thermal_state_flags flags;
155 unsigned long temperature;
159 struct acpi_handle_list devices;
162 struct acpi_thermal_active {
163 struct acpi_thermal_state_flags flags;
164 unsigned long temperature;
165 struct acpi_handle_list devices;
168 struct acpi_thermal_trips {
169 struct acpi_thermal_critical critical;
170 struct acpi_thermal_hot hot;
171 struct acpi_thermal_passive passive;
172 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
175 struct acpi_thermal_flags {
176 u8 cooling_mode:1; /* _SCP */
177 u8 devices:1; /* _TZD */
181 struct acpi_thermal {
182 struct acpi_device * device;
184 unsigned long temperature;
185 unsigned long last_temperature;
186 unsigned long polling_frequency;
188 struct acpi_thermal_flags flags;
189 struct acpi_thermal_state state;
190 struct acpi_thermal_trips trips;
191 struct acpi_handle_list devices;
192 struct thermal_zone_device *thermal_zone;
198 #ifdef CONFIG_ACPI_PROCFS
199 static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
200 static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
201 static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
202 static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
203 static ssize_t acpi_thermal_write_cooling_mode(struct file *,
204 const char __user *, size_t,
206 static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
207 static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
210 static const struct file_operations acpi_thermal_state_fops = {
211 .owner = THIS_MODULE,
212 .open = acpi_thermal_state_open_fs,
215 .release = single_release,
218 static const struct file_operations acpi_thermal_temp_fops = {
219 .owner = THIS_MODULE,
220 .open = acpi_thermal_temp_open_fs,
223 .release = single_release,
226 static const struct file_operations acpi_thermal_trip_fops = {
227 .owner = THIS_MODULE,
228 .open = acpi_thermal_trip_open_fs,
231 .release = single_release,
234 static const struct file_operations acpi_thermal_cooling_fops = {
235 .owner = THIS_MODULE,
236 .open = acpi_thermal_cooling_open_fs,
238 .write = acpi_thermal_write_cooling_mode,
240 .release = single_release,
243 static const struct file_operations acpi_thermal_polling_fops = {
244 .owner = THIS_MODULE,
245 .open = acpi_thermal_polling_open_fs,
247 .write = acpi_thermal_write_polling,
249 .release = single_release,
251 #endif /* CONFIG_ACPI_PROCFS*/
253 /* --------------------------------------------------------------------------
254 Thermal Zone Management
255 -------------------------------------------------------------------------- */
257 static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
259 acpi_status status = AE_OK;
260 unsigned long long tmp;
265 tz->last_temperature = tz->temperature;
267 status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp);
268 if (ACPI_FAILURE(status))
271 tz->temperature = tmp;
272 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
278 static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
280 acpi_status status = AE_OK;
281 unsigned long long tmp;
286 status = acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, &tmp);
287 if (ACPI_FAILURE(status))
290 tz->polling_frequency = tmp;
291 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
292 tz->polling_frequency));
297 static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
299 acpi_status status = AE_OK;
300 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
301 struct acpi_object_list arg_list = { 1, &arg0 };
302 acpi_handle handle = NULL;
308 status = acpi_get_handle(tz->device->handle, "_SCP", &handle);
309 if (ACPI_FAILURE(status)) {
310 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
314 arg0.integer.value = mode;
316 status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
317 if (ACPI_FAILURE(status))
323 #define ACPI_TRIPS_CRITICAL 0x01
324 #define ACPI_TRIPS_HOT 0x02
325 #define ACPI_TRIPS_PASSIVE 0x04
326 #define ACPI_TRIPS_ACTIVE 0x08
327 #define ACPI_TRIPS_DEVICES 0x10
329 #define ACPI_TRIPS_REFRESH_THRESHOLDS (ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE)
330 #define ACPI_TRIPS_REFRESH_DEVICES ACPI_TRIPS_DEVICES
332 #define ACPI_TRIPS_INIT (ACPI_TRIPS_CRITICAL | ACPI_TRIPS_HOT | \
333 ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE | \
337 * This exception is thrown out in two cases:
338 * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
339 * when re-evaluating the AML code.
340 * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change.
341 * We need to re-bind the cooling devices of a thermal zone when this occurs.
343 #define ACPI_THERMAL_TRIPS_EXCEPTION(flags, str) \
345 if (flags != ACPI_TRIPS_INIT) \
346 ACPI_EXCEPTION((AE_INFO, AE_ERROR, \
347 "ACPI thermal trip point %s changed\n" \
348 "Please send acpidump to linux-acpi@vger.kernel.org\n", str)); \
351 static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
353 acpi_status status = AE_OK;
354 unsigned long long tmp;
355 struct acpi_handle_list devices;
359 /* Critical Shutdown */
360 if (flag & ACPI_TRIPS_CRITICAL) {
361 status = acpi_evaluate_integer(tz->device->handle,
363 tz->trips.critical.temperature = tmp;
365 * Treat freezing temperatures as invalid as well; some
366 * BIOSes return really low values and cause reboots at startup.
367 * Below zero (Celsius) values clearly aren't right for sure..
368 * ... so lets discard those as invalid.
370 if (ACPI_FAILURE(status)) {
371 tz->trips.critical.flags.valid = 0;
372 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
373 "No critical threshold\n"));
374 } else if (tmp <= 2732) {
375 printk(KERN_WARNING FW_BUG "Invalid critical threshold "
377 tz->trips.critical.flags.valid = 0;
379 tz->trips.critical.flags.valid = 1;
380 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
381 "Found critical threshold [%lu]\n",
382 tz->trips.critical.temperature));
384 if (tz->trips.critical.flags.valid == 1) {
386 tz->trips.critical.flags.valid = 0;
387 } else if (crt > 0) {
388 unsigned long crt_k = CELSIUS_TO_KELVIN(crt);
390 * Allow override critical threshold
392 if (crt_k > tz->trips.critical.temperature)
393 printk(KERN_WARNING PREFIX
394 "Critical threshold %d C\n", crt);
395 tz->trips.critical.temperature = crt_k;
400 /* Critical Sleep (optional) */
401 if (flag & ACPI_TRIPS_HOT) {
402 status = acpi_evaluate_integer(tz->device->handle,
404 if (ACPI_FAILURE(status)) {
405 tz->trips.hot.flags.valid = 0;
406 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
407 "No hot threshold\n"));
409 tz->trips.hot.temperature = tmp;
410 tz->trips.hot.flags.valid = 1;
411 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
412 "Found hot threshold [%lu]\n",
413 tz->trips.critical.temperature));
417 /* Passive (optional) */
418 if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.flags.valid) ||
419 (flag == ACPI_TRIPS_INIT)) {
420 valid = tz->trips.passive.flags.valid;
423 } else if (psv > 0) {
424 tmp = CELSIUS_TO_KELVIN(psv);
427 status = acpi_evaluate_integer(tz->device->handle,
431 if (ACPI_FAILURE(status))
432 tz->trips.passive.flags.valid = 0;
434 tz->trips.passive.temperature = tmp;
435 tz->trips.passive.flags.valid = 1;
436 if (flag == ACPI_TRIPS_INIT) {
437 status = acpi_evaluate_integer(
438 tz->device->handle, "_TC1",
440 if (ACPI_FAILURE(status))
441 tz->trips.passive.flags.valid = 0;
443 tz->trips.passive.tc1 = tmp;
444 status = acpi_evaluate_integer(
445 tz->device->handle, "_TC2",
447 if (ACPI_FAILURE(status))
448 tz->trips.passive.flags.valid = 0;
450 tz->trips.passive.tc2 = tmp;
451 status = acpi_evaluate_integer(
452 tz->device->handle, "_TSP",
454 if (ACPI_FAILURE(status))
455 tz->trips.passive.flags.valid = 0;
457 tz->trips.passive.tsp = tmp;
461 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.flags.valid) {
462 memset(&devices, 0, sizeof(struct acpi_handle_list));
463 status = acpi_evaluate_reference(tz->device->handle, "_PSL",
465 if (ACPI_FAILURE(status)) {
466 printk(KERN_WARNING PREFIX
467 "Invalid passive threshold\n");
468 tz->trips.passive.flags.valid = 0;
471 tz->trips.passive.flags.valid = 1;
473 if (memcmp(&tz->trips.passive.devices, &devices,
474 sizeof(struct acpi_handle_list))) {
475 memcpy(&tz->trips.passive.devices, &devices,
476 sizeof(struct acpi_handle_list));
477 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
480 if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
481 if (valid != tz->trips.passive.flags.valid)
482 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
485 /* Active (optional) */
486 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
487 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
488 valid = tz->trips.active[i].flags.valid;
491 break; /* disable all active trip points */
493 if ((flag == ACPI_TRIPS_INIT) || ((flag & ACPI_TRIPS_ACTIVE) &&
494 tz->trips.active[i].flags.valid)) {
495 status = acpi_evaluate_integer(tz->device->handle,
497 if (ACPI_FAILURE(status)) {
498 tz->trips.active[i].flags.valid = 0;
504 tz->trips.active[0].temperature =
505 CELSIUS_TO_KELVIN(act);
508 * Don't allow override higher than
509 * the next higher trip point
511 tz->trips.active[i - 1].temperature =
512 (tz->trips.active[i - 2].temperature <
513 CELSIUS_TO_KELVIN(act) ?
514 tz->trips.active[i - 2].temperature :
515 CELSIUS_TO_KELVIN(act));
518 tz->trips.active[i].temperature = tmp;
519 tz->trips.active[i].flags.valid = 1;
524 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].flags.valid ) {
525 memset(&devices, 0, sizeof(struct acpi_handle_list));
526 status = acpi_evaluate_reference(tz->device->handle,
527 name, NULL, &devices);
528 if (ACPI_FAILURE(status)) {
529 printk(KERN_WARNING PREFIX
530 "Invalid active%d threshold\n", i);
531 tz->trips.active[i].flags.valid = 0;
534 tz->trips.active[i].flags.valid = 1;
536 if (memcmp(&tz->trips.active[i].devices, &devices,
537 sizeof(struct acpi_handle_list))) {
538 memcpy(&tz->trips.active[i].devices, &devices,
539 sizeof(struct acpi_handle_list));
540 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
543 if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
544 if (valid != tz->trips.active[i].flags.valid)
545 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
547 if (!tz->trips.active[i].flags.valid)
551 if (flag & ACPI_TRIPS_DEVICES) {
552 memset(&devices, 0, sizeof(struct acpi_handle_list));
553 status = acpi_evaluate_reference(tz->device->handle, "_TZD",
555 if (memcmp(&tz->devices, &devices,
556 sizeof(struct acpi_handle_list))) {
557 memcpy(&tz->devices, &devices,
558 sizeof(struct acpi_handle_list));
559 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
566 static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
568 int i, valid, ret = acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
573 valid = tz->trips.critical.flags.valid |
574 tz->trips.hot.flags.valid |
575 tz->trips.passive.flags.valid;
577 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
578 valid |= tz->trips.active[i].flags.valid;
581 printk(KERN_WARNING FW_BUG "No valid trip found\n");
587 static void acpi_thermal_check(void *data)
589 struct acpi_thermal *tz = data;
591 thermal_zone_device_update(tz->thermal_zone);
594 /* sys I/F for generic thermal sysfs support */
595 #define KELVIN_TO_MILLICELSIUS(t, off) (((t) - (off)) * 100)
597 static int thermal_get_temp(struct thermal_zone_device *thermal,
600 struct acpi_thermal *tz = thermal->devdata;
606 result = acpi_thermal_get_temperature(tz);
610 *temp = KELVIN_TO_MILLICELSIUS(tz->temperature, tz->kelvin_offset);
614 static const char enabled[] = "kernel";
615 static const char disabled[] = "user";
616 static int thermal_get_mode(struct thermal_zone_device *thermal,
617 enum thermal_device_mode *mode)
619 struct acpi_thermal *tz = thermal->devdata;
624 *mode = tz->tz_enabled ? THERMAL_DEVICE_ENABLED :
625 THERMAL_DEVICE_DISABLED;
630 static int thermal_set_mode(struct thermal_zone_device *thermal,
631 enum thermal_device_mode mode)
633 struct acpi_thermal *tz = thermal->devdata;
640 * enable/disable thermal management from ACPI thermal driver
642 if (mode == THERMAL_DEVICE_ENABLED)
644 else if (mode == THERMAL_DEVICE_DISABLED)
649 if (enable != tz->tz_enabled) {
650 tz->tz_enabled = enable;
651 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
652 "%s ACPI thermal control\n",
653 tz->tz_enabled ? enabled : disabled));
654 acpi_thermal_check(tz);
659 static int thermal_get_trip_type(struct thermal_zone_device *thermal,
660 int trip, enum thermal_trip_type *type)
662 struct acpi_thermal *tz = thermal->devdata;
668 if (tz->trips.critical.flags.valid) {
670 *type = THERMAL_TRIP_CRITICAL;
676 if (tz->trips.hot.flags.valid) {
678 *type = THERMAL_TRIP_HOT;
684 if (tz->trips.passive.flags.valid) {
686 *type = THERMAL_TRIP_PASSIVE;
692 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
693 tz->trips.active[i].flags.valid; i++) {
695 *type = THERMAL_TRIP_ACTIVE;
704 static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
705 int trip, unsigned long *temp)
707 struct acpi_thermal *tz = thermal->devdata;
713 if (tz->trips.critical.flags.valid) {
715 *temp = KELVIN_TO_MILLICELSIUS(
716 tz->trips.critical.temperature,
723 if (tz->trips.hot.flags.valid) {
725 *temp = KELVIN_TO_MILLICELSIUS(
726 tz->trips.hot.temperature,
733 if (tz->trips.passive.flags.valid) {
735 *temp = KELVIN_TO_MILLICELSIUS(
736 tz->trips.passive.temperature,
743 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
744 tz->trips.active[i].flags.valid; i++) {
746 *temp = KELVIN_TO_MILLICELSIUS(
747 tz->trips.active[i].temperature,
757 static int thermal_get_crit_temp(struct thermal_zone_device *thermal,
758 unsigned long *temperature) {
759 struct acpi_thermal *tz = thermal->devdata;
761 if (tz->trips.critical.flags.valid) {
762 *temperature = KELVIN_TO_MILLICELSIUS(
763 tz->trips.critical.temperature,
770 static int thermal_notify(struct thermal_zone_device *thermal, int trip,
771 enum thermal_trip_type trip_type)
774 struct acpi_thermal *tz = thermal->devdata;
776 if (trip_type == THERMAL_TRIP_CRITICAL)
777 type = ACPI_THERMAL_NOTIFY_CRITICAL;
778 else if (trip_type == THERMAL_TRIP_HOT)
779 type = ACPI_THERMAL_NOTIFY_HOT;
783 acpi_bus_generate_proc_event(tz->device, type, 1);
784 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
785 dev_name(&tz->device->dev), type, 1);
787 if (trip_type == THERMAL_TRIP_CRITICAL && nocrt)
793 typedef int (*cb)(struct thermal_zone_device *, int,
794 struct thermal_cooling_device *);
795 static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
796 struct thermal_cooling_device *cdev,
799 struct acpi_device *device = cdev->devdata;
800 struct acpi_thermal *tz = thermal->devdata;
801 struct acpi_device *dev;
809 if (tz->trips.critical.flags.valid)
812 if (tz->trips.hot.flags.valid)
815 if (tz->trips.passive.flags.valid) {
817 for (i = 0; i < tz->trips.passive.devices.count;
819 handle = tz->trips.passive.devices.handles[i];
820 status = acpi_bus_get_device(handle, &dev);
821 if (ACPI_SUCCESS(status) && (dev == device)) {
822 result = action(thermal, trip, cdev);
829 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
830 if (!tz->trips.active[i].flags.valid)
834 j < tz->trips.active[i].devices.count;
836 handle = tz->trips.active[i].devices.handles[j];
837 status = acpi_bus_get_device(handle, &dev);
838 if (ACPI_SUCCESS(status) && (dev == device)) {
839 result = action(thermal, trip, cdev);
846 for (i = 0; i < tz->devices.count; i++) {
847 handle = tz->devices.handles[i];
848 status = acpi_bus_get_device(handle, &dev);
849 if (ACPI_SUCCESS(status) && (dev == device)) {
850 result = action(thermal, -1, cdev);
861 acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
862 struct thermal_cooling_device *cdev)
864 return acpi_thermal_cooling_device_cb(thermal, cdev,
865 thermal_zone_bind_cooling_device);
869 acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
870 struct thermal_cooling_device *cdev)
872 return acpi_thermal_cooling_device_cb(thermal, cdev,
873 thermal_zone_unbind_cooling_device);
876 static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
877 .bind = acpi_thermal_bind_cooling_device,
878 .unbind = acpi_thermal_unbind_cooling_device,
879 .get_temp = thermal_get_temp,
880 .get_mode = thermal_get_mode,
881 .set_mode = thermal_set_mode,
882 .get_trip_type = thermal_get_trip_type,
883 .get_trip_temp = thermal_get_trip_temp,
884 .get_crit_temp = thermal_get_crit_temp,
885 .notify = thermal_notify,
888 static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
895 if (tz->trips.critical.flags.valid)
898 if (tz->trips.hot.flags.valid)
901 if (tz->trips.passive.flags.valid)
904 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
905 tz->trips.active[i].flags.valid; i++, trips++);
907 if (tz->trips.passive.flags.valid)
909 thermal_zone_device_register("acpitz", trips, tz,
910 &acpi_thermal_zone_ops,
911 tz->trips.passive.tc1,
912 tz->trips.passive.tc2,
913 tz->trips.passive.tsp*100,
914 tz->polling_frequency*100);
917 thermal_zone_device_register("acpitz", trips, tz,
918 &acpi_thermal_zone_ops,
920 tz->polling_frequency*100);
921 if (IS_ERR(tz->thermal_zone))
924 result = sysfs_create_link(&tz->device->dev.kobj,
925 &tz->thermal_zone->device.kobj, "thermal_zone");
929 result = sysfs_create_link(&tz->thermal_zone->device.kobj,
930 &tz->device->dev.kobj, "device");
934 status = acpi_attach_data(tz->device->handle,
935 acpi_bus_private_data_handler,
937 if (ACPI_FAILURE(status)) {
938 printk(KERN_ERR PREFIX
939 "Error attaching device data\n");
945 dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
946 tz->thermal_zone->id);
950 static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
952 sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
953 sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
954 thermal_zone_device_unregister(tz->thermal_zone);
955 tz->thermal_zone = NULL;
956 acpi_detach_data(tz->device->handle, acpi_bus_private_data_handler);
960 /* --------------------------------------------------------------------------
962 -------------------------------------------------------------------------- */
963 #ifdef CONFIG_ACPI_PROCFS
964 static struct proc_dir_entry *acpi_thermal_dir;
966 static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
968 struct acpi_thermal *tz = seq->private;
974 seq_puts(seq, "state: ");
976 if (!tz->state.critical && !tz->state.hot && !tz->state.passive
977 && !tz->state.active)
978 seq_puts(seq, "ok\n");
980 if (tz->state.critical)
981 seq_puts(seq, "critical ");
983 seq_puts(seq, "hot ");
984 if (tz->state.passive)
985 seq_puts(seq, "passive ");
986 if (tz->state.active)
987 seq_printf(seq, "active[%d]", tz->state.active_index);
995 static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
997 return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
1000 static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
1003 struct acpi_thermal *tz = seq->private;
1009 result = acpi_thermal_get_temperature(tz);
1013 seq_printf(seq, "temperature: %ld C\n",
1014 KELVIN_TO_CELSIUS(tz->temperature));
1020 static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
1022 return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
1025 static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
1027 struct acpi_thermal *tz = seq->private;
1028 struct acpi_device *device;
1038 if (tz->trips.critical.flags.valid)
1039 seq_printf(seq, "critical (S5): %ld C%s",
1040 KELVIN_TO_CELSIUS(tz->trips.critical.temperature),
1041 nocrt ? " <disabled>\n" : "\n");
1043 if (tz->trips.hot.flags.valid)
1044 seq_printf(seq, "hot (S4): %ld C%s",
1045 KELVIN_TO_CELSIUS(tz->trips.hot.temperature),
1046 nocrt ? " <disabled>\n" : "\n");
1048 if (tz->trips.passive.flags.valid) {
1050 "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
1051 KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
1052 tz->trips.passive.tc1, tz->trips.passive.tc2,
1053 tz->trips.passive.tsp);
1054 for (j = 0; j < tz->trips.passive.devices.count; j++) {
1055 status = acpi_bus_get_device(tz->trips.passive.devices.
1056 handles[j], &device);
1057 seq_printf(seq, "%4.4s ", status ? "" :
1058 acpi_device_bid(device));
1060 seq_puts(seq, "\n");
1062 seq_printf(seq, "passive (forced):");
1063 if (tz->thermal_zone->forced_passive)
1064 seq_printf(seq, " %i C\n",
1065 tz->thermal_zone->forced_passive / 1000);
1067 seq_printf(seq, "<not set>\n");
1070 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1071 if (!(tz->trips.active[i].flags.valid))
1073 seq_printf(seq, "active[%d]: %ld C: devices=",
1075 KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
1076 for (j = 0; j < tz->trips.active[i].devices.count; j++){
1077 status = acpi_bus_get_device(tz->trips.active[i].
1080 seq_printf(seq, "%4.4s ", status ? "" :
1081 acpi_device_bid(device));
1083 seq_puts(seq, "\n");
1090 static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
1092 return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
1095 static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
1097 struct acpi_thermal *tz = seq->private;
1103 if (!tz->flags.cooling_mode)
1104 seq_puts(seq, "<setting not supported>\n");
1106 seq_puts(seq, "0 - Active; 1 - Passive\n");
1112 static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
1114 return single_open(file, acpi_thermal_cooling_seq_show,
1119 acpi_thermal_write_cooling_mode(struct file *file,
1120 const char __user * buffer,
1121 size_t count, loff_t * ppos)
1123 struct seq_file *m = file->private_data;
1124 struct acpi_thermal *tz = m->private;
1126 char mode_string[12] = { '\0' };
1129 if (!tz || (count > sizeof(mode_string) - 1))
1132 if (!tz->flags.cooling_mode)
1135 if (copy_from_user(mode_string, buffer, count))
1138 mode_string[count] = '\0';
1140 result = acpi_thermal_set_cooling_mode(tz,
1141 simple_strtoul(mode_string, NULL,
1146 acpi_thermal_check(tz);
1151 static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
1153 struct acpi_thermal *tz = seq->private;
1159 if (!tz->thermal_zone->polling_delay) {
1160 seq_puts(seq, "<polling disabled>\n");
1164 seq_printf(seq, "polling frequency: %d seconds\n",
1165 (tz->thermal_zone->polling_delay / 1000));
1171 static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
1173 return single_open(file, acpi_thermal_polling_seq_show,
1177 static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
1182 /* Convert value to deci-seconds */
1183 tz->polling_frequency = seconds * 10;
1185 tz->thermal_zone->polling_delay = seconds * 1000;
1188 thermal_zone_device_update(tz->thermal_zone);
1190 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1191 "Polling frequency set to %lu seconds\n",
1192 tz->polling_frequency/10));
1198 acpi_thermal_write_polling(struct file *file,
1199 const char __user * buffer,
1200 size_t count, loff_t * ppos)
1202 struct seq_file *m = file->private_data;
1203 struct acpi_thermal *tz = m->private;
1205 char polling_string[12] = { '\0' };
1209 if (!tz || (count > sizeof(polling_string) - 1))
1212 if (copy_from_user(polling_string, buffer, count))
1215 polling_string[count] = '\0';
1217 seconds = simple_strtoul(polling_string, NULL, 0);
1219 result = acpi_thermal_set_polling(tz, seconds);
1223 acpi_thermal_check(tz);
1228 static int acpi_thermal_add_fs(struct acpi_device *device)
1230 struct proc_dir_entry *entry = NULL;
1233 if (!acpi_device_dir(device)) {
1234 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
1236 if (!acpi_device_dir(device))
1241 entry = proc_create_data(ACPI_THERMAL_FILE_STATE,
1242 S_IRUGO, acpi_device_dir(device),
1243 &acpi_thermal_state_fops,
1244 acpi_driver_data(device));
1248 /* 'temperature' [R] */
1249 entry = proc_create_data(ACPI_THERMAL_FILE_TEMPERATURE,
1250 S_IRUGO, acpi_device_dir(device),
1251 &acpi_thermal_temp_fops,
1252 acpi_driver_data(device));
1256 /* 'trip_points' [R] */
1257 entry = proc_create_data(ACPI_THERMAL_FILE_TRIP_POINTS,
1259 acpi_device_dir(device),
1260 &acpi_thermal_trip_fops,
1261 acpi_driver_data(device));
1265 /* 'cooling_mode' [R/W] */
1266 entry = proc_create_data(ACPI_THERMAL_FILE_COOLING_MODE,
1267 S_IFREG | S_IRUGO | S_IWUSR,
1268 acpi_device_dir(device),
1269 &acpi_thermal_cooling_fops,
1270 acpi_driver_data(device));
1274 /* 'polling_frequency' [R/W] */
1275 entry = proc_create_data(ACPI_THERMAL_FILE_POLLING_FREQ,
1276 S_IFREG | S_IRUGO | S_IWUSR,
1277 acpi_device_dir(device),
1278 &acpi_thermal_polling_fops,
1279 acpi_driver_data(device));
1285 static int acpi_thermal_remove_fs(struct acpi_device *device)
1288 if (acpi_device_dir(device)) {
1289 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1290 acpi_device_dir(device));
1291 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1292 acpi_device_dir(device));
1293 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1294 acpi_device_dir(device));
1295 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1296 acpi_device_dir(device));
1297 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1298 acpi_device_dir(device));
1299 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1300 acpi_device_dir(device) = NULL;
1306 static inline int acpi_thermal_add_fs(struct acpi_device *device) { return 0; }
1307 static inline int acpi_thermal_remove_fs(struct acpi_device *device)
1311 #endif /* CONFIG_ACPI_PROCFS */
1312 /* --------------------------------------------------------------------------
1314 -------------------------------------------------------------------------- */
1316 static void acpi_thermal_notify(struct acpi_device *device, u32 event)
1318 struct acpi_thermal *tz = acpi_driver_data(device);
1325 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1326 acpi_thermal_check(tz);
1328 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
1329 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
1330 acpi_thermal_check(tz);
1331 acpi_bus_generate_proc_event(device, event, 0);
1332 acpi_bus_generate_netlink_event(device->pnp.device_class,
1333 dev_name(&device->dev), event, 0);
1335 case ACPI_THERMAL_NOTIFY_DEVICES:
1336 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
1337 acpi_thermal_check(tz);
1338 acpi_bus_generate_proc_event(device, event, 0);
1339 acpi_bus_generate_netlink_event(device->pnp.device_class,
1340 dev_name(&device->dev), event, 0);
1343 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1344 "Unsupported event [0x%x]\n", event));
1349 static int acpi_thermal_get_info(struct acpi_thermal *tz)
1357 /* Get temperature [_TMP] (required) */
1358 result = acpi_thermal_get_temperature(tz);
1362 /* Get trip points [_CRT, _PSV, etc.] (required) */
1363 result = acpi_thermal_get_trip_points(tz);
1367 /* Set the cooling mode [_SCP] to active cooling (default) */
1368 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
1370 tz->flags.cooling_mode = 1;
1372 /* Get default polling frequency [_TZP] (optional) */
1374 tz->polling_frequency = tzp;
1376 acpi_thermal_get_polling_frequency(tz);
1382 * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI
1383 * handles temperature values with a single decimal place. As a consequence,
1384 * some implementations use an offset of 273.1 and others use an offset of
1385 * 273.2. Try to find out which one is being used, to present the most
1386 * accurate and visually appealing number.
1388 * The heuristic below should work for all ACPI thermal zones which have a
1389 * critical trip point with a value being a multiple of 0.5 degree Celsius.
1391 static void acpi_thermal_guess_offset(struct acpi_thermal *tz)
1393 if (tz->trips.critical.flags.valid &&
1394 (tz->trips.critical.temperature % 5) == 1)
1395 tz->kelvin_offset = 2731;
1397 tz->kelvin_offset = 2732;
1400 static int acpi_thermal_add(struct acpi_device *device)
1403 struct acpi_thermal *tz = NULL;
1409 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
1413 tz->device = device;
1414 strcpy(tz->name, device->pnp.bus_id);
1415 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1416 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1417 device->driver_data = tz;
1418 mutex_init(&tz->lock);
1421 result = acpi_thermal_get_info(tz);
1425 acpi_thermal_guess_offset(tz);
1427 result = acpi_thermal_register_thermal_zone(tz);
1431 result = acpi_thermal_add_fs(device);
1433 goto unregister_thermal_zone;
1435 printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
1436 acpi_device_name(device), acpi_device_bid(device),
1437 KELVIN_TO_CELSIUS(tz->temperature));
1440 unregister_thermal_zone:
1441 thermal_zone_device_unregister(tz->thermal_zone);
1448 static int acpi_thermal_remove(struct acpi_device *device, int type)
1450 struct acpi_thermal *tz = NULL;
1452 if (!device || !acpi_driver_data(device))
1455 tz = acpi_driver_data(device);
1457 acpi_thermal_remove_fs(device);
1458 acpi_thermal_unregister_thermal_zone(tz);
1459 mutex_destroy(&tz->lock);
1464 static int acpi_thermal_resume(struct acpi_device *device)
1466 struct acpi_thermal *tz = NULL;
1467 int i, j, power_state, result;
1470 if (!device || !acpi_driver_data(device))
1473 tz = acpi_driver_data(device);
1475 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1476 if (!(&tz->trips.active[i]))
1478 if (!tz->trips.active[i].flags.valid)
1480 tz->trips.active[i].flags.enabled = 1;
1481 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1482 result = acpi_bus_get_power(tz->trips.active[i].devices.
1483 handles[j], &power_state);
1484 if (result || (power_state != ACPI_STATE_D0)) {
1485 tz->trips.active[i].flags.enabled = 0;
1489 tz->state.active |= tz->trips.active[i].flags.enabled;
1492 acpi_thermal_check(tz);
1497 static int thermal_act(const struct dmi_system_id *d) {
1500 printk(KERN_NOTICE "ACPI: %s detected: "
1501 "disabling all active thermal trip points\n", d->ident);
1506 static int thermal_nocrt(const struct dmi_system_id *d) {
1508 printk(KERN_NOTICE "ACPI: %s detected: "
1509 "disabling all critical thermal trip point actions.\n", d->ident);
1513 static int thermal_tzp(const struct dmi_system_id *d) {
1516 printk(KERN_NOTICE "ACPI: %s detected: "
1517 "enabling thermal zone polling\n", d->ident);
1518 tzp = 300; /* 300 dS = 30 Seconds */
1522 static int thermal_psv(const struct dmi_system_id *d) {
1525 printk(KERN_NOTICE "ACPI: %s detected: "
1526 "disabling all passive thermal trip points\n", d->ident);
1532 static struct dmi_system_id thermal_dmi_table[] __initdata = {
1534 * Award BIOS on this AOpen makes thermal control almost worthless.
1535 * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1538 .callback = thermal_act,
1539 .ident = "AOpen i915GMm-HFS",
1541 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1542 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1546 .callback = thermal_psv,
1547 .ident = "AOpen i915GMm-HFS",
1549 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1550 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1554 .callback = thermal_tzp,
1555 .ident = "AOpen i915GMm-HFS",
1557 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1558 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1562 .callback = thermal_nocrt,
1563 .ident = "Gigabyte GA-7ZX",
1565 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
1566 DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
1572 static int __init acpi_thermal_init(void)
1576 dmi_check_system(thermal_dmi_table);
1579 printk(KERN_NOTICE "ACPI: thermal control disabled\n");
1583 #ifdef CONFIG_ACPI_PROCFS
1584 acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1585 if (!acpi_thermal_dir)
1589 result = acpi_bus_register_driver(&acpi_thermal_driver);
1591 #ifdef CONFIG_ACPI_PROCFS
1592 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1600 static void __exit acpi_thermal_exit(void)
1603 acpi_bus_unregister_driver(&acpi_thermal_driver);
1605 #ifdef CONFIG_ACPI_PROCFS
1606 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1612 module_init(acpi_thermal_init);
1613 module_exit(acpi_thermal_exit);