Skip to content

Commit bd365a0

Browse files
cylin-realtekkartben
authored andcommitted
drivers: rtc: rts5912: add rtc driver
Add rtc driver for Realtek rts5912. Signed-off-by: Chia-Yang Lin <cylin0708@realtek.com>
1 parent 6362391 commit bd365a0

File tree

8 files changed

+270
-0
lines changed

8 files changed

+270
-0
lines changed

drivers/rtc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ zephyr_library_sources_ifdef(CONFIG_RTC_SMARTBOND rtc_smartbond.c)
2525
zephyr_library_sources_ifdef(CONFIG_RTC_ATMEL_SAM rtc_sam.c)
2626
zephyr_library_sources_ifdef(CONFIG_RTC_ATMEL_SAM0 rtc_sam0.c)
2727
zephyr_library_sources_ifdef(CONFIG_RTC_RPI_PICO rtc_rpi_pico.c)
28+
zephyr_library_sources_ifdef(CONFIG_RTC_RTS5912 rtc_rts5912.c)
2829
zephyr_library_sources_ifdef(CONFIG_RTC_RV3028 rtc_rv3028.c)
2930
zephyr_library_sources_ifdef(CONFIG_RTC_NUMAKER rtc_numaker.c)
3031
zephyr_library_sources_ifdef(CONFIG_RTC_XMC4XXX rtc_xmc4xxx.c)

drivers/rtc/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ source "drivers/rtc/Kconfig.mc146818"
5252
source "drivers/rtc/Kconfig.pcf8523"
5353
source "drivers/rtc/Kconfig.pcf8563"
5454
source "drivers/rtc/Kconfig.rpi_pico"
55+
source "drivers/rtc/Kconfig.rts5912"
5556
source "drivers/rtc/Kconfig.rv3028"
5657
source "drivers/rtc/Kconfig.sam"
5758
source "drivers/rtc/Kconfig.sam0"

drivers/rtc/Kconfig.rts5912

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (c) 2025, Realtek, SIBG-SD7
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
config RTC_RTS5912
5+
bool "Realtek RTS5912 rtc driver"
6+
default y
7+
depends on CLOCK_CONTROL
8+
depends on DT_HAS_REALTEK_RTS5912_RTC_ENABLED
9+
help
10+
Enable support for Realtek RTC driver.

drivers/rtc/rtc_rts5912.c

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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)

dts/arm/realtek/ec/rts5912.dtsi

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@
7979
clock-names = "rc25m", "pll";
8080
};
8181

82+
rtc: rtc@4000c100 {
83+
compatible = "realtek,rts5912-rtc";
84+
reg = <0x4000c100 0x20>;
85+
clocks = <&sccon RTS5912_SCCON_PERIPH_GRP2 PERIPH_GRP2_RTC_CLKPWR>;
86+
clock-names = "rtc";
87+
status = "disabled";
88+
};
89+
8290
slwtmr0: slwtmr0@4000c200 {
8391
compatible = "realtek,rts5912-slwtimer";
8492
reg = <0x4000c200 0x10>;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright (c) 2025 Realtek, SIBG-SD7
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: RTC on Realtek RTS5912 EC
5+
6+
compatible: "realtek,rts5912-rtc"
7+
8+
include: rtc-device.yaml
9+
10+
properties:
11+
reg:
12+
required: true

soc/realtek/ec/rts5912/reg/reg_rtc.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2025 Realtek, SIBG-SD7
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_SOC_REALTEK_RTS5912_REG_RTC_H
8+
#define ZEPHYR_SOC_REALTEK_RTS5912_REG_RTC_H
9+
10+
#include <zephyr/kernel.h>
11+
12+
/*
13+
* @brief RTC Controller (RTC)
14+
*/
15+
16+
typedef struct {
17+
volatile uint8_t SEC;
18+
volatile uint8_t SECALARM;
19+
volatile uint8_t MIN;
20+
volatile uint8_t MINALARM;
21+
volatile uint8_t HR;
22+
volatile uint8_t HRALARM;
23+
volatile uint8_t DAYWEEK;
24+
volatile uint8_t DAYMONTH;
25+
volatile uint8_t MONTH;
26+
volatile uint8_t YEAR;
27+
volatile uint8_t CTRL0;
28+
volatile uint8_t CTRL1;
29+
volatile uint8_t CTRL2;
30+
volatile uint8_t DAYWEEKALARM;
31+
volatile uint8_t DAYMONTHALARM;
32+
volatile const uint8_t RESERVED;
33+
volatile const uint32_t RESERVED1[2];
34+
volatile uint32_t DLSFW;
35+
volatile uint32_t DLSBW;
36+
volatile uint32_t WEEK;
37+
} RTC_Type;
38+
39+
/* HR */
40+
#define RTC_HR_VAL_Pos (0UL)
41+
#define RTC_HR_VAL_Msk GENMASK(5, 0)
42+
#define RTC_HR_AMPM_Pos (7UL)
43+
#define RTC_HR_AMPM_Msk BIT(RTC_HR_AMPM_Pos)
44+
/* DAYWEEK */
45+
#define RTC_DAYWEEK_Pos (0UL)
46+
#define RTC_DAYWEEK_Msk GENMASK(3, 0)
47+
/* DAYMONTH */
48+
#define RTC_DAYMONTH_Pos (0UL)
49+
#define RTC_DAYMONTH_Msk GENMASK(5, 0)
50+
/* CTRL0 */
51+
#define RTC_CTRL0_DIVCTL_Pos (4UL)
52+
#define RTC_CTRL0_DIVCTL_Msk GENMASK(6, 4)
53+
/* CTRL1 */
54+
#define RTC_CTRL1_HRMODE_Pos (1UL)
55+
#define RTC_CTRL1_HRMODE_Msk BIT(RTC_CTRL1_HRMODE_Pos)
56+
#define RTC_CTRL1_DATEMODE_Pos (2UL)
57+
#define RTC_CTRL1_DATEMODE_Msk BIT(RTC_CTRL1_DATEMODE_Pos)
58+
#define RTC_CTRL1_SETMODE_Pos (7UL)
59+
#define RTC_CTRL1_SETMODE_Msk BIT(RTC_CTRL1_SETMODE_Pos)
60+
/* WEEK */
61+
#define RTC_WEEK_NUM_Pos (0UL)
62+
#define RTC_WEEK_NUM_Msk GENMASK(7, 0)
63+
64+
#endif /* ZEPHYR_SOC_REALTEK_RTS5912_REG_RTC_H */
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* Copyright (c) 2025 Realtek Semiconductor Corporation, SIBG-SD7
5+
*
6+
*/
7+
8+
/ {
9+
aliases {
10+
rtc = &rtc;
11+
};
12+
};
13+
14+
&rtc {
15+
status = "okay";
16+
};

0 commit comments

Comments
 (0)