blob: dea4c23df7648bb8ed3781bc455edb835a58b0b8 [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);
Rafael J. Wysocki77e76602008-07-07 03:33:34 +020045 acpi_enable_wakeup_device_power(dev, sleep_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 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);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +020069
Alexey Starikovskiy9b039332007-09-26 19:47:30 +040070 if (!dev->wakeup.flags.valid)
71 continue;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +020072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 /* If users want to disable run-wake GPE,
74 * we only disable it for wake and leave it for runtime
75 */
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +020076 if ((!dev->wakeup.state.enabled && !dev->wakeup.flags.prepared)
77 || sleep_state > (u32) dev->wakeup.sleep_state) {
Alexey Starikovskiy9b039332007-09-26 19:47:30 +040078 if (dev->wakeup.flags.run_wake) {
79 spin_unlock(&acpi_device_lock);
80 /* set_gpe_type will disable GPE, leave it like that */
81 acpi_set_gpe_type(dev->wakeup.gpe_device,
82 dev->wakeup.gpe_number,
83 ACPI_GPE_TYPE_RUNTIME);
84 spin_lock(&acpi_device_lock);
85 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 continue;
87 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 spin_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 if (!dev->wakeup.flags.run_wake)
Len Brown4be44fc2005-08-05 00:44:28 -040090 acpi_enable_gpe(dev->wakeup.gpe_device,
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +040091 dev->wakeup.gpe_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 spin_lock(&acpi_device_lock);
93 }
94 spin_unlock(&acpi_device_lock);
95}
96
97/**
98 * acpi_disable_wakeup_device - disable devices' wakeup capability
99 * @sleep_state: ACPI state
100 * Disable all wakup devices's GPE and wakeup capability
101 */
Len Brown4be44fc2005-08-05 00:44:28 -0400102void acpi_disable_wakeup_device(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Len Brown4be44fc2005-08-05 00:44:28 -0400104 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 ACPI_FUNCTION_TRACE("acpi_disable_wakeup_device");
107
108 spin_lock(&acpi_device_lock);
109 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
Alexey Starikovskiy9b039332007-09-26 19:47:30 +0400110 struct acpi_device *dev =
111 container_of(node, struct acpi_device, wakeup_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Alexey Starikovskiy9b039332007-09-26 19:47:30 +0400113 if (!dev->wakeup.flags.valid)
114 continue;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200115
116 if ((!dev->wakeup.state.enabled && !dev->wakeup.flags.prepared)
117 || sleep_state > (u32) dev->wakeup.sleep_state) {
Alexey Starikovskiy9b039332007-09-26 19:47:30 +0400118 if (dev->wakeup.flags.run_wake) {
119 spin_unlock(&acpi_device_lock);
120 acpi_set_gpe_type(dev->wakeup.gpe_device,
121 dev->wakeup.gpe_number,
122 ACPI_GPE_TYPE_WAKE_RUN);
123 /* Re-enable it, since set_gpe_type will disable it */
124 acpi_enable_gpe(dev->wakeup.gpe_device,
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +0400125 dev->wakeup.gpe_number);
Alexey Starikovskiy9b039332007-09-26 19:47:30 +0400126 spin_lock(&acpi_device_lock);
127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 continue;
129 }
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 spin_unlock(&acpi_device_lock);
132 acpi_disable_wakeup_device_power(dev);
133 /* Never disable run-wake GPE */
134 if (!dev->wakeup.flags.run_wake) {
Len Brown4be44fc2005-08-05 00:44:28 -0400135 acpi_disable_gpe(dev->wakeup.gpe_device,
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +0400136 dev->wakeup.gpe_number);
Len Brown4be44fc2005-08-05 00:44:28 -0400137 acpi_clear_gpe(dev->wakeup.gpe_device,
138 dev->wakeup.gpe_number, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 spin_lock(&acpi_device_lock);
141 }
142 spin_unlock(&acpi_device_lock);
143}
144
145static int __init acpi_wakeup_device_init(void)
146{
Len Brown4be44fc2005-08-05 00:44:28 -0400147 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 if (acpi_disabled)
150 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 spin_lock(&acpi_device_lock);
153 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
Len Brown4be44fc2005-08-05 00:44:28 -0400154 struct acpi_device *dev = container_of(node,
155 struct acpi_device,
156 wakeup_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 /* In case user doesn't load button driver */
Alexey Starikovskiy9b039332007-09-26 19:47:30 +0400158 if (!dev->wakeup.flags.run_wake || dev->wakeup.state.enabled)
159 continue;
160 spin_unlock(&acpi_device_lock);
161 acpi_set_gpe_type(dev->wakeup.gpe_device,
162 dev->wakeup.gpe_number,
163 ACPI_GPE_TYPE_WAKE_RUN);
164 acpi_enable_gpe(dev->wakeup.gpe_device,
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +0400165 dev->wakeup.gpe_number);
Alexey Starikovskiy9b039332007-09-26 19:47:30 +0400166 dev->wakeup.state.enabled = 1;
167 spin_lock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
169 spin_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return 0;
171}
172
173late_initcall(acpi_wakeup_device_init);