blob: fa512ed420179738600c9eda0a0839565370c784 [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;
Linus Torvalds34800592012-03-27 16:41:24 -0700113 ret = request_irq(info->irq_1hz, sa1100_rtc_interrupt, 0, "rtc 1Hz", dev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800114 if (ret) {
Haojian Zhuang3888c092012-02-21 11:51:13 +0800115 dev_err(dev, "IRQ %d already in use.\n", info->irq_1hz);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800116 goto fail_ui;
117 }
Linus Torvalds34800592012-03-27 16:41:24 -0700118 ret = request_irq(info->irq_alarm, sa1100_rtc_interrupt, 0, "rtc Alrm", dev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800119 if (ret) {
Haojian Zhuang3888c092012-02-21 11:51:13 +0800120 dev_err(dev, "IRQ %d already in use.\n", info->irq_alarm);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800121 goto fail_ai;
122 }
Russell Kinga0164a52012-01-19 11:55:21 +0000123 rtc->max_user_freq = RTC_FREQ;
124 rtc_irq_set_freq(rtc, NULL, RTC_FREQ);
Marcelo Roberto Jimenezd2ccb522010-12-16 21:31:32 +0100125
Richard Purdiee842f1c2006-03-27 01:16:46 -0800126 return 0;
127
Richard Purdiee842f1c2006-03-27 01:16:46 -0800128 fail_ai:
Haojian Zhuang3888c092012-02-21 11:51:13 +0800129 free_irq(info->irq_1hz, dev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800130 fail_ui:
Haojian Zhuang8e8bbcb2012-02-23 23:36:37 +0800131 clk_disable_unprepare(info->clk);
132 fail_clk:
Richard Purdiee842f1c2006-03-27 01:16:46 -0800133 return ret;
134}
135
136static void sa1100_rtc_release(struct device *dev)
137{
Haojian Zhuang3888c092012-02-21 11:51:13 +0800138 struct sa1100_rtc *info = dev_get_drvdata(dev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800139
Haojian Zhuang3888c092012-02-21 11:51:13 +0800140 spin_lock_irq(&info->lock);
141 RTSR = 0;
142 spin_unlock_irq(&info->lock);
143
144 free_irq(info->irq_alarm, dev);
145 free_irq(info->irq_1hz, dev);
Haojian Zhuang8e8bbcb2012-02-23 23:36:37 +0800146 clk_disable_unprepare(info->clk);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800147}
148
John Stultz16380c12011-02-02 17:02:41 -0800149static int sa1100_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
150{
Haojian Zhuang3888c092012-02-21 11:51:13 +0800151 struct sa1100_rtc *info = dev_get_drvdata(dev);
152
153 spin_lock_irq(&info->lock);
John Stultz16380c12011-02-02 17:02:41 -0800154 if (enabled)
Russell Kinga0164a52012-01-19 11:55:21 +0000155 RTSR |= RTSR_ALE;
John Stultz16380c12011-02-02 17:02:41 -0800156 else
Russell Kinga0164a52012-01-19 11:55:21 +0000157 RTSR &= ~RTSR_ALE;
Haojian Zhuang3888c092012-02-21 11:51:13 +0800158 spin_unlock_irq(&info->lock);
John Stultz16380c12011-02-02 17:02:41 -0800159 return 0;
160}
161
Richard Purdiee842f1c2006-03-27 01:16:46 -0800162static int sa1100_rtc_read_time(struct device *dev, struct rtc_time *tm)
163{
Russell Kinga0164a52012-01-19 11:55:21 +0000164 rtc_time_to_tm(RCNR, tm);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800165 return 0;
166}
167
168static int sa1100_rtc_set_time(struct device *dev, struct rtc_time *tm)
169{
170 unsigned long time;
171 int ret;
172
173 ret = rtc_tm_to_time(tm, &time);
174 if (ret == 0)
Russell Kinga0164a52012-01-19 11:55:21 +0000175 RCNR = time;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800176 return ret;
177}
178
179static int sa1100_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
180{
Russell Kinga0164a52012-01-19 11:55:21 +0000181 u32 rtsr;
David Brownell32b49da2007-02-20 13:58:13 -0800182
Russell Kinga0164a52012-01-19 11:55:21 +0000183 rtsr = RTSR;
David Brownell32b49da2007-02-20 13:58:13 -0800184 alrm->enabled = (rtsr & RTSR_ALE) ? 1 : 0;
185 alrm->pending = (rtsr & RTSR_AL) ? 1 : 0;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800186 return 0;
187}
188
189static int sa1100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
190{
Haojian Zhuang3888c092012-02-21 11:51:13 +0800191 struct sa1100_rtc *info = dev_get_drvdata(dev);
Haojian Zhuang1d8c38c2012-02-20 19:49:30 +0800192 unsigned long time;
Russell Kinga0164a52012-01-19 11:55:21 +0000193 int ret;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800194
Haojian Zhuang3888c092012-02-21 11:51:13 +0800195 spin_lock_irq(&info->lock);
Haojian Zhuang1d8c38c2012-02-20 19:49:30 +0800196 ret = rtc_tm_to_time(&alrm->time, &time);
197 if (ret != 0)
198 goto out;
199 RTSR = RTSR & (RTSR_HZE|RTSR_ALE|RTSR_AL);
200 RTAR = time;
201 if (alrm->enabled)
202 RTSR |= RTSR_ALE;
203 else
204 RTSR &= ~RTSR_ALE;
205out:
Haojian Zhuang3888c092012-02-21 11:51:13 +0800206 spin_unlock_irq(&info->lock);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800207
Russell Kinga0164a52012-01-19 11:55:21 +0000208 return ret;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800209}
210
211static int sa1100_rtc_proc(struct device *dev, struct seq_file *seq)
212{
Russell Kinga0164a52012-01-19 11:55:21 +0000213 seq_printf(seq, "trim/divider\t\t: 0x%08x\n", (u32) RTTR);
214 seq_printf(seq, "RTSR\t\t\t: 0x%08x\n", (u32)RTSR);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800215
216 return 0;
217}
218
David Brownellff8371a2006-09-30 23:28:17 -0700219static const struct rtc_class_ops sa1100_rtc_ops = {
Richard Purdiee842f1c2006-03-27 01:16:46 -0800220 .open = sa1100_rtc_open,
Richard Purdiee842f1c2006-03-27 01:16:46 -0800221 .release = sa1100_rtc_release,
Richard Purdiee842f1c2006-03-27 01:16:46 -0800222 .read_time = sa1100_rtc_read_time,
223 .set_time = sa1100_rtc_set_time,
224 .read_alarm = sa1100_rtc_read_alarm,
225 .set_alarm = sa1100_rtc_set_alarm,
226 .proc = sa1100_rtc_proc,
John Stultz16380c12011-02-02 17:02:41 -0800227 .alarm_irq_enable = sa1100_rtc_alarm_irq_enable,
Richard Purdiee842f1c2006-03-27 01:16:46 -0800228};
229
230static int sa1100_rtc_probe(struct platform_device *pdev)
231{
Russell Kinga0164a52012-01-19 11:55:21 +0000232 struct rtc_device *rtc;
Haojian Zhuang3888c092012-02-21 11:51:13 +0800233 struct sa1100_rtc *info;
234 int irq_1hz, irq_alarm, ret = 0;
235
236 irq_1hz = platform_get_irq_byname(pdev, "rtc 1Hz");
237 irq_alarm = platform_get_irq_byname(pdev, "rtc alarm");
238 if (irq_1hz < 0 || irq_alarm < 0)
239 return -ENODEV;
240
241 info = kzalloc(sizeof(struct sa1100_rtc), GFP_KERNEL);
242 if (!info)
243 return -ENOMEM;
Haojian Zhuang8e8bbcb2012-02-23 23:36:37 +0800244 info->clk = clk_get(&pdev->dev, NULL);
245 if (IS_ERR(info->clk)) {
246 dev_err(&pdev->dev, "failed to find rtc clock source\n");
247 ret = PTR_ERR(info->clk);
248 goto err_clk;
249 }
Haojian Zhuang3888c092012-02-21 11:51:13 +0800250 info->irq_1hz = irq_1hz;
251 info->irq_alarm = irq_alarm;
252 spin_lock_init(&info->lock);
253 platform_set_drvdata(pdev, info);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800254
255 /*
256 * According to the manual we should be able to let RTTR be zero
257 * and then a default diviser for a 32.768KHz clock is used.
258 * Apparently this doesn't work, at least for my SA1110 rev 5.
259 * If the clock divider is uninitialized then reset it to the
260 * default value to get the 1Hz clock.
261 */
Russell Kinga0164a52012-01-19 11:55:21 +0000262 if (RTTR == 0) {
263 RTTR = RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16);
264 dev_warn(&pdev->dev, "warning: "
265 "initializing default clock divider/trim value\n");
Richard Purdiee842f1c2006-03-27 01:16:46 -0800266 /* The current RTC value probably doesn't make sense either */
Russell Kinga0164a52012-01-19 11:55:21 +0000267 RCNR = 0;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800268 }
269
Uli Luckase5a2c9c2008-06-18 09:54:03 +0100270 device_init_wakeup(&pdev->dev, 1);
271
Russell Kinga0164a52012-01-19 11:55:21 +0000272 rtc = rtc_device_register(pdev->name, &pdev->dev, &sa1100_rtc_ops,
273 THIS_MODULE);
274
Haojian Zhuang3888c092012-02-21 11:51:13 +0800275 if (IS_ERR(rtc)) {
276 ret = PTR_ERR(rtc);
277 goto err_dev;
278 }
279 info->rtc = rtc;
Russell Kinga0164a52012-01-19 11:55:21 +0000280
Marcelo Roberto Jimenez7decaa52010-10-18 22:35:54 +0100281 /* Fix for a nasty initialization problem the in SA11xx RTSR register.
282 * See also the comments in sa1100_rtc_interrupt().
283 *
284 * Sometimes bit 1 of the RTSR (RTSR_HZ) will wake up 1, which means an
285 * interrupt pending, even though interrupts were never enabled.
286 * In this case, this bit it must be reset before enabling
287 * interruptions to avoid a nonexistent interrupt to occur.
288 *
289 * In principle, the same problem would apply to bit 0, although it has
290 * never been observed to happen.
291 *
292 * This issue is addressed both here and in sa1100_rtc_interrupt().
293 * If the issue is not addressed here, in the times when the processor
294 * wakes up with the bit set there will be one spurious interrupt.
295 *
296 * The issue is also dealt with in sa1100_rtc_interrupt() to be on the
297 * safe side, once the condition that lead to this strange
298 * initialization is unknown and could in principle happen during
299 * normal processing.
300 *
301 * Notice that clearing bit 1 and 0 is accomplished by writting ONES to
302 * the corresponding bits in RTSR. */
Russell Kinga0164a52012-01-19 11:55:21 +0000303 RTSR = RTSR_AL | RTSR_HZ;
Marcelo Roberto Jimenez7decaa52010-10-18 22:35:54 +0100304
Richard Purdiee842f1c2006-03-27 01:16:46 -0800305 return 0;
Haojian Zhuang3888c092012-02-21 11:51:13 +0800306err_dev:
307 platform_set_drvdata(pdev, NULL);
Haojian Zhuang8e8bbcb2012-02-23 23:36:37 +0800308 clk_put(info->clk);
309err_clk:
Haojian Zhuang3888c092012-02-21 11:51:13 +0800310 kfree(info);
311 return ret;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800312}
313
314static int sa1100_rtc_remove(struct platform_device *pdev)
315{
Haojian Zhuang3888c092012-02-21 11:51:13 +0800316 struct sa1100_rtc *info = platform_get_drvdata(pdev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800317
Haojian Zhuang3888c092012-02-21 11:51:13 +0800318 if (info) {
319 rtc_device_unregister(info->rtc);
Haojian Zhuang8e8bbcb2012-02-23 23:36:37 +0800320 clk_put(info->clk);
Haojian Zhuang3888c092012-02-21 11:51:13 +0800321 platform_set_drvdata(pdev, NULL);
322 kfree(info);
323 }
Russell Kinga0164a52012-01-19 11:55:21 +0000324
Richard Purdiee842f1c2006-03-27 01:16:46 -0800325 return 0;
326}
327
Russell King6bc54e62007-11-12 22:49:58 +0000328#ifdef CONFIG_PM
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800329static int sa1100_rtc_suspend(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 enable_irq_wake(info->irq_alarm);
Russell King6bc54e62007-11-12 22:49:58 +0000334 return 0;
335}
336
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800337static int sa1100_rtc_resume(struct device *dev)
Russell King6bc54e62007-11-12 22:49:58 +0000338{
Haojian Zhuang3888c092012-02-21 11:51:13 +0800339 struct sa1100_rtc *info = dev_get_drvdata(dev);
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800340 if (device_may_wakeup(dev))
Haojian Zhuang3888c092012-02-21 11:51:13 +0800341 disable_irq_wake(info->irq_alarm);
Russell King6bc54e62007-11-12 22:49:58 +0000342 return 0;
343}
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800344
Alexey Dobriyan47145212009-12-14 18:00:08 -0800345static const struct dev_pm_ops sa1100_rtc_pm_ops = {
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800346 .suspend = sa1100_rtc_suspend,
347 .resume = sa1100_rtc_resume,
348};
Russell King6bc54e62007-11-12 22:49:58 +0000349#endif
350
Haojian Zhuang8bec2e92012-03-05 19:26:42 +0800351static struct of_device_id sa1100_rtc_dt_ids[] = {
352 { .compatible = "mrvl,sa1100-rtc", },
353 { .compatible = "mrvl,mmp-rtc", },
354 {}
355};
356MODULE_DEVICE_TABLE(of, sa1100_rtc_dt_ids);
357
Richard Purdiee842f1c2006-03-27 01:16:46 -0800358static struct platform_driver sa1100_rtc_driver = {
359 .probe = sa1100_rtc_probe,
360 .remove = sa1100_rtc_remove,
Richard Purdiee842f1c2006-03-27 01:16:46 -0800361 .driver = {
Haojian Zhuang5d027cd2009-07-21 14:31:09 +0800362 .name = "sa1100-rtc",
363#ifdef CONFIG_PM
364 .pm = &sa1100_rtc_pm_ops,
365#endif
Haojian Zhuang8bec2e92012-03-05 19:26:42 +0800366 .of_match_table = sa1100_rtc_dt_ids,
Richard Purdiee842f1c2006-03-27 01:16:46 -0800367 },
368};
369
Axel Lin0c4eae62012-01-10 15:10:48 -0800370module_platform_driver(sa1100_rtc_driver);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800371
372MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
373MODULE_DESCRIPTION("SA11x0/PXA2xx Realtime Clock Driver (RTC)");
374MODULE_LICENSE("GPL");
Kay Sieversad28a072008-04-10 21:29:25 -0700375MODULE_ALIAS("platform:sa1100-rtc");