17 #include <linux/module.h>
18 #include <linux/kernel.h>
20 #include <linux/rtc.h>
29 #include <linux/slab.h>
32 #define DRV_NAME "sh-rtc"
33 #define DRV_VERSION "0.2.3"
35 #define RTC_REG(r) ((r) * rtc_reg_size)
37 #define R64CNT RTC_REG(0)
39 #define RSECCNT RTC_REG(1)
40 #define RMINCNT RTC_REG(2)
41 #define RHRCNT RTC_REG(3)
42 #define RWKCNT RTC_REG(4)
43 #define RDAYCNT RTC_REG(5)
44 #define RMONCNT RTC_REG(6)
45 #define RYRCNT RTC_REG(7)
46 #define RSECAR RTC_REG(8)
47 #define RMINAR RTC_REG(9)
48 #define RHRAR RTC_REG(10)
49 #define RWKAR RTC_REG(11)
50 #define RDAYAR RTC_REG(12)
51 #define RMONAR RTC_REG(13)
52 #define RCR1 RTC_REG(14)
53 #define RCR2 RTC_REG(15)
72 #define PF_COUNT 0x200
85 #define RCR2_PESMASK 0x70
86 #define RCR2_RTCEN 0x08
88 #define RCR2_RESET 0x02
89 #define RCR2_START 0x01
105 static int __sh_rtc_interrupt(
struct sh_rtc *
rtc)
107 unsigned int tmp, pending;
121 static int __sh_rtc_alarm(
struct sh_rtc *
rtc)
123 unsigned int tmp, pending;
136 static int __sh_rtc_periodic(
struct sh_rtc *rtc)
140 unsigned int tmp, pending;
174 spin_lock(&rtc->
lock);
175 ret = __sh_rtc_interrupt(rtc);
176 spin_unlock(&rtc->
lock);
186 spin_lock(&rtc->
lock);
187 ret = __sh_rtc_alarm(rtc);
188 spin_unlock(&rtc->
lock);
198 spin_lock(&rtc->
lock);
199 ret = __sh_rtc_periodic(rtc);
200 spin_unlock(&rtc->
lock);
210 spin_lock(&rtc->
lock);
211 ret = __sh_rtc_interrupt(rtc);
212 ret |= __sh_rtc_alarm(rtc);
213 ret |= __sh_rtc_periodic(rtc);
214 spin_unlock(&rtc->
lock);
224 spin_lock_irq(&rtc->
lock);
239 spin_unlock_irq(&rtc->
lock);
244 static int sh_rtc_irq_set_freq(
struct device *dev,
int freq)
249 spin_lock_irq(&rtc->
lock);
290 spin_unlock_irq(&rtc->
lock);
294 static inline void sh_rtc_setaie(
struct device *dev,
unsigned int enable)
299 spin_lock_irq(&rtc->
lock);
310 spin_unlock_irq(&rtc->
lock);
328 static inline void sh_rtc_setcie(
struct device *dev,
unsigned int enable)
333 spin_lock_irq(&rtc->
lock);
344 spin_unlock_irq(&rtc->
lock);
347 static int sh_rtc_alarm_irq_enable(
struct device *dev,
unsigned int enabled)
349 sh_rtc_setaie(dev, enabled);
356 struct sh_rtc *rtc = platform_get_drvdata(pdev);
357 unsigned int sec128, sec2,
yr, yr100, cf_bit;
362 spin_lock_irq(&rtc->
lock);
384 yr100 =
bcd2bin((yr == 0x99) ? 0x19 : 0x20);
392 spin_unlock_irq(&rtc->
lock);
395 #if RTC_BIT_INVERTED != 0
396 if ((sec128 & RTC_BIT_INVERTED))
402 sh_rtc_setcie(dev, 0);
404 dev_dbg(dev,
"%s: tm is secs=%d, mins=%d, hours=%d, "
405 "mday=%d, mon=%d, year=%d, wday=%d\n",
413 static int sh_rtc_set_time(
struct device *dev,
struct rtc_time *tm)
416 struct sh_rtc *rtc = platform_get_drvdata(pdev);
420 spin_lock_irq(&rtc->
lock);
450 spin_unlock_irq(&rtc->
lock);
455 static inline int sh_rtc_read_alarm_value(
struct sh_rtc *rtc,
int reg_off)
472 struct sh_rtc *rtc = platform_get_drvdata(pdev);
475 spin_lock_irq(&rtc->
lock);
489 spin_unlock_irq(&rtc->
lock);
494 static inline void sh_rtc_write_alarm_value(
struct sh_rtc *rtc,
495 int value,
int reg_off)
504 static int sh_rtc_check_alarm(
struct rtc_time *tm)
542 struct sh_rtc *rtc = platform_get_drvdata(pdev);
547 err = sh_rtc_check_alarm(tm);
551 spin_lock_irq(&rtc->
lock);
567 sh_rtc_write_alarm_value(rtc, mon,
RMONAR);
574 spin_unlock_irq(&rtc->
lock);
580 .read_time = sh_rtc_read_time,
581 .set_time = sh_rtc_set_time,
582 .read_alarm = sh_rtc_read_alarm,
583 .set_alarm = sh_rtc_set_alarm,
585 .alarm_irq_enable = sh_rtc_alarm_irq_enable,
621 rtc->
regsize = resource_size(res);
640 snprintf(clk_name,
sizeof(clk_name),
"rtc%d", clk_id);
643 if (IS_ERR(rtc->
clk)) {
656 if (pdev->
dev.platform_data) {
672 "request IRQ failed with %d, IRQ %d\n", ret,
679 0,
"sh-rtc period", rtc);
682 "request period IRQ failed with %d, IRQ %d\n",
688 0,
"sh-rtc carry", rtc);
691 "request carry IRQ failed with %d, IRQ %d\n",
698 0,
"sh-rtc alarm", rtc);
701 "request alarm IRQ failed with %d, IRQ %d\n",
709 platform_set_drvdata(pdev, rtc);
712 sh_rtc_irq_set_freq(&pdev->
dev, 0);
713 sh_rtc_irq_set_state(&pdev->
dev, 0);
714 sh_rtc_setaie(&pdev->
dev, 0);
715 sh_rtc_setcie(&pdev->
dev, 0);
727 rtc->
rtc_dev->max_user_freq = 256;
752 struct sh_rtc *rtc = platform_get_drvdata(pdev);
755 sh_rtc_irq_set_state(&pdev->
dev, 0);
757 sh_rtc_setaie(&pdev->
dev, 0);
758 sh_rtc_setcie(&pdev->
dev, 0);
773 platform_set_drvdata(pdev,
NULL);
780 static void sh_rtc_set_irq_wake(
struct device *dev,
int enabled)
783 struct sh_rtc *rtc = platform_get_drvdata(pdev);
793 static int sh_rtc_suspend(
struct device *dev)
795 if (device_may_wakeup(dev))
796 sh_rtc_set_irq_wake(dev, 1);
801 static int sh_rtc_resume(
struct device *dev)
803 if (device_may_wakeup(dev))
804 sh_rtc_set_irq_wake(dev, 0);
809 static const struct dev_pm_ops sh_rtc_dev_pm_ops = {
810 .suspend = sh_rtc_suspend,
811 .resume = sh_rtc_resume,
818 .pm = &sh_rtc_dev_pm_ops,
823 static int __init sh_rtc_init(
void)
828 static void __exit sh_rtc_exit(
void)