tizen 2.4 release
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / include / linux / wakelock.h
1 /* include/linux/wakelock.h
2  *
3  * Copyright (C) 2007-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 #ifndef _LINUX_WAKELOCK_H
17 #define _LINUX_WAKELOCK_H
18
19 #include <linux/list.h>
20 #include <linux/ktime.h>
21 #ifdef CONFIG_PM_AUTOSLEEP
22 #include <linux/device.h>
23 #endif
24
25 /* A wake_lock prevents the system from entering suspend or other low power
26  * states when active. If the type is set to WAKE_LOCK_SUSPEND, the wake_lock
27  * prevents a full system suspend. If the type is WAKE_LOCK_IDLE, low power
28  * states that cause large interrupt latencies or that disable a set of
29  * interrupts will not entered from idle until the wake_locks are released.
30  */
31
32 enum {
33         WAKE_LOCK_SUSPEND, /* Prevent suspend */
34         WAKE_LOCK_IDLE,    /* Prevent low power idle */
35         WAKE_LOCK_TYPE_COUNT
36 };
37
38 struct wake_lock {
39 #ifdef CONFIG_HAS_WAKELOCK
40         struct list_head    link;
41         int                 flags;
42         const char         *name;
43         unsigned long       expires;
44 #ifdef CONFIG_WAKELOCK_STAT
45         struct {
46                 int             count;
47                 int             expire_count;
48                 int             wakeup_count;
49                 ktime_t         total_time;
50                 ktime_t         prevent_suspend_time;
51                 ktime_t         max_time;
52                 ktime_t         last_time;
53         } stat;
54 #endif
55 #else
56 struct wakeup_source ws;
57 #endif
58 };
59
60 #ifdef CONFIG_HAS_WAKELOCK
61
62 void wake_lock_init(struct wake_lock *lock, int type, const char *name);
63 void wake_lock_destroy(struct wake_lock *lock);
64 void wake_lock(struct wake_lock *lock);
65 void wake_lock_timeout(struct wake_lock *lock, long timeout);
66 void wake_unlock(struct wake_lock *lock);
67
68 /* wake_lock_active returns a non-zero value if the wake_lock is currently
69  * locked. If the wake_lock has a timeout, it does not check the timeout
70  * but if the timeout had aready been checked it will return 0.
71  */
72 int wake_lock_active(struct wake_lock *lock);
73
74 /* has_wake_lock returns 0 if no wake locks of the specified type are active,
75  * and non-zero if one or more wake locks are held. Specifically it returns
76  * -1 if one or more wake locks with no timeout are active or the
77  * number of jiffies until all active wake locks time out.
78  */
79 long has_wake_lock(int type);
80
81 #else
82
83 #ifdef CONFIG_PM_AUTOSLEEP
84 static inline void wake_lock_init(struct wake_lock *lock, int type,
85                                   const char *name)
86 {
87         wakeup_source_init(&lock->ws, name);
88 }
89
90 static inline void wake_lock_destroy(struct wake_lock *lock)
91 {
92         wakeup_source_trash(&lock->ws);
93 }
94
95 static inline void wake_lock(struct wake_lock *lock)
96 {
97         __pm_stay_awake(&lock->ws);
98 }
99
100 static inline void wake_lock_timeout(struct wake_lock *lock, long timeout)
101 {
102         __pm_wakeup_event(&lock->ws, jiffies_to_msecs(timeout));
103 }
104
105 static inline void wake_unlock(struct wake_lock *lock)
106 {
107         __pm_relax(&lock->ws);
108 }
109
110 static inline int wake_lock_active(struct wake_lock *lock)
111 {
112         return lock->ws.active;
113 }
114 #else
115 static inline void wake_lock_init(struct wake_lock *lock, int type,
116                                         const char *name) {}
117 static inline void wake_lock_destroy(struct wake_lock *lock) {}
118 static inline void wake_lock(struct wake_lock *lock) {}
119 static inline void wake_lock_timeout(struct wake_lock *lock, long timeout) {}
120 static inline void wake_unlock(struct wake_lock *lock) {}
121
122 static inline int wake_lock_active(struct wake_lock *lock) { return 0; }
123 #endif
124 static inline long has_wake_lock(int type) { return 0; }
125
126 #endif
127
128 #endif
129