2 * Real Time Clock driver for Wolfson Microelectronics WM8350
4 * Copyright (C) 2007, 2008 Wolfson Microelectronics PLC.
6 * Author: Liam Girdwood
7 * linux@wolfsonmicro.com
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/time.h>
19 #include <linux/rtc.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/wm8350/rtc.h>
25 #include <linux/mfd/wm8350/core.h>
26 #include <linux/delay.h>
27 #include <linux/platform_device.h>
29 #define WM8350_SET_ALM_RETRIES 5
30 #define WM8350_SET_TIME_RETRIES 5
31 #define WM8350_GET_TIME_RETRIES 5
34 * Read current time and date in RTC
36 static int wm8350_rtc_readtime(struct device
*dev
, struct rtc_time
*tm
)
38 struct wm8350
*wm8350
= dev_get_drvdata(dev
);
39 u16 time1
[4], time2
[4];
40 int retries
= WM8350_GET_TIME_RETRIES
, ret
;
43 * Read the time twice and compare.
44 * If time1 == time2, then time is valid else retry.
47 ret
= wm8350_block_read(wm8350
, WM8350_RTC_SECONDS_MINUTES
,
51 ret
= wm8350_block_read(wm8350
, WM8350_RTC_SECONDS_MINUTES
,
56 if (memcmp(time1
, time2
, sizeof(time1
)) == 0) {
57 tm
->tm_sec
= time1
[0] & WM8350_RTC_SECS_MASK
;
59 tm
->tm_min
= (time1
[0] & WM8350_RTC_MINS_MASK
)
60 >> WM8350_RTC_MINS_SHIFT
;
62 tm
->tm_hour
= time1
[1] & WM8350_RTC_HRS_MASK
;
64 tm
->tm_wday
= ((time1
[1] >> WM8350_RTC_DAY_SHIFT
)
67 tm
->tm_mon
= ((time1
[2] & WM8350_RTC_MTH_MASK
)
68 >> WM8350_RTC_MTH_SHIFT
) - 1;
70 tm
->tm_mday
= (time1
[2] & WM8350_RTC_DATE_MASK
);
72 tm
->tm_year
= ((time1
[3] & WM8350_RTC_YHUNDREDS_MASK
)
73 >> WM8350_RTC_YHUNDREDS_SHIFT
) * 100;
74 tm
->tm_year
+= time1
[3] & WM8350_RTC_YUNITS_MASK
;
76 tm
->tm_yday
= rtc_year_days(tm
->tm_mday
, tm
->tm_mon
,
80 dev_dbg(dev
, "Read (%d left): %04x %04x %04x %04x\n",
82 time1
[0], time1
[1], time1
[2], time1
[3]);
88 dev_err(dev
, "timed out reading RTC time\n");
93 * Set current time and date in RTC
95 static int wm8350_rtc_settime(struct device
*dev
, struct rtc_time
*tm
)
97 struct wm8350
*wm8350
= dev_get_drvdata(dev
);
100 int ret
, retries
= WM8350_SET_TIME_RETRIES
;
102 time
[0] = tm
->tm_sec
;
103 time
[0] |= tm
->tm_min
<< WM8350_RTC_MINS_SHIFT
;
104 time
[1] = tm
->tm_hour
;
105 time
[1] |= (tm
->tm_wday
+ 1) << WM8350_RTC_DAY_SHIFT
;
106 time
[2] = tm
->tm_mday
;
107 time
[2] |= (tm
->tm_mon
+ 1) << WM8350_RTC_MTH_SHIFT
;
108 time
[3] = ((tm
->tm_year
+ 1900) / 100) << WM8350_RTC_YHUNDREDS_SHIFT
;
109 time
[3] |= (tm
->tm_year
+ 1900) % 100;
111 dev_dbg(dev
, "Setting: %04x %04x %04x %04x\n",
112 time
[0], time
[1], time
[2], time
[3]);
114 /* Set RTC_SET to stop the clock */
115 ret
= wm8350_set_bits(wm8350
, WM8350_RTC_TIME_CONTROL
, WM8350_RTC_SET
);
119 /* Wait until confirmation of stopping */
121 rtc_ctrl
= wm8350_reg_read(wm8350
, WM8350_RTC_TIME_CONTROL
);
122 schedule_timeout_uninterruptible(msecs_to_jiffies(1));
123 } while (--retries
&& !(rtc_ctrl
& WM8350_RTC_STS
));
126 dev_err(dev
, "timed out on set confirmation\n");
130 /* Write time to RTC */
131 ret
= wm8350_block_write(wm8350
, WM8350_RTC_SECONDS_MINUTES
, 4, time
);
135 /* Clear RTC_SET to start the clock */
136 ret
= wm8350_clear_bits(wm8350
, WM8350_RTC_TIME_CONTROL
,
142 * Read alarm time and date in RTC
144 static int wm8350_rtc_readalarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
146 struct wm8350
*wm8350
= dev_get_drvdata(dev
);
147 struct rtc_time
*tm
= &alrm
->time
;
151 ret
= wm8350_block_read(wm8350
, WM8350_ALARM_SECONDS_MINUTES
, 4, time
);
155 tm
->tm_sec
= time
[0] & WM8350_RTC_ALMSECS_MASK
;
156 if (tm
->tm_sec
== WM8350_RTC_ALMSECS_MASK
)
159 tm
->tm_min
= time
[0] & WM8350_RTC_ALMMINS_MASK
;
160 if (tm
->tm_min
== WM8350_RTC_ALMMINS_MASK
)
163 tm
->tm_min
>>= WM8350_RTC_ALMMINS_SHIFT
;
165 tm
->tm_hour
= time
[1] & WM8350_RTC_ALMHRS_MASK
;
166 if (tm
->tm_hour
== WM8350_RTC_ALMHRS_MASK
)
169 tm
->tm_wday
= ((time
[1] >> WM8350_RTC_ALMDAY_SHIFT
) & 0x7) - 1;
173 tm
->tm_mon
= time
[2] & WM8350_RTC_ALMMTH_MASK
;
174 if (tm
->tm_mon
== WM8350_RTC_ALMMTH_MASK
)
177 tm
->tm_mon
= (tm
->tm_mon
>> WM8350_RTC_ALMMTH_SHIFT
) - 1;
179 tm
->tm_mday
= (time
[2] & WM8350_RTC_ALMDATE_MASK
);
180 if (tm
->tm_mday
== WM8350_RTC_ALMDATE_MASK
)
185 alrm
->enabled
= !(time
[3] & WM8350_RTC_ALMSTS
);
190 static int wm8350_rtc_stop_alarm(struct wm8350
*wm8350
)
192 int retries
= WM8350_SET_ALM_RETRIES
;
196 /* Set RTC_SET to stop the clock */
197 ret
= wm8350_set_bits(wm8350
, WM8350_RTC_TIME_CONTROL
,
202 /* Wait until confirmation of stopping */
204 rtc_ctrl
= wm8350_reg_read(wm8350
, WM8350_RTC_TIME_CONTROL
);
205 schedule_timeout_uninterruptible(msecs_to_jiffies(1));
206 } while (retries
-- && !(rtc_ctrl
& WM8350_RTC_ALMSTS
));
208 if (!(rtc_ctrl
& WM8350_RTC_ALMSTS
))
214 static int wm8350_rtc_start_alarm(struct wm8350
*wm8350
)
217 int retries
= WM8350_SET_ALM_RETRIES
;
220 ret
= wm8350_clear_bits(wm8350
, WM8350_RTC_TIME_CONTROL
,
225 /* Wait until confirmation */
227 rtc_ctrl
= wm8350_reg_read(wm8350
, WM8350_RTC_TIME_CONTROL
);
228 schedule_timeout_uninterruptible(msecs_to_jiffies(1));
229 } while (retries
-- && rtc_ctrl
& WM8350_RTC_ALMSTS
);
231 if (rtc_ctrl
& WM8350_RTC_ALMSTS
)
237 static int wm8350_rtc_alarm_irq_enable(struct device
*dev
,
238 unsigned int enabled
)
240 struct wm8350
*wm8350
= dev_get_drvdata(dev
);
243 return wm8350_rtc_start_alarm(wm8350
);
245 return wm8350_rtc_stop_alarm(wm8350
);
248 static int wm8350_rtc_setalarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
250 struct wm8350
*wm8350
= dev_get_drvdata(dev
);
251 struct rtc_time
*tm
= &alrm
->time
;
255 memset(time
, 0, sizeof(time
));
257 if (tm
->tm_sec
!= -1)
258 time
[0] |= tm
->tm_sec
;
260 time
[0] |= WM8350_RTC_ALMSECS_MASK
;
262 if (tm
->tm_min
!= -1)
263 time
[0] |= tm
->tm_min
<< WM8350_RTC_ALMMINS_SHIFT
;
265 time
[0] |= WM8350_RTC_ALMMINS_MASK
;
267 if (tm
->tm_hour
!= -1)
268 time
[1] |= tm
->tm_hour
;
270 time
[1] |= WM8350_RTC_ALMHRS_MASK
;
272 if (tm
->tm_wday
!= -1)
273 time
[1] |= (tm
->tm_wday
+ 1) << WM8350_RTC_ALMDAY_SHIFT
;
275 time
[1] |= WM8350_RTC_ALMDAY_MASK
;
277 if (tm
->tm_mday
!= -1)
278 time
[2] |= tm
->tm_mday
;
280 time
[2] |= WM8350_RTC_ALMDATE_MASK
;
282 if (tm
->tm_mon
!= -1)
283 time
[2] |= (tm
->tm_mon
+ 1) << WM8350_RTC_ALMMTH_SHIFT
;
285 time
[2] |= WM8350_RTC_ALMMTH_MASK
;
287 ret
= wm8350_rtc_stop_alarm(wm8350
);
291 /* Write time to RTC */
292 ret
= wm8350_block_write(wm8350
, WM8350_ALARM_SECONDS_MINUTES
,
298 ret
= wm8350_rtc_start_alarm(wm8350
);
303 static irqreturn_t
wm8350_rtc_alarm_handler(int irq
, void *data
)
305 struct wm8350
*wm8350
= data
;
306 struct rtc_device
*rtc
= wm8350
->rtc
.rtc
;
309 rtc_update_irq(rtc
, 1, RTC_IRQF
| RTC_AF
);
311 /* Make it one shot */
312 ret
= wm8350_set_bits(wm8350
, WM8350_RTC_TIME_CONTROL
,
315 dev_err(&(wm8350
->rtc
.pdev
->dev
),
316 "Failed to disable alarm: %d\n", ret
);
322 static irqreturn_t
wm8350_rtc_update_handler(int irq
, void *data
)
324 struct wm8350
*wm8350
= data
;
325 struct rtc_device
*rtc
= wm8350
->rtc
.rtc
;
327 rtc_update_irq(rtc
, 1, RTC_IRQF
| RTC_UF
);
332 static const struct rtc_class_ops wm8350_rtc_ops
= {
333 .read_time
= wm8350_rtc_readtime
,
334 .set_time
= wm8350_rtc_settime
,
335 .read_alarm
= wm8350_rtc_readalarm
,
336 .set_alarm
= wm8350_rtc_setalarm
,
337 .alarm_irq_enable
= wm8350_rtc_alarm_irq_enable
,
340 #ifdef CONFIG_PM_SLEEP
341 static int wm8350_rtc_suspend(struct device
*dev
)
343 struct platform_device
*pdev
= to_platform_device(dev
);
344 struct wm8350
*wm8350
= dev_get_drvdata(&pdev
->dev
);
348 reg
= wm8350_reg_read(wm8350
, WM8350_RTC_TIME_CONTROL
);
350 if (device_may_wakeup(&wm8350
->rtc
.pdev
->dev
) &&
351 reg
& WM8350_RTC_ALMSTS
) {
352 ret
= wm8350_rtc_stop_alarm(wm8350
);
354 dev_err(&pdev
->dev
, "Failed to stop RTC alarm: %d\n",
361 static int wm8350_rtc_resume(struct device
*dev
)
363 struct platform_device
*pdev
= to_platform_device(dev
);
364 struct wm8350
*wm8350
= dev_get_drvdata(&pdev
->dev
);
367 if (wm8350
->rtc
.alarm_enabled
) {
368 ret
= wm8350_rtc_start_alarm(wm8350
);
371 "Failed to restart RTC alarm: %d\n", ret
);
378 static int wm8350_rtc_probe(struct platform_device
*pdev
)
380 struct wm8350
*wm8350
= platform_get_drvdata(pdev
);
381 struct wm8350_rtc
*wm_rtc
= &wm8350
->rtc
;
385 timectl
= wm8350_reg_read(wm8350
, WM8350_RTC_TIME_CONTROL
);
386 if (timectl
& WM8350_RTC_BCD
) {
387 dev_err(&pdev
->dev
, "RTC BCD mode not supported\n");
390 if (timectl
& WM8350_RTC_12HR
) {
391 dev_err(&pdev
->dev
, "RTC 12 hour mode not supported\n");
395 /* enable the RTC if it's not already enabled */
396 power5
= wm8350_reg_read(wm8350
, WM8350_POWER_MGMT_5
);
397 if (!(power5
& WM8350_RTC_TICK_ENA
)) {
398 dev_info(wm8350
->dev
, "Starting RTC\n");
400 wm8350_reg_unlock(wm8350
);
402 ret
= wm8350_set_bits(wm8350
, WM8350_POWER_MGMT_5
,
403 WM8350_RTC_TICK_ENA
);
405 dev_err(&pdev
->dev
, "failed to enable RTC: %d\n", ret
);
409 wm8350_reg_lock(wm8350
);
412 if (timectl
& WM8350_RTC_STS
) {
415 ret
= wm8350_clear_bits(wm8350
, WM8350_RTC_TIME_CONTROL
,
418 dev_err(&pdev
->dev
, "failed to start: %d\n", ret
);
422 retries
= WM8350_SET_TIME_RETRIES
;
424 timectl
= wm8350_reg_read(wm8350
,
425 WM8350_RTC_TIME_CONTROL
);
426 } while (timectl
& WM8350_RTC_STS
&& --retries
);
429 dev_err(&pdev
->dev
, "failed to start: timeout\n");
434 device_init_wakeup(&pdev
->dev
, 1);
436 wm_rtc
->rtc
= devm_rtc_device_register(&pdev
->dev
, "wm8350",
437 &wm8350_rtc_ops
, THIS_MODULE
);
438 if (IS_ERR(wm_rtc
->rtc
)) {
439 ret
= PTR_ERR(wm_rtc
->rtc
);
440 dev_err(&pdev
->dev
, "failed to register RTC: %d\n", ret
);
444 wm8350_register_irq(wm8350
, WM8350_IRQ_RTC_SEC
,
445 wm8350_rtc_update_handler
, 0,
446 "RTC Seconds", wm8350
);
447 wm8350_mask_irq(wm8350
, WM8350_IRQ_RTC_SEC
);
449 wm8350_register_irq(wm8350
, WM8350_IRQ_RTC_ALM
,
450 wm8350_rtc_alarm_handler
, 0,
451 "RTC Alarm", wm8350
);
456 static int wm8350_rtc_remove(struct platform_device
*pdev
)
458 struct wm8350
*wm8350
= platform_get_drvdata(pdev
);
460 wm8350_free_irq(wm8350
, WM8350_IRQ_RTC_SEC
, wm8350
);
461 wm8350_free_irq(wm8350
, WM8350_IRQ_RTC_ALM
, wm8350
);
466 static SIMPLE_DEV_PM_OPS(wm8350_rtc_pm_ops
, wm8350_rtc_suspend
,
469 static struct platform_driver wm8350_rtc_driver
= {
470 .probe
= wm8350_rtc_probe
,
471 .remove
= wm8350_rtc_remove
,
473 .name
= "wm8350-rtc",
474 .pm
= &wm8350_rtc_pm_ops
,
478 module_platform_driver(wm8350_rtc_driver
);
480 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
481 MODULE_DESCRIPTION("RTC driver for the WM8350");
482 MODULE_LICENSE("GPL");
483 MODULE_ALIAS("platform:wm8350-rtc");