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
27 struct rtc_device *rtc;
28 struct mc13xxx *mc13xxx;
32 static int mc13xxx_rtc_irq_enable_unlocked(struct device *dev,
33 unsigned int enabled, int irq)
35 struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
36 int (*func)(struct mc13xxx *mc13xxx, int irq);
41 func = enabled ? mc13xxx_irq_unmask : mc13xxx_irq_mask;
42 return func(priv->mc13xxx, irq);
45 static int mc13xxx_rtc_irq_enable(struct device *dev,
46 unsigned int enabled, int irq)
48 struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
51 mc13xxx_lock(priv->mc13xxx);
53 ret = mc13xxx_rtc_irq_enable_unlocked(dev, enabled, irq);
55 mc13xxx_unlock(priv->mc13xxx);
60 static int mc13xxx_rtc_read_time(struct device *dev, struct rtc_time *tm)
62 struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
63 unsigned int seconds, days1, days2;
67 mc13xxx_lock(priv->mc13xxx);
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);
84 mc13xxx_unlock(priv->mc13xxx);
89 if (days2 == days1 + 1) {
90 if (seconds >= 86400 / 2)
99 s1970 = days1 * 86400 + seconds;
101 rtc_time_to_tm(s1970, tm);
103 return rtc_valid_tm(tm);
106 static int mc13xxx_rtc_set_mmss(struct device *dev, unsigned long secs)
108 struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
109 unsigned int seconds, days;
110 unsigned int alarmseconds;
113 seconds = secs % 86400;
116 mc13xxx_lock(priv->mc13xxx);
119 * temporarily invalidate alarm to prevent triggering it when the day is
120 * already updated while the time isn't yet.
122 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCTODA, &alarmseconds);
126 if (alarmseconds < 86400) {
127 ret = mc13xxx_reg_write(priv->mc13xxx,
128 MC13XXX_RTCTODA, 0x1ffff);
134 * write seconds=0 to prevent a day switch between writing days
137 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTOD, 0);
141 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCDAY, days);
145 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTOD, seconds);
150 if (alarmseconds < 86400) {
151 ret = mc13xxx_reg_write(priv->mc13xxx,
152 MC13XXX_RTCTODA, alarmseconds);
157 ret = mc13xxx_irq_ack(priv->mc13xxx, MC13XXX_IRQ_RTCRST);
161 ret = mc13xxx_irq_unmask(priv->mc13xxx, MC13XXX_IRQ_RTCRST);
165 mc13xxx_unlock(priv->mc13xxx);
170 static int mc13xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
172 struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
173 unsigned seconds, days;
175 int enabled, pending;
178 mc13xxx_lock(priv->mc13xxx);
180 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCTODA, &seconds);
183 if (seconds >= 86400) {
188 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCDAY, &days);
192 ret = mc13xxx_irq_status(priv->mc13xxx, MC13XXX_IRQ_TODA,
196 mc13xxx_unlock(priv->mc13xxx);
201 alarm->enabled = enabled;
202 alarm->pending = pending;
204 s1970 = days * 86400 + seconds;
206 rtc_time_to_tm(s1970, &alarm->time);
207 dev_dbg(dev, "%s: %lu\n", __func__, s1970);
212 static int mc13xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
214 struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
216 unsigned seconds, days;
219 mc13xxx_lock(priv->mc13xxx);
221 /* disable alarm to prevent false triggering */
222 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTODA, 0x1ffff);
226 ret = mc13xxx_irq_ack(priv->mc13xxx, MC13XXX_IRQ_TODA);
230 ret = rtc_tm_to_time(&alarm->time, &s1970);
234 dev_dbg(dev, "%s: o%2.s %lu\n", __func__, alarm->enabled ? "n" : "ff",
237 ret = mc13xxx_rtc_irq_enable_unlocked(dev, alarm->enabled,
242 seconds = s1970 % 86400;
243 days = s1970 / 86400;
245 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCDAYA, days);
249 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTODA, seconds);
252 mc13xxx_unlock(priv->mc13xxx);
257 static irqreturn_t mc13xxx_rtc_alarm_handler(int irq, void *dev)
259 struct mc13xxx_rtc *priv = dev;
260 struct mc13xxx *mc13xxx = priv->mc13xxx;
262 dev_dbg(&priv->rtc->dev, "Alarm\n");
264 rtc_update_irq(priv->rtc, 1, RTC_IRQF | RTC_AF);
266 mc13xxx_irq_ack(mc13xxx, irq);
271 static irqreturn_t mc13xxx_rtc_update_handler(int irq, void *dev)
273 struct mc13xxx_rtc *priv = dev;
274 struct mc13xxx *mc13xxx = priv->mc13xxx;
276 dev_dbg(&priv->rtc->dev, "1HZ\n");
278 rtc_update_irq(priv->rtc, 1, RTC_IRQF | RTC_UF);
280 mc13xxx_irq_ack(mc13xxx, irq);
285 static int mc13xxx_rtc_alarm_irq_enable(struct device *dev,
286 unsigned int enabled)
288 return mc13xxx_rtc_irq_enable(dev, enabled, MC13XXX_IRQ_TODA);
291 static const struct rtc_class_ops mc13xxx_rtc_ops = {
292 .read_time = mc13xxx_rtc_read_time,
293 .set_mmss = mc13xxx_rtc_set_mmss,
294 .read_alarm = mc13xxx_rtc_read_alarm,
295 .set_alarm = mc13xxx_rtc_set_alarm,
296 .alarm_irq_enable = mc13xxx_rtc_alarm_irq_enable,
299 static irqreturn_t mc13xxx_rtc_reset_handler(int irq, void *dev)
301 struct mc13xxx_rtc *priv = dev;
302 struct mc13xxx *mc13xxx = priv->mc13xxx;
304 dev_dbg(&priv->rtc->dev, "RTCRST\n");
307 mc13xxx_irq_mask(mc13xxx, irq);
312 static int __init mc13xxx_rtc_probe(struct platform_device *pdev)
315 struct mc13xxx_rtc *priv;
316 struct mc13xxx *mc13xxx;
319 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
323 mc13xxx = dev_get_drvdata(pdev->dev.parent);
324 priv->mc13xxx = mc13xxx;
326 platform_set_drvdata(pdev, priv);
328 priv->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
329 &mc13xxx_rtc_ops, THIS_MODULE);
330 if (IS_ERR(priv->rtc))
331 return PTR_ERR(priv->rtc);
333 mc13xxx_lock(mc13xxx);
335 ret = mc13xxx_irq_request(mc13xxx, MC13XXX_IRQ_RTCRST,
336 mc13xxx_rtc_reset_handler, DRIVER_NAME, priv);
338 goto err_reset_irq_request;
340 ret = mc13xxx_irq_status(mc13xxx, MC13XXX_IRQ_RTCRST,
341 NULL, &rtcrst_pending);
343 goto err_reset_irq_status;
345 priv->valid = !rtcrst_pending;
347 ret = mc13xxx_irq_request_nounmask(mc13xxx, MC13XXX_IRQ_1HZ,
348 mc13xxx_rtc_update_handler, DRIVER_NAME, priv);
350 goto err_reset_irq_status;
352 ret = mc13xxx_irq_request_nounmask(mc13xxx, MC13XXX_IRQ_TODA,
353 mc13xxx_rtc_alarm_handler, DRIVER_NAME, priv);
355 goto err_reset_irq_request;
357 mc13xxx_irq_free(mc13xxx, MC13XXX_IRQ_1HZ, priv);
359 err_reset_irq_status:
360 mc13xxx_irq_free(mc13xxx, MC13XXX_IRQ_RTCRST, priv);
362 err_reset_irq_request:
363 mc13xxx_unlock(mc13xxx);
368 static int mc13xxx_rtc_remove(struct platform_device *pdev)
370 struct mc13xxx_rtc *priv = platform_get_drvdata(pdev);
372 mc13xxx_lock(priv->mc13xxx);
374 mc13xxx_irq_free(priv->mc13xxx, MC13XXX_IRQ_TODA, priv);
375 mc13xxx_irq_free(priv->mc13xxx, MC13XXX_IRQ_1HZ, priv);
376 mc13xxx_irq_free(priv->mc13xxx, MC13XXX_IRQ_RTCRST, priv);
378 mc13xxx_unlock(priv->mc13xxx);
383 static const struct platform_device_id mc13xxx_rtc_idtable[] = {
385 .name = "mc13783-rtc",
387 .name = "mc13892-rtc",
389 .name = "mc34708-rtc",
393 MODULE_DEVICE_TABLE(platform, mc13xxx_rtc_idtable);
395 static struct platform_driver mc13xxx_rtc_driver = {
396 .id_table = mc13xxx_rtc_idtable,
397 .remove = mc13xxx_rtc_remove,
400 .owner = THIS_MODULE,
404 module_platform_driver_probe(mc13xxx_rtc_driver, &mc13xxx_rtc_probe);
406 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
407 MODULE_DESCRIPTION("RTC driver for Freescale MC13XXX PMIC");
408 MODULE_LICENSE("GPL v2");