blob: 59a55e7abf780f5b142776d5a35c125efba41d28 [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 *
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
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)
27/*
28 * EFI Epoch is 1/1/1998
29 */
30#define EFI_RTC_EPOCH 1998
31
32/*
33 * returns day of the year [0-365]
34 */
35static inline int
36compute_yday(efi_time_t *eft)
37{
38 /* efi_time_t.month is in the [1-12] so, we need -1 */
Lee, Chun-Yi809d9622014-06-06 14:35:48 -070039 return rtc_year_days(eft->day, eft->month - 1, eft->year);
dann frazier5e3fd9e2009-03-31 15:24:48 -070040}
41/*
42 * returns day of the week [0-6] 0=Sunday
43 *
44 * Don't try to provide a year that's before 1998, please !
45 */
46static int
47compute_wday(efi_time_t *eft)
48{
49 int y;
50 int ndays = 0;
51
Jan Beulich6e85bab2014-08-08 14:20:09 -070052 if (eft->year < EFI_RTC_EPOCH) {
53 pr_err("EFI year < " __stringify(EFI_RTC_EPOCH) ", invalid date\n");
dann frazier5e3fd9e2009-03-31 15:24:48 -070054 return -1;
55 }
56
57 for (y = EFI_RTC_EPOCH; y < eft->year; y++)
58 ndays += 365 + (is_leap_year(y) ? 1 : 0);
59
60 ndays += compute_yday(eft);
61
62 /*
63 * 4=1/1/1998 was a Thursday
64 */
65 return (ndays + 4) % 7;
66}
67
68static void
69convert_to_efi_time(struct rtc_time *wtime, efi_time_t *eft)
70{
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;
Jingoo Han34650f92013-02-21 16:45:25 -080076 eft->second = wtime->tm_sec;
dann frazier5e3fd9e2009-03-31 15:24:48 -070077 eft->nanosecond = 0;
78 eft->daylight = wtime->tm_isdst ? EFI_ISDST : 0;
79 eft->timezone = EFI_UNSPECIFIED_TIMEZONE;
80}
81
Jan Beulich6e85bab2014-08-08 14:20:09 -070082static bool
dann frazier5e3fd9e2009-03-31 15:24:48 -070083convert_from_efi_time(efi_time_t *eft, struct rtc_time *wtime)
84{
85 memset(wtime, 0, sizeof(*wtime));
Jan Beulich6e85bab2014-08-08 14:20:09 -070086
87 if (eft->second >= 60)
88 return false;
dann frazier5e3fd9e2009-03-31 15:24:48 -070089 wtime->tm_sec = eft->second;
Jan Beulich6e85bab2014-08-08 14:20:09 -070090
91 if (eft->minute >= 60)
92 return false;
dann frazier5e3fd9e2009-03-31 15:24:48 -070093 wtime->tm_min = eft->minute;
Jan Beulich6e85bab2014-08-08 14:20:09 -070094
95 if (eft->hour >= 24)
96 return false;
dann frazier5e3fd9e2009-03-31 15:24:48 -070097 wtime->tm_hour = eft->hour;
Jan Beulich6e85bab2014-08-08 14:20:09 -070098
99 if (!eft->day || eft->day > 31)
100 return false;
dann frazier5e3fd9e2009-03-31 15:24:48 -0700101 wtime->tm_mday = eft->day;
Jan Beulich6e85bab2014-08-08 14:20:09 -0700102
103 if (!eft->month || eft->month > 12)
104 return false;
dann frazier5e3fd9e2009-03-31 15:24:48 -0700105 wtime->tm_mon = eft->month - 1;
106 wtime->tm_year = eft->year - 1900;
107
108 /* day of the week [0-6], Sunday=0 */
109 wtime->tm_wday = compute_wday(eft);
Jan Beulich6e85bab2014-08-08 14:20:09 -0700110 if (wtime->tm_wday < 0)
111 return false;
dann frazier5e3fd9e2009-03-31 15:24:48 -0700112
113 /* day in the year [1-365]*/
114 wtime->tm_yday = compute_yday(eft);
115
116
117 switch (eft->daylight & EFI_ISDST) {
118 case EFI_ISDST:
119 wtime->tm_isdst = 1;
120 break;
121 case EFI_TIME_ADJUST_DAYLIGHT:
122 wtime->tm_isdst = 0;
123 break;
124 default:
125 wtime->tm_isdst = -1;
126 }
Jan Beulich6e85bab2014-08-08 14:20:09 -0700127
128 return true;
dann frazier5e3fd9e2009-03-31 15:24:48 -0700129}
130
131static int efi_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
132{
133 efi_time_t eft;
134 efi_status_t status;
135
136 /*
137 * As of EFI v1.10, this call always returns an unsupported status
138 */
139 status = efi.get_wakeup_time((efi_bool_t *)&wkalrm->enabled,
140 (efi_bool_t *)&wkalrm->pending, &eft);
141
142 if (status != EFI_SUCCESS)
143 return -EINVAL;
144
Jan Beulich6e85bab2014-08-08 14:20:09 -0700145 if (!convert_from_efi_time(&eft, &wkalrm->time))
146 return -EIO;
dann frazier5e3fd9e2009-03-31 15:24:48 -0700147
148 return rtc_valid_tm(&wkalrm->time);
149}
150
151static int efi_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
152{
153 efi_time_t eft;
154 efi_status_t status;
155
156 convert_to_efi_time(&wkalrm->time, &eft);
157
158 /*
159 * XXX Fixme:
160 * As of EFI 0.92 with the firmware I have on my
161 * machine this call does not seem to work quite
162 * right
163 *
164 * As of v1.10, this call always returns an unsupported status
165 */
166 status = efi.set_wakeup_time((efi_bool_t)wkalrm->enabled, &eft);
167
Jingoo Han34650f92013-02-21 16:45:25 -0800168 dev_warn(dev, "write status is %d\n", (int)status);
dann frazier5e3fd9e2009-03-31 15:24:48 -0700169
170 return status == EFI_SUCCESS ? 0 : -EINVAL;
171}
172
173static int efi_read_time(struct device *dev, struct rtc_time *tm)
174{
175 efi_status_t status;
176 efi_time_t eft;
177 efi_time_cap_t cap;
178
179 status = efi.get_time(&eft, &cap);
180
181 if (status != EFI_SUCCESS) {
182 /* should never happen */
Jingoo Han34650f92013-02-21 16:45:25 -0800183 dev_err(dev, "can't read time\n");
dann frazier5e3fd9e2009-03-31 15:24:48 -0700184 return -EINVAL;
185 }
186
Jan Beulich6e85bab2014-08-08 14:20:09 -0700187 if (!convert_from_efi_time(&eft, tm))
188 return -EIO;
dann frazier5e3fd9e2009-03-31 15:24:48 -0700189
190 return rtc_valid_tm(tm);
191}
192
193static int efi_set_time(struct device *dev, struct rtc_time *tm)
194{
195 efi_status_t status;
196 efi_time_t eft;
197
198 convert_to_efi_time(tm, &eft);
199
200 status = efi.set_time(&eft);
201
202 return status == EFI_SUCCESS ? 0 : -EINVAL;
203}
204
205static 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,
210};
211
212static int __init efi_rtc_probe(struct platform_device *dev)
213{
214 struct rtc_device *rtc;
215
Jingoo Han6ae15b02013-04-29 16:19:36 -0700216 rtc = devm_rtc_device_register(&dev->dev, "rtc-efi", &efi_rtc_ops,
dann frazier5e3fd9e2009-03-31 15:24:48 -0700217 THIS_MODULE);
218 if (IS_ERR(rtc))
219 return PTR_ERR(rtc);
220
221 platform_set_drvdata(dev, rtc);
222
223 return 0;
224}
225
dann frazier5e3fd9e2009-03-31 15:24:48 -0700226static struct platform_driver efi_rtc_driver = {
227 .driver = {
228 .name = "rtc-efi",
229 .owner = THIS_MODULE,
230 },
dann frazier5e3fd9e2009-03-31 15:24:48 -0700231};
232
Jingoo Han52c6ecb2013-04-29 16:18:40 -0700233module_platform_driver_probe(efi_rtc_driver, efi_rtc_probe);
dann frazier5e3fd9e2009-03-31 15:24:48 -0700234
235MODULE_AUTHOR("dann frazier <dannf@hp.com>");
236MODULE_LICENSE("GPL");
237MODULE_DESCRIPTION("EFI RTC driver");
Ard Biesheuvel3f71f6d2014-09-11 10:20:40 +0200238MODULE_ALIAS("platform:rtc-efi");