Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 1 | /* |
| 2 | * rtc-mrst.c: Driver for Moorestown virtual RTC |
| 3 | * |
| 4 | * (C) Copyright 2009 Intel Corporation |
| 5 | * Author: Jacob Pan (jacob.jun.pan@intel.com) |
| 6 | * Feng Tang (feng.tang@intel.com) |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; version 2 |
| 11 | * of the License. |
| 12 | * |
| 13 | * Note: |
| 14 | * VRTC is emulated by system controller firmware, the real HW |
| 15 | * RTC is located in the PMIC device. SCU FW shadows PMIC RTC |
| 16 | * in a memory mapped IO space that is visible to the host IA |
| 17 | * processor. |
| 18 | * |
| 19 | * This driver is based upon drivers/rtc/rtc-cmos.c |
| 20 | */ |
| 21 | |
| 22 | /* |
| 23 | * Note: |
| 24 | * * vRTC only supports binary mode and 24H mode |
| 25 | * * vRTC only support PIE and AIE, no UIE, and its PIE only happens |
| 26 | * at 23:59:59pm everyday, no support for adjustable frequency |
| 27 | * * Alarm function is also limited to hr/min/sec. |
| 28 | */ |
| 29 | |
| 30 | #include <linux/mod_devicetable.h> |
| 31 | #include <linux/platform_device.h> |
| 32 | #include <linux/interrupt.h> |
| 33 | #include <linux/spinlock.h> |
| 34 | #include <linux/kernel.h> |
Arnd Bergmann | 463a863 | 2016-05-30 20:57:51 +0200 | [diff] [blame] | 35 | #include <linux/mc146818rtc.h> |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 36 | #include <linux/module.h> |
| 37 | #include <linux/init.h> |
| 38 | #include <linux/sfi.h> |
| 39 | |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 40 | #include <asm/intel_scu_ipc.h> |
Kuppuswamy Sathyanarayanan | 05454c2 | 2013-10-17 15:35:27 -0700 | [diff] [blame] | 41 | #include <asm/intel-mid.h> |
| 42 | #include <asm/intel_mid_vrtc.h> |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 43 | |
| 44 | struct mrst_rtc { |
| 45 | struct rtc_device *rtc; |
| 46 | struct device *dev; |
| 47 | int irq; |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 48 | |
| 49 | u8 enabled_wake; |
| 50 | u8 suspend_ctrl; |
| 51 | }; |
| 52 | |
| 53 | static const char driver_name[] = "rtc_mrst"; |
| 54 | |
| 55 | #define RTC_IRQMASK (RTC_PF | RTC_AF) |
| 56 | |
| 57 | static inline int is_intr(u8 rtc_intr) |
| 58 | { |
| 59 | if (!(rtc_intr & RTC_IRQF)) |
| 60 | return 0; |
| 61 | return rtc_intr & RTC_IRQMASK; |
| 62 | } |
| 63 | |
Feng Tang | 168202c | 2011-02-15 00:13:32 +0800 | [diff] [blame] | 64 | static inline unsigned char vrtc_is_updating(void) |
| 65 | { |
| 66 | unsigned char uip; |
| 67 | unsigned long flags; |
| 68 | |
| 69 | spin_lock_irqsave(&rtc_lock, flags); |
| 70 | uip = (vrtc_cmos_read(RTC_FREQ_SELECT) & RTC_UIP); |
| 71 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 72 | return uip; |
| 73 | } |
| 74 | |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 75 | /* |
| 76 | * rtc_time's year contains the increment over 1900, but vRTC's YEAR |
| 77 | * register can't be programmed to value larger than 0x64, so vRTC |
Feng Tang | 57e6319 | 2011-11-10 13:23:39 +0000 | [diff] [blame] | 78 | * driver chose to use 1972 (1970 is UNIX time start point) as the base, |
Feng Tang | d3e1884 | 2010-11-17 12:11:24 +0000 | [diff] [blame] | 79 | * and does the translation at read/write time. |
| 80 | * |
Feng Tang | 57e6319 | 2011-11-10 13:23:39 +0000 | [diff] [blame] | 81 | * Why not just use 1970 as the offset? it's because using 1972 will |
Feng Tang | d3e1884 | 2010-11-17 12:11:24 +0000 | [diff] [blame] | 82 | * make it consistent in leap year setting for both vrtc and low-level |
Feng Tang | 57e6319 | 2011-11-10 13:23:39 +0000 | [diff] [blame] | 83 | * physical rtc devices. Then why not use 1960 as the offset? If we use |
| 84 | * 1960, for a device's first use, its YEAR register is 0 and the system |
| 85 | * year will be parsed as 1960 which is not a valid UNIX time and will |
| 86 | * cause many applications to fail mysteriously. |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 87 | */ |
| 88 | static int mrst_read_time(struct device *dev, struct rtc_time *time) |
| 89 | { |
| 90 | unsigned long flags; |
| 91 | |
Feng Tang | 168202c | 2011-02-15 00:13:32 +0800 | [diff] [blame] | 92 | if (vrtc_is_updating()) |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 93 | mdelay(20); |
| 94 | |
| 95 | spin_lock_irqsave(&rtc_lock, flags); |
| 96 | time->tm_sec = vrtc_cmos_read(RTC_SECONDS); |
| 97 | time->tm_min = vrtc_cmos_read(RTC_MINUTES); |
| 98 | time->tm_hour = vrtc_cmos_read(RTC_HOURS); |
| 99 | time->tm_mday = vrtc_cmos_read(RTC_DAY_OF_MONTH); |
| 100 | time->tm_mon = vrtc_cmos_read(RTC_MONTH); |
| 101 | time->tm_year = vrtc_cmos_read(RTC_YEAR); |
| 102 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 103 | |
Feng Tang | 57e6319 | 2011-11-10 13:23:39 +0000 | [diff] [blame] | 104 | /* Adjust for the 1972/1900 */ |
| 105 | time->tm_year += 72; |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 106 | time->tm_mon--; |
Alexandre Belloni | 22652ba | 2018-02-19 16:23:56 +0100 | [diff] [blame] | 107 | return 0; |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static int mrst_set_time(struct device *dev, struct rtc_time *time) |
| 111 | { |
| 112 | int ret; |
| 113 | unsigned long flags; |
| 114 | unsigned char mon, day, hrs, min, sec; |
| 115 | unsigned int yrs; |
| 116 | |
| 117 | yrs = time->tm_year; |
| 118 | mon = time->tm_mon + 1; /* tm_mon starts at zero */ |
| 119 | day = time->tm_mday; |
| 120 | hrs = time->tm_hour; |
| 121 | min = time->tm_min; |
| 122 | sec = time->tm_sec; |
| 123 | |
Alexandre Belloni | f441f98 | 2018-03-10 06:32:00 +0100 | [diff] [blame] | 124 | if (yrs < 72 || yrs > 172) |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 125 | return -EINVAL; |
Feng Tang | 57e6319 | 2011-11-10 13:23:39 +0000 | [diff] [blame] | 126 | yrs -= 72; |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 127 | |
| 128 | spin_lock_irqsave(&rtc_lock, flags); |
| 129 | |
| 130 | vrtc_cmos_write(yrs, RTC_YEAR); |
| 131 | vrtc_cmos_write(mon, RTC_MONTH); |
| 132 | vrtc_cmos_write(day, RTC_DAY_OF_MONTH); |
| 133 | vrtc_cmos_write(hrs, RTC_HOURS); |
| 134 | vrtc_cmos_write(min, RTC_MINUTES); |
| 135 | vrtc_cmos_write(sec, RTC_SECONDS); |
| 136 | |
| 137 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 138 | |
| 139 | ret = intel_scu_ipc_simple_command(IPCMSG_VRTC, IPC_CMD_VRTC_SETTIME); |
| 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | static int mrst_read_alarm(struct device *dev, struct rtc_wkalrm *t) |
| 144 | { |
| 145 | struct mrst_rtc *mrst = dev_get_drvdata(dev); |
| 146 | unsigned char rtc_control; |
| 147 | |
| 148 | if (mrst->irq <= 0) |
| 149 | return -EIO; |
| 150 | |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 151 | /* vRTC only supports binary mode */ |
| 152 | spin_lock_irq(&rtc_lock); |
| 153 | t->time.tm_sec = vrtc_cmos_read(RTC_SECONDS_ALARM); |
| 154 | t->time.tm_min = vrtc_cmos_read(RTC_MINUTES_ALARM); |
| 155 | t->time.tm_hour = vrtc_cmos_read(RTC_HOURS_ALARM); |
| 156 | |
| 157 | rtc_control = vrtc_cmos_read(RTC_CONTROL); |
| 158 | spin_unlock_irq(&rtc_lock); |
| 159 | |
| 160 | t->enabled = !!(rtc_control & RTC_AIE); |
| 161 | t->pending = 0; |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static void mrst_checkintr(struct mrst_rtc *mrst, unsigned char rtc_control) |
| 167 | { |
| 168 | unsigned char rtc_intr; |
| 169 | |
| 170 | /* |
| 171 | * NOTE after changing RTC_xIE bits we always read INTR_FLAGS; |
| 172 | * allegedly some older rtcs need that to handle irqs properly |
| 173 | */ |
| 174 | rtc_intr = vrtc_cmos_read(RTC_INTR_FLAGS); |
| 175 | rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF; |
| 176 | if (is_intr(rtc_intr)) |
| 177 | rtc_update_irq(mrst->rtc, 1, rtc_intr); |
| 178 | } |
| 179 | |
| 180 | static void mrst_irq_enable(struct mrst_rtc *mrst, unsigned char mask) |
| 181 | { |
| 182 | unsigned char rtc_control; |
| 183 | |
| 184 | /* |
| 185 | * Flush any pending IRQ status, notably for update irqs, |
| 186 | * before we enable new IRQs |
| 187 | */ |
| 188 | rtc_control = vrtc_cmos_read(RTC_CONTROL); |
| 189 | mrst_checkintr(mrst, rtc_control); |
| 190 | |
| 191 | rtc_control |= mask; |
| 192 | vrtc_cmos_write(rtc_control, RTC_CONTROL); |
| 193 | |
| 194 | mrst_checkintr(mrst, rtc_control); |
| 195 | } |
| 196 | |
| 197 | static void mrst_irq_disable(struct mrst_rtc *mrst, unsigned char mask) |
| 198 | { |
| 199 | unsigned char rtc_control; |
| 200 | |
| 201 | rtc_control = vrtc_cmos_read(RTC_CONTROL); |
| 202 | rtc_control &= ~mask; |
| 203 | vrtc_cmos_write(rtc_control, RTC_CONTROL); |
| 204 | mrst_checkintr(mrst, rtc_control); |
| 205 | } |
| 206 | |
| 207 | static int mrst_set_alarm(struct device *dev, struct rtc_wkalrm *t) |
| 208 | { |
| 209 | struct mrst_rtc *mrst = dev_get_drvdata(dev); |
| 210 | unsigned char hrs, min, sec; |
| 211 | int ret = 0; |
| 212 | |
| 213 | if (!mrst->irq) |
| 214 | return -EIO; |
| 215 | |
| 216 | hrs = t->time.tm_hour; |
| 217 | min = t->time.tm_min; |
| 218 | sec = t->time.tm_sec; |
| 219 | |
| 220 | spin_lock_irq(&rtc_lock); |
| 221 | /* Next rtc irq must not be from previous alarm setting */ |
| 222 | mrst_irq_disable(mrst, RTC_AIE); |
| 223 | |
| 224 | /* Update alarm */ |
| 225 | vrtc_cmos_write(hrs, RTC_HOURS_ALARM); |
| 226 | vrtc_cmos_write(min, RTC_MINUTES_ALARM); |
| 227 | vrtc_cmos_write(sec, RTC_SECONDS_ALARM); |
| 228 | |
| 229 | spin_unlock_irq(&rtc_lock); |
| 230 | |
| 231 | ret = intel_scu_ipc_simple_command(IPCMSG_VRTC, IPC_CMD_VRTC_SETALARM); |
| 232 | if (ret) |
| 233 | return ret; |
| 234 | |
| 235 | spin_lock_irq(&rtc_lock); |
| 236 | if (t->enabled) |
| 237 | mrst_irq_enable(mrst, RTC_AIE); |
| 238 | |
| 239 | spin_unlock_irq(&rtc_lock); |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 244 | /* Currently, the vRTC doesn't support UIE ON/OFF */ |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 245 | static int mrst_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 246 | { |
| 247 | struct mrst_rtc *mrst = dev_get_drvdata(dev); |
| 248 | unsigned long flags; |
| 249 | |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 250 | spin_lock_irqsave(&rtc_lock, flags); |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 251 | if (enabled) |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 252 | mrst_irq_enable(mrst, RTC_AIE); |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 253 | else |
| 254 | mrst_irq_disable(mrst, RTC_AIE); |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 255 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 256 | return 0; |
| 257 | } |
| 258 | |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 259 | |
Javier Martinez Canillas | 6fca3fc | 2016-04-26 18:17:35 -0400 | [diff] [blame] | 260 | #if IS_ENABLED(CONFIG_RTC_INTF_PROC) |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 261 | |
| 262 | static int mrst_procfs(struct device *dev, struct seq_file *seq) |
| 263 | { |
| 264 | unsigned char rtc_control, valid; |
| 265 | |
| 266 | spin_lock_irq(&rtc_lock); |
| 267 | rtc_control = vrtc_cmos_read(RTC_CONTROL); |
| 268 | valid = vrtc_cmos_read(RTC_VALID); |
| 269 | spin_unlock_irq(&rtc_lock); |
| 270 | |
Joe Perches | 4395eb1 | 2015-04-15 16:17:51 -0700 | [diff] [blame] | 271 | seq_printf(seq, |
| 272 | "periodic_IRQ\t: %s\n" |
| 273 | "alarm\t\t: %s\n" |
| 274 | "BCD\t\t: no\n" |
| 275 | "periodic_freq\t: daily (not adjustable)\n", |
| 276 | (rtc_control & RTC_PIE) ? "on" : "off", |
| 277 | (rtc_control & RTC_AIE) ? "on" : "off"); |
| 278 | |
| 279 | return 0; |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | #else |
| 283 | #define mrst_procfs NULL |
| 284 | #endif |
| 285 | |
| 286 | static const struct rtc_class_ops mrst_rtc_ops = { |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 287 | .read_time = mrst_read_time, |
| 288 | .set_time = mrst_set_time, |
| 289 | .read_alarm = mrst_read_alarm, |
| 290 | .set_alarm = mrst_set_alarm, |
| 291 | .proc = mrst_procfs, |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 292 | .alarm_irq_enable = mrst_rtc_alarm_irq_enable, |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 293 | }; |
| 294 | |
| 295 | static struct mrst_rtc mrst_rtc; |
| 296 | |
| 297 | /* |
| 298 | * When vRTC IRQ is captured by SCU FW, FW will clear the AIE bit in |
| 299 | * Reg B, so no need for this driver to clear it |
| 300 | */ |
| 301 | static irqreturn_t mrst_rtc_irq(int irq, void *p) |
| 302 | { |
| 303 | u8 irqstat; |
| 304 | |
| 305 | spin_lock(&rtc_lock); |
| 306 | /* This read will clear all IRQ flags inside Reg C */ |
| 307 | irqstat = vrtc_cmos_read(RTC_INTR_FLAGS); |
| 308 | spin_unlock(&rtc_lock); |
| 309 | |
| 310 | irqstat &= RTC_IRQMASK | RTC_IRQF; |
| 311 | if (is_intr(irqstat)) { |
| 312 | rtc_update_irq(p, 1, irqstat); |
| 313 | return IRQ_HANDLED; |
| 314 | } |
| 315 | return IRQ_NONE; |
| 316 | } |
| 317 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 318 | static int vrtc_mrst_do_probe(struct device *dev, struct resource *iomem, |
| 319 | int rtc_irq) |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 320 | { |
| 321 | int retval = 0; |
| 322 | unsigned char rtc_control; |
| 323 | |
| 324 | /* There can be only one ... */ |
| 325 | if (mrst_rtc.dev) |
| 326 | return -EBUSY; |
| 327 | |
| 328 | if (!iomem) |
| 329 | return -ENODEV; |
| 330 | |
Alexandre Belloni | 32b41f9 | 2018-06-05 16:18:17 +0200 | [diff] [blame^] | 331 | iomem = devm_request_mem_region(dev, iomem->start, resource_size(iomem), |
| 332 | driver_name); |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 333 | if (!iomem) { |
| 334 | dev_dbg(dev, "i/o mem already in use.\n"); |
| 335 | return -EBUSY; |
| 336 | } |
| 337 | |
| 338 | mrst_rtc.irq = rtc_irq; |
Feng Tang | de97a21 | 2011-04-07 09:39:44 +0800 | [diff] [blame] | 339 | mrst_rtc.dev = dev; |
| 340 | dev_set_drvdata(dev, &mrst_rtc); |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 341 | |
Alexandre Belloni | 32b41f9 | 2018-06-05 16:18:17 +0200 | [diff] [blame^] | 342 | mrst_rtc.rtc = devm_rtc_allocate_device(dev); |
| 343 | if (IS_ERR(mrst_rtc.rtc)) |
| 344 | return PTR_ERR(mrst_rtc.rtc); |
| 345 | |
| 346 | mrst_rtc.rtc->ops = &mrst_rtc_ops; |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 347 | |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 348 | rename_region(iomem, dev_name(&mrst_rtc.rtc->dev)); |
| 349 | |
| 350 | spin_lock_irq(&rtc_lock); |
| 351 | mrst_irq_disable(&mrst_rtc, RTC_PIE | RTC_AIE); |
| 352 | rtc_control = vrtc_cmos_read(RTC_CONTROL); |
| 353 | spin_unlock_irq(&rtc_lock); |
| 354 | |
| 355 | if (!(rtc_control & RTC_24H) || (rtc_control & (RTC_DM_BINARY))) |
| 356 | dev_dbg(dev, "TODO: support more than 24-hr BCD mode\n"); |
| 357 | |
| 358 | if (rtc_irq) { |
Alexandre Belloni | 32b41f9 | 2018-06-05 16:18:17 +0200 | [diff] [blame^] | 359 | retval = devm_request_irq(dev, rtc_irq, mrst_rtc_irq, |
| 360 | 0, dev_name(&mrst_rtc.rtc->dev), |
| 361 | mrst_rtc.rtc); |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 362 | if (retval < 0) { |
| 363 | dev_dbg(dev, "IRQ %d is already in use, err %d\n", |
| 364 | rtc_irq, retval); |
Alexandre Belloni | 32b41f9 | 2018-06-05 16:18:17 +0200 | [diff] [blame^] | 365 | goto cleanup0; |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 366 | } |
| 367 | } |
Alexandre Belloni | 32b41f9 | 2018-06-05 16:18:17 +0200 | [diff] [blame^] | 368 | |
| 369 | retval = rtc_register_device(mrst_rtc.rtc); |
| 370 | if (retval) { |
| 371 | retval = PTR_ERR(mrst_rtc.rtc); |
| 372 | goto cleanup0; |
| 373 | } |
| 374 | |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 375 | dev_dbg(dev, "initialised\n"); |
| 376 | return 0; |
| 377 | |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 378 | cleanup0: |
Feng Tang | de97a21 | 2011-04-07 09:39:44 +0800 | [diff] [blame] | 379 | mrst_rtc.dev = NULL; |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 380 | dev_err(dev, "rtc-mrst: unable to initialise\n"); |
| 381 | return retval; |
| 382 | } |
| 383 | |
| 384 | static void rtc_mrst_do_shutdown(void) |
| 385 | { |
| 386 | spin_lock_irq(&rtc_lock); |
| 387 | mrst_irq_disable(&mrst_rtc, RTC_IRQMASK); |
| 388 | spin_unlock_irq(&rtc_lock); |
| 389 | } |
| 390 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 391 | static void rtc_mrst_do_remove(struct device *dev) |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 392 | { |
| 393 | struct mrst_rtc *mrst = dev_get_drvdata(dev); |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 394 | |
| 395 | rtc_mrst_do_shutdown(); |
| 396 | |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 397 | mrst->rtc = NULL; |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 398 | mrst->dev = NULL; |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Lars-Peter Clausen | ddd2a30 | 2015-03-25 15:55:09 -0700 | [diff] [blame] | 401 | #ifdef CONFIG_PM_SLEEP |
| 402 | static int mrst_suspend(struct device *dev) |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 403 | { |
| 404 | struct mrst_rtc *mrst = dev_get_drvdata(dev); |
| 405 | unsigned char tmp; |
| 406 | |
| 407 | /* Only the alarm might be a wakeup event source */ |
| 408 | spin_lock_irq(&rtc_lock); |
| 409 | mrst->suspend_ctrl = tmp = vrtc_cmos_read(RTC_CONTROL); |
| 410 | if (tmp & (RTC_PIE | RTC_AIE)) { |
| 411 | unsigned char mask; |
| 412 | |
| 413 | if (device_may_wakeup(dev)) |
| 414 | mask = RTC_IRQMASK & ~RTC_AIE; |
| 415 | else |
| 416 | mask = RTC_IRQMASK; |
| 417 | tmp &= ~mask; |
| 418 | vrtc_cmos_write(tmp, RTC_CONTROL); |
| 419 | |
| 420 | mrst_checkintr(mrst, tmp); |
| 421 | } |
| 422 | spin_unlock_irq(&rtc_lock); |
| 423 | |
| 424 | if (tmp & RTC_AIE) { |
| 425 | mrst->enabled_wake = 1; |
| 426 | enable_irq_wake(mrst->irq); |
| 427 | } |
| 428 | |
| 429 | dev_dbg(&mrst_rtc.rtc->dev, "suspend%s, ctrl %02x\n", |
| 430 | (tmp & RTC_AIE) ? ", alarm may wake" : "", |
| 431 | tmp); |
| 432 | |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | /* |
| 437 | * We want RTC alarms to wake us from the deep power saving state |
| 438 | */ |
| 439 | static inline int mrst_poweroff(struct device *dev) |
| 440 | { |
Lars-Peter Clausen | ddd2a30 | 2015-03-25 15:55:09 -0700 | [diff] [blame] | 441 | return mrst_suspend(dev); |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | static int mrst_resume(struct device *dev) |
| 445 | { |
| 446 | struct mrst_rtc *mrst = dev_get_drvdata(dev); |
| 447 | unsigned char tmp = mrst->suspend_ctrl; |
| 448 | |
| 449 | /* Re-enable any irqs previously active */ |
| 450 | if (tmp & RTC_IRQMASK) { |
| 451 | unsigned char mask; |
| 452 | |
| 453 | if (mrst->enabled_wake) { |
| 454 | disable_irq_wake(mrst->irq); |
| 455 | mrst->enabled_wake = 0; |
| 456 | } |
| 457 | |
| 458 | spin_lock_irq(&rtc_lock); |
| 459 | do { |
| 460 | vrtc_cmos_write(tmp, RTC_CONTROL); |
| 461 | |
| 462 | mask = vrtc_cmos_read(RTC_INTR_FLAGS); |
| 463 | mask &= (tmp & RTC_IRQMASK) | RTC_IRQF; |
| 464 | if (!is_intr(mask)) |
| 465 | break; |
| 466 | |
| 467 | rtc_update_irq(mrst->rtc, 1, mask); |
| 468 | tmp &= ~RTC_AIE; |
| 469 | } while (mask & RTC_AIE); |
| 470 | spin_unlock_irq(&rtc_lock); |
| 471 | } |
| 472 | |
| 473 | dev_dbg(&mrst_rtc.rtc->dev, "resume, ctrl %02x\n", tmp); |
| 474 | |
| 475 | return 0; |
| 476 | } |
| 477 | |
Lars-Peter Clausen | ddd2a30 | 2015-03-25 15:55:09 -0700 | [diff] [blame] | 478 | static SIMPLE_DEV_PM_OPS(mrst_pm_ops, mrst_suspend, mrst_resume); |
| 479 | #define MRST_PM_OPS (&mrst_pm_ops) |
| 480 | |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 481 | #else |
Lars-Peter Clausen | ddd2a30 | 2015-03-25 15:55:09 -0700 | [diff] [blame] | 482 | #define MRST_PM_OPS NULL |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 483 | |
| 484 | static inline int mrst_poweroff(struct device *dev) |
| 485 | { |
| 486 | return -ENOSYS; |
| 487 | } |
| 488 | |
| 489 | #endif |
| 490 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 491 | static int vrtc_mrst_platform_probe(struct platform_device *pdev) |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 492 | { |
| 493 | return vrtc_mrst_do_probe(&pdev->dev, |
| 494 | platform_get_resource(pdev, IORESOURCE_MEM, 0), |
| 495 | platform_get_irq(pdev, 0)); |
| 496 | } |
| 497 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 498 | static int vrtc_mrst_platform_remove(struct platform_device *pdev) |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 499 | { |
| 500 | rtc_mrst_do_remove(&pdev->dev); |
| 501 | return 0; |
| 502 | } |
| 503 | |
| 504 | static void vrtc_mrst_platform_shutdown(struct platform_device *pdev) |
| 505 | { |
| 506 | if (system_state == SYSTEM_POWER_OFF && !mrst_poweroff(&pdev->dev)) |
| 507 | return; |
| 508 | |
| 509 | rtc_mrst_do_shutdown(); |
| 510 | } |
| 511 | |
| 512 | MODULE_ALIAS("platform:vrtc_mrst"); |
| 513 | |
| 514 | static struct platform_driver vrtc_mrst_platform_driver = { |
| 515 | .probe = vrtc_mrst_platform_probe, |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 516 | .remove = vrtc_mrst_platform_remove, |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 517 | .shutdown = vrtc_mrst_platform_shutdown, |
| 518 | .driver = { |
Lars-Peter Clausen | ddd2a30 | 2015-03-25 15:55:09 -0700 | [diff] [blame] | 519 | .name = driver_name, |
| 520 | .pm = MRST_PM_OPS, |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 521 | } |
| 522 | }; |
| 523 | |
Axel Lin | 0c4eae6 | 2012-01-10 15:10:48 -0800 | [diff] [blame] | 524 | module_platform_driver(vrtc_mrst_platform_driver); |
Feng Tang | 0146f26 | 2010-11-10 17:29:17 +0000 | [diff] [blame] | 525 | |
| 526 | MODULE_AUTHOR("Jacob Pan; Feng Tang"); |
| 527 | MODULE_DESCRIPTION("Driver for Moorestown virtual RTC"); |
| 528 | MODULE_LICENSE("GPL"); |