Lines Matching +full:at91rm9200 +full:- +full:rtc

1 // SPDX-License-Identifier: GPL-2.0+
7 * Based on rtc-at91rm9200.c by Rick Bronson
20 #include <linux/rtc.h>
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
35 * epoch (1970-Jan-1 00:00:00 UTC). Some systems also include the
38 * The RTC's value is the RTT counter plus that offset. The RTC's alarm
42 * choose from, or a "real" RTC module. All systems have multiple GPBR
43 * registers available, likewise usable for more than "RTC" support.
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 */
81 #define rtt_readl(rtc, field) \ argument
82 readl((rtc)->rtt + AT91_RTT_ ## field)
83 #define rtt_writel(rtc, field, val) \ argument
84 writel((val), (rtc)->rtt + AT91_RTT_ ## field)
86 static inline unsigned int gpbr_readl(struct sam9_rtc *rtc) in gpbr_readl() argument
90 regmap_read(rtc->gpbr, rtc->gpbr_offset, &val); in gpbr_readl()
95 static inline void gpbr_writel(struct sam9_rtc *rtc, unsigned int val) in gpbr_writel() argument
97 regmap_write(rtc->gpbr, rtc->gpbr_offset, val); in gpbr_writel()
101 * Read current time and date in RTC
105 struct sam9_rtc *rtc = dev_get_drvdata(dev); in at91_rtc_readtime() local
110 offset = gpbr_readl(rtc); in at91_rtc_readtime()
112 return -EILSEQ; in at91_rtc_readtime()
115 secs = rtt_readl(rtc, VR); in at91_rtc_readtime()
116 secs2 = rtt_readl(rtc, VR); in at91_rtc_readtime()
118 secs = rtt_readl(rtc, VR); in at91_rtc_readtime()
128 * Set current time and date in RTC
132 struct sam9_rtc *rtc = dev_get_drvdata(dev); in at91_rtc_settime() local
140 mr = rtt_readl(rtc, MR); in at91_rtc_settime()
143 rtt_writel(rtc, MR, mr & ~(AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN)); in at91_rtc_settime()
146 offset = gpbr_readl(rtc); in at91_rtc_settime()
150 gpbr_writel(rtc, secs); in at91_rtc_settime()
153 alarm = rtt_readl(rtc, AR); in at91_rtc_settime()
157 alarm += (offset - secs); in at91_rtc_settime()
160 alarm -= (secs - offset); in at91_rtc_settime()
166 rtt_writel(rtc, AR, alarm); in at91_rtc_settime()
169 /* reset the timer, and re-enable interrupts */ in at91_rtc_settime()
170 rtt_writel(rtc, MR, mr | AT91_RTT_RTTRST); in at91_rtc_settime()
177 struct sam9_rtc *rtc = dev_get_drvdata(dev); in at91_rtc_readalarm() local
178 struct rtc_time *tm = &alrm->time; in at91_rtc_readalarm()
179 u32 alarm = rtt_readl(rtc, AR); in at91_rtc_readalarm()
182 offset = gpbr_readl(rtc); in at91_rtc_readalarm()
184 return -EILSEQ; in at91_rtc_readalarm()
192 if (rtt_readl(rtc, MR) & AT91_RTT_ALMIEN) in at91_rtc_readalarm()
193 alrm->enabled = 1; in at91_rtc_readalarm()
201 struct sam9_rtc *rtc = dev_get_drvdata(dev); in at91_rtc_setalarm() local
202 struct rtc_time *tm = &alrm->time; in at91_rtc_setalarm()
209 offset = gpbr_readl(rtc); in at91_rtc_setalarm()
212 return -EILSEQ; in at91_rtc_setalarm()
214 mr = rtt_readl(rtc, MR); in at91_rtc_setalarm()
215 rtt_writel(rtc, MR, mr & ~AT91_RTT_ALMIEN); in at91_rtc_setalarm()
219 rtt_writel(rtc, AR, ALARM_DISABLED); in at91_rtc_setalarm()
224 rtt_writel(rtc, AR, secs - offset); in at91_rtc_setalarm()
225 if (alrm->enabled) in at91_rtc_setalarm()
226 rtt_writel(rtc, MR, mr | AT91_RTT_ALMIEN); in at91_rtc_setalarm()
235 struct sam9_rtc *rtc = dev_get_drvdata(dev); in at91_rtc_alarm_irq_enable() local
236 u32 mr = rtt_readl(rtc, MR); in at91_rtc_alarm_irq_enable()
240 rtt_writel(rtc, MR, mr | AT91_RTT_ALMIEN); in at91_rtc_alarm_irq_enable()
242 rtt_writel(rtc, MR, mr & ~AT91_RTT_ALMIEN); in at91_rtc_alarm_irq_enable()
247 * Provide additional RTC information in /proc/driver/rtc
251 struct sam9_rtc *rtc = dev_get_drvdata(dev); in at91_rtc_proc() local
252 u32 mr = rtt_readl(rtc, MR); in at91_rtc_proc()
259 static irqreturn_t at91_rtc_cache_events(struct sam9_rtc *rtc) in at91_rtc_cache_events() argument
266 mr = rtt_readl(rtc, MR) & (AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN); in at91_rtc_cache_events()
267 sr = rtt_readl(rtc, SR) & (mr >> 16); in at91_rtc_cache_events()
273 rtc->events |= (RTC_AF | RTC_IRQF); in at91_rtc_cache_events()
277 rtc->events |= (RTC_UF | RTC_IRQF); in at91_rtc_cache_events()
282 static void at91_rtc_flush_events(struct sam9_rtc *rtc) in at91_rtc_flush_events() argument
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()
295 * IRQ handler for the RTC
299 struct sam9_rtc *rtc = _rtc; in at91_rtc_interrupt() local
302 spin_lock(&rtc->lock); in at91_rtc_interrupt()
304 ret = at91_rtc_cache_events(rtc); in at91_rtc_interrupt()
307 if (rtc->suspended) { in at91_rtc_interrupt()
309 rtt_writel(rtc, MR, in at91_rtc_interrupt()
310 rtt_readl(rtc, MR) & in at91_rtc_interrupt()
315 at91_rtc_flush_events(rtc); in at91_rtc_interrupt()
318 spin_unlock(&rtc->lock); in at91_rtc_interrupt()
333 * Initialize and install RTC driver
337 struct sam9_rtc *rtc; in at91_rtc_probe() local
347 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL); in at91_rtc_probe()
348 if (!rtc) 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()
358 platform_set_drvdata(pdev, rtc); 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()
372 rtc->gpbr_offset = args.args[0]; in at91_rtc_probe()
373 if (IS_ERR(rtc->gpbr)) { in at91_rtc_probe()
374 dev_err(&pdev->dev, "failed to retrieve gpbr regmap, aborting.\n"); in at91_rtc_probe()
375 return -ENOMEM; in at91_rtc_probe()
378 rtc->sclk = devm_clk_get(&pdev->dev, NULL); in at91_rtc_probe()
379 if (IS_ERR(rtc->sclk)) in at91_rtc_probe()
380 return PTR_ERR(rtc->sclk); in at91_rtc_probe()
382 ret = clk_prepare_enable(rtc->sclk); in at91_rtc_probe()
384 dev_err(&pdev->dev, "Could not enable slow clock\n"); in at91_rtc_probe()
388 sclk_rate = clk_get_rate(rtc->sclk); in at91_rtc_probe()
390 dev_err(&pdev->dev, "Invalid slow clock rate\n"); in at91_rtc_probe()
391 ret = -EINVAL; in at91_rtc_probe()
395 mr = rtt_readl(rtc, MR); in at91_rtc_probe()
397 /* unless RTT is counting at 1 Hz, re-initialize it */ in at91_rtc_probe()
400 gpbr_writel(rtc, 0); in at91_rtc_probe()
405 rtt_writel(rtc, MR, mr); in at91_rtc_probe()
407 rtc->rtcdev = devm_rtc_allocate_device(&pdev->dev); in at91_rtc_probe()
408 if (IS_ERR(rtc->rtcdev)) { in at91_rtc_probe()
409 ret = PTR_ERR(rtc->rtcdev); in at91_rtc_probe()
413 rtc->rtcdev->ops = &at91_rtc_ops; in at91_rtc_probe()
414 rtc->rtcdev->range_max = U32_MAX; in at91_rtc_probe()
417 ret = devm_request_irq(&pdev->dev, rtc->irq, at91_rtc_interrupt, in at91_rtc_probe()
419 dev_name(&rtc->rtcdev->dev), rtc); in at91_rtc_probe()
421 dev_dbg(&pdev->dev, "can't share IRQ %d?\n", rtc->irq); in at91_rtc_probe()
428 * clock, discrete RTC, etc in at91_rtc_probe()
431 if (gpbr_readl(rtc) == 0) in at91_rtc_probe()
432 dev_warn(&pdev->dev, "%s: SET TIME!\n", in at91_rtc_probe()
433 dev_name(&rtc->rtcdev->dev)); in at91_rtc_probe()
435 return devm_rtc_register_device(rtc->rtcdev); in at91_rtc_probe()
438 clk_disable_unprepare(rtc->sclk); in at91_rtc_probe()
444 * Disable and remove the RTC driver
448 struct sam9_rtc *rtc = platform_get_drvdata(pdev); in at91_rtc_remove() local
449 u32 mr = rtt_readl(rtc, MR); in at91_rtc_remove()
452 rtt_writel(rtc, MR, mr & ~(AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN)); in at91_rtc_remove()
454 clk_disable_unprepare(rtc->sclk); in at91_rtc_remove()
459 struct sam9_rtc *rtc = platform_get_drvdata(pdev); in at91_rtc_shutdown() local
460 u32 mr = rtt_readl(rtc, MR); in at91_rtc_shutdown()
462 rtc->imr = mr & (AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN); in at91_rtc_shutdown()
463 rtt_writel(rtc, MR, mr & ~rtc->imr); in at91_rtc_shutdown()
468 /* AT91SAM9 RTC Power management control */
472 struct sam9_rtc *rtc = dev_get_drvdata(dev); in at91_rtc_suspend() local
473 u32 mr = rtt_readl(rtc, MR); in at91_rtc_suspend()
479 rtc->imr = mr & (AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN); in at91_rtc_suspend()
480 if (rtc->imr) { in at91_rtc_suspend()
484 enable_irq_wake(rtc->irq); in at91_rtc_suspend()
485 spin_lock_irqsave(&rtc->lock, flags); in at91_rtc_suspend()
486 rtc->suspended = true; in at91_rtc_suspend()
487 spin_unlock_irqrestore(&rtc->lock, flags); in at91_rtc_suspend()
490 rtt_writel(rtc, MR, mr & ~AT91_RTT_RTTINCIEN); in at91_rtc_suspend()
492 rtt_writel(rtc, MR, mr & ~rtc->imr); in at91_rtc_suspend()
501 struct sam9_rtc *rtc = dev_get_drvdata(dev); in at91_rtc_resume() local
504 if (rtc->imr) { in at91_rtc_resume()
508 disable_irq_wake(rtc->irq); in at91_rtc_resume()
509 mr = rtt_readl(rtc, MR); in at91_rtc_resume()
510 rtt_writel(rtc, MR, mr | rtc->imr); in at91_rtc_resume()
512 spin_lock_irqsave(&rtc->lock, flags); in at91_rtc_resume()
513 rtc->suspended = false; in at91_rtc_resume()
514 at91_rtc_cache_events(rtc); in at91_rtc_resume()
515 at91_rtc_flush_events(rtc); in at91_rtc_resume()
516 spin_unlock_irqrestore(&rtc->lock, flags); in at91_rtc_resume()
526 { .compatible = "atmel,at91sam9260-rtt" },
536 .name = "rtc-at91sam9",
545 MODULE_DESCRIPTION("RTC driver for Atmel AT91SAM9x");