blob: 8bca7465c78d50fd750e27905e82304aef1cc04f [file] [log] [blame]
Rafael J. Wysockiec2cd812012-11-02 01:40:09 +01001/*
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>
Rafael J. Wysocki86b38322012-11-02 01:40:18 +010026#include <linux/export.h>
Rafael J. Wysockiec2cd812012-11-02 01:40:09 +010027#include <linux/mutex.h>
Rafael J. Wysocki86b38322012-11-02 01:40:18 +010028#include <linux/pm_qos.h>
Rafael J. Wysockicd7bd022012-11-02 01:40:28 +010029#include <linux/pm_runtime.h>
Rafael J. Wysockiec2cd812012-11-02 01:40:09 +010030
31#include <acpi/acpi.h>
32#include <acpi/acpi_bus.h>
Rafael J. Wysocki9ce4e602013-01-17 14:11:08 +010033#include <acpi/acpi_drivers.h>
34
35#include "internal.h"
36
37#define _COMPONENT ACPI_POWER_COMPONENT
38ACPI_MODULE_NAME("device_pm");
Rafael J. Wysockiec2cd812012-11-02 01:40:09 +010039
40static 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 */
52acpi_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 */
79acpi_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}
Rafael J. Wysocki86b38322012-11-02 01:40:18 +0100101
102/**
Rafael J. Wysocki9ce4e602013-01-17 14:11:08 +0100103 * acpi_power_state_string - String representation of ACPI device power state.
104 * @state: ACPI device power state to return the string representation of.
105 */
106const 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:
118 return "D3";
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 */
133int 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
189/**
190 * acpi_device_set_power - Set power state of an ACPI device.
191 * @device: Device to set the power state of.
192 * @state: New power state to set.
193 *
194 * Callers must ensure that the device is power manageable before using this
195 * function.
196 */
197int acpi_device_set_power(struct acpi_device *device, int state)
198{
199 int result = 0;
200 acpi_status status = AE_OK;
201 char object_name[5] = { '_', 'P', 'S', '0' + state, '\0' };
202 bool cut_power = false;
203
204 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
205 return -EINVAL;
206
207 /* Make sure this is a valid target state */
208
209 if (state == device->power.state) {
210 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device is already at %s\n",
211 acpi_power_state_string(state)));
212 return 0;
213 }
214
215 if (!device->power.states[state].flags.valid) {
216 printk(KERN_WARNING PREFIX "Device does not support %s\n",
217 acpi_power_state_string(state));
218 return -ENODEV;
219 }
220 if (device->parent && (state < device->parent->power.state)) {
221 printk(KERN_WARNING PREFIX
222 "Cannot set device to a higher-powered"
223 " state than parent\n");
224 return -ENODEV;
225 }
226
227 /* For D3cold we should first transition into D3hot. */
228 if (state == ACPI_STATE_D3_COLD
229 && device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible) {
230 state = ACPI_STATE_D3_HOT;
231 object_name[3] = '3';
232 cut_power = true;
233 }
234
235 /*
236 * Transition Power
237 * ----------------
238 * On transitions to a high-powered state we first apply power (via
239 * power resources) then evalute _PSx. Conversly for transitions to
240 * a lower-powered state.
241 */
242 if (state < device->power.state) {
243 if (device->power.state >= ACPI_STATE_D3_HOT &&
244 state != ACPI_STATE_D0) {
245 printk(KERN_WARNING PREFIX
246 "Cannot transition to non-D0 state from D3\n");
247 return -ENODEV;
248 }
249 if (device->power.flags.power_resources) {
250 result = acpi_power_transition(device, state);
251 if (result)
252 goto end;
253 }
254 if (device->power.states[state].flags.explicit_set) {
255 status = acpi_evaluate_object(device->handle,
256 object_name, NULL, NULL);
257 if (ACPI_FAILURE(status)) {
258 result = -ENODEV;
259 goto end;
260 }
261 }
262 } else {
263 if (device->power.states[state].flags.explicit_set) {
264 status = acpi_evaluate_object(device->handle,
265 object_name, NULL, NULL);
266 if (ACPI_FAILURE(status)) {
267 result = -ENODEV;
268 goto end;
269 }
270 }
271 if (device->power.flags.power_resources) {
272 result = acpi_power_transition(device, state);
273 if (result)
274 goto end;
275 }
276 }
277
278 if (cut_power)
279 result = acpi_power_transition(device, ACPI_STATE_D3_COLD);
280
281 end:
282 if (result)
283 printk(KERN_WARNING PREFIX
284 "Device [%s] failed to transition to %s\n",
285 device->pnp.bus_id,
286 acpi_power_state_string(state));
287 else {
288 device->power.state = state;
289 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
290 "Device [%s] transitioned to %s\n",
291 device->pnp.bus_id,
292 acpi_power_state_string(state)));
293 }
294
295 return result;
296}
297EXPORT_SYMBOL(acpi_device_set_power);
298
299int acpi_bus_set_power(acpi_handle handle, int state)
300{
301 struct acpi_device *device;
302 int result;
303
304 result = acpi_bus_get_device(handle, &device);
305 if (result)
306 return result;
307
308 if (!device->flags.power_manageable) {
309 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
310 "Device [%s] is not power manageable\n",
311 dev_name(&device->dev)));
312 return -ENODEV;
313 }
314
315 return acpi_device_set_power(device, state);
316}
317EXPORT_SYMBOL(acpi_bus_set_power);
318
319int acpi_bus_init_power(struct acpi_device *device)
320{
321 int state;
322 int result;
323
324 if (!device)
325 return -EINVAL;
326
327 device->power.state = ACPI_STATE_UNKNOWN;
328
329 result = acpi_device_get_power(device, &state);
330 if (result)
331 return result;
332
333 if (device->power.flags.power_resources)
334 result = acpi_power_on_resources(device, state);
335
336 if (!result)
337 device->power.state = state;
338
339 return result;
340}
341
342int acpi_bus_update_power(acpi_handle handle, int *state_p)
343{
344 struct acpi_device *device;
345 int state;
346 int result;
347
348 result = acpi_bus_get_device(handle, &device);
349 if (result)
350 return result;
351
352 result = acpi_device_get_power(device, &state);
353 if (result)
354 return result;
355
356 result = acpi_device_set_power(device, state);
357 if (!result && state_p)
358 *state_p = state;
359
360 return result;
361}
362EXPORT_SYMBOL_GPL(acpi_bus_update_power);
363
364bool acpi_bus_power_manageable(acpi_handle handle)
365{
366 struct acpi_device *device;
367 int result;
368
369 result = acpi_bus_get_device(handle, &device);
370 return result ? false : device->flags.power_manageable;
371}
372EXPORT_SYMBOL(acpi_bus_power_manageable);
373
374bool acpi_bus_can_wakeup(acpi_handle handle)
375{
376 struct acpi_device *device;
377 int result;
378
379 result = acpi_bus_get_device(handle, &device);
380 return result ? false : device->wakeup.flags.valid;
381}
382EXPORT_SYMBOL(acpi_bus_can_wakeup);
383
384/**
Rafael J. Wysocki86b38322012-11-02 01:40:18 +0100385 * acpi_device_power_state - Get preferred power state of ACPI device.
386 * @dev: Device whose preferred target power state to return.
387 * @adev: ACPI device node corresponding to @dev.
388 * @target_state: System state to match the resultant device state.
389 * @d_max_in: Deepest low-power state to take into consideration.
390 * @d_min_p: Location to store the upper limit of the allowed states range.
391 * Return value: Preferred power state of the device on success, -ENODEV
392 * (if there's no 'struct acpi_device' for @dev) or -EINVAL on failure
393 *
394 * Find the lowest power (highest number) ACPI device power state that the
395 * device can be in while the system is in the state represented by
396 * @target_state. If @d_min_p is set, the highest power (lowest number) device
397 * power state that @dev can be in for the given system sleep state is stored
398 * at the location pointed to by it.
399 *
400 * Callers must ensure that @dev and @adev are valid pointers and that @adev
401 * actually corresponds to @dev before using this function.
402 */
403int acpi_device_power_state(struct device *dev, struct acpi_device *adev,
404 u32 target_state, int d_max_in, int *d_min_p)
405{
406 char acpi_method[] = "_SxD";
407 unsigned long long d_min, d_max;
408 bool wakeup = false;
409
410 if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3)
411 return -EINVAL;
412
413 if (d_max_in > ACPI_STATE_D3_HOT) {
414 enum pm_qos_flags_status stat;
415
416 stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF);
417 if (stat == PM_QOS_FLAGS_ALL)
418 d_max_in = ACPI_STATE_D3_HOT;
419 }
420
421 acpi_method[2] = '0' + target_state;
422 /*
423 * If the sleep state is S0, the lowest limit from ACPI is D3,
424 * but if the device has _S0W, we will use the value from _S0W
425 * as the lowest limit from ACPI. Finally, we will constrain
426 * the lowest limit with the specified one.
427 */
428 d_min = ACPI_STATE_D0;
429 d_max = ACPI_STATE_D3;
430
431 /*
432 * If present, _SxD methods return the minimum D-state (highest power
433 * state) we can use for the corresponding S-states. Otherwise, the
434 * minimum D-state is D0 (ACPI 3.x).
435 *
436 * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
437 * provided -- that's our fault recovery, we ignore retval.
438 */
439 if (target_state > ACPI_STATE_S0) {
440 acpi_evaluate_integer(adev->handle, acpi_method, NULL, &d_min);
441 wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid
442 && adev->wakeup.sleep_state >= target_state;
443 } else if (dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) !=
444 PM_QOS_FLAGS_NONE) {
445 wakeup = adev->wakeup.flags.valid;
446 }
447
448 /*
449 * If _PRW says we can wake up the system from the target sleep state,
450 * the D-state returned by _SxD is sufficient for that (we assume a
451 * wakeup-aware driver if wake is set). Still, if _SxW exists
452 * (ACPI 3.x), it should return the maximum (lowest power) D-state that
453 * can wake the system. _S0W may be valid, too.
454 */
455 if (wakeup) {
456 acpi_status status;
457
458 acpi_method[3] = 'W';
459 status = acpi_evaluate_integer(adev->handle, acpi_method, NULL,
460 &d_max);
461 if (ACPI_FAILURE(status)) {
462 if (target_state != ACPI_STATE_S0 ||
463 status != AE_NOT_FOUND)
464 d_max = d_min;
465 } else if (d_max < d_min) {
466 /* Warn the user of the broken DSDT */
467 printk(KERN_WARNING "ACPI: Wrong value from %s\n",
468 acpi_method);
469 /* Sanitize it */
470 d_min = d_max;
471 }
472 }
473
474 if (d_max_in < d_min)
475 return -EINVAL;
476 if (d_min_p)
477 *d_min_p = d_min;
478 /* constrain d_max with specified lowest limit (max number) */
479 if (d_max > d_max_in) {
480 for (d_max = d_max_in; d_max > d_min; d_max--) {
481 if (adev->power.states[d_max].flags.valid)
482 break;
483 }
484 }
485 return d_max;
486}
487EXPORT_SYMBOL_GPL(acpi_device_power_state);
Rafael J. Wysockicd7bd022012-11-02 01:40:28 +0100488
Rafael J. Wysockia6ae7592012-11-02 01:40:53 +0100489/**
490 * acpi_pm_device_sleep_state - Get preferred power state of ACPI device.
491 * @dev: Device whose preferred target power state to return.
492 * @d_min_p: Location to store the upper limit of the allowed states range.
493 * @d_max_in: Deepest low-power state to take into consideration.
494 * Return value: Preferred power state of the device on success, -ENODEV
495 * (if there's no 'struct acpi_device' for @dev) or -EINVAL on failure
496 *
497 * The caller must ensure that @dev is valid before using this function.
498 */
499int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)
500{
501 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
502 struct acpi_device *adev;
503
504 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
505 dev_dbg(dev, "ACPI handle without context in %s!\n", __func__);
506 return -ENODEV;
507 }
508
509 return acpi_device_power_state(dev, adev, acpi_target_system_state(),
510 d_max_in, d_min_p);
511}
512EXPORT_SYMBOL(acpi_pm_device_sleep_state);
513
Rafael J. Wysockicd7bd022012-11-02 01:40:28 +0100514#ifdef CONFIG_PM_RUNTIME
515/**
Rafael J. Wysockie5cc8ef2012-11-02 01:41:01 +0100516 * acpi_wakeup_device - Wakeup notification handler for ACPI devices.
517 * @handle: ACPI handle of the device the notification is for.
518 * @event: Type of the signaled event.
519 * @context: Device corresponding to @handle.
520 */
521static void acpi_wakeup_device(acpi_handle handle, u32 event, void *context)
522{
523 struct device *dev = context;
524
525 if (event == ACPI_NOTIFY_DEVICE_WAKE && dev) {
526 pm_wakeup_event(dev, 0);
527 pm_runtime_resume(dev);
528 }
529}
530
531/**
Rafael J. Wysockidee83702012-11-02 01:40:36 +0100532 * __acpi_device_run_wake - Enable/disable runtime remote wakeup for device.
533 * @adev: ACPI device to enable/disable the remote wakeup for.
Rafael J. Wysockicd7bd022012-11-02 01:40:28 +0100534 * @enable: Whether to enable or disable the wakeup functionality.
535 *
Rafael J. Wysockidee83702012-11-02 01:40:36 +0100536 * Enable/disable the GPE associated with @adev so that it can generate
537 * wakeup signals for the device in response to external (remote) events and
538 * enable/disable device wakeup power.
539 *
540 * Callers must ensure that @adev is a valid ACPI device node before executing
541 * this function.
542 */
543int __acpi_device_run_wake(struct acpi_device *adev, bool enable)
544{
545 struct acpi_device_wakeup *wakeup = &adev->wakeup;
546
547 if (enable) {
548 acpi_status res;
549 int error;
550
551 error = acpi_enable_wakeup_device_power(adev, ACPI_STATE_S0);
552 if (error)
553 return error;
554
555 res = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);
556 if (ACPI_FAILURE(res)) {
557 acpi_disable_wakeup_device_power(adev);
558 return -EIO;
559 }
560 } else {
561 acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
562 acpi_disable_wakeup_device_power(adev);
563 }
564 return 0;
565}
566
567/**
568 * acpi_pm_device_run_wake - Enable/disable remote wakeup for given device.
569 * @dev: Device to enable/disable the platform to wake up.
570 * @enable: Whether to enable or disable the wakeup functionality.
Rafael J. Wysockicd7bd022012-11-02 01:40:28 +0100571 */
572int acpi_pm_device_run_wake(struct device *phys_dev, bool enable)
573{
Rafael J. Wysockidee83702012-11-02 01:40:36 +0100574 struct acpi_device *adev;
Rafael J. Wysockicd7bd022012-11-02 01:40:28 +0100575 acpi_handle handle;
576
577 if (!device_run_wake(phys_dev))
578 return -EINVAL;
579
580 handle = DEVICE_ACPI_HANDLE(phys_dev);
Rafael J. Wysockidee83702012-11-02 01:40:36 +0100581 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
582 dev_dbg(phys_dev, "ACPI handle without context in %s!\n",
Rafael J. Wysockicd7bd022012-11-02 01:40:28 +0100583 __func__);
584 return -ENODEV;
585 }
586
Rafael J. Wysockidee83702012-11-02 01:40:36 +0100587 return __acpi_device_run_wake(adev, enable);
Rafael J. Wysockicd7bd022012-11-02 01:40:28 +0100588}
589EXPORT_SYMBOL(acpi_pm_device_run_wake);
Rafael J. Wysockie5cc8ef2012-11-02 01:41:01 +0100590#else
591static inline void acpi_wakeup_device(acpi_handle handle, u32 event,
592 void *context) {}
Rafael J. Wysockicd7bd022012-11-02 01:40:28 +0100593#endif /* CONFIG_PM_RUNTIME */
Rafael J. Wysockidee83702012-11-02 01:40:36 +0100594
595 #ifdef CONFIG_PM_SLEEP
596/**
597 * __acpi_device_sleep_wake - Enable or disable device to wake up the system.
598 * @dev: Device to enable/desible to wake up the system.
599 * @target_state: System state the device is supposed to wake up from.
600 * @enable: Whether to enable or disable @dev to wake up the system.
601 */
602int __acpi_device_sleep_wake(struct acpi_device *adev, u32 target_state,
603 bool enable)
604{
605 return enable ?
606 acpi_enable_wakeup_device_power(adev, target_state) :
607 acpi_disable_wakeup_device_power(adev);
608}
Rafael J. Wysockia6ae7592012-11-02 01:40:53 +0100609
610/**
611 * acpi_pm_device_sleep_wake - Enable or disable device to wake up the system.
612 * @dev: Device to enable/desible to wake up the system from sleep states.
613 * @enable: Whether to enable or disable @dev to wake up the system.
614 */
615int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
616{
617 acpi_handle handle;
618 struct acpi_device *adev;
619 int error;
620
621 if (!device_can_wakeup(dev))
622 return -EINVAL;
623
624 handle = DEVICE_ACPI_HANDLE(dev);
625 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
626 dev_dbg(dev, "ACPI handle without context in %s!\n", __func__);
627 return -ENODEV;
628 }
629
630 error = __acpi_device_sleep_wake(adev, acpi_target_system_state(),
631 enable);
632 if (!error)
633 dev_info(dev, "System wakeup %s by ACPI\n",
634 enable ? "enabled" : "disabled");
635
636 return error;
637}
Rafael J. Wysockidee83702012-11-02 01:40:36 +0100638#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockie5cc8ef2012-11-02 01:41:01 +0100639
640/**
641 * acpi_dev_pm_get_node - Get ACPI device node for the given physical device.
642 * @dev: Device to get the ACPI node for.
643 */
Rafael J. Wysockid2e5f0c2012-12-23 00:02:44 +0100644struct acpi_device *acpi_dev_pm_get_node(struct device *dev)
Rafael J. Wysockie5cc8ef2012-11-02 01:41:01 +0100645{
646 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
647 struct acpi_device *adev;
648
Rafael J. Wysocki5cc36c72012-12-16 23:28:53 +0100649 return handle && !acpi_bus_get_device(handle, &adev) ? adev : NULL;
Rafael J. Wysockie5cc8ef2012-11-02 01:41:01 +0100650}
651
652/**
653 * acpi_dev_pm_low_power - Put ACPI device into a low-power state.
654 * @dev: Device to put into a low-power state.
655 * @adev: ACPI device node corresponding to @dev.
656 * @system_state: System state to choose the device state for.
657 */
658static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev,
659 u32 system_state)
660{
661 int power_state;
662
663 if (!acpi_device_power_manageable(adev))
664 return 0;
665
666 power_state = acpi_device_power_state(dev, adev, system_state,
667 ACPI_STATE_D3, NULL);
668 if (power_state < ACPI_STATE_D0 || power_state > ACPI_STATE_D3)
669 return -EIO;
670
671 return acpi_device_set_power(adev, power_state);
672}
673
674/**
675 * acpi_dev_pm_full_power - Put ACPI device into the full-power state.
676 * @adev: ACPI device node to put into the full-power state.
677 */
678static int acpi_dev_pm_full_power(struct acpi_device *adev)
679{
680 return acpi_device_power_manageable(adev) ?
681 acpi_device_set_power(adev, ACPI_STATE_D0) : 0;
682}
683
684#ifdef CONFIG_PM_RUNTIME
685/**
686 * acpi_dev_runtime_suspend - Put device into a low-power state using ACPI.
687 * @dev: Device to put into a low-power state.
688 *
689 * Put the given device into a runtime low-power state using the standard ACPI
690 * mechanism. Set up remote wakeup if desired, choose the state to put the
691 * device into (this checks if remote wakeup is expected to work too), and set
692 * the power state of the device.
693 */
694int acpi_dev_runtime_suspend(struct device *dev)
695{
696 struct acpi_device *adev = acpi_dev_pm_get_node(dev);
697 bool remote_wakeup;
698 int error;
699
700 if (!adev)
701 return 0;
702
703 remote_wakeup = dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) >
704 PM_QOS_FLAGS_NONE;
705 error = __acpi_device_run_wake(adev, remote_wakeup);
706 if (remote_wakeup && error)
707 return -EAGAIN;
708
709 error = acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
710 if (error)
711 __acpi_device_run_wake(adev, false);
712
713 return error;
714}
715EXPORT_SYMBOL_GPL(acpi_dev_runtime_suspend);
716
717/**
718 * acpi_dev_runtime_resume - Put device into the full-power state using ACPI.
719 * @dev: Device to put into the full-power state.
720 *
721 * Put the given device into the full-power state using the standard ACPI
722 * mechanism at run time. Set the power state of the device to ACPI D0 and
723 * disable remote wakeup.
724 */
725int acpi_dev_runtime_resume(struct device *dev)
726{
727 struct acpi_device *adev = acpi_dev_pm_get_node(dev);
728 int error;
729
730 if (!adev)
731 return 0;
732
733 error = acpi_dev_pm_full_power(adev);
734 __acpi_device_run_wake(adev, false);
735 return error;
736}
737EXPORT_SYMBOL_GPL(acpi_dev_runtime_resume);
738
739/**
740 * acpi_subsys_runtime_suspend - Suspend device using ACPI.
741 * @dev: Device to suspend.
742 *
743 * Carry out the generic runtime suspend procedure for @dev and use ACPI to put
744 * it into a runtime low-power state.
745 */
746int acpi_subsys_runtime_suspend(struct device *dev)
747{
748 int ret = pm_generic_runtime_suspend(dev);
749 return ret ? ret : acpi_dev_runtime_suspend(dev);
750}
751EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend);
752
753/**
754 * acpi_subsys_runtime_resume - Resume device using ACPI.
755 * @dev: Device to Resume.
756 *
757 * Use ACPI to put the given device into the full-power state and carry out the
758 * generic runtime resume procedure for it.
759 */
760int acpi_subsys_runtime_resume(struct device *dev)
761{
762 int ret = acpi_dev_runtime_resume(dev);
763 return ret ? ret : pm_generic_runtime_resume(dev);
764}
765EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume);
766#endif /* CONFIG_PM_RUNTIME */
767
768#ifdef CONFIG_PM_SLEEP
769/**
770 * acpi_dev_suspend_late - Put device into a low-power state using ACPI.
771 * @dev: Device to put into a low-power state.
772 *
773 * Put the given device into a low-power state during system transition to a
774 * sleep state using the standard ACPI mechanism. Set up system wakeup if
775 * desired, choose the state to put the device into (this checks if system
776 * wakeup is expected to work too), and set the power state of the device.
777 */
778int acpi_dev_suspend_late(struct device *dev)
779{
780 struct acpi_device *adev = acpi_dev_pm_get_node(dev);
781 u32 target_state;
782 bool wakeup;
783 int error;
784
785 if (!adev)
786 return 0;
787
788 target_state = acpi_target_system_state();
789 wakeup = device_may_wakeup(dev);
790 error = __acpi_device_sleep_wake(adev, target_state, wakeup);
791 if (wakeup && error)
792 return error;
793
794 error = acpi_dev_pm_low_power(dev, adev, target_state);
795 if (error)
796 __acpi_device_sleep_wake(adev, ACPI_STATE_UNKNOWN, false);
797
798 return error;
799}
800EXPORT_SYMBOL_GPL(acpi_dev_suspend_late);
801
802/**
803 * acpi_dev_resume_early - Put device into the full-power state using ACPI.
804 * @dev: Device to put into the full-power state.
805 *
806 * Put the given device into the full-power state using the standard ACPI
807 * mechanism during system transition to the working state. Set the power
808 * state of the device to ACPI D0 and disable remote wakeup.
809 */
810int acpi_dev_resume_early(struct device *dev)
811{
812 struct acpi_device *adev = acpi_dev_pm_get_node(dev);
813 int error;
814
815 if (!adev)
816 return 0;
817
818 error = acpi_dev_pm_full_power(adev);
819 __acpi_device_sleep_wake(adev, ACPI_STATE_UNKNOWN, false);
820 return error;
821}
822EXPORT_SYMBOL_GPL(acpi_dev_resume_early);
823
824/**
825 * acpi_subsys_prepare - Prepare device for system transition to a sleep state.
826 * @dev: Device to prepare.
827 */
828int acpi_subsys_prepare(struct device *dev)
829{
830 /*
831 * Follow PCI and resume devices suspended at run time before running
832 * their system suspend callbacks.
833 */
834 pm_runtime_resume(dev);
835 return pm_generic_prepare(dev);
836}
837EXPORT_SYMBOL_GPL(acpi_subsys_prepare);
838
839/**
840 * acpi_subsys_suspend_late - Suspend device using ACPI.
841 * @dev: Device to suspend.
842 *
843 * Carry out the generic late suspend procedure for @dev and use ACPI to put
844 * it into a low-power state during system transition into a sleep state.
845 */
846int acpi_subsys_suspend_late(struct device *dev)
847{
848 int ret = pm_generic_suspend_late(dev);
849 return ret ? ret : acpi_dev_suspend_late(dev);
850}
851EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late);
852
853/**
854 * acpi_subsys_resume_early - Resume device using ACPI.
855 * @dev: Device to Resume.
856 *
857 * Use ACPI to put the given device into the full-power state and carry out the
858 * generic early resume procedure for it during system transition into the
859 * working state.
860 */
861int acpi_subsys_resume_early(struct device *dev)
862{
863 int ret = acpi_dev_resume_early(dev);
864 return ret ? ret : pm_generic_resume_early(dev);
865}
866EXPORT_SYMBOL_GPL(acpi_subsys_resume_early);
867#endif /* CONFIG_PM_SLEEP */
868
869static struct dev_pm_domain acpi_general_pm_domain = {
870 .ops = {
871#ifdef CONFIG_PM_RUNTIME
872 .runtime_suspend = acpi_subsys_runtime_suspend,
873 .runtime_resume = acpi_subsys_runtime_resume,
874 .runtime_idle = pm_generic_runtime_idle,
875#endif
876#ifdef CONFIG_PM_SLEEP
877 .prepare = acpi_subsys_prepare,
878 .suspend_late = acpi_subsys_suspend_late,
879 .resume_early = acpi_subsys_resume_early,
880 .poweroff_late = acpi_subsys_suspend_late,
881 .restore_early = acpi_subsys_resume_early,
882#endif
883 },
884};
885
886/**
887 * acpi_dev_pm_attach - Prepare device for ACPI power management.
888 * @dev: Device to prepare.
Rafael J. Wysockib88ce2a2012-11-26 10:03:06 +0100889 * @power_on: Whether or not to power on the device.
Rafael J. Wysockie5cc8ef2012-11-02 01:41:01 +0100890 *
891 * If @dev has a valid ACPI handle that has a valid struct acpi_device object
892 * attached to it, install a wakeup notification handler for the device and
Rafael J. Wysockib88ce2a2012-11-26 10:03:06 +0100893 * add it to the general ACPI PM domain. If @power_on is set, the device will
894 * be put into the ACPI D0 state before the function returns.
Rafael J. Wysockie5cc8ef2012-11-02 01:41:01 +0100895 *
896 * This assumes that the @dev's bus type uses generic power management callbacks
897 * (or doesn't use any power management callbacks at all).
898 *
899 * Callers must ensure proper synchronization of this function with power
900 * management callbacks.
901 */
Rafael J. Wysockib88ce2a2012-11-26 10:03:06 +0100902int acpi_dev_pm_attach(struct device *dev, bool power_on)
Rafael J. Wysockie5cc8ef2012-11-02 01:41:01 +0100903{
904 struct acpi_device *adev = acpi_dev_pm_get_node(dev);
905
906 if (!adev)
907 return -ENODEV;
908
909 if (dev->pm_domain)
910 return -EEXIST;
911
912 acpi_add_pm_notifier(adev, acpi_wakeup_device, dev);
913 dev->pm_domain = &acpi_general_pm_domain;
Rafael J. Wysockib88ce2a2012-11-26 10:03:06 +0100914 if (power_on) {
915 acpi_dev_pm_full_power(adev);
916 __acpi_device_run_wake(adev, false);
917 }
Rafael J. Wysockie5cc8ef2012-11-02 01:41:01 +0100918 return 0;
919}
920EXPORT_SYMBOL_GPL(acpi_dev_pm_attach);
921
922/**
923 * acpi_dev_pm_detach - Remove ACPI power management from the device.
924 * @dev: Device to take care of.
Rafael J. Wysockib88ce2a2012-11-26 10:03:06 +0100925 * @power_off: Whether or not to try to remove power from the device.
Rafael J. Wysockie5cc8ef2012-11-02 01:41:01 +0100926 *
927 * Remove the device from the general ACPI PM domain and remove its wakeup
Rafael J. Wysockib88ce2a2012-11-26 10:03:06 +0100928 * notifier. If @power_off is set, additionally remove power from the device if
929 * possible.
Rafael J. Wysockie5cc8ef2012-11-02 01:41:01 +0100930 *
931 * Callers must ensure proper synchronization of this function with power
932 * management callbacks.
933 */
Rafael J. Wysockib88ce2a2012-11-26 10:03:06 +0100934void acpi_dev_pm_detach(struct device *dev, bool power_off)
Rafael J. Wysockie5cc8ef2012-11-02 01:41:01 +0100935{
936 struct acpi_device *adev = acpi_dev_pm_get_node(dev);
937
938 if (adev && dev->pm_domain == &acpi_general_pm_domain) {
939 dev->pm_domain = NULL;
940 acpi_remove_pm_notifier(adev, acpi_wakeup_device);
Rafael J. Wysockib88ce2a2012-11-26 10:03:06 +0100941 if (power_off) {
942 /*
943 * If the device's PM QoS resume latency limit or flags
944 * have been exposed to user space, they have to be
945 * hidden at this point, so that they don't affect the
946 * choice of the low-power state to put the device into.
947 */
948 dev_pm_qos_hide_latency_limit(dev);
949 dev_pm_qos_hide_flags(dev);
950 __acpi_device_run_wake(adev, false);
951 acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
952 }
Rafael J. Wysockie5cc8ef2012-11-02 01:41:01 +0100953 }
954}
955EXPORT_SYMBOL_GPL(acpi_dev_pm_detach);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100956
957/**
958 * acpi_dev_pm_add_dependent - Add physical device depending for PM.
959 * @handle: Handle of ACPI device node.
960 * @depdev: Device depending on that node for PM.
961 */
962void acpi_dev_pm_add_dependent(acpi_handle handle, struct device *depdev)
963{
964 struct acpi_device_physical_node *dep;
965 struct acpi_device *adev;
966
967 if (!depdev || acpi_bus_get_device(handle, &adev))
968 return;
969
970 mutex_lock(&adev->physical_node_lock);
971
972 list_for_each_entry(dep, &adev->power_dependent, node)
973 if (dep->dev == depdev)
974 goto out;
975
976 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
977 if (dep) {
978 dep->dev = depdev;
979 list_add_tail(&dep->node, &adev->power_dependent);
980 }
981
982 out:
983 mutex_unlock(&adev->physical_node_lock);
984}
985EXPORT_SYMBOL_GPL(acpi_dev_pm_add_dependent);
986
987/**
988 * acpi_dev_pm_remove_dependent - Remove physical device depending for PM.
989 * @handle: Handle of ACPI device node.
990 * @depdev: Device depending on that node for PM.
991 */
992void acpi_dev_pm_remove_dependent(acpi_handle handle, struct device *depdev)
993{
994 struct acpi_device_physical_node *dep;
995 struct acpi_device *adev;
996
997 if (!depdev || acpi_bus_get_device(handle, &adev))
998 return;
999
1000 mutex_lock(&adev->physical_node_lock);
1001
1002 list_for_each_entry(dep, &adev->power_dependent, node)
1003 if (dep->dev == depdev) {
1004 list_del(&dep->node);
1005 kfree(dep);
1006 break;
1007 }
1008
1009 mutex_unlock(&adev->physical_node_lock);
1010}
1011EXPORT_SYMBOL_GPL(acpi_dev_pm_remove_dependent);