blob: 91d58bdc7f881949087054b3f62cfa0b346cd2e6 [file] [log] [blame]
Richard Purdiee842f1c2006-03-27 01:16:46 -08001/*
2 * Real Time Clock interface for StrongARM SA1x00 and XScale PXA2xx
3 *
4 * Copyright (c) 2000 Nils Faerber
5 *
6 * Based on rtc.c by Paul Gortmaker
7 *
8 * Original Driver by Nils Faerber <nils@kernelconcepts.de>
9 *
10 * Modifications from:
11 * CIH <cih@coventive.com>
Nicolas Pitre2f82af02009-09-14 03:25:28 -040012 * Nicolas Pitre <nico@fluxnic.net>
Richard Purdiee842f1c2006-03-27 01:16:46 -080013 * Andrew Christian <andrew.christian@hp.com>
14 *
15 * Converted to the RTC subsystem and Driver Model
16 * by Richard Purdie <rpurdie@rpsys.net>
17 *
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License
20 * as published by the Free Software Foundation; either version
21 * 2 of the License, or (at your option) any later version.
22 */
23
24#include <linux/platform_device.h>
25#include <linux/module.h>
26#include <linux/rtc.h>
27#include <linux/init.h>
28#include <linux/fs.h>
29#include <linux/interrupt.h>
Russell Kinga0164a52012-01-19 11:55:21 +000030#include <linux/string.h>
Richard Purdiee842f1c2006-03-27 01:16:46 -080031#include <linux/pm.h>
Russell Kinga0164a52012-01-19 11:55:21 +000032#include <linux/bitops.h>
Richard Purdiee842f1c2006-03-27 01:16:46 -080033
Russell Kinga09e64f2008-08-05 16:14:15 +010034#include <mach/hardware.h>
Richard Purdiee842f1c2006-03-27 01:16:46 -080035#include <asm/irq.h>
Richard Purdiee842f1c2006-03-27 01:16:46 -080036
Russell Kinga0164a52012-01-19 11:55:21 +000037#ifdef CONFIG_ARCH_PXA
38#include <mach/regs-rtc.h>
39#endif
40
Marcelo Roberto Jimeneza404ad12010-10-18 22:33:53 +010041#define RTC_DEF_DIVIDER (32768 - 1)
Richard Purdiee842f1c2006-03-27 01:16:46 -080042#define RTC_DEF_TRIM 0
43
Russell Kinga0164a52012-01-19 11:55:21 +000044static const unsigned long RTC_FREQ = 1024;
Russell Kinga0164a52012-01-19 11:55:21 +000045static DEFINE_SPINLOCK(sa1100_rtc_lock);
Richard Purdiee842f1c2006-03-27 01:16:46 -080046
David Howells7d12e782006-10-05 14:55:46 +010047static irqreturn_t sa1100_rtc_interrupt(int irq, void *dev_id)
Richard Purdiee842f1c2006-03-27 01:16:46 -080048{
49 struct platform_device *pdev = to_platform_device(dev_id);
Russell Kinga0164a52012-01-19 11:55:21 +000050 struct rtc_device *rtc = platform_get_drvdata(pdev);
Richard Purdiee842f1c2006-03-27 01:16:46 -080051 unsigned int rtsr;
52 unsigned long events = 0;
53
Russell Kinga0164a52012-01-19 11:55:21 +000054 spin_lock(&sa1100_rtc_lock);
Richard Purdiee842f1c2006-03-27 01:16:46 -080055
Russell Kinga0164a52012-01-19 11:55:21 +000056 rtsr = RTSR;
Richard Purdiee842f1c2006-03-27 01:16:46 -080057 /* clear interrupt sources */
Russell Kinga0164a52012-01-19 11:55:21 +000058 RTSR = 0;
Marcelo Roberto Jimenez7decaa52010-10-18 22:35:54 +010059 /* Fix for a nasty initialization problem the in SA11xx RTSR register.
60 * See also the comments in sa1100_rtc_probe(). */
61 if (rtsr & (RTSR_ALE | RTSR_HZE)) {
62 /* This is the original code, before there was the if test
63 * above. This code does not clear interrupts that were not
64 * enabled. */
Russell Kinga0164a52012-01-19 11:55:21 +000065 RTSR = (RTSR_AL | RTSR_HZ) & (rtsr >> 2);
Marcelo Roberto Jimenez7decaa52010-10-18 22:35:54 +010066 } else {
67 /* For some reason, it is possible to enter this routine
68 * without interruptions enabled, it has been tested with
69 * several units (Bug in SA11xx chip?).
70 *
71 * This situation leads to an infinite "loop" of interrupt
72 * routine calling and as a result the processor seems to
73 * lock on its first call to open(). */
Russell Kinga0164a52012-01-19 11:55:21 +000074 RTSR = RTSR_AL | RTSR_HZ;
Marcelo Roberto Jimenez7decaa52010-10-18 22:35:54 +010075 }
Richard Purdiee842f1c2006-03-27 01:16:46 -080076
77 /* clear alarm interrupt if it has occurred */
78 if (rtsr & RTSR_AL)
79 rtsr &= ~RTSR_ALE;
Russell Kinga0164a52012-01-19 11:55:21 +000080 RTSR = rtsr & (RTSR_ALE | RTSR_HZE);
Richard Purdiee842f1c2006-03-27 01:16:46 -080081
82 /* update irq data & counter */
83 if (rtsr & RTSR_AL)
84 events |= RTC_AF | RTC_IRQF;
85 if (rtsr & RTSR_HZ)
86 events |= RTC_UF | RTC_IRQF;
87
Russell Kinga0164a52012-01-19 11:55:21 +000088 rtc_update_irq(rtc, 1, events);
Richard Purdiee842f1c2006-03-27 01:16:46 -080089
Russell Kinga0164a52012-01-19 11:55:21 +000090 spin_unlock(&sa1100_rtc_lock);
Richard Purdiee842f1c2006-03-27 01:16:46 -080091
92 return IRQ_HANDLED;
93}
94
Richard Purdiee842f1c2006-03-27 01:16:46 -080095static int sa1100_rtc_open(struct device *dev)
96{
97 int ret;
Russell Kinga0164a52012-01-19 11:55:21 +000098 struct platform_device *plat_dev = to_platform_device(dev);
99 struct rtc_device *rtc = platform_get_drvdata(plat_dev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800100
Russell Kinga0164a52012-01-19 11:55:21 +0000101 ret = request_irq(IRQ_RTC1Hz, sa1100_rtc_interrupt, IRQF_DISABLED,
102 "rtc 1Hz", dev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800103 if (ret) {
Russell Kinga0164a52012-01-19 11:55:21 +0000104 dev_err(dev, "IRQ %d already in use.\n", IRQ_RTC1Hz);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800105 goto fail_ui;
106 }
Russell Kinga0164a52012-01-19 11:55:21 +0000107 ret = request_irq(IRQ_RTCAlrm, sa1100_rtc_interrupt, IRQF_DISABLED,
108 "rtc Alrm", dev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800109 if (ret) {
Russell Kinga0164a52012-01-19 11:55:21 +0000110 dev_err(dev, "IRQ %d already in use.\n", IRQ_RTCAlrm);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800111 goto fail_ai;
112 }
Russell Kinga0164a52012-01-19 11:55:21 +0000113 rtc->max_user_freq = RTC_FREQ;
114 rtc_irq_set_freq(rtc, NULL, RTC_FREQ);
Marcelo Roberto Jimenezd2ccb522010-12-16 21:31:32 +0100115
Richard Purdiee842f1c2006-03-27 01:16:46 -0800116 return 0;
117
Richard Purdiee842f1c2006-03-27 01:16:46 -0800118 fail_ai:
Russell Kinga0164a52012-01-19 11:55:21 +0000119 free_irq(IRQ_RTC1Hz, dev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800120 fail_ui:
121 return ret;
122}
123
124static void sa1100_rtc_release(struct device *dev)
125{
Russell Kinga0164a52012-01-19 11:55:21 +0000126 spin_lock_irq(&sa1100_rtc_lock);
127 RTSR = 0;
128 spin_unlock_irq(&sa1100_rtc_lock);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800129
Russell Kinga0164a52012-01-19 11:55:21 +0000130 free_irq(IRQ_RTCAlrm, dev);
131 free_irq(IRQ_RTC1Hz, dev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800132}
133
John Stultz16380c12011-02-02 17:02:41 -0800134static int sa1100_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
135{
Russell Kinga0164a52012-01-19 11:55:21 +0000136 spin_lock_irq(&sa1100_rtc_lock);
John Stultz16380c12011-02-02 17:02:41 -0800137 if (enabled)
Russell Kinga0164a52012-01-19 11:55:21 +0000138 RTSR |= RTSR_ALE;
John Stultz16380c12011-02-02 17:02:41 -0800139 else
Russell Kinga0164a52012-01-19 11:55:21 +0000140 RTSR &= ~RTSR_ALE;
141 spin_unlock_irq(&sa1100_rtc_lock);
John Stultz16380c12011-02-02 17:02:41 -0800142 return 0;
143}
144
Richard Purdiee842f1c2006-03-27 01:16:46 -0800145static int sa1100_rtc_read_time(struct device *dev, struct rtc_time *tm)
146{
Russell Kinga0164a52012-01-19 11:55:21 +0000147 rtc_time_to_tm(RCNR, tm);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800148 return 0;
149}
150
151static int sa1100_rtc_set_time(struct device *dev, struct rtc_time *tm)
152{
153 unsigned long time;
154 int ret;
155
156 ret = rtc_tm_to_time(tm, &time);
157 if (ret == 0)
Russell Kinga0164a52012-01-19 11:55:21 +0000158 RCNR = time;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800159 return ret;
160}
161
162static int sa1100_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
163{
Russell Kinga0164a52012-01-19 11:55:21 +0000164 u32 rtsr;
David Brownell32b49da2007-02-20 13:58:13 -0800165
Russell Kinga0164a52012-01-19 11:55:21 +0000166 rtsr = RTSR;
David Brownell32b49da2007-02-20 13:58:13 -0800167 alrm->enabled = (rtsr & RTSR_ALE) ? 1 : 0;
168 alrm->pending = (rtsr & RTSR_AL) ? 1 : 0;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800169 return 0;
170}
171
172static int sa1100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
173{
Haojian Zhuang1d8c38c2012-02-20 19:49:30 +0800174 unsigned long time;
Russell Kinga0164a52012-01-19 11:55:21 +0000175 int ret;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800176
Russell Kinga0164a52012-01-19 11:55:21 +0000177 spin_lock_irq(&sa1100_rtc_lock);
Haojian Zhuang1d8c38c2012-02-20 19:49:30 +0800178 ret = rtc_tm_to_time(&alrm->time, &time);
179 if (ret != 0)
180 goto out;
181 RTSR = RTSR & (RTSR_HZE|RTSR_ALE|RTSR_AL);
182 RTAR = time;
183 if (alrm->enabled)
184 RTSR |= RTSR_ALE;
185 else
186 RTSR &= ~RTSR_ALE;
187out:
Russell Kinga0164a52012-01-19 11:55:21 +0000188 spin_unlock_irq(&sa1100_rtc_lock);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800189
Russell Kinga0164a52012-01-19 11:55:21 +0000190 return ret;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800191}
192
193static int sa1100_rtc_proc(struct device *dev, struct seq_file *seq)
194{
Russell Kinga0164a52012-01-19 11:55:21 +0000195 seq_printf(seq, "trim/divider\t\t: 0x%08x\n", (u32) RTTR);
196 seq_printf(seq, "RTSR\t\t\t: 0x%08x\n", (u32)RTSR);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800197
198 return 0;
199}
200
David Brownellff8371a2006-09-30 23:28:17 -0700201static const struct rtc_class_ops sa1100_rtc_ops = {
Richard Purdiee842f1c2006-03-27 01:16:46 -0800202 .open = sa1100_rtc_open,
Richard Purdiee842f1c2006-03-27 01:16:46 -0800203 .release = sa1100_rtc_release,
Richard Purdiee842f1c2006-03-27 01:16:46 -0800204 .read_time = sa1100_rtc_read_time,
205 .set_time = sa1100_rtc_set_time,
206 .read_alarm = sa1100_rtc_read_alarm,
207 .set_alarm = sa1100_rtc_set_alarm,
208 .proc = sa1100_rtc_proc,
John Stultz16380c12011-02-02 17:02:41 -0800209 .alarm_irq_enable = sa1100_rtc_alarm_irq_enable,
Richard Purdiee842f1c2006-03-27 01:16:46 -0800210};
211
212static int sa1100_rtc_probe(struct platform_device *pdev)
213{
Russell Kinga0164a52012-01-19 11:55:21 +0000214 struct rtc_device *rtc;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800215
216 /*
217 * According to the manual we should be able to let RTTR be zero
218 * and then a default diviser for a 32.768KHz clock is used.
219 * Apparently this doesn't work, at least for my SA1110 rev 5.
220 * If the clock divider is uninitialized then reset it to the
221 * default value to get the 1Hz clock.
222 */
Russell Kinga0164a52012-01-19 11:55:21 +0000223 if (RTTR == 0) {
224 RTTR = RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16);
225 dev_warn(&pdev->dev, "warning: "
226 "initializing default clock divider/trim value\n");
Richard Purdiee842f1c2006-03-27 01:16:46 -0800227 /* The current RTC value probably doesn't make sense either */
Russell Kinga0164a52012-01-19 11:55:21 +0000228 RCNR = 0;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800229 }
230
Uli Luckase5a2c9c2008-06-18 09:54:03 +0100231 device_init_wakeup(&pdev->dev, 1);
232
Russell Kinga0164a52012-01-19 11:55:21 +0000233 rtc = rtc_device_register(pdev->name, &pdev->dev, &sa1100_rtc_ops,
234 THIS_MODULE);
235
236 if (IS_ERR(rtc))
237 return PTR_ERR(rtc);
238
239 platform_set_drvdata(pdev, rtc);
240
Marcelo Roberto Jimenez7decaa52010-10-18 22:35:54 +0100241 /* Fix for a nasty initialization problem the in SA11xx RTSR register.
242 * See also the comments in sa1100_rtc_interrupt().
243 *
244 * Sometimes bit 1 of the RTSR (RTSR_HZ) will wake up 1, which means an
245 * interrupt pending, even though interrupts were never enabled.
246 * In this case, this bit it must be reset before enabling
247 * interruptions to avoid a nonexistent interrupt to occur.
248 *
249 * In principle, the same problem would apply to bit 0, although it has
250 * never been observed to happen.
251 *
252 * This issue is addressed both here and in sa1100_rtc_interrupt().
253 * If the issue is not addressed here, in the times when the processor
254 * wakes up with the bit set there will be one spurious interrupt.
255 *
256 * The issue is also dealt with in sa1100_rtc_interrupt() to be on the
257 * safe side, once the condition that lead to this strange
258 * initialization is unknown and could in principle happen during
259 * normal processing.
260 *
261 * Notice that clearing bit 1 and 0 is accomplished by writting ONES to
262 * the corresponding bits in RTSR. */
Russell Kinga0164a52012-01-19 11:55:21 +0000263 RTSR = RTSR_AL | RTSR_HZ;
Marcelo Roberto Jimenez7decaa52010-10-18 22:35:54 +0100264
Richard Purdiee842f1c2006-03-27 01:16:46 -0800265 return 0;
266}
267
268static int sa1100_rtc_remove(struct platform_device *pdev)
269{
Russell Kinga0164a52012-01-19 11:55:21 +0000270 struct rtc_device *rtc = platform_get_drvdata(pdev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800271
Russell Kinga0164a52012-01-19 11:55:21 +0000272 if (rtc)
273 rtc_device_unregister(rtc);
274
Richard Purdiee842f1c2006-03-27 01:16:46 -0800275 return 0;
276}
277
Russell King6bc54e62007-11-12 22:49:58 +0000278#ifdef CONFIG_PM
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800279static int sa1100_rtc_suspend(struct device *dev)
Russell King6bc54e62007-11-12 22:49:58 +0000280{
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800281 if (device_may_wakeup(dev))
Russell Kinga0164a52012-01-19 11:55:21 +0000282 enable_irq_wake(IRQ_RTCAlrm);
Russell King6bc54e62007-11-12 22:49:58 +0000283 return 0;
284}
285
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800286static int sa1100_rtc_resume(struct device *dev)
Russell King6bc54e62007-11-12 22:49:58 +0000287{
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800288 if (device_may_wakeup(dev))
Russell Kinga0164a52012-01-19 11:55:21 +0000289 disable_irq_wake(IRQ_RTCAlrm);
Russell King6bc54e62007-11-12 22:49:58 +0000290 return 0;
291}
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800292
Alexey Dobriyan47145212009-12-14 18:00:08 -0800293static const struct dev_pm_ops sa1100_rtc_pm_ops = {
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800294 .suspend = sa1100_rtc_suspend,
295 .resume = sa1100_rtc_resume,
296};
Russell King6bc54e62007-11-12 22:49:58 +0000297#endif
298
Richard Purdiee842f1c2006-03-27 01:16:46 -0800299static struct platform_driver sa1100_rtc_driver = {
300 .probe = sa1100_rtc_probe,
301 .remove = sa1100_rtc_remove,
Richard Purdiee842f1c2006-03-27 01:16:46 -0800302 .driver = {
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800303 .name = "sa1100-rtc",
304#ifdef CONFIG_PM
305 .pm = &sa1100_rtc_pm_ops,
306#endif
Richard Purdiee842f1c2006-03-27 01:16:46 -0800307 },
308};
309
Axel Lin0c4eae62012-01-10 15:10:48 -0800310module_platform_driver(sa1100_rtc_driver);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800311
312MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
313MODULE_DESCRIPTION("SA11x0/PXA2xx Realtime Clock Driver (RTC)");
314MODULE_LICENSE("GPL");
Kay Sieversad28a072008-04-10 21:29:25 -0700315MODULE_ALIAS("platform:sa1100-rtc");