blob: 295dc6ddac6dc669d208b9e7233a94746634d929 [file] [log] [blame]
Kevin Wells9aa449b2010-10-27 15:33:01 -07001/*
2 * Copyright (C) 2010 NXP Semiconductors
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * You should have received a copy of the GNU General Public License along
10 * with this program; if not, write to the Free Software Foundation, Inc.,
11 * 675 Mass Ave, Cambridge, MA 02139, USA.
12 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/platform_device.h>
18#include <linux/spinlock.h>
19#include <linux/rtc.h>
20#include <linux/slab.h>
21#include <linux/io.h>
Roland Stiggee862e7c2012-05-29 15:07:39 -070022#include <linux/of.h>
Kevin Wells9aa449b2010-10-27 15:33:01 -070023
24/*
25 * Clock and Power control register offsets
26 */
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
35
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)
43
44#define LPC32XX_RTC_INTSTAT_MATCH0 (1 << 0)
45#define LPC32XX_RTC_INTSTAT_MATCH1 (1 << 1)
46#define LPC32XX_RTC_INTSTAT_ONSW (1 << 2)
47
48#define LPC32XX_RTC_KEY_ONSW_LOADVAL 0xB5C13F27
49
Kevin Wells9aa449b2010-10-27 15:33:01 -070050#define rtc_readl(dev, reg) \
51 __raw_readl((dev)->rtc_base + (reg))
52#define rtc_writel(dev, reg, val) \
53 __raw_writel((val), (dev)->rtc_base + (reg))
54
55struct lpc32xx_rtc {
56 void __iomem *rtc_base;
57 int irq;
58 unsigned char alarm_enabled;
59 struct rtc_device *rtc;
60 spinlock_t lock;
61};
62
63static int lpc32xx_rtc_read_time(struct device *dev, struct rtc_time *time)
64{
65 unsigned long elapsed_sec;
66 struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
67
68 elapsed_sec = rtc_readl(rtc, LPC32XX_RTC_UCOUNT);
69 rtc_time_to_tm(elapsed_sec, time);
70
Alexandre Belloniab626702018-02-19 16:23:55 +010071 return 0;
Kevin Wells9aa449b2010-10-27 15:33:01 -070072}
73
74static int lpc32xx_rtc_set_mmss(struct device *dev, unsigned long secs)
75{
76 struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
77 u32 tmp;
78
79 spin_lock_irq(&rtc->lock);
80
81 /* RTC must be disabled during count update */
82 tmp = rtc_readl(rtc, LPC32XX_RTC_CTRL);
83 rtc_writel(rtc, LPC32XX_RTC_CTRL, tmp | LPC32XX_RTC_CTRL_CNTR_DIS);
84 rtc_writel(rtc, LPC32XX_RTC_UCOUNT, secs);
85 rtc_writel(rtc, LPC32XX_RTC_DCOUNT, 0xFFFFFFFF - secs);
86 rtc_writel(rtc, LPC32XX_RTC_CTRL, tmp &= ~LPC32XX_RTC_CTRL_CNTR_DIS);
87
88 spin_unlock_irq(&rtc->lock);
89
90 return 0;
91}
92
93static int lpc32xx_rtc_read_alarm(struct device *dev,
94 struct rtc_wkalrm *wkalrm)
95{
96 struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
97
98 rtc_time_to_tm(rtc_readl(rtc, LPC32XX_RTC_MATCH0), &wkalrm->time);
99 wkalrm->enabled = rtc->alarm_enabled;
100 wkalrm->pending = !!(rtc_readl(rtc, LPC32XX_RTC_INTSTAT) &
101 LPC32XX_RTC_INTSTAT_MATCH0);
102
103 return rtc_valid_tm(&wkalrm->time);
104}
105
106static int lpc32xx_rtc_set_alarm(struct device *dev,
107 struct rtc_wkalrm *wkalrm)
108{
109 struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
110 unsigned long alarmsecs;
111 u32 tmp;
112 int ret;
113
114 ret = rtc_tm_to_time(&wkalrm->time, &alarmsecs);
115 if (ret < 0) {
116 dev_warn(dev, "Failed to convert time: %d\n", ret);
117 return ret;
118 }
119
120 spin_lock_irq(&rtc->lock);
121
122 /* Disable alarm during update */
123 tmp = rtc_readl(rtc, LPC32XX_RTC_CTRL);
124 rtc_writel(rtc, LPC32XX_RTC_CTRL, tmp & ~LPC32XX_RTC_CTRL_MATCH0);
125
126 rtc_writel(rtc, LPC32XX_RTC_MATCH0, alarmsecs);
127
128 rtc->alarm_enabled = wkalrm->enabled;
129 if (wkalrm->enabled) {
130 rtc_writel(rtc, LPC32XX_RTC_INTSTAT,
131 LPC32XX_RTC_INTSTAT_MATCH0);
132 rtc_writel(rtc, LPC32XX_RTC_CTRL, tmp |
133 LPC32XX_RTC_CTRL_MATCH0);
134 }
135
136 spin_unlock_irq(&rtc->lock);
137
138 return 0;
139}
140
141static int lpc32xx_rtc_alarm_irq_enable(struct device *dev,
142 unsigned int enabled)
143{
144 struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
145 u32 tmp;
146
147 spin_lock_irq(&rtc->lock);
148 tmp = rtc_readl(rtc, LPC32XX_RTC_CTRL);
149
150 if (enabled) {
151 rtc->alarm_enabled = 1;
152 tmp |= LPC32XX_RTC_CTRL_MATCH0;
153 } else {
154 rtc->alarm_enabled = 0;
155 tmp &= ~LPC32XX_RTC_CTRL_MATCH0;
156 }
157
158 rtc_writel(rtc, LPC32XX_RTC_CTRL, tmp);
159 spin_unlock_irq(&rtc->lock);
160
161 return 0;
162}
163
164static irqreturn_t lpc32xx_rtc_alarm_interrupt(int irq, void *dev)
165{
166 struct lpc32xx_rtc *rtc = dev;
167
168 spin_lock(&rtc->lock);
169
170 /* Disable alarm interrupt */
171 rtc_writel(rtc, LPC32XX_RTC_CTRL,
172 rtc_readl(rtc, LPC32XX_RTC_CTRL) &
173 ~LPC32XX_RTC_CTRL_MATCH0);
174 rtc->alarm_enabled = 0;
175
176 /*
177 * Write a large value to the match value so the RTC won't
178 * keep firing the match status
179 */
180 rtc_writel(rtc, LPC32XX_RTC_MATCH0, 0xFFFFFFFF);
181 rtc_writel(rtc, LPC32XX_RTC_INTSTAT, LPC32XX_RTC_INTSTAT_MATCH0);
182
183 spin_unlock(&rtc->lock);
184
185 rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF);
186
187 return IRQ_HANDLED;
188}
189
190static const struct rtc_class_ops lpc32xx_rtc_ops = {
191 .read_time = lpc32xx_rtc_read_time,
192 .set_mmss = lpc32xx_rtc_set_mmss,
193 .read_alarm = lpc32xx_rtc_read_alarm,
194 .set_alarm = lpc32xx_rtc_set_alarm,
195 .alarm_irq_enable = lpc32xx_rtc_alarm_irq_enable,
196};
197
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800198static int lpc32xx_rtc_probe(struct platform_device *pdev)
Kevin Wells9aa449b2010-10-27 15:33:01 -0700199{
200 struct resource *res;
201 struct lpc32xx_rtc *rtc;
Alexandre Belloni6bbad582019-04-09 12:16:26 +0200202 int rtcirq, err;
Kevin Wells9aa449b2010-10-27 15:33:01 -0700203 u32 tmp;
204
Kevin Wells9aa449b2010-10-27 15:33:01 -0700205 rtcirq = platform_get_irq(pdev, 0);
Vladimir Zapolskiy529af7d2015-12-02 08:10:28 +0200206 if (rtcirq < 0) {
Kevin Wells9aa449b2010-10-27 15:33:01 -0700207 dev_warn(&pdev->dev, "Can't get interrupt resource\n");
208 rtcirq = -1;
209 }
210
211 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
Jingoo Han5c336b02014-04-03 14:49:46 -0700212 if (unlikely(!rtc))
Kevin Wells9aa449b2010-10-27 15:33:01 -0700213 return -ENOMEM;
Jingoo Han5c336b02014-04-03 14:49:46 -0700214
Kevin Wells9aa449b2010-10-27 15:33:01 -0700215 rtc->irq = rtcirq;
216
Julia Lawall7c1d69e2013-09-11 14:24:27 -0700217 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
218 rtc->rtc_base = devm_ioremap_resource(&pdev->dev, res);
219 if (IS_ERR(rtc->rtc_base))
220 return PTR_ERR(rtc->rtc_base);
Kevin Wells9aa449b2010-10-27 15:33:01 -0700221
222 spin_lock_init(&rtc->lock);
223
224 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300225 * The RTC is on a separate power domain and can keep it's state
Kevin Wells9aa449b2010-10-27 15:33:01 -0700226 * across a chip power cycle. If the RTC has never been previously
227 * setup, then set it up now for the first time.
228 */
229 tmp = rtc_readl(rtc, LPC32XX_RTC_CTRL);
230 if (rtc_readl(rtc, LPC32XX_RTC_KEY) != LPC32XX_RTC_KEY_ONSW_LOADVAL) {
231 tmp &= ~(LPC32XX_RTC_CTRL_SW_RESET |
232 LPC32XX_RTC_CTRL_CNTR_DIS |
233 LPC32XX_RTC_CTRL_MATCH0 |
234 LPC32XX_RTC_CTRL_MATCH1 |
235 LPC32XX_RTC_CTRL_ONSW_MATCH0 |
236 LPC32XX_RTC_CTRL_ONSW_MATCH1 |
237 LPC32XX_RTC_CTRL_ONSW_FORCE_HI);
238 rtc_writel(rtc, LPC32XX_RTC_CTRL, tmp);
239
240 /* Clear latched interrupt states */
241 rtc_writel(rtc, LPC32XX_RTC_MATCH0, 0xFFFFFFFF);
242 rtc_writel(rtc, LPC32XX_RTC_INTSTAT,
243 LPC32XX_RTC_INTSTAT_MATCH0 |
244 LPC32XX_RTC_INTSTAT_MATCH1 |
245 LPC32XX_RTC_INTSTAT_ONSW);
246
247 /* Write key value to RTC so it won't reload on reset */
248 rtc_writel(rtc, LPC32XX_RTC_KEY,
249 LPC32XX_RTC_KEY_ONSW_LOADVAL);
250 } else {
251 rtc_writel(rtc, LPC32XX_RTC_CTRL,
252 tmp & ~LPC32XX_RTC_CTRL_MATCH0);
253 }
254
255 platform_set_drvdata(pdev, rtc);
256
Alexandre Belloni6bbad582019-04-09 12:16:26 +0200257 rtc->rtc = devm_rtc_allocate_device(&pdev->dev);
258 if (IS_ERR(rtc->rtc))
Kevin Wells9aa449b2010-10-27 15:33:01 -0700259 return PTR_ERR(rtc->rtc);
Alexandre Belloni6bbad582019-04-09 12:16:26 +0200260
261 rtc->rtc->ops = &lpc32xx_rtc_ops;
262
263 err = rtc_register_device(rtc->rtc);
264 if (err)
265 return err;
Kevin Wells9aa449b2010-10-27 15:33:01 -0700266
267 /*
268 * IRQ is enabled after device registration in case alarm IRQ
269 * is pending upon suspend exit.
270 */
271 if (rtc->irq >= 0) {
272 if (devm_request_irq(&pdev->dev, rtc->irq,
273 lpc32xx_rtc_alarm_interrupt,
Yong Zhang2f6e5f92012-03-23 15:02:34 -0700274 0, pdev->name, rtc) < 0) {
Kevin Wells9aa449b2010-10-27 15:33:01 -0700275 dev_warn(&pdev->dev, "Can't request interrupt.\n");
276 rtc->irq = -1;
277 } else {
278 device_init_wakeup(&pdev->dev, 1);
279 }
280 }
281
282 return 0;
283}
284
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800285static int lpc32xx_rtc_remove(struct platform_device *pdev)
Kevin Wells9aa449b2010-10-27 15:33:01 -0700286{
287 struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
288
289 if (rtc->irq >= 0)
290 device_init_wakeup(&pdev->dev, 0);
291
Kevin Wells9aa449b2010-10-27 15:33:01 -0700292 return 0;
293}
294
295#ifdef CONFIG_PM
296static int lpc32xx_rtc_suspend(struct device *dev)
297{
Wolfram Sang85368bb2018-04-19 16:06:14 +0200298 struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
Kevin Wells9aa449b2010-10-27 15:33:01 -0700299
300 if (rtc->irq >= 0) {
Wolfram Sang85368bb2018-04-19 16:06:14 +0200301 if (device_may_wakeup(dev))
Kevin Wells9aa449b2010-10-27 15:33:01 -0700302 enable_irq_wake(rtc->irq);
303 else
304 disable_irq_wake(rtc->irq);
305 }
306
307 return 0;
308}
309
310static int lpc32xx_rtc_resume(struct device *dev)
311{
Wolfram Sang85368bb2018-04-19 16:06:14 +0200312 struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
Kevin Wells9aa449b2010-10-27 15:33:01 -0700313
Wolfram Sang85368bb2018-04-19 16:06:14 +0200314 if (rtc->irq >= 0 && device_may_wakeup(dev))
Kevin Wells9aa449b2010-10-27 15:33:01 -0700315 disable_irq_wake(rtc->irq);
316
317 return 0;
318}
319
320/* Unconditionally disable the alarm */
321static int lpc32xx_rtc_freeze(struct device *dev)
322{
Wolfram Sang85368bb2018-04-19 16:06:14 +0200323 struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
Kevin Wells9aa449b2010-10-27 15:33:01 -0700324
325 spin_lock_irq(&rtc->lock);
326
327 rtc_writel(rtc, LPC32XX_RTC_CTRL,
328 rtc_readl(rtc, LPC32XX_RTC_CTRL) &
329 ~LPC32XX_RTC_CTRL_MATCH0);
330
331 spin_unlock_irq(&rtc->lock);
332
333 return 0;
334}
335
336static int lpc32xx_rtc_thaw(struct device *dev)
337{
Wolfram Sang85368bb2018-04-19 16:06:14 +0200338 struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
Kevin Wells9aa449b2010-10-27 15:33:01 -0700339
340 if (rtc->alarm_enabled) {
341 spin_lock_irq(&rtc->lock);
342
343 rtc_writel(rtc, LPC32XX_RTC_CTRL,
344 rtc_readl(rtc, LPC32XX_RTC_CTRL) |
345 LPC32XX_RTC_CTRL_MATCH0);
346
347 spin_unlock_irq(&rtc->lock);
348 }
349
350 return 0;
351}
352
353static const struct dev_pm_ops lpc32xx_rtc_pm_ops = {
354 .suspend = lpc32xx_rtc_suspend,
355 .resume = lpc32xx_rtc_resume,
356 .freeze = lpc32xx_rtc_freeze,
357 .thaw = lpc32xx_rtc_thaw,
358 .restore = lpc32xx_rtc_resume
359};
360
361#define LPC32XX_RTC_PM_OPS (&lpc32xx_rtc_pm_ops)
362#else
363#define LPC32XX_RTC_PM_OPS NULL
364#endif
365
Roland Stiggee862e7c2012-05-29 15:07:39 -0700366#ifdef CONFIG_OF
367static const struct of_device_id lpc32xx_rtc_match[] = {
368 { .compatible = "nxp,lpc3220-rtc" },
369 { }
370};
371MODULE_DEVICE_TABLE(of, lpc32xx_rtc_match);
372#endif
373
Kevin Wells9aa449b2010-10-27 15:33:01 -0700374static struct platform_driver lpc32xx_rtc_driver = {
375 .probe = lpc32xx_rtc_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800376 .remove = lpc32xx_rtc_remove,
Kevin Wells9aa449b2010-10-27 15:33:01 -0700377 .driver = {
Alexandre Belloni6bbad582019-04-09 12:16:26 +0200378 .name = "rtc-lpc32xx",
Roland Stiggee862e7c2012-05-29 15:07:39 -0700379 .pm = LPC32XX_RTC_PM_OPS,
380 .of_match_table = of_match_ptr(lpc32xx_rtc_match),
Kevin Wells9aa449b2010-10-27 15:33:01 -0700381 },
382};
383
Axel Lin0c4eae62012-01-10 15:10:48 -0800384module_platform_driver(lpc32xx_rtc_driver);
Kevin Wells9aa449b2010-10-27 15:33:01 -0700385
386MODULE_AUTHOR("Kevin Wells <wellsk40@gmail.com");
387MODULE_DESCRIPTION("RTC driver for the LPC32xx SoC");
388MODULE_LICENSE("GPL");
389MODULE_ALIAS("platform:rtc-lpc32xx");