blob: c855f4446b5fc45263166051829166e7f1b1a0c4 [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);
Konstantin Karasyov74ce14682006-05-08 08:32:00 -040085static int acpi_thermal_resume(struct acpi_device *device, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
87static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
88static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -040089static ssize_t acpi_thermal_write_trip_points(struct file *,
90 const char __user *, size_t,
91 loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -040093static ssize_t acpi_thermal_write_cooling_mode(struct file *,
94 const char __user *, size_t,
95 loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -040097static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
98 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100static struct acpi_driver acpi_thermal_driver = {
Len Brown4be44fc2005-08-05 00:44:28 -0400101 .name = ACPI_THERMAL_DRIVER_NAME,
102 .class = ACPI_THERMAL_CLASS,
103 .ids = ACPI_THERMAL_HID,
104 .ops = {
105 .add = acpi_thermal_add,
106 .remove = acpi_thermal_remove,
Konstantin Karasyov74ce14682006-05-08 08:32:00 -0400107 .resume = acpi_thermal_resume,
Len Brown4be44fc2005-08-05 00:44:28 -0400108 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109};
110
111struct acpi_thermal_state {
Len Brown4be44fc2005-08-05 00:44:28 -0400112 u8 critical:1;
113 u8 hot:1;
114 u8 passive:1;
115 u8 active:1;
116 u8 reserved:4;
117 int active_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118};
119
120struct acpi_thermal_state_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400121 u8 valid:1;
122 u8 enabled:1;
123 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
126struct acpi_thermal_critical {
127 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400128 unsigned long temperature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129};
130
131struct acpi_thermal_hot {
132 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400133 unsigned long temperature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
135
136struct acpi_thermal_passive {
137 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400138 unsigned long temperature;
139 unsigned long tc1;
140 unsigned long tc2;
141 unsigned long tsp;
142 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143};
144
145struct acpi_thermal_active {
146 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400147 unsigned long temperature;
148 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149};
150
151struct acpi_thermal_trips {
152 struct acpi_thermal_critical critical;
Len Brown4be44fc2005-08-05 00:44:28 -0400153 struct acpi_thermal_hot hot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 struct acpi_thermal_passive passive;
155 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
156};
157
158struct acpi_thermal_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400159 u8 cooling_mode:1; /* _SCP */
160 u8 devices:1; /* _TZD */
161 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162};
163
164struct acpi_thermal {
Len Brown4be44fc2005-08-05 00:44:28 -0400165 acpi_handle handle;
166 acpi_bus_id name;
167 unsigned long temperature;
168 unsigned long last_temperature;
169 unsigned long polling_frequency;
170 u8 cooling_mode;
171 volatile u8 zombie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 struct acpi_thermal_flags flags;
173 struct acpi_thermal_state state;
174 struct acpi_thermal_trips trips;
Len Brown4be44fc2005-08-05 00:44:28 -0400175 struct acpi_handle_list devices;
176 struct timer_list timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177};
178
179static struct file_operations acpi_thermal_state_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400180 .open = acpi_thermal_state_open_fs,
181 .read = seq_read,
182 .llseek = seq_lseek,
183 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184};
185
186static struct file_operations acpi_thermal_temp_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400187 .open = acpi_thermal_temp_open_fs,
188 .read = seq_read,
189 .llseek = seq_lseek,
190 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191};
192
193static struct file_operations acpi_thermal_trip_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400194 .open = acpi_thermal_trip_open_fs,
195 .read = seq_read,
196 .write = acpi_thermal_write_trip_points,
197 .llseek = seq_lseek,
198 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199};
200
201static struct file_operations acpi_thermal_cooling_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400202 .open = acpi_thermal_cooling_open_fs,
203 .read = seq_read,
204 .write = acpi_thermal_write_cooling_mode,
205 .llseek = seq_lseek,
206 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207};
208
209static struct file_operations acpi_thermal_polling_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400210 .open = acpi_thermal_polling_open_fs,
211 .read = seq_read,
212 .write = acpi_thermal_write_polling,
213 .llseek = seq_lseek,
214 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215};
216
217/* --------------------------------------------------------------------------
218 Thermal Zone Management
219 -------------------------------------------------------------------------- */
220
Len Brown4be44fc2005-08-05 00:44:28 -0400221static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Len Brown4be44fc2005-08-05 00:44:28 -0400223 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400227 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 tz->last_temperature = tz->temperature;
230
Len Brown4be44fc2005-08-05 00:44:28 -0400231 status =
232 acpi_evaluate_integer(tz->handle, "_TMP", NULL, &tz->temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400234 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Len Brown4be44fc2005-08-05 00:44:28 -0400236 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
237 tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Patrick Mocheld550d982006-06-27 00:41:40 -0400239 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Len Brown4be44fc2005-08-05 00:44:28 -0400242static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Len Brown4be44fc2005-08-05 00:44:28 -0400244 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400248 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
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))
Patrick Mocheld550d982006-06-27 00:41:40 -0400254 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
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
Patrick Mocheld550d982006-06-27 00:41:40 -0400259 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
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{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400266 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 tz->polling_frequency = seconds * 10; /* Convert value to deci-seconds */
269
Len Brown4be44fc2005-08-05 00:44:28 -0400270 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
271 "Polling frequency set to %lu seconds\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_cooling_mode(struct acpi_thermal *tz, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Len Brown4be44fc2005-08-05 00:44:28 -0400279 acpi_status status = AE_OK;
280 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
281 struct acpi_object_list arg_list = { 1, &arg0 };
282 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400286 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 status = acpi_get_handle(tz->handle, "_SCP", &handle);
289 if (ACPI_FAILURE(status)) {
290 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400291 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293
294 arg0.integer.value = mode;
295
296 status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
297 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400298 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 tz->cooling_mode = mode;
301
Len Brown4be44fc2005-08-05 00:44:28 -0400302 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Cooling mode [%s]\n",
303 mode ? "passive" : "active"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Patrick Mocheld550d982006-06-27 00:41:40 -0400305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Len Brown4be44fc2005-08-05 00:44:28 -0400308static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Len Brown4be44fc2005-08-05 00:44:28 -0400310 acpi_status status = AE_OK;
311 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400315 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 /* Critical Shutdown (required) */
318
Len Brown4be44fc2005-08-05 00:44:28 -0400319 status = acpi_evaluate_integer(tz->handle, "_CRT", NULL,
320 &tz->trips.critical.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (ACPI_FAILURE(status)) {
322 tz->trips.critical.flags.valid = 0;
Thomas Renningera6fc6722006-06-26 23:58:43 -0400323 ACPI_EXCEPTION((AE_INFO, status, "No critical threshold"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400324 return -ENODEV;
Len Brown4be44fc2005-08-05 00:44:28 -0400325 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 tz->trips.critical.flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400327 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
328 "Found critical threshold [%lu]\n",
329 tz->trips.critical.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331
332 /* Critical Sleep (optional) */
333
Len Brown4be44fc2005-08-05 00:44:28 -0400334 status =
335 acpi_evaluate_integer(tz->handle, "_HOT", NULL,
336 &tz->trips.hot.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (ACPI_FAILURE(status)) {
338 tz->trips.hot.flags.valid = 0;
339 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No hot threshold\n"));
Len Brown4be44fc2005-08-05 00:44:28 -0400340 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 tz->trips.hot.flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400342 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found hot threshold [%lu]\n",
343 tz->trips.hot.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345
346 /* Passive: Processors (optional) */
347
Len Brown4be44fc2005-08-05 00:44:28 -0400348 status =
349 acpi_evaluate_integer(tz->handle, "_PSV", NULL,
350 &tz->trips.passive.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (ACPI_FAILURE(status)) {
352 tz->trips.passive.flags.valid = 0;
353 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No passive threshold\n"));
Len Brown4be44fc2005-08-05 00:44:28 -0400354 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 tz->trips.passive.flags.valid = 1;
356
Len Brown4be44fc2005-08-05 00:44:28 -0400357 status =
358 acpi_evaluate_integer(tz->handle, "_TC1", NULL,
359 &tz->trips.passive.tc1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (ACPI_FAILURE(status))
361 tz->trips.passive.flags.valid = 0;
362
Len Brown4be44fc2005-08-05 00:44:28 -0400363 status =
364 acpi_evaluate_integer(tz->handle, "_TC2", NULL,
365 &tz->trips.passive.tc2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (ACPI_FAILURE(status))
367 tz->trips.passive.flags.valid = 0;
368
Len Brown4be44fc2005-08-05 00:44:28 -0400369 status =
370 acpi_evaluate_integer(tz->handle, "_TSP", NULL,
371 &tz->trips.passive.tsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (ACPI_FAILURE(status))
373 tz->trips.passive.flags.valid = 0;
374
Len Brown4be44fc2005-08-05 00:44:28 -0400375 status =
376 acpi_evaluate_reference(tz->handle, "_PSL", NULL,
377 &tz->trips.passive.devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (ACPI_FAILURE(status))
379 tz->trips.passive.flags.valid = 0;
380
381 if (!tz->trips.passive.flags.valid)
Len Browncece9292006-06-26 23:04:31 -0400382 printk(KERN_WARNING PREFIX "Invalid passive threshold\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 else
Len Brown4be44fc2005-08-05 00:44:28 -0400384 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
385 "Found passive threshold [%lu]\n",
386 tz->trips.passive.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388
389 /* Active: Fans, etc. (optional) */
390
Len Brown4be44fc2005-08-05 00:44:28 -0400391 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Len Brown4be44fc2005-08-05 00:44:28 -0400393 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Len Brown4be44fc2005-08-05 00:44:28 -0400395 status =
396 acpi_evaluate_integer(tz->handle, name, NULL,
397 &tz->trips.active[i].temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (ACPI_FAILURE(status))
399 break;
400
401 name[2] = 'L';
Len Brown4be44fc2005-08-05 00:44:28 -0400402 status =
403 acpi_evaluate_reference(tz->handle, name, NULL,
404 &tz->trips.active[i].devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (ACPI_SUCCESS(status)) {
406 tz->trips.active[i].flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400407 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
408 "Found active threshold [%d]:[%lu]\n",
409 i, tz->trips.active[i].temperature));
410 } else
Thomas Renningera6fc6722006-06-26 23:58:43 -0400411 ACPI_EXCEPTION((AE_INFO, status,
412 "Invalid active threshold [%d]", i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414
Patrick Mocheld550d982006-06-27 00:41:40 -0400415 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
Len Brown4be44fc2005-08-05 00:44:28 -0400418static int acpi_thermal_get_devices(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Len Brown4be44fc2005-08-05 00:44:28 -0400420 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400424 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Len Brown4be44fc2005-08-05 00:44:28 -0400426 status =
427 acpi_evaluate_reference(tz->handle, "_TZD", NULL, &tz->devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400429 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Patrick Mocheld550d982006-06-27 00:41:40 -0400431 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
Len Brown4be44fc2005-08-05 00:44:28 -0400434static int acpi_thermal_call_usermode(char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Len Brown4be44fc2005-08-05 00:44:28 -0400436 char *argv[2] = { NULL, NULL };
437 char *envp[3] = { NULL, NULL, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 if (!path)
Patrick Mocheld550d982006-06-27 00:41:40 -0400441 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 argv[0] = path;
444
445 /* minimal command environment */
446 envp[0] = "HOME=/";
447 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
Len Brown4be44fc2005-08-05 00:44:28 -0400448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 call_usermodehelper(argv[0], argv, envp, 0);
450
Patrick Mocheld550d982006-06-27 00:41:40 -0400451 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Len Brown4be44fc2005-08-05 00:44:28 -0400454static int acpi_thermal_critical(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Len Brown4be44fc2005-08-05 00:44:28 -0400456 int result = 0;
457 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 if (!tz || !tz->trips.critical.flags.valid)
Patrick Mocheld550d982006-06-27 00:41:40 -0400461 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 if (tz->temperature >= tz->trips.critical.temperature) {
Len Browncece9292006-06-26 23:04:31 -0400464 printk(KERN_WARNING PREFIX "Critical trip point\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 tz->trips.critical.flags.enabled = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400466 } else if (tz->trips.critical.flags.enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 tz->trips.critical.flags.enabled = 0;
468
469 result = acpi_bus_get_device(tz->handle, &device);
470 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400471 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Len Brown4be44fc2005-08-05 00:44:28 -0400473 printk(KERN_EMERG
474 "Critical temperature reached (%ld C), shutting down.\n",
475 KELVIN_TO_CELSIUS(tz->temperature));
476 acpi_bus_generate_event(device, ACPI_THERMAL_NOTIFY_CRITICAL,
477 tz->trips.critical.flags.enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 acpi_thermal_call_usermode(ACPI_THERMAL_PATH_POWEROFF);
480
Patrick Mocheld550d982006-06-27 00:41:40 -0400481 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
Len Brown4be44fc2005-08-05 00:44:28 -0400484static int acpi_thermal_hot(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Len Brown4be44fc2005-08-05 00:44:28 -0400486 int result = 0;
487 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 if (!tz || !tz->trips.hot.flags.valid)
Patrick Mocheld550d982006-06-27 00:41:40 -0400491 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 if (tz->temperature >= tz->trips.hot.temperature) {
Len Browncece9292006-06-26 23:04:31 -0400494 printk(KERN_WARNING PREFIX "Hot trip point\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 tz->trips.hot.flags.enabled = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400496 } else if (tz->trips.hot.flags.enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 tz->trips.hot.flags.enabled = 0;
498
499 result = acpi_bus_get_device(tz->handle, &device);
500 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400501 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Len Brown4be44fc2005-08-05 00:44:28 -0400503 acpi_bus_generate_event(device, ACPI_THERMAL_NOTIFY_HOT,
504 tz->trips.hot.flags.enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 /* TBD: Call user-mode "sleep(S4)" function */
507
Patrick Mocheld550d982006-06-27 00:41:40 -0400508 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400511static void acpi_thermal_passive(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400513 int result = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 struct acpi_thermal_passive *passive = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400515 int trend = 0;
516 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 if (!tz || !tz->trips.passive.flags.valid)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400520 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 passive = &(tz->trips.passive);
523
524 /*
525 * Above Trip?
526 * -----------
527 * Calculate the thermal trend (using the passive cooling equation)
528 * and modify the performance limit for all passive cooling devices
529 * accordingly. Note that we assume symmetry.
530 */
531 if (tz->temperature >= passive->temperature) {
Len Brown4be44fc2005-08-05 00:44:28 -0400532 trend =
533 (passive->tc1 * (tz->temperature - tz->last_temperature)) +
534 (passive->tc2 * (tz->temperature - passive->temperature));
535 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
536 "trend[%d]=(tc1[%lu]*(tmp[%lu]-last[%lu]))+(tc2[%lu]*(tmp[%lu]-psv[%lu]))\n",
537 trend, passive->tc1, tz->temperature,
538 tz->last_temperature, passive->tc2,
539 tz->temperature, passive->temperature));
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400540 passive->flags.enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 /* Heating up? */
542 if (trend > 0)
Len Brown4be44fc2005-08-05 00:44:28 -0400543 for (i = 0; i < passive->devices.count; i++)
544 acpi_processor_set_thermal_limit(passive->
545 devices.
546 handles[i],
547 ACPI_PROCESSOR_LIMIT_INCREMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /* Cooling off? */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400549 else if (trend < 0) {
Len Brown4be44fc2005-08-05 00:44:28 -0400550 for (i = 0; i < passive->devices.count; i++)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400551 /*
552 * assume that we are on highest
553 * freq/lowest thrott and can leave
554 * passive mode, even in error case
555 */
556 if (!acpi_processor_set_thermal_limit
557 (passive->devices.handles[i],
558 ACPI_PROCESSOR_LIMIT_DECREMENT))
559 result = 0;
560 /*
561 * Leave cooling mode, even if the temp might
562 * higher than trip point This is because some
563 * machines might have long thermal polling
564 * frequencies (tsp) defined. We will fall back
565 * into passive mode in next cycle (probably quicker)
566 */
567 if (result) {
568 passive->flags.enabled = 0;
569 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
570 "Disabling passive cooling, still above threshold,"
571 " but we are cooling down\n"));
572 }
573 }
574 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576
577 /*
578 * Below Trip?
579 * -----------
580 * Implement passive cooling hysteresis to slowly increase performance
581 * and avoid thrashing around the passive trip point. Note that we
582 * assume symmetry.
583 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400584 if (!passive->flags.enabled)
585 return;
586 for (i = 0; i < passive->devices.count; i++)
587 if (!acpi_processor_set_thermal_limit
588 (passive->devices.handles[i],
589 ACPI_PROCESSOR_LIMIT_DECREMENT))
590 result = 0;
591 if (result) {
592 passive->flags.enabled = 0;
593 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
594 "Disabling passive cooling (zone is cool)\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400598static void acpi_thermal_active(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Len Brown4be44fc2005-08-05 00:44:28 -0400600 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 struct acpi_thermal_active *active = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400602 int i = 0;
603 int j = 0;
604 unsigned long maxtemp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 if (!tz)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400608 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Len Brown4be44fc2005-08-05 00:44:28 -0400610 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 active = &(tz->trips.active[i]);
612 if (!active || !active->flags.valid)
613 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (tz->temperature >= active->temperature) {
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400615 /*
616 * Above Threshold?
617 * ----------------
618 * If not already enabled, turn ON all cooling devices
619 * associated with this active threshold.
620 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (active->temperature > maxtemp)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400622 tz->state.active_index = i;
623 maxtemp = active->temperature;
624 if (active->flags.enabled)
625 continue;
626 for (j = 0; j < active->devices.count; j++) {
627 result =
628 acpi_bus_set_power(active->devices.
629 handles[j],
630 ACPI_STATE_D0);
631 if (result) {
Len Browncece9292006-06-26 23:04:31 -0400632 printk(KERN_WARNING PREFIX
633 "Unable to turn cooling device [%p] 'on'\n",
Thomas Renningera6fc6722006-06-26 23:58:43 -0400634 active->devices.
Len Browncece9292006-06-26 23:04:31 -0400635 handles[j]);
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400636 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400638 active->flags.enabled = 1;
639 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
640 "Cooling device [%p] now 'on'\n",
641 active->devices.handles[j]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400643 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400645 if (!active->flags.enabled)
646 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /*
648 * Below Threshold?
649 * ----------------
650 * Turn OFF all cooling devices associated with this
651 * threshold.
652 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400653 for (j = 0; j < active->devices.count; j++) {
654 result = acpi_bus_set_power(active->devices.handles[j],
655 ACPI_STATE_D3);
656 if (result) {
Len Browncece9292006-06-26 23:04:31 -0400657 printk(KERN_WARNING PREFIX
658 "Unable to turn cooling device [%p] 'off'\n",
659 active->devices.handles[j]);
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400660 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400662 active->flags.enabled = 0;
663 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
664 "Cooling device [%p] now 'off'\n",
665 active->devices.handles[j]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
Len Brown4be44fc2005-08-05 00:44:28 -0400670static void acpi_thermal_check(void *context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Len Brown4be44fc2005-08-05 00:44:28 -0400672static void acpi_thermal_run(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
674 struct acpi_thermal *tz = (struct acpi_thermal *)data;
675 if (!tz->zombie)
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400676 acpi_os_execute(OSL_GPE_HANDLER, acpi_thermal_check, (void *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
Len Brown4be44fc2005-08-05 00:44:28 -0400679static void acpi_thermal_check(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Len Brown4be44fc2005-08-05 00:44:28 -0400681 int result = 0;
682 struct acpi_thermal *tz = (struct acpi_thermal *)data;
683 unsigned long sleep_time = 0;
684 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 struct acpi_thermal_state state;
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 if (!tz) {
Len Brown64684632006-06-26 23:41:38 -0400689 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400690 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692
693 state = tz->state;
694
695 result = acpi_thermal_get_temperature(tz);
696 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400697 return;
Len Brown4be44fc2005-08-05 00:44:28 -0400698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 memset(&tz->state, 0, sizeof(tz->state));
Len Brown4be44fc2005-08-05 00:44:28 -0400700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 /*
702 * Check Trip Points
703 * -----------------
704 * Compare the current temperature to the trip point values to see
705 * if we've entered one of the thermal policy states. Note that
706 * this function determines when a state is entered, but the
707 * individual policy decides when it is exited (e.g. hysteresis).
708 */
709 if (tz->trips.critical.flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400710 state.critical |=
711 (tz->temperature >= tz->trips.critical.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (tz->trips.hot.flags.valid)
713 state.hot |= (tz->temperature >= tz->trips.hot.temperature);
714 if (tz->trips.passive.flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400715 state.passive |=
716 (tz->temperature >= tz->trips.passive.temperature);
717 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (tz->trips.active[i].flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400719 state.active |=
720 (tz->temperature >=
721 tz->trips.active[i].temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 /*
724 * Invoke Policy
725 * -------------
726 * Separated from the above check to allow individual policy to
727 * determine when to exit a given state.
728 */
729 if (state.critical)
730 acpi_thermal_critical(tz);
731 if (state.hot)
732 acpi_thermal_hot(tz);
733 if (state.passive)
734 acpi_thermal_passive(tz);
735 if (state.active)
736 acpi_thermal_active(tz);
737
738 /*
739 * Calculate State
740 * ---------------
741 * Again, separated from the above two to allow independent policy
742 * decisions.
743 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400744 tz->state.critical = tz->trips.critical.flags.enabled;
745 tz->state.hot = tz->trips.hot.flags.enabled;
746 tz->state.passive = tz->trips.passive.flags.enabled;
747 tz->state.active = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400748 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400749 tz->state.active |= tz->trips.active[i].flags.enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 /*
752 * Calculate Sleep Time
753 * --------------------
754 * If we're in the passive state, use _TSP's value. Otherwise
755 * use the default polling frequency (e.g. _TZP). If no polling
756 * frequency is specified then we'll wait forever (at least until
757 * a thermal event occurs). Note that _TSP and _TZD values are
758 * given in 1/10th seconds (we must covert to milliseconds).
759 */
760 if (tz->state.passive)
761 sleep_time = tz->trips.passive.tsp * 100;
762 else if (tz->polling_frequency > 0)
763 sleep_time = tz->polling_frequency * 100;
764
Len Brown4be44fc2005-08-05 00:44:28 -0400765 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: temperature[%lu] sleep[%lu]\n",
766 tz->name, tz->temperature, sleep_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 /*
769 * Schedule Next Poll
770 * ------------------
771 */
772 if (!sleep_time) {
773 if (timer_pending(&(tz->timer)))
774 del_timer(&(tz->timer));
Len Brown4be44fc2005-08-05 00:44:28 -0400775 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (timer_pending(&(tz->timer)))
777 mod_timer(&(tz->timer), (HZ * sleep_time) / 1000);
778 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400779 tz->timer.data = (unsigned long)tz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 tz->timer.function = acpi_thermal_run;
781 tz->timer.expires = jiffies + (HZ * sleep_time) / 1000;
782 add_timer(&(tz->timer));
783 }
784 }
785
Patrick Mocheld550d982006-06-27 00:41:40 -0400786 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789/* --------------------------------------------------------------------------
790 FS Interface (/proc)
791 -------------------------------------------------------------------------- */
792
Len Brown4be44fc2005-08-05 00:44:28 -0400793static struct proc_dir_entry *acpi_thermal_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
796{
Len Brown4be44fc2005-08-05 00:44:28 -0400797 struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 if (!tz)
801 goto end;
802
803 seq_puts(seq, "state: ");
804
Len Brown4be44fc2005-08-05 00:44:28 -0400805 if (!tz->state.critical && !tz->state.hot && !tz->state.passive
806 && !tz->state.active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 seq_puts(seq, "ok\n");
808 else {
809 if (tz->state.critical)
810 seq_puts(seq, "critical ");
811 if (tz->state.hot)
812 seq_puts(seq, "hot ");
813 if (tz->state.passive)
814 seq_puts(seq, "passive ");
815 if (tz->state.active)
816 seq_printf(seq, "active[%d]", tz->state.active_index);
817 seq_puts(seq, "\n");
818 }
819
Len Brown4be44fc2005-08-05 00:44:28 -0400820 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400821 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823
824static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
825{
826 return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
827}
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
830{
Len Brown4be44fc2005-08-05 00:44:28 -0400831 int result = 0;
832 struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 if (!tz)
836 goto end;
837
838 result = acpi_thermal_get_temperature(tz);
839 if (result)
840 goto end;
841
Len Brown4be44fc2005-08-05 00:44:28 -0400842 seq_printf(seq, "temperature: %ld C\n",
843 KELVIN_TO_CELSIUS(tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Len Brown4be44fc2005-08-05 00:44:28 -0400845 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400846 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
849static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
850{
851 return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
852}
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
855{
Len Brown4be44fc2005-08-05 00:44:28 -0400856 struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
857 int i = 0;
858 int j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 if (!tz)
862 goto end;
863
864 if (tz->trips.critical.flags.valid)
865 seq_printf(seq, "critical (S5): %ld C\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400866 KELVIN_TO_CELSIUS(tz->trips.critical.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 if (tz->trips.hot.flags.valid)
869 seq_printf(seq, "hot (S4): %ld C\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400870 KELVIN_TO_CELSIUS(tz->trips.hot.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 if (tz->trips.passive.flags.valid) {
Len Brown4be44fc2005-08-05 00:44:28 -0400873 seq_printf(seq,
874 "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
875 KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
876 tz->trips.passive.tc1, tz->trips.passive.tc2,
877 tz->trips.passive.tsp);
878 for (j = 0; j < tz->trips.passive.devices.count; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Len Brown4be44fc2005-08-05 00:44:28 -0400880 seq_printf(seq, "0x%p ",
881 tz->trips.passive.devices.handles[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
883 seq_puts(seq, "\n");
884 }
885
886 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
887 if (!(tz->trips.active[i].flags.valid))
888 break;
889 seq_printf(seq, "active[%d]: %ld C: devices=",
Len Brown4be44fc2005-08-05 00:44:28 -0400890 i,
891 KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
892 for (j = 0; j < tz->trips.active[i].devices.count; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 seq_printf(seq, "0x%p ",
Len Brown4be44fc2005-08-05 00:44:28 -0400894 tz->trips.active[i].devices.handles[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 seq_puts(seq, "\n");
896 }
897
Len Brown4be44fc2005-08-05 00:44:28 -0400898 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400899 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
902static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
903{
904 return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
905}
906
907static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -0400908acpi_thermal_write_trip_points(struct file *file,
909 const char __user * buffer,
910 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
Len Brown4be44fc2005-08-05 00:44:28 -0400912 struct seq_file *m = (struct seq_file *)file->private_data;
913 struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Len Brown4be44fc2005-08-05 00:44:28 -0400915 char *limit_string;
916 int num, critical, hot, passive;
917 int *active;
918 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 limit_string = kmalloc(ACPI_THERMAL_MAX_LIMIT_STR_LEN, GFP_KERNEL);
Len Brown4be44fc2005-08-05 00:44:28 -0400922 if (!limit_string)
Patrick Mocheld550d982006-06-27 00:41:40 -0400923 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 memset(limit_string, 0, ACPI_THERMAL_MAX_LIMIT_STR_LEN);
926
Len Brown4be44fc2005-08-05 00:44:28 -0400927 active = kmalloc(ACPI_THERMAL_MAX_ACTIVE * sizeof(int), GFP_KERNEL);
Dave Jonesfdc136c2006-03-08 22:12:00 -0500928 if (!active) {
929 kfree(limit_string);
Patrick Mocheld550d982006-06-27 00:41:40 -0400930 return -ENOMEM;
Dave Jonesfdc136c2006-03-08 22:12:00 -0500931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 if (!tz || (count > ACPI_THERMAL_MAX_LIMIT_STR_LEN - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 count = -EINVAL;
935 goto end;
936 }
Len Brown4be44fc2005-08-05 00:44:28 -0400937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (copy_from_user(limit_string, buffer, count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 count = -EFAULT;
940 goto end;
941 }
Len Brown4be44fc2005-08-05 00:44:28 -0400942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 limit_string[count] = '\0';
944
945 num = sscanf(limit_string, "%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d",
Len Brown4be44fc2005-08-05 00:44:28 -0400946 &critical, &hot, &passive,
947 &active[0], &active[1], &active[2], &active[3], &active[4],
948 &active[5], &active[6], &active[7], &active[8],
949 &active[9]);
950 if (!(num >= 5 && num < (ACPI_THERMAL_MAX_ACTIVE + 3))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 count = -EINVAL;
952 goto end;
953 }
954
955 tz->trips.critical.temperature = CELSIUS_TO_KELVIN(critical);
956 tz->trips.hot.temperature = CELSIUS_TO_KELVIN(hot);
957 tz->trips.passive.temperature = CELSIUS_TO_KELVIN(passive);
958 for (i = 0; i < num - 3; i++) {
959 if (!(tz->trips.active[i].flags.valid))
960 break;
961 tz->trips.active[i].temperature = CELSIUS_TO_KELVIN(active[i]);
962 }
Len Brown4be44fc2005-08-05 00:44:28 -0400963
964 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 kfree(active);
966 kfree(limit_string);
Patrick Mocheld550d982006-06-27 00:41:40 -0400967 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
971{
Len Brown4be44fc2005-08-05 00:44:28 -0400972 struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 if (!tz)
976 goto end;
977
978 if (!tz->flags.cooling_mode) {
979 seq_puts(seq, "<setting not supported>\n");
980 }
981
Len Brown4be44fc2005-08-05 00:44:28 -0400982 if (tz->cooling_mode == ACPI_THERMAL_MODE_CRITICAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 seq_printf(seq, "cooling mode: critical\n");
984 else
985 seq_printf(seq, "cooling mode: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400986 tz->cooling_mode ? "passive" : "active");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Len Brown4be44fc2005-08-05 00:44:28 -0400988 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400989 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
992static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
993{
994 return single_open(file, acpi_thermal_cooling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -0400995 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996}
997
998static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -0400999acpi_thermal_write_cooling_mode(struct file *file,
1000 const char __user * buffer,
1001 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
Len Brown4be44fc2005-08-05 00:44:28 -04001003 struct seq_file *m = (struct seq_file *)file->private_data;
1004 struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
1005 int result = 0;
1006 char mode_string[12] = { '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 if (!tz || (count > sizeof(mode_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001010 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 if (!tz->flags.cooling_mode)
Patrick Mocheld550d982006-06-27 00:41:40 -04001013 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 if (copy_from_user(mode_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001016 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -04001017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 mode_string[count] = '\0';
Len Brown4be44fc2005-08-05 00:44:28 -04001019
1020 result = acpi_thermal_set_cooling_mode(tz,
1021 simple_strtoul(mode_string, NULL,
1022 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001024 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 acpi_thermal_check(tz);
1027
Patrick Mocheld550d982006-06-27 00:41:40 -04001028 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
1032{
Len Brown4be44fc2005-08-05 00:44:28 -04001033 struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 if (!tz)
1037 goto end;
1038
1039 if (!tz->polling_frequency) {
1040 seq_puts(seq, "<polling disabled>\n");
1041 goto end;
1042 }
1043
1044 seq_printf(seq, "polling frequency: %lu seconds\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001045 (tz->polling_frequency / 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Len Brown4be44fc2005-08-05 00:44:28 -04001047 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001048 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
1051static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
1052{
1053 return single_open(file, acpi_thermal_polling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001054 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
1056
1057static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001058acpi_thermal_write_polling(struct file *file,
1059 const char __user * buffer,
1060 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Len Brown4be44fc2005-08-05 00:44:28 -04001062 struct seq_file *m = (struct seq_file *)file->private_data;
1063 struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
1064 int result = 0;
1065 char polling_string[12] = { '\0' };
1066 int seconds = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 if (!tz || (count > sizeof(polling_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001070 return -EINVAL;
Len Brown4be44fc2005-08-05 00:44:28 -04001071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (copy_from_user(polling_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001073 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -04001074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 polling_string[count] = '\0';
1076
1077 seconds = simple_strtoul(polling_string, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 result = acpi_thermal_set_polling(tz, seconds);
1080 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001081 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 acpi_thermal_check(tz);
1084
Patrick Mocheld550d982006-06-27 00:41:40 -04001085 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086}
1087
Len Brown4be44fc2005-08-05 00:44:28 -04001088static int acpi_thermal_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Len Brown4be44fc2005-08-05 00:44:28 -04001090 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 if (!acpi_device_dir(device)) {
1094 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -04001095 acpi_thermal_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001097 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 acpi_device_dir(device)->owner = THIS_MODULE;
1099 }
1100
1101 /* 'state' [R] */
1102 entry = create_proc_entry(ACPI_THERMAL_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -04001103 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001105 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 else {
1107 entry->proc_fops = &acpi_thermal_state_fops;
1108 entry->data = acpi_driver_data(device);
1109 entry->owner = THIS_MODULE;
1110 }
1111
1112 /* 'temperature' [R] */
1113 entry = create_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
Len Brown4be44fc2005-08-05 00:44:28 -04001114 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001116 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 else {
1118 entry->proc_fops = &acpi_thermal_temp_fops;
1119 entry->data = acpi_driver_data(device);
1120 entry->owner = THIS_MODULE;
1121 }
1122
1123 /* 'trip_points' [R/W] */
1124 entry = create_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
Len Brown4be44fc2005-08-05 00:44:28 -04001125 S_IFREG | S_IRUGO | S_IWUSR,
1126 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001128 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 else {
1130 entry->proc_fops = &acpi_thermal_trip_fops;
1131 entry->data = acpi_driver_data(device);
1132 entry->owner = THIS_MODULE;
1133 }
1134
1135 /* 'cooling_mode' [R/W] */
1136 entry = create_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
Len Brown4be44fc2005-08-05 00:44:28 -04001137 S_IFREG | S_IRUGO | S_IWUSR,
1138 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001140 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 else {
1142 entry->proc_fops = &acpi_thermal_cooling_fops;
1143 entry->data = acpi_driver_data(device);
1144 entry->owner = THIS_MODULE;
1145 }
1146
1147 /* 'polling_frequency' [R/W] */
1148 entry = create_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
Len Brown4be44fc2005-08-05 00:44:28 -04001149 S_IFREG | S_IRUGO | S_IWUSR,
1150 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001152 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 else {
1154 entry->proc_fops = &acpi_thermal_polling_fops;
1155 entry->data = acpi_driver_data(device);
1156 entry->owner = THIS_MODULE;
1157 }
1158
Patrick Mocheld550d982006-06-27 00:41:40 -04001159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
Len Brown4be44fc2005-08-05 00:44:28 -04001162static int acpi_thermal_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 if (acpi_device_dir(device)) {
1166 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1167 acpi_device_dir(device));
1168 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1169 acpi_device_dir(device));
1170 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1171 acpi_device_dir(device));
1172 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1173 acpi_device_dir(device));
1174 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1175 acpi_device_dir(device));
1176 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1177 acpi_device_dir(device) = NULL;
1178 }
1179
Patrick Mocheld550d982006-06-27 00:41:40 -04001180 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181}
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183/* --------------------------------------------------------------------------
1184 Driver Interface
1185 -------------------------------------------------------------------------- */
1186
Len Brown4be44fc2005-08-05 00:44:28 -04001187static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Len Brown4be44fc2005-08-05 00:44:28 -04001189 struct acpi_thermal *tz = (struct acpi_thermal *)data;
1190 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001194 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 if (acpi_bus_get_device(tz->handle, &device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001197 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 switch (event) {
1200 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1201 acpi_thermal_check(tz);
1202 break;
1203 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
1204 acpi_thermal_get_trip_points(tz);
1205 acpi_thermal_check(tz);
1206 acpi_bus_generate_event(device, event, 0);
1207 break;
1208 case ACPI_THERMAL_NOTIFY_DEVICES:
1209 if (tz->flags.devices)
1210 acpi_thermal_get_devices(tz);
1211 acpi_bus_generate_event(device, event, 0);
1212 break;
1213 default:
1214 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001215 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 break;
1217 }
1218
Patrick Mocheld550d982006-06-27 00:41:40 -04001219 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
1221
Len Brown4be44fc2005-08-05 00:44:28 -04001222static int acpi_thermal_get_info(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Len Brown4be44fc2005-08-05 00:44:28 -04001224 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001228 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 /* Get temperature [_TMP] (required) */
1231 result = acpi_thermal_get_temperature(tz);
1232 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001233 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 /* Get trip points [_CRT, _PSV, etc.] (required) */
1236 result = acpi_thermal_get_trip_points(tz);
1237 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001238 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 /* Set the cooling mode [_SCP] to active cooling (default) */
1241 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
Len Brown4be44fc2005-08-05 00:44:28 -04001242 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 tz->flags.cooling_mode = 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001244 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 /* Oh,we have not _SCP method.
Len Brown4be44fc2005-08-05 00:44:28 -04001246 Generally show cooling_mode by _ACx, _PSV,spec 12.2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 tz->flags.cooling_mode = 0;
Len Brown4be44fc2005-08-05 00:44:28 -04001248 if (tz->trips.active[0].flags.valid
1249 && tz->trips.passive.flags.valid) {
1250 if (tz->trips.passive.temperature >
1251 tz->trips.active[0].temperature)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 tz->cooling_mode = ACPI_THERMAL_MODE_ACTIVE;
Len Brown4be44fc2005-08-05 00:44:28 -04001253 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 tz->cooling_mode = ACPI_THERMAL_MODE_PASSIVE;
Len Brown4be44fc2005-08-05 00:44:28 -04001255 } else if (!tz->trips.active[0].flags.valid
1256 && tz->trips.passive.flags.valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 tz->cooling_mode = ACPI_THERMAL_MODE_PASSIVE;
Len Brown4be44fc2005-08-05 00:44:28 -04001258 } else if (tz->trips.active[0].flags.valid
1259 && !tz->trips.passive.flags.valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 tz->cooling_mode = ACPI_THERMAL_MODE_ACTIVE;
1261 } else {
1262 /* _ACx and _PSV are optional, but _CRT is required */
1263 tz->cooling_mode = ACPI_THERMAL_MODE_CRITICAL;
1264 }
1265 }
1266
1267 /* Get default polling frequency [_TZP] (optional) */
1268 if (tzp)
1269 tz->polling_frequency = tzp;
1270 else
1271 acpi_thermal_get_polling_frequency(tz);
1272
1273 /* Get devices in this thermal zone [_TZD] (optional) */
1274 result = acpi_thermal_get_devices(tz);
1275 if (!result)
1276 tz->flags.devices = 1;
1277
Patrick Mocheld550d982006-06-27 00:41:40 -04001278 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279}
1280
Len Brown4be44fc2005-08-05 00:44:28 -04001281static int acpi_thermal_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
Len Brown4be44fc2005-08-05 00:44:28 -04001283 int result = 0;
1284 acpi_status status = AE_OK;
1285 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001289 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 tz = kmalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
1292 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001293 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 memset(tz, 0, sizeof(struct acpi_thermal));
1295
1296 tz->handle = device->handle;
1297 strcpy(tz->name, device->pnp.bus_id);
1298 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1299 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1300 acpi_driver_data(device) = tz;
1301
1302 result = acpi_thermal_get_info(tz);
1303 if (result)
1304 goto end;
1305
1306 result = acpi_thermal_add_fs(device);
1307 if (result)
Vasily Averin09047e72006-04-27 05:25:00 -04001308 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 init_timer(&tz->timer);
1311
1312 acpi_thermal_check(tz);
1313
1314 status = acpi_install_notify_handler(tz->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001315 ACPI_DEVICE_NOTIFY,
1316 acpi_thermal_notify, tz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 result = -ENODEV;
1319 goto end;
1320 }
1321
1322 printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001323 acpi_device_name(device), acpi_device_bid(device),
1324 KELVIN_TO_CELSIUS(tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Len Brown4be44fc2005-08-05 00:44:28 -04001326 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 if (result) {
1328 acpi_thermal_remove_fs(device);
1329 kfree(tz);
1330 }
1331
Patrick Mocheld550d982006-06-27 00:41:40 -04001332 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333}
1334
Len Brown4be44fc2005-08-05 00:44:28 -04001335static int acpi_thermal_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
Len Brown4be44fc2005-08-05 00:44:28 -04001337 acpi_status status = AE_OK;
1338 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001342 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Len Brown4be44fc2005-08-05 00:44:28 -04001344 tz = (struct acpi_thermal *)acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 /* avoid timer adding new defer task */
1347 tz->zombie = 1;
1348 /* wait for running timer (on other CPUs) finish */
1349 del_timer_sync(&(tz->timer));
1350 /* synchronize deferred task */
1351 acpi_os_wait_events_complete(NULL);
1352 /* deferred task may reinsert timer */
1353 del_timer_sync(&(tz->timer));
1354
1355 status = acpi_remove_notify_handler(tz->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001356 ACPI_DEVICE_NOTIFY,
1357 acpi_thermal_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 /* Terminate policy */
Len Brown4be44fc2005-08-05 00:44:28 -04001360 if (tz->trips.passive.flags.valid && tz->trips.passive.flags.enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 tz->trips.passive.flags.enabled = 0;
1362 acpi_thermal_passive(tz);
1363 }
1364 if (tz->trips.active[0].flags.valid
Len Brown4be44fc2005-08-05 00:44:28 -04001365 && tz->trips.active[0].flags.enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 tz->trips.active[0].flags.enabled = 0;
1367 acpi_thermal_active(tz);
1368 }
1369
1370 acpi_thermal_remove_fs(device);
1371
1372 kfree(tz);
Patrick Mocheld550d982006-06-27 00:41:40 -04001373 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
1375
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001376static int acpi_thermal_resume(struct acpi_device *device, int state)
1377{
1378 struct acpi_thermal *tz = NULL;
1379
1380 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001381 return -EINVAL;
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001382
1383 tz = (struct acpi_thermal *)acpi_driver_data(device);
1384
1385 acpi_thermal_check(tz);
1386
1387 return AE_OK;
1388}
1389
Len Brown4be44fc2005-08-05 00:44:28 -04001390static int __init acpi_thermal_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
Len Brown4be44fc2005-08-05 00:44:28 -04001392 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1396 if (!acpi_thermal_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001397 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 acpi_thermal_dir->owner = THIS_MODULE;
1399
1400 result = acpi_bus_register_driver(&acpi_thermal_driver);
1401 if (result < 0) {
1402 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04001403 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
1405
Patrick Mocheld550d982006-06-27 00:41:40 -04001406 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407}
1408
Len Brown4be44fc2005-08-05 00:44:28 -04001409static void __exit acpi_thermal_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 acpi_bus_unregister_driver(&acpi_thermal_driver);
1413
1414 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1415
Patrick Mocheld550d982006-06-27 00:41:40 -04001416 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419module_init(acpi_thermal_init);
1420module_exit(acpi_thermal_exit);