1 // SPDX-License-Identifier: GPL-2.0
3 * Real Time Clock driver for Freescale MC13XXX PMIC
5 * (C) 2009 Sascha Hauer, Pengutronix
6 * (C) 2009 Uwe Kleine-Koenig, Pengutronix
9 #include <linux/mfd/mc13xxx.h>
10 #include <linux/platform_device.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/mod_devicetable.h>
14 #include <linux/slab.h>
15 #include <linux/rtc.h>
17 #define DRIVER_NAME "mc13xxx-rtc"
19 #define MC13XXX_RTCTOD 20
20 #define MC13XXX_RTCTODA 21
21 #define MC13XXX_RTCDAY 22
22 #define MC13XXX_RTCDAYA 23
24 #define SEC_PER_DAY (24 * 60 * 60)
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_alarm_irq_enable(struct device *dev,
48 struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
51 mc13xxx_lock(priv->mc13xxx);
53 ret = mc13xxx_rtc_irq_enable_unlocked(dev, enabled, MC13XXX_IRQ_TODA);
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;
71 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCDAY, &days1);
75 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCTOD, &seconds);
79 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCDAY, &days2);
82 } while (days1 != days2);
84 rtc_time64_to_tm((time64_t)days1 * SEC_PER_DAY + seconds, tm);
89 static int mc13xxx_rtc_set_time(struct device *dev, struct rtc_time *tm)
91 struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
92 unsigned int seconds, days;
93 unsigned int alarmseconds;
96 days = div_s64_rem(rtc_tm_to_time64(tm), SEC_PER_DAY, &seconds);
98 mc13xxx_lock(priv->mc13xxx);
101 * temporarily invalidate alarm to prevent triggering it when the day is
102 * already updated while the time isn't yet.
104 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCTODA, &alarmseconds);
108 if (alarmseconds < SEC_PER_DAY) {
109 ret = mc13xxx_reg_write(priv->mc13xxx,
110 MC13XXX_RTCTODA, 0x1ffff);
116 * write seconds=0 to prevent a day switch between writing days
119 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTOD, 0);
123 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCDAY, days);
127 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTOD, seconds);
132 if (alarmseconds < SEC_PER_DAY) {
133 ret = mc13xxx_reg_write(priv->mc13xxx,
134 MC13XXX_RTCTODA, alarmseconds);
140 ret = mc13xxx_irq_ack(priv->mc13xxx, MC13XXX_IRQ_RTCRST);
144 ret = mc13xxx_irq_unmask(priv->mc13xxx, MC13XXX_IRQ_RTCRST);
150 mc13xxx_unlock(priv->mc13xxx);
155 static int mc13xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
157 struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
158 unsigned int seconds, days;
160 int enabled, pending;
163 mc13xxx_lock(priv->mc13xxx);
165 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCTODA, &seconds);
168 if (seconds >= SEC_PER_DAY) {
173 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCDAY, &days);
177 ret = mc13xxx_irq_status(priv->mc13xxx, MC13XXX_IRQ_TODA,
181 mc13xxx_unlock(priv->mc13xxx);
186 alarm->enabled = enabled;
187 alarm->pending = pending;
189 s1970 = (time64_t)days * SEC_PER_DAY + seconds;
191 rtc_time64_to_tm(s1970, &alarm->time);
192 dev_dbg(dev, "%s: %lld\n", __func__, (long long)s1970);
197 static int mc13xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
199 struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
204 mc13xxx_lock(priv->mc13xxx);
206 /* disable alarm to prevent false triggering */
207 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTODA, 0x1ffff);
211 ret = mc13xxx_irq_ack(priv->mc13xxx, MC13XXX_IRQ_TODA);
215 s1970 = rtc_tm_to_time64(&alarm->time);
217 dev_dbg(dev, "%s: %s %lld\n", __func__, alarm->enabled ? "on" : "off",
220 ret = mc13xxx_rtc_irq_enable_unlocked(dev, alarm->enabled,
225 days = div_s64_rem(s1970, SEC_PER_DAY, &seconds);
227 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCDAYA, days);
231 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTODA, seconds);
234 mc13xxx_unlock(priv->mc13xxx);
239 static irqreturn_t mc13xxx_rtc_alarm_handler(int irq, void *dev)
241 struct mc13xxx_rtc *priv = dev;
242 struct mc13xxx *mc13xxx = priv->mc13xxx;
244 rtc_update_irq(priv->rtc, 1, RTC_IRQF | RTC_AF);
246 mc13xxx_irq_ack(mc13xxx, irq);
251 static const struct rtc_class_ops mc13xxx_rtc_ops = {
252 .read_time = mc13xxx_rtc_read_time,
253 .set_time = mc13xxx_rtc_set_time,
254 .read_alarm = mc13xxx_rtc_read_alarm,
255 .set_alarm = mc13xxx_rtc_set_alarm,
256 .alarm_irq_enable = mc13xxx_rtc_alarm_irq_enable,
259 static irqreturn_t mc13xxx_rtc_reset_handler(int irq, void *dev)
261 struct mc13xxx_rtc *priv = dev;
262 struct mc13xxx *mc13xxx = priv->mc13xxx;
266 mc13xxx_irq_mask(mc13xxx, irq);
271 static int __init mc13xxx_rtc_probe(struct platform_device *pdev)
274 struct mc13xxx_rtc *priv;
275 struct mc13xxx *mc13xxx;
277 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
281 mc13xxx = dev_get_drvdata(pdev->dev.parent);
282 priv->mc13xxx = mc13xxx;
285 priv->rtc = devm_rtc_allocate_device(&pdev->dev);
286 if (IS_ERR(priv->rtc))
287 return PTR_ERR(priv->rtc);
288 platform_set_drvdata(pdev, priv);
290 priv->rtc->ops = &mc13xxx_rtc_ops;
291 /* 15bit days + hours, minutes, seconds */
292 priv->rtc->range_max = (timeu64_t)(1 << 15) * SEC_PER_DAY - 1;
294 mc13xxx_lock(mc13xxx);
296 mc13xxx_irq_ack(mc13xxx, MC13XXX_IRQ_RTCRST);
298 ret = mc13xxx_irq_request(mc13xxx, MC13XXX_IRQ_RTCRST,
299 mc13xxx_rtc_reset_handler, DRIVER_NAME, priv);
301 goto err_irq_request;
303 ret = mc13xxx_irq_request_nounmask(mc13xxx, MC13XXX_IRQ_TODA,
304 mc13xxx_rtc_alarm_handler, DRIVER_NAME, priv);
306 goto err_irq_request;
308 mc13xxx_unlock(mc13xxx);
310 ret = devm_rtc_register_device(priv->rtc);
312 mc13xxx_lock(mc13xxx);
313 goto err_irq_request;
319 mc13xxx_irq_free(mc13xxx, MC13XXX_IRQ_TODA, priv);
320 mc13xxx_irq_free(mc13xxx, MC13XXX_IRQ_RTCRST, priv);
322 mc13xxx_unlock(mc13xxx);
327 static int mc13xxx_rtc_remove(struct platform_device *pdev)
329 struct mc13xxx_rtc *priv = platform_get_drvdata(pdev);
331 mc13xxx_lock(priv->mc13xxx);
333 mc13xxx_irq_free(priv->mc13xxx, MC13XXX_IRQ_TODA, priv);
334 mc13xxx_irq_free(priv->mc13xxx, MC13XXX_IRQ_RTCRST, priv);
336 mc13xxx_unlock(priv->mc13xxx);
341 static const struct platform_device_id mc13xxx_rtc_idtable[] = {
343 .name = "mc13783-rtc",
345 .name = "mc13892-rtc",
347 .name = "mc34708-rtc",
351 MODULE_DEVICE_TABLE(platform, mc13xxx_rtc_idtable);
353 static struct platform_driver mc13xxx_rtc_driver = {
354 .id_table = mc13xxx_rtc_idtable,
355 .remove = mc13xxx_rtc_remove,
361 module_platform_driver_probe(mc13xxx_rtc_driver, &mc13xxx_rtc_probe);
363 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
364 MODULE_DESCRIPTION("RTC driver for Freescale MC13XXX PMIC");
365 MODULE_LICENSE("GPL v2");