autosleep.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * kernel/power/autosleep.c
  3. *
  4. * Opportunistic sleep support.
  5. *
  6. * Copyright (C) 2012 Rafael J. Wysocki <rjw@sisk.pl>
  7. */
  8. #include <linux/device.h>
  9. #include <linux/mutex.h>
  10. #include <linux/pm_wakeup.h>
  11. #include <mtk_hibernate_core.h>
  12. #include "power.h"
  13. #define HIB_AUTOSLEEP_DEBUG 1
  14. #define _TAG_HIB_M "HIB/AUTOSLEEP"
  15. #if (HIB_AUTOSLEEP_DEBUG)
  16. #define hib_autoslp_log(fmt, ...) pr_warn("[%s][%s]" fmt, _TAG_HIB_M, __func__, ##__VA_ARGS__)
  17. #else
  18. #define hib_autoslp_log(fmt, ...)
  19. #endif
  20. #define hib_autoslp_warn(fmt, ...) pr_warn("[%s][%s]" fmt, _TAG_HIB_M, __func__, ##__VA_ARGS__)
  21. static suspend_state_t autosleep_state;
  22. static struct workqueue_struct *autosleep_wq;
  23. /*
  24. * Note: it is only safe to mutex_lock(&autosleep_lock) if a wakeup_source
  25. * is active, otherwise a deadlock with try_to_suspend() is possible.
  26. * Alternatively mutex_lock_interruptible() can be used. This will then fail
  27. * if an auto_sleep cycle tries to freeze processes.
  28. */
  29. static DEFINE_MUTEX(autosleep_lock);
  30. static struct wakeup_source *autosleep_ws;
  31. static void try_to_suspend(struct work_struct *work)
  32. {
  33. unsigned int initial_count, final_count;
  34. if (!pm_get_wakeup_count(&initial_count, true))
  35. goto out;
  36. mutex_lock(&autosleep_lock);
  37. if (!pm_save_wakeup_count(initial_count) ||
  38. system_state != SYSTEM_RUNNING) {
  39. mutex_unlock(&autosleep_lock);
  40. goto out;
  41. }
  42. if (autosleep_state == PM_SUSPEND_ON) {
  43. #ifdef CONFIG_MTK_HIBERNATION
  44. system_is_hibernating = false;
  45. #endif
  46. mutex_unlock(&autosleep_lock);
  47. return;
  48. }
  49. #ifdef CONFIG_MTK_HIBERNATION
  50. if (autosleep_state >= PM_SUSPEND_MAX) {
  51. mtk_hibernate_via_autosleep(&autosleep_state);
  52. } else {
  53. hib_autoslp_log("pm_suspend: state(%d)\n", autosleep_state);
  54. if (!system_is_hibernating) {
  55. hib_autoslp_warn("calling pm_suspend() state(%d)\n", autosleep_state);
  56. pm_suspend(autosleep_state);
  57. } else {
  58. hib_autoslp_warn("system is hibernating: so changing state(%d->%d)\n",
  59. autosleep_state, PM_SUSPEND_MAX);
  60. autosleep_state = PM_SUSPEND_MAX;
  61. }
  62. }
  63. #else /* !CONFIG_MTK_HIBERNATION */
  64. if (autosleep_state >= PM_SUSPEND_MAX)
  65. hibernate();
  66. else
  67. pm_suspend(autosleep_state);
  68. #endif /* CONFIG_MTK_HIBERNATION */
  69. mutex_unlock(&autosleep_lock);
  70. if (!pm_get_wakeup_count(&final_count, false))
  71. goto out;
  72. /*
  73. * If the wakeup occured for an unknown reason, wait to prevent the
  74. * system from trying to suspend and waking up in a tight loop.
  75. */
  76. if (final_count == initial_count)
  77. schedule_timeout_uninterruptible(HZ / 2);
  78. out:
  79. queue_up_suspend_work();
  80. }
  81. static DECLARE_WORK(suspend_work, try_to_suspend);
  82. void queue_up_suspend_work(void)
  83. {
  84. if (autosleep_state > PM_SUSPEND_ON)
  85. queue_work(autosleep_wq, &suspend_work);
  86. }
  87. suspend_state_t pm_autosleep_state(void)
  88. {
  89. return autosleep_state;
  90. }
  91. int pm_autosleep_lock(void)
  92. {
  93. return mutex_lock_interruptible(&autosleep_lock);
  94. }
  95. void pm_autosleep_unlock(void)
  96. {
  97. mutex_unlock(&autosleep_lock);
  98. }
  99. int pm_autosleep_set_state(suspend_state_t state)
  100. {
  101. #ifndef CONFIG_HIBERNATION
  102. if (state >= PM_SUSPEND_MAX)
  103. return -EINVAL;
  104. #endif
  105. __pm_stay_awake(autosleep_ws);
  106. mutex_lock(&autosleep_lock);
  107. autosleep_state = state;
  108. __pm_relax(autosleep_ws);
  109. if (state > PM_SUSPEND_ON) {
  110. pm_wakep_autosleep_enabled(true);
  111. queue_up_suspend_work();
  112. } else {
  113. pm_wakep_autosleep_enabled(false);
  114. }
  115. mutex_unlock(&autosleep_lock);
  116. return 0;
  117. }
  118. int __init pm_autosleep_init(void)
  119. {
  120. autosleep_ws = wakeup_source_register("autosleep");
  121. if (!autosleep_ws)
  122. return -ENOMEM;
  123. autosleep_wq = alloc_ordered_workqueue("autosleep", 0);
  124. if (autosleep_wq)
  125. return 0;
  126. wakeup_source_unregister(autosleep_ws);
  127. return -ENOMEM;
  128. }