Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * wakeup.c - support wakeup devices |
| 3 | * Copyright (C) 2004 Li Shaohua <shaohua.li@intel.com> |
| 4 | */ |
| 5 | |
| 6 | #include <linux/init.h> |
| 7 | #include <linux/acpi.h> |
| 8 | #include <acpi/acpi_drivers.h> |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/types.h> |
| 11 | #include <acpi/acevents.h> |
| 12 | #include "sleep.h" |
| 13 | |
| 14 | #define _COMPONENT ACPI_SYSTEM_COMPONENT |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 15 | ACPI_MODULE_NAME("wakeup_devices") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 17 | extern struct list_head acpi_wakeup_device_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | extern spinlock_t acpi_device_lock; |
| 19 | |
| 20 | #ifdef CONFIG_ACPI_SLEEP |
| 21 | /** |
| 22 | * acpi_enable_wakeup_device_prep - prepare wakeup devices |
| 23 | * @sleep_state: ACPI state |
| 24 | * Enable all wakup devices power if the devices' wakeup level |
| 25 | * is higher than requested sleep level |
| 26 | */ |
| 27 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 28 | void acpi_enable_wakeup_device_prep(u8 sleep_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 30 | struct list_head *node, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | ACPI_FUNCTION_TRACE("acpi_enable_wakeup_device_prep"); |
| 33 | |
| 34 | spin_lock(&acpi_device_lock); |
| 35 | list_for_each_safe(node, next, &acpi_wakeup_device_list) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 36 | struct acpi_device *dev = container_of(node, |
| 37 | struct acpi_device, |
| 38 | wakeup_list); |
| 39 | |
| 40 | if (!dev->wakeup.flags.valid || |
| 41 | !dev->wakeup.state.enabled || |
| 42 | (sleep_state > (u32) dev->wakeup.sleep_state)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | continue; |
| 44 | |
| 45 | spin_unlock(&acpi_device_lock); |
| 46 | acpi_enable_wakeup_device_power(dev); |
| 47 | spin_lock(&acpi_device_lock); |
| 48 | } |
| 49 | spin_unlock(&acpi_device_lock); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * acpi_enable_wakeup_device - enable wakeup devices |
| 54 | * @sleep_state: ACPI state |
| 55 | * Enable all wakup devices's GPE |
| 56 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 57 | void acpi_enable_wakeup_device(u8 sleep_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 59 | struct list_head *node, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | /* |
| 62 | * Caution: this routine must be invoked when interrupt is disabled |
| 63 | * Refer ACPI2.0: P212 |
| 64 | */ |
| 65 | ACPI_FUNCTION_TRACE("acpi_enable_wakeup_device"); |
| 66 | spin_lock(&acpi_device_lock); |
| 67 | list_for_each_safe(node, next, &acpi_wakeup_device_list) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 68 | struct acpi_device *dev = container_of(node, |
| 69 | struct acpi_device, |
| 70 | wakeup_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | /* If users want to disable run-wake GPE, |
| 73 | * we only disable it for wake and leave it for runtime |
| 74 | */ |
| 75 | if (dev->wakeup.flags.run_wake && !dev->wakeup.state.enabled) { |
| 76 | spin_unlock(&acpi_device_lock); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 77 | acpi_set_gpe_type(dev->wakeup.gpe_device, |
| 78 | dev->wakeup.gpe_number, |
| 79 | ACPI_GPE_TYPE_RUNTIME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | /* Re-enable it, since set_gpe_type will disable it */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 81 | acpi_enable_gpe(dev->wakeup.gpe_device, |
| 82 | dev->wakeup.gpe_number, ACPI_ISR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | spin_lock(&acpi_device_lock); |
| 84 | continue; |
| 85 | } |
| 86 | |
| 87 | if (!dev->wakeup.flags.valid || |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 88 | !dev->wakeup.state.enabled || |
| 89 | (sleep_state > (u32) dev->wakeup.sleep_state)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | continue; |
| 91 | |
| 92 | spin_unlock(&acpi_device_lock); |
| 93 | /* run-wake GPE has been enabled */ |
| 94 | if (!dev->wakeup.flags.run_wake) |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 95 | acpi_enable_gpe(dev->wakeup.gpe_device, |
| 96 | dev->wakeup.gpe_number, ACPI_ISR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | dev->wakeup.state.active = 1; |
| 98 | spin_lock(&acpi_device_lock); |
| 99 | } |
| 100 | spin_unlock(&acpi_device_lock); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * acpi_disable_wakeup_device - disable devices' wakeup capability |
| 105 | * @sleep_state: ACPI state |
| 106 | * Disable all wakup devices's GPE and wakeup capability |
| 107 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 108 | void acpi_disable_wakeup_device(u8 sleep_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 110 | struct list_head *node, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
| 112 | ACPI_FUNCTION_TRACE("acpi_disable_wakeup_device"); |
| 113 | |
| 114 | spin_lock(&acpi_device_lock); |
| 115 | list_for_each_safe(node, next, &acpi_wakeup_device_list) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 116 | struct acpi_device *dev = container_of(node, |
| 117 | struct acpi_device, |
| 118 | wakeup_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
| 120 | if (dev->wakeup.flags.run_wake && !dev->wakeup.state.enabled) { |
| 121 | spin_unlock(&acpi_device_lock); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 122 | acpi_set_gpe_type(dev->wakeup.gpe_device, |
| 123 | dev->wakeup.gpe_number, |
| 124 | ACPI_GPE_TYPE_WAKE_RUN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | /* Re-enable it, since set_gpe_type will disable it */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 126 | acpi_enable_gpe(dev->wakeup.gpe_device, |
| 127 | dev->wakeup.gpe_number, ACPI_NOT_ISR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | spin_lock(&acpi_device_lock); |
| 129 | continue; |
| 130 | } |
| 131 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 132 | if (!dev->wakeup.flags.valid || |
| 133 | !dev->wakeup.state.active || |
| 134 | (sleep_state > (u32) dev->wakeup.sleep_state)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | continue; |
| 136 | |
| 137 | spin_unlock(&acpi_device_lock); |
| 138 | acpi_disable_wakeup_device_power(dev); |
| 139 | /* Never disable run-wake GPE */ |
| 140 | if (!dev->wakeup.flags.run_wake) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 141 | acpi_disable_gpe(dev->wakeup.gpe_device, |
| 142 | dev->wakeup.gpe_number, ACPI_NOT_ISR); |
| 143 | acpi_clear_gpe(dev->wakeup.gpe_device, |
| 144 | dev->wakeup.gpe_number, ACPI_NOT_ISR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | } |
| 146 | dev->wakeup.state.active = 0; |
| 147 | spin_lock(&acpi_device_lock); |
| 148 | } |
| 149 | spin_unlock(&acpi_device_lock); |
| 150 | } |
| 151 | |
| 152 | static int __init acpi_wakeup_device_init(void) |
| 153 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 154 | struct list_head *node, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
| 156 | if (acpi_disabled) |
| 157 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
| 159 | spin_lock(&acpi_device_lock); |
| 160 | list_for_each_safe(node, next, &acpi_wakeup_device_list) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 161 | struct acpi_device *dev = container_of(node, |
| 162 | struct acpi_device, |
| 163 | wakeup_list); |
| 164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | /* In case user doesn't load button driver */ |
| 166 | if (dev->wakeup.flags.run_wake && !dev->wakeup.state.enabled) { |
| 167 | spin_unlock(&acpi_device_lock); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 168 | acpi_set_gpe_type(dev->wakeup.gpe_device, |
| 169 | dev->wakeup.gpe_number, |
| 170 | ACPI_GPE_TYPE_WAKE_RUN); |
| 171 | acpi_enable_gpe(dev->wakeup.gpe_device, |
| 172 | dev->wakeup.gpe_number, ACPI_NOT_ISR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | dev->wakeup.state.enabled = 1; |
| 174 | spin_lock(&acpi_device_lock); |
| 175 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } |
| 177 | spin_unlock(&acpi_device_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | late_initcall(acpi_wakeup_device_init); |
| 183 | #endif |
| 184 | |
| 185 | /* |
Satoru Takeuchi | b7b09b1 | 2006-11-02 19:08:57 +0900 | [diff] [blame] | 186 | * Disable all wakeup GPEs before entering requested sleep state. |
| 187 | * @sleep_state: ACPI state |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | * Since acpi_enter_sleep_state() will disable all |
| 189 | * RUNTIME GPEs, we simply mark all GPES that |
Satoru Takeuchi | b7b09b1 | 2006-11-02 19:08:57 +0900 | [diff] [blame] | 190 | * are not enabled for wakeup from requested state as RUNTIME. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | */ |
Alexey Starikovskiy | 729b4d4 | 2005-12-01 04:29:00 -0500 | [diff] [blame] | 192 | void acpi_gpe_sleep_prepare(u32 sleep_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 194 | struct list_head *node, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
| 196 | list_for_each_safe(node, next, &acpi_wakeup_device_list) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 197 | struct acpi_device *dev = container_of(node, |
| 198 | struct acpi_device, |
| 199 | wakeup_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
Alexey Starikovskiy | 729b4d4 | 2005-12-01 04:29:00 -0500 | [diff] [blame] | 201 | /* The GPE can wakeup system from this state, don't touch it */ |
| 202 | if ((u32) dev->wakeup.sleep_state >= sleep_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | continue; |
| 204 | /* acpi_set_gpe_type will automatically disable GPE */ |
| 205 | acpi_set_gpe_type(dev->wakeup.gpe_device, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 206 | dev->wakeup.gpe_number, |
| 207 | ACPI_GPE_TYPE_RUNTIME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | } |
| 209 | } |