2 * drivers/acpi/device_pm.c - ACPI device power management routines.
4 * Copyright (C) 2012, Intel Corp.
5 * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 #include <linux/device.h>
26 #include <linux/export.h>
27 #include <linux/mutex.h>
28 #include <linux/pm_qos.h>
29 #include <linux/pm_runtime.h>
31 #include <acpi/acpi.h>
32 #include <acpi/acpi_bus.h>
33 #include <acpi/acpi_drivers.h>
37 #define _COMPONENT ACPI_POWER_COMPONENT
38 ACPI_MODULE_NAME("device_pm");
40 static DEFINE_MUTEX(acpi_pm_notifier_lock
);
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.
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.
52 acpi_status
acpi_add_pm_notifier(struct acpi_device
*adev
,
53 acpi_notify_handler handler
, void *context
)
55 acpi_status status
= AE_ALREADY_EXISTS
;
57 mutex_lock(&acpi_pm_notifier_lock
);
59 if (adev
->wakeup
.flags
.notifier_present
)
62 status
= acpi_install_notify_handler(adev
->handle
,
65 if (ACPI_FAILURE(status
))
68 adev
->wakeup
.flags
.notifier_present
= true;
71 mutex_unlock(&acpi_pm_notifier_lock
);
76 * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device.
77 * @adev: ACPI device to remove the notifier from.
79 acpi_status
acpi_remove_pm_notifier(struct acpi_device
*adev
,
80 acpi_notify_handler handler
)
82 acpi_status status
= AE_BAD_PARAMETER
;
84 mutex_lock(&acpi_pm_notifier_lock
);
86 if (!adev
->wakeup
.flags
.notifier_present
)
89 status
= acpi_remove_notify_handler(adev
->handle
,
92 if (ACPI_FAILURE(status
))
95 adev
->wakeup
.flags
.notifier_present
= false;
98 mutex_unlock(&acpi_pm_notifier_lock
);
103 * acpi_power_state_string - String representation of ACPI device power state.
104 * @state: ACPI device power state to return the string representation of.
106 const char *acpi_power_state_string(int state
)
115 case ACPI_STATE_D3_HOT
:
117 case ACPI_STATE_D3_COLD
:
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.
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).
133 int acpi_device_get_power(struct acpi_device
*device
, int *state
)
135 int result
= ACPI_STATE_UNKNOWN
;
137 if (!device
|| !state
)
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
;
148 * Get the device's power state from power resources settings and _PSC,
151 if (device
->power
.flags
.power_resources
) {
152 int error
= acpi_power_get_inferred_state(device
, &result
);
156 if (device
->power
.flags
.explicit_get
) {
157 acpi_handle handle
= device
->handle
;
158 unsigned long long psc
;
161 status
= acpi_evaluate_integer(handle
, "_PSC", NULL
, &psc
);
162 if (ACPI_FAILURE(status
))
166 * The power resources settings may indicate a power state
167 * shallower than the actual power state of the device.
169 * Moreover, on systems predating ACPI 4.0, if the device
170 * doesn't depend on any power resources and _PSC returns 3,
171 * that means "power off". We need to maintain compatibility
172 * with those systems.
174 if (psc
> result
&& psc
< ACPI_STATE_D3_COLD
)
176 else if (result
== ACPI_STATE_UNKNOWN
)
177 result
= psc
> ACPI_STATE_D2
? ACPI_STATE_D3_COLD
: psc
;
181 * If we were unsure about the device parent's power state up to this
182 * point, the fact that the device is in D0 implies that the parent has
185 if (device
->parent
&& device
->parent
->power
.state
== ACPI_STATE_UNKNOWN
186 && result
== ACPI_STATE_D0
)
187 device
->parent
->power
.state
= ACPI_STATE_D0
;
192 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device [%s] power state is %s\n",
193 device
->pnp
.bus_id
, acpi_power_state_string(*state
)));
198 static int acpi_dev_pm_explicit_set(struct acpi_device
*adev
, int state
)
200 if (adev
->power
.states
[state
].flags
.explicit_set
) {
201 char method
[5] = { '_', 'P', 'S', '0' + state
, '\0' };
204 status
= acpi_evaluate_object(adev
->handle
, method
, NULL
, NULL
);
205 if (ACPI_FAILURE(status
))
212 * acpi_device_set_power - Set power state of an ACPI device.
213 * @device: Device to set the power state of.
214 * @state: New power state to set.
216 * Callers must ensure that the device is power manageable before using this
219 int acpi_device_set_power(struct acpi_device
*device
, int state
)
222 bool cut_power
= false;
224 if (!device
|| (state
< ACPI_STATE_D0
) || (state
> ACPI_STATE_D3_COLD
))
227 /* Make sure this is a valid target state */
229 if (state
== device
->power
.state
) {
230 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Device is already at %s\n",
231 acpi_power_state_string(state
)));
235 if (!device
->power
.states
[state
].flags
.valid
) {
236 printk(KERN_WARNING PREFIX
"Device does not support %s\n",
237 acpi_power_state_string(state
));
240 if (device
->parent
&& (state
< device
->parent
->power
.state
)) {
241 printk(KERN_WARNING PREFIX
242 "Cannot set device to a higher-powered"
243 " state than parent\n");
247 /* For D3cold we should first transition into D3hot. */
248 if (state
== ACPI_STATE_D3_COLD
249 && device
->power
.states
[ACPI_STATE_D3_COLD
].flags
.os_accessible
) {
250 state
= ACPI_STATE_D3_HOT
;
254 if (state
< device
->power
.state
&& state
!= ACPI_STATE_D0
255 && device
->power
.state
>= ACPI_STATE_D3_HOT
) {
256 printk(KERN_WARNING PREFIX
257 "Cannot transition to non-D0 state from D3\n");
264 * In accordance with the ACPI specification first apply power (via
265 * power resources) and then evalute _PSx.
267 if (device
->power
.flags
.power_resources
) {
268 result
= acpi_power_transition(device
, state
);
272 result
= acpi_dev_pm_explicit_set(device
, state
);
277 device
->power
.state
= state
;
278 state
= ACPI_STATE_D3_COLD
;
279 result
= acpi_power_transition(device
, state
);
284 printk(KERN_WARNING PREFIX
285 "Device [%s] failed to transition to %s\n",
287 acpi_power_state_string(state
));
289 device
->power
.state
= state
;
290 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
291 "Device [%s] transitioned to %s\n",
293 acpi_power_state_string(state
)));
298 EXPORT_SYMBOL(acpi_device_set_power
);
300 int acpi_bus_set_power(acpi_handle handle
, int state
)
302 struct acpi_device
*device
;
305 result
= acpi_bus_get_device(handle
, &device
);
309 if (!device
->flags
.power_manageable
) {
310 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
311 "Device [%s] is not power manageable\n",
312 dev_name(&device
->dev
)));
316 return acpi_device_set_power(device
, state
);
318 EXPORT_SYMBOL(acpi_bus_set_power
);
320 int acpi_bus_init_power(struct acpi_device
*device
)
328 device
->power
.state
= ACPI_STATE_UNKNOWN
;
330 result
= acpi_device_get_power(device
, &state
);
334 if (state
< ACPI_STATE_D3_COLD
&& device
->power
.flags
.power_resources
) {
335 result
= acpi_power_on_resources(device
, state
);
339 result
= acpi_dev_pm_explicit_set(device
, state
);
342 } else if (state
== ACPI_STATE_UNKNOWN
) {
343 /* No power resources and missing _PSC? Try to force D0. */
344 state
= ACPI_STATE_D0
;
345 result
= acpi_dev_pm_explicit_set(device
, state
);
349 device
->power
.state
= state
;
353 int acpi_bus_update_power(acpi_handle handle
, int *state_p
)
355 struct acpi_device
*device
;
359 result
= acpi_bus_get_device(handle
, &device
);
363 result
= acpi_device_get_power(device
, &state
);
367 if (state
== ACPI_STATE_UNKNOWN
)
368 state
= ACPI_STATE_D0
;
370 result
= acpi_device_set_power(device
, state
);
371 if (!result
&& state_p
)
376 EXPORT_SYMBOL_GPL(acpi_bus_update_power
);
378 bool acpi_bus_power_manageable(acpi_handle handle
)
380 struct acpi_device
*device
;
383 result
= acpi_bus_get_device(handle
, &device
);
384 return result
? false : device
->flags
.power_manageable
;
386 EXPORT_SYMBOL(acpi_bus_power_manageable
);
388 bool acpi_bus_can_wakeup(acpi_handle handle
)
390 struct acpi_device
*device
;
393 result
= acpi_bus_get_device(handle
, &device
);
394 return result
? false : device
->wakeup
.flags
.valid
;
396 EXPORT_SYMBOL(acpi_bus_can_wakeup
);
399 * acpi_device_power_state - Get preferred power state of ACPI device.
400 * @dev: Device whose preferred target power state to return.
401 * @adev: ACPI device node corresponding to @dev.
402 * @target_state: System state to match the resultant device state.
403 * @d_max_in: Deepest low-power state to take into consideration.
404 * @d_min_p: Location to store the upper limit of the allowed states range.
405 * Return value: Preferred power state of the device on success, -ENODEV
406 * (if there's no 'struct acpi_device' for @dev) or -EINVAL on failure
408 * Find the lowest power (highest number) ACPI device power state that the
409 * device can be in while the system is in the state represented by
410 * @target_state. If @d_min_p is set, the highest power (lowest number) device
411 * power state that @dev can be in for the given system sleep state is stored
412 * at the location pointed to by it.
414 * Callers must ensure that @dev and @adev are valid pointers and that @adev
415 * actually corresponds to @dev before using this function.
417 int acpi_device_power_state(struct device
*dev
, struct acpi_device
*adev
,
418 u32 target_state
, int d_max_in
, int *d_min_p
)
420 char acpi_method
[] = "_SxD";
421 unsigned long long d_min
, d_max
;
424 if (d_max_in
< ACPI_STATE_D0
|| d_max_in
> ACPI_STATE_D3
)
427 if (d_max_in
> ACPI_STATE_D3_HOT
) {
428 enum pm_qos_flags_status stat
;
430 stat
= dev_pm_qos_flags(dev
, PM_QOS_FLAG_NO_POWER_OFF
);
431 if (stat
== PM_QOS_FLAGS_ALL
)
432 d_max_in
= ACPI_STATE_D3_HOT
;
435 acpi_method
[2] = '0' + target_state
;
437 * If the sleep state is S0, the lowest limit from ACPI is D3,
438 * but if the device has _S0W, we will use the value from _S0W
439 * as the lowest limit from ACPI. Finally, we will constrain
440 * the lowest limit with the specified one.
442 d_min
= ACPI_STATE_D0
;
443 d_max
= ACPI_STATE_D3
;
446 * If present, _SxD methods return the minimum D-state (highest power
447 * state) we can use for the corresponding S-states. Otherwise, the
448 * minimum D-state is D0 (ACPI 3.x).
450 * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
451 * provided -- that's our fault recovery, we ignore retval.
453 if (target_state
> ACPI_STATE_S0
) {
454 acpi_evaluate_integer(adev
->handle
, acpi_method
, NULL
, &d_min
);
455 wakeup
= device_may_wakeup(dev
) && adev
->wakeup
.flags
.valid
456 && adev
->wakeup
.sleep_state
>= target_state
;
457 } else if (dev_pm_qos_flags(dev
, PM_QOS_FLAG_REMOTE_WAKEUP
) !=
459 wakeup
= adev
->wakeup
.flags
.valid
;
463 * If _PRW says we can wake up the system from the target sleep state,
464 * the D-state returned by _SxD is sufficient for that (we assume a
465 * wakeup-aware driver if wake is set). Still, if _SxW exists
466 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
467 * can wake the system. _S0W may be valid, too.
472 acpi_method
[3] = 'W';
473 status
= acpi_evaluate_integer(adev
->handle
, acpi_method
, NULL
,
475 if (ACPI_FAILURE(status
)) {
476 if (target_state
!= ACPI_STATE_S0
||
477 status
!= AE_NOT_FOUND
)
479 } else if (d_max
< d_min
) {
480 /* Warn the user of the broken DSDT */
481 printk(KERN_WARNING
"ACPI: Wrong value from %s\n",
488 if (d_max_in
< d_min
)
492 /* constrain d_max with specified lowest limit (max number) */
493 if (d_max
> d_max_in
) {
494 for (d_max
= d_max_in
; d_max
> d_min
; d_max
--) {
495 if (adev
->power
.states
[d_max
].flags
.valid
)
501 EXPORT_SYMBOL_GPL(acpi_device_power_state
);
504 * acpi_pm_device_sleep_state - Get preferred power state of ACPI device.
505 * @dev: Device whose preferred target power state to return.
506 * @d_min_p: Location to store the upper limit of the allowed states range.
507 * @d_max_in: Deepest low-power state to take into consideration.
508 * Return value: Preferred power state of the device on success, -ENODEV
509 * (if there's no 'struct acpi_device' for @dev) or -EINVAL on failure
511 * The caller must ensure that @dev is valid before using this function.
513 int acpi_pm_device_sleep_state(struct device
*dev
, int *d_min_p
, int d_max_in
)
515 acpi_handle handle
= DEVICE_ACPI_HANDLE(dev
);
516 struct acpi_device
*adev
;
518 if (!handle
|| acpi_bus_get_device(handle
, &adev
)) {
519 dev_dbg(dev
, "ACPI handle without context in %s!\n", __func__
);
523 return acpi_device_power_state(dev
, adev
, acpi_target_system_state(),
526 EXPORT_SYMBOL(acpi_pm_device_sleep_state
);
528 #ifdef CONFIG_PM_RUNTIME
530 * acpi_wakeup_device - Wakeup notification handler for ACPI devices.
531 * @handle: ACPI handle of the device the notification is for.
532 * @event: Type of the signaled event.
533 * @context: Device corresponding to @handle.
535 static void acpi_wakeup_device(acpi_handle handle
, u32 event
, void *context
)
537 struct device
*dev
= context
;
539 if (event
== ACPI_NOTIFY_DEVICE_WAKE
&& dev
) {
540 pm_wakeup_event(dev
, 0);
541 pm_runtime_resume(dev
);
546 * __acpi_device_run_wake - Enable/disable runtime remote wakeup for device.
547 * @adev: ACPI device to enable/disable the remote wakeup for.
548 * @enable: Whether to enable or disable the wakeup functionality.
550 * Enable/disable the GPE associated with @adev so that it can generate
551 * wakeup signals for the device in response to external (remote) events and
552 * enable/disable device wakeup power.
554 * Callers must ensure that @adev is a valid ACPI device node before executing
557 int __acpi_device_run_wake(struct acpi_device
*adev
, bool enable
)
559 struct acpi_device_wakeup
*wakeup
= &adev
->wakeup
;
565 error
= acpi_enable_wakeup_device_power(adev
, ACPI_STATE_S0
);
569 res
= acpi_enable_gpe(wakeup
->gpe_device
, wakeup
->gpe_number
);
570 if (ACPI_FAILURE(res
)) {
571 acpi_disable_wakeup_device_power(adev
);
575 acpi_disable_gpe(wakeup
->gpe_device
, wakeup
->gpe_number
);
576 acpi_disable_wakeup_device_power(adev
);
582 * acpi_pm_device_run_wake - Enable/disable remote wakeup for given device.
583 * @dev: Device to enable/disable the platform to wake up.
584 * @enable: Whether to enable or disable the wakeup functionality.
586 int acpi_pm_device_run_wake(struct device
*phys_dev
, bool enable
)
588 struct acpi_device
*adev
;
591 if (!device_run_wake(phys_dev
))
594 handle
= DEVICE_ACPI_HANDLE(phys_dev
);
595 if (!handle
|| acpi_bus_get_device(handle
, &adev
)) {
596 dev_dbg(phys_dev
, "ACPI handle without context in %s!\n",
601 return __acpi_device_run_wake(adev
, enable
);
603 EXPORT_SYMBOL(acpi_pm_device_run_wake
);
605 static inline void acpi_wakeup_device(acpi_handle handle
, u32 event
,
607 #endif /* CONFIG_PM_RUNTIME */
609 #ifdef CONFIG_PM_SLEEP
611 * __acpi_device_sleep_wake - Enable or disable device to wake up the system.
612 * @dev: Device to enable/desible to wake up the system.
613 * @target_state: System state the device is supposed to wake up from.
614 * @enable: Whether to enable or disable @dev to wake up the system.
616 int __acpi_device_sleep_wake(struct acpi_device
*adev
, u32 target_state
,
620 acpi_enable_wakeup_device_power(adev
, target_state
) :
621 acpi_disable_wakeup_device_power(adev
);
625 * acpi_pm_device_sleep_wake - Enable or disable device to wake up the system.
626 * @dev: Device to enable/desible to wake up the system from sleep states.
627 * @enable: Whether to enable or disable @dev to wake up the system.
629 int acpi_pm_device_sleep_wake(struct device
*dev
, bool enable
)
632 struct acpi_device
*adev
;
635 if (!device_can_wakeup(dev
))
638 handle
= DEVICE_ACPI_HANDLE(dev
);
639 if (!handle
|| acpi_bus_get_device(handle
, &adev
)) {
640 dev_dbg(dev
, "ACPI handle without context in %s!\n", __func__
);
644 error
= __acpi_device_sleep_wake(adev
, acpi_target_system_state(),
647 dev_info(dev
, "System wakeup %s by ACPI\n",
648 enable
? "enabled" : "disabled");
652 #endif /* CONFIG_PM_SLEEP */
655 * acpi_dev_pm_get_node - Get ACPI device node for the given physical device.
656 * @dev: Device to get the ACPI node for.
658 struct acpi_device
*acpi_dev_pm_get_node(struct device
*dev
)
660 acpi_handle handle
= DEVICE_ACPI_HANDLE(dev
);
661 struct acpi_device
*adev
;
663 return handle
&& !acpi_bus_get_device(handle
, &adev
) ? adev
: NULL
;
667 * acpi_dev_pm_low_power - Put ACPI device into a low-power state.
668 * @dev: Device to put into a low-power state.
669 * @adev: ACPI device node corresponding to @dev.
670 * @system_state: System state to choose the device state for.
672 static int acpi_dev_pm_low_power(struct device
*dev
, struct acpi_device
*adev
,
677 if (!acpi_device_power_manageable(adev
))
680 power_state
= acpi_device_power_state(dev
, adev
, system_state
,
681 ACPI_STATE_D3
, NULL
);
682 if (power_state
< ACPI_STATE_D0
|| power_state
> ACPI_STATE_D3
)
685 return acpi_device_set_power(adev
, power_state
);
689 * acpi_dev_pm_full_power - Put ACPI device into the full-power state.
690 * @adev: ACPI device node to put into the full-power state.
692 static int acpi_dev_pm_full_power(struct acpi_device
*adev
)
694 return acpi_device_power_manageable(adev
) ?
695 acpi_device_set_power(adev
, ACPI_STATE_D0
) : 0;
698 #ifdef CONFIG_PM_RUNTIME
700 * acpi_dev_runtime_suspend - Put device into a low-power state using ACPI.
701 * @dev: Device to put into a low-power state.
703 * Put the given device into a runtime low-power state using the standard ACPI
704 * mechanism. Set up remote wakeup if desired, choose the state to put the
705 * device into (this checks if remote wakeup is expected to work too), and set
706 * the power state of the device.
708 int acpi_dev_runtime_suspend(struct device
*dev
)
710 struct acpi_device
*adev
= acpi_dev_pm_get_node(dev
);
717 remote_wakeup
= dev_pm_qos_flags(dev
, PM_QOS_FLAG_REMOTE_WAKEUP
) >
719 error
= __acpi_device_run_wake(adev
, remote_wakeup
);
720 if (remote_wakeup
&& error
)
723 error
= acpi_dev_pm_low_power(dev
, adev
, ACPI_STATE_S0
);
725 __acpi_device_run_wake(adev
, false);
729 EXPORT_SYMBOL_GPL(acpi_dev_runtime_suspend
);
732 * acpi_dev_runtime_resume - Put device into the full-power state using ACPI.
733 * @dev: Device to put into the full-power state.
735 * Put the given device into the full-power state using the standard ACPI
736 * mechanism at run time. Set the power state of the device to ACPI D0 and
737 * disable remote wakeup.
739 int acpi_dev_runtime_resume(struct device
*dev
)
741 struct acpi_device
*adev
= acpi_dev_pm_get_node(dev
);
747 error
= acpi_dev_pm_full_power(adev
);
748 __acpi_device_run_wake(adev
, false);
751 EXPORT_SYMBOL_GPL(acpi_dev_runtime_resume
);
754 * acpi_subsys_runtime_suspend - Suspend device using ACPI.
755 * @dev: Device to suspend.
757 * Carry out the generic runtime suspend procedure for @dev and use ACPI to put
758 * it into a runtime low-power state.
760 int acpi_subsys_runtime_suspend(struct device
*dev
)
762 int ret
= pm_generic_runtime_suspend(dev
);
763 return ret
? ret
: acpi_dev_runtime_suspend(dev
);
765 EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend
);
768 * acpi_subsys_runtime_resume - Resume device using ACPI.
769 * @dev: Device to Resume.
771 * Use ACPI to put the given device into the full-power state and carry out the
772 * generic runtime resume procedure for it.
774 int acpi_subsys_runtime_resume(struct device
*dev
)
776 int ret
= acpi_dev_runtime_resume(dev
);
777 return ret
? ret
: pm_generic_runtime_resume(dev
);
779 EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume
);
780 #endif /* CONFIG_PM_RUNTIME */
782 #ifdef CONFIG_PM_SLEEP
784 * acpi_dev_suspend_late - Put device into a low-power state using ACPI.
785 * @dev: Device to put into a low-power state.
787 * Put the given device into a low-power state during system transition to a
788 * sleep state using the standard ACPI mechanism. Set up system wakeup if
789 * desired, choose the state to put the device into (this checks if system
790 * wakeup is expected to work too), and set the power state of the device.
792 int acpi_dev_suspend_late(struct device
*dev
)
794 struct acpi_device
*adev
= acpi_dev_pm_get_node(dev
);
802 target_state
= acpi_target_system_state();
803 wakeup
= device_may_wakeup(dev
);
804 error
= __acpi_device_sleep_wake(adev
, target_state
, wakeup
);
808 error
= acpi_dev_pm_low_power(dev
, adev
, target_state
);
810 __acpi_device_sleep_wake(adev
, ACPI_STATE_UNKNOWN
, false);
814 EXPORT_SYMBOL_GPL(acpi_dev_suspend_late
);
817 * acpi_dev_resume_early - Put device into the full-power state using ACPI.
818 * @dev: Device to put into the full-power state.
820 * Put the given device into the full-power state using the standard ACPI
821 * mechanism during system transition to the working state. Set the power
822 * state of the device to ACPI D0 and disable remote wakeup.
824 int acpi_dev_resume_early(struct device
*dev
)
826 struct acpi_device
*adev
= acpi_dev_pm_get_node(dev
);
832 error
= acpi_dev_pm_full_power(adev
);
833 __acpi_device_sleep_wake(adev
, ACPI_STATE_UNKNOWN
, false);
836 EXPORT_SYMBOL_GPL(acpi_dev_resume_early
);
839 * acpi_subsys_prepare - Prepare device for system transition to a sleep state.
840 * @dev: Device to prepare.
842 int acpi_subsys_prepare(struct device
*dev
)
845 * Follow PCI and resume devices suspended at run time before running
846 * their system suspend callbacks.
848 pm_runtime_resume(dev
);
849 return pm_generic_prepare(dev
);
851 EXPORT_SYMBOL_GPL(acpi_subsys_prepare
);
854 * acpi_subsys_suspend_late - Suspend device using ACPI.
855 * @dev: Device to suspend.
857 * Carry out the generic late suspend procedure for @dev and use ACPI to put
858 * it into a low-power state during system transition into a sleep state.
860 int acpi_subsys_suspend_late(struct device
*dev
)
862 int ret
= pm_generic_suspend_late(dev
);
863 return ret
? ret
: acpi_dev_suspend_late(dev
);
865 EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late
);
868 * acpi_subsys_resume_early - Resume device using ACPI.
869 * @dev: Device to Resume.
871 * Use ACPI to put the given device into the full-power state and carry out the
872 * generic early resume procedure for it during system transition into the
875 int acpi_subsys_resume_early(struct device
*dev
)
877 int ret
= acpi_dev_resume_early(dev
);
878 return ret
? ret
: pm_generic_resume_early(dev
);
880 EXPORT_SYMBOL_GPL(acpi_subsys_resume_early
);
881 #endif /* CONFIG_PM_SLEEP */
883 static struct dev_pm_domain acpi_general_pm_domain
= {
885 #ifdef CONFIG_PM_RUNTIME
886 .runtime_suspend
= acpi_subsys_runtime_suspend
,
887 .runtime_resume
= acpi_subsys_runtime_resume
,
888 .runtime_idle
= pm_generic_runtime_idle
,
890 #ifdef CONFIG_PM_SLEEP
891 .prepare
= acpi_subsys_prepare
,
892 .suspend_late
= acpi_subsys_suspend_late
,
893 .resume_early
= acpi_subsys_resume_early
,
894 .poweroff_late
= acpi_subsys_suspend_late
,
895 .restore_early
= acpi_subsys_resume_early
,
901 * acpi_dev_pm_attach - Prepare device for ACPI power management.
902 * @dev: Device to prepare.
903 * @power_on: Whether or not to power on the device.
905 * If @dev has a valid ACPI handle that has a valid struct acpi_device object
906 * attached to it, install a wakeup notification handler for the device and
907 * add it to the general ACPI PM domain. If @power_on is set, the device will
908 * be put into the ACPI D0 state before the function returns.
910 * This assumes that the @dev's bus type uses generic power management callbacks
911 * (or doesn't use any power management callbacks at all).
913 * Callers must ensure proper synchronization of this function with power
914 * management callbacks.
916 int acpi_dev_pm_attach(struct device
*dev
, bool power_on
)
918 struct acpi_device
*adev
= acpi_dev_pm_get_node(dev
);
926 acpi_add_pm_notifier(adev
, acpi_wakeup_device
, dev
);
927 dev
->pm_domain
= &acpi_general_pm_domain
;
929 acpi_dev_pm_full_power(adev
);
930 __acpi_device_run_wake(adev
, false);
934 EXPORT_SYMBOL_GPL(acpi_dev_pm_attach
);
937 * acpi_dev_pm_detach - Remove ACPI power management from the device.
938 * @dev: Device to take care of.
939 * @power_off: Whether or not to try to remove power from the device.
941 * Remove the device from the general ACPI PM domain and remove its wakeup
942 * notifier. If @power_off is set, additionally remove power from the device if
945 * Callers must ensure proper synchronization of this function with power
946 * management callbacks.
948 void acpi_dev_pm_detach(struct device
*dev
, bool power_off
)
950 struct acpi_device
*adev
= acpi_dev_pm_get_node(dev
);
952 if (adev
&& dev
->pm_domain
== &acpi_general_pm_domain
) {
953 dev
->pm_domain
= NULL
;
954 acpi_remove_pm_notifier(adev
, acpi_wakeup_device
);
957 * If the device's PM QoS resume latency limit or flags
958 * have been exposed to user space, they have to be
959 * hidden at this point, so that they don't affect the
960 * choice of the low-power state to put the device into.
962 dev_pm_qos_hide_latency_limit(dev
);
963 dev_pm_qos_hide_flags(dev
);
964 __acpi_device_run_wake(adev
, false);
965 acpi_dev_pm_low_power(dev
, adev
, ACPI_STATE_S0
);
969 EXPORT_SYMBOL_GPL(acpi_dev_pm_detach
);
972 * acpi_dev_pm_add_dependent - Add physical device depending for PM.
973 * @handle: Handle of ACPI device node.
974 * @depdev: Device depending on that node for PM.
976 void acpi_dev_pm_add_dependent(acpi_handle handle
, struct device
*depdev
)
978 struct acpi_device_physical_node
*dep
;
979 struct acpi_device
*adev
;
981 if (!depdev
|| acpi_bus_get_device(handle
, &adev
))
984 mutex_lock(&adev
->physical_node_lock
);
986 list_for_each_entry(dep
, &adev
->power_dependent
, node
)
987 if (dep
->dev
== depdev
)
990 dep
= kzalloc(sizeof(*dep
), GFP_KERNEL
);
993 list_add_tail(&dep
->node
, &adev
->power_dependent
);
997 mutex_unlock(&adev
->physical_node_lock
);
999 EXPORT_SYMBOL_GPL(acpi_dev_pm_add_dependent
);
1002 * acpi_dev_pm_remove_dependent - Remove physical device depending for PM.
1003 * @handle: Handle of ACPI device node.
1004 * @depdev: Device depending on that node for PM.
1006 void acpi_dev_pm_remove_dependent(acpi_handle handle
, struct device
*depdev
)
1008 struct acpi_device_physical_node
*dep
;
1009 struct acpi_device
*adev
;
1011 if (!depdev
|| acpi_bus_get_device(handle
, &adev
))
1014 mutex_lock(&adev
->physical_node_lock
);
1016 list_for_each_entry(dep
, &adev
->power_dependent
, node
)
1017 if (dep
->dev
== depdev
) {
1018 list_del(&dep
->node
);
1023 mutex_unlock(&adev
->physical_node_lock
);
1025 EXPORT_SYMBOL_GPL(acpi_dev_pm_remove_dependent
);