1 From 9241770134d9bea522051622c52a3195a3d218e0 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..74699dd 100644
15 --- a/drivers/rtc/Kconfig
16 +++ b/drivers/rtc/Kconfig
17 @@ -488,6 +488,17 @@ config RTC_DRV_EFI
18 This driver can also be built as a module. If so, the module
19 will be called rtc-efi.
21 +config RTC_DRV_JZ4740
22 + tristate "Ingenic JZ4720/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 JZ4720/JZ4740 SoC RTC controller.
29 + This driver can also be buillt as a module. If so, the module
30 + will be called rtc-jz4740.
32 config RTC_DRV_STK17TA8
33 tristate "Simtek STK17TA8"
35 diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
36 index 44ef194..7002033 100644
37 --- a/drivers/rtc/Makefile
38 +++ b/drivers/rtc/Makefile
39 @@ -45,6 +45,7 @@ obj-$(CONFIG_RTC_DRV_EP93XX) += rtc-ep93xx.o
40 obj-$(CONFIG_RTC_DRV_FM3130) += rtc-fm3130.o
41 obj-$(CONFIG_RTC_DRV_GENERIC) += rtc-generic.o
42 obj-$(CONFIG_RTC_DRV_ISL1208) += rtc-isl1208.o
43 +obj-$(CONFIG_RTC_DRV_JZ4740) += rtc-jz4740.o
44 obj-$(CONFIG_RTC_DRV_M41T80) += rtc-m41t80.o
45 obj-$(CONFIG_RTC_DRV_M41T94) += rtc-m41t94.o
46 obj-$(CONFIG_RTC_DRV_M48T35) += rtc-m48t35.o
47 diff --git a/drivers/rtc/rtc-jz4740.c b/drivers/rtc/rtc-jz4740.c
49 index 0000000..aac905a
51 +++ b/drivers/rtc/rtc-jz4740.c
54 + * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
55 + * JZ4720/JZ4740 SoC RTC driver
57 + * This program is free software; you can redistribute it and/or modify it
58 + * under the terms of the GNU General Public License as published by the
59 + * Free Software Foundation; either version 2 of the License, or (at your
60 + * option) any later version.
62 + * You should have received a copy of the GNU General Public License along
63 + * with this program; if not, write to the Free Software Foundation, Inc.,
64 + * 675 Mass Ave, Cambridge, MA 02139, USA.
68 +#include <linux/kernel.h>
69 +#include <linux/module.h>
70 +#include <linux/platform_device.h>
71 +#include <linux/rtc.h>
72 +#include <linux/slab.h>
73 +#include <linux/spinlock.h>
75 +#define JZ_REG_RTC_CTRL 0x00
76 +#define JZ_REG_RTC_SEC 0x04
77 +#define JZ_REG_RTC_SEC_ALARM 0x08
78 +#define JZ_REG_RTC_REGULATOR 0x0C
79 +#define JZ_REG_RTC_HIBERNATE 0x20
80 +#define JZ_REG_RTC_SCRATCHPAD 0x34
82 +#define JZ_RTC_CTRL_WRDY BIT(7)
83 +#define JZ_RTC_CTRL_1HZ BIT(6)
84 +#define JZ_RTC_CTRL_1HZ_IRQ BIT(5)
85 +#define JZ_RTC_CTRL_AF BIT(4)
86 +#define JZ_RTC_CTRL_AF_IRQ BIT(3)
87 +#define JZ_RTC_CTRL_AE BIT(2)
88 +#define JZ_RTC_CTRL_ENABLE BIT(0)
91 + struct resource *mem;
94 + struct rtc_device *rtc;
101 +static inline uint32_t jz4740_rtc_reg_read(struct jz4740_rtc *rtc, size_t reg)
103 + return readl(rtc->base + reg);
106 +static inline void jz4740_rtc_wait_write_ready(struct jz4740_rtc *rtc)
110 + ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL);
111 + } while (!(ctrl & JZ_RTC_CTRL_WRDY));
115 +static inline void jz4740_rtc_reg_write(struct jz4740_rtc *rtc, size_t reg,
118 + jz4740_rtc_wait_write_ready(rtc);
119 + writel(val, rtc->base + reg);
122 +static void jz4740_rtc_ctrl_set_bits(struct jz4740_rtc *rtc, uint32_t mask,
125 + unsigned long flags;
128 + spin_lock_irqsave(&rtc->lock, flags);
130 + ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL);
132 + /* Don't clear interrupt flags by accident */
133 + ctrl |= JZ_RTC_CTRL_1HZ | JZ_RTC_CTRL_AF;
138 + jz4740_rtc_reg_write(rtc, JZ_REG_RTC_CTRL, ctrl);
140 + spin_unlock_irqrestore(&rtc->lock, flags);
143 +static inline struct jz4740_rtc *dev_to_rtc(struct device *dev)
145 + return dev_get_drvdata(dev);
148 +static int jz4740_rtc_read_time(struct device *dev, struct rtc_time *time)
150 + struct jz4740_rtc *rtc = dev_to_rtc(dev);
151 + uint32_t secs, secs2;
153 + secs = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC);
154 + secs2 = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC);
156 + while (secs != secs2) {
158 + secs2 = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC);
161 + rtc_time_to_tm(secs, time);
163 + return rtc_valid_tm(time);
166 +static int jz4740_rtc_set_mmss(struct device *dev, unsigned long secs)
168 + struct jz4740_rtc *rtc = dev_to_rtc(dev);
170 + if ((uint32_t)secs != secs)
173 + jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SEC, secs);
178 +static int jz4740_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
180 + struct jz4740_rtc *rtc = dev_to_rtc(dev);
181 + uint32_t secs, secs2;
184 + secs = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC_ALARM);
185 + secs2 = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC_ALARM);
187 + while (secs != secs2) {
189 + secs2 = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC_ALARM);
192 + ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL);
194 + alrm->enabled = !!(ctrl & JZ_RTC_CTRL_AE);
195 + alrm->pending = !!(ctrl & JZ_RTC_CTRL_AF);
197 + rtc_time_to_tm(secs, &alrm->time);
199 + return rtc_valid_tm(&alrm->time);
202 +static int jz4740_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
204 + struct jz4740_rtc *rtc = dev_to_rtc(dev);
205 + unsigned long secs;
207 + rtc_tm_to_time(&alrm->time, &secs);
209 + if ((uint32_t)secs != secs)
212 + jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SEC_ALARM, (uint32_t)secs);
213 + jz4740_rtc_ctrl_set_bits(rtc, JZ_RTC_CTRL_AE,
214 + alrm->enabled ? JZ_RTC_CTRL_AE : 0);
219 +static int jz4740_rtc_update_irq_enable(struct device *dev, unsigned int enable)
221 + struct jz4740_rtc *rtc = dev_to_rtc(dev);
222 + jz4740_rtc_ctrl_set_bits(rtc, JZ_RTC_CTRL_1HZ_IRQ,
223 + enable ? JZ_RTC_CTRL_1HZ_IRQ : 0);
228 +static int jz4740_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
230 + struct jz4740_rtc *rtc = dev_to_rtc(dev);
231 + jz4740_rtc_ctrl_set_bits(rtc, JZ_RTC_CTRL_AF_IRQ,
232 + enable ? JZ_RTC_CTRL_AF_IRQ : 0);
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;
250 + ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL);
252 + if (ctrl & JZ_RTC_CTRL_1HZ)
253 + events |= (RTC_UF | RTC_IRQF);
255 + if (ctrl & JZ_RTC_CTRL_AF)
256 + events |= (RTC_AF | RTC_IRQF);
258 + rtc_update_irq(rtc->rtc, 1, events);
260 + jz4740_rtc_ctrl_set_bits(rtc, JZ_RTC_CTRL_1HZ | JZ_RTC_CTRL_AF, 0);
262 + return IRQ_HANDLED;
265 +void jz4740_rtc_poweroff(struct device *dev)
267 + struct jz4740_rtc *rtc = dev_get_drvdata(dev);
268 + jz4740_rtc_reg_write(rtc, JZ_REG_RTC_HIBERNATE, 1);
270 +EXPORT_SYMBOL_GPL(jz4740_rtc_poweroff);
272 +static int __devinit jz4740_rtc_probe(struct platform_device *pdev)
275 + struct jz4740_rtc *rtc;
276 + uint32_t scratchpad;
278 + rtc = kmalloc(sizeof(*rtc), GFP_KERNEL);
280 + rtc->irq = platform_get_irq(pdev, 0);
282 + if (rtc->irq < 0) {
284 + dev_err(&pdev->dev, "Failed to get platform irq\n");
288 + rtc->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
291 + dev_err(&pdev->dev, "Failed to get platform mmio memory\n");
295 + rtc->mem = request_mem_region(rtc->mem->start, resource_size(rtc->mem),
300 + dev_err(&pdev->dev, "Failed to request mmio memory region\n");
304 + 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,
319 + if (IS_ERR(rtc->rtc)) {
320 + ret = PTR_ERR(rtc->rtc);
321 + dev_err(&pdev->dev, "Failed to register rtc device: %d\n", ret);
325 + ret = request_irq(rtc->irq, jz4740_rtc_irq, 0,
329 + dev_err(&pdev->dev, "Failed to request rtc irq: %d\n", ret);
330 + goto err_unregister_rtc;
333 + scratchpad = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SCRATCHPAD);
334 + if (scratchpad != 0x12345678) {
335 + jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SCRATCHPAD, 0x12345678);
336 + jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SEC, 0);
342 + rtc_device_unregister(rtc->rtc);
344 + platform_set_drvdata(pdev, NULL);
345 + iounmap(rtc->base);
346 +err_release_mem_region:
347 + release_mem_region(rtc->mem->start, resource_size(rtc->mem));
354 +static int __devexit jz4740_rtc_remove(struct platform_device *pdev)
356 + struct jz4740_rtc *rtc = platform_get_drvdata(pdev);
358 + free_irq(rtc->irq, rtc);
360 + rtc_device_unregister(rtc->rtc);
362 + iounmap(rtc->base);
363 + release_mem_region(rtc->mem->start, resource_size(rtc->mem));
367 + platform_set_drvdata(pdev, NULL);
372 +struct platform_driver jz4740_rtc_driver = {
373 + .probe = jz4740_rtc_probe,
374 + .remove = __devexit_p(jz4740_rtc_remove),
376 + .name = "jz4740-rtc",
377 + .owner = THIS_MODULE,
381 +static int __init jz4740_rtc_init(void)
383 + return platform_driver_register(&jz4740_rtc_driver);
385 +module_init(jz4740_rtc_init);
387 +static void __exit jz4740_rtc_exit(void)
389 + platform_driver_unregister(&jz4740_rtc_driver);
391 +module_exit(jz4740_rtc_exit);
393 +MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
394 +MODULE_LICENSE("GPL");
395 +MODULE_DESCRIPTION("RTC driver for the JZ4720/JZ4740 SoC\n");
396 +MODULE_ALIAS("platform:jz4740-rtc");