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_time_to_tm(days1
* SEC_PER_DAY
+ seconds
, tm
);
88 return rtc_valid_tm(tm
);
91 static int mc13xxx_rtc_set_mmss(struct device
*dev
, unsigned long secs
)
93 struct mc13xxx_rtc
*priv
= dev_get_drvdata(dev
);
94 unsigned int seconds
, days
;
95 unsigned int alarmseconds
;
98 seconds
= secs
% SEC_PER_DAY
;
99 days
= secs
/ SEC_PER_DAY
;
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
= days
* SEC_PER_DAY
+ seconds
;
194 rtc_time_to_tm(s1970
, &alarm
->time
);
195 dev_dbg(dev
, "%s: %lu\n", __func__
, s1970
);
200 static int mc13xxx_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
202 struct mc13xxx_rtc
*priv
= dev_get_drvdata(dev
);
204 unsigned seconds
, days
;
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 ret
= rtc_tm_to_time(&alarm
->time
, &s1970
);
222 dev_dbg(dev
, "%s: o%2.s %lu\n", __func__
, alarm
->enabled
? "n" : "ff",
225 ret
= mc13xxx_rtc_irq_enable_unlocked(dev
, alarm
->enabled
,
230 seconds
= s1970
% SEC_PER_DAY
;
231 days
= s1970
/ SEC_PER_DAY
;
233 ret
= mc13xxx_reg_write(priv
->mc13xxx
, MC13XXX_RTCDAYA
, days
);
237 ret
= mc13xxx_reg_write(priv
->mc13xxx
, MC13XXX_RTCTODA
, seconds
);
240 mc13xxx_unlock(priv
->mc13xxx
);
245 static irqreturn_t
mc13xxx_rtc_alarm_handler(int irq
, void *dev
)
247 struct mc13xxx_rtc
*priv
= dev
;
248 struct mc13xxx
*mc13xxx
= priv
->mc13xxx
;
250 rtc_update_irq(priv
->rtc
, 1, RTC_IRQF
| RTC_AF
);
252 mc13xxx_irq_ack(mc13xxx
, irq
);
257 static irqreturn_t
mc13xxx_rtc_update_handler(int irq
, void *dev
)
259 struct mc13xxx_rtc
*priv
= dev
;
260 struct mc13xxx
*mc13xxx
= priv
->mc13xxx
;
262 rtc_update_irq(priv
->rtc
, 1, RTC_IRQF
| RTC_UF
);
264 mc13xxx_irq_ack(mc13xxx
, irq
);
269 static const struct rtc_class_ops mc13xxx_rtc_ops
= {
270 .read_time
= mc13xxx_rtc_read_time
,
271 .set_mmss
= mc13xxx_rtc_set_mmss
,
272 .read_alarm
= mc13xxx_rtc_read_alarm
,
273 .set_alarm
= mc13xxx_rtc_set_alarm
,
274 .alarm_irq_enable
= mc13xxx_rtc_alarm_irq_enable
,
277 static irqreturn_t
mc13xxx_rtc_reset_handler(int irq
, void *dev
)
279 struct mc13xxx_rtc
*priv
= dev
;
280 struct mc13xxx
*mc13xxx
= priv
->mc13xxx
;
284 mc13xxx_irq_mask(mc13xxx
, irq
);
289 static int __init
mc13xxx_rtc_probe(struct platform_device
*pdev
)
292 struct mc13xxx_rtc
*priv
;
293 struct mc13xxx
*mc13xxx
;
295 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
299 mc13xxx
= dev_get_drvdata(pdev
->dev
.parent
);
300 priv
->mc13xxx
= mc13xxx
;
303 platform_set_drvdata(pdev
, priv
);
305 mc13xxx_lock(mc13xxx
);
307 mc13xxx_irq_ack(mc13xxx
, MC13XXX_IRQ_RTCRST
);
309 ret
= mc13xxx_irq_request(mc13xxx
, MC13XXX_IRQ_RTCRST
,
310 mc13xxx_rtc_reset_handler
, DRIVER_NAME
, priv
);
312 goto err_irq_request
;
314 ret
= mc13xxx_irq_request(mc13xxx
, MC13XXX_IRQ_1HZ
,
315 mc13xxx_rtc_update_handler
, DRIVER_NAME
, priv
);
317 goto err_irq_request
;
319 ret
= mc13xxx_irq_request_nounmask(mc13xxx
, MC13XXX_IRQ_TODA
,
320 mc13xxx_rtc_alarm_handler
, DRIVER_NAME
, priv
);
322 goto err_irq_request
;
324 mc13xxx_unlock(mc13xxx
);
326 priv
->rtc
= devm_rtc_device_register(&pdev
->dev
, pdev
->name
,
327 &mc13xxx_rtc_ops
, THIS_MODULE
);
332 mc13xxx_irq_free(mc13xxx
, MC13XXX_IRQ_TODA
, priv
);
333 mc13xxx_irq_free(mc13xxx
, MC13XXX_IRQ_1HZ
, priv
);
334 mc13xxx_irq_free(mc13xxx
, MC13XXX_IRQ_RTCRST
, priv
);
336 mc13xxx_unlock(mc13xxx
);
341 static int mc13xxx_rtc_remove(struct platform_device
*pdev
)
343 struct mc13xxx_rtc
*priv
= platform_get_drvdata(pdev
);
345 mc13xxx_lock(priv
->mc13xxx
);
347 mc13xxx_irq_free(priv
->mc13xxx
, MC13XXX_IRQ_TODA
, priv
);
348 mc13xxx_irq_free(priv
->mc13xxx
, MC13XXX_IRQ_1HZ
, priv
);
349 mc13xxx_irq_free(priv
->mc13xxx
, MC13XXX_IRQ_RTCRST
, priv
);
351 mc13xxx_unlock(priv
->mc13xxx
);
356 static const struct platform_device_id mc13xxx_rtc_idtable
[] = {
358 .name
= "mc13783-rtc",
360 .name
= "mc13892-rtc",
362 .name
= "mc34708-rtc",
366 MODULE_DEVICE_TABLE(platform
, mc13xxx_rtc_idtable
);
368 static struct platform_driver mc13xxx_rtc_driver
= {
369 .id_table
= mc13xxx_rtc_idtable
,
370 .remove
= mc13xxx_rtc_remove
,
376 module_platform_driver_probe(mc13xxx_rtc_driver
, &mc13xxx_rtc_probe
);
378 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
379 MODULE_DESCRIPTION("RTC driver for Freescale MC13XXX PMIC");
380 MODULE_LICENSE("GPL v2");