JFFS2: fix min/max confusion
[firewire-audio.git] / drivers / rtc / rtc-coh901331.c
blob7fe1fa26c52c5e202287db14ea5cf2551f7031e2
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);
61 clk_disable(rtap->clk);
62 /* Set alarm flag */
63 rtc_update_irq(rtap->rtc, 1, RTC_AF);
65 return IRQ_HANDLED;
68 static int coh901331_read_time(struct device *dev, struct rtc_time *tm)
70 struct coh901331_port *rtap = dev_get_drvdata(dev);
72 clk_enable(rtap->clk);
73 /* Check if the time is valid */
74 if (readl(rtap->virtbase + COH901331_VALID)) {
75 rtc_time_to_tm(readl(rtap->virtbase + COH901331_CUR_TIME), tm);
76 clk_disable(rtap->clk);
77 return rtc_valid_tm(tm);
79 clk_disable(rtap->clk);
80 return -EINVAL;
83 static int coh901331_set_mmss(struct device *dev, unsigned long secs)
85 struct coh901331_port *rtap = dev_get_drvdata(dev);
87 clk_enable(rtap->clk);
88 writel(secs, rtap->virtbase + COH901331_SET_TIME);
89 clk_disable(rtap->clk);
91 return 0;
94 static int coh901331_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
96 struct coh901331_port *rtap = dev_get_drvdata(dev);
98 clk_enable(rtap->clk);
99 rtc_time_to_tm(readl(rtap->virtbase + COH901331_ALARM), &alarm->time);
100 alarm->pending = readl(rtap->virtbase + COH901331_IRQ_EVENT) & 1U;
101 alarm->enabled = readl(rtap->virtbase + COH901331_IRQ_MASK) & 1U;
102 clk_disable(rtap->clk);
104 return 0;
107 static int coh901331_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
109 struct coh901331_port *rtap = dev_get_drvdata(dev);
110 unsigned long time;
112 rtc_tm_to_time(&alarm->time, &time);
113 clk_enable(rtap->clk);
114 writel(time, rtap->virtbase + COH901331_ALARM);
115 writel(alarm->enabled, rtap->virtbase + COH901331_IRQ_MASK);
116 clk_disable(rtap->clk);
118 return 0;
121 static int coh901331_alarm_irq_enable(struct device *dev, unsigned int enabled)
123 struct coh901331_port *rtap = dev_get_drvdata(dev);
125 clk_enable(rtap->clk);
126 if (enabled)
127 writel(1, rtap->virtbase + COH901331_IRQ_MASK);
128 else
129 writel(0, rtap->virtbase + COH901331_IRQ_MASK);
130 clk_disable(rtap->clk);
133 static struct rtc_class_ops coh901331_ops = {
134 .read_time = coh901331_read_time,
135 .set_mmss = coh901331_set_mmss,
136 .read_alarm = coh901331_read_alarm,
137 .set_alarm = coh901331_set_alarm,
138 .alarm_irq_enable = coh901331_alarm_irq_enable,
141 static int __exit coh901331_remove(struct platform_device *pdev)
143 struct coh901331_port *rtap = dev_get_drvdata(&pdev->dev);
145 if (rtap) {
146 free_irq(rtap->irq, rtap);
147 rtc_device_unregister(rtap->rtc);
148 clk_put(rtap->clk);
149 iounmap(rtap->virtbase);
150 release_mem_region(rtap->phybase, rtap->physize);
151 platform_set_drvdata(pdev, NULL);
152 kfree(rtap);
155 return 0;
159 static int __init coh901331_probe(struct platform_device *pdev)
161 int ret;
162 struct coh901331_port *rtap;
163 struct resource *res;
165 rtap = kzalloc(sizeof(struct coh901331_port), GFP_KERNEL);
166 if (!rtap)
167 return -ENOMEM;
169 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
170 if (!res) {
171 ret = -ENOENT;
172 goto out_no_resource;
174 rtap->phybase = res->start;
175 rtap->physize = resource_size(res);
177 if (request_mem_region(rtap->phybase, rtap->physize,
178 "rtc-coh901331") == NULL) {
179 ret = -EBUSY;
180 goto out_no_memregion;
183 rtap->virtbase = ioremap(rtap->phybase, rtap->physize);
184 if (!rtap->virtbase) {
185 ret = -ENOMEM;
186 goto out_no_remap;
189 rtap->irq = platform_get_irq(pdev, 0);
190 if (request_irq(rtap->irq, coh901331_interrupt, IRQF_DISABLED,
191 "RTC COH 901 331 Alarm", rtap)) {
192 ret = -EIO;
193 goto out_no_irq;
196 rtap->clk = clk_get(&pdev->dev, NULL);
197 if (IS_ERR(rtap->clk)) {
198 ret = PTR_ERR(rtap->clk);
199 dev_err(&pdev->dev, "could not get clock\n");
200 goto out_no_clk;
203 /* We enable/disable the clock only to assure it works */
204 ret = clk_enable(rtap->clk);
205 if (ret) {
206 dev_err(&pdev->dev, "could not enable clock\n");
207 goto out_no_clk_enable;
209 clk_disable(rtap->clk);
211 rtap->rtc = rtc_device_register("coh901331", &pdev->dev, &coh901331_ops,
212 THIS_MODULE);
213 if (IS_ERR(rtap->rtc)) {
214 ret = PTR_ERR(rtap->rtc);
215 goto out_no_rtc;
218 platform_set_drvdata(pdev, rtap);
220 return 0;
222 out_no_rtc:
223 out_no_clk_enable:
224 clk_put(rtap->clk);
225 out_no_clk:
226 free_irq(rtap->irq, rtap);
227 out_no_irq:
228 iounmap(rtap->virtbase);
229 out_no_remap:
230 platform_set_drvdata(pdev, NULL);
231 out_no_memregion:
232 release_mem_region(rtap->phybase, SZ_4K);
233 out_no_resource:
234 kfree(rtap);
235 return ret;
238 #ifdef CONFIG_PM
239 static int coh901331_suspend(struct platform_device *pdev, pm_message_t state)
241 struct coh901331_port *rtap = dev_get_drvdata(&pdev->dev);
244 * If this RTC alarm will be used for waking the system up,
245 * don't disable it of course. Else we just disable the alarm
246 * and await suspension.
248 if (device_may_wakeup(&pdev->dev)) {
249 enable_irq_wake(rtap->irq);
250 } else {
251 clk_enable(rtap->clk);
252 rtap->irqmaskstore = readl(rtap->virtbase + COH901331_IRQ_MASK);
253 writel(0, rtap->virtbase + COH901331_IRQ_MASK);
254 clk_disable(rtap->clk);
256 return 0;
259 static int coh901331_resume(struct platform_device *pdev)
261 struct coh901331_port *rtap = dev_get_drvdata(&pdev->dev);
263 if (device_may_wakeup(&pdev->dev))
264 disable_irq_wake(rtap->irq);
265 else
266 clk_enable(rtap->clk);
267 writel(rtap->irqmaskstore, rtap->virtbase + COH901331_IRQ_MASK);
268 clk_disable(rtap->clk);
269 return 0;
271 #else
272 #define coh901331_suspend NULL
273 #define coh901331_resume NULL
274 #endif
276 static void coh901331_shutdown(struct platform_device *pdev)
278 struct coh901331_port *rtap = dev_get_drvdata(&pdev->dev);
280 clk_enable(rtap->clk);
281 writel(0, rtap->virtbase + COH901331_IRQ_MASK);
282 clk_disable(rtap->clk);
285 static struct platform_driver coh901331_driver = {
286 .driver = {
287 .name = "rtc-coh901331",
288 .owner = THIS_MODULE,
290 .remove = __exit_p(coh901331_remove),
291 .suspend = coh901331_suspend,
292 .resume = coh901331_resume,
293 .shutdown = coh901331_shutdown,
296 static int __init coh901331_init(void)
298 return platform_driver_probe(&coh901331_driver, coh901331_probe);
301 static void __exit coh901331_exit(void)
303 platform_driver_unregister(&coh901331_driver);
306 module_init(coh901331_init);
307 module_exit(coh901331_exit);
309 MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
310 MODULE_DESCRIPTION("ST-Ericsson AB COH 901 331 RTC Driver");
311 MODULE_LICENSE("GPL");