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/init.h>
37 #include <linux/types.h>
38 #include <linux/proc_fs.h>
39 #include <linux/timer.h>
40 #include <linux/jiffies.h>
41 #include <linux/kmod.h>
42 #include <linux/seq_file.h>
43 #include <linux/reboot.h>
44 #include <asm/uaccess.h>
46 #include <acpi/acpi_bus.h>
47 #include <acpi/acpi_drivers.h>
49 #define ACPI_THERMAL_COMPONENT 0x04000000
50 #define ACPI_THERMAL_CLASS "thermal_zone"
51 #define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
52 #define ACPI_THERMAL_FILE_STATE "state"
53 #define ACPI_THERMAL_FILE_TEMPERATURE "temperature"
54 #define ACPI_THERMAL_FILE_TRIP_POINTS "trip_points"
55 #define ACPI_THERMAL_FILE_COOLING_MODE "cooling_mode"
56 #define ACPI_THERMAL_FILE_POLLING_FREQ "polling_frequency"
57 #define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
58 #define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
59 #define ACPI_THERMAL_NOTIFY_DEVICES 0x82
60 #define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
61 #define ACPI_THERMAL_NOTIFY_HOT 0xF1
62 #define ACPI_THERMAL_MODE_ACTIVE 0x00
64 #define ACPI_THERMAL_MAX_ACTIVE 10
65 #define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
67 #define KELVIN_TO_CELSIUS(t) (long)(((long)t-2732>=0) ? ((long)t-2732+5)/10 : ((long)t-2732-5)/10)
68 #define CELSIUS_TO_KELVIN(t) ((t+273)*10)
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.\n");
82 module_param(tzp, int, 0444);
83 MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.\n");
86 module_param(nocrt, int, 0);
87 MODULE_PARM_DESC(nocrt, "Set to disable action on ACPI thermal zone critical and hot trips.\n");
90 module_param(off, int, 0);
91 MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.\n");
94 module_param(psv, int, 0644);
95 MODULE_PARM_DESC(psv, "Disable or override all passive trip points.\n");
97 static int acpi_thermal_add(struct acpi_device *device);
98 static int acpi_thermal_remove(struct acpi_device *device, int type);
99 static int acpi_thermal_resume(struct acpi_device *device);
100 static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
101 static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
102 static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
103 static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
104 static ssize_t acpi_thermal_write_cooling_mode(struct file *,
105 const char __user *, size_t,
107 static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
108 static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
111 static const struct acpi_device_id thermal_device_ids[] = {
112 {ACPI_THERMAL_HID, 0},
115 MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
117 static struct acpi_driver acpi_thermal_driver = {
119 .class = ACPI_THERMAL_CLASS,
120 .ids = thermal_device_ids,
122 .add = acpi_thermal_add,
123 .remove = acpi_thermal_remove,
124 .resume = acpi_thermal_resume,
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 timer_list timer;
195 static const struct file_operations acpi_thermal_state_fops = {
196 .open = acpi_thermal_state_open_fs,
199 .release = single_release,
202 static const struct file_operations acpi_thermal_temp_fops = {
203 .open = acpi_thermal_temp_open_fs,
206 .release = single_release,
209 static const struct file_operations acpi_thermal_trip_fops = {
210 .open = acpi_thermal_trip_open_fs,
213 .release = single_release,
216 static const struct file_operations acpi_thermal_cooling_fops = {
217 .open = acpi_thermal_cooling_open_fs,
219 .write = acpi_thermal_write_cooling_mode,
221 .release = single_release,
224 static const struct file_operations acpi_thermal_polling_fops = {
225 .open = acpi_thermal_polling_open_fs,
227 .write = acpi_thermal_write_polling,
229 .release = single_release,
232 /* --------------------------------------------------------------------------
233 Thermal Zone Management
234 -------------------------------------------------------------------------- */
236 static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
238 acpi_status status = AE_OK;
244 tz->last_temperature = tz->temperature;
247 acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tz->temperature);
248 if (ACPI_FAILURE(status))
251 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
257 static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
259 acpi_status status = AE_OK;
266 acpi_evaluate_integer(tz->device->handle, "_TZP", NULL,
267 &tz->polling_frequency);
268 if (ACPI_FAILURE(status))
271 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
272 tz->polling_frequency));
277 static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
283 tz->polling_frequency = seconds * 10; /* Convert value to deci-seconds */
285 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
286 "Polling frequency set to %lu seconds\n",
287 tz->polling_frequency/10));
292 static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
294 acpi_status status = AE_OK;
295 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
296 struct acpi_object_list arg_list = { 1, &arg0 };
297 acpi_handle handle = NULL;
303 status = acpi_get_handle(tz->device->handle, "_SCP", &handle);
304 if (ACPI_FAILURE(status)) {
305 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
309 arg0.integer.value = mode;
311 status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
312 if (ACPI_FAILURE(status))
318 static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
320 acpi_status status = AE_OK;
327 /* Critical Shutdown (required) */
329 status = acpi_evaluate_integer(tz->device->handle, "_CRT", NULL,
330 &tz->trips.critical.temperature);
331 if (ACPI_FAILURE(status)) {
332 tz->trips.critical.flags.valid = 0;
333 ACPI_EXCEPTION((AE_INFO, status, "No critical threshold"));
336 tz->trips.critical.flags.valid = 1;
337 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
338 "Found critical threshold [%lu]\n",
339 tz->trips.critical.temperature));
342 /* Critical Sleep (optional) */
345 acpi_evaluate_integer(tz->device->handle, "_HOT", NULL,
346 &tz->trips.hot.temperature);
347 if (ACPI_FAILURE(status)) {
348 tz->trips.hot.flags.valid = 0;
349 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No hot threshold\n"));
351 tz->trips.hot.flags.valid = 1;
352 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found hot threshold [%lu]\n",
353 tz->trips.hot.temperature));
356 /* Passive: Processors (optional) */
360 } else if (psv > 0) {
361 tz->trips.passive.temperature = CELSIUS_TO_KELVIN(psv);
364 status = acpi_evaluate_integer(tz->device->handle,
365 "_PSV", NULL, &tz->trips.passive.temperature);
368 if (ACPI_FAILURE(status)) {
369 tz->trips.passive.flags.valid = 0;
370 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No passive threshold\n"));
372 tz->trips.passive.flags.valid = 1;
375 acpi_evaluate_integer(tz->device->handle, "_TC1", NULL,
376 &tz->trips.passive.tc1);
377 if (ACPI_FAILURE(status))
378 tz->trips.passive.flags.valid = 0;
381 acpi_evaluate_integer(tz->device->handle, "_TC2", NULL,
382 &tz->trips.passive.tc2);
383 if (ACPI_FAILURE(status))
384 tz->trips.passive.flags.valid = 0;
387 acpi_evaluate_integer(tz->device->handle, "_TSP", NULL,
388 &tz->trips.passive.tsp);
389 if (ACPI_FAILURE(status))
390 tz->trips.passive.flags.valid = 0;
393 acpi_evaluate_reference(tz->device->handle, "_PSL", NULL,
394 &tz->trips.passive.devices);
395 if (ACPI_FAILURE(status))
396 tz->trips.passive.flags.valid = 0;
398 if (!tz->trips.passive.flags.valid)
399 printk(KERN_WARNING PREFIX "Invalid passive threshold\n");
401 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
402 "Found passive threshold [%lu]\n",
403 tz->trips.passive.temperature));
406 /* Active: Fans, etc. (optional) */
408 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
410 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
413 break; /* disable all active trip points */
415 status = acpi_evaluate_integer(tz->device->handle,
416 name, NULL, &tz->trips.active[i].temperature);
418 if (ACPI_FAILURE(status)) {
419 if (i == 0) /* no active trip points */
421 if (act <= 0) /* no override requested */
423 if (i == 1) { /* 1 trip point */
424 tz->trips.active[0].temperature =
425 CELSIUS_TO_KELVIN(act);
426 } else { /* multiple trips */
428 * Don't allow override higher than
429 * the next higher trip point
431 tz->trips.active[i - 1].temperature =
432 (tz->trips.active[i - 2].temperature <
433 CELSIUS_TO_KELVIN(act) ?
434 tz->trips.active[i - 2].temperature :
435 CELSIUS_TO_KELVIN(act));
442 acpi_evaluate_reference(tz->device->handle, name, NULL,
443 &tz->trips.active[i].devices);
444 if (ACPI_SUCCESS(status)) {
445 tz->trips.active[i].flags.valid = 1;
446 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
447 "Found active threshold [%d]:[%lu]\n",
448 i, tz->trips.active[i].temperature));
450 ACPI_EXCEPTION((AE_INFO, status,
451 "Invalid active threshold [%d]", i));
457 static int acpi_thermal_get_devices(struct acpi_thermal *tz)
459 acpi_status status = AE_OK;
466 acpi_evaluate_reference(tz->device->handle, "_TZD", NULL, &tz->devices);
467 if (ACPI_FAILURE(status))
473 static int acpi_thermal_critical(struct acpi_thermal *tz)
475 if (!tz || !tz->trips.critical.flags.valid || nocrt)
478 if (tz->temperature >= tz->trips.critical.temperature) {
479 printk(KERN_WARNING PREFIX "Critical trip point\n");
480 tz->trips.critical.flags.enabled = 1;
481 } else if (tz->trips.critical.flags.enabled)
482 tz->trips.critical.flags.enabled = 0;
485 "Critical temperature reached (%ld C), shutting down.\n",
486 KELVIN_TO_CELSIUS(tz->temperature));
487 acpi_bus_generate_event(tz->device, ACPI_THERMAL_NOTIFY_CRITICAL,
488 tz->trips.critical.flags.enabled);
490 orderly_poweroff(true);
495 static int acpi_thermal_hot(struct acpi_thermal *tz)
497 if (!tz || !tz->trips.hot.flags.valid || nocrt)
500 if (tz->temperature >= tz->trips.hot.temperature) {
501 printk(KERN_WARNING PREFIX "Hot trip point\n");
502 tz->trips.hot.flags.enabled = 1;
503 } else if (tz->trips.hot.flags.enabled)
504 tz->trips.hot.flags.enabled = 0;
506 acpi_bus_generate_event(tz->device, ACPI_THERMAL_NOTIFY_HOT,
507 tz->trips.hot.flags.enabled);
509 /* TBD: Call user-mode "sleep(S4)" function */
514 static void acpi_thermal_passive(struct acpi_thermal *tz)
517 struct acpi_thermal_passive *passive = NULL;
522 if (!tz || !tz->trips.passive.flags.valid)
525 passive = &(tz->trips.passive);
530 * Calculate the thermal trend (using the passive cooling equation)
531 * and modify the performance limit for all passive cooling devices
532 * accordingly. Note that we assume symmetry.
534 if (tz->temperature >= passive->temperature) {
536 (passive->tc1 * (tz->temperature - tz->last_temperature)) +
537 (passive->tc2 * (tz->temperature - passive->temperature));
538 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
539 "trend[%d]=(tc1[%lu]*(tmp[%lu]-last[%lu]))+(tc2[%lu]*(tmp[%lu]-psv[%lu]))\n",
540 trend, passive->tc1, tz->temperature,
541 tz->last_temperature, passive->tc2,
542 tz->temperature, passive->temperature));
543 passive->flags.enabled = 1;
546 for (i = 0; i < passive->devices.count; i++)
547 acpi_processor_set_thermal_limit(passive->
550 ACPI_PROCESSOR_LIMIT_INCREMENT);
552 else if (trend < 0) {
553 for (i = 0; i < passive->devices.count; i++)
555 * assume that we are on highest
556 * freq/lowest thrott and can leave
557 * passive mode, even in error case
559 if (!acpi_processor_set_thermal_limit
560 (passive->devices.handles[i],
561 ACPI_PROCESSOR_LIMIT_DECREMENT))
564 * Leave cooling mode, even if the temp might
565 * higher than trip point This is because some
566 * machines might have long thermal polling
567 * frequencies (tsp) defined. We will fall back
568 * into passive mode in next cycle (probably quicker)
571 passive->flags.enabled = 0;
572 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
573 "Disabling passive cooling, still above threshold,"
574 " but we are cooling down\n"));
583 * Implement passive cooling hysteresis to slowly increase performance
584 * and avoid thrashing around the passive trip point. Note that we
587 if (!passive->flags.enabled)
589 for (i = 0; i < passive->devices.count; i++)
590 if (!acpi_processor_set_thermal_limit
591 (passive->devices.handles[i],
592 ACPI_PROCESSOR_LIMIT_DECREMENT))
595 passive->flags.enabled = 0;
596 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
597 "Disabling passive cooling (zone is cool)\n"));
601 static void acpi_thermal_active(struct acpi_thermal *tz)
604 struct acpi_thermal_active *active = NULL;
607 unsigned long maxtemp = 0;
613 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
614 active = &(tz->trips.active[i]);
615 if (!active || !active->flags.valid)
617 if (tz->temperature >= active->temperature) {
621 * If not already enabled, turn ON all cooling devices
622 * associated with this active threshold.
624 if (active->temperature > maxtemp)
625 tz->state.active_index = i;
626 maxtemp = active->temperature;
627 if (active->flags.enabled)
629 for (j = 0; j < active->devices.count; j++) {
631 acpi_bus_set_power(active->devices.
635 printk(KERN_WARNING PREFIX
636 "Unable to turn cooling device [%p] 'on'\n",
641 active->flags.enabled = 1;
642 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
643 "Cooling device [%p] now 'on'\n",
644 active->devices.handles[j]));
648 if (!active->flags.enabled)
653 * Turn OFF all cooling devices associated with this
656 for (j = 0; j < active->devices.count; j++) {
657 result = acpi_bus_set_power(active->devices.handles[j],
660 printk(KERN_WARNING PREFIX
661 "Unable to turn cooling device [%p] 'off'\n",
662 active->devices.handles[j]);
665 active->flags.enabled = 0;
666 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
667 "Cooling device [%p] now 'off'\n",
668 active->devices.handles[j]));
673 static void acpi_thermal_check(void *context);
675 static void acpi_thermal_run(unsigned long data)
677 struct acpi_thermal *tz = (struct acpi_thermal *)data;
679 acpi_os_execute(OSL_GPE_HANDLER, acpi_thermal_check, (void *)data);
682 static void acpi_thermal_check(void *data)
685 struct acpi_thermal *tz = data;
686 unsigned long sleep_time = 0;
688 struct acpi_thermal_state state;
692 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
698 result = acpi_thermal_get_temperature(tz);
702 memset(&tz->state, 0, sizeof(tz->state));
707 * Compare the current temperature to the trip point values to see
708 * if we've entered one of the thermal policy states. Note that
709 * this function determines when a state is entered, but the
710 * individual policy decides when it is exited (e.g. hysteresis).
712 if (tz->trips.critical.flags.valid)
714 (tz->temperature >= tz->trips.critical.temperature);
715 if (tz->trips.hot.flags.valid)
716 state.hot |= (tz->temperature >= tz->trips.hot.temperature);
717 if (tz->trips.passive.flags.valid)
719 (tz->temperature >= tz->trips.passive.temperature);
720 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
721 if (tz->trips.active[i].flags.valid)
724 tz->trips.active[i].temperature);
729 * Separated from the above check to allow individual policy to
730 * determine when to exit a given state.
733 acpi_thermal_critical(tz);
735 acpi_thermal_hot(tz);
737 acpi_thermal_passive(tz);
739 acpi_thermal_active(tz);
744 * Again, separated from the above two to allow independent policy
747 tz->state.critical = tz->trips.critical.flags.enabled;
748 tz->state.hot = tz->trips.hot.flags.enabled;
749 tz->state.passive = tz->trips.passive.flags.enabled;
750 tz->state.active = 0;
751 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
752 tz->state.active |= tz->trips.active[i].flags.enabled;
755 * Calculate Sleep Time
756 * --------------------
757 * If we're in the passive state, use _TSP's value. Otherwise
758 * use the default polling frequency (e.g. _TZP). If no polling
759 * frequency is specified then we'll wait forever (at least until
760 * a thermal event occurs). Note that _TSP and _TZD values are
761 * given in 1/10th seconds (we must covert to milliseconds).
763 if (tz->state.passive)
764 sleep_time = tz->trips.passive.tsp * 100;
765 else if (tz->polling_frequency > 0)
766 sleep_time = tz->polling_frequency * 100;
768 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: temperature[%lu] sleep[%lu]\n",
769 tz->name, tz->temperature, sleep_time));
776 if (timer_pending(&(tz->timer)))
777 del_timer(&(tz->timer));
779 if (timer_pending(&(tz->timer)))
780 mod_timer(&(tz->timer),
781 jiffies + (HZ * sleep_time) / 1000);
783 tz->timer.data = (unsigned long)tz;
784 tz->timer.function = acpi_thermal_run;
785 tz->timer.expires = jiffies + (HZ * sleep_time) / 1000;
786 add_timer(&(tz->timer));
793 /* --------------------------------------------------------------------------
795 -------------------------------------------------------------------------- */
797 static struct proc_dir_entry *acpi_thermal_dir;
799 static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
801 struct acpi_thermal *tz = seq->private;
807 seq_puts(seq, "state: ");
809 if (!tz->state.critical && !tz->state.hot && !tz->state.passive
810 && !tz->state.active)
811 seq_puts(seq, "ok\n");
813 if (tz->state.critical)
814 seq_puts(seq, "critical ");
816 seq_puts(seq, "hot ");
817 if (tz->state.passive)
818 seq_puts(seq, "passive ");
819 if (tz->state.active)
820 seq_printf(seq, "active[%d]", tz->state.active_index);
828 static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
830 return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
833 static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
836 struct acpi_thermal *tz = seq->private;
842 result = acpi_thermal_get_temperature(tz);
846 seq_printf(seq, "temperature: %ld C\n",
847 KELVIN_TO_CELSIUS(tz->temperature));
853 static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
855 return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
858 static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
860 struct acpi_thermal *tz = seq->private;
861 struct acpi_device *device;
871 if (tz->trips.critical.flags.valid)
872 seq_printf(seq, "critical (S5): %ld C%s",
873 KELVIN_TO_CELSIUS(tz->trips.critical.temperature),
874 nocrt ? " <disabled>\n" : "\n");
876 if (tz->trips.hot.flags.valid)
877 seq_printf(seq, "hot (S4): %ld C%s",
878 KELVIN_TO_CELSIUS(tz->trips.hot.temperature),
879 nocrt ? " <disabled>\n" : "\n");
881 if (tz->trips.passive.flags.valid) {
883 "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
884 KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
885 tz->trips.passive.tc1, tz->trips.passive.tc2,
886 tz->trips.passive.tsp);
887 for (j = 0; j < tz->trips.passive.devices.count; j++) {
888 status = acpi_bus_get_device(tz->trips.passive.devices.
889 handles[j], &device);
890 seq_printf(seq, "%4.4s ", status ? "" :
891 acpi_device_bid(device));
896 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
897 if (!(tz->trips.active[i].flags.valid))
899 seq_printf(seq, "active[%d]: %ld C: devices=",
901 KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
902 for (j = 0; j < tz->trips.active[i].devices.count; j++){
903 status = acpi_bus_get_device(tz->trips.active[i].
906 seq_printf(seq, "%4.4s ", status ? "" :
907 acpi_device_bid(device));
916 static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
918 return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
921 static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
923 struct acpi_thermal *tz = seq->private;
929 if (!tz->flags.cooling_mode)
930 seq_puts(seq, "<setting not supported>\n");
932 seq_puts(seq, "0 - Active; 1 - Passive\n");
938 static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
940 return single_open(file, acpi_thermal_cooling_seq_show,
945 acpi_thermal_write_cooling_mode(struct file *file,
946 const char __user * buffer,
947 size_t count, loff_t * ppos)
949 struct seq_file *m = file->private_data;
950 struct acpi_thermal *tz = m->private;
952 char mode_string[12] = { '\0' };
955 if (!tz || (count > sizeof(mode_string) - 1))
958 if (!tz->flags.cooling_mode)
961 if (copy_from_user(mode_string, buffer, count))
964 mode_string[count] = '\0';
966 result = acpi_thermal_set_cooling_mode(tz,
967 simple_strtoul(mode_string, NULL,
972 acpi_thermal_check(tz);
977 static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
979 struct acpi_thermal *tz = seq->private;
985 if (!tz->polling_frequency) {
986 seq_puts(seq, "<polling disabled>\n");
990 seq_printf(seq, "polling frequency: %lu seconds\n",
991 (tz->polling_frequency / 10));
997 static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
999 return single_open(file, acpi_thermal_polling_seq_show,
1004 acpi_thermal_write_polling(struct file *file,
1005 const char __user * buffer,
1006 size_t count, loff_t * ppos)
1008 struct seq_file *m = file->private_data;
1009 struct acpi_thermal *tz = m->private;
1011 char polling_string[12] = { '\0' };
1015 if (!tz || (count > sizeof(polling_string) - 1))
1018 if (copy_from_user(polling_string, buffer, count))
1021 polling_string[count] = '\0';
1023 seconds = simple_strtoul(polling_string, NULL, 0);
1025 result = acpi_thermal_set_polling(tz, seconds);
1029 acpi_thermal_check(tz);
1034 static int acpi_thermal_add_fs(struct acpi_device *device)
1036 struct proc_dir_entry *entry = NULL;
1039 if (!acpi_device_dir(device)) {
1040 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
1042 if (!acpi_device_dir(device))
1044 acpi_device_dir(device)->owner = THIS_MODULE;
1048 entry = create_proc_entry(ACPI_THERMAL_FILE_STATE,
1049 S_IRUGO, acpi_device_dir(device));
1053 entry->proc_fops = &acpi_thermal_state_fops;
1054 entry->data = acpi_driver_data(device);
1055 entry->owner = THIS_MODULE;
1058 /* 'temperature' [R] */
1059 entry = create_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1060 S_IRUGO, acpi_device_dir(device));
1064 entry->proc_fops = &acpi_thermal_temp_fops;
1065 entry->data = acpi_driver_data(device);
1066 entry->owner = THIS_MODULE;
1069 /* 'trip_points' [R/W] */
1070 entry = create_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1071 S_IFREG | S_IRUGO | S_IWUSR,
1072 acpi_device_dir(device));
1076 entry->proc_fops = &acpi_thermal_trip_fops;
1077 entry->data = acpi_driver_data(device);
1078 entry->owner = THIS_MODULE;
1081 /* 'cooling_mode' [R/W] */
1082 entry = create_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1083 S_IFREG | S_IRUGO | S_IWUSR,
1084 acpi_device_dir(device));
1088 entry->proc_fops = &acpi_thermal_cooling_fops;
1089 entry->data = acpi_driver_data(device);
1090 entry->owner = THIS_MODULE;
1093 /* 'polling_frequency' [R/W] */
1094 entry = create_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1095 S_IFREG | S_IRUGO | S_IWUSR,
1096 acpi_device_dir(device));
1100 entry->proc_fops = &acpi_thermal_polling_fops;
1101 entry->data = acpi_driver_data(device);
1102 entry->owner = THIS_MODULE;
1108 static int acpi_thermal_remove_fs(struct acpi_device *device)
1111 if (acpi_device_dir(device)) {
1112 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1113 acpi_device_dir(device));
1114 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1115 acpi_device_dir(device));
1116 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1117 acpi_device_dir(device));
1118 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1119 acpi_device_dir(device));
1120 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1121 acpi_device_dir(device));
1122 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1123 acpi_device_dir(device) = NULL;
1129 /* --------------------------------------------------------------------------
1131 -------------------------------------------------------------------------- */
1133 static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
1135 struct acpi_thermal *tz = data;
1136 struct acpi_device *device = NULL;
1142 device = tz->device;
1145 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1146 acpi_thermal_check(tz);
1148 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
1149 acpi_thermal_get_trip_points(tz);
1150 acpi_thermal_check(tz);
1151 acpi_bus_generate_event(device, event, 0);
1153 case ACPI_THERMAL_NOTIFY_DEVICES:
1154 if (tz->flags.devices)
1155 acpi_thermal_get_devices(tz);
1156 acpi_bus_generate_event(device, event, 0);
1159 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1160 "Unsupported event [0x%x]\n", event));
1167 static int acpi_thermal_get_info(struct acpi_thermal *tz)
1175 /* Get temperature [_TMP] (required) */
1176 result = acpi_thermal_get_temperature(tz);
1180 /* Get trip points [_CRT, _PSV, etc.] (required) */
1181 result = acpi_thermal_get_trip_points(tz);
1185 /* Set the cooling mode [_SCP] to active cooling (default) */
1186 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
1188 tz->flags.cooling_mode = 1;
1190 /* Get default polling frequency [_TZP] (optional) */
1192 tz->polling_frequency = tzp;
1194 acpi_thermal_get_polling_frequency(tz);
1196 /* Get devices in this thermal zone [_TZD] (optional) */
1197 result = acpi_thermal_get_devices(tz);
1199 tz->flags.devices = 1;
1204 static int acpi_thermal_add(struct acpi_device *device)
1207 acpi_status status = AE_OK;
1208 struct acpi_thermal *tz = NULL;
1214 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
1218 tz->device = device;
1219 strcpy(tz->name, device->pnp.bus_id);
1220 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1221 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1222 acpi_driver_data(device) = tz;
1224 result = acpi_thermal_get_info(tz);
1228 result = acpi_thermal_add_fs(device);
1232 init_timer(&tz->timer);
1234 acpi_thermal_check(tz);
1236 status = acpi_install_notify_handler(device->handle,
1238 acpi_thermal_notify, tz);
1239 if (ACPI_FAILURE(status)) {
1244 printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
1245 acpi_device_name(device), acpi_device_bid(device),
1246 KELVIN_TO_CELSIUS(tz->temperature));
1250 acpi_thermal_remove_fs(device);
1257 static int acpi_thermal_remove(struct acpi_device *device, int type)
1259 acpi_status status = AE_OK;
1260 struct acpi_thermal *tz = NULL;
1263 if (!device || !acpi_driver_data(device))
1266 tz = acpi_driver_data(device);
1268 /* avoid timer adding new defer task */
1270 /* wait for running timer (on other CPUs) finish */
1271 del_timer_sync(&(tz->timer));
1272 /* synchronize deferred task */
1273 acpi_os_wait_events_complete(NULL);
1274 /* deferred task may reinsert timer */
1275 del_timer_sync(&(tz->timer));
1277 status = acpi_remove_notify_handler(device->handle,
1279 acpi_thermal_notify);
1281 /* Terminate policy */
1282 if (tz->trips.passive.flags.valid && tz->trips.passive.flags.enabled) {
1283 tz->trips.passive.flags.enabled = 0;
1284 acpi_thermal_passive(tz);
1286 if (tz->trips.active[0].flags.valid
1287 && tz->trips.active[0].flags.enabled) {
1288 tz->trips.active[0].flags.enabled = 0;
1289 acpi_thermal_active(tz);
1292 acpi_thermal_remove_fs(device);
1298 static int acpi_thermal_resume(struct acpi_device *device)
1300 struct acpi_thermal *tz = NULL;
1301 int i, j, power_state, result;
1304 if (!device || !acpi_driver_data(device))
1307 tz = acpi_driver_data(device);
1309 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1310 if (!(&tz->trips.active[i]))
1312 if (!tz->trips.active[i].flags.valid)
1314 tz->trips.active[i].flags.enabled = 1;
1315 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1316 result = acpi_bus_get_power(tz->trips.active[i].devices.
1317 handles[j], &power_state);
1318 if (result || (power_state != ACPI_STATE_D0)) {
1319 tz->trips.active[i].flags.enabled = 0;
1323 tz->state.active |= tz->trips.active[i].flags.enabled;
1326 acpi_thermal_check(tz);
1331 static int __init acpi_thermal_init(void)
1336 printk(KERN_NOTICE "ACPI: thermal control disabled\n");
1339 acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1340 if (!acpi_thermal_dir)
1342 acpi_thermal_dir->owner = THIS_MODULE;
1344 result = acpi_bus_register_driver(&acpi_thermal_driver);
1346 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1353 static void __exit acpi_thermal_exit(void)
1356 acpi_bus_unregister_driver(&acpi_thermal_driver);
1358 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1363 module_init(acpi_thermal_init);
1364 module_exit(acpi_thermal_exit);