blob: 3f29fd53e9a60223f8599d4102ef1fe5422dc89d [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>
Bjorn Helgaase60cc7a2009-03-13 12:08:26 -060011
12#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "sleep.h"
14
15#define _COMPONENT ACPI_SYSTEM_COMPONENT
Len Brown4be44fc2005-08-05 00:44:28 -040016ACPI_MODULE_NAME("wakeup_devices")
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Len Brown4be44fc2005-08-05 00:44:28 -040018extern struct list_head acpi_wakeup_device_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070019extern spinlock_t acpi_device_lock;
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021/**
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 Brown4be44fc2005-08-05 00:44:28 -040028void acpi_enable_wakeup_device_prep(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Len Brown4be44fc2005-08-05 00:44:28 -040030 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 spin_lock(&acpi_device_lock);
33 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
Len Brown4be44fc2005-08-05 00:44:28 -040034 struct acpi_device *dev = container_of(node,
35 struct acpi_device,
36 wakeup_list);
37
38 if (!dev->wakeup.flags.valid ||
39 !dev->wakeup.state.enabled ||
40 (sleep_state > (u32) dev->wakeup.sleep_state))
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 continue;
42
43 spin_unlock(&acpi_device_lock);
Rafael J. Wysocki77e76602008-07-07 03:33:34 +020044 acpi_enable_wakeup_device_power(dev, sleep_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 spin_lock(&acpi_device_lock);
46 }
47 spin_unlock(&acpi_device_lock);
48}
49
50/**
51 * acpi_enable_wakeup_device - enable wakeup devices
52 * @sleep_state: ACPI state
53 * Enable all wakup devices's GPE
54 */
Len Brown4be44fc2005-08-05 00:44:28 -040055void acpi_enable_wakeup_device(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Len Brown4be44fc2005-08-05 00:44:28 -040057 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 /*
60 * Caution: this routine must be invoked when interrupt is disabled
61 * Refer ACPI2.0: P212
62 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 spin_lock(&acpi_device_lock);
64 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
Alexey Starikovskiy9b039332007-09-26 19:47:30 +040065 struct acpi_device *dev =
66 container_of(node, struct acpi_device, wakeup_list);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +020067
Alexey Starikovskiy9b039332007-09-26 19:47:30 +040068 if (!dev->wakeup.flags.valid)
69 continue;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +020070
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 */
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +020074 if ((!dev->wakeup.state.enabled && !dev->wakeup.flags.prepared)
75 || sleep_state > (u32) dev->wakeup.sleep_state) {
Alexey Starikovskiy9b039332007-09-26 19:47:30 +040076 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,
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +040089 dev->wakeup.gpe_number);
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 spin_lock(&acpi_device_lock);
105 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
Alexey Starikovskiy9b039332007-09-26 19:47:30 +0400106 struct acpi_device *dev =
107 container_of(node, struct acpi_device, wakeup_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Alexey Starikovskiy9b039332007-09-26 19:47:30 +0400109 if (!dev->wakeup.flags.valid)
110 continue;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200111
112 if ((!dev->wakeup.state.enabled && !dev->wakeup.flags.prepared)
113 || sleep_state > (u32) dev->wakeup.sleep_state) {
Alexey Starikovskiy9b039332007-09-26 19:47:30 +0400114 if (dev->wakeup.flags.run_wake) {
115 spin_unlock(&acpi_device_lock);
116 acpi_set_gpe_type(dev->wakeup.gpe_device,
117 dev->wakeup.gpe_number,
118 ACPI_GPE_TYPE_WAKE_RUN);
119 /* Re-enable it, since set_gpe_type will disable it */
120 acpi_enable_gpe(dev->wakeup.gpe_device,
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +0400121 dev->wakeup.gpe_number);
Alexey Starikovskiy9b039332007-09-26 19:47:30 +0400122 spin_lock(&acpi_device_lock);
123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 continue;
125 }
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 spin_unlock(&acpi_device_lock);
128 acpi_disable_wakeup_device_power(dev);
129 /* Never disable run-wake GPE */
130 if (!dev->wakeup.flags.run_wake) {
Len Brown4be44fc2005-08-05 00:44:28 -0400131 acpi_disable_gpe(dev->wakeup.gpe_device,
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +0400132 dev->wakeup.gpe_number);
Len Brown4be44fc2005-08-05 00:44:28 -0400133 acpi_clear_gpe(dev->wakeup.gpe_device,
134 dev->wakeup.gpe_number, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 spin_lock(&acpi_device_lock);
137 }
138 spin_unlock(&acpi_device_lock);
139}
140
141static int __init acpi_wakeup_device_init(void)
142{
Len Brown4be44fc2005-08-05 00:44:28 -0400143 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 if (acpi_disabled)
146 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 spin_lock(&acpi_device_lock);
149 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
Len Brown4be44fc2005-08-05 00:44:28 -0400150 struct acpi_device *dev = container_of(node,
151 struct acpi_device,
152 wakeup_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 /* In case user doesn't load button driver */
Alexey Starikovskiy9b039332007-09-26 19:47:30 +0400154 if (!dev->wakeup.flags.run_wake || dev->wakeup.state.enabled)
155 continue;
156 spin_unlock(&acpi_device_lock);
157 acpi_set_gpe_type(dev->wakeup.gpe_device,
158 dev->wakeup.gpe_number,
159 ACPI_GPE_TYPE_WAKE_RUN);
160 acpi_enable_gpe(dev->wakeup.gpe_device,
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +0400161 dev->wakeup.gpe_number);
Alexey Starikovskiy9b039332007-09-26 19:47:30 +0400162 dev->wakeup.state.enabled = 1;
163 spin_lock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
165 spin_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return 0;
167}
168
169late_initcall(acpi_wakeup_device_init);