blob: 56358e149b2bd1b59159ad8cfa5c4a561d31e479 [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>
39#include <linux/sched.h>
40#include <linux/kmod.h>
41#include <linux/seq_file.h>
42#include <asm/uaccess.h>
43
44#include <acpi/acpi_bus.h>
45#include <acpi/acpi_drivers.h>
46
47#define ACPI_THERMAL_COMPONENT 0x04000000
48#define ACPI_THERMAL_CLASS "thermal_zone"
49#define ACPI_THERMAL_DRIVER_NAME "ACPI Thermal Zone Driver"
50#define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
51#define ACPI_THERMAL_FILE_STATE "state"
52#define ACPI_THERMAL_FILE_TEMPERATURE "temperature"
53#define ACPI_THERMAL_FILE_TRIP_POINTS "trip_points"
54#define ACPI_THERMAL_FILE_COOLING_MODE "cooling_mode"
55#define ACPI_THERMAL_FILE_POLLING_FREQ "polling_frequency"
56#define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
57#define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
58#define ACPI_THERMAL_NOTIFY_DEVICES 0x82
59#define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
60#define ACPI_THERMAL_NOTIFY_HOT 0xF1
61#define ACPI_THERMAL_MODE_ACTIVE 0x00
62#define ACPI_THERMAL_MODE_PASSIVE 0x01
63#define ACPI_THERMAL_MODE_CRITICAL 0xff
64#define ACPI_THERMAL_PATH_POWEROFF "/sbin/poweroff"
65
66#define ACPI_THERMAL_MAX_ACTIVE 10
67#define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
68
69#define KELVIN_TO_CELSIUS(t) (long)(((long)t-2732>=0) ? ((long)t-2732+5)/10 : ((long)t-2732-5)/10)
70#define CELSIUS_TO_KELVIN(t) ((t+273)*10)
71
72#define _COMPONENT ACPI_THERMAL_COMPONENT
Len Brown4be44fc2005-08-05 00:44:28 -040073ACPI_MODULE_NAME("acpi_thermal")
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Thomas Renninger1cbf4c52004-09-16 11:07:00 -040075MODULE_AUTHOR("Paul Diefenbaugh");
Linus Torvalds1da177e2005-04-16 15:20:36 -070076MODULE_DESCRIPTION(ACPI_THERMAL_DRIVER_NAME);
77MODULE_LICENSE("GPL");
78
79static int tzp;
80module_param(tzp, int, 0);
81MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.\n");
82
Len Brown4be44fc2005-08-05 00:44:28 -040083static int acpi_thermal_add(struct acpi_device *device);
84static int acpi_thermal_remove(struct acpi_device *device, int type);
Konstantin Karasyov74ce14682006-05-08 08:32:00 -040085static int acpi_thermal_resume(struct acpi_device *device, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
87static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
88static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -040089static ssize_t acpi_thermal_write_trip_points(struct file *,
90 const char __user *, size_t,
91 loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -040093static ssize_t acpi_thermal_write_cooling_mode(struct file *,
94 const char __user *, size_t,
95 loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -040097static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
98 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100static struct acpi_driver acpi_thermal_driver = {
Len Brown4be44fc2005-08-05 00:44:28 -0400101 .name = ACPI_THERMAL_DRIVER_NAME,
102 .class = ACPI_THERMAL_CLASS,
103 .ids = ACPI_THERMAL_HID,
104 .ops = {
105 .add = acpi_thermal_add,
106 .remove = acpi_thermal_remove,
Konstantin Karasyov74ce14682006-05-08 08:32:00 -0400107 .resume = acpi_thermal_resume,
Len Brown4be44fc2005-08-05 00:44:28 -0400108 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109};
110
111struct acpi_thermal_state {
Len Brown4be44fc2005-08-05 00:44:28 -0400112 u8 critical:1;
113 u8 hot:1;
114 u8 passive:1;
115 u8 active:1;
116 u8 reserved:4;
117 int active_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118};
119
120struct acpi_thermal_state_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400121 u8 valid:1;
122 u8 enabled:1;
123 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
126struct acpi_thermal_critical {
127 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400128 unsigned long temperature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129};
130
131struct acpi_thermal_hot {
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_passive {
137 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400138 unsigned long temperature;
139 unsigned long tc1;
140 unsigned long tc2;
141 unsigned long tsp;
142 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143};
144
145struct acpi_thermal_active {
146 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400147 unsigned long temperature;
148 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149};
150
151struct acpi_thermal_trips {
152 struct acpi_thermal_critical critical;
Len Brown4be44fc2005-08-05 00:44:28 -0400153 struct acpi_thermal_hot hot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 struct acpi_thermal_passive passive;
155 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
156};
157
158struct acpi_thermal_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400159 u8 cooling_mode:1; /* _SCP */
160 u8 devices:1; /* _TZD */
161 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162};
163
164struct acpi_thermal {
Len Brown4be44fc2005-08-05 00:44:28 -0400165 acpi_handle handle;
Patrick Mochel8348e1b2006-05-19 16:54:40 -0400166 struct acpi_device * device;
Len Brown4be44fc2005-08-05 00:44:28 -0400167 acpi_bus_id name;
168 unsigned long temperature;
169 unsigned long last_temperature;
170 unsigned long polling_frequency;
171 u8 cooling_mode;
172 volatile u8 zombie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 struct acpi_thermal_flags flags;
174 struct acpi_thermal_state state;
175 struct acpi_thermal_trips trips;
Len Brown4be44fc2005-08-05 00:44:28 -0400176 struct acpi_handle_list devices;
177 struct timer_list timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178};
179
180static struct file_operations acpi_thermal_state_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400181 .open = acpi_thermal_state_open_fs,
182 .read = seq_read,
183 .llseek = seq_lseek,
184 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185};
186
187static struct file_operations acpi_thermal_temp_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400188 .open = acpi_thermal_temp_open_fs,
189 .read = seq_read,
190 .llseek = seq_lseek,
191 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192};
193
194static struct file_operations acpi_thermal_trip_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400195 .open = acpi_thermal_trip_open_fs,
196 .read = seq_read,
197 .write = acpi_thermal_write_trip_points,
198 .llseek = seq_lseek,
199 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200};
201
202static struct file_operations acpi_thermal_cooling_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400203 .open = acpi_thermal_cooling_open_fs,
204 .read = seq_read,
205 .write = acpi_thermal_write_cooling_mode,
206 .llseek = seq_lseek,
207 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208};
209
210static struct file_operations acpi_thermal_polling_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400211 .open = acpi_thermal_polling_open_fs,
212 .read = seq_read,
213 .write = acpi_thermal_write_polling,
214 .llseek = seq_lseek,
215 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216};
217
218/* --------------------------------------------------------------------------
219 Thermal Zone Management
220 -------------------------------------------------------------------------- */
221
Len Brown4be44fc2005-08-05 00:44:28 -0400222static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Len Brown4be44fc2005-08-05 00:44:28 -0400224 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400228 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 tz->last_temperature = tz->temperature;
231
Len Brown4be44fc2005-08-05 00:44:28 -0400232 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400233 acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tz->temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400235 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Len Brown4be44fc2005-08-05 00:44:28 -0400237 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
238 tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Patrick Mocheld550d982006-06-27 00:41:40 -0400240 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
Len Brown4be44fc2005-08-05 00:44:28 -0400243static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Len Brown4be44fc2005-08-05 00:44:28 -0400245 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400249 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Len Brown4be44fc2005-08-05 00:44:28 -0400251 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400252 acpi_evaluate_integer(tz->device->handle, "_TZP", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400253 &tz->polling_frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400255 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Len Brown4be44fc2005-08-05 00:44:28 -0400257 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
258 tz->polling_frequency));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Patrick Mocheld550d982006-06-27 00:41:40 -0400260 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Len Brown4be44fc2005-08-05 00:44:28 -0400263static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400267 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 tz->polling_frequency = seconds * 10; /* Convert value to deci-seconds */
270
Len Brown4be44fc2005-08-05 00:44:28 -0400271 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
272 "Polling frequency set to %lu seconds\n",
273 tz->polling_frequency));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Patrick Mocheld550d982006-06-27 00:41:40 -0400275 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
Len Brown4be44fc2005-08-05 00:44:28 -0400278static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Len Brown4be44fc2005-08-05 00:44:28 -0400280 acpi_status status = AE_OK;
281 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
282 struct acpi_object_list arg_list = { 1, &arg0 };
283 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400287 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400289 status = acpi_get_handle(tz->device->handle, "_SCP", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (ACPI_FAILURE(status)) {
291 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400292 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294
295 arg0.integer.value = mode;
296
297 status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
298 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400299 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 tz->cooling_mode = mode;
302
Len Brown4be44fc2005-08-05 00:44:28 -0400303 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Cooling mode [%s]\n",
304 mode ? "passive" : "active"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Patrick Mocheld550d982006-06-27 00:41:40 -0400306 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
Len Brown4be44fc2005-08-05 00:44:28 -0400309static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Len Brown4be44fc2005-08-05 00:44:28 -0400311 acpi_status status = AE_OK;
312 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400316 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 /* Critical Shutdown (required) */
319
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400320 status = acpi_evaluate_integer(tz->device->handle, "_CRT", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400321 &tz->trips.critical.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (ACPI_FAILURE(status)) {
323 tz->trips.critical.flags.valid = 0;
Thomas Renningera6fc6722006-06-26 23:58:43 -0400324 ACPI_EXCEPTION((AE_INFO, status, "No critical threshold"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400325 return -ENODEV;
Len Brown4be44fc2005-08-05 00:44:28 -0400326 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 tz->trips.critical.flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400328 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
329 "Found critical threshold [%lu]\n",
330 tz->trips.critical.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
332
333 /* Critical Sleep (optional) */
334
Len Brown4be44fc2005-08-05 00:44:28 -0400335 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400336 acpi_evaluate_integer(tz->device->handle, "_HOT", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400337 &tz->trips.hot.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (ACPI_FAILURE(status)) {
339 tz->trips.hot.flags.valid = 0;
340 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No hot threshold\n"));
Len Brown4be44fc2005-08-05 00:44:28 -0400341 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 tz->trips.hot.flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400343 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found hot threshold [%lu]\n",
344 tz->trips.hot.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346
347 /* Passive: Processors (optional) */
348
Len Brown4be44fc2005-08-05 00:44:28 -0400349 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400350 acpi_evaluate_integer(tz->device->handle, "_PSV", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400351 &tz->trips.passive.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (ACPI_FAILURE(status)) {
353 tz->trips.passive.flags.valid = 0;
354 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No passive threshold\n"));
Len Brown4be44fc2005-08-05 00:44:28 -0400355 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 tz->trips.passive.flags.valid = 1;
357
Len Brown4be44fc2005-08-05 00:44:28 -0400358 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400359 acpi_evaluate_integer(tz->device->handle, "_TC1", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400360 &tz->trips.passive.tc1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (ACPI_FAILURE(status))
362 tz->trips.passive.flags.valid = 0;
363
Len Brown4be44fc2005-08-05 00:44:28 -0400364 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400365 acpi_evaluate_integer(tz->device->handle, "_TC2", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400366 &tz->trips.passive.tc2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (ACPI_FAILURE(status))
368 tz->trips.passive.flags.valid = 0;
369
Len Brown4be44fc2005-08-05 00:44:28 -0400370 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400371 acpi_evaluate_integer(tz->device->handle, "_TSP", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400372 &tz->trips.passive.tsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (ACPI_FAILURE(status))
374 tz->trips.passive.flags.valid = 0;
375
Len Brown4be44fc2005-08-05 00:44:28 -0400376 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400377 acpi_evaluate_reference(tz->device->handle, "_PSL", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400378 &tz->trips.passive.devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (ACPI_FAILURE(status))
380 tz->trips.passive.flags.valid = 0;
381
382 if (!tz->trips.passive.flags.valid)
Len Browncece9292006-06-26 23:04:31 -0400383 printk(KERN_WARNING PREFIX "Invalid passive threshold\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 else
Len Brown4be44fc2005-08-05 00:44:28 -0400385 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
386 "Found passive threshold [%lu]\n",
387 tz->trips.passive.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389
390 /* Active: Fans, etc. (optional) */
391
Len Brown4be44fc2005-08-05 00:44:28 -0400392 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Len Brown4be44fc2005-08-05 00:44:28 -0400394 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Len Brown4be44fc2005-08-05 00:44:28 -0400396 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400397 acpi_evaluate_integer(tz->device->handle, name, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400398 &tz->trips.active[i].temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (ACPI_FAILURE(status))
400 break;
401
402 name[2] = 'L';
Len Brown4be44fc2005-08-05 00:44:28 -0400403 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400404 acpi_evaluate_reference(tz->device->handle, name, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400405 &tz->trips.active[i].devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (ACPI_SUCCESS(status)) {
407 tz->trips.active[i].flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400408 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
409 "Found active threshold [%d]:[%lu]\n",
410 i, tz->trips.active[i].temperature));
411 } else
Thomas Renningera6fc6722006-06-26 23:58:43 -0400412 ACPI_EXCEPTION((AE_INFO, status,
413 "Invalid active threshold [%d]", i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415
Patrick Mocheld550d982006-06-27 00:41:40 -0400416 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
Len Brown4be44fc2005-08-05 00:44:28 -0400419static int acpi_thermal_get_devices(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Len Brown4be44fc2005-08-05 00:44:28 -0400421 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400425 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Len Brown4be44fc2005-08-05 00:44:28 -0400427 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400428 acpi_evaluate_reference(tz->device->handle, "_TZD", NULL, &tz->devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400430 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Patrick Mocheld550d982006-06-27 00:41:40 -0400432 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Len Brown4be44fc2005-08-05 00:44:28 -0400435static int acpi_thermal_call_usermode(char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Len Brown4be44fc2005-08-05 00:44:28 -0400437 char *argv[2] = { NULL, NULL };
438 char *envp[3] = { NULL, NULL, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 if (!path)
Patrick Mocheld550d982006-06-27 00:41:40 -0400442 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 argv[0] = path;
445
446 /* minimal command environment */
447 envp[0] = "HOME=/";
448 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
Len Brown4be44fc2005-08-05 00:44:28 -0400449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 call_usermodehelper(argv[0], argv, envp, 0);
451
Patrick Mocheld550d982006-06-27 00:41:40 -0400452 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
Len Brown4be44fc2005-08-05 00:44:28 -0400455static int acpi_thermal_critical(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (!tz || !tz->trips.critical.flags.valid)
Patrick Mocheld550d982006-06-27 00:41:40 -0400458 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 if (tz->temperature >= tz->trips.critical.temperature) {
Len Browncece9292006-06-26 23:04:31 -0400461 printk(KERN_WARNING PREFIX "Critical trip point\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 tz->trips.critical.flags.enabled = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400463 } else if (tz->trips.critical.flags.enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 tz->trips.critical.flags.enabled = 0;
465
Len Brown4be44fc2005-08-05 00:44:28 -0400466 printk(KERN_EMERG
467 "Critical temperature reached (%ld C), shutting down.\n",
468 KELVIN_TO_CELSIUS(tz->temperature));
Patrick Mochel8348e1b2006-05-19 16:54:40 -0400469 acpi_bus_generate_event(tz->device, ACPI_THERMAL_NOTIFY_CRITICAL,
Len Brown4be44fc2005-08-05 00:44:28 -0400470 tz->trips.critical.flags.enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 acpi_thermal_call_usermode(ACPI_THERMAL_PATH_POWEROFF);
473
Patrick Mocheld550d982006-06-27 00:41:40 -0400474 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
Len Brown4be44fc2005-08-05 00:44:28 -0400477static int acpi_thermal_hot(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (!tz || !tz->trips.hot.flags.valid)
Patrick Mocheld550d982006-06-27 00:41:40 -0400480 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 if (tz->temperature >= tz->trips.hot.temperature) {
Len Browncece9292006-06-26 23:04:31 -0400483 printk(KERN_WARNING PREFIX "Hot trip point\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 tz->trips.hot.flags.enabled = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400485 } else if (tz->trips.hot.flags.enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 tz->trips.hot.flags.enabled = 0;
487
Patrick Mochel8348e1b2006-05-19 16:54:40 -0400488 acpi_bus_generate_event(tz->device, ACPI_THERMAL_NOTIFY_HOT,
Len Brown4be44fc2005-08-05 00:44:28 -0400489 tz->trips.hot.flags.enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 /* TBD: Call user-mode "sleep(S4)" function */
492
Patrick Mocheld550d982006-06-27 00:41:40 -0400493 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400496static void acpi_thermal_passive(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400498 int result = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 struct acpi_thermal_passive *passive = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400500 int trend = 0;
501 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 if (!tz || !tz->trips.passive.flags.valid)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400505 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 passive = &(tz->trips.passive);
508
509 /*
510 * Above Trip?
511 * -----------
512 * Calculate the thermal trend (using the passive cooling equation)
513 * and modify the performance limit for all passive cooling devices
514 * accordingly. Note that we assume symmetry.
515 */
516 if (tz->temperature >= passive->temperature) {
Len Brown4be44fc2005-08-05 00:44:28 -0400517 trend =
518 (passive->tc1 * (tz->temperature - tz->last_temperature)) +
519 (passive->tc2 * (tz->temperature - passive->temperature));
520 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
521 "trend[%d]=(tc1[%lu]*(tmp[%lu]-last[%lu]))+(tc2[%lu]*(tmp[%lu]-psv[%lu]))\n",
522 trend, passive->tc1, tz->temperature,
523 tz->last_temperature, passive->tc2,
524 tz->temperature, passive->temperature));
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400525 passive->flags.enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /* Heating up? */
527 if (trend > 0)
Len Brown4be44fc2005-08-05 00:44:28 -0400528 for (i = 0; i < passive->devices.count; i++)
529 acpi_processor_set_thermal_limit(passive->
530 devices.
531 handles[i],
532 ACPI_PROCESSOR_LIMIT_INCREMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 /* Cooling off? */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400534 else if (trend < 0) {
Len Brown4be44fc2005-08-05 00:44:28 -0400535 for (i = 0; i < passive->devices.count; i++)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400536 /*
537 * assume that we are on highest
538 * freq/lowest thrott and can leave
539 * passive mode, even in error case
540 */
541 if (!acpi_processor_set_thermal_limit
542 (passive->devices.handles[i],
543 ACPI_PROCESSOR_LIMIT_DECREMENT))
544 result = 0;
545 /*
546 * Leave cooling mode, even if the temp might
547 * higher than trip point This is because some
548 * machines might have long thermal polling
549 * frequencies (tsp) defined. We will fall back
550 * into passive mode in next cycle (probably quicker)
551 */
552 if (result) {
553 passive->flags.enabled = 0;
554 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
555 "Disabling passive cooling, still above threshold,"
556 " but we are cooling down\n"));
557 }
558 }
559 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561
562 /*
563 * Below Trip?
564 * -----------
565 * Implement passive cooling hysteresis to slowly increase performance
566 * and avoid thrashing around the passive trip point. Note that we
567 * assume symmetry.
568 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400569 if (!passive->flags.enabled)
570 return;
571 for (i = 0; i < passive->devices.count; i++)
572 if (!acpi_processor_set_thermal_limit
573 (passive->devices.handles[i],
574 ACPI_PROCESSOR_LIMIT_DECREMENT))
575 result = 0;
576 if (result) {
577 passive->flags.enabled = 0;
578 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
579 "Disabling passive cooling (zone is cool)\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400583static void acpi_thermal_active(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Len Brown4be44fc2005-08-05 00:44:28 -0400585 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 struct acpi_thermal_active *active = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400587 int i = 0;
588 int j = 0;
589 unsigned long maxtemp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 if (!tz)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400593 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Len Brown4be44fc2005-08-05 00:44:28 -0400595 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 active = &(tz->trips.active[i]);
597 if (!active || !active->flags.valid)
598 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (tz->temperature >= active->temperature) {
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400600 /*
601 * Above Threshold?
602 * ----------------
603 * If not already enabled, turn ON all cooling devices
604 * associated with this active threshold.
605 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (active->temperature > maxtemp)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400607 tz->state.active_index = i;
608 maxtemp = active->temperature;
609 if (active->flags.enabled)
610 continue;
611 for (j = 0; j < active->devices.count; j++) {
612 result =
613 acpi_bus_set_power(active->devices.
614 handles[j],
615 ACPI_STATE_D0);
616 if (result) {
Len Browncece9292006-06-26 23:04:31 -0400617 printk(KERN_WARNING PREFIX
618 "Unable to turn cooling device [%p] 'on'\n",
Thomas Renningera6fc6722006-06-26 23:58:43 -0400619 active->devices.
Len Browncece9292006-06-26 23:04:31 -0400620 handles[j]);
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400621 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400623 active->flags.enabled = 1;
624 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
625 "Cooling device [%p] now 'on'\n",
626 active->devices.handles[j]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400628 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400630 if (!active->flags.enabled)
631 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /*
633 * Below Threshold?
634 * ----------------
635 * Turn OFF all cooling devices associated with this
636 * threshold.
637 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400638 for (j = 0; j < active->devices.count; j++) {
639 result = acpi_bus_set_power(active->devices.handles[j],
640 ACPI_STATE_D3);
641 if (result) {
Len Browncece9292006-06-26 23:04:31 -0400642 printk(KERN_WARNING PREFIX
643 "Unable to turn cooling device [%p] 'off'\n",
644 active->devices.handles[j]);
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400645 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400647 active->flags.enabled = 0;
648 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
649 "Cooling device [%p] now 'off'\n",
650 active->devices.handles[j]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
Len Brown4be44fc2005-08-05 00:44:28 -0400655static void acpi_thermal_check(void *context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Len Brown4be44fc2005-08-05 00:44:28 -0400657static void acpi_thermal_run(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 struct acpi_thermal *tz = (struct acpi_thermal *)data;
660 if (!tz->zombie)
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400661 acpi_os_execute(OSL_GPE_HANDLER, acpi_thermal_check, (void *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
Len Brown4be44fc2005-08-05 00:44:28 -0400664static void acpi_thermal_check(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Len Brown4be44fc2005-08-05 00:44:28 -0400666 int result = 0;
667 struct acpi_thermal *tz = (struct acpi_thermal *)data;
668 unsigned long sleep_time = 0;
669 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 struct acpi_thermal_state state;
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 if (!tz) {
Len Brown64684632006-06-26 23:41:38 -0400674 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400675 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677
678 state = tz->state;
679
680 result = acpi_thermal_get_temperature(tz);
681 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400682 return;
Len Brown4be44fc2005-08-05 00:44:28 -0400683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 memset(&tz->state, 0, sizeof(tz->state));
Len Brown4be44fc2005-08-05 00:44:28 -0400685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 /*
687 * Check Trip Points
688 * -----------------
689 * Compare the current temperature to the trip point values to see
690 * if we've entered one of the thermal policy states. Note that
691 * this function determines when a state is entered, but the
692 * individual policy decides when it is exited (e.g. hysteresis).
693 */
694 if (tz->trips.critical.flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400695 state.critical |=
696 (tz->temperature >= tz->trips.critical.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (tz->trips.hot.flags.valid)
698 state.hot |= (tz->temperature >= tz->trips.hot.temperature);
699 if (tz->trips.passive.flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400700 state.passive |=
701 (tz->temperature >= tz->trips.passive.temperature);
702 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (tz->trips.active[i].flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400704 state.active |=
705 (tz->temperature >=
706 tz->trips.active[i].temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 /*
709 * Invoke Policy
710 * -------------
711 * Separated from the above check to allow individual policy to
712 * determine when to exit a given state.
713 */
714 if (state.critical)
715 acpi_thermal_critical(tz);
716 if (state.hot)
717 acpi_thermal_hot(tz);
718 if (state.passive)
719 acpi_thermal_passive(tz);
720 if (state.active)
721 acpi_thermal_active(tz);
722
723 /*
724 * Calculate State
725 * ---------------
726 * Again, separated from the above two to allow independent policy
727 * decisions.
728 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400729 tz->state.critical = tz->trips.critical.flags.enabled;
730 tz->state.hot = tz->trips.hot.flags.enabled;
731 tz->state.passive = tz->trips.passive.flags.enabled;
732 tz->state.active = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400733 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400734 tz->state.active |= tz->trips.active[i].flags.enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 /*
737 * Calculate Sleep Time
738 * --------------------
739 * If we're in the passive state, use _TSP's value. Otherwise
740 * use the default polling frequency (e.g. _TZP). If no polling
741 * frequency is specified then we'll wait forever (at least until
742 * a thermal event occurs). Note that _TSP and _TZD values are
743 * given in 1/10th seconds (we must covert to milliseconds).
744 */
745 if (tz->state.passive)
746 sleep_time = tz->trips.passive.tsp * 100;
747 else if (tz->polling_frequency > 0)
748 sleep_time = tz->polling_frequency * 100;
749
Len Brown4be44fc2005-08-05 00:44:28 -0400750 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: temperature[%lu] sleep[%lu]\n",
751 tz->name, tz->temperature, sleep_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 /*
754 * Schedule Next Poll
755 * ------------------
756 */
757 if (!sleep_time) {
758 if (timer_pending(&(tz->timer)))
759 del_timer(&(tz->timer));
Len Brown4be44fc2005-08-05 00:44:28 -0400760 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (timer_pending(&(tz->timer)))
762 mod_timer(&(tz->timer), (HZ * sleep_time) / 1000);
763 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400764 tz->timer.data = (unsigned long)tz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 tz->timer.function = acpi_thermal_run;
766 tz->timer.expires = jiffies + (HZ * sleep_time) / 1000;
767 add_timer(&(tz->timer));
768 }
769 }
770
Patrick Mocheld550d982006-06-27 00:41:40 -0400771 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774/* --------------------------------------------------------------------------
775 FS Interface (/proc)
776 -------------------------------------------------------------------------- */
777
Len Brown4be44fc2005-08-05 00:44:28 -0400778static struct proc_dir_entry *acpi_thermal_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
781{
Len Brown4be44fc2005-08-05 00:44:28 -0400782 struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 if (!tz)
786 goto end;
787
788 seq_puts(seq, "state: ");
789
Len Brown4be44fc2005-08-05 00:44:28 -0400790 if (!tz->state.critical && !tz->state.hot && !tz->state.passive
791 && !tz->state.active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 seq_puts(seq, "ok\n");
793 else {
794 if (tz->state.critical)
795 seq_puts(seq, "critical ");
796 if (tz->state.hot)
797 seq_puts(seq, "hot ");
798 if (tz->state.passive)
799 seq_puts(seq, "passive ");
800 if (tz->state.active)
801 seq_printf(seq, "active[%d]", tz->state.active_index);
802 seq_puts(seq, "\n");
803 }
804
Len Brown4be44fc2005-08-05 00:44:28 -0400805 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400806 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
809static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
810{
811 return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
812}
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
815{
Len Brown4be44fc2005-08-05 00:44:28 -0400816 int result = 0;
817 struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 if (!tz)
821 goto end;
822
823 result = acpi_thermal_get_temperature(tz);
824 if (result)
825 goto end;
826
Len Brown4be44fc2005-08-05 00:44:28 -0400827 seq_printf(seq, "temperature: %ld C\n",
828 KELVIN_TO_CELSIUS(tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Len Brown4be44fc2005-08-05 00:44:28 -0400830 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400831 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
834static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
835{
836 return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
837}
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
840{
Len Brown4be44fc2005-08-05 00:44:28 -0400841 struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
842 int i = 0;
843 int j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 if (!tz)
847 goto end;
848
849 if (tz->trips.critical.flags.valid)
850 seq_printf(seq, "critical (S5): %ld C\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400851 KELVIN_TO_CELSIUS(tz->trips.critical.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 if (tz->trips.hot.flags.valid)
854 seq_printf(seq, "hot (S4): %ld C\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400855 KELVIN_TO_CELSIUS(tz->trips.hot.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 if (tz->trips.passive.flags.valid) {
Len Brown4be44fc2005-08-05 00:44:28 -0400858 seq_printf(seq,
859 "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
860 KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
861 tz->trips.passive.tc1, tz->trips.passive.tc2,
862 tz->trips.passive.tsp);
863 for (j = 0; j < tz->trips.passive.devices.count; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Len Brown4be44fc2005-08-05 00:44:28 -0400865 seq_printf(seq, "0x%p ",
866 tz->trips.passive.devices.handles[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
868 seq_puts(seq, "\n");
869 }
870
871 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
872 if (!(tz->trips.active[i].flags.valid))
873 break;
874 seq_printf(seq, "active[%d]: %ld C: devices=",
Len Brown4be44fc2005-08-05 00:44:28 -0400875 i,
876 KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
877 for (j = 0; j < tz->trips.active[i].devices.count; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 seq_printf(seq, "0x%p ",
Len Brown4be44fc2005-08-05 00:44:28 -0400879 tz->trips.active[i].devices.handles[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 seq_puts(seq, "\n");
881 }
882
Len Brown4be44fc2005-08-05 00:44:28 -0400883 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400884 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
886
887static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
888{
889 return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
890}
891
892static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -0400893acpi_thermal_write_trip_points(struct file *file,
894 const char __user * buffer,
895 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Len Brown4be44fc2005-08-05 00:44:28 -0400897 struct seq_file *m = (struct seq_file *)file->private_data;
898 struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Len Brown4be44fc2005-08-05 00:44:28 -0400900 char *limit_string;
901 int num, critical, hot, passive;
902 int *active;
903 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 limit_string = kmalloc(ACPI_THERMAL_MAX_LIMIT_STR_LEN, GFP_KERNEL);
Len Brown4be44fc2005-08-05 00:44:28 -0400907 if (!limit_string)
Patrick Mocheld550d982006-06-27 00:41:40 -0400908 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 memset(limit_string, 0, ACPI_THERMAL_MAX_LIMIT_STR_LEN);
911
Len Brown4be44fc2005-08-05 00:44:28 -0400912 active = kmalloc(ACPI_THERMAL_MAX_ACTIVE * sizeof(int), GFP_KERNEL);
Dave Jonesfdc136c2006-03-08 22:12:00 -0500913 if (!active) {
914 kfree(limit_string);
Patrick Mocheld550d982006-06-27 00:41:40 -0400915 return -ENOMEM;
Dave Jonesfdc136c2006-03-08 22:12:00 -0500916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 if (!tz || (count > ACPI_THERMAL_MAX_LIMIT_STR_LEN - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 count = -EINVAL;
920 goto end;
921 }
Len Brown4be44fc2005-08-05 00:44:28 -0400922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if (copy_from_user(limit_string, buffer, count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 count = -EFAULT;
925 goto end;
926 }
Len Brown4be44fc2005-08-05 00:44:28 -0400927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 limit_string[count] = '\0';
929
930 num = sscanf(limit_string, "%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d",
Len Brown4be44fc2005-08-05 00:44:28 -0400931 &critical, &hot, &passive,
932 &active[0], &active[1], &active[2], &active[3], &active[4],
933 &active[5], &active[6], &active[7], &active[8],
934 &active[9]);
935 if (!(num >= 5 && num < (ACPI_THERMAL_MAX_ACTIVE + 3))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 count = -EINVAL;
937 goto end;
938 }
939
940 tz->trips.critical.temperature = CELSIUS_TO_KELVIN(critical);
941 tz->trips.hot.temperature = CELSIUS_TO_KELVIN(hot);
942 tz->trips.passive.temperature = CELSIUS_TO_KELVIN(passive);
943 for (i = 0; i < num - 3; i++) {
944 if (!(tz->trips.active[i].flags.valid))
945 break;
946 tz->trips.active[i].temperature = CELSIUS_TO_KELVIN(active[i]);
947 }
Len Brown4be44fc2005-08-05 00:44:28 -0400948
949 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 kfree(active);
951 kfree(limit_string);
Patrick Mocheld550d982006-06-27 00:41:40 -0400952 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
956{
Len Brown4be44fc2005-08-05 00:44:28 -0400957 struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 if (!tz)
961 goto end;
962
963 if (!tz->flags.cooling_mode) {
964 seq_puts(seq, "<setting not supported>\n");
965 }
966
Len Brown4be44fc2005-08-05 00:44:28 -0400967 if (tz->cooling_mode == ACPI_THERMAL_MODE_CRITICAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 seq_printf(seq, "cooling mode: critical\n");
969 else
970 seq_printf(seq, "cooling mode: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400971 tz->cooling_mode ? "passive" : "active");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Len Brown4be44fc2005-08-05 00:44:28 -0400973 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400974 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
977static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
978{
979 return single_open(file, acpi_thermal_cooling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -0400980 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
983static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -0400984acpi_thermal_write_cooling_mode(struct file *file,
985 const char __user * buffer,
986 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
Len Brown4be44fc2005-08-05 00:44:28 -0400988 struct seq_file *m = (struct seq_file *)file->private_data;
989 struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
990 int result = 0;
991 char mode_string[12] = { '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 if (!tz || (count > sizeof(mode_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400995 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 if (!tz->flags.cooling_mode)
Patrick Mocheld550d982006-06-27 00:41:40 -0400998 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 if (copy_from_user(mode_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001001 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -04001002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 mode_string[count] = '\0';
Len Brown4be44fc2005-08-05 00:44:28 -04001004
1005 result = acpi_thermal_set_cooling_mode(tz,
1006 simple_strtoul(mode_string, NULL,
1007 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001009 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 acpi_thermal_check(tz);
1012
Patrick Mocheld550d982006-06-27 00:41:40 -04001013 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
1017{
Len Brown4be44fc2005-08-05 00:44:28 -04001018 struct acpi_thermal *tz = (struct acpi_thermal *)seq->private;
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->polling_frequency) {
1025 seq_puts(seq, "<polling disabled>\n");
1026 goto end;
1027 }
1028
1029 seq_printf(seq, "polling frequency: %lu seconds\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001030 (tz->polling_frequency / 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Len Brown4be44fc2005-08-05 00:44:28 -04001032 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001033 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
1036static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
1037{
1038 return single_open(file, acpi_thermal_polling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001039 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
1042static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001043acpi_thermal_write_polling(struct file *file,
1044 const char __user * buffer,
1045 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
Len Brown4be44fc2005-08-05 00:44:28 -04001047 struct seq_file *m = (struct seq_file *)file->private_data;
1048 struct acpi_thermal *tz = (struct acpi_thermal *)m->private;
1049 int result = 0;
1050 char polling_string[12] = { '\0' };
1051 int seconds = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 if (!tz || (count > sizeof(polling_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001055 return -EINVAL;
Len Brown4be44fc2005-08-05 00:44:28 -04001056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (copy_from_user(polling_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001058 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -04001059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 polling_string[count] = '\0';
1061
1062 seconds = simple_strtoul(polling_string, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 result = acpi_thermal_set_polling(tz, seconds);
1065 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001066 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 acpi_thermal_check(tz);
1069
Patrick Mocheld550d982006-06-27 00:41:40 -04001070 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
Len Brown4be44fc2005-08-05 00:44:28 -04001073static int acpi_thermal_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
Len Brown4be44fc2005-08-05 00:44:28 -04001075 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 if (!acpi_device_dir(device)) {
1079 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -04001080 acpi_thermal_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001082 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 acpi_device_dir(device)->owner = THIS_MODULE;
1084 }
1085
1086 /* 'state' [R] */
1087 entry = create_proc_entry(ACPI_THERMAL_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -04001088 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001090 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 else {
1092 entry->proc_fops = &acpi_thermal_state_fops;
1093 entry->data = acpi_driver_data(device);
1094 entry->owner = THIS_MODULE;
1095 }
1096
1097 /* 'temperature' [R] */
1098 entry = create_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
Len Brown4be44fc2005-08-05 00:44:28 -04001099 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001101 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 else {
1103 entry->proc_fops = &acpi_thermal_temp_fops;
1104 entry->data = acpi_driver_data(device);
1105 entry->owner = THIS_MODULE;
1106 }
1107
1108 /* 'trip_points' [R/W] */
1109 entry = create_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
Len Brown4be44fc2005-08-05 00:44:28 -04001110 S_IFREG | S_IRUGO | S_IWUSR,
1111 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001113 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 else {
1115 entry->proc_fops = &acpi_thermal_trip_fops;
1116 entry->data = acpi_driver_data(device);
1117 entry->owner = THIS_MODULE;
1118 }
1119
1120 /* 'cooling_mode' [R/W] */
1121 entry = create_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
Len Brown4be44fc2005-08-05 00:44:28 -04001122 S_IFREG | S_IRUGO | S_IWUSR,
1123 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001125 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 else {
1127 entry->proc_fops = &acpi_thermal_cooling_fops;
1128 entry->data = acpi_driver_data(device);
1129 entry->owner = THIS_MODULE;
1130 }
1131
1132 /* 'polling_frequency' [R/W] */
1133 entry = create_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
Len Brown4be44fc2005-08-05 00:44:28 -04001134 S_IFREG | S_IRUGO | S_IWUSR,
1135 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001137 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 else {
1139 entry->proc_fops = &acpi_thermal_polling_fops;
1140 entry->data = acpi_driver_data(device);
1141 entry->owner = THIS_MODULE;
1142 }
1143
Patrick Mocheld550d982006-06-27 00:41:40 -04001144 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145}
1146
Len Brown4be44fc2005-08-05 00:44:28 -04001147static int acpi_thermal_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 if (acpi_device_dir(device)) {
1151 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1152 acpi_device_dir(device));
1153 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1154 acpi_device_dir(device));
1155 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1156 acpi_device_dir(device));
1157 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1158 acpi_device_dir(device));
1159 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1160 acpi_device_dir(device));
1161 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1162 acpi_device_dir(device) = NULL;
1163 }
1164
Patrick Mocheld550d982006-06-27 00:41:40 -04001165 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166}
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168/* --------------------------------------------------------------------------
1169 Driver Interface
1170 -------------------------------------------------------------------------- */
1171
Len Brown4be44fc2005-08-05 00:44:28 -04001172static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Len Brown4be44fc2005-08-05 00:44:28 -04001174 struct acpi_thermal *tz = (struct acpi_thermal *)data;
1175 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001179 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Patrick Mochel8348e1b2006-05-19 16:54:40 -04001181 device = tz->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 switch (event) {
1184 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1185 acpi_thermal_check(tz);
1186 break;
1187 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
1188 acpi_thermal_get_trip_points(tz);
1189 acpi_thermal_check(tz);
1190 acpi_bus_generate_event(device, event, 0);
1191 break;
1192 case ACPI_THERMAL_NOTIFY_DEVICES:
1193 if (tz->flags.devices)
1194 acpi_thermal_get_devices(tz);
1195 acpi_bus_generate_event(device, event, 0);
1196 break;
1197 default:
1198 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001199 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 break;
1201 }
1202
Patrick Mocheld550d982006-06-27 00:41:40 -04001203 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
1205
Len Brown4be44fc2005-08-05 00:44:28 -04001206static int acpi_thermal_get_info(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
Len Brown4be44fc2005-08-05 00:44:28 -04001208 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001212 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 /* Get temperature [_TMP] (required) */
1215 result = acpi_thermal_get_temperature(tz);
1216 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001217 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 /* Get trip points [_CRT, _PSV, etc.] (required) */
1220 result = acpi_thermal_get_trip_points(tz);
1221 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001222 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 /* Set the cooling mode [_SCP] to active cooling (default) */
1225 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
Len Brown4be44fc2005-08-05 00:44:28 -04001226 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 tz->flags.cooling_mode = 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001228 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 /* Oh,we have not _SCP method.
Len Brown4be44fc2005-08-05 00:44:28 -04001230 Generally show cooling_mode by _ACx, _PSV,spec 12.2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 tz->flags.cooling_mode = 0;
Len Brown4be44fc2005-08-05 00:44:28 -04001232 if (tz->trips.active[0].flags.valid
1233 && tz->trips.passive.flags.valid) {
1234 if (tz->trips.passive.temperature >
1235 tz->trips.active[0].temperature)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 tz->cooling_mode = ACPI_THERMAL_MODE_ACTIVE;
Len Brown4be44fc2005-08-05 00:44:28 -04001237 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 tz->cooling_mode = ACPI_THERMAL_MODE_PASSIVE;
Len Brown4be44fc2005-08-05 00:44:28 -04001239 } else if (!tz->trips.active[0].flags.valid
1240 && tz->trips.passive.flags.valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 tz->cooling_mode = ACPI_THERMAL_MODE_PASSIVE;
Len Brown4be44fc2005-08-05 00:44:28 -04001242 } else if (tz->trips.active[0].flags.valid
1243 && !tz->trips.passive.flags.valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 tz->cooling_mode = ACPI_THERMAL_MODE_ACTIVE;
1245 } else {
1246 /* _ACx and _PSV are optional, but _CRT is required */
1247 tz->cooling_mode = ACPI_THERMAL_MODE_CRITICAL;
1248 }
1249 }
1250
1251 /* Get default polling frequency [_TZP] (optional) */
1252 if (tzp)
1253 tz->polling_frequency = tzp;
1254 else
1255 acpi_thermal_get_polling_frequency(tz);
1256
1257 /* Get devices in this thermal zone [_TZD] (optional) */
1258 result = acpi_thermal_get_devices(tz);
1259 if (!result)
1260 tz->flags.devices = 1;
1261
Patrick Mocheld550d982006-06-27 00:41:40 -04001262 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263}
1264
Len Brown4be44fc2005-08-05 00:44:28 -04001265static int acpi_thermal_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
Len Brown4be44fc2005-08-05 00:44:28 -04001267 int result = 0;
1268 acpi_status status = AE_OK;
1269 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001273 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 tz = kmalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
1276 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001277 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 memset(tz, 0, sizeof(struct acpi_thermal));
1279
1280 tz->handle = device->handle;
Patrick Mochel8348e1b2006-05-19 16:54:40 -04001281 tz->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 strcpy(tz->name, device->pnp.bus_id);
1283 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1284 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1285 acpi_driver_data(device) = tz;
1286
1287 result = acpi_thermal_get_info(tz);
1288 if (result)
1289 goto end;
1290
1291 result = acpi_thermal_add_fs(device);
1292 if (result)
Vasily Averin09047e72006-04-27 05:25:00 -04001293 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 init_timer(&tz->timer);
1296
1297 acpi_thermal_check(tz);
1298
Patrick Mochel38ba7c92006-05-19 16:54:48 -04001299 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001300 ACPI_DEVICE_NOTIFY,
1301 acpi_thermal_notify, tz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 result = -ENODEV;
1304 goto end;
1305 }
1306
1307 printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001308 acpi_device_name(device), acpi_device_bid(device),
1309 KELVIN_TO_CELSIUS(tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Len Brown4be44fc2005-08-05 00:44:28 -04001311 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (result) {
1313 acpi_thermal_remove_fs(device);
1314 kfree(tz);
1315 }
1316
Patrick Mocheld550d982006-06-27 00:41:40 -04001317 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318}
1319
Len Brown4be44fc2005-08-05 00:44:28 -04001320static int acpi_thermal_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
Len Brown4be44fc2005-08-05 00:44:28 -04001322 acpi_status status = AE_OK;
1323 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001327 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Len Brown4be44fc2005-08-05 00:44:28 -04001329 tz = (struct acpi_thermal *)acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 /* avoid timer adding new defer task */
1332 tz->zombie = 1;
1333 /* wait for running timer (on other CPUs) finish */
1334 del_timer_sync(&(tz->timer));
1335 /* synchronize deferred task */
1336 acpi_os_wait_events_complete(NULL);
1337 /* deferred task may reinsert timer */
1338 del_timer_sync(&(tz->timer));
1339
Patrick Mochel38ba7c92006-05-19 16:54:48 -04001340 status = acpi_remove_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001341 ACPI_DEVICE_NOTIFY,
1342 acpi_thermal_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 /* Terminate policy */
Len Brown4be44fc2005-08-05 00:44:28 -04001345 if (tz->trips.passive.flags.valid && tz->trips.passive.flags.enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 tz->trips.passive.flags.enabled = 0;
1347 acpi_thermal_passive(tz);
1348 }
1349 if (tz->trips.active[0].flags.valid
Len Brown4be44fc2005-08-05 00:44:28 -04001350 && tz->trips.active[0].flags.enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 tz->trips.active[0].flags.enabled = 0;
1352 acpi_thermal_active(tz);
1353 }
1354
1355 acpi_thermal_remove_fs(device);
1356
1357 kfree(tz);
Patrick Mocheld550d982006-06-27 00:41:40 -04001358 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359}
1360
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001361static int acpi_thermal_resume(struct acpi_device *device, int state)
1362{
1363 struct acpi_thermal *tz = NULL;
1364
1365 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001366 return -EINVAL;
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001367
1368 tz = (struct acpi_thermal *)acpi_driver_data(device);
1369
1370 acpi_thermal_check(tz);
1371
1372 return AE_OK;
1373}
1374
Len Brown4be44fc2005-08-05 00:44:28 -04001375static int __init acpi_thermal_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
Len Brown4be44fc2005-08-05 00:44:28 -04001377 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1381 if (!acpi_thermal_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001382 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 acpi_thermal_dir->owner = THIS_MODULE;
1384
1385 result = acpi_bus_register_driver(&acpi_thermal_driver);
1386 if (result < 0) {
1387 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04001388 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
1390
Patrick Mocheld550d982006-06-27 00:41:40 -04001391 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392}
1393
Len Brown4be44fc2005-08-05 00:44:28 -04001394static void __exit acpi_thermal_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 acpi_bus_unregister_driver(&acpi_thermal_driver);
1398
1399 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1400
Patrick Mocheld550d982006-06-27 00:41:40 -04001401 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402}
1403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404module_init(acpi_thermal_init);
1405module_exit(acpi_thermal_exit);