wakeup.c 5.7 KB

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