blob: 6b959976b7a42444c5c8c36ec859e9cd4343b81c [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/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/kmod.h>
42#include <linux/seq_file.h>
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -070043#include <linux/reboot.h>
Stephen Rothwellf6f5c452009-03-03 12:47:17 +110044#include <linux/device.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;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800193 struct thermal_zone_device *thermal_zone;
194 int tz_enabled;
Alexey Starikovskiy6e215782007-09-01 00:11:59 +0400195 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196};
197
Arjan van de Vend7508032006-07-04 13:06:00 -0400198static const struct file_operations acpi_thermal_state_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700199 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400200 .open = acpi_thermal_state_open_fs,
201 .read = seq_read,
202 .llseek = seq_lseek,
203 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
Arjan van de Vend7508032006-07-04 13:06:00 -0400206static const struct file_operations acpi_thermal_temp_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700207 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400208 .open = acpi_thermal_temp_open_fs,
209 .read = seq_read,
210 .llseek = seq_lseek,
211 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212};
213
Arjan van de Vend7508032006-07-04 13:06:00 -0400214static const struct file_operations acpi_thermal_trip_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700215 .owner = THIS_MODULE,
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 = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700223 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400224 .open = acpi_thermal_cooling_open_fs,
225 .read = seq_read,
226 .write = acpi_thermal_write_cooling_mode,
227 .llseek = seq_lseek,
228 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229};
230
Arjan van de Vend7508032006-07-04 13:06:00 -0400231static const struct file_operations acpi_thermal_polling_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700232 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400233 .open = acpi_thermal_polling_open_fs,
234 .read = seq_read,
235 .write = acpi_thermal_write_polling,
236 .llseek = seq_lseek,
237 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238};
239
240/* --------------------------------------------------------------------------
241 Thermal Zone Management
242 -------------------------------------------------------------------------- */
243
Len Brown4be44fc2005-08-05 00:44:28 -0400244static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Len Brown4be44fc2005-08-05 00:44:28 -0400246 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400247 unsigned long long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400250 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 tz->last_temperature = tz->temperature;
253
Matthew Wilcox27663c52008-10-10 02:22:59 -0400254 status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400256 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Matthew Wilcox27663c52008-10-10 02:22:59 -0400258 tz->temperature = tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400259 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
260 tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Patrick Mocheld550d982006-06-27 00:41:40 -0400262 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
Len Brown4be44fc2005-08-05 00:44:28 -0400265static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Len Brown4be44fc2005-08-05 00:44:28 -0400267 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400268 unsigned long long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400271 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Matthew Wilcox27663c52008-10-10 02:22:59 -0400273 status = acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, &tmp);
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
Matthew Wilcox27663c52008-10-10 02:22:59 -0400277 tz->polling_frequency = tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400278 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
279 tz->polling_frequency));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Patrick Mocheld550d982006-06-27 00:41:40 -0400281 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
Len Brown4be44fc2005-08-05 00:44:28 -0400284static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400288 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 tz->polling_frequency = seconds * 10; /* Convert value to deci-seconds */
291
Matthew Garrettb1569e92008-12-03 17:55:32 +0000292 tz->thermal_zone->polling_delay = seconds * 1000;
293
294 if (tz->tz_enabled)
295 thermal_zone_device_update(tz->thermal_zone);
296
Len Brown4be44fc2005-08-05 00:44:28 -0400297 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
298 "Polling frequency set to %lu seconds\n",
Sanjoy Mahajan636cedf2007-02-16 01:24:43 -0500299 tz->polling_frequency/10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Patrick Mocheld550d982006-06-27 00:41:40 -0400301 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
Len Brown4be44fc2005-08-05 00:44:28 -0400304static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Len Brown4be44fc2005-08-05 00:44:28 -0400306 acpi_status status = AE_OK;
307 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
308 struct acpi_object_list arg_list = { 1, &arg0 };
309 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400313 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400315 status = acpi_get_handle(tz->device->handle, "_SCP", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (ACPI_FAILURE(status)) {
317 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400318 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
320
321 arg0.integer.value = mode;
322
323 status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
324 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400325 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Patrick Mocheld550d982006-06-27 00:41:40 -0400327 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Zhang Ruice44e192008-01-17 15:51:25 +0800330#define ACPI_TRIPS_CRITICAL 0x01
331#define ACPI_TRIPS_HOT 0x02
332#define ACPI_TRIPS_PASSIVE 0x04
333#define ACPI_TRIPS_ACTIVE 0x08
334#define ACPI_TRIPS_DEVICES 0x10
335
336#define ACPI_TRIPS_REFRESH_THRESHOLDS (ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE)
337#define ACPI_TRIPS_REFRESH_DEVICES ACPI_TRIPS_DEVICES
338
339#define ACPI_TRIPS_INIT (ACPI_TRIPS_CRITICAL | ACPI_TRIPS_HOT | \
340 ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE | \
341 ACPI_TRIPS_DEVICES)
342
343/*
344 * This exception is thrown out in two cases:
345 * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
346 * when re-evaluating the AML code.
347 * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change.
348 * We need to re-bind the cooling devices of a thermal zone when this occurs.
349 */
350#define ACPI_THERMAL_TRIPS_EXCEPTION(flags, str) \
351do { \
352 if (flags != ACPI_TRIPS_INIT) \
353 ACPI_EXCEPTION((AE_INFO, AE_ERROR, \
354 "ACPI thermal trip point %s changed\n" \
355 "Please send acpidump to linux-acpi@vger.kernel.org\n", str)); \
356} while (0)
357
358static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Len Brown4be44fc2005-08-05 00:44:28 -0400360 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400361 unsigned long long tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800362 struct acpi_handle_list devices;
363 int valid = 0;
364 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 /* Critical Shutdown (required) */
Zhang Ruice44e192008-01-17 15:51:25 +0800367 if (flag & ACPI_TRIPS_CRITICAL) {
368 status = acpi_evaluate_integer(tz->device->handle,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400369 "_CRT", NULL, &tmp);
370 tz->trips.critical.temperature = tmp;
Arjan van de Vena39a2d72008-05-19 15:55:15 -0700371 /*
372 * Treat freezing temperatures as invalid as well; some
373 * BIOSes return really low values and cause reboots at startup.
374 * Below zero (Celcius) values clearly aren't right for sure..
375 * ... so lets discard those as invalid.
376 */
377 if (ACPI_FAILURE(status) ||
378 tz->trips.critical.temperature <= 2732) {
Len Brownc52a7412007-08-14 15:49:32 -0400379 tz->trips.critical.flags.valid = 0;
Zhang Ruice44e192008-01-17 15:51:25 +0800380 ACPI_EXCEPTION((AE_INFO, status,
Arjan van de Vena39a2d72008-05-19 15:55:15 -0700381 "No or invalid critical threshold"));
Zhang Ruice44e192008-01-17 15:51:25 +0800382 return -ENODEV;
383 } else {
384 tz->trips.critical.flags.valid = 1;
385 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
386 "Found critical threshold [%lu]\n",
387 tz->trips.critical.temperature));
388 }
389 if (tz->trips.critical.flags.valid == 1) {
390 if (crt == -1) {
391 tz->trips.critical.flags.valid = 0;
392 } else if (crt > 0) {
393 unsigned long crt_k = CELSIUS_TO_KELVIN(crt);
394 /*
Zhang Rui22a94d72008-10-17 02:41:20 -0400395 * Allow override critical threshold
Zhang Ruice44e192008-01-17 15:51:25 +0800396 */
Zhang Rui22a94d72008-10-17 02:41:20 -0400397 if (crt_k > tz->trips.critical.temperature)
398 printk(KERN_WARNING PREFIX
399 "Critical threshold %d C\n", crt);
400 tz->trips.critical.temperature = crt_k;
Zhang Ruice44e192008-01-17 15:51:25 +0800401 }
Len Brownc52a7412007-08-14 15:49:32 -0400402 }
403 }
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 /* Critical Sleep (optional) */
Zhang Ruice44e192008-01-17 15:51:25 +0800406 if (flag & ACPI_TRIPS_HOT) {
Len Browna70cdc52007-08-12 00:12:35 -0400407 status = acpi_evaluate_integer(tz->device->handle,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400408 "_HOT", NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800409 if (ACPI_FAILURE(status)) {
410 tz->trips.hot.flags.valid = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400411 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Zhang Ruice44e192008-01-17 15:51:25 +0800412 "No hot threshold\n"));
413 } else {
Matthew Wilcox27663c52008-10-10 02:22:59 -0400414 tz->trips.hot.temperature = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800415 tz->trips.hot.flags.valid = 1;
416 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
417 "Found hot threshold [%lu]\n",
418 tz->trips.critical.temperature));
419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421
Zhang Ruice44e192008-01-17 15:51:25 +0800422 /* Passive (optional) */
Zhang Rui0e4240d2009-01-16 12:53:42 -0500423 if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.flags.valid) ||
424 (flag == ACPI_TRIPS_INIT)) {
Zhang Ruice44e192008-01-17 15:51:25 +0800425 valid = tz->trips.passive.flags.valid;
426 if (psv == -1) {
427 status = AE_SUPPORT;
428 } else if (psv > 0) {
Matthew Wilcox27663c52008-10-10 02:22:59 -0400429 tmp = CELSIUS_TO_KELVIN(psv);
Zhang Ruice44e192008-01-17 15:51:25 +0800430 status = AE_OK;
431 } else {
432 status = acpi_evaluate_integer(tz->device->handle,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400433 "_PSV", NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Zhang Ruice44e192008-01-17 15:51:25 +0800436 if (ACPI_FAILURE(status))
437 tz->trips.passive.flags.valid = 0;
438 else {
Matthew Wilcox27663c52008-10-10 02:22:59 -0400439 tz->trips.passive.temperature = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800440 tz->trips.passive.flags.valid = 1;
441 if (flag == ACPI_TRIPS_INIT) {
442 status = acpi_evaluate_integer(
443 tz->device->handle, "_TC1",
Matthew Wilcox27663c52008-10-10 02:22:59 -0400444 NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800445 if (ACPI_FAILURE(status))
446 tz->trips.passive.flags.valid = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400447 else
448 tz->trips.passive.tc1 = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800449 status = acpi_evaluate_integer(
450 tz->device->handle, "_TC2",
Matthew Wilcox27663c52008-10-10 02:22:59 -0400451 NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800452 if (ACPI_FAILURE(status))
453 tz->trips.passive.flags.valid = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400454 else
455 tz->trips.passive.tc2 = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800456 status = acpi_evaluate_integer(
457 tz->device->handle, "_TSP",
Matthew Wilcox27663c52008-10-10 02:22:59 -0400458 NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800459 if (ACPI_FAILURE(status))
460 tz->trips.passive.flags.valid = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400461 else
462 tz->trips.passive.tsp = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800463 }
464 }
465 }
466 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.flags.valid) {
467 memset(&devices, 0, sizeof(struct acpi_handle_list));
468 status = acpi_evaluate_reference(tz->device->handle, "_PSL",
469 NULL, &devices);
Zhang Rui0e4240d2009-01-16 12:53:42 -0500470 if (ACPI_FAILURE(status)) {
471 printk(KERN_WARNING PREFIX
472 "Invalid passive threshold\n");
Zhang Ruice44e192008-01-17 15:51:25 +0800473 tz->trips.passive.flags.valid = 0;
Zhang Rui0e4240d2009-01-16 12:53:42 -0500474 }
Zhang Ruice44e192008-01-17 15:51:25 +0800475 else
476 tz->trips.passive.flags.valid = 1;
477
478 if (memcmp(&tz->trips.passive.devices, &devices,
479 sizeof(struct acpi_handle_list))) {
480 memcpy(&tz->trips.passive.devices, &devices,
481 sizeof(struct acpi_handle_list));
482 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
483 }
484 }
485 if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
486 if (valid != tz->trips.passive.flags.valid)
487 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
488 }
489
490 /* Active (optional) */
Len Brown4be44fc2005-08-05 00:44:28 -0400491 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Len Brown4be44fc2005-08-05 00:44:28 -0400492 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
Zhang Ruice44e192008-01-17 15:51:25 +0800493 valid = tz->trips.active[i].flags.valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Len Brownf8707ec2007-08-12 00:12:54 -0400495 if (act == -1)
Zhang Ruice44e192008-01-17 15:51:25 +0800496 break; /* disable all active trip points */
Len Brownf8707ec2007-08-12 00:12:54 -0400497
Zhang Rui0e4240d2009-01-16 12:53:42 -0500498 if ((flag == ACPI_TRIPS_INIT) || ((flag & ACPI_TRIPS_ACTIVE) &&
499 tz->trips.active[i].flags.valid)) {
Zhang Ruice44e192008-01-17 15:51:25 +0800500 status = acpi_evaluate_integer(tz->device->handle,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400501 name, NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800502 if (ACPI_FAILURE(status)) {
503 tz->trips.active[i].flags.valid = 0;
504 if (i == 0)
505 break;
506 if (act <= 0)
507 break;
508 if (i == 1)
509 tz->trips.active[0].temperature =
510 CELSIUS_TO_KELVIN(act);
511 else
512 /*
513 * Don't allow override higher than
514 * the next higher trip point
515 */
516 tz->trips.active[i - 1].temperature =
517 (tz->trips.active[i - 2].temperature <
518 CELSIUS_TO_KELVIN(act) ?
519 tz->trips.active[i - 2].temperature :
520 CELSIUS_TO_KELVIN(act));
Len Brownf8707ec2007-08-12 00:12:54 -0400521 break;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400522 } else {
523 tz->trips.active[i].temperature = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800524 tz->trips.active[i].flags.valid = 1;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400525 }
Len Brownf8707ec2007-08-12 00:12:54 -0400526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 name[2] = 'L';
Zhang Ruice44e192008-01-17 15:51:25 +0800529 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].flags.valid ) {
530 memset(&devices, 0, sizeof(struct acpi_handle_list));
531 status = acpi_evaluate_reference(tz->device->handle,
532 name, NULL, &devices);
Zhang Rui0e4240d2009-01-16 12:53:42 -0500533 if (ACPI_FAILURE(status)) {
534 printk(KERN_WARNING PREFIX
535 "Invalid active%d threshold\n", i);
Zhang Ruice44e192008-01-17 15:51:25 +0800536 tz->trips.active[i].flags.valid = 0;
Zhang Rui0e4240d2009-01-16 12:53:42 -0500537 }
Zhang Ruice44e192008-01-17 15:51:25 +0800538 else
539 tz->trips.active[i].flags.valid = 1;
540
541 if (memcmp(&tz->trips.active[i].devices, &devices,
542 sizeof(struct acpi_handle_list))) {
543 memcpy(&tz->trips.active[i].devices, &devices,
544 sizeof(struct acpi_handle_list));
545 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
546 }
547 }
548 if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
549 if (valid != tz->trips.active[i].flags.valid)
550 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
551
552 if (!tz->trips.active[i].flags.valid)
553 break;
554 }
555
556 if (flag & ACPI_TRIPS_DEVICES) {
557 memset(&devices, 0, sizeof(struct acpi_handle_list));
558 status = acpi_evaluate_reference(tz->device->handle, "_TZD",
559 NULL, &devices);
560 if (memcmp(&tz->devices, &devices,
561 sizeof(struct acpi_handle_list))) {
562 memcpy(&tz->devices, &devices,
563 sizeof(struct acpi_handle_list));
564 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567
Patrick Mocheld550d982006-06-27 00:41:40 -0400568 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
Zhang Ruice44e192008-01-17 15:51:25 +0800571static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Zhang Ruice44e192008-01-17 15:51:25 +0800573 return acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Len Brown4be44fc2005-08-05 00:44:28 -0400576static void acpi_thermal_check(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200578 struct acpi_thermal *tz = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Matthew Garrettb1569e92008-12-03 17:55:32 +0000580 thermal_zone_device_update(tz->thermal_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
Zhang Rui3f655ef2008-01-17 15:51:11 +0800583/* sys I/F for generic thermal sysfs support */
Zhang, Rui5e012762008-02-28 07:51:30 +0800584#define KELVIN_TO_MILLICELSIUS(t) (t * 100 - 273200)
585
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000586static int thermal_get_temp(struct thermal_zone_device *thermal,
587 unsigned long *temp)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800588{
589 struct acpi_thermal *tz = thermal->devdata;
Zhang, Rui76ecb4f2008-04-10 16:20:23 +0800590 int result;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800591
592 if (!tz)
593 return -EINVAL;
594
Zhang, Rui76ecb4f2008-04-10 16:20:23 +0800595 result = acpi_thermal_get_temperature(tz);
596 if (result)
597 return result;
598
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000599 *temp = KELVIN_TO_MILLICELSIUS(tz->temperature);
600 return 0;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800601}
602
603static const char enabled[] = "kernel";
604static const char disabled[] = "user";
605static int thermal_get_mode(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000606 enum thermal_device_mode *mode)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800607{
608 struct acpi_thermal *tz = thermal->devdata;
609
610 if (!tz)
611 return -EINVAL;
612
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000613 *mode = tz->tz_enabled ? THERMAL_DEVICE_ENABLED :
614 THERMAL_DEVICE_DISABLED;
615
616 return 0;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800617}
618
619static int thermal_set_mode(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000620 enum thermal_device_mode mode)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800621{
622 struct acpi_thermal *tz = thermal->devdata;
623 int enable;
624
625 if (!tz)
626 return -EINVAL;
627
628 /*
629 * enable/disable thermal management from ACPI thermal driver
630 */
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000631 if (mode == THERMAL_DEVICE_ENABLED)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800632 enable = 1;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000633 else if (mode == THERMAL_DEVICE_DISABLED)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800634 enable = 0;
635 else
636 return -EINVAL;
637
638 if (enable != tz->tz_enabled) {
639 tz->tz_enabled = enable;
640 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
641 "%s ACPI thermal control\n",
642 tz->tz_enabled ? enabled : disabled));
643 acpi_thermal_check(tz);
644 }
645 return 0;
646}
647
648static int thermal_get_trip_type(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000649 int trip, enum thermal_trip_type *type)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800650{
651 struct acpi_thermal *tz = thermal->devdata;
652 int i;
653
654 if (!tz || trip < 0)
655 return -EINVAL;
656
657 if (tz->trips.critical.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000658 if (!trip) {
659 *type = THERMAL_TRIP_CRITICAL;
660 return 0;
661 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800662 trip--;
663 }
664
665 if (tz->trips.hot.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000666 if (!trip) {
667 *type = THERMAL_TRIP_HOT;
668 return 0;
669 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800670 trip--;
671 }
672
673 if (tz->trips.passive.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000674 if (!trip) {
675 *type = THERMAL_TRIP_PASSIVE;
676 return 0;
677 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800678 trip--;
679 }
680
681 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
682 tz->trips.active[i].flags.valid; i++) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000683 if (!trip) {
684 *type = THERMAL_TRIP_ACTIVE;
685 return 0;
686 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800687 trip--;
688 }
689
690 return -EINVAL;
691}
692
693static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000694 int trip, unsigned long *temp)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800695{
696 struct acpi_thermal *tz = thermal->devdata;
697 int i;
698
699 if (!tz || trip < 0)
700 return -EINVAL;
701
702 if (tz->trips.critical.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000703 if (!trip) {
704 *temp = KELVIN_TO_MILLICELSIUS(
705 tz->trips.critical.temperature);
706 return 0;
707 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800708 trip--;
709 }
710
711 if (tz->trips.hot.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000712 if (!trip) {
713 *temp = KELVIN_TO_MILLICELSIUS(
714 tz->trips.hot.temperature);
715 return 0;
716 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800717 trip--;
718 }
719
720 if (tz->trips.passive.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000721 if (!trip) {
722 *temp = KELVIN_TO_MILLICELSIUS(
723 tz->trips.passive.temperature);
724 return 0;
725 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800726 trip--;
727 }
728
729 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
730 tz->trips.active[i].flags.valid; i++) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000731 if (!trip) {
732 *temp = KELVIN_TO_MILLICELSIUS(
733 tz->trips.active[i].temperature);
734 return 0;
735 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800736 trip--;
737 }
738
739 return -EINVAL;
740}
741
Zhang, Rui9ec732f2008-04-10 16:13:10 +0800742static int thermal_get_crit_temp(struct thermal_zone_device *thermal,
743 unsigned long *temperature) {
744 struct acpi_thermal *tz = thermal->devdata;
745
746 if (tz->trips.critical.flags.valid) {
747 *temperature = KELVIN_TO_MILLICELSIUS(
748 tz->trips.critical.temperature);
749 return 0;
750 } else
751 return -EINVAL;
752}
753
Matthew Garrettb1569e92008-12-03 17:55:32 +0000754static int thermal_notify(struct thermal_zone_device *thermal, int trip,
755 enum thermal_trip_type trip_type)
756{
757 u8 type = 0;
758 struct acpi_thermal *tz = thermal->devdata;
759
760 if (trip_type == THERMAL_TRIP_CRITICAL)
761 type = ACPI_THERMAL_NOTIFY_CRITICAL;
762 else if (trip_type == THERMAL_TRIP_HOT)
763 type = ACPI_THERMAL_NOTIFY_HOT;
764 else
765 return 0;
766
767 acpi_bus_generate_proc_event(tz->device, type, 1);
768 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
Stephen Rothwellf6f5c452009-03-03 12:47:17 +1100769 dev_name(&tz->device->dev), type, 1);
Matthew Garrettb1569e92008-12-03 17:55:32 +0000770
771 if (trip_type == THERMAL_TRIP_CRITICAL && nocrt)
772 return 1;
773
774 return 0;
775}
776
Zhang Rui3f655ef2008-01-17 15:51:11 +0800777typedef int (*cb)(struct thermal_zone_device *, int,
778 struct thermal_cooling_device *);
779static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
780 struct thermal_cooling_device *cdev,
781 cb action)
782{
783 struct acpi_device *device = cdev->devdata;
784 struct acpi_thermal *tz = thermal->devdata;
Zhang Rui653a00c2008-01-17 15:51:18 +0800785 struct acpi_device *dev;
786 acpi_status status;
787 acpi_handle handle;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800788 int i;
789 int j;
790 int trip = -1;
791 int result = 0;
792
793 if (tz->trips.critical.flags.valid)
794 trip++;
795
796 if (tz->trips.hot.flags.valid)
797 trip++;
798
799 if (tz->trips.passive.flags.valid) {
800 trip++;
801 for (i = 0; i < tz->trips.passive.devices.count;
802 i++) {
Zhang Rui653a00c2008-01-17 15:51:18 +0800803 handle = tz->trips.passive.devices.handles[i];
804 status = acpi_bus_get_device(handle, &dev);
805 if (ACPI_SUCCESS(status) && (dev == device)) {
806 result = action(thermal, trip, cdev);
807 if (result)
808 goto failed;
809 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800810 }
811 }
812
813 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
814 if (!tz->trips.active[i].flags.valid)
815 break;
816 trip++;
817 for (j = 0;
818 j < tz->trips.active[i].devices.count;
819 j++) {
Zhang Rui653a00c2008-01-17 15:51:18 +0800820 handle = tz->trips.active[i].devices.handles[j];
821 status = acpi_bus_get_device(handle, &dev);
822 if (ACPI_SUCCESS(status) && (dev == device)) {
823 result = action(thermal, trip, cdev);
824 if (result)
825 goto failed;
826 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800827 }
828 }
829
830 for (i = 0; i < tz->devices.count; i++) {
Zhang Rui653a00c2008-01-17 15:51:18 +0800831 handle = tz->devices.handles[i];
832 status = acpi_bus_get_device(handle, &dev);
833 if (ACPI_SUCCESS(status) && (dev == device)) {
834 result = action(thermal, -1, cdev);
835 if (result)
836 goto failed;
837 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800838 }
839
840failed:
841 return result;
842}
843
844static int
845acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
846 struct thermal_cooling_device *cdev)
847{
848 return acpi_thermal_cooling_device_cb(thermal, cdev,
849 thermal_zone_bind_cooling_device);
850}
851
852static int
853acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
854 struct thermal_cooling_device *cdev)
855{
856 return acpi_thermal_cooling_device_cb(thermal, cdev,
857 thermal_zone_unbind_cooling_device);
858}
859
860static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
861 .bind = acpi_thermal_bind_cooling_device,
862 .unbind = acpi_thermal_unbind_cooling_device,
863 .get_temp = thermal_get_temp,
864 .get_mode = thermal_get_mode,
865 .set_mode = thermal_set_mode,
866 .get_trip_type = thermal_get_trip_type,
867 .get_trip_temp = thermal_get_trip_temp,
Zhang, Rui9ec732f2008-04-10 16:13:10 +0800868 .get_crit_temp = thermal_get_crit_temp,
Matthew Garrettb1569e92008-12-03 17:55:32 +0000869 .notify = thermal_notify,
Zhang Rui3f655ef2008-01-17 15:51:11 +0800870};
871
872static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
873{
874 int trips = 0;
875 int result;
Zhang Rui20733932008-01-17 15:51:21 +0800876 acpi_status status;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800877 int i;
878
879 if (tz->trips.critical.flags.valid)
880 trips++;
881
882 if (tz->trips.hot.flags.valid)
883 trips++;
884
885 if (tz->trips.passive.flags.valid)
886 trips++;
887
888 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
889 tz->trips.active[i].flags.valid; i++, trips++);
Matthew Garrettb1569e92008-12-03 17:55:32 +0000890
891 if (tz->trips.passive.flags.valid)
892 tz->thermal_zone =
893 thermal_zone_device_register("acpitz", trips, tz,
894 &acpi_thermal_zone_ops,
895 tz->trips.passive.tc1,
896 tz->trips.passive.tc2,
897 tz->trips.passive.tsp*100,
898 tz->polling_frequency*100);
899 else
900 tz->thermal_zone =
901 thermal_zone_device_register("acpitz", trips, tz,
902 &acpi_thermal_zone_ops,
903 0, 0, 0,
904 tz->polling_frequency);
Krzysztof Heltbb070e42008-04-08 17:41:52 -0700905 if (IS_ERR(tz->thermal_zone))
Zhang Rui3f655ef2008-01-17 15:51:11 +0800906 return -ENODEV;
907
908 result = sysfs_create_link(&tz->device->dev.kobj,
909 &tz->thermal_zone->device.kobj, "thermal_zone");
910 if (result)
911 return result;
912
913 result = sysfs_create_link(&tz->thermal_zone->device.kobj,
914 &tz->device->dev.kobj, "device");
915 if (result)
916 return result;
917
Zhang Rui20733932008-01-17 15:51:21 +0800918 status = acpi_attach_data(tz->device->handle,
919 acpi_bus_private_data_handler,
920 tz->thermal_zone);
921 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800922 printk(KERN_ERR PREFIX
923 "Error attaching device data\n");
Zhang Rui20733932008-01-17 15:51:21 +0800924 return -ENODEV;
925 }
926
Zhang Rui3f655ef2008-01-17 15:51:11 +0800927 tz->tz_enabled = 1;
928
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200929 dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
930 tz->thermal_zone->id);
Zhang Rui3f655ef2008-01-17 15:51:11 +0800931 return 0;
932}
933
934static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
935{
936 sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
937 sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
938 thermal_zone_device_unregister(tz->thermal_zone);
939 tz->thermal_zone = NULL;
Zhang Rui20733932008-01-17 15:51:21 +0800940 acpi_detach_data(tz->device->handle, acpi_bus_private_data_handler);
Zhang Rui3f655ef2008-01-17 15:51:11 +0800941}
942
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944/* --------------------------------------------------------------------------
945 FS Interface (/proc)
946 -------------------------------------------------------------------------- */
947
Len Brown4be44fc2005-08-05 00:44:28 -0400948static struct proc_dir_entry *acpi_thermal_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
951{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200952 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 if (!tz)
956 goto end;
957
958 seq_puts(seq, "state: ");
959
Len Brown4be44fc2005-08-05 00:44:28 -0400960 if (!tz->state.critical && !tz->state.hot && !tz->state.passive
961 && !tz->state.active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 seq_puts(seq, "ok\n");
963 else {
964 if (tz->state.critical)
965 seq_puts(seq, "critical ");
966 if (tz->state.hot)
967 seq_puts(seq, "hot ");
968 if (tz->state.passive)
969 seq_puts(seq, "passive ");
970 if (tz->state.active)
971 seq_printf(seq, "active[%d]", tz->state.active_index);
972 seq_puts(seq, "\n");
973 }
974
Len Brown4be44fc2005-08-05 00:44:28 -0400975 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400976 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
979static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
980{
981 return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
982}
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
985{
Len Brown4be44fc2005-08-05 00:44:28 -0400986 int result = 0;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200987 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 if (!tz)
991 goto end;
992
993 result = acpi_thermal_get_temperature(tz);
994 if (result)
995 goto end;
996
Len Brown4be44fc2005-08-05 00:44:28 -0400997 seq_printf(seq, "temperature: %ld C\n",
998 KELVIN_TO_CELSIUS(tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Len Brown4be44fc2005-08-05 00:44:28 -04001000 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001001 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
1004static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
1005{
1006 return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
1007}
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
1010{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001011 struct acpi_thermal *tz = seq->private;
Thomas Renninger68ccfaa2006-11-19 23:01:40 +01001012 struct acpi_device *device;
Thomas Renningere7c746e2007-06-18 00:40:51 -04001013 acpi_status status;
1014
Len Brown4be44fc2005-08-05 00:44:28 -04001015 int i = 0;
1016 int j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 if (!tz)
1020 goto end;
1021
1022 if (tz->trips.critical.flags.valid)
Len Brownf5487142007-08-12 00:12:44 -04001023 seq_printf(seq, "critical (S5): %ld C%s",
1024 KELVIN_TO_CELSIUS(tz->trips.critical.temperature),
1025 nocrt ? " <disabled>\n" : "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027 if (tz->trips.hot.flags.valid)
Len Brownf5487142007-08-12 00:12:44 -04001028 seq_printf(seq, "hot (S4): %ld C%s",
1029 KELVIN_TO_CELSIUS(tz->trips.hot.temperature),
1030 nocrt ? " <disabled>\n" : "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 if (tz->trips.passive.flags.valid) {
Len Brown4be44fc2005-08-05 00:44:28 -04001033 seq_printf(seq,
1034 "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
1035 KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
1036 tz->trips.passive.tc1, tz->trips.passive.tc2,
1037 tz->trips.passive.tsp);
1038 for (j = 0; j < tz->trips.passive.devices.count; j++) {
Thomas Renningere7c746e2007-06-18 00:40:51 -04001039 status = acpi_bus_get_device(tz->trips.passive.devices.
1040 handles[j], &device);
1041 seq_printf(seq, "%4.4s ", status ? "" :
1042 acpi_device_bid(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
1044 seq_puts(seq, "\n");
1045 }
1046
1047 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1048 if (!(tz->trips.active[i].flags.valid))
1049 break;
1050 seq_printf(seq, "active[%d]: %ld C: devices=",
Len Brown4be44fc2005-08-05 00:44:28 -04001051 i,
1052 KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
Thomas Renninger68ccfaa2006-11-19 23:01:40 +01001053 for (j = 0; j < tz->trips.active[i].devices.count; j++){
Thomas Renningere7c746e2007-06-18 00:40:51 -04001054 status = acpi_bus_get_device(tz->trips.active[i].
1055 devices.handles[j],
1056 &device);
1057 seq_printf(seq, "%4.4s ", status ? "" :
1058 acpi_device_bid(device));
Thomas Renninger68ccfaa2006-11-19 23:01:40 +01001059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 seq_puts(seq, "\n");
1061 }
1062
Len Brown4be44fc2005-08-05 00:44:28 -04001063 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001064 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
1067static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
1068{
1069 return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
1070}
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
1073{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001074 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 if (!tz)
1078 goto end;
1079
Len Browneaca2d32007-04-30 23:27:43 -04001080 if (!tz->flags.cooling_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 seq_puts(seq, "<setting not supported>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 else
Len Browneaca2d32007-04-30 23:27:43 -04001083 seq_puts(seq, "0 - Active; 1 - Passive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Len Brown4be44fc2005-08-05 00:44:28 -04001085 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001086 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
1089static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
1090{
1091 return single_open(file, acpi_thermal_cooling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001092 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
1095static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001096acpi_thermal_write_cooling_mode(struct file *file,
1097 const char __user * buffer,
1098 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001100 struct seq_file *m = file->private_data;
1101 struct acpi_thermal *tz = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001102 int result = 0;
1103 char mode_string[12] = { '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106 if (!tz || (count > sizeof(mode_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001107 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 if (!tz->flags.cooling_mode)
Patrick Mocheld550d982006-06-27 00:41:40 -04001110 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 if (copy_from_user(mode_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001113 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -04001114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 mode_string[count] = '\0';
Len Brown4be44fc2005-08-05 00:44:28 -04001116
1117 result = acpi_thermal_set_cooling_mode(tz,
1118 simple_strtoul(mode_string, NULL,
1119 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001121 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 acpi_thermal_check(tz);
1124
Patrick Mocheld550d982006-06-27 00:41:40 -04001125 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
1127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
1129{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001130 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133 if (!tz)
1134 goto end;
1135
Matthew Garrettb1569e92008-12-03 17:55:32 +00001136 if (!tz->thermal_zone->polling_delay) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 seq_puts(seq, "<polling disabled>\n");
1138 goto end;
1139 }
1140
Matthew Garrettb1569e92008-12-03 17:55:32 +00001141 seq_printf(seq, "polling frequency: %d seconds\n",
1142 (tz->thermal_zone->polling_delay / 1000));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Len Brown4be44fc2005-08-05 00:44:28 -04001144 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001145 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
1147
1148static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
1149{
1150 return single_open(file, acpi_thermal_polling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001151 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152}
1153
1154static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001155acpi_thermal_write_polling(struct file *file,
1156 const char __user * buffer,
1157 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001159 struct seq_file *m = file->private_data;
1160 struct acpi_thermal *tz = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001161 int result = 0;
1162 char polling_string[12] = { '\0' };
1163 int seconds = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 if (!tz || (count > sizeof(polling_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001167 return -EINVAL;
Len Brown4be44fc2005-08-05 00:44:28 -04001168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (copy_from_user(polling_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001170 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -04001171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 polling_string[count] = '\0';
1173
1174 seconds = simple_strtoul(polling_string, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 result = acpi_thermal_set_polling(tz, seconds);
1177 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001178 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180 acpi_thermal_check(tz);
1181
Patrick Mocheld550d982006-06-27 00:41:40 -04001182 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
1184
Len Brown4be44fc2005-08-05 00:44:28 -04001185static int acpi_thermal_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
Len Brown4be44fc2005-08-05 00:44:28 -04001187 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 if (!acpi_device_dir(device)) {
1191 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -04001192 acpi_thermal_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001194 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 acpi_device_dir(device)->owner = THIS_MODULE;
1196 }
1197
1198 /* 'state' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001199 entry = proc_create_data(ACPI_THERMAL_FILE_STATE,
1200 S_IRUGO, acpi_device_dir(device),
1201 &acpi_thermal_state_fops,
1202 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001204 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 /* 'temperature' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001207 entry = proc_create_data(ACPI_THERMAL_FILE_TEMPERATURE,
1208 S_IRUGO, acpi_device_dir(device),
1209 &acpi_thermal_temp_fops,
1210 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001212 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Pavel Machek2db9ccb2007-08-24 11:45:50 +02001214 /* 'trip_points' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001215 entry = proc_create_data(ACPI_THERMAL_FILE_TRIP_POINTS,
1216 S_IRUGO,
1217 acpi_device_dir(device),
1218 &acpi_thermal_trip_fops,
1219 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001221 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 /* 'cooling_mode' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001224 entry = proc_create_data(ACPI_THERMAL_FILE_COOLING_MODE,
1225 S_IFREG | S_IRUGO | S_IWUSR,
1226 acpi_device_dir(device),
1227 &acpi_thermal_cooling_fops,
1228 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001230 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 /* 'polling_frequency' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001233 entry = proc_create_data(ACPI_THERMAL_FILE_POLLING_FREQ,
1234 S_IFREG | S_IRUGO | S_IWUSR,
1235 acpi_device_dir(device),
1236 &acpi_thermal_polling_fops,
1237 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001239 return -ENODEV;
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_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 if (acpi_device_dir(device)) {
1247 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1248 acpi_device_dir(device));
1249 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1250 acpi_device_dir(device));
1251 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1252 acpi_device_dir(device));
1253 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1254 acpi_device_dir(device));
1255 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1256 acpi_device_dir(device));
1257 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1258 acpi_device_dir(device) = NULL;
1259 }
1260
Patrick Mocheld550d982006-06-27 00:41:40 -04001261 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262}
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264/* --------------------------------------------------------------------------
1265 Driver Interface
1266 -------------------------------------------------------------------------- */
1267
Len Brown4be44fc2005-08-05 00:44:28 -04001268static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001270 struct acpi_thermal *tz = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001271 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001275 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Patrick Mochel8348e1b2006-05-19 16:54:40 -04001277 device = tz->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 switch (event) {
1280 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1281 acpi_thermal_check(tz);
1282 break;
1283 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
Zhang Ruice44e192008-01-17 15:51:25 +08001284 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 acpi_thermal_check(tz);
Len Brown14e04fb32007-08-23 15:20:26 -04001286 acpi_bus_generate_proc_event(device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08001287 acpi_bus_generate_netlink_event(device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +01001288 dev_name(&device->dev), event, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 break;
1290 case ACPI_THERMAL_NOTIFY_DEVICES:
Zhang Ruice44e192008-01-17 15:51:25 +08001291 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
1292 acpi_thermal_check(tz);
Len Brown14e04fb32007-08-23 15:20:26 -04001293 acpi_bus_generate_proc_event(device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08001294 acpi_bus_generate_netlink_event(device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +01001295 dev_name(&device->dev), event, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 break;
1297 default:
1298 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001299 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 break;
1301 }
1302
Patrick Mocheld550d982006-06-27 00:41:40 -04001303 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304}
1305
Len Brown4be44fc2005-08-05 00:44:28 -04001306static int acpi_thermal_get_info(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
Len Brown4be44fc2005-08-05 00:44:28 -04001308 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001312 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 /* Get temperature [_TMP] (required) */
1315 result = acpi_thermal_get_temperature(tz);
1316 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001317 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 /* Get trip points [_CRT, _PSV, etc.] (required) */
1320 result = acpi_thermal_get_trip_points(tz);
1321 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001322 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 /* Set the cooling mode [_SCP] to active cooling (default) */
1325 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
Len Brown4be44fc2005-08-05 00:44:28 -04001326 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 tz->flags.cooling_mode = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 /* Get default polling frequency [_TZP] (optional) */
1330 if (tzp)
1331 tz->polling_frequency = tzp;
1332 else
1333 acpi_thermal_get_polling_frequency(tz);
1334
Patrick Mocheld550d982006-06-27 00:41:40 -04001335 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
1337
Len Brown4be44fc2005-08-05 00:44:28 -04001338static int acpi_thermal_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
Len Brown4be44fc2005-08-05 00:44:28 -04001340 int result = 0;
1341 acpi_status status = AE_OK;
1342 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001346 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Burman Yan36bcbec2006-12-19 12:56:11 -08001348 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001350 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Patrick Mochel8348e1b2006-05-19 16:54:40 -04001352 tz->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 strcpy(tz->name, device->pnp.bus_id);
1354 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1355 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001356 device->driver_data = tz;
Alexey Starikovskiy6e215782007-09-01 00:11:59 +04001357 mutex_init(&tz->lock);
Zhang Rui3f655ef2008-01-17 15:51:11 +08001358
1359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 result = acpi_thermal_get_info(tz);
1361 if (result)
Zhang Rui3f655ef2008-01-17 15:51:11 +08001362 goto free_memory;
1363
1364 result = acpi_thermal_register_thermal_zone(tz);
1365 if (result)
1366 goto free_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 result = acpi_thermal_add_fs(device);
1369 if (result)
Zhang Rui3f655ef2008-01-17 15:51:11 +08001370 goto unregister_thermal_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Patrick Mochel38ba7c92006-05-19 16:54:48 -04001372 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001373 ACPI_DEVICE_NOTIFY,
1374 acpi_thermal_notify, tz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 result = -ENODEV;
Zhang Rui3f655ef2008-01-17 15:51:11 +08001377 goto remove_fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 }
1379
1380 printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001381 acpi_device_name(device), acpi_device_bid(device),
1382 KELVIN_TO_CELSIUS(tz->temperature));
Zhang Rui3f655ef2008-01-17 15:51:11 +08001383 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Zhang Rui3f655ef2008-01-17 15:51:11 +08001385remove_fs:
1386 acpi_thermal_remove_fs(device);
1387unregister_thermal_zone:
1388 thermal_zone_device_unregister(tz->thermal_zone);
1389free_memory:
1390 kfree(tz);
1391end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001392 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393}
1394
Len Brown4be44fc2005-08-05 00:44:28 -04001395static int acpi_thermal_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
Len Brown4be44fc2005-08-05 00:44:28 -04001397 acpi_status status = AE_OK;
1398 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001401 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001403 tz = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Patrick Mochel38ba7c92006-05-19 16:54:48 -04001405 status = acpi_remove_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001406 ACPI_DEVICE_NOTIFY,
1407 acpi_thermal_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 acpi_thermal_remove_fs(device);
Zhang Rui3f655ef2008-01-17 15:51:11 +08001410 acpi_thermal_unregister_thermal_zone(tz);
Alexey Starikovskiy6e215782007-09-01 00:11:59 +04001411 mutex_destroy(&tz->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 kfree(tz);
Patrick Mocheld550d982006-06-27 00:41:40 -04001413 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
1415
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001416static int acpi_thermal_resume(struct acpi_device *device)
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001417{
1418 struct acpi_thermal *tz = NULL;
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001419 int i, j, power_state, result;
1420
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001421
1422 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001423 return -EINVAL;
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001424
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001425 tz = acpi_driver_data(device);
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001426
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001427 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001428 if (!(&tz->trips.active[i]))
1429 break;
1430 if (!tz->trips.active[i].flags.valid)
1431 break;
1432 tz->trips.active[i].flags.enabled = 1;
1433 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1434 result = acpi_bus_get_power(tz->trips.active[i].devices.
1435 handles[j], &power_state);
1436 if (result || (power_state != ACPI_STATE_D0)) {
1437 tz->trips.active[i].flags.enabled = 0;
1438 break;
1439 }
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001440 }
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001441 tz->state.active |= tz->trips.active[i].flags.enabled;
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001442 }
1443
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001444 acpi_thermal_check(tz);
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001445
1446 return AE_OK;
1447}
1448
Jeff Garzik18552562007-10-03 15:15:40 -04001449static int thermal_act(const struct dmi_system_id *d) {
Len Brown0b5bfa12007-08-12 00:13:02 -04001450
1451 if (act == 0) {
1452 printk(KERN_NOTICE "ACPI: %s detected: "
1453 "disabling all active thermal trip points\n", d->ident);
1454 act = -1;
1455 }
1456 return 0;
1457}
Jeff Garzik18552562007-10-03 15:15:40 -04001458static int thermal_nocrt(const struct dmi_system_id *d) {
Len Brown8c99fdc2007-08-20 18:46:50 -04001459
1460 printk(KERN_NOTICE "ACPI: %s detected: "
1461 "disabling all critical thermal trip point actions.\n", d->ident);
1462 nocrt = 1;
1463 return 0;
1464}
Jeff Garzik18552562007-10-03 15:15:40 -04001465static int thermal_tzp(const struct dmi_system_id *d) {
Len Brown0b5bfa12007-08-12 00:13:02 -04001466
1467 if (tzp == 0) {
1468 printk(KERN_NOTICE "ACPI: %s detected: "
1469 "enabling thermal zone polling\n", d->ident);
1470 tzp = 300; /* 300 dS = 30 Seconds */
1471 }
1472 return 0;
1473}
Jeff Garzik18552562007-10-03 15:15:40 -04001474static int thermal_psv(const struct dmi_system_id *d) {
Len Brown0b5bfa12007-08-12 00:13:02 -04001475
1476 if (psv == 0) {
1477 printk(KERN_NOTICE "ACPI: %s detected: "
1478 "disabling all passive thermal trip points\n", d->ident);
1479 psv = -1;
1480 }
1481 return 0;
1482}
1483
1484static struct dmi_system_id thermal_dmi_table[] __initdata = {
1485 /*
1486 * Award BIOS on this AOpen makes thermal control almost worthless.
1487 * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1488 */
1489 {
1490 .callback = thermal_act,
1491 .ident = "AOpen i915GMm-HFS",
1492 .matches = {
1493 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1494 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1495 },
1496 },
1497 {
1498 .callback = thermal_psv,
1499 .ident = "AOpen i915GMm-HFS",
1500 .matches = {
1501 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1502 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1503 },
1504 },
1505 {
1506 .callback = thermal_tzp,
1507 .ident = "AOpen i915GMm-HFS",
1508 .matches = {
1509 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1510 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1511 },
1512 },
Len Brown8c99fdc2007-08-20 18:46:50 -04001513 {
1514 .callback = thermal_nocrt,
1515 .ident = "Gigabyte GA-7ZX",
1516 .matches = {
1517 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
1518 DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
1519 },
1520 },
Len Brown0b5bfa12007-08-12 00:13:02 -04001521 {}
1522};
Len Brown0b5bfa12007-08-12 00:13:02 -04001523
Len Brown4be44fc2005-08-05 00:44:28 -04001524static int __init acpi_thermal_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
Len Brown4be44fc2005-08-05 00:44:28 -04001526 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Len Brown0b5bfa12007-08-12 00:13:02 -04001528 dmi_check_system(thermal_dmi_table);
1529
Len Brown72b33ef2007-08-12 00:12:17 -04001530 if (off) {
1531 printk(KERN_NOTICE "ACPI: thermal control disabled\n");
1532 return -ENODEV;
1533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1535 if (!acpi_thermal_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001536 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 acpi_thermal_dir->owner = THIS_MODULE;
1538
1539 result = acpi_bus_register_driver(&acpi_thermal_driver);
1540 if (result < 0) {
1541 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04001542 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 }
1544
Patrick Mocheld550d982006-06-27 00:41:40 -04001545 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
1547
Len Brown4be44fc2005-08-05 00:44:28 -04001548static void __exit acpi_thermal_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 acpi_bus_unregister_driver(&acpi_thermal_driver);
1552
1553 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1554
Patrick Mocheld550d982006-06-27 00:41:40 -04001555 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556}
1557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558module_init(acpi_thermal_init);
1559module_exit(acpi_thermal_exit);