2 * Freescale STMP37XX/STMP378X Real Time Clock driver
4 * Copyright (c) 2007 Sigmatel, Inc.
5 * Peter Hartley, <peter.hartley@sigmatel.com>
7 * Copyright 2008 Freescale Semiconductor, Inc. All Rights Reserved.
8 * Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
9 * Copyright 2011 Wolfram Sang, Pengutronix e.K.
13 * The code contained herein is licensed under the GNU General Public
14 * License. You may obtain a copy of the GNU General Public License
15 * Version 2 or later at the following locations:
17 * http://www.opensource.org/licenses/gpl-license.html
18 * http://www.gnu.org/copyleft/gpl.html
20 #include <linux/kernel.h>
21 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/platform_device.h>
25 #include <linux/interrupt.h>
26 #include <linux/rtc.h>
27 #include <linux/slab.h>
29 #include <mach/common.h>
31 #define STMP3XXX_RTC_CTRL 0x0
32 #define STMP3XXX_RTC_CTRL_SET 0x4
33 #define STMP3XXX_RTC_CTRL_CLR 0x8
34 #define STMP3XXX_RTC_CTRL_ALARM_IRQ_EN 0x00000001
35 #define STMP3XXX_RTC_CTRL_ONEMSEC_IRQ_EN 0x00000002
36 #define STMP3XXX_RTC_CTRL_ALARM_IRQ 0x00000004
38 #define STMP3XXX_RTC_STAT 0x10
39 #define STMP3XXX_RTC_STAT_STALE_SHIFT 16
40 #define STMP3XXX_RTC_STAT_RTC_PRESENT 0x80000000
42 #define STMP3XXX_RTC_SECONDS 0x30
44 #define STMP3XXX_RTC_ALARM 0x40
46 #define STMP3XXX_RTC_PERSISTENT0 0x60
47 #define STMP3XXX_RTC_PERSISTENT0_SET 0x64
48 #define STMP3XXX_RTC_PERSISTENT0_CLR 0x68
49 #define STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN 0x00000002
50 #define STMP3XXX_RTC_PERSISTENT0_ALARM_EN 0x00000004
51 #define STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE 0x00000080
53 struct stmp3xxx_rtc_data
{
54 struct rtc_device
*rtc
;
59 static void stmp3xxx_wait_time(struct stmp3xxx_rtc_data
*rtc_data
)
62 * The datasheet doesn't say which way round the
63 * NEW_REGS/STALE_REGS bitfields go. In fact it's 0x1=P0,
64 * 0x2=P1, .., 0x20=P5, 0x40=ALARM, 0x80=SECONDS
66 while (readl(rtc_data
->io
+ STMP3XXX_RTC_STAT
) &
67 (0x80 << STMP3XXX_RTC_STAT_STALE_SHIFT
))
72 static int stmp3xxx_rtc_gettime(struct device
*dev
, struct rtc_time
*rtc_tm
)
74 struct stmp3xxx_rtc_data
*rtc_data
= dev_get_drvdata(dev
);
76 stmp3xxx_wait_time(rtc_data
);
77 rtc_time_to_tm(readl(rtc_data
->io
+ STMP3XXX_RTC_SECONDS
), rtc_tm
);
81 static int stmp3xxx_rtc_set_mmss(struct device
*dev
, unsigned long t
)
83 struct stmp3xxx_rtc_data
*rtc_data
= dev_get_drvdata(dev
);
85 writel(t
, rtc_data
->io
+ STMP3XXX_RTC_SECONDS
);
86 stmp3xxx_wait_time(rtc_data
);
90 /* interrupt(s) handler */
91 static irqreturn_t
stmp3xxx_rtc_interrupt(int irq
, void *dev_id
)
93 struct stmp3xxx_rtc_data
*rtc_data
= dev_get_drvdata(dev_id
);
94 u32 status
= readl(rtc_data
->io
+ STMP3XXX_RTC_CTRL
);
96 if (status
& STMP3XXX_RTC_CTRL_ALARM_IRQ
) {
97 writel(STMP3XXX_RTC_CTRL_ALARM_IRQ
,
98 rtc_data
->io
+ STMP3XXX_RTC_CTRL_CLR
);
99 rtc_update_irq(rtc_data
->rtc
, 1, RTC_AF
| RTC_IRQF
);
106 static int stmp3xxx_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
108 struct stmp3xxx_rtc_data
*rtc_data
= dev_get_drvdata(dev
);
111 writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN
|
112 STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN
,
113 rtc_data
->io
+ STMP3XXX_RTC_PERSISTENT0_SET
);
114 writel(STMP3XXX_RTC_CTRL_ALARM_IRQ_EN
,
115 rtc_data
->io
+ STMP3XXX_RTC_CTRL_SET
);
117 writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN
|
118 STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN
,
119 rtc_data
->io
+ STMP3XXX_RTC_PERSISTENT0_CLR
);
120 writel(STMP3XXX_RTC_CTRL_ALARM_IRQ_EN
,
121 rtc_data
->io
+ STMP3XXX_RTC_CTRL_CLR
);
126 static int stmp3xxx_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
128 struct stmp3xxx_rtc_data
*rtc_data
= dev_get_drvdata(dev
);
130 rtc_time_to_tm(readl(rtc_data
->io
+ STMP3XXX_RTC_ALARM
), &alm
->time
);
134 static int stmp3xxx_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
137 struct stmp3xxx_rtc_data
*rtc_data
= dev_get_drvdata(dev
);
139 rtc_tm_to_time(&alm
->time
, &t
);
140 writel(t
, rtc_data
->io
+ STMP3XXX_RTC_ALARM
);
142 stmp3xxx_alarm_irq_enable(dev
, alm
->enabled
);
147 static struct rtc_class_ops stmp3xxx_rtc_ops
= {
149 stmp3xxx_alarm_irq_enable
,
150 .read_time
= stmp3xxx_rtc_gettime
,
151 .set_mmss
= stmp3xxx_rtc_set_mmss
,
152 .read_alarm
= stmp3xxx_rtc_read_alarm
,
153 .set_alarm
= stmp3xxx_rtc_set_alarm
,
156 static int stmp3xxx_rtc_remove(struct platform_device
*pdev
)
158 struct stmp3xxx_rtc_data
*rtc_data
= platform_get_drvdata(pdev
);
163 writel(STMP3XXX_RTC_CTRL_ALARM_IRQ_EN
,
164 rtc_data
->io
+ STMP3XXX_RTC_CTRL_CLR
);
165 free_irq(rtc_data
->irq_alarm
, &pdev
->dev
);
166 rtc_device_unregister(rtc_data
->rtc
);
167 platform_set_drvdata(pdev
, NULL
);
168 iounmap(rtc_data
->io
);
174 static int stmp3xxx_rtc_probe(struct platform_device
*pdev
)
176 struct stmp3xxx_rtc_data
*rtc_data
;
180 rtc_data
= kzalloc(sizeof *rtc_data
, GFP_KERNEL
);
184 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
186 dev_err(&pdev
->dev
, "failed to get resource\n");
191 rtc_data
->io
= ioremap(r
->start
, resource_size(r
));
193 dev_err(&pdev
->dev
, "ioremap failed\n");
198 rtc_data
->irq_alarm
= platform_get_irq(pdev
, 0);
200 if (!(readl(STMP3XXX_RTC_STAT
+ rtc_data
->io
) &
201 STMP3XXX_RTC_STAT_RTC_PRESENT
)) {
202 dev_err(&pdev
->dev
, "no device onboard\n");
207 platform_set_drvdata(pdev
, rtc_data
);
209 mxs_reset_block(rtc_data
->io
);
210 writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN
|
211 STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN
|
212 STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE
,
213 rtc_data
->io
+ STMP3XXX_RTC_PERSISTENT0_CLR
);
215 writel(STMP3XXX_RTC_CTRL_ONEMSEC_IRQ_EN
|
216 STMP3XXX_RTC_CTRL_ALARM_IRQ_EN
,
217 rtc_data
->io
+ STMP3XXX_RTC_CTRL_CLR
);
219 rtc_data
->rtc
= rtc_device_register(pdev
->name
, &pdev
->dev
,
220 &stmp3xxx_rtc_ops
, THIS_MODULE
);
221 if (IS_ERR(rtc_data
->rtc
)) {
222 err
= PTR_ERR(rtc_data
->rtc
);
226 err
= request_irq(rtc_data
->irq_alarm
, stmp3xxx_rtc_interrupt
, 0,
227 "RTC alarm", &pdev
->dev
);
229 dev_err(&pdev
->dev
, "Cannot claim IRQ%d\n",
230 rtc_data
->irq_alarm
);
237 rtc_device_unregister(rtc_data
->rtc
);
239 platform_set_drvdata(pdev
, NULL
);
240 iounmap(rtc_data
->io
);
247 static int stmp3xxx_rtc_suspend(struct platform_device
*dev
, pm_message_t state
)
252 static int stmp3xxx_rtc_resume(struct platform_device
*dev
)
254 struct stmp3xxx_rtc_data
*rtc_data
= platform_get_drvdata(dev
);
256 mxs_reset_block(rtc_data
->io
);
257 writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN
|
258 STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN
|
259 STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE
,
260 rtc_data
->io
+ STMP3XXX_RTC_PERSISTENT0_CLR
);
264 #define stmp3xxx_rtc_suspend NULL
265 #define stmp3xxx_rtc_resume NULL
268 static struct platform_driver stmp3xxx_rtcdrv
= {
269 .probe
= stmp3xxx_rtc_probe
,
270 .remove
= stmp3xxx_rtc_remove
,
271 .suspend
= stmp3xxx_rtc_suspend
,
272 .resume
= stmp3xxx_rtc_resume
,
274 .name
= "stmp3xxx-rtc",
275 .owner
= THIS_MODULE
,
279 module_platform_driver(stmp3xxx_rtcdrv
);
281 MODULE_DESCRIPTION("STMP3xxx RTC Driver");
282 MODULE_AUTHOR("dmitry pervushin <dpervushin@embeddedalley.com> and "
283 "Wolfram Sang <w.sang@pengutronix.de>");
284 MODULE_LICENSE("GPL");