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 <hal/nrf_saadc.h>
10 
11 #define LOG_LEVEL CONFIG_ADC_LOG_LEVEL
12 #include <logging/log.h>
13 LOG_MODULE_REGISTER(adc_nrfx_saadc);
14 
15 #define DT_DRV_COMPAT nordic_nrf_saadc
16 
17 struct driver_data {
18 	struct adc_context ctx;
19 
20 	uint8_t positive_inputs[SAADC_CH_NUM];
21 };
22 
23 static struct driver_data m_data = {
24 	ADC_CONTEXT_INIT_TIMER(m_data, ctx),
25 	ADC_CONTEXT_INIT_LOCK(m_data, ctx),
26 	ADC_CONTEXT_INIT_SYNC(m_data, ctx),
27 };
28 
29 
30 /* 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)31 static int adc_nrfx_channel_setup(const struct device *dev,
32 				  const struct adc_channel_cfg *channel_cfg)
33 {
34 	nrf_saadc_channel_config_t config = {
35 		.resistor_p = NRF_SAADC_RESISTOR_DISABLED,
36 		.resistor_n = NRF_SAADC_RESISTOR_DISABLED,
37 		.burst      = NRF_SAADC_BURST_DISABLED,
38 	};
39 	uint8_t channel_id = channel_cfg->channel_id;
40 
41 	if (channel_id >= SAADC_CH_NUM) {
42 		return -EINVAL;
43 	}
44 
45 	switch (channel_cfg->gain) {
46 	case ADC_GAIN_1_6:
47 		config.gain = NRF_SAADC_GAIN1_6;
48 		break;
49 	case ADC_GAIN_1_5:
50 		config.gain = NRF_SAADC_GAIN1_5;
51 		break;
52 	case ADC_GAIN_1_4:
53 		config.gain = NRF_SAADC_GAIN1_4;
54 		break;
55 	case ADC_GAIN_1_3:
56 		config.gain = NRF_SAADC_GAIN1_3;
57 		break;
58 	case ADC_GAIN_1_2:
59 		config.gain = NRF_SAADC_GAIN1_2;
60 		break;
61 	case ADC_GAIN_1:
62 		config.gain = NRF_SAADC_GAIN1;
63 		break;
64 	case ADC_GAIN_2:
65 		config.gain = NRF_SAADC_GAIN2;
66 		break;
67 	case ADC_GAIN_4:
68 		config.gain = NRF_SAADC_GAIN4;
69 		break;
70 	default:
71 		LOG_ERR("Selected ADC gain is not valid");
72 		return -EINVAL;
73 	}
74 
75 	switch (channel_cfg->reference) {
76 	case ADC_REF_INTERNAL:
77 		config.reference = NRF_SAADC_REFERENCE_INTERNAL;
78 		break;
79 	case ADC_REF_VDD_1_4:
80 		config.reference = NRF_SAADC_REFERENCE_VDD4;
81 		break;
82 	default:
83 		LOG_ERR("Selected ADC reference is not valid");
84 		return -EINVAL;
85 	}
86 
87 	switch (channel_cfg->acquisition_time) {
88 	case ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 3):
89 		config.acq_time = NRF_SAADC_ACQTIME_3US;
90 		break;
91 	case ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 5):
92 		config.acq_time = NRF_SAADC_ACQTIME_5US;
93 		break;
94 	case ADC_ACQ_TIME_DEFAULT:
95 	case ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 10):
96 		config.acq_time = NRF_SAADC_ACQTIME_10US;
97 		break;
98 	case ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 15):
99 		config.acq_time = NRF_SAADC_ACQTIME_15US;
100 		break;
101 	case ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 20):
102 		config.acq_time = NRF_SAADC_ACQTIME_20US;
103 		break;
104 	case ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 40):
105 		config.acq_time = NRF_SAADC_ACQTIME_40US;
106 		break;
107 	default:
108 		LOG_ERR("Selected ADC acquisition time is not valid");
109 		return -EINVAL;
110 	}
111 
112 	config.mode = (channel_cfg->differential ?
113 		NRF_SAADC_MODE_DIFFERENTIAL : NRF_SAADC_MODE_SINGLE_ENDED);
114 
115 	/* Keep the channel disabled in hardware (set positive input to
116 	 * NRF_SAADC_INPUT_DISABLED) until it is selected to be included
117 	 * in a sampling sequence.
118 	 */
119 
120 	nrf_saadc_channel_init(NRF_SAADC, channel_id, &config);
121 	nrf_saadc_channel_input_set(NRF_SAADC,
122 				    channel_id,
123 				    NRF_SAADC_INPUT_DISABLED,
124 				    channel_cfg->input_negative);
125 
126 	/* Store the positive input selection in a dedicated array,
127 	 * to get it later when the channel is selected for a sampling
128 	 * and to mark the channel as configured (ready to be selected).
129 	 */
130 	m_data.positive_inputs[channel_id] = channel_cfg->input_positive;
131 
132 	return 0;
133 }
134 
adc_context_start_sampling(struct adc_context * ctx)135 static void adc_context_start_sampling(struct adc_context *ctx)
136 {
137 	nrf_saadc_enable(NRF_SAADC);
138 
139 	if (ctx->sequence.calibrate) {
140 		nrf_saadc_task_trigger(NRF_SAADC,
141 				       NRF_SAADC_TASK_CALIBRATEOFFSET);
142 	} else {
143 		nrf_saadc_task_trigger(NRF_SAADC, NRF_SAADC_TASK_START);
144 		nrf_saadc_task_trigger(NRF_SAADC, NRF_SAADC_TASK_SAMPLE);
145 	}
146 }
147 
adc_context_update_buffer_pointer(struct adc_context * ctx,bool repeat)148 static void adc_context_update_buffer_pointer(struct adc_context *ctx,
149 					      bool repeat)
150 {
151 	ARG_UNUSED(ctx);
152 
153 	if (!repeat) {
154 		nrf_saadc_buffer_pointer_set(
155 			NRF_SAADC,
156 			nrf_saadc_buffer_pointer_get(NRF_SAADC) +
157 			nrf_saadc_amount_get(NRF_SAADC));
158 	}
159 }
160 
set_resolution(const struct adc_sequence * sequence)161 static int set_resolution(const struct adc_sequence *sequence)
162 {
163 	nrf_saadc_resolution_t nrf_resolution;
164 
165 	switch (sequence->resolution) {
166 	case  8:
167 		nrf_resolution = NRF_SAADC_RESOLUTION_8BIT;
168 		break;
169 	case 10:
170 		nrf_resolution = NRF_SAADC_RESOLUTION_10BIT;
171 		break;
172 	case 12:
173 		nrf_resolution = NRF_SAADC_RESOLUTION_12BIT;
174 		break;
175 	case 14:
176 		nrf_resolution = NRF_SAADC_RESOLUTION_14BIT;
177 		break;
178 	default:
179 		LOG_ERR("ADC resolution value %d is not valid",
180 			    sequence->resolution);
181 		return -EINVAL;
182 	}
183 
184 	nrf_saadc_resolution_set(NRF_SAADC, nrf_resolution);
185 	return 0;
186 }
187 
set_oversampling(const struct adc_sequence * sequence,uint8_t active_channels)188 static int set_oversampling(const struct adc_sequence *sequence,
189 			    uint8_t active_channels)
190 {
191 	nrf_saadc_oversample_t nrf_oversampling;
192 
193 	if ((active_channels > 1) && (sequence->oversampling > 0)) {
194 		LOG_ERR(
195 			"Oversampling is supported for single channel only");
196 		return -EINVAL;
197 	}
198 
199 	switch (sequence->oversampling) {
200 	case 0:
201 		nrf_oversampling = NRF_SAADC_OVERSAMPLE_DISABLED;
202 		break;
203 	case 1:
204 		nrf_oversampling = NRF_SAADC_OVERSAMPLE_2X;
205 		break;
206 	case 2:
207 		nrf_oversampling = NRF_SAADC_OVERSAMPLE_4X;
208 		break;
209 	case 3:
210 		nrf_oversampling = NRF_SAADC_OVERSAMPLE_8X;
211 		break;
212 	case 4:
213 		nrf_oversampling = NRF_SAADC_OVERSAMPLE_16X;
214 		break;
215 	case 5:
216 		nrf_oversampling = NRF_SAADC_OVERSAMPLE_32X;
217 		break;
218 	case 6:
219 		nrf_oversampling = NRF_SAADC_OVERSAMPLE_64X;
220 		break;
221 	case 7:
222 		nrf_oversampling = NRF_SAADC_OVERSAMPLE_128X;
223 		break;
224 	case 8:
225 		nrf_oversampling = NRF_SAADC_OVERSAMPLE_256X;
226 		break;
227 	default:
228 		LOG_ERR("Oversampling value %d is not valid",
229 			    sequence->oversampling);
230 		return -EINVAL;
231 	}
232 
233 	nrf_saadc_oversample_set(NRF_SAADC, nrf_oversampling);
234 	return 0;
235 }
236 
check_buffer_size(const struct adc_sequence * sequence,uint8_t active_channels)237 static int check_buffer_size(const struct adc_sequence *sequence,
238 			     uint8_t active_channels)
239 {
240 	size_t needed_buffer_size;
241 
242 	needed_buffer_size = active_channels * sizeof(nrf_saadc_value_t);
243 	if (sequence->options) {
244 		needed_buffer_size *= (1 + sequence->options->extra_samplings);
245 	}
246 
247 	if (sequence->buffer_size < needed_buffer_size) {
248 		LOG_ERR("Provided buffer is too small (%u/%u)",
249 			    sequence->buffer_size, needed_buffer_size);
250 		return -ENOMEM;
251 	}
252 
253 	return 0;
254 }
255 
start_read(const struct device * dev,const struct adc_sequence * sequence)256 static int start_read(const struct device *dev,
257 		      const struct adc_sequence *sequence)
258 {
259 	int error;
260 	uint32_t selected_channels = sequence->channels;
261 	uint8_t active_channels;
262 	uint8_t channel_id;
263 
264 	/* Signal an error if channel selection is invalid (no channels or
265 	 * a non-existing one is selected).
266 	 */
267 	if (!selected_channels ||
268 	    (selected_channels & ~BIT_MASK(SAADC_CH_NUM))) {
269 		LOG_ERR("Invalid selection of channels");
270 		return -EINVAL;
271 	}
272 
273 	active_channels = 0U;
274 
275 	/* Enable only the channels selected for the pointed sequence.
276 	 * Disable all the rest.
277 	 */
278 	channel_id = 0U;
279 	do {
280 		if (selected_channels & BIT(channel_id)) {
281 			/* Signal an error if a selected channel has not been
282 			 * configured yet.
283 			 */
284 			if (m_data.positive_inputs[channel_id] == 0U) {
285 				LOG_ERR("Channel %u not configured",
286 					    channel_id);
287 				return -EINVAL;
288 			}
289 			/* When oversampling is used, the burst mode needs to
290 			 * be activated. Unfortunately, this mode cannot be
291 			 * activated permanently in the channel setup, because
292 			 * then the multiple channel sampling fails (the END
293 			 * event is not generated) after switching to a single
294 			 * channel sampling and back. Thus, when oversampling
295 			 * is not used (hence, the multiple channel sampling is
296 			 * possible), the burst mode have to be deactivated.
297 			 */
298 			nrf_saadc_burst_set(NRF_SAADC, channel_id,
299 				(sequence->oversampling != 0U ?
300 					NRF_SAADC_BURST_ENABLED :
301 					NRF_SAADC_BURST_DISABLED));
302 			nrf_saadc_channel_pos_input_set(
303 				NRF_SAADC,
304 				channel_id,
305 				m_data.positive_inputs[channel_id]);
306 			++active_channels;
307 		} else {
308 			nrf_saadc_channel_pos_input_set(
309 				NRF_SAADC,
310 				channel_id,
311 				NRF_SAADC_INPUT_DISABLED);
312 		}
313 	} while (++channel_id < SAADC_CH_NUM);
314 
315 	error = set_resolution(sequence);
316 	if (error) {
317 		return error;
318 	}
319 
320 	error = set_oversampling(sequence, active_channels);
321 	if (error) {
322 		return error;
323 	}
324 
325 	error = check_buffer_size(sequence, active_channels);
326 	if (error) {
327 		return error;
328 	}
329 
330 	nrf_saadc_buffer_init(NRF_SAADC,
331 			      (nrf_saadc_value_t *)sequence->buffer,
332 			      active_channels);
333 
334 	adc_context_start_read(&m_data.ctx, sequence);
335 
336 	error = adc_context_wait_for_completion(&m_data.ctx);
337 	return error;
338 }
339 
340 /* Implementation of the ADC driver API function: adc_read. */
adc_nrfx_read(const struct device * dev,const struct adc_sequence * sequence)341 static int adc_nrfx_read(const struct device *dev,
342 			 const struct adc_sequence *sequence)
343 {
344 	int error;
345 
346 	adc_context_lock(&m_data.ctx, false, NULL);
347 	error = start_read(dev, sequence);
348 	adc_context_release(&m_data.ctx, error);
349 
350 	return error;
351 }
352 
353 #ifdef CONFIG_ADC_ASYNC
354 /* 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)355 static int adc_nrfx_read_async(const struct device *dev,
356 			       const struct adc_sequence *sequence,
357 			       struct k_poll_signal *async)
358 {
359 	int error;
360 
361 	adc_context_lock(&m_data.ctx, true, async);
362 	error = start_read(dev, sequence);
363 	adc_context_release(&m_data.ctx, error);
364 
365 	return error;
366 }
367 #endif /* CONFIG_ADC_ASYNC */
368 
saadc_irq_handler(const struct device * dev)369 static void saadc_irq_handler(const struct device *dev)
370 {
371 	if (nrf_saadc_event_check(NRF_SAADC, NRF_SAADC_EVENT_END)) {
372 		nrf_saadc_event_clear(NRF_SAADC, NRF_SAADC_EVENT_END);
373 
374 		nrf_saadc_task_trigger(NRF_SAADC, NRF_SAADC_TASK_STOP);
375 		nrf_saadc_disable(NRF_SAADC);
376 
377 		adc_context_on_sampling_done(&m_data.ctx, dev);
378 	} else if (nrf_saadc_event_check(NRF_SAADC,
379 					 NRF_SAADC_EVENT_CALIBRATEDONE)) {
380 		nrf_saadc_event_clear(NRF_SAADC, NRF_SAADC_EVENT_CALIBRATEDONE);
381 
382 		/*
383 		 * The workaround for Nordic nRF52832 anomalies 86 and
384 		 * 178 is an explicit STOP after CALIBRATEOFFSET
385 		 * before issuing START.
386 		 */
387 		nrf_saadc_task_trigger(NRF_SAADC, NRF_SAADC_TASK_STOP);
388 		nrf_saadc_task_trigger(NRF_SAADC, NRF_SAADC_TASK_START);
389 		nrf_saadc_task_trigger(NRF_SAADC, NRF_SAADC_TASK_SAMPLE);
390 	}
391 }
392 
init_saadc(const struct device * dev)393 static int init_saadc(const struct device *dev)
394 {
395 	nrf_saadc_event_clear(NRF_SAADC, NRF_SAADC_EVENT_END);
396 	nrf_saadc_event_clear(NRF_SAADC, NRF_SAADC_EVENT_CALIBRATEDONE);
397 	nrf_saadc_int_enable(NRF_SAADC,
398 			     NRF_SAADC_INT_END | NRF_SAADC_INT_CALIBRATEDONE);
399 	NRFX_IRQ_ENABLE(DT_INST_IRQN(0));
400 
401 	IRQ_CONNECT(DT_INST_IRQN(0), DT_INST_IRQ(0, priority),
402 		    saadc_irq_handler, DEVICE_DT_INST_GET(0), 0);
403 
404 	adc_context_unlock_unconditionally(&m_data.ctx);
405 
406 	return 0;
407 }
408 
409 static const struct adc_driver_api adc_nrfx_driver_api = {
410 	.channel_setup = adc_nrfx_channel_setup,
411 	.read          = adc_nrfx_read,
412 #ifdef CONFIG_ADC_ASYNC
413 	.read_async    = adc_nrfx_read_async,
414 #endif
415 	.ref_internal  = 600,
416 };
417 
418 /*
419  * There is only one instance on supported SoCs, so inst is guaranteed
420  * to be 0 if any instance is okay. (We use adc_0 above, so the driver
421  * is relying on the numeric instance value in a way that happens to
422  * be safe.)
423  *
424  * Just in case that assumption becomes invalid in the future, we use
425  * a BUILD_ASSERT().
426  */
427 #define SAADC_INIT(inst)						\
428 	BUILD_ASSERT((inst) == 0,					\
429 		     "multiple instances not supported");		\
430 	DEVICE_DT_INST_DEFINE(0,					\
431 			    init_saadc,					\
432 			    NULL,					\
433 			    NULL,					\
434 			    NULL,					\
435 			    POST_KERNEL,				\
436 			    CONFIG_KERNEL_INIT_PRIORITY_DEVICE,		\
437 			    &adc_nrfx_driver_api);
438 
439 DT_INST_FOREACH_STATUS_OKAY(SAADC_INIT)
440