1 From 21a0c050c7471b9d87e720a84f6733bbe8e19835 Mon Sep 17 00:00:00 2001
2 From: Lars-Peter Clausen <lars@metafoo.de>
3 Date: Sat, 19 Jun 2010 18:29:50 +0000
4 Subject: [PATCH] RTC: Add JZ4740 RTC driver
6 Add support for the RTC unit on JZ4740 SoCs.
8 Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
9 Cc: Alessandro Zummo <a.zummo@towertech.it>
10 Cc: Paul Gortmaker <p_gortmaker@yahoo.com>
11 Cc: rtc-linux@googlegroups.com
12 Acked-by: Wan ZongShun <mcuos.com@gmail.com>
13 Cc: linux-mips@linux-mips.org
14 Cc: linux-kernel@vger.kernel.org
15 Cc: Alessandro Zummo <a.zummo@towertech.it>,
16 Patchwork: https://patchwork.linux-mips.org/patch/1424/
17 Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
19 drivers/rtc/Kconfig | 11 ++
20 drivers/rtc/Makefile | 1 +
21 drivers/rtc/rtc-jz4740.c | 345 ++++++++++++++++++++++++++++++++++++++++++++++
22 3 files changed, 357 insertions(+), 0 deletions(-)
23 create mode 100644 drivers/rtc/rtc-jz4740.c
25 --- a/drivers/rtc/Kconfig
26 +++ b/drivers/rtc/Kconfig
27 @@ -914,4 +914,15 @@ config RTC_DRV_MPC5121
28 This driver can also be built as a module. If so, the module
29 will be called rtc-mpc5121.
31 +config RTC_DRV_JZ4740
32 + tristate "Ingenic JZ4740 SoC"
33 + depends on RTC_CLASS
34 + depends on MACH_JZ4740
36 + If you say yes here you get support for the Ingenic JZ4740 SoC RTC
39 + This driver can also be buillt as a module. If so, the module
40 + will be called rtc-jz4740.
43 --- a/drivers/rtc/Makefile
44 +++ b/drivers/rtc/Makefile
45 @@ -47,6 +47,7 @@ obj-$(CONFIG_RTC_DRV_EP93XX) += rtc-ep93
46 obj-$(CONFIG_RTC_DRV_FM3130) += rtc-fm3130.o
47 obj-$(CONFIG_RTC_DRV_GENERIC) += rtc-generic.o
48 obj-$(CONFIG_RTC_DRV_ISL1208) += rtc-isl1208.o
49 +obj-$(CONFIG_RTC_DRV_JZ4740) += rtc-jz4740.o
50 obj-$(CONFIG_RTC_DRV_M41T80) += rtc-m41t80.o
51 obj-$(CONFIG_RTC_DRV_M41T94) += rtc-m41t94.o
52 obj-$(CONFIG_RTC_DRV_M48T35) += rtc-m48t35.o
54 +++ b/drivers/rtc/rtc-jz4740.c
57 + * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
58 + * JZ4740 SoC RTC driver
60 + * This program is free software; you can redistribute it and/or modify it
61 + * under the terms of the GNU General Public License as published by the
62 + * Free Software Foundation; either version 2 of the License, or (at your
63 + * option) any later version.
65 + * You should have received a copy of the GNU General Public License along
66 + * with this program; if not, write to the Free Software Foundation, Inc.,
67 + * 675 Mass Ave, Cambridge, MA 02139, USA.
71 +#include <linux/kernel.h>
72 +#include <linux/module.h>
73 +#include <linux/platform_device.h>
74 +#include <linux/rtc.h>
75 +#include <linux/slab.h>
76 +#include <linux/spinlock.h>
78 +#define JZ_REG_RTC_CTRL 0x00
79 +#define JZ_REG_RTC_SEC 0x04
80 +#define JZ_REG_RTC_SEC_ALARM 0x08
81 +#define JZ_REG_RTC_REGULATOR 0x0C
82 +#define JZ_REG_RTC_HIBERNATE 0x20
83 +#define JZ_REG_RTC_SCRATCHPAD 0x34
85 +#define JZ_RTC_CTRL_WRDY BIT(7)
86 +#define JZ_RTC_CTRL_1HZ BIT(6)
87 +#define JZ_RTC_CTRL_1HZ_IRQ BIT(5)
88 +#define JZ_RTC_CTRL_AF BIT(4)
89 +#define JZ_RTC_CTRL_AF_IRQ BIT(3)
90 +#define JZ_RTC_CTRL_AE BIT(2)
91 +#define JZ_RTC_CTRL_ENABLE BIT(0)
94 + struct resource *mem;
97 + struct rtc_device *rtc;
104 +static inline uint32_t jz4740_rtc_reg_read(struct jz4740_rtc *rtc, size_t reg)
106 + return readl(rtc->base + reg);
109 +static int jz4740_rtc_wait_write_ready(struct jz4740_rtc *rtc)
112 + int timeout = 1000;
115 + ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL);
116 + } while (!(ctrl & JZ_RTC_CTRL_WRDY) && --timeout);
118 + return timeout ? 0 : -EIO;
121 +static inline int jz4740_rtc_reg_write(struct jz4740_rtc *rtc, size_t reg,
125 + ret = jz4740_rtc_wait_write_ready(rtc);
127 + writel(val, rtc->base + reg);
132 +static int jz4740_rtc_ctrl_set_bits(struct jz4740_rtc *rtc, uint32_t mask,
136 + unsigned long flags;
139 + spin_lock_irqsave(&rtc->lock, flags);
141 + ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL);
143 + /* Don't clear interrupt flags by accident */
144 + ctrl |= JZ_RTC_CTRL_1HZ | JZ_RTC_CTRL_AF;
151 + ret = jz4740_rtc_reg_write(rtc, JZ_REG_RTC_CTRL, ctrl);
153 + spin_unlock_irqrestore(&rtc->lock, flags);
158 +static int jz4740_rtc_read_time(struct device *dev, struct rtc_time *time)
160 + struct jz4740_rtc *rtc = dev_get_drvdata(dev);
161 + uint32_t secs, secs2;
164 + /* If the seconds register is read while it is updated, it can contain a
165 + * bogus value. This can be avoided by making sure that two consecutive
166 + * reads have the same value.
168 + secs = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC);
169 + secs2 = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC);
171 + while (secs != secs2 && --timeout) {
173 + secs2 = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC);
179 + rtc_time_to_tm(secs, time);
181 + return rtc_valid_tm(time);
184 +static int jz4740_rtc_set_mmss(struct device *dev, unsigned long secs)
186 + struct jz4740_rtc *rtc = dev_get_drvdata(dev);
188 + return jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SEC, secs);
191 +static int jz4740_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
193 + struct jz4740_rtc *rtc = dev_get_drvdata(dev);
197 + secs = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC_ALARM);
199 + ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL);
201 + alrm->enabled = !!(ctrl & JZ_RTC_CTRL_AE);
202 + alrm->pending = !!(ctrl & JZ_RTC_CTRL_AF);
204 + rtc_time_to_tm(secs, &alrm->time);
206 + return rtc_valid_tm(&alrm->time);
209 +static int jz4740_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
212 + struct jz4740_rtc *rtc = dev_get_drvdata(dev);
213 + unsigned long secs;
215 + rtc_tm_to_time(&alrm->time, &secs);
217 + ret = jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SEC_ALARM, secs);
219 + ret = jz4740_rtc_ctrl_set_bits(rtc, JZ_RTC_CTRL_AE, alrm->enabled);
224 +static int jz4740_rtc_update_irq_enable(struct device *dev, unsigned int enable)
226 + struct jz4740_rtc *rtc = dev_get_drvdata(dev);
227 + return jz4740_rtc_ctrl_set_bits(rtc, JZ_RTC_CTRL_1HZ_IRQ, enable);
230 +static int jz4740_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
232 + struct jz4740_rtc *rtc = dev_get_drvdata(dev);
233 + return jz4740_rtc_ctrl_set_bits(rtc, JZ_RTC_CTRL_AF_IRQ, enable);
236 +static struct rtc_class_ops jz4740_rtc_ops = {
237 + .read_time = jz4740_rtc_read_time,
238 + .set_mmss = jz4740_rtc_set_mmss,
239 + .read_alarm = jz4740_rtc_read_alarm,
240 + .set_alarm = jz4740_rtc_set_alarm,
241 + .update_irq_enable = jz4740_rtc_update_irq_enable,
242 + .alarm_irq_enable = jz4740_rtc_alarm_irq_enable,
245 +static irqreturn_t jz4740_rtc_irq(int irq, void *data)
247 + struct jz4740_rtc *rtc = data;
249 + unsigned long events = 0;
251 + ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL);
253 + if (ctrl & JZ_RTC_CTRL_1HZ)
254 + events |= (RTC_UF | RTC_IRQF);
256 + if (ctrl & JZ_RTC_CTRL_AF)
257 + events |= (RTC_AF | RTC_IRQF);
259 + rtc_update_irq(rtc->rtc, 1, events);
261 + jz4740_rtc_ctrl_set_bits(rtc, JZ_RTC_CTRL_1HZ | JZ_RTC_CTRL_AF, false);
263 + return IRQ_HANDLED;
266 +void jz4740_rtc_poweroff(struct device *dev)
268 + struct jz4740_rtc *rtc = dev_get_drvdata(dev);
269 + jz4740_rtc_reg_write(rtc, JZ_REG_RTC_HIBERNATE, 1);
271 +EXPORT_SYMBOL_GPL(jz4740_rtc_poweroff);
273 +static int __devinit jz4740_rtc_probe(struct platform_device *pdev)
276 + struct jz4740_rtc *rtc;
277 + uint32_t scratchpad;
279 + rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
283 + rtc->irq = platform_get_irq(pdev, 0);
284 + if (rtc->irq < 0) {
286 + dev_err(&pdev->dev, "Failed to get platform irq\n");
290 + rtc->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
293 + dev_err(&pdev->dev, "Failed to get platform mmio memory\n");
297 + rtc->mem = request_mem_region(rtc->mem->start, resource_size(rtc->mem),
301 + dev_err(&pdev->dev, "Failed to request mmio memory region\n");
305 + rtc->base = ioremap_nocache(rtc->mem->start, resource_size(rtc->mem));
308 + dev_err(&pdev->dev, "Failed to ioremap mmio memory\n");
309 + goto err_release_mem_region;
312 + spin_lock_init(&rtc->lock);
314 + platform_set_drvdata(pdev, rtc);
316 + rtc->rtc = rtc_device_register(pdev->name, &pdev->dev, &jz4740_rtc_ops,
318 + if (IS_ERR(rtc->rtc)) {
319 + ret = PTR_ERR(rtc->rtc);
320 + dev_err(&pdev->dev, "Failed to register rtc device: %d\n", ret);
324 + ret = request_irq(rtc->irq, jz4740_rtc_irq, 0,
327 + dev_err(&pdev->dev, "Failed to request rtc irq: %d\n", ret);
328 + goto err_unregister_rtc;
331 + scratchpad = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SCRATCHPAD);
332 + if (scratchpad != 0x12345678) {
333 + ret = jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SCRATCHPAD, 0x12345678);
334 + ret = jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SEC, 0);
336 + dev_err(&pdev->dev, "Could not write write to RTC registers\n");
344 + free_irq(rtc->irq, rtc);
346 + rtc_device_unregister(rtc->rtc);
348 + platform_set_drvdata(pdev, NULL);
349 + iounmap(rtc->base);
350 +err_release_mem_region:
351 + release_mem_region(rtc->mem->start, resource_size(rtc->mem));
358 +static int __devexit jz4740_rtc_remove(struct platform_device *pdev)
360 + struct jz4740_rtc *rtc = platform_get_drvdata(pdev);
362 + free_irq(rtc->irq, rtc);
364 + rtc_device_unregister(rtc->rtc);
366 + iounmap(rtc->base);
367 + release_mem_region(rtc->mem->start, resource_size(rtc->mem));
371 + platform_set_drvdata(pdev, NULL);
376 +struct platform_driver jz4740_rtc_driver = {
377 + .probe = jz4740_rtc_probe,
378 + .remove = __devexit_p(jz4740_rtc_remove),
380 + .name = "jz4740-rtc",
381 + .owner = THIS_MODULE,
385 +static int __init jz4740_rtc_init(void)
387 + return platform_driver_register(&jz4740_rtc_driver);
389 +module_init(jz4740_rtc_init);
391 +static void __exit jz4740_rtc_exit(void)
393 + platform_driver_unregister(&jz4740_rtc_driver);
395 +module_exit(jz4740_rtc_exit);
397 +MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
398 +MODULE_LICENSE("GPL");
399 +MODULE_DESCRIPTION("RTC driver for the JZ4740 SoC\n");
400 +MODULE_ALIAS("platform:jz4740-rtc");