blob: e443b7850ede85bd49aa2e1e45fcc3c3d0d45bcf [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>
Richard Purdiee842f1c2006-03-27 01:16:46 -080036
Russell Kinga09e64f2008-08-05 16:14:15 +010037#include <mach/hardware.h>
Richard Purdiee842f1c2006-03-27 01:16:46 -080038#include <asm/irq.h>
Richard Purdiee842f1c2006-03-27 01:16:46 -080039
Haojian Zhuang3888c092012-02-21 11:51:13 +080040#if defined(CONFIG_ARCH_PXA) || defined(CONFIG_ARCH_MMP)
Russell Kinga0164a52012-01-19 11:55:21 +000041#include <mach/regs-rtc.h>
42#endif
43
Marcelo Roberto Jimeneza404ad12010-10-18 22:33:53 +010044#define RTC_DEF_DIVIDER (32768 - 1)
Richard Purdiee842f1c2006-03-27 01:16:46 -080045#define RTC_DEF_TRIM 0
Haojian Zhuang3888c092012-02-21 11:51:13 +080046#define RTC_FREQ 1024
Richard Purdiee842f1c2006-03-27 01:16:46 -080047
Haojian Zhuang3888c092012-02-21 11:51:13 +080048struct sa1100_rtc {
49 spinlock_t lock;
50 int irq_1hz;
51 int irq_alarm;
52 struct rtc_device *rtc;
Haojian Zhuang8e8bbcb2012-02-23 23:36:37 +080053 struct clk *clk;
Haojian Zhuang3888c092012-02-21 11:51:13 +080054};
Richard Purdiee842f1c2006-03-27 01:16:46 -080055
David Howells7d12e782006-10-05 14:55:46 +010056static irqreturn_t sa1100_rtc_interrupt(int irq, void *dev_id)
Richard Purdiee842f1c2006-03-27 01:16:46 -080057{
Haojian Zhuang3888c092012-02-21 11:51:13 +080058 struct sa1100_rtc *info = dev_get_drvdata(dev_id);
59 struct rtc_device *rtc = info->rtc;
Richard Purdiee842f1c2006-03-27 01:16:46 -080060 unsigned int rtsr;
61 unsigned long events = 0;
62
Haojian Zhuang3888c092012-02-21 11:51:13 +080063 spin_lock(&info->lock);
Richard Purdiee842f1c2006-03-27 01:16:46 -080064
Russell Kinga0164a52012-01-19 11:55:21 +000065 rtsr = RTSR;
Richard Purdiee842f1c2006-03-27 01:16:46 -080066 /* clear interrupt sources */
Russell Kinga0164a52012-01-19 11:55:21 +000067 RTSR = 0;
Marcelo Roberto Jimenez7decaa52010-10-18 22:35:54 +010068 /* Fix for a nasty initialization problem the in SA11xx RTSR register.
69 * See also the comments in sa1100_rtc_probe(). */
70 if (rtsr & (RTSR_ALE | RTSR_HZE)) {
71 /* This is the original code, before there was the if test
72 * above. This code does not clear interrupts that were not
73 * enabled. */
Russell Kinga0164a52012-01-19 11:55:21 +000074 RTSR = (RTSR_AL | RTSR_HZ) & (rtsr >> 2);
Marcelo Roberto Jimenez7decaa52010-10-18 22:35:54 +010075 } else {
76 /* For some reason, it is possible to enter this routine
77 * without interruptions enabled, it has been tested with
78 * several units (Bug in SA11xx chip?).
79 *
80 * This situation leads to an infinite "loop" of interrupt
81 * routine calling and as a result the processor seems to
82 * lock on its first call to open(). */
Russell Kinga0164a52012-01-19 11:55:21 +000083 RTSR = RTSR_AL | RTSR_HZ;
Marcelo Roberto Jimenez7decaa52010-10-18 22:35:54 +010084 }
Richard Purdiee842f1c2006-03-27 01:16:46 -080085
86 /* clear alarm interrupt if it has occurred */
87 if (rtsr & RTSR_AL)
88 rtsr &= ~RTSR_ALE;
Russell Kinga0164a52012-01-19 11:55:21 +000089 RTSR = rtsr & (RTSR_ALE | RTSR_HZE);
Richard Purdiee842f1c2006-03-27 01:16:46 -080090
91 /* update irq data & counter */
92 if (rtsr & RTSR_AL)
93 events |= RTC_AF | RTC_IRQF;
94 if (rtsr & RTSR_HZ)
95 events |= RTC_UF | RTC_IRQF;
96
Russell Kinga0164a52012-01-19 11:55:21 +000097 rtc_update_irq(rtc, 1, events);
Richard Purdiee842f1c2006-03-27 01:16:46 -080098
Haojian Zhuang3888c092012-02-21 11:51:13 +080099 spin_unlock(&info->lock);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800100
101 return IRQ_HANDLED;
102}
103
Richard Purdiee842f1c2006-03-27 01:16:46 -0800104static int sa1100_rtc_open(struct device *dev)
105{
Haojian Zhuang3888c092012-02-21 11:51:13 +0800106 struct sa1100_rtc *info = dev_get_drvdata(dev);
107 struct rtc_device *rtc = info->rtc;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800108 int ret;
109
Haojian Zhuang8e8bbcb2012-02-23 23:36:37 +0800110 ret = clk_prepare_enable(info->clk);
111 if (ret)
112 goto fail_clk;
Haojian Zhuang3888c092012-02-21 11:51:13 +0800113 ret = request_irq(info->irq_1hz, sa1100_rtc_interrupt, IRQF_DISABLED,
Russell Kinga0164a52012-01-19 11:55:21 +0000114 "rtc 1Hz", dev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800115 if (ret) {
Haojian Zhuang3888c092012-02-21 11:51:13 +0800116 dev_err(dev, "IRQ %d already in use.\n", info->irq_1hz);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800117 goto fail_ui;
118 }
Haojian Zhuang3888c092012-02-21 11:51:13 +0800119 ret = request_irq(info->irq_alarm, sa1100_rtc_interrupt, IRQF_DISABLED,
Russell Kinga0164a52012-01-19 11:55:21 +0000120 "rtc Alrm", dev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800121 if (ret) {
Haojian Zhuang3888c092012-02-21 11:51:13 +0800122 dev_err(dev, "IRQ %d already in use.\n", info->irq_alarm);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800123 goto fail_ai;
124 }
Russell Kinga0164a52012-01-19 11:55:21 +0000125 rtc->max_user_freq = RTC_FREQ;
126 rtc_irq_set_freq(rtc, NULL, RTC_FREQ);
Marcelo Roberto Jimenezd2ccb522010-12-16 21:31:32 +0100127
Richard Purdiee842f1c2006-03-27 01:16:46 -0800128 return 0;
129
Richard Purdiee842f1c2006-03-27 01:16:46 -0800130 fail_ai:
Haojian Zhuang3888c092012-02-21 11:51:13 +0800131 free_irq(info->irq_1hz, dev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800132 fail_ui:
Haojian Zhuang8e8bbcb2012-02-23 23:36:37 +0800133 clk_disable_unprepare(info->clk);
134 fail_clk:
Richard Purdiee842f1c2006-03-27 01:16:46 -0800135 return ret;
136}
137
138static void sa1100_rtc_release(struct device *dev)
139{
Haojian Zhuang3888c092012-02-21 11:51:13 +0800140 struct sa1100_rtc *info = dev_get_drvdata(dev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800141
Haojian Zhuang3888c092012-02-21 11:51:13 +0800142 spin_lock_irq(&info->lock);
143 RTSR = 0;
144 spin_unlock_irq(&info->lock);
145
146 free_irq(info->irq_alarm, dev);
147 free_irq(info->irq_1hz, dev);
Haojian Zhuang8e8bbcb2012-02-23 23:36:37 +0800148 clk_disable_unprepare(info->clk);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800149}
150
John Stultz16380c12011-02-02 17:02:41 -0800151static int sa1100_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
152{
Haojian Zhuang3888c092012-02-21 11:51:13 +0800153 struct sa1100_rtc *info = dev_get_drvdata(dev);
154
155 spin_lock_irq(&info->lock);
John Stultz16380c12011-02-02 17:02:41 -0800156 if (enabled)
Russell Kinga0164a52012-01-19 11:55:21 +0000157 RTSR |= RTSR_ALE;
John Stultz16380c12011-02-02 17:02:41 -0800158 else
Russell Kinga0164a52012-01-19 11:55:21 +0000159 RTSR &= ~RTSR_ALE;
Haojian Zhuang3888c092012-02-21 11:51:13 +0800160 spin_unlock_irq(&info->lock);
John Stultz16380c12011-02-02 17:02:41 -0800161 return 0;
162}
163
Richard Purdiee842f1c2006-03-27 01:16:46 -0800164static int sa1100_rtc_read_time(struct device *dev, struct rtc_time *tm)
165{
Russell Kinga0164a52012-01-19 11:55:21 +0000166 rtc_time_to_tm(RCNR, tm);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800167 return 0;
168}
169
170static int sa1100_rtc_set_time(struct device *dev, struct rtc_time *tm)
171{
172 unsigned long time;
173 int ret;
174
175 ret = rtc_tm_to_time(tm, &time);
176 if (ret == 0)
Russell Kinga0164a52012-01-19 11:55:21 +0000177 RCNR = time;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800178 return ret;
179}
180
181static int sa1100_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
182{
Russell Kinga0164a52012-01-19 11:55:21 +0000183 u32 rtsr;
David Brownell32b49da2007-02-20 13:58:13 -0800184
Russell Kinga0164a52012-01-19 11:55:21 +0000185 rtsr = RTSR;
David Brownell32b49da2007-02-20 13:58:13 -0800186 alrm->enabled = (rtsr & RTSR_ALE) ? 1 : 0;
187 alrm->pending = (rtsr & RTSR_AL) ? 1 : 0;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800188 return 0;
189}
190
191static int sa1100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
192{
Haojian Zhuang3888c092012-02-21 11:51:13 +0800193 struct sa1100_rtc *info = dev_get_drvdata(dev);
Haojian Zhuang1d8c38c2012-02-20 19:49:30 +0800194 unsigned long time;
Russell Kinga0164a52012-01-19 11:55:21 +0000195 int ret;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800196
Haojian Zhuang3888c092012-02-21 11:51:13 +0800197 spin_lock_irq(&info->lock);
Haojian Zhuang1d8c38c2012-02-20 19:49:30 +0800198 ret = rtc_tm_to_time(&alrm->time, &time);
199 if (ret != 0)
200 goto out;
201 RTSR = RTSR & (RTSR_HZE|RTSR_ALE|RTSR_AL);
202 RTAR = time;
203 if (alrm->enabled)
204 RTSR |= RTSR_ALE;
205 else
206 RTSR &= ~RTSR_ALE;
207out:
Haojian Zhuang3888c092012-02-21 11:51:13 +0800208 spin_unlock_irq(&info->lock);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800209
Russell Kinga0164a52012-01-19 11:55:21 +0000210 return ret;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800211}
212
213static int sa1100_rtc_proc(struct device *dev, struct seq_file *seq)
214{
Russell Kinga0164a52012-01-19 11:55:21 +0000215 seq_printf(seq, "trim/divider\t\t: 0x%08x\n", (u32) RTTR);
216 seq_printf(seq, "RTSR\t\t\t: 0x%08x\n", (u32)RTSR);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800217
218 return 0;
219}
220
David Brownellff8371a2006-09-30 23:28:17 -0700221static const struct rtc_class_ops sa1100_rtc_ops = {
Richard Purdiee842f1c2006-03-27 01:16:46 -0800222 .open = sa1100_rtc_open,
Richard Purdiee842f1c2006-03-27 01:16:46 -0800223 .release = sa1100_rtc_release,
Richard Purdiee842f1c2006-03-27 01:16:46 -0800224 .read_time = sa1100_rtc_read_time,
225 .set_time = sa1100_rtc_set_time,
226 .read_alarm = sa1100_rtc_read_alarm,
227 .set_alarm = sa1100_rtc_set_alarm,
228 .proc = sa1100_rtc_proc,
John Stultz16380c12011-02-02 17:02:41 -0800229 .alarm_irq_enable = sa1100_rtc_alarm_irq_enable,
Richard Purdiee842f1c2006-03-27 01:16:46 -0800230};
231
232static int sa1100_rtc_probe(struct platform_device *pdev)
233{
Russell Kinga0164a52012-01-19 11:55:21 +0000234 struct rtc_device *rtc;
Haojian Zhuang3888c092012-02-21 11:51:13 +0800235 struct sa1100_rtc *info;
236 int irq_1hz, irq_alarm, ret = 0;
237
238 irq_1hz = platform_get_irq_byname(pdev, "rtc 1Hz");
239 irq_alarm = platform_get_irq_byname(pdev, "rtc alarm");
240 if (irq_1hz < 0 || irq_alarm < 0)
241 return -ENODEV;
242
243 info = kzalloc(sizeof(struct sa1100_rtc), GFP_KERNEL);
244 if (!info)
245 return -ENOMEM;
Haojian Zhuang8e8bbcb2012-02-23 23:36:37 +0800246 info->clk = clk_get(&pdev->dev, NULL);
247 if (IS_ERR(info->clk)) {
248 dev_err(&pdev->dev, "failed to find rtc clock source\n");
249 ret = PTR_ERR(info->clk);
250 goto err_clk;
251 }
Haojian Zhuang3888c092012-02-21 11:51:13 +0800252 info->irq_1hz = irq_1hz;
253 info->irq_alarm = irq_alarm;
254 spin_lock_init(&info->lock);
255 platform_set_drvdata(pdev, info);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800256
257 /*
258 * According to the manual we should be able to let RTTR be zero
259 * and then a default diviser for a 32.768KHz clock is used.
260 * Apparently this doesn't work, at least for my SA1110 rev 5.
261 * If the clock divider is uninitialized then reset it to the
262 * default value to get the 1Hz clock.
263 */
Russell Kinga0164a52012-01-19 11:55:21 +0000264 if (RTTR == 0) {
265 RTTR = RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16);
266 dev_warn(&pdev->dev, "warning: "
267 "initializing default clock divider/trim value\n");
Richard Purdiee842f1c2006-03-27 01:16:46 -0800268 /* The current RTC value probably doesn't make sense either */
Russell Kinga0164a52012-01-19 11:55:21 +0000269 RCNR = 0;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800270 }
271
Uli Luckase5a2c9c2008-06-18 09:54:03 +0100272 device_init_wakeup(&pdev->dev, 1);
273
Russell Kinga0164a52012-01-19 11:55:21 +0000274 rtc = rtc_device_register(pdev->name, &pdev->dev, &sa1100_rtc_ops,
275 THIS_MODULE);
276
Haojian Zhuang3888c092012-02-21 11:51:13 +0800277 if (IS_ERR(rtc)) {
278 ret = PTR_ERR(rtc);
279 goto err_dev;
280 }
281 info->rtc = rtc;
Russell Kinga0164a52012-01-19 11:55:21 +0000282
Marcelo Roberto Jimenez7decaa52010-10-18 22:35:54 +0100283 /* Fix for a nasty initialization problem the in SA11xx RTSR register.
284 * See also the comments in sa1100_rtc_interrupt().
285 *
286 * Sometimes bit 1 of the RTSR (RTSR_HZ) will wake up 1, which means an
287 * interrupt pending, even though interrupts were never enabled.
288 * In this case, this bit it must be reset before enabling
289 * interruptions to avoid a nonexistent interrupt to occur.
290 *
291 * In principle, the same problem would apply to bit 0, although it has
292 * never been observed to happen.
293 *
294 * This issue is addressed both here and in sa1100_rtc_interrupt().
295 * If the issue is not addressed here, in the times when the processor
296 * wakes up with the bit set there will be one spurious interrupt.
297 *
298 * The issue is also dealt with in sa1100_rtc_interrupt() to be on the
299 * safe side, once the condition that lead to this strange
300 * initialization is unknown and could in principle happen during
301 * normal processing.
302 *
303 * Notice that clearing bit 1 and 0 is accomplished by writting ONES to
304 * the corresponding bits in RTSR. */
Russell Kinga0164a52012-01-19 11:55:21 +0000305 RTSR = RTSR_AL | RTSR_HZ;
Marcelo Roberto Jimenez7decaa52010-10-18 22:35:54 +0100306
Richard Purdiee842f1c2006-03-27 01:16:46 -0800307 return 0;
Haojian Zhuang3888c092012-02-21 11:51:13 +0800308err_dev:
309 platform_set_drvdata(pdev, NULL);
Haojian Zhuang8e8bbcb2012-02-23 23:36:37 +0800310 clk_put(info->clk);
311err_clk:
Haojian Zhuang3888c092012-02-21 11:51:13 +0800312 kfree(info);
313 return ret;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800314}
315
316static int sa1100_rtc_remove(struct platform_device *pdev)
317{
Haojian Zhuang3888c092012-02-21 11:51:13 +0800318 struct sa1100_rtc *info = platform_get_drvdata(pdev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800319
Haojian Zhuang3888c092012-02-21 11:51:13 +0800320 if (info) {
321 rtc_device_unregister(info->rtc);
Haojian Zhuang8e8bbcb2012-02-23 23:36:37 +0800322 clk_put(info->clk);
Haojian Zhuang3888c092012-02-21 11:51:13 +0800323 platform_set_drvdata(pdev, NULL);
324 kfree(info);
325 }
Russell Kinga0164a52012-01-19 11:55:21 +0000326
Richard Purdiee842f1c2006-03-27 01:16:46 -0800327 return 0;
328}
329
Russell King6bc54e62007-11-12 22:49:58 +0000330#ifdef CONFIG_PM
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800331static int sa1100_rtc_suspend(struct device *dev)
Russell King6bc54e62007-11-12 22:49:58 +0000332{
Haojian Zhuang3888c092012-02-21 11:51:13 +0800333 struct sa1100_rtc *info = dev_get_drvdata(dev);
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800334 if (device_may_wakeup(dev))
Haojian Zhuang3888c092012-02-21 11:51:13 +0800335 enable_irq_wake(info->irq_alarm);
Russell King6bc54e62007-11-12 22:49:58 +0000336 return 0;
337}
338
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800339static int sa1100_rtc_resume(struct device *dev)
Russell King6bc54e62007-11-12 22:49:58 +0000340{
Haojian Zhuang3888c092012-02-21 11:51:13 +0800341 struct sa1100_rtc *info = dev_get_drvdata(dev);
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800342 if (device_may_wakeup(dev))
Haojian Zhuang3888c092012-02-21 11:51:13 +0800343 disable_irq_wake(info->irq_alarm);
Russell King6bc54e62007-11-12 22:49:58 +0000344 return 0;
345}
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800346
Alexey Dobriyan47145212009-12-14 18:00:08 -0800347static const struct dev_pm_ops sa1100_rtc_pm_ops = {
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800348 .suspend = sa1100_rtc_suspend,
349 .resume = sa1100_rtc_resume,
350};
Russell King6bc54e62007-11-12 22:49:58 +0000351#endif
352
Haojian Zhuang8bec2e92012-03-05 19:26:42 +0800353static struct of_device_id sa1100_rtc_dt_ids[] = {
354 { .compatible = "mrvl,sa1100-rtc", },
355 { .compatible = "mrvl,mmp-rtc", },
356 {}
357};
358MODULE_DEVICE_TABLE(of, sa1100_rtc_dt_ids);
359
Richard Purdiee842f1c2006-03-27 01:16:46 -0800360static struct platform_driver sa1100_rtc_driver = {
361 .probe = sa1100_rtc_probe,
362 .remove = sa1100_rtc_remove,
Richard Purdiee842f1c2006-03-27 01:16:46 -0800363 .driver = {
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800364 .name = "sa1100-rtc",
365#ifdef CONFIG_PM
366 .pm = &sa1100_rtc_pm_ops,
367#endif
Haojian Zhuang8bec2e92012-03-05 19:26:42 +0800368 .of_match_table = sa1100_rtc_dt_ids,
Richard Purdiee842f1c2006-03-27 01:16:46 -0800369 },
370};
371
Axel Lin0c4eae62012-01-10 15:10:48 -0800372module_platform_driver(sa1100_rtc_driver);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800373
374MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
375MODULE_DESCRIPTION("SA11x0/PXA2xx Realtime Clock Driver (RTC)");
376MODULE_LICENSE("GPL");
Kay Sieversad28a072008-04-10 21:29:25 -0700377MODULE_ALIAS("platform:sa1100-rtc");