|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * Copyright (c) 2025 Realtek Semiconductor Corporation, SIBG-SD7 |
| 5 | + * Author: Chia-Yang Lin <cylin0708@realtek.com> |
| 6 | + */ |
| 7 | + |
| 8 | +#define DT_DRV_COMPAT realtek_rts5912_rtc |
| 9 | + |
| 10 | +#include <soc.h> |
| 11 | +#include <errno.h> |
| 12 | +#include <zephyr/device.h> |
| 13 | +#include <zephyr/drivers/rtc.h> |
| 14 | +#include <zephyr/kernel.h> |
| 15 | +#include <zephyr/logging/log.h> |
| 16 | +#include <zephyr/drivers/clock_control.h> |
| 17 | +#include <zephyr/drivers/clock_control/clock_control_rts5912.h> |
| 18 | +#include "rtc_utils.h" |
| 19 | + |
| 20 | +#include "reg/reg_system.h" |
| 21 | +#include "reg/reg_rtc.h" |
| 22 | + |
| 23 | +LOG_MODULE_REGISTER(rtc_rts5912, CONFIG_RTC_LOG_LEVEL); |
| 24 | + |
| 25 | +#define RTS5912_RTC_TIME_MASK \ |
| 26 | + (RTC_ALARM_TIME_MASK_SECOND | RTC_ALARM_TIME_MASK_MINUTE | RTC_ALARM_TIME_MASK_HOUR | \ |
| 27 | + RTC_ALARM_TIME_MASK_WEEKDAY | RTC_ALARM_TIME_MASK_MONTHDAY | RTC_ALARM_TIME_MASK_MONTH | \ |
| 28 | + RTC_ALARM_TIME_MASK_YEAR) |
| 29 | + |
| 30 | +#define RTS5912_RTC_DIVCTL_NORMAL_OPERATION BIT(1) |
| 31 | + |
| 32 | +#define RTS5912_RTC_DAYWEEK_OFFSET 1 |
| 33 | +#define RTS5912_RTC_MONTH_OFFSET 1 |
| 34 | +#define RTS5912_RTC_YEAR_OFFSET 100 |
| 35 | + |
| 36 | +struct rtc_rts5912_config { |
| 37 | + RTC_Type *regs; |
| 38 | + uint32_t rtc_base; |
| 39 | + uint32_t rtc_clk_grp; |
| 40 | + uint32_t rtc_clk_idx; |
| 41 | + const struct device *clk_dev; |
| 42 | +}; |
| 43 | + |
| 44 | +struct rtc_rts5912_data { |
| 45 | + struct k_spinlock lock; |
| 46 | +}; |
| 47 | + |
| 48 | +static void rtc_rts5912_reset_rtc_time(const struct device *dev) |
| 49 | +{ |
| 50 | + const struct rtc_rts5912_config *const dev_cfg = dev->config; |
| 51 | + RTC_Type *rtc_regs = dev_cfg->regs; |
| 52 | + |
| 53 | + rtc_regs->CTRL1 |= RTC_CTRL1_SETMODE_Msk; |
| 54 | + rtc_regs->CTRL0 &= ~RTC_CTRL0_DIVCTL_Msk; |
| 55 | + rtc_regs->CTRL0 |= (RTS5912_RTC_DIVCTL_NORMAL_OPERATION << RTC_CTRL0_DIVCTL_Pos); |
| 56 | + rtc_regs->CTRL1 |= RTC_CTRL1_DATEMODE_Msk; |
| 57 | + rtc_regs->CTRL1 |= RTC_CTRL1_HRMODE_Msk; |
| 58 | + rtc_regs->SEC = 0; |
| 59 | + rtc_regs->MIN = 0; |
| 60 | + rtc_regs->HR &= ~(RTC_HR_AMPM_Msk | RTC_HR_VAL_Msk); |
| 61 | + rtc_regs->DAYWEEK = BIT(0); |
| 62 | + rtc_regs->DAYMONTH = BIT(0); |
| 63 | + rtc_regs->MONTH = BIT(0); |
| 64 | + rtc_regs->YEAR = 0; |
| 65 | + rtc_regs->WEEK &= ~RTC_WEEK_NUM_Msk; |
| 66 | +} |
| 67 | + |
| 68 | +static int rtc_rts5912_set_time(const struct device *dev, const struct rtc_time *timeptr) |
| 69 | +{ |
| 70 | + const struct rtc_rts5912_config *const dev_cfg = dev->config; |
| 71 | + RTC_Type *rtc_regs = dev_cfg->regs; |
| 72 | + |
| 73 | + if (!rtc_utils_validate_rtc_time(timeptr, RTS5912_RTC_TIME_MASK)) { |
| 74 | + rtc_rts5912_reset_rtc_time(dev); |
| 75 | + k_msleep(1); |
| 76 | + return -EINVAL; |
| 77 | + } |
| 78 | + |
| 79 | + rtc_regs->CTRL1 |= RTC_CTRL1_SETMODE_Msk; |
| 80 | + rtc_regs->SEC = timeptr->tm_sec; |
| 81 | + rtc_regs->MIN = timeptr->tm_min; |
| 82 | + rtc_regs->HR = timeptr->tm_hour; |
| 83 | + rtc_regs->DAYWEEK = timeptr->tm_wday + RTS5912_RTC_DAYWEEK_OFFSET; |
| 84 | + rtc_regs->DAYMONTH = timeptr->tm_mday; |
| 85 | + rtc_regs->MONTH = timeptr->tm_mon + RTS5912_RTC_MONTH_OFFSET; |
| 86 | + rtc_regs->YEAR = timeptr->tm_year % RTS5912_RTC_YEAR_OFFSET; |
| 87 | + /* Need to delay in order to update register after setting RTC time */ |
| 88 | + k_msleep(1); |
| 89 | + rtc_regs->CTRL1 &= ~RTC_CTRL1_SETMODE_Msk; |
| 90 | + |
| 91 | + return 0; |
| 92 | +} |
| 93 | + |
| 94 | +static int rtc_rts5912_get_time(const struct device *dev, struct rtc_time *timeptr) |
| 95 | +{ |
| 96 | + const struct rtc_rts5912_config *const dev_cfg = dev->config; |
| 97 | + RTC_Type *rtc_regs = dev_cfg->regs; |
| 98 | + |
| 99 | + timeptr->tm_sec = rtc_regs->SEC; |
| 100 | + timeptr->tm_min = rtc_regs->MIN; |
| 101 | + timeptr->tm_hour = rtc_regs->HR; |
| 102 | + timeptr->tm_wday = rtc_regs->DAYWEEK - RTS5912_RTC_DAYWEEK_OFFSET; |
| 103 | + timeptr->tm_mday = rtc_regs->DAYMONTH; |
| 104 | + timeptr->tm_mon = rtc_regs->MONTH - RTS5912_RTC_MONTH_OFFSET; |
| 105 | + timeptr->tm_year = rtc_regs->YEAR + RTS5912_RTC_YEAR_OFFSET; |
| 106 | + |
| 107 | + /* No support for daylight saving time flag and nanoseconds in rts5912 RTC */ |
| 108 | + timeptr->tm_isdst = -1; |
| 109 | + timeptr->tm_nsec = 0; |
| 110 | + |
| 111 | + return 0; |
| 112 | +} |
| 113 | + |
| 114 | +static DEVICE_API(rtc, rtc_rts5912_driver_api) = { |
| 115 | + .set_time = rtc_rts5912_set_time, |
| 116 | + .get_time = rtc_rts5912_get_time, |
| 117 | +}; |
| 118 | + |
| 119 | +static int rtc_rts5912_init(const struct device *dev) |
| 120 | +{ |
| 121 | + const struct rtc_rts5912_config *const rtc_config = dev->config; |
| 122 | + struct rts5912_sccon_subsys sccon; |
| 123 | + |
| 124 | + int rc; |
| 125 | + |
| 126 | + if (!device_is_ready(rtc_config->clk_dev)) { |
| 127 | + LOG_ERR("RTC device not ready"); |
| 128 | + return -ENODEV; |
| 129 | + } |
| 130 | + |
| 131 | + sccon.clk_grp = rtc_config->rtc_clk_grp; |
| 132 | + sccon.clk_idx = rtc_config->rtc_clk_idx; |
| 133 | + rc = clock_control_on(rtc_config->clk_dev, (clock_control_subsys_t)&sccon); |
| 134 | + if (rc < 0) { |
| 135 | + LOG_ERR("Failed to turn on RTC clock (%d)", rc); |
| 136 | + return rc; |
| 137 | + } |
| 138 | + |
| 139 | + rtc_rts5912_reset_rtc_time(dev); |
| 140 | + |
| 141 | + return rc; |
| 142 | +} |
| 143 | + |
| 144 | +#define RTC_RTS5912_CONFIG(inst) \ |
| 145 | + static struct rtc_rts5912_config rtc_rts5912_config_##inst = { \ |
| 146 | + .regs = (RTC_Type *)(DT_INST_REG_ADDR(inst)), \ |
| 147 | + .rtc_base = DT_INST_REG_ADDR(inst), \ |
| 148 | + .rtc_clk_grp = DT_INST_CLOCKS_CELL_BY_NAME(inst, rtc, clk_grp), \ |
| 149 | + .rtc_clk_idx = DT_INST_CLOCKS_CELL_BY_NAME(inst, rtc, clk_idx), \ |
| 150 | + .clk_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(inst)), \ |
| 151 | + }; |
| 152 | + |
| 153 | +#define RTC_RTS5912_DEVICE_INIT(index) \ |
| 154 | + RTC_RTS5912_CONFIG(index) \ |
| 155 | + DEVICE_DT_INST_DEFINE(index, &rtc_rts5912_init, NULL, NULL, &rtc_rts5912_config_##index, \ |
| 156 | + POST_KERNEL, CONFIG_RTC_INIT_PRIORITY, &rtc_rts5912_driver_api); |
| 157 | + |
| 158 | +DT_INST_FOREACH_STATUS_OKAY(RTC_RTS5912_DEVICE_INIT) |
0 commit comments