Automatic merge of rsync://rsync.kernel.org/pub/scm/linux/kernel/git/gregkh/driver...
[linux-2.6/verdex.git] / drivers / char / s3c2410-rtc.c
blobed867db550a9da16a690a9a122b28d33491ac6f8
1 /* drivers/char/s3c2410_rtc.c
3 * Copyright (c) 2004 Simtec Electronics <linux@simtec.co.uk>
4 * http://www.simtec.co.uk/products/SWLINUX/
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * S3C2410 Internal RTC Driver
12 * Changelog:
13 * 08-Nov-2004 BJD Initial creation
14 * 12-Nov-2004 BJD Added periodic IRQ and PM code
15 * 22-Nov-2004 BJD Sign-test on alarm code to check for <0
16 * 10-Mar-2005 LCVR Changed S3C2410_VA_RTC to S3C24XX_VA_RTC
19 #include <linux/module.h>
20 #include <linux/fs.h>
21 #include <linux/string.h>
22 #include <linux/init.h>
23 #include <linux/device.h>
24 #include <linux/interrupt.h>
25 #include <linux/rtc.h>
26 #include <linux/bcd.h>
28 #include <asm/hardware.h>
29 #include <asm/uaccess.h>
30 #include <asm/io.h>
31 #include <asm/irq.h>
32 #include <asm/rtc.h>
34 #include <asm/mach/time.h>
36 #include <asm/hardware/clock.h>
37 #include <asm/arch/regs-rtc.h>
39 /* need this for the RTC_AF definitions */
40 #include <linux/mc146818rtc.h>
42 #undef S3C24XX_VA_RTC
43 #define S3C24XX_VA_RTC s3c2410_rtc_base
45 static struct resource *s3c2410_rtc_mem;
47 static void __iomem *s3c2410_rtc_base;
48 static int s3c2410_rtc_alarmno = NO_IRQ;
49 static int s3c2410_rtc_tickno = NO_IRQ;
50 static int s3c2410_rtc_freq = 1;
52 static DEFINE_SPINLOCK(s3c2410_rtc_pie_lock);
54 /* IRQ Handlers */
56 static irqreturn_t s3c2410_rtc_alarmirq(int irq, void *id, struct pt_regs *r)
58 rtc_update(1, RTC_AF | RTC_IRQF);
59 return IRQ_HANDLED;
62 static irqreturn_t s3c2410_rtc_tickirq(int irq, void *id, struct pt_regs *r)
64 rtc_update(1, RTC_PF | RTC_IRQF);
65 return IRQ_HANDLED;
68 /* Update control registers */
69 static void s3c2410_rtc_setaie(int to)
71 unsigned int tmp;
73 pr_debug("%s: aie=%d\n", __FUNCTION__, to);
75 tmp = readb(S3C2410_RTCALM);
77 if (to)
78 tmp |= S3C2410_RTCALM_ALMEN;
79 else
80 tmp &= ~S3C2410_RTCALM_ALMEN;
83 writeb(tmp, S3C2410_RTCALM);
86 static void s3c2410_rtc_setpie(int to)
88 unsigned int tmp;
90 pr_debug("%s: pie=%d\n", __FUNCTION__, to);
92 spin_lock_irq(&s3c2410_rtc_pie_lock);
93 tmp = readb(S3C2410_TICNT) & ~S3C2410_TICNT_ENABLE;
95 if (to)
96 tmp |= S3C2410_TICNT_ENABLE;
98 writeb(tmp, S3C2410_TICNT);
99 spin_unlock_irq(&s3c2410_rtc_pie_lock);
102 static void s3c2410_rtc_setfreq(int freq)
104 unsigned int tmp;
106 spin_lock_irq(&s3c2410_rtc_pie_lock);
107 tmp = readb(S3C2410_TICNT) & S3C2410_TICNT_ENABLE;
109 s3c2410_rtc_freq = freq;
111 tmp |= (128 / freq)-1;
113 writeb(tmp, S3C2410_TICNT);
114 spin_unlock_irq(&s3c2410_rtc_pie_lock);
117 /* Time read/write */
119 static int s3c2410_rtc_gettime(struct rtc_time *rtc_tm)
121 unsigned int have_retried = 0;
123 retry_get_time:
124 rtc_tm->tm_min = readb(S3C2410_RTCMIN);
125 rtc_tm->tm_hour = readb(S3C2410_RTCHOUR);
126 rtc_tm->tm_mday = readb(S3C2410_RTCDATE);
127 rtc_tm->tm_mon = readb(S3C2410_RTCMON);
128 rtc_tm->tm_year = readb(S3C2410_RTCYEAR);
129 rtc_tm->tm_sec = readb(S3C2410_RTCSEC);
131 /* the only way to work out wether the system was mid-update
132 * when we read it is to check the second counter, and if it
133 * is zero, then we re-try the entire read
136 if (rtc_tm->tm_sec == 0 && !have_retried) {
137 have_retried = 1;
138 goto retry_get_time;
141 pr_debug("read time %02x.%02x.%02x %02x/%02x/%02x\n",
142 rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday,
143 rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);
145 BCD_TO_BIN(rtc_tm->tm_sec);
146 BCD_TO_BIN(rtc_tm->tm_min);
147 BCD_TO_BIN(rtc_tm->tm_hour);
148 BCD_TO_BIN(rtc_tm->tm_mday);
149 BCD_TO_BIN(rtc_tm->tm_mon);
150 BCD_TO_BIN(rtc_tm->tm_year);
152 rtc_tm->tm_year += 100;
153 rtc_tm->tm_mon -= 1;
155 return 0;
159 static int s3c2410_rtc_settime(struct rtc_time *tm)
161 /* the rtc gets round the y2k problem by just not supporting it */
163 if (tm->tm_year < 100)
164 return -EINVAL;
166 writeb(BIN2BCD(tm->tm_sec), S3C2410_RTCSEC);
167 writeb(BIN2BCD(tm->tm_min), S3C2410_RTCMIN);
168 writeb(BIN2BCD(tm->tm_hour), S3C2410_RTCHOUR);
169 writeb(BIN2BCD(tm->tm_mday), S3C2410_RTCDATE);
170 writeb(BIN2BCD(tm->tm_mon + 1), S3C2410_RTCMON);
171 writeb(BIN2BCD(tm->tm_year - 100), S3C2410_RTCYEAR);
173 return 0;
176 static int s3c2410_rtc_getalarm(struct rtc_wkalrm *alrm)
178 struct rtc_time *alm_tm = &alrm->time;
179 unsigned int alm_en;
181 alm_tm->tm_sec = readb(S3C2410_ALMSEC);
182 alm_tm->tm_min = readb(S3C2410_ALMMIN);
183 alm_tm->tm_hour = readb(S3C2410_ALMHOUR);
184 alm_tm->tm_mon = readb(S3C2410_ALMMON);
185 alm_tm->tm_mday = readb(S3C2410_ALMDATE);
186 alm_tm->tm_year = readb(S3C2410_ALMYEAR);
188 alm_en = readb(S3C2410_RTCALM);
190 pr_debug("read alarm %02x %02x.%02x.%02x %02x/%02x/%02x\n",
191 alm_en,
192 alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday,
193 alm_tm->tm_hour, alm_tm->tm_min, alm_tm->tm_sec);
196 /* decode the alarm enable field */
198 if (alm_en & S3C2410_RTCALM_SECEN) {
199 BCD_TO_BIN(alm_tm->tm_sec);
200 } else {
201 alm_tm->tm_sec = 0xff;
204 if (alm_en & S3C2410_RTCALM_MINEN) {
205 BCD_TO_BIN(alm_tm->tm_min);
206 } else {
207 alm_tm->tm_min = 0xff;
210 if (alm_en & S3C2410_RTCALM_HOUREN) {
211 BCD_TO_BIN(alm_tm->tm_hour);
212 } else {
213 alm_tm->tm_hour = 0xff;
216 if (alm_en & S3C2410_RTCALM_DAYEN) {
217 BCD_TO_BIN(alm_tm->tm_mday);
218 } else {
219 alm_tm->tm_mday = 0xff;
222 if (alm_en & S3C2410_RTCALM_MONEN) {
223 BCD_TO_BIN(alm_tm->tm_mon);
224 alm_tm->tm_mon -= 1;
225 } else {
226 alm_tm->tm_mon = 0xff;
229 if (alm_en & S3C2410_RTCALM_YEAREN) {
230 BCD_TO_BIN(alm_tm->tm_year);
231 } else {
232 alm_tm->tm_year = 0xffff;
235 /* todo - set alrm->enabled ? */
237 return 0;
240 static int s3c2410_rtc_setalarm(struct rtc_wkalrm *alrm)
242 struct rtc_time *tm = &alrm->time;
243 unsigned int alrm_en;
245 pr_debug("s3c2410_rtc_setalarm: %d, %02x/%02x/%02x %02x.%02x.%02x\n",
246 alrm->enabled,
247 tm->tm_mday & 0xff, tm->tm_mon & 0xff, tm->tm_year & 0xff,
248 tm->tm_hour & 0xff, tm->tm_min & 0xff, tm->tm_sec);
250 if (alrm->enabled || 1) {
251 alrm_en = readb(S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;
252 writeb(0x00, S3C2410_RTCALM);
254 if (tm->tm_sec < 60 && tm->tm_sec >= 0) {
255 alrm_en |= S3C2410_RTCALM_SECEN;
256 writeb(BIN2BCD(tm->tm_sec), S3C2410_ALMSEC);
259 if (tm->tm_min < 60 && tm->tm_min >= 0) {
260 alrm_en |= S3C2410_RTCALM_MINEN;
261 writeb(BIN2BCD(tm->tm_min), S3C2410_ALMMIN);
264 if (tm->tm_hour < 24 && tm->tm_hour >= 0) {
265 alrm_en |= S3C2410_RTCALM_HOUREN;
266 writeb(BIN2BCD(tm->tm_hour), S3C2410_ALMHOUR);
269 pr_debug("setting S3C2410_RTCALM to %08x\n", alrm_en);
271 writeb(alrm_en, S3C2410_RTCALM);
272 enable_irq_wake(s3c2410_rtc_alarmno);
273 } else {
274 alrm_en = readb(S3C2410_RTCALM);
275 alrm_en &= ~S3C2410_RTCALM_ALMEN;
276 writeb(alrm_en, S3C2410_RTCALM);
277 disable_irq_wake(s3c2410_rtc_alarmno);
280 return 0;
283 static int s3c2410_rtc_ioctl(unsigned int cmd, unsigned long arg)
285 switch (cmd) {
286 case RTC_AIE_OFF:
287 case RTC_AIE_ON:
288 s3c2410_rtc_setaie((cmd == RTC_AIE_ON) ? 1 : 0);
289 return 0;
291 case RTC_PIE_OFF:
292 case RTC_PIE_ON:
293 s3c2410_rtc_setpie((cmd == RTC_PIE_ON) ? 1 : 0);
294 return 0;
296 case RTC_IRQP_READ:
297 return put_user(s3c2410_rtc_freq, (unsigned long __user *)arg);
299 case RTC_IRQP_SET:
300 if (arg < 1 || arg > 64)
301 return -EINVAL;
303 if (!capable(CAP_SYS_RESOURCE))
304 return -EACCES;
306 /* check for power of 2 */
308 if ((arg & (arg-1)) != 0)
309 return -EINVAL;
311 pr_debug("s3c2410_rtc: setting frequency %ld\n", arg);
313 s3c2410_rtc_setfreq(arg);
314 return 0;
316 case RTC_UIE_ON:
317 case RTC_UIE_OFF:
318 return -EINVAL;
321 return -EINVAL;
324 static int s3c2410_rtc_proc(char *buf)
326 unsigned int rtcalm = readb(S3C2410_RTCALM);
327 unsigned int ticnt = readb (S3C2410_TICNT);
328 char *p = buf;
330 p += sprintf(p, "alarm_IRQ\t: %s\n",
331 (rtcalm & S3C2410_RTCALM_ALMEN) ? "yes" : "no" );
332 p += sprintf(p, "periodic_IRQ\t: %s\n",
333 (ticnt & S3C2410_TICNT_ENABLE) ? "yes" : "no" );
334 p += sprintf(p, "periodic_freq\t: %d\n", s3c2410_rtc_freq);
336 return p - buf;
339 static int s3c2410_rtc_open(void)
341 int ret;
343 ret = request_irq(s3c2410_rtc_alarmno, s3c2410_rtc_alarmirq,
344 SA_INTERRUPT, "s3c2410-rtc alarm", NULL);
346 if (ret)
347 printk(KERN_ERR "IRQ%d already in use\n", s3c2410_rtc_alarmno);
349 ret = request_irq(s3c2410_rtc_tickno, s3c2410_rtc_tickirq,
350 SA_INTERRUPT, "s3c2410-rtc tick", NULL);
352 if (ret) {
353 printk(KERN_ERR "IRQ%d already in use\n", s3c2410_rtc_tickno);
354 goto tick_err;
357 return ret;
359 tick_err:
360 free_irq(s3c2410_rtc_alarmno, NULL);
361 return ret;
364 static void s3c2410_rtc_release(void)
366 /* do not clear AIE here, it may be needed for wake */
368 s3c2410_rtc_setpie(0);
369 free_irq(s3c2410_rtc_alarmno, NULL);
370 free_irq(s3c2410_rtc_tickno, NULL);
373 static struct rtc_ops s3c2410_rtcops = {
374 .owner = THIS_MODULE,
375 .open = s3c2410_rtc_open,
376 .release = s3c2410_rtc_release,
377 .ioctl = s3c2410_rtc_ioctl,
378 .read_time = s3c2410_rtc_gettime,
379 .set_time = s3c2410_rtc_settime,
380 .read_alarm = s3c2410_rtc_getalarm,
381 .set_alarm = s3c2410_rtc_setalarm,
382 .proc = s3c2410_rtc_proc,
385 static void s3c2410_rtc_enable(struct device *dev, int en)
387 unsigned int tmp;
389 if (s3c2410_rtc_base == NULL)
390 return;
392 if (!en) {
393 tmp = readb(S3C2410_RTCCON);
394 writeb(tmp & ~S3C2410_RTCCON_RTCEN, S3C2410_RTCCON);
396 tmp = readb(S3C2410_TICNT);
397 writeb(tmp & ~S3C2410_TICNT_ENABLE, S3C2410_TICNT);
398 } else {
399 /* re-enable the device, and check it is ok */
401 if ((readb(S3C2410_RTCCON) & S3C2410_RTCCON_RTCEN) == 0){
402 dev_info(dev, "rtc disabled, re-enabling\n");
404 tmp = readb(S3C2410_RTCCON);
405 writeb(tmp | S3C2410_RTCCON_RTCEN , S3C2410_RTCCON);
408 if ((readb(S3C2410_RTCCON) & S3C2410_RTCCON_CNTSEL)){
409 dev_info(dev, "removing S3C2410_RTCCON_CNTSEL\n");
411 tmp = readb(S3C2410_RTCCON);
412 writeb(tmp& ~S3C2410_RTCCON_CNTSEL , S3C2410_RTCCON);
415 if ((readb(S3C2410_RTCCON) & S3C2410_RTCCON_CLKRST)){
416 dev_info(dev, "removing S3C2410_RTCCON_CLKRST\n");
418 tmp = readb(S3C2410_RTCCON);
419 writeb(tmp & ~S3C2410_RTCCON_CLKRST, S3C2410_RTCCON);
424 static int s3c2410_rtc_remove(struct device *dev)
426 unregister_rtc(&s3c2410_rtcops);
428 s3c2410_rtc_setpie(0);
429 s3c2410_rtc_setaie(0);
431 if (s3c2410_rtc_mem != NULL) {
432 pr_debug("s3c2410_rtc: releasing s3c2410_rtc_mem\n");
433 iounmap(s3c2410_rtc_base);
434 release_resource(s3c2410_rtc_mem);
435 kfree(s3c2410_rtc_mem);
438 return 0;
441 static int s3c2410_rtc_probe(struct device *dev)
443 struct platform_device *pdev = to_platform_device(dev);
444 struct resource *res;
445 int ret;
447 pr_debug("%s: probe=%p, device=%p\n", __FUNCTION__, pdev, dev);
449 /* find the IRQs */
451 s3c2410_rtc_tickno = platform_get_irq(pdev, 1);
452 if (s3c2410_rtc_tickno <= 0) {
453 dev_err(dev, "no irq for rtc tick\n");
454 return -ENOENT;
457 s3c2410_rtc_alarmno = platform_get_irq(pdev, 0);
458 if (s3c2410_rtc_alarmno <= 0) {
459 dev_err(dev, "no irq for alarm\n");
460 return -ENOENT;
463 pr_debug("s3c2410_rtc: tick irq %d, alarm irq %d\n",
464 s3c2410_rtc_tickno, s3c2410_rtc_alarmno);
466 /* get the memory region */
468 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
469 if (res == NULL) {
470 dev_err(dev, "failed to get memory region resource\n");
471 return -ENOENT;
474 s3c2410_rtc_mem = request_mem_region(res->start, res->end-res->start+1,
475 pdev->name);
477 if (s3c2410_rtc_mem == NULL) {
478 dev_err(dev, "failed to reserve memory region\n");
479 ret = -ENOENT;
480 goto exit_err;
483 s3c2410_rtc_base = ioremap(res->start, res->end - res->start + 1);
484 if (s3c2410_rtc_base == NULL) {
485 dev_err(dev, "failed ioremap()\n");
486 ret = -EINVAL;
487 goto exit_err;
490 s3c2410_rtc_mem = res;
491 pr_debug("s3c2410_rtc_base=%p\n", s3c2410_rtc_base);
493 pr_debug("s3c2410_rtc: RTCCON=%02x\n", readb(S3C2410_RTCCON));
495 /* check to see if everything is setup correctly */
497 s3c2410_rtc_enable(dev, 1);
499 pr_debug("s3c2410_rtc: RTCCON=%02x\n", readb(S3C2410_RTCCON));
501 s3c2410_rtc_setfreq(s3c2410_rtc_freq);
503 /* register RTC and exit */
505 register_rtc(&s3c2410_rtcops);
506 return 0;
508 exit_err:
509 dev_err(dev, "error %d during initialisation\n", ret);
511 return ret;
514 #ifdef CONFIG_PM
516 /* S3C2410 RTC Power management control */
518 static struct timespec s3c2410_rtc_delta;
520 static int ticnt_save;
522 static int s3c2410_rtc_suspend(struct device *dev, pm_message_t state, u32 level)
524 struct rtc_time tm;
525 struct timespec time;
527 time.tv_nsec = 0;
529 if (level == SUSPEND_POWER_DOWN) {
530 /* save TICNT for anyone using periodic interrupts */
532 ticnt_save = readb(S3C2410_TICNT);
534 /* calculate time delta for suspend */
536 s3c2410_rtc_gettime(&tm);
537 rtc_tm_to_time(&tm, &time.tv_sec);
538 save_time_delta(&s3c2410_rtc_delta, &time);
539 s3c2410_rtc_enable(dev, 0);
542 return 0;
545 static int s3c2410_rtc_resume(struct device *dev, u32 level)
547 struct rtc_time tm;
548 struct timespec time;
550 time.tv_nsec = 0;
552 s3c2410_rtc_enable(dev, 1);
553 s3c2410_rtc_gettime(&tm);
554 rtc_tm_to_time(&tm, &time.tv_sec);
555 restore_time_delta(&s3c2410_rtc_delta, &time);
557 writeb(ticnt_save, S3C2410_TICNT);
558 return 0;
560 #else
561 #define s3c2410_rtc_suspend NULL
562 #define s3c2410_rtc_resume NULL
563 #endif
565 static struct device_driver s3c2410_rtcdrv = {
566 .name = "s3c2410-rtc",
567 .bus = &platform_bus_type,
568 .probe = s3c2410_rtc_probe,
569 .remove = s3c2410_rtc_remove,
570 .suspend = s3c2410_rtc_suspend,
571 .resume = s3c2410_rtc_resume,
574 static char __initdata banner[] = "S3C2410 RTC, (c) 2004 Simtec Electronics\n";
576 static int __init s3c2410_rtc_init(void)
578 printk(banner);
579 return driver_register(&s3c2410_rtcdrv);
582 static void __exit s3c2410_rtc_exit(void)
584 driver_unregister(&s3c2410_rtcdrv);
587 module_init(s3c2410_rtc_init);
588 module_exit(s3c2410_rtc_exit);
590 MODULE_DESCRIPTION("S3C24XX RTC Driver");
591 MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
592 MODULE_LICENSE("GPL");