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>
17 #include <linux/slab.h>
20 * Registers in the COH 901 331
22 /* Alarm value 32bit (R/W) */
23 #define COH901331_ALARM 0x00U
24 /* Used to set current time 32bit (R/W) */
25 #define COH901331_SET_TIME 0x04U
26 /* Indication if current time is valid 32bit (R/-) */
27 #define COH901331_VALID 0x08U
28 /* Read the current time 32bit (R/-) */
29 #define COH901331_CUR_TIME 0x0cU
30 /* Event register for the "alarm" interrupt */
31 #define COH901331_IRQ_EVENT 0x10U
32 /* Mask register for the "alarm" interrupt */
33 #define COH901331_IRQ_MASK 0x14U
34 /* Force register for the "alarm" interrupt */
35 #define COH901331_IRQ_FORCE 0x18U
38 * Reference to RTC block clock
39 * Notice that the frequent clk_enable()/clk_disable() on this
40 * clock is mainly to be able to turn on/off other clocks in the
41 * hierarchy as needed, the RTC clock is always on anyway.
43 struct coh901331_port
{
44 struct rtc_device
*rtc
;
48 void __iomem
*virtbase
;
50 #ifdef CONFIG_PM_SLEEP
55 static irqreturn_t
coh901331_interrupt(int irq
, void *data
)
57 struct coh901331_port
*rtap
= data
;
59 clk_enable(rtap
->clk
);
61 writel(1, rtap
->virtbase
+ COH901331_IRQ_EVENT
);
63 * Disable the interrupt. This is necessary because
64 * the RTC lives on a lower-clocked line and will
65 * not release the IRQ line until after a few (slower)
66 * clock cycles. The interrupt will be re-enabled when
67 * a new alarm is set anyway.
69 writel(0, rtap
->virtbase
+ COH901331_IRQ_MASK
);
70 clk_disable(rtap
->clk
);
73 rtc_update_irq(rtap
->rtc
, 1, RTC_AF
);
78 static int coh901331_read_time(struct device
*dev
, struct rtc_time
*tm
)
80 struct coh901331_port
*rtap
= dev_get_drvdata(dev
);
82 clk_enable(rtap
->clk
);
83 /* Check if the time is valid */
84 if (readl(rtap
->virtbase
+ COH901331_VALID
)) {
85 rtc_time_to_tm(readl(rtap
->virtbase
+ COH901331_CUR_TIME
), tm
);
86 clk_disable(rtap
->clk
);
87 return rtc_valid_tm(tm
);
89 clk_disable(rtap
->clk
);
93 static int coh901331_set_mmss(struct device
*dev
, unsigned long secs
)
95 struct coh901331_port
*rtap
= dev_get_drvdata(dev
);
97 clk_enable(rtap
->clk
);
98 writel(secs
, rtap
->virtbase
+ COH901331_SET_TIME
);
99 clk_disable(rtap
->clk
);
104 static int coh901331_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
106 struct coh901331_port
*rtap
= dev_get_drvdata(dev
);
108 clk_enable(rtap
->clk
);
109 rtc_time_to_tm(readl(rtap
->virtbase
+ COH901331_ALARM
), &alarm
->time
);
110 alarm
->pending
= readl(rtap
->virtbase
+ COH901331_IRQ_EVENT
) & 1U;
111 alarm
->enabled
= readl(rtap
->virtbase
+ COH901331_IRQ_MASK
) & 1U;
112 clk_disable(rtap
->clk
);
117 static int coh901331_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
119 struct coh901331_port
*rtap
= dev_get_drvdata(dev
);
122 rtc_tm_to_time(&alarm
->time
, &time
);
123 clk_enable(rtap
->clk
);
124 writel(time
, rtap
->virtbase
+ COH901331_ALARM
);
125 writel(alarm
->enabled
, rtap
->virtbase
+ COH901331_IRQ_MASK
);
126 clk_disable(rtap
->clk
);
131 static int coh901331_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
133 struct coh901331_port
*rtap
= dev_get_drvdata(dev
);
135 clk_enable(rtap
->clk
);
137 writel(1, rtap
->virtbase
+ COH901331_IRQ_MASK
);
139 writel(0, rtap
->virtbase
+ COH901331_IRQ_MASK
);
140 clk_disable(rtap
->clk
);
145 static struct rtc_class_ops coh901331_ops
= {
146 .read_time
= coh901331_read_time
,
147 .set_mmss
= coh901331_set_mmss
,
148 .read_alarm
= coh901331_read_alarm
,
149 .set_alarm
= coh901331_set_alarm
,
150 .alarm_irq_enable
= coh901331_alarm_irq_enable
,
153 static int __exit
coh901331_remove(struct platform_device
*pdev
)
155 struct coh901331_port
*rtap
= dev_get_drvdata(&pdev
->dev
);
158 clk_unprepare(rtap
->clk
);
159 platform_set_drvdata(pdev
, NULL
);
166 static int __init
coh901331_probe(struct platform_device
*pdev
)
169 struct coh901331_port
*rtap
;
170 struct resource
*res
;
172 rtap
= devm_kzalloc(&pdev
->dev
,
173 sizeof(struct coh901331_port
), GFP_KERNEL
);
177 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
181 rtap
->phybase
= res
->start
;
182 rtap
->physize
= resource_size(res
);
184 if (devm_request_mem_region(&pdev
->dev
, rtap
->phybase
, rtap
->physize
,
185 "rtc-coh901331") == NULL
)
188 rtap
->virtbase
= devm_ioremap(&pdev
->dev
, rtap
->phybase
, rtap
->physize
);
192 rtap
->irq
= platform_get_irq(pdev
, 0);
193 if (devm_request_irq(&pdev
->dev
, rtap
->irq
, coh901331_interrupt
, 0,
194 "RTC COH 901 331 Alarm", rtap
))
197 rtap
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
198 if (IS_ERR(rtap
->clk
)) {
199 ret
= PTR_ERR(rtap
->clk
);
200 dev_err(&pdev
->dev
, "could not get clock\n");
204 /* We enable/disable the clock only to assure it works */
205 ret
= clk_prepare_enable(rtap
->clk
);
207 dev_err(&pdev
->dev
, "could not enable clock\n");
210 clk_disable(rtap
->clk
);
212 platform_set_drvdata(pdev
, rtap
);
213 rtap
->rtc
= devm_rtc_device_register(&pdev
->dev
, "coh901331",
214 &coh901331_ops
, THIS_MODULE
);
215 if (IS_ERR(rtap
->rtc
)) {
216 ret
= PTR_ERR(rtap
->rtc
);
223 platform_set_drvdata(pdev
, NULL
);
224 clk_unprepare(rtap
->clk
);
228 #ifdef CONFIG_PM_SLEEP
229 static int coh901331_suspend(struct device
*dev
)
231 struct coh901331_port
*rtap
= dev_get_drvdata(dev
);
234 * If this RTC alarm will be used for waking the system up,
235 * don't disable it of course. Else we just disable the alarm
236 * and await suspension.
238 if (device_may_wakeup(dev
)) {
239 enable_irq_wake(rtap
->irq
);
241 clk_enable(rtap
->clk
);
242 rtap
->irqmaskstore
= readl(rtap
->virtbase
+ COH901331_IRQ_MASK
);
243 writel(0, rtap
->virtbase
+ COH901331_IRQ_MASK
);
244 clk_disable(rtap
->clk
);
246 clk_unprepare(rtap
->clk
);
250 static int coh901331_resume(struct device
*dev
)
252 struct coh901331_port
*rtap
= dev_get_drvdata(dev
);
254 clk_prepare(rtap
->clk
);
255 if (device_may_wakeup(dev
)) {
256 disable_irq_wake(rtap
->irq
);
258 clk_enable(rtap
->clk
);
259 writel(rtap
->irqmaskstore
, rtap
->virtbase
+ COH901331_IRQ_MASK
);
260 clk_disable(rtap
->clk
);
266 static SIMPLE_DEV_PM_OPS(coh901331_pm_ops
, coh901331_suspend
, coh901331_resume
);
268 static void coh901331_shutdown(struct platform_device
*pdev
)
270 struct coh901331_port
*rtap
= dev_get_drvdata(&pdev
->dev
);
272 clk_enable(rtap
->clk
);
273 writel(0, rtap
->virtbase
+ COH901331_IRQ_MASK
);
274 clk_disable_unprepare(rtap
->clk
);
277 static struct platform_driver coh901331_driver
= {
279 .name
= "rtc-coh901331",
280 .owner
= THIS_MODULE
,
281 .pm
= &coh901331_pm_ops
,
283 .remove
= __exit_p(coh901331_remove
),
284 .shutdown
= coh901331_shutdown
,
287 module_platform_driver_probe(coh901331_driver
, coh901331_probe
);
289 MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
290 MODULE_DESCRIPTION("ST-Ericsson AB COH 901 331 RTC Driver");
291 MODULE_LICENSE("GPL");