1 /*
2 * Copyright (c) 2021 Antonio Tessarolo
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6 #define DT_DRV_COMPAT nxp_vf610_adc
7
8 #include <errno.h>
9 #include <zephyr/drivers/adc.h>
10 #include <adc_imx6sx.h>
11
12 #include <zephyr/logging/log.h>
13 LOG_MODULE_REGISTER(vf610_adc, CONFIG_ADC_LOG_LEVEL);
14
15 #define ADC_CONTEXT_USES_KERNEL_TIMER
16 #include "adc_context.h"
17
18 struct vf610_adc_config {
19 ADC_Type *base;
20 uint8_t clock_source;
21 uint8_t divide_ratio;
22 void (*irq_config_func)(const struct device *dev);
23 };
24
25 struct vf610_adc_data {
26 const struct device *dev;
27 struct adc_context ctx;
28 uint16_t *buffer;
29 uint16_t *repeat_buffer;
30 uint32_t channels;
31 uint8_t channel_id;
32 };
33
vf610_adc_channel_setup(const struct device * dev,const struct adc_channel_cfg * channel_cfg)34 static int vf610_adc_channel_setup(const struct device *dev,
35 const struct adc_channel_cfg *channel_cfg)
36 {
37 uint8_t channel_id = channel_cfg->channel_id;
38
39 if (channel_id > (ADC_HC0_ADCH_MASK >> ADC_HC0_ADCH_SHIFT)) {
40 LOG_ERR("Channel %d is not valid", channel_id);
41 return -EINVAL;
42 }
43
44 if (channel_cfg->acquisition_time != ADC_ACQ_TIME_DEFAULT) {
45 LOG_ERR("Invalid channel acquisition time");
46 return -EINVAL;
47 }
48
49 if (channel_cfg->differential) {
50 LOG_ERR("Differential channels are not supported");
51 return -EINVAL;
52 }
53
54 if (channel_cfg->gain != ADC_GAIN_1) {
55 LOG_ERR("Invalid channel gain");
56 return -EINVAL;
57 }
58
59 if (channel_cfg->reference != ADC_REF_INTERNAL) {
60 LOG_ERR("Invalid channel reference");
61 return -EINVAL;
62 }
63
64 return 0;
65 }
66
start_read(const struct device * dev,const struct adc_sequence * sequence)67 static int start_read(const struct device *dev, const struct adc_sequence *sequence)
68 {
69 const struct vf610_adc_config *config = dev->config;
70 struct vf610_adc_data *data = dev->data;
71 enum _adc_average_number mode;
72 enum _adc_resolution_mode resolution;
73 int error;
74 ADC_Type *base = config->base;
75
76 switch (sequence->resolution) {
77 case 8:
78 resolution = adcResolutionBit8;
79 break;
80 case 10:
81 resolution = adcResolutionBit10;
82 break;
83 case 12:
84 resolution = adcResolutionBit12;
85 break;
86 default:
87 LOG_ERR("Invalid resolution");
88 return -EINVAL;
89 }
90
91 ADC_SetResolutionMode(base, resolution);
92
93 switch (sequence->oversampling) {
94 case 0:
95 mode = adcAvgNumNone;
96 break;
97 case 2:
98 mode = adcAvgNum4;
99 break;
100 case 3:
101 mode = adcAvgNum8;
102 break;
103 case 4:
104 mode = adcAvgNum16;
105 break;
106 case 5:
107 mode = adcAvgNum32;
108 break;
109 default:
110 LOG_ERR("Invalid oversampling");
111 return -EINVAL;
112 }
113 ADC_SetAverageNum(config->base, mode);
114
115 data->buffer = sequence->buffer;
116
117 adc_context_start_read(&data->ctx, sequence);
118
119 error = adc_context_wait_for_completion(&data->ctx);
120 return error;
121 }
122
vf610_adc_read(const struct device * dev,const struct adc_sequence * sequence)123 static int vf610_adc_read(const struct device *dev,
124 const struct adc_sequence *sequence)
125 {
126 struct vf610_adc_data *data = dev->data;
127 int error;
128
129 adc_context_lock(&data->ctx, false, NULL);
130 error = start_read(dev, sequence);
131 adc_context_release(&data->ctx, error);
132
133 return error;
134 }
135
136 #ifdef CONFIG_ADC_ASYNC
vf610_adc_read_async(struct device * dev,const struct adc_sequence * sequence,struct k_poll_signal * async)137 static int vf610_adc_read_async(struct device *dev,
138 const struct adc_sequence *sequence,
139 struct k_poll_signal *async)
140 {
141 struct vf610_adc_data *data = dev->driver_data;
142 int error;
143
144 adc_context_lock(&data->ctx, true, async);
145 error = start_read(dev, sequence);
146 adc_context_release(&data->ctx, error);
147
148 return error;
149 }
150 #endif
151
vf610_adc_start_channel(const struct device * dev)152 static void vf610_adc_start_channel(const struct device *dev)
153 {
154 const struct vf610_adc_config *config = dev->config;
155 struct vf610_adc_data *data = dev->data;
156
157 data->channel_id = find_lsb_set(data->channels) - 1;
158
159 LOG_DBG("Starting channel %d", data->channel_id);
160
161 ADC_SetIntCmd(config->base, true);
162
163 ADC_TriggerSingleConvert(config->base, data->channel_id);
164 }
165
adc_context_start_sampling(struct adc_context * ctx)166 static void adc_context_start_sampling(struct adc_context *ctx)
167 {
168 struct vf610_adc_data *data =
169 CONTAINER_OF(ctx, struct vf610_adc_data, ctx);
170
171 data->channels = ctx->sequence.channels;
172 data->repeat_buffer = data->buffer;
173
174 vf610_adc_start_channel(data->dev);
175 }
176
adc_context_update_buffer_pointer(struct adc_context * ctx,bool repeat_sampling)177 static void adc_context_update_buffer_pointer(struct adc_context *ctx,
178 bool repeat_sampling)
179 {
180 struct vf610_adc_data *data =
181 CONTAINER_OF(ctx, struct vf610_adc_data, ctx);
182
183 if (repeat_sampling) {
184 data->buffer = data->repeat_buffer;
185 }
186 }
187
vf610_adc_isr(void * arg)188 static void vf610_adc_isr(void *arg)
189 {
190 struct device *dev = (struct device *)arg;
191 const struct vf610_adc_config *config = dev->config;
192 struct vf610_adc_data *data = dev->data;
193 ADC_Type *base = config->base;
194 uint16_t result;
195
196 result = ADC_GetConvertResult(base);
197 LOG_DBG("Finished channel %d. Result is 0x%04x",
198 data->channel_id, result);
199
200 *data->buffer++ = result;
201 data->channels &= ~BIT(data->channel_id);
202
203 if (data->channels) {
204 vf610_adc_start_channel(dev);
205 } else {
206 adc_context_on_sampling_done(&data->ctx, dev);
207 }
208 }
209
vf610_adc_init(const struct device * dev)210 static int vf610_adc_init(const struct device *dev)
211 {
212 const struct vf610_adc_config *config = dev->config;
213 struct vf610_adc_data *data = dev->data;
214 ADC_Type *base = config->base;
215 adc_init_config_t adc_config;
216
217 adc_config.averageNumber = adcAvgNumNone;
218 adc_config.resolutionMode = adcResolutionBit12;
219 adc_config.clockSource = config->clock_source;
220 adc_config.divideRatio = config->divide_ratio;
221
222 ADC_Init(base, &adc_config);
223
224 ADC_SetConvertTrigMode(base, adcSoftwareTrigger);
225
226 ADC_SetCalibration(base, true);
227
228 config->irq_config_func(dev);
229 data->dev = dev;
230
231 adc_context_unlock_unconditionally(&data->ctx);
232
233 return 0;
234 }
235
236 static const struct adc_driver_api vf610_adc_driver_api = {
237 .channel_setup = vf610_adc_channel_setup,
238 .read = vf610_adc_read,
239 #ifdef CONFIG_ADC_ASYNC
240 .read_async = vf610_adc_read_async,
241 #endif
242 };
243
244 #define VF610_ADC_INIT(n) \
245 static void vf610_adc_config_func_##n(const struct device *dev);\
246 \
247 static const struct vf610_adc_config vf610_adc_config_##n = { \
248 .base = (ADC_Type *)DT_INST_REG_ADDR(n), \
249 .clock_source = DT_INST_PROP(n, clk_source), \
250 .divide_ratio = DT_INST_PROP(n, clk_divider), \
251 .irq_config_func = vf610_adc_config_func_##n, \
252 }; \
253 \
254 static struct vf610_adc_data vf610_adc_data_##n = { \
255 ADC_CONTEXT_INIT_TIMER(vf610_adc_data_##n, ctx), \
256 ADC_CONTEXT_INIT_LOCK(vf610_adc_data_##n, ctx), \
257 ADC_CONTEXT_INIT_SYNC(vf610_adc_data_##n, ctx), \
258 }; \
259 \
260 DEVICE_DT_INST_DEFINE(n, &vf610_adc_init, \
261 NULL, &vf610_adc_data_##n, \
262 &vf610_adc_config_##n, POST_KERNEL, \
263 CONFIG_ADC_INIT_PRIORITY, \
264 &vf610_adc_driver_api); \
265 \
266 static void vf610_adc_config_func_##n(const struct device *dev) \
267 { \
268 IRQ_CONNECT(DT_INST_IRQN(n), DT_INST_IRQ(n, priority), \
269 vf610_adc_isr, \
270 DEVICE_DT_INST_GET(n), 0); \
271 \
272 irq_enable(DT_INST_IRQN(n)); \
273 }
274
275 DT_INST_FOREACH_STATUS_OKAY(VF610_ADC_INIT)
276