blob: abc19abd5f2d298a5f88ccc2314efe35cfc816a7 [file] [log] [blame]
Richard Purdiee842f1c2006-03-27 01:16:46 -08001/*
2 * Real Time Clock interface for StrongARM SA1x00 and XScale PXA2xx
3 *
4 * Copyright (c) 2000 Nils Faerber
5 *
6 * Based on rtc.c by Paul Gortmaker
7 *
8 * Original Driver by Nils Faerber <nils@kernelconcepts.de>
9 *
10 * Modifications from:
11 * CIH <cih@coventive.com>
Nicolas Pitre2f82af02009-09-14 03:25:28 -040012 * Nicolas Pitre <nico@fluxnic.net>
Richard Purdiee842f1c2006-03-27 01:16:46 -080013 * Andrew Christian <andrew.christian@hp.com>
14 *
15 * Converted to the RTC subsystem and Driver Model
16 * by Richard Purdie <rpurdie@rpsys.net>
17 *
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License
20 * as published by the Free Software Foundation; either version
21 * 2 of the License, or (at your option) any later version.
22 */
23
24#include <linux/platform_device.h>
25#include <linux/module.h>
Haojian Zhuang8e8bbcb2012-02-23 23:36:37 +080026#include <linux/clk.h>
Richard Purdiee842f1c2006-03-27 01:16:46 -080027#include <linux/rtc.h>
28#include <linux/init.h>
29#include <linux/fs.h>
30#include <linux/interrupt.h>
Haojian Zhuang3888c092012-02-21 11:51:13 +080031#include <linux/slab.h>
Russell Kinga0164a52012-01-19 11:55:21 +000032#include <linux/string.h>
Haojian Zhuang8bec2e92012-03-05 19:26:42 +080033#include <linux/of.h>
Richard Purdiee842f1c2006-03-27 01:16:46 -080034#include <linux/pm.h>
Russell Kinga0164a52012-01-19 11:55:21 +000035#include <linux/bitops.h>
Rob Herring23019a72012-03-20 14:33:19 -050036#include <linux/io.h>
Richard Purdiee842f1c2006-03-27 01:16:46 -080037
Russell Kinga09e64f2008-08-05 16:14:15 +010038#include <mach/hardware.h>
Rob Herring905cdc882012-02-23 14:27:58 +010039#include <mach/irqs.h>
Richard Purdiee842f1c2006-03-27 01:16:46 -080040
Haojian Zhuang3888c092012-02-21 11:51:13 +080041#if defined(CONFIG_ARCH_PXA) || defined(CONFIG_ARCH_MMP)
Russell Kinga0164a52012-01-19 11:55:21 +000042#include <mach/regs-rtc.h>
43#endif
44
Rob Herring8c0961b2015-05-12 16:23:23 -050045#include "rtc-sa1100.h"
46
Marcelo Roberto Jimeneza404ad12010-10-18 22:33:53 +010047#define RTC_DEF_DIVIDER (32768 - 1)
Richard Purdiee842f1c2006-03-27 01:16:46 -080048#define RTC_DEF_TRIM 0
Haojian Zhuang3888c092012-02-21 11:51:13 +080049#define RTC_FREQ 1024
Richard Purdiee842f1c2006-03-27 01:16:46 -080050
Richard Purdiee842f1c2006-03-27 01:16:46 -080051
David Howells7d12e782006-10-05 14:55:46 +010052static irqreturn_t sa1100_rtc_interrupt(int irq, void *dev_id)
Richard Purdiee842f1c2006-03-27 01:16:46 -080053{
Haojian Zhuang3888c092012-02-21 11:51:13 +080054 struct sa1100_rtc *info = dev_get_drvdata(dev_id);
55 struct rtc_device *rtc = info->rtc;
Richard Purdiee842f1c2006-03-27 01:16:46 -080056 unsigned int rtsr;
57 unsigned long events = 0;
58
Haojian Zhuang3888c092012-02-21 11:51:13 +080059 spin_lock(&info->lock);
Richard Purdiee842f1c2006-03-27 01:16:46 -080060
Russell Kinga0164a52012-01-19 11:55:21 +000061 rtsr = RTSR;
Richard Purdiee842f1c2006-03-27 01:16:46 -080062 /* clear interrupt sources */
Russell Kinga0164a52012-01-19 11:55:21 +000063 RTSR = 0;
Marcelo Roberto Jimenez7decaa52010-10-18 22:35:54 +010064 /* Fix for a nasty initialization problem the in SA11xx RTSR register.
65 * See also the comments in sa1100_rtc_probe(). */
66 if (rtsr & (RTSR_ALE | RTSR_HZE)) {
67 /* This is the original code, before there was the if test
68 * above. This code does not clear interrupts that were not
69 * enabled. */
Russell Kinga0164a52012-01-19 11:55:21 +000070 RTSR = (RTSR_AL | RTSR_HZ) & (rtsr >> 2);
Marcelo Roberto Jimenez7decaa52010-10-18 22:35:54 +010071 } else {
72 /* For some reason, it is possible to enter this routine
73 * without interruptions enabled, it has been tested with
74 * several units (Bug in SA11xx chip?).
75 *
76 * This situation leads to an infinite "loop" of interrupt
77 * routine calling and as a result the processor seems to
78 * lock on its first call to open(). */
Russell Kinga0164a52012-01-19 11:55:21 +000079 RTSR = RTSR_AL | RTSR_HZ;
Marcelo Roberto Jimenez7decaa52010-10-18 22:35:54 +010080 }
Richard Purdiee842f1c2006-03-27 01:16:46 -080081
82 /* clear alarm interrupt if it has occurred */
83 if (rtsr & RTSR_AL)
84 rtsr &= ~RTSR_ALE;
Russell Kinga0164a52012-01-19 11:55:21 +000085 RTSR = rtsr & (RTSR_ALE | RTSR_HZE);
Richard Purdiee842f1c2006-03-27 01:16:46 -080086
87 /* update irq data & counter */
88 if (rtsr & RTSR_AL)
89 events |= RTC_AF | RTC_IRQF;
90 if (rtsr & RTSR_HZ)
91 events |= RTC_UF | RTC_IRQF;
92
Russell Kinga0164a52012-01-19 11:55:21 +000093 rtc_update_irq(rtc, 1, events);
Richard Purdiee842f1c2006-03-27 01:16:46 -080094
Haojian Zhuang3888c092012-02-21 11:51:13 +080095 spin_unlock(&info->lock);
Richard Purdiee842f1c2006-03-27 01:16:46 -080096
97 return IRQ_HANDLED;
98}
99
Richard Purdiee842f1c2006-03-27 01:16:46 -0800100static int sa1100_rtc_open(struct device *dev)
101{
Haojian Zhuang3888c092012-02-21 11:51:13 +0800102 struct sa1100_rtc *info = dev_get_drvdata(dev);
103 struct rtc_device *rtc = info->rtc;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800104 int ret;
105
Linus Torvalds34800592012-03-27 16:41:24 -0700106 ret = request_irq(info->irq_1hz, sa1100_rtc_interrupt, 0, "rtc 1Hz", dev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800107 if (ret) {
Haojian Zhuang3888c092012-02-21 11:51:13 +0800108 dev_err(dev, "IRQ %d already in use.\n", info->irq_1hz);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800109 goto fail_ui;
110 }
Linus Torvalds34800592012-03-27 16:41:24 -0700111 ret = request_irq(info->irq_alarm, sa1100_rtc_interrupt, 0, "rtc Alrm", dev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800112 if (ret) {
Haojian Zhuang3888c092012-02-21 11:51:13 +0800113 dev_err(dev, "IRQ %d already in use.\n", info->irq_alarm);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800114 goto fail_ai;
115 }
Russell Kinga0164a52012-01-19 11:55:21 +0000116 rtc->max_user_freq = RTC_FREQ;
117 rtc_irq_set_freq(rtc, NULL, RTC_FREQ);
Marcelo Roberto Jimenezd2ccb522010-12-16 21:31:32 +0100118
Richard Purdiee842f1c2006-03-27 01:16:46 -0800119 return 0;
120
Richard Purdiee842f1c2006-03-27 01:16:46 -0800121 fail_ai:
Haojian Zhuang3888c092012-02-21 11:51:13 +0800122 free_irq(info->irq_1hz, dev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800123 fail_ui:
Haojian Zhuang8e8bbcb2012-02-23 23:36:37 +0800124 clk_disable_unprepare(info->clk);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800125 return ret;
126}
127
128static void sa1100_rtc_release(struct device *dev)
129{
Haojian Zhuang3888c092012-02-21 11:51:13 +0800130 struct sa1100_rtc *info = dev_get_drvdata(dev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800131
Haojian Zhuang3888c092012-02-21 11:51:13 +0800132 spin_lock_irq(&info->lock);
133 RTSR = 0;
134 spin_unlock_irq(&info->lock);
135
136 free_irq(info->irq_alarm, dev);
137 free_irq(info->irq_1hz, dev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800138}
139
John Stultz16380c12011-02-02 17:02:41 -0800140static int sa1100_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
141{
Haojian Zhuang3888c092012-02-21 11:51:13 +0800142 struct sa1100_rtc *info = dev_get_drvdata(dev);
143
144 spin_lock_irq(&info->lock);
John Stultz16380c12011-02-02 17:02:41 -0800145 if (enabled)
Russell Kinga0164a52012-01-19 11:55:21 +0000146 RTSR |= RTSR_ALE;
John Stultz16380c12011-02-02 17:02:41 -0800147 else
Russell Kinga0164a52012-01-19 11:55:21 +0000148 RTSR &= ~RTSR_ALE;
Haojian Zhuang3888c092012-02-21 11:51:13 +0800149 spin_unlock_irq(&info->lock);
John Stultz16380c12011-02-02 17:02:41 -0800150 return 0;
151}
152
Richard Purdiee842f1c2006-03-27 01:16:46 -0800153static int sa1100_rtc_read_time(struct device *dev, struct rtc_time *tm)
154{
Russell Kinga0164a52012-01-19 11:55:21 +0000155 rtc_time_to_tm(RCNR, tm);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800156 return 0;
157}
158
159static int sa1100_rtc_set_time(struct device *dev, struct rtc_time *tm)
160{
161 unsigned long time;
162 int ret;
163
164 ret = rtc_tm_to_time(tm, &time);
165 if (ret == 0)
Russell Kinga0164a52012-01-19 11:55:21 +0000166 RCNR = time;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800167 return ret;
168}
169
170static int sa1100_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
171{
Russell Kinga0164a52012-01-19 11:55:21 +0000172 u32 rtsr;
David Brownell32b49da2007-02-20 13:58:13 -0800173
Russell Kinga0164a52012-01-19 11:55:21 +0000174 rtsr = RTSR;
David Brownell32b49da2007-02-20 13:58:13 -0800175 alrm->enabled = (rtsr & RTSR_ALE) ? 1 : 0;
176 alrm->pending = (rtsr & RTSR_AL) ? 1 : 0;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800177 return 0;
178}
179
180static int sa1100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
181{
Haojian Zhuang3888c092012-02-21 11:51:13 +0800182 struct sa1100_rtc *info = dev_get_drvdata(dev);
Haojian Zhuang1d8c38c2012-02-20 19:49:30 +0800183 unsigned long time;
Russell Kinga0164a52012-01-19 11:55:21 +0000184 int ret;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800185
Haojian Zhuang3888c092012-02-21 11:51:13 +0800186 spin_lock_irq(&info->lock);
Haojian Zhuang1d8c38c2012-02-20 19:49:30 +0800187 ret = rtc_tm_to_time(&alrm->time, &time);
188 if (ret != 0)
189 goto out;
190 RTSR = RTSR & (RTSR_HZE|RTSR_ALE|RTSR_AL);
191 RTAR = time;
192 if (alrm->enabled)
193 RTSR |= RTSR_ALE;
194 else
195 RTSR &= ~RTSR_ALE;
196out:
Haojian Zhuang3888c092012-02-21 11:51:13 +0800197 spin_unlock_irq(&info->lock);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800198
Russell Kinga0164a52012-01-19 11:55:21 +0000199 return ret;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800200}
201
202static int sa1100_rtc_proc(struct device *dev, struct seq_file *seq)
203{
Russell Kinga0164a52012-01-19 11:55:21 +0000204 seq_printf(seq, "trim/divider\t\t: 0x%08x\n", (u32) RTTR);
205 seq_printf(seq, "RTSR\t\t\t: 0x%08x\n", (u32)RTSR);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800206
207 return 0;
208}
209
David Brownellff8371a2006-09-30 23:28:17 -0700210static const struct rtc_class_ops sa1100_rtc_ops = {
Richard Purdiee842f1c2006-03-27 01:16:46 -0800211 .open = sa1100_rtc_open,
Richard Purdiee842f1c2006-03-27 01:16:46 -0800212 .release = sa1100_rtc_release,
Richard Purdiee842f1c2006-03-27 01:16:46 -0800213 .read_time = sa1100_rtc_read_time,
214 .set_time = sa1100_rtc_set_time,
215 .read_alarm = sa1100_rtc_read_alarm,
216 .set_alarm = sa1100_rtc_set_alarm,
217 .proc = sa1100_rtc_proc,
John Stultz16380c12011-02-02 17:02:41 -0800218 .alarm_irq_enable = sa1100_rtc_alarm_irq_enable,
Richard Purdiee842f1c2006-03-27 01:16:46 -0800219};
220
Rob Herring8c0961b2015-05-12 16:23:23 -0500221int sa1100_rtc_init(struct platform_device *pdev, struct sa1100_rtc *info)
Richard Purdiee842f1c2006-03-27 01:16:46 -0800222{
Russell Kinga0164a52012-01-19 11:55:21 +0000223 struct rtc_device *rtc;
Rob Herring8c0961b2015-05-12 16:23:23 -0500224 int ret;
Haojian Zhuang3888c092012-02-21 11:51:13 +0800225
Rob Herring8c0961b2015-05-12 16:23:23 -0500226 spin_lock_init(&info->lock);
Haojian Zhuang3888c092012-02-21 11:51:13 +0800227
Jingoo Han55d735e2013-04-29 16:20:54 -0700228 info->clk = devm_clk_get(&pdev->dev, NULL);
Haojian Zhuang8e8bbcb2012-02-23 23:36:37 +0800229 if (IS_ERR(info->clk)) {
230 dev_err(&pdev->dev, "failed to find rtc clock source\n");
Jingoo Han55d735e2013-04-29 16:20:54 -0700231 return PTR_ERR(info->clk);
Haojian Zhuang8e8bbcb2012-02-23 23:36:37 +0800232 }
Richard Purdiee842f1c2006-03-27 01:16:46 -0800233
Chao Xie0cc0c382013-02-21 16:45:17 -0800234 ret = clk_prepare_enable(info->clk);
235 if (ret)
Jingoo Han66600bb2013-07-03 15:06:35 -0700236 return ret;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800237 /*
238 * According to the manual we should be able to let RTTR be zero
239 * and then a default diviser for a 32.768KHz clock is used.
240 * Apparently this doesn't work, at least for my SA1110 rev 5.
241 * If the clock divider is uninitialized then reset it to the
242 * default value to get the 1Hz clock.
243 */
Russell Kinga0164a52012-01-19 11:55:21 +0000244 if (RTTR == 0) {
245 RTTR = RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16);
246 dev_warn(&pdev->dev, "warning: "
247 "initializing default clock divider/trim value\n");
Richard Purdiee842f1c2006-03-27 01:16:46 -0800248 /* The current RTC value probably doesn't make sense either */
Russell Kinga0164a52012-01-19 11:55:21 +0000249 RCNR = 0;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800250 }
251
Jingoo Han55d735e2013-04-29 16:20:54 -0700252 rtc = devm_rtc_device_register(&pdev->dev, pdev->name, &sa1100_rtc_ops,
253 THIS_MODULE);
Haojian Zhuang3888c092012-02-21 11:51:13 +0800254 if (IS_ERR(rtc)) {
Rob Herring8c0961b2015-05-12 16:23:23 -0500255 clk_disable_unprepare(info->clk);
256 return PTR_ERR(rtc);
Haojian Zhuang3888c092012-02-21 11:51:13 +0800257 }
258 info->rtc = rtc;
Russell Kinga0164a52012-01-19 11:55:21 +0000259
Marcelo Roberto Jimenez7decaa52010-10-18 22:35:54 +0100260 /* Fix for a nasty initialization problem the in SA11xx RTSR register.
261 * See also the comments in sa1100_rtc_interrupt().
262 *
263 * Sometimes bit 1 of the RTSR (RTSR_HZ) will wake up 1, which means an
264 * interrupt pending, even though interrupts were never enabled.
265 * In this case, this bit it must be reset before enabling
266 * interruptions to avoid a nonexistent interrupt to occur.
267 *
268 * In principle, the same problem would apply to bit 0, although it has
269 * never been observed to happen.
270 *
271 * This issue is addressed both here and in sa1100_rtc_interrupt().
272 * If the issue is not addressed here, in the times when the processor
273 * wakes up with the bit set there will be one spurious interrupt.
274 *
275 * The issue is also dealt with in sa1100_rtc_interrupt() to be on the
276 * safe side, once the condition that lead to this strange
277 * initialization is unknown and could in principle happen during
278 * normal processing.
279 *
280 * Notice that clearing bit 1 and 0 is accomplished by writting ONES to
281 * the corresponding bits in RTSR. */
Russell Kinga0164a52012-01-19 11:55:21 +0000282 RTSR = RTSR_AL | RTSR_HZ;
Marcelo Roberto Jimenez7decaa52010-10-18 22:35:54 +0100283
Richard Purdiee842f1c2006-03-27 01:16:46 -0800284 return 0;
Rob Herring8c0961b2015-05-12 16:23:23 -0500285}
286EXPORT_SYMBOL_GPL(sa1100_rtc_init);
287
288static int sa1100_rtc_probe(struct platform_device *pdev)
289{
290 struct sa1100_rtc *info;
291 int irq_1hz, irq_alarm;
292
293 irq_1hz = platform_get_irq_byname(pdev, "rtc 1Hz");
294 irq_alarm = platform_get_irq_byname(pdev, "rtc alarm");
295 if (irq_1hz < 0 || irq_alarm < 0)
296 return -ENODEV;
297
298 info = devm_kzalloc(&pdev->dev, sizeof(struct sa1100_rtc), GFP_KERNEL);
299 if (!info)
300 return -ENOMEM;
301 info->irq_1hz = irq_1hz;
302 info->irq_alarm = irq_alarm;
303
304 platform_set_drvdata(pdev, info);
305 device_init_wakeup(&pdev->dev, 1);
306
307 return sa1100_rtc_init(pdev, info);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800308}
309
310static int sa1100_rtc_remove(struct platform_device *pdev)
311{
Haojian Zhuang3888c092012-02-21 11:51:13 +0800312 struct sa1100_rtc *info = platform_get_drvdata(pdev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800313
Jingoo Han66600bb2013-07-03 15:06:35 -0700314 if (info)
Chao Xie0cc0c382013-02-21 16:45:17 -0800315 clk_disable_unprepare(info->clk);
Russell Kinga0164a52012-01-19 11:55:21 +0000316
Richard Purdiee842f1c2006-03-27 01:16:46 -0800317 return 0;
318}
319
Jingoo Hanaaa92fa2013-04-29 16:19:59 -0700320#ifdef CONFIG_PM_SLEEP
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800321static int sa1100_rtc_suspend(struct device *dev)
Russell King6bc54e62007-11-12 22:49:58 +0000322{
Haojian Zhuang3888c092012-02-21 11:51:13 +0800323 struct sa1100_rtc *info = dev_get_drvdata(dev);
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800324 if (device_may_wakeup(dev))
Haojian Zhuang3888c092012-02-21 11:51:13 +0800325 enable_irq_wake(info->irq_alarm);
Russell King6bc54e62007-11-12 22:49:58 +0000326 return 0;
327}
328
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800329static int sa1100_rtc_resume(struct device *dev)
Russell King6bc54e62007-11-12 22:49:58 +0000330{
Haojian Zhuang3888c092012-02-21 11:51:13 +0800331 struct sa1100_rtc *info = dev_get_drvdata(dev);
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800332 if (device_may_wakeup(dev))
Haojian Zhuang3888c092012-02-21 11:51:13 +0800333 disable_irq_wake(info->irq_alarm);
Russell King6bc54e62007-11-12 22:49:58 +0000334 return 0;
335}
Russell King6bc54e62007-11-12 22:49:58 +0000336#endif
337
Jingoo Hanaaa92fa2013-04-29 16:19:59 -0700338static SIMPLE_DEV_PM_OPS(sa1100_rtc_pm_ops, sa1100_rtc_suspend,
339 sa1100_rtc_resume);
340
Sachin Kamatc8a60462013-02-21 16:44:28 -0800341#ifdef CONFIG_OF
Jingoo Handee21a62014-06-06 14:36:13 -0700342static const struct of_device_id sa1100_rtc_dt_ids[] = {
Haojian Zhuang8bec2e92012-03-05 19:26:42 +0800343 { .compatible = "mrvl,sa1100-rtc", },
344 { .compatible = "mrvl,mmp-rtc", },
345 {}
346};
347MODULE_DEVICE_TABLE(of, sa1100_rtc_dt_ids);
Sachin Kamatc8a60462013-02-21 16:44:28 -0800348#endif
Haojian Zhuang8bec2e92012-03-05 19:26:42 +0800349
Richard Purdiee842f1c2006-03-27 01:16:46 -0800350static struct platform_driver sa1100_rtc_driver = {
351 .probe = sa1100_rtc_probe,
352 .remove = sa1100_rtc_remove,
Richard Purdiee842f1c2006-03-27 01:16:46 -0800353 .driver = {
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800354 .name = "sa1100-rtc",
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800355 .pm = &sa1100_rtc_pm_ops,
Sachin Kamatc8a60462013-02-21 16:44:28 -0800356 .of_match_table = of_match_ptr(sa1100_rtc_dt_ids),
Richard Purdiee842f1c2006-03-27 01:16:46 -0800357 },
358};
359
Axel Lin0c4eae62012-01-10 15:10:48 -0800360module_platform_driver(sa1100_rtc_driver);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800361
362MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
363MODULE_DESCRIPTION("SA11x0/PXA2xx Realtime Clock Driver (RTC)");
364MODULE_LICENSE("GPL");
Kay Sieversad28a072008-04-10 21:29:25 -0700365MODULE_ALIAS("platform:sa1100-rtc");