2 * Real Time Clock driver for Freescale MC13XXX PMIC
4 * (C) 2009 Sascha Hauer, Pengutronix
5 * (C) 2009 Uwe Kleine-Koenig, Pengutronix
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/mfd/mc13xxx.h>
13 #include <linux/platform_device.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/mod_devicetable.h>
17 #include <linux/slab.h>
18 #include <linux/rtc.h>
20 #define DRIVER_NAME "mc13xxx-rtc"
22 #define MC13XXX_RTCTOD 20
23 #define MC13XXX_RTCTODA 21
24 #define MC13XXX_RTCDAY 22
25 #define MC13XXX_RTCDAYA 23
27 #define SEC_PER_DAY (24 * 60 * 60)
30 struct rtc_device
*rtc
;
31 struct mc13xxx
*mc13xxx
;
35 static int mc13xxx_rtc_irq_enable_unlocked(struct device
*dev
,
36 unsigned int enabled
, int irq
)
38 struct mc13xxx_rtc
*priv
= dev_get_drvdata(dev
);
39 int (*func
)(struct mc13xxx
*mc13xxx
, int irq
);
44 func
= enabled
? mc13xxx_irq_unmask
: mc13xxx_irq_mask
;
45 return func(priv
->mc13xxx
, irq
);
48 static int mc13xxx_rtc_alarm_irq_enable(struct device
*dev
,
51 struct mc13xxx_rtc
*priv
= dev_get_drvdata(dev
);
54 mc13xxx_lock(priv
->mc13xxx
);
56 ret
= mc13xxx_rtc_irq_enable_unlocked(dev
, enabled
, MC13XXX_IRQ_TODA
);
58 mc13xxx_unlock(priv
->mc13xxx
);
63 static int mc13xxx_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
65 struct mc13xxx_rtc
*priv
= dev_get_drvdata(dev
);
66 unsigned int seconds
, days1
, days2
;
74 ret
= mc13xxx_reg_read(priv
->mc13xxx
, MC13XXX_RTCDAY
, &days1
);
78 ret
= mc13xxx_reg_read(priv
->mc13xxx
, MC13XXX_RTCTOD
, &seconds
);
82 ret
= mc13xxx_reg_read(priv
->mc13xxx
, MC13XXX_RTCDAY
, &days2
);
85 } while (days1
!= days2
);
87 rtc_time64_to_tm((time64_t
)days1
* SEC_PER_DAY
+ seconds
, tm
);
92 static int mc13xxx_rtc_set_mmss(struct device
*dev
, time64_t secs
)
94 struct mc13xxx_rtc
*priv
= dev_get_drvdata(dev
);
95 unsigned int seconds
, days
;
96 unsigned int alarmseconds
;
99 days
= div_s64_rem(secs
, SEC_PER_DAY
, &seconds
);
101 mc13xxx_lock(priv
->mc13xxx
);
104 * temporarily invalidate alarm to prevent triggering it when the day is
105 * already updated while the time isn't yet.
107 ret
= mc13xxx_reg_read(priv
->mc13xxx
, MC13XXX_RTCTODA
, &alarmseconds
);
111 if (alarmseconds
< SEC_PER_DAY
) {
112 ret
= mc13xxx_reg_write(priv
->mc13xxx
,
113 MC13XXX_RTCTODA
, 0x1ffff);
119 * write seconds=0 to prevent a day switch between writing days
122 ret
= mc13xxx_reg_write(priv
->mc13xxx
, MC13XXX_RTCTOD
, 0);
126 ret
= mc13xxx_reg_write(priv
->mc13xxx
, MC13XXX_RTCDAY
, days
);
130 ret
= mc13xxx_reg_write(priv
->mc13xxx
, MC13XXX_RTCTOD
, seconds
);
135 if (alarmseconds
< SEC_PER_DAY
) {
136 ret
= mc13xxx_reg_write(priv
->mc13xxx
,
137 MC13XXX_RTCTODA
, alarmseconds
);
143 ret
= mc13xxx_irq_ack(priv
->mc13xxx
, MC13XXX_IRQ_RTCRST
);
147 ret
= mc13xxx_irq_unmask(priv
->mc13xxx
, MC13XXX_IRQ_RTCRST
);
153 mc13xxx_unlock(priv
->mc13xxx
);
158 static int mc13xxx_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
160 struct mc13xxx_rtc
*priv
= dev_get_drvdata(dev
);
161 unsigned seconds
, days
;
163 int enabled
, pending
;
166 mc13xxx_lock(priv
->mc13xxx
);
168 ret
= mc13xxx_reg_read(priv
->mc13xxx
, MC13XXX_RTCTODA
, &seconds
);
171 if (seconds
>= SEC_PER_DAY
) {
176 ret
= mc13xxx_reg_read(priv
->mc13xxx
, MC13XXX_RTCDAY
, &days
);
180 ret
= mc13xxx_irq_status(priv
->mc13xxx
, MC13XXX_IRQ_TODA
,
184 mc13xxx_unlock(priv
->mc13xxx
);
189 alarm
->enabled
= enabled
;
190 alarm
->pending
= pending
;
192 s1970
= (time64_t
)days
* SEC_PER_DAY
+ seconds
;
194 rtc_time64_to_tm(s1970
, &alarm
->time
);
195 dev_dbg(dev
, "%s: %lld\n", __func__
, (long long)s1970
);
200 static int mc13xxx_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
202 struct mc13xxx_rtc
*priv
= dev_get_drvdata(dev
);
207 mc13xxx_lock(priv
->mc13xxx
);
209 /* disable alarm to prevent false triggering */
210 ret
= mc13xxx_reg_write(priv
->mc13xxx
, MC13XXX_RTCTODA
, 0x1ffff);
214 ret
= mc13xxx_irq_ack(priv
->mc13xxx
, MC13XXX_IRQ_TODA
);
218 s1970
= rtc_tm_to_time64(&alarm
->time
);
220 dev_dbg(dev
, "%s: %s %lld\n", __func__
, alarm
->enabled
? "on" : "off",
223 ret
= mc13xxx_rtc_irq_enable_unlocked(dev
, alarm
->enabled
,
228 days
= div_s64_rem(s1970
, SEC_PER_DAY
, &seconds
);
230 ret
= mc13xxx_reg_write(priv
->mc13xxx
, MC13XXX_RTCDAYA
, days
);
234 ret
= mc13xxx_reg_write(priv
->mc13xxx
, MC13XXX_RTCTODA
, seconds
);
237 mc13xxx_unlock(priv
->mc13xxx
);
242 static irqreturn_t
mc13xxx_rtc_alarm_handler(int irq
, void *dev
)
244 struct mc13xxx_rtc
*priv
= dev
;
245 struct mc13xxx
*mc13xxx
= priv
->mc13xxx
;
247 rtc_update_irq(priv
->rtc
, 1, RTC_IRQF
| RTC_AF
);
249 mc13xxx_irq_ack(mc13xxx
, irq
);
254 static const struct rtc_class_ops mc13xxx_rtc_ops
= {
255 .read_time
= mc13xxx_rtc_read_time
,
256 .set_mmss64
= mc13xxx_rtc_set_mmss
,
257 .read_alarm
= mc13xxx_rtc_read_alarm
,
258 .set_alarm
= mc13xxx_rtc_set_alarm
,
259 .alarm_irq_enable
= mc13xxx_rtc_alarm_irq_enable
,
262 static irqreturn_t
mc13xxx_rtc_reset_handler(int irq
, void *dev
)
264 struct mc13xxx_rtc
*priv
= dev
;
265 struct mc13xxx
*mc13xxx
= priv
->mc13xxx
;
269 mc13xxx_irq_mask(mc13xxx
, irq
);
274 static int __init
mc13xxx_rtc_probe(struct platform_device
*pdev
)
277 struct mc13xxx_rtc
*priv
;
278 struct mc13xxx
*mc13xxx
;
280 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
284 mc13xxx
= dev_get_drvdata(pdev
->dev
.parent
);
285 priv
->mc13xxx
= mc13xxx
;
288 platform_set_drvdata(pdev
, priv
);
290 mc13xxx_lock(mc13xxx
);
292 mc13xxx_irq_ack(mc13xxx
, MC13XXX_IRQ_RTCRST
);
294 ret
= mc13xxx_irq_request(mc13xxx
, MC13XXX_IRQ_RTCRST
,
295 mc13xxx_rtc_reset_handler
, DRIVER_NAME
, priv
);
297 goto err_irq_request
;
299 ret
= mc13xxx_irq_request_nounmask(mc13xxx
, MC13XXX_IRQ_TODA
,
300 mc13xxx_rtc_alarm_handler
, DRIVER_NAME
, priv
);
302 goto err_irq_request
;
304 mc13xxx_unlock(mc13xxx
);
306 priv
->rtc
= devm_rtc_device_register(&pdev
->dev
, pdev
->name
,
307 &mc13xxx_rtc_ops
, THIS_MODULE
);
312 mc13xxx_irq_free(mc13xxx
, MC13XXX_IRQ_TODA
, priv
);
313 mc13xxx_irq_free(mc13xxx
, MC13XXX_IRQ_RTCRST
, priv
);
315 mc13xxx_unlock(mc13xxx
);
320 static int mc13xxx_rtc_remove(struct platform_device
*pdev
)
322 struct mc13xxx_rtc
*priv
= platform_get_drvdata(pdev
);
324 mc13xxx_lock(priv
->mc13xxx
);
326 mc13xxx_irq_free(priv
->mc13xxx
, MC13XXX_IRQ_TODA
, priv
);
327 mc13xxx_irq_free(priv
->mc13xxx
, MC13XXX_IRQ_RTCRST
, priv
);
329 mc13xxx_unlock(priv
->mc13xxx
);
334 static const struct platform_device_id mc13xxx_rtc_idtable
[] = {
336 .name
= "mc13783-rtc",
338 .name
= "mc13892-rtc",
340 .name
= "mc34708-rtc",
344 MODULE_DEVICE_TABLE(platform
, mc13xxx_rtc_idtable
);
346 static struct platform_driver mc13xxx_rtc_driver
= {
347 .id_table
= mc13xxx_rtc_idtable
,
348 .remove
= mc13xxx_rtc_remove
,
354 module_platform_driver_probe(mc13xxx_rtc_driver
, &mc13xxx_rtc_probe
);
356 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
357 MODULE_DESCRIPTION("RTC driver for Freescale MC13XXX PMIC");
358 MODULE_LICENSE("GPL v2");