UBI: Validate data_size
[linux/fpc-iii.git] / drivers / rtc / rtc-wm831x.c
blobf3c211099a1482947ff5f7fd9427961657493110
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/slab.h>
20 #include <linux/bcd.h>
21 #include <linux/interrupt.h>
22 #include <linux/ioctl.h>
23 #include <linux/completion.h>
24 #include <linux/mfd/wm831x/core.h>
25 #include <linux/delay.h>
26 #include <linux/platform_device.h>
27 #include <linux/random.h>
30 * R16416 (0x4020) - RTC Write Counter
32 #define WM831X_RTC_WR_CNT_MASK 0xFFFF /* RTC_WR_CNT - [15:0] */
33 #define WM831X_RTC_WR_CNT_SHIFT 0 /* RTC_WR_CNT - [15:0] */
34 #define WM831X_RTC_WR_CNT_WIDTH 16 /* RTC_WR_CNT - [15:0] */
37 * R16417 (0x4021) - RTC Time 1
39 #define WM831X_RTC_TIME_MASK 0xFFFF /* RTC_TIME - [15:0] */
40 #define WM831X_RTC_TIME_SHIFT 0 /* RTC_TIME - [15:0] */
41 #define WM831X_RTC_TIME_WIDTH 16 /* RTC_TIME - [15:0] */
44 * R16418 (0x4022) - RTC Time 2
46 #define WM831X_RTC_TIME_MASK 0xFFFF /* RTC_TIME - [15:0] */
47 #define WM831X_RTC_TIME_SHIFT 0 /* RTC_TIME - [15:0] */
48 #define WM831X_RTC_TIME_WIDTH 16 /* RTC_TIME - [15:0] */
51 * R16419 (0x4023) - RTC Alarm 1
53 #define WM831X_RTC_ALM_MASK 0xFFFF /* RTC_ALM - [15:0] */
54 #define WM831X_RTC_ALM_SHIFT 0 /* RTC_ALM - [15:0] */
55 #define WM831X_RTC_ALM_WIDTH 16 /* RTC_ALM - [15:0] */
58 * R16420 (0x4024) - RTC Alarm 2
60 #define WM831X_RTC_ALM_MASK 0xFFFF /* RTC_ALM - [15:0] */
61 #define WM831X_RTC_ALM_SHIFT 0 /* RTC_ALM - [15:0] */
62 #define WM831X_RTC_ALM_WIDTH 16 /* RTC_ALM - [15:0] */
65 * R16421 (0x4025) - RTC Control
67 #define WM831X_RTC_VALID 0x8000 /* RTC_VALID */
68 #define WM831X_RTC_VALID_MASK 0x8000 /* RTC_VALID */
69 #define WM831X_RTC_VALID_SHIFT 15 /* RTC_VALID */
70 #define WM831X_RTC_VALID_WIDTH 1 /* RTC_VALID */
71 #define WM831X_RTC_SYNC_BUSY 0x4000 /* RTC_SYNC_BUSY */
72 #define WM831X_RTC_SYNC_BUSY_MASK 0x4000 /* RTC_SYNC_BUSY */
73 #define WM831X_RTC_SYNC_BUSY_SHIFT 14 /* RTC_SYNC_BUSY */
74 #define WM831X_RTC_SYNC_BUSY_WIDTH 1 /* RTC_SYNC_BUSY */
75 #define WM831X_RTC_ALM_ENA 0x0400 /* RTC_ALM_ENA */
76 #define WM831X_RTC_ALM_ENA_MASK 0x0400 /* RTC_ALM_ENA */
77 #define WM831X_RTC_ALM_ENA_SHIFT 10 /* RTC_ALM_ENA */
78 #define WM831X_RTC_ALM_ENA_WIDTH 1 /* RTC_ALM_ENA */
79 #define WM831X_RTC_PINT_FREQ_MASK 0x0070 /* RTC_PINT_FREQ - [6:4] */
80 #define WM831X_RTC_PINT_FREQ_SHIFT 4 /* RTC_PINT_FREQ - [6:4] */
81 #define WM831X_RTC_PINT_FREQ_WIDTH 3 /* RTC_PINT_FREQ - [6:4] */
84 * R16422 (0x4026) - RTC Trim
86 #define WM831X_RTC_TRIM_MASK 0x03FF /* RTC_TRIM - [9:0] */
87 #define WM831X_RTC_TRIM_SHIFT 0 /* RTC_TRIM - [9:0] */
88 #define WM831X_RTC_TRIM_WIDTH 10 /* RTC_TRIM - [9:0] */
90 #define WM831X_SET_TIME_RETRIES 5
91 #define WM831X_GET_TIME_RETRIES 5
93 struct wm831x_rtc {
94 struct wm831x *wm831x;
95 struct rtc_device *rtc;
96 unsigned int alarm_enabled:1;
99 static void wm831x_rtc_add_randomness(struct wm831x *wm831x)
101 int ret;
102 u16 reg;
105 * The write counter contains a pseudo-random number which is
106 * regenerated every time we set the RTC so it should be a
107 * useful per-system source of entropy.
109 ret = wm831x_reg_read(wm831x, WM831X_RTC_WRITE_COUNTER);
110 if (ret >= 0) {
111 reg = ret;
112 add_device_randomness(&reg, sizeof(reg));
113 } else {
114 dev_warn(wm831x->dev, "Failed to read RTC write counter: %d\n",
115 ret);
120 * Read current time and date in RTC
122 static int wm831x_rtc_readtime(struct device *dev, struct rtc_time *tm)
124 struct wm831x_rtc *wm831x_rtc = dev_get_drvdata(dev);
125 struct wm831x *wm831x = wm831x_rtc->wm831x;
126 u16 time1[2], time2[2];
127 int ret;
128 int count = 0;
130 /* Has the RTC been programmed? */
131 ret = wm831x_reg_read(wm831x, WM831X_RTC_CONTROL);
132 if (ret < 0) {
133 dev_err(dev, "Failed to read RTC control: %d\n", ret);
134 return ret;
136 if (!(ret & WM831X_RTC_VALID)) {
137 dev_dbg(dev, "RTC not yet configured\n");
138 return -EINVAL;
141 /* Read twice to make sure we don't read a corrupt, partially
142 * incremented, value.
144 do {
145 ret = wm831x_bulk_read(wm831x, WM831X_RTC_TIME_1,
146 2, time1);
147 if (ret != 0)
148 continue;
150 ret = wm831x_bulk_read(wm831x, WM831X_RTC_TIME_1,
151 2, time2);
152 if (ret != 0)
153 continue;
155 if (memcmp(time1, time2, sizeof(time1)) == 0) {
156 u32 time = (time1[0] << 16) | time1[1];
158 rtc_time_to_tm(time, tm);
159 return rtc_valid_tm(tm);
162 } while (++count < WM831X_GET_TIME_RETRIES);
164 dev_err(dev, "Timed out reading current time\n");
166 return -EIO;
170 * Set current time and date in RTC
172 static int wm831x_rtc_set_mmss(struct device *dev, unsigned long time)
174 struct wm831x_rtc *wm831x_rtc = dev_get_drvdata(dev);
175 struct wm831x *wm831x = wm831x_rtc->wm831x;
176 struct rtc_time new_tm;
177 unsigned long new_time;
178 int ret;
179 int count = 0;
181 ret = wm831x_reg_write(wm831x, WM831X_RTC_TIME_1,
182 (time >> 16) & 0xffff);
183 if (ret < 0) {
184 dev_err(dev, "Failed to write TIME_1: %d\n", ret);
185 return ret;
188 ret = wm831x_reg_write(wm831x, WM831X_RTC_TIME_2, time & 0xffff);
189 if (ret < 0) {
190 dev_err(dev, "Failed to write TIME_2: %d\n", ret);
191 return ret;
194 /* Wait for the update to complete - should happen first time
195 * round but be conservative.
197 do {
198 msleep(1);
200 ret = wm831x_reg_read(wm831x, WM831X_RTC_CONTROL);
201 if (ret < 0)
202 ret = WM831X_RTC_SYNC_BUSY;
203 } while (!(ret & WM831X_RTC_SYNC_BUSY) &&
204 ++count < WM831X_SET_TIME_RETRIES);
206 if (ret & WM831X_RTC_SYNC_BUSY) {
207 dev_err(dev, "Timed out writing RTC update\n");
208 return -EIO;
211 /* Check that the update was accepted; security features may
212 * have caused the update to be ignored.
214 ret = wm831x_rtc_readtime(dev, &new_tm);
215 if (ret < 0)
216 return ret;
218 ret = rtc_tm_to_time(&new_tm, &new_time);
219 if (ret < 0) {
220 dev_err(dev, "Failed to convert time: %d\n", ret);
221 return ret;
224 /* Allow a second of change in case of tick */
225 if (new_time - time > 1) {
226 dev_err(dev, "RTC update not permitted by hardware\n");
227 return -EPERM;
230 return 0;
234 * Read alarm time and date in RTC
236 static int wm831x_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
238 struct wm831x_rtc *wm831x_rtc = dev_get_drvdata(dev);
239 int ret;
240 u16 data[2];
241 u32 time;
243 ret = wm831x_bulk_read(wm831x_rtc->wm831x, WM831X_RTC_ALARM_1,
244 2, data);
245 if (ret != 0) {
246 dev_err(dev, "Failed to read alarm time: %d\n", ret);
247 return ret;
250 time = (data[0] << 16) | data[1];
252 rtc_time_to_tm(time, &alrm->time);
254 ret = wm831x_reg_read(wm831x_rtc->wm831x, WM831X_RTC_CONTROL);
255 if (ret < 0) {
256 dev_err(dev, "Failed to read RTC control: %d\n", ret);
257 return ret;
260 if (ret & WM831X_RTC_ALM_ENA)
261 alrm->enabled = 1;
262 else
263 alrm->enabled = 0;
265 return 0;
268 static int wm831x_rtc_stop_alarm(struct wm831x_rtc *wm831x_rtc)
270 wm831x_rtc->alarm_enabled = 0;
272 return wm831x_set_bits(wm831x_rtc->wm831x, WM831X_RTC_CONTROL,
273 WM831X_RTC_ALM_ENA, 0);
276 static int wm831x_rtc_start_alarm(struct wm831x_rtc *wm831x_rtc)
278 wm831x_rtc->alarm_enabled = 1;
280 return wm831x_set_bits(wm831x_rtc->wm831x, WM831X_RTC_CONTROL,
281 WM831X_RTC_ALM_ENA, WM831X_RTC_ALM_ENA);
284 static int wm831x_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
286 struct wm831x_rtc *wm831x_rtc = dev_get_drvdata(dev);
287 struct wm831x *wm831x = wm831x_rtc->wm831x;
288 int ret;
289 unsigned long time;
291 ret = rtc_tm_to_time(&alrm->time, &time);
292 if (ret < 0) {
293 dev_err(dev, "Failed to convert time: %d\n", ret);
294 return ret;
297 ret = wm831x_rtc_stop_alarm(wm831x_rtc);
298 if (ret < 0) {
299 dev_err(dev, "Failed to stop alarm: %d\n", ret);
300 return ret;
303 ret = wm831x_reg_write(wm831x, WM831X_RTC_ALARM_1,
304 (time >> 16) & 0xffff);
305 if (ret < 0) {
306 dev_err(dev, "Failed to write ALARM_1: %d\n", ret);
307 return ret;
310 ret = wm831x_reg_write(wm831x, WM831X_RTC_ALARM_2, time & 0xffff);
311 if (ret < 0) {
312 dev_err(dev, "Failed to write ALARM_2: %d\n", ret);
313 return ret;
316 if (alrm->enabled) {
317 ret = wm831x_rtc_start_alarm(wm831x_rtc);
318 if (ret < 0) {
319 dev_err(dev, "Failed to start alarm: %d\n", ret);
320 return ret;
324 return 0;
327 static int wm831x_rtc_alarm_irq_enable(struct device *dev,
328 unsigned int enabled)
330 struct wm831x_rtc *wm831x_rtc = dev_get_drvdata(dev);
332 if (enabled)
333 return wm831x_rtc_start_alarm(wm831x_rtc);
334 else
335 return wm831x_rtc_stop_alarm(wm831x_rtc);
338 static irqreturn_t wm831x_alm_irq(int irq, void *data)
340 struct wm831x_rtc *wm831x_rtc = data;
342 rtc_update_irq(wm831x_rtc->rtc, 1, RTC_IRQF | RTC_AF);
344 return IRQ_HANDLED;
347 static irqreturn_t wm831x_per_irq(int irq, void *data)
349 struct wm831x_rtc *wm831x_rtc = data;
351 rtc_update_irq(wm831x_rtc->rtc, 1, RTC_IRQF | RTC_UF);
353 return IRQ_HANDLED;
356 static const struct rtc_class_ops wm831x_rtc_ops = {
357 .read_time = wm831x_rtc_readtime,
358 .set_mmss = wm831x_rtc_set_mmss,
359 .read_alarm = wm831x_rtc_readalarm,
360 .set_alarm = wm831x_rtc_setalarm,
361 .alarm_irq_enable = wm831x_rtc_alarm_irq_enable,
364 #ifdef CONFIG_PM
365 /* Turn off the alarm if it should not be a wake source. */
366 static int wm831x_rtc_suspend(struct device *dev)
368 struct platform_device *pdev = to_platform_device(dev);
369 struct wm831x_rtc *wm831x_rtc = dev_get_drvdata(&pdev->dev);
370 int ret, enable;
372 if (wm831x_rtc->alarm_enabled && device_may_wakeup(&pdev->dev))
373 enable = WM831X_RTC_ALM_ENA;
374 else
375 enable = 0;
377 ret = wm831x_set_bits(wm831x_rtc->wm831x, WM831X_RTC_CONTROL,
378 WM831X_RTC_ALM_ENA, enable);
379 if (ret != 0)
380 dev_err(&pdev->dev, "Failed to update RTC alarm: %d\n", ret);
382 return 0;
385 /* Enable the alarm if it should be enabled (in case it was disabled to
386 * prevent use as a wake source).
388 static int wm831x_rtc_resume(struct device *dev)
390 struct platform_device *pdev = to_platform_device(dev);
391 struct wm831x_rtc *wm831x_rtc = dev_get_drvdata(&pdev->dev);
392 int ret;
394 if (wm831x_rtc->alarm_enabled) {
395 ret = wm831x_rtc_start_alarm(wm831x_rtc);
396 if (ret != 0)
397 dev_err(&pdev->dev,
398 "Failed to restart RTC alarm: %d\n", ret);
401 return 0;
404 /* Unconditionally disable the alarm */
405 static int wm831x_rtc_freeze(struct device *dev)
407 struct platform_device *pdev = to_platform_device(dev);
408 struct wm831x_rtc *wm831x_rtc = dev_get_drvdata(&pdev->dev);
409 int ret;
411 ret = wm831x_set_bits(wm831x_rtc->wm831x, WM831X_RTC_CONTROL,
412 WM831X_RTC_ALM_ENA, 0);
413 if (ret != 0)
414 dev_err(&pdev->dev, "Failed to stop RTC alarm: %d\n", ret);
416 return 0;
418 #else
419 #define wm831x_rtc_suspend NULL
420 #define wm831x_rtc_resume NULL
421 #define wm831x_rtc_freeze NULL
422 #endif
424 static int wm831x_rtc_probe(struct platform_device *pdev)
426 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
427 struct wm831x_rtc *wm831x_rtc;
428 int per_irq = platform_get_irq_byname(pdev, "PER");
429 int alm_irq = platform_get_irq_byname(pdev, "ALM");
430 int ret = 0;
432 wm831x_rtc = kzalloc(sizeof(*wm831x_rtc), GFP_KERNEL);
433 if (wm831x_rtc == NULL)
434 return -ENOMEM;
436 platform_set_drvdata(pdev, wm831x_rtc);
437 wm831x_rtc->wm831x = wm831x;
439 ret = wm831x_reg_read(wm831x, WM831X_RTC_CONTROL);
440 if (ret < 0) {
441 dev_err(&pdev->dev, "Failed to read RTC control: %d\n", ret);
442 goto err;
444 if (ret & WM831X_RTC_ALM_ENA)
445 wm831x_rtc->alarm_enabled = 1;
447 device_init_wakeup(&pdev->dev, 1);
449 wm831x_rtc->rtc = rtc_device_register("wm831x", &pdev->dev,
450 &wm831x_rtc_ops, THIS_MODULE);
451 if (IS_ERR(wm831x_rtc->rtc)) {
452 ret = PTR_ERR(wm831x_rtc->rtc);
453 goto err;
456 ret = request_threaded_irq(per_irq, NULL, wm831x_per_irq,
457 IRQF_TRIGGER_RISING, "RTC period",
458 wm831x_rtc);
459 if (ret != 0) {
460 dev_err(&pdev->dev, "Failed to request periodic IRQ %d: %d\n",
461 per_irq, ret);
464 ret = request_threaded_irq(alm_irq, NULL, wm831x_alm_irq,
465 IRQF_TRIGGER_RISING, "RTC alarm",
466 wm831x_rtc);
467 if (ret != 0) {
468 dev_err(&pdev->dev, "Failed to request alarm IRQ %d: %d\n",
469 alm_irq, ret);
472 wm831x_rtc_add_randomness(wm831x);
474 return 0;
476 err:
477 kfree(wm831x_rtc);
478 return ret;
481 static int __devexit wm831x_rtc_remove(struct platform_device *pdev)
483 struct wm831x_rtc *wm831x_rtc = platform_get_drvdata(pdev);
484 int per_irq = platform_get_irq_byname(pdev, "PER");
485 int alm_irq = platform_get_irq_byname(pdev, "ALM");
487 free_irq(alm_irq, wm831x_rtc);
488 free_irq(per_irq, wm831x_rtc);
489 rtc_device_unregister(wm831x_rtc->rtc);
490 kfree(wm831x_rtc);
492 return 0;
495 static const struct dev_pm_ops wm831x_rtc_pm_ops = {
496 .suspend = wm831x_rtc_suspend,
497 .resume = wm831x_rtc_resume,
499 .freeze = wm831x_rtc_freeze,
500 .thaw = wm831x_rtc_resume,
501 .restore = wm831x_rtc_resume,
503 .poweroff = wm831x_rtc_suspend,
506 static struct platform_driver wm831x_rtc_driver = {
507 .probe = wm831x_rtc_probe,
508 .remove = __devexit_p(wm831x_rtc_remove),
509 .driver = {
510 .name = "wm831x-rtc",
511 .pm = &wm831x_rtc_pm_ops,
515 static int __init wm831x_rtc_init(void)
517 return platform_driver_register(&wm831x_rtc_driver);
519 module_init(wm831x_rtc_init);
521 static void __exit wm831x_rtc_exit(void)
523 platform_driver_unregister(&wm831x_rtc_driver);
525 module_exit(wm831x_rtc_exit);
527 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
528 MODULE_DESCRIPTION("RTC driver for the WM831x series PMICs");
529 MODULE_LICENSE("GPL");
530 MODULE_ALIAS("platform:wm831x-rtc");