Linux 4.13.16
[linux/fpc-iii.git] / kernel / time / alarmtimer.c
blob0b8ff7d257ea290743f2d0ab0194488784bc56ef
1 /*
2 * Alarmtimer interface
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
8 * interface.
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/sched/signal.h>
23 #include <linux/sched/debug.h>
24 #include <linux/alarmtimer.h>
25 #include <linux/mutex.h>
26 #include <linux/platform_device.h>
27 #include <linux/posix-timers.h>
28 #include <linux/workqueue.h>
29 #include <linux/freezer.h>
30 #include <linux/compat.h>
32 #include "posix-timers.h"
34 #define CREATE_TRACE_POINTS
35 #include <trace/events/alarmtimer.h>
37 /**
38 * struct alarm_base - Alarm timer bases
39 * @lock: Lock for syncrhonized access to the base
40 * @timerqueue: Timerqueue head managing the list of events
41 * @gettime: Function to read the time correlating to the base
42 * @base_clockid: clockid for the base
44 static struct alarm_base {
45 spinlock_t lock;
46 struct timerqueue_head timerqueue;
47 ktime_t (*gettime)(void);
48 clockid_t base_clockid;
49 } alarm_bases[ALARM_NUMTYPE];
51 #if defined(CONFIG_POSIX_TIMERS) || defined(CONFIG_RTC_CLASS)
52 /* freezer information to handle clock_nanosleep triggered wakeups */
53 static enum alarmtimer_type freezer_alarmtype;
54 static ktime_t freezer_expires;
55 static ktime_t freezer_delta;
56 static DEFINE_SPINLOCK(freezer_delta_lock);
57 #endif
59 static struct wakeup_source *ws;
61 #ifdef CONFIG_RTC_CLASS
62 /* rtc timer and device for setting alarm wakeups at suspend */
63 static struct rtc_timer rtctimer;
64 static struct rtc_device *rtcdev;
65 static DEFINE_SPINLOCK(rtcdev_lock);
67 /**
68 * alarmtimer_get_rtcdev - Return selected rtcdevice
70 * This function returns the rtc device to use for wakealarms.
71 * If one has not already been chosen, it checks to see if a
72 * functional rtc device is available.
74 struct rtc_device *alarmtimer_get_rtcdev(void)
76 unsigned long flags;
77 struct rtc_device *ret;
79 spin_lock_irqsave(&rtcdev_lock, flags);
80 ret = rtcdev;
81 spin_unlock_irqrestore(&rtcdev_lock, flags);
83 return ret;
85 EXPORT_SYMBOL_GPL(alarmtimer_get_rtcdev);
87 static int alarmtimer_rtc_add_device(struct device *dev,
88 struct class_interface *class_intf)
90 unsigned long flags;
91 struct rtc_device *rtc = to_rtc_device(dev);
93 if (rtcdev)
94 return -EBUSY;
96 if (!rtc->ops->set_alarm)
97 return -1;
98 if (!device_may_wakeup(rtc->dev.parent))
99 return -1;
101 spin_lock_irqsave(&rtcdev_lock, flags);
102 if (!rtcdev) {
103 rtcdev = rtc;
104 /* hold a reference so it doesn't go away */
105 get_device(dev);
107 spin_unlock_irqrestore(&rtcdev_lock, flags);
108 return 0;
111 static inline void alarmtimer_rtc_timer_init(void)
113 rtc_timer_init(&rtctimer, NULL, NULL);
116 static struct class_interface alarmtimer_rtc_interface = {
117 .add_dev = &alarmtimer_rtc_add_device,
120 static int alarmtimer_rtc_interface_setup(void)
122 alarmtimer_rtc_interface.class = rtc_class;
123 return class_interface_register(&alarmtimer_rtc_interface);
125 static void alarmtimer_rtc_interface_remove(void)
127 class_interface_unregister(&alarmtimer_rtc_interface);
129 #else
130 struct rtc_device *alarmtimer_get_rtcdev(void)
132 return NULL;
134 #define rtcdev (NULL)
135 static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
136 static inline void alarmtimer_rtc_interface_remove(void) { }
137 static inline void alarmtimer_rtc_timer_init(void) { }
138 #endif
141 * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
142 * @base: pointer to the base where the timer is being run
143 * @alarm: pointer to alarm being enqueued.
145 * Adds alarm to a alarm_base timerqueue
147 * Must hold base->lock when calling.
149 static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
151 if (alarm->state & ALARMTIMER_STATE_ENQUEUED)
152 timerqueue_del(&base->timerqueue, &alarm->node);
154 timerqueue_add(&base->timerqueue, &alarm->node);
155 alarm->state |= ALARMTIMER_STATE_ENQUEUED;
159 * alarmtimer_dequeue - Removes an alarm timer from an alarm_base timerqueue
160 * @base: pointer to the base where the timer is running
161 * @alarm: pointer to alarm being removed
163 * Removes alarm to a alarm_base timerqueue
165 * Must hold base->lock when calling.
167 static void alarmtimer_dequeue(struct alarm_base *base, struct alarm *alarm)
169 if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED))
170 return;
172 timerqueue_del(&base->timerqueue, &alarm->node);
173 alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
178 * alarmtimer_fired - Handles alarm hrtimer being fired.
179 * @timer: pointer to hrtimer being run
181 * When a alarm timer fires, this runs through the timerqueue to
182 * see which alarms expired, and runs those. If there are more alarm
183 * timers queued for the future, we set the hrtimer to fire when
184 * when the next future alarm timer expires.
186 static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
188 struct alarm *alarm = container_of(timer, struct alarm, timer);
189 struct alarm_base *base = &alarm_bases[alarm->type];
190 unsigned long flags;
191 int ret = HRTIMER_NORESTART;
192 int restart = ALARMTIMER_NORESTART;
194 spin_lock_irqsave(&base->lock, flags);
195 alarmtimer_dequeue(base, alarm);
196 spin_unlock_irqrestore(&base->lock, flags);
198 if (alarm->function)
199 restart = alarm->function(alarm, base->gettime());
201 spin_lock_irqsave(&base->lock, flags);
202 if (restart != ALARMTIMER_NORESTART) {
203 hrtimer_set_expires(&alarm->timer, alarm->node.expires);
204 alarmtimer_enqueue(base, alarm);
205 ret = HRTIMER_RESTART;
207 spin_unlock_irqrestore(&base->lock, flags);
209 trace_alarmtimer_fired(alarm, base->gettime());
210 return ret;
214 ktime_t alarm_expires_remaining(const struct alarm *alarm)
216 struct alarm_base *base = &alarm_bases[alarm->type];
217 return ktime_sub(alarm->node.expires, base->gettime());
219 EXPORT_SYMBOL_GPL(alarm_expires_remaining);
221 #ifdef CONFIG_RTC_CLASS
223 * alarmtimer_suspend - Suspend time callback
224 * @dev: unused
225 * @state: unused
227 * When we are going into suspend, we look through the bases
228 * to see which is the soonest timer to expire. We then
229 * set an rtc timer to fire that far into the future, which
230 * will wake us from suspend.
232 static int alarmtimer_suspend(struct device *dev)
234 ktime_t min, now, expires;
235 int i, ret, type;
236 struct rtc_device *rtc;
237 unsigned long flags;
238 struct rtc_time tm;
240 spin_lock_irqsave(&freezer_delta_lock, flags);
241 min = freezer_delta;
242 expires = freezer_expires;
243 type = freezer_alarmtype;
244 freezer_delta = 0;
245 spin_unlock_irqrestore(&freezer_delta_lock, flags);
247 rtc = alarmtimer_get_rtcdev();
248 /* If we have no rtcdev, just return */
249 if (!rtc)
250 return 0;
252 /* Find the soonest timer to expire*/
253 for (i = 0; i < ALARM_NUMTYPE; i++) {
254 struct alarm_base *base = &alarm_bases[i];
255 struct timerqueue_node *next;
256 ktime_t delta;
258 spin_lock_irqsave(&base->lock, flags);
259 next = timerqueue_getnext(&base->timerqueue);
260 spin_unlock_irqrestore(&base->lock, flags);
261 if (!next)
262 continue;
263 delta = ktime_sub(next->expires, base->gettime());
264 if (!min || (delta < min)) {
265 expires = next->expires;
266 min = delta;
267 type = i;
270 if (min == 0)
271 return 0;
273 if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) {
274 __pm_wakeup_event(ws, 2 * MSEC_PER_SEC);
275 return -EBUSY;
278 trace_alarmtimer_suspend(expires, type);
280 /* Setup an rtc timer to fire that far in the future */
281 rtc_timer_cancel(rtc, &rtctimer);
282 rtc_read_time(rtc, &tm);
283 now = rtc_tm_to_ktime(tm);
284 now = ktime_add(now, min);
286 /* Set alarm, if in the past reject suspend briefly to handle */
287 ret = rtc_timer_start(rtc, &rtctimer, now, 0);
288 if (ret < 0)
289 __pm_wakeup_event(ws, MSEC_PER_SEC);
290 return ret;
293 static int alarmtimer_resume(struct device *dev)
295 struct rtc_device *rtc;
297 rtc = alarmtimer_get_rtcdev();
298 if (rtc)
299 rtc_timer_cancel(rtc, &rtctimer);
300 return 0;
303 #else
304 static int alarmtimer_suspend(struct device *dev)
306 return 0;
309 static int alarmtimer_resume(struct device *dev)
311 return 0;
313 #endif
316 * alarm_init - Initialize an alarm structure
317 * @alarm: ptr to alarm to be initialized
318 * @type: the type of the alarm
319 * @function: callback that is run when the alarm fires
321 void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
322 enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
324 timerqueue_init(&alarm->node);
325 hrtimer_init(&alarm->timer, alarm_bases[type].base_clockid,
326 HRTIMER_MODE_ABS);
327 alarm->timer.function = alarmtimer_fired;
328 alarm->function = function;
329 alarm->type = type;
330 alarm->state = ALARMTIMER_STATE_INACTIVE;
332 EXPORT_SYMBOL_GPL(alarm_init);
335 * alarm_start - Sets an absolute alarm to fire
336 * @alarm: ptr to alarm to set
337 * @start: time to run the alarm
339 void alarm_start(struct alarm *alarm, ktime_t start)
341 struct alarm_base *base = &alarm_bases[alarm->type];
342 unsigned long flags;
344 spin_lock_irqsave(&base->lock, flags);
345 alarm->node.expires = start;
346 alarmtimer_enqueue(base, alarm);
347 hrtimer_start(&alarm->timer, alarm->node.expires, HRTIMER_MODE_ABS);
348 spin_unlock_irqrestore(&base->lock, flags);
350 trace_alarmtimer_start(alarm, base->gettime());
352 EXPORT_SYMBOL_GPL(alarm_start);
355 * alarm_start_relative - Sets a relative alarm to fire
356 * @alarm: ptr to alarm to set
357 * @start: time relative to now to run the alarm
359 void alarm_start_relative(struct alarm *alarm, ktime_t start)
361 struct alarm_base *base = &alarm_bases[alarm->type];
363 start = ktime_add_safe(start, base->gettime());
364 alarm_start(alarm, start);
366 EXPORT_SYMBOL_GPL(alarm_start_relative);
368 void alarm_restart(struct alarm *alarm)
370 struct alarm_base *base = &alarm_bases[alarm->type];
371 unsigned long flags;
373 spin_lock_irqsave(&base->lock, flags);
374 hrtimer_set_expires(&alarm->timer, alarm->node.expires);
375 hrtimer_restart(&alarm->timer);
376 alarmtimer_enqueue(base, alarm);
377 spin_unlock_irqrestore(&base->lock, flags);
379 EXPORT_SYMBOL_GPL(alarm_restart);
382 * alarm_try_to_cancel - Tries to cancel an alarm timer
383 * @alarm: ptr to alarm to be canceled
385 * Returns 1 if the timer was canceled, 0 if it was not running,
386 * and -1 if the callback was running
388 int alarm_try_to_cancel(struct alarm *alarm)
390 struct alarm_base *base = &alarm_bases[alarm->type];
391 unsigned long flags;
392 int ret;
394 spin_lock_irqsave(&base->lock, flags);
395 ret = hrtimer_try_to_cancel(&alarm->timer);
396 if (ret >= 0)
397 alarmtimer_dequeue(base, alarm);
398 spin_unlock_irqrestore(&base->lock, flags);
400 trace_alarmtimer_cancel(alarm, base->gettime());
401 return ret;
403 EXPORT_SYMBOL_GPL(alarm_try_to_cancel);
407 * alarm_cancel - Spins trying to cancel an alarm timer until it is done
408 * @alarm: ptr to alarm to be canceled
410 * Returns 1 if the timer was canceled, 0 if it was not active.
412 int alarm_cancel(struct alarm *alarm)
414 for (;;) {
415 int ret = alarm_try_to_cancel(alarm);
416 if (ret >= 0)
417 return ret;
418 cpu_relax();
421 EXPORT_SYMBOL_GPL(alarm_cancel);
424 u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval)
426 u64 overrun = 1;
427 ktime_t delta;
429 delta = ktime_sub(now, alarm->node.expires);
431 if (delta < 0)
432 return 0;
434 if (unlikely(delta >= interval)) {
435 s64 incr = ktime_to_ns(interval);
437 overrun = ktime_divns(delta, incr);
439 alarm->node.expires = ktime_add_ns(alarm->node.expires,
440 incr*overrun);
442 if (alarm->node.expires > now)
443 return overrun;
445 * This (and the ktime_add() below) is the
446 * correction for exact:
448 overrun++;
451 alarm->node.expires = ktime_add_safe(alarm->node.expires, interval);
452 return overrun;
454 EXPORT_SYMBOL_GPL(alarm_forward);
456 u64 alarm_forward_now(struct alarm *alarm, ktime_t interval)
458 struct alarm_base *base = &alarm_bases[alarm->type];
460 return alarm_forward(alarm, base->gettime(), interval);
462 EXPORT_SYMBOL_GPL(alarm_forward_now);
464 #ifdef CONFIG_POSIX_TIMERS
466 static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
468 struct alarm_base *base;
469 unsigned long flags;
470 ktime_t delta;
472 switch(type) {
473 case ALARM_REALTIME:
474 base = &alarm_bases[ALARM_REALTIME];
475 type = ALARM_REALTIME_FREEZER;
476 break;
477 case ALARM_BOOTTIME:
478 base = &alarm_bases[ALARM_BOOTTIME];
479 type = ALARM_BOOTTIME_FREEZER;
480 break;
481 default:
482 WARN_ONCE(1, "Invalid alarm type: %d\n", type);
483 return;
486 delta = ktime_sub(absexp, base->gettime());
488 spin_lock_irqsave(&freezer_delta_lock, flags);
489 if (!freezer_delta || (delta < freezer_delta)) {
490 freezer_delta = delta;
491 freezer_expires = absexp;
492 freezer_alarmtype = type;
494 spin_unlock_irqrestore(&freezer_delta_lock, flags);
498 * clock2alarm - helper that converts from clockid to alarmtypes
499 * @clockid: clockid.
501 static enum alarmtimer_type clock2alarm(clockid_t clockid)
503 if (clockid == CLOCK_REALTIME_ALARM)
504 return ALARM_REALTIME;
505 if (clockid == CLOCK_BOOTTIME_ALARM)
506 return ALARM_BOOTTIME;
507 return -1;
511 * alarm_handle_timer - Callback for posix timers
512 * @alarm: alarm that fired
514 * Posix timer callback for expired alarm timers.
516 static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
517 ktime_t now)
519 struct k_itimer *ptr = container_of(alarm, struct k_itimer,
520 it.alarm.alarmtimer);
521 enum alarmtimer_restart result = ALARMTIMER_NORESTART;
522 unsigned long flags;
523 int si_private = 0;
525 spin_lock_irqsave(&ptr->it_lock, flags);
527 ptr->it_active = 0;
528 if (ptr->it_interval)
529 si_private = ++ptr->it_requeue_pending;
531 if (posix_timer_event(ptr, si_private) && ptr->it_interval) {
533 * Handle ignored signals and rearm the timer. This will go
534 * away once we handle ignored signals proper.
536 ptr->it_overrun += alarm_forward_now(alarm, ptr->it_interval);
537 ++ptr->it_requeue_pending;
538 ptr->it_active = 1;
539 result = ALARMTIMER_RESTART;
541 spin_unlock_irqrestore(&ptr->it_lock, flags);
543 return result;
547 * alarm_timer_rearm - Posix timer callback for rearming timer
548 * @timr: Pointer to the posixtimer data struct
550 static void alarm_timer_rearm(struct k_itimer *timr)
552 struct alarm *alarm = &timr->it.alarm.alarmtimer;
554 timr->it_overrun += alarm_forward_now(alarm, timr->it_interval);
555 alarm_start(alarm, alarm->node.expires);
559 * alarm_timer_forward - Posix timer callback for forwarding timer
560 * @timr: Pointer to the posixtimer data struct
561 * @now: Current time to forward the timer against
563 static int alarm_timer_forward(struct k_itimer *timr, ktime_t now)
565 struct alarm *alarm = &timr->it.alarm.alarmtimer;
567 return (int) alarm_forward(alarm, timr->it_interval, now);
571 * alarm_timer_remaining - Posix timer callback to retrieve remaining time
572 * @timr: Pointer to the posixtimer data struct
573 * @now: Current time to calculate against
575 static ktime_t alarm_timer_remaining(struct k_itimer *timr, ktime_t now)
577 struct alarm *alarm = &timr->it.alarm.alarmtimer;
579 return ktime_sub(now, alarm->node.expires);
583 * alarm_timer_try_to_cancel - Posix timer callback to cancel a timer
584 * @timr: Pointer to the posixtimer data struct
586 static int alarm_timer_try_to_cancel(struct k_itimer *timr)
588 return alarm_try_to_cancel(&timr->it.alarm.alarmtimer);
592 * alarm_timer_arm - Posix timer callback to arm a timer
593 * @timr: Pointer to the posixtimer data struct
594 * @expires: The new expiry time
595 * @absolute: Expiry value is absolute time
596 * @sigev_none: Posix timer does not deliver signals
598 static void alarm_timer_arm(struct k_itimer *timr, ktime_t expires,
599 bool absolute, bool sigev_none)
601 struct alarm *alarm = &timr->it.alarm.alarmtimer;
602 struct alarm_base *base = &alarm_bases[alarm->type];
604 if (!absolute)
605 expires = ktime_add_safe(expires, base->gettime());
606 if (sigev_none)
607 alarm->node.expires = expires;
608 else
609 alarm_start(&timr->it.alarm.alarmtimer, expires);
613 * alarm_clock_getres - posix getres interface
614 * @which_clock: clockid
615 * @tp: timespec to fill
617 * Returns the granularity of underlying alarm base clock
619 static int alarm_clock_getres(const clockid_t which_clock, struct timespec64 *tp)
621 if (!alarmtimer_get_rtcdev())
622 return -EINVAL;
624 tp->tv_sec = 0;
625 tp->tv_nsec = hrtimer_resolution;
626 return 0;
630 * alarm_clock_get - posix clock_get interface
631 * @which_clock: clockid
632 * @tp: timespec to fill.
634 * Provides the underlying alarm base time.
636 static int alarm_clock_get(clockid_t which_clock, struct timespec64 *tp)
638 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
640 if (!alarmtimer_get_rtcdev())
641 return -EINVAL;
643 *tp = ktime_to_timespec64(base->gettime());
644 return 0;
648 * alarm_timer_create - posix timer_create interface
649 * @new_timer: k_itimer pointer to manage
651 * Initializes the k_itimer structure.
653 static int alarm_timer_create(struct k_itimer *new_timer)
655 enum alarmtimer_type type;
657 if (!alarmtimer_get_rtcdev())
658 return -ENOTSUPP;
660 if (!capable(CAP_WAKE_ALARM))
661 return -EPERM;
663 type = clock2alarm(new_timer->it_clock);
664 alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
665 return 0;
669 * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
670 * @alarm: ptr to alarm that fired
672 * Wakes up the task that set the alarmtimer
674 static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
675 ktime_t now)
677 struct task_struct *task = (struct task_struct *)alarm->data;
679 alarm->data = NULL;
680 if (task)
681 wake_up_process(task);
682 return ALARMTIMER_NORESTART;
686 * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
687 * @alarm: ptr to alarmtimer
688 * @absexp: absolute expiration time
690 * Sets the alarm timer and sleeps until it is fired or interrupted.
692 static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp,
693 enum alarmtimer_type type)
695 struct restart_block *restart;
696 alarm->data = (void *)current;
697 do {
698 set_current_state(TASK_INTERRUPTIBLE);
699 alarm_start(alarm, absexp);
700 if (likely(alarm->data))
701 schedule();
703 alarm_cancel(alarm);
704 } while (alarm->data && !signal_pending(current));
706 __set_current_state(TASK_RUNNING);
708 if (!alarm->data)
709 return 0;
711 if (freezing(current))
712 alarmtimer_freezerset(absexp, type);
713 restart = &current->restart_block;
714 if (restart->nanosleep.type != TT_NONE) {
715 struct timespec64 rmt;
716 ktime_t rem;
718 rem = ktime_sub(absexp, alarm_bases[type].gettime());
720 if (rem <= 0)
721 return 0;
722 rmt = ktime_to_timespec64(rem);
724 return nanosleep_copyout(restart, &rmt);
726 return -ERESTART_RESTARTBLOCK;
730 * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
731 * @restart: ptr to restart block
733 * Handles restarted clock_nanosleep calls
735 static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
737 enum alarmtimer_type type = restart->nanosleep.clockid;
738 ktime_t exp = restart->nanosleep.expires;
739 struct alarm alarm;
741 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
743 return alarmtimer_do_nsleep(&alarm, exp, type);
747 * alarm_timer_nsleep - alarmtimer nanosleep
748 * @which_clock: clockid
749 * @flags: determins abstime or relative
750 * @tsreq: requested sleep time (abs or rel)
751 * @rmtp: remaining sleep time saved
753 * Handles clock_nanosleep calls against _ALARM clockids
755 static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
756 const struct timespec64 *tsreq)
758 enum alarmtimer_type type = clock2alarm(which_clock);
759 struct restart_block *restart = &current->restart_block;
760 struct alarm alarm;
761 ktime_t exp;
762 int ret = 0;
764 if (!alarmtimer_get_rtcdev())
765 return -ENOTSUPP;
767 if (flags & ~TIMER_ABSTIME)
768 return -EINVAL;
770 if (!capable(CAP_WAKE_ALARM))
771 return -EPERM;
773 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
775 exp = timespec64_to_ktime(*tsreq);
776 /* Convert (if necessary) to absolute time */
777 if (flags != TIMER_ABSTIME) {
778 ktime_t now = alarm_bases[type].gettime();
779 exp = ktime_add(now, exp);
782 ret = alarmtimer_do_nsleep(&alarm, exp, type);
783 if (ret != -ERESTART_RESTARTBLOCK)
784 return ret;
786 /* abs timers don't set remaining time or restart */
787 if (flags == TIMER_ABSTIME)
788 return -ERESTARTNOHAND;
790 restart->fn = alarm_timer_nsleep_restart;
791 restart->nanosleep.clockid = type;
792 restart->nanosleep.expires = exp;
793 return ret;
796 const struct k_clock alarm_clock = {
797 .clock_getres = alarm_clock_getres,
798 .clock_get = alarm_clock_get,
799 .timer_create = alarm_timer_create,
800 .timer_set = common_timer_set,
801 .timer_del = common_timer_del,
802 .timer_get = common_timer_get,
803 .timer_arm = alarm_timer_arm,
804 .timer_rearm = alarm_timer_rearm,
805 .timer_forward = alarm_timer_forward,
806 .timer_remaining = alarm_timer_remaining,
807 .timer_try_to_cancel = alarm_timer_try_to_cancel,
808 .nsleep = alarm_timer_nsleep,
810 #endif /* CONFIG_POSIX_TIMERS */
813 /* Suspend hook structures */
814 static const struct dev_pm_ops alarmtimer_pm_ops = {
815 .suspend = alarmtimer_suspend,
816 .resume = alarmtimer_resume,
819 static struct platform_driver alarmtimer_driver = {
820 .driver = {
821 .name = "alarmtimer",
822 .pm = &alarmtimer_pm_ops,
827 * alarmtimer_init - Initialize alarm timer code
829 * This function initializes the alarm bases and registers
830 * the posix clock ids.
832 static int __init alarmtimer_init(void)
834 struct platform_device *pdev;
835 int error = 0;
836 int i;
838 alarmtimer_rtc_timer_init();
840 /* Initialize alarm bases */
841 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
842 alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
843 alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
844 alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
845 for (i = 0; i < ALARM_NUMTYPE; i++) {
846 timerqueue_init_head(&alarm_bases[i].timerqueue);
847 spin_lock_init(&alarm_bases[i].lock);
850 error = alarmtimer_rtc_interface_setup();
851 if (error)
852 return error;
854 error = platform_driver_register(&alarmtimer_driver);
855 if (error)
856 goto out_if;
858 pdev = platform_device_register_simple("alarmtimer", -1, NULL, 0);
859 if (IS_ERR(pdev)) {
860 error = PTR_ERR(pdev);
861 goto out_drv;
863 ws = wakeup_source_register("alarmtimer");
864 return 0;
866 out_drv:
867 platform_driver_unregister(&alarmtimer_driver);
868 out_if:
869 alarmtimer_rtc_interface_remove();
870 return error;
872 device_initcall(alarmtimer_init);