Lines Matching +full:real +full:- +full:time
1 // SPDX-License-Identifier: GPL-2.0+
3 * "RTT as Real Time Clock" driver for AT91SAM9 SoC family
7 * Based on rtc-at91rm9200.c by Rick Bronson
23 #include <linux/time.h>
28 * to implement the Real Time Clock interfaces
30 * - A "Real-time Timer" (RTT) counts up in seconds from a base time.
33 * - One of the "General Purpose Backup Registers" (GPBRs) holds the
34 * base time, normally an offset from the beginning of the POSIX
35 * epoch (1970-Jan-1 00:00:00 UTC). Some systems also include the
42 * choose from, or a "real" RTC module. All systems have multiple GPBR
46 #define AT91_RTT_MR 0x00 /* Real-time Mode Register */
52 #define AT91_RTT_AR 0x04 /* Real-time Alarm Register */
55 #define AT91_RTT_VR 0x08 /* Real-time Value Register */
56 #define AT91_RTT_CRTV (0xffffffff) /* Current Real-time Value */
58 #define AT91_RTT_SR 0x0c /* Real-time Status Register */
82 readl((rtc)->rtt + AT91_RTT_ ## field)
84 writel((val), (rtc)->rtt + AT91_RTT_ ## field)
90 regmap_read(rtc->gpbr, rtc->gpbr_offset, &val); in gpbr_readl()
97 regmap_write(rtc->gpbr, rtc->gpbr_offset, val); in gpbr_writel()
101 * Read current time and date in RTC
109 /* read current time offset */ in at91_rtc_readtime()
112 return -EILSEQ; in at91_rtc_readtime()
128 * Set current time and date in RTC
145 /* read current time offset */ in at91_rtc_settime()
148 /* store the new base time in a battery backup register */ in at91_rtc_settime()
152 /* adjust the alarm time for the new base */ in at91_rtc_settime()
156 /* time jumped backwards, increase time until alarm */ in at91_rtc_settime()
157 alarm += (offset - secs); in at91_rtc_settime()
159 /* time jumped forwards, decrease time until alarm */ in at91_rtc_settime()
160 alarm -= (secs - offset); in at91_rtc_settime()
162 /* time jumped past the alarm, disable alarm */ in at91_rtc_settime()
169 /* reset the timer, and re-enable interrupts */ in at91_rtc_settime()
178 struct rtc_time *tm = &alrm->time; in at91_rtc_readalarm()
184 return -EILSEQ; in at91_rtc_readalarm()
193 alrm->enabled = 1; in at91_rtc_readalarm()
202 struct rtc_time *tm = &alrm->time; in at91_rtc_setalarm()
211 /* time is not set */ in at91_rtc_setalarm()
212 return -EILSEQ; in at91_rtc_setalarm()
224 rtt_writel(rtc, AR, secs - offset); in at91_rtc_setalarm()
225 if (alrm->enabled) in at91_rtc_setalarm()
273 rtc->events |= (RTC_AF | RTC_IRQF); in at91_rtc_cache_events()
277 rtc->events |= (RTC_UF | RTC_IRQF); in at91_rtc_cache_events()
284 if (!rtc->events) in at91_rtc_flush_events()
287 rtc_update_irq(rtc->rtcdev, 1, rtc->events); in at91_rtc_flush_events()
288 rtc->events = 0; in at91_rtc_flush_events()
291 rtc->events >> 8, rtc->events & 0x000000FF); in at91_rtc_flush_events()
302 spin_lock(&rtc->lock); in at91_rtc_interrupt()
307 if (rtc->suspended) { in at91_rtc_interrupt()
318 spin_unlock(&rtc->lock); in at91_rtc_interrupt()
347 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL); in at91_rtc_probe()
349 return -ENOMEM; in at91_rtc_probe()
351 spin_lock_init(&rtc->lock); in at91_rtc_probe()
352 rtc->irq = irq; in at91_rtc_probe()
355 if (!device_can_wakeup(&pdev->dev)) in at91_rtc_probe()
356 device_init_wakeup(&pdev->dev, 1); in at91_rtc_probe()
360 rtc->rtt = devm_platform_ioremap_resource(pdev, 0); in at91_rtc_probe()
361 if (IS_ERR(rtc->rtt)) in at91_rtc_probe()
362 return PTR_ERR(rtc->rtt); in at91_rtc_probe()
364 ret = of_parse_phandle_with_fixed_args(pdev->dev.of_node, in at91_rtc_probe()
365 "atmel,rtt-rtc-time-reg", 1, 0, in at91_rtc_probe()
370 rtc->gpbr = syscon_node_to_regmap(args.np); in at91_rtc_probe()
371 rtc->gpbr_offset = args.args[0]; in at91_rtc_probe()
372 if (IS_ERR(rtc->gpbr)) { in at91_rtc_probe()
373 dev_err(&pdev->dev, "failed to retrieve gpbr regmap, aborting.\n"); in at91_rtc_probe()
374 return -ENOMEM; in at91_rtc_probe()
377 rtc->sclk = devm_clk_get(&pdev->dev, NULL); in at91_rtc_probe()
378 if (IS_ERR(rtc->sclk)) in at91_rtc_probe()
379 return PTR_ERR(rtc->sclk); in at91_rtc_probe()
381 ret = clk_prepare_enable(rtc->sclk); in at91_rtc_probe()
383 dev_err(&pdev->dev, "Could not enable slow clock\n"); in at91_rtc_probe()
387 sclk_rate = clk_get_rate(rtc->sclk); in at91_rtc_probe()
389 dev_err(&pdev->dev, "Invalid slow clock rate\n"); in at91_rtc_probe()
390 ret = -EINVAL; in at91_rtc_probe()
396 /* unless RTT is counting at 1 Hz, re-initialize it */ in at91_rtc_probe()
406 rtc->rtcdev = devm_rtc_allocate_device(&pdev->dev); in at91_rtc_probe()
407 if (IS_ERR(rtc->rtcdev)) { in at91_rtc_probe()
408 ret = PTR_ERR(rtc->rtcdev); in at91_rtc_probe()
412 rtc->rtcdev->ops = &at91_rtc_ops; in at91_rtc_probe()
413 rtc->rtcdev->range_max = U32_MAX; in at91_rtc_probe()
416 ret = devm_request_irq(&pdev->dev, rtc->irq, at91_rtc_interrupt, in at91_rtc_probe()
418 dev_name(&rtc->rtcdev->dev), rtc); in at91_rtc_probe()
420 dev_dbg(&pdev->dev, "can't share IRQ %d?\n", rtc->irq); in at91_rtc_probe()
426 * initialize the time from some external source like a GPS, wall in at91_rtc_probe()
431 dev_warn(&pdev->dev, "%s: SET TIME!\n", in at91_rtc_probe()
432 dev_name(&rtc->rtcdev->dev)); in at91_rtc_probe()
434 return rtc_register_device(rtc->rtcdev); in at91_rtc_probe()
437 clk_disable_unprepare(rtc->sclk); in at91_rtc_probe()
453 clk_disable_unprepare(rtc->sclk); in at91_rtc_remove()
463 rtc->imr = mr & (AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN); in at91_rtc_shutdown()
464 rtt_writel(rtc, MR, mr & ~rtc->imr); in at91_rtc_shutdown()
480 rtc->imr = mr & (AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN); in at91_rtc_suspend()
481 if (rtc->imr) { in at91_rtc_suspend()
485 enable_irq_wake(rtc->irq); in at91_rtc_suspend()
486 spin_lock_irqsave(&rtc->lock, flags); in at91_rtc_suspend()
487 rtc->suspended = true; in at91_rtc_suspend()
488 spin_unlock_irqrestore(&rtc->lock, flags); in at91_rtc_suspend()
493 rtt_writel(rtc, MR, mr & ~rtc->imr); in at91_rtc_suspend()
505 if (rtc->imr) { in at91_rtc_resume()
509 disable_irq_wake(rtc->irq); in at91_rtc_resume()
511 rtt_writel(rtc, MR, mr | rtc->imr); in at91_rtc_resume()
513 spin_lock_irqsave(&rtc->lock, flags); in at91_rtc_resume()
514 rtc->suspended = false; in at91_rtc_resume()
517 spin_unlock_irqrestore(&rtc->lock, flags); in at91_rtc_resume()
527 { .compatible = "atmel,at91sam9260-rtt" },
537 .name = "rtc-at91sam9",