fat: provide option for setting timezone offset
[linux-2.6/libata-dev.git] / drivers / acpi / device_pm.c
blobf09dc987cf17bce2ea762cdf902165cdbb7bd3d9
1 /*
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);
36 /**
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)
54 goto out;
56 status = acpi_install_notify_handler(adev->handle,
57 ACPI_SYSTEM_NOTIFY,
58 handler, context);
59 if (ACPI_FAILURE(status))
60 goto out;
62 adev->wakeup.flags.notifier_present = true;
64 out:
65 mutex_unlock(&acpi_pm_notifier_lock);
66 return status;
69 /**
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)
81 goto out;
83 status = acpi_remove_notify_handler(adev->handle,
84 ACPI_SYSTEM_NOTIFY,
85 handler);
86 if (ACPI_FAILURE(status))
87 goto out;
89 adev->wakeup.flags.notifier_present = false;
91 out:
92 mutex_unlock(&acpi_pm_notifier_lock);
93 return status;
96 /**
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;
120 bool wakeup = false;
122 if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3)
123 return -EINVAL;
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) !=
156 PM_QOS_FLAGS_NONE) {
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.
167 if (wakeup) {
168 acpi_status status;
170 acpi_method[3] = 'W';
171 status = acpi_evaluate_integer(adev->handle, acpi_method, NULL,
172 &d_max);
173 if (ACPI_FAILURE(status)) {
174 if (target_state != ACPI_STATE_S0 ||
175 status != AE_NOT_FOUND)
176 d_max = d_min;
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",
180 acpi_method);
181 /* Sanitize it */
182 d_min = d_max;
186 if (d_max_in < d_min)
187 return -EINVAL;
188 if (d_min_p)
189 *d_min_p = 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)
194 break;
197 return d_max;
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__);
218 return -ENODEV;
221 return acpi_device_power_state(dev, adev, acpi_target_system_state(),
222 d_max_in, d_min_p);
224 EXPORT_SYMBOL(acpi_pm_device_sleep_state);
226 #ifdef CONFIG_PM_RUNTIME
228 * acpi_wakeup_device - Wakeup notification handler for ACPI devices.
229 * @handle: ACPI handle of the device the notification is for.
230 * @event: Type of the signaled event.
231 * @context: Device corresponding to @handle.
233 static void acpi_wakeup_device(acpi_handle handle, u32 event, void *context)
235 struct device *dev = context;
237 if (event == ACPI_NOTIFY_DEVICE_WAKE && dev) {
238 pm_wakeup_event(dev, 0);
239 pm_runtime_resume(dev);
244 * __acpi_device_run_wake - Enable/disable runtime remote wakeup for device.
245 * @adev: ACPI device to enable/disable the remote wakeup for.
246 * @enable: Whether to enable or disable the wakeup functionality.
248 * Enable/disable the GPE associated with @adev so that it can generate
249 * wakeup signals for the device in response to external (remote) events and
250 * enable/disable device wakeup power.
252 * Callers must ensure that @adev is a valid ACPI device node before executing
253 * this function.
255 int __acpi_device_run_wake(struct acpi_device *adev, bool enable)
257 struct acpi_device_wakeup *wakeup = &adev->wakeup;
259 if (enable) {
260 acpi_status res;
261 int error;
263 error = acpi_enable_wakeup_device_power(adev, ACPI_STATE_S0);
264 if (error)
265 return error;
267 res = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);
268 if (ACPI_FAILURE(res)) {
269 acpi_disable_wakeup_device_power(adev);
270 return -EIO;
272 } else {
273 acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
274 acpi_disable_wakeup_device_power(adev);
276 return 0;
280 * acpi_pm_device_run_wake - Enable/disable remote wakeup for given device.
281 * @dev: Device to enable/disable the platform to wake up.
282 * @enable: Whether to enable or disable the wakeup functionality.
284 int acpi_pm_device_run_wake(struct device *phys_dev, bool enable)
286 struct acpi_device *adev;
287 acpi_handle handle;
289 if (!device_run_wake(phys_dev))
290 return -EINVAL;
292 handle = DEVICE_ACPI_HANDLE(phys_dev);
293 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
294 dev_dbg(phys_dev, "ACPI handle without context in %s!\n",
295 __func__);
296 return -ENODEV;
299 return __acpi_device_run_wake(adev, enable);
301 EXPORT_SYMBOL(acpi_pm_device_run_wake);
302 #else
303 static inline void acpi_wakeup_device(acpi_handle handle, u32 event,
304 void *context) {}
305 #endif /* CONFIG_PM_RUNTIME */
307 #ifdef CONFIG_PM_SLEEP
309 * __acpi_device_sleep_wake - Enable or disable device to wake up the system.
310 * @dev: Device to enable/desible to wake up the system.
311 * @target_state: System state the device is supposed to wake up from.
312 * @enable: Whether to enable or disable @dev to wake up the system.
314 int __acpi_device_sleep_wake(struct acpi_device *adev, u32 target_state,
315 bool enable)
317 return enable ?
318 acpi_enable_wakeup_device_power(adev, target_state) :
319 acpi_disable_wakeup_device_power(adev);
323 * acpi_pm_device_sleep_wake - Enable or disable device to wake up the system.
324 * @dev: Device to enable/desible to wake up the system from sleep states.
325 * @enable: Whether to enable or disable @dev to wake up the system.
327 int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
329 acpi_handle handle;
330 struct acpi_device *adev;
331 int error;
333 if (!device_can_wakeup(dev))
334 return -EINVAL;
336 handle = DEVICE_ACPI_HANDLE(dev);
337 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
338 dev_dbg(dev, "ACPI handle without context in %s!\n", __func__);
339 return -ENODEV;
342 error = __acpi_device_sleep_wake(adev, acpi_target_system_state(),
343 enable);
344 if (!error)
345 dev_info(dev, "System wakeup %s by ACPI\n",
346 enable ? "enabled" : "disabled");
348 return error;
350 #endif /* CONFIG_PM_SLEEP */
353 * acpi_dev_pm_get_node - Get ACPI device node for the given physical device.
354 * @dev: Device to get the ACPI node for.
356 static struct acpi_device *acpi_dev_pm_get_node(struct device *dev)
358 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
359 struct acpi_device *adev;
361 return handle && ACPI_SUCCESS(acpi_bus_get_device(handle, &adev)) ?
362 adev : NULL;
366 * acpi_dev_pm_low_power - Put ACPI device into a low-power state.
367 * @dev: Device to put into a low-power state.
368 * @adev: ACPI device node corresponding to @dev.
369 * @system_state: System state to choose the device state for.
371 static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev,
372 u32 system_state)
374 int power_state;
376 if (!acpi_device_power_manageable(adev))
377 return 0;
379 power_state = acpi_device_power_state(dev, adev, system_state,
380 ACPI_STATE_D3, NULL);
381 if (power_state < ACPI_STATE_D0 || power_state > ACPI_STATE_D3)
382 return -EIO;
384 return acpi_device_set_power(adev, power_state);
388 * acpi_dev_pm_full_power - Put ACPI device into the full-power state.
389 * @adev: ACPI device node to put into the full-power state.
391 static int acpi_dev_pm_full_power(struct acpi_device *adev)
393 return acpi_device_power_manageable(adev) ?
394 acpi_device_set_power(adev, ACPI_STATE_D0) : 0;
397 #ifdef CONFIG_PM_RUNTIME
399 * acpi_dev_runtime_suspend - Put device into a low-power state using ACPI.
400 * @dev: Device to put into a low-power state.
402 * Put the given device into a runtime low-power state using the standard ACPI
403 * mechanism. Set up remote wakeup if desired, choose the state to put the
404 * device into (this checks if remote wakeup is expected to work too), and set
405 * the power state of the device.
407 int acpi_dev_runtime_suspend(struct device *dev)
409 struct acpi_device *adev = acpi_dev_pm_get_node(dev);
410 bool remote_wakeup;
411 int error;
413 if (!adev)
414 return 0;
416 remote_wakeup = dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) >
417 PM_QOS_FLAGS_NONE;
418 error = __acpi_device_run_wake(adev, remote_wakeup);
419 if (remote_wakeup && error)
420 return -EAGAIN;
422 error = acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
423 if (error)
424 __acpi_device_run_wake(adev, false);
426 return error;
428 EXPORT_SYMBOL_GPL(acpi_dev_runtime_suspend);
431 * acpi_dev_runtime_resume - Put device into the full-power state using ACPI.
432 * @dev: Device to put into the full-power state.
434 * Put the given device into the full-power state using the standard ACPI
435 * mechanism at run time. Set the power state of the device to ACPI D0 and
436 * disable remote wakeup.
438 int acpi_dev_runtime_resume(struct device *dev)
440 struct acpi_device *adev = acpi_dev_pm_get_node(dev);
441 int error;
443 if (!adev)
444 return 0;
446 error = acpi_dev_pm_full_power(adev);
447 __acpi_device_run_wake(adev, false);
448 return error;
450 EXPORT_SYMBOL_GPL(acpi_dev_runtime_resume);
453 * acpi_subsys_runtime_suspend - Suspend device using ACPI.
454 * @dev: Device to suspend.
456 * Carry out the generic runtime suspend procedure for @dev and use ACPI to put
457 * it into a runtime low-power state.
459 int acpi_subsys_runtime_suspend(struct device *dev)
461 int ret = pm_generic_runtime_suspend(dev);
462 return ret ? ret : acpi_dev_runtime_suspend(dev);
464 EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend);
467 * acpi_subsys_runtime_resume - Resume device using ACPI.
468 * @dev: Device to Resume.
470 * Use ACPI to put the given device into the full-power state and carry out the
471 * generic runtime resume procedure for it.
473 int acpi_subsys_runtime_resume(struct device *dev)
475 int ret = acpi_dev_runtime_resume(dev);
476 return ret ? ret : pm_generic_runtime_resume(dev);
478 EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume);
479 #endif /* CONFIG_PM_RUNTIME */
481 #ifdef CONFIG_PM_SLEEP
483 * acpi_dev_suspend_late - Put device into a low-power state using ACPI.
484 * @dev: Device to put into a low-power state.
486 * Put the given device into a low-power state during system transition to a
487 * sleep state using the standard ACPI mechanism. Set up system wakeup if
488 * desired, choose the state to put the device into (this checks if system
489 * wakeup is expected to work too), and set the power state of the device.
491 int acpi_dev_suspend_late(struct device *dev)
493 struct acpi_device *adev = acpi_dev_pm_get_node(dev);
494 u32 target_state;
495 bool wakeup;
496 int error;
498 if (!adev)
499 return 0;
501 target_state = acpi_target_system_state();
502 wakeup = device_may_wakeup(dev);
503 error = __acpi_device_sleep_wake(adev, target_state, wakeup);
504 if (wakeup && error)
505 return error;
507 error = acpi_dev_pm_low_power(dev, adev, target_state);
508 if (error)
509 __acpi_device_sleep_wake(adev, ACPI_STATE_UNKNOWN, false);
511 return error;
513 EXPORT_SYMBOL_GPL(acpi_dev_suspend_late);
516 * acpi_dev_resume_early - Put device into the full-power state using ACPI.
517 * @dev: Device to put into the full-power state.
519 * Put the given device into the full-power state using the standard ACPI
520 * mechanism during system transition to the working state. Set the power
521 * state of the device to ACPI D0 and disable remote wakeup.
523 int acpi_dev_resume_early(struct device *dev)
525 struct acpi_device *adev = acpi_dev_pm_get_node(dev);
526 int error;
528 if (!adev)
529 return 0;
531 error = acpi_dev_pm_full_power(adev);
532 __acpi_device_sleep_wake(adev, ACPI_STATE_UNKNOWN, false);
533 return error;
535 EXPORT_SYMBOL_GPL(acpi_dev_resume_early);
538 * acpi_subsys_prepare - Prepare device for system transition to a sleep state.
539 * @dev: Device to prepare.
541 int acpi_subsys_prepare(struct device *dev)
544 * Follow PCI and resume devices suspended at run time before running
545 * their system suspend callbacks.
547 pm_runtime_resume(dev);
548 return pm_generic_prepare(dev);
550 EXPORT_SYMBOL_GPL(acpi_subsys_prepare);
553 * acpi_subsys_suspend_late - Suspend device using ACPI.
554 * @dev: Device to suspend.
556 * Carry out the generic late suspend procedure for @dev and use ACPI to put
557 * it into a low-power state during system transition into a sleep state.
559 int acpi_subsys_suspend_late(struct device *dev)
561 int ret = pm_generic_suspend_late(dev);
562 return ret ? ret : acpi_dev_suspend_late(dev);
564 EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late);
567 * acpi_subsys_resume_early - Resume device using ACPI.
568 * @dev: Device to Resume.
570 * Use ACPI to put the given device into the full-power state and carry out the
571 * generic early resume procedure for it during system transition into the
572 * working state.
574 int acpi_subsys_resume_early(struct device *dev)
576 int ret = acpi_dev_resume_early(dev);
577 return ret ? ret : pm_generic_resume_early(dev);
579 EXPORT_SYMBOL_GPL(acpi_subsys_resume_early);
580 #endif /* CONFIG_PM_SLEEP */
582 static struct dev_pm_domain acpi_general_pm_domain = {
583 .ops = {
584 #ifdef CONFIG_PM_RUNTIME
585 .runtime_suspend = acpi_subsys_runtime_suspend,
586 .runtime_resume = acpi_subsys_runtime_resume,
587 .runtime_idle = pm_generic_runtime_idle,
588 #endif
589 #ifdef CONFIG_PM_SLEEP
590 .prepare = acpi_subsys_prepare,
591 .suspend_late = acpi_subsys_suspend_late,
592 .resume_early = acpi_subsys_resume_early,
593 .poweroff_late = acpi_subsys_suspend_late,
594 .restore_early = acpi_subsys_resume_early,
595 #endif
600 * acpi_dev_pm_attach - Prepare device for ACPI power management.
601 * @dev: Device to prepare.
602 * @power_on: Whether or not to power on the device.
604 * If @dev has a valid ACPI handle that has a valid struct acpi_device object
605 * attached to it, install a wakeup notification handler for the device and
606 * add it to the general ACPI PM domain. If @power_on is set, the device will
607 * be put into the ACPI D0 state before the function returns.
609 * This assumes that the @dev's bus type uses generic power management callbacks
610 * (or doesn't use any power management callbacks at all).
612 * Callers must ensure proper synchronization of this function with power
613 * management callbacks.
615 int acpi_dev_pm_attach(struct device *dev, bool power_on)
617 struct acpi_device *adev = acpi_dev_pm_get_node(dev);
619 if (!adev)
620 return -ENODEV;
622 if (dev->pm_domain)
623 return -EEXIST;
625 acpi_add_pm_notifier(adev, acpi_wakeup_device, dev);
626 dev->pm_domain = &acpi_general_pm_domain;
627 if (power_on) {
628 acpi_dev_pm_full_power(adev);
629 __acpi_device_run_wake(adev, false);
631 return 0;
633 EXPORT_SYMBOL_GPL(acpi_dev_pm_attach);
636 * acpi_dev_pm_detach - Remove ACPI power management from the device.
637 * @dev: Device to take care of.
638 * @power_off: Whether or not to try to remove power from the device.
640 * Remove the device from the general ACPI PM domain and remove its wakeup
641 * notifier. If @power_off is set, additionally remove power from the device if
642 * possible.
644 * Callers must ensure proper synchronization of this function with power
645 * management callbacks.
647 void acpi_dev_pm_detach(struct device *dev, bool power_off)
649 struct acpi_device *adev = acpi_dev_pm_get_node(dev);
651 if (adev && dev->pm_domain == &acpi_general_pm_domain) {
652 dev->pm_domain = NULL;
653 acpi_remove_pm_notifier(adev, acpi_wakeup_device);
654 if (power_off) {
656 * If the device's PM QoS resume latency limit or flags
657 * have been exposed to user space, they have to be
658 * hidden at this point, so that they don't affect the
659 * choice of the low-power state to put the device into.
661 dev_pm_qos_hide_latency_limit(dev);
662 dev_pm_qos_hide_flags(dev);
663 __acpi_device_run_wake(adev, false);
664 acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
668 EXPORT_SYMBOL_GPL(acpi_dev_pm_detach);