2 * Copyright (C) 2007-2009 ST-Ericsson AB
3 * License terms: GNU General Public License (GPL) version 2
4 * Real Time Clock interface for ST-Ericsson AB COH 901 331 RTC.
5 * Author: Linus Walleij <linus.walleij@stericsson.com>
6 * Based on rtc-pl031.c by Deepak Saxena <dsaxena@plexity.net>
7 * Copyright 2006 (c) MontaVista Software, Inc.
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/rtc.h>
12 #include <linux/clk.h>
13 #include <linux/interrupt.h>
15 #include <linux/platform_device.h>
19 * Registers in the COH 901 331
21 /* Alarm value 32bit (R/W) */
22 #define COH901331_ALARM 0x00U
23 /* Used to set current time 32bit (R/W) */
24 #define COH901331_SET_TIME 0x04U
25 /* Indication if current time is valid 32bit (R/-) */
26 #define COH901331_VALID 0x08U
27 /* Read the current time 32bit (R/-) */
28 #define COH901331_CUR_TIME 0x0cU
29 /* Event register for the "alarm" interrupt */
30 #define COH901331_IRQ_EVENT 0x10U
31 /* Mask register for the "alarm" interrupt */
32 #define COH901331_IRQ_MASK 0x14U
33 /* Force register for the "alarm" interrupt */
34 #define COH901331_IRQ_FORCE 0x18U
37 * Reference to RTC block clock
38 * Notice that the frequent clk_enable()/clk_disable() on this
39 * clock is mainly to be able to turn on/off other clocks in the
40 * hierarchy as needed, the RTC clock is always on anyway.
42 struct coh901331_port {
43 struct rtc_device *rtc;
47 void __iomem *virtbase;
54 static irqreturn_t coh901331_interrupt(int irq, void *data)
56 struct coh901331_port *rtap = data;
58 clk_enable(rtap->clk);
60 writel(1, rtap->virtbase + COH901331_IRQ_EVENT);
62 * Disable the interrupt. This is necessary because
63 * the RTC lives on a lower-clocked line and will
64 * not release the IRQ line until after a few (slower)
65 * clock cycles. The interrupt will be re-enabled when
66 * a new alarm is set anyway.
68 writel(0, rtap->virtbase + COH901331_IRQ_MASK);
69 clk_disable(rtap->clk);
72 rtc_update_irq(rtap->rtc, 1, RTC_AF);
77 static int coh901331_read_time(struct device *dev, struct rtc_time *tm)
79 struct coh901331_port *rtap = dev_get_drvdata(dev);
81 clk_enable(rtap->clk);
82 /* Check if the time is valid */
83 if (readl(rtap->virtbase + COH901331_VALID)) {
84 rtc_time_to_tm(readl(rtap->virtbase + COH901331_CUR_TIME), tm);
85 clk_disable(rtap->clk);
86 return rtc_valid_tm(tm);
88 clk_disable(rtap->clk);
92 static int coh901331_set_mmss(struct device *dev, unsigned long secs)
94 struct coh901331_port *rtap = dev_get_drvdata(dev);
96 clk_enable(rtap->clk);
97 writel(secs, rtap->virtbase + COH901331_SET_TIME);
98 clk_disable(rtap->clk);
103 static int coh901331_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
105 struct coh901331_port *rtap = dev_get_drvdata(dev);
107 clk_enable(rtap->clk);
108 rtc_time_to_tm(readl(rtap->virtbase + COH901331_ALARM), &alarm->time);
109 alarm->pending = readl(rtap->virtbase + COH901331_IRQ_EVENT) & 1U;
110 alarm->enabled = readl(rtap->virtbase + COH901331_IRQ_MASK) & 1U;
111 clk_disable(rtap->clk);
116 static int coh901331_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
118 struct coh901331_port *rtap = dev_get_drvdata(dev);
121 rtc_tm_to_time(&alarm->time, &time);
122 clk_enable(rtap->clk);
123 writel(time, rtap->virtbase + COH901331_ALARM);
124 writel(alarm->enabled, rtap->virtbase + COH901331_IRQ_MASK);
125 clk_disable(rtap->clk);
130 static int coh901331_alarm_irq_enable(struct device *dev, unsigned int enabled)
132 struct coh901331_port *rtap = dev_get_drvdata(dev);
134 clk_enable(rtap->clk);
136 writel(1, rtap->virtbase + COH901331_IRQ_MASK);
138 writel(0, rtap->virtbase + COH901331_IRQ_MASK);
139 clk_disable(rtap->clk);
144 static struct rtc_class_ops coh901331_ops = {
145 .read_time = coh901331_read_time,
146 .set_mmss = coh901331_set_mmss,
147 .read_alarm = coh901331_read_alarm,
148 .set_alarm = coh901331_set_alarm,
149 .alarm_irq_enable = coh901331_alarm_irq_enable,
152 static int __exit coh901331_remove(struct platform_device *pdev)
154 struct coh901331_port *rtap = dev_get_drvdata(&pdev->dev);
157 free_irq(rtap->irq, rtap);
158 rtc_device_unregister(rtap->rtc);
160 iounmap(rtap->virtbase);
161 release_mem_region(rtap->phybase, rtap->physize);
162 platform_set_drvdata(pdev, NULL);
170 static int __init coh901331_probe(struct platform_device *pdev)
173 struct coh901331_port *rtap;
174 struct resource *res;
176 rtap = kzalloc(sizeof(struct coh901331_port), GFP_KERNEL);
180 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
183 goto out_no_resource;
185 rtap->phybase = res->start;
186 rtap->physize = resource_size(res);
188 if (request_mem_region(rtap->phybase, rtap->physize,
189 "rtc-coh901331") == NULL) {
191 goto out_no_memregion;
194 rtap->virtbase = ioremap(rtap->phybase, rtap->physize);
195 if (!rtap->virtbase) {
200 rtap->irq = platform_get_irq(pdev, 0);
201 if (request_irq(rtap->irq, coh901331_interrupt, IRQF_DISABLED,
202 "RTC COH 901 331 Alarm", rtap)) {
207 rtap->clk = clk_get(&pdev->dev, NULL);
208 if (IS_ERR(rtap->clk)) {
209 ret = PTR_ERR(rtap->clk);
210 dev_err(&pdev->dev, "could not get clock\n");
214 /* We enable/disable the clock only to assure it works */
215 ret = clk_enable(rtap->clk);
217 dev_err(&pdev->dev, "could not enable clock\n");
218 goto out_no_clk_enable;
220 clk_disable(rtap->clk);
222 rtap->rtc = rtc_device_register("coh901331", &pdev->dev, &coh901331_ops,
224 if (IS_ERR(rtap->rtc)) {
225 ret = PTR_ERR(rtap->rtc);
229 platform_set_drvdata(pdev, rtap);
237 free_irq(rtap->irq, rtap);
239 iounmap(rtap->virtbase);
241 platform_set_drvdata(pdev, NULL);
243 release_mem_region(rtap->phybase, SZ_4K);
250 static int coh901331_suspend(struct platform_device *pdev, pm_message_t state)
252 struct coh901331_port *rtap = dev_get_drvdata(&pdev->dev);
255 * If this RTC alarm will be used for waking the system up,
256 * don't disable it of course. Else we just disable the alarm
257 * and await suspension.
259 if (device_may_wakeup(&pdev->dev)) {
260 enable_irq_wake(rtap->irq);
262 clk_enable(rtap->clk);
263 rtap->irqmaskstore = readl(rtap->virtbase + COH901331_IRQ_MASK);
264 writel(0, rtap->virtbase + COH901331_IRQ_MASK);
265 clk_disable(rtap->clk);
270 static int coh901331_resume(struct platform_device *pdev)
272 struct coh901331_port *rtap = dev_get_drvdata(&pdev->dev);
274 if (device_may_wakeup(&pdev->dev)) {
275 disable_irq_wake(rtap->irq);
277 clk_enable(rtap->clk);
278 writel(rtap->irqmaskstore, rtap->virtbase + COH901331_IRQ_MASK);
279 clk_disable(rtap->clk);
284 #define coh901331_suspend NULL
285 #define coh901331_resume NULL
288 static void coh901331_shutdown(struct platform_device *pdev)
290 struct coh901331_port *rtap = dev_get_drvdata(&pdev->dev);
292 clk_enable(rtap->clk);
293 writel(0, rtap->virtbase + COH901331_IRQ_MASK);
294 clk_disable(rtap->clk);
297 static struct platform_driver coh901331_driver = {
299 .name = "rtc-coh901331",
300 .owner = THIS_MODULE,
302 .remove = __exit_p(coh901331_remove),
303 .suspend = coh901331_suspend,
304 .resume = coh901331_resume,
305 .shutdown = coh901331_shutdown,
308 static int __init coh901331_init(void)
310 return platform_driver_probe(&coh901331_driver, coh901331_probe);
313 static void __exit coh901331_exit(void)
315 platform_driver_unregister(&coh901331_driver);
318 module_init(coh901331_init);
319 module_exit(coh901331_exit);
321 MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
322 MODULE_DESCRIPTION("ST-Ericsson AB COH 901 331 RTC Driver");
323 MODULE_LICENSE("GPL");