Skip to content

Commit 7d12798

Browse files
ene-stevenfabiobaltieri
authored andcommitted
drivers: adc: initial device driver for ENE KB1200
Add adc driver for ENE KB1200 Signed-off-by: Steven Chang <[email protected]>
1 parent 4d45103 commit 7d12798

File tree

4 files changed

+269
-0
lines changed

4 files changed

+269
-0
lines changed

drivers/adc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,4 @@ zephyr_library_sources_ifdef(CONFIG_ADC_MAX11102_17 adc_max11102_17.c)
4949
zephyr_library_sources_ifdef(CONFIG_ADC_AD5592 adc_ad5592.c)
5050
zephyr_library_sources_ifdef(CONFIG_ADC_LTC2451 adc_ltc2451.c)
5151
zephyr_library_sources_ifdef(CONFIG_ADC_NUMAKER adc_numaker.c)
52+
zephyr_library_sources_ifdef(CONFIG_ADC_ENE_KB1200 adc_ene_kb1200.c)

drivers/adc/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,6 @@ source "drivers/adc/Kconfig.ltc2451"
126126

127127
source "drivers/adc/Kconfig.numaker"
128128

129+
source "drivers/adc/Kconfig.ene"
130+
129131
endif # ADC

drivers/adc/Kconfig.ene

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (c) 2024 ENE Technology Inc.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
config ADC_ENE_KB1200
5+
bool "ENE KB1200 ADC driver"
6+
default y
7+
depends on DT_HAS_ENE_KB1200_ADC_ENABLED
8+
select PINCTRL
9+
help
10+
Enable ADC driver for ENE KB1200.

drivers/adc/adc_ene_kb1200.c

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

0 commit comments

Comments
 (0)