rtc-efi.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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@hp.com>
  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/time.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/rtc.h>
  21. #include <linux/efi.h>
  22. #define EFI_ISDST (EFI_TIME_ADJUST_DAYLIGHT|EFI_TIME_IN_DAYLIGHT)
  23. /*
  24. * EFI Epoch is 1/1/1998
  25. */
  26. #define EFI_RTC_EPOCH 1998
  27. /*
  28. * returns day of the year [0-365]
  29. */
  30. static inline int
  31. compute_yday(efi_time_t *eft)
  32. {
  33. /* efi_time_t.month is in the [1-12] so, we need -1 */
  34. return rtc_year_days(eft->day - 1, eft->month - 1, eft->year);
  35. }
  36. /*
  37. * returns day of the week [0-6] 0=Sunday
  38. *
  39. * Don't try to provide a year that's before 1998, please !
  40. */
  41. static int
  42. compute_wday(efi_time_t *eft)
  43. {
  44. int y;
  45. int ndays = 0;
  46. if (eft->year < 1998) {
  47. pr_err("EFI year < 1998, invalid date\n");
  48. return -1;
  49. }
  50. for (y = EFI_RTC_EPOCH; y < eft->year; y++)
  51. ndays += 365 + (is_leap_year(y) ? 1 : 0);
  52. ndays += compute_yday(eft);
  53. /*
  54. * 4=1/1/1998 was a Thursday
  55. */
  56. return (ndays + 4) % 7;
  57. }
  58. static void
  59. convert_to_efi_time(struct rtc_time *wtime, efi_time_t *eft)
  60. {
  61. eft->year = wtime->tm_year + 1900;
  62. eft->month = wtime->tm_mon + 1;
  63. eft->day = wtime->tm_mday;
  64. eft->hour = wtime->tm_hour;
  65. eft->minute = wtime->tm_min;
  66. eft->second = wtime->tm_sec;
  67. eft->nanosecond = 0;
  68. eft->daylight = wtime->tm_isdst ? EFI_ISDST : 0;
  69. eft->timezone = EFI_UNSPECIFIED_TIMEZONE;
  70. }
  71. static void
  72. convert_from_efi_time(efi_time_t *eft, struct rtc_time *wtime)
  73. {
  74. memset(wtime, 0, sizeof(*wtime));
  75. wtime->tm_sec = eft->second;
  76. wtime->tm_min = eft->minute;
  77. wtime->tm_hour = eft->hour;
  78. wtime->tm_mday = eft->day;
  79. wtime->tm_mon = eft->month - 1;
  80. wtime->tm_year = eft->year - 1900;
  81. /* day of the week [0-6], Sunday=0 */
  82. wtime->tm_wday = compute_wday(eft);
  83. /* day in the year [1-365]*/
  84. wtime->tm_yday = compute_yday(eft);
  85. switch (eft->daylight & EFI_ISDST) {
  86. case EFI_ISDST:
  87. wtime->tm_isdst = 1;
  88. break;
  89. case EFI_TIME_ADJUST_DAYLIGHT:
  90. wtime->tm_isdst = 0;
  91. break;
  92. default:
  93. wtime->tm_isdst = -1;
  94. }
  95. }
  96. static int efi_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
  97. {
  98. efi_time_t eft;
  99. efi_status_t status;
  100. /*
  101. * As of EFI v1.10, this call always returns an unsupported status
  102. */
  103. status = efi.get_wakeup_time((efi_bool_t *)&wkalrm->enabled,
  104. (efi_bool_t *)&wkalrm->pending, &eft);
  105. if (status != EFI_SUCCESS)
  106. return -EINVAL;
  107. convert_from_efi_time(&eft, &wkalrm->time);
  108. return rtc_valid_tm(&wkalrm->time);
  109. }
  110. static int efi_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
  111. {
  112. efi_time_t eft;
  113. efi_status_t status;
  114. convert_to_efi_time(&wkalrm->time, &eft);
  115. /*
  116. * XXX Fixme:
  117. * As of EFI 0.92 with the firmware I have on my
  118. * machine this call does not seem to work quite
  119. * right
  120. *
  121. * As of v1.10, this call always returns an unsupported status
  122. */
  123. status = efi.set_wakeup_time((efi_bool_t)wkalrm->enabled, &eft);
  124. dev_warn(dev, "write status is %d\n", (int)status);
  125. return status == EFI_SUCCESS ? 0 : -EINVAL;
  126. }
  127. static int efi_read_time(struct device *dev, struct rtc_time *tm)
  128. {
  129. efi_status_t status;
  130. efi_time_t eft;
  131. efi_time_cap_t cap;
  132. status = efi.get_time(&eft, &cap);
  133. if (status != EFI_SUCCESS) {
  134. /* should never happen */
  135. dev_err(dev, "can't read time\n");
  136. return -EINVAL;
  137. }
  138. convert_from_efi_time(&eft, tm);
  139. return rtc_valid_tm(tm);
  140. }
  141. static int efi_set_time(struct device *dev, struct rtc_time *tm)
  142. {
  143. efi_status_t status;
  144. efi_time_t eft;
  145. convert_to_efi_time(tm, &eft);
  146. status = efi.set_time(&eft);
  147. return status == EFI_SUCCESS ? 0 : -EINVAL;
  148. }
  149. static const struct rtc_class_ops efi_rtc_ops = {
  150. .read_time = efi_read_time,
  151. .set_time = efi_set_time,
  152. .read_alarm = efi_read_alarm,
  153. .set_alarm = efi_set_alarm,
  154. };
  155. static int __init efi_rtc_probe(struct platform_device *dev)
  156. {
  157. struct rtc_device *rtc;
  158. rtc = rtc_device_register("rtc-efi", &dev->dev, &efi_rtc_ops,
  159. THIS_MODULE);
  160. if (IS_ERR(rtc))
  161. return PTR_ERR(rtc);
  162. platform_set_drvdata(dev, rtc);
  163. return 0;
  164. }
  165. static int __exit efi_rtc_remove(struct platform_device *dev)
  166. {
  167. struct rtc_device *rtc = platform_get_drvdata(dev);
  168. rtc_device_unregister(rtc);
  169. return 0;
  170. }
  171. static struct platform_driver efi_rtc_driver = {
  172. .driver = {
  173. .name = "rtc-efi",
  174. .owner = THIS_MODULE,
  175. },
  176. .remove = __exit_p(efi_rtc_remove),
  177. };
  178. static int __init efi_rtc_init(void)
  179. {
  180. return platform_driver_probe(&efi_rtc_driver, efi_rtc_probe);
  181. }
  182. static void __exit efi_rtc_exit(void)
  183. {
  184. platform_driver_unregister(&efi_rtc_driver);
  185. }
  186. module_init(efi_rtc_init);
  187. module_exit(efi_rtc_exit);
  188. MODULE_AUTHOR("dann frazier <dannf@hp.com>");
  189. MODULE_LICENSE("GPL");
  190. MODULE_DESCRIPTION("EFI RTC driver");