blob: 2d34806d45dd704cf43222bec4ed50e8c33839c0 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include "sleep.h"
12
13#define _COMPONENT ACPI_SYSTEM_COMPONENT
Len Brown4be44fc2005-08-05 00:44:28 -040014ACPI_MODULE_NAME("wakeup_devices")
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Len Brown4be44fc2005-08-05 00:44:28 -040016extern struct list_head acpi_wakeup_device_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017extern spinlock_t acpi_device_lock;
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019/**
20 * acpi_enable_wakeup_device_prep - prepare wakeup devices
21 * @sleep_state: ACPI state
22 * Enable all wakup devices power if the devices' wakeup level
23 * is higher than requested sleep level
24 */
25
Len Brown4be44fc2005-08-05 00:44:28 -040026void acpi_enable_wakeup_device_prep(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
Len Brown4be44fc2005-08-05 00:44:28 -040028 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 spin_lock(&acpi_device_lock);
31 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
Len Brown4be44fc2005-08-05 00:44:28 -040032 struct acpi_device *dev = container_of(node,
33 struct acpi_device,
34 wakeup_list);
35
36 if (!dev->wakeup.flags.valid ||
37 !dev->wakeup.state.enabled ||
38 (sleep_state > (u32) dev->wakeup.sleep_state))
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 continue;
40
41 spin_unlock(&acpi_device_lock);
Rafael J. Wysocki77e76602008-07-07 03:33:34 +020042 acpi_enable_wakeup_device_power(dev, sleep_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 spin_lock(&acpi_device_lock);
44 }
45 spin_unlock(&acpi_device_lock);
46}
47
48/**
49 * acpi_enable_wakeup_device - enable wakeup devices
50 * @sleep_state: ACPI state
51 * Enable all wakup devices's GPE
52 */
Len Brown4be44fc2005-08-05 00:44:28 -040053void acpi_enable_wakeup_device(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Len Brown4be44fc2005-08-05 00:44:28 -040055 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57 /*
58 * Caution: this routine must be invoked when interrupt is disabled
59 * Refer ACPI2.0: P212
60 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 spin_lock(&acpi_device_lock);
62 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
Alexey Starikovskiy9b039332007-09-26 19:47:30 +040063 struct acpi_device *dev =
64 container_of(node, struct acpi_device, wakeup_list);
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +020065
Alexey Starikovskiy9b039332007-09-26 19:47:30 +040066 if (!dev->wakeup.flags.valid)
67 continue;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +020068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 /* If users want to disable run-wake GPE,
70 * we only disable it for wake and leave it for runtime
71 */
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +020072 if ((!dev->wakeup.state.enabled && !dev->wakeup.flags.prepared)
73 || sleep_state > (u32) dev->wakeup.sleep_state) {
Alexey Starikovskiy9b039332007-09-26 19:47:30 +040074 if (dev->wakeup.flags.run_wake) {
75 spin_unlock(&acpi_device_lock);
76 /* set_gpe_type will disable GPE, leave it like that */
77 acpi_set_gpe_type(dev->wakeup.gpe_device,
78 dev->wakeup.gpe_number,
79 ACPI_GPE_TYPE_RUNTIME);
80 spin_lock(&acpi_device_lock);
81 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 continue;
83 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 spin_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 if (!dev->wakeup.flags.run_wake)
Len Brown4be44fc2005-08-05 00:44:28 -040086 acpi_enable_gpe(dev->wakeup.gpe_device,
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +040087 dev->wakeup.gpe_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 spin_lock(&acpi_device_lock);
89 }
90 spin_unlock(&acpi_device_lock);
91}
92
93/**
94 * acpi_disable_wakeup_device - disable devices' wakeup capability
95 * @sleep_state: ACPI state
96 * Disable all wakup devices's GPE and wakeup capability
97 */
Len Brown4be44fc2005-08-05 00:44:28 -040098void acpi_disable_wakeup_device(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Len Brown4be44fc2005-08-05 00:44:28 -0400100 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 spin_lock(&acpi_device_lock);
103 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
Alexey Starikovskiy9b039332007-09-26 19:47:30 +0400104 struct acpi_device *dev =
105 container_of(node, struct acpi_device, wakeup_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Alexey Starikovskiy9b039332007-09-26 19:47:30 +0400107 if (!dev->wakeup.flags.valid)
108 continue;
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200109
110 if ((!dev->wakeup.state.enabled && !dev->wakeup.flags.prepared)
111 || sleep_state > (u32) dev->wakeup.sleep_state) {
Alexey Starikovskiy9b039332007-09-26 19:47:30 +0400112 if (dev->wakeup.flags.run_wake) {
113 spin_unlock(&acpi_device_lock);
114 acpi_set_gpe_type(dev->wakeup.gpe_device,
115 dev->wakeup.gpe_number,
116 ACPI_GPE_TYPE_WAKE_RUN);
117 /* Re-enable it, since set_gpe_type will disable it */
118 acpi_enable_gpe(dev->wakeup.gpe_device,
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +0400119 dev->wakeup.gpe_number);
Alexey Starikovskiy9b039332007-09-26 19:47:30 +0400120 spin_lock(&acpi_device_lock);
121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 continue;
123 }
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 spin_unlock(&acpi_device_lock);
126 acpi_disable_wakeup_device_power(dev);
127 /* Never disable run-wake GPE */
128 if (!dev->wakeup.flags.run_wake) {
Len Brown4be44fc2005-08-05 00:44:28 -0400129 acpi_disable_gpe(dev->wakeup.gpe_device,
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +0400130 dev->wakeup.gpe_number);
Len Brown4be44fc2005-08-05 00:44:28 -0400131 acpi_clear_gpe(dev->wakeup.gpe_device,
132 dev->wakeup.gpe_number, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 spin_lock(&acpi_device_lock);
135 }
136 spin_unlock(&acpi_device_lock);
137}
138
139static int __init acpi_wakeup_device_init(void)
140{
Len Brown4be44fc2005-08-05 00:44:28 -0400141 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 if (acpi_disabled)
144 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 spin_lock(&acpi_device_lock);
147 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
Len Brown4be44fc2005-08-05 00:44:28 -0400148 struct acpi_device *dev = container_of(node,
149 struct acpi_device,
150 wakeup_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 /* In case user doesn't load button driver */
Alexey Starikovskiy9b039332007-09-26 19:47:30 +0400152 if (!dev->wakeup.flags.run_wake || dev->wakeup.state.enabled)
153 continue;
154 spin_unlock(&acpi_device_lock);
155 acpi_set_gpe_type(dev->wakeup.gpe_device,
156 dev->wakeup.gpe_number,
157 ACPI_GPE_TYPE_WAKE_RUN);
158 acpi_enable_gpe(dev->wakeup.gpe_device,
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +0400159 dev->wakeup.gpe_number);
Alexey Starikovskiy9b039332007-09-26 19:47:30 +0400160 dev->wakeup.state.enabled = 1;
161 spin_lock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
163 spin_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return 0;
165}
166
167late_initcall(acpi_wakeup_device_init);