1 /*
2 * Copyright (c) 2023 Renesas Electronics Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT renesas_smartbond_adc
8
9 #define ADC_CONTEXT_USES_KERNEL_TIMER
10 #include <DA1469xAB.h>
11 #include "adc_context.h"
12 #include <zephyr/dt-bindings/adc/smartbond-adc.h>
13
14 #define LOG_LEVEL CONFIG_ADC_LOG_LEVEL
15 #include <zephyr/logging/log.h>
16 #include <zephyr/irq.h>
17 #include <zephyr/sys/math_extras.h>
18 #include <zephyr/drivers/pinctrl.h>
19
20 LOG_MODULE_REGISTER(adc_smartbond_adc);
21
22 struct adc_smartbond_cfg {
23 const struct pinctrl_dev_config *pcfg;
24 };
25
26 struct adc_smartbond_data {
27 struct adc_context ctx;
28
29 /* Buffer to store channel data */
30 uint16_t *buffer;
31 /* Copy of channel mask from sequence */
32 uint32_t channel_read_mask;
33 /* Number of bits in sequence channels */
34 uint8_t sequence_channel_count;
35 /* Index in buffer to store current value to */
36 uint8_t result_index;
37 };
38
39 #define SMARTBOND_ADC_CHANNEL_COUNT 8
40
41 /*
42 * Channels are handled by software this array holds individual
43 * settings for each channel that must be applied before conversion.
44 */
45 struct adc_smartbond_channel_cfg {
46 uint32_t gp_adc_ctrl_reg;
47 uint32_t gp_adc_ctrl2_reg;
48 } m_channels[SMARTBOND_ADC_CHANNEL_COUNT];
49
50 /* Implementation of the ADC driver API function: adc_channel_setup. */
adc_smartbond_channel_setup(const struct device * dev,const struct adc_channel_cfg * channel_cfg)51 static int adc_smartbond_channel_setup(const struct device *dev,
52 const struct adc_channel_cfg *channel_cfg)
53 {
54 uint8_t channel_id = channel_cfg->channel_id;
55 struct adc_smartbond_channel_cfg *config = &m_channels[channel_id];
56
57 if (channel_id >= SMARTBOND_ADC_CHANNEL_COUNT) {
58 return -EINVAL;
59 }
60
61 if (channel_cfg->acquisition_time != ADC_ACQ_TIME_DEFAULT) {
62 LOG_ERR("Selected ADC acquisition time is not valid");
63 return -EINVAL;
64 }
65
66 if (channel_cfg->differential) {
67 if (channel_cfg->input_positive != SMARTBOND_GPADC_P1_09 &&
68 channel_cfg->input_positive != SMARTBOND_GPADC_P0_08) {
69 LOG_ERR("Differential channels supported only for P1_09 and P0_08");
70 return -EINVAL;
71 }
72 }
73
74 switch (channel_cfg->gain) {
75 case ADC_GAIN_1_3:
76 /* Turn on attenuator and increase sample time to 32 cycles */
77 config->gp_adc_ctrl2_reg = 0x101;
78 break;
79 case ADC_GAIN_1:
80 config->gp_adc_ctrl2_reg = 0;
81 break;
82 default:
83 LOG_ERR("Selected ADC gain is not valid");
84 return -EINVAL;
85 }
86
87 switch (channel_cfg->reference) {
88 case ADC_REF_INTERNAL:
89 break;
90 default:
91 LOG_ERR("Selected ADC reference is not valid");
92 return -EINVAL;
93 }
94
95 config->gp_adc_ctrl_reg =
96 channel_cfg->input_positive << GPADC_GP_ADC_CTRL_REG_GP_ADC_SEL_Pos;
97 if (!channel_cfg->differential) {
98 config->gp_adc_ctrl_reg |= GPADC_GP_ADC_CTRL_REG_GP_ADC_SE_Msk;
99 }
100
101 return 0;
102 }
103
104 #define PER_CHANNEL_ADC_CONFIG_MASK (GPADC_GP_ADC_CTRL_REG_GP_ADC_SEL_Msk | \
105 GPADC_GP_ADC_CTRL_REG_GP_ADC_SE_Msk)
106
pop_count(uint32_t n)107 static int pop_count(uint32_t n)
108 {
109 return __builtin_popcount(n);
110 }
111
adc_context_start_sampling(struct adc_context * ctx)112 static void adc_context_start_sampling(struct adc_context *ctx)
113 {
114 uint32_t val;
115 struct adc_smartbond_data *data =
116 CONTAINER_OF(ctx, struct adc_smartbond_data, ctx);
117 /* Extract lower channel from sequence mask */
118 int current_channel = u32_count_trailing_zeros(data->channel_read_mask);
119
120 if (ctx->sequence.calibrate) {
121 /* TODO: Add calibration code */
122 } else {
123 val = GPADC->GP_ADC_CTRL_REG & ~PER_CHANNEL_ADC_CONFIG_MASK;
124 val |= m_channels[current_channel].gp_adc_ctrl_reg;
125 val |= GPADC_GP_ADC_CTRL_REG_GP_ADC_START_Msk |
126 GPADC_GP_ADC_CTRL_REG_GP_ADC_MINT_Msk;
127
128 GPADC->GP_ADC_CTRL2_REG = m_channels[current_channel].gp_adc_ctrl2_reg;
129 GPADC->GP_ADC_CTRL_REG = val;
130 }
131 }
132
adc_context_update_buffer_pointer(struct adc_context * ctx,bool repeat)133 static void adc_context_update_buffer_pointer(struct adc_context *ctx,
134 bool repeat)
135 {
136 struct adc_smartbond_data *data =
137 CONTAINER_OF(ctx, struct adc_smartbond_data, ctx);
138
139 if (!repeat) {
140 data->buffer += data->sequence_channel_count;
141 }
142 }
143
check_buffer_size(const struct adc_sequence * sequence,uint8_t active_channels)144 static int check_buffer_size(const struct adc_sequence *sequence,
145 uint8_t active_channels)
146 {
147 size_t needed_buffer_size;
148
149 needed_buffer_size = active_channels * sizeof(uint16_t);
150 if (sequence->options) {
151 needed_buffer_size *= (1 + sequence->options->extra_samplings);
152 }
153
154 if (sequence->buffer_size < needed_buffer_size) {
155 LOG_ERR("Provided buffer is too small (%u/%u)",
156 sequence->buffer_size, needed_buffer_size);
157 return -ENOMEM;
158 }
159
160 return 0;
161 }
162
start_read(const struct device * dev,const struct adc_sequence * sequence)163 static int start_read(const struct device *dev,
164 const struct adc_sequence *sequence)
165 {
166 int error;
167 struct adc_smartbond_data *data = dev->data;
168
169 if (sequence->oversampling > 7U) {
170 LOG_ERR("Invalid oversampling");
171 return -EINVAL;
172 }
173
174 if ((sequence->channels == 0) ||
175 ((sequence->channels & ~BIT_MASK(SMARTBOND_ADC_CHANNEL_COUNT)) != 0)) {
176 LOG_ERR("Channel scanning is not supported");
177 return -EINVAL;
178 }
179
180 if (sequence->resolution < 8 || sequence->resolution > 15) {
181 LOG_ERR("ADC resolution value %d is not valid",
182 sequence->resolution);
183 return -EINVAL;
184 }
185
186 error = check_buffer_size(sequence, 1);
187 if (error) {
188 return error;
189 }
190
191 data->buffer = sequence->buffer;
192 data->channel_read_mask = sequence->channels;
193 data->sequence_channel_count = pop_count(sequence->channels);
194 data->result_index = 0;
195
196 adc_context_start_read(&data->ctx, sequence);
197
198 error = adc_context_wait_for_completion(&data->ctx);
199 return error;
200 }
201
adc_smartbond_isr(const struct device * dev)202 static void adc_smartbond_isr(const struct device *dev)
203 {
204 struct adc_smartbond_data *data = dev->data;
205 int current_channel = u32_count_trailing_zeros(data->channel_read_mask);
206
207 GPADC->GP_ADC_CLEAR_INT_REG = 0;
208 /* Store current channel value, result is left justified, move bits right */
209 data->buffer[data->result_index++] = ((uint16_t)GPADC->GP_ADC_RESULT_REG) >>
210 (16 - data->ctx.sequence.resolution);
211 /* Exclude channel from mask for further reading */
212 data->channel_read_mask ^= 1 << current_channel;
213
214 if (data->channel_read_mask == 0) {
215 adc_context_on_sampling_done(&data->ctx, dev);
216 } else {
217 adc_context_start_sampling(&data->ctx);
218 }
219
220 LOG_DBG("%s ISR triggered.", dev->name);
221 }
222
223 /* Implementation of the ADC driver API function: adc_read. */
adc_smartbond_read(const struct device * dev,const struct adc_sequence * sequence)224 static int adc_smartbond_read(const struct device *dev,
225 const struct adc_sequence *sequence)
226 {
227 int error;
228 struct adc_smartbond_data *data = dev->data;
229
230 adc_context_lock(&data->ctx, false, NULL);
231 error = start_read(dev, sequence);
232 adc_context_release(&data->ctx, error);
233
234 return error;
235 }
236
237 #if defined(CONFIG_ADC_ASYNC)
238 /* Implementation of the ADC driver API function: adc_read_sync. */
adc_smartbond_read_async(const struct device * dev,const struct adc_sequence * sequence,struct k_poll_signal * async)239 static int adc_smartbond_read_async(const struct device *dev,
240 const struct adc_sequence *sequence,
241 struct k_poll_signal *async)
242 {
243 struct adc_smartbond_data *data = dev->data;
244 int error;
245
246 adc_context_lock(&data->ctx, true, async);
247 error = start_read(dev, sequence);
248 adc_context_release(&data->ctx, error);
249
250 return error;
251 }
252 #endif /* CONFIG_ADC_ASYNC */
253
adc_smartbond_init(const struct device * dev)254 static int adc_smartbond_init(const struct device *dev)
255 {
256 int err;
257 struct adc_smartbond_data *data = dev->data;
258 const struct adc_smartbond_cfg *config = dev->config;
259
260 GPADC->GP_ADC_CTRL_REG = GPADC_GP_ADC_CTRL_REG_GP_ADC_EN_Msk;
261 GPADC->GP_ADC_CTRL2_REG = 0;
262 GPADC->GP_ADC_CTRL3_REG = 0x40;
263 GPADC->GP_ADC_CLEAR_INT_REG = 0x0;
264
265 /*
266 * Configure dt provided device signals when available.
267 * pinctrl is optional so ENOENT is not setup failure.
268 */
269 err = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);
270 if (err < 0 && err != -ENOENT) {
271 LOG_ERR("ADC pinctrl setup failed (%d)", err);
272 return err;
273 }
274 IRQ_CONNECT(DT_INST_IRQN(0), DT_INST_IRQ(0, priority),
275 adc_smartbond_isr, DEVICE_DT_INST_GET(0), 0);
276
277 NVIC_ClearPendingIRQ(DT_INST_IRQN(0));
278 NVIC_EnableIRQ(DT_INST_IRQN(0));
279
280 adc_context_unlock_unconditionally(&data->ctx);
281
282 return 0;
283 }
284
285 static const struct adc_driver_api adc_smartbond_driver_api = {
286 .channel_setup = adc_smartbond_channel_setup,
287 .read = adc_smartbond_read,
288 #ifdef CONFIG_ADC_ASYNC
289 .read_async = adc_smartbond_read_async,
290 #endif
291 .ref_internal = 1200,
292 };
293
294 /*
295 * There is only one instance on supported SoCs, so inst is guaranteed
296 * to be 0 if any instance is okay. (We use adc_0 above, so the driver
297 * is relying on the numeric instance value in a way that happens to
298 * be safe.)
299 *
300 * Just in case that assumption becomes invalid in the future, we use
301 * a BUILD_ASSERT().
302 */
303 #define ADC_INIT(inst) \
304 BUILD_ASSERT((inst) == 0, \
305 "multiple instances not supported"); \
306 PINCTRL_DT_INST_DEFINE(inst); \
307 static const struct adc_smartbond_cfg adc_smartbond_cfg_##inst = {\
308 .pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(inst), \
309 }; \
310 static struct adc_smartbond_data adc_smartbond_data_##inst = { \
311 ADC_CONTEXT_INIT_TIMER(adc_smartbond_data_##inst, ctx), \
312 ADC_CONTEXT_INIT_LOCK(adc_smartbond_data_##inst, ctx), \
313 ADC_CONTEXT_INIT_SYNC(adc_smartbond_data_##inst, ctx), \
314 }; \
315 DEVICE_DT_INST_DEFINE(0, \
316 adc_smartbond_init, NULL, \
317 &adc_smartbond_data_##inst, \
318 &adc_smartbond_cfg_##inst, \
319 POST_KERNEL, \
320 CONFIG_ADC_INIT_PRIORITY, \
321 &adc_smartbond_driver_api);
322
323 DT_INST_FOREACH_STATUS_OKAY(ADC_INIT)
324