blob: 9683daf56d8e3a5dd7591fcb460645d3626341cd [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;
45static struct rtc_time rtc_alarm;
46static DEFINE_SPINLOCK(sa1100_rtc_lock);
Richard Purdiee842f1c2006-03-27 01:16:46 -080047
Russell King797276e2008-04-20 12:30:41 +010048/*
49 * Calculate the next alarm time given the requested alarm time mask
50 * and the current time.
51 */
Marcelo Roberto Jimeneza404ad12010-10-18 22:33:53 +010052static void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now,
53 struct rtc_time *alrm)
Russell King797276e2008-04-20 12:30:41 +010054{
55 unsigned long next_time;
56 unsigned long now_time;
57
58 next->tm_year = now->tm_year;
59 next->tm_mon = now->tm_mon;
60 next->tm_mday = now->tm_mday;
61 next->tm_hour = alrm->tm_hour;
62 next->tm_min = alrm->tm_min;
63 next->tm_sec = alrm->tm_sec;
64
65 rtc_tm_to_time(now, &now_time);
66 rtc_tm_to_time(next, &next_time);
67
68 if (next_time < now_time) {
69 /* Advance one day */
70 next_time += 60 * 60 * 24;
71 rtc_time_to_tm(next_time, next);
72 }
73}
74
David Howells7d12e782006-10-05 14:55:46 +010075static irqreturn_t sa1100_rtc_interrupt(int irq, void *dev_id)
Richard Purdiee842f1c2006-03-27 01:16:46 -080076{
77 struct platform_device *pdev = to_platform_device(dev_id);
Russell Kinga0164a52012-01-19 11:55:21 +000078 struct rtc_device *rtc = platform_get_drvdata(pdev);
Richard Purdiee842f1c2006-03-27 01:16:46 -080079 unsigned int rtsr;
80 unsigned long events = 0;
81
Russell Kinga0164a52012-01-19 11:55:21 +000082 spin_lock(&sa1100_rtc_lock);
Richard Purdiee842f1c2006-03-27 01:16:46 -080083
Russell Kinga0164a52012-01-19 11:55:21 +000084 rtsr = RTSR;
Richard Purdiee842f1c2006-03-27 01:16:46 -080085 /* clear interrupt sources */
Russell Kinga0164a52012-01-19 11:55:21 +000086 RTSR = 0;
Marcelo Roberto Jimenez7decaa52010-10-18 22:35:54 +010087 /* Fix for a nasty initialization problem the in SA11xx RTSR register.
88 * See also the comments in sa1100_rtc_probe(). */
89 if (rtsr & (RTSR_ALE | RTSR_HZE)) {
90 /* This is the original code, before there was the if test
91 * above. This code does not clear interrupts that were not
92 * enabled. */
Russell Kinga0164a52012-01-19 11:55:21 +000093 RTSR = (RTSR_AL | RTSR_HZ) & (rtsr >> 2);
Marcelo Roberto Jimenez7decaa52010-10-18 22:35:54 +010094 } else {
95 /* For some reason, it is possible to enter this routine
96 * without interruptions enabled, it has been tested with
97 * several units (Bug in SA11xx chip?).
98 *
99 * This situation leads to an infinite "loop" of interrupt
100 * routine calling and as a result the processor seems to
101 * lock on its first call to open(). */
Russell Kinga0164a52012-01-19 11:55:21 +0000102 RTSR = RTSR_AL | RTSR_HZ;
Marcelo Roberto Jimenez7decaa52010-10-18 22:35:54 +0100103 }
Richard Purdiee842f1c2006-03-27 01:16:46 -0800104
105 /* clear alarm interrupt if it has occurred */
106 if (rtsr & RTSR_AL)
107 rtsr &= ~RTSR_ALE;
Russell Kinga0164a52012-01-19 11:55:21 +0000108 RTSR = rtsr & (RTSR_ALE | RTSR_HZE);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800109
110 /* update irq data & counter */
111 if (rtsr & RTSR_AL)
112 events |= RTC_AF | RTC_IRQF;
113 if (rtsr & RTSR_HZ)
114 events |= RTC_UF | RTC_IRQF;
115
Russell Kinga0164a52012-01-19 11:55:21 +0000116 rtc_update_irq(rtc, 1, events);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800117
Russell Kinga0164a52012-01-19 11:55:21 +0000118 spin_unlock(&sa1100_rtc_lock);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800119
120 return IRQ_HANDLED;
121}
122
Richard Purdiee842f1c2006-03-27 01:16:46 -0800123static int sa1100_rtc_open(struct device *dev)
124{
125 int ret;
Russell Kinga0164a52012-01-19 11:55:21 +0000126 struct platform_device *plat_dev = to_platform_device(dev);
127 struct rtc_device *rtc = platform_get_drvdata(plat_dev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800128
Russell Kinga0164a52012-01-19 11:55:21 +0000129 ret = request_irq(IRQ_RTC1Hz, sa1100_rtc_interrupt, IRQF_DISABLED,
130 "rtc 1Hz", dev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800131 if (ret) {
Russell Kinga0164a52012-01-19 11:55:21 +0000132 dev_err(dev, "IRQ %d already in use.\n", IRQ_RTC1Hz);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800133 goto fail_ui;
134 }
Russell Kinga0164a52012-01-19 11:55:21 +0000135 ret = request_irq(IRQ_RTCAlrm, sa1100_rtc_interrupt, IRQF_DISABLED,
136 "rtc Alrm", dev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800137 if (ret) {
Russell Kinga0164a52012-01-19 11:55:21 +0000138 dev_err(dev, "IRQ %d already in use.\n", IRQ_RTCAlrm);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800139 goto fail_ai;
140 }
Russell Kinga0164a52012-01-19 11:55:21 +0000141 rtc->max_user_freq = RTC_FREQ;
142 rtc_irq_set_freq(rtc, NULL, RTC_FREQ);
Marcelo Roberto Jimenezd2ccb522010-12-16 21:31:32 +0100143
Richard Purdiee842f1c2006-03-27 01:16:46 -0800144 return 0;
145
Richard Purdiee842f1c2006-03-27 01:16:46 -0800146 fail_ai:
Russell Kinga0164a52012-01-19 11:55:21 +0000147 free_irq(IRQ_RTC1Hz, dev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800148 fail_ui:
149 return ret;
150}
151
152static void sa1100_rtc_release(struct device *dev)
153{
Russell Kinga0164a52012-01-19 11:55:21 +0000154 spin_lock_irq(&sa1100_rtc_lock);
155 RTSR = 0;
156 spin_unlock_irq(&sa1100_rtc_lock);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800157
Russell Kinga0164a52012-01-19 11:55:21 +0000158 free_irq(IRQ_RTCAlrm, dev);
159 free_irq(IRQ_RTC1Hz, dev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800160}
161
John Stultz16380c12011-02-02 17:02:41 -0800162static int sa1100_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
163{
Russell Kinga0164a52012-01-19 11:55:21 +0000164 spin_lock_irq(&sa1100_rtc_lock);
John Stultz16380c12011-02-02 17:02:41 -0800165 if (enabled)
Russell Kinga0164a52012-01-19 11:55:21 +0000166 RTSR |= RTSR_ALE;
John Stultz16380c12011-02-02 17:02:41 -0800167 else
Russell Kinga0164a52012-01-19 11:55:21 +0000168 RTSR &= ~RTSR_ALE;
169 spin_unlock_irq(&sa1100_rtc_lock);
John Stultz16380c12011-02-02 17:02:41 -0800170 return 0;
171}
172
Richard Purdiee842f1c2006-03-27 01:16:46 -0800173static int sa1100_rtc_read_time(struct device *dev, struct rtc_time *tm)
174{
Russell Kinga0164a52012-01-19 11:55:21 +0000175 rtc_time_to_tm(RCNR, tm);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800176 return 0;
177}
178
179static int sa1100_rtc_set_time(struct device *dev, struct rtc_time *tm)
180{
181 unsigned long time;
182 int ret;
183
184 ret = rtc_tm_to_time(tm, &time);
185 if (ret == 0)
Russell Kinga0164a52012-01-19 11:55:21 +0000186 RCNR = time;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800187 return ret;
188}
189
190static int sa1100_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
191{
Russell Kinga0164a52012-01-19 11:55:21 +0000192 u32 rtsr;
David Brownell32b49da2007-02-20 13:58:13 -0800193
Russell Kinga0164a52012-01-19 11:55:21 +0000194 memcpy(&alrm->time, &rtc_alarm, sizeof(struct rtc_time));
195 rtsr = RTSR;
David Brownell32b49da2007-02-20 13:58:13 -0800196 alrm->enabled = (rtsr & RTSR_ALE) ? 1 : 0;
197 alrm->pending = (rtsr & RTSR_AL) ? 1 : 0;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800198 return 0;
199}
200
201static int sa1100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
202{
Jett.Zhou42874752011-11-30 12:26:22 +0800203 struct rtc_time now_tm, alarm_tm;
Russell Kinga0164a52012-01-19 11:55:21 +0000204 int ret;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800205
Russell Kinga0164a52012-01-19 11:55:21 +0000206 spin_lock_irq(&sa1100_rtc_lock);
Jett.Zhou42874752011-11-30 12:26:22 +0800207
Russell Kinga0164a52012-01-19 11:55:21 +0000208 now = RCNR;
209 rtc_time_to_tm(now, &now_tm);
210 rtc_next_alarm_time(&alarm_tm, &now_tm, alrm->time);
211 rtc_tm_to_time(&alarm_tm, &time);
212 RTAR = time;
Jett.Zhou42874752011-11-30 12:26:22 +0800213 if (alrm->enabled)
Russell Kinga0164a52012-01-19 11:55:21 +0000214 RTSR |= RTSR_ALE;
Jett.Zhou42874752011-11-30 12:26:22 +0800215 else
Russell Kinga0164a52012-01-19 11:55:21 +0000216 RTSR &= ~RTSR_ALE;
Jett.Zhou42874752011-11-30 12:26:22 +0800217
Russell Kinga0164a52012-01-19 11:55:21 +0000218 spin_unlock_irq(&sa1100_rtc_lock);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800219
Russell Kinga0164a52012-01-19 11:55:21 +0000220 return ret;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800221}
222
223static int sa1100_rtc_proc(struct device *dev, struct seq_file *seq)
224{
Russell Kinga0164a52012-01-19 11:55:21 +0000225 seq_printf(seq, "trim/divider\t\t: 0x%08x\n", (u32) RTTR);
226 seq_printf(seq, "RTSR\t\t\t: 0x%08x\n", (u32)RTSR);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800227
228 return 0;
229}
230
David Brownellff8371a2006-09-30 23:28:17 -0700231static const struct rtc_class_ops sa1100_rtc_ops = {
Richard Purdiee842f1c2006-03-27 01:16:46 -0800232 .open = sa1100_rtc_open,
Richard Purdiee842f1c2006-03-27 01:16:46 -0800233 .release = sa1100_rtc_release,
Richard Purdiee842f1c2006-03-27 01:16:46 -0800234 .read_time = sa1100_rtc_read_time,
235 .set_time = sa1100_rtc_set_time,
236 .read_alarm = sa1100_rtc_read_alarm,
237 .set_alarm = sa1100_rtc_set_alarm,
238 .proc = sa1100_rtc_proc,
John Stultz16380c12011-02-02 17:02:41 -0800239 .alarm_irq_enable = sa1100_rtc_alarm_irq_enable,
Richard Purdiee842f1c2006-03-27 01:16:46 -0800240};
241
242static int sa1100_rtc_probe(struct platform_device *pdev)
243{
Russell Kinga0164a52012-01-19 11:55:21 +0000244 struct rtc_device *rtc;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800245
246 /*
247 * According to the manual we should be able to let RTTR be zero
248 * and then a default diviser for a 32.768KHz clock is used.
249 * Apparently this doesn't work, at least for my SA1110 rev 5.
250 * If the clock divider is uninitialized then reset it to the
251 * default value to get the 1Hz clock.
252 */
Russell Kinga0164a52012-01-19 11:55:21 +0000253 if (RTTR == 0) {
254 RTTR = RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16);
255 dev_warn(&pdev->dev, "warning: "
256 "initializing default clock divider/trim value\n");
Richard Purdiee842f1c2006-03-27 01:16:46 -0800257 /* The current RTC value probably doesn't make sense either */
Russell Kinga0164a52012-01-19 11:55:21 +0000258 RCNR = 0;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800259 }
260
Uli Luckase5a2c9c2008-06-18 09:54:03 +0100261 device_init_wakeup(&pdev->dev, 1);
262
Russell Kinga0164a52012-01-19 11:55:21 +0000263 rtc = rtc_device_register(pdev->name, &pdev->dev, &sa1100_rtc_ops,
264 THIS_MODULE);
265
266 if (IS_ERR(rtc))
267 return PTR_ERR(rtc);
268
269 platform_set_drvdata(pdev, rtc);
270
Marcelo Roberto Jimenez7decaa52010-10-18 22:35:54 +0100271 /* Fix for a nasty initialization problem the in SA11xx RTSR register.
272 * See also the comments in sa1100_rtc_interrupt().
273 *
274 * Sometimes bit 1 of the RTSR (RTSR_HZ) will wake up 1, which means an
275 * interrupt pending, even though interrupts were never enabled.
276 * In this case, this bit it must be reset before enabling
277 * interruptions to avoid a nonexistent interrupt to occur.
278 *
279 * In principle, the same problem would apply to bit 0, although it has
280 * never been observed to happen.
281 *
282 * This issue is addressed both here and in sa1100_rtc_interrupt().
283 * If the issue is not addressed here, in the times when the processor
284 * wakes up with the bit set there will be one spurious interrupt.
285 *
286 * The issue is also dealt with in sa1100_rtc_interrupt() to be on the
287 * safe side, once the condition that lead to this strange
288 * initialization is unknown and could in principle happen during
289 * normal processing.
290 *
291 * Notice that clearing bit 1 and 0 is accomplished by writting ONES to
292 * the corresponding bits in RTSR. */
Russell Kinga0164a52012-01-19 11:55:21 +0000293 RTSR = RTSR_AL | RTSR_HZ;
Marcelo Roberto Jimenez7decaa52010-10-18 22:35:54 +0100294
Richard Purdiee842f1c2006-03-27 01:16:46 -0800295 return 0;
296}
297
298static int sa1100_rtc_remove(struct platform_device *pdev)
299{
Russell Kinga0164a52012-01-19 11:55:21 +0000300 struct rtc_device *rtc = platform_get_drvdata(pdev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800301
Russell Kinga0164a52012-01-19 11:55:21 +0000302 if (rtc)
303 rtc_device_unregister(rtc);
304
Richard Purdiee842f1c2006-03-27 01:16:46 -0800305 return 0;
306}
307
Russell King6bc54e62007-11-12 22:49:58 +0000308#ifdef CONFIG_PM
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800309static int sa1100_rtc_suspend(struct device *dev)
Russell King6bc54e62007-11-12 22:49:58 +0000310{
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800311 if (device_may_wakeup(dev))
Russell Kinga0164a52012-01-19 11:55:21 +0000312 enable_irq_wake(IRQ_RTCAlrm);
Russell King6bc54e62007-11-12 22:49:58 +0000313 return 0;
314}
315
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800316static int sa1100_rtc_resume(struct device *dev)
Russell King6bc54e62007-11-12 22:49:58 +0000317{
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800318 if (device_may_wakeup(dev))
Russell Kinga0164a52012-01-19 11:55:21 +0000319 disable_irq_wake(IRQ_RTCAlrm);
Russell King6bc54e62007-11-12 22:49:58 +0000320 return 0;
321}
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800322
Alexey Dobriyan47145212009-12-14 18:00:08 -0800323static const struct dev_pm_ops sa1100_rtc_pm_ops = {
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800324 .suspend = sa1100_rtc_suspend,
325 .resume = sa1100_rtc_resume,
326};
Russell King6bc54e62007-11-12 22:49:58 +0000327#endif
328
Richard Purdiee842f1c2006-03-27 01:16:46 -0800329static struct platform_driver sa1100_rtc_driver = {
330 .probe = sa1100_rtc_probe,
331 .remove = sa1100_rtc_remove,
Richard Purdiee842f1c2006-03-27 01:16:46 -0800332 .driver = {
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800333 .name = "sa1100-rtc",
334#ifdef CONFIG_PM
335 .pm = &sa1100_rtc_pm_ops,
336#endif
Richard Purdiee842f1c2006-03-27 01:16:46 -0800337 },
338};
339
Axel Lin0c4eae62012-01-10 15:10:48 -0800340module_platform_driver(sa1100_rtc_driver);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800341
342MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
343MODULE_DESCRIPTION("SA11x0/PXA2xx Realtime Clock Driver (RTC)");
344MODULE_LICENSE("GPL");
Kay Sieversad28a072008-04-10 21:29:25 -0700345MODULE_ALIAS("platform:sa1100-rtc");