Merge tag 'gpio-for-v3.11-3' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw...
[linux-2.6.git] / drivers / rtc / rtc-sirfsoc.c
blobaa7ed4b5f7f06c7e37ab37b69b6f6e94cd177ad1
1 /*
2 * SiRFSoC Real Time Clock interface for Linux
4 * Copyright (c) 2013 Cambridge Silicon Radio Limited, a CSR plc group company.
6 * Licensed under GPLv2 or later.
7 */
9 #include <linux/module.h>
10 #include <linux/err.h>
11 #include <linux/rtc.h>
12 #include <linux/platform_device.h>
13 #include <linux/slab.h>
14 #include <linux/io.h>
15 #include <linux/of.h>
16 #include <linux/rtc/sirfsoc_rtciobrg.h>
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 */
35 /* Refer to RTC DIV switch */
36 #define RTC_HZ 16
38 /* This macro is also defined in arch/arm/plat-sirfsoc/cpu.c */
39 #define RTC_SHIFT 4
41 #define INTR_SYSRTC_CN 0x48
43 struct sirfsoc_rtc_drv {
44 struct rtc_device *rtc;
45 u32 rtc_base;
46 u32 irq;
47 /* Overflow for every 8 years extra time */
48 u32 overflow_rtc;
49 #ifdef CONFIG_PM
50 u32 saved_counter;
51 u32 saved_overflow_rtc;
52 #endif
55 static int sirfsoc_rtc_read_alarm(struct device *dev,
56 struct rtc_wkalrm *alrm)
58 unsigned long rtc_alarm, rtc_count;
59 struct sirfsoc_rtc_drv *rtcdrv;
61 rtcdrv = (struct sirfsoc_rtc_drv *)dev_get_drvdata(dev);
63 local_irq_disable();
65 rtc_count = sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_CN);
67 rtc_alarm = sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_ALARM0);
68 memset(alrm, 0, sizeof(struct rtc_wkalrm));
71 * assume alarm interval not beyond one round counter overflow_rtc:
72 * 0->0xffffffff
74 /* if alarm is in next overflow cycle */
75 if (rtc_count > rtc_alarm)
76 rtc_time_to_tm((rtcdrv->overflow_rtc + 1)
77 << (BITS_PER_LONG - RTC_SHIFT)
78 | rtc_alarm >> RTC_SHIFT, &(alrm->time));
79 else
80 rtc_time_to_tm(rtcdrv->overflow_rtc
81 << (BITS_PER_LONG - RTC_SHIFT)
82 | rtc_alarm >> RTC_SHIFT, &(alrm->time));
83 if (sirfsoc_rtc_iobrg_readl(
84 rtcdrv->rtc_base + RTC_STATUS) & SIRFSOC_RTC_AL0E)
85 alrm->enabled = 1;
86 local_irq_enable();
88 return 0;
91 static int sirfsoc_rtc_set_alarm(struct device *dev,
92 struct rtc_wkalrm *alrm)
94 unsigned long rtc_status_reg, rtc_alarm;
95 struct sirfsoc_rtc_drv *rtcdrv;
96 rtcdrv = (struct sirfsoc_rtc_drv *)dev_get_drvdata(dev);
98 if (alrm->enabled) {
99 rtc_tm_to_time(&(alrm->time), &rtc_alarm);
101 local_irq_disable();
103 rtc_status_reg = sirfsoc_rtc_iobrg_readl(
104 rtcdrv->rtc_base + RTC_STATUS);
105 if (rtc_status_reg & SIRFSOC_RTC_AL0E) {
107 * An ongoing alarm in progress - ingore it and not
108 * to return EBUSY
110 dev_info(dev, "An old alarm was set, will be replaced by a new one\n");
113 sirfsoc_rtc_iobrg_writel(
114 rtc_alarm << RTC_SHIFT, rtcdrv->rtc_base + RTC_ALARM0);
115 rtc_status_reg &= ~0x07; /* mask out the lower status bits */
117 * This bit RTC_AL sets it as a wake-up source for Sleep Mode
118 * Writing 1 into this bit will clear it
120 rtc_status_reg |= SIRFSOC_RTC_AL0;
121 /* enable the RTC alarm interrupt */
122 rtc_status_reg |= SIRFSOC_RTC_AL0E;
123 sirfsoc_rtc_iobrg_writel(
124 rtc_status_reg, rtcdrv->rtc_base + RTC_STATUS);
125 local_irq_enable();
126 } else {
128 * if this function was called with enabled=0
129 * then it could mean that the application is
130 * trying to cancel an ongoing alarm
132 local_irq_disable();
134 rtc_status_reg = sirfsoc_rtc_iobrg_readl(
135 rtcdrv->rtc_base + RTC_STATUS);
136 if (rtc_status_reg & SIRFSOC_RTC_AL0E) {
137 /* clear the RTC status register's alarm bit */
138 rtc_status_reg &= ~0x07;
139 /* write 1 into SIRFSOC_RTC_AL0 to force a clear */
140 rtc_status_reg |= (SIRFSOC_RTC_AL0);
141 /* Clear the Alarm enable bit */
142 rtc_status_reg &= ~(SIRFSOC_RTC_AL0E);
144 sirfsoc_rtc_iobrg_writel(rtc_status_reg,
145 rtcdrv->rtc_base + RTC_STATUS);
148 local_irq_enable();
151 return 0;
154 static int sirfsoc_rtc_read_time(struct device *dev,
155 struct rtc_time *tm)
157 unsigned long tmp_rtc = 0;
158 struct sirfsoc_rtc_drv *rtcdrv;
159 rtcdrv = (struct sirfsoc_rtc_drv *)dev_get_drvdata(dev);
161 * This patch is taken from WinCE - Need to validate this for
162 * correctness. To work around sirfsoc RTC counter double sync logic
163 * fail, read several times to make sure get stable value.
165 do {
166 tmp_rtc = sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_CN);
167 cpu_relax();
168 } while (tmp_rtc != sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_CN));
170 rtc_time_to_tm(rtcdrv->overflow_rtc << (BITS_PER_LONG - RTC_SHIFT) |
171 tmp_rtc >> RTC_SHIFT, tm);
172 return 0;
175 static int sirfsoc_rtc_set_time(struct device *dev,
176 struct rtc_time *tm)
178 unsigned long rtc_time;
179 struct sirfsoc_rtc_drv *rtcdrv;
180 rtcdrv = (struct sirfsoc_rtc_drv *)dev_get_drvdata(dev);
182 rtc_tm_to_time(tm, &rtc_time);
184 rtcdrv->overflow_rtc = rtc_time >> (BITS_PER_LONG - RTC_SHIFT);
186 sirfsoc_rtc_iobrg_writel(rtcdrv->overflow_rtc,
187 rtcdrv->rtc_base + RTC_SW_VALUE);
188 sirfsoc_rtc_iobrg_writel(
189 rtc_time << RTC_SHIFT, rtcdrv->rtc_base + RTC_CN);
191 return 0;
194 static int sirfsoc_rtc_ioctl(struct device *dev, unsigned int cmd,
195 unsigned long arg)
197 switch (cmd) {
198 case RTC_PIE_ON:
199 case RTC_PIE_OFF:
200 case RTC_UIE_ON:
201 case RTC_UIE_OFF:
202 case RTC_AIE_ON:
203 case RTC_AIE_OFF:
204 return 0;
206 default:
207 return -ENOIOCTLCMD;
211 static const struct rtc_class_ops sirfsoc_rtc_ops = {
212 .read_time = sirfsoc_rtc_read_time,
213 .set_time = sirfsoc_rtc_set_time,
214 .read_alarm = sirfsoc_rtc_read_alarm,
215 .set_alarm = sirfsoc_rtc_set_alarm,
216 .ioctl = sirfsoc_rtc_ioctl
219 static irqreturn_t sirfsoc_rtc_irq_handler(int irq, void *pdata)
221 struct sirfsoc_rtc_drv *rtcdrv = pdata;
222 unsigned long rtc_status_reg = 0x0;
223 unsigned long events = 0x0;
225 rtc_status_reg = sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_STATUS);
226 /* this bit will be set ONLY if an alarm was active
227 * and it expired NOW
228 * So this is being used as an ASSERT
230 if (rtc_status_reg & SIRFSOC_RTC_AL0) {
232 * clear the RTC status register's alarm bit
233 * mask out the lower status bits
235 rtc_status_reg &= ~0x07;
236 /* write 1 into SIRFSOC_RTC_AL0 to ACK the alarm interrupt */
237 rtc_status_reg |= (SIRFSOC_RTC_AL0);
238 /* Clear the Alarm enable bit */
239 rtc_status_reg &= ~(SIRFSOC_RTC_AL0E);
241 sirfsoc_rtc_iobrg_writel(rtc_status_reg, rtcdrv->rtc_base + RTC_STATUS);
242 /* this should wake up any apps polling/waiting on the read
243 * after setting the alarm
245 events |= RTC_IRQF | RTC_AF;
246 rtc_update_irq(rtcdrv->rtc, 1, events);
248 return IRQ_HANDLED;
251 static const struct of_device_id sirfsoc_rtc_of_match[] = {
252 { .compatible = "sirf,prima2-sysrtc"},
255 MODULE_DEVICE_TABLE(of, sirfsoc_rtc_of_match);
257 static int sirfsoc_rtc_probe(struct platform_device *pdev)
259 int err;
260 unsigned long rtc_div;
261 struct sirfsoc_rtc_drv *rtcdrv;
262 struct device_node *np = pdev->dev.of_node;
264 rtcdrv = devm_kzalloc(&pdev->dev,
265 sizeof(struct sirfsoc_rtc_drv), GFP_KERNEL);
266 if (rtcdrv == NULL) {
267 dev_err(&pdev->dev,
268 "%s: can't alloc mem for drv struct\n",
269 pdev->name);
270 return -ENOMEM;
273 err = of_property_read_u32(np, "reg", &rtcdrv->rtc_base);
274 if (err) {
275 dev_err(&pdev->dev, "unable to find base address of rtc node in dtb\n");
276 goto error;
279 platform_set_drvdata(pdev, rtcdrv);
281 /* Register rtc alarm as a wakeup source */
282 device_init_wakeup(&pdev->dev, 1);
285 * Set SYS_RTC counter in RTC_HZ HZ Units
286 * We are using 32K RTC crystal (32768 / RTC_HZ / 2) -1
287 * If 16HZ, therefore RTC_DIV = 1023;
289 rtc_div = ((32768 / RTC_HZ) / 2) - 1;
290 sirfsoc_rtc_iobrg_writel(rtc_div, rtcdrv->rtc_base + RTC_DIV);
292 rtcdrv->rtc = rtc_device_register(pdev->name, &(pdev->dev),
293 &sirfsoc_rtc_ops, THIS_MODULE);
294 if (IS_ERR(rtcdrv->rtc)) {
295 err = PTR_ERR(rtcdrv->rtc);
296 dev_err(&pdev->dev, "can't register RTC device\n");
297 return err;
300 /* 0x3 -> RTC_CLK */
301 sirfsoc_rtc_iobrg_writel(SIRFSOC_RTC_CLK,
302 rtcdrv->rtc_base + RTC_CLOCK_SWITCH);
304 /* reset SYS RTC ALARM0 */
305 sirfsoc_rtc_iobrg_writel(0x0, rtcdrv->rtc_base + RTC_ALARM0);
307 /* reset SYS RTC ALARM1 */
308 sirfsoc_rtc_iobrg_writel(0x0, rtcdrv->rtc_base + RTC_ALARM1);
310 /* Restore RTC Overflow From Register After Command Reboot */
311 rtcdrv->overflow_rtc =
312 sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_SW_VALUE);
314 rtcdrv->irq = platform_get_irq(pdev, 0);
315 err = devm_request_irq(
316 &pdev->dev,
317 rtcdrv->irq,
318 sirfsoc_rtc_irq_handler,
319 IRQF_SHARED,
320 pdev->name,
321 rtcdrv);
322 if (err) {
323 dev_err(&pdev->dev, "Unable to register for the SiRF SOC RTC IRQ\n");
324 goto error;
327 return 0;
329 error:
330 if (rtcdrv->rtc)
331 rtc_device_unregister(rtcdrv->rtc);
333 return err;
336 static int sirfsoc_rtc_remove(struct platform_device *pdev)
338 struct sirfsoc_rtc_drv *rtcdrv = platform_get_drvdata(pdev);
340 device_init_wakeup(&pdev->dev, 0);
341 rtc_device_unregister(rtcdrv->rtc);
343 return 0;
346 #ifdef CONFIG_PM
348 static int sirfsoc_rtc_suspend(struct device *dev)
350 struct platform_device *pdev = to_platform_device(dev);
351 struct sirfsoc_rtc_drv *rtcdrv = platform_get_drvdata(pdev);
352 rtcdrv->overflow_rtc =
353 sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_SW_VALUE);
355 rtcdrv->saved_counter =
356 sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_CN);
357 rtcdrv->saved_overflow_rtc = rtcdrv->overflow_rtc;
358 if (device_may_wakeup(&pdev->dev))
359 enable_irq_wake(rtcdrv->irq);
361 return 0;
364 static int sirfsoc_rtc_freeze(struct device *dev)
366 sirfsoc_rtc_suspend(dev);
368 return 0;
371 static int sirfsoc_rtc_thaw(struct device *dev)
373 u32 tmp;
374 struct sirfsoc_rtc_drv *rtcdrv;
375 rtcdrv = (struct sirfsoc_rtc_drv *)dev_get_drvdata(dev);
378 * if resume from snapshot and the rtc power is losed,
379 * restroe the rtc settings
381 if (SIRFSOC_RTC_CLK != sirfsoc_rtc_iobrg_readl(
382 rtcdrv->rtc_base + RTC_CLOCK_SWITCH)) {
383 u32 rtc_div;
384 /* 0x3 -> RTC_CLK */
385 sirfsoc_rtc_iobrg_writel(SIRFSOC_RTC_CLK,
386 rtcdrv->rtc_base + RTC_CLOCK_SWITCH);
388 * Set SYS_RTC counter in RTC_HZ HZ Units
389 * We are using 32K RTC crystal (32768 / RTC_HZ / 2) -1
390 * If 16HZ, therefore RTC_DIV = 1023;
392 rtc_div = ((32768 / RTC_HZ) / 2) - 1;
394 sirfsoc_rtc_iobrg_writel(rtc_div, rtcdrv->rtc_base + RTC_DIV);
396 /* reset SYS RTC ALARM0 */
397 sirfsoc_rtc_iobrg_writel(0x0, rtcdrv->rtc_base + RTC_ALARM0);
399 /* reset SYS RTC ALARM1 */
400 sirfsoc_rtc_iobrg_writel(0x0, rtcdrv->rtc_base + RTC_ALARM1);
402 rtcdrv->overflow_rtc = rtcdrv->saved_overflow_rtc;
405 * if current counter is small than previous,
406 * it means overflow in sleep
408 tmp = sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_CN);
409 if (tmp <= rtcdrv->saved_counter)
410 rtcdrv->overflow_rtc++;
412 *PWRC Value Be Changed When Suspend, Restore Overflow
413 * In Memory To Register
415 sirfsoc_rtc_iobrg_writel(rtcdrv->overflow_rtc,
416 rtcdrv->rtc_base + RTC_SW_VALUE);
418 return 0;
421 static int sirfsoc_rtc_resume(struct device *dev)
423 struct platform_device *pdev = to_platform_device(dev);
424 struct sirfsoc_rtc_drv *rtcdrv = platform_get_drvdata(pdev);
425 sirfsoc_rtc_thaw(dev);
426 if (device_may_wakeup(&pdev->dev))
427 disable_irq_wake(rtcdrv->irq);
429 return 0;
432 static int sirfsoc_rtc_restore(struct device *dev)
434 struct platform_device *pdev = to_platform_device(dev);
435 struct sirfsoc_rtc_drv *rtcdrv = platform_get_drvdata(pdev);
437 if (device_may_wakeup(&pdev->dev))
438 disable_irq_wake(rtcdrv->irq);
439 return 0;
442 #else
443 #define sirfsoc_rtc_suspend NULL
444 #define sirfsoc_rtc_resume NULL
445 #define sirfsoc_rtc_freeze NULL
446 #define sirfsoc_rtc_thaw NULL
447 #define sirfsoc_rtc_restore NULL
448 #endif
450 static const struct dev_pm_ops sirfsoc_rtc_pm_ops = {
451 .suspend = sirfsoc_rtc_suspend,
452 .resume = sirfsoc_rtc_resume,
453 .freeze = sirfsoc_rtc_freeze,
454 .thaw = sirfsoc_rtc_thaw,
455 .restore = sirfsoc_rtc_restore,
458 static struct platform_driver sirfsoc_rtc_driver = {
459 .driver = {
460 .name = "sirfsoc-rtc",
461 .owner = THIS_MODULE,
462 #ifdef CONFIG_PM
463 .pm = &sirfsoc_rtc_pm_ops,
464 #endif
465 .of_match_table = of_match_ptr(sirfsoc_rtc_of_match),
467 .probe = sirfsoc_rtc_probe,
468 .remove = sirfsoc_rtc_remove,
470 module_platform_driver(sirfsoc_rtc_driver);
472 MODULE_DESCRIPTION("SiRF SoC rtc driver");
473 MODULE_AUTHOR("Xianglong Du <Xianglong.Du@csr.com>");
474 MODULE_LICENSE("GPL v2");
475 MODULE_ALIAS("platform:sirfsoc-rtc");