Merge tag 'v3.10.56' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / time / alarmtimer.c
CommitLineData
ff3ead96
JS
1/*
2 * Alarmtimer interface
3 *
4 * This interface provides a timer which is similarto hrtimers,
5 * but triggers a RTC alarm if the box is suspend.
6 *
7 * This interface is influenced by the Android RTC Alarm timer
8 * interface.
9 *
10 * Copyright (C) 2010 IBM Corperation
11 *
12 * Author: John Stultz <john.stultz@linaro.org>
13 *
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.
17 */
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>
6fa3eb70
S
28#include <linux/xlog.h>
29#include <linux/module.h>
30#include <mach/mtk_rtc.h>
31
32#define XLOG_MYTAG "Power/Alarm"
33
34#define ANDROID_ALARM_PRINT_INFO (1U << 0)
35#define ANDROID_ALARM_PRINT_IO (1U << 1)
36#define ANDROID_ALARM_PRINT_INT (1U << 2)
37
38static int debug_mask = ANDROID_ALARM_PRINT_INFO;
39module_param_named(debug_mask, debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP);
40
41#define alarm_dbg(debug_level_mask, fmt, args...) \
42do { \
43 if (debug_mask & ANDROID_ALARM_PRINT_##debug_level_mask) \
44 xlog_printk(ANDROID_LOG_INFO, XLOG_MYTAG, fmt, ##args); \
45} while (0)
ff3ead96 46
180bf812
JS
47/**
48 * struct alarm_base - Alarm timer bases
49 * @lock: Lock for syncrhonized access to the base
50 * @timerqueue: Timerqueue head managing the list of events
51 * @timer: hrtimer used to schedule events while running
52 * @gettime: Function to read the time correlating to the base
53 * @base_clockid: clockid for the base
180bf812 54 */
ff3ead96
JS
55static struct alarm_base {
56 spinlock_t lock;
57 struct timerqueue_head timerqueue;
ff3ead96
JS
58 ktime_t (*gettime)(void);
59 clockid_t base_clockid;
ff3ead96
JS
60} alarm_bases[ALARM_NUMTYPE];
61
c008ba58
JS
62/* freezer delta & lock used to handle clock_nanosleep triggered wakeups */
63static ktime_t freezer_delta;
64static DEFINE_SPINLOCK(freezer_delta_lock);
65
59a93c27
TP
66static struct wakeup_source *ws;
67
472647dc 68#ifdef CONFIG_RTC_CLASS
180bf812 69/* rtc timer and device for setting alarm wakeups at suspend */
c5e14e76 70static struct rtc_timer rtctimer;
ff3ead96 71static struct rtc_device *rtcdev;
c008ba58 72static DEFINE_SPINLOCK(rtcdev_lock);
ff3ead96 73
c008ba58
JS
74/**
75 * alarmtimer_get_rtcdev - Return selected rtcdevice
76 *
77 * This function returns the rtc device to use for wakealarms.
78 * If one has not already been chosen, it checks to see if a
79 * functional rtc device is available.
80 */
57c498fa 81struct rtc_device *alarmtimer_get_rtcdev(void)
c008ba58 82{
c008ba58
JS
83 unsigned long flags;
84 struct rtc_device *ret;
85
86 spin_lock_irqsave(&rtcdev_lock, flags);
c008ba58
JS
87 ret = rtcdev;
88 spin_unlock_irqrestore(&rtcdev_lock, flags);
89
90 return ret;
91}
8bc0dafb
JS
92
93
94static int alarmtimer_rtc_add_device(struct device *dev,
95 struct class_interface *class_intf)
96{
97 unsigned long flags;
98 struct rtc_device *rtc = to_rtc_device(dev);
99
100 if (rtcdev)
101 return -EBUSY;
102
103 if (!rtc->ops->set_alarm)
104 return -1;
6fa3eb70
S
105 //if (!device_may_wakeup(rtc->dev.parent))
106 // return -1;
8bc0dafb
JS
107
108 spin_lock_irqsave(&rtcdev_lock, flags);
109 if (!rtcdev) {
110 rtcdev = rtc;
111 /* hold a reference so it doesn't go away */
112 get_device(dev);
113 }
114 spin_unlock_irqrestore(&rtcdev_lock, flags);
115 return 0;
116}
117
c5e14e76
TG
118static inline void alarmtimer_rtc_timer_init(void)
119{
120 rtc_timer_init(&rtctimer, NULL, NULL);
121}
122
8bc0dafb
JS
123static struct class_interface alarmtimer_rtc_interface = {
124 .add_dev = &alarmtimer_rtc_add_device,
125};
126
4523f6ad 127static int alarmtimer_rtc_interface_setup(void)
8bc0dafb
JS
128{
129 alarmtimer_rtc_interface.class = rtc_class;
4523f6ad
TG
130 return class_interface_register(&alarmtimer_rtc_interface);
131}
132static void alarmtimer_rtc_interface_remove(void)
133{
134 class_interface_unregister(&alarmtimer_rtc_interface);
8bc0dafb 135}
1c6b39ad 136#else
57c498fa 137struct rtc_device *alarmtimer_get_rtcdev(void)
4523f6ad
TG
138{
139 return NULL;
140}
141#define rtcdev (NULL)
142static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
143static inline void alarmtimer_rtc_interface_remove(void) { }
c5e14e76 144static inline void alarmtimer_rtc_timer_init(void) { }
c008ba58 145#endif
ff3ead96 146
6fa3eb70
S
147void alarm_set_power_on(struct timespec new_pwron_time, bool logo)
148{
149 unsigned long pwron_time;
150 struct rtc_wkalrm alm;
151 struct rtc_device *alarm_rtc_dev;
152// ktime_t now;
153
154 printk("alarm set power on\n");
155
156#ifdef RTC_PWRON_SEC
157 /* round down the second */
158 new_pwron_time.tv_sec = (new_pwron_time.tv_sec / 60) * 60;
159#endif
160 if (new_pwron_time.tv_sec > 0) {
161 pwron_time = new_pwron_time.tv_sec;
162#ifdef RTC_PWRON_SEC
163 pwron_time += RTC_PWRON_SEC;
164#endif
165 alm.enabled = (logo ? 3 : 2);
166 } else {
167 pwron_time = 0;
168 alm.enabled = 4;
169 }
170 alarm_rtc_dev = alarmtimer_get_rtcdev();
171 rtc_time_to_tm(pwron_time, &alm.time);
172/*
173 rtc_timer_cancel(alarm_rtc_dev, &rtctimer);
174 now = rtc_tm_to_ktime(alm.time);
175 rtc_timer_start(alarm_rtc_dev, &rtctimer, now, ktime_set(0, 0));
176*/
177 rtc_timer_cancel(alarm_rtc_dev, &rtctimer);
178 rtc_set_alarm(alarm_rtc_dev, &alm);
179 rtc_set_alarm_poweron(alarm_rtc_dev, &alm);
180}
181
182void alarm_get_power_on(struct rtc_wkalrm *alm)
183{
184 if (!alm)
185 return;
186
187 memset(alm, 0, sizeof(struct rtc_wkalrm));
188#ifndef CONFIG_MTK_FPGA
189 rtc_read_pwron_alarm(alm);
190#endif
191}
192
180bf812 193/**
ff3ead96
JS
194 * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
195 * @base: pointer to the base where the timer is being run
196 * @alarm: pointer to alarm being enqueued.
197 *
dae373be 198 * Adds alarm to a alarm_base timerqueue
ff3ead96
JS
199 *
200 * Must hold base->lock when calling.
201 */
202static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
203{
dae373be
JS
204 if (alarm->state & ALARMTIMER_STATE_ENQUEUED)
205 timerqueue_del(&base->timerqueue, &alarm->node);
206
ff3ead96 207 timerqueue_add(&base->timerqueue, &alarm->node);
a28cde81 208 alarm->state |= ALARMTIMER_STATE_ENQUEUED;
ff3ead96
JS
209}
210
180bf812 211/**
a65bcc12 212 * alarmtimer_dequeue - Removes an alarm timer from an alarm_base timerqueue
ff3ead96
JS
213 * @base: pointer to the base where the timer is running
214 * @alarm: pointer to alarm being removed
215 *
dae373be 216 * Removes alarm to a alarm_base timerqueue
ff3ead96
JS
217 *
218 * Must hold base->lock when calling.
219 */
a65bcc12 220static void alarmtimer_dequeue(struct alarm_base *base, struct alarm *alarm)
ff3ead96 221{
a28cde81
JS
222 if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED))
223 return;
224
ff3ead96 225 timerqueue_del(&base->timerqueue, &alarm->node);
a28cde81 226 alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
ff3ead96
JS
227}
228
7068b7a1 229
180bf812 230/**
7068b7a1
JS
231 * alarmtimer_fired - Handles alarm hrtimer being fired.
232 * @timer: pointer to hrtimer being run
ff3ead96 233 *
180bf812
JS
234 * When a alarm timer fires, this runs through the timerqueue to
235 * see which alarms expired, and runs those. If there are more alarm
236 * timers queued for the future, we set the hrtimer to fire when
237 * when the next future alarm timer expires.
ff3ead96 238 */
7068b7a1 239static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
ff3ead96 240{
dae373be
JS
241 struct alarm *alarm = container_of(timer, struct alarm, timer);
242 struct alarm_base *base = &alarm_bases[alarm->type];
ff3ead96 243 unsigned long flags;
7068b7a1 244 int ret = HRTIMER_NORESTART;
54da23b7 245 int restart = ALARMTIMER_NORESTART;
ff3ead96 246
6fa3eb70
S
247 alarm_dbg(INT, "alarmtimer_fired \n");
248
ff3ead96 249 spin_lock_irqsave(&base->lock, flags);
a65bcc12 250 alarmtimer_dequeue(base, alarm);
dae373be 251 spin_unlock_irqrestore(&base->lock, flags);
54da23b7 252
dae373be
JS
253 if (alarm->function)
254 restart = alarm->function(alarm, base->gettime());
ff3ead96 255
dae373be
JS
256 spin_lock_irqsave(&base->lock, flags);
257 if (restart != ALARMTIMER_NORESTART) {
258 hrtimer_set_expires(&alarm->timer, alarm->node.expires);
259 alarmtimer_enqueue(base, alarm);
7068b7a1 260 ret = HRTIMER_RESTART;
ff3ead96
JS
261 }
262 spin_unlock_irqrestore(&base->lock, flags);
ff3ead96 263
7068b7a1 264 return ret;
ff3ead96 265
ff3ead96
JS
266}
267
6fa3eb70
S
268ktime_t alarm_expires_remaining(const struct alarm *alarm)
269{
270 struct alarm_base *base = &alarm_bases[alarm->type];
271 return ktime_sub(alarm->node.expires, base->gettime());
272}
273
472647dc 274#ifdef CONFIG_RTC_CLASS
180bf812 275/**
ff3ead96
JS
276 * alarmtimer_suspend - Suspend time callback
277 * @dev: unused
278 * @state: unused
279 *
280 * When we are going into suspend, we look through the bases
281 * to see which is the soonest timer to expire. We then
282 * set an rtc timer to fire that far into the future, which
283 * will wake us from suspend.
284 */
285static int alarmtimer_suspend(struct device *dev)
286{
287 struct rtc_time tm;
288 ktime_t min, now;
289 unsigned long flags;
c008ba58 290 struct rtc_device *rtc;
ff3ead96 291 int i;
59a93c27 292 int ret;
ff3ead96
JS
293
294 spin_lock_irqsave(&freezer_delta_lock, flags);
295 min = freezer_delta;
296 freezer_delta = ktime_set(0, 0);
297 spin_unlock_irqrestore(&freezer_delta_lock, flags);
298
8bc0dafb 299 rtc = alarmtimer_get_rtcdev();
ff3ead96 300 /* If we have no rtcdev, just return */
c008ba58 301 if (!rtc)
ff3ead96
JS
302 return 0;
303
304 /* Find the soonest timer to expire*/
305 for (i = 0; i < ALARM_NUMTYPE; i++) {
306 struct alarm_base *base = &alarm_bases[i];
307 struct timerqueue_node *next;
308 ktime_t delta;
309
310 spin_lock_irqsave(&base->lock, flags);
311 next = timerqueue_getnext(&base->timerqueue);
312 spin_unlock_irqrestore(&base->lock, flags);
313 if (!next)
314 continue;
315 delta = ktime_sub(next->expires, base->gettime());
316 if (!min.tv64 || (delta.tv64 < min.tv64))
317 min = delta;
318 }
6fa3eb70
S
319 if (min.tv64 == 0) {
320 alarm_dbg(INT, "min.tv64 == 0\n");
ff3ead96 321 return 0;
6fa3eb70 322 }
59a93c27 323 if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) {
6fa3eb70 324 alarm_dbg(INT, "min.tv64 < 2S, give up suspend\n");
59a93c27
TP
325 __pm_wakeup_event(ws, 2 * MSEC_PER_SEC);
326 return -EBUSY;
327 }
ff3ead96
JS
328
329 /* Setup an rtc timer to fire that far in the future */
c008ba58
JS
330 rtc_timer_cancel(rtc, &rtctimer);
331 rtc_read_time(rtc, &tm);
ff3ead96
JS
332 now = rtc_tm_to_ktime(tm);
333 now = ktime_add(now, min);
334
6fa3eb70
S
335 alarm_dbg(INFO, "now:%02d=%02d:%02d %02d/%02d/%04d. min.tv64=%lld\n",
336 tm.tm_hour, tm.tm_min,
337 tm.tm_sec, tm.tm_mon + 1,
338 tm.tm_mday,
339 tm.tm_year + 1900, min.tv64);
59a93c27
TP
340 /* Set alarm, if in the past reject suspend briefly to handle */
341 ret = rtc_timer_start(rtc, &rtctimer, now, ktime_set(0, 0));
342 if (ret < 0)
343 __pm_wakeup_event(ws, MSEC_PER_SEC);
344 return ret;
ff3ead96 345}
472647dc
JS
346#else
347static int alarmtimer_suspend(struct device *dev)
348{
349 return 0;
350}
351#endif
ff3ead96 352
9a7adcf5
JS
353static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
354{
355 ktime_t delta;
356 unsigned long flags;
357 struct alarm_base *base = &alarm_bases[type];
358
359 delta = ktime_sub(absexp, base->gettime());
360
361 spin_lock_irqsave(&freezer_delta_lock, flags);
362 if (!freezer_delta.tv64 || (delta.tv64 < freezer_delta.tv64))
363 freezer_delta = delta;
364 spin_unlock_irqrestore(&freezer_delta_lock, flags);
365}
366
367
180bf812 368/**
ff3ead96
JS
369 * alarm_init - Initialize an alarm structure
370 * @alarm: ptr to alarm to be initialized
371 * @type: the type of the alarm
372 * @function: callback that is run when the alarm fires
ff3ead96
JS
373 */
374void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
4b41308d 375 enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
ff3ead96
JS
376{
377 timerqueue_init(&alarm->node);
dae373be
JS
378 hrtimer_init(&alarm->timer, alarm_bases[type].base_clockid,
379 HRTIMER_MODE_ABS);
380 alarm->timer.function = alarmtimer_fired;
ff3ead96
JS
381 alarm->function = function;
382 alarm->type = type;
a28cde81 383 alarm->state = ALARMTIMER_STATE_INACTIVE;
ff3ead96
JS
384}
385
180bf812 386/**
6fa3eb70 387 * alarm_start - Sets an absolute alarm to fire
ff3ead96
JS
388 * @alarm: ptr to alarm to set
389 * @start: time to run the alarm
ff3ead96 390 */
dae373be 391int alarm_start(struct alarm *alarm, ktime_t start)
ff3ead96
JS
392{
393 struct alarm_base *base = &alarm_bases[alarm->type];
394 unsigned long flags;
dae373be 395 int ret;
ff3ead96
JS
396
397 spin_lock_irqsave(&base->lock, flags);
ff3ead96 398 alarm->node.expires = start;
ff3ead96 399 alarmtimer_enqueue(base, alarm);
dae373be
JS
400 ret = hrtimer_start(&alarm->timer, alarm->node.expires,
401 HRTIMER_MODE_ABS);
ff3ead96 402 spin_unlock_irqrestore(&base->lock, flags);
dae373be 403 return ret;
ff3ead96
JS
404}
405
6fa3eb70
S
406/**
407 * alarm_start_relative - Sets a relative alarm to fire
408 * @alarm: ptr to alarm to set
409 * @start: time relative to now to run the alarm
410 */
411int alarm_start_relative(struct alarm *alarm, ktime_t start)
412{
413 struct alarm_base *base = &alarm_bases[alarm->type];
414
415 start = ktime_add(start, base->gettime());
416 return alarm_start(alarm, start);
417}
418
419void alarm_restart(struct alarm *alarm)
420{
421 struct alarm_base *base = &alarm_bases[alarm->type];
422 unsigned long flags;
423
424 spin_lock_irqsave(&base->lock, flags);
425 hrtimer_set_expires(&alarm->timer, alarm->node.expires);
426 hrtimer_restart(&alarm->timer);
427 alarmtimer_enqueue(base, alarm);
428 spin_unlock_irqrestore(&base->lock, flags);
429}
430
180bf812 431/**
9082c465 432 * alarm_try_to_cancel - Tries to cancel an alarm timer
ff3ead96 433 * @alarm: ptr to alarm to be canceled
9082c465
JS
434 *
435 * Returns 1 if the timer was canceled, 0 if it was not running,
436 * and -1 if the callback was running
ff3ead96 437 */
9082c465 438int alarm_try_to_cancel(struct alarm *alarm)
ff3ead96
JS
439{
440 struct alarm_base *base = &alarm_bases[alarm->type];
441 unsigned long flags;
dae373be 442 int ret;
9082c465 443
dae373be
JS
444 spin_lock_irqsave(&base->lock, flags);
445 ret = hrtimer_try_to_cancel(&alarm->timer);
446 if (ret >= 0)
a65bcc12 447 alarmtimer_dequeue(base, alarm);
ff3ead96 448 spin_unlock_irqrestore(&base->lock, flags);
9082c465 449 return ret;
ff3ead96
JS
450}
451
452
9082c465
JS
453/**
454 * alarm_cancel - Spins trying to cancel an alarm timer until it is done
455 * @alarm: ptr to alarm to be canceled
456 *
457 * Returns 1 if the timer was canceled, 0 if it was not active.
458 */
459int alarm_cancel(struct alarm *alarm)
460{
461 for (;;) {
462 int ret = alarm_try_to_cancel(alarm);
463 if (ret >= 0)
464 return ret;
465 cpu_relax();
466 }
467}
468
dce75a8c
JS
469
470u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval)
471{
472 u64 overrun = 1;
473 ktime_t delta;
474
475 delta = ktime_sub(now, alarm->node.expires);
476
477 if (delta.tv64 < 0)
478 return 0;
479
480 if (unlikely(delta.tv64 >= interval.tv64)) {
481 s64 incr = ktime_to_ns(interval);
482
483 overrun = ktime_divns(delta, incr);
484
485 alarm->node.expires = ktime_add_ns(alarm->node.expires,
486 incr*overrun);
487
488 if (alarm->node.expires.tv64 > now.tv64)
489 return overrun;
490 /*
491 * This (and the ktime_add() below) is the
492 * correction for exact:
493 */
494 overrun++;
495 }
496
497 alarm->node.expires = ktime_add(alarm->node.expires, interval);
498 return overrun;
499}
500
6fa3eb70
S
501u64 alarm_forward_now(struct alarm *alarm, ktime_t interval)
502{
503 struct alarm_base *base = &alarm_bases[alarm->type];
504
505 return alarm_forward(alarm, base->gettime(), interval);
506}
dce75a8c
JS
507
508
509
180bf812 510/**
9a7adcf5
JS
511 * clock2alarm - helper that converts from clockid to alarmtypes
512 * @clockid: clockid.
9a7adcf5
JS
513 */
514static enum alarmtimer_type clock2alarm(clockid_t clockid)
515{
516 if (clockid == CLOCK_REALTIME_ALARM)
517 return ALARM_REALTIME;
518 if (clockid == CLOCK_BOOTTIME_ALARM)
519 return ALARM_BOOTTIME;
520 return -1;
521}
522
180bf812 523/**
9a7adcf5
JS
524 * alarm_handle_timer - Callback for posix timers
525 * @alarm: alarm that fired
526 *
527 * Posix timer callback for expired alarm timers.
528 */
4b41308d
JS
529static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
530 ktime_t now)
9a7adcf5 531{
3c478642 532 unsigned long flags;
9a7adcf5 533 struct k_itimer *ptr = container_of(alarm, struct k_itimer,
9e264762 534 it.alarm.alarmtimer);
3c478642
RL
535 enum alarmtimer_restart result = ALARMTIMER_NORESTART;
536
537 spin_lock_irqsave(&ptr->it_lock, flags);
5cebda5d
RL
538 if ((ptr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) {
539 if (posix_timer_event(ptr, 0) != 0)
540 ptr->it_overrun++;
541 }
4b41308d 542
54da23b7 543 /* Re-add periodic timers */
9e264762
JS
544 if (ptr->it.alarm.interval.tv64) {
545 ptr->it_overrun += alarm_forward(alarm, now,
546 ptr->it.alarm.interval);
3c478642 547 result = ALARMTIMER_RESTART;
54da23b7 548 }
3c478642
RL
549 spin_unlock_irqrestore(&ptr->it_lock, flags);
550
551 return result;
9a7adcf5
JS
552}
553
180bf812 554/**
9a7adcf5
JS
555 * alarm_clock_getres - posix getres interface
556 * @which_clock: clockid
557 * @tp: timespec to fill
558 *
559 * Returns the granularity of underlying alarm base clock
560 */
561static int alarm_clock_getres(const clockid_t which_clock, struct timespec *tp)
562{
563 clockid_t baseid = alarm_bases[clock2alarm(which_clock)].base_clockid;
564
1c6b39ad 565 if (!alarmtimer_get_rtcdev())
4501cfd0 566 return -EINVAL;
1c6b39ad 567
9a7adcf5
JS
568 return hrtimer_get_res(baseid, tp);
569}
570
571/**
572 * alarm_clock_get - posix clock_get interface
573 * @which_clock: clockid
574 * @tp: timespec to fill.
575 *
576 * Provides the underlying alarm base time.
577 */
578static int alarm_clock_get(clockid_t which_clock, struct timespec *tp)
579{
580 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
581
1c6b39ad 582 if (!alarmtimer_get_rtcdev())
4501cfd0 583 return -EINVAL;
1c6b39ad 584
9a7adcf5
JS
585 *tp = ktime_to_timespec(base->gettime());
586 return 0;
587}
588
589/**
590 * alarm_timer_create - posix timer_create interface
591 * @new_timer: k_itimer pointer to manage
592 *
593 * Initializes the k_itimer structure.
594 */
595static int alarm_timer_create(struct k_itimer *new_timer)
596{
597 enum alarmtimer_type type;
598 struct alarm_base *base;
599
1c6b39ad
JS
600 if (!alarmtimer_get_rtcdev())
601 return -ENOTSUPP;
602
9a7adcf5
JS
603 if (!capable(CAP_WAKE_ALARM))
604 return -EPERM;
605
606 type = clock2alarm(new_timer->it_clock);
607 base = &alarm_bases[type];
9e264762 608 alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
9a7adcf5
JS
609 return 0;
610}
611
612/**
613 * alarm_timer_get - posix timer_get interface
614 * @new_timer: k_itimer pointer
615 * @cur_setting: itimerspec data to fill
616 *
617 * Copies the itimerspec data out from the k_itimer
618 */
619static void alarm_timer_get(struct k_itimer *timr,
620 struct itimerspec *cur_setting)
621{
ea7802f6
JS
622 memset(cur_setting, 0, sizeof(struct itimerspec));
623
9a7adcf5 624 cur_setting->it_interval =
9e264762 625 ktime_to_timespec(timr->it.alarm.interval);
9a7adcf5 626 cur_setting->it_value =
9e264762 627 ktime_to_timespec(timr->it.alarm.alarmtimer.node.expires);
9a7adcf5
JS
628 return;
629}
630
631/**
632 * alarm_timer_del - posix timer_del interface
633 * @timr: k_itimer pointer to be deleted
634 *
635 * Cancels any programmed alarms for the given timer.
636 */
637static int alarm_timer_del(struct k_itimer *timr)
638{
1c6b39ad
JS
639 if (!rtcdev)
640 return -ENOTSUPP;
641
9082c465
JS
642 if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
643 return TIMER_RETRY;
644
9a7adcf5
JS
645 return 0;
646}
647
648/**
649 * alarm_timer_set - posix timer_set interface
650 * @timr: k_itimer pointer to be deleted
651 * @flags: timer flags
652 * @new_setting: itimerspec to be used
653 * @old_setting: itimerspec being replaced
654 *
655 * Sets the timer to new_setting, and starts the timer.
656 */
657static int alarm_timer_set(struct k_itimer *timr, int flags,
658 struct itimerspec *new_setting,
659 struct itimerspec *old_setting)
660{
c9331927
JS
661 ktime_t exp;
662
1c6b39ad
JS
663 if (!rtcdev)
664 return -ENOTSUPP;
665
c9331927
JS
666 if (flags & ~TIMER_ABSTIME)
667 return -EINVAL;
668
971c90bf
JS
669 if (old_setting)
670 alarm_timer_get(timr, old_setting);
9a7adcf5
JS
671
672 /* If the timer was already set, cancel it */
9082c465
JS
673 if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
674 return TIMER_RETRY;
9a7adcf5
JS
675
676 /* start the timer */
9e264762 677 timr->it.alarm.interval = timespec_to_ktime(new_setting->it_interval);
c9331927
JS
678 exp = timespec_to_ktime(new_setting->it_value);
679 /* Convert (if necessary) to absolute time */
680 if (flags != TIMER_ABSTIME) {
681 ktime_t now;
682
683 now = alarm_bases[timr->it.alarm.alarmtimer.type].gettime();
684 exp = ktime_add(now, exp);
685 }
686
687 alarm_start(&timr->it.alarm.alarmtimer, exp);
9a7adcf5
JS
688 return 0;
689}
690
691/**
692 * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
693 * @alarm: ptr to alarm that fired
694 *
695 * Wakes up the task that set the alarmtimer
696 */
4b41308d
JS
697static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
698 ktime_t now)
9a7adcf5
JS
699{
700 struct task_struct *task = (struct task_struct *)alarm->data;
701
702 alarm->data = NULL;
703 if (task)
704 wake_up_process(task);
4b41308d 705 return ALARMTIMER_NORESTART;
9a7adcf5
JS
706}
707
708/**
709 * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
710 * @alarm: ptr to alarmtimer
711 * @absexp: absolute expiration time
712 *
713 * Sets the alarm timer and sleeps until it is fired or interrupted.
714 */
715static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp)
716{
717 alarm->data = (void *)current;
718 do {
719 set_current_state(TASK_INTERRUPTIBLE);
9e264762 720 alarm_start(alarm, absexp);
9a7adcf5
JS
721 if (likely(alarm->data))
722 schedule();
723
724 alarm_cancel(alarm);
725 } while (alarm->data && !signal_pending(current));
726
727 __set_current_state(TASK_RUNNING);
728
729 return (alarm->data == NULL);
730}
731
732
733/**
734 * update_rmtp - Update remaining timespec value
735 * @exp: expiration time
736 * @type: timer type
737 * @rmtp: user pointer to remaining timepsec value
738 *
739 * Helper function that fills in rmtp value with time between
740 * now and the exp value
741 */
742static int update_rmtp(ktime_t exp, enum alarmtimer_type type,
743 struct timespec __user *rmtp)
744{
745 struct timespec rmt;
746 ktime_t rem;
747
748 rem = ktime_sub(exp, alarm_bases[type].gettime());
749
750 if (rem.tv64 <= 0)
751 return 0;
752 rmt = ktime_to_timespec(rem);
753
754 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
755 return -EFAULT;
756
757 return 1;
758
759}
760
761/**
762 * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
763 * @restart: ptr to restart block
764 *
765 * Handles restarted clock_nanosleep calls
766 */
767static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
768{
ab8177bc 769 enum alarmtimer_type type = restart->nanosleep.clockid;
9a7adcf5
JS
770 ktime_t exp;
771 struct timespec __user *rmtp;
772 struct alarm alarm;
773 int ret = 0;
774
775 exp.tv64 = restart->nanosleep.expires;
776 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
777
778 if (alarmtimer_do_nsleep(&alarm, exp))
779 goto out;
780
781 if (freezing(current))
782 alarmtimer_freezerset(exp, type);
783
784 rmtp = restart->nanosleep.rmtp;
785 if (rmtp) {
786 ret = update_rmtp(exp, type, rmtp);
787 if (ret <= 0)
788 goto out;
789 }
790
791
792 /* The other values in restart are already filled in */
793 ret = -ERESTART_RESTARTBLOCK;
794out:
795 return ret;
796}
797
798/**
799 * alarm_timer_nsleep - alarmtimer nanosleep
800 * @which_clock: clockid
801 * @flags: determins abstime or relative
802 * @tsreq: requested sleep time (abs or rel)
803 * @rmtp: remaining sleep time saved
804 *
805 * Handles clock_nanosleep calls against _ALARM clockids
806 */
807static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
808 struct timespec *tsreq, struct timespec __user *rmtp)
809{
810 enum alarmtimer_type type = clock2alarm(which_clock);
811 struct alarm alarm;
812 ktime_t exp;
813 int ret = 0;
814 struct restart_block *restart;
815
1c6b39ad
JS
816 if (!alarmtimer_get_rtcdev())
817 return -ENOTSUPP;
818
c9331927
JS
819 if (flags & ~TIMER_ABSTIME)
820 return -EINVAL;
821
9a7adcf5
JS
822 if (!capable(CAP_WAKE_ALARM))
823 return -EPERM;
824
825 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
826
827 exp = timespec_to_ktime(*tsreq);
828 /* Convert (if necessary) to absolute time */
829 if (flags != TIMER_ABSTIME) {
830 ktime_t now = alarm_bases[type].gettime();
831 exp = ktime_add(now, exp);
832 }
833
834 if (alarmtimer_do_nsleep(&alarm, exp))
835 goto out;
836
837 if (freezing(current))
838 alarmtimer_freezerset(exp, type);
839
840 /* abs timers don't set remaining time or restart */
841 if (flags == TIMER_ABSTIME) {
842 ret = -ERESTARTNOHAND;
843 goto out;
844 }
845
846 if (rmtp) {
847 ret = update_rmtp(exp, type, rmtp);
848 if (ret <= 0)
849 goto out;
850 }
851
852 restart = &current_thread_info()->restart_block;
853 restart->fn = alarm_timer_nsleep_restart;
ab8177bc 854 restart->nanosleep.clockid = type;
9a7adcf5
JS
855 restart->nanosleep.expires = exp.tv64;
856 restart->nanosleep.rmtp = rmtp;
857 ret = -ERESTART_RESTARTBLOCK;
858
859out:
860 return ret;
861}
ff3ead96 862
ff3ead96
JS
863
864/* Suspend hook structures */
865static const struct dev_pm_ops alarmtimer_pm_ops = {
866 .suspend = alarmtimer_suspend,
867};
868
869static struct platform_driver alarmtimer_driver = {
870 .driver = {
871 .name = "alarmtimer",
872 .pm = &alarmtimer_pm_ops,
873 }
874};
875
876/**
877 * alarmtimer_init - Initialize alarm timer code
878 *
879 * This function initializes the alarm bases and registers
880 * the posix clock ids.
881 */
882static int __init alarmtimer_init(void)
883{
4523f6ad 884 struct platform_device *pdev;
ff3ead96
JS
885 int error = 0;
886 int i;
9a7adcf5
JS
887 struct k_clock alarm_clock = {
888 .clock_getres = alarm_clock_getres,
889 .clock_get = alarm_clock_get,
890 .timer_create = alarm_timer_create,
891 .timer_set = alarm_timer_set,
892 .timer_del = alarm_timer_del,
893 .timer_get = alarm_timer_get,
894 .nsleep = alarm_timer_nsleep,
895 };
896
c5e14e76 897 alarmtimer_rtc_timer_init();
ad30dfa9 898
9a7adcf5
JS
899 posix_timers_register_clock(CLOCK_REALTIME_ALARM, &alarm_clock);
900 posix_timers_register_clock(CLOCK_BOOTTIME_ALARM, &alarm_clock);
ff3ead96
JS
901
902 /* Initialize alarm bases */
903 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
904 alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
905 alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
906 alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
907 for (i = 0; i < ALARM_NUMTYPE; i++) {
908 timerqueue_init_head(&alarm_bases[i].timerqueue);
909 spin_lock_init(&alarm_bases[i].lock);
ff3ead96 910 }
8bc0dafb 911
4523f6ad
TG
912 error = alarmtimer_rtc_interface_setup();
913 if (error)
914 return error;
915
ff3ead96 916 error = platform_driver_register(&alarmtimer_driver);
4523f6ad
TG
917 if (error)
918 goto out_if;
ff3ead96 919
4523f6ad
TG
920 pdev = platform_device_register_simple("alarmtimer", -1, NULL, 0);
921 if (IS_ERR(pdev)) {
922 error = PTR_ERR(pdev);
923 goto out_drv;
924 }
59a93c27 925 ws = wakeup_source_register("alarmtimer");
4523f6ad
TG
926 return 0;
927
928out_drv:
929 platform_driver_unregister(&alarmtimer_driver);
930out_if:
931 alarmtimer_rtc_interface_remove();
ff3ead96
JS
932 return error;
933}
934device_initcall(alarmtimer_init);