blob: ff078d9b52f63ad787ed724346e3f26cc26fce5b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * acpi_thermal.c - ACPI Thermal Zone Driver ($Revision: 41 $)
3 *
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6 *
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 *
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 *
25 * This driver fully implements the ACPI thermal policy as described in the
26 * ACPI 2.0 Specification.
27 *
28 * TBD: 1. Implement passive cooling hysteresis.
29 * 2. Enhance passive cooling (CPU) states/limit interface to support
30 * concepts of 'multiple limiters', upper/lower limits, etc.
31 *
32 */
33
34#include <linux/kernel.h>
35#include <linux/module.h>
36#include <linux/init.h>
37#include <linux/types.h>
38#include <linux/proc_fs.h>
39#include <linux/sched.h>
40#include <linux/kmod.h>
41#include <linux/seq_file.h>
42#include <asm/uaccess.h>
43
44#include <acpi/acpi_bus.h>
45#include <acpi/acpi_drivers.h>
46
47#define ACPI_THERMAL_COMPONENT 0x04000000
48#define ACPI_THERMAL_CLASS "thermal_zone"
49#define ACPI_THERMAL_DRIVER_NAME "ACPI Thermal Zone Driver"
50#define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
51#define ACPI_THERMAL_FILE_STATE "state"
52#define ACPI_THERMAL_FILE_TEMPERATURE "temperature"
53#define ACPI_THERMAL_FILE_TRIP_POINTS "trip_points"
54#define ACPI_THERMAL_FILE_COOLING_MODE "cooling_mode"
55#define ACPI_THERMAL_FILE_POLLING_FREQ "polling_frequency"
56#define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
57#define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
58#define ACPI_THERMAL_NOTIFY_DEVICES 0x82
59#define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
60#define ACPI_THERMAL_NOTIFY_HOT 0xF1
61#define ACPI_THERMAL_MODE_ACTIVE 0x00
62#define ACPI_THERMAL_MODE_PASSIVE 0x01
63#define ACPI_THERMAL_MODE_CRITICAL 0xff
64#define ACPI_THERMAL_PATH_POWEROFF "/sbin/poweroff"
65
66#define ACPI_THERMAL_MAX_ACTIVE 10
67#define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
68
69#define KELVIN_TO_CELSIUS(t) (long)(((long)t-2732>=0) ? ((long)t-2732+5)/10 : ((long)t-2732-5)/10)
70#define CELSIUS_TO_KELVIN(t) ((t+273)*10)
71
72#define _COMPONENT ACPI_THERMAL_COMPONENT
Len Brown4be44fc2005-08-05 00:44:28 -040073ACPI_MODULE_NAME("acpi_thermal")
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Thomas Renninger1cbf4c52004-09-16 11:07:00 -040075MODULE_AUTHOR("Paul Diefenbaugh");
Linus Torvalds1da177e2005-04-16 15:20:36 -070076MODULE_DESCRIPTION(ACPI_THERMAL_DRIVER_NAME);
77MODULE_LICENSE("GPL");
78
79static int tzp;
80module_param(tzp, int, 0);
81MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.\n");
82
Len Brown4be44fc2005-08-05 00:44:28 -040083static int acpi_thermal_add(struct acpi_device *device);
84static int acpi_thermal_remove(struct acpi_device *device, int type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +080085static int acpi_thermal_resume(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
87static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
88static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -040089static ssize_t acpi_thermal_write_trip_points(struct file *,
90 const char __user *, size_t,
91 loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -040093static ssize_t acpi_thermal_write_cooling_mode(struct file *,
94 const char __user *, size_t,
95 loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
Len Brown4be44fc2005-08-05 00:44:28 -040097static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
98 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100static struct acpi_driver acpi_thermal_driver = {
Len Brown4be44fc2005-08-05 00:44:28 -0400101 .name = ACPI_THERMAL_DRIVER_NAME,
102 .class = ACPI_THERMAL_CLASS,
103 .ids = ACPI_THERMAL_HID,
104 .ops = {
105 .add = acpi_thermal_add,
106 .remove = acpi_thermal_remove,
Konstantin Karasyov74ce14682006-05-08 08:32:00 -0400107 .resume = acpi_thermal_resume,
Len Brown4be44fc2005-08-05 00:44:28 -0400108 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109};
110
111struct acpi_thermal_state {
Len Brown4be44fc2005-08-05 00:44:28 -0400112 u8 critical:1;
113 u8 hot:1;
114 u8 passive:1;
115 u8 active:1;
116 u8 reserved:4;
117 int active_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118};
119
120struct acpi_thermal_state_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400121 u8 valid:1;
122 u8 enabled:1;
123 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
126struct acpi_thermal_critical {
127 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400128 unsigned long temperature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129};
130
131struct acpi_thermal_hot {
132 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400133 unsigned long temperature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
135
136struct acpi_thermal_passive {
137 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400138 unsigned long temperature;
139 unsigned long tc1;
140 unsigned long tc2;
141 unsigned long tsp;
142 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143};
144
145struct acpi_thermal_active {
146 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400147 unsigned long temperature;
148 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149};
150
151struct acpi_thermal_trips {
152 struct acpi_thermal_critical critical;
Len Brown4be44fc2005-08-05 00:44:28 -0400153 struct acpi_thermal_hot hot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 struct acpi_thermal_passive passive;
155 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
156};
157
158struct acpi_thermal_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400159 u8 cooling_mode:1; /* _SCP */
160 u8 devices:1; /* _TZD */
161 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162};
163
164struct acpi_thermal {
Patrick Mochel8348e1b2006-05-19 16:54:40 -0400165 struct acpi_device * device;
Len Brown4be44fc2005-08-05 00:44:28 -0400166 acpi_bus_id name;
167 unsigned long temperature;
168 unsigned long last_temperature;
169 unsigned long polling_frequency;
170 u8 cooling_mode;
171 volatile u8 zombie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 struct acpi_thermal_flags flags;
173 struct acpi_thermal_state state;
174 struct acpi_thermal_trips trips;
Len Brown4be44fc2005-08-05 00:44:28 -0400175 struct acpi_handle_list devices;
176 struct timer_list timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177};
178
Arjan van de Vend7508032006-07-04 13:06:00 -0400179static const struct file_operations acpi_thermal_state_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400180 .open = acpi_thermal_state_open_fs,
181 .read = seq_read,
182 .llseek = seq_lseek,
183 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184};
185
Arjan van de Vend7508032006-07-04 13:06:00 -0400186static const struct file_operations acpi_thermal_temp_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400187 .open = acpi_thermal_temp_open_fs,
188 .read = seq_read,
189 .llseek = seq_lseek,
190 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191};
192
Arjan van de Vend7508032006-07-04 13:06:00 -0400193static const struct file_operations acpi_thermal_trip_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400194 .open = acpi_thermal_trip_open_fs,
195 .read = seq_read,
196 .write = acpi_thermal_write_trip_points,
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_cooling_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400202 .open = acpi_thermal_cooling_open_fs,
203 .read = seq_read,
204 .write = acpi_thermal_write_cooling_mode,
205 .llseek = seq_lseek,
206 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207};
208
Arjan van de Vend7508032006-07-04 13:06:00 -0400209static const struct file_operations acpi_thermal_polling_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400210 .open = acpi_thermal_polling_open_fs,
211 .read = seq_read,
212 .write = acpi_thermal_write_polling,
213 .llseek = seq_lseek,
214 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215};
216
217/* --------------------------------------------------------------------------
218 Thermal Zone Management
219 -------------------------------------------------------------------------- */
220
Len Brown4be44fc2005-08-05 00:44:28 -0400221static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Len Brown4be44fc2005-08-05 00:44:28 -0400223 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400227 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 tz->last_temperature = tz->temperature;
230
Len Brown4be44fc2005-08-05 00:44:28 -0400231 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400232 acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tz->temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400234 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Len Brown4be44fc2005-08-05 00:44:28 -0400236 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
237 tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Patrick Mocheld550d982006-06-27 00:41:40 -0400239 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Len Brown4be44fc2005-08-05 00:44:28 -0400242static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Len Brown4be44fc2005-08-05 00:44:28 -0400244 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400248 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Len Brown4be44fc2005-08-05 00:44:28 -0400250 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400251 acpi_evaluate_integer(tz->device->handle, "_TZP", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400252 &tz->polling_frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400254 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Len Brown4be44fc2005-08-05 00:44:28 -0400256 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
257 tz->polling_frequency));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Patrick Mocheld550d982006-06-27 00:41:40 -0400259 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
Len Brown4be44fc2005-08-05 00:44:28 -0400262static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400266 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 tz->polling_frequency = seconds * 10; /* Convert value to deci-seconds */
269
Len Brown4be44fc2005-08-05 00:44:28 -0400270 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
271 "Polling frequency set to %lu seconds\n",
Sanjoy Mahajan636cedf2007-02-16 01:24:43 -0500272 tz->polling_frequency/10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Patrick Mocheld550d982006-06-27 00:41:40 -0400274 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
Len Brown4be44fc2005-08-05 00:44:28 -0400277static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Len Brown4be44fc2005-08-05 00:44:28 -0400279 acpi_status status = AE_OK;
280 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
281 struct acpi_object_list arg_list = { 1, &arg0 };
282 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400286 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400288 status = acpi_get_handle(tz->device->handle, "_SCP", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (ACPI_FAILURE(status)) {
290 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400291 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293
294 arg0.integer.value = mode;
295
296 status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
297 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400298 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 tz->cooling_mode = mode;
301
Len Brown4be44fc2005-08-05 00:44:28 -0400302 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Cooling mode [%s]\n",
303 mode ? "passive" : "active"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Patrick Mocheld550d982006-06-27 00:41:40 -0400305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Len Brown4be44fc2005-08-05 00:44:28 -0400308static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Len Brown4be44fc2005-08-05 00:44:28 -0400310 acpi_status status = AE_OK;
311 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400315 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 /* Critical Shutdown (required) */
318
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400319 status = acpi_evaluate_integer(tz->device->handle, "_CRT", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400320 &tz->trips.critical.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (ACPI_FAILURE(status)) {
322 tz->trips.critical.flags.valid = 0;
Thomas Renningera6fc6722006-06-26 23:58:43 -0400323 ACPI_EXCEPTION((AE_INFO, status, "No critical threshold"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400324 return -ENODEV;
Len Brown4be44fc2005-08-05 00:44:28 -0400325 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 tz->trips.critical.flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400327 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
328 "Found critical threshold [%lu]\n",
329 tz->trips.critical.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331
332 /* Critical Sleep (optional) */
333
Len Brown4be44fc2005-08-05 00:44:28 -0400334 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400335 acpi_evaluate_integer(tz->device->handle, "_HOT", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400336 &tz->trips.hot.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (ACPI_FAILURE(status)) {
338 tz->trips.hot.flags.valid = 0;
339 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No hot threshold\n"));
Len Brown4be44fc2005-08-05 00:44:28 -0400340 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 tz->trips.hot.flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400342 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found hot threshold [%lu]\n",
343 tz->trips.hot.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345
346 /* Passive: Processors (optional) */
347
Len Brown4be44fc2005-08-05 00:44:28 -0400348 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400349 acpi_evaluate_integer(tz->device->handle, "_PSV", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400350 &tz->trips.passive.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (ACPI_FAILURE(status)) {
352 tz->trips.passive.flags.valid = 0;
353 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No passive threshold\n"));
Len Brown4be44fc2005-08-05 00:44:28 -0400354 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 tz->trips.passive.flags.valid = 1;
356
Len Brown4be44fc2005-08-05 00:44:28 -0400357 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400358 acpi_evaluate_integer(tz->device->handle, "_TC1", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400359 &tz->trips.passive.tc1);
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_integer(tz->device->handle, "_TC2", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400365 &tz->trips.passive.tc2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (ACPI_FAILURE(status))
367 tz->trips.passive.flags.valid = 0;
368
Len Brown4be44fc2005-08-05 00:44:28 -0400369 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400370 acpi_evaluate_integer(tz->device->handle, "_TSP", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400371 &tz->trips.passive.tsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (ACPI_FAILURE(status))
373 tz->trips.passive.flags.valid = 0;
374
Len Brown4be44fc2005-08-05 00:44:28 -0400375 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400376 acpi_evaluate_reference(tz->device->handle, "_PSL", NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400377 &tz->trips.passive.devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (ACPI_FAILURE(status))
379 tz->trips.passive.flags.valid = 0;
380
381 if (!tz->trips.passive.flags.valid)
Len Browncece9292006-06-26 23:04:31 -0400382 printk(KERN_WARNING PREFIX "Invalid passive threshold\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 else
Len Brown4be44fc2005-08-05 00:44:28 -0400384 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
385 "Found passive threshold [%lu]\n",
386 tz->trips.passive.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388
389 /* Active: Fans, etc. (optional) */
390
Len Brown4be44fc2005-08-05 00:44:28 -0400391 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Len Brown4be44fc2005-08-05 00:44:28 -0400393 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Len Brown4be44fc2005-08-05 00:44:28 -0400395 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400396 acpi_evaluate_integer(tz->device->handle, name, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400397 &tz->trips.active[i].temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (ACPI_FAILURE(status))
399 break;
400
401 name[2] = 'L';
Len Brown4be44fc2005-08-05 00:44:28 -0400402 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400403 acpi_evaluate_reference(tz->device->handle, name, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400404 &tz->trips.active[i].devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (ACPI_SUCCESS(status)) {
406 tz->trips.active[i].flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400407 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
408 "Found active threshold [%d]:[%lu]\n",
409 i, tz->trips.active[i].temperature));
410 } else
Thomas Renningera6fc6722006-06-26 23:58:43 -0400411 ACPI_EXCEPTION((AE_INFO, status,
412 "Invalid active threshold [%d]", i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414
Patrick Mocheld550d982006-06-27 00:41:40 -0400415 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
Len Brown4be44fc2005-08-05 00:44:28 -0400418static int acpi_thermal_get_devices(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Len Brown4be44fc2005-08-05 00:44:28 -0400420 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400424 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Len Brown4be44fc2005-08-05 00:44:28 -0400426 status =
Patrick Mochel38ba7c92006-05-19 16:54:48 -0400427 acpi_evaluate_reference(tz->device->handle, "_TZD", NULL, &tz->devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400429 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Patrick Mocheld550d982006-06-27 00:41:40 -0400431 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
Len Brown4be44fc2005-08-05 00:44:28 -0400434static int acpi_thermal_call_usermode(char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Len Brown4be44fc2005-08-05 00:44:28 -0400436 char *argv[2] = { NULL, NULL };
437 char *envp[3] = { NULL, NULL, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 if (!path)
Patrick Mocheld550d982006-06-27 00:41:40 -0400441 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 argv[0] = path;
444
445 /* minimal command environment */
446 envp[0] = "HOME=/";
447 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
Len Brown4be44fc2005-08-05 00:44:28 -0400448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 call_usermodehelper(argv[0], argv, envp, 0);
450
Patrick Mocheld550d982006-06-27 00:41:40 -0400451 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Len Brown4be44fc2005-08-05 00:44:28 -0400454static int acpi_thermal_critical(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (!tz || !tz->trips.critical.flags.valid)
Patrick Mocheld550d982006-06-27 00:41:40 -0400457 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 if (tz->temperature >= tz->trips.critical.temperature) {
Len Browncece9292006-06-26 23:04:31 -0400460 printk(KERN_WARNING PREFIX "Critical trip point\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 tz->trips.critical.flags.enabled = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400462 } else if (tz->trips.critical.flags.enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 tz->trips.critical.flags.enabled = 0;
464
Len Brown4be44fc2005-08-05 00:44:28 -0400465 printk(KERN_EMERG
466 "Critical temperature reached (%ld C), shutting down.\n",
467 KELVIN_TO_CELSIUS(tz->temperature));
Patrick Mochel8348e1b2006-05-19 16:54:40 -0400468 acpi_bus_generate_event(tz->device, ACPI_THERMAL_NOTIFY_CRITICAL,
Len Brown4be44fc2005-08-05 00:44:28 -0400469 tz->trips.critical.flags.enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 acpi_thermal_call_usermode(ACPI_THERMAL_PATH_POWEROFF);
472
Patrick Mocheld550d982006-06-27 00:41:40 -0400473 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
Len Brown4be44fc2005-08-05 00:44:28 -0400476static int acpi_thermal_hot(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (!tz || !tz->trips.hot.flags.valid)
Patrick Mocheld550d982006-06-27 00:41:40 -0400479 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 if (tz->temperature >= tz->trips.hot.temperature) {
Len Browncece9292006-06-26 23:04:31 -0400482 printk(KERN_WARNING PREFIX "Hot trip point\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 tz->trips.hot.flags.enabled = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400484 } else if (tz->trips.hot.flags.enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 tz->trips.hot.flags.enabled = 0;
486
Patrick Mochel8348e1b2006-05-19 16:54:40 -0400487 acpi_bus_generate_event(tz->device, ACPI_THERMAL_NOTIFY_HOT,
Len Brown4be44fc2005-08-05 00:44:28 -0400488 tz->trips.hot.flags.enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 /* TBD: Call user-mode "sleep(S4)" function */
491
Patrick Mocheld550d982006-06-27 00:41:40 -0400492 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400495static void acpi_thermal_passive(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400497 int result = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 struct acpi_thermal_passive *passive = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400499 int trend = 0;
500 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 if (!tz || !tz->trips.passive.flags.valid)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400504 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 passive = &(tz->trips.passive);
507
508 /*
509 * Above Trip?
510 * -----------
511 * Calculate the thermal trend (using the passive cooling equation)
512 * and modify the performance limit for all passive cooling devices
513 * accordingly. Note that we assume symmetry.
514 */
515 if (tz->temperature >= passive->temperature) {
Len Brown4be44fc2005-08-05 00:44:28 -0400516 trend =
517 (passive->tc1 * (tz->temperature - tz->last_temperature)) +
518 (passive->tc2 * (tz->temperature - passive->temperature));
519 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
520 "trend[%d]=(tc1[%lu]*(tmp[%lu]-last[%lu]))+(tc2[%lu]*(tmp[%lu]-psv[%lu]))\n",
521 trend, passive->tc1, tz->temperature,
522 tz->last_temperature, passive->tc2,
523 tz->temperature, passive->temperature));
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400524 passive->flags.enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 /* Heating up? */
526 if (trend > 0)
Len Brown4be44fc2005-08-05 00:44:28 -0400527 for (i = 0; i < passive->devices.count; i++)
528 acpi_processor_set_thermal_limit(passive->
529 devices.
530 handles[i],
531 ACPI_PROCESSOR_LIMIT_INCREMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 /* Cooling off? */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400533 else if (trend < 0) {
Len Brown4be44fc2005-08-05 00:44:28 -0400534 for (i = 0; i < passive->devices.count; i++)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400535 /*
536 * assume that we are on highest
537 * freq/lowest thrott and can leave
538 * passive mode, even in error case
539 */
540 if (!acpi_processor_set_thermal_limit
541 (passive->devices.handles[i],
542 ACPI_PROCESSOR_LIMIT_DECREMENT))
543 result = 0;
544 /*
545 * Leave cooling mode, even if the temp might
546 * higher than trip point This is because some
547 * machines might have long thermal polling
548 * frequencies (tsp) defined. We will fall back
549 * into passive mode in next cycle (probably quicker)
550 */
551 if (result) {
552 passive->flags.enabled = 0;
553 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
554 "Disabling passive cooling, still above threshold,"
555 " but we are cooling down\n"));
556 }
557 }
558 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
560
561 /*
562 * Below Trip?
563 * -----------
564 * Implement passive cooling hysteresis to slowly increase performance
565 * and avoid thrashing around the passive trip point. Note that we
566 * assume symmetry.
567 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400568 if (!passive->flags.enabled)
569 return;
570 for (i = 0; i < passive->devices.count; i++)
571 if (!acpi_processor_set_thermal_limit
572 (passive->devices.handles[i],
573 ACPI_PROCESSOR_LIMIT_DECREMENT))
574 result = 0;
575 if (result) {
576 passive->flags.enabled = 0;
577 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
578 "Disabling passive cooling (zone is cool)\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400582static void acpi_thermal_active(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Len Brown4be44fc2005-08-05 00:44:28 -0400584 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 struct acpi_thermal_active *active = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400586 int i = 0;
587 int j = 0;
588 unsigned long maxtemp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 if (!tz)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400592 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Len Brown4be44fc2005-08-05 00:44:28 -0400594 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 active = &(tz->trips.active[i]);
596 if (!active || !active->flags.valid)
597 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (tz->temperature >= active->temperature) {
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400599 /*
600 * Above Threshold?
601 * ----------------
602 * If not already enabled, turn ON all cooling devices
603 * associated with this active threshold.
604 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (active->temperature > maxtemp)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400606 tz->state.active_index = i;
607 maxtemp = active->temperature;
608 if (active->flags.enabled)
609 continue;
610 for (j = 0; j < active->devices.count; j++) {
611 result =
612 acpi_bus_set_power(active->devices.
613 handles[j],
614 ACPI_STATE_D0);
615 if (result) {
Len Browncece9292006-06-26 23:04:31 -0400616 printk(KERN_WARNING PREFIX
617 "Unable to turn cooling device [%p] 'on'\n",
Thomas Renningera6fc6722006-06-26 23:58:43 -0400618 active->devices.
Len Browncece9292006-06-26 23:04:31 -0400619 handles[j]);
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400620 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400622 active->flags.enabled = 1;
623 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
624 "Cooling device [%p] now 'on'\n",
625 active->devices.handles[j]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400627 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400629 if (!active->flags.enabled)
630 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /*
632 * Below Threshold?
633 * ----------------
634 * Turn OFF all cooling devices associated with this
635 * threshold.
636 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400637 for (j = 0; j < active->devices.count; j++) {
638 result = acpi_bus_set_power(active->devices.handles[j],
639 ACPI_STATE_D3);
640 if (result) {
Len Browncece9292006-06-26 23:04:31 -0400641 printk(KERN_WARNING PREFIX
642 "Unable to turn cooling device [%p] 'off'\n",
643 active->devices.handles[j]);
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400644 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400646 active->flags.enabled = 0;
647 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
648 "Cooling device [%p] now 'off'\n",
649 active->devices.handles[j]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
Len Brown4be44fc2005-08-05 00:44:28 -0400654static void acpi_thermal_check(void *context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Len Brown4be44fc2005-08-05 00:44:28 -0400656static void acpi_thermal_run(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
658 struct acpi_thermal *tz = (struct acpi_thermal *)data;
659 if (!tz->zombie)
Alexey Starikovskiyb8d35192006-05-05 03:23:00 -0400660 acpi_os_execute(OSL_GPE_HANDLER, acpi_thermal_check, (void *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
Len Brown4be44fc2005-08-05 00:44:28 -0400663static void acpi_thermal_check(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Len Brown4be44fc2005-08-05 00:44:28 -0400665 int result = 0;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200666 struct acpi_thermal *tz = data;
Len Brown4be44fc2005-08-05 00:44:28 -0400667 unsigned long sleep_time = 0;
668 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 struct acpi_thermal_state state;
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 if (!tz) {
Len Brown64684632006-06-26 23:41:38 -0400673 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400674 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676
677 state = tz->state;
678
679 result = acpi_thermal_get_temperature(tz);
680 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400681 return;
Len Brown4be44fc2005-08-05 00:44:28 -0400682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 memset(&tz->state, 0, sizeof(tz->state));
Len Brown4be44fc2005-08-05 00:44:28 -0400684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 /*
686 * Check Trip Points
687 * -----------------
688 * Compare the current temperature to the trip point values to see
689 * if we've entered one of the thermal policy states. Note that
690 * this function determines when a state is entered, but the
691 * individual policy decides when it is exited (e.g. hysteresis).
692 */
693 if (tz->trips.critical.flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400694 state.critical |=
695 (tz->temperature >= tz->trips.critical.temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (tz->trips.hot.flags.valid)
697 state.hot |= (tz->temperature >= tz->trips.hot.temperature);
698 if (tz->trips.passive.flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400699 state.passive |=
700 (tz->temperature >= tz->trips.passive.temperature);
701 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (tz->trips.active[i].flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400703 state.active |=
704 (tz->temperature >=
705 tz->trips.active[i].temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 /*
708 * Invoke Policy
709 * -------------
710 * Separated from the above check to allow individual policy to
711 * determine when to exit a given state.
712 */
713 if (state.critical)
714 acpi_thermal_critical(tz);
715 if (state.hot)
716 acpi_thermal_hot(tz);
717 if (state.passive)
718 acpi_thermal_passive(tz);
719 if (state.active)
720 acpi_thermal_active(tz);
721
722 /*
723 * Calculate State
724 * ---------------
725 * Again, separated from the above two to allow independent policy
726 * decisions.
727 */
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400728 tz->state.critical = tz->trips.critical.flags.enabled;
729 tz->state.hot = tz->trips.hot.flags.enabled;
730 tz->state.passive = tz->trips.passive.flags.enabled;
731 tz->state.active = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400732 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
Thomas Renninger1cbf4c52004-09-16 11:07:00 -0400733 tz->state.active |= tz->trips.active[i].flags.enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 /*
736 * Calculate Sleep Time
737 * --------------------
738 * If we're in the passive state, use _TSP's value. Otherwise
739 * use the default polling frequency (e.g. _TZP). If no polling
740 * frequency is specified then we'll wait forever (at least until
741 * a thermal event occurs). Note that _TSP and _TZD values are
742 * given in 1/10th seconds (we must covert to milliseconds).
743 */
744 if (tz->state.passive)
745 sleep_time = tz->trips.passive.tsp * 100;
746 else if (tz->polling_frequency > 0)
747 sleep_time = tz->polling_frequency * 100;
748
Len Brown4be44fc2005-08-05 00:44:28 -0400749 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: temperature[%lu] sleep[%lu]\n",
750 tz->name, tz->temperature, sleep_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 /*
753 * Schedule Next Poll
754 * ------------------
755 */
756 if (!sleep_time) {
757 if (timer_pending(&(tz->timer)))
758 del_timer(&(tz->timer));
Len Brown4be44fc2005-08-05 00:44:28 -0400759 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (timer_pending(&(tz->timer)))
761 mod_timer(&(tz->timer), (HZ * sleep_time) / 1000);
762 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400763 tz->timer.data = (unsigned long)tz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 tz->timer.function = acpi_thermal_run;
765 tz->timer.expires = jiffies + (HZ * sleep_time) / 1000;
766 add_timer(&(tz->timer));
767 }
768 }
769
Patrick Mocheld550d982006-06-27 00:41:40 -0400770 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773/* --------------------------------------------------------------------------
774 FS Interface (/proc)
775 -------------------------------------------------------------------------- */
776
Len Brown4be44fc2005-08-05 00:44:28 -0400777static struct proc_dir_entry *acpi_thermal_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
780{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200781 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 if (!tz)
785 goto end;
786
787 seq_puts(seq, "state: ");
788
Len Brown4be44fc2005-08-05 00:44:28 -0400789 if (!tz->state.critical && !tz->state.hot && !tz->state.passive
790 && !tz->state.active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 seq_puts(seq, "ok\n");
792 else {
793 if (tz->state.critical)
794 seq_puts(seq, "critical ");
795 if (tz->state.hot)
796 seq_puts(seq, "hot ");
797 if (tz->state.passive)
798 seq_puts(seq, "passive ");
799 if (tz->state.active)
800 seq_printf(seq, "active[%d]", tz->state.active_index);
801 seq_puts(seq, "\n");
802 }
803
Len Brown4be44fc2005-08-05 00:44:28 -0400804 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400805 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
808static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
809{
810 return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
811}
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
814{
Len Brown4be44fc2005-08-05 00:44:28 -0400815 int result = 0;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200816 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 if (!tz)
820 goto end;
821
822 result = acpi_thermal_get_temperature(tz);
823 if (result)
824 goto end;
825
Len Brown4be44fc2005-08-05 00:44:28 -0400826 seq_printf(seq, "temperature: %ld C\n",
827 KELVIN_TO_CELSIUS(tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Len Brown4be44fc2005-08-05 00:44:28 -0400829 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400830 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
833static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
834{
835 return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
836}
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
839{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200840 struct acpi_thermal *tz = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400841 int i = 0;
842 int j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 if (!tz)
846 goto end;
847
848 if (tz->trips.critical.flags.valid)
849 seq_printf(seq, "critical (S5): %ld C\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400850 KELVIN_TO_CELSIUS(tz->trips.critical.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 if (tz->trips.hot.flags.valid)
853 seq_printf(seq, "hot (S4): %ld C\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400854 KELVIN_TO_CELSIUS(tz->trips.hot.temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 if (tz->trips.passive.flags.valid) {
Len Brown4be44fc2005-08-05 00:44:28 -0400857 seq_printf(seq,
858 "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
859 KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
860 tz->trips.passive.tc1, tz->trips.passive.tc2,
861 tz->trips.passive.tsp);
862 for (j = 0; j < tz->trips.passive.devices.count; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Len Brown4be44fc2005-08-05 00:44:28 -0400864 seq_printf(seq, "0x%p ",
865 tz->trips.passive.devices.handles[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867 seq_puts(seq, "\n");
868 }
869
870 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
871 if (!(tz->trips.active[i].flags.valid))
872 break;
873 seq_printf(seq, "active[%d]: %ld C: devices=",
Len Brown4be44fc2005-08-05 00:44:28 -0400874 i,
875 KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
876 for (j = 0; j < tz->trips.active[i].devices.count; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 seq_printf(seq, "0x%p ",
Len Brown4be44fc2005-08-05 00:44:28 -0400878 tz->trips.active[i].devices.handles[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 seq_puts(seq, "\n");
880 }
881
Len Brown4be44fc2005-08-05 00:44:28 -0400882 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400883 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
886static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
887{
888 return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
889}
890
891static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -0400892acpi_thermal_write_trip_points(struct file *file,
893 const char __user * buffer,
894 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200896 struct seq_file *m = file->private_data;
897 struct acpi_thermal *tz = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Len Brown4be44fc2005-08-05 00:44:28 -0400899 char *limit_string;
900 int num, critical, hot, passive;
901 int *active;
902 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Burman Yan36bcbec2006-12-19 12:56:11 -0800905 limit_string = kzalloc(ACPI_THERMAL_MAX_LIMIT_STR_LEN, GFP_KERNEL);
Len Brown4be44fc2005-08-05 00:44:28 -0400906 if (!limit_string)
Patrick Mocheld550d982006-06-27 00:41:40 -0400907 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Len Brown4be44fc2005-08-05 00:44:28 -0400909 active = kmalloc(ACPI_THERMAL_MAX_ACTIVE * sizeof(int), GFP_KERNEL);
Dave Jonesfdc136c2006-03-08 22:12:00 -0500910 if (!active) {
911 kfree(limit_string);
Patrick Mocheld550d982006-06-27 00:41:40 -0400912 return -ENOMEM;
Dave Jonesfdc136c2006-03-08 22:12:00 -0500913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 if (!tz || (count > ACPI_THERMAL_MAX_LIMIT_STR_LEN - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 count = -EINVAL;
917 goto end;
918 }
Len Brown4be44fc2005-08-05 00:44:28 -0400919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (copy_from_user(limit_string, buffer, count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 count = -EFAULT;
922 goto end;
923 }
Len Brown4be44fc2005-08-05 00:44:28 -0400924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 limit_string[count] = '\0';
926
927 num = sscanf(limit_string, "%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d",
Len Brown4be44fc2005-08-05 00:44:28 -0400928 &critical, &hot, &passive,
929 &active[0], &active[1], &active[2], &active[3], &active[4],
930 &active[5], &active[6], &active[7], &active[8],
931 &active[9]);
932 if (!(num >= 5 && num < (ACPI_THERMAL_MAX_ACTIVE + 3))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 count = -EINVAL;
934 goto end;
935 }
936
937 tz->trips.critical.temperature = CELSIUS_TO_KELVIN(critical);
938 tz->trips.hot.temperature = CELSIUS_TO_KELVIN(hot);
939 tz->trips.passive.temperature = CELSIUS_TO_KELVIN(passive);
940 for (i = 0; i < num - 3; i++) {
941 if (!(tz->trips.active[i].flags.valid))
942 break;
943 tz->trips.active[i].temperature = CELSIUS_TO_KELVIN(active[i]);
944 }
Len Brown4be44fc2005-08-05 00:44:28 -0400945
946 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 kfree(active);
948 kfree(limit_string);
Patrick Mocheld550d982006-06-27 00:41:40 -0400949 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950}
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
953{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200954 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 if (!tz)
958 goto end;
959
960 if (!tz->flags.cooling_mode) {
961 seq_puts(seq, "<setting not supported>\n");
962 }
963
Len Brown4be44fc2005-08-05 00:44:28 -0400964 if (tz->cooling_mode == ACPI_THERMAL_MODE_CRITICAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 seq_printf(seq, "cooling mode: critical\n");
966 else
967 seq_printf(seq, "cooling mode: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400968 tz->cooling_mode ? "passive" : "active");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Len Brown4be44fc2005-08-05 00:44:28 -0400970 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400971 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
973
974static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
975{
976 return single_open(file, acpi_thermal_cooling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -0400977 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
980static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -0400981acpi_thermal_write_cooling_mode(struct file *file,
982 const char __user * buffer,
983 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200985 struct seq_file *m = file->private_data;
986 struct acpi_thermal *tz = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400987 int result = 0;
988 char mode_string[12] = { '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 if (!tz || (count > sizeof(mode_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400992 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 if (!tz->flags.cooling_mode)
Patrick Mocheld550d982006-06-27 00:41:40 -0400995 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 if (copy_from_user(mode_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -0400998 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -0400999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 mode_string[count] = '\0';
Len Brown4be44fc2005-08-05 00:44:28 -04001001
1002 result = acpi_thermal_set_cooling_mode(tz,
1003 simple_strtoul(mode_string, NULL,
1004 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001006 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 acpi_thermal_check(tz);
1009
Patrick Mocheld550d982006-06-27 00:41:40 -04001010 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
1014{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001015 struct acpi_thermal *tz = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 if (!tz)
1019 goto end;
1020
1021 if (!tz->polling_frequency) {
1022 seq_puts(seq, "<polling disabled>\n");
1023 goto end;
1024 }
1025
1026 seq_printf(seq, "polling frequency: %lu seconds\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001027 (tz->polling_frequency / 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Len Brown4be44fc2005-08-05 00:44:28 -04001029 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001030 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032
1033static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
1034{
1035 return single_open(file, acpi_thermal_polling_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001036 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
1039static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001040acpi_thermal_write_polling(struct file *file,
1041 const char __user * buffer,
1042 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001044 struct seq_file *m = file->private_data;
1045 struct acpi_thermal *tz = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001046 int result = 0;
1047 char polling_string[12] = { '\0' };
1048 int seconds = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 if (!tz || (count > sizeof(polling_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -04001052 return -EINVAL;
Len Brown4be44fc2005-08-05 00:44:28 -04001053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (copy_from_user(polling_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001055 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -04001056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 polling_string[count] = '\0';
1058
1059 seconds = simple_strtoul(polling_string, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 result = acpi_thermal_set_polling(tz, seconds);
1062 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001063 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 acpi_thermal_check(tz);
1066
Patrick Mocheld550d982006-06-27 00:41:40 -04001067 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
1069
Len Brown4be44fc2005-08-05 00:44:28 -04001070static int acpi_thermal_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Len Brown4be44fc2005-08-05 00:44:28 -04001072 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 if (!acpi_device_dir(device)) {
1076 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -04001077 acpi_thermal_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001079 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 acpi_device_dir(device)->owner = THIS_MODULE;
1081 }
1082
1083 /* 'state' [R] */
1084 entry = create_proc_entry(ACPI_THERMAL_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -04001085 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001087 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 else {
1089 entry->proc_fops = &acpi_thermal_state_fops;
1090 entry->data = acpi_driver_data(device);
1091 entry->owner = THIS_MODULE;
1092 }
1093
1094 /* 'temperature' [R] */
1095 entry = create_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
Len Brown4be44fc2005-08-05 00:44:28 -04001096 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001098 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 else {
1100 entry->proc_fops = &acpi_thermal_temp_fops;
1101 entry->data = acpi_driver_data(device);
1102 entry->owner = THIS_MODULE;
1103 }
1104
1105 /* 'trip_points' [R/W] */
1106 entry = create_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
Len Brown4be44fc2005-08-05 00:44:28 -04001107 S_IFREG | S_IRUGO | S_IWUSR,
1108 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001110 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 else {
1112 entry->proc_fops = &acpi_thermal_trip_fops;
1113 entry->data = acpi_driver_data(device);
1114 entry->owner = THIS_MODULE;
1115 }
1116
1117 /* 'cooling_mode' [R/W] */
1118 entry = create_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
Len Brown4be44fc2005-08-05 00:44:28 -04001119 S_IFREG | S_IRUGO | S_IWUSR,
1120 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001122 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 else {
1124 entry->proc_fops = &acpi_thermal_cooling_fops;
1125 entry->data = acpi_driver_data(device);
1126 entry->owner = THIS_MODULE;
1127 }
1128
1129 /* 'polling_frequency' [R/W] */
1130 entry = create_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
Len Brown4be44fc2005-08-05 00:44:28 -04001131 S_IFREG | S_IRUGO | S_IWUSR,
1132 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -04001134 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 else {
1136 entry->proc_fops = &acpi_thermal_polling_fops;
1137 entry->data = acpi_driver_data(device);
1138 entry->owner = THIS_MODULE;
1139 }
1140
Patrick Mocheld550d982006-06-27 00:41:40 -04001141 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143
Len Brown4be44fc2005-08-05 00:44:28 -04001144static int acpi_thermal_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 if (acpi_device_dir(device)) {
1148 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1149 acpi_device_dir(device));
1150 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1151 acpi_device_dir(device));
1152 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1153 acpi_device_dir(device));
1154 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1155 acpi_device_dir(device));
1156 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1157 acpi_device_dir(device));
1158 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1159 acpi_device_dir(device) = NULL;
1160 }
1161
Patrick Mocheld550d982006-06-27 00:41:40 -04001162 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165/* --------------------------------------------------------------------------
1166 Driver Interface
1167 -------------------------------------------------------------------------- */
1168
Len Brown4be44fc2005-08-05 00:44:28 -04001169static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001171 struct acpi_thermal *tz = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001172 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001176 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Patrick Mochel8348e1b2006-05-19 16:54:40 -04001178 device = tz->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180 switch (event) {
1181 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1182 acpi_thermal_check(tz);
1183 break;
1184 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
1185 acpi_thermal_get_trip_points(tz);
1186 acpi_thermal_check(tz);
1187 acpi_bus_generate_event(device, event, 0);
1188 break;
1189 case ACPI_THERMAL_NOTIFY_DEVICES:
1190 if (tz->flags.devices)
1191 acpi_thermal_get_devices(tz);
1192 acpi_bus_generate_event(device, event, 0);
1193 break;
1194 default:
1195 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001196 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 break;
1198 }
1199
Patrick Mocheld550d982006-06-27 00:41:40 -04001200 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
Len Brown4be44fc2005-08-05 00:44:28 -04001203static int acpi_thermal_get_info(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
Len Brown4be44fc2005-08-05 00:44:28 -04001205 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001209 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 /* Get temperature [_TMP] (required) */
1212 result = acpi_thermal_get_temperature(tz);
1213 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001214 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 /* Get trip points [_CRT, _PSV, etc.] (required) */
1217 result = acpi_thermal_get_trip_points(tz);
1218 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001219 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 /* Set the cooling mode [_SCP] to active cooling (default) */
1222 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
Len Brown4be44fc2005-08-05 00:44:28 -04001223 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 tz->flags.cooling_mode = 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001225 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 /* Oh,we have not _SCP method.
Len Brown4be44fc2005-08-05 00:44:28 -04001227 Generally show cooling_mode by _ACx, _PSV,spec 12.2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 tz->flags.cooling_mode = 0;
Len Brown4be44fc2005-08-05 00:44:28 -04001229 if (tz->trips.active[0].flags.valid
1230 && tz->trips.passive.flags.valid) {
1231 if (tz->trips.passive.temperature >
1232 tz->trips.active[0].temperature)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 tz->cooling_mode = ACPI_THERMAL_MODE_ACTIVE;
Len Brown4be44fc2005-08-05 00:44:28 -04001234 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 tz->cooling_mode = ACPI_THERMAL_MODE_PASSIVE;
Len Brown4be44fc2005-08-05 00:44:28 -04001236 } else if (!tz->trips.active[0].flags.valid
1237 && tz->trips.passive.flags.valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 tz->cooling_mode = ACPI_THERMAL_MODE_PASSIVE;
Len Brown4be44fc2005-08-05 00:44:28 -04001239 } else if (tz->trips.active[0].flags.valid
1240 && !tz->trips.passive.flags.valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 tz->cooling_mode = ACPI_THERMAL_MODE_ACTIVE;
1242 } else {
1243 /* _ACx and _PSV are optional, but _CRT is required */
1244 tz->cooling_mode = ACPI_THERMAL_MODE_CRITICAL;
1245 }
1246 }
1247
1248 /* Get default polling frequency [_TZP] (optional) */
1249 if (tzp)
1250 tz->polling_frequency = tzp;
1251 else
1252 acpi_thermal_get_polling_frequency(tz);
1253
1254 /* Get devices in this thermal zone [_TZD] (optional) */
1255 result = acpi_thermal_get_devices(tz);
1256 if (!result)
1257 tz->flags.devices = 1;
1258
Patrick Mocheld550d982006-06-27 00:41:40 -04001259 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260}
1261
Len Brown4be44fc2005-08-05 00:44:28 -04001262static int acpi_thermal_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
Len Brown4be44fc2005-08-05 00:44:28 -04001264 int result = 0;
1265 acpi_status status = AE_OK;
1266 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001270 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Burman Yan36bcbec2006-12-19 12:56:11 -08001272 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001274 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Patrick Mochel8348e1b2006-05-19 16:54:40 -04001276 tz->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 strcpy(tz->name, device->pnp.bus_id);
1278 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1279 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1280 acpi_driver_data(device) = tz;
1281
1282 result = acpi_thermal_get_info(tz);
1283 if (result)
1284 goto end;
1285
1286 result = acpi_thermal_add_fs(device);
1287 if (result)
Vasily Averin09047e72006-04-27 05:25:00 -04001288 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 init_timer(&tz->timer);
1291
1292 acpi_thermal_check(tz);
1293
Patrick Mochel38ba7c92006-05-19 16:54:48 -04001294 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001295 ACPI_DEVICE_NOTIFY,
1296 acpi_thermal_notify, tz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 result = -ENODEV;
1299 goto end;
1300 }
1301
1302 printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001303 acpi_device_name(device), acpi_device_bid(device),
1304 KELVIN_TO_CELSIUS(tz->temperature));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Len Brown4be44fc2005-08-05 00:44:28 -04001306 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 if (result) {
1308 acpi_thermal_remove_fs(device);
1309 kfree(tz);
1310 }
1311
Patrick Mocheld550d982006-06-27 00:41:40 -04001312 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313}
1314
Len Brown4be44fc2005-08-05 00:44:28 -04001315static int acpi_thermal_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
Len Brown4be44fc2005-08-05 00:44:28 -04001317 acpi_status status = AE_OK;
1318 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001322 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001324 tz = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 /* avoid timer adding new defer task */
1327 tz->zombie = 1;
1328 /* wait for running timer (on other CPUs) finish */
1329 del_timer_sync(&(tz->timer));
1330 /* synchronize deferred task */
1331 acpi_os_wait_events_complete(NULL);
1332 /* deferred task may reinsert timer */
1333 del_timer_sync(&(tz->timer));
1334
Patrick Mochel38ba7c92006-05-19 16:54:48 -04001335 status = acpi_remove_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001336 ACPI_DEVICE_NOTIFY,
1337 acpi_thermal_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 /* Terminate policy */
Len Brown4be44fc2005-08-05 00:44:28 -04001340 if (tz->trips.passive.flags.valid && tz->trips.passive.flags.enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 tz->trips.passive.flags.enabled = 0;
1342 acpi_thermal_passive(tz);
1343 }
1344 if (tz->trips.active[0].flags.valid
Len Brown4be44fc2005-08-05 00:44:28 -04001345 && tz->trips.active[0].flags.enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 tz->trips.active[0].flags.enabled = 0;
1347 acpi_thermal_active(tz);
1348 }
1349
1350 acpi_thermal_remove_fs(device);
1351
1352 kfree(tz);
Patrick Mocheld550d982006-06-27 00:41:40 -04001353 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354}
1355
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001356static int acpi_thermal_resume(struct acpi_device *device)
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001357{
1358 struct acpi_thermal *tz = NULL;
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001359 int i;
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001360
1361 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001362 return -EINVAL;
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001363
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001364 tz = acpi_driver_data(device);
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001365
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001366 acpi_thermal_get_temperature(tz);
1367
1368 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1369 if (tz->trips.active[i].flags.valid) {
1370 tz->temperature = tz->trips.active[i].temperature;
1371 tz->trips.active[i].flags.enabled = 0;
1372
1373 acpi_thermal_active(tz);
1374
1375 tz->state.active |= tz->trips.active[i].flags.enabled;
1376 tz->state.active_index = i;
1377 }
1378 }
1379
1380 acpi_thermal_check(tz);
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001381
1382 return AE_OK;
1383}
1384
Len Brown4be44fc2005-08-05 00:44:28 -04001385static int __init acpi_thermal_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
Len Brown4be44fc2005-08-05 00:44:28 -04001387 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1391 if (!acpi_thermal_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001392 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 acpi_thermal_dir->owner = THIS_MODULE;
1394
1395 result = acpi_bus_register_driver(&acpi_thermal_driver);
1396 if (result < 0) {
1397 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04001398 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
1400
Patrick Mocheld550d982006-06-27 00:41:40 -04001401 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402}
1403
Len Brown4be44fc2005-08-05 00:44:28 -04001404static void __exit acpi_thermal_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 acpi_bus_unregister_driver(&acpi_thermal_driver);
1408
1409 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1410
Patrick Mocheld550d982006-06-27 00:41:40 -04001411 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412}
1413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414module_init(acpi_thermal_init);
1415module_exit(acpi_thermal_exit);