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 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 28 | #define ANDROID_ALARM_PRINT_ERROR (1U << 0) |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 29 | #define ANDROID_ALARM_PRINT_INIT_STATUS (1U << 1) |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 30 | #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åg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 35 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 36 | static int debug_mask = ANDROID_ALARM_PRINT_ERROR | \ |
| 37 | ANDROID_ALARM_PRINT_INIT_STATUS; |
| 38 | module_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åg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 41 | do { \ |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 42 | if (debug_mask & ANDROID_ALARM_PRINT_##debug_level_mask) { \ |
| 43 | pr_info(args); \ |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 44 | } \ |
| 45 | } while (0) |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 46 | |
| 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åg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 55 | struct 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åg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 64 | static struct rtc_device *alarm_rtc_dev; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 65 | static DEFINE_SPINLOCK(alarm_slock); |
| 66 | static DEFINE_MUTEX(alarm_setrtc_mutex); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 67 | static struct wake_lock alarm_rtc_wake_lock; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 68 | static struct platform_device *alarm_platform_dev; |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 69 | struct alarm_queue alarms[ANDROID_ALARM_TYPE_COUNT]; |
| 70 | static bool suspended; |
Ashay Jaiswal | c1b2e47 | 2013-05-17 15:15:02 +0530 | [diff] [blame^] | 71 | static long power_on_alarm; |
| 72 | |
| 73 | void set_power_on_alarm(long secs) |
| 74 | { |
| 75 | power_on_alarm = secs; |
| 76 | } |
| 77 | |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 78 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 79 | 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] | 80 | { |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 81 | 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åg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 87 | return; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 90 | if (is_wakeup && !suspended && head_removed) |
| 91 | wake_unlock(&alarm_rtc_wake_lock); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 92 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 93 | if (!base->first) |
| 94 | return; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 95 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 96 | alarm = container_of(base->first, struct alarm, node); |
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 | 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åg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 100 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 101 | 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åg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 105 | } |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 106 | |
| 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åg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 113 | static void alarm_enqueue_locked(struct alarm *alarm) |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 114 | { |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 115 | 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åg | 2cd2468 | 2011-01-07 19:00:01 -0800 | [diff] [blame] | 120 | bool was_first = false; |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 121 | |
| 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åg | 2cd2468 | 2011-01-07 19:00:01 -0800 | [diff] [blame] | 125 | if (base->first == &alarm->node) { |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 126 | base->first = rb_next(&alarm->node); |
Arve Hjønnevåg | 2cd2468 | 2011-01-07 19:00:01 -0800 | [diff] [blame] | 127 | was_first = true; |
| 128 | } |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 129 | 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åg | 2cd2468 | 2011-01-07 19:00:01 -0800 | [diff] [blame] | 148 | if (leftmost) |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 149 | base->first = &alarm->node; |
Arve Hjønnevåg | 2cd2468 | 2011-01-07 19:00:01 -0800 | [diff] [blame] | 150 | if (leftmost || was_first) |
| 151 | update_timer_locked(base, was_first); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 152 | |
| 153 | rb_link_node(&alarm->node, parent, link); |
| 154 | rb_insert_color(&alarm->node, &base->alarms); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 157 | /** |
| 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 | */ |
| 163 | void alarm_init(struct alarm *alarm, |
| 164 | enum android_alarm_type type, void (*function)(struct alarm *)) |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 165 | { |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 166 | 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 | */ |
| 180 | void alarm_start_range(struct alarm *alarm, ktime_t start, ktime_t end) |
| 181 | { |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 182 | unsigned long flags; |
| 183 | |
| 184 | spin_lock_irqsave(&alarm_slock, flags); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 185 | 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 | */ |
| 201 | int 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åg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 217 | } |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 218 | 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 | */ |
| 239 | int 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 | */ |
| 253 | int 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åg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 289 | } |
| 290 | spin_unlock_irqrestore(&alarm_slock, flags); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 291 | 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"); |
| 304 | err: |
| 305 | wake_unlock(&alarm_rtc_wake_lock); |
| 306 | mutex_unlock(&alarm_setrtc_mutex); |
| 307 | return ret; |
| 308 | } |
| 309 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 310 | |
| 311 | void |
| 312 | alarm_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åg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 334 | /** |
| 335 | * alarm_get_elapsed_realtime - get the elapsed real time in ktime_t format |
| 336 | * |
| 337 | * returns the time in ktime_t format |
| 338 | */ |
| 339 | ktime_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åg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | static enum hrtimer_restart alarm_timer_triggered(struct hrtimer *timer) |
| 353 | { |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 354 | struct alarm_queue *base; |
| 355 | struct alarm *alarm; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 356 | unsigned long flags; |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 357 | ktime_t now; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 358 | |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 359 | spin_lock_irqsave(&alarm_slock, flags); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 360 | |
| 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åg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 386 | } |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 387 | 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åg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 390 | spin_unlock_irqrestore(&alarm_slock, flags); |
| 391 | return HRTIMER_NORESTART; |
| 392 | } |
| 393 | |
| 394 | static 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åg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 399 | pr_alarm(INT, "rtc alarm triggered\n"); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 400 | wake_lock_timeout(&alarm_rtc_wake_lock, 1 * HZ); |
| 401 | } |
| 402 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 403 | 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] | 404 | { |
| 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åg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 411 | struct timespec rtc_delta; |
Arve Hjønnevåg | b2a4ab2 | 2010-03-12 20:05:32 -0800 | [diff] [blame] | 412 | struct timespec wall_time; |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 413 | struct alarm_queue *wakeup_queue = NULL; |
| 414 | struct alarm_queue *tmp_queue = NULL; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 415 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 416 | pr_alarm(SUSPEND, "alarm_suspend(%p, %d)\n", pdev, state.event); |
| 417 | |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 418 | spin_lock_irqsave(&alarm_slock, flags); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 419 | suspended = true; |
| 420 | spin_unlock_irqrestore(&alarm_slock, flags); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 421 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 422 | hrtimer_cancel(&alarms[ANDROID_ALARM_RTC_WAKEUP].timer); |
| 423 | hrtimer_cancel(&alarms[ |
JP Abgrall | 692e468 | 2011-09-02 18:14:12 -0700 | [diff] [blame] | 424 | ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP].timer); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 425 | |
| 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åg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 435 | rtc_read_time(alarm_rtc_dev, &rtc_current_rtc_time); |
Arve Hjønnevåg | b2a4ab2 | 2010-03-12 20:05:32 -0800 | [diff] [blame] | 436 | 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åg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 441 | |
| 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åg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 446 | 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åg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 451 | pr_alarm(SUSPEND, |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 452 | "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åg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 456 | pr_alarm(SUSPEND, "alarm about to go off\n"); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 457 | 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åg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 462 | suspended = false; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 463 | wake_lock_timeout(&alarm_rtc_wake_lock, 2 * HZ); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 464 | 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åg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 468 | err = -EBUSY; |
| 469 | spin_unlock_irqrestore(&alarm_slock, flags); |
| 470 | } |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 471 | } |
| 472 | return err; |
| 473 | } |
| 474 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 475 | static int alarm_resume(struct platform_device *pdev) |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 476 | { |
| 477 | struct rtc_wkalrm alarm; |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 478 | 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åg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 493 | return 0; |
| 494 | } |
| 495 | |
Ashay Jaiswal | c1b2e47 | 2013-05-17 15:15:02 +0530 | [diff] [blame^] | 496 | static 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 | |
| 530 | disable_alarm: |
| 531 | rtc_alarm_irq_enable(alarm_rtc_dev, 0); |
| 532 | spin_unlock_irqrestore(&alarm_slock, flags); |
| 533 | } |
| 534 | |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 535 | static struct rtc_task alarm_rtc_task = { |
| 536 | .func = alarm_triggered_func |
| 537 | }; |
| 538 | |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 539 | static 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åg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 552 | 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åg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 562 | 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] | 563 | mutex_unlock(&alarm_setrtc_mutex); |
| 564 | |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 565 | return 0; |
| 566 | |
| 567 | err3: |
| 568 | platform_device_unregister(alarm_platform_dev); |
| 569 | err2: |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 570 | err1: |
| 571 | mutex_unlock(&alarm_setrtc_mutex); |
| 572 | return err; |
| 573 | } |
| 574 | |
| 575 | static 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åg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 579 | pr_alarm(INIT_STATUS, "lost rtc device for alarms"); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 580 | rtc_irq_unregister(alarm_rtc_dev, &alarm_rtc_task); |
| 581 | platform_device_unregister(alarm_platform_dev); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 582 | alarm_rtc_dev = NULL; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | static struct class_interface rtc_alarm_interface = { |
| 587 | .add_dev = &rtc_alarm_add_device, |
| 588 | .remove_dev = &rtc_alarm_remove_device, |
| 589 | }; |
| 590 | |
| 591 | static struct platform_driver alarm_driver = { |
| 592 | .suspend = alarm_suspend, |
| 593 | .resume = alarm_resume, |
Ashay Jaiswal | c1b2e47 | 2013-05-17 15:15:02 +0530 | [diff] [blame^] | 594 | .shutdown = alarm_shutdown, |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 595 | .driver = { |
| 596 | .name = "alarm" |
| 597 | } |
| 598 | }; |
| 599 | |
| 600 | static int __init alarm_late_init(void) |
| 601 | { |
| 602 | unsigned long flags; |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 603 | struct timespec tmp_time, system_time; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 604 | |
| 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åg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 611 | getnstimeofday(&tmp_time); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 612 | ktime_get_ts(&system_time); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 613 | 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åg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 616 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 617 | spin_unlock_irqrestore(&alarm_slock, flags); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 618 | return 0; |
| 619 | } |
| 620 | |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 621 | static int __init alarm_driver_init(void) |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 622 | { |
| 623 | int err; |
| 624 | int i; |
| 625 | |
| 626 | for (i = 0; i < ANDROID_ALARM_SYSTEMTIME; i++) { |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 627 | hrtimer_init(&alarms[i].timer, |
| 628 | CLOCK_REALTIME, HRTIMER_MODE_ABS); |
| 629 | alarms[i].timer.function = alarm_timer_triggered; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 630 | } |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 631 | hrtimer_init(&alarms[ANDROID_ALARM_SYSTEMTIME].timer, |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 632 | CLOCK_MONOTONIC, HRTIMER_MODE_ABS); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 633 | alarms[ANDROID_ALARM_SYSTEMTIME].timer.function = alarm_timer_triggered; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 634 | err = platform_driver_register(&alarm_driver); |
| 635 | if (err < 0) |
| 636 | goto err1; |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 637 | 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 | |
| 645 | err2: |
| 646 | wake_lock_destroy(&alarm_rtc_wake_lock); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 647 | platform_driver_unregister(&alarm_driver); |
| 648 | err1: |
| 649 | return err; |
| 650 | } |
| 651 | |
| 652 | static void __exit alarm_exit(void) |
| 653 | { |
| 654 | class_interface_unregister(&rtc_alarm_interface); |
| 655 | wake_lock_destroy(&alarm_rtc_wake_lock); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 656 | platform_driver_unregister(&alarm_driver); |
| 657 | } |
| 658 | |
| 659 | late_initcall(alarm_late_init); |
Arve Hjønnevåg | fa56540 | 2009-05-04 14:09:15 -0700 | [diff] [blame] | 660 | module_init(alarm_driver_init); |
Arve Hjønnevåg | b54b33d | 2008-10-14 17:38:04 -0700 | [diff] [blame] | 661 | module_exit(alarm_exit); |
| 662 | |