blob: 675bfb51536722bbea5d703146a9972bb4be5d30 [file] [log] [blame]
Uwe Kleine-König43299f2852009-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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Uwe Kleine-König43299f2852009-12-15 16:46:09 -080017#include <linux/rtc.h>
18
19#define DRIVER_NAME "mc13783-rtc"
20
21#define MC13783_RTCTOD 20
22#define MC13783_RTCTODA 21
23#define MC13783_RTCDAY 22
24#define MC13783_RTCDAYA 23
25
26struct mc13783_rtc {
27 struct rtc_device *rtc;
28 struct mc13783 *mc13783;
29 int valid;
30};
31
Uwe Kleine-König94073512010-03-05 13:44:30 -080032static int mc13783_rtc_irq_enable_unlocked(struct device *dev,
33 unsigned int enabled, int irq)
34{
35 struct mc13783_rtc *priv = dev_get_drvdata(dev);
36 int (*func)(struct mc13783 *mc13783, int irq);
37
38 if (!priv->valid)
39 return -ENODATA;
40
41 func = enabled ? mc13783_irq_unmask : mc13783_irq_mask;
42 return func(priv->mc13783, irq);
43}
44
45static int mc13783_rtc_irq_enable(struct device *dev,
46 unsigned int enabled, int irq)
47{
48 struct mc13783_rtc *priv = dev_get_drvdata(dev);
49 int ret;
50
51 mc13783_lock(priv->mc13783);
52
53 ret = mc13783_rtc_irq_enable_unlocked(dev, enabled, irq);
54
55 mc13783_unlock(priv->mc13783);
56
57 return ret;
58}
59
Uwe Kleine-König43299f2852009-12-15 16:46:09 -080060static int mc13783_rtc_read_time(struct device *dev, struct rtc_time *tm)
61{
62 struct mc13783_rtc *priv = dev_get_drvdata(dev);
63 unsigned int seconds, days1, days2;
64 unsigned long s1970;
65 int ret;
66
67 mc13783_lock(priv->mc13783);
68
69 if (!priv->valid) {
70 ret = -ENODATA;
71 goto out;
72 }
73
74 ret = mc13783_reg_read(priv->mc13783, MC13783_RTCDAY, &days1);
75 if (unlikely(ret))
76 goto out;
77
78 ret = mc13783_reg_read(priv->mc13783, MC13783_RTCTOD, &seconds);
79 if (unlikely(ret))
80 goto out;
81
82 ret = mc13783_reg_read(priv->mc13783, MC13783_RTCDAY, &days2);
83out:
84 mc13783_unlock(priv->mc13783);
85
86 if (ret)
87 return ret;
88
89 if (days2 == days1 + 1) {
90 if (seconds >= 86400 / 2)
91 days2 = days1;
92 else
93 days1 = days2;
94 }
95
96 if (days1 != days2)
97 return -EIO;
98
99 s1970 = days1 * 86400 + seconds;
100
101 rtc_time_to_tm(s1970, tm);
102
103 return rtc_valid_tm(tm);
104}
105
106static int mc13783_rtc_set_mmss(struct device *dev, unsigned long secs)
107{
108 struct mc13783_rtc *priv = dev_get_drvdata(dev);
109 unsigned int seconds, days;
Uwe Kleine-König94073512010-03-05 13:44:30 -0800110 unsigned int alarmseconds;
Uwe Kleine-König43299f2852009-12-15 16:46:09 -0800111 int ret;
112
113 seconds = secs % 86400;
114 days = secs / 86400;
115
116 mc13783_lock(priv->mc13783);
117
118 /*
Uwe Kleine-König94073512010-03-05 13:44:30 -0800119 * temporarily invalidate alarm to prevent triggering it when the day is
120 * already updated while the time isn't yet.
121 */
122 ret = mc13783_reg_read(priv->mc13783, MC13783_RTCTODA, &alarmseconds);
123 if (unlikely(ret))
124 goto out;
125
126 if (alarmseconds < 86400) {
127 ret = mc13783_reg_write(priv->mc13783,
128 MC13783_RTCTODA, 0x1ffff);
129 if (unlikely(ret))
130 goto out;
131 }
132
133 /*
134 * write seconds=0 to prevent a day switch between writing days
Uwe Kleine-König43299f2852009-12-15 16:46:09 -0800135 * and seconds below
136 */
137 ret = mc13783_reg_write(priv->mc13783, MC13783_RTCTOD, 0);
138 if (unlikely(ret))
139 goto out;
140
141 ret = mc13783_reg_write(priv->mc13783, MC13783_RTCDAY, days);
142 if (unlikely(ret))
143 goto out;
144
145 ret = mc13783_reg_write(priv->mc13783, MC13783_RTCTOD, seconds);
146 if (unlikely(ret))
147 goto out;
148
Uwe Kleine-König94073512010-03-05 13:44:30 -0800149 /* restore alarm */
150 if (alarmseconds < 86400) {
151 ret = mc13783_reg_write(priv->mc13783,
152 MC13783_RTCTODA, alarmseconds);
153 if (unlikely(ret))
154 goto out;
155 }
156
Uwe Kleine-König4dd4dd62010-03-05 13:44:28 -0800157 ret = mc13783_irq_ack(priv->mc13783, MC13783_IRQ_RTCRST);
Uwe Kleine-König43299f2852009-12-15 16:46:09 -0800158 if (unlikely(ret))
159 goto out;
160
Uwe Kleine-König4dd4dd62010-03-05 13:44:28 -0800161 ret = mc13783_irq_unmask(priv->mc13783, MC13783_IRQ_RTCRST);
Uwe Kleine-König43299f2852009-12-15 16:46:09 -0800162out:
163 priv->valid = !ret;
164
165 mc13783_unlock(priv->mc13783);
166
167 return ret;
168}
169
Uwe Kleine-König94073512010-03-05 13:44:30 -0800170static int mc13783_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
171{
172 struct mc13783_rtc *priv = dev_get_drvdata(dev);
173 unsigned seconds, days;
174 unsigned long s1970;
175 int enabled, pending;
176 int ret;
177
178 mc13783_lock(priv->mc13783);
179
180 ret = mc13783_reg_read(priv->mc13783, MC13783_RTCTODA, &seconds);
181 if (unlikely(ret))
182 goto out;
183 if (seconds >= 86400) {
184 ret = -ENODATA;
185 goto out;
186 }
187
188 ret = mc13783_reg_read(priv->mc13783, MC13783_RTCDAY, &days);
189 if (unlikely(ret))
190 goto out;
191
192 ret = mc13783_irq_status(priv->mc13783, MC13783_IRQ_TODA,
193 &enabled, &pending);
194
195out:
196 mc13783_unlock(priv->mc13783);
197
198 if (ret)
199 return ret;
200
201 alarm->enabled = enabled;
202 alarm->pending = pending;
203
204 s1970 = days * 86400 + seconds;
205
206 rtc_time_to_tm(s1970, &alarm->time);
207 dev_dbg(dev, "%s: %lu\n", __func__, s1970);
208
209 return 0;
210}
211
212static int mc13783_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
213{
214 struct mc13783_rtc *priv = dev_get_drvdata(dev);
215 unsigned long s1970;
216 unsigned seconds, days;
217 int ret;
218
219 mc13783_lock(priv->mc13783);
220
221 /* disable alarm to prevent false triggering */
222 ret = mc13783_reg_write(priv->mc13783, MC13783_RTCTODA, 0x1ffff);
223 if (unlikely(ret))
224 goto out;
225
226 ret = mc13783_irq_ack(priv->mc13783, MC13783_IRQ_TODA);
227 if (unlikely(ret))
228 goto out;
229
230 ret = rtc_tm_to_time(&alarm->time, &s1970);
231 if (unlikely(ret))
232 goto out;
233
234 dev_dbg(dev, "%s: o%2.s %lu\n", __func__, alarm->enabled ? "n" : "ff",
235 s1970);
236
237 ret = mc13783_rtc_irq_enable_unlocked(dev, alarm->enabled,
238 MC13783_IRQ_TODA);
239 if (unlikely(ret))
240 goto out;
241
242 seconds = s1970 % 86400;
243 days = s1970 / 86400;
244
245 ret = mc13783_reg_write(priv->mc13783, MC13783_RTCDAYA, days);
246 if (unlikely(ret))
247 goto out;
248
249 ret = mc13783_reg_write(priv->mc13783, MC13783_RTCTODA, seconds);
250
251out:
252 mc13783_unlock(priv->mc13783);
253
254 return ret;
255}
256
257static irqreturn_t mc13783_rtc_alarm_handler(int irq, void *dev)
258{
259 struct mc13783_rtc *priv = dev;
260 struct mc13783 *mc13783 = priv->mc13783;
261
262 dev_dbg(&priv->rtc->dev, "Alarm\n");
263
264 rtc_update_irq(priv->rtc, 1, RTC_IRQF | RTC_AF);
265
266 mc13783_irq_ack(mc13783, irq);
267
268 return IRQ_HANDLED;
269}
270
Uwe Kleine-König43299f2852009-12-15 16:46:09 -0800271static irqreturn_t mc13783_rtc_update_handler(int irq, void *dev)
272{
273 struct mc13783_rtc *priv = dev;
274 struct mc13783 *mc13783 = priv->mc13783;
275
276 dev_dbg(&priv->rtc->dev, "1HZ\n");
277
278 rtc_update_irq(priv->rtc, 1, RTC_IRQF | RTC_UF);
279
Uwe Kleine-König4dd4dd62010-03-05 13:44:28 -0800280 mc13783_irq_ack(mc13783, irq);
Uwe Kleine-König43299f2852009-12-15 16:46:09 -0800281
282 return IRQ_HANDLED;
283}
284
285static int mc13783_rtc_update_irq_enable(struct device *dev,
286 unsigned int enabled)
287{
Uwe Kleine-König94073512010-03-05 13:44:30 -0800288 return mc13783_rtc_irq_enable(dev, enabled, MC13783_IRQ_1HZ);
289}
Uwe Kleine-König43299f2852009-12-15 16:46:09 -0800290
Uwe Kleine-König94073512010-03-05 13:44:30 -0800291static int mc13783_rtc_alarm_irq_enable(struct device *dev,
292 unsigned int enabled)
293{
294 return mc13783_rtc_irq_enable(dev, enabled, MC13783_IRQ_TODA);
Uwe Kleine-König43299f2852009-12-15 16:46:09 -0800295}
296
297static const struct rtc_class_ops mc13783_rtc_ops = {
298 .read_time = mc13783_rtc_read_time,
299 .set_mmss = mc13783_rtc_set_mmss,
Uwe Kleine-König94073512010-03-05 13:44:30 -0800300 .read_alarm = mc13783_rtc_read_alarm,
301 .set_alarm = mc13783_rtc_set_alarm,
302 .alarm_irq_enable = mc13783_rtc_alarm_irq_enable,
Uwe Kleine-König43299f2852009-12-15 16:46:09 -0800303 .update_irq_enable = mc13783_rtc_update_irq_enable,
304};
305
306static irqreturn_t mc13783_rtc_reset_handler(int irq, void *dev)
307{
308 struct mc13783_rtc *priv = dev;
309 struct mc13783 *mc13783 = priv->mc13783;
310
311 dev_dbg(&priv->rtc->dev, "RTCRST\n");
312 priv->valid = 0;
313
Uwe Kleine-König4dd4dd62010-03-05 13:44:28 -0800314 mc13783_irq_mask(mc13783, irq);
Uwe Kleine-König43299f2852009-12-15 16:46:09 -0800315
316 return IRQ_HANDLED;
317}
318
319static int __devinit mc13783_rtc_probe(struct platform_device *pdev)
320{
321 int ret;
322 struct mc13783_rtc *priv;
Uwe Kleine-König05731b92010-03-23 13:35:13 -0700323 struct mc13783 *mc13783;
Uwe Kleine-König4c014e82010-03-05 13:44:30 -0800324 int rtcrst_pending;
Uwe Kleine-König43299f2852009-12-15 16:46:09 -0800325
326 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
327 if (!priv)
328 return -ENOMEM;
329
Uwe Kleine-König05731b92010-03-23 13:35:13 -0700330 mc13783 = dev_get_drvdata(pdev->dev.parent);
331 priv->mc13783 = mc13783;
332
Uwe Kleine-König43299f2852009-12-15 16:46:09 -0800333 platform_set_drvdata(pdev, priv);
334
Uwe Kleine-König05731b92010-03-23 13:35:13 -0700335 mc13783_lock(mc13783);
Uwe Kleine-König43299f2852009-12-15 16:46:09 -0800336
Uwe Kleine-König05731b92010-03-23 13:35:13 -0700337 ret = mc13783_irq_request(mc13783, MC13783_IRQ_RTCRST,
Uwe Kleine-König43299f2852009-12-15 16:46:09 -0800338 mc13783_rtc_reset_handler, DRIVER_NAME, priv);
339 if (ret)
340 goto err_reset_irq_request;
341
Uwe Kleine-König05731b92010-03-23 13:35:13 -0700342 ret = mc13783_irq_status(mc13783, MC13783_IRQ_RTCRST,
Uwe Kleine-König4c014e82010-03-05 13:44:30 -0800343 NULL, &rtcrst_pending);
344 if (ret)
345 goto err_reset_irq_status;
346
347 priv->valid = !rtcrst_pending;
348
Uwe Kleine-König05731b92010-03-23 13:35:13 -0700349 ret = mc13783_irq_request_nounmask(mc13783, MC13783_IRQ_1HZ,
Uwe Kleine-König43299f2852009-12-15 16:46:09 -0800350 mc13783_rtc_update_handler, DRIVER_NAME, priv);
351 if (ret)
352 goto err_update_irq_request;
353
Uwe Kleine-König05731b92010-03-23 13:35:13 -0700354 ret = mc13783_irq_request_nounmask(mc13783, MC13783_IRQ_TODA,
Uwe Kleine-König94073512010-03-05 13:44:30 -0800355 mc13783_rtc_alarm_handler, DRIVER_NAME, priv);
356 if (ret)
357 goto err_alarm_irq_request;
358
Uwe Kleine-König43299f2852009-12-15 16:46:09 -0800359 priv->rtc = rtc_device_register(pdev->name,
360 &pdev->dev, &mc13783_rtc_ops, THIS_MODULE);
Uwe Kleine-König43299f2852009-12-15 16:46:09 -0800361 if (IS_ERR(priv->rtc)) {
362 ret = PTR_ERR(priv->rtc);
363
Uwe Kleine-König05731b92010-03-23 13:35:13 -0700364 mc13783_irq_free(mc13783, MC13783_IRQ_TODA, priv);
Uwe Kleine-König94073512010-03-05 13:44:30 -0800365err_alarm_irq_request:
366
Uwe Kleine-König05731b92010-03-23 13:35:13 -0700367 mc13783_irq_free(mc13783, MC13783_IRQ_1HZ, priv);
Uwe Kleine-König43299f2852009-12-15 16:46:09 -0800368err_update_irq_request:
369
Uwe Kleine-König4c014e82010-03-05 13:44:30 -0800370err_reset_irq_status:
371
Uwe Kleine-König05731b92010-03-23 13:35:13 -0700372 mc13783_irq_free(mc13783, MC13783_IRQ_RTCRST, priv);
Uwe Kleine-König43299f2852009-12-15 16:46:09 -0800373err_reset_irq_request:
374
Uwe Kleine-König43299f2852009-12-15 16:46:09 -0800375 platform_set_drvdata(pdev, NULL);
376 kfree(priv);
377 }
378
Uwe Kleine-König05731b92010-03-23 13:35:13 -0700379 mc13783_unlock(mc13783);
Uwe Kleine-König4c014e82010-03-05 13:44:30 -0800380
Uwe Kleine-König43299f2852009-12-15 16:46:09 -0800381 return ret;
382}
383
384static int __devexit mc13783_rtc_remove(struct platform_device *pdev)
385{
386 struct mc13783_rtc *priv = platform_get_drvdata(pdev);
387
Uwe Kleine-König43299f2852009-12-15 16:46:09 -0800388 mc13783_lock(priv->mc13783);
389
Uwe Kleine-König4c014e82010-03-05 13:44:30 -0800390 rtc_device_unregister(priv->rtc);
391
Uwe Kleine-König94073512010-03-05 13:44:30 -0800392 mc13783_irq_free(priv->mc13783, MC13783_IRQ_TODA, priv);
Uwe Kleine-König43299f2852009-12-15 16:46:09 -0800393 mc13783_irq_free(priv->mc13783, MC13783_IRQ_1HZ, priv);
394 mc13783_irq_free(priv->mc13783, MC13783_IRQ_RTCRST, priv);
395
396 mc13783_unlock(priv->mc13783);
397
398 platform_set_drvdata(pdev, NULL);
399
400 kfree(priv);
401
402 return 0;
403}
404
405static struct platform_driver mc13783_rtc_driver = {
406 .remove = __devexit_p(mc13783_rtc_remove),
407 .driver = {
408 .name = DRIVER_NAME,
409 .owner = THIS_MODULE,
410 },
411};
412
413static int __init mc13783_rtc_init(void)
414{
415 return platform_driver_probe(&mc13783_rtc_driver, &mc13783_rtc_probe);
416}
417module_init(mc13783_rtc_init);
418
419static void __exit mc13783_rtc_exit(void)
420{
421 platform_driver_unregister(&mc13783_rtc_driver);
422}
423module_exit(mc13783_rtc_exit);
424
425MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
426MODULE_DESCRIPTION("RTC driver for Freescale MC13783 PMIC");
427MODULE_LICENSE("GPL v2");
428MODULE_ALIAS("platform:" DRIVER_NAME);