1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Real Time Clock driver for Wolfson Microelectronics WM8350
5 * Copyright (C) 2007, 2008 Wolfson Microelectronics PLC.
7 * Author: Liam Girdwood
8 * linux@wolfsonmicro.com
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/time.h>
14 #include <linux/rtc.h>
15 #include <linux/bcd.h>
16 #include <linux/interrupt.h>
17 #include <linux/ioctl.h>
18 #include <linux/completion.h>
19 #include <linux/mfd/wm8350/rtc.h>
20 #include <linux/mfd/wm8350/core.h>
21 #include <linux/delay.h>
22 #include <linux/platform_device.h>
24 #define WM8350_SET_ALM_RETRIES 5
25 #define WM8350_SET_TIME_RETRIES 5
26 #define WM8350_GET_TIME_RETRIES 5
29 * Read current time and date in RTC
31 static int wm8350_rtc_readtime(struct device *dev, struct rtc_time *tm)
33 struct wm8350 *wm8350 = dev_get_drvdata(dev);
34 u16 time1[4], time2[4];
35 int retries = WM8350_GET_TIME_RETRIES, ret;
38 * Read the time twice and compare.
39 * If time1 == time2, then time is valid else retry.
42 ret = wm8350_block_read(wm8350, WM8350_RTC_SECONDS_MINUTES,
46 ret = wm8350_block_read(wm8350, WM8350_RTC_SECONDS_MINUTES,
51 if (memcmp(time1, time2, sizeof(time1)) == 0) {
52 tm->tm_sec = time1[0] & WM8350_RTC_SECS_MASK;
54 tm->tm_min = (time1[0] & WM8350_RTC_MINS_MASK)
55 >> WM8350_RTC_MINS_SHIFT;
57 tm->tm_hour = time1[1] & WM8350_RTC_HRS_MASK;
59 tm->tm_wday = ((time1[1] >> WM8350_RTC_DAY_SHIFT)
62 tm->tm_mon = ((time1[2] & WM8350_RTC_MTH_MASK)
63 >> WM8350_RTC_MTH_SHIFT) - 1;
65 tm->tm_mday = (time1[2] & WM8350_RTC_DATE_MASK);
67 tm->tm_year = ((time1[3] & WM8350_RTC_YHUNDREDS_MASK)
68 >> WM8350_RTC_YHUNDREDS_SHIFT) * 100;
69 tm->tm_year += time1[3] & WM8350_RTC_YUNITS_MASK;
71 tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon,
75 dev_dbg(dev, "Read (%d left): %04x %04x %04x %04x\n",
77 time1[0], time1[1], time1[2], time1[3]);
83 dev_err(dev, "timed out reading RTC time\n");
88 * Set current time and date in RTC
90 static int wm8350_rtc_settime(struct device *dev, struct rtc_time *tm)
92 struct wm8350 *wm8350 = dev_get_drvdata(dev);
95 int ret, retries = WM8350_SET_TIME_RETRIES;
98 time[0] |= tm->tm_min << WM8350_RTC_MINS_SHIFT;
99 time[1] = tm->tm_hour;
100 time[1] |= (tm->tm_wday + 1) << WM8350_RTC_DAY_SHIFT;
101 time[2] = tm->tm_mday;
102 time[2] |= (tm->tm_mon + 1) << WM8350_RTC_MTH_SHIFT;
103 time[3] = ((tm->tm_year + 1900) / 100) << WM8350_RTC_YHUNDREDS_SHIFT;
104 time[3] |= (tm->tm_year + 1900) % 100;
106 dev_dbg(dev, "Setting: %04x %04x %04x %04x\n",
107 time[0], time[1], time[2], time[3]);
109 /* Set RTC_SET to stop the clock */
110 ret = wm8350_set_bits(wm8350, WM8350_RTC_TIME_CONTROL, WM8350_RTC_SET);
114 /* Wait until confirmation of stopping */
116 rtc_ctrl = wm8350_reg_read(wm8350, WM8350_RTC_TIME_CONTROL);
117 schedule_timeout_uninterruptible(msecs_to_jiffies(1));
118 } while (--retries && !(rtc_ctrl & WM8350_RTC_STS));
121 dev_err(dev, "timed out on set confirmation\n");
125 /* Write time to RTC */
126 ret = wm8350_block_write(wm8350, WM8350_RTC_SECONDS_MINUTES, 4, time);
130 /* Clear RTC_SET to start the clock */
131 ret = wm8350_clear_bits(wm8350, WM8350_RTC_TIME_CONTROL,
137 * Read alarm time and date in RTC
139 static int wm8350_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
141 struct wm8350 *wm8350 = dev_get_drvdata(dev);
142 struct rtc_time *tm = &alrm->time;
146 ret = wm8350_block_read(wm8350, WM8350_ALARM_SECONDS_MINUTES, 4, time);
150 tm->tm_sec = time[0] & WM8350_RTC_ALMSECS_MASK;
151 if (tm->tm_sec == WM8350_RTC_ALMSECS_MASK)
154 tm->tm_min = time[0] & WM8350_RTC_ALMMINS_MASK;
155 if (tm->tm_min == WM8350_RTC_ALMMINS_MASK)
158 tm->tm_min >>= WM8350_RTC_ALMMINS_SHIFT;
160 tm->tm_hour = time[1] & WM8350_RTC_ALMHRS_MASK;
161 if (tm->tm_hour == WM8350_RTC_ALMHRS_MASK)
164 tm->tm_wday = ((time[1] >> WM8350_RTC_ALMDAY_SHIFT) & 0x7) - 1;
168 tm->tm_mon = time[2] & WM8350_RTC_ALMMTH_MASK;
169 if (tm->tm_mon == WM8350_RTC_ALMMTH_MASK)
172 tm->tm_mon = (tm->tm_mon >> WM8350_RTC_ALMMTH_SHIFT) - 1;
174 tm->tm_mday = (time[2] & WM8350_RTC_ALMDATE_MASK);
175 if (tm->tm_mday == WM8350_RTC_ALMDATE_MASK)
180 alrm->enabled = !(time[3] & WM8350_RTC_ALMSTS);
185 static int wm8350_rtc_stop_alarm(struct wm8350 *wm8350)
187 int retries = WM8350_SET_ALM_RETRIES;
191 /* Set RTC_SET to stop the clock */
192 ret = wm8350_set_bits(wm8350, WM8350_RTC_TIME_CONTROL,
197 /* Wait until confirmation of stopping */
199 rtc_ctrl = wm8350_reg_read(wm8350, WM8350_RTC_TIME_CONTROL);
200 schedule_timeout_uninterruptible(msecs_to_jiffies(1));
201 } while (retries-- && !(rtc_ctrl & WM8350_RTC_ALMSTS));
203 if (!(rtc_ctrl & WM8350_RTC_ALMSTS))
209 static int wm8350_rtc_start_alarm(struct wm8350 *wm8350)
212 int retries = WM8350_SET_ALM_RETRIES;
215 ret = wm8350_clear_bits(wm8350, WM8350_RTC_TIME_CONTROL,
220 /* Wait until confirmation */
222 rtc_ctrl = wm8350_reg_read(wm8350, WM8350_RTC_TIME_CONTROL);
223 schedule_timeout_uninterruptible(msecs_to_jiffies(1));
224 } while (retries-- && rtc_ctrl & WM8350_RTC_ALMSTS);
226 if (rtc_ctrl & WM8350_RTC_ALMSTS)
232 static int wm8350_rtc_alarm_irq_enable(struct device *dev,
233 unsigned int enabled)
235 struct wm8350 *wm8350 = dev_get_drvdata(dev);
238 return wm8350_rtc_start_alarm(wm8350);
240 return wm8350_rtc_stop_alarm(wm8350);
243 static int wm8350_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
245 struct wm8350 *wm8350 = dev_get_drvdata(dev);
246 struct rtc_time *tm = &alrm->time;
250 memset(time, 0, sizeof(time));
252 if (tm->tm_sec != -1)
253 time[0] |= tm->tm_sec;
255 time[0] |= WM8350_RTC_ALMSECS_MASK;
257 if (tm->tm_min != -1)
258 time[0] |= tm->tm_min << WM8350_RTC_ALMMINS_SHIFT;
260 time[0] |= WM8350_RTC_ALMMINS_MASK;
262 if (tm->tm_hour != -1)
263 time[1] |= tm->tm_hour;
265 time[1] |= WM8350_RTC_ALMHRS_MASK;
267 if (tm->tm_wday != -1)
268 time[1] |= (tm->tm_wday + 1) << WM8350_RTC_ALMDAY_SHIFT;
270 time[1] |= WM8350_RTC_ALMDAY_MASK;
272 if (tm->tm_mday != -1)
273 time[2] |= tm->tm_mday;
275 time[2] |= WM8350_RTC_ALMDATE_MASK;
277 if (tm->tm_mon != -1)
278 time[2] |= (tm->tm_mon + 1) << WM8350_RTC_ALMMTH_SHIFT;
280 time[2] |= WM8350_RTC_ALMMTH_MASK;
282 ret = wm8350_rtc_stop_alarm(wm8350);
286 /* Write time to RTC */
287 ret = wm8350_block_write(wm8350, WM8350_ALARM_SECONDS_MINUTES,
293 ret = wm8350_rtc_start_alarm(wm8350);
298 static irqreturn_t wm8350_rtc_alarm_handler(int irq, void *data)
300 struct wm8350 *wm8350 = data;
301 struct rtc_device *rtc = wm8350->rtc.rtc;
304 rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF);
306 /* Make it one shot */
307 ret = wm8350_set_bits(wm8350, WM8350_RTC_TIME_CONTROL,
310 dev_err(&(wm8350->rtc.pdev->dev),
311 "Failed to disable alarm: %d\n", ret);
317 static irqreturn_t wm8350_rtc_update_handler(int irq, void *data)
319 struct wm8350 *wm8350 = data;
320 struct rtc_device *rtc = wm8350->rtc.rtc;
322 rtc_update_irq(rtc, 1, RTC_IRQF | RTC_UF);
327 static const struct rtc_class_ops wm8350_rtc_ops = {
328 .read_time = wm8350_rtc_readtime,
329 .set_time = wm8350_rtc_settime,
330 .read_alarm = wm8350_rtc_readalarm,
331 .set_alarm = wm8350_rtc_setalarm,
332 .alarm_irq_enable = wm8350_rtc_alarm_irq_enable,
335 #ifdef CONFIG_PM_SLEEP
336 static int wm8350_rtc_suspend(struct device *dev)
338 struct wm8350 *wm8350 = dev_get_drvdata(dev);
342 reg = wm8350_reg_read(wm8350, WM8350_RTC_TIME_CONTROL);
344 if (device_may_wakeup(&wm8350->rtc.pdev->dev) &&
345 reg & WM8350_RTC_ALMSTS) {
346 ret = wm8350_rtc_stop_alarm(wm8350);
348 dev_err(dev, "Failed to stop RTC alarm: %d\n", ret);
354 static int wm8350_rtc_resume(struct device *dev)
356 struct wm8350 *wm8350 = dev_get_drvdata(dev);
359 if (wm8350->rtc.alarm_enabled) {
360 ret = wm8350_rtc_start_alarm(wm8350);
362 dev_err(dev, "Failed to restart RTC alarm: %d\n", ret);
369 static int wm8350_rtc_probe(struct platform_device *pdev)
371 struct wm8350 *wm8350 = platform_get_drvdata(pdev);
372 struct wm8350_rtc *wm_rtc = &wm8350->rtc;
376 timectl = wm8350_reg_read(wm8350, WM8350_RTC_TIME_CONTROL);
377 if (timectl & WM8350_RTC_BCD) {
378 dev_err(&pdev->dev, "RTC BCD mode not supported\n");
381 if (timectl & WM8350_RTC_12HR) {
382 dev_err(&pdev->dev, "RTC 12 hour mode not supported\n");
386 /* enable the RTC if it's not already enabled */
387 power5 = wm8350_reg_read(wm8350, WM8350_POWER_MGMT_5);
388 if (!(power5 & WM8350_RTC_TICK_ENA)) {
389 dev_info(wm8350->dev, "Starting RTC\n");
391 wm8350_reg_unlock(wm8350);
393 ret = wm8350_set_bits(wm8350, WM8350_POWER_MGMT_5,
394 WM8350_RTC_TICK_ENA);
396 dev_err(&pdev->dev, "failed to enable RTC: %d\n", ret);
400 wm8350_reg_lock(wm8350);
403 if (timectl & WM8350_RTC_STS) {
406 ret = wm8350_clear_bits(wm8350, WM8350_RTC_TIME_CONTROL,
409 dev_err(&pdev->dev, "failed to start: %d\n", ret);
413 retries = WM8350_SET_TIME_RETRIES;
415 timectl = wm8350_reg_read(wm8350,
416 WM8350_RTC_TIME_CONTROL);
417 } while (timectl & WM8350_RTC_STS && --retries);
420 dev_err(&pdev->dev, "failed to start: timeout\n");
425 device_init_wakeup(&pdev->dev, 1);
427 wm_rtc->rtc = devm_rtc_device_register(&pdev->dev, "wm8350",
428 &wm8350_rtc_ops, THIS_MODULE);
429 if (IS_ERR(wm_rtc->rtc)) {
430 ret = PTR_ERR(wm_rtc->rtc);
431 dev_err(&pdev->dev, "failed to register RTC: %d\n", ret);
435 ret = wm8350_register_irq(wm8350, WM8350_IRQ_RTC_SEC,
436 wm8350_rtc_update_handler, 0,
437 "RTC Seconds", wm8350);
441 wm8350_mask_irq(wm8350, WM8350_IRQ_RTC_SEC);
443 ret = wm8350_register_irq(wm8350, WM8350_IRQ_RTC_ALM,
444 wm8350_rtc_alarm_handler, 0,
445 "RTC Alarm", wm8350);
447 wm8350_free_irq(wm8350, WM8350_IRQ_RTC_SEC, wm8350);
454 static int wm8350_rtc_remove(struct platform_device *pdev)
456 struct wm8350 *wm8350 = platform_get_drvdata(pdev);
458 wm8350_free_irq(wm8350, WM8350_IRQ_RTC_SEC, wm8350);
459 wm8350_free_irq(wm8350, WM8350_IRQ_RTC_ALM, wm8350);
464 static SIMPLE_DEV_PM_OPS(wm8350_rtc_pm_ops, wm8350_rtc_suspend,
467 static struct platform_driver wm8350_rtc_driver = {
468 .probe = wm8350_rtc_probe,
469 .remove = wm8350_rtc_remove,
471 .name = "wm8350-rtc",
472 .pm = &wm8350_rtc_pm_ops,
476 module_platform_driver(wm8350_rtc_driver);
478 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
479 MODULE_DESCRIPTION("RTC driver for the WM8350");
480 MODULE_LICENSE("GPL");
481 MODULE_ALIAS("platform:wm8350-rtc");