]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/acpi/thermal.c
Merge branch 'rc-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuil...
[net-next-2.6.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 37#include <linux/init.h>
5a0e3ad6 38#include <linux/slab.h>
1da177e4 39#include <linux/types.h>
43d9f87b
ZR
40
41#ifdef CONFIG_ACPI_PROCFS
1da177e4 42#include <linux/proc_fs.h>
43d9f87b
ZR
43#include <linux/seq_file.h>
44#endif
45
cd354f1a 46#include <linux/jiffies.h>
1da177e4 47#include <linux/kmod.h>
10a0a8d4 48#include <linux/reboot.h>
f6f5c45e 49#include <linux/device.h>
1da177e4 50#include <asm/uaccess.h>
3f655ef8 51#include <linux/thermal.h>
1da177e4
LT
52#include <acpi/acpi_bus.h>
53#include <acpi/acpi_drivers.h>
54
a192a958
LB
55#define PREFIX "ACPI: "
56
1da177e4 57#define ACPI_THERMAL_CLASS "thermal_zone"
1da177e4
LT
58#define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
59#define ACPI_THERMAL_FILE_STATE "state"
60#define ACPI_THERMAL_FILE_TEMPERATURE "temperature"
61#define ACPI_THERMAL_FILE_TRIP_POINTS "trip_points"
62#define ACPI_THERMAL_FILE_COOLING_MODE "cooling_mode"
63#define ACPI_THERMAL_FILE_POLLING_FREQ "polling_frequency"
64#define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
65#define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
66#define ACPI_THERMAL_NOTIFY_DEVICES 0x82
67#define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
68#define ACPI_THERMAL_NOTIFY_HOT 0xF1
69#define ACPI_THERMAL_MODE_ACTIVE 0x00
1da177e4
LT
70
71#define ACPI_THERMAL_MAX_ACTIVE 10
72#define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
73
1da177e4 74#define _COMPONENT ACPI_THERMAL_COMPONENT
f52fd66d 75ACPI_MODULE_NAME("thermal");
1da177e4 76
1cbf4c56 77MODULE_AUTHOR("Paul Diefenbaugh");
7cda93e0 78MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
1da177e4
LT
79MODULE_LICENSE("GPL");
80
f8707ec9
LB
81static int act;
82module_param(act, int, 0644);
3c1d36da 83MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.");
f8707ec9 84
c52a7419
LB
85static int crt;
86module_param(crt, int, 0644);
87MODULE_PARM_DESC(crt, "Disable or lower all critical trip points.");
88
1da177e4 89static int tzp;
730ff34d 90module_param(tzp, int, 0444);
3c1d36da 91MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.");
1da177e4 92
f5487145
LB
93static int nocrt;
94module_param(nocrt, int, 0);
8c99fdce 95MODULE_PARM_DESC(nocrt, "Set to take no action upon ACPI thermal zone critical trips points.");
f5487145 96
72b33ef8
LB
97static int off;
98module_param(off, int, 0);
3c1d36da 99MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
72b33ef8 100
a70cdc52
LB
101static int psv;
102module_param(psv, int, 0644);
3c1d36da 103MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
a70cdc52 104
4be44fcd
LB
105static int acpi_thermal_add(struct acpi_device *device);
106static int acpi_thermal_remove(struct acpi_device *device, int type);
5d9464a4 107static int acpi_thermal_resume(struct acpi_device *device);
342d550d 108static void acpi_thermal_notify(struct acpi_device *device, u32 event);
1da177e4 109
1ba90e3a
TR
110static const struct acpi_device_id thermal_device_ids[] = {
111 {ACPI_THERMAL_HID, 0},
112 {"", 0},
113};
114MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
115
1da177e4 116static struct acpi_driver acpi_thermal_driver = {
c2b6705b 117 .name = "thermal",
4be44fcd 118 .class = ACPI_THERMAL_CLASS,
1ba90e3a 119 .ids = thermal_device_ids,
4be44fcd
LB
120 .ops = {
121 .add = acpi_thermal_add,
122 .remove = acpi_thermal_remove,
74ce1468 123 .resume = acpi_thermal_resume,
342d550d 124 .notify = acpi_thermal_notify,
4be44fcd 125 },
1da177e4
LT
126};
127
128struct acpi_thermal_state {
4be44fcd
LB
129 u8 critical:1;
130 u8 hot:1;
131 u8 passive:1;
132 u8 active:1;
133 u8 reserved:4;
134 int active_index;
1da177e4
LT
135};
136
137struct acpi_thermal_state_flags {
4be44fcd
LB
138 u8 valid:1;
139 u8 enabled:1;
140 u8 reserved:6;
1da177e4
LT
141};
142
143struct acpi_thermal_critical {
144 struct acpi_thermal_state_flags flags;
4be44fcd 145 unsigned long temperature;
1da177e4
LT
146};
147
148struct acpi_thermal_hot {
149 struct acpi_thermal_state_flags flags;
4be44fcd 150 unsigned long temperature;
1da177e4
LT
151};
152
153struct acpi_thermal_passive {
154 struct acpi_thermal_state_flags flags;
4be44fcd
LB
155 unsigned long temperature;
156 unsigned long tc1;
157 unsigned long tc2;
158 unsigned long tsp;
159 struct acpi_handle_list devices;
1da177e4
LT
160};
161
162struct acpi_thermal_active {
163 struct acpi_thermal_state_flags flags;
4be44fcd
LB
164 unsigned long temperature;
165 struct acpi_handle_list devices;
1da177e4
LT
166};
167
168struct acpi_thermal_trips {
169 struct acpi_thermal_critical critical;
4be44fcd 170 struct acpi_thermal_hot hot;
1da177e4
LT
171 struct acpi_thermal_passive passive;
172 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
173};
174
175struct acpi_thermal_flags {
4be44fcd
LB
176 u8 cooling_mode:1; /* _SCP */
177 u8 devices:1; /* _TZD */
178 u8 reserved:6;
1da177e4
LT
179};
180
181struct acpi_thermal {
8348e1b1 182 struct acpi_device * device;
4be44fcd
LB
183 acpi_bus_id name;
184 unsigned long temperature;
185 unsigned long last_temperature;
186 unsigned long polling_frequency;
4be44fcd 187 volatile u8 zombie;
1da177e4
LT
188 struct acpi_thermal_flags flags;
189 struct acpi_thermal_state state;
190 struct acpi_thermal_trips trips;
4be44fcd 191 struct acpi_handle_list devices;
3f655ef8
ZR
192 struct thermal_zone_device *thermal_zone;
193 int tz_enabled;
13614e37 194 int kelvin_offset;
6e215785 195 struct mutex lock;
1da177e4
LT
196};
197
43d9f87b
ZR
198#ifdef CONFIG_ACPI_PROCFS
199static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
200static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
201static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
202static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
203static ssize_t acpi_thermal_write_cooling_mode(struct file *,
204 const char __user *, size_t,
205 loff_t *);
206static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
207static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
208 size_t, loff_t *);
209
d7508032 210static const struct file_operations acpi_thermal_state_fops = {
cf7acfab 211 .owner = THIS_MODULE,
4be44fcd
LB
212 .open = acpi_thermal_state_open_fs,
213 .read = seq_read,
214 .llseek = seq_lseek,
215 .release = single_release,
1da177e4
LT
216};
217
d7508032 218static const struct file_operations acpi_thermal_temp_fops = {
cf7acfab 219 .owner = THIS_MODULE,
4be44fcd
LB
220 .open = acpi_thermal_temp_open_fs,
221 .read = seq_read,
222 .llseek = seq_lseek,
223 .release = single_release,
1da177e4
LT
224};
225
d7508032 226static const struct file_operations acpi_thermal_trip_fops = {
cf7acfab 227 .owner = THIS_MODULE,
4be44fcd
LB
228 .open = acpi_thermal_trip_open_fs,
229 .read = seq_read,
4be44fcd
LB
230 .llseek = seq_lseek,
231 .release = single_release,
1da177e4
LT
232};
233
d7508032 234static const struct file_operations acpi_thermal_cooling_fops = {
cf7acfab 235 .owner = THIS_MODULE,
4be44fcd
LB
236 .open = acpi_thermal_cooling_open_fs,
237 .read = seq_read,
238 .write = acpi_thermal_write_cooling_mode,
239 .llseek = seq_lseek,
240 .release = single_release,
1da177e4
LT
241};
242
d7508032 243static const struct file_operations acpi_thermal_polling_fops = {
cf7acfab 244 .owner = THIS_MODULE,
4be44fcd
LB
245 .open = acpi_thermal_polling_open_fs,
246 .read = seq_read,
247 .write = acpi_thermal_write_polling,
248 .llseek = seq_lseek,
249 .release = single_release,
1da177e4 250};
43d9f87b 251#endif /* CONFIG_ACPI_PROCFS*/
1da177e4
LT
252
253/* --------------------------------------------------------------------------
254 Thermal Zone Management
255 -------------------------------------------------------------------------- */
256
4be44fcd 257static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
1da177e4 258{
4be44fcd 259 acpi_status status = AE_OK;
27663c58 260 unsigned long long tmp;
1da177e4
LT
261
262 if (!tz)
d550d98d 263 return -EINVAL;
1da177e4
LT
264
265 tz->last_temperature = tz->temperature;
266
27663c58 267 status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp);
1da177e4 268 if (ACPI_FAILURE(status))
d550d98d 269 return -ENODEV;
1da177e4 270
27663c58 271 tz->temperature = tmp;
4be44fcd
LB
272 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
273 tz->temperature));
1da177e4 274
d550d98d 275 return 0;
1da177e4
LT
276}
277
4be44fcd 278static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
1da177e4 279{
4be44fcd 280 acpi_status status = AE_OK;
27663c58 281 unsigned long long tmp;
1da177e4
LT
282
283 if (!tz)
d550d98d 284 return -EINVAL;
1da177e4 285
27663c58 286 status = acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, &tmp);
1da177e4 287 if (ACPI_FAILURE(status))
d550d98d 288 return -ENODEV;
1da177e4 289
27663c58 290 tz->polling_frequency = tmp;
4be44fcd
LB
291 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
292 tz->polling_frequency));
1da177e4 293
d550d98d 294 return 0;
1da177e4
LT
295}
296
4be44fcd 297static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
1da177e4 298{
4be44fcd
LB
299 acpi_status status = AE_OK;
300 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
301 struct acpi_object_list arg_list = { 1, &arg0 };
302 acpi_handle handle = NULL;
1da177e4 303
1da177e4
LT
304
305 if (!tz)
d550d98d 306 return -EINVAL;
1da177e4 307
38ba7c9e 308 status = acpi_get_handle(tz->device->handle, "_SCP", &handle);
1da177e4
LT
309 if (ACPI_FAILURE(status)) {
310 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
d550d98d 311 return -ENODEV;
1da177e4
LT
312 }
313
314 arg0.integer.value = mode;
315
316 status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
317 if (ACPI_FAILURE(status))
d550d98d 318 return -ENODEV;
1da177e4 319
d550d98d 320 return 0;
1da177e4
LT
321}
322
ce44e197
ZR
323#define ACPI_TRIPS_CRITICAL 0x01
324#define ACPI_TRIPS_HOT 0x02
325#define ACPI_TRIPS_PASSIVE 0x04
326#define ACPI_TRIPS_ACTIVE 0x08
327#define ACPI_TRIPS_DEVICES 0x10
1da177e4 328
ce44e197
ZR
329#define ACPI_TRIPS_REFRESH_THRESHOLDS (ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE)
330#define ACPI_TRIPS_REFRESH_DEVICES ACPI_TRIPS_DEVICES
1da177e4 331
ce44e197
ZR
332#define ACPI_TRIPS_INIT (ACPI_TRIPS_CRITICAL | ACPI_TRIPS_HOT | \
333 ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE | \
334 ACPI_TRIPS_DEVICES)
1da177e4 335
ce44e197
ZR
336/*
337 * This exception is thrown out in two cases:
338 * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
339 * when re-evaluating the AML code.
340 * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change.
341 * We need to re-bind the cooling devices of a thermal zone when this occurs.
342 */
343#define ACPI_THERMAL_TRIPS_EXCEPTION(flags, str) \
344do { \
345 if (flags != ACPI_TRIPS_INIT) \
346 ACPI_EXCEPTION((AE_INFO, AE_ERROR, \
347 "ACPI thermal trip point %s changed\n" \
348 "Please send acpidump to linux-acpi@vger.kernel.org\n", str)); \
349} while (0)
350
351static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
352{
353 acpi_status status = AE_OK;
27663c58 354 unsigned long long tmp;
ce44e197
ZR
355 struct acpi_handle_list devices;
356 int valid = 0;
357 int i;
1da177e4 358
fa809452 359 /* Critical Shutdown */
ce44e197
ZR
360 if (flag & ACPI_TRIPS_CRITICAL) {
361 status = acpi_evaluate_integer(tz->device->handle,
27663c58
MW
362 "_CRT", NULL, &tmp);
363 tz->trips.critical.temperature = tmp;
a39a2d7c
AV
364 /*
365 * Treat freezing temperatures as invalid as well; some
366 * BIOSes return really low values and cause reboots at startup.
b731d7b6 367 * Below zero (Celsius) values clearly aren't right for sure..
a39a2d7c
AV
368 * ... so lets discard those as invalid.
369 */
fa809452
TR
370 if (ACPI_FAILURE(status)) {
371 tz->trips.critical.flags.valid = 0;
372 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
373 "No critical threshold\n"));
374 } else if (tmp <= 2732) {
375 printk(KERN_WARNING FW_BUG "Invalid critical threshold "
376 "(%llu)\n", tmp);
c52a7419 377 tz->trips.critical.flags.valid = 0;
ce44e197
ZR
378 } else {
379 tz->trips.critical.flags.valid = 1;
380 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
fa809452
TR
381 "Found critical threshold [%lu]\n",
382 tz->trips.critical.temperature));
ce44e197
ZR
383 }
384 if (tz->trips.critical.flags.valid == 1) {
385 if (crt == -1) {
386 tz->trips.critical.flags.valid = 0;
387 } else if (crt > 0) {
388 unsigned long crt_k = CELSIUS_TO_KELVIN(crt);
389 /*
22a94d79 390 * Allow override critical threshold
ce44e197 391 */
22a94d79
ZR
392 if (crt_k > tz->trips.critical.temperature)
393 printk(KERN_WARNING PREFIX
394 "Critical threshold %d C\n", crt);
395 tz->trips.critical.temperature = crt_k;
ce44e197 396 }
c52a7419
LB
397 }
398 }
399
1da177e4 400 /* Critical Sleep (optional) */
ce44e197 401 if (flag & ACPI_TRIPS_HOT) {
a70cdc52 402 status = acpi_evaluate_integer(tz->device->handle,
27663c58 403 "_HOT", NULL, &tmp);
ce44e197
ZR
404 if (ACPI_FAILURE(status)) {
405 tz->trips.hot.flags.valid = 0;
406 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
407 "No hot threshold\n"));
408 } else {
27663c58 409 tz->trips.hot.temperature = tmp;
ce44e197
ZR
410 tz->trips.hot.flags.valid = 1;
411 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
412 "Found hot threshold [%lu]\n",
413 tz->trips.critical.temperature));
414 }
a70cdc52
LB
415 }
416
ce44e197 417 /* Passive (optional) */
0e4240d9
ZR
418 if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.flags.valid) ||
419 (flag == ACPI_TRIPS_INIT)) {
ce44e197
ZR
420 valid = tz->trips.passive.flags.valid;
421 if (psv == -1) {
422 status = AE_SUPPORT;
423 } else if (psv > 0) {
27663c58 424 tmp = CELSIUS_TO_KELVIN(psv);
ce44e197
ZR
425 status = AE_OK;
426 } else {
427 status = acpi_evaluate_integer(tz->device->handle,
27663c58 428 "_PSV", NULL, &tmp);
ce44e197 429 }
1da177e4 430
1da177e4
LT
431 if (ACPI_FAILURE(status))
432 tz->trips.passive.flags.valid = 0;
ce44e197 433 else {
27663c58 434 tz->trips.passive.temperature = tmp;
ce44e197
ZR
435 tz->trips.passive.flags.valid = 1;
436 if (flag == ACPI_TRIPS_INIT) {
437 status = acpi_evaluate_integer(
438 tz->device->handle, "_TC1",
27663c58 439 NULL, &tmp);
ce44e197
ZR
440 if (ACPI_FAILURE(status))
441 tz->trips.passive.flags.valid = 0;
27663c58
MW
442 else
443 tz->trips.passive.tc1 = tmp;
ce44e197
ZR
444 status = acpi_evaluate_integer(
445 tz->device->handle, "_TC2",
27663c58 446 NULL, &tmp);
ce44e197
ZR
447 if (ACPI_FAILURE(status))
448 tz->trips.passive.flags.valid = 0;
27663c58
MW
449 else
450 tz->trips.passive.tc2 = tmp;
ce44e197
ZR
451 status = acpi_evaluate_integer(
452 tz->device->handle, "_TSP",
27663c58 453 NULL, &tmp);
ce44e197
ZR
454 if (ACPI_FAILURE(status))
455 tz->trips.passive.flags.valid = 0;
27663c58
MW
456 else
457 tz->trips.passive.tsp = tmp;
ce44e197
ZR
458 }
459 }
460 }
461 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.flags.valid) {
462 memset(&devices, 0, sizeof(struct acpi_handle_list));
463 status = acpi_evaluate_reference(tz->device->handle, "_PSL",
464 NULL, &devices);
0e4240d9
ZR
465 if (ACPI_FAILURE(status)) {
466 printk(KERN_WARNING PREFIX
467 "Invalid passive threshold\n");
1da177e4 468 tz->trips.passive.flags.valid = 0;
0e4240d9 469 }
1da177e4 470 else
ce44e197 471 tz->trips.passive.flags.valid = 1;
1da177e4 472
ce44e197
ZR
473 if (memcmp(&tz->trips.passive.devices, &devices,
474 sizeof(struct acpi_handle_list))) {
475 memcpy(&tz->trips.passive.devices, &devices,
476 sizeof(struct acpi_handle_list));
477 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
478 }
479 }
480 if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
481 if (valid != tz->trips.passive.flags.valid)
482 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
483 }
1da177e4 484
ce44e197 485 /* Active (optional) */
4be44fcd 486 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
4be44fcd 487 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
ce44e197 488 valid = tz->trips.active[i].flags.valid;
1da177e4 489
f8707ec9 490 if (act == -1)
ce44e197
ZR
491 break; /* disable all active trip points */
492
0e4240d9
ZR
493 if ((flag == ACPI_TRIPS_INIT) || ((flag & ACPI_TRIPS_ACTIVE) &&
494 tz->trips.active[i].flags.valid)) {
ce44e197 495 status = acpi_evaluate_integer(tz->device->handle,
27663c58 496 name, NULL, &tmp);
ce44e197
ZR
497 if (ACPI_FAILURE(status)) {
498 tz->trips.active[i].flags.valid = 0;
499 if (i == 0)
500 break;
501 if (act <= 0)
502 break;
503 if (i == 1)
504 tz->trips.active[0].temperature =
505 CELSIUS_TO_KELVIN(act);
506 else
507 /*
508 * Don't allow override higher than
509 * the next higher trip point
510 */
511 tz->trips.active[i - 1].temperature =
512 (tz->trips.active[i - 2].temperature <
513 CELSIUS_TO_KELVIN(act) ?
514 tz->trips.active[i - 2].temperature :
515 CELSIUS_TO_KELVIN(act));
f8707ec9 516 break;
27663c58
MW
517 } else {
518 tz->trips.active[i].temperature = tmp;
ce44e197 519 tz->trips.active[i].flags.valid = 1;
27663c58 520 }
f8707ec9 521 }
1da177e4
LT
522
523 name[2] = 'L';
ce44e197
ZR
524 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].flags.valid ) {
525 memset(&devices, 0, sizeof(struct acpi_handle_list));
526 status = acpi_evaluate_reference(tz->device->handle,
527 name, NULL, &devices);
0e4240d9
ZR
528 if (ACPI_FAILURE(status)) {
529 printk(KERN_WARNING PREFIX
530 "Invalid active%d threshold\n", i);
ce44e197 531 tz->trips.active[i].flags.valid = 0;
0e4240d9 532 }
ce44e197
ZR
533 else
534 tz->trips.active[i].flags.valid = 1;
535
536 if (memcmp(&tz->trips.active[i].devices, &devices,
537 sizeof(struct acpi_handle_list))) {
538 memcpy(&tz->trips.active[i].devices, &devices,
539 sizeof(struct acpi_handle_list));
540 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
541 }
542 }
543 if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
544 if (valid != tz->trips.active[i].flags.valid)
545 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
546
547 if (!tz->trips.active[i].flags.valid)
548 break;
549 }
550
551 if (flag & ACPI_TRIPS_DEVICES) {
552 memset(&devices, 0, sizeof(struct acpi_handle_list));
553 status = acpi_evaluate_reference(tz->device->handle, "_TZD",
554 NULL, &devices);
555 if (memcmp(&tz->devices, &devices,
556 sizeof(struct acpi_handle_list))) {
557 memcpy(&tz->devices, &devices,
558 sizeof(struct acpi_handle_list));
559 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
560 }
1da177e4
LT
561 }
562
d550d98d 563 return 0;
1da177e4
LT
564}
565
ce44e197 566static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
1da177e4 567{
8b7ef6d8
TR
568 int i, valid, ret = acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
569
570 if (ret)
571 return ret;
572
573 valid = tz->trips.critical.flags.valid |
574 tz->trips.hot.flags.valid |
575 tz->trips.passive.flags.valid;
576
577 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
578 valid |= tz->trips.active[i].flags.valid;
579
580 if (!valid) {
581 printk(KERN_WARNING FW_BUG "No valid trip found\n");
582 return -ENODEV;
583 }
584 return 0;
1da177e4
LT
585}
586
4be44fcd 587static void acpi_thermal_check(void *data)
1da177e4 588{
50dd0969 589 struct acpi_thermal *tz = data;
1da177e4 590
b1569e99 591 thermal_zone_device_update(tz->thermal_zone);
1da177e4
LT
592}
593
3f655ef8 594/* sys I/F for generic thermal sysfs support */
13614e37 595#define KELVIN_TO_MILLICELSIUS(t, off) (((t) - (off)) * 100)
5e012760 596
6503e5df
MG
597static int thermal_get_temp(struct thermal_zone_device *thermal,
598 unsigned long *temp)
3f655ef8
ZR
599{
600 struct acpi_thermal *tz = thermal->devdata;
76ecb4f2 601 int result;
3f655ef8
ZR
602
603 if (!tz)
604 return -EINVAL;
605
76ecb4f2
ZR
606 result = acpi_thermal_get_temperature(tz);
607 if (result)
608 return result;
609
13614e37 610 *temp = KELVIN_TO_MILLICELSIUS(tz->temperature, tz->kelvin_offset);
6503e5df 611 return 0;
3f655ef8
ZR
612}
613
614static const char enabled[] = "kernel";
615static const char disabled[] = "user";
616static int thermal_get_mode(struct thermal_zone_device *thermal,
6503e5df 617 enum thermal_device_mode *mode)
3f655ef8
ZR
618{
619 struct acpi_thermal *tz = thermal->devdata;
620
621 if (!tz)
622 return -EINVAL;
623
6503e5df
MG
624 *mode = tz->tz_enabled ? THERMAL_DEVICE_ENABLED :
625 THERMAL_DEVICE_DISABLED;
626
627 return 0;
3f655ef8
ZR
628}
629
630static int thermal_set_mode(struct thermal_zone_device *thermal,
6503e5df 631 enum thermal_device_mode mode)
3f655ef8
ZR
632{
633 struct acpi_thermal *tz = thermal->devdata;
634 int enable;
635
636 if (!tz)
637 return -EINVAL;
638
639 /*
640 * enable/disable thermal management from ACPI thermal driver
641 */
6503e5df 642 if (mode == THERMAL_DEVICE_ENABLED)
3f655ef8 643 enable = 1;
6503e5df 644 else if (mode == THERMAL_DEVICE_DISABLED)
3f655ef8
ZR
645 enable = 0;
646 else
647 return -EINVAL;
648
649 if (enable != tz->tz_enabled) {
650 tz->tz_enabled = enable;
651 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
652 "%s ACPI thermal control\n",
653 tz->tz_enabled ? enabled : disabled));
654 acpi_thermal_check(tz);
655 }
656 return 0;
657}
658
659static int thermal_get_trip_type(struct thermal_zone_device *thermal,
6503e5df 660 int trip, enum thermal_trip_type *type)
3f655ef8
ZR
661{
662 struct acpi_thermal *tz = thermal->devdata;
663 int i;
664
665 if (!tz || trip < 0)
666 return -EINVAL;
667
668 if (tz->trips.critical.flags.valid) {
6503e5df
MG
669 if (!trip) {
670 *type = THERMAL_TRIP_CRITICAL;
671 return 0;
672 }
3f655ef8
ZR
673 trip--;
674 }
675
676 if (tz->trips.hot.flags.valid) {
6503e5df
MG
677 if (!trip) {
678 *type = THERMAL_TRIP_HOT;
679 return 0;
680 }
3f655ef8
ZR
681 trip--;
682 }
683
684 if (tz->trips.passive.flags.valid) {
6503e5df
MG
685 if (!trip) {
686 *type = THERMAL_TRIP_PASSIVE;
687 return 0;
688 }
3f655ef8
ZR
689 trip--;
690 }
691
692 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
693 tz->trips.active[i].flags.valid; i++) {
6503e5df
MG
694 if (!trip) {
695 *type = THERMAL_TRIP_ACTIVE;
696 return 0;
697 }
3f655ef8
ZR
698 trip--;
699 }
700
701 return -EINVAL;
702}
703
704static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
6503e5df 705 int trip, unsigned long *temp)
3f655ef8
ZR
706{
707 struct acpi_thermal *tz = thermal->devdata;
708 int i;
709
710 if (!tz || trip < 0)
711 return -EINVAL;
712
713 if (tz->trips.critical.flags.valid) {
6503e5df
MG
714 if (!trip) {
715 *temp = KELVIN_TO_MILLICELSIUS(
13614e37
JD
716 tz->trips.critical.temperature,
717 tz->kelvin_offset);
6503e5df
MG
718 return 0;
719 }
3f655ef8
ZR
720 trip--;
721 }
722
723 if (tz->trips.hot.flags.valid) {
6503e5df
MG
724 if (!trip) {
725 *temp = KELVIN_TO_MILLICELSIUS(
13614e37
JD
726 tz->trips.hot.temperature,
727 tz->kelvin_offset);
6503e5df
MG
728 return 0;
729 }
3f655ef8
ZR
730 trip--;
731 }
732
733 if (tz->trips.passive.flags.valid) {
6503e5df
MG
734 if (!trip) {
735 *temp = KELVIN_TO_MILLICELSIUS(
13614e37
JD
736 tz->trips.passive.temperature,
737 tz->kelvin_offset);
6503e5df
MG
738 return 0;
739 }
3f655ef8
ZR
740 trip--;
741 }
742
743 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
744 tz->trips.active[i].flags.valid; i++) {
6503e5df
MG
745 if (!trip) {
746 *temp = KELVIN_TO_MILLICELSIUS(
13614e37
JD
747 tz->trips.active[i].temperature,
748 tz->kelvin_offset);
6503e5df
MG
749 return 0;
750 }
3f655ef8
ZR
751 trip--;
752 }
753
754 return -EINVAL;
755}
756
9ec732ff
ZR
757static int thermal_get_crit_temp(struct thermal_zone_device *thermal,
758 unsigned long *temperature) {
759 struct acpi_thermal *tz = thermal->devdata;
760
761 if (tz->trips.critical.flags.valid) {
762 *temperature = KELVIN_TO_MILLICELSIUS(
13614e37
JD
763 tz->trips.critical.temperature,
764 tz->kelvin_offset);
9ec732ff
ZR
765 return 0;
766 } else
767 return -EINVAL;
768}
769
b1569e99
MG
770static int thermal_notify(struct thermal_zone_device *thermal, int trip,
771 enum thermal_trip_type trip_type)
772{
773 u8 type = 0;
774 struct acpi_thermal *tz = thermal->devdata;
775
776 if (trip_type == THERMAL_TRIP_CRITICAL)
777 type = ACPI_THERMAL_NOTIFY_CRITICAL;
778 else if (trip_type == THERMAL_TRIP_HOT)
779 type = ACPI_THERMAL_NOTIFY_HOT;
780 else
781 return 0;
782
783 acpi_bus_generate_proc_event(tz->device, type, 1);
784 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
f6f5c45e 785 dev_name(&tz->device->dev), type, 1);
b1569e99
MG
786
787 if (trip_type == THERMAL_TRIP_CRITICAL && nocrt)
788 return 1;
789
790 return 0;
791}
792
3f655ef8
ZR
793typedef int (*cb)(struct thermal_zone_device *, int,
794 struct thermal_cooling_device *);
795static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
796 struct thermal_cooling_device *cdev,
797 cb action)
798{
799 struct acpi_device *device = cdev->devdata;
800 struct acpi_thermal *tz = thermal->devdata;
653a00c9
ZR
801 struct acpi_device *dev;
802 acpi_status status;
803 acpi_handle handle;
3f655ef8
ZR
804 int i;
805 int j;
806 int trip = -1;
807 int result = 0;
808
809 if (tz->trips.critical.flags.valid)
810 trip++;
811
812 if (tz->trips.hot.flags.valid)
813 trip++;
814
815 if (tz->trips.passive.flags.valid) {
816 trip++;
817 for (i = 0; i < tz->trips.passive.devices.count;
818 i++) {
653a00c9
ZR
819 handle = tz->trips.passive.devices.handles[i];
820 status = acpi_bus_get_device(handle, &dev);
821 if (ACPI_SUCCESS(status) && (dev == device)) {
822 result = action(thermal, trip, cdev);
823 if (result)
824 goto failed;
825 }
3f655ef8
ZR
826 }
827 }
828
829 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
830 if (!tz->trips.active[i].flags.valid)
831 break;
832 trip++;
833 for (j = 0;
834 j < tz->trips.active[i].devices.count;
835 j++) {
653a00c9
ZR
836 handle = tz->trips.active[i].devices.handles[j];
837 status = acpi_bus_get_device(handle, &dev);
838 if (ACPI_SUCCESS(status) && (dev == device)) {
839 result = action(thermal, trip, cdev);
840 if (result)
841 goto failed;
842 }
3f655ef8
ZR
843 }
844 }
845
846 for (i = 0; i < tz->devices.count; i++) {
653a00c9
ZR
847 handle = tz->devices.handles[i];
848 status = acpi_bus_get_device(handle, &dev);
849 if (ACPI_SUCCESS(status) && (dev == device)) {
850 result = action(thermal, -1, cdev);
851 if (result)
852 goto failed;
853 }
3f655ef8
ZR
854 }
855
856failed:
857 return result;
858}
859
860static int
861acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
862 struct thermal_cooling_device *cdev)
863{
864 return acpi_thermal_cooling_device_cb(thermal, cdev,
865 thermal_zone_bind_cooling_device);
866}
867
868static int
869acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
870 struct thermal_cooling_device *cdev)
871{
872 return acpi_thermal_cooling_device_cb(thermal, cdev,
873 thermal_zone_unbind_cooling_device);
874}
875
876static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
877 .bind = acpi_thermal_bind_cooling_device,
878 .unbind = acpi_thermal_unbind_cooling_device,
879 .get_temp = thermal_get_temp,
880 .get_mode = thermal_get_mode,
881 .set_mode = thermal_set_mode,
882 .get_trip_type = thermal_get_trip_type,
883 .get_trip_temp = thermal_get_trip_temp,
9ec732ff 884 .get_crit_temp = thermal_get_crit_temp,
b1569e99 885 .notify = thermal_notify,
3f655ef8
ZR
886};
887
888static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
889{
890 int trips = 0;
891 int result;
20733939 892 acpi_status status;
3f655ef8
ZR
893 int i;
894
895 if (tz->trips.critical.flags.valid)
896 trips++;
897
898 if (tz->trips.hot.flags.valid)
899 trips++;
900
901 if (tz->trips.passive.flags.valid)
902 trips++;
903
904 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
905 tz->trips.active[i].flags.valid; i++, trips++);
b1569e99
MG
906
907 if (tz->trips.passive.flags.valid)
908 tz->thermal_zone =
909 thermal_zone_device_register("acpitz", trips, tz,
910 &acpi_thermal_zone_ops,
911 tz->trips.passive.tc1,
912 tz->trips.passive.tc2,
913 tz->trips.passive.tsp*100,
914 tz->polling_frequency*100);
915 else
916 tz->thermal_zone =
917 thermal_zone_device_register("acpitz", trips, tz,
918 &acpi_thermal_zone_ops,
919 0, 0, 0,
67405439 920 tz->polling_frequency*100);
bb070e43 921 if (IS_ERR(tz->thermal_zone))
3f655ef8
ZR
922 return -ENODEV;
923
924 result = sysfs_create_link(&tz->device->dev.kobj,
925 &tz->thermal_zone->device.kobj, "thermal_zone");
926 if (result)
927 return result;
928
929 result = sysfs_create_link(&tz->thermal_zone->device.kobj,
930 &tz->device->dev.kobj, "device");
931 if (result)
932 return result;
933
20733939
ZR
934 status = acpi_attach_data(tz->device->handle,
935 acpi_bus_private_data_handler,
936 tz->thermal_zone);
937 if (ACPI_FAILURE(status)) {
55ac9a01
LM
938 printk(KERN_ERR PREFIX
939 "Error attaching device data\n");
20733939
ZR
940 return -ENODEV;
941 }
942
3f655ef8
ZR
943 tz->tz_enabled = 1;
944
fc3a8828
GKH
945 dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
946 tz->thermal_zone->id);
3f655ef8
ZR
947 return 0;
948}
949
950static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
951{
952 sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
953 sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
954 thermal_zone_device_unregister(tz->thermal_zone);
955 tz->thermal_zone = NULL;
20733939 956 acpi_detach_data(tz->device->handle, acpi_bus_private_data_handler);
3f655ef8
ZR
957}
958
959
1da177e4
LT
960/* --------------------------------------------------------------------------
961 FS Interface (/proc)
962 -------------------------------------------------------------------------- */
43d9f87b 963#ifdef CONFIG_ACPI_PROCFS
4be44fcd 964static struct proc_dir_entry *acpi_thermal_dir;
1da177e4
LT
965
966static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
967{
50dd0969 968 struct acpi_thermal *tz = seq->private;
1da177e4 969
1da177e4
LT
970
971 if (!tz)
972 goto end;
973
974 seq_puts(seq, "state: ");
975
4be44fcd
LB
976 if (!tz->state.critical && !tz->state.hot && !tz->state.passive
977 && !tz->state.active)
1da177e4
LT
978 seq_puts(seq, "ok\n");
979 else {
980 if (tz->state.critical)
981 seq_puts(seq, "critical ");
982 if (tz->state.hot)
983 seq_puts(seq, "hot ");
984 if (tz->state.passive)
985 seq_puts(seq, "passive ");
986 if (tz->state.active)
987 seq_printf(seq, "active[%d]", tz->state.active_index);
988 seq_puts(seq, "\n");
989 }
990
4be44fcd 991 end:
d550d98d 992 return 0;
1da177e4
LT
993}
994
995static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
996{
997 return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
998}
999
1da177e4
LT
1000static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
1001{
4be44fcd 1002 int result = 0;
50dd0969 1003 struct acpi_thermal *tz = seq->private;
1da177e4 1004
1da177e4
LT
1005
1006 if (!tz)
1007 goto end;
1008
1009 result = acpi_thermal_get_temperature(tz);
1010 if (result)
1011 goto end;
1012
4be44fcd
LB
1013 seq_printf(seq, "temperature: %ld C\n",
1014 KELVIN_TO_CELSIUS(tz->temperature));
1da177e4 1015
4be44fcd 1016 end:
d550d98d 1017 return 0;
1da177e4
LT
1018}
1019
1020static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
1021{
1022 return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
1023}
1024
1da177e4
LT
1025static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
1026{
50dd0969 1027 struct acpi_thermal *tz = seq->private;
68ccfaa8 1028 struct acpi_device *device;
e7c746ef
TR
1029 acpi_status status;
1030
4be44fcd
LB
1031 int i = 0;
1032 int j = 0;
1da177e4 1033
1da177e4
LT
1034
1035 if (!tz)
1036 goto end;
1037
1038 if (tz->trips.critical.flags.valid)
f5487145
LB
1039 seq_printf(seq, "critical (S5): %ld C%s",
1040 KELVIN_TO_CELSIUS(tz->trips.critical.temperature),
1041 nocrt ? " <disabled>\n" : "\n");
1da177e4
LT
1042
1043 if (tz->trips.hot.flags.valid)
f5487145
LB
1044 seq_printf(seq, "hot (S4): %ld C%s",
1045 KELVIN_TO_CELSIUS(tz->trips.hot.temperature),
1046 nocrt ? " <disabled>\n" : "\n");
1da177e4
LT
1047
1048 if (tz->trips.passive.flags.valid) {
4be44fcd
LB
1049 seq_printf(seq,
1050 "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
1051 KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
1052 tz->trips.passive.tc1, tz->trips.passive.tc2,
1053 tz->trips.passive.tsp);
1054 for (j = 0; j < tz->trips.passive.devices.count; j++) {
e7c746ef
TR
1055 status = acpi_bus_get_device(tz->trips.passive.devices.
1056 handles[j], &device);
1057 seq_printf(seq, "%4.4s ", status ? "" :
1058 acpi_device_bid(device));
1da177e4
LT
1059 }
1060 seq_puts(seq, "\n");
7fb2616e
FP
1061 } else {
1062 seq_printf(seq, "passive (forced):");
1063 if (tz->thermal_zone->forced_passive)
1064 seq_printf(seq, " %i C\n",
1065 tz->thermal_zone->forced_passive / 1000);
1066 else
1067 seq_printf(seq, "<not set>\n");
1da177e4
LT
1068 }
1069
1070 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1071 if (!(tz->trips.active[i].flags.valid))
1072 break;
1073 seq_printf(seq, "active[%d]: %ld C: devices=",
4be44fcd
LB
1074 i,
1075 KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
68ccfaa8 1076 for (j = 0; j < tz->trips.active[i].devices.count; j++){
e7c746ef
TR
1077 status = acpi_bus_get_device(tz->trips.active[i].
1078 devices.handles[j],
1079 &device);
1080 seq_printf(seq, "%4.4s ", status ? "" :
1081 acpi_device_bid(device));
68ccfaa8 1082 }
1da177e4
LT
1083 seq_puts(seq, "\n");
1084 }
1085
4be44fcd 1086 end:
d550d98d 1087 return 0;
1da177e4
LT
1088}
1089
1090static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
1091{
1092 return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
1093}
1094
1da177e4
LT
1095static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
1096{
50dd0969 1097 struct acpi_thermal *tz = seq->private;
1da177e4 1098
1da177e4
LT
1099
1100 if (!tz)
1101 goto end;
1102
eaca2d3f 1103 if (!tz->flags.cooling_mode)
1da177e4 1104 seq_puts(seq, "<setting not supported>\n");
1da177e4 1105 else
eaca2d3f 1106 seq_puts(seq, "0 - Active; 1 - Passive\n");
1da177e4 1107
4be44fcd 1108 end:
d550d98d 1109 return 0;
1da177e4
LT
1110}
1111
1112static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
1113{
1114 return single_open(file, acpi_thermal_cooling_seq_show,
4be44fcd 1115 PDE(inode)->data);
1da177e4
LT
1116}
1117
1118static ssize_t
4be44fcd
LB
1119acpi_thermal_write_cooling_mode(struct file *file,
1120 const char __user * buffer,
1121 size_t count, loff_t * ppos)
1da177e4 1122{
50dd0969
JE
1123 struct seq_file *m = file->private_data;
1124 struct acpi_thermal *tz = m->private;
4be44fcd
LB
1125 int result = 0;
1126 char mode_string[12] = { '\0' };
1da177e4 1127
1da177e4
LT
1128
1129 if (!tz || (count > sizeof(mode_string) - 1))
d550d98d 1130 return -EINVAL;
1da177e4
LT
1131
1132 if (!tz->flags.cooling_mode)
d550d98d 1133 return -ENODEV;
1da177e4
LT
1134
1135 if (copy_from_user(mode_string, buffer, count))
d550d98d 1136 return -EFAULT;
4be44fcd 1137
1da177e4 1138 mode_string[count] = '\0';
4be44fcd
LB
1139
1140 result = acpi_thermal_set_cooling_mode(tz,
1141 simple_strtoul(mode_string, NULL,
1142 0));
1da177e4 1143 if (result)
d550d98d 1144 return result;
1da177e4
LT
1145
1146 acpi_thermal_check(tz);
1147
d550d98d 1148 return count;
1da177e4
LT
1149}
1150
1da177e4
LT
1151static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
1152{
50dd0969 1153 struct acpi_thermal *tz = seq->private;
1da177e4 1154
1da177e4
LT
1155
1156 if (!tz)
1157 goto end;
1158
b1569e99 1159 if (!tz->thermal_zone->polling_delay) {
1da177e4
LT
1160 seq_puts(seq, "<polling disabled>\n");
1161 goto end;
1162 }
1163
b1569e99
MG
1164 seq_printf(seq, "polling frequency: %d seconds\n",
1165 (tz->thermal_zone->polling_delay / 1000));
1da177e4 1166
4be44fcd 1167 end:
d550d98d 1168 return 0;
1da177e4
LT
1169}
1170
1171static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
1172{
1173 return single_open(file, acpi_thermal_polling_seq_show,
4be44fcd 1174 PDE(inode)->data);
1da177e4
LT
1175}
1176
43d9f87b
ZR
1177static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
1178{
1179 if (!tz)
1180 return -EINVAL;
1181
1182 /* Convert value to deci-seconds */
1183 tz->polling_frequency = seconds * 10;
1184
1185 tz->thermal_zone->polling_delay = seconds * 1000;
1186
1187 if (tz->tz_enabled)
1188 thermal_zone_device_update(tz->thermal_zone);
1189
1190 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1191 "Polling frequency set to %lu seconds\n",
1192 tz->polling_frequency/10));
1193
1194 return 0;
1195}
1196
1da177e4 1197static ssize_t
4be44fcd
LB
1198acpi_thermal_write_polling(struct file *file,
1199 const char __user * buffer,
1200 size_t count, loff_t * ppos)
1da177e4 1201{
50dd0969
JE
1202 struct seq_file *m = file->private_data;
1203 struct acpi_thermal *tz = m->private;
4be44fcd
LB
1204 int result = 0;
1205 char polling_string[12] = { '\0' };
1206 int seconds = 0;
1da177e4 1207
1da177e4
LT
1208
1209 if (!tz || (count > sizeof(polling_string) - 1))
d550d98d 1210 return -EINVAL;
4be44fcd 1211
1da177e4 1212 if (copy_from_user(polling_string, buffer, count))
d550d98d 1213 return -EFAULT;
4be44fcd 1214
1da177e4
LT
1215 polling_string[count] = '\0';
1216
1217 seconds = simple_strtoul(polling_string, NULL, 0);
4be44fcd 1218
1da177e4
LT
1219 result = acpi_thermal_set_polling(tz, seconds);
1220 if (result)
d550d98d 1221 return result;
1da177e4
LT
1222
1223 acpi_thermal_check(tz);
1224
d550d98d 1225 return count;
1da177e4
LT
1226}
1227
4be44fcd 1228static int acpi_thermal_add_fs(struct acpi_device *device)
1da177e4 1229{
4be44fcd 1230 struct proc_dir_entry *entry = NULL;
1da177e4 1231
1da177e4
LT
1232
1233 if (!acpi_device_dir(device)) {
1234 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
4be44fcd 1235 acpi_thermal_dir);
1da177e4 1236 if (!acpi_device_dir(device))
d550d98d 1237 return -ENODEV;
1da177e4
LT
1238 }
1239
1240 /* 'state' [R] */
cf7acfab
DL
1241 entry = proc_create_data(ACPI_THERMAL_FILE_STATE,
1242 S_IRUGO, acpi_device_dir(device),
1243 &acpi_thermal_state_fops,
1244 acpi_driver_data(device));
1da177e4 1245 if (!entry)
d550d98d 1246 return -ENODEV;
1da177e4
LT
1247
1248 /* 'temperature' [R] */
cf7acfab
DL
1249 entry = proc_create_data(ACPI_THERMAL_FILE_TEMPERATURE,
1250 S_IRUGO, acpi_device_dir(device),
1251 &acpi_thermal_temp_fops,
1252 acpi_driver_data(device));
1da177e4 1253 if (!entry)
d550d98d 1254 return -ENODEV;
1da177e4 1255
2db9ccba 1256 /* 'trip_points' [R] */
cf7acfab
DL
1257 entry = proc_create_data(ACPI_THERMAL_FILE_TRIP_POINTS,
1258 S_IRUGO,
1259 acpi_device_dir(device),
1260 &acpi_thermal_trip_fops,
1261 acpi_driver_data(device));
1da177e4 1262 if (!entry)
d550d98d 1263 return -ENODEV;
1da177e4
LT
1264
1265 /* 'cooling_mode' [R/W] */
cf7acfab
DL
1266 entry = proc_create_data(ACPI_THERMAL_FILE_COOLING_MODE,
1267 S_IFREG | S_IRUGO | S_IWUSR,
1268 acpi_device_dir(device),
1269 &acpi_thermal_cooling_fops,
1270 acpi_driver_data(device));
1da177e4 1271 if (!entry)
d550d98d 1272 return -ENODEV;
1da177e4
LT
1273
1274 /* 'polling_frequency' [R/W] */
cf7acfab
DL
1275 entry = proc_create_data(ACPI_THERMAL_FILE_POLLING_FREQ,
1276 S_IFREG | S_IRUGO | S_IWUSR,
1277 acpi_device_dir(device),
1278 &acpi_thermal_polling_fops,
1279 acpi_driver_data(device));
1da177e4 1280 if (!entry)
d550d98d 1281 return -ENODEV;
d550d98d 1282 return 0;
1da177e4
LT
1283}
1284
4be44fcd 1285static int acpi_thermal_remove_fs(struct acpi_device *device)
1da177e4 1286{
1da177e4
LT
1287
1288 if (acpi_device_dir(device)) {
1289 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1290 acpi_device_dir(device));
1291 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1292 acpi_device_dir(device));
1293 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1294 acpi_device_dir(device));
1295 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1296 acpi_device_dir(device));
1297 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1298 acpi_device_dir(device));
1299 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1300 acpi_device_dir(device) = NULL;
1301 }
1302
d550d98d 1303 return 0;
1da177e4 1304}
43d9f87b
ZR
1305#else
1306static inline int acpi_thermal_add_fs(struct acpi_device *device) { return 0; }
1307static inline int acpi_thermal_remove_fs(struct acpi_device *device)
1308{
1309 return 0;
1310}
1311#endif /* CONFIG_ACPI_PROCFS */
1da177e4
LT
1312/* --------------------------------------------------------------------------
1313 Driver Interface
1314 -------------------------------------------------------------------------- */
1315
342d550d 1316static void acpi_thermal_notify(struct acpi_device *device, u32 event)
1da177e4 1317{
342d550d 1318 struct acpi_thermal *tz = acpi_driver_data(device);
1da177e4 1319
1da177e4
LT
1320
1321 if (!tz)
d550d98d 1322 return;
1da177e4 1323
1da177e4
LT
1324 switch (event) {
1325 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1326 acpi_thermal_check(tz);
1327 break;
1328 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
ce44e197 1329 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
1da177e4 1330 acpi_thermal_check(tz);
14e04fb3 1331 acpi_bus_generate_proc_event(device, event, 0);
962ce8ca 1332 acpi_bus_generate_netlink_event(device->pnp.device_class,
0794469d 1333 dev_name(&device->dev), event, 0);
1da177e4
LT
1334 break;
1335 case ACPI_THERMAL_NOTIFY_DEVICES:
ce44e197
ZR
1336 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
1337 acpi_thermal_check(tz);
14e04fb3 1338 acpi_bus_generate_proc_event(device, event, 0);
962ce8ca 1339 acpi_bus_generate_netlink_event(device->pnp.device_class,
0794469d 1340 dev_name(&device->dev), event, 0);
1da177e4
LT
1341 break;
1342 default:
1343 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd 1344 "Unsupported event [0x%x]\n", event));
1da177e4
LT
1345 break;
1346 }
1da177e4
LT
1347}
1348
4be44fcd 1349static int acpi_thermal_get_info(struct acpi_thermal *tz)
1da177e4 1350{
4be44fcd 1351 int result = 0;
1da177e4 1352
1da177e4
LT
1353
1354 if (!tz)
d550d98d 1355 return -EINVAL;
1da177e4
LT
1356
1357 /* Get temperature [_TMP] (required) */
1358 result = acpi_thermal_get_temperature(tz);
1359 if (result)
d550d98d 1360 return result;
1da177e4
LT
1361
1362 /* Get trip points [_CRT, _PSV, etc.] (required) */
1363 result = acpi_thermal_get_trip_points(tz);
1364 if (result)
d550d98d 1365 return result;
1da177e4
LT
1366
1367 /* Set the cooling mode [_SCP] to active cooling (default) */
1368 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
4be44fcd 1369 if (!result)
1da177e4 1370 tz->flags.cooling_mode = 1;
1da177e4
LT
1371
1372 /* Get default polling frequency [_TZP] (optional) */
1373 if (tzp)
1374 tz->polling_frequency = tzp;
1375 else
1376 acpi_thermal_get_polling_frequency(tz);
1377
d550d98d 1378 return 0;
1da177e4
LT
1379}
1380
13614e37
JD
1381/*
1382 * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI
1383 * handles temperature values with a single decimal place. As a consequence,
1384 * some implementations use an offset of 273.1 and others use an offset of
1385 * 273.2. Try to find out which one is being used, to present the most
1386 * accurate and visually appealing number.
1387 *
1388 * The heuristic below should work for all ACPI thermal zones which have a
1389 * critical trip point with a value being a multiple of 0.5 degree Celsius.
1390 */
1391static void acpi_thermal_guess_offset(struct acpi_thermal *tz)
1392{
1393 if (tz->trips.critical.flags.valid &&
1394 (tz->trips.critical.temperature % 5) == 1)
1395 tz->kelvin_offset = 2731;
1396 else
1397 tz->kelvin_offset = 2732;
1398}
1399
4be44fcd 1400static int acpi_thermal_add(struct acpi_device *device)
1da177e4 1401{
4be44fcd 1402 int result = 0;
4be44fcd 1403 struct acpi_thermal *tz = NULL;
1da177e4 1404
1da177e4
LT
1405
1406 if (!device)
d550d98d 1407 return -EINVAL;
1da177e4 1408
36bcbec7 1409 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
1da177e4 1410 if (!tz)
d550d98d 1411 return -ENOMEM;
1da177e4 1412
8348e1b1 1413 tz->device = device;
1da177e4
LT
1414 strcpy(tz->name, device->pnp.bus_id);
1415 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1416 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
db89b4f0 1417 device->driver_data = tz;
6e215785 1418 mutex_init(&tz->lock);
3f655ef8
ZR
1419
1420
1da177e4
LT
1421 result = acpi_thermal_get_info(tz);
1422 if (result)
3f655ef8
ZR
1423 goto free_memory;
1424
13614e37
JD
1425 acpi_thermal_guess_offset(tz);
1426
3f655ef8
ZR
1427 result = acpi_thermal_register_thermal_zone(tz);
1428 if (result)
1429 goto free_memory;
1da177e4
LT
1430
1431 result = acpi_thermal_add_fs(device);
1432 if (result)
3f655ef8 1433 goto unregister_thermal_zone;
1da177e4 1434
1da177e4 1435 printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
4be44fcd
LB
1436 acpi_device_name(device), acpi_device_bid(device),
1437 KELVIN_TO_CELSIUS(tz->temperature));
3f655ef8 1438 goto end;
1da177e4 1439
3f655ef8
ZR
1440unregister_thermal_zone:
1441 thermal_zone_device_unregister(tz->thermal_zone);
1442free_memory:
1443 kfree(tz);
1444end:
d550d98d 1445 return result;
1da177e4
LT
1446}
1447
4be44fcd 1448static int acpi_thermal_remove(struct acpi_device *device, int type)
1da177e4 1449{
4be44fcd 1450 struct acpi_thermal *tz = NULL;
1da177e4 1451
1da177e4 1452 if (!device || !acpi_driver_data(device))
d550d98d 1453 return -EINVAL;
1da177e4 1454
50dd0969 1455 tz = acpi_driver_data(device);
1da177e4 1456
1da177e4 1457 acpi_thermal_remove_fs(device);
3f655ef8 1458 acpi_thermal_unregister_thermal_zone(tz);
6e215785 1459 mutex_destroy(&tz->lock);
1da177e4 1460 kfree(tz);
d550d98d 1461 return 0;
1da177e4
LT
1462}
1463
5d9464a4 1464static int acpi_thermal_resume(struct acpi_device *device)
74ce1468
KK
1465{
1466 struct acpi_thermal *tz = NULL;
b1028c54
KK
1467 int i, j, power_state, result;
1468
74ce1468
KK
1469
1470 if (!device || !acpi_driver_data(device))
d550d98d 1471 return -EINVAL;
74ce1468 1472
50dd0969 1473 tz = acpi_driver_data(device);
74ce1468 1474
bed936f7 1475 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
b1028c54
KK
1476 if (!(&tz->trips.active[i]))
1477 break;
1478 if (!tz->trips.active[i].flags.valid)
1479 break;
1480 tz->trips.active[i].flags.enabled = 1;
1481 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1482 result = acpi_bus_get_power(tz->trips.active[i].devices.
1483 handles[j], &power_state);
1484 if (result || (power_state != ACPI_STATE_D0)) {
1485 tz->trips.active[i].flags.enabled = 0;
1486 break;
1487 }
bed936f7 1488 }
b1028c54 1489 tz->state.active |= tz->trips.active[i].flags.enabled;
bed936f7
KK
1490 }
1491
b1028c54 1492 acpi_thermal_check(tz);
74ce1468
KK
1493
1494 return AE_OK;
1495}
1496
1855256c 1497static int thermal_act(const struct dmi_system_id *d) {
0b5bfa1c
LB
1498
1499 if (act == 0) {
1500 printk(KERN_NOTICE "ACPI: %s detected: "
1501 "disabling all active thermal trip points\n", d->ident);
1502 act = -1;
1503 }
1504 return 0;
1505}
1855256c 1506static int thermal_nocrt(const struct dmi_system_id *d) {
8c99fdce
LB
1507
1508 printk(KERN_NOTICE "ACPI: %s detected: "
1509 "disabling all critical thermal trip point actions.\n", d->ident);
1510 nocrt = 1;
1511 return 0;
1512}
1855256c 1513static int thermal_tzp(const struct dmi_system_id *d) {
0b5bfa1c
LB
1514
1515 if (tzp == 0) {
1516 printk(KERN_NOTICE "ACPI: %s detected: "
1517 "enabling thermal zone polling\n", d->ident);
1518 tzp = 300; /* 300 dS = 30 Seconds */
1519 }
1520 return 0;
1521}
1855256c 1522static int thermal_psv(const struct dmi_system_id *d) {
0b5bfa1c
LB
1523
1524 if (psv == 0) {
1525 printk(KERN_NOTICE "ACPI: %s detected: "
1526 "disabling all passive thermal trip points\n", d->ident);
1527 psv = -1;
1528 }
1529 return 0;
1530}
1531
1532static struct dmi_system_id thermal_dmi_table[] __initdata = {
1533 /*
1534 * Award BIOS on this AOpen makes thermal control almost worthless.
1535 * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1536 */
1537 {
1538 .callback = thermal_act,
1539 .ident = "AOpen i915GMm-HFS",
1540 .matches = {
1541 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1542 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1543 },
1544 },
1545 {
1546 .callback = thermal_psv,
1547 .ident = "AOpen i915GMm-HFS",
1548 .matches = {
1549 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1550 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1551 },
1552 },
1553 {
1554 .callback = thermal_tzp,
1555 .ident = "AOpen i915GMm-HFS",
1556 .matches = {
1557 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1558 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1559 },
1560 },
8c99fdce
LB
1561 {
1562 .callback = thermal_nocrt,
1563 .ident = "Gigabyte GA-7ZX",
1564 .matches = {
1565 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
1566 DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
1567 },
1568 },
0b5bfa1c
LB
1569 {}
1570};
0b5bfa1c 1571
4be44fcd 1572static int __init acpi_thermal_init(void)
1da177e4 1573{
4be44fcd 1574 int result = 0;
1da177e4 1575
0b5bfa1c
LB
1576 dmi_check_system(thermal_dmi_table);
1577
72b33ef8
LB
1578 if (off) {
1579 printk(KERN_NOTICE "ACPI: thermal control disabled\n");
1580 return -ENODEV;
1581 }
43d9f87b
ZR
1582
1583#ifdef CONFIG_ACPI_PROCFS
1da177e4
LT
1584 acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1585 if (!acpi_thermal_dir)
d550d98d 1586 return -ENODEV;
43d9f87b 1587#endif
1da177e4
LT
1588
1589 result = acpi_bus_register_driver(&acpi_thermal_driver);
1590 if (result < 0) {
43d9f87b 1591#ifdef CONFIG_ACPI_PROCFS
1da177e4 1592 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
43d9f87b 1593#endif
d550d98d 1594 return -ENODEV;
1da177e4
LT
1595 }
1596
d550d98d 1597 return 0;
1da177e4
LT
1598}
1599
4be44fcd 1600static void __exit acpi_thermal_exit(void)
1da177e4 1601{
1da177e4
LT
1602
1603 acpi_bus_unregister_driver(&acpi_thermal_driver);
1604
43d9f87b 1605#ifdef CONFIG_ACPI_PROCFS
1da177e4 1606 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
43d9f87b 1607#endif
1da177e4 1608
d550d98d 1609 return;
1da177e4
LT
1610}
1611
1da177e4
LT
1612module_init(acpi_thermal_init);
1613module_exit(acpi_thermal_exit);