2 * acpi_thermal.c - ACPI Thermal Zone Driver ($Revision: 41 $)
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 * This driver fully implements the ACPI thermal policy as described in the
26 * ACPI 2.0 Specification.
28 * TBD: 1. Implement passive cooling hysteresis.
29 * 2. Enhance passive cooling (CPU) states/limit interface to support
30 * concepts of 'multiple limiters', upper/lower limits, etc.
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/dmi.h>
37 #include <linux/init.h>
38 #include <linux/types.h>
39 #include <linux/proc_fs.h>
40 #include <linux/timer.h>
41 #include <linux/jiffies.h>
42 #include <linux/kmod.h>
43 #include <linux/seq_file.h>
44 #include <linux/reboot.h>
45 #include <asm/uaccess.h>
46 #include <linux/thermal.h>
47 #include <acpi/acpi_bus.h>
48 #include <acpi/acpi_drivers.h>
50 #define ACPI_THERMAL_COMPONENT 0x04000000
51 #define ACPI_THERMAL_CLASS "thermal_zone"
52 #define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
53 #define ACPI_THERMAL_FILE_STATE "state"
54 #define ACPI_THERMAL_FILE_TEMPERATURE "temperature"
55 #define ACPI_THERMAL_FILE_TRIP_POINTS "trip_points"
56 #define ACPI_THERMAL_FILE_COOLING_MODE "cooling_mode"
57 #define ACPI_THERMAL_FILE_POLLING_FREQ "polling_frequency"
58 #define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
59 #define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
60 #define ACPI_THERMAL_NOTIFY_DEVICES 0x82
61 #define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
62 #define ACPI_THERMAL_NOTIFY_HOT 0xF1
63 #define ACPI_THERMAL_MODE_ACTIVE 0x00
65 #define ACPI_THERMAL_MAX_ACTIVE 10
66 #define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
68 #define KELVIN_TO_CELSIUS(t) (long)(((long)t-2732>=0) ? ((long)t-2732+5)/10 : ((long)t-2732-5)/10)
69 #define CELSIUS_TO_KELVIN(t) ((t+273)*10)
71 #define _COMPONENT ACPI_THERMAL_COMPONENT
72 ACPI_MODULE_NAME("thermal");
74 MODULE_AUTHOR("Paul Diefenbaugh");
75 MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
76 MODULE_LICENSE("GPL");
79 module_param(act, int, 0644);
80 MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.");
83 module_param(crt, int, 0644);
84 MODULE_PARM_DESC(crt, "Disable or lower all critical trip points.");
87 module_param(tzp, int, 0444);
88 MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.");
91 module_param(nocrt, int, 0);
92 MODULE_PARM_DESC(nocrt, "Set to take no action upon ACPI thermal zone critical trips points.");
95 module_param(off, int, 0);
96 MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
99 module_param(psv, int, 0644);
100 MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
102 static int acpi_thermal_add(struct acpi_device *device);
103 static int acpi_thermal_remove(struct acpi_device *device, int type);
104 static int acpi_thermal_resume(struct acpi_device *device);
105 static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
106 static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
107 static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
108 static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
109 static ssize_t acpi_thermal_write_cooling_mode(struct file *,
110 const char __user *, size_t,
112 static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
113 static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
116 static const struct acpi_device_id thermal_device_ids[] = {
117 {ACPI_THERMAL_HID, 0},
120 MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
122 static struct acpi_driver acpi_thermal_driver = {
124 .class = ACPI_THERMAL_CLASS,
125 .ids = thermal_device_ids,
127 .add = acpi_thermal_add,
128 .remove = acpi_thermal_remove,
129 .resume = acpi_thermal_resume,
133 struct acpi_thermal_state {
142 struct acpi_thermal_state_flags {
148 struct acpi_thermal_critical {
149 struct acpi_thermal_state_flags flags;
150 unsigned long temperature;
153 struct acpi_thermal_hot {
154 struct acpi_thermal_state_flags flags;
155 unsigned long temperature;
158 struct acpi_thermal_passive {
159 struct acpi_thermal_state_flags flags;
160 unsigned long temperature;
164 struct acpi_handle_list devices;
167 struct acpi_thermal_active {
168 struct acpi_thermal_state_flags flags;
169 unsigned long temperature;
170 struct acpi_handle_list devices;
173 struct acpi_thermal_trips {
174 struct acpi_thermal_critical critical;
175 struct acpi_thermal_hot hot;
176 struct acpi_thermal_passive passive;
177 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
180 struct acpi_thermal_flags {
181 u8 cooling_mode:1; /* _SCP */
182 u8 devices:1; /* _TZD */
186 struct acpi_thermal {
187 struct acpi_device * device;
189 unsigned long temperature;
190 unsigned long last_temperature;
191 unsigned long polling_frequency;
193 struct acpi_thermal_flags flags;
194 struct acpi_thermal_state state;
195 struct acpi_thermal_trips trips;
196 struct acpi_handle_list devices;
197 struct timer_list timer;
198 struct thermal_zone_device *thermal_zone;
203 static const struct file_operations acpi_thermal_state_fops = {
204 .open = acpi_thermal_state_open_fs,
207 .release = single_release,
210 static const struct file_operations acpi_thermal_temp_fops = {
211 .open = acpi_thermal_temp_open_fs,
214 .release = single_release,
217 static const struct file_operations acpi_thermal_trip_fops = {
218 .open = acpi_thermal_trip_open_fs,
221 .release = single_release,
224 static const struct file_operations acpi_thermal_cooling_fops = {
225 .open = acpi_thermal_cooling_open_fs,
227 .write = acpi_thermal_write_cooling_mode,
229 .release = single_release,
232 static const struct file_operations acpi_thermal_polling_fops = {
233 .open = acpi_thermal_polling_open_fs,
235 .write = acpi_thermal_write_polling,
237 .release = single_release,
240 /* --------------------------------------------------------------------------
241 Thermal Zone Management
242 -------------------------------------------------------------------------- */
244 static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
246 acpi_status status = AE_OK;
252 tz->last_temperature = tz->temperature;
255 acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tz->temperature);
256 if (ACPI_FAILURE(status))
259 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
265 static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
267 acpi_status status = AE_OK;
274 acpi_evaluate_integer(tz->device->handle, "_TZP", NULL,
275 &tz->polling_frequency);
276 if (ACPI_FAILURE(status))
279 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
280 tz->polling_frequency));
285 static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
291 tz->polling_frequency = seconds * 10; /* Convert value to deci-seconds */
293 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
294 "Polling frequency set to %lu seconds\n",
295 tz->polling_frequency/10));
300 static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
302 acpi_status status = AE_OK;
303 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
304 struct acpi_object_list arg_list = { 1, &arg0 };
305 acpi_handle handle = NULL;
311 status = acpi_get_handle(tz->device->handle, "_SCP", &handle);
312 if (ACPI_FAILURE(status)) {
313 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
317 arg0.integer.value = mode;
319 status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
320 if (ACPI_FAILURE(status))
326 static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
328 acpi_status status = AE_OK;
335 /* Critical Shutdown (required) */
337 status = acpi_evaluate_integer(tz->device->handle, "_CRT", NULL,
338 &tz->trips.critical.temperature);
339 if (ACPI_FAILURE(status)) {
340 tz->trips.critical.flags.valid = 0;
341 ACPI_EXCEPTION((AE_INFO, status, "No critical threshold"));
344 tz->trips.critical.flags.valid = 1;
345 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
346 "Found critical threshold [%lu]\n",
347 tz->trips.critical.temperature));
350 if (tz->trips.critical.flags.valid == 1) {
352 tz->trips.critical.flags.valid = 0;
353 } else if (crt > 0) {
354 unsigned long crt_k = CELSIUS_TO_KELVIN(crt);
357 * Allow override to lower critical threshold
359 if (crt_k < tz->trips.critical.temperature)
360 tz->trips.critical.temperature = crt_k;
364 /* Critical Sleep (optional) */
367 acpi_evaluate_integer(tz->device->handle, "_HOT", NULL,
368 &tz->trips.hot.temperature);
369 if (ACPI_FAILURE(status)) {
370 tz->trips.hot.flags.valid = 0;
371 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No hot threshold\n"));
373 tz->trips.hot.flags.valid = 1;
374 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found hot threshold [%lu]\n",
375 tz->trips.hot.temperature));
378 /* Passive: Processors (optional) */
382 } else if (psv > 0) {
383 tz->trips.passive.temperature = CELSIUS_TO_KELVIN(psv);
386 status = acpi_evaluate_integer(tz->device->handle,
387 "_PSV", NULL, &tz->trips.passive.temperature);
390 if (ACPI_FAILURE(status)) {
391 tz->trips.passive.flags.valid = 0;
392 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No passive threshold\n"));
394 tz->trips.passive.flags.valid = 1;
397 acpi_evaluate_integer(tz->device->handle, "_TC1", NULL,
398 &tz->trips.passive.tc1);
399 if (ACPI_FAILURE(status))
400 tz->trips.passive.flags.valid = 0;
403 acpi_evaluate_integer(tz->device->handle, "_TC2", NULL,
404 &tz->trips.passive.tc2);
405 if (ACPI_FAILURE(status))
406 tz->trips.passive.flags.valid = 0;
409 acpi_evaluate_integer(tz->device->handle, "_TSP", NULL,
410 &tz->trips.passive.tsp);
411 if (ACPI_FAILURE(status))
412 tz->trips.passive.flags.valid = 0;
415 acpi_evaluate_reference(tz->device->handle, "_PSL", NULL,
416 &tz->trips.passive.devices);
417 if (ACPI_FAILURE(status))
418 tz->trips.passive.flags.valid = 0;
420 if (!tz->trips.passive.flags.valid)
421 printk(KERN_WARNING PREFIX "Invalid passive threshold\n");
423 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
424 "Found passive threshold [%lu]\n",
425 tz->trips.passive.temperature));
428 /* Active: Fans, etc. (optional) */
430 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
432 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
435 break; /* disable all active trip points */
437 status = acpi_evaluate_integer(tz->device->handle,
438 name, NULL, &tz->trips.active[i].temperature);
440 if (ACPI_FAILURE(status)) {
441 if (i == 0) /* no active trip points */
443 if (act <= 0) /* no override requested */
445 if (i == 1) { /* 1 trip point */
446 tz->trips.active[0].temperature =
447 CELSIUS_TO_KELVIN(act);
448 } else { /* multiple trips */
450 * Don't allow override higher than
451 * the next higher trip point
453 tz->trips.active[i - 1].temperature =
454 (tz->trips.active[i - 2].temperature <
455 CELSIUS_TO_KELVIN(act) ?
456 tz->trips.active[i - 2].temperature :
457 CELSIUS_TO_KELVIN(act));
464 acpi_evaluate_reference(tz->device->handle, name, NULL,
465 &tz->trips.active[i].devices);
466 if (ACPI_SUCCESS(status)) {
467 tz->trips.active[i].flags.valid = 1;
468 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
469 "Found active threshold [%d]:[%lu]\n",
470 i, tz->trips.active[i].temperature));
472 ACPI_EXCEPTION((AE_INFO, status,
473 "Invalid active threshold [%d]", i));
479 static int acpi_thermal_get_devices(struct acpi_thermal *tz)
481 acpi_status status = AE_OK;
488 acpi_evaluate_reference(tz->device->handle, "_TZD", NULL, &tz->devices);
489 if (ACPI_FAILURE(status))
495 static int acpi_thermal_critical(struct acpi_thermal *tz)
497 if (!tz || !tz->trips.critical.flags.valid || nocrt)
500 if (tz->temperature >= tz->trips.critical.temperature) {
501 printk(KERN_WARNING PREFIX "Critical trip point\n");
502 tz->trips.critical.flags.enabled = 1;
503 } else if (tz->trips.critical.flags.enabled)
504 tz->trips.critical.flags.enabled = 0;
507 "Critical temperature reached (%ld C), shutting down.\n",
508 KELVIN_TO_CELSIUS(tz->temperature));
509 acpi_bus_generate_proc_event(tz->device, ACPI_THERMAL_NOTIFY_CRITICAL,
510 tz->trips.critical.flags.enabled);
511 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
512 tz->device->dev.bus_id,
513 ACPI_THERMAL_NOTIFY_CRITICAL,
514 tz->trips.critical.flags.enabled);
516 orderly_poweroff(true);
521 static int acpi_thermal_hot(struct acpi_thermal *tz)
523 if (!tz || !tz->trips.hot.flags.valid || nocrt)
526 if (tz->temperature >= tz->trips.hot.temperature) {
527 printk(KERN_WARNING PREFIX "Hot trip point\n");
528 tz->trips.hot.flags.enabled = 1;
529 } else if (tz->trips.hot.flags.enabled)
530 tz->trips.hot.flags.enabled = 0;
532 acpi_bus_generate_proc_event(tz->device, ACPI_THERMAL_NOTIFY_HOT,
533 tz->trips.hot.flags.enabled);
534 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
535 tz->device->dev.bus_id,
536 ACPI_THERMAL_NOTIFY_HOT,
537 tz->trips.hot.flags.enabled);
539 /* TBD: Call user-mode "sleep(S4)" function */
544 static void acpi_thermal_passive(struct acpi_thermal *tz)
547 struct acpi_thermal_passive *passive = NULL;
552 if (!tz || !tz->trips.passive.flags.valid)
555 passive = &(tz->trips.passive);
560 * Calculate the thermal trend (using the passive cooling equation)
561 * and modify the performance limit for all passive cooling devices
562 * accordingly. Note that we assume symmetry.
564 if (tz->temperature >= passive->temperature) {
566 (passive->tc1 * (tz->temperature - tz->last_temperature)) +
567 (passive->tc2 * (tz->temperature - passive->temperature));
568 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
569 "trend[%d]=(tc1[%lu]*(tmp[%lu]-last[%lu]))+(tc2[%lu]*(tmp[%lu]-psv[%lu]))\n",
570 trend, passive->tc1, tz->temperature,
571 tz->last_temperature, passive->tc2,
572 tz->temperature, passive->temperature));
573 passive->flags.enabled = 1;
576 for (i = 0; i < passive->devices.count; i++)
577 acpi_processor_set_thermal_limit(passive->
580 ACPI_PROCESSOR_LIMIT_INCREMENT);
582 else if (trend < 0) {
583 for (i = 0; i < passive->devices.count; i++)
585 * assume that we are on highest
586 * freq/lowest thrott and can leave
587 * passive mode, even in error case
589 if (!acpi_processor_set_thermal_limit
590 (passive->devices.handles[i],
591 ACPI_PROCESSOR_LIMIT_DECREMENT))
594 * Leave cooling mode, even if the temp might
595 * higher than trip point This is because some
596 * machines might have long thermal polling
597 * frequencies (tsp) defined. We will fall back
598 * into passive mode in next cycle (probably quicker)
601 passive->flags.enabled = 0;
602 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
603 "Disabling passive cooling, still above threshold,"
604 " but we are cooling down\n"));
613 * Implement passive cooling hysteresis to slowly increase performance
614 * and avoid thrashing around the passive trip point. Note that we
617 if (!passive->flags.enabled)
619 for (i = 0; i < passive->devices.count; i++)
620 if (!acpi_processor_set_thermal_limit
621 (passive->devices.handles[i],
622 ACPI_PROCESSOR_LIMIT_DECREMENT))
625 passive->flags.enabled = 0;
626 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
627 "Disabling passive cooling (zone is cool)\n"));
631 static void acpi_thermal_active(struct acpi_thermal *tz)
634 struct acpi_thermal_active *active = NULL;
637 unsigned long maxtemp = 0;
643 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
644 active = &(tz->trips.active[i]);
645 if (!active || !active->flags.valid)
647 if (tz->temperature >= active->temperature) {
651 * If not already enabled, turn ON all cooling devices
652 * associated with this active threshold.
654 if (active->temperature > maxtemp)
655 tz->state.active_index = i;
656 maxtemp = active->temperature;
657 if (active->flags.enabled)
659 for (j = 0; j < active->devices.count; j++) {
661 acpi_bus_set_power(active->devices.
665 printk(KERN_WARNING PREFIX
666 "Unable to turn cooling device [%p] 'on'\n",
671 active->flags.enabled = 1;
672 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
673 "Cooling device [%p] now 'on'\n",
674 active->devices.handles[j]));
678 if (!active->flags.enabled)
683 * Turn OFF all cooling devices associated with this
686 for (j = 0; j < active->devices.count; j++) {
687 result = acpi_bus_set_power(active->devices.handles[j],
690 printk(KERN_WARNING PREFIX
691 "Unable to turn cooling device [%p] 'off'\n",
692 active->devices.handles[j]);
695 active->flags.enabled = 0;
696 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
697 "Cooling device [%p] now 'off'\n",
698 active->devices.handles[j]));
703 static void acpi_thermal_check(void *context);
705 static void acpi_thermal_run(unsigned long data)
707 struct acpi_thermal *tz = (struct acpi_thermal *)data;
709 acpi_os_execute(OSL_GPE_HANDLER, acpi_thermal_check, (void *)data);
712 static void acpi_thermal_check(void *data)
715 struct acpi_thermal *tz = data;
716 unsigned long sleep_time = 0;
717 unsigned long timeout_jiffies = 0;
719 struct acpi_thermal_state state;
723 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
727 /* Check if someone else is already running */
728 if (!mutex_trylock(&tz->lock))
733 result = acpi_thermal_get_temperature(tz);
740 memset(&tz->state, 0, sizeof(tz->state));
745 * Compare the current temperature to the trip point values to see
746 * if we've entered one of the thermal policy states. Note that
747 * this function determines when a state is entered, but the
748 * individual policy decides when it is exited (e.g. hysteresis).
750 if (tz->trips.critical.flags.valid)
752 (tz->temperature >= tz->trips.critical.temperature);
753 if (tz->trips.hot.flags.valid)
754 state.hot |= (tz->temperature >= tz->trips.hot.temperature);
755 if (tz->trips.passive.flags.valid)
757 (tz->temperature >= tz->trips.passive.temperature);
758 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
759 if (tz->trips.active[i].flags.valid)
762 tz->trips.active[i].temperature);
767 * Separated from the above check to allow individual policy to
768 * determine when to exit a given state.
771 acpi_thermal_critical(tz);
773 acpi_thermal_hot(tz);
775 acpi_thermal_passive(tz);
777 acpi_thermal_active(tz);
782 * Again, separated from the above two to allow independent policy
785 tz->state.critical = tz->trips.critical.flags.enabled;
786 tz->state.hot = tz->trips.hot.flags.enabled;
787 tz->state.passive = tz->trips.passive.flags.enabled;
788 tz->state.active = 0;
789 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
790 tz->state.active |= tz->trips.active[i].flags.enabled;
793 * Calculate Sleep Time
794 * --------------------
795 * If we're in the passive state, use _TSP's value. Otherwise
796 * use the default polling frequency (e.g. _TZP). If no polling
797 * frequency is specified then we'll wait forever (at least until
798 * a thermal event occurs). Note that _TSP and _TZD values are
799 * given in 1/10th seconds (we must covert to milliseconds).
801 if (tz->state.passive) {
802 sleep_time = tz->trips.passive.tsp * 100;
803 timeout_jiffies = jiffies + (HZ * sleep_time) / 1000;
804 } else if (tz->polling_frequency > 0) {
805 sleep_time = tz->polling_frequency * 100;
806 timeout_jiffies = round_jiffies(jiffies + (HZ * sleep_time) / 1000);
809 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: temperature[%lu] sleep[%lu]\n",
810 tz->name, tz->temperature, sleep_time));
817 if (timer_pending(&(tz->timer)))
818 del_timer(&(tz->timer));
820 if (timer_pending(&(tz->timer)))
821 mod_timer(&(tz->timer), timeout_jiffies);
823 tz->timer.data = (unsigned long)tz;
824 tz->timer.function = acpi_thermal_run;
825 tz->timer.expires = timeout_jiffies;
826 add_timer(&(tz->timer));
830 mutex_unlock(&tz->lock);
833 /* sys I/F for generic thermal sysfs support */
834 static int thermal_get_temp(struct thermal_zone_device *thermal, char *buf)
836 struct acpi_thermal *tz = thermal->devdata;
841 return sprintf(buf, "%ld\n", KELVIN_TO_CELSIUS(tz->temperature));
844 static const char enabled[] = "kernel";
845 static const char disabled[] = "user";
846 static int thermal_get_mode(struct thermal_zone_device *thermal,
849 struct acpi_thermal *tz = thermal->devdata;
854 return sprintf(buf, "%s\n", tz->tz_enabled ?
858 static int thermal_set_mode(struct thermal_zone_device *thermal,
861 struct acpi_thermal *tz = thermal->devdata;
868 * enable/disable thermal management from ACPI thermal driver
870 if (!strncmp(buf, enabled, sizeof enabled - 1))
872 else if (!strncmp(buf, disabled, sizeof disabled - 1))
877 if (enable != tz->tz_enabled) {
878 tz->tz_enabled = enable;
879 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
880 "%s ACPI thermal control\n",
881 tz->tz_enabled ? enabled : disabled));
882 acpi_thermal_check(tz);
887 static int thermal_get_trip_type(struct thermal_zone_device *thermal,
890 struct acpi_thermal *tz = thermal->devdata;
896 if (tz->trips.critical.flags.valid) {
898 return sprintf(buf, "critical\n");
902 if (tz->trips.hot.flags.valid) {
904 return sprintf(buf, "hot\n");
908 if (tz->trips.passive.flags.valid) {
910 return sprintf(buf, "passive\n");
914 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
915 tz->trips.active[i].flags.valid; i++) {
917 return sprintf(buf, "active%d\n", i);
924 static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
927 struct acpi_thermal *tz = thermal->devdata;
933 if (tz->trips.critical.flags.valid) {
935 return sprintf(buf, "%ld\n", KELVIN_TO_CELSIUS(
936 tz->trips.critical.temperature));
940 if (tz->trips.hot.flags.valid) {
942 return sprintf(buf, "%ld\n", KELVIN_TO_CELSIUS(
943 tz->trips.hot.temperature));
947 if (tz->trips.passive.flags.valid) {
949 return sprintf(buf, "%ld\n", KELVIN_TO_CELSIUS(
950 tz->trips.passive.temperature));
954 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
955 tz->trips.active[i].flags.valid; i++) {
957 return sprintf(buf, "%ld\n", KELVIN_TO_CELSIUS(
958 tz->trips.active[i].temperature));
965 typedef int (*cb)(struct thermal_zone_device *, int,
966 struct thermal_cooling_device *);
967 static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
968 struct thermal_cooling_device *cdev,
971 struct acpi_device *device = cdev->devdata;
972 struct acpi_thermal *tz = thermal->devdata;
973 acpi_handle handle = device->handle;
979 if (tz->trips.critical.flags.valid)
982 if (tz->trips.hot.flags.valid)
985 if (tz->trips.passive.flags.valid) {
987 for (i = 0; i < tz->trips.passive.devices.count;
989 if (tz->trips.passive.devices.handles[i] !=
992 result = action(thermal, trip, cdev);
998 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
999 if (!tz->trips.active[i].flags.valid)
1003 j < tz->trips.active[i].devices.count;
1005 if (tz->trips.active[i].devices.
1006 handles[j] != handle)
1008 result = action(thermal, trip, cdev);
1014 for (i = 0; i < tz->devices.count; i++) {
1015 if (tz->devices.handles[i] != handle)
1017 result = action(thermal, -1, cdev);
1027 acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
1028 struct thermal_cooling_device *cdev)
1030 return acpi_thermal_cooling_device_cb(thermal, cdev,
1031 thermal_zone_bind_cooling_device);
1035 acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
1036 struct thermal_cooling_device *cdev)
1038 return acpi_thermal_cooling_device_cb(thermal, cdev,
1039 thermal_zone_unbind_cooling_device);
1042 static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
1043 .bind = acpi_thermal_bind_cooling_device,
1044 .unbind = acpi_thermal_unbind_cooling_device,
1045 .get_temp = thermal_get_temp,
1046 .get_mode = thermal_get_mode,
1047 .set_mode = thermal_set_mode,
1048 .get_trip_type = thermal_get_trip_type,
1049 .get_trip_temp = thermal_get_trip_temp,
1052 static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
1058 if (tz->trips.critical.flags.valid)
1061 if (tz->trips.hot.flags.valid)
1064 if (tz->trips.passive.flags.valid)
1067 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
1068 tz->trips.active[i].flags.valid; i++, trips++);
1069 tz->thermal_zone = thermal_zone_device_register("ACPI thermal zone",
1070 trips, tz, &acpi_thermal_zone_ops);
1071 if (!tz->thermal_zone)
1074 result = sysfs_create_link(&tz->device->dev.kobj,
1075 &tz->thermal_zone->device.kobj, "thermal_zone");
1079 result = sysfs_create_link(&tz->thermal_zone->device.kobj,
1080 &tz->device->dev.kobj, "device");
1086 printk(KERN_INFO PREFIX "%s is registered as thermal_zone%d\n",
1087 tz->device->dev.bus_id, tz->thermal_zone->id);
1091 static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
1093 sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
1094 sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
1095 thermal_zone_device_unregister(tz->thermal_zone);
1096 tz->thermal_zone = NULL;
1100 /* --------------------------------------------------------------------------
1101 FS Interface (/proc)
1102 -------------------------------------------------------------------------- */
1104 static struct proc_dir_entry *acpi_thermal_dir;
1106 static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
1108 struct acpi_thermal *tz = seq->private;
1114 seq_puts(seq, "state: ");
1116 if (!tz->state.critical && !tz->state.hot && !tz->state.passive
1117 && !tz->state.active)
1118 seq_puts(seq, "ok\n");
1120 if (tz->state.critical)
1121 seq_puts(seq, "critical ");
1123 seq_puts(seq, "hot ");
1124 if (tz->state.passive)
1125 seq_puts(seq, "passive ");
1126 if (tz->state.active)
1127 seq_printf(seq, "active[%d]", tz->state.active_index);
1128 seq_puts(seq, "\n");
1135 static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
1137 return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
1140 static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
1143 struct acpi_thermal *tz = seq->private;
1149 result = acpi_thermal_get_temperature(tz);
1153 seq_printf(seq, "temperature: %ld C\n",
1154 KELVIN_TO_CELSIUS(tz->temperature));
1160 static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
1162 return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
1165 static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
1167 struct acpi_thermal *tz = seq->private;
1168 struct acpi_device *device;
1178 if (tz->trips.critical.flags.valid)
1179 seq_printf(seq, "critical (S5): %ld C%s",
1180 KELVIN_TO_CELSIUS(tz->trips.critical.temperature),
1181 nocrt ? " <disabled>\n" : "\n");
1183 if (tz->trips.hot.flags.valid)
1184 seq_printf(seq, "hot (S4): %ld C%s",
1185 KELVIN_TO_CELSIUS(tz->trips.hot.temperature),
1186 nocrt ? " <disabled>\n" : "\n");
1188 if (tz->trips.passive.flags.valid) {
1190 "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
1191 KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
1192 tz->trips.passive.tc1, tz->trips.passive.tc2,
1193 tz->trips.passive.tsp);
1194 for (j = 0; j < tz->trips.passive.devices.count; j++) {
1195 status = acpi_bus_get_device(tz->trips.passive.devices.
1196 handles[j], &device);
1197 seq_printf(seq, "%4.4s ", status ? "" :
1198 acpi_device_bid(device));
1200 seq_puts(seq, "\n");
1203 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1204 if (!(tz->trips.active[i].flags.valid))
1206 seq_printf(seq, "active[%d]: %ld C: devices=",
1208 KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
1209 for (j = 0; j < tz->trips.active[i].devices.count; j++){
1210 status = acpi_bus_get_device(tz->trips.active[i].
1213 seq_printf(seq, "%4.4s ", status ? "" :
1214 acpi_device_bid(device));
1216 seq_puts(seq, "\n");
1223 static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
1225 return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
1228 static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
1230 struct acpi_thermal *tz = seq->private;
1236 if (!tz->flags.cooling_mode)
1237 seq_puts(seq, "<setting not supported>\n");
1239 seq_puts(seq, "0 - Active; 1 - Passive\n");
1245 static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
1247 return single_open(file, acpi_thermal_cooling_seq_show,
1252 acpi_thermal_write_cooling_mode(struct file *file,
1253 const char __user * buffer,
1254 size_t count, loff_t * ppos)
1256 struct seq_file *m = file->private_data;
1257 struct acpi_thermal *tz = m->private;
1259 char mode_string[12] = { '\0' };
1262 if (!tz || (count > sizeof(mode_string) - 1))
1265 if (!tz->flags.cooling_mode)
1268 if (copy_from_user(mode_string, buffer, count))
1271 mode_string[count] = '\0';
1273 result = acpi_thermal_set_cooling_mode(tz,
1274 simple_strtoul(mode_string, NULL,
1279 acpi_thermal_check(tz);
1284 static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
1286 struct acpi_thermal *tz = seq->private;
1292 if (!tz->polling_frequency) {
1293 seq_puts(seq, "<polling disabled>\n");
1297 seq_printf(seq, "polling frequency: %lu seconds\n",
1298 (tz->polling_frequency / 10));
1304 static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
1306 return single_open(file, acpi_thermal_polling_seq_show,
1311 acpi_thermal_write_polling(struct file *file,
1312 const char __user * buffer,
1313 size_t count, loff_t * ppos)
1315 struct seq_file *m = file->private_data;
1316 struct acpi_thermal *tz = m->private;
1318 char polling_string[12] = { '\0' };
1322 if (!tz || (count > sizeof(polling_string) - 1))
1325 if (copy_from_user(polling_string, buffer, count))
1328 polling_string[count] = '\0';
1330 seconds = simple_strtoul(polling_string, NULL, 0);
1332 result = acpi_thermal_set_polling(tz, seconds);
1336 acpi_thermal_check(tz);
1341 static int acpi_thermal_add_fs(struct acpi_device *device)
1343 struct proc_dir_entry *entry = NULL;
1346 if (!acpi_device_dir(device)) {
1347 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
1349 if (!acpi_device_dir(device))
1351 acpi_device_dir(device)->owner = THIS_MODULE;
1355 entry = create_proc_entry(ACPI_THERMAL_FILE_STATE,
1356 S_IRUGO, acpi_device_dir(device));
1360 entry->proc_fops = &acpi_thermal_state_fops;
1361 entry->data = acpi_driver_data(device);
1362 entry->owner = THIS_MODULE;
1365 /* 'temperature' [R] */
1366 entry = create_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1367 S_IRUGO, acpi_device_dir(device));
1371 entry->proc_fops = &acpi_thermal_temp_fops;
1372 entry->data = acpi_driver_data(device);
1373 entry->owner = THIS_MODULE;
1376 /* 'trip_points' [R] */
1377 entry = create_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1379 acpi_device_dir(device));
1383 entry->proc_fops = &acpi_thermal_trip_fops;
1384 entry->data = acpi_driver_data(device);
1385 entry->owner = THIS_MODULE;
1388 /* 'cooling_mode' [R/W] */
1389 entry = create_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1390 S_IFREG | S_IRUGO | S_IWUSR,
1391 acpi_device_dir(device));
1395 entry->proc_fops = &acpi_thermal_cooling_fops;
1396 entry->data = acpi_driver_data(device);
1397 entry->owner = THIS_MODULE;
1400 /* 'polling_frequency' [R/W] */
1401 entry = create_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1402 S_IFREG | S_IRUGO | S_IWUSR,
1403 acpi_device_dir(device));
1407 entry->proc_fops = &acpi_thermal_polling_fops;
1408 entry->data = acpi_driver_data(device);
1409 entry->owner = THIS_MODULE;
1415 static int acpi_thermal_remove_fs(struct acpi_device *device)
1418 if (acpi_device_dir(device)) {
1419 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1420 acpi_device_dir(device));
1421 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1422 acpi_device_dir(device));
1423 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1424 acpi_device_dir(device));
1425 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1426 acpi_device_dir(device));
1427 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1428 acpi_device_dir(device));
1429 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1430 acpi_device_dir(device) = NULL;
1436 /* --------------------------------------------------------------------------
1438 -------------------------------------------------------------------------- */
1440 static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
1442 struct acpi_thermal *tz = data;
1443 struct acpi_device *device = NULL;
1449 device = tz->device;
1452 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1453 acpi_thermal_check(tz);
1455 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
1456 acpi_thermal_get_trip_points(tz);
1457 acpi_thermal_check(tz);
1458 acpi_bus_generate_proc_event(device, event, 0);
1459 acpi_bus_generate_netlink_event(device->pnp.device_class,
1460 device->dev.bus_id, event, 0);
1462 case ACPI_THERMAL_NOTIFY_DEVICES:
1463 if (tz->flags.devices)
1464 acpi_thermal_get_devices(tz);
1465 acpi_bus_generate_proc_event(device, event, 0);
1466 acpi_bus_generate_netlink_event(device->pnp.device_class,
1467 device->dev.bus_id, event, 0);
1470 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1471 "Unsupported event [0x%x]\n", event));
1478 static int acpi_thermal_get_info(struct acpi_thermal *tz)
1486 /* Get temperature [_TMP] (required) */
1487 result = acpi_thermal_get_temperature(tz);
1491 /* Get trip points [_CRT, _PSV, etc.] (required) */
1492 result = acpi_thermal_get_trip_points(tz);
1496 /* Set the cooling mode [_SCP] to active cooling (default) */
1497 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
1499 tz->flags.cooling_mode = 1;
1501 /* Get default polling frequency [_TZP] (optional) */
1503 tz->polling_frequency = tzp;
1505 acpi_thermal_get_polling_frequency(tz);
1507 /* Get devices in this thermal zone [_TZD] (optional) */
1508 result = acpi_thermal_get_devices(tz);
1510 tz->flags.devices = 1;
1515 static int acpi_thermal_add(struct acpi_device *device)
1518 acpi_status status = AE_OK;
1519 struct acpi_thermal *tz = NULL;
1525 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
1529 tz->device = device;
1530 strcpy(tz->name, device->pnp.bus_id);
1531 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1532 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1533 acpi_driver_data(device) = tz;
1534 mutex_init(&tz->lock);
1537 result = acpi_thermal_get_info(tz);
1541 result = acpi_thermal_register_thermal_zone(tz);
1545 result = acpi_thermal_add_fs(device);
1547 goto unregister_thermal_zone;
1549 init_timer(&tz->timer);
1551 acpi_thermal_check(tz);
1553 status = acpi_install_notify_handler(device->handle,
1555 acpi_thermal_notify, tz);
1556 if (ACPI_FAILURE(status)) {
1561 printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
1562 acpi_device_name(device), acpi_device_bid(device),
1563 KELVIN_TO_CELSIUS(tz->temperature));
1567 acpi_thermal_remove_fs(device);
1568 unregister_thermal_zone:
1569 thermal_zone_device_unregister(tz->thermal_zone);
1576 static int acpi_thermal_remove(struct acpi_device *device, int type)
1578 acpi_status status = AE_OK;
1579 struct acpi_thermal *tz = NULL;
1582 if (!device || !acpi_driver_data(device))
1585 tz = acpi_driver_data(device);
1587 /* avoid timer adding new defer task */
1589 /* wait for running timer (on other CPUs) finish */
1590 del_timer_sync(&(tz->timer));
1591 /* synchronize deferred task */
1592 acpi_os_wait_events_complete(NULL);
1593 /* deferred task may reinsert timer */
1594 del_timer_sync(&(tz->timer));
1596 status = acpi_remove_notify_handler(device->handle,
1598 acpi_thermal_notify);
1600 /* Terminate policy */
1601 if (tz->trips.passive.flags.valid && tz->trips.passive.flags.enabled) {
1602 tz->trips.passive.flags.enabled = 0;
1603 acpi_thermal_passive(tz);
1605 if (tz->trips.active[0].flags.valid
1606 && tz->trips.active[0].flags.enabled) {
1607 tz->trips.active[0].flags.enabled = 0;
1608 acpi_thermal_active(tz);
1611 acpi_thermal_remove_fs(device);
1612 acpi_thermal_unregister_thermal_zone(tz);
1613 mutex_destroy(&tz->lock);
1618 static int acpi_thermal_resume(struct acpi_device *device)
1620 struct acpi_thermal *tz = NULL;
1621 int i, j, power_state, result;
1624 if (!device || !acpi_driver_data(device))
1627 tz = acpi_driver_data(device);
1629 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1630 if (!(&tz->trips.active[i]))
1632 if (!tz->trips.active[i].flags.valid)
1634 tz->trips.active[i].flags.enabled = 1;
1635 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1636 result = acpi_bus_get_power(tz->trips.active[i].devices.
1637 handles[j], &power_state);
1638 if (result || (power_state != ACPI_STATE_D0)) {
1639 tz->trips.active[i].flags.enabled = 0;
1643 tz->state.active |= tz->trips.active[i].flags.enabled;
1646 acpi_thermal_check(tz);
1652 static int thermal_act(const struct dmi_system_id *d) {
1655 printk(KERN_NOTICE "ACPI: %s detected: "
1656 "disabling all active thermal trip points\n", d->ident);
1661 static int thermal_nocrt(const struct dmi_system_id *d) {
1663 printk(KERN_NOTICE "ACPI: %s detected: "
1664 "disabling all critical thermal trip point actions.\n", d->ident);
1668 static int thermal_tzp(const struct dmi_system_id *d) {
1671 printk(KERN_NOTICE "ACPI: %s detected: "
1672 "enabling thermal zone polling\n", d->ident);
1673 tzp = 300; /* 300 dS = 30 Seconds */
1677 static int thermal_psv(const struct dmi_system_id *d) {
1680 printk(KERN_NOTICE "ACPI: %s detected: "
1681 "disabling all passive thermal trip points\n", d->ident);
1687 static struct dmi_system_id thermal_dmi_table[] __initdata = {
1689 * Award BIOS on this AOpen makes thermal control almost worthless.
1690 * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1693 .callback = thermal_act,
1694 .ident = "AOpen i915GMm-HFS",
1696 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1697 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1701 .callback = thermal_psv,
1702 .ident = "AOpen i915GMm-HFS",
1704 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1705 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1709 .callback = thermal_tzp,
1710 .ident = "AOpen i915GMm-HFS",
1712 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1713 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1717 .callback = thermal_nocrt,
1718 .ident = "Gigabyte GA-7ZX",
1720 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
1721 DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
1726 #endif /* CONFIG_DMI */
1728 static int __init acpi_thermal_init(void)
1732 dmi_check_system(thermal_dmi_table);
1735 printk(KERN_NOTICE "ACPI: thermal control disabled\n");
1738 acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1739 if (!acpi_thermal_dir)
1741 acpi_thermal_dir->owner = THIS_MODULE;
1743 result = acpi_bus_register_driver(&acpi_thermal_driver);
1745 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1752 static void __exit acpi_thermal_exit(void)
1755 acpi_bus_unregister_driver(&acpi_thermal_driver);
1757 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1762 module_init(acpi_thermal_init);
1763 module_exit(acpi_thermal_exit);