blob: 194ecfe8b3600d6195a491eae99483546f1a3abe [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>
43#include <asm/uaccess.h>
44
45#include <acpi/acpi_bus.h>
46#include <acpi/acpi_drivers.h>
47
48#define ACPI_THERMAL_COMPONENT 0x04000000
49#define ACPI_THERMAL_CLASS "thermal_zone"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define ACPI_THERMAL_PATH_POWEROFF "/sbin/poweroff"
63
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;
78module_param(tzp, int, 0);
79MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.\n");
80
Len Brown4be44fc2005-08-05 00:44:28 -040081static int acpi_thermal_add(struct acpi_device *device);
82static int acpi_thermal_remove(struct acpi_device *device, int type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +080083static int acpi_thermal_resume(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
85static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
86static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -040088static ssize_t acpi_thermal_write_cooling_mode(struct file *,
89 const char __user *, size_t,
90 loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -040092static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
93 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95static struct acpi_driver acpi_thermal_driver = {
Len Brownc2b67052007-02-12 23:33:40 -050096 .name = "thermal",
Len Brown4be44fc2005-08-05 00:44:28 -040097 .class = ACPI_THERMAL_CLASS,
98 .ids = ACPI_THERMAL_HID,
99 .ops = {
100 .add = acpi_thermal_add,
101 .remove = acpi_thermal_remove,
Konstantin Karasyov74ce14682006-05-08 08:32:00 -0400102 .resume = acpi_thermal_resume,
Len Brown4be44fc2005-08-05 00:44:28 -0400103 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104};
105
106struct acpi_thermal_state {
Len Brown4be44fc2005-08-05 00:44:28 -0400107 u8 critical:1;
108 u8 hot:1;
109 u8 passive:1;
110 u8 active:1;
111 u8 reserved:4;
112 int active_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
114
115struct acpi_thermal_state_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400116 u8 valid:1;
117 u8 enabled:1;
118 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119};
120
121struct acpi_thermal_critical {
122 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400123 unsigned long temperature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
126struct acpi_thermal_hot {
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_passive {
132 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400133 unsigned long temperature;
134 unsigned long tc1;
135 unsigned long tc2;
136 unsigned long tsp;
137 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
140struct acpi_thermal_active {
141 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400142 unsigned long temperature;
143 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144};
145
146struct acpi_thermal_trips {
147 struct acpi_thermal_critical critical;
Len Brown4be44fc2005-08-05 00:44:28 -0400148 struct acpi_thermal_hot hot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 struct acpi_thermal_passive passive;
150 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
151};
152
153struct acpi_thermal_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400154 u8 cooling_mode:1; /* _SCP */
155 u8 devices:1; /* _TZD */
156 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157};
158
159struct acpi_thermal {
Patrick Mochel8348e1b2006-05-19 16:54:40 -0400160 struct acpi_device * device;
Len Brown4be44fc2005-08-05 00:44:28 -0400161 acpi_bus_id name;
162 unsigned long temperature;
163 unsigned long last_temperature;
164 unsigned long polling_frequency;
Len Brown4be44fc2005-08-05 00:44:28 -0400165 volatile u8 zombie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 struct acpi_thermal_flags flags;
167 struct acpi_thermal_state state;
168 struct acpi_thermal_trips trips;
Len Brown4be44fc2005-08-05 00:44:28 -0400169 struct acpi_handle_list devices;
170 struct timer_list timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171};
172
Arjan van de Vend7508032006-07-04 13:06:00 -0400173static const struct file_operations acpi_thermal_state_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400174 .open = acpi_thermal_state_open_fs,
175 .read = seq_read,
176 .llseek = seq_lseek,
177 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178};
179
Arjan van de Vend7508032006-07-04 13:06:00 -0400180static const struct file_operations acpi_thermal_temp_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400181 .open = acpi_thermal_temp_open_fs,
182 .read = seq_read,
183 .llseek = seq_lseek,
184 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185};
186
Arjan van de Vend7508032006-07-04 13:06:00 -0400187static const struct file_operations acpi_thermal_trip_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400188 .open = acpi_thermal_trip_open_fs,
189 .read = seq_read,
Len Brown4be44fc2005-08-05 00:44:28 -0400190 .llseek = seq_lseek,
191 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192};
193
Arjan van de Vend7508032006-07-04 13:06:00 -0400194static const struct file_operations acpi_thermal_cooling_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400195 .open = acpi_thermal_cooling_open_fs,
196 .read = seq_read,
197 .write = acpi_thermal_write_cooling_mode,
198 .llseek = seq_lseek,
199 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200};
201
Arjan van de Vend7508032006-07-04 13:06:00 -0400202static const struct file_operations acpi_thermal_polling_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400203 .open = acpi_thermal_polling_open_fs,
204 .read = seq_read,
205 .write = acpi_thermal_write_polling,
206 .llseek = seq_lseek,
207 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208};
209
210/* --------------------------------------------------------------------------
211 Thermal Zone Management
212 -------------------------------------------------------------------------- */
213
Len Brown4be44fc2005-08-05 00:44:28 -0400214static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Len Brown4be44fc2005-08-05 00:44:28 -0400216 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400220 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 tz->last_temperature = tz->temperature;
223
Len Brown4be44fc2005-08-05 00:44:28 -0400224 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400225 acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tz->temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400227 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Len Brown4be44fc2005-08-05 00:44:28 -0400229 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
230 tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Patrick Mocheld550d982006-06-27 00:41:40 -0400232 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Len Brown4be44fc2005-08-05 00:44:28 -0400235static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Len Brown4be44fc2005-08-05 00:44:28 -0400237 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400241 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Len Brown4be44fc2005-08-05 00:44:28 -0400243 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400244 acpi_evaluate_integer(tz->device->handle, "_TZP", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400245 &tz->polling_frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400247 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Len Brown4be44fc2005-08-05 00:44:28 -0400249 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
250 tz->polling_frequency));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Patrick Mocheld550d982006-06-27 00:41:40 -0400252 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
Len Brown4be44fc2005-08-05 00:44:28 -0400255static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400259 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 tz->polling_frequency = seconds * 10; /* Convert value to deci-seconds */
262
Len Brown4be44fc2005-08-05 00:44:28 -0400263 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
264 "Polling frequency set to %lu seconds\n",
Sanjoy Mahajan636cedf2007-02-16 01:24:43 -0500265 tz->polling_frequency/10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Patrick Mocheld550d982006-06-27 00:41:40 -0400267 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
Len Brown4be44fc2005-08-05 00:44:28 -0400270static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Len Brown4be44fc2005-08-05 00:44:28 -0400272 acpi_status status = AE_OK;
273 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
274 struct acpi_object_list arg_list = { 1, &arg0 };
275 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400279 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400281 status = acpi_get_handle(tz->device->handle, "_SCP", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (ACPI_FAILURE(status)) {
283 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400284 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286
287 arg0.integer.value = mode;
288
289 status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
290 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400291 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Patrick Mocheld550d982006-06-27 00:41:40 -0400293 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Len Brown4be44fc2005-08-05 00:44:28 -0400296static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Len Brown4be44fc2005-08-05 00:44:28 -0400298 acpi_status status = AE_OK;
299 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400303 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 /* Critical Shutdown (required) */
306
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400307 status = acpi_evaluate_integer(tz->device->handle, "_CRT", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400308 &tz->trips.critical.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (ACPI_FAILURE(status)) {
310 tz->trips.critical.flags.valid = 0;
Thomas Renningera6fc6722006-06-26 23:58:43 -0400311 ACPI_EXCEPTION((AE_INFO, status, "No critical threshold"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400312 return -ENODEV;
Len Brown4be44fc2005-08-05 00:44:28 -0400313 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 tz->trips.critical.flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400315 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
316 "Found critical threshold [%lu]\n",
317 tz->trips.critical.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
319
320 /* Critical Sleep (optional) */
321
Len Brown4be44fc2005-08-05 00:44:28 -0400322 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400323 acpi_evaluate_integer(tz->device->handle, "_HOT", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400324 &tz->trips.hot.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (ACPI_FAILURE(status)) {
326 tz->trips.hot.flags.valid = 0;
327 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No hot threshold\n"));
Len Brown4be44fc2005-08-05 00:44:28 -0400328 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 tz->trips.hot.flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400330 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found hot threshold [%lu]\n",
331 tz->trips.hot.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
333
334 /* Passive: Processors (optional) */
335
Len Brown4be44fc2005-08-05 00:44:28 -0400336 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400337 acpi_evaluate_integer(tz->device->handle, "_PSV", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400338 &tz->trips.passive.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (ACPI_FAILURE(status)) {
340 tz->trips.passive.flags.valid = 0;
341 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No passive threshold\n"));
Len Brown4be44fc2005-08-05 00:44:28 -0400342 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 tz->trips.passive.flags.valid = 1;
344
Len Brown4be44fc2005-08-05 00:44:28 -0400345 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400346 acpi_evaluate_integer(tz->device->handle, "_TC1", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400347 &tz->trips.passive.tc1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (ACPI_FAILURE(status))
349 tz->trips.passive.flags.valid = 0;
350
Len Brown4be44fc2005-08-05 00:44:28 -0400351 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400352 acpi_evaluate_integer(tz->device->handle, "_TC2", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400353 &tz->trips.passive.tc2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (ACPI_FAILURE(status))
355 tz->trips.passive.flags.valid = 0;
356
Len Brown4be44fc2005-08-05 00:44:28 -0400357 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400358 acpi_evaluate_integer(tz->device->handle, "_TSP", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400359 &tz->trips.passive.tsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (ACPI_FAILURE(status))
361 tz->trips.passive.flags.valid = 0;
362
Len Brown4be44fc2005-08-05 00:44:28 -0400363 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400364 acpi_evaluate_reference(tz->device->handle, "_PSL", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400365 &tz->trips.passive.devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (ACPI_FAILURE(status))
367 tz->trips.passive.flags.valid = 0;
368
369 if (!tz->trips.passive.flags.valid)
Len Browncece9292006-06-26 23:04:31 -0400370 printk(KERN_WARNING PREFIX "Invalid passive threshold\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 else
Len Brown4be44fc2005-08-05 00:44:28 -0400372 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
373 "Found passive threshold [%lu]\n",
374 tz->trips.passive.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376
377 /* Active: Fans, etc. (optional) */
378
Len Brown4be44fc2005-08-05 00:44:28 -0400379 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Len Brown4be44fc2005-08-05 00:44:28 -0400381 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Len Brown4be44fc2005-08-05 00:44:28 -0400383 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400384 acpi_evaluate_integer(tz->device->handle, name, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400385 &tz->trips.active[i].temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (ACPI_FAILURE(status))
387 break;
388
389 name[2] = 'L';
Len Brown4be44fc2005-08-05 00:44:28 -0400390 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400391 acpi_evaluate_reference(tz->device->handle, name, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400392 &tz->trips.active[i].devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (ACPI_SUCCESS(status)) {
394 tz->trips.active[i].flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400395 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
396 "Found active threshold [%d]:[%lu]\n",
397 i, tz->trips.active[i].temperature));
398 } else
Thomas Renningera6fc6722006-06-26 23:58:43 -0400399 ACPI_EXCEPTION((AE_INFO, status,
400 "Invalid active threshold [%d]", i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402
Patrick Mocheld550d982006-06-27 00:41:40 -0400403 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
Len Brown4be44fc2005-08-05 00:44:28 -0400406static int acpi_thermal_get_devices(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Len Brown4be44fc2005-08-05 00:44:28 -0400408 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400412 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Len Brown4be44fc2005-08-05 00:44:28 -0400414 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400415 acpi_evaluate_reference(tz->device->handle, "_TZD", NULL, &tz->devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400417 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Patrick Mocheld550d982006-06-27 00:41:40 -0400419 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
Len Brown4be44fc2005-08-05 00:44:28 -0400422static int acpi_thermal_call_usermode(char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Len Brown4be44fc2005-08-05 00:44:28 -0400424 char *argv[2] = { NULL, NULL };
425 char *envp[3] = { NULL, NULL, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 if (!path)
Patrick Mocheld550d982006-06-27 00:41:40 -0400429 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 argv[0] = path;
432
433 /* minimal command environment */
434 envp[0] = "HOME=/";
435 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
Len Brown4be44fc2005-08-05 00:44:28 -0400436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 call_usermodehelper(argv[0], argv, envp, 0);
438
Patrick Mocheld550d982006-06-27 00:41:40 -0400439 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
Len Brown4be44fc2005-08-05 00:44:28 -0400442static int acpi_thermal_critical(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (!tz || !tz->trips.critical.flags.valid)
Patrick Mocheld550d982006-06-27 00:41:40 -0400445 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 if (tz->temperature >= tz->trips.critical.temperature) {
Len Browncece9292006-06-26 23:04:31 -0400448 printk(KERN_WARNING PREFIX "Critical trip point\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 tz->trips.critical.flags.enabled = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400450 } else if (tz->trips.critical.flags.enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 tz->trips.critical.flags.enabled = 0;
452
Len Brown4be44fc2005-08-05 00:44:28 -0400453 printk(KERN_EMERG
454 "Critical temperature reached (%ld C), shutting down.\n",
455 KELVIN_TO_CELSIUS(tz->temperature));
Patrick Mochel8348e1b2006-05-19 16:54:40 -0400456 acpi_bus_generate_event(tz->device, ACPI_THERMAL_NOTIFY_CRITICAL,
Len Brown4be44fc2005-08-05 00:44:28 -0400457 tz->trips.critical.flags.enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 acpi_thermal_call_usermode(ACPI_THERMAL_PATH_POWEROFF);
460
Patrick Mocheld550d982006-06-27 00:41:40 -0400461 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
Len Brown4be44fc2005-08-05 00:44:28 -0400464static int acpi_thermal_hot(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (!tz || !tz->trips.hot.flags.valid)
Patrick Mocheld550d982006-06-27 00:41:40 -0400467 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 if (tz->temperature >= tz->trips.hot.temperature) {
Len Browncece9292006-06-26 23:04:31 -0400470 printk(KERN_WARNING PREFIX "Hot trip point\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 tz->trips.hot.flags.enabled = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400472 } else if (tz->trips.hot.flags.enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 tz->trips.hot.flags.enabled = 0;
474
Patrick Mochel8348e1b2006-05-19 16:54:40 -0400475 acpi_bus_generate_event(tz->device, ACPI_THERMAL_NOTIFY_HOT,
Len Brown4be44fc2005-08-05 00:44:28 -0400476 tz->trips.hot.flags.enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 /* TBD: Call user-mode "sleep(S4)" function */
479
Patrick Mocheld550d982006-06-27 00:41:40 -0400480 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400483static void acpi_thermal_passive(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400485 int result = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 struct acpi_thermal_passive *passive = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400487 int trend = 0;
488 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 if (!tz || !tz->trips.passive.flags.valid)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400492 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 passive = &(tz->trips.passive);
495
496 /*
497 * Above Trip?
498 * -----------
499 * Calculate the thermal trend (using the passive cooling equation)
500 * and modify the performance limit for all passive cooling devices
501 * accordingly. Note that we assume symmetry.
502 */
503 if (tz->temperature >= passive->temperature) {
Len Brown4be44fc2005-08-05 00:44:28 -0400504 trend =
505 (passive->tc1 * (tz->temperature - tz->last_temperature)) +
506 (passive->tc2 * (tz->temperature - passive->temperature));
507 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
508 "trend[%d]=(tc1[%lu]*(tmp[%lu]-last[%lu]))+(tc2[%lu]*(tmp[%lu]-psv[%lu]))\n",
509 trend, passive->tc1, tz->temperature,
510 tz->last_temperature, passive->tc2,
511 tz->temperature, passive->temperature));
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400512 passive->flags.enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 /* Heating up? */
514 if (trend > 0)
Len Brown4be44fc2005-08-05 00:44:28 -0400515 for (i = 0; i < passive->devices.count; i++)
516 acpi_processor_set_thermal_limit(passive->
517 devices.
518 handles[i],
519 ACPI_PROCESSOR_LIMIT_INCREMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /* Cooling off? */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400521 else if (trend < 0) {
Len Brown4be44fc2005-08-05 00:44:28 -0400522 for (i = 0; i < passive->devices.count; i++)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400523 /*
524 * assume that we are on highest
525 * freq/lowest thrott and can leave
526 * passive mode, even in error case
527 */
528 if (!acpi_processor_set_thermal_limit
529 (passive->devices.handles[i],
530 ACPI_PROCESSOR_LIMIT_DECREMENT))
531 result = 0;
532 /*
533 * Leave cooling mode, even if the temp might
534 * higher than trip point This is because some
535 * machines might have long thermal polling
536 * frequencies (tsp) defined. We will fall back
537 * into passive mode in next cycle (probably quicker)
538 */
539 if (result) {
540 passive->flags.enabled = 0;
541 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
542 "Disabling passive cooling, still above threshold,"
543 " but we are cooling down\n"));
544 }
545 }
546 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548
549 /*
550 * Below Trip?
551 * -----------
552 * Implement passive cooling hysteresis to slowly increase performance
553 * and avoid thrashing around the passive trip point. Note that we
554 * assume symmetry.
555 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400556 if (!passive->flags.enabled)
557 return;
558 for (i = 0; i < passive->devices.count; i++)
559 if (!acpi_processor_set_thermal_limit
560 (passive->devices.handles[i],
561 ACPI_PROCESSOR_LIMIT_DECREMENT))
562 result = 0;
563 if (result) {
564 passive->flags.enabled = 0;
565 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
566 "Disabling passive cooling (zone is cool)\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400570static void acpi_thermal_active(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Len Brown4be44fc2005-08-05 00:44:28 -0400572 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 struct acpi_thermal_active *active = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400574 int i = 0;
575 int j = 0;
576 unsigned long maxtemp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 if (!tz)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400580 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Len Brown4be44fc2005-08-05 00:44:28 -0400582 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 active = &(tz->trips.active[i]);
584 if (!active || !active->flags.valid)
585 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (tz->temperature >= active->temperature) {
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400587 /*
588 * Above Threshold?
589 * ----------------
590 * If not already enabled, turn ON all cooling devices
591 * associated with this active threshold.
592 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (active->temperature > maxtemp)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400594 tz->state.active_index = i;
595 maxtemp = active->temperature;
596 if (active->flags.enabled)
597 continue;
598 for (j = 0; j < active->devices.count; j++) {
599 result =
600 acpi_bus_set_power(active->devices.
601 handles[j],
602 ACPI_STATE_D0);
603 if (result) {
Len Browncece9292006-06-26 23:04:31 -0400604 printk(KERN_WARNING PREFIX
605 "Unable to turn cooling device [%p] 'on'\n",
Thomas Renningera6fc6722006-06-26 23:58:43 -0400606 active->devices.
Len Browncece9292006-06-26 23:04:31 -0400607 handles[j]);
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400608 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400610 active->flags.enabled = 1;
611 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
612 "Cooling device [%p] now 'on'\n",
613 active->devices.handles[j]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400615 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400617 if (!active->flags.enabled)
618 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 /*
620 * Below Threshold?
621 * ----------------
622 * Turn OFF all cooling devices associated with this
623 * threshold.
624 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400625 for (j = 0; j < active->devices.count; j++) {
626 result = acpi_bus_set_power(active->devices.handles[j],
627 ACPI_STATE_D3);
628 if (result) {
Len Browncece9292006-06-26 23:04:31 -0400629 printk(KERN_WARNING PREFIX
630 "Unable to turn cooling device [%p] 'off'\n",
631 active->devices.handles[j]);
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400632 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400634 active->flags.enabled = 0;
635 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
636 "Cooling device [%p] now 'off'\n",
637 active->devices.handles[j]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
Len Brown4be44fc2005-08-05 00:44:28 -0400642static void acpi_thermal_check(void *context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Len Brown4be44fc2005-08-05 00:44:28 -0400644static void acpi_thermal_run(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
646 struct acpi_thermal *tz = (struct acpi_thermal *)data;
647 if (!tz->zombie)
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400648 acpi_os_execute(OSL_GPE_HANDLER, acpi_thermal_check, (void *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
Len Brown4be44fc2005-08-05 00:44:28 -0400651static void acpi_thermal_check(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Len Brown4be44fc2005-08-05 00:44:28 -0400653 int result = 0;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200654 struct acpi_thermal *tz = data;
Len Brown4be44fc2005-08-05 00:44:28 -0400655 unsigned long sleep_time = 0;
656 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 struct acpi_thermal_state state;
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 if (!tz) {
Len Brown64684632006-06-26 23:41:38 -0400661 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400662 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664
665 state = tz->state;
666
667 result = acpi_thermal_get_temperature(tz);
668 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400669 return;
Len Brown4be44fc2005-08-05 00:44:28 -0400670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 memset(&tz->state, 0, sizeof(tz->state));
Len Brown4be44fc2005-08-05 00:44:28 -0400672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 /*
674 * Check Trip Points
675 * -----------------
676 * Compare the current temperature to the trip point values to see
677 * if we've entered one of the thermal policy states. Note that
678 * this function determines when a state is entered, but the
679 * individual policy decides when it is exited (e.g. hysteresis).
680 */
681 if (tz->trips.critical.flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400682 state.critical |=
683 (tz->temperature >= tz->trips.critical.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (tz->trips.hot.flags.valid)
685 state.hot |= (tz->temperature >= tz->trips.hot.temperature);
686 if (tz->trips.passive.flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400687 state.passive |=
688 (tz->temperature >= tz->trips.passive.temperature);
689 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (tz->trips.active[i].flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400691 state.active |=
692 (tz->temperature >=
693 tz->trips.active[i].temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 /*
696 * Invoke Policy
697 * -------------
698 * Separated from the above check to allow individual policy to
699 * determine when to exit a given state.
700 */
701 if (state.critical)
702 acpi_thermal_critical(tz);
703 if (state.hot)
704 acpi_thermal_hot(tz);
705 if (state.passive)
706 acpi_thermal_passive(tz);
707 if (state.active)
708 acpi_thermal_active(tz);
709
710 /*
711 * Calculate State
712 * ---------------
713 * Again, separated from the above two to allow independent policy
714 * decisions.
715 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400716 tz->state.critical = tz->trips.critical.flags.enabled;
717 tz->state.hot = tz->trips.hot.flags.enabled;
718 tz->state.passive = tz->trips.passive.flags.enabled;
719 tz->state.active = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400720 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400721 tz->state.active |= tz->trips.active[i].flags.enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 /*
724 * Calculate Sleep Time
725 * --------------------
726 * If we're in the passive state, use _TSP's value. Otherwise
727 * use the default polling frequency (e.g. _TZP). If no polling
728 * frequency is specified then we'll wait forever (at least until
729 * a thermal event occurs). Note that _TSP and _TZD values are
730 * given in 1/10th seconds (we must covert to milliseconds).
731 */
732 if (tz->state.passive)
733 sleep_time = tz->trips.passive.tsp * 100;
734 else if (tz->polling_frequency > 0)
735 sleep_time = tz->polling_frequency * 100;
736
Len Brown4be44fc2005-08-05 00:44:28 -0400737 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: temperature[%lu] sleep[%lu]\n",
738 tz->name, tz->temperature, sleep_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 /*
741 * Schedule Next Poll
742 * ------------------
743 */
744 if (!sleep_time) {
745 if (timer_pending(&(tz->timer)))
746 del_timer(&(tz->timer));
Len Brown4be44fc2005-08-05 00:44:28 -0400747 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (timer_pending(&(tz->timer)))
Andrew Morton94e22e12007-04-23 14:41:13 -0700749 mod_timer(&(tz->timer),
750 jiffies + (HZ * sleep_time) / 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400752 tz->timer.data = (unsigned long)tz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 tz->timer.function = acpi_thermal_run;
754 tz->timer.expires = jiffies + (HZ * sleep_time) / 1000;
755 add_timer(&(tz->timer));
756 }
757 }
758
Patrick Mocheld550d982006-06-27 00:41:40 -0400759 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762/* --------------------------------------------------------------------------
763 FS Interface (/proc)
764 -------------------------------------------------------------------------- */
765
Len Brown4be44fc2005-08-05 00:44:28 -0400766static struct proc_dir_entry *acpi_thermal_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
769{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200770 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 if (!tz)
774 goto end;
775
776 seq_puts(seq, "state: ");
777
Len Brown4be44fc2005-08-05 00:44:28 -0400778 if (!tz->state.critical && !tz->state.hot && !tz->state.passive
779 && !tz->state.active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 seq_puts(seq, "ok\n");
781 else {
782 if (tz->state.critical)
783 seq_puts(seq, "critical ");
784 if (tz->state.hot)
785 seq_puts(seq, "hot ");
786 if (tz->state.passive)
787 seq_puts(seq, "passive ");
788 if (tz->state.active)
789 seq_printf(seq, "active[%d]", tz->state.active_index);
790 seq_puts(seq, "\n");
791 }
792
Len Brown4be44fc2005-08-05 00:44:28 -0400793 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400794 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796
797static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
798{
799 return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
800}
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
803{
Len Brown4be44fc2005-08-05 00:44:28 -0400804 int result = 0;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200805 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 if (!tz)
809 goto end;
810
811 result = acpi_thermal_get_temperature(tz);
812 if (result)
813 goto end;
814
Len Brown4be44fc2005-08-05 00:44:28 -0400815 seq_printf(seq, "temperature: %ld C\n",
816 KELVIN_TO_CELSIUS(tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Len Brown4be44fc2005-08-05 00:44:28 -0400818 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400819 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
822static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
823{
824 return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
825}
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
828{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200829 struct acpi_thermal *tz = seq->private;
Thomas Renninger68ccfaa2006-11-19 23:01:40 +0100830 struct acpi_device *device;
Len Brown4be44fc2005-08-05 00:44:28 -0400831 int i = 0;
832 int j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 if (!tz)
836 goto end;
837
838 if (tz->trips.critical.flags.valid)
839 seq_printf(seq, "critical (S5): %ld C\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400840 KELVIN_TO_CELSIUS(tz->trips.critical.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 if (tz->trips.hot.flags.valid)
843 seq_printf(seq, "hot (S4): %ld C\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400844 KELVIN_TO_CELSIUS(tz->trips.hot.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 if (tz->trips.passive.flags.valid) {
Len Brown4be44fc2005-08-05 00:44:28 -0400847 seq_printf(seq,
848 "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
849 KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
850 tz->trips.passive.tc1, tz->trips.passive.tc2,
851 tz->trips.passive.tsp);
852 for (j = 0; j < tz->trips.passive.devices.count; j++) {
Thomas Renninger68ccfaa2006-11-19 23:01:40 +0100853 acpi_bus_get_device(tz->trips.passive.devices.handles[j], &device);
854 seq_printf(seq, "%4.4s ", acpi_device_bid(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856 seq_puts(seq, "\n");
857 }
858
859 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
860 if (!(tz->trips.active[i].flags.valid))
861 break;
862 seq_printf(seq, "active[%d]: %ld C: devices=",
Len Brown4be44fc2005-08-05 00:44:28 -0400863 i,
864 KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
Thomas Renninger68ccfaa2006-11-19 23:01:40 +0100865 for (j = 0; j < tz->trips.active[i].devices.count; j++){
866 acpi_bus_get_device(tz->trips.active[i].devices.handles[j], &device);
867 seq_printf(seq, "%4.4s ", acpi_device_bid(device));
868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 seq_puts(seq, "\n");
870 }
871
Len Brown4be44fc2005-08-05 00:44:28 -0400872 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400873 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
876static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
877{
878 return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
879}
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
882{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200883 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 if (!tz)
887 goto end;
888
Len Browneaca2d32007-04-30 23:27:43 -0400889 if (!tz->flags.cooling_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 seq_puts(seq, "<setting not supported>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 else
Len Browneaca2d32007-04-30 23:27:43 -0400892 seq_puts(seq, "0 - Active; 1 - Passive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Len Brown4be44fc2005-08-05 00:44:28 -0400894 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400895 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896}
897
898static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
899{
900 return single_open(file, acpi_thermal_cooling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -0400901 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
904static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -0400905acpi_thermal_write_cooling_mode(struct file *file,
906 const char __user * buffer,
907 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200909 struct seq_file *m = file->private_data;
910 struct acpi_thermal *tz = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400911 int result = 0;
912 char mode_string[12] = { '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 if (!tz || (count > sizeof(mode_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400916 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 if (!tz->flags.cooling_mode)
Patrick Mocheld550d982006-06-27 00:41:40 -0400919 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 if (copy_from_user(mode_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -0400922 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -0400923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 mode_string[count] = '\0';
Len Brown4be44fc2005-08-05 00:44:28 -0400925
926 result = acpi_thermal_set_cooling_mode(tz,
927 simple_strtoul(mode_string, NULL,
928 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400930 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 acpi_thermal_check(tz);
933
Patrick Mocheld550d982006-06-27 00:41:40 -0400934 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
938{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200939 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 if (!tz)
943 goto end;
944
945 if (!tz->polling_frequency) {
946 seq_puts(seq, "<polling disabled>\n");
947 goto end;
948 }
949
950 seq_printf(seq, "polling frequency: %lu seconds\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400951 (tz->polling_frequency / 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Len Brown4be44fc2005-08-05 00:44:28 -0400953 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400954 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
957static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
958{
959 return single_open(file, acpi_thermal_polling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -0400960 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
963static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -0400964acpi_thermal_write_polling(struct file *file,
965 const char __user * buffer,
966 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200968 struct seq_file *m = file->private_data;
969 struct acpi_thermal *tz = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400970 int result = 0;
971 char polling_string[12] = { '\0' };
972 int seconds = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 if (!tz || (count > sizeof(polling_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400976 return -EINVAL;
Len Brown4be44fc2005-08-05 00:44:28 -0400977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 if (copy_from_user(polling_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -0400979 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -0400980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 polling_string[count] = '\0';
982
983 seconds = simple_strtoul(polling_string, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -0400984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 result = acpi_thermal_set_polling(tz, seconds);
986 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400987 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 acpi_thermal_check(tz);
990
Patrick Mocheld550d982006-06-27 00:41:40 -0400991 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
Len Brown4be44fc2005-08-05 00:44:28 -0400994static int acpi_thermal_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
Len Brown4be44fc2005-08-05 00:44:28 -0400996 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 if (!acpi_device_dir(device)) {
1000 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -04001001 acpi_thermal_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001003 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 acpi_device_dir(device)->owner = THIS_MODULE;
1005 }
1006
1007 /* 'state' [R] */
1008 entry = create_proc_entry(ACPI_THERMAL_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -04001009 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001011 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 else {
1013 entry->proc_fops = &acpi_thermal_state_fops;
1014 entry->data = acpi_driver_data(device);
1015 entry->owner = THIS_MODULE;
1016 }
1017
1018 /* 'temperature' [R] */
1019 entry = create_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
Len Brown4be44fc2005-08-05 00:44:28 -04001020 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001022 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 else {
1024 entry->proc_fops = &acpi_thermal_temp_fops;
1025 entry->data = acpi_driver_data(device);
1026 entry->owner = THIS_MODULE;
1027 }
1028
1029 /* 'trip_points' [R/W] */
1030 entry = create_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
Len Brown4be44fc2005-08-05 00:44:28 -04001031 S_IFREG | S_IRUGO | S_IWUSR,
1032 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001034 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 else {
1036 entry->proc_fops = &acpi_thermal_trip_fops;
1037 entry->data = acpi_driver_data(device);
1038 entry->owner = THIS_MODULE;
1039 }
1040
1041 /* 'cooling_mode' [R/W] */
1042 entry = create_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
Len Brown4be44fc2005-08-05 00:44:28 -04001043 S_IFREG | S_IRUGO | S_IWUSR,
1044 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001046 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 else {
1048 entry->proc_fops = &acpi_thermal_cooling_fops;
1049 entry->data = acpi_driver_data(device);
1050 entry->owner = THIS_MODULE;
1051 }
1052
1053 /* 'polling_frequency' [R/W] */
1054 entry = create_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
Len Brown4be44fc2005-08-05 00:44:28 -04001055 S_IFREG | S_IRUGO | S_IWUSR,
1056 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001058 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 else {
1060 entry->proc_fops = &acpi_thermal_polling_fops;
1061 entry->data = acpi_driver_data(device);
1062 entry->owner = THIS_MODULE;
1063 }
1064
Patrick Mocheld550d982006-06-27 00:41:40 -04001065 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
Len Brown4be44fc2005-08-05 00:44:28 -04001068static int acpi_thermal_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 if (acpi_device_dir(device)) {
1072 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1073 acpi_device_dir(device));
1074 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1075 acpi_device_dir(device));
1076 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1077 acpi_device_dir(device));
1078 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1079 acpi_device_dir(device));
1080 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1081 acpi_device_dir(device));
1082 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1083 acpi_device_dir(device) = NULL;
1084 }
1085
Patrick Mocheld550d982006-06-27 00:41:40 -04001086 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089/* --------------------------------------------------------------------------
1090 Driver Interface
1091 -------------------------------------------------------------------------- */
1092
Len Brown4be44fc2005-08-05 00:44:28 -04001093static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001095 struct acpi_thermal *tz = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001096 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001100 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Patrick Mochel8348e1b2006-05-19 16:54:40 -04001102 device = tz->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 switch (event) {
1105 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1106 acpi_thermal_check(tz);
1107 break;
1108 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
1109 acpi_thermal_get_trip_points(tz);
1110 acpi_thermal_check(tz);
1111 acpi_bus_generate_event(device, event, 0);
1112 break;
1113 case ACPI_THERMAL_NOTIFY_DEVICES:
1114 if (tz->flags.devices)
1115 acpi_thermal_get_devices(tz);
1116 acpi_bus_generate_event(device, event, 0);
1117 break;
1118 default:
1119 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001120 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 break;
1122 }
1123
Patrick Mocheld550d982006-06-27 00:41:40 -04001124 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125}
1126
Len Brown4be44fc2005-08-05 00:44:28 -04001127static int acpi_thermal_get_info(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
Len Brown4be44fc2005-08-05 00:44:28 -04001129 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001133 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 /* Get temperature [_TMP] (required) */
1136 result = acpi_thermal_get_temperature(tz);
1137 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001138 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 /* Get trip points [_CRT, _PSV, etc.] (required) */
1141 result = acpi_thermal_get_trip_points(tz);
1142 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001143 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 /* Set the cooling mode [_SCP] to active cooling (default) */
1146 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
Len Brown4be44fc2005-08-05 00:44:28 -04001147 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 tz->flags.cooling_mode = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 /* Get default polling frequency [_TZP] (optional) */
1151 if (tzp)
1152 tz->polling_frequency = tzp;
1153 else
1154 acpi_thermal_get_polling_frequency(tz);
1155
1156 /* Get devices in this thermal zone [_TZD] (optional) */
1157 result = acpi_thermal_get_devices(tz);
1158 if (!result)
1159 tz->flags.devices = 1;
1160
Patrick Mocheld550d982006-06-27 00:41:40 -04001161 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
Len Brown4be44fc2005-08-05 00:44:28 -04001164static int acpi_thermal_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
Len Brown4be44fc2005-08-05 00:44:28 -04001166 int result = 0;
1167 acpi_status status = AE_OK;
1168 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001172 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Burman Yan36bcbec2006-12-19 12:56:11 -08001174 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001176 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Patrick Mochel8348e1b2006-05-19 16:54:40 -04001178 tz->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 strcpy(tz->name, device->pnp.bus_id);
1180 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1181 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1182 acpi_driver_data(device) = tz;
1183
1184 result = acpi_thermal_get_info(tz);
1185 if (result)
1186 goto end;
1187
1188 result = acpi_thermal_add_fs(device);
1189 if (result)
Vasily Averin09047e72006-04-27 05:25:00 -04001190 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 init_timer(&tz->timer);
1193
1194 acpi_thermal_check(tz);
1195
Patrick Mochel38ba7c92006-05-19 16:54:48 -04001196 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001197 ACPI_DEVICE_NOTIFY,
1198 acpi_thermal_notify, tz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 result = -ENODEV;
1201 goto end;
1202 }
1203
1204 printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001205 acpi_device_name(device), acpi_device_bid(device),
1206 KELVIN_TO_CELSIUS(tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Len Brown4be44fc2005-08-05 00:44:28 -04001208 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 if (result) {
1210 acpi_thermal_remove_fs(device);
1211 kfree(tz);
1212 }
1213
Patrick Mocheld550d982006-06-27 00:41:40 -04001214 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
Len Brown4be44fc2005-08-05 00:44:28 -04001217static int acpi_thermal_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
Len Brown4be44fc2005-08-05 00:44:28 -04001219 acpi_status status = AE_OK;
1220 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001224 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001226 tz = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 /* avoid timer adding new defer task */
1229 tz->zombie = 1;
1230 /* wait for running timer (on other CPUs) finish */
1231 del_timer_sync(&(tz->timer));
1232 /* synchronize deferred task */
1233 acpi_os_wait_events_complete(NULL);
1234 /* deferred task may reinsert timer */
1235 del_timer_sync(&(tz->timer));
1236
Patrick Mochel38ba7c92006-05-19 16:54:48 -04001237 status = acpi_remove_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001238 ACPI_DEVICE_NOTIFY,
1239 acpi_thermal_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 /* Terminate policy */
Len Brown4be44fc2005-08-05 00:44:28 -04001242 if (tz->trips.passive.flags.valid && tz->trips.passive.flags.enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 tz->trips.passive.flags.enabled = 0;
1244 acpi_thermal_passive(tz);
1245 }
1246 if (tz->trips.active[0].flags.valid
Len Brown4be44fc2005-08-05 00:44:28 -04001247 && tz->trips.active[0].flags.enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 tz->trips.active[0].flags.enabled = 0;
1249 acpi_thermal_active(tz);
1250 }
1251
1252 acpi_thermal_remove_fs(device);
1253
1254 kfree(tz);
Patrick Mocheld550d982006-06-27 00:41:40 -04001255 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256}
1257
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001258static int acpi_thermal_resume(struct acpi_device *device)
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001259{
1260 struct acpi_thermal *tz = NULL;
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001261 int i, j, power_state, result;
1262
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001263
1264 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001265 return -EINVAL;
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001266
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001267 tz = acpi_driver_data(device);
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001268
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001269 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001270 if (!(&tz->trips.active[i]))
1271 break;
1272 if (!tz->trips.active[i].flags.valid)
1273 break;
1274 tz->trips.active[i].flags.enabled = 1;
1275 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1276 result = acpi_bus_get_power(tz->trips.active[i].devices.
1277 handles[j], &power_state);
1278 if (result || (power_state != ACPI_STATE_D0)) {
1279 tz->trips.active[i].flags.enabled = 0;
1280 break;
1281 }
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001282 }
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001283 tz->state.active |= tz->trips.active[i].flags.enabled;
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001284 }
1285
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001286 acpi_thermal_check(tz);
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001287
1288 return AE_OK;
1289}
1290
Len Brown4be44fc2005-08-05 00:44:28 -04001291static int __init acpi_thermal_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
Len Brown4be44fc2005-08-05 00:44:28 -04001293 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 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);