18 #include <linux/time.h>
19 #include <linux/hrtimer.h>
21 #include <linux/rtc.h>
37 static struct alarm_base {
50 #ifdef CONFIG_RTC_CLASS
70 spin_unlock_irqrestore(&rtcdev_lock, flags);
76 static int alarmtimer_rtc_add_device(
struct device *
dev,
85 if (!rtc->
ops->set_alarm)
87 if (!device_may_wakeup(rtc->
dev.parent))
96 spin_unlock_irqrestore(&rtcdev_lock, flags);
100 static inline void alarmtimer_rtc_timer_init(
void)
106 .
add_dev = &alarmtimer_rtc_add_device,
109 static int alarmtimer_rtc_interface_setup(
void)
114 static void alarmtimer_rtc_interface_remove(
void)
123 #define rtcdev (NULL)
124 static inline int alarmtimer_rtc_interface_setup(
void) {
return 0; }
125 static inline void alarmtimer_rtc_interface_remove(
void) { }
126 static inline void alarmtimer_rtc_timer_init(
void) { }
138 static void alarmtimer_enqueue(
struct alarm_base *base,
struct alarm *
alarm)
156 static void alarmtimer_dequeue(
struct alarm_base *base,
struct alarm *alarm)
177 struct alarm *alarm =
container_of(timer,
struct alarm, timer);
178 struct alarm_base *base = &alarm_bases[alarm->
type];
184 alarmtimer_dequeue(base, alarm);
185 spin_unlock_irqrestore(&base->lock, flags);
188 restart = alarm->
function(alarm, base->gettime());
192 hrtimer_set_expires(&alarm->
timer, alarm->
node.expires);
193 alarmtimer_enqueue(base, alarm);
196 spin_unlock_irqrestore(&base->lock, flags);
202 #ifdef CONFIG_RTC_CLASS
213 static int alarmtimer_suspend(
struct device *dev)
224 freezer_delta = ktime_set(0, 0);
225 spin_unlock_irqrestore(&freezer_delta_lock, flags);
234 struct alarm_base *base = &alarm_bases[
i];
239 next = timerqueue_getnext(&base->timerqueue);
240 spin_unlock_irqrestore(&base->lock, flags);
243 delta = ktime_sub(next->
expires, base->gettime());
244 if (!min.tv64 || (delta.
tv64 < min.tv64))
259 now = ktime_add(now, min);
268 static int alarmtimer_suspend(
struct device *dev)
278 struct alarm_base *base = &alarm_bases[
type];
280 delta = ktime_sub(absexp, base->gettime());
283 if (!freezer_delta.
tv64 || (delta.
tv64 < freezer_delta.
tv64))
284 freezer_delta =
delta;
285 spin_unlock_irqrestore(&freezer_delta_lock, flags);
298 timerqueue_init(&alarm->
node);
301 alarm->
timer.function = alarmtimer_fired;
314 struct alarm_base *base = &alarm_bases[alarm->
type];
320 alarmtimer_enqueue(base, alarm);
323 spin_unlock_irqrestore(&base->lock, flags);
336 struct alarm_base *base = &alarm_bases[alarm->
type];
343 alarmtimer_dequeue(base, alarm);
344 spin_unlock_irqrestore(&base->lock, flags);
371 delta = ktime_sub(now, alarm->
node.expires);
377 s64 incr = ktime_to_ns(interval);
384 if (alarm->
node.expires.tv64 > now.
tv64)
393 alarm->
node.expires = ktime_add(alarm->
node.expires, interval);
423 it.alarm.alarmtimer);
428 if (ptr->
it.
alarm.interval.tv64) {
445 clockid_t baseid = alarm_bases[clock2alarm(which_clock)].base_clockid;
462 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
467 *tp = ktime_to_timespec(base->gettime());
477 static int alarm_timer_create(
struct k_itimer *new_timer)
480 struct alarm_base *base;
488 type = clock2alarm(new_timer->
it_clock);
489 base = &alarm_bases[
type];
501 static void alarm_timer_get(
struct k_itimer *timr,
507 ktime_to_timespec(timr->
it.
alarm.interval);
509 ktime_to_timespec(timr->
it.
alarm.alarmtimer.node.expires);
519 static int alarm_timer_del(
struct k_itimer *timr)
539 static int alarm_timer_set(
struct k_itimer *timr,
int flags,
547 alarm_timer_get(timr, old_setting);
556 timespec_to_ktime(new_setting->
it_value));
584 static int alarmtimer_do_nsleep(
struct alarm *alarm,
ktime_t absexp)
617 rem = ktime_sub(exp, alarm_bases[type].gettime());
621 rmt = ktime_to_timespec(rem);
645 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
647 if (alarmtimer_do_nsleep(&alarm, exp))
651 alarmtimer_freezerset(exp, type);
655 ret = update_rmtp(exp, type, rmtp);
676 static int alarm_timer_nsleep(
const clockid_t which_clock,
int flags,
691 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
693 exp = timespec_to_ktime(*tsreq);
697 exp = ktime_add(now, exp);
700 if (alarmtimer_do_nsleep(&alarm, exp))
704 alarmtimer_freezerset(exp, type);
713 ret = update_rmtp(exp, type, rmtp);
719 restart->
fn = alarm_timer_nsleep_restart;
731 static const struct dev_pm_ops alarmtimer_pm_ops = {
732 .suspend = alarmtimer_suspend,
737 .name =
"alarmtimer",
738 .pm = &alarmtimer_pm_ops,
748 static int __init alarmtimer_init(
void)
755 .clock_get = alarm_clock_get,
756 .timer_create = alarm_timer_create,
757 .timer_set = alarm_timer_set,
758 .timer_del = alarm_timer_del,
759 .timer_get = alarm_timer_get,
760 .nsleep = alarm_timer_nsleep,
763 alarmtimer_rtc_timer_init();
774 timerqueue_init_head(&alarm_bases[i].timerqueue);
778 error = alarmtimer_rtc_interface_setup();
786 pdev = platform_device_register_simple(
"alarmtimer", -1,
NULL, 0);
788 error = PTR_ERR(pdev);
797 alarmtimer_rtc_interface_remove();