• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* drivers/rtc/alarm.c
2  *
3  * Copyright (C) 2007 Google, Inc.
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 
16 #include <asm/mach/time.h>
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/spinlock.h>
23 #include <linux/sysdev.h>
24 #include <linux/wakelock.h>
25 
26 #define ANDROID_ALARM_PRINT_ERRORS (1U << 0)
27 #define ANDROID_ALARM_PRINT_INIT_STATUS (1U << 1)
28 #define ANDROID_ALARM_PRINT_INFO (1U << 2)
29 #define ANDROID_ALARM_PRINT_IO (1U << 3)
30 #define ANDROID_ALARM_PRINT_INT (1U << 4)
31 #define ANDROID_ALARM_PRINT_FLOW (1U << 5)
32 
33 #if 0
34 #define ANDROID_ALARM_DPRINTF_MASK (~0)
35 #define ANDROID_ALARM_DPRINTF(debug_level_mask, args...) \
36 	do { \
37 		if (ANDROID_ALARM_DPRINTF_MASK & debug_level_mask) { \
38 			printk(args); \
39 		} \
40 	} while (0)
41 #else
42 #define ANDROID_ALARM_DPRINTF(args...)
43 #endif
44 
45 #define ANDROID_ALARM_WAKEUP_MASK ( \
46 	ANDROID_ALARM_RTC_WAKEUP_MASK | \
47 	ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP_MASK)
48 
49 /* support old usespace code */
50 #define ANDROID_ALARM_SET_OLD               _IOW('a', 2, time_t) /* set alarm */
51 #define ANDROID_ALARM_SET_AND_WAIT_OLD      _IOW('a', 3, time_t)
52 
53 static struct rtc_device *alarm_rtc_dev;
54 static int alarm_opened;
55 static DEFINE_SPINLOCK(alarm_slock);
56 static DEFINE_MUTEX(alarm_setrtc_mutex);
57 static struct wake_lock alarm_wake_lock;
58 static struct wake_lock alarm_rtc_wake_lock;
59 static DECLARE_WAIT_QUEUE_HEAD(alarm_wait_queue);
60 static uint32_t alarm_pending;
61 static uint32_t alarm_enabled;
62 static uint32_t wait_pending;
63 static struct platform_device *alarm_platform_dev;
64 static struct hrtimer alarm_timer[ANDROID_ALARM_TYPE_COUNT];
65 static struct timespec alarm_time[ANDROID_ALARM_TYPE_COUNT];
66 static struct timespec elapsed_rtc_delta;
67 
alarm_start_hrtimer(enum android_alarm_type alarm_type)68 static void alarm_start_hrtimer(enum android_alarm_type alarm_type)
69 {
70 	struct timespec hr_alarm_time;
71 	if (!(alarm_enabled & (1U << alarm_type)))
72 		return;
73 	hr_alarm_time = alarm_time[alarm_type];
74 	if (alarm_type == ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP ||
75 	    alarm_type == ANDROID_ALARM_ELAPSED_REALTIME)
76 		set_normalized_timespec(&hr_alarm_time,
77 			hr_alarm_time.tv_sec + elapsed_rtc_delta.tv_sec,
78 			hr_alarm_time.tv_nsec + elapsed_rtc_delta.tv_nsec);
79 	ANDROID_ALARM_DPRINTF(ANDROID_ALARM_PRINT_FLOW,
80 		"alarm start hrtimer %d at %ld.%09ld\n",
81 		alarm_type, hr_alarm_time.tv_sec, hr_alarm_time.tv_nsec);
82 	hrtimer_start(&alarm_timer[alarm_type],
83 		      timespec_to_ktime(hr_alarm_time), HRTIMER_MODE_ABS);
84 }
85 
alarm_ioctl(struct file * file,unsigned int cmd,unsigned long arg)86 static long alarm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
87 {
88 	int rv = 0;
89 	unsigned long flags;
90 	int i;
91 	struct timespec new_alarm_time;
92 	struct timespec new_rtc_time;
93 	struct timespec tmp_time;
94 	struct rtc_time rtc_new_rtc_time;
95 	enum android_alarm_type alarm_type = ANDROID_ALARM_IOCTL_TO_TYPE(cmd);
96 	uint32_t alarm_type_mask = 1U << alarm_type;
97 
98 	if (alarm_type >= ANDROID_ALARM_TYPE_COUNT)
99 		return -EINVAL;
100 
101 	if (ANDROID_ALARM_BASE_CMD(cmd) != ANDROID_ALARM_GET_TIME(0)) {
102 		if ((file->f_flags & O_ACCMODE) == O_RDONLY)
103 			return -EPERM;
104 		if (file->private_data == NULL &&
105 		    cmd != ANDROID_ALARM_SET_RTC) {
106 			spin_lock_irqsave(&alarm_slock, flags);
107 			if (alarm_opened) {
108 				spin_unlock_irqrestore(&alarm_slock, flags);
109 				return -EBUSY;
110 			}
111 			alarm_opened = 1;
112 			file->private_data = (void *)1;
113 			spin_unlock_irqrestore(&alarm_slock, flags);
114 		}
115 	}
116 
117 	switch (ANDROID_ALARM_BASE_CMD(cmd)) {
118 	case ANDROID_ALARM_CLEAR(0):
119 		spin_lock_irqsave(&alarm_slock, flags);
120 		ANDROID_ALARM_DPRINTF(ANDROID_ALARM_PRINT_IO,
121 				      "alarm %d clear\n", alarm_type);
122 		hrtimer_try_to_cancel(&alarm_timer[alarm_type]);
123 		if (alarm_pending) {
124 			alarm_pending &= ~alarm_type_mask;
125 			if (!alarm_pending && !wait_pending)
126 				wake_unlock(&alarm_wake_lock);
127 		}
128 		alarm_enabled &= ~alarm_type_mask;
129 		spin_unlock_irqrestore(&alarm_slock, flags);
130 		break;
131 
132 	case ANDROID_ALARM_SET_OLD:
133 	case ANDROID_ALARM_SET_AND_WAIT_OLD:
134 		if (get_user(new_alarm_time.tv_sec, (int __user *)arg)) {
135 			rv = -EFAULT;
136 			goto err1;
137 		}
138 		new_alarm_time.tv_nsec = 0;
139 		goto from_old_alarm_set;
140 
141 	case ANDROID_ALARM_SET_AND_WAIT(0):
142 	case ANDROID_ALARM_SET(0):
143 		if (copy_from_user(&new_alarm_time, (void __user *)arg,
144 		    sizeof(new_alarm_time))) {
145 			rv = -EFAULT;
146 			goto err1;
147 		}
148 from_old_alarm_set:
149 		spin_lock_irqsave(&alarm_slock, flags);
150 		ANDROID_ALARM_DPRINTF(ANDROID_ALARM_PRINT_IO,
151 			"alarm %d set %ld.%09ld\n", alarm_type,
152 			new_alarm_time.tv_sec, new_alarm_time.tv_nsec);
153 		alarm_time[alarm_type] = new_alarm_time;
154 		alarm_enabled |= alarm_type_mask;
155 		alarm_start_hrtimer(alarm_type);
156 		spin_unlock_irqrestore(&alarm_slock, flags);
157 		if (ANDROID_ALARM_BASE_CMD(cmd) != ANDROID_ALARM_SET_AND_WAIT(0)
158 		    && cmd != ANDROID_ALARM_SET_AND_WAIT_OLD)
159 			break;
160 		/* fall though */
161 	case ANDROID_ALARM_WAIT:
162 		spin_lock_irqsave(&alarm_slock, flags);
163 		ANDROID_ALARM_DPRINTF(ANDROID_ALARM_PRINT_IO, "alarm wait\n");
164 		if (!alarm_pending && wait_pending) {
165 			wake_unlock(&alarm_wake_lock);
166 			wait_pending = 0;
167 		}
168 		spin_unlock_irqrestore(&alarm_slock, flags);
169 		rv = wait_event_interruptible(alarm_wait_queue, alarm_pending);
170 		if (rv)
171 			goto err1;
172 		spin_lock_irqsave(&alarm_slock, flags);
173 		rv = alarm_pending;
174 		wait_pending = 1;
175 		alarm_pending = 0;
176 		if (rv & ANDROID_ALARM_WAKEUP_MASK)
177 			wake_unlock(&alarm_rtc_wake_lock);
178 		spin_unlock_irqrestore(&alarm_slock, flags);
179 		break;
180 	case ANDROID_ALARM_SET_RTC:
181 		if (copy_from_user(&new_rtc_time, (void __user *)arg,
182 		    sizeof(new_rtc_time))) {
183 			rv = -EFAULT;
184 			goto err1;
185 		}
186 		rtc_time_to_tm(new_rtc_time.tv_sec, &rtc_new_rtc_time);
187 
188 		ANDROID_ALARM_DPRINTF(ANDROID_ALARM_PRINT_IO,
189 			"set rtc %ld %ld - rtc %02d:%02d:%02d %02d/%02d/%04d\n",
190 			new_rtc_time.tv_sec, new_rtc_time.tv_nsec,
191 			rtc_new_rtc_time.tm_hour, rtc_new_rtc_time.tm_min,
192 			rtc_new_rtc_time.tm_sec, rtc_new_rtc_time.tm_mon + 1,
193 			rtc_new_rtc_time.tm_mday,
194 			rtc_new_rtc_time.tm_year + 1900);
195 
196 		mutex_lock(&alarm_setrtc_mutex);
197 		spin_lock_irqsave(&alarm_slock, flags);
198 		for (i = 0; i < ANDROID_ALARM_SYSTEMTIME; i++)
199 			hrtimer_try_to_cancel(&alarm_timer[i]);
200 		getnstimeofday(&tmp_time);
201 		elapsed_rtc_delta = timespec_sub(elapsed_rtc_delta,
202 					timespec_sub(tmp_time, new_rtc_time));
203 		spin_unlock_irqrestore(&alarm_slock, flags);
204 		rv = do_settimeofday(&new_rtc_time);
205 		spin_lock_irqsave(&alarm_slock, flags);
206 		for (i = 0; i < ANDROID_ALARM_SYSTEMTIME; i++)
207 			alarm_start_hrtimer(i);
208 		spin_unlock_irqrestore(&alarm_slock, flags);
209 		if (rv < 0) {
210 			ANDROID_ALARM_DPRINTF(ANDROID_ALARM_PRINT_ERRORS,
211 					      "Failed to set time\n");
212 			mutex_unlock(&alarm_setrtc_mutex);
213 			goto err1;
214 		}
215 		rv = rtc_set_time(alarm_rtc_dev, &rtc_new_rtc_time);
216 		spin_lock_irqsave(&alarm_slock, flags);
217 		alarm_pending |= ANDROID_ALARM_TIME_CHANGE_MASK;
218 		wake_up(&alarm_wait_queue);
219 		spin_unlock_irqrestore(&alarm_slock, flags);
220 		mutex_unlock(&alarm_setrtc_mutex);
221 		if (rv < 0) {
222 			ANDROID_ALARM_DPRINTF(ANDROID_ALARM_PRINT_ERRORS,
223 			    "Failed to set RTC, time will be lost on reboot\n");
224 			goto err1;
225 		}
226 		break;
227 	case ANDROID_ALARM_GET_TIME(0):
228 		mutex_lock(&alarm_setrtc_mutex);
229 		spin_lock_irqsave(&alarm_slock, flags);
230 		if (alarm_type != ANDROID_ALARM_SYSTEMTIME) {
231 			getnstimeofday(&tmp_time);
232 			if (alarm_type >= ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP)
233 				tmp_time = timespec_sub(tmp_time,
234 							elapsed_rtc_delta);
235 		} else
236 			ktime_get_ts(&tmp_time);
237 		spin_unlock_irqrestore(&alarm_slock, flags);
238 		mutex_unlock(&alarm_setrtc_mutex);
239 		if (copy_to_user((void __user *)arg, &tmp_time,
240 		    sizeof(tmp_time))) {
241 			rv = -EFAULT;
242 			goto err1;
243 		}
244 		break;
245 
246 	default:
247 		rv = -EINVAL;
248 		goto err1;
249 	}
250 err1:
251 	return rv;
252 }
253 
alarm_open(struct inode * inode,struct file * file)254 static int alarm_open(struct inode *inode, struct file *file)
255 {
256 	file->private_data = NULL;
257 	return 0;
258 }
259 
alarm_release(struct inode * inode,struct file * file)260 static int alarm_release(struct inode *inode, struct file *file)
261 {
262 	int i;
263 	unsigned long flags;
264 
265 	spin_lock_irqsave(&alarm_slock, flags);
266 	if (file->private_data != 0) {
267 		for (i = 0; i < ANDROID_ALARM_TYPE_COUNT; i++) {
268 			uint32_t alarm_type_mask = 1U << i;
269 			if (alarm_enabled & alarm_type_mask) {
270 				ANDROID_ALARM_DPRINTF(ANDROID_ALARM_PRINT_INFO,
271 					"alarm_release: clear alarm, "
272 					"pending %d\n",
273 					!!(alarm_pending & alarm_type_mask));
274 				alarm_enabled &= ~alarm_type_mask;
275 			}
276 			spin_unlock_irqrestore(&alarm_slock, flags);
277 			hrtimer_cancel(&alarm_timer[i]);
278 			spin_lock_irqsave(&alarm_slock, flags);
279 		}
280 		if (alarm_pending | wait_pending) {
281 			if (alarm_pending)
282 				ANDROID_ALARM_DPRINTF(ANDROID_ALARM_PRINT_INFO,
283 					"alarm_release: clear pending alarms "
284 					"%x\n", alarm_pending);
285 			wake_unlock(&alarm_wake_lock);
286 			wait_pending = 0;
287 			alarm_pending = 0;
288 		}
289 		alarm_opened = 0;
290 	}
291 	spin_unlock_irqrestore(&alarm_slock, flags);
292 	return 0;
293 }
294 
alarm_timer_triggered(struct hrtimer * timer)295 static enum hrtimer_restart alarm_timer_triggered(struct hrtimer *timer)
296 {
297 	unsigned long flags;
298 	enum android_alarm_type alarm_type = (timer - alarm_timer);
299 	uint32_t alarm_type_mask = 1U << alarm_type;
300 
301 
302 	ANDROID_ALARM_DPRINTF(ANDROID_ALARM_PRINT_INT,
303 			      "alarm_timer_triggered type %d\n", alarm_type);
304 	spin_lock_irqsave(&alarm_slock, flags);
305 	if (alarm_enabled & alarm_type_mask) {
306 		wake_lock_timeout(&alarm_wake_lock, 5 * HZ);
307 		alarm_enabled &= ~alarm_type_mask;
308 		alarm_pending |= alarm_type_mask;
309 		wake_up(&alarm_wait_queue);
310 	}
311 	spin_unlock_irqrestore(&alarm_slock, flags);
312 	return HRTIMER_NORESTART;
313 }
314 
alarm_triggered_func(void * p)315 static void alarm_triggered_func(void *p)
316 {
317 	struct rtc_device *rtc = alarm_rtc_dev;
318 	if (!(rtc->irq_data & RTC_AF))
319 		return;
320 	ANDROID_ALARM_DPRINTF(ANDROID_ALARM_PRINT_INT, "rtc alarm triggered\n");
321 	wake_lock_timeout(&alarm_rtc_wake_lock, 1 * HZ);
322 }
323 
alarm_suspend(struct platform_device * pdev,pm_message_t state)324 int alarm_suspend(struct platform_device *pdev, pm_message_t state)
325 {
326 	int                 err = 0;
327 	unsigned long       flags;
328 	struct rtc_wkalrm   rtc_alarm;
329 	struct rtc_time     rtc_current_rtc_time;
330 	unsigned long       rtc_current_time;
331 	unsigned long       rtc_alarm_time;
332 	struct timespec     rtc_current_timespec;
333 	struct timespec     rtc_delta;
334 	struct timespec     elapsed_realtime_alarm_time;
335 
336 	ANDROID_ALARM_DPRINTF(ANDROID_ALARM_PRINT_FLOW,
337 			      "alarm_suspend(%p, %d)\n", pdev, state.event);
338 	spin_lock_irqsave(&alarm_slock, flags);
339 	if (alarm_pending && !wake_lock_active(&alarm_wake_lock)) {
340 		ANDROID_ALARM_DPRINTF(ANDROID_ALARM_PRINT_INFO,
341 				      "alarm pending\n");
342 		err = -EBUSY;
343 		goto err1;
344 	}
345 	if (alarm_enabled & ANDROID_ALARM_WAKEUP_MASK) {
346 		spin_unlock_irqrestore(&alarm_slock, flags);
347 		if (alarm_enabled & ANDROID_ALARM_RTC_WAKEUP_MASK)
348 			hrtimer_cancel(&alarm_timer[ANDROID_ALARM_RTC_WAKEUP]);
349 		if (alarm_enabled & ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP_MASK)
350 			hrtimer_cancel(&alarm_timer[
351 					ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP]);
352 
353 		rtc_read_time(alarm_rtc_dev, &rtc_current_rtc_time);
354 		rtc_current_timespec.tv_nsec = 0;
355 		rtc_tm_to_time(&rtc_current_rtc_time,
356 			       &rtc_current_timespec.tv_sec);
357 		save_time_delta(&rtc_delta, &rtc_current_timespec);
358 		set_normalized_timespec(&elapsed_realtime_alarm_time,
359 			alarm_time[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP]
360 			.tv_sec + elapsed_rtc_delta.tv_sec,
361 			alarm_time[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP]
362 			.tv_nsec + elapsed_rtc_delta.tv_nsec);
363 		if ((alarm_enabled & ANDROID_ALARM_RTC_WAKEUP_MASK) &&
364 		    (!(alarm_enabled &
365 		       ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP_MASK) ||
366 		     timespec_compare(&alarm_time[ANDROID_ALARM_RTC_WAKEUP],
367 				      &elapsed_realtime_alarm_time) < 0))
368 			rtc_alarm_time = timespec_sub(
369 					alarm_time[ANDROID_ALARM_RTC_WAKEUP],
370 					rtc_delta).tv_sec;
371 		else
372 			rtc_alarm_time = timespec_sub(
373 				elapsed_realtime_alarm_time, rtc_delta).tv_sec;
374 		rtc_time_to_tm(rtc_alarm_time, &rtc_alarm.time);
375 		rtc_alarm.enabled = 1;
376 		rtc_set_alarm(alarm_rtc_dev, &rtc_alarm);
377 		rtc_read_time(alarm_rtc_dev, &rtc_current_rtc_time);
378 		rtc_tm_to_time(&rtc_current_rtc_time, &rtc_current_time);
379 		ANDROID_ALARM_DPRINTF(ANDROID_ALARM_PRINT_INFO,
380 			"rtc alarm set at %ld, now %ld, rtc delta %ld.%09ld\n",
381 			rtc_alarm_time, rtc_current_time,
382 			rtc_delta.tv_sec, rtc_delta.tv_nsec);
383 		if (rtc_current_time + 1 >= rtc_alarm_time) {
384 			ANDROID_ALARM_DPRINTF(ANDROID_ALARM_PRINT_INFO,
385 					      "alarm about to go off\n");
386 			memset(&rtc_alarm, 0, sizeof(rtc_alarm));
387 			rtc_alarm.enabled = 0;
388 			rtc_set_alarm(alarm_rtc_dev, &rtc_alarm);
389 
390 			spin_lock_irqsave(&alarm_slock, flags);
391 			wake_lock_timeout(&alarm_rtc_wake_lock, 2 * HZ);
392 			alarm_start_hrtimer(ANDROID_ALARM_RTC_WAKEUP);
393 			alarm_start_hrtimer(
394 				ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP);
395 			err = -EBUSY;
396 			spin_unlock_irqrestore(&alarm_slock, flags);
397 		}
398 	} else {
399 err1:
400 		spin_unlock_irqrestore(&alarm_slock, flags);
401 	}
402 	return err;
403 }
404 
alarm_resume(struct platform_device * pdev)405 int alarm_resume(struct platform_device *pdev)
406 {
407 	struct rtc_wkalrm alarm;
408 	ANDROID_ALARM_DPRINTF(ANDROID_ALARM_PRINT_FLOW,
409 			      "alarm_resume(%p)\n", pdev);
410 	if (alarm_enabled & ANDROID_ALARM_WAKEUP_MASK) {
411 		memset(&alarm, 0, sizeof(alarm));
412 		alarm.enabled = 0;
413 		rtc_set_alarm(alarm_rtc_dev, &alarm);
414 		alarm_start_hrtimer(ANDROID_ALARM_RTC_WAKEUP);
415 		alarm_start_hrtimer(ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP);
416 	}
417 	return 0;
418 }
419 
420 static struct rtc_task alarm_rtc_task = {
421 	.func = alarm_triggered_func
422 };
423 
424 static struct file_operations alarm_fops = {
425 	.owner = THIS_MODULE,
426 	.unlocked_ioctl = alarm_ioctl,
427 	.open = alarm_open,
428 	.release = alarm_release,
429 };
430 
431 static struct miscdevice alarm_device = {
432 	.minor = MISC_DYNAMIC_MINOR,
433 	.name = "alarm",
434 	.fops = &alarm_fops,
435 };
436 
rtc_alarm_add_device(struct device * dev,struct class_interface * class_intf)437 static int rtc_alarm_add_device(struct device *dev,
438 				struct class_interface *class_intf)
439 {
440 	int err;
441 	struct rtc_device *rtc = to_rtc_device(dev);
442 
443 	mutex_lock(&alarm_setrtc_mutex);
444 
445 	if (alarm_rtc_dev) {
446 		err = -EBUSY;
447 		goto err1;
448 	}
449 
450 	err = misc_register(&alarm_device);
451 	if (err)
452 		goto err1;
453 	alarm_platform_dev =
454 		platform_device_register_simple("alarm", -1, NULL, 0);
455 	if (IS_ERR(alarm_platform_dev)) {
456 		err = PTR_ERR(alarm_platform_dev);
457 		goto err2;
458 	}
459 	err = rtc_irq_register(rtc, &alarm_rtc_task);
460 	if (err)
461 		goto err3;
462 	alarm_rtc_dev = rtc;
463 	mutex_unlock(&alarm_setrtc_mutex);
464 
465 	ANDROID_ALARM_DPRINTF(ANDROID_ALARM_PRINT_INFO, "alarm: parent %p\n",
466 			      alarm_platform_dev->dev.power.pm_parent);
467 	return 0;
468 
469 err3:
470 	platform_device_unregister(alarm_platform_dev);
471 err2:
472 	misc_deregister(&alarm_device);
473 err1:
474 	mutex_unlock(&alarm_setrtc_mutex);
475 	return err;
476 }
477 
rtc_alarm_remove_device(struct device * dev,struct class_interface * class_intf)478 static void rtc_alarm_remove_device(struct device *dev,
479 				    struct class_interface *class_intf)
480 {
481 	if (dev == &alarm_rtc_dev->dev) {
482 		rtc_irq_unregister(alarm_rtc_dev, &alarm_rtc_task);
483 		platform_device_unregister(alarm_platform_dev);
484 		misc_deregister(&alarm_device);
485 		alarm_rtc_dev = NULL;
486 	}
487 }
488 
489 static struct class_interface rtc_alarm_interface = {
490 	.add_dev = &rtc_alarm_add_device,
491 	.remove_dev = &rtc_alarm_remove_device,
492 };
493 
494 static struct platform_driver alarm_driver = {
495 	.suspend = alarm_suspend,
496 	.resume = alarm_resume,
497 	.driver = {
498 		.name = "alarm"
499 	}
500 };
501 
alarm_late_init(void)502 static int __init alarm_late_init(void)
503 {
504 	unsigned long   flags;
505 	struct timespec system_time;
506 
507 	/* this needs to run after the rtc is read at boot */
508 	spin_lock_irqsave(&alarm_slock, flags);
509 	/* We read the current rtc and system time so we can later calulate
510 	 * elasped realtime to be (boot_systemtime + rtc - boot_rtc) ==
511 	 * (rtc - (boot_rtc - boot_systemtime))
512 	 */
513 	getnstimeofday(&elapsed_rtc_delta);
514 	ktime_get_ts(&system_time);
515 	elapsed_rtc_delta = timespec_sub(elapsed_rtc_delta, system_time);
516 	spin_unlock_irqrestore(&alarm_slock, flags);
517 
518 	ANDROID_ALARM_DPRINTF(ANDROID_ALARM_PRINT_INFO,
519 		"alarm_late_init: rtc to elapsed realtime delta %ld.%09ld\n",
520 		elapsed_rtc_delta.tv_sec, elapsed_rtc_delta.tv_nsec);
521 	return 0;
522 }
523 
alarm_init(void)524 static int __init alarm_init(void)
525 {
526 	int err;
527 	int i;
528 
529 	for (i = 0; i < ANDROID_ALARM_SYSTEMTIME; i++) {
530 		hrtimer_init(&alarm_timer[i], CLOCK_REALTIME, HRTIMER_MODE_ABS);
531 		alarm_timer[i].function = alarm_timer_triggered;
532 	}
533 	hrtimer_init(&alarm_timer[ANDROID_ALARM_SYSTEMTIME],
534 		     CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
535 	alarm_timer[ANDROID_ALARM_SYSTEMTIME].function = alarm_timer_triggered;
536 	err = platform_driver_register(&alarm_driver);
537 	if (err < 0)
538 		goto err1;
539 	wake_lock_init(&alarm_wake_lock, WAKE_LOCK_SUSPEND, "alarm");
540 	wake_lock_init(&alarm_rtc_wake_lock, WAKE_LOCK_SUSPEND, "alarm_rtc");
541 	rtc_alarm_interface.class = rtc_class;
542 	err = class_interface_register(&rtc_alarm_interface);
543 	if (err < 0)
544 		goto err2;
545 
546 	return 0;
547 
548 err2:
549 	wake_lock_destroy(&alarm_rtc_wake_lock);
550 	wake_lock_destroy(&alarm_wake_lock);
551 	platform_driver_unregister(&alarm_driver);
552 err1:
553 	return err;
554 }
555 
alarm_exit(void)556 static void  __exit alarm_exit(void)
557 {
558 	class_interface_unregister(&rtc_alarm_interface);
559 	wake_lock_destroy(&alarm_rtc_wake_lock);
560 	wake_lock_destroy(&alarm_wake_lock);
561 	platform_driver_unregister(&alarm_driver);
562 }
563 
564 late_initcall(alarm_late_init);
565 module_init(alarm_init);
566 module_exit(alarm_exit);
567 
568