staging: rtl8188eu: rename HalSetBrateCfg() - style
[linux/fpc-iii.git] / drivers / rtc / rtc-mc13xxx.c
blob0fa33708fc49eeba5e0c92c35cde5e31409bccce
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/mod_devicetable.h>
17 #include <linux/slab.h>
18 #include <linux/rtc.h>
20 #define DRIVER_NAME "mc13xxx-rtc"
22 #define MC13XXX_RTCTOD 20
23 #define MC13XXX_RTCTODA 21
24 #define MC13XXX_RTCDAY 22
25 #define MC13XXX_RTCDAYA 23
27 #define SEC_PER_DAY (24 * 60 * 60)
29 struct mc13xxx_rtc {
30 struct rtc_device *rtc;
31 struct mc13xxx *mc13xxx;
32 int valid;
35 static int mc13xxx_rtc_irq_enable_unlocked(struct device *dev,
36 unsigned int enabled, int irq)
38 struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
39 int (*func)(struct mc13xxx *mc13xxx, int irq);
41 if (!priv->valid)
42 return -ENODATA;
44 func = enabled ? mc13xxx_irq_unmask : mc13xxx_irq_mask;
45 return func(priv->mc13xxx, irq);
48 static int mc13xxx_rtc_alarm_irq_enable(struct device *dev,
49 unsigned int enabled)
51 struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
52 int ret;
54 mc13xxx_lock(priv->mc13xxx);
56 ret = mc13xxx_rtc_irq_enable_unlocked(dev, enabled, MC13XXX_IRQ_TODA);
58 mc13xxx_unlock(priv->mc13xxx);
60 return ret;
63 static int mc13xxx_rtc_read_time(struct device *dev, struct rtc_time *tm)
65 struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
66 unsigned int seconds, days1, days2;
68 if (!priv->valid)
69 return -ENODATA;
71 do {
72 int ret;
74 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCDAY, &days1);
75 if (ret)
76 return ret;
78 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCTOD, &seconds);
79 if (ret)
80 return ret;
82 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCDAY, &days2);
83 if (ret)
84 return ret;
85 } while (days1 != days2);
87 rtc_time64_to_tm((time64_t)days1 * SEC_PER_DAY + seconds, tm);
89 return 0;
92 static int mc13xxx_rtc_set_mmss(struct device *dev, time64_t secs)
94 struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
95 unsigned int seconds, days;
96 unsigned int alarmseconds;
97 int ret;
99 days = div_s64_rem(secs, SEC_PER_DAY, &seconds);
101 mc13xxx_lock(priv->mc13xxx);
104 * temporarily invalidate alarm to prevent triggering it when the day is
105 * already updated while the time isn't yet.
107 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCTODA, &alarmseconds);
108 if (unlikely(ret))
109 goto out;
111 if (alarmseconds < SEC_PER_DAY) {
112 ret = mc13xxx_reg_write(priv->mc13xxx,
113 MC13XXX_RTCTODA, 0x1ffff);
114 if (unlikely(ret))
115 goto out;
119 * write seconds=0 to prevent a day switch between writing days
120 * and seconds below
122 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTOD, 0);
123 if (unlikely(ret))
124 goto out;
126 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCDAY, days);
127 if (unlikely(ret))
128 goto out;
130 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTOD, seconds);
131 if (unlikely(ret))
132 goto out;
134 /* restore alarm */
135 if (alarmseconds < SEC_PER_DAY) {
136 ret = mc13xxx_reg_write(priv->mc13xxx,
137 MC13XXX_RTCTODA, alarmseconds);
138 if (unlikely(ret))
139 goto out;
142 if (!priv->valid) {
143 ret = mc13xxx_irq_ack(priv->mc13xxx, MC13XXX_IRQ_RTCRST);
144 if (unlikely(ret))
145 goto out;
147 ret = mc13xxx_irq_unmask(priv->mc13xxx, MC13XXX_IRQ_RTCRST);
150 out:
151 priv->valid = !ret;
153 mc13xxx_unlock(priv->mc13xxx);
155 return ret;
158 static int mc13xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
160 struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
161 unsigned seconds, days;
162 time64_t s1970;
163 int enabled, pending;
164 int ret;
166 mc13xxx_lock(priv->mc13xxx);
168 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCTODA, &seconds);
169 if (unlikely(ret))
170 goto out;
171 if (seconds >= SEC_PER_DAY) {
172 ret = -ENODATA;
173 goto out;
176 ret = mc13xxx_reg_read(priv->mc13xxx, MC13XXX_RTCDAY, &days);
177 if (unlikely(ret))
178 goto out;
180 ret = mc13xxx_irq_status(priv->mc13xxx, MC13XXX_IRQ_TODA,
181 &enabled, &pending);
183 out:
184 mc13xxx_unlock(priv->mc13xxx);
186 if (ret)
187 return ret;
189 alarm->enabled = enabled;
190 alarm->pending = pending;
192 s1970 = (time64_t)days * SEC_PER_DAY + seconds;
194 rtc_time64_to_tm(s1970, &alarm->time);
195 dev_dbg(dev, "%s: %lld\n", __func__, (long long)s1970);
197 return 0;
200 static int mc13xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
202 struct mc13xxx_rtc *priv = dev_get_drvdata(dev);
203 time64_t s1970;
204 u32 seconds, days;
205 int ret;
207 mc13xxx_lock(priv->mc13xxx);
209 /* disable alarm to prevent false triggering */
210 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTODA, 0x1ffff);
211 if (unlikely(ret))
212 goto out;
214 ret = mc13xxx_irq_ack(priv->mc13xxx, MC13XXX_IRQ_TODA);
215 if (unlikely(ret))
216 goto out;
218 s1970 = rtc_tm_to_time64(&alarm->time);
220 dev_dbg(dev, "%s: %s %lld\n", __func__, alarm->enabled ? "on" : "off",
221 (long long)s1970);
223 ret = mc13xxx_rtc_irq_enable_unlocked(dev, alarm->enabled,
224 MC13XXX_IRQ_TODA);
225 if (unlikely(ret))
226 goto out;
228 days = div_s64_rem(s1970, SEC_PER_DAY, &seconds);
230 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCDAYA, days);
231 if (unlikely(ret))
232 goto out;
234 ret = mc13xxx_reg_write(priv->mc13xxx, MC13XXX_RTCTODA, seconds);
236 out:
237 mc13xxx_unlock(priv->mc13xxx);
239 return ret;
242 static irqreturn_t mc13xxx_rtc_alarm_handler(int irq, void *dev)
244 struct mc13xxx_rtc *priv = dev;
245 struct mc13xxx *mc13xxx = priv->mc13xxx;
247 rtc_update_irq(priv->rtc, 1, RTC_IRQF | RTC_AF);
249 mc13xxx_irq_ack(mc13xxx, irq);
251 return IRQ_HANDLED;
254 static const struct rtc_class_ops mc13xxx_rtc_ops = {
255 .read_time = mc13xxx_rtc_read_time,
256 .set_mmss64 = mc13xxx_rtc_set_mmss,
257 .read_alarm = mc13xxx_rtc_read_alarm,
258 .set_alarm = mc13xxx_rtc_set_alarm,
259 .alarm_irq_enable = mc13xxx_rtc_alarm_irq_enable,
262 static irqreturn_t mc13xxx_rtc_reset_handler(int irq, void *dev)
264 struct mc13xxx_rtc *priv = dev;
265 struct mc13xxx *mc13xxx = priv->mc13xxx;
267 priv->valid = 0;
269 mc13xxx_irq_mask(mc13xxx, irq);
271 return IRQ_HANDLED;
274 static int __init mc13xxx_rtc_probe(struct platform_device *pdev)
276 int ret;
277 struct mc13xxx_rtc *priv;
278 struct mc13xxx *mc13xxx;
280 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
281 if (!priv)
282 return -ENOMEM;
284 mc13xxx = dev_get_drvdata(pdev->dev.parent);
285 priv->mc13xxx = mc13xxx;
286 priv->valid = 1;
288 platform_set_drvdata(pdev, priv);
290 mc13xxx_lock(mc13xxx);
292 mc13xxx_irq_ack(mc13xxx, MC13XXX_IRQ_RTCRST);
294 ret = mc13xxx_irq_request(mc13xxx, MC13XXX_IRQ_RTCRST,
295 mc13xxx_rtc_reset_handler, DRIVER_NAME, priv);
296 if (ret)
297 goto err_irq_request;
299 ret = mc13xxx_irq_request_nounmask(mc13xxx, MC13XXX_IRQ_TODA,
300 mc13xxx_rtc_alarm_handler, DRIVER_NAME, priv);
301 if (ret)
302 goto err_irq_request;
304 mc13xxx_unlock(mc13xxx);
306 priv->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
307 &mc13xxx_rtc_ops, THIS_MODULE);
309 return 0;
311 err_irq_request:
312 mc13xxx_irq_free(mc13xxx, MC13XXX_IRQ_TODA, priv);
313 mc13xxx_irq_free(mc13xxx, MC13XXX_IRQ_RTCRST, priv);
315 mc13xxx_unlock(mc13xxx);
317 return ret;
320 static int mc13xxx_rtc_remove(struct platform_device *pdev)
322 struct mc13xxx_rtc *priv = platform_get_drvdata(pdev);
324 mc13xxx_lock(priv->mc13xxx);
326 mc13xxx_irq_free(priv->mc13xxx, MC13XXX_IRQ_TODA, priv);
327 mc13xxx_irq_free(priv->mc13xxx, MC13XXX_IRQ_RTCRST, priv);
329 mc13xxx_unlock(priv->mc13xxx);
331 return 0;
334 static const struct platform_device_id mc13xxx_rtc_idtable[] = {
336 .name = "mc13783-rtc",
337 }, {
338 .name = "mc13892-rtc",
339 }, {
340 .name = "mc34708-rtc",
342 { /* sentinel */ }
344 MODULE_DEVICE_TABLE(platform, mc13xxx_rtc_idtable);
346 static struct platform_driver mc13xxx_rtc_driver = {
347 .id_table = mc13xxx_rtc_idtable,
348 .remove = mc13xxx_rtc_remove,
349 .driver = {
350 .name = DRIVER_NAME,
354 module_platform_driver_probe(mc13xxx_rtc_driver, &mc13xxx_rtc_probe);
356 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
357 MODULE_DESCRIPTION("RTC driver for Freescale MC13XXX PMIC");
358 MODULE_LICENSE("GPL v2");