rtc-efi.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * rtc-efi: RTC Class Driver for EFI-based systems
  4. *
  5. * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
  6. *
  7. * Author: dann frazier <dannf@dannf.org>
  8. * Based on efirtc.c by Stephane Eranian
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/stringify.h>
  14. #include <linux/time.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/rtc.h>
  17. #include <linux/efi.h>
  18. #define EFI_ISDST (EFI_TIME_ADJUST_DAYLIGHT|EFI_TIME_IN_DAYLIGHT)
  19. /*
  20. * returns day of the year [0-365]
  21. */
  22. static inline int
  23. compute_yday(efi_time_t *eft)
  24. {
  25. /* efi_time_t.month is in the [1-12] so, we need -1 */
  26. return rtc_year_days(eft->day, eft->month - 1, eft->year);
  27. }
  28. /*
  29. * returns day of the week [0-6] 0=Sunday
  30. */
  31. static int
  32. compute_wday(efi_time_t *eft, int yday)
  33. {
  34. int ndays = eft->year * (365 % 7)
  35. + (eft->year - 1) / 4
  36. - (eft->year - 1) / 100
  37. + (eft->year - 1) / 400
  38. + yday;
  39. /*
  40. * 1/1/0000 may or may not have been a Sunday (if it ever existed at
  41. * all) but assuming it was makes this calculation work correctly.
  42. */
  43. return ndays % 7;
  44. }
  45. static void
  46. convert_to_efi_time(struct rtc_time *wtime, efi_time_t *eft)
  47. {
  48. eft->year = wtime->tm_year + 1900;
  49. eft->month = wtime->tm_mon + 1;
  50. eft->day = wtime->tm_mday;
  51. eft->hour = wtime->tm_hour;
  52. eft->minute = wtime->tm_min;
  53. eft->second = wtime->tm_sec;
  54. eft->nanosecond = 0;
  55. eft->daylight = wtime->tm_isdst ? EFI_ISDST : 0;
  56. eft->timezone = EFI_UNSPECIFIED_TIMEZONE;
  57. }
  58. static bool
  59. convert_from_efi_time(efi_time_t *eft, struct rtc_time *wtime)
  60. {
  61. memset(wtime, 0, sizeof(*wtime));
  62. if (eft->second >= 60)
  63. return false;
  64. wtime->tm_sec = eft->second;
  65. if (eft->minute >= 60)
  66. return false;
  67. wtime->tm_min = eft->minute;
  68. if (eft->hour >= 24)
  69. return false;
  70. wtime->tm_hour = eft->hour;
  71. if (!eft->day || eft->day > 31)
  72. return false;
  73. wtime->tm_mday = eft->day;
  74. if (!eft->month || eft->month > 12)
  75. return false;
  76. wtime->tm_mon = eft->month - 1;
  77. if (eft->year < 1900 || eft->year > 9999)
  78. return false;
  79. wtime->tm_year = eft->year - 1900;
  80. /* day in the year [1-365]*/
  81. wtime->tm_yday = compute_yday(eft);
  82. /* day of the week [0-6], Sunday=0 */
  83. wtime->tm_wday = compute_wday(eft, wtime->tm_yday);
  84. switch (eft->daylight & EFI_ISDST) {
  85. case EFI_ISDST:
  86. wtime->tm_isdst = 1;
  87. break;
  88. case EFI_TIME_ADJUST_DAYLIGHT:
  89. wtime->tm_isdst = 0;
  90. break;
  91. default:
  92. wtime->tm_isdst = -1;
  93. }
  94. return true;
  95. }
  96. static int efi_read_time(struct device *dev, struct rtc_time *tm)
  97. {
  98. efi_status_t status;
  99. efi_time_t eft;
  100. efi_time_cap_t cap;
  101. status = efi.get_time(&eft, &cap);
  102. if (status != EFI_SUCCESS) {
  103. /* should never happen */
  104. dev_err_once(dev, "can't read time\n");
  105. return -EINVAL;
  106. }
  107. if (!convert_from_efi_time(&eft, tm))
  108. return -EIO;
  109. return 0;
  110. }
  111. static int efi_set_time(struct device *dev, struct rtc_time *tm)
  112. {
  113. efi_status_t status;
  114. efi_time_t eft;
  115. convert_to_efi_time(tm, &eft);
  116. status = efi.set_time(&eft);
  117. return status == EFI_SUCCESS ? 0 : -EINVAL;
  118. }
  119. static int efi_procfs(struct device *dev, struct seq_file *seq)
  120. {
  121. efi_time_t eft;
  122. efi_time_cap_t cap;
  123. memset(&eft, 0, sizeof(eft));
  124. memset(&cap, 0, sizeof(cap));
  125. efi.get_time(&eft, &cap);
  126. seq_printf(seq,
  127. "Time\t\t: %u:%u:%u.%09u\n"
  128. "Date\t\t: %u-%u-%u\n"
  129. "Daylight\t: %u\n",
  130. eft.hour, eft.minute, eft.second, eft.nanosecond,
  131. eft.year, eft.month, eft.day,
  132. eft.daylight);
  133. if (eft.timezone == EFI_UNSPECIFIED_TIMEZONE)
  134. seq_puts(seq, "Timezone\t: unspecified\n");
  135. else
  136. /* XXX fixme: convert to string? */
  137. seq_printf(seq, "Timezone\t: %u\n", eft.timezone);
  138. /*
  139. * now prints the capabilities
  140. */
  141. seq_printf(seq,
  142. "Resolution\t: %u\n"
  143. "Accuracy\t: %u\n"
  144. "SetstoZero\t: %u\n",
  145. cap.resolution, cap.accuracy, cap.sets_to_zero);
  146. return 0;
  147. }
  148. static const struct rtc_class_ops efi_rtc_ops = {
  149. .read_time = efi_read_time,
  150. .set_time = efi_set_time,
  151. .proc = efi_procfs,
  152. };
  153. static int __init efi_rtc_probe(struct platform_device *dev)
  154. {
  155. struct rtc_device *rtc;
  156. efi_time_t eft;
  157. efi_time_cap_t cap;
  158. /* First check if the RTC is usable */
  159. if (efi.get_time(&eft, &cap) != EFI_SUCCESS)
  160. return -ENODEV;
  161. rtc = devm_rtc_allocate_device(&dev->dev);
  162. if (IS_ERR(rtc))
  163. return PTR_ERR(rtc);
  164. platform_set_drvdata(dev, rtc);
  165. rtc->ops = &efi_rtc_ops;
  166. clear_bit(RTC_FEATURE_ALARM, rtc->features);
  167. device_init_wakeup(&dev->dev, true);
  168. return devm_rtc_register_device(rtc);
  169. }
  170. static struct platform_driver efi_rtc_driver = {
  171. .driver = {
  172. .name = "rtc-efi",
  173. },
  174. };
  175. module_platform_driver_probe(efi_rtc_driver, efi_rtc_probe);
  176. MODULE_AUTHOR("dann frazier <dannf@dannf.org>");
  177. MODULE_LICENSE("GPL");
  178. MODULE_DESCRIPTION("EFI RTC driver");
  179. MODULE_ALIAS("platform:rtc-efi");