blob: ac40f971a40e0164e426133849f0a8dab9f97e92 [file] [log] [blame]
dmitry pervushindf17f632009-09-22 16:46:26 -07001/*
2 * Freescale STMP37XX/STMP378X Real Time Clock driver
3 *
4 * Copyright (c) 2007 Sigmatel, Inc.
5 * Peter Hartley, <peter.hartley@sigmatel.com>
6 *
7 * Copyright 2008 Freescale Semiconductor, Inc. All Rights Reserved.
8 * Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
9 */
10
11/*
12 * The code contained herein is licensed under the GNU General Public
13 * License. You may obtain a copy of the GNU General Public License
14 * Version 2 or later at the following locations:
15 *
16 * http://www.opensource.org/licenses/gpl-license.html
17 * http://www.gnu.org/copyleft/gpl.html
18 */
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/init.h>
22#include <linux/platform_device.h>
23#include <linux/interrupt.h>
24#include <linux/rtc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
dmitry pervushindf17f632009-09-22 16:46:26 -070026
Wolfram Sang46b21212011-05-25 12:56:50 +020027#include <mach/common.h>
28#include <mach/mxs.h>
Wolfram Sang47eac332011-05-25 12:56:49 +020029
30#define STMP3XXX_RTC_CTRL 0x0
31#define STMP3XXX_RTC_CTRL_ALARM_IRQ_EN 0x00000001
32#define STMP3XXX_RTC_CTRL_ONEMSEC_IRQ_EN 0x00000002
33#define STMP3XXX_RTC_CTRL_ALARM_IRQ 0x00000004
34#define STMP3XXX_RTC_CTRL_ONEMSEC_IRQ 0x00000008
35
36#define STMP3XXX_RTC_STAT 0x10
37#define STMP3XXX_RTC_STAT_STALE_SHIFT 16
38#define STMP3XXX_RTC_STAT_RTC_PRESENT 0x80000000
39
40#define STMP3XXX_RTC_SECONDS 0x30
41
42#define STMP3XXX_RTC_ALARM 0x40
43
44#define STMP3XXX_RTC_PERSISTENT0 0x60
45#define STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN 0x00000002
46#define STMP3XXX_RTC_PERSISTENT0_ALARM_EN 0x00000004
47#define STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE 0x00000080
dmitry pervushindf17f632009-09-22 16:46:26 -070048
49struct stmp3xxx_rtc_data {
50 struct rtc_device *rtc;
51 unsigned irq_count;
52 void __iomem *io;
53 int irq_alarm, irq_1msec;
54};
55
56static void stmp3xxx_wait_time(struct stmp3xxx_rtc_data *rtc_data)
57{
58 /*
59 * The datasheet doesn't say which way round the
60 * NEW_REGS/STALE_REGS bitfields go. In fact it's 0x1=P0,
61 * 0x2=P1, .., 0x20=P5, 0x40=ALARM, 0x80=SECONDS
62 */
Wolfram Sang47eac332011-05-25 12:56:49 +020063 while (__raw_readl(rtc_data->io + STMP3XXX_RTC_STAT) &
64 (0x80 << STMP3XXX_RTC_STAT_STALE_SHIFT))
dmitry pervushindf17f632009-09-22 16:46:26 -070065 cpu_relax();
66}
67
68/* Time read/write */
69static int stmp3xxx_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
70{
71 struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
72
73 stmp3xxx_wait_time(rtc_data);
Wolfram Sang47eac332011-05-25 12:56:49 +020074 rtc_time_to_tm(__raw_readl(rtc_data->io + STMP3XXX_RTC_SECONDS),
75 rtc_tm);
dmitry pervushindf17f632009-09-22 16:46:26 -070076 return 0;
77}
78
79static int stmp3xxx_rtc_set_mmss(struct device *dev, unsigned long t)
80{
81 struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
82
Wolfram Sang47eac332011-05-25 12:56:49 +020083 __raw_writel(t, rtc_data->io + STMP3XXX_RTC_SECONDS);
dmitry pervushindf17f632009-09-22 16:46:26 -070084 stmp3xxx_wait_time(rtc_data);
85 return 0;
86}
87
88/* interrupt(s) handler */
89static irqreturn_t stmp3xxx_rtc_interrupt(int irq, void *dev_id)
90{
91 struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev_id);
92 u32 status;
93 u32 events = 0;
94
Wolfram Sang47eac332011-05-25 12:56:49 +020095 status = __raw_readl(rtc_data->io + STMP3XXX_RTC_CTRL) &
96 (STMP3XXX_RTC_CTRL_ALARM_IRQ |
97 STMP3XXX_RTC_CTRL_ONEMSEC_IRQ);
dmitry pervushindf17f632009-09-22 16:46:26 -070098
Wolfram Sang47eac332011-05-25 12:56:49 +020099 if (status & STMP3XXX_RTC_CTRL_ALARM_IRQ) {
Wolfram Sang46b21212011-05-25 12:56:50 +0200100 __mxs_clrl(STMP3XXX_RTC_CTRL_ALARM_IRQ,
Wolfram Sang47eac332011-05-25 12:56:49 +0200101 rtc_data->io + STMP3XXX_RTC_CTRL);
dmitry pervushindf17f632009-09-22 16:46:26 -0700102 events |= RTC_AF | RTC_IRQF;
103 }
104
Wolfram Sang47eac332011-05-25 12:56:49 +0200105 if (status & STMP3XXX_RTC_CTRL_ONEMSEC_IRQ) {
Wolfram Sang46b21212011-05-25 12:56:50 +0200106 __mxs_clrl(STMP3XXX_RTC_CTRL_ONEMSEC_IRQ,
Wolfram Sang47eac332011-05-25 12:56:49 +0200107 rtc_data->io + STMP3XXX_RTC_CTRL);
dmitry pervushindf17f632009-09-22 16:46:26 -0700108 if (++rtc_data->irq_count % 1000 == 0) {
109 events |= RTC_UF | RTC_IRQF;
110 rtc_data->irq_count = 0;
111 }
112 }
113
114 if (events)
115 rtc_update_irq(rtc_data->rtc, 1, events);
116
117 return IRQ_HANDLED;
118}
119
120static int stmp3xxx_alarm_irq_enable(struct device *dev, unsigned int enabled)
121{
122 struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
Wolfram Sang47eac332011-05-25 12:56:49 +0200123 void __iomem *p = rtc_data->io + STMP3XXX_RTC_PERSISTENT0,
124 *ctl = rtc_data->io + STMP3XXX_RTC_CTRL;
dmitry pervushindf17f632009-09-22 16:46:26 -0700125
126 if (enabled) {
Wolfram Sang46b21212011-05-25 12:56:50 +0200127 __mxs_setl(STMP3XXX_RTC_PERSISTENT0_ALARM_EN |
Wolfram Sang47eac332011-05-25 12:56:49 +0200128 STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN, p);
Wolfram Sang46b21212011-05-25 12:56:50 +0200129 __mxs_setl(STMP3XXX_RTC_CTRL_ALARM_IRQ_EN, ctl);
dmitry pervushindf17f632009-09-22 16:46:26 -0700130 } else {
Wolfram Sang46b21212011-05-25 12:56:50 +0200131 __mxs_clrl(STMP3XXX_RTC_PERSISTENT0_ALARM_EN |
Wolfram Sang47eac332011-05-25 12:56:49 +0200132 STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN, p);
Wolfram Sang46b21212011-05-25 12:56:50 +0200133 __mxs_clrl(STMP3XXX_RTC_CTRL_ALARM_IRQ_EN, ctl);
dmitry pervushindf17f632009-09-22 16:46:26 -0700134 }
135 return 0;
136}
137
dmitry pervushindf17f632009-09-22 16:46:26 -0700138static int stmp3xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
139{
140 struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
141
Wolfram Sang47eac332011-05-25 12:56:49 +0200142 rtc_time_to_tm(__raw_readl(rtc_data->io + STMP3XXX_RTC_ALARM),
143 &alm->time);
dmitry pervushindf17f632009-09-22 16:46:26 -0700144 return 0;
145}
146
147static int stmp3xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
148{
149 unsigned long t;
150 struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
151
152 rtc_tm_to_time(&alm->time, &t);
Wolfram Sang47eac332011-05-25 12:56:49 +0200153 __raw_writel(t, rtc_data->io + STMP3XXX_RTC_ALARM);
dmitry pervushindf17f632009-09-22 16:46:26 -0700154 return 0;
155}
156
157static struct rtc_class_ops stmp3xxx_rtc_ops = {
158 .alarm_irq_enable =
159 stmp3xxx_alarm_irq_enable,
dmitry pervushindf17f632009-09-22 16:46:26 -0700160 .read_time = stmp3xxx_rtc_gettime,
161 .set_mmss = stmp3xxx_rtc_set_mmss,
162 .read_alarm = stmp3xxx_rtc_read_alarm,
163 .set_alarm = stmp3xxx_rtc_set_alarm,
164};
165
166static int stmp3xxx_rtc_remove(struct platform_device *pdev)
167{
168 struct stmp3xxx_rtc_data *rtc_data = platform_get_drvdata(pdev);
169
170 if (!rtc_data)
171 return 0;
172
Wolfram Sang46b21212011-05-25 12:56:50 +0200173 __mxs_clrl(STMP3XXX_RTC_CTRL_ONEMSEC_IRQ_EN |
Wolfram Sang47eac332011-05-25 12:56:49 +0200174 STMP3XXX_RTC_CTRL_ALARM_IRQ_EN,
175 rtc_data->io + STMP3XXX_RTC_CTRL);
dmitry pervushindf17f632009-09-22 16:46:26 -0700176 free_irq(rtc_data->irq_alarm, &pdev->dev);
177 free_irq(rtc_data->irq_1msec, &pdev->dev);
178 rtc_device_unregister(rtc_data->rtc);
179 iounmap(rtc_data->io);
180 kfree(rtc_data);
181
182 return 0;
183}
184
185static int stmp3xxx_rtc_probe(struct platform_device *pdev)
186{
187 struct stmp3xxx_rtc_data *rtc_data;
188 struct resource *r;
189 int err;
190
191 rtc_data = kzalloc(sizeof *rtc_data, GFP_KERNEL);
192 if (!rtc_data)
193 return -ENOMEM;
194
195 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
196 if (!r) {
197 dev_err(&pdev->dev, "failed to get resource\n");
198 err = -ENXIO;
199 goto out_free;
200 }
201
202 rtc_data->io = ioremap(r->start, resource_size(r));
203 if (!rtc_data->io) {
204 dev_err(&pdev->dev, "ioremap failed\n");
205 err = -EIO;
206 goto out_free;
207 }
208
209 rtc_data->irq_alarm = platform_get_irq(pdev, 0);
210 rtc_data->irq_1msec = platform_get_irq(pdev, 1);
211
Wolfram Sang47eac332011-05-25 12:56:49 +0200212 if (!(__raw_readl(STMP3XXX_RTC_STAT + rtc_data->io) &
213 STMP3XXX_RTC_STAT_RTC_PRESENT)) {
dmitry pervushindf17f632009-09-22 16:46:26 -0700214 dev_err(&pdev->dev, "no device onboard\n");
215 err = -ENODEV;
216 goto out_remap;
217 }
218
Wolfram Sang46b21212011-05-25 12:56:50 +0200219 mxs_reset_block(rtc_data->io);
220 __mxs_clrl(STMP3XXX_RTC_PERSISTENT0_ALARM_EN |
Wolfram Sang47eac332011-05-25 12:56:49 +0200221 STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN |
222 STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE,
223 rtc_data->io + STMP3XXX_RTC_PERSISTENT0);
dmitry pervushindf17f632009-09-22 16:46:26 -0700224 rtc_data->rtc = rtc_device_register(pdev->name, &pdev->dev,
225 &stmp3xxx_rtc_ops, THIS_MODULE);
226 if (IS_ERR(rtc_data->rtc)) {
227 err = PTR_ERR(rtc_data->rtc);
228 goto out_remap;
229 }
230
231 rtc_data->irq_count = 0;
232 err = request_irq(rtc_data->irq_alarm, stmp3xxx_rtc_interrupt,
233 IRQF_DISABLED, "RTC alarm", &pdev->dev);
234 if (err) {
235 dev_err(&pdev->dev, "Cannot claim IRQ%d\n",
236 rtc_data->irq_alarm);
237 goto out_irq_alarm;
238 }
239 err = request_irq(rtc_data->irq_1msec, stmp3xxx_rtc_interrupt,
240 IRQF_DISABLED, "RTC tick", &pdev->dev);
241 if (err) {
242 dev_err(&pdev->dev, "Cannot claim IRQ%d\n",
243 rtc_data->irq_1msec);
244 goto out_irq1;
245 }
246
247 platform_set_drvdata(pdev, rtc_data);
248
249 return 0;
250
251out_irq1:
252 free_irq(rtc_data->irq_alarm, &pdev->dev);
253out_irq_alarm:
Wolfram Sang46b21212011-05-25 12:56:50 +0200254 __mxs_clrl(STMP3XXX_RTC_CTRL_ONEMSEC_IRQ_EN |
Wolfram Sang47eac332011-05-25 12:56:49 +0200255 STMP3XXX_RTC_CTRL_ALARM_IRQ_EN,
256 rtc_data->io + STMP3XXX_RTC_CTRL);
dmitry pervushindf17f632009-09-22 16:46:26 -0700257 rtc_device_unregister(rtc_data->rtc);
258out_remap:
259 iounmap(rtc_data->io);
260out_free:
261 kfree(rtc_data);
262 return err;
263}
264
265#ifdef CONFIG_PM
266static int stmp3xxx_rtc_suspend(struct platform_device *dev, pm_message_t state)
267{
268 return 0;
269}
270
271static int stmp3xxx_rtc_resume(struct platform_device *dev)
272{
273 struct stmp3xxx_rtc_data *rtc_data = platform_get_drvdata(dev);
274
Wolfram Sang46b21212011-05-25 12:56:50 +0200275 mxs_reset_block(rtc_data->io);
276 __mxs_clrl(STMP3XXX_RTC_PERSISTENT0_ALARM_EN |
Wolfram Sang47eac332011-05-25 12:56:49 +0200277 STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN |
278 STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE,
279 rtc_data->io + STMP3XXX_RTC_PERSISTENT0);
dmitry pervushindf17f632009-09-22 16:46:26 -0700280 return 0;
281}
282#else
283#define stmp3xxx_rtc_suspend NULL
284#define stmp3xxx_rtc_resume NULL
285#endif
286
287static struct platform_driver stmp3xxx_rtcdrv = {
288 .probe = stmp3xxx_rtc_probe,
289 .remove = stmp3xxx_rtc_remove,
290 .suspend = stmp3xxx_rtc_suspend,
291 .resume = stmp3xxx_rtc_resume,
292 .driver = {
293 .name = "stmp3xxx-rtc",
294 .owner = THIS_MODULE,
295 },
296};
297
298static int __init stmp3xxx_rtc_init(void)
299{
300 return platform_driver_register(&stmp3xxx_rtcdrv);
301}
302
303static void __exit stmp3xxx_rtc_exit(void)
304{
305 platform_driver_unregister(&stmp3xxx_rtcdrv);
306}
307
308module_init(stmp3xxx_rtc_init);
309module_exit(stmp3xxx_rtc_exit);
310
311MODULE_DESCRIPTION("STMP3xxx RTC Driver");
312MODULE_AUTHOR("dmitry pervushin <dpervushin@embeddedalley.com>");
313MODULE_LICENSE("GPL");