14 #include <linux/kernel.h>
15 #include <linux/module.h>
19 #include <linux/rtc.h>
20 #include <linux/slab.h>
27 #define LPC32XX_RTC_UCOUNT 0x00
28 #define LPC32XX_RTC_DCOUNT 0x04
29 #define LPC32XX_RTC_MATCH0 0x08
30 #define LPC32XX_RTC_MATCH1 0x0C
31 #define LPC32XX_RTC_CTRL 0x10
32 #define LPC32XX_RTC_INTSTAT 0x14
33 #define LPC32XX_RTC_KEY 0x18
34 #define LPC32XX_RTC_SRAM 0x80
36 #define LPC32XX_RTC_CTRL_MATCH0 (1 << 0)
37 #define LPC32XX_RTC_CTRL_MATCH1 (1 << 1)
38 #define LPC32XX_RTC_CTRL_ONSW_MATCH0 (1 << 2)
39 #define LPC32XX_RTC_CTRL_ONSW_MATCH1 (1 << 3)
40 #define LPC32XX_RTC_CTRL_SW_RESET (1 << 4)
41 #define LPC32XX_RTC_CTRL_CNTR_DIS (1 << 6)
42 #define LPC32XX_RTC_CTRL_ONSW_FORCE_HI (1 << 7)
44 #define LPC32XX_RTC_INTSTAT_MATCH0 (1 << 0)
45 #define LPC32XX_RTC_INTSTAT_MATCH1 (1 << 1)
46 #define LPC32XX_RTC_INTSTAT_ONSW (1 << 2)
48 #define LPC32XX_RTC_KEY_ONSW_LOADVAL 0xB5C13F27
50 #define RTC_NAME "rtc-lpc32xx"
52 #define rtc_readl(dev, reg) \
53 __raw_readl((dev)->rtc_base + (reg))
54 #define rtc_writel(dev, reg, val) \
55 __raw_writel((val), (dev)->rtc_base + (reg))
67 unsigned long elapsed_sec;
76 static int lpc32xx_rtc_set_mmss(
struct device *
dev,
unsigned long secs)
81 spin_lock_irq(&rtc->
lock);
90 spin_unlock_irq(&rtc->
lock);
95 static int lpc32xx_rtc_read_alarm(
struct device *
dev,
108 static int lpc32xx_rtc_set_alarm(
struct device *dev,
112 unsigned long alarmsecs;
118 dev_warn(dev,
"Failed to convert time: %d\n", ret);
122 spin_lock_irq(&rtc->
lock);
138 spin_unlock_irq(&rtc->
lock);
143 static int lpc32xx_rtc_alarm_irq_enable(
struct device *dev,
149 spin_lock_irq(&rtc->
lock);
161 spin_unlock_irq(&rtc->
lock);
166 static irqreturn_t lpc32xx_rtc_alarm_interrupt(
int irq,
void *dev)
170 spin_lock(&rtc->
lock);
185 spin_unlock(&rtc->
lock);
193 .read_time = lpc32xx_rtc_read_time,
194 .set_mmss = lpc32xx_rtc_set_mmss,
195 .read_alarm = lpc32xx_rtc_read_alarm,
196 .set_alarm = lpc32xx_rtc_set_alarm,
197 .alarm_irq_enable = lpc32xx_rtc_alarm_irq_enable,
210 dev_err(&pdev->
dev,
"Can't get memory resource\n");
215 if (rtcirq < 0 || rtcirq >=
NR_IRQS) {
216 dev_warn(&pdev->
dev,
"Can't get interrupt resource\n");
222 dev_err(&pdev->
dev,
"Can't allocate memory\n");
227 size = resource_size(res);
231 dev_err(&pdev->
dev,
"RTC registers are not free\n");
274 platform_set_drvdata(pdev, rtc);
278 if (IS_ERR(rtc->
rtc)) {
280 platform_set_drvdata(pdev,
NULL);
281 return PTR_ERR(rtc->
rtc);
289 if (devm_request_irq(&pdev->
dev, rtc->
irq,
290 lpc32xx_rtc_alarm_interrupt,
291 0, pdev->
name, rtc) < 0) {
292 dev_warn(&pdev->
dev,
"Can't request interrupt.\n");
304 struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
309 platform_set_drvdata(pdev,
NULL);
316 static int lpc32xx_rtc_suspend(
struct device *dev)
319 struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
322 if (device_may_wakeup(&pdev->
dev))
323 enable_irq_wake(rtc->
irq);
325 disable_irq_wake(rtc->
irq);
331 static int lpc32xx_rtc_resume(
struct device *dev)
334 struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
336 if (rtc->
irq >= 0 && device_may_wakeup(&pdev->
dev))
337 disable_irq_wake(rtc->
irq);
343 static int lpc32xx_rtc_freeze(
struct device *dev)
346 struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
348 spin_lock_irq(&rtc->
lock);
354 spin_unlock_irq(&rtc->
lock);
359 static int lpc32xx_rtc_thaw(
struct device *dev)
362 struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
365 spin_lock_irq(&rtc->
lock);
371 spin_unlock_irq(&rtc->
lock);
377 static const struct dev_pm_ops lpc32xx_rtc_pm_ops = {
378 .
suspend = lpc32xx_rtc_suspend,
379 .resume = lpc32xx_rtc_resume,
380 .freeze = lpc32xx_rtc_freeze,
381 .thaw = lpc32xx_rtc_thaw,
382 .restore = lpc32xx_rtc_resume
385 #define LPC32XX_RTC_PM_OPS (&lpc32xx_rtc_pm_ops)
387 #define LPC32XX_RTC_PM_OPS NULL
391 static const struct of_device_id lpc32xx_rtc_match[] = {
399 .probe = lpc32xx_rtc_probe,