Skip to content

Commit c2f3e86

Browse files
dylanHsieh4963JasonLin-RealTek
authored andcommitted
driver: adc: add adc driver for rts5912
Add adc driver for Realtek rts5912. Signed-off-by: Dylan Hsieh <[email protected]
1 parent 4102010 commit c2f3e86

File tree

12 files changed

+466
-0
lines changed

12 files changed

+466
-0
lines changed

boards/realtek/rts5912_evb/rts5912_evb.dts

+11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@
2020
};
2121
};
2222

23+
&adc0 {
24+
status = "okay";
25+
pinctrl-0 = <&adc0_gpio074 &adc1_gpio075
26+
&adc2_gpio076 &adc3_gpio077
27+
&adc4_gpio078 &adc5_gpio079
28+
&adc6_gpio080 &adc7_gpio081
29+
&adc8_gpio082 &adc9_gpio054
30+
&adc10_gpio098 &adc11_gpio024>;
31+
pinctrl-names = "default";
32+
};
33+
2334
&uart0 {
2435
status = "okay";
2536
current-speed = <115200>;

boards/realtek/rts5912_evb/rts5912_evb.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ flash: 320
1515
supported:
1616
- gpio
1717
- pinmux
18+
- adc
1819
vendor: realtek

boards/realtek/rts5912_evb/rts5912_evb_defconfig

+3
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ CONFIG_GPIO=y
2020

2121
# Input Driver
2222
CONFIG_INPUT=y
23+
24+
# Enable ADC
25+
CONFIG_ADC=y

drivers/adc/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,4 @@ zephyr_library_sources_ifdef(CONFIG_ADC_AD4114 adc_ad4114.c)
6161
zephyr_library_sources_ifdef(CONFIG_ADC_AD7124 adc_ad7124.c)
6262
zephyr_library_sources_ifdef(CONFIG_ADC_AD405X adc_ad405x.c)
6363
zephyr_library_sources_ifdef(CONFIG_ADC_AD4130 adc_ad4130.c)
64+
zephyr_library_sources_ifdef(CONFIG_ADC_REALTEK_RTS5912 adc_realtek_rts5912.c)

drivers/adc/Kconfig

+2
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,6 @@ source "drivers/adc/Kconfig.ad405x"
146146

147147
source "drivers/adc/Kconfig.ad4130"
148148

149+
source "drivers/adc/Kconfig.rts5912"
150+
149151
endif # ADC

drivers/adc/Kconfig.rts5912

+10
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 ADC_REALTEK_RTS5912
5+
bool "Realtek RTS5912 ADC drivers"
6+
default y
7+
depends on DT_HAS_REALTEK_RTS5912_ADC_ENABLED
8+
select PINCTRL
9+
help
10+
This option enables the ADC driver for Realtek RTS5912 of processors.

drivers/adc/adc_realtek_rts5912.c

+304
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,304 @@
1+
/*
2+
* Copyright (c) 2025 Realtek, SIBG-SD7
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#define DT_DRV_COMPAT realtek_rts5912_adc
8+
9+
#include <zephyr/drivers/adc.h>
10+
#include <zephyr/drivers/clock_control.h>
11+
#include <zephyr/drivers/clock_control/clock_control_rts5912.h>
12+
#include <zephyr/drivers/pinctrl.h>
13+
14+
#include "reg/reg_adc.h"
15+
16+
#define ADC_CONTEXT_USES_KERNEL_TIMER
17+
#include "adc_context.h"
18+
19+
#include <zephyr/logging/log.h>
20+
LOG_MODULE_REGISTER(adc_rts5912, CONFIG_ADC_LOG_LEVEL);
21+
22+
#define RTS5912_ADC_MAX_CHAN 12
23+
#define RTS5912_ADC_POLLING_TIME_MS 1
24+
#define RTS5912_ADC_ENABLE_TIMEOUT 100
25+
26+
struct adc_rts5912_config {
27+
volatile struct adc_regs *regs;
28+
const struct pinctrl_dev_config *pcfg;
29+
#ifdef CONFIG_CLOCK_CONTROL
30+
const struct device *clk_dev;
31+
struct rts5912_sccon_subsys sccon_cfg;
32+
#endif
33+
};
34+
35+
struct adc_rts5912_data {
36+
struct adc_context ctx;
37+
const struct device *adc_dev;
38+
volatile uint16_t *buffer;
39+
volatile uint16_t *repeat_buffer;
40+
uint32_t channels;
41+
};
42+
43+
static void adc_context_start_sampling(struct adc_context *ctx)
44+
{
45+
struct adc_rts5912_data *data = CONTAINER_OF(ctx, struct adc_rts5912_data, ctx);
46+
const struct device *adc_dev = data->adc_dev;
47+
const struct adc_rts5912_config *const cfg = adc_dev->config;
48+
volatile struct adc_regs *regs = cfg->regs;
49+
50+
data->repeat_buffer = data->buffer;
51+
52+
regs->ctrl |= ADC_CTRL_SGLDNINTEN;
53+
regs->ctrl |= ADC_CTRL_START;
54+
}
55+
56+
static void adc_context_update_buffer_pointer(struct adc_context *ctx, bool repeat_sampling)
57+
{
58+
struct adc_rts5912_data *data = CONTAINER_OF(ctx, struct adc_rts5912_data, ctx);
59+
60+
if (repeat_sampling) {
61+
data->buffer = data->repeat_buffer;
62+
}
63+
}
64+
65+
static int adc_rts5912_channel_setup(const struct device *dev,
66+
const struct adc_channel_cfg *channel_cfg)
67+
{
68+
const struct adc_rts5912_config *const cfg = dev->config;
69+
volatile struct adc_regs *regs = cfg->regs;
70+
71+
if (channel_cfg->acquisition_time != ADC_ACQ_TIME_DEFAULT) {
72+
LOG_ERR("Conversion time not supported!");
73+
return -EINVAL;
74+
}
75+
76+
if (channel_cfg->channel_id >= RTS5912_ADC_MAX_CHAN) {
77+
LOG_ERR("Channel %d not supported!", channel_cfg->channel_id);
78+
return -EINVAL;
79+
}
80+
81+
if (channel_cfg->gain != ADC_GAIN_1) {
82+
LOG_ERR("ADC gain not supported!");
83+
return -EINVAL;
84+
}
85+
86+
uint8_t channel_id = channel_cfg->channel_id;
87+
88+
regs->chctrl |= ((0x01ul << channel_id) | (ADC_CHCTRL_LPFBP << channel_id));
89+
LOG_DBG("CHCTRL = 0x%08x", regs->chctrl);
90+
91+
return 0;
92+
}
93+
94+
static bool adc_rts5912_validate_buffer_size(const struct adc_sequence *sequence)
95+
{
96+
int chan_count = 0;
97+
size_t buff_need;
98+
uint32_t chan_mask;
99+
100+
for (chan_mask = 0x80; chan_mask != 0; chan_mask >>= 1) {
101+
if (chan_mask & sequence->channels) {
102+
chan_count++;
103+
}
104+
}
105+
106+
buff_need = chan_count * sizeof(uint16_t);
107+
108+
if (sequence->options) {
109+
buff_need *= 1 + sequence->options->extra_samplings;
110+
}
111+
112+
if (buff_need > sequence->buffer_size) {
113+
return false;
114+
}
115+
116+
return true;
117+
}
118+
119+
static int adc_rts5912_enable(const struct device *dev)
120+
{
121+
const struct adc_rts5912_config *const cfg = dev->config;
122+
volatile struct adc_regs *regs = cfg->regs;
123+
124+
int64_t st = k_uptime_get();
125+
126+
regs->ctrl |= ADC_CTRL_EN;
127+
while ((k_uptime_get() - st) < RTS5912_ADC_ENABLE_TIMEOUT) {
128+
if (regs->sts & ADC_STS_RDY) {
129+
return 0;
130+
}
131+
k_msleep(RTS5912_ADC_POLLING_TIME_MS);
132+
}
133+
134+
LOG_ERR("ADC enable timeout");
135+
regs->ctrl &= ~ADC_CTRL_EN;
136+
137+
return -EIO;
138+
}
139+
140+
static int adc_rts5912_start_read(const struct device *dev, const struct adc_sequence *sequence)
141+
{
142+
struct adc_rts5912_data *const data = dev->data;
143+
144+
if (sequence->channels & ~BIT_MASK(RTS5912_ADC_MAX_CHAN)) {
145+
LOG_ERR("Incorrect channels, bitmask 0x%x", sequence->channels);
146+
return -EINVAL;
147+
}
148+
149+
if (sequence->channels == 0UL) {
150+
LOG_ERR("No channel selected");
151+
return -EINVAL;
152+
}
153+
154+
if (!adc_rts5912_validate_buffer_size(sequence)) {
155+
LOG_ERR("Incorrect buffer size");
156+
return -ENOMEM;
157+
}
158+
159+
data->channels = sequence->channels;
160+
data->buffer = sequence->buffer;
161+
162+
if (adc_rts5912_enable(dev) < 0) {
163+
return -EIO;
164+
}
165+
166+
adc_context_start_read(&data->ctx, sequence);
167+
168+
return adc_context_wait_for_completion(&data->ctx);
169+
}
170+
171+
static int adc_rts5912_read(const struct device *dev, const struct adc_sequence *sequence)
172+
{
173+
struct adc_rts5912_data *const data = dev->data;
174+
int error;
175+
176+
adc_context_lock(&data->ctx, false, NULL);
177+
error = adc_rts5912_start_read(dev, sequence);
178+
adc_context_release(&data->ctx, error);
179+
180+
return error;
181+
}
182+
183+
static void rts5912_adc_get_sample(const struct device *dev)
184+
{
185+
const struct adc_rts5912_config *const cfg = dev->config;
186+
volatile struct adc_regs *regs = cfg->regs;
187+
struct adc_rts5912_data *const data = dev->data;
188+
uint32_t idx;
189+
uint32_t channels = data->channels;
190+
uint32_t bit;
191+
192+
/*
193+
* Using the enabled channel bit set, from
194+
* lowest channel number to highest, find out
195+
* which channel is enabled and copy the ADC
196+
* values from hardware registers to the data
197+
* buffer.
198+
*/
199+
bit = find_lsb_set(channels);
200+
201+
while (bit != 0) {
202+
idx = bit - 1;
203+
204+
*data->buffer = ((uint16_t)regs->chdata[idx] & ADC_CHDATA_RESULT_Msk);
205+
data->buffer++;
206+
207+
LOG_DBG("idx=%d, data=%x", idx, regs->chdata[idx]);
208+
209+
channels &= ~BIT(idx);
210+
bit = find_lsb_set(channels);
211+
}
212+
}
213+
214+
static void adc_rts5912_single_isr(const struct device *dev)
215+
{
216+
const struct adc_rts5912_config *const cfg = dev->config;
217+
volatile struct adc_regs *regs = cfg->regs;
218+
struct adc_rts5912_data *const data = dev->data;
219+
220+
if (regs->sts & ADC_STS_SGLDN) {
221+
LOG_DBG("single done interrupt triggered.");
222+
223+
regs->ctrl &= ~(ADC_CTRL_SGLDNINTEN);
224+
regs->sts &= regs->sts;
225+
226+
rts5912_adc_get_sample(dev);
227+
228+
regs->ctrl &= ~ADC_CTRL_EN;
229+
adc_context_on_sampling_done(&data->ctx, dev);
230+
}
231+
}
232+
233+
static int adc_rts5912_init(const struct device *dev)
234+
{
235+
const struct adc_rts5912_config *const cfg = dev->config;
236+
struct adc_rts5912_data *const data = dev->data;
237+
volatile struct adc_regs *regs = cfg->regs;
238+
239+
int ret;
240+
241+
data->adc_dev = dev;
242+
243+
ret = pinctrl_apply_state(cfg->pcfg, PINCTRL_STATE_DEFAULT);
244+
if (ret != 0) {
245+
LOG_ERR("rts5912 ADC pinctrl setup failed (%d)", ret);
246+
return ret;
247+
}
248+
249+
#ifdef CONFIG_CLOCK_CONTROL
250+
if (!device_is_ready(cfg->clk_dev)) {
251+
LOG_ERR("clock \"%s\" device not ready", cfg->clk_dev->name);
252+
return -ENODEV;
253+
}
254+
255+
ret = clock_control_on(cfg->clk_dev, (clock_control_subsys_t)&cfg->sccon_cfg);
256+
if (ret != 0) {
257+
LOG_ERR("clock power on fail");
258+
return ret;
259+
}
260+
#endif
261+
262+
regs->ctrl = ADC_CTRL_RST;
263+
264+
IRQ_CONNECT(DT_INST_IRQN(0), DT_INST_IRQ(0, priority), adc_rts5912_single_isr,
265+
DEVICE_DT_INST_GET(0), 0);
266+
irq_enable(DT_INST_IRQN(0));
267+
268+
adc_context_unlock_unconditionally(&data->ctx);
269+
270+
return 0;
271+
}
272+
273+
#define DEV_CONFIG_CLK_DEV_INIT(n) \
274+
.clk_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(n)), \
275+
.sccon_cfg = { \
276+
.clk_grp = DT_INST_CLOCKS_CELL(n, clk_grp), \
277+
.clk_idx = DT_INST_CLOCKS_CELL(n, clk_idx), \
278+
}
279+
280+
#define ADC_RTS5912_INIT(n) \
281+
PINCTRL_DT_INST_DEFINE(n); \
282+
\
283+
static DEVICE_API(adc, adc_rts5912_api_##n) = { \
284+
.channel_setup = adc_rts5912_channel_setup, \
285+
.read = adc_rts5912_read, \
286+
.ref_internal = DT_INST_PROP(n, vref_mv), \
287+
}; \
288+
\
289+
static struct adc_rts5912_config adc_rts5912_dev_cfg_##n = { \
290+
.regs = (struct adc_regs *)(DT_INST_REG_ADDR(n)), \
291+
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), \
292+
DEV_CONFIG_CLK_DEV_INIT(n)}; \
293+
\
294+
static struct adc_rts5912_data adc_rts5912_dev_data_##n = { \
295+
ADC_CONTEXT_INIT_TIMER(adc_rts5912_dev_data_##n, ctx), \
296+
ADC_CONTEXT_INIT_LOCK(adc_rts5912_dev_data_##n, ctx), \
297+
ADC_CONTEXT_INIT_SYNC(adc_rts5912_dev_data_##n, ctx), \
298+
}; \
299+
\
300+
DEVICE_DT_INST_DEFINE(n, adc_rts5912_init, NULL, &adc_rts5912_dev_data_##n, \
301+
&adc_rts5912_dev_cfg_##n, PRE_KERNEL_1, CONFIG_ADC_INIT_PRIORITY, \
302+
&adc_rts5912_api_##n);
303+
304+
DT_INST_FOREACH_STATUS_OKAY(ADC_RTS5912_INIT)

dts/arm/realtek/ec/rts5912.dtsi

+10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
#include <arm/armv8-m.dtsi>
9+
#include <zephyr/dt-bindings/adc/adc.h>
910
#include <zephyr/dt-bindings/clock/rts5912_clock.h>
1011
#include <zephyr/dt-bindings/gpio/realtek-gpio.h>
1112

@@ -106,6 +107,15 @@
106107
status = "okay";
107108
};
108109

110+
adc0: adc@4000fe00 {
111+
compatible = "realtek,rts5912-adc";
112+
reg = <0x4000fe00 0x38>;
113+
clocks = <&sccon RTS5912_SCCON_ADC ADC0_CLKPWR>;
114+
interrupts = <221 0>;
115+
#io-channel-cells = <1>;
116+
status = "disabled";
117+
};
118+
109119
uart0: uart@40010100 {
110120
compatible = "ns16550";
111121
reg = <0x40010100 0x100>;

0 commit comments

Comments
 (0)