]>
Commit | Line | Data |
---|---|---|
c942fddf | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* |
3 | * acpi_thermal.c - ACPI Thermal Zone Driver ($Revision: 41 $) | |
4 | * | |
5 | * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> | |
6 | * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> | |
7 | * | |
1da177e4 LT |
8 | * This driver fully implements the ACPI thermal policy as described in the |
9 | * ACPI 2.0 Specification. | |
10 | * | |
11 | * TBD: 1. Implement passive cooling hysteresis. | |
12 | * 2. Enhance passive cooling (CPU) states/limit interface to support | |
13 | * concepts of 'multiple limiters', upper/lower limits, etc. | |
1da177e4 LT |
14 | */ |
15 | ||
16 | #include <linux/kernel.h> | |
17 | #include <linux/module.h> | |
0b5bfa1c | 18 | #include <linux/dmi.h> |
1da177e4 | 19 | #include <linux/init.h> |
5a0e3ad6 | 20 | #include <linux/slab.h> |
1da177e4 | 21 | #include <linux/types.h> |
cd354f1a | 22 | #include <linux/jiffies.h> |
1da177e4 | 23 | #include <linux/kmod.h> |
10a0a8d4 | 24 | #include <linux/reboot.h> |
f6f5c45e | 25 | #include <linux/device.h> |
3f655ef8 | 26 | #include <linux/thermal.h> |
8b48463f | 27 | #include <linux/acpi.h> |
a59ffb20 | 28 | #include <linux/workqueue.h> |
7c0f6ba6 | 29 | #include <linux/uaccess.h> |
7f49a5cb | 30 | #include <linux/units.h> |
1da177e4 | 31 | |
a192a958 LB |
32 | #define PREFIX "ACPI: " |
33 | ||
1da177e4 | 34 | #define ACPI_THERMAL_CLASS "thermal_zone" |
1da177e4 | 35 | #define ACPI_THERMAL_DEVICE_NAME "Thermal Zone" |
1da177e4 LT |
36 | #define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80 |
37 | #define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81 | |
38 | #define ACPI_THERMAL_NOTIFY_DEVICES 0x82 | |
39 | #define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0 | |
40 | #define ACPI_THERMAL_NOTIFY_HOT 0xF1 | |
41 | #define ACPI_THERMAL_MODE_ACTIVE 0x00 | |
1da177e4 LT |
42 | |
43 | #define ACPI_THERMAL_MAX_ACTIVE 10 | |
44 | #define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65 | |
45 | ||
1da177e4 | 46 | #define _COMPONENT ACPI_THERMAL_COMPONENT |
f52fd66d | 47 | ACPI_MODULE_NAME("thermal"); |
1da177e4 | 48 | |
1cbf4c56 | 49 | MODULE_AUTHOR("Paul Diefenbaugh"); |
7cda93e0 | 50 | MODULE_DESCRIPTION("ACPI Thermal Zone Driver"); |
1da177e4 LT |
51 | MODULE_LICENSE("GPL"); |
52 | ||
f8707ec9 LB |
53 | static int act; |
54 | module_param(act, int, 0644); | |
3c1d36da | 55 | MODULE_PARM_DESC(act, "Disable or override all lowest active trip points."); |
f8707ec9 | 56 | |
c52a7419 LB |
57 | static int crt; |
58 | module_param(crt, int, 0644); | |
59 | MODULE_PARM_DESC(crt, "Disable or lower all critical trip points."); | |
60 | ||
1da177e4 | 61 | static int tzp; |
730ff34d | 62 | module_param(tzp, int, 0444); |
3c1d36da | 63 | MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds."); |
1da177e4 | 64 | |
f5487145 LB |
65 | static int nocrt; |
66 | module_param(nocrt, int, 0); | |
8c99fdce | 67 | MODULE_PARM_DESC(nocrt, "Set to take no action upon ACPI thermal zone critical trips points."); |
f5487145 | 68 | |
72b33ef8 LB |
69 | static int off; |
70 | module_param(off, int, 0); | |
3c1d36da | 71 | MODULE_PARM_DESC(off, "Set to disable ACPI thermal support."); |
72b33ef8 | 72 | |
a70cdc52 LB |
73 | static int psv; |
74 | module_param(psv, int, 0644); | |
3c1d36da | 75 | MODULE_PARM_DESC(psv, "Disable or override all passive trip points."); |
a70cdc52 | 76 | |
a59ffb20 AL |
77 | static struct workqueue_struct *acpi_thermal_pm_queue; |
78 | ||
4be44fcd | 79 | static int acpi_thermal_add(struct acpi_device *device); |
51fac838 | 80 | static int acpi_thermal_remove(struct acpi_device *device); |
342d550d | 81 | static void acpi_thermal_notify(struct acpi_device *device, u32 event); |
1da177e4 | 82 | |
1ba90e3a TR |
83 | static const struct acpi_device_id thermal_device_ids[] = { |
84 | {ACPI_THERMAL_HID, 0}, | |
85 | {"", 0}, | |
86 | }; | |
87 | MODULE_DEVICE_TABLE(acpi, thermal_device_ids); | |
88 | ||
90692404 | 89 | #ifdef CONFIG_PM_SLEEP |
a59ffb20 | 90 | static int acpi_thermal_suspend(struct device *dev); |
167cffb6 | 91 | static int acpi_thermal_resume(struct device *dev); |
fae9e2a4 | 92 | #else |
a59ffb20 | 93 | #define acpi_thermal_suspend NULL |
fae9e2a4 | 94 | #define acpi_thermal_resume NULL |
90692404 | 95 | #endif |
a59ffb20 | 96 | static SIMPLE_DEV_PM_OPS(acpi_thermal_pm, acpi_thermal_suspend, acpi_thermal_resume); |
167cffb6 | 97 | |
1da177e4 | 98 | static struct acpi_driver acpi_thermal_driver = { |
c2b6705b | 99 | .name = "thermal", |
4be44fcd | 100 | .class = ACPI_THERMAL_CLASS, |
1ba90e3a | 101 | .ids = thermal_device_ids, |
4be44fcd LB |
102 | .ops = { |
103 | .add = acpi_thermal_add, | |
104 | .remove = acpi_thermal_remove, | |
342d550d | 105 | .notify = acpi_thermal_notify, |
4be44fcd | 106 | }, |
167cffb6 | 107 | .drv.pm = &acpi_thermal_pm, |
1da177e4 LT |
108 | }; |
109 | ||
110 | struct acpi_thermal_state { | |
4be44fcd LB |
111 | u8 critical:1; |
112 | u8 hot:1; | |
113 | u8 passive:1; | |
114 | u8 active:1; | |
115 | u8 reserved:4; | |
116 | int active_index; | |
1da177e4 LT |
117 | }; |
118 | ||
119 | struct acpi_thermal_state_flags { | |
4be44fcd LB |
120 | u8 valid:1; |
121 | u8 enabled:1; | |
122 | u8 reserved:6; | |
1da177e4 LT |
123 | }; |
124 | ||
125 | struct acpi_thermal_critical { | |
126 | struct acpi_thermal_state_flags flags; | |
4be44fcd | 127 | unsigned long temperature; |
1da177e4 LT |
128 | }; |
129 | ||
130 | struct acpi_thermal_hot { | |
131 | struct acpi_thermal_state_flags flags; | |
4be44fcd | 132 | unsigned long temperature; |
1da177e4 LT |
133 | }; |
134 | ||
135 | struct acpi_thermal_passive { | |
136 | struct acpi_thermal_state_flags flags; | |
4be44fcd LB |
137 | unsigned long temperature; |
138 | unsigned long tc1; | |
139 | unsigned long tc2; | |
140 | unsigned long tsp; | |
141 | struct acpi_handle_list devices; | |
1da177e4 LT |
142 | }; |
143 | ||
144 | struct acpi_thermal_active { | |
145 | struct acpi_thermal_state_flags flags; | |
4be44fcd LB |
146 | unsigned long temperature; |
147 | struct acpi_handle_list devices; | |
1da177e4 LT |
148 | }; |
149 | ||
150 | struct acpi_thermal_trips { | |
151 | struct acpi_thermal_critical critical; | |
4be44fcd | 152 | struct acpi_thermal_hot hot; |
1da177e4 LT |
153 | struct acpi_thermal_passive passive; |
154 | struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE]; | |
155 | }; | |
156 | ||
157 | struct acpi_thermal_flags { | |
4be44fcd LB |
158 | u8 cooling_mode:1; /* _SCP */ |
159 | u8 devices:1; /* _TZD */ | |
160 | u8 reserved:6; | |
1da177e4 LT |
161 | }; |
162 | ||
163 | struct acpi_thermal { | |
8348e1b1 | 164 | struct acpi_device * device; |
4be44fcd LB |
165 | acpi_bus_id name; |
166 | unsigned long temperature; | |
167 | unsigned long last_temperature; | |
168 | unsigned long polling_frequency; | |
4be44fcd | 169 | volatile u8 zombie; |
1da177e4 LT |
170 | struct acpi_thermal_flags flags; |
171 | struct acpi_thermal_state state; | |
172 | struct acpi_thermal_trips trips; | |
4be44fcd | 173 | struct acpi_handle_list devices; |
3f655ef8 | 174 | struct thermal_zone_device *thermal_zone; |
7f49a5cb | 175 | int kelvin_offset; /* in millidegrees */ |
a59ffb20 | 176 | struct work_struct thermal_check_work; |
81b704d3 RW |
177 | struct mutex thermal_check_lock; |
178 | refcount_t thermal_check_count; | |
1da177e4 LT |
179 | }; |
180 | ||
1da177e4 LT |
181 | /* -------------------------------------------------------------------------- |
182 | Thermal Zone Management | |
183 | -------------------------------------------------------------------------- */ | |
184 | ||
4be44fcd | 185 | static int acpi_thermal_get_temperature(struct acpi_thermal *tz) |
1da177e4 | 186 | { |
4be44fcd | 187 | acpi_status status = AE_OK; |
27663c58 | 188 | unsigned long long tmp; |
1da177e4 LT |
189 | |
190 | if (!tz) | |
d550d98d | 191 | return -EINVAL; |
1da177e4 LT |
192 | |
193 | tz->last_temperature = tz->temperature; | |
194 | ||
27663c58 | 195 | status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp); |
1da177e4 | 196 | if (ACPI_FAILURE(status)) |
d550d98d | 197 | return -ENODEV; |
1da177e4 | 198 | |
27663c58 | 199 | tz->temperature = tmp; |
4be44fcd LB |
200 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n", |
201 | tz->temperature)); | |
1da177e4 | 202 | |
d550d98d | 203 | return 0; |
1da177e4 LT |
204 | } |
205 | ||
4be44fcd | 206 | static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz) |
1da177e4 | 207 | { |
4be44fcd | 208 | acpi_status status = AE_OK; |
27663c58 | 209 | unsigned long long tmp; |
1da177e4 LT |
210 | |
211 | if (!tz) | |
d550d98d | 212 | return -EINVAL; |
1da177e4 | 213 | |
27663c58 | 214 | status = acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, &tmp); |
1da177e4 | 215 | if (ACPI_FAILURE(status)) |
d550d98d | 216 | return -ENODEV; |
1da177e4 | 217 | |
27663c58 | 218 | tz->polling_frequency = tmp; |
4be44fcd LB |
219 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n", |
220 | tz->polling_frequency)); | |
1da177e4 | 221 | |
d550d98d | 222 | return 0; |
1da177e4 LT |
223 | } |
224 | ||
4be44fcd | 225 | static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode) |
1da177e4 | 226 | { |
1da177e4 | 227 | if (!tz) |
d550d98d | 228 | return -EINVAL; |
1da177e4 | 229 | |
e88c7409 KS |
230 | if (ACPI_FAILURE(acpi_execute_simple_method(tz->device->handle, |
231 | "_SCP", mode))) | |
d550d98d | 232 | return -ENODEV; |
1da177e4 | 233 | |
d550d98d | 234 | return 0; |
1da177e4 LT |
235 | } |
236 | ||
ce44e197 ZR |
237 | #define ACPI_TRIPS_CRITICAL 0x01 |
238 | #define ACPI_TRIPS_HOT 0x02 | |
239 | #define ACPI_TRIPS_PASSIVE 0x04 | |
240 | #define ACPI_TRIPS_ACTIVE 0x08 | |
241 | #define ACPI_TRIPS_DEVICES 0x10 | |
1da177e4 | 242 | |
ce44e197 ZR |
243 | #define ACPI_TRIPS_REFRESH_THRESHOLDS (ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE) |
244 | #define ACPI_TRIPS_REFRESH_DEVICES ACPI_TRIPS_DEVICES | |
1da177e4 | 245 | |
ce44e197 ZR |
246 | #define ACPI_TRIPS_INIT (ACPI_TRIPS_CRITICAL | ACPI_TRIPS_HOT | \ |
247 | ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE | \ | |
248 | ACPI_TRIPS_DEVICES) | |
1da177e4 | 249 | |
ce44e197 ZR |
250 | /* |
251 | * This exception is thrown out in two cases: | |
252 | * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid | |
253 | * when re-evaluating the AML code. | |
254 | * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change. | |
255 | * We need to re-bind the cooling devices of a thermal zone when this occurs. | |
256 | */ | |
257 | #define ACPI_THERMAL_TRIPS_EXCEPTION(flags, str) \ | |
258 | do { \ | |
259 | if (flags != ACPI_TRIPS_INIT) \ | |
260 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, \ | |
261 | "ACPI thermal trip point %s changed\n" \ | |
7e3cf246 | 262 | "Please send acpidump to linux-acpi@vger.kernel.org", str)); \ |
ce44e197 ZR |
263 | } while (0) |
264 | ||
265 | static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag) | |
266 | { | |
267 | acpi_status status = AE_OK; | |
27663c58 | 268 | unsigned long long tmp; |
ce44e197 ZR |
269 | struct acpi_handle_list devices; |
270 | int valid = 0; | |
271 | int i; | |
1da177e4 | 272 | |
fa809452 | 273 | /* Critical Shutdown */ |
ce44e197 ZR |
274 | if (flag & ACPI_TRIPS_CRITICAL) { |
275 | status = acpi_evaluate_integer(tz->device->handle, | |
27663c58 MW |
276 | "_CRT", NULL, &tmp); |
277 | tz->trips.critical.temperature = tmp; | |
a39a2d7c AV |
278 | /* |
279 | * Treat freezing temperatures as invalid as well; some | |
280 | * BIOSes return really low values and cause reboots at startup. | |
b731d7b6 | 281 | * Below zero (Celsius) values clearly aren't right for sure.. |
a39a2d7c AV |
282 | * ... so lets discard those as invalid. |
283 | */ | |
fa809452 TR |
284 | if (ACPI_FAILURE(status)) { |
285 | tz->trips.critical.flags.valid = 0; | |
286 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | |
287 | "No critical threshold\n")); | |
288 | } else if (tmp <= 2732) { | |
766a8a6d AS |
289 | pr_warn(FW_BUG "Invalid critical threshold (%llu)\n", |
290 | tmp); | |
c52a7419 | 291 | tz->trips.critical.flags.valid = 0; |
ce44e197 ZR |
292 | } else { |
293 | tz->trips.critical.flags.valid = 1; | |
294 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | |
fa809452 TR |
295 | "Found critical threshold [%lu]\n", |
296 | tz->trips.critical.temperature)); | |
ce44e197 ZR |
297 | } |
298 | if (tz->trips.critical.flags.valid == 1) { | |
299 | if (crt == -1) { | |
300 | tz->trips.critical.flags.valid = 0; | |
301 | } else if (crt > 0) { | |
7f49a5cb AM |
302 | unsigned long crt_k = celsius_to_deci_kelvin(crt); |
303 | ||
ce44e197 | 304 | /* |
22a94d79 | 305 | * Allow override critical threshold |
ce44e197 | 306 | */ |
22a94d79 | 307 | if (crt_k > tz->trips.critical.temperature) |
766a8a6d AS |
308 | pr_warn(PREFIX "Critical threshold %d C\n", |
309 | crt); | |
22a94d79 | 310 | tz->trips.critical.temperature = crt_k; |
ce44e197 | 311 | } |
c52a7419 LB |
312 | } |
313 | } | |
314 | ||
1da177e4 | 315 | /* Critical Sleep (optional) */ |
ce44e197 | 316 | if (flag & ACPI_TRIPS_HOT) { |
a70cdc52 | 317 | status = acpi_evaluate_integer(tz->device->handle, |
27663c58 | 318 | "_HOT", NULL, &tmp); |
ce44e197 ZR |
319 | if (ACPI_FAILURE(status)) { |
320 | tz->trips.hot.flags.valid = 0; | |
321 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | |
322 | "No hot threshold\n")); | |
323 | } else { | |
27663c58 | 324 | tz->trips.hot.temperature = tmp; |
ce44e197 ZR |
325 | tz->trips.hot.flags.valid = 1; |
326 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | |
327 | "Found hot threshold [%lu]\n", | |
ee17fdf2 | 328 | tz->trips.hot.temperature)); |
ce44e197 | 329 | } |
a70cdc52 LB |
330 | } |
331 | ||
ce44e197 | 332 | /* Passive (optional) */ |
0e4240d9 ZR |
333 | if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.flags.valid) || |
334 | (flag == ACPI_TRIPS_INIT)) { | |
ce44e197 ZR |
335 | valid = tz->trips.passive.flags.valid; |
336 | if (psv == -1) { | |
337 | status = AE_SUPPORT; | |
338 | } else if (psv > 0) { | |
7f49a5cb | 339 | tmp = celsius_to_deci_kelvin(psv); |
ce44e197 ZR |
340 | status = AE_OK; |
341 | } else { | |
342 | status = acpi_evaluate_integer(tz->device->handle, | |
27663c58 | 343 | "_PSV", NULL, &tmp); |
ce44e197 | 344 | } |
1da177e4 | 345 | |
1da177e4 LT |
346 | if (ACPI_FAILURE(status)) |
347 | tz->trips.passive.flags.valid = 0; | |
ce44e197 | 348 | else { |
27663c58 | 349 | tz->trips.passive.temperature = tmp; |
ce44e197 ZR |
350 | tz->trips.passive.flags.valid = 1; |
351 | if (flag == ACPI_TRIPS_INIT) { | |
352 | status = acpi_evaluate_integer( | |
353 | tz->device->handle, "_TC1", | |
27663c58 | 354 | NULL, &tmp); |
ce44e197 ZR |
355 | if (ACPI_FAILURE(status)) |
356 | tz->trips.passive.flags.valid = 0; | |
27663c58 MW |
357 | else |
358 | tz->trips.passive.tc1 = tmp; | |
ce44e197 ZR |
359 | status = acpi_evaluate_integer( |
360 | tz->device->handle, "_TC2", | |
27663c58 | 361 | NULL, &tmp); |
ce44e197 ZR |
362 | if (ACPI_FAILURE(status)) |
363 | tz->trips.passive.flags.valid = 0; | |
27663c58 MW |
364 | else |
365 | tz->trips.passive.tc2 = tmp; | |
ce44e197 ZR |
366 | status = acpi_evaluate_integer( |
367 | tz->device->handle, "_TSP", | |
27663c58 | 368 | NULL, &tmp); |
ce44e197 ZR |
369 | if (ACPI_FAILURE(status)) |
370 | tz->trips.passive.flags.valid = 0; | |
27663c58 MW |
371 | else |
372 | tz->trips.passive.tsp = tmp; | |
ce44e197 ZR |
373 | } |
374 | } | |
375 | } | |
376 | if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.flags.valid) { | |
377 | memset(&devices, 0, sizeof(struct acpi_handle_list)); | |
378 | status = acpi_evaluate_reference(tz->device->handle, "_PSL", | |
379 | NULL, &devices); | |
0e4240d9 | 380 | if (ACPI_FAILURE(status)) { |
766a8a6d | 381 | pr_warn(PREFIX "Invalid passive threshold\n"); |
1da177e4 | 382 | tz->trips.passive.flags.valid = 0; |
0e4240d9 | 383 | } |
1da177e4 | 384 | else |
ce44e197 | 385 | tz->trips.passive.flags.valid = 1; |
1da177e4 | 386 | |
ce44e197 ZR |
387 | if (memcmp(&tz->trips.passive.devices, &devices, |
388 | sizeof(struct acpi_handle_list))) { | |
389 | memcpy(&tz->trips.passive.devices, &devices, | |
390 | sizeof(struct acpi_handle_list)); | |
391 | ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device"); | |
392 | } | |
393 | } | |
394 | if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) { | |
395 | if (valid != tz->trips.passive.flags.valid) | |
396 | ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state"); | |
397 | } | |
1da177e4 | 398 | |
ce44e197 | 399 | /* Active (optional) */ |
4be44fcd | 400 | for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) { |
4be44fcd | 401 | char name[5] = { '_', 'A', 'C', ('0' + i), '\0' }; |
ce44e197 | 402 | valid = tz->trips.active[i].flags.valid; |
1da177e4 | 403 | |
f8707ec9 | 404 | if (act == -1) |
ce44e197 ZR |
405 | break; /* disable all active trip points */ |
406 | ||
0e4240d9 ZR |
407 | if ((flag == ACPI_TRIPS_INIT) || ((flag & ACPI_TRIPS_ACTIVE) && |
408 | tz->trips.active[i].flags.valid)) { | |
ce44e197 | 409 | status = acpi_evaluate_integer(tz->device->handle, |
27663c58 | 410 | name, NULL, &tmp); |
ce44e197 ZR |
411 | if (ACPI_FAILURE(status)) { |
412 | tz->trips.active[i].flags.valid = 0; | |
413 | if (i == 0) | |
414 | break; | |
415 | if (act <= 0) | |
416 | break; | |
417 | if (i == 1) | |
418 | tz->trips.active[0].temperature = | |
7f49a5cb | 419 | celsius_to_deci_kelvin(act); |
ce44e197 ZR |
420 | else |
421 | /* | |
422 | * Don't allow override higher than | |
423 | * the next higher trip point | |
424 | */ | |
425 | tz->trips.active[i - 1].temperature = | |
426 | (tz->trips.active[i - 2].temperature < | |
7f49a5cb | 427 | celsius_to_deci_kelvin(act) ? |
ce44e197 | 428 | tz->trips.active[i - 2].temperature : |
7f49a5cb | 429 | celsius_to_deci_kelvin(act)); |
f8707ec9 | 430 | break; |
27663c58 MW |
431 | } else { |
432 | tz->trips.active[i].temperature = tmp; | |
ce44e197 | 433 | tz->trips.active[i].flags.valid = 1; |
27663c58 | 434 | } |
f8707ec9 | 435 | } |
1da177e4 LT |
436 | |
437 | name[2] = 'L'; | |
ce44e197 ZR |
438 | if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].flags.valid ) { |
439 | memset(&devices, 0, sizeof(struct acpi_handle_list)); | |
440 | status = acpi_evaluate_reference(tz->device->handle, | |
441 | name, NULL, &devices); | |
0e4240d9 | 442 | if (ACPI_FAILURE(status)) { |
766a8a6d AS |
443 | pr_warn(PREFIX "Invalid active%d threshold\n", |
444 | i); | |
ce44e197 | 445 | tz->trips.active[i].flags.valid = 0; |
0e4240d9 | 446 | } |
ce44e197 ZR |
447 | else |
448 | tz->trips.active[i].flags.valid = 1; | |
449 | ||
450 | if (memcmp(&tz->trips.active[i].devices, &devices, | |
451 | sizeof(struct acpi_handle_list))) { | |
452 | memcpy(&tz->trips.active[i].devices, &devices, | |
453 | sizeof(struct acpi_handle_list)); | |
454 | ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device"); | |
455 | } | |
456 | } | |
457 | if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES)) | |
458 | if (valid != tz->trips.active[i].flags.valid) | |
459 | ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state"); | |
460 | ||
461 | if (!tz->trips.active[i].flags.valid) | |
462 | break; | |
463 | } | |
464 | ||
e88c7409 | 465 | if (flag & ACPI_TRIPS_DEVICES) { |
668e0200 | 466 | memset(&devices, 0, sizeof(devices)); |
ce44e197 ZR |
467 | status = acpi_evaluate_reference(tz->device->handle, "_TZD", |
468 | NULL, &devices); | |
668e0200 LT |
469 | if (ACPI_SUCCESS(status) |
470 | && memcmp(&tz->devices, &devices, sizeof(devices))) { | |
471 | tz->devices = devices; | |
ce44e197 ZR |
472 | ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device"); |
473 | } | |
1da177e4 LT |
474 | } |
475 | ||
d550d98d | 476 | return 0; |
1da177e4 LT |
477 | } |
478 | ||
ce44e197 | 479 | static int acpi_thermal_get_trip_points(struct acpi_thermal *tz) |
1da177e4 | 480 | { |
8b7ef6d8 TR |
481 | int i, valid, ret = acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT); |
482 | ||
483 | if (ret) | |
484 | return ret; | |
485 | ||
486 | valid = tz->trips.critical.flags.valid | | |
487 | tz->trips.hot.flags.valid | | |
488 | tz->trips.passive.flags.valid; | |
489 | ||
490 | for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) | |
491 | valid |= tz->trips.active[i].flags.valid; | |
492 | ||
493 | if (!valid) { | |
766a8a6d | 494 | pr_warn(FW_BUG "No valid trip found\n"); |
8b7ef6d8 TR |
495 | return -ENODEV; |
496 | } | |
497 | return 0; | |
1da177e4 LT |
498 | } |
499 | ||
3f655ef8 | 500 | /* sys I/F for generic thermal sysfs support */ |
5e012760 | 501 | |
17e8351a | 502 | static int thermal_get_temp(struct thermal_zone_device *thermal, int *temp) |
3f655ef8 ZR |
503 | { |
504 | struct acpi_thermal *tz = thermal->devdata; | |
76ecb4f2 | 505 | int result; |
3f655ef8 ZR |
506 | |
507 | if (!tz) | |
508 | return -EINVAL; | |
509 | ||
76ecb4f2 ZR |
510 | result = acpi_thermal_get_temperature(tz); |
511 | if (result) | |
512 | return result; | |
513 | ||
7f49a5cb | 514 | *temp = deci_kelvin_to_millicelsius_with_offset(tz->temperature, |
7b83fd9d | 515 | tz->kelvin_offset); |
6503e5df | 516 | return 0; |
3f655ef8 ZR |
517 | } |
518 | ||
3f655ef8 | 519 | static int thermal_get_trip_type(struct thermal_zone_device *thermal, |
6503e5df | 520 | int trip, enum thermal_trip_type *type) |
3f655ef8 ZR |
521 | { |
522 | struct acpi_thermal *tz = thermal->devdata; | |
523 | int i; | |
524 | ||
525 | if (!tz || trip < 0) | |
526 | return -EINVAL; | |
527 | ||
528 | if (tz->trips.critical.flags.valid) { | |
6503e5df MG |
529 | if (!trip) { |
530 | *type = THERMAL_TRIP_CRITICAL; | |
531 | return 0; | |
532 | } | |
3f655ef8 ZR |
533 | trip--; |
534 | } | |
535 | ||
536 | if (tz->trips.hot.flags.valid) { | |
6503e5df MG |
537 | if (!trip) { |
538 | *type = THERMAL_TRIP_HOT; | |
539 | return 0; | |
540 | } | |
3f655ef8 ZR |
541 | trip--; |
542 | } | |
543 | ||
544 | if (tz->trips.passive.flags.valid) { | |
6503e5df MG |
545 | if (!trip) { |
546 | *type = THERMAL_TRIP_PASSIVE; | |
547 | return 0; | |
548 | } | |
3f655ef8 ZR |
549 | trip--; |
550 | } | |
551 | ||
552 | for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE && | |
553 | tz->trips.active[i].flags.valid; i++) { | |
6503e5df MG |
554 | if (!trip) { |
555 | *type = THERMAL_TRIP_ACTIVE; | |
556 | return 0; | |
557 | } | |
3f655ef8 ZR |
558 | trip--; |
559 | } | |
560 | ||
561 | return -EINVAL; | |
562 | } | |
563 | ||
564 | static int thermal_get_trip_temp(struct thermal_zone_device *thermal, | |
17e8351a | 565 | int trip, int *temp) |
3f655ef8 ZR |
566 | { |
567 | struct acpi_thermal *tz = thermal->devdata; | |
568 | int i; | |
569 | ||
570 | if (!tz || trip < 0) | |
571 | return -EINVAL; | |
572 | ||
573 | if (tz->trips.critical.flags.valid) { | |
6503e5df | 574 | if (!trip) { |
7f49a5cb | 575 | *temp = deci_kelvin_to_millicelsius_with_offset( |
13614e37 JD |
576 | tz->trips.critical.temperature, |
577 | tz->kelvin_offset); | |
6503e5df MG |
578 | return 0; |
579 | } | |
3f655ef8 ZR |
580 | trip--; |
581 | } | |
582 | ||
583 | if (tz->trips.hot.flags.valid) { | |
6503e5df | 584 | if (!trip) { |
7f49a5cb | 585 | *temp = deci_kelvin_to_millicelsius_with_offset( |
13614e37 JD |
586 | tz->trips.hot.temperature, |
587 | tz->kelvin_offset); | |
6503e5df MG |
588 | return 0; |
589 | } | |
3f655ef8 ZR |
590 | trip--; |
591 | } | |
592 | ||
593 | if (tz->trips.passive.flags.valid) { | |
6503e5df | 594 | if (!trip) { |
7f49a5cb | 595 | *temp = deci_kelvin_to_millicelsius_with_offset( |
13614e37 JD |
596 | tz->trips.passive.temperature, |
597 | tz->kelvin_offset); | |
6503e5df MG |
598 | return 0; |
599 | } | |
3f655ef8 ZR |
600 | trip--; |
601 | } | |
602 | ||
603 | for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE && | |
604 | tz->trips.active[i].flags.valid; i++) { | |
6503e5df | 605 | if (!trip) { |
7f49a5cb | 606 | *temp = deci_kelvin_to_millicelsius_with_offset( |
13614e37 JD |
607 | tz->trips.active[i].temperature, |
608 | tz->kelvin_offset); | |
6503e5df MG |
609 | return 0; |
610 | } | |
3f655ef8 ZR |
611 | trip--; |
612 | } | |
613 | ||
614 | return -EINVAL; | |
615 | } | |
616 | ||
9ec732ff | 617 | static int thermal_get_crit_temp(struct thermal_zone_device *thermal, |
17e8351a SH |
618 | int *temperature) |
619 | { | |
9ec732ff ZR |
620 | struct acpi_thermal *tz = thermal->devdata; |
621 | ||
622 | if (tz->trips.critical.flags.valid) { | |
7f49a5cb | 623 | *temperature = deci_kelvin_to_millicelsius_with_offset( |
13614e37 JD |
624 | tz->trips.critical.temperature, |
625 | tz->kelvin_offset); | |
9ec732ff ZR |
626 | return 0; |
627 | } else | |
628 | return -EINVAL; | |
629 | } | |
630 | ||
601f3d42 ZR |
631 | static int thermal_get_trend(struct thermal_zone_device *thermal, |
632 | int trip, enum thermal_trend *trend) | |
633 | { | |
634 | struct acpi_thermal *tz = thermal->devdata; | |
635 | enum thermal_trip_type type; | |
636 | int i; | |
637 | ||
638 | if (thermal_get_trip_type(thermal, trip, &type)) | |
639 | return -EINVAL; | |
640 | ||
4ae46bef | 641 | if (type == THERMAL_TRIP_ACTIVE) { |
17e8351a | 642 | int trip_temp; |
7f49a5cb | 643 | int temp = deci_kelvin_to_millicelsius_with_offset( |
7b83fd9d | 644 | tz->temperature, tz->kelvin_offset); |
94a40931 ZR |
645 | if (thermal_get_trip_temp(thermal, trip, &trip_temp)) |
646 | return -EINVAL; | |
647 | ||
648 | if (temp > trip_temp) { | |
649 | *trend = THERMAL_TREND_RAISING; | |
650 | return 0; | |
651 | } else { | |
652 | /* Fall back on default trend */ | |
653 | return -EINVAL; | |
654 | } | |
4ae46bef | 655 | } |
601f3d42 ZR |
656 | |
657 | /* | |
658 | * tz->temperature has already been updated by generic thermal layer, | |
659 | * before this callback being invoked | |
660 | */ | |
661 | i = (tz->trips.passive.tc1 * (tz->temperature - tz->last_temperature)) | |
662 | + (tz->trips.passive.tc2 | |
663 | * (tz->temperature - tz->trips.passive.temperature)); | |
664 | ||
665 | if (i > 0) | |
666 | *trend = THERMAL_TREND_RAISING; | |
667 | else if (i < 0) | |
668 | *trend = THERMAL_TREND_DROPPING; | |
669 | else | |
670 | *trend = THERMAL_TREND_STABLE; | |
671 | return 0; | |
672 | } | |
673 | ||
d0c2a876 | 674 | static void acpi_thermal_zone_device_hot(struct thermal_zone_device *thermal) |
b1569e99 | 675 | { |
b1569e99 MG |
676 | struct acpi_thermal *tz = thermal->devdata; |
677 | ||
b1569e99 | 678 | acpi_bus_generate_netlink_event(tz->device->pnp.device_class, |
d0c2a876 DL |
679 | dev_name(&tz->device->dev), |
680 | ACPI_THERMAL_NOTIFY_HOT, 1); | |
681 | } | |
b1569e99 | 682 | |
d0c2a876 DL |
683 | static void acpi_thermal_zone_device_critical(struct thermal_zone_device *thermal) |
684 | { | |
685 | struct acpi_thermal *tz = thermal->devdata; | |
b1569e99 | 686 | |
d0c2a876 DL |
687 | acpi_bus_generate_netlink_event(tz->device->pnp.device_class, |
688 | dev_name(&tz->device->dev), | |
689 | ACPI_THERMAL_NOTIFY_CRITICAL, 1); | |
690 | ||
691 | thermal_zone_device_critical(thermal); | |
b1569e99 MG |
692 | } |
693 | ||
3f655ef8 ZR |
694 | static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal, |
695 | struct thermal_cooling_device *cdev, | |
9d99842f | 696 | bool bind) |
3f655ef8 ZR |
697 | { |
698 | struct acpi_device *device = cdev->devdata; | |
699 | struct acpi_thermal *tz = thermal->devdata; | |
653a00c9 ZR |
700 | struct acpi_device *dev; |
701 | acpi_status status; | |
702 | acpi_handle handle; | |
3f655ef8 ZR |
703 | int i; |
704 | int j; | |
705 | int trip = -1; | |
706 | int result = 0; | |
707 | ||
708 | if (tz->trips.critical.flags.valid) | |
709 | trip++; | |
710 | ||
711 | if (tz->trips.hot.flags.valid) | |
712 | trip++; | |
713 | ||
714 | if (tz->trips.passive.flags.valid) { | |
715 | trip++; | |
716 | for (i = 0; i < tz->trips.passive.devices.count; | |
717 | i++) { | |
653a00c9 ZR |
718 | handle = tz->trips.passive.devices.handles[i]; |
719 | status = acpi_bus_get_device(handle, &dev); | |
9d99842f ZR |
720 | if (ACPI_FAILURE(status) || dev != device) |
721 | continue; | |
722 | if (bind) | |
723 | result = | |
724 | thermal_zone_bind_cooling_device | |
725 | (thermal, trip, cdev, | |
6cd9e9f6 KS |
726 | THERMAL_NO_LIMIT, THERMAL_NO_LIMIT, |
727 | THERMAL_WEIGHT_DEFAULT); | |
9d99842f ZR |
728 | else |
729 | result = | |
730 | thermal_zone_unbind_cooling_device | |
731 | (thermal, trip, cdev); | |
732 | if (result) | |
733 | goto failed; | |
3f655ef8 ZR |
734 | } |
735 | } | |
736 | ||
737 | for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) { | |
738 | if (!tz->trips.active[i].flags.valid) | |
739 | break; | |
740 | trip++; | |
741 | for (j = 0; | |
742 | j < tz->trips.active[i].devices.count; | |
743 | j++) { | |
653a00c9 ZR |
744 | handle = tz->trips.active[i].devices.handles[j]; |
745 | status = acpi_bus_get_device(handle, &dev); | |
9d99842f ZR |
746 | if (ACPI_FAILURE(status) || dev != device) |
747 | continue; | |
748 | if (bind) | |
749 | result = thermal_zone_bind_cooling_device | |
750 | (thermal, trip, cdev, | |
6cd9e9f6 KS |
751 | THERMAL_NO_LIMIT, THERMAL_NO_LIMIT, |
752 | THERMAL_WEIGHT_DEFAULT); | |
9d99842f ZR |
753 | else |
754 | result = thermal_zone_unbind_cooling_device | |
755 | (thermal, trip, cdev); | |
756 | if (result) | |
757 | goto failed; | |
3f655ef8 ZR |
758 | } |
759 | } | |
760 | ||
761 | for (i = 0; i < tz->devices.count; i++) { | |
653a00c9 ZR |
762 | handle = tz->devices.handles[i]; |
763 | status = acpi_bus_get_device(handle, &dev); | |
764 | if (ACPI_SUCCESS(status) && (dev == device)) { | |
9d99842f ZR |
765 | if (bind) |
766 | result = thermal_zone_bind_cooling_device | |
70f2903b LT |
767 | (thermal, THERMAL_TRIPS_NONE, |
768 | cdev, THERMAL_NO_LIMIT, | |
6cd9e9f6 KS |
769 | THERMAL_NO_LIMIT, |
770 | THERMAL_WEIGHT_DEFAULT); | |
9d99842f ZR |
771 | else |
772 | result = thermal_zone_unbind_cooling_device | |
70f2903b LT |
773 | (thermal, THERMAL_TRIPS_NONE, |
774 | cdev); | |
653a00c9 ZR |
775 | if (result) |
776 | goto failed; | |
777 | } | |
3f655ef8 ZR |
778 | } |
779 | ||
780 | failed: | |
781 | return result; | |
782 | } | |
783 | ||
784 | static int | |
785 | acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal, | |
786 | struct thermal_cooling_device *cdev) | |
787 | { | |
9d99842f | 788 | return acpi_thermal_cooling_device_cb(thermal, cdev, true); |
3f655ef8 ZR |
789 | } |
790 | ||
791 | static int | |
792 | acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal, | |
793 | struct thermal_cooling_device *cdev) | |
794 | { | |
9d99842f | 795 | return acpi_thermal_cooling_device_cb(thermal, cdev, false); |
3f655ef8 ZR |
796 | } |
797 | ||
ec9c9c2e | 798 | static struct thermal_zone_device_ops acpi_thermal_zone_ops = { |
3f655ef8 ZR |
799 | .bind = acpi_thermal_bind_cooling_device, |
800 | .unbind = acpi_thermal_unbind_cooling_device, | |
801 | .get_temp = thermal_get_temp, | |
3f655ef8 ZR |
802 | .get_trip_type = thermal_get_trip_type, |
803 | .get_trip_temp = thermal_get_trip_temp, | |
9ec732ff | 804 | .get_crit_temp = thermal_get_crit_temp, |
601f3d42 | 805 | .get_trend = thermal_get_trend, |
d0c2a876 DL |
806 | .hot = acpi_thermal_zone_device_hot, |
807 | .critical = acpi_thermal_zone_device_critical, | |
3f655ef8 ZR |
808 | }; |
809 | ||
810 | static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz) | |
811 | { | |
812 | int trips = 0; | |
813 | int result; | |
20733939 | 814 | acpi_status status; |
3f655ef8 ZR |
815 | int i; |
816 | ||
817 | if (tz->trips.critical.flags.valid) | |
818 | trips++; | |
819 | ||
820 | if (tz->trips.hot.flags.valid) | |
821 | trips++; | |
822 | ||
823 | if (tz->trips.passive.flags.valid) | |
824 | trips++; | |
825 | ||
826 | for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE && | |
827 | tz->trips.active[i].flags.valid; i++, trips++); | |
b1569e99 MG |
828 | |
829 | if (tz->trips.passive.flags.valid) | |
830 | tz->thermal_zone = | |
c56f5c03 | 831 | thermal_zone_device_register("acpitz", trips, 0, tz, |
50125a9b | 832 | &acpi_thermal_zone_ops, NULL, |
b1569e99 MG |
833 | tz->trips.passive.tsp*100, |
834 | tz->polling_frequency*100); | |
835 | else | |
836 | tz->thermal_zone = | |
c56f5c03 | 837 | thermal_zone_device_register("acpitz", trips, 0, tz, |
50125a9b D |
838 | &acpi_thermal_zone_ops, NULL, |
839 | 0, tz->polling_frequency*100); | |
bb070e43 | 840 | if (IS_ERR(tz->thermal_zone)) |
3f655ef8 ZR |
841 | return -ENODEV; |
842 | ||
843 | result = sysfs_create_link(&tz->device->dev.kobj, | |
844 | &tz->thermal_zone->device.kobj, "thermal_zone"); | |
845 | if (result) | |
172066cc | 846 | goto unregister_tzd; |
3f655ef8 ZR |
847 | |
848 | result = sysfs_create_link(&tz->thermal_zone->device.kobj, | |
849 | &tz->device->dev.kobj, "device"); | |
850 | if (result) | |
172066cc | 851 | goto remove_tz_link; |
3f655ef8 | 852 | |
e6f8a4d6 LT |
853 | status = acpi_bus_attach_private_data(tz->device->handle, |
854 | tz->thermal_zone); | |
172066cc AP |
855 | if (ACPI_FAILURE(status)) { |
856 | result = -ENODEV; | |
857 | goto remove_dev_link; | |
858 | } | |
20733939 | 859 | |
7f4957be AP |
860 | result = thermal_zone_device_enable(tz->thermal_zone); |
861 | if (result) | |
862 | goto acpi_bus_detach; | |
3f655ef8 | 863 | |
fc3a8828 GKH |
864 | dev_info(&tz->device->dev, "registered as thermal_zone%d\n", |
865 | tz->thermal_zone->id); | |
172066cc | 866 | |
3f655ef8 | 867 | return 0; |
172066cc | 868 | |
7f4957be AP |
869 | acpi_bus_detach: |
870 | acpi_bus_detach_private_data(tz->device->handle); | |
172066cc AP |
871 | remove_dev_link: |
872 | sysfs_remove_link(&tz->thermal_zone->device.kobj, "device"); | |
873 | remove_tz_link: | |
874 | sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone"); | |
875 | unregister_tzd: | |
876 | thermal_zone_device_unregister(tz->thermal_zone); | |
877 | ||
878 | return result; | |
3f655ef8 ZR |
879 | } |
880 | ||
881 | static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz) | |
882 | { | |
883 | sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone"); | |
884 | sysfs_remove_link(&tz->thermal_zone->device.kobj, "device"); | |
885 | thermal_zone_device_unregister(tz->thermal_zone); | |
886 | tz->thermal_zone = NULL; | |
e6f8a4d6 | 887 | acpi_bus_detach_private_data(tz->device->handle); |
3f655ef8 ZR |
888 | } |
889 | ||
890 | ||
1da177e4 LT |
891 | /* -------------------------------------------------------------------------- |
892 | Driver Interface | |
893 | -------------------------------------------------------------------------- */ | |
894 | ||
81b704d3 RW |
895 | static void acpi_queue_thermal_check(struct acpi_thermal *tz) |
896 | { | |
897 | if (!work_pending(&tz->thermal_check_work)) | |
898 | queue_work(acpi_thermal_pm_queue, &tz->thermal_check_work); | |
899 | } | |
900 | ||
342d550d | 901 | static void acpi_thermal_notify(struct acpi_device *device, u32 event) |
1da177e4 | 902 | { |
342d550d | 903 | struct acpi_thermal *tz = acpi_driver_data(device); |
1da177e4 | 904 | |
1da177e4 LT |
905 | |
906 | if (!tz) | |
d550d98d | 907 | return; |
1da177e4 | 908 | |
1da177e4 LT |
909 | switch (event) { |
910 | case ACPI_THERMAL_NOTIFY_TEMPERATURE: | |
81b704d3 | 911 | acpi_queue_thermal_check(tz); |
1da177e4 LT |
912 | break; |
913 | case ACPI_THERMAL_NOTIFY_THRESHOLDS: | |
ce44e197 | 914 | acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS); |
81b704d3 | 915 | acpi_queue_thermal_check(tz); |
962ce8ca | 916 | acpi_bus_generate_netlink_event(device->pnp.device_class, |
0794469d | 917 | dev_name(&device->dev), event, 0); |
1da177e4 LT |
918 | break; |
919 | case ACPI_THERMAL_NOTIFY_DEVICES: | |
ce44e197 | 920 | acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES); |
81b704d3 | 921 | acpi_queue_thermal_check(tz); |
962ce8ca | 922 | acpi_bus_generate_netlink_event(device->pnp.device_class, |
0794469d | 923 | dev_name(&device->dev), event, 0); |
1da177e4 LT |
924 | break; |
925 | default: | |
926 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | |
4be44fcd | 927 | "Unsupported event [0x%x]\n", event)); |
1da177e4 LT |
928 | break; |
929 | } | |
1da177e4 LT |
930 | } |
931 | ||
261cba2d ZR |
932 | /* |
933 | * On some platforms, the AML code has dependency about | |
934 | * the evaluating order of _TMP and _CRT/_HOT/_PSV/_ACx. | |
935 | * 1. On HP Pavilion G4-1016tx, _TMP must be invoked after | |
936 | * /_CRT/_HOT/_PSV/_ACx, or else system will be power off. | |
937 | * 2. On HP Compaq 6715b/6715s, the return value of _PSV is 0 | |
938 | * if _TMP has never been evaluated. | |
939 | * | |
940 | * As this dependency is totally transparent to OS, evaluate | |
941 | * all of them once, in the order of _CRT/_HOT/_PSV/_ACx, | |
942 | * _TMP, before they are actually used. | |
943 | */ | |
944 | static void acpi_thermal_aml_dependency_fix(struct acpi_thermal *tz) | |
945 | { | |
946 | acpi_handle handle = tz->device->handle; | |
947 | unsigned long long value; | |
948 | int i; | |
949 | ||
950 | acpi_evaluate_integer(handle, "_CRT", NULL, &value); | |
951 | acpi_evaluate_integer(handle, "_HOT", NULL, &value); | |
952 | acpi_evaluate_integer(handle, "_PSV", NULL, &value); | |
953 | for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) { | |
954 | char name[5] = { '_', 'A', 'C', ('0' + i), '\0' }; | |
955 | acpi_status status; | |
956 | ||
957 | status = acpi_evaluate_integer(handle, name, NULL, &value); | |
958 | if (status == AE_NOT_FOUND) | |
959 | break; | |
960 | } | |
961 | acpi_evaluate_integer(handle, "_TMP", NULL, &value); | |
962 | } | |
963 | ||
4be44fcd | 964 | static int acpi_thermal_get_info(struct acpi_thermal *tz) |
1da177e4 | 965 | { |
4be44fcd | 966 | int result = 0; |
1da177e4 | 967 | |
1da177e4 LT |
968 | |
969 | if (!tz) | |
d550d98d | 970 | return -EINVAL; |
1da177e4 | 971 | |
261cba2d ZR |
972 | acpi_thermal_aml_dependency_fix(tz); |
973 | ||
9bcb8118 MG |
974 | /* Get trip points [_CRT, _PSV, etc.] (required) */ |
975 | result = acpi_thermal_get_trip_points(tz); | |
1da177e4 | 976 | if (result) |
d550d98d | 977 | return result; |
1da177e4 | 978 | |
9bcb8118 MG |
979 | /* Get temperature [_TMP] (required) */ |
980 | result = acpi_thermal_get_temperature(tz); | |
1da177e4 | 981 | if (result) |
d550d98d | 982 | return result; |
1da177e4 LT |
983 | |
984 | /* Set the cooling mode [_SCP] to active cooling (default) */ | |
985 | result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE); | |
4be44fcd | 986 | if (!result) |
1da177e4 | 987 | tz->flags.cooling_mode = 1; |
1da177e4 LT |
988 | |
989 | /* Get default polling frequency [_TZP] (optional) */ | |
990 | if (tzp) | |
991 | tz->polling_frequency = tzp; | |
992 | else | |
993 | acpi_thermal_get_polling_frequency(tz); | |
994 | ||
d550d98d | 995 | return 0; |
1da177e4 LT |
996 | } |
997 | ||
13614e37 JD |
998 | /* |
999 | * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI | |
1000 | * handles temperature values with a single decimal place. As a consequence, | |
1001 | * some implementations use an offset of 273.1 and others use an offset of | |
1002 | * 273.2. Try to find out which one is being used, to present the most | |
1003 | * accurate and visually appealing number. | |
1004 | * | |
1005 | * The heuristic below should work for all ACPI thermal zones which have a | |
1006 | * critical trip point with a value being a multiple of 0.5 degree Celsius. | |
1007 | */ | |
1008 | static void acpi_thermal_guess_offset(struct acpi_thermal *tz) | |
1009 | { | |
1010 | if (tz->trips.critical.flags.valid && | |
1011 | (tz->trips.critical.temperature % 5) == 1) | |
7f49a5cb | 1012 | tz->kelvin_offset = 273100; |
13614e37 | 1013 | else |
7f49a5cb | 1014 | tz->kelvin_offset = 273200; |
13614e37 JD |
1015 | } |
1016 | ||
a59ffb20 AL |
1017 | static void acpi_thermal_check_fn(struct work_struct *work) |
1018 | { | |
1019 | struct acpi_thermal *tz = container_of(work, struct acpi_thermal, | |
1020 | thermal_check_work); | |
81b704d3 RW |
1021 | |
1022 | /* | |
1023 | * In general, it is not sufficient to check the pending bit, because | |
1024 | * subsequent instances of this function may be queued after one of them | |
1025 | * has started running (e.g. if _TMP sleeps). Avoid bailing out if just | |
1026 | * one of them is running, though, because it may have done the actual | |
1027 | * check some time ago, so allow at least one of them to block on the | |
1028 | * mutex while another one is running the update. | |
1029 | */ | |
1030 | if (!refcount_dec_not_one(&tz->thermal_check_count)) | |
1031 | return; | |
1032 | ||
1033 | mutex_lock(&tz->thermal_check_lock); | |
1034 | ||
1035 | thermal_zone_device_update(tz->thermal_zone, THERMAL_EVENT_UNSPECIFIED); | |
1036 | ||
1037 | refcount_inc(&tz->thermal_check_count); | |
1038 | ||
1039 | mutex_unlock(&tz->thermal_check_lock); | |
a59ffb20 AL |
1040 | } |
1041 | ||
4be44fcd | 1042 | static int acpi_thermal_add(struct acpi_device *device) |
1da177e4 | 1043 | { |
4be44fcd | 1044 | int result = 0; |
4be44fcd | 1045 | struct acpi_thermal *tz = NULL; |
1da177e4 | 1046 | |
1da177e4 LT |
1047 | |
1048 | if (!device) | |
d550d98d | 1049 | return -EINVAL; |
1da177e4 | 1050 | |
36bcbec7 | 1051 | tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL); |
1da177e4 | 1052 | if (!tz) |
d550d98d | 1053 | return -ENOMEM; |
1da177e4 | 1054 | |
8348e1b1 | 1055 | tz->device = device; |
1da177e4 LT |
1056 | strcpy(tz->name, device->pnp.bus_id); |
1057 | strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME); | |
1058 | strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS); | |
db89b4f0 | 1059 | device->driver_data = tz; |
3f655ef8 | 1060 | |
1da177e4 LT |
1061 | result = acpi_thermal_get_info(tz); |
1062 | if (result) | |
3f655ef8 ZR |
1063 | goto free_memory; |
1064 | ||
13614e37 JD |
1065 | acpi_thermal_guess_offset(tz); |
1066 | ||
3f655ef8 ZR |
1067 | result = acpi_thermal_register_thermal_zone(tz); |
1068 | if (result) | |
1069 | goto free_memory; | |
1da177e4 | 1070 | |
81b704d3 RW |
1071 | refcount_set(&tz->thermal_check_count, 3); |
1072 | mutex_init(&tz->thermal_check_lock); | |
a59ffb20 AL |
1073 | INIT_WORK(&tz->thermal_check_work, acpi_thermal_check_fn); |
1074 | ||
766a8a6d | 1075 | pr_info(PREFIX "%s [%s] (%ld C)\n", acpi_device_name(device), |
7f49a5cb | 1076 | acpi_device_bid(device), deci_kelvin_to_celsius(tz->temperature)); |
3f655ef8 | 1077 | goto end; |
1da177e4 | 1078 | |
3f655ef8 ZR |
1079 | free_memory: |
1080 | kfree(tz); | |
1081 | end: | |
d550d98d | 1082 | return result; |
1da177e4 LT |
1083 | } |
1084 | ||
51fac838 | 1085 | static int acpi_thermal_remove(struct acpi_device *device) |
1da177e4 | 1086 | { |
4be44fcd | 1087 | struct acpi_thermal *tz = NULL; |
1da177e4 | 1088 | |
1da177e4 | 1089 | if (!device || !acpi_driver_data(device)) |
d550d98d | 1090 | return -EINVAL; |
1da177e4 | 1091 | |
a59ffb20 | 1092 | flush_workqueue(acpi_thermal_pm_queue); |
50dd0969 | 1093 | tz = acpi_driver_data(device); |
1da177e4 | 1094 | |
3f655ef8 | 1095 | acpi_thermal_unregister_thermal_zone(tz); |
1da177e4 | 1096 | kfree(tz); |
d550d98d | 1097 | return 0; |
1da177e4 LT |
1098 | } |
1099 | ||
90692404 | 1100 | #ifdef CONFIG_PM_SLEEP |
a59ffb20 AL |
1101 | static int acpi_thermal_suspend(struct device *dev) |
1102 | { | |
1103 | /* Make sure the previously queued thermal check work has been done */ | |
1104 | flush_workqueue(acpi_thermal_pm_queue); | |
1105 | return 0; | |
1106 | } | |
1107 | ||
167cffb6 | 1108 | static int acpi_thermal_resume(struct device *dev) |
74ce1468 | 1109 | { |
167cffb6 | 1110 | struct acpi_thermal *tz; |
b1028c54 KK |
1111 | int i, j, power_state, result; |
1112 | ||
167cffb6 | 1113 | if (!dev) |
d550d98d | 1114 | return -EINVAL; |
74ce1468 | 1115 | |
167cffb6 RW |
1116 | tz = acpi_driver_data(to_acpi_device(dev)); |
1117 | if (!tz) | |
1118 | return -EINVAL; | |
74ce1468 | 1119 | |
bed936f7 | 1120 | for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) { |
b1028c54 KK |
1121 | if (!(&tz->trips.active[i])) |
1122 | break; | |
1123 | if (!tz->trips.active[i].flags.valid) | |
1124 | break; | |
1125 | tz->trips.active[i].flags.enabled = 1; | |
1126 | for (j = 0; j < tz->trips.active[i].devices.count; j++) { | |
488a76c5 RW |
1127 | result = acpi_bus_update_power( |
1128 | tz->trips.active[i].devices.handles[j], | |
1129 | &power_state); | |
b1028c54 KK |
1130 | if (result || (power_state != ACPI_STATE_D0)) { |
1131 | tz->trips.active[i].flags.enabled = 0; | |
1132 | break; | |
1133 | } | |
bed936f7 | 1134 | } |
b1028c54 | 1135 | tz->state.active |= tz->trips.active[i].flags.enabled; |
bed936f7 KK |
1136 | } |
1137 | ||
81b704d3 | 1138 | acpi_queue_thermal_check(tz); |
74ce1468 KK |
1139 | |
1140 | return AE_OK; | |
1141 | } | |
90692404 | 1142 | #endif |
74ce1468 | 1143 | |
1855256c | 1144 | static int thermal_act(const struct dmi_system_id *d) { |
0b5bfa1c LB |
1145 | |
1146 | if (act == 0) { | |
766a8a6d AS |
1147 | pr_notice(PREFIX "%s detected: " |
1148 | "disabling all active thermal trip points\n", d->ident); | |
0b5bfa1c LB |
1149 | act = -1; |
1150 | } | |
1151 | return 0; | |
1152 | } | |
1855256c | 1153 | static int thermal_nocrt(const struct dmi_system_id *d) { |
8c99fdce | 1154 | |
766a8a6d AS |
1155 | pr_notice(PREFIX "%s detected: " |
1156 | "disabling all critical thermal trip point actions.\n", d->ident); | |
8c99fdce LB |
1157 | nocrt = 1; |
1158 | return 0; | |
1159 | } | |
1855256c | 1160 | static int thermal_tzp(const struct dmi_system_id *d) { |
0b5bfa1c LB |
1161 | |
1162 | if (tzp == 0) { | |
766a8a6d AS |
1163 | pr_notice(PREFIX "%s detected: " |
1164 | "enabling thermal zone polling\n", d->ident); | |
0b5bfa1c LB |
1165 | tzp = 300; /* 300 dS = 30 Seconds */ |
1166 | } | |
1167 | return 0; | |
1168 | } | |
1855256c | 1169 | static int thermal_psv(const struct dmi_system_id *d) { |
0b5bfa1c LB |
1170 | |
1171 | if (psv == 0) { | |
766a8a6d AS |
1172 | pr_notice(PREFIX "%s detected: " |
1173 | "disabling all passive thermal trip points\n", d->ident); | |
0b5bfa1c LB |
1174 | psv = -1; |
1175 | } | |
1176 | return 0; | |
1177 | } | |
1178 | ||
6faadbbb | 1179 | static const struct dmi_system_id thermal_dmi_table[] __initconst = { |
0b5bfa1c LB |
1180 | /* |
1181 | * Award BIOS on this AOpen makes thermal control almost worthless. | |
1182 | * http://bugzilla.kernel.org/show_bug.cgi?id=8842 | |
1183 | */ | |
1184 | { | |
1185 | .callback = thermal_act, | |
1186 | .ident = "AOpen i915GMm-HFS", | |
1187 | .matches = { | |
1188 | DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"), | |
1189 | DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"), | |
1190 | }, | |
1191 | }, | |
1192 | { | |
1193 | .callback = thermal_psv, | |
1194 | .ident = "AOpen i915GMm-HFS", | |
1195 | .matches = { | |
1196 | DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"), | |
1197 | DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"), | |
1198 | }, | |
1199 | }, | |
1200 | { | |
1201 | .callback = thermal_tzp, | |
1202 | .ident = "AOpen i915GMm-HFS", | |
1203 | .matches = { | |
1204 | DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"), | |
1205 | DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"), | |
1206 | }, | |
1207 | }, | |
8c99fdce LB |
1208 | { |
1209 | .callback = thermal_nocrt, | |
1210 | .ident = "Gigabyte GA-7ZX", | |
1211 | .matches = { | |
1212 | DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."), | |
1213 | DMI_MATCH(DMI_BOARD_NAME, "7ZX"), | |
1214 | }, | |
1215 | }, | |
0b5bfa1c LB |
1216 | {} |
1217 | }; | |
0b5bfa1c | 1218 | |
4be44fcd | 1219 | static int __init acpi_thermal_init(void) |
1da177e4 | 1220 | { |
4be44fcd | 1221 | int result = 0; |
1da177e4 | 1222 | |
0b5bfa1c LB |
1223 | dmi_check_system(thermal_dmi_table); |
1224 | ||
72b33ef8 | 1225 | if (off) { |
766a8a6d | 1226 | pr_notice(PREFIX "thermal control disabled\n"); |
72b33ef8 LB |
1227 | return -ENODEV; |
1228 | } | |
43d9f87b | 1229 | |
e053dc90 BS |
1230 | acpi_thermal_pm_queue = alloc_workqueue("acpi_thermal_pm", |
1231 | WQ_HIGHPRI | WQ_MEM_RECLAIM, 0); | |
a59ffb20 AL |
1232 | if (!acpi_thermal_pm_queue) |
1233 | return -ENODEV; | |
1234 | ||
1da177e4 | 1235 | result = acpi_bus_register_driver(&acpi_thermal_driver); |
a59ffb20 AL |
1236 | if (result < 0) { |
1237 | destroy_workqueue(acpi_thermal_pm_queue); | |
d550d98d | 1238 | return -ENODEV; |
a59ffb20 | 1239 | } |
1da177e4 | 1240 | |
d550d98d | 1241 | return 0; |
1da177e4 LT |
1242 | } |
1243 | ||
4be44fcd | 1244 | static void __exit acpi_thermal_exit(void) |
1da177e4 | 1245 | { |
1da177e4 | 1246 | acpi_bus_unregister_driver(&acpi_thermal_driver); |
2807bd18 | 1247 | destroy_workqueue(acpi_thermal_pm_queue); |
1da177e4 | 1248 | |
d550d98d | 1249 | return; |
1da177e4 LT |
1250 | } |
1251 | ||
1da177e4 LT |
1252 | module_init(acpi_thermal_init); |
1253 | module_exit(acpi_thermal_exit); |