blob: e318ecf7f0f5081b4b624975a44d2fbc3160f68e [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;
Ashay Jaiswalc1b2e472013-05-17 15:15:02 +053071static long power_on_alarm;
72
73void set_power_on_alarm(long secs)
74{
75 power_on_alarm = secs;
76}
77
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -070078
Arve Hjønnevågfa565402009-05-04 14:09:15 -070079static void update_timer_locked(struct alarm_queue *base, bool head_removed)
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -070080{
Arve Hjønnevågfa565402009-05-04 14:09:15 -070081 struct alarm *alarm;
82 bool is_wakeup = base == &alarms[ANDROID_ALARM_RTC_WAKEUP] ||
83 base == &alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP];
84
85 if (base->stopped) {
86 pr_alarm(FLOW, "changed alarm while setting the wall time\n");
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -070087 return;
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -070088 }
89
Arve Hjønnevågfa565402009-05-04 14:09:15 -070090 if (is_wakeup && !suspended && head_removed)
91 wake_unlock(&alarm_rtc_wake_lock);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -070092
Arve Hjønnevågfa565402009-05-04 14:09:15 -070093 if (!base->first)
94 return;
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -070095
Arve Hjønnevågfa565402009-05-04 14:09:15 -070096 alarm = container_of(base->first, struct alarm, node);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -070097
Arve Hjønnevågfa565402009-05-04 14:09:15 -070098 pr_alarm(FLOW, "selected alarm, type %d, func %pF at %lld\n",
99 alarm->type, alarm->function, ktime_to_ns(alarm->expires));
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700100
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700101 if (is_wakeup && suspended) {
102 pr_alarm(FLOW, "changed alarm while suspened\n");
103 wake_lock_timeout(&alarm_rtc_wake_lock, 1 * HZ);
104 return;
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700105 }
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700106
107 hrtimer_try_to_cancel(&base->timer);
108 base->timer.node.expires = ktime_add(base->delta, alarm->expires);
109 base->timer._softexpires = ktime_add(base->delta, alarm->softexpires);
110 hrtimer_start_expires(&base->timer, HRTIMER_MODE_ABS);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700111}
112
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700113static void alarm_enqueue_locked(struct alarm *alarm)
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700114{
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700115 struct alarm_queue *base = &alarms[alarm->type];
116 struct rb_node **link = &base->alarms.rb_node;
117 struct rb_node *parent = NULL;
118 struct alarm *entry;
119 int leftmost = 1;
Arve Hjønnevåg2cd24682011-01-07 19:00:01 -0800120 bool was_first = false;
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700121
122 pr_alarm(FLOW, "added alarm, type %d, func %pF at %lld\n",
123 alarm->type, alarm->function, ktime_to_ns(alarm->expires));
124
Arve Hjønnevåg2cd24682011-01-07 19:00:01 -0800125 if (base->first == &alarm->node) {
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700126 base->first = rb_next(&alarm->node);
Arve Hjønnevåg2cd24682011-01-07 19:00:01 -0800127 was_first = true;
128 }
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700129 if (!RB_EMPTY_NODE(&alarm->node)) {
130 rb_erase(&alarm->node, &base->alarms);
131 RB_CLEAR_NODE(&alarm->node);
132 }
133
134 while (*link) {
135 parent = *link;
136 entry = rb_entry(parent, struct alarm, node);
137 /*
138 * We dont care about collisions. Nodes with
139 * the same expiry time stay together.
140 */
141 if (alarm->expires.tv64 < entry->expires.tv64) {
142 link = &(*link)->rb_left;
143 } else {
144 link = &(*link)->rb_right;
145 leftmost = 0;
146 }
147 }
Arve Hjønnevåg2cd24682011-01-07 19:00:01 -0800148 if (leftmost)
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700149 base->first = &alarm->node;
Arve Hjønnevåg2cd24682011-01-07 19:00:01 -0800150 if (leftmost || was_first)
151 update_timer_locked(base, was_first);
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700152
153 rb_link_node(&alarm->node, parent, link);
154 rb_insert_color(&alarm->node, &base->alarms);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700155}
156
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700157/**
158 * alarm_init - initialize an alarm
159 * @alarm: the alarm to be initialized
160 * @type: the alarm type to be used
161 * @function: alarm callback function
162 */
163void alarm_init(struct alarm *alarm,
164 enum android_alarm_type type, void (*function)(struct alarm *))
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700165{
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700166 RB_CLEAR_NODE(&alarm->node);
167 alarm->type = type;
168 alarm->function = function;
169
170 pr_alarm(FLOW, "created alarm, type %d, func %pF\n", type, function);
171}
172
173
174/**
175 * alarm_start_range - (re)start an alarm
176 * @alarm: the alarm to be added
177 * @start: earliest expiry time
178 * @end: expiry time
179 */
180void alarm_start_range(struct alarm *alarm, ktime_t start, ktime_t end)
181{
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700182 unsigned long flags;
183
184 spin_lock_irqsave(&alarm_slock, flags);
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700185 alarm->softexpires = start;
186 alarm->expires = end;
187 alarm_enqueue_locked(alarm);
188 spin_unlock_irqrestore(&alarm_slock, flags);
189}
190
191/**
192 * alarm_try_to_cancel - try to deactivate an alarm
193 * @alarm: alarm to stop
194 *
195 * Returns:
196 * 0 when the alarm was not active
197 * 1 when the alarm was active
198 * -1 when the alarm may currently be excuting the callback function and
199 * cannot be stopped (it may also be inactive)
200 */
201int alarm_try_to_cancel(struct alarm *alarm)
202{
203 struct alarm_queue *base = &alarms[alarm->type];
204 unsigned long flags;
205 bool first = false;
206 int ret = 0;
207
208 spin_lock_irqsave(&alarm_slock, flags);
209 if (!RB_EMPTY_NODE(&alarm->node)) {
210 pr_alarm(FLOW, "canceled alarm, type %d, func %pF at %lld\n",
211 alarm->type, alarm->function,
212 ktime_to_ns(alarm->expires));
213 ret = 1;
214 if (base->first == &alarm->node) {
215 base->first = rb_next(&alarm->node);
216 first = true;
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700217 }
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700218 rb_erase(&alarm->node, &base->alarms);
219 RB_CLEAR_NODE(&alarm->node);
220 if (first)
221 update_timer_locked(base, true);
222 } else
223 pr_alarm(FLOW, "tried to cancel alarm, type %d, func %pF\n",
224 alarm->type, alarm->function);
225 spin_unlock_irqrestore(&alarm_slock, flags);
226 if (!ret && hrtimer_callback_running(&base->timer))
227 ret = -1;
228 return ret;
229}
230
231/**
232 * alarm_cancel - cancel an alarm and wait for the handler to finish.
233 * @alarm: the alarm to be cancelled
234 *
235 * Returns:
236 * 0 when the alarm was not active
237 * 1 when the alarm was active
238 */
239int alarm_cancel(struct alarm *alarm)
240{
241 for (;;) {
242 int ret = alarm_try_to_cancel(alarm);
243 if (ret >= 0)
244 return ret;
245 cpu_relax();
246 }
247}
248
249/**
250 * alarm_set_rtc - set the kernel and rtc walltime
251 * @new_time: timespec value containing the new time
252 */
253int alarm_set_rtc(struct timespec new_time)
254{
255 int i;
256 int ret;
257 unsigned long flags;
258 struct rtc_time rtc_new_rtc_time;
259 struct timespec tmp_time;
260
261 rtc_time_to_tm(new_time.tv_sec, &rtc_new_rtc_time);
262
263 pr_alarm(TSET, "set rtc %ld %ld - rtc %02d:%02d:%02d %02d/%02d/%04d\n",
264 new_time.tv_sec, new_time.tv_nsec,
265 rtc_new_rtc_time.tm_hour, rtc_new_rtc_time.tm_min,
266 rtc_new_rtc_time.tm_sec, rtc_new_rtc_time.tm_mon + 1,
267 rtc_new_rtc_time.tm_mday,
268 rtc_new_rtc_time.tm_year + 1900);
269
270 mutex_lock(&alarm_setrtc_mutex);
271 spin_lock_irqsave(&alarm_slock, flags);
272 wake_lock(&alarm_rtc_wake_lock);
273 getnstimeofday(&tmp_time);
274 for (i = 0; i < ANDROID_ALARM_SYSTEMTIME; i++) {
275 hrtimer_try_to_cancel(&alarms[i].timer);
276 alarms[i].stopped = true;
277 alarms[i].stopped_time = timespec_to_ktime(tmp_time);
278 }
279 alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP].delta =
280 alarms[ANDROID_ALARM_ELAPSED_REALTIME].delta =
281 ktime_sub(alarms[ANDROID_ALARM_ELAPSED_REALTIME].delta,
282 timespec_to_ktime(timespec_sub(tmp_time, new_time)));
283 spin_unlock_irqrestore(&alarm_slock, flags);
284 ret = do_settimeofday(&new_time);
285 spin_lock_irqsave(&alarm_slock, flags);
286 for (i = 0; i < ANDROID_ALARM_SYSTEMTIME; i++) {
287 alarms[i].stopped = false;
288 update_timer_locked(&alarms[i], false);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700289 }
290 spin_unlock_irqrestore(&alarm_slock, flags);
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700291 if (ret < 0) {
292 pr_alarm(ERROR, "alarm_set_rtc: Failed to set time\n");
293 goto err;
294 }
295 if (!alarm_rtc_dev) {
296 pr_alarm(ERROR,
297 "alarm_set_rtc: no RTC, time will be lost on reboot\n");
298 goto err;
299 }
300 ret = rtc_set_time(alarm_rtc_dev, &rtc_new_rtc_time);
301 if (ret < 0)
302 pr_alarm(ERROR, "alarm_set_rtc: "
303 "Failed to set RTC, time will be lost on reboot\n");
304err:
305 wake_unlock(&alarm_rtc_wake_lock);
306 mutex_unlock(&alarm_setrtc_mutex);
307 return ret;
308}
309
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700310
311void
312alarm_update_timedelta(struct timespec tmp_time, struct timespec new_time)
313{
314 int i;
315 unsigned long flags;
316
317 spin_lock_irqsave(&alarm_slock, flags);
318 for (i = 0; i < ANDROID_ALARM_SYSTEMTIME; i++) {
319 hrtimer_try_to_cancel(&alarms[i].timer);
320 alarms[i].stopped = true;
321 alarms[i].stopped_time = timespec_to_ktime(tmp_time);
322 }
323 alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP].delta =
324 alarms[ANDROID_ALARM_ELAPSED_REALTIME].delta =
325 ktime_sub(alarms[ANDROID_ALARM_ELAPSED_REALTIME].delta,
326 timespec_to_ktime(timespec_sub(tmp_time, new_time)));
327 for (i = 0; i < ANDROID_ALARM_SYSTEMTIME; i++) {
328 alarms[i].stopped = false;
329 update_timer_locked(&alarms[i], false);
330 }
331 spin_unlock_irqrestore(&alarm_slock, flags);
332}
333
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700334/**
335 * alarm_get_elapsed_realtime - get the elapsed real time in ktime_t format
336 *
337 * returns the time in ktime_t format
338 */
339ktime_t alarm_get_elapsed_realtime(void)
340{
341 ktime_t now;
342 unsigned long flags;
343 struct alarm_queue *base = &alarms[ANDROID_ALARM_ELAPSED_REALTIME];
344
345 spin_lock_irqsave(&alarm_slock, flags);
346 now = base->stopped ? base->stopped_time : ktime_get_real();
347 now = ktime_sub(now, base->delta);
348 spin_unlock_irqrestore(&alarm_slock, flags);
349 return now;
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700350}
351
352static enum hrtimer_restart alarm_timer_triggered(struct hrtimer *timer)
353{
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700354 struct alarm_queue *base;
355 struct alarm *alarm;
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700356 unsigned long flags;
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700357 ktime_t now;
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700358
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700359 spin_lock_irqsave(&alarm_slock, flags);
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700360
361 base = container_of(timer, struct alarm_queue, timer);
362 now = base->stopped ? base->stopped_time : hrtimer_cb_get_time(timer);
363 now = ktime_sub(now, base->delta);
364
365 pr_alarm(INT, "alarm_timer_triggered type %d at %lld\n",
366 base - alarms, ktime_to_ns(now));
367
368 while (base->first) {
369 alarm = container_of(base->first, struct alarm, node);
370 if (alarm->softexpires.tv64 > now.tv64) {
371 pr_alarm(FLOW, "don't call alarm, %pF, %lld (s %lld)\n",
372 alarm->function, ktime_to_ns(alarm->expires),
373 ktime_to_ns(alarm->softexpires));
374 break;
375 }
376 base->first = rb_next(&alarm->node);
377 rb_erase(&alarm->node, &base->alarms);
378 RB_CLEAR_NODE(&alarm->node);
379 pr_alarm(CALL, "call alarm, type %d, func %pF, %lld (s %lld)\n",
380 alarm->type, alarm->function,
381 ktime_to_ns(alarm->expires),
382 ktime_to_ns(alarm->softexpires));
383 spin_unlock_irqrestore(&alarm_slock, flags);
384 alarm->function(alarm);
385 spin_lock_irqsave(&alarm_slock, flags);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700386 }
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700387 if (!base->first)
388 pr_alarm(FLOW, "no more alarms of type %d\n", base - alarms);
389 update_timer_locked(base, true);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700390 spin_unlock_irqrestore(&alarm_slock, flags);
391 return HRTIMER_NORESTART;
392}
393
394static void alarm_triggered_func(void *p)
395{
396 struct rtc_device *rtc = alarm_rtc_dev;
397 if (!(rtc->irq_data & RTC_AF))
398 return;
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700399 pr_alarm(INT, "rtc alarm triggered\n");
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700400 wake_lock_timeout(&alarm_rtc_wake_lock, 1 * HZ);
401}
402
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700403static int alarm_suspend(struct platform_device *pdev, pm_message_t state)
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700404{
405 int err = 0;
406 unsigned long flags;
407 struct rtc_wkalrm rtc_alarm;
408 struct rtc_time rtc_current_rtc_time;
409 unsigned long rtc_current_time;
410 unsigned long rtc_alarm_time;
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700411 struct timespec rtc_delta;
Arve Hjønnevågb2a4ab22010-03-12 20:05:32 -0800412 struct timespec wall_time;
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700413 struct alarm_queue *wakeup_queue = NULL;
414 struct alarm_queue *tmp_queue = NULL;
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700415
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700416 pr_alarm(SUSPEND, "alarm_suspend(%p, %d)\n", pdev, state.event);
417
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700418 spin_lock_irqsave(&alarm_slock, flags);
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700419 suspended = true;
420 spin_unlock_irqrestore(&alarm_slock, flags);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700421
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700422 hrtimer_cancel(&alarms[ANDROID_ALARM_RTC_WAKEUP].timer);
423 hrtimer_cancel(&alarms[
JP Abgrall692e4682011-09-02 18:14:12 -0700424 ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP].timer);
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700425
426 tmp_queue = &alarms[ANDROID_ALARM_RTC_WAKEUP];
427 if (tmp_queue->first)
428 wakeup_queue = tmp_queue;
429 tmp_queue = &alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP];
430 if (tmp_queue->first && (!wakeup_queue ||
431 hrtimer_get_expires(&tmp_queue->timer).tv64 <
432 hrtimer_get_expires(&wakeup_queue->timer).tv64))
433 wakeup_queue = tmp_queue;
434 if (wakeup_queue) {
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700435 rtc_read_time(alarm_rtc_dev, &rtc_current_rtc_time);
Arve Hjønnevågb2a4ab22010-03-12 20:05:32 -0800436 getnstimeofday(&wall_time);
437 rtc_tm_to_time(&rtc_current_rtc_time, &rtc_current_time);
438 set_normalized_timespec(&rtc_delta,
439 wall_time.tv_sec - rtc_current_time,
440 wall_time.tv_nsec);
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700441
442 rtc_alarm_time = timespec_sub(ktime_to_timespec(
443 hrtimer_get_expires(&wakeup_queue->timer)),
444 rtc_delta).tv_sec;
445
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700446 rtc_time_to_tm(rtc_alarm_time, &rtc_alarm.time);
447 rtc_alarm.enabled = 1;
448 rtc_set_alarm(alarm_rtc_dev, &rtc_alarm);
449 rtc_read_time(alarm_rtc_dev, &rtc_current_rtc_time);
450 rtc_tm_to_time(&rtc_current_rtc_time, &rtc_current_time);
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700451 pr_alarm(SUSPEND,
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700452 "rtc alarm set at %ld, now %ld, rtc delta %ld.%09ld\n",
453 rtc_alarm_time, rtc_current_time,
454 rtc_delta.tv_sec, rtc_delta.tv_nsec);
455 if (rtc_current_time + 1 >= rtc_alarm_time) {
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700456 pr_alarm(SUSPEND, "alarm about to go off\n");
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700457 memset(&rtc_alarm, 0, sizeof(rtc_alarm));
458 rtc_alarm.enabled = 0;
459 rtc_set_alarm(alarm_rtc_dev, &rtc_alarm);
460
461 spin_lock_irqsave(&alarm_slock, flags);
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700462 suspended = false;
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700463 wake_lock_timeout(&alarm_rtc_wake_lock, 2 * HZ);
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700464 update_timer_locked(&alarms[ANDROID_ALARM_RTC_WAKEUP],
465 false);
466 update_timer_locked(&alarms[
467 ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP], false);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700468 err = -EBUSY;
469 spin_unlock_irqrestore(&alarm_slock, flags);
470 }
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700471 }
472 return err;
473}
474
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700475static int alarm_resume(struct platform_device *pdev)
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700476{
477 struct rtc_wkalrm alarm;
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700478 unsigned long flags;
479
480 pr_alarm(SUSPEND, "alarm_resume(%p)\n", pdev);
481
482 memset(&alarm, 0, sizeof(alarm));
483 alarm.enabled = 0;
484 rtc_set_alarm(alarm_rtc_dev, &alarm);
485
486 spin_lock_irqsave(&alarm_slock, flags);
487 suspended = false;
488 update_timer_locked(&alarms[ANDROID_ALARM_RTC_WAKEUP], false);
489 update_timer_locked(&alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP],
490 false);
491 spin_unlock_irqrestore(&alarm_slock, flags);
492
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700493 return 0;
494}
495
Ashay Jaiswalc1b2e472013-05-17 15:15:02 +0530496static void alarm_shutdown(struct platform_device *dev)
497{
498 struct timespec wall_time;
499 struct rtc_time rtc_time;
500 struct rtc_wkalrm alarm;
501 unsigned long flags;
502 long rtc_secs, alarm_delta, alarm_time;
503 int rc;
504
505 spin_lock_irqsave(&alarm_slock, flags);
506
507 if (!power_on_alarm)
508 goto disable_alarm;
509
510 rtc_read_time(alarm_rtc_dev, &rtc_time);
511 getnstimeofday(&wall_time);
512 rtc_tm_to_time(&rtc_time, &rtc_secs);
513 alarm_delta = wall_time.tv_sec - rtc_secs;
514 alarm_time = power_on_alarm - alarm_delta;
515 if (alarm_time <= rtc_secs)
516 goto disable_alarm;
517
518 rtc_time_to_tm(alarm_time, &alarm.time);
519 alarm.enabled = 1;
520 rc = rtc_set_alarm(alarm_rtc_dev, &alarm);
521 if (rc)
522 pr_alarm(ERROR, "Unable to set power-on alarm\n");
523 else
524 pr_alarm(FLOW, "Power-on alarm set to %lu\n",
525 alarm_time);
526
527 spin_unlock_irqrestore(&alarm_slock, flags);
528 return;
529
530disable_alarm:
531 rtc_alarm_irq_enable(alarm_rtc_dev, 0);
532 spin_unlock_irqrestore(&alarm_slock, flags);
533}
534
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700535static struct rtc_task alarm_rtc_task = {
536 .func = alarm_triggered_func
537};
538
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700539static int rtc_alarm_add_device(struct device *dev,
540 struct class_interface *class_intf)
541{
542 int err;
543 struct rtc_device *rtc = to_rtc_device(dev);
544
545 mutex_lock(&alarm_setrtc_mutex);
546
547 if (alarm_rtc_dev) {
548 err = -EBUSY;
549 goto err1;
550 }
551
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700552 alarm_platform_dev =
553 platform_device_register_simple("alarm", -1, NULL, 0);
554 if (IS_ERR(alarm_platform_dev)) {
555 err = PTR_ERR(alarm_platform_dev);
556 goto err2;
557 }
558 err = rtc_irq_register(rtc, &alarm_rtc_task);
559 if (err)
560 goto err3;
561 alarm_rtc_dev = rtc;
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700562 pr_alarm(INIT_STATUS, "using rtc device, %s, for alarms", rtc->name);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700563 mutex_unlock(&alarm_setrtc_mutex);
564
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700565 return 0;
566
567err3:
568 platform_device_unregister(alarm_platform_dev);
569err2:
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700570err1:
571 mutex_unlock(&alarm_setrtc_mutex);
572 return err;
573}
574
575static void rtc_alarm_remove_device(struct device *dev,
576 struct class_interface *class_intf)
577{
578 if (dev == &alarm_rtc_dev->dev) {
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700579 pr_alarm(INIT_STATUS, "lost rtc device for alarms");
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700580 rtc_irq_unregister(alarm_rtc_dev, &alarm_rtc_task);
581 platform_device_unregister(alarm_platform_dev);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700582 alarm_rtc_dev = NULL;
583 }
584}
585
586static struct class_interface rtc_alarm_interface = {
587 .add_dev = &rtc_alarm_add_device,
588 .remove_dev = &rtc_alarm_remove_device,
589};
590
591static struct platform_driver alarm_driver = {
592 .suspend = alarm_suspend,
593 .resume = alarm_resume,
Ashay Jaiswalc1b2e472013-05-17 15:15:02 +0530594 .shutdown = alarm_shutdown,
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700595 .driver = {
596 .name = "alarm"
597 }
598};
599
600static int __init alarm_late_init(void)
601{
602 unsigned long flags;
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700603 struct timespec tmp_time, system_time;
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700604
605 /* this needs to run after the rtc is read at boot */
606 spin_lock_irqsave(&alarm_slock, flags);
607 /* We read the current rtc and system time so we can later calulate
608 * elasped realtime to be (boot_systemtime + rtc - boot_rtc) ==
609 * (rtc - (boot_rtc - boot_systemtime))
610 */
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700611 getnstimeofday(&tmp_time);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700612 ktime_get_ts(&system_time);
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700613 alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP].delta =
614 alarms[ANDROID_ALARM_ELAPSED_REALTIME].delta =
615 timespec_to_ktime(timespec_sub(tmp_time, system_time));
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700616
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700617 spin_unlock_irqrestore(&alarm_slock, flags);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700618 return 0;
619}
620
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700621static int __init alarm_driver_init(void)
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700622{
623 int err;
624 int i;
625
626 for (i = 0; i < ANDROID_ALARM_SYSTEMTIME; i++) {
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700627 hrtimer_init(&alarms[i].timer,
628 CLOCK_REALTIME, HRTIMER_MODE_ABS);
629 alarms[i].timer.function = alarm_timer_triggered;
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700630 }
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700631 hrtimer_init(&alarms[ANDROID_ALARM_SYSTEMTIME].timer,
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700632 CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700633 alarms[ANDROID_ALARM_SYSTEMTIME].timer.function = alarm_timer_triggered;
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700634 err = platform_driver_register(&alarm_driver);
635 if (err < 0)
636 goto err1;
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700637 wake_lock_init(&alarm_rtc_wake_lock, WAKE_LOCK_SUSPEND, "alarm_rtc");
638 rtc_alarm_interface.class = rtc_class;
639 err = class_interface_register(&rtc_alarm_interface);
640 if (err < 0)
641 goto err2;
642
643 return 0;
644
645err2:
646 wake_lock_destroy(&alarm_rtc_wake_lock);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700647 platform_driver_unregister(&alarm_driver);
648err1:
649 return err;
650}
651
652static void __exit alarm_exit(void)
653{
654 class_interface_unregister(&rtc_alarm_interface);
655 wake_lock_destroy(&alarm_rtc_wake_lock);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700656 platform_driver_unregister(&alarm_driver);
657}
658
659late_initcall(alarm_late_init);
Arve Hjønnevågfa565402009-05-04 14:09:15 -0700660module_init(alarm_driver_init);
Arve Hjønnevågb54b33d2008-10-14 17:38:04 -0700661module_exit(alarm_exit);
662