rtc-efi.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 int efi_procfs(struct device *dev, struct seq_file *seq)
  157. {
  158. efi_time_t eft, alm;
  159. efi_time_cap_t cap;
  160. efi_bool_t enabled, pending;
  161. memset(&eft, 0, sizeof(eft));
  162. memset(&alm, 0, sizeof(alm));
  163. memset(&cap, 0, sizeof(cap));
  164. efi.get_time(&eft, &cap);
  165. efi.get_wakeup_time(&enabled, &pending, &alm);
  166. seq_printf(seq,
  167. "Time\t\t: %u:%u:%u.%09u\n"
  168. "Date\t\t: %u-%u-%u\n"
  169. "Daylight\t: %u\n",
  170. eft.hour, eft.minute, eft.second, eft.nanosecond,
  171. eft.year, eft.month, eft.day,
  172. eft.daylight);
  173. if (eft.timezone == EFI_UNSPECIFIED_TIMEZONE)
  174. seq_puts(seq, "Timezone\t: unspecified\n");
  175. else
  176. /* XXX fixme: convert to string? */
  177. seq_printf(seq, "Timezone\t: %u\n", eft.timezone);
  178. seq_printf(seq,
  179. "Alarm Time\t: %u:%u:%u.%09u\n"
  180. "Alarm Date\t: %u-%u-%u\n"
  181. "Alarm Daylight\t: %u\n"
  182. "Enabled\t\t: %s\n"
  183. "Pending\t\t: %s\n",
  184. alm.hour, alm.minute, alm.second, alm.nanosecond,
  185. alm.year, alm.month, alm.day,
  186. alm.daylight,
  187. enabled == 1 ? "yes" : "no",
  188. pending == 1 ? "yes" : "no");
  189. if (eft.timezone == EFI_UNSPECIFIED_TIMEZONE)
  190. seq_puts(seq, "Timezone\t: unspecified\n");
  191. else
  192. /* XXX fixme: convert to string? */
  193. seq_printf(seq, "Timezone\t: %u\n", alm.timezone);
  194. /*
  195. * now prints the capabilities
  196. */
  197. seq_printf(seq,
  198. "Resolution\t: %u\n"
  199. "Accuracy\t: %u\n"
  200. "SetstoZero\t: %u\n",
  201. cap.resolution, cap.accuracy, cap.sets_to_zero);
  202. return 0;
  203. }
  204. static const struct rtc_class_ops efi_rtc_ops = {
  205. .read_time = efi_read_time,
  206. .set_time = efi_set_time,
  207. .read_alarm = efi_read_alarm,
  208. .set_alarm = efi_set_alarm,
  209. .proc = efi_procfs,
  210. };
  211. static int __init efi_rtc_probe(struct platform_device *dev)
  212. {
  213. struct rtc_device *rtc;
  214. efi_time_t eft;
  215. efi_time_cap_t cap;
  216. /* First check if the RTC is usable */
  217. if (efi.get_time(&eft, &cap) != EFI_SUCCESS)
  218. return -ENODEV;
  219. rtc = devm_rtc_device_register(&dev->dev, "rtc-efi", &efi_rtc_ops,
  220. THIS_MODULE);
  221. if (IS_ERR(rtc))
  222. return PTR_ERR(rtc);
  223. rtc->uie_unsupported = 1;
  224. platform_set_drvdata(dev, rtc);
  225. return 0;
  226. }
  227. static struct platform_driver efi_rtc_driver = {
  228. .driver = {
  229. .name = "rtc-efi",
  230. },
  231. };
  232. module_platform_driver_probe(efi_rtc_driver, efi_rtc_probe);
  233. MODULE_ALIAS("platform:rtc-efi");
  234. MODULE_AUTHOR("dann frazier <dannf@dannf.org>");
  235. MODULE_LICENSE("GPL");
  236. MODULE_DESCRIPTION("EFI RTC driver");
  237. MODULE_ALIAS("platform:rtc-efi");