tracing/core: remove unused parameter in tracing_fill_pipe_page()
[linux/fpc-iii.git] / drivers / rtc / rtc-sa1100.c
blobd26a5f82aabaf8eb58c61eb39c678d0f890ed5d1
1 /*
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>
10 * Modifications from:
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>
28 #include <linux/fs.h>
29 #include <linux/interrupt.h>
30 #include <linux/string.h>
31 #include <linux/pm.h>
32 #include <linux/bitops.h>
34 #include <mach/hardware.h>
35 #include <asm/irq.h>
37 #ifdef CONFIG_ARCH_PXA
38 #include <mach/pxa-regs.h>
39 #endif
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) {
79 /* Advance one day */
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;
89 int ret;
91 do {
92 now = RCNR;
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);
96 if (ret != 0)
97 break;
99 RTSR = RTSR & (RTSR_HZE|RTSR_ALE|RTSR_AL);
100 RTAR = time;
101 } while (now != RCNR);
103 return ret;
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);
110 unsigned int rtsr;
111 unsigned long events = 0;
113 spin_lock(&sa1100_rtc_lock);
115 rtsr = RTSR;
116 /* clear interrupt sources */
117 RTSR = 0;
118 RTSR = (RTSR_AL | RTSR_HZ) & (rtsr >> 2);
120 /* clear alarm interrupt if it has occurred */
121 if (rtsr & RTSR_AL)
122 rtsr &= ~RTSR_ALE;
123 RTSR = rtsr & (RTSR_ALE | RTSR_HZE);
125 /* update irq data & counter */
126 if (rtsr & RTSR_AL)
127 events |= RTC_AF | RTC_IRQF;
128 if (rtsr & RTSR_HZ)
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);
138 return IRQ_HANDLED;
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)));
162 return IRQ_HANDLED;
165 static int sa1100_rtc_read_callback(struct device *dev, int data)
167 if (data & RTC_PF) {
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;
173 data += missed << 8;
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 ) {
181 data += 0x100;
182 OSSR = OSSR_M1; /* clear match on timer 1 */
183 OSMR1 = osmr1 + period;
186 return data;
189 static int sa1100_rtc_open(struct device *dev)
191 int ret;
193 ret = request_irq(IRQ_RTC1Hz, sa1100_rtc_interrupt, IRQF_DISABLED,
194 "rtc 1Hz", dev);
195 if (ret) {
196 dev_err(dev, "IRQ %d already in use.\n", IRQ_RTC1Hz);
197 goto fail_ui;
199 ret = request_irq(IRQ_RTCAlrm, sa1100_rtc_interrupt, IRQF_DISABLED,
200 "rtc Alrm", dev);
201 if (ret) {
202 dev_err(dev, "IRQ %d already in use.\n", IRQ_RTCAlrm);
203 goto fail_ai;
205 ret = request_irq(IRQ_OST1, timer1_interrupt, IRQF_DISABLED,
206 "rtc timer", dev);
207 if (ret) {
208 dev_err(dev, "IRQ %d already in use.\n", IRQ_OST1);
209 goto fail_pi;
211 return 0;
213 fail_pi:
214 free_irq(IRQ_RTCAlrm, dev);
215 fail_ai:
216 free_irq(IRQ_RTC1Hz, dev);
217 fail_ui:
218 return ret;
221 static void sa1100_rtc_release(struct device *dev)
223 spin_lock_irq(&sa1100_rtc_lock);
224 RTSR = 0;
225 OIER &= ~OIER_E1;
226 OSSR = OSSR_M1;
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,
236 unsigned long arg)
238 switch(cmd) {
239 case RTC_AIE_OFF:
240 spin_lock_irq(&sa1100_rtc_lock);
241 RTSR &= ~RTSR_ALE;
242 spin_unlock_irq(&sa1100_rtc_lock);
243 return 0;
244 case RTC_AIE_ON:
245 spin_lock_irq(&sa1100_rtc_lock);
246 RTSR |= RTSR_ALE;
247 spin_unlock_irq(&sa1100_rtc_lock);
248 return 0;
249 case RTC_UIE_OFF:
250 spin_lock_irq(&sa1100_rtc_lock);
251 RTSR &= ~RTSR_HZE;
252 spin_unlock_irq(&sa1100_rtc_lock);
253 return 0;
254 case RTC_UIE_ON:
255 spin_lock_irq(&sa1100_rtc_lock);
256 RTSR |= RTSR_HZE;
257 spin_unlock_irq(&sa1100_rtc_lock);
258 return 0;
259 case RTC_PIE_OFF:
260 spin_lock_irq(&sa1100_rtc_lock);
261 OIER &= ~OIER_E1;
262 spin_unlock_irq(&sa1100_rtc_lock);
263 return 0;
264 case RTC_PIE_ON:
265 spin_lock_irq(&sa1100_rtc_lock);
266 OSMR1 = timer_freq / rtc_freq + OSCR;
267 OIER |= OIER_E1;
268 rtc_timer1_count = 1;
269 spin_unlock_irq(&sa1100_rtc_lock);
270 return 0;
271 case RTC_IRQP_READ:
272 return put_user(rtc_freq, (unsigned long *)arg);
273 case RTC_IRQP_SET:
274 if (arg < 1 || arg > timer_freq)
275 return -EINVAL;
276 rtc_freq = arg;
277 return 0;
279 return -ENOIOCTLCMD;
282 static int sa1100_rtc_read_time(struct device *dev, struct rtc_time *tm)
284 rtc_time_to_tm(RCNR, tm);
285 return 0;
288 static int sa1100_rtc_set_time(struct device *dev, struct rtc_time *tm)
290 unsigned long time;
291 int ret;
293 ret = rtc_tm_to_time(tm, &time);
294 if (ret == 0)
295 RCNR = time;
296 return ret;
299 static int sa1100_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
301 u32 rtsr;
303 memcpy(&alrm->time, &rtc_alarm, sizeof(struct rtc_time));
304 rtsr = RTSR;
305 alrm->enabled = (rtsr & RTSR_ALE) ? 1 : 0;
306 alrm->pending = (rtsr & RTSR_AL) ? 1 : 0;
307 return 0;
310 static int sa1100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
312 int ret;
314 spin_lock_irq(&sa1100_rtc_lock);
315 ret = rtc_update_alarm(&alrm->time);
316 if (ret == 0) {
317 if (alrm->enabled)
318 RTSR |= RTSR_ALE;
319 else
320 RTSR &= ~RTSR_ALE;
322 spin_unlock_irq(&sa1100_rtc_lock);
324 return ret;
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);
336 return 0;
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.
364 if (RTTR == 0) {
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 */
368 RCNR = 0;
371 device_init_wakeup(&pdev->dev, 1);
373 rtc = rtc_device_register(pdev->name, &pdev->dev, &sa1100_rtc_ops,
374 THIS_MODULE);
376 if (IS_ERR(rtc))
377 return PTR_ERR(rtc);
379 platform_set_drvdata(pdev, rtc);
381 return 0;
384 static int sa1100_rtc_remove(struct platform_device *pdev)
386 struct rtc_device *rtc = platform_get_drvdata(pdev);
388 if (rtc)
389 rtc_device_unregister(rtc);
391 return 0;
394 #ifdef CONFIG_PM
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);
399 return 0;
402 static int sa1100_rtc_resume(struct platform_device *pdev)
404 if (device_may_wakeup(&pdev->dev))
405 disable_irq_wake(IRQ_RTCAlrm);
406 return 0;
408 #else
409 #define sa1100_rtc_suspend NULL
410 #define sa1100_rtc_resume NULL
411 #endif
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,
418 .driver = {
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");