firewire: core: fw_iso_resource_manage: return -EBUSY when out of resources
[linux/fpc-iii.git] / drivers / rtc / rtc-mc13783.c
blob850f983c039c52a7f6b7e60393e93bc8606bdb22
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_read_time(struct device *dev, struct rtc_time *tm)
33 struct mc13783_rtc *priv = dev_get_drvdata(dev);
34 unsigned int seconds, days1, days2;
35 unsigned long s1970;
36 int ret;
38 mc13783_lock(priv->mc13783);
40 if (!priv->valid) {
41 ret = -ENODATA;
42 goto out;
45 ret = mc13783_reg_read(priv->mc13783, MC13783_RTCDAY, &days1);
46 if (unlikely(ret))
47 goto out;
49 ret = mc13783_reg_read(priv->mc13783, MC13783_RTCTOD, &seconds);
50 if (unlikely(ret))
51 goto out;
53 ret = mc13783_reg_read(priv->mc13783, MC13783_RTCDAY, &days2);
54 out:
55 mc13783_unlock(priv->mc13783);
57 if (ret)
58 return ret;
60 if (days2 == days1 + 1) {
61 if (seconds >= 86400 / 2)
62 days2 = days1;
63 else
64 days1 = days2;
67 if (days1 != days2)
68 return -EIO;
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;
81 int ret;
83 seconds = secs % 86400;
84 days = secs / 86400;
86 mc13783_lock(priv->mc13783);
89 * first write seconds=0 to prevent a day switch between writing days
90 * and seconds below
92 ret = mc13783_reg_write(priv->mc13783, MC13783_RTCTOD, 0);
93 if (unlikely(ret))
94 goto out;
96 ret = mc13783_reg_write(priv->mc13783, MC13783_RTCDAY, days);
97 if (unlikely(ret))
98 goto out;
100 ret = mc13783_reg_write(priv->mc13783, MC13783_RTCTOD, seconds);
101 if (unlikely(ret))
102 goto out;
104 ret = mc13783_ackirq(priv->mc13783, MC13783_IRQ_RTCRST);
105 if (unlikely(ret))
106 goto out;
108 ret = mc13783_unmask(priv->mc13783, MC13783_IRQ_RTCRST);
109 out:
110 priv->valid = !ret;
112 mc13783_unlock(priv->mc13783);
114 return ret;
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);
128 return IRQ_HANDLED;
131 static int mc13783_rtc_update_irq_enable(struct device *dev,
132 unsigned int enabled)
134 struct mc13783_rtc *priv = dev_get_drvdata(dev);
135 int ret = -ENODATA;
137 mc13783_lock(priv->mc13783);
138 if (!priv->valid)
139 goto out;
141 ret = (enabled ? mc13783_unmask : mc13783_mask)(priv->mc13783,
142 MC13783_IRQ_1HZ);
143 out:
144 mc13783_unlock(priv->mc13783);
146 return ret;
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");
161 priv->valid = 0;
163 mc13783_mask(mc13783, irq);
165 return IRQ_HANDLED;
168 static int __devinit mc13783_rtc_probe(struct platform_device *pdev)
170 int ret;
171 struct mc13783_rtc *priv;
173 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
174 if (!priv)
175 return -ENOMEM;
177 priv->mc13783 = dev_get_drvdata(pdev->dev.parent);
178 platform_set_drvdata(pdev, priv);
180 priv->valid = 1;
182 mc13783_lock(priv->mc13783);
184 ret = mc13783_irq_request(priv->mc13783, MC13783_IRQ_RTCRST,
185 mc13783_rtc_reset_handler, DRIVER_NAME, priv);
186 if (ret)
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);
191 if (ret)
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);
213 kfree(priv);
216 return ret;
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);
234 kfree(priv);
236 return 0;
239 static struct platform_driver mc13783_rtc_driver = {
240 .remove = __devexit_p(mc13783_rtc_remove),
241 .driver = {
242 .name = DRIVER_NAME,
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);