13 #include <linux/rtc.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
20 #include <mach/hardware.h>
22 #define RTC_INPUT_CLK_32768HZ (0x00 << 5)
23 #define RTC_INPUT_CLK_32000HZ (0x01 << 5)
24 #define RTC_INPUT_CLK_38400HZ (0x02 << 5)
26 #define RTC_SW_BIT (1 << 0)
27 #define RTC_ALM_BIT (1 << 2)
28 #define RTC_1HZ_BIT (1 << 4)
29 #define RTC_2HZ_BIT (1 << 7)
30 #define RTC_SAM0_BIT (1 << 8)
31 #define RTC_SAM1_BIT (1 << 9)
32 #define RTC_SAM2_BIT (1 << 10)
33 #define RTC_SAM3_BIT (1 << 11)
34 #define RTC_SAM4_BIT (1 << 12)
35 #define RTC_SAM5_BIT (1 << 13)
36 #define RTC_SAM6_BIT (1 << 14)
37 #define RTC_SAM7_BIT (1 << 15)
38 #define PIT_ALL_ON (RTC_2HZ_BIT | RTC_SAM0_BIT | RTC_SAM1_BIT | \
39 RTC_SAM2_BIT | RTC_SAM3_BIT | RTC_SAM4_BIT | \
40 RTC_SAM5_BIT | RTC_SAM6_BIT | RTC_SAM7_BIT)
42 #define RTC_ENABLE_BIT (1 << 7)
45 #define MAX_PIE_FREQ 512
58 #define MXC_RTC_TIME 0
59 #define MXC_RTC_ALARM 1
61 #define RTC_HOURMIN 0x00
62 #define RTC_SECOND 0x04
63 #define RTC_ALRM_HM 0x08
64 #define RTC_ALRM_SEC 0x0C
65 #define RTC_RTCCTL 0x10
66 #define RTC_RTCISR 0x14
67 #define RTC_RTCIENR 0x18
68 #define RTC_STPWCH 0x1C
70 #define RTC_DAYALARM 0x24
71 #define RTC_TEST1 0x28
72 #define RTC_TEST2 0x2C
73 #define RTC_TEST3 0x30
87 static u32 get_alarm_or_time(
struct device *
dev,
int time_alarm)
110 return (((day * 24 + hr) * 60) +
min) * 60 +
sec;
116 static void set_alarm_or_time(
struct device *dev,
int time_alarm,
u32 time)
132 sec = time - min * 60;
134 temp = (hr << 8) + min;
136 switch (time_alarm) {
154 static int rtc_update_alarm(
struct device *dev,
struct rtc_time *alrm)
157 unsigned long now,
time;
164 alarm_tm.tm_year = now_tm.tm_year;
165 alarm_tm.tm_mon = now_tm.tm_mon;
166 alarm_tm.tm_mday = now_tm.tm_mday;
167 alarm_tm.tm_hour = alrm->
tm_hour;
168 alarm_tm.tm_min = alrm->
tm_min;
169 alarm_tm.tm_sec = alrm->
tm_sec;
179 static void mxc_rtc_irq_enable(
struct device *dev,
unsigned int bit,
187 spin_lock_irq(&pdata->
rtc->irq_lock);
196 spin_unlock_irq(&pdata->
rtc->irq_lock);
218 mxc_rtc_irq_enable(&pdev->
dev, RTC_ALM_BIT, 0);
228 spin_unlock_irqrestore(&pdata->
rtc->irq_lock, flags);
236 static void mxc_rtc_release(
struct device *dev)
242 spin_lock_irq(&pdata->
rtc->irq_lock);
250 spin_unlock_irq(&pdata->
rtc->irq_lock);
253 static int mxc_rtc_alarm_irq_enable(
struct device *dev,
unsigned int enabled)
255 mxc_rtc_irq_enable(dev, RTC_ALM_BIT, enabled);
279 static int mxc_rtc_set_mmss(
struct device *dev,
unsigned long time)
326 ret = rtc_update_alarm(dev, &alrm->
time);
331 mxc_rtc_irq_enable(dev, RTC_ALM_BIT, alrm->
enabled);
338 .release = mxc_rtc_release,
339 .read_time = mxc_rtc_read_time,
340 .set_mmss = mxc_rtc_set_mmss,
341 .read_alarm = mxc_rtc_read_alarm,
342 .set_alarm = mxc_rtc_set_alarm,
343 .alarm_irq_enable = mxc_rtc_alarm_irq_enable,
364 resource_size(res), pdev->
name))
371 if (IS_ERR(pdata->
clk)) {
372 dev_err(&pdev->
dev,
"unable to get clock!\n");
373 ret = PTR_ERR(pdata->
clk);
374 goto exit_free_pdata;
377 clk_prepare_enable(pdata->
clk);
382 else if (rate == 32000)
384 else if (rate == 38400)
387 dev_err(&pdev->
dev,
"rtc clock is not valid (%lu)\n", rate);
395 dev_err(&pdev->
dev,
"hardware module can't be enabled!\n");
400 platform_set_drvdata(pdev, pdata);
405 if (pdata->
irq >= 0 &&
406 devm_request_irq(&pdev->
dev, pdata->
irq, mxc_rtc_interrupt,
408 dev_warn(&pdev->
dev,
"interrupt not available.\n");
419 goto exit_clr_drvdata;
427 platform_set_drvdata(pdev,
NULL);
429 clk_disable_unprepare(pdata->
clk);
442 clk_disable_unprepare(pdata->
clk);
443 platform_set_drvdata(pdev,
NULL);
449 static int mxc_rtc_suspend(
struct device *dev)
453 if (device_may_wakeup(dev))
454 enable_irq_wake(pdata->
irq);
459 static int mxc_rtc_resume(
struct device *dev)
463 if (device_may_wakeup(dev))
464 disable_irq_wake(pdata->
irq);
471 .resume = mxc_rtc_resume,
479 .pm = &mxc_rtc_pm_ops,
483 .probe = mxc_rtc_probe,