blob: 360eae24a8c8ce09644fb084fb99d5b248b1b072 [file] [log] [blame]
Loc Hof12d8692014-06-06 14:35:42 -07001/*
2 * APM X-Gene SoC Real Time Clock Driver
3 *
4 * Copyright (c) 2014, Applied Micro Circuits Corporation
5 * Author: Rameshwar Prasad Sahu <rsahu@apm.com>
6 * Loc Ho <lho@apm.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#include <linux/init.h>
24#include <linux/module.h>
25#include <linux/of.h>
26#include <linux/platform_device.h>
27#include <linux/io.h>
28#include <linux/slab.h>
29#include <linux/clk.h>
30#include <linux/delay.h>
31#include <linux/rtc.h>
32
33/* RTC CSR Registers */
34#define RTC_CCVR 0x00
35#define RTC_CMR 0x04
36#define RTC_CLR 0x08
37#define RTC_CCR 0x0C
38#define RTC_CCR_IE BIT(0)
39#define RTC_CCR_MASK BIT(1)
40#define RTC_CCR_EN BIT(2)
41#define RTC_CCR_WEN BIT(3)
42#define RTC_STAT 0x10
43#define RTC_STAT_BIT BIT(0)
44#define RTC_RSTAT 0x14
45#define RTC_EOI 0x18
46#define RTC_VER 0x1C
47
48struct xgene_rtc_dev {
49 struct rtc_device *rtc;
50 struct device *dev;
51 unsigned long alarm_time;
52 void __iomem *csr_base;
53 struct clk *clk;
54 unsigned int irq_wake;
Loc Hod0bcd822014-04-14 12:09:04 -060055 unsigned int irq_enabled;
Loc Hof12d8692014-06-06 14:35:42 -070056};
57
58static int xgene_rtc_read_time(struct device *dev, struct rtc_time *tm)
59{
60 struct xgene_rtc_dev *pdata = dev_get_drvdata(dev);
61
62 rtc_time_to_tm(readl(pdata->csr_base + RTC_CCVR), tm);
63 return rtc_valid_tm(tm);
64}
65
66static int xgene_rtc_set_mmss(struct device *dev, unsigned long secs)
67{
68 struct xgene_rtc_dev *pdata = dev_get_drvdata(dev);
69
70 /*
71 * NOTE: After the following write, the RTC_CCVR is only reflected
72 * after the update cycle of 1 seconds.
73 */
74 writel((u32) secs, pdata->csr_base + RTC_CLR);
75 readl(pdata->csr_base + RTC_CLR); /* Force a barrier */
76
77 return 0;
78}
79
80static int xgene_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
81{
82 struct xgene_rtc_dev *pdata = dev_get_drvdata(dev);
83
84 rtc_time_to_tm(pdata->alarm_time, &alrm->time);
85 alrm->enabled = readl(pdata->csr_base + RTC_CCR) & RTC_CCR_IE;
86
87 return 0;
88}
89
90static int xgene_rtc_alarm_irq_enable(struct device *dev, u32 enabled)
91{
92 struct xgene_rtc_dev *pdata = dev_get_drvdata(dev);
93 u32 ccr;
94
95 ccr = readl(pdata->csr_base + RTC_CCR);
96 if (enabled) {
97 ccr &= ~RTC_CCR_MASK;
98 ccr |= RTC_CCR_IE;
99 } else {
100 ccr &= ~RTC_CCR_IE;
101 ccr |= RTC_CCR_MASK;
102 }
103 writel(ccr, pdata->csr_base + RTC_CCR);
104
105 return 0;
106}
107
Loc Hod0bcd822014-04-14 12:09:04 -0600108static int xgene_rtc_alarm_irq_enabled(struct device *dev)
109{
110 struct xgene_rtc_dev *pdata = dev_get_drvdata(dev);
111
112 return readl(pdata->csr_base + RTC_CCR) & RTC_CCR_IE ? 1 : 0;
113}
114
Loc Hof12d8692014-06-06 14:35:42 -0700115static int xgene_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
116{
117 struct xgene_rtc_dev *pdata = dev_get_drvdata(dev);
Loc Hof12d8692014-06-06 14:35:42 -0700118 unsigned long alarm_time;
119
Loc Hof12d8692014-06-06 14:35:42 -0700120 rtc_tm_to_time(&alrm->time, &alarm_time);
Loc Hof12d8692014-06-06 14:35:42 -0700121 pdata->alarm_time = alarm_time;
122 writel((u32) pdata->alarm_time, pdata->csr_base + RTC_CMR);
123
124 xgene_rtc_alarm_irq_enable(dev, alrm->enabled);
125
126 return 0;
127}
128
129static const struct rtc_class_ops xgene_rtc_ops = {
130 .read_time = xgene_rtc_read_time,
131 .set_mmss = xgene_rtc_set_mmss,
132 .read_alarm = xgene_rtc_read_alarm,
133 .set_alarm = xgene_rtc_set_alarm,
134 .alarm_irq_enable = xgene_rtc_alarm_irq_enable,
135};
136
137static irqreturn_t xgene_rtc_interrupt(int irq, void *id)
138{
139 struct xgene_rtc_dev *pdata = (struct xgene_rtc_dev *) id;
140
141 /* Check if interrupt asserted */
142 if (!(readl(pdata->csr_base + RTC_STAT) & RTC_STAT_BIT))
143 return IRQ_NONE;
144
145 /* Clear interrupt */
146 readl(pdata->csr_base + RTC_EOI);
147
148 rtc_update_irq(pdata->rtc, 1, RTC_IRQF | RTC_AF);
149
150 return IRQ_HANDLED;
151}
152
153static int xgene_rtc_probe(struct platform_device *pdev)
154{
155 struct xgene_rtc_dev *pdata;
156 struct resource *res;
157 int ret;
158 int irq;
159
160 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
161 if (!pdata)
162 return -ENOMEM;
163 platform_set_drvdata(pdev, pdata);
164 pdata->dev = &pdev->dev;
165
166 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
167 pdata->csr_base = devm_ioremap_resource(&pdev->dev, res);
168 if (IS_ERR(pdata->csr_base))
169 return PTR_ERR(pdata->csr_base);
170
171 irq = platform_get_irq(pdev, 0);
172 if (irq < 0) {
173 dev_err(&pdev->dev, "No IRQ resource\n");
174 return irq;
175 }
176 ret = devm_request_irq(&pdev->dev, irq, xgene_rtc_interrupt, 0,
177 dev_name(&pdev->dev), pdata);
178 if (ret) {
179 dev_err(&pdev->dev, "Could not request IRQ\n");
180 return ret;
181 }
182
183 pdata->clk = devm_clk_get(&pdev->dev, NULL);
184 if (IS_ERR(pdata->clk)) {
185 dev_err(&pdev->dev, "Couldn't get the clock for RTC\n");
186 return -ENODEV;
187 }
Loc Hod0bcd822014-04-14 12:09:04 -0600188 ret = clk_prepare_enable(pdata->clk);
189 if (ret)
190 return ret;
Loc Hof12d8692014-06-06 14:35:42 -0700191
192 /* Turn on the clock and the crystal */
193 writel(RTC_CCR_EN, pdata->csr_base + RTC_CCR);
194
Loc Hod0bcd822014-04-14 12:09:04 -0600195 ret = device_init_wakeup(&pdev->dev, 1);
196 if (ret) {
197 clk_disable_unprepare(pdata->clk);
198 return ret;
199 }
Loc Hof12d8692014-06-06 14:35:42 -0700200
201 pdata->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
202 &xgene_rtc_ops, THIS_MODULE);
203 if (IS_ERR(pdata->rtc)) {
204 clk_disable_unprepare(pdata->clk);
205 return PTR_ERR(pdata->rtc);
206 }
207
208 /* HW does not support update faster than 1 seconds */
209 pdata->rtc->uie_unsupported = 1;
210
211 return 0;
212}
213
214static int xgene_rtc_remove(struct platform_device *pdev)
215{
216 struct xgene_rtc_dev *pdata = platform_get_drvdata(pdev);
217
218 xgene_rtc_alarm_irq_enable(&pdev->dev, 0);
219 device_init_wakeup(&pdev->dev, 0);
220 clk_disable_unprepare(pdata->clk);
221 return 0;
222}
223
224#ifdef CONFIG_PM_SLEEP
225static int xgene_rtc_suspend(struct device *dev)
226{
227 struct platform_device *pdev = to_platform_device(dev);
228 struct xgene_rtc_dev *pdata = platform_get_drvdata(pdev);
229 int irq;
230
231 irq = platform_get_irq(pdev, 0);
Loc Hod0bcd822014-04-14 12:09:04 -0600232
233 /*
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.
237 */
Loc Hof12d8692014-06-06 14:35:42 -0700238 if (device_may_wakeup(&pdev->dev)) {
239 if (!enable_irq_wake(irq))
240 pdata->irq_wake = 1;
241 } else {
Loc Hod0bcd822014-04-14 12:09:04 -0600242 pdata->irq_enabled = xgene_rtc_alarm_irq_enabled(dev);
Loc Hof12d8692014-06-06 14:35:42 -0700243 xgene_rtc_alarm_irq_enable(dev, 0);
Loc Hod0bcd822014-04-14 12:09:04 -0600244 clk_disable_unprepare(pdata->clk);
Loc Hof12d8692014-06-06 14:35:42 -0700245 }
Loc Hof12d8692014-06-06 14:35:42 -0700246 return 0;
247}
248
249static int xgene_rtc_resume(struct device *dev)
250{
251 struct platform_device *pdev = to_platform_device(dev);
252 struct xgene_rtc_dev *pdata = platform_get_drvdata(pdev);
253 int irq;
Loc Hod0bcd822014-04-14 12:09:04 -0600254 int rc;
Loc Hof12d8692014-06-06 14:35:42 -0700255
256 irq = platform_get_irq(pdev, 0);
Loc Hod0bcd822014-04-14 12:09:04 -0600257
Loc Hof12d8692014-06-06 14:35:42 -0700258 if (device_may_wakeup(&pdev->dev)) {
259 if (pdata->irq_wake) {
260 disable_irq_wake(irq);
261 pdata->irq_wake = 0;
262 }
263 } else {
Loc Hod0bcd822014-04-14 12:09:04 -0600264 rc = clk_prepare_enable(pdata->clk);
265 if (rc) {
266 dev_err(dev, "Unable to enable clock error %d\n", rc);
267 return rc;
268 }
269 xgene_rtc_alarm_irq_enable(dev, pdata->irq_enabled);
Loc Hof12d8692014-06-06 14:35:42 -0700270 }
271
272 return 0;
273}
274#endif
275
276static SIMPLE_DEV_PM_OPS(xgene_rtc_pm_ops, xgene_rtc_suspend, xgene_rtc_resume);
277
278#ifdef CONFIG_OF
279static const struct of_device_id xgene_rtc_of_match[] = {
280 {.compatible = "apm,xgene-rtc" },
281 { }
282};
283MODULE_DEVICE_TABLE(of, xgene_rtc_of_match);
284#endif
285
286static struct platform_driver xgene_rtc_driver = {
287 .probe = xgene_rtc_probe,
288 .remove = xgene_rtc_remove,
289 .driver = {
Loc Hof12d8692014-06-06 14:35:42 -0700290 .name = "xgene-rtc",
291 .pm = &xgene_rtc_pm_ops,
292 .of_match_table = of_match_ptr(xgene_rtc_of_match),
293 },
294};
295
296module_platform_driver(xgene_rtc_driver);
297
298MODULE_DESCRIPTION("APM X-Gene SoC RTC driver");
299MODULE_AUTHOR("Rameshwar Sahu <rsahu@apm.com>");
300MODULE_LICENSE("GPL");