1 /*
2 * Copyright (c) 2018 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define ADC_CONTEXT_USES_KERNEL_TIMER
8 #include "adc_context.h"
9 #include <haly/nrfy_saadc.h>
10 #include <zephyr/dt-bindings/adc/nrf-saadc-v3.h>
11 #include <zephyr/dt-bindings/adc/nrf-saadc-nrf54l.h>
12 #include <zephyr/linker/devicetree_regions.h>
13
14 #define LOG_LEVEL CONFIG_ADC_LOG_LEVEL
15 #include <zephyr/logging/log.h>
16 #include <zephyr/irq.h>
17 LOG_MODULE_REGISTER(adc_nrfx_saadc);
18
19 #define DT_DRV_COMPAT nordic_nrf_saadc
20
21 #if (NRF_SAADC_HAS_AIN_AS_PIN)
22
23 #if defined(CONFIG_NRF_PLATFORM_HALTIUM)
24 static const uint8_t saadc_psels[NRF_SAADC_AIN7 + 1] = {
25 [NRF_SAADC_AIN0] = NRF_PIN_PORT_TO_PIN_NUMBER(0U, 1),
26 [NRF_SAADC_AIN1] = NRF_PIN_PORT_TO_PIN_NUMBER(1U, 1),
27 [NRF_SAADC_AIN2] = NRF_PIN_PORT_TO_PIN_NUMBER(2U, 1),
28 [NRF_SAADC_AIN3] = NRF_PIN_PORT_TO_PIN_NUMBER(3U, 1),
29 [NRF_SAADC_AIN4] = NRF_PIN_PORT_TO_PIN_NUMBER(4U, 1),
30 [NRF_SAADC_AIN5] = NRF_PIN_PORT_TO_PIN_NUMBER(5U, 1),
31 [NRF_SAADC_AIN6] = NRF_PIN_PORT_TO_PIN_NUMBER(6U, 1),
32 [NRF_SAADC_AIN7] = NRF_PIN_PORT_TO_PIN_NUMBER(7U, 1),
33 };
34 #elif defined(CONFIG_SOC_NRF54L05) || defined(CONFIG_SOC_NRF54L10) || defined(CONFIG_SOC_NRF54L15)
35 static const uint32_t saadc_psels[NRF_SAADC_DVDD + 1] = {
36 [NRF_SAADC_AIN0] = NRF_PIN_PORT_TO_PIN_NUMBER(4U, 1),
37 [NRF_SAADC_AIN1] = NRF_PIN_PORT_TO_PIN_NUMBER(5U, 1),
38 [NRF_SAADC_AIN2] = NRF_PIN_PORT_TO_PIN_NUMBER(6U, 1),
39 [NRF_SAADC_AIN3] = NRF_PIN_PORT_TO_PIN_NUMBER(7U, 1),
40 [NRF_SAADC_AIN4] = NRF_PIN_PORT_TO_PIN_NUMBER(11U, 1),
41 [NRF_SAADC_AIN5] = NRF_PIN_PORT_TO_PIN_NUMBER(12U, 1),
42 [NRF_SAADC_AIN6] = NRF_PIN_PORT_TO_PIN_NUMBER(13U, 1),
43 [NRF_SAADC_AIN7] = NRF_PIN_PORT_TO_PIN_NUMBER(14U, 1),
44 [NRF_SAADC_VDD] = NRF_SAADC_INPUT_VDD,
45 [NRF_SAADC_AVDD] = NRF_SAADC_INPUT_AVDD,
46 [NRF_SAADC_DVDD] = NRF_SAADC_INPUT_DVDD,
47 };
48 #endif
49
50 #else
51 BUILD_ASSERT((NRF_SAADC_AIN0 == NRF_SAADC_INPUT_AIN0) &&
52 (NRF_SAADC_AIN1 == NRF_SAADC_INPUT_AIN1) &&
53 (NRF_SAADC_AIN2 == NRF_SAADC_INPUT_AIN2) &&
54 (NRF_SAADC_AIN3 == NRF_SAADC_INPUT_AIN3) &&
55 (NRF_SAADC_AIN4 == NRF_SAADC_INPUT_AIN4) &&
56 (NRF_SAADC_AIN5 == NRF_SAADC_INPUT_AIN5) &&
57 (NRF_SAADC_AIN6 == NRF_SAADC_INPUT_AIN6) &&
58 (NRF_SAADC_AIN7 == NRF_SAADC_INPUT_AIN7) &&
59 #if defined(SAADC_CH_PSELP_PSELP_VDDHDIV5)
60 (NRF_SAADC_VDDHDIV5 == NRF_SAADC_INPUT_VDDHDIV5) &&
61 #endif
62 #if defined(SAADC_CH_PSELP_PSELP_VDD)
63 (NRF_SAADC_VDD == NRF_SAADC_INPUT_VDD) &&
64 #endif
65 1,
66 "Definitions from nrf-adc.h do not match those from nrf_saadc.h");
67 #endif
68
69 #if defined(CONFIG_NRF_PLATFORM_HALTIUM)
70
71 /* Haltium devices always use bounce buffers in RAM */
72
73 #define SAADC_MEMORY_SECTION \
74 COND_CODE_1(DT_NODE_HAS_PROP(DT_NODELABEL(adc), memory_regions), \
75 (__attribute__((__section__(LINKER_DT_NODE_REGION_NAME( \
76 DT_PHANDLE(DT_NODELABEL(adc), memory_regions)))))), \
77 ())
78
79 static uint16_t adc_samples_buffer[SAADC_CH_NUM] SAADC_MEMORY_SECTION;
80
81 #define ADC_BUFFER_IN_RAM
82
83 #endif /* defined(CONFIG_NRF_PLATFORM_HALTIUM) */
84
85 struct driver_data {
86 struct adc_context ctx;
87
88 uint8_t positive_inputs[SAADC_CH_NUM];
89 uint8_t single_ended_channels;
90
91 #if defined(ADC_BUFFER_IN_RAM)
92 void *samples_buffer;
93 void *user_buffer;
94 uint8_t active_channels;
95 #endif
96 };
97
98 static struct driver_data m_data = {
99 ADC_CONTEXT_INIT_TIMER(m_data, ctx),
100 ADC_CONTEXT_INIT_LOCK(m_data, ctx),
101 ADC_CONTEXT_INIT_SYNC(m_data, ctx),
102 #if defined(ADC_BUFFER_IN_RAM)
103 .samples_buffer = adc_samples_buffer,
104 #endif
105 };
106
107 /* Helper function to convert number of samples to the byte representation. */
samples_to_bytes(const struct adc_sequence * sequence,uint16_t number_of_samples)108 static uint32_t samples_to_bytes(const struct adc_sequence *sequence, uint16_t number_of_samples)
109 {
110 if (NRF_SAADC_8BIT_SAMPLE_WIDTH == 8 && sequence->resolution == 8) {
111 return number_of_samples;
112 }
113
114 return number_of_samples * 2;
115 }
116
117 /* Helper function to convert acquisition time to register TACQ value. */
adc_convert_acq_time(uint16_t acquisition_time,nrf_saadc_acqtime_t * p_tacq_val)118 static int adc_convert_acq_time(uint16_t acquisition_time, nrf_saadc_acqtime_t *p_tacq_val)
119 {
120 int result = 0;
121
122 #if NRF_SAADC_HAS_ACQTIME_ENUM
123 switch (acquisition_time) {
124 case ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 3):
125 *p_tacq_val = NRF_SAADC_ACQTIME_3US;
126 break;
127 case ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 5):
128 *p_tacq_val = NRF_SAADC_ACQTIME_5US;
129 break;
130 case ADC_ACQ_TIME_DEFAULT:
131 case ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 10):
132 *p_tacq_val = NRF_SAADC_ACQTIME_10US;
133 break;
134 case ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 15):
135 *p_tacq_val = NRF_SAADC_ACQTIME_15US;
136 break;
137 case ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 20):
138 *p_tacq_val = NRF_SAADC_ACQTIME_20US;
139 break;
140 case ADC_ACQ_TIME_MAX:
141 case ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 40):
142 *p_tacq_val = NRF_SAADC_ACQTIME_40US;
143 break;
144 default:
145 result = -EINVAL;
146 }
147 #else
148 #define MINIMUM_ACQ_TIME_IN_NS 125
149 #define DEFAULT_ACQ_TIME_IN_NS 10000
150
151 nrf_saadc_acqtime_t tacq = 0;
152 uint16_t acq_time =
153 (acquisition_time == ADC_ACQ_TIME_DEFAULT
154 ? DEFAULT_ACQ_TIME_IN_NS
155 : (ADC_ACQ_TIME_VALUE(acquisition_time) *
156 (ADC_ACQ_TIME_UNIT(acquisition_time) == ADC_ACQ_TIME_MICROSECONDS
157 ? 1000
158 : 1)));
159
160 tacq = (nrf_saadc_acqtime_t)(acq_time / MINIMUM_ACQ_TIME_IN_NS) - 1;
161 if ((tacq > NRF_SAADC_ACQTIME_MAX) || (acq_time < MINIMUM_ACQ_TIME_IN_NS)) {
162 result = -EINVAL;
163 } else {
164 *p_tacq_val = tacq;
165 }
166 #endif
167
168 return result;
169 }
170
171 /* Implementation of the ADC driver API function: adc_channel_setup. */
adc_nrfx_channel_setup(const struct device * dev,const struct adc_channel_cfg * channel_cfg)172 static int adc_nrfx_channel_setup(const struct device *dev,
173 const struct adc_channel_cfg *channel_cfg)
174 {
175 nrf_saadc_channel_config_t config = {
176 #if NRF_SAADC_HAS_CH_CONFIG_RES
177 .resistor_p = NRF_SAADC_RESISTOR_DISABLED,
178 .resistor_n = NRF_SAADC_RESISTOR_DISABLED,
179 #endif
180 .burst = NRF_SAADC_BURST_DISABLED,
181 };
182 uint8_t channel_id = channel_cfg->channel_id;
183 uint32_t input_negative = channel_cfg->input_negative;
184
185 if (channel_id >= SAADC_CH_NUM) {
186 return -EINVAL;
187 }
188
189 switch (channel_cfg->gain) {
190 #if defined(SAADC_CH_CONFIG_GAIN_Gain1_6)
191 case ADC_GAIN_1_6:
192 config.gain = NRF_SAADC_GAIN1_6;
193 break;
194 #endif
195 #if defined(SAADC_CH_CONFIG_GAIN_Gain1_5)
196 case ADC_GAIN_1_5:
197 config.gain = NRF_SAADC_GAIN1_5;
198 break;
199 #endif
200 #if defined(SAADC_CH_CONFIG_GAIN_Gain1_4) || defined(SAADC_CH_CONFIG_GAIN_Gain2_8)
201 case ADC_GAIN_1_4:
202 config.gain = NRF_SAADC_GAIN1_4;
203 break;
204 #endif
205 #if defined(SAADC_CH_CONFIG_GAIN_Gain1_3) || defined(SAADC_CH_CONFIG_GAIN_Gain2_6)
206 case ADC_GAIN_1_3:
207 config.gain = NRF_SAADC_GAIN1_3;
208 break;
209 #endif
210 #if defined(SAADC_CH_CONFIG_GAIN_Gain2_5)
211 case ADC_GAIN_2_5:
212 config.gain = NRF_SAADC_GAIN2_5;
213 break;
214 #endif
215 #if defined(SAADC_CH_CONFIG_GAIN_Gain1_2) || defined(SAADC_CH_CONFIG_GAIN_Gain2_4)
216 case ADC_GAIN_1_2:
217 config.gain = NRF_SAADC_GAIN1_2;
218 break;
219 #endif
220 #if defined(SAADC_CH_CONFIG_GAIN_Gain2_3)
221 case ADC_GAIN_2_3:
222 config.gain = NRF_SAADC_GAIN2_3;
223 break;
224 #endif
225 case ADC_GAIN_1:
226 config.gain = NRF_SAADC_GAIN1;
227 break;
228 case ADC_GAIN_2:
229 config.gain = NRF_SAADC_GAIN2;
230 break;
231 #if defined(SAADC_CH_CONFIG_GAIN_Gain4)
232 case ADC_GAIN_4:
233 config.gain = NRF_SAADC_GAIN4;
234 break;
235 #endif
236 default:
237 LOG_ERR("Selected ADC gain is not valid");
238 return -EINVAL;
239 }
240
241 switch (channel_cfg->reference) {
242 #if defined(SAADC_CH_CONFIG_REFSEL_Internal)
243 case ADC_REF_INTERNAL:
244 config.reference = NRF_SAADC_REFERENCE_INTERNAL;
245 break;
246 #endif
247 #if defined(SAADC_CH_CONFIG_REFSEL_VDD1_4)
248 case ADC_REF_VDD_1_4:
249 config.reference = NRF_SAADC_REFERENCE_VDD4;
250 break;
251 #endif
252 #if defined(SAADC_CH_CONFIG_REFSEL_External)
253 case ADC_REF_EXTERNAL0:
254 config.reference = NRF_SAADC_REFERENCE_EXTERNAL;
255 break;
256 #endif
257 default:
258 LOG_ERR("Selected ADC reference is not valid");
259 return -EINVAL;
260 }
261
262 int ret = adc_convert_acq_time(channel_cfg->acquisition_time, &config.acq_time);
263
264 if (ret) {
265 LOG_ERR("Selected ADC acquisition time is not valid");
266 return -EINVAL;
267 }
268
269 /* Store channel mode to allow correcting negative readings in single-ended mode
270 * after ADC sequence ends.
271 */
272 if (channel_cfg->differential) {
273 config.mode = NRF_SAADC_MODE_DIFFERENTIAL;
274 m_data.single_ended_channels &= ~BIT(channel_cfg->channel_id);
275 } else {
276 config.mode = NRF_SAADC_MODE_SINGLE_ENDED;
277 m_data.single_ended_channels |= BIT(channel_cfg->channel_id);
278 }
279
280 #if (NRF_SAADC_HAS_AIN_AS_PIN)
281 if ((channel_cfg->input_positive >= ARRAY_SIZE(saadc_psels)) ||
282 (channel_cfg->input_positive < NRF_SAADC_AIN0)) {
283 return -EINVAL;
284 }
285
286 if (config.mode == NRF_SAADC_MODE_DIFFERENTIAL) {
287 if (input_negative > NRF_SAADC_AIN7 ||
288 input_negative < NRF_SAADC_AIN0) {
289 return -EINVAL;
290 }
291
292 input_negative = saadc_psels[input_negative];
293 } else {
294 input_negative = NRF_SAADC_INPUT_DISABLED;
295 }
296 #endif
297 /* Store the positive input selection in a dedicated array,
298 * to get it later when the channel is selected for a sampling
299 * and to mark the channel as configured (ready to be selected).
300 */
301 m_data.positive_inputs[channel_id] = channel_cfg->input_positive;
302
303 nrf_saadc_channel_init(NRF_SAADC, channel_id, &config);
304 /* Keep the channel disabled in hardware (set positive input to
305 * NRF_SAADC_INPUT_DISABLED) until it is selected to be included
306 * in a sampling sequence.
307 */
308 nrf_saadc_channel_input_set(NRF_SAADC,
309 channel_id,
310 NRF_SAADC_INPUT_DISABLED,
311 input_negative);
312
313 return 0;
314 }
315
adc_context_start_sampling(struct adc_context * ctx)316 static void adc_context_start_sampling(struct adc_context *ctx)
317 {
318 nrf_saadc_enable(NRF_SAADC);
319
320 if (ctx->sequence.calibrate) {
321 nrf_saadc_task_trigger(NRF_SAADC,
322 NRF_SAADC_TASK_CALIBRATEOFFSET);
323 } else {
324 nrf_saadc_task_trigger(NRF_SAADC, NRF_SAADC_TASK_START);
325 nrf_saadc_task_trigger(NRF_SAADC, NRF_SAADC_TASK_SAMPLE);
326 }
327 }
328
adc_context_update_buffer_pointer(struct adc_context * ctx,bool repeat)329 static void adc_context_update_buffer_pointer(struct adc_context *ctx,
330 bool repeat)
331 {
332 ARG_UNUSED(ctx);
333
334 if (!repeat) {
335 #if defined(ADC_BUFFER_IN_RAM)
336 m_data.user_buffer = (uint8_t *)m_data.user_buffer +
337 samples_to_bytes(&ctx->sequence, nrfy_saadc_amount_get(NRF_SAADC));
338 #else
339 nrf_saadc_value_t *buffer =
340 (uint8_t *)nrf_saadc_buffer_pointer_get(NRF_SAADC) +
341 samples_to_bytes(&ctx->sequence, nrfy_saadc_amount_get(NRF_SAADC));
342 nrfy_saadc_buffer_pointer_set(NRF_SAADC, buffer);
343 #endif
344 }
345 }
346
set_resolution(const struct adc_sequence * sequence)347 static int set_resolution(const struct adc_sequence *sequence)
348 {
349 nrf_saadc_resolution_t nrf_resolution;
350
351 switch (sequence->resolution) {
352 case 8:
353 nrf_resolution = NRF_SAADC_RESOLUTION_8BIT;
354 break;
355 case 10:
356 nrf_resolution = NRF_SAADC_RESOLUTION_10BIT;
357 break;
358 case 12:
359 nrf_resolution = NRF_SAADC_RESOLUTION_12BIT;
360 break;
361 case 14:
362 nrf_resolution = NRF_SAADC_RESOLUTION_14BIT;
363 break;
364 default:
365 LOG_ERR("ADC resolution value %d is not valid",
366 sequence->resolution);
367 return -EINVAL;
368 }
369
370 nrf_saadc_resolution_set(NRF_SAADC, nrf_resolution);
371 return 0;
372 }
373
set_oversampling(const struct adc_sequence * sequence,uint8_t active_channels)374 static int set_oversampling(const struct adc_sequence *sequence,
375 uint8_t active_channels)
376 {
377 nrf_saadc_oversample_t nrf_oversampling;
378
379 if ((active_channels > 1) && (sequence->oversampling > 0)) {
380 LOG_ERR(
381 "Oversampling is supported for single channel only");
382 return -EINVAL;
383 }
384
385 switch (sequence->oversampling) {
386 case 0:
387 nrf_oversampling = NRF_SAADC_OVERSAMPLE_DISABLED;
388 break;
389 case 1:
390 nrf_oversampling = NRF_SAADC_OVERSAMPLE_2X;
391 break;
392 case 2:
393 nrf_oversampling = NRF_SAADC_OVERSAMPLE_4X;
394 break;
395 case 3:
396 nrf_oversampling = NRF_SAADC_OVERSAMPLE_8X;
397 break;
398 case 4:
399 nrf_oversampling = NRF_SAADC_OVERSAMPLE_16X;
400 break;
401 case 5:
402 nrf_oversampling = NRF_SAADC_OVERSAMPLE_32X;
403 break;
404 case 6:
405 nrf_oversampling = NRF_SAADC_OVERSAMPLE_64X;
406 break;
407 case 7:
408 nrf_oversampling = NRF_SAADC_OVERSAMPLE_128X;
409 break;
410 case 8:
411 nrf_oversampling = NRF_SAADC_OVERSAMPLE_256X;
412 break;
413 default:
414 LOG_ERR("Oversampling value %d is not valid",
415 sequence->oversampling);
416 return -EINVAL;
417 }
418
419 nrf_saadc_oversample_set(NRF_SAADC, nrf_oversampling);
420 return 0;
421 }
422
check_buffer_size(const struct adc_sequence * sequence,uint8_t active_channels)423 static int check_buffer_size(const struct adc_sequence *sequence,
424 uint8_t active_channels)
425 {
426 size_t needed_buffer_size;
427
428 needed_buffer_size = samples_to_bytes(sequence, active_channels);
429
430 if (sequence->options) {
431 needed_buffer_size *= (1 + sequence->options->extra_samplings);
432 }
433
434 if (sequence->buffer_size < needed_buffer_size) {
435 LOG_ERR("Provided buffer is too small (%u/%u)",
436 sequence->buffer_size, needed_buffer_size);
437 return -ENOMEM;
438 }
439
440 return 0;
441 }
442
has_single_ended(const struct adc_sequence * sequence)443 static bool has_single_ended(const struct adc_sequence *sequence)
444 {
445 return sequence->channels & m_data.single_ended_channels;
446 }
447
correct_single_ended(const struct adc_sequence * sequence)448 static void correct_single_ended(const struct adc_sequence *sequence)
449 {
450 uint16_t channel_bit = BIT(0);
451 uint8_t selected_channels = sequence->channels;
452 uint8_t single_ended_channels = m_data.single_ended_channels;
453 int16_t *sample = nrf_saadc_buffer_pointer_get(NRF_SAADC);
454
455 while (channel_bit <= single_ended_channels) {
456 if (channel_bit & selected_channels) {
457 if ((channel_bit & single_ended_channels) && (*sample < 0)) {
458 *sample = 0;
459 }
460
461 sample++;
462 }
463
464 channel_bit <<= 1;
465 }
466 }
467
start_read(const struct device * dev,const struct adc_sequence * sequence)468 static int start_read(const struct device *dev,
469 const struct adc_sequence *sequence)
470 {
471 int error;
472 uint32_t selected_channels = sequence->channels;
473 uint8_t resolution = sequence->resolution;
474 uint8_t active_channels;
475 uint8_t channel_id;
476
477 /* Signal an error if channel selection is invalid (no channels or
478 * a non-existing one is selected).
479 */
480 if (!selected_channels ||
481 (selected_channels & ~BIT_MASK(SAADC_CH_NUM))) {
482 LOG_ERR("Invalid selection of channels");
483 return -EINVAL;
484 }
485
486 active_channels = 0U;
487
488 /* Enable only the channels selected for the pointed sequence.
489 * Disable all the rest.
490 */
491 channel_id = 0U;
492 do {
493 if (selected_channels & BIT(channel_id)) {
494 /* Signal an error if a selected channel has not been
495 * configured yet.
496 */
497 if (m_data.positive_inputs[channel_id] == 0U) {
498 LOG_ERR("Channel %u not configured",
499 channel_id);
500 return -EINVAL;
501 }
502 /* Signal an error if the channel is configured as
503 * single ended with a resolution which is identical
504 * to the sample bit size. The SAADC's "single ended"
505 * mode is really differential mode with the
506 * negative input tied to ground. We can therefore
507 * observe negative values if the positive input falls
508 * below ground. If the sample bitsize is larger than
509 * the resolution, we can detect negative values and
510 * correct them to 0 after the sequencen has ended.
511 */
512 if ((m_data.single_ended_channels & BIT(channel_id)) &&
513 (NRF_SAADC_8BIT_SAMPLE_WIDTH == 8 && resolution == 8)) {
514 LOG_ERR("Channel %u invalid single ended resolution",
515 channel_id);
516 return -EINVAL;
517 }
518 /* When oversampling is used, the burst mode needs to
519 * be activated. Unfortunately, this mode cannot be
520 * activated permanently in the channel setup, because
521 * then the multiple channel sampling fails (the END
522 * event is not generated) after switching to a single
523 * channel sampling and back. Thus, when oversampling
524 * is not used (hence, the multiple channel sampling is
525 * possible), the burst mode have to be deactivated.
526 */
527 nrf_saadc_burst_set(NRF_SAADC, channel_id,
528 (sequence->oversampling != 0U ?
529 NRF_SAADC_BURST_ENABLED :
530 NRF_SAADC_BURST_DISABLED));
531 nrf_saadc_channel_pos_input_set(
532 NRF_SAADC,
533 channel_id,
534 #if NRF_SAADC_HAS_AIN_AS_PIN
535 saadc_psels[m_data.positive_inputs[channel_id]]
536 #else
537 m_data.positive_inputs[channel_id]
538 #endif
539 );
540 ++active_channels;
541 } else {
542 nrf_saadc_burst_set(
543 NRF_SAADC,
544 channel_id,
545 NRF_SAADC_BURST_DISABLED);
546 nrf_saadc_channel_pos_input_set(
547 NRF_SAADC,
548 channel_id,
549 NRF_SAADC_INPUT_DISABLED);
550 }
551 } while (++channel_id < SAADC_CH_NUM);
552
553 error = set_resolution(sequence);
554 if (error) {
555 return error;
556 }
557
558 error = set_oversampling(sequence, active_channels);
559 if (error) {
560 return error;
561 }
562
563 error = check_buffer_size(sequence, active_channels);
564 if (error) {
565 return error;
566 }
567
568 #if defined(ADC_BUFFER_IN_RAM)
569 m_data.user_buffer = sequence->buffer;
570 m_data.active_channels = active_channels;
571
572 nrf_saadc_buffer_init(NRF_SAADC,
573 (nrf_saadc_value_t *)m_data.samples_buffer,
574 active_channels);
575 #else
576 nrf_saadc_buffer_init(NRF_SAADC,
577 (nrf_saadc_value_t *)sequence->buffer,
578 active_channels);
579 #endif
580
581 adc_context_start_read(&m_data.ctx, sequence);
582
583 return adc_context_wait_for_completion(&m_data.ctx);
584 }
585
586 /* Implementation of the ADC driver API function: adc_read. */
adc_nrfx_read(const struct device * dev,const struct adc_sequence * sequence)587 static int adc_nrfx_read(const struct device *dev,
588 const struct adc_sequence *sequence)
589 {
590 int error;
591
592 adc_context_lock(&m_data.ctx, false, NULL);
593 error = start_read(dev, sequence);
594 adc_context_release(&m_data.ctx, error);
595
596 return error;
597 }
598
599 #ifdef CONFIG_ADC_ASYNC
600 /* Implementation of the ADC driver API function: adc_read_async. */
adc_nrfx_read_async(const struct device * dev,const struct adc_sequence * sequence,struct k_poll_signal * async)601 static int adc_nrfx_read_async(const struct device *dev,
602 const struct adc_sequence *sequence,
603 struct k_poll_signal *async)
604 {
605 int error;
606
607 adc_context_lock(&m_data.ctx, true, async);
608 error = start_read(dev, sequence);
609 adc_context_release(&m_data.ctx, error);
610
611 return error;
612 }
613 #endif /* CONFIG_ADC_ASYNC */
614
saadc_irq_handler(const struct device * dev)615 static void saadc_irq_handler(const struct device *dev)
616 {
617 if (nrf_saadc_event_check(NRF_SAADC, NRF_SAADC_EVENT_END)) {
618 nrf_saadc_event_clear(NRF_SAADC, NRF_SAADC_EVENT_END);
619
620 nrf_saadc_task_trigger(NRF_SAADC, NRF_SAADC_TASK_STOP);
621 nrf_saadc_disable(NRF_SAADC);
622
623 if (has_single_ended(&m_data.ctx.sequence)) {
624 correct_single_ended(&m_data.ctx.sequence);
625 }
626
627 #if defined(ADC_BUFFER_IN_RAM)
628 memcpy(m_data.user_buffer, m_data.samples_buffer,
629 samples_to_bytes(&m_data.ctx.sequence, m_data.active_channels));
630 #endif
631
632 adc_context_on_sampling_done(&m_data.ctx, dev);
633 } else if (nrf_saadc_event_check(NRF_SAADC,
634 NRF_SAADC_EVENT_CALIBRATEDONE)) {
635 nrf_saadc_event_clear(NRF_SAADC, NRF_SAADC_EVENT_CALIBRATEDONE);
636
637 /*
638 * The workaround for Nordic nRF52832 anomalies 86 and
639 * 178 is an explicit STOP after CALIBRATEOFFSET
640 * before issuing START.
641 */
642 nrf_saadc_task_trigger(NRF_SAADC, NRF_SAADC_TASK_STOP);
643 nrf_saadc_task_trigger(NRF_SAADC, NRF_SAADC_TASK_START);
644 nrf_saadc_task_trigger(NRF_SAADC, NRF_SAADC_TASK_SAMPLE);
645 }
646 }
647
init_saadc(const struct device * dev)648 static int init_saadc(const struct device *dev)
649 {
650 nrf_saadc_event_clear(NRF_SAADC, NRF_SAADC_EVENT_END);
651 nrf_saadc_event_clear(NRF_SAADC, NRF_SAADC_EVENT_CALIBRATEDONE);
652 nrf_saadc_int_enable(NRF_SAADC,
653 NRF_SAADC_INT_END | NRF_SAADC_INT_CALIBRATEDONE);
654 NRFX_IRQ_ENABLE(DT_INST_IRQN(0));
655
656 IRQ_CONNECT(DT_INST_IRQN(0), DT_INST_IRQ(0, priority),
657 saadc_irq_handler, DEVICE_DT_INST_GET(0), 0);
658
659 adc_context_unlock_unconditionally(&m_data.ctx);
660
661 return 0;
662 }
663
664 static DEVICE_API(adc, adc_nrfx_driver_api) = {
665 .channel_setup = adc_nrfx_channel_setup,
666 .read = adc_nrfx_read,
667 #ifdef CONFIG_ADC_ASYNC
668 .read_async = adc_nrfx_read_async,
669 #endif
670 #if defined(CONFIG_SOC_NRF54L05) || defined(CONFIG_SOC_NRF54L10) || defined(CONFIG_SOC_NRF54L15)
671 .ref_internal = 900,
672 #elif defined(CONFIG_NRF_PLATFORM_HALTIUM)
673 .ref_internal = 1024,
674 #else
675 .ref_internal = 600,
676 #endif
677 };
678
679 /*
680 * There is only one instance on supported SoCs, so inst is guaranteed
681 * to be 0 if any instance is okay. (We use adc_0 above, so the driver
682 * is relying on the numeric instance value in a way that happens to
683 * be safe.)
684 *
685 * Just in case that assumption becomes invalid in the future, we use
686 * a BUILD_ASSERT().
687 */
688 #define SAADC_INIT(inst) \
689 BUILD_ASSERT((inst) == 0, \
690 "multiple instances not supported"); \
691 DEVICE_DT_INST_DEFINE(0, \
692 init_saadc, \
693 NULL, \
694 NULL, \
695 NULL, \
696 POST_KERNEL, \
697 CONFIG_ADC_INIT_PRIORITY, \
698 &adc_nrfx_driver_api);
699
700 DT_INST_FOREACH_STATUS_OKAY(SAADC_INIT)
701