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