22 #include <linux/kernel.h>
24 #include <linux/module.h>
28 #include <linux/rtc.h>
32 #include <linux/slab.h>
43 #define DAVINCI_PRTCIF_PID 0x00
44 #define PRTCIF_CTLR 0x04
45 #define PRTCIF_LDATA 0x08
46 #define PRTCIF_UDATA 0x0C
47 #define PRTCIF_INTEN 0x10
48 #define PRTCIF_INTFLG 0x14
51 #define PRTCIF_CTLR_BUSY BIT(31)
52 #define PRTCIF_CTLR_SIZE BIT(25)
53 #define PRTCIF_CTLR_DIR BIT(24)
54 #define PRTCIF_CTLR_BENU_MSB BIT(23)
55 #define PRTCIF_CTLR_BENU_3RD_BYTE BIT(22)
56 #define PRTCIF_CTLR_BENU_2ND_BYTE BIT(21)
57 #define PRTCIF_CTLR_BENU_LSB BIT(20)
58 #define PRTCIF_CTLR_BENU_MASK (0x00F00000)
59 #define PRTCIF_CTLR_BENL_MSB BIT(19)
60 #define PRTCIF_CTLR_BENL_3RD_BYTE BIT(18)
61 #define PRTCIF_CTLR_BENL_2ND_BYTE BIT(17)
62 #define PRTCIF_CTLR_BENL_LSB BIT(16)
63 #define PRTCIF_CTLR_BENL_MASK (0x000F0000)
66 #define PRTCIF_INTEN_RTCSS BIT(1)
67 #define PRTCIF_INTEN_RTCIF BIT(0)
68 #define PRTCIF_INTEN_MASK (PRTCIF_INTEN_RTCSS \
72 #define PRTCIF_INTFLG_RTCSS BIT(1)
73 #define PRTCIF_INTFLG_RTCIF BIT(0)
74 #define PRTCIF_INTFLG_MASK (PRTCIF_INTFLG_RTCSS \
75 | PRTCIF_INTFLG_RTCIF)
78 #define PRTCSS_RTC_INTC_EXTENA1 (0x0C)
79 #define PRTCSS_RTC_CTRL (0x10)
80 #define PRTCSS_RTC_WDT (0x11)
81 #define PRTCSS_RTC_TMR0 (0x12)
82 #define PRTCSS_RTC_TMR1 (0x13)
83 #define PRTCSS_RTC_CCTRL (0x14)
84 #define PRTCSS_RTC_SEC (0x15)
85 #define PRTCSS_RTC_MIN (0x16)
86 #define PRTCSS_RTC_HOUR (0x17)
87 #define PRTCSS_RTC_DAY0 (0x18)
88 #define PRTCSS_RTC_DAY1 (0x19)
89 #define PRTCSS_RTC_AMIN (0x1A)
90 #define PRTCSS_RTC_AHOUR (0x1B)
91 #define PRTCSS_RTC_ADAY0 (0x1C)
92 #define PRTCSS_RTC_ADAY1 (0x1D)
93 #define PRTCSS_RTC_CLKC_CNT (0x20)
96 #define PRTCSS_RTC_INTC_EXTENA1_MASK (0x07)
99 #define PRTCSS_RTC_CTRL_WDTBUS BIT(7)
100 #define PRTCSS_RTC_CTRL_WEN BIT(6)
101 #define PRTCSS_RTC_CTRL_WDRT BIT(5)
102 #define PRTCSS_RTC_CTRL_WDTFLG BIT(4)
103 #define PRTCSS_RTC_CTRL_TE BIT(3)
104 #define PRTCSS_RTC_CTRL_TIEN BIT(2)
105 #define PRTCSS_RTC_CTRL_TMRFLG BIT(1)
106 #define PRTCSS_RTC_CTRL_TMMD BIT(0)
109 #define PRTCSS_RTC_CCTRL_CALBUSY BIT(7)
110 #define PRTCSS_RTC_CCTRL_DAEN BIT(5)
111 #define PRTCSS_RTC_CCTRL_HAEN BIT(4)
112 #define PRTCSS_RTC_CCTRL_MAEN BIT(3)
113 #define PRTCSS_RTC_CCTRL_ALMFLG BIT(2)
114 #define PRTCSS_RTC_CCTRL_AIEN BIT(1)
115 #define PRTCSS_RTC_CCTRL_CAEN BIT(0)
144 static inline void rtcss_write(
struct davinci_rtc *davinci_rtc,
147 rtcif_wait(davinci_rtc);
152 rtcif_wait(davinci_rtc);
155 static inline u8 rtcss_read(
struct davinci_rtc *davinci_rtc,
u8 addr)
157 rtcif_wait(davinci_rtc);
162 rtcif_wait(davinci_rtc);
167 static inline void davinci_rtcss_calendar_wait(
struct davinci_rtc *davinci_rtc)
174 static irqreturn_t davinci_rtc_interrupt(
int irq,
void *class_dev)
176 struct davinci_rtc *davinci_rtc = class_dev;
180 u8 rtc_ctrl, rtc_cctrl;
198 }
else if (tmr_irq) {
216 davinci_rtc_ioctl(
struct device *
dev,
unsigned int cmd,
unsigned long arg)
240 spin_unlock_irqrestore(&davinci_rtc_lock, flags);
247 int tmp_days,
year, mon;
249 for (year = 2000;; year++) {
251 if (days >= tmp_days)
254 for (mon = 0;; mon++) {
256 if (days >= tmp_days) {
271 static int convert2days(
u16 *days,
struct rtc_time *tm)
280 for (i = 2000; i < 1900 + tm->
tm_year; i++)
288 static int davinci_rtc_read_time(
struct device *dev,
struct rtc_time *tm)
297 davinci_rtcss_calendar_wait(davinci_rtc);
300 davinci_rtcss_calendar_wait(davinci_rtc);
303 davinci_rtcss_calendar_wait(davinci_rtc);
306 davinci_rtcss_calendar_wait(davinci_rtc);
309 davinci_rtcss_calendar_wait(davinci_rtc);
312 spin_unlock_irqrestore(&davinci_rtc_lock, flags);
318 if (convertfromdays(days, tm) < 0)
324 static int davinci_rtc_set_time(
struct device *dev,
struct rtc_time *tm)
331 if (convert2days(&days, tm) < 0)
336 davinci_rtcss_calendar_wait(davinci_rtc);
339 davinci_rtcss_calendar_wait(davinci_rtc);
342 davinci_rtcss_calendar_wait(davinci_rtc);
345 davinci_rtcss_calendar_wait(davinci_rtc);
348 davinci_rtcss_calendar_wait(davinci_rtc);
355 spin_unlock_irqrestore(&davinci_rtc_lock, flags);
360 static int davinci_rtc_alarm_irq_enable(
struct device *dev,
378 davinci_rtcss_calendar_wait(davinci_rtc);
381 spin_unlock_irqrestore(&davinci_rtc_lock, flags);
386 static int davinci_rtc_read_alarm(
struct device *dev,
struct rtc_wkalrm *alm)
395 davinci_rtcss_calendar_wait(davinci_rtc);
398 davinci_rtcss_calendar_wait(davinci_rtc);
401 davinci_rtcss_calendar_wait(davinci_rtc);
404 davinci_rtcss_calendar_wait(davinci_rtc);
407 spin_unlock_irqrestore(&davinci_rtc_lock, flags);
412 if (convertfromdays(days, &alm->
time) < 0)
415 alm->
pending = !!(rtcss_read(davinci_rtc,
423 static int davinci_rtc_set_alarm(
struct device *dev,
struct rtc_wkalrm *alm)
429 if (alm->
time.tm_mday <= 0 && alm->
time.tm_mon < 0
430 && alm->
time.tm_year < 0) {
432 unsigned long now, then;
434 davinci_rtc_read_time(dev, &tm);
450 if (convert2days(&days, &alm->
time) < 0)
455 davinci_rtcss_calendar_wait(davinci_rtc);
458 davinci_rtcss_calendar_wait(davinci_rtc);
461 davinci_rtcss_calendar_wait(davinci_rtc);
464 davinci_rtcss_calendar_wait(davinci_rtc);
467 spin_unlock_irqrestore(&davinci_rtc_lock, flags);
473 .ioctl = davinci_rtc_ioctl,
474 .read_time = davinci_rtc_read_time,
475 .set_time = davinci_rtc_set_time,
476 .alarm_irq_enable = davinci_rtc_alarm_irq_enable,
477 .read_alarm = davinci_rtc_read_alarm,
478 .set_alarm = davinci_rtc_set_alarm,
484 struct davinci_rtc *davinci_rtc;
488 davinci_rtc = kzalloc(
sizeof(
struct davinci_rtc),
GFP_KERNEL);
490 dev_dbg(dev,
"could not allocate memory for private data\n");
495 if (davinci_rtc->
irq < 0) {
497 ret = davinci_rtc->
irq;
503 dev_err(dev,
"no mem resource\n");
509 davinci_rtc->
base_size = resource_size(res);
514 dev_err(dev,
"RTC registers at %08x are not free\n",
521 if (!davinci_rtc->
base) {
522 dev_err(dev,
"unable to ioremap MEM resource\n");
527 platform_set_drvdata(pdev, davinci_rtc);
531 if (IS_ERR(davinci_rtc->
rtc)) {
532 dev_err(dev,
"unable to register RTC device, err %ld\n",
533 PTR_ERR(davinci_rtc->
rtc));
545 0,
"davinci_rtc", davinci_rtc);
547 dev_err(dev,
"unable to register davinci RTC interrupt\n");
565 platform_set_drvdata(pdev,
NULL);
577 struct davinci_rtc *davinci_rtc = platform_get_drvdata(pdev);
590 platform_set_drvdata(pdev,
NULL);
598 .probe = davinci_rtc_probe,
601 .name =
"rtc_davinci",
606 static int __init rtc_init(
void)
612 static void __exit rtc_exit(
void)