blob: d60c81b7b693c9c26afa236cf85abafd1b6e2ec2 [file] [log] [blame]
Uwe Kleine-König43299f22009-12-15 16:46:09 -08001/*
2 * Real Time Clock driver for Freescale MC13783 PMIC
3 *
4 * (C) 2009 Sascha Hauer, Pengutronix
5 * (C) 2009 Uwe Kleine-Koenig, Pengutronix
6 *
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.
10 */
11
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>
17
18#define DRIVER_NAME "mc13783-rtc"
19
20#define MC13783_RTCTOD 20
21#define MC13783_RTCTODA 21
22#define MC13783_RTCDAY 22
23#define MC13783_RTCDAYA 23
24
25struct mc13783_rtc {
26 struct rtc_device *rtc;
27 struct mc13783 *mc13783;
28 int valid;
29};
30
Uwe Kleine-König94073512010-03-05 13:44:30 -080031static int mc13783_rtc_irq_enable_unlocked(struct device *dev,
32 unsigned int enabled, int irq)
33{
34 struct mc13783_rtc *priv = dev_get_drvdata(dev);
35 int (*func)(struct mc13783 *mc13783, int irq);
36
37 if (!priv->valid)
38 return -ENODATA;
39
40 func = enabled ? mc13783_irq_unmask : mc13783_irq_mask;
41 return func(priv->mc13783, irq);
42}
43
44static int mc13783_rtc_irq_enable(struct device *dev,
45 unsigned int enabled, int irq)
46{
47 struct mc13783_rtc *priv = dev_get_drvdata(dev);
48 int ret;
49
50 mc13783_lock(priv->mc13783);
51
52 ret = mc13783_rtc_irq_enable_unlocked(dev, enabled, irq);
53
54 mc13783_unlock(priv->mc13783);
55
56 return ret;
57}
58
Uwe Kleine-König43299f22009-12-15 16:46:09 -080059static int mc13783_rtc_read_time(struct device *dev, struct rtc_time *tm)
60{
61 struct mc13783_rtc *priv = dev_get_drvdata(dev);
62 unsigned int seconds, days1, days2;
63 unsigned long s1970;
64 int ret;
65
66 mc13783_lock(priv->mc13783);
67
68 if (!priv->valid) {
69 ret = -ENODATA;
70 goto out;
71 }
72
73 ret = mc13783_reg_read(priv->mc13783, MC13783_RTCDAY, &days1);
74 if (unlikely(ret))
75 goto out;
76
77 ret = mc13783_reg_read(priv->mc13783, MC13783_RTCTOD, &seconds);
78 if (unlikely(ret))
79 goto out;
80
81 ret = mc13783_reg_read(priv->mc13783, MC13783_RTCDAY, &days2);
82out:
83 mc13783_unlock(priv->mc13783);
84
85 if (ret)
86 return ret;
87
88 if (days2 == days1 + 1) {
89 if (seconds >= 86400 / 2)
90 days2 = days1;
91 else
92 days1 = days2;
93 }
94
95 if (days1 != days2)
96 return -EIO;
97
98 s1970 = days1 * 86400 + seconds;
99
100 rtc_time_to_tm(s1970, tm);
101
102 return rtc_valid_tm(tm);
103}
104
105static int mc13783_rtc_set_mmss(struct device *dev, unsigned long secs)
106{
107 struct mc13783_rtc *priv = dev_get_drvdata(dev);
108 unsigned int seconds, days;
Uwe Kleine-König94073512010-03-05 13:44:30 -0800109 unsigned int alarmseconds;
Uwe Kleine-König43299f22009-12-15 16:46:09 -0800110 int ret;
111
112 seconds = secs % 86400;
113 days = secs / 86400;
114
115 mc13783_lock(priv->mc13783);
116
117 /*
Uwe Kleine-König94073512010-03-05 13:44:30 -0800118 * temporarily invalidate alarm to prevent triggering it when the day is
119 * already updated while the time isn't yet.
120 */
121 ret = mc13783_reg_read(priv->mc13783, MC13783_RTCTODA, &alarmseconds);
122 if (unlikely(ret))
123 goto out;
124
125 if (alarmseconds < 86400) {
126 ret = mc13783_reg_write(priv->mc13783,
127 MC13783_RTCTODA, 0x1ffff);
128 if (unlikely(ret))
129 goto out;
130 }
131
132 /*
133 * write seconds=0 to prevent a day switch between writing days
Uwe Kleine-König43299f22009-12-15 16:46:09 -0800134 * and seconds below
135 */
136 ret = mc13783_reg_write(priv->mc13783, MC13783_RTCTOD, 0);
137 if (unlikely(ret))
138 goto out;
139
140 ret = mc13783_reg_write(priv->mc13783, MC13783_RTCDAY, days);
141 if (unlikely(ret))
142 goto out;
143
144 ret = mc13783_reg_write(priv->mc13783, MC13783_RTCTOD, seconds);
145 if (unlikely(ret))
146 goto out;
147
Uwe Kleine-König94073512010-03-05 13:44:30 -0800148 /* restore alarm */
149 if (alarmseconds < 86400) {
150 ret = mc13783_reg_write(priv->mc13783,
151 MC13783_RTCTODA, alarmseconds);
152 if (unlikely(ret))
153 goto out;
154 }
155
Uwe Kleine-König4dd4dd62010-03-05 13:44:28 -0800156 ret = mc13783_irq_ack(priv->mc13783, MC13783_IRQ_RTCRST);
Uwe Kleine-König43299f22009-12-15 16:46:09 -0800157 if (unlikely(ret))
158 goto out;
159
Uwe Kleine-König4dd4dd62010-03-05 13:44:28 -0800160 ret = mc13783_irq_unmask(priv->mc13783, MC13783_IRQ_RTCRST);
Uwe Kleine-König43299f22009-12-15 16:46:09 -0800161out:
162 priv->valid = !ret;
163
164 mc13783_unlock(priv->mc13783);
165
166 return ret;
167}
168
Uwe Kleine-König94073512010-03-05 13:44:30 -0800169static int mc13783_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
170{
171 struct mc13783_rtc *priv = dev_get_drvdata(dev);
172 unsigned seconds, days;
173 unsigned long s1970;
174 int enabled, pending;
175 int ret;
176
177 mc13783_lock(priv->mc13783);
178
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;
185 }
186
187 ret = mc13783_reg_read(priv->mc13783, MC13783_RTCDAY, &days);
188 if (unlikely(ret))
189 goto out;
190
191 ret = mc13783_irq_status(priv->mc13783, MC13783_IRQ_TODA,
192 &enabled, &pending);
193
194out:
195 mc13783_unlock(priv->mc13783);
196
197 if (ret)
198 return ret;
199
200 alarm->enabled = enabled;
201 alarm->pending = pending;
202
203 s1970 = days * 86400 + seconds;
204
205 rtc_time_to_tm(s1970, &alarm->time);
206 dev_dbg(dev, "%s: %lu\n", __func__, s1970);
207
208 return 0;
209}
210
211static int mc13783_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
212{
213 struct mc13783_rtc *priv = dev_get_drvdata(dev);
214 unsigned long s1970;
215 unsigned seconds, days;
216 int ret;
217
218 mc13783_lock(priv->mc13783);
219
220 /* disable alarm to prevent false triggering */
221 ret = mc13783_reg_write(priv->mc13783, MC13783_RTCTODA, 0x1ffff);
222 if (unlikely(ret))
223 goto out;
224
225 ret = mc13783_irq_ack(priv->mc13783, MC13783_IRQ_TODA);
226 if (unlikely(ret))
227 goto out;
228
229 ret = rtc_tm_to_time(&alarm->time, &s1970);
230 if (unlikely(ret))
231 goto out;
232
233 dev_dbg(dev, "%s: o%2.s %lu\n", __func__, alarm->enabled ? "n" : "ff",
234 s1970);
235
236 ret = mc13783_rtc_irq_enable_unlocked(dev, alarm->enabled,
237 MC13783_IRQ_TODA);
238 if (unlikely(ret))
239 goto out;
240
241 seconds = s1970 % 86400;
242 days = s1970 / 86400;
243
244 ret = mc13783_reg_write(priv->mc13783, MC13783_RTCDAYA, days);
245 if (unlikely(ret))
246 goto out;
247
248 ret = mc13783_reg_write(priv->mc13783, MC13783_RTCTODA, seconds);
249
250out:
251 mc13783_unlock(priv->mc13783);
252
253 return ret;
254}
255
256static irqreturn_t mc13783_rtc_alarm_handler(int irq, void *dev)
257{
258 struct mc13783_rtc *priv = dev;
259 struct mc13783 *mc13783 = priv->mc13783;
260
261 dev_dbg(&priv->rtc->dev, "Alarm\n");
262
263 rtc_update_irq(priv->rtc, 1, RTC_IRQF | RTC_AF);
264
265 mc13783_irq_ack(mc13783, irq);
266
267 return IRQ_HANDLED;
268}
269
Uwe Kleine-König43299f22009-12-15 16:46:09 -0800270static irqreturn_t mc13783_rtc_update_handler(int irq, void *dev)
271{
272 struct mc13783_rtc *priv = dev;
273 struct mc13783 *mc13783 = priv->mc13783;
274
275 dev_dbg(&priv->rtc->dev, "1HZ\n");
276
277 rtc_update_irq(priv->rtc, 1, RTC_IRQF | RTC_UF);
278
Uwe Kleine-König4dd4dd62010-03-05 13:44:28 -0800279 mc13783_irq_ack(mc13783, irq);
Uwe Kleine-König43299f22009-12-15 16:46:09 -0800280
281 return IRQ_HANDLED;
282}
283
284static int mc13783_rtc_update_irq_enable(struct device *dev,
285 unsigned int enabled)
286{
Uwe Kleine-König94073512010-03-05 13:44:30 -0800287 return mc13783_rtc_irq_enable(dev, enabled, MC13783_IRQ_1HZ);
288}
Uwe Kleine-König43299f22009-12-15 16:46:09 -0800289
Uwe Kleine-König94073512010-03-05 13:44:30 -0800290static int mc13783_rtc_alarm_irq_enable(struct device *dev,
291 unsigned int enabled)
292{
293 return mc13783_rtc_irq_enable(dev, enabled, MC13783_IRQ_TODA);
Uwe Kleine-König43299f22009-12-15 16:46:09 -0800294}
295
296static const struct rtc_class_ops mc13783_rtc_ops = {
297 .read_time = mc13783_rtc_read_time,
298 .set_mmss = mc13783_rtc_set_mmss,
Uwe Kleine-König94073512010-03-05 13:44:30 -0800299 .read_alarm = mc13783_rtc_read_alarm,
300 .set_alarm = mc13783_rtc_set_alarm,
301 .alarm_irq_enable = mc13783_rtc_alarm_irq_enable,
Uwe Kleine-König43299f22009-12-15 16:46:09 -0800302 .update_irq_enable = mc13783_rtc_update_irq_enable,
303};
304
305static irqreturn_t mc13783_rtc_reset_handler(int irq, void *dev)
306{
307 struct mc13783_rtc *priv = dev;
308 struct mc13783 *mc13783 = priv->mc13783;
309
310 dev_dbg(&priv->rtc->dev, "RTCRST\n");
311 priv->valid = 0;
312
Uwe Kleine-König4dd4dd62010-03-05 13:44:28 -0800313 mc13783_irq_mask(mc13783, irq);
Uwe Kleine-König43299f22009-12-15 16:46:09 -0800314
315 return IRQ_HANDLED;
316}
317
318static int __devinit mc13783_rtc_probe(struct platform_device *pdev)
319{
320 int ret;
321 struct mc13783_rtc *priv;
Uwe Kleine-König4c014e82010-03-05 13:44:30 -0800322 int rtcrst_pending;
Uwe Kleine-König43299f22009-12-15 16:46:09 -0800323
324 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
325 if (!priv)
326 return -ENOMEM;
327
328 priv->mc13783 = dev_get_drvdata(pdev->dev.parent);
329 platform_set_drvdata(pdev, priv);
330
Uwe Kleine-König43299f22009-12-15 16:46:09 -0800331 mc13783_lock(priv->mc13783);
332
333 ret = mc13783_irq_request(priv->mc13783, MC13783_IRQ_RTCRST,
334 mc13783_rtc_reset_handler, DRIVER_NAME, priv);
335 if (ret)
336 goto err_reset_irq_request;
337
Uwe Kleine-König4c014e82010-03-05 13:44:30 -0800338 ret = mc13783_irq_status(priv->mc13783, MC13783_IRQ_RTCRST,
339 NULL, &rtcrst_pending);
340 if (ret)
341 goto err_reset_irq_status;
342
343 priv->valid = !rtcrst_pending;
344
Uwe Kleine-König43299f22009-12-15 16:46:09 -0800345 ret = mc13783_irq_request_nounmask(priv->mc13783, MC13783_IRQ_1HZ,
346 mc13783_rtc_update_handler, DRIVER_NAME, priv);
347 if (ret)
348 goto err_update_irq_request;
349
Uwe Kleine-König94073512010-03-05 13:44:30 -0800350 ret = mc13783_irq_request_nounmask(priv->mc13783, MC13783_IRQ_TODA,
351 mc13783_rtc_alarm_handler, DRIVER_NAME, priv);
352 if (ret)
353 goto err_alarm_irq_request;
354
Uwe Kleine-König43299f22009-12-15 16:46:09 -0800355 priv->rtc = rtc_device_register(pdev->name,
356 &pdev->dev, &mc13783_rtc_ops, THIS_MODULE);
Uwe Kleine-König43299f22009-12-15 16:46:09 -0800357 if (IS_ERR(priv->rtc)) {
358 ret = PTR_ERR(priv->rtc);
359
Uwe Kleine-König94073512010-03-05 13:44:30 -0800360 mc13783_irq_free(priv->mc13783, MC13783_IRQ_TODA, priv);
361err_alarm_irq_request:
362
Uwe Kleine-König43299f22009-12-15 16:46:09 -0800363 mc13783_irq_free(priv->mc13783, MC13783_IRQ_1HZ, priv);
364err_update_irq_request:
365
Uwe Kleine-König4c014e82010-03-05 13:44:30 -0800366err_reset_irq_status:
367
Uwe Kleine-König43299f22009-12-15 16:46:09 -0800368 mc13783_irq_free(priv->mc13783, MC13783_IRQ_RTCRST, priv);
369err_reset_irq_request:
370
Uwe Kleine-König43299f22009-12-15 16:46:09 -0800371 platform_set_drvdata(pdev, NULL);
372 kfree(priv);
373 }
374
Uwe Kleine-König4c014e82010-03-05 13:44:30 -0800375 mc13783_unlock(priv->mc13783);
376
Uwe Kleine-König43299f22009-12-15 16:46:09 -0800377 return ret;
378}
379
380static int __devexit mc13783_rtc_remove(struct platform_device *pdev)
381{
382 struct mc13783_rtc *priv = platform_get_drvdata(pdev);
383
Uwe Kleine-König43299f22009-12-15 16:46:09 -0800384 mc13783_lock(priv->mc13783);
385
Uwe Kleine-König4c014e82010-03-05 13:44:30 -0800386 rtc_device_unregister(priv->rtc);
387
Uwe Kleine-König94073512010-03-05 13:44:30 -0800388 mc13783_irq_free(priv->mc13783, MC13783_IRQ_TODA, priv);
Uwe Kleine-König43299f22009-12-15 16:46:09 -0800389 mc13783_irq_free(priv->mc13783, MC13783_IRQ_1HZ, priv);
390 mc13783_irq_free(priv->mc13783, MC13783_IRQ_RTCRST, priv);
391
392 mc13783_unlock(priv->mc13783);
393
394 platform_set_drvdata(pdev, NULL);
395
396 kfree(priv);
397
398 return 0;
399}
400
401static struct platform_driver mc13783_rtc_driver = {
402 .remove = __devexit_p(mc13783_rtc_remove),
403 .driver = {
404 .name = DRIVER_NAME,
405 .owner = THIS_MODULE,
406 },
407};
408
409static int __init mc13783_rtc_init(void)
410{
411 return platform_driver_probe(&mc13783_rtc_driver, &mc13783_rtc_probe);
412}
413module_init(mc13783_rtc_init);
414
415static void __exit mc13783_rtc_exit(void)
416{
417 platform_driver_unregister(&mc13783_rtc_driver);
418}
419module_exit(mc13783_rtc_exit);
420
421MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
422MODULE_DESCRIPTION("RTC driver for Freescale MC13783 PMIC");
423MODULE_LICENSE("GPL v2");
424MODULE_ALIAS("platform:" DRIVER_NAME);