blob: 5a62de1b7f2a0e040f1d32a537bc8693a37b0584 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * acpi_thermal.c - ACPI Thermal Zone Driver ($Revision: 41 $)
3 *
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6 *
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 *
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 *
25 * This driver fully implements the ACPI thermal policy as described in the
26 * ACPI 2.0 Specification.
27 *
28 * TBD: 1. Implement passive cooling hysteresis.
29 * 2. Enhance passive cooling (CPU) states/limit interface to support
30 * concepts of 'multiple limiters', upper/lower limits, etc.
31 *
32 */
33
34#include <linux/kernel.h>
35#include <linux/module.h>
36#include <linux/init.h>
37#include <linux/types.h>
38#include <linux/proc_fs.h>
Tim Schmielaucd354f12007-02-14 00:33:14 -080039#include <linux/timer.h>
40#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/kmod.h>
42#include <linux/seq_file.h>
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -070043#include <linux/reboot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/uaccess.h>
45
46#include <acpi/acpi_bus.h>
47#include <acpi/acpi_drivers.h>
48
49#define ACPI_THERMAL_COMPONENT 0x04000000
50#define ACPI_THERMAL_CLASS "thermal_zone"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
52#define ACPI_THERMAL_FILE_STATE "state"
53#define ACPI_THERMAL_FILE_TEMPERATURE "temperature"
54#define ACPI_THERMAL_FILE_TRIP_POINTS "trip_points"
55#define ACPI_THERMAL_FILE_COOLING_MODE "cooling_mode"
56#define ACPI_THERMAL_FILE_POLLING_FREQ "polling_frequency"
57#define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
58#define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
59#define ACPI_THERMAL_NOTIFY_DEVICES 0x82
60#define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
61#define ACPI_THERMAL_NOTIFY_HOT 0xF1
62#define ACPI_THERMAL_MODE_ACTIVE 0x00
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64#define ACPI_THERMAL_MAX_ACTIVE 10
65#define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
66
67#define KELVIN_TO_CELSIUS(t) (long)(((long)t-2732>=0) ? ((long)t-2732+5)/10 : ((long)t-2732-5)/10)
68#define CELSIUS_TO_KELVIN(t) ((t+273)*10)
69
70#define _COMPONENT ACPI_THERMAL_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050071ACPI_MODULE_NAME("thermal");
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Thomas Renninger1cbf4c52004-09-16 11:07:00 -040073MODULE_AUTHOR("Paul Diefenbaugh");
Len Brown7cda93e2007-02-12 23:50:02 -050074MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070075MODULE_LICENSE("GPL");
76
77static int tzp;
78module_param(tzp, int, 0);
79MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.\n");
80
Len Brown4be44fc2005-08-05 00:44:28 -040081static int acpi_thermal_add(struct acpi_device *device);
82static int acpi_thermal_remove(struct acpi_device *device, int type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +080083static int acpi_thermal_resume(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
85static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
86static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -040088static ssize_t acpi_thermal_write_cooling_mode(struct file *,
89 const char __user *, size_t,
90 loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -040092static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
93 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Thomas Renninger1ba90e32007-07-23 14:44:41 +020095static const struct acpi_device_id thermal_device_ids[] = {
96 {ACPI_THERMAL_HID, 0},
97 {"", 0},
98};
99MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static struct acpi_driver acpi_thermal_driver = {
Len Brownc2b67052007-02-12 23:33:40 -0500102 .name = "thermal",
Len Brown4be44fc2005-08-05 00:44:28 -0400103 .class = ACPI_THERMAL_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200104 .ids = thermal_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -0400105 .ops = {
106 .add = acpi_thermal_add,
107 .remove = acpi_thermal_remove,
Konstantin Karasyov74ce14682006-05-08 08:32:00 -0400108 .resume = acpi_thermal_resume,
Len Brown4be44fc2005-08-05 00:44:28 -0400109 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110};
111
112struct acpi_thermal_state {
Len Brown4be44fc2005-08-05 00:44:28 -0400113 u8 critical:1;
114 u8 hot:1;
115 u8 passive:1;
116 u8 active:1;
117 u8 reserved:4;
118 int active_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119};
120
121struct acpi_thermal_state_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400122 u8 valid:1;
123 u8 enabled:1;
124 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
127struct acpi_thermal_critical {
128 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400129 unsigned long temperature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130};
131
132struct acpi_thermal_hot {
133 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400134 unsigned long temperature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135};
136
137struct acpi_thermal_passive {
138 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400139 unsigned long temperature;
140 unsigned long tc1;
141 unsigned long tc2;
142 unsigned long tsp;
143 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144};
145
146struct acpi_thermal_active {
147 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400148 unsigned long temperature;
149 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150};
151
152struct acpi_thermal_trips {
153 struct acpi_thermal_critical critical;
Len Brown4be44fc2005-08-05 00:44:28 -0400154 struct acpi_thermal_hot hot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct acpi_thermal_passive passive;
156 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
157};
158
159struct acpi_thermal_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400160 u8 cooling_mode:1; /* _SCP */
161 u8 devices:1; /* _TZD */
162 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163};
164
165struct acpi_thermal {
Patrick Mochel8348e1b2006-05-19 16:54:40 -0400166 struct acpi_device * device;
Len Brown4be44fc2005-08-05 00:44:28 -0400167 acpi_bus_id name;
168 unsigned long temperature;
169 unsigned long last_temperature;
170 unsigned long polling_frequency;
Len Brown4be44fc2005-08-05 00:44:28 -0400171 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
Arjan van de Vend7508032006-07-04 13:06:00 -0400179static const 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
Arjan van de Vend7508032006-07-04 13:06:00 -0400186static const 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
Arjan van de Vend7508032006-07-04 13:06:00 -0400193static const 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,
Len Brown4be44fc2005-08-05 00:44:28 -0400196 .llseek = seq_lseek,
197 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198};
199
Arjan van de Vend7508032006-07-04 13:06:00 -0400200static const struct file_operations acpi_thermal_cooling_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400201 .open = acpi_thermal_cooling_open_fs,
202 .read = seq_read,
203 .write = acpi_thermal_write_cooling_mode,
204 .llseek = seq_lseek,
205 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206};
207
Arjan van de Vend7508032006-07-04 13:06:00 -0400208static const struct file_operations acpi_thermal_polling_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400209 .open = acpi_thermal_polling_open_fs,
210 .read = seq_read,
211 .write = acpi_thermal_write_polling,
212 .llseek = seq_lseek,
213 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214};
215
216/* --------------------------------------------------------------------------
217 Thermal Zone Management
218 -------------------------------------------------------------------------- */
219
Len Brown4be44fc2005-08-05 00:44:28 -0400220static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Len Brown4be44fc2005-08-05 00:44:28 -0400222 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400226 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 tz->last_temperature = tz->temperature;
229
Len Brown4be44fc2005-08-05 00:44:28 -0400230 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400231 acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tz->temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400233 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
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
Patrick Mocheld550d982006-06-27 00:41:40 -0400238 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400247 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Len Brown4be44fc2005-08-05 00:44:28 -0400249 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400250 acpi_evaluate_integer(tz->device->handle, "_TZP", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400251 &tz->polling_frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400253 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Len Brown4be44fc2005-08-05 00:44:28 -0400255 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
256 tz->polling_frequency));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Patrick Mocheld550d982006-06-27 00:41:40 -0400258 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Len Brown4be44fc2005-08-05 00:44:28 -0400261static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400265 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 tz->polling_frequency = seconds * 10; /* Convert value to deci-seconds */
268
Len Brown4be44fc2005-08-05 00:44:28 -0400269 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
270 "Polling frequency set to %lu seconds\n",
Sanjoy Mahajan636cedf2007-02-16 01:24:43 -0500271 tz->polling_frequency/10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Patrick Mocheld550d982006-06-27 00:41:40 -0400273 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
Len Brown4be44fc2005-08-05 00:44:28 -0400276static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Len Brown4be44fc2005-08-05 00:44:28 -0400278 acpi_status status = AE_OK;
279 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
280 struct acpi_object_list arg_list = { 1, &arg0 };
281 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400285 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400287 status = acpi_get_handle(tz->device->handle, "_SCP", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (ACPI_FAILURE(status)) {
289 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400290 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292
293 arg0.integer.value = mode;
294
295 status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
296 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400297 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Patrick Mocheld550d982006-06-27 00:41:40 -0400299 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
Len Brown4be44fc2005-08-05 00:44:28 -0400302static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Len Brown4be44fc2005-08-05 00:44:28 -0400304 acpi_status status = AE_OK;
305 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400309 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 /* Critical Shutdown (required) */
312
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400313 status = acpi_evaluate_integer(tz->device->handle, "_CRT", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400314 &tz->trips.critical.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (ACPI_FAILURE(status)) {
316 tz->trips.critical.flags.valid = 0;
Thomas Renningera6fc6722006-06-26 23:58:43 -0400317 ACPI_EXCEPTION((AE_INFO, status, "No critical threshold"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400318 return -ENODEV;
Len Brown4be44fc2005-08-05 00:44:28 -0400319 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 tz->trips.critical.flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400321 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
322 "Found critical threshold [%lu]\n",
323 tz->trips.critical.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
325
326 /* Critical Sleep (optional) */
327
Len Brown4be44fc2005-08-05 00:44:28 -0400328 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400329 acpi_evaluate_integer(tz->device->handle, "_HOT", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400330 &tz->trips.hot.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (ACPI_FAILURE(status)) {
332 tz->trips.hot.flags.valid = 0;
333 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No hot threshold\n"));
Len Brown4be44fc2005-08-05 00:44:28 -0400334 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 tz->trips.hot.flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400336 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found hot threshold [%lu]\n",
337 tz->trips.hot.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339
340 /* Passive: Processors (optional) */
341
Len Brown4be44fc2005-08-05 00:44:28 -0400342 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400343 acpi_evaluate_integer(tz->device->handle, "_PSV", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400344 &tz->trips.passive.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (ACPI_FAILURE(status)) {
346 tz->trips.passive.flags.valid = 0;
347 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No passive threshold\n"));
Len Brown4be44fc2005-08-05 00:44:28 -0400348 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 tz->trips.passive.flags.valid = 1;
350
Len Brown4be44fc2005-08-05 00:44:28 -0400351 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400352 acpi_evaluate_integer(tz->device->handle, "_TC1", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400353 &tz->trips.passive.tc1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (ACPI_FAILURE(status))
355 tz->trips.passive.flags.valid = 0;
356
Len Brown4be44fc2005-08-05 00:44:28 -0400357 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400358 acpi_evaluate_integer(tz->device->handle, "_TC2", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400359 &tz->trips.passive.tc2);
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 =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400364 acpi_evaluate_integer(tz->device->handle, "_TSP", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400365 &tz->trips.passive.tsp);
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 =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400370 acpi_evaluate_reference(tz->device->handle, "_PSL", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400371 &tz->trips.passive.devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (ACPI_FAILURE(status))
373 tz->trips.passive.flags.valid = 0;
374
375 if (!tz->trips.passive.flags.valid)
Len Browncece9292006-06-26 23:04:31 -0400376 printk(KERN_WARNING PREFIX "Invalid passive threshold\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 else
Len Brown4be44fc2005-08-05 00:44:28 -0400378 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
379 "Found passive threshold [%lu]\n",
380 tz->trips.passive.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382
383 /* Active: Fans, etc. (optional) */
384
Len Brown4be44fc2005-08-05 00:44:28 -0400385 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Len Brown4be44fc2005-08-05 00:44:28 -0400387 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Len Brown4be44fc2005-08-05 00:44:28 -0400389 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400390 acpi_evaluate_integer(tz->device->handle, name, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400391 &tz->trips.active[i].temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (ACPI_FAILURE(status))
393 break;
394
395 name[2] = 'L';
Len Brown4be44fc2005-08-05 00:44:28 -0400396 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400397 acpi_evaluate_reference(tz->device->handle, name, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400398 &tz->trips.active[i].devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (ACPI_SUCCESS(status)) {
400 tz->trips.active[i].flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400401 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
402 "Found active threshold [%d]:[%lu]\n",
403 i, tz->trips.active[i].temperature));
404 } else
Thomas Renningera6fc6722006-06-26 23:58:43 -0400405 ACPI_EXCEPTION((AE_INFO, status,
406 "Invalid active threshold [%d]", i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408
Patrick Mocheld550d982006-06-27 00:41:40 -0400409 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
Len Brown4be44fc2005-08-05 00:44:28 -0400412static int acpi_thermal_get_devices(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Len Brown4be44fc2005-08-05 00:44:28 -0400414 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400418 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Len Brown4be44fc2005-08-05 00:44:28 -0400420 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400421 acpi_evaluate_reference(tz->device->handle, "_TZD", NULL, &tz->devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400423 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Patrick Mocheld550d982006-06-27 00:41:40 -0400425 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
Len Brown4be44fc2005-08-05 00:44:28 -0400428static int acpi_thermal_critical(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (!tz || !tz->trips.critical.flags.valid)
Patrick Mocheld550d982006-06-27 00:41:40 -0400431 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 if (tz->temperature >= tz->trips.critical.temperature) {
Len Browncece9292006-06-26 23:04:31 -0400434 printk(KERN_WARNING PREFIX "Critical trip point\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 tz->trips.critical.flags.enabled = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400436 } else if (tz->trips.critical.flags.enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 tz->trips.critical.flags.enabled = 0;
438
Len Brown4be44fc2005-08-05 00:44:28 -0400439 printk(KERN_EMERG
440 "Critical temperature reached (%ld C), shutting down.\n",
441 KELVIN_TO_CELSIUS(tz->temperature));
Patrick Mochel8348e1b2006-05-19 16:54:40 -0400442 acpi_bus_generate_event(tz->device, ACPI_THERMAL_NOTIFY_CRITICAL,
Len Brown4be44fc2005-08-05 00:44:28 -0400443 tz->trips.critical.flags.enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -0700445 orderly_poweroff(true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Patrick Mocheld550d982006-06-27 00:41:40 -0400447 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Len Brown4be44fc2005-08-05 00:44:28 -0400450static int acpi_thermal_hot(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (!tz || !tz->trips.hot.flags.valid)
Patrick Mocheld550d982006-06-27 00:41:40 -0400453 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 if (tz->temperature >= tz->trips.hot.temperature) {
Len Browncece9292006-06-26 23:04:31 -0400456 printk(KERN_WARNING PREFIX "Hot trip point\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 tz->trips.hot.flags.enabled = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400458 } else if (tz->trips.hot.flags.enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 tz->trips.hot.flags.enabled = 0;
460
Patrick Mochel8348e1b2006-05-19 16:54:40 -0400461 acpi_bus_generate_event(tz->device, ACPI_THERMAL_NOTIFY_HOT,
Len Brown4be44fc2005-08-05 00:44:28 -0400462 tz->trips.hot.flags.enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 /* TBD: Call user-mode "sleep(S4)" function */
465
Patrick Mocheld550d982006-06-27 00:41:40 -0400466 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400469static void acpi_thermal_passive(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400471 int result = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 struct acpi_thermal_passive *passive = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400473 int trend = 0;
474 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 if (!tz || !tz->trips.passive.flags.valid)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400478 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 passive = &(tz->trips.passive);
481
482 /*
483 * Above Trip?
484 * -----------
485 * Calculate the thermal trend (using the passive cooling equation)
486 * and modify the performance limit for all passive cooling devices
487 * accordingly. Note that we assume symmetry.
488 */
489 if (tz->temperature >= passive->temperature) {
Len Brown4be44fc2005-08-05 00:44:28 -0400490 trend =
491 (passive->tc1 * (tz->temperature - tz->last_temperature)) +
492 (passive->tc2 * (tz->temperature - passive->temperature));
493 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
494 "trend[%d]=(tc1[%lu]*(tmp[%lu]-last[%lu]))+(tc2[%lu]*(tmp[%lu]-psv[%lu]))\n",
495 trend, passive->tc1, tz->temperature,
496 tz->last_temperature, passive->tc2,
497 tz->temperature, passive->temperature));
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400498 passive->flags.enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 /* Heating up? */
500 if (trend > 0)
Len Brown4be44fc2005-08-05 00:44:28 -0400501 for (i = 0; i < passive->devices.count; i++)
502 acpi_processor_set_thermal_limit(passive->
503 devices.
504 handles[i],
505 ACPI_PROCESSOR_LIMIT_INCREMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 /* Cooling off? */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400507 else if (trend < 0) {
Len Brown4be44fc2005-08-05 00:44:28 -0400508 for (i = 0; i < passive->devices.count; i++)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400509 /*
510 * assume that we are on highest
511 * freq/lowest thrott and can leave
512 * passive mode, even in error case
513 */
514 if (!acpi_processor_set_thermal_limit
515 (passive->devices.handles[i],
516 ACPI_PROCESSOR_LIMIT_DECREMENT))
517 result = 0;
518 /*
519 * Leave cooling mode, even if the temp might
520 * higher than trip point This is because some
521 * machines might have long thermal polling
522 * frequencies (tsp) defined. We will fall back
523 * into passive mode in next cycle (probably quicker)
524 */
525 if (result) {
526 passive->flags.enabled = 0;
527 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
528 "Disabling passive cooling, still above threshold,"
529 " but we are cooling down\n"));
530 }
531 }
532 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534
535 /*
536 * Below Trip?
537 * -----------
538 * Implement passive cooling hysteresis to slowly increase performance
539 * and avoid thrashing around the passive trip point. Note that we
540 * assume symmetry.
541 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400542 if (!passive->flags.enabled)
543 return;
544 for (i = 0; i < passive->devices.count; i++)
545 if (!acpi_processor_set_thermal_limit
546 (passive->devices.handles[i],
547 ACPI_PROCESSOR_LIMIT_DECREMENT))
548 result = 0;
549 if (result) {
550 passive->flags.enabled = 0;
551 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
552 "Disabling passive cooling (zone is cool)\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400556static void acpi_thermal_active(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Len Brown4be44fc2005-08-05 00:44:28 -0400558 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 struct acpi_thermal_active *active = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400560 int i = 0;
561 int j = 0;
562 unsigned long maxtemp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 if (!tz)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400566 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Len Brown4be44fc2005-08-05 00:44:28 -0400568 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 active = &(tz->trips.active[i]);
570 if (!active || !active->flags.valid)
571 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (tz->temperature >= active->temperature) {
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400573 /*
574 * Above Threshold?
575 * ----------------
576 * If not already enabled, turn ON all cooling devices
577 * associated with this active threshold.
578 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (active->temperature > maxtemp)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400580 tz->state.active_index = i;
581 maxtemp = active->temperature;
582 if (active->flags.enabled)
583 continue;
584 for (j = 0; j < active->devices.count; j++) {
585 result =
586 acpi_bus_set_power(active->devices.
587 handles[j],
588 ACPI_STATE_D0);
589 if (result) {
Len Browncece9292006-06-26 23:04:31 -0400590 printk(KERN_WARNING PREFIX
591 "Unable to turn cooling device [%p] 'on'\n",
Thomas Renningera6fc6722006-06-26 23:58:43 -0400592 active->devices.
Len Browncece9292006-06-26 23:04:31 -0400593 handles[j]);
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400594 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400596 active->flags.enabled = 1;
597 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
598 "Cooling device [%p] now 'on'\n",
599 active->devices.handles[j]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400601 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400603 if (!active->flags.enabled)
604 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 /*
606 * Below Threshold?
607 * ----------------
608 * Turn OFF all cooling devices associated with this
609 * threshold.
610 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400611 for (j = 0; j < active->devices.count; j++) {
612 result = acpi_bus_set_power(active->devices.handles[j],
613 ACPI_STATE_D3);
614 if (result) {
Len Browncece9292006-06-26 23:04:31 -0400615 printk(KERN_WARNING PREFIX
616 "Unable to turn cooling device [%p] 'off'\n",
617 active->devices.handles[j]);
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400618 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400620 active->flags.enabled = 0;
621 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
622 "Cooling device [%p] now 'off'\n",
623 active->devices.handles[j]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
Len Brown4be44fc2005-08-05 00:44:28 -0400628static void acpi_thermal_check(void *context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Len Brown4be44fc2005-08-05 00:44:28 -0400630static void acpi_thermal_run(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
632 struct acpi_thermal *tz = (struct acpi_thermal *)data;
633 if (!tz->zombie)
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400634 acpi_os_execute(OSL_GPE_HANDLER, acpi_thermal_check, (void *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
Len Brown4be44fc2005-08-05 00:44:28 -0400637static void acpi_thermal_check(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Len Brown4be44fc2005-08-05 00:44:28 -0400639 int result = 0;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200640 struct acpi_thermal *tz = data;
Len Brown4be44fc2005-08-05 00:44:28 -0400641 unsigned long sleep_time = 0;
642 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 struct acpi_thermal_state state;
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 if (!tz) {
Len Brown64684632006-06-26 23:41:38 -0400647 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400648 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650
651 state = tz->state;
652
653 result = acpi_thermal_get_temperature(tz);
654 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400655 return;
Len Brown4be44fc2005-08-05 00:44:28 -0400656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 memset(&tz->state, 0, sizeof(tz->state));
Len Brown4be44fc2005-08-05 00:44:28 -0400658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 /*
660 * Check Trip Points
661 * -----------------
662 * Compare the current temperature to the trip point values to see
663 * if we've entered one of the thermal policy states. Note that
664 * this function determines when a state is entered, but the
665 * individual policy decides when it is exited (e.g. hysteresis).
666 */
667 if (tz->trips.critical.flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400668 state.critical |=
669 (tz->temperature >= tz->trips.critical.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (tz->trips.hot.flags.valid)
671 state.hot |= (tz->temperature >= tz->trips.hot.temperature);
672 if (tz->trips.passive.flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400673 state.passive |=
674 (tz->temperature >= tz->trips.passive.temperature);
675 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (tz->trips.active[i].flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400677 state.active |=
678 (tz->temperature >=
679 tz->trips.active[i].temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 /*
682 * Invoke Policy
683 * -------------
684 * Separated from the above check to allow individual policy to
685 * determine when to exit a given state.
686 */
687 if (state.critical)
688 acpi_thermal_critical(tz);
689 if (state.hot)
690 acpi_thermal_hot(tz);
691 if (state.passive)
692 acpi_thermal_passive(tz);
693 if (state.active)
694 acpi_thermal_active(tz);
695
696 /*
697 * Calculate State
698 * ---------------
699 * Again, separated from the above two to allow independent policy
700 * decisions.
701 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400702 tz->state.critical = tz->trips.critical.flags.enabled;
703 tz->state.hot = tz->trips.hot.flags.enabled;
704 tz->state.passive = tz->trips.passive.flags.enabled;
705 tz->state.active = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400706 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400707 tz->state.active |= tz->trips.active[i].flags.enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 /*
710 * Calculate Sleep Time
711 * --------------------
712 * If we're in the passive state, use _TSP's value. Otherwise
713 * use the default polling frequency (e.g. _TZP). If no polling
714 * frequency is specified then we'll wait forever (at least until
715 * a thermal event occurs). Note that _TSP and _TZD values are
716 * given in 1/10th seconds (we must covert to milliseconds).
717 */
718 if (tz->state.passive)
719 sleep_time = tz->trips.passive.tsp * 100;
720 else if (tz->polling_frequency > 0)
721 sleep_time = tz->polling_frequency * 100;
722
Len Brown4be44fc2005-08-05 00:44:28 -0400723 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: temperature[%lu] sleep[%lu]\n",
724 tz->name, tz->temperature, sleep_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 /*
727 * Schedule Next Poll
728 * ------------------
729 */
730 if (!sleep_time) {
731 if (timer_pending(&(tz->timer)))
732 del_timer(&(tz->timer));
Len Brown4be44fc2005-08-05 00:44:28 -0400733 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (timer_pending(&(tz->timer)))
Andrew Morton94e22e12007-04-23 14:41:13 -0700735 mod_timer(&(tz->timer),
736 jiffies + (HZ * sleep_time) / 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400738 tz->timer.data = (unsigned long)tz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 tz->timer.function = acpi_thermal_run;
740 tz->timer.expires = jiffies + (HZ * sleep_time) / 1000;
741 add_timer(&(tz->timer));
742 }
743 }
744
Patrick Mocheld550d982006-06-27 00:41:40 -0400745 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748/* --------------------------------------------------------------------------
749 FS Interface (/proc)
750 -------------------------------------------------------------------------- */
751
Len Brown4be44fc2005-08-05 00:44:28 -0400752static struct proc_dir_entry *acpi_thermal_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
755{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200756 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 if (!tz)
760 goto end;
761
762 seq_puts(seq, "state: ");
763
Len Brown4be44fc2005-08-05 00:44:28 -0400764 if (!tz->state.critical && !tz->state.hot && !tz->state.passive
765 && !tz->state.active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 seq_puts(seq, "ok\n");
767 else {
768 if (tz->state.critical)
769 seq_puts(seq, "critical ");
770 if (tz->state.hot)
771 seq_puts(seq, "hot ");
772 if (tz->state.passive)
773 seq_puts(seq, "passive ");
774 if (tz->state.active)
775 seq_printf(seq, "active[%d]", tz->state.active_index);
776 seq_puts(seq, "\n");
777 }
778
Len Brown4be44fc2005-08-05 00:44:28 -0400779 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400780 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
783static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
784{
785 return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
786}
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
789{
Len Brown4be44fc2005-08-05 00:44:28 -0400790 int result = 0;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200791 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 if (!tz)
795 goto end;
796
797 result = acpi_thermal_get_temperature(tz);
798 if (result)
799 goto end;
800
Len Brown4be44fc2005-08-05 00:44:28 -0400801 seq_printf(seq, "temperature: %ld C\n",
802 KELVIN_TO_CELSIUS(tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Len Brown4be44fc2005-08-05 00:44:28 -0400804 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400805 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
808static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
809{
810 return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
811}
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
814{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200815 struct acpi_thermal *tz = seq->private;
Thomas Renninger68ccfaa2006-11-19 23:01:40 +0100816 struct acpi_device *device;
Thomas Renningere7c746e2007-06-18 00:40:51 -0400817 acpi_status status;
818
Len Brown4be44fc2005-08-05 00:44:28 -0400819 int i = 0;
820 int j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 if (!tz)
824 goto end;
825
826 if (tz->trips.critical.flags.valid)
827 seq_printf(seq, "critical (S5): %ld C\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400828 KELVIN_TO_CELSIUS(tz->trips.critical.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 if (tz->trips.hot.flags.valid)
831 seq_printf(seq, "hot (S4): %ld C\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400832 KELVIN_TO_CELSIUS(tz->trips.hot.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 if (tz->trips.passive.flags.valid) {
Len Brown4be44fc2005-08-05 00:44:28 -0400835 seq_printf(seq,
836 "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
837 KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
838 tz->trips.passive.tc1, tz->trips.passive.tc2,
839 tz->trips.passive.tsp);
840 for (j = 0; j < tz->trips.passive.devices.count; j++) {
Thomas Renningere7c746e2007-06-18 00:40:51 -0400841 status = acpi_bus_get_device(tz->trips.passive.devices.
842 handles[j], &device);
843 seq_printf(seq, "%4.4s ", status ? "" :
844 acpi_device_bid(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
846 seq_puts(seq, "\n");
847 }
848
849 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
850 if (!(tz->trips.active[i].flags.valid))
851 break;
852 seq_printf(seq, "active[%d]: %ld C: devices=",
Len Brown4be44fc2005-08-05 00:44:28 -0400853 i,
854 KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
Thomas Renninger68ccfaa2006-11-19 23:01:40 +0100855 for (j = 0; j < tz->trips.active[i].devices.count; j++){
Thomas Renningere7c746e2007-06-18 00:40:51 -0400856 status = acpi_bus_get_device(tz->trips.active[i].
857 devices.handles[j],
858 &device);
859 seq_printf(seq, "%4.4s ", status ? "" :
860 acpi_device_bid(device));
Thomas Renninger68ccfaa2006-11-19 23:01:40 +0100861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 seq_puts(seq, "\n");
863 }
864
Len Brown4be44fc2005-08-05 00:44:28 -0400865 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400866 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
869static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
870{
871 return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
872}
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
875{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200876 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 if (!tz)
880 goto end;
881
Len Browneaca2d32007-04-30 23:27:43 -0400882 if (!tz->flags.cooling_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 seq_puts(seq, "<setting not supported>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 else
Len Browneaca2d32007-04-30 23:27:43 -0400885 seq_puts(seq, "0 - Active; 1 - Passive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Len Brown4be44fc2005-08-05 00:44:28 -0400887 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400888 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
891static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
892{
893 return single_open(file, acpi_thermal_cooling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -0400894 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895}
896
897static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -0400898acpi_thermal_write_cooling_mode(struct file *file,
899 const char __user * buffer,
900 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200902 struct seq_file *m = file->private_data;
903 struct acpi_thermal *tz = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400904 int result = 0;
905 char mode_string[12] = { '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 if (!tz || (count > sizeof(mode_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400909 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 if (!tz->flags.cooling_mode)
Patrick Mocheld550d982006-06-27 00:41:40 -0400912 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 if (copy_from_user(mode_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -0400915 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -0400916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 mode_string[count] = '\0';
Len Brown4be44fc2005-08-05 00:44:28 -0400918
919 result = acpi_thermal_set_cooling_mode(tz,
920 simple_strtoul(mode_string, NULL,
921 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400923 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 acpi_thermal_check(tz);
926
Patrick Mocheld550d982006-06-27 00:41:40 -0400927 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
931{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200932 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 if (!tz)
936 goto end;
937
938 if (!tz->polling_frequency) {
939 seq_puts(seq, "<polling disabled>\n");
940 goto end;
941 }
942
943 seq_printf(seq, "polling frequency: %lu seconds\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400944 (tz->polling_frequency / 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Len Brown4be44fc2005-08-05 00:44:28 -0400946 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400947 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
949
950static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
951{
952 return single_open(file, acpi_thermal_polling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -0400953 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954}
955
956static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -0400957acpi_thermal_write_polling(struct file *file,
958 const char __user * buffer,
959 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200961 struct seq_file *m = file->private_data;
962 struct acpi_thermal *tz = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400963 int result = 0;
964 char polling_string[12] = { '\0' };
965 int seconds = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 if (!tz || (count > sizeof(polling_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400969 return -EINVAL;
Len Brown4be44fc2005-08-05 00:44:28 -0400970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (copy_from_user(polling_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -0400972 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -0400973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 polling_string[count] = '\0';
975
976 seconds = simple_strtoul(polling_string, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -0400977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 result = acpi_thermal_set_polling(tz, seconds);
979 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400980 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 acpi_thermal_check(tz);
983
Patrick Mocheld550d982006-06-27 00:41:40 -0400984 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
Len Brown4be44fc2005-08-05 00:44:28 -0400987static int acpi_thermal_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
Len Brown4be44fc2005-08-05 00:44:28 -0400989 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 if (!acpi_device_dir(device)) {
993 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400994 acpi_thermal_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400996 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 acpi_device_dir(device)->owner = THIS_MODULE;
998 }
999
1000 /* 'state' [R] */
1001 entry = create_proc_entry(ACPI_THERMAL_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -04001002 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001004 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 else {
1006 entry->proc_fops = &acpi_thermal_state_fops;
1007 entry->data = acpi_driver_data(device);
1008 entry->owner = THIS_MODULE;
1009 }
1010
1011 /* 'temperature' [R] */
1012 entry = create_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
Len Brown4be44fc2005-08-05 00:44:28 -04001013 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001015 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 else {
1017 entry->proc_fops = &acpi_thermal_temp_fops;
1018 entry->data = acpi_driver_data(device);
1019 entry->owner = THIS_MODULE;
1020 }
1021
1022 /* 'trip_points' [R/W] */
1023 entry = create_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
Len Brown4be44fc2005-08-05 00:44:28 -04001024 S_IFREG | S_IRUGO | S_IWUSR,
1025 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001027 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 else {
1029 entry->proc_fops = &acpi_thermal_trip_fops;
1030 entry->data = acpi_driver_data(device);
1031 entry->owner = THIS_MODULE;
1032 }
1033
1034 /* 'cooling_mode' [R/W] */
1035 entry = create_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
Len Brown4be44fc2005-08-05 00:44:28 -04001036 S_IFREG | S_IRUGO | S_IWUSR,
1037 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001039 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 else {
1041 entry->proc_fops = &acpi_thermal_cooling_fops;
1042 entry->data = acpi_driver_data(device);
1043 entry->owner = THIS_MODULE;
1044 }
1045
1046 /* 'polling_frequency' [R/W] */
1047 entry = create_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
Len Brown4be44fc2005-08-05 00:44:28 -04001048 S_IFREG | S_IRUGO | S_IWUSR,
1049 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001051 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 else {
1053 entry->proc_fops = &acpi_thermal_polling_fops;
1054 entry->data = acpi_driver_data(device);
1055 entry->owner = THIS_MODULE;
1056 }
1057
Patrick Mocheld550d982006-06-27 00:41:40 -04001058 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
Len Brown4be44fc2005-08-05 00:44:28 -04001061static int acpi_thermal_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 if (acpi_device_dir(device)) {
1065 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1066 acpi_device_dir(device));
1067 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1068 acpi_device_dir(device));
1069 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1070 acpi_device_dir(device));
1071 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1072 acpi_device_dir(device));
1073 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1074 acpi_device_dir(device));
1075 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1076 acpi_device_dir(device) = NULL;
1077 }
1078
Patrick Mocheld550d982006-06-27 00:41:40 -04001079 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082/* --------------------------------------------------------------------------
1083 Driver Interface
1084 -------------------------------------------------------------------------- */
1085
Len Brown4be44fc2005-08-05 00:44:28 -04001086static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001088 struct acpi_thermal *tz = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001089 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001093 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Patrick Mochel8348e1b2006-05-19 16:54:40 -04001095 device = tz->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 switch (event) {
1098 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1099 acpi_thermal_check(tz);
1100 break;
1101 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
1102 acpi_thermal_get_trip_points(tz);
1103 acpi_thermal_check(tz);
1104 acpi_bus_generate_event(device, event, 0);
1105 break;
1106 case ACPI_THERMAL_NOTIFY_DEVICES:
1107 if (tz->flags.devices)
1108 acpi_thermal_get_devices(tz);
1109 acpi_bus_generate_event(device, event, 0);
1110 break;
1111 default:
1112 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001113 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 break;
1115 }
1116
Patrick Mocheld550d982006-06-27 00:41:40 -04001117 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
Len Brown4be44fc2005-08-05 00:44:28 -04001120static int acpi_thermal_get_info(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
Len Brown4be44fc2005-08-05 00:44:28 -04001122 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001126 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 /* Get temperature [_TMP] (required) */
1129 result = acpi_thermal_get_temperature(tz);
1130 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001131 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133 /* Get trip points [_CRT, _PSV, etc.] (required) */
1134 result = acpi_thermal_get_trip_points(tz);
1135 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001136 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 /* Set the cooling mode [_SCP] to active cooling (default) */
1139 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
Len Brown4be44fc2005-08-05 00:44:28 -04001140 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 tz->flags.cooling_mode = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 /* Get default polling frequency [_TZP] (optional) */
1144 if (tzp)
1145 tz->polling_frequency = tzp;
1146 else
1147 acpi_thermal_get_polling_frequency(tz);
1148
1149 /* Get devices in this thermal zone [_TZD] (optional) */
1150 result = acpi_thermal_get_devices(tz);
1151 if (!result)
1152 tz->flags.devices = 1;
1153
Patrick Mocheld550d982006-06-27 00:41:40 -04001154 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155}
1156
Len Brown4be44fc2005-08-05 00:44:28 -04001157static int acpi_thermal_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
Len Brown4be44fc2005-08-05 00:44:28 -04001159 int result = 0;
1160 acpi_status status = AE_OK;
1161 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001165 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Burman Yan36bcbec2006-12-19 12:56:11 -08001167 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001169 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Patrick Mochel8348e1b2006-05-19 16:54:40 -04001171 tz->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 strcpy(tz->name, device->pnp.bus_id);
1173 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1174 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1175 acpi_driver_data(device) = tz;
1176
1177 result = acpi_thermal_get_info(tz);
1178 if (result)
1179 goto end;
1180
1181 result = acpi_thermal_add_fs(device);
1182 if (result)
Vasily Averin09047e72006-04-27 05:25:00 -04001183 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 init_timer(&tz->timer);
1186
1187 acpi_thermal_check(tz);
1188
Patrick Mochel38ba7c92006-05-19 16:54:48 -04001189 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001190 ACPI_DEVICE_NOTIFY,
1191 acpi_thermal_notify, tz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 result = -ENODEV;
1194 goto end;
1195 }
1196
1197 printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001198 acpi_device_name(device), acpi_device_bid(device),
1199 KELVIN_TO_CELSIUS(tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Len Brown4be44fc2005-08-05 00:44:28 -04001201 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (result) {
1203 acpi_thermal_remove_fs(device);
1204 kfree(tz);
1205 }
1206
Patrick Mocheld550d982006-06-27 00:41:40 -04001207 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
Len Brown4be44fc2005-08-05 00:44:28 -04001210static int acpi_thermal_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Len Brown4be44fc2005-08-05 00:44:28 -04001212 acpi_status status = AE_OK;
1213 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001217 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001219 tz = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 /* avoid timer adding new defer task */
1222 tz->zombie = 1;
1223 /* wait for running timer (on other CPUs) finish */
1224 del_timer_sync(&(tz->timer));
1225 /* synchronize deferred task */
1226 acpi_os_wait_events_complete(NULL);
1227 /* deferred task may reinsert timer */
1228 del_timer_sync(&(tz->timer));
1229
Patrick Mochel38ba7c92006-05-19 16:54:48 -04001230 status = acpi_remove_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001231 ACPI_DEVICE_NOTIFY,
1232 acpi_thermal_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 /* Terminate policy */
Len Brown4be44fc2005-08-05 00:44:28 -04001235 if (tz->trips.passive.flags.valid && tz->trips.passive.flags.enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 tz->trips.passive.flags.enabled = 0;
1237 acpi_thermal_passive(tz);
1238 }
1239 if (tz->trips.active[0].flags.valid
Len Brown4be44fc2005-08-05 00:44:28 -04001240 && tz->trips.active[0].flags.enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 tz->trips.active[0].flags.enabled = 0;
1242 acpi_thermal_active(tz);
1243 }
1244
1245 acpi_thermal_remove_fs(device);
1246
1247 kfree(tz);
Patrick Mocheld550d982006-06-27 00:41:40 -04001248 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001251static int acpi_thermal_resume(struct acpi_device *device)
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001252{
1253 struct acpi_thermal *tz = NULL;
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001254 int i, j, power_state, result;
1255
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001256
1257 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001258 return -EINVAL;
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001259
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001260 tz = acpi_driver_data(device);
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001261
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001262 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001263 if (!(&tz->trips.active[i]))
1264 break;
1265 if (!tz->trips.active[i].flags.valid)
1266 break;
1267 tz->trips.active[i].flags.enabled = 1;
1268 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1269 result = acpi_bus_get_power(tz->trips.active[i].devices.
1270 handles[j], &power_state);
1271 if (result || (power_state != ACPI_STATE_D0)) {
1272 tz->trips.active[i].flags.enabled = 0;
1273 break;
1274 }
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001275 }
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001276 tz->state.active |= tz->trips.active[i].flags.enabled;
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001277 }
1278
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001279 acpi_thermal_check(tz);
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001280
1281 return AE_OK;
1282}
1283
Len Brown4be44fc2005-08-05 00:44:28 -04001284static int __init acpi_thermal_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
Len Brown4be44fc2005-08-05 00:44:28 -04001286 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1290 if (!acpi_thermal_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001291 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 acpi_thermal_dir->owner = THIS_MODULE;
1293
1294 result = acpi_bus_register_driver(&acpi_thermal_driver);
1295 if (result < 0) {
1296 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04001297 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
1299
Patrick Mocheld550d982006-06-27 00:41:40 -04001300 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301}
1302
Len Brown4be44fc2005-08-05 00:44:28 -04001303static void __exit acpi_thermal_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 acpi_bus_unregister_driver(&acpi_thermal_driver);
1307
1308 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1309
Patrick Mocheld550d982006-06-27 00:41:40 -04001310 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
1312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313module_init(acpi_thermal_init);
1314module_exit(acpi_thermal_exit);