c165b33d1d3138b31cfc7dc459487d0ce388f625
[linux-2.6.git] / kernel / power / earlysuspend.c
1 /* 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>
20 #include <linux/syscalls.h> /* sys_sync */
21 #include <linux/wakelock.h>
22 #include <linux/workqueue.h>
23
24 #include "power.h"
25
26 enum {
27         DEBUG_USER_STATE = 1U << 0,
28         DEBUG_SUSPEND = 1U << 2,
29         DEBUG_VERBOSE = 1U << 3,
30 };
31 static int debug_mask = DEBUG_USER_STATE;
32 module_param_named(debug_mask, debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP);
33
34 suspend_state_t requested_suspend_state = PM_SUSPEND_MEM;
35 static DEFINE_MUTEX(early_suspend_lock);
36 static LIST_HEAD(early_suspend_handlers);
37 static void early_suspend(struct work_struct *work);
38 static void late_resume(struct work_struct *work);
39 static void suspend(struct work_struct *work);
40 static DECLARE_WORK(early_suspend_work, early_suspend);
41 static DECLARE_WORK(late_resume_work, late_resume);
42 static DECLARE_DELAYED_WORK(suspend_work, suspend);
43 static DEFINE_SPINLOCK(state_lock);
44 enum {
45         SUSPEND_REQUESTED = 0x1,
46         SUSPENDED = 0x2,
47         SUSPEND_REQUESTED_AND_SUSPENDED = SUSPEND_REQUESTED | SUSPENDED,
48 };
49 static int state;
50
51 void register_early_suspend(struct early_suspend *handler)
52 {
53         struct list_head *pos;
54
55         mutex_lock(&early_suspend_lock);
56         list_for_each(pos, &early_suspend_handlers) {
57                 struct early_suspend *e;
58                 e = list_entry(pos, struct early_suspend, link);
59                 if (e->level > handler->level)
60                         break;
61         }
62         list_add_tail(&handler->link, pos);
63         if ((state & SUSPENDED) && handler->suspend)
64                 handler->suspend(handler);
65         mutex_unlock(&early_suspend_lock);
66 }
67 EXPORT_SYMBOL(register_early_suspend);
68
69 void unregister_early_suspend(struct early_suspend *handler)
70 {
71         mutex_lock(&early_suspend_lock);
72         list_del(&handler->link);
73         mutex_unlock(&early_suspend_lock);
74 }
75 EXPORT_SYMBOL(unregister_early_suspend);
76
77 static void suspend(struct work_struct *work)
78 {
79         if (debug_mask & DEBUG_SUSPEND)
80                 pr_info("early_suspend: suspend\n");
81         pm_suspend(requested_suspend_state);
82
83         /*
84          * Check if we made it till late_resume. If not then this is not
85          * actual resume. Suspend again.
86          */
87         mutex_lock(&early_suspend_lock);
88         if (state & SUSPENDED)
89                 queue_delayed_work(suspend_work_queue, &suspend_work, HZ);
90         mutex_unlock(&early_suspend_lock);
91 }
92
93 static void early_suspend(struct work_struct *work)
94 {
95         struct early_suspend *pos;
96         unsigned long irqflags;
97         int abort = 0;
98
99         mutex_lock(&early_suspend_lock);
100         spin_lock_irqsave(&state_lock, irqflags);
101         if (state == SUSPEND_REQUESTED)
102                 state |= SUSPENDED;
103         else
104                 abort = 1;
105         spin_unlock_irqrestore(&state_lock, irqflags);
106
107         if (abort) {
108                 if (debug_mask & DEBUG_SUSPEND)
109                         pr_info("early_suspend: abort, state %d\n", state);
110                 mutex_unlock(&early_suspend_lock);
111                 return;
112         }
113
114         if (debug_mask & DEBUG_SUSPEND)
115                 pr_info("early_suspend: call handlers\n");
116         list_for_each_entry(pos, &early_suspend_handlers, link) {
117                 if (pos->suspend != NULL) {
118                         if (debug_mask & DEBUG_VERBOSE)
119                                 pr_info("early_suspend: calling %pf\n", pos->suspend);
120                         pos->suspend(pos);
121                 }
122         }
123
124         if (debug_mask & DEBUG_SUSPEND)
125                 pr_info("early_suspend: sync\n");
126
127         sys_sync();
128         queue_delayed_work(suspend_work_queue, &suspend_work, HZ);
129         mutex_unlock(&early_suspend_lock);
130 }
131
132 static void late_resume(struct work_struct *work)
133 {
134         struct early_suspend *pos;
135         unsigned long irqflags;
136         int abort = 0;
137
138         mutex_lock(&early_suspend_lock);
139         spin_lock_irqsave(&state_lock, irqflags);
140         if (state == SUSPENDED)
141                 state &= ~SUSPENDED;
142         else
143                 abort = 1;
144         spin_unlock_irqrestore(&state_lock, irqflags);
145
146         if (abort) {
147                 if (debug_mask & DEBUG_SUSPEND)
148                         pr_info("late_resume: abort, state %d\n", state);
149                 goto abort;
150         }
151         if (debug_mask & DEBUG_SUSPEND)
152                 pr_info("late_resume: call handlers\n");
153         list_for_each_entry_reverse(pos, &early_suspend_handlers, link) {
154                 if (pos->resume != NULL) {
155                         if (debug_mask & DEBUG_VERBOSE)
156                                 pr_info("late_resume: calling %pf\n", pos->resume);
157
158                         pos->resume(pos);
159                 }
160         }
161         if (debug_mask & DEBUG_SUSPEND)
162                 pr_info("late_resume: done\n");
163 abort:
164         mutex_unlock(&early_suspend_lock);
165 }
166
167 void request_suspend_state(suspend_state_t new_state)
168 {
169         unsigned long irqflags;
170         int old_sleep;
171
172         spin_lock_irqsave(&state_lock, irqflags);
173         old_sleep = state & SUSPEND_REQUESTED;
174         if (debug_mask & DEBUG_USER_STATE) {
175                 struct timespec ts;
176                 struct rtc_time tm;
177                 getnstimeofday(&ts);
178                 rtc_time_to_tm(ts.tv_sec, &tm);
179                 pr_info("request_suspend_state: %s (%d->%d) at %lld "
180                         "(%d-%02d-%02d %02d:%02d:%02d.%09lu UTC)\n",
181                         new_state != PM_SUSPEND_ON ? "sleep" : "wakeup",
182                         requested_suspend_state, new_state,
183                         ktime_to_ns(ktime_get()),
184                         tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
185                         tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec);
186         }
187         if (!old_sleep && new_state != PM_SUSPEND_ON) {
188                 state |= SUSPEND_REQUESTED;
189                 queue_work(suspend_work_queue, &early_suspend_work);
190         } else if (old_sleep && new_state == PM_SUSPEND_ON) {
191                 state &= ~SUSPEND_REQUESTED;
192                 queue_work(suspend_work_queue, &late_resume_work);
193         }
194         requested_suspend_state = new_state;
195         spin_unlock_irqrestore(&state_lock, irqflags);
196 }
197
198 suspend_state_t get_suspend_state(void)
199 {
200         return requested_suspend_state;
201 }