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/slab.h>
17 #include <linux/rtc.h>
19 #define DRIVER_NAME "mc13xxx-rtc"
21 #define MC13XXX_RTCTOD 20
22 #define MC13XXX_RTCTODA 21
23 #define MC13XXX_RTCDAY 22
24 #define MC13XXX_RTCDAYA 23
26 #define SEC_PER_DAY (24 * 60 * 60)
29 struct rtc_device
*rtc
;
30 struct mc13xxx
*mc13xxx
;
34 static int mc13xxx_rtc_irq_enable_unlocked(struct device
*dev
,
35 unsigned int enabled
, int irq
)
37 struct mc13xxx_rtc
*priv
= dev_get_drvdata(dev
);
38 int (*func
)(struct mc13xxx
*mc13xxx
, int irq
);
43 func
= enabled
? mc13xxx_irq_unmask
: mc13xxx_irq_mask
;
44 return func(priv
->mc13xxx
, irq
);
47 static int mc13xxx_rtc_alarm_irq_enable(struct device
*dev
,
50 struct mc13xxx_rtc
*priv
= dev_get_drvdata(dev
);
53 mc13xxx_lock(priv
->mc13xxx
);
55 ret
= mc13xxx_rtc_irq_enable_unlocked(dev
, enabled
, MC13XXX_IRQ_TODA
);
57 mc13xxx_unlock(priv
->mc13xxx
);
62 static int mc13xxx_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
64 struct mc13xxx_rtc
*priv
= dev_get_drvdata(dev
);
65 unsigned int seconds
, days1
, days2
;
73 ret
= mc13xxx_reg_read(priv
->mc13xxx
, MC13XXX_RTCDAY
, &days1
);
77 ret
= mc13xxx_reg_read(priv
->mc13xxx
, MC13XXX_RTCTOD
, &seconds
);
81 ret
= mc13xxx_reg_read(priv
->mc13xxx
, MC13XXX_RTCDAY
, &days2
);
84 } while (days1
!= days2
);
86 rtc_time64_to_tm((time64_t
)days1
* SEC_PER_DAY
+ seconds
, tm
);
91 static int mc13xxx_rtc_set_mmss(struct device
*dev
, time64_t secs
)
93 struct mc13xxx_rtc
*priv
= dev_get_drvdata(dev
);
94 unsigned int seconds
, days
;
95 unsigned int alarmseconds
;
98 days
= div_s64_rem(secs
, SEC_PER_DAY
, &seconds
);
100 mc13xxx_lock(priv
->mc13xxx
);
103 * temporarily invalidate alarm to prevent triggering it when the day is
104 * already updated while the time isn't yet.
106 ret
= mc13xxx_reg_read(priv
->mc13xxx
, MC13XXX_RTCTODA
, &alarmseconds
);
110 if (alarmseconds
< SEC_PER_DAY
) {
111 ret
= mc13xxx_reg_write(priv
->mc13xxx
,
112 MC13XXX_RTCTODA
, 0x1ffff);
118 * write seconds=0 to prevent a day switch between writing days
121 ret
= mc13xxx_reg_write(priv
->mc13xxx
, MC13XXX_RTCTOD
, 0);
125 ret
= mc13xxx_reg_write(priv
->mc13xxx
, MC13XXX_RTCDAY
, days
);
129 ret
= mc13xxx_reg_write(priv
->mc13xxx
, MC13XXX_RTCTOD
, seconds
);
134 if (alarmseconds
< SEC_PER_DAY
) {
135 ret
= mc13xxx_reg_write(priv
->mc13xxx
,
136 MC13XXX_RTCTODA
, alarmseconds
);
142 ret
= mc13xxx_irq_ack(priv
->mc13xxx
, MC13XXX_IRQ_RTCRST
);
146 ret
= mc13xxx_irq_unmask(priv
->mc13xxx
, MC13XXX_IRQ_RTCRST
);
152 mc13xxx_unlock(priv
->mc13xxx
);
157 static int mc13xxx_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
159 struct mc13xxx_rtc
*priv
= dev_get_drvdata(dev
);
160 unsigned seconds
, days
;
162 int enabled
, pending
;
165 mc13xxx_lock(priv
->mc13xxx
);
167 ret
= mc13xxx_reg_read(priv
->mc13xxx
, MC13XXX_RTCTODA
, &seconds
);
170 if (seconds
>= SEC_PER_DAY
) {
175 ret
= mc13xxx_reg_read(priv
->mc13xxx
, MC13XXX_RTCDAY
, &days
);
179 ret
= mc13xxx_irq_status(priv
->mc13xxx
, MC13XXX_IRQ_TODA
,
183 mc13xxx_unlock(priv
->mc13xxx
);
188 alarm
->enabled
= enabled
;
189 alarm
->pending
= pending
;
191 s1970
= (time64_t
)days
* SEC_PER_DAY
+ seconds
;
193 rtc_time64_to_tm(s1970
, &alarm
->time
);
194 dev_dbg(dev
, "%s: %lld\n", __func__
, (long long)s1970
);
199 static int mc13xxx_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
201 struct mc13xxx_rtc
*priv
= dev_get_drvdata(dev
);
206 mc13xxx_lock(priv
->mc13xxx
);
208 /* disable alarm to prevent false triggering */
209 ret
= mc13xxx_reg_write(priv
->mc13xxx
, MC13XXX_RTCTODA
, 0x1ffff);
213 ret
= mc13xxx_irq_ack(priv
->mc13xxx
, MC13XXX_IRQ_TODA
);
217 s1970
= rtc_tm_to_time64(&alarm
->time
);
219 dev_dbg(dev
, "%s: %s %lld\n", __func__
, alarm
->enabled
? "on" : "off",
222 ret
= mc13xxx_rtc_irq_enable_unlocked(dev
, alarm
->enabled
,
227 days
= div_s64_rem(s1970
, SEC_PER_DAY
, &seconds
);
229 ret
= mc13xxx_reg_write(priv
->mc13xxx
, MC13XXX_RTCDAYA
, days
);
233 ret
= mc13xxx_reg_write(priv
->mc13xxx
, MC13XXX_RTCTODA
, seconds
);
236 mc13xxx_unlock(priv
->mc13xxx
);
241 static irqreturn_t
mc13xxx_rtc_alarm_handler(int irq
, void *dev
)
243 struct mc13xxx_rtc
*priv
= dev
;
244 struct mc13xxx
*mc13xxx
= priv
->mc13xxx
;
246 rtc_update_irq(priv
->rtc
, 1, RTC_IRQF
| RTC_AF
);
248 mc13xxx_irq_ack(mc13xxx
, irq
);
253 static const struct rtc_class_ops mc13xxx_rtc_ops
= {
254 .read_time
= mc13xxx_rtc_read_time
,
255 .set_mmss64
= mc13xxx_rtc_set_mmss
,
256 .read_alarm
= mc13xxx_rtc_read_alarm
,
257 .set_alarm
= mc13xxx_rtc_set_alarm
,
258 .alarm_irq_enable
= mc13xxx_rtc_alarm_irq_enable
,
261 static irqreturn_t
mc13xxx_rtc_reset_handler(int irq
, void *dev
)
263 struct mc13xxx_rtc
*priv
= dev
;
264 struct mc13xxx
*mc13xxx
= priv
->mc13xxx
;
268 mc13xxx_irq_mask(mc13xxx
, irq
);
273 static int __init
mc13xxx_rtc_probe(struct platform_device
*pdev
)
276 struct mc13xxx_rtc
*priv
;
277 struct mc13xxx
*mc13xxx
;
279 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
283 mc13xxx
= dev_get_drvdata(pdev
->dev
.parent
);
284 priv
->mc13xxx
= mc13xxx
;
287 platform_set_drvdata(pdev
, priv
);
289 mc13xxx_lock(mc13xxx
);
291 mc13xxx_irq_ack(mc13xxx
, MC13XXX_IRQ_RTCRST
);
293 ret
= mc13xxx_irq_request(mc13xxx
, MC13XXX_IRQ_RTCRST
,
294 mc13xxx_rtc_reset_handler
, DRIVER_NAME
, priv
);
296 goto err_irq_request
;
298 ret
= mc13xxx_irq_request_nounmask(mc13xxx
, MC13XXX_IRQ_TODA
,
299 mc13xxx_rtc_alarm_handler
, DRIVER_NAME
, priv
);
301 goto err_irq_request
;
303 mc13xxx_unlock(mc13xxx
);
305 priv
->rtc
= devm_rtc_device_register(&pdev
->dev
, pdev
->name
,
306 &mc13xxx_rtc_ops
, THIS_MODULE
);
311 mc13xxx_irq_free(mc13xxx
, MC13XXX_IRQ_TODA
, priv
);
312 mc13xxx_irq_free(mc13xxx
, MC13XXX_IRQ_RTCRST
, priv
);
314 mc13xxx_unlock(mc13xxx
);
319 static int mc13xxx_rtc_remove(struct platform_device
*pdev
)
321 struct mc13xxx_rtc
*priv
= platform_get_drvdata(pdev
);
323 mc13xxx_lock(priv
->mc13xxx
);
325 mc13xxx_irq_free(priv
->mc13xxx
, MC13XXX_IRQ_TODA
, priv
);
326 mc13xxx_irq_free(priv
->mc13xxx
, MC13XXX_IRQ_RTCRST
, priv
);
328 mc13xxx_unlock(priv
->mc13xxx
);
333 static const struct platform_device_id mc13xxx_rtc_idtable
[] = {
335 .name
= "mc13783-rtc",
337 .name
= "mc13892-rtc",
339 .name
= "mc34708-rtc",
343 MODULE_DEVICE_TABLE(platform
, mc13xxx_rtc_idtable
);
345 static struct platform_driver mc13xxx_rtc_driver
= {
346 .id_table
= mc13xxx_rtc_idtable
,
347 .remove
= mc13xxx_rtc_remove
,
353 module_platform_driver_probe(mc13xxx_rtc_driver
, &mc13xxx_rtc_probe
);
355 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
356 MODULE_DESCRIPTION("RTC driver for Freescale MC13XXX PMIC");
357 MODULE_LICENSE("GPL v2");