Linux 2.6.34-rc3
[pohmelfs.git] / drivers / rtc / rtc-mc13783.c
blob1379c7faa44803be830bd37a6b39b554dd8e0c03
1 /*
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
25 struct mc13783_rtc {
26 struct rtc_device *rtc;
27 struct mc13783 *mc13783;
28 int valid;
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);
37 if (!priv->valid)
38 return -ENODATA;
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);
48 int ret;
50 mc13783_lock(priv->mc13783);
52 ret = mc13783_rtc_irq_enable_unlocked(dev, enabled, irq);
54 mc13783_unlock(priv->mc13783);
56 return ret;
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;
63 unsigned long s1970;
64 int ret;
66 mc13783_lock(priv->mc13783);
68 if (!priv->valid) {
69 ret = -ENODATA;
70 goto out;
73 ret = mc13783_reg_read(priv->mc13783, MC13783_RTCDAY, &days1);
74 if (unlikely(ret))
75 goto out;
77 ret = mc13783_reg_read(priv->mc13783, MC13783_RTCTOD, &seconds);
78 if (unlikely(ret))
79 goto out;
81 ret = mc13783_reg_read(priv->mc13783, MC13783_RTCDAY, &days2);
82 out:
83 mc13783_unlock(priv->mc13783);
85 if (ret)
86 return ret;
88 if (days2 == days1 + 1) {
89 if (seconds >= 86400 / 2)
90 days2 = days1;
91 else
92 days1 = days2;
95 if (days1 != days2)
96 return -EIO;
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;
110 int ret;
112 seconds = secs % 86400;
113 days = 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);
122 if (unlikely(ret))
123 goto out;
125 if (alarmseconds < 86400) {
126 ret = mc13783_reg_write(priv->mc13783,
127 MC13783_RTCTODA, 0x1ffff);
128 if (unlikely(ret))
129 goto out;
133 * write seconds=0 to prevent a day switch between writing days
134 * and seconds below
136 ret = mc13783_reg_write(priv->mc13783, MC13783_RTCTOD, 0);
137 if (unlikely(ret))
138 goto out;
140 ret = mc13783_reg_write(priv->mc13783, MC13783_RTCDAY, days);
141 if (unlikely(ret))
142 goto out;
144 ret = mc13783_reg_write(priv->mc13783, MC13783_RTCTOD, seconds);
145 if (unlikely(ret))
146 goto out;
148 /* restore alarm */
149 if (alarmseconds < 86400) {
150 ret = mc13783_reg_write(priv->mc13783,
151 MC13783_RTCTODA, alarmseconds);
152 if (unlikely(ret))
153 goto out;
156 ret = mc13783_irq_ack(priv->mc13783, MC13783_IRQ_RTCRST);
157 if (unlikely(ret))
158 goto out;
160 ret = mc13783_irq_unmask(priv->mc13783, MC13783_IRQ_RTCRST);
161 out:
162 priv->valid = !ret;
164 mc13783_unlock(priv->mc13783);
166 return ret;
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;
173 unsigned long s1970;
174 int enabled, pending;
175 int ret;
177 mc13783_lock(priv->mc13783);
179 ret = mc13783_reg_read(priv->mc13783, MC13783_RTCTODA, &seconds);
180 if (unlikely(ret))
181 goto out;
182 if (seconds >= 86400) {
183 ret = -ENODATA;
184 goto out;
187 ret = mc13783_reg_read(priv->mc13783, MC13783_RTCDAY, &days);
188 if (unlikely(ret))
189 goto out;
191 ret = mc13783_irq_status(priv->mc13783, MC13783_IRQ_TODA,
192 &enabled, &pending);
194 out:
195 mc13783_unlock(priv->mc13783);
197 if (ret)
198 return ret;
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);
208 return 0;
211 static int mc13783_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
213 struct mc13783_rtc *priv = dev_get_drvdata(dev);
214 unsigned long s1970;
215 unsigned seconds, days;
216 int ret;
218 mc13783_lock(priv->mc13783);
220 /* disable alarm to prevent false triggering */
221 ret = mc13783_reg_write(priv->mc13783, MC13783_RTCTODA, 0x1ffff);
222 if (unlikely(ret))
223 goto out;
225 ret = mc13783_irq_ack(priv->mc13783, MC13783_IRQ_TODA);
226 if (unlikely(ret))
227 goto out;
229 ret = rtc_tm_to_time(&alarm->time, &s1970);
230 if (unlikely(ret))
231 goto out;
233 dev_dbg(dev, "%s: o%2.s %lu\n", __func__, alarm->enabled ? "n" : "ff",
234 s1970);
236 ret = mc13783_rtc_irq_enable_unlocked(dev, alarm->enabled,
237 MC13783_IRQ_TODA);
238 if (unlikely(ret))
239 goto out;
241 seconds = s1970 % 86400;
242 days = s1970 / 86400;
244 ret = mc13783_reg_write(priv->mc13783, MC13783_RTCDAYA, days);
245 if (unlikely(ret))
246 goto out;
248 ret = mc13783_reg_write(priv->mc13783, MC13783_RTCTODA, seconds);
250 out:
251 mc13783_unlock(priv->mc13783);
253 return ret;
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);
267 return IRQ_HANDLED;
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);
281 return IRQ_HANDLED;
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");
311 priv->valid = 0;
313 mc13783_irq_mask(mc13783, irq);
315 return IRQ_HANDLED;
318 static int __devinit mc13783_rtc_probe(struct platform_device *pdev)
320 int ret;
321 struct mc13783_rtc *priv;
322 struct mc13783 *mc13783;
323 int rtcrst_pending;
325 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
326 if (!priv)
327 return -ENOMEM;
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);
338 if (ret)
339 goto err_reset_irq_request;
341 ret = mc13783_irq_status(mc13783, MC13783_IRQ_RTCRST,
342 NULL, &rtcrst_pending);
343 if (ret)
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);
350 if (ret)
351 goto err_update_irq_request;
353 ret = mc13783_irq_request_nounmask(mc13783, MC13783_IRQ_TODA,
354 mc13783_rtc_alarm_handler, DRIVER_NAME, priv);
355 if (ret)
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);
375 kfree(priv);
378 mc13783_unlock(mc13783);
380 return ret;
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);
399 kfree(priv);
401 return 0;
404 static struct platform_driver mc13783_rtc_driver = {
405 .remove = __devexit_p(mc13783_rtc_remove),
406 .driver = {
407 .name = DRIVER_NAME,
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);