Linux 2.6.33.13
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / rtc / rtc-coh901331.c
blob44c4399ee7144fc2e8cffca50d8a96426b05a8fb
1 /*
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.
8 */
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>
14 #include <linux/pm.h>
15 #include <linux/platform_device.h>
16 #include <linux/io.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;
44 struct clk *clk;
45 u32 phybase;
46 u32 physize;
47 void __iomem *virtbase;
48 int irq;
49 #ifdef CONFIG_PM
50 u32 irqmaskstore;
51 #endif
54 static irqreturn_t coh901331_interrupt(int irq, void *data)
56 struct coh901331_port *rtap = data;
58 clk_enable(rtap->clk);
59 /* Ack IRQ */
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);
71 /* Set alarm flag */
72 rtc_update_irq(rtap->rtc, 1, RTC_AF);
74 return IRQ_HANDLED;
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);
89 return -EINVAL;
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);
100 return 0;
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);
113 return 0;
116 static int coh901331_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
118 struct coh901331_port *rtap = dev_get_drvdata(dev);
119 unsigned long time;
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);
127 return 0;
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);
135 if (enabled)
136 writel(1, rtap->virtbase + COH901331_IRQ_MASK);
137 else
138 writel(0, rtap->virtbase + COH901331_IRQ_MASK);
139 clk_disable(rtap->clk);
141 return 0;
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);
156 if (rtap) {
157 free_irq(rtap->irq, rtap);
158 rtc_device_unregister(rtap->rtc);
159 clk_put(rtap->clk);
160 iounmap(rtap->virtbase);
161 release_mem_region(rtap->phybase, rtap->physize);
162 platform_set_drvdata(pdev, NULL);
163 kfree(rtap);
166 return 0;
170 static int __init coh901331_probe(struct platform_device *pdev)
172 int ret;
173 struct coh901331_port *rtap;
174 struct resource *res;
176 rtap = kzalloc(sizeof(struct coh901331_port), GFP_KERNEL);
177 if (!rtap)
178 return -ENOMEM;
180 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
181 if (!res) {
182 ret = -ENOENT;
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) {
190 ret = -EBUSY;
191 goto out_no_memregion;
194 rtap->virtbase = ioremap(rtap->phybase, rtap->physize);
195 if (!rtap->virtbase) {
196 ret = -ENOMEM;
197 goto out_no_remap;
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)) {
203 ret = -EIO;
204 goto out_no_irq;
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");
211 goto out_no_clk;
214 /* We enable/disable the clock only to assure it works */
215 ret = clk_enable(rtap->clk);
216 if (ret) {
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,
223 THIS_MODULE);
224 if (IS_ERR(rtap->rtc)) {
225 ret = PTR_ERR(rtap->rtc);
226 goto out_no_rtc;
229 platform_set_drvdata(pdev, rtap);
231 return 0;
233 out_no_rtc:
234 out_no_clk_enable:
235 clk_put(rtap->clk);
236 out_no_clk:
237 free_irq(rtap->irq, rtap);
238 out_no_irq:
239 iounmap(rtap->virtbase);
240 out_no_remap:
241 platform_set_drvdata(pdev, NULL);
242 out_no_memregion:
243 release_mem_region(rtap->phybase, SZ_4K);
244 out_no_resource:
245 kfree(rtap);
246 return ret;
249 #ifdef CONFIG_PM
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);
261 } else {
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);
267 return 0;
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);
276 } else {
277 clk_enable(rtap->clk);
278 writel(rtap->irqmaskstore, rtap->virtbase + COH901331_IRQ_MASK);
279 clk_disable(rtap->clk);
281 return 0;
283 #else
284 #define coh901331_suspend NULL
285 #define coh901331_resume NULL
286 #endif
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 = {
298 .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");