blob: 1c410ef859c68709f7c3ab7cb8d58189d2871eb5 [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>
Zhang Rui3f655ef2008-01-17 15:51:11 +080046#include <linux/thermal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <acpi/acpi_bus.h>
48#include <acpi/acpi_drivers.h>
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#define _COMPONENT ACPI_THERMAL_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050068ACPI_MODULE_NAME("thermal");
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Thomas Renninger1cbf4c52004-09-16 11:07:00 -040070MODULE_AUTHOR("Paul Diefenbaugh");
Len Brown7cda93e2007-02-12 23:50:02 -050071MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070072MODULE_LICENSE("GPL");
73
Len Brownf8707ec2007-08-12 00:12:54 -040074static int act;
75module_param(act, int, 0644);
Len Brown3c1d36d2007-08-14 15:12:56 -040076MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.");
Len Brownf8707ec2007-08-12 00:12:54 -040077
Len Brownc52a7412007-08-14 15:49:32 -040078static int crt;
79module_param(crt, int, 0644);
80MODULE_PARM_DESC(crt, "Disable or lower all critical trip points.");
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082static int tzp;
Len Brown730ff342007-08-12 00:12:26 -040083module_param(tzp, int, 0444);
Len Brown3c1d36d2007-08-14 15:12:56 -040084MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Len Brownf5487142007-08-12 00:12:44 -040086static int nocrt;
87module_param(nocrt, int, 0);
Len Brown8c99fdc2007-08-20 18:46:50 -040088MODULE_PARM_DESC(nocrt, "Set to take no action upon ACPI thermal zone critical trips points.");
Len Brownf5487142007-08-12 00:12:44 -040089
Len Brown72b33ef2007-08-12 00:12:17 -040090static int off;
91module_param(off, int, 0);
Len Brown3c1d36d2007-08-14 15:12:56 -040092MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
Len Brown72b33ef2007-08-12 00:12:17 -040093
Len Browna70cdc52007-08-12 00:12:35 -040094static int psv;
95module_param(psv, int, 0644);
Len Brown3c1d36d2007-08-14 15:12:56 -040096MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
Len Browna70cdc52007-08-12 00:12:35 -040097
Len Brown4be44fc2005-08-05 00:44:28 -040098static int acpi_thermal_add(struct acpi_device *device);
99static int acpi_thermal_remove(struct acpi_device *device, int type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800100static int acpi_thermal_resume(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
102static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
103static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -0400105static ssize_t acpi_thermal_write_cooling_mode(struct file *,
106 const char __user *, size_t,
107 loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -0400109static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
110 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200112static const struct acpi_device_id thermal_device_ids[] = {
113 {ACPI_THERMAL_HID, 0},
114 {"", 0},
115};
116MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118static struct acpi_driver acpi_thermal_driver = {
Len Brownc2b67052007-02-12 23:33:40 -0500119 .name = "thermal",
Len Brown4be44fc2005-08-05 00:44:28 -0400120 .class = ACPI_THERMAL_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200121 .ids = thermal_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -0400122 .ops = {
123 .add = acpi_thermal_add,
124 .remove = acpi_thermal_remove,
Konstantin Karasyov74ce14682006-05-08 08:32:00 -0400125 .resume = acpi_thermal_resume,
Len Brown4be44fc2005-08-05 00:44:28 -0400126 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
129struct acpi_thermal_state {
Len Brown4be44fc2005-08-05 00:44:28 -0400130 u8 critical:1;
131 u8 hot:1;
132 u8 passive:1;
133 u8 active:1;
134 u8 reserved:4;
135 int active_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136};
137
138struct acpi_thermal_state_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400139 u8 valid:1;
140 u8 enabled:1;
141 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142};
143
144struct acpi_thermal_critical {
145 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400146 unsigned long temperature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147};
148
149struct acpi_thermal_hot {
150 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400151 unsigned long temperature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152};
153
154struct acpi_thermal_passive {
155 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400156 unsigned long temperature;
157 unsigned long tc1;
158 unsigned long tc2;
159 unsigned long tsp;
160 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161};
162
163struct acpi_thermal_active {
164 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400165 unsigned long temperature;
166 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167};
168
169struct acpi_thermal_trips {
170 struct acpi_thermal_critical critical;
Len Brown4be44fc2005-08-05 00:44:28 -0400171 struct acpi_thermal_hot hot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 struct acpi_thermal_passive passive;
173 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
174};
175
176struct acpi_thermal_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400177 u8 cooling_mode:1; /* _SCP */
178 u8 devices:1; /* _TZD */
179 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180};
181
182struct acpi_thermal {
Patrick Mochel8348e1b2006-05-19 16:54:40 -0400183 struct acpi_device * device;
Len Brown4be44fc2005-08-05 00:44:28 -0400184 acpi_bus_id name;
185 unsigned long temperature;
186 unsigned long last_temperature;
187 unsigned long polling_frequency;
Len Brown4be44fc2005-08-05 00:44:28 -0400188 volatile u8 zombie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 struct acpi_thermal_flags flags;
190 struct acpi_thermal_state state;
191 struct acpi_thermal_trips trips;
Len Brown4be44fc2005-08-05 00:44:28 -0400192 struct acpi_handle_list devices;
193 struct timer_list timer;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800194 struct thermal_zone_device *thermal_zone;
195 int tz_enabled;
Alexey Starikovskiy6e215782007-09-01 00:11:59 +0400196 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197};
198
Arjan van de Vend7508032006-07-04 13:06:00 -0400199static const struct file_operations acpi_thermal_state_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700200 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400201 .open = acpi_thermal_state_open_fs,
202 .read = seq_read,
203 .llseek = seq_lseek,
204 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205};
206
Arjan van de Vend7508032006-07-04 13:06:00 -0400207static const struct file_operations acpi_thermal_temp_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700208 .owner = THIS_MODULE,
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 = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700216 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400217 .open = acpi_thermal_trip_open_fs,
218 .read = seq_read,
Len Brown4be44fc2005-08-05 00:44:28 -0400219 .llseek = seq_lseek,
220 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221};
222
Arjan van de Vend7508032006-07-04 13:06:00 -0400223static const struct file_operations acpi_thermal_cooling_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700224 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400225 .open = acpi_thermal_cooling_open_fs,
226 .read = seq_read,
227 .write = acpi_thermal_write_cooling_mode,
228 .llseek = seq_lseek,
229 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230};
231
Arjan van de Vend7508032006-07-04 13:06:00 -0400232static const struct file_operations acpi_thermal_polling_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700233 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400234 .open = acpi_thermal_polling_open_fs,
235 .read = seq_read,
236 .write = acpi_thermal_write_polling,
237 .llseek = seq_lseek,
238 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239};
240
241/* --------------------------------------------------------------------------
242 Thermal Zone Management
243 -------------------------------------------------------------------------- */
244
Len Brown4be44fc2005-08-05 00:44:28 -0400245static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Len Brown4be44fc2005-08-05 00:44:28 -0400247 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400248 unsigned long long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400251 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 tz->last_temperature = tz->temperature;
254
Matthew Wilcox27663c52008-10-10 02:22:59 -0400255 status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400257 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Matthew Wilcox27663c52008-10-10 02:22:59 -0400259 tz->temperature = tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400260 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
261 tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Patrick Mocheld550d982006-06-27 00:41:40 -0400263 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
Len Brown4be44fc2005-08-05 00:44:28 -0400266static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Len Brown4be44fc2005-08-05 00:44:28 -0400268 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400269 unsigned long long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400272 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Matthew Wilcox27663c52008-10-10 02:22:59 -0400274 status = acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400276 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Matthew Wilcox27663c52008-10-10 02:22:59 -0400278 tz->polling_frequency = tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400279 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
280 tz->polling_frequency));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Patrick Mocheld550d982006-06-27 00:41:40 -0400282 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
Len Brown4be44fc2005-08-05 00:44:28 -0400285static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400289 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 tz->polling_frequency = seconds * 10; /* Convert value to deci-seconds */
292
Len Brown4be44fc2005-08-05 00:44:28 -0400293 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
294 "Polling frequency set to %lu seconds\n",
Sanjoy Mahajan636cedf2007-02-16 01:24:43 -0500295 tz->polling_frequency/10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Patrick Mocheld550d982006-06-27 00:41:40 -0400297 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Len Brown4be44fc2005-08-05 00:44:28 -0400300static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Len Brown4be44fc2005-08-05 00:44:28 -0400302 acpi_status status = AE_OK;
303 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
304 struct acpi_object_list arg_list = { 1, &arg0 };
305 acpi_handle handle = NULL;
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
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400311 status = acpi_get_handle(tz->device->handle, "_SCP", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (ACPI_FAILURE(status)) {
313 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400314 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
316
317 arg0.integer.value = mode;
318
319 status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
320 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400321 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Patrick Mocheld550d982006-06-27 00:41:40 -0400323 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
Zhang Ruice44e192008-01-17 15:51:25 +0800326#define ACPI_TRIPS_CRITICAL 0x01
327#define ACPI_TRIPS_HOT 0x02
328#define ACPI_TRIPS_PASSIVE 0x04
329#define ACPI_TRIPS_ACTIVE 0x08
330#define ACPI_TRIPS_DEVICES 0x10
331
332#define ACPI_TRIPS_REFRESH_THRESHOLDS (ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE)
333#define ACPI_TRIPS_REFRESH_DEVICES ACPI_TRIPS_DEVICES
334
335#define ACPI_TRIPS_INIT (ACPI_TRIPS_CRITICAL | ACPI_TRIPS_HOT | \
336 ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE | \
337 ACPI_TRIPS_DEVICES)
338
339/*
340 * This exception is thrown out in two cases:
341 * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
342 * when re-evaluating the AML code.
343 * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change.
344 * We need to re-bind the cooling devices of a thermal zone when this occurs.
345 */
346#define ACPI_THERMAL_TRIPS_EXCEPTION(flags, str) \
347do { \
348 if (flags != ACPI_TRIPS_INIT) \
349 ACPI_EXCEPTION((AE_INFO, AE_ERROR, \
350 "ACPI thermal trip point %s changed\n" \
351 "Please send acpidump to linux-acpi@vger.kernel.org\n", str)); \
352} while (0)
353
354static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Len Brown4be44fc2005-08-05 00:44:28 -0400356 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400357 unsigned long long tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800358 struct acpi_handle_list devices;
359 int valid = 0;
360 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 /* Critical Shutdown (required) */
Zhang Ruice44e192008-01-17 15:51:25 +0800363 if (flag & ACPI_TRIPS_CRITICAL) {
364 status = acpi_evaluate_integer(tz->device->handle,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400365 "_CRT", NULL, &tmp);
366 tz->trips.critical.temperature = tmp;
Arjan van de Vena39a2d72008-05-19 15:55:15 -0700367 /*
368 * Treat freezing temperatures as invalid as well; some
369 * BIOSes return really low values and cause reboots at startup.
370 * Below zero (Celcius) values clearly aren't right for sure..
371 * ... so lets discard those as invalid.
372 */
373 if (ACPI_FAILURE(status) ||
374 tz->trips.critical.temperature <= 2732) {
Len Brownc52a7412007-08-14 15:49:32 -0400375 tz->trips.critical.flags.valid = 0;
Zhang Ruice44e192008-01-17 15:51:25 +0800376 ACPI_EXCEPTION((AE_INFO, status,
Arjan van de Vena39a2d72008-05-19 15:55:15 -0700377 "No or invalid critical threshold"));
Zhang Ruice44e192008-01-17 15:51:25 +0800378 return -ENODEV;
379 } else {
380 tz->trips.critical.flags.valid = 1;
381 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
382 "Found critical threshold [%lu]\n",
383 tz->trips.critical.temperature));
384 }
385 if (tz->trips.critical.flags.valid == 1) {
386 if (crt == -1) {
387 tz->trips.critical.flags.valid = 0;
388 } else if (crt > 0) {
389 unsigned long crt_k = CELSIUS_TO_KELVIN(crt);
390 /*
Zhang Rui22a94d72008-10-17 02:41:20 -0400391 * Allow override critical threshold
Zhang Ruice44e192008-01-17 15:51:25 +0800392 */
Zhang Rui22a94d72008-10-17 02:41:20 -0400393 if (crt_k > tz->trips.critical.temperature)
394 printk(KERN_WARNING PREFIX
395 "Critical threshold %d C\n", crt);
396 tz->trips.critical.temperature = crt_k;
Zhang Ruice44e192008-01-17 15:51:25 +0800397 }
Len Brownc52a7412007-08-14 15:49:32 -0400398 }
399 }
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 /* Critical Sleep (optional) */
Zhang Ruice44e192008-01-17 15:51:25 +0800402 if (flag & ACPI_TRIPS_HOT) {
Len Browna70cdc52007-08-12 00:12:35 -0400403 status = acpi_evaluate_integer(tz->device->handle,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400404 "_HOT", NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800405 if (ACPI_FAILURE(status)) {
406 tz->trips.hot.flags.valid = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400407 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Zhang Ruice44e192008-01-17 15:51:25 +0800408 "No hot threshold\n"));
409 } else {
Matthew Wilcox27663c52008-10-10 02:22:59 -0400410 tz->trips.hot.temperature = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800411 tz->trips.hot.flags.valid = 1;
412 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
413 "Found hot threshold [%lu]\n",
414 tz->trips.critical.temperature));
415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417
Zhang Ruice44e192008-01-17 15:51:25 +0800418 /* Passive (optional) */
Zhang Rui0e4240d2009-01-16 12:53:42 -0500419 if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.flags.valid) ||
420 (flag == ACPI_TRIPS_INIT)) {
Zhang Ruice44e192008-01-17 15:51:25 +0800421 valid = tz->trips.passive.flags.valid;
422 if (psv == -1) {
423 status = AE_SUPPORT;
424 } else if (psv > 0) {
Matthew Wilcox27663c52008-10-10 02:22:59 -0400425 tmp = CELSIUS_TO_KELVIN(psv);
Zhang Ruice44e192008-01-17 15:51:25 +0800426 status = AE_OK;
427 } else {
428 status = acpi_evaluate_integer(tz->device->handle,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400429 "_PSV", NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Zhang Ruice44e192008-01-17 15:51:25 +0800432 if (ACPI_FAILURE(status))
433 tz->trips.passive.flags.valid = 0;
434 else {
Matthew Wilcox27663c52008-10-10 02:22:59 -0400435 tz->trips.passive.temperature = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800436 tz->trips.passive.flags.valid = 1;
437 if (flag == ACPI_TRIPS_INIT) {
438 status = acpi_evaluate_integer(
439 tz->device->handle, "_TC1",
Matthew Wilcox27663c52008-10-10 02:22:59 -0400440 NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800441 if (ACPI_FAILURE(status))
442 tz->trips.passive.flags.valid = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400443 else
444 tz->trips.passive.tc1 = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800445 status = acpi_evaluate_integer(
446 tz->device->handle, "_TC2",
Matthew Wilcox27663c52008-10-10 02:22:59 -0400447 NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800448 if (ACPI_FAILURE(status))
449 tz->trips.passive.flags.valid = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400450 else
451 tz->trips.passive.tc2 = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800452 status = acpi_evaluate_integer(
453 tz->device->handle, "_TSP",
Matthew Wilcox27663c52008-10-10 02:22:59 -0400454 NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800455 if (ACPI_FAILURE(status))
456 tz->trips.passive.flags.valid = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400457 else
458 tz->trips.passive.tsp = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800459 }
460 }
461 }
462 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.flags.valid) {
463 memset(&devices, 0, sizeof(struct acpi_handle_list));
464 status = acpi_evaluate_reference(tz->device->handle, "_PSL",
465 NULL, &devices);
Zhang Rui0e4240d2009-01-16 12:53:42 -0500466 if (ACPI_FAILURE(status)) {
467 printk(KERN_WARNING PREFIX
468 "Invalid passive threshold\n");
Zhang Ruice44e192008-01-17 15:51:25 +0800469 tz->trips.passive.flags.valid = 0;
Zhang Rui0e4240d2009-01-16 12:53:42 -0500470 }
Zhang Ruice44e192008-01-17 15:51:25 +0800471 else
472 tz->trips.passive.flags.valid = 1;
473
474 if (memcmp(&tz->trips.passive.devices, &devices,
475 sizeof(struct acpi_handle_list))) {
476 memcpy(&tz->trips.passive.devices, &devices,
477 sizeof(struct acpi_handle_list));
478 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
479 }
480 }
481 if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
482 if (valid != tz->trips.passive.flags.valid)
483 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
484 }
485
486 /* Active (optional) */
Len Brown4be44fc2005-08-05 00:44:28 -0400487 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Len Brown4be44fc2005-08-05 00:44:28 -0400488 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
Zhang Ruice44e192008-01-17 15:51:25 +0800489 valid = tz->trips.active[i].flags.valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Len Brownf8707ec2007-08-12 00:12:54 -0400491 if (act == -1)
Zhang Ruice44e192008-01-17 15:51:25 +0800492 break; /* disable all active trip points */
Len Brownf8707ec2007-08-12 00:12:54 -0400493
Zhang Rui0e4240d2009-01-16 12:53:42 -0500494 if ((flag == ACPI_TRIPS_INIT) || ((flag & ACPI_TRIPS_ACTIVE) &&
495 tz->trips.active[i].flags.valid)) {
Zhang Ruice44e192008-01-17 15:51:25 +0800496 status = acpi_evaluate_integer(tz->device->handle,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400497 name, NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800498 if (ACPI_FAILURE(status)) {
499 tz->trips.active[i].flags.valid = 0;
500 if (i == 0)
501 break;
502 if (act <= 0)
503 break;
504 if (i == 1)
505 tz->trips.active[0].temperature =
506 CELSIUS_TO_KELVIN(act);
507 else
508 /*
509 * Don't allow override higher than
510 * the next higher trip point
511 */
512 tz->trips.active[i - 1].temperature =
513 (tz->trips.active[i - 2].temperature <
514 CELSIUS_TO_KELVIN(act) ?
515 tz->trips.active[i - 2].temperature :
516 CELSIUS_TO_KELVIN(act));
Len Brownf8707ec2007-08-12 00:12:54 -0400517 break;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400518 } else {
519 tz->trips.active[i].temperature = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800520 tz->trips.active[i].flags.valid = 1;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400521 }
Len Brownf8707ec2007-08-12 00:12:54 -0400522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 name[2] = 'L';
Zhang Ruice44e192008-01-17 15:51:25 +0800525 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].flags.valid ) {
526 memset(&devices, 0, sizeof(struct acpi_handle_list));
527 status = acpi_evaluate_reference(tz->device->handle,
528 name, NULL, &devices);
Zhang Rui0e4240d2009-01-16 12:53:42 -0500529 if (ACPI_FAILURE(status)) {
530 printk(KERN_WARNING PREFIX
531 "Invalid active%d threshold\n", i);
Zhang Ruice44e192008-01-17 15:51:25 +0800532 tz->trips.active[i].flags.valid = 0;
Zhang Rui0e4240d2009-01-16 12:53:42 -0500533 }
Zhang Ruice44e192008-01-17 15:51:25 +0800534 else
535 tz->trips.active[i].flags.valid = 1;
536
537 if (memcmp(&tz->trips.active[i].devices, &devices,
538 sizeof(struct acpi_handle_list))) {
539 memcpy(&tz->trips.active[i].devices, &devices,
540 sizeof(struct acpi_handle_list));
541 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
542 }
543 }
544 if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
545 if (valid != tz->trips.active[i].flags.valid)
546 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
547
548 if (!tz->trips.active[i].flags.valid)
549 break;
550 }
551
552 if (flag & ACPI_TRIPS_DEVICES) {
553 memset(&devices, 0, sizeof(struct acpi_handle_list));
554 status = acpi_evaluate_reference(tz->device->handle, "_TZD",
555 NULL, &devices);
556 if (memcmp(&tz->devices, &devices,
557 sizeof(struct acpi_handle_list))) {
558 memcpy(&tz->devices, &devices,
559 sizeof(struct acpi_handle_list));
560 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563
Patrick Mocheld550d982006-06-27 00:41:40 -0400564 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Zhang Ruice44e192008-01-17 15:51:25 +0800567static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Zhang Ruice44e192008-01-17 15:51:25 +0800569 return acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
Len Brown4be44fc2005-08-05 00:44:28 -0400572static int acpi_thermal_critical(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Zhang Rui223630f2007-12-06 23:36:35 -0500574 if (!tz || !tz->trips.critical.flags.valid)
Patrick Mocheld550d982006-06-27 00:41:40 -0400575 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 if (tz->temperature >= tz->trips.critical.temperature) {
Len Browncece9292006-06-26 23:04:31 -0400578 printk(KERN_WARNING PREFIX "Critical trip point\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 tz->trips.critical.flags.enabled = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400580 } else if (tz->trips.critical.flags.enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 tz->trips.critical.flags.enabled = 0;
582
Len Brown14e04fb32007-08-23 15:20:26 -0400583 acpi_bus_generate_proc_event(tz->device, ACPI_THERMAL_NOTIFY_CRITICAL,
Len Brown4be44fc2005-08-05 00:44:28 -0400584 tz->trips.critical.flags.enabled);
Zhang Rui962ce8c2007-08-23 01:24:31 +0800585 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +0100586 dev_name(&tz->device->dev),
Zhang Rui962ce8c2007-08-23 01:24:31 +0800587 ACPI_THERMAL_NOTIFY_CRITICAL,
588 tz->trips.critical.flags.enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Zhang Rui223630f2007-12-06 23:36:35 -0500590 /* take no action if nocrt is set */
591 if(!nocrt) {
592 printk(KERN_EMERG
593 "Critical temperature reached (%ld C), shutting down.\n",
594 KELVIN_TO_CELSIUS(tz->temperature));
595 orderly_poweroff(true);
596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Patrick Mocheld550d982006-06-27 00:41:40 -0400598 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
Len Brown4be44fc2005-08-05 00:44:28 -0400601static int acpi_thermal_hot(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Zhang Rui223630f2007-12-06 23:36:35 -0500603 if (!tz || !tz->trips.hot.flags.valid)
Patrick Mocheld550d982006-06-27 00:41:40 -0400604 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 if (tz->temperature >= tz->trips.hot.temperature) {
Len Browncece9292006-06-26 23:04:31 -0400607 printk(KERN_WARNING PREFIX "Hot trip point\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 tz->trips.hot.flags.enabled = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400609 } else if (tz->trips.hot.flags.enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 tz->trips.hot.flags.enabled = 0;
611
Len Brown14e04fb32007-08-23 15:20:26 -0400612 acpi_bus_generate_proc_event(tz->device, ACPI_THERMAL_NOTIFY_HOT,
Len Brown4be44fc2005-08-05 00:44:28 -0400613 tz->trips.hot.flags.enabled);
Zhang Rui962ce8c2007-08-23 01:24:31 +0800614 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +0100615 dev_name(&tz->device->dev),
Zhang Rui962ce8c2007-08-23 01:24:31 +0800616 ACPI_THERMAL_NOTIFY_HOT,
617 tz->trips.hot.flags.enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Zhang Rui223630f2007-12-06 23:36:35 -0500619 /* TBD: Call user-mode "sleep(S4)" function if nocrt is cleared */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Patrick Mocheld550d982006-06-27 00:41:40 -0400621 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400624static void acpi_thermal_passive(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400626 int result = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 struct acpi_thermal_passive *passive = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400628 int trend = 0;
629 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 if (!tz || !tz->trips.passive.flags.valid)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400633 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 passive = &(tz->trips.passive);
636
637 /*
638 * Above Trip?
639 * -----------
640 * Calculate the thermal trend (using the passive cooling equation)
641 * and modify the performance limit for all passive cooling devices
642 * accordingly. Note that we assume symmetry.
643 */
644 if (tz->temperature >= passive->temperature) {
Len Brown4be44fc2005-08-05 00:44:28 -0400645 trend =
646 (passive->tc1 * (tz->temperature - tz->last_temperature)) +
647 (passive->tc2 * (tz->temperature - passive->temperature));
648 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
649 "trend[%d]=(tc1[%lu]*(tmp[%lu]-last[%lu]))+(tc2[%lu]*(tmp[%lu]-psv[%lu]))\n",
650 trend, passive->tc1, tz->temperature,
651 tz->last_temperature, passive->tc2,
652 tz->temperature, passive->temperature));
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400653 passive->flags.enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 /* Heating up? */
655 if (trend > 0)
Len Brown4be44fc2005-08-05 00:44:28 -0400656 for (i = 0; i < passive->devices.count; i++)
657 acpi_processor_set_thermal_limit(passive->
658 devices.
659 handles[i],
660 ACPI_PROCESSOR_LIMIT_INCREMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 /* Cooling off? */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400662 else if (trend < 0) {
Len Brown4be44fc2005-08-05 00:44:28 -0400663 for (i = 0; i < passive->devices.count; i++)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400664 /*
665 * assume that we are on highest
666 * freq/lowest thrott and can leave
667 * passive mode, even in error case
668 */
669 if (!acpi_processor_set_thermal_limit
670 (passive->devices.handles[i],
671 ACPI_PROCESSOR_LIMIT_DECREMENT))
672 result = 0;
673 /*
674 * Leave cooling mode, even if the temp might
675 * higher than trip point This is because some
676 * machines might have long thermal polling
677 * frequencies (tsp) defined. We will fall back
678 * into passive mode in next cycle (probably quicker)
679 */
680 if (result) {
681 passive->flags.enabled = 0;
682 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
683 "Disabling passive cooling, still above threshold,"
684 " but we are cooling down\n"));
685 }
686 }
687 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
689
690 /*
691 * Below Trip?
692 * -----------
693 * Implement passive cooling hysteresis to slowly increase performance
694 * and avoid thrashing around the passive trip point. Note that we
695 * assume symmetry.
696 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400697 if (!passive->flags.enabled)
698 return;
699 for (i = 0; i < passive->devices.count; i++)
700 if (!acpi_processor_set_thermal_limit
701 (passive->devices.handles[i],
702 ACPI_PROCESSOR_LIMIT_DECREMENT))
703 result = 0;
704 if (result) {
705 passive->flags.enabled = 0;
706 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
707 "Disabling passive cooling (zone is cool)\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400711static void acpi_thermal_active(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Len Brown4be44fc2005-08-05 00:44:28 -0400713 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 struct acpi_thermal_active *active = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400715 int i = 0;
716 int j = 0;
717 unsigned long maxtemp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 if (!tz)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400721 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Len Brown4be44fc2005-08-05 00:44:28 -0400723 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 active = &(tz->trips.active[i]);
725 if (!active || !active->flags.valid)
726 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (tz->temperature >= active->temperature) {
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400728 /*
729 * Above Threshold?
730 * ----------------
731 * If not already enabled, turn ON all cooling devices
732 * associated with this active threshold.
733 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (active->temperature > maxtemp)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400735 tz->state.active_index = i;
736 maxtemp = active->temperature;
737 if (active->flags.enabled)
738 continue;
739 for (j = 0; j < active->devices.count; j++) {
740 result =
741 acpi_bus_set_power(active->devices.
742 handles[j],
743 ACPI_STATE_D0);
744 if (result) {
Len Browncece9292006-06-26 23:04:31 -0400745 printk(KERN_WARNING PREFIX
746 "Unable to turn cooling device [%p] 'on'\n",
Thomas Renningera6fc6722006-06-26 23:58:43 -0400747 active->devices.
Len Browncece9292006-06-26 23:04:31 -0400748 handles[j]);
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400749 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400751 active->flags.enabled = 1;
752 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
753 "Cooling device [%p] now 'on'\n",
754 active->devices.handles[j]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400756 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400758 if (!active->flags.enabled)
759 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 /*
761 * Below Threshold?
762 * ----------------
763 * Turn OFF all cooling devices associated with this
764 * threshold.
765 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400766 for (j = 0; j < active->devices.count; j++) {
767 result = acpi_bus_set_power(active->devices.handles[j],
768 ACPI_STATE_D3);
769 if (result) {
Len Browncece9292006-06-26 23:04:31 -0400770 printk(KERN_WARNING PREFIX
771 "Unable to turn cooling device [%p] 'off'\n",
772 active->devices.handles[j]);
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400773 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400775 active->flags.enabled = 0;
776 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
777 "Cooling device [%p] now 'off'\n",
778 active->devices.handles[j]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
Len Brown4be44fc2005-08-05 00:44:28 -0400783static void acpi_thermal_check(void *context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Len Brown4be44fc2005-08-05 00:44:28 -0400785static void acpi_thermal_run(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 struct acpi_thermal *tz = (struct acpi_thermal *)data;
788 if (!tz->zombie)
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400789 acpi_os_execute(OSL_GPE_HANDLER, acpi_thermal_check, (void *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
Zhao Yakuiea510112008-07-14 15:14:03 +0800792static void acpi_thermal_active_off(void *data)
793{
794 int result = 0;
795 struct acpi_thermal *tz = data;
796 int i = 0;
797 int j = 0;
798 struct acpi_thermal_active *active = NULL;
799
800 if (!tz) {
801 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
802 return;
803 }
804
805 result = acpi_thermal_get_temperature(tz);
806 if (result)
807 return;
808
809 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
810 active = &(tz->trips.active[i]);
811 if (!active || !active->flags.valid)
812 break;
813 if (tz->temperature >= active->temperature) {
814 /*
815 * If the thermal temperature is greater than the
816 * active threshod, unnecessary to turn off the
817 * the active cooling device.
818 */
819 continue;
820 }
821 /*
822 * Below Threshold?
823 * ----------------
824 * Turn OFF all cooling devices associated with this
825 * threshold.
826 */
827 for (j = 0; j < active->devices.count; j++)
828 result = acpi_bus_set_power(active->devices.handles[j],
829 ACPI_STATE_D3);
830 }
831}
832
Len Brown4be44fc2005-08-05 00:44:28 -0400833static void acpi_thermal_check(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
Len Brown4be44fc2005-08-05 00:44:28 -0400835 int result = 0;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200836 struct acpi_thermal *tz = data;
Len Brown4be44fc2005-08-05 00:44:28 -0400837 unsigned long sleep_time = 0;
Len Brown21bc42a2007-09-04 12:49:22 -0400838 unsigned long timeout_jiffies = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400839 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 struct acpi_thermal_state state;
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 if (!tz) {
Len Brown64684632006-06-26 23:41:38 -0400844 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400845 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
847
Alexey Starikovskiy6e215782007-09-01 00:11:59 +0400848 /* Check if someone else is already running */
849 if (!mutex_trylock(&tz->lock))
850 return;
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 state = tz->state;
853
854 result = acpi_thermal_get_temperature(tz);
855 if (result)
Alexey Starikovskiy6e215782007-09-01 00:11:59 +0400856 goto unlock;
Len Brown4be44fc2005-08-05 00:44:28 -0400857
Zhang Rui3f655ef2008-01-17 15:51:11 +0800858 if (!tz->tz_enabled)
859 goto unlock;
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 memset(&tz->state, 0, sizeof(tz->state));
Len Brown4be44fc2005-08-05 00:44:28 -0400862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 /*
864 * Check Trip Points
865 * -----------------
866 * Compare the current temperature to the trip point values to see
867 * if we've entered one of the thermal policy states. Note that
868 * this function determines when a state is entered, but the
869 * individual policy decides when it is exited (e.g. hysteresis).
870 */
871 if (tz->trips.critical.flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400872 state.critical |=
873 (tz->temperature >= tz->trips.critical.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if (tz->trips.hot.flags.valid)
875 state.hot |= (tz->temperature >= tz->trips.hot.temperature);
876 if (tz->trips.passive.flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400877 state.passive |=
878 (tz->temperature >= tz->trips.passive.temperature);
879 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (tz->trips.active[i].flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400881 state.active |=
882 (tz->temperature >=
883 tz->trips.active[i].temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 /*
886 * Invoke Policy
887 * -------------
888 * Separated from the above check to allow individual policy to
889 * determine when to exit a given state.
890 */
891 if (state.critical)
892 acpi_thermal_critical(tz);
893 if (state.hot)
894 acpi_thermal_hot(tz);
895 if (state.passive)
896 acpi_thermal_passive(tz);
897 if (state.active)
898 acpi_thermal_active(tz);
899
900 /*
901 * Calculate State
902 * ---------------
903 * Again, separated from the above two to allow independent policy
904 * decisions.
905 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400906 tz->state.critical = tz->trips.critical.flags.enabled;
907 tz->state.hot = tz->trips.hot.flags.enabled;
908 tz->state.passive = tz->trips.passive.flags.enabled;
909 tz->state.active = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400910 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400911 tz->state.active |= tz->trips.active[i].flags.enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 /*
914 * Calculate Sleep Time
915 * --------------------
916 * If we're in the passive state, use _TSP's value. Otherwise
917 * use the default polling frequency (e.g. _TZP). If no polling
918 * frequency is specified then we'll wait forever (at least until
919 * a thermal event occurs). Note that _TSP and _TZD values are
920 * given in 1/10th seconds (we must covert to milliseconds).
921 */
Len Brown21bc42a2007-09-04 12:49:22 -0400922 if (tz->state.passive) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 sleep_time = tz->trips.passive.tsp * 100;
Len Brown21bc42a2007-09-04 12:49:22 -0400924 timeout_jiffies = jiffies + (HZ * sleep_time) / 1000;
925 } else if (tz->polling_frequency > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 sleep_time = tz->polling_frequency * 100;
Len Brown21bc42a2007-09-04 12:49:22 -0400927 timeout_jiffies = round_jiffies(jiffies + (HZ * sleep_time) / 1000);
928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Len Brown4be44fc2005-08-05 00:44:28 -0400930 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: temperature[%lu] sleep[%lu]\n",
931 tz->name, tz->temperature, sleep_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 /*
934 * Schedule Next Poll
935 * ------------------
936 */
937 if (!sleep_time) {
938 if (timer_pending(&(tz->timer)))
939 del_timer(&(tz->timer));
Len Brown4be44fc2005-08-05 00:44:28 -0400940 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (timer_pending(&(tz->timer)))
Len Brown21bc42a2007-09-04 12:49:22 -0400942 mod_timer(&(tz->timer), timeout_jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400944 tz->timer.data = (unsigned long)tz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 tz->timer.function = acpi_thermal_run;
Len Brown21bc42a2007-09-04 12:49:22 -0400946 tz->timer.expires = timeout_jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 add_timer(&(tz->timer));
948 }
949 }
Alexey Starikovskiy6e215782007-09-01 00:11:59 +0400950 unlock:
951 mutex_unlock(&tz->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
Zhang Rui3f655ef2008-01-17 15:51:11 +0800954/* sys I/F for generic thermal sysfs support */
Zhang, Rui5e012762008-02-28 07:51:30 +0800955#define KELVIN_TO_MILLICELSIUS(t) (t * 100 - 273200)
956
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000957static int thermal_get_temp(struct thermal_zone_device *thermal,
958 unsigned long *temp)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800959{
960 struct acpi_thermal *tz = thermal->devdata;
Zhang, Rui76ecb4f2008-04-10 16:20:23 +0800961 int result;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800962
963 if (!tz)
964 return -EINVAL;
965
Zhang, Rui76ecb4f2008-04-10 16:20:23 +0800966 result = acpi_thermal_get_temperature(tz);
967 if (result)
968 return result;
969
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000970 *temp = KELVIN_TO_MILLICELSIUS(tz->temperature);
971 return 0;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800972}
973
974static const char enabled[] = "kernel";
975static const char disabled[] = "user";
976static int thermal_get_mode(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000977 enum thermal_device_mode *mode)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800978{
979 struct acpi_thermal *tz = thermal->devdata;
980
981 if (!tz)
982 return -EINVAL;
983
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000984 *mode = tz->tz_enabled ? THERMAL_DEVICE_ENABLED :
985 THERMAL_DEVICE_DISABLED;
986
987 return 0;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800988}
989
990static int thermal_set_mode(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000991 enum thermal_device_mode mode)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800992{
993 struct acpi_thermal *tz = thermal->devdata;
994 int enable;
995
996 if (!tz)
997 return -EINVAL;
998
999 /*
1000 * enable/disable thermal management from ACPI thermal driver
1001 */
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001002 if (mode == THERMAL_DEVICE_ENABLED)
Zhang Rui3f655ef2008-01-17 15:51:11 +08001003 enable = 1;
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001004 else if (mode == THERMAL_DEVICE_DISABLED)
Zhang Rui3f655ef2008-01-17 15:51:11 +08001005 enable = 0;
1006 else
1007 return -EINVAL;
1008
1009 if (enable != tz->tz_enabled) {
1010 tz->tz_enabled = enable;
1011 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1012 "%s ACPI thermal control\n",
1013 tz->tz_enabled ? enabled : disabled));
1014 acpi_thermal_check(tz);
1015 }
1016 return 0;
1017}
1018
1019static int thermal_get_trip_type(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001020 int trip, enum thermal_trip_type *type)
Zhang Rui3f655ef2008-01-17 15:51:11 +08001021{
1022 struct acpi_thermal *tz = thermal->devdata;
1023 int i;
1024
1025 if (!tz || trip < 0)
1026 return -EINVAL;
1027
1028 if (tz->trips.critical.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001029 if (!trip) {
1030 *type = THERMAL_TRIP_CRITICAL;
1031 return 0;
1032 }
Zhang Rui3f655ef2008-01-17 15:51:11 +08001033 trip--;
1034 }
1035
1036 if (tz->trips.hot.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001037 if (!trip) {
1038 *type = THERMAL_TRIP_HOT;
1039 return 0;
1040 }
Zhang Rui3f655ef2008-01-17 15:51:11 +08001041 trip--;
1042 }
1043
1044 if (tz->trips.passive.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001045 if (!trip) {
1046 *type = THERMAL_TRIP_PASSIVE;
1047 return 0;
1048 }
Zhang Rui3f655ef2008-01-17 15:51:11 +08001049 trip--;
1050 }
1051
1052 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
1053 tz->trips.active[i].flags.valid; i++) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001054 if (!trip) {
1055 *type = THERMAL_TRIP_ACTIVE;
1056 return 0;
1057 }
Zhang Rui3f655ef2008-01-17 15:51:11 +08001058 trip--;
1059 }
1060
1061 return -EINVAL;
1062}
1063
1064static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001065 int trip, unsigned long *temp)
Zhang Rui3f655ef2008-01-17 15:51:11 +08001066{
1067 struct acpi_thermal *tz = thermal->devdata;
1068 int i;
1069
1070 if (!tz || trip < 0)
1071 return -EINVAL;
1072
1073 if (tz->trips.critical.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001074 if (!trip) {
1075 *temp = KELVIN_TO_MILLICELSIUS(
1076 tz->trips.critical.temperature);
1077 return 0;
1078 }
Zhang Rui3f655ef2008-01-17 15:51:11 +08001079 trip--;
1080 }
1081
1082 if (tz->trips.hot.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001083 if (!trip) {
1084 *temp = KELVIN_TO_MILLICELSIUS(
1085 tz->trips.hot.temperature);
1086 return 0;
1087 }
Zhang Rui3f655ef2008-01-17 15:51:11 +08001088 trip--;
1089 }
1090
1091 if (tz->trips.passive.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001092 if (!trip) {
1093 *temp = KELVIN_TO_MILLICELSIUS(
1094 tz->trips.passive.temperature);
1095 return 0;
1096 }
Zhang Rui3f655ef2008-01-17 15:51:11 +08001097 trip--;
1098 }
1099
1100 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
1101 tz->trips.active[i].flags.valid; i++) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001102 if (!trip) {
1103 *temp = KELVIN_TO_MILLICELSIUS(
1104 tz->trips.active[i].temperature);
1105 return 0;
1106 }
Zhang Rui3f655ef2008-01-17 15:51:11 +08001107 trip--;
1108 }
1109
1110 return -EINVAL;
1111}
1112
Zhang, Rui9ec732f2008-04-10 16:13:10 +08001113static int thermal_get_crit_temp(struct thermal_zone_device *thermal,
1114 unsigned long *temperature) {
1115 struct acpi_thermal *tz = thermal->devdata;
1116
1117 if (tz->trips.critical.flags.valid) {
1118 *temperature = KELVIN_TO_MILLICELSIUS(
1119 tz->trips.critical.temperature);
1120 return 0;
1121 } else
1122 return -EINVAL;
1123}
1124
Zhang Rui3f655ef2008-01-17 15:51:11 +08001125typedef int (*cb)(struct thermal_zone_device *, int,
1126 struct thermal_cooling_device *);
1127static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
1128 struct thermal_cooling_device *cdev,
1129 cb action)
1130{
1131 struct acpi_device *device = cdev->devdata;
1132 struct acpi_thermal *tz = thermal->devdata;
Zhang Rui653a00c2008-01-17 15:51:18 +08001133 struct acpi_device *dev;
1134 acpi_status status;
1135 acpi_handle handle;
Zhang Rui3f655ef2008-01-17 15:51:11 +08001136 int i;
1137 int j;
1138 int trip = -1;
1139 int result = 0;
1140
1141 if (tz->trips.critical.flags.valid)
1142 trip++;
1143
1144 if (tz->trips.hot.flags.valid)
1145 trip++;
1146
1147 if (tz->trips.passive.flags.valid) {
1148 trip++;
1149 for (i = 0; i < tz->trips.passive.devices.count;
1150 i++) {
Zhang Rui653a00c2008-01-17 15:51:18 +08001151 handle = tz->trips.passive.devices.handles[i];
1152 status = acpi_bus_get_device(handle, &dev);
1153 if (ACPI_SUCCESS(status) && (dev == device)) {
1154 result = action(thermal, trip, cdev);
1155 if (result)
1156 goto failed;
1157 }
Zhang Rui3f655ef2008-01-17 15:51:11 +08001158 }
1159 }
1160
1161 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1162 if (!tz->trips.active[i].flags.valid)
1163 break;
1164 trip++;
1165 for (j = 0;
1166 j < tz->trips.active[i].devices.count;
1167 j++) {
Zhang Rui653a00c2008-01-17 15:51:18 +08001168 handle = tz->trips.active[i].devices.handles[j];
1169 status = acpi_bus_get_device(handle, &dev);
1170 if (ACPI_SUCCESS(status) && (dev == device)) {
1171 result = action(thermal, trip, cdev);
1172 if (result)
1173 goto failed;
1174 }
Zhang Rui3f655ef2008-01-17 15:51:11 +08001175 }
1176 }
1177
1178 for (i = 0; i < tz->devices.count; i++) {
Zhang Rui653a00c2008-01-17 15:51:18 +08001179 handle = tz->devices.handles[i];
1180 status = acpi_bus_get_device(handle, &dev);
1181 if (ACPI_SUCCESS(status) && (dev == device)) {
1182 result = action(thermal, -1, cdev);
1183 if (result)
1184 goto failed;
1185 }
Zhang Rui3f655ef2008-01-17 15:51:11 +08001186 }
1187
1188failed:
1189 return result;
1190}
1191
1192static int
1193acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
1194 struct thermal_cooling_device *cdev)
1195{
1196 return acpi_thermal_cooling_device_cb(thermal, cdev,
1197 thermal_zone_bind_cooling_device);
1198}
1199
1200static int
1201acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
1202 struct thermal_cooling_device *cdev)
1203{
1204 return acpi_thermal_cooling_device_cb(thermal, cdev,
1205 thermal_zone_unbind_cooling_device);
1206}
1207
1208static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
1209 .bind = acpi_thermal_bind_cooling_device,
1210 .unbind = acpi_thermal_unbind_cooling_device,
1211 .get_temp = thermal_get_temp,
1212 .get_mode = thermal_get_mode,
1213 .set_mode = thermal_set_mode,
1214 .get_trip_type = thermal_get_trip_type,
1215 .get_trip_temp = thermal_get_trip_temp,
Zhang, Rui9ec732f2008-04-10 16:13:10 +08001216 .get_crit_temp = thermal_get_crit_temp,
Zhang Rui3f655ef2008-01-17 15:51:11 +08001217};
1218
1219static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
1220{
1221 int trips = 0;
1222 int result;
Zhang Rui20733932008-01-17 15:51:21 +08001223 acpi_status status;
Zhang Rui3f655ef2008-01-17 15:51:11 +08001224 int i;
1225
1226 if (tz->trips.critical.flags.valid)
1227 trips++;
1228
1229 if (tz->trips.hot.flags.valid)
1230 trips++;
1231
1232 if (tz->trips.passive.flags.valid)
1233 trips++;
1234
1235 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
1236 tz->trips.active[i].flags.valid; i++, trips++);
Zhang Ruie9ae7102008-04-22 08:50:09 +08001237 tz->thermal_zone = thermal_zone_device_register("acpitz",
Zhang Rui3f655ef2008-01-17 15:51:11 +08001238 trips, tz, &acpi_thermal_zone_ops);
Krzysztof Heltbb070e42008-04-08 17:41:52 -07001239 if (IS_ERR(tz->thermal_zone))
Zhang Rui3f655ef2008-01-17 15:51:11 +08001240 return -ENODEV;
1241
1242 result = sysfs_create_link(&tz->device->dev.kobj,
1243 &tz->thermal_zone->device.kobj, "thermal_zone");
1244 if (result)
1245 return result;
1246
1247 result = sysfs_create_link(&tz->thermal_zone->device.kobj,
1248 &tz->device->dev.kobj, "device");
1249 if (result)
1250 return result;
1251
Zhang Rui20733932008-01-17 15:51:21 +08001252 status = acpi_attach_data(tz->device->handle,
1253 acpi_bus_private_data_handler,
1254 tz->thermal_zone);
1255 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001256 printk(KERN_ERR PREFIX
1257 "Error attaching device data\n");
Zhang Rui20733932008-01-17 15:51:21 +08001258 return -ENODEV;
1259 }
1260
Zhang Rui3f655ef2008-01-17 15:51:11 +08001261 tz->tz_enabled = 1;
1262
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +02001263 dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
1264 tz->thermal_zone->id);
Zhang Rui3f655ef2008-01-17 15:51:11 +08001265 return 0;
1266}
1267
1268static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
1269{
1270 sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
1271 sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
1272 thermal_zone_device_unregister(tz->thermal_zone);
1273 tz->thermal_zone = NULL;
Zhang Rui20733932008-01-17 15:51:21 +08001274 acpi_detach_data(tz->device->handle, acpi_bus_private_data_handler);
Zhang Rui3f655ef2008-01-17 15:51:11 +08001275}
1276
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278/* --------------------------------------------------------------------------
1279 FS Interface (/proc)
1280 -------------------------------------------------------------------------- */
1281
Len Brown4be44fc2005-08-05 00:44:28 -04001282static struct proc_dir_entry *acpi_thermal_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
1285{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001286 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 if (!tz)
1290 goto end;
1291
1292 seq_puts(seq, "state: ");
1293
Len Brown4be44fc2005-08-05 00:44:28 -04001294 if (!tz->state.critical && !tz->state.hot && !tz->state.passive
1295 && !tz->state.active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 seq_puts(seq, "ok\n");
1297 else {
1298 if (tz->state.critical)
1299 seq_puts(seq, "critical ");
1300 if (tz->state.hot)
1301 seq_puts(seq, "hot ");
1302 if (tz->state.passive)
1303 seq_puts(seq, "passive ");
1304 if (tz->state.active)
1305 seq_printf(seq, "active[%d]", tz->state.active_index);
1306 seq_puts(seq, "\n");
1307 }
1308
Len Brown4be44fc2005-08-05 00:44:28 -04001309 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001310 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
1312
1313static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
1314{
1315 return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
1316}
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
1319{
Len Brown4be44fc2005-08-05 00:44:28 -04001320 int result = 0;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001321 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 if (!tz)
1325 goto end;
1326
1327 result = acpi_thermal_get_temperature(tz);
1328 if (result)
1329 goto end;
1330
Len Brown4be44fc2005-08-05 00:44:28 -04001331 seq_printf(seq, "temperature: %ld C\n",
1332 KELVIN_TO_CELSIUS(tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Len Brown4be44fc2005-08-05 00:44:28 -04001334 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001335 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
1337
1338static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
1339{
1340 return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
1341}
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
1344{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001345 struct acpi_thermal *tz = seq->private;
Thomas Renninger68ccfaa2006-11-19 23:01:40 +01001346 struct acpi_device *device;
Thomas Renningere7c746e2007-06-18 00:40:51 -04001347 acpi_status status;
1348
Len Brown4be44fc2005-08-05 00:44:28 -04001349 int i = 0;
1350 int j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 if (!tz)
1354 goto end;
1355
1356 if (tz->trips.critical.flags.valid)
Len Brownf5487142007-08-12 00:12:44 -04001357 seq_printf(seq, "critical (S5): %ld C%s",
1358 KELVIN_TO_CELSIUS(tz->trips.critical.temperature),
1359 nocrt ? " <disabled>\n" : "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 if (tz->trips.hot.flags.valid)
Len Brownf5487142007-08-12 00:12:44 -04001362 seq_printf(seq, "hot (S4): %ld C%s",
1363 KELVIN_TO_CELSIUS(tz->trips.hot.temperature),
1364 nocrt ? " <disabled>\n" : "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 if (tz->trips.passive.flags.valid) {
Len Brown4be44fc2005-08-05 00:44:28 -04001367 seq_printf(seq,
1368 "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
1369 KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
1370 tz->trips.passive.tc1, tz->trips.passive.tc2,
1371 tz->trips.passive.tsp);
1372 for (j = 0; j < tz->trips.passive.devices.count; j++) {
Thomas Renningere7c746e2007-06-18 00:40:51 -04001373 status = acpi_bus_get_device(tz->trips.passive.devices.
1374 handles[j], &device);
1375 seq_printf(seq, "%4.4s ", status ? "" :
1376 acpi_device_bid(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
1378 seq_puts(seq, "\n");
1379 }
1380
1381 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1382 if (!(tz->trips.active[i].flags.valid))
1383 break;
1384 seq_printf(seq, "active[%d]: %ld C: devices=",
Len Brown4be44fc2005-08-05 00:44:28 -04001385 i,
1386 KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
Thomas Renninger68ccfaa2006-11-19 23:01:40 +01001387 for (j = 0; j < tz->trips.active[i].devices.count; j++){
Thomas Renningere7c746e2007-06-18 00:40:51 -04001388 status = acpi_bus_get_device(tz->trips.active[i].
1389 devices.handles[j],
1390 &device);
1391 seq_printf(seq, "%4.4s ", status ? "" :
1392 acpi_device_bid(device));
Thomas Renninger68ccfaa2006-11-19 23:01:40 +01001393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 seq_puts(seq, "\n");
1395 }
1396
Len Brown4be44fc2005-08-05 00:44:28 -04001397 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001398 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399}
1400
1401static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
1402{
1403 return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
1404}
1405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
1407{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001408 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 if (!tz)
1412 goto end;
1413
Len Browneaca2d32007-04-30 23:27:43 -04001414 if (!tz->flags.cooling_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 seq_puts(seq, "<setting not supported>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 else
Len Browneaca2d32007-04-30 23:27:43 -04001417 seq_puts(seq, "0 - Active; 1 - Passive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Len Brown4be44fc2005-08-05 00:44:28 -04001419 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001420 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421}
1422
1423static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
1424{
1425 return single_open(file, acpi_thermal_cooling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001426 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427}
1428
1429static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001430acpi_thermal_write_cooling_mode(struct file *file,
1431 const char __user * buffer,
1432 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001434 struct seq_file *m = file->private_data;
1435 struct acpi_thermal *tz = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001436 int result = 0;
1437 char mode_string[12] = { '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 if (!tz || (count > sizeof(mode_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001441 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 if (!tz->flags.cooling_mode)
Patrick Mocheld550d982006-06-27 00:41:40 -04001444 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 if (copy_from_user(mode_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001447 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -04001448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 mode_string[count] = '\0';
Len Brown4be44fc2005-08-05 00:44:28 -04001450
1451 result = acpi_thermal_set_cooling_mode(tz,
1452 simple_strtoul(mode_string, NULL,
1453 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001455 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 acpi_thermal_check(tz);
1458
Patrick Mocheld550d982006-06-27 00:41:40 -04001459 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
1463{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001464 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467 if (!tz)
1468 goto end;
1469
1470 if (!tz->polling_frequency) {
1471 seq_puts(seq, "<polling disabled>\n");
1472 goto end;
1473 }
1474
1475 seq_printf(seq, "polling frequency: %lu seconds\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001476 (tz->polling_frequency / 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Len Brown4be44fc2005-08-05 00:44:28 -04001478 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001479 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480}
1481
1482static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
1483{
1484 return single_open(file, acpi_thermal_polling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001485 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486}
1487
1488static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001489acpi_thermal_write_polling(struct file *file,
1490 const char __user * buffer,
1491 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001493 struct seq_file *m = file->private_data;
1494 struct acpi_thermal *tz = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001495 int result = 0;
1496 char polling_string[12] = { '\0' };
1497 int seconds = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 if (!tz || (count > sizeof(polling_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001501 return -EINVAL;
Len Brown4be44fc2005-08-05 00:44:28 -04001502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 if (copy_from_user(polling_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001504 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -04001505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 polling_string[count] = '\0';
1507
1508 seconds = simple_strtoul(polling_string, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 result = acpi_thermal_set_polling(tz, seconds);
1511 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001512 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514 acpi_thermal_check(tz);
1515
Patrick Mocheld550d982006-06-27 00:41:40 -04001516 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
1518
Len Brown4be44fc2005-08-05 00:44:28 -04001519static int acpi_thermal_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
Len Brown4be44fc2005-08-05 00:44:28 -04001521 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 if (!acpi_device_dir(device)) {
1525 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -04001526 acpi_thermal_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001528 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 acpi_device_dir(device)->owner = THIS_MODULE;
1530 }
1531
1532 /* 'state' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001533 entry = proc_create_data(ACPI_THERMAL_FILE_STATE,
1534 S_IRUGO, acpi_device_dir(device),
1535 &acpi_thermal_state_fops,
1536 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001538 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
1540 /* 'temperature' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001541 entry = proc_create_data(ACPI_THERMAL_FILE_TEMPERATURE,
1542 S_IRUGO, acpi_device_dir(device),
1543 &acpi_thermal_temp_fops,
1544 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001546 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Pavel Machek2db9ccb2007-08-24 11:45:50 +02001548 /* 'trip_points' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001549 entry = proc_create_data(ACPI_THERMAL_FILE_TRIP_POINTS,
1550 S_IRUGO,
1551 acpi_device_dir(device),
1552 &acpi_thermal_trip_fops,
1553 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001555 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557 /* 'cooling_mode' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001558 entry = proc_create_data(ACPI_THERMAL_FILE_COOLING_MODE,
1559 S_IFREG | S_IRUGO | S_IWUSR,
1560 acpi_device_dir(device),
1561 &acpi_thermal_cooling_fops,
1562 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001564 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 /* 'polling_frequency' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001567 entry = proc_create_data(ACPI_THERMAL_FILE_POLLING_FREQ,
1568 S_IFREG | S_IRUGO | S_IWUSR,
1569 acpi_device_dir(device),
1570 &acpi_thermal_polling_fops,
1571 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001573 return -ENODEV;
Patrick Mocheld550d982006-06-27 00:41:40 -04001574 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
Len Brown4be44fc2005-08-05 00:44:28 -04001577static int acpi_thermal_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 if (acpi_device_dir(device)) {
1581 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1582 acpi_device_dir(device));
1583 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1584 acpi_device_dir(device));
1585 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1586 acpi_device_dir(device));
1587 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1588 acpi_device_dir(device));
1589 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1590 acpi_device_dir(device));
1591 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1592 acpi_device_dir(device) = NULL;
1593 }
1594
Patrick Mocheld550d982006-06-27 00:41:40 -04001595 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596}
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598/* --------------------------------------------------------------------------
1599 Driver Interface
1600 -------------------------------------------------------------------------- */
1601
Len Brown4be44fc2005-08-05 00:44:28 -04001602static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001604 struct acpi_thermal *tz = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001605 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
1608 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001609 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Patrick Mochel8348e1b2006-05-19 16:54:40 -04001611 device = tz->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
1613 switch (event) {
1614 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1615 acpi_thermal_check(tz);
1616 break;
1617 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
Zhang Ruice44e192008-01-17 15:51:25 +08001618 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 acpi_thermal_check(tz);
Len Brown14e04fb32007-08-23 15:20:26 -04001620 acpi_bus_generate_proc_event(device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08001621 acpi_bus_generate_netlink_event(device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +01001622 dev_name(&device->dev), event, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 break;
1624 case ACPI_THERMAL_NOTIFY_DEVICES:
Zhang Ruice44e192008-01-17 15:51:25 +08001625 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
1626 acpi_thermal_check(tz);
Len Brown14e04fb32007-08-23 15:20:26 -04001627 acpi_bus_generate_proc_event(device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08001628 acpi_bus_generate_netlink_event(device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +01001629 dev_name(&device->dev), event, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 break;
1631 default:
1632 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001633 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 break;
1635 }
1636
Patrick Mocheld550d982006-06-27 00:41:40 -04001637 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
1639
Len Brown4be44fc2005-08-05 00:44:28 -04001640static int acpi_thermal_get_info(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
Len Brown4be44fc2005-08-05 00:44:28 -04001642 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001646 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
1648 /* Get temperature [_TMP] (required) */
1649 result = acpi_thermal_get_temperature(tz);
1650 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001651 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653 /* Get trip points [_CRT, _PSV, etc.] (required) */
1654 result = acpi_thermal_get_trip_points(tz);
1655 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001656 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
1658 /* Set the cooling mode [_SCP] to active cooling (default) */
1659 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
Len Brown4be44fc2005-08-05 00:44:28 -04001660 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 tz->flags.cooling_mode = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
1663 /* Get default polling frequency [_TZP] (optional) */
1664 if (tzp)
1665 tz->polling_frequency = tzp;
1666 else
1667 acpi_thermal_get_polling_frequency(tz);
1668
Patrick Mocheld550d982006-06-27 00:41:40 -04001669 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670}
1671
Len Brown4be44fc2005-08-05 00:44:28 -04001672static int acpi_thermal_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
Len Brown4be44fc2005-08-05 00:44:28 -04001674 int result = 0;
1675 acpi_status status = AE_OK;
1676 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
1679 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001680 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Burman Yan36bcbec2006-12-19 12:56:11 -08001682 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001684 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Patrick Mochel8348e1b2006-05-19 16:54:40 -04001686 tz->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 strcpy(tz->name, device->pnp.bus_id);
1688 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1689 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001690 device->driver_data = tz;
Alexey Starikovskiy6e215782007-09-01 00:11:59 +04001691 mutex_init(&tz->lock);
Zhang Rui3f655ef2008-01-17 15:51:11 +08001692
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 result = acpi_thermal_get_info(tz);
1695 if (result)
Zhang Rui3f655ef2008-01-17 15:51:11 +08001696 goto free_memory;
1697
1698 result = acpi_thermal_register_thermal_zone(tz);
1699 if (result)
1700 goto free_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
1702 result = acpi_thermal_add_fs(device);
1703 if (result)
Zhang Rui3f655ef2008-01-17 15:51:11 +08001704 goto unregister_thermal_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 init_timer(&tz->timer);
1707
Zhao Yakuiea510112008-07-14 15:14:03 +08001708 acpi_thermal_active_off(tz);
1709
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 acpi_thermal_check(tz);
1711
Patrick Mochel38ba7c92006-05-19 16:54:48 -04001712 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001713 ACPI_DEVICE_NOTIFY,
1714 acpi_thermal_notify, tz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 result = -ENODEV;
Zhang Rui3f655ef2008-01-17 15:51:11 +08001717 goto remove_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
1719
1720 printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001721 acpi_device_name(device), acpi_device_bid(device),
1722 KELVIN_TO_CELSIUS(tz->temperature));
Zhang Rui3f655ef2008-01-17 15:51:11 +08001723 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Zhang Rui3f655ef2008-01-17 15:51:11 +08001725remove_fs:
1726 acpi_thermal_remove_fs(device);
1727unregister_thermal_zone:
1728 thermal_zone_device_unregister(tz->thermal_zone);
1729free_memory:
1730 kfree(tz);
1731end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001732 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733}
1734
Len Brown4be44fc2005-08-05 00:44:28 -04001735static int acpi_thermal_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
Len Brown4be44fc2005-08-05 00:44:28 -04001737 acpi_status status = AE_OK;
1738 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
1741 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001742 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001744 tz = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746 /* avoid timer adding new defer task */
1747 tz->zombie = 1;
1748 /* wait for running timer (on other CPUs) finish */
1749 del_timer_sync(&(tz->timer));
1750 /* synchronize deferred task */
1751 acpi_os_wait_events_complete(NULL);
1752 /* deferred task may reinsert timer */
1753 del_timer_sync(&(tz->timer));
1754
Patrick Mochel38ba7c92006-05-19 16:54:48 -04001755 status = acpi_remove_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001756 ACPI_DEVICE_NOTIFY,
1757 acpi_thermal_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
1759 /* Terminate policy */
Len Brown4be44fc2005-08-05 00:44:28 -04001760 if (tz->trips.passive.flags.valid && tz->trips.passive.flags.enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 tz->trips.passive.flags.enabled = 0;
1762 acpi_thermal_passive(tz);
1763 }
1764 if (tz->trips.active[0].flags.valid
Len Brown4be44fc2005-08-05 00:44:28 -04001765 && tz->trips.active[0].flags.enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 tz->trips.active[0].flags.enabled = 0;
1767 acpi_thermal_active(tz);
1768 }
1769
1770 acpi_thermal_remove_fs(device);
Zhang Rui3f655ef2008-01-17 15:51:11 +08001771 acpi_thermal_unregister_thermal_zone(tz);
Alexey Starikovskiy6e215782007-09-01 00:11:59 +04001772 mutex_destroy(&tz->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 kfree(tz);
Patrick Mocheld550d982006-06-27 00:41:40 -04001774 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775}
1776
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001777static int acpi_thermal_resume(struct acpi_device *device)
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001778{
1779 struct acpi_thermal *tz = NULL;
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001780 int i, j, power_state, result;
1781
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001782
1783 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001784 return -EINVAL;
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001785
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001786 tz = acpi_driver_data(device);
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001787
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001788 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001789 if (!(&tz->trips.active[i]))
1790 break;
1791 if (!tz->trips.active[i].flags.valid)
1792 break;
1793 tz->trips.active[i].flags.enabled = 1;
1794 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1795 result = acpi_bus_get_power(tz->trips.active[i].devices.
1796 handles[j], &power_state);
1797 if (result || (power_state != ACPI_STATE_D0)) {
1798 tz->trips.active[i].flags.enabled = 0;
1799 break;
1800 }
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001801 }
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001802 tz->state.active |= tz->trips.active[i].flags.enabled;
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001803 }
1804
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001805 acpi_thermal_check(tz);
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001806
1807 return AE_OK;
1808}
1809
Jeff Garzik18552562007-10-03 15:15:40 -04001810static int thermal_act(const struct dmi_system_id *d) {
Len Brown0b5bfa12007-08-12 00:13:02 -04001811
1812 if (act == 0) {
1813 printk(KERN_NOTICE "ACPI: %s detected: "
1814 "disabling all active thermal trip points\n", d->ident);
1815 act = -1;
1816 }
1817 return 0;
1818}
Jeff Garzik18552562007-10-03 15:15:40 -04001819static int thermal_nocrt(const struct dmi_system_id *d) {
Len Brown8c99fdc2007-08-20 18:46:50 -04001820
1821 printk(KERN_NOTICE "ACPI: %s detected: "
1822 "disabling all critical thermal trip point actions.\n", d->ident);
1823 nocrt = 1;
1824 return 0;
1825}
Jeff Garzik18552562007-10-03 15:15:40 -04001826static int thermal_tzp(const struct dmi_system_id *d) {
Len Brown0b5bfa12007-08-12 00:13:02 -04001827
1828 if (tzp == 0) {
1829 printk(KERN_NOTICE "ACPI: %s detected: "
1830 "enabling thermal zone polling\n", d->ident);
1831 tzp = 300; /* 300 dS = 30 Seconds */
1832 }
1833 return 0;
1834}
Jeff Garzik18552562007-10-03 15:15:40 -04001835static int thermal_psv(const struct dmi_system_id *d) {
Len Brown0b5bfa12007-08-12 00:13:02 -04001836
1837 if (psv == 0) {
1838 printk(KERN_NOTICE "ACPI: %s detected: "
1839 "disabling all passive thermal trip points\n", d->ident);
1840 psv = -1;
1841 }
1842 return 0;
1843}
1844
1845static struct dmi_system_id thermal_dmi_table[] __initdata = {
1846 /*
1847 * Award BIOS on this AOpen makes thermal control almost worthless.
1848 * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1849 */
1850 {
1851 .callback = thermal_act,
1852 .ident = "AOpen i915GMm-HFS",
1853 .matches = {
1854 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1855 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1856 },
1857 },
1858 {
1859 .callback = thermal_psv,
1860 .ident = "AOpen i915GMm-HFS",
1861 .matches = {
1862 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1863 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1864 },
1865 },
1866 {
1867 .callback = thermal_tzp,
1868 .ident = "AOpen i915GMm-HFS",
1869 .matches = {
1870 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1871 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1872 },
1873 },
Len Brown8c99fdc2007-08-20 18:46:50 -04001874 {
1875 .callback = thermal_nocrt,
1876 .ident = "Gigabyte GA-7ZX",
1877 .matches = {
1878 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
1879 DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
1880 },
1881 },
Len Brown0b5bfa12007-08-12 00:13:02 -04001882 {}
1883};
Len Brown0b5bfa12007-08-12 00:13:02 -04001884
Len Brown4be44fc2005-08-05 00:44:28 -04001885static int __init acpi_thermal_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
Len Brown4be44fc2005-08-05 00:44:28 -04001887 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Len Brown0b5bfa12007-08-12 00:13:02 -04001889 dmi_check_system(thermal_dmi_table);
1890
Len Brown72b33ef2007-08-12 00:12:17 -04001891 if (off) {
1892 printk(KERN_NOTICE "ACPI: thermal control disabled\n");
1893 return -ENODEV;
1894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1896 if (!acpi_thermal_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001897 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 acpi_thermal_dir->owner = THIS_MODULE;
1899
1900 result = acpi_bus_register_driver(&acpi_thermal_driver);
1901 if (result < 0) {
1902 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04001903 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 }
1905
Patrick Mocheld550d982006-06-27 00:41:40 -04001906 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907}
1908
Len Brown4be44fc2005-08-05 00:44:28 -04001909static void __exit acpi_thermal_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
1912 acpi_bus_unregister_driver(&acpi_thermal_driver);
1913
1914 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1915
Patrick Mocheld550d982006-06-27 00:41:40 -04001916 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917}
1918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919module_init(acpi_thermal_init);
1920module_exit(acpi_thermal_exit);