Linux 4.1.16
[linux/fpc-iii.git] / drivers / rtc / rtc-mrst.c
blob548ea6f6f384b27f78e51b545116948d4c8cc652
1 /*
2 * rtc-mrst.c: Driver for Moorestown virtual RTC
4 * (C) Copyright 2009 Intel Corporation
5 * Author: Jacob Pan (jacob.jun.pan@intel.com)
6 * Feng Tang (feng.tang@intel.com)
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.
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.
19 * This driver is based upon drivers/rtc/rtc-cmos.c
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.
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>
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/sfi.h>
39 #include <asm-generic/rtc.h>
40 #include <asm/intel_scu_ipc.h>
41 #include <asm/intel-mid.h>
42 #include <asm/intel_mid_vrtc.h>
44 struct mrst_rtc {
45 struct rtc_device *rtc;
46 struct device *dev;
47 int irq;
48 struct resource *iomem;
50 u8 enabled_wake;
51 u8 suspend_ctrl;
54 static const char driver_name[] = "rtc_mrst";
56 #define RTC_IRQMASK (RTC_PF | RTC_AF)
58 static inline int is_intr(u8 rtc_intr)
60 if (!(rtc_intr & RTC_IRQF))
61 return 0;
62 return rtc_intr & RTC_IRQMASK;
65 static inline unsigned char vrtc_is_updating(void)
67 unsigned char uip;
68 unsigned long flags;
70 spin_lock_irqsave(&rtc_lock, flags);
71 uip = (vrtc_cmos_read(RTC_FREQ_SELECT) & RTC_UIP);
72 spin_unlock_irqrestore(&rtc_lock, flags);
73 return uip;
77 * rtc_time's year contains the increment over 1900, but vRTC's YEAR
78 * register can't be programmed to value larger than 0x64, so vRTC
79 * driver chose to use 1972 (1970 is UNIX time start point) as the base,
80 * and does the translation at read/write time.
82 * Why not just use 1970 as the offset? it's because using 1972 will
83 * make it consistent in leap year setting for both vrtc and low-level
84 * physical rtc devices. Then why not use 1960 as the offset? If we use
85 * 1960, for a device's first use, its YEAR register is 0 and the system
86 * year will be parsed as 1960 which is not a valid UNIX time and will
87 * cause many applications to fail mysteriously.
89 static int mrst_read_time(struct device *dev, struct rtc_time *time)
91 unsigned long flags;
93 if (vrtc_is_updating())
94 mdelay(20);
96 spin_lock_irqsave(&rtc_lock, flags);
97 time->tm_sec = vrtc_cmos_read(RTC_SECONDS);
98 time->tm_min = vrtc_cmos_read(RTC_MINUTES);
99 time->tm_hour = vrtc_cmos_read(RTC_HOURS);
100 time->tm_mday = vrtc_cmos_read(RTC_DAY_OF_MONTH);
101 time->tm_mon = vrtc_cmos_read(RTC_MONTH);
102 time->tm_year = vrtc_cmos_read(RTC_YEAR);
103 spin_unlock_irqrestore(&rtc_lock, flags);
105 /* Adjust for the 1972/1900 */
106 time->tm_year += 72;
107 time->tm_mon--;
108 return rtc_valid_tm(time);
111 static int mrst_set_time(struct device *dev, struct rtc_time *time)
113 int ret;
114 unsigned long flags;
115 unsigned char mon, day, hrs, min, sec;
116 unsigned int yrs;
118 yrs = time->tm_year;
119 mon = time->tm_mon + 1; /* tm_mon starts at zero */
120 day = time->tm_mday;
121 hrs = time->tm_hour;
122 min = time->tm_min;
123 sec = time->tm_sec;
125 if (yrs < 72 || yrs > 138)
126 return -EINVAL;
127 yrs -= 72;
129 spin_lock_irqsave(&rtc_lock, flags);
131 vrtc_cmos_write(yrs, RTC_YEAR);
132 vrtc_cmos_write(mon, RTC_MONTH);
133 vrtc_cmos_write(day, RTC_DAY_OF_MONTH);
134 vrtc_cmos_write(hrs, RTC_HOURS);
135 vrtc_cmos_write(min, RTC_MINUTES);
136 vrtc_cmos_write(sec, RTC_SECONDS);
138 spin_unlock_irqrestore(&rtc_lock, flags);
140 ret = intel_scu_ipc_simple_command(IPCMSG_VRTC, IPC_CMD_VRTC_SETTIME);
141 return ret;
144 static int mrst_read_alarm(struct device *dev, struct rtc_wkalrm *t)
146 struct mrst_rtc *mrst = dev_get_drvdata(dev);
147 unsigned char rtc_control;
149 if (mrst->irq <= 0)
150 return -EIO;
152 /* Basic alarms only support hour, minute, and seconds fields.
153 * Some also support day and month, for alarms up to a year in
154 * the future.
156 t->time.tm_mday = -1;
157 t->time.tm_mon = -1;
158 t->time.tm_year = -1;
160 /* vRTC only supports binary mode */
161 spin_lock_irq(&rtc_lock);
162 t->time.tm_sec = vrtc_cmos_read(RTC_SECONDS_ALARM);
163 t->time.tm_min = vrtc_cmos_read(RTC_MINUTES_ALARM);
164 t->time.tm_hour = vrtc_cmos_read(RTC_HOURS_ALARM);
166 rtc_control = vrtc_cmos_read(RTC_CONTROL);
167 spin_unlock_irq(&rtc_lock);
169 t->enabled = !!(rtc_control & RTC_AIE);
170 t->pending = 0;
172 return 0;
175 static void mrst_checkintr(struct mrst_rtc *mrst, unsigned char rtc_control)
177 unsigned char rtc_intr;
180 * NOTE after changing RTC_xIE bits we always read INTR_FLAGS;
181 * allegedly some older rtcs need that to handle irqs properly
183 rtc_intr = vrtc_cmos_read(RTC_INTR_FLAGS);
184 rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
185 if (is_intr(rtc_intr))
186 rtc_update_irq(mrst->rtc, 1, rtc_intr);
189 static void mrst_irq_enable(struct mrst_rtc *mrst, unsigned char mask)
191 unsigned char rtc_control;
194 * Flush any pending IRQ status, notably for update irqs,
195 * before we enable new IRQs
197 rtc_control = vrtc_cmos_read(RTC_CONTROL);
198 mrst_checkintr(mrst, rtc_control);
200 rtc_control |= mask;
201 vrtc_cmos_write(rtc_control, RTC_CONTROL);
203 mrst_checkintr(mrst, rtc_control);
206 static void mrst_irq_disable(struct mrst_rtc *mrst, unsigned char mask)
208 unsigned char rtc_control;
210 rtc_control = vrtc_cmos_read(RTC_CONTROL);
211 rtc_control &= ~mask;
212 vrtc_cmos_write(rtc_control, RTC_CONTROL);
213 mrst_checkintr(mrst, rtc_control);
216 static int mrst_set_alarm(struct device *dev, struct rtc_wkalrm *t)
218 struct mrst_rtc *mrst = dev_get_drvdata(dev);
219 unsigned char hrs, min, sec;
220 int ret = 0;
222 if (!mrst->irq)
223 return -EIO;
225 hrs = t->time.tm_hour;
226 min = t->time.tm_min;
227 sec = t->time.tm_sec;
229 spin_lock_irq(&rtc_lock);
230 /* Next rtc irq must not be from previous alarm setting */
231 mrst_irq_disable(mrst, RTC_AIE);
233 /* Update alarm */
234 vrtc_cmos_write(hrs, RTC_HOURS_ALARM);
235 vrtc_cmos_write(min, RTC_MINUTES_ALARM);
236 vrtc_cmos_write(sec, RTC_SECONDS_ALARM);
238 spin_unlock_irq(&rtc_lock);
240 ret = intel_scu_ipc_simple_command(IPCMSG_VRTC, IPC_CMD_VRTC_SETALARM);
241 if (ret)
242 return ret;
244 spin_lock_irq(&rtc_lock);
245 if (t->enabled)
246 mrst_irq_enable(mrst, RTC_AIE);
248 spin_unlock_irq(&rtc_lock);
250 return 0;
253 /* Currently, the vRTC doesn't support UIE ON/OFF */
254 static int mrst_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
256 struct mrst_rtc *mrst = dev_get_drvdata(dev);
257 unsigned long flags;
259 spin_lock_irqsave(&rtc_lock, flags);
260 if (enabled)
261 mrst_irq_enable(mrst, RTC_AIE);
262 else
263 mrst_irq_disable(mrst, RTC_AIE);
264 spin_unlock_irqrestore(&rtc_lock, flags);
265 return 0;
269 #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
271 static int mrst_procfs(struct device *dev, struct seq_file *seq)
273 unsigned char rtc_control, valid;
275 spin_lock_irq(&rtc_lock);
276 rtc_control = vrtc_cmos_read(RTC_CONTROL);
277 valid = vrtc_cmos_read(RTC_VALID);
278 spin_unlock_irq(&rtc_lock);
280 seq_printf(seq,
281 "periodic_IRQ\t: %s\n"
282 "alarm\t\t: %s\n"
283 "BCD\t\t: no\n"
284 "periodic_freq\t: daily (not adjustable)\n",
285 (rtc_control & RTC_PIE) ? "on" : "off",
286 (rtc_control & RTC_AIE) ? "on" : "off");
288 return 0;
291 #else
292 #define mrst_procfs NULL
293 #endif
295 static const struct rtc_class_ops mrst_rtc_ops = {
296 .read_time = mrst_read_time,
297 .set_time = mrst_set_time,
298 .read_alarm = mrst_read_alarm,
299 .set_alarm = mrst_set_alarm,
300 .proc = mrst_procfs,
301 .alarm_irq_enable = mrst_rtc_alarm_irq_enable,
304 static struct mrst_rtc mrst_rtc;
307 * When vRTC IRQ is captured by SCU FW, FW will clear the AIE bit in
308 * Reg B, so no need for this driver to clear it
310 static irqreturn_t mrst_rtc_irq(int irq, void *p)
312 u8 irqstat;
314 spin_lock(&rtc_lock);
315 /* This read will clear all IRQ flags inside Reg C */
316 irqstat = vrtc_cmos_read(RTC_INTR_FLAGS);
317 spin_unlock(&rtc_lock);
319 irqstat &= RTC_IRQMASK | RTC_IRQF;
320 if (is_intr(irqstat)) {
321 rtc_update_irq(p, 1, irqstat);
322 return IRQ_HANDLED;
324 return IRQ_NONE;
327 static int vrtc_mrst_do_probe(struct device *dev, struct resource *iomem,
328 int rtc_irq)
330 int retval = 0;
331 unsigned char rtc_control;
333 /* There can be only one ... */
334 if (mrst_rtc.dev)
335 return -EBUSY;
337 if (!iomem)
338 return -ENODEV;
340 iomem = request_mem_region(iomem->start, resource_size(iomem),
341 driver_name);
342 if (!iomem) {
343 dev_dbg(dev, "i/o mem already in use.\n");
344 return -EBUSY;
347 mrst_rtc.irq = rtc_irq;
348 mrst_rtc.iomem = iomem;
349 mrst_rtc.dev = dev;
350 dev_set_drvdata(dev, &mrst_rtc);
352 mrst_rtc.rtc = rtc_device_register(driver_name, dev,
353 &mrst_rtc_ops, THIS_MODULE);
354 if (IS_ERR(mrst_rtc.rtc)) {
355 retval = PTR_ERR(mrst_rtc.rtc);
356 goto cleanup0;
359 rename_region(iomem, dev_name(&mrst_rtc.rtc->dev));
361 spin_lock_irq(&rtc_lock);
362 mrst_irq_disable(&mrst_rtc, RTC_PIE | RTC_AIE);
363 rtc_control = vrtc_cmos_read(RTC_CONTROL);
364 spin_unlock_irq(&rtc_lock);
366 if (!(rtc_control & RTC_24H) || (rtc_control & (RTC_DM_BINARY)))
367 dev_dbg(dev, "TODO: support more than 24-hr BCD mode\n");
369 if (rtc_irq) {
370 retval = request_irq(rtc_irq, mrst_rtc_irq,
371 0, dev_name(&mrst_rtc.rtc->dev),
372 mrst_rtc.rtc);
373 if (retval < 0) {
374 dev_dbg(dev, "IRQ %d is already in use, err %d\n",
375 rtc_irq, retval);
376 goto cleanup1;
379 dev_dbg(dev, "initialised\n");
380 return 0;
382 cleanup1:
383 rtc_device_unregister(mrst_rtc.rtc);
384 cleanup0:
385 mrst_rtc.dev = NULL;
386 release_mem_region(iomem->start, resource_size(iomem));
387 dev_err(dev, "rtc-mrst: unable to initialise\n");
388 return retval;
391 static void rtc_mrst_do_shutdown(void)
393 spin_lock_irq(&rtc_lock);
394 mrst_irq_disable(&mrst_rtc, RTC_IRQMASK);
395 spin_unlock_irq(&rtc_lock);
398 static void rtc_mrst_do_remove(struct device *dev)
400 struct mrst_rtc *mrst = dev_get_drvdata(dev);
401 struct resource *iomem;
403 rtc_mrst_do_shutdown();
405 if (mrst->irq)
406 free_irq(mrst->irq, mrst->rtc);
408 rtc_device_unregister(mrst->rtc);
409 mrst->rtc = NULL;
411 iomem = mrst->iomem;
412 release_mem_region(iomem->start, resource_size(iomem));
413 mrst->iomem = NULL;
415 mrst->dev = NULL;
418 #ifdef CONFIG_PM_SLEEP
419 static int mrst_suspend(struct device *dev)
421 struct mrst_rtc *mrst = dev_get_drvdata(dev);
422 unsigned char tmp;
424 /* Only the alarm might be a wakeup event source */
425 spin_lock_irq(&rtc_lock);
426 mrst->suspend_ctrl = tmp = vrtc_cmos_read(RTC_CONTROL);
427 if (tmp & (RTC_PIE | RTC_AIE)) {
428 unsigned char mask;
430 if (device_may_wakeup(dev))
431 mask = RTC_IRQMASK & ~RTC_AIE;
432 else
433 mask = RTC_IRQMASK;
434 tmp &= ~mask;
435 vrtc_cmos_write(tmp, RTC_CONTROL);
437 mrst_checkintr(mrst, tmp);
439 spin_unlock_irq(&rtc_lock);
441 if (tmp & RTC_AIE) {
442 mrst->enabled_wake = 1;
443 enable_irq_wake(mrst->irq);
446 dev_dbg(&mrst_rtc.rtc->dev, "suspend%s, ctrl %02x\n",
447 (tmp & RTC_AIE) ? ", alarm may wake" : "",
448 tmp);
450 return 0;
454 * We want RTC alarms to wake us from the deep power saving state
456 static inline int mrst_poweroff(struct device *dev)
458 return mrst_suspend(dev);
461 static int mrst_resume(struct device *dev)
463 struct mrst_rtc *mrst = dev_get_drvdata(dev);
464 unsigned char tmp = mrst->suspend_ctrl;
466 /* Re-enable any irqs previously active */
467 if (tmp & RTC_IRQMASK) {
468 unsigned char mask;
470 if (mrst->enabled_wake) {
471 disable_irq_wake(mrst->irq);
472 mrst->enabled_wake = 0;
475 spin_lock_irq(&rtc_lock);
476 do {
477 vrtc_cmos_write(tmp, RTC_CONTROL);
479 mask = vrtc_cmos_read(RTC_INTR_FLAGS);
480 mask &= (tmp & RTC_IRQMASK) | RTC_IRQF;
481 if (!is_intr(mask))
482 break;
484 rtc_update_irq(mrst->rtc, 1, mask);
485 tmp &= ~RTC_AIE;
486 } while (mask & RTC_AIE);
487 spin_unlock_irq(&rtc_lock);
490 dev_dbg(&mrst_rtc.rtc->dev, "resume, ctrl %02x\n", tmp);
492 return 0;
495 static SIMPLE_DEV_PM_OPS(mrst_pm_ops, mrst_suspend, mrst_resume);
496 #define MRST_PM_OPS (&mrst_pm_ops)
498 #else
499 #define MRST_PM_OPS NULL
501 static inline int mrst_poweroff(struct device *dev)
503 return -ENOSYS;
506 #endif
508 static int vrtc_mrst_platform_probe(struct platform_device *pdev)
510 return vrtc_mrst_do_probe(&pdev->dev,
511 platform_get_resource(pdev, IORESOURCE_MEM, 0),
512 platform_get_irq(pdev, 0));
515 static int vrtc_mrst_platform_remove(struct platform_device *pdev)
517 rtc_mrst_do_remove(&pdev->dev);
518 return 0;
521 static void vrtc_mrst_platform_shutdown(struct platform_device *pdev)
523 if (system_state == SYSTEM_POWER_OFF && !mrst_poweroff(&pdev->dev))
524 return;
526 rtc_mrst_do_shutdown();
529 MODULE_ALIAS("platform:vrtc_mrst");
531 static struct platform_driver vrtc_mrst_platform_driver = {
532 .probe = vrtc_mrst_platform_probe,
533 .remove = vrtc_mrst_platform_remove,
534 .shutdown = vrtc_mrst_platform_shutdown,
535 .driver = {
536 .name = driver_name,
537 .pm = MRST_PM_OPS,
541 module_platform_driver(vrtc_mrst_platform_driver);
543 MODULE_AUTHOR("Jacob Pan; Feng Tang");
544 MODULE_DESCRIPTION("Driver for Moorestown virtual RTC");
545 MODULE_LICENSE("GPL");