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/sched.h>
40 #include <linux/kmod.h>
41 #include <linux/seq_file.h>
42 #include <asm/uaccess.h>
44 #include <acpi/acpi_bus.h>
45 #include <acpi/acpi_drivers.h>
47 #define ACPI_THERMAL_COMPONENT 0x04000000
48 #define ACPI_THERMAL_CLASS "thermal_zone"
49 #define ACPI_THERMAL_DRIVER_NAME "ACPI Thermal Zone Driver"
50 #define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
51 #define ACPI_THERMAL_FILE_STATE "state"
52 #define ACPI_THERMAL_FILE_TEMPERATURE "temperature"
53 #define ACPI_THERMAL_FILE_TRIP_POINTS "trip_points"
54 #define ACPI_THERMAL_FILE_COOLING_MODE "cooling_mode"
55 #define ACPI_THERMAL_FILE_POLLING_FREQ "polling_frequency"
56 #define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
57 #define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
58 #define ACPI_THERMAL_NOTIFY_DEVICES 0x82
59 #define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
60 #define ACPI_THERMAL_NOTIFY_HOT 0xF1
61 #define ACPI_THERMAL_MODE_ACTIVE 0x00
62 #define ACPI_THERMAL_MODE_PASSIVE 0x01
63 #define ACPI_THERMAL_MODE_CRITICAL 0xff
64 #define ACPI_THERMAL_PATH_POWEROFF "/sbin/poweroff"
66 #define ACPI_THERMAL_MAX_ACTIVE 10
67 #define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
69 #define KELVIN_TO_CELSIUS(t) (long)(((long)t-2732>=0) ? ((long)t-2732+5)/10 : ((long)t-2732-5)/10)
70 #define CELSIUS_TO_KELVIN(t) ((t+273)*10)
72 #define _COMPONENT ACPI_THERMAL_COMPONENT
73 ACPI_MODULE_NAME("acpi_thermal")
75 MODULE_AUTHOR("Paul Diefenbaugh");
76 MODULE_DESCRIPTION(ACPI_THERMAL_DRIVER_NAME);
77 MODULE_LICENSE("GPL");
80 module_param(tzp, int, 0);
81 MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.\n");
83 static int acpi_thermal_add(struct acpi_device *device);
84 static int acpi_thermal_remove(struct acpi_device *device, int type);
85 static int acpi_thermal_resume(struct acpi_device *device, int state);
86 static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
87 static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
88 static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
89 static ssize_t acpi_thermal_write_trip_points(struct file *,
90 const char __user *, size_t,
92 static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
93 static ssize_t acpi_thermal_write_cooling_mode(struct file *,
94 const char __user *, size_t,
96 static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
97 static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
100 static struct acpi_driver acpi_thermal_driver = {
101 .name = ACPI_THERMAL_DRIVER_NAME,
102 .class = ACPI_THERMAL_CLASS,
103 .ids = ACPI_THERMAL_HID,
105 .add = acpi_thermal_add,
106 .remove = acpi_thermal_remove,
107 .resume = acpi_thermal_resume,
111 struct acpi_thermal_state {
120 struct acpi_thermal_state_flags {
126 struct acpi_thermal_critical {
127 struct acpi_thermal_state_flags flags;
128 unsigned long temperature;
131 struct acpi_thermal_hot {
132 struct acpi_thermal_state_flags flags;
133 unsigned long temperature;
136 struct acpi_thermal_passive {
137 struct acpi_thermal_state_flags flags;
138 unsigned long temperature;
142 struct acpi_handle_list devices;
145 struct acpi_thermal_active {
146 struct acpi_thermal_state_flags flags;
147 unsigned long temperature;
148 struct acpi_handle_list devices;
151 struct acpi_thermal_trips {
152 struct acpi_thermal_critical critical;
153 struct acpi_thermal_hot hot;
154 struct acpi_thermal_passive passive;
155 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
158 struct acpi_thermal_flags {
159 u8 cooling_mode:1; /* _SCP */
160 u8 devices:1; /* _TZD */
164 struct acpi_thermal {
166 struct acpi_device * device;
168 unsigned long temperature;
169 unsigned long last_temperature;
170 unsigned long polling_frequency;
173 struct acpi_thermal_flags flags;
174 struct acpi_thermal_state state;
175 struct acpi_thermal_trips trips;
176 struct acpi_handle_list devices;
177 struct timer_list timer;
180 static struct file_operations acpi_thermal_state_fops = {
181 .open = acpi_thermal_state_open_fs,
184 .release = single_release,
187 static struct file_operations acpi_thermal_temp_fops = {
188 .open = acpi_thermal_temp_open_fs,
191 .release = single_release,
194 static struct file_operations acpi_thermal_trip_fops = {
195 .open = acpi_thermal_trip_open_fs,
197 .write = acpi_thermal_write_trip_points,
199 .release = single_release,
202 static struct file_operations acpi_thermal_cooling_fops = {
203 .open = acpi_thermal_cooling_open_fs,
205 .write = acpi_thermal_write_cooling_mode,
207 .release = single_release,
210 static struct file_operations acpi_thermal_polling_fops = {
211 .open = acpi_thermal_polling_open_fs,
213 .write = acpi_thermal_write_polling,
215 .release = single_release,
218 /* --------------------------------------------------------------------------
219 Thermal Zone Management
220 -------------------------------------------------------------------------- */
222 static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
224 acpi_status status = AE_OK;
230 tz->last_temperature = tz->temperature;
233 acpi_evaluate_integer(tz->handle, "_TMP", NULL, &tz->temperature);
234 if (ACPI_FAILURE(status))
237 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
243 static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
245 acpi_status status = AE_OK;
252 acpi_evaluate_integer(tz->handle, "_TZP", NULL,
253 &tz->polling_frequency);
254 if (ACPI_FAILURE(status))
257 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
258 tz->polling_frequency));
263 static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
269 tz->polling_frequency = seconds * 10; /* Convert value to deci-seconds */
271 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
272 "Polling frequency set to %lu seconds\n",
273 tz->polling_frequency));
278 static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
280 acpi_status status = AE_OK;
281 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
282 struct acpi_object_list arg_list = { 1, &arg0 };
283 acpi_handle handle = NULL;
289 status = acpi_get_handle(tz->handle, "_SCP", &handle);
290 if (ACPI_FAILURE(status)) {
291 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
295 arg0.integer.value = mode;
297 status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
298 if (ACPI_FAILURE(status))
301 tz->cooling_mode = mode;
303 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Cooling mode [%s]\n",
304 mode ? "passive" : "active"));
309 static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
311 acpi_status status = AE_OK;
318 /* Critical Shutdown (required) */
320 status = acpi_evaluate_integer(tz->handle, "_CRT", NULL,
321 &tz->trips.critical.temperature);
322 if (ACPI_FAILURE(status)) {
323 tz->trips.critical.flags.valid = 0;
324 ACPI_EXCEPTION((AE_INFO, status, "No critical threshold"));
327 tz->trips.critical.flags.valid = 1;
328 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
329 "Found critical threshold [%lu]\n",
330 tz->trips.critical.temperature));
333 /* Critical Sleep (optional) */
336 acpi_evaluate_integer(tz->handle, "_HOT", NULL,
337 &tz->trips.hot.temperature);
338 if (ACPI_FAILURE(status)) {
339 tz->trips.hot.flags.valid = 0;
340 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No hot threshold\n"));
342 tz->trips.hot.flags.valid = 1;
343 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found hot threshold [%lu]\n",
344 tz->trips.hot.temperature));
347 /* Passive: Processors (optional) */
350 acpi_evaluate_integer(tz->handle, "_PSV", NULL,
351 &tz->trips.passive.temperature);
352 if (ACPI_FAILURE(status)) {
353 tz->trips.passive.flags.valid = 0;
354 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No passive threshold\n"));
356 tz->trips.passive.flags.valid = 1;
359 acpi_evaluate_integer(tz->handle, "_TC1", NULL,
360 &tz->trips.passive.tc1);
361 if (ACPI_FAILURE(status))
362 tz->trips.passive.flags.valid = 0;
365 acpi_evaluate_integer(tz->handle, "_TC2", NULL,
366 &tz->trips.passive.tc2);
367 if (ACPI_FAILURE(status))
368 tz->trips.passive.flags.valid = 0;
371 acpi_evaluate_integer(tz->handle, "_TSP", NULL,
372 &tz->trips.passive.tsp);
373 if (ACPI_FAILURE(status))
374 tz->trips.passive.flags.valid = 0;
377 acpi_evaluate_reference(tz->handle, "_PSL", NULL,
378 &tz->trips.passive.devices);
379 if (ACPI_FAILURE(status))
380 tz->trips.passive.flags.valid = 0;
382 if (!tz->trips.passive.flags.valid)
383 printk(KERN_WARNING PREFIX "Invalid passive threshold\n");
385 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
386 "Found passive threshold [%lu]\n",
387 tz->trips.passive.temperature));
390 /* Active: Fans, etc. (optional) */
392 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
394 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
397 acpi_evaluate_integer(tz->handle, name, NULL,
398 &tz->trips.active[i].temperature);
399 if (ACPI_FAILURE(status))
404 acpi_evaluate_reference(tz->handle, name, NULL,
405 &tz->trips.active[i].devices);
406 if (ACPI_SUCCESS(status)) {
407 tz->trips.active[i].flags.valid = 1;
408 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
409 "Found active threshold [%d]:[%lu]\n",
410 i, tz->trips.active[i].temperature));
412 ACPI_EXCEPTION((AE_INFO, status,
413 "Invalid active threshold [%d]", i));
419 static int acpi_thermal_get_devices(struct acpi_thermal *tz)
421 acpi_status status = AE_OK;
428 acpi_evaluate_reference(tz->handle, "_TZD", NULL, &tz->devices);
429 if (ACPI_FAILURE(status))
435 static int acpi_thermal_call_usermode(char *path)
437 char *argv[2] = { NULL, NULL };
438 char *envp[3] = { NULL, NULL, NULL };
446 /* minimal command environment */
448 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
450 call_usermodehelper(argv[0], argv, envp, 0);
455 static int acpi_thermal_critical(struct acpi_thermal *tz)
457 if (!tz || !tz->trips.critical.flags.valid)
460 if (tz->temperature >= tz->trips.critical.temperature) {
461 printk(KERN_WARNING PREFIX "Critical trip point\n");
462 tz->trips.critical.flags.enabled = 1;
463 } else if (tz->trips.critical.flags.enabled)
464 tz->trips.critical.flags.enabled = 0;
467 "Critical temperature reached (%ld C), shutting down.\n",
468 KELVIN_TO_CELSIUS(tz->temperature));
469 acpi_bus_generate_event(tz->device, ACPI_THERMAL_NOTIFY_CRITICAL,
470 tz->trips.critical.flags.enabled);
472 acpi_thermal_call_usermode(ACPI_THERMAL_PATH_POWEROFF);
477 static int acpi_thermal_hot(struct acpi_thermal *tz)
479 if (!tz || !tz->trips.hot.flags.valid)
482 if (tz->temperature >= tz->trips.hot.temperature) {
483 printk(KERN_WARNING PREFIX "Hot trip point\n");
484 tz->trips.hot.flags.enabled = 1;
485 } else if (tz->trips.hot.flags.enabled)
486 tz->trips.hot.flags.enabled = 0;
488 acpi_bus_generate_event(tz->device, ACPI_THERMAL_NOTIFY_HOT,
489 tz->trips.hot.flags.enabled);
491 /* TBD: Call user-mode "sleep(S4)" function */
496 static void acpi_thermal_passive(struct acpi_thermal *tz)
499 struct acpi_thermal_passive *passive = NULL;
504 if (!tz || !tz->trips.passive.flags.valid)
507 passive = &(tz->trips.passive);
512 * Calculate the thermal trend (using the passive cooling equation)
513 * and modify the performance limit for all passive cooling devices
514 * accordingly. Note that we assume symmetry.
516 if (tz->temperature >= passive->temperature) {
518 (passive->tc1 * (tz->temperature - tz->last_temperature)) +
519 (passive->tc2 * (tz->temperature - passive->temperature));
520 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
521 "trend[%d]=(tc1[%lu]*(tmp[%lu]-last[%lu]))+(tc2[%lu]*(tmp[%lu]-psv[%lu]))\n",
522 trend, passive->tc1, tz->temperature,
523 tz->last_temperature, passive->tc2,
524 tz->temperature, passive->temperature));
525 passive->flags.enabled = 1;
528 for (i = 0; i < passive->devices.count; i++)
529 acpi_processor_set_thermal_limit(passive->
532 ACPI_PROCESSOR_LIMIT_INCREMENT);
534 else if (trend < 0) {
535 for (i = 0; i < passive->devices.count; i++)
537 * assume that we are on highest
538 * freq/lowest thrott and can leave
539 * passive mode, even in error case
541 if (!acpi_processor_set_thermal_limit
542 (passive->devices.handles[i],
543 ACPI_PROCESSOR_LIMIT_DECREMENT))
546 * Leave cooling mode, even if the temp might
547 * higher than trip point This is because some
548 * machines might have long thermal polling
549 * frequencies (tsp) defined. We will fall back
550 * into passive mode in next cycle (probably quicker)
553 passive->flags.enabled = 0;
554 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
555 "Disabling passive cooling, still above threshold,"
556 " but we are cooling down\n"));
565 * Implement passive cooling hysteresis to slowly increase performance
566 * and avoid thrashing around the passive trip point. Note that we
569 if (!passive->flags.enabled)
571 for (i = 0; i < passive->devices.count; i++)
572 if (!acpi_processor_set_thermal_limit
573 (passive->devices.handles[i],
574 ACPI_PROCESSOR_LIMIT_DECREMENT))
577 passive->flags.enabled = 0;
578 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
579 "Disabling passive cooling (zone is cool)\n"));
583 static void acpi_thermal_active(struct acpi_thermal *tz)
586 struct acpi_thermal_active *active = NULL;
589 unsigned long maxtemp = 0;
595 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
596 active = &(tz->trips.active[i]);
597 if (!active || !active->flags.valid)
599 if (tz->temperature >= active->temperature) {
603 * If not already enabled, turn ON all cooling devices
604 * associated with this active threshold.
606 if (active->temperature > maxtemp)
607 tz->state.active_index = i;
608 maxtemp = active->temperature;
609 if (active->flags.enabled)
611 for (j = 0; j < active->devices.count; j++) {
613 acpi_bus_set_power(active->devices.
617 printk(KERN_WARNING PREFIX
618 "Unable to turn cooling device [%p] 'on'\n",
623 active->flags.enabled = 1;
624 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
625 "Cooling device [%p] now 'on'\n",
626 active->devices.handles[j]));
630 if (!active->flags.enabled)
635 * Turn OFF all cooling devices associated with this
638 for (j = 0; j < active->devices.count; j++) {
639 result = acpi_bus_set_power(active->devices.handles[j],
642 printk(KERN_WARNING PREFIX
643 "Unable to turn cooling device [%p] 'off'\n",
644 active->devices.handles[j]);
647 active->flags.enabled = 0;
648 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
649 "Cooling device [%p] now 'off'\n",
650 active->devices.handles[j]));
655 static void acpi_thermal_check(void *context);
657 static void acpi_thermal_run(unsigned long data)
659 struct acpi_thermal *tz = (struct acpi_thermal *)data;
661 acpi_os_execute(OSL_GPE_HANDLER, acpi_thermal_check, (void *)data);
664 static void acpi_thermal_check(void *data)
667 struct acpi_thermal *tz = (struct acpi_thermal *)data;
668 unsigned long sleep_time = 0;
670 struct acpi_thermal_state state;
674 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
680 result = acpi_thermal_get_temperature(tz);
684 memset(&tz->state, 0, sizeof(tz->state));
689 * Compare the current temperature to the trip point values to see
690 * if we've entered one of the thermal policy states. Note that
691 * this function determines when a state is entered, but the
692 * individual policy decides when it is exited (e.g. hysteresis).
694 if (tz->trips.critical.flags.valid)
696 (tz->temperature >= tz->trips.critical.temperature);
697 if (tz->trips.hot.flags.valid)
698 state.hot |= (tz->temperature >= tz->trips.hot.temperature);
699 if (tz->trips.passive.flags.valid)
701 (tz->temperature >= tz->trips.passive.temperature);
702 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
703 if (tz->trips.active[i].flags.valid)
706 tz->trips.active[i].temperature);
711 * Separated from the above check to allow individual policy to
712 * determine when to exit a given state.
715 acpi_thermal_critical(tz);
717 acpi_thermal_hot(tz);
719 acpi_thermal_passive(tz);
721 acpi_thermal_active(tz);
726 * Again, separated from the above two to allow independent policy
729 tz->state.critical = tz->trips.critical.flags.enabled;
730 tz->state.hot = tz->trips.hot.flags.enabled;
731 tz->state.passive = tz->trips.passive.flags.enabled;
732 tz->state.active = 0;
733 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
734 tz->state.active |= tz->trips.active[i].flags.enabled;
737 * Calculate Sleep Time
738 * --------------------
739 * If we're in the passive state, use _TSP's value. Otherwise
740 * use the default polling frequency (e.g. _TZP). If no polling
741 * frequency is specified then we'll wait forever (at least until
742 * a thermal event occurs). Note that _TSP and _TZD values are
743 * given in 1/10th seconds (we must covert to milliseconds).
745 if (tz->state.passive)
746 sleep_time = tz->trips.passive.tsp * 100;
747 else if (tz->polling_frequency > 0)
748 sleep_time = tz->polling_frequency * 100;
750 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: temperature[%lu] sleep[%lu]\n",
751 tz->name, tz->temperature, sleep_time));
758 if (timer_pending(&(tz->timer)))
759 del_timer(&(tz->timer));
761 if (timer_pending(&(tz->timer)))
762 mod_timer(&(tz->timer), (HZ * sleep_time) / 1000);
764 tz->timer.data = (unsigned long)tz;
765 tz->timer.function = acpi_thermal_run;
766 tz->timer.expires = jiffies + (HZ * sleep_time) / 1000;
767 add_timer(&(tz->timer));
774 /* --------------------------------------------------------------------------
776 -------------------------------------------------------------------------- */
778 static struct proc_dir_entry *acpi_thermal_dir;
780 static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
782 struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
788 seq_puts(seq, "state: ");
790 if (!tz->state.critical && !tz->state.hot && !tz->state.passive
791 && !tz->state.active)
792 seq_puts(seq, "ok\n");
794 if (tz->state.critical)
795 seq_puts(seq, "critical ");
797 seq_puts(seq, "hot ");
798 if (tz->state.passive)
799 seq_puts(seq, "passive ");
800 if (tz->state.active)
801 seq_printf(seq, "active[%d]", tz->state.active_index);
809 static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
811 return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
814 static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
817 struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
823 result = acpi_thermal_get_temperature(tz);
827 seq_printf(seq, "temperature: %ld C\n",
828 KELVIN_TO_CELSIUS(tz->temperature));
834 static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
836 return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
839 static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
841 struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
849 if (tz->trips.critical.flags.valid)
850 seq_printf(seq, "critical (S5): %ld C\n",
851 KELVIN_TO_CELSIUS(tz->trips.critical.temperature));
853 if (tz->trips.hot.flags.valid)
854 seq_printf(seq, "hot (S4): %ld C\n",
855 KELVIN_TO_CELSIUS(tz->trips.hot.temperature));
857 if (tz->trips.passive.flags.valid) {
859 "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
860 KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
861 tz->trips.passive.tc1, tz->trips.passive.tc2,
862 tz->trips.passive.tsp);
863 for (j = 0; j < tz->trips.passive.devices.count; j++) {
865 seq_printf(seq, "0x%p ",
866 tz->trips.passive.devices.handles[j]);
871 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
872 if (!(tz->trips.active[i].flags.valid))
874 seq_printf(seq, "active[%d]: %ld C: devices=",
876 KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
877 for (j = 0; j < tz->trips.active[i].devices.count; j++)
878 seq_printf(seq, "0x%p ",
879 tz->trips.active[i].devices.handles[j]);
887 static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
889 return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
893 acpi_thermal_write_trip_points(struct file *file,
894 const char __user * buffer,
895 size_t count, loff_t * ppos)
897 struct seq_file *m = (struct seq_file *)file->private_data;
898 struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
901 int num, critical, hot, passive;
906 limit_string = kmalloc(ACPI_THERMAL_MAX_LIMIT_STR_LEN, GFP_KERNEL);
910 memset(limit_string, 0, ACPI_THERMAL_MAX_LIMIT_STR_LEN);
912 active = kmalloc(ACPI_THERMAL_MAX_ACTIVE * sizeof(int), GFP_KERNEL);
918 if (!tz || (count > ACPI_THERMAL_MAX_LIMIT_STR_LEN - 1)) {
923 if (copy_from_user(limit_string, buffer, count)) {
928 limit_string[count] = '\0';
930 num = sscanf(limit_string, "%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d",
931 &critical, &hot, &passive,
932 &active[0], &active[1], &active[2], &active[3], &active[4],
933 &active[5], &active[6], &active[7], &active[8],
935 if (!(num >= 5 && num < (ACPI_THERMAL_MAX_ACTIVE + 3))) {
940 tz->trips.critical.temperature = CELSIUS_TO_KELVIN(critical);
941 tz->trips.hot.temperature = CELSIUS_TO_KELVIN(hot);
942 tz->trips.passive.temperature = CELSIUS_TO_KELVIN(passive);
943 for (i = 0; i < num - 3; i++) {
944 if (!(tz->trips.active[i].flags.valid))
946 tz->trips.active[i].temperature = CELSIUS_TO_KELVIN(active[i]);
955 static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
957 struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
963 if (!tz->flags.cooling_mode) {
964 seq_puts(seq, "<setting not supported>\n");
967 if (tz->cooling_mode == ACPI_THERMAL_MODE_CRITICAL)
968 seq_printf(seq, "cooling mode: critical\n");
970 seq_printf(seq, "cooling mode: %s\n",
971 tz->cooling_mode ? "passive" : "active");
977 static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
979 return single_open(file, acpi_thermal_cooling_seq_show,
984 acpi_thermal_write_cooling_mode(struct file *file,
985 const char __user * buffer,
986 size_t count, loff_t * ppos)
988 struct seq_file *m = (struct seq_file *)file->private_data;
989 struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
991 char mode_string[12] = { '\0' };
994 if (!tz || (count > sizeof(mode_string) - 1))
997 if (!tz->flags.cooling_mode)
1000 if (copy_from_user(mode_string, buffer, count))
1003 mode_string[count] = '\0';
1005 result = acpi_thermal_set_cooling_mode(tz,
1006 simple_strtoul(mode_string, NULL,
1011 acpi_thermal_check(tz);
1016 static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
1018 struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
1024 if (!tz->polling_frequency) {
1025 seq_puts(seq, "<polling disabled>\n");
1029 seq_printf(seq, "polling frequency: %lu seconds\n",
1030 (tz->polling_frequency / 10));
1036 static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
1038 return single_open(file, acpi_thermal_polling_seq_show,
1043 acpi_thermal_write_polling(struct file *file,
1044 const char __user * buffer,
1045 size_t count, loff_t * ppos)
1047 struct seq_file *m = (struct seq_file *)file->private_data;
1048 struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
1050 char polling_string[12] = { '\0' };
1054 if (!tz || (count > sizeof(polling_string) - 1))
1057 if (copy_from_user(polling_string, buffer, count))
1060 polling_string[count] = '\0';
1062 seconds = simple_strtoul(polling_string, NULL, 0);
1064 result = acpi_thermal_set_polling(tz, seconds);
1068 acpi_thermal_check(tz);
1073 static int acpi_thermal_add_fs(struct acpi_device *device)
1075 struct proc_dir_entry *entry = NULL;
1078 if (!acpi_device_dir(device)) {
1079 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
1081 if (!acpi_device_dir(device))
1083 acpi_device_dir(device)->owner = THIS_MODULE;
1087 entry = create_proc_entry(ACPI_THERMAL_FILE_STATE,
1088 S_IRUGO, acpi_device_dir(device));
1092 entry->proc_fops = &acpi_thermal_state_fops;
1093 entry->data = acpi_driver_data(device);
1094 entry->owner = THIS_MODULE;
1097 /* 'temperature' [R] */
1098 entry = create_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1099 S_IRUGO, acpi_device_dir(device));
1103 entry->proc_fops = &acpi_thermal_temp_fops;
1104 entry->data = acpi_driver_data(device);
1105 entry->owner = THIS_MODULE;
1108 /* 'trip_points' [R/W] */
1109 entry = create_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1110 S_IFREG | S_IRUGO | S_IWUSR,
1111 acpi_device_dir(device));
1115 entry->proc_fops = &acpi_thermal_trip_fops;
1116 entry->data = acpi_driver_data(device);
1117 entry->owner = THIS_MODULE;
1120 /* 'cooling_mode' [R/W] */
1121 entry = create_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1122 S_IFREG | S_IRUGO | S_IWUSR,
1123 acpi_device_dir(device));
1127 entry->proc_fops = &acpi_thermal_cooling_fops;
1128 entry->data = acpi_driver_data(device);
1129 entry->owner = THIS_MODULE;
1132 /* 'polling_frequency' [R/W] */
1133 entry = create_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1134 S_IFREG | S_IRUGO | S_IWUSR,
1135 acpi_device_dir(device));
1139 entry->proc_fops = &acpi_thermal_polling_fops;
1140 entry->data = acpi_driver_data(device);
1141 entry->owner = THIS_MODULE;
1147 static int acpi_thermal_remove_fs(struct acpi_device *device)
1150 if (acpi_device_dir(device)) {
1151 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1152 acpi_device_dir(device));
1153 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1154 acpi_device_dir(device));
1155 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1156 acpi_device_dir(device));
1157 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1158 acpi_device_dir(device));
1159 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1160 acpi_device_dir(device));
1161 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1162 acpi_device_dir(device) = NULL;
1168 /* --------------------------------------------------------------------------
1170 -------------------------------------------------------------------------- */
1172 static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
1174 struct acpi_thermal *tz = (struct acpi_thermal *)data;
1175 struct acpi_device *device = NULL;
1181 device = tz->device;
1184 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1185 acpi_thermal_check(tz);
1187 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
1188 acpi_thermal_get_trip_points(tz);
1189 acpi_thermal_check(tz);
1190 acpi_bus_generate_event(device, event, 0);
1192 case ACPI_THERMAL_NOTIFY_DEVICES:
1193 if (tz->flags.devices)
1194 acpi_thermal_get_devices(tz);
1195 acpi_bus_generate_event(device, event, 0);
1198 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1199 "Unsupported event [0x%x]\n", event));
1206 static int acpi_thermal_get_info(struct acpi_thermal *tz)
1214 /* Get temperature [_TMP] (required) */
1215 result = acpi_thermal_get_temperature(tz);
1219 /* Get trip points [_CRT, _PSV, etc.] (required) */
1220 result = acpi_thermal_get_trip_points(tz);
1224 /* Set the cooling mode [_SCP] to active cooling (default) */
1225 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
1227 tz->flags.cooling_mode = 1;
1229 /* Oh,we have not _SCP method.
1230 Generally show cooling_mode by _ACx, _PSV,spec 12.2 */
1231 tz->flags.cooling_mode = 0;
1232 if (tz->trips.active[0].flags.valid
1233 && tz->trips.passive.flags.valid) {
1234 if (tz->trips.passive.temperature >
1235 tz->trips.active[0].temperature)
1236 tz->cooling_mode = ACPI_THERMAL_MODE_ACTIVE;
1238 tz->cooling_mode = ACPI_THERMAL_MODE_PASSIVE;
1239 } else if (!tz->trips.active[0].flags.valid
1240 && tz->trips.passive.flags.valid) {
1241 tz->cooling_mode = ACPI_THERMAL_MODE_PASSIVE;
1242 } else if (tz->trips.active[0].flags.valid
1243 && !tz->trips.passive.flags.valid) {
1244 tz->cooling_mode = ACPI_THERMAL_MODE_ACTIVE;
1246 /* _ACx and _PSV are optional, but _CRT is required */
1247 tz->cooling_mode = ACPI_THERMAL_MODE_CRITICAL;
1251 /* Get default polling frequency [_TZP] (optional) */
1253 tz->polling_frequency = tzp;
1255 acpi_thermal_get_polling_frequency(tz);
1257 /* Get devices in this thermal zone [_TZD] (optional) */
1258 result = acpi_thermal_get_devices(tz);
1260 tz->flags.devices = 1;
1265 static int acpi_thermal_add(struct acpi_device *device)
1268 acpi_status status = AE_OK;
1269 struct acpi_thermal *tz = NULL;
1275 tz = kmalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
1278 memset(tz, 0, sizeof(struct acpi_thermal));
1280 tz->handle = device->handle;
1281 tz->device = device;
1282 strcpy(tz->name, device->pnp.bus_id);
1283 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1284 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1285 acpi_driver_data(device) = tz;
1287 result = acpi_thermal_get_info(tz);
1291 result = acpi_thermal_add_fs(device);
1295 init_timer(&tz->timer);
1297 acpi_thermal_check(tz);
1299 status = acpi_install_notify_handler(tz->handle,
1301 acpi_thermal_notify, tz);
1302 if (ACPI_FAILURE(status)) {
1307 printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
1308 acpi_device_name(device), acpi_device_bid(device),
1309 KELVIN_TO_CELSIUS(tz->temperature));
1313 acpi_thermal_remove_fs(device);
1320 static int acpi_thermal_remove(struct acpi_device *device, int type)
1322 acpi_status status = AE_OK;
1323 struct acpi_thermal *tz = NULL;
1326 if (!device || !acpi_driver_data(device))
1329 tz = (struct acpi_thermal *)acpi_driver_data(device);
1331 /* avoid timer adding new defer task */
1333 /* wait for running timer (on other CPUs) finish */
1334 del_timer_sync(&(tz->timer));
1335 /* synchronize deferred task */
1336 acpi_os_wait_events_complete(NULL);
1337 /* deferred task may reinsert timer */
1338 del_timer_sync(&(tz->timer));
1340 status = acpi_remove_notify_handler(tz->handle,
1342 acpi_thermal_notify);
1344 /* Terminate policy */
1345 if (tz->trips.passive.flags.valid && tz->trips.passive.flags.enabled) {
1346 tz->trips.passive.flags.enabled = 0;
1347 acpi_thermal_passive(tz);
1349 if (tz->trips.active[0].flags.valid
1350 && tz->trips.active[0].flags.enabled) {
1351 tz->trips.active[0].flags.enabled = 0;
1352 acpi_thermal_active(tz);
1355 acpi_thermal_remove_fs(device);
1361 static int acpi_thermal_resume(struct acpi_device *device, int state)
1363 struct acpi_thermal *tz = NULL;
1365 if (!device || !acpi_driver_data(device))
1368 tz = (struct acpi_thermal *)acpi_driver_data(device);
1370 acpi_thermal_check(tz);
1375 static int __init acpi_thermal_init(void)
1380 acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1381 if (!acpi_thermal_dir)
1383 acpi_thermal_dir->owner = THIS_MODULE;
1385 result = acpi_bus_register_driver(&acpi_thermal_driver);
1387 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1394 static void __exit acpi_thermal_exit(void)
1397 acpi_bus_unregister_driver(&acpi_thermal_driver);
1399 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1404 module_init(acpi_thermal_init);
1405 module_exit(acpi_thermal_exit);