Merge remote-tracking branch 'remotes/aosp/android-3.0' into develop-3.0
[firefly-linux-kernel-4.4.55.git] / kernel / power / wakelock.c
1 /* kernel/power/wakelock.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/platform_device.h>
18 #include <linux/rtc.h>
19 #include <linux/suspend.h>
20 #include <linux/syscalls.h> /* sys_sync */
21 #include <linux/wakelock.h>
22 #ifdef CONFIG_WAKELOCK_STAT
23 #include <linux/proc_fs.h>
24 #endif
25 #include "power.h"
26
27 enum {
28         DEBUG_EXIT_SUSPEND = 1U << 0,
29         DEBUG_WAKEUP = 1U << 1,
30         DEBUG_SUSPEND = 1U << 2,
31         DEBUG_EXPIRE = 1U << 3,
32         DEBUG_WAKE_LOCK = 1U << 4,
33 };
34 static int debug_mask = DEBUG_EXIT_SUSPEND | DEBUG_WAKEUP;
35 module_param_named(debug_mask, debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP);
36
37 #define WAKE_LOCK_TYPE_MASK              (0x0f)
38 #define WAKE_LOCK_INITIALIZED            (1U << 8)
39 #define WAKE_LOCK_ACTIVE                 (1U << 9)
40 #define WAKE_LOCK_AUTO_EXPIRE            (1U << 10)
41 #define WAKE_LOCK_PREVENTING_SUSPEND     (1U << 11)
42
43 static DEFINE_SPINLOCK(list_lock);
44 static LIST_HEAD(inactive_locks);
45 static struct list_head active_wake_locks[WAKE_LOCK_TYPE_COUNT];
46 static int current_event_num;
47 struct workqueue_struct *suspend_work_queue;
48 struct wake_lock main_wake_lock;
49 suspend_state_t requested_suspend_state = PM_SUSPEND_MEM;
50 static struct wake_lock unknown_wakeup;
51 static struct wake_lock suspend_backoff_lock;
52
53 #define SUSPEND_BACKOFF_THRESHOLD       10
54 #define SUSPEND_BACKOFF_INTERVAL        10000
55
56 static unsigned suspend_short_count;
57
58 #ifdef CONFIG_WAKELOCK_STAT
59 static struct wake_lock deleted_wake_locks;
60 static ktime_t last_sleep_time_update;
61 static int wait_for_wakeup;
62
63 int get_expired_time(struct wake_lock *lock, ktime_t *expire_time)
64 {
65         struct timespec ts;
66         struct timespec kt;
67         struct timespec tomono;
68         struct timespec delta;
69         struct timespec sleep;
70         long timeout;
71
72         if (!(lock->flags & WAKE_LOCK_AUTO_EXPIRE))
73                 return 0;
74         get_xtime_and_monotonic_and_sleep_offset(&kt, &tomono, &sleep);
75         timeout = lock->expires - jiffies;
76         if (timeout > 0)
77                 return 0;
78         jiffies_to_timespec(-timeout, &delta);
79         set_normalized_timespec(&ts, kt.tv_sec + tomono.tv_sec - delta.tv_sec,
80                                 kt.tv_nsec + tomono.tv_nsec - delta.tv_nsec);
81         *expire_time = timespec_to_ktime(ts);
82         return 1;
83 }
84
85
86 static int print_lock_stat(struct seq_file *m, struct wake_lock *lock)
87 {
88         int lock_count = lock->stat.count;
89         int expire_count = lock->stat.expire_count;
90         ktime_t active_time = ktime_set(0, 0);
91         ktime_t total_time = lock->stat.total_time;
92         ktime_t max_time = lock->stat.max_time;
93
94         ktime_t prevent_suspend_time = lock->stat.prevent_suspend_time;
95         if (lock->flags & WAKE_LOCK_ACTIVE) {
96                 ktime_t now, add_time;
97                 int expired = get_expired_time(lock, &now);
98                 if (!expired)
99                         now = ktime_get();
100                 add_time = ktime_sub(now, lock->stat.last_time);
101                 lock_count++;
102                 if (!expired)
103                         active_time = add_time;
104                 else
105                         expire_count++;
106                 total_time = ktime_add(total_time, add_time);
107                 if (lock->flags & WAKE_LOCK_PREVENTING_SUSPEND)
108                         prevent_suspend_time = ktime_add(prevent_suspend_time,
109                                         ktime_sub(now, last_sleep_time_update));
110                 if (add_time.tv64 > max_time.tv64)
111                         max_time = add_time;
112         }
113
114         return seq_printf(m,
115                      "\"%s\"\t%d\t%d\t%d\t%lld\t%lld\t%lld\t%lld\t%lld\n",
116                      lock->name, lock_count, expire_count,
117                      lock->stat.wakeup_count, ktime_to_ns(active_time),
118                      ktime_to_ns(total_time),
119                      ktime_to_ns(prevent_suspend_time), ktime_to_ns(max_time),
120                      ktime_to_ns(lock->stat.last_time));
121 }
122
123 static int wakelock_stats_show(struct seq_file *m, void *unused)
124 {
125         unsigned long irqflags;
126         struct wake_lock *lock;
127         int ret;
128         int type;
129
130         spin_lock_irqsave(&list_lock, irqflags);
131
132         ret = seq_puts(m, "name\tcount\texpire_count\twake_count\tactive_since"
133                         "\ttotal_time\tsleep_time\tmax_time\tlast_change\n");
134         list_for_each_entry(lock, &inactive_locks, link)
135                 ret = print_lock_stat(m, lock);
136         for (type = 0; type < WAKE_LOCK_TYPE_COUNT; type++) {
137                 list_for_each_entry(lock, &active_wake_locks[type], link)
138                         ret = print_lock_stat(m, lock);
139         }
140         spin_unlock_irqrestore(&list_lock, irqflags);
141         return 0;
142 }
143
144 static void wake_unlock_stat_locked(struct wake_lock *lock, int expired)
145 {
146         ktime_t duration;
147         ktime_t now;
148         if (!(lock->flags & WAKE_LOCK_ACTIVE))
149                 return;
150         if (get_expired_time(lock, &now))
151                 expired = 1;
152         else
153                 now = ktime_get();
154         lock->stat.count++;
155         if (expired)
156                 lock->stat.expire_count++;
157         duration = ktime_sub(now, lock->stat.last_time);
158         lock->stat.total_time = ktime_add(lock->stat.total_time, duration);
159         if (ktime_to_ns(duration) > ktime_to_ns(lock->stat.max_time))
160                 lock->stat.max_time = duration;
161         lock->stat.last_time = ktime_get();
162         if (lock->flags & WAKE_LOCK_PREVENTING_SUSPEND) {
163                 duration = ktime_sub(now, last_sleep_time_update);
164                 lock->stat.prevent_suspend_time = ktime_add(
165                         lock->stat.prevent_suspend_time, duration);
166                 lock->flags &= ~WAKE_LOCK_PREVENTING_SUSPEND;
167         }
168 }
169
170 static void update_sleep_wait_stats_locked(int done)
171 {
172         struct wake_lock *lock;
173         ktime_t now, etime, elapsed, add;
174         int expired;
175
176         now = ktime_get();
177         elapsed = ktime_sub(now, last_sleep_time_update);
178         list_for_each_entry(lock, &active_wake_locks[WAKE_LOCK_SUSPEND], link) {
179                 expired = get_expired_time(lock, &etime);
180                 if (lock->flags & WAKE_LOCK_PREVENTING_SUSPEND) {
181                         if (expired)
182                                 add = ktime_sub(etime, last_sleep_time_update);
183                         else
184                                 add = elapsed;
185                         lock->stat.prevent_suspend_time = ktime_add(
186                                 lock->stat.prevent_suspend_time, add);
187                 }
188                 if (done || expired)
189                         lock->flags &= ~WAKE_LOCK_PREVENTING_SUSPEND;
190                 else
191                         lock->flags |= WAKE_LOCK_PREVENTING_SUSPEND;
192         }
193         last_sleep_time_update = now;
194 }
195 #endif
196
197
198 static void expire_wake_lock(struct wake_lock *lock)
199 {
200 #ifdef CONFIG_WAKELOCK_STAT
201         wake_unlock_stat_locked(lock, 1);
202 #endif
203         lock->flags &= ~(WAKE_LOCK_ACTIVE | WAKE_LOCK_AUTO_EXPIRE);
204         list_del(&lock->link);
205         list_add(&lock->link, &inactive_locks);
206         if (debug_mask & (DEBUG_WAKE_LOCK | DEBUG_EXPIRE))
207                 pr_info("expired wake lock %s\n", lock->name);
208 }
209
210 /* Caller must acquire the list_lock spinlock */
211 static void print_active_locks_locked(int type)
212 {
213         struct wake_lock *lock;
214
215         BUG_ON(type >= WAKE_LOCK_TYPE_COUNT);
216         list_for_each_entry(lock, &active_wake_locks[type], link) {
217                 if (lock->flags & WAKE_LOCK_AUTO_EXPIRE) {
218                         long timeout = lock->expires - jiffies;
219                         if (timeout <= 0)
220                                 pr_info("wake lock %s, expired\n", lock->name);
221                         else
222                                 pr_info("active wake lock %s, time left %ld.%03lu\n",
223                                         lock->name, timeout / HZ,
224                                         (timeout % HZ) * MSEC_PER_SEC / HZ);
225                 } else
226                         pr_info("active wake lock %s\n", lock->name);
227         }
228 }
229
230 void print_active_wake_locks(int type)
231 {
232         unsigned long irqflags;
233         spin_lock_irqsave(&list_lock, irqflags);
234         print_active_locks_locked(type);
235         spin_unlock_irqrestore(&list_lock, irqflags);
236 }
237
238 static long has_wake_lock_locked(int type)
239 {
240         struct wake_lock *lock, *n;
241         long max_timeout = 0;
242
243         BUG_ON(type >= WAKE_LOCK_TYPE_COUNT);
244         list_for_each_entry_safe(lock, n, &active_wake_locks[type], link) {
245                 if (lock->flags & WAKE_LOCK_AUTO_EXPIRE) {
246                         long timeout = lock->expires - jiffies;
247                         if (timeout <= 0)
248                                 expire_wake_lock(lock);
249                         else if (timeout > max_timeout)
250                                 max_timeout = timeout;
251                 } else
252                         return -1;
253         }
254         return max_timeout;
255 }
256
257 long has_wake_lock(int type)
258 {
259         long ret;
260         unsigned long irqflags;
261         spin_lock_irqsave(&list_lock, irqflags);
262         ret = has_wake_lock_locked(type);
263         if (ret && (debug_mask & DEBUG_WAKEUP) && type == WAKE_LOCK_SUSPEND)
264                 print_active_locks_locked(type);
265         spin_unlock_irqrestore(&list_lock, irqflags);
266         return ret;
267 }
268
269 static void suspend_backoff(void)
270 {
271         pr_info("suspend: too many immediate wakeups, back off\n");
272         wake_lock_timeout(&suspend_backoff_lock,
273                           msecs_to_jiffies(SUSPEND_BACKOFF_INTERVAL));
274 }
275
276 static void suspend(struct work_struct *work)
277 {
278         int ret;
279         int entry_event_num;
280         struct timespec ts_entry, ts_exit;
281
282         if (has_wake_lock(WAKE_LOCK_SUSPEND)) {
283                 if (debug_mask & DEBUG_SUSPEND)
284                         pr_info("suspend: abort suspend\n");
285                 return;
286         }
287
288         entry_event_num = current_event_num;
289         sys_sync();
290         if (debug_mask & DEBUG_SUSPEND)
291                 pr_info("suspend: enter suspend\n");
292         getnstimeofday(&ts_entry);
293         ret = pm_suspend(requested_suspend_state);
294         getnstimeofday(&ts_exit);
295
296         if (debug_mask & DEBUG_EXIT_SUSPEND) {
297                 struct rtc_time tm;
298                 rtc_time_to_tm(ts_exit.tv_sec, &tm);
299                 pr_info("suspend: exit suspend, ret = %d "
300                         "(%d-%02d-%02d %02d:%02d:%02d.%09lu UTC)\n", ret,
301                         tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
302                         tm.tm_hour, tm.tm_min, tm.tm_sec, ts_exit.tv_nsec);
303         }
304
305         if (ts_exit.tv_sec - ts_entry.tv_sec <= 1) {
306                 ++suspend_short_count;
307
308                 if (suspend_short_count == SUSPEND_BACKOFF_THRESHOLD) {
309                         suspend_backoff();
310                         suspend_short_count = 0;
311                 }
312         } else {
313                 suspend_short_count = 0;
314         }
315
316         if (current_event_num == entry_event_num) {
317                 if (debug_mask & DEBUG_SUSPEND)
318                         pr_info("suspend: pm_suspend returned with no event\n");
319                 wake_lock_timeout(&unknown_wakeup, HZ / 2);
320         }
321 }
322 static DECLARE_WORK(suspend_work, suspend);
323
324 static void expire_wake_locks(unsigned long data)
325 {
326         long has_lock;
327         unsigned long irqflags;
328         if (debug_mask & DEBUG_EXPIRE)
329                 pr_info("expire_wake_locks: start\n");
330         spin_lock_irqsave(&list_lock, irqflags);
331         if (debug_mask & DEBUG_SUSPEND)
332                 print_active_locks_locked(WAKE_LOCK_SUSPEND);
333         has_lock = has_wake_lock_locked(WAKE_LOCK_SUSPEND);
334         if (debug_mask & DEBUG_EXPIRE)
335                 pr_info("expire_wake_locks: done, has_lock %ld\n", has_lock);
336         if (has_lock == 0)
337                 queue_work(suspend_work_queue, &suspend_work);
338         spin_unlock_irqrestore(&list_lock, irqflags);
339 }
340 static DEFINE_TIMER(expire_timer, expire_wake_locks, 0, 0);
341
342 static int power_suspend_late(struct device *dev)
343 {
344         int ret = has_wake_lock(WAKE_LOCK_SUSPEND) ? -EAGAIN : 0;
345 #ifdef CONFIG_WAKELOCK_STAT
346         wait_for_wakeup = !ret;
347 #endif
348         if (debug_mask & DEBUG_SUSPEND)
349                 pr_info("power_suspend_late return %d\n", ret);
350         return ret;
351 }
352
353 static struct dev_pm_ops power_driver_pm_ops = {
354         .suspend_noirq = power_suspend_late,
355 };
356
357 static struct platform_driver power_driver = {
358         .driver.name = "power",
359         .driver.pm = &power_driver_pm_ops,
360 };
361 static struct platform_device power_device = {
362         .name = "power",
363 };
364
365 void wake_lock_init(struct wake_lock *lock, int type, const char *name)
366 {
367         unsigned long irqflags = 0;
368
369         if (name)
370                 lock->name = name;
371         BUG_ON(!lock->name);
372         BUG_ON(lock->flags & WAKE_LOCK_INITIALIZED);
373
374         if (debug_mask & DEBUG_WAKE_LOCK)
375                 pr_info("wake_lock_init name=%s\n", lock->name);
376 #ifdef CONFIG_WAKELOCK_STAT
377         lock->stat.count = 0;
378         lock->stat.expire_count = 0;
379         lock->stat.wakeup_count = 0;
380         lock->stat.total_time = ktime_set(0, 0);
381         lock->stat.prevent_suspend_time = ktime_set(0, 0);
382         lock->stat.max_time = ktime_set(0, 0);
383         lock->stat.last_time = ktime_set(0, 0);
384 #endif
385         lock->flags = (type & WAKE_LOCK_TYPE_MASK) | WAKE_LOCK_INITIALIZED;
386
387         INIT_LIST_HEAD(&lock->link);
388         spin_lock_irqsave(&list_lock, irqflags);
389         list_add(&lock->link, &inactive_locks);
390         spin_unlock_irqrestore(&list_lock, irqflags);
391 }
392 EXPORT_SYMBOL(wake_lock_init);
393
394 void wake_lock_destroy(struct wake_lock *lock)
395 {
396         unsigned long irqflags;
397         if (debug_mask & DEBUG_WAKE_LOCK)
398                 pr_info("wake_lock_destroy name=%s\n", lock->name);
399         spin_lock_irqsave(&list_lock, irqflags);
400         lock->flags &= ~WAKE_LOCK_INITIALIZED;
401 #ifdef CONFIG_WAKELOCK_STAT
402         if (lock->stat.count) {
403                 deleted_wake_locks.stat.count += lock->stat.count;
404                 deleted_wake_locks.stat.expire_count += lock->stat.expire_count;
405                 deleted_wake_locks.stat.total_time =
406                         ktime_add(deleted_wake_locks.stat.total_time,
407                                   lock->stat.total_time);
408                 deleted_wake_locks.stat.prevent_suspend_time =
409                         ktime_add(deleted_wake_locks.stat.prevent_suspend_time,
410                                   lock->stat.prevent_suspend_time);
411                 deleted_wake_locks.stat.max_time =
412                         ktime_add(deleted_wake_locks.stat.max_time,
413                                   lock->stat.max_time);
414         }
415 #endif
416         list_del(&lock->link);
417         spin_unlock_irqrestore(&list_lock, irqflags);
418 }
419 EXPORT_SYMBOL(wake_lock_destroy);
420
421 static void wake_lock_internal(
422         struct wake_lock *lock, long timeout, int has_timeout)
423 {
424         int type;
425         unsigned long irqflags;
426         long expire_in;
427
428         spin_lock_irqsave(&list_lock, irqflags);
429         type = lock->flags & WAKE_LOCK_TYPE_MASK;
430         BUG_ON(type >= WAKE_LOCK_TYPE_COUNT);
431         BUG_ON(!(lock->flags & WAKE_LOCK_INITIALIZED));
432 #ifdef CONFIG_WAKELOCK_STAT
433         if (type == WAKE_LOCK_SUSPEND && wait_for_wakeup) {
434                 if (debug_mask & DEBUG_WAKEUP)
435                         pr_info("wakeup wake lock: %s\n", lock->name);
436                 wait_for_wakeup = 0;
437                 lock->stat.wakeup_count++;
438         }
439         if ((lock->flags & WAKE_LOCK_AUTO_EXPIRE) &&
440             (long)(lock->expires - jiffies) <= 0) {
441                 wake_unlock_stat_locked(lock, 0);
442                 lock->stat.last_time = ktime_get();
443         }
444 #endif
445         if (!(lock->flags & WAKE_LOCK_ACTIVE)) {
446                 lock->flags |= WAKE_LOCK_ACTIVE;
447 #ifdef CONFIG_WAKELOCK_STAT
448                 lock->stat.last_time = ktime_get();
449 #endif
450         }
451         list_del(&lock->link);
452         if (has_timeout) {
453                 if (debug_mask & DEBUG_WAKE_LOCK)
454                         pr_info("wake_lock: %s, type %d, timeout %ld.%03lu\n",
455                                 lock->name, type, timeout / HZ,
456                                 (timeout % HZ) * MSEC_PER_SEC / HZ);
457                 lock->expires = jiffies + timeout;
458                 lock->flags |= WAKE_LOCK_AUTO_EXPIRE;
459                 list_add_tail(&lock->link, &active_wake_locks[type]);
460         } else {
461                 if (debug_mask & DEBUG_WAKE_LOCK)
462                         pr_info("wake_lock: %s, type %d\n", lock->name, type);
463                 lock->expires = LONG_MAX;
464                 lock->flags &= ~WAKE_LOCK_AUTO_EXPIRE;
465                 list_add(&lock->link, &active_wake_locks[type]);
466         }
467         if (type == WAKE_LOCK_SUSPEND) {
468                 current_event_num++;
469 #ifdef CONFIG_WAKELOCK_STAT
470                 if (lock == &main_wake_lock)
471                         update_sleep_wait_stats_locked(1);
472                 else if (!wake_lock_active(&main_wake_lock))
473                         update_sleep_wait_stats_locked(0);
474 #endif
475                 if (has_timeout)
476                         expire_in = has_wake_lock_locked(type);
477                 else
478                         expire_in = -1;
479                 if (expire_in > 0) {
480                         if (debug_mask & DEBUG_EXPIRE)
481                                 pr_info("wake_lock: %s, start expire timer, "
482                                         "%ld\n", lock->name, expire_in);
483                         mod_timer(&expire_timer, jiffies + expire_in);
484                 } else {
485                         if (del_timer(&expire_timer))
486                                 if (debug_mask & DEBUG_EXPIRE)
487                                         pr_info("wake_lock: %s, stop expire timer\n",
488                                                 lock->name);
489                         if (expire_in == 0)
490                                 queue_work(suspend_work_queue, &suspend_work);
491                 }
492         }
493         spin_unlock_irqrestore(&list_lock, irqflags);
494 }
495
496 void wake_lock(struct wake_lock *lock)
497 {
498         wake_lock_internal(lock, 0, 0);
499 }
500 EXPORT_SYMBOL(wake_lock);
501
502 void wake_lock_timeout(struct wake_lock *lock, long timeout)
503 {
504         wake_lock_internal(lock, timeout, 1);
505 }
506 EXPORT_SYMBOL(wake_lock_timeout);
507
508 void wake_unlock(struct wake_lock *lock)
509 {
510         int type;
511         unsigned long irqflags;
512         spin_lock_irqsave(&list_lock, irqflags);
513         type = lock->flags & WAKE_LOCK_TYPE_MASK;
514 #ifdef CONFIG_WAKELOCK_STAT
515         wake_unlock_stat_locked(lock, 0);
516 #endif
517         if (debug_mask & DEBUG_WAKE_LOCK)
518                 pr_info("wake_unlock: %s\n", lock->name);
519         lock->flags &= ~(WAKE_LOCK_ACTIVE | WAKE_LOCK_AUTO_EXPIRE);
520         list_del(&lock->link);
521         list_add(&lock->link, &inactive_locks);
522         if (type == WAKE_LOCK_SUSPEND) {
523                 long has_lock = has_wake_lock_locked(type);
524                 if (has_lock > 0) {
525                         if (debug_mask & DEBUG_EXPIRE)
526                                 pr_info("wake_unlock: %s, start expire timer, "
527                                         "%ld\n", lock->name, has_lock);
528                         mod_timer(&expire_timer, jiffies + has_lock);
529                 } else {
530                         if (del_timer(&expire_timer))
531                                 if (debug_mask & DEBUG_EXPIRE)
532                                         pr_info("wake_unlock: %s, stop expire "
533                                                 "timer\n", lock->name);
534                         if (has_lock == 0)
535                                 queue_work(suspend_work_queue, &suspend_work);
536                 }
537                 if (lock == &main_wake_lock) {
538                         if (debug_mask & DEBUG_SUSPEND)
539                                 print_active_locks_locked(WAKE_LOCK_SUSPEND);
540 #ifdef CONFIG_WAKELOCK_STAT
541                         update_sleep_wait_stats_locked(0);
542 #endif
543                 }
544         }
545         spin_unlock_irqrestore(&list_lock, irqflags);
546 }
547 EXPORT_SYMBOL(wake_unlock);
548
549 int wake_lock_active(struct wake_lock *lock)
550 {
551         return !!(lock->flags & WAKE_LOCK_ACTIVE);
552 }
553 EXPORT_SYMBOL(wake_lock_active);
554
555 static int wakelock_stats_open(struct inode *inode, struct file *file)
556 {
557         return single_open(file, wakelock_stats_show, NULL);
558 }
559
560 static const struct file_operations wakelock_stats_fops = {
561         .owner = THIS_MODULE,
562         .open = wakelock_stats_open,
563         .read = seq_read,
564         .llseek = seq_lseek,
565         .release = single_release,
566 };
567
568 static int __init wakelocks_init(void)
569 {
570         int ret;
571         int i;
572
573         for (i = 0; i < ARRAY_SIZE(active_wake_locks); i++)
574                 INIT_LIST_HEAD(&active_wake_locks[i]);
575
576 #ifdef CONFIG_WAKELOCK_STAT
577         wake_lock_init(&deleted_wake_locks, WAKE_LOCK_SUSPEND,
578                         "deleted_wake_locks");
579 #endif
580         wake_lock_init(&main_wake_lock, WAKE_LOCK_SUSPEND, "main");
581         wake_lock(&main_wake_lock);
582         wake_lock_init(&unknown_wakeup, WAKE_LOCK_SUSPEND, "unknown_wakeups");
583         wake_lock_init(&suspend_backoff_lock, WAKE_LOCK_SUSPEND,
584                        "suspend_backoff");
585
586         ret = platform_device_register(&power_device);
587         if (ret) {
588                 pr_err("wakelocks_init: platform_device_register failed\n");
589                 goto err_platform_device_register;
590         }
591         ret = platform_driver_register(&power_driver);
592         if (ret) {
593                 pr_err("wakelocks_init: platform_driver_register failed\n");
594                 goto err_platform_driver_register;
595         }
596
597         suspend_work_queue = create_singlethread_workqueue("suspend");
598         if (suspend_work_queue == NULL) {
599                 ret = -ENOMEM;
600                 goto err_suspend_work_queue;
601         }
602
603 #ifdef CONFIG_WAKELOCK_STAT
604         proc_create("wakelocks", S_IRUGO, NULL, &wakelock_stats_fops);
605 #endif
606
607         return 0;
608
609 err_suspend_work_queue:
610         platform_driver_unregister(&power_driver);
611 err_platform_driver_register:
612         platform_device_unregister(&power_device);
613 err_platform_device_register:
614         wake_lock_destroy(&suspend_backoff_lock);
615         wake_lock_destroy(&unknown_wakeup);
616         wake_lock_destroy(&main_wake_lock);
617 #ifdef CONFIG_WAKELOCK_STAT
618         wake_lock_destroy(&deleted_wake_locks);
619 #endif
620         return ret;
621 }
622
623 static void  __exit wakelocks_exit(void)
624 {
625 #ifdef CONFIG_WAKELOCK_STAT
626         remove_proc_entry("wakelocks", NULL);
627 #endif
628         destroy_workqueue(suspend_work_queue);
629         platform_driver_unregister(&power_driver);
630         platform_device_unregister(&power_device);
631         wake_lock_destroy(&suspend_backoff_lock);
632         wake_lock_destroy(&unknown_wakeup);
633         wake_lock_destroy(&main_wake_lock);
634 #ifdef CONFIG_WAKELOCK_STAT
635         wake_lock_destroy(&deleted_wake_locks);
636 #endif
637 }
638
639 core_initcall(wakelocks_init);
640 module_exit(wakelocks_exit);