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.
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:
16 * http://www.opensource.org/licenses/gpl-license.html
17 * http://www.gnu.org/copyleft/gpl.html
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>
26 #include <mach/platform.h>
27 #include <mach/stmp3xxx.h>
28 #include <mach/regs-rtc.h>
30 struct stmp3xxx_rtc_data
{
31 struct rtc_device
*rtc
;
34 int irq_alarm
, irq_1msec
;
37 static void stmp3xxx_wait_time(struct stmp3xxx_rtc_data
*rtc_data
)
40 * The datasheet doesn't say which way round the
41 * NEW_REGS/STALE_REGS bitfields go. In fact it's 0x1=P0,
42 * 0x2=P1, .., 0x20=P5, 0x40=ALARM, 0x80=SECONDS
44 while (__raw_readl(rtc_data
->io
+ HW_RTC_STAT
) &
45 BF(0x80, RTC_STAT_STALE_REGS
))
50 static int stmp3xxx_rtc_gettime(struct device
*dev
, struct rtc_time
*rtc_tm
)
52 struct stmp3xxx_rtc_data
*rtc_data
= dev_get_drvdata(dev
);
54 stmp3xxx_wait_time(rtc_data
);
55 rtc_time_to_tm(__raw_readl(rtc_data
->io
+ HW_RTC_SECONDS
), rtc_tm
);
59 static int stmp3xxx_rtc_set_mmss(struct device
*dev
, unsigned long t
)
61 struct stmp3xxx_rtc_data
*rtc_data
= dev_get_drvdata(dev
);
63 __raw_writel(t
, rtc_data
->io
+ HW_RTC_SECONDS
);
64 stmp3xxx_wait_time(rtc_data
);
68 /* interrupt(s) handler */
69 static irqreturn_t
stmp3xxx_rtc_interrupt(int irq
, void *dev_id
)
71 struct stmp3xxx_rtc_data
*rtc_data
= dev_get_drvdata(dev_id
);
75 status
= __raw_readl(rtc_data
->io
+ HW_RTC_CTRL
) &
76 (BM_RTC_CTRL_ALARM_IRQ
| BM_RTC_CTRL_ONEMSEC_IRQ
);
78 if (status
& BM_RTC_CTRL_ALARM_IRQ
) {
79 stmp3xxx_clearl(BM_RTC_CTRL_ALARM_IRQ
,
80 rtc_data
->io
+ HW_RTC_CTRL
);
81 events
|= RTC_AF
| RTC_IRQF
;
84 if (status
& BM_RTC_CTRL_ONEMSEC_IRQ
) {
85 stmp3xxx_clearl(BM_RTC_CTRL_ONEMSEC_IRQ
,
86 rtc_data
->io
+ HW_RTC_CTRL
);
87 if (++rtc_data
->irq_count
% 1000 == 0) {
88 events
|= RTC_UF
| RTC_IRQF
;
89 rtc_data
->irq_count
= 0;
94 rtc_update_irq(rtc_data
->rtc
, 1, events
);
99 static int stmp3xxx_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
101 struct stmp3xxx_rtc_data
*rtc_data
= dev_get_drvdata(dev
);
102 void __iomem
*p
= rtc_data
->io
+ HW_RTC_PERSISTENT0
,
103 *ctl
= rtc_data
->io
+ HW_RTC_CTRL
;
106 stmp3xxx_setl(BM_RTC_PERSISTENT0_ALARM_EN
|
107 BM_RTC_PERSISTENT0_ALARM_WAKE_EN
, p
);
108 stmp3xxx_setl(BM_RTC_CTRL_ALARM_IRQ_EN
, ctl
);
110 stmp3xxx_clearl(BM_RTC_PERSISTENT0_ALARM_EN
|
111 BM_RTC_PERSISTENT0_ALARM_WAKE_EN
, p
);
112 stmp3xxx_clearl(BM_RTC_CTRL_ALARM_IRQ_EN
, ctl
);
117 static int stmp3xxx_update_irq_enable(struct device
*dev
, unsigned int enabled
)
119 struct stmp3xxx_rtc_data
*rtc_data
= dev_get_drvdata(dev
);
122 stmp3xxx_setl(BM_RTC_CTRL_ONEMSEC_IRQ_EN
,
123 rtc_data
->io
+ HW_RTC_CTRL
);
125 stmp3xxx_clearl(BM_RTC_CTRL_ONEMSEC_IRQ_EN
,
126 rtc_data
->io
+ HW_RTC_CTRL
);
130 static int stmp3xxx_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
132 struct stmp3xxx_rtc_data
*rtc_data
= dev_get_drvdata(dev
);
134 rtc_time_to_tm(__raw_readl(rtc_data
->io
+ HW_RTC_ALARM
), &alm
->time
);
138 static int stmp3xxx_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
141 struct stmp3xxx_rtc_data
*rtc_data
= dev_get_drvdata(dev
);
143 rtc_tm_to_time(&alm
->time
, &t
);
144 __raw_writel(t
, rtc_data
->io
+ HW_RTC_ALARM
);
148 static struct rtc_class_ops stmp3xxx_rtc_ops
= {
150 stmp3xxx_alarm_irq_enable
,
152 stmp3xxx_update_irq_enable
,
153 .read_time
= stmp3xxx_rtc_gettime
,
154 .set_mmss
= stmp3xxx_rtc_set_mmss
,
155 .read_alarm
= stmp3xxx_rtc_read_alarm
,
156 .set_alarm
= stmp3xxx_rtc_set_alarm
,
159 static int stmp3xxx_rtc_remove(struct platform_device
*pdev
)
161 struct stmp3xxx_rtc_data
*rtc_data
= platform_get_drvdata(pdev
);
166 stmp3xxx_clearl(BM_RTC_CTRL_ONEMSEC_IRQ_EN
| BM_RTC_CTRL_ALARM_IRQ_EN
,
167 rtc_data
->io
+ HW_RTC_CTRL
);
168 free_irq(rtc_data
->irq_alarm
, &pdev
->dev
);
169 free_irq(rtc_data
->irq_1msec
, &pdev
->dev
);
170 rtc_device_unregister(rtc_data
->rtc
);
171 iounmap(rtc_data
->io
);
177 static int stmp3xxx_rtc_probe(struct platform_device
*pdev
)
179 struct stmp3xxx_rtc_data
*rtc_data
;
183 rtc_data
= kzalloc(sizeof *rtc_data
, GFP_KERNEL
);
187 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
189 dev_err(&pdev
->dev
, "failed to get resource\n");
194 rtc_data
->io
= ioremap(r
->start
, resource_size(r
));
196 dev_err(&pdev
->dev
, "ioremap failed\n");
201 rtc_data
->irq_alarm
= platform_get_irq(pdev
, 0);
202 rtc_data
->irq_1msec
= platform_get_irq(pdev
, 1);
204 if (!(__raw_readl(HW_RTC_STAT
+ rtc_data
->io
) &
205 BM_RTC_STAT_RTC_PRESENT
)) {
206 dev_err(&pdev
->dev
, "no device onboard\n");
211 stmp3xxx_reset_block(rtc_data
->io
, true);
212 stmp3xxx_clearl(BM_RTC_PERSISTENT0_ALARM_EN
|
213 BM_RTC_PERSISTENT0_ALARM_WAKE_EN
|
214 BM_RTC_PERSISTENT0_ALARM_WAKE
,
215 rtc_data
->io
+ HW_RTC_PERSISTENT0
);
216 rtc_data
->rtc
= rtc_device_register(pdev
->name
, &pdev
->dev
,
217 &stmp3xxx_rtc_ops
, THIS_MODULE
);
218 if (IS_ERR(rtc_data
->rtc
)) {
219 err
= PTR_ERR(rtc_data
->rtc
);
223 rtc_data
->irq_count
= 0;
224 err
= request_irq(rtc_data
->irq_alarm
, stmp3xxx_rtc_interrupt
,
225 IRQF_DISABLED
, "RTC alarm", &pdev
->dev
);
227 dev_err(&pdev
->dev
, "Cannot claim IRQ%d\n",
228 rtc_data
->irq_alarm
);
231 err
= request_irq(rtc_data
->irq_1msec
, stmp3xxx_rtc_interrupt
,
232 IRQF_DISABLED
, "RTC tick", &pdev
->dev
);
234 dev_err(&pdev
->dev
, "Cannot claim IRQ%d\n",
235 rtc_data
->irq_1msec
);
239 platform_set_drvdata(pdev
, rtc_data
);
244 free_irq(rtc_data
->irq_alarm
, &pdev
->dev
);
246 stmp3xxx_clearl(BM_RTC_CTRL_ONEMSEC_IRQ_EN
| BM_RTC_CTRL_ALARM_IRQ_EN
,
247 rtc_data
->io
+ HW_RTC_CTRL
);
248 rtc_device_unregister(rtc_data
->rtc
);
250 iounmap(rtc_data
->io
);
257 static int stmp3xxx_rtc_suspend(struct platform_device
*dev
, pm_message_t state
)
262 static int stmp3xxx_rtc_resume(struct platform_device
*dev
)
264 struct stmp3xxx_rtc_data
*rtc_data
= platform_get_drvdata(dev
);
266 stmp3xxx_reset_block(rtc_data
->io
, true);
267 stmp3xxx_clearl(BM_RTC_PERSISTENT0_ALARM_EN
|
268 BM_RTC_PERSISTENT0_ALARM_WAKE_EN
|
269 BM_RTC_PERSISTENT0_ALARM_WAKE
,
270 rtc_data
->io
+ HW_RTC_PERSISTENT0
);
274 #define stmp3xxx_rtc_suspend NULL
275 #define stmp3xxx_rtc_resume NULL
278 static struct platform_driver stmp3xxx_rtcdrv
= {
279 .probe
= stmp3xxx_rtc_probe
,
280 .remove
= stmp3xxx_rtc_remove
,
281 .suspend
= stmp3xxx_rtc_suspend
,
282 .resume
= stmp3xxx_rtc_resume
,
284 .name
= "stmp3xxx-rtc",
285 .owner
= THIS_MODULE
,
289 static int __init
stmp3xxx_rtc_init(void)
291 return platform_driver_register(&stmp3xxx_rtcdrv
);
294 static void __exit
stmp3xxx_rtc_exit(void)
296 platform_driver_unregister(&stmp3xxx_rtcdrv
);
299 module_init(stmp3xxx_rtc_init
);
300 module_exit(stmp3xxx_rtc_exit
);
302 MODULE_DESCRIPTION("STMP3xxx RTC Driver");
303 MODULE_AUTHOR("dmitry pervushin <dpervushin@embeddedalley.com>");
304 MODULE_LICENSE("GPL");