ASoC: Ensure we delay long enough for WM8994 FLL to lock
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / rtc / rtc-s3c.c
blob2a305ef579a716d3a26768219c87c97f5c03eb79
1 /* drivers/rtc/rtc-s3c.c
3 * Copyright (c) 2004,2006 Simtec Electronics
4 * Ben Dooks, <ben@simtec.co.uk>
5 * http://armlinux.simtec.co.uk/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * S3C2410/S3C2440/S3C24XX Internal RTC Driver
14 #include <linux/module.h>
15 #include <linux/fs.h>
16 #include <linux/string.h>
17 #include <linux/init.h>
18 #include <linux/platform_device.h>
19 #include <linux/interrupt.h>
20 #include <linux/rtc.h>
21 #include <linux/bcd.h>
22 #include <linux/clk.h>
23 #include <linux/log2.h>
24 #include <linux/slab.h>
26 #include <mach/hardware.h>
27 #include <asm/uaccess.h>
28 #include <asm/io.h>
29 #include <asm/irq.h>
30 #include <plat/regs-rtc.h>
32 enum s3c_cpu_type {
33 TYPE_S3C2410,
34 TYPE_S3C64XX,
37 /* I have yet to find an S3C implementation with more than one
38 * of these rtc blocks in */
40 static struct resource *s3c_rtc_mem;
42 static void __iomem *s3c_rtc_base;
43 static int s3c_rtc_alarmno = NO_IRQ;
44 static int s3c_rtc_tickno = NO_IRQ;
45 static enum s3c_cpu_type s3c_rtc_cpu_type;
47 static DEFINE_SPINLOCK(s3c_rtc_pie_lock);
49 /* IRQ Handlers */
51 static irqreturn_t s3c_rtc_alarmirq(int irq, void *id)
53 struct rtc_device *rdev = id;
55 rtc_update_irq(rdev, 1, RTC_AF | RTC_IRQF);
56 return IRQ_HANDLED;
59 static irqreturn_t s3c_rtc_tickirq(int irq, void *id)
61 struct rtc_device *rdev = id;
63 rtc_update_irq(rdev, 1, RTC_PF | RTC_IRQF);
64 return IRQ_HANDLED;
67 /* Update control registers */
68 static void s3c_rtc_setaie(int to)
70 unsigned int tmp;
72 pr_debug("%s: aie=%d\n", __func__, to);
74 tmp = readb(s3c_rtc_base + S3C2410_RTCALM) & ~S3C2410_RTCALM_ALMEN;
76 if (to)
77 tmp |= S3C2410_RTCALM_ALMEN;
79 writeb(tmp, s3c_rtc_base + S3C2410_RTCALM);
82 static int s3c_rtc_setpie(struct device *dev, int enabled)
84 unsigned int tmp;
86 pr_debug("%s: pie=%d\n", __func__, enabled);
88 spin_lock_irq(&s3c_rtc_pie_lock);
90 if (s3c_rtc_cpu_type == TYPE_S3C64XX) {
91 tmp = readb(s3c_rtc_base + S3C2410_RTCCON);
92 tmp &= ~S3C64XX_RTCCON_TICEN;
94 if (enabled)
95 tmp |= S3C64XX_RTCCON_TICEN;
97 writeb(tmp, s3c_rtc_base + S3C2410_RTCCON);
98 } else {
99 tmp = readb(s3c_rtc_base + S3C2410_TICNT);
100 tmp &= ~S3C2410_TICNT_ENABLE;
102 if (enabled)
103 tmp |= S3C2410_TICNT_ENABLE;
105 writeb(tmp, s3c_rtc_base + S3C2410_TICNT);
108 spin_unlock_irq(&s3c_rtc_pie_lock);
110 return 0;
113 static int s3c_rtc_setfreq(struct device *dev, int freq)
115 struct platform_device *pdev = to_platform_device(dev);
116 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
117 unsigned int tmp = 0;
119 if (!is_power_of_2(freq))
120 return -EINVAL;
122 spin_lock_irq(&s3c_rtc_pie_lock);
124 if (s3c_rtc_cpu_type == TYPE_S3C2410) {
125 tmp = readb(s3c_rtc_base + S3C2410_TICNT);
126 tmp &= S3C2410_TICNT_ENABLE;
129 tmp |= (rtc_dev->max_user_freq / freq)-1;
131 writeb(tmp, s3c_rtc_base + S3C2410_TICNT);
132 spin_unlock_irq(&s3c_rtc_pie_lock);
134 return 0;
137 /* Time read/write */
139 static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
141 unsigned int have_retried = 0;
142 void __iomem *base = s3c_rtc_base;
144 retry_get_time:
145 rtc_tm->tm_min = readb(base + S3C2410_RTCMIN);
146 rtc_tm->tm_hour = readb(base + S3C2410_RTCHOUR);
147 rtc_tm->tm_mday = readb(base + S3C2410_RTCDATE);
148 rtc_tm->tm_mon = readb(base + S3C2410_RTCMON);
149 rtc_tm->tm_year = readb(base + S3C2410_RTCYEAR);
150 rtc_tm->tm_sec = readb(base + S3C2410_RTCSEC);
152 /* the only way to work out wether the system was mid-update
153 * when we read it is to check the second counter, and if it
154 * is zero, then we re-try the entire read
157 if (rtc_tm->tm_sec == 0 && !have_retried) {
158 have_retried = 1;
159 goto retry_get_time;
162 pr_debug("read time %02x.%02x.%02x %02x/%02x/%02x\n",
163 rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday,
164 rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);
166 rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
167 rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
168 rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
169 rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
170 rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
171 rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);
173 rtc_tm->tm_year += 100;
174 rtc_tm->tm_mon -= 1;
176 return 0;
179 static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm)
181 void __iomem *base = s3c_rtc_base;
182 int year = tm->tm_year - 100;
184 pr_debug("set time %02d.%02d.%02d %02d/%02d/%02d\n",
185 tm->tm_year, tm->tm_mon, tm->tm_mday,
186 tm->tm_hour, tm->tm_min, tm->tm_sec);
188 /* we get around y2k by simply not supporting it */
190 if (year < 0 || year >= 100) {
191 dev_err(dev, "rtc only supports 100 years\n");
192 return -EINVAL;
195 writeb(bin2bcd(tm->tm_sec), base + S3C2410_RTCSEC);
196 writeb(bin2bcd(tm->tm_min), base + S3C2410_RTCMIN);
197 writeb(bin2bcd(tm->tm_hour), base + S3C2410_RTCHOUR);
198 writeb(bin2bcd(tm->tm_mday), base + S3C2410_RTCDATE);
199 writeb(bin2bcd(tm->tm_mon + 1), base + S3C2410_RTCMON);
200 writeb(bin2bcd(year), base + S3C2410_RTCYEAR);
202 return 0;
205 static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
207 struct rtc_time *alm_tm = &alrm->time;
208 void __iomem *base = s3c_rtc_base;
209 unsigned int alm_en;
211 alm_tm->tm_sec = readb(base + S3C2410_ALMSEC);
212 alm_tm->tm_min = readb(base + S3C2410_ALMMIN);
213 alm_tm->tm_hour = readb(base + S3C2410_ALMHOUR);
214 alm_tm->tm_mon = readb(base + S3C2410_ALMMON);
215 alm_tm->tm_mday = readb(base + S3C2410_ALMDATE);
216 alm_tm->tm_year = readb(base + S3C2410_ALMYEAR);
218 alm_en = readb(base + S3C2410_RTCALM);
220 alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0;
222 pr_debug("read alarm %02x %02x.%02x.%02x %02x/%02x/%02x\n",
223 alm_en,
224 alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday,
225 alm_tm->tm_hour, alm_tm->tm_min, alm_tm->tm_sec);
228 /* decode the alarm enable field */
230 if (alm_en & S3C2410_RTCALM_SECEN)
231 alm_tm->tm_sec = bcd2bin(alm_tm->tm_sec);
232 else
233 alm_tm->tm_sec = 0xff;
235 if (alm_en & S3C2410_RTCALM_MINEN)
236 alm_tm->tm_min = bcd2bin(alm_tm->tm_min);
237 else
238 alm_tm->tm_min = 0xff;
240 if (alm_en & S3C2410_RTCALM_HOUREN)
241 alm_tm->tm_hour = bcd2bin(alm_tm->tm_hour);
242 else
243 alm_tm->tm_hour = 0xff;
245 if (alm_en & S3C2410_RTCALM_DAYEN)
246 alm_tm->tm_mday = bcd2bin(alm_tm->tm_mday);
247 else
248 alm_tm->tm_mday = 0xff;
250 if (alm_en & S3C2410_RTCALM_MONEN) {
251 alm_tm->tm_mon = bcd2bin(alm_tm->tm_mon);
252 alm_tm->tm_mon -= 1;
253 } else {
254 alm_tm->tm_mon = 0xff;
257 if (alm_en & S3C2410_RTCALM_YEAREN)
258 alm_tm->tm_year = bcd2bin(alm_tm->tm_year);
259 else
260 alm_tm->tm_year = 0xffff;
262 return 0;
265 static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
267 struct rtc_time *tm = &alrm->time;
268 void __iomem *base = s3c_rtc_base;
269 unsigned int alrm_en;
271 pr_debug("s3c_rtc_setalarm: %d, %02x/%02x/%02x %02x.%02x.%02x\n",
272 alrm->enabled,
273 tm->tm_mday & 0xff, tm->tm_mon & 0xff, tm->tm_year & 0xff,
274 tm->tm_hour & 0xff, tm->tm_min & 0xff, tm->tm_sec);
277 alrm_en = readb(base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;
278 writeb(0x00, base + S3C2410_RTCALM);
280 if (tm->tm_sec < 60 && tm->tm_sec >= 0) {
281 alrm_en |= S3C2410_RTCALM_SECEN;
282 writeb(bin2bcd(tm->tm_sec), base + S3C2410_ALMSEC);
285 if (tm->tm_min < 60 && tm->tm_min >= 0) {
286 alrm_en |= S3C2410_RTCALM_MINEN;
287 writeb(bin2bcd(tm->tm_min), base + S3C2410_ALMMIN);
290 if (tm->tm_hour < 24 && tm->tm_hour >= 0) {
291 alrm_en |= S3C2410_RTCALM_HOUREN;
292 writeb(bin2bcd(tm->tm_hour), base + S3C2410_ALMHOUR);
295 pr_debug("setting S3C2410_RTCALM to %08x\n", alrm_en);
297 writeb(alrm_en, base + S3C2410_RTCALM);
299 s3c_rtc_setaie(alrm->enabled);
301 return 0;
304 static int s3c_rtc_proc(struct device *dev, struct seq_file *seq)
306 unsigned int ticnt;
308 if (s3c_rtc_cpu_type == TYPE_S3C64XX) {
309 ticnt = readb(s3c_rtc_base + S3C2410_RTCCON);
310 ticnt &= S3C64XX_RTCCON_TICEN;
311 } else {
312 ticnt = readb(s3c_rtc_base + S3C2410_TICNT);
313 ticnt &= S3C2410_TICNT_ENABLE;
316 seq_printf(seq, "periodic_IRQ\t: %s\n", ticnt ? "yes" : "no");
317 return 0;
320 static int s3c_rtc_open(struct device *dev)
322 struct platform_device *pdev = to_platform_device(dev);
323 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
324 int ret;
326 ret = request_irq(s3c_rtc_alarmno, s3c_rtc_alarmirq,
327 IRQF_DISABLED, "s3c2410-rtc alarm", rtc_dev);
329 if (ret) {
330 dev_err(dev, "IRQ%d error %d\n", s3c_rtc_alarmno, ret);
331 return ret;
334 ret = request_irq(s3c_rtc_tickno, s3c_rtc_tickirq,
335 IRQF_DISABLED, "s3c2410-rtc tick", rtc_dev);
337 if (ret) {
338 dev_err(dev, "IRQ%d error %d\n", s3c_rtc_tickno, ret);
339 goto tick_err;
342 return ret;
344 tick_err:
345 free_irq(s3c_rtc_alarmno, rtc_dev);
346 return ret;
349 static void s3c_rtc_release(struct device *dev)
351 struct platform_device *pdev = to_platform_device(dev);
352 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
354 /* do not clear AIE here, it may be needed for wake */
356 s3c_rtc_setpie(dev, 0);
357 free_irq(s3c_rtc_alarmno, rtc_dev);
358 free_irq(s3c_rtc_tickno, rtc_dev);
361 static const struct rtc_class_ops s3c_rtcops = {
362 .open = s3c_rtc_open,
363 .release = s3c_rtc_release,
364 .read_time = s3c_rtc_gettime,
365 .set_time = s3c_rtc_settime,
366 .read_alarm = s3c_rtc_getalarm,
367 .set_alarm = s3c_rtc_setalarm,
368 .irq_set_freq = s3c_rtc_setfreq,
369 .irq_set_state = s3c_rtc_setpie,
370 .proc = s3c_rtc_proc,
373 static void s3c_rtc_enable(struct platform_device *pdev, int en)
375 void __iomem *base = s3c_rtc_base;
376 unsigned int tmp;
378 if (s3c_rtc_base == NULL)
379 return;
381 if (!en) {
382 tmp = readb(base + S3C2410_RTCCON);
383 if (s3c_rtc_cpu_type == TYPE_S3C64XX)
384 tmp &= ~S3C64XX_RTCCON_TICEN;
385 tmp &= ~S3C2410_RTCCON_RTCEN;
386 writeb(tmp, base + S3C2410_RTCCON);
388 if (s3c_rtc_cpu_type == TYPE_S3C2410) {
389 tmp = readb(base + S3C2410_TICNT);
390 tmp &= ~S3C2410_TICNT_ENABLE;
391 writeb(tmp, base + S3C2410_TICNT);
393 } else {
394 /* re-enable the device, and check it is ok */
396 if ((readb(base+S3C2410_RTCCON) & S3C2410_RTCCON_RTCEN) == 0){
397 dev_info(&pdev->dev, "rtc disabled, re-enabling\n");
399 tmp = readb(base + S3C2410_RTCCON);
400 writeb(tmp|S3C2410_RTCCON_RTCEN, base+S3C2410_RTCCON);
403 if ((readb(base + S3C2410_RTCCON) & S3C2410_RTCCON_CNTSEL)){
404 dev_info(&pdev->dev, "removing RTCCON_CNTSEL\n");
406 tmp = readb(base + S3C2410_RTCCON);
407 writeb(tmp& ~S3C2410_RTCCON_CNTSEL, base+S3C2410_RTCCON);
410 if ((readb(base + S3C2410_RTCCON) & S3C2410_RTCCON_CLKRST)){
411 dev_info(&pdev->dev, "removing RTCCON_CLKRST\n");
413 tmp = readb(base + S3C2410_RTCCON);
414 writeb(tmp & ~S3C2410_RTCCON_CLKRST, base+S3C2410_RTCCON);
419 static int __devexit s3c_rtc_remove(struct platform_device *dev)
421 struct rtc_device *rtc = platform_get_drvdata(dev);
423 platform_set_drvdata(dev, NULL);
424 rtc_device_unregister(rtc);
426 s3c_rtc_setpie(&dev->dev, 0);
427 s3c_rtc_setaie(0);
429 iounmap(s3c_rtc_base);
430 release_resource(s3c_rtc_mem);
431 kfree(s3c_rtc_mem);
433 return 0;
436 static int __devinit s3c_rtc_probe(struct platform_device *pdev)
438 struct rtc_device *rtc;
439 struct resource *res;
440 int ret;
442 pr_debug("%s: probe=%p\n", __func__, pdev);
444 /* find the IRQs */
446 s3c_rtc_tickno = platform_get_irq(pdev, 1);
447 if (s3c_rtc_tickno < 0) {
448 dev_err(&pdev->dev, "no irq for rtc tick\n");
449 return -ENOENT;
452 s3c_rtc_alarmno = platform_get_irq(pdev, 0);
453 if (s3c_rtc_alarmno < 0) {
454 dev_err(&pdev->dev, "no irq for alarm\n");
455 return -ENOENT;
458 pr_debug("s3c2410_rtc: tick irq %d, alarm irq %d\n",
459 s3c_rtc_tickno, s3c_rtc_alarmno);
461 /* get the memory region */
463 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
464 if (res == NULL) {
465 dev_err(&pdev->dev, "failed to get memory region resource\n");
466 return -ENOENT;
469 s3c_rtc_mem = request_mem_region(res->start,
470 res->end-res->start+1,
471 pdev->name);
473 if (s3c_rtc_mem == NULL) {
474 dev_err(&pdev->dev, "failed to reserve memory region\n");
475 ret = -ENOENT;
476 goto err_nores;
479 s3c_rtc_base = ioremap(res->start, res->end - res->start + 1);
480 if (s3c_rtc_base == NULL) {
481 dev_err(&pdev->dev, "failed ioremap()\n");
482 ret = -EINVAL;
483 goto err_nomap;
486 /* check to see if everything is setup correctly */
488 s3c_rtc_enable(pdev, 1);
490 pr_debug("s3c2410_rtc: RTCCON=%02x\n",
491 readb(s3c_rtc_base + S3C2410_RTCCON));
493 device_init_wakeup(&pdev->dev, 1);
495 /* register RTC and exit */
497 rtc = rtc_device_register("s3c", &pdev->dev, &s3c_rtcops,
498 THIS_MODULE);
500 if (IS_ERR(rtc)) {
501 dev_err(&pdev->dev, "cannot attach rtc\n");
502 ret = PTR_ERR(rtc);
503 goto err_nortc;
506 s3c_rtc_cpu_type = platform_get_device_id(pdev)->driver_data;
508 if (s3c_rtc_cpu_type == TYPE_S3C64XX)
509 rtc->max_user_freq = 32768;
510 else
511 rtc->max_user_freq = 128;
513 platform_set_drvdata(pdev, rtc);
515 s3c_rtc_setfreq(&pdev->dev, 1);
517 return 0;
519 err_nortc:
520 s3c_rtc_enable(pdev, 0);
521 iounmap(s3c_rtc_base);
523 err_nomap:
524 release_resource(s3c_rtc_mem);
526 err_nores:
527 return ret;
530 #ifdef CONFIG_PM
532 /* RTC Power management control */
534 static int ticnt_save, ticnt_en_save;
536 static int s3c_rtc_suspend(struct platform_device *pdev, pm_message_t state)
538 /* save TICNT for anyone using periodic interrupts */
539 ticnt_save = readb(s3c_rtc_base + S3C2410_TICNT);
540 if (s3c_rtc_cpu_type == TYPE_S3C64XX) {
541 ticnt_en_save = readb(s3c_rtc_base + S3C2410_RTCCON);
542 ticnt_en_save &= S3C64XX_RTCCON_TICEN;
544 s3c_rtc_enable(pdev, 0);
546 if (device_may_wakeup(&pdev->dev))
547 enable_irq_wake(s3c_rtc_alarmno);
549 return 0;
552 static int s3c_rtc_resume(struct platform_device *pdev)
554 unsigned int tmp;
556 s3c_rtc_enable(pdev, 1);
557 writeb(ticnt_save, s3c_rtc_base + S3C2410_TICNT);
558 if (s3c_rtc_cpu_type == TYPE_S3C64XX && ticnt_en_save) {
559 tmp = readb(s3c_rtc_base + S3C2410_RTCCON);
560 writeb(tmp | ticnt_en_save, s3c_rtc_base + S3C2410_RTCCON);
563 if (device_may_wakeup(&pdev->dev))
564 disable_irq_wake(s3c_rtc_alarmno);
566 return 0;
568 #else
569 #define s3c_rtc_suspend NULL
570 #define s3c_rtc_resume NULL
571 #endif
573 static struct platform_device_id s3c_rtc_driver_ids[] = {
575 .name = "s3c2410-rtc",
576 .driver_data = TYPE_S3C2410,
577 }, {
578 .name = "s3c64xx-rtc",
579 .driver_data = TYPE_S3C64XX,
584 MODULE_DEVICE_TABLE(platform, s3c_rtc_driver_ids);
586 static struct platform_driver s3c_rtc_driver = {
587 .probe = s3c_rtc_probe,
588 .remove = __devexit_p(s3c_rtc_remove),
589 .suspend = s3c_rtc_suspend,
590 .resume = s3c_rtc_resume,
591 .id_table = s3c_rtc_driver_ids,
592 .driver = {
593 .name = "s3c-rtc",
594 .owner = THIS_MODULE,
598 static char __initdata banner[] = "S3C24XX RTC, (c) 2004,2006 Simtec Electronics\n";
600 static int __init s3c_rtc_init(void)
602 printk(banner);
603 return platform_driver_register(&s3c_rtc_driver);
606 static void __exit s3c_rtc_exit(void)
608 platform_driver_unregister(&s3c_rtc_driver);
611 module_init(s3c_rtc_init);
612 module_exit(s3c_rtc_exit);
614 MODULE_DESCRIPTION("Samsung S3C RTC Driver");
615 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
616 MODULE_LICENSE("GPL");
617 MODULE_ALIAS("platform:s3c2410-rtc");