1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007-2009 ST-Ericsson AB
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/mod_devicetable.h>
12 #include <linux/rtc.h>
13 #include <linux/clk.h>
14 #include <linux/interrupt.h>
16 #include <linux/platform_device.h>
18 #include <linux/slab.h>
21 * Registers in the COH 901 331
23 /* Alarm value 32bit (R/W) */
24 #define COH901331_ALARM 0x00U
25 /* Used to set current time 32bit (R/W) */
26 #define COH901331_SET_TIME 0x04U
27 /* Indication if current time is valid 32bit (R/-) */
28 #define COH901331_VALID 0x08U
29 /* Read the current time 32bit (R/-) */
30 #define COH901331_CUR_TIME 0x0cU
31 /* Event register for the "alarm" interrupt */
32 #define COH901331_IRQ_EVENT 0x10U
33 /* Mask register for the "alarm" interrupt */
34 #define COH901331_IRQ_MASK 0x14U
35 /* Force register for the "alarm" interrupt */
36 #define COH901331_IRQ_FORCE 0x18U
39 * Reference to RTC block clock
40 * Notice that the frequent clk_enable()/clk_disable() on this
41 * clock is mainly to be able to turn on/off other clocks in the
42 * hierarchy as needed, the RTC clock is always on anyway.
44 struct coh901331_port
{
45 struct rtc_device
*rtc
;
47 void __iomem
*virtbase
;
49 #ifdef CONFIG_PM_SLEEP
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 clk_disable(rtap
->clk
);
88 rtc_time64_to_tm(readl(rtap
->virtbase
+ COH901331_CUR_TIME
), tm
);
89 clk_disable(rtap
->clk
);
93 static int coh901331_set_time(struct device
*dev
, struct rtc_time
*tm
)
95 struct coh901331_port
*rtap
= dev_get_drvdata(dev
);
97 clk_enable(rtap
->clk
);
98 writel(rtc_tm_to_time64(tm
), 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_time64_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
);
120 unsigned long time
= rtc_tm_to_time64(&alarm
->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 const struct rtc_class_ops coh901331_ops
= {
145 .read_time
= coh901331_read_time
,
146 .set_time
= coh901331_set_time
,
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
= platform_get_drvdata(pdev
);
157 clk_unprepare(rtap
->clk
);
163 static int __init
coh901331_probe(struct platform_device
*pdev
)
166 struct coh901331_port
*rtap
;
167 struct resource
*res
;
169 rtap
= devm_kzalloc(&pdev
->dev
,
170 sizeof(struct coh901331_port
), GFP_KERNEL
);
174 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
175 rtap
->virtbase
= devm_ioremap_resource(&pdev
->dev
, res
);
176 if (IS_ERR(rtap
->virtbase
))
177 return PTR_ERR(rtap
->virtbase
);
179 rtap
->irq
= platform_get_irq(pdev
, 0);
180 if (devm_request_irq(&pdev
->dev
, rtap
->irq
, coh901331_interrupt
, 0,
181 "RTC COH 901 331 Alarm", rtap
))
184 rtap
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
185 if (IS_ERR(rtap
->clk
)) {
186 ret
= PTR_ERR(rtap
->clk
);
187 dev_err(&pdev
->dev
, "could not get clock\n");
191 rtap
->rtc
= devm_rtc_allocate_device(&pdev
->dev
);
192 if (IS_ERR(rtap
->rtc
))
193 return PTR_ERR(rtap
->rtc
);
195 rtap
->rtc
->ops
= &coh901331_ops
;
196 rtap
->rtc
->range_max
= U32_MAX
;
198 /* We enable/disable the clock only to assure it works */
199 ret
= clk_prepare_enable(rtap
->clk
);
201 dev_err(&pdev
->dev
, "could not enable clock\n");
204 clk_disable(rtap
->clk
);
206 platform_set_drvdata(pdev
, rtap
);
208 ret
= rtc_register_device(rtap
->rtc
);
215 clk_unprepare(rtap
->clk
);
219 #ifdef CONFIG_PM_SLEEP
220 static int coh901331_suspend(struct device
*dev
)
222 struct coh901331_port
*rtap
= dev_get_drvdata(dev
);
225 * If this RTC alarm will be used for waking the system up,
226 * don't disable it of course. Else we just disable the alarm
227 * and await suspension.
229 if (device_may_wakeup(dev
)) {
230 enable_irq_wake(rtap
->irq
);
232 clk_enable(rtap
->clk
);
233 rtap
->irqmaskstore
= readl(rtap
->virtbase
+ COH901331_IRQ_MASK
);
234 writel(0, rtap
->virtbase
+ COH901331_IRQ_MASK
);
235 clk_disable(rtap
->clk
);
237 clk_unprepare(rtap
->clk
);
241 static int coh901331_resume(struct device
*dev
)
244 struct coh901331_port
*rtap
= dev_get_drvdata(dev
);
246 ret
= clk_prepare(rtap
->clk
);
250 if (device_may_wakeup(dev
)) {
251 disable_irq_wake(rtap
->irq
);
253 clk_enable(rtap
->clk
);
254 writel(rtap
->irqmaskstore
, rtap
->virtbase
+ COH901331_IRQ_MASK
);
255 clk_disable(rtap
->clk
);
261 static SIMPLE_DEV_PM_OPS(coh901331_pm_ops
, coh901331_suspend
, coh901331_resume
);
263 static void coh901331_shutdown(struct platform_device
*pdev
)
265 struct coh901331_port
*rtap
= platform_get_drvdata(pdev
);
267 clk_enable(rtap
->clk
);
268 writel(0, rtap
->virtbase
+ COH901331_IRQ_MASK
);
269 clk_disable_unprepare(rtap
->clk
);
272 static const struct of_device_id coh901331_dt_match
[] = {
273 { .compatible
= "stericsson,coh901331" },
276 MODULE_DEVICE_TABLE(of
, coh901331_dt_match
);
278 static struct platform_driver coh901331_driver
= {
280 .name
= "rtc-coh901331",
281 .pm
= &coh901331_pm_ops
,
282 .of_match_table
= coh901331_dt_match
,
284 .remove
= __exit_p(coh901331_remove
),
285 .shutdown
= coh901331_shutdown
,
288 module_platform_driver_probe(coh901331_driver
, coh901331_probe
);
290 MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
291 MODULE_DESCRIPTION("ST-Ericsson AB COH 901 331 RTC Driver");
292 MODULE_LICENSE("GPL");