blob: 03ea530981d1e66328625fb26567cc40e35f9f23 [file] [log] [blame]
Linus Walleijaa958f52009-09-22 16:46:24 -07001/*
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>
17
18/*
19 * Registers in the COH 901 331
20 */
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
35
36/*
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.
41 */
42struct 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
52};
53
54static irqreturn_t coh901331_interrupt(int irq, void *data)
55{
56 struct coh901331_port *rtap = data;
57
58 clk_enable(rtap->clk);
59 /* Ack IRQ */
60 writel(1, rtap->virtbase + COH901331_IRQ_EVENT);
Linus Walleij378ce742009-11-14 01:03:24 +010061 /*
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.
67 */
68 writel(0, rtap->virtbase + COH901331_IRQ_MASK);
Linus Walleijaa958f52009-09-22 16:46:24 -070069 clk_disable(rtap->clk);
Linus Walleij378ce742009-11-14 01:03:24 +010070
Linus Walleijaa958f52009-09-22 16:46:24 -070071 /* Set alarm flag */
72 rtc_update_irq(rtap->rtc, 1, RTC_AF);
73
74 return IRQ_HANDLED;
75}
76
77static int coh901331_read_time(struct device *dev, struct rtc_time *tm)
78{
79 struct coh901331_port *rtap = dev_get_drvdata(dev);
80
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);
87 }
88 clk_disable(rtap->clk);
89 return -EINVAL;
90}
91
92static int coh901331_set_mmss(struct device *dev, unsigned long secs)
93{
94 struct coh901331_port *rtap = dev_get_drvdata(dev);
95
96 clk_enable(rtap->clk);
97 writel(secs, rtap->virtbase + COH901331_SET_TIME);
98 clk_disable(rtap->clk);
99
100 return 0;
101}
102
103static int coh901331_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
104{
105 struct coh901331_port *rtap = dev_get_drvdata(dev);
106
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);
112
113 return 0;
114}
115
116static int coh901331_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
117{
118 struct coh901331_port *rtap = dev_get_drvdata(dev);
119 unsigned long time;
120
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);
126
127 return 0;
128}
129
130static int coh901331_alarm_irq_enable(struct device *dev, unsigned int enabled)
131{
132 struct coh901331_port *rtap = dev_get_drvdata(dev);
133
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);
Linus Walleij378ce742009-11-14 01:03:24 +0100140
141 return 0;
Linus Walleijaa958f52009-09-22 16:46:24 -0700142}
143
144static 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,
150};
151
152static int __exit coh901331_remove(struct platform_device *pdev)
153{
154 struct coh901331_port *rtap = dev_get_drvdata(&pdev->dev);
155
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);
164 }
165
166 return 0;
167}
168
169
170static int __init coh901331_probe(struct platform_device *pdev)
171{
172 int ret;
173 struct coh901331_port *rtap;
174 struct resource *res;
175
176 rtap = kzalloc(sizeof(struct coh901331_port), GFP_KERNEL);
177 if (!rtap)
178 return -ENOMEM;
179
180 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
181 if (!res) {
182 ret = -ENOENT;
183 goto out_no_resource;
184 }
185 rtap->phybase = res->start;
186 rtap->physize = resource_size(res);
187
188 if (request_mem_region(rtap->phybase, rtap->physize,
189 "rtc-coh901331") == NULL) {
190 ret = -EBUSY;
191 goto out_no_memregion;
192 }
193
194 rtap->virtbase = ioremap(rtap->phybase, rtap->physize);
195 if (!rtap->virtbase) {
196 ret = -ENOMEM;
197 goto out_no_remap;
198 }
199
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;
205 }
206
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;
212 }
213
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;
219 }
220 clk_disable(rtap->clk);
221
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;
227 }
228
229 platform_set_drvdata(pdev, rtap);
230
231 return 0;
232
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;
247}
248
249#ifdef CONFIG_PM
250static int coh901331_suspend(struct platform_device *pdev, pm_message_t state)
251{
252 struct coh901331_port *rtap = dev_get_drvdata(&pdev->dev);
253
254 /*
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.
258 */
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);
266 }
267 return 0;
268}
269
270static int coh901331_resume(struct platform_device *pdev)
271{
272 struct coh901331_port *rtap = dev_get_drvdata(&pdev->dev);
273
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);
280 return 0;
281}
282#else
283#define coh901331_suspend NULL
284#define coh901331_resume NULL
285#endif
286
287static void coh901331_shutdown(struct platform_device *pdev)
288{
289 struct coh901331_port *rtap = dev_get_drvdata(&pdev->dev);
290
291 clk_enable(rtap->clk);
292 writel(0, rtap->virtbase + COH901331_IRQ_MASK);
293 clk_disable(rtap->clk);
294}
295
296static struct platform_driver coh901331_driver = {
297 .driver = {
298 .name = "rtc-coh901331",
299 .owner = THIS_MODULE,
300 },
301 .remove = __exit_p(coh901331_remove),
302 .suspend = coh901331_suspend,
303 .resume = coh901331_resume,
304 .shutdown = coh901331_shutdown,
305};
306
307static int __init coh901331_init(void)
308{
309 return platform_driver_probe(&coh901331_driver, coh901331_probe);
310}
311
312static void __exit coh901331_exit(void)
313{
314 platform_driver_unregister(&coh901331_driver);
315}
316
317module_init(coh901331_init);
318module_exit(coh901331_exit);
319
320MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
321MODULE_DESCRIPTION("ST-Ericsson AB COH 901 331 RTC Driver");
322MODULE_LICENSE("GPL");