rtc-efi.c 5.0 KB

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