4 * This interface provides a timer which is similarto hrtimers,
5 * but triggers a RTC alarm if the box is suspend.
7 * This interface is influenced by the Android RTC Alarm timer
10 * Copyright (C) 2010 IBM Corperation
12 * Author: John Stultz <john.stultz@linaro.org>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
18 #include <linux/time.h>
19 #include <linux/hrtimer.h>
20 #include <linux/timerqueue.h>
21 #include <linux/rtc.h>
22 #include <linux/alarmtimer.h>
23 #include <linux/mutex.h>
24 #include <linux/platform_device.h>
25 #include <linux/posix-timers.h>
26 #include <linux/workqueue.h>
27 #include <linux/freezer.h>
30 static struct alarm_base {
32 struct timerqueue_head timerqueue;
34 ktime_t (*gettime)(void);
35 clockid_t base_clockid;
36 struct work_struct irqwork;
37 } alarm_bases[ALARM_NUMTYPE];
39 static struct rtc_timer rtctimer;
40 static struct rtc_device *rtcdev;
42 static ktime_t freezer_delta;
43 static DEFINE_SPINLOCK(freezer_delta_lock);
46 /**************************************************************************
47 * alarmtimer management code
51 * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
52 * @base: pointer to the base where the timer is being run
53 * @alarm: pointer to alarm being enqueued.
55 * Adds alarm to a alarm_base timerqueue and if necessary sets
58 * Must hold base->lock when calling.
60 static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
62 timerqueue_add(&base->timerqueue, &alarm->node);
63 if (&alarm->node == timerqueue_getnext(&base->timerqueue)) {
64 hrtimer_try_to_cancel(&base->timer);
65 hrtimer_start(&base->timer, alarm->node.expires,
71 * alarmtimer_remove - Removes an alarm timer from an alarm_base timerqueue
72 * @base: pointer to the base where the timer is running
73 * @alarm: pointer to alarm being removed
75 * Removes alarm to a alarm_base timerqueue and if necessary sets
78 * Must hold base->lock when calling.
80 static void alarmtimer_remove(struct alarm_base *base, struct alarm *alarm)
82 struct timerqueue_node *next = timerqueue_getnext(&base->timerqueue);
84 timerqueue_del(&base->timerqueue, &alarm->node);
85 if (next == &alarm->node) {
86 hrtimer_try_to_cancel(&base->timer);
87 next = timerqueue_getnext(&base->timerqueue);
90 hrtimer_start(&base->timer, next->expires, HRTIMER_MODE_ABS);
95 * alarmtimer_do_work - Handles alarm being fired.
96 * @work: pointer to workqueue being run
98 * When a timer fires, this runs through the timerqueue to see
99 * which alarm timers, and run those that expired. If there are
100 * more alarm timers queued, we set the hrtimer to fire in the
103 void alarmtimer_do_work(struct work_struct *work)
105 struct alarm_base *base = container_of(work, struct alarm_base,
107 struct timerqueue_node *next;
111 spin_lock_irqsave(&base->lock, flags);
112 now = base->gettime();
113 while ((next = timerqueue_getnext(&base->timerqueue))) {
115 ktime_t expired = next->expires;
117 if (expired.tv64 >= now.tv64)
120 alarm = container_of(next, struct alarm, node);
122 timerqueue_del(&base->timerqueue, &alarm->node);
124 /* Re-add periodic timers */
125 if (alarm->period.tv64) {
126 alarm->node.expires = ktime_add(expired, alarm->period);
127 timerqueue_add(&base->timerqueue, &alarm->node);
130 spin_unlock_irqrestore(&base->lock, flags);
132 alarm->function(alarm);
133 spin_lock_irqsave(&base->lock, flags);
137 hrtimer_start(&base->timer, next->expires,
140 spin_unlock_irqrestore(&base->lock, flags);
145 * alarmtimer_fired - Handles alarm hrtimer being fired.
146 * @timer: pointer to hrtimer being run
148 * When a timer fires, this schedules the do_work function to
151 static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
153 struct alarm_base *base = container_of(timer, struct alarm_base, timer);
154 schedule_work(&base->irqwork);
155 return HRTIMER_NORESTART;
160 * alarmtimer_suspend - Suspend time callback
164 * When we are going into suspend, we look through the bases
165 * to see which is the soonest timer to expire. We then
166 * set an rtc timer to fire that far into the future, which
167 * will wake us from suspend.
169 static int alarmtimer_suspend(struct device *dev)
176 spin_lock_irqsave(&freezer_delta_lock, flags);
178 freezer_delta = ktime_set(0, 0);
179 spin_unlock_irqrestore(&freezer_delta_lock, flags);
181 /* If we have no rtcdev, just return */
185 /* Find the soonest timer to expire*/
186 for (i = 0; i < ALARM_NUMTYPE; i++) {
187 struct alarm_base *base = &alarm_bases[i];
188 struct timerqueue_node *next;
191 spin_lock_irqsave(&base->lock, flags);
192 next = timerqueue_getnext(&base->timerqueue);
193 spin_unlock_irqrestore(&base->lock, flags);
196 delta = ktime_sub(next->expires, base->gettime());
197 if (!min.tv64 || (delta.tv64 < min.tv64))
203 /* XXX - Should we enforce a minimum sleep time? */
204 WARN_ON(min.tv64 < NSEC_PER_SEC);
206 /* Setup an rtc timer to fire that far in the future */
207 rtc_timer_cancel(rtcdev, &rtctimer);
208 rtc_read_time(rtcdev, &tm);
209 now = rtc_tm_to_ktime(tm);
210 now = ktime_add(now, min);
212 rtc_timer_start(rtcdev, &rtctimer, now, ktime_set(0, 0));
218 /**************************************************************************
219 * alarm kernel interface code
223 * alarm_init - Initialize an alarm structure
224 * @alarm: ptr to alarm to be initialized
225 * @type: the type of the alarm
226 * @function: callback that is run when the alarm fires
228 * In-kernel interface to initializes the alarm structure.
230 void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
231 void (*function)(struct alarm *))
233 timerqueue_init(&alarm->node);
234 alarm->period = ktime_set(0, 0);
235 alarm->function = function;
241 * alarm_start - Sets an alarm to fire
242 * @alarm: ptr to alarm to set
243 * @start: time to run the alarm
244 * @period: period at which the alarm will recur
246 * In-kernel interface set an alarm timer.
248 void alarm_start(struct alarm *alarm, ktime_t start, ktime_t period)
250 struct alarm_base *base = &alarm_bases[alarm->type];
253 spin_lock_irqsave(&base->lock, flags);
255 alarmtimer_remove(base, alarm);
256 alarm->node.expires = start;
257 alarm->period = period;
258 alarmtimer_enqueue(base, alarm);
260 spin_unlock_irqrestore(&base->lock, flags);
264 * alarm_cancel - Tries to cancel an alarm timer
265 * @alarm: ptr to alarm to be canceled
267 * In-kernel interface to cancel an alarm timer.
269 void alarm_cancel(struct alarm *alarm)
271 struct alarm_base *base = &alarm_bases[alarm->type];
274 spin_lock_irqsave(&base->lock, flags);
276 alarmtimer_remove(base, alarm);
278 spin_unlock_irqrestore(&base->lock, flags);
283 /**************************************************************************
284 * alarmtimer initialization code
287 /* Suspend hook structures */
288 static const struct dev_pm_ops alarmtimer_pm_ops = {
289 .suspend = alarmtimer_suspend,
292 static struct platform_driver alarmtimer_driver = {
294 .name = "alarmtimer",
295 .pm = &alarmtimer_pm_ops,
300 * alarmtimer_init - Initialize alarm timer code
302 * This function initializes the alarm bases and registers
303 * the posix clock ids.
305 static int __init alarmtimer_init(void)
310 /* Initialize alarm bases */
311 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
312 alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
313 alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
314 alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
315 for (i = 0; i < ALARM_NUMTYPE; i++) {
316 timerqueue_init_head(&alarm_bases[i].timerqueue);
317 spin_lock_init(&alarm_bases[i].lock);
318 hrtimer_init(&alarm_bases[i].timer,
319 alarm_bases[i].base_clockid,
321 alarm_bases[i].timer.function = alarmtimer_fired;
322 INIT_WORK(&alarm_bases[i].irqwork, alarmtimer_do_work);
324 error = platform_driver_register(&alarmtimer_driver);
325 platform_device_register_simple("alarmtimer", -1, NULL, 0);
329 device_initcall(alarmtimer_init);
332 * has_wakealarm - check rtc device has wakealarm ability
333 * @dev: current device
334 * @name_ptr: name to be returned
336 * This helper function checks to see if the rtc device can wake
339 static int __init has_wakealarm(struct device *dev, void *name_ptr)
341 struct rtc_device *candidate = to_rtc_device(dev);
343 if (!candidate->ops->set_alarm)
345 if (!device_may_wakeup(candidate->dev.parent))
348 *(const char **)name_ptr = dev_name(dev);
353 * alarmtimer_init_late - Late initializing of alarmtimer code
355 * This function locates a rtc device to use for wakealarms.
356 * Run as late_initcall to make sure rtc devices have been
359 static int __init alarmtimer_init_late(void)
363 /* Find an rtc device and init the rtc_timer */
364 class_find_device(rtc_class, NULL, &str, has_wakealarm);
366 rtcdev = rtc_class_open(str);
368 printk(KERN_WARNING "No RTC device found, ALARM timers will"
369 " not wake from suspend");
371 rtc_timer_init(&rtctimer, NULL, NULL);
375 late_initcall(alarmtimer_init_late);