rtc_kern.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020 Intel Corporation
  4. * Author: Johannes Berg <[email protected]>
  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. unsigned long long secs;
  45. if (!enable && !uml_rtc_alarm_enabled)
  46. return 0;
  47. uml_rtc_alarm_enabled = enable;
  48. secs = uml_rtc_alarm_time - ktime_get_real_seconds();
  49. if (time_travel_mode == TT_MODE_OFF) {
  50. if (!enable) {
  51. uml_rtc_disable_alarm();
  52. return 0;
  53. }
  54. /* enable or update */
  55. return uml_rtc_enable_alarm(secs);
  56. } else {
  57. time_travel_del_event(&uml_rtc_alarm_event);
  58. if (enable)
  59. time_travel_add_event_rel(&uml_rtc_alarm_event,
  60. secs * NSEC_PER_SEC);
  61. }
  62. return 0;
  63. }
  64. static int uml_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  65. {
  66. uml_rtc_alarm_irq_enable(dev, 0);
  67. uml_rtc_alarm_time = rtc_tm_to_time64(&alrm->time);
  68. uml_rtc_alarm_irq_enable(dev, alrm->enabled);
  69. return 0;
  70. }
  71. static const struct rtc_class_ops uml_rtc_ops = {
  72. .read_time = uml_rtc_read_time,
  73. .read_alarm = uml_rtc_read_alarm,
  74. .alarm_irq_enable = uml_rtc_alarm_irq_enable,
  75. .set_alarm = uml_rtc_set_alarm,
  76. };
  77. static irqreturn_t uml_rtc_interrupt(int irq, void *data)
  78. {
  79. unsigned long long c = 0;
  80. /* alarm triggered, it's now off */
  81. uml_rtc_alarm_enabled = false;
  82. os_read_file(uml_rtc_irq_fd, &c, sizeof(c));
  83. WARN_ON(c == 0);
  84. pm_system_wakeup();
  85. rtc_update_irq(uml_rtc, 1, RTC_IRQF | RTC_AF);
  86. return IRQ_HANDLED;
  87. }
  88. static int uml_rtc_setup(void)
  89. {
  90. int err;
  91. err = uml_rtc_start(time_travel_mode != TT_MODE_OFF);
  92. if (WARN(err < 0, "err = %d\n", err))
  93. return err;
  94. uml_rtc_irq_fd = err;
  95. err = um_request_irq(UM_IRQ_ALLOC, uml_rtc_irq_fd, IRQ_READ,
  96. uml_rtc_interrupt, 0, "rtc", NULL);
  97. if (err < 0) {
  98. uml_rtc_stop(time_travel_mode != TT_MODE_OFF);
  99. return err;
  100. }
  101. irq_set_irq_wake(err, 1);
  102. uml_rtc_irq = err;
  103. return 0;
  104. }
  105. static void uml_rtc_cleanup(void)
  106. {
  107. um_free_irq(uml_rtc_irq, NULL);
  108. uml_rtc_stop(time_travel_mode != TT_MODE_OFF);
  109. }
  110. static int uml_rtc_probe(struct platform_device *pdev)
  111. {
  112. int err;
  113. err = uml_rtc_setup();
  114. if (err)
  115. return err;
  116. uml_rtc = devm_rtc_allocate_device(&pdev->dev);
  117. if (IS_ERR(uml_rtc)) {
  118. err = PTR_ERR(uml_rtc);
  119. goto cleanup;
  120. }
  121. uml_rtc->ops = &uml_rtc_ops;
  122. device_init_wakeup(&pdev->dev, 1);
  123. err = devm_rtc_register_device(uml_rtc);
  124. if (err)
  125. goto cleanup;
  126. return 0;
  127. cleanup:
  128. uml_rtc_cleanup();
  129. return err;
  130. }
  131. static int uml_rtc_remove(struct platform_device *pdev)
  132. {
  133. device_init_wakeup(&pdev->dev, 0);
  134. uml_rtc_cleanup();
  135. return 0;
  136. }
  137. static struct platform_driver uml_rtc_driver = {
  138. .probe = uml_rtc_probe,
  139. .remove = uml_rtc_remove,
  140. .driver = {
  141. .name = "uml-rtc",
  142. },
  143. };
  144. static int __init uml_rtc_init(void)
  145. {
  146. struct platform_device *pdev;
  147. int err;
  148. err = platform_driver_register(&uml_rtc_driver);
  149. if (err)
  150. return err;
  151. pdev = platform_device_alloc("uml-rtc", 0);
  152. if (!pdev) {
  153. err = -ENOMEM;
  154. goto unregister;
  155. }
  156. err = platform_device_add(pdev);
  157. if (err)
  158. goto unregister;
  159. return 0;
  160. unregister:
  161. platform_device_put(pdev);
  162. platform_driver_unregister(&uml_rtc_driver);
  163. return err;
  164. }
  165. device_initcall(uml_rtc_init);