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_sdadc
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_sdadc);
21 
22 struct sdadc_smartbond_cfg {
23 	const struct pinctrl_dev_config *pcfg;
24 	/** Value for SDADC_CLK_FREQ */
25 	uint8_t sdadc_clk_freq;
26 };
27 
28 struct sdadc_smartbond_data {
29 	struct adc_context ctx;
30 
31 	/* Buffer to store channel data */
32 	uint16_t *buffer;
33 	/* Copy of channel mask from sequence */
34 	uint32_t channel_read_mask;
35 	/* Number of bits in sequence channels */
36 	uint8_t sequence_channel_count;
37 	/* Index in buffer to store current value to */
38 	uint8_t result_index;
39 };
40 
41 #define SMARTBOND_SDADC_CHANNEL_COUNT	8
42 
43 struct sdadc_smartbond_channel_cfg {
44 	uint32_t sd_adc_ctrl_reg;
45 };
46 
47 static struct sdadc_smartbond_channel_cfg m_sdchannels[SMARTBOND_SDADC_CHANNEL_COUNT];
48 
49 /* Implementation of the ADC driver API function: adc_channel_setup. */
sdadc_smartbond_channel_setup(const struct device * dev,const struct adc_channel_cfg * channel_cfg)50 static int sdadc_smartbond_channel_setup(const struct device *dev,
51 					 const struct adc_channel_cfg *channel_cfg)
52 {
53 	uint8_t channel_id = channel_cfg->channel_id;
54 	struct sdadc_smartbond_channel_cfg *config = &m_sdchannels[channel_id];
55 
56 	if (channel_id >= SMARTBOND_SDADC_CHANNEL_COUNT) {
57 		return -EINVAL;
58 	}
59 
60 	if (channel_cfg->acquisition_time != ADC_ACQ_TIME_DEFAULT) {
61 		LOG_ERR("Selected ADC acquisition time is not valid");
62 		return -EINVAL;
63 	}
64 
65 	if (channel_cfg->input_positive > SMARTBOND_SDADC_VBAT) {
66 		LOG_ERR("Channels out of range");
67 		return -EINVAL;
68 	}
69 	if (channel_cfg->differential) {
70 		if (channel_cfg->input_negative >= SMARTBOND_SDADC_VBAT) {
71 			LOG_ERR("Differential negative channels out of range");
72 			return -EINVAL;
73 		}
74 	}
75 
76 	config->sd_adc_ctrl_reg = 0;
77 	if ((channel_cfg->input_positive == SMARTBOND_SDADC_VBAT &&
78 	     channel_cfg->gain != ADC_GAIN_1_4) ||
79 	    (channel_cfg->input_positive != SMARTBOND_SDADC_VBAT &&
80 	     channel_cfg->gain != ADC_GAIN_1)) {
81 		LOG_ERR("ADC gain should be 1/4 for VBAT and 1 for all other channels");
82 		return -EINVAL;
83 	}
84 
85 	switch (channel_cfg->reference) {
86 	case ADC_REF_INTERNAL:
87 		break;
88 	default:
89 		LOG_ERR("Selected ADC reference is not valid");
90 		return -EINVAL;
91 	}
92 
93 	config->sd_adc_ctrl_reg =
94 		channel_cfg->input_positive << SDADC_SDADC_CTRL_REG_SDADC_INP_SEL_Pos;
95 	if (channel_cfg->differential) {
96 		config->sd_adc_ctrl_reg |=
97 			channel_cfg->input_negative << SDADC_SDADC_CTRL_REG_SDADC_INN_SEL_Pos;
98 	} else {
99 		config->sd_adc_ctrl_reg |= SDADC_SDADC_CTRL_REG_SDADC_SE_Msk;
100 	}
101 
102 	return 0;
103 }
104 
105 #define PER_CHANNEL_ADC_CONFIG_MASK (SDADC_SDADC_CTRL_REG_SDADC_INP_SEL_Msk |	\
106 				     SDADC_SDADC_CTRL_REG_SDADC_INN_SEL_Msk |	\
107 				     SDADC_SDADC_CTRL_REG_SDADC_SE_Msk		\
108 )
109 
pop_count(uint32_t n)110 static int pop_count(uint32_t n)
111 {
112 	return __builtin_popcount(n);
113 }
114 
adc_context_start_sampling(struct adc_context * ctx)115 static void adc_context_start_sampling(struct adc_context *ctx)
116 {
117 	uint32_t val;
118 	struct sdadc_smartbond_data *data =
119 		CONTAINER_OF(ctx, struct sdadc_smartbond_data, ctx);
120 	/* Extract lower channel from sequence mask */
121 	int current_channel = u32_count_trailing_zeros(data->channel_read_mask);
122 
123 	if (ctx->sequence.calibrate) {
124 		/* TODO: Add calibration code */
125 	} else {
126 		val = SDADC->SDADC_CTRL_REG & ~PER_CHANNEL_ADC_CONFIG_MASK;
127 		val |= m_sdchannels[current_channel].sd_adc_ctrl_reg;
128 		val |= SDADC_SDADC_CTRL_REG_SDADC_START_Msk |
129 		       SDADC_SDADC_CTRL_REG_SDADC_MINT_Msk;
130 		val |= (ctx->sequence.oversampling - 7) << SDADC_SDADC_CTRL_REG_SDADC_OSR_Pos;
131 
132 		SDADC->SDADC_CTRL_REG = val;
133 	}
134 }
135 
adc_context_update_buffer_pointer(struct adc_context * ctx,bool repeat)136 static void adc_context_update_buffer_pointer(struct adc_context *ctx,
137 					      bool repeat)
138 {
139 	struct sdadc_smartbond_data *data =
140 		CONTAINER_OF(ctx, struct sdadc_smartbond_data, ctx);
141 
142 	if (!repeat) {
143 		data->buffer += data->sequence_channel_count;
144 	}
145 }
146 
check_buffer_size(const struct adc_sequence * sequence,uint8_t active_channels)147 static int check_buffer_size(const struct adc_sequence *sequence,
148 			     uint8_t active_channels)
149 {
150 	size_t needed_buffer_size;
151 
152 	needed_buffer_size = active_channels * sizeof(uint16_t);
153 	if (sequence->options) {
154 		needed_buffer_size *= (1 + sequence->options->extra_samplings);
155 	}
156 
157 	if (sequence->buffer_size < needed_buffer_size) {
158 		LOG_ERR("Provided buffer is too small (%u/%u)",
159 			sequence->buffer_size, needed_buffer_size);
160 		return -ENOMEM;
161 	}
162 
163 	return 0;
164 }
165 
start_read(const struct device * dev,const struct adc_sequence * sequence)166 static int start_read(const struct device *dev,
167 		      const struct adc_sequence *sequence)
168 {
169 	int error;
170 	struct sdadc_smartbond_data *data = dev->data;
171 
172 	if (sequence->oversampling < 7U || sequence->oversampling > 10) {
173 		LOG_ERR("Invalid oversampling");
174 		return -EINVAL;
175 	}
176 
177 	if ((sequence->channels == 0) ||
178 	    ((sequence->channels & ~BIT_MASK(SMARTBOND_SDADC_CHANNEL_COUNT)) != 0)) {
179 		LOG_ERR("Channel scanning is not supported");
180 		return -EINVAL;
181 	}
182 
183 	if (sequence->resolution < 8 || sequence->resolution > 15) {
184 		LOG_ERR("ADC resolution value %d is not valid",
185 			sequence->resolution);
186 		return -EINVAL;
187 	}
188 
189 	error = check_buffer_size(sequence, 1);
190 	if (error) {
191 		return error;
192 	}
193 
194 	data->buffer = sequence->buffer;
195 	data->channel_read_mask = sequence->channels;
196 	data->sequence_channel_count = pop_count(sequence->channels);
197 	data->result_index = 0;
198 
199 	adc_context_start_read(&data->ctx, sequence);
200 
201 	error = adc_context_wait_for_completion(&data->ctx);
202 	return error;
203 }
204 
sdadc_smartbond_isr(const struct device * dev)205 static void sdadc_smartbond_isr(const struct device *dev)
206 {
207 	struct sdadc_smartbond_data *data = dev->data;
208 	int current_channel = u32_count_trailing_zeros(data->channel_read_mask);
209 
210 	SDADC->SDADC_CLEAR_INT_REG = 0;
211 	/* Store current channel value, result is left justified, move bits right */
212 	data->buffer[data->result_index++] = ((uint16_t)SDADC->SDADC_RESULT_REG) >>
213 		(16 - data->ctx.sequence.resolution);
214 	/* Exclude channel from mask for further reading */
215 	data->channel_read_mask ^= 1 << current_channel;
216 
217 	if (data->channel_read_mask == 0) {
218 		adc_context_on_sampling_done(&data->ctx, dev);
219 	} else {
220 		adc_context_start_sampling(&data->ctx);
221 	}
222 
223 	LOG_DBG("%s ISR triggered.", dev->name);
224 }
225 
226 /* Implementation of the ADC driver API function: adc_read. */
sdadc_smartbond_read(const struct device * dev,const struct adc_sequence * sequence)227 static int sdadc_smartbond_read(const struct device *dev,
228 				const struct adc_sequence *sequence)
229 {
230 	int error;
231 	struct sdadc_smartbond_data *data = dev->data;
232 
233 	adc_context_lock(&data->ctx, false, NULL);
234 	error = start_read(dev, sequence);
235 	adc_context_release(&data->ctx, error);
236 
237 	return error;
238 }
239 
240 #if defined(CONFIG_ADC_ASYNC)
241 /* Implementation of the ADC driver API function: adc_read_sync. */
sdadc_smartbond_read_async(const struct device * dev,const struct adc_sequence * sequence,struct k_poll_signal * async)242 static int sdadc_smartbond_read_async(const struct device *dev,
243 				      const struct adc_sequence *sequence,
244 				      struct k_poll_signal *async)
245 {
246 	struct sdadc_smartbond_data *data = dev->data;
247 	int error;
248 
249 	adc_context_lock(&data->ctx, true, async);
250 	error = start_read(dev, sequence);
251 	adc_context_release(&data->ctx, error);
252 
253 	return error;
254 }
255 #endif /* CONFIG_ADC_ASYNC */
256 
sdadc_smartbond_init(const struct device * dev)257 static int sdadc_smartbond_init(const struct device *dev)
258 {
259 	int err;
260 	struct sdadc_smartbond_data *data = dev->data;
261 	const struct sdadc_smartbond_cfg *config = dev->config;
262 
263 	SDADC->SDADC_CTRL_REG = SDADC_SDADC_CTRL_REG_SDADC_EN_Msk;
264 	SDADC->SDADC_CLEAR_INT_REG = 0x0;
265 	SDADC->SDADC_TEST_REG =
266 		(SDADC->SDADC_TEST_REG & ~SDADC_SDADC_TEST_REG_SDADC_CLK_FREQ_Msk) |
267 		(config->sdadc_clk_freq) << SDADC_SDADC_TEST_REG_SDADC_CLK_FREQ_Pos;
268 	/*
269 	 * Configure dt provided device signals when available.
270 	 * pinctrl is optional so ENOENT is not setup failure.
271 	 */
272 	err = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);
273 	if (err < 0 && err != -ENOENT) {
274 		LOG_ERR("ADC pinctrl setup failed (%d)", err);
275 		return err;
276 	}
277 	IRQ_CONNECT(DT_INST_IRQN(0), DT_INST_IRQ(0, priority),
278 		    sdadc_smartbond_isr, DEVICE_DT_INST_GET(0), 0);
279 
280 	NVIC_ClearPendingIRQ(DT_INST_IRQN(0));
281 	NVIC_EnableIRQ(DT_INST_IRQN(0));
282 
283 	adc_context_unlock_unconditionally(&data->ctx);
284 
285 	return 0;
286 }
287 
288 static const struct adc_driver_api sdadc_smartbond_driver_api = {
289 	.channel_setup = sdadc_smartbond_channel_setup,
290 	.read          = sdadc_smartbond_read,
291 #ifdef CONFIG_ADC_ASYNC
292 	.read_async    = sdadc_smartbond_read_async,
293 #endif
294 	.ref_internal  = 1200,
295 };
296 
297 /*
298  * There is only one instance on supported SoCs, so inst is guaranteed
299  * to be 0 if any instance is okay. (We use adc_0 above, so the driver
300  * is relying on the numeric instance value in a way that happens to
301  * be safe.)
302  *
303  * Just in case that assumption becomes invalid in the future, we use
304  * a BUILD_ASSERT().
305  */
306 #define SDADC_INIT(inst)							\
307 	BUILD_ASSERT((inst) == 0,						\
308 		     "multiple instances not supported");			\
309 	PINCTRL_DT_INST_DEFINE(inst);						\
310 	static const struct sdadc_smartbond_cfg sdadc_smartbond_cfg_##inst = {	\
311 		.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(inst),			\
312 		.sdadc_clk_freq = DT_INST_PROP(inst, clock_freq),		\
313 	};									\
314 	static struct sdadc_smartbond_data sdadc_smartbond_data_##inst = {	\
315 		ADC_CONTEXT_INIT_TIMER(sdadc_smartbond_data_##inst, ctx),	\
316 		ADC_CONTEXT_INIT_LOCK(sdadc_smartbond_data_##inst, ctx),	\
317 		ADC_CONTEXT_INIT_SYNC(sdadc_smartbond_data_##inst, ctx),	\
318 	};									\
319 	DEVICE_DT_INST_DEFINE(0,						\
320 			      sdadc_smartbond_init, NULL,			\
321 			      &sdadc_smartbond_data_##inst,			\
322 			      &sdadc_smartbond_cfg_##inst,			\
323 			      POST_KERNEL,					\
324 			      CONFIG_ADC_INIT_PRIORITY,				\
325 			      &sdadc_smartbond_driver_api);
326 
327 DT_INST_FOREACH_STATUS_OKAY(SDADC_INIT)
328