ACPI: thermal: expose "thermal.tzp=" to set global polling frequency
[firewire-audio.git] / drivers / acpi / thermal.c
blobb6b3bec845473f68ac88576c38be0a12430ff46e
1 /*
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");
77 static int tzp;
78 module_param(tzp, int, 0444);
79 MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.\n");
81 static int off;
82 module_param(off, int, 0);
83 MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.\n");
85 static int acpi_thermal_add(struct acpi_device *device);
86 static int acpi_thermal_remove(struct acpi_device *device, int type);
87 static int acpi_thermal_resume(struct acpi_device *device);
88 static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
89 static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
90 static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
91 static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
92 static ssize_t acpi_thermal_write_cooling_mode(struct file *,
93 const char __user *, size_t,
94 loff_t *);
95 static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
96 static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
97 size_t, loff_t *);
99 static const struct acpi_device_id thermal_device_ids[] = {
100 {ACPI_THERMAL_HID, 0},
101 {"", 0},
103 MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
105 static struct acpi_driver acpi_thermal_driver = {
106 .name = "thermal",
107 .class = ACPI_THERMAL_CLASS,
108 .ids = thermal_device_ids,
109 .ops = {
110 .add = acpi_thermal_add,
111 .remove = acpi_thermal_remove,
112 .resume = acpi_thermal_resume,
116 struct acpi_thermal_state {
117 u8 critical:1;
118 u8 hot:1;
119 u8 passive:1;
120 u8 active:1;
121 u8 reserved:4;
122 int active_index;
125 struct acpi_thermal_state_flags {
126 u8 valid:1;
127 u8 enabled:1;
128 u8 reserved:6;
131 struct acpi_thermal_critical {
132 struct acpi_thermal_state_flags flags;
133 unsigned long temperature;
136 struct acpi_thermal_hot {
137 struct acpi_thermal_state_flags flags;
138 unsigned long temperature;
141 struct acpi_thermal_passive {
142 struct acpi_thermal_state_flags flags;
143 unsigned long temperature;
144 unsigned long tc1;
145 unsigned long tc2;
146 unsigned long tsp;
147 struct acpi_handle_list devices;
150 struct acpi_thermal_active {
151 struct acpi_thermal_state_flags flags;
152 unsigned long temperature;
153 struct acpi_handle_list devices;
156 struct acpi_thermal_trips {
157 struct acpi_thermal_critical critical;
158 struct acpi_thermal_hot hot;
159 struct acpi_thermal_passive passive;
160 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
163 struct acpi_thermal_flags {
164 u8 cooling_mode:1; /* _SCP */
165 u8 devices:1; /* _TZD */
166 u8 reserved:6;
169 struct acpi_thermal {
170 struct acpi_device * device;
171 acpi_bus_id name;
172 unsigned long temperature;
173 unsigned long last_temperature;
174 unsigned long polling_frequency;
175 volatile u8 zombie;
176 struct acpi_thermal_flags flags;
177 struct acpi_thermal_state state;
178 struct acpi_thermal_trips trips;
179 struct acpi_handle_list devices;
180 struct timer_list timer;
183 static const struct file_operations acpi_thermal_state_fops = {
184 .open = acpi_thermal_state_open_fs,
185 .read = seq_read,
186 .llseek = seq_lseek,
187 .release = single_release,
190 static const struct file_operations acpi_thermal_temp_fops = {
191 .open = acpi_thermal_temp_open_fs,
192 .read = seq_read,
193 .llseek = seq_lseek,
194 .release = single_release,
197 static const struct file_operations acpi_thermal_trip_fops = {
198 .open = acpi_thermal_trip_open_fs,
199 .read = seq_read,
200 .llseek = seq_lseek,
201 .release = single_release,
204 static const struct file_operations acpi_thermal_cooling_fops = {
205 .open = acpi_thermal_cooling_open_fs,
206 .read = seq_read,
207 .write = acpi_thermal_write_cooling_mode,
208 .llseek = seq_lseek,
209 .release = single_release,
212 static const struct file_operations acpi_thermal_polling_fops = {
213 .open = acpi_thermal_polling_open_fs,
214 .read = seq_read,
215 .write = acpi_thermal_write_polling,
216 .llseek = seq_lseek,
217 .release = single_release,
220 /* --------------------------------------------------------------------------
221 Thermal Zone Management
222 -------------------------------------------------------------------------- */
224 static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
226 acpi_status status = AE_OK;
229 if (!tz)
230 return -EINVAL;
232 tz->last_temperature = tz->temperature;
234 status =
235 acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tz->temperature);
236 if (ACPI_FAILURE(status))
237 return -ENODEV;
239 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
240 tz->temperature));
242 return 0;
245 static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
247 acpi_status status = AE_OK;
250 if (!tz)
251 return -EINVAL;
253 status =
254 acpi_evaluate_integer(tz->device->handle, "_TZP", NULL,
255 &tz->polling_frequency);
256 if (ACPI_FAILURE(status))
257 return -ENODEV;
259 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
260 tz->polling_frequency));
262 return 0;
265 static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
268 if (!tz)
269 return -EINVAL;
271 tz->polling_frequency = seconds * 10; /* Convert value to deci-seconds */
273 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
274 "Polling frequency set to %lu seconds\n",
275 tz->polling_frequency/10));
277 return 0;
280 static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
282 acpi_status status = AE_OK;
283 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
284 struct acpi_object_list arg_list = { 1, &arg0 };
285 acpi_handle handle = NULL;
288 if (!tz)
289 return -EINVAL;
291 status = acpi_get_handle(tz->device->handle, "_SCP", &handle);
292 if (ACPI_FAILURE(status)) {
293 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
294 return -ENODEV;
297 arg0.integer.value = mode;
299 status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
300 if (ACPI_FAILURE(status))
301 return -ENODEV;
303 return 0;
306 static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
308 acpi_status status = AE_OK;
309 int i = 0;
312 if (!tz)
313 return -EINVAL;
315 /* Critical Shutdown (required) */
317 status = acpi_evaluate_integer(tz->device->handle, "_CRT", NULL,
318 &tz->trips.critical.temperature);
319 if (ACPI_FAILURE(status)) {
320 tz->trips.critical.flags.valid = 0;
321 ACPI_EXCEPTION((AE_INFO, status, "No critical threshold"));
322 return -ENODEV;
323 } else {
324 tz->trips.critical.flags.valid = 1;
325 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
326 "Found critical threshold [%lu]\n",
327 tz->trips.critical.temperature));
330 /* Critical Sleep (optional) */
332 status =
333 acpi_evaluate_integer(tz->device->handle, "_HOT", NULL,
334 &tz->trips.hot.temperature);
335 if (ACPI_FAILURE(status)) {
336 tz->trips.hot.flags.valid = 0;
337 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No hot threshold\n"));
338 } else {
339 tz->trips.hot.flags.valid = 1;
340 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found hot threshold [%lu]\n",
341 tz->trips.hot.temperature));
344 /* Passive: Processors (optional) */
346 status =
347 acpi_evaluate_integer(tz->device->handle, "_PSV", NULL,
348 &tz->trips.passive.temperature);
349 if (ACPI_FAILURE(status)) {
350 tz->trips.passive.flags.valid = 0;
351 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No passive threshold\n"));
352 } else {
353 tz->trips.passive.flags.valid = 1;
355 status =
356 acpi_evaluate_integer(tz->device->handle, "_TC1", NULL,
357 &tz->trips.passive.tc1);
358 if (ACPI_FAILURE(status))
359 tz->trips.passive.flags.valid = 0;
361 status =
362 acpi_evaluate_integer(tz->device->handle, "_TC2", NULL,
363 &tz->trips.passive.tc2);
364 if (ACPI_FAILURE(status))
365 tz->trips.passive.flags.valid = 0;
367 status =
368 acpi_evaluate_integer(tz->device->handle, "_TSP", NULL,
369 &tz->trips.passive.tsp);
370 if (ACPI_FAILURE(status))
371 tz->trips.passive.flags.valid = 0;
373 status =
374 acpi_evaluate_reference(tz->device->handle, "_PSL", NULL,
375 &tz->trips.passive.devices);
376 if (ACPI_FAILURE(status))
377 tz->trips.passive.flags.valid = 0;
379 if (!tz->trips.passive.flags.valid)
380 printk(KERN_WARNING PREFIX "Invalid passive threshold\n");
381 else
382 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
383 "Found passive threshold [%lu]\n",
384 tz->trips.passive.temperature));
387 /* Active: Fans, etc. (optional) */
389 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
391 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
393 status =
394 acpi_evaluate_integer(tz->device->handle, name, NULL,
395 &tz->trips.active[i].temperature);
396 if (ACPI_FAILURE(status))
397 break;
399 name[2] = 'L';
400 status =
401 acpi_evaluate_reference(tz->device->handle, name, NULL,
402 &tz->trips.active[i].devices);
403 if (ACPI_SUCCESS(status)) {
404 tz->trips.active[i].flags.valid = 1;
405 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
406 "Found active threshold [%d]:[%lu]\n",
407 i, tz->trips.active[i].temperature));
408 } else
409 ACPI_EXCEPTION((AE_INFO, status,
410 "Invalid active threshold [%d]", i));
413 return 0;
416 static int acpi_thermal_get_devices(struct acpi_thermal *tz)
418 acpi_status status = AE_OK;
421 if (!tz)
422 return -EINVAL;
424 status =
425 acpi_evaluate_reference(tz->device->handle, "_TZD", NULL, &tz->devices);
426 if (ACPI_FAILURE(status))
427 return -ENODEV;
429 return 0;
432 static int acpi_thermal_critical(struct acpi_thermal *tz)
434 if (!tz || !tz->trips.critical.flags.valid)
435 return -EINVAL;
437 if (tz->temperature >= tz->trips.critical.temperature) {
438 printk(KERN_WARNING PREFIX "Critical trip point\n");
439 tz->trips.critical.flags.enabled = 1;
440 } else if (tz->trips.critical.flags.enabled)
441 tz->trips.critical.flags.enabled = 0;
443 printk(KERN_EMERG
444 "Critical temperature reached (%ld C), shutting down.\n",
445 KELVIN_TO_CELSIUS(tz->temperature));
446 acpi_bus_generate_event(tz->device, ACPI_THERMAL_NOTIFY_CRITICAL,
447 tz->trips.critical.flags.enabled);
449 orderly_poweroff(true);
451 return 0;
454 static int acpi_thermal_hot(struct acpi_thermal *tz)
456 if (!tz || !tz->trips.hot.flags.valid)
457 return -EINVAL;
459 if (tz->temperature >= tz->trips.hot.temperature) {
460 printk(KERN_WARNING PREFIX "Hot trip point\n");
461 tz->trips.hot.flags.enabled = 1;
462 } else if (tz->trips.hot.flags.enabled)
463 tz->trips.hot.flags.enabled = 0;
465 acpi_bus_generate_event(tz->device, ACPI_THERMAL_NOTIFY_HOT,
466 tz->trips.hot.flags.enabled);
468 /* TBD: Call user-mode "sleep(S4)" function */
470 return 0;
473 static void acpi_thermal_passive(struct acpi_thermal *tz)
475 int result = 1;
476 struct acpi_thermal_passive *passive = NULL;
477 int trend = 0;
478 int i = 0;
481 if (!tz || !tz->trips.passive.flags.valid)
482 return;
484 passive = &(tz->trips.passive);
487 * Above Trip?
488 * -----------
489 * Calculate the thermal trend (using the passive cooling equation)
490 * and modify the performance limit for all passive cooling devices
491 * accordingly. Note that we assume symmetry.
493 if (tz->temperature >= passive->temperature) {
494 trend =
495 (passive->tc1 * (tz->temperature - tz->last_temperature)) +
496 (passive->tc2 * (tz->temperature - passive->temperature));
497 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
498 "trend[%d]=(tc1[%lu]*(tmp[%lu]-last[%lu]))+(tc2[%lu]*(tmp[%lu]-psv[%lu]))\n",
499 trend, passive->tc1, tz->temperature,
500 tz->last_temperature, passive->tc2,
501 tz->temperature, passive->temperature));
502 passive->flags.enabled = 1;
503 /* Heating up? */
504 if (trend > 0)
505 for (i = 0; i < passive->devices.count; i++)
506 acpi_processor_set_thermal_limit(passive->
507 devices.
508 handles[i],
509 ACPI_PROCESSOR_LIMIT_INCREMENT);
510 /* Cooling off? */
511 else if (trend < 0) {
512 for (i = 0; i < passive->devices.count; i++)
514 * assume that we are on highest
515 * freq/lowest thrott and can leave
516 * passive mode, even in error case
518 if (!acpi_processor_set_thermal_limit
519 (passive->devices.handles[i],
520 ACPI_PROCESSOR_LIMIT_DECREMENT))
521 result = 0;
523 * Leave cooling mode, even if the temp might
524 * higher than trip point This is because some
525 * machines might have long thermal polling
526 * frequencies (tsp) defined. We will fall back
527 * into passive mode in next cycle (probably quicker)
529 if (result) {
530 passive->flags.enabled = 0;
531 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
532 "Disabling passive cooling, still above threshold,"
533 " but we are cooling down\n"));
536 return;
540 * Below Trip?
541 * -----------
542 * Implement passive cooling hysteresis to slowly increase performance
543 * and avoid thrashing around the passive trip point. Note that we
544 * assume symmetry.
546 if (!passive->flags.enabled)
547 return;
548 for (i = 0; i < passive->devices.count; i++)
549 if (!acpi_processor_set_thermal_limit
550 (passive->devices.handles[i],
551 ACPI_PROCESSOR_LIMIT_DECREMENT))
552 result = 0;
553 if (result) {
554 passive->flags.enabled = 0;
555 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
556 "Disabling passive cooling (zone is cool)\n"));
560 static void acpi_thermal_active(struct acpi_thermal *tz)
562 int result = 0;
563 struct acpi_thermal_active *active = NULL;
564 int i = 0;
565 int j = 0;
566 unsigned long maxtemp = 0;
569 if (!tz)
570 return;
572 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
573 active = &(tz->trips.active[i]);
574 if (!active || !active->flags.valid)
575 break;
576 if (tz->temperature >= active->temperature) {
578 * Above Threshold?
579 * ----------------
580 * If not already enabled, turn ON all cooling devices
581 * associated with this active threshold.
583 if (active->temperature > maxtemp)
584 tz->state.active_index = i;
585 maxtemp = active->temperature;
586 if (active->flags.enabled)
587 continue;
588 for (j = 0; j < active->devices.count; j++) {
589 result =
590 acpi_bus_set_power(active->devices.
591 handles[j],
592 ACPI_STATE_D0);
593 if (result) {
594 printk(KERN_WARNING PREFIX
595 "Unable to turn cooling device [%p] 'on'\n",
596 active->devices.
597 handles[j]);
598 continue;
600 active->flags.enabled = 1;
601 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
602 "Cooling device [%p] now 'on'\n",
603 active->devices.handles[j]));
605 continue;
607 if (!active->flags.enabled)
608 continue;
610 * Below Threshold?
611 * ----------------
612 * Turn OFF all cooling devices associated with this
613 * threshold.
615 for (j = 0; j < active->devices.count; j++) {
616 result = acpi_bus_set_power(active->devices.handles[j],
617 ACPI_STATE_D3);
618 if (result) {
619 printk(KERN_WARNING PREFIX
620 "Unable to turn cooling device [%p] 'off'\n",
621 active->devices.handles[j]);
622 continue;
624 active->flags.enabled = 0;
625 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
626 "Cooling device [%p] now 'off'\n",
627 active->devices.handles[j]));
632 static void acpi_thermal_check(void *context);
634 static void acpi_thermal_run(unsigned long data)
636 struct acpi_thermal *tz = (struct acpi_thermal *)data;
637 if (!tz->zombie)
638 acpi_os_execute(OSL_GPE_HANDLER, acpi_thermal_check, (void *)data);
641 static void acpi_thermal_check(void *data)
643 int result = 0;
644 struct acpi_thermal *tz = data;
645 unsigned long sleep_time = 0;
646 int i = 0;
647 struct acpi_thermal_state state;
650 if (!tz) {
651 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
652 return;
655 state = tz->state;
657 result = acpi_thermal_get_temperature(tz);
658 if (result)
659 return;
661 memset(&tz->state, 0, sizeof(tz->state));
664 * Check Trip Points
665 * -----------------
666 * Compare the current temperature to the trip point values to see
667 * if we've entered one of the thermal policy states. Note that
668 * this function determines when a state is entered, but the
669 * individual policy decides when it is exited (e.g. hysteresis).
671 if (tz->trips.critical.flags.valid)
672 state.critical |=
673 (tz->temperature >= tz->trips.critical.temperature);
674 if (tz->trips.hot.flags.valid)
675 state.hot |= (tz->temperature >= tz->trips.hot.temperature);
676 if (tz->trips.passive.flags.valid)
677 state.passive |=
678 (tz->temperature >= tz->trips.passive.temperature);
679 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
680 if (tz->trips.active[i].flags.valid)
681 state.active |=
682 (tz->temperature >=
683 tz->trips.active[i].temperature);
686 * Invoke Policy
687 * -------------
688 * Separated from the above check to allow individual policy to
689 * determine when to exit a given state.
691 if (state.critical)
692 acpi_thermal_critical(tz);
693 if (state.hot)
694 acpi_thermal_hot(tz);
695 if (state.passive)
696 acpi_thermal_passive(tz);
697 if (state.active)
698 acpi_thermal_active(tz);
701 * Calculate State
702 * ---------------
703 * Again, separated from the above two to allow independent policy
704 * decisions.
706 tz->state.critical = tz->trips.critical.flags.enabled;
707 tz->state.hot = tz->trips.hot.flags.enabled;
708 tz->state.passive = tz->trips.passive.flags.enabled;
709 tz->state.active = 0;
710 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
711 tz->state.active |= tz->trips.active[i].flags.enabled;
714 * Calculate Sleep Time
715 * --------------------
716 * If we're in the passive state, use _TSP's value. Otherwise
717 * use the default polling frequency (e.g. _TZP). If no polling
718 * frequency is specified then we'll wait forever (at least until
719 * a thermal event occurs). Note that _TSP and _TZD values are
720 * given in 1/10th seconds (we must covert to milliseconds).
722 if (tz->state.passive)
723 sleep_time = tz->trips.passive.tsp * 100;
724 else if (tz->polling_frequency > 0)
725 sleep_time = tz->polling_frequency * 100;
727 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: temperature[%lu] sleep[%lu]\n",
728 tz->name, tz->temperature, sleep_time));
731 * Schedule Next Poll
732 * ------------------
734 if (!sleep_time) {
735 if (timer_pending(&(tz->timer)))
736 del_timer(&(tz->timer));
737 } else {
738 if (timer_pending(&(tz->timer)))
739 mod_timer(&(tz->timer),
740 jiffies + (HZ * sleep_time) / 1000);
741 else {
742 tz->timer.data = (unsigned long)tz;
743 tz->timer.function = acpi_thermal_run;
744 tz->timer.expires = jiffies + (HZ * sleep_time) / 1000;
745 add_timer(&(tz->timer));
749 return;
752 /* --------------------------------------------------------------------------
753 FS Interface (/proc)
754 -------------------------------------------------------------------------- */
756 static struct proc_dir_entry *acpi_thermal_dir;
758 static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
760 struct acpi_thermal *tz = seq->private;
763 if (!tz)
764 goto end;
766 seq_puts(seq, "state: ");
768 if (!tz->state.critical && !tz->state.hot && !tz->state.passive
769 && !tz->state.active)
770 seq_puts(seq, "ok\n");
771 else {
772 if (tz->state.critical)
773 seq_puts(seq, "critical ");
774 if (tz->state.hot)
775 seq_puts(seq, "hot ");
776 if (tz->state.passive)
777 seq_puts(seq, "passive ");
778 if (tz->state.active)
779 seq_printf(seq, "active[%d]", tz->state.active_index);
780 seq_puts(seq, "\n");
783 end:
784 return 0;
787 static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
789 return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
792 static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
794 int result = 0;
795 struct acpi_thermal *tz = seq->private;
798 if (!tz)
799 goto end;
801 result = acpi_thermal_get_temperature(tz);
802 if (result)
803 goto end;
805 seq_printf(seq, "temperature: %ld C\n",
806 KELVIN_TO_CELSIUS(tz->temperature));
808 end:
809 return 0;
812 static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
814 return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
817 static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
819 struct acpi_thermal *tz = seq->private;
820 struct acpi_device *device;
821 acpi_status status;
823 int i = 0;
824 int j = 0;
827 if (!tz)
828 goto end;
830 if (tz->trips.critical.flags.valid)
831 seq_printf(seq, "critical (S5): %ld C\n",
832 KELVIN_TO_CELSIUS(tz->trips.critical.temperature));
834 if (tz->trips.hot.flags.valid)
835 seq_printf(seq, "hot (S4): %ld C\n",
836 KELVIN_TO_CELSIUS(tz->trips.hot.temperature));
838 if (tz->trips.passive.flags.valid) {
839 seq_printf(seq,
840 "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
841 KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
842 tz->trips.passive.tc1, tz->trips.passive.tc2,
843 tz->trips.passive.tsp);
844 for (j = 0; j < tz->trips.passive.devices.count; j++) {
845 status = acpi_bus_get_device(tz->trips.passive.devices.
846 handles[j], &device);
847 seq_printf(seq, "%4.4s ", status ? "" :
848 acpi_device_bid(device));
850 seq_puts(seq, "\n");
853 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
854 if (!(tz->trips.active[i].flags.valid))
855 break;
856 seq_printf(seq, "active[%d]: %ld C: devices=",
858 KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
859 for (j = 0; j < tz->trips.active[i].devices.count; j++){
860 status = acpi_bus_get_device(tz->trips.active[i].
861 devices.handles[j],
862 &device);
863 seq_printf(seq, "%4.4s ", status ? "" :
864 acpi_device_bid(device));
866 seq_puts(seq, "\n");
869 end:
870 return 0;
873 static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
875 return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
878 static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
880 struct acpi_thermal *tz = seq->private;
883 if (!tz)
884 goto end;
886 if (!tz->flags.cooling_mode)
887 seq_puts(seq, "<setting not supported>\n");
888 else
889 seq_puts(seq, "0 - Active; 1 - Passive\n");
891 end:
892 return 0;
895 static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
897 return single_open(file, acpi_thermal_cooling_seq_show,
898 PDE(inode)->data);
901 static ssize_t
902 acpi_thermal_write_cooling_mode(struct file *file,
903 const char __user * buffer,
904 size_t count, loff_t * ppos)
906 struct seq_file *m = file->private_data;
907 struct acpi_thermal *tz = m->private;
908 int result = 0;
909 char mode_string[12] = { '\0' };
912 if (!tz || (count > sizeof(mode_string) - 1))
913 return -EINVAL;
915 if (!tz->flags.cooling_mode)
916 return -ENODEV;
918 if (copy_from_user(mode_string, buffer, count))
919 return -EFAULT;
921 mode_string[count] = '\0';
923 result = acpi_thermal_set_cooling_mode(tz,
924 simple_strtoul(mode_string, NULL,
925 0));
926 if (result)
927 return result;
929 acpi_thermal_check(tz);
931 return count;
934 static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
936 struct acpi_thermal *tz = seq->private;
939 if (!tz)
940 goto end;
942 if (!tz->polling_frequency) {
943 seq_puts(seq, "<polling disabled>\n");
944 goto end;
947 seq_printf(seq, "polling frequency: %lu seconds\n",
948 (tz->polling_frequency / 10));
950 end:
951 return 0;
954 static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
956 return single_open(file, acpi_thermal_polling_seq_show,
957 PDE(inode)->data);
960 static ssize_t
961 acpi_thermal_write_polling(struct file *file,
962 const char __user * buffer,
963 size_t count, loff_t * ppos)
965 struct seq_file *m = file->private_data;
966 struct acpi_thermal *tz = m->private;
967 int result = 0;
968 char polling_string[12] = { '\0' };
969 int seconds = 0;
972 if (!tz || (count > sizeof(polling_string) - 1))
973 return -EINVAL;
975 if (copy_from_user(polling_string, buffer, count))
976 return -EFAULT;
978 polling_string[count] = '\0';
980 seconds = simple_strtoul(polling_string, NULL, 0);
982 result = acpi_thermal_set_polling(tz, seconds);
983 if (result)
984 return result;
986 acpi_thermal_check(tz);
988 return count;
991 static int acpi_thermal_add_fs(struct acpi_device *device)
993 struct proc_dir_entry *entry = NULL;
996 if (!acpi_device_dir(device)) {
997 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
998 acpi_thermal_dir);
999 if (!acpi_device_dir(device))
1000 return -ENODEV;
1001 acpi_device_dir(device)->owner = THIS_MODULE;
1004 /* 'state' [R] */
1005 entry = create_proc_entry(ACPI_THERMAL_FILE_STATE,
1006 S_IRUGO, acpi_device_dir(device));
1007 if (!entry)
1008 return -ENODEV;
1009 else {
1010 entry->proc_fops = &acpi_thermal_state_fops;
1011 entry->data = acpi_driver_data(device);
1012 entry->owner = THIS_MODULE;
1015 /* 'temperature' [R] */
1016 entry = create_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1017 S_IRUGO, acpi_device_dir(device));
1018 if (!entry)
1019 return -ENODEV;
1020 else {
1021 entry->proc_fops = &acpi_thermal_temp_fops;
1022 entry->data = acpi_driver_data(device);
1023 entry->owner = THIS_MODULE;
1026 /* 'trip_points' [R/W] */
1027 entry = create_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1028 S_IFREG | S_IRUGO | S_IWUSR,
1029 acpi_device_dir(device));
1030 if (!entry)
1031 return -ENODEV;
1032 else {
1033 entry->proc_fops = &acpi_thermal_trip_fops;
1034 entry->data = acpi_driver_data(device);
1035 entry->owner = THIS_MODULE;
1038 /* 'cooling_mode' [R/W] */
1039 entry = create_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1040 S_IFREG | S_IRUGO | S_IWUSR,
1041 acpi_device_dir(device));
1042 if (!entry)
1043 return -ENODEV;
1044 else {
1045 entry->proc_fops = &acpi_thermal_cooling_fops;
1046 entry->data = acpi_driver_data(device);
1047 entry->owner = THIS_MODULE;
1050 /* 'polling_frequency' [R/W] */
1051 entry = create_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1052 S_IFREG | S_IRUGO | S_IWUSR,
1053 acpi_device_dir(device));
1054 if (!entry)
1055 return -ENODEV;
1056 else {
1057 entry->proc_fops = &acpi_thermal_polling_fops;
1058 entry->data = acpi_driver_data(device);
1059 entry->owner = THIS_MODULE;
1062 return 0;
1065 static int acpi_thermal_remove_fs(struct acpi_device *device)
1068 if (acpi_device_dir(device)) {
1069 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1070 acpi_device_dir(device));
1071 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1072 acpi_device_dir(device));
1073 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1074 acpi_device_dir(device));
1075 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1076 acpi_device_dir(device));
1077 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1078 acpi_device_dir(device));
1079 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1080 acpi_device_dir(device) = NULL;
1083 return 0;
1086 /* --------------------------------------------------------------------------
1087 Driver Interface
1088 -------------------------------------------------------------------------- */
1090 static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
1092 struct acpi_thermal *tz = data;
1093 struct acpi_device *device = NULL;
1096 if (!tz)
1097 return;
1099 device = tz->device;
1101 switch (event) {
1102 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1103 acpi_thermal_check(tz);
1104 break;
1105 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
1106 acpi_thermal_get_trip_points(tz);
1107 acpi_thermal_check(tz);
1108 acpi_bus_generate_event(device, event, 0);
1109 break;
1110 case ACPI_THERMAL_NOTIFY_DEVICES:
1111 if (tz->flags.devices)
1112 acpi_thermal_get_devices(tz);
1113 acpi_bus_generate_event(device, event, 0);
1114 break;
1115 default:
1116 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1117 "Unsupported event [0x%x]\n", event));
1118 break;
1121 return;
1124 static int acpi_thermal_get_info(struct acpi_thermal *tz)
1126 int result = 0;
1129 if (!tz)
1130 return -EINVAL;
1132 /* Get temperature [_TMP] (required) */
1133 result = acpi_thermal_get_temperature(tz);
1134 if (result)
1135 return result;
1137 /* Get trip points [_CRT, _PSV, etc.] (required) */
1138 result = acpi_thermal_get_trip_points(tz);
1139 if (result)
1140 return result;
1142 /* Set the cooling mode [_SCP] to active cooling (default) */
1143 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
1144 if (!result)
1145 tz->flags.cooling_mode = 1;
1147 /* Get default polling frequency [_TZP] (optional) */
1148 if (tzp)
1149 tz->polling_frequency = tzp;
1150 else
1151 acpi_thermal_get_polling_frequency(tz);
1153 /* Get devices in this thermal zone [_TZD] (optional) */
1154 result = acpi_thermal_get_devices(tz);
1155 if (!result)
1156 tz->flags.devices = 1;
1158 return 0;
1161 static int acpi_thermal_add(struct acpi_device *device)
1163 int result = 0;
1164 acpi_status status = AE_OK;
1165 struct acpi_thermal *tz = NULL;
1168 if (!device)
1169 return -EINVAL;
1171 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
1172 if (!tz)
1173 return -ENOMEM;
1175 tz->device = device;
1176 strcpy(tz->name, device->pnp.bus_id);
1177 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1178 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1179 acpi_driver_data(device) = tz;
1181 result = acpi_thermal_get_info(tz);
1182 if (result)
1183 goto end;
1185 result = acpi_thermal_add_fs(device);
1186 if (result)
1187 goto end;
1189 init_timer(&tz->timer);
1191 acpi_thermal_check(tz);
1193 status = acpi_install_notify_handler(device->handle,
1194 ACPI_DEVICE_NOTIFY,
1195 acpi_thermal_notify, tz);
1196 if (ACPI_FAILURE(status)) {
1197 result = -ENODEV;
1198 goto end;
1201 printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
1202 acpi_device_name(device), acpi_device_bid(device),
1203 KELVIN_TO_CELSIUS(tz->temperature));
1205 end:
1206 if (result) {
1207 acpi_thermal_remove_fs(device);
1208 kfree(tz);
1211 return result;
1214 static int acpi_thermal_remove(struct acpi_device *device, int type)
1216 acpi_status status = AE_OK;
1217 struct acpi_thermal *tz = NULL;
1220 if (!device || !acpi_driver_data(device))
1221 return -EINVAL;
1223 tz = acpi_driver_data(device);
1225 /* avoid timer adding new defer task */
1226 tz->zombie = 1;
1227 /* wait for running timer (on other CPUs) finish */
1228 del_timer_sync(&(tz->timer));
1229 /* synchronize deferred task */
1230 acpi_os_wait_events_complete(NULL);
1231 /* deferred task may reinsert timer */
1232 del_timer_sync(&(tz->timer));
1234 status = acpi_remove_notify_handler(device->handle,
1235 ACPI_DEVICE_NOTIFY,
1236 acpi_thermal_notify);
1238 /* Terminate policy */
1239 if (tz->trips.passive.flags.valid && tz->trips.passive.flags.enabled) {
1240 tz->trips.passive.flags.enabled = 0;
1241 acpi_thermal_passive(tz);
1243 if (tz->trips.active[0].flags.valid
1244 && tz->trips.active[0].flags.enabled) {
1245 tz->trips.active[0].flags.enabled = 0;
1246 acpi_thermal_active(tz);
1249 acpi_thermal_remove_fs(device);
1251 kfree(tz);
1252 return 0;
1255 static int acpi_thermal_resume(struct acpi_device *device)
1257 struct acpi_thermal *tz = NULL;
1258 int i, j, power_state, result;
1261 if (!device || !acpi_driver_data(device))
1262 return -EINVAL;
1264 tz = acpi_driver_data(device);
1266 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1267 if (!(&tz->trips.active[i]))
1268 break;
1269 if (!tz->trips.active[i].flags.valid)
1270 break;
1271 tz->trips.active[i].flags.enabled = 1;
1272 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1273 result = acpi_bus_get_power(tz->trips.active[i].devices.
1274 handles[j], &power_state);
1275 if (result || (power_state != ACPI_STATE_D0)) {
1276 tz->trips.active[i].flags.enabled = 0;
1277 break;
1280 tz->state.active |= tz->trips.active[i].flags.enabled;
1283 acpi_thermal_check(tz);
1285 return AE_OK;
1288 static int __init acpi_thermal_init(void)
1290 int result = 0;
1292 if (off) {
1293 printk(KERN_NOTICE "ACPI: thermal control disabled\n");
1294 return -ENODEV;
1296 acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1297 if (!acpi_thermal_dir)
1298 return -ENODEV;
1299 acpi_thermal_dir->owner = THIS_MODULE;
1301 result = acpi_bus_register_driver(&acpi_thermal_driver);
1302 if (result < 0) {
1303 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1304 return -ENODEV;
1307 return 0;
1310 static void __exit acpi_thermal_exit(void)
1313 acpi_bus_unregister_driver(&acpi_thermal_driver);
1315 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1317 return;
1320 module_init(acpi_thermal_init);
1321 module_exit(acpi_thermal_exit);