Commit | Line | Data |
---|---|---|
ec2cd81c RW |
1 | /* |
2 | * drivers/acpi/device_pm.c - ACPI device power management routines. | |
3 | * | |
4 | * Copyright (C) 2012, Intel Corp. | |
5 | * Author: Rafael J. Wysocki <rafael.j.wysocki@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 version 2 as published | |
11 | * by the Free Software Foundation. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License along | |
19 | * with this program; if not, write to the Free Software Foundation, Inc., | |
20 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
21 | * | |
22 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
23 | */ | |
24 | ||
25 | #include <linux/device.h> | |
86b3832c | 26 | #include <linux/export.h> |
ec2cd81c | 27 | #include <linux/mutex.h> |
86b3832c | 28 | #include <linux/pm_qos.h> |
cd7bd02d | 29 | #include <linux/pm_runtime.h> |
ec2cd81c RW |
30 | |
31 | #include <acpi/acpi.h> | |
32 | #include <acpi/acpi_bus.h> | |
9ce4e607 RW |
33 | #include <acpi/acpi_drivers.h> |
34 | ||
35 | #include "internal.h" | |
36 | ||
37 | #define _COMPONENT ACPI_POWER_COMPONENT | |
38 | ACPI_MODULE_NAME("device_pm"); | |
ec2cd81c RW |
39 | |
40 | static DEFINE_MUTEX(acpi_pm_notifier_lock); | |
41 | ||
42 | /** | |
43 | * acpi_add_pm_notifier - Register PM notifier for given ACPI device. | |
44 | * @adev: ACPI device to add the notifier for. | |
45 | * @context: Context information to pass to the notifier routine. | |
46 | * | |
47 | * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of | |
48 | * PM wakeup events. For example, wakeup events may be generated for bridges | |
49 | * if one of the devices below the bridge is signaling wakeup, even if the | |
50 | * bridge itself doesn't have a wakeup GPE associated with it. | |
51 | */ | |
52 | acpi_status acpi_add_pm_notifier(struct acpi_device *adev, | |
53 | acpi_notify_handler handler, void *context) | |
54 | { | |
55 | acpi_status status = AE_ALREADY_EXISTS; | |
56 | ||
57 | mutex_lock(&acpi_pm_notifier_lock); | |
58 | ||
59 | if (adev->wakeup.flags.notifier_present) | |
60 | goto out; | |
61 | ||
62 | status = acpi_install_notify_handler(adev->handle, | |
63 | ACPI_SYSTEM_NOTIFY, | |
64 | handler, context); | |
65 | if (ACPI_FAILURE(status)) | |
66 | goto out; | |
67 | ||
68 | adev->wakeup.flags.notifier_present = true; | |
69 | ||
70 | out: | |
71 | mutex_unlock(&acpi_pm_notifier_lock); | |
72 | return status; | |
73 | } | |
74 | ||
75 | /** | |
76 | * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device. | |
77 | * @adev: ACPI device to remove the notifier from. | |
78 | */ | |
79 | acpi_status acpi_remove_pm_notifier(struct acpi_device *adev, | |
80 | acpi_notify_handler handler) | |
81 | { | |
82 | acpi_status status = AE_BAD_PARAMETER; | |
83 | ||
84 | mutex_lock(&acpi_pm_notifier_lock); | |
85 | ||
86 | if (!adev->wakeup.flags.notifier_present) | |
87 | goto out; | |
88 | ||
89 | status = acpi_remove_notify_handler(adev->handle, | |
90 | ACPI_SYSTEM_NOTIFY, | |
91 | handler); | |
92 | if (ACPI_FAILURE(status)) | |
93 | goto out; | |
94 | ||
95 | adev->wakeup.flags.notifier_present = false; | |
96 | ||
97 | out: | |
98 | mutex_unlock(&acpi_pm_notifier_lock); | |
99 | return status; | |
100 | } | |
86b3832c | 101 | |
9ce4e607 RW |
102 | /** |
103 | * acpi_power_state_string - String representation of ACPI device power state. | |
104 | * @state: ACPI device power state to return the string representation of. | |
105 | */ | |
106 | const char *acpi_power_state_string(int state) | |
107 | { | |
108 | switch (state) { | |
109 | case ACPI_STATE_D0: | |
110 | return "D0"; | |
111 | case ACPI_STATE_D1: | |
112 | return "D1"; | |
113 | case ACPI_STATE_D2: | |
114 | return "D2"; | |
115 | case ACPI_STATE_D3_HOT: | |
116 | return "D3hot"; | |
117 | case ACPI_STATE_D3_COLD: | |
898fee4f | 118 | return "D3cold"; |
9ce4e607 RW |
119 | default: |
120 | return "(unknown)"; | |
121 | } | |
122 | } | |
123 | ||
124 | /** | |
125 | * acpi_device_get_power - Get power state of an ACPI device. | |
126 | * @device: Device to get the power state of. | |
127 | * @state: Place to store the power state of the device. | |
128 | * | |
129 | * This function does not update the device's power.state field, but it may | |
130 | * update its parent's power.state field (when the parent's power state is | |
131 | * unknown and the device's power state turns out to be D0). | |
132 | */ | |
133 | int acpi_device_get_power(struct acpi_device *device, int *state) | |
134 | { | |
135 | int result = ACPI_STATE_UNKNOWN; | |
136 | ||
137 | if (!device || !state) | |
138 | return -EINVAL; | |
139 | ||
140 | if (!device->flags.power_manageable) { | |
141 | /* TBD: Non-recursive algorithm for walking up hierarchy. */ | |
142 | *state = device->parent ? | |
143 | device->parent->power.state : ACPI_STATE_D0; | |
144 | goto out; | |
145 | } | |
146 | ||
147 | /* | |
148 | * Get the device's power state either directly (via _PSC) or | |
149 | * indirectly (via power resources). | |
150 | */ | |
151 | if (device->power.flags.explicit_get) { | |
152 | unsigned long long psc; | |
153 | acpi_status status = acpi_evaluate_integer(device->handle, | |
154 | "_PSC", NULL, &psc); | |
155 | if (ACPI_FAILURE(status)) | |
156 | return -ENODEV; | |
157 | ||
158 | result = psc; | |
159 | } | |
160 | /* The test below covers ACPI_STATE_UNKNOWN too. */ | |
161 | if (result <= ACPI_STATE_D2) { | |
162 | ; /* Do nothing. */ | |
163 | } else if (device->power.flags.power_resources) { | |
164 | int error = acpi_power_get_inferred_state(device, &result); | |
165 | if (error) | |
166 | return error; | |
167 | } else if (result == ACPI_STATE_D3_HOT) { | |
168 | result = ACPI_STATE_D3; | |
169 | } | |
170 | ||
171 | /* | |
172 | * If we were unsure about the device parent's power state up to this | |
173 | * point, the fact that the device is in D0 implies that the parent has | |
174 | * to be in D0 too. | |
175 | */ | |
176 | if (device->parent && device->parent->power.state == ACPI_STATE_UNKNOWN | |
177 | && result == ACPI_STATE_D0) | |
178 | device->parent->power.state = ACPI_STATE_D0; | |
179 | ||
180 | *state = result; | |
181 | ||
182 | out: | |
183 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is %s\n", | |
184 | device->pnp.bus_id, acpi_power_state_string(*state))); | |
185 | ||
186 | return 0; | |
187 | } | |
188 | ||
9c0f45e3 RW |
189 | static int acpi_dev_pm_explicit_set(struct acpi_device *adev, int state) |
190 | { | |
191 | if (adev->power.states[state].flags.explicit_set) { | |
192 | char method[5] = { '_', 'P', 'S', '0' + state, '\0' }; | |
193 | acpi_status status; | |
194 | ||
195 | status = acpi_evaluate_object(adev->handle, method, NULL, NULL); | |
196 | if (ACPI_FAILURE(status)) | |
197 | return -ENODEV; | |
198 | } | |
199 | return 0; | |
200 | } | |
201 | ||
9ce4e607 RW |
202 | /** |
203 | * acpi_device_set_power - Set power state of an ACPI device. | |
204 | * @device: Device to set the power state of. | |
205 | * @state: New power state to set. | |
206 | * | |
207 | * Callers must ensure that the device is power manageable before using this | |
208 | * function. | |
209 | */ | |
210 | int acpi_device_set_power(struct acpi_device *device, int state) | |
211 | { | |
212 | int result = 0; | |
9ce4e607 RW |
213 | bool cut_power = false; |
214 | ||
215 | if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD)) | |
216 | return -EINVAL; | |
217 | ||
218 | /* Make sure this is a valid target state */ | |
219 | ||
220 | if (state == device->power.state) { | |
221 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device is already at %s\n", | |
222 | acpi_power_state_string(state))); | |
223 | return 0; | |
224 | } | |
225 | ||
226 | if (!device->power.states[state].flags.valid) { | |
227 | printk(KERN_WARNING PREFIX "Device does not support %s\n", | |
228 | acpi_power_state_string(state)); | |
229 | return -ENODEV; | |
230 | } | |
231 | if (device->parent && (state < device->parent->power.state)) { | |
232 | printk(KERN_WARNING PREFIX | |
233 | "Cannot set device to a higher-powered" | |
234 | " state than parent\n"); | |
235 | return -ENODEV; | |
236 | } | |
237 | ||
238 | /* For D3cold we should first transition into D3hot. */ | |
239 | if (state == ACPI_STATE_D3_COLD | |
240 | && device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible) { | |
241 | state = ACPI_STATE_D3_HOT; | |
9ce4e607 RW |
242 | cut_power = true; |
243 | } | |
244 | ||
e78adb75 RW |
245 | if (state < device->power.state && state != ACPI_STATE_D0 |
246 | && device->power.state >= ACPI_STATE_D3_HOT) { | |
247 | printk(KERN_WARNING PREFIX | |
248 | "Cannot transition to non-D0 state from D3\n"); | |
249 | return -ENODEV; | |
250 | } | |
251 | ||
9ce4e607 RW |
252 | /* |
253 | * Transition Power | |
254 | * ---------------- | |
e78adb75 RW |
255 | * In accordance with the ACPI specification first apply power (via |
256 | * power resources) and then evalute _PSx. | |
9ce4e607 | 257 | */ |
e78adb75 RW |
258 | if (device->power.flags.power_resources) { |
259 | result = acpi_power_transition(device, state); | |
9c0f45e3 RW |
260 | if (result) |
261 | goto end; | |
9ce4e607 | 262 | } |
e78adb75 RW |
263 | result = acpi_dev_pm_explicit_set(device, state); |
264 | if (result) | |
265 | goto end; | |
9ce4e607 RW |
266 | |
267 | if (cut_power) | |
268 | result = acpi_power_transition(device, ACPI_STATE_D3_COLD); | |
269 | ||
e78adb75 RW |
270 | end: |
271 | if (result) { | |
9ce4e607 RW |
272 | printk(KERN_WARNING PREFIX |
273 | "Device [%s] failed to transition to %s\n", | |
274 | device->pnp.bus_id, | |
275 | acpi_power_state_string(state)); | |
e78adb75 | 276 | } else { |
9ce4e607 RW |
277 | device->power.state = state; |
278 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | |
279 | "Device [%s] transitioned to %s\n", | |
280 | device->pnp.bus_id, | |
281 | acpi_power_state_string(state))); | |
282 | } | |
283 | ||
284 | return result; | |
285 | } | |
286 | EXPORT_SYMBOL(acpi_device_set_power); | |
287 | ||
288 | int acpi_bus_set_power(acpi_handle handle, int state) | |
289 | { | |
290 | struct acpi_device *device; | |
291 | int result; | |
292 | ||
293 | result = acpi_bus_get_device(handle, &device); | |
294 | if (result) | |
295 | return result; | |
296 | ||
297 | if (!device->flags.power_manageable) { | |
298 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | |
299 | "Device [%s] is not power manageable\n", | |
300 | dev_name(&device->dev))); | |
301 | return -ENODEV; | |
302 | } | |
303 | ||
304 | return acpi_device_set_power(device, state); | |
305 | } | |
306 | EXPORT_SYMBOL(acpi_bus_set_power); | |
307 | ||
308 | int acpi_bus_init_power(struct acpi_device *device) | |
309 | { | |
310 | int state; | |
311 | int result; | |
312 | ||
313 | if (!device) | |
314 | return -EINVAL; | |
315 | ||
316 | device->power.state = ACPI_STATE_UNKNOWN; | |
317 | ||
318 | result = acpi_device_get_power(device, &state); | |
319 | if (result) | |
320 | return result; | |
321 | ||
a2367807 | 322 | if (state < ACPI_STATE_D3_COLD && device->power.flags.power_resources) { |
9ce4e607 | 323 | result = acpi_power_on_resources(device, state); |
a2367807 RW |
324 | if (result) |
325 | return result; | |
9ce4e607 | 326 | |
9c0f45e3 RW |
327 | result = acpi_dev_pm_explicit_set(device, state); |
328 | if (result) | |
329 | return result; | |
a2367807 RW |
330 | } |
331 | device->power.state = state; | |
332 | return 0; | |
9ce4e607 RW |
333 | } |
334 | ||
335 | int acpi_bus_update_power(acpi_handle handle, int *state_p) | |
336 | { | |
337 | struct acpi_device *device; | |
338 | int state; | |
339 | int result; | |
340 | ||
341 | result = acpi_bus_get_device(handle, &device); | |
342 | if (result) | |
343 | return result; | |
344 | ||
345 | result = acpi_device_get_power(device, &state); | |
346 | if (result) | |
347 | return result; | |
348 | ||
349 | result = acpi_device_set_power(device, state); | |
350 | if (!result && state_p) | |
351 | *state_p = state; | |
352 | ||
353 | return result; | |
354 | } | |
355 | EXPORT_SYMBOL_GPL(acpi_bus_update_power); | |
356 | ||
357 | bool acpi_bus_power_manageable(acpi_handle handle) | |
358 | { | |
359 | struct acpi_device *device; | |
360 | int result; | |
361 | ||
362 | result = acpi_bus_get_device(handle, &device); | |
363 | return result ? false : device->flags.power_manageable; | |
364 | } | |
365 | EXPORT_SYMBOL(acpi_bus_power_manageable); | |
366 | ||
367 | bool acpi_bus_can_wakeup(acpi_handle handle) | |
368 | { | |
369 | struct acpi_device *device; | |
370 | int result; | |
371 | ||
372 | result = acpi_bus_get_device(handle, &device); | |
373 | return result ? false : device->wakeup.flags.valid; | |
374 | } | |
375 | EXPORT_SYMBOL(acpi_bus_can_wakeup); | |
376 | ||
86b3832c RW |
377 | /** |
378 | * acpi_device_power_state - Get preferred power state of ACPI device. | |
379 | * @dev: Device whose preferred target power state to return. | |
380 | * @adev: ACPI device node corresponding to @dev. | |
381 | * @target_state: System state to match the resultant device state. | |
382 | * @d_max_in: Deepest low-power state to take into consideration. | |
383 | * @d_min_p: Location to store the upper limit of the allowed states range. | |
384 | * Return value: Preferred power state of the device on success, -ENODEV | |
385 | * (if there's no 'struct acpi_device' for @dev) or -EINVAL on failure | |
386 | * | |
387 | * Find the lowest power (highest number) ACPI device power state that the | |
388 | * device can be in while the system is in the state represented by | |
389 | * @target_state. If @d_min_p is set, the highest power (lowest number) device | |
390 | * power state that @dev can be in for the given system sleep state is stored | |
391 | * at the location pointed to by it. | |
392 | * | |
393 | * Callers must ensure that @dev and @adev are valid pointers and that @adev | |
394 | * actually corresponds to @dev before using this function. | |
395 | */ | |
396 | int acpi_device_power_state(struct device *dev, struct acpi_device *adev, | |
397 | u32 target_state, int d_max_in, int *d_min_p) | |
398 | { | |
399 | char acpi_method[] = "_SxD"; | |
400 | unsigned long long d_min, d_max; | |
401 | bool wakeup = false; | |
402 | ||
403 | if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3) | |
404 | return -EINVAL; | |
405 | ||
406 | if (d_max_in > ACPI_STATE_D3_HOT) { | |
407 | enum pm_qos_flags_status stat; | |
408 | ||
409 | stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF); | |
410 | if (stat == PM_QOS_FLAGS_ALL) | |
411 | d_max_in = ACPI_STATE_D3_HOT; | |
412 | } | |
413 | ||
414 | acpi_method[2] = '0' + target_state; | |
415 | /* | |
416 | * If the sleep state is S0, the lowest limit from ACPI is D3, | |
417 | * but if the device has _S0W, we will use the value from _S0W | |
418 | * as the lowest limit from ACPI. Finally, we will constrain | |
419 | * the lowest limit with the specified one. | |
420 | */ | |
421 | d_min = ACPI_STATE_D0; | |
422 | d_max = ACPI_STATE_D3; | |
423 | ||
424 | /* | |
425 | * If present, _SxD methods return the minimum D-state (highest power | |
426 | * state) we can use for the corresponding S-states. Otherwise, the | |
427 | * minimum D-state is D0 (ACPI 3.x). | |
428 | * | |
429 | * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer | |
430 | * provided -- that's our fault recovery, we ignore retval. | |
431 | */ | |
432 | if (target_state > ACPI_STATE_S0) { | |
433 | acpi_evaluate_integer(adev->handle, acpi_method, NULL, &d_min); | |
434 | wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid | |
435 | && adev->wakeup.sleep_state >= target_state; | |
436 | } else if (dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) != | |
437 | PM_QOS_FLAGS_NONE) { | |
438 | wakeup = adev->wakeup.flags.valid; | |
439 | } | |
440 | ||
441 | /* | |
442 | * If _PRW says we can wake up the system from the target sleep state, | |
443 | * the D-state returned by _SxD is sufficient for that (we assume a | |
444 | * wakeup-aware driver if wake is set). Still, if _SxW exists | |
445 | * (ACPI 3.x), it should return the maximum (lowest power) D-state that | |
446 | * can wake the system. _S0W may be valid, too. | |
447 | */ | |
448 | if (wakeup) { | |
449 | acpi_status status; | |
450 | ||
451 | acpi_method[3] = 'W'; | |
452 | status = acpi_evaluate_integer(adev->handle, acpi_method, NULL, | |
453 | &d_max); | |
454 | if (ACPI_FAILURE(status)) { | |
455 | if (target_state != ACPI_STATE_S0 || | |
456 | status != AE_NOT_FOUND) | |
457 | d_max = d_min; | |
458 | } else if (d_max < d_min) { | |
459 | /* Warn the user of the broken DSDT */ | |
460 | printk(KERN_WARNING "ACPI: Wrong value from %s\n", | |
461 | acpi_method); | |
462 | /* Sanitize it */ | |
463 | d_min = d_max; | |
464 | } | |
465 | } | |
466 | ||
467 | if (d_max_in < d_min) | |
468 | return -EINVAL; | |
469 | if (d_min_p) | |
470 | *d_min_p = d_min; | |
471 | /* constrain d_max with specified lowest limit (max number) */ | |
472 | if (d_max > d_max_in) { | |
473 | for (d_max = d_max_in; d_max > d_min; d_max--) { | |
474 | if (adev->power.states[d_max].flags.valid) | |
475 | break; | |
476 | } | |
477 | } | |
478 | return d_max; | |
479 | } | |
480 | EXPORT_SYMBOL_GPL(acpi_device_power_state); | |
cd7bd02d | 481 | |
a6ae7594 RW |
482 | /** |
483 | * acpi_pm_device_sleep_state - Get preferred power state of ACPI device. | |
484 | * @dev: Device whose preferred target power state to return. | |
485 | * @d_min_p: Location to store the upper limit of the allowed states range. | |
486 | * @d_max_in: Deepest low-power state to take into consideration. | |
487 | * Return value: Preferred power state of the device on success, -ENODEV | |
488 | * (if there's no 'struct acpi_device' for @dev) or -EINVAL on failure | |
489 | * | |
490 | * The caller must ensure that @dev is valid before using this function. | |
491 | */ | |
492 | int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in) | |
493 | { | |
494 | acpi_handle handle = DEVICE_ACPI_HANDLE(dev); | |
495 | struct acpi_device *adev; | |
496 | ||
497 | if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) { | |
498 | dev_dbg(dev, "ACPI handle without context in %s!\n", __func__); | |
499 | return -ENODEV; | |
500 | } | |
501 | ||
502 | return acpi_device_power_state(dev, adev, acpi_target_system_state(), | |
503 | d_max_in, d_min_p); | |
504 | } | |
505 | EXPORT_SYMBOL(acpi_pm_device_sleep_state); | |
506 | ||
cd7bd02d | 507 | #ifdef CONFIG_PM_RUNTIME |
e5cc8ef3 RW |
508 | /** |
509 | * acpi_wakeup_device - Wakeup notification handler for ACPI devices. | |
510 | * @handle: ACPI handle of the device the notification is for. | |
511 | * @event: Type of the signaled event. | |
512 | * @context: Device corresponding to @handle. | |
513 | */ | |
514 | static void acpi_wakeup_device(acpi_handle handle, u32 event, void *context) | |
515 | { | |
516 | struct device *dev = context; | |
517 | ||
518 | if (event == ACPI_NOTIFY_DEVICE_WAKE && dev) { | |
519 | pm_wakeup_event(dev, 0); | |
520 | pm_runtime_resume(dev); | |
521 | } | |
522 | } | |
523 | ||
cd7bd02d | 524 | /** |
dee8370c RW |
525 | * __acpi_device_run_wake - Enable/disable runtime remote wakeup for device. |
526 | * @adev: ACPI device to enable/disable the remote wakeup for. | |
cd7bd02d RW |
527 | * @enable: Whether to enable or disable the wakeup functionality. |
528 | * | |
dee8370c RW |
529 | * Enable/disable the GPE associated with @adev so that it can generate |
530 | * wakeup signals for the device in response to external (remote) events and | |
531 | * enable/disable device wakeup power. | |
532 | * | |
533 | * Callers must ensure that @adev is a valid ACPI device node before executing | |
534 | * this function. | |
535 | */ | |
536 | int __acpi_device_run_wake(struct acpi_device *adev, bool enable) | |
537 | { | |
538 | struct acpi_device_wakeup *wakeup = &adev->wakeup; | |
539 | ||
540 | if (enable) { | |
541 | acpi_status res; | |
542 | int error; | |
543 | ||
544 | error = acpi_enable_wakeup_device_power(adev, ACPI_STATE_S0); | |
545 | if (error) | |
546 | return error; | |
547 | ||
548 | res = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number); | |
549 | if (ACPI_FAILURE(res)) { | |
550 | acpi_disable_wakeup_device_power(adev); | |
551 | return -EIO; | |
552 | } | |
553 | } else { | |
554 | acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number); | |
555 | acpi_disable_wakeup_device_power(adev); | |
556 | } | |
557 | return 0; | |
558 | } | |
559 | ||
560 | /** | |
561 | * acpi_pm_device_run_wake - Enable/disable remote wakeup for given device. | |
562 | * @dev: Device to enable/disable the platform to wake up. | |
563 | * @enable: Whether to enable or disable the wakeup functionality. | |
cd7bd02d RW |
564 | */ |
565 | int acpi_pm_device_run_wake(struct device *phys_dev, bool enable) | |
566 | { | |
dee8370c | 567 | struct acpi_device *adev; |
cd7bd02d RW |
568 | acpi_handle handle; |
569 | ||
570 | if (!device_run_wake(phys_dev)) | |
571 | return -EINVAL; | |
572 | ||
573 | handle = DEVICE_ACPI_HANDLE(phys_dev); | |
dee8370c RW |
574 | if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) { |
575 | dev_dbg(phys_dev, "ACPI handle without context in %s!\n", | |
cd7bd02d RW |
576 | __func__); |
577 | return -ENODEV; | |
578 | } | |
579 | ||
dee8370c | 580 | return __acpi_device_run_wake(adev, enable); |
cd7bd02d RW |
581 | } |
582 | EXPORT_SYMBOL(acpi_pm_device_run_wake); | |
e5cc8ef3 RW |
583 | #else |
584 | static inline void acpi_wakeup_device(acpi_handle handle, u32 event, | |
585 | void *context) {} | |
cd7bd02d | 586 | #endif /* CONFIG_PM_RUNTIME */ |
dee8370c | 587 | |
4d56410b | 588 | #ifdef CONFIG_PM_SLEEP |
dee8370c RW |
589 | /** |
590 | * __acpi_device_sleep_wake - Enable or disable device to wake up the system. | |
591 | * @dev: Device to enable/desible to wake up the system. | |
592 | * @target_state: System state the device is supposed to wake up from. | |
593 | * @enable: Whether to enable or disable @dev to wake up the system. | |
594 | */ | |
595 | int __acpi_device_sleep_wake(struct acpi_device *adev, u32 target_state, | |
596 | bool enable) | |
597 | { | |
598 | return enable ? | |
599 | acpi_enable_wakeup_device_power(adev, target_state) : | |
600 | acpi_disable_wakeup_device_power(adev); | |
601 | } | |
a6ae7594 RW |
602 | |
603 | /** | |
604 | * acpi_pm_device_sleep_wake - Enable or disable device to wake up the system. | |
605 | * @dev: Device to enable/desible to wake up the system from sleep states. | |
606 | * @enable: Whether to enable or disable @dev to wake up the system. | |
607 | */ | |
608 | int acpi_pm_device_sleep_wake(struct device *dev, bool enable) | |
609 | { | |
610 | acpi_handle handle; | |
611 | struct acpi_device *adev; | |
612 | int error; | |
613 | ||
614 | if (!device_can_wakeup(dev)) | |
615 | return -EINVAL; | |
616 | ||
617 | handle = DEVICE_ACPI_HANDLE(dev); | |
618 | if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) { | |
619 | dev_dbg(dev, "ACPI handle without context in %s!\n", __func__); | |
620 | return -ENODEV; | |
621 | } | |
622 | ||
623 | error = __acpi_device_sleep_wake(adev, acpi_target_system_state(), | |
624 | enable); | |
625 | if (!error) | |
626 | dev_info(dev, "System wakeup %s by ACPI\n", | |
627 | enable ? "enabled" : "disabled"); | |
628 | ||
629 | return error; | |
630 | } | |
dee8370c | 631 | #endif /* CONFIG_PM_SLEEP */ |
e5cc8ef3 RW |
632 | |
633 | /** | |
634 | * acpi_dev_pm_get_node - Get ACPI device node for the given physical device. | |
635 | * @dev: Device to get the ACPI node for. | |
636 | */ | |
d2e5f0c1 | 637 | struct acpi_device *acpi_dev_pm_get_node(struct device *dev) |
e5cc8ef3 RW |
638 | { |
639 | acpi_handle handle = DEVICE_ACPI_HANDLE(dev); | |
640 | struct acpi_device *adev; | |
641 | ||
5cc36c72 | 642 | return handle && !acpi_bus_get_device(handle, &adev) ? adev : NULL; |
e5cc8ef3 RW |
643 | } |
644 | ||
645 | /** | |
646 | * acpi_dev_pm_low_power - Put ACPI device into a low-power state. | |
647 | * @dev: Device to put into a low-power state. | |
648 | * @adev: ACPI device node corresponding to @dev. | |
649 | * @system_state: System state to choose the device state for. | |
650 | */ | |
651 | static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev, | |
652 | u32 system_state) | |
653 | { | |
654 | int power_state; | |
655 | ||
656 | if (!acpi_device_power_manageable(adev)) | |
657 | return 0; | |
658 | ||
659 | power_state = acpi_device_power_state(dev, adev, system_state, | |
660 | ACPI_STATE_D3, NULL); | |
661 | if (power_state < ACPI_STATE_D0 || power_state > ACPI_STATE_D3) | |
662 | return -EIO; | |
663 | ||
664 | return acpi_device_set_power(adev, power_state); | |
665 | } | |
666 | ||
667 | /** | |
668 | * acpi_dev_pm_full_power - Put ACPI device into the full-power state. | |
669 | * @adev: ACPI device node to put into the full-power state. | |
670 | */ | |
671 | static int acpi_dev_pm_full_power(struct acpi_device *adev) | |
672 | { | |
673 | return acpi_device_power_manageable(adev) ? | |
674 | acpi_device_set_power(adev, ACPI_STATE_D0) : 0; | |
675 | } | |
676 | ||
677 | #ifdef CONFIG_PM_RUNTIME | |
678 | /** | |
679 | * acpi_dev_runtime_suspend - Put device into a low-power state using ACPI. | |
680 | * @dev: Device to put into a low-power state. | |
681 | * | |
682 | * Put the given device into a runtime low-power state using the standard ACPI | |
683 | * mechanism. Set up remote wakeup if desired, choose the state to put the | |
684 | * device into (this checks if remote wakeup is expected to work too), and set | |
685 | * the power state of the device. | |
686 | */ | |
687 | int acpi_dev_runtime_suspend(struct device *dev) | |
688 | { | |
689 | struct acpi_device *adev = acpi_dev_pm_get_node(dev); | |
690 | bool remote_wakeup; | |
691 | int error; | |
692 | ||
693 | if (!adev) | |
694 | return 0; | |
695 | ||
696 | remote_wakeup = dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) > | |
697 | PM_QOS_FLAGS_NONE; | |
698 | error = __acpi_device_run_wake(adev, remote_wakeup); | |
699 | if (remote_wakeup && error) | |
700 | return -EAGAIN; | |
701 | ||
702 | error = acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0); | |
703 | if (error) | |
704 | __acpi_device_run_wake(adev, false); | |
705 | ||
706 | return error; | |
707 | } | |
708 | EXPORT_SYMBOL_GPL(acpi_dev_runtime_suspend); | |
709 | ||
710 | /** | |
711 | * acpi_dev_runtime_resume - Put device into the full-power state using ACPI. | |
712 | * @dev: Device to put into the full-power state. | |
713 | * | |
714 | * Put the given device into the full-power state using the standard ACPI | |
715 | * mechanism at run time. Set the power state of the device to ACPI D0 and | |
716 | * disable remote wakeup. | |
717 | */ | |
718 | int acpi_dev_runtime_resume(struct device *dev) | |
719 | { | |
720 | struct acpi_device *adev = acpi_dev_pm_get_node(dev); | |
721 | int error; | |
722 | ||
723 | if (!adev) | |
724 | return 0; | |
725 | ||
726 | error = acpi_dev_pm_full_power(adev); | |
727 | __acpi_device_run_wake(adev, false); | |
728 | return error; | |
729 | } | |
730 | EXPORT_SYMBOL_GPL(acpi_dev_runtime_resume); | |
731 | ||
732 | /** | |
733 | * acpi_subsys_runtime_suspend - Suspend device using ACPI. | |
734 | * @dev: Device to suspend. | |
735 | * | |
736 | * Carry out the generic runtime suspend procedure for @dev and use ACPI to put | |
737 | * it into a runtime low-power state. | |
738 | */ | |
739 | int acpi_subsys_runtime_suspend(struct device *dev) | |
740 | { | |
741 | int ret = pm_generic_runtime_suspend(dev); | |
742 | return ret ? ret : acpi_dev_runtime_suspend(dev); | |
743 | } | |
744 | EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend); | |
745 | ||
746 | /** | |
747 | * acpi_subsys_runtime_resume - Resume device using ACPI. | |
748 | * @dev: Device to Resume. | |
749 | * | |
750 | * Use ACPI to put the given device into the full-power state and carry out the | |
751 | * generic runtime resume procedure for it. | |
752 | */ | |
753 | int acpi_subsys_runtime_resume(struct device *dev) | |
754 | { | |
755 | int ret = acpi_dev_runtime_resume(dev); | |
756 | return ret ? ret : pm_generic_runtime_resume(dev); | |
757 | } | |
758 | EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume); | |
759 | #endif /* CONFIG_PM_RUNTIME */ | |
760 | ||
761 | #ifdef CONFIG_PM_SLEEP | |
762 | /** | |
763 | * acpi_dev_suspend_late - Put device into a low-power state using ACPI. | |
764 | * @dev: Device to put into a low-power state. | |
765 | * | |
766 | * Put the given device into a low-power state during system transition to a | |
767 | * sleep state using the standard ACPI mechanism. Set up system wakeup if | |
768 | * desired, choose the state to put the device into (this checks if system | |
769 | * wakeup is expected to work too), and set the power state of the device. | |
770 | */ | |
771 | int acpi_dev_suspend_late(struct device *dev) | |
772 | { | |
773 | struct acpi_device *adev = acpi_dev_pm_get_node(dev); | |
774 | u32 target_state; | |
775 | bool wakeup; | |
776 | int error; | |
777 | ||
778 | if (!adev) | |
779 | return 0; | |
780 | ||
781 | target_state = acpi_target_system_state(); | |
782 | wakeup = device_may_wakeup(dev); | |
783 | error = __acpi_device_sleep_wake(adev, target_state, wakeup); | |
784 | if (wakeup && error) | |
785 | return error; | |
786 | ||
787 | error = acpi_dev_pm_low_power(dev, adev, target_state); | |
788 | if (error) | |
789 | __acpi_device_sleep_wake(adev, ACPI_STATE_UNKNOWN, false); | |
790 | ||
791 | return error; | |
792 | } | |
793 | EXPORT_SYMBOL_GPL(acpi_dev_suspend_late); | |
794 | ||
795 | /** | |
796 | * acpi_dev_resume_early - Put device into the full-power state using ACPI. | |
797 | * @dev: Device to put into the full-power state. | |
798 | * | |
799 | * Put the given device into the full-power state using the standard ACPI | |
800 | * mechanism during system transition to the working state. Set the power | |
801 | * state of the device to ACPI D0 and disable remote wakeup. | |
802 | */ | |
803 | int acpi_dev_resume_early(struct device *dev) | |
804 | { | |
805 | struct acpi_device *adev = acpi_dev_pm_get_node(dev); | |
806 | int error; | |
807 | ||
808 | if (!adev) | |
809 | return 0; | |
810 | ||
811 | error = acpi_dev_pm_full_power(adev); | |
812 | __acpi_device_sleep_wake(adev, ACPI_STATE_UNKNOWN, false); | |
813 | return error; | |
814 | } | |
815 | EXPORT_SYMBOL_GPL(acpi_dev_resume_early); | |
816 | ||
817 | /** | |
818 | * acpi_subsys_prepare - Prepare device for system transition to a sleep state. | |
819 | * @dev: Device to prepare. | |
820 | */ | |
821 | int acpi_subsys_prepare(struct device *dev) | |
822 | { | |
823 | /* | |
824 | * Follow PCI and resume devices suspended at run time before running | |
825 | * their system suspend callbacks. | |
826 | */ | |
827 | pm_runtime_resume(dev); | |
828 | return pm_generic_prepare(dev); | |
829 | } | |
830 | EXPORT_SYMBOL_GPL(acpi_subsys_prepare); | |
831 | ||
832 | /** | |
833 | * acpi_subsys_suspend_late - Suspend device using ACPI. | |
834 | * @dev: Device to suspend. | |
835 | * | |
836 | * Carry out the generic late suspend procedure for @dev and use ACPI to put | |
837 | * it into a low-power state during system transition into a sleep state. | |
838 | */ | |
839 | int acpi_subsys_suspend_late(struct device *dev) | |
840 | { | |
841 | int ret = pm_generic_suspend_late(dev); | |
842 | return ret ? ret : acpi_dev_suspend_late(dev); | |
843 | } | |
844 | EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late); | |
845 | ||
846 | /** | |
847 | * acpi_subsys_resume_early - Resume device using ACPI. | |
848 | * @dev: Device to Resume. | |
849 | * | |
850 | * Use ACPI to put the given device into the full-power state and carry out the | |
851 | * generic early resume procedure for it during system transition into the | |
852 | * working state. | |
853 | */ | |
854 | int acpi_subsys_resume_early(struct device *dev) | |
855 | { | |
856 | int ret = acpi_dev_resume_early(dev); | |
857 | return ret ? ret : pm_generic_resume_early(dev); | |
858 | } | |
859 | EXPORT_SYMBOL_GPL(acpi_subsys_resume_early); | |
860 | #endif /* CONFIG_PM_SLEEP */ | |
861 | ||
862 | static struct dev_pm_domain acpi_general_pm_domain = { | |
863 | .ops = { | |
864 | #ifdef CONFIG_PM_RUNTIME | |
865 | .runtime_suspend = acpi_subsys_runtime_suspend, | |
866 | .runtime_resume = acpi_subsys_runtime_resume, | |
867 | .runtime_idle = pm_generic_runtime_idle, | |
868 | #endif | |
869 | #ifdef CONFIG_PM_SLEEP | |
870 | .prepare = acpi_subsys_prepare, | |
871 | .suspend_late = acpi_subsys_suspend_late, | |
872 | .resume_early = acpi_subsys_resume_early, | |
873 | .poweroff_late = acpi_subsys_suspend_late, | |
874 | .restore_early = acpi_subsys_resume_early, | |
875 | #endif | |
876 | }, | |
877 | }; | |
878 | ||
879 | /** | |
880 | * acpi_dev_pm_attach - Prepare device for ACPI power management. | |
881 | * @dev: Device to prepare. | |
b88ce2a4 | 882 | * @power_on: Whether or not to power on the device. |
e5cc8ef3 RW |
883 | * |
884 | * If @dev has a valid ACPI handle that has a valid struct acpi_device object | |
885 | * attached to it, install a wakeup notification handler for the device and | |
b88ce2a4 RW |
886 | * add it to the general ACPI PM domain. If @power_on is set, the device will |
887 | * be put into the ACPI D0 state before the function returns. | |
e5cc8ef3 RW |
888 | * |
889 | * This assumes that the @dev's bus type uses generic power management callbacks | |
890 | * (or doesn't use any power management callbacks at all). | |
891 | * | |
892 | * Callers must ensure proper synchronization of this function with power | |
893 | * management callbacks. | |
894 | */ | |
b88ce2a4 | 895 | int acpi_dev_pm_attach(struct device *dev, bool power_on) |
e5cc8ef3 RW |
896 | { |
897 | struct acpi_device *adev = acpi_dev_pm_get_node(dev); | |
898 | ||
899 | if (!adev) | |
900 | return -ENODEV; | |
901 | ||
902 | if (dev->pm_domain) | |
903 | return -EEXIST; | |
904 | ||
905 | acpi_add_pm_notifier(adev, acpi_wakeup_device, dev); | |
906 | dev->pm_domain = &acpi_general_pm_domain; | |
b88ce2a4 RW |
907 | if (power_on) { |
908 | acpi_dev_pm_full_power(adev); | |
909 | __acpi_device_run_wake(adev, false); | |
910 | } | |
e5cc8ef3 RW |
911 | return 0; |
912 | } | |
913 | EXPORT_SYMBOL_GPL(acpi_dev_pm_attach); | |
914 | ||
915 | /** | |
916 | * acpi_dev_pm_detach - Remove ACPI power management from the device. | |
917 | * @dev: Device to take care of. | |
b88ce2a4 | 918 | * @power_off: Whether or not to try to remove power from the device. |
e5cc8ef3 RW |
919 | * |
920 | * Remove the device from the general ACPI PM domain and remove its wakeup | |
b88ce2a4 RW |
921 | * notifier. If @power_off is set, additionally remove power from the device if |
922 | * possible. | |
e5cc8ef3 RW |
923 | * |
924 | * Callers must ensure proper synchronization of this function with power | |
925 | * management callbacks. | |
926 | */ | |
b88ce2a4 | 927 | void acpi_dev_pm_detach(struct device *dev, bool power_off) |
e5cc8ef3 RW |
928 | { |
929 | struct acpi_device *adev = acpi_dev_pm_get_node(dev); | |
930 | ||
931 | if (adev && dev->pm_domain == &acpi_general_pm_domain) { | |
932 | dev->pm_domain = NULL; | |
933 | acpi_remove_pm_notifier(adev, acpi_wakeup_device); | |
b88ce2a4 RW |
934 | if (power_off) { |
935 | /* | |
936 | * If the device's PM QoS resume latency limit or flags | |
937 | * have been exposed to user space, they have to be | |
938 | * hidden at this point, so that they don't affect the | |
939 | * choice of the low-power state to put the device into. | |
940 | */ | |
941 | dev_pm_qos_hide_latency_limit(dev); | |
942 | dev_pm_qos_hide_flags(dev); | |
943 | __acpi_device_run_wake(adev, false); | |
944 | acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0); | |
945 | } | |
e5cc8ef3 RW |
946 | } |
947 | } | |
948 | EXPORT_SYMBOL_GPL(acpi_dev_pm_detach); | |
bc9b6407 RW |
949 | |
950 | /** | |
951 | * acpi_dev_pm_add_dependent - Add physical device depending for PM. | |
952 | * @handle: Handle of ACPI device node. | |
953 | * @depdev: Device depending on that node for PM. | |
954 | */ | |
955 | void acpi_dev_pm_add_dependent(acpi_handle handle, struct device *depdev) | |
956 | { | |
957 | struct acpi_device_physical_node *dep; | |
958 | struct acpi_device *adev; | |
959 | ||
960 | if (!depdev || acpi_bus_get_device(handle, &adev)) | |
961 | return; | |
962 | ||
963 | mutex_lock(&adev->physical_node_lock); | |
964 | ||
965 | list_for_each_entry(dep, &adev->power_dependent, node) | |
966 | if (dep->dev == depdev) | |
967 | goto out; | |
968 | ||
969 | dep = kzalloc(sizeof(*dep), GFP_KERNEL); | |
970 | if (dep) { | |
971 | dep->dev = depdev; | |
972 | list_add_tail(&dep->node, &adev->power_dependent); | |
973 | } | |
974 | ||
975 | out: | |
976 | mutex_unlock(&adev->physical_node_lock); | |
977 | } | |
978 | EXPORT_SYMBOL_GPL(acpi_dev_pm_add_dependent); | |
979 | ||
980 | /** | |
981 | * acpi_dev_pm_remove_dependent - Remove physical device depending for PM. | |
982 | * @handle: Handle of ACPI device node. | |
983 | * @depdev: Device depending on that node for PM. | |
984 | */ | |
985 | void acpi_dev_pm_remove_dependent(acpi_handle handle, struct device *depdev) | |
986 | { | |
987 | struct acpi_device_physical_node *dep; | |
988 | struct acpi_device *adev; | |
989 | ||
990 | if (!depdev || acpi_bus_get_device(handle, &adev)) | |
991 | return; | |
992 | ||
993 | mutex_lock(&adev->physical_node_lock); | |
994 | ||
995 | list_for_each_entry(dep, &adev->power_dependent, node) | |
996 | if (dep->dev == depdev) { | |
997 | list_del(&dep->node); | |
998 | kfree(dep); | |
999 | break; | |
1000 | } | |
1001 | ||
1002 | mutex_unlock(&adev->physical_node_lock); | |
1003 | } | |
1004 | EXPORT_SYMBOL_GPL(acpi_dev_pm_remove_dependent); |