rtc_kern.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020 Intel Corporation
  4. * Author: Johannes Berg <johannes@sipsolutions.net>
  5. */
  6. #include <linux/platform_device.h>
  7. #include <linux/time-internal.h>
  8. #include <linux/suspend.h>
  9. #include <linux/err.h>
  10. #include <linux/rtc.h>
  11. #include <kern_util.h>
  12. #include <irq_kern.h>
  13. #include <os.h>
  14. #include "rtc.h"
  15. static time64_t uml_rtc_alarm_time;
  16. static bool uml_rtc_alarm_enabled;
  17. static struct rtc_device *uml_rtc;
  18. static int uml_rtc_irq_fd, uml_rtc_irq;
  19. #ifdef CONFIG_UML_TIME_TRAVEL_SUPPORT
  20. static void uml_rtc_time_travel_alarm(struct time_travel_event *ev)
  21. {
  22. uml_rtc_send_timetravel_alarm();
  23. }
  24. static struct time_travel_event uml_rtc_alarm_event = {
  25. .fn = uml_rtc_time_travel_alarm,
  26. };
  27. #endif
  28. static int uml_rtc_read_time(struct device *dev, struct rtc_time *tm)
  29. {
  30. struct timespec64 ts;
  31. /* Use this to get correct time in time-travel mode */
  32. read_persistent_clock64(&ts);
  33. rtc_time64_to_tm(timespec64_to_ktime(ts) / NSEC_PER_SEC, tm);
  34. return 0;
  35. }
  36. static int uml_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  37. {
  38. rtc_time64_to_tm(uml_rtc_alarm_time, &alrm->time);
  39. alrm->enabled = uml_rtc_alarm_enabled;
  40. return 0;
  41. }
  42. static int uml_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
  43. {
  44. struct timespec64 ts;
  45. unsigned long long secs;
  46. if (!enable && !uml_rtc_alarm_enabled)
  47. return 0;
  48. uml_rtc_alarm_enabled = enable;
  49. read_persistent_clock64(&ts);
  50. secs = uml_rtc_alarm_time - ts.tv_sec;
  51. if (time_travel_mode == TT_MODE_OFF) {
  52. if (!enable) {
  53. uml_rtc_disable_alarm();
  54. return 0;
  55. }
  56. /* enable or update */
  57. return uml_rtc_enable_alarm(secs);
  58. } else {
  59. time_travel_del_event(&uml_rtc_alarm_event);
  60. if (enable)
  61. time_travel_add_event_rel(&uml_rtc_alarm_event,
  62. secs * NSEC_PER_SEC -
  63. ts.tv_nsec);
  64. }
  65. return 0;
  66. }
  67. static int uml_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  68. {
  69. uml_rtc_alarm_irq_enable(dev, 0);
  70. uml_rtc_alarm_time = rtc_tm_to_time64(&alrm->time);
  71. uml_rtc_alarm_irq_enable(dev, alrm->enabled);
  72. return 0;
  73. }
  74. static const struct rtc_class_ops uml_rtc_ops = {
  75. .read_time = uml_rtc_read_time,
  76. .read_alarm = uml_rtc_read_alarm,
  77. .alarm_irq_enable = uml_rtc_alarm_irq_enable,
  78. .set_alarm = uml_rtc_set_alarm,
  79. };
  80. static irqreturn_t uml_rtc_interrupt(int irq, void *data)
  81. {
  82. unsigned long long c = 0;
  83. /* alarm triggered, it's now off */
  84. uml_rtc_alarm_enabled = false;
  85. os_read_file(uml_rtc_irq_fd, &c, sizeof(c));
  86. WARN_ON(c == 0);
  87. pm_system_wakeup();
  88. rtc_update_irq(uml_rtc, 1, RTC_IRQF | RTC_AF);
  89. return IRQ_HANDLED;
  90. }
  91. static int uml_rtc_setup(void)
  92. {
  93. int err;
  94. err = uml_rtc_start(time_travel_mode != TT_MODE_OFF);
  95. if (WARN(err < 0, "err = %d\n", err))
  96. return err;
  97. uml_rtc_irq_fd = err;
  98. err = um_request_irq(UM_IRQ_ALLOC, uml_rtc_irq_fd, IRQ_READ,
  99. uml_rtc_interrupt, 0, "rtc", NULL);
  100. if (err < 0) {
  101. uml_rtc_stop(time_travel_mode != TT_MODE_OFF);
  102. return err;
  103. }
  104. irq_set_irq_wake(err, 1);
  105. uml_rtc_irq = err;
  106. return 0;
  107. }
  108. static void uml_rtc_cleanup(void)
  109. {
  110. um_free_irq(uml_rtc_irq, NULL);
  111. uml_rtc_stop(time_travel_mode != TT_MODE_OFF);
  112. }
  113. static int uml_rtc_probe(struct platform_device *pdev)
  114. {
  115. int err;
  116. err = uml_rtc_setup();
  117. if (err)
  118. return err;
  119. uml_rtc = devm_rtc_allocate_device(&pdev->dev);
  120. if (IS_ERR(uml_rtc)) {
  121. err = PTR_ERR(uml_rtc);
  122. goto cleanup;
  123. }
  124. uml_rtc->ops = &uml_rtc_ops;
  125. device_init_wakeup(&pdev->dev, 1);
  126. err = devm_rtc_register_device(uml_rtc);
  127. if (err)
  128. goto cleanup;
  129. return 0;
  130. cleanup:
  131. uml_rtc_cleanup();
  132. return err;
  133. }
  134. static void uml_rtc_remove(struct platform_device *pdev)
  135. {
  136. device_init_wakeup(&pdev->dev, 0);
  137. uml_rtc_cleanup();
  138. }
  139. static struct platform_driver uml_rtc_driver = {
  140. .probe = uml_rtc_probe,
  141. .remove = uml_rtc_remove,
  142. .driver = {
  143. .name = "uml-rtc",
  144. },
  145. };
  146. static int __init uml_rtc_init(void)
  147. {
  148. struct platform_device *pdev;
  149. int err;
  150. err = platform_driver_register(&uml_rtc_driver);
  151. if (err)
  152. return err;
  153. pdev = platform_device_alloc("uml-rtc", 0);
  154. if (!pdev) {
  155. err = -ENOMEM;
  156. goto unregister;
  157. }
  158. err = platform_device_add(pdev);
  159. if (err)
  160. goto unregister;
  161. return 0;
  162. unregister:
  163. platform_device_put(pdev);
  164. platform_driver_unregister(&uml_rtc_driver);
  165. return err;
  166. }
  167. device_initcall(uml_rtc_init);