blob: 74e25be8abc9ad0eaa132318461da72f67ab6b91 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * acpi_thermal.c - ACPI Thermal Zone Driver ($Revision: 41 $)
3 *
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6 *
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 *
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 *
25 * This driver fully implements the ACPI thermal policy as described in the
26 * ACPI 2.0 Specification.
27 *
28 * TBD: 1. Implement passive cooling hysteresis.
29 * 2. Enhance passive cooling (CPU) states/limit interface to support
30 * concepts of 'multiple limiters', upper/lower limits, etc.
31 *
32 */
33
34#include <linux/kernel.h>
35#include <linux/module.h>
36#include <linux/init.h>
37#include <linux/types.h>
38#include <linux/proc_fs.h>
Tim Schmielaucd354f12007-02-14 00:33:14 -080039#include <linux/timer.h>
40#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/kmod.h>
42#include <linux/seq_file.h>
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -070043#include <linux/reboot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/uaccess.h>
45
46#include <acpi/acpi_bus.h>
47#include <acpi/acpi_drivers.h>
48
49#define ACPI_THERMAL_COMPONENT 0x04000000
50#define ACPI_THERMAL_CLASS "thermal_zone"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
52#define ACPI_THERMAL_FILE_STATE "state"
53#define ACPI_THERMAL_FILE_TEMPERATURE "temperature"
54#define ACPI_THERMAL_FILE_TRIP_POINTS "trip_points"
55#define ACPI_THERMAL_FILE_COOLING_MODE "cooling_mode"
56#define ACPI_THERMAL_FILE_POLLING_FREQ "polling_frequency"
57#define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
58#define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
59#define ACPI_THERMAL_NOTIFY_DEVICES 0x82
60#define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
61#define ACPI_THERMAL_NOTIFY_HOT 0xF1
62#define ACPI_THERMAL_MODE_ACTIVE 0x00
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64#define ACPI_THERMAL_MAX_ACTIVE 10
65#define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
66
67#define KELVIN_TO_CELSIUS(t) (long)(((long)t-2732>=0) ? ((long)t-2732+5)/10 : ((long)t-2732-5)/10)
68#define CELSIUS_TO_KELVIN(t) ((t+273)*10)
69
70#define _COMPONENT ACPI_THERMAL_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050071ACPI_MODULE_NAME("thermal");
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Thomas Renninger1cbf4c52004-09-16 11:07:00 -040073MODULE_AUTHOR("Paul Diefenbaugh");
Len Brown7cda93e2007-02-12 23:50:02 -050074MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070075MODULE_LICENSE("GPL");
76
77static int tzp;
Len Brown730ff342007-08-12 00:12:26 -040078module_param(tzp, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.\n");
80
Len Brown72b33ef2007-08-12 00:12:17 -040081static int off;
82module_param(off, int, 0);
83MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.\n");
84
Len Browna70cdc52007-08-12 00:12:35 -040085static int psv;
86module_param(psv, int, 0644);
87MODULE_PARM_DESC(psv, "Disable or override all passive trip points.\n");
88
Len Brown4be44fc2005-08-05 00:44:28 -040089static int acpi_thermal_add(struct acpi_device *device);
90static int acpi_thermal_remove(struct acpi_device *device, int type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +080091static int acpi_thermal_resume(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
93static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
94static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -040096static ssize_t acpi_thermal_write_cooling_mode(struct file *,
97 const char __user *, size_t,
98 loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -0400100static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
101 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200103static const struct acpi_device_id thermal_device_ids[] = {
104 {ACPI_THERMAL_HID, 0},
105 {"", 0},
106};
107MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static struct acpi_driver acpi_thermal_driver = {
Len Brownc2b67052007-02-12 23:33:40 -0500110 .name = "thermal",
Len Brown4be44fc2005-08-05 00:44:28 -0400111 .class = ACPI_THERMAL_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200112 .ids = thermal_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -0400113 .ops = {
114 .add = acpi_thermal_add,
115 .remove = acpi_thermal_remove,
Konstantin Karasyov74ce14682006-05-08 08:32:00 -0400116 .resume = acpi_thermal_resume,
Len Brown4be44fc2005-08-05 00:44:28 -0400117 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118};
119
120struct acpi_thermal_state {
Len Brown4be44fc2005-08-05 00:44:28 -0400121 u8 critical:1;
122 u8 hot:1;
123 u8 passive:1;
124 u8 active:1;
125 u8 reserved:4;
126 int active_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
129struct acpi_thermal_state_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400130 u8 valid:1;
131 u8 enabled:1;
132 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133};
134
135struct acpi_thermal_critical {
136 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400137 unsigned long temperature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
140struct acpi_thermal_hot {
141 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400142 unsigned long temperature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143};
144
145struct acpi_thermal_passive {
146 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400147 unsigned long temperature;
148 unsigned long tc1;
149 unsigned long tc2;
150 unsigned long tsp;
151 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152};
153
154struct acpi_thermal_active {
155 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400156 unsigned long temperature;
157 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158};
159
160struct acpi_thermal_trips {
161 struct acpi_thermal_critical critical;
Len Brown4be44fc2005-08-05 00:44:28 -0400162 struct acpi_thermal_hot hot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 struct acpi_thermal_passive passive;
164 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
165};
166
167struct acpi_thermal_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400168 u8 cooling_mode:1; /* _SCP */
169 u8 devices:1; /* _TZD */
170 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171};
172
173struct acpi_thermal {
Patrick Mochel8348e1b2006-05-19 16:54:40 -0400174 struct acpi_device * device;
Len Brown4be44fc2005-08-05 00:44:28 -0400175 acpi_bus_id name;
176 unsigned long temperature;
177 unsigned long last_temperature;
178 unsigned long polling_frequency;
Len Brown4be44fc2005-08-05 00:44:28 -0400179 volatile u8 zombie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 struct acpi_thermal_flags flags;
181 struct acpi_thermal_state state;
182 struct acpi_thermal_trips trips;
Len Brown4be44fc2005-08-05 00:44:28 -0400183 struct acpi_handle_list devices;
184 struct timer_list timer;
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_state_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400188 .open = acpi_thermal_state_open_fs,
189 .read = seq_read,
190 .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_temp_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400195 .open = acpi_thermal_temp_open_fs,
196 .read = seq_read,
197 .llseek = seq_lseek,
198 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199};
200
Arjan van de Vend7508032006-07-04 13:06:00 -0400201static const struct file_operations acpi_thermal_trip_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400202 .open = acpi_thermal_trip_open_fs,
203 .read = seq_read,
Len Brown4be44fc2005-08-05 00:44:28 -0400204 .llseek = seq_lseek,
205 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206};
207
Arjan van de Vend7508032006-07-04 13:06:00 -0400208static const struct file_operations acpi_thermal_cooling_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400209 .open = acpi_thermal_cooling_open_fs,
210 .read = seq_read,
211 .write = acpi_thermal_write_cooling_mode,
212 .llseek = seq_lseek,
213 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214};
215
Arjan van de Vend7508032006-07-04 13:06:00 -0400216static const struct file_operations acpi_thermal_polling_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400217 .open = acpi_thermal_polling_open_fs,
218 .read = seq_read,
219 .write = acpi_thermal_write_polling,
220 .llseek = seq_lseek,
221 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222};
223
224/* --------------------------------------------------------------------------
225 Thermal Zone Management
226 -------------------------------------------------------------------------- */
227
Len Brown4be44fc2005-08-05 00:44:28 -0400228static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Len Brown4be44fc2005-08-05 00:44:28 -0400230 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400234 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 tz->last_temperature = tz->temperature;
237
Len Brown4be44fc2005-08-05 00:44:28 -0400238 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400239 acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tz->temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400241 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Len Brown4be44fc2005-08-05 00:44:28 -0400243 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
244 tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Patrick Mocheld550d982006-06-27 00:41:40 -0400246 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
Len Brown4be44fc2005-08-05 00:44:28 -0400249static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Len Brown4be44fc2005-08-05 00:44:28 -0400251 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400255 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Len Brown4be44fc2005-08-05 00:44:28 -0400257 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400258 acpi_evaluate_integer(tz->device->handle, "_TZP", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400259 &tz->polling_frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400261 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Len Brown4be44fc2005-08-05 00:44:28 -0400263 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
264 tz->polling_frequency));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Patrick Mocheld550d982006-06-27 00:41:40 -0400266 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
Len Brown4be44fc2005-08-05 00:44:28 -0400269static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400273 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 tz->polling_frequency = seconds * 10; /* Convert value to deci-seconds */
276
Len Brown4be44fc2005-08-05 00:44:28 -0400277 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
278 "Polling frequency set to %lu seconds\n",
Sanjoy Mahajan636cedf2007-02-16 01:24:43 -0500279 tz->polling_frequency/10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Patrick Mocheld550d982006-06-27 00:41:40 -0400281 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
Len Brown4be44fc2005-08-05 00:44:28 -0400284static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Len Brown4be44fc2005-08-05 00:44:28 -0400286 acpi_status status = AE_OK;
287 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
288 struct acpi_object_list arg_list = { 1, &arg0 };
289 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400293 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400295 status = acpi_get_handle(tz->device->handle, "_SCP", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (ACPI_FAILURE(status)) {
297 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400298 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300
301 arg0.integer.value = mode;
302
303 status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
304 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400305 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Patrick Mocheld550d982006-06-27 00:41:40 -0400307 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
Len Brown4be44fc2005-08-05 00:44:28 -0400310static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Len Brown4be44fc2005-08-05 00:44:28 -0400312 acpi_status status = AE_OK;
313 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400317 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 /* Critical Shutdown (required) */
320
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400321 status = acpi_evaluate_integer(tz->device->handle, "_CRT", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400322 &tz->trips.critical.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (ACPI_FAILURE(status)) {
324 tz->trips.critical.flags.valid = 0;
Thomas Renningera6fc6722006-06-26 23:58:43 -0400325 ACPI_EXCEPTION((AE_INFO, status, "No critical threshold"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400326 return -ENODEV;
Len Brown4be44fc2005-08-05 00:44:28 -0400327 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 tz->trips.critical.flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400329 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
330 "Found critical threshold [%lu]\n",
331 tz->trips.critical.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
333
334 /* Critical Sleep (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, "_HOT", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400338 &tz->trips.hot.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (ACPI_FAILURE(status)) {
340 tz->trips.hot.flags.valid = 0;
341 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No hot threshold\n"));
Len Brown4be44fc2005-08-05 00:44:28 -0400342 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 tz->trips.hot.flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400344 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found hot threshold [%lu]\n",
345 tz->trips.hot.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347
348 /* Passive: Processors (optional) */
349
Len Browna70cdc52007-08-12 00:12:35 -0400350 if (psv == -1) {
351 status = AE_SUPPORT;
352 } else if (psv > 0) {
353 tz->trips.passive.temperature = CELSIUS_TO_KELVIN(psv);
354 status = AE_OK;
355 } else {
356 status = acpi_evaluate_integer(tz->device->handle,
357 "_PSV", NULL, &tz->trips.passive.temperature);
358 }
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (ACPI_FAILURE(status)) {
361 tz->trips.passive.flags.valid = 0;
362 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No passive threshold\n"));
Len Brown4be44fc2005-08-05 00:44:28 -0400363 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 tz->trips.passive.flags.valid = 1;
365
Len Brown4be44fc2005-08-05 00:44:28 -0400366 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400367 acpi_evaluate_integer(tz->device->handle, "_TC1", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400368 &tz->trips.passive.tc1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (ACPI_FAILURE(status))
370 tz->trips.passive.flags.valid = 0;
371
Len Brown4be44fc2005-08-05 00:44:28 -0400372 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400373 acpi_evaluate_integer(tz->device->handle, "_TC2", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400374 &tz->trips.passive.tc2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (ACPI_FAILURE(status))
376 tz->trips.passive.flags.valid = 0;
377
Len Brown4be44fc2005-08-05 00:44:28 -0400378 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400379 acpi_evaluate_integer(tz->device->handle, "_TSP", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400380 &tz->trips.passive.tsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (ACPI_FAILURE(status))
382 tz->trips.passive.flags.valid = 0;
383
Len Brown4be44fc2005-08-05 00:44:28 -0400384 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400385 acpi_evaluate_reference(tz->device->handle, "_PSL", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400386 &tz->trips.passive.devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (ACPI_FAILURE(status))
388 tz->trips.passive.flags.valid = 0;
389
390 if (!tz->trips.passive.flags.valid)
Len Browncece9292006-06-26 23:04:31 -0400391 printk(KERN_WARNING PREFIX "Invalid passive threshold\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 else
Len Brown4be44fc2005-08-05 00:44:28 -0400393 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
394 "Found passive threshold [%lu]\n",
395 tz->trips.passive.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
397
398 /* Active: Fans, etc. (optional) */
399
Len Brown4be44fc2005-08-05 00:44:28 -0400400 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Len Brown4be44fc2005-08-05 00:44:28 -0400402 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Len Brown4be44fc2005-08-05 00:44:28 -0400404 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400405 acpi_evaluate_integer(tz->device->handle, name, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400406 &tz->trips.active[i].temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (ACPI_FAILURE(status))
408 break;
409
410 name[2] = 'L';
Len Brown4be44fc2005-08-05 00:44:28 -0400411 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400412 acpi_evaluate_reference(tz->device->handle, name, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400413 &tz->trips.active[i].devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (ACPI_SUCCESS(status)) {
415 tz->trips.active[i].flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400416 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
417 "Found active threshold [%d]:[%lu]\n",
418 i, tz->trips.active[i].temperature));
419 } else
Thomas Renningera6fc6722006-06-26 23:58:43 -0400420 ACPI_EXCEPTION((AE_INFO, status,
421 "Invalid active threshold [%d]", i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423
Patrick Mocheld550d982006-06-27 00:41:40 -0400424 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
Len Brown4be44fc2005-08-05 00:44:28 -0400427static int acpi_thermal_get_devices(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Len Brown4be44fc2005-08-05 00:44:28 -0400429 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400433 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Len Brown4be44fc2005-08-05 00:44:28 -0400435 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400436 acpi_evaluate_reference(tz->device->handle, "_TZD", NULL, &tz->devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400438 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Patrick Mocheld550d982006-06-27 00:41:40 -0400440 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
Len Brown4be44fc2005-08-05 00:44:28 -0400443static int acpi_thermal_critical(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (!tz || !tz->trips.critical.flags.valid)
Patrick Mocheld550d982006-06-27 00:41:40 -0400446 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 if (tz->temperature >= tz->trips.critical.temperature) {
Len Browncece9292006-06-26 23:04:31 -0400449 printk(KERN_WARNING PREFIX "Critical trip point\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 tz->trips.critical.flags.enabled = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400451 } else if (tz->trips.critical.flags.enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 tz->trips.critical.flags.enabled = 0;
453
Len Brown4be44fc2005-08-05 00:44:28 -0400454 printk(KERN_EMERG
455 "Critical temperature reached (%ld C), shutting down.\n",
456 KELVIN_TO_CELSIUS(tz->temperature));
Patrick Mochel8348e1b2006-05-19 16:54:40 -0400457 acpi_bus_generate_event(tz->device, ACPI_THERMAL_NOTIFY_CRITICAL,
Len Brown4be44fc2005-08-05 00:44:28 -0400458 tz->trips.critical.flags.enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -0700460 orderly_poweroff(true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Patrick Mocheld550d982006-06-27 00:41:40 -0400462 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
Len Brown4be44fc2005-08-05 00:44:28 -0400465static int acpi_thermal_hot(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (!tz || !tz->trips.hot.flags.valid)
Patrick Mocheld550d982006-06-27 00:41:40 -0400468 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 if (tz->temperature >= tz->trips.hot.temperature) {
Len Browncece9292006-06-26 23:04:31 -0400471 printk(KERN_WARNING PREFIX "Hot trip point\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 tz->trips.hot.flags.enabled = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400473 } else if (tz->trips.hot.flags.enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 tz->trips.hot.flags.enabled = 0;
475
Patrick Mochel8348e1b2006-05-19 16:54:40 -0400476 acpi_bus_generate_event(tz->device, ACPI_THERMAL_NOTIFY_HOT,
Len Brown4be44fc2005-08-05 00:44:28 -0400477 tz->trips.hot.flags.enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 /* TBD: Call user-mode "sleep(S4)" function */
480
Patrick Mocheld550d982006-06-27 00:41:40 -0400481 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400484static void acpi_thermal_passive(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400486 int result = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 struct acpi_thermal_passive *passive = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400488 int trend = 0;
489 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 if (!tz || !tz->trips.passive.flags.valid)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400493 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 passive = &(tz->trips.passive);
496
497 /*
498 * Above Trip?
499 * -----------
500 * Calculate the thermal trend (using the passive cooling equation)
501 * and modify the performance limit for all passive cooling devices
502 * accordingly. Note that we assume symmetry.
503 */
504 if (tz->temperature >= passive->temperature) {
Len Brown4be44fc2005-08-05 00:44:28 -0400505 trend =
506 (passive->tc1 * (tz->temperature - tz->last_temperature)) +
507 (passive->tc2 * (tz->temperature - passive->temperature));
508 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
509 "trend[%d]=(tc1[%lu]*(tmp[%lu]-last[%lu]))+(tc2[%lu]*(tmp[%lu]-psv[%lu]))\n",
510 trend, passive->tc1, tz->temperature,
511 tz->last_temperature, passive->tc2,
512 tz->temperature, passive->temperature));
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400513 passive->flags.enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 /* Heating up? */
515 if (trend > 0)
Len Brown4be44fc2005-08-05 00:44:28 -0400516 for (i = 0; i < passive->devices.count; i++)
517 acpi_processor_set_thermal_limit(passive->
518 devices.
519 handles[i],
520 ACPI_PROCESSOR_LIMIT_INCREMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 /* Cooling off? */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400522 else if (trend < 0) {
Len Brown4be44fc2005-08-05 00:44:28 -0400523 for (i = 0; i < passive->devices.count; i++)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400524 /*
525 * assume that we are on highest
526 * freq/lowest thrott and can leave
527 * passive mode, even in error case
528 */
529 if (!acpi_processor_set_thermal_limit
530 (passive->devices.handles[i],
531 ACPI_PROCESSOR_LIMIT_DECREMENT))
532 result = 0;
533 /*
534 * Leave cooling mode, even if the temp might
535 * higher than trip point This is because some
536 * machines might have long thermal polling
537 * frequencies (tsp) defined. We will fall back
538 * into passive mode in next cycle (probably quicker)
539 */
540 if (result) {
541 passive->flags.enabled = 0;
542 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
543 "Disabling passive cooling, still above threshold,"
544 " but we are cooling down\n"));
545 }
546 }
547 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549
550 /*
551 * Below Trip?
552 * -----------
553 * Implement passive cooling hysteresis to slowly increase performance
554 * and avoid thrashing around the passive trip point. Note that we
555 * assume symmetry.
556 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400557 if (!passive->flags.enabled)
558 return;
559 for (i = 0; i < passive->devices.count; i++)
560 if (!acpi_processor_set_thermal_limit
561 (passive->devices.handles[i],
562 ACPI_PROCESSOR_LIMIT_DECREMENT))
563 result = 0;
564 if (result) {
565 passive->flags.enabled = 0;
566 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
567 "Disabling passive cooling (zone is cool)\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400571static void acpi_thermal_active(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Len Brown4be44fc2005-08-05 00:44:28 -0400573 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 struct acpi_thermal_active *active = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400575 int i = 0;
576 int j = 0;
577 unsigned long maxtemp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 if (!tz)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400581 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Len Brown4be44fc2005-08-05 00:44:28 -0400583 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 active = &(tz->trips.active[i]);
585 if (!active || !active->flags.valid)
586 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (tz->temperature >= active->temperature) {
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400588 /*
589 * Above Threshold?
590 * ----------------
591 * If not already enabled, turn ON all cooling devices
592 * associated with this active threshold.
593 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (active->temperature > maxtemp)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400595 tz->state.active_index = i;
596 maxtemp = active->temperature;
597 if (active->flags.enabled)
598 continue;
599 for (j = 0; j < active->devices.count; j++) {
600 result =
601 acpi_bus_set_power(active->devices.
602 handles[j],
603 ACPI_STATE_D0);
604 if (result) {
Len Browncece9292006-06-26 23:04:31 -0400605 printk(KERN_WARNING PREFIX
606 "Unable to turn cooling device [%p] 'on'\n",
Thomas Renningera6fc6722006-06-26 23:58:43 -0400607 active->devices.
Len Browncece9292006-06-26 23:04:31 -0400608 handles[j]);
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400609 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400611 active->flags.enabled = 1;
612 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
613 "Cooling device [%p] now 'on'\n",
614 active->devices.handles[j]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400616 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400618 if (!active->flags.enabled)
619 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 /*
621 * Below Threshold?
622 * ----------------
623 * Turn OFF all cooling devices associated with this
624 * threshold.
625 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400626 for (j = 0; j < active->devices.count; j++) {
627 result = acpi_bus_set_power(active->devices.handles[j],
628 ACPI_STATE_D3);
629 if (result) {
Len Browncece9292006-06-26 23:04:31 -0400630 printk(KERN_WARNING PREFIX
631 "Unable to turn cooling device [%p] 'off'\n",
632 active->devices.handles[j]);
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400633 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400635 active->flags.enabled = 0;
636 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
637 "Cooling device [%p] now 'off'\n",
638 active->devices.handles[j]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
Len Brown4be44fc2005-08-05 00:44:28 -0400643static void acpi_thermal_check(void *context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Len Brown4be44fc2005-08-05 00:44:28 -0400645static void acpi_thermal_run(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 struct acpi_thermal *tz = (struct acpi_thermal *)data;
648 if (!tz->zombie)
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400649 acpi_os_execute(OSL_GPE_HANDLER, acpi_thermal_check, (void *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
Len Brown4be44fc2005-08-05 00:44:28 -0400652static void acpi_thermal_check(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Len Brown4be44fc2005-08-05 00:44:28 -0400654 int result = 0;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200655 struct acpi_thermal *tz = data;
Len Brown4be44fc2005-08-05 00:44:28 -0400656 unsigned long sleep_time = 0;
657 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 struct acpi_thermal_state state;
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 if (!tz) {
Len Brown64684632006-06-26 23:41:38 -0400662 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400663 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
665
666 state = tz->state;
667
668 result = acpi_thermal_get_temperature(tz);
669 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400670 return;
Len Brown4be44fc2005-08-05 00:44:28 -0400671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 memset(&tz->state, 0, sizeof(tz->state));
Len Brown4be44fc2005-08-05 00:44:28 -0400673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 /*
675 * Check Trip Points
676 * -----------------
677 * Compare the current temperature to the trip point values to see
678 * if we've entered one of the thermal policy states. Note that
679 * this function determines when a state is entered, but the
680 * individual policy decides when it is exited (e.g. hysteresis).
681 */
682 if (tz->trips.critical.flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400683 state.critical |=
684 (tz->temperature >= tz->trips.critical.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (tz->trips.hot.flags.valid)
686 state.hot |= (tz->temperature >= tz->trips.hot.temperature);
687 if (tz->trips.passive.flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400688 state.passive |=
689 (tz->temperature >= tz->trips.passive.temperature);
690 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (tz->trips.active[i].flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400692 state.active |=
693 (tz->temperature >=
694 tz->trips.active[i].temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 /*
697 * Invoke Policy
698 * -------------
699 * Separated from the above check to allow individual policy to
700 * determine when to exit a given state.
701 */
702 if (state.critical)
703 acpi_thermal_critical(tz);
704 if (state.hot)
705 acpi_thermal_hot(tz);
706 if (state.passive)
707 acpi_thermal_passive(tz);
708 if (state.active)
709 acpi_thermal_active(tz);
710
711 /*
712 * Calculate State
713 * ---------------
714 * Again, separated from the above two to allow independent policy
715 * decisions.
716 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400717 tz->state.critical = tz->trips.critical.flags.enabled;
718 tz->state.hot = tz->trips.hot.flags.enabled;
719 tz->state.passive = tz->trips.passive.flags.enabled;
720 tz->state.active = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400721 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400722 tz->state.active |= tz->trips.active[i].flags.enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 /*
725 * Calculate Sleep Time
726 * --------------------
727 * If we're in the passive state, use _TSP's value. Otherwise
728 * use the default polling frequency (e.g. _TZP). If no polling
729 * frequency is specified then we'll wait forever (at least until
730 * a thermal event occurs). Note that _TSP and _TZD values are
731 * given in 1/10th seconds (we must covert to milliseconds).
732 */
733 if (tz->state.passive)
734 sleep_time = tz->trips.passive.tsp * 100;
735 else if (tz->polling_frequency > 0)
736 sleep_time = tz->polling_frequency * 100;
737
Len Brown4be44fc2005-08-05 00:44:28 -0400738 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: temperature[%lu] sleep[%lu]\n",
739 tz->name, tz->temperature, sleep_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 /*
742 * Schedule Next Poll
743 * ------------------
744 */
745 if (!sleep_time) {
746 if (timer_pending(&(tz->timer)))
747 del_timer(&(tz->timer));
Len Brown4be44fc2005-08-05 00:44:28 -0400748 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (timer_pending(&(tz->timer)))
Andrew Morton94e22e12007-04-23 14:41:13 -0700750 mod_timer(&(tz->timer),
751 jiffies + (HZ * sleep_time) / 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400753 tz->timer.data = (unsigned long)tz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 tz->timer.function = acpi_thermal_run;
755 tz->timer.expires = jiffies + (HZ * sleep_time) / 1000;
756 add_timer(&(tz->timer));
757 }
758 }
759
Patrick Mocheld550d982006-06-27 00:41:40 -0400760 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763/* --------------------------------------------------------------------------
764 FS Interface (/proc)
765 -------------------------------------------------------------------------- */
766
Len Brown4be44fc2005-08-05 00:44:28 -0400767static struct proc_dir_entry *acpi_thermal_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
770{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200771 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 if (!tz)
775 goto end;
776
777 seq_puts(seq, "state: ");
778
Len Brown4be44fc2005-08-05 00:44:28 -0400779 if (!tz->state.critical && !tz->state.hot && !tz->state.passive
780 && !tz->state.active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 seq_puts(seq, "ok\n");
782 else {
783 if (tz->state.critical)
784 seq_puts(seq, "critical ");
785 if (tz->state.hot)
786 seq_puts(seq, "hot ");
787 if (tz->state.passive)
788 seq_puts(seq, "passive ");
789 if (tz->state.active)
790 seq_printf(seq, "active[%d]", tz->state.active_index);
791 seq_puts(seq, "\n");
792 }
793
Len Brown4be44fc2005-08-05 00:44:28 -0400794 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400795 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
798static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
799{
800 return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
801}
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
804{
Len Brown4be44fc2005-08-05 00:44:28 -0400805 int result = 0;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200806 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 if (!tz)
810 goto end;
811
812 result = acpi_thermal_get_temperature(tz);
813 if (result)
814 goto end;
815
Len Brown4be44fc2005-08-05 00:44:28 -0400816 seq_printf(seq, "temperature: %ld C\n",
817 KELVIN_TO_CELSIUS(tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Len Brown4be44fc2005-08-05 00:44:28 -0400819 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400820 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
823static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
824{
825 return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
826}
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
829{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200830 struct acpi_thermal *tz = seq->private;
Thomas Renninger68ccfaa2006-11-19 23:01:40 +0100831 struct acpi_device *device;
Thomas Renningere7c746e2007-06-18 00:40:51 -0400832 acpi_status status;
833
Len Brown4be44fc2005-08-05 00:44:28 -0400834 int i = 0;
835 int j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 if (!tz)
839 goto end;
840
841 if (tz->trips.critical.flags.valid)
842 seq_printf(seq, "critical (S5): %ld C\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400843 KELVIN_TO_CELSIUS(tz->trips.critical.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 if (tz->trips.hot.flags.valid)
846 seq_printf(seq, "hot (S4): %ld C\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400847 KELVIN_TO_CELSIUS(tz->trips.hot.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 if (tz->trips.passive.flags.valid) {
Len Brown4be44fc2005-08-05 00:44:28 -0400850 seq_printf(seq,
851 "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
852 KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
853 tz->trips.passive.tc1, tz->trips.passive.tc2,
854 tz->trips.passive.tsp);
855 for (j = 0; j < tz->trips.passive.devices.count; j++) {
Thomas Renningere7c746e2007-06-18 00:40:51 -0400856 status = acpi_bus_get_device(tz->trips.passive.devices.
857 handles[j], &device);
858 seq_printf(seq, "%4.4s ", status ? "" :
859 acpi_device_bid(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
861 seq_puts(seq, "\n");
862 }
863
864 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
865 if (!(tz->trips.active[i].flags.valid))
866 break;
867 seq_printf(seq, "active[%d]: %ld C: devices=",
Len Brown4be44fc2005-08-05 00:44:28 -0400868 i,
869 KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
Thomas Renninger68ccfaa2006-11-19 23:01:40 +0100870 for (j = 0; j < tz->trips.active[i].devices.count; j++){
Thomas Renningere7c746e2007-06-18 00:40:51 -0400871 status = acpi_bus_get_device(tz->trips.active[i].
872 devices.handles[j],
873 &device);
874 seq_printf(seq, "%4.4s ", status ? "" :
875 acpi_device_bid(device));
Thomas Renninger68ccfaa2006-11-19 23:01:40 +0100876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 seq_puts(seq, "\n");
878 }
879
Len Brown4be44fc2005-08-05 00:44:28 -0400880 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400881 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
883
884static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
885{
886 return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
887}
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
890{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200891 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 if (!tz)
895 goto end;
896
Len Browneaca2d32007-04-30 23:27:43 -0400897 if (!tz->flags.cooling_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 seq_puts(seq, "<setting not supported>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 else
Len Browneaca2d32007-04-30 23:27:43 -0400900 seq_puts(seq, "0 - Active; 1 - Passive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Len Brown4be44fc2005-08-05 00:44:28 -0400902 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400903 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
906static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
907{
908 return single_open(file, acpi_thermal_cooling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -0400909 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
912static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -0400913acpi_thermal_write_cooling_mode(struct file *file,
914 const char __user * buffer,
915 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200917 struct seq_file *m = file->private_data;
918 struct acpi_thermal *tz = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400919 int result = 0;
920 char mode_string[12] = { '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 if (!tz || (count > sizeof(mode_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400924 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 if (!tz->flags.cooling_mode)
Patrick Mocheld550d982006-06-27 00:41:40 -0400927 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 if (copy_from_user(mode_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -0400930 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -0400931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 mode_string[count] = '\0';
Len Brown4be44fc2005-08-05 00:44:28 -0400933
934 result = acpi_thermal_set_cooling_mode(tz,
935 simple_strtoul(mode_string, NULL,
936 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400938 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 acpi_thermal_check(tz);
941
Patrick Mocheld550d982006-06-27 00:41:40 -0400942 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
946{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200947 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 if (!tz)
951 goto end;
952
953 if (!tz->polling_frequency) {
954 seq_puts(seq, "<polling disabled>\n");
955 goto end;
956 }
957
958 seq_printf(seq, "polling frequency: %lu seconds\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400959 (tz->polling_frequency / 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Len Brown4be44fc2005-08-05 00:44:28 -0400961 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400962 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
965static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
966{
967 return single_open(file, acpi_thermal_polling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -0400968 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
970
971static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -0400972acpi_thermal_write_polling(struct file *file,
973 const char __user * buffer,
974 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200976 struct seq_file *m = file->private_data;
977 struct acpi_thermal *tz = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400978 int result = 0;
979 char polling_string[12] = { '\0' };
980 int seconds = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 if (!tz || (count > sizeof(polling_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400984 return -EINVAL;
Len Brown4be44fc2005-08-05 00:44:28 -0400985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (copy_from_user(polling_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -0400987 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -0400988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 polling_string[count] = '\0';
990
991 seconds = simple_strtoul(polling_string, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -0400992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 result = acpi_thermal_set_polling(tz, seconds);
994 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400995 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 acpi_thermal_check(tz);
998
Patrick Mocheld550d982006-06-27 00:41:40 -0400999 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
Len Brown4be44fc2005-08-05 00:44:28 -04001002static int acpi_thermal_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Len Brown4be44fc2005-08-05 00:44:28 -04001004 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 if (!acpi_device_dir(device)) {
1008 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -04001009 acpi_thermal_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001011 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 acpi_device_dir(device)->owner = THIS_MODULE;
1013 }
1014
1015 /* 'state' [R] */
1016 entry = create_proc_entry(ACPI_THERMAL_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -04001017 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001019 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 else {
1021 entry->proc_fops = &acpi_thermal_state_fops;
1022 entry->data = acpi_driver_data(device);
1023 entry->owner = THIS_MODULE;
1024 }
1025
1026 /* 'temperature' [R] */
1027 entry = create_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
Len Brown4be44fc2005-08-05 00:44:28 -04001028 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001030 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 else {
1032 entry->proc_fops = &acpi_thermal_temp_fops;
1033 entry->data = acpi_driver_data(device);
1034 entry->owner = THIS_MODULE;
1035 }
1036
1037 /* 'trip_points' [R/W] */
1038 entry = create_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
Len Brown4be44fc2005-08-05 00:44:28 -04001039 S_IFREG | S_IRUGO | S_IWUSR,
1040 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001042 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 else {
1044 entry->proc_fops = &acpi_thermal_trip_fops;
1045 entry->data = acpi_driver_data(device);
1046 entry->owner = THIS_MODULE;
1047 }
1048
1049 /* 'cooling_mode' [R/W] */
1050 entry = create_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
Len Brown4be44fc2005-08-05 00:44:28 -04001051 S_IFREG | S_IRUGO | S_IWUSR,
1052 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001054 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 else {
1056 entry->proc_fops = &acpi_thermal_cooling_fops;
1057 entry->data = acpi_driver_data(device);
1058 entry->owner = THIS_MODULE;
1059 }
1060
1061 /* 'polling_frequency' [R/W] */
1062 entry = create_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
Len Brown4be44fc2005-08-05 00:44:28 -04001063 S_IFREG | S_IRUGO | S_IWUSR,
1064 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001066 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 else {
1068 entry->proc_fops = &acpi_thermal_polling_fops;
1069 entry->data = acpi_driver_data(device);
1070 entry->owner = THIS_MODULE;
1071 }
1072
Patrick Mocheld550d982006-06-27 00:41:40 -04001073 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
Len Brown4be44fc2005-08-05 00:44:28 -04001076static int acpi_thermal_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 if (acpi_device_dir(device)) {
1080 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1081 acpi_device_dir(device));
1082 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1083 acpi_device_dir(device));
1084 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1085 acpi_device_dir(device));
1086 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1087 acpi_device_dir(device));
1088 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1089 acpi_device_dir(device));
1090 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1091 acpi_device_dir(device) = NULL;
1092 }
1093
Patrick Mocheld550d982006-06-27 00:41:40 -04001094 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095}
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097/* --------------------------------------------------------------------------
1098 Driver Interface
1099 -------------------------------------------------------------------------- */
1100
Len Brown4be44fc2005-08-05 00:44:28 -04001101static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001103 struct acpi_thermal *tz = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001104 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001108 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Patrick Mochel8348e1b2006-05-19 16:54:40 -04001110 device = tz->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 switch (event) {
1113 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1114 acpi_thermal_check(tz);
1115 break;
1116 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
1117 acpi_thermal_get_trip_points(tz);
1118 acpi_thermal_check(tz);
1119 acpi_bus_generate_event(device, event, 0);
1120 break;
1121 case ACPI_THERMAL_NOTIFY_DEVICES:
1122 if (tz->flags.devices)
1123 acpi_thermal_get_devices(tz);
1124 acpi_bus_generate_event(device, event, 0);
1125 break;
1126 default:
1127 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001128 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 break;
1130 }
1131
Patrick Mocheld550d982006-06-27 00:41:40 -04001132 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133}
1134
Len Brown4be44fc2005-08-05 00:44:28 -04001135static int acpi_thermal_get_info(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Len Brown4be44fc2005-08-05 00:44:28 -04001137 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001141 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 /* Get temperature [_TMP] (required) */
1144 result = acpi_thermal_get_temperature(tz);
1145 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001146 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 /* Get trip points [_CRT, _PSV, etc.] (required) */
1149 result = acpi_thermal_get_trip_points(tz);
1150 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001151 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153 /* Set the cooling mode [_SCP] to active cooling (default) */
1154 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
Len Brown4be44fc2005-08-05 00:44:28 -04001155 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 tz->flags.cooling_mode = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 /* Get default polling frequency [_TZP] (optional) */
1159 if (tzp)
1160 tz->polling_frequency = tzp;
1161 else
1162 acpi_thermal_get_polling_frequency(tz);
1163
1164 /* Get devices in this thermal zone [_TZD] (optional) */
1165 result = acpi_thermal_get_devices(tz);
1166 if (!result)
1167 tz->flags.devices = 1;
1168
Patrick Mocheld550d982006-06-27 00:41:40 -04001169 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171
Len Brown4be44fc2005-08-05 00:44:28 -04001172static int acpi_thermal_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Len Brown4be44fc2005-08-05 00:44:28 -04001174 int result = 0;
1175 acpi_status status = AE_OK;
1176 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001180 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Burman Yan36bcbec2006-12-19 12:56:11 -08001182 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001184 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Patrick Mochel8348e1b2006-05-19 16:54:40 -04001186 tz->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 strcpy(tz->name, device->pnp.bus_id);
1188 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1189 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1190 acpi_driver_data(device) = tz;
1191
1192 result = acpi_thermal_get_info(tz);
1193 if (result)
1194 goto end;
1195
1196 result = acpi_thermal_add_fs(device);
1197 if (result)
Vasily Averin09047e72006-04-27 05:25:00 -04001198 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 init_timer(&tz->timer);
1201
1202 acpi_thermal_check(tz);
1203
Patrick Mochel38ba7c92006-05-19 16:54:48 -04001204 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001205 ACPI_DEVICE_NOTIFY,
1206 acpi_thermal_notify, tz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 result = -ENODEV;
1209 goto end;
1210 }
1211
1212 printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001213 acpi_device_name(device), acpi_device_bid(device),
1214 KELVIN_TO_CELSIUS(tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Len Brown4be44fc2005-08-05 00:44:28 -04001216 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if (result) {
1218 acpi_thermal_remove_fs(device);
1219 kfree(tz);
1220 }
1221
Patrick Mocheld550d982006-06-27 00:41:40 -04001222 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
Len Brown4be44fc2005-08-05 00:44:28 -04001225static int acpi_thermal_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
Len Brown4be44fc2005-08-05 00:44:28 -04001227 acpi_status status = AE_OK;
1228 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001232 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001234 tz = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 /* avoid timer adding new defer task */
1237 tz->zombie = 1;
1238 /* wait for running timer (on other CPUs) finish */
1239 del_timer_sync(&(tz->timer));
1240 /* synchronize deferred task */
1241 acpi_os_wait_events_complete(NULL);
1242 /* deferred task may reinsert timer */
1243 del_timer_sync(&(tz->timer));
1244
Patrick Mochel38ba7c92006-05-19 16:54:48 -04001245 status = acpi_remove_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001246 ACPI_DEVICE_NOTIFY,
1247 acpi_thermal_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 /* Terminate policy */
Len Brown4be44fc2005-08-05 00:44:28 -04001250 if (tz->trips.passive.flags.valid && tz->trips.passive.flags.enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 tz->trips.passive.flags.enabled = 0;
1252 acpi_thermal_passive(tz);
1253 }
1254 if (tz->trips.active[0].flags.valid
Len Brown4be44fc2005-08-05 00:44:28 -04001255 && tz->trips.active[0].flags.enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 tz->trips.active[0].flags.enabled = 0;
1257 acpi_thermal_active(tz);
1258 }
1259
1260 acpi_thermal_remove_fs(device);
1261
1262 kfree(tz);
Patrick Mocheld550d982006-06-27 00:41:40 -04001263 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264}
1265
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001266static int acpi_thermal_resume(struct acpi_device *device)
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001267{
1268 struct acpi_thermal *tz = NULL;
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001269 int i, j, power_state, result;
1270
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001271
1272 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001273 return -EINVAL;
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001274
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001275 tz = acpi_driver_data(device);
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001276
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001277 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001278 if (!(&tz->trips.active[i]))
1279 break;
1280 if (!tz->trips.active[i].flags.valid)
1281 break;
1282 tz->trips.active[i].flags.enabled = 1;
1283 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1284 result = acpi_bus_get_power(tz->trips.active[i].devices.
1285 handles[j], &power_state);
1286 if (result || (power_state != ACPI_STATE_D0)) {
1287 tz->trips.active[i].flags.enabled = 0;
1288 break;
1289 }
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001290 }
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001291 tz->state.active |= tz->trips.active[i].flags.enabled;
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001292 }
1293
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001294 acpi_thermal_check(tz);
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001295
1296 return AE_OK;
1297}
1298
Len Brown4be44fc2005-08-05 00:44:28 -04001299static int __init acpi_thermal_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
Len Brown4be44fc2005-08-05 00:44:28 -04001301 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Len Brown72b33ef2007-08-12 00:12:17 -04001303 if (off) {
1304 printk(KERN_NOTICE "ACPI: thermal control disabled\n");
1305 return -ENODEV;
1306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1308 if (!acpi_thermal_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001309 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 acpi_thermal_dir->owner = THIS_MODULE;
1311
1312 result = acpi_bus_register_driver(&acpi_thermal_driver);
1313 if (result < 0) {
1314 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04001315 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
1317
Patrick Mocheld550d982006-06-27 00:41:40 -04001318 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319}
1320
Len Brown4be44fc2005-08-05 00:44:28 -04001321static void __exit acpi_thermal_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 acpi_bus_unregister_driver(&acpi_thermal_driver);
1325
1326 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1327
Patrick Mocheld550d982006-06-27 00:41:40 -04001328 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329}
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331module_init(acpi_thermal_init);
1332module_exit(acpi_thermal_exit);