Staging: sm750fb: fix build warning with proc_panDisplay
[linux/fpc-iii.git] / drivers / rtc / rtc-efi.c
blobcb989cd00b14260f19f3034c8f49792a9ac2d564
1 /*
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]
35 static inline int
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 !
46 static int
47 compute_wday(efi_time_t *eft)
49 int y;
50 int ndays = 0;
52 if (eft->year < EFI_RTC_EPOCH) {
53 pr_err("EFI year < " __stringify(EFI_RTC_EPOCH) ", invalid date\n");
54 return -1;
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;
68 static void
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;
77 eft->nanosecond = 0;
78 eft->daylight = wtime->tm_isdst ? EFI_ISDST : 0;
79 eft->timezone = EFI_UNSPECIFIED_TIMEZONE;
82 static bool
83 convert_from_efi_time(efi_time_t *eft, struct rtc_time *wtime)
85 memset(wtime, 0, sizeof(*wtime));
87 if (eft->second >= 60)
88 return false;
89 wtime->tm_sec = eft->second;
91 if (eft->minute >= 60)
92 return false;
93 wtime->tm_min = eft->minute;
95 if (eft->hour >= 24)
96 return false;
97 wtime->tm_hour = eft->hour;
99 if (!eft->day || eft->day > 31)
100 return false;
101 wtime->tm_mday = eft->day;
103 if (!eft->month || eft->month > 12)
104 return false;
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)
111 return false;
113 /* day in the year [1-365]*/
114 wtime->tm_yday = compute_yday(eft);
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;
128 return true;
131 static int efi_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
133 efi_time_t eft;
134 efi_status_t status;
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)
143 return -EINVAL;
145 if (!convert_from_efi_time(&eft, &wkalrm->time))
146 return -EIO;
148 return rtc_valid_tm(&wkalrm->time);
151 static int efi_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
153 efi_time_t eft;
154 efi_status_t status;
156 convert_to_efi_time(&wkalrm->time, &eft);
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
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)
175 efi_status_t status;
176 efi_time_t eft;
177 efi_time_cap_t cap;
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");
184 return -EINVAL;
187 if (!convert_from_efi_time(&eft, tm))
188 return -EIO;
190 return rtc_valid_tm(tm);
193 static int efi_set_time(struct device *dev, struct rtc_time *tm)
195 efi_status_t status;
196 efi_time_t eft;
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,
217 THIS_MODULE);
218 if (IS_ERR(rtc))
219 return PTR_ERR(rtc);
221 rtc->uie_unsupported = 1;
222 platform_set_drvdata(dev, rtc);
224 return 0;
227 static struct platform_driver efi_rtc_driver = {
228 .driver = {
229 .name = "rtc-efi",
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");