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_read_time(struct device
*dev
, struct rtc_time
*tm
)
33 struct mc13783_rtc
*priv
= dev_get_drvdata(dev
);
34 unsigned int seconds
, days1
, days2
;
38 mc13783_lock(priv
->mc13783
);
45 ret
= mc13783_reg_read(priv
->mc13783
, MC13783_RTCDAY
, &days1
);
49 ret
= mc13783_reg_read(priv
->mc13783
, MC13783_RTCTOD
, &seconds
);
53 ret
= mc13783_reg_read(priv
->mc13783
, MC13783_RTCDAY
, &days2
);
55 mc13783_unlock(priv
->mc13783
);
60 if (days2
== days1
+ 1) {
61 if (seconds
>= 86400 / 2)
70 s1970
= days1
* 86400 + seconds
;
72 rtc_time_to_tm(s1970
, tm
);
74 return rtc_valid_tm(tm
);
77 static int mc13783_rtc_set_mmss(struct device
*dev
, unsigned long secs
)
79 struct mc13783_rtc
*priv
= dev_get_drvdata(dev
);
80 unsigned int seconds
, days
;
83 seconds
= secs
% 86400;
86 mc13783_lock(priv
->mc13783
);
89 * first write seconds=0 to prevent a day switch between writing days
92 ret
= mc13783_reg_write(priv
->mc13783
, MC13783_RTCTOD
, 0);
96 ret
= mc13783_reg_write(priv
->mc13783
, MC13783_RTCDAY
, days
);
100 ret
= mc13783_reg_write(priv
->mc13783
, MC13783_RTCTOD
, seconds
);
104 ret
= mc13783_ackirq(priv
->mc13783
, MC13783_IRQ_RTCRST
);
108 ret
= mc13783_unmask(priv
->mc13783
, MC13783_IRQ_RTCRST
);
112 mc13783_unlock(priv
->mc13783
);
117 static irqreturn_t
mc13783_rtc_update_handler(int irq
, void *dev
)
119 struct mc13783_rtc
*priv
= dev
;
120 struct mc13783
*mc13783
= priv
->mc13783
;
122 dev_dbg(&priv
->rtc
->dev
, "1HZ\n");
124 rtc_update_irq(priv
->rtc
, 1, RTC_IRQF
| RTC_UF
);
126 mc13783_ackirq(mc13783
, irq
);
131 static int mc13783_rtc_update_irq_enable(struct device
*dev
,
132 unsigned int enabled
)
134 struct mc13783_rtc
*priv
= dev_get_drvdata(dev
);
137 mc13783_lock(priv
->mc13783
);
141 ret
= (enabled
? mc13783_unmask
: mc13783_mask
)(priv
->mc13783
,
144 mc13783_unlock(priv
->mc13783
);
149 static const struct rtc_class_ops mc13783_rtc_ops
= {
150 .read_time
= mc13783_rtc_read_time
,
151 .set_mmss
= mc13783_rtc_set_mmss
,
152 .update_irq_enable
= mc13783_rtc_update_irq_enable
,
155 static irqreturn_t
mc13783_rtc_reset_handler(int irq
, void *dev
)
157 struct mc13783_rtc
*priv
= dev
;
158 struct mc13783
*mc13783
= priv
->mc13783
;
160 dev_dbg(&priv
->rtc
->dev
, "RTCRST\n");
163 mc13783_mask(mc13783
, irq
);
168 static int __devinit
mc13783_rtc_probe(struct platform_device
*pdev
)
171 struct mc13783_rtc
*priv
;
173 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
177 priv
->mc13783
= dev_get_drvdata(pdev
->dev
.parent
);
178 platform_set_drvdata(pdev
, priv
);
182 mc13783_lock(priv
->mc13783
);
184 ret
= mc13783_irq_request(priv
->mc13783
, MC13783_IRQ_RTCRST
,
185 mc13783_rtc_reset_handler
, DRIVER_NAME
, priv
);
187 goto err_reset_irq_request
;
189 ret
= mc13783_irq_request_nounmask(priv
->mc13783
, MC13783_IRQ_1HZ
,
190 mc13783_rtc_update_handler
, DRIVER_NAME
, priv
);
192 goto err_update_irq_request
;
194 mc13783_unlock(priv
->mc13783
);
196 priv
->rtc
= rtc_device_register(pdev
->name
,
197 &pdev
->dev
, &mc13783_rtc_ops
, THIS_MODULE
);
199 if (IS_ERR(priv
->rtc
)) {
200 ret
= PTR_ERR(priv
->rtc
);
202 mc13783_lock(priv
->mc13783
);
204 mc13783_irq_free(priv
->mc13783
, MC13783_IRQ_1HZ
, priv
);
205 err_update_irq_request
:
207 mc13783_irq_free(priv
->mc13783
, MC13783_IRQ_RTCRST
, priv
);
208 err_reset_irq_request
:
210 mc13783_unlock(priv
->mc13783
);
212 platform_set_drvdata(pdev
, NULL
);
219 static int __devexit
mc13783_rtc_remove(struct platform_device
*pdev
)
221 struct mc13783_rtc
*priv
= platform_get_drvdata(pdev
);
223 rtc_device_unregister(priv
->rtc
);
225 mc13783_lock(priv
->mc13783
);
227 mc13783_irq_free(priv
->mc13783
, MC13783_IRQ_1HZ
, priv
);
228 mc13783_irq_free(priv
->mc13783
, MC13783_IRQ_RTCRST
, priv
);
230 mc13783_unlock(priv
->mc13783
);
232 platform_set_drvdata(pdev
, NULL
);
239 static struct platform_driver mc13783_rtc_driver
= {
240 .remove
= __devexit_p(mc13783_rtc_remove
),
243 .owner
= THIS_MODULE
,
247 static int __init
mc13783_rtc_init(void)
249 return platform_driver_probe(&mc13783_rtc_driver
, &mc13783_rtc_probe
);
251 module_init(mc13783_rtc_init
);
253 static void __exit
mc13783_rtc_exit(void)
255 platform_driver_unregister(&mc13783_rtc_driver
);
257 module_exit(mc13783_rtc_exit
);
259 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
260 MODULE_DESCRIPTION("RTC driver for Freescale MC13783 PMIC");
261 MODULE_LICENSE("GPL v2");
262 MODULE_ALIAS("platform:" DRIVER_NAME
);