ACPI: don't duplicate input events on netlink
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / acpi / thermal.c
CommitLineData
1da177e4
LT
1/*
2 * acpi_thermal.c - ACPI Thermal Zone Driver ($Revision: 41 $)
3 *
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6 *
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
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.
13 *
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.
18 *
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.
22 *
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 *
25 * This driver fully implements the ACPI thermal policy as described in the
26 * ACPI 2.0 Specification.
27 *
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.
31 *
32 */
33
34#include <linux/kernel.h>
35#include <linux/module.h>
0b5bfa1c 36#include <linux/dmi.h>
1da177e4
LT
37#include <linux/init.h>
38#include <linux/types.h>
39#include <linux/proc_fs.h>
cd354f1a
TS
40#include <linux/timer.h>
41#include <linux/jiffies.h>
1da177e4
LT
42#include <linux/kmod.h>
43#include <linux/seq_file.h>
10a0a8d4 44#include <linux/reboot.h>
1da177e4
LT
45#include <asm/uaccess.h>
46
47#include <acpi/acpi_bus.h>
48#include <acpi/acpi_drivers.h>
49
50#define ACPI_THERMAL_COMPONENT 0x04000000
51#define ACPI_THERMAL_CLASS "thermal_zone"
1da177e4
LT
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
1da177e4
LT
64
65#define ACPI_THERMAL_MAX_ACTIVE 10
66#define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
67
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)
70
71#define _COMPONENT ACPI_THERMAL_COMPONENT
f52fd66d 72ACPI_MODULE_NAME("thermal");
1da177e4 73
1cbf4c56 74MODULE_AUTHOR("Paul Diefenbaugh");
7cda93e0 75MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
1da177e4
LT
76MODULE_LICENSE("GPL");
77
f8707ec9
LB
78static int act;
79module_param(act, int, 0644);
80MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.\n");
81
1da177e4 82static int tzp;
730ff34d 83module_param(tzp, int, 0444);
1da177e4
LT
84MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.\n");
85
f5487145
LB
86static int nocrt;
87module_param(nocrt, int, 0);
88MODULE_PARM_DESC(nocrt, "Set to disable action on ACPI thermal zone critical and hot trips.\n");
89
72b33ef8
LB
90static int off;
91module_param(off, int, 0);
92MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.\n");
93
a70cdc52
LB
94static int psv;
95module_param(psv, int, 0644);
96MODULE_PARM_DESC(psv, "Disable or override all passive trip points.\n");
97
4be44fcd
LB
98static int acpi_thermal_add(struct acpi_device *device);
99static int acpi_thermal_remove(struct acpi_device *device, int type);
5d9464a4 100static int acpi_thermal_resume(struct acpi_device *device);
1da177e4
LT
101static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
102static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
103static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
1da177e4 104static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
4be44fcd
LB
105static ssize_t acpi_thermal_write_cooling_mode(struct file *,
106 const char __user *, size_t,
107 loff_t *);
1da177e4 108static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
4be44fcd
LB
109static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
110 size_t, loff_t *);
1da177e4 111
1ba90e3a
TR
112static const struct acpi_device_id thermal_device_ids[] = {
113 {ACPI_THERMAL_HID, 0},
114 {"", 0},
115};
116MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
117
1da177e4 118static struct acpi_driver acpi_thermal_driver = {
c2b6705b 119 .name = "thermal",
4be44fcd 120 .class = ACPI_THERMAL_CLASS,
1ba90e3a 121 .ids = thermal_device_ids,
4be44fcd
LB
122 .ops = {
123 .add = acpi_thermal_add,
124 .remove = acpi_thermal_remove,
74ce1468 125 .resume = acpi_thermal_resume,
4be44fcd 126 },
1da177e4
LT
127};
128
129struct acpi_thermal_state {
4be44fcd
LB
130 u8 critical:1;
131 u8 hot:1;
132 u8 passive:1;
133 u8 active:1;
134 u8 reserved:4;
135 int active_index;
1da177e4
LT
136};
137
138struct acpi_thermal_state_flags {
4be44fcd
LB
139 u8 valid:1;
140 u8 enabled:1;
141 u8 reserved:6;
1da177e4
LT
142};
143
144struct acpi_thermal_critical {
145 struct acpi_thermal_state_flags flags;
4be44fcd 146 unsigned long temperature;
1da177e4
LT
147};
148
149struct acpi_thermal_hot {
150 struct acpi_thermal_state_flags flags;
4be44fcd 151 unsigned long temperature;
1da177e4
LT
152};
153
154struct acpi_thermal_passive {
155 struct acpi_thermal_state_flags flags;
4be44fcd
LB
156 unsigned long temperature;
157 unsigned long tc1;
158 unsigned long tc2;
159 unsigned long tsp;
160 struct acpi_handle_list devices;
1da177e4
LT
161};
162
163struct acpi_thermal_active {
164 struct acpi_thermal_state_flags flags;
4be44fcd
LB
165 unsigned long temperature;
166 struct acpi_handle_list devices;
1da177e4
LT
167};
168
169struct acpi_thermal_trips {
170 struct acpi_thermal_critical critical;
4be44fcd 171 struct acpi_thermal_hot hot;
1da177e4
LT
172 struct acpi_thermal_passive passive;
173 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
174};
175
176struct acpi_thermal_flags {
4be44fcd
LB
177 u8 cooling_mode:1; /* _SCP */
178 u8 devices:1; /* _TZD */
179 u8 reserved:6;
1da177e4
LT
180};
181
182struct acpi_thermal {
8348e1b1 183 struct acpi_device * device;
4be44fcd
LB
184 acpi_bus_id name;
185 unsigned long temperature;
186 unsigned long last_temperature;
187 unsigned long polling_frequency;
4be44fcd 188 volatile u8 zombie;
1da177e4
LT
189 struct acpi_thermal_flags flags;
190 struct acpi_thermal_state state;
191 struct acpi_thermal_trips trips;
4be44fcd
LB
192 struct acpi_handle_list devices;
193 struct timer_list timer;
1da177e4
LT
194};
195
d7508032 196static const struct file_operations acpi_thermal_state_fops = {
4be44fcd
LB
197 .open = acpi_thermal_state_open_fs,
198 .read = seq_read,
199 .llseek = seq_lseek,
200 .release = single_release,
1da177e4
LT
201};
202
d7508032 203static const struct file_operations acpi_thermal_temp_fops = {
4be44fcd
LB
204 .open = acpi_thermal_temp_open_fs,
205 .read = seq_read,
206 .llseek = seq_lseek,
207 .release = single_release,
1da177e4
LT
208};
209
d7508032 210static const struct file_operations acpi_thermal_trip_fops = {
4be44fcd
LB
211 .open = acpi_thermal_trip_open_fs,
212 .read = seq_read,
4be44fcd
LB
213 .llseek = seq_lseek,
214 .release = single_release,
1da177e4
LT
215};
216
d7508032 217static const struct file_operations acpi_thermal_cooling_fops = {
4be44fcd
LB
218 .open = acpi_thermal_cooling_open_fs,
219 .read = seq_read,
220 .write = acpi_thermal_write_cooling_mode,
221 .llseek = seq_lseek,
222 .release = single_release,
1da177e4
LT
223};
224
d7508032 225static const struct file_operations acpi_thermal_polling_fops = {
4be44fcd
LB
226 .open = acpi_thermal_polling_open_fs,
227 .read = seq_read,
228 .write = acpi_thermal_write_polling,
229 .llseek = seq_lseek,
230 .release = single_release,
1da177e4
LT
231};
232
233/* --------------------------------------------------------------------------
234 Thermal Zone Management
235 -------------------------------------------------------------------------- */
236
4be44fcd 237static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
1da177e4 238{
4be44fcd 239 acpi_status status = AE_OK;
1da177e4 240
1da177e4
LT
241
242 if (!tz)
d550d98d 243 return -EINVAL;
1da177e4
LT
244
245 tz->last_temperature = tz->temperature;
246
4be44fcd 247 status =
38ba7c9e 248 acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tz->temperature);
1da177e4 249 if (ACPI_FAILURE(status))
d550d98d 250 return -ENODEV;
1da177e4 251
4be44fcd
LB
252 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
253 tz->temperature));
1da177e4 254
d550d98d 255 return 0;
1da177e4
LT
256}
257
4be44fcd 258static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
1da177e4 259{
4be44fcd 260 acpi_status status = AE_OK;
1da177e4 261
1da177e4
LT
262
263 if (!tz)
d550d98d 264 return -EINVAL;
1da177e4 265
4be44fcd 266 status =
38ba7c9e 267 acpi_evaluate_integer(tz->device->handle, "_TZP", NULL,
4be44fcd 268 &tz->polling_frequency);
1da177e4 269 if (ACPI_FAILURE(status))
d550d98d 270 return -ENODEV;
1da177e4 271
4be44fcd
LB
272 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
273 tz->polling_frequency));
1da177e4 274
d550d98d 275 return 0;
1da177e4
LT
276}
277
4be44fcd 278static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
1da177e4 279{
1da177e4
LT
280
281 if (!tz)
d550d98d 282 return -EINVAL;
1da177e4
LT
283
284 tz->polling_frequency = seconds * 10; /* Convert value to deci-seconds */
285
4be44fcd
LB
286 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
287 "Polling frequency set to %lu seconds\n",
636cedf9 288 tz->polling_frequency/10));
1da177e4 289
d550d98d 290 return 0;
1da177e4
LT
291}
292
4be44fcd 293static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
1da177e4 294{
4be44fcd
LB
295 acpi_status status = AE_OK;
296 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
297 struct acpi_object_list arg_list = { 1, &arg0 };
298 acpi_handle handle = NULL;
1da177e4 299
1da177e4
LT
300
301 if (!tz)
d550d98d 302 return -EINVAL;
1da177e4 303
38ba7c9e 304 status = acpi_get_handle(tz->device->handle, "_SCP", &handle);
1da177e4
LT
305 if (ACPI_FAILURE(status)) {
306 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
d550d98d 307 return -ENODEV;
1da177e4
LT
308 }
309
310 arg0.integer.value = mode;
311
312 status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
313 if (ACPI_FAILURE(status))
d550d98d 314 return -ENODEV;
1da177e4 315
d550d98d 316 return 0;
1da177e4
LT
317}
318
4be44fcd 319static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
1da177e4 320{
4be44fcd
LB
321 acpi_status status = AE_OK;
322 int i = 0;
1da177e4 323
1da177e4
LT
324
325 if (!tz)
d550d98d 326 return -EINVAL;
1da177e4
LT
327
328 /* Critical Shutdown (required) */
329
38ba7c9e 330 status = acpi_evaluate_integer(tz->device->handle, "_CRT", NULL,
4be44fcd 331 &tz->trips.critical.temperature);
1da177e4
LT
332 if (ACPI_FAILURE(status)) {
333 tz->trips.critical.flags.valid = 0;
a6fc6720 334 ACPI_EXCEPTION((AE_INFO, status, "No critical threshold"));
d550d98d 335 return -ENODEV;
4be44fcd 336 } else {
1da177e4 337 tz->trips.critical.flags.valid = 1;
4be44fcd
LB
338 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
339 "Found critical threshold [%lu]\n",
340 tz->trips.critical.temperature));
1da177e4
LT
341 }
342
343 /* Critical Sleep (optional) */
344
4be44fcd 345 status =
38ba7c9e 346 acpi_evaluate_integer(tz->device->handle, "_HOT", NULL,
4be44fcd 347 &tz->trips.hot.temperature);
1da177e4
LT
348 if (ACPI_FAILURE(status)) {
349 tz->trips.hot.flags.valid = 0;
350 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No hot threshold\n"));
4be44fcd 351 } else {
1da177e4 352 tz->trips.hot.flags.valid = 1;
4be44fcd
LB
353 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found hot threshold [%lu]\n",
354 tz->trips.hot.temperature));
1da177e4
LT
355 }
356
357 /* Passive: Processors (optional) */
358
a70cdc52
LB
359 if (psv == -1) {
360 status = AE_SUPPORT;
361 } else if (psv > 0) {
362 tz->trips.passive.temperature = CELSIUS_TO_KELVIN(psv);
363 status = AE_OK;
364 } else {
365 status = acpi_evaluate_integer(tz->device->handle,
366 "_PSV", NULL, &tz->trips.passive.temperature);
367 }
368
1da177e4
LT
369 if (ACPI_FAILURE(status)) {
370 tz->trips.passive.flags.valid = 0;
371 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No passive threshold\n"));
4be44fcd 372 } else {
1da177e4
LT
373 tz->trips.passive.flags.valid = 1;
374
4be44fcd 375 status =
38ba7c9e 376 acpi_evaluate_integer(tz->device->handle, "_TC1", NULL,
4be44fcd 377 &tz->trips.passive.tc1);
1da177e4
LT
378 if (ACPI_FAILURE(status))
379 tz->trips.passive.flags.valid = 0;
380
4be44fcd 381 status =
38ba7c9e 382 acpi_evaluate_integer(tz->device->handle, "_TC2", NULL,
4be44fcd 383 &tz->trips.passive.tc2);
1da177e4
LT
384 if (ACPI_FAILURE(status))
385 tz->trips.passive.flags.valid = 0;
386
4be44fcd 387 status =
38ba7c9e 388 acpi_evaluate_integer(tz->device->handle, "_TSP", NULL,
4be44fcd 389 &tz->trips.passive.tsp);
1da177e4
LT
390 if (ACPI_FAILURE(status))
391 tz->trips.passive.flags.valid = 0;
392
4be44fcd 393 status =
38ba7c9e 394 acpi_evaluate_reference(tz->device->handle, "_PSL", NULL,
4be44fcd 395 &tz->trips.passive.devices);
1da177e4
LT
396 if (ACPI_FAILURE(status))
397 tz->trips.passive.flags.valid = 0;
398
399 if (!tz->trips.passive.flags.valid)
cece9296 400 printk(KERN_WARNING PREFIX "Invalid passive threshold\n");
1da177e4 401 else
4be44fcd
LB
402 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
403 "Found passive threshold [%lu]\n",
404 tz->trips.passive.temperature));
1da177e4
LT
405 }
406
407 /* Active: Fans, etc. (optional) */
408
4be44fcd 409 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1da177e4 410
4be44fcd 411 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
1da177e4 412
f8707ec9
LB
413 if (act == -1)
414 break; /* disable all active trip points */
415
416 status = acpi_evaluate_integer(tz->device->handle,
417 name, NULL, &tz->trips.active[i].temperature);
418
419 if (ACPI_FAILURE(status)) {
420 if (i == 0) /* no active trip points */
421 break;
422 if (act <= 0) /* no override requested */
423 break;
424 if (i == 1) { /* 1 trip point */
425 tz->trips.active[0].temperature =
426 CELSIUS_TO_KELVIN(act);
427 } else { /* multiple trips */
428 /*
429 * Don't allow override higher than
430 * the next higher trip point
431 */
432 tz->trips.active[i - 1].temperature =
433 (tz->trips.active[i - 2].temperature <
434 CELSIUS_TO_KELVIN(act) ?
435 tz->trips.active[i - 2].temperature :
436 CELSIUS_TO_KELVIN(act));
437 }
1da177e4 438 break;
f8707ec9 439 }
1da177e4
LT
440
441 name[2] = 'L';
4be44fcd 442 status =
38ba7c9e 443 acpi_evaluate_reference(tz->device->handle, name, NULL,
4be44fcd 444 &tz->trips.active[i].devices);
1da177e4
LT
445 if (ACPI_SUCCESS(status)) {
446 tz->trips.active[i].flags.valid = 1;
4be44fcd
LB
447 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
448 "Found active threshold [%d]:[%lu]\n",
449 i, tz->trips.active[i].temperature));
450 } else
a6fc6720
TR
451 ACPI_EXCEPTION((AE_INFO, status,
452 "Invalid active threshold [%d]", i));
1da177e4
LT
453 }
454
d550d98d 455 return 0;
1da177e4
LT
456}
457
4be44fcd 458static int acpi_thermal_get_devices(struct acpi_thermal *tz)
1da177e4 459{
4be44fcd 460 acpi_status status = AE_OK;
1da177e4 461
1da177e4
LT
462
463 if (!tz)
d550d98d 464 return -EINVAL;
1da177e4 465
4be44fcd 466 status =
38ba7c9e 467 acpi_evaluate_reference(tz->device->handle, "_TZD", NULL, &tz->devices);
1da177e4 468 if (ACPI_FAILURE(status))
d550d98d 469 return -ENODEV;
1da177e4 470
d550d98d 471 return 0;
1da177e4
LT
472}
473
4be44fcd 474static int acpi_thermal_critical(struct acpi_thermal *tz)
1da177e4 475{
f5487145 476 if (!tz || !tz->trips.critical.flags.valid || nocrt)
d550d98d 477 return -EINVAL;
1da177e4
LT
478
479 if (tz->temperature >= tz->trips.critical.temperature) {
cece9296 480 printk(KERN_WARNING PREFIX "Critical trip point\n");
1da177e4 481 tz->trips.critical.flags.enabled = 1;
4be44fcd 482 } else if (tz->trips.critical.flags.enabled)
1da177e4
LT
483 tz->trips.critical.flags.enabled = 0;
484
4be44fcd
LB
485 printk(KERN_EMERG
486 "Critical temperature reached (%ld C), shutting down.\n",
487 KELVIN_TO_CELSIUS(tz->temperature));
8348e1b1 488 acpi_bus_generate_event(tz->device, ACPI_THERMAL_NOTIFY_CRITICAL,
4be44fcd 489 tz->trips.critical.flags.enabled);
962ce8ca
ZR
490 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
491 tz->device->dev.bus_id,
492 ACPI_THERMAL_NOTIFY_CRITICAL,
493 tz->trips.critical.flags.enabled);
1da177e4 494
10a0a8d4 495 orderly_poweroff(true);
1da177e4 496
d550d98d 497 return 0;
1da177e4
LT
498}
499
4be44fcd 500static int acpi_thermal_hot(struct acpi_thermal *tz)
1da177e4 501{
f5487145 502 if (!tz || !tz->trips.hot.flags.valid || nocrt)
d550d98d 503 return -EINVAL;
1da177e4
LT
504
505 if (tz->temperature >= tz->trips.hot.temperature) {
cece9296 506 printk(KERN_WARNING PREFIX "Hot trip point\n");
1da177e4 507 tz->trips.hot.flags.enabled = 1;
4be44fcd 508 } else if (tz->trips.hot.flags.enabled)
1da177e4
LT
509 tz->trips.hot.flags.enabled = 0;
510
8348e1b1 511 acpi_bus_generate_event(tz->device, ACPI_THERMAL_NOTIFY_HOT,
4be44fcd 512 tz->trips.hot.flags.enabled);
962ce8ca
ZR
513 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
514 tz->device->dev.bus_id,
515 ACPI_THERMAL_NOTIFY_HOT,
516 tz->trips.hot.flags.enabled);
1da177e4
LT
517
518 /* TBD: Call user-mode "sleep(S4)" function */
519
d550d98d 520 return 0;
1da177e4
LT
521}
522
1cbf4c56 523static void acpi_thermal_passive(struct acpi_thermal *tz)
1da177e4 524{
1cbf4c56 525 int result = 1;
1da177e4 526 struct acpi_thermal_passive *passive = NULL;
4be44fcd
LB
527 int trend = 0;
528 int i = 0;
1da177e4 529
1da177e4
LT
530
531 if (!tz || !tz->trips.passive.flags.valid)
1cbf4c56 532 return;
1da177e4
LT
533
534 passive = &(tz->trips.passive);
535
536 /*
537 * Above Trip?
538 * -----------
539 * Calculate the thermal trend (using the passive cooling equation)
540 * and modify the performance limit for all passive cooling devices
541 * accordingly. Note that we assume symmetry.
542 */
543 if (tz->temperature >= passive->temperature) {
4be44fcd
LB
544 trend =
545 (passive->tc1 * (tz->temperature - tz->last_temperature)) +
546 (passive->tc2 * (tz->temperature - passive->temperature));
547 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
548 "trend[%d]=(tc1[%lu]*(tmp[%lu]-last[%lu]))+(tc2[%lu]*(tmp[%lu]-psv[%lu]))\n",
549 trend, passive->tc1, tz->temperature,
550 tz->last_temperature, passive->tc2,
551 tz->temperature, passive->temperature));
1cbf4c56 552 passive->flags.enabled = 1;
1da177e4
LT
553 /* Heating up? */
554 if (trend > 0)
4be44fcd
LB
555 for (i = 0; i < passive->devices.count; i++)
556 acpi_processor_set_thermal_limit(passive->
557 devices.
558 handles[i],
559 ACPI_PROCESSOR_LIMIT_INCREMENT);
1da177e4 560 /* Cooling off? */
1cbf4c56 561 else if (trend < 0) {
4be44fcd 562 for (i = 0; i < passive->devices.count; i++)
1cbf4c56
TR
563 /*
564 * assume that we are on highest
565 * freq/lowest thrott and can leave
566 * passive mode, even in error case
567 */
568 if (!acpi_processor_set_thermal_limit
569 (passive->devices.handles[i],
570 ACPI_PROCESSOR_LIMIT_DECREMENT))
571 result = 0;
572 /*
573 * Leave cooling mode, even if the temp might
574 * higher than trip point This is because some
575 * machines might have long thermal polling
576 * frequencies (tsp) defined. We will fall back
577 * into passive mode in next cycle (probably quicker)
578 */
579 if (result) {
580 passive->flags.enabled = 0;
581 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
582 "Disabling passive cooling, still above threshold,"
583 " but we are cooling down\n"));
584 }
585 }
586 return;
1da177e4
LT
587 }
588
589 /*
590 * Below Trip?
591 * -----------
592 * Implement passive cooling hysteresis to slowly increase performance
593 * and avoid thrashing around the passive trip point. Note that we
594 * assume symmetry.
595 */
1cbf4c56
TR
596 if (!passive->flags.enabled)
597 return;
598 for (i = 0; i < passive->devices.count; i++)
599 if (!acpi_processor_set_thermal_limit
600 (passive->devices.handles[i],
601 ACPI_PROCESSOR_LIMIT_DECREMENT))
602 result = 0;
603 if (result) {
604 passive->flags.enabled = 0;
605 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
606 "Disabling passive cooling (zone is cool)\n"));
1da177e4 607 }
1da177e4
LT
608}
609
1cbf4c56 610static void acpi_thermal_active(struct acpi_thermal *tz)
1da177e4 611{
4be44fcd 612 int result = 0;
1da177e4 613 struct acpi_thermal_active *active = NULL;
4be44fcd
LB
614 int i = 0;
615 int j = 0;
616 unsigned long maxtemp = 0;
1da177e4 617
1da177e4
LT
618
619 if (!tz)
1cbf4c56 620 return;
1da177e4 621
4be44fcd 622 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1da177e4
LT
623 active = &(tz->trips.active[i]);
624 if (!active || !active->flags.valid)
625 break;
1da177e4 626 if (tz->temperature >= active->temperature) {
1cbf4c56
TR
627 /*
628 * Above Threshold?
629 * ----------------
630 * If not already enabled, turn ON all cooling devices
631 * associated with this active threshold.
632 */
1da177e4 633 if (active->temperature > maxtemp)
1cbf4c56
TR
634 tz->state.active_index = i;
635 maxtemp = active->temperature;
636 if (active->flags.enabled)
637 continue;
1da177e4 638 for (j = 0; j < active->devices.count; j++) {
4be44fcd
LB
639 result =
640 acpi_bus_set_power(active->devices.
641 handles[j],
1cbf4c56 642 ACPI_STATE_D0);
1da177e4 643 if (result) {
cece9296
LB
644 printk(KERN_WARNING PREFIX
645 "Unable to turn cooling device [%p] 'on'\n",
a6fc6720 646 active->devices.
cece9296 647 handles[j]);
1da177e4
LT
648 continue;
649 }
1cbf4c56 650 active->flags.enabled = 1;
4be44fcd 651 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1cbf4c56 652 "Cooling device [%p] now 'on'\n",
4be44fcd 653 active->devices.handles[j]));
1da177e4 654 }
1cbf4c56
TR
655 continue;
656 }
657 if (!active->flags.enabled)
658 continue;
659 /*
660 * Below Threshold?
661 * ----------------
662 * Turn OFF all cooling devices associated with this
663 * threshold.
664 */
665 for (j = 0; j < active->devices.count; j++) {
666 result = acpi_bus_set_power(active->devices.handles[j],
667 ACPI_STATE_D3);
668 if (result) {
cece9296
LB
669 printk(KERN_WARNING PREFIX
670 "Unable to turn cooling device [%p] 'off'\n",
671 active->devices.handles[j]);
1cbf4c56
TR
672 continue;
673 }
674 active->flags.enabled = 0;
675 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
676 "Cooling device [%p] now 'off'\n",
677 active->devices.handles[j]));
1da177e4
LT
678 }
679 }
1da177e4
LT
680}
681
4be44fcd 682static void acpi_thermal_check(void *context);
1da177e4 683
4be44fcd 684static void acpi_thermal_run(unsigned long data)
1da177e4
LT
685{
686 struct acpi_thermal *tz = (struct acpi_thermal *)data;
687 if (!tz->zombie)
b8d35192 688 acpi_os_execute(OSL_GPE_HANDLER, acpi_thermal_check, (void *)data);
1da177e4
LT
689}
690
4be44fcd 691static void acpi_thermal_check(void *data)
1da177e4 692{
4be44fcd 693 int result = 0;
50dd0969 694 struct acpi_thermal *tz = data;
4be44fcd
LB
695 unsigned long sleep_time = 0;
696 int i = 0;
1da177e4
LT
697 struct acpi_thermal_state state;
698
1da177e4
LT
699
700 if (!tz) {
6468463a 701 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
d550d98d 702 return;
1da177e4
LT
703 }
704
705 state = tz->state;
706
707 result = acpi_thermal_get_temperature(tz);
708 if (result)
d550d98d 709 return;
4be44fcd 710
1da177e4 711 memset(&tz->state, 0, sizeof(tz->state));
4be44fcd 712
1da177e4
LT
713 /*
714 * Check Trip Points
715 * -----------------
716 * Compare the current temperature to the trip point values to see
717 * if we've entered one of the thermal policy states. Note that
718 * this function determines when a state is entered, but the
719 * individual policy decides when it is exited (e.g. hysteresis).
720 */
721 if (tz->trips.critical.flags.valid)
4be44fcd
LB
722 state.critical |=
723 (tz->temperature >= tz->trips.critical.temperature);
1da177e4
LT
724 if (tz->trips.hot.flags.valid)
725 state.hot |= (tz->temperature >= tz->trips.hot.temperature);
726 if (tz->trips.passive.flags.valid)
4be44fcd
LB
727 state.passive |=
728 (tz->temperature >= tz->trips.passive.temperature);
729 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
1da177e4 730 if (tz->trips.active[i].flags.valid)
4be44fcd
LB
731 state.active |=
732 (tz->temperature >=
733 tz->trips.active[i].temperature);
1da177e4
LT
734
735 /*
736 * Invoke Policy
737 * -------------
738 * Separated from the above check to allow individual policy to
739 * determine when to exit a given state.
740 */
741 if (state.critical)
742 acpi_thermal_critical(tz);
743 if (state.hot)
744 acpi_thermal_hot(tz);
745 if (state.passive)
746 acpi_thermal_passive(tz);
747 if (state.active)
748 acpi_thermal_active(tz);
749
750 /*
751 * Calculate State
752 * ---------------
753 * Again, separated from the above two to allow independent policy
754 * decisions.
755 */
1cbf4c56
TR
756 tz->state.critical = tz->trips.critical.flags.enabled;
757 tz->state.hot = tz->trips.hot.flags.enabled;
758 tz->state.passive = tz->trips.passive.flags.enabled;
759 tz->state.active = 0;
4be44fcd 760 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
1cbf4c56 761 tz->state.active |= tz->trips.active[i].flags.enabled;
1da177e4
LT
762
763 /*
764 * Calculate Sleep Time
765 * --------------------
766 * If we're in the passive state, use _TSP's value. Otherwise
767 * use the default polling frequency (e.g. _TZP). If no polling
768 * frequency is specified then we'll wait forever (at least until
769 * a thermal event occurs). Note that _TSP and _TZD values are
770 * given in 1/10th seconds (we must covert to milliseconds).
771 */
772 if (tz->state.passive)
773 sleep_time = tz->trips.passive.tsp * 100;
774 else if (tz->polling_frequency > 0)
775 sleep_time = tz->polling_frequency * 100;
776
4be44fcd
LB
777 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: temperature[%lu] sleep[%lu]\n",
778 tz->name, tz->temperature, sleep_time));
1da177e4
LT
779
780 /*
781 * Schedule Next Poll
782 * ------------------
783 */
784 if (!sleep_time) {
785 if (timer_pending(&(tz->timer)))
786 del_timer(&(tz->timer));
4be44fcd 787 } else {
1da177e4 788 if (timer_pending(&(tz->timer)))
94e22e13
AM
789 mod_timer(&(tz->timer),
790 jiffies + (HZ * sleep_time) / 1000);
1da177e4 791 else {
4be44fcd 792 tz->timer.data = (unsigned long)tz;
1da177e4
LT
793 tz->timer.function = acpi_thermal_run;
794 tz->timer.expires = jiffies + (HZ * sleep_time) / 1000;
795 add_timer(&(tz->timer));
796 }
797 }
798
d550d98d 799 return;
1da177e4
LT
800}
801
1da177e4
LT
802/* --------------------------------------------------------------------------
803 FS Interface (/proc)
804 -------------------------------------------------------------------------- */
805
4be44fcd 806static struct proc_dir_entry *acpi_thermal_dir;
1da177e4
LT
807
808static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
809{
50dd0969 810 struct acpi_thermal *tz = seq->private;
1da177e4 811
1da177e4
LT
812
813 if (!tz)
814 goto end;
815
816 seq_puts(seq, "state: ");
817
4be44fcd
LB
818 if (!tz->state.critical && !tz->state.hot && !tz->state.passive
819 && !tz->state.active)
1da177e4
LT
820 seq_puts(seq, "ok\n");
821 else {
822 if (tz->state.critical)
823 seq_puts(seq, "critical ");
824 if (tz->state.hot)
825 seq_puts(seq, "hot ");
826 if (tz->state.passive)
827 seq_puts(seq, "passive ");
828 if (tz->state.active)
829 seq_printf(seq, "active[%d]", tz->state.active_index);
830 seq_puts(seq, "\n");
831 }
832
4be44fcd 833 end:
d550d98d 834 return 0;
1da177e4
LT
835}
836
837static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
838{
839 return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
840}
841
1da177e4
LT
842static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
843{
4be44fcd 844 int result = 0;
50dd0969 845 struct acpi_thermal *tz = seq->private;
1da177e4 846
1da177e4
LT
847
848 if (!tz)
849 goto end;
850
851 result = acpi_thermal_get_temperature(tz);
852 if (result)
853 goto end;
854
4be44fcd
LB
855 seq_printf(seq, "temperature: %ld C\n",
856 KELVIN_TO_CELSIUS(tz->temperature));
1da177e4 857
4be44fcd 858 end:
d550d98d 859 return 0;
1da177e4
LT
860}
861
862static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
863{
864 return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
865}
866
1da177e4
LT
867static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
868{
50dd0969 869 struct acpi_thermal *tz = seq->private;
68ccfaa8 870 struct acpi_device *device;
e7c746ef
TR
871 acpi_status status;
872
4be44fcd
LB
873 int i = 0;
874 int j = 0;
1da177e4 875
1da177e4
LT
876
877 if (!tz)
878 goto end;
879
880 if (tz->trips.critical.flags.valid)
f5487145
LB
881 seq_printf(seq, "critical (S5): %ld C%s",
882 KELVIN_TO_CELSIUS(tz->trips.critical.temperature),
883 nocrt ? " <disabled>\n" : "\n");
1da177e4
LT
884
885 if (tz->trips.hot.flags.valid)
f5487145
LB
886 seq_printf(seq, "hot (S4): %ld C%s",
887 KELVIN_TO_CELSIUS(tz->trips.hot.temperature),
888 nocrt ? " <disabled>\n" : "\n");
1da177e4
LT
889
890 if (tz->trips.passive.flags.valid) {
4be44fcd
LB
891 seq_printf(seq,
892 "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
893 KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
894 tz->trips.passive.tc1, tz->trips.passive.tc2,
895 tz->trips.passive.tsp);
896 for (j = 0; j < tz->trips.passive.devices.count; j++) {
e7c746ef
TR
897 status = acpi_bus_get_device(tz->trips.passive.devices.
898 handles[j], &device);
899 seq_printf(seq, "%4.4s ", status ? "" :
900 acpi_device_bid(device));
1da177e4
LT
901 }
902 seq_puts(seq, "\n");
903 }
904
905 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
906 if (!(tz->trips.active[i].flags.valid))
907 break;
908 seq_printf(seq, "active[%d]: %ld C: devices=",
4be44fcd
LB
909 i,
910 KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
68ccfaa8 911 for (j = 0; j < tz->trips.active[i].devices.count; j++){
e7c746ef
TR
912 status = acpi_bus_get_device(tz->trips.active[i].
913 devices.handles[j],
914 &device);
915 seq_printf(seq, "%4.4s ", status ? "" :
916 acpi_device_bid(device));
68ccfaa8 917 }
1da177e4
LT
918 seq_puts(seq, "\n");
919 }
920
4be44fcd 921 end:
d550d98d 922 return 0;
1da177e4
LT
923}
924
925static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
926{
927 return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
928}
929
1da177e4
LT
930static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
931{
50dd0969 932 struct acpi_thermal *tz = seq->private;
1da177e4 933
1da177e4
LT
934
935 if (!tz)
936 goto end;
937
eaca2d3f 938 if (!tz->flags.cooling_mode)
1da177e4 939 seq_puts(seq, "<setting not supported>\n");
1da177e4 940 else
eaca2d3f 941 seq_puts(seq, "0 - Active; 1 - Passive\n");
1da177e4 942
4be44fcd 943 end:
d550d98d 944 return 0;
1da177e4
LT
945}
946
947static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
948{
949 return single_open(file, acpi_thermal_cooling_seq_show,
4be44fcd 950 PDE(inode)->data);
1da177e4
LT
951}
952
953static ssize_t
4be44fcd
LB
954acpi_thermal_write_cooling_mode(struct file *file,
955 const char __user * buffer,
956 size_t count, loff_t * ppos)
1da177e4 957{
50dd0969
JE
958 struct seq_file *m = file->private_data;
959 struct acpi_thermal *tz = m->private;
4be44fcd
LB
960 int result = 0;
961 char mode_string[12] = { '\0' };
1da177e4 962
1da177e4
LT
963
964 if (!tz || (count > sizeof(mode_string) - 1))
d550d98d 965 return -EINVAL;
1da177e4
LT
966
967 if (!tz->flags.cooling_mode)
d550d98d 968 return -ENODEV;
1da177e4
LT
969
970 if (copy_from_user(mode_string, buffer, count))
d550d98d 971 return -EFAULT;
4be44fcd 972
1da177e4 973 mode_string[count] = '\0';
4be44fcd
LB
974
975 result = acpi_thermal_set_cooling_mode(tz,
976 simple_strtoul(mode_string, NULL,
977 0));
1da177e4 978 if (result)
d550d98d 979 return result;
1da177e4
LT
980
981 acpi_thermal_check(tz);
982
d550d98d 983 return count;
1da177e4
LT
984}
985
1da177e4
LT
986static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
987{
50dd0969 988 struct acpi_thermal *tz = seq->private;
1da177e4 989
1da177e4
LT
990
991 if (!tz)
992 goto end;
993
994 if (!tz->polling_frequency) {
995 seq_puts(seq, "<polling disabled>\n");
996 goto end;
997 }
998
999 seq_printf(seq, "polling frequency: %lu seconds\n",
4be44fcd 1000 (tz->polling_frequency / 10));
1da177e4 1001
4be44fcd 1002 end:
d550d98d 1003 return 0;
1da177e4
LT
1004}
1005
1006static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
1007{
1008 return single_open(file, acpi_thermal_polling_seq_show,
4be44fcd 1009 PDE(inode)->data);
1da177e4
LT
1010}
1011
1012static ssize_t
4be44fcd
LB
1013acpi_thermal_write_polling(struct file *file,
1014 const char __user * buffer,
1015 size_t count, loff_t * ppos)
1da177e4 1016{
50dd0969
JE
1017 struct seq_file *m = file->private_data;
1018 struct acpi_thermal *tz = m->private;
4be44fcd
LB
1019 int result = 0;
1020 char polling_string[12] = { '\0' };
1021 int seconds = 0;
1da177e4 1022
1da177e4
LT
1023
1024 if (!tz || (count > sizeof(polling_string) - 1))
d550d98d 1025 return -EINVAL;
4be44fcd 1026
1da177e4 1027 if (copy_from_user(polling_string, buffer, count))
d550d98d 1028 return -EFAULT;
4be44fcd 1029
1da177e4
LT
1030 polling_string[count] = '\0';
1031
1032 seconds = simple_strtoul(polling_string, NULL, 0);
4be44fcd 1033
1da177e4
LT
1034 result = acpi_thermal_set_polling(tz, seconds);
1035 if (result)
d550d98d 1036 return result;
1da177e4
LT
1037
1038 acpi_thermal_check(tz);
1039
d550d98d 1040 return count;
1da177e4
LT
1041}
1042
4be44fcd 1043static int acpi_thermal_add_fs(struct acpi_device *device)
1da177e4 1044{
4be44fcd 1045 struct proc_dir_entry *entry = NULL;
1da177e4 1046
1da177e4
LT
1047
1048 if (!acpi_device_dir(device)) {
1049 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
4be44fcd 1050 acpi_thermal_dir);
1da177e4 1051 if (!acpi_device_dir(device))
d550d98d 1052 return -ENODEV;
1da177e4
LT
1053 acpi_device_dir(device)->owner = THIS_MODULE;
1054 }
1055
1056 /* 'state' [R] */
1057 entry = create_proc_entry(ACPI_THERMAL_FILE_STATE,
4be44fcd 1058 S_IRUGO, acpi_device_dir(device));
1da177e4 1059 if (!entry)
d550d98d 1060 return -ENODEV;
1da177e4
LT
1061 else {
1062 entry->proc_fops = &acpi_thermal_state_fops;
1063 entry->data = acpi_driver_data(device);
1064 entry->owner = THIS_MODULE;
1065 }
1066
1067 /* 'temperature' [R] */
1068 entry = create_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
4be44fcd 1069 S_IRUGO, acpi_device_dir(device));
1da177e4 1070 if (!entry)
d550d98d 1071 return -ENODEV;
1da177e4
LT
1072 else {
1073 entry->proc_fops = &acpi_thermal_temp_fops;
1074 entry->data = acpi_driver_data(device);
1075 entry->owner = THIS_MODULE;
1076 }
1077
1078 /* 'trip_points' [R/W] */
1079 entry = create_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
4be44fcd
LB
1080 S_IFREG | S_IRUGO | S_IWUSR,
1081 acpi_device_dir(device));
1da177e4 1082 if (!entry)
d550d98d 1083 return -ENODEV;
1da177e4
LT
1084 else {
1085 entry->proc_fops = &acpi_thermal_trip_fops;
1086 entry->data = acpi_driver_data(device);
1087 entry->owner = THIS_MODULE;
1088 }
1089
1090 /* 'cooling_mode' [R/W] */
1091 entry = create_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
4be44fcd
LB
1092 S_IFREG | S_IRUGO | S_IWUSR,
1093 acpi_device_dir(device));
1da177e4 1094 if (!entry)
d550d98d 1095 return -ENODEV;
1da177e4
LT
1096 else {
1097 entry->proc_fops = &acpi_thermal_cooling_fops;
1098 entry->data = acpi_driver_data(device);
1099 entry->owner = THIS_MODULE;
1100 }
1101
1102 /* 'polling_frequency' [R/W] */
1103 entry = create_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
4be44fcd
LB
1104 S_IFREG | S_IRUGO | S_IWUSR,
1105 acpi_device_dir(device));
1da177e4 1106 if (!entry)
d550d98d 1107 return -ENODEV;
1da177e4
LT
1108 else {
1109 entry->proc_fops = &acpi_thermal_polling_fops;
1110 entry->data = acpi_driver_data(device);
1111 entry->owner = THIS_MODULE;
1112 }
1113
d550d98d 1114 return 0;
1da177e4
LT
1115}
1116
4be44fcd 1117static int acpi_thermal_remove_fs(struct acpi_device *device)
1da177e4 1118{
1da177e4
LT
1119
1120 if (acpi_device_dir(device)) {
1121 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1122 acpi_device_dir(device));
1123 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1124 acpi_device_dir(device));
1125 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1126 acpi_device_dir(device));
1127 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1128 acpi_device_dir(device));
1129 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1130 acpi_device_dir(device));
1131 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1132 acpi_device_dir(device) = NULL;
1133 }
1134
d550d98d 1135 return 0;
1da177e4
LT
1136}
1137
1da177e4
LT
1138/* --------------------------------------------------------------------------
1139 Driver Interface
1140 -------------------------------------------------------------------------- */
1141
4be44fcd 1142static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
1da177e4 1143{
50dd0969 1144 struct acpi_thermal *tz = data;
4be44fcd 1145 struct acpi_device *device = NULL;
1da177e4 1146
1da177e4
LT
1147
1148 if (!tz)
d550d98d 1149 return;
1da177e4 1150
8348e1b1 1151 device = tz->device;
1da177e4
LT
1152
1153 switch (event) {
1154 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1155 acpi_thermal_check(tz);
1156 break;
1157 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
1158 acpi_thermal_get_trip_points(tz);
1159 acpi_thermal_check(tz);
1160 acpi_bus_generate_event(device, event, 0);
962ce8ca
ZR
1161 acpi_bus_generate_netlink_event(device->pnp.device_class,
1162 device->dev.bus_id, event, 0);
1da177e4
LT
1163 break;
1164 case ACPI_THERMAL_NOTIFY_DEVICES:
1165 if (tz->flags.devices)
1166 acpi_thermal_get_devices(tz);
1167 acpi_bus_generate_event(device, event, 0);
962ce8ca
ZR
1168 acpi_bus_generate_netlink_event(device->pnp.device_class,
1169 device->dev.bus_id, event, 0);
1da177e4
LT
1170 break;
1171 default:
1172 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd 1173 "Unsupported event [0x%x]\n", event));
1da177e4
LT
1174 break;
1175 }
1176
d550d98d 1177 return;
1da177e4
LT
1178}
1179
4be44fcd 1180static int acpi_thermal_get_info(struct acpi_thermal *tz)
1da177e4 1181{
4be44fcd 1182 int result = 0;
1da177e4 1183
1da177e4
LT
1184
1185 if (!tz)
d550d98d 1186 return -EINVAL;
1da177e4
LT
1187
1188 /* Get temperature [_TMP] (required) */
1189 result = acpi_thermal_get_temperature(tz);
1190 if (result)
d550d98d 1191 return result;
1da177e4
LT
1192
1193 /* Get trip points [_CRT, _PSV, etc.] (required) */
1194 result = acpi_thermal_get_trip_points(tz);
1195 if (result)
d550d98d 1196 return result;
1da177e4
LT
1197
1198 /* Set the cooling mode [_SCP] to active cooling (default) */
1199 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
4be44fcd 1200 if (!result)
1da177e4 1201 tz->flags.cooling_mode = 1;
1da177e4
LT
1202
1203 /* Get default polling frequency [_TZP] (optional) */
1204 if (tzp)
1205 tz->polling_frequency = tzp;
1206 else
1207 acpi_thermal_get_polling_frequency(tz);
1208
1209 /* Get devices in this thermal zone [_TZD] (optional) */
1210 result = acpi_thermal_get_devices(tz);
1211 if (!result)
1212 tz->flags.devices = 1;
1213
d550d98d 1214 return 0;
1da177e4
LT
1215}
1216
4be44fcd 1217static int acpi_thermal_add(struct acpi_device *device)
1da177e4 1218{
4be44fcd
LB
1219 int result = 0;
1220 acpi_status status = AE_OK;
1221 struct acpi_thermal *tz = NULL;
1da177e4 1222
1da177e4
LT
1223
1224 if (!device)
d550d98d 1225 return -EINVAL;
1da177e4 1226
36bcbec7 1227 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
1da177e4 1228 if (!tz)
d550d98d 1229 return -ENOMEM;
1da177e4 1230
8348e1b1 1231 tz->device = device;
1da177e4
LT
1232 strcpy(tz->name, device->pnp.bus_id);
1233 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1234 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1235 acpi_driver_data(device) = tz;
1236
1237 result = acpi_thermal_get_info(tz);
1238 if (result)
1239 goto end;
1240
1241 result = acpi_thermal_add_fs(device);
1242 if (result)
09047e75 1243 goto end;
1da177e4
LT
1244
1245 init_timer(&tz->timer);
1246
1247 acpi_thermal_check(tz);
1248
38ba7c9e 1249 status = acpi_install_notify_handler(device->handle,
4be44fcd
LB
1250 ACPI_DEVICE_NOTIFY,
1251 acpi_thermal_notify, tz);
1da177e4 1252 if (ACPI_FAILURE(status)) {
1da177e4
LT
1253 result = -ENODEV;
1254 goto end;
1255 }
1256
1257 printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
4be44fcd
LB
1258 acpi_device_name(device), acpi_device_bid(device),
1259 KELVIN_TO_CELSIUS(tz->temperature));
1da177e4 1260
4be44fcd 1261 end:
1da177e4
LT
1262 if (result) {
1263 acpi_thermal_remove_fs(device);
1264 kfree(tz);
1265 }
1266
d550d98d 1267 return result;
1da177e4
LT
1268}
1269
4be44fcd 1270static int acpi_thermal_remove(struct acpi_device *device, int type)
1da177e4 1271{
4be44fcd
LB
1272 acpi_status status = AE_OK;
1273 struct acpi_thermal *tz = NULL;
1da177e4 1274
1da177e4
LT
1275
1276 if (!device || !acpi_driver_data(device))
d550d98d 1277 return -EINVAL;
1da177e4 1278
50dd0969 1279 tz = acpi_driver_data(device);
1da177e4
LT
1280
1281 /* avoid timer adding new defer task */
1282 tz->zombie = 1;
1283 /* wait for running timer (on other CPUs) finish */
1284 del_timer_sync(&(tz->timer));
1285 /* synchronize deferred task */
1286 acpi_os_wait_events_complete(NULL);
1287 /* deferred task may reinsert timer */
1288 del_timer_sync(&(tz->timer));
1289
38ba7c9e 1290 status = acpi_remove_notify_handler(device->handle,
4be44fcd
LB
1291 ACPI_DEVICE_NOTIFY,
1292 acpi_thermal_notify);
1da177e4
LT
1293
1294 /* Terminate policy */
4be44fcd 1295 if (tz->trips.passive.flags.valid && tz->trips.passive.flags.enabled) {
1da177e4
LT
1296 tz->trips.passive.flags.enabled = 0;
1297 acpi_thermal_passive(tz);
1298 }
1299 if (tz->trips.active[0].flags.valid
4be44fcd 1300 && tz->trips.active[0].flags.enabled) {
1da177e4
LT
1301 tz->trips.active[0].flags.enabled = 0;
1302 acpi_thermal_active(tz);
1303 }
1304
1305 acpi_thermal_remove_fs(device);
1306
1307 kfree(tz);
d550d98d 1308 return 0;
1da177e4
LT
1309}
1310
5d9464a4 1311static int acpi_thermal_resume(struct acpi_device *device)
74ce1468
KK
1312{
1313 struct acpi_thermal *tz = NULL;
b1028c54
KK
1314 int i, j, power_state, result;
1315
74ce1468
KK
1316
1317 if (!device || !acpi_driver_data(device))
d550d98d 1318 return -EINVAL;
74ce1468 1319
50dd0969 1320 tz = acpi_driver_data(device);
74ce1468 1321
bed936f7 1322 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
b1028c54
KK
1323 if (!(&tz->trips.active[i]))
1324 break;
1325 if (!tz->trips.active[i].flags.valid)
1326 break;
1327 tz->trips.active[i].flags.enabled = 1;
1328 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1329 result = acpi_bus_get_power(tz->trips.active[i].devices.
1330 handles[j], &power_state);
1331 if (result || (power_state != ACPI_STATE_D0)) {
1332 tz->trips.active[i].flags.enabled = 0;
1333 break;
1334 }
bed936f7 1335 }
b1028c54 1336 tz->state.active |= tz->trips.active[i].flags.enabled;
bed936f7
KK
1337 }
1338
b1028c54 1339 acpi_thermal_check(tz);
74ce1468
KK
1340
1341 return AE_OK;
1342}
1343
0b5bfa1c
LB
1344#ifdef CONFIG_DMI
1345static int thermal_act(struct dmi_system_id *d) {
1346
1347 if (act == 0) {
1348 printk(KERN_NOTICE "ACPI: %s detected: "
1349 "disabling all active thermal trip points\n", d->ident);
1350 act = -1;
1351 }
1352 return 0;
1353}
1354static int thermal_tzp(struct dmi_system_id *d) {
1355
1356 if (tzp == 0) {
1357 printk(KERN_NOTICE "ACPI: %s detected: "
1358 "enabling thermal zone polling\n", d->ident);
1359 tzp = 300; /* 300 dS = 30 Seconds */
1360 }
1361 return 0;
1362}
1363static int thermal_psv(struct dmi_system_id *d) {
1364
1365 if (psv == 0) {
1366 printk(KERN_NOTICE "ACPI: %s detected: "
1367 "disabling all passive thermal trip points\n", d->ident);
1368 psv = -1;
1369 }
1370 return 0;
1371}
1372
1373static struct dmi_system_id thermal_dmi_table[] __initdata = {
1374 /*
1375 * Award BIOS on this AOpen makes thermal control almost worthless.
1376 * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1377 */
1378 {
1379 .callback = thermal_act,
1380 .ident = "AOpen i915GMm-HFS",
1381 .matches = {
1382 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1383 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1384 },
1385 },
1386 {
1387 .callback = thermal_psv,
1388 .ident = "AOpen i915GMm-HFS",
1389 .matches = {
1390 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1391 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1392 },
1393 },
1394 {
1395 .callback = thermal_tzp,
1396 .ident = "AOpen i915GMm-HFS",
1397 .matches = {
1398 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1399 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1400 },
1401 },
1402 {}
1403};
1404#endif /* CONFIG_DMI */
1405
4be44fcd 1406static int __init acpi_thermal_init(void)
1da177e4 1407{
4be44fcd 1408 int result = 0;
1da177e4 1409
0b5bfa1c
LB
1410 dmi_check_system(thermal_dmi_table);
1411
72b33ef8
LB
1412 if (off) {
1413 printk(KERN_NOTICE "ACPI: thermal control disabled\n");
1414 return -ENODEV;
1415 }
1da177e4
LT
1416 acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1417 if (!acpi_thermal_dir)
d550d98d 1418 return -ENODEV;
1da177e4
LT
1419 acpi_thermal_dir->owner = THIS_MODULE;
1420
1421 result = acpi_bus_register_driver(&acpi_thermal_driver);
1422 if (result < 0) {
1423 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
d550d98d 1424 return -ENODEV;
1da177e4
LT
1425 }
1426
d550d98d 1427 return 0;
1da177e4
LT
1428}
1429
4be44fcd 1430static void __exit acpi_thermal_exit(void)
1da177e4 1431{
1da177e4
LT
1432
1433 acpi_bus_unregister_driver(&acpi_thermal_driver);
1434
1435 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1436
d550d98d 1437 return;
1da177e4
LT
1438}
1439
1da177e4
LT
1440module_init(acpi_thermal_init);
1441module_exit(acpi_thermal_exit);