blob: 5f79b44512120e489950f65a22d912aa88ed66ec [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>
Len Brown0b5bfa12007-08-12 00:13:02 -040036#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/init.h>
38#include <linux/types.h>
39#include <linux/proc_fs.h>
Tim Schmielaucd354f12007-02-14 00:33:14 -080040#include <linux/timer.h>
41#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/kmod.h>
43#include <linux/seq_file.h>
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -070044#include <linux/reboot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/uaccess.h>
46
47#include <acpi/acpi_bus.h>
48#include <acpi/acpi_drivers.h>
49
50#define ACPI_THERMAL_COMPONENT 0x04000000
51#define ACPI_THERMAL_CLASS "thermal_zone"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
53#define ACPI_THERMAL_FILE_STATE "state"
54#define ACPI_THERMAL_FILE_TEMPERATURE "temperature"
55#define ACPI_THERMAL_FILE_TRIP_POINTS "trip_points"
56#define ACPI_THERMAL_FILE_COOLING_MODE "cooling_mode"
57#define ACPI_THERMAL_FILE_POLLING_FREQ "polling_frequency"
58#define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
59#define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
60#define ACPI_THERMAL_NOTIFY_DEVICES 0x82
61#define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
62#define ACPI_THERMAL_NOTIFY_HOT 0xF1
63#define ACPI_THERMAL_MODE_ACTIVE 0x00
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65#define ACPI_THERMAL_MAX_ACTIVE 10
66#define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
67
68#define KELVIN_TO_CELSIUS(t) (long)(((long)t-2732>=0) ? ((long)t-2732+5)/10 : ((long)t-2732-5)/10)
69#define CELSIUS_TO_KELVIN(t) ((t+273)*10)
70
71#define _COMPONENT ACPI_THERMAL_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050072ACPI_MODULE_NAME("thermal");
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Thomas Renninger1cbf4c52004-09-16 11:07:00 -040074MODULE_AUTHOR("Paul Diefenbaugh");
Len Brown7cda93e2007-02-12 23:50:02 -050075MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070076MODULE_LICENSE("GPL");
77
Len Brownf8707ec2007-08-12 00:12:54 -040078static int act;
79module_param(act, int, 0644);
Len Brown3c1d36d2007-08-14 15:12:56 -040080MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.");
Len Brownf8707ec2007-08-12 00:12:54 -040081
Len Brownc52a7412007-08-14 15:49:32 -040082static int crt;
83module_param(crt, int, 0644);
84MODULE_PARM_DESC(crt, "Disable or lower all critical trip points.");
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static int tzp;
Len Brown730ff342007-08-12 00:12:26 -040087module_param(tzp, int, 0444);
Len Brown3c1d36d2007-08-14 15:12:56 -040088MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Len Brownf5487142007-08-12 00:12:44 -040090static int nocrt;
91module_param(nocrt, int, 0);
Len Brown8c99fdc2007-08-20 18:46:50 -040092MODULE_PARM_DESC(nocrt, "Set to take no action upon ACPI thermal zone critical trips points.");
Len Brownf5487142007-08-12 00:12:44 -040093
Len Brown72b33ef2007-08-12 00:12:17 -040094static int off;
95module_param(off, int, 0);
Len Brown3c1d36d2007-08-14 15:12:56 -040096MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
Len Brown72b33ef2007-08-12 00:12:17 -040097
Len Browna70cdc52007-08-12 00:12:35 -040098static int psv;
99module_param(psv, int, 0644);
Len Brown3c1d36d2007-08-14 15:12:56 -0400100MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
Len Browna70cdc52007-08-12 00:12:35 -0400101
Len Brown4be44fc2005-08-05 00:44:28 -0400102static int acpi_thermal_add(struct acpi_device *device);
103static int acpi_thermal_remove(struct acpi_device *device, int type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800104static int acpi_thermal_resume(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
106static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
107static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -0400109static ssize_t acpi_thermal_write_cooling_mode(struct file *,
110 const char __user *, size_t,
111 loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -0400113static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
114 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200116static const struct acpi_device_id thermal_device_ids[] = {
117 {ACPI_THERMAL_HID, 0},
118 {"", 0},
119};
120MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static struct acpi_driver acpi_thermal_driver = {
Len Brownc2b67052007-02-12 23:33:40 -0500123 .name = "thermal",
Len Brown4be44fc2005-08-05 00:44:28 -0400124 .class = ACPI_THERMAL_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200125 .ids = thermal_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -0400126 .ops = {
127 .add = acpi_thermal_add,
128 .remove = acpi_thermal_remove,
Konstantin Karasyov74ce14682006-05-08 08:32:00 -0400129 .resume = acpi_thermal_resume,
Len Brown4be44fc2005-08-05 00:44:28 -0400130 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
133struct acpi_thermal_state {
Len Brown4be44fc2005-08-05 00:44:28 -0400134 u8 critical:1;
135 u8 hot:1;
136 u8 passive:1;
137 u8 active:1;
138 u8 reserved:4;
139 int active_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140};
141
142struct acpi_thermal_state_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400143 u8 valid:1;
144 u8 enabled:1;
145 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146};
147
148struct acpi_thermal_critical {
149 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400150 unsigned long temperature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
153struct acpi_thermal_hot {
154 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400155 unsigned long temperature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156};
157
158struct acpi_thermal_passive {
159 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400160 unsigned long temperature;
161 unsigned long tc1;
162 unsigned long tc2;
163 unsigned long tsp;
164 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165};
166
167struct acpi_thermal_active {
168 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400169 unsigned long temperature;
170 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171};
172
173struct acpi_thermal_trips {
174 struct acpi_thermal_critical critical;
Len Brown4be44fc2005-08-05 00:44:28 -0400175 struct acpi_thermal_hot hot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 struct acpi_thermal_passive passive;
177 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
178};
179
180struct acpi_thermal_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400181 u8 cooling_mode:1; /* _SCP */
182 u8 devices:1; /* _TZD */
183 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184};
185
186struct acpi_thermal {
Patrick Mochel8348e1b2006-05-19 16:54:40 -0400187 struct acpi_device * device;
Len Brown4be44fc2005-08-05 00:44:28 -0400188 acpi_bus_id name;
189 unsigned long temperature;
190 unsigned long last_temperature;
191 unsigned long polling_frequency;
Len Brown4be44fc2005-08-05 00:44:28 -0400192 volatile u8 zombie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 struct acpi_thermal_flags flags;
194 struct acpi_thermal_state state;
195 struct acpi_thermal_trips trips;
Len Brown4be44fc2005-08-05 00:44:28 -0400196 struct acpi_handle_list devices;
197 struct timer_list timer;
Alexey Starikovskiy6e215782007-09-01 00:11:59 +0400198 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199};
200
Arjan van de Vend7508032006-07-04 13:06:00 -0400201static const struct file_operations acpi_thermal_state_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400202 .open = acpi_thermal_state_open_fs,
203 .read = seq_read,
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_temp_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400209 .open = acpi_thermal_temp_open_fs,
210 .read = seq_read,
211 .llseek = seq_lseek,
212 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213};
214
Arjan van de Vend7508032006-07-04 13:06:00 -0400215static const struct file_operations acpi_thermal_trip_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400216 .open = acpi_thermal_trip_open_fs,
217 .read = seq_read,
Len Brown4be44fc2005-08-05 00:44:28 -0400218 .llseek = seq_lseek,
219 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220};
221
Arjan van de Vend7508032006-07-04 13:06:00 -0400222static const struct file_operations acpi_thermal_cooling_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400223 .open = acpi_thermal_cooling_open_fs,
224 .read = seq_read,
225 .write = acpi_thermal_write_cooling_mode,
226 .llseek = seq_lseek,
227 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228};
229
Arjan van de Vend7508032006-07-04 13:06:00 -0400230static const struct file_operations acpi_thermal_polling_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400231 .open = acpi_thermal_polling_open_fs,
232 .read = seq_read,
233 .write = acpi_thermal_write_polling,
234 .llseek = seq_lseek,
235 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236};
237
238/* --------------------------------------------------------------------------
239 Thermal Zone Management
240 -------------------------------------------------------------------------- */
241
Len Brown4be44fc2005-08-05 00:44:28 -0400242static int acpi_thermal_get_temperature(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
250 tz->last_temperature = tz->temperature;
251
Len Brown4be44fc2005-08-05 00:44:28 -0400252 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400253 acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tz->temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400255 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Len Brown4be44fc2005-08-05 00:44:28 -0400257 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
258 tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Patrick Mocheld550d982006-06-27 00:41:40 -0400260 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Len Brown4be44fc2005-08-05 00:44:28 -0400263static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Len Brown4be44fc2005-08-05 00:44:28 -0400265 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400269 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Len Brown4be44fc2005-08-05 00:44:28 -0400271 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400272 acpi_evaluate_integer(tz->device->handle, "_TZP", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400273 &tz->polling_frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400275 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Len Brown4be44fc2005-08-05 00:44:28 -0400277 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
278 tz->polling_frequency));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Patrick Mocheld550d982006-06-27 00:41:40 -0400280 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
Len Brown4be44fc2005-08-05 00:44:28 -0400283static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400287 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 tz->polling_frequency = seconds * 10; /* Convert value to deci-seconds */
290
Len Brown4be44fc2005-08-05 00:44:28 -0400291 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
292 "Polling frequency set to %lu seconds\n",
Sanjoy Mahajan636cedf2007-02-16 01:24:43 -0500293 tz->polling_frequency/10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Patrick Mocheld550d982006-06-27 00:41:40 -0400295 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Len Brown4be44fc2005-08-05 00:44:28 -0400298static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Len Brown4be44fc2005-08-05 00:44:28 -0400300 acpi_status status = AE_OK;
301 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
302 struct acpi_object_list arg_list = { 1, &arg0 };
303 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400307 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400309 status = acpi_get_handle(tz->device->handle, "_SCP", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (ACPI_FAILURE(status)) {
311 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400312 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314
315 arg0.integer.value = mode;
316
317 status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
318 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400319 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Patrick Mocheld550d982006-06-27 00:41:40 -0400321 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
Len Brown4be44fc2005-08-05 00:44:28 -0400324static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Len Brown4be44fc2005-08-05 00:44:28 -0400326 acpi_status status = AE_OK;
327 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400331 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 /* Critical Shutdown (required) */
334
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400335 status = acpi_evaluate_integer(tz->device->handle, "_CRT", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400336 &tz->trips.critical.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (ACPI_FAILURE(status)) {
338 tz->trips.critical.flags.valid = 0;
Thomas Renningera6fc6722006-06-26 23:58:43 -0400339 ACPI_EXCEPTION((AE_INFO, status, "No critical threshold"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400340 return -ENODEV;
Len Brown4be44fc2005-08-05 00:44:28 -0400341 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 tz->trips.critical.flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400343 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
344 "Found critical threshold [%lu]\n",
345 tz->trips.critical.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347
Len Brownc52a7412007-08-14 15:49:32 -0400348 if (tz->trips.critical.flags.valid == 1) {
349 if (crt == -1) {
350 tz->trips.critical.flags.valid = 0;
351 } else if (crt > 0) {
352 unsigned long crt_k = CELSIUS_TO_KELVIN(crt);
353
354 /*
355 * Allow override to lower critical threshold
356 */
357 if (crt_k < tz->trips.critical.temperature)
358 tz->trips.critical.temperature = crt_k;
359 }
360 }
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 /* Critical Sleep (optional) */
363
Len Brown4be44fc2005-08-05 00:44:28 -0400364 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400365 acpi_evaluate_integer(tz->device->handle, "_HOT", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400366 &tz->trips.hot.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (ACPI_FAILURE(status)) {
368 tz->trips.hot.flags.valid = 0;
369 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No hot threshold\n"));
Len Brown4be44fc2005-08-05 00:44:28 -0400370 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 tz->trips.hot.flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400372 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found hot threshold [%lu]\n",
373 tz->trips.hot.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375
376 /* Passive: Processors (optional) */
377
Len Browna70cdc52007-08-12 00:12:35 -0400378 if (psv == -1) {
379 status = AE_SUPPORT;
380 } else if (psv > 0) {
381 tz->trips.passive.temperature = CELSIUS_TO_KELVIN(psv);
382 status = AE_OK;
383 } else {
384 status = acpi_evaluate_integer(tz->device->handle,
385 "_PSV", NULL, &tz->trips.passive.temperature);
386 }
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (ACPI_FAILURE(status)) {
389 tz->trips.passive.flags.valid = 0;
390 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No passive threshold\n"));
Len Brown4be44fc2005-08-05 00:44:28 -0400391 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 tz->trips.passive.flags.valid = 1;
393
Len Brown4be44fc2005-08-05 00:44:28 -0400394 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400395 acpi_evaluate_integer(tz->device->handle, "_TC1", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400396 &tz->trips.passive.tc1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (ACPI_FAILURE(status))
398 tz->trips.passive.flags.valid = 0;
399
Len Brown4be44fc2005-08-05 00:44:28 -0400400 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400401 acpi_evaluate_integer(tz->device->handle, "_TC2", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400402 &tz->trips.passive.tc2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (ACPI_FAILURE(status))
404 tz->trips.passive.flags.valid = 0;
405
Len Brown4be44fc2005-08-05 00:44:28 -0400406 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400407 acpi_evaluate_integer(tz->device->handle, "_TSP", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400408 &tz->trips.passive.tsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (ACPI_FAILURE(status))
410 tz->trips.passive.flags.valid = 0;
411
Len Brown4be44fc2005-08-05 00:44:28 -0400412 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400413 acpi_evaluate_reference(tz->device->handle, "_PSL", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400414 &tz->trips.passive.devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (ACPI_FAILURE(status))
416 tz->trips.passive.flags.valid = 0;
417
418 if (!tz->trips.passive.flags.valid)
Len Browncece9292006-06-26 23:04:31 -0400419 printk(KERN_WARNING PREFIX "Invalid passive threshold\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 else
Len Brown4be44fc2005-08-05 00:44:28 -0400421 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
422 "Found passive threshold [%lu]\n",
423 tz->trips.passive.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425
426 /* Active: Fans, etc. (optional) */
427
Len Brown4be44fc2005-08-05 00:44:28 -0400428 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Len Brown4be44fc2005-08-05 00:44:28 -0400430 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Len Brownf8707ec2007-08-12 00:12:54 -0400432 if (act == -1)
433 break; /* disable all active trip points */
434
435 status = acpi_evaluate_integer(tz->device->handle,
436 name, NULL, &tz->trips.active[i].temperature);
437
438 if (ACPI_FAILURE(status)) {
439 if (i == 0) /* no active trip points */
440 break;
441 if (act <= 0) /* no override requested */
442 break;
443 if (i == 1) { /* 1 trip point */
444 tz->trips.active[0].temperature =
445 CELSIUS_TO_KELVIN(act);
446 } else { /* multiple trips */
447 /*
448 * Don't allow override higher than
449 * the next higher trip point
450 */
451 tz->trips.active[i - 1].temperature =
452 (tz->trips.active[i - 2].temperature <
453 CELSIUS_TO_KELVIN(act) ?
454 tz->trips.active[i - 2].temperature :
455 CELSIUS_TO_KELVIN(act));
456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 break;
Len Brownf8707ec2007-08-12 00:12:54 -0400458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 name[2] = 'L';
Len Brown4be44fc2005-08-05 00:44:28 -0400461 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400462 acpi_evaluate_reference(tz->device->handle, name, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400463 &tz->trips.active[i].devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (ACPI_SUCCESS(status)) {
465 tz->trips.active[i].flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400466 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
467 "Found active threshold [%d]:[%lu]\n",
468 i, tz->trips.active[i].temperature));
469 } else
Thomas Renningera6fc6722006-06-26 23:58:43 -0400470 ACPI_EXCEPTION((AE_INFO, status,
471 "Invalid active threshold [%d]", i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473
Patrick Mocheld550d982006-06-27 00:41:40 -0400474 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
Len Brown4be44fc2005-08-05 00:44:28 -0400477static int acpi_thermal_get_devices(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Len Brown4be44fc2005-08-05 00:44:28 -0400479 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400483 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Len Brown4be44fc2005-08-05 00:44:28 -0400485 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400486 acpi_evaluate_reference(tz->device->handle, "_TZD", NULL, &tz->devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400488 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Patrick Mocheld550d982006-06-27 00:41:40 -0400490 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
Len Brown4be44fc2005-08-05 00:44:28 -0400493static int acpi_thermal_critical(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Len Brownf5487142007-08-12 00:12:44 -0400495 if (!tz || !tz->trips.critical.flags.valid || nocrt)
Patrick Mocheld550d982006-06-27 00:41:40 -0400496 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 if (tz->temperature >= tz->trips.critical.temperature) {
Len Browncece9292006-06-26 23:04:31 -0400499 printk(KERN_WARNING PREFIX "Critical trip point\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 tz->trips.critical.flags.enabled = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400501 } else if (tz->trips.critical.flags.enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 tz->trips.critical.flags.enabled = 0;
503
Len Brown4be44fc2005-08-05 00:44:28 -0400504 printk(KERN_EMERG
505 "Critical temperature reached (%ld C), shutting down.\n",
506 KELVIN_TO_CELSIUS(tz->temperature));
Len Brown14e04fb2007-08-23 15:20:26 -0400507 acpi_bus_generate_proc_event(tz->device, ACPI_THERMAL_NOTIFY_CRITICAL,
Len Brown4be44fc2005-08-05 00:44:28 -0400508 tz->trips.critical.flags.enabled);
Zhang Rui962ce8c2007-08-23 01:24:31 +0800509 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
510 tz->device->dev.bus_id,
511 ACPI_THERMAL_NOTIFY_CRITICAL,
512 tz->trips.critical.flags.enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -0700514 orderly_poweroff(true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Patrick Mocheld550d982006-06-27 00:41:40 -0400516 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
Len Brown4be44fc2005-08-05 00:44:28 -0400519static int acpi_thermal_hot(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Len Brownf5487142007-08-12 00:12:44 -0400521 if (!tz || !tz->trips.hot.flags.valid || nocrt)
Patrick Mocheld550d982006-06-27 00:41:40 -0400522 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 if (tz->temperature >= tz->trips.hot.temperature) {
Len Browncece9292006-06-26 23:04:31 -0400525 printk(KERN_WARNING PREFIX "Hot trip point\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 tz->trips.hot.flags.enabled = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400527 } else if (tz->trips.hot.flags.enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 tz->trips.hot.flags.enabled = 0;
529
Len Brown14e04fb2007-08-23 15:20:26 -0400530 acpi_bus_generate_proc_event(tz->device, ACPI_THERMAL_NOTIFY_HOT,
Len Brown4be44fc2005-08-05 00:44:28 -0400531 tz->trips.hot.flags.enabled);
Zhang Rui962ce8c2007-08-23 01:24:31 +0800532 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
533 tz->device->dev.bus_id,
534 ACPI_THERMAL_NOTIFY_HOT,
535 tz->trips.hot.flags.enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 /* TBD: Call user-mode "sleep(S4)" function */
538
Patrick Mocheld550d982006-06-27 00:41:40 -0400539 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400542static void acpi_thermal_passive(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400544 int result = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 struct acpi_thermal_passive *passive = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400546 int trend = 0;
547 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 if (!tz || !tz->trips.passive.flags.valid)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400551 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 passive = &(tz->trips.passive);
554
555 /*
556 * Above Trip?
557 * -----------
558 * Calculate the thermal trend (using the passive cooling equation)
559 * and modify the performance limit for all passive cooling devices
560 * accordingly. Note that we assume symmetry.
561 */
562 if (tz->temperature >= passive->temperature) {
Len Brown4be44fc2005-08-05 00:44:28 -0400563 trend =
564 (passive->tc1 * (tz->temperature - tz->last_temperature)) +
565 (passive->tc2 * (tz->temperature - passive->temperature));
566 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
567 "trend[%d]=(tc1[%lu]*(tmp[%lu]-last[%lu]))+(tc2[%lu]*(tmp[%lu]-psv[%lu]))\n",
568 trend, passive->tc1, tz->temperature,
569 tz->last_temperature, passive->tc2,
570 tz->temperature, passive->temperature));
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400571 passive->flags.enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 /* Heating up? */
573 if (trend > 0)
Len Brown4be44fc2005-08-05 00:44:28 -0400574 for (i = 0; i < passive->devices.count; i++)
575 acpi_processor_set_thermal_limit(passive->
576 devices.
577 handles[i],
578 ACPI_PROCESSOR_LIMIT_INCREMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 /* Cooling off? */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400580 else if (trend < 0) {
Len Brown4be44fc2005-08-05 00:44:28 -0400581 for (i = 0; i < passive->devices.count; i++)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400582 /*
583 * assume that we are on highest
584 * freq/lowest thrott and can leave
585 * passive mode, even in error case
586 */
587 if (!acpi_processor_set_thermal_limit
588 (passive->devices.handles[i],
589 ACPI_PROCESSOR_LIMIT_DECREMENT))
590 result = 0;
591 /*
592 * Leave cooling mode, even if the temp might
593 * higher than trip point This is because some
594 * machines might have long thermal polling
595 * frequencies (tsp) defined. We will fall back
596 * into passive mode in next cycle (probably quicker)
597 */
598 if (result) {
599 passive->flags.enabled = 0;
600 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
601 "Disabling passive cooling, still above threshold,"
602 " but we are cooling down\n"));
603 }
604 }
605 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607
608 /*
609 * Below Trip?
610 * -----------
611 * Implement passive cooling hysteresis to slowly increase performance
612 * and avoid thrashing around the passive trip point. Note that we
613 * assume symmetry.
614 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400615 if (!passive->flags.enabled)
616 return;
617 for (i = 0; i < passive->devices.count; i++)
618 if (!acpi_processor_set_thermal_limit
619 (passive->devices.handles[i],
620 ACPI_PROCESSOR_LIMIT_DECREMENT))
621 result = 0;
622 if (result) {
623 passive->flags.enabled = 0;
624 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
625 "Disabling passive cooling (zone is cool)\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400629static void acpi_thermal_active(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Len Brown4be44fc2005-08-05 00:44:28 -0400631 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 struct acpi_thermal_active *active = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400633 int i = 0;
634 int j = 0;
635 unsigned long maxtemp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 if (!tz)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400639 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Len Brown4be44fc2005-08-05 00:44:28 -0400641 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 active = &(tz->trips.active[i]);
643 if (!active || !active->flags.valid)
644 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (tz->temperature >= active->temperature) {
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400646 /*
647 * Above Threshold?
648 * ----------------
649 * If not already enabled, turn ON all cooling devices
650 * associated with this active threshold.
651 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (active->temperature > maxtemp)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400653 tz->state.active_index = i;
654 maxtemp = active->temperature;
655 if (active->flags.enabled)
656 continue;
657 for (j = 0; j < active->devices.count; j++) {
658 result =
659 acpi_bus_set_power(active->devices.
660 handles[j],
661 ACPI_STATE_D0);
662 if (result) {
Len Browncece9292006-06-26 23:04:31 -0400663 printk(KERN_WARNING PREFIX
664 "Unable to turn cooling device [%p] 'on'\n",
Thomas Renningera6fc6722006-06-26 23:58:43 -0400665 active->devices.
Len Browncece9292006-06-26 23:04:31 -0400666 handles[j]);
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400667 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400669 active->flags.enabled = 1;
670 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
671 "Cooling device [%p] now 'on'\n",
672 active->devices.handles[j]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400674 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400676 if (!active->flags.enabled)
677 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 /*
679 * Below Threshold?
680 * ----------------
681 * Turn OFF all cooling devices associated with this
682 * threshold.
683 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400684 for (j = 0; j < active->devices.count; j++) {
685 result = acpi_bus_set_power(active->devices.handles[j],
686 ACPI_STATE_D3);
687 if (result) {
Len Browncece9292006-06-26 23:04:31 -0400688 printk(KERN_WARNING PREFIX
689 "Unable to turn cooling device [%p] 'off'\n",
690 active->devices.handles[j]);
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400691 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400693 active->flags.enabled = 0;
694 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
695 "Cooling device [%p] now 'off'\n",
696 active->devices.handles[j]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
Len Brown4be44fc2005-08-05 00:44:28 -0400701static void acpi_thermal_check(void *context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Len Brown4be44fc2005-08-05 00:44:28 -0400703static void acpi_thermal_run(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 struct acpi_thermal *tz = (struct acpi_thermal *)data;
706 if (!tz->zombie)
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400707 acpi_os_execute(OSL_GPE_HANDLER, acpi_thermal_check, (void *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
Len Brown4be44fc2005-08-05 00:44:28 -0400710static void acpi_thermal_check(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Len Brown4be44fc2005-08-05 00:44:28 -0400712 int result = 0;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200713 struct acpi_thermal *tz = data;
Len Brown4be44fc2005-08-05 00:44:28 -0400714 unsigned long sleep_time = 0;
Len Brown21bc42a2007-09-04 12:49:22 -0400715 unsigned long timeout_jiffies = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400716 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 struct acpi_thermal_state state;
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 if (!tz) {
Len Brown64684632006-06-26 23:41:38 -0400721 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400722 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
724
Alexey Starikovskiy6e215782007-09-01 00:11:59 +0400725 /* Check if someone else is already running */
726 if (!mutex_trylock(&tz->lock))
727 return;
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 state = tz->state;
730
731 result = acpi_thermal_get_temperature(tz);
732 if (result)
Alexey Starikovskiy6e215782007-09-01 00:11:59 +0400733 goto unlock;
Len Brown4be44fc2005-08-05 00:44:28 -0400734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 memset(&tz->state, 0, sizeof(tz->state));
Len Brown4be44fc2005-08-05 00:44:28 -0400736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 /*
738 * Check Trip Points
739 * -----------------
740 * Compare the current temperature to the trip point values to see
741 * if we've entered one of the thermal policy states. Note that
742 * this function determines when a state is entered, but the
743 * individual policy decides when it is exited (e.g. hysteresis).
744 */
745 if (tz->trips.critical.flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400746 state.critical |=
747 (tz->temperature >= tz->trips.critical.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (tz->trips.hot.flags.valid)
749 state.hot |= (tz->temperature >= tz->trips.hot.temperature);
750 if (tz->trips.passive.flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400751 state.passive |=
752 (tz->temperature >= tz->trips.passive.temperature);
753 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (tz->trips.active[i].flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400755 state.active |=
756 (tz->temperature >=
757 tz->trips.active[i].temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 /*
760 * Invoke Policy
761 * -------------
762 * Separated from the above check to allow individual policy to
763 * determine when to exit a given state.
764 */
765 if (state.critical)
766 acpi_thermal_critical(tz);
767 if (state.hot)
768 acpi_thermal_hot(tz);
769 if (state.passive)
770 acpi_thermal_passive(tz);
771 if (state.active)
772 acpi_thermal_active(tz);
773
774 /*
775 * Calculate State
776 * ---------------
777 * Again, separated from the above two to allow independent policy
778 * decisions.
779 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400780 tz->state.critical = tz->trips.critical.flags.enabled;
781 tz->state.hot = tz->trips.hot.flags.enabled;
782 tz->state.passive = tz->trips.passive.flags.enabled;
783 tz->state.active = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400784 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400785 tz->state.active |= tz->trips.active[i].flags.enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 /*
788 * Calculate Sleep Time
789 * --------------------
790 * If we're in the passive state, use _TSP's value. Otherwise
791 * use the default polling frequency (e.g. _TZP). If no polling
792 * frequency is specified then we'll wait forever (at least until
793 * a thermal event occurs). Note that _TSP and _TZD values are
794 * given in 1/10th seconds (we must covert to milliseconds).
795 */
Len Brown21bc42a2007-09-04 12:49:22 -0400796 if (tz->state.passive) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 sleep_time = tz->trips.passive.tsp * 100;
Len Brown21bc42a2007-09-04 12:49:22 -0400798 timeout_jiffies = jiffies + (HZ * sleep_time) / 1000;
799 } else if (tz->polling_frequency > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 sleep_time = tz->polling_frequency * 100;
Len Brown21bc42a2007-09-04 12:49:22 -0400801 timeout_jiffies = round_jiffies(jiffies + (HZ * sleep_time) / 1000);
802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Len Brown4be44fc2005-08-05 00:44:28 -0400804 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: temperature[%lu] sleep[%lu]\n",
805 tz->name, tz->temperature, sleep_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 /*
808 * Schedule Next Poll
809 * ------------------
810 */
811 if (!sleep_time) {
812 if (timer_pending(&(tz->timer)))
813 del_timer(&(tz->timer));
Len Brown4be44fc2005-08-05 00:44:28 -0400814 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (timer_pending(&(tz->timer)))
Len Brown21bc42a2007-09-04 12:49:22 -0400816 mod_timer(&(tz->timer), timeout_jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400818 tz->timer.data = (unsigned long)tz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 tz->timer.function = acpi_thermal_run;
Len Brown21bc42a2007-09-04 12:49:22 -0400820 tz->timer.expires = timeout_jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 add_timer(&(tz->timer));
822 }
823 }
Alexey Starikovskiy6e215782007-09-01 00:11:59 +0400824 unlock:
825 mutex_unlock(&tz->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828/* --------------------------------------------------------------------------
829 FS Interface (/proc)
830 -------------------------------------------------------------------------- */
831
Len Brown4be44fc2005-08-05 00:44:28 -0400832static struct proc_dir_entry *acpi_thermal_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
835{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200836 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 if (!tz)
840 goto end;
841
842 seq_puts(seq, "state: ");
843
Len Brown4be44fc2005-08-05 00:44:28 -0400844 if (!tz->state.critical && !tz->state.hot && !tz->state.passive
845 && !tz->state.active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 seq_puts(seq, "ok\n");
847 else {
848 if (tz->state.critical)
849 seq_puts(seq, "critical ");
850 if (tz->state.hot)
851 seq_puts(seq, "hot ");
852 if (tz->state.passive)
853 seq_puts(seq, "passive ");
854 if (tz->state.active)
855 seq_printf(seq, "active[%d]", tz->state.active_index);
856 seq_puts(seq, "\n");
857 }
858
Len Brown4be44fc2005-08-05 00:44:28 -0400859 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400860 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
862
863static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
864{
865 return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
866}
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
869{
Len Brown4be44fc2005-08-05 00:44:28 -0400870 int result = 0;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200871 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 if (!tz)
875 goto end;
876
877 result = acpi_thermal_get_temperature(tz);
878 if (result)
879 goto end;
880
Len Brown4be44fc2005-08-05 00:44:28 -0400881 seq_printf(seq, "temperature: %ld C\n",
882 KELVIN_TO_CELSIUS(tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Len Brown4be44fc2005-08-05 00:44:28 -0400884 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400885 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
888static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
889{
890 return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
891}
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
894{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200895 struct acpi_thermal *tz = seq->private;
Thomas Renninger68ccfaa2006-11-19 23:01:40 +0100896 struct acpi_device *device;
Thomas Renningere7c746e2007-06-18 00:40:51 -0400897 acpi_status status;
898
Len Brown4be44fc2005-08-05 00:44:28 -0400899 int i = 0;
900 int j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 if (!tz)
904 goto end;
905
906 if (tz->trips.critical.flags.valid)
Len Brownf5487142007-08-12 00:12:44 -0400907 seq_printf(seq, "critical (S5): %ld C%s",
908 KELVIN_TO_CELSIUS(tz->trips.critical.temperature),
909 nocrt ? " <disabled>\n" : "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 if (tz->trips.hot.flags.valid)
Len Brownf5487142007-08-12 00:12:44 -0400912 seq_printf(seq, "hot (S4): %ld C%s",
913 KELVIN_TO_CELSIUS(tz->trips.hot.temperature),
914 nocrt ? " <disabled>\n" : "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 if (tz->trips.passive.flags.valid) {
Len Brown4be44fc2005-08-05 00:44:28 -0400917 seq_printf(seq,
918 "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
919 KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
920 tz->trips.passive.tc1, tz->trips.passive.tc2,
921 tz->trips.passive.tsp);
922 for (j = 0; j < tz->trips.passive.devices.count; j++) {
Thomas Renningere7c746e2007-06-18 00:40:51 -0400923 status = acpi_bus_get_device(tz->trips.passive.devices.
924 handles[j], &device);
925 seq_printf(seq, "%4.4s ", status ? "" :
926 acpi_device_bid(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928 seq_puts(seq, "\n");
929 }
930
931 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
932 if (!(tz->trips.active[i].flags.valid))
933 break;
934 seq_printf(seq, "active[%d]: %ld C: devices=",
Len Brown4be44fc2005-08-05 00:44:28 -0400935 i,
936 KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
Thomas Renninger68ccfaa2006-11-19 23:01:40 +0100937 for (j = 0; j < tz->trips.active[i].devices.count; j++){
Thomas Renningere7c746e2007-06-18 00:40:51 -0400938 status = acpi_bus_get_device(tz->trips.active[i].
939 devices.handles[j],
940 &device);
941 seq_printf(seq, "%4.4s ", status ? "" :
942 acpi_device_bid(device));
Thomas Renninger68ccfaa2006-11-19 23:01:40 +0100943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 seq_puts(seq, "\n");
945 }
946
Len Brown4be44fc2005-08-05 00:44:28 -0400947 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400948 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950
951static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
952{
953 return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
954}
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
957{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200958 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 if (!tz)
962 goto end;
963
Len Browneaca2d32007-04-30 23:27:43 -0400964 if (!tz->flags.cooling_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 seq_puts(seq, "<setting not supported>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 else
Len Browneaca2d32007-04-30 23:27:43 -0400967 seq_puts(seq, "0 - Active; 1 - Passive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Len Brown4be44fc2005-08-05 00:44:28 -0400969 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400970 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972
973static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
974{
975 return single_open(file, acpi_thermal_cooling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -0400976 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
979static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -0400980acpi_thermal_write_cooling_mode(struct file *file,
981 const char __user * buffer,
982 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200984 struct seq_file *m = file->private_data;
985 struct acpi_thermal *tz = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400986 int result = 0;
987 char mode_string[12] = { '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 if (!tz || (count > sizeof(mode_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400991 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 if (!tz->flags.cooling_mode)
Patrick Mocheld550d982006-06-27 00:41:40 -0400994 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 if (copy_from_user(mode_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -0400997 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -0400998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 mode_string[count] = '\0';
Len Brown4be44fc2005-08-05 00:44:28 -04001000
1001 result = acpi_thermal_set_cooling_mode(tz,
1002 simple_strtoul(mode_string, NULL,
1003 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001005 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 acpi_thermal_check(tz);
1008
Patrick Mocheld550d982006-06-27 00:41:40 -04001009 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010}
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
1013{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001014 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 if (!tz)
1018 goto end;
1019
1020 if (!tz->polling_frequency) {
1021 seq_puts(seq, "<polling disabled>\n");
1022 goto end;
1023 }
1024
1025 seq_printf(seq, "polling frequency: %lu seconds\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001026 (tz->polling_frequency / 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Len Brown4be44fc2005-08-05 00:44:28 -04001028 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001029 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
1032static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
1033{
1034 return single_open(file, acpi_thermal_polling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001035 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
1038static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001039acpi_thermal_write_polling(struct file *file,
1040 const char __user * buffer,
1041 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001043 struct seq_file *m = file->private_data;
1044 struct acpi_thermal *tz = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001045 int result = 0;
1046 char polling_string[12] = { '\0' };
1047 int seconds = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 if (!tz || (count > sizeof(polling_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001051 return -EINVAL;
Len Brown4be44fc2005-08-05 00:44:28 -04001052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (copy_from_user(polling_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001054 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -04001055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 polling_string[count] = '\0';
1057
1058 seconds = simple_strtoul(polling_string, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 result = acpi_thermal_set_polling(tz, seconds);
1061 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001062 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 acpi_thermal_check(tz);
1065
Patrick Mocheld550d982006-06-27 00:41:40 -04001066 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
1068
Len Brown4be44fc2005-08-05 00:44:28 -04001069static int acpi_thermal_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
Len Brown4be44fc2005-08-05 00:44:28 -04001071 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 if (!acpi_device_dir(device)) {
1075 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -04001076 acpi_thermal_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001078 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 acpi_device_dir(device)->owner = THIS_MODULE;
1080 }
1081
1082 /* 'state' [R] */
1083 entry = create_proc_entry(ACPI_THERMAL_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -04001084 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001086 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 else {
1088 entry->proc_fops = &acpi_thermal_state_fops;
1089 entry->data = acpi_driver_data(device);
1090 entry->owner = THIS_MODULE;
1091 }
1092
1093 /* 'temperature' [R] */
1094 entry = create_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
Len Brown4be44fc2005-08-05 00:44:28 -04001095 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001097 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 else {
1099 entry->proc_fops = &acpi_thermal_temp_fops;
1100 entry->data = acpi_driver_data(device);
1101 entry->owner = THIS_MODULE;
1102 }
1103
Pavel Machek2db9ccb2007-08-24 11:45:50 +02001104 /* 'trip_points' [R] */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 entry = create_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
Pavel Machek2db9ccb2007-08-24 11:45:50 +02001106 S_IRUGO,
Len Brown4be44fc2005-08-05 00:44:28 -04001107 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001109 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 else {
1111 entry->proc_fops = &acpi_thermal_trip_fops;
1112 entry->data = acpi_driver_data(device);
1113 entry->owner = THIS_MODULE;
1114 }
1115
1116 /* 'cooling_mode' [R/W] */
1117 entry = create_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
Len Brown4be44fc2005-08-05 00:44:28 -04001118 S_IFREG | S_IRUGO | S_IWUSR,
1119 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001121 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 else {
1123 entry->proc_fops = &acpi_thermal_cooling_fops;
1124 entry->data = acpi_driver_data(device);
1125 entry->owner = THIS_MODULE;
1126 }
1127
1128 /* 'polling_frequency' [R/W] */
1129 entry = create_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
Len Brown4be44fc2005-08-05 00:44:28 -04001130 S_IFREG | S_IRUGO | S_IWUSR,
1131 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001133 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 else {
1135 entry->proc_fops = &acpi_thermal_polling_fops;
1136 entry->data = acpi_driver_data(device);
1137 entry->owner = THIS_MODULE;
1138 }
1139
Patrick Mocheld550d982006-06-27 00:41:40 -04001140 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142
Len Brown4be44fc2005-08-05 00:44:28 -04001143static int acpi_thermal_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 if (acpi_device_dir(device)) {
1147 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1148 acpi_device_dir(device));
1149 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1150 acpi_device_dir(device));
1151 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1152 acpi_device_dir(device));
1153 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1154 acpi_device_dir(device));
1155 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1156 acpi_device_dir(device));
1157 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1158 acpi_device_dir(device) = NULL;
1159 }
1160
Patrick Mocheld550d982006-06-27 00:41:40 -04001161 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164/* --------------------------------------------------------------------------
1165 Driver Interface
1166 -------------------------------------------------------------------------- */
1167
Len Brown4be44fc2005-08-05 00:44:28 -04001168static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001170 struct acpi_thermal *tz = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001171 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001175 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Patrick Mochel8348e1b2006-05-19 16:54:40 -04001177 device = tz->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 switch (event) {
1180 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1181 acpi_thermal_check(tz);
1182 break;
1183 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
1184 acpi_thermal_get_trip_points(tz);
1185 acpi_thermal_check(tz);
Len Brown14e04fb2007-08-23 15:20:26 -04001186 acpi_bus_generate_proc_event(device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08001187 acpi_bus_generate_netlink_event(device->pnp.device_class,
1188 device->dev.bus_id, event, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 break;
1190 case ACPI_THERMAL_NOTIFY_DEVICES:
1191 if (tz->flags.devices)
1192 acpi_thermal_get_devices(tz);
Len Brown14e04fb2007-08-23 15:20:26 -04001193 acpi_bus_generate_proc_event(device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08001194 acpi_bus_generate_netlink_event(device->pnp.device_class,
1195 device->dev.bus_id, event, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 break;
1197 default:
1198 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001199 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 break;
1201 }
1202
Patrick Mocheld550d982006-06-27 00:41:40 -04001203 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
1205
Len Brown4be44fc2005-08-05 00:44:28 -04001206static int acpi_thermal_get_info(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
Len Brown4be44fc2005-08-05 00:44:28 -04001208 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001212 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 /* Get temperature [_TMP] (required) */
1215 result = acpi_thermal_get_temperature(tz);
1216 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001217 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 /* Get trip points [_CRT, _PSV, etc.] (required) */
1220 result = acpi_thermal_get_trip_points(tz);
1221 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001222 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 /* Set the cooling mode [_SCP] to active cooling (default) */
1225 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
Len Brown4be44fc2005-08-05 00:44:28 -04001226 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 tz->flags.cooling_mode = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 /* Get default polling frequency [_TZP] (optional) */
1230 if (tzp)
1231 tz->polling_frequency = tzp;
1232 else
1233 acpi_thermal_get_polling_frequency(tz);
1234
1235 /* Get devices in this thermal zone [_TZD] (optional) */
1236 result = acpi_thermal_get_devices(tz);
1237 if (!result)
1238 tz->flags.devices = 1;
1239
Patrick Mocheld550d982006-06-27 00:41:40 -04001240 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
Len Brown4be44fc2005-08-05 00:44:28 -04001243static int acpi_thermal_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
Len Brown4be44fc2005-08-05 00:44:28 -04001245 int result = 0;
1246 acpi_status status = AE_OK;
1247 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001251 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Burman Yan36bcbec2006-12-19 12:56:11 -08001253 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001255 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Patrick Mochel8348e1b2006-05-19 16:54:40 -04001257 tz->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 strcpy(tz->name, device->pnp.bus_id);
1259 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1260 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1261 acpi_driver_data(device) = tz;
Alexey Starikovskiy6e215782007-09-01 00:11:59 +04001262 mutex_init(&tz->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 result = acpi_thermal_get_info(tz);
1264 if (result)
1265 goto end;
1266
1267 result = acpi_thermal_add_fs(device);
1268 if (result)
Vasily Averin09047e72006-04-27 05:25:00 -04001269 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 init_timer(&tz->timer);
1272
1273 acpi_thermal_check(tz);
1274
Patrick Mochel38ba7c92006-05-19 16:54:48 -04001275 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001276 ACPI_DEVICE_NOTIFY,
1277 acpi_thermal_notify, tz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 result = -ENODEV;
1280 goto end;
1281 }
1282
1283 printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001284 acpi_device_name(device), acpi_device_bid(device),
1285 KELVIN_TO_CELSIUS(tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Len Brown4be44fc2005-08-05 00:44:28 -04001287 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 if (result) {
1289 acpi_thermal_remove_fs(device);
1290 kfree(tz);
1291 }
1292
Patrick Mocheld550d982006-06-27 00:41:40 -04001293 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294}
1295
Len Brown4be44fc2005-08-05 00:44:28 -04001296static int acpi_thermal_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
Len Brown4be44fc2005-08-05 00:44:28 -04001298 acpi_status status = AE_OK;
1299 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001303 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001305 tz = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 /* avoid timer adding new defer task */
1308 tz->zombie = 1;
1309 /* wait for running timer (on other CPUs) finish */
1310 del_timer_sync(&(tz->timer));
1311 /* synchronize deferred task */
1312 acpi_os_wait_events_complete(NULL);
1313 /* deferred task may reinsert timer */
1314 del_timer_sync(&(tz->timer));
1315
Patrick Mochel38ba7c92006-05-19 16:54:48 -04001316 status = acpi_remove_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001317 ACPI_DEVICE_NOTIFY,
1318 acpi_thermal_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 /* Terminate policy */
Len Brown4be44fc2005-08-05 00:44:28 -04001321 if (tz->trips.passive.flags.valid && tz->trips.passive.flags.enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 tz->trips.passive.flags.enabled = 0;
1323 acpi_thermal_passive(tz);
1324 }
1325 if (tz->trips.active[0].flags.valid
Len Brown4be44fc2005-08-05 00:44:28 -04001326 && tz->trips.active[0].flags.enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 tz->trips.active[0].flags.enabled = 0;
1328 acpi_thermal_active(tz);
1329 }
1330
1331 acpi_thermal_remove_fs(device);
Alexey Starikovskiy6e215782007-09-01 00:11:59 +04001332 mutex_destroy(&tz->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 kfree(tz);
Patrick Mocheld550d982006-06-27 00:41:40 -04001334 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001337static int acpi_thermal_resume(struct acpi_device *device)
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001338{
1339 struct acpi_thermal *tz = NULL;
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001340 int i, j, power_state, result;
1341
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001342
1343 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001344 return -EINVAL;
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001345
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001346 tz = acpi_driver_data(device);
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001347
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001348 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001349 if (!(&tz->trips.active[i]))
1350 break;
1351 if (!tz->trips.active[i].flags.valid)
1352 break;
1353 tz->trips.active[i].flags.enabled = 1;
1354 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1355 result = acpi_bus_get_power(tz->trips.active[i].devices.
1356 handles[j], &power_state);
1357 if (result || (power_state != ACPI_STATE_D0)) {
1358 tz->trips.active[i].flags.enabled = 0;
1359 break;
1360 }
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001361 }
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001362 tz->state.active |= tz->trips.active[i].flags.enabled;
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001363 }
1364
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001365 acpi_thermal_check(tz);
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001366
1367 return AE_OK;
1368}
1369
Len Brown0b5bfa12007-08-12 00:13:02 -04001370#ifdef CONFIG_DMI
Jeff Garzik18552562007-10-03 15:15:40 -04001371static int thermal_act(const struct dmi_system_id *d) {
Len Brown0b5bfa12007-08-12 00:13:02 -04001372
1373 if (act == 0) {
1374 printk(KERN_NOTICE "ACPI: %s detected: "
1375 "disabling all active thermal trip points\n", d->ident);
1376 act = -1;
1377 }
1378 return 0;
1379}
Jeff Garzik18552562007-10-03 15:15:40 -04001380static int thermal_nocrt(const struct dmi_system_id *d) {
Len Brown8c99fdc2007-08-20 18:46:50 -04001381
1382 printk(KERN_NOTICE "ACPI: %s detected: "
1383 "disabling all critical thermal trip point actions.\n", d->ident);
1384 nocrt = 1;
1385 return 0;
1386}
Jeff Garzik18552562007-10-03 15:15:40 -04001387static int thermal_tzp(const struct dmi_system_id *d) {
Len Brown0b5bfa12007-08-12 00:13:02 -04001388
1389 if (tzp == 0) {
1390 printk(KERN_NOTICE "ACPI: %s detected: "
1391 "enabling thermal zone polling\n", d->ident);
1392 tzp = 300; /* 300 dS = 30 Seconds */
1393 }
1394 return 0;
1395}
Jeff Garzik18552562007-10-03 15:15:40 -04001396static int thermal_psv(const struct dmi_system_id *d) {
Len Brown0b5bfa12007-08-12 00:13:02 -04001397
1398 if (psv == 0) {
1399 printk(KERN_NOTICE "ACPI: %s detected: "
1400 "disabling all passive thermal trip points\n", d->ident);
1401 psv = -1;
1402 }
1403 return 0;
1404}
1405
1406static struct dmi_system_id thermal_dmi_table[] __initdata = {
1407 /*
1408 * Award BIOS on this AOpen makes thermal control almost worthless.
1409 * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1410 */
1411 {
1412 .callback = thermal_act,
1413 .ident = "AOpen i915GMm-HFS",
1414 .matches = {
1415 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1416 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1417 },
1418 },
1419 {
1420 .callback = thermal_psv,
1421 .ident = "AOpen i915GMm-HFS",
1422 .matches = {
1423 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1424 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1425 },
1426 },
1427 {
1428 .callback = thermal_tzp,
1429 .ident = "AOpen i915GMm-HFS",
1430 .matches = {
1431 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1432 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1433 },
1434 },
Len Brown8c99fdc2007-08-20 18:46:50 -04001435 {
1436 .callback = thermal_nocrt,
1437 .ident = "Gigabyte GA-7ZX",
1438 .matches = {
1439 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
1440 DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
1441 },
1442 },
Len Brown0b5bfa12007-08-12 00:13:02 -04001443 {}
1444};
1445#endif /* CONFIG_DMI */
1446
Len Brown4be44fc2005-08-05 00:44:28 -04001447static int __init acpi_thermal_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Len Brown4be44fc2005-08-05 00:44:28 -04001449 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Len Brown0b5bfa12007-08-12 00:13:02 -04001451 dmi_check_system(thermal_dmi_table);
1452
Len Brown72b33ef2007-08-12 00:12:17 -04001453 if (off) {
1454 printk(KERN_NOTICE "ACPI: thermal control disabled\n");
1455 return -ENODEV;
1456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1458 if (!acpi_thermal_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001459 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 acpi_thermal_dir->owner = THIS_MODULE;
1461
1462 result = acpi_bus_register_driver(&acpi_thermal_driver);
1463 if (result < 0) {
1464 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04001465 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
1467
Patrick Mocheld550d982006-06-27 00:41:40 -04001468 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
Len Brown4be44fc2005-08-05 00:44:28 -04001471static void __exit acpi_thermal_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 acpi_bus_unregister_driver(&acpi_thermal_driver);
1475
1476 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1477
Patrick Mocheld550d982006-06-27 00:41:40 -04001478 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481module_init(acpi_thermal_init);
1482module_exit(acpi_thermal_exit);