Skip to content

Commit bffb153

Browse files
ene-stevenkartben
authored andcommitted
drivers: adc: adc driver
Add adc driver for ENE KB106X Signed-off-by: Steven Chang <[email protected]>
1 parent 43f311b commit bffb153

File tree

6 files changed

+337
-0
lines changed

6 files changed

+337
-0
lines changed

drivers/adc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ zephyr_library_sources_ifdef(CONFIG_ADC_MAX11102_17 adc_max11102_17.c)
5555
zephyr_library_sources_ifdef(CONFIG_ADC_AD559X adc_ad559x.c)
5656
zephyr_library_sources_ifdef(CONFIG_ADC_LTC2451 adc_ltc2451.c)
5757
zephyr_library_sources_ifdef(CONFIG_ADC_NUMAKER adc_numaker.c)
58+
zephyr_library_sources_ifdef(CONFIG_ADC_ENE_KB106X adc_ene_kb106x.c)
5859
zephyr_library_sources_ifdef(CONFIG_ADC_ENE_KB1200 adc_ene_kb1200.c)
5960
zephyr_library_sources_ifdef(CONFIG_ADC_MCUX_GAU adc_mcux_gau_adc.c)
6061
zephyr_library_sources_ifdef(CONFIG_ADC_AMBIQ adc_ambiq.c)

drivers/adc/Kconfig.ene

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,11 @@ config ADC_ENE_KB1200
88
select PINCTRL
99
help
1010
Enable ADC driver for ENE KB1200.
11+
12+
config ADC_ENE_KB106X
13+
bool "ENE KB106X ADC driver"
14+
default y
15+
depends on DT_HAS_ENE_KB106X_ADC_ENABLED
16+
select PINCTRL
17+
help
18+
Enable ADC driver for ENE KB106X.

