PM: Backoff suspend if repeated attempts fail
[linux-2.6.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(int type)
212 {
213         struct wake_lock *lock;
214         bool print_expired = true;
215
216         BUG_ON(type >= WAKE_LOCK_TYPE_COUNT);
217         list_for_each_entry(lock, &active_wake_locks[type], link) {
218                 if (lock->flags & WAKE_LOCK_AUTO_EXPIRE) {
219                         long timeout = lock->expires - jiffies;
220                         if (timeout > 0)
221                                 pr_info("active wake lock %s, time left %ld\n",
222                                         lock->name, timeout);
223                         else if (print_expired)
224                                 pr_info("wake lock %s, expired\n", lock->name);
225                 } else {
226                         pr_info("active wake lock %s\n", lock->name);
227                         if (!(debug_mask & DEBUG_EXPIRE))
228                                 print_expired = false;
229                 }
230         }
231 }
232
233 static long has_wake_lock_locked(int type)
234 {
235         struct wake_lock *lock, *n;
236         long max_timeout = 0;
237
238         BUG_ON(type >= WAKE_LOCK_TYPE_COUNT);
239         list_for_each_entry_safe(lock, n, &active_wake_locks[type], link) {
240                 if (lock->flags & WAKE_LOCK_AUTO_EXPIRE) {
241                         long timeout = lock->expires - jiffies;
242                         if (timeout <= 0)
243                                 expire_wake_lock(lock);
244                         else if (timeout > max_timeout)
245                                 max_timeout = timeout;
246                 } else
247                         return -1;
248         }
249         return max_timeout;
250 }
251
252 long has_wake_lock(int type)
253 {
254         long ret;
255         unsigned long irqflags;
256         spin_lock_irqsave(&list_lock, irqflags);
257         ret = has_wake_lock_locked(type);
258         if (ret && (debug_mask & DEBUG_WAKEUP) && type == WAKE_LOCK_SUSPEND)
259                 print_active_locks(type);
260         spin_unlock_irqrestore(&list_lock, irqflags);
261         return ret;
262 }
263
264 static void suspend_backoff(void)
265 {
266         pr_info("suspend: too many immediate wakeups, back off\n");
267         wake_lock_timeout(&suspend_backoff_lock,
268                           msecs_to_jiffies(SUSPEND_BACKOFF_INTERVAL));
269 }
270
271 static void suspend(struct work_struct *work)
272 {
273         int ret;
274         int entry_event_num;
275         struct timespec ts_entry, ts_exit;
276
277         if (has_wake_lock(WAKE_LOCK_SUSPEND)) {
278                 if (debug_mask & DEBUG_SUSPEND)
279                         pr_info("suspend: abort suspend\n");
280                 return;
281         }
282
283         entry_event_num = current_event_num;
284         sys_sync();
285         if (debug_mask & DEBUG_SUSPEND)
286                 pr_info("suspend: enter suspend\n");
287         getnstimeofday(&ts_entry);
288         ret = pm_suspend(requested_suspend_state);
289         getnstimeofday(&ts_exit);
290
291         if (debug_mask & DEBUG_EXIT_SUSPEND) {
292                 struct rtc_time tm;
293                 rtc_time_to_tm(ts_exit.tv_sec, &tm);
294                 pr_info("suspend: exit suspend, ret = %d "
295                         "(%d-%02d-%02d %02d:%02d:%02d.%09lu UTC)\n", ret,
296                         tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
297                         tm.tm_hour, tm.tm_min, tm.tm_sec, ts_exit.tv_nsec);
298         }
299
300         if (ts_exit.tv_sec - ts_entry.tv_sec <= 1) {
301                 ++suspend_short_count;
302
303                 if (suspend_short_count == SUSPEND_BACKOFF_THRESHOLD) {
304                         suspend_backoff();
305                         suspend_short_count = 0;
306                 }
307         } else {
308                 suspend_short_count = 0;
309         }
310
311         if (current_event_num == entry_event_num) {
312                 if (debug_mask & DEBUG_SUSPEND)
313                         pr_info("suspend: pm_suspend returned with no event\n");
314                 wake_lock_timeout(&unknown_wakeup, HZ / 2);
315         }
316 }
317 static DECLARE_WORK(suspend_work, suspend);
318
319 static void expire_wake_locks(unsigned long data)
320 {
321         long has_lock;
322         unsigned long irqflags;
323         if (debug_mask & DEBUG_EXPIRE)
324                 pr_info("expire_wake_locks: start\n");
325         spin_lock_irqsave(&list_lock, irqflags);
326         if (debug_mask & DEBUG_SUSPEND)
327                 print_active_locks(WAKE_LOCK_SUSPEND);
328         has_lock = has_wake_lock_locked(WAKE_LOCK_SUSPEND);
329         if (debug_mask & DEBUG_EXPIRE)
330                 pr_info("expire_wake_locks: done, has_lock %ld\n", has_lock);
331         if (has_lock == 0)
332                 queue_work(suspend_work_queue, &suspend_work);
333         spin_unlock_irqrestore(&list_lock, irqflags);
334 }
335 static DEFINE_TIMER(expire_timer, expire_wake_locks, 0, 0);
336
337 static int power_suspend_late(struct device *dev)
338 {
339         int ret = has_wake_lock(WAKE_LOCK_SUSPEND) ? -EAGAIN : 0;
340 #ifdef CONFIG_WAKELOCK_STAT
341         wait_for_wakeup = !ret;
342 #endif
343         if (debug_mask & DEBUG_SUSPEND)
344                 pr_info("power_suspend_late return %d\n", ret);
345         return ret;
346 }
347
348 static struct dev_pm_ops power_driver_pm_ops = {
349         .suspend_noirq = power_suspend_late,
350 };
351
352 static struct platform_driver power_driver = {
353         .driver.name = "power",
354         .driver.pm = &power_driver_pm_ops,
355 };
356 static struct platform_device power_device = {
357         .name = "power",
358 };
359
360 void wake_lock_init(struct wake_lock *lock, int type, const char *name)
361 {
362         unsigned long irqflags = 0;
363
364         if (name)
365                 lock->name = name;
366         BUG_ON(!lock->name);
367
368         if (debug_mask & DEBUG_WAKE_LOCK)
369                 pr_info("wake_lock_init name=%s\n", lock->name);
370 #ifdef CONFIG_WAKELOCK_STAT
371         lock->stat.count = 0;
372         lock->stat.expire_count = 0;
373         lock->stat.wakeup_count = 0;
374         lock->stat.total_time = ktime_set(0, 0);
375         lock->stat.prevent_suspend_time = ktime_set(0, 0);
376         lock->stat.max_time = ktime_set(0, 0);
377         lock->stat.last_time = ktime_set(0, 0);
378 #endif
379         lock->flags = (type & WAKE_LOCK_TYPE_MASK) | WAKE_LOCK_INITIALIZED;
380
381         INIT_LIST_HEAD(&lock->link);
382         spin_lock_irqsave(&list_lock, irqflags);
383         list_add(&lock->link, &inactive_locks);
384         spin_unlock_irqrestore(&list_lock, irqflags);
385 }
386 EXPORT_SYMBOL(wake_lock_init);
387
388 void wake_lock_destroy(struct wake_lock *lock)
389 {
390         unsigned long irqflags;
391         if (debug_mask & DEBUG_WAKE_LOCK)
392                 pr_info("wake_lock_destroy name=%s\n", lock->name);
393         spin_lock_irqsave(&list_lock, irqflags);
394         lock->flags &= ~WAKE_LOCK_INITIALIZED;
395 #ifdef CONFIG_WAKELOCK_STAT
396         if (lock->stat.count) {
397                 deleted_wake_locks.stat.count += lock->stat.count;
398                 deleted_wake_locks.stat.expire_count += lock->stat.expire_count;
399                 deleted_wake_locks.stat.total_time =
400                         ktime_add(deleted_wake_locks.stat.total_time,
401                                   lock->stat.total_time);
402                 deleted_wake_locks.stat.prevent_suspend_time =
403                         ktime_add(deleted_wake_locks.stat.prevent_suspend_time,
404                                   lock->stat.prevent_suspend_time);
405                 deleted_wake_locks.stat.max_time =
406                         ktime_add(deleted_wake_locks.stat.max_time,
407                                   lock->stat.max_time);
408         }
409 #endif
410         list_del(&lock->link);
411         spin_unlock_irqrestore(&list_lock, irqflags);
412 }
413 EXPORT_SYMBOL(wake_lock_destroy);
414
415 static void wake_lock_internal(
416         struct wake_lock *lock, long timeout, int has_timeout)
417 {
418         int type;
419         unsigned long irqflags;
420         long expire_in;
421
422         spin_lock_irqsave(&list_lock, irqflags);
423         type = lock->flags & WAKE_LOCK_TYPE_MASK;
424         BUG_ON(type >= WAKE_LOCK_TYPE_COUNT);
425         BUG_ON(!(lock->flags & WAKE_LOCK_INITIALIZED));
426 #ifdef CONFIG_WAKELOCK_STAT
427         if (type == WAKE_LOCK_SUSPEND && wait_for_wakeup) {
428                 if (debug_mask & DEBUG_WAKEUP)
429                         pr_info("wakeup wake lock: %s\n", lock->name);
430                 wait_for_wakeup = 0;
431                 lock->stat.wakeup_count++;
432         }
433         if ((lock->flags & WAKE_LOCK_AUTO_EXPIRE) &&
434             (long)(lock->expires - jiffies) <= 0) {
435                 wake_unlock_stat_locked(lock, 0);
436                 lock->stat.last_time = ktime_get();
437         }
438 #endif
439         if (!(lock->flags & WAKE_LOCK_ACTIVE)) {
440                 lock->flags |= WAKE_LOCK_ACTIVE;
441 #ifdef CONFIG_WAKELOCK_STAT
442                 lock->stat.last_time = ktime_get();
443 #endif
444         }
445         list_del(&lock->link);
446         if (has_timeout) {
447                 if (debug_mask & DEBUG_WAKE_LOCK)
448                         pr_info("wake_lock: %s, type %d, timeout %ld.%03lu\n",
449                                 lock->name, type, timeout / HZ,
450                                 (timeout % HZ) * MSEC_PER_SEC / HZ);
451                 lock->expires = jiffies + timeout;
452                 lock->flags |= WAKE_LOCK_AUTO_EXPIRE;
453                 list_add_tail(&lock->link, &active_wake_locks[type]);
454         } else {
455                 if (debug_mask & DEBUG_WAKE_LOCK)
456                         pr_info("wake_lock: %s, type %d\n", lock->name, type);
457                 lock->expires = LONG_MAX;
458                 lock->flags &= ~WAKE_LOCK_AUTO_EXPIRE;
459                 list_add(&lock->link, &active_wake_locks[type]);
460         }
461         if (type == WAKE_LOCK_SUSPEND) {
462                 current_event_num++;
463 #ifdef CONFIG_WAKELOCK_STAT
464                 if (lock == &main_wake_lock)
465                         update_sleep_wait_stats_locked(1);
466                 else if (!wake_lock_active(&main_wake_lock))
467                         update_sleep_wait_stats_locked(0);
468 #endif
469                 if (has_timeout)
470                         expire_in = has_wake_lock_locked(type);
471                 else
472                         expire_in = -1;
473                 if (expire_in > 0) {
474                         if (debug_mask & DEBUG_EXPIRE)
475                                 pr_info("wake_lock: %s, start expire timer, "
476                                         "%ld\n", lock->name, expire_in);
477                         mod_timer(&expire_timer, jiffies + expire_in);
478                 } else {
479                         if (del_timer(&expire_timer))
480                                 if (debug_mask & DEBUG_EXPIRE)
481                                         pr_info("wake_lock: %s, stop expire timer\n",
482                                                 lock->name);
483                         if (expire_in == 0)
484                                 queue_work(suspend_work_queue, &suspend_work);
485                 }
486         }
487         spin_unlock_irqrestore(&list_lock, irqflags);
488 }
489
490 void wake_lock(struct wake_lock *lock)
491 {
492         wake_lock_internal(lock, 0, 0);
493 }
494 EXPORT_SYMBOL(wake_lock);
495
496 void wake_lock_timeout(struct wake_lock *lock, long timeout)
497 {
498         wake_lock_internal(lock, timeout, 1);
499 }
500 EXPORT_SYMBOL(wake_lock_timeout);
501
502 void wake_unlock(struct wake_lock *lock)
503 {
504         int type;
505         unsigned long irqflags;
506         spin_lock_irqsave(&list_lock, irqflags);
507         type = lock->flags & WAKE_LOCK_TYPE_MASK;
508 #ifdef CONFIG_WAKELOCK_STAT
509         wake_unlock_stat_locked(lock, 0);
510 #endif
511         if (debug_mask & DEBUG_WAKE_LOCK)
512                 pr_info("wake_unlock: %s\n", lock->name);
513         lock->flags &= ~(WAKE_LOCK_ACTIVE | WAKE_LOCK_AUTO_EXPIRE);
514         list_del(&lock->link);
515         list_add(&lock->link, &inactive_locks);
516         if (type == WAKE_LOCK_SUSPEND) {
517                 long has_lock = has_wake_lock_locked(type);
518                 if (has_lock > 0) {
519                         if (debug_mask & DEBUG_EXPIRE)
520                                 pr_info("wake_unlock: %s, start expire timer, "
521                                         "%ld\n", lock->name, has_lock);
522                         mod_timer(&expire_timer, jiffies + has_lock);
523                 } else {
524                         if (del_timer(&expire_timer))
525                                 if (debug_mask & DEBUG_EXPIRE)
526                                         pr_info("wake_unlock: %s, stop expire "
527                                                 "timer\n", lock->name);
528                         if (has_lock == 0)
529                                 queue_work(suspend_work_queue, &suspend_work);
530                 }
531                 if (lock == &main_wake_lock) {
532                         if (debug_mask & DEBUG_SUSPEND)
533                                 print_active_locks(WAKE_LOCK_SUSPEND);
534 #ifdef CONFIG_WAKELOCK_STAT
535                         update_sleep_wait_stats_locked(0);
536 #endif
537                 }
538         }
539         spin_unlock_irqrestore(&list_lock, irqflags);
540 }
541 EXPORT_SYMBOL(wake_unlock);
542
543 int wake_lock_active(struct wake_lock *lock)
544 {
545         return !!(lock->flags & WAKE_LOCK_ACTIVE);
546 }
547 EXPORT_SYMBOL(wake_lock_active);
548
549 static int wakelock_stats_open(struct inode *inode, struct file *file)
550 {
551         return single_open(file, wakelock_stats_show, NULL);
552 }
553
554 static const struct file_operations wakelock_stats_fops = {
555         .owner = THIS_MODULE,
556         .open = wakelock_stats_open,
557         .read = seq_read,
558         .llseek = seq_lseek,
559         .release = single_release,
560 };
561
562 static int __init wakelocks_init(void)
563 {
564         int ret;
565         int i;
566
567         for (i = 0; i < ARRAY_SIZE(active_wake_locks); i++)
568                 INIT_LIST_HEAD(&active_wake_locks[i]);
569
570 #ifdef CONFIG_WAKELOCK_STAT
571         wake_lock_init(&deleted_wake_locks, WAKE_LOCK_SUSPEND,
572                         "deleted_wake_locks");
573 #endif
574         wake_lock_init(&main_wake_lock, WAKE_LOCK_SUSPEND, "main");
575         wake_lock(&main_wake_lock);
576         wake_lock_init(&unknown_wakeup, WAKE_LOCK_SUSPEND, "unknown_wakeups");
577         wake_lock_init(&suspend_backoff_lock, WAKE_LOCK_SUSPEND,
578                        "suspend_backoff");
579
580         ret = platform_device_register(&power_device);
581         if (ret) {
582                 pr_err("wakelocks_init: platform_device_register failed\n");
583                 goto err_platform_device_register;
584         }
585         ret = platform_driver_register(&power_driver);
586         if (ret) {
587                 pr_err("wakelocks_init: platform_driver_register failed\n");
588                 goto err_platform_driver_register;
589         }
590
591         suspend_work_queue = create_singlethread_workqueue("suspend");
592         if (suspend_work_queue == NULL) {
593                 ret = -ENOMEM;
594                 goto err_suspend_work_queue;
595         }
596
597 #ifdef CONFIG_WAKELOCK_STAT
598         proc_create("wakelocks", S_IRUGO, NULL, &wakelock_stats_fops);
599 #endif
600
601         return 0;
602
603 err_suspend_work_queue:
604         platform_driver_unregister(&power_driver);
605 err_platform_driver_register:
606         platform_device_unregister(&power_device);
607 err_platform_device_register:
608         wake_lock_destroy(&suspend_backoff_lock);
609         wake_lock_destroy(&unknown_wakeup);
610         wake_lock_destroy(&main_wake_lock);
611 #ifdef CONFIG_WAKELOCK_STAT
612         wake_lock_destroy(&deleted_wake_locks);
613 #endif
614         return ret;
615 }
616
617 static void  __exit wakelocks_exit(void)
618 {
619 #ifdef CONFIG_WAKELOCK_STAT
620         remove_proc_entry("wakelocks", NULL);
621 #endif
622         destroy_workqueue(suspend_work_queue);
623         platform_driver_unregister(&power_driver);
624         platform_device_unregister(&power_device);
625         wake_lock_destroy(&suspend_backoff_lock);
626         wake_lock_destroy(&unknown_wakeup);
627         wake_lock_destroy(&main_wake_lock);
628 #ifdef CONFIG_WAKELOCK_STAT
629         wake_lock_destroy(&deleted_wake_locks);
630 #endif
631 }
632
633 core_initcall(wakelocks_init);
634 module_exit(wakelocks_exit);