blob: 0914eaa9a097762a8bca397e29926dc3da71d1de [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);
Bjorn Helgaas342d5502009-04-07 15:37:06 +0000101static void acpi_thermal_notify(struct acpi_device *device, u32 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
103static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
104static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -0400106static ssize_t acpi_thermal_write_cooling_mode(struct file *,
107 const char __user *, size_t,
108 loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -0400110static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
111 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200113static const struct acpi_device_id thermal_device_ids[] = {
114 {ACPI_THERMAL_HID, 0},
115 {"", 0},
116};
117MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119static struct acpi_driver acpi_thermal_driver = {
Len Brownc2b6705b2007-02-12 23:33:40 -0500120 .name = "thermal",
Len Brown4be44fc2005-08-05 00:44:28 -0400121 .class = ACPI_THERMAL_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200122 .ids = thermal_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -0400123 .ops = {
124 .add = acpi_thermal_add,
125 .remove = acpi_thermal_remove,
Konstantin Karasyov74ce1462006-05-08 08:32:00 -0400126 .resume = acpi_thermal_resume,
Bjorn Helgaas342d5502009-04-07 15:37:06 +0000127 .notify = acpi_thermal_notify,
Len Brown4be44fc2005-08-05 00:44:28 -0400128 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129};
130
131struct acpi_thermal_state {
Len Brown4be44fc2005-08-05 00:44:28 -0400132 u8 critical:1;
133 u8 hot:1;
134 u8 passive:1;
135 u8 active:1;
136 u8 reserved:4;
137 int active_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
140struct acpi_thermal_state_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400141 u8 valid:1;
142 u8 enabled:1;
143 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144};
145
146struct acpi_thermal_critical {
147 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400148 unsigned long temperature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149};
150
151struct acpi_thermal_hot {
152 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400153 unsigned long temperature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154};
155
156struct acpi_thermal_passive {
157 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400158 unsigned long temperature;
159 unsigned long tc1;
160 unsigned long tc2;
161 unsigned long tsp;
162 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163};
164
165struct acpi_thermal_active {
166 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400167 unsigned long temperature;
168 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169};
170
171struct acpi_thermal_trips {
172 struct acpi_thermal_critical critical;
Len Brown4be44fc2005-08-05 00:44:28 -0400173 struct acpi_thermal_hot hot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 struct acpi_thermal_passive passive;
175 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
176};
177
178struct acpi_thermal_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400179 u8 cooling_mode:1; /* _SCP */
180 u8 devices:1; /* _TZD */
181 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182};
183
184struct acpi_thermal {
Patrick Mochel8348e1b2006-05-19 16:54:40 -0400185 struct acpi_device * device;
Len Brown4be44fc2005-08-05 00:44:28 -0400186 acpi_bus_id name;
187 unsigned long temperature;
188 unsigned long last_temperature;
189 unsigned long polling_frequency;
Len Brown4be44fc2005-08-05 00:44:28 -0400190 volatile u8 zombie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 struct acpi_thermal_flags flags;
192 struct acpi_thermal_state state;
193 struct acpi_thermal_trips trips;
Len Brown4be44fc2005-08-05 00:44:28 -0400194 struct acpi_handle_list devices;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800195 struct thermal_zone_device *thermal_zone;
196 int tz_enabled;
Alexey Starikovskiy6e215782007-09-01 00:11:59 +0400197 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198};
199
Arjan van de Vend7508032006-07-04 13:06:00 -0400200static const struct file_operations acpi_thermal_state_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700201 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400202 .open = acpi_thermal_state_open_fs,
203 .read = seq_read,
204 .llseek = seq_lseek,
205 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206};
207
Arjan van de Vend7508032006-07-04 13:06:00 -0400208static const struct file_operations acpi_thermal_temp_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700209 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400210 .open = acpi_thermal_temp_open_fs,
211 .read = seq_read,
212 .llseek = seq_lseek,
213 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214};
215
Arjan van de Vend7508032006-07-04 13:06:00 -0400216static const struct file_operations acpi_thermal_trip_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700217 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400218 .open = acpi_thermal_trip_open_fs,
219 .read = seq_read,
Len Brown4be44fc2005-08-05 00:44:28 -0400220 .llseek = seq_lseek,
221 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222};
223
Arjan van de Vend7508032006-07-04 13:06:00 -0400224static const struct file_operations acpi_thermal_cooling_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700225 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400226 .open = acpi_thermal_cooling_open_fs,
227 .read = seq_read,
228 .write = acpi_thermal_write_cooling_mode,
229 .llseek = seq_lseek,
230 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231};
232
Arjan van de Vend7508032006-07-04 13:06:00 -0400233static const struct file_operations acpi_thermal_polling_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700234 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400235 .open = acpi_thermal_polling_open_fs,
236 .read = seq_read,
237 .write = acpi_thermal_write_polling,
238 .llseek = seq_lseek,
239 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240};
241
242/* --------------------------------------------------------------------------
243 Thermal Zone Management
244 -------------------------------------------------------------------------- */
245
Len Brown4be44fc2005-08-05 00:44:28 -0400246static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Len Brown4be44fc2005-08-05 00:44:28 -0400248 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400249 unsigned long long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400252 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 tz->last_temperature = tz->temperature;
255
Matthew Wilcox27663c52008-10-10 02:22:59 -0400256 status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400258 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Matthew Wilcox27663c52008-10-10 02:22:59 -0400260 tz->temperature = tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400261 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
262 tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Patrick Mocheld550d982006-06-27 00:41:40 -0400264 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
Len Brown4be44fc2005-08-05 00:44:28 -0400267static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Len Brown4be44fc2005-08-05 00:44:28 -0400269 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400270 unsigned long long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400273 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Matthew Wilcox27663c52008-10-10 02:22:59 -0400275 status = acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400277 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Matthew Wilcox27663c52008-10-10 02:22:59 -0400279 tz->polling_frequency = tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400280 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
281 tz->polling_frequency));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Patrick Mocheld550d982006-06-27 00:41:40 -0400283 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
Len Brown4be44fc2005-08-05 00:44:28 -0400286static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400290 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 tz->polling_frequency = seconds * 10; /* Convert value to deci-seconds */
293
Matthew Garrettb1569e92008-12-03 17:55:32 +0000294 tz->thermal_zone->polling_delay = seconds * 1000;
295
296 if (tz->tz_enabled)
297 thermal_zone_device_update(tz->thermal_zone);
298
Len Brown4be44fc2005-08-05 00:44:28 -0400299 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
300 "Polling frequency set to %lu seconds\n",
Sanjoy Mahajan636cedf2007-02-16 01:24:43 -0500301 tz->polling_frequency/10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Patrick Mocheld550d982006-06-27 00:41:40 -0400303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
Len Brown4be44fc2005-08-05 00:44:28 -0400306static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Len Brown4be44fc2005-08-05 00:44:28 -0400308 acpi_status status = AE_OK;
309 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
310 struct acpi_object_list arg_list = { 1, &arg0 };
311 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400315 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400317 status = acpi_get_handle(tz->device->handle, "_SCP", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (ACPI_FAILURE(status)) {
319 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400320 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322
323 arg0.integer.value = mode;
324
325 status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
326 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400327 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Patrick Mocheld550d982006-06-27 00:41:40 -0400329 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Zhang Ruice44e192008-01-17 15:51:25 +0800332#define ACPI_TRIPS_CRITICAL 0x01
333#define ACPI_TRIPS_HOT 0x02
334#define ACPI_TRIPS_PASSIVE 0x04
335#define ACPI_TRIPS_ACTIVE 0x08
336#define ACPI_TRIPS_DEVICES 0x10
337
338#define ACPI_TRIPS_REFRESH_THRESHOLDS (ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE)
339#define ACPI_TRIPS_REFRESH_DEVICES ACPI_TRIPS_DEVICES
340
341#define ACPI_TRIPS_INIT (ACPI_TRIPS_CRITICAL | ACPI_TRIPS_HOT | \
342 ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE | \
343 ACPI_TRIPS_DEVICES)
344
345/*
346 * This exception is thrown out in two cases:
347 * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
348 * when re-evaluating the AML code.
349 * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change.
350 * We need to re-bind the cooling devices of a thermal zone when this occurs.
351 */
352#define ACPI_THERMAL_TRIPS_EXCEPTION(flags, str) \
353do { \
354 if (flags != ACPI_TRIPS_INIT) \
355 ACPI_EXCEPTION((AE_INFO, AE_ERROR, \
356 "ACPI thermal trip point %s changed\n" \
357 "Please send acpidump to linux-acpi@vger.kernel.org\n", str)); \
358} while (0)
359
360static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Len Brown4be44fc2005-08-05 00:44:28 -0400362 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400363 unsigned long long tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800364 struct acpi_handle_list devices;
365 int valid = 0;
366 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 /* Critical Shutdown (required) */
Zhang Ruice44e192008-01-17 15:51:25 +0800369 if (flag & ACPI_TRIPS_CRITICAL) {
370 status = acpi_evaluate_integer(tz->device->handle,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400371 "_CRT", NULL, &tmp);
372 tz->trips.critical.temperature = tmp;
Arjan van de Vena39a2d72008-05-19 15:55:15 -0700373 /*
374 * Treat freezing temperatures as invalid as well; some
375 * BIOSes return really low values and cause reboots at startup.
Adam Buchbinderb731d7b2009-03-13 12:15:26 -0400376 * Below zero (Celsius) values clearly aren't right for sure..
Arjan van de Vena39a2d72008-05-19 15:55:15 -0700377 * ... so lets discard those as invalid.
378 */
379 if (ACPI_FAILURE(status) ||
380 tz->trips.critical.temperature <= 2732) {
Len Brownc52a7412007-08-14 15:49:32 -0400381 tz->trips.critical.flags.valid = 0;
Zhang Ruice44e192008-01-17 15:51:25 +0800382 ACPI_EXCEPTION((AE_INFO, status,
Arjan van de Vena39a2d72008-05-19 15:55:15 -0700383 "No or invalid critical threshold"));
Zhang Ruice44e192008-01-17 15:51:25 +0800384 return -ENODEV;
385 } else {
386 tz->trips.critical.flags.valid = 1;
387 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
388 "Found critical threshold [%lu]\n",
389 tz->trips.critical.temperature));
390 }
391 if (tz->trips.critical.flags.valid == 1) {
392 if (crt == -1) {
393 tz->trips.critical.flags.valid = 0;
394 } else if (crt > 0) {
395 unsigned long crt_k = CELSIUS_TO_KELVIN(crt);
396 /*
Zhang Rui22a94d72008-10-17 02:41:20 -0400397 * Allow override critical threshold
Zhang Ruice44e192008-01-17 15:51:25 +0800398 */
Zhang Rui22a94d72008-10-17 02:41:20 -0400399 if (crt_k > tz->trips.critical.temperature)
400 printk(KERN_WARNING PREFIX
401 "Critical threshold %d C\n", crt);
402 tz->trips.critical.temperature = crt_k;
Zhang Ruice44e192008-01-17 15:51:25 +0800403 }
Len Brownc52a7412007-08-14 15:49:32 -0400404 }
405 }
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 /* Critical Sleep (optional) */
Zhang Ruice44e192008-01-17 15:51:25 +0800408 if (flag & ACPI_TRIPS_HOT) {
Len Browna70cdc52007-08-12 00:12:35 -0400409 status = acpi_evaluate_integer(tz->device->handle,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400410 "_HOT", NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800411 if (ACPI_FAILURE(status)) {
412 tz->trips.hot.flags.valid = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400413 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Zhang Ruice44e192008-01-17 15:51:25 +0800414 "No hot threshold\n"));
415 } else {
Matthew Wilcox27663c52008-10-10 02:22:59 -0400416 tz->trips.hot.temperature = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800417 tz->trips.hot.flags.valid = 1;
418 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
419 "Found hot threshold [%lu]\n",
420 tz->trips.critical.temperature));
421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423
Zhang Ruice44e192008-01-17 15:51:25 +0800424 /* Passive (optional) */
Zhang Rui0e4240d2009-01-16 12:53:42 -0500425 if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.flags.valid) ||
426 (flag == ACPI_TRIPS_INIT)) {
Zhang Ruice44e192008-01-17 15:51:25 +0800427 valid = tz->trips.passive.flags.valid;
428 if (psv == -1) {
429 status = AE_SUPPORT;
430 } else if (psv > 0) {
Matthew Wilcox27663c52008-10-10 02:22:59 -0400431 tmp = CELSIUS_TO_KELVIN(psv);
Zhang Ruice44e192008-01-17 15:51:25 +0800432 status = AE_OK;
433 } else {
434 status = acpi_evaluate_integer(tz->device->handle,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400435 "_PSV", NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Zhang Ruice44e192008-01-17 15:51:25 +0800438 if (ACPI_FAILURE(status))
439 tz->trips.passive.flags.valid = 0;
440 else {
Matthew Wilcox27663c52008-10-10 02:22:59 -0400441 tz->trips.passive.temperature = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800442 tz->trips.passive.flags.valid = 1;
443 if (flag == ACPI_TRIPS_INIT) {
444 status = acpi_evaluate_integer(
445 tz->device->handle, "_TC1",
Matthew Wilcox27663c52008-10-10 02:22:59 -0400446 NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800447 if (ACPI_FAILURE(status))
448 tz->trips.passive.flags.valid = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400449 else
450 tz->trips.passive.tc1 = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800451 status = acpi_evaluate_integer(
452 tz->device->handle, "_TC2",
Matthew Wilcox27663c52008-10-10 02:22:59 -0400453 NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800454 if (ACPI_FAILURE(status))
455 tz->trips.passive.flags.valid = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400456 else
457 tz->trips.passive.tc2 = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800458 status = acpi_evaluate_integer(
459 tz->device->handle, "_TSP",
Matthew Wilcox27663c52008-10-10 02:22:59 -0400460 NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800461 if (ACPI_FAILURE(status))
462 tz->trips.passive.flags.valid = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400463 else
464 tz->trips.passive.tsp = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800465 }
466 }
467 }
468 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.flags.valid) {
469 memset(&devices, 0, sizeof(struct acpi_handle_list));
470 status = acpi_evaluate_reference(tz->device->handle, "_PSL",
471 NULL, &devices);
Zhang Rui0e4240d2009-01-16 12:53:42 -0500472 if (ACPI_FAILURE(status)) {
473 printk(KERN_WARNING PREFIX
474 "Invalid passive threshold\n");
Zhang Ruice44e192008-01-17 15:51:25 +0800475 tz->trips.passive.flags.valid = 0;
Zhang Rui0e4240d2009-01-16 12:53:42 -0500476 }
Zhang Ruice44e192008-01-17 15:51:25 +0800477 else
478 tz->trips.passive.flags.valid = 1;
479
480 if (memcmp(&tz->trips.passive.devices, &devices,
481 sizeof(struct acpi_handle_list))) {
482 memcpy(&tz->trips.passive.devices, &devices,
483 sizeof(struct acpi_handle_list));
484 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
485 }
486 }
487 if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
488 if (valid != tz->trips.passive.flags.valid)
489 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
490 }
491
492 /* Active (optional) */
Len Brown4be44fc2005-08-05 00:44:28 -0400493 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Len Brown4be44fc2005-08-05 00:44:28 -0400494 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
Zhang Ruice44e192008-01-17 15:51:25 +0800495 valid = tz->trips.active[i].flags.valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Len Brownf8707ec2007-08-12 00:12:54 -0400497 if (act == -1)
Zhang Ruice44e192008-01-17 15:51:25 +0800498 break; /* disable all active trip points */
Len Brownf8707ec2007-08-12 00:12:54 -0400499
Zhang Rui0e4240d2009-01-16 12:53:42 -0500500 if ((flag == ACPI_TRIPS_INIT) || ((flag & ACPI_TRIPS_ACTIVE) &&
501 tz->trips.active[i].flags.valid)) {
Zhang Ruice44e192008-01-17 15:51:25 +0800502 status = acpi_evaluate_integer(tz->device->handle,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400503 name, NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800504 if (ACPI_FAILURE(status)) {
505 tz->trips.active[i].flags.valid = 0;
506 if (i == 0)
507 break;
508 if (act <= 0)
509 break;
510 if (i == 1)
511 tz->trips.active[0].temperature =
512 CELSIUS_TO_KELVIN(act);
513 else
514 /*
515 * Don't allow override higher than
516 * the next higher trip point
517 */
518 tz->trips.active[i - 1].temperature =
519 (tz->trips.active[i - 2].temperature <
520 CELSIUS_TO_KELVIN(act) ?
521 tz->trips.active[i - 2].temperature :
522 CELSIUS_TO_KELVIN(act));
Len Brownf8707ec2007-08-12 00:12:54 -0400523 break;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400524 } else {
525 tz->trips.active[i].temperature = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800526 tz->trips.active[i].flags.valid = 1;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400527 }
Len Brownf8707ec2007-08-12 00:12:54 -0400528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 name[2] = 'L';
Zhang Ruice44e192008-01-17 15:51:25 +0800531 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].flags.valid ) {
532 memset(&devices, 0, sizeof(struct acpi_handle_list));
533 status = acpi_evaluate_reference(tz->device->handle,
534 name, NULL, &devices);
Zhang Rui0e4240d2009-01-16 12:53:42 -0500535 if (ACPI_FAILURE(status)) {
536 printk(KERN_WARNING PREFIX
537 "Invalid active%d threshold\n", i);
Zhang Ruice44e192008-01-17 15:51:25 +0800538 tz->trips.active[i].flags.valid = 0;
Zhang Rui0e4240d2009-01-16 12:53:42 -0500539 }
Zhang Ruice44e192008-01-17 15:51:25 +0800540 else
541 tz->trips.active[i].flags.valid = 1;
542
543 if (memcmp(&tz->trips.active[i].devices, &devices,
544 sizeof(struct acpi_handle_list))) {
545 memcpy(&tz->trips.active[i].devices, &devices,
546 sizeof(struct acpi_handle_list));
547 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
548 }
549 }
550 if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
551 if (valid != tz->trips.active[i].flags.valid)
552 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
553
554 if (!tz->trips.active[i].flags.valid)
555 break;
556 }
557
558 if (flag & ACPI_TRIPS_DEVICES) {
559 memset(&devices, 0, sizeof(struct acpi_handle_list));
560 status = acpi_evaluate_reference(tz->device->handle, "_TZD",
561 NULL, &devices);
562 if (memcmp(&tz->devices, &devices,
563 sizeof(struct acpi_handle_list))) {
564 memcpy(&tz->devices, &devices,
565 sizeof(struct acpi_handle_list));
566 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569
Patrick Mocheld550d982006-06-27 00:41:40 -0400570 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Zhang Ruice44e192008-01-17 15:51:25 +0800573static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Zhang Ruice44e192008-01-17 15:51:25 +0800575 return acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
Len Brown4be44fc2005-08-05 00:44:28 -0400578static void acpi_thermal_check(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200580 struct acpi_thermal *tz = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Matthew Garrettb1569e92008-12-03 17:55:32 +0000582 thermal_zone_device_update(tz->thermal_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
Zhang Rui3f655ef2008-01-17 15:51:11 +0800585/* sys I/F for generic thermal sysfs support */
Zhang, Rui5e012762008-02-28 07:51:30 +0800586#define KELVIN_TO_MILLICELSIUS(t) (t * 100 - 273200)
587
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000588static int thermal_get_temp(struct thermal_zone_device *thermal,
589 unsigned long *temp)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800590{
591 struct acpi_thermal *tz = thermal->devdata;
Zhang, Rui76ecb4f2008-04-10 16:20:23 +0800592 int result;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800593
594 if (!tz)
595 return -EINVAL;
596
Zhang, Rui76ecb4f2008-04-10 16:20:23 +0800597 result = acpi_thermal_get_temperature(tz);
598 if (result)
599 return result;
600
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000601 *temp = KELVIN_TO_MILLICELSIUS(tz->temperature);
602 return 0;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800603}
604
605static const char enabled[] = "kernel";
606static const char disabled[] = "user";
607static int thermal_get_mode(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000608 enum thermal_device_mode *mode)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800609{
610 struct acpi_thermal *tz = thermal->devdata;
611
612 if (!tz)
613 return -EINVAL;
614
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000615 *mode = tz->tz_enabled ? THERMAL_DEVICE_ENABLED :
616 THERMAL_DEVICE_DISABLED;
617
618 return 0;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800619}
620
621static int thermal_set_mode(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000622 enum thermal_device_mode mode)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800623{
624 struct acpi_thermal *tz = thermal->devdata;
625 int enable;
626
627 if (!tz)
628 return -EINVAL;
629
630 /*
631 * enable/disable thermal management from ACPI thermal driver
632 */
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000633 if (mode == THERMAL_DEVICE_ENABLED)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800634 enable = 1;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000635 else if (mode == THERMAL_DEVICE_DISABLED)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800636 enable = 0;
637 else
638 return -EINVAL;
639
640 if (enable != tz->tz_enabled) {
641 tz->tz_enabled = enable;
642 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
643 "%s ACPI thermal control\n",
644 tz->tz_enabled ? enabled : disabled));
645 acpi_thermal_check(tz);
646 }
647 return 0;
648}
649
650static int thermal_get_trip_type(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000651 int trip, enum thermal_trip_type *type)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800652{
653 struct acpi_thermal *tz = thermal->devdata;
654 int i;
655
656 if (!tz || trip < 0)
657 return -EINVAL;
658
659 if (tz->trips.critical.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000660 if (!trip) {
661 *type = THERMAL_TRIP_CRITICAL;
662 return 0;
663 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800664 trip--;
665 }
666
667 if (tz->trips.hot.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000668 if (!trip) {
669 *type = THERMAL_TRIP_HOT;
670 return 0;
671 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800672 trip--;
673 }
674
675 if (tz->trips.passive.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000676 if (!trip) {
677 *type = THERMAL_TRIP_PASSIVE;
678 return 0;
679 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800680 trip--;
681 }
682
683 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
684 tz->trips.active[i].flags.valid; i++) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000685 if (!trip) {
686 *type = THERMAL_TRIP_ACTIVE;
687 return 0;
688 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800689 trip--;
690 }
691
692 return -EINVAL;
693}
694
695static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000696 int trip, unsigned long *temp)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800697{
698 struct acpi_thermal *tz = thermal->devdata;
699 int i;
700
701 if (!tz || trip < 0)
702 return -EINVAL;
703
704 if (tz->trips.critical.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000705 if (!trip) {
706 *temp = KELVIN_TO_MILLICELSIUS(
707 tz->trips.critical.temperature);
708 return 0;
709 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800710 trip--;
711 }
712
713 if (tz->trips.hot.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000714 if (!trip) {
715 *temp = KELVIN_TO_MILLICELSIUS(
716 tz->trips.hot.temperature);
717 return 0;
718 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800719 trip--;
720 }
721
722 if (tz->trips.passive.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000723 if (!trip) {
724 *temp = KELVIN_TO_MILLICELSIUS(
725 tz->trips.passive.temperature);
726 return 0;
727 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800728 trip--;
729 }
730
731 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
732 tz->trips.active[i].flags.valid; i++) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000733 if (!trip) {
734 *temp = KELVIN_TO_MILLICELSIUS(
735 tz->trips.active[i].temperature);
736 return 0;
737 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800738 trip--;
739 }
740
741 return -EINVAL;
742}
743
Zhang, Rui9ec732f2008-04-10 16:13:10 +0800744static int thermal_get_crit_temp(struct thermal_zone_device *thermal,
745 unsigned long *temperature) {
746 struct acpi_thermal *tz = thermal->devdata;
747
748 if (tz->trips.critical.flags.valid) {
749 *temperature = KELVIN_TO_MILLICELSIUS(
750 tz->trips.critical.temperature);
751 return 0;
752 } else
753 return -EINVAL;
754}
755
Matthew Garrettb1569e92008-12-03 17:55:32 +0000756static int thermal_notify(struct thermal_zone_device *thermal, int trip,
757 enum thermal_trip_type trip_type)
758{
759 u8 type = 0;
760 struct acpi_thermal *tz = thermal->devdata;
761
762 if (trip_type == THERMAL_TRIP_CRITICAL)
763 type = ACPI_THERMAL_NOTIFY_CRITICAL;
764 else if (trip_type == THERMAL_TRIP_HOT)
765 type = ACPI_THERMAL_NOTIFY_HOT;
766 else
767 return 0;
768
769 acpi_bus_generate_proc_event(tz->device, type, 1);
770 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
Stephen Rothwellf6f5c452009-03-03 12:47:17 +1100771 dev_name(&tz->device->dev), type, 1);
Matthew Garrettb1569e92008-12-03 17:55:32 +0000772
773 if (trip_type == THERMAL_TRIP_CRITICAL && nocrt)
774 return 1;
775
776 return 0;
777}
778
Zhang Rui3f655ef2008-01-17 15:51:11 +0800779typedef int (*cb)(struct thermal_zone_device *, int,
780 struct thermal_cooling_device *);
781static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
782 struct thermal_cooling_device *cdev,
783 cb action)
784{
785 struct acpi_device *device = cdev->devdata;
786 struct acpi_thermal *tz = thermal->devdata;
Zhang Rui653a00c2008-01-17 15:51:18 +0800787 struct acpi_device *dev;
788 acpi_status status;
789 acpi_handle handle;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800790 int i;
791 int j;
792 int trip = -1;
793 int result = 0;
794
795 if (tz->trips.critical.flags.valid)
796 trip++;
797
798 if (tz->trips.hot.flags.valid)
799 trip++;
800
801 if (tz->trips.passive.flags.valid) {
802 trip++;
803 for (i = 0; i < tz->trips.passive.devices.count;
804 i++) {
Zhang Rui653a00c2008-01-17 15:51:18 +0800805 handle = tz->trips.passive.devices.handles[i];
806 status = acpi_bus_get_device(handle, &dev);
807 if (ACPI_SUCCESS(status) && (dev == device)) {
808 result = action(thermal, trip, cdev);
809 if (result)
810 goto failed;
811 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800812 }
813 }
814
815 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
816 if (!tz->trips.active[i].flags.valid)
817 break;
818 trip++;
819 for (j = 0;
820 j < tz->trips.active[i].devices.count;
821 j++) {
Zhang Rui653a00c2008-01-17 15:51:18 +0800822 handle = tz->trips.active[i].devices.handles[j];
823 status = acpi_bus_get_device(handle, &dev);
824 if (ACPI_SUCCESS(status) && (dev == device)) {
825 result = action(thermal, trip, cdev);
826 if (result)
827 goto failed;
828 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800829 }
830 }
831
832 for (i = 0; i < tz->devices.count; i++) {
Zhang Rui653a00c2008-01-17 15:51:18 +0800833 handle = tz->devices.handles[i];
834 status = acpi_bus_get_device(handle, &dev);
835 if (ACPI_SUCCESS(status) && (dev == device)) {
836 result = action(thermal, -1, cdev);
837 if (result)
838 goto failed;
839 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800840 }
841
842failed:
843 return result;
844}
845
846static int
847acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
848 struct thermal_cooling_device *cdev)
849{
850 return acpi_thermal_cooling_device_cb(thermal, cdev,
851 thermal_zone_bind_cooling_device);
852}
853
854static int
855acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
856 struct thermal_cooling_device *cdev)
857{
858 return acpi_thermal_cooling_device_cb(thermal, cdev,
859 thermal_zone_unbind_cooling_device);
860}
861
862static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
863 .bind = acpi_thermal_bind_cooling_device,
864 .unbind = acpi_thermal_unbind_cooling_device,
865 .get_temp = thermal_get_temp,
866 .get_mode = thermal_get_mode,
867 .set_mode = thermal_set_mode,
868 .get_trip_type = thermal_get_trip_type,
869 .get_trip_temp = thermal_get_trip_temp,
Zhang, Rui9ec732f2008-04-10 16:13:10 +0800870 .get_crit_temp = thermal_get_crit_temp,
Matthew Garrettb1569e92008-12-03 17:55:32 +0000871 .notify = thermal_notify,
Zhang Rui3f655ef2008-01-17 15:51:11 +0800872};
873
874static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
875{
876 int trips = 0;
877 int result;
Zhang Rui20733932008-01-17 15:51:21 +0800878 acpi_status status;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800879 int i;
880
881 if (tz->trips.critical.flags.valid)
882 trips++;
883
884 if (tz->trips.hot.flags.valid)
885 trips++;
886
887 if (tz->trips.passive.flags.valid)
888 trips++;
889
890 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
891 tz->trips.active[i].flags.valid; i++, trips++);
Matthew Garrettb1569e92008-12-03 17:55:32 +0000892
893 if (tz->trips.passive.flags.valid)
894 tz->thermal_zone =
895 thermal_zone_device_register("acpitz", trips, tz,
896 &acpi_thermal_zone_ops,
897 tz->trips.passive.tc1,
898 tz->trips.passive.tc2,
899 tz->trips.passive.tsp*100,
900 tz->polling_frequency*100);
901 else
902 tz->thermal_zone =
903 thermal_zone_device_register("acpitz", trips, tz,
904 &acpi_thermal_zone_ops,
905 0, 0, 0,
906 tz->polling_frequency);
Krzysztof Heltbb070e42008-04-08 17:41:52 -0700907 if (IS_ERR(tz->thermal_zone))
Zhang Rui3f655ef2008-01-17 15:51:11 +0800908 return -ENODEV;
909
910 result = sysfs_create_link(&tz->device->dev.kobj,
911 &tz->thermal_zone->device.kobj, "thermal_zone");
912 if (result)
913 return result;
914
915 result = sysfs_create_link(&tz->thermal_zone->device.kobj,
916 &tz->device->dev.kobj, "device");
917 if (result)
918 return result;
919
Zhang Rui20733932008-01-17 15:51:21 +0800920 status = acpi_attach_data(tz->device->handle,
921 acpi_bus_private_data_handler,
922 tz->thermal_zone);
923 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800924 printk(KERN_ERR PREFIX
925 "Error attaching device data\n");
Zhang Rui20733932008-01-17 15:51:21 +0800926 return -ENODEV;
927 }
928
Zhang Rui3f655ef2008-01-17 15:51:11 +0800929 tz->tz_enabled = 1;
930
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200931 dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
932 tz->thermal_zone->id);
Zhang Rui3f655ef2008-01-17 15:51:11 +0800933 return 0;
934}
935
936static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
937{
938 sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
939 sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
940 thermal_zone_device_unregister(tz->thermal_zone);
941 tz->thermal_zone = NULL;
Zhang Rui20733932008-01-17 15:51:21 +0800942 acpi_detach_data(tz->device->handle, acpi_bus_private_data_handler);
Zhang Rui3f655ef2008-01-17 15:51:11 +0800943}
944
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946/* --------------------------------------------------------------------------
947 FS Interface (/proc)
948 -------------------------------------------------------------------------- */
949
Len Brown4be44fc2005-08-05 00:44:28 -0400950static struct proc_dir_entry *acpi_thermal_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
953{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200954 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 if (!tz)
958 goto end;
959
960 seq_puts(seq, "state: ");
961
Len Brown4be44fc2005-08-05 00:44:28 -0400962 if (!tz->state.critical && !tz->state.hot && !tz->state.passive
963 && !tz->state.active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 seq_puts(seq, "ok\n");
965 else {
966 if (tz->state.critical)
967 seq_puts(seq, "critical ");
968 if (tz->state.hot)
969 seq_puts(seq, "hot ");
970 if (tz->state.passive)
971 seq_puts(seq, "passive ");
972 if (tz->state.active)
973 seq_printf(seq, "active[%d]", tz->state.active_index);
974 seq_puts(seq, "\n");
975 }
976
Len Brown4be44fc2005-08-05 00:44:28 -0400977 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400978 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
981static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
982{
983 return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
984}
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
987{
Len Brown4be44fc2005-08-05 00:44:28 -0400988 int result = 0;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200989 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 if (!tz)
993 goto end;
994
995 result = acpi_thermal_get_temperature(tz);
996 if (result)
997 goto end;
998
Len Brown4be44fc2005-08-05 00:44:28 -0400999 seq_printf(seq, "temperature: %ld C\n",
1000 KELVIN_TO_CELSIUS(tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Len Brown4be44fc2005-08-05 00:44:28 -04001002 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001003 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
1005
1006static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
1007{
1008 return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
1009}
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
1012{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001013 struct acpi_thermal *tz = seq->private;
Thomas Renninger68ccfaa2006-11-19 23:01:40 +01001014 struct acpi_device *device;
Thomas Renningere7c746e2007-06-18 00:40:51 -04001015 acpi_status status;
1016
Len Brown4be44fc2005-08-05 00:44:28 -04001017 int i = 0;
1018 int j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 if (!tz)
1022 goto end;
1023
1024 if (tz->trips.critical.flags.valid)
Len Brownf5487142007-08-12 00:12:44 -04001025 seq_printf(seq, "critical (S5): %ld C%s",
1026 KELVIN_TO_CELSIUS(tz->trips.critical.temperature),
1027 nocrt ? " <disabled>\n" : "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 if (tz->trips.hot.flags.valid)
Len Brownf5487142007-08-12 00:12:44 -04001030 seq_printf(seq, "hot (S4): %ld C%s",
1031 KELVIN_TO_CELSIUS(tz->trips.hot.temperature),
1032 nocrt ? " <disabled>\n" : "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 if (tz->trips.passive.flags.valid) {
Len Brown4be44fc2005-08-05 00:44:28 -04001035 seq_printf(seq,
1036 "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
1037 KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
1038 tz->trips.passive.tc1, tz->trips.passive.tc2,
1039 tz->trips.passive.tsp);
1040 for (j = 0; j < tz->trips.passive.devices.count; j++) {
Thomas Renningere7c746e2007-06-18 00:40:51 -04001041 status = acpi_bus_get_device(tz->trips.passive.devices.
1042 handles[j], &device);
1043 seq_printf(seq, "%4.4s ", status ? "" :
1044 acpi_device_bid(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
1046 seq_puts(seq, "\n");
1047 }
1048
1049 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1050 if (!(tz->trips.active[i].flags.valid))
1051 break;
1052 seq_printf(seq, "active[%d]: %ld C: devices=",
Len Brown4be44fc2005-08-05 00:44:28 -04001053 i,
1054 KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
Thomas Renninger68ccfaa2006-11-19 23:01:40 +01001055 for (j = 0; j < tz->trips.active[i].devices.count; j++){
Thomas Renningere7c746e2007-06-18 00:40:51 -04001056 status = acpi_bus_get_device(tz->trips.active[i].
1057 devices.handles[j],
1058 &device);
1059 seq_printf(seq, "%4.4s ", status ? "" :
1060 acpi_device_bid(device));
Thomas Renninger68ccfaa2006-11-19 23:01:40 +01001061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 seq_puts(seq, "\n");
1063 }
1064
Len Brown4be44fc2005-08-05 00:44:28 -04001065 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001066 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
1068
1069static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
1070{
1071 return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
1072}
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
1075{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001076 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 if (!tz)
1080 goto end;
1081
Len Browneaca2d32007-04-30 23:27:43 -04001082 if (!tz->flags.cooling_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 seq_puts(seq, "<setting not supported>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 else
Len Browneaca2d32007-04-30 23:27:43 -04001085 seq_puts(seq, "0 - Active; 1 - Passive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Len Brown4be44fc2005-08-05 00:44:28 -04001087 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001088 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
1090
1091static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
1092{
1093 return single_open(file, acpi_thermal_cooling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001094 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095}
1096
1097static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001098acpi_thermal_write_cooling_mode(struct file *file,
1099 const char __user * buffer,
1100 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001102 struct seq_file *m = file->private_data;
1103 struct acpi_thermal *tz = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001104 int result = 0;
1105 char mode_string[12] = { '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 if (!tz || (count > sizeof(mode_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001109 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 if (!tz->flags.cooling_mode)
Patrick Mocheld550d982006-06-27 00:41:40 -04001112 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 if (copy_from_user(mode_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001115 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -04001116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 mode_string[count] = '\0';
Len Brown4be44fc2005-08-05 00:44:28 -04001118
1119 result = acpi_thermal_set_cooling_mode(tz,
1120 simple_strtoul(mode_string, NULL,
1121 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001123 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 acpi_thermal_check(tz);
1126
Patrick Mocheld550d982006-06-27 00:41:40 -04001127 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
1131{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001132 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 if (!tz)
1136 goto end;
1137
Matthew Garrettb1569e92008-12-03 17:55:32 +00001138 if (!tz->thermal_zone->polling_delay) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 seq_puts(seq, "<polling disabled>\n");
1140 goto end;
1141 }
1142
Matthew Garrettb1569e92008-12-03 17:55:32 +00001143 seq_printf(seq, "polling frequency: %d seconds\n",
1144 (tz->thermal_zone->polling_delay / 1000));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Len Brown4be44fc2005-08-05 00:44:28 -04001146 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001147 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
1149
1150static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
1151{
1152 return single_open(file, acpi_thermal_polling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001153 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
1156static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001157acpi_thermal_write_polling(struct file *file,
1158 const char __user * buffer,
1159 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001161 struct seq_file *m = file->private_data;
1162 struct acpi_thermal *tz = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001163 int result = 0;
1164 char polling_string[12] = { '\0' };
1165 int seconds = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 if (!tz || (count > sizeof(polling_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001169 return -EINVAL;
Len Brown4be44fc2005-08-05 00:44:28 -04001170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if (copy_from_user(polling_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001172 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -04001173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 polling_string[count] = '\0';
1175
1176 seconds = simple_strtoul(polling_string, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 result = acpi_thermal_set_polling(tz, seconds);
1179 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001180 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 acpi_thermal_check(tz);
1183
Patrick Mocheld550d982006-06-27 00:41:40 -04001184 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186
Len Brown4be44fc2005-08-05 00:44:28 -04001187static int acpi_thermal_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Len Brown4be44fc2005-08-05 00:44:28 -04001189 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 if (!acpi_device_dir(device)) {
1193 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -04001194 acpi_thermal_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001196 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
1198
1199 /* 'state' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001200 entry = proc_create_data(ACPI_THERMAL_FILE_STATE,
1201 S_IRUGO, acpi_device_dir(device),
1202 &acpi_thermal_state_fops,
1203 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001205 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 /* 'temperature' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001208 entry = proc_create_data(ACPI_THERMAL_FILE_TEMPERATURE,
1209 S_IRUGO, acpi_device_dir(device),
1210 &acpi_thermal_temp_fops,
1211 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001213 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Pavel Machek2db9ccb2007-08-24 11:45:50 +02001215 /* 'trip_points' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001216 entry = proc_create_data(ACPI_THERMAL_FILE_TRIP_POINTS,
1217 S_IRUGO,
1218 acpi_device_dir(device),
1219 &acpi_thermal_trip_fops,
1220 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001222 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 /* 'cooling_mode' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001225 entry = proc_create_data(ACPI_THERMAL_FILE_COOLING_MODE,
1226 S_IFREG | S_IRUGO | S_IWUSR,
1227 acpi_device_dir(device),
1228 &acpi_thermal_cooling_fops,
1229 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001231 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 /* 'polling_frequency' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001234 entry = proc_create_data(ACPI_THERMAL_FILE_POLLING_FREQ,
1235 S_IFREG | S_IRUGO | S_IWUSR,
1236 acpi_device_dir(device),
1237 &acpi_thermal_polling_fops,
1238 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001240 return -ENODEV;
Patrick Mocheld550d982006-06-27 00:41:40 -04001241 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
Len Brown4be44fc2005-08-05 00:44:28 -04001244static int acpi_thermal_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 if (acpi_device_dir(device)) {
1248 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1249 acpi_device_dir(device));
1250 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1251 acpi_device_dir(device));
1252 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1253 acpi_device_dir(device));
1254 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1255 acpi_device_dir(device));
1256 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1257 acpi_device_dir(device));
1258 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1259 acpi_device_dir(device) = NULL;
1260 }
1261
Patrick Mocheld550d982006-06-27 00:41:40 -04001262 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263}
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265/* --------------------------------------------------------------------------
1266 Driver Interface
1267 -------------------------------------------------------------------------- */
1268
Bjorn Helgaas342d5502009-04-07 15:37:06 +00001269static void acpi_thermal_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
Bjorn Helgaas342d5502009-04-07 15:37:06 +00001271 struct acpi_thermal *tz = acpi_driver_data(device);
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 switch (event) {
1278 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1279 acpi_thermal_check(tz);
1280 break;
1281 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
Zhang Ruice44e192008-01-17 15:51:25 +08001282 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 acpi_thermal_check(tz);
Len Brown14e04fb2007-08-23 15:20:26 -04001284 acpi_bus_generate_proc_event(device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08001285 acpi_bus_generate_netlink_event(device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +01001286 dev_name(&device->dev), event, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 break;
1288 case ACPI_THERMAL_NOTIFY_DEVICES:
Zhang Ruice44e192008-01-17 15:51:25 +08001289 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
1290 acpi_thermal_check(tz);
Len Brown14e04fb2007-08-23 15:20:26 -04001291 acpi_bus_generate_proc_event(device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08001292 acpi_bus_generate_netlink_event(device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +01001293 dev_name(&device->dev), event, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 break;
1295 default:
1296 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001297 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 break;
1299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300}
1301
Len Brown4be44fc2005-08-05 00:44:28 -04001302static int acpi_thermal_get_info(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
Len Brown4be44fc2005-08-05 00:44:28 -04001304 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001308 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 /* Get temperature [_TMP] (required) */
1311 result = acpi_thermal_get_temperature(tz);
1312 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001313 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 /* Get trip points [_CRT, _PSV, etc.] (required) */
1316 result = acpi_thermal_get_trip_points(tz);
1317 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001318 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 /* Set the cooling mode [_SCP] to active cooling (default) */
1321 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
Len Brown4be44fc2005-08-05 00:44:28 -04001322 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 tz->flags.cooling_mode = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 /* Get default polling frequency [_TZP] (optional) */
1326 if (tzp)
1327 tz->polling_frequency = tzp;
1328 else
1329 acpi_thermal_get_polling_frequency(tz);
1330
Patrick Mocheld550d982006-06-27 00:41:40 -04001331 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
1333
Len Brown4be44fc2005-08-05 00:44:28 -04001334static int acpi_thermal_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
Len Brown4be44fc2005-08-05 00:44:28 -04001336 int result = 0;
Len Brown4be44fc2005-08-05 00:44:28 -04001337 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001341 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Burman Yan36bcbec2006-12-19 12:56:11 -08001343 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001345 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Patrick Mochel8348e1b2006-05-19 16:54:40 -04001347 tz->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 strcpy(tz->name, device->pnp.bus_id);
1349 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1350 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001351 device->driver_data = tz;
Alexey Starikovskiy6e215782007-09-01 00:11:59 +04001352 mutex_init(&tz->lock);
Zhang Rui3f655ef2008-01-17 15:51:11 +08001353
1354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 result = acpi_thermal_get_info(tz);
1356 if (result)
Zhang Rui3f655ef2008-01-17 15:51:11 +08001357 goto free_memory;
1358
1359 result = acpi_thermal_register_thermal_zone(tz);
1360 if (result)
1361 goto free_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 result = acpi_thermal_add_fs(device);
1364 if (result)
Zhang Rui3f655ef2008-01-17 15:51:11 +08001365 goto unregister_thermal_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001368 acpi_device_name(device), acpi_device_bid(device),
1369 KELVIN_TO_CELSIUS(tz->temperature));
Zhang Rui3f655ef2008-01-17 15:51:11 +08001370 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Zhang Rui3f655ef2008-01-17 15:51:11 +08001372unregister_thermal_zone:
1373 thermal_zone_device_unregister(tz->thermal_zone);
1374free_memory:
1375 kfree(tz);
1376end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001377 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
1379
Len Brown4be44fc2005-08-05 00:44:28 -04001380static int acpi_thermal_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
Len Brown4be44fc2005-08-05 00:44:28 -04001382 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001385 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001387 tz = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 acpi_thermal_remove_fs(device);
Zhang Rui3f655ef2008-01-17 15:51:11 +08001390 acpi_thermal_unregister_thermal_zone(tz);
Alexey Starikovskiy6e215782007-09-01 00:11:59 +04001391 mutex_destroy(&tz->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 kfree(tz);
Patrick Mocheld550d982006-06-27 00:41:40 -04001393 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394}
1395
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001396static int acpi_thermal_resume(struct acpi_device *device)
Konstantin Karasyov74ce1462006-05-08 08:32:00 -04001397{
1398 struct acpi_thermal *tz = NULL;
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001399 int i, j, power_state, result;
1400
Konstantin Karasyov74ce1462006-05-08 08:32:00 -04001401
1402 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001403 return -EINVAL;
Konstantin Karasyov74ce1462006-05-08 08:32:00 -04001404
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001405 tz = acpi_driver_data(device);
Konstantin Karasyov74ce1462006-05-08 08:32:00 -04001406
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001407 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001408 if (!(&tz->trips.active[i]))
1409 break;
1410 if (!tz->trips.active[i].flags.valid)
1411 break;
1412 tz->trips.active[i].flags.enabled = 1;
1413 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1414 result = acpi_bus_get_power(tz->trips.active[i].devices.
1415 handles[j], &power_state);
1416 if (result || (power_state != ACPI_STATE_D0)) {
1417 tz->trips.active[i].flags.enabled = 0;
1418 break;
1419 }
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001420 }
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001421 tz->state.active |= tz->trips.active[i].flags.enabled;
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001422 }
1423
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001424 acpi_thermal_check(tz);
Konstantin Karasyov74ce1462006-05-08 08:32:00 -04001425
1426 return AE_OK;
1427}
1428
Jeff Garzik18552562007-10-03 15:15:40 -04001429static int thermal_act(const struct dmi_system_id *d) {
Len Brown0b5bfa12007-08-12 00:13:02 -04001430
1431 if (act == 0) {
1432 printk(KERN_NOTICE "ACPI: %s detected: "
1433 "disabling all active thermal trip points\n", d->ident);
1434 act = -1;
1435 }
1436 return 0;
1437}
Jeff Garzik18552562007-10-03 15:15:40 -04001438static int thermal_nocrt(const struct dmi_system_id *d) {
Len Brown8c99fdc2007-08-20 18:46:50 -04001439
1440 printk(KERN_NOTICE "ACPI: %s detected: "
1441 "disabling all critical thermal trip point actions.\n", d->ident);
1442 nocrt = 1;
1443 return 0;
1444}
Jeff Garzik18552562007-10-03 15:15:40 -04001445static int thermal_tzp(const struct dmi_system_id *d) {
Len Brown0b5bfa12007-08-12 00:13:02 -04001446
1447 if (tzp == 0) {
1448 printk(KERN_NOTICE "ACPI: %s detected: "
1449 "enabling thermal zone polling\n", d->ident);
1450 tzp = 300; /* 300 dS = 30 Seconds */
1451 }
1452 return 0;
1453}
Jeff Garzik18552562007-10-03 15:15:40 -04001454static int thermal_psv(const struct dmi_system_id *d) {
Len Brown0b5bfa12007-08-12 00:13:02 -04001455
1456 if (psv == 0) {
1457 printk(KERN_NOTICE "ACPI: %s detected: "
1458 "disabling all passive thermal trip points\n", d->ident);
1459 psv = -1;
1460 }
1461 return 0;
1462}
1463
1464static struct dmi_system_id thermal_dmi_table[] __initdata = {
1465 /*
1466 * Award BIOS on this AOpen makes thermal control almost worthless.
1467 * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1468 */
1469 {
1470 .callback = thermal_act,
1471 .ident = "AOpen i915GMm-HFS",
1472 .matches = {
1473 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1474 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1475 },
1476 },
1477 {
1478 .callback = thermal_psv,
1479 .ident = "AOpen i915GMm-HFS",
1480 .matches = {
1481 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1482 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1483 },
1484 },
1485 {
1486 .callback = thermal_tzp,
1487 .ident = "AOpen i915GMm-HFS",
1488 .matches = {
1489 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1490 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1491 },
1492 },
Len Brown8c99fdc2007-08-20 18:46:50 -04001493 {
1494 .callback = thermal_nocrt,
1495 .ident = "Gigabyte GA-7ZX",
1496 .matches = {
1497 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
1498 DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
1499 },
1500 },
Len Brown0b5bfa12007-08-12 00:13:02 -04001501 {}
1502};
Len Brown0b5bfa12007-08-12 00:13:02 -04001503
Len Brown4be44fc2005-08-05 00:44:28 -04001504static int __init acpi_thermal_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
Len Brown4be44fc2005-08-05 00:44:28 -04001506 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Len Brown0b5bfa12007-08-12 00:13:02 -04001508 dmi_check_system(thermal_dmi_table);
1509
Len Brown72b33ef2007-08-12 00:12:17 -04001510 if (off) {
1511 printk(KERN_NOTICE "ACPI: thermal control disabled\n");
1512 return -ENODEV;
1513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1515 if (!acpi_thermal_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001516 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 result = acpi_bus_register_driver(&acpi_thermal_driver);
1519 if (result < 0) {
1520 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04001521 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
1523
Patrick Mocheld550d982006-06-27 00:41:40 -04001524 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525}
1526
Len Brown4be44fc2005-08-05 00:44:28 -04001527static void __exit acpi_thermal_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 acpi_bus_unregister_driver(&acpi_thermal_driver);
1531
1532 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1533
Patrick Mocheld550d982006-06-27 00:41:40 -04001534 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535}
1536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537module_init(acpi_thermal_init);
1538module_exit(acpi_thermal_exit);