2 * Real Time Clock driver for Freescale MC13783 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/mc13783.h>
13 #include <linux/platform_device.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/rtc.h>
18 #define DRIVER_NAME "mc13783-rtc"
20 #define MC13783_RTCTOD 20
21 #define MC13783_RTCTODA 21
22 #define MC13783_RTCDAY 22
23 #define MC13783_RTCDAYA 23
26 struct rtc_device
*rtc
;
27 struct mc13783
*mc13783
;
31 static int mc13783_rtc_irq_enable_unlocked(struct device
*dev
,
32 unsigned int enabled
, int irq
)
34 struct mc13783_rtc
*priv
= dev_get_drvdata(dev
);
35 int (*func
)(struct mc13783
*mc13783
, int irq
);
40 func
= enabled
? mc13783_irq_unmask
: mc13783_irq_mask
;
41 return func(priv
->mc13783
, irq
);
44 static int mc13783_rtc_irq_enable(struct device
*dev
,
45 unsigned int enabled
, int irq
)
47 struct mc13783_rtc
*priv
= dev_get_drvdata(dev
);
50 mc13783_lock(priv
->mc13783
);
52 ret
= mc13783_rtc_irq_enable_unlocked(dev
, enabled
, irq
);
54 mc13783_unlock(priv
->mc13783
);
59 static int mc13783_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
61 struct mc13783_rtc
*priv
= dev_get_drvdata(dev
);
62 unsigned int seconds
, days1
, days2
;
66 mc13783_lock(priv
->mc13783
);
73 ret
= mc13783_reg_read(priv
->mc13783
, MC13783_RTCDAY
, &days1
);
77 ret
= mc13783_reg_read(priv
->mc13783
, MC13783_RTCTOD
, &seconds
);
81 ret
= mc13783_reg_read(priv
->mc13783
, MC13783_RTCDAY
, &days2
);
83 mc13783_unlock(priv
->mc13783
);
88 if (days2
== days1
+ 1) {
89 if (seconds
>= 86400 / 2)
98 s1970
= days1
* 86400 + seconds
;
100 rtc_time_to_tm(s1970
, tm
);
102 return rtc_valid_tm(tm
);
105 static int mc13783_rtc_set_mmss(struct device
*dev
, unsigned long secs
)
107 struct mc13783_rtc
*priv
= dev_get_drvdata(dev
);
108 unsigned int seconds
, days
;
109 unsigned int alarmseconds
;
112 seconds
= secs
% 86400;
115 mc13783_lock(priv
->mc13783
);
118 * temporarily invalidate alarm to prevent triggering it when the day is
119 * already updated while the time isn't yet.
121 ret
= mc13783_reg_read(priv
->mc13783
, MC13783_RTCTODA
, &alarmseconds
);
125 if (alarmseconds
< 86400) {
126 ret
= mc13783_reg_write(priv
->mc13783
,
127 MC13783_RTCTODA
, 0x1ffff);
133 * write seconds=0 to prevent a day switch between writing days
136 ret
= mc13783_reg_write(priv
->mc13783
, MC13783_RTCTOD
, 0);
140 ret
= mc13783_reg_write(priv
->mc13783
, MC13783_RTCDAY
, days
);
144 ret
= mc13783_reg_write(priv
->mc13783
, MC13783_RTCTOD
, seconds
);
149 if (alarmseconds
< 86400) {
150 ret
= mc13783_reg_write(priv
->mc13783
,
151 MC13783_RTCTODA
, alarmseconds
);
156 ret
= mc13783_irq_ack(priv
->mc13783
, MC13783_IRQ_RTCRST
);
160 ret
= mc13783_irq_unmask(priv
->mc13783
, MC13783_IRQ_RTCRST
);
164 mc13783_unlock(priv
->mc13783
);
169 static int mc13783_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
171 struct mc13783_rtc
*priv
= dev_get_drvdata(dev
);
172 unsigned seconds
, days
;
174 int enabled
, pending
;
177 mc13783_lock(priv
->mc13783
);
179 ret
= mc13783_reg_read(priv
->mc13783
, MC13783_RTCTODA
, &seconds
);
182 if (seconds
>= 86400) {
187 ret
= mc13783_reg_read(priv
->mc13783
, MC13783_RTCDAY
, &days
);
191 ret
= mc13783_irq_status(priv
->mc13783
, MC13783_IRQ_TODA
,
195 mc13783_unlock(priv
->mc13783
);
200 alarm
->enabled
= enabled
;
201 alarm
->pending
= pending
;
203 s1970
= days
* 86400 + seconds
;
205 rtc_time_to_tm(s1970
, &alarm
->time
);
206 dev_dbg(dev
, "%s: %lu\n", __func__
, s1970
);
211 static int mc13783_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
213 struct mc13783_rtc
*priv
= dev_get_drvdata(dev
);
215 unsigned seconds
, days
;
218 mc13783_lock(priv
->mc13783
);
220 /* disable alarm to prevent false triggering */
221 ret
= mc13783_reg_write(priv
->mc13783
, MC13783_RTCTODA
, 0x1ffff);
225 ret
= mc13783_irq_ack(priv
->mc13783
, MC13783_IRQ_TODA
);
229 ret
= rtc_tm_to_time(&alarm
->time
, &s1970
);
233 dev_dbg(dev
, "%s: o%2.s %lu\n", __func__
, alarm
->enabled
? "n" : "ff",
236 ret
= mc13783_rtc_irq_enable_unlocked(dev
, alarm
->enabled
,
241 seconds
= s1970
% 86400;
242 days
= s1970
/ 86400;
244 ret
= mc13783_reg_write(priv
->mc13783
, MC13783_RTCDAYA
, days
);
248 ret
= mc13783_reg_write(priv
->mc13783
, MC13783_RTCTODA
, seconds
);
251 mc13783_unlock(priv
->mc13783
);
256 static irqreturn_t
mc13783_rtc_alarm_handler(int irq
, void *dev
)
258 struct mc13783_rtc
*priv
= dev
;
259 struct mc13783
*mc13783
= priv
->mc13783
;
261 dev_dbg(&priv
->rtc
->dev
, "Alarm\n");
263 rtc_update_irq(priv
->rtc
, 1, RTC_IRQF
| RTC_AF
);
265 mc13783_irq_ack(mc13783
, irq
);
270 static irqreturn_t
mc13783_rtc_update_handler(int irq
, void *dev
)
272 struct mc13783_rtc
*priv
= dev
;
273 struct mc13783
*mc13783
= priv
->mc13783
;
275 dev_dbg(&priv
->rtc
->dev
, "1HZ\n");
277 rtc_update_irq(priv
->rtc
, 1, RTC_IRQF
| RTC_UF
);
279 mc13783_irq_ack(mc13783
, irq
);
284 static int mc13783_rtc_update_irq_enable(struct device
*dev
,
285 unsigned int enabled
)
287 return mc13783_rtc_irq_enable(dev
, enabled
, MC13783_IRQ_1HZ
);
290 static int mc13783_rtc_alarm_irq_enable(struct device
*dev
,
291 unsigned int enabled
)
293 return mc13783_rtc_irq_enable(dev
, enabled
, MC13783_IRQ_TODA
);
296 static const struct rtc_class_ops mc13783_rtc_ops
= {
297 .read_time
= mc13783_rtc_read_time
,
298 .set_mmss
= mc13783_rtc_set_mmss
,
299 .read_alarm
= mc13783_rtc_read_alarm
,
300 .set_alarm
= mc13783_rtc_set_alarm
,
301 .alarm_irq_enable
= mc13783_rtc_alarm_irq_enable
,
302 .update_irq_enable
= mc13783_rtc_update_irq_enable
,
305 static irqreturn_t
mc13783_rtc_reset_handler(int irq
, void *dev
)
307 struct mc13783_rtc
*priv
= dev
;
308 struct mc13783
*mc13783
= priv
->mc13783
;
310 dev_dbg(&priv
->rtc
->dev
, "RTCRST\n");
313 mc13783_irq_mask(mc13783
, irq
);
318 static int __devinit
mc13783_rtc_probe(struct platform_device
*pdev
)
321 struct mc13783_rtc
*priv
;
322 struct mc13783
*mc13783
;
325 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
329 mc13783
= dev_get_drvdata(pdev
->dev
.parent
);
330 priv
->mc13783
= mc13783
;
332 platform_set_drvdata(pdev
, priv
);
334 mc13783_lock(mc13783
);
336 ret
= mc13783_irq_request(mc13783
, MC13783_IRQ_RTCRST
,
337 mc13783_rtc_reset_handler
, DRIVER_NAME
, priv
);
339 goto err_reset_irq_request
;
341 ret
= mc13783_irq_status(mc13783
, MC13783_IRQ_RTCRST
,
342 NULL
, &rtcrst_pending
);
344 goto err_reset_irq_status
;
346 priv
->valid
= !rtcrst_pending
;
348 ret
= mc13783_irq_request_nounmask(mc13783
, MC13783_IRQ_1HZ
,
349 mc13783_rtc_update_handler
, DRIVER_NAME
, priv
);
351 goto err_update_irq_request
;
353 ret
= mc13783_irq_request_nounmask(mc13783
, MC13783_IRQ_TODA
,
354 mc13783_rtc_alarm_handler
, DRIVER_NAME
, priv
);
356 goto err_alarm_irq_request
;
358 priv
->rtc
= rtc_device_register(pdev
->name
,
359 &pdev
->dev
, &mc13783_rtc_ops
, THIS_MODULE
);
360 if (IS_ERR(priv
->rtc
)) {
361 ret
= PTR_ERR(priv
->rtc
);
363 mc13783_irq_free(mc13783
, MC13783_IRQ_TODA
, priv
);
364 err_alarm_irq_request
:
366 mc13783_irq_free(mc13783
, MC13783_IRQ_1HZ
, priv
);
367 err_update_irq_request
:
369 err_reset_irq_status
:
371 mc13783_irq_free(mc13783
, MC13783_IRQ_RTCRST
, priv
);
372 err_reset_irq_request
:
374 platform_set_drvdata(pdev
, NULL
);
378 mc13783_unlock(mc13783
);
383 static int __devexit
mc13783_rtc_remove(struct platform_device
*pdev
)
385 struct mc13783_rtc
*priv
= platform_get_drvdata(pdev
);
387 mc13783_lock(priv
->mc13783
);
389 rtc_device_unregister(priv
->rtc
);
391 mc13783_irq_free(priv
->mc13783
, MC13783_IRQ_TODA
, priv
);
392 mc13783_irq_free(priv
->mc13783
, MC13783_IRQ_1HZ
, priv
);
393 mc13783_irq_free(priv
->mc13783
, MC13783_IRQ_RTCRST
, priv
);
395 mc13783_unlock(priv
->mc13783
);
397 platform_set_drvdata(pdev
, NULL
);
404 static struct platform_driver mc13783_rtc_driver
= {
405 .remove
= __devexit_p(mc13783_rtc_remove
),
408 .owner
= THIS_MODULE
,
412 static int __init
mc13783_rtc_init(void)
414 return platform_driver_probe(&mc13783_rtc_driver
, &mc13783_rtc_probe
);
416 module_init(mc13783_rtc_init
);
418 static void __exit
mc13783_rtc_exit(void)
420 platform_driver_unregister(&mc13783_rtc_driver
);
422 module_exit(mc13783_rtc_exit
);
424 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
425 MODULE_DESCRIPTION("RTC driver for Freescale MC13783 PMIC");
426 MODULE_LICENSE("GPL v2");
427 MODULE_ALIAS("platform:" DRIVER_NAME
);