2 * rtc-efi: RTC Class Driver for EFI-based systems
4 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
6 * Author: dann frazier <dannf@hp.com>
7 * Based on efirtc.c by Stephane Eranian
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.
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/stringify.h>
21 #include <linux/time.h>
22 #include <linux/platform_device.h>
23 #include <linux/rtc.h>
24 #include <linux/efi.h>
26 #define EFI_ISDST (EFI_TIME_ADJUST_DAYLIGHT|EFI_TIME_IN_DAYLIGHT)
28 * EFI Epoch is 1/1/1998
30 #define EFI_RTC_EPOCH 1998
33 * returns day of the year [0-365]
36 compute_yday(efi_time_t *eft)
38 /* efi_time_t.month is in the [1-12] so, we need -1 */
39 return rtc_year_days(eft->day, eft->month - 1, eft->year);
42 * returns day of the week [0-6] 0=Sunday
44 * Don't try to provide a year that's before 1998, please !
47 compute_wday(efi_time_t *eft)
52 if (eft->year < EFI_RTC_EPOCH) {
53 pr_err("EFI year < " __stringify(EFI_RTC_EPOCH) ", invalid date\n");
57 for (y = EFI_RTC_EPOCH; y < eft->year; y++)
58 ndays += 365 + (is_leap_year(y) ? 1 : 0);
60 ndays += compute_yday(eft);
63 * 4=1/1/1998 was a Thursday
65 return (ndays + 4) % 7;
69 convert_to_efi_time(struct rtc_time *wtime, efi_time_t *eft)
71 eft->year = wtime->tm_year + 1900;
72 eft->month = wtime->tm_mon + 1;
73 eft->day = wtime->tm_mday;
74 eft->hour = wtime->tm_hour;
75 eft->minute = wtime->tm_min;
76 eft->second = wtime->tm_sec;
78 eft->daylight = wtime->tm_isdst ? EFI_ISDST : 0;
79 eft->timezone = EFI_UNSPECIFIED_TIMEZONE;
83 convert_from_efi_time(efi_time_t *eft, struct rtc_time *wtime)
85 memset(wtime, 0, sizeof(*wtime));
87 if (eft->second >= 60)
89 wtime->tm_sec = eft->second;
91 if (eft->minute >= 60)
93 wtime->tm_min = eft->minute;
97 wtime->tm_hour = eft->hour;
99 if (!eft->day || eft->day > 31)
101 wtime->tm_mday = eft->day;
103 if (!eft->month || eft->month > 12)
105 wtime->tm_mon = eft->month - 1;
106 wtime->tm_year = eft->year - 1900;
108 /* day of the week [0-6], Sunday=0 */
109 wtime->tm_wday = compute_wday(eft);
110 if (wtime->tm_wday < 0)
113 /* day in the year [1-365]*/
114 wtime->tm_yday = compute_yday(eft);
117 switch (eft->daylight & EFI_ISDST) {
121 case EFI_TIME_ADJUST_DAYLIGHT:
125 wtime->tm_isdst = -1;
131 static int efi_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
137 * As of EFI v1.10, this call always returns an unsupported status
139 status = efi.get_wakeup_time((efi_bool_t *)&wkalrm->enabled,
140 (efi_bool_t *)&wkalrm->pending, &eft);
142 if (status != EFI_SUCCESS)
145 if (!convert_from_efi_time(&eft, &wkalrm->time))
148 return rtc_valid_tm(&wkalrm->time);
151 static int efi_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
156 convert_to_efi_time(&wkalrm->time, &eft);
160 * As of EFI 0.92 with the firmware I have on my
161 * machine this call does not seem to work quite
164 * As of v1.10, this call always returns an unsupported status
166 status = efi.set_wakeup_time((efi_bool_t)wkalrm->enabled, &eft);
168 dev_warn(dev, "write status is %d\n", (int)status);
170 return status == EFI_SUCCESS ? 0 : -EINVAL;
173 static int efi_read_time(struct device *dev, struct rtc_time *tm)
179 status = efi.get_time(&eft, &cap);
181 if (status != EFI_SUCCESS) {
182 /* should never happen */
183 dev_err(dev, "can't read time\n");
187 if (!convert_from_efi_time(&eft, tm))
190 return rtc_valid_tm(tm);
193 static int efi_set_time(struct device *dev, struct rtc_time *tm)
198 convert_to_efi_time(tm, &eft);
200 status = efi.set_time(&eft);
202 return status == EFI_SUCCESS ? 0 : -EINVAL;
205 static const struct rtc_class_ops efi_rtc_ops = {
206 .read_time = efi_read_time,
207 .set_time = efi_set_time,
208 .read_alarm = efi_read_alarm,
209 .set_alarm = efi_set_alarm,
212 static int __init efi_rtc_probe(struct platform_device *dev)
214 struct rtc_device *rtc;
216 rtc = devm_rtc_device_register(&dev->dev, "rtc-efi", &efi_rtc_ops,
221 rtc->uie_unsupported = 1;
222 platform_set_drvdata(dev, rtc);
227 static struct platform_driver efi_rtc_driver = {
233 module_platform_driver_probe(efi_rtc_driver, efi_rtc_probe);
235 MODULE_ALIAS("platform:rtc-efi");
236 MODULE_AUTHOR("dann frazier <dannf@hp.com>");
237 MODULE_LICENSE("GPL");
238 MODULE_DESCRIPTION("EFI RTC driver");
239 MODULE_ALIAS("platform:rtc-efi");