Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * sleep.c - ACPI sleep support. |
| 3 | * |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 4 | * Copyright (c) 2005 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Copyright (c) 2004 David Shaohua Li <shaohua.li@intel.com> |
| 6 | * Copyright (c) 2000-2003 Patrick Mochel |
| 7 | * Copyright (c) 2003 Open Source Development Lab |
| 8 | * |
| 9 | * This file is released under the GPLv2. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <linux/delay.h> |
| 14 | #include <linux/irq.h> |
| 15 | #include <linux/dmi.h> |
| 16 | #include <linux/device.h> |
| 17 | #include <linux/suspend.h> |
Alexey Starikovskiy | f216cc3 | 2007-09-20 21:32:35 +0400 | [diff] [blame] | 18 | |
| 19 | #include <asm/io.h> |
| 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <acpi/acpi_bus.h> |
| 22 | #include <acpi/acpi_drivers.h> |
| 23 | #include "sleep.h" |
| 24 | |
| 25 | u8 sleep_states[ACPI_S_STATE_COUNT]; |
| 26 | |
Alexey Starikovskiy | 853298b | 2007-09-25 18:45:15 +0400 | [diff] [blame] | 27 | #ifdef CONFIG_PM_SLEEP |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 28 | static u32 acpi_target_sleep_state = ACPI_STATE_S0; |
Alexey Starikovskiy | 853298b | 2007-09-25 18:45:15 +0400 | [diff] [blame] | 29 | #endif |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 30 | |
Alexey Starikovskiy | 2f3f2226 | 2007-09-24 14:33:21 +0200 | [diff] [blame] | 31 | int acpi_sleep_prepare(u32 acpi_state) |
| 32 | { |
| 33 | #ifdef CONFIG_ACPI_SLEEP |
| 34 | /* do we have a wakeup address for S2 and S3? */ |
| 35 | if (acpi_state == ACPI_STATE_S3) { |
| 36 | if (!acpi_wakeup_address) { |
| 37 | return -EFAULT; |
| 38 | } |
| 39 | acpi_set_firmware_waking_vector((acpi_physical_address) |
| 40 | virt_to_phys((void *) |
| 41 | acpi_wakeup_address)); |
| 42 | |
| 43 | } |
| 44 | ACPI_FLUSH_CPU_CACHE(); |
| 45 | acpi_enable_wakeup_device_prep(acpi_state); |
| 46 | #endif |
| 47 | acpi_gpe_sleep_prepare(acpi_state); |
| 48 | acpi_enter_sleep_state_prep(acpi_state); |
| 49 | return 0; |
| 50 | } |
| 51 | |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 52 | #ifdef CONFIG_SUSPEND |
Rafael J. Wysocki | 26398a7 | 2007-10-18 03:04:40 -0700 | [diff] [blame] | 53 | static struct platform_suspend_ops acpi_pm_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | extern void do_suspend_lowlevel(void); |
| 56 | |
| 57 | static u32 acpi_suspend_states[] = { |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 58 | [PM_SUSPEND_ON] = ACPI_STATE_S0, |
| 59 | [PM_SUSPEND_STANDBY] = ACPI_STATE_S1, |
| 60 | [PM_SUSPEND_MEM] = ACPI_STATE_S3, |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 61 | [PM_SUSPEND_MAX] = ACPI_STATE_S5 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | static int init_8259A_after_S1; |
| 65 | |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 66 | /** |
| 67 | * acpi_pm_set_target - Set the target system sleep state to the state |
| 68 | * associated with given @pm_state, if supported. |
| 69 | */ |
| 70 | |
| 71 | static int acpi_pm_set_target(suspend_state_t pm_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { |
| 73 | u32 acpi_state = acpi_suspend_states[pm_state]; |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 74 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 76 | if (sleep_states[acpi_state]) { |
| 77 | acpi_target_sleep_state = acpi_state; |
| 78 | } else { |
| 79 | printk(KERN_ERR "ACPI does not support this state: %d\n", |
| 80 | pm_state); |
| 81 | error = -ENOSYS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | } |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 83 | return error; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * acpi_pm_prepare - Do preliminary suspend work. |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 88 | * |
| 89 | * If necessary, set the firmware waking vector and do arch-specific |
| 90 | * nastiness to get the wakeup code to the waking vector. |
| 91 | */ |
| 92 | |
Rafael J. Wysocki | e6c5eb9 | 2007-10-18 03:04:41 -0700 | [diff] [blame] | 93 | static int acpi_pm_prepare(void) |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 94 | { |
| 95 | int error = acpi_sleep_prepare(acpi_target_sleep_state); |
| 96 | |
| 97 | if (error) |
| 98 | acpi_target_sleep_state = ACPI_STATE_S0; |
| 99 | |
| 100 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | /** |
| 104 | * acpi_pm_enter - Actually enter a sleep state. |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 105 | * @pm_state: ignored |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | * |
Rafael J. Wysocki | 50ad147 | 2007-07-24 11:58:39 +0200 | [diff] [blame] | 107 | * Flush caches and go to sleep. For STR we have to call arch-specific |
| 108 | * assembly, which in turn call acpi_enter_sleep_state(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | * It's unfortunate, but it works. Please fix if you're feeling frisky. |
| 110 | */ |
| 111 | |
| 112 | static int acpi_pm_enter(suspend_state_t pm_state) |
| 113 | { |
| 114 | acpi_status status = AE_OK; |
| 115 | unsigned long flags = 0; |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 116 | u32 acpi_state = acpi_target_sleep_state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
| 118 | ACPI_FLUSH_CPU_CACHE(); |
| 119 | |
| 120 | /* Do arch specific saving of state. */ |
Rafael J. Wysocki | 50ad147 | 2007-07-24 11:58:39 +0200 | [diff] [blame] | 121 | if (acpi_state == ACPI_STATE_S3) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | int error = acpi_save_state_mem(); |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 123 | |
| 124 | if (error) { |
| 125 | acpi_target_sleep_state = ACPI_STATE_S0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | return error; |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 127 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | local_irq_save(flags); |
| 131 | acpi_enable_wakeup_device(acpi_state); |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 132 | switch (acpi_state) { |
| 133 | case ACPI_STATE_S1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | barrier(); |
| 135 | status = acpi_enter_sleep_state(acpi_state); |
| 136 | break; |
| 137 | |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 138 | case ACPI_STATE_S3: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | do_suspend_lowlevel(); |
| 140 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | } |
Arnaud Patard | 872d83d | 2006-04-27 05:25:00 -0400 | [diff] [blame] | 142 | |
| 143 | /* ACPI 3.0 specs (P62) says that it's the responsabilty |
| 144 | * of the OSPM to clear the status bit [ implying that the |
| 145 | * POWER_BUTTON event should not reach userspace ] |
| 146 | */ |
| 147 | if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3)) |
| 148 | acpi_clear_event(ACPI_EVENT_POWER_BUTTON); |
| 149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | local_irq_restore(flags); |
| 151 | printk(KERN_DEBUG "Back to C!\n"); |
| 152 | |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 153 | /* restore processor state */ |
Rafael J. Wysocki | 50ad147 | 2007-07-24 11:58:39 +0200 | [diff] [blame] | 154 | if (acpi_state == ACPI_STATE_S3) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | acpi_restore_state_mem(); |
| 156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | return ACPI_SUCCESS(status) ? 0 : -EFAULT; |
| 158 | } |
| 159 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | /** |
| 161 | * acpi_pm_finish - Finish up suspend sequence. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | * |
| 163 | * This is called after we wake back up (or if entering the sleep state |
| 164 | * failed). |
| 165 | */ |
| 166 | |
Rafael J. Wysocki | e6c5eb9 | 2007-10-18 03:04:41 -0700 | [diff] [blame] | 167 | static void acpi_pm_finish(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | { |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 169 | u32 acpi_state = acpi_target_sleep_state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
| 171 | acpi_leave_sleep_state(acpi_state); |
| 172 | acpi_disable_wakeup_device(acpi_state); |
| 173 | |
| 174 | /* reset firmware waking vector */ |
| 175 | acpi_set_firmware_waking_vector((acpi_physical_address) 0); |
| 176 | |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 177 | acpi_target_sleep_state = ACPI_STATE_S0; |
| 178 | |
Len Brown | e8b2fd0 | 2007-07-24 22:26:33 -0400 | [diff] [blame] | 179 | #ifdef CONFIG_X86 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | if (init_8259A_after_S1) { |
| 181 | printk("Broken toshiba laptop -> kicking interrupts\n"); |
| 182 | init_8259A(0); |
| 183 | } |
Len Brown | e8b2fd0 | 2007-07-24 22:26:33 -0400 | [diff] [blame] | 184 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Shaohua Li | eb9289e | 2005-10-30 15:00:01 -0800 | [diff] [blame] | 187 | static int acpi_pm_state_valid(suspend_state_t pm_state) |
| 188 | { |
Johannes Berg | e8c9c50 | 2007-04-30 15:09:54 -0700 | [diff] [blame] | 189 | u32 acpi_state; |
Shaohua Li | eb9289e | 2005-10-30 15:00:01 -0800 | [diff] [blame] | 190 | |
Johannes Berg | e8c9c50 | 2007-04-30 15:09:54 -0700 | [diff] [blame] | 191 | switch (pm_state) { |
| 192 | case PM_SUSPEND_ON: |
| 193 | case PM_SUSPEND_STANDBY: |
| 194 | case PM_SUSPEND_MEM: |
| 195 | acpi_state = acpi_suspend_states[pm_state]; |
| 196 | |
| 197 | return sleep_states[acpi_state]; |
| 198 | default: |
| 199 | return 0; |
| 200 | } |
Shaohua Li | eb9289e | 2005-10-30 15:00:01 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Rafael J. Wysocki | 26398a7 | 2007-10-18 03:04:40 -0700 | [diff] [blame] | 203 | static struct platform_suspend_ops acpi_pm_ops = { |
Shaohua Li | eb9289e | 2005-10-30 15:00:01 -0800 | [diff] [blame] | 204 | .valid = acpi_pm_state_valid, |
Rafael J. Wysocki | e9b3aba | 2007-07-17 22:40:06 +0200 | [diff] [blame] | 205 | .set_target = acpi_pm_set_target, |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 206 | .prepare = acpi_pm_prepare, |
| 207 | .enter = acpi_pm_enter, |
| 208 | .finish = acpi_pm_finish, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | }; |
| 210 | |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 211 | /* |
| 212 | * Toshiba fails to preserve interrupts over S1, reinitialization |
| 213 | * of 8259 is needed after S1 resume. |
| 214 | */ |
Jeff Garzik | 1855256 | 2007-10-03 15:15:40 -0400 | [diff] [blame] | 215 | static int __init init_ints_after_s1(const struct dmi_system_id *d) |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 216 | { |
| 217 | printk(KERN_WARNING "%s with broken S1 detected.\n", d->ident); |
| 218 | init_8259A_after_S1 = 1; |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static struct dmi_system_id __initdata acpisleep_dmi_table[] = { |
| 223 | { |
| 224 | .callback = init_ints_after_s1, |
| 225 | .ident = "Toshiba Satellite 4030cdt", |
| 226 | .matches = {DMI_MATCH(DMI_PRODUCT_NAME, "S4030CDT/4.3"),}, |
| 227 | }, |
| 228 | {}, |
| 229 | }; |
| 230 | #endif /* CONFIG_SUSPEND */ |
| 231 | |
Rafael J. Wysocki | b0cb1a1 | 2007-07-29 23:24:36 +0200 | [diff] [blame] | 232 | #ifdef CONFIG_HIBERNATION |
Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 233 | static int acpi_hibernation_start(void) |
| 234 | { |
| 235 | acpi_target_sleep_state = ACPI_STATE_S4; |
| 236 | return 0; |
| 237 | } |
| 238 | |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 239 | static int acpi_hibernation_prepare(void) |
| 240 | { |
| 241 | return acpi_sleep_prepare(ACPI_STATE_S4); |
| 242 | } |
| 243 | |
| 244 | static int acpi_hibernation_enter(void) |
| 245 | { |
| 246 | acpi_status status = AE_OK; |
| 247 | unsigned long flags = 0; |
| 248 | |
| 249 | ACPI_FLUSH_CPU_CACHE(); |
| 250 | |
| 251 | local_irq_save(flags); |
| 252 | acpi_enable_wakeup_device(ACPI_STATE_S4); |
| 253 | /* This shouldn't return. If it returns, we have a problem */ |
| 254 | status = acpi_enter_sleep_state(ACPI_STATE_S4); |
| 255 | local_irq_restore(flags); |
| 256 | |
| 257 | return ACPI_SUCCESS(status) ? 0 : -EFAULT; |
| 258 | } |
| 259 | |
Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame^] | 260 | static void acpi_hibernation_leave(void) |
| 261 | { |
| 262 | /* |
| 263 | * If ACPI is not enabled by the BIOS and the boot kernel, we need to |
| 264 | * enable it here. |
| 265 | */ |
| 266 | acpi_enable(); |
| 267 | } |
| 268 | |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 269 | static void acpi_hibernation_finish(void) |
| 270 | { |
| 271 | acpi_leave_sleep_state(ACPI_STATE_S4); |
| 272 | acpi_disable_wakeup_device(ACPI_STATE_S4); |
| 273 | |
| 274 | /* reset firmware waking vector */ |
| 275 | acpi_set_firmware_waking_vector((acpi_physical_address) 0); |
Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 276 | |
| 277 | acpi_target_sleep_state = ACPI_STATE_S0; |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 280 | static int acpi_hibernation_pre_restore(void) |
| 281 | { |
| 282 | acpi_status status; |
| 283 | |
| 284 | status = acpi_hw_disable_all_gpes(); |
| 285 | |
| 286 | return ACPI_SUCCESS(status) ? 0 : -EFAULT; |
| 287 | } |
| 288 | |
| 289 | static void acpi_hibernation_restore_cleanup(void) |
| 290 | { |
| 291 | acpi_hw_enable_all_runtime_gpes(); |
| 292 | } |
| 293 | |
Rafael J. Wysocki | b3dac3b | 2007-10-18 03:04:43 -0700 | [diff] [blame] | 294 | static struct platform_hibernation_ops acpi_hibernation_ops = { |
Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 295 | .start = acpi_hibernation_start, |
| 296 | .pre_snapshot = acpi_hibernation_prepare, |
| 297 | .finish = acpi_hibernation_finish, |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 298 | .prepare = acpi_hibernation_prepare, |
| 299 | .enter = acpi_hibernation_enter, |
Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame^] | 300 | .leave = acpi_hibernation_leave, |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 301 | .pre_restore = acpi_hibernation_pre_restore, |
| 302 | .restore_cleanup = acpi_hibernation_restore_cleanup, |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 303 | }; |
Rafael J. Wysocki | b0cb1a1 | 2007-07-29 23:24:36 +0200 | [diff] [blame] | 304 | #endif /* CONFIG_HIBERNATION */ |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 305 | |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 306 | int acpi_suspend(u32 acpi_state) |
| 307 | { |
| 308 | suspend_state_t states[] = { |
| 309 | [1] = PM_SUSPEND_STANDBY, |
| 310 | [3] = PM_SUSPEND_MEM, |
| 311 | [5] = PM_SUSPEND_MAX |
| 312 | }; |
| 313 | |
| 314 | if (acpi_state < 6 && states[acpi_state]) |
| 315 | return pm_suspend(states[acpi_state]); |
| 316 | if (acpi_state == 4) |
| 317 | return hibernate(); |
| 318 | return -EINVAL; |
| 319 | } |
| 320 | |
Alexey Starikovskiy | 853298b | 2007-09-25 18:45:15 +0400 | [diff] [blame] | 321 | #ifdef CONFIG_PM_SLEEP |
Shaohua Li | fd4aff1 | 2007-07-17 22:40:25 +0200 | [diff] [blame] | 322 | /** |
| 323 | * acpi_pm_device_sleep_state - return preferred power state of ACPI device |
| 324 | * in the system sleep state given by %acpi_target_sleep_state |
| 325 | * @dev: device to examine |
| 326 | * @wake: if set, the device should be able to wake up the system |
| 327 | * @d_min_p: used to store the upper limit of allowed states range |
| 328 | * Return value: preferred power state of the device on success, -ENODEV on |
| 329 | * failure (ie. if there's no 'struct acpi_device' for @dev) |
| 330 | * |
| 331 | * Find the lowest power (highest number) ACPI device power state that |
| 332 | * device @dev can be in while the system is in the sleep state represented |
| 333 | * by %acpi_target_sleep_state. If @wake is nonzero, the device should be |
| 334 | * able to wake up the system from this sleep state. If @d_min_p is set, |
| 335 | * the highest power (lowest number) device power state of @dev allowed |
| 336 | * in this system sleep state is stored at the location pointed to by it. |
| 337 | * |
| 338 | * The caller must ensure that @dev is valid before using this function. |
| 339 | * The caller is also responsible for figuring out if the device is |
| 340 | * supposed to be able to wake up the system and passing this information |
| 341 | * via @wake. |
| 342 | */ |
| 343 | |
| 344 | int acpi_pm_device_sleep_state(struct device *dev, int wake, int *d_min_p) |
| 345 | { |
| 346 | acpi_handle handle = DEVICE_ACPI_HANDLE(dev); |
| 347 | struct acpi_device *adev; |
| 348 | char acpi_method[] = "_SxD"; |
| 349 | unsigned long d_min, d_max; |
| 350 | |
| 351 | if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) { |
Shaohua Li | ead7759 | 2007-08-23 15:01:13 +0800 | [diff] [blame] | 352 | printk(KERN_DEBUG "ACPI handle has no context!\n"); |
Shaohua Li | fd4aff1 | 2007-07-17 22:40:25 +0200 | [diff] [blame] | 353 | return -ENODEV; |
| 354 | } |
| 355 | |
| 356 | acpi_method[2] = '0' + acpi_target_sleep_state; |
| 357 | /* |
| 358 | * If the sleep state is S0, we will return D3, but if the device has |
| 359 | * _S0W, we will use the value from _S0W |
| 360 | */ |
| 361 | d_min = ACPI_STATE_D0; |
| 362 | d_max = ACPI_STATE_D3; |
| 363 | |
| 364 | /* |
| 365 | * If present, _SxD methods return the minimum D-state (highest power |
| 366 | * state) we can use for the corresponding S-states. Otherwise, the |
| 367 | * minimum D-state is D0 (ACPI 3.x). |
| 368 | * |
| 369 | * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer |
| 370 | * provided -- that's our fault recovery, we ignore retval. |
| 371 | */ |
| 372 | if (acpi_target_sleep_state > ACPI_STATE_S0) |
| 373 | acpi_evaluate_integer(handle, acpi_method, NULL, &d_min); |
| 374 | |
| 375 | /* |
| 376 | * If _PRW says we can wake up the system from the target sleep state, |
| 377 | * the D-state returned by _SxD is sufficient for that (we assume a |
| 378 | * wakeup-aware driver if wake is set). Still, if _SxW exists |
| 379 | * (ACPI 3.x), it should return the maximum (lowest power) D-state that |
| 380 | * can wake the system. _S0W may be valid, too. |
| 381 | */ |
| 382 | if (acpi_target_sleep_state == ACPI_STATE_S0 || |
| 383 | (wake && adev->wakeup.state.enabled && |
| 384 | adev->wakeup.sleep_state <= acpi_target_sleep_state)) { |
| 385 | acpi_method[3] = 'W'; |
| 386 | acpi_evaluate_integer(handle, acpi_method, NULL, &d_max); |
| 387 | /* Sanity check */ |
| 388 | if (d_max < d_min) |
| 389 | d_min = d_max; |
| 390 | } |
| 391 | |
| 392 | if (d_min_p) |
| 393 | *d_min_p = d_min; |
| 394 | return d_max; |
| 395 | } |
Alexey Starikovskiy | 853298b | 2007-09-25 18:45:15 +0400 | [diff] [blame] | 396 | #endif |
Shaohua Li | fd4aff1 | 2007-07-17 22:40:25 +0200 | [diff] [blame] | 397 | |
Alexey Starikovskiy | f216cc3 | 2007-09-20 21:32:35 +0400 | [diff] [blame] | 398 | static void acpi_power_off_prepare(void) |
| 399 | { |
| 400 | /* Prepare to power off the system */ |
| 401 | acpi_sleep_prepare(ACPI_STATE_S5); |
| 402 | } |
| 403 | |
| 404 | static void acpi_power_off(void) |
| 405 | { |
| 406 | /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */ |
| 407 | printk("%s called\n", __FUNCTION__); |
| 408 | local_irq_disable(); |
| 409 | acpi_enter_sleep_state(ACPI_STATE_S5); |
| 410 | } |
| 411 | |
Alexey Starikovskiy | aafbcd1 | 2007-02-10 01:32:16 -0500 | [diff] [blame] | 412 | int __init acpi_sleep_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | { |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 414 | acpi_status status; |
| 415 | u8 type_a, type_b; |
| 416 | #ifdef CONFIG_SUSPEND |
Alexey Starikovskiy | e2a5b42 | 2005-03-18 16:20:46 -0500 | [diff] [blame] | 417 | int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
| 419 | dmi_check_system(acpisleep_dmi_table); |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 420 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | |
| 422 | if (acpi_disabled) |
| 423 | return 0; |
| 424 | |
Frans Pop | 5a50fe7 | 2007-09-20 22:27:44 +0200 | [diff] [blame] | 425 | sleep_states[ACPI_STATE_S0] = 1; |
| 426 | printk(KERN_INFO PREFIX "(supports S0"); |
| 427 | |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 428 | #ifdef CONFIG_SUSPEND |
Frans Pop | 5a50fe7 | 2007-09-20 22:27:44 +0200 | [diff] [blame] | 429 | for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | status = acpi_get_sleep_type_data(i, &type_a, &type_b); |
| 431 | if (ACPI_SUCCESS(status)) { |
| 432 | sleep_states[i] = 1; |
| 433 | printk(" S%d", i); |
| 434 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
Rafael J. Wysocki | 26398a7 | 2007-10-18 03:04:40 -0700 | [diff] [blame] | 437 | suspend_set_ops(&acpi_pm_ops); |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 438 | #endif |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 439 | |
Rafael J. Wysocki | b0cb1a1 | 2007-07-29 23:24:36 +0200 | [diff] [blame] | 440 | #ifdef CONFIG_HIBERNATION |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 441 | status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b); |
| 442 | if (ACPI_SUCCESS(status)) { |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 443 | hibernation_set_ops(&acpi_hibernation_ops); |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 444 | sleep_states[ACPI_STATE_S4] = 1; |
Alexey Starikovskiy | f216cc3 | 2007-09-20 21:32:35 +0400 | [diff] [blame] | 445 | printk(" S4"); |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 446 | } |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 447 | #endif |
Alexey Starikovskiy | f216cc3 | 2007-09-20 21:32:35 +0400 | [diff] [blame] | 448 | status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b); |
| 449 | if (ACPI_SUCCESS(status)) { |
| 450 | sleep_states[ACPI_STATE_S5] = 1; |
| 451 | printk(" S5"); |
| 452 | pm_power_off_prepare = acpi_power_off_prepare; |
| 453 | pm_power_off = acpi_power_off; |
| 454 | } |
| 455 | printk(")\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | return 0; |
| 457 | } |