blob: 652b969158d49ddc22fa1853dc800b86dae31c1e [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 Belloniba4a84f2019-04-09 12:16:28 +0200202 int err;
Kevin Wells9aa449b2010-10-27 15:33:01 -0700203 u32 tmp;
204
Kevin Wells9aa449b2010-10-27 15:33:01 -0700205 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
Jingoo Han5c336b02014-04-03 14:49:46 -0700206 if (unlikely(!rtc))
Kevin Wells9aa449b2010-10-27 15:33:01 -0700207 return -ENOMEM;
Jingoo Han5c336b02014-04-03 14:49:46 -0700208
Julia Lawall7c1d69e2013-09-11 14:24:27 -0700209 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
210 rtc->rtc_base = devm_ioremap_resource(&pdev->dev, res);
211 if (IS_ERR(rtc->rtc_base))
212 return PTR_ERR(rtc->rtc_base);
Kevin Wells9aa449b2010-10-27 15:33:01 -0700213
214 spin_lock_init(&rtc->lock);
215
216 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300217 * The RTC is on a separate power domain and can keep it's state
Kevin Wells9aa449b2010-10-27 15:33:01 -0700218 * across a chip power cycle. If the RTC has never been previously
219 * setup, then set it up now for the first time.
220 */
221 tmp = rtc_readl(rtc, LPC32XX_RTC_CTRL);
222 if (rtc_readl(rtc, LPC32XX_RTC_KEY) != LPC32XX_RTC_KEY_ONSW_LOADVAL) {
223 tmp &= ~(LPC32XX_RTC_CTRL_SW_RESET |
224 LPC32XX_RTC_CTRL_CNTR_DIS |
225 LPC32XX_RTC_CTRL_MATCH0 |
226 LPC32XX_RTC_CTRL_MATCH1 |
227 LPC32XX_RTC_CTRL_ONSW_MATCH0 |
228 LPC32XX_RTC_CTRL_ONSW_MATCH1 |
229 LPC32XX_RTC_CTRL_ONSW_FORCE_HI);
230 rtc_writel(rtc, LPC32XX_RTC_CTRL, tmp);
231
232 /* Clear latched interrupt states */
233 rtc_writel(rtc, LPC32XX_RTC_MATCH0, 0xFFFFFFFF);
234 rtc_writel(rtc, LPC32XX_RTC_INTSTAT,
235 LPC32XX_RTC_INTSTAT_MATCH0 |
236 LPC32XX_RTC_INTSTAT_MATCH1 |
237 LPC32XX_RTC_INTSTAT_ONSW);
238
239 /* Write key value to RTC so it won't reload on reset */
240 rtc_writel(rtc, LPC32XX_RTC_KEY,
241 LPC32XX_RTC_KEY_ONSW_LOADVAL);
242 } else {
243 rtc_writel(rtc, LPC32XX_RTC_CTRL,
244 tmp & ~LPC32XX_RTC_CTRL_MATCH0);
245 }
246
247 platform_set_drvdata(pdev, rtc);
248
Alexandre Belloni6bbad582019-04-09 12:16:26 +0200249 rtc->rtc = devm_rtc_allocate_device(&pdev->dev);
250 if (IS_ERR(rtc->rtc))
Kevin Wells9aa449b2010-10-27 15:33:01 -0700251 return PTR_ERR(rtc->rtc);
Alexandre Belloni6bbad582019-04-09 12:16:26 +0200252
253 rtc->rtc->ops = &lpc32xx_rtc_ops;
Alexandre Belloni3a134262019-04-09 12:16:27 +0200254 rtc->rtc->range_max = U32_MAX;
Alexandre Belloni6bbad582019-04-09 12:16:26 +0200255
256 err = rtc_register_device(rtc->rtc);
257 if (err)
258 return err;
Kevin Wells9aa449b2010-10-27 15:33:01 -0700259
260 /*
261 * IRQ is enabled after device registration in case alarm IRQ
262 * is pending upon suspend exit.
263 */
Alexandre Belloniba4a84f2019-04-09 12:16:28 +0200264 rtc->irq = platform_get_irq(pdev, 0);
265 if (rtc->irq < 0) {
266 dev_warn(&pdev->dev, "Can't get interrupt resource\n");
267 } else {
Kevin Wells9aa449b2010-10-27 15:33:01 -0700268 if (devm_request_irq(&pdev->dev, rtc->irq,
269 lpc32xx_rtc_alarm_interrupt,
Yong Zhang2f6e5f92012-03-23 15:02:34 -0700270 0, pdev->name, rtc) < 0) {
Kevin Wells9aa449b2010-10-27 15:33:01 -0700271 dev_warn(&pdev->dev, "Can't request interrupt.\n");
272 rtc->irq = -1;
273 } else {
274 device_init_wakeup(&pdev->dev, 1);
275 }
276 }
277
278 return 0;
279}
280
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800281static int lpc32xx_rtc_remove(struct platform_device *pdev)
Kevin Wells9aa449b2010-10-27 15:33:01 -0700282{
283 struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
284
285 if (rtc->irq >= 0)
286 device_init_wakeup(&pdev->dev, 0);
287
Kevin Wells9aa449b2010-10-27 15:33:01 -0700288 return 0;
289}
290
291#ifdef CONFIG_PM
292static int lpc32xx_rtc_suspend(struct device *dev)
293{
Wolfram Sang85368bb2018-04-19 16:06:14 +0200294 struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
Kevin Wells9aa449b2010-10-27 15:33:01 -0700295
296 if (rtc->irq >= 0) {
Wolfram Sang85368bb2018-04-19 16:06:14 +0200297 if (device_may_wakeup(dev))
Kevin Wells9aa449b2010-10-27 15:33:01 -0700298 enable_irq_wake(rtc->irq);
299 else
300 disable_irq_wake(rtc->irq);
301 }
302
303 return 0;
304}
305
306static int lpc32xx_rtc_resume(struct device *dev)
307{
Wolfram Sang85368bb2018-04-19 16:06:14 +0200308 struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
Kevin Wells9aa449b2010-10-27 15:33:01 -0700309
Wolfram Sang85368bb2018-04-19 16:06:14 +0200310 if (rtc->irq >= 0 && device_may_wakeup(dev))
Kevin Wells9aa449b2010-10-27 15:33:01 -0700311 disable_irq_wake(rtc->irq);
312
313 return 0;
314}
315
316/* Unconditionally disable the alarm */
317static int lpc32xx_rtc_freeze(struct device *dev)
318{
Wolfram Sang85368bb2018-04-19 16:06:14 +0200319 struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
Kevin Wells9aa449b2010-10-27 15:33:01 -0700320
321 spin_lock_irq(&rtc->lock);
322
323 rtc_writel(rtc, LPC32XX_RTC_CTRL,
324 rtc_readl(rtc, LPC32XX_RTC_CTRL) &
325 ~LPC32XX_RTC_CTRL_MATCH0);
326
327 spin_unlock_irq(&rtc->lock);
328
329 return 0;
330}
331
332static int lpc32xx_rtc_thaw(struct device *dev)
333{
Wolfram Sang85368bb2018-04-19 16:06:14 +0200334 struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
Kevin Wells9aa449b2010-10-27 15:33:01 -0700335
336 if (rtc->alarm_enabled) {
337 spin_lock_irq(&rtc->lock);
338
339 rtc_writel(rtc, LPC32XX_RTC_CTRL,
340 rtc_readl(rtc, LPC32XX_RTC_CTRL) |
341 LPC32XX_RTC_CTRL_MATCH0);
342
343 spin_unlock_irq(&rtc->lock);
344 }
345
346 return 0;
347}
348
349static const struct dev_pm_ops lpc32xx_rtc_pm_ops = {
350 .suspend = lpc32xx_rtc_suspend,
351 .resume = lpc32xx_rtc_resume,
352 .freeze = lpc32xx_rtc_freeze,
353 .thaw = lpc32xx_rtc_thaw,
354 .restore = lpc32xx_rtc_resume
355};
356
357#define LPC32XX_RTC_PM_OPS (&lpc32xx_rtc_pm_ops)
358#else
359#define LPC32XX_RTC_PM_OPS NULL
360#endif
361
Roland Stiggee862e7c2012-05-29 15:07:39 -0700362#ifdef CONFIG_OF
363static const struct of_device_id lpc32xx_rtc_match[] = {
364 { .compatible = "nxp,lpc3220-rtc" },
365 { }
366};
367MODULE_DEVICE_TABLE(of, lpc32xx_rtc_match);
368#endif
369
Kevin Wells9aa449b2010-10-27 15:33:01 -0700370static struct platform_driver lpc32xx_rtc_driver = {
371 .probe = lpc32xx_rtc_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800372 .remove = lpc32xx_rtc_remove,
Kevin Wells9aa449b2010-10-27 15:33:01 -0700373 .driver = {
Alexandre Belloni6bbad582019-04-09 12:16:26 +0200374 .name = "rtc-lpc32xx",
Roland Stiggee862e7c2012-05-29 15:07:39 -0700375 .pm = LPC32XX_RTC_PM_OPS,
376 .of_match_table = of_match_ptr(lpc32xx_rtc_match),
Kevin Wells9aa449b2010-10-27 15:33:01 -0700377 },
378};
379
Axel Lin0c4eae62012-01-10 15:10:48 -0800380module_platform_driver(lpc32xx_rtc_driver);
Kevin Wells9aa449b2010-10-27 15:33:01 -0700381
382MODULE_AUTHOR("Kevin Wells <wellsk40@gmail.com");
383MODULE_DESCRIPTION("RTC driver for the LPC32xx SoC");
384MODULE_LICENSE("GPL");
385MODULE_ALIAS("platform:rtc-lpc32xx");