htcleo: add Cotulla's fixes for non-android touchscreen!
[htc-linux.git] / kernel / power / wakelock.c
blob530dbcef34aef9ebd7d9e9731e73259e1a278a27
1 /* kernel/power/wakelock.c
3 * Copyright (C) 2005-2008 Google, Inc.
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.
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.
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"
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 DEBUG_FORBID_SUSPEND = 1U << 5,
35 static int debug_mask = DEBUG_EXIT_SUSPEND | DEBUG_WAKEUP | DEBUG_FORBID_SUSPEND;
36 module_param_named(debug_mask, debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP);
38 #define WAKE_LOCK_TYPE_MASK (0x0f)
39 #define WAKE_LOCK_INITIALIZED (1U << 8)
40 #define WAKE_LOCK_ACTIVE (1U << 9)
41 #define WAKE_LOCK_AUTO_EXPIRE (1U << 10)
42 #define WAKE_LOCK_PREVENTING_SUSPEND (1U << 11)
44 static DEFINE_SPINLOCK(list_lock);
45 static LIST_HEAD(inactive_locks);
46 static struct list_head active_wake_locks[WAKE_LOCK_TYPE_COUNT];
47 static int current_event_num;
48 struct workqueue_struct *suspend_work_queue;
49 struct wake_lock main_wake_lock;
50 suspend_state_t requested_suspend_state = PM_SUSPEND_MEM;
51 static struct wake_lock unknown_wakeup;
53 #ifdef CONFIG_WAKELOCK_STAT
54 static struct wake_lock deleted_wake_locks;
55 static ktime_t last_sleep_time_update;
56 static int wait_for_wakeup;
58 int get_expired_time(struct wake_lock *lock, ktime_t *expire_time)
60 struct timespec ts;
61 struct timespec kt;
62 struct timespec tomono;
63 struct timespec delta;
64 unsigned long seq;
65 long timeout;
67 if (!(lock->flags & WAKE_LOCK_AUTO_EXPIRE))
68 return 0;
69 do {
70 seq = read_seqbegin(&xtime_lock);
71 timeout = lock->expires - jiffies;
72 if (timeout > 0)
73 return 0;
74 kt = current_kernel_time();
75 tomono = wall_to_monotonic;
76 } while (read_seqretry(&xtime_lock, seq));
77 jiffies_to_timespec(-timeout, &delta);
78 set_normalized_timespec(&ts, kt.tv_sec + tomono.tv_sec - delta.tv_sec,
79 kt.tv_nsec + tomono.tv_nsec - delta.tv_nsec);
80 *expire_time = timespec_to_ktime(ts);
81 return 1;
85 static int print_lock_stat(struct seq_file *m, struct wake_lock *lock)
87 int lock_count = lock->stat.count;
88 int expire_count = lock->stat.expire_count;
89 ktime_t active_time = ktime_set(0, 0);
90 ktime_t total_time = lock->stat.total_time;
91 ktime_t max_time = lock->stat.max_time;
93 ktime_t prevent_suspend_time = lock->stat.prevent_suspend_time;
94 if (lock->flags & WAKE_LOCK_ACTIVE) {
95 ktime_t now, add_time;
96 int expired = get_expired_time(lock, &now);
97 if (!expired)
98 now = ktime_get();
99 add_time = ktime_sub(now, lock->stat.last_time);
100 lock_count++;
101 if (!expired)
102 active_time = add_time;
103 else
104 expire_count++;
105 total_time = ktime_add(total_time, add_time);
106 if (lock->flags & WAKE_LOCK_PREVENTING_SUSPEND)
107 prevent_suspend_time = ktime_add(prevent_suspend_time,
108 ktime_sub(now, last_sleep_time_update));
109 if (add_time.tv64 > max_time.tv64)
110 max_time = add_time;
113 return seq_printf(m,
114 "\"%s\"\t%d\t%d\t%d\t%lld\t%lld\t%lld\t%lld\t%lld\n",
115 lock->name, lock_count, expire_count,
116 lock->stat.wakeup_count, ktime_to_ns(active_time),
117 ktime_to_ns(total_time),
118 ktime_to_ns(prevent_suspend_time), ktime_to_ns(max_time),
119 ktime_to_ns(lock->stat.last_time));
122 static int wakelock_stats_show(struct seq_file *m, void *unused)
124 unsigned long irqflags;
125 struct wake_lock *lock;
126 int ret;
127 int type;
129 spin_lock_irqsave(&list_lock, irqflags);
131 ret = seq_puts(m, "name\tcount\texpire_count\twake_count\tactive_since"
132 "\ttotal_time\tsleep_time\tmax_time\tlast_change\n");
133 list_for_each_entry(lock, &inactive_locks, link)
134 ret = print_lock_stat(m, lock);
135 for (type = 0; type < WAKE_LOCK_TYPE_COUNT; type++) {
136 list_for_each_entry(lock, &active_wake_locks[type], link)
137 ret = print_lock_stat(m, lock);
139 spin_unlock_irqrestore(&list_lock, irqflags);
140 return 0;
143 static void wake_unlock_stat_locked(struct wake_lock *lock, int expired)
145 ktime_t duration;
146 ktime_t now;
147 if (!(lock->flags & WAKE_LOCK_ACTIVE))
148 return;
149 if (get_expired_time(lock, &now))
150 expired = 1;
151 else
152 now = ktime_get();
153 lock->stat.count++;
154 if (expired)
155 lock->stat.expire_count++;
156 duration = ktime_sub(now, lock->stat.last_time);
157 lock->stat.total_time = ktime_add(lock->stat.total_time, duration);
158 if (ktime_to_ns(duration) > ktime_to_ns(lock->stat.max_time))
159 lock->stat.max_time = duration;
160 lock->stat.last_time = ktime_get();
161 if (lock->flags & WAKE_LOCK_PREVENTING_SUSPEND) {
162 duration = ktime_sub(now, last_sleep_time_update);
163 lock->stat.prevent_suspend_time = ktime_add(
164 lock->stat.prevent_suspend_time, duration);
165 lock->flags &= ~WAKE_LOCK_PREVENTING_SUSPEND;
169 static void update_sleep_wait_stats_locked(int done)
171 struct wake_lock *lock;
172 ktime_t now, etime, elapsed, add;
173 int expired;
175 now = ktime_get();
176 elapsed = ktime_sub(now, last_sleep_time_update);
177 list_for_each_entry(lock, &active_wake_locks[WAKE_LOCK_SUSPEND], link) {
178 expired = get_expired_time(lock, &etime);
179 if (lock->flags & WAKE_LOCK_PREVENTING_SUSPEND) {
180 if (expired)
181 add = ktime_sub(etime, last_sleep_time_update);
182 else
183 add = elapsed;
184 lock->stat.prevent_suspend_time = ktime_add(
185 lock->stat.prevent_suspend_time, add);
187 if (done || expired)
188 lock->flags &= ~WAKE_LOCK_PREVENTING_SUSPEND;
189 else
190 lock->flags |= WAKE_LOCK_PREVENTING_SUSPEND;
192 last_sleep_time_update = now;
194 #endif
197 static void expire_wake_lock(struct wake_lock *lock)
199 #ifdef CONFIG_WAKELOCK_STAT
200 wake_unlock_stat_locked(lock, 1);
201 #endif
202 lock->flags &= ~(WAKE_LOCK_ACTIVE | WAKE_LOCK_AUTO_EXPIRE);
203 list_del(&lock->link);
204 list_add(&lock->link, &inactive_locks);
205 if (debug_mask & (DEBUG_WAKE_LOCK | DEBUG_EXPIRE))
206 pr_info("expired wake lock %s\n", lock->name);
209 /* Caller must acquire the list_lock spinlock */
210 static void print_active_locks_locked(int type)
212 struct wake_lock *lock;
214 BUG_ON(type >= WAKE_LOCK_TYPE_COUNT);
215 list_for_each_entry(lock, &active_wake_locks[type], link) {
216 if (lock->flags & WAKE_LOCK_AUTO_EXPIRE) {
217 long timeout = lock->expires - jiffies;
218 if (timeout <= 0)
219 pr_info("wake lock %s, expired\n", lock->name);
220 else
221 pr_info("active wake lock %s, time left %ld.%03lu\n",
222 lock->name, timeout / HZ,
223 (timeout % HZ) * MSEC_PER_SEC / HZ);
224 } else
225 pr_info("active wake lock %s\n", lock->name);
229 void print_active_locks(int type)
231 unsigned long irqflags;
232 spin_lock_irqsave(&list_lock, irqflags);
233 print_active_locks_locked(type);
234 spin_unlock_irqrestore(&list_lock, irqflags);
237 static long has_wake_lock_locked(int type)
239 struct wake_lock *lock, *n;
240 long max_timeout = 0;
242 BUG_ON(type >= WAKE_LOCK_TYPE_COUNT);
243 list_for_each_entry_safe(lock, n, &active_wake_locks[type], link) {
244 if (lock->flags & WAKE_LOCK_AUTO_EXPIRE) {
245 long timeout = lock->expires - jiffies;
246 if (timeout <= 0)
247 expire_wake_lock(lock);
248 else if (timeout > max_timeout)
249 max_timeout = timeout;
250 } else
251 return -1;
253 return max_timeout;
256 long has_wake_lock(int type)
258 long ret;
259 unsigned long irqflags;
260 spin_lock_irqsave(&list_lock, irqflags);
261 ret = has_wake_lock_locked(type);
262 spin_unlock_irqrestore(&list_lock, irqflags);
263 return ret;
266 static void suspend(struct work_struct *work)
268 int ret;
269 int entry_event_num;
270 unsigned long irqflags;
272 pr_info("[R] suspend start\n");
273 if (has_wake_lock(WAKE_LOCK_SUSPEND)) {
274 if (debug_mask & DEBUG_SUSPEND || debug_mask & DEBUG_FORBID_SUSPEND)
275 pr_info("suspend: abort suspend\n");
276 if (debug_mask & DEBUG_FORBID_SUSPEND)
277 print_active_locks(WAKE_LOCK_SUSPEND);
278 return;
281 entry_event_num = current_event_num;
282 sys_sync();
283 if (debug_mask & DEBUG_SUSPEND)
284 pr_info("suspend: enter suspend\n");
285 ret = pm_suspend(requested_suspend_state);
286 if (debug_mask & DEBUG_EXIT_SUSPEND) {
287 struct timespec ts;
288 struct rtc_time tm;
289 getnstimeofday(&ts);
290 rtc_time_to_tm(ts.tv_sec, &tm);
291 pr_info("suspend: exit suspend, ret = %d "
292 "(%d-%02d-%02d %02d:%02d:%02d.%09lu UTC)\n", ret,
293 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
294 tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec);
296 if (current_event_num == entry_event_num) {
297 if (debug_mask & DEBUG_SUSPEND)
298 pr_info("suspend: pm_suspend returned with no event\n");
299 wake_lock_timeout(&unknown_wakeup, HZ / 2);
301 pr_info("[R] resume end\n");
303 static DECLARE_WORK(suspend_work, suspend);
305 static void expire_wake_locks(unsigned long data)
307 long has_lock;
308 unsigned long irqflags;
309 if (debug_mask & DEBUG_EXPIRE)
310 pr_info("expire_wake_locks: start\n");
311 spin_lock_irqsave(&list_lock, irqflags);
312 if (debug_mask & DEBUG_SUSPEND)
313 print_active_locks_locked(WAKE_LOCK_SUSPEND);
314 has_lock = has_wake_lock_locked(WAKE_LOCK_SUSPEND);
315 if (debug_mask & DEBUG_EXPIRE)
316 pr_info("expire_wake_locks: done, has_lock %ld\n", has_lock);
317 if (has_lock == 0)
318 queue_work(suspend_work_queue, &suspend_work);
319 spin_unlock_irqrestore(&list_lock, irqflags);
321 static DEFINE_TIMER(expire_timer, expire_wake_locks, 0, 0);
323 static int power_suspend_late(struct device *dev)
325 int ret = has_wake_lock(WAKE_LOCK_SUSPEND) ? -EAGAIN : 0;
326 #ifdef CONFIG_WAKELOCK_STAT
327 wait_for_wakeup = 1;
328 #endif
329 if (debug_mask & DEBUG_SUSPEND)
330 pr_info("power_suspend_late return %d\n", ret);
331 if (ret && (debug_mask & DEBUG_FORBID_SUSPEND))
332 print_active_locks(WAKE_LOCK_SUSPEND);
333 return ret;
336 static struct dev_pm_ops power_driver_pm_ops = {
337 .suspend_noirq = power_suspend_late,
340 static struct platform_driver power_driver = {
341 .driver.name = "power",
342 .driver.pm = &power_driver_pm_ops,
344 static struct platform_device power_device = {
345 .name = "power",
348 void wake_lock_init(struct wake_lock *lock, int type, const char *name)
350 unsigned long irqflags = 0;
352 if (name)
353 lock->name = name;
354 BUG_ON(!lock->name);
356 if (debug_mask & DEBUG_WAKE_LOCK)
357 pr_info("wake_lock_init name=%s\n", lock->name);
358 #ifdef CONFIG_WAKELOCK_STAT
359 lock->stat.count = 0;
360 lock->stat.expire_count = 0;
361 lock->stat.wakeup_count = 0;
362 lock->stat.total_time = ktime_set(0, 0);
363 lock->stat.prevent_suspend_time = ktime_set(0, 0);
364 lock->stat.max_time = ktime_set(0, 0);
365 lock->stat.last_time = ktime_set(0, 0);
366 #endif
367 lock->flags = (type & WAKE_LOCK_TYPE_MASK) | WAKE_LOCK_INITIALIZED;
369 INIT_LIST_HEAD(&lock->link);
370 spin_lock_irqsave(&list_lock, irqflags);
371 list_add(&lock->link, &inactive_locks);
372 spin_unlock_irqrestore(&list_lock, irqflags);
374 EXPORT_SYMBOL(wake_lock_init);
376 void wake_lock_destroy(struct wake_lock *lock)
378 unsigned long irqflags;
379 if (debug_mask & DEBUG_WAKE_LOCK)
380 pr_info("wake_lock_destroy name=%s\n", lock->name);
381 spin_lock_irqsave(&list_lock, irqflags);
382 lock->flags &= ~WAKE_LOCK_INITIALIZED;
383 #ifdef CONFIG_WAKELOCK_STAT
384 if (lock->stat.count) {
385 deleted_wake_locks.stat.count += lock->stat.count;
386 deleted_wake_locks.stat.expire_count += lock->stat.expire_count;
387 deleted_wake_locks.stat.total_time =
388 ktime_add(deleted_wake_locks.stat.total_time,
389 lock->stat.total_time);
390 deleted_wake_locks.stat.prevent_suspend_time =
391 ktime_add(deleted_wake_locks.stat.prevent_suspend_time,
392 lock->stat.prevent_suspend_time);
393 deleted_wake_locks.stat.max_time =
394 ktime_add(deleted_wake_locks.stat.max_time,
395 lock->stat.max_time);
397 #endif
398 list_del(&lock->link);
399 spin_unlock_irqrestore(&list_lock, irqflags);
401 EXPORT_SYMBOL(wake_lock_destroy);
403 static void wake_lock_internal(
404 struct wake_lock *lock, long timeout, int has_timeout)
406 int type;
407 unsigned long irqflags;
408 long expire_in;
410 spin_lock_irqsave(&list_lock, irqflags);
411 type = lock->flags & WAKE_LOCK_TYPE_MASK;
412 BUG_ON(type >= WAKE_LOCK_TYPE_COUNT);
413 BUG_ON(!(lock->flags & WAKE_LOCK_INITIALIZED));
414 #ifdef CONFIG_WAKELOCK_STAT
415 if (type == WAKE_LOCK_SUSPEND && wait_for_wakeup) {
416 if (debug_mask & DEBUG_WAKEUP)
417 pr_info("wakeup wake lock: %s\n", lock->name);
418 wait_for_wakeup = 0;
419 lock->stat.wakeup_count++;
421 if ((lock->flags & WAKE_LOCK_AUTO_EXPIRE) &&
422 (long)(lock->expires - jiffies) <= 0) {
423 wake_unlock_stat_locked(lock, 0);
424 lock->stat.last_time = ktime_get();
426 #endif
427 if (!(lock->flags & WAKE_LOCK_ACTIVE)) {
428 lock->flags |= WAKE_LOCK_ACTIVE;
429 #ifdef CONFIG_WAKELOCK_STAT
430 lock->stat.last_time = ktime_get();
431 #endif
433 list_del(&lock->link);
434 if (has_timeout) {
435 if (debug_mask & DEBUG_WAKE_LOCK)
436 pr_info("wake_lock: %s, type %d, timeout %ld.%03lu\n",
437 lock->name, type, timeout / HZ,
438 (timeout % HZ) * MSEC_PER_SEC / HZ);
439 lock->expires = jiffies + timeout;
440 lock->flags |= WAKE_LOCK_AUTO_EXPIRE;
441 list_add_tail(&lock->link, &active_wake_locks[type]);
442 } else {
443 if (debug_mask & DEBUG_WAKE_LOCK)
444 pr_info("wake_lock: %s, type %d\n", lock->name, type);
445 lock->expires = LONG_MAX;
446 lock->flags &= ~WAKE_LOCK_AUTO_EXPIRE;
447 list_add(&lock->link, &active_wake_locks[type]);
449 if (type == WAKE_LOCK_SUSPEND) {
450 current_event_num++;
451 #ifdef CONFIG_WAKELOCK_STAT
452 if (lock == &main_wake_lock)
453 update_sleep_wait_stats_locked(1);
454 else if (!wake_lock_active(&main_wake_lock))
455 update_sleep_wait_stats_locked(0);
456 #endif
457 if (has_timeout)
458 expire_in = has_wake_lock_locked(type);
459 else
460 expire_in = -1;
461 if (expire_in > 0) {
462 if (debug_mask & DEBUG_EXPIRE)
463 pr_info("wake_lock: %s, start expire timer, "
464 "%ld\n", lock->name, expire_in);
465 mod_timer(&expire_timer, jiffies + expire_in);
466 } else {
467 if (del_timer(&expire_timer))
468 if (debug_mask & DEBUG_EXPIRE)
469 pr_info("wake_lock: %s, stop expire timer\n",
470 lock->name);
471 if (expire_in == 0)
472 queue_work(suspend_work_queue, &suspend_work);
475 spin_unlock_irqrestore(&list_lock, irqflags);
478 void wake_lock(struct wake_lock *lock)
480 wake_lock_internal(lock, 0, 0);
482 EXPORT_SYMBOL(wake_lock);
484 void wake_lock_timeout(struct wake_lock *lock, long timeout)
486 wake_lock_internal(lock, timeout, 1);
488 EXPORT_SYMBOL(wake_lock_timeout);
490 void wake_unlock(struct wake_lock *lock)
492 int type;
493 unsigned long irqflags;
494 spin_lock_irqsave(&list_lock, irqflags);
495 type = lock->flags & WAKE_LOCK_TYPE_MASK;
496 #ifdef CONFIG_WAKELOCK_STAT
497 wake_unlock_stat_locked(lock, 0);
498 #endif
499 if (debug_mask & DEBUG_WAKE_LOCK)
500 pr_info("wake_unlock: %s\n", lock->name);
501 lock->flags &= ~(WAKE_LOCK_ACTIVE | WAKE_LOCK_AUTO_EXPIRE);
502 list_del(&lock->link);
503 list_add(&lock->link, &inactive_locks);
504 if (type == WAKE_LOCK_SUSPEND) {
505 long has_lock = has_wake_lock_locked(type);
506 if (has_lock > 0) {
507 if (debug_mask & DEBUG_EXPIRE)
508 pr_info("wake_unlock: %s, start expire timer, "
509 "%ld\n", lock->name, has_lock);
510 mod_timer(&expire_timer, jiffies + has_lock);
511 } else {
512 if (del_timer(&expire_timer))
513 if (debug_mask & DEBUG_EXPIRE)
514 pr_info("wake_unlock: %s, stop expire "
515 "timer\n", lock->name);
516 if (has_lock == 0)
517 queue_work(suspend_work_queue, &suspend_work);
519 if (lock == &main_wake_lock) {
520 if (debug_mask & DEBUG_SUSPEND)
521 print_active_locks_locked(WAKE_LOCK_SUSPEND);
522 #ifdef CONFIG_WAKELOCK_STAT
523 update_sleep_wait_stats_locked(0);
524 #endif
527 spin_unlock_irqrestore(&list_lock, irqflags);
529 EXPORT_SYMBOL(wake_unlock);
531 int wake_lock_active(struct wake_lock *lock)
533 return !!(lock->flags & WAKE_LOCK_ACTIVE);
535 EXPORT_SYMBOL(wake_lock_active);
537 static int wakelock_stats_open(struct inode *inode, struct file *file)
539 return single_open(file, wakelock_stats_show, NULL);
542 static const struct file_operations wakelock_stats_fops = {
543 .owner = THIS_MODULE,
544 .open = wakelock_stats_open,
545 .read = seq_read,
546 .llseek = seq_lseek,
547 .release = single_release,
550 static int __init wakelocks_init(void)
552 int ret;
553 int i;
555 for (i = 0; i < ARRAY_SIZE(active_wake_locks); i++)
556 INIT_LIST_HEAD(&active_wake_locks[i]);
558 #ifdef CONFIG_WAKELOCK_STAT
559 wake_lock_init(&deleted_wake_locks, WAKE_LOCK_SUSPEND,
560 "deleted_wake_locks");
561 #endif
562 wake_lock_init(&main_wake_lock, WAKE_LOCK_SUSPEND, "main");
563 wake_lock(&main_wake_lock);
564 wake_lock_init(&unknown_wakeup, WAKE_LOCK_SUSPEND, "unknown_wakeups");
566 ret = platform_device_register(&power_device);
567 if (ret) {
568 pr_err("wakelocks_init: platform_device_register failed\n");
569 goto err_platform_device_register;
571 ret = platform_driver_register(&power_driver);
572 if (ret) {
573 pr_err("wakelocks_init: platform_driver_register failed\n");
574 goto err_platform_driver_register;
577 suspend_work_queue = create_singlethread_workqueue("suspend");
578 if (suspend_work_queue == NULL) {
579 ret = -ENOMEM;
580 goto err_suspend_work_queue;
583 #ifdef CONFIG_WAKELOCK_STAT
584 proc_create("wakelocks", S_IRUGO, NULL, &wakelock_stats_fops);
585 #endif
587 return 0;
589 err_suspend_work_queue:
590 platform_driver_unregister(&power_driver);
591 err_platform_driver_register:
592 platform_device_unregister(&power_device);
593 err_platform_device_register:
594 wake_lock_destroy(&unknown_wakeup);
595 wake_lock_destroy(&main_wake_lock);
596 #ifdef CONFIG_WAKELOCK_STAT
597 wake_lock_destroy(&deleted_wake_locks);
598 #endif
599 return ret;
602 static void __exit wakelocks_exit(void)
604 #ifdef CONFIG_WAKELOCK_STAT
605 remove_proc_entry("wakelocks", NULL);
606 #endif
607 destroy_workqueue(suspend_work_queue);
608 platform_driver_unregister(&power_driver);
609 platform_device_unregister(&power_device);
610 wake_lock_destroy(&unknown_wakeup);
611 wake_lock_destroy(&main_wake_lock);
612 #ifdef CONFIG_WAKELOCK_STAT
613 wake_lock_destroy(&deleted_wake_locks);
614 #endif
617 core_initcall(wakelocks_init);
618 module_exit(wakelocks_exit);