rtc-efi.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * rtc-efi: RTC Class Driver for EFI-based systems
  3. *
  4. * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
  5. *
  6. * Author: dann frazier <dannf@dannf.org>
  7. * Based on efirtc.c by Stephane Eranian
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/stringify.h>
  19. #include <linux/time.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/rtc.h>
  22. #include <linux/efi.h>
  23. #define EFI_ISDST (EFI_TIME_ADJUST_DAYLIGHT|EFI_TIME_IN_DAYLIGHT)
  24. /*
  25. * returns day of the year [0-365]
  26. */
  27. static inline int
  28. compute_yday(efi_time_t *eft)
  29. {
  30. /* efi_time_t.month is in the [1-12] so, we need -1 */
  31. return rtc_year_days(eft->day, eft->month - 1, eft->year);
  32. }
  33. /*
  34. * returns day of the week [0-6] 0=Sunday
  35. */
  36. static int
  37. compute_wday(efi_time_t *eft, int yday)
  38. {
  39. int ndays = eft->year * (365 % 7)
  40. + (eft->year - 1) / 4
  41. - (eft->year - 1) / 100
  42. + (eft->year - 1) / 400
  43. + yday;
  44. /*
  45. * 1/1/0000 may or may not have been a Sunday (if it ever existed at
  46. * all) but assuming it was makes this calculation work correctly.
  47. */
  48. return ndays % 7;
  49. }
  50. static void
  51. convert_to_efi_time(struct rtc_time *wtime, efi_time_t *eft)
  52. {
  53. eft->year = wtime->tm_year + 1900;
  54. eft->month = wtime->tm_mon + 1;
  55. eft->day = wtime->tm_mday;
  56. eft->hour = wtime->tm_hour;
  57. eft->minute = wtime->tm_min;
  58. eft->second = wtime->tm_sec;
  59. eft->nanosecond = 0;
  60. eft->daylight = wtime->tm_isdst ? EFI_ISDST : 0;
  61. eft->timezone = EFI_UNSPECIFIED_TIMEZONE;
  62. }
  63. static bool
  64. convert_from_efi_time(efi_time_t *eft, struct rtc_time *wtime)
  65. {
  66. memset(wtime, 0, sizeof(*wtime));
  67. if (eft->second >= 60)
  68. return false;
  69. wtime->tm_sec = eft->second;
  70. if (eft->minute >= 60)
  71. return false;
  72. wtime->tm_min = eft->minute;
  73. if (eft->hour >= 24)
  74. return false;
  75. wtime->tm_hour = eft->hour;
  76. if (!eft->day || eft->day > 31)
  77. return false;
  78. wtime->tm_mday = eft->day;
  79. if (!eft->month || eft->month > 12)
  80. return false;
  81. wtime->tm_mon = eft->month - 1;
  82. if (eft->year < 1900 || eft->year > 9999)
  83. return false;
  84. wtime->tm_year = eft->year - 1900;
  85. /* day in the year [1-365]*/
  86. wtime->tm_yday = compute_yday(eft);
  87. /* day of the week [0-6], Sunday=0 */
  88. wtime->tm_wday = compute_wday(eft, wtime->tm_yday);
  89. switch (eft->daylight & EFI_ISDST) {
  90. case EFI_ISDST:
  91. wtime->tm_isdst = 1;
  92. break;
  93. case EFI_TIME_ADJUST_DAYLIGHT:
  94. wtime->tm_isdst = 0;
  95. break;
  96. default:
  97. wtime->tm_isdst = -1;
  98. }
  99. return true;
  100. }
  101. static int efi_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
  102. {
  103. efi_time_t eft;
  104. efi_status_t status;
  105. /*
  106. * As of EFI v1.10, this call always returns an unsupported status
  107. */
  108. status = efi.get_wakeup_time((efi_bool_t *)&wkalrm->enabled,
  109. (efi_bool_t *)&wkalrm->pending, &eft);
  110. if (status != EFI_SUCCESS)
  111. return -EINVAL;
  112. if (!convert_from_efi_time(&eft, &wkalrm->time))
  113. return -EIO;
  114. return rtc_valid_tm(&wkalrm->time);
  115. }
  116. static int efi_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
  117. {
  118. efi_time_t eft;
  119. efi_status_t status;
  120. convert_to_efi_time(&wkalrm->time, &eft);
  121. /*
  122. * XXX Fixme:
  123. * As of EFI 0.92 with the firmware I have on my
  124. * machine this call does not seem to work quite
  125. * right
  126. *
  127. * As of v1.10, this call always returns an unsupported status
  128. */
  129. status = efi.set_wakeup_time((efi_bool_t)wkalrm->enabled, &eft);
  130. dev_warn(dev, "write status is %d\n", (int)status);
  131. return status == EFI_SUCCESS ? 0 : -EINVAL;
  132. }
  133. static int efi_read_time(struct device *dev, struct rtc_time *tm)
  134. {
  135. efi_status_t status;
  136. efi_time_t eft;
  137. efi_time_cap_t cap;
  138. status = efi.get_time(&eft, &cap);
  139. if (status != EFI_SUCCESS) {
  140. /* should never happen */
  141. dev_err(dev, "can't read time\n");
  142. return -EINVAL;
  143. }
  144. if (!convert_from_efi_time(&eft, tm))
  145. return -EIO;
  146. return rtc_valid_tm(tm);
  147. }
  148. static int efi_set_time(struct device *dev, struct rtc_time *tm)
  149. {
  150. efi_status_t status;
  151. efi_time_t eft;
  152. convert_to_efi_time(tm, &eft);
  153. status = efi.set_time(&eft);
  154. return status == EFI_SUCCESS ? 0 : -EINVAL;
  155. }
  156. static const struct rtc_class_ops efi_rtc_ops = {
  157. .read_time = efi_read_time,
  158. .set_time = efi_set_time,
  159. .read_alarm = efi_read_alarm,
  160. .set_alarm = efi_set_alarm,
  161. };
  162. static int __init efi_rtc_probe(struct platform_device *dev)
  163. {
  164. struct rtc_device *rtc;
  165. rtc = devm_rtc_device_register(&dev->dev, "rtc-efi", &efi_rtc_ops,
  166. THIS_MODULE);
  167. if (IS_ERR(rtc))
  168. return PTR_ERR(rtc);
  169. rtc->uie_unsupported = 1;
  170. platform_set_drvdata(dev, rtc);
  171. return 0;
  172. }
  173. static struct platform_driver efi_rtc_driver = {
  174. .driver = {
  175. .name = "rtc-efi",
  176. },
  177. };
  178. module_platform_driver_probe(efi_rtc_driver, efi_rtc_probe);
  179. MODULE_ALIAS("platform:rtc-efi");
  180. MODULE_AUTHOR("dann frazier <dannf@dannf.org>");
  181. MODULE_LICENSE("GPL");
  182. MODULE_DESCRIPTION("EFI RTC driver");
  183. MODULE_ALIAS("platform:rtc-efi");