Lines Matching +full:am62 +full:- +full:rtc
1 // SPDX-License-Identifier: GPL-2.0
3 * Texas Instruments K3 RTC driver
5 * Copyright (C) 2021-2022 Texas Instruments Incorporated - https://www.ti.com/
17 #include <linux/rtc.h>
46 #define K3RTC_MIN_OFFSET (-277761)
50 .name = "peripheral-registers",
107 * struct ti_k3_rtc - Private data for ti-k3-rtc
111 * @rtc_dev: rtc device
112 * @regmap: rtc mmio regmap
113 * @r_fields: rtc register fields
129 ret = regmap_field_read(priv->r_fields[f], &val); in k3rtc_field_read()
141 regmap_field_write(priv->r_fields[f], val); in k3rtc_field_write()
145 * k3rtc_fence - Ensure a register sync took place between the two domains
148 * Return: 0 if the sync took place, else returns -ETIMEDOUT
154 ret = regmap_field_read_poll_timeout(priv->r_fields[K3RTC_PEND], ret, in k3rtc_fence()
155 !ret, 2, priv->sync_timeout_us); in k3rtc_fence()
183 ret = regmap_field_read_poll_timeout(priv->r_fields[K3RTC_UNLOCK], ret, in k3rtc_unlock_rtc()
184 ret, 2, priv->sync_timeout_us); in k3rtc_unlock_rtc()
190 * This is the list of SoCs affected by TI's i2327 errata causing the RTC
191 * state-machine to break if not unlocked fast enough during boot. These
193 * boot-flow before we (Linux) can use this device.
206 * HWBUG: The compare state machine is broken if the RTC module in k3rtc_configure()
207 * is NOT unlocked in under one second of boot - which is pretty long in k3rtc_configure()
208 * time from the perspective of Linux driver (module load, u-boot in k3rtc_configure()
211 * In such occurrence, it is assumed that the RTC module is unusable in k3rtc_configure()
219 return -EFAULT; in k3rtc_configure()
237 usleep_range(priv->sync_timeout_us, priv->sync_timeout_us + 5); in k3rtc_configure()
243 "Failed fence osc_dep enable(%d) - is 32k clk working?!\n", ret); in k3rtc_configure()
286 * Read operation on LSW will freeze the RTC, so to update in ti_k3_rtc_set_time()
290 regmap_write(priv->regmap, REG_K3RTC_S_CNT_LSW, seconds); in ti_k3_rtc_set_time()
291 regmap_write(priv->regmap, REG_K3RTC_S_CNT_MSW, seconds >> 32); in ti_k3_rtc_set_time()
309 * Ensure the write sync is through - NOTE: it should be OK to have in ti_k3_rtc_alarm_irq_enable()
324 rtc_time64_to_tm((((time64_t)seconds_hi) << 32) | (time64_t)seconds_lo, &alarm->time); in ti_k3_rtc_read_alarm()
326 alarm->enabled = k3rtc_field_read(priv, K3RTC_IRQ_ENABLE_SET); in ti_k3_rtc_read_alarm()
337 seconds = rtc_tm_to_time64(&alarm->time); in ti_k3_rtc_set_alarm()
350 return ti_k3_rtc_alarm_irq_enable(dev, alarm->enabled); in ti_k3_rtc_set_alarm()
356 u32 ticks_per_hr = priv->rate_32k * 3600; in ti_k3_rtc_read_offset()
362 /* Convert from RTC calibration register format to ppb format */ in ti_k3_rtc_read_offset()
365 tmp -= ticks_per_hr / 2LL; in ti_k3_rtc_read_offset()
371 *offset = (long)-tmp; in ti_k3_rtc_read_offset()
379 u32 ticks_per_hr = priv->rate_32k * 3600; in ti_k3_rtc_set_offset()
385 return -ERANGE; in ti_k3_rtc_set_offset()
387 /* Convert from ppb format to RTC calibration register format */ in ti_k3_rtc_set_offset()
390 tmp -= K3RTC_PPB_MULT / 2LL; in ti_k3_rtc_set_offset()
396 comp = (int)-tmp; in ti_k3_rtc_set_offset()
412 * de-assert depends on 32k clock edge in the 32k domain in ti_k3_rtc_interrupt()
414 * the status bit is cleared, but the IRQ stays re-asserted. in ti_k3_rtc_interrupt()
421 usleep_range(priv->sync_timeout_us, priv->sync_timeout_us + 2); in ti_k3_rtc_interrupt()
440 regmap_write(priv->regmap, REG_K3RTC_IRQSTATUS_SYS, 0x1); in ti_k3_rtc_interrupt()
463 ret = regmap_field_read_poll_timeout(priv->r_fields[K3RTC_IRQ_STATUS], in ti_k3_rtc_interrupt()
464 ret, !ret, 2, priv->sync_timeout_us); in ti_k3_rtc_interrupt()
470 /* Notify RTC core on event */ in ti_k3_rtc_interrupt()
471 rtc_update_irq(priv->rtc_dev, 1, RTC_IRQF | RTC_AF); in ti_k3_rtc_interrupt()
491 return regmap_bulk_read(priv->regmap, REG_K3RTC_SCRATCH0 + offset, val, bytes / 4); in ti_k3_rtc_scratch_read()
500 ret = regmap_bulk_write(priv->regmap, REG_K3RTC_SCRATCH0 + offset, val, bytes / 4); in ti_k3_rtc_scratch_write()
511 .size = REG_K3RTC_SCRATCH7 - REG_K3RTC_SCRATCH0 + 4,
524 priv->rate_32k = clk_get_rate(clk); in k3rtc_get_32kclk()
527 if (priv->rate_32k != 32768) in k3rtc_get_32kclk()
529 priv->rate_32k); in k3rtc_get_32kclk()
536 priv->sync_timeout_us = (u32)(DIV_ROUND_UP_ULL(1000000, priv->rate_32k) * 4); in k3rtc_get_32kclk()
555 struct device *dev = &pdev->dev; in ti_k3_rtc_probe()
562 return -ENOMEM; in ti_k3_rtc_probe()
568 priv->regmap = devm_regmap_init_mmio(dev, rtc_base, &ti_k3_rtc_regmap_config); in ti_k3_rtc_probe()
569 if (IS_ERR(priv->regmap)) in ti_k3_rtc_probe()
570 return PTR_ERR(priv->regmap); in ti_k3_rtc_probe()
572 ret = devm_regmap_field_bulk_alloc(dev, priv->regmap, priv->r_fields, in ti_k3_rtc_probe()
587 priv->irq = (unsigned int)ret; in ti_k3_rtc_probe()
589 priv->rtc_dev = devm_rtc_allocate_device(dev); in ti_k3_rtc_probe()
590 if (IS_ERR(priv->rtc_dev)) in ti_k3_rtc_probe()
591 return PTR_ERR(priv->rtc_dev); in ti_k3_rtc_probe()
593 priv->rtc_dev->ops = &ti_k3_rtc_ops; in ti_k3_rtc_probe()
594 priv->rtc_dev->range_max = (1ULL << 48) - 1; /* 48Bit seconds */ in ti_k3_rtc_probe()
597 ret = devm_request_threaded_irq(dev, priv->irq, NULL, in ti_k3_rtc_probe()
612 if (device_property_present(dev, "wakeup-source")) in ti_k3_rtc_probe()
617 ret = devm_rtc_register_device(priv->rtc_dev); in ti_k3_rtc_probe()
621 return devm_rtc_nvmem_register(priv->rtc_dev, &ti_k3_rtc_nvmem_config); in ti_k3_rtc_probe()
625 {.compatible = "ti,am62-rtc" },
635 return enable_irq_wake(priv->irq); in ti_k3_rtc_suspend()
645 disable_irq_wake(priv->irq); in ti_k3_rtc_resume()
654 .name = "rtc-ti-k3",
662 MODULE_DESCRIPTION("TI K3 RTC driver");