alarmtimer.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * Alarmtimer interface
  3. *
  4. * This interface provides a timer which is similarto hrtimers,
  5. * but triggers a RTC alarm if the box is suspend.
  6. *
  7. * This interface is influenced by the Android RTC Alarm timer
  8. * interface.
  9. *
  10. * Copyright (C) 2010 IBM Corperation
  11. *
  12. * Author: John Stultz <john.stultz@linaro.org>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. */
  18. #include <linux/time.h>
  19. #include <linux/hrtimer.h>
  20. #include <linux/timerqueue.h>
  21. #include <linux/rtc.h>
  22. #include <linux/alarmtimer.h>
  23. #include <linux/mutex.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/posix-timers.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/freezer.h>
  28. static struct alarm_base {
  29. spinlock_t lock;
  30. struct timerqueue_head timerqueue;
  31. struct hrtimer timer;
  32. ktime_t (*gettime)(void);
  33. clockid_t base_clockid;
  34. struct work_struct irqwork;
  35. } alarm_bases[ALARM_NUMTYPE];
  36. static struct rtc_timer rtctimer;
  37. static struct rtc_device *rtcdev;
  38. static ktime_t freezer_delta;
  39. static DEFINE_SPINLOCK(freezer_delta_lock);
  40. /**************************************************************************
  41. * alarmtimer management code
  42. */
  43. /*
  44. * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
  45. * @base: pointer to the base where the timer is being run
  46. * @alarm: pointer to alarm being enqueued.
  47. *
  48. * Adds alarm to a alarm_base timerqueue and if necessary sets
  49. * an hrtimer to run.
  50. *
  51. * Must hold base->lock when calling.
  52. */
  53. static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
  54. {
  55. timerqueue_add(&base->timerqueue, &alarm->node);
  56. if (&alarm->node == timerqueue_getnext(&base->timerqueue)) {
  57. hrtimer_try_to_cancel(&base->timer);
  58. hrtimer_start(&base->timer, alarm->node.expires,
  59. HRTIMER_MODE_ABS);
  60. }
  61. }
  62. /*
  63. * alarmtimer_remove - Removes an alarm timer from an alarm_base timerqueue
  64. * @base: pointer to the base where the timer is running
  65. * @alarm: pointer to alarm being removed
  66. *
  67. * Removes alarm to a alarm_base timerqueue and if necessary sets
  68. * a new timer to run.
  69. *
  70. * Must hold base->lock when calling.
  71. */
  72. static void alarmtimer_remove(struct alarm_base *base, struct alarm *alarm)
  73. {
  74. struct timerqueue_node *next = timerqueue_getnext(&base->timerqueue);
  75. timerqueue_del(&base->timerqueue, &alarm->node);
  76. if (next == &alarm->node) {
  77. hrtimer_try_to_cancel(&base->timer);
  78. next = timerqueue_getnext(&base->timerqueue);
  79. if (!next)
  80. return;
  81. hrtimer_start(&base->timer, next->expires, HRTIMER_MODE_ABS);
  82. }
  83. }
  84. /*
  85. * alarmtimer_do_work - Handles alarm being fired.
  86. * @work: pointer to workqueue being run
  87. *
  88. * When a timer fires, this runs through the timerqueue to see
  89. * which alarm timers, and run those that expired. If there are
  90. * more alarm timers queued, we set the hrtimer to fire in the
  91. * future.
  92. */
  93. void alarmtimer_do_work(struct work_struct *work)
  94. {
  95. struct alarm_base *base = container_of(work, struct alarm_base,
  96. irqwork);
  97. struct timerqueue_node *next;
  98. unsigned long flags;
  99. ktime_t now;
  100. spin_lock_irqsave(&base->lock, flags);
  101. now = base->gettime();
  102. while ((next = timerqueue_getnext(&base->timerqueue))) {
  103. struct alarm *alarm;
  104. ktime_t expired = next->expires;
  105. if (expired.tv64 >= now.tv64)
  106. break;
  107. alarm = container_of(next, struct alarm, node);
  108. timerqueue_del(&base->timerqueue, &alarm->node);
  109. alarm->enabled = 0;
  110. /* Re-add periodic timers */
  111. if (alarm->period.tv64) {
  112. alarm->node.expires = ktime_add(expired, alarm->period);
  113. timerqueue_add(&base->timerqueue, &alarm->node);
  114. alarm->enabled = 1;
  115. }
  116. spin_unlock_irqrestore(&base->lock, flags);
  117. if (alarm->function)
  118. alarm->function(alarm);
  119. spin_lock_irqsave(&base->lock, flags);
  120. }
  121. if (next) {
  122. hrtimer_start(&base->timer, next->expires,
  123. HRTIMER_MODE_ABS);
  124. }
  125. spin_unlock_irqrestore(&base->lock, flags);
  126. }
  127. /*
  128. * alarmtimer_fired - Handles alarm hrtimer being fired.
  129. * @timer: pointer to hrtimer being run
  130. *
  131. * When a timer fires, this schedules the do_work function to
  132. * be run.
  133. */
  134. static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
  135. {
  136. struct alarm_base *base = container_of(timer, struct alarm_base, timer);
  137. schedule_work(&base->irqwork);
  138. return HRTIMER_NORESTART;
  139. }
  140. /*
  141. * alarmtimer_suspend - Suspend time callback
  142. * @dev: unused
  143. * @state: unused
  144. *
  145. * When we are going into suspend, we look through the bases
  146. * to see which is the soonest timer to expire. We then
  147. * set an rtc timer to fire that far into the future, which
  148. * will wake us from suspend.
  149. */
  150. static int alarmtimer_suspend(struct device *dev)
  151. {
  152. struct rtc_time tm;
  153. ktime_t min, now;
  154. unsigned long flags;
  155. int i;
  156. spin_lock_irqsave(&freezer_delta_lock, flags);
  157. min = freezer_delta;
  158. freezer_delta = ktime_set(0, 0);
  159. spin_unlock_irqrestore(&freezer_delta_lock, flags);
  160. /* If we have no rtcdev, just return */
  161. if (!rtcdev)
  162. return 0;
  163. /* Find the soonest timer to expire*/
  164. for (i = 0; i < ALARM_NUMTYPE; i++) {
  165. struct alarm_base *base = &alarm_bases[i];
  166. struct timerqueue_node *next;
  167. ktime_t delta;
  168. spin_lock_irqsave(&base->lock, flags);
  169. next = timerqueue_getnext(&base->timerqueue);
  170. spin_unlock_irqrestore(&base->lock, flags);
  171. if (!next)
  172. continue;
  173. delta = ktime_sub(next->expires, base->gettime());
  174. if (!min.tv64 || (delta.tv64 < min.tv64))
  175. min = delta;
  176. }
  177. if (min.tv64 == 0)
  178. return 0;
  179. /* XXX - Should we enforce a minimum sleep time? */
  180. WARN_ON(min.tv64 < NSEC_PER_SEC);
  181. /* Setup an rtc timer to fire that far in the future */
  182. rtc_timer_cancel(rtcdev, &rtctimer);
  183. rtc_read_time(rtcdev, &tm);
  184. now = rtc_tm_to_ktime(tm);
  185. now = ktime_add(now, min);
  186. rtc_timer_start(rtcdev, &rtctimer, now, ktime_set(0, 0));
  187. return 0;
  188. }
  189. /**************************************************************************
  190. * alarm kernel interface code
  191. */
  192. /*
  193. * alarm_init - Initialize an alarm structure
  194. * @alarm: ptr to alarm to be initialized
  195. * @type: the type of the alarm
  196. * @function: callback that is run when the alarm fires
  197. *
  198. * In-kernel interface to initializes the alarm structure.
  199. */
  200. void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
  201. void (*function)(struct alarm *))
  202. {
  203. timerqueue_init(&alarm->node);
  204. alarm->period = ktime_set(0, 0);
  205. alarm->function = function;
  206. alarm->type = type;
  207. alarm->enabled = 0;
  208. }
  209. /*
  210. * alarm_start - Sets an alarm to fire
  211. * @alarm: ptr to alarm to set
  212. * @start: time to run the alarm
  213. * @period: period at which the alarm will recur
  214. *
  215. * In-kernel interface set an alarm timer.
  216. */
  217. void alarm_start(struct alarm *alarm, ktime_t start, ktime_t period)
  218. {
  219. struct alarm_base *base = &alarm_bases[alarm->type];
  220. unsigned long flags;
  221. spin_lock_irqsave(&base->lock, flags);
  222. if (alarm->enabled)
  223. alarmtimer_remove(base, alarm);
  224. alarm->node.expires = start;
  225. alarm->period = period;
  226. alarmtimer_enqueue(base, alarm);
  227. alarm->enabled = 1;
  228. spin_unlock_irqrestore(&base->lock, flags);
  229. }
  230. /*
  231. * alarm_cancel - Tries to cancel an alarm timer
  232. * @alarm: ptr to alarm to be canceled
  233. *
  234. * In-kernel interface to cancel an alarm timer.
  235. */
  236. void alarm_cancel(struct alarm *alarm)
  237. {
  238. struct alarm_base *base = &alarm_bases[alarm->type];
  239. unsigned long flags;
  240. spin_lock_irqsave(&base->lock, flags);
  241. if (alarm->enabled)
  242. alarmtimer_remove(base, alarm);
  243. alarm->enabled = 0;
  244. spin_unlock_irqrestore(&base->lock, flags);
  245. }
  246. /**************************************************************************
  247. * alarmtimer initialization code
  248. */
  249. /* Suspend hook structures */
  250. static const struct dev_pm_ops alarmtimer_pm_ops = {
  251. .suspend = alarmtimer_suspend,
  252. };
  253. static struct platform_driver alarmtimer_driver = {
  254. .driver = {
  255. .name = "alarmtimer",
  256. .pm = &alarmtimer_pm_ops,
  257. }
  258. };
  259. /**
  260. * alarmtimer_init - Initialize alarm timer code
  261. *
  262. * This function initializes the alarm bases and registers
  263. * the posix clock ids.
  264. */
  265. static int __init alarmtimer_init(void)
  266. {
  267. int error = 0;
  268. int i;
  269. /* Initialize alarm bases */
  270. alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
  271. alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
  272. alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
  273. alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
  274. for (i = 0; i < ALARM_NUMTYPE; i++) {
  275. timerqueue_init_head(&alarm_bases[i].timerqueue);
  276. spin_lock_init(&alarm_bases[i].lock);
  277. hrtimer_init(&alarm_bases[i].timer,
  278. alarm_bases[i].base_clockid,
  279. HRTIMER_MODE_ABS);
  280. alarm_bases[i].timer.function = alarmtimer_fired;
  281. INIT_WORK(&alarm_bases[i].irqwork, alarmtimer_do_work);
  282. }
  283. error = platform_driver_register(&alarmtimer_driver);
  284. platform_device_register_simple("alarmtimer", -1, NULL, 0);
  285. return error;
  286. }
  287. device_initcall(alarmtimer_init);
  288. /**
  289. * has_wakealarm - check rtc device has wakealarm ability
  290. * @dev: current device
  291. * @name_ptr: name to be returned
  292. *
  293. * This helper function checks to see if the rtc device can wake
  294. * from suspend.
  295. */
  296. static int __init has_wakealarm(struct device *dev, void *name_ptr)
  297. {
  298. struct rtc_device *candidate = to_rtc_device(dev);
  299. if (!candidate->ops->set_alarm)
  300. return 0;
  301. if (!device_may_wakeup(candidate->dev.parent))
  302. return 0;
  303. *(const char **)name_ptr = dev_name(dev);
  304. return 1;
  305. }
  306. /**
  307. * alarmtimer_init_late - Late initializing of alarmtimer code
  308. *
  309. * This function locates a rtc device to use for wakealarms.
  310. * Run as late_initcall to make sure rtc devices have been
  311. * registered.
  312. */
  313. static int __init alarmtimer_init_late(void)
  314. {
  315. char *str;
  316. /* Find an rtc device and init the rtc_timer */
  317. class_find_device(rtc_class, NULL, &str, has_wakealarm);
  318. if (str)
  319. rtcdev = rtc_class_open(str);
  320. if (!rtcdev) {
  321. printk(KERN_WARNING "No RTC device found, ALARM timers will"
  322. " not wake from suspend");
  323. }
  324. rtc_timer_init(&rtctimer, NULL, NULL);
  325. return 0;
  326. }
  327. late_initcall(alarmtimer_init_late);