dann frazier | 5e3fd9e | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 1 | /* |
| 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 Han | 34650f9 | 2013-02-21 16:45:25 -0800 | [diff] [blame] | 16 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 17 | |
dann frazier | 5e3fd9e | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 18 | #include <linux/kernel.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/time.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/rtc.h> |
| 23 | #include <linux/efi.h> |
| 24 | |
| 25 | #define EFI_ISDST (EFI_TIME_ADJUST_DAYLIGHT|EFI_TIME_IN_DAYLIGHT) |
| 26 | /* |
| 27 | * EFI Epoch is 1/1/1998 |
| 28 | */ |
| 29 | #define EFI_RTC_EPOCH 1998 |
| 30 | |
| 31 | /* |
| 32 | * returns day of the year [0-365] |
| 33 | */ |
| 34 | static inline int |
| 35 | compute_yday(efi_time_t *eft) |
| 36 | { |
| 37 | /* efi_time_t.month is in the [1-12] so, we need -1 */ |
| 38 | return rtc_year_days(eft->day - 1, eft->month - 1, eft->year); |
| 39 | } |
| 40 | /* |
| 41 | * returns day of the week [0-6] 0=Sunday |
| 42 | * |
| 43 | * Don't try to provide a year that's before 1998, please ! |
| 44 | */ |
| 45 | static int |
| 46 | compute_wday(efi_time_t *eft) |
| 47 | { |
| 48 | int y; |
| 49 | int ndays = 0; |
| 50 | |
| 51 | if (eft->year < 1998) { |
Jingoo Han | 34650f9 | 2013-02-21 16:45:25 -0800 | [diff] [blame] | 52 | pr_err("EFI year < 1998, invalid date\n"); |
dann frazier | 5e3fd9e | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | for (y = EFI_RTC_EPOCH; y < eft->year; y++) |
| 57 | ndays += 365 + (is_leap_year(y) ? 1 : 0); |
| 58 | |
| 59 | ndays += compute_yday(eft); |
| 60 | |
| 61 | /* |
| 62 | * 4=1/1/1998 was a Thursday |
| 63 | */ |
| 64 | return (ndays + 4) % 7; |
| 65 | } |
| 66 | |
| 67 | static void |
| 68 | convert_to_efi_time(struct rtc_time *wtime, efi_time_t *eft) |
| 69 | { |
| 70 | eft->year = wtime->tm_year + 1900; |
| 71 | eft->month = wtime->tm_mon + 1; |
| 72 | eft->day = wtime->tm_mday; |
| 73 | eft->hour = wtime->tm_hour; |
| 74 | eft->minute = wtime->tm_min; |
Jingoo Han | 34650f9 | 2013-02-21 16:45:25 -0800 | [diff] [blame] | 75 | eft->second = wtime->tm_sec; |
dann frazier | 5e3fd9e | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 76 | eft->nanosecond = 0; |
| 77 | eft->daylight = wtime->tm_isdst ? EFI_ISDST : 0; |
| 78 | eft->timezone = EFI_UNSPECIFIED_TIMEZONE; |
| 79 | } |
| 80 | |
| 81 | static void |
| 82 | convert_from_efi_time(efi_time_t *eft, struct rtc_time *wtime) |
| 83 | { |
| 84 | memset(wtime, 0, sizeof(*wtime)); |
| 85 | wtime->tm_sec = eft->second; |
| 86 | wtime->tm_min = eft->minute; |
| 87 | wtime->tm_hour = eft->hour; |
| 88 | wtime->tm_mday = eft->day; |
| 89 | wtime->tm_mon = eft->month - 1; |
| 90 | wtime->tm_year = eft->year - 1900; |
| 91 | |
| 92 | /* day of the week [0-6], Sunday=0 */ |
| 93 | wtime->tm_wday = compute_wday(eft); |
| 94 | |
| 95 | /* day in the year [1-365]*/ |
| 96 | wtime->tm_yday = compute_yday(eft); |
| 97 | |
| 98 | |
| 99 | switch (eft->daylight & EFI_ISDST) { |
| 100 | case EFI_ISDST: |
| 101 | wtime->tm_isdst = 1; |
| 102 | break; |
| 103 | case EFI_TIME_ADJUST_DAYLIGHT: |
| 104 | wtime->tm_isdst = 0; |
| 105 | break; |
| 106 | default: |
| 107 | wtime->tm_isdst = -1; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | static int efi_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm) |
| 112 | { |
| 113 | efi_time_t eft; |
| 114 | efi_status_t status; |
| 115 | |
| 116 | /* |
| 117 | * As of EFI v1.10, this call always returns an unsupported status |
| 118 | */ |
| 119 | status = efi.get_wakeup_time((efi_bool_t *)&wkalrm->enabled, |
| 120 | (efi_bool_t *)&wkalrm->pending, &eft); |
| 121 | |
| 122 | if (status != EFI_SUCCESS) |
| 123 | return -EINVAL; |
| 124 | |
| 125 | convert_from_efi_time(&eft, &wkalrm->time); |
| 126 | |
| 127 | return rtc_valid_tm(&wkalrm->time); |
| 128 | } |
| 129 | |
| 130 | static int efi_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm) |
| 131 | { |
| 132 | efi_time_t eft; |
| 133 | efi_status_t status; |
| 134 | |
| 135 | convert_to_efi_time(&wkalrm->time, &eft); |
| 136 | |
| 137 | /* |
| 138 | * XXX Fixme: |
| 139 | * As of EFI 0.92 with the firmware I have on my |
| 140 | * machine this call does not seem to work quite |
| 141 | * right |
| 142 | * |
| 143 | * As of v1.10, this call always returns an unsupported status |
| 144 | */ |
| 145 | status = efi.set_wakeup_time((efi_bool_t)wkalrm->enabled, &eft); |
| 146 | |
Jingoo Han | 34650f9 | 2013-02-21 16:45:25 -0800 | [diff] [blame] | 147 | dev_warn(dev, "write status is %d\n", (int)status); |
dann frazier | 5e3fd9e | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 148 | |
| 149 | return status == EFI_SUCCESS ? 0 : -EINVAL; |
| 150 | } |
| 151 | |
| 152 | static int efi_read_time(struct device *dev, struct rtc_time *tm) |
| 153 | { |
| 154 | efi_status_t status; |
| 155 | efi_time_t eft; |
| 156 | efi_time_cap_t cap; |
| 157 | |
| 158 | status = efi.get_time(&eft, &cap); |
| 159 | |
| 160 | if (status != EFI_SUCCESS) { |
| 161 | /* should never happen */ |
Jingoo Han | 34650f9 | 2013-02-21 16:45:25 -0800 | [diff] [blame] | 162 | dev_err(dev, "can't read time\n"); |
dann frazier | 5e3fd9e | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 163 | return -EINVAL; |
| 164 | } |
| 165 | |
| 166 | convert_from_efi_time(&eft, tm); |
| 167 | |
| 168 | return rtc_valid_tm(tm); |
| 169 | } |
| 170 | |
| 171 | static int efi_set_time(struct device *dev, struct rtc_time *tm) |
| 172 | { |
| 173 | efi_status_t status; |
| 174 | efi_time_t eft; |
| 175 | |
| 176 | convert_to_efi_time(tm, &eft); |
| 177 | |
| 178 | status = efi.set_time(&eft); |
| 179 | |
| 180 | return status == EFI_SUCCESS ? 0 : -EINVAL; |
| 181 | } |
| 182 | |
| 183 | static const struct rtc_class_ops efi_rtc_ops = { |
| 184 | .read_time = efi_read_time, |
| 185 | .set_time = efi_set_time, |
| 186 | .read_alarm = efi_read_alarm, |
| 187 | .set_alarm = efi_set_alarm, |
| 188 | }; |
| 189 | |
| 190 | static int __init efi_rtc_probe(struct platform_device *dev) |
| 191 | { |
| 192 | struct rtc_device *rtc; |
| 193 | |
| 194 | rtc = rtc_device_register("rtc-efi", &dev->dev, &efi_rtc_ops, |
| 195 | THIS_MODULE); |
| 196 | if (IS_ERR(rtc)) |
| 197 | return PTR_ERR(rtc); |
| 198 | |
| 199 | platform_set_drvdata(dev, rtc); |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | static int __exit efi_rtc_remove(struct platform_device *dev) |
| 205 | { |
| 206 | struct rtc_device *rtc = platform_get_drvdata(dev); |
| 207 | |
| 208 | rtc_device_unregister(rtc); |
| 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | static struct platform_driver efi_rtc_driver = { |
| 214 | .driver = { |
| 215 | .name = "rtc-efi", |
| 216 | .owner = THIS_MODULE, |
| 217 | }, |
dann frazier | 5e3fd9e | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 218 | .remove = __exit_p(efi_rtc_remove), |
| 219 | }; |
| 220 | |
| 221 | static int __init efi_rtc_init(void) |
| 222 | { |
| 223 | return platform_driver_probe(&efi_rtc_driver, efi_rtc_probe); |
| 224 | } |
| 225 | |
| 226 | static void __exit efi_rtc_exit(void) |
| 227 | { |
| 228 | platform_driver_unregister(&efi_rtc_driver); |
| 229 | } |
| 230 | |
| 231 | module_init(efi_rtc_init); |
| 232 | module_exit(efi_rtc_exit); |
| 233 | |
| 234 | MODULE_AUTHOR("dann frazier <dannf@hp.com>"); |
| 235 | MODULE_LICENSE("GPL"); |
| 236 | MODULE_DESCRIPTION("EFI RTC driver"); |