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>
27 #include <linux/random.h>
32 #define WM831X_RTC_WR_CNT_MASK 0xFFFF
33 #define WM831X_RTC_WR_CNT_SHIFT 0
34 #define WM831X_RTC_WR_CNT_WIDTH 16
39 #define WM831X_RTC_TIME_MASK 0xFFFF
40 #define WM831X_RTC_TIME_SHIFT 0
41 #define WM831X_RTC_TIME_WIDTH 16
46 #define WM831X_RTC_TIME_MASK 0xFFFF
47 #define WM831X_RTC_TIME_SHIFT 0
48 #define WM831X_RTC_TIME_WIDTH 16
53 #define WM831X_RTC_ALM_MASK 0xFFFF
54 #define WM831X_RTC_ALM_SHIFT 0
55 #define WM831X_RTC_ALM_WIDTH 16
60 #define WM831X_RTC_ALM_MASK 0xFFFF
61 #define WM831X_RTC_ALM_SHIFT 0
62 #define WM831X_RTC_ALM_WIDTH 16
67 #define WM831X_RTC_VALID 0x8000
68 #define WM831X_RTC_VALID_MASK 0x8000
69 #define WM831X_RTC_VALID_SHIFT 15
70 #define WM831X_RTC_VALID_WIDTH 1
71 #define WM831X_RTC_SYNC_BUSY 0x4000
72 #define WM831X_RTC_SYNC_BUSY_MASK 0x4000
73 #define WM831X_RTC_SYNC_BUSY_SHIFT 14
74 #define WM831X_RTC_SYNC_BUSY_WIDTH 1
75 #define WM831X_RTC_ALM_ENA 0x0400
76 #define WM831X_RTC_ALM_ENA_MASK 0x0400
77 #define WM831X_RTC_ALM_ENA_SHIFT 10
78 #define WM831X_RTC_ALM_ENA_WIDTH 1
79 #define WM831X_RTC_PINT_FREQ_MASK 0x0070
80 #define WM831X_RTC_PINT_FREQ_SHIFT 4
81 #define WM831X_RTC_PINT_FREQ_WIDTH 3
86 #define WM831X_RTC_TRIM_MASK 0x03FF
87 #define WM831X_RTC_TRIM_SHIFT 0
88 #define WM831X_RTC_TRIM_WIDTH 10
90 #define WM831X_SET_TIME_RETRIES 5
91 #define WM831X_GET_TIME_RETRIES 5
99 static void wm831x_rtc_add_randomness(
struct wm831x *
wm831x)
114 dev_warn(wm831x->
dev,
"Failed to read RTC write counter: %d\n",
126 u16 time1[2], time2[2];
133 dev_err(dev,
"Failed to read RTC control: %d\n", ret);
137 dev_dbg(dev,
"RTC not yet configured\n");
155 if (
memcmp(time1, time2,
sizeof(time1)) == 0) {
156 u32 time = (time1[0] << 16) | time1[1];
164 dev_err(dev,
"Timed out reading current time\n");
172 static int wm831x_rtc_set_mmss(
struct device *dev,
unsigned long time)
175 struct wm831x *wm831x = wm831x_rtc->
wm831x;
177 unsigned long new_time;
182 (time >> 16) & 0xffff);
184 dev_err(dev,
"Failed to write TIME_1: %d\n", ret);
190 dev_err(dev,
"Failed to write TIME_2: %d\n", ret);
206 if (ret & WM831X_RTC_SYNC_BUSY) {
207 dev_err(dev,
"Timed out writing RTC update\n");
214 ret = wm831x_rtc_readtime(dev, &new_tm);
220 dev_err(dev,
"Failed to convert time: %d\n", ret);
225 if (new_time - time > 1) {
226 dev_err(dev,
"RTC update not permitted by hardware\n");
236 static int wm831x_rtc_readalarm(
struct device *dev,
struct rtc_wkalrm *alrm)
246 dev_err(dev,
"Failed to read alarm time: %d\n", ret);
250 time = (data[0] << 16) | data[1];
256 dev_err(dev,
"Failed to read RTC control: %d\n", ret);
268 static int wm831x_rtc_stop_alarm(
struct wm831x_rtc *wm831x_rtc)
276 static int wm831x_rtc_start_alarm(
struct wm831x_rtc *wm831x_rtc)
287 struct wm831x *wm831x = wm831x_rtc->
wm831x;
293 dev_err(dev,
"Failed to convert time: %d\n", ret);
297 ret = wm831x_rtc_stop_alarm(wm831x_rtc);
299 dev_err(dev,
"Failed to stop alarm: %d\n", ret);
304 (time >> 16) & 0xffff);
306 dev_err(dev,
"Failed to write ALARM_1: %d\n", ret);
312 dev_err(dev,
"Failed to write ALARM_2: %d\n", ret);
317 ret = wm831x_rtc_start_alarm(wm831x_rtc);
319 dev_err(dev,
"Failed to start alarm: %d\n", ret);
327 static int wm831x_rtc_alarm_irq_enable(
struct device *dev,
333 return wm831x_rtc_start_alarm(wm831x_rtc);
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;
348 .read_time = wm831x_rtc_readtime,
349 .set_mmss = wm831x_rtc_set_mmss,
350 .read_alarm = wm831x_rtc_readalarm,
351 .set_alarm = wm831x_rtc_setalarm,
352 .alarm_irq_enable = wm831x_rtc_alarm_irq_enable,
371 dev_err(&pdev->
dev,
"Failed to update RTC alarm: %d\n", ret);
386 ret = wm831x_rtc_start_alarm(wm831x_rtc);
389 "Failed to restart RTC alarm: %d\n", ret);
405 dev_err(&pdev->
dev,
"Failed to stop RTC alarm: %d\n", ret);
410 #define wm831x_rtc_suspend NULL
411 #define wm831x_rtc_resume NULL
412 #define wm831x_rtc_freeze NULL
418 struct wm831x_rtc *wm831x_rtc;
423 if (wm831x_rtc ==
NULL)
426 platform_set_drvdata(pdev, wm831x_rtc);
431 dev_err(&pdev->
dev,
"Failed to read RTC control: %d\n", ret);
441 if (IS_ERR(wm831x_rtc->
rtc)) {
442 ret = PTR_ERR(wm831x_rtc->
rtc);
450 dev_err(&pdev->
dev,
"Failed to request alarm IRQ %d: %d\n",
454 wm831x_rtc_add_randomness(wm831x);
464 struct wm831x_rtc *wm831x_rtc = platform_get_drvdata(pdev);
473 static const struct dev_pm_ops wm831x_rtc_pm_ops = {
485 .probe = wm831x_rtc_probe,
488 .name =
"wm831x-rtc",
489 .pm = &wm831x_rtc_pm_ops,