1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2010 NXP Semiconductors
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/platform_device.h>
10 #include <linux/spinlock.h>
11 #include <linux/rtc.h>
12 #include <linux/slab.h>
17 * Clock and Power control register offsets
19 #define LPC32XX_RTC_UCOUNT 0x00
20 #define LPC32XX_RTC_DCOUNT 0x04
21 #define LPC32XX_RTC_MATCH0 0x08
22 #define LPC32XX_RTC_MATCH1 0x0C
23 #define LPC32XX_RTC_CTRL 0x10
24 #define LPC32XX_RTC_INTSTAT 0x14
25 #define LPC32XX_RTC_KEY 0x18
26 #define LPC32XX_RTC_SRAM 0x80
28 #define LPC32XX_RTC_CTRL_MATCH0 (1 << 0)
29 #define LPC32XX_RTC_CTRL_MATCH1 (1 << 1)
30 #define LPC32XX_RTC_CTRL_ONSW_MATCH0 (1 << 2)
31 #define LPC32XX_RTC_CTRL_ONSW_MATCH1 (1 << 3)
32 #define LPC32XX_RTC_CTRL_SW_RESET (1 << 4)
33 #define LPC32XX_RTC_CTRL_CNTR_DIS (1 << 6)
34 #define LPC32XX_RTC_CTRL_ONSW_FORCE_HI (1 << 7)
36 #define LPC32XX_RTC_INTSTAT_MATCH0 (1 << 0)
37 #define LPC32XX_RTC_INTSTAT_MATCH1 (1 << 1)
38 #define LPC32XX_RTC_INTSTAT_ONSW (1 << 2)
40 #define LPC32XX_RTC_KEY_ONSW_LOADVAL 0xB5C13F27
42 #define rtc_readl(dev, reg) \
43 __raw_readl((dev)->rtc_base + (reg))
44 #define rtc_writel(dev, reg, val) \
45 __raw_writel((val), (dev)->rtc_base + (reg))
48 void __iomem
*rtc_base
;
50 unsigned char alarm_enabled
;
51 struct rtc_device
*rtc
;
55 static int lpc32xx_rtc_read_time(struct device
*dev
, struct rtc_time
*time
)
57 unsigned long elapsed_sec
;
58 struct lpc32xx_rtc
*rtc
= dev_get_drvdata(dev
);
60 elapsed_sec
= rtc_readl(rtc
, LPC32XX_RTC_UCOUNT
);
61 rtc_time64_to_tm(elapsed_sec
, time
);
66 static int lpc32xx_rtc_set_time(struct device
*dev
, struct rtc_time
*time
)
68 struct lpc32xx_rtc
*rtc
= dev_get_drvdata(dev
);
69 u32 secs
= rtc_tm_to_time64(time
);
72 spin_lock_irq(&rtc
->lock
);
74 /* RTC must be disabled during count update */
75 tmp
= rtc_readl(rtc
, LPC32XX_RTC_CTRL
);
76 rtc_writel(rtc
, LPC32XX_RTC_CTRL
, tmp
| LPC32XX_RTC_CTRL_CNTR_DIS
);
77 rtc_writel(rtc
, LPC32XX_RTC_UCOUNT
, secs
);
78 rtc_writel(rtc
, LPC32XX_RTC_DCOUNT
, 0xFFFFFFFF - secs
);
79 rtc_writel(rtc
, LPC32XX_RTC_CTRL
, tmp
&= ~LPC32XX_RTC_CTRL_CNTR_DIS
);
81 spin_unlock_irq(&rtc
->lock
);
86 static int lpc32xx_rtc_read_alarm(struct device
*dev
,
87 struct rtc_wkalrm
*wkalrm
)
89 struct lpc32xx_rtc
*rtc
= dev_get_drvdata(dev
);
91 rtc_time64_to_tm(rtc_readl(rtc
, LPC32XX_RTC_MATCH0
), &wkalrm
->time
);
92 wkalrm
->enabled
= rtc
->alarm_enabled
;
93 wkalrm
->pending
= !!(rtc_readl(rtc
, LPC32XX_RTC_INTSTAT
) &
94 LPC32XX_RTC_INTSTAT_MATCH0
);
96 return rtc_valid_tm(&wkalrm
->time
);
99 static int lpc32xx_rtc_set_alarm(struct device
*dev
,
100 struct rtc_wkalrm
*wkalrm
)
102 struct lpc32xx_rtc
*rtc
= dev_get_drvdata(dev
);
103 unsigned long alarmsecs
;
106 alarmsecs
= rtc_tm_to_time64(&wkalrm
->time
);
108 spin_lock_irq(&rtc
->lock
);
110 /* Disable alarm during update */
111 tmp
= rtc_readl(rtc
, LPC32XX_RTC_CTRL
);
112 rtc_writel(rtc
, LPC32XX_RTC_CTRL
, tmp
& ~LPC32XX_RTC_CTRL_MATCH0
);
114 rtc_writel(rtc
, LPC32XX_RTC_MATCH0
, alarmsecs
);
116 rtc
->alarm_enabled
= wkalrm
->enabled
;
117 if (wkalrm
->enabled
) {
118 rtc_writel(rtc
, LPC32XX_RTC_INTSTAT
,
119 LPC32XX_RTC_INTSTAT_MATCH0
);
120 rtc_writel(rtc
, LPC32XX_RTC_CTRL
, tmp
|
121 LPC32XX_RTC_CTRL_MATCH0
);
124 spin_unlock_irq(&rtc
->lock
);
129 static int lpc32xx_rtc_alarm_irq_enable(struct device
*dev
,
130 unsigned int enabled
)
132 struct lpc32xx_rtc
*rtc
= dev_get_drvdata(dev
);
135 spin_lock_irq(&rtc
->lock
);
136 tmp
= rtc_readl(rtc
, LPC32XX_RTC_CTRL
);
139 rtc
->alarm_enabled
= 1;
140 tmp
|= LPC32XX_RTC_CTRL_MATCH0
;
142 rtc
->alarm_enabled
= 0;
143 tmp
&= ~LPC32XX_RTC_CTRL_MATCH0
;
146 rtc_writel(rtc
, LPC32XX_RTC_CTRL
, tmp
);
147 spin_unlock_irq(&rtc
->lock
);
152 static irqreturn_t
lpc32xx_rtc_alarm_interrupt(int irq
, void *dev
)
154 struct lpc32xx_rtc
*rtc
= dev
;
156 spin_lock(&rtc
->lock
);
158 /* Disable alarm interrupt */
159 rtc_writel(rtc
, LPC32XX_RTC_CTRL
,
160 rtc_readl(rtc
, LPC32XX_RTC_CTRL
) &
161 ~LPC32XX_RTC_CTRL_MATCH0
);
162 rtc
->alarm_enabled
= 0;
165 * Write a large value to the match value so the RTC won't
166 * keep firing the match status
168 rtc_writel(rtc
, LPC32XX_RTC_MATCH0
, 0xFFFFFFFF);
169 rtc_writel(rtc
, LPC32XX_RTC_INTSTAT
, LPC32XX_RTC_INTSTAT_MATCH0
);
171 spin_unlock(&rtc
->lock
);
173 rtc_update_irq(rtc
->rtc
, 1, RTC_IRQF
| RTC_AF
);
178 static const struct rtc_class_ops lpc32xx_rtc_ops
= {
179 .read_time
= lpc32xx_rtc_read_time
,
180 .set_time
= lpc32xx_rtc_set_time
,
181 .read_alarm
= lpc32xx_rtc_read_alarm
,
182 .set_alarm
= lpc32xx_rtc_set_alarm
,
183 .alarm_irq_enable
= lpc32xx_rtc_alarm_irq_enable
,
186 static int lpc32xx_rtc_probe(struct platform_device
*pdev
)
188 struct resource
*res
;
189 struct lpc32xx_rtc
*rtc
;
193 rtc
= devm_kzalloc(&pdev
->dev
, sizeof(*rtc
), GFP_KERNEL
);
197 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
198 rtc
->rtc_base
= devm_ioremap_resource(&pdev
->dev
, res
);
199 if (IS_ERR(rtc
->rtc_base
))
200 return PTR_ERR(rtc
->rtc_base
);
202 spin_lock_init(&rtc
->lock
);
205 * The RTC is on a separate power domain and can keep it's state
206 * across a chip power cycle. If the RTC has never been previously
207 * setup, then set it up now for the first time.
209 tmp
= rtc_readl(rtc
, LPC32XX_RTC_CTRL
);
210 if (rtc_readl(rtc
, LPC32XX_RTC_KEY
) != LPC32XX_RTC_KEY_ONSW_LOADVAL
) {
211 tmp
&= ~(LPC32XX_RTC_CTRL_SW_RESET
|
212 LPC32XX_RTC_CTRL_CNTR_DIS
|
213 LPC32XX_RTC_CTRL_MATCH0
|
214 LPC32XX_RTC_CTRL_MATCH1
|
215 LPC32XX_RTC_CTRL_ONSW_MATCH0
|
216 LPC32XX_RTC_CTRL_ONSW_MATCH1
|
217 LPC32XX_RTC_CTRL_ONSW_FORCE_HI
);
218 rtc_writel(rtc
, LPC32XX_RTC_CTRL
, tmp
);
220 /* Clear latched interrupt states */
221 rtc_writel(rtc
, LPC32XX_RTC_MATCH0
, 0xFFFFFFFF);
222 rtc_writel(rtc
, LPC32XX_RTC_INTSTAT
,
223 LPC32XX_RTC_INTSTAT_MATCH0
|
224 LPC32XX_RTC_INTSTAT_MATCH1
|
225 LPC32XX_RTC_INTSTAT_ONSW
);
227 /* Write key value to RTC so it won't reload on reset */
228 rtc_writel(rtc
, LPC32XX_RTC_KEY
,
229 LPC32XX_RTC_KEY_ONSW_LOADVAL
);
231 rtc_writel(rtc
, LPC32XX_RTC_CTRL
,
232 tmp
& ~LPC32XX_RTC_CTRL_MATCH0
);
235 platform_set_drvdata(pdev
, rtc
);
237 rtc
->rtc
= devm_rtc_allocate_device(&pdev
->dev
);
238 if (IS_ERR(rtc
->rtc
))
239 return PTR_ERR(rtc
->rtc
);
241 rtc
->rtc
->ops
= &lpc32xx_rtc_ops
;
242 rtc
->rtc
->range_max
= U32_MAX
;
244 err
= rtc_register_device(rtc
->rtc
);
249 * IRQ is enabled after device registration in case alarm IRQ
250 * is pending upon suspend exit.
252 rtc
->irq
= platform_get_irq(pdev
, 0);
254 dev_warn(&pdev
->dev
, "Can't get interrupt resource\n");
256 if (devm_request_irq(&pdev
->dev
, rtc
->irq
,
257 lpc32xx_rtc_alarm_interrupt
,
258 0, pdev
->name
, rtc
) < 0) {
259 dev_warn(&pdev
->dev
, "Can't request interrupt.\n");
262 device_init_wakeup(&pdev
->dev
, 1);
269 static int lpc32xx_rtc_remove(struct platform_device
*pdev
)
271 struct lpc32xx_rtc
*rtc
= platform_get_drvdata(pdev
);
274 device_init_wakeup(&pdev
->dev
, 0);
280 static int lpc32xx_rtc_suspend(struct device
*dev
)
282 struct lpc32xx_rtc
*rtc
= dev_get_drvdata(dev
);
285 if (device_may_wakeup(dev
))
286 enable_irq_wake(rtc
->irq
);
288 disable_irq_wake(rtc
->irq
);
294 static int lpc32xx_rtc_resume(struct device
*dev
)
296 struct lpc32xx_rtc
*rtc
= dev_get_drvdata(dev
);
298 if (rtc
->irq
>= 0 && device_may_wakeup(dev
))
299 disable_irq_wake(rtc
->irq
);
304 /* Unconditionally disable the alarm */
305 static int lpc32xx_rtc_freeze(struct device
*dev
)
307 struct lpc32xx_rtc
*rtc
= dev_get_drvdata(dev
);
309 spin_lock_irq(&rtc
->lock
);
311 rtc_writel(rtc
, LPC32XX_RTC_CTRL
,
312 rtc_readl(rtc
, LPC32XX_RTC_CTRL
) &
313 ~LPC32XX_RTC_CTRL_MATCH0
);
315 spin_unlock_irq(&rtc
->lock
);
320 static int lpc32xx_rtc_thaw(struct device
*dev
)
322 struct lpc32xx_rtc
*rtc
= dev_get_drvdata(dev
);
324 if (rtc
->alarm_enabled
) {
325 spin_lock_irq(&rtc
->lock
);
327 rtc_writel(rtc
, LPC32XX_RTC_CTRL
,
328 rtc_readl(rtc
, LPC32XX_RTC_CTRL
) |
329 LPC32XX_RTC_CTRL_MATCH0
);
331 spin_unlock_irq(&rtc
->lock
);
337 static const struct dev_pm_ops lpc32xx_rtc_pm_ops
= {
338 .suspend
= lpc32xx_rtc_suspend
,
339 .resume
= lpc32xx_rtc_resume
,
340 .freeze
= lpc32xx_rtc_freeze
,
341 .thaw
= lpc32xx_rtc_thaw
,
342 .restore
= lpc32xx_rtc_resume
345 #define LPC32XX_RTC_PM_OPS (&lpc32xx_rtc_pm_ops)
347 #define LPC32XX_RTC_PM_OPS NULL
351 static const struct of_device_id lpc32xx_rtc_match
[] = {
352 { .compatible
= "nxp,lpc3220-rtc" },
355 MODULE_DEVICE_TABLE(of
, lpc32xx_rtc_match
);
358 static struct platform_driver lpc32xx_rtc_driver
= {
359 .probe
= lpc32xx_rtc_probe
,
360 .remove
= lpc32xx_rtc_remove
,
362 .name
= "rtc-lpc32xx",
363 .pm
= LPC32XX_RTC_PM_OPS
,
364 .of_match_table
= of_match_ptr(lpc32xx_rtc_match
),
368 module_platform_driver(lpc32xx_rtc_driver
);
370 MODULE_AUTHOR("Kevin Wells <wellsk40@gmail.com");
371 MODULE_DESCRIPTION("RTC driver for the LPC32xx SoC");
372 MODULE_LICENSE("GPL");
373 MODULE_ALIAS("platform:rtc-lpc32xx");