drivers/adc/adc_ene_kb106x.c

Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
/*
2+
* Copyright (c) 2025 ENE Technology Inc.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#define DT_DRV_COMPAT ene_kb106x_adc
8+
9+
#include <zephyr/kernel.h>
10+
#include <zephyr/drivers/adc.h>
11+
#include <zephyr/drivers/pinctrl.h>
12+
#include <errno.h>
13+
#include <reg/adc.h>
14+
#include <zephyr/logging/log.h>
15+
16+
#define ADC_CONTEXT_USES_KERNEL_TIMER
17+
#include "adc_context.h"
18+
19+
LOG_MODULE_REGISTER(adc_ene_kb106x, CONFIG_ADC_LOG_LEVEL);
20+
21+
struct adc_kb106x_config {
22+
/* ADC Register base address */
23+
struct adc_regs *adc;
24+
/* Pin control */
25+
const struct pinctrl_dev_config *pcfg;
26+
};
27+
28+
struct adc_kb106x_data {
29+
struct adc_context ctx;
30+
const struct device *adc_dev;
31+
uint16_t *buffer;
32+
uint16_t *repeat_buffer;
33+
uint16_t *buf_end;
34+
};
35+
36+
/* ADC local functions */
37+
static bool adc_kb106x_validate_buffer_size(const struct adc_sequence *sequence)
38+
{
39+
int chan_count = 0;
40+
size_t buff_need;
41+
uint32_t chan_mask;
42+
43+
for (chan_mask = 0x80; chan_mask != 0; chan_mask >>= 1) {
44+
if (chan_mask & sequence->channels) {
45+
chan_count++;
46+
}
47+
}
48+
49+
buff_need = chan_count * sizeof(uint16_t);
50+
51+
if (sequence->options) {
52+
buff_need *= 1 + sequence->options->extra_samplings;
53+
}
54+
55+
if (buff_need > sequence->buffer_size) {
56+
return false;
57+
}
58+
59+
return true;
60+
}
61+
62+
/* ADC Sample Flow (by using adc_context.h api function)
63+
* 1. Start ADC sampling (set up flag ctx->sync)
64+
* adc_context_start_read() -> adc_context_start_sampling()
65+
* 2. Wait ADC sample finish (by monitor flag ctx->sync)
66+
* adc_context_wait_for_completion
67+
* 3. Finish ADC sample (isr clear flag ctx->sync)
68+
* adc_context_on_sampling_done -> adc_context_complete
69+
*/
70+
static int adc_kb106x_start_read(const struct device *dev, const struct adc_sequence *sequence)
71+
{
72+
const struct adc_kb106x_config *config = dev->config;
73+
struct adc_kb106x_data *data = dev->data;
74+
int error = 0;
75+
76+
if (!sequence->channels || (sequence->channels & ~BIT_MASK(ADC_MAX_CHAN))) {
77+
LOG_ERR("Invalid ADC channels.");
78+
return -EINVAL;
79+
}
80+
/* Fixed 10 bit resolution of ene ADC */
81+
if (sequence->resolution != ADC_RESOLUTION) {
82+
LOG_ERR("Unfixed 10 bit ADC resolution.");
83+
return -ENOTSUP;
84+
}
85+
/* Check sequence->buffer_size is enough */
86+
if (!adc_kb106x_validate_buffer_size(sequence)) {
87+
LOG_ERR("ADC buffer size too small.");
88+
return -ENOMEM;
89+
}
90+
/* Only support single sampling */
91+
if (sequence->options != NULL) {
92+
LOG_ERR("ADC only support single sampling.");
93+
return -ENOTSUP;
94+
}
95+
96+
/* assign record buffer pointer */
97+
data->buffer = sequence->buffer;
98+
data->buf_end = data->buffer + sequence->buffer_size / sizeof(uint16_t);
99+
/* store device for adc_context_start_read() */
100+
data->adc_dev = dev;
101+
/* Inform adc start sampling */
102+
adc_context_start_read(&data->ctx, sequence);
103+
/* Since kb106x adc has no irq. So need polling the adc conversion
104+
* flag to be valid, then record adc value.
105+
*/
106+
uint32_t channels = (config->adc->ADCCFG & ADC_CHANNEL_BIT_MASK) >> ADC_CHANNEL_BIT_POS;
107+
108+
while (channels) {
109+
int count;
110+
int ch_num;
111+
112+
count = 0;
113+
ch_num = find_lsb_set(channels) - 1;
114+
/* wait valid flag */
115+
while (config->adc->ADCDAT[ch_num] & ADC_INVALID_VALUE) {
116+
k_busy_wait(ADC_WAIT_TIME);
117+
count++;
118+
if (count >= ADC_WAIT_CNT) {
119+
LOG_ERR("ADC busy timeout...");
120+
error = -EBUSY;
121+
break;
122+
}
123+
}
124+
/* check buffer size is enough then record adc value */
125+
if (data->buffer < data->buf_end) {
126+
*data->buffer = (uint16_t)(config->adc->ADCDAT[ch_num]);
127+
data->buffer++;
128+
} else {
129+
error = -EINVAL;
130+
break;
131+
}
132+
133+
/* clear completed channel */
134+
channels &= ~BIT(ch_num);
135+
}
136+
/* Besause polling the adc conversion flag. don't need wait_for_completion*/
137+
138+
/* Inform adc sampling is done */
139+
adc_context_on_sampling_done(&data->ctx, dev);
140+
return error;
141+
}
142+
143+
/* ADC api functions */
144+
static int adc_kb106x_channel_setup(const struct device *dev,
145+
const struct adc_channel_cfg *channel_cfg)
146+
{
147+
if (channel_cfg->channel_id >= ADC_MAX_CHAN) {
148+
LOG_ERR("Invalid channel %d.", channel_cfg->channel_id);
149+
return -EINVAL;
150+
}
151+
152+
if (channel_cfg->acquisition_time != ADC_ACQ_TIME_DEFAULT) {
153+
LOG_ERR("Unsupported channel acquisition time.");
154+
return -ENOTSUP;
155+
}
156+
157+
if (channel_cfg->differential) {
158+
LOG_ERR("Differential channels are not supported.");
159+
return -ENOTSUP;
160+
}
161+
162+
if (channel_cfg->gain != ADC_GAIN_1) {
163+
LOG_ERR("Unsupported channel gain %d.", channel_cfg->gain);
164+
return -ENOTSUP;
165+
}
166+
167+
if (channel_cfg->reference != ADC_REF_INTERNAL) {
168+
LOG_ERR("Unsupported channel reference.");
169+
return -ENOTSUP;
170+
}
171+
LOG_DBG("ADC channel %d configured.", channel_cfg->channel_id);
172+
return 0;
173+
}
174+
175+
static int adc_kb106x_read(const struct device *dev, const struct adc_sequence *sequence)
176+
{
177+
struct adc_kb106x_data *data = dev->data;
178+
int error;
179+
180+
adc_context_lock(&data->ctx, false, NULL);
181+
error = adc_kb106x_start_read(dev, sequence);
182+
adc_context_release(&data->ctx, error);
183+
184+
return error;
185+
}
186+
187+
#if defined(CONFIG_ADC_ASYNC)
188+
static int adc_kb106x_read_async(const struct device *dev, const struct adc_sequence *sequence,
189+
struct k_poll_signal *async)
190+
{
191+
struct adc_kb106x_data *data = dev->data;
192+
int error;
193+
194+
adc_context_lock(&data->ctx, true, async);
195+
error = adc_kb106x_start_read(dev, sequence);
196+
adc_context_release(&data->ctx, error);
197+
198+
return error;
199+
}
200+
#endif /* CONFIG_ADC_ASYNC */
201+
202+
/* ADC api function (using by adc_context.H function) */
203+
static void adc_context_start_sampling(struct adc_context *ctx)
204+
{
205+
struct adc_kb106x_data *data = CONTAINER_OF(ctx, struct adc_kb106x_data, ctx);
206+
const struct device *dev = data->adc_dev;
207+
const struct adc_kb106x_config *config = dev->config;
208+
209+
data->repeat_buffer = data->buffer;
210+
config->adc->ADCCFG = (config->adc->ADCCFG & ~ADC_CHANNEL_BIT_MASK) |
211+
(ctx->sequence.channels << ADC_CHANNEL_BIT_POS);
212+
config->adc->ADCCFG |= ADC_FUNCTION_ENABLE;
213+
}
214+
215+
static void adc_context_update_buffer_pointer(struct adc_context *ctx, bool repeat_sampling)
216+
{
217+
struct adc_kb106x_data *data = CONTAINER_OF(ctx, struct adc_kb106x_data, ctx);
218+
219+
if (repeat_sampling) {
220+
data->buffer = data->repeat_buffer;
221+
}
222+
}
223+
224+
static DEVICE_API(adc, adc_kb106x_api) = {
225+
.channel_setup = adc_kb106x_channel_setup,
226+
.read = adc_kb106x_read,
227+
.ref_internal = ADC_VREF_ANALOG,
228+
#if defined(CONFIG_ADC_ASYNC)
229+
.read_async = adc_kb106x_read_async,
230+
#endif
231+
};
232+
233+
static int adc_kb106x_init(const struct device *dev)
234+
{
235+
const struct adc_kb106x_config *config = dev->config;
236+
struct adc_kb106x_data *data = dev->data;
237+
int ret;
238+
239+
adc_context_unlock_unconditionally(&data->ctx);
240+
/* Configure pin-mux for ADC device */
241+
ret = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);
242+
if (ret < 0) {
243+
LOG_ERR("ADC pinctrl setup failed (%d).", ret);
244+
return ret;
245+
}
246+
247+
return 0;
248+
}
249+
250+
#define ADC_KB106X_DEVICE(inst) \
251+
PINCTRL_DT_INST_DEFINE(inst); \
252+
static struct adc_kb106x_data adc_kb106x_data_##inst = { \
253+
ADC_CONTEXT_INIT_TIMER(adc_kb106x_data_##inst, ctx), \
254+
ADC_CONTEXT_INIT_LOCK(adc_kb106x_data_##inst, ctx), \
255+
ADC_CONTEXT_INIT_SYNC(adc_kb106x_data_##inst, ctx), \
256+
}; \
257+
static const struct adc_kb106x_config adc_kb106x_config_##inst = { \
258+
.adc = (struct adc_regs *)DT_INST_REG_ADDR(inst), \
259+
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(inst), \
260+
}; \
261+
DEVICE_DT_INST_DEFINE(inst, &adc_kb106x_init, NULL, &adc_kb106x_data_##inst, \
262+
&adc_kb106x_config_##inst, PRE_KERNEL_1, \
263+
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &adc_kb106x_api);
264+
265+
DT_INST_FOREACH_STATUS_OKAY(ADC_KB106X_DEVICE)

dts/arm/ene/kb106x/kb106x.dtsi

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@
118118
interrupts = <18 3>;
119119
status = "disabled";
120120
};
121+
122+
adc0: adc@40130000 {
123+
compatible = "ene,kb106x-adc";
124+
reg = <0x40130000 0x100>;
125+
status = "disabled";
126+
#io-channel-cells = <1>;
127+
};
121128
};
122129
};
123130

