PM: Enable partial support for FB earlysuspend
[linux-2.6.git] / kernel / power / fbearlysuspend.c
1 /* kernel/power/fbearlysuspend.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/module.h>
17 #include <linux/wait.h>
18
19 #include "power.h"
20
21 static wait_queue_head_t fb_state_wq;
22 static DEFINE_SPINLOCK(fb_state_lock);
23 static enum {
24         FB_STATE_STOPPED_DRAWING,
25         FB_STATE_REQUEST_STOP_DRAWING,
26         FB_STATE_DRAWING_OK,
27 } fb_state;
28
29 static ssize_t wait_for_fb_sleep_show(struct kobject *kobj,
30                                       struct kobj_attribute *attr, char *buf)
31 {
32         char *s = buf;
33         int ret;
34
35         ret = wait_event_interruptible(fb_state_wq,
36                                        fb_state != FB_STATE_DRAWING_OK);
37         if (ret && fb_state == FB_STATE_DRAWING_OK)
38                 return ret;
39         else
40                 s += sprintf(buf, "sleeping");
41         return s - buf;
42 }
43
44 static ssize_t wait_for_fb_wake_show(struct kobject *kobj,
45                                      struct kobj_attribute *attr, char *buf)
46 {
47         char *s = buf;
48         int ret;
49         unsigned long irq_flags;
50
51         spin_lock_irqsave(&fb_state_lock, irq_flags);
52         if (fb_state == FB_STATE_REQUEST_STOP_DRAWING) {
53                 fb_state = FB_STATE_STOPPED_DRAWING;
54                 wake_up(&fb_state_wq);
55         }
56         spin_unlock_irqrestore(&fb_state_lock, irq_flags);
57
58         ret = wait_event_interruptible(fb_state_wq,
59                                        fb_state == FB_STATE_DRAWING_OK);
60         if (ret && fb_state != FB_STATE_DRAWING_OK)
61                 return ret;
62         else
63                 s += sprintf(buf, "awake");
64
65         return s - buf;
66 }
67
68 #define power_ro_attr(_name) \
69 static struct kobj_attribute _name##_attr = {   \
70         .attr   = {                             \
71                 .name = __stringify(_name),     \
72                 .mode = 0444,                   \
73         },                                      \
74         .show   = _name##_show,                 \
75         .store  = NULL,         \
76 }
77
78 power_ro_attr(wait_for_fb_sleep);
79 power_ro_attr(wait_for_fb_wake);
80
81 static struct attribute *g[] = {
82         &wait_for_fb_sleep_attr.attr,
83         &wait_for_fb_wake_attr.attr,
84         NULL,
85 };
86
87 static struct attribute_group attr_group = {
88         .attrs = g,
89 };
90
91 static int __init android_power_init(void)
92 {
93         int ret;
94
95         init_waitqueue_head(&fb_state_wq);
96         fb_state = FB_STATE_DRAWING_OK;
97
98         ret = sysfs_create_group(power_kobj, &attr_group);
99         if (ret) {
100                 pr_err("android_power_init: sysfs_create_group failed\n");
101                 return ret;
102         }
103
104         return 0;
105 }
106
107 static void  __exit android_power_exit(void)
108 {
109         sysfs_remove_group(power_kobj, &attr_group);
110 }
111
112 module_init(android_power_init);
113 module_exit(android_power_exit);
114