17 #include <linux/module.h>
19 #include <linux/string.h>
23 #include <linux/rtc.h>
27 #include <linux/slab.h>
32 #include <mach/hardware.h>
52 static struct clk *rtc_clk;
53 static void __iomem *s3c_rtc_base;
54 static int s3c_rtc_alarmno =
NO_IRQ;
55 static int s3c_rtc_tickno =
NO_IRQ;
61 static void s3c_rtc_alarm_clk_enable(
bool enable)
64 static bool alarm_clk_enabled;
65 unsigned long irq_flags;
69 if (!alarm_clk_enabled) {
71 alarm_clk_enabled =
true;
74 if (alarm_clk_enabled) {
76 alarm_clk_enabled =
false;
79 spin_unlock_irqrestore(&s3c_rtc_alarm_clk_lock, irq_flags);
84 static irqreturn_t s3c_rtc_alarmirq(
int irq,
void *
id)
96 s3c_rtc_alarm_clk_enable(
false);
101 static irqreturn_t s3c_rtc_tickirq(
int irq,
void *
id)
120 pr_debug(
"%s: aie=%d\n", __func__, enabled);
131 s3c_rtc_alarm_clk_enable(enabled);
136 static int s3c_rtc_setfreq(
struct device *dev,
int freq)
139 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
140 unsigned int tmp = 0;
147 spin_lock_irq(&s3c_rtc_pie_lock);
167 spin_unlock_irq(&s3c_rtc_pie_lock);
175 static int s3c_rtc_gettime(
struct device *dev,
struct rtc_time *rtc_tm)
177 unsigned int have_retried = 0;
194 if (rtc_tm->
tm_sec == 0 && !have_retried) {
208 pr_debug(
"read time %04d.%02d.%02d %02d:%02d:%02d\n",
220 void __iomem *base = s3c_rtc_base;
223 pr_debug(
"set time %04d.%02d.%02d %02d:%02d:%02d\n",
229 if (year < 0 || year >= 100) {
230 dev_err(dev,
"rtc only supports 100 years\n");
249 void __iomem *base = s3c_rtc_base;
264 pr_debug(
"read alarm %d, %04d.%02d.%02d %02d:%02d:%02d\n",
311 void __iomem *base = s3c_rtc_base;
312 unsigned int alrm_en;
315 pr_debug(
"s3c_rtc_setalarm: %d, %04d.%02d.%02d %02d:%02d:%02d\n",
338 pr_debug(
"setting S3C2410_RTCALM to %08x\n", alrm_en);
342 s3c_rtc_setaie(dev, alrm->
enabled);
361 seq_printf(seq,
"periodic_IRQ\t: %s\n", ticnt ?
"yes" :
"no");
367 .read_time = s3c_rtc_gettime,
368 .set_time = s3c_rtc_settime,
369 .read_alarm = s3c_rtc_getalarm,
370 .set_alarm = s3c_rtc_setalarm,
371 .proc = s3c_rtc_proc,
372 .alarm_irq_enable = s3c_rtc_setaie,
377 void __iomem *base = s3c_rtc_base;
380 if (s3c_rtc_base ==
NULL)
400 dev_info(&pdev->
dev,
"rtc disabled, re-enabling\n");
403 writew(tmp | S3C2410_RTCCON_RTCEN,
411 writew(tmp & ~S3C2410_RTCCON_CNTSEL,
419 writew(tmp & ~S3C2410_RTCCON_CLKRST,
433 platform_set_drvdata(dev,
NULL);
436 s3c_rtc_setaie(&dev->
dev, 0);
450 static inline int s3c_rtc_get_driver_data(
struct platform_device *pdev)
454 if (pdev->
dev.of_node) {
472 pr_debug(
"%s: probe=%p\n", __func__, pdev);
477 if (s3c_rtc_tickno < 0) {
479 return s3c_rtc_tickno;
483 if (s3c_rtc_alarmno < 0) {
485 return s3c_rtc_alarmno;
488 pr_debug(
"s3c2410_rtc: tick irq %d, alarm irq %d\n",
489 s3c_rtc_tickno, s3c_rtc_alarmno);
495 dev_err(&pdev->
dev,
"failed to get memory region resource\n");
502 if (s3c_rtc_mem ==
NULL) {
503 dev_err(&pdev->
dev,
"failed to reserve memory region\n");
509 if (s3c_rtc_base ==
NULL) {
516 if (IS_ERR(rtc_clk)) {
517 dev_err(&pdev->
dev,
"failed to find rtc clock source\n");
518 ret = PTR_ERR(rtc_clk);
527 s3c_rtc_enable(pdev, 1);
529 pr_debug(
"s3c2410_rtc: RTCCON=%02x\n",
545 s3c_rtc_cpu_type = s3c_rtc_get_driver_data(pdev);
549 s3c_rtc_gettime(
NULL, &rtc_tm);
559 s3c_rtc_settime(
NULL, &rtc_tm);
561 dev_warn(&pdev->
dev,
"warning: invalid RTC value so initializing it\n");
575 platform_set_drvdata(pdev, rtc);
577 s3c_rtc_setfreq(&pdev->
dev, 1);
579 ret =
request_irq(s3c_rtc_alarmno, s3c_rtc_alarmirq,
580 0,
"s3c2410-rtc alarm", rtc);
582 dev_err(&pdev->
dev,
"IRQ%d error %d\n", s3c_rtc_alarmno, ret);
587 0,
"s3c2410-rtc tick", rtc);
589 dev_err(&pdev->
dev,
"IRQ%d error %d\n", s3c_rtc_tickno, ret);
602 platform_set_drvdata(pdev,
NULL);
606 s3c_rtc_enable(pdev, 0);
624 static int ticnt_save, ticnt_en_save;
635 s3c_rtc_enable(pdev, 0);
637 if (device_may_wakeup(&pdev->
dev) && !wake_en) {
638 if (enable_irq_wake(s3c_rtc_alarmno) == 0)
641 dev_err(&pdev->
dev,
"enable_irq_wake failed\n");
653 s3c_rtc_enable(pdev, 1);
655 if (s3c_rtc_cpu_type ==
TYPE_S3C64XX && ticnt_en_save) {
660 if (device_may_wakeup(&pdev->
dev) && wake_en) {
661 disable_irq_wake(s3c_rtc_alarmno);
669 #define s3c_rtc_suspend NULL
670 #define s3c_rtc_resume NULL
683 .compatible =
"samsung,s3c2410-rtc",
686 .compatible =
"samsung,s3c2416-rtc",
689 .compatible =
"samsung,s3c2443-rtc",
692 .compatible =
"samsung,s3c6410-rtc",
699 #define s3c_rtc_dt_match NULL
704 .name =
"s3c2410-rtc",
707 .name =
"s3c2416-rtc",
710 .name =
"s3c2443-rtc",
713 .name =
"s3c64xx-rtc",
722 .probe = s3c_rtc_probe,
726 .id_table = s3c_rtc_driver_ids,