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 * struct alarm_base - Alarm timer bases
31 * @lock: Lock for syncrhonized access to the base
32 * @timerqueue: Timerqueue head managing the list of events
33 * @timer: hrtimer used to schedule events while running
34 * @gettime: Function to read the time correlating to the base
35 * @base_clockid: clockid for the base
37 static struct alarm_base {
39 struct timerqueue_head timerqueue;
41 ktime_t (*gettime)(void);
42 clockid_t base_clockid;
43 } alarm_bases[ALARM_NUMTYPE];
45 /* freezer delta & lock used to handle clock_nanosleep triggered wakeups */
46 static ktime_t freezer_delta;
47 static DEFINE_SPINLOCK(freezer_delta_lock);
49 #ifdef CONFIG_RTC_CLASS
50 /* rtc timer and device for setting alarm wakeups at suspend */
51 static struct rtc_timer rtctimer;
52 static struct rtc_device *rtcdev;
53 static DEFINE_SPINLOCK(rtcdev_lock);
56 * has_wakealarm - check rtc device has wakealarm ability
57 * @dev: current device
58 * @name_ptr: name to be returned
60 * This helper function checks to see if the rtc device can wake
63 static int has_wakealarm(struct device *dev, void *name_ptr)
65 struct rtc_device *candidate = to_rtc_device(dev);
67 if (!candidate->ops->set_alarm)
69 if (!device_may_wakeup(candidate->dev.parent))
72 *(const char **)name_ptr = dev_name(dev);
77 * alarmtimer_get_rtcdev - Return selected rtcdevice
79 * This function returns the rtc device to use for wakealarms.
80 * If one has not already been chosen, it checks to see if a
81 * functional rtc device is available.
83 static struct rtc_device *alarmtimer_get_rtcdev(void)
88 struct rtc_device *ret;
90 spin_lock_irqsave(&rtcdev_lock, flags);
92 /* Find an rtc device and init the rtc_timer */
93 dev = class_find_device(rtc_class, NULL, &str, has_wakealarm);
94 /* If we have a device then str is valid. See has_wakealarm() */
96 rtcdev = rtc_class_open(str);
98 * Drop the reference we got in class_find_device,
99 * rtc_open takes its own.
102 rtc_timer_init(&rtctimer, NULL, NULL);
106 spin_unlock_irqrestore(&rtcdev_lock, flags);
114 * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
115 * @base: pointer to the base where the timer is being run
116 * @alarm: pointer to alarm being enqueued.
118 * Adds alarm to a alarm_base timerqueue and if necessary sets
121 * Must hold base->lock when calling.
123 static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
125 timerqueue_add(&base->timerqueue, &alarm->node);
126 if (&alarm->node == timerqueue_getnext(&base->timerqueue)) {
127 hrtimer_try_to_cancel(&base->timer);
128 hrtimer_start(&base->timer, alarm->node.expires,
134 * alarmtimer_remove - Removes an alarm timer from an alarm_base timerqueue
135 * @base: pointer to the base where the timer is running
136 * @alarm: pointer to alarm being removed
138 * Removes alarm to a alarm_base timerqueue and if necessary sets
139 * a new timer to run.
141 * Must hold base->lock when calling.
143 static void alarmtimer_remove(struct alarm_base *base, struct alarm *alarm)
145 struct timerqueue_node *next = timerqueue_getnext(&base->timerqueue);
147 timerqueue_del(&base->timerqueue, &alarm->node);
148 if (next == &alarm->node) {
149 hrtimer_try_to_cancel(&base->timer);
150 next = timerqueue_getnext(&base->timerqueue);
153 hrtimer_start(&base->timer, next->expires, HRTIMER_MODE_ABS);
159 * alarmtimer_fired - Handles alarm hrtimer being fired.
160 * @timer: pointer to hrtimer being run
162 * When a alarm timer fires, this runs through the timerqueue to
163 * see which alarms expired, and runs those. If there are more alarm
164 * timers queued for the future, we set the hrtimer to fire when
165 * when the next future alarm timer expires.
167 static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
169 struct alarm_base *base = container_of(timer, struct alarm_base, timer);
170 struct timerqueue_node *next;
173 int ret = HRTIMER_NORESTART;
175 spin_lock_irqsave(&base->lock, flags);
176 now = base->gettime();
177 while ((next = timerqueue_getnext(&base->timerqueue))) {
179 ktime_t expired = next->expires;
181 if (expired.tv64 >= now.tv64)
184 alarm = container_of(next, struct alarm, node);
186 timerqueue_del(&base->timerqueue, &alarm->node);
188 /* Re-add periodic timers */
189 if (alarm->period.tv64) {
190 alarm->node.expires = ktime_add(expired, alarm->period);
191 timerqueue_add(&base->timerqueue, &alarm->node);
194 spin_unlock_irqrestore(&base->lock, flags);
196 alarm->function(alarm);
197 spin_lock_irqsave(&base->lock, flags);
201 hrtimer_set_expires(&base->timer, next->expires);
202 ret = HRTIMER_RESTART;
204 spin_unlock_irqrestore(&base->lock, flags);
210 #ifdef CONFIG_RTC_CLASS
212 * alarmtimer_suspend - Suspend time callback
216 * When we are going into suspend, we look through the bases
217 * to see which is the soonest timer to expire. We then
218 * set an rtc timer to fire that far into the future, which
219 * will wake us from suspend.
221 static int alarmtimer_suspend(struct device *dev)
226 struct rtc_device *rtc;
229 spin_lock_irqsave(&freezer_delta_lock, flags);
231 freezer_delta = ktime_set(0, 0);
232 spin_unlock_irqrestore(&freezer_delta_lock, flags);
234 rtc = alarmtimer_get_rtcdev();
235 /* If we have no rtcdev, just return */
239 /* Find the soonest timer to expire*/
240 for (i = 0; i < ALARM_NUMTYPE; i++) {
241 struct alarm_base *base = &alarm_bases[i];
242 struct timerqueue_node *next;
245 spin_lock_irqsave(&base->lock, flags);
246 next = timerqueue_getnext(&base->timerqueue);
247 spin_unlock_irqrestore(&base->lock, flags);
250 delta = ktime_sub(next->expires, base->gettime());
251 if (!min.tv64 || (delta.tv64 < min.tv64))
257 /* XXX - Should we enforce a minimum sleep time? */
258 WARN_ON(min.tv64 < NSEC_PER_SEC);
260 /* Setup an rtc timer to fire that far in the future */
261 rtc_timer_cancel(rtc, &rtctimer);
262 rtc_read_time(rtc, &tm);
263 now = rtc_tm_to_ktime(tm);
264 now = ktime_add(now, min);
266 rtc_timer_start(rtc, &rtctimer, now, ktime_set(0, 0));
271 static int alarmtimer_suspend(struct device *dev)
277 static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
281 struct alarm_base *base = &alarm_bases[type];
283 delta = ktime_sub(absexp, base->gettime());
285 spin_lock_irqsave(&freezer_delta_lock, flags);
286 if (!freezer_delta.tv64 || (delta.tv64 < freezer_delta.tv64))
287 freezer_delta = delta;
288 spin_unlock_irqrestore(&freezer_delta_lock, flags);
293 * alarm_init - Initialize an alarm structure
294 * @alarm: ptr to alarm to be initialized
295 * @type: the type of the alarm
296 * @function: callback that is run when the alarm fires
298 void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
299 void (*function)(struct alarm *))
301 timerqueue_init(&alarm->node);
302 alarm->period = ktime_set(0, 0);
303 alarm->function = function;
309 * alarm_start - Sets an alarm to fire
310 * @alarm: ptr to alarm to set
311 * @start: time to run the alarm
312 * @period: period at which the alarm will recur
314 void alarm_start(struct alarm *alarm, ktime_t start, ktime_t period)
316 struct alarm_base *base = &alarm_bases[alarm->type];
319 spin_lock_irqsave(&base->lock, flags);
321 alarmtimer_remove(base, alarm);
322 alarm->node.expires = start;
323 alarm->period = period;
324 alarmtimer_enqueue(base, alarm);
326 spin_unlock_irqrestore(&base->lock, flags);
330 * alarm_cancel - Tries to cancel an alarm timer
331 * @alarm: ptr to alarm to be canceled
333 void alarm_cancel(struct alarm *alarm)
335 struct alarm_base *base = &alarm_bases[alarm->type];
338 spin_lock_irqsave(&base->lock, flags);
340 alarmtimer_remove(base, alarm);
342 spin_unlock_irqrestore(&base->lock, flags);
347 * clock2alarm - helper that converts from clockid to alarmtypes
350 static enum alarmtimer_type clock2alarm(clockid_t clockid)
352 if (clockid == CLOCK_REALTIME_ALARM)
353 return ALARM_REALTIME;
354 if (clockid == CLOCK_BOOTTIME_ALARM)
355 return ALARM_BOOTTIME;
360 * alarm_handle_timer - Callback for posix timers
361 * @alarm: alarm that fired
363 * Posix timer callback for expired alarm timers.
365 static void alarm_handle_timer(struct alarm *alarm)
367 struct k_itimer *ptr = container_of(alarm, struct k_itimer,
369 if (posix_timer_event(ptr, 0) != 0)
374 * alarm_clock_getres - posix getres interface
375 * @which_clock: clockid
376 * @tp: timespec to fill
378 * Returns the granularity of underlying alarm base clock
380 static int alarm_clock_getres(const clockid_t which_clock, struct timespec *tp)
382 clockid_t baseid = alarm_bases[clock2alarm(which_clock)].base_clockid;
384 return hrtimer_get_res(baseid, tp);
388 * alarm_clock_get - posix clock_get interface
389 * @which_clock: clockid
390 * @tp: timespec to fill.
392 * Provides the underlying alarm base time.
394 static int alarm_clock_get(clockid_t which_clock, struct timespec *tp)
396 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
398 *tp = ktime_to_timespec(base->gettime());
403 * alarm_timer_create - posix timer_create interface
404 * @new_timer: k_itimer pointer to manage
406 * Initializes the k_itimer structure.
408 static int alarm_timer_create(struct k_itimer *new_timer)
410 enum alarmtimer_type type;
411 struct alarm_base *base;
413 if (!capable(CAP_WAKE_ALARM))
416 type = clock2alarm(new_timer->it_clock);
417 base = &alarm_bases[type];
418 alarm_init(&new_timer->it.alarmtimer, type, alarm_handle_timer);
423 * alarm_timer_get - posix timer_get interface
424 * @new_timer: k_itimer pointer
425 * @cur_setting: itimerspec data to fill
427 * Copies the itimerspec data out from the k_itimer
429 static void alarm_timer_get(struct k_itimer *timr,
430 struct itimerspec *cur_setting)
432 cur_setting->it_interval =
433 ktime_to_timespec(timr->it.alarmtimer.period);
434 cur_setting->it_value =
435 ktime_to_timespec(timr->it.alarmtimer.node.expires);
440 * alarm_timer_del - posix timer_del interface
441 * @timr: k_itimer pointer to be deleted
443 * Cancels any programmed alarms for the given timer.
445 static int alarm_timer_del(struct k_itimer *timr)
447 alarm_cancel(&timr->it.alarmtimer);
452 * alarm_timer_set - posix timer_set interface
453 * @timr: k_itimer pointer to be deleted
454 * @flags: timer flags
455 * @new_setting: itimerspec to be used
456 * @old_setting: itimerspec being replaced
458 * Sets the timer to new_setting, and starts the timer.
460 static int alarm_timer_set(struct k_itimer *timr, int flags,
461 struct itimerspec *new_setting,
462 struct itimerspec *old_setting)
464 /* Save old values */
465 old_setting->it_interval =
466 ktime_to_timespec(timr->it.alarmtimer.period);
467 old_setting->it_value =
468 ktime_to_timespec(timr->it.alarmtimer.node.expires);
470 /* If the timer was already set, cancel it */
471 alarm_cancel(&timr->it.alarmtimer);
473 /* start the timer */
474 alarm_start(&timr->it.alarmtimer,
475 timespec_to_ktime(new_setting->it_value),
476 timespec_to_ktime(new_setting->it_interval));
481 * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
482 * @alarm: ptr to alarm that fired
484 * Wakes up the task that set the alarmtimer
486 static void alarmtimer_nsleep_wakeup(struct alarm *alarm)
488 struct task_struct *task = (struct task_struct *)alarm->data;
492 wake_up_process(task);
496 * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
497 * @alarm: ptr to alarmtimer
498 * @absexp: absolute expiration time
500 * Sets the alarm timer and sleeps until it is fired or interrupted.
502 static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp)
504 alarm->data = (void *)current;
506 set_current_state(TASK_INTERRUPTIBLE);
507 alarm_start(alarm, absexp, ktime_set(0, 0));
508 if (likely(alarm->data))
512 } while (alarm->data && !signal_pending(current));
514 __set_current_state(TASK_RUNNING);
516 return (alarm->data == NULL);
521 * update_rmtp - Update remaining timespec value
522 * @exp: expiration time
524 * @rmtp: user pointer to remaining timepsec value
526 * Helper function that fills in rmtp value with time between
527 * now and the exp value
529 static int update_rmtp(ktime_t exp, enum alarmtimer_type type,
530 struct timespec __user *rmtp)
535 rem = ktime_sub(exp, alarm_bases[type].gettime());
539 rmt = ktime_to_timespec(rem);
541 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
549 * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
550 * @restart: ptr to restart block
552 * Handles restarted clock_nanosleep calls
554 static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
556 enum alarmtimer_type type = restart->nanosleep.clockid;
558 struct timespec __user *rmtp;
562 exp.tv64 = restart->nanosleep.expires;
563 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
565 if (alarmtimer_do_nsleep(&alarm, exp))
568 if (freezing(current))
569 alarmtimer_freezerset(exp, type);
571 rmtp = restart->nanosleep.rmtp;
573 ret = update_rmtp(exp, type, rmtp);
579 /* The other values in restart are already filled in */
580 ret = -ERESTART_RESTARTBLOCK;
586 * alarm_timer_nsleep - alarmtimer nanosleep
587 * @which_clock: clockid
588 * @flags: determins abstime or relative
589 * @tsreq: requested sleep time (abs or rel)
590 * @rmtp: remaining sleep time saved
592 * Handles clock_nanosleep calls against _ALARM clockids
594 static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
595 struct timespec *tsreq, struct timespec __user *rmtp)
597 enum alarmtimer_type type = clock2alarm(which_clock);
601 struct restart_block *restart;
603 if (!capable(CAP_WAKE_ALARM))
606 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
608 exp = timespec_to_ktime(*tsreq);
609 /* Convert (if necessary) to absolute time */
610 if (flags != TIMER_ABSTIME) {
611 ktime_t now = alarm_bases[type].gettime();
612 exp = ktime_add(now, exp);
615 if (alarmtimer_do_nsleep(&alarm, exp))
618 if (freezing(current))
619 alarmtimer_freezerset(exp, type);
621 /* abs timers don't set remaining time or restart */
622 if (flags == TIMER_ABSTIME) {
623 ret = -ERESTARTNOHAND;
628 ret = update_rmtp(exp, type, rmtp);
633 restart = ¤t_thread_info()->restart_block;
634 restart->fn = alarm_timer_nsleep_restart;
635 restart->nanosleep.clockid = type;
636 restart->nanosleep.expires = exp.tv64;
637 restart->nanosleep.rmtp = rmtp;
638 ret = -ERESTART_RESTARTBLOCK;
645 /* Suspend hook structures */
646 static const struct dev_pm_ops alarmtimer_pm_ops = {
647 .suspend = alarmtimer_suspend,
650 static struct platform_driver alarmtimer_driver = {
652 .name = "alarmtimer",
653 .pm = &alarmtimer_pm_ops,
658 * alarmtimer_init - Initialize alarm timer code
660 * This function initializes the alarm bases and registers
661 * the posix clock ids.
663 static int __init alarmtimer_init(void)
667 struct k_clock alarm_clock = {
668 .clock_getres = alarm_clock_getres,
669 .clock_get = alarm_clock_get,
670 .timer_create = alarm_timer_create,
671 .timer_set = alarm_timer_set,
672 .timer_del = alarm_timer_del,
673 .timer_get = alarm_timer_get,
674 .nsleep = alarm_timer_nsleep,
677 posix_timers_register_clock(CLOCK_REALTIME_ALARM, &alarm_clock);
678 posix_timers_register_clock(CLOCK_BOOTTIME_ALARM, &alarm_clock);
680 /* Initialize alarm bases */
681 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
682 alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
683 alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
684 alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
685 for (i = 0; i < ALARM_NUMTYPE; i++) {
686 timerqueue_init_head(&alarm_bases[i].timerqueue);
687 spin_lock_init(&alarm_bases[i].lock);
688 hrtimer_init(&alarm_bases[i].timer,
689 alarm_bases[i].base_clockid,
691 alarm_bases[i].timer.function = alarmtimer_fired;
693 error = platform_driver_register(&alarmtimer_driver);
694 platform_device_register_simple("alarmtimer", -1, NULL, 0);
698 device_initcall(alarmtimer_init);