blob: 3521c37bbd3390c3a74e7c5fa2efb77eb5ffb4ee [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Tim Schmielaucd354f12007-02-14 00:33:14 -080039#include <linux/timer.h>
40#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/kmod.h>
42#include <linux/seq_file.h>
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -070043#include <linux/reboot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/uaccess.h>
45
46#include <acpi/acpi_bus.h>
47#include <acpi/acpi_drivers.h>
48
49#define ACPI_THERMAL_COMPONENT 0x04000000
50#define ACPI_THERMAL_CLASS "thermal_zone"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
52#define ACPI_THERMAL_FILE_STATE "state"
53#define ACPI_THERMAL_FILE_TEMPERATURE "temperature"
54#define ACPI_THERMAL_FILE_TRIP_POINTS "trip_points"
55#define ACPI_THERMAL_FILE_COOLING_MODE "cooling_mode"
56#define ACPI_THERMAL_FILE_POLLING_FREQ "polling_frequency"
57#define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
58#define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
59#define ACPI_THERMAL_NOTIFY_DEVICES 0x82
60#define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
61#define ACPI_THERMAL_NOTIFY_HOT 0xF1
62#define ACPI_THERMAL_MODE_ACTIVE 0x00
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
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
Len Brownf52fd662007-02-12 22:42:12 -050071ACPI_MODULE_NAME("thermal");
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Thomas Renninger1cbf4c52004-09-16 11:07:00 -040073MODULE_AUTHOR("Paul Diefenbaugh");
Len Brown7cda93e2007-02-12 23:50:02 -050074MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070075MODULE_LICENSE("GPL");
76
Len Brownf8707ec2007-08-12 00:12:54 -040077static int act;
78module_param(act, int, 0644);
79MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.\n");
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static int tzp;
Len Brown730ff342007-08-12 00:12:26 -040082module_param(tzp, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.\n");
84
Len Brownf5487142007-08-12 00:12:44 -040085static int nocrt;
86module_param(nocrt, int, 0);
87MODULE_PARM_DESC(nocrt, "Set to disable action on ACPI thermal zone critical and hot trips.\n");
88
Len Brown72b33ef2007-08-12 00:12:17 -040089static int off;
90module_param(off, int, 0);
91MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.\n");
92
Len Browna70cdc52007-08-12 00:12:35 -040093static int psv;
94module_param(psv, int, 0644);
95MODULE_PARM_DESC(psv, "Disable or override all passive trip points.\n");
96
Len Brown4be44fc2005-08-05 00:44:28 -040097static int acpi_thermal_add(struct acpi_device *device);
98static int acpi_thermal_remove(struct acpi_device *device, int type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +080099static int acpi_thermal_resume(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
101static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
102static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -0400104static ssize_t acpi_thermal_write_cooling_mode(struct file *,
105 const char __user *, size_t,
106 loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -0400108static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
109 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200111static const struct acpi_device_id thermal_device_ids[] = {
112 {ACPI_THERMAL_HID, 0},
113 {"", 0},
114};
115MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117static struct acpi_driver acpi_thermal_driver = {
Len Brownc2b67052007-02-12 23:33:40 -0500118 .name = "thermal",
Len Brown4be44fc2005-08-05 00:44:28 -0400119 .class = ACPI_THERMAL_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200120 .ids = thermal_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -0400121 .ops = {
122 .add = acpi_thermal_add,
123 .remove = acpi_thermal_remove,
Konstantin Karasyov74ce14682006-05-08 08:32:00 -0400124 .resume = acpi_thermal_resume,
Len Brown4be44fc2005-08-05 00:44:28 -0400125 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127
128struct acpi_thermal_state {
Len Brown4be44fc2005-08-05 00:44:28 -0400129 u8 critical:1;
130 u8 hot:1;
131 u8 passive:1;
132 u8 active:1;
133 u8 reserved:4;
134 int active_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135};
136
137struct acpi_thermal_state_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400138 u8 valid:1;
139 u8 enabled:1;
140 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
143struct acpi_thermal_critical {
144 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400145 unsigned long temperature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146};
147
148struct acpi_thermal_hot {
149 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400150 unsigned long temperature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
153struct acpi_thermal_passive {
154 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400155 unsigned long temperature;
156 unsigned long tc1;
157 unsigned long tc2;
158 unsigned long tsp;
159 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160};
161
162struct acpi_thermal_active {
163 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400164 unsigned long temperature;
165 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166};
167
168struct acpi_thermal_trips {
169 struct acpi_thermal_critical critical;
Len Brown4be44fc2005-08-05 00:44:28 -0400170 struct acpi_thermal_hot hot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 struct acpi_thermal_passive passive;
172 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
173};
174
175struct acpi_thermal_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400176 u8 cooling_mode:1; /* _SCP */
177 u8 devices:1; /* _TZD */
178 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179};
180
181struct acpi_thermal {
Patrick Mochel8348e1b2006-05-19 16:54:40 -0400182 struct acpi_device * device;
Len Brown4be44fc2005-08-05 00:44:28 -0400183 acpi_bus_id name;
184 unsigned long temperature;
185 unsigned long last_temperature;
186 unsigned long polling_frequency;
Len Brown4be44fc2005-08-05 00:44:28 -0400187 volatile u8 zombie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 struct acpi_thermal_flags flags;
189 struct acpi_thermal_state state;
190 struct acpi_thermal_trips trips;
Len Brown4be44fc2005-08-05 00:44:28 -0400191 struct acpi_handle_list devices;
192 struct timer_list timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193};
194
Arjan van de Vend7508032006-07-04 13:06:00 -0400195static const struct file_operations acpi_thermal_state_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400196 .open = acpi_thermal_state_open_fs,
197 .read = seq_read,
198 .llseek = seq_lseek,
199 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200};
201
Arjan van de Vend7508032006-07-04 13:06:00 -0400202static const struct file_operations acpi_thermal_temp_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400203 .open = acpi_thermal_temp_open_fs,
204 .read = seq_read,
205 .llseek = seq_lseek,
206 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207};
208
Arjan van de Vend7508032006-07-04 13:06:00 -0400209static const struct file_operations acpi_thermal_trip_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400210 .open = acpi_thermal_trip_open_fs,
211 .read = seq_read,
Len Brown4be44fc2005-08-05 00:44:28 -0400212 .llseek = seq_lseek,
213 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214};
215
Arjan van de Vend7508032006-07-04 13:06:00 -0400216static const struct file_operations acpi_thermal_cooling_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400217 .open = acpi_thermal_cooling_open_fs,
218 .read = seq_read,
219 .write = acpi_thermal_write_cooling_mode,
220 .llseek = seq_lseek,
221 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222};
223
Arjan van de Vend7508032006-07-04 13:06:00 -0400224static const struct file_operations acpi_thermal_polling_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400225 .open = acpi_thermal_polling_open_fs,
226 .read = seq_read,
227 .write = acpi_thermal_write_polling,
228 .llseek = seq_lseek,
229 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230};
231
232/* --------------------------------------------------------------------------
233 Thermal Zone Management
234 -------------------------------------------------------------------------- */
235
Len Brown4be44fc2005-08-05 00:44:28 -0400236static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Len Brown4be44fc2005-08-05 00:44:28 -0400238 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400242 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 tz->last_temperature = tz->temperature;
245
Len Brown4be44fc2005-08-05 00:44:28 -0400246 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400247 acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tz->temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400249 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Len Brown4be44fc2005-08-05 00:44:28 -0400251 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
252 tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Patrick Mocheld550d982006-06-27 00:41:40 -0400254 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Len Brown4be44fc2005-08-05 00:44:28 -0400257static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Len Brown4be44fc2005-08-05 00:44:28 -0400259 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400263 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Len Brown4be44fc2005-08-05 00:44:28 -0400265 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400266 acpi_evaluate_integer(tz->device->handle, "_TZP", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400267 &tz->polling_frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400269 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Len Brown4be44fc2005-08-05 00:44:28 -0400271 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
272 tz->polling_frequency));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Patrick Mocheld550d982006-06-27 00:41:40 -0400274 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
Len Brown4be44fc2005-08-05 00:44:28 -0400277static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400281 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 tz->polling_frequency = seconds * 10; /* Convert value to deci-seconds */
284
Len Brown4be44fc2005-08-05 00:44:28 -0400285 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
286 "Polling frequency set to %lu seconds\n",
Sanjoy Mahajan636cedf2007-02-16 01:24:43 -0500287 tz->polling_frequency/10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Patrick Mocheld550d982006-06-27 00:41:40 -0400289 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Len Brown4be44fc2005-08-05 00:44:28 -0400292static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Len Brown4be44fc2005-08-05 00:44:28 -0400294 acpi_status status = AE_OK;
295 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
296 struct acpi_object_list arg_list = { 1, &arg0 };
297 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400301 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400303 status = acpi_get_handle(tz->device->handle, "_SCP", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (ACPI_FAILURE(status)) {
305 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400306 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308
309 arg0.integer.value = mode;
310
311 status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
312 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400313 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Patrick Mocheld550d982006-06-27 00:41:40 -0400315 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
Len Brown4be44fc2005-08-05 00:44:28 -0400318static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Len Brown4be44fc2005-08-05 00:44:28 -0400320 acpi_status status = AE_OK;
321 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400325 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 /* Critical Shutdown (required) */
328
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400329 status = acpi_evaluate_integer(tz->device->handle, "_CRT", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400330 &tz->trips.critical.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (ACPI_FAILURE(status)) {
332 tz->trips.critical.flags.valid = 0;
Thomas Renningera6fc6722006-06-26 23:58:43 -0400333 ACPI_EXCEPTION((AE_INFO, status, "No critical threshold"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400334 return -ENODEV;
Len Brown4be44fc2005-08-05 00:44:28 -0400335 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 tz->trips.critical.flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400337 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
338 "Found critical threshold [%lu]\n",
339 tz->trips.critical.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341
342 /* Critical Sleep (optional) */
343
Len Brown4be44fc2005-08-05 00:44:28 -0400344 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400345 acpi_evaluate_integer(tz->device->handle, "_HOT", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400346 &tz->trips.hot.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (ACPI_FAILURE(status)) {
348 tz->trips.hot.flags.valid = 0;
349 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No hot threshold\n"));
Len Brown4be44fc2005-08-05 00:44:28 -0400350 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 tz->trips.hot.flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400352 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found hot threshold [%lu]\n",
353 tz->trips.hot.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355
356 /* Passive: Processors (optional) */
357
Len Browna70cdc52007-08-12 00:12:35 -0400358 if (psv == -1) {
359 status = AE_SUPPORT;
360 } else if (psv > 0) {
361 tz->trips.passive.temperature = CELSIUS_TO_KELVIN(psv);
362 status = AE_OK;
363 } else {
364 status = acpi_evaluate_integer(tz->device->handle,
365 "_PSV", NULL, &tz->trips.passive.temperature);
366 }
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (ACPI_FAILURE(status)) {
369 tz->trips.passive.flags.valid = 0;
370 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No passive threshold\n"));
Len Brown4be44fc2005-08-05 00:44:28 -0400371 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 tz->trips.passive.flags.valid = 1;
373
Len Brown4be44fc2005-08-05 00:44:28 -0400374 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400375 acpi_evaluate_integer(tz->device->handle, "_TC1", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400376 &tz->trips.passive.tc1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (ACPI_FAILURE(status))
378 tz->trips.passive.flags.valid = 0;
379
Len Brown4be44fc2005-08-05 00:44:28 -0400380 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400381 acpi_evaluate_integer(tz->device->handle, "_TC2", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400382 &tz->trips.passive.tc2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (ACPI_FAILURE(status))
384 tz->trips.passive.flags.valid = 0;
385
Len Brown4be44fc2005-08-05 00:44:28 -0400386 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400387 acpi_evaluate_integer(tz->device->handle, "_TSP", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400388 &tz->trips.passive.tsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (ACPI_FAILURE(status))
390 tz->trips.passive.flags.valid = 0;
391
Len Brown4be44fc2005-08-05 00:44:28 -0400392 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400393 acpi_evaluate_reference(tz->device->handle, "_PSL", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400394 &tz->trips.passive.devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (ACPI_FAILURE(status))
396 tz->trips.passive.flags.valid = 0;
397
398 if (!tz->trips.passive.flags.valid)
Len Browncece9292006-06-26 23:04:31 -0400399 printk(KERN_WARNING PREFIX "Invalid passive threshold\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 else
Len Brown4be44fc2005-08-05 00:44:28 -0400401 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
402 "Found passive threshold [%lu]\n",
403 tz->trips.passive.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405
406 /* Active: Fans, etc. (optional) */
407
Len Brown4be44fc2005-08-05 00:44:28 -0400408 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Len Brown4be44fc2005-08-05 00:44:28 -0400410 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Len Brownf8707ec2007-08-12 00:12:54 -0400412 if (act == -1)
413 break; /* disable all active trip points */
414
415 status = acpi_evaluate_integer(tz->device->handle,
416 name, NULL, &tz->trips.active[i].temperature);
417
418 if (ACPI_FAILURE(status)) {
419 if (i == 0) /* no active trip points */
420 break;
421 if (act <= 0) /* no override requested */
422 break;
423 if (i == 1) { /* 1 trip point */
424 tz->trips.active[0].temperature =
425 CELSIUS_TO_KELVIN(act);
426 } else { /* multiple trips */
427 /*
428 * Don't allow override higher than
429 * the next higher trip point
430 */
431 tz->trips.active[i - 1].temperature =
432 (tz->trips.active[i - 2].temperature <
433 CELSIUS_TO_KELVIN(act) ?
434 tz->trips.active[i - 2].temperature :
435 CELSIUS_TO_KELVIN(act));
436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 break;
Len Brownf8707ec2007-08-12 00:12:54 -0400438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 name[2] = 'L';
Len Brown4be44fc2005-08-05 00:44:28 -0400441 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400442 acpi_evaluate_reference(tz->device->handle, name, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400443 &tz->trips.active[i].devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (ACPI_SUCCESS(status)) {
445 tz->trips.active[i].flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400446 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
447 "Found active threshold [%d]:[%lu]\n",
448 i, tz->trips.active[i].temperature));
449 } else
Thomas Renningera6fc6722006-06-26 23:58:43 -0400450 ACPI_EXCEPTION((AE_INFO, status,
451 "Invalid active threshold [%d]", i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453
Patrick Mocheld550d982006-06-27 00:41:40 -0400454 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Len Brown4be44fc2005-08-05 00:44:28 -0400457static int acpi_thermal_get_devices(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Len Brown4be44fc2005-08-05 00:44:28 -0400459 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400463 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Len Brown4be44fc2005-08-05 00:44:28 -0400465 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400466 acpi_evaluate_reference(tz->device->handle, "_TZD", NULL, &tz->devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400468 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Patrick Mocheld550d982006-06-27 00:41:40 -0400470 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
Len Brown4be44fc2005-08-05 00:44:28 -0400473static int acpi_thermal_critical(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Len Brownf5487142007-08-12 00:12:44 -0400475 if (!tz || !tz->trips.critical.flags.valid || nocrt)
Patrick Mocheld550d982006-06-27 00:41:40 -0400476 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 if (tz->temperature >= tz->trips.critical.temperature) {
Len Browncece9292006-06-26 23:04:31 -0400479 printk(KERN_WARNING PREFIX "Critical trip point\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 tz->trips.critical.flags.enabled = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400481 } else if (tz->trips.critical.flags.enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 tz->trips.critical.flags.enabled = 0;
483
Len Brown4be44fc2005-08-05 00:44:28 -0400484 printk(KERN_EMERG
485 "Critical temperature reached (%ld C), shutting down.\n",
486 KELVIN_TO_CELSIUS(tz->temperature));
Patrick Mochel8348e1b2006-05-19 16:54:40 -0400487 acpi_bus_generate_event(tz->device, ACPI_THERMAL_NOTIFY_CRITICAL,
Len Brown4be44fc2005-08-05 00:44:28 -0400488 tz->trips.critical.flags.enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -0700490 orderly_poweroff(true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Patrick Mocheld550d982006-06-27 00:41:40 -0400492 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
Len Brown4be44fc2005-08-05 00:44:28 -0400495static int acpi_thermal_hot(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Len Brownf5487142007-08-12 00:12:44 -0400497 if (!tz || !tz->trips.hot.flags.valid || nocrt)
Patrick Mocheld550d982006-06-27 00:41:40 -0400498 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 if (tz->temperature >= tz->trips.hot.temperature) {
Len Browncece9292006-06-26 23:04:31 -0400501 printk(KERN_WARNING PREFIX "Hot trip point\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 tz->trips.hot.flags.enabled = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400503 } else if (tz->trips.hot.flags.enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 tz->trips.hot.flags.enabled = 0;
505
Patrick Mochel8348e1b2006-05-19 16:54:40 -0400506 acpi_bus_generate_event(tz->device, ACPI_THERMAL_NOTIFY_HOT,
Len Brown4be44fc2005-08-05 00:44:28 -0400507 tz->trips.hot.flags.enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 /* TBD: Call user-mode "sleep(S4)" function */
510
Patrick Mocheld550d982006-06-27 00:41:40 -0400511 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400514static void acpi_thermal_passive(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400516 int result = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 struct acpi_thermal_passive *passive = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400518 int trend = 0;
519 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 if (!tz || !tz->trips.passive.flags.valid)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400523 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 passive = &(tz->trips.passive);
526
527 /*
528 * Above Trip?
529 * -----------
530 * Calculate the thermal trend (using the passive cooling equation)
531 * and modify the performance limit for all passive cooling devices
532 * accordingly. Note that we assume symmetry.
533 */
534 if (tz->temperature >= passive->temperature) {
Len Brown4be44fc2005-08-05 00:44:28 -0400535 trend =
536 (passive->tc1 * (tz->temperature - tz->last_temperature)) +
537 (passive->tc2 * (tz->temperature - passive->temperature));
538 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
539 "trend[%d]=(tc1[%lu]*(tmp[%lu]-last[%lu]))+(tc2[%lu]*(tmp[%lu]-psv[%lu]))\n",
540 trend, passive->tc1, tz->temperature,
541 tz->last_temperature, passive->tc2,
542 tz->temperature, passive->temperature));
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400543 passive->flags.enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 /* Heating up? */
545 if (trend > 0)
Len Brown4be44fc2005-08-05 00:44:28 -0400546 for (i = 0; i < passive->devices.count; i++)
547 acpi_processor_set_thermal_limit(passive->
548 devices.
549 handles[i],
550 ACPI_PROCESSOR_LIMIT_INCREMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /* Cooling off? */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400552 else if (trend < 0) {
Len Brown4be44fc2005-08-05 00:44:28 -0400553 for (i = 0; i < passive->devices.count; i++)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400554 /*
555 * assume that we are on highest
556 * freq/lowest thrott and can leave
557 * passive mode, even in error case
558 */
559 if (!acpi_processor_set_thermal_limit
560 (passive->devices.handles[i],
561 ACPI_PROCESSOR_LIMIT_DECREMENT))
562 result = 0;
563 /*
564 * Leave cooling mode, even if the temp might
565 * higher than trip point This is because some
566 * machines might have long thermal polling
567 * frequencies (tsp) defined. We will fall back
568 * into passive mode in next cycle (probably quicker)
569 */
570 if (result) {
571 passive->flags.enabled = 0;
572 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
573 "Disabling passive cooling, still above threshold,"
574 " but we are cooling down\n"));
575 }
576 }
577 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579
580 /*
581 * Below Trip?
582 * -----------
583 * Implement passive cooling hysteresis to slowly increase performance
584 * and avoid thrashing around the passive trip point. Note that we
585 * assume symmetry.
586 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400587 if (!passive->flags.enabled)
588 return;
589 for (i = 0; i < passive->devices.count; i++)
590 if (!acpi_processor_set_thermal_limit
591 (passive->devices.handles[i],
592 ACPI_PROCESSOR_LIMIT_DECREMENT))
593 result = 0;
594 if (result) {
595 passive->flags.enabled = 0;
596 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
597 "Disabling passive cooling (zone is cool)\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400601static void acpi_thermal_active(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Len Brown4be44fc2005-08-05 00:44:28 -0400603 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 struct acpi_thermal_active *active = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400605 int i = 0;
606 int j = 0;
607 unsigned long maxtemp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 if (!tz)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400611 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Len Brown4be44fc2005-08-05 00:44:28 -0400613 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 active = &(tz->trips.active[i]);
615 if (!active || !active->flags.valid)
616 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (tz->temperature >= active->temperature) {
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400618 /*
619 * Above Threshold?
620 * ----------------
621 * If not already enabled, turn ON all cooling devices
622 * associated with this active threshold.
623 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (active->temperature > maxtemp)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400625 tz->state.active_index = i;
626 maxtemp = active->temperature;
627 if (active->flags.enabled)
628 continue;
629 for (j = 0; j < active->devices.count; j++) {
630 result =
631 acpi_bus_set_power(active->devices.
632 handles[j],
633 ACPI_STATE_D0);
634 if (result) {
Len Browncece9292006-06-26 23:04:31 -0400635 printk(KERN_WARNING PREFIX
636 "Unable to turn cooling device [%p] 'on'\n",
Thomas Renningera6fc6722006-06-26 23:58:43 -0400637 active->devices.
Len Browncece9292006-06-26 23:04:31 -0400638 handles[j]);
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400639 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400641 active->flags.enabled = 1;
642 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
643 "Cooling device [%p] now 'on'\n",
644 active->devices.handles[j]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400646 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400648 if (!active->flags.enabled)
649 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 /*
651 * Below Threshold?
652 * ----------------
653 * Turn OFF all cooling devices associated with this
654 * threshold.
655 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400656 for (j = 0; j < active->devices.count; j++) {
657 result = acpi_bus_set_power(active->devices.handles[j],
658 ACPI_STATE_D3);
659 if (result) {
Len Browncece9292006-06-26 23:04:31 -0400660 printk(KERN_WARNING PREFIX
661 "Unable to turn cooling device [%p] 'off'\n",
662 active->devices.handles[j]);
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400663 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400665 active->flags.enabled = 0;
666 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
667 "Cooling device [%p] now 'off'\n",
668 active->devices.handles[j]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
Len Brown4be44fc2005-08-05 00:44:28 -0400673static void acpi_thermal_check(void *context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Len Brown4be44fc2005-08-05 00:44:28 -0400675static void acpi_thermal_run(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
677 struct acpi_thermal *tz = (struct acpi_thermal *)data;
678 if (!tz->zombie)
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400679 acpi_os_execute(OSL_GPE_HANDLER, acpi_thermal_check, (void *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
Len Brown4be44fc2005-08-05 00:44:28 -0400682static void acpi_thermal_check(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Len Brown4be44fc2005-08-05 00:44:28 -0400684 int result = 0;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200685 struct acpi_thermal *tz = data;
Len Brown4be44fc2005-08-05 00:44:28 -0400686 unsigned long sleep_time = 0;
687 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 struct acpi_thermal_state state;
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 if (!tz) {
Len Brown64684632006-06-26 23:41:38 -0400692 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400693 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
695
696 state = tz->state;
697
698 result = acpi_thermal_get_temperature(tz);
699 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400700 return;
Len Brown4be44fc2005-08-05 00:44:28 -0400701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 memset(&tz->state, 0, sizeof(tz->state));
Len Brown4be44fc2005-08-05 00:44:28 -0400703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 /*
705 * Check Trip Points
706 * -----------------
707 * Compare the current temperature to the trip point values to see
708 * if we've entered one of the thermal policy states. Note that
709 * this function determines when a state is entered, but the
710 * individual policy decides when it is exited (e.g. hysteresis).
711 */
712 if (tz->trips.critical.flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400713 state.critical |=
714 (tz->temperature >= tz->trips.critical.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (tz->trips.hot.flags.valid)
716 state.hot |= (tz->temperature >= tz->trips.hot.temperature);
717 if (tz->trips.passive.flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400718 state.passive |=
719 (tz->temperature >= tz->trips.passive.temperature);
720 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (tz->trips.active[i].flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400722 state.active |=
723 (tz->temperature >=
724 tz->trips.active[i].temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 /*
727 * Invoke Policy
728 * -------------
729 * Separated from the above check to allow individual policy to
730 * determine when to exit a given state.
731 */
732 if (state.critical)
733 acpi_thermal_critical(tz);
734 if (state.hot)
735 acpi_thermal_hot(tz);
736 if (state.passive)
737 acpi_thermal_passive(tz);
738 if (state.active)
739 acpi_thermal_active(tz);
740
741 /*
742 * Calculate State
743 * ---------------
744 * Again, separated from the above two to allow independent policy
745 * decisions.
746 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400747 tz->state.critical = tz->trips.critical.flags.enabled;
748 tz->state.hot = tz->trips.hot.flags.enabled;
749 tz->state.passive = tz->trips.passive.flags.enabled;
750 tz->state.active = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400751 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400752 tz->state.active |= tz->trips.active[i].flags.enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 /*
755 * Calculate Sleep Time
756 * --------------------
757 * If we're in the passive state, use _TSP's value. Otherwise
758 * use the default polling frequency (e.g. _TZP). If no polling
759 * frequency is specified then we'll wait forever (at least until
760 * a thermal event occurs). Note that _TSP and _TZD values are
761 * given in 1/10th seconds (we must covert to milliseconds).
762 */
763 if (tz->state.passive)
764 sleep_time = tz->trips.passive.tsp * 100;
765 else if (tz->polling_frequency > 0)
766 sleep_time = tz->polling_frequency * 100;
767
Len Brown4be44fc2005-08-05 00:44:28 -0400768 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: temperature[%lu] sleep[%lu]\n",
769 tz->name, tz->temperature, sleep_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 /*
772 * Schedule Next Poll
773 * ------------------
774 */
775 if (!sleep_time) {
776 if (timer_pending(&(tz->timer)))
777 del_timer(&(tz->timer));
Len Brown4be44fc2005-08-05 00:44:28 -0400778 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (timer_pending(&(tz->timer)))
Andrew Morton94e22e12007-04-23 14:41:13 -0700780 mod_timer(&(tz->timer),
781 jiffies + (HZ * sleep_time) / 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400783 tz->timer.data = (unsigned long)tz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 tz->timer.function = acpi_thermal_run;
785 tz->timer.expires = jiffies + (HZ * sleep_time) / 1000;
786 add_timer(&(tz->timer));
787 }
788 }
789
Patrick Mocheld550d982006-06-27 00:41:40 -0400790 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793/* --------------------------------------------------------------------------
794 FS Interface (/proc)
795 -------------------------------------------------------------------------- */
796
Len Brown4be44fc2005-08-05 00:44:28 -0400797static struct proc_dir_entry *acpi_thermal_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
800{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200801 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 if (!tz)
805 goto end;
806
807 seq_puts(seq, "state: ");
808
Len Brown4be44fc2005-08-05 00:44:28 -0400809 if (!tz->state.critical && !tz->state.hot && !tz->state.passive
810 && !tz->state.active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 seq_puts(seq, "ok\n");
812 else {
813 if (tz->state.critical)
814 seq_puts(seq, "critical ");
815 if (tz->state.hot)
816 seq_puts(seq, "hot ");
817 if (tz->state.passive)
818 seq_puts(seq, "passive ");
819 if (tz->state.active)
820 seq_printf(seq, "active[%d]", tz->state.active_index);
821 seq_puts(seq, "\n");
822 }
823
Len Brown4be44fc2005-08-05 00:44:28 -0400824 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400825 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
827
828static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
829{
830 return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
831}
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
834{
Len Brown4be44fc2005-08-05 00:44:28 -0400835 int result = 0;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200836 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 if (!tz)
840 goto end;
841
842 result = acpi_thermal_get_temperature(tz);
843 if (result)
844 goto end;
845
Len Brown4be44fc2005-08-05 00:44:28 -0400846 seq_printf(seq, "temperature: %ld C\n",
847 KELVIN_TO_CELSIUS(tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Len Brown4be44fc2005-08-05 00:44:28 -0400849 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400850 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
853static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
854{
855 return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
856}
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
859{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200860 struct acpi_thermal *tz = seq->private;
Thomas Renninger68ccfaa2006-11-19 23:01:40 +0100861 struct acpi_device *device;
Thomas Renningere7c746e2007-06-18 00:40:51 -0400862 acpi_status status;
863
Len Brown4be44fc2005-08-05 00:44:28 -0400864 int i = 0;
865 int j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 if (!tz)
869 goto end;
870
871 if (tz->trips.critical.flags.valid)
Len Brownf5487142007-08-12 00:12:44 -0400872 seq_printf(seq, "critical (S5): %ld C%s",
873 KELVIN_TO_CELSIUS(tz->trips.critical.temperature),
874 nocrt ? " <disabled>\n" : "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 if (tz->trips.hot.flags.valid)
Len Brownf5487142007-08-12 00:12:44 -0400877 seq_printf(seq, "hot (S4): %ld C%s",
878 KELVIN_TO_CELSIUS(tz->trips.hot.temperature),
879 nocrt ? " <disabled>\n" : "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 if (tz->trips.passive.flags.valid) {
Len Brown4be44fc2005-08-05 00:44:28 -0400882 seq_printf(seq,
883 "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
884 KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
885 tz->trips.passive.tc1, tz->trips.passive.tc2,
886 tz->trips.passive.tsp);
887 for (j = 0; j < tz->trips.passive.devices.count; j++) {
Thomas Renningere7c746e2007-06-18 00:40:51 -0400888 status = acpi_bus_get_device(tz->trips.passive.devices.
889 handles[j], &device);
890 seq_printf(seq, "%4.4s ", status ? "" :
891 acpi_device_bid(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
893 seq_puts(seq, "\n");
894 }
895
896 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
897 if (!(tz->trips.active[i].flags.valid))
898 break;
899 seq_printf(seq, "active[%d]: %ld C: devices=",
Len Brown4be44fc2005-08-05 00:44:28 -0400900 i,
901 KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
Thomas Renninger68ccfaa2006-11-19 23:01:40 +0100902 for (j = 0; j < tz->trips.active[i].devices.count; j++){
Thomas Renningere7c746e2007-06-18 00:40:51 -0400903 status = acpi_bus_get_device(tz->trips.active[i].
904 devices.handles[j],
905 &device);
906 seq_printf(seq, "%4.4s ", status ? "" :
907 acpi_device_bid(device));
Thomas Renninger68ccfaa2006-11-19 23:01:40 +0100908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 seq_puts(seq, "\n");
910 }
911
Len Brown4be44fc2005-08-05 00:44:28 -0400912 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400913 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
916static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
917{
918 return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
919}
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
922{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200923 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 if (!tz)
927 goto end;
928
Len Browneaca2d32007-04-30 23:27:43 -0400929 if (!tz->flags.cooling_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 seq_puts(seq, "<setting not supported>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 else
Len Browneaca2d32007-04-30 23:27:43 -0400932 seq_puts(seq, "0 - Active; 1 - Passive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Len Brown4be44fc2005-08-05 00:44:28 -0400934 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400935 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937
938static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
939{
940 return single_open(file, acpi_thermal_cooling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -0400941 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
944static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -0400945acpi_thermal_write_cooling_mode(struct file *file,
946 const char __user * buffer,
947 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200949 struct seq_file *m = file->private_data;
950 struct acpi_thermal *tz = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400951 int result = 0;
952 char mode_string[12] = { '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 if (!tz || (count > sizeof(mode_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400956 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 if (!tz->flags.cooling_mode)
Patrick Mocheld550d982006-06-27 00:41:40 -0400959 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 if (copy_from_user(mode_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -0400962 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -0400963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 mode_string[count] = '\0';
Len Brown4be44fc2005-08-05 00:44:28 -0400965
966 result = acpi_thermal_set_cooling_mode(tz,
967 simple_strtoul(mode_string, NULL,
968 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400970 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 acpi_thermal_check(tz);
973
Patrick Mocheld550d982006-06-27 00:41:40 -0400974 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
978{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200979 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 if (!tz)
983 goto end;
984
985 if (!tz->polling_frequency) {
986 seq_puts(seq, "<polling disabled>\n");
987 goto end;
988 }
989
990 seq_printf(seq, "polling frequency: %lu seconds\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400991 (tz->polling_frequency / 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Len Brown4be44fc2005-08-05 00:44:28 -0400993 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400994 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
996
997static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
998{
999 return single_open(file, acpi_thermal_polling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001000 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001}
1002
1003static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001004acpi_thermal_write_polling(struct file *file,
1005 const char __user * buffer,
1006 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001008 struct seq_file *m = file->private_data;
1009 struct acpi_thermal *tz = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001010 int result = 0;
1011 char polling_string[12] = { '\0' };
1012 int seconds = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 if (!tz || (count > sizeof(polling_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001016 return -EINVAL;
Len Brown4be44fc2005-08-05 00:44:28 -04001017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (copy_from_user(polling_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001019 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -04001020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 polling_string[count] = '\0';
1022
1023 seconds = simple_strtoul(polling_string, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 result = acpi_thermal_set_polling(tz, seconds);
1026 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001027 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 acpi_thermal_check(tz);
1030
Patrick Mocheld550d982006-06-27 00:41:40 -04001031 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032}
1033
Len Brown4be44fc2005-08-05 00:44:28 -04001034static int acpi_thermal_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
Len Brown4be44fc2005-08-05 00:44:28 -04001036 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 if (!acpi_device_dir(device)) {
1040 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -04001041 acpi_thermal_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001043 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 acpi_device_dir(device)->owner = THIS_MODULE;
1045 }
1046
1047 /* 'state' [R] */
1048 entry = create_proc_entry(ACPI_THERMAL_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -04001049 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001051 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 else {
1053 entry->proc_fops = &acpi_thermal_state_fops;
1054 entry->data = acpi_driver_data(device);
1055 entry->owner = THIS_MODULE;
1056 }
1057
1058 /* 'temperature' [R] */
1059 entry = create_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
Len Brown4be44fc2005-08-05 00:44:28 -04001060 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001062 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 else {
1064 entry->proc_fops = &acpi_thermal_temp_fops;
1065 entry->data = acpi_driver_data(device);
1066 entry->owner = THIS_MODULE;
1067 }
1068
1069 /* 'trip_points' [R/W] */
1070 entry = create_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
Len Brown4be44fc2005-08-05 00:44:28 -04001071 S_IFREG | S_IRUGO | S_IWUSR,
1072 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001074 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 else {
1076 entry->proc_fops = &acpi_thermal_trip_fops;
1077 entry->data = acpi_driver_data(device);
1078 entry->owner = THIS_MODULE;
1079 }
1080
1081 /* 'cooling_mode' [R/W] */
1082 entry = create_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
Len Brown4be44fc2005-08-05 00:44:28 -04001083 S_IFREG | S_IRUGO | S_IWUSR,
1084 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001086 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 else {
1088 entry->proc_fops = &acpi_thermal_cooling_fops;
1089 entry->data = acpi_driver_data(device);
1090 entry->owner = THIS_MODULE;
1091 }
1092
1093 /* 'polling_frequency' [R/W] */
1094 entry = create_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
Len Brown4be44fc2005-08-05 00:44:28 -04001095 S_IFREG | S_IRUGO | S_IWUSR,
1096 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001098 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 else {
1100 entry->proc_fops = &acpi_thermal_polling_fops;
1101 entry->data = acpi_driver_data(device);
1102 entry->owner = THIS_MODULE;
1103 }
1104
Patrick Mocheld550d982006-06-27 00:41:40 -04001105 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
1107
Len Brown4be44fc2005-08-05 00:44:28 -04001108static int acpi_thermal_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 if (acpi_device_dir(device)) {
1112 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1113 acpi_device_dir(device));
1114 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1115 acpi_device_dir(device));
1116 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1117 acpi_device_dir(device));
1118 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1119 acpi_device_dir(device));
1120 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1121 acpi_device_dir(device));
1122 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1123 acpi_device_dir(device) = NULL;
1124 }
1125
Patrick Mocheld550d982006-06-27 00:41:40 -04001126 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129/* --------------------------------------------------------------------------
1130 Driver Interface
1131 -------------------------------------------------------------------------- */
1132
Len Brown4be44fc2005-08-05 00:44:28 -04001133static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001135 struct acpi_thermal *tz = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001136 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001140 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Patrick Mochel8348e1b2006-05-19 16:54:40 -04001142 device = tz->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 switch (event) {
1145 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1146 acpi_thermal_check(tz);
1147 break;
1148 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
1149 acpi_thermal_get_trip_points(tz);
1150 acpi_thermal_check(tz);
1151 acpi_bus_generate_event(device, event, 0);
1152 break;
1153 case ACPI_THERMAL_NOTIFY_DEVICES:
1154 if (tz->flags.devices)
1155 acpi_thermal_get_devices(tz);
1156 acpi_bus_generate_event(device, event, 0);
1157 break;
1158 default:
1159 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001160 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 break;
1162 }
1163
Patrick Mocheld550d982006-06-27 00:41:40 -04001164 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166
Len Brown4be44fc2005-08-05 00:44:28 -04001167static int acpi_thermal_get_info(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
Len Brown4be44fc2005-08-05 00:44:28 -04001169 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001173 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 /* Get temperature [_TMP] (required) */
1176 result = acpi_thermal_get_temperature(tz);
1177 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001178 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180 /* Get trip points [_CRT, _PSV, etc.] (required) */
1181 result = acpi_thermal_get_trip_points(tz);
1182 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001183 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 /* Set the cooling mode [_SCP] to active cooling (default) */
1186 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
Len Brown4be44fc2005-08-05 00:44:28 -04001187 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 tz->flags.cooling_mode = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 /* Get default polling frequency [_TZP] (optional) */
1191 if (tzp)
1192 tz->polling_frequency = tzp;
1193 else
1194 acpi_thermal_get_polling_frequency(tz);
1195
1196 /* Get devices in this thermal zone [_TZD] (optional) */
1197 result = acpi_thermal_get_devices(tz);
1198 if (!result)
1199 tz->flags.devices = 1;
1200
Patrick Mocheld550d982006-06-27 00:41:40 -04001201 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
1203
Len Brown4be44fc2005-08-05 00:44:28 -04001204static int acpi_thermal_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
Len Brown4be44fc2005-08-05 00:44:28 -04001206 int result = 0;
1207 acpi_status status = AE_OK;
1208 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001212 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Burman Yan36bcbec2006-12-19 12:56:11 -08001214 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001216 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Patrick Mochel8348e1b2006-05-19 16:54:40 -04001218 tz->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 strcpy(tz->name, device->pnp.bus_id);
1220 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1221 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1222 acpi_driver_data(device) = tz;
1223
1224 result = acpi_thermal_get_info(tz);
1225 if (result)
1226 goto end;
1227
1228 result = acpi_thermal_add_fs(device);
1229 if (result)
Vasily Averin09047e72006-04-27 05:25:00 -04001230 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 init_timer(&tz->timer);
1233
1234 acpi_thermal_check(tz);
1235
Patrick Mochel38ba7c92006-05-19 16:54:48 -04001236 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001237 ACPI_DEVICE_NOTIFY,
1238 acpi_thermal_notify, tz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 result = -ENODEV;
1241 goto end;
1242 }
1243
1244 printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001245 acpi_device_name(device), acpi_device_bid(device),
1246 KELVIN_TO_CELSIUS(tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Len Brown4be44fc2005-08-05 00:44:28 -04001248 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (result) {
1250 acpi_thermal_remove_fs(device);
1251 kfree(tz);
1252 }
1253
Patrick Mocheld550d982006-06-27 00:41:40 -04001254 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
1256
Len Brown4be44fc2005-08-05 00:44:28 -04001257static int acpi_thermal_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
Len Brown4be44fc2005-08-05 00:44:28 -04001259 acpi_status status = AE_OK;
1260 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001264 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001266 tz = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 /* avoid timer adding new defer task */
1269 tz->zombie = 1;
1270 /* wait for running timer (on other CPUs) finish */
1271 del_timer_sync(&(tz->timer));
1272 /* synchronize deferred task */
1273 acpi_os_wait_events_complete(NULL);
1274 /* deferred task may reinsert timer */
1275 del_timer_sync(&(tz->timer));
1276
Patrick Mochel38ba7c92006-05-19 16:54:48 -04001277 status = acpi_remove_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001278 ACPI_DEVICE_NOTIFY,
1279 acpi_thermal_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 /* Terminate policy */
Len Brown4be44fc2005-08-05 00:44:28 -04001282 if (tz->trips.passive.flags.valid && tz->trips.passive.flags.enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 tz->trips.passive.flags.enabled = 0;
1284 acpi_thermal_passive(tz);
1285 }
1286 if (tz->trips.active[0].flags.valid
Len Brown4be44fc2005-08-05 00:44:28 -04001287 && tz->trips.active[0].flags.enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 tz->trips.active[0].flags.enabled = 0;
1289 acpi_thermal_active(tz);
1290 }
1291
1292 acpi_thermal_remove_fs(device);
1293
1294 kfree(tz);
Patrick Mocheld550d982006-06-27 00:41:40 -04001295 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001298static int acpi_thermal_resume(struct acpi_device *device)
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001299{
1300 struct acpi_thermal *tz = NULL;
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001301 int i, j, power_state, result;
1302
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001303
1304 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001305 return -EINVAL;
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001306
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001307 tz = acpi_driver_data(device);
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001308
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001309 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001310 if (!(&tz->trips.active[i]))
1311 break;
1312 if (!tz->trips.active[i].flags.valid)
1313 break;
1314 tz->trips.active[i].flags.enabled = 1;
1315 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1316 result = acpi_bus_get_power(tz->trips.active[i].devices.
1317 handles[j], &power_state);
1318 if (result || (power_state != ACPI_STATE_D0)) {
1319 tz->trips.active[i].flags.enabled = 0;
1320 break;
1321 }
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001322 }
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001323 tz->state.active |= tz->trips.active[i].flags.enabled;
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001324 }
1325
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001326 acpi_thermal_check(tz);
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001327
1328 return AE_OK;
1329}
1330
Len Brown4be44fc2005-08-05 00:44:28 -04001331static int __init acpi_thermal_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Len Brown4be44fc2005-08-05 00:44:28 -04001333 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Len Brown72b33ef2007-08-12 00:12:17 -04001335 if (off) {
1336 printk(KERN_NOTICE "ACPI: thermal control disabled\n");
1337 return -ENODEV;
1338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1340 if (!acpi_thermal_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001341 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 acpi_thermal_dir->owner = THIS_MODULE;
1343
1344 result = acpi_bus_register_driver(&acpi_thermal_driver);
1345 if (result < 0) {
1346 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04001347 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
1349
Patrick Mocheld550d982006-06-27 00:41:40 -04001350 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351}
1352
Len Brown4be44fc2005-08-05 00:44:28 -04001353static void __exit acpi_thermal_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 acpi_bus_unregister_driver(&acpi_thermal_driver);
1357
1358 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1359
Patrick Mocheld550d982006-06-27 00:41:40 -04001360 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363module_init(acpi_thermal_init);
1364module_exit(acpi_thermal_exit);