Merge tag 'arc-5.1-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/vgupta/arc
[linux-2.6-block.git] / drivers / rtc / rtc-coh901331.c
CommitLineData
aa958f57
LW
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>
ac316725 11#include <linux/mod_devicetable.h>
aa958f57
LW
12#include <linux/rtc.h>
13#include <linux/clk.h>
14#include <linux/interrupt.h>
15#include <linux/pm.h>
16#include <linux/platform_device.h>
17#include <linux/io.h>
5a0e3ad6 18#include <linux/slab.h>
aa958f57
LW
19
20/*
21 * Registers in the COH 901 331
22 */
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
37
38/*
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.
43 */
44struct coh901331_port {
45 struct rtc_device *rtc;
46 struct clk *clk;
aa958f57
LW
47 void __iomem *virtbase;
48 int irq;
62068e2c 49#ifdef CONFIG_PM_SLEEP
aa958f57
LW
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);
378ce74b
LW
61 /*
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);
aa958f57 69 clk_disable(rtap->clk);
378ce74b 70
aa958f57
LW
71 /* 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);
ab62670e 86 return 0;
aa958f57
LW
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);
378ce74b
LW
140
141 return 0;
aa958f57
LW
142}
143
34c7b3ac 144static const struct rtc_class_ops coh901331_ops = {
aa958f57
LW
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{
3a02893f 154 struct coh901331_port *rtap = platform_get_drvdata(pdev);
aa958f57 155
7fcbf5fd 156 if (rtap)
8384dfeb 157 clk_unprepare(rtap->clk);
aa958f57
LW
158
159 return 0;
160}
161
162
163static int __init coh901331_probe(struct platform_device *pdev)
164{
165 int ret;
166 struct coh901331_port *rtap;
167 struct resource *res;
168
36ac1d24
LW
169 rtap = devm_kzalloc(&pdev->dev,
170 sizeof(struct coh901331_port), GFP_KERNEL);
aa958f57
LW
171 if (!rtap)
172 return -ENOMEM;
173
174 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
daaf90f0
JH
175 rtap->virtbase = devm_ioremap_resource(&pdev->dev, res);
176 if (IS_ERR(rtap->virtbase))
177 return PTR_ERR(rtap->virtbase);
aa958f57
LW
178
179 rtap->irq = platform_get_irq(pdev, 0);
36ac1d24
LW
180 if (devm_request_irq(&pdev->dev, rtap->irq, coh901331_interrupt, 0,
181 "RTC COH 901 331 Alarm", rtap))
182 return -EIO;
aa958f57 183
3b759d7f 184 rtap->clk = devm_clk_get(&pdev->dev, NULL);
aa958f57
LW
185 if (IS_ERR(rtap->clk)) {
186 ret = PTR_ERR(rtap->clk);
187 dev_err(&pdev->dev, "could not get clock\n");
36ac1d24 188 return ret;
aa958f57
LW
189 }
190
191 /* We enable/disable the clock only to assure it works */
8384dfeb 192 ret = clk_prepare_enable(rtap->clk);
aa958f57
LW
193 if (ret) {
194 dev_err(&pdev->dev, "could not enable clock\n");
3b759d7f 195 return ret;
aa958f57
LW
196 }
197 clk_disable(rtap->clk);
198
9cf3b5fa 199 platform_set_drvdata(pdev, rtap);
94b60dba
JH
200 rtap->rtc = devm_rtc_device_register(&pdev->dev, "coh901331",
201 &coh901331_ops, THIS_MODULE);
aa958f57
LW
202 if (IS_ERR(rtap->rtc)) {
203 ret = PTR_ERR(rtap->rtc);
204 goto out_no_rtc;
205 }
206
aa958f57
LW
207 return 0;
208
209 out_no_rtc:
8384dfeb 210 clk_unprepare(rtap->clk);
aa958f57
LW
211 return ret;
212}
213
62068e2c
JH
214#ifdef CONFIG_PM_SLEEP
215static int coh901331_suspend(struct device *dev)
aa958f57 216{
62068e2c 217 struct coh901331_port *rtap = dev_get_drvdata(dev);
aa958f57
LW
218
219 /*
220 * If this RTC alarm will be used for waking the system up,
221 * don't disable it of course. Else we just disable the alarm
222 * and await suspension.
223 */
62068e2c 224 if (device_may_wakeup(dev)) {
aa958f57
LW
225 enable_irq_wake(rtap->irq);
226 } else {
227 clk_enable(rtap->clk);
228 rtap->irqmaskstore = readl(rtap->virtbase + COH901331_IRQ_MASK);
229 writel(0, rtap->virtbase + COH901331_IRQ_MASK);
230 clk_disable(rtap->clk);
231 }
8384dfeb 232 clk_unprepare(rtap->clk);
aa958f57
LW
233 return 0;
234}
235
62068e2c 236static int coh901331_resume(struct device *dev)
aa958f57 237{
5910fa0d 238 int ret;
62068e2c 239 struct coh901331_port *rtap = dev_get_drvdata(dev);
aa958f57 240
5910fa0d
KL
241 ret = clk_prepare(rtap->clk);
242 if (ret)
243 return ret;
244
62068e2c 245 if (device_may_wakeup(dev)) {
aa958f57 246 disable_irq_wake(rtap->irq);
5a98c04d 247 } else {
aa958f57
LW
248 clk_enable(rtap->clk);
249 writel(rtap->irqmaskstore, rtap->virtbase + COH901331_IRQ_MASK);
250 clk_disable(rtap->clk);
5a98c04d 251 }
aa958f57
LW
252 return 0;
253}
aa958f57
LW
254#endif
255
62068e2c
JH
256static SIMPLE_DEV_PM_OPS(coh901331_pm_ops, coh901331_suspend, coh901331_resume);
257
aa958f57
LW
258static void coh901331_shutdown(struct platform_device *pdev)
259{
3a02893f 260 struct coh901331_port *rtap = platform_get_drvdata(pdev);
aa958f57
LW
261
262 clk_enable(rtap->clk);
263 writel(0, rtap->virtbase + COH901331_IRQ_MASK);
828296de 264 clk_disable_unprepare(rtap->clk);
aa958f57
LW
265}
266
a16b6c63
LW
267static const struct of_device_id coh901331_dt_match[] = {
268 { .compatible = "stericsson,coh901331" },
269 {},
270};
73798d5c 271MODULE_DEVICE_TABLE(of, coh901331_dt_match);
a16b6c63 272
aa958f57
LW
273static struct platform_driver coh901331_driver = {
274 .driver = {
275 .name = "rtc-coh901331",
62068e2c 276 .pm = &coh901331_pm_ops,
a16b6c63 277 .of_match_table = coh901331_dt_match,
aa958f57
LW
278 },
279 .remove = __exit_p(coh901331_remove),
aa958f57
LW
280 .shutdown = coh901331_shutdown,
281};
282
4fdf7a9f 283module_platform_driver_probe(coh901331_driver, coh901331_probe);
aa958f57
LW
284
285MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
286MODULE_DESCRIPTION("ST-Ericsson AB COH 901 331 RTC Driver");
287MODULE_LICENSE("GPL");