wakeup.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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, sleep_state);
  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 =
  57. container_of(node, struct acpi_device, wakeup_list);
  58. if (!dev->wakeup.flags.valid)
  59. continue;
  60. /* If users want to disable run-wake GPE,
  61. * we only disable it for wake and leave it for runtime
  62. */
  63. if ((!dev->wakeup.state.enabled && !dev->wakeup.flags.prepared)
  64. || sleep_state > (u32) dev->wakeup.sleep_state) {
  65. if (dev->wakeup.flags.run_wake) {
  66. spin_unlock(&acpi_device_lock);
  67. /* set_gpe_type will disable GPE, leave it like that */
  68. acpi_set_gpe_type(dev->wakeup.gpe_device,
  69. dev->wakeup.gpe_number,
  70. ACPI_GPE_TYPE_RUNTIME);
  71. spin_lock(&acpi_device_lock);
  72. }
  73. continue;
  74. }
  75. spin_unlock(&acpi_device_lock);
  76. if (!dev->wakeup.flags.run_wake)
  77. acpi_enable_gpe(dev->wakeup.gpe_device,
  78. dev->wakeup.gpe_number, ACPI_ISR);
  79. spin_lock(&acpi_device_lock);
  80. }
  81. spin_unlock(&acpi_device_lock);
  82. }
  83. /**
  84. * acpi_disable_wakeup_device - disable devices' wakeup capability
  85. * @sleep_state: ACPI state
  86. * Disable all wakup devices's GPE and wakeup capability
  87. */
  88. void acpi_disable_wakeup_device(u8 sleep_state)
  89. {
  90. struct list_head *node, *next;
  91. ACPI_FUNCTION_TRACE("acpi_disable_wakeup_device");
  92. spin_lock(&acpi_device_lock);
  93. list_for_each_safe(node, next, &acpi_wakeup_device_list) {
  94. struct acpi_device *dev =
  95. container_of(node, struct acpi_device, wakeup_list);
  96. if (!dev->wakeup.flags.valid)
  97. continue;
  98. if ((!dev->wakeup.state.enabled && !dev->wakeup.flags.prepared)
  99. || sleep_state > (u32) dev->wakeup.sleep_state) {
  100. if (dev->wakeup.flags.run_wake) {
  101. spin_unlock(&acpi_device_lock);
  102. acpi_set_gpe_type(dev->wakeup.gpe_device,
  103. dev->wakeup.gpe_number,
  104. ACPI_GPE_TYPE_WAKE_RUN);
  105. /* Re-enable it, since set_gpe_type will disable it */
  106. acpi_enable_gpe(dev->wakeup.gpe_device,
  107. dev->wakeup.gpe_number, ACPI_NOT_ISR);
  108. spin_lock(&acpi_device_lock);
  109. }
  110. continue;
  111. }
  112. spin_unlock(&acpi_device_lock);
  113. acpi_disable_wakeup_device_power(dev);
  114. /* Never disable run-wake GPE */
  115. if (!dev->wakeup.flags.run_wake) {
  116. acpi_disable_gpe(dev->wakeup.gpe_device,
  117. dev->wakeup.gpe_number, ACPI_NOT_ISR);
  118. acpi_clear_gpe(dev->wakeup.gpe_device,
  119. dev->wakeup.gpe_number, ACPI_NOT_ISR);
  120. }
  121. spin_lock(&acpi_device_lock);
  122. }
  123. spin_unlock(&acpi_device_lock);
  124. }
  125. static int __init acpi_wakeup_device_init(void)
  126. {
  127. struct list_head *node, *next;
  128. if (acpi_disabled)
  129. return 0;
  130. spin_lock(&acpi_device_lock);
  131. list_for_each_safe(node, next, &acpi_wakeup_device_list) {
  132. struct acpi_device *dev = container_of(node,
  133. struct acpi_device,
  134. wakeup_list);
  135. /* In case user doesn't load button driver */
  136. if (!dev->wakeup.flags.run_wake || dev->wakeup.state.enabled)
  137. continue;
  138. spin_unlock(&acpi_device_lock);
  139. acpi_set_gpe_type(dev->wakeup.gpe_device,
  140. dev->wakeup.gpe_number,
  141. ACPI_GPE_TYPE_WAKE_RUN);
  142. acpi_enable_gpe(dev->wakeup.gpe_device,
  143. dev->wakeup.gpe_number, ACPI_NOT_ISR);
  144. dev->wakeup.state.enabled = 1;
  145. spin_lock(&acpi_device_lock);
  146. }
  147. spin_unlock(&acpi_device_lock);
  148. return 0;
  149. }
  150. late_initcall(acpi_wakeup_device_init);