blob: 9340af7dc86e3a88132ae83ae54bd0213961185f [file] [log] [blame]
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -07001/* drivers/rtc/alarm.c
2 *
Arve Hjønnevågfa565402009-05-04 14:09:15 -07003 * Copyright (C) 2007-2009 Google, Inc.
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -07004 *
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 */
15
Steve Mucklef132c6c2012-06-06 18:30:57 -070016#include <linux/module.h>
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -070017#include <linux/android_alarm.h>
18#include <linux/device.h>
19#include <linux/miscdevice.h>
20#include <linux/platform_device.h>
21#include <linux/rtc.h>
22#include <linux/sched.h>
23#include <linux/spinlock.h>
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -070024#include <linux/wakelock.h>
25
Steve Mucklef132c6c2012-06-06 18:30:57 -070026#include <asm/mach/time.h>
27
Arve Hjønnevågfa565402009-05-04 14:09:15 -070028#define ANDROID_ALARM_PRINT_ERROR (1U << 0)
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -070029#define ANDROID_ALARM_PRINT_INIT_STATUS (1U << 1)
Arve Hjønnevågfa565402009-05-04 14:09:15 -070030#define ANDROID_ALARM_PRINT_TSET (1U << 2)
31#define ANDROID_ALARM_PRINT_CALL (1U << 3)
32#define ANDROID_ALARM_PRINT_SUSPEND (1U << 4)
33#define ANDROID_ALARM_PRINT_INT (1U << 5)
34#define ANDROID_ALARM_PRINT_FLOW (1U << 6)
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -070035
Arve Hjønnevågfa565402009-05-04 14:09:15 -070036static int debug_mask = ANDROID_ALARM_PRINT_ERROR | \
37 ANDROID_ALARM_PRINT_INIT_STATUS;
38module_param_named(debug_mask, debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP);
39
40#define pr_alarm(debug_level_mask, args...) \
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -070041 do { \
Arve Hjønnevågfa565402009-05-04 14:09:15 -070042 if (debug_mask & ANDROID_ALARM_PRINT_##debug_level_mask) { \
43 pr_info(args); \
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -070044 } \
45 } while (0)
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -070046
47#define ANDROID_ALARM_WAKEUP_MASK ( \
48 ANDROID_ALARM_RTC_WAKEUP_MASK | \
49 ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP_MASK)
50
51/* support old usespace code */
52#define ANDROID_ALARM_SET_OLD _IOW('a', 2, time_t) /* set alarm */
53#define ANDROID_ALARM_SET_AND_WAIT_OLD _IOW('a', 3, time_t)
54
Arve Hjønnevågfa565402009-05-04 14:09:15 -070055struct alarm_queue {
56 struct rb_root alarms;
57 struct rb_node *first;
58 struct hrtimer timer;
59 ktime_t delta;
60 bool stopped;
61 ktime_t stopped_time;
62};
63
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -070064static struct rtc_device *alarm_rtc_dev;
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -070065static DEFINE_SPINLOCK(alarm_slock);
66static DEFINE_MUTEX(alarm_setrtc_mutex);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -070067static struct wake_lock alarm_rtc_wake_lock;
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -070068static struct platform_device *alarm_platform_dev;
Arve Hjønnevågfa565402009-05-04 14:09:15 -070069struct alarm_queue alarms[ANDROID_ALARM_TYPE_COUNT];
70static bool suspended;
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -070071
Arve Hjønnevågfa565402009-05-04 14:09:15 -070072static void update_timer_locked(struct alarm_queue *base, bool head_removed)
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -070073{
Arve Hjønnevågfa565402009-05-04 14:09:15 -070074 struct alarm *alarm;
75 bool is_wakeup = base == &alarms[ANDROID_ALARM_RTC_WAKEUP] ||
76 base == &alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP];
77
78 if (base->stopped) {
79 pr_alarm(FLOW, "changed alarm while setting the wall time\n");
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -070080 return;
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -070081 }
82
Arve Hjønnevågfa565402009-05-04 14:09:15 -070083 if (is_wakeup && !suspended && head_removed)
84 wake_unlock(&alarm_rtc_wake_lock);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -070085
Arve Hjønnevågfa565402009-05-04 14:09:15 -070086 if (!base->first)
87 return;
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -070088
Arve Hjønnevågfa565402009-05-04 14:09:15 -070089 alarm = container_of(base->first, struct alarm, node);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -070090
Arve Hjønnevågfa565402009-05-04 14:09:15 -070091 pr_alarm(FLOW, "selected alarm, type %d, func %pF at %lld\n",
92 alarm->type, alarm->function, ktime_to_ns(alarm->expires));
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -070093
Arve Hjønnevågfa565402009-05-04 14:09:15 -070094 if (is_wakeup && suspended) {
95 pr_alarm(FLOW, "changed alarm while suspened\n");
96 wake_lock_timeout(&alarm_rtc_wake_lock, 1 * HZ);
97 return;
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -070098 }
Arve Hjønnevågfa565402009-05-04 14:09:15 -070099
100 hrtimer_try_to_cancel(&base->timer);
101 base->timer.node.expires = ktime_add(base->delta, alarm->expires);
102 base->timer._softexpires = ktime_add(base->delta, alarm->softexpires);
103 hrtimer_start_expires(&base->timer, HRTIMER_MODE_ABS);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700104}
105
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700106static void alarm_enqueue_locked(struct alarm *alarm)
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700107{
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700108 struct alarm_queue *base = &alarms[alarm->type];
109 struct rb_node **link = &base->alarms.rb_node;
110 struct rb_node *parent = NULL;
111 struct alarm *entry;
112 int leftmost = 1;
Arve Hjønnevåg2cd24682011-01-07 19:00:01 -0800113 bool was_first = false;
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700114
115 pr_alarm(FLOW, "added alarm, type %d, func %pF at %lld\n",
116 alarm->type, alarm->function, ktime_to_ns(alarm->expires));
117
Arve Hjønnevåg2cd24682011-01-07 19:00:01 -0800118 if (base->first == &alarm->node) {
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700119 base->first = rb_next(&alarm->node);
Arve Hjønnevåg2cd24682011-01-07 19:00:01 -0800120 was_first = true;
121 }
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700122 if (!RB_EMPTY_NODE(&alarm->node)) {
123 rb_erase(&alarm->node, &base->alarms);
124 RB_CLEAR_NODE(&alarm->node);
125 }
126
127 while (*link) {
128 parent = *link;
129 entry = rb_entry(parent, struct alarm, node);
130 /*
131 * We dont care about collisions. Nodes with
132 * the same expiry time stay together.
133 */
134 if (alarm->expires.tv64 < entry->expires.tv64) {
135 link = &(*link)->rb_left;
136 } else {
137 link = &(*link)->rb_right;
138 leftmost = 0;
139 }
140 }
Arve Hjønnevåg2cd24682011-01-07 19:00:01 -0800141 if (leftmost)
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700142 base->first = &alarm->node;
Arve Hjønnevåg2cd24682011-01-07 19:00:01 -0800143 if (leftmost || was_first)
144 update_timer_locked(base, was_first);
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700145
146 rb_link_node(&alarm->node, parent, link);
147 rb_insert_color(&alarm->node, &base->alarms);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700148}
149
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700150/**
151 * alarm_init - initialize an alarm
152 * @alarm: the alarm to be initialized
153 * @type: the alarm type to be used
154 * @function: alarm callback function
155 */
156void alarm_init(struct alarm *alarm,
157 enum android_alarm_type type, void (*function)(struct alarm *))
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700158{
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700159 RB_CLEAR_NODE(&alarm->node);
160 alarm->type = type;
161 alarm->function = function;
162
163 pr_alarm(FLOW, "created alarm, type %d, func %pF\n", type, function);
164}
165
166
167/**
168 * alarm_start_range - (re)start an alarm
169 * @alarm: the alarm to be added
170 * @start: earliest expiry time
171 * @end: expiry time
172 */
173void alarm_start_range(struct alarm *alarm, ktime_t start, ktime_t end)
174{
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700175 unsigned long flags;
176
177 spin_lock_irqsave(&alarm_slock, flags);
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700178 alarm->softexpires = start;
179 alarm->expires = end;
180 alarm_enqueue_locked(alarm);
181 spin_unlock_irqrestore(&alarm_slock, flags);
182}
183
184/**
185 * alarm_try_to_cancel - try to deactivate an alarm
186 * @alarm: alarm to stop
187 *
188 * Returns:
189 * 0 when the alarm was not active
190 * 1 when the alarm was active
191 * -1 when the alarm may currently be excuting the callback function and
192 * cannot be stopped (it may also be inactive)
193 */
194int alarm_try_to_cancel(struct alarm *alarm)
195{
196 struct alarm_queue *base = &alarms[alarm->type];
197 unsigned long flags;
198 bool first = false;
199 int ret = 0;
200
201 spin_lock_irqsave(&alarm_slock, flags);
202 if (!RB_EMPTY_NODE(&alarm->node)) {
203 pr_alarm(FLOW, "canceled alarm, type %d, func %pF at %lld\n",
204 alarm->type, alarm->function,
205 ktime_to_ns(alarm->expires));
206 ret = 1;
207 if (base->first == &alarm->node) {
208 base->first = rb_next(&alarm->node);
209 first = true;
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700210 }
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700211 rb_erase(&alarm->node, &base->alarms);
212 RB_CLEAR_NODE(&alarm->node);
213 if (first)
214 update_timer_locked(base, true);
215 } else
216 pr_alarm(FLOW, "tried to cancel alarm, type %d, func %pF\n",
217 alarm->type, alarm->function);
218 spin_unlock_irqrestore(&alarm_slock, flags);
219 if (!ret && hrtimer_callback_running(&base->timer))
220 ret = -1;
221 return ret;
222}
223
224/**
225 * alarm_cancel - cancel an alarm and wait for the handler to finish.
226 * @alarm: the alarm to be cancelled
227 *
228 * Returns:
229 * 0 when the alarm was not active
230 * 1 when the alarm was active
231 */
232int alarm_cancel(struct alarm *alarm)
233{
234 for (;;) {
235 int ret = alarm_try_to_cancel(alarm);
236 if (ret >= 0)
237 return ret;
238 cpu_relax();
239 }
240}
241
242/**
243 * alarm_set_rtc - set the kernel and rtc walltime
244 * @new_time: timespec value containing the new time
245 */
246int alarm_set_rtc(struct timespec new_time)
247{
248 int i;
249 int ret;
250 unsigned long flags;
251 struct rtc_time rtc_new_rtc_time;
252 struct timespec tmp_time;
253
254 rtc_time_to_tm(new_time.tv_sec, &rtc_new_rtc_time);
255
256 pr_alarm(TSET, "set rtc %ld %ld - rtc %02d:%02d:%02d %02d/%02d/%04d\n",
257 new_time.tv_sec, new_time.tv_nsec,
258 rtc_new_rtc_time.tm_hour, rtc_new_rtc_time.tm_min,
259 rtc_new_rtc_time.tm_sec, rtc_new_rtc_time.tm_mon + 1,
260 rtc_new_rtc_time.tm_mday,
261 rtc_new_rtc_time.tm_year + 1900);
262
263 mutex_lock(&alarm_setrtc_mutex);
264 spin_lock_irqsave(&alarm_slock, flags);
265 wake_lock(&alarm_rtc_wake_lock);
266 getnstimeofday(&tmp_time);
267 for (i = 0; i < ANDROID_ALARM_SYSTEMTIME; i++) {
268 hrtimer_try_to_cancel(&alarms[i].timer);
269 alarms[i].stopped = true;
270 alarms[i].stopped_time = timespec_to_ktime(tmp_time);
271 }
272 alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP].delta =
273 alarms[ANDROID_ALARM_ELAPSED_REALTIME].delta =
274 ktime_sub(alarms[ANDROID_ALARM_ELAPSED_REALTIME].delta,
275 timespec_to_ktime(timespec_sub(tmp_time, new_time)));
276 spin_unlock_irqrestore(&alarm_slock, flags);
277 ret = do_settimeofday(&new_time);
278 spin_lock_irqsave(&alarm_slock, flags);
279 for (i = 0; i < ANDROID_ALARM_SYSTEMTIME; i++) {
280 alarms[i].stopped = false;
281 update_timer_locked(&alarms[i], false);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700282 }
283 spin_unlock_irqrestore(&alarm_slock, flags);
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700284 if (ret < 0) {
285 pr_alarm(ERROR, "alarm_set_rtc: Failed to set time\n");
286 goto err;
287 }
288 if (!alarm_rtc_dev) {
289 pr_alarm(ERROR,
290 "alarm_set_rtc: no RTC, time will be lost on reboot\n");
291 goto err;
292 }
293 ret = rtc_set_time(alarm_rtc_dev, &rtc_new_rtc_time);
294 if (ret < 0)
295 pr_alarm(ERROR, "alarm_set_rtc: "
296 "Failed to set RTC, time will be lost on reboot\n");
297err:
298 wake_unlock(&alarm_rtc_wake_lock);
299 mutex_unlock(&alarm_setrtc_mutex);
300 return ret;
301}
302
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700303
304void
305alarm_update_timedelta(struct timespec tmp_time, struct timespec new_time)
306{
307 int i;
308 unsigned long flags;
309
310 spin_lock_irqsave(&alarm_slock, flags);
311 for (i = 0; i < ANDROID_ALARM_SYSTEMTIME; i++) {
312 hrtimer_try_to_cancel(&alarms[i].timer);
313 alarms[i].stopped = true;
314 alarms[i].stopped_time = timespec_to_ktime(tmp_time);
315 }
316 alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP].delta =
317 alarms[ANDROID_ALARM_ELAPSED_REALTIME].delta =
318 ktime_sub(alarms[ANDROID_ALARM_ELAPSED_REALTIME].delta,
319 timespec_to_ktime(timespec_sub(tmp_time, new_time)));
320 for (i = 0; i < ANDROID_ALARM_SYSTEMTIME; i++) {
321 alarms[i].stopped = false;
322 update_timer_locked(&alarms[i], false);
323 }
324 spin_unlock_irqrestore(&alarm_slock, flags);
325}
326
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700327/**
328 * alarm_get_elapsed_realtime - get the elapsed real time in ktime_t format
329 *
330 * returns the time in ktime_t format
331 */
332ktime_t alarm_get_elapsed_realtime(void)
333{
334 ktime_t now;
335 unsigned long flags;
336 struct alarm_queue *base = &alarms[ANDROID_ALARM_ELAPSED_REALTIME];
337
338 spin_lock_irqsave(&alarm_slock, flags);
339 now = base->stopped ? base->stopped_time : ktime_get_real();
340 now = ktime_sub(now, base->delta);
341 spin_unlock_irqrestore(&alarm_slock, flags);
342 return now;
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700343}
344
345static enum hrtimer_restart alarm_timer_triggered(struct hrtimer *timer)
346{
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700347 struct alarm_queue *base;
348 struct alarm *alarm;
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700349 unsigned long flags;
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700350 ktime_t now;
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700351
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700352 spin_lock_irqsave(&alarm_slock, flags);
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700353
354 base = container_of(timer, struct alarm_queue, timer);
355 now = base->stopped ? base->stopped_time : hrtimer_cb_get_time(timer);
356 now = ktime_sub(now, base->delta);
357
358 pr_alarm(INT, "alarm_timer_triggered type %d at %lld\n",
359 base - alarms, ktime_to_ns(now));
360
361 while (base->first) {
362 alarm = container_of(base->first, struct alarm, node);
363 if (alarm->softexpires.tv64 > now.tv64) {
364 pr_alarm(FLOW, "don't call alarm, %pF, %lld (s %lld)\n",
365 alarm->function, ktime_to_ns(alarm->expires),
366 ktime_to_ns(alarm->softexpires));
367 break;
368 }
369 base->first = rb_next(&alarm->node);
370 rb_erase(&alarm->node, &base->alarms);
371 RB_CLEAR_NODE(&alarm->node);
372 pr_alarm(CALL, "call alarm, type %d, func %pF, %lld (s %lld)\n",
373 alarm->type, alarm->function,
374 ktime_to_ns(alarm->expires),
375 ktime_to_ns(alarm->softexpires));
376 spin_unlock_irqrestore(&alarm_slock, flags);
377 alarm->function(alarm);
378 spin_lock_irqsave(&alarm_slock, flags);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700379 }
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700380 if (!base->first)
381 pr_alarm(FLOW, "no more alarms of type %d\n", base - alarms);
382 update_timer_locked(base, true);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700383 spin_unlock_irqrestore(&alarm_slock, flags);
384 return HRTIMER_NORESTART;
385}
386
387static void alarm_triggered_func(void *p)
388{
389 struct rtc_device *rtc = alarm_rtc_dev;
390 if (!(rtc->irq_data & RTC_AF))
391 return;
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700392 pr_alarm(INT, "rtc alarm triggered\n");
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700393 wake_lock_timeout(&alarm_rtc_wake_lock, 1 * HZ);
394}
395
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700396static int alarm_suspend(struct platform_device *pdev, pm_message_t state)
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700397{
398 int err = 0;
399 unsigned long flags;
400 struct rtc_wkalrm rtc_alarm;
401 struct rtc_time rtc_current_rtc_time;
402 unsigned long rtc_current_time;
403 unsigned long rtc_alarm_time;
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700404 struct timespec rtc_delta;
Arve Hjønnevågb2a4ab22010-03-12 20:05:32 -0800405 struct timespec wall_time;
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700406 struct alarm_queue *wakeup_queue = NULL;
407 struct alarm_queue *tmp_queue = NULL;
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700408
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700409 pr_alarm(SUSPEND, "alarm_suspend(%p, %d)\n", pdev, state.event);
410
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700411 spin_lock_irqsave(&alarm_slock, flags);
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700412 suspended = true;
413 spin_unlock_irqrestore(&alarm_slock, flags);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700414
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700415 hrtimer_cancel(&alarms[ANDROID_ALARM_RTC_WAKEUP].timer);
416 hrtimer_cancel(&alarms[
JP Abgrall692e4682011-09-02 18:14:12 -0700417 ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP].timer);
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700418
419 tmp_queue = &alarms[ANDROID_ALARM_RTC_WAKEUP];
420 if (tmp_queue->first)
421 wakeup_queue = tmp_queue;
422 tmp_queue = &alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP];
423 if (tmp_queue->first && (!wakeup_queue ||
424 hrtimer_get_expires(&tmp_queue->timer).tv64 <
425 hrtimer_get_expires(&wakeup_queue->timer).tv64))
426 wakeup_queue = tmp_queue;
427 if (wakeup_queue) {
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700428 rtc_read_time(alarm_rtc_dev, &rtc_current_rtc_time);
Arve Hjønnevågb2a4ab22010-03-12 20:05:32 -0800429 getnstimeofday(&wall_time);
430 rtc_tm_to_time(&rtc_current_rtc_time, &rtc_current_time);
431 set_normalized_timespec(&rtc_delta,
432 wall_time.tv_sec - rtc_current_time,
433 wall_time.tv_nsec);
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700434
435 rtc_alarm_time = timespec_sub(ktime_to_timespec(
436 hrtimer_get_expires(&wakeup_queue->timer)),
437 rtc_delta).tv_sec;
438
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700439 rtc_time_to_tm(rtc_alarm_time, &rtc_alarm.time);
440 rtc_alarm.enabled = 1;
441 rtc_set_alarm(alarm_rtc_dev, &rtc_alarm);
442 rtc_read_time(alarm_rtc_dev, &rtc_current_rtc_time);
443 rtc_tm_to_time(&rtc_current_rtc_time, &rtc_current_time);
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700444 pr_alarm(SUSPEND,
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700445 "rtc alarm set at %ld, now %ld, rtc delta %ld.%09ld\n",
446 rtc_alarm_time, rtc_current_time,
447 rtc_delta.tv_sec, rtc_delta.tv_nsec);
448 if (rtc_current_time + 1 >= rtc_alarm_time) {
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700449 pr_alarm(SUSPEND, "alarm about to go off\n");
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700450 memset(&rtc_alarm, 0, sizeof(rtc_alarm));
451 rtc_alarm.enabled = 0;
452 rtc_set_alarm(alarm_rtc_dev, &rtc_alarm);
453
454 spin_lock_irqsave(&alarm_slock, flags);
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700455 suspended = false;
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700456 wake_lock_timeout(&alarm_rtc_wake_lock, 2 * HZ);
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700457 update_timer_locked(&alarms[ANDROID_ALARM_RTC_WAKEUP],
458 false);
459 update_timer_locked(&alarms[
460 ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP], false);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700461 err = -EBUSY;
462 spin_unlock_irqrestore(&alarm_slock, flags);
463 }
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700464 }
465 return err;
466}
467
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700468static int alarm_resume(struct platform_device *pdev)
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700469{
470 struct rtc_wkalrm alarm;
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700471 unsigned long flags;
472
473 pr_alarm(SUSPEND, "alarm_resume(%p)\n", pdev);
474
475 memset(&alarm, 0, sizeof(alarm));
476 alarm.enabled = 0;
477 rtc_set_alarm(alarm_rtc_dev, &alarm);
478
479 spin_lock_irqsave(&alarm_slock, flags);
480 suspended = false;
481 update_timer_locked(&alarms[ANDROID_ALARM_RTC_WAKEUP], false);
482 update_timer_locked(&alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP],
483 false);
484 spin_unlock_irqrestore(&alarm_slock, flags);
485
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700486 return 0;
487}
488
489static struct rtc_task alarm_rtc_task = {
490 .func = alarm_triggered_func
491};
492
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700493static int rtc_alarm_add_device(struct device *dev,
494 struct class_interface *class_intf)
495{
496 int err;
497 struct rtc_device *rtc = to_rtc_device(dev);
498
499 mutex_lock(&alarm_setrtc_mutex);
500
501 if (alarm_rtc_dev) {
502 err = -EBUSY;
503 goto err1;
504 }
505
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700506 alarm_platform_dev =
507 platform_device_register_simple("alarm", -1, NULL, 0);
508 if (IS_ERR(alarm_platform_dev)) {
509 err = PTR_ERR(alarm_platform_dev);
510 goto err2;
511 }
512 err = rtc_irq_register(rtc, &alarm_rtc_task);
513 if (err)
514 goto err3;
515 alarm_rtc_dev = rtc;
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700516 pr_alarm(INIT_STATUS, "using rtc device, %s, for alarms", rtc->name);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700517 mutex_unlock(&alarm_setrtc_mutex);
518
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700519 return 0;
520
521err3:
522 platform_device_unregister(alarm_platform_dev);
523err2:
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700524err1:
525 mutex_unlock(&alarm_setrtc_mutex);
526 return err;
527}
528
529static void rtc_alarm_remove_device(struct device *dev,
530 struct class_interface *class_intf)
531{
532 if (dev == &alarm_rtc_dev->dev) {
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700533 pr_alarm(INIT_STATUS, "lost rtc device for alarms");
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700534 rtc_irq_unregister(alarm_rtc_dev, &alarm_rtc_task);
535 platform_device_unregister(alarm_platform_dev);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700536 alarm_rtc_dev = NULL;
537 }
538}
539
540static struct class_interface rtc_alarm_interface = {
541 .add_dev = &rtc_alarm_add_device,
542 .remove_dev = &rtc_alarm_remove_device,
543};
544
545static struct platform_driver alarm_driver = {
546 .suspend = alarm_suspend,
547 .resume = alarm_resume,
548 .driver = {
549 .name = "alarm"
550 }
551};
552
553static int __init alarm_late_init(void)
554{
555 unsigned long flags;
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700556 struct timespec tmp_time, system_time;
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700557
558 /* this needs to run after the rtc is read at boot */
559 spin_lock_irqsave(&alarm_slock, flags);
560 /* We read the current rtc and system time so we can later calulate
561 * elasped realtime to be (boot_systemtime + rtc - boot_rtc) ==
562 * (rtc - (boot_rtc - boot_systemtime))
563 */
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700564 getnstimeofday(&tmp_time);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700565 ktime_get_ts(&system_time);
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700566 alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP].delta =
567 alarms[ANDROID_ALARM_ELAPSED_REALTIME].delta =
568 timespec_to_ktime(timespec_sub(tmp_time, system_time));
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700569
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700570 spin_unlock_irqrestore(&alarm_slock, flags);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700571 return 0;
572}
573
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700574static int __init alarm_driver_init(void)
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700575{
576 int err;
577 int i;
578
579 for (i = 0; i < ANDROID_ALARM_SYSTEMTIME; i++) {
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700580 hrtimer_init(&alarms[i].timer,
581 CLOCK_REALTIME, HRTIMER_MODE_ABS);
582 alarms[i].timer.function = alarm_timer_triggered;
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700583 }
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700584 hrtimer_init(&alarms[ANDROID_ALARM_SYSTEMTIME].timer,
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700585 CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700586 alarms[ANDROID_ALARM_SYSTEMTIME].timer.function = alarm_timer_triggered;
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700587 err = platform_driver_register(&alarm_driver);
588 if (err < 0)
589 goto err1;
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700590 wake_lock_init(&alarm_rtc_wake_lock, WAKE_LOCK_SUSPEND, "alarm_rtc");
591 rtc_alarm_interface.class = rtc_class;
592 err = class_interface_register(&rtc_alarm_interface);
593 if (err < 0)
594 goto err2;
595
596 return 0;
597
598err2:
599 wake_lock_destroy(&alarm_rtc_wake_lock);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700600 platform_driver_unregister(&alarm_driver);
601err1:
602 return err;
603}
604
605static void __exit alarm_exit(void)
606{
607 class_interface_unregister(&rtc_alarm_interface);
608 wake_lock_destroy(&alarm_rtc_wake_lock);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700609 platform_driver_unregister(&alarm_driver);
610}
611
612late_initcall(alarm_late_init);
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700613module_init(alarm_driver_init);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700614module_exit(alarm_exit);
615