dts/bindings/adc/ene,kb106x-adc.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright (c) 2025 ENE Technology Inc.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: ENE KB106X ADC controller
5+
6+
compatible: "ene,kb106x-adc"
7+
8+
include: [adc-controller.yaml, pinctrl-device.yaml]
9+
10+
properties:
11+
reg:
12+
required: true
13+
14+
"#io-channel-cells":
15+
const: 1
16+
17+
pinctrl-0:
18+
required: true
19+
20+
pinctrl-names:
21+
required: true
22+
23+
io-channel-cells:
24+
- input

soc/ene/kb106x/common/reg/adc.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2025 ENE Technology Inc.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ENE_KB106X_ADC_H
8+
#define ENE_KB106X_ADC_H
9+
10+
/**
11+
* Structure type to access Analog to Digital Converter (ADC).
12+
*/
13+
struct adc_regs {
14+
volatile uint32_t ADCCFG; /* Configuration Register */
15+
volatile uint32_t Reserved[3]; /* Reserved */
16+
volatile uint32_t ADCDAT[10]; /* Data Register */
17+
};
18+
19+
#define ADC_CHANNEL_BIT_POS 16
20+
#define ADC_CHANNEL_BIT_MASK 0x03FF0000
21+
22+
#define ADC_RESOLUTION 10 /* Unit:bits */
23+
#define ADC_VREF_ANALOG 3300 /* Unit:mV */
24+
#define ADC_MAX_CHAN 10
25+
26+
#define ADC_FUNCTION_ENABLE 0x0001
27+
#define ADC_INVALID_VALUE 0x8000
28+
29+
#define ADC_WAIT_TIME 100
30+
#define ADC_WAIT_CNT 100
31+
32+
#endif /* ENE_KB106X_ADDA_H */

0 commit comments

Comments
 (0)