2 * Real Time Clock interface for StrongARM SA1x00 and XScale PXA2xx
4 * Copyright (c) 2000 Nils Faerber
6 * Based on rtc.c by Paul Gortmaker
8 * Original Driver by Nils Faerber <nils@kernelconcepts.de>
11 * CIH <cih@coventive.com>
12 * Nicolas Pitre <nico@cam.org>
13 * Andrew Christian <andrew.christian@hp.com>
15 * Converted to the RTC subsystem and Driver Model
16 * by Richard Purdie <rpurdie@rpsys.net>
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.
24 #include <linux/platform_device.h>
25 #include <linux/module.h>
26 #include <linux/rtc.h>
27 #include <linux/init.h>
29 #include <linux/interrupt.h>
30 #include <linux/string.h>
32 #include <linux/bitops.h>
34 #include <mach/hardware.h>
37 #ifdef CONFIG_ARCH_PXA
38 #include <mach/pxa-regs.h>
41 #define RTC_DEF_DIVIDER 32768 - 1
42 #define RTC_DEF_TRIM 0
44 static unsigned long rtc_freq = 1024;
45 static unsigned long timer_freq;
46 static struct rtc_time rtc_alarm;
47 static DEFINE_SPINLOCK(sa1100_rtc_lock);
49 static inline int rtc_periodic_alarm(struct rtc_time *tm)
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);
60 * Calculate the next alarm time given the requested alarm time mask
61 * and the current time.
63 static void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now, struct rtc_time *alrm)
65 unsigned long next_time;
66 unsigned long now_time;
68 next->tm_year = now->tm_year;
69 next->tm_mon = now->tm_mon;
70 next->tm_mday = now->tm_mday;
71 next->tm_hour = alrm->tm_hour;
72 next->tm_min = alrm->tm_min;
73 next->tm_sec = alrm->tm_sec;
75 rtc_tm_to_time(now, &now_time);
76 rtc_tm_to_time(next, &next_time);
78 if (next_time < now_time) {
80 next_time += 60 * 60 * 24;
81 rtc_time_to_tm(next_time, next);
85 static int rtc_update_alarm(struct rtc_time *alrm)
87 struct rtc_time alarm_tm, now_tm;
88 unsigned long now, time;
93 rtc_time_to_tm(now, &now_tm);
94 rtc_next_alarm_time(&alarm_tm, &now_tm, alrm);
95 ret = rtc_tm_to_time(&alarm_tm, &time);
99 RTSR = RTSR & (RTSR_HZE|RTSR_ALE|RTSR_AL);
101 } while (now != RCNR);
106 static irqreturn_t sa1100_rtc_interrupt(int irq, void *dev_id)
108 struct platform_device *pdev = to_platform_device(dev_id);
109 struct rtc_device *rtc = platform_get_drvdata(pdev);
111 unsigned long events = 0;
113 spin_lock(&sa1100_rtc_lock);
116 /* clear interrupt sources */
118 RTSR = (RTSR_AL | RTSR_HZ) & (rtsr >> 2);
120 /* clear alarm interrupt if it has occurred */
123 RTSR = rtsr & (RTSR_ALE | RTSR_HZE);
125 /* update irq data & counter */
127 events |= RTC_AF | RTC_IRQF;
129 events |= RTC_UF | RTC_IRQF;
131 rtc_update_irq(rtc, 1, events);
133 if (rtsr & RTSR_AL && rtc_periodic_alarm(&rtc_alarm))
134 rtc_update_alarm(&rtc_alarm);
136 spin_unlock(&sa1100_rtc_lock);
141 static int rtc_timer1_count;
143 static irqreturn_t timer1_interrupt(int irq, void *dev_id)
145 struct platform_device *pdev = to_platform_device(dev_id);
146 struct rtc_device *rtc = platform_get_drvdata(pdev);
149 * If we match for the first time, rtc_timer1_count will be 1.
150 * Otherwise, we wrapped around (very unlikely but
151 * still possible) so compute the amount of missed periods.
152 * The match reg is updated only when the data is actually retrieved
153 * to avoid unnecessary interrupts.
155 OSSR = OSSR_M1; /* clear match on timer1 */
157 rtc_update_irq(rtc, rtc_timer1_count, RTC_PF | RTC_IRQF);
159 if (rtc_timer1_count == 1)
160 rtc_timer1_count = (rtc_freq * ((1 << 30) / (timer_freq >> 2)));
165 static int sa1100_rtc_read_callback(struct device *dev, int data)
168 /* interpolate missed periods and set match for the next */
169 unsigned long period = timer_freq / rtc_freq;
170 unsigned long oscr = OSCR;
171 unsigned long osmr1 = OSMR1;
172 unsigned long missed = (oscr - osmr1)/period;
174 OSSR = OSSR_M1; /* clear match on timer 1 */
175 OSMR1 = osmr1 + (missed + 1)*period;
176 /* Ensure we didn't miss another match in the mean time.
177 * Here we compare (match - OSCR) 8 instead of 0 --
178 * see comment in pxa_timer_interrupt() for explanation.
180 while( (signed long)((osmr1 = OSMR1) - OSCR) <= 8 ) {
182 OSSR = OSSR_M1; /* clear match on timer 1 */
183 OSMR1 = osmr1 + period;
189 static int sa1100_rtc_open(struct device *dev)
193 ret = request_irq(IRQ_RTC1Hz, sa1100_rtc_interrupt, IRQF_DISABLED,
196 dev_err(dev, "IRQ %d already in use.\n", IRQ_RTC1Hz);
199 ret = request_irq(IRQ_RTCAlrm, sa1100_rtc_interrupt, IRQF_DISABLED,
202 dev_err(dev, "IRQ %d already in use.\n", IRQ_RTCAlrm);
205 ret = request_irq(IRQ_OST1, timer1_interrupt, IRQF_DISABLED,
208 dev_err(dev, "IRQ %d already in use.\n", IRQ_OST1);
214 free_irq(IRQ_RTCAlrm, dev);
216 free_irq(IRQ_RTC1Hz, dev);
221 static void sa1100_rtc_release(struct device *dev)
223 spin_lock_irq(&sa1100_rtc_lock);
227 spin_unlock_irq(&sa1100_rtc_lock);
229 free_irq(IRQ_OST1, dev);
230 free_irq(IRQ_RTCAlrm, dev);
231 free_irq(IRQ_RTC1Hz, dev);
235 static int sa1100_rtc_ioctl(struct device *dev, unsigned int cmd,
240 spin_lock_irq(&sa1100_rtc_lock);
242 spin_unlock_irq(&sa1100_rtc_lock);
245 spin_lock_irq(&sa1100_rtc_lock);
247 spin_unlock_irq(&sa1100_rtc_lock);
250 spin_lock_irq(&sa1100_rtc_lock);
252 spin_unlock_irq(&sa1100_rtc_lock);
255 spin_lock_irq(&sa1100_rtc_lock);
257 spin_unlock_irq(&sa1100_rtc_lock);
260 spin_lock_irq(&sa1100_rtc_lock);
262 spin_unlock_irq(&sa1100_rtc_lock);
265 spin_lock_irq(&sa1100_rtc_lock);
266 OSMR1 = timer_freq / rtc_freq + OSCR;
268 rtc_timer1_count = 1;
269 spin_unlock_irq(&sa1100_rtc_lock);
272 return put_user(rtc_freq, (unsigned long *)arg);
274 if (arg < 1 || arg > timer_freq)
282 static int sa1100_rtc_read_time(struct device *dev, struct rtc_time *tm)
284 rtc_time_to_tm(RCNR, tm);
288 static int sa1100_rtc_set_time(struct device *dev, struct rtc_time *tm)
293 ret = rtc_tm_to_time(tm, &time);
299 static int sa1100_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
303 memcpy(&alrm->time, &rtc_alarm, sizeof(struct rtc_time));
305 alrm->enabled = (rtsr & RTSR_ALE) ? 1 : 0;
306 alrm->pending = (rtsr & RTSR_AL) ? 1 : 0;
310 static int sa1100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
314 spin_lock_irq(&sa1100_rtc_lock);
315 ret = rtc_update_alarm(&alrm->time);
322 spin_unlock_irq(&sa1100_rtc_lock);
327 static int sa1100_rtc_proc(struct device *dev, struct seq_file *seq)
329 seq_printf(seq, "trim/divider\t: 0x%08x\n", (u32) RTTR);
330 seq_printf(seq, "update_IRQ\t: %s\n",
331 (RTSR & RTSR_HZE) ? "yes" : "no");
332 seq_printf(seq, "periodic_IRQ\t: %s\n",
333 (OIER & OIER_E1) ? "yes" : "no");
334 seq_printf(seq, "periodic_freq\t: %ld\n", rtc_freq);
339 static const struct rtc_class_ops sa1100_rtc_ops = {
340 .open = sa1100_rtc_open,
341 .read_callback = sa1100_rtc_read_callback,
342 .release = sa1100_rtc_release,
343 .ioctl = sa1100_rtc_ioctl,
344 .read_time = sa1100_rtc_read_time,
345 .set_time = sa1100_rtc_set_time,
346 .read_alarm = sa1100_rtc_read_alarm,
347 .set_alarm = sa1100_rtc_set_alarm,
348 .proc = sa1100_rtc_proc,
351 static int sa1100_rtc_probe(struct platform_device *pdev)
353 struct rtc_device *rtc;
355 timer_freq = get_clock_tick_rate();
358 * According to the manual we should be able to let RTTR be zero
359 * and then a default diviser for a 32.768KHz clock is used.
360 * Apparently this doesn't work, at least for my SA1110 rev 5.
361 * If the clock divider is uninitialized then reset it to the
362 * default value to get the 1Hz clock.
365 RTTR = RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16);
366 dev_warn(&pdev->dev, "warning: initializing default clock divider/trim value\n");
367 /* The current RTC value probably doesn't make sense either */
371 device_init_wakeup(&pdev->dev, 1);
373 rtc = rtc_device_register(pdev->name, &pdev->dev, &sa1100_rtc_ops,
379 platform_set_drvdata(pdev, rtc);
384 static int sa1100_rtc_remove(struct platform_device *pdev)
386 struct rtc_device *rtc = platform_get_drvdata(pdev);
389 rtc_device_unregister(rtc);
395 static int sa1100_rtc_suspend(struct platform_device *pdev, pm_message_t state)
397 if (device_may_wakeup(&pdev->dev))
398 enable_irq_wake(IRQ_RTCAlrm);
402 static int sa1100_rtc_resume(struct platform_device *pdev)
404 if (device_may_wakeup(&pdev->dev))
405 disable_irq_wake(IRQ_RTCAlrm);
409 #define sa1100_rtc_suspend NULL
410 #define sa1100_rtc_resume NULL
413 static struct platform_driver sa1100_rtc_driver = {
414 .probe = sa1100_rtc_probe,
415 .remove = sa1100_rtc_remove,
416 .suspend = sa1100_rtc_suspend,
417 .resume = sa1100_rtc_resume,
419 .name = "sa1100-rtc",
423 static int __init sa1100_rtc_init(void)
425 return platform_driver_register(&sa1100_rtc_driver);
428 static void __exit sa1100_rtc_exit(void)
430 platform_driver_unregister(&sa1100_rtc_driver);
433 module_init(sa1100_rtc_init);
434 module_exit(sa1100_rtc_exit);
436 MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
437 MODULE_DESCRIPTION("SA11x0/PXA2xx Realtime Clock Driver (RTC)");
438 MODULE_LICENSE("GPL");
439 MODULE_ALIAS("platform:sa1100-rtc");