blob: ed8e41becf0c4997177697157ac8ee8a368d9ab1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Brown4be44fc2005-08-05 00:44:28 -040015ACPI_MODULE_NAME("wakeup_devices")
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Len Brown4be44fc2005-08-05 00:44:28 -040017extern struct list_head acpi_wakeup_device_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018extern spinlock_t acpi_device_lock;
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020/**
21 * acpi_enable_wakeup_device_prep - prepare wakeup devices
22 * @sleep_state: ACPI state
23 * Enable all wakup devices power if the devices' wakeup level
24 * is higher than requested sleep level
25 */
26
Len Brown4be44fc2005-08-05 00:44:28 -040027void acpi_enable_wakeup_device_prep(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
Len Brown4be44fc2005-08-05 00:44:28 -040029 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31 ACPI_FUNCTION_TRACE("acpi_enable_wakeup_device_prep");
32
33 spin_lock(&acpi_device_lock);
34 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
Len Brown4be44fc2005-08-05 00:44:28 -040035 struct acpi_device *dev = container_of(node,
36 struct acpi_device,
37 wakeup_list);
38
39 if (!dev->wakeup.flags.valid ||
40 !dev->wakeup.state.enabled ||
41 (sleep_state > (u32) dev->wakeup.sleep_state))
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 continue;
43
44 spin_unlock(&acpi_device_lock);
45 acpi_enable_wakeup_device_power(dev);
46 spin_lock(&acpi_device_lock);
47 }
48 spin_unlock(&acpi_device_lock);
49}
50
51/**
52 * acpi_enable_wakeup_device - enable wakeup devices
53 * @sleep_state: ACPI state
54 * Enable all wakup devices's GPE
55 */
Len Brown4be44fc2005-08-05 00:44:28 -040056void acpi_enable_wakeup_device(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Len Brown4be44fc2005-08-05 00:44:28 -040058 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 /*
61 * Caution: this routine must be invoked when interrupt is disabled
62 * Refer ACPI2.0: P212
63 */
64 ACPI_FUNCTION_TRACE("acpi_enable_wakeup_device");
65 spin_lock(&acpi_device_lock);
66 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
Alexey Starikovskiy9b039332007-09-26 19:47:30 +040067 struct acpi_device *dev =
68 container_of(node, struct acpi_device, wakeup_list);
69 if (!dev->wakeup.flags.valid)
70 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 /* If users want to disable run-wake GPE,
72 * we only disable it for wake and leave it for runtime
73 */
Alexey Starikovskiy9b039332007-09-26 19:47:30 +040074 if (!dev->wakeup.state.enabled ||
75 sleep_state > (u32) dev->wakeup.sleep_state) {
76 if (dev->wakeup.flags.run_wake) {
77 spin_unlock(&acpi_device_lock);
78 /* set_gpe_type will disable GPE, leave it like that */
79 acpi_set_gpe_type(dev->wakeup.gpe_device,
80 dev->wakeup.gpe_number,
81 ACPI_GPE_TYPE_RUNTIME);
82 spin_lock(&acpi_device_lock);
83 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 continue;
85 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 spin_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (!dev->wakeup.flags.run_wake)
Len Brown4be44fc2005-08-05 00:44:28 -040088 acpi_enable_gpe(dev->wakeup.gpe_device,
89 dev->wakeup.gpe_number, ACPI_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 spin_lock(&acpi_device_lock);
91 }
92 spin_unlock(&acpi_device_lock);
93}
94
95/**
96 * acpi_disable_wakeup_device - disable devices' wakeup capability
97 * @sleep_state: ACPI state
98 * Disable all wakup devices's GPE and wakeup capability
99 */
Len Brown4be44fc2005-08-05 00:44:28 -0400100void acpi_disable_wakeup_device(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Len Brown4be44fc2005-08-05 00:44:28 -0400102 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 ACPI_FUNCTION_TRACE("acpi_disable_wakeup_device");
105
106 spin_lock(&acpi_device_lock);
107 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
Alexey Starikovskiy9b039332007-09-26 19:47:30 +0400108 struct acpi_device *dev =
109 container_of(node, struct acpi_device, wakeup_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Alexey Starikovskiy9b039332007-09-26 19:47:30 +0400111 if (!dev->wakeup.flags.valid)
112 continue;
113 if (!dev->wakeup.state.enabled ||
114 sleep_state > (u32) dev->wakeup.sleep_state) {
115 if (dev->wakeup.flags.run_wake) {
116 spin_unlock(&acpi_device_lock);
117 acpi_set_gpe_type(dev->wakeup.gpe_device,
118 dev->wakeup.gpe_number,
119 ACPI_GPE_TYPE_WAKE_RUN);
120 /* Re-enable it, since set_gpe_type will disable it */
121 acpi_enable_gpe(dev->wakeup.gpe_device,
122 dev->wakeup.gpe_number, ACPI_NOT_ISR);
123 spin_lock(&acpi_device_lock);
124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 continue;
126 }
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 spin_unlock(&acpi_device_lock);
129 acpi_disable_wakeup_device_power(dev);
130 /* Never disable run-wake GPE */
131 if (!dev->wakeup.flags.run_wake) {
Len Brown4be44fc2005-08-05 00:44:28 -0400132 acpi_disable_gpe(dev->wakeup.gpe_device,
133 dev->wakeup.gpe_number, ACPI_NOT_ISR);
134 acpi_clear_gpe(dev->wakeup.gpe_device,
135 dev->wakeup.gpe_number, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 spin_lock(&acpi_device_lock);
138 }
139 spin_unlock(&acpi_device_lock);
140}
141
142static int __init acpi_wakeup_device_init(void)
143{
Len Brown4be44fc2005-08-05 00:44:28 -0400144 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 if (acpi_disabled)
147 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 spin_lock(&acpi_device_lock);
150 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
Len Brown4be44fc2005-08-05 00:44:28 -0400151 struct acpi_device *dev = container_of(node,
152 struct acpi_device,
153 wakeup_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 /* In case user doesn't load button driver */
Alexey Starikovskiy9b039332007-09-26 19:47:30 +0400155 if (!dev->wakeup.flags.run_wake || dev->wakeup.state.enabled)
156 continue;
157 spin_unlock(&acpi_device_lock);
158 acpi_set_gpe_type(dev->wakeup.gpe_device,
159 dev->wakeup.gpe_number,
160 ACPI_GPE_TYPE_WAKE_RUN);
161 acpi_enable_gpe(dev->wakeup.gpe_device,
162 dev->wakeup.gpe_number, ACPI_NOT_ISR);
163 dev->wakeup.state.enabled = 1;
164 spin_lock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
166 spin_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return 0;
168}
169
170late_initcall(acpi_wakeup_device_init);