blob: 398ab7a7ceca6c40e1a0f23b92604969d85b6b20 [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>
Rob Herring23019a72012-03-20 14:33:19 -050033#include <linux/io.h>
Richard Purdiee842f1c2006-03-27 01:16:46 -080034
Russell Kinga09e64f2008-08-05 16:14:15 +010035#include <mach/hardware.h>
Richard Purdiee842f1c2006-03-27 01:16:46 -080036#include <asm/irq.h>
Richard Purdiee842f1c2006-03-27 01:16:46 -080037
Russell Kinga0164a52012-01-19 11:55:21 +000038#ifdef CONFIG_ARCH_PXA
39#include <mach/regs-rtc.h>
40#endif
41
Marcelo Roberto Jimeneza404ad12010-10-18 22:33:53 +010042#define RTC_DEF_DIVIDER (32768 - 1)
Richard Purdiee842f1c2006-03-27 01:16:46 -080043#define RTC_DEF_TRIM 0
44
Russell Kinga0164a52012-01-19 11:55:21 +000045static const unsigned long RTC_FREQ = 1024;
46static struct rtc_time rtc_alarm;
47static DEFINE_SPINLOCK(sa1100_rtc_lock);
Richard Purdiee842f1c2006-03-27 01:16:46 -080048
Russell King57270fc2012-01-19 11:50:40 +000049static inline int rtc_periodic_alarm(struct rtc_time *tm)
50{
51 return (tm->tm_year == -1) ||
52 ((unsigned)tm->tm_mon >= 12) ||
53 ((unsigned)(tm->tm_mday - 1) >= 31) ||
54 ((unsigned)tm->tm_hour > 23) ||
55 ((unsigned)tm->tm_min > 59) ||
56 ((unsigned)tm->tm_sec > 59);
57}
58
Russell King797276e2008-04-20 12:30:41 +010059/*
60 * Calculate the next alarm time given the requested alarm time mask
61 * and the current time.
62 */
Marcelo Roberto Jimeneza404ad12010-10-18 22:33:53 +010063static void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now,
64 struct rtc_time *alrm)
Russell King797276e2008-04-20 12:30:41 +010065{
66 unsigned long next_time;
67 unsigned long now_time;
68
69 next->tm_year = now->tm_year;
70 next->tm_mon = now->tm_mon;
71 next->tm_mday = now->tm_mday;
72 next->tm_hour = alrm->tm_hour;
73 next->tm_min = alrm->tm_min;
74 next->tm_sec = alrm->tm_sec;
75
76 rtc_tm_to_time(now, &now_time);
77 rtc_tm_to_time(next, &next_time);
78
79 if (next_time < now_time) {
80 /* Advance one day */
81 next_time += 60 * 60 * 24;
82 rtc_time_to_tm(next_time, next);
83 }
84}
85
Russell King57270fc2012-01-19 11:50:40 +000086static int rtc_update_alarm(struct rtc_time *alrm)
87{
88 struct rtc_time alarm_tm, now_tm;
89 unsigned long now, time;
90 int ret;
91
92 do {
93 now = RCNR;
94 rtc_time_to_tm(now, &now_tm);
95 rtc_next_alarm_time(&alarm_tm, &now_tm, alrm);
96 ret = rtc_tm_to_time(&alarm_tm, &time);
97 if (ret != 0)
98 break;
99
100 RTSR = RTSR & (RTSR_HZE|RTSR_ALE|RTSR_AL);
101 RTAR = time;
102 } while (now != RCNR);
103
104 return ret;
105}
106
David Howells7d12e782006-10-05 14:55:46 +0100107static irqreturn_t sa1100_rtc_interrupt(int irq, void *dev_id)
Richard Purdiee842f1c2006-03-27 01:16:46 -0800108{
109 struct platform_device *pdev = to_platform_device(dev_id);
Russell Kinga0164a52012-01-19 11:55:21 +0000110 struct rtc_device *rtc = platform_get_drvdata(pdev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800111 unsigned int rtsr;
112 unsigned long events = 0;
113
Russell Kinga0164a52012-01-19 11:55:21 +0000114 spin_lock(&sa1100_rtc_lock);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800115
Russell Kinga0164a52012-01-19 11:55:21 +0000116 rtsr = RTSR;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800117 /* clear interrupt sources */
Russell Kinga0164a52012-01-19 11:55:21 +0000118 RTSR = 0;
Marcelo Roberto Jimenez7decaa52010-10-18 22:35:54 +0100119 /* Fix for a nasty initialization problem the in SA11xx RTSR register.
120 * See also the comments in sa1100_rtc_probe(). */
121 if (rtsr & (RTSR_ALE | RTSR_HZE)) {
122 /* This is the original code, before there was the if test
123 * above. This code does not clear interrupts that were not
124 * enabled. */
Russell Kinga0164a52012-01-19 11:55:21 +0000125 RTSR = (RTSR_AL | RTSR_HZ) & (rtsr >> 2);
Marcelo Roberto Jimenez7decaa52010-10-18 22:35:54 +0100126 } else {
127 /* For some reason, it is possible to enter this routine
128 * without interruptions enabled, it has been tested with
129 * several units (Bug in SA11xx chip?).
130 *
131 * This situation leads to an infinite "loop" of interrupt
132 * routine calling and as a result the processor seems to
133 * lock on its first call to open(). */
Russell Kinga0164a52012-01-19 11:55:21 +0000134 RTSR = RTSR_AL | RTSR_HZ;
Marcelo Roberto Jimenez7decaa52010-10-18 22:35:54 +0100135 }
Richard Purdiee842f1c2006-03-27 01:16:46 -0800136
137 /* clear alarm interrupt if it has occurred */
138 if (rtsr & RTSR_AL)
139 rtsr &= ~RTSR_ALE;
Russell Kinga0164a52012-01-19 11:55:21 +0000140 RTSR = rtsr & (RTSR_ALE | RTSR_HZE);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800141
142 /* update irq data & counter */
143 if (rtsr & RTSR_AL)
144 events |= RTC_AF | RTC_IRQF;
145 if (rtsr & RTSR_HZ)
146 events |= RTC_UF | RTC_IRQF;
147
Russell Kinga0164a52012-01-19 11:55:21 +0000148 rtc_update_irq(rtc, 1, events);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800149
Russell King57270fc2012-01-19 11:50:40 +0000150 if (rtsr & RTSR_AL && rtc_periodic_alarm(&rtc_alarm))
151 rtc_update_alarm(&rtc_alarm);
152
Russell Kinga0164a52012-01-19 11:55:21 +0000153 spin_unlock(&sa1100_rtc_lock);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800154
155 return IRQ_HANDLED;
156}
157
Richard Purdiee842f1c2006-03-27 01:16:46 -0800158static int sa1100_rtc_open(struct device *dev)
159{
160 int ret;
Russell Kinga0164a52012-01-19 11:55:21 +0000161 struct platform_device *plat_dev = to_platform_device(dev);
162 struct rtc_device *rtc = platform_get_drvdata(plat_dev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800163
Russell Kinga0164a52012-01-19 11:55:21 +0000164 ret = request_irq(IRQ_RTC1Hz, sa1100_rtc_interrupt, IRQF_DISABLED,
165 "rtc 1Hz", dev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800166 if (ret) {
Russell Kinga0164a52012-01-19 11:55:21 +0000167 dev_err(dev, "IRQ %d already in use.\n", IRQ_RTC1Hz);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800168 goto fail_ui;
169 }
Russell Kinga0164a52012-01-19 11:55:21 +0000170 ret = request_irq(IRQ_RTCAlrm, sa1100_rtc_interrupt, IRQF_DISABLED,
171 "rtc Alrm", dev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800172 if (ret) {
Russell Kinga0164a52012-01-19 11:55:21 +0000173 dev_err(dev, "IRQ %d already in use.\n", IRQ_RTCAlrm);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800174 goto fail_ai;
175 }
Russell Kinga0164a52012-01-19 11:55:21 +0000176 rtc->max_user_freq = RTC_FREQ;
177 rtc_irq_set_freq(rtc, NULL, RTC_FREQ);
Marcelo Roberto Jimenezd2ccb522010-12-16 21:31:32 +0100178
Richard Purdiee842f1c2006-03-27 01:16:46 -0800179 return 0;
180
Richard Purdiee842f1c2006-03-27 01:16:46 -0800181 fail_ai:
Russell Kinga0164a52012-01-19 11:55:21 +0000182 free_irq(IRQ_RTC1Hz, dev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800183 fail_ui:
184 return ret;
185}
186
187static void sa1100_rtc_release(struct device *dev)
188{
Russell Kinga0164a52012-01-19 11:55:21 +0000189 spin_lock_irq(&sa1100_rtc_lock);
190 RTSR = 0;
191 spin_unlock_irq(&sa1100_rtc_lock);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800192
Russell Kinga0164a52012-01-19 11:55:21 +0000193 free_irq(IRQ_RTCAlrm, dev);
194 free_irq(IRQ_RTC1Hz, dev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800195}
196
John Stultz16380c12011-02-02 17:02:41 -0800197static int sa1100_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
198{
Russell Kinga0164a52012-01-19 11:55:21 +0000199 spin_lock_irq(&sa1100_rtc_lock);
John Stultz16380c12011-02-02 17:02:41 -0800200 if (enabled)
Russell Kinga0164a52012-01-19 11:55:21 +0000201 RTSR |= RTSR_ALE;
John Stultz16380c12011-02-02 17:02:41 -0800202 else
Russell Kinga0164a52012-01-19 11:55:21 +0000203 RTSR &= ~RTSR_ALE;
204 spin_unlock_irq(&sa1100_rtc_lock);
John Stultz16380c12011-02-02 17:02:41 -0800205 return 0;
206}
207
Richard Purdiee842f1c2006-03-27 01:16:46 -0800208static int sa1100_rtc_read_time(struct device *dev, struct rtc_time *tm)
209{
Russell Kinga0164a52012-01-19 11:55:21 +0000210 rtc_time_to_tm(RCNR, tm);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800211 return 0;
212}
213
214static int sa1100_rtc_set_time(struct device *dev, struct rtc_time *tm)
215{
216 unsigned long time;
217 int ret;
218
219 ret = rtc_tm_to_time(tm, &time);
220 if (ret == 0)
Russell Kinga0164a52012-01-19 11:55:21 +0000221 RCNR = time;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800222 return ret;
223}
224
225static int sa1100_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
226{
Russell Kinga0164a52012-01-19 11:55:21 +0000227 u32 rtsr;
David Brownell32b49da2007-02-20 13:58:13 -0800228
Russell Kinga0164a52012-01-19 11:55:21 +0000229 memcpy(&alrm->time, &rtc_alarm, sizeof(struct rtc_time));
230 rtsr = RTSR;
David Brownell32b49da2007-02-20 13:58:13 -0800231 alrm->enabled = (rtsr & RTSR_ALE) ? 1 : 0;
232 alrm->pending = (rtsr & RTSR_AL) ? 1 : 0;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800233 return 0;
234}
235
236static int sa1100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
237{
Russell Kinga0164a52012-01-19 11:55:21 +0000238 int ret;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800239
Russell Kinga0164a52012-01-19 11:55:21 +0000240 spin_lock_irq(&sa1100_rtc_lock);
Russell King57270fc2012-01-19 11:50:40 +0000241 ret = rtc_update_alarm(&alrm->time);
242 if (ret == 0) {
243 if (alrm->enabled)
244 RTSR |= RTSR_ALE;
245 else
246 RTSR &= ~RTSR_ALE;
247 }
Russell Kinga0164a52012-01-19 11:55:21 +0000248 spin_unlock_irq(&sa1100_rtc_lock);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800249
Russell Kinga0164a52012-01-19 11:55:21 +0000250 return ret;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800251}
252
253static int sa1100_rtc_proc(struct device *dev, struct seq_file *seq)
254{
Russell Kinga0164a52012-01-19 11:55:21 +0000255 seq_printf(seq, "trim/divider\t\t: 0x%08x\n", (u32) RTTR);
256 seq_printf(seq, "RTSR\t\t\t: 0x%08x\n", (u32)RTSR);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800257
258 return 0;
259}
260
David Brownellff8371a2006-09-30 23:28:17 -0700261static const struct rtc_class_ops sa1100_rtc_ops = {
Richard Purdiee842f1c2006-03-27 01:16:46 -0800262 .open = sa1100_rtc_open,
Richard Purdiee842f1c2006-03-27 01:16:46 -0800263 .release = sa1100_rtc_release,
Richard Purdiee842f1c2006-03-27 01:16:46 -0800264 .read_time = sa1100_rtc_read_time,
265 .set_time = sa1100_rtc_set_time,
266 .read_alarm = sa1100_rtc_read_alarm,
267 .set_alarm = sa1100_rtc_set_alarm,
268 .proc = sa1100_rtc_proc,
John Stultz16380c12011-02-02 17:02:41 -0800269 .alarm_irq_enable = sa1100_rtc_alarm_irq_enable,
Richard Purdiee842f1c2006-03-27 01:16:46 -0800270};
271
272static int sa1100_rtc_probe(struct platform_device *pdev)
273{
Russell Kinga0164a52012-01-19 11:55:21 +0000274 struct rtc_device *rtc;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800275
276 /*
277 * According to the manual we should be able to let RTTR be zero
278 * and then a default diviser for a 32.768KHz clock is used.
279 * Apparently this doesn't work, at least for my SA1110 rev 5.
280 * If the clock divider is uninitialized then reset it to the
281 * default value to get the 1Hz clock.
282 */
Russell Kinga0164a52012-01-19 11:55:21 +0000283 if (RTTR == 0) {
284 RTTR = RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16);
285 dev_warn(&pdev->dev, "warning: "
286 "initializing default clock divider/trim value\n");
Richard Purdiee842f1c2006-03-27 01:16:46 -0800287 /* The current RTC value probably doesn't make sense either */
Russell Kinga0164a52012-01-19 11:55:21 +0000288 RCNR = 0;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800289 }
290
Uli Luckase5a2c9c2008-06-18 09:54:03 +0100291 device_init_wakeup(&pdev->dev, 1);
292
Russell Kinga0164a52012-01-19 11:55:21 +0000293 rtc = rtc_device_register(pdev->name, &pdev->dev, &sa1100_rtc_ops,
294 THIS_MODULE);
295
296 if (IS_ERR(rtc))
297 return PTR_ERR(rtc);
298
299 platform_set_drvdata(pdev, rtc);
300
Marcelo Roberto Jimenez7decaa52010-10-18 22:35:54 +0100301 /* Fix for a nasty initialization problem the in SA11xx RTSR register.
302 * See also the comments in sa1100_rtc_interrupt().
303 *
304 * Sometimes bit 1 of the RTSR (RTSR_HZ) will wake up 1, which means an
305 * interrupt pending, even though interrupts were never enabled.
306 * In this case, this bit it must be reset before enabling
307 * interruptions to avoid a nonexistent interrupt to occur.
308 *
309 * In principle, the same problem would apply to bit 0, although it has
310 * never been observed to happen.
311 *
312 * This issue is addressed both here and in sa1100_rtc_interrupt().
313 * If the issue is not addressed here, in the times when the processor
314 * wakes up with the bit set there will be one spurious interrupt.
315 *
316 * The issue is also dealt with in sa1100_rtc_interrupt() to be on the
317 * safe side, once the condition that lead to this strange
318 * initialization is unknown and could in principle happen during
319 * normal processing.
320 *
321 * Notice that clearing bit 1 and 0 is accomplished by writting ONES to
322 * the corresponding bits in RTSR. */
Russell Kinga0164a52012-01-19 11:55:21 +0000323 RTSR = RTSR_AL | RTSR_HZ;
Marcelo Roberto Jimenez7decaa52010-10-18 22:35:54 +0100324
Richard Purdiee842f1c2006-03-27 01:16:46 -0800325 return 0;
326}
327
328static int sa1100_rtc_remove(struct platform_device *pdev)
329{
Russell Kinga0164a52012-01-19 11:55:21 +0000330 struct rtc_device *rtc = platform_get_drvdata(pdev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800331
Russell Kinga0164a52012-01-19 11:55:21 +0000332 if (rtc)
333 rtc_device_unregister(rtc);
334
Richard Purdiee842f1c2006-03-27 01:16:46 -0800335 return 0;
336}
337
Russell King6bc54e62007-11-12 22:49:58 +0000338#ifdef CONFIG_PM
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800339static int sa1100_rtc_suspend(struct device *dev)
Russell King6bc54e62007-11-12 22:49:58 +0000340{
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800341 if (device_may_wakeup(dev))
Russell Kinga0164a52012-01-19 11:55:21 +0000342 enable_irq_wake(IRQ_RTCAlrm);
Russell King6bc54e62007-11-12 22:49:58 +0000343 return 0;
344}
345
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800346static int sa1100_rtc_resume(struct device *dev)
Russell King6bc54e62007-11-12 22:49:58 +0000347{
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800348 if (device_may_wakeup(dev))
Russell Kinga0164a52012-01-19 11:55:21 +0000349 disable_irq_wake(IRQ_RTCAlrm);
Russell King6bc54e62007-11-12 22:49:58 +0000350 return 0;
351}
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800352
Alexey Dobriyan47145212009-12-14 18:00:08 -0800353static const struct dev_pm_ops sa1100_rtc_pm_ops = {
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800354 .suspend = sa1100_rtc_suspend,
355 .resume = sa1100_rtc_resume,
356};
Russell King6bc54e62007-11-12 22:49:58 +0000357#endif
358
Richard Purdiee842f1c2006-03-27 01:16:46 -0800359static struct platform_driver sa1100_rtc_driver = {
360 .probe = sa1100_rtc_probe,
361 .remove = sa1100_rtc_remove,
Richard Purdiee842f1c2006-03-27 01:16:46 -0800362 .driver = {
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800363 .name = "sa1100-rtc",
364#ifdef CONFIG_PM
365 .pm = &sa1100_rtc_pm_ops,
366#endif
Richard Purdiee842f1c2006-03-27 01:16:46 -0800367 },
368};
369
Axel Lin0c4eae62012-01-10 15:10:48 -0800370module_platform_driver(sa1100_rtc_driver);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800371
372MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
373MODULE_DESCRIPTION("SA11x0/PXA2xx Realtime Clock Driver (RTC)");
374MODULE_LICENSE("GPL");
Kay Sieversad28a072008-04-10 21:29:25 -0700375MODULE_ALIAS("platform:sa1100-rtc");