code style scripts/checkpatch.pl (linux-3.9-rc1) formatting
[linux-2.6.32.60-moxart.git] / drivers / rtc / rtc-wm831x.c
blobdaefe66f3501eeb4eb038ae91a829dfda04979d6
1 /*
2 * Real Time Clock driver for Wolfson Microelectronics WM831x
4 * Copyright (C) 2009 Wolfson Microelectronics PLC.
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/time.h>
18 #include <linux/rtc.h>
19 #include <linux/bcd.h>
20 #include <linux/interrupt.h>
21 #include <linux/ioctl.h>
22 #include <linux/completion.h>
23 #include <linux/mfd/wm831x/core.h>
24 #include <linux/delay.h>
25 #include <linux/platform_device.h>
26 #include <linux/random.h>
29 * R16416 (0x4020) - RTC Write Counter
31 #define WM831X_RTC_WR_CNT_MASK 0xFFFF /* RTC_WR_CNT - [15:0] */
32 #define WM831X_RTC_WR_CNT_SHIFT 0 /* RTC_WR_CNT - [15:0] */
33 #define WM831X_RTC_WR_CNT_WIDTH 16 /* RTC_WR_CNT - [15:0] */
36 * R16417 (0x4021) - RTC Time 1
38 #define WM831X_RTC_TIME_MASK 0xFFFF /* RTC_TIME - [15:0] */
39 #define WM831X_RTC_TIME_SHIFT 0 /* RTC_TIME - [15:0] */
40 #define WM831X_RTC_TIME_WIDTH 16 /* RTC_TIME - [15:0] */
43 * R16418 (0x4022) - RTC Time 2
45 #define WM831X_RTC_TIME_MASK 0xFFFF /* RTC_TIME - [15:0] */
46 #define WM831X_RTC_TIME_SHIFT 0 /* RTC_TIME - [15:0] */
47 #define WM831X_RTC_TIME_WIDTH 16 /* RTC_TIME - [15:0] */
50 * R16419 (0x4023) - RTC Alarm 1
52 #define WM831X_RTC_ALM_MASK 0xFFFF /* RTC_ALM - [15:0] */
53 #define WM831X_RTC_ALM_SHIFT 0 /* RTC_ALM - [15:0] */
54 #define WM831X_RTC_ALM_WIDTH 16 /* RTC_ALM - [15:0] */
57 * R16420 (0x4024) - RTC Alarm 2
59 #define WM831X_RTC_ALM_MASK 0xFFFF /* RTC_ALM - [15:0] */
60 #define WM831X_RTC_ALM_SHIFT 0 /* RTC_ALM - [15:0] */
61 #define WM831X_RTC_ALM_WIDTH 16 /* RTC_ALM - [15:0] */
64 * R16421 (0x4025) - RTC Control
66 #define WM831X_RTC_VALID 0x8000 /* RTC_VALID */
67 #define WM831X_RTC_VALID_MASK 0x8000 /* RTC_VALID */
68 #define WM831X_RTC_VALID_SHIFT 15 /* RTC_VALID */
69 #define WM831X_RTC_VALID_WIDTH 1 /* RTC_VALID */
70 #define WM831X_RTC_SYNC_BUSY 0x4000 /* RTC_SYNC_BUSY */
71 #define WM831X_RTC_SYNC_BUSY_MASK 0x4000 /* RTC_SYNC_BUSY */
72 #define WM831X_RTC_SYNC_BUSY_SHIFT 14 /* RTC_SYNC_BUSY */
73 #define WM831X_RTC_SYNC_BUSY_WIDTH 1 /* RTC_SYNC_BUSY */
74 #define WM831X_RTC_ALM_ENA 0x0400 /* RTC_ALM_ENA */
75 #define WM831X_RTC_ALM_ENA_MASK 0x0400 /* RTC_ALM_ENA */
76 #define WM831X_RTC_ALM_ENA_SHIFT 10 /* RTC_ALM_ENA */
77 #define WM831X_RTC_ALM_ENA_WIDTH 1 /* RTC_ALM_ENA */
78 #define WM831X_RTC_PINT_FREQ_MASK 0x0070 /* RTC_PINT_FREQ - [6:4] */
79 #define WM831X_RTC_PINT_FREQ_SHIFT 4 /* RTC_PINT_FREQ - [6:4] */
80 #define WM831X_RTC_PINT_FREQ_WIDTH 3 /* RTC_PINT_FREQ - [6:4] */
83 * R16422 (0x4026) - RTC Trim
85 #define WM831X_RTC_TRIM_MASK 0x03FF /* RTC_TRIM - [9:0] */
86 #define WM831X_RTC_TRIM_SHIFT 0 /* RTC_TRIM - [9:0] */
87 #define WM831X_RTC_TRIM_WIDTH 10 /* RTC_TRIM - [9:0] */
89 #define WM831X_SET_TIME_RETRIES 5
90 #define WM831X_GET_TIME_RETRIES 5
92 struct wm831x_rtc {
93 struct wm831x *wm831x;
94 struct rtc_device *rtc;
95 unsigned int alarm_enabled:1;
98 static void wm831x_rtc_add_randomness(struct wm831x *wm831x)
100 int ret;
101 u16 reg;
104 * The write counter contains a pseudo-random number which is
105 * regenerated every time we set the RTC so it should be a
106 * useful per-system source of entropy.
108 ret = wm831x_reg_read(wm831x, WM831X_RTC_WRITE_COUNTER);
109 if (ret >= 0) {
110 reg = ret;
111 add_device_randomness(&reg, sizeof(reg));
112 } else {
113 dev_warn(wm831x->dev, "Failed to read RTC write counter: %d\n",
114 ret);
119 * Read current time and date in RTC
121 static int wm831x_rtc_readtime(struct device *dev, struct rtc_time *tm)
123 struct wm831x_rtc *wm831x_rtc = dev_get_drvdata(dev);
124 struct wm831x *wm831x = wm831x_rtc->wm831x;
125 u16 time1[2], time2[2];
126 int ret;
127 int count = 0;
129 /* Has the RTC been programmed? */
130 ret = wm831x_reg_read(wm831x, WM831X_RTC_CONTROL);
131 if (ret < 0) {
132 dev_err(dev, "Failed to read RTC control: %d\n", ret);
133 return ret;
135 if (!(ret & WM831X_RTC_VALID)) {
136 dev_dbg(dev, "RTC not yet configured\n");
137 return -EINVAL;
140 /* Read twice to make sure we don't read a corrupt, partially
141 * incremented, value.
143 do {
144 ret = wm831x_bulk_read(wm831x, WM831X_RTC_TIME_1,
145 2, time1);
146 if (ret != 0)
147 continue;
149 ret = wm831x_bulk_read(wm831x, WM831X_RTC_TIME_1,
150 2, time2);
151 if (ret != 0)
152 continue;
154 if (memcmp(time1, time2, sizeof(time1)) == 0) {
155 u32 time = (time1[0] << 16) | time1[1];
157 rtc_time_to_tm(time, tm);
158 return rtc_valid_tm(tm);
161 } while (++count < WM831X_GET_TIME_RETRIES);
163 dev_err(dev, "Timed out reading current time\n");
165 return -EIO;
169 * Set current time and date in RTC
171 static int wm831x_rtc_set_mmss(struct device *dev, unsigned long time)
173 struct wm831x_rtc *wm831x_rtc = dev_get_drvdata(dev);
174 struct wm831x *wm831x = wm831x_rtc->wm831x;
175 struct rtc_time new_tm;
176 unsigned long new_time;
177 int ret;
178 int count = 0;
180 ret = wm831x_reg_write(wm831x, WM831X_RTC_TIME_1,
181 (time >> 16) & 0xffff);
182 if (ret < 0) {
183 dev_err(dev, "Failed to write TIME_1: %d\n", ret);
184 return ret;
187 ret = wm831x_reg_write(wm831x, WM831X_RTC_TIME_2, time & 0xffff);
188 if (ret < 0) {
189 dev_err(dev, "Failed to write TIME_2: %d\n", ret);
190 return ret;
193 /* Wait for the update to complete - should happen first time
194 * round but be conservative.
196 do {
197 msleep(1);
199 ret = wm831x_reg_read(wm831x, WM831X_RTC_CONTROL);
200 if (ret < 0)
201 ret = WM831X_RTC_SYNC_BUSY;
202 } while (!(ret & WM831X_RTC_SYNC_BUSY) &&
203 ++count < WM831X_SET_TIME_RETRIES);
205 if (ret & WM831X_RTC_SYNC_BUSY) {
206 dev_err(dev, "Timed out writing RTC update\n");
207 return -EIO;
210 /* Check that the update was accepted; security features may
211 * have caused the update to be ignored.
213 ret = wm831x_rtc_readtime(dev, &new_tm);
214 if (ret < 0)
215 return ret;
217 ret = rtc_tm_to_time(&new_tm, &new_time);
218 if (ret < 0) {
219 dev_err(dev, "Failed to convert time: %d\n", ret);
220 return ret;
223 /* Allow a second of change in case of tick */
224 if (new_time - time > 1) {
225 dev_err(dev, "RTC update not permitted by hardware\n");
226 return -EPERM;
229 return 0;
233 * Read alarm time and date in RTC
235 static int wm831x_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
237 struct wm831x_rtc *wm831x_rtc = dev_get_drvdata(dev);
238 int ret;
239 u16 data[2];
240 u32 time;
242 ret = wm831x_bulk_read(wm831x_rtc->wm831x, WM831X_RTC_ALARM_1,
243 2, data);
244 if (ret != 0) {
245 dev_err(dev, "Failed to read alarm time: %d\n", ret);
246 return ret;
249 time = (data[0] << 16) | data[1];
251 rtc_time_to_tm(time, &alrm->time);
253 ret = wm831x_reg_read(wm831x_rtc->wm831x, WM831X_RTC_CONTROL);
254 if (ret < 0) {
255 dev_err(dev, "Failed to read RTC control: %d\n", ret);
256 return ret;
259 if (ret & WM831X_RTC_ALM_ENA)
260 alrm->enabled = 1;
261 else
262 alrm->enabled = 0;
264 return 0;
267 static int wm831x_rtc_stop_alarm(struct wm831x_rtc *wm831x_rtc)
269 wm831x_rtc->alarm_enabled = 0;
271 return wm831x_set_bits(wm831x_rtc->wm831x, WM831X_RTC_CONTROL,
272 WM831X_RTC_ALM_ENA, 0);
275 static int wm831x_rtc_start_alarm(struct wm831x_rtc *wm831x_rtc)
277 wm831x_rtc->alarm_enabled = 1;
279 return wm831x_set_bits(wm831x_rtc->wm831x, WM831X_RTC_CONTROL,
280 WM831X_RTC_ALM_ENA, WM831X_RTC_ALM_ENA);
283 static int wm831x_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
285 struct wm831x_rtc *wm831x_rtc = dev_get_drvdata(dev);
286 struct wm831x *wm831x = wm831x_rtc->wm831x;
287 int ret;
288 unsigned long time;
290 ret = rtc_tm_to_time(&alrm->time, &time);
291 if (ret < 0) {
292 dev_err(dev, "Failed to convert time: %d\n", ret);
293 return ret;
296 ret = wm831x_rtc_stop_alarm(wm831x_rtc);
297 if (ret < 0) {
298 dev_err(dev, "Failed to stop alarm: %d\n", ret);
299 return ret;
302 ret = wm831x_reg_write(wm831x, WM831X_RTC_ALARM_1,
303 (time >> 16) & 0xffff);
304 if (ret < 0) {
305 dev_err(dev, "Failed to write ALARM_1: %d\n", ret);
306 return ret;
309 ret = wm831x_reg_write(wm831x, WM831X_RTC_ALARM_2, time & 0xffff);
310 if (ret < 0) {
311 dev_err(dev, "Failed to write ALARM_2: %d\n", ret);
312 return ret;
315 if (alrm->enabled) {
316 ret = wm831x_rtc_start_alarm(wm831x_rtc);
317 if (ret < 0) {
318 dev_err(dev, "Failed to start alarm: %d\n", ret);
319 return ret;
323 return 0;
326 static int wm831x_rtc_alarm_irq_enable(struct device *dev,
327 unsigned int enabled)
329 struct wm831x_rtc *wm831x_rtc = dev_get_drvdata(dev);
331 if (enabled)
332 return wm831x_rtc_start_alarm(wm831x_rtc);
333 else
334 return wm831x_rtc_stop_alarm(wm831x_rtc);
337 static int wm831x_rtc_update_irq_enable(struct device *dev,
338 unsigned int enabled)
340 struct wm831x_rtc *wm831x_rtc = dev_get_drvdata(dev);
341 int val;
343 if (enabled)
344 val = 1 << WM831X_RTC_PINT_FREQ_SHIFT;
345 else
346 val = 0;
348 return wm831x_set_bits(wm831x_rtc->wm831x, WM831X_RTC_CONTROL,
349 WM831X_RTC_PINT_FREQ_MASK, val);
352 static irqreturn_t wm831x_alm_irq(int irq, void *data)
354 struct wm831x_rtc *wm831x_rtc = data;
356 rtc_update_irq(wm831x_rtc->rtc, 1, RTC_IRQF | RTC_AF);
358 return IRQ_HANDLED;
361 static irqreturn_t wm831x_per_irq(int irq, void *data)
363 struct wm831x_rtc *wm831x_rtc = data;
365 rtc_update_irq(wm831x_rtc->rtc, 1, RTC_IRQF | RTC_UF);
367 return IRQ_HANDLED;
370 static const struct rtc_class_ops wm831x_rtc_ops = {
371 .read_time = wm831x_rtc_readtime,
372 .set_mmss = wm831x_rtc_set_mmss,
373 .read_alarm = wm831x_rtc_readalarm,
374 .set_alarm = wm831x_rtc_setalarm,
375 .alarm_irq_enable = wm831x_rtc_alarm_irq_enable,
376 .update_irq_enable = wm831x_rtc_update_irq_enable,
379 #ifdef CONFIG_PM
380 /* Turn off the alarm if it should not be a wake source. */
381 static int wm831x_rtc_suspend(struct device *dev)
383 struct platform_device *pdev = to_platform_device(dev);
384 struct wm831x_rtc *wm831x_rtc = dev_get_drvdata(&pdev->dev);
385 int ret, enable;
387 if (wm831x_rtc->alarm_enabled && device_may_wakeup(&pdev->dev))
388 enable = WM831X_RTC_ALM_ENA;
389 else
390 enable = 0;
392 ret = wm831x_set_bits(wm831x_rtc->wm831x, WM831X_RTC_CONTROL,
393 WM831X_RTC_ALM_ENA, enable);
394 if (ret != 0)
395 dev_err(&pdev->dev, "Failed to update RTC alarm: %d\n", ret);
397 return 0;
400 /* Enable the alarm if it should be enabled (in case it was disabled to
401 * prevent use as a wake source).
403 static int wm831x_rtc_resume(struct device *dev)
405 struct platform_device *pdev = to_platform_device(dev);
406 struct wm831x_rtc *wm831x_rtc = dev_get_drvdata(&pdev->dev);
407 int ret;
409 if (wm831x_rtc->alarm_enabled) {
410 ret = wm831x_rtc_start_alarm(wm831x_rtc);
411 if (ret != 0)
412 dev_err(&pdev->dev,
413 "Failed to restart RTC alarm: %d\n", ret);
416 return 0;
419 /* Unconditionally disable the alarm */
420 static int wm831x_rtc_freeze(struct device *dev)
422 struct platform_device *pdev = to_platform_device(dev);
423 struct wm831x_rtc *wm831x_rtc = dev_get_drvdata(&pdev->dev);
424 int ret;
426 ret = wm831x_set_bits(wm831x_rtc->wm831x, WM831X_RTC_CONTROL,
427 WM831X_RTC_ALM_ENA, 0);
428 if (ret != 0)
429 dev_err(&pdev->dev, "Failed to stop RTC alarm: %d\n", ret);
431 return 0;
433 #else
434 #define wm831x_rtc_suspend NULL
435 #define wm831x_rtc_resume NULL
436 #define wm831x_rtc_freeze NULL
437 #endif
439 static int wm831x_rtc_probe(struct platform_device *pdev)
441 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
442 struct wm831x_rtc *wm831x_rtc;
443 int per_irq = platform_get_irq_byname(pdev, "PER");
444 int alm_irq = platform_get_irq_byname(pdev, "ALM");
445 int ret = 0;
447 wm831x_rtc = kzalloc(sizeof(*wm831x_rtc), GFP_KERNEL);
448 if (wm831x_rtc == NULL)
449 return -ENOMEM;
451 platform_set_drvdata(pdev, wm831x_rtc);
452 wm831x_rtc->wm831x = wm831x;
454 ret = wm831x_reg_read(wm831x, WM831X_RTC_CONTROL);
455 if (ret < 0) {
456 dev_err(&pdev->dev, "Failed to read RTC control: %d\n", ret);
457 goto err;
459 if (ret & WM831X_RTC_ALM_ENA)
460 wm831x_rtc->alarm_enabled = 1;
462 device_init_wakeup(&pdev->dev, 1);
464 wm831x_rtc->rtc = rtc_device_register("wm831x", &pdev->dev,
465 &wm831x_rtc_ops, THIS_MODULE);
466 if (IS_ERR(wm831x_rtc->rtc)) {
467 ret = PTR_ERR(wm831x_rtc->rtc);
468 goto err;
471 ret = wm831x_request_irq(wm831x, per_irq, wm831x_per_irq,
472 IRQF_TRIGGER_RISING, "wm831x_rtc_per",
473 wm831x_rtc);
474 if (ret != 0) {
475 dev_err(&pdev->dev, "Failed to request periodic IRQ %d: %d\n",
476 per_irq, ret);
479 ret = wm831x_request_irq(wm831x, alm_irq, wm831x_alm_irq,
480 IRQF_TRIGGER_RISING, "wm831x_rtc_alm",
481 wm831x_rtc);
482 if (ret != 0) {
483 dev_err(&pdev->dev, "Failed to request alarm IRQ %d: %d\n",
484 alm_irq, ret);
487 wm831x_rtc_add_randomness(wm831x);
489 return 0;
491 err:
492 kfree(wm831x_rtc);
493 return ret;
496 static int __devexit wm831x_rtc_remove(struct platform_device *pdev)
498 struct wm831x_rtc *wm831x_rtc = platform_get_drvdata(pdev);
499 int per_irq = platform_get_irq_byname(pdev, "PER");
500 int alm_irq = platform_get_irq_byname(pdev, "ALM");
502 wm831x_free_irq(wm831x_rtc->wm831x, alm_irq, wm831x_rtc);
503 wm831x_free_irq(wm831x_rtc->wm831x, per_irq, wm831x_rtc);
504 rtc_device_unregister(wm831x_rtc->rtc);
505 kfree(wm831x_rtc);
507 return 0;
510 static struct dev_pm_ops wm831x_rtc_pm_ops = {
511 .suspend = wm831x_rtc_suspend,
512 .resume = wm831x_rtc_resume,
514 .freeze = wm831x_rtc_freeze,
515 .thaw = wm831x_rtc_resume,
516 .restore = wm831x_rtc_resume,
518 .poweroff = wm831x_rtc_suspend,
521 static struct platform_driver wm831x_rtc_driver = {
522 .probe = wm831x_rtc_probe,
523 .remove = __devexit_p(wm831x_rtc_remove),
524 .driver = {
525 .name = "wm831x-rtc",
526 .pm = &wm831x_rtc_pm_ops,
530 static int __init wm831x_rtc_init(void)
532 return platform_driver_register(&wm831x_rtc_driver);
534 module_init(wm831x_rtc_init);
536 static void __exit wm831x_rtc_exit(void)
538 platform_driver_unregister(&wm831x_rtc_driver);
540 module_exit(wm831x_rtc_exit);
542 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
543 MODULE_DESCRIPTION("RTC driver for the WM831x series PMICs");
544 MODULE_LICENSE("GPL");
545 MODULE_ALIAS("platform:wm831x-rtc");