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>
34 static DEFINE_MUTEX(acpi_pm_notifier_lock
);
37 * acpi_add_pm_notifier - Register PM notifier for given ACPI device.
38 * @adev: ACPI device to add the notifier for.
39 * @context: Context information to pass to the notifier routine.
41 * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of
42 * PM wakeup events. For example, wakeup events may be generated for bridges
43 * if one of the devices below the bridge is signaling wakeup, even if the
44 * bridge itself doesn't have a wakeup GPE associated with it.
46 acpi_status
acpi_add_pm_notifier(struct acpi_device
*adev
,
47 acpi_notify_handler handler
, void *context
)
49 acpi_status status
= AE_ALREADY_EXISTS
;
51 mutex_lock(&acpi_pm_notifier_lock
);
53 if (adev
->wakeup
.flags
.notifier_present
)
56 status
= acpi_install_notify_handler(adev
->handle
,
59 if (ACPI_FAILURE(status
))
62 adev
->wakeup
.flags
.notifier_present
= true;
65 mutex_unlock(&acpi_pm_notifier_lock
);
70 * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device.
71 * @adev: ACPI device to remove the notifier from.
73 acpi_status
acpi_remove_pm_notifier(struct acpi_device
*adev
,
74 acpi_notify_handler handler
)
76 acpi_status status
= AE_BAD_PARAMETER
;
78 mutex_lock(&acpi_pm_notifier_lock
);
80 if (!adev
->wakeup
.flags
.notifier_present
)
83 status
= acpi_remove_notify_handler(adev
->handle
,
86 if (ACPI_FAILURE(status
))
89 adev
->wakeup
.flags
.notifier_present
= false;
92 mutex_unlock(&acpi_pm_notifier_lock
);
97 * acpi_device_power_state - Get preferred power state of ACPI device.
98 * @dev: Device whose preferred target power state to return.
99 * @adev: ACPI device node corresponding to @dev.
100 * @target_state: System state to match the resultant device state.
101 * @d_max_in: Deepest low-power state to take into consideration.
102 * @d_min_p: Location to store the upper limit of the allowed states range.
103 * Return value: Preferred power state of the device on success, -ENODEV
104 * (if there's no 'struct acpi_device' for @dev) or -EINVAL on failure
106 * Find the lowest power (highest number) ACPI device power state that the
107 * device can be in while the system is in the state represented by
108 * @target_state. If @d_min_p is set, the highest power (lowest number) device
109 * power state that @dev can be in for the given system sleep state is stored
110 * at the location pointed to by it.
112 * Callers must ensure that @dev and @adev are valid pointers and that @adev
113 * actually corresponds to @dev before using this function.
115 int acpi_device_power_state(struct device
*dev
, struct acpi_device
*adev
,
116 u32 target_state
, int d_max_in
, int *d_min_p
)
118 char acpi_method
[] = "_SxD";
119 unsigned long long d_min
, d_max
;
122 if (d_max_in
< ACPI_STATE_D0
|| d_max_in
> ACPI_STATE_D3
)
125 if (d_max_in
> ACPI_STATE_D3_HOT
) {
126 enum pm_qos_flags_status stat
;
128 stat
= dev_pm_qos_flags(dev
, PM_QOS_FLAG_NO_POWER_OFF
);
129 if (stat
== PM_QOS_FLAGS_ALL
)
130 d_max_in
= ACPI_STATE_D3_HOT
;
133 acpi_method
[2] = '0' + target_state
;
135 * If the sleep state is S0, the lowest limit from ACPI is D3,
136 * but if the device has _S0W, we will use the value from _S0W
137 * as the lowest limit from ACPI. Finally, we will constrain
138 * the lowest limit with the specified one.
140 d_min
= ACPI_STATE_D0
;
141 d_max
= ACPI_STATE_D3
;
144 * If present, _SxD methods return the minimum D-state (highest power
145 * state) we can use for the corresponding S-states. Otherwise, the
146 * minimum D-state is D0 (ACPI 3.x).
148 * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
149 * provided -- that's our fault recovery, we ignore retval.
151 if (target_state
> ACPI_STATE_S0
) {
152 acpi_evaluate_integer(adev
->handle
, acpi_method
, NULL
, &d_min
);
153 wakeup
= device_may_wakeup(dev
) && adev
->wakeup
.flags
.valid
154 && adev
->wakeup
.sleep_state
>= target_state
;
155 } else if (dev_pm_qos_flags(dev
, PM_QOS_FLAG_REMOTE_WAKEUP
) !=
157 wakeup
= adev
->wakeup
.flags
.valid
;
161 * If _PRW says we can wake up the system from the target sleep state,
162 * the D-state returned by _SxD is sufficient for that (we assume a
163 * wakeup-aware driver if wake is set). Still, if _SxW exists
164 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
165 * can wake the system. _S0W may be valid, too.
170 acpi_method
[3] = 'W';
171 status
= acpi_evaluate_integer(adev
->handle
, acpi_method
, NULL
,
173 if (ACPI_FAILURE(status
)) {
174 if (target_state
!= ACPI_STATE_S0
||
175 status
!= AE_NOT_FOUND
)
177 } else if (d_max
< d_min
) {
178 /* Warn the user of the broken DSDT */
179 printk(KERN_WARNING
"ACPI: Wrong value from %s\n",
186 if (d_max_in
< d_min
)
190 /* constrain d_max with specified lowest limit (max number) */
191 if (d_max
> d_max_in
) {
192 for (d_max
= d_max_in
; d_max
> d_min
; d_max
--) {
193 if (adev
->power
.states
[d_max
].flags
.valid
)
199 EXPORT_SYMBOL_GPL(acpi_device_power_state
);
202 * acpi_pm_device_sleep_state - Get preferred power state of ACPI device.
203 * @dev: Device whose preferred target power state to return.
204 * @d_min_p: Location to store the upper limit of the allowed states range.
205 * @d_max_in: Deepest low-power state to take into consideration.
206 * Return value: Preferred power state of the device on success, -ENODEV
207 * (if there's no 'struct acpi_device' for @dev) or -EINVAL on failure
209 * The caller must ensure that @dev is valid before using this function.
211 int acpi_pm_device_sleep_state(struct device
*dev
, int *d_min_p
, int d_max_in
)
213 acpi_handle handle
= DEVICE_ACPI_HANDLE(dev
);
214 struct acpi_device
*adev
;
216 if (!handle
|| ACPI_FAILURE(acpi_bus_get_device(handle
, &adev
))) {
217 dev_dbg(dev
, "ACPI handle without context in %s!\n", __func__
);
221 return acpi_device_power_state(dev
, adev
, acpi_target_system_state(),
224 EXPORT_SYMBOL(acpi_pm_device_sleep_state
);
226 #ifdef CONFIG_PM_RUNTIME
228 * __acpi_device_run_wake - Enable/disable runtime remote wakeup for device.
229 * @adev: ACPI device to enable/disable the remote wakeup for.
230 * @enable: Whether to enable or disable the wakeup functionality.
232 * Enable/disable the GPE associated with @adev so that it can generate
233 * wakeup signals for the device in response to external (remote) events and
234 * enable/disable device wakeup power.
236 * Callers must ensure that @adev is a valid ACPI device node before executing
239 int __acpi_device_run_wake(struct acpi_device
*adev
, bool enable
)
241 struct acpi_device_wakeup
*wakeup
= &adev
->wakeup
;
247 error
= acpi_enable_wakeup_device_power(adev
, ACPI_STATE_S0
);
251 res
= acpi_enable_gpe(wakeup
->gpe_device
, wakeup
->gpe_number
);
252 if (ACPI_FAILURE(res
)) {
253 acpi_disable_wakeup_device_power(adev
);
257 acpi_disable_gpe(wakeup
->gpe_device
, wakeup
->gpe_number
);
258 acpi_disable_wakeup_device_power(adev
);
264 * acpi_pm_device_run_wake - Enable/disable remote wakeup for given device.
265 * @dev: Device to enable/disable the platform to wake up.
266 * @enable: Whether to enable or disable the wakeup functionality.
268 int acpi_pm_device_run_wake(struct device
*phys_dev
, bool enable
)
270 struct acpi_device
*adev
;
273 if (!device_run_wake(phys_dev
))
276 handle
= DEVICE_ACPI_HANDLE(phys_dev
);
277 if (!handle
|| ACPI_FAILURE(acpi_bus_get_device(handle
, &adev
))) {
278 dev_dbg(phys_dev
, "ACPI handle without context in %s!\n",
283 return __acpi_device_run_wake(adev
, enable
);
285 EXPORT_SYMBOL(acpi_pm_device_run_wake
);
286 #endif /* CONFIG_PM_RUNTIME */
288 #ifdef CONFIG_PM_SLEEP
290 * __acpi_device_sleep_wake - Enable or disable device to wake up the system.
291 * @dev: Device to enable/desible to wake up the system.
292 * @target_state: System state the device is supposed to wake up from.
293 * @enable: Whether to enable or disable @dev to wake up the system.
295 int __acpi_device_sleep_wake(struct acpi_device
*adev
, u32 target_state
,
299 acpi_enable_wakeup_device_power(adev
, target_state
) :
300 acpi_disable_wakeup_device_power(adev
);
304 * acpi_pm_device_sleep_wake - Enable or disable device to wake up the system.
305 * @dev: Device to enable/desible to wake up the system from sleep states.
306 * @enable: Whether to enable or disable @dev to wake up the system.
308 int acpi_pm_device_sleep_wake(struct device
*dev
, bool enable
)
311 struct acpi_device
*adev
;
314 if (!device_can_wakeup(dev
))
317 handle
= DEVICE_ACPI_HANDLE(dev
);
318 if (!handle
|| ACPI_FAILURE(acpi_bus_get_device(handle
, &adev
))) {
319 dev_dbg(dev
, "ACPI handle without context in %s!\n", __func__
);
323 error
= __acpi_device_sleep_wake(adev
, acpi_target_system_state(),
326 dev_info(dev
, "System wakeup %s by ACPI\n",
327 enable
? "enabled" : "disabled");
331 #endif /* CONFIG_PM_SLEEP */