17 #include <linux/kernel.h>
18 #include <linux/module.h>
20 #include <linux/rtc.h>
21 #include <linux/slab.h>
24 #define JZ_REG_RTC_CTRL 0x00
25 #define JZ_REG_RTC_SEC 0x04
26 #define JZ_REG_RTC_SEC_ALARM 0x08
27 #define JZ_REG_RTC_REGULATOR 0x0C
28 #define JZ_REG_RTC_HIBERNATE 0x20
29 #define JZ_REG_RTC_SCRATCHPAD 0x34
31 #define JZ_RTC_CTRL_WRDY BIT(7)
32 #define JZ_RTC_CTRL_1HZ BIT(6)
33 #define JZ_RTC_CTRL_1HZ_IRQ BIT(5)
34 #define JZ_RTC_CTRL_AF BIT(4)
35 #define JZ_RTC_CTRL_AF_IRQ BIT(3)
36 #define JZ_RTC_CTRL_AE BIT(2)
37 #define JZ_RTC_CTRL_ENABLE BIT(0)
55 static int jz4740_rtc_wait_write_ready(
struct jz4740_rtc *
rtc)
64 return timeout ? 0 : -
EIO;
67 static inline int jz4740_rtc_reg_write(
struct jz4740_rtc *rtc,
size_t reg,
71 ret = jz4740_rtc_wait_write_ready(rtc);
99 spin_unlock_irqrestore(&rtc->
lock, flags);
117 while (secs != secs2 && --timeout) {
130 static int jz4740_rtc_set_mmss(
struct device *dev,
unsigned long secs)
137 static int jz4740_rtc_read_alarm(
struct device *dev,
struct rtc_wkalrm *alrm)
155 static int jz4740_rtc_set_alarm(
struct device *dev,
struct rtc_wkalrm *alrm)
165 ret = jz4740_rtc_ctrl_set_bits(rtc,
171 static int jz4740_rtc_alarm_irq_enable(
struct device *dev,
unsigned int enable)
178 .read_time = jz4740_rtc_read_time,
179 .set_mmss = jz4740_rtc_set_mmss,
180 .read_alarm = jz4740_rtc_read_alarm,
181 .set_alarm = jz4740_rtc_set_alarm,
182 .alarm_irq_enable = jz4740_rtc_alarm_irq_enable,
201 jz4740_rtc_ctrl_set_bits(rtc, JZ_RTC_CTRL_1HZ | JZ_RTC_CTRL_AF,
false);
226 dev_err(&pdev->
dev,
"Failed to get platform irq\n");
233 dev_err(&pdev->
dev,
"Failed to get platform mmio memory\n");
241 dev_err(&pdev->
dev,
"Failed to request mmio memory region\n");
248 dev_err(&pdev->
dev,
"Failed to ioremap mmio memory\n");
249 goto err_release_mem_region;
254 platform_set_drvdata(pdev, rtc);
260 if (IS_ERR(rtc->
rtc)) {
261 ret = PTR_ERR(rtc->
rtc);
262 dev_err(&pdev->
dev,
"Failed to register rtc device: %d\n", ret);
269 dev_err(&pdev->
dev,
"Failed to request rtc irq: %d\n", ret);
270 goto err_unregister_rtc;
274 if (scratchpad != 0x12345678) {
278 dev_err(&pdev->
dev,
"Could not write write to RTC registers\n");
290 platform_set_drvdata(pdev,
NULL);
292 err_release_mem_region:
302 struct jz4740_rtc *rtc = platform_get_drvdata(pdev);
313 platform_set_drvdata(pdev,
NULL);
320 static int jz4740_rtc_suspend(
struct device *dev)
324 if (device_may_wakeup(dev))
325 enable_irq_wake(rtc->
irq);
329 static int jz4740_rtc_resume(
struct device *dev)
333 if (device_may_wakeup(dev))
334 disable_irq_wake(rtc->
irq);
338 static const struct dev_pm_ops jz4740_pm_ops = {
340 .resume = jz4740_rtc_resume,
342 #define JZ4740_RTC_PM_OPS (&jz4740_pm_ops)
345 #define JZ4740_RTC_PM_OPS NULL
349 .probe = jz4740_rtc_probe,
352 .name =
"jz4740-rtc",