staging: fix build failure in bcm driver
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / rtc / rtc-mc13xxx.c
blob5314b153bfba73f795d11b2099534af19c0098a5
1 /*
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
26 struct mc13xxx_rtc {
27 struct rtc_device *rtc;
28 struct mc13xxx *mc13xxx;
29 int valid;
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);
38 if (!priv->valid)
39 return -ENODATA;
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);
49 int ret;
51 mc13xxx_lock(priv->mc13xxx);
53 ret = mc13xxx_rtc_irq_enable_unlocked(dev, enabled, irq);
55 mc13xxx_unlock(priv->mc13xxx);
57 return ret;
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;
64 unsigned long s1970;
65 int ret;
67 mc13xxx_lock(priv->mc13xxx);
69 if (!priv->valid) {
70 ret = -ENODATA;
71 goto out;
74 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCDAY, &days1);
75 if (unlikely(ret))
76 goto out;
78 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCTOD, &seconds);
79 if (unlikely(ret))
80 goto out;
82 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCDAY, &days2);
83 out:
84 mc13xxx_unlock(priv->mc13xxx);
86 if (ret)
87 return ret;
89 if (days2 == days1 + 1) {
90 if (seconds >= 86400 / 2)
91 days2 = days1;
92 else
93 days1 = days2;
96 if (days1 != days2)
97 return -EIO;
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;
111 int ret;
113 seconds = secs % 86400;
114 days = 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);
123 if (unlikely(ret))
124 goto out;
126 if (alarmseconds < 86400) {
127 ret = mc13xxx_reg_write(priv->mc13xxx,
128 MC13XXX_RTCTODA, 0x1ffff);
129 if (unlikely(ret))
130 goto out;
134 * write seconds=0 to prevent a day switch between writing days
135 * and seconds below
137 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTOD, 0);
138 if (unlikely(ret))
139 goto out;
141 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCDAY, days);
142 if (unlikely(ret))
143 goto out;
145 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTOD, seconds);
146 if (unlikely(ret))
147 goto out;
149 /* restore alarm */
150 if (alarmseconds < 86400) {
151 ret = mc13xxx_reg_write(priv->mc13xxx,
152 MC13XXX_RTCTODA, alarmseconds);
153 if (unlikely(ret))
154 goto out;
157 ret = mc13xxx_irq_ack(priv->mc13xxx, MC13XXX_IRQ_RTCRST);
158 if (unlikely(ret))
159 goto out;
161 ret = mc13xxx_irq_unmask(priv->mc13xxx, MC13XXX_IRQ_RTCRST);
162 out:
163 priv->valid = !ret;
165 mc13xxx_unlock(priv->mc13xxx);
167 return ret;
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;
174 unsigned long s1970;
175 int enabled, pending;
176 int ret;
178 mc13xxx_lock(priv->mc13xxx);
180 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCTODA, &seconds);
181 if (unlikely(ret))
182 goto out;
183 if (seconds >= 86400) {
184 ret = -ENODATA;
185 goto out;
188 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCDAY, &days);
189 if (unlikely(ret))
190 goto out;
192 ret = mc13xxx_irq_status(priv->mc13xxx, MC13XXX_IRQ_TODA,
193 &enabled, &pending);
195 out:
196 mc13xxx_unlock(priv->mc13xxx);
198 if (ret)
199 return ret;
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);
209 return 0;
212 static int mc13xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
214 struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
215 unsigned long s1970;
216 unsigned seconds, days;
217 int ret;
219 mc13xxx_lock(priv->mc13xxx);
221 /* disable alarm to prevent false triggering */
222 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTODA, 0x1ffff);
223 if (unlikely(ret))
224 goto out;
226 ret = mc13xxx_irq_ack(priv->mc13xxx, MC13XXX_IRQ_TODA);
227 if (unlikely(ret))
228 goto out;
230 ret = rtc_tm_to_time(&alarm->time, &s1970);
231 if (unlikely(ret))
232 goto out;
234 dev_dbg(dev, "%s: o%2.s %lu\n", __func__, alarm->enabled ? "n" : "ff",
235 s1970);
237 ret = mc13xxx_rtc_irq_enable_unlocked(dev, alarm->enabled,
238 MC13XXX_IRQ_TODA);
239 if (unlikely(ret))
240 goto out;
242 seconds = s1970 % 86400;
243 days = s1970 / 86400;
245 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCDAYA, days);
246 if (unlikely(ret))
247 goto out;
249 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTODA, seconds);
251 out:
252 mc13xxx_unlock(priv->mc13xxx);
254 return ret;
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);
268 return IRQ_HANDLED;
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);
282 return IRQ_HANDLED;
285 static int mc13xxx_rtc_update_irq_enable(struct device *dev,
286 unsigned int enabled)
288 return mc13xxx_rtc_irq_enable(dev, enabled, MC13XXX_IRQ_1HZ);
291 static int mc13xxx_rtc_alarm_irq_enable(struct device *dev,
292 unsigned int enabled)
294 return mc13xxx_rtc_irq_enable(dev, enabled, MC13XXX_IRQ_TODA);
297 static const struct rtc_class_ops mc13xxx_rtc_ops = {
298 .read_time = mc13xxx_rtc_read_time,
299 .set_mmss = mc13xxx_rtc_set_mmss,
300 .read_alarm = mc13xxx_rtc_read_alarm,
301 .set_alarm = mc13xxx_rtc_set_alarm,
302 .alarm_irq_enable = mc13xxx_rtc_alarm_irq_enable,
303 .update_irq_enable = mc13xxx_rtc_update_irq_enable,
306 static irqreturn_t mc13xxx_rtc_reset_handler(int irq, void *dev)
308 struct mc13xxx_rtc *priv = dev;
309 struct mc13xxx *mc13xxx = priv->mc13xxx;
311 dev_dbg(&priv->rtc->dev, "RTCRST\n");
312 priv->valid = 0;
314 mc13xxx_irq_mask(mc13xxx, irq);
316 return IRQ_HANDLED;
319 static int __devinit mc13xxx_rtc_probe(struct platform_device *pdev)
321 int ret;
322 struct mc13xxx_rtc *priv;
323 struct mc13xxx *mc13xxx;
324 int rtcrst_pending;
326 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
327 if (!priv)
328 return -ENOMEM;
330 mc13xxx = dev_get_drvdata(pdev->dev.parent);
331 priv->mc13xxx = mc13xxx;
333 platform_set_drvdata(pdev, priv);
335 mc13xxx_lock(mc13xxx);
337 ret = mc13xxx_irq_request(mc13xxx, MC13XXX_IRQ_RTCRST,
338 mc13xxx_rtc_reset_handler, DRIVER_NAME, priv);
339 if (ret)
340 goto err_reset_irq_request;
342 ret = mc13xxx_irq_status(mc13xxx, MC13XXX_IRQ_RTCRST,
343 NULL, &rtcrst_pending);
344 if (ret)
345 goto err_reset_irq_status;
347 priv->valid = !rtcrst_pending;
349 ret = mc13xxx_irq_request_nounmask(mc13xxx, MC13XXX_IRQ_1HZ,
350 mc13xxx_rtc_update_handler, DRIVER_NAME, priv);
351 if (ret)
352 goto err_update_irq_request;
354 ret = mc13xxx_irq_request_nounmask(mc13xxx, MC13XXX_IRQ_TODA,
355 mc13xxx_rtc_alarm_handler, DRIVER_NAME, priv);
356 if (ret)
357 goto err_alarm_irq_request;
359 priv->rtc = rtc_device_register(pdev->name,
360 &pdev->dev, &mc13xxx_rtc_ops, THIS_MODULE);
361 if (IS_ERR(priv->rtc)) {
362 ret = PTR_ERR(priv->rtc);
364 mc13xxx_irq_free(mc13xxx, MC13XXX_IRQ_TODA, priv);
365 err_alarm_irq_request:
367 mc13xxx_irq_free(mc13xxx, MC13XXX_IRQ_1HZ, priv);
368 err_update_irq_request:
370 err_reset_irq_status:
372 mc13xxx_irq_free(mc13xxx, MC13XXX_IRQ_RTCRST, priv);
373 err_reset_irq_request:
375 platform_set_drvdata(pdev, NULL);
376 kfree(priv);
379 mc13xxx_unlock(mc13xxx);
381 return ret;
384 static int __devexit mc13xxx_rtc_remove(struct platform_device *pdev)
386 struct mc13xxx_rtc *priv = platform_get_drvdata(pdev);
388 mc13xxx_lock(priv->mc13xxx);
390 rtc_device_unregister(priv->rtc);
392 mc13xxx_irq_free(priv->mc13xxx, MC13XXX_IRQ_TODA, priv);
393 mc13xxx_irq_free(priv->mc13xxx, MC13XXX_IRQ_1HZ, priv);
394 mc13xxx_irq_free(priv->mc13xxx, MC13XXX_IRQ_RTCRST, priv);
396 mc13xxx_unlock(priv->mc13xxx);
398 platform_set_drvdata(pdev, NULL);
400 kfree(priv);
402 return 0;
405 const struct platform_device_id mc13xxx_rtc_idtable[] = {
407 .name = "mc13783-rtc",
408 }, {
409 .name = "mc13892-rtc",
413 static struct platform_driver mc13xxx_rtc_driver = {
414 .id_table = mc13xxx_rtc_idtable,
415 .remove = __devexit_p(mc13xxx_rtc_remove),
416 .driver = {
417 .name = DRIVER_NAME,
418 .owner = THIS_MODULE,
422 static int __init mc13xxx_rtc_init(void)
424 return platform_driver_probe(&mc13xxx_rtc_driver, &mc13xxx_rtc_probe);
426 module_init(mc13xxx_rtc_init);
428 static void __exit mc13xxx_rtc_exit(void)
430 platform_driver_unregister(&mc13xxx_rtc_driver);
432 module_exit(mc13xxx_rtc_exit);
434 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
435 MODULE_DESCRIPTION("RTC driver for Freescale MC13XXX PMIC");
436 MODULE_LICENSE("GPL v2");
437 MODULE_ALIAS("platform:" DRIVER_NAME);