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@dannf.org>
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)
29 * returns day of the year [0-365]
32 compute_yday(efi_time_t
*eft
)
34 /* efi_time_t.month is in the [1-12] so, we need -1 */
35 return rtc_year_days(eft
->day
, eft
->month
- 1, eft
->year
);
39 * returns day of the week [0-6] 0=Sunday
42 compute_wday(efi_time_t
*eft
, int yday
)
44 int ndays
= eft
->year
* (365 % 7)
46 - (eft
->year
- 1) / 100
47 + (eft
->year
- 1) / 400
51 * 1/1/0000 may or may not have been a Sunday (if it ever existed at
52 * all) but assuming it was makes this calculation work correctly.
58 convert_to_efi_time(struct rtc_time
*wtime
, efi_time_t
*eft
)
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
;
67 eft
->daylight
= wtime
->tm_isdst
? EFI_ISDST
: 0;
68 eft
->timezone
= EFI_UNSPECIFIED_TIMEZONE
;
72 convert_from_efi_time(efi_time_t
*eft
, struct rtc_time
*wtime
)
74 memset(wtime
, 0, sizeof(*wtime
));
76 if (eft
->second
>= 60)
78 wtime
->tm_sec
= eft
->second
;
80 if (eft
->minute
>= 60)
82 wtime
->tm_min
= eft
->minute
;
86 wtime
->tm_hour
= eft
->hour
;
88 if (!eft
->day
|| eft
->day
> 31)
90 wtime
->tm_mday
= eft
->day
;
92 if (!eft
->month
|| eft
->month
> 12)
94 wtime
->tm_mon
= eft
->month
- 1;
96 if (eft
->year
< 1900 || eft
->year
> 9999)
98 wtime
->tm_year
= eft
->year
- 1900;
100 /* day in the year [1-365]*/
101 wtime
->tm_yday
= compute_yday(eft
);
103 /* day of the week [0-6], Sunday=0 */
104 wtime
->tm_wday
= compute_wday(eft
, wtime
->tm_yday
);
106 switch (eft
->daylight
& EFI_ISDST
) {
110 case EFI_TIME_ADJUST_DAYLIGHT
:
114 wtime
->tm_isdst
= -1;
120 static int efi_read_alarm(struct device
*dev
, struct rtc_wkalrm
*wkalrm
)
126 * As of EFI v1.10, this call always returns an unsupported status
128 status
= efi
.get_wakeup_time((efi_bool_t
*)&wkalrm
->enabled
,
129 (efi_bool_t
*)&wkalrm
->pending
, &eft
);
131 if (status
!= EFI_SUCCESS
)
134 if (!convert_from_efi_time(&eft
, &wkalrm
->time
))
137 return rtc_valid_tm(&wkalrm
->time
);
140 static int efi_set_alarm(struct device
*dev
, struct rtc_wkalrm
*wkalrm
)
145 convert_to_efi_time(&wkalrm
->time
, &eft
);
149 * As of EFI 0.92 with the firmware I have on my
150 * machine this call does not seem to work quite
153 * As of v1.10, this call always returns an unsupported status
155 status
= efi
.set_wakeup_time((efi_bool_t
)wkalrm
->enabled
, &eft
);
157 dev_warn(dev
, "write status is %d\n", (int)status
);
159 return status
== EFI_SUCCESS
? 0 : -EINVAL
;
162 static int efi_read_time(struct device
*dev
, struct rtc_time
*tm
)
168 status
= efi
.get_time(&eft
, &cap
);
170 if (status
!= EFI_SUCCESS
) {
171 /* should never happen */
172 dev_err(dev
, "can't read time\n");
176 if (!convert_from_efi_time(&eft
, tm
))
179 return rtc_valid_tm(tm
);
182 static int efi_set_time(struct device
*dev
, struct rtc_time
*tm
)
187 convert_to_efi_time(tm
, &eft
);
189 status
= efi
.set_time(&eft
);
191 return status
== EFI_SUCCESS
? 0 : -EINVAL
;
194 static const struct rtc_class_ops efi_rtc_ops
= {
195 .read_time
= efi_read_time
,
196 .set_time
= efi_set_time
,
197 .read_alarm
= efi_read_alarm
,
198 .set_alarm
= efi_set_alarm
,
201 static int __init
efi_rtc_probe(struct platform_device
*dev
)
203 struct rtc_device
*rtc
;
205 rtc
= devm_rtc_device_register(&dev
->dev
, "rtc-efi", &efi_rtc_ops
,
210 rtc
->uie_unsupported
= 1;
211 platform_set_drvdata(dev
, rtc
);
216 static struct platform_driver efi_rtc_driver
= {
222 module_platform_driver_probe(efi_rtc_driver
, efi_rtc_probe
);
224 MODULE_ALIAS("platform:rtc-efi");
225 MODULE_AUTHOR("dann frazier <dannf@dannf.org>");
226 MODULE_LICENSE("GPL");
227 MODULE_DESCRIPTION("EFI RTC driver");
228 MODULE_ALIAS("platform:rtc-efi");