aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/power/earlysuspend.c
diff options
context:
space:
mode:
authorJonathan Herman <hermanjl@cs.unc.edu>2013-01-22 10:38:37 -0500
committerJonathan Herman <hermanjl@cs.unc.edu>2013-01-22 10:38:37 -0500
commitfcc9d2e5a6c89d22b8b773a64fb4ad21ac318446 (patch)
treea57612d1888735a2ec7972891b68c1ac5ec8faea /kernel/power/earlysuspend.c
parent8dea78da5cee153b8af9c07a2745f6c55057fe12 (diff)
Added missing tegra files.HEADmaster
Diffstat (limited to 'kernel/power/earlysuspend.c')
-rw-r--r--kernel/power/earlysuspend.c187
1 files changed, 187 insertions, 0 deletions
diff --git a/kernel/power/earlysuspend.c b/kernel/power/earlysuspend.c
new file mode 100644
index 00000000000..b15f02eba45
--- /dev/null
+++ b/kernel/power/earlysuspend.c
@@ -0,0 +1,187 @@
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
26enum {
27 DEBUG_USER_STATE = 1U << 0,
28 DEBUG_SUSPEND = 1U << 2,
29 DEBUG_VERBOSE = 1U << 3,
30};
31static int debug_mask = DEBUG_USER_STATE;
32module_param_named(debug_mask, debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP);
33
34static DEFINE_MUTEX(early_suspend_lock);
35static LIST_HEAD(early_suspend_handlers);
36static void early_suspend(struct work_struct *work);
37static void late_resume(struct work_struct *work);
38static DECLARE_WORK(early_suspend_work, early_suspend);
39static DECLARE_WORK(late_resume_work, late_resume);
40static DEFINE_SPINLOCK(state_lock);
41enum {
42 SUSPEND_REQUESTED = 0x1,
43 SUSPENDED = 0x2,
44 SUSPEND_REQUESTED_AND_SUSPENDED = SUSPEND_REQUESTED | SUSPENDED,
45};
46static int state;
47
48void register_early_suspend(struct early_suspend *handler)
49{
50 struct list_head *pos;
51
52 mutex_lock(&early_suspend_lock);
53 list_for_each(pos, &early_suspend_handlers) {
54 struct early_suspend *e;
55 e = list_entry(pos, struct early_suspend, link);
56 if (e->level > handler->level)
57 break;
58 }
59 list_add_tail(&handler->link, pos);
60 if ((state & SUSPENDED) && handler->suspend)
61 handler->suspend(handler);
62 mutex_unlock(&early_suspend_lock);
63}
64EXPORT_SYMBOL(register_early_suspend);
65
66void unregister_early_suspend(struct early_suspend *handler)
67{
68 mutex_lock(&early_suspend_lock);
69 list_del(&handler->link);
70 mutex_unlock(&early_suspend_lock);
71}
72EXPORT_SYMBOL(unregister_early_suspend);
73
74static void early_suspend(struct work_struct *work)
75{
76 struct early_suspend *pos;
77 unsigned long irqflags;
78 int abort = 0;
79
80 mutex_lock(&early_suspend_lock);
81 spin_lock_irqsave(&state_lock, irqflags);
82 if (state == SUSPEND_REQUESTED)
83 state |= SUSPENDED;
84 else
85 abort = 1;
86 spin_unlock_irqrestore(&state_lock, irqflags);
87
88 if (abort) {
89 if (debug_mask & DEBUG_SUSPEND)
90 pr_info("early_suspend: abort, state %d\n", state);
91 mutex_unlock(&early_suspend_lock);
92 goto abort;
93 }
94
95 if (debug_mask & DEBUG_SUSPEND)
96 pr_info("early_suspend: call handlers\n");
97 list_for_each_entry(pos, &early_suspend_handlers, link) {
98 if (pos->suspend != NULL) {
99 if (debug_mask & DEBUG_VERBOSE)
100 pr_info("early_suspend: calling %pf\n", pos->suspend);
101 pos->suspend(pos);
102 }
103 }
104 mutex_unlock(&early_suspend_lock);
105
106 if (debug_mask & DEBUG_SUSPEND)
107 pr_info("early_suspend: sync\n");
108
109 sys_sync();
110abort:
111 spin_lock_irqsave(&state_lock, irqflags);
112 if (state == SUSPEND_REQUESTED_AND_SUSPENDED)
113 wake_unlock(&main_wake_lock);
114 spin_unlock_irqrestore(&state_lock, irqflags);
115}
116
117static void late_resume(struct work_struct *work)
118{
119 struct early_suspend *pos;
120 unsigned long irqflags;
121 int abort = 0;
122
123 mutex_lock(&early_suspend_lock);
124 spin_lock_irqsave(&state_lock, irqflags);
125 if (state == SUSPENDED)
126 state &= ~SUSPENDED;
127 else
128 abort = 1;
129 spin_unlock_irqrestore(&state_lock, irqflags);
130
131 if (abort) {
132 if (debug_mask & DEBUG_SUSPEND)
133 pr_info("late_resume: abort, state %d\n", state);
134 goto abort;
135 }
136 if (debug_mask & DEBUG_SUSPEND)
137 pr_info("late_resume: call handlers\n");
138 list_for_each_entry_reverse(pos, &early_suspend_handlers, link) {
139 if (pos->resume != NULL) {
140 if (debug_mask & DEBUG_VERBOSE)
141 pr_info("late_resume: calling %pf\n", pos->resume);
142
143 pos->resume(pos);
144 }
145 }
146 if (debug_mask & DEBUG_SUSPEND)
147 pr_info("late_resume: done\n");
148abort:
149 mutex_unlock(&early_suspend_lock);
150}
151
152void request_suspend_state(suspend_state_t new_state)
153{
154 unsigned long irqflags;
155 int old_sleep;
156
157 spin_lock_irqsave(&state_lock, irqflags);
158 old_sleep = state & SUSPEND_REQUESTED;
159 if (debug_mask & DEBUG_USER_STATE) {
160 struct timespec ts;
161 struct rtc_time tm;
162 getnstimeofday(&ts);
163 rtc_time_to_tm(ts.tv_sec, &tm);
164 pr_info("request_suspend_state: %s (%d->%d) at %lld "
165 "(%d-%02d-%02d %02d:%02d:%02d.%09lu UTC)\n",
166 new_state != PM_SUSPEND_ON ? "sleep" : "wakeup",
167 requested_suspend_state, new_state,
168 ktime_to_ns(ktime_get()),
169 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
170 tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec);
171 }
172 if (!old_sleep && new_state != PM_SUSPEND_ON) {
173 state |= SUSPEND_REQUESTED;
174 queue_work(suspend_work_queue, &early_suspend_work);
175 } else if (old_sleep && new_state == PM_SUSPEND_ON) {
176 state &= ~SUSPEND_REQUESTED;
177 wake_lock(&main_wake_lock);
178 queue_work(suspend_work_queue, &late_resume_work);
179 }
180 requested_suspend_state = new_state;
181 spin_unlock_irqrestore(&state_lock, irqflags);
182}
183
184suspend_state_t get_suspend_state(void)
185{
186 return requested_suspend_state;
187}