htcleo: add Cotulla's fixes for non-android touchscreen!
[htc-linux.git] / kernel / power / suspend.c
blobcd515ba1c8ce976f340da0c5aab7a0cd7447cdb0
1 /*
2 * kernel/power/suspend.c - Suspend to RAM and standby functionality.
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
6 * Copyright (c) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
8 * This file is released under the GPLv2.
9 */
11 #include <linux/string.h>
12 #include <linux/delay.h>
13 #include <linux/errno.h>
14 #include <linux/init.h>
15 #include <linux/console.h>
16 #include <linux/cpu.h>
17 #include <linux/syscalls.h>
19 #include "power.h"
21 const char *const pm_states[PM_SUSPEND_MAX] = {
22 #ifdef CONFIG_EARLYSUSPEND
23 [PM_SUSPEND_ON] = "on",
24 #endif
25 [PM_SUSPEND_STANDBY] = "standby",
26 [PM_SUSPEND_MEM] = "mem",
29 static struct platform_suspend_ops *suspend_ops;
31 /**
32 * suspend_set_ops - Set the global suspend method table.
33 * @ops: Pointer to ops structure.
35 void suspend_set_ops(struct platform_suspend_ops *ops)
37 mutex_lock(&pm_mutex);
38 suspend_ops = ops;
39 mutex_unlock(&pm_mutex);
42 bool valid_state(suspend_state_t state)
45 * All states need lowlevel support and need to be valid to the lowlevel
46 * implementation, no valid callback implies that none are valid.
48 return suspend_ops && suspend_ops->valid && suspend_ops->valid(state);
51 /**
52 * suspend_valid_only_mem - generic memory-only valid callback
54 * Platform drivers that implement mem suspend only and only need
55 * to check for that in their .valid callback can use this instead
56 * of rolling their own .valid callback.
58 int suspend_valid_only_mem(suspend_state_t state)
60 return state == PM_SUSPEND_MEM;
63 static int suspend_test(int level)
65 #ifdef CONFIG_PM_DEBUG
66 if (pm_test_level == level) {
67 printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n");
68 mdelay(5000);
69 return 1;
71 #endif /* !CONFIG_PM_DEBUG */
72 return 0;
75 /**
76 * suspend_prepare - Do prep work before entering low-power state.
78 * This is common code that is called for each state that we're entering.
79 * Run suspend notifiers, allocate a console and stop all processes.
81 static int suspend_prepare(void)
83 int error;
85 if (!suspend_ops || !suspend_ops->enter)
86 return -EPERM;
88 pm_prepare_console();
90 error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
91 if (error)
92 goto Finish;
94 error = usermodehelper_disable();
95 if (error)
96 goto Finish;
98 error = suspend_freeze_processes();
99 if (!error)
100 return 0;
102 suspend_thaw_processes();
103 usermodehelper_enable();
104 Finish:
105 pm_notifier_call_chain(PM_POST_SUSPEND);
106 pm_restore_console();
107 return error;
110 /* default implementation */
111 void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
113 local_irq_disable();
116 /* default implementation */
117 void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
119 local_irq_enable();
123 * suspend_enter - enter the desired system sleep state.
124 * @state: state to enter
126 * This function should be called after devices have been suspended.
128 static int suspend_enter(suspend_state_t state)
130 int error;
132 if (suspend_ops->prepare) {
133 error = suspend_ops->prepare();
134 if (error)
135 return error;
138 error = dpm_suspend_noirq(PMSG_SUSPEND);
139 if (error) {
140 printk(KERN_ERR "PM: Some devices failed to power down\n");
141 goto Platfrom_finish;
144 if (suspend_ops->prepare_late) {
145 error = suspend_ops->prepare_late();
146 if (error)
147 goto Power_up_devices;
150 if (suspend_test(TEST_PLATFORM))
151 goto Platform_wake;
153 error = disable_nonboot_cpus();
154 if (error || suspend_test(TEST_CPUS))
155 goto Enable_cpus;
157 arch_suspend_disable_irqs();
158 BUG_ON(!irqs_disabled());
160 error = sysdev_suspend(PMSG_SUSPEND);
161 if (!error) {
162 if (!suspend_test(TEST_CORE))
163 error = suspend_ops->enter(state);
164 sysdev_resume();
167 arch_suspend_enable_irqs();
168 BUG_ON(irqs_disabled());
170 Enable_cpus:
171 enable_nonboot_cpus();
173 Platform_wake:
174 if (suspend_ops->wake)
175 suspend_ops->wake();
177 Power_up_devices:
178 dpm_resume_noirq(PMSG_RESUME);
180 Platfrom_finish:
181 if (suspend_ops->finish)
182 suspend_ops->finish();
184 return error;
188 * suspend_devices_and_enter - suspend devices and enter the desired system
189 * sleep state.
190 * @state: state to enter
192 int suspend_devices_and_enter(suspend_state_t state)
194 int error;
196 if (!suspend_ops)
197 return -ENOSYS;
199 if (suspend_ops->begin) {
200 error = suspend_ops->begin(state);
201 if (error)
202 goto Close;
204 suspend_console();
205 suspend_test_start();
206 error = dpm_suspend_start(PMSG_SUSPEND);
207 if (error) {
208 printk(KERN_ERR "PM: Some devices failed to suspend\n");
209 goto Recover_platform;
211 suspend_test_finish("suspend devices");
212 if (suspend_test(TEST_DEVICES))
213 goto Recover_platform;
215 suspend_enter(state);
217 Resume_devices:
218 suspend_test_start();
219 dpm_resume_end(PMSG_RESUME);
220 suspend_test_finish("resume devices");
221 resume_console();
222 Close:
223 if (suspend_ops->end)
224 suspend_ops->end();
225 return error;
227 Recover_platform:
228 if (suspend_ops->recover)
229 suspend_ops->recover();
230 goto Resume_devices;
234 * suspend_finish - Do final work before exiting suspend sequence.
236 * Call platform code to clean up, restart processes, and free the
237 * console that we've allocated. This is not called for suspend-to-disk.
239 static void suspend_finish(void)
241 suspend_thaw_processes();
242 usermodehelper_enable();
243 pm_notifier_call_chain(PM_POST_SUSPEND);
244 pm_restore_console();
248 * enter_state - Do common work of entering low-power state.
249 * @state: pm_state structure for state we're entering.
251 * Make sure we're the only ones trying to enter a sleep state. Fail
252 * if someone has beat us to it, since we don't want anything weird to
253 * happen when we wake up.
254 * Then, do the setup for suspend, enter the state, and cleaup (after
255 * we've woken up).
257 int enter_state(suspend_state_t state)
259 int error;
261 if (!valid_state(state))
262 return -ENODEV;
264 if (!mutex_trylock(&pm_mutex))
265 return -EBUSY;
267 printk(KERN_INFO "PM: Syncing filesystems ... ");
268 sys_sync();
269 printk("done.\n");
271 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
272 error = suspend_prepare();
273 if (error)
274 goto Unlock;
276 if (suspend_test(TEST_FREEZER))
277 goto Finish;
279 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
280 error = suspend_devices_and_enter(state);
282 Finish:
283 pr_debug("PM: Finishing wakeup.\n");
284 suspend_finish();
285 Unlock:
286 mutex_unlock(&pm_mutex);
287 return error;
291 * pm_suspend - Externally visible function for suspending system.
292 * @state: Enumerated value of state to enter.
294 * Determine whether or not value is within range, get state
295 * structure, and enter (above).
297 int pm_suspend(suspend_state_t state)
299 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
300 return enter_state(state);
301 return -EINVAL;
303 EXPORT_SYMBOL(pm_suspend);