blob: abf19435dbad0bb14b21467783da2467a828174d [file] [log] [blame]
Thomas Gleixnera636cd62019-05-19 15:51:34 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Xianglong Due88b8152013-07-03 15:08:04 -07002/*
3 * SiRFSoC Real Time Clock interface for Linux
4 *
5 * Copyright (c) 2013 Cambridge Silicon Radio Limited, a CSR plc group company.
Xianglong Due88b8152013-07-03 15:08:04 -07006 */
7
8#include <linux/module.h>
9#include <linux/err.h>
10#include <linux/rtc.h>
11#include <linux/platform_device.h>
12#include <linux/slab.h>
13#include <linux/io.h>
14#include <linux/of.h>
Guo Zengdfe6c042015-07-14 01:31:38 +000015#include <linux/regmap.h>
Xianglong Due88b8152013-07-03 15:08:04 -070016#include <linux/rtc/sirfsoc_rtciobrg.h>
17
18
19#define RTC_CN 0x00
20#define RTC_ALARM0 0x04
21#define RTC_ALARM1 0x18
22#define RTC_STATUS 0x08
23#define RTC_SW_VALUE 0x40
24#define SIRFSOC_RTC_AL1E (1<<6)
25#define SIRFSOC_RTC_AL1 (1<<4)
26#define SIRFSOC_RTC_HZE (1<<3)
27#define SIRFSOC_RTC_AL0E (1<<2)
28#define SIRFSOC_RTC_HZ (1<<1)
29#define SIRFSOC_RTC_AL0 (1<<0)
30#define RTC_DIV 0x0c
31#define RTC_DEEP_CTRL 0x14
32#define RTC_CLOCK_SWITCH 0x1c
33#define SIRFSOC_RTC_CLK 0x03 /* others are reserved */
34
35/* Refer to RTC DIV switch */
36#define RTC_HZ 16
37
38/* This macro is also defined in arch/arm/plat-sirfsoc/cpu.c */
39#define RTC_SHIFT 4
40
41#define INTR_SYSRTC_CN 0x48
42
43struct sirfsoc_rtc_drv {
44 struct rtc_device *rtc;
45 u32 rtc_base;
46 u32 irq;
Xianglong Du28984c7d2013-09-11 14:24:23 -070047 unsigned irq_wake;
Xianglong Due88b8152013-07-03 15:08:04 -070048 /* Overflow for every 8 years extra time */
49 u32 overflow_rtc;
Barry Songe9bc7362014-12-10 15:53:51 -080050 spinlock_t lock;
Guo Zengdfe6c042015-07-14 01:31:38 +000051 struct regmap *regmap;
Xianglong Due88b8152013-07-03 15:08:04 -070052#ifdef CONFIG_PM
53 u32 saved_counter;
54 u32 saved_overflow_rtc;
55#endif
56};
57
Guo Zengdfe6c042015-07-14 01:31:38 +000058static u32 sirfsoc_rtc_readl(struct sirfsoc_rtc_drv *rtcdrv, u32 offset)
59{
60 u32 val;
61
62 regmap_read(rtcdrv->regmap, rtcdrv->rtc_base + offset, &val);
63 return val;
64}
65
66static void sirfsoc_rtc_writel(struct sirfsoc_rtc_drv *rtcdrv,
67 u32 offset, u32 val)
68{
69 regmap_write(rtcdrv->regmap, rtcdrv->rtc_base + offset, val);
70}
71
Xianglong Due88b8152013-07-03 15:08:04 -070072static int sirfsoc_rtc_read_alarm(struct device *dev,
73 struct rtc_wkalrm *alrm)
74{
75 unsigned long rtc_alarm, rtc_count;
76 struct sirfsoc_rtc_drv *rtcdrv;
77
Jingoo Hanb7efdf32013-11-12 15:10:35 -080078 rtcdrv = dev_get_drvdata(dev);
Xianglong Due88b8152013-07-03 15:08:04 -070079
Barry Songe9bc7362014-12-10 15:53:51 -080080 spin_lock_irq(&rtcdrv->lock);
Xianglong Due88b8152013-07-03 15:08:04 -070081
Guo Zengdfe6c042015-07-14 01:31:38 +000082 rtc_count = sirfsoc_rtc_readl(rtcdrv, RTC_CN);
Xianglong Due88b8152013-07-03 15:08:04 -070083
Guo Zengdfe6c042015-07-14 01:31:38 +000084 rtc_alarm = sirfsoc_rtc_readl(rtcdrv, RTC_ALARM0);
Xianglong Due88b8152013-07-03 15:08:04 -070085 memset(alrm, 0, sizeof(struct rtc_wkalrm));
86
87 /*
88 * assume alarm interval not beyond one round counter overflow_rtc:
89 * 0->0xffffffff
90 */
91 /* if alarm is in next overflow cycle */
92 if (rtc_count > rtc_alarm)
Alexandre Belloni09b875a2020-03-05 17:04:52 +010093 rtc_time64_to_tm((rtcdrv->overflow_rtc + 1)
94 << (BITS_PER_LONG - RTC_SHIFT)
95 | rtc_alarm >> RTC_SHIFT, &alrm->time);
Xianglong Due88b8152013-07-03 15:08:04 -070096 else
Alexandre Belloni09b875a2020-03-05 17:04:52 +010097 rtc_time64_to_tm(rtcdrv->overflow_rtc
98 << (BITS_PER_LONG - RTC_SHIFT)
99 | rtc_alarm >> RTC_SHIFT, &alrm->time);
Guo Zengdfe6c042015-07-14 01:31:38 +0000100 if (sirfsoc_rtc_readl(rtcdrv, RTC_STATUS) & SIRFSOC_RTC_AL0E)
Xianglong Due88b8152013-07-03 15:08:04 -0700101 alrm->enabled = 1;
Barry Songe9bc7362014-12-10 15:53:51 -0800102
103 spin_unlock_irq(&rtcdrv->lock);
Xianglong Due88b8152013-07-03 15:08:04 -0700104
105 return 0;
106}
107
108static int sirfsoc_rtc_set_alarm(struct device *dev,
109 struct rtc_wkalrm *alrm)
110{
111 unsigned long rtc_status_reg, rtc_alarm;
112 struct sirfsoc_rtc_drv *rtcdrv;
Jingoo Hanb7efdf32013-11-12 15:10:35 -0800113 rtcdrv = dev_get_drvdata(dev);
Xianglong Due88b8152013-07-03 15:08:04 -0700114
115 if (alrm->enabled) {
Alexandre Belloni09b875a2020-03-05 17:04:52 +0100116 rtc_alarm = rtc_tm_to_time64(&alrm->time);
Xianglong Due88b8152013-07-03 15:08:04 -0700117
Barry Songe9bc7362014-12-10 15:53:51 -0800118 spin_lock_irq(&rtcdrv->lock);
Xianglong Due88b8152013-07-03 15:08:04 -0700119
Guo Zengdfe6c042015-07-14 01:31:38 +0000120 rtc_status_reg = sirfsoc_rtc_readl(rtcdrv, RTC_STATUS);
Xianglong Due88b8152013-07-03 15:08:04 -0700121 if (rtc_status_reg & SIRFSOC_RTC_AL0E) {
122 /*
123 * An ongoing alarm in progress - ingore it and not
124 * to return EBUSY
125 */
126 dev_info(dev, "An old alarm was set, will be replaced by a new one\n");
127 }
128
Guo Zengdfe6c042015-07-14 01:31:38 +0000129 sirfsoc_rtc_writel(rtcdrv, RTC_ALARM0, rtc_alarm << RTC_SHIFT);
Xianglong Due88b8152013-07-03 15:08:04 -0700130 rtc_status_reg &= ~0x07; /* mask out the lower status bits */
131 /*
132 * This bit RTC_AL sets it as a wake-up source for Sleep Mode
133 * Writing 1 into this bit will clear it
134 */
135 rtc_status_reg |= SIRFSOC_RTC_AL0;
136 /* enable the RTC alarm interrupt */
137 rtc_status_reg |= SIRFSOC_RTC_AL0E;
Guo Zengdfe6c042015-07-14 01:31:38 +0000138 sirfsoc_rtc_writel(rtcdrv, RTC_STATUS, rtc_status_reg);
Barry Songe9bc7362014-12-10 15:53:51 -0800139
140 spin_unlock_irq(&rtcdrv->lock);
Xianglong Due88b8152013-07-03 15:08:04 -0700141 } else {
142 /*
143 * if this function was called with enabled=0
144 * then it could mean that the application is
145 * trying to cancel an ongoing alarm
146 */
Barry Songe9bc7362014-12-10 15:53:51 -0800147 spin_lock_irq(&rtcdrv->lock);
Xianglong Due88b8152013-07-03 15:08:04 -0700148
Guo Zengdfe6c042015-07-14 01:31:38 +0000149 rtc_status_reg = sirfsoc_rtc_readl(rtcdrv, RTC_STATUS);
Xianglong Due88b8152013-07-03 15:08:04 -0700150 if (rtc_status_reg & SIRFSOC_RTC_AL0E) {
151 /* clear the RTC status register's alarm bit */
152 rtc_status_reg &= ~0x07;
153 /* write 1 into SIRFSOC_RTC_AL0 to force a clear */
154 rtc_status_reg |= (SIRFSOC_RTC_AL0);
155 /* Clear the Alarm enable bit */
156 rtc_status_reg &= ~(SIRFSOC_RTC_AL0E);
157
Guo Zengdfe6c042015-07-14 01:31:38 +0000158 sirfsoc_rtc_writel(rtcdrv, RTC_STATUS,
159 rtc_status_reg);
Xianglong Due88b8152013-07-03 15:08:04 -0700160 }
161
Barry Songe9bc7362014-12-10 15:53:51 -0800162 spin_unlock_irq(&rtcdrv->lock);
Xianglong Due88b8152013-07-03 15:08:04 -0700163 }
164
165 return 0;
166}
167
168static int sirfsoc_rtc_read_time(struct device *dev,
169 struct rtc_time *tm)
170{
171 unsigned long tmp_rtc = 0;
172 struct sirfsoc_rtc_drv *rtcdrv;
Jingoo Hanb7efdf32013-11-12 15:10:35 -0800173 rtcdrv = dev_get_drvdata(dev);
Xianglong Due88b8152013-07-03 15:08:04 -0700174 /*
175 * This patch is taken from WinCE - Need to validate this for
176 * correctness. To work around sirfsoc RTC counter double sync logic
177 * fail, read several times to make sure get stable value.
178 */
179 do {
Guo Zengdfe6c042015-07-14 01:31:38 +0000180 tmp_rtc = sirfsoc_rtc_readl(rtcdrv, RTC_CN);
Xianglong Due88b8152013-07-03 15:08:04 -0700181 cpu_relax();
Guo Zengdfe6c042015-07-14 01:31:38 +0000182 } while (tmp_rtc != sirfsoc_rtc_readl(rtcdrv, RTC_CN));
Xianglong Due88b8152013-07-03 15:08:04 -0700183
Alexandre Belloni09b875a2020-03-05 17:04:52 +0100184 rtc_time64_to_tm(rtcdrv->overflow_rtc << (BITS_PER_LONG - RTC_SHIFT)
185 | tmp_rtc >> RTC_SHIFT, tm);
Xianglong Due88b8152013-07-03 15:08:04 -0700186 return 0;
187}
188
189static int sirfsoc_rtc_set_time(struct device *dev,
190 struct rtc_time *tm)
191{
192 unsigned long rtc_time;
193 struct sirfsoc_rtc_drv *rtcdrv;
Jingoo Hanb7efdf32013-11-12 15:10:35 -0800194 rtcdrv = dev_get_drvdata(dev);
Xianglong Due88b8152013-07-03 15:08:04 -0700195
Alexandre Belloni09b875a2020-03-05 17:04:52 +0100196 rtc_time = rtc_tm_to_time64(tm);
Xianglong Due88b8152013-07-03 15:08:04 -0700197
198 rtcdrv->overflow_rtc = rtc_time >> (BITS_PER_LONG - RTC_SHIFT);
199
Guo Zengdfe6c042015-07-14 01:31:38 +0000200 sirfsoc_rtc_writel(rtcdrv, RTC_SW_VALUE, rtcdrv->overflow_rtc);
201 sirfsoc_rtc_writel(rtcdrv, RTC_CN, rtc_time << RTC_SHIFT);
Xianglong Due88b8152013-07-03 15:08:04 -0700202
203 return 0;
204}
205
hao liu09e427f2014-12-10 15:53:49 -0800206static int sirfsoc_rtc_alarm_irq_enable(struct device *dev,
207 unsigned int enabled)
208{
209 unsigned long rtc_status_reg = 0x0;
210 struct sirfsoc_rtc_drv *rtcdrv;
211
212 rtcdrv = dev_get_drvdata(dev);
213
Barry Songe9bc7362014-12-10 15:53:51 -0800214 spin_lock_irq(&rtcdrv->lock);
hao liu09e427f2014-12-10 15:53:49 -0800215
Guo Zengdfe6c042015-07-14 01:31:38 +0000216 rtc_status_reg = sirfsoc_rtc_readl(rtcdrv, RTC_STATUS);
hao liu09e427f2014-12-10 15:53:49 -0800217 if (enabled)
218 rtc_status_reg |= SIRFSOC_RTC_AL0E;
219 else
220 rtc_status_reg &= ~SIRFSOC_RTC_AL0E;
221
Guo Zengdfe6c042015-07-14 01:31:38 +0000222 sirfsoc_rtc_writel(rtcdrv, RTC_STATUS, rtc_status_reg);
Barry Songe9bc7362014-12-10 15:53:51 -0800223
224 spin_unlock_irq(&rtcdrv->lock);
hao liu09e427f2014-12-10 15:53:49 -0800225
226 return 0;
227
228}
229
Xianglong Due88b8152013-07-03 15:08:04 -0700230static const struct rtc_class_ops sirfsoc_rtc_ops = {
231 .read_time = sirfsoc_rtc_read_time,
232 .set_time = sirfsoc_rtc_set_time,
233 .read_alarm = sirfsoc_rtc_read_alarm,
234 .set_alarm = sirfsoc_rtc_set_alarm,
hao liu09e427f2014-12-10 15:53:49 -0800235 .alarm_irq_enable = sirfsoc_rtc_alarm_irq_enable
Xianglong Due88b8152013-07-03 15:08:04 -0700236};
237
238static irqreturn_t sirfsoc_rtc_irq_handler(int irq, void *pdata)
239{
240 struct sirfsoc_rtc_drv *rtcdrv = pdata;
241 unsigned long rtc_status_reg = 0x0;
242 unsigned long events = 0x0;
243
Barry Songe9bc7362014-12-10 15:53:51 -0800244 spin_lock(&rtcdrv->lock);
245
Guo Zengdfe6c042015-07-14 01:31:38 +0000246 rtc_status_reg = sirfsoc_rtc_readl(rtcdrv, RTC_STATUS);
Xianglong Due88b8152013-07-03 15:08:04 -0700247 /* this bit will be set ONLY if an alarm was active
248 * and it expired NOW
249 * So this is being used as an ASSERT
250 */
251 if (rtc_status_reg & SIRFSOC_RTC_AL0) {
252 /*
253 * clear the RTC status register's alarm bit
254 * mask out the lower status bits
255 */
256 rtc_status_reg &= ~0x07;
257 /* write 1 into SIRFSOC_RTC_AL0 to ACK the alarm interrupt */
258 rtc_status_reg |= (SIRFSOC_RTC_AL0);
259 /* Clear the Alarm enable bit */
260 rtc_status_reg &= ~(SIRFSOC_RTC_AL0E);
261 }
Guo Zengdfe6c042015-07-14 01:31:38 +0000262
263 sirfsoc_rtc_writel(rtcdrv, RTC_STATUS, rtc_status_reg);
Barry Songe9bc7362014-12-10 15:53:51 -0800264
265 spin_unlock(&rtcdrv->lock);
266
Xianglong Due88b8152013-07-03 15:08:04 -0700267 /* this should wake up any apps polling/waiting on the read
268 * after setting the alarm
269 */
270 events |= RTC_IRQF | RTC_AF;
271 rtc_update_irq(rtcdrv->rtc, 1, events);
272
273 return IRQ_HANDLED;
274}
275
276static const struct of_device_id sirfsoc_rtc_of_match[] = {
277 { .compatible = "sirf,prima2-sysrtc"},
278 {},
279};
Guo Zengdfe6c042015-07-14 01:31:38 +0000280
YueHaibing153a9172019-04-10 21:43:36 +0800281static const struct regmap_config sysrtc_regmap_config = {
Guo Zengdfe6c042015-07-14 01:31:38 +0000282 .reg_bits = 32,
283 .val_bits = 32,
284 .fast_io = true,
285};
286
Xianglong Due88b8152013-07-03 15:08:04 -0700287MODULE_DEVICE_TABLE(of, sirfsoc_rtc_of_match);
288
289static int sirfsoc_rtc_probe(struct platform_device *pdev)
290{
291 int err;
292 unsigned long rtc_div;
293 struct sirfsoc_rtc_drv *rtcdrv;
294 struct device_node *np = pdev->dev.of_node;
295
296 rtcdrv = devm_kzalloc(&pdev->dev,
297 sizeof(struct sirfsoc_rtc_drv), GFP_KERNEL);
Jingoo Han98e2d212014-04-03 14:49:45 -0700298 if (rtcdrv == NULL)
Xianglong Due88b8152013-07-03 15:08:04 -0700299 return -ENOMEM;
Xianglong Due88b8152013-07-03 15:08:04 -0700300
Barry Songe9bc7362014-12-10 15:53:51 -0800301 spin_lock_init(&rtcdrv->lock);
302
Xianglong Due88b8152013-07-03 15:08:04 -0700303 err = of_property_read_u32(np, "reg", &rtcdrv->rtc_base);
304 if (err) {
305 dev_err(&pdev->dev, "unable to find base address of rtc node in dtb\n");
Sachin Kamat3d091622013-11-12 15:10:31 -0800306 return err;
Xianglong Due88b8152013-07-03 15:08:04 -0700307 }
308
309 platform_set_drvdata(pdev, rtcdrv);
310
311 /* Register rtc alarm as a wakeup source */
312 device_init_wakeup(&pdev->dev, 1);
313
Guo Zengdfe6c042015-07-14 01:31:38 +0000314 rtcdrv->regmap = devm_regmap_init_iobg(&pdev->dev,
315 &sysrtc_regmap_config);
316 if (IS_ERR(rtcdrv->regmap)) {
317 err = PTR_ERR(rtcdrv->regmap);
318 dev_err(&pdev->dev, "Failed to allocate register map: %d\n",
319 err);
320 return err;
321 }
322
Xianglong Due88b8152013-07-03 15:08:04 -0700323 /*
324 * Set SYS_RTC counter in RTC_HZ HZ Units
325 * We are using 32K RTC crystal (32768 / RTC_HZ / 2) -1
326 * If 16HZ, therefore RTC_DIV = 1023;
327 */
328 rtc_div = ((32768 / RTC_HZ) / 2) - 1;
Guo Zengdfe6c042015-07-14 01:31:38 +0000329 sirfsoc_rtc_writel(rtcdrv, RTC_DIV, rtc_div);
Xianglong Due88b8152013-07-03 15:08:04 -0700330
Xianglong Due88b8152013-07-03 15:08:04 -0700331 /* 0x3 -> RTC_CLK */
Guo Zengdfe6c042015-07-14 01:31:38 +0000332 sirfsoc_rtc_writel(rtcdrv, RTC_CLOCK_SWITCH, SIRFSOC_RTC_CLK);
Xianglong Due88b8152013-07-03 15:08:04 -0700333
334 /* reset SYS RTC ALARM0 */
Guo Zengdfe6c042015-07-14 01:31:38 +0000335 sirfsoc_rtc_writel(rtcdrv, RTC_ALARM0, 0x0);
Xianglong Due88b8152013-07-03 15:08:04 -0700336
337 /* reset SYS RTC ALARM1 */
Guo Zengdfe6c042015-07-14 01:31:38 +0000338 sirfsoc_rtc_writel(rtcdrv, RTC_ALARM1, 0x0);
Xianglong Due88b8152013-07-03 15:08:04 -0700339
340 /* Restore RTC Overflow From Register After Command Reboot */
341 rtcdrv->overflow_rtc =
Guo Zengdfe6c042015-07-14 01:31:38 +0000342 sirfsoc_rtc_readl(rtcdrv, RTC_SW_VALUE);
Xianglong Due88b8152013-07-03 15:08:04 -0700343
Alexandre Belloni2911ee92020-03-05 17:04:50 +0100344 rtcdrv->rtc = devm_rtc_allocate_device(&pdev->dev);
345 if (IS_ERR(rtcdrv->rtc))
346 return PTR_ERR(rtcdrv->rtc);
347
348 rtcdrv->rtc->ops = &sirfsoc_rtc_ops;
Alexandre Bellonicd65dd412020-03-05 17:04:51 +0100349 rtcdrv->rtc->range_max = (1ULL << 60) - 1;
Guo Zeng0e953252014-12-10 15:52:24 -0800350
Xianglong Due88b8152013-07-03 15:08:04 -0700351 rtcdrv->irq = platform_get_irq(pdev, 0);
Alexandre Belloni2911ee92020-03-05 17:04:50 +0100352 err = devm_request_irq(&pdev->dev, rtcdrv->irq, sirfsoc_rtc_irq_handler,
353 IRQF_SHARED, pdev->name, rtcdrv);
Xianglong Due88b8152013-07-03 15:08:04 -0700354 if (err) {
355 dev_err(&pdev->dev, "Unable to register for the SiRF SOC RTC IRQ\n");
Sachin Kamat3d091622013-11-12 15:10:31 -0800356 return err;
Xianglong Due88b8152013-07-03 15:08:04 -0700357 }
358
Alexandre Belloni2911ee92020-03-05 17:04:50 +0100359 return rtc_register_device(rtcdrv->rtc);
Xianglong Due88b8152013-07-03 15:08:04 -0700360}
361
Xianglong Du3916b092014-04-03 14:49:53 -0700362#ifdef CONFIG_PM_SLEEP
Xianglong Due88b8152013-07-03 15:08:04 -0700363static int sirfsoc_rtc_suspend(struct device *dev)
364{
Xianglong Du3916b092014-04-03 14:49:53 -0700365 struct sirfsoc_rtc_drv *rtcdrv = dev_get_drvdata(dev);
Xianglong Due88b8152013-07-03 15:08:04 -0700366 rtcdrv->overflow_rtc =
Guo Zengdfe6c042015-07-14 01:31:38 +0000367 sirfsoc_rtc_readl(rtcdrv, RTC_SW_VALUE);
Xianglong Due88b8152013-07-03 15:08:04 -0700368
369 rtcdrv->saved_counter =
Guo Zengdfe6c042015-07-14 01:31:38 +0000370 sirfsoc_rtc_readl(rtcdrv, RTC_CN);
Xianglong Due88b8152013-07-03 15:08:04 -0700371 rtcdrv->saved_overflow_rtc = rtcdrv->overflow_rtc;
Xianglong Du3916b092014-04-03 14:49:53 -0700372 if (device_may_wakeup(dev) && !enable_irq_wake(rtcdrv->irq))
Xianglong Du28984c7d2013-09-11 14:24:23 -0700373 rtcdrv->irq_wake = 1;
Xianglong Due88b8152013-07-03 15:08:04 -0700374
375 return 0;
376}
377
Xianglong Du3916b092014-04-03 14:49:53 -0700378static int sirfsoc_rtc_resume(struct device *dev)
Xianglong Due88b8152013-07-03 15:08:04 -0700379{
380 u32 tmp;
Xianglong Du3916b092014-04-03 14:49:53 -0700381 struct sirfsoc_rtc_drv *rtcdrv = dev_get_drvdata(dev);
Xianglong Due88b8152013-07-03 15:08:04 -0700382
383 /*
Xianglong Du3916b092014-04-03 14:49:53 -0700384 * if resume from snapshot and the rtc power is lost,
Xianglong Due88b8152013-07-03 15:08:04 -0700385 * restroe the rtc settings
386 */
Guo Zengdfe6c042015-07-14 01:31:38 +0000387 if (SIRFSOC_RTC_CLK != sirfsoc_rtc_readl(rtcdrv, RTC_CLOCK_SWITCH)) {
Xianglong Due88b8152013-07-03 15:08:04 -0700388 u32 rtc_div;
389 /* 0x3 -> RTC_CLK */
Guo Zengdfe6c042015-07-14 01:31:38 +0000390 sirfsoc_rtc_writel(rtcdrv, RTC_CLOCK_SWITCH, SIRFSOC_RTC_CLK);
Xianglong Due88b8152013-07-03 15:08:04 -0700391 /*
392 * Set SYS_RTC counter in RTC_HZ HZ Units
393 * We are using 32K RTC crystal (32768 / RTC_HZ / 2) -1
394 * If 16HZ, therefore RTC_DIV = 1023;
395 */
396 rtc_div = ((32768 / RTC_HZ) / 2) - 1;
397
Guo Zengdfe6c042015-07-14 01:31:38 +0000398 sirfsoc_rtc_writel(rtcdrv, RTC_DIV, rtc_div);
Xianglong Due88b8152013-07-03 15:08:04 -0700399
400 /* reset SYS RTC ALARM0 */
Guo Zengdfe6c042015-07-14 01:31:38 +0000401 sirfsoc_rtc_writel(rtcdrv, RTC_ALARM0, 0x0);
Xianglong Due88b8152013-07-03 15:08:04 -0700402
403 /* reset SYS RTC ALARM1 */
Guo Zengdfe6c042015-07-14 01:31:38 +0000404 sirfsoc_rtc_writel(rtcdrv, RTC_ALARM1, 0x0);
Xianglong Due88b8152013-07-03 15:08:04 -0700405 }
406 rtcdrv->overflow_rtc = rtcdrv->saved_overflow_rtc;
407
408 /*
409 * if current counter is small than previous,
410 * it means overflow in sleep
411 */
Guo Zengdfe6c042015-07-14 01:31:38 +0000412 tmp = sirfsoc_rtc_readl(rtcdrv, RTC_CN);
Xianglong Due88b8152013-07-03 15:08:04 -0700413 if (tmp <= rtcdrv->saved_counter)
414 rtcdrv->overflow_rtc++;
415 /*
416 *PWRC Value Be Changed When Suspend, Restore Overflow
417 * In Memory To Register
418 */
Guo Zengdfe6c042015-07-14 01:31:38 +0000419 sirfsoc_rtc_writel(rtcdrv, RTC_SW_VALUE, rtcdrv->overflow_rtc);
Xianglong Due88b8152013-07-03 15:08:04 -0700420
Xianglong Du3916b092014-04-03 14:49:53 -0700421 if (device_may_wakeup(dev) && rtcdrv->irq_wake) {
Xianglong Due88b8152013-07-03 15:08:04 -0700422 disable_irq_wake(rtcdrv->irq);
Xianglong Du28984c7d2013-09-11 14:24:23 -0700423 rtcdrv->irq_wake = 0;
424 }
Xianglong Due88b8152013-07-03 15:08:04 -0700425
426 return 0;
427}
Xianglong Due88b8152013-07-03 15:08:04 -0700428#endif
429
Xianglong Du3916b092014-04-03 14:49:53 -0700430static SIMPLE_DEV_PM_OPS(sirfsoc_rtc_pm_ops,
431 sirfsoc_rtc_suspend, sirfsoc_rtc_resume);
Xianglong Due88b8152013-07-03 15:08:04 -0700432
433static struct platform_driver sirfsoc_rtc_driver = {
434 .driver = {
435 .name = "sirfsoc-rtc",
Xianglong Due88b8152013-07-03 15:08:04 -0700436 .pm = &sirfsoc_rtc_pm_ops,
Sachin Kamatd1496322013-11-12 15:10:56 -0800437 .of_match_table = sirfsoc_rtc_of_match,
Xianglong Due88b8152013-07-03 15:08:04 -0700438 },
439 .probe = sirfsoc_rtc_probe,
Xianglong Due88b8152013-07-03 15:08:04 -0700440};
441module_platform_driver(sirfsoc_rtc_driver);
442
443MODULE_DESCRIPTION("SiRF SoC rtc driver");
444MODULE_AUTHOR("Xianglong Du <Xianglong.Du@csr.com>");
445MODULE_LICENSE("GPL v2");
446MODULE_ALIAS("platform:sirfsoc-rtc");