rtc: set alarm logging enable
[linux-2.6.git] / drivers / rtc / alarm.c
1 /* drivers/rtc/alarm.c
2  *
3  * Copyright (C) 2007-2009 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 <asm/mach/time.h>
17 #include <linux/android_alarm.h>
18 #include <linux/device.h>
19 #include <linux/miscdevice.h>
20 #include <linux/platform_device.h>
21 #include <linux/rtc.h>
22 #include <linux/sched.h>
23 #include <linux/spinlock.h>
24 #include <linux/sysdev.h>
25 #include <linux/wakelock.h>
26
27 #define ANDROID_ALARM_PRINT_ERROR (1U << 0)
28 #define ANDROID_ALARM_PRINT_INIT_STATUS (1U << 1)
29 #define ANDROID_ALARM_PRINT_TSET (1U << 2)
30 #define ANDROID_ALARM_PRINT_CALL (1U << 3)
31 #define ANDROID_ALARM_PRINT_SUSPEND (1U << 4)
32 #define ANDROID_ALARM_PRINT_INT (1U << 5)
33 #define ANDROID_ALARM_PRINT_FLOW (1U << 6)
34
35 static int debug_mask = ANDROID_ALARM_PRINT_ERROR | \
36                         ANDROID_ALARM_PRINT_SUSPEND | \
37                         ANDROID_ALARM_PRINT_INIT_STATUS;
38 module_param_named(debug_mask, debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP);
39
40 #define pr_alarm(debug_level_mask, args...) \
41         do { \
42                 if (debug_mask & ANDROID_ALARM_PRINT_##debug_level_mask) { \
43                         pr_info(args); \
44                 } \
45         } while (0)
46
47 #define ANDROID_ALARM_WAKEUP_MASK ( \
48         ANDROID_ALARM_RTC_WAKEUP_MASK | \
49         ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP_MASK)
50
51 /* support old usespace code */
52 #define ANDROID_ALARM_SET_OLD               _IOW('a', 2, time_t) /* set alarm */
53 #define ANDROID_ALARM_SET_AND_WAIT_OLD      _IOW('a', 3, time_t)
54
55 struct alarm_queue {
56         struct rb_root alarms;
57         struct rb_node *first;
58         struct hrtimer timer;
59         ktime_t delta;
60         bool stopped;
61         ktime_t stopped_time;
62 };
63
64 static struct rtc_device *alarm_rtc_dev;
65 static DEFINE_SPINLOCK(alarm_slock);
66 static DEFINE_MUTEX(alarm_setrtc_mutex);
67 static struct wake_lock alarm_rtc_wake_lock;
68 static struct platform_device *alarm_platform_dev;
69 struct alarm_queue alarms[ANDROID_ALARM_TYPE_COUNT];
70 static bool suspended;
71
72 static void update_timer_locked(struct alarm_queue *base, bool head_removed)
73 {
74         struct alarm *alarm;
75         bool is_wakeup = base == &alarms[ANDROID_ALARM_RTC_WAKEUP] ||
76                         base == &alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP];
77
78         if (base->stopped) {
79                 pr_alarm(FLOW, "changed alarm while setting the wall time\n");
80                 return;
81         }
82
83         if (is_wakeup && !suspended && head_removed)
84                 wake_unlock(&alarm_rtc_wake_lock);
85
86         if (!base->first)
87                 return;
88
89         alarm = container_of(base->first, struct alarm, node);
90
91         pr_alarm(FLOW, "selected alarm, type %d, func %pF at %lld\n",
92                 alarm->type, alarm->function, ktime_to_ns(alarm->expires));
93
94         if (is_wakeup && suspended) {
95                 pr_alarm(FLOW, "changed alarm while suspened\n");
96                 wake_lock_timeout(&alarm_rtc_wake_lock, 1 * HZ);
97                 return;
98         }
99
100         hrtimer_try_to_cancel(&base->timer);
101         base->timer.node.expires = ktime_add(base->delta, alarm->expires);
102         base->timer._softexpires = ktime_add(base->delta, alarm->softexpires);
103         hrtimer_start_expires(&base->timer, HRTIMER_MODE_ABS);
104 }
105
106 static void alarm_enqueue_locked(struct alarm *alarm)
107 {
108         struct alarm_queue *base = &alarms[alarm->type];
109         struct rb_node **link = &base->alarms.rb_node;
110         struct rb_node *parent = NULL;
111         struct alarm *entry;
112         int leftmost = 1;
113         bool was_first = false;
114
115         pr_alarm(FLOW, "added alarm, type %d, func %pF at %lld\n",
116                 alarm->type, alarm->function, ktime_to_ns(alarm->expires));
117
118         if (base->first == &alarm->node) {
119                 base->first = rb_next(&alarm->node);
120                 was_first = true;
121         }
122         if (!RB_EMPTY_NODE(&alarm->node)) {
123                 rb_erase(&alarm->node, &base->alarms);
124                 RB_CLEAR_NODE(&alarm->node);
125         }
126
127         while (*link) {
128                 parent = *link;
129                 entry = rb_entry(parent, struct alarm, node);
130                 /*
131                 * We dont care about collisions. Nodes with
132                 * the same expiry time stay together.
133                 */
134                 if (alarm->expires.tv64 < entry->expires.tv64) {
135                         link = &(*link)->rb_left;
136                 } else {
137                         link = &(*link)->rb_right;
138                         leftmost = 0;
139                 }
140         }
141         if (leftmost)
142                 base->first = &alarm->node;
143         if (leftmost || was_first)
144                 update_timer_locked(base, was_first);
145
146         rb_link_node(&alarm->node, parent, link);
147         rb_insert_color(&alarm->node, &base->alarms);
148 }
149
150 /**
151  * alarm_init - initialize an alarm
152  * @alarm:      the alarm to be initialized
153  * @type:       the alarm type to be used
154  * @function:   alarm callback function
155  */
156 void alarm_init(struct alarm *alarm,
157         enum android_alarm_type type, void (*function)(struct alarm *))
158 {
159         RB_CLEAR_NODE(&alarm->node);
160         alarm->type = type;
161         alarm->function = function;
162
163         pr_alarm(FLOW, "created alarm, type %d, func %pF\n", type, function);
164 }
165
166
167 /**
168  * alarm_start_range - (re)start an alarm
169  * @alarm:      the alarm to be added
170  * @start:      earliest expiry time
171  * @end:        expiry time
172  */
173 void alarm_start_range(struct alarm *alarm, ktime_t start, ktime_t end)
174 {
175         unsigned long flags;
176
177         spin_lock_irqsave(&alarm_slock, flags);
178         alarm->softexpires = start;
179         alarm->expires = end;
180         alarm_enqueue_locked(alarm);
181         spin_unlock_irqrestore(&alarm_slock, flags);
182 }
183
184 /**
185  * alarm_try_to_cancel - try to deactivate an alarm
186  * @alarm:      alarm to stop
187  *
188  * Returns:
189  *  0 when the alarm was not active
190  *  1 when the alarm was active
191  * -1 when the alarm may currently be excuting the callback function and
192  *    cannot be stopped (it may also be inactive)
193  */
194 int alarm_try_to_cancel(struct alarm *alarm)
195 {
196         struct alarm_queue *base = &alarms[alarm->type];
197         unsigned long flags;
198         bool first = false;
199         int ret = 0;
200
201         spin_lock_irqsave(&alarm_slock, flags);
202         if (!RB_EMPTY_NODE(&alarm->node)) {
203                 pr_alarm(FLOW, "canceled alarm, type %d, func %pF at %lld\n",
204                         alarm->type, alarm->function,
205                         ktime_to_ns(alarm->expires));
206                 ret = 1;
207                 if (base->first == &alarm->node) {
208                         base->first = rb_next(&alarm->node);
209                         first = true;
210                 }
211                 rb_erase(&alarm->node, &base->alarms);
212                 RB_CLEAR_NODE(&alarm->node);
213                 if (first)
214                         update_timer_locked(base, true);
215         } else
216                 pr_alarm(FLOW, "tried to cancel alarm, type %d, func %pF\n",
217                         alarm->type, alarm->function);
218         spin_unlock_irqrestore(&alarm_slock, flags);
219         if (!ret && hrtimer_callback_running(&base->timer))
220                 ret = -1;
221         return ret;
222 }
223
224 /**
225  * alarm_cancel - cancel an alarm and wait for the handler to finish.
226  * @alarm:      the alarm to be cancelled
227  *
228  * Returns:
229  *  0 when the alarm was not active
230  *  1 when the alarm was active
231  */
232 int alarm_cancel(struct alarm *alarm)
233 {
234         for (;;) {
235                 int ret = alarm_try_to_cancel(alarm);
236                 if (ret >= 0)
237                         return ret;
238                 cpu_relax();
239         }
240 }
241
242 /**
243  * alarm_set_rtc - set the kernel and rtc walltime
244  * @new_time:   timespec value containing the new time
245  */
246 int alarm_set_rtc(struct timespec new_time)
247 {
248         int i;
249         int ret;
250         unsigned long flags;
251         struct rtc_time rtc_new_rtc_time;
252         struct timespec tmp_time;
253
254         rtc_time_to_tm(new_time.tv_sec, &rtc_new_rtc_time);
255
256         pr_alarm(TSET, "set rtc %ld %ld - rtc %02d:%02d:%02d %02d/%02d/%04d\n",
257                 new_time.tv_sec, new_time.tv_nsec,
258                 rtc_new_rtc_time.tm_hour, rtc_new_rtc_time.tm_min,
259                 rtc_new_rtc_time.tm_sec, rtc_new_rtc_time.tm_mon + 1,
260                 rtc_new_rtc_time.tm_mday,
261                 rtc_new_rtc_time.tm_year + 1900);
262
263         mutex_lock(&alarm_setrtc_mutex);
264         spin_lock_irqsave(&alarm_slock, flags);
265         wake_lock(&alarm_rtc_wake_lock);
266         getnstimeofday(&tmp_time);
267         for (i = 0; i < ANDROID_ALARM_SYSTEMTIME; i++) {
268                 hrtimer_try_to_cancel(&alarms[i].timer);
269                 alarms[i].stopped = true;
270                 alarms[i].stopped_time = timespec_to_ktime(tmp_time);
271         }
272         alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP].delta =
273                 alarms[ANDROID_ALARM_ELAPSED_REALTIME].delta =
274                 ktime_sub(alarms[ANDROID_ALARM_ELAPSED_REALTIME].delta,
275                         timespec_to_ktime(timespec_sub(tmp_time, new_time)));
276         spin_unlock_irqrestore(&alarm_slock, flags);
277         ret = do_settimeofday(&new_time);
278         spin_lock_irqsave(&alarm_slock, flags);
279         for (i = 0; i < ANDROID_ALARM_SYSTEMTIME; i++) {
280                 alarms[i].stopped = false;
281                 update_timer_locked(&alarms[i], false);
282         }
283         spin_unlock_irqrestore(&alarm_slock, flags);
284         if (ret < 0) {
285                 pr_alarm(ERROR, "alarm_set_rtc: Failed to set time\n");
286                 goto err;
287         }
288         if (!alarm_rtc_dev) {
289                 pr_alarm(ERROR,
290                         "alarm_set_rtc: no RTC, time will be lost on reboot\n");
291                 goto err;
292         }
293         ret = rtc_set_time(alarm_rtc_dev, &rtc_new_rtc_time);
294         if (ret < 0)
295                 pr_alarm(ERROR, "alarm_set_rtc: "
296                         "Failed to set RTC, time will be lost on reboot\n");
297 err:
298         wake_unlock(&alarm_rtc_wake_lock);
299         mutex_unlock(&alarm_setrtc_mutex);
300         return ret;
301 }
302
303 /**
304  * alarm_get_elapsed_realtime - get the elapsed real time in ktime_t format
305  *
306  * returns the time in ktime_t format
307  */
308 ktime_t alarm_get_elapsed_realtime(void)
309 {
310         ktime_t now;
311         unsigned long flags;
312         struct alarm_queue *base = &alarms[ANDROID_ALARM_ELAPSED_REALTIME];
313
314         spin_lock_irqsave(&alarm_slock, flags);
315         now = base->stopped ? base->stopped_time : ktime_get_real();
316         now = ktime_sub(now, base->delta);
317         spin_unlock_irqrestore(&alarm_slock, flags);
318         return now;
319 }
320
321 static enum hrtimer_restart alarm_timer_triggered(struct hrtimer *timer)
322 {
323         struct alarm_queue *base;
324         struct alarm *alarm;
325         unsigned long flags;
326         ktime_t now;
327
328         spin_lock_irqsave(&alarm_slock, flags);
329
330         base = container_of(timer, struct alarm_queue, timer);
331         now = base->stopped ? base->stopped_time : hrtimer_cb_get_time(timer);
332         now = ktime_sub(now, base->delta);
333
334         pr_alarm(INT, "alarm_timer_triggered type %d at %lld\n",
335                 base - alarms, ktime_to_ns(now));
336
337         while (base->first) {
338                 alarm = container_of(base->first, struct alarm, node);
339                 if (alarm->softexpires.tv64 > now.tv64) {
340                         pr_alarm(FLOW, "don't call alarm, %pF, %lld (s %lld)\n",
341                                 alarm->function, ktime_to_ns(alarm->expires),
342                                 ktime_to_ns(alarm->softexpires));
343                         break;
344                 }
345                 base->first = rb_next(&alarm->node);
346                 rb_erase(&alarm->node, &base->alarms);
347                 RB_CLEAR_NODE(&alarm->node);
348                 pr_alarm(CALL, "call alarm, type %d, func %pF, %lld (s %lld)\n",
349                         alarm->type, alarm->function,
350                         ktime_to_ns(alarm->expires),
351                         ktime_to_ns(alarm->softexpires));
352                 spin_unlock_irqrestore(&alarm_slock, flags);
353                 alarm->function(alarm);
354                 spin_lock_irqsave(&alarm_slock, flags);
355         }
356         if (!base->first)
357                 pr_alarm(FLOW, "no more alarms of type %d\n", base - alarms);
358         update_timer_locked(base, true);
359         spin_unlock_irqrestore(&alarm_slock, flags);
360         return HRTIMER_NORESTART;
361 }
362
363 static void alarm_triggered_func(void *p)
364 {
365         struct rtc_device *rtc = alarm_rtc_dev;
366         if (!(rtc->irq_data & RTC_AF))
367                 return;
368         pr_alarm(INT, "rtc alarm triggered\n");
369         wake_lock_timeout(&alarm_rtc_wake_lock, 1 * HZ);
370 }
371
372 static int alarm_suspend(struct platform_device *pdev, pm_message_t state)
373 {
374         int                 err = 0;
375         unsigned long       flags;
376         struct rtc_wkalrm   rtc_alarm;
377         struct rtc_time     rtc_current_rtc_time;
378         unsigned long       rtc_current_time;
379         unsigned long       rtc_alarm_time;
380         struct timespec     rtc_delta;
381         struct timespec     wall_time;
382         struct alarm_queue *wakeup_queue = NULL;
383         struct alarm_queue *tmp_queue = NULL;
384
385         pr_alarm(SUSPEND, "alarm_suspend(%p, %d)\n", pdev, state.event);
386
387         spin_lock_irqsave(&alarm_slock, flags);
388         suspended = true;
389         spin_unlock_irqrestore(&alarm_slock, flags);
390
391         hrtimer_cancel(&alarms[ANDROID_ALARM_RTC_WAKEUP].timer);
392         hrtimer_cancel(&alarms[
393                         ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP].timer);
394
395         tmp_queue = &alarms[ANDROID_ALARM_RTC_WAKEUP];
396         if (tmp_queue->first)
397                 wakeup_queue = tmp_queue;
398         tmp_queue = &alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP];
399         if (tmp_queue->first && (!wakeup_queue ||
400                                 hrtimer_get_expires(&tmp_queue->timer).tv64 <
401                                 hrtimer_get_expires(&wakeup_queue->timer).tv64))
402                 wakeup_queue = tmp_queue;
403         if (wakeup_queue) {
404                 rtc_read_time(alarm_rtc_dev, &rtc_current_rtc_time);
405                 getnstimeofday(&wall_time);
406                 rtc_tm_to_time(&rtc_current_rtc_time, &rtc_current_time);
407                 set_normalized_timespec(&rtc_delta,
408                                         wall_time.tv_sec - rtc_current_time,
409                                         wall_time.tv_nsec);
410
411                 rtc_alarm_time = timespec_sub(ktime_to_timespec(
412                         hrtimer_get_expires(&wakeup_queue->timer)),
413                         rtc_delta).tv_sec;
414
415                 rtc_time_to_tm(rtc_alarm_time, &rtc_alarm.time);
416                 rtc_alarm.enabled = 1;
417                 rtc_set_alarm(alarm_rtc_dev, &rtc_alarm);
418                 rtc_read_time(alarm_rtc_dev, &rtc_current_rtc_time);
419                 rtc_tm_to_time(&rtc_current_rtc_time, &rtc_current_time);
420                 pr_alarm(SUSPEND,
421                         "rtc alarm set at %ld, now %ld, rtc delta %ld.%09ld\n",
422                         rtc_alarm_time, rtc_current_time,
423                         rtc_delta.tv_sec, rtc_delta.tv_nsec);
424                 if (rtc_current_time + 1 >= rtc_alarm_time) {
425                         pr_alarm(SUSPEND, "alarm about to go off\n");
426                         memset(&rtc_alarm, 0, sizeof(rtc_alarm));
427                         rtc_alarm.enabled = 0;
428                         rtc_set_alarm(alarm_rtc_dev, &rtc_alarm);
429
430                         spin_lock_irqsave(&alarm_slock, flags);
431                         suspended = false;
432                         wake_lock_timeout(&alarm_rtc_wake_lock, 2 * HZ);
433                         update_timer_locked(&alarms[ANDROID_ALARM_RTC_WAKEUP],
434                                                                         false);
435                         update_timer_locked(&alarms[
436                                 ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP], false);
437                         err = -EBUSY;
438                         spin_unlock_irqrestore(&alarm_slock, flags);
439                 }
440         }
441         return err;
442 }
443
444 static int alarm_resume(struct platform_device *pdev)
445 {
446         struct rtc_wkalrm alarm;
447         unsigned long       flags;
448
449         pr_alarm(SUSPEND, "alarm_resume(%p)\n", pdev);
450
451         memset(&alarm, 0, sizeof(alarm));
452         alarm.enabled = 0;
453         rtc_set_alarm(alarm_rtc_dev, &alarm);
454
455         spin_lock_irqsave(&alarm_slock, flags);
456         suspended = false;
457         update_timer_locked(&alarms[ANDROID_ALARM_RTC_WAKEUP], false);
458         update_timer_locked(&alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP],
459                                                                         false);
460         spin_unlock_irqrestore(&alarm_slock, flags);
461
462         return 0;
463 }
464
465 static struct rtc_task alarm_rtc_task = {
466         .func = alarm_triggered_func
467 };
468
469 static int rtc_alarm_add_device(struct device *dev,
470                                 struct class_interface *class_intf)
471 {
472         int err;
473         struct rtc_device *rtc = to_rtc_device(dev);
474
475         mutex_lock(&alarm_setrtc_mutex);
476
477         if (alarm_rtc_dev) {
478                 err = -EBUSY;
479                 goto err1;
480         }
481
482         alarm_platform_dev =
483                 platform_device_register_simple("alarm", -1, NULL, 0);
484         if (IS_ERR(alarm_platform_dev)) {
485                 err = PTR_ERR(alarm_platform_dev);
486                 goto err2;
487         }
488         err = rtc_irq_register(rtc, &alarm_rtc_task);
489         if (err)
490                 goto err3;
491         alarm_rtc_dev = rtc;
492         pr_alarm(INIT_STATUS, "using rtc device, %s, for alarms", rtc->name);
493         mutex_unlock(&alarm_setrtc_mutex);
494
495         return 0;
496
497 err3:
498         platform_device_unregister(alarm_platform_dev);
499 err2:
500 err1:
501         mutex_unlock(&alarm_setrtc_mutex);
502         return err;
503 }
504
505 static void rtc_alarm_remove_device(struct device *dev,
506                                     struct class_interface *class_intf)
507 {
508         if (dev == &alarm_rtc_dev->dev) {
509                 pr_alarm(INIT_STATUS, "lost rtc device for alarms");
510                 rtc_irq_unregister(alarm_rtc_dev, &alarm_rtc_task);
511                 platform_device_unregister(alarm_platform_dev);
512                 alarm_rtc_dev = NULL;
513         }
514 }
515
516 static struct class_interface rtc_alarm_interface = {
517         .add_dev = &rtc_alarm_add_device,
518         .remove_dev = &rtc_alarm_remove_device,
519 };
520
521 static struct platform_driver alarm_driver = {
522         .suspend = alarm_suspend,
523         .resume = alarm_resume,
524         .driver = {
525                 .name = "alarm"
526         }
527 };
528
529 static int __init alarm_late_init(void)
530 {
531         unsigned long   flags;
532         struct timespec tmp_time, system_time;
533
534         /* this needs to run after the rtc is read at boot */
535         spin_lock_irqsave(&alarm_slock, flags);
536         /* We read the current rtc and system time so we can later calulate
537          * elasped realtime to be (boot_systemtime + rtc - boot_rtc) ==
538          * (rtc - (boot_rtc - boot_systemtime))
539          */
540         getnstimeofday(&tmp_time);
541         ktime_get_ts(&system_time);
542         alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP].delta =
543                 alarms[ANDROID_ALARM_ELAPSED_REALTIME].delta =
544                         timespec_to_ktime(timespec_sub(tmp_time, system_time));
545
546         spin_unlock_irqrestore(&alarm_slock, flags);
547         return 0;
548 }
549
550 static int __init alarm_driver_init(void)
551 {
552         int err;
553         int i;
554
555         for (i = 0; i < ANDROID_ALARM_SYSTEMTIME; i++) {
556                 hrtimer_init(&alarms[i].timer,
557                                 CLOCK_REALTIME, HRTIMER_MODE_ABS);
558                 alarms[i].timer.function = alarm_timer_triggered;
559         }
560         hrtimer_init(&alarms[ANDROID_ALARM_SYSTEMTIME].timer,
561                      CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
562         alarms[ANDROID_ALARM_SYSTEMTIME].timer.function = alarm_timer_triggered;
563         err = platform_driver_register(&alarm_driver);
564         if (err < 0)
565                 goto err1;
566         wake_lock_init(&alarm_rtc_wake_lock, WAKE_LOCK_SUSPEND, "alarm_rtc");
567         rtc_alarm_interface.class = rtc_class;
568         err = class_interface_register(&rtc_alarm_interface);
569         if (err < 0)
570                 goto err2;
571
572         return 0;
573
574 err2:
575         wake_lock_destroy(&alarm_rtc_wake_lock);
576         platform_driver_unregister(&alarm_driver);
577 err1:
578         return err;
579 }
580
581 static void  __exit alarm_exit(void)
582 {
583         class_interface_unregister(&rtc_alarm_interface);
584         wake_lock_destroy(&alarm_rtc_wake_lock);
585         platform_driver_unregister(&alarm_driver);
586 }
587
588 late_initcall(alarm_late_init);
589 module_init(alarm_driver_init);
590 module_exit(alarm_exit);
591