Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 1 | /* drivers/rtc/alarm.c |
| 2 | * |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 3 | * Copyright (C) 2007-2009 Google, Inc. |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 4 | * |
| 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 Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 16 | #include <linux/module.h> |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 17 | #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åg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 24 | #include <linux/wakelock.h> |
| 25 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 26 | #include <asm/mach/time.h> |
| 27 | |
Mohit Aggarwal | 7289fdd | 2013-08-06 23:24:31 +0530 | [diff] [blame] | 28 | #define ALARM_DELTA 120 |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 29 | #define ANDROID_ALARM_PRINT_ERROR (1U << 0) |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 30 | #define ANDROID_ALARM_PRINT_INIT_STATUS (1U << 1) |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 31 | #define ANDROID_ALARM_PRINT_TSET (1U << 2) |
| 32 | #define ANDROID_ALARM_PRINT_CALL (1U << 3) |
| 33 | #define ANDROID_ALARM_PRINT_SUSPEND (1U << 4) |
| 34 | #define ANDROID_ALARM_PRINT_INT (1U << 5) |
| 35 | #define ANDROID_ALARM_PRINT_FLOW (1U << 6) |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 36 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 37 | static int debug_mask = ANDROID_ALARM_PRINT_ERROR | \ |
| 38 | ANDROID_ALARM_PRINT_INIT_STATUS; |
| 39 | module_param_named(debug_mask, debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP); |
| 40 | |
| 41 | #define pr_alarm(debug_level_mask, args...) \ |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 42 | do { \ |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 43 | if (debug_mask & ANDROID_ALARM_PRINT_##debug_level_mask) { \ |
| 44 | pr_info(args); \ |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 45 | } \ |
| 46 | } while (0) |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 47 | |
| 48 | #define ANDROID_ALARM_WAKEUP_MASK ( \ |
| 49 | ANDROID_ALARM_RTC_WAKEUP_MASK | \ |
| 50 | ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP_MASK) |
| 51 | |
| 52 | /* support old usespace code */ |
| 53 | #define ANDROID_ALARM_SET_OLD _IOW('a', 2, time_t) /* set alarm */ |
| 54 | #define ANDROID_ALARM_SET_AND_WAIT_OLD _IOW('a', 3, time_t) |
| 55 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 56 | struct alarm_queue { |
| 57 | struct rb_root alarms; |
| 58 | struct rb_node *first; |
| 59 | struct hrtimer timer; |
| 60 | ktime_t delta; |
| 61 | bool stopped; |
| 62 | ktime_t stopped_time; |
| 63 | }; |
| 64 | |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 65 | static struct rtc_device *alarm_rtc_dev; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 66 | static DEFINE_SPINLOCK(alarm_slock); |
| 67 | static DEFINE_MUTEX(alarm_setrtc_mutex); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 68 | static struct wake_lock alarm_rtc_wake_lock; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 69 | static struct platform_device *alarm_platform_dev; |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 70 | struct alarm_queue alarms[ANDROID_ALARM_TYPE_COUNT]; |
| 71 | static bool suspended; |
Ashay Jaiswal | c1b2e47 | 2013-05-17 15:15:02 +0530 | [diff] [blame] | 72 | static long power_on_alarm; |
| 73 | |
| 74 | void set_power_on_alarm(long secs) |
| 75 | { |
| 76 | power_on_alarm = secs; |
| 77 | } |
| 78 | |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 79 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 80 | static void update_timer_locked(struct alarm_queue *base, bool head_removed) |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 81 | { |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 82 | struct alarm *alarm; |
| 83 | bool is_wakeup = base == &alarms[ANDROID_ALARM_RTC_WAKEUP] || |
Figo Wang | 091c9af | 2013-09-12 20:41:27 +0800 | [diff] [blame] | 84 | base == &alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP] || |
| 85 | base == &alarms[ANDROID_ALARM_RTC_POWEROFF_WAKEUP]; |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 86 | |
| 87 | if (base->stopped) { |
| 88 | pr_alarm(FLOW, "changed alarm while setting the wall time\n"); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 89 | return; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 92 | if (is_wakeup && !suspended && head_removed) |
| 93 | wake_unlock(&alarm_rtc_wake_lock); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 94 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 95 | if (!base->first) |
| 96 | return; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 97 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 98 | alarm = container_of(base->first, struct alarm, node); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 99 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 100 | pr_alarm(FLOW, "selected alarm, type %d, func %pF at %lld\n", |
| 101 | alarm->type, alarm->function, ktime_to_ns(alarm->expires)); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 102 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 103 | if (is_wakeup && suspended) { |
| 104 | pr_alarm(FLOW, "changed alarm while suspened\n"); |
| 105 | wake_lock_timeout(&alarm_rtc_wake_lock, 1 * HZ); |
| 106 | return; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 107 | } |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 108 | |
| 109 | hrtimer_try_to_cancel(&base->timer); |
| 110 | base->timer.node.expires = ktime_add(base->delta, alarm->expires); |
| 111 | base->timer._softexpires = ktime_add(base->delta, alarm->softexpires); |
| 112 | hrtimer_start_expires(&base->timer, HRTIMER_MODE_ABS); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 115 | static void alarm_enqueue_locked(struct alarm *alarm) |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 116 | { |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 117 | struct alarm_queue *base = &alarms[alarm->type]; |
| 118 | struct rb_node **link = &base->alarms.rb_node; |
| 119 | struct rb_node *parent = NULL; |
| 120 | struct alarm *entry; |
| 121 | int leftmost = 1; |
Arve Hjønnevåg | 2cd2468 | 2011-01-07 19:00:01 -0800 | [diff] [blame] | 122 | bool was_first = false; |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 123 | |
| 124 | pr_alarm(FLOW, "added alarm, type %d, func %pF at %lld\n", |
| 125 | alarm->type, alarm->function, ktime_to_ns(alarm->expires)); |
| 126 | |
Arve Hjønnevåg | 2cd2468 | 2011-01-07 19:00:01 -0800 | [diff] [blame] | 127 | if (base->first == &alarm->node) { |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 128 | base->first = rb_next(&alarm->node); |
Arve Hjønnevåg | 2cd2468 | 2011-01-07 19:00:01 -0800 | [diff] [blame] | 129 | was_first = true; |
| 130 | } |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 131 | if (!RB_EMPTY_NODE(&alarm->node)) { |
| 132 | rb_erase(&alarm->node, &base->alarms); |
| 133 | RB_CLEAR_NODE(&alarm->node); |
| 134 | } |
| 135 | |
| 136 | while (*link) { |
| 137 | parent = *link; |
| 138 | entry = rb_entry(parent, struct alarm, node); |
| 139 | /* |
| 140 | * We dont care about collisions. Nodes with |
| 141 | * the same expiry time stay together. |
| 142 | */ |
| 143 | if (alarm->expires.tv64 < entry->expires.tv64) { |
| 144 | link = &(*link)->rb_left; |
| 145 | } else { |
| 146 | link = &(*link)->rb_right; |
| 147 | leftmost = 0; |
| 148 | } |
| 149 | } |
Arve Hjønnevåg | 2cd2468 | 2011-01-07 19:00:01 -0800 | [diff] [blame] | 150 | if (leftmost) |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 151 | base->first = &alarm->node; |
Arve Hjønnevåg | 2cd2468 | 2011-01-07 19:00:01 -0800 | [diff] [blame] | 152 | if (leftmost || was_first) |
| 153 | update_timer_locked(base, was_first); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 154 | |
| 155 | rb_link_node(&alarm->node, parent, link); |
| 156 | rb_insert_color(&alarm->node, &base->alarms); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 159 | /** |
| 160 | * alarm_init - initialize an alarm |
| 161 | * @alarm: the alarm to be initialized |
| 162 | * @type: the alarm type to be used |
| 163 | * @function: alarm callback function |
| 164 | */ |
| 165 | void alarm_init(struct alarm *alarm, |
| 166 | enum android_alarm_type type, void (*function)(struct alarm *)) |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 167 | { |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 168 | RB_CLEAR_NODE(&alarm->node); |
| 169 | alarm->type = type; |
| 170 | alarm->function = function; |
| 171 | |
| 172 | pr_alarm(FLOW, "created alarm, type %d, func %pF\n", type, function); |
| 173 | } |
| 174 | |
| 175 | |
| 176 | /** |
| 177 | * alarm_start_range - (re)start an alarm |
| 178 | * @alarm: the alarm to be added |
| 179 | * @start: earliest expiry time |
| 180 | * @end: expiry time |
| 181 | */ |
| 182 | void alarm_start_range(struct alarm *alarm, ktime_t start, ktime_t end) |
| 183 | { |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 184 | unsigned long flags; |
| 185 | |
| 186 | spin_lock_irqsave(&alarm_slock, flags); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 187 | alarm->softexpires = start; |
| 188 | alarm->expires = end; |
| 189 | alarm_enqueue_locked(alarm); |
| 190 | spin_unlock_irqrestore(&alarm_slock, flags); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * alarm_try_to_cancel - try to deactivate an alarm |
| 195 | * @alarm: alarm to stop |
| 196 | * |
| 197 | * Returns: |
| 198 | * 0 when the alarm was not active |
| 199 | * 1 when the alarm was active |
| 200 | * -1 when the alarm may currently be excuting the callback function and |
| 201 | * cannot be stopped (it may also be inactive) |
| 202 | */ |
| 203 | int alarm_try_to_cancel(struct alarm *alarm) |
| 204 | { |
| 205 | struct alarm_queue *base = &alarms[alarm->type]; |
| 206 | unsigned long flags; |
| 207 | bool first = false; |
| 208 | int ret = 0; |
| 209 | |
| 210 | spin_lock_irqsave(&alarm_slock, flags); |
| 211 | if (!RB_EMPTY_NODE(&alarm->node)) { |
| 212 | pr_alarm(FLOW, "canceled alarm, type %d, func %pF at %lld\n", |
| 213 | alarm->type, alarm->function, |
| 214 | ktime_to_ns(alarm->expires)); |
| 215 | ret = 1; |
| 216 | if (base->first == &alarm->node) { |
| 217 | base->first = rb_next(&alarm->node); |
| 218 | first = true; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 219 | } |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 220 | rb_erase(&alarm->node, &base->alarms); |
| 221 | RB_CLEAR_NODE(&alarm->node); |
| 222 | if (first) |
| 223 | update_timer_locked(base, true); |
| 224 | } else |
| 225 | pr_alarm(FLOW, "tried to cancel alarm, type %d, func %pF\n", |
| 226 | alarm->type, alarm->function); |
| 227 | spin_unlock_irqrestore(&alarm_slock, flags); |
| 228 | if (!ret && hrtimer_callback_running(&base->timer)) |
| 229 | ret = -1; |
| 230 | return ret; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * alarm_cancel - cancel an alarm and wait for the handler to finish. |
| 235 | * @alarm: the alarm to be cancelled |
| 236 | * |
| 237 | * Returns: |
| 238 | * 0 when the alarm was not active |
| 239 | * 1 when the alarm was active |
| 240 | */ |
| 241 | int alarm_cancel(struct alarm *alarm) |
| 242 | { |
| 243 | for (;;) { |
| 244 | int ret = alarm_try_to_cancel(alarm); |
| 245 | if (ret >= 0) |
| 246 | return ret; |
| 247 | cpu_relax(); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * alarm_set_rtc - set the kernel and rtc walltime |
| 253 | * @new_time: timespec value containing the new time |
| 254 | */ |
| 255 | int alarm_set_rtc(struct timespec new_time) |
| 256 | { |
| 257 | int i; |
| 258 | int ret; |
| 259 | unsigned long flags; |
| 260 | struct rtc_time rtc_new_rtc_time; |
| 261 | struct timespec tmp_time; |
| 262 | |
| 263 | rtc_time_to_tm(new_time.tv_sec, &rtc_new_rtc_time); |
| 264 | |
| 265 | pr_alarm(TSET, "set rtc %ld %ld - rtc %02d:%02d:%02d %02d/%02d/%04d\n", |
| 266 | new_time.tv_sec, new_time.tv_nsec, |
| 267 | rtc_new_rtc_time.tm_hour, rtc_new_rtc_time.tm_min, |
| 268 | rtc_new_rtc_time.tm_sec, rtc_new_rtc_time.tm_mon + 1, |
| 269 | rtc_new_rtc_time.tm_mday, |
| 270 | rtc_new_rtc_time.tm_year + 1900); |
| 271 | |
| 272 | mutex_lock(&alarm_setrtc_mutex); |
| 273 | spin_lock_irqsave(&alarm_slock, flags); |
| 274 | wake_lock(&alarm_rtc_wake_lock); |
| 275 | getnstimeofday(&tmp_time); |
| 276 | for (i = 0; i < ANDROID_ALARM_SYSTEMTIME; i++) { |
| 277 | hrtimer_try_to_cancel(&alarms[i].timer); |
| 278 | alarms[i].stopped = true; |
| 279 | alarms[i].stopped_time = timespec_to_ktime(tmp_time); |
| 280 | } |
| 281 | alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP].delta = |
| 282 | alarms[ANDROID_ALARM_ELAPSED_REALTIME].delta = |
| 283 | ktime_sub(alarms[ANDROID_ALARM_ELAPSED_REALTIME].delta, |
| 284 | timespec_to_ktime(timespec_sub(tmp_time, new_time))); |
| 285 | spin_unlock_irqrestore(&alarm_slock, flags); |
| 286 | ret = do_settimeofday(&new_time); |
| 287 | spin_lock_irqsave(&alarm_slock, flags); |
| 288 | for (i = 0; i < ANDROID_ALARM_SYSTEMTIME; i++) { |
| 289 | alarms[i].stopped = false; |
| 290 | update_timer_locked(&alarms[i], false); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 291 | } |
| 292 | spin_unlock_irqrestore(&alarm_slock, flags); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 293 | if (ret < 0) { |
| 294 | pr_alarm(ERROR, "alarm_set_rtc: Failed to set time\n"); |
| 295 | goto err; |
| 296 | } |
| 297 | if (!alarm_rtc_dev) { |
| 298 | pr_alarm(ERROR, |
| 299 | "alarm_set_rtc: no RTC, time will be lost on reboot\n"); |
| 300 | goto err; |
| 301 | } |
| 302 | ret = rtc_set_time(alarm_rtc_dev, &rtc_new_rtc_time); |
| 303 | if (ret < 0) |
| 304 | pr_alarm(ERROR, "alarm_set_rtc: " |
| 305 | "Failed to set RTC, time will be lost on reboot\n"); |
| 306 | err: |
| 307 | wake_unlock(&alarm_rtc_wake_lock); |
| 308 | mutex_unlock(&alarm_setrtc_mutex); |
| 309 | return ret; |
| 310 | } |
| 311 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 312 | |
| 313 | void |
| 314 | alarm_update_timedelta(struct timespec tmp_time, struct timespec new_time) |
| 315 | { |
| 316 | int i; |
| 317 | unsigned long flags; |
| 318 | |
| 319 | spin_lock_irqsave(&alarm_slock, flags); |
| 320 | for (i = 0; i < ANDROID_ALARM_SYSTEMTIME; i++) { |
| 321 | hrtimer_try_to_cancel(&alarms[i].timer); |
| 322 | alarms[i].stopped = true; |
| 323 | alarms[i].stopped_time = timespec_to_ktime(tmp_time); |
| 324 | } |
| 325 | alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP].delta = |
| 326 | alarms[ANDROID_ALARM_ELAPSED_REALTIME].delta = |
| 327 | ktime_sub(alarms[ANDROID_ALARM_ELAPSED_REALTIME].delta, |
| 328 | timespec_to_ktime(timespec_sub(tmp_time, new_time))); |
| 329 | for (i = 0; i < ANDROID_ALARM_SYSTEMTIME; i++) { |
| 330 | alarms[i].stopped = false; |
| 331 | update_timer_locked(&alarms[i], false); |
| 332 | } |
| 333 | spin_unlock_irqrestore(&alarm_slock, flags); |
| 334 | } |
| 335 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 336 | /** |
| 337 | * alarm_get_elapsed_realtime - get the elapsed real time in ktime_t format |
| 338 | * |
| 339 | * returns the time in ktime_t format |
| 340 | */ |
| 341 | ktime_t alarm_get_elapsed_realtime(void) |
| 342 | { |
| 343 | ktime_t now; |
| 344 | unsigned long flags; |
| 345 | struct alarm_queue *base = &alarms[ANDROID_ALARM_ELAPSED_REALTIME]; |
| 346 | |
| 347 | spin_lock_irqsave(&alarm_slock, flags); |
| 348 | now = base->stopped ? base->stopped_time : ktime_get_real(); |
| 349 | now = ktime_sub(now, base->delta); |
| 350 | spin_unlock_irqrestore(&alarm_slock, flags); |
| 351 | return now; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | static enum hrtimer_restart alarm_timer_triggered(struct hrtimer *timer) |
| 355 | { |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 356 | struct alarm_queue *base; |
| 357 | struct alarm *alarm; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 358 | unsigned long flags; |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 359 | ktime_t now; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 360 | |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 361 | spin_lock_irqsave(&alarm_slock, flags); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 362 | |
| 363 | base = container_of(timer, struct alarm_queue, timer); |
| 364 | now = base->stopped ? base->stopped_time : hrtimer_cb_get_time(timer); |
| 365 | now = ktime_sub(now, base->delta); |
| 366 | |
| 367 | pr_alarm(INT, "alarm_timer_triggered type %d at %lld\n", |
| 368 | base - alarms, ktime_to_ns(now)); |
| 369 | |
| 370 | while (base->first) { |
| 371 | alarm = container_of(base->first, struct alarm, node); |
| 372 | if (alarm->softexpires.tv64 > now.tv64) { |
| 373 | pr_alarm(FLOW, "don't call alarm, %pF, %lld (s %lld)\n", |
| 374 | alarm->function, ktime_to_ns(alarm->expires), |
| 375 | ktime_to_ns(alarm->softexpires)); |
| 376 | break; |
| 377 | } |
| 378 | base->first = rb_next(&alarm->node); |
| 379 | rb_erase(&alarm->node, &base->alarms); |
| 380 | RB_CLEAR_NODE(&alarm->node); |
| 381 | pr_alarm(CALL, "call alarm, type %d, func %pF, %lld (s %lld)\n", |
| 382 | alarm->type, alarm->function, |
| 383 | ktime_to_ns(alarm->expires), |
| 384 | ktime_to_ns(alarm->softexpires)); |
| 385 | spin_unlock_irqrestore(&alarm_slock, flags); |
| 386 | alarm->function(alarm); |
| 387 | spin_lock_irqsave(&alarm_slock, flags); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 388 | } |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 389 | if (!base->first) |
| 390 | pr_alarm(FLOW, "no more alarms of type %d\n", base - alarms); |
| 391 | update_timer_locked(base, true); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 392 | spin_unlock_irqrestore(&alarm_slock, flags); |
| 393 | return HRTIMER_NORESTART; |
| 394 | } |
| 395 | |
| 396 | static void alarm_triggered_func(void *p) |
| 397 | { |
| 398 | struct rtc_device *rtc = alarm_rtc_dev; |
| 399 | if (!(rtc->irq_data & RTC_AF)) |
| 400 | return; |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 401 | pr_alarm(INT, "rtc alarm triggered\n"); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 402 | wake_lock_timeout(&alarm_rtc_wake_lock, 1 * HZ); |
| 403 | } |
| 404 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 405 | static int alarm_suspend(struct platform_device *pdev, pm_message_t state) |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 406 | { |
| 407 | int err = 0; |
| 408 | unsigned long flags; |
| 409 | struct rtc_wkalrm rtc_alarm; |
| 410 | struct rtc_time rtc_current_rtc_time; |
| 411 | unsigned long rtc_current_time; |
| 412 | unsigned long rtc_alarm_time; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 413 | struct timespec rtc_delta; |
Arve Hjønnevåg | b2a4ab2 | 2010-03-12 20:05:32 -0800 | [diff] [blame] | 414 | struct timespec wall_time; |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 415 | struct alarm_queue *wakeup_queue = NULL; |
| 416 | struct alarm_queue *tmp_queue = NULL; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 417 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 418 | pr_alarm(SUSPEND, "alarm_suspend(%p, %d)\n", pdev, state.event); |
| 419 | |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 420 | spin_lock_irqsave(&alarm_slock, flags); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 421 | suspended = true; |
| 422 | spin_unlock_irqrestore(&alarm_slock, flags); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 423 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 424 | hrtimer_cancel(&alarms[ANDROID_ALARM_RTC_WAKEUP].timer); |
| 425 | hrtimer_cancel(&alarms[ |
JP Abgrall | 692e468 | 2011-09-02 18:14:12 -0700 | [diff] [blame] | 426 | ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP].timer); |
Figo Wang | 091c9af | 2013-09-12 20:41:27 +0800 | [diff] [blame] | 427 | hrtimer_cancel(&alarms[ |
| 428 | ANDROID_ALARM_RTC_POWEROFF_WAKEUP].timer); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 429 | |
| 430 | tmp_queue = &alarms[ANDROID_ALARM_RTC_WAKEUP]; |
| 431 | if (tmp_queue->first) |
| 432 | wakeup_queue = tmp_queue; |
Figo Wang | 091c9af | 2013-09-12 20:41:27 +0800 | [diff] [blame] | 433 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 434 | tmp_queue = &alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP]; |
| 435 | if (tmp_queue->first && (!wakeup_queue || |
| 436 | hrtimer_get_expires(&tmp_queue->timer).tv64 < |
| 437 | hrtimer_get_expires(&wakeup_queue->timer).tv64)) |
| 438 | wakeup_queue = tmp_queue; |
Figo Wang | 091c9af | 2013-09-12 20:41:27 +0800 | [diff] [blame] | 439 | |
| 440 | tmp_queue = &alarms[ANDROID_ALARM_RTC_POWEROFF_WAKEUP]; |
| 441 | if (tmp_queue->first && (!wakeup_queue || |
| 442 | hrtimer_get_expires(&tmp_queue->timer).tv64 < |
| 443 | hrtimer_get_expires(&wakeup_queue->timer).tv64)) |
| 444 | wakeup_queue = tmp_queue; |
| 445 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 446 | if (wakeup_queue) { |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 447 | rtc_read_time(alarm_rtc_dev, &rtc_current_rtc_time); |
Arve Hjønnevåg | b2a4ab2 | 2010-03-12 20:05:32 -0800 | [diff] [blame] | 448 | getnstimeofday(&wall_time); |
| 449 | rtc_tm_to_time(&rtc_current_rtc_time, &rtc_current_time); |
| 450 | set_normalized_timespec(&rtc_delta, |
| 451 | wall_time.tv_sec - rtc_current_time, |
| 452 | wall_time.tv_nsec); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 453 | |
| 454 | rtc_alarm_time = timespec_sub(ktime_to_timespec( |
| 455 | hrtimer_get_expires(&wakeup_queue->timer)), |
| 456 | rtc_delta).tv_sec; |
| 457 | |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 458 | rtc_time_to_tm(rtc_alarm_time, &rtc_alarm.time); |
| 459 | rtc_alarm.enabled = 1; |
| 460 | rtc_set_alarm(alarm_rtc_dev, &rtc_alarm); |
| 461 | rtc_read_time(alarm_rtc_dev, &rtc_current_rtc_time); |
| 462 | rtc_tm_to_time(&rtc_current_rtc_time, &rtc_current_time); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 463 | pr_alarm(SUSPEND, |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 464 | "rtc alarm set at %ld, now %ld, rtc delta %ld.%09ld\n", |
| 465 | rtc_alarm_time, rtc_current_time, |
| 466 | rtc_delta.tv_sec, rtc_delta.tv_nsec); |
| 467 | if (rtc_current_time + 1 >= rtc_alarm_time) { |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 468 | pr_alarm(SUSPEND, "alarm about to go off\n"); |
Figo Wang | 1b2c1c6 | 2013-07-30 09:33:59 +0800 | [diff] [blame] | 469 | rtc_time_to_tm(0, &rtc_alarm.time); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 470 | rtc_alarm.enabled = 0; |
| 471 | rtc_set_alarm(alarm_rtc_dev, &rtc_alarm); |
| 472 | |
| 473 | spin_lock_irqsave(&alarm_slock, flags); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 474 | suspended = false; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 475 | wake_lock_timeout(&alarm_rtc_wake_lock, 2 * HZ); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 476 | update_timer_locked(&alarms[ANDROID_ALARM_RTC_WAKEUP], |
| 477 | false); |
| 478 | update_timer_locked(&alarms[ |
| 479 | ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP], false); |
Figo Wang | 091c9af | 2013-09-12 20:41:27 +0800 | [diff] [blame] | 480 | update_timer_locked(&alarms[ |
| 481 | ANDROID_ALARM_RTC_POWEROFF_WAKEUP], false); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 482 | err = -EBUSY; |
| 483 | spin_unlock_irqrestore(&alarm_slock, flags); |
| 484 | } |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 485 | } |
| 486 | return err; |
| 487 | } |
| 488 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 489 | static int alarm_resume(struct platform_device *pdev) |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 490 | { |
| 491 | struct rtc_wkalrm alarm; |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 492 | unsigned long flags; |
| 493 | |
| 494 | pr_alarm(SUSPEND, "alarm_resume(%p)\n", pdev); |
| 495 | |
Figo Wang | 1b2c1c6 | 2013-07-30 09:33:59 +0800 | [diff] [blame] | 496 | rtc_time_to_tm(0, &alarm.time); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 497 | alarm.enabled = 0; |
| 498 | rtc_set_alarm(alarm_rtc_dev, &alarm); |
| 499 | |
| 500 | spin_lock_irqsave(&alarm_slock, flags); |
| 501 | suspended = false; |
| 502 | update_timer_locked(&alarms[ANDROID_ALARM_RTC_WAKEUP], false); |
| 503 | update_timer_locked(&alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP], |
| 504 | false); |
Figo Wang | 091c9af | 2013-09-12 20:41:27 +0800 | [diff] [blame] | 505 | update_timer_locked(&alarms[ANDROID_ALARM_RTC_POWEROFF_WAKEUP], |
| 506 | false); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 507 | spin_unlock_irqrestore(&alarm_slock, flags); |
| 508 | |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 509 | return 0; |
| 510 | } |
| 511 | |
Ashay Jaiswal | c1b2e47 | 2013-05-17 15:15:02 +0530 | [diff] [blame] | 512 | static void alarm_shutdown(struct platform_device *dev) |
| 513 | { |
| 514 | struct timespec wall_time; |
| 515 | struct rtc_time rtc_time; |
| 516 | struct rtc_wkalrm alarm; |
| 517 | unsigned long flags; |
| 518 | long rtc_secs, alarm_delta, alarm_time; |
| 519 | int rc; |
| 520 | |
| 521 | spin_lock_irqsave(&alarm_slock, flags); |
| 522 | |
Mohit Aggarwal | 6a5881b | 2014-03-04 21:43:37 +0530 | [diff] [blame] | 523 | if (!power_on_alarm) { |
| 524 | spin_unlock_irqrestore(&alarm_slock, flags); |
Ashay Jaiswal | c1b2e47 | 2013-05-17 15:15:02 +0530 | [diff] [blame] | 525 | goto disable_alarm; |
Mohit Aggarwal | 6a5881b | 2014-03-04 21:43:37 +0530 | [diff] [blame] | 526 | } |
| 527 | spin_unlock_irqrestore(&alarm_slock, flags); |
Ashay Jaiswal | c1b2e47 | 2013-05-17 15:15:02 +0530 | [diff] [blame] | 528 | |
| 529 | rtc_read_time(alarm_rtc_dev, &rtc_time); |
| 530 | getnstimeofday(&wall_time); |
| 531 | rtc_tm_to_time(&rtc_time, &rtc_secs); |
| 532 | alarm_delta = wall_time.tv_sec - rtc_secs; |
| 533 | alarm_time = power_on_alarm - alarm_delta; |
Mohit Aggarwal | 7289fdd | 2013-08-06 23:24:31 +0530 | [diff] [blame] | 534 | |
| 535 | /* |
| 536 | * Substract ALARM_DELTA from actual alarm time |
| 537 | * to powerup the device before actual alarm |
| 538 | * expiration. |
| 539 | */ |
| 540 | if ((alarm_time - ALARM_DELTA) > rtc_secs) |
| 541 | alarm_time -= ALARM_DELTA; |
| 542 | |
Ashay Jaiswal | c1b2e47 | 2013-05-17 15:15:02 +0530 | [diff] [blame] | 543 | if (alarm_time <= rtc_secs) |
| 544 | goto disable_alarm; |
| 545 | |
| 546 | rtc_time_to_tm(alarm_time, &alarm.time); |
| 547 | alarm.enabled = 1; |
| 548 | rc = rtc_set_alarm(alarm_rtc_dev, &alarm); |
| 549 | if (rc) |
| 550 | pr_alarm(ERROR, "Unable to set power-on alarm\n"); |
| 551 | else |
| 552 | pr_alarm(FLOW, "Power-on alarm set to %lu\n", |
| 553 | alarm_time); |
| 554 | |
Ashay Jaiswal | c1b2e47 | 2013-05-17 15:15:02 +0530 | [diff] [blame] | 555 | return; |
| 556 | |
| 557 | disable_alarm: |
Prasad Sodagudi | c83a382 | 2013-12-31 16:34:26 +0530 | [diff] [blame] | 558 | rtc_alarm_irq_enable(alarm_rtc_dev, 0); |
Ashay Jaiswal | c1b2e47 | 2013-05-17 15:15:02 +0530 | [diff] [blame] | 559 | } |
| 560 | |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 561 | static struct rtc_task alarm_rtc_task = { |
| 562 | .func = alarm_triggered_func |
| 563 | }; |
| 564 | |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 565 | static int rtc_alarm_add_device(struct device *dev, |
| 566 | struct class_interface *class_intf) |
| 567 | { |
| 568 | int err; |
| 569 | struct rtc_device *rtc = to_rtc_device(dev); |
| 570 | |
| 571 | mutex_lock(&alarm_setrtc_mutex); |
| 572 | |
| 573 | if (alarm_rtc_dev) { |
| 574 | err = -EBUSY; |
| 575 | goto err1; |
| 576 | } |
| 577 | |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 578 | alarm_platform_dev = |
| 579 | platform_device_register_simple("alarm", -1, NULL, 0); |
| 580 | if (IS_ERR(alarm_platform_dev)) { |
| 581 | err = PTR_ERR(alarm_platform_dev); |
| 582 | goto err2; |
| 583 | } |
| 584 | err = rtc_irq_register(rtc, &alarm_rtc_task); |
| 585 | if (err) |
| 586 | goto err3; |
| 587 | alarm_rtc_dev = rtc; |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 588 | pr_alarm(INIT_STATUS, "using rtc device, %s, for alarms", rtc->name); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 589 | mutex_unlock(&alarm_setrtc_mutex); |
| 590 | |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 591 | return 0; |
| 592 | |
| 593 | err3: |
| 594 | platform_device_unregister(alarm_platform_dev); |
| 595 | err2: |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 596 | err1: |
| 597 | mutex_unlock(&alarm_setrtc_mutex); |
| 598 | return err; |
| 599 | } |
| 600 | |
| 601 | static void rtc_alarm_remove_device(struct device *dev, |
| 602 | struct class_interface *class_intf) |
| 603 | { |
| 604 | if (dev == &alarm_rtc_dev->dev) { |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 605 | pr_alarm(INIT_STATUS, "lost rtc device for alarms"); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 606 | rtc_irq_unregister(alarm_rtc_dev, &alarm_rtc_task); |
| 607 | platform_device_unregister(alarm_platform_dev); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 608 | alarm_rtc_dev = NULL; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | static struct class_interface rtc_alarm_interface = { |
| 613 | .add_dev = &rtc_alarm_add_device, |
| 614 | .remove_dev = &rtc_alarm_remove_device, |
| 615 | }; |
| 616 | |
| 617 | static struct platform_driver alarm_driver = { |
| 618 | .suspend = alarm_suspend, |
| 619 | .resume = alarm_resume, |
Ashay Jaiswal | c1b2e47 | 2013-05-17 15:15:02 +0530 | [diff] [blame] | 620 | .shutdown = alarm_shutdown, |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 621 | .driver = { |
| 622 | .name = "alarm" |
| 623 | } |
| 624 | }; |
| 625 | |
| 626 | static int __init alarm_late_init(void) |
| 627 | { |
| 628 | unsigned long flags; |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 629 | struct timespec tmp_time, system_time; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 630 | |
| 631 | /* this needs to run after the rtc is read at boot */ |
| 632 | spin_lock_irqsave(&alarm_slock, flags); |
| 633 | /* We read the current rtc and system time so we can later calulate |
| 634 | * elasped realtime to be (boot_systemtime + rtc - boot_rtc) == |
| 635 | * (rtc - (boot_rtc - boot_systemtime)) |
| 636 | */ |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 637 | getnstimeofday(&tmp_time); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 638 | ktime_get_ts(&system_time); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 639 | alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP].delta = |
| 640 | alarms[ANDROID_ALARM_ELAPSED_REALTIME].delta = |
| 641 | timespec_to_ktime(timespec_sub(tmp_time, system_time)); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 642 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 643 | spin_unlock_irqrestore(&alarm_slock, flags); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 644 | return 0; |
| 645 | } |
| 646 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 647 | static int __init alarm_driver_init(void) |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 648 | { |
| 649 | int err; |
| 650 | int i; |
| 651 | |
| 652 | for (i = 0; i < ANDROID_ALARM_SYSTEMTIME; i++) { |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 653 | hrtimer_init(&alarms[i].timer, |
| 654 | CLOCK_REALTIME, HRTIMER_MODE_ABS); |
| 655 | alarms[i].timer.function = alarm_timer_triggered; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 656 | } |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 657 | hrtimer_init(&alarms[ANDROID_ALARM_SYSTEMTIME].timer, |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 658 | CLOCK_MONOTONIC, HRTIMER_MODE_ABS); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 659 | alarms[ANDROID_ALARM_SYSTEMTIME].timer.function = alarm_timer_triggered; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 660 | err = platform_driver_register(&alarm_driver); |
| 661 | if (err < 0) |
| 662 | goto err1; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 663 | wake_lock_init(&alarm_rtc_wake_lock, WAKE_LOCK_SUSPEND, "alarm_rtc"); |
| 664 | rtc_alarm_interface.class = rtc_class; |
| 665 | err = class_interface_register(&rtc_alarm_interface); |
| 666 | if (err < 0) |
| 667 | goto err2; |
| 668 | |
| 669 | return 0; |
| 670 | |
| 671 | err2: |
| 672 | wake_lock_destroy(&alarm_rtc_wake_lock); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 673 | platform_driver_unregister(&alarm_driver); |
| 674 | err1: |
| 675 | return err; |
| 676 | } |
| 677 | |
| 678 | static void __exit alarm_exit(void) |
| 679 | { |
| 680 | class_interface_unregister(&rtc_alarm_interface); |
| 681 | wake_lock_destroy(&alarm_rtc_wake_lock); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 682 | platform_driver_unregister(&alarm_driver); |
| 683 | } |
| 684 | |
| 685 | late_initcall(alarm_late_init); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 686 | module_init(alarm_driver_init); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 687 | module_exit(alarm_exit); |
| 688 | |