blob: 5a6b2fa9f27b23756f857fabef19beba32b68758 [file] [log] [blame]
Arve Hjønnevågc1783f52008-10-07 20:48:01 -07001/* kernel/power/earlysuspend.c
2 *
3 * Copyright (C) 2005-2008 Google, Inc.
4 *
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 */
15
16#include <linux/earlysuspend.h>
17#include <linux/module.h>
18#include <linux/mutex.h>
19#include <linux/rtc.h>
Arve Hjønnevågc1783f52008-10-07 20:48:01 -070020#include <linux/wakelock.h>
21#include <linux/workqueue.h>
22
23#include "power.h"
24
25enum {
26 DEBUG_USER_STATE = 1U << 0,
27 DEBUG_SUSPEND = 1U << 2,
Erik Gillingfec502d2011-07-21 14:07:45 -070028 DEBUG_VERBOSE = 1U << 3,
Arve Hjønnevågc1783f52008-10-07 20:48:01 -070029};
30static int debug_mask = DEBUG_USER_STATE;
31module_param_named(debug_mask, debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP);
32
33static DEFINE_MUTEX(early_suspend_lock);
34static LIST_HEAD(early_suspend_handlers);
35static void early_suspend(struct work_struct *work);
36static void late_resume(struct work_struct *work);
37static DECLARE_WORK(early_suspend_work, early_suspend);
38static DECLARE_WORK(late_resume_work, late_resume);
39static DEFINE_SPINLOCK(state_lock);
40enum {
41 SUSPEND_REQUESTED = 0x1,
42 SUSPENDED = 0x2,
43 SUSPEND_REQUESTED_AND_SUSPENDED = SUSPEND_REQUESTED | SUSPENDED,
44};
45static int state;
46
47void register_early_suspend(struct early_suspend *handler)
48{
49 struct list_head *pos;
50
51 mutex_lock(&early_suspend_lock);
52 list_for_each(pos, &early_suspend_handlers) {
53 struct early_suspend *e;
54 e = list_entry(pos, struct early_suspend, link);
55 if (e->level > handler->level)
56 break;
57 }
58 list_add_tail(&handler->link, pos);
59 if ((state & SUSPENDED) && handler->suspend)
60 handler->suspend(handler);
61 mutex_unlock(&early_suspend_lock);
62}
63EXPORT_SYMBOL(register_early_suspend);
64
65void unregister_early_suspend(struct early_suspend *handler)
66{
67 mutex_lock(&early_suspend_lock);
68 list_del(&handler->link);
69 mutex_unlock(&early_suspend_lock);
70}
71EXPORT_SYMBOL(unregister_early_suspend);
72
73static void early_suspend(struct work_struct *work)
74{
75 struct early_suspend *pos;
76 unsigned long irqflags;
77 int abort = 0;
78
79 mutex_lock(&early_suspend_lock);
80 spin_lock_irqsave(&state_lock, irqflags);
81 if (state == SUSPEND_REQUESTED)
82 state |= SUSPENDED;
83 else
84 abort = 1;
85 spin_unlock_irqrestore(&state_lock, irqflags);
86
87 if (abort) {
88 if (debug_mask & DEBUG_SUSPEND)
89 pr_info("early_suspend: abort, state %d\n", state);
90 mutex_unlock(&early_suspend_lock);
91 goto abort;
92 }
93
94 if (debug_mask & DEBUG_SUSPEND)
95 pr_info("early_suspend: call handlers\n");
96 list_for_each_entry(pos, &early_suspend_handlers, link) {
Erik Gillingfec502d2011-07-21 14:07:45 -070097 if (pos->suspend != NULL) {
98 if (debug_mask & DEBUG_VERBOSE)
99 pr_info("early_suspend: calling %pf\n", pos->suspend);
Arve Hjønnevågc1783f52008-10-07 20:48:01 -0700100 pos->suspend(pos);
Erik Gillingfec502d2011-07-21 14:07:45 -0700101 }
Arve Hjønnevågc1783f52008-10-07 20:48:01 -0700102 }
103 mutex_unlock(&early_suspend_lock);
104
Bryan Huntsmand074fa22011-11-16 13:52:50 -0800105 suspend_sys_sync_queue();
Arve Hjønnevågc1783f52008-10-07 20:48:01 -0700106abort:
107 spin_lock_irqsave(&state_lock, irqflags);
108 if (state == SUSPEND_REQUESTED_AND_SUSPENDED)
109 wake_unlock(&main_wake_lock);
110 spin_unlock_irqrestore(&state_lock, irqflags);
111}
112
113static void late_resume(struct work_struct *work)
114{
115 struct early_suspend *pos;
116 unsigned long irqflags;
117 int abort = 0;
118
119 mutex_lock(&early_suspend_lock);
120 spin_lock_irqsave(&state_lock, irqflags);
121 if (state == SUSPENDED)
122 state &= ~SUSPENDED;
123 else
124 abort = 1;
125 spin_unlock_irqrestore(&state_lock, irqflags);
126
127 if (abort) {
128 if (debug_mask & DEBUG_SUSPEND)
129 pr_info("late_resume: abort, state %d\n", state);
130 goto abort;
131 }
132 if (debug_mask & DEBUG_SUSPEND)
133 pr_info("late_resume: call handlers\n");
Erik Gillingfec502d2011-07-21 14:07:45 -0700134 list_for_each_entry_reverse(pos, &early_suspend_handlers, link) {
135 if (pos->resume != NULL) {
136 if (debug_mask & DEBUG_VERBOSE)
137 pr_info("late_resume: calling %pf\n", pos->resume);
138
Arve Hjønnevågc1783f52008-10-07 20:48:01 -0700139 pos->resume(pos);
Erik Gillingfec502d2011-07-21 14:07:45 -0700140 }
141 }
Arve Hjønnevågc1783f52008-10-07 20:48:01 -0700142 if (debug_mask & DEBUG_SUSPEND)
143 pr_info("late_resume: done\n");
144abort:
145 mutex_unlock(&early_suspend_lock);
146}
147
148void request_suspend_state(suspend_state_t new_state)
149{
150 unsigned long irqflags;
151 int old_sleep;
152
153 spin_lock_irqsave(&state_lock, irqflags);
154 old_sleep = state & SUSPEND_REQUESTED;
155 if (debug_mask & DEBUG_USER_STATE) {
156 struct timespec ts;
157 struct rtc_time tm;
158 getnstimeofday(&ts);
159 rtc_time_to_tm(ts.tv_sec, &tm);
160 pr_info("request_suspend_state: %s (%d->%d) at %lld "
161 "(%d-%02d-%02d %02d:%02d:%02d.%09lu UTC)\n",
162 new_state != PM_SUSPEND_ON ? "sleep" : "wakeup",
163 requested_suspend_state, new_state,
164 ktime_to_ns(ktime_get()),
165 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
166 tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec);
167 }
168 if (!old_sleep && new_state != PM_SUSPEND_ON) {
169 state |= SUSPEND_REQUESTED;
170 queue_work(suspend_work_queue, &early_suspend_work);
171 } else if (old_sleep && new_state == PM_SUSPEND_ON) {
172 state &= ~SUSPEND_REQUESTED;
173 wake_lock(&main_wake_lock);
174 queue_work(suspend_work_queue, &late_resume_work);
175 }
176 requested_suspend_state = new_state;
177 spin_unlock_irqrestore(&state_lock, irqflags);
178}
179
180suspend_state_t get_suspend_state(void)
181{
182 return requested_suspend_state;
183}