blob: b6b3bec845473f68ac88576c38be0a12430ff46e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * acpi_thermal.c - ACPI Thermal Zone Driver ($Revision: 41 $)
3 *
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6 *
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 *
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 *
25 * This driver fully implements the ACPI thermal policy as described in the
26 * ACPI 2.0 Specification.
27 *
28 * TBD: 1. Implement passive cooling hysteresis.
29 * 2. Enhance passive cooling (CPU) states/limit interface to support
30 * concepts of 'multiple limiters', upper/lower limits, etc.
31 *
32 */
33
34#include <linux/kernel.h>
35#include <linux/module.h>
36#include <linux/init.h>
37#include <linux/types.h>
38#include <linux/proc_fs.h>
Tim Schmielaucd354f12007-02-14 00:33:14 -080039#include <linux/timer.h>
40#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/kmod.h>
42#include <linux/seq_file.h>
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -070043#include <linux/reboot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/uaccess.h>
45
46#include <acpi/acpi_bus.h>
47#include <acpi/acpi_drivers.h>
48
49#define ACPI_THERMAL_COMPONENT 0x04000000
50#define ACPI_THERMAL_CLASS "thermal_zone"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
52#define ACPI_THERMAL_FILE_STATE "state"
53#define ACPI_THERMAL_FILE_TEMPERATURE "temperature"
54#define ACPI_THERMAL_FILE_TRIP_POINTS "trip_points"
55#define ACPI_THERMAL_FILE_COOLING_MODE "cooling_mode"
56#define ACPI_THERMAL_FILE_POLLING_FREQ "polling_frequency"
57#define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
58#define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
59#define ACPI_THERMAL_NOTIFY_DEVICES 0x82
60#define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
61#define ACPI_THERMAL_NOTIFY_HOT 0xF1
62#define ACPI_THERMAL_MODE_ACTIVE 0x00
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64#define ACPI_THERMAL_MAX_ACTIVE 10
65#define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
66
67#define KELVIN_TO_CELSIUS(t) (long)(((long)t-2732>=0) ? ((long)t-2732+5)/10 : ((long)t-2732-5)/10)
68#define CELSIUS_TO_KELVIN(t) ((t+273)*10)
69
70#define _COMPONENT ACPI_THERMAL_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050071ACPI_MODULE_NAME("thermal");
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Thomas Renninger1cbf4c52004-09-16 11:07:00 -040073MODULE_AUTHOR("Paul Diefenbaugh");
Len Brown7cda93e2007-02-12 23:50:02 -050074MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070075MODULE_LICENSE("GPL");
76
77static int tzp;
Len Brown730ff342007-08-12 00:12:26 -040078module_param(tzp, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.\n");
80
Len Brown72b33ef2007-08-12 00:12:17 -040081static int off;
82module_param(off, int, 0);
83MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.\n");
84
Len Brown4be44fc2005-08-05 00:44:28 -040085static int acpi_thermal_add(struct acpi_device *device);
86static int acpi_thermal_remove(struct acpi_device *device, int type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +080087static int acpi_thermal_resume(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
89static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
90static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -040092static ssize_t acpi_thermal_write_cooling_mode(struct file *,
93 const char __user *, size_t,
94 loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -040096static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
97 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Thomas Renninger1ba90e32007-07-23 14:44:41 +020099static const struct acpi_device_id thermal_device_ids[] = {
100 {ACPI_THERMAL_HID, 0},
101 {"", 0},
102};
103MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static struct acpi_driver acpi_thermal_driver = {
Len Brownc2b67052007-02-12 23:33:40 -0500106 .name = "thermal",
Len Brown4be44fc2005-08-05 00:44:28 -0400107 .class = ACPI_THERMAL_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200108 .ids = thermal_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -0400109 .ops = {
110 .add = acpi_thermal_add,
111 .remove = acpi_thermal_remove,
Konstantin Karasyov74ce14682006-05-08 08:32:00 -0400112 .resume = acpi_thermal_resume,
Len Brown4be44fc2005-08-05 00:44:28 -0400113 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114};
115
116struct acpi_thermal_state {
Len Brown4be44fc2005-08-05 00:44:28 -0400117 u8 critical:1;
118 u8 hot:1;
119 u8 passive:1;
120 u8 active:1;
121 u8 reserved:4;
122 int active_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123};
124
125struct acpi_thermal_state_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400126 u8 valid:1;
127 u8 enabled:1;
128 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129};
130
131struct acpi_thermal_critical {
132 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400133 unsigned long temperature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
135
136struct acpi_thermal_hot {
137 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400138 unsigned long temperature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139};
140
141struct acpi_thermal_passive {
142 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400143 unsigned long temperature;
144 unsigned long tc1;
145 unsigned long tc2;
146 unsigned long tsp;
147 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148};
149
150struct acpi_thermal_active {
151 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400152 unsigned long temperature;
153 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154};
155
156struct acpi_thermal_trips {
157 struct acpi_thermal_critical critical;
Len Brown4be44fc2005-08-05 00:44:28 -0400158 struct acpi_thermal_hot hot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 struct acpi_thermal_passive passive;
160 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
161};
162
163struct acpi_thermal_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400164 u8 cooling_mode:1; /* _SCP */
165 u8 devices:1; /* _TZD */
166 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167};
168
169struct acpi_thermal {
Patrick Mochel8348e1b2006-05-19 16:54:40 -0400170 struct acpi_device * device;
Len Brown4be44fc2005-08-05 00:44:28 -0400171 acpi_bus_id name;
172 unsigned long temperature;
173 unsigned long last_temperature;
174 unsigned long polling_frequency;
Len Brown4be44fc2005-08-05 00:44:28 -0400175 volatile u8 zombie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 struct acpi_thermal_flags flags;
177 struct acpi_thermal_state state;
178 struct acpi_thermal_trips trips;
Len Brown4be44fc2005-08-05 00:44:28 -0400179 struct acpi_handle_list devices;
180 struct timer_list timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181};
182
Arjan van de Vend7508032006-07-04 13:06:00 -0400183static const struct file_operations acpi_thermal_state_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400184 .open = acpi_thermal_state_open_fs,
185 .read = seq_read,
186 .llseek = seq_lseek,
187 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188};
189
Arjan van de Vend7508032006-07-04 13:06:00 -0400190static const struct file_operations acpi_thermal_temp_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400191 .open = acpi_thermal_temp_open_fs,
192 .read = seq_read,
193 .llseek = seq_lseek,
194 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195};
196
Arjan van de Vend7508032006-07-04 13:06:00 -0400197static const struct file_operations acpi_thermal_trip_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400198 .open = acpi_thermal_trip_open_fs,
199 .read = seq_read,
Len Brown4be44fc2005-08-05 00:44:28 -0400200 .llseek = seq_lseek,
201 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202};
203
Arjan van de Vend7508032006-07-04 13:06:00 -0400204static const struct file_operations acpi_thermal_cooling_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400205 .open = acpi_thermal_cooling_open_fs,
206 .read = seq_read,
207 .write = acpi_thermal_write_cooling_mode,
208 .llseek = seq_lseek,
209 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210};
211
Arjan van de Vend7508032006-07-04 13:06:00 -0400212static const struct file_operations acpi_thermal_polling_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400213 .open = acpi_thermal_polling_open_fs,
214 .read = seq_read,
215 .write = acpi_thermal_write_polling,
216 .llseek = seq_lseek,
217 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218};
219
220/* --------------------------------------------------------------------------
221 Thermal Zone Management
222 -------------------------------------------------------------------------- */
223
Len Brown4be44fc2005-08-05 00:44:28 -0400224static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Len Brown4be44fc2005-08-05 00:44:28 -0400226 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400230 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 tz->last_temperature = tz->temperature;
233
Len Brown4be44fc2005-08-05 00:44:28 -0400234 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400235 acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tz->temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400237 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Len Brown4be44fc2005-08-05 00:44:28 -0400239 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
240 tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Patrick Mocheld550d982006-06-27 00:41:40 -0400242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
Len Brown4be44fc2005-08-05 00:44:28 -0400245static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Len Brown4be44fc2005-08-05 00:44:28 -0400247 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400251 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Len Brown4be44fc2005-08-05 00:44:28 -0400253 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400254 acpi_evaluate_integer(tz->device->handle, "_TZP", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400255 &tz->polling_frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400257 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Len Brown4be44fc2005-08-05 00:44:28 -0400259 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
260 tz->polling_frequency));
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_set_polling(struct acpi_thermal *tz, int seconds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400269 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 tz->polling_frequency = seconds * 10; /* Convert value to deci-seconds */
272
Len Brown4be44fc2005-08-05 00:44:28 -0400273 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
274 "Polling frequency set to %lu seconds\n",
Sanjoy Mahajan636cedf2007-02-16 01:24:43 -0500275 tz->polling_frequency/10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Patrick Mocheld550d982006-06-27 00:41:40 -0400277 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
Len Brown4be44fc2005-08-05 00:44:28 -0400280static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Len Brown4be44fc2005-08-05 00:44:28 -0400282 acpi_status status = AE_OK;
283 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
284 struct acpi_object_list arg_list = { 1, &arg0 };
285 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400289 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400291 status = acpi_get_handle(tz->device->handle, "_SCP", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (ACPI_FAILURE(status)) {
293 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400294 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296
297 arg0.integer.value = mode;
298
299 status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
300 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400301 return -ENODEV;
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_get_trip_points(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Len Brown4be44fc2005-08-05 00:44:28 -0400308 acpi_status status = AE_OK;
309 int i = 0;
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
315 /* Critical Shutdown (required) */
316
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400317 status = acpi_evaluate_integer(tz->device->handle, "_CRT", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400318 &tz->trips.critical.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (ACPI_FAILURE(status)) {
320 tz->trips.critical.flags.valid = 0;
Thomas Renningera6fc6722006-06-26 23:58:43 -0400321 ACPI_EXCEPTION((AE_INFO, status, "No critical threshold"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400322 return -ENODEV;
Len Brown4be44fc2005-08-05 00:44:28 -0400323 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 tz->trips.critical.flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400325 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
326 "Found critical threshold [%lu]\n",
327 tz->trips.critical.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329
330 /* Critical Sleep (optional) */
331
Len Brown4be44fc2005-08-05 00:44:28 -0400332 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400333 acpi_evaluate_integer(tz->device->handle, "_HOT", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400334 &tz->trips.hot.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (ACPI_FAILURE(status)) {
336 tz->trips.hot.flags.valid = 0;
337 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No hot threshold\n"));
Len Brown4be44fc2005-08-05 00:44:28 -0400338 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 tz->trips.hot.flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400340 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found hot threshold [%lu]\n",
341 tz->trips.hot.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343
344 /* Passive: Processors (optional) */
345
Len Brown4be44fc2005-08-05 00:44:28 -0400346 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400347 acpi_evaluate_integer(tz->device->handle, "_PSV", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400348 &tz->trips.passive.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (ACPI_FAILURE(status)) {
350 tz->trips.passive.flags.valid = 0;
351 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No passive threshold\n"));
Len Brown4be44fc2005-08-05 00:44:28 -0400352 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 tz->trips.passive.flags.valid = 1;
354
Len Brown4be44fc2005-08-05 00:44:28 -0400355 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400356 acpi_evaluate_integer(tz->device->handle, "_TC1", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400357 &tz->trips.passive.tc1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (ACPI_FAILURE(status))
359 tz->trips.passive.flags.valid = 0;
360
Len Brown4be44fc2005-08-05 00:44:28 -0400361 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400362 acpi_evaluate_integer(tz->device->handle, "_TC2", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400363 &tz->trips.passive.tc2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (ACPI_FAILURE(status))
365 tz->trips.passive.flags.valid = 0;
366
Len Brown4be44fc2005-08-05 00:44:28 -0400367 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400368 acpi_evaluate_integer(tz->device->handle, "_TSP", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400369 &tz->trips.passive.tsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (ACPI_FAILURE(status))
371 tz->trips.passive.flags.valid = 0;
372
Len Brown4be44fc2005-08-05 00:44:28 -0400373 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400374 acpi_evaluate_reference(tz->device->handle, "_PSL", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400375 &tz->trips.passive.devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (ACPI_FAILURE(status))
377 tz->trips.passive.flags.valid = 0;
378
379 if (!tz->trips.passive.flags.valid)
Len Browncece9292006-06-26 23:04:31 -0400380 printk(KERN_WARNING PREFIX "Invalid passive threshold\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 else
Len Brown4be44fc2005-08-05 00:44:28 -0400382 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
383 "Found passive threshold [%lu]\n",
384 tz->trips.passive.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
386
387 /* Active: Fans, etc. (optional) */
388
Len Brown4be44fc2005-08-05 00:44:28 -0400389 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Len Brown4be44fc2005-08-05 00:44:28 -0400391 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Len Brown4be44fc2005-08-05 00:44:28 -0400393 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400394 acpi_evaluate_integer(tz->device->handle, name, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400395 &tz->trips.active[i].temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (ACPI_FAILURE(status))
397 break;
398
399 name[2] = 'L';
Len Brown4be44fc2005-08-05 00:44:28 -0400400 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400401 acpi_evaluate_reference(tz->device->handle, name, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400402 &tz->trips.active[i].devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (ACPI_SUCCESS(status)) {
404 tz->trips.active[i].flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400405 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
406 "Found active threshold [%d]:[%lu]\n",
407 i, tz->trips.active[i].temperature));
408 } else
Thomas Renningera6fc6722006-06-26 23:58:43 -0400409 ACPI_EXCEPTION((AE_INFO, status,
410 "Invalid active threshold [%d]", i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412
Patrick Mocheld550d982006-06-27 00:41:40 -0400413 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
Len Brown4be44fc2005-08-05 00:44:28 -0400416static int acpi_thermal_get_devices(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Len Brown4be44fc2005-08-05 00:44:28 -0400418 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400422 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Len Brown4be44fc2005-08-05 00:44:28 -0400424 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400425 acpi_evaluate_reference(tz->device->handle, "_TZD", NULL, &tz->devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400427 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Patrick Mocheld550d982006-06-27 00:41:40 -0400429 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Len Brown4be44fc2005-08-05 00:44:28 -0400432static int acpi_thermal_critical(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (!tz || !tz->trips.critical.flags.valid)
Patrick Mocheld550d982006-06-27 00:41:40 -0400435 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 if (tz->temperature >= tz->trips.critical.temperature) {
Len Browncece9292006-06-26 23:04:31 -0400438 printk(KERN_WARNING PREFIX "Critical trip point\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 tz->trips.critical.flags.enabled = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400440 } else if (tz->trips.critical.flags.enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 tz->trips.critical.flags.enabled = 0;
442
Len Brown4be44fc2005-08-05 00:44:28 -0400443 printk(KERN_EMERG
444 "Critical temperature reached (%ld C), shutting down.\n",
445 KELVIN_TO_CELSIUS(tz->temperature));
Patrick Mochel8348e1b2006-05-19 16:54:40 -0400446 acpi_bus_generate_event(tz->device, ACPI_THERMAL_NOTIFY_CRITICAL,
Len Brown4be44fc2005-08-05 00:44:28 -0400447 tz->trips.critical.flags.enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -0700449 orderly_poweroff(true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Patrick Mocheld550d982006-06-27 00:41:40 -0400451 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Len Brown4be44fc2005-08-05 00:44:28 -0400454static int acpi_thermal_hot(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (!tz || !tz->trips.hot.flags.valid)
Patrick Mocheld550d982006-06-27 00:41:40 -0400457 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 if (tz->temperature >= tz->trips.hot.temperature) {
Len Browncece9292006-06-26 23:04:31 -0400460 printk(KERN_WARNING PREFIX "Hot trip point\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 tz->trips.hot.flags.enabled = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400462 } else if (tz->trips.hot.flags.enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 tz->trips.hot.flags.enabled = 0;
464
Patrick Mochel8348e1b2006-05-19 16:54:40 -0400465 acpi_bus_generate_event(tz->device, ACPI_THERMAL_NOTIFY_HOT,
Len Brown4be44fc2005-08-05 00:44:28 -0400466 tz->trips.hot.flags.enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 /* TBD: Call user-mode "sleep(S4)" function */
469
Patrick Mocheld550d982006-06-27 00:41:40 -0400470 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400473static void acpi_thermal_passive(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400475 int result = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 struct acpi_thermal_passive *passive = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400477 int trend = 0;
478 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 if (!tz || !tz->trips.passive.flags.valid)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400482 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 passive = &(tz->trips.passive);
485
486 /*
487 * Above Trip?
488 * -----------
489 * Calculate the thermal trend (using the passive cooling equation)
490 * and modify the performance limit for all passive cooling devices
491 * accordingly. Note that we assume symmetry.
492 */
493 if (tz->temperature >= passive->temperature) {
Len Brown4be44fc2005-08-05 00:44:28 -0400494 trend =
495 (passive->tc1 * (tz->temperature - tz->last_temperature)) +
496 (passive->tc2 * (tz->temperature - passive->temperature));
497 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
498 "trend[%d]=(tc1[%lu]*(tmp[%lu]-last[%lu]))+(tc2[%lu]*(tmp[%lu]-psv[%lu]))\n",
499 trend, passive->tc1, tz->temperature,
500 tz->last_temperature, passive->tc2,
501 tz->temperature, passive->temperature));
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400502 passive->flags.enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 /* Heating up? */
504 if (trend > 0)
Len Brown4be44fc2005-08-05 00:44:28 -0400505 for (i = 0; i < passive->devices.count; i++)
506 acpi_processor_set_thermal_limit(passive->
507 devices.
508 handles[i],
509 ACPI_PROCESSOR_LIMIT_INCREMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 /* Cooling off? */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400511 else if (trend < 0) {
Len Brown4be44fc2005-08-05 00:44:28 -0400512 for (i = 0; i < passive->devices.count; i++)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400513 /*
514 * assume that we are on highest
515 * freq/lowest thrott and can leave
516 * passive mode, even in error case
517 */
518 if (!acpi_processor_set_thermal_limit
519 (passive->devices.handles[i],
520 ACPI_PROCESSOR_LIMIT_DECREMENT))
521 result = 0;
522 /*
523 * Leave cooling mode, even if the temp might
524 * higher than trip point This is because some
525 * machines might have long thermal polling
526 * frequencies (tsp) defined. We will fall back
527 * into passive mode in next cycle (probably quicker)
528 */
529 if (result) {
530 passive->flags.enabled = 0;
531 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
532 "Disabling passive cooling, still above threshold,"
533 " but we are cooling down\n"));
534 }
535 }
536 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
538
539 /*
540 * Below Trip?
541 * -----------
542 * Implement passive cooling hysteresis to slowly increase performance
543 * and avoid thrashing around the passive trip point. Note that we
544 * assume symmetry.
545 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400546 if (!passive->flags.enabled)
547 return;
548 for (i = 0; i < passive->devices.count; i++)
549 if (!acpi_processor_set_thermal_limit
550 (passive->devices.handles[i],
551 ACPI_PROCESSOR_LIMIT_DECREMENT))
552 result = 0;
553 if (result) {
554 passive->flags.enabled = 0;
555 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
556 "Disabling passive cooling (zone is cool)\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400560static void acpi_thermal_active(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Len Brown4be44fc2005-08-05 00:44:28 -0400562 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 struct acpi_thermal_active *active = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400564 int i = 0;
565 int j = 0;
566 unsigned long maxtemp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 if (!tz)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400570 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Len Brown4be44fc2005-08-05 00:44:28 -0400572 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 active = &(tz->trips.active[i]);
574 if (!active || !active->flags.valid)
575 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (tz->temperature >= active->temperature) {
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400577 /*
578 * Above Threshold?
579 * ----------------
580 * If not already enabled, turn ON all cooling devices
581 * associated with this active threshold.
582 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (active->temperature > maxtemp)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400584 tz->state.active_index = i;
585 maxtemp = active->temperature;
586 if (active->flags.enabled)
587 continue;
588 for (j = 0; j < active->devices.count; j++) {
589 result =
590 acpi_bus_set_power(active->devices.
591 handles[j],
592 ACPI_STATE_D0);
593 if (result) {
Len Browncece9292006-06-26 23:04:31 -0400594 printk(KERN_WARNING PREFIX
595 "Unable to turn cooling device [%p] 'on'\n",
Thomas Renningera6fc6722006-06-26 23:58:43 -0400596 active->devices.
Len Browncece9292006-06-26 23:04:31 -0400597 handles[j]);
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400598 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400600 active->flags.enabled = 1;
601 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
602 "Cooling device [%p] now 'on'\n",
603 active->devices.handles[j]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400605 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400607 if (!active->flags.enabled)
608 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 /*
610 * Below Threshold?
611 * ----------------
612 * Turn OFF all cooling devices associated with this
613 * threshold.
614 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400615 for (j = 0; j < active->devices.count; j++) {
616 result = acpi_bus_set_power(active->devices.handles[j],
617 ACPI_STATE_D3);
618 if (result) {
Len Browncece9292006-06-26 23:04:31 -0400619 printk(KERN_WARNING PREFIX
620 "Unable to turn cooling device [%p] 'off'\n",
621 active->devices.handles[j]);
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400622 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400624 active->flags.enabled = 0;
625 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
626 "Cooling device [%p] now 'off'\n",
627 active->devices.handles[j]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
Len Brown4be44fc2005-08-05 00:44:28 -0400632static void acpi_thermal_check(void *context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Len Brown4be44fc2005-08-05 00:44:28 -0400634static void acpi_thermal_run(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 struct acpi_thermal *tz = (struct acpi_thermal *)data;
637 if (!tz->zombie)
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400638 acpi_os_execute(OSL_GPE_HANDLER, acpi_thermal_check, (void *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
Len Brown4be44fc2005-08-05 00:44:28 -0400641static void acpi_thermal_check(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Len Brown4be44fc2005-08-05 00:44:28 -0400643 int result = 0;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200644 struct acpi_thermal *tz = data;
Len Brown4be44fc2005-08-05 00:44:28 -0400645 unsigned long sleep_time = 0;
646 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 struct acpi_thermal_state state;
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 if (!tz) {
Len Brown64684632006-06-26 23:41:38 -0400651 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400652 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654
655 state = tz->state;
656
657 result = acpi_thermal_get_temperature(tz);
658 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400659 return;
Len Brown4be44fc2005-08-05 00:44:28 -0400660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 memset(&tz->state, 0, sizeof(tz->state));
Len Brown4be44fc2005-08-05 00:44:28 -0400662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 /*
664 * Check Trip Points
665 * -----------------
666 * Compare the current temperature to the trip point values to see
667 * if we've entered one of the thermal policy states. Note that
668 * this function determines when a state is entered, but the
669 * individual policy decides when it is exited (e.g. hysteresis).
670 */
671 if (tz->trips.critical.flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400672 state.critical |=
673 (tz->temperature >= tz->trips.critical.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (tz->trips.hot.flags.valid)
675 state.hot |= (tz->temperature >= tz->trips.hot.temperature);
676 if (tz->trips.passive.flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400677 state.passive |=
678 (tz->temperature >= tz->trips.passive.temperature);
679 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (tz->trips.active[i].flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400681 state.active |=
682 (tz->temperature >=
683 tz->trips.active[i].temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 /*
686 * Invoke Policy
687 * -------------
688 * Separated from the above check to allow individual policy to
689 * determine when to exit a given state.
690 */
691 if (state.critical)
692 acpi_thermal_critical(tz);
693 if (state.hot)
694 acpi_thermal_hot(tz);
695 if (state.passive)
696 acpi_thermal_passive(tz);
697 if (state.active)
698 acpi_thermal_active(tz);
699
700 /*
701 * Calculate State
702 * ---------------
703 * Again, separated from the above two to allow independent policy
704 * decisions.
705 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400706 tz->state.critical = tz->trips.critical.flags.enabled;
707 tz->state.hot = tz->trips.hot.flags.enabled;
708 tz->state.passive = tz->trips.passive.flags.enabled;
709 tz->state.active = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400710 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400711 tz->state.active |= tz->trips.active[i].flags.enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 /*
714 * Calculate Sleep Time
715 * --------------------
716 * If we're in the passive state, use _TSP's value. Otherwise
717 * use the default polling frequency (e.g. _TZP). If no polling
718 * frequency is specified then we'll wait forever (at least until
719 * a thermal event occurs). Note that _TSP and _TZD values are
720 * given in 1/10th seconds (we must covert to milliseconds).
721 */
722 if (tz->state.passive)
723 sleep_time = tz->trips.passive.tsp * 100;
724 else if (tz->polling_frequency > 0)
725 sleep_time = tz->polling_frequency * 100;
726
Len Brown4be44fc2005-08-05 00:44:28 -0400727 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: temperature[%lu] sleep[%lu]\n",
728 tz->name, tz->temperature, sleep_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 /*
731 * Schedule Next Poll
732 * ------------------
733 */
734 if (!sleep_time) {
735 if (timer_pending(&(tz->timer)))
736 del_timer(&(tz->timer));
Len Brown4be44fc2005-08-05 00:44:28 -0400737 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (timer_pending(&(tz->timer)))
Andrew Morton94e22e12007-04-23 14:41:13 -0700739 mod_timer(&(tz->timer),
740 jiffies + (HZ * sleep_time) / 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400742 tz->timer.data = (unsigned long)tz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 tz->timer.function = acpi_thermal_run;
744 tz->timer.expires = jiffies + (HZ * sleep_time) / 1000;
745 add_timer(&(tz->timer));
746 }
747 }
748
Patrick Mocheld550d982006-06-27 00:41:40 -0400749 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752/* --------------------------------------------------------------------------
753 FS Interface (/proc)
754 -------------------------------------------------------------------------- */
755
Len Brown4be44fc2005-08-05 00:44:28 -0400756static struct proc_dir_entry *acpi_thermal_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
759{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200760 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 if (!tz)
764 goto end;
765
766 seq_puts(seq, "state: ");
767
Len Brown4be44fc2005-08-05 00:44:28 -0400768 if (!tz->state.critical && !tz->state.hot && !tz->state.passive
769 && !tz->state.active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 seq_puts(seq, "ok\n");
771 else {
772 if (tz->state.critical)
773 seq_puts(seq, "critical ");
774 if (tz->state.hot)
775 seq_puts(seq, "hot ");
776 if (tz->state.passive)
777 seq_puts(seq, "passive ");
778 if (tz->state.active)
779 seq_printf(seq, "active[%d]", tz->state.active_index);
780 seq_puts(seq, "\n");
781 }
782
Len Brown4be44fc2005-08-05 00:44:28 -0400783 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400784 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
787static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
788{
789 return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
790}
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
793{
Len Brown4be44fc2005-08-05 00:44:28 -0400794 int result = 0;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200795 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 if (!tz)
799 goto end;
800
801 result = acpi_thermal_get_temperature(tz);
802 if (result)
803 goto end;
804
Len Brown4be44fc2005-08-05 00:44:28 -0400805 seq_printf(seq, "temperature: %ld C\n",
806 KELVIN_TO_CELSIUS(tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Len Brown4be44fc2005-08-05 00:44:28 -0400808 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400809 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
812static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
813{
814 return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
815}
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
818{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200819 struct acpi_thermal *tz = seq->private;
Thomas Renninger68ccfaa2006-11-19 23:01:40 +0100820 struct acpi_device *device;
Thomas Renningere7c746e2007-06-18 00:40:51 -0400821 acpi_status status;
822
Len Brown4be44fc2005-08-05 00:44:28 -0400823 int i = 0;
824 int j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 if (!tz)
828 goto end;
829
830 if (tz->trips.critical.flags.valid)
831 seq_printf(seq, "critical (S5): %ld C\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400832 KELVIN_TO_CELSIUS(tz->trips.critical.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 if (tz->trips.hot.flags.valid)
835 seq_printf(seq, "hot (S4): %ld C\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400836 KELVIN_TO_CELSIUS(tz->trips.hot.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 if (tz->trips.passive.flags.valid) {
Len Brown4be44fc2005-08-05 00:44:28 -0400839 seq_printf(seq,
840 "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
841 KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
842 tz->trips.passive.tc1, tz->trips.passive.tc2,
843 tz->trips.passive.tsp);
844 for (j = 0; j < tz->trips.passive.devices.count; j++) {
Thomas Renningere7c746e2007-06-18 00:40:51 -0400845 status = acpi_bus_get_device(tz->trips.passive.devices.
846 handles[j], &device);
847 seq_printf(seq, "%4.4s ", status ? "" :
848 acpi_device_bid(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850 seq_puts(seq, "\n");
851 }
852
853 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
854 if (!(tz->trips.active[i].flags.valid))
855 break;
856 seq_printf(seq, "active[%d]: %ld C: devices=",
Len Brown4be44fc2005-08-05 00:44:28 -0400857 i,
858 KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
Thomas Renninger68ccfaa2006-11-19 23:01:40 +0100859 for (j = 0; j < tz->trips.active[i].devices.count; j++){
Thomas Renningere7c746e2007-06-18 00:40:51 -0400860 status = acpi_bus_get_device(tz->trips.active[i].
861 devices.handles[j],
862 &device);
863 seq_printf(seq, "%4.4s ", status ? "" :
864 acpi_device_bid(device));
Thomas Renninger68ccfaa2006-11-19 23:01:40 +0100865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 seq_puts(seq, "\n");
867 }
868
Len Brown4be44fc2005-08-05 00:44:28 -0400869 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400870 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
872
873static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
874{
875 return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
876}
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
879{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200880 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 if (!tz)
884 goto end;
885
Len Browneaca2d32007-04-30 23:27:43 -0400886 if (!tz->flags.cooling_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 seq_puts(seq, "<setting not supported>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 else
Len Browneaca2d32007-04-30 23:27:43 -0400889 seq_puts(seq, "0 - Active; 1 - Passive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Len Brown4be44fc2005-08-05 00:44:28 -0400891 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400892 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894
895static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
896{
897 return single_open(file, acpi_thermal_cooling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -0400898 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
901static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -0400902acpi_thermal_write_cooling_mode(struct file *file,
903 const char __user * buffer,
904 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200906 struct seq_file *m = file->private_data;
907 struct acpi_thermal *tz = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400908 int result = 0;
909 char mode_string[12] = { '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 if (!tz || (count > sizeof(mode_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400913 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 if (!tz->flags.cooling_mode)
Patrick Mocheld550d982006-06-27 00:41:40 -0400916 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 if (copy_from_user(mode_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -0400919 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -0400920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 mode_string[count] = '\0';
Len Brown4be44fc2005-08-05 00:44:28 -0400922
923 result = acpi_thermal_set_cooling_mode(tz,
924 simple_strtoul(mode_string, NULL,
925 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400927 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 acpi_thermal_check(tz);
930
Patrick Mocheld550d982006-06-27 00:41:40 -0400931 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
935{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200936 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 if (!tz)
940 goto end;
941
942 if (!tz->polling_frequency) {
943 seq_puts(seq, "<polling disabled>\n");
944 goto end;
945 }
946
947 seq_printf(seq, "polling frequency: %lu seconds\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400948 (tz->polling_frequency / 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Len Brown4be44fc2005-08-05 00:44:28 -0400950 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400951 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
954static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
955{
956 return single_open(file, acpi_thermal_polling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -0400957 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
960static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -0400961acpi_thermal_write_polling(struct file *file,
962 const char __user * buffer,
963 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200965 struct seq_file *m = file->private_data;
966 struct acpi_thermal *tz = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400967 int result = 0;
968 char polling_string[12] = { '\0' };
969 int seconds = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 if (!tz || (count > sizeof(polling_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400973 return -EINVAL;
Len Brown4be44fc2005-08-05 00:44:28 -0400974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (copy_from_user(polling_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -0400976 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -0400977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 polling_string[count] = '\0';
979
980 seconds = simple_strtoul(polling_string, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -0400981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 result = acpi_thermal_set_polling(tz, seconds);
983 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400984 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 acpi_thermal_check(tz);
987
Patrick Mocheld550d982006-06-27 00:41:40 -0400988 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
Len Brown4be44fc2005-08-05 00:44:28 -0400991static int acpi_thermal_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Len Brown4be44fc2005-08-05 00:44:28 -0400993 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 if (!acpi_device_dir(device)) {
997 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400998 acpi_thermal_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001000 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 acpi_device_dir(device)->owner = THIS_MODULE;
1002 }
1003
1004 /* 'state' [R] */
1005 entry = create_proc_entry(ACPI_THERMAL_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -04001006 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001008 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 else {
1010 entry->proc_fops = &acpi_thermal_state_fops;
1011 entry->data = acpi_driver_data(device);
1012 entry->owner = THIS_MODULE;
1013 }
1014
1015 /* 'temperature' [R] */
1016 entry = create_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
Len Brown4be44fc2005-08-05 00:44:28 -04001017 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001019 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 else {
1021 entry->proc_fops = &acpi_thermal_temp_fops;
1022 entry->data = acpi_driver_data(device);
1023 entry->owner = THIS_MODULE;
1024 }
1025
1026 /* 'trip_points' [R/W] */
1027 entry = create_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
Len Brown4be44fc2005-08-05 00:44:28 -04001028 S_IFREG | S_IRUGO | S_IWUSR,
1029 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001031 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 else {
1033 entry->proc_fops = &acpi_thermal_trip_fops;
1034 entry->data = acpi_driver_data(device);
1035 entry->owner = THIS_MODULE;
1036 }
1037
1038 /* 'cooling_mode' [R/W] */
1039 entry = create_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
Len Brown4be44fc2005-08-05 00:44:28 -04001040 S_IFREG | S_IRUGO | S_IWUSR,
1041 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001043 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 else {
1045 entry->proc_fops = &acpi_thermal_cooling_fops;
1046 entry->data = acpi_driver_data(device);
1047 entry->owner = THIS_MODULE;
1048 }
1049
1050 /* 'polling_frequency' [R/W] */
1051 entry = create_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
Len Brown4be44fc2005-08-05 00:44:28 -04001052 S_IFREG | S_IRUGO | S_IWUSR,
1053 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001055 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 else {
1057 entry->proc_fops = &acpi_thermal_polling_fops;
1058 entry->data = acpi_driver_data(device);
1059 entry->owner = THIS_MODULE;
1060 }
1061
Patrick Mocheld550d982006-06-27 00:41:40 -04001062 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
Len Brown4be44fc2005-08-05 00:44:28 -04001065static int acpi_thermal_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 if (acpi_device_dir(device)) {
1069 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1070 acpi_device_dir(device));
1071 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1072 acpi_device_dir(device));
1073 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1074 acpi_device_dir(device));
1075 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1076 acpi_device_dir(device));
1077 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1078 acpi_device_dir(device));
1079 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1080 acpi_device_dir(device) = NULL;
1081 }
1082
Patrick Mocheld550d982006-06-27 00:41:40 -04001083 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084}
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086/* --------------------------------------------------------------------------
1087 Driver Interface
1088 -------------------------------------------------------------------------- */
1089
Len Brown4be44fc2005-08-05 00:44:28 -04001090static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001092 struct acpi_thermal *tz = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001093 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001097 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Patrick Mochel8348e1b2006-05-19 16:54:40 -04001099 device = tz->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 switch (event) {
1102 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1103 acpi_thermal_check(tz);
1104 break;
1105 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
1106 acpi_thermal_get_trip_points(tz);
1107 acpi_thermal_check(tz);
1108 acpi_bus_generate_event(device, event, 0);
1109 break;
1110 case ACPI_THERMAL_NOTIFY_DEVICES:
1111 if (tz->flags.devices)
1112 acpi_thermal_get_devices(tz);
1113 acpi_bus_generate_event(device, event, 0);
1114 break;
1115 default:
1116 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001117 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 break;
1119 }
1120
Patrick Mocheld550d982006-06-27 00:41:40 -04001121 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
1123
Len Brown4be44fc2005-08-05 00:44:28 -04001124static int acpi_thermal_get_info(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
Len Brown4be44fc2005-08-05 00:44:28 -04001126 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001130 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 /* Get temperature [_TMP] (required) */
1133 result = acpi_thermal_get_temperature(tz);
1134 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001135 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 /* Get trip points [_CRT, _PSV, etc.] (required) */
1138 result = acpi_thermal_get_trip_points(tz);
1139 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001140 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 /* Set the cooling mode [_SCP] to active cooling (default) */
1143 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
Len Brown4be44fc2005-08-05 00:44:28 -04001144 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 tz->flags.cooling_mode = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 /* Get default polling frequency [_TZP] (optional) */
1148 if (tzp)
1149 tz->polling_frequency = tzp;
1150 else
1151 acpi_thermal_get_polling_frequency(tz);
1152
1153 /* Get devices in this thermal zone [_TZD] (optional) */
1154 result = acpi_thermal_get_devices(tz);
1155 if (!result)
1156 tz->flags.devices = 1;
1157
Patrick Mocheld550d982006-06-27 00:41:40 -04001158 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
1160
Len Brown4be44fc2005-08-05 00:44:28 -04001161static int acpi_thermal_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
Len Brown4be44fc2005-08-05 00:44:28 -04001163 int result = 0;
1164 acpi_status status = AE_OK;
1165 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001169 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Burman Yan36bcbec2006-12-19 12:56:11 -08001171 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001173 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Patrick Mochel8348e1b2006-05-19 16:54:40 -04001175 tz->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 strcpy(tz->name, device->pnp.bus_id);
1177 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1178 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1179 acpi_driver_data(device) = tz;
1180
1181 result = acpi_thermal_get_info(tz);
1182 if (result)
1183 goto end;
1184
1185 result = acpi_thermal_add_fs(device);
1186 if (result)
Vasily Averin09047e72006-04-27 05:25:00 -04001187 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 init_timer(&tz->timer);
1190
1191 acpi_thermal_check(tz);
1192
Patrick Mochel38ba7c92006-05-19 16:54:48 -04001193 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001194 ACPI_DEVICE_NOTIFY,
1195 acpi_thermal_notify, tz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 result = -ENODEV;
1198 goto end;
1199 }
1200
1201 printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001202 acpi_device_name(device), acpi_device_bid(device),
1203 KELVIN_TO_CELSIUS(tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Len Brown4be44fc2005-08-05 00:44:28 -04001205 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 if (result) {
1207 acpi_thermal_remove_fs(device);
1208 kfree(tz);
1209 }
1210
Patrick Mocheld550d982006-06-27 00:41:40 -04001211 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
Len Brown4be44fc2005-08-05 00:44:28 -04001214static int acpi_thermal_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Len Brown4be44fc2005-08-05 00:44:28 -04001216 acpi_status status = AE_OK;
1217 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001221 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001223 tz = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 /* avoid timer adding new defer task */
1226 tz->zombie = 1;
1227 /* wait for running timer (on other CPUs) finish */
1228 del_timer_sync(&(tz->timer));
1229 /* synchronize deferred task */
1230 acpi_os_wait_events_complete(NULL);
1231 /* deferred task may reinsert timer */
1232 del_timer_sync(&(tz->timer));
1233
Patrick Mochel38ba7c92006-05-19 16:54:48 -04001234 status = acpi_remove_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001235 ACPI_DEVICE_NOTIFY,
1236 acpi_thermal_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 /* Terminate policy */
Len Brown4be44fc2005-08-05 00:44:28 -04001239 if (tz->trips.passive.flags.valid && tz->trips.passive.flags.enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 tz->trips.passive.flags.enabled = 0;
1241 acpi_thermal_passive(tz);
1242 }
1243 if (tz->trips.active[0].flags.valid
Len Brown4be44fc2005-08-05 00:44:28 -04001244 && tz->trips.active[0].flags.enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 tz->trips.active[0].flags.enabled = 0;
1246 acpi_thermal_active(tz);
1247 }
1248
1249 acpi_thermal_remove_fs(device);
1250
1251 kfree(tz);
Patrick Mocheld550d982006-06-27 00:41:40 -04001252 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001255static int acpi_thermal_resume(struct acpi_device *device)
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001256{
1257 struct acpi_thermal *tz = NULL;
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001258 int i, j, power_state, result;
1259
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001260
1261 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001262 return -EINVAL;
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001263
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001264 tz = acpi_driver_data(device);
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001265
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001266 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001267 if (!(&tz->trips.active[i]))
1268 break;
1269 if (!tz->trips.active[i].flags.valid)
1270 break;
1271 tz->trips.active[i].flags.enabled = 1;
1272 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1273 result = acpi_bus_get_power(tz->trips.active[i].devices.
1274 handles[j], &power_state);
1275 if (result || (power_state != ACPI_STATE_D0)) {
1276 tz->trips.active[i].flags.enabled = 0;
1277 break;
1278 }
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001279 }
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001280 tz->state.active |= tz->trips.active[i].flags.enabled;
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001281 }
1282
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001283 acpi_thermal_check(tz);
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001284
1285 return AE_OK;
1286}
1287
Len Brown4be44fc2005-08-05 00:44:28 -04001288static int __init acpi_thermal_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
Len Brown4be44fc2005-08-05 00:44:28 -04001290 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Len Brown72b33ef2007-08-12 00:12:17 -04001292 if (off) {
1293 printk(KERN_NOTICE "ACPI: thermal control disabled\n");
1294 return -ENODEV;
1295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1297 if (!acpi_thermal_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001298 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 acpi_thermal_dir->owner = THIS_MODULE;
1300
1301 result = acpi_bus_register_driver(&acpi_thermal_driver);
1302 if (result < 0) {
1303 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04001304 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
1306
Patrick Mocheld550d982006-06-27 00:41:40 -04001307 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
1309
Len Brown4be44fc2005-08-05 00:44:28 -04001310static void __exit acpi_thermal_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 acpi_bus_unregister_driver(&acpi_thermal_driver);
1314
1315 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1316
Patrick Mocheld550d982006-06-27 00:41:40 -04001317 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318}
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320module_init(acpi_thermal_init);
1321module_exit(acpi_thermal_exit);