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