blob: 60e2aea346616269050ea4391e666a6a0dcc8646 [file] [log] [blame]
John Stultzff3ead92011-01-11 09:42:13 -08001/*
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>
Mao Jinlong2e1a4ae2016-02-17 15:21:49 +080028#include <linux/workqueue.h>
John Stultzff3ead92011-01-11 09:42:13 -080029
John Stultz180bf812011-04-28 12:58:11 -070030/**
31 * struct alarm_base - Alarm timer bases
32 * @lock: Lock for syncrhonized access to the base
33 * @timerqueue: Timerqueue head managing the list of events
John Stultz180bf812011-04-28 12:58:11 -070034 * @gettime: Function to read the time correlating to the base
35 * @base_clockid: clockid for the base
John Stultz180bf812011-04-28 12:58:11 -070036 */
John Stultzff3ead92011-01-11 09:42:13 -080037static struct alarm_base {
38 spinlock_t lock;
39 struct timerqueue_head timerqueue;
John Stultzff3ead92011-01-11 09:42:13 -080040 ktime_t (*gettime)(void);
41 clockid_t base_clockid;
John Stultzff3ead92011-01-11 09:42:13 -080042} alarm_bases[ALARM_NUMTYPE];
43
John Stultzc008ba582011-06-16 18:27:09 -070044/* freezer delta & lock used to handle clock_nanosleep triggered wakeups */
45static ktime_t freezer_delta;
46static DEFINE_SPINLOCK(freezer_delta_lock);
47
Todd Poynor59a93c22012-08-09 00:37:27 -070048static struct wakeup_source *ws;
Mao Jinlong2e1a4ae2016-02-17 15:21:49 +080049static struct delayed_work work;
50static struct workqueue_struct *power_off_alarm_workqueue;
Todd Poynor59a93c22012-08-09 00:37:27 -070051
John Stultz472647d2011-04-29 15:03:10 -070052#ifdef CONFIG_RTC_CLASS
John Stultz180bf812011-04-28 12:58:11 -070053/* rtc timer and device for setting alarm wakeups at suspend */
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +010054static struct rtc_timer rtctimer;
John Stultzff3ead92011-01-11 09:42:13 -080055static struct rtc_device *rtcdev;
John Stultzc008ba582011-06-16 18:27:09 -070056static DEFINE_SPINLOCK(rtcdev_lock);
Mao Jinlong2e1a4ae2016-02-17 15:21:49 +080057static struct mutex power_on_alarm_lock;
58static struct alarm init_alarm;
59
60/**
61 * power_on_alarm_init - Init power on alarm value
62 *
63 * Read rtc alarm value after device booting up and add this alarm
64 * into alarm queue.
65 */
66void power_on_alarm_init(void)
67{
68 struct rtc_wkalrm rtc_alarm;
69 struct rtc_time rt;
70 unsigned long alarm_time;
71 struct rtc_device *rtc;
72 ktime_t alarm_ktime;
73
74 rtc = alarmtimer_get_rtcdev();
75
76 if (!rtc)
77 return;
78
79 rtc_read_alarm(rtc, &rtc_alarm);
80 rt = rtc_alarm.time;
81
82 rtc_tm_to_time(&rt, &alarm_time);
83
84 if (alarm_time) {
85 alarm_ktime = ktime_set(alarm_time, 0);
86 alarm_init(&init_alarm, ALARM_POWEROFF_REALTIME, NULL);
87 alarm_start(&init_alarm, alarm_ktime);
88 }
89}
90
91/**
92 * set_power_on_alarm - set power on alarm value into rtc register
93 *
94 * Get the soonest power off alarm timer and set the alarm value into rtc
95 * register.
96 */
97void set_power_on_alarm(void)
98{
99 int rc;
100 struct timespec wall_time, alarm_ts;
101 long alarm_secs = 0l;
102 long rtc_secs, alarm_time, alarm_delta;
103 struct rtc_time rtc_time;
104 struct rtc_wkalrm alarm;
105 struct rtc_device *rtc;
106 struct timerqueue_node *next;
107 unsigned long flags;
108 struct alarm_base *base = &alarm_bases[ALARM_POWEROFF_REALTIME];
109
110 rc = mutex_lock_interruptible(&power_on_alarm_lock);
111 if (rc != 0)
112 return;
113
114 spin_lock_irqsave(&base->lock, flags);
115 next = timerqueue_getnext(&base->timerqueue);
116 spin_unlock_irqrestore(&base->lock, flags);
117
118 if (next) {
119 alarm_ts = ktime_to_timespec(next->expires);
120 alarm_secs = alarm_ts.tv_sec;
121 }
122
123 if (!alarm_secs)
124 goto disable_alarm;
125
126 getnstimeofday(&wall_time);
127
128 /*
129 * alarm_secs have to be bigger than "wall_time +1".
130 * It is to make sure that alarm time will be always
131 * bigger than wall time.
132 */
133 if (alarm_secs <= wall_time.tv_sec + 1)
134 goto disable_alarm;
135
136 rtc = alarmtimer_get_rtcdev();
137 if (!rtc)
138 goto exit;
139
140 rtc_read_time(rtc, &rtc_time);
141 rtc_tm_to_time(&rtc_time, &rtc_secs);
142 alarm_delta = wall_time.tv_sec - rtc_secs;
143 alarm_time = alarm_secs - alarm_delta;
144
145 rtc_time_to_tm(alarm_time, &alarm.time);
146 alarm.enabled = 1;
147 rc = rtc_set_alarm(rtcdev, &alarm);
148 if (rc)
149 goto disable_alarm;
150
151 mutex_unlock(&power_on_alarm_lock);
152 return;
153
154disable_alarm:
155 rtc_alarm_irq_enable(rtcdev, 0);
156exit:
157 mutex_unlock(&power_on_alarm_lock);
158}
John Stultzff3ead92011-01-11 09:42:13 -0800159
Mao Jinlonga0adcbc2016-02-17 15:19:08 +0800160static void alarmtimer_triggered_func(void *p)
161{
162 struct rtc_device *rtc = rtcdev;
163
164 if (!(rtc->irq_data & RTC_AF))
165 return;
166 __pm_wakeup_event(ws, 2 * MSEC_PER_SEC);
167}
168
169static struct rtc_task alarmtimer_rtc_task = {
170 .func = alarmtimer_triggered_func
171};
John Stultzc008ba582011-06-16 18:27:09 -0700172/**
John Stultzc008ba582011-06-16 18:27:09 -0700173 * alarmtimer_get_rtcdev - Return selected rtcdevice
174 *
175 * This function returns the rtc device to use for wakealarms.
176 * If one has not already been chosen, it checks to see if a
177 * functional rtc device is available.
178 */
John Stultz57c498f2012-04-20 12:31:45 -0700179struct rtc_device *alarmtimer_get_rtcdev(void)
John Stultzc008ba582011-06-16 18:27:09 -0700180{
John Stultzc008ba582011-06-16 18:27:09 -0700181 unsigned long flags;
Mao Jinlonga0adcbc2016-02-17 15:19:08 +0800182 struct rtc_device *ret = NULL;
John Stultzc008ba582011-06-16 18:27:09 -0700183
184 spin_lock_irqsave(&rtcdev_lock, flags);
John Stultzc008ba582011-06-16 18:27:09 -0700185 ret = rtcdev;
186 spin_unlock_irqrestore(&rtcdev_lock, flags);
187
188 return ret;
189}
Pramod Gurav71d5d2b2014-06-13 11:49:42 +0530190EXPORT_SYMBOL_GPL(alarmtimer_get_rtcdev);
John Stultz8bc0daf2011-07-14 18:35:13 -0700191
192static int alarmtimer_rtc_add_device(struct device *dev,
193 struct class_interface *class_intf)
194{
195 unsigned long flags;
Mao Jinlonga0adcbc2016-02-17 15:19:08 +0800196 int err = 0;
John Stultz8bc0daf2011-07-14 18:35:13 -0700197 struct rtc_device *rtc = to_rtc_device(dev);
John Stultz8bc0daf2011-07-14 18:35:13 -0700198 if (rtcdev)
199 return -EBUSY;
John Stultz8bc0daf2011-07-14 18:35:13 -0700200 if (!rtc->ops->set_alarm)
201 return -1;
John Stultz8bc0daf2011-07-14 18:35:13 -0700202
203 spin_lock_irqsave(&rtcdev_lock, flags);
204 if (!rtcdev) {
Mao Jinlonga0adcbc2016-02-17 15:19:08 +0800205 err = rtc_irq_register(rtc, &alarmtimer_rtc_task);
206 if (err)
207 goto rtc_irq_reg_err;
John Stultz8bc0daf2011-07-14 18:35:13 -0700208 rtcdev = rtc;
209 /* hold a reference so it doesn't go away */
210 get_device(dev);
211 }
Mao Jinlonga0adcbc2016-02-17 15:19:08 +0800212
213rtc_irq_reg_err:
John Stultz8bc0daf2011-07-14 18:35:13 -0700214 spin_unlock_irqrestore(&rtcdev_lock, flags);
Mao Jinlonga0adcbc2016-02-17 15:19:08 +0800215 return err;
216
217}
218
219static void alarmtimer_rtc_remove_device(struct device *dev,
220 struct class_interface *class_intf)
221{
222 if (rtcdev && dev == &rtcdev->dev) {
223 rtc_irq_unregister(rtcdev, &alarmtimer_rtc_task);
224 rtcdev = NULL;
225 }
John Stultz8bc0daf2011-07-14 18:35:13 -0700226}
227
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +0100228static inline void alarmtimer_rtc_timer_init(void)
229{
Mao Jinlong2e1a4ae2016-02-17 15:21:49 +0800230 mutex_init(&power_on_alarm_lock);
231
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +0100232 rtc_timer_init(&rtctimer, NULL, NULL);
233}
234
John Stultz8bc0daf2011-07-14 18:35:13 -0700235static struct class_interface alarmtimer_rtc_interface = {
236 .add_dev = &alarmtimer_rtc_add_device,
Mao Jinlonga0adcbc2016-02-17 15:19:08 +0800237 .remove_dev = &alarmtimer_rtc_remove_device,
John Stultz8bc0daf2011-07-14 18:35:13 -0700238};
239
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200240static int alarmtimer_rtc_interface_setup(void)
John Stultz8bc0daf2011-07-14 18:35:13 -0700241{
242 alarmtimer_rtc_interface.class = rtc_class;
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200243 return class_interface_register(&alarmtimer_rtc_interface);
244}
245static void alarmtimer_rtc_interface_remove(void)
246{
247 class_interface_unregister(&alarmtimer_rtc_interface);
John Stultz8bc0daf2011-07-14 18:35:13 -0700248}
John Stultz1c6b39a2011-06-16 18:47:37 -0700249#else
John Stultz57c498f2012-04-20 12:31:45 -0700250struct rtc_device *alarmtimer_get_rtcdev(void)
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200251{
252 return NULL;
253}
254#define rtcdev (NULL)
255static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
256static inline void alarmtimer_rtc_interface_remove(void) { }
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +0100257static inline void alarmtimer_rtc_timer_init(void) { }
Mao Jinlong2e1a4ae2016-02-17 15:21:49 +0800258void set_power_on_alarm(void) { }
John Stultzc008ba582011-06-16 18:27:09 -0700259#endif
John Stultzff3ead92011-01-11 09:42:13 -0800260
Mao Jinlong2e1a4ae2016-02-17 15:21:49 +0800261static void alarm_work_func(struct work_struct *unused)
262{
263 set_power_on_alarm();
264}
265
John Stultz180bf812011-04-28 12:58:11 -0700266/**
John Stultzff3ead92011-01-11 09:42:13 -0800267 * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
268 * @base: pointer to the base where the timer is being run
269 * @alarm: pointer to alarm being enqueued.
270 *
John Stultzdae373b2012-09-13 19:12:16 -0400271 * Adds alarm to a alarm_base timerqueue
John Stultzff3ead92011-01-11 09:42:13 -0800272 *
273 * Must hold base->lock when calling.
274 */
275static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
276{
John Stultzdae373b2012-09-13 19:12:16 -0400277 if (alarm->state & ALARMTIMER_STATE_ENQUEUED)
278 timerqueue_del(&base->timerqueue, &alarm->node);
279
John Stultzff3ead92011-01-11 09:42:13 -0800280 timerqueue_add(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700281 alarm->state |= ALARMTIMER_STATE_ENQUEUED;
John Stultzff3ead92011-01-11 09:42:13 -0800282}
283
John Stultz180bf812011-04-28 12:58:11 -0700284/**
John Stultza65bcc12012-09-13 19:25:22 -0400285 * alarmtimer_dequeue - Removes an alarm timer from an alarm_base timerqueue
John Stultzff3ead92011-01-11 09:42:13 -0800286 * @base: pointer to the base where the timer is running
287 * @alarm: pointer to alarm being removed
288 *
John Stultzdae373b2012-09-13 19:12:16 -0400289 * Removes alarm to a alarm_base timerqueue
John Stultzff3ead92011-01-11 09:42:13 -0800290 *
291 * Must hold base->lock when calling.
292 */
John Stultza65bcc12012-09-13 19:25:22 -0400293static void alarmtimer_dequeue(struct alarm_base *base, struct alarm *alarm)
John Stultzff3ead92011-01-11 09:42:13 -0800294{
John Stultza28cde82011-08-10 12:30:21 -0700295 if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED))
296 return;
297
John Stultzff3ead92011-01-11 09:42:13 -0800298 timerqueue_del(&base->timerqueue, &alarm->node);
John Stultza28cde82011-08-10 12:30:21 -0700299 alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
John Stultzff3ead92011-01-11 09:42:13 -0800300}
301
John Stultz7068b7a2011-04-28 13:29:18 -0700302
John Stultz180bf812011-04-28 12:58:11 -0700303/**
John Stultz7068b7a2011-04-28 13:29:18 -0700304 * alarmtimer_fired - Handles alarm hrtimer being fired.
305 * @timer: pointer to hrtimer being run
John Stultzff3ead92011-01-11 09:42:13 -0800306 *
John Stultz180bf812011-04-28 12:58:11 -0700307 * When a alarm timer fires, this runs through the timerqueue to
308 * see which alarms expired, and runs those. If there are more alarm
309 * timers queued for the future, we set the hrtimer to fire when
310 * when the next future alarm timer expires.
John Stultzff3ead92011-01-11 09:42:13 -0800311 */
John Stultz7068b7a2011-04-28 13:29:18 -0700312static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
John Stultzff3ead92011-01-11 09:42:13 -0800313{
John Stultzdae373b2012-09-13 19:12:16 -0400314 struct alarm *alarm = container_of(timer, struct alarm, timer);
315 struct alarm_base *base = &alarm_bases[alarm->type];
John Stultzff3ead92011-01-11 09:42:13 -0800316 unsigned long flags;
John Stultz7068b7a2011-04-28 13:29:18 -0700317 int ret = HRTIMER_NORESTART;
John Stultz54da23b2011-08-10 11:08:07 -0700318 int restart = ALARMTIMER_NORESTART;
John Stultzff3ead92011-01-11 09:42:13 -0800319
320 spin_lock_irqsave(&base->lock, flags);
John Stultza65bcc12012-09-13 19:25:22 -0400321 alarmtimer_dequeue(base, alarm);
John Stultzdae373b2012-09-13 19:12:16 -0400322 spin_unlock_irqrestore(&base->lock, flags);
John Stultzff3ead92011-01-11 09:42:13 -0800323
John Stultzdae373b2012-09-13 19:12:16 -0400324 if (alarm->function)
325 restart = alarm->function(alarm, base->gettime());
John Stultzff3ead92011-01-11 09:42:13 -0800326
John Stultzdae373b2012-09-13 19:12:16 -0400327 spin_lock_irqsave(&base->lock, flags);
328 if (restart != ALARMTIMER_NORESTART) {
329 hrtimer_set_expires(&alarm->timer, alarm->node.expires);
330 alarmtimer_enqueue(base, alarm);
John Stultz7068b7a2011-04-28 13:29:18 -0700331 ret = HRTIMER_RESTART;
John Stultzff3ead92011-01-11 09:42:13 -0800332 }
333 spin_unlock_irqrestore(&base->lock, flags);
John Stultzff3ead92011-01-11 09:42:13 -0800334
Mao Jinlong2e1a4ae2016-02-17 15:21:49 +0800335 /* set next power off alarm */
336 if (alarm->type == ALARM_POWEROFF_REALTIME)
337 queue_delayed_work(power_off_alarm_workqueue, &work, 0);
338
John Stultz7068b7a2011-04-28 13:29:18 -0700339 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800340
John Stultzff3ead92011-01-11 09:42:13 -0800341}
342
Todd Poynor6cffe002013-05-15 14:38:11 -0700343ktime_t alarm_expires_remaining(const struct alarm *alarm)
344{
345 struct alarm_base *base = &alarm_bases[alarm->type];
346 return ktime_sub(alarm->node.expires, base->gettime());
347}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200348EXPORT_SYMBOL_GPL(alarm_expires_remaining);
Todd Poynor6cffe002013-05-15 14:38:11 -0700349
John Stultz472647d2011-04-29 15:03:10 -0700350#ifdef CONFIG_RTC_CLASS
John Stultz180bf812011-04-28 12:58:11 -0700351/**
John Stultzff3ead92011-01-11 09:42:13 -0800352 * alarmtimer_suspend - Suspend time callback
353 * @dev: unused
354 * @state: unused
355 *
356 * When we are going into suspend, we look through the bases
357 * to see which is the soonest timer to expire. We then
358 * set an rtc timer to fire that far into the future, which
359 * will wake us from suspend.
360 */
361static int alarmtimer_suspend(struct device *dev)
362{
363 struct rtc_time tm;
364 ktime_t min, now;
365 unsigned long flags;
John Stultzc008ba582011-06-16 18:27:09 -0700366 struct rtc_device *rtc;
John Stultzff3ead92011-01-11 09:42:13 -0800367 int i;
Todd Poynor59a93c22012-08-09 00:37:27 -0700368 int ret;
John Stultzff3ead92011-01-11 09:42:13 -0800369
Mao Jinlong2e1a4ae2016-02-17 15:21:49 +0800370 cancel_delayed_work_sync(&work);
371
John Stultzff3ead92011-01-11 09:42:13 -0800372 spin_lock_irqsave(&freezer_delta_lock, flags);
373 min = freezer_delta;
374 freezer_delta = ktime_set(0, 0);
375 spin_unlock_irqrestore(&freezer_delta_lock, flags);
376
John Stultz8bc0daf2011-07-14 18:35:13 -0700377 rtc = alarmtimer_get_rtcdev();
John Stultzff3ead92011-01-11 09:42:13 -0800378 /* If we have no rtcdev, just return */
John Stultzc008ba582011-06-16 18:27:09 -0700379 if (!rtc)
John Stultzff3ead92011-01-11 09:42:13 -0800380 return 0;
381
382 /* Find the soonest timer to expire*/
383 for (i = 0; i < ALARM_NUMTYPE; i++) {
384 struct alarm_base *base = &alarm_bases[i];
385 struct timerqueue_node *next;
386 ktime_t delta;
387
388 spin_lock_irqsave(&base->lock, flags);
389 next = timerqueue_getnext(&base->timerqueue);
390 spin_unlock_irqrestore(&base->lock, flags);
391 if (!next)
392 continue;
393 delta = ktime_sub(next->expires, base->gettime());
394 if (!min.tv64 || (delta.tv64 < min.tv64))
395 min = delta;
396 }
397 if (min.tv64 == 0)
398 return 0;
399
Todd Poynor59a93c22012-08-09 00:37:27 -0700400 if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) {
401 __pm_wakeup_event(ws, 2 * MSEC_PER_SEC);
402 return -EBUSY;
403 }
John Stultzff3ead92011-01-11 09:42:13 -0800404
405 /* Setup an rtc timer to fire that far in the future */
John Stultzc008ba582011-06-16 18:27:09 -0700406 rtc_timer_cancel(rtc, &rtctimer);
407 rtc_read_time(rtc, &tm);
John Stultzff3ead92011-01-11 09:42:13 -0800408 now = rtc_tm_to_ktime(tm);
409 now = ktime_add(now, min);
410
Todd Poynor59a93c22012-08-09 00:37:27 -0700411 /* Set alarm, if in the past reject suspend briefly to handle */
412 ret = rtc_timer_start(rtc, &rtctimer, now, ktime_set(0, 0));
413 if (ret < 0)
414 __pm_wakeup_event(ws, MSEC_PER_SEC);
415 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800416}
zhuo-haoa0e32132015-11-17 20:08:07 +0800417
418static int alarmtimer_resume(struct device *dev)
419{
420 struct rtc_device *rtc;
421
422 rtc = alarmtimer_get_rtcdev();
423 if (rtc)
424 rtc_timer_cancel(rtc, &rtctimer);
Mao Jinlong2e1a4ae2016-02-17 15:21:49 +0800425
426 queue_delayed_work(power_off_alarm_workqueue, &work, 0);
zhuo-haoa0e32132015-11-17 20:08:07 +0800427 return 0;
428}
429
John Stultz472647d2011-04-29 15:03:10 -0700430#else
431static int alarmtimer_suspend(struct device *dev)
432{
433 return 0;
434}
zhuo-haoa0e32132015-11-17 20:08:07 +0800435
436static int alarmtimer_resume(struct device *dev)
437{
438 return 0;
439}
John Stultz472647d2011-04-29 15:03:10 -0700440#endif
John Stultzff3ead92011-01-11 09:42:13 -0800441
John Stultz9a7adcf2011-01-11 09:54:33 -0800442static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
443{
444 ktime_t delta;
445 unsigned long flags;
446 struct alarm_base *base = &alarm_bases[type];
447
448 delta = ktime_sub(absexp, base->gettime());
449
450 spin_lock_irqsave(&freezer_delta_lock, flags);
451 if (!freezer_delta.tv64 || (delta.tv64 < freezer_delta.tv64))
452 freezer_delta = delta;
453 spin_unlock_irqrestore(&freezer_delta_lock, flags);
454}
455
456
John Stultz180bf812011-04-28 12:58:11 -0700457/**
John Stultzff3ead92011-01-11 09:42:13 -0800458 * alarm_init - Initialize an alarm structure
459 * @alarm: ptr to alarm to be initialized
460 * @type: the type of the alarm
461 * @function: callback that is run when the alarm fires
John Stultzff3ead92011-01-11 09:42:13 -0800462 */
463void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
John Stultz4b413082011-08-10 10:37:59 -0700464 enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
John Stultzff3ead92011-01-11 09:42:13 -0800465{
466 timerqueue_init(&alarm->node);
John Stultzdae373b2012-09-13 19:12:16 -0400467 hrtimer_init(&alarm->timer, alarm_bases[type].base_clockid,
468 HRTIMER_MODE_ABS);
469 alarm->timer.function = alarmtimer_fired;
John Stultzff3ead92011-01-11 09:42:13 -0800470 alarm->function = function;
471 alarm->type = type;
John Stultza28cde82011-08-10 12:30:21 -0700472 alarm->state = ALARMTIMER_STATE_INACTIVE;
John Stultzff3ead92011-01-11 09:42:13 -0800473}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200474EXPORT_SYMBOL_GPL(alarm_init);
John Stultzff3ead92011-01-11 09:42:13 -0800475
John Stultz180bf812011-04-28 12:58:11 -0700476/**
Todd Poynor6cffe002013-05-15 14:38:11 -0700477 * alarm_start - Sets an absolute alarm to fire
John Stultzff3ead92011-01-11 09:42:13 -0800478 * @alarm: ptr to alarm to set
479 * @start: time to run the alarm
John Stultzff3ead92011-01-11 09:42:13 -0800480 */
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000481void alarm_start(struct alarm *alarm, ktime_t start)
John Stultzff3ead92011-01-11 09:42:13 -0800482{
483 struct alarm_base *base = &alarm_bases[alarm->type];
484 unsigned long flags;
485
486 spin_lock_irqsave(&base->lock, flags);
John Stultzff3ead92011-01-11 09:42:13 -0800487 alarm->node.expires = start;
John Stultzff3ead92011-01-11 09:42:13 -0800488 alarmtimer_enqueue(base, alarm);
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000489 hrtimer_start(&alarm->timer, alarm->node.expires, HRTIMER_MODE_ABS);
John Stultzff3ead92011-01-11 09:42:13 -0800490 spin_unlock_irqrestore(&base->lock, flags);
491}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200492EXPORT_SYMBOL_GPL(alarm_start);
John Stultzff3ead92011-01-11 09:42:13 -0800493
John Stultz180bf812011-04-28 12:58:11 -0700494/**
Todd Poynor6cffe002013-05-15 14:38:11 -0700495 * alarm_start_relative - Sets a relative alarm to fire
496 * @alarm: ptr to alarm to set
497 * @start: time relative to now to run the alarm
498 */
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000499void alarm_start_relative(struct alarm *alarm, ktime_t start)
Todd Poynor6cffe002013-05-15 14:38:11 -0700500{
501 struct alarm_base *base = &alarm_bases[alarm->type];
502
503 start = ktime_add(start, base->gettime());
Thomas Gleixnerb1932172015-04-14 21:09:18 +0000504 alarm_start(alarm, start);
Todd Poynor6cffe002013-05-15 14:38:11 -0700505}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200506EXPORT_SYMBOL_GPL(alarm_start_relative);
Todd Poynor6cffe002013-05-15 14:38:11 -0700507
508void alarm_restart(struct alarm *alarm)
509{
510 struct alarm_base *base = &alarm_bases[alarm->type];
511 unsigned long flags;
512
513 spin_lock_irqsave(&base->lock, flags);
514 hrtimer_set_expires(&alarm->timer, alarm->node.expires);
515 hrtimer_restart(&alarm->timer);
516 alarmtimer_enqueue(base, alarm);
517 spin_unlock_irqrestore(&base->lock, flags);
518}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200519EXPORT_SYMBOL_GPL(alarm_restart);
Todd Poynor6cffe002013-05-15 14:38:11 -0700520
521/**
John Stultz9082c462011-08-10 12:41:36 -0700522 * alarm_try_to_cancel - Tries to cancel an alarm timer
John Stultzff3ead92011-01-11 09:42:13 -0800523 * @alarm: ptr to alarm to be canceled
John Stultz9082c462011-08-10 12:41:36 -0700524 *
525 * Returns 1 if the timer was canceled, 0 if it was not running,
526 * and -1 if the callback was running
John Stultzff3ead92011-01-11 09:42:13 -0800527 */
John Stultz9082c462011-08-10 12:41:36 -0700528int alarm_try_to_cancel(struct alarm *alarm)
John Stultzff3ead92011-01-11 09:42:13 -0800529{
530 struct alarm_base *base = &alarm_bases[alarm->type];
531 unsigned long flags;
John Stultzdae373b2012-09-13 19:12:16 -0400532 int ret;
533
John Stultzff3ead92011-01-11 09:42:13 -0800534 spin_lock_irqsave(&base->lock, flags);
John Stultzdae373b2012-09-13 19:12:16 -0400535 ret = hrtimer_try_to_cancel(&alarm->timer);
536 if (ret >= 0)
John Stultza65bcc12012-09-13 19:25:22 -0400537 alarmtimer_dequeue(base, alarm);
John Stultzff3ead92011-01-11 09:42:13 -0800538 spin_unlock_irqrestore(&base->lock, flags);
John Stultz9082c462011-08-10 12:41:36 -0700539 return ret;
John Stultzff3ead92011-01-11 09:42:13 -0800540}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200541EXPORT_SYMBOL_GPL(alarm_try_to_cancel);
John Stultzff3ead92011-01-11 09:42:13 -0800542
543
John Stultz9082c462011-08-10 12:41:36 -0700544/**
545 * alarm_cancel - Spins trying to cancel an alarm timer until it is done
546 * @alarm: ptr to alarm to be canceled
547 *
548 * Returns 1 if the timer was canceled, 0 if it was not active.
549 */
550int alarm_cancel(struct alarm *alarm)
551{
552 for (;;) {
553 int ret = alarm_try_to_cancel(alarm);
554 if (ret >= 0)
555 return ret;
556 cpu_relax();
557 }
558}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200559EXPORT_SYMBOL_GPL(alarm_cancel);
John Stultz9082c462011-08-10 12:41:36 -0700560
John Stultzdce75a82011-08-10 11:31:03 -0700561
562u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval)
563{
564 u64 overrun = 1;
565 ktime_t delta;
566
567 delta = ktime_sub(now, alarm->node.expires);
568
569 if (delta.tv64 < 0)
570 return 0;
571
572 if (unlikely(delta.tv64 >= interval.tv64)) {
573 s64 incr = ktime_to_ns(interval);
574
575 overrun = ktime_divns(delta, incr);
576
577 alarm->node.expires = ktime_add_ns(alarm->node.expires,
578 incr*overrun);
579
580 if (alarm->node.expires.tv64 > now.tv64)
581 return overrun;
582 /*
583 * This (and the ktime_add() below) is the
584 * correction for exact:
585 */
586 overrun++;
587 }
588
589 alarm->node.expires = ktime_add(alarm->node.expires, interval);
590 return overrun;
591}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200592EXPORT_SYMBOL_GPL(alarm_forward);
John Stultzdce75a82011-08-10 11:31:03 -0700593
Todd Poynor6cffe002013-05-15 14:38:11 -0700594u64 alarm_forward_now(struct alarm *alarm, ktime_t interval)
595{
596 struct alarm_base *base = &alarm_bases[alarm->type];
597
598 return alarm_forward(alarm, base->gettime(), interval);
599}
Marcus Gelderie11682a42013-06-04 09:32:09 +0200600EXPORT_SYMBOL_GPL(alarm_forward_now);
John Stultzdce75a82011-08-10 11:31:03 -0700601
602
John Stultz180bf812011-04-28 12:58:11 -0700603/**
John Stultz9a7adcf2011-01-11 09:54:33 -0800604 * clock2alarm - helper that converts from clockid to alarmtypes
605 * @clockid: clockid.
John Stultz9a7adcf2011-01-11 09:54:33 -0800606 */
Mao Jinlong2e1a4ae2016-02-17 15:21:49 +0800607enum alarmtimer_type clock2alarm(clockid_t clockid)
John Stultz9a7adcf2011-01-11 09:54:33 -0800608{
609 if (clockid == CLOCK_REALTIME_ALARM)
610 return ALARM_REALTIME;
611 if (clockid == CLOCK_BOOTTIME_ALARM)
612 return ALARM_BOOTTIME;
Mao Jinlong2e1a4ae2016-02-17 15:21:49 +0800613 if (clockid == CLOCK_POWEROFF_ALARM)
614 return ALARM_POWEROFF_REALTIME;
John Stultz9a7adcf2011-01-11 09:54:33 -0800615 return -1;
616}
617
John Stultz180bf812011-04-28 12:58:11 -0700618/**
John Stultz9a7adcf2011-01-11 09:54:33 -0800619 * alarm_handle_timer - Callback for posix timers
620 * @alarm: alarm that fired
621 *
622 * Posix timer callback for expired alarm timers.
623 */
John Stultz4b413082011-08-10 10:37:59 -0700624static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
625 ktime_t now)
John Stultz9a7adcf2011-01-11 09:54:33 -0800626{
Richard Larocque474e9412014-09-09 18:31:05 -0700627 unsigned long flags;
John Stultz9a7adcf2011-01-11 09:54:33 -0800628 struct k_itimer *ptr = container_of(alarm, struct k_itimer,
John Stultz9e264762011-08-10 12:09:24 -0700629 it.alarm.alarmtimer);
Richard Larocque474e9412014-09-09 18:31:05 -0700630 enum alarmtimer_restart result = ALARMTIMER_NORESTART;
631
632 spin_lock_irqsave(&ptr->it_lock, flags);
Richard Larocque265b81d2014-09-09 18:31:04 -0700633 if ((ptr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) {
634 if (posix_timer_event(ptr, 0) != 0)
635 ptr->it_overrun++;
636 }
John Stultz4b413082011-08-10 10:37:59 -0700637
John Stultz54da23b2011-08-10 11:08:07 -0700638 /* Re-add periodic timers */
John Stultz9e264762011-08-10 12:09:24 -0700639 if (ptr->it.alarm.interval.tv64) {
640 ptr->it_overrun += alarm_forward(alarm, now,
641 ptr->it.alarm.interval);
Richard Larocque474e9412014-09-09 18:31:05 -0700642 result = ALARMTIMER_RESTART;
John Stultz54da23b2011-08-10 11:08:07 -0700643 }
Richard Larocque474e9412014-09-09 18:31:05 -0700644 spin_unlock_irqrestore(&ptr->it_lock, flags);
645
646 return result;
John Stultz9a7adcf2011-01-11 09:54:33 -0800647}
648
John Stultz180bf812011-04-28 12:58:11 -0700649/**
John Stultz9a7adcf2011-01-11 09:54:33 -0800650 * alarm_clock_getres - posix getres interface
651 * @which_clock: clockid
652 * @tp: timespec to fill
653 *
654 * Returns the granularity of underlying alarm base clock
655 */
656static int alarm_clock_getres(const clockid_t which_clock, struct timespec *tp)
657{
John Stultz1c6b39a2011-06-16 18:47:37 -0700658 if (!alarmtimer_get_rtcdev())
KOSAKI Motohiro98d6f4d2013-10-14 17:33:16 -0400659 return -EINVAL;
John Stultz1c6b39a2011-06-16 18:47:37 -0700660
Thomas Gleixner056a3ca2015-04-14 21:08:32 +0000661 tp->tv_sec = 0;
662 tp->tv_nsec = hrtimer_resolution;
663 return 0;
John Stultz9a7adcf2011-01-11 09:54:33 -0800664}
665
666/**
667 * alarm_clock_get - posix clock_get interface
668 * @which_clock: clockid
669 * @tp: timespec to fill.
670 *
671 * Provides the underlying alarm base time.
672 */
673static int alarm_clock_get(clockid_t which_clock, struct timespec *tp)
674{
675 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
676
John Stultz1c6b39a2011-06-16 18:47:37 -0700677 if (!alarmtimer_get_rtcdev())
KOSAKI Motohiro98d6f4d2013-10-14 17:33:16 -0400678 return -EINVAL;
John Stultz1c6b39a2011-06-16 18:47:37 -0700679
John Stultz9a7adcf2011-01-11 09:54:33 -0800680 *tp = ktime_to_timespec(base->gettime());
681 return 0;
682}
683
684/**
685 * alarm_timer_create - posix timer_create interface
686 * @new_timer: k_itimer pointer to manage
687 *
688 * Initializes the k_itimer structure.
689 */
690static int alarm_timer_create(struct k_itimer *new_timer)
691{
692 enum alarmtimer_type type;
John Stultz9a7adcf2011-01-11 09:54:33 -0800693
John Stultz1c6b39a2011-06-16 18:47:37 -0700694 if (!alarmtimer_get_rtcdev())
695 return -ENOTSUPP;
696
John Stultz9a7adcf2011-01-11 09:54:33 -0800697 if (!capable(CAP_WAKE_ALARM))
698 return -EPERM;
699
700 type = clock2alarm(new_timer->it_clock);
John Stultz9e264762011-08-10 12:09:24 -0700701 alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
John Stultz9a7adcf2011-01-11 09:54:33 -0800702 return 0;
703}
704
705/**
706 * alarm_timer_get - posix timer_get interface
707 * @new_timer: k_itimer pointer
708 * @cur_setting: itimerspec data to fill
709 *
Richard Larocquee86fea72014-09-09 18:31:03 -0700710 * Copies out the current itimerspec data
John Stultz9a7adcf2011-01-11 09:54:33 -0800711 */
712static void alarm_timer_get(struct k_itimer *timr,
713 struct itimerspec *cur_setting)
714{
Richard Larocquee86fea72014-09-09 18:31:03 -0700715 ktime_t relative_expiry_time =
716 alarm_expires_remaining(&(timr->it.alarm.alarmtimer));
John Stultzea7802f2011-08-04 07:51:56 -0700717
Richard Larocquee86fea72014-09-09 18:31:03 -0700718 if (ktime_to_ns(relative_expiry_time) > 0) {
719 cur_setting->it_value = ktime_to_timespec(relative_expiry_time);
720 } else {
721 cur_setting->it_value.tv_sec = 0;
722 cur_setting->it_value.tv_nsec = 0;
723 }
724
725 cur_setting->it_interval = ktime_to_timespec(timr->it.alarm.interval);
John Stultz9a7adcf2011-01-11 09:54:33 -0800726}
727
728/**
729 * alarm_timer_del - posix timer_del interface
730 * @timr: k_itimer pointer to be deleted
731 *
732 * Cancels any programmed alarms for the given timer.
733 */
734static int alarm_timer_del(struct k_itimer *timr)
735{
John Stultz1c6b39a2011-06-16 18:47:37 -0700736 if (!rtcdev)
737 return -ENOTSUPP;
738
John Stultz9082c462011-08-10 12:41:36 -0700739 if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
740 return TIMER_RETRY;
741
John Stultz9a7adcf2011-01-11 09:54:33 -0800742 return 0;
743}
744
745/**
746 * alarm_timer_set - posix timer_set interface
747 * @timr: k_itimer pointer to be deleted
748 * @flags: timer flags
749 * @new_setting: itimerspec to be used
750 * @old_setting: itimerspec being replaced
751 *
752 * Sets the timer to new_setting, and starts the timer.
753 */
754static int alarm_timer_set(struct k_itimer *timr, int flags,
755 struct itimerspec *new_setting,
756 struct itimerspec *old_setting)
757{
John Stultz16927772014-07-07 14:06:11 -0700758 ktime_t exp;
759
John Stultz1c6b39a2011-06-16 18:47:37 -0700760 if (!rtcdev)
761 return -ENOTSUPP;
762
John Stultz16927772014-07-07 14:06:11 -0700763 if (flags & ~TIMER_ABSTIME)
764 return -EINVAL;
765
John Stultz971c90b2011-08-04 07:25:35 -0700766 if (old_setting)
767 alarm_timer_get(timr, old_setting);
John Stultz9a7adcf2011-01-11 09:54:33 -0800768
769 /* If the timer was already set, cancel it */
John Stultz9082c462011-08-10 12:41:36 -0700770 if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
771 return TIMER_RETRY;
John Stultz9a7adcf2011-01-11 09:54:33 -0800772
773 /* start the timer */
John Stultz9e264762011-08-10 12:09:24 -0700774 timr->it.alarm.interval = timespec_to_ktime(new_setting->it_interval);
John Stultz16927772014-07-07 14:06:11 -0700775 exp = timespec_to_ktime(new_setting->it_value);
776 /* Convert (if necessary) to absolute time */
777 if (flags != TIMER_ABSTIME) {
778 ktime_t now;
779
780 now = alarm_bases[timr->it.alarm.alarmtimer.type].gettime();
781 exp = ktime_add(now, exp);
782 }
783
784 alarm_start(&timr->it.alarm.alarmtimer, exp);
John Stultz9a7adcf2011-01-11 09:54:33 -0800785 return 0;
786}
787
788/**
789 * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
790 * @alarm: ptr to alarm that fired
791 *
792 * Wakes up the task that set the alarmtimer
793 */
John Stultz4b413082011-08-10 10:37:59 -0700794static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
795 ktime_t now)
John Stultz9a7adcf2011-01-11 09:54:33 -0800796{
797 struct task_struct *task = (struct task_struct *)alarm->data;
798
799 alarm->data = NULL;
800 if (task)
801 wake_up_process(task);
John Stultz4b413082011-08-10 10:37:59 -0700802 return ALARMTIMER_NORESTART;
John Stultz9a7adcf2011-01-11 09:54:33 -0800803}
804
805/**
806 * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
807 * @alarm: ptr to alarmtimer
808 * @absexp: absolute expiration time
809 *
810 * Sets the alarm timer and sleeps until it is fired or interrupted.
811 */
812static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp)
813{
814 alarm->data = (void *)current;
815 do {
816 set_current_state(TASK_INTERRUPTIBLE);
John Stultz9e264762011-08-10 12:09:24 -0700817 alarm_start(alarm, absexp);
John Stultz9a7adcf2011-01-11 09:54:33 -0800818 if (likely(alarm->data))
819 schedule();
820
821 alarm_cancel(alarm);
822 } while (alarm->data && !signal_pending(current));
823
824 __set_current_state(TASK_RUNNING);
825
826 return (alarm->data == NULL);
827}
828
829
830/**
831 * update_rmtp - Update remaining timespec value
832 * @exp: expiration time
833 * @type: timer type
834 * @rmtp: user pointer to remaining timepsec value
835 *
836 * Helper function that fills in rmtp value with time between
837 * now and the exp value
838 */
839static int update_rmtp(ktime_t exp, enum alarmtimer_type type,
840 struct timespec __user *rmtp)
841{
842 struct timespec rmt;
843 ktime_t rem;
844
845 rem = ktime_sub(exp, alarm_bases[type].gettime());
846
847 if (rem.tv64 <= 0)
848 return 0;
849 rmt = ktime_to_timespec(rem);
850
851 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
852 return -EFAULT;
853
854 return 1;
855
856}
857
858/**
859 * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
860 * @restart: ptr to restart block
861 *
862 * Handles restarted clock_nanosleep calls
863 */
864static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
865{
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200866 enum alarmtimer_type type = restart->nanosleep.clockid;
John Stultz9a7adcf2011-01-11 09:54:33 -0800867 ktime_t exp;
868 struct timespec __user *rmtp;
869 struct alarm alarm;
870 int ret = 0;
871
872 exp.tv64 = restart->nanosleep.expires;
873 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
874
875 if (alarmtimer_do_nsleep(&alarm, exp))
876 goto out;
877
878 if (freezing(current))
879 alarmtimer_freezerset(exp, type);
880
881 rmtp = restart->nanosleep.rmtp;
882 if (rmtp) {
883 ret = update_rmtp(exp, type, rmtp);
884 if (ret <= 0)
885 goto out;
886 }
887
888
889 /* The other values in restart are already filled in */
890 ret = -ERESTART_RESTARTBLOCK;
891out:
892 return ret;
893}
894
895/**
896 * alarm_timer_nsleep - alarmtimer nanosleep
897 * @which_clock: clockid
898 * @flags: determins abstime or relative
899 * @tsreq: requested sleep time (abs or rel)
900 * @rmtp: remaining sleep time saved
901 *
902 * Handles clock_nanosleep calls against _ALARM clockids
903 */
904static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
905 struct timespec *tsreq, struct timespec __user *rmtp)
906{
907 enum alarmtimer_type type = clock2alarm(which_clock);
908 struct alarm alarm;
909 ktime_t exp;
910 int ret = 0;
911 struct restart_block *restart;
912
John Stultz1c6b39a2011-06-16 18:47:37 -0700913 if (!alarmtimer_get_rtcdev())
914 return -ENOTSUPP;
915
John Stultz16927772014-07-07 14:06:11 -0700916 if (flags & ~TIMER_ABSTIME)
917 return -EINVAL;
918
John Stultz9a7adcf2011-01-11 09:54:33 -0800919 if (!capable(CAP_WAKE_ALARM))
920 return -EPERM;
921
922 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
923
924 exp = timespec_to_ktime(*tsreq);
925 /* Convert (if necessary) to absolute time */
926 if (flags != TIMER_ABSTIME) {
927 ktime_t now = alarm_bases[type].gettime();
928 exp = ktime_add(now, exp);
929 }
930
931 if (alarmtimer_do_nsleep(&alarm, exp))
932 goto out;
933
934 if (freezing(current))
935 alarmtimer_freezerset(exp, type);
936
937 /* abs timers don't set remaining time or restart */
938 if (flags == TIMER_ABSTIME) {
939 ret = -ERESTARTNOHAND;
940 goto out;
941 }
942
943 if (rmtp) {
944 ret = update_rmtp(exp, type, rmtp);
945 if (ret <= 0)
946 goto out;
947 }
948
Andy Lutomirskif56141e2015-02-12 15:01:14 -0800949 restart = &current->restart_block;
John Stultz9a7adcf2011-01-11 09:54:33 -0800950 restart->fn = alarm_timer_nsleep_restart;
Thomas Gleixnerab8177b2011-05-20 13:05:15 +0200951 restart->nanosleep.clockid = type;
John Stultz9a7adcf2011-01-11 09:54:33 -0800952 restart->nanosleep.expires = exp.tv64;
953 restart->nanosleep.rmtp = rmtp;
954 ret = -ERESTART_RESTARTBLOCK;
955
956out:
957 return ret;
958}
John Stultzff3ead92011-01-11 09:42:13 -0800959
John Stultzff3ead92011-01-11 09:42:13 -0800960
961/* Suspend hook structures */
962static const struct dev_pm_ops alarmtimer_pm_ops = {
963 .suspend = alarmtimer_suspend,
zhuo-haoa0e32132015-11-17 20:08:07 +0800964 .resume = alarmtimer_resume,
John Stultzff3ead92011-01-11 09:42:13 -0800965};
966
967static struct platform_driver alarmtimer_driver = {
968 .driver = {
969 .name = "alarmtimer",
970 .pm = &alarmtimer_pm_ops,
971 }
972};
973
974/**
975 * alarmtimer_init - Initialize alarm timer code
976 *
977 * This function initializes the alarm bases and registers
978 * the posix clock ids.
979 */
980static int __init alarmtimer_init(void)
981{
Thomas Gleixner4523f6a2011-09-14 10:54:29 +0200982 struct platform_device *pdev;
John Stultzff3ead92011-01-11 09:42:13 -0800983 int error = 0;
984 int i;
John Stultz9a7adcf2011-01-11 09:54:33 -0800985 struct k_clock alarm_clock = {
986 .clock_getres = alarm_clock_getres,
987 .clock_get = alarm_clock_get,
988 .timer_create = alarm_timer_create,
989 .timer_set = alarm_timer_set,
990 .timer_del = alarm_timer_del,
991 .timer_get = alarm_timer_get,
992 .nsleep = alarm_timer_nsleep,
993 };
994
Thomas Gleixnerc5e14e72012-03-24 12:46:23 +0100995 alarmtimer_rtc_timer_init();
John Stultzad30dfa2012-03-23 15:52:25 -0700996
John Stultz9a7adcf2011-01-11 09:54:33 -0800997 posix_timers_register_clock(CLOCK_REALTIME_ALARM, &alarm_clock);
998 posix_timers_register_clock(CLOCK_BOOTTIME_ALARM, &alarm_clock);
Mao Jinlong2e1a4ae2016-02-17 15:21:49 +0800999 posix_timers_register_clock(CLOCK_POWEROFF_ALARM, &alarm_clock);
John Stultzff3ead92011-01-11 09:42:13 -08001000
1001 /* Initialize alarm bases */
1002 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
1003 alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
Mao Jinlong2e1a4ae2016-02-17 15:21:49 +08001004 alarm_bases[ALARM_POWEROFF_REALTIME].base_clockid = CLOCK_REALTIME;
1005 alarm_bases[ALARM_POWEROFF_REALTIME].gettime = &ktime_get_real;
John Stultzff3ead92011-01-11 09:42:13 -08001006 alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
1007 alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
1008 for (i = 0; i < ALARM_NUMTYPE; i++) {
1009 timerqueue_init_head(&alarm_bases[i].timerqueue);
1010 spin_lock_init(&alarm_bases[i].lock);
John Stultzff3ead92011-01-11 09:42:13 -08001011 }
John Stultz8bc0daf2011-07-14 18:35:13 -07001012
Thomas Gleixner4523f6a2011-09-14 10:54:29 +02001013 error = alarmtimer_rtc_interface_setup();
1014 if (error)
1015 return error;
John Stultzff3ead92011-01-11 09:42:13 -08001016
Thomas Gleixner4523f6a2011-09-14 10:54:29 +02001017 error = platform_driver_register(&alarmtimer_driver);
1018 if (error)
1019 goto out_if;
1020
1021 pdev = platform_device_register_simple("alarmtimer", -1, NULL, 0);
1022 if (IS_ERR(pdev)) {
1023 error = PTR_ERR(pdev);
1024 goto out_drv;
1025 }
Todd Poynor59a93c22012-08-09 00:37:27 -07001026 ws = wakeup_source_register("alarmtimer");
Mao Jinlong2e1a4ae2016-02-17 15:21:49 +08001027 if (!ws) {
1028 error = -ENOMEM;
1029 goto out_ws;
1030 }
Thomas Gleixner4523f6a2011-09-14 10:54:29 +02001031
Mao Jinlong2e1a4ae2016-02-17 15:21:49 +08001032 INIT_DELAYED_WORK(&work, alarm_work_func);
1033 power_off_alarm_workqueue =
1034 create_singlethread_workqueue("power_off_alarm");
1035 if (!power_off_alarm_workqueue) {
1036 error = -ENOMEM;
1037 goto out_wq;
1038 }
1039
1040 return 0;
1041out_wq:
1042 wakeup_source_unregister(ws);
1043out_ws:
1044 platform_device_unregister(pdev);
Thomas Gleixner4523f6a2011-09-14 10:54:29 +02001045out_drv:
1046 platform_driver_unregister(&alarmtimer_driver);
1047out_if:
1048 alarmtimer_rtc_interface_remove();
John Stultzff3ead92011-01-11 09:42:13 -08001049 return error;
1050}
1051device_initcall(alarmtimer_init);