Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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> | |
cd354f1a TS |
39 | #include <linux/timer.h> |
40 | #include <linux/jiffies.h> | |
1da177e4 LT |
41 | #include <linux/kmod.h> |
42 | #include <linux/seq_file.h> | |
10a0a8d4 | 43 | #include <linux/reboot.h> |
1da177e4 LT |
44 | #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" | |
1da177e4 LT |
51 | #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 | |
1da177e4 LT |
63 | |
64 | #define ACPI_THERMAL_MAX_ACTIVE 10 | |
65 | #define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65 | |
66 | ||
67 | #define KELVIN_TO_CELSIUS(t) (long)(((long)t-2732>=0) ? ((long)t-2732+5)/10 : ((long)t-2732-5)/10) | |
68 | #define CELSIUS_TO_KELVIN(t) ((t+273)*10) | |
69 | ||
70 | #define _COMPONENT ACPI_THERMAL_COMPONENT | |
f52fd66d | 71 | ACPI_MODULE_NAME("thermal"); |
1da177e4 | 72 | |
1cbf4c56 | 73 | MODULE_AUTHOR("Paul Diefenbaugh"); |
7cda93e0 | 74 | MODULE_DESCRIPTION("ACPI Thermal Zone Driver"); |
1da177e4 LT |
75 | MODULE_LICENSE("GPL"); |
76 | ||
77 | static int tzp; | |
78 | module_param(tzp, int, 0); | |
79 | MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.\n"); | |
80 | ||
4be44fcd LB |
81 | static int acpi_thermal_add(struct acpi_device *device); |
82 | static int acpi_thermal_remove(struct acpi_device *device, int type); | |
5d9464a4 | 83 | static int acpi_thermal_resume(struct acpi_device *device); |
1da177e4 LT |
84 | static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file); |
85 | static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file); | |
86 | static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file); | |
1da177e4 | 87 | static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file); |
4be44fcd LB |
88 | static ssize_t acpi_thermal_write_cooling_mode(struct file *, |
89 | const char __user *, size_t, | |
90 | loff_t *); | |
1da177e4 | 91 | static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file); |
4be44fcd LB |
92 | static ssize_t acpi_thermal_write_polling(struct file *, const char __user *, |
93 | size_t, loff_t *); | |
1da177e4 | 94 | |
1ba90e3a TR |
95 | static const struct acpi_device_id thermal_device_ids[] = { |
96 | {ACPI_THERMAL_HID, 0}, | |
97 | {"", 0}, | |
98 | }; | |
99 | MODULE_DEVICE_TABLE(acpi, thermal_device_ids); | |
100 | ||
1da177e4 | 101 | static struct acpi_driver acpi_thermal_driver = { |
c2b6705b | 102 | .name = "thermal", |
4be44fcd | 103 | .class = ACPI_THERMAL_CLASS, |
1ba90e3a | 104 | .ids = thermal_device_ids, |
4be44fcd LB |
105 | .ops = { |
106 | .add = acpi_thermal_add, | |
107 | .remove = acpi_thermal_remove, | |
74ce1468 | 108 | .resume = acpi_thermal_resume, |
4be44fcd | 109 | }, |
1da177e4 LT |
110 | }; |
111 | ||
112 | struct acpi_thermal_state { | |
4be44fcd LB |
113 | u8 critical:1; |
114 | u8 hot:1; | |
115 | u8 passive:1; | |
116 | u8 active:1; | |
117 | u8 reserved:4; | |
118 | int active_index; | |
1da177e4 LT |
119 | }; |
120 | ||
121 | struct acpi_thermal_state_flags { | |
4be44fcd LB |
122 | u8 valid:1; |
123 | u8 enabled:1; | |
124 | u8 reserved:6; | |
1da177e4 LT |
125 | }; |
126 | ||
127 | struct acpi_thermal_critical { | |
128 | struct acpi_thermal_state_flags flags; | |
4be44fcd | 129 | unsigned long temperature; |
1da177e4 LT |
130 | }; |
131 | ||
132 | struct acpi_thermal_hot { | |
133 | struct acpi_thermal_state_flags flags; | |
4be44fcd | 134 | unsigned long temperature; |
1da177e4 LT |
135 | }; |
136 | ||
137 | struct acpi_thermal_passive { | |
138 | struct acpi_thermal_state_flags flags; | |
4be44fcd LB |
139 | unsigned long temperature; |
140 | unsigned long tc1; | |
141 | unsigned long tc2; | |
142 | unsigned long tsp; | |
143 | struct acpi_handle_list devices; | |
1da177e4 LT |
144 | }; |
145 | ||
146 | struct acpi_thermal_active { | |
147 | struct acpi_thermal_state_flags flags; | |
4be44fcd LB |
148 | unsigned long temperature; |
149 | struct acpi_handle_list devices; | |
1da177e4 LT |
150 | }; |
151 | ||
152 | struct acpi_thermal_trips { | |
153 | struct acpi_thermal_critical critical; | |
4be44fcd | 154 | struct acpi_thermal_hot hot; |
1da177e4 LT |
155 | struct acpi_thermal_passive passive; |
156 | struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE]; | |
157 | }; | |
158 | ||
159 | struct acpi_thermal_flags { | |
4be44fcd LB |
160 | u8 cooling_mode:1; /* _SCP */ |
161 | u8 devices:1; /* _TZD */ | |
162 | u8 reserved:6; | |
1da177e4 LT |
163 | }; |
164 | ||
165 | struct acpi_thermal { | |
8348e1b1 | 166 | struct acpi_device * device; |
4be44fcd LB |
167 | acpi_bus_id name; |
168 | unsigned long temperature; | |
169 | unsigned long last_temperature; | |
170 | unsigned long polling_frequency; | |
4be44fcd | 171 | volatile u8 zombie; |
1da177e4 LT |
172 | struct acpi_thermal_flags flags; |
173 | struct acpi_thermal_state state; | |
174 | struct acpi_thermal_trips trips; | |
4be44fcd LB |
175 | struct acpi_handle_list devices; |
176 | struct timer_list timer; | |
1da177e4 LT |
177 | }; |
178 | ||
d7508032 | 179 | static const struct file_operations acpi_thermal_state_fops = { |
4be44fcd LB |
180 | .open = acpi_thermal_state_open_fs, |
181 | .read = seq_read, | |
182 | .llseek = seq_lseek, | |
183 | .release = single_release, | |
1da177e4 LT |
184 | }; |
185 | ||
d7508032 | 186 | static const struct file_operations acpi_thermal_temp_fops = { |
4be44fcd LB |
187 | .open = acpi_thermal_temp_open_fs, |
188 | .read = seq_read, | |
189 | .llseek = seq_lseek, | |
190 | .release = single_release, | |
1da177e4 LT |
191 | }; |
192 | ||
d7508032 | 193 | static const struct file_operations acpi_thermal_trip_fops = { |
4be44fcd LB |
194 | .open = acpi_thermal_trip_open_fs, |
195 | .read = seq_read, | |
4be44fcd LB |
196 | .llseek = seq_lseek, |
197 | .release = single_release, | |
1da177e4 LT |
198 | }; |
199 | ||
d7508032 | 200 | static const struct file_operations acpi_thermal_cooling_fops = { |
4be44fcd LB |
201 | .open = acpi_thermal_cooling_open_fs, |
202 | .read = seq_read, | |
203 | .write = acpi_thermal_write_cooling_mode, | |
204 | .llseek = seq_lseek, | |
205 | .release = single_release, | |
1da177e4 LT |
206 | }; |
207 | ||
d7508032 | 208 | static const struct file_operations acpi_thermal_polling_fops = { |
4be44fcd LB |
209 | .open = acpi_thermal_polling_open_fs, |
210 | .read = seq_read, | |
211 | .write = acpi_thermal_write_polling, | |
212 | .llseek = seq_lseek, | |
213 | .release = single_release, | |
1da177e4 LT |
214 | }; |
215 | ||
216 | /* -------------------------------------------------------------------------- | |
217 | Thermal Zone Management | |
218 | -------------------------------------------------------------------------- */ | |
219 | ||
4be44fcd | 220 | static int acpi_thermal_get_temperature(struct acpi_thermal *tz) |
1da177e4 | 221 | { |
4be44fcd | 222 | acpi_status status = AE_OK; |
1da177e4 | 223 | |
1da177e4 LT |
224 | |
225 | if (!tz) | |
d550d98d | 226 | return -EINVAL; |
1da177e4 LT |
227 | |
228 | tz->last_temperature = tz->temperature; | |
229 | ||
4be44fcd | 230 | status = |
38ba7c9e | 231 | acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tz->temperature); |
1da177e4 | 232 | if (ACPI_FAILURE(status)) |
d550d98d | 233 | return -ENODEV; |
1da177e4 | 234 | |
4be44fcd LB |
235 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n", |
236 | tz->temperature)); | |
1da177e4 | 237 | |
d550d98d | 238 | return 0; |
1da177e4 LT |
239 | } |
240 | ||
4be44fcd | 241 | static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz) |
1da177e4 | 242 | { |
4be44fcd | 243 | acpi_status status = AE_OK; |
1da177e4 | 244 | |
1da177e4 LT |
245 | |
246 | if (!tz) | |
d550d98d | 247 | return -EINVAL; |
1da177e4 | 248 | |
4be44fcd | 249 | status = |
38ba7c9e | 250 | acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, |
4be44fcd | 251 | &tz->polling_frequency); |
1da177e4 | 252 | if (ACPI_FAILURE(status)) |
d550d98d | 253 | return -ENODEV; |
1da177e4 | 254 | |
4be44fcd LB |
255 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n", |
256 | tz->polling_frequency)); | |
1da177e4 | 257 | |
d550d98d | 258 | return 0; |
1da177e4 LT |
259 | } |
260 | ||
4be44fcd | 261 | static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds) |
1da177e4 | 262 | { |
1da177e4 LT |
263 | |
264 | if (!tz) | |
d550d98d | 265 | return -EINVAL; |
1da177e4 LT |
266 | |
267 | tz->polling_frequency = seconds * 10; /* Convert value to deci-seconds */ | |
268 | ||
4be44fcd LB |
269 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
270 | "Polling frequency set to %lu seconds\n", | |
636cedf9 | 271 | tz->polling_frequency/10)); |
1da177e4 | 272 | |
d550d98d | 273 | return 0; |
1da177e4 LT |
274 | } |
275 | ||
4be44fcd | 276 | static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode) |
1da177e4 | 277 | { |
4be44fcd LB |
278 | acpi_status status = AE_OK; |
279 | union acpi_object arg0 = { ACPI_TYPE_INTEGER }; | |
280 | struct acpi_object_list arg_list = { 1, &arg0 }; | |
281 | acpi_handle handle = NULL; | |
1da177e4 | 282 | |
1da177e4 LT |
283 | |
284 | if (!tz) | |
d550d98d | 285 | return -EINVAL; |
1da177e4 | 286 | |
38ba7c9e | 287 | status = acpi_get_handle(tz->device->handle, "_SCP", &handle); |
1da177e4 LT |
288 | if (ACPI_FAILURE(status)) { |
289 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n")); | |
d550d98d | 290 | return -ENODEV; |
1da177e4 LT |
291 | } |
292 | ||
293 | arg0.integer.value = mode; | |
294 | ||
295 | status = acpi_evaluate_object(handle, NULL, &arg_list, NULL); | |
296 | if (ACPI_FAILURE(status)) | |
d550d98d | 297 | return -ENODEV; |
1da177e4 | 298 | |
d550d98d | 299 | return 0; |
1da177e4 LT |
300 | } |
301 | ||
4be44fcd | 302 | static int acpi_thermal_get_trip_points(struct acpi_thermal *tz) |
1da177e4 | 303 | { |
4be44fcd LB |
304 | acpi_status status = AE_OK; |
305 | int i = 0; | |
1da177e4 | 306 | |
1da177e4 LT |
307 | |
308 | if (!tz) | |
d550d98d | 309 | return -EINVAL; |
1da177e4 LT |
310 | |
311 | /* Critical Shutdown (required) */ | |
312 | ||
38ba7c9e | 313 | status = acpi_evaluate_integer(tz->device->handle, "_CRT", NULL, |
4be44fcd | 314 | &tz->trips.critical.temperature); |
1da177e4 LT |
315 | if (ACPI_FAILURE(status)) { |
316 | tz->trips.critical.flags.valid = 0; | |
a6fc6720 | 317 | ACPI_EXCEPTION((AE_INFO, status, "No critical threshold")); |
d550d98d | 318 | return -ENODEV; |
4be44fcd | 319 | } else { |
1da177e4 | 320 | tz->trips.critical.flags.valid = 1; |
4be44fcd LB |
321 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
322 | "Found critical threshold [%lu]\n", | |
323 | tz->trips.critical.temperature)); | |
1da177e4 LT |
324 | } |
325 | ||
326 | /* Critical Sleep (optional) */ | |
327 | ||
4be44fcd | 328 | status = |
38ba7c9e | 329 | acpi_evaluate_integer(tz->device->handle, "_HOT", NULL, |
4be44fcd | 330 | &tz->trips.hot.temperature); |
1da177e4 LT |
331 | if (ACPI_FAILURE(status)) { |
332 | tz->trips.hot.flags.valid = 0; | |
333 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No hot threshold\n")); | |
4be44fcd | 334 | } else { |
1da177e4 | 335 | tz->trips.hot.flags.valid = 1; |
4be44fcd LB |
336 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found hot threshold [%lu]\n", |
337 | tz->trips.hot.temperature)); | |
1da177e4 LT |
338 | } |
339 | ||
340 | /* Passive: Processors (optional) */ | |
341 | ||
4be44fcd | 342 | status = |
38ba7c9e | 343 | acpi_evaluate_integer(tz->device->handle, "_PSV", NULL, |
4be44fcd | 344 | &tz->trips.passive.temperature); |
1da177e4 LT |
345 | if (ACPI_FAILURE(status)) { |
346 | tz->trips.passive.flags.valid = 0; | |
347 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No passive threshold\n")); | |
4be44fcd | 348 | } else { |
1da177e4 LT |
349 | tz->trips.passive.flags.valid = 1; |
350 | ||
4be44fcd | 351 | status = |
38ba7c9e | 352 | acpi_evaluate_integer(tz->device->handle, "_TC1", NULL, |
4be44fcd | 353 | &tz->trips.passive.tc1); |
1da177e4 LT |
354 | if (ACPI_FAILURE(status)) |
355 | tz->trips.passive.flags.valid = 0; | |
356 | ||
4be44fcd | 357 | status = |
38ba7c9e | 358 | acpi_evaluate_integer(tz->device->handle, "_TC2", NULL, |
4be44fcd | 359 | &tz->trips.passive.tc2); |
1da177e4 LT |
360 | if (ACPI_FAILURE(status)) |
361 | tz->trips.passive.flags.valid = 0; | |
362 | ||
4be44fcd | 363 | status = |
38ba7c9e | 364 | acpi_evaluate_integer(tz->device->handle, "_TSP", NULL, |
4be44fcd | 365 | &tz->trips.passive.tsp); |
1da177e4 LT |
366 | if (ACPI_FAILURE(status)) |
367 | tz->trips.passive.flags.valid = 0; | |
368 | ||
4be44fcd | 369 | status = |
38ba7c9e | 370 | acpi_evaluate_reference(tz->device->handle, "_PSL", NULL, |
4be44fcd | 371 | &tz->trips.passive.devices); |
1da177e4 LT |
372 | if (ACPI_FAILURE(status)) |
373 | tz->trips.passive.flags.valid = 0; | |
374 | ||
375 | if (!tz->trips.passive.flags.valid) | |
cece9296 | 376 | printk(KERN_WARNING PREFIX "Invalid passive threshold\n"); |
1da177e4 | 377 | else |
4be44fcd LB |
378 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
379 | "Found passive threshold [%lu]\n", | |
380 | tz->trips.passive.temperature)); | |
1da177e4 LT |
381 | } |
382 | ||
383 | /* Active: Fans, etc. (optional) */ | |
384 | ||
4be44fcd | 385 | for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) { |
1da177e4 | 386 | |
4be44fcd | 387 | char name[5] = { '_', 'A', 'C', ('0' + i), '\0' }; |
1da177e4 | 388 | |
4be44fcd | 389 | status = |
38ba7c9e | 390 | acpi_evaluate_integer(tz->device->handle, name, NULL, |
4be44fcd | 391 | &tz->trips.active[i].temperature); |
1da177e4 LT |
392 | if (ACPI_FAILURE(status)) |
393 | break; | |
394 | ||
395 | name[2] = 'L'; | |
4be44fcd | 396 | status = |
38ba7c9e | 397 | acpi_evaluate_reference(tz->device->handle, name, NULL, |
4be44fcd | 398 | &tz->trips.active[i].devices); |
1da177e4 LT |
399 | if (ACPI_SUCCESS(status)) { |
400 | tz->trips.active[i].flags.valid = 1; | |
4be44fcd LB |
401 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
402 | "Found active threshold [%d]:[%lu]\n", | |
403 | i, tz->trips.active[i].temperature)); | |
404 | } else | |
a6fc6720 TR |
405 | ACPI_EXCEPTION((AE_INFO, status, |
406 | "Invalid active threshold [%d]", i)); | |
1da177e4 LT |
407 | } |
408 | ||
d550d98d | 409 | return 0; |
1da177e4 LT |
410 | } |
411 | ||
4be44fcd | 412 | static int acpi_thermal_get_devices(struct acpi_thermal *tz) |
1da177e4 | 413 | { |
4be44fcd | 414 | acpi_status status = AE_OK; |
1da177e4 | 415 | |
1da177e4 LT |
416 | |
417 | if (!tz) | |
d550d98d | 418 | return -EINVAL; |
1da177e4 | 419 | |
4be44fcd | 420 | status = |
38ba7c9e | 421 | acpi_evaluate_reference(tz->device->handle, "_TZD", NULL, &tz->devices); |
1da177e4 | 422 | if (ACPI_FAILURE(status)) |
d550d98d | 423 | return -ENODEV; |
1da177e4 | 424 | |
d550d98d | 425 | return 0; |
1da177e4 LT |
426 | } |
427 | ||
4be44fcd | 428 | static int acpi_thermal_critical(struct acpi_thermal *tz) |
1da177e4 | 429 | { |
1da177e4 | 430 | if (!tz || !tz->trips.critical.flags.valid) |
d550d98d | 431 | return -EINVAL; |
1da177e4 LT |
432 | |
433 | if (tz->temperature >= tz->trips.critical.temperature) { | |
cece9296 | 434 | printk(KERN_WARNING PREFIX "Critical trip point\n"); |
1da177e4 | 435 | tz->trips.critical.flags.enabled = 1; |
4be44fcd | 436 | } else if (tz->trips.critical.flags.enabled) |
1da177e4 LT |
437 | tz->trips.critical.flags.enabled = 0; |
438 | ||
4be44fcd LB |
439 | printk(KERN_EMERG |
440 | "Critical temperature reached (%ld C), shutting down.\n", | |
441 | KELVIN_TO_CELSIUS(tz->temperature)); | |
8348e1b1 | 442 | acpi_bus_generate_event(tz->device, ACPI_THERMAL_NOTIFY_CRITICAL, |
4be44fcd | 443 | tz->trips.critical.flags.enabled); |
1da177e4 | 444 | |
10a0a8d4 | 445 | orderly_poweroff(true); |
1da177e4 | 446 | |
d550d98d | 447 | return 0; |
1da177e4 LT |
448 | } |
449 | ||
4be44fcd | 450 | static int acpi_thermal_hot(struct acpi_thermal *tz) |
1da177e4 | 451 | { |
1da177e4 | 452 | if (!tz || !tz->trips.hot.flags.valid) |
d550d98d | 453 | return -EINVAL; |
1da177e4 LT |
454 | |
455 | if (tz->temperature >= tz->trips.hot.temperature) { | |
cece9296 | 456 | printk(KERN_WARNING PREFIX "Hot trip point\n"); |
1da177e4 | 457 | tz->trips.hot.flags.enabled = 1; |
4be44fcd | 458 | } else if (tz->trips.hot.flags.enabled) |
1da177e4 LT |
459 | tz->trips.hot.flags.enabled = 0; |
460 | ||
8348e1b1 | 461 | acpi_bus_generate_event(tz->device, ACPI_THERMAL_NOTIFY_HOT, |
4be44fcd | 462 | tz->trips.hot.flags.enabled); |
1da177e4 LT |
463 | |
464 | /* TBD: Call user-mode "sleep(S4)" function */ | |
465 | ||
d550d98d | 466 | return 0; |
1da177e4 LT |
467 | } |
468 | ||
1cbf4c56 | 469 | static void acpi_thermal_passive(struct acpi_thermal *tz) |
1da177e4 | 470 | { |
1cbf4c56 | 471 | int result = 1; |
1da177e4 | 472 | struct acpi_thermal_passive *passive = NULL; |
4be44fcd LB |
473 | int trend = 0; |
474 | int i = 0; | |
1da177e4 | 475 | |
1da177e4 LT |
476 | |
477 | if (!tz || !tz->trips.passive.flags.valid) | |
1cbf4c56 | 478 | return; |
1da177e4 LT |
479 | |
480 | passive = &(tz->trips.passive); | |
481 | ||
482 | /* | |
483 | * Above Trip? | |
484 | * ----------- | |
485 | * Calculate the thermal trend (using the passive cooling equation) | |
486 | * and modify the performance limit for all passive cooling devices | |
487 | * accordingly. Note that we assume symmetry. | |
488 | */ | |
489 | if (tz->temperature >= passive->temperature) { | |
4be44fcd LB |
490 | trend = |
491 | (passive->tc1 * (tz->temperature - tz->last_temperature)) + | |
492 | (passive->tc2 * (tz->temperature - passive->temperature)); | |
493 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | |
494 | "trend[%d]=(tc1[%lu]*(tmp[%lu]-last[%lu]))+(tc2[%lu]*(tmp[%lu]-psv[%lu]))\n", | |
495 | trend, passive->tc1, tz->temperature, | |
496 | tz->last_temperature, passive->tc2, | |
497 | tz->temperature, passive->temperature)); | |
1cbf4c56 | 498 | passive->flags.enabled = 1; |
1da177e4 LT |
499 | /* Heating up? */ |
500 | if (trend > 0) | |
4be44fcd LB |
501 | for (i = 0; i < passive->devices.count; i++) |
502 | acpi_processor_set_thermal_limit(passive-> | |
503 | devices. | |
504 | handles[i], | |
505 | ACPI_PROCESSOR_LIMIT_INCREMENT); | |
1da177e4 | 506 | /* Cooling off? */ |
1cbf4c56 | 507 | else if (trend < 0) { |
4be44fcd | 508 | for (i = 0; i < passive->devices.count; i++) |
1cbf4c56 TR |
509 | /* |
510 | * assume that we are on highest | |
511 | * freq/lowest thrott and can leave | |
512 | * passive mode, even in error case | |
513 | */ | |
514 | if (!acpi_processor_set_thermal_limit | |
515 | (passive->devices.handles[i], | |
516 | ACPI_PROCESSOR_LIMIT_DECREMENT)) | |
517 | result = 0; | |
518 | /* | |
519 | * Leave cooling mode, even if the temp might | |
520 | * higher than trip point This is because some | |
521 | * machines might have long thermal polling | |
522 | * frequencies (tsp) defined. We will fall back | |
523 | * into passive mode in next cycle (probably quicker) | |
524 | */ | |
525 | if (result) { | |
526 | passive->flags.enabled = 0; | |
527 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | |
528 | "Disabling passive cooling, still above threshold," | |
529 | " but we are cooling down\n")); | |
530 | } | |
531 | } | |
532 | return; | |
1da177e4 LT |
533 | } |
534 | ||
535 | /* | |
536 | * Below Trip? | |
537 | * ----------- | |
538 | * Implement passive cooling hysteresis to slowly increase performance | |
539 | * and avoid thrashing around the passive trip point. Note that we | |
540 | * assume symmetry. | |
541 | */ | |
1cbf4c56 TR |
542 | if (!passive->flags.enabled) |
543 | return; | |
544 | for (i = 0; i < passive->devices.count; i++) | |
545 | if (!acpi_processor_set_thermal_limit | |
546 | (passive->devices.handles[i], | |
547 | ACPI_PROCESSOR_LIMIT_DECREMENT)) | |
548 | result = 0; | |
549 | if (result) { | |
550 | passive->flags.enabled = 0; | |
551 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | |
552 | "Disabling passive cooling (zone is cool)\n")); | |
1da177e4 | 553 | } |
1da177e4 LT |
554 | } |
555 | ||
1cbf4c56 | 556 | static void acpi_thermal_active(struct acpi_thermal *tz) |
1da177e4 | 557 | { |
4be44fcd | 558 | int result = 0; |
1da177e4 | 559 | struct acpi_thermal_active *active = NULL; |
4be44fcd LB |
560 | int i = 0; |
561 | int j = 0; | |
562 | unsigned long maxtemp = 0; | |
1da177e4 | 563 | |
1da177e4 LT |
564 | |
565 | if (!tz) | |
1cbf4c56 | 566 | return; |
1da177e4 | 567 | |
4be44fcd | 568 | for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) { |
1da177e4 LT |
569 | active = &(tz->trips.active[i]); |
570 | if (!active || !active->flags.valid) | |
571 | break; | |
1da177e4 | 572 | if (tz->temperature >= active->temperature) { |
1cbf4c56 TR |
573 | /* |
574 | * Above Threshold? | |
575 | * ---------------- | |
576 | * If not already enabled, turn ON all cooling devices | |
577 | * associated with this active threshold. | |
578 | */ | |
1da177e4 | 579 | if (active->temperature > maxtemp) |
1cbf4c56 TR |
580 | tz->state.active_index = i; |
581 | maxtemp = active->temperature; | |
582 | if (active->flags.enabled) | |
583 | continue; | |
1da177e4 | 584 | for (j = 0; j < active->devices.count; j++) { |
4be44fcd LB |
585 | result = |
586 | acpi_bus_set_power(active->devices. | |
587 | handles[j], | |
1cbf4c56 | 588 | ACPI_STATE_D0); |
1da177e4 | 589 | if (result) { |
cece9296 LB |
590 | printk(KERN_WARNING PREFIX |
591 | "Unable to turn cooling device [%p] 'on'\n", | |
a6fc6720 | 592 | active->devices. |
cece9296 | 593 | handles[j]); |
1da177e4 LT |
594 | continue; |
595 | } | |
1cbf4c56 | 596 | active->flags.enabled = 1; |
4be44fcd | 597 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
1cbf4c56 | 598 | "Cooling device [%p] now 'on'\n", |
4be44fcd | 599 | active->devices.handles[j])); |
1da177e4 | 600 | } |
1cbf4c56 TR |
601 | continue; |
602 | } | |
603 | if (!active->flags.enabled) | |
604 | continue; | |
605 | /* | |
606 | * Below Threshold? | |
607 | * ---------------- | |
608 | * Turn OFF all cooling devices associated with this | |
609 | * threshold. | |
610 | */ | |
611 | for (j = 0; j < active->devices.count; j++) { | |
612 | result = acpi_bus_set_power(active->devices.handles[j], | |
613 | ACPI_STATE_D3); | |
614 | if (result) { | |
cece9296 LB |
615 | printk(KERN_WARNING PREFIX |
616 | "Unable to turn cooling device [%p] 'off'\n", | |
617 | active->devices.handles[j]); | |
1cbf4c56 TR |
618 | continue; |
619 | } | |
620 | active->flags.enabled = 0; | |
621 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | |
622 | "Cooling device [%p] now 'off'\n", | |
623 | active->devices.handles[j])); | |
1da177e4 LT |
624 | } |
625 | } | |
1da177e4 LT |
626 | } |
627 | ||
4be44fcd | 628 | static void acpi_thermal_check(void *context); |
1da177e4 | 629 | |
4be44fcd | 630 | static void acpi_thermal_run(unsigned long data) |
1da177e4 LT |
631 | { |
632 | struct acpi_thermal *tz = (struct acpi_thermal *)data; | |
633 | if (!tz->zombie) | |
b8d35192 | 634 | acpi_os_execute(OSL_GPE_HANDLER, acpi_thermal_check, (void *)data); |
1da177e4 LT |
635 | } |
636 | ||
4be44fcd | 637 | static void acpi_thermal_check(void *data) |
1da177e4 | 638 | { |
4be44fcd | 639 | int result = 0; |
50dd0969 | 640 | struct acpi_thermal *tz = data; |
4be44fcd LB |
641 | unsigned long sleep_time = 0; |
642 | int i = 0; | |
1da177e4 LT |
643 | struct acpi_thermal_state state; |
644 | ||
1da177e4 LT |
645 | |
646 | if (!tz) { | |
6468463a | 647 | printk(KERN_ERR PREFIX "Invalid (NULL) context\n"); |
d550d98d | 648 | return; |
1da177e4 LT |
649 | } |
650 | ||
651 | state = tz->state; | |
652 | ||
653 | result = acpi_thermal_get_temperature(tz); | |
654 | if (result) | |
d550d98d | 655 | return; |
4be44fcd | 656 | |
1da177e4 | 657 | memset(&tz->state, 0, sizeof(tz->state)); |
4be44fcd | 658 | |
1da177e4 LT |
659 | /* |
660 | * Check Trip Points | |
661 | * ----------------- | |
662 | * Compare the current temperature to the trip point values to see | |
663 | * if we've entered one of the thermal policy states. Note that | |
664 | * this function determines when a state is entered, but the | |
665 | * individual policy decides when it is exited (e.g. hysteresis). | |
666 | */ | |
667 | if (tz->trips.critical.flags.valid) | |
4be44fcd LB |
668 | state.critical |= |
669 | (tz->temperature >= tz->trips.critical.temperature); | |
1da177e4 LT |
670 | if (tz->trips.hot.flags.valid) |
671 | state.hot |= (tz->temperature >= tz->trips.hot.temperature); | |
672 | if (tz->trips.passive.flags.valid) | |
4be44fcd LB |
673 | state.passive |= |
674 | (tz->temperature >= tz->trips.passive.temperature); | |
675 | for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) | |
1da177e4 | 676 | if (tz->trips.active[i].flags.valid) |
4be44fcd LB |
677 | state.active |= |
678 | (tz->temperature >= | |
679 | tz->trips.active[i].temperature); | |
1da177e4 LT |
680 | |
681 | /* | |
682 | * Invoke Policy | |
683 | * ------------- | |
684 | * Separated from the above check to allow individual policy to | |
685 | * determine when to exit a given state. | |
686 | */ | |
687 | if (state.critical) | |
688 | acpi_thermal_critical(tz); | |
689 | if (state.hot) | |
690 | acpi_thermal_hot(tz); | |
691 | if (state.passive) | |
692 | acpi_thermal_passive(tz); | |
693 | if (state.active) | |
694 | acpi_thermal_active(tz); | |
695 | ||
696 | /* | |
697 | * Calculate State | |
698 | * --------------- | |
699 | * Again, separated from the above two to allow independent policy | |
700 | * decisions. | |
701 | */ | |
1cbf4c56 TR |
702 | tz->state.critical = tz->trips.critical.flags.enabled; |
703 | tz->state.hot = tz->trips.hot.flags.enabled; | |
704 | tz->state.passive = tz->trips.passive.flags.enabled; | |
705 | tz->state.active = 0; | |
4be44fcd | 706 | for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) |
1cbf4c56 | 707 | tz->state.active |= tz->trips.active[i].flags.enabled; |
1da177e4 LT |
708 | |
709 | /* | |
710 | * Calculate Sleep Time | |
711 | * -------------------- | |
712 | * If we're in the passive state, use _TSP's value. Otherwise | |
713 | * use the default polling frequency (e.g. _TZP). If no polling | |
714 | * frequency is specified then we'll wait forever (at least until | |
715 | * a thermal event occurs). Note that _TSP and _TZD values are | |
716 | * given in 1/10th seconds (we must covert to milliseconds). | |
717 | */ | |
718 | if (tz->state.passive) | |
719 | sleep_time = tz->trips.passive.tsp * 100; | |
720 | else if (tz->polling_frequency > 0) | |
721 | sleep_time = tz->polling_frequency * 100; | |
722 | ||
4be44fcd LB |
723 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: temperature[%lu] sleep[%lu]\n", |
724 | tz->name, tz->temperature, sleep_time)); | |
1da177e4 LT |
725 | |
726 | /* | |
727 | * Schedule Next Poll | |
728 | * ------------------ | |
729 | */ | |
730 | if (!sleep_time) { | |
731 | if (timer_pending(&(tz->timer))) | |
732 | del_timer(&(tz->timer)); | |
4be44fcd | 733 | } else { |
1da177e4 | 734 | if (timer_pending(&(tz->timer))) |
94e22e13 AM |
735 | mod_timer(&(tz->timer), |
736 | jiffies + (HZ * sleep_time) / 1000); | |
1da177e4 | 737 | else { |
4be44fcd | 738 | tz->timer.data = (unsigned long)tz; |
1da177e4 LT |
739 | tz->timer.function = acpi_thermal_run; |
740 | tz->timer.expires = jiffies + (HZ * sleep_time) / 1000; | |
741 | add_timer(&(tz->timer)); | |
742 | } | |
743 | } | |
744 | ||
d550d98d | 745 | return; |
1da177e4 LT |
746 | } |
747 | ||
1da177e4 LT |
748 | /* -------------------------------------------------------------------------- |
749 | FS Interface (/proc) | |
750 | -------------------------------------------------------------------------- */ | |
751 | ||
4be44fcd | 752 | static struct proc_dir_entry *acpi_thermal_dir; |
1da177e4 LT |
753 | |
754 | static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset) | |
755 | { | |
50dd0969 | 756 | struct acpi_thermal *tz = seq->private; |
1da177e4 | 757 | |
1da177e4 LT |
758 | |
759 | if (!tz) | |
760 | goto end; | |
761 | ||
762 | seq_puts(seq, "state: "); | |
763 | ||
4be44fcd LB |
764 | if (!tz->state.critical && !tz->state.hot && !tz->state.passive |
765 | && !tz->state.active) | |
1da177e4 LT |
766 | seq_puts(seq, "ok\n"); |
767 | else { | |
768 | if (tz->state.critical) | |
769 | seq_puts(seq, "critical "); | |
770 | if (tz->state.hot) | |
771 | seq_puts(seq, "hot "); | |
772 | if (tz->state.passive) | |
773 | seq_puts(seq, "passive "); | |
774 | if (tz->state.active) | |
775 | seq_printf(seq, "active[%d]", tz->state.active_index); | |
776 | seq_puts(seq, "\n"); | |
777 | } | |
778 | ||
4be44fcd | 779 | end: |
d550d98d | 780 | return 0; |
1da177e4 LT |
781 | } |
782 | ||
783 | static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file) | |
784 | { | |
785 | return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data); | |
786 | } | |
787 | ||
1da177e4 LT |
788 | static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset) |
789 | { | |
4be44fcd | 790 | int result = 0; |
50dd0969 | 791 | struct acpi_thermal *tz = seq->private; |
1da177e4 | 792 | |
1da177e4 LT |
793 | |
794 | if (!tz) | |
795 | goto end; | |
796 | ||
797 | result = acpi_thermal_get_temperature(tz); | |
798 | if (result) | |
799 | goto end; | |
800 | ||
4be44fcd LB |
801 | seq_printf(seq, "temperature: %ld C\n", |
802 | KELVIN_TO_CELSIUS(tz->temperature)); | |
1da177e4 | 803 | |
4be44fcd | 804 | end: |
d550d98d | 805 | return 0; |
1da177e4 LT |
806 | } |
807 | ||
808 | static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file) | |
809 | { | |
810 | return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data); | |
811 | } | |
812 | ||
1da177e4 LT |
813 | static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset) |
814 | { | |
50dd0969 | 815 | struct acpi_thermal *tz = seq->private; |
68ccfaa8 | 816 | struct acpi_device *device; |
e7c746ef TR |
817 | acpi_status status; |
818 | ||
4be44fcd LB |
819 | int i = 0; |
820 | int j = 0; | |
1da177e4 | 821 | |
1da177e4 LT |
822 | |
823 | if (!tz) | |
824 | goto end; | |
825 | ||
826 | if (tz->trips.critical.flags.valid) | |
827 | seq_printf(seq, "critical (S5): %ld C\n", | |
4be44fcd | 828 | KELVIN_TO_CELSIUS(tz->trips.critical.temperature)); |
1da177e4 LT |
829 | |
830 | if (tz->trips.hot.flags.valid) | |
831 | seq_printf(seq, "hot (S4): %ld C\n", | |
4be44fcd | 832 | KELVIN_TO_CELSIUS(tz->trips.hot.temperature)); |
1da177e4 LT |
833 | |
834 | if (tz->trips.passive.flags.valid) { | |
4be44fcd LB |
835 | seq_printf(seq, |
836 | "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=", | |
837 | KELVIN_TO_CELSIUS(tz->trips.passive.temperature), | |
838 | tz->trips.passive.tc1, tz->trips.passive.tc2, | |
839 | tz->trips.passive.tsp); | |
840 | for (j = 0; j < tz->trips.passive.devices.count; j++) { | |
e7c746ef TR |
841 | status = acpi_bus_get_device(tz->trips.passive.devices. |
842 | handles[j], &device); | |
843 | seq_printf(seq, "%4.4s ", status ? "" : | |
844 | acpi_device_bid(device)); | |
1da177e4 LT |
845 | } |
846 | seq_puts(seq, "\n"); | |
847 | } | |
848 | ||
849 | for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) { | |
850 | if (!(tz->trips.active[i].flags.valid)) | |
851 | break; | |
852 | seq_printf(seq, "active[%d]: %ld C: devices=", | |
4be44fcd LB |
853 | i, |
854 | KELVIN_TO_CELSIUS(tz->trips.active[i].temperature)); | |
68ccfaa8 | 855 | for (j = 0; j < tz->trips.active[i].devices.count; j++){ |
e7c746ef TR |
856 | status = acpi_bus_get_device(tz->trips.active[i]. |
857 | devices.handles[j], | |
858 | &device); | |
859 | seq_printf(seq, "%4.4s ", status ? "" : | |
860 | acpi_device_bid(device)); | |
68ccfaa8 | 861 | } |
1da177e4 LT |
862 | seq_puts(seq, "\n"); |
863 | } | |
864 | ||
4be44fcd | 865 | end: |
d550d98d | 866 | return 0; |
1da177e4 LT |
867 | } |
868 | ||
869 | static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file) | |
870 | { | |
871 | return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data); | |
872 | } | |
873 | ||
1da177e4 LT |
874 | static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset) |
875 | { | |
50dd0969 | 876 | struct acpi_thermal *tz = seq->private; |
1da177e4 | 877 | |
1da177e4 LT |
878 | |
879 | if (!tz) | |
880 | goto end; | |
881 | ||
eaca2d3f | 882 | if (!tz->flags.cooling_mode) |
1da177e4 | 883 | seq_puts(seq, "<setting not supported>\n"); |
1da177e4 | 884 | else |
eaca2d3f | 885 | seq_puts(seq, "0 - Active; 1 - Passive\n"); |
1da177e4 | 886 | |
4be44fcd | 887 | end: |
d550d98d | 888 | return 0; |
1da177e4 LT |
889 | } |
890 | ||
891 | static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file) | |
892 | { | |
893 | return single_open(file, acpi_thermal_cooling_seq_show, | |
4be44fcd | 894 | PDE(inode)->data); |
1da177e4 LT |
895 | } |
896 | ||
897 | static ssize_t | |
4be44fcd LB |
898 | acpi_thermal_write_cooling_mode(struct file *file, |
899 | const char __user * buffer, | |
900 | size_t count, loff_t * ppos) | |
1da177e4 | 901 | { |
50dd0969 JE |
902 | struct seq_file *m = file->private_data; |
903 | struct acpi_thermal *tz = m->private; | |
4be44fcd LB |
904 | int result = 0; |
905 | char mode_string[12] = { '\0' }; | |
1da177e4 | 906 | |
1da177e4 LT |
907 | |
908 | if (!tz || (count > sizeof(mode_string) - 1)) | |
d550d98d | 909 | return -EINVAL; |
1da177e4 LT |
910 | |
911 | if (!tz->flags.cooling_mode) | |
d550d98d | 912 | return -ENODEV; |
1da177e4 LT |
913 | |
914 | if (copy_from_user(mode_string, buffer, count)) | |
d550d98d | 915 | return -EFAULT; |
4be44fcd | 916 | |
1da177e4 | 917 | mode_string[count] = '\0'; |
4be44fcd LB |
918 | |
919 | result = acpi_thermal_set_cooling_mode(tz, | |
920 | simple_strtoul(mode_string, NULL, | |
921 | 0)); | |
1da177e4 | 922 | if (result) |
d550d98d | 923 | return result; |
1da177e4 LT |
924 | |
925 | acpi_thermal_check(tz); | |
926 | ||
d550d98d | 927 | return count; |
1da177e4 LT |
928 | } |
929 | ||
1da177e4 LT |
930 | static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset) |
931 | { | |
50dd0969 | 932 | struct acpi_thermal *tz = seq->private; |
1da177e4 | 933 | |
1da177e4 LT |
934 | |
935 | if (!tz) | |
936 | goto end; | |
937 | ||
938 | if (!tz->polling_frequency) { | |
939 | seq_puts(seq, "<polling disabled>\n"); | |
940 | goto end; | |
941 | } | |
942 | ||
943 | seq_printf(seq, "polling frequency: %lu seconds\n", | |
4be44fcd | 944 | (tz->polling_frequency / 10)); |
1da177e4 | 945 | |
4be44fcd | 946 | end: |
d550d98d | 947 | return 0; |
1da177e4 LT |
948 | } |
949 | ||
950 | static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file) | |
951 | { | |
952 | return single_open(file, acpi_thermal_polling_seq_show, | |
4be44fcd | 953 | PDE(inode)->data); |
1da177e4 LT |
954 | } |
955 | ||
956 | static ssize_t | |
4be44fcd LB |
957 | acpi_thermal_write_polling(struct file *file, |
958 | const char __user * buffer, | |
959 | size_t count, loff_t * ppos) | |
1da177e4 | 960 | { |
50dd0969 JE |
961 | struct seq_file *m = file->private_data; |
962 | struct acpi_thermal *tz = m->private; | |
4be44fcd LB |
963 | int result = 0; |
964 | char polling_string[12] = { '\0' }; | |
965 | int seconds = 0; | |
1da177e4 | 966 | |
1da177e4 LT |
967 | |
968 | if (!tz || (count > sizeof(polling_string) - 1)) | |
d550d98d | 969 | return -EINVAL; |
4be44fcd | 970 | |
1da177e4 | 971 | if (copy_from_user(polling_string, buffer, count)) |
d550d98d | 972 | return -EFAULT; |
4be44fcd | 973 | |
1da177e4 LT |
974 | polling_string[count] = '\0'; |
975 | ||
976 | seconds = simple_strtoul(polling_string, NULL, 0); | |
4be44fcd | 977 | |
1da177e4 LT |
978 | result = acpi_thermal_set_polling(tz, seconds); |
979 | if (result) | |
d550d98d | 980 | return result; |
1da177e4 LT |
981 | |
982 | acpi_thermal_check(tz); | |
983 | ||
d550d98d | 984 | return count; |
1da177e4 LT |
985 | } |
986 | ||
4be44fcd | 987 | static int acpi_thermal_add_fs(struct acpi_device *device) |
1da177e4 | 988 | { |
4be44fcd | 989 | struct proc_dir_entry *entry = NULL; |
1da177e4 | 990 | |
1da177e4 LT |
991 | |
992 | if (!acpi_device_dir(device)) { | |
993 | acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), | |
4be44fcd | 994 | acpi_thermal_dir); |
1da177e4 | 995 | if (!acpi_device_dir(device)) |
d550d98d | 996 | return -ENODEV; |
1da177e4 LT |
997 | acpi_device_dir(device)->owner = THIS_MODULE; |
998 | } | |
999 | ||
1000 | /* 'state' [R] */ | |
1001 | entry = create_proc_entry(ACPI_THERMAL_FILE_STATE, | |
4be44fcd | 1002 | S_IRUGO, acpi_device_dir(device)); |
1da177e4 | 1003 | if (!entry) |
d550d98d | 1004 | return -ENODEV; |
1da177e4 LT |
1005 | else { |
1006 | entry->proc_fops = &acpi_thermal_state_fops; | |
1007 | entry->data = acpi_driver_data(device); | |
1008 | entry->owner = THIS_MODULE; | |
1009 | } | |
1010 | ||
1011 | /* 'temperature' [R] */ | |
1012 | entry = create_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE, | |
4be44fcd | 1013 | S_IRUGO, acpi_device_dir(device)); |
1da177e4 | 1014 | if (!entry) |
d550d98d | 1015 | return -ENODEV; |
1da177e4 LT |
1016 | else { |
1017 | entry->proc_fops = &acpi_thermal_temp_fops; | |
1018 | entry->data = acpi_driver_data(device); | |
1019 | entry->owner = THIS_MODULE; | |
1020 | } | |
1021 | ||
1022 | /* 'trip_points' [R/W] */ | |
1023 | entry = create_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS, | |
4be44fcd LB |
1024 | S_IFREG | S_IRUGO | S_IWUSR, |
1025 | acpi_device_dir(device)); | |
1da177e4 | 1026 | if (!entry) |
d550d98d | 1027 | return -ENODEV; |
1da177e4 LT |
1028 | else { |
1029 | entry->proc_fops = &acpi_thermal_trip_fops; | |
1030 | entry->data = acpi_driver_data(device); | |
1031 | entry->owner = THIS_MODULE; | |
1032 | } | |
1033 | ||
1034 | /* 'cooling_mode' [R/W] */ | |
1035 | entry = create_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE, | |
4be44fcd LB |
1036 | S_IFREG | S_IRUGO | S_IWUSR, |
1037 | acpi_device_dir(device)); | |
1da177e4 | 1038 | if (!entry) |
d550d98d | 1039 | return -ENODEV; |
1da177e4 LT |
1040 | else { |
1041 | entry->proc_fops = &acpi_thermal_cooling_fops; | |
1042 | entry->data = acpi_driver_data(device); | |
1043 | entry->owner = THIS_MODULE; | |
1044 | } | |
1045 | ||
1046 | /* 'polling_frequency' [R/W] */ | |
1047 | entry = create_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ, | |
4be44fcd LB |
1048 | S_IFREG | S_IRUGO | S_IWUSR, |
1049 | acpi_device_dir(device)); | |
1da177e4 | 1050 | if (!entry) |
d550d98d | 1051 | return -ENODEV; |
1da177e4 LT |
1052 | else { |
1053 | entry->proc_fops = &acpi_thermal_polling_fops; | |
1054 | entry->data = acpi_driver_data(device); | |
1055 | entry->owner = THIS_MODULE; | |
1056 | } | |
1057 | ||
d550d98d | 1058 | return 0; |
1da177e4 LT |
1059 | } |
1060 | ||
4be44fcd | 1061 | static int acpi_thermal_remove_fs(struct acpi_device *device) |
1da177e4 | 1062 | { |
1da177e4 LT |
1063 | |
1064 | if (acpi_device_dir(device)) { | |
1065 | remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ, | |
1066 | acpi_device_dir(device)); | |
1067 | remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE, | |
1068 | acpi_device_dir(device)); | |
1069 | remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS, | |
1070 | acpi_device_dir(device)); | |
1071 | remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE, | |
1072 | acpi_device_dir(device)); | |
1073 | remove_proc_entry(ACPI_THERMAL_FILE_STATE, | |
1074 | acpi_device_dir(device)); | |
1075 | remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir); | |
1076 | acpi_device_dir(device) = NULL; | |
1077 | } | |
1078 | ||
d550d98d | 1079 | return 0; |
1da177e4 LT |
1080 | } |
1081 | ||
1da177e4 LT |
1082 | /* -------------------------------------------------------------------------- |
1083 | Driver Interface | |
1084 | -------------------------------------------------------------------------- */ | |
1085 | ||
4be44fcd | 1086 | static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data) |
1da177e4 | 1087 | { |
50dd0969 | 1088 | struct acpi_thermal *tz = data; |
4be44fcd | 1089 | struct acpi_device *device = NULL; |
1da177e4 | 1090 | |
1da177e4 LT |
1091 | |
1092 | if (!tz) | |
d550d98d | 1093 | return; |
1da177e4 | 1094 | |
8348e1b1 | 1095 | device = tz->device; |
1da177e4 LT |
1096 | |
1097 | switch (event) { | |
1098 | case ACPI_THERMAL_NOTIFY_TEMPERATURE: | |
1099 | acpi_thermal_check(tz); | |
1100 | break; | |
1101 | case ACPI_THERMAL_NOTIFY_THRESHOLDS: | |
1102 | acpi_thermal_get_trip_points(tz); | |
1103 | acpi_thermal_check(tz); | |
1104 | acpi_bus_generate_event(device, event, 0); | |
1105 | break; | |
1106 | case ACPI_THERMAL_NOTIFY_DEVICES: | |
1107 | if (tz->flags.devices) | |
1108 | acpi_thermal_get_devices(tz); | |
1109 | acpi_bus_generate_event(device, event, 0); | |
1110 | break; | |
1111 | default: | |
1112 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | |
4be44fcd | 1113 | "Unsupported event [0x%x]\n", event)); |
1da177e4 LT |
1114 | break; |
1115 | } | |
1116 | ||
d550d98d | 1117 | return; |
1da177e4 LT |
1118 | } |
1119 | ||
4be44fcd | 1120 | static int acpi_thermal_get_info(struct acpi_thermal *tz) |
1da177e4 | 1121 | { |
4be44fcd | 1122 | int result = 0; |
1da177e4 | 1123 | |
1da177e4 LT |
1124 | |
1125 | if (!tz) | |
d550d98d | 1126 | return -EINVAL; |
1da177e4 LT |
1127 | |
1128 | /* Get temperature [_TMP] (required) */ | |
1129 | result = acpi_thermal_get_temperature(tz); | |
1130 | if (result) | |
d550d98d | 1131 | return result; |
1da177e4 LT |
1132 | |
1133 | /* Get trip points [_CRT, _PSV, etc.] (required) */ | |
1134 | result = acpi_thermal_get_trip_points(tz); | |
1135 | if (result) | |
d550d98d | 1136 | return result; |
1da177e4 LT |
1137 | |
1138 | /* Set the cooling mode [_SCP] to active cooling (default) */ | |
1139 | result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE); | |
4be44fcd | 1140 | if (!result) |
1da177e4 | 1141 | tz->flags.cooling_mode = 1; |
1da177e4 LT |
1142 | |
1143 | /* Get default polling frequency [_TZP] (optional) */ | |
1144 | if (tzp) | |
1145 | tz->polling_frequency = tzp; | |
1146 | else | |
1147 | acpi_thermal_get_polling_frequency(tz); | |
1148 | ||
1149 | /* Get devices in this thermal zone [_TZD] (optional) */ | |
1150 | result = acpi_thermal_get_devices(tz); | |
1151 | if (!result) | |
1152 | tz->flags.devices = 1; | |
1153 | ||
d550d98d | 1154 | return 0; |
1da177e4 LT |
1155 | } |
1156 | ||
4be44fcd | 1157 | static int acpi_thermal_add(struct acpi_device *device) |
1da177e4 | 1158 | { |
4be44fcd LB |
1159 | int result = 0; |
1160 | acpi_status status = AE_OK; | |
1161 | struct acpi_thermal *tz = NULL; | |
1da177e4 | 1162 | |
1da177e4 LT |
1163 | |
1164 | if (!device) | |
d550d98d | 1165 | return -EINVAL; |
1da177e4 | 1166 | |
36bcbec7 | 1167 | tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL); |
1da177e4 | 1168 | if (!tz) |
d550d98d | 1169 | return -ENOMEM; |
1da177e4 | 1170 | |
8348e1b1 | 1171 | tz->device = device; |
1da177e4 LT |
1172 | strcpy(tz->name, device->pnp.bus_id); |
1173 | strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME); | |
1174 | strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS); | |
1175 | acpi_driver_data(device) = tz; | |
1176 | ||
1177 | result = acpi_thermal_get_info(tz); | |
1178 | if (result) | |
1179 | goto end; | |
1180 | ||
1181 | result = acpi_thermal_add_fs(device); | |
1182 | if (result) | |
09047e75 | 1183 | goto end; |
1da177e4 LT |
1184 | |
1185 | init_timer(&tz->timer); | |
1186 | ||
1187 | acpi_thermal_check(tz); | |
1188 | ||
38ba7c9e | 1189 | status = acpi_install_notify_handler(device->handle, |
4be44fcd LB |
1190 | ACPI_DEVICE_NOTIFY, |
1191 | acpi_thermal_notify, tz); | |
1da177e4 | 1192 | if (ACPI_FAILURE(status)) { |
1da177e4 LT |
1193 | result = -ENODEV; |
1194 | goto end; | |
1195 | } | |
1196 | ||
1197 | printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n", | |
4be44fcd LB |
1198 | acpi_device_name(device), acpi_device_bid(device), |
1199 | KELVIN_TO_CELSIUS(tz->temperature)); | |
1da177e4 | 1200 | |
4be44fcd | 1201 | end: |
1da177e4 LT |
1202 | if (result) { |
1203 | acpi_thermal_remove_fs(device); | |
1204 | kfree(tz); | |
1205 | } | |
1206 | ||
d550d98d | 1207 | return result; |
1da177e4 LT |
1208 | } |
1209 | ||
4be44fcd | 1210 | static int acpi_thermal_remove(struct acpi_device *device, int type) |
1da177e4 | 1211 | { |
4be44fcd LB |
1212 | acpi_status status = AE_OK; |
1213 | struct acpi_thermal *tz = NULL; | |
1da177e4 | 1214 | |
1da177e4 LT |
1215 | |
1216 | if (!device || !acpi_driver_data(device)) | |
d550d98d | 1217 | return -EINVAL; |
1da177e4 | 1218 | |
50dd0969 | 1219 | tz = acpi_driver_data(device); |
1da177e4 LT |
1220 | |
1221 | /* avoid timer adding new defer task */ | |
1222 | tz->zombie = 1; | |
1223 | /* wait for running timer (on other CPUs) finish */ | |
1224 | del_timer_sync(&(tz->timer)); | |
1225 | /* synchronize deferred task */ | |
1226 | acpi_os_wait_events_complete(NULL); | |
1227 | /* deferred task may reinsert timer */ | |
1228 | del_timer_sync(&(tz->timer)); | |
1229 | ||
38ba7c9e | 1230 | status = acpi_remove_notify_handler(device->handle, |
4be44fcd LB |
1231 | ACPI_DEVICE_NOTIFY, |
1232 | acpi_thermal_notify); | |
1da177e4 LT |
1233 | |
1234 | /* Terminate policy */ | |
4be44fcd | 1235 | if (tz->trips.passive.flags.valid && tz->trips.passive.flags.enabled) { |
1da177e4 LT |
1236 | tz->trips.passive.flags.enabled = 0; |
1237 | acpi_thermal_passive(tz); | |
1238 | } | |
1239 | if (tz->trips.active[0].flags.valid | |
4be44fcd | 1240 | && tz->trips.active[0].flags.enabled) { |
1da177e4 LT |
1241 | tz->trips.active[0].flags.enabled = 0; |
1242 | acpi_thermal_active(tz); | |
1243 | } | |
1244 | ||
1245 | acpi_thermal_remove_fs(device); | |
1246 | ||
1247 | kfree(tz); | |
d550d98d | 1248 | return 0; |
1da177e4 LT |
1249 | } |
1250 | ||
5d9464a4 | 1251 | static int acpi_thermal_resume(struct acpi_device *device) |
74ce1468 KK |
1252 | { |
1253 | struct acpi_thermal *tz = NULL; | |
b1028c54 KK |
1254 | int i, j, power_state, result; |
1255 | ||
74ce1468 KK |
1256 | |
1257 | if (!device || !acpi_driver_data(device)) | |
d550d98d | 1258 | return -EINVAL; |
74ce1468 | 1259 | |
50dd0969 | 1260 | tz = acpi_driver_data(device); |
74ce1468 | 1261 | |
bed936f7 | 1262 | for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) { |
b1028c54 KK |
1263 | if (!(&tz->trips.active[i])) |
1264 | break; | |
1265 | if (!tz->trips.active[i].flags.valid) | |
1266 | break; | |
1267 | tz->trips.active[i].flags.enabled = 1; | |
1268 | for (j = 0; j < tz->trips.active[i].devices.count; j++) { | |
1269 | result = acpi_bus_get_power(tz->trips.active[i].devices. | |
1270 | handles[j], &power_state); | |
1271 | if (result || (power_state != ACPI_STATE_D0)) { | |
1272 | tz->trips.active[i].flags.enabled = 0; | |
1273 | break; | |
1274 | } | |
bed936f7 | 1275 | } |
b1028c54 | 1276 | tz->state.active |= tz->trips.active[i].flags.enabled; |
bed936f7 KK |
1277 | } |
1278 | ||
b1028c54 | 1279 | acpi_thermal_check(tz); |
74ce1468 KK |
1280 | |
1281 | return AE_OK; | |
1282 | } | |
1283 | ||
4be44fcd | 1284 | static int __init acpi_thermal_init(void) |
1da177e4 | 1285 | { |
4be44fcd | 1286 | int result = 0; |
1da177e4 | 1287 | |
1da177e4 LT |
1288 | |
1289 | acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir); | |
1290 | if (!acpi_thermal_dir) | |
d550d98d | 1291 | return -ENODEV; |
1da177e4 LT |
1292 | acpi_thermal_dir->owner = THIS_MODULE; |
1293 | ||
1294 | result = acpi_bus_register_driver(&acpi_thermal_driver); | |
1295 | if (result < 0) { | |
1296 | remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir); | |
d550d98d | 1297 | return -ENODEV; |
1da177e4 LT |
1298 | } |
1299 | ||
d550d98d | 1300 | return 0; |
1da177e4 LT |
1301 | } |
1302 | ||
4be44fcd | 1303 | static void __exit acpi_thermal_exit(void) |
1da177e4 | 1304 | { |
1da177e4 LT |
1305 | |
1306 | acpi_bus_unregister_driver(&acpi_thermal_driver); | |
1307 | ||
1308 | remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir); | |
1309 | ||
d550d98d | 1310 | return; |
1da177e4 LT |
1311 | } |
1312 | ||
1da177e4 LT |
1313 | module_init(acpi_thermal_init); |
1314 | module_exit(acpi_thermal_exit); |