blob: 3806961b4348cff032f792d452c14006789c5dae [file] [log] [blame]
dann frazier5e3fd9e2009-03-31 15:24:48 -07001/*
2 * rtc-efi: RTC Class Driver for EFI-based systems
3 *
4 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
5 *
dann frazier37563e5e2015-04-24 11:54:20 -06006 * Author: dann frazier <dannf@dannf.org>
dann frazier5e3fd9e2009-03-31 15:24:48 -07007 * 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
Jingoo Han34650f92013-02-21 16:45:25 -080016#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
dann frazier5e3fd9e2009-03-31 15:24:48 -070018#include <linux/kernel.h>
19#include <linux/module.h>
Jan Beulich6e85bab2014-08-08 14:20:09 -070020#include <linux/stringify.h>
dann frazier5e3fd9e2009-03-31 15:24:48 -070021#include <linux/time.h>
22#include <linux/platform_device.h>
23#include <linux/rtc.h>
24#include <linux/efi.h>
25
26#define EFI_ISDST (EFI_TIME_ADJUST_DAYLIGHT|EFI_TIME_IN_DAYLIGHT)
dann frazier5e3fd9e2009-03-31 15:24:48 -070027
28/*
29 * returns day of the year [0-365]
30 */
31static inline int
32compute_yday(efi_time_t *eft)
33{
34 /* efi_time_t.month is in the [1-12] so, we need -1 */
Lee, Chun-Yi809d9622014-06-06 14:35:48 -070035 return rtc_year_days(eft->day, eft->month - 1, eft->year);
dann frazier5e3fd9e2009-03-31 15:24:48 -070036}
Ard Biesheuvelb2bd2372015-06-09 11:15:35 +020037
dann frazier5e3fd9e2009-03-31 15:24:48 -070038/*
39 * returns day of the week [0-6] 0=Sunday
dann frazier5e3fd9e2009-03-31 15:24:48 -070040 */
41static int
Ard Biesheuvelb2bd2372015-06-09 11:15:35 +020042compute_wday(efi_time_t *eft, int yday)
dann frazier5e3fd9e2009-03-31 15:24:48 -070043{
Ard Biesheuvelb2bd2372015-06-09 11:15:35 +020044 int ndays = eft->year * (365 % 7)
45 + (eft->year - 1) / 4
46 - (eft->year - 1) / 100
47 + (eft->year - 1) / 400
48 + yday;
dann frazier5e3fd9e2009-03-31 15:24:48 -070049
50 /*
Ard Biesheuvelb2bd2372015-06-09 11:15:35 +020051 * 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.
dann frazier5e3fd9e2009-03-31 15:24:48 -070053 */
Ard Biesheuvelb2bd2372015-06-09 11:15:35 +020054 return ndays % 7;
dann frazier5e3fd9e2009-03-31 15:24:48 -070055}
56
57static void
58convert_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;
Jingoo Han34650f92013-02-21 16:45:25 -080065 eft->second = wtime->tm_sec;
dann frazier5e3fd9e2009-03-31 15:24:48 -070066 eft->nanosecond = 0;
67 eft->daylight = wtime->tm_isdst ? EFI_ISDST : 0;
68 eft->timezone = EFI_UNSPECIFIED_TIMEZONE;
69}
70
Jan Beulich6e85bab2014-08-08 14:20:09 -070071static bool
dann frazier5e3fd9e2009-03-31 15:24:48 -070072convert_from_efi_time(efi_time_t *eft, struct rtc_time *wtime)
73{
74 memset(wtime, 0, sizeof(*wtime));
Jan Beulich6e85bab2014-08-08 14:20:09 -070075
76 if (eft->second >= 60)
77 return false;
dann frazier5e3fd9e2009-03-31 15:24:48 -070078 wtime->tm_sec = eft->second;
Jan Beulich6e85bab2014-08-08 14:20:09 -070079
80 if (eft->minute >= 60)
81 return false;
dann frazier5e3fd9e2009-03-31 15:24:48 -070082 wtime->tm_min = eft->minute;
Jan Beulich6e85bab2014-08-08 14:20:09 -070083
84 if (eft->hour >= 24)
85 return false;
dann frazier5e3fd9e2009-03-31 15:24:48 -070086 wtime->tm_hour = eft->hour;
Jan Beulich6e85bab2014-08-08 14:20:09 -070087
88 if (!eft->day || eft->day > 31)
89 return false;
dann frazier5e3fd9e2009-03-31 15:24:48 -070090 wtime->tm_mday = eft->day;
Jan Beulich6e85bab2014-08-08 14:20:09 -070091
92 if (!eft->month || eft->month > 12)
93 return false;
dann frazier5e3fd9e2009-03-31 15:24:48 -070094 wtime->tm_mon = eft->month - 1;
dann frazier5e3fd9e2009-03-31 15:24:48 -070095
Ard Biesheuvelb2bd2372015-06-09 11:15:35 +020096 if (eft->year < 1900 || eft->year > 9999)
Jan Beulich6e85bab2014-08-08 14:20:09 -070097 return false;
Ard Biesheuvelb2bd2372015-06-09 11:15:35 +020098 wtime->tm_year = eft->year - 1900;
dann frazier5e3fd9e2009-03-31 15:24:48 -070099
100 /* day in the year [1-365]*/
101 wtime->tm_yday = compute_yday(eft);
102
Ard Biesheuvelb2bd2372015-06-09 11:15:35 +0200103 /* day of the week [0-6], Sunday=0 */
104 wtime->tm_wday = compute_wday(eft, wtime->tm_yday);
dann frazier5e3fd9e2009-03-31 15:24:48 -0700105
106 switch (eft->daylight & EFI_ISDST) {
107 case EFI_ISDST:
108 wtime->tm_isdst = 1;
109 break;
110 case EFI_TIME_ADJUST_DAYLIGHT:
111 wtime->tm_isdst = 0;
112 break;
113 default:
114 wtime->tm_isdst = -1;
115 }
Jan Beulich6e85bab2014-08-08 14:20:09 -0700116
117 return true;
dann frazier5e3fd9e2009-03-31 15:24:48 -0700118}
119
120static int efi_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
121{
122 efi_time_t eft;
123 efi_status_t status;
124
125 /*
126 * As of EFI v1.10, this call always returns an unsupported status
127 */
128 status = efi.get_wakeup_time((efi_bool_t *)&wkalrm->enabled,
129 (efi_bool_t *)&wkalrm->pending, &eft);
130
131 if (status != EFI_SUCCESS)
132 return -EINVAL;
133
Jan Beulich6e85bab2014-08-08 14:20:09 -0700134 if (!convert_from_efi_time(&eft, &wkalrm->time))
135 return -EIO;
dann frazier5e3fd9e2009-03-31 15:24:48 -0700136
137 return rtc_valid_tm(&wkalrm->time);
138}
139
140static int efi_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
141{
142 efi_time_t eft;
143 efi_status_t status;
144
145 convert_to_efi_time(&wkalrm->time, &eft);
146
147 /*
148 * XXX Fixme:
149 * As of EFI 0.92 with the firmware I have on my
150 * machine this call does not seem to work quite
151 * right
152 *
153 * As of v1.10, this call always returns an unsupported status
154 */
155 status = efi.set_wakeup_time((efi_bool_t)wkalrm->enabled, &eft);
156
Jingoo Han34650f92013-02-21 16:45:25 -0800157 dev_warn(dev, "write status is %d\n", (int)status);
dann frazier5e3fd9e2009-03-31 15:24:48 -0700158
159 return status == EFI_SUCCESS ? 0 : -EINVAL;
160}
161
162static int efi_read_time(struct device *dev, struct rtc_time *tm)
163{
164 efi_status_t status;
165 efi_time_t eft;
166 efi_time_cap_t cap;
167
168 status = efi.get_time(&eft, &cap);
169
170 if (status != EFI_SUCCESS) {
171 /* should never happen */
Jingoo Han34650f92013-02-21 16:45:25 -0800172 dev_err(dev, "can't read time\n");
dann frazier5e3fd9e2009-03-31 15:24:48 -0700173 return -EINVAL;
174 }
175
Jan Beulich6e85bab2014-08-08 14:20:09 -0700176 if (!convert_from_efi_time(&eft, tm))
177 return -EIO;
dann frazier5e3fd9e2009-03-31 15:24:48 -0700178
179 return rtc_valid_tm(tm);
180}
181
182static int efi_set_time(struct device *dev, struct rtc_time *tm)
183{
184 efi_status_t status;
185 efi_time_t eft;
186
187 convert_to_efi_time(tm, &eft);
188
189 status = efi.set_time(&eft);
190
191 return status == EFI_SUCCESS ? 0 : -EINVAL;
192}
193
194static 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,
199};
200
201static int __init efi_rtc_probe(struct platform_device *dev)
202{
203 struct rtc_device *rtc;
204
Jingoo Han6ae15b02013-04-29 16:19:36 -0700205 rtc = devm_rtc_device_register(&dev->dev, "rtc-efi", &efi_rtc_ops,
dann frazier5e3fd9e2009-03-31 15:24:48 -0700206 THIS_MODULE);
207 if (IS_ERR(rtc))
208 return PTR_ERR(rtc);
209
Ard Biesheuvel822a0272015-01-13 12:28:53 +0000210 rtc->uie_unsupported = 1;
dann frazier5e3fd9e2009-03-31 15:24:48 -0700211 platform_set_drvdata(dev, rtc);
212
213 return 0;
214}
215
dann frazier5e3fd9e2009-03-31 15:24:48 -0700216static struct platform_driver efi_rtc_driver = {
217 .driver = {
218 .name = "rtc-efi",
dann frazier5e3fd9e2009-03-31 15:24:48 -0700219 },
dann frazier5e3fd9e2009-03-31 15:24:48 -0700220};
221
Jingoo Han52c6ecb2013-04-29 16:18:40 -0700222module_platform_driver_probe(efi_rtc_driver, efi_rtc_probe);
dann frazier5e3fd9e2009-03-31 15:24:48 -0700223
Pali Rohár451ff6d2014-09-25 16:05:22 -0700224MODULE_ALIAS("platform:rtc-efi");
dann frazier37563e5e2015-04-24 11:54:20 -0600225MODULE_AUTHOR("dann frazier <dannf@dannf.org>");
dann frazier5e3fd9e2009-03-31 15:24:48 -0700226MODULE_LICENSE("GPL");
227MODULE_DESCRIPTION("EFI RTC driver");
Ard Biesheuvel3f71f6d2014-09-11 10:20:40 +0200228MODULE_ALIAS("platform:rtc-efi");