blob: d0b44ce2ec5a5a25034d21f754f9bbc5e2a358d6 [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>
39#include <linux/sched.h>
40#include <linux/kmod.h>
41#include <linux/seq_file.h>
42#include <asm/uaccess.h>
43
44#include <acpi/acpi_bus.h>
45#include <acpi/acpi_drivers.h>
46
47#define ACPI_THERMAL_COMPONENT 0x04000000
48#define ACPI_THERMAL_CLASS "thermal_zone"
49#define ACPI_THERMAL_DRIVER_NAME "ACPI Thermal Zone Driver"
50#define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
51#define ACPI_THERMAL_FILE_STATE "state"
52#define ACPI_THERMAL_FILE_TEMPERATURE "temperature"
53#define ACPI_THERMAL_FILE_TRIP_POINTS "trip_points"
54#define ACPI_THERMAL_FILE_COOLING_MODE "cooling_mode"
55#define ACPI_THERMAL_FILE_POLLING_FREQ "polling_frequency"
56#define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
57#define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
58#define ACPI_THERMAL_NOTIFY_DEVICES 0x82
59#define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
60#define ACPI_THERMAL_NOTIFY_HOT 0xF1
61#define ACPI_THERMAL_MODE_ACTIVE 0x00
62#define ACPI_THERMAL_MODE_PASSIVE 0x01
63#define ACPI_THERMAL_MODE_CRITICAL 0xff
64#define ACPI_THERMAL_PATH_POWEROFF "/sbin/poweroff"
65
66#define ACPI_THERMAL_MAX_ACTIVE 10
67#define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
68
69#define KELVIN_TO_CELSIUS(t) (long)(((long)t-2732>=0) ? ((long)t-2732+5)/10 : ((long)t-2732-5)/10)
70#define CELSIUS_TO_KELVIN(t) ((t+273)*10)
71
72#define _COMPONENT ACPI_THERMAL_COMPONENT
Len Brown4be44fc2005-08-05 00:44:28 -040073ACPI_MODULE_NAME("acpi_thermal")
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Thomas Renninger1cbf4c52004-09-16 11:07:00 -040075MODULE_AUTHOR("Paul Diefenbaugh");
Linus Torvalds1da177e2005-04-16 15:20:36 -070076MODULE_DESCRIPTION(ACPI_THERMAL_DRIVER_NAME);
77MODULE_LICENSE("GPL");
78
79static int tzp;
80module_param(tzp, int, 0);
81MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.\n");
82
Len Brown4be44fc2005-08-05 00:44:28 -040083static int acpi_thermal_add(struct acpi_device *device);
84static int acpi_thermal_remove(struct acpi_device *device, int type);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
86static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
87static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -040088static ssize_t acpi_thermal_write_trip_points(struct file *,
89 const char __user *, size_t,
90 loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -040092static ssize_t acpi_thermal_write_cooling_mode(struct file *,
93 const char __user *, size_t,
94 loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -040096static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
97 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99static struct acpi_driver acpi_thermal_driver = {
Len Brown4be44fc2005-08-05 00:44:28 -0400100 .name = ACPI_THERMAL_DRIVER_NAME,
101 .class = ACPI_THERMAL_CLASS,
102 .ids = ACPI_THERMAL_HID,
103 .ops = {
104 .add = acpi_thermal_add,
105 .remove = acpi_thermal_remove,
106 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107};
108
109struct acpi_thermal_state {
Len Brown4be44fc2005-08-05 00:44:28 -0400110 u8 critical:1;
111 u8 hot:1;
112 u8 passive:1;
113 u8 active:1;
114 u8 reserved:4;
115 int active_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116};
117
118struct acpi_thermal_state_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400119 u8 valid:1;
120 u8 enabled:1;
121 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122};
123
124struct acpi_thermal_critical {
125 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400126 unsigned long temperature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
129struct acpi_thermal_hot {
130 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400131 unsigned long temperature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132};
133
134struct acpi_thermal_passive {
135 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400136 unsigned long temperature;
137 unsigned long tc1;
138 unsigned long tc2;
139 unsigned long tsp;
140 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
143struct acpi_thermal_active {
144 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400145 unsigned long temperature;
146 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147};
148
149struct acpi_thermal_trips {
150 struct acpi_thermal_critical critical;
Len Brown4be44fc2005-08-05 00:44:28 -0400151 struct acpi_thermal_hot hot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 struct acpi_thermal_passive passive;
153 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
154};
155
156struct acpi_thermal_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400157 u8 cooling_mode:1; /* _SCP */
158 u8 devices:1; /* _TZD */
159 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160};
161
162struct acpi_thermal {
Len Brown4be44fc2005-08-05 00:44:28 -0400163 acpi_handle handle;
164 acpi_bus_id name;
165 unsigned long temperature;
166 unsigned long last_temperature;
167 unsigned long polling_frequency;
168 u8 cooling_mode;
169 volatile u8 zombie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 struct acpi_thermal_flags flags;
171 struct acpi_thermal_state state;
172 struct acpi_thermal_trips trips;
Len Brown4be44fc2005-08-05 00:44:28 -0400173 struct acpi_handle_list devices;
174 struct timer_list timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175};
176
177static struct file_operations acpi_thermal_state_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400178 .open = acpi_thermal_state_open_fs,
179 .read = seq_read,
180 .llseek = seq_lseek,
181 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182};
183
184static struct file_operations acpi_thermal_temp_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400185 .open = acpi_thermal_temp_open_fs,
186 .read = seq_read,
187 .llseek = seq_lseek,
188 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189};
190
191static struct file_operations acpi_thermal_trip_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400192 .open = acpi_thermal_trip_open_fs,
193 .read = seq_read,
194 .write = acpi_thermal_write_trip_points,
195 .llseek = seq_lseek,
196 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197};
198
199static struct file_operations acpi_thermal_cooling_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400200 .open = acpi_thermal_cooling_open_fs,
201 .read = seq_read,
202 .write = acpi_thermal_write_cooling_mode,
203 .llseek = seq_lseek,
204 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205};
206
207static struct file_operations acpi_thermal_polling_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400208 .open = acpi_thermal_polling_open_fs,
209 .read = seq_read,
210 .write = acpi_thermal_write_polling,
211 .llseek = seq_lseek,
212 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213};
214
215/* --------------------------------------------------------------------------
216 Thermal Zone Management
217 -------------------------------------------------------------------------- */
218
Len Brown4be44fc2005-08-05 00:44:28 -0400219static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Len Brown4be44fc2005-08-05 00:44:28 -0400221 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 ACPI_FUNCTION_TRACE("acpi_thermal_get_temperature");
224
225 if (!tz)
226 return_VALUE(-EINVAL);
227
228 tz->last_temperature = tz->temperature;
229
Len Brown4be44fc2005-08-05 00:44:28 -0400230 status =
231 acpi_evaluate_integer(tz->handle, "_TMP", NULL, &tz->temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (ACPI_FAILURE(status))
233 return_VALUE(-ENODEV);
234
Len Brown4be44fc2005-08-05 00:44:28 -0400235 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
236 tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 return_VALUE(0);
239}
240
Len Brown4be44fc2005-08-05 00:44:28 -0400241static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Len Brown4be44fc2005-08-05 00:44:28 -0400243 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 ACPI_FUNCTION_TRACE("acpi_thermal_get_polling_frequency");
246
247 if (!tz)
248 return_VALUE(-EINVAL);
249
Len Brown4be44fc2005-08-05 00:44:28 -0400250 status =
251 acpi_evaluate_integer(tz->handle, "_TZP", NULL,
252 &tz->polling_frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (ACPI_FAILURE(status))
254 return_VALUE(-ENODEV);
255
Len Brown4be44fc2005-08-05 00:44:28 -0400256 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
257 tz->polling_frequency));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 return_VALUE(0);
260}
261
Len Brown4be44fc2005-08-05 00:44:28 -0400262static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 ACPI_FUNCTION_TRACE("acpi_thermal_set_polling");
265
266 if (!tz)
267 return_VALUE(-EINVAL);
268
269 tz->polling_frequency = seconds * 10; /* Convert value to deci-seconds */
270
Len Brown4be44fc2005-08-05 00:44:28 -0400271 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
272 "Polling frequency set to %lu seconds\n",
273 tz->polling_frequency));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 return_VALUE(0);
276}
277
Len Brown4be44fc2005-08-05 00:44:28 -0400278static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Len Brown4be44fc2005-08-05 00:44:28 -0400280 acpi_status status = AE_OK;
281 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
282 struct acpi_object_list arg_list = { 1, &arg0 };
283 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 ACPI_FUNCTION_TRACE("acpi_thermal_set_cooling_mode");
286
287 if (!tz)
288 return_VALUE(-EINVAL);
289
290 status = acpi_get_handle(tz->handle, "_SCP", &handle);
291 if (ACPI_FAILURE(status)) {
292 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
293 return_VALUE(-ENODEV);
294 }
295
296 arg0.integer.value = mode;
297
298 status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
299 if (ACPI_FAILURE(status))
300 return_VALUE(-ENODEV);
301
302 tz->cooling_mode = mode;
303
Len Brown4be44fc2005-08-05 00:44:28 -0400304 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Cooling mode [%s]\n",
305 mode ? "passive" : "active"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 return_VALUE(0);
308}
309
Len Brown4be44fc2005-08-05 00:44:28 -0400310static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Len Brown4be44fc2005-08-05 00:44:28 -0400312 acpi_status status = AE_OK;
313 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 ACPI_FUNCTION_TRACE("acpi_thermal_get_trip_points");
316
317 if (!tz)
318 return_VALUE(-EINVAL);
319
320 /* Critical Shutdown (required) */
321
Len Brown4be44fc2005-08-05 00:44:28 -0400322 status = acpi_evaluate_integer(tz->handle, "_CRT", NULL,
323 &tz->trips.critical.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (ACPI_FAILURE(status)) {
325 tz->trips.critical.flags.valid = 0;
326 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "No critical threshold\n"));
327 return_VALUE(-ENODEV);
Len Brown4be44fc2005-08-05 00:44:28 -0400328 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 tz->trips.critical.flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400330 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
331 "Found critical threshold [%lu]\n",
332 tz->trips.critical.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
334
335 /* Critical Sleep (optional) */
336
Len Brown4be44fc2005-08-05 00:44:28 -0400337 status =
338 acpi_evaluate_integer(tz->handle, "_HOT", NULL,
339 &tz->trips.hot.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (ACPI_FAILURE(status)) {
341 tz->trips.hot.flags.valid = 0;
342 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No hot threshold\n"));
Len Brown4be44fc2005-08-05 00:44:28 -0400343 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 tz->trips.hot.flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400345 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found hot threshold [%lu]\n",
346 tz->trips.hot.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348
349 /* Passive: Processors (optional) */
350
Len Brown4be44fc2005-08-05 00:44:28 -0400351 status =
352 acpi_evaluate_integer(tz->handle, "_PSV", NULL,
353 &tz->trips.passive.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (ACPI_FAILURE(status)) {
355 tz->trips.passive.flags.valid = 0;
356 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No passive threshold\n"));
Len Brown4be44fc2005-08-05 00:44:28 -0400357 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 tz->trips.passive.flags.valid = 1;
359
Len Brown4be44fc2005-08-05 00:44:28 -0400360 status =
361 acpi_evaluate_integer(tz->handle, "_TC1", NULL,
362 &tz->trips.passive.tc1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (ACPI_FAILURE(status))
364 tz->trips.passive.flags.valid = 0;
365
Len Brown4be44fc2005-08-05 00:44:28 -0400366 status =
367 acpi_evaluate_integer(tz->handle, "_TC2", NULL,
368 &tz->trips.passive.tc2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (ACPI_FAILURE(status))
370 tz->trips.passive.flags.valid = 0;
371
Len Brown4be44fc2005-08-05 00:44:28 -0400372 status =
373 acpi_evaluate_integer(tz->handle, "_TSP", NULL,
374 &tz->trips.passive.tsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (ACPI_FAILURE(status))
376 tz->trips.passive.flags.valid = 0;
377
Len Brown4be44fc2005-08-05 00:44:28 -0400378 status =
379 acpi_evaluate_reference(tz->handle, "_PSL", NULL,
380 &tz->trips.passive.devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (ACPI_FAILURE(status))
382 tz->trips.passive.flags.valid = 0;
383
384 if (!tz->trips.passive.flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400385 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
386 "Invalid passive threshold\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 else
Len Brown4be44fc2005-08-05 00:44:28 -0400388 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
389 "Found passive threshold [%lu]\n",
390 tz->trips.passive.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392
393 /* Active: Fans, etc. (optional) */
394
Len Brown4be44fc2005-08-05 00:44:28 -0400395 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Len Brown4be44fc2005-08-05 00:44:28 -0400397 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Len Brown4be44fc2005-08-05 00:44:28 -0400399 status =
400 acpi_evaluate_integer(tz->handle, name, NULL,
401 &tz->trips.active[i].temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (ACPI_FAILURE(status))
403 break;
404
405 name[2] = 'L';
Len Brown4be44fc2005-08-05 00:44:28 -0400406 status =
407 acpi_evaluate_reference(tz->handle, name, NULL,
408 &tz->trips.active[i].devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (ACPI_SUCCESS(status)) {
410 tz->trips.active[i].flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400411 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
412 "Found active threshold [%d]:[%lu]\n",
413 i, tz->trips.active[i].temperature));
414 } else
415 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
416 "Invalid active threshold [%d]\n",
417 i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419
420 return_VALUE(0);
421}
422
Len Brown4be44fc2005-08-05 00:44:28 -0400423static int acpi_thermal_get_devices(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Len Brown4be44fc2005-08-05 00:44:28 -0400425 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 ACPI_FUNCTION_TRACE("acpi_thermal_get_devices");
428
429 if (!tz)
430 return_VALUE(-EINVAL);
431
Len Brown4be44fc2005-08-05 00:44:28 -0400432 status =
433 acpi_evaluate_reference(tz->handle, "_TZD", NULL, &tz->devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (ACPI_FAILURE(status))
435 return_VALUE(-ENODEV);
436
437 return_VALUE(0);
438}
439
Len Brown4be44fc2005-08-05 00:44:28 -0400440static int acpi_thermal_call_usermode(char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Len Brown4be44fc2005-08-05 00:44:28 -0400442 char *argv[2] = { NULL, NULL };
443 char *envp[3] = { NULL, NULL, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 ACPI_FUNCTION_TRACE("acpi_thermal_call_usermode");
446
447 if (!path)
448 return_VALUE(-EINVAL);
449
450 argv[0] = path;
451
452 /* minimal command environment */
453 envp[0] = "HOME=/";
454 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
Len Brown4be44fc2005-08-05 00:44:28 -0400455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 call_usermodehelper(argv[0], argv, envp, 0);
457
458 return_VALUE(0);
459}
460
Len Brown4be44fc2005-08-05 00:44:28 -0400461static int acpi_thermal_critical(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Len Brown4be44fc2005-08-05 00:44:28 -0400463 int result = 0;
464 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 ACPI_FUNCTION_TRACE("acpi_thermal_critical");
467
468 if (!tz || !tz->trips.critical.flags.valid)
469 return_VALUE(-EINVAL);
470
471 if (tz->temperature >= tz->trips.critical.temperature) {
472 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Critical trip point\n"));
473 tz->trips.critical.flags.enabled = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400474 } else if (tz->trips.critical.flags.enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 tz->trips.critical.flags.enabled = 0;
476
477 result = acpi_bus_get_device(tz->handle, &device);
478 if (result)
479 return_VALUE(result);
480
Len Brown4be44fc2005-08-05 00:44:28 -0400481 printk(KERN_EMERG
482 "Critical temperature reached (%ld C), shutting down.\n",
483 KELVIN_TO_CELSIUS(tz->temperature));
484 acpi_bus_generate_event(device, ACPI_THERMAL_NOTIFY_CRITICAL,
485 tz->trips.critical.flags.enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 acpi_thermal_call_usermode(ACPI_THERMAL_PATH_POWEROFF);
488
489 return_VALUE(0);
490}
491
Len Brown4be44fc2005-08-05 00:44:28 -0400492static int acpi_thermal_hot(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Len Brown4be44fc2005-08-05 00:44:28 -0400494 int result = 0;
495 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 ACPI_FUNCTION_TRACE("acpi_thermal_hot");
498
499 if (!tz || !tz->trips.hot.flags.valid)
500 return_VALUE(-EINVAL);
501
502 if (tz->temperature >= tz->trips.hot.temperature) {
503 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Hot trip point\n"));
504 tz->trips.hot.flags.enabled = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400505 } else if (tz->trips.hot.flags.enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 tz->trips.hot.flags.enabled = 0;
507
508 result = acpi_bus_get_device(tz->handle, &device);
509 if (result)
510 return_VALUE(result);
511
Len Brown4be44fc2005-08-05 00:44:28 -0400512 acpi_bus_generate_event(device, ACPI_THERMAL_NOTIFY_HOT,
513 tz->trips.hot.flags.enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 /* TBD: Call user-mode "sleep(S4)" function */
516
517 return_VALUE(0);
518}
519
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400520static void acpi_thermal_passive(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400522 int result = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 struct acpi_thermal_passive *passive = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400524 int trend = 0;
525 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 ACPI_FUNCTION_TRACE("acpi_thermal_passive");
528
529 if (!tz || !tz->trips.passive.flags.valid)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400530 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 passive = &(tz->trips.passive);
533
534 /*
535 * Above Trip?
536 * -----------
537 * Calculate the thermal trend (using the passive cooling equation)
538 * and modify the performance limit for all passive cooling devices
539 * accordingly. Note that we assume symmetry.
540 */
541 if (tz->temperature >= passive->temperature) {
Len Brown4be44fc2005-08-05 00:44:28 -0400542 trend =
543 (passive->tc1 * (tz->temperature - tz->last_temperature)) +
544 (passive->tc2 * (tz->temperature - passive->temperature));
545 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
546 "trend[%d]=(tc1[%lu]*(tmp[%lu]-last[%lu]))+(tc2[%lu]*(tmp[%lu]-psv[%lu]))\n",
547 trend, passive->tc1, tz->temperature,
548 tz->last_temperature, passive->tc2,
549 tz->temperature, passive->temperature));
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400550 passive->flags.enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /* Heating up? */
552 if (trend > 0)
Len Brown4be44fc2005-08-05 00:44:28 -0400553 for (i = 0; i < passive->devices.count; i++)
554 acpi_processor_set_thermal_limit(passive->
555 devices.
556 handles[i],
557 ACPI_PROCESSOR_LIMIT_INCREMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 /* Cooling off? */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400559 else if (trend < 0) {
Len Brown4be44fc2005-08-05 00:44:28 -0400560 for (i = 0; i < passive->devices.count; i++)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400561 /*
562 * assume that we are on highest
563 * freq/lowest thrott and can leave
564 * passive mode, even in error case
565 */
566 if (!acpi_processor_set_thermal_limit
567 (passive->devices.handles[i],
568 ACPI_PROCESSOR_LIMIT_DECREMENT))
569 result = 0;
570 /*
571 * Leave cooling mode, even if the temp might
572 * higher than trip point This is because some
573 * machines might have long thermal polling
574 * frequencies (tsp) defined. We will fall back
575 * into passive mode in next cycle (probably quicker)
576 */
577 if (result) {
578 passive->flags.enabled = 0;
579 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
580 "Disabling passive cooling, still above threshold,"
581 " but we are cooling down\n"));
582 }
583 }
584 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
586
587 /*
588 * Below Trip?
589 * -----------
590 * Implement passive cooling hysteresis to slowly increase performance
591 * and avoid thrashing around the passive trip point. Note that we
592 * assume symmetry.
593 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400594 if (!passive->flags.enabled)
595 return;
596 for (i = 0; i < passive->devices.count; i++)
597 if (!acpi_processor_set_thermal_limit
598 (passive->devices.handles[i],
599 ACPI_PROCESSOR_LIMIT_DECREMENT))
600 result = 0;
601 if (result) {
602 passive->flags.enabled = 0;
603 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
604 "Disabling passive cooling (zone is cool)\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400608static void acpi_thermal_active(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Len Brown4be44fc2005-08-05 00:44:28 -0400610 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 struct acpi_thermal_active *active = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400612 int i = 0;
613 int j = 0;
614 unsigned long maxtemp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 ACPI_FUNCTION_TRACE("acpi_thermal_active");
617
618 if (!tz)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400619 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Len Brown4be44fc2005-08-05 00:44:28 -0400621 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 active = &(tz->trips.active[i]);
623 if (!active || !active->flags.valid)
624 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (tz->temperature >= active->temperature) {
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400626 /*
627 * Above Threshold?
628 * ----------------
629 * If not already enabled, turn ON all cooling devices
630 * associated with this active threshold.
631 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (active->temperature > maxtemp)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400633 tz->state.active_index = i;
634 maxtemp = active->temperature;
635 if (active->flags.enabled)
636 continue;
637 for (j = 0; j < active->devices.count; j++) {
638 result =
639 acpi_bus_set_power(active->devices.
640 handles[j],
641 ACPI_STATE_D0);
642 if (result) {
643 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
644 "Unable to turn cooling device [%p] 'on'\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400645 active->devices.
646 handles[j]));
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400647 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400649 active->flags.enabled = 1;
650 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
651 "Cooling device [%p] now 'on'\n",
652 active->devices.handles[j]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400654 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400656 if (!active->flags.enabled)
657 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 /*
659 * Below Threshold?
660 * ----------------
661 * Turn OFF all cooling devices associated with this
662 * threshold.
663 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400664 for (j = 0; j < active->devices.count; j++) {
665 result = acpi_bus_set_power(active->devices.handles[j],
666 ACPI_STATE_D3);
667 if (result) {
668 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
669 "Unable to turn cooling device [%p] 'off'\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400670 active->devices.handles[j]));
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400671 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400673 active->flags.enabled = 0;
674 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
675 "Cooling device [%p] now 'off'\n",
676 active->devices.handles[j]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
Len Brown4be44fc2005-08-05 00:44:28 -0400681static void acpi_thermal_check(void *context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Len Brown4be44fc2005-08-05 00:44:28 -0400683static void acpi_thermal_run(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 struct acpi_thermal *tz = (struct acpi_thermal *)data;
686 if (!tz->zombie)
Len Brown4be44fc2005-08-05 00:44:28 -0400687 acpi_os_queue_for_execution(OSD_PRIORITY_GPE,
688 acpi_thermal_check, (void *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
Len Brown4be44fc2005-08-05 00:44:28 -0400691static void acpi_thermal_check(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Len Brown4be44fc2005-08-05 00:44:28 -0400693 int result = 0;
694 struct acpi_thermal *tz = (struct acpi_thermal *)data;
695 unsigned long sleep_time = 0;
696 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 struct acpi_thermal_state state;
698
699 ACPI_FUNCTION_TRACE("acpi_thermal_check");
700
701 if (!tz) {
702 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid (NULL) context.\n"));
703 return_VOID;
704 }
705
706 state = tz->state;
707
708 result = acpi_thermal_get_temperature(tz);
709 if (result)
710 return_VOID;
Len Brown4be44fc2005-08-05 00:44:28 -0400711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 memset(&tz->state, 0, sizeof(tz->state));
Len Brown4be44fc2005-08-05 00:44:28 -0400713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 /*
715 * Check Trip Points
716 * -----------------
717 * Compare the current temperature to the trip point values to see
718 * if we've entered one of the thermal policy states. Note that
719 * this function determines when a state is entered, but the
720 * individual policy decides when it is exited (e.g. hysteresis).
721 */
722 if (tz->trips.critical.flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400723 state.critical |=
724 (tz->temperature >= tz->trips.critical.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (tz->trips.hot.flags.valid)
726 state.hot |= (tz->temperature >= tz->trips.hot.temperature);
727 if (tz->trips.passive.flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400728 state.passive |=
729 (tz->temperature >= tz->trips.passive.temperature);
730 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (tz->trips.active[i].flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400732 state.active |=
733 (tz->temperature >=
734 tz->trips.active[i].temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 /*
737 * Invoke Policy
738 * -------------
739 * Separated from the above check to allow individual policy to
740 * determine when to exit a given state.
741 */
742 if (state.critical)
743 acpi_thermal_critical(tz);
744 if (state.hot)
745 acpi_thermal_hot(tz);
746 if (state.passive)
747 acpi_thermal_passive(tz);
748 if (state.active)
749 acpi_thermal_active(tz);
750
751 /*
752 * Calculate State
753 * ---------------
754 * Again, separated from the above two to allow independent policy
755 * decisions.
756 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400757 tz->state.critical = tz->trips.critical.flags.enabled;
758 tz->state.hot = tz->trips.hot.flags.enabled;
759 tz->state.passive = tz->trips.passive.flags.enabled;
760 tz->state.active = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400761 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400762 tz->state.active |= tz->trips.active[i].flags.enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 /*
765 * Calculate Sleep Time
766 * --------------------
767 * If we're in the passive state, use _TSP's value. Otherwise
768 * use the default polling frequency (e.g. _TZP). If no polling
769 * frequency is specified then we'll wait forever (at least until
770 * a thermal event occurs). Note that _TSP and _TZD values are
771 * given in 1/10th seconds (we must covert to milliseconds).
772 */
773 if (tz->state.passive)
774 sleep_time = tz->trips.passive.tsp * 100;
775 else if (tz->polling_frequency > 0)
776 sleep_time = tz->polling_frequency * 100;
777
Len Brown4be44fc2005-08-05 00:44:28 -0400778 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: temperature[%lu] sleep[%lu]\n",
779 tz->name, tz->temperature, sleep_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 /*
782 * Schedule Next Poll
783 * ------------------
784 */
785 if (!sleep_time) {
786 if (timer_pending(&(tz->timer)))
787 del_timer(&(tz->timer));
Len Brown4be44fc2005-08-05 00:44:28 -0400788 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (timer_pending(&(tz->timer)))
790 mod_timer(&(tz->timer), (HZ * sleep_time) / 1000);
791 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400792 tz->timer.data = (unsigned long)tz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 tz->timer.function = acpi_thermal_run;
794 tz->timer.expires = jiffies + (HZ * sleep_time) / 1000;
795 add_timer(&(tz->timer));
796 }
797 }
798
799 return_VOID;
800}
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802/* --------------------------------------------------------------------------
803 FS Interface (/proc)
804 -------------------------------------------------------------------------- */
805
Len Brown4be44fc2005-08-05 00:44:28 -0400806static struct proc_dir_entry *acpi_thermal_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
809{
Len Brown4be44fc2005-08-05 00:44:28 -0400810 struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 ACPI_FUNCTION_TRACE("acpi_thermal_state_seq_show");
813
814 if (!tz)
815 goto end;
816
817 seq_puts(seq, "state: ");
818
Len Brown4be44fc2005-08-05 00:44:28 -0400819 if (!tz->state.critical && !tz->state.hot && !tz->state.passive
820 && !tz->state.active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 seq_puts(seq, "ok\n");
822 else {
823 if (tz->state.critical)
824 seq_puts(seq, "critical ");
825 if (tz->state.hot)
826 seq_puts(seq, "hot ");
827 if (tz->state.passive)
828 seq_puts(seq, "passive ");
829 if (tz->state.active)
830 seq_printf(seq, "active[%d]", tz->state.active_index);
831 seq_puts(seq, "\n");
832 }
833
Len Brown4be44fc2005-08-05 00:44:28 -0400834 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return_VALUE(0);
836}
837
838static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
839{
840 return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
841}
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
844{
Len Brown4be44fc2005-08-05 00:44:28 -0400845 int result = 0;
846 struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 ACPI_FUNCTION_TRACE("acpi_thermal_temp_seq_show");
849
850 if (!tz)
851 goto end;
852
853 result = acpi_thermal_get_temperature(tz);
854 if (result)
855 goto end;
856
Len Brown4be44fc2005-08-05 00:44:28 -0400857 seq_printf(seq, "temperature: %ld C\n",
858 KELVIN_TO_CELSIUS(tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Len Brown4be44fc2005-08-05 00:44:28 -0400860 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return_VALUE(0);
862}
863
864static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
865{
866 return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
867}
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
870{
Len Brown4be44fc2005-08-05 00:44:28 -0400871 struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
872 int i = 0;
873 int j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 ACPI_FUNCTION_TRACE("acpi_thermal_trip_seq_show");
876
877 if (!tz)
878 goto end;
879
880 if (tz->trips.critical.flags.valid)
881 seq_printf(seq, "critical (S5): %ld C\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400882 KELVIN_TO_CELSIUS(tz->trips.critical.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 if (tz->trips.hot.flags.valid)
885 seq_printf(seq, "hot (S4): %ld C\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400886 KELVIN_TO_CELSIUS(tz->trips.hot.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 if (tz->trips.passive.flags.valid) {
Len Brown4be44fc2005-08-05 00:44:28 -0400889 seq_printf(seq,
890 "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
891 KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
892 tz->trips.passive.tc1, tz->trips.passive.tc2,
893 tz->trips.passive.tsp);
894 for (j = 0; j < tz->trips.passive.devices.count; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Len Brown4be44fc2005-08-05 00:44:28 -0400896 seq_printf(seq, "0x%p ",
897 tz->trips.passive.devices.handles[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
899 seq_puts(seq, "\n");
900 }
901
902 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
903 if (!(tz->trips.active[i].flags.valid))
904 break;
905 seq_printf(seq, "active[%d]: %ld C: devices=",
Len Brown4be44fc2005-08-05 00:44:28 -0400906 i,
907 KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
908 for (j = 0; j < tz->trips.active[i].devices.count; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 seq_printf(seq, "0x%p ",
Len Brown4be44fc2005-08-05 00:44:28 -0400910 tz->trips.active[i].devices.handles[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 seq_puts(seq, "\n");
912 }
913
Len Brown4be44fc2005-08-05 00:44:28 -0400914 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return_VALUE(0);
916}
917
918static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
919{
920 return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
921}
922
923static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -0400924acpi_thermal_write_trip_points(struct file *file,
925 const char __user * buffer,
926 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Len Brown4be44fc2005-08-05 00:44:28 -0400928 struct seq_file *m = (struct seq_file *)file->private_data;
929 struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Len Brown4be44fc2005-08-05 00:44:28 -0400931 char *limit_string;
932 int num, critical, hot, passive;
933 int *active;
934 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 ACPI_FUNCTION_TRACE("acpi_thermal_write_trip_points");
937
938 limit_string = kmalloc(ACPI_THERMAL_MAX_LIMIT_STR_LEN, GFP_KERNEL);
Len Brown4be44fc2005-08-05 00:44:28 -0400939 if (!limit_string)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return_VALUE(-ENOMEM);
941
942 memset(limit_string, 0, ACPI_THERMAL_MAX_LIMIT_STR_LEN);
943
Len Brown4be44fc2005-08-05 00:44:28 -0400944 active = kmalloc(ACPI_THERMAL_MAX_ACTIVE * sizeof(int), GFP_KERNEL);
Dave Jonesfdc136c2006-03-08 22:12:00 -0500945 if (!active) {
946 kfree(limit_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return_VALUE(-ENOMEM);
Dave Jonesfdc136c2006-03-08 22:12:00 -0500948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 if (!tz || (count > ACPI_THERMAL_MAX_LIMIT_STR_LEN - 1)) {
951 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid argument\n"));
952 count = -EINVAL;
953 goto end;
954 }
Len Brown4be44fc2005-08-05 00:44:28 -0400955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (copy_from_user(limit_string, buffer, count)) {
957 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid data\n"));
958 count = -EFAULT;
959 goto end;
960 }
Len Brown4be44fc2005-08-05 00:44:28 -0400961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 limit_string[count] = '\0';
963
964 num = sscanf(limit_string, "%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d",
Len Brown4be44fc2005-08-05 00:44:28 -0400965 &critical, &hot, &passive,
966 &active[0], &active[1], &active[2], &active[3], &active[4],
967 &active[5], &active[6], &active[7], &active[8],
968 &active[9]);
969 if (!(num >= 5 && num < (ACPI_THERMAL_MAX_ACTIVE + 3))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid data format\n"));
971 count = -EINVAL;
972 goto end;
973 }
974
975 tz->trips.critical.temperature = CELSIUS_TO_KELVIN(critical);
976 tz->trips.hot.temperature = CELSIUS_TO_KELVIN(hot);
977 tz->trips.passive.temperature = CELSIUS_TO_KELVIN(passive);
978 for (i = 0; i < num - 3; i++) {
979 if (!(tz->trips.active[i].flags.valid))
980 break;
981 tz->trips.active[i].temperature = CELSIUS_TO_KELVIN(active[i]);
982 }
Len Brown4be44fc2005-08-05 00:44:28 -0400983
984 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 kfree(active);
986 kfree(limit_string);
987 return_VALUE(count);
988}
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
991{
Len Brown4be44fc2005-08-05 00:44:28 -0400992 struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 ACPI_FUNCTION_TRACE("acpi_thermal_cooling_seq_show");
995
996 if (!tz)
997 goto end;
998
999 if (!tz->flags.cooling_mode) {
1000 seq_puts(seq, "<setting not supported>\n");
1001 }
1002
Len Brown4be44fc2005-08-05 00:44:28 -04001003 if (tz->cooling_mode == ACPI_THERMAL_MODE_CRITICAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 seq_printf(seq, "cooling mode: critical\n");
1005 else
1006 seq_printf(seq, "cooling mode: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001007 tz->cooling_mode ? "passive" : "active");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Len Brown4be44fc2005-08-05 00:44:28 -04001009 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return_VALUE(0);
1011}
1012
1013static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
1014{
1015 return single_open(file, acpi_thermal_cooling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001016 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
1019static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001020acpi_thermal_write_cooling_mode(struct file *file,
1021 const char __user * buffer,
1022 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
Len Brown4be44fc2005-08-05 00:44:28 -04001024 struct seq_file *m = (struct seq_file *)file->private_data;
1025 struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
1026 int result = 0;
1027 char mode_string[12] = { '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 ACPI_FUNCTION_TRACE("acpi_thermal_write_cooling_mode");
1030
1031 if (!tz || (count > sizeof(mode_string) - 1))
1032 return_VALUE(-EINVAL);
1033
1034 if (!tz->flags.cooling_mode)
1035 return_VALUE(-ENODEV);
1036
1037 if (copy_from_user(mode_string, buffer, count))
1038 return_VALUE(-EFAULT);
Len Brown4be44fc2005-08-05 00:44:28 -04001039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 mode_string[count] = '\0';
Len Brown4be44fc2005-08-05 00:44:28 -04001041
1042 result = acpi_thermal_set_cooling_mode(tz,
1043 simple_strtoul(mode_string, NULL,
1044 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (result)
1046 return_VALUE(result);
1047
1048 acpi_thermal_check(tz);
1049
1050 return_VALUE(count);
1051}
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
1054{
Len Brown4be44fc2005-08-05 00:44:28 -04001055 struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 ACPI_FUNCTION_TRACE("acpi_thermal_polling_seq_show");
1058
1059 if (!tz)
1060 goto end;
1061
1062 if (!tz->polling_frequency) {
1063 seq_puts(seq, "<polling disabled>\n");
1064 goto end;
1065 }
1066
1067 seq_printf(seq, "polling frequency: %lu seconds\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001068 (tz->polling_frequency / 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Len Brown4be44fc2005-08-05 00:44:28 -04001070 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return_VALUE(0);
1072}
1073
1074static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
1075{
1076 return single_open(file, acpi_thermal_polling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001077 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
1080static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001081acpi_thermal_write_polling(struct file *file,
1082 const char __user * buffer,
1083 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
Len Brown4be44fc2005-08-05 00:44:28 -04001085 struct seq_file *m = (struct seq_file *)file->private_data;
1086 struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
1087 int result = 0;
1088 char polling_string[12] = { '\0' };
1089 int seconds = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 ACPI_FUNCTION_TRACE("acpi_thermal_write_polling");
1092
1093 if (!tz || (count > sizeof(polling_string) - 1))
1094 return_VALUE(-EINVAL);
Len Brown4be44fc2005-08-05 00:44:28 -04001095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (copy_from_user(polling_string, buffer, count))
1097 return_VALUE(-EFAULT);
Len Brown4be44fc2005-08-05 00:44:28 -04001098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 polling_string[count] = '\0';
1100
1101 seconds = simple_strtoul(polling_string, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 result = acpi_thermal_set_polling(tz, seconds);
1104 if (result)
1105 return_VALUE(result);
1106
1107 acpi_thermal_check(tz);
1108
1109 return_VALUE(count);
1110}
1111
Len Brown4be44fc2005-08-05 00:44:28 -04001112static int acpi_thermal_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
Len Brown4be44fc2005-08-05 00:44:28 -04001114 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 ACPI_FUNCTION_TRACE("acpi_thermal_add_fs");
1117
1118 if (!acpi_device_dir(device)) {
1119 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -04001120 acpi_thermal_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (!acpi_device_dir(device))
1122 return_VALUE(-ENODEV);
1123 acpi_device_dir(device)->owner = THIS_MODULE;
1124 }
1125
1126 /* 'state' [R] */
1127 entry = create_proc_entry(ACPI_THERMAL_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -04001128 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (!entry)
1130 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
Len Brown4be44fc2005-08-05 00:44:28 -04001131 "Unable to create '%s' fs entry\n",
1132 ACPI_THERMAL_FILE_STATE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 else {
1134 entry->proc_fops = &acpi_thermal_state_fops;
1135 entry->data = acpi_driver_data(device);
1136 entry->owner = THIS_MODULE;
1137 }
1138
1139 /* 'temperature' [R] */
1140 entry = create_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
Len Brown4be44fc2005-08-05 00:44:28 -04001141 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (!entry)
1143 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
Len Brown4be44fc2005-08-05 00:44:28 -04001144 "Unable to create '%s' fs entry\n",
1145 ACPI_THERMAL_FILE_TEMPERATURE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 else {
1147 entry->proc_fops = &acpi_thermal_temp_fops;
1148 entry->data = acpi_driver_data(device);
1149 entry->owner = THIS_MODULE;
1150 }
1151
1152 /* 'trip_points' [R/W] */
1153 entry = create_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
Len Brown4be44fc2005-08-05 00:44:28 -04001154 S_IFREG | S_IRUGO | S_IWUSR,
1155 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (!entry)
1157 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
Len Brown4be44fc2005-08-05 00:44:28 -04001158 "Unable to create '%s' fs entry\n",
1159 ACPI_THERMAL_FILE_TRIP_POINTS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 else {
1161 entry->proc_fops = &acpi_thermal_trip_fops;
1162 entry->data = acpi_driver_data(device);
1163 entry->owner = THIS_MODULE;
1164 }
1165
1166 /* 'cooling_mode' [R/W] */
1167 entry = create_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
Len Brown4be44fc2005-08-05 00:44:28 -04001168 S_IFREG | S_IRUGO | S_IWUSR,
1169 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (!entry)
1171 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
Len Brown4be44fc2005-08-05 00:44:28 -04001172 "Unable to create '%s' fs entry\n",
1173 ACPI_THERMAL_FILE_COOLING_MODE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 else {
1175 entry->proc_fops = &acpi_thermal_cooling_fops;
1176 entry->data = acpi_driver_data(device);
1177 entry->owner = THIS_MODULE;
1178 }
1179
1180 /* 'polling_frequency' [R/W] */
1181 entry = create_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
Len Brown4be44fc2005-08-05 00:44:28 -04001182 S_IFREG | S_IRUGO | S_IWUSR,
1183 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (!entry)
1185 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
Len Brown4be44fc2005-08-05 00:44:28 -04001186 "Unable to create '%s' fs entry\n",
1187 ACPI_THERMAL_FILE_POLLING_FREQ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 else {
1189 entry->proc_fops = &acpi_thermal_polling_fops;
1190 entry->data = acpi_driver_data(device);
1191 entry->owner = THIS_MODULE;
1192 }
1193
1194 return_VALUE(0);
1195}
1196
Len Brown4be44fc2005-08-05 00:44:28 -04001197static int acpi_thermal_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
1199 ACPI_FUNCTION_TRACE("acpi_thermal_remove_fs");
1200
1201 if (acpi_device_dir(device)) {
1202 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1203 acpi_device_dir(device));
1204 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1205 acpi_device_dir(device));
1206 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1207 acpi_device_dir(device));
1208 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1209 acpi_device_dir(device));
1210 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1211 acpi_device_dir(device));
1212 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1213 acpi_device_dir(device) = NULL;
1214 }
1215
1216 return_VALUE(0);
1217}
1218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219/* --------------------------------------------------------------------------
1220 Driver Interface
1221 -------------------------------------------------------------------------- */
1222
Len Brown4be44fc2005-08-05 00:44:28 -04001223static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
Len Brown4be44fc2005-08-05 00:44:28 -04001225 struct acpi_thermal *tz = (struct acpi_thermal *)data;
1226 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 ACPI_FUNCTION_TRACE("acpi_thermal_notify");
1229
1230 if (!tz)
1231 return_VOID;
1232
1233 if (acpi_bus_get_device(tz->handle, &device))
1234 return_VOID;
1235
1236 switch (event) {
1237 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1238 acpi_thermal_check(tz);
1239 break;
1240 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
1241 acpi_thermal_get_trip_points(tz);
1242 acpi_thermal_check(tz);
1243 acpi_bus_generate_event(device, event, 0);
1244 break;
1245 case ACPI_THERMAL_NOTIFY_DEVICES:
1246 if (tz->flags.devices)
1247 acpi_thermal_get_devices(tz);
1248 acpi_bus_generate_event(device, event, 0);
1249 break;
1250 default:
1251 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001252 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 break;
1254 }
1255
1256 return_VOID;
1257}
1258
Len Brown4be44fc2005-08-05 00:44:28 -04001259static int acpi_thermal_get_info(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
Len Brown4be44fc2005-08-05 00:44:28 -04001261 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 ACPI_FUNCTION_TRACE("acpi_thermal_get_info");
1264
1265 if (!tz)
1266 return_VALUE(-EINVAL);
1267
1268 /* Get temperature [_TMP] (required) */
1269 result = acpi_thermal_get_temperature(tz);
1270 if (result)
1271 return_VALUE(result);
1272
1273 /* Get trip points [_CRT, _PSV, etc.] (required) */
1274 result = acpi_thermal_get_trip_points(tz);
1275 if (result)
1276 return_VALUE(result);
1277
1278 /* Set the cooling mode [_SCP] to active cooling (default) */
1279 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
Len Brown4be44fc2005-08-05 00:44:28 -04001280 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 tz->flags.cooling_mode = 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001282 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 /* Oh,we have not _SCP method.
Len Brown4be44fc2005-08-05 00:44:28 -04001284 Generally show cooling_mode by _ACx, _PSV,spec 12.2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 tz->flags.cooling_mode = 0;
Len Brown4be44fc2005-08-05 00:44:28 -04001286 if (tz->trips.active[0].flags.valid
1287 && tz->trips.passive.flags.valid) {
1288 if (tz->trips.passive.temperature >
1289 tz->trips.active[0].temperature)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 tz->cooling_mode = ACPI_THERMAL_MODE_ACTIVE;
Len Brown4be44fc2005-08-05 00:44:28 -04001291 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 tz->cooling_mode = ACPI_THERMAL_MODE_PASSIVE;
Len Brown4be44fc2005-08-05 00:44:28 -04001293 } else if (!tz->trips.active[0].flags.valid
1294 && tz->trips.passive.flags.valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 tz->cooling_mode = ACPI_THERMAL_MODE_PASSIVE;
Len Brown4be44fc2005-08-05 00:44:28 -04001296 } else if (tz->trips.active[0].flags.valid
1297 && !tz->trips.passive.flags.valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 tz->cooling_mode = ACPI_THERMAL_MODE_ACTIVE;
1299 } else {
1300 /* _ACx and _PSV are optional, but _CRT is required */
1301 tz->cooling_mode = ACPI_THERMAL_MODE_CRITICAL;
1302 }
1303 }
1304
1305 /* Get default polling frequency [_TZP] (optional) */
1306 if (tzp)
1307 tz->polling_frequency = tzp;
1308 else
1309 acpi_thermal_get_polling_frequency(tz);
1310
1311 /* Get devices in this thermal zone [_TZD] (optional) */
1312 result = acpi_thermal_get_devices(tz);
1313 if (!result)
1314 tz->flags.devices = 1;
1315
1316 return_VALUE(0);
1317}
1318
Len Brown4be44fc2005-08-05 00:44:28 -04001319static int acpi_thermal_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
Len Brown4be44fc2005-08-05 00:44:28 -04001321 int result = 0;
1322 acpi_status status = AE_OK;
1323 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 ACPI_FUNCTION_TRACE("acpi_thermal_add");
1326
1327 if (!device)
1328 return_VALUE(-EINVAL);
1329
1330 tz = kmalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
1331 if (!tz)
1332 return_VALUE(-ENOMEM);
1333 memset(tz, 0, sizeof(struct acpi_thermal));
1334
1335 tz->handle = device->handle;
1336 strcpy(tz->name, device->pnp.bus_id);
1337 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1338 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1339 acpi_driver_data(device) = tz;
1340
1341 result = acpi_thermal_get_info(tz);
1342 if (result)
1343 goto end;
1344
1345 result = acpi_thermal_add_fs(device);
1346 if (result)
1347 return_VALUE(result);
1348
1349 init_timer(&tz->timer);
1350
1351 acpi_thermal_check(tz);
1352
1353 status = acpi_install_notify_handler(tz->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001354 ACPI_DEVICE_NOTIFY,
1355 acpi_thermal_notify, tz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 if (ACPI_FAILURE(status)) {
1357 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
Len Brown4be44fc2005-08-05 00:44:28 -04001358 "Error installing notify handler\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 result = -ENODEV;
1360 goto end;
1361 }
1362
1363 printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001364 acpi_device_name(device), acpi_device_bid(device),
1365 KELVIN_TO_CELSIUS(tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Len Brown4be44fc2005-08-05 00:44:28 -04001367 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 if (result) {
1369 acpi_thermal_remove_fs(device);
1370 kfree(tz);
1371 }
1372
1373 return_VALUE(result);
1374}
1375
Len Brown4be44fc2005-08-05 00:44:28 -04001376static int acpi_thermal_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
Len Brown4be44fc2005-08-05 00:44:28 -04001378 acpi_status status = AE_OK;
1379 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 ACPI_FUNCTION_TRACE("acpi_thermal_remove");
1382
1383 if (!device || !acpi_driver_data(device))
1384 return_VALUE(-EINVAL);
1385
Len Brown4be44fc2005-08-05 00:44:28 -04001386 tz = (struct acpi_thermal *)acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 /* avoid timer adding new defer task */
1389 tz->zombie = 1;
1390 /* wait for running timer (on other CPUs) finish */
1391 del_timer_sync(&(tz->timer));
1392 /* synchronize deferred task */
1393 acpi_os_wait_events_complete(NULL);
1394 /* deferred task may reinsert timer */
1395 del_timer_sync(&(tz->timer));
1396
1397 status = acpi_remove_notify_handler(tz->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001398 ACPI_DEVICE_NOTIFY,
1399 acpi_thermal_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 if (ACPI_FAILURE(status))
1401 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
Len Brown4be44fc2005-08-05 00:44:28 -04001402 "Error removing notify handler\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 /* Terminate policy */
Len Brown4be44fc2005-08-05 00:44:28 -04001405 if (tz->trips.passive.flags.valid && tz->trips.passive.flags.enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 tz->trips.passive.flags.enabled = 0;
1407 acpi_thermal_passive(tz);
1408 }
1409 if (tz->trips.active[0].flags.valid
Len Brown4be44fc2005-08-05 00:44:28 -04001410 && tz->trips.active[0].flags.enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 tz->trips.active[0].flags.enabled = 0;
1412 acpi_thermal_active(tz);
1413 }
1414
1415 acpi_thermal_remove_fs(device);
1416
1417 kfree(tz);
1418 return_VALUE(0);
1419}
1420
Len Brown4be44fc2005-08-05 00:44:28 -04001421static int __init acpi_thermal_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
Len Brown4be44fc2005-08-05 00:44:28 -04001423 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 ACPI_FUNCTION_TRACE("acpi_thermal_init");
1426
1427 acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1428 if (!acpi_thermal_dir)
1429 return_VALUE(-ENODEV);
1430 acpi_thermal_dir->owner = THIS_MODULE;
1431
1432 result = acpi_bus_register_driver(&acpi_thermal_driver);
1433 if (result < 0) {
1434 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1435 return_VALUE(-ENODEV);
1436 }
1437
1438 return_VALUE(0);
1439}
1440
Len Brown4be44fc2005-08-05 00:44:28 -04001441static void __exit acpi_thermal_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
1443 ACPI_FUNCTION_TRACE("acpi_thermal_exit");
1444
1445 acpi_bus_unregister_driver(&acpi_thermal_driver);
1446
1447 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1448
1449 return_VOID;
1450}
1451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452module_init(acpi_thermal_init);
1453module_exit(acpi_thermal_exit);