1 From 103784e50d565c9e9325a9256e0547a40c6b959c Mon Sep 17 00:00:00 2001
2 From: Lars-Peter Clausen <lars@metafoo.de>
3 Date: Sat, 24 Apr 2010 12:12:37 +0200
4 Subject: [PATCH] Add jz4740 rtc driver
7 drivers/rtc/Kconfig | 11 ++
8 drivers/rtc/Makefile | 1 +
9 drivers/rtc/rtc-jz4740.c | 344 ++++++++++++++++++++++++++++++++++++++++++++++
10 3 files changed, 356 insertions(+), 0 deletions(-)
11 create mode 100644 drivers/rtc/rtc-jz4740.c
13 diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
14 index 6a13037..8210bc7 100644
15 --- a/drivers/rtc/Kconfig
16 +++ b/drivers/rtc/Kconfig
17 @@ -888,4 +888,15 @@ config RTC_DRV_MPC5121
18 This driver can also be built as a module. If so, the module
19 will be called rtc-mpc5121.
21 +config RTC_DRV_JZ4740
22 + tristate "Ingenic JZ4740 SoC"
23 + depends on RTC_CLASS
24 + depends on SOC_JZ4740
26 + If you say yes here you get support for the
27 + Ingenic JZ4740 SoC RTC controller.
29 + This driver can also be buillt as a module. If so, the module
30 + will be called rtc-jz4740.
33 diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
34 index 44ef194..7002033 100644
35 --- a/drivers/rtc/Makefile
36 +++ b/drivers/rtc/Makefile
37 @@ -45,6 +45,7 @@ obj-$(CONFIG_RTC_DRV_EP93XX) += rtc-ep93xx.o
38 obj-$(CONFIG_RTC_DRV_FM3130) += rtc-fm3130.o
39 obj-$(CONFIG_RTC_DRV_GENERIC) += rtc-generic.o
40 obj-$(CONFIG_RTC_DRV_ISL1208) += rtc-isl1208.o
41 +obj-$(CONFIG_RTC_DRV_JZ4740) += rtc-jz4740.o
42 obj-$(CONFIG_RTC_DRV_M41T80) += rtc-m41t80.o
43 obj-$(CONFIG_RTC_DRV_M41T94) += rtc-m41t94.o
44 obj-$(CONFIG_RTC_DRV_M48T35) += rtc-m48t35.o
45 diff --git a/drivers/rtc/rtc-jz4740.c b/drivers/rtc/rtc-jz4740.c
47 index 0000000..aac905a
49 +++ b/drivers/rtc/rtc-jz4740.c
52 + * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
53 + * JZ4720/JZ4740 SoC RTC driver
55 + * This program is free software; you can redistribute it and/or modify it
56 + * under the terms of the GNU General Public License as published by the
57 + * Free Software Foundation; either version 2 of the License, or (at your
58 + * option) any later version.
60 + * You should have received a copy of the GNU General Public License along
61 + * with this program; if not, write to the Free Software Foundation, Inc.,
62 + * 675 Mass Ave, Cambridge, MA 02139, USA.
66 +#include <linux/kernel.h>
67 +#include <linux/module.h>
68 +#include <linux/platform_device.h>
69 +#include <linux/rtc.h>
70 +#include <linux/slab.h>
71 +#include <linux/spinlock.h>
73 +#define JZ_REG_RTC_CTRL 0x00
74 +#define JZ_REG_RTC_SEC 0x04
75 +#define JZ_REG_RTC_SEC_ALARM 0x08
76 +#define JZ_REG_RTC_REGULATOR 0x0C
77 +#define JZ_REG_RTC_HIBERNATE 0x20
78 +#define JZ_REG_RTC_SCRATCHPAD 0x34
80 +#define JZ_RTC_CTRL_WRDY BIT(7)
81 +#define JZ_RTC_CTRL_1HZ BIT(6)
82 +#define JZ_RTC_CTRL_1HZ_IRQ BIT(5)
83 +#define JZ_RTC_CTRL_AF BIT(4)
84 +#define JZ_RTC_CTRL_AF_IRQ BIT(3)
85 +#define JZ_RTC_CTRL_AE BIT(2)
86 +#define JZ_RTC_CTRL_ENABLE BIT(0)
89 + struct resource *mem;
92 + struct rtc_device *rtc;
99 +static inline uint32_t jz4740_rtc_reg_read(struct jz4740_rtc *rtc, size_t reg)
101 + return readl(rtc->base + reg);
104 +static inline void jz4740_rtc_wait_write_ready(struct jz4740_rtc *rtc)
108 + ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL);
109 + } while (!(ctrl & JZ_RTC_CTRL_WRDY));
113 +static inline void jz4740_rtc_reg_write(struct jz4740_rtc *rtc, size_t reg,
116 + jz4740_rtc_wait_write_ready(rtc);
117 + writel(val, rtc->base + reg);
120 +static void jz4740_rtc_ctrl_set_bits(struct jz4740_rtc *rtc, uint32_t mask,
123 + unsigned long flags;
126 + spin_lock_irqsave(&rtc->lock, flags);
128 + ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL);
130 + /* Don't clear interrupt flags by accident */
131 + ctrl |= JZ_RTC_CTRL_1HZ | JZ_RTC_CTRL_AF;
136 + jz4740_rtc_reg_write(rtc, JZ_REG_RTC_CTRL, ctrl);
138 + spin_unlock_irqrestore(&rtc->lock, flags);
141 +static inline struct jz4740_rtc *dev_to_rtc(struct device *dev)
143 + return dev_get_drvdata(dev);
146 +static int jz4740_rtc_read_time(struct device *dev, struct rtc_time *time)
148 + struct jz4740_rtc *rtc = dev_to_rtc(dev);
149 + uint32_t secs, secs2;
151 + secs = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC);
152 + secs2 = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC);
154 + while (secs != secs2) {
156 + secs2 = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC);
159 + rtc_time_to_tm(secs, time);
161 + return rtc_valid_tm(time);
164 +static int jz4740_rtc_set_mmss(struct device *dev, unsigned long secs)
166 + struct jz4740_rtc *rtc = dev_to_rtc(dev);
168 + if ((uint32_t)secs != secs)
171 + jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SEC, secs);
176 +static int jz4740_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
178 + struct jz4740_rtc *rtc = dev_to_rtc(dev);
179 + uint32_t secs, secs2;
182 + secs = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC_ALARM);
183 + secs2 = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC_ALARM);
185 + while (secs != secs2) {
187 + secs2 = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC_ALARM);
190 + ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL);
192 + alrm->enabled = !!(ctrl & JZ_RTC_CTRL_AE);
193 + alrm->pending = !!(ctrl & JZ_RTC_CTRL_AF);
195 + rtc_time_to_tm(secs, &alrm->time);
197 + return rtc_valid_tm(&alrm->time);
200 +static int jz4740_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
202 + struct jz4740_rtc *rtc = dev_to_rtc(dev);
203 + unsigned long secs;
205 + rtc_tm_to_time(&alrm->time, &secs);
207 + if ((uint32_t)secs != secs)
210 + jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SEC_ALARM, (uint32_t)secs);
211 + jz4740_rtc_ctrl_set_bits(rtc, JZ_RTC_CTRL_AE,
212 + alrm->enabled ? JZ_RTC_CTRL_AE : 0);
217 +static int jz4740_rtc_update_irq_enable(struct device *dev, unsigned int enable)
219 + struct jz4740_rtc *rtc = dev_to_rtc(dev);
220 + jz4740_rtc_ctrl_set_bits(rtc, JZ_RTC_CTRL_1HZ_IRQ,
221 + enable ? JZ_RTC_CTRL_1HZ_IRQ : 0);
226 +static int jz4740_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
228 + struct jz4740_rtc *rtc = dev_to_rtc(dev);
229 + jz4740_rtc_ctrl_set_bits(rtc, JZ_RTC_CTRL_AF_IRQ,
230 + enable ? JZ_RTC_CTRL_AF_IRQ : 0);
234 +static struct rtc_class_ops jz4740_rtc_ops = {
235 + .read_time = jz4740_rtc_read_time,
236 + .set_mmss = jz4740_rtc_set_mmss,
237 + .read_alarm = jz4740_rtc_read_alarm,
238 + .set_alarm = jz4740_rtc_set_alarm,
239 + .update_irq_enable = jz4740_rtc_update_irq_enable,
240 + .alarm_irq_enable = jz4740_rtc_alarm_irq_enable,
243 +static irqreturn_t jz4740_rtc_irq(int irq, void *data)
245 + struct jz4740_rtc *rtc = data;
247 + unsigned long events = 0;
248 + ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL);
250 + if (ctrl & JZ_RTC_CTRL_1HZ)
251 + events |= (RTC_UF | RTC_IRQF);
253 + if (ctrl & JZ_RTC_CTRL_AF)
254 + events |= (RTC_AF | RTC_IRQF);
256 + rtc_update_irq(rtc->rtc, 1, events);
258 + jz4740_rtc_ctrl_set_bits(rtc, JZ_RTC_CTRL_1HZ | JZ_RTC_CTRL_AF, 0);
260 + return IRQ_HANDLED;
263 +void jz4740_rtc_poweroff(struct device *dev)
265 + struct jz4740_rtc *rtc = dev_get_drvdata(dev);
266 + jz4740_rtc_reg_write(rtc, JZ_REG_RTC_HIBERNATE, 1);
268 +EXPORT_SYMBOL_GPL(jz4740_rtc_poweroff);
270 +static int __devinit jz4740_rtc_probe(struct platform_device *pdev)
273 + struct jz4740_rtc *rtc;
274 + uint32_t scratchpad;
276 + rtc = kmalloc(sizeof(*rtc), GFP_KERNEL);
278 + rtc->irq = platform_get_irq(pdev, 0);
280 + if (rtc->irq < 0) {
282 + dev_err(&pdev->dev, "Failed to get platform irq\n");
286 + rtc->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
289 + dev_err(&pdev->dev, "Failed to get platform mmio memory\n");
293 + rtc->mem = request_mem_region(rtc->mem->start, resource_size(rtc->mem),
298 + dev_err(&pdev->dev, "Failed to request mmio memory region\n");
302 + rtc->base = ioremap_nocache(rtc->mem->start, resource_size(rtc->mem));
306 + dev_err(&pdev->dev, "Failed to ioremap mmio memory\n");
307 + goto err_release_mem_region;
310 + spin_lock_init(&rtc->lock);
312 + platform_set_drvdata(pdev, rtc);
314 + rtc->rtc = rtc_device_register(pdev->name, &pdev->dev, &jz4740_rtc_ops,
317 + if (IS_ERR(rtc->rtc)) {
318 + ret = PTR_ERR(rtc->rtc);
319 + dev_err(&pdev->dev, "Failed to register rtc device: %d\n", ret);
323 + 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 + jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SCRATCHPAD, 0x12345678);
334 + jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SEC, 0);
340 + rtc_device_unregister(rtc->rtc);
342 + platform_set_drvdata(pdev, NULL);
343 + iounmap(rtc->base);
344 +err_release_mem_region:
345 + release_mem_region(rtc->mem->start, resource_size(rtc->mem));
352 +static int __devexit jz4740_rtc_remove(struct platform_device *pdev)
354 + struct jz4740_rtc *rtc = platform_get_drvdata(pdev);
356 + free_irq(rtc->irq, rtc);
358 + rtc_device_unregister(rtc->rtc);
360 + iounmap(rtc->base);
361 + release_mem_region(rtc->mem->start, resource_size(rtc->mem));
365 + platform_set_drvdata(pdev, NULL);
370 +struct platform_driver jz4740_rtc_driver = {
371 + .probe = jz4740_rtc_probe,
372 + .remove = __devexit_p(jz4740_rtc_remove),
374 + .name = "jz4740-rtc",
375 + .owner = THIS_MODULE,
379 +static int __init jz4740_rtc_init(void)
381 + return platform_driver_register(&jz4740_rtc_driver);
383 +module_init(jz4740_rtc_init);
385 +static void __exit jz4740_rtc_exit(void)
387 + platform_driver_unregister(&jz4740_rtc_driver);
389 +module_exit(jz4740_rtc_exit);
391 +MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
392 +MODULE_LICENSE("GPL");
393 +MODULE_DESCRIPTION("RTC driver for the JZ4720/JZ4740 SoC\n");
394 +MODULE_ALIAS("platform:jz4740-rtc");