xref: /linux/drivers/iio/adc/nxp-sar-adc.c (revision fab183d632628381b466a41479489541ac0e29a0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * NXP SAR-ADC driver (adapted from Freescale Vybrid vf610 ADC driver
4  * by Fugang Duan <B38611@freescale.com>)
5  *
6  * Copyright 2013 Freescale Semiconductor, Inc.
7  * Copyright 2017, 2020-2025 NXP
8  * Copyright 2025, Linaro Ltd
9  */
10 #include <linux/bitfield.h>
11 #include <linux/bitops.h>
12 #include <linux/circ_buf.h>
13 #include <linux/cleanup.h>
14 #include <linux/clk.h>
15 #include <linux/completion.h>
16 #include <linux/delay.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/dmaengine.h>
19 #include <linux/err.h>
20 #include <linux/interrupt.h>
21 #include <linux/iopoll.h>
22 #include <linux/math64.h>
23 #include <linux/minmax.h>
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm.h>
27 #include <linux/property.h>
28 #include <linux/slab.h>
29 #include <linux/spinlock.h>
30 #include <linux/time.h>
31 #include <linux/types.h>
32 #include <linux/units.h>
33 
34 #include <linux/iio/iio.h>
35 #include <linux/iio/triggered_buffer.h>
36 #include <linux/iio/trigger_consumer.h>
37 
38 /* SAR ADC registers. */
39 #define NXP_SAR_ADC_CDR(__base, __channel)	(((__base) + 0x100) + ((__channel) * 0x4))
40 
41 #define NXP_SAR_ADC_CDR_CDATA_MASK	GENMASK(11, 0)
42 #define NXP_SAR_ADC_CDR_VALID		BIT(19)
43 
44 /* Main Configuration Register */
45 #define NXP_SAR_ADC_MCR(__base)		((__base) + 0x00)
46 
47 #define NXP_SAR_ADC_MCR_PWDN		BIT(0)
48 #define NXP_SAR_ADC_MCR_ACKO		BIT(5)
49 #define NXP_SAR_ADC_MCR_ADCLKSEL	BIT(8)
50 #define NXP_SAR_ADC_MCR_TSAMP_MASK	GENMASK(10, 9)
51 #define NXP_SAR_ADC_MCR_NRSMPL_MASK	GENMASK(12, 11)
52 #define NXP_SAR_ADC_MCR_AVGEN		BIT(13)
53 #define NXP_SAR_ADC_MCR_CALSTART	BIT(14)
54 #define NXP_SAR_ADC_MCR_NSTART		BIT(24)
55 #define NXP_SAR_ADC_MCR_MODE		BIT(29)
56 #define NXP_SAR_ADC_MCR_OWREN		BIT(31)
57 
58 /* Main Status Register */
59 #define NXP_SAR_ADC_MSR(__base)		((__base) + 0x04)
60 
61 #define NXP_SAR_ADC_MSR_CALBUSY		BIT(29)
62 #define NXP_SAR_ADC_MSR_CALFAIL		BIT(30)
63 
64 /* Interrupt Status Register */
65 #define NXP_SAR_ADC_ISR(__base)		((__base) + 0x10)
66 
67 #define NXP_SAR_ADC_ISR_ECH		BIT(0)
68 
69 /*  Channel Pending Register */
70 #define NXP_SAR_ADC_CEOCFR0(__base)	((__base) + 0x14)
71 #define NXP_SAR_ADC_CEOCFR1(__base)	((__base) + 0x18)
72 
73 #define NXP_SAR_ADC_EOC_CH(c)		BIT(c)
74 
75 /* Interrupt Mask Register */
76 #define NXP_SAR_ADC_IMR(__base)		((__base) + 0x20)
77 
78 /* Channel Interrupt Mask Register */
79 #define NXP_SAR_ADC_CIMR0(__base)	((__base) + 0x24)
80 #define NXP_SAR_ADC_CIMR1(__base)	((__base) + 0x28)
81 
82 /* DMA Setting Register */
83 #define NXP_SAR_ADC_DMAE(__base)	((__base) + 0x40)
84 
85 #define NXP_SAR_ADC_DMAE_DMAEN		BIT(0)
86 #define NXP_SAR_ADC_DMAE_DCLR		BIT(1)
87 
88 /* DMA Control register */
89 #define NXP_SAR_ADC_DMAR0(__base)	((__base) + 0x44)
90 #define NXP_SAR_ADC_DMAR1(__base)	((__base) + 0x48)
91 
92 /* Conversion Timing Register */
93 #define NXP_SAR_ADC_CTR0(__base)	((__base) + 0x94)
94 #define NXP_SAR_ADC_CTR1(__base)	((__base) + 0x98)
95 
96 #define NXP_SAR_ADC_CTR_INPSAMP_MIN	0x08
97 #define NXP_SAR_ADC_CTR_INPSAMP_MAX	0xff
98 
99 /* Normal Conversion Mask Register */
100 #define NXP_SAR_ADC_NCMR0(__base)	((__base) + 0xa4)
101 #define NXP_SAR_ADC_NCMR1(__base)	((__base) + 0xa8)
102 
103 /* Normal Conversion Mask Register field define */
104 #define NXP_SAR_ADC_CH_MASK		GENMASK(7, 0)
105 
106 /* Other field define */
107 #define NXP_SAR_ADC_CONV_TIMEOUT	(msecs_to_jiffies(100))
108 #define NXP_SAR_ADC_CAL_TIMEOUT_US	(100 * USEC_PER_MSEC)
109 #define NXP_SAR_ADC_WAIT_US		(2 * USEC_PER_MSEC)
110 #define NXP_SAR_ADC_RESOLUTION		12
111 
112 /* Duration of conversion phases */
113 #define NXP_SAR_ADC_TPT			2
114 #define NXP_SAR_ADC_DP			2
115 #define NXP_SAR_ADC_CT			((NXP_SAR_ADC_RESOLUTION + 2) * 4)
116 #define NXP_SAR_ADC_CONV_TIME		(NXP_SAR_ADC_TPT + NXP_SAR_ADC_CT + NXP_SAR_ADC_DP)
117 
118 #define NXP_SAR_ADC_NR_CHANNELS		8
119 
120 #define NXP_PAGE_SIZE			SZ_4K
121 #define NXP_SAR_ADC_DMA_SAMPLE_SZ	DMA_SLAVE_BUSWIDTH_4_BYTES
122 #define NXP_SAR_ADC_DMA_BUFF_SZ		(NXP_PAGE_SIZE * NXP_SAR_ADC_DMA_SAMPLE_SZ)
123 #define NXP_SAR_ADC_DMA_SAMPLE_CNT	(NXP_SAR_ADC_DMA_BUFF_SZ / NXP_SAR_ADC_DMA_SAMPLE_SZ)
124 
125 struct nxp_sar_adc {
126 	void __iomem *regs;
127 	phys_addr_t regs_phys;
128 	u8 current_channel;
129 	u8 channels_used;
130 	u16 value;
131 	u32 vref_mV;
132 
133 	/* Save and restore context. */
134 	u32 inpsamp;
135 	u32 pwdn;
136 
137 	struct clk *clk;
138 	struct dma_chan	*dma_chan;
139 	struct completion completion;
140 	struct circ_buf dma_buf;
141 
142 	dma_addr_t rx_dma_buf;
143 	dma_cookie_t cookie;
144 
145 	/* Protect circular buffers access. */
146 	spinlock_t lock;
147 
148 	/* Array of enabled channels. */
149 	u16 buffered_chan[NXP_SAR_ADC_NR_CHANNELS];
150 
151 	/* Buffer to be filled by the DMA. */
152 	IIO_DECLARE_BUFFER_WITH_TS(u16, buffer, NXP_SAR_ADC_NR_CHANNELS);
153 };
154 
155 struct nxp_sar_adc_data {
156 	u32 vref_mV;
157 	const char *model;
158 };
159 
160 #define ADC_CHAN(_idx, _chan_type) {				\
161 	.type = (_chan_type),					\
162 	.indexed = 1,						\
163 	.channel = (_idx),					\
164 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
165 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |	\
166 				BIT(IIO_CHAN_INFO_SAMP_FREQ),	\
167 	.scan_index = (_idx),					\
168 	.scan_type = {						\
169 		.sign = 'u',					\
170 		.realbits = 12,					\
171 		.storagebits = 16,				\
172 	},							\
173 }
174 
175 static const struct iio_chan_spec nxp_sar_adc_iio_channels[] = {
176 	ADC_CHAN(0, IIO_VOLTAGE),
177 	ADC_CHAN(1, IIO_VOLTAGE),
178 	ADC_CHAN(2, IIO_VOLTAGE),
179 	ADC_CHAN(3, IIO_VOLTAGE),
180 	ADC_CHAN(4, IIO_VOLTAGE),
181 	ADC_CHAN(5, IIO_VOLTAGE),
182 	ADC_CHAN(6, IIO_VOLTAGE),
183 	ADC_CHAN(7, IIO_VOLTAGE),
184 	/*
185 	 * The NXP SAR ADC documentation marks the channels 8 to 31 as
186 	 * "Reserved". Reflect the same in the driver in case new ADC
187 	 * variants comes with more channels.
188 	 */
189 	IIO_CHAN_SOFT_TIMESTAMP(32),
190 };
191 
nxp_sar_adc_irq_cfg(struct nxp_sar_adc * info,bool enable)192 static void nxp_sar_adc_irq_cfg(struct nxp_sar_adc *info, bool enable)
193 {
194 	if (enable)
195 		writel(NXP_SAR_ADC_ISR_ECH, NXP_SAR_ADC_IMR(info->regs));
196 	else
197 		writel(0, NXP_SAR_ADC_IMR(info->regs));
198 }
199 
nxp_sar_adc_wait_for(struct nxp_sar_adc * info,u64 cycles)200 static void nxp_sar_adc_wait_for(struct nxp_sar_adc *info, u64 cycles)
201 {
202 	u64 rate;
203 
204 	rate = clk_get_rate(info->clk);
205 	if (rate)
206 		ndelay(div64_u64(NSEC_PER_SEC * cycles, rate));
207 }
208 
nxp_sar_adc_set_enabled(struct nxp_sar_adc * info,bool enable)209 static bool nxp_sar_adc_set_enabled(struct nxp_sar_adc *info, bool enable)
210 {
211 	u32 mcr;
212 	bool pwdn;
213 
214 	mcr = readl(NXP_SAR_ADC_MCR(info->regs));
215 
216 	/*
217 	 * Get the current state and return it later. This is used for
218 	 * suspend/resume to get the power state
219 	 */
220 	pwdn = FIELD_GET(NXP_SAR_ADC_MCR_PWDN, mcr);
221 
222 	/* When the enabled flag is not set, we set the power down bit */
223 	FIELD_MODIFY(NXP_SAR_ADC_MCR_PWDN, &mcr, !enable);
224 
225 	writel(mcr, NXP_SAR_ADC_MCR(info->regs));
226 
227 	/*
228 	 * Ensure there are at least three cycles between the
229 	 * configuration of NCMR and the setting of NSTART.
230 	 */
231 	if (enable)
232 		nxp_sar_adc_wait_for(info, 3);
233 
234 	return pwdn;
235 }
236 
nxp_sar_adc_enable(struct nxp_sar_adc * info)237 static inline bool nxp_sar_adc_enable(struct nxp_sar_adc *info)
238 {
239 	return nxp_sar_adc_set_enabled(info, true);
240 }
241 
nxp_sar_adc_disable(struct nxp_sar_adc * info)242 static inline bool nxp_sar_adc_disable(struct nxp_sar_adc *info)
243 {
244 	return nxp_sar_adc_set_enabled(info, false);
245 }
246 
nxp_sar_adc_calibration_start(void __iomem * base)247 static inline void nxp_sar_adc_calibration_start(void __iomem *base)
248 {
249 	u32 mcr = readl(NXP_SAR_ADC_MCR(base));
250 
251 	FIELD_MODIFY(NXP_SAR_ADC_MCR_CALSTART, &mcr, 0x1);
252 
253 	writel(mcr, NXP_SAR_ADC_MCR(base));
254 }
255 
nxp_sar_adc_calibration_wait(void __iomem * base)256 static inline int nxp_sar_adc_calibration_wait(void __iomem *base)
257 {
258 	u32 msr;
259 	int ret;
260 
261 	ret = readl_poll_timeout(NXP_SAR_ADC_MSR(base), msr,
262 				 !FIELD_GET(NXP_SAR_ADC_MSR_CALBUSY, msr),
263 				 NXP_SAR_ADC_WAIT_US,
264 				 NXP_SAR_ADC_CAL_TIMEOUT_US);
265 	if (ret)
266 		return ret;
267 
268 	if (FIELD_GET(NXP_SAR_ADC_MSR_CALFAIL, msr)) {
269 		/*
270 		 * If the calibration fails, the status register bit must be
271 		 * cleared.
272 		 */
273 		FIELD_MODIFY(NXP_SAR_ADC_MSR_CALFAIL, &msr, 0x0);
274 		writel(msr, NXP_SAR_ADC_MSR(base));
275 
276 		return -EAGAIN;
277 	}
278 
279 	return 0;
280 }
281 
nxp_sar_adc_calibration(struct nxp_sar_adc * info)282 static int nxp_sar_adc_calibration(struct nxp_sar_adc *info)
283 {
284 	int ret;
285 
286 	/* Calibration works only if the ADC is powered up. */
287 	nxp_sar_adc_enable(info);
288 
289 	/* The calibration operation starts. */
290 	nxp_sar_adc_calibration_start(info->regs);
291 
292 	ret = nxp_sar_adc_calibration_wait(info->regs);
293 
294 	/*
295 	 * Calibration works only if the ADC is powered up. However
296 	 * the calibration is called from the probe function where the
297 	 * iio is not enabled, so we disable after the calibration.
298 	 */
299 	nxp_sar_adc_disable(info);
300 
301 	return ret;
302 }
303 
nxp_sar_adc_conversion_timing_set(struct nxp_sar_adc * info,u32 inpsamp)304 static void nxp_sar_adc_conversion_timing_set(struct nxp_sar_adc *info, u32 inpsamp)
305 {
306 	inpsamp = clamp(inpsamp, NXP_SAR_ADC_CTR_INPSAMP_MIN, NXP_SAR_ADC_CTR_INPSAMP_MAX);
307 
308 	writel(inpsamp, NXP_SAR_ADC_CTR0(info->regs));
309 }
310 
nxp_sar_adc_conversion_timing_get(struct nxp_sar_adc * info)311 static u32 nxp_sar_adc_conversion_timing_get(struct nxp_sar_adc *info)
312 {
313 	return readl(NXP_SAR_ADC_CTR0(info->regs));
314 }
315 
nxp_sar_adc_read_notify(struct nxp_sar_adc * info)316 static void nxp_sar_adc_read_notify(struct nxp_sar_adc *info)
317 {
318 	writel(NXP_SAR_ADC_CH_MASK, NXP_SAR_ADC_CEOCFR0(info->regs));
319 	writel(NXP_SAR_ADC_CH_MASK, NXP_SAR_ADC_CEOCFR1(info->regs));
320 }
321 
nxp_sar_adc_read_data(struct nxp_sar_adc * info,unsigned int chan)322 static int nxp_sar_adc_read_data(struct nxp_sar_adc *info, unsigned int chan)
323 {
324 	u32 ceocfr, cdr;
325 
326 	ceocfr = readl(NXP_SAR_ADC_CEOCFR0(info->regs));
327 
328 	if (!field_get(NXP_SAR_ADC_EOC_CH(chan), ceocfr))
329 		return -EIO;
330 
331 	cdr = readl(NXP_SAR_ADC_CDR(info->regs, chan));
332 	if (!(FIELD_GET(NXP_SAR_ADC_CDR_VALID, cdr)))
333 		return -EIO;
334 
335 	return FIELD_GET(NXP_SAR_ADC_CDR_CDATA_MASK, cdr);
336 }
337 
nxp_sar_adc_isr_buffer(struct iio_dev * indio_dev)338 static void nxp_sar_adc_isr_buffer(struct iio_dev *indio_dev)
339 {
340 	struct nxp_sar_adc *info = iio_priv(indio_dev);
341 	unsigned int i;
342 	int ret;
343 
344 	for (i = 0; i < info->channels_used; i++) {
345 		ret = nxp_sar_adc_read_data(info, info->buffered_chan[i]);
346 		if (ret < 0) {
347 			nxp_sar_adc_read_notify(info);
348 			iio_trigger_notify_done(indio_dev->trig);
349 			return;
350 		}
351 
352 		info->buffer[i] = ret;
353 	}
354 
355 	nxp_sar_adc_read_notify(info);
356 
357 	iio_push_to_buffers_with_ts(indio_dev, info->buffer, sizeof(info->buffer),
358 				    iio_get_time_ns(indio_dev));
359 
360 	iio_trigger_notify_done(indio_dev->trig);
361 }
362 
nxp_sar_adc_isr_read_raw(struct iio_dev * indio_dev)363 static void nxp_sar_adc_isr_read_raw(struct iio_dev *indio_dev)
364 {
365 	struct nxp_sar_adc *info = iio_priv(indio_dev);
366 	int ret;
367 
368 	ret = nxp_sar_adc_read_data(info, info->current_channel);
369 	nxp_sar_adc_read_notify(info);
370 	if (ret < 0)
371 		return;
372 
373 	info->value = ret;
374 	complete(&info->completion);
375 }
376 
nxp_sar_adc_isr(int irq,void * dev_id)377 static irqreturn_t nxp_sar_adc_isr(int irq, void *dev_id)
378 {
379 	struct iio_dev *indio_dev = dev_id;
380 	struct nxp_sar_adc *info = iio_priv(indio_dev);
381 	int isr;
382 
383 	isr = readl(NXP_SAR_ADC_ISR(info->regs));
384 	if (!(FIELD_GET(NXP_SAR_ADC_ISR_ECH, isr)))
385 		return IRQ_NONE;
386 
387 	if (iio_buffer_enabled(indio_dev))
388 		nxp_sar_adc_isr_buffer(indio_dev);
389 	else
390 		nxp_sar_adc_isr_read_raw(indio_dev);
391 
392 	writel(NXP_SAR_ADC_ISR_ECH, NXP_SAR_ADC_ISR(info->regs));
393 
394 	return IRQ_HANDLED;
395 }
396 
nxp_sar_adc_channels_disable(struct nxp_sar_adc * info,u32 mask)397 static void nxp_sar_adc_channels_disable(struct nxp_sar_adc *info, u32 mask)
398 {
399 	u32 ncmr, cimr;
400 
401 	ncmr = readl(NXP_SAR_ADC_NCMR0(info->regs));
402 	cimr = readl(NXP_SAR_ADC_CIMR0(info->regs));
403 
404 	/* FIELD_MODIFY() can not be used because the mask is not constant */
405 	ncmr &= ~mask;
406 	cimr &= ~mask;
407 
408 	writel(ncmr, NXP_SAR_ADC_NCMR0(info->regs));
409 	writel(cimr, NXP_SAR_ADC_CIMR0(info->regs));
410 }
411 
nxp_sar_adc_channels_enable(struct nxp_sar_adc * info,u32 mask)412 static void nxp_sar_adc_channels_enable(struct nxp_sar_adc *info, u32 mask)
413 {
414 	u32 ncmr, cimr;
415 
416 	ncmr = readl(NXP_SAR_ADC_NCMR0(info->regs));
417 	cimr = readl(NXP_SAR_ADC_CIMR0(info->regs));
418 
419 	ncmr |= mask;
420 	cimr |= mask;
421 
422 	writel(ncmr, NXP_SAR_ADC_NCMR0(info->regs));
423 	writel(cimr, NXP_SAR_ADC_CIMR0(info->regs));
424 }
425 
nxp_sar_adc_dma_channels_enable(struct nxp_sar_adc * info,u32 mask)426 static void nxp_sar_adc_dma_channels_enable(struct nxp_sar_adc *info, u32 mask)
427 {
428 	u32 dmar;
429 
430 	dmar = readl(NXP_SAR_ADC_DMAR0(info->regs));
431 
432 	dmar |= mask;
433 
434 	writel(dmar, NXP_SAR_ADC_DMAR0(info->regs));
435 }
436 
nxp_sar_adc_dma_channels_disable(struct nxp_sar_adc * info,u32 mask)437 static void nxp_sar_adc_dma_channels_disable(struct nxp_sar_adc *info, u32 mask)
438 {
439 	u32 dmar;
440 
441 	dmar = readl(NXP_SAR_ADC_DMAR0(info->regs));
442 
443 	dmar &= ~mask;
444 
445 	writel(dmar, NXP_SAR_ADC_DMAR0(info->regs));
446 }
447 
nxp_sar_adc_dma_cfg(struct nxp_sar_adc * info,bool enable)448 static void nxp_sar_adc_dma_cfg(struct nxp_sar_adc *info, bool enable)
449 {
450 	u32 dmae;
451 
452 	dmae = readl(NXP_SAR_ADC_DMAE(info->regs));
453 
454 	FIELD_MODIFY(NXP_SAR_ADC_DMAE_DMAEN, &dmae, enable);
455 
456 	writel(dmae, NXP_SAR_ADC_DMAE(info->regs));
457 }
458 
nxp_sar_adc_stop_conversion(struct nxp_sar_adc * info)459 static void nxp_sar_adc_stop_conversion(struct nxp_sar_adc *info)
460 {
461 	u32 mcr;
462 
463 	mcr = readl(NXP_SAR_ADC_MCR(info->regs));
464 
465 	FIELD_MODIFY(NXP_SAR_ADC_MCR_NSTART, &mcr, 0x0);
466 
467 	writel(mcr, NXP_SAR_ADC_MCR(info->regs));
468 
469 	/*
470 	 * On disable, we have to wait for the transaction to finish.
471 	 * ADC does not abort the transaction if a chain conversion is
472 	 * in progress. Wait for the worst case scenario - 80 ADC clk
473 	 * cycles. The clock rate is 80MHz, this routine is called
474 	 * only when the capture finishes. The delay will be very
475 	 * short, usec-ish, which is acceptable in the atomic context.
476 	 */
477 	nxp_sar_adc_wait_for(info, 80);
478 }
479 
nxp_sar_adc_start_conversion(struct nxp_sar_adc * info,bool raw)480 static int nxp_sar_adc_start_conversion(struct nxp_sar_adc *info, bool raw)
481 {
482 	u32 mcr;
483 
484 	mcr = readl(NXP_SAR_ADC_MCR(info->regs));
485 
486 	FIELD_MODIFY(NXP_SAR_ADC_MCR_NSTART, &mcr, 0x1);
487 	FIELD_MODIFY(NXP_SAR_ADC_MCR_MODE, &mcr, raw ? 0 : 1);
488 
489 	writel(mcr, NXP_SAR_ADC_MCR(info->regs));
490 
491 	return 0;
492 }
493 
nxp_sar_adc_read_channel(struct nxp_sar_adc * info,int channel)494 static int nxp_sar_adc_read_channel(struct nxp_sar_adc *info, int channel)
495 {
496 	int ret;
497 
498 	info->current_channel = channel;
499 	nxp_sar_adc_channels_enable(info, BIT(channel));
500 	nxp_sar_adc_irq_cfg(info, true);
501 	nxp_sar_adc_enable(info);
502 
503 	reinit_completion(&info->completion);
504 	ret = nxp_sar_adc_start_conversion(info, true);
505 	if (ret < 0)
506 		goto out_disable;
507 
508 	if (!wait_for_completion_interruptible_timeout(&info->completion,
509 						       NXP_SAR_ADC_CONV_TIMEOUT))
510 		ret = -ETIMEDOUT;
511 
512 	nxp_sar_adc_stop_conversion(info);
513 
514 out_disable:
515 	nxp_sar_adc_channels_disable(info, BIT(channel));
516 	nxp_sar_adc_irq_cfg(info, false);
517 	nxp_sar_adc_disable(info);
518 
519 	return ret;
520 }
521 
nxp_sar_adc_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)522 static int nxp_sar_adc_read_raw(struct iio_dev *indio_dev,
523 				struct iio_chan_spec const *chan, int *val,
524 				int *val2, long mask)
525 {
526 	struct nxp_sar_adc *info = iio_priv(indio_dev);
527 	u32 inpsamp;
528 	int ret;
529 
530 	switch (mask) {
531 	case IIO_CHAN_INFO_RAW:
532 		if (!iio_device_claim_direct(indio_dev))
533 			return -EBUSY;
534 
535 		ret = nxp_sar_adc_read_channel(info, chan->channel);
536 
537 		iio_device_release_direct(indio_dev);
538 
539 		if (ret)
540 			return ret;
541 
542 		*val = info->value;
543 		return IIO_VAL_INT;
544 
545 	case IIO_CHAN_INFO_SCALE:
546 		*val = info->vref_mV;
547 		*val2 = NXP_SAR_ADC_RESOLUTION;
548 		return IIO_VAL_FRACTIONAL_LOG2;
549 
550 	case IIO_CHAN_INFO_SAMP_FREQ:
551 		inpsamp = nxp_sar_adc_conversion_timing_get(info);
552 		*val = clk_get_rate(info->clk) / (inpsamp + NXP_SAR_ADC_CONV_TIME);
553 		return IIO_VAL_INT;
554 
555 	default:
556 		return -EINVAL;
557 	}
558 }
559 
nxp_sar_adc_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)560 static int nxp_sar_adc_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
561 				 int val, int val2, long mask)
562 {
563 	struct nxp_sar_adc *info = iio_priv(indio_dev);
564 	u32 inpsamp;
565 
566 	switch (mask) {
567 	case IIO_CHAN_INFO_SAMP_FREQ:
568 		if (val <= 0)
569 			return -EINVAL;
570 
571 		/*
572 		 * Configures the sample period duration in terms of the SAR
573 		 * controller clock. The minimum acceptable value is 8.
574 		 * Configuring it to a value lower than 8 sets the sample period
575 		 * to 8 cycles.  We read the clock value and divide by the
576 		 * sampling timing which gives us the number of cycles expected.
577 		 * The value is 8-bit wide, consequently the max value is 0xFF.
578 		 */
579 		inpsamp = clk_get_rate(info->clk) / val;
580 		if (inpsamp < NXP_SAR_ADC_CONV_TIME)
581 			return -EINVAL;
582 
583 		inpsamp -= NXP_SAR_ADC_CONV_TIME;
584 		nxp_sar_adc_conversion_timing_set(info, inpsamp);
585 		return 0;
586 
587 	default:
588 		return -EINVAL;
589 	}
590 }
591 
nxp_sar_adc_dma_cb(void * data)592 static void nxp_sar_adc_dma_cb(void *data)
593 {
594 	struct iio_dev *indio_dev = data;
595 	struct nxp_sar_adc *info = iio_priv(indio_dev);
596 	struct dma_tx_state state;
597 	struct circ_buf *dma_buf;
598 	struct device *dev_dma;
599 	u32 *dma_samples;
600 	s64 timestamp;
601 	int idx, ret;
602 
603 	guard(spinlock_irqsave)(&info->lock);
604 
605 	dma_buf = &info->dma_buf;
606 	dma_samples = (u32 *)dma_buf->buf;
607 	dev_dma = info->dma_chan->device->dev;
608 
609 	/*
610 	 * DMA in some corner cases might have already be charged for
611 	 * the next transfer. Potentially there can be a race where
612 	 * the residue changes while the dma engine updates the
613 	 * buffer. That could be handled by using the
614 	 * callback_result() instead of callback() because the residue
615 	 * will be passed as a parameter to the function. However this
616 	 * new callback is pretty new and the backend does not update
617 	 * the residue. So let's stick to the version other drivers do
618 	 * which has proven running well in production since several
619 	 * years.
620 	 */
621 	dmaengine_tx_status(info->dma_chan, info->cookie, &state);
622 
623 	dma_sync_single_for_cpu(dev_dma, info->rx_dma_buf,
624 				NXP_SAR_ADC_DMA_BUFF_SZ, DMA_FROM_DEVICE);
625 
626 	/* Current head position. */
627 	dma_buf->head = (NXP_SAR_ADC_DMA_BUFF_SZ - state.residue) /
628 			NXP_SAR_ADC_DMA_SAMPLE_SZ;
629 
630 	/* If everything was transferred, avoid an off by one error. */
631 	if (!state.residue)
632 		dma_buf->head--;
633 
634 	/* Something went wrong and nothing transferred. */
635 	if (state.residue != NXP_SAR_ADC_DMA_BUFF_SZ) {
636 		/* Make sure that head is multiple of info->channels_used. */
637 		dma_buf->head -= dma_buf->head % info->channels_used;
638 
639 		/*
640 		 * dma_buf->tail != dma_buf->head condition will become false
641 		 * because dma_buf->tail will be incremented with 1.
642 		 */
643 		while (dma_buf->tail != dma_buf->head) {
644 			idx = dma_buf->tail % info->channels_used;
645 			info->buffer[idx] = dma_samples[dma_buf->tail];
646 			dma_buf->tail = (dma_buf->tail + 1) % NXP_SAR_ADC_DMA_SAMPLE_CNT;
647 			if (idx != info->channels_used - 1)
648 				continue;
649 
650 			/*
651 			 * iio_push_to_buffers_with_ts() should not be
652 			 * called with dma_samples as parameter. The samples
653 			 * will be smashed if timestamp is enabled.
654 			 */
655 			timestamp = iio_get_time_ns(indio_dev);
656 			ret = iio_push_to_buffers_with_ts(indio_dev, info->buffer,
657 							  sizeof(info->buffer),
658 							  timestamp);
659 			if (ret < 0 && ret != -EBUSY)
660 				dev_err_ratelimited(&indio_dev->dev,
661 						    "failed to push iio buffer: %d",
662 						    ret);
663 		}
664 
665 		dma_buf->tail = dma_buf->head;
666 	}
667 
668 	dma_sync_single_for_device(dev_dma, info->rx_dma_buf,
669 				   NXP_SAR_ADC_DMA_BUFF_SZ, DMA_FROM_DEVICE);
670 }
671 
nxp_sar_adc_start_cyclic_dma(struct iio_dev * indio_dev)672 static int nxp_sar_adc_start_cyclic_dma(struct iio_dev *indio_dev)
673 {
674 	struct nxp_sar_adc *info = iio_priv(indio_dev);
675 	struct dma_slave_config config = { };
676 	struct dma_async_tx_descriptor *desc;
677 	int ret;
678 
679 	info->dma_buf.head = 0;
680 	info->dma_buf.tail = 0;
681 
682 	config.direction = DMA_DEV_TO_MEM;
683 	config.src_addr_width = NXP_SAR_ADC_DMA_SAMPLE_SZ;
684 	config.src_addr = NXP_SAR_ADC_CDR(info->regs_phys, info->buffered_chan[0]);
685 	config.src_port_window_size = info->channels_used;
686 	config.src_maxburst = info->channels_used;
687 	ret = dmaengine_slave_config(info->dma_chan, &config);
688 	if (ret < 0)
689 		return ret;
690 
691 	desc = dmaengine_prep_dma_cyclic(info->dma_chan,
692 					 info->rx_dma_buf,
693 					 NXP_SAR_ADC_DMA_BUFF_SZ,
694 					 NXP_SAR_ADC_DMA_BUFF_SZ / 2,
695 					 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
696 	if (!desc)
697 		return -EINVAL;
698 
699 	desc->callback = nxp_sar_adc_dma_cb;
700 	desc->callback_param = indio_dev;
701 	info->cookie = dmaengine_submit(desc);
702 	ret = dma_submit_error(info->cookie);
703 	if (ret) {
704 		dmaengine_terminate_async(info->dma_chan);
705 		return ret;
706 	}
707 
708 	dma_async_issue_pending(info->dma_chan);
709 
710 	return 0;
711 }
712 
nxp_sar_adc_buffer_software_do_predisable(struct iio_dev * indio_dev)713 static void nxp_sar_adc_buffer_software_do_predisable(struct iio_dev *indio_dev)
714 {
715 	struct nxp_sar_adc *info = iio_priv(indio_dev);
716 
717 	/*
718 	 * The ADC DMAEN bit should be cleared before DMA transaction
719 	 * is canceled.
720 	 */
721 	nxp_sar_adc_stop_conversion(info);
722 	dmaengine_terminate_sync(info->dma_chan);
723 	nxp_sar_adc_dma_cfg(info, false);
724 	nxp_sar_adc_dma_channels_disable(info, *indio_dev->active_scan_mask);
725 
726 	dma_release_channel(info->dma_chan);
727 }
728 
nxp_sar_adc_buffer_software_do_postenable(struct iio_dev * indio_dev)729 static int nxp_sar_adc_buffer_software_do_postenable(struct iio_dev *indio_dev)
730 {
731 	struct nxp_sar_adc *info = iio_priv(indio_dev);
732 	int ret;
733 
734 	info->dma_chan = dma_request_chan(indio_dev->dev.parent, "rx");
735 	if (IS_ERR(info->dma_chan))
736 		return PTR_ERR(info->dma_chan);
737 
738 	nxp_sar_adc_dma_channels_enable(info, *indio_dev->active_scan_mask);
739 
740 	nxp_sar_adc_dma_cfg(info, true);
741 
742 	ret = nxp_sar_adc_start_cyclic_dma(indio_dev);
743 	if (ret)
744 		goto out_dma_channels_disable;
745 
746 	ret = nxp_sar_adc_start_conversion(info, false);
747 	if (ret)
748 		goto out_stop_cyclic_dma;
749 
750 	return 0;
751 
752 out_stop_cyclic_dma:
753 	dmaengine_terminate_sync(info->dma_chan);
754 
755 out_dma_channels_disable:
756 	nxp_sar_adc_dma_cfg(info, false);
757 	nxp_sar_adc_dma_channels_disable(info, *indio_dev->active_scan_mask);
758 	dma_release_channel(info->dma_chan);
759 
760 	return ret;
761 }
762 
nxp_sar_adc_buffer_trigger_do_predisable(struct iio_dev * indio_dev)763 static void nxp_sar_adc_buffer_trigger_do_predisable(struct iio_dev *indio_dev)
764 {
765 	struct nxp_sar_adc *info = iio_priv(indio_dev);
766 
767 	nxp_sar_adc_irq_cfg(info, false);
768 }
769 
nxp_sar_adc_buffer_trigger_do_postenable(struct iio_dev * indio_dev)770 static int nxp_sar_adc_buffer_trigger_do_postenable(struct iio_dev *indio_dev)
771 {
772 	struct nxp_sar_adc *info = iio_priv(indio_dev);
773 
774 	nxp_sar_adc_irq_cfg(info, true);
775 
776 	return 0;
777 }
778 
nxp_sar_adc_buffer_postenable(struct iio_dev * indio_dev)779 static int nxp_sar_adc_buffer_postenable(struct iio_dev *indio_dev)
780 {
781 	struct nxp_sar_adc *info = iio_priv(indio_dev);
782 	int current_mode = iio_device_get_current_mode(indio_dev);
783 	unsigned long channel;
784 	int ret;
785 
786 	info->channels_used = 0;
787 
788 	/*
789 	 * The SAR-ADC has two groups of channels.
790 	 *
791 	 *	- Group #0:
792 	 *	* bit 0-7  : channel 0 -> channel 7
793 	 *	* bit 8-31 : reserved
794 	 *
795 	 *	- Group #32:
796 	 *	* bit 0-7  : Internal
797 	 *	* bit 8-31 : reserved
798 	 *
799 	 * The 8 channels from group #0 are used in this driver for
800 	 * ADC as described when declaring the IIO device and the
801 	 * mapping is the same. That means the active_scan_mask can be
802 	 * used directly to write the channel interrupt mask.
803 	 */
804 	nxp_sar_adc_channels_enable(info, *indio_dev->active_scan_mask);
805 
806 	for_each_set_bit(channel, indio_dev->active_scan_mask, NXP_SAR_ADC_NR_CHANNELS)
807 		info->buffered_chan[info->channels_used++] = channel;
808 
809 	nxp_sar_adc_enable(info);
810 
811 	if (current_mode == INDIO_BUFFER_SOFTWARE)
812 		ret = nxp_sar_adc_buffer_software_do_postenable(indio_dev);
813 	else
814 		ret = nxp_sar_adc_buffer_trigger_do_postenable(indio_dev);
815 	if (ret)
816 		goto out_postenable;
817 
818 	return 0;
819 
820 out_postenable:
821 	nxp_sar_adc_disable(info);
822 	nxp_sar_adc_channels_disable(info, *indio_dev->active_scan_mask);
823 
824 	return ret;
825 }
826 
nxp_sar_adc_buffer_predisable(struct iio_dev * indio_dev)827 static int nxp_sar_adc_buffer_predisable(struct iio_dev *indio_dev)
828 {
829 	struct nxp_sar_adc *info = iio_priv(indio_dev);
830 	int currentmode = iio_device_get_current_mode(indio_dev);
831 
832 	if (currentmode == INDIO_BUFFER_SOFTWARE)
833 		nxp_sar_adc_buffer_software_do_predisable(indio_dev);
834 	else
835 		nxp_sar_adc_buffer_trigger_do_predisable(indio_dev);
836 
837 	nxp_sar_adc_disable(info);
838 
839 	nxp_sar_adc_channels_disable(info, *indio_dev->active_scan_mask);
840 
841 	return 0;
842 }
843 
nxp_sar_adc_trigger_handler(int irq,void * p)844 static irqreturn_t nxp_sar_adc_trigger_handler(int irq, void *p)
845 {
846 	struct iio_poll_func *pf = p;
847 	struct iio_dev *indio_dev = pf->indio_dev;
848 	struct nxp_sar_adc *info = iio_priv(indio_dev);
849 	int ret;
850 
851 	ret = nxp_sar_adc_start_conversion(info, true);
852 	if (ret < 0)
853 		dev_dbg(&indio_dev->dev, "Failed to start conversion\n");
854 
855 	return IRQ_HANDLED;
856 }
857 
858 static const struct iio_buffer_setup_ops iio_triggered_buffer_setup_ops = {
859 	.postenable = nxp_sar_adc_buffer_postenable,
860 	.predisable = nxp_sar_adc_buffer_predisable,
861 };
862 
863 static const struct iio_info nxp_sar_adc_iio_info = {
864 	.read_raw  = nxp_sar_adc_read_raw,
865 	.write_raw = nxp_sar_adc_write_raw,
866 };
867 
nxp_sar_adc_dma_probe(struct device * dev,struct nxp_sar_adc * info)868 static int nxp_sar_adc_dma_probe(struct device *dev, struct nxp_sar_adc *info)
869 {
870 	u8 *rx_buf;
871 
872 	rx_buf = dmam_alloc_coherent(dev, NXP_SAR_ADC_DMA_BUFF_SZ,
873 				     &info->rx_dma_buf, GFP_KERNEL);
874 	if (!rx_buf)
875 		return -ENOMEM;
876 
877 	info->dma_buf.buf = rx_buf;
878 
879 	return 0;
880 }
881 
882 /*
883  * The documentation describes the reset values for the registers.
884  * However some registers do not have these values after a reset. It
885  * is not a desirable situation. In some other SoC family
886  * documentation NXP recommends not assuming the default values are
887  * set and to initialize the registers conforming to the documentation
888  * reset information to prevent this situation. Assume the same rule
889  * applies here as there is a discrepancy between what is read from
890  * the registers at reset time and the documentation.
891  */
nxp_sar_adc_set_default_values(struct nxp_sar_adc * info)892 static void nxp_sar_adc_set_default_values(struct nxp_sar_adc *info)
893 {
894 	writel(0x00003901, NXP_SAR_ADC_MCR(info->regs));
895 	writel(0x00000001, NXP_SAR_ADC_MSR(info->regs));
896 	writel(0x00000014, NXP_SAR_ADC_CTR0(info->regs));
897 	writel(0x00000014, NXP_SAR_ADC_CTR1(info->regs));
898 	writel(0x00000000, NXP_SAR_ADC_CIMR0(info->regs));
899 	writel(0x00000000, NXP_SAR_ADC_CIMR1(info->regs));
900 	writel(0x00000000, NXP_SAR_ADC_NCMR0(info->regs));
901 	writel(0x00000000, NXP_SAR_ADC_NCMR1(info->regs));
902 }
903 
nxp_sar_adc_probe(struct platform_device * pdev)904 static int nxp_sar_adc_probe(struct platform_device *pdev)
905 {
906 	struct device *dev = &pdev->dev;
907 	const struct nxp_sar_adc_data *data = device_get_match_data(dev);
908 	struct nxp_sar_adc *info;
909 	struct iio_dev *indio_dev;
910 	struct resource *mem;
911 	int irq, ret;
912 
913 	indio_dev = devm_iio_device_alloc(dev, sizeof(*info));
914 	if (!indio_dev)
915 		return -ENOMEM;
916 
917 	info = iio_priv(indio_dev);
918 	info->vref_mV = data->vref_mV;
919 	spin_lock_init(&info->lock);
920 	info->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &mem);
921 	if (IS_ERR(info->regs))
922 		return dev_err_probe(dev, PTR_ERR(info->regs),
923 				     "Failed to get and remap resource");
924 
925 	info->regs_phys = mem->start;
926 
927 	irq = platform_get_irq(pdev, 0);
928 	if (irq < 0)
929 		return irq;
930 
931 	ret = devm_request_irq(dev, irq, nxp_sar_adc_isr, 0, dev_name(dev),
932 			       indio_dev);
933 	if (ret < 0)
934 		return ret;
935 
936 	info->clk = devm_clk_get_enabled(dev, NULL);
937 	if (IS_ERR(info->clk))
938 		return dev_err_probe(dev, PTR_ERR(info->clk),
939 				     "Failed to get the clock\n");
940 
941 	platform_set_drvdata(pdev, indio_dev);
942 
943 	init_completion(&info->completion);
944 
945 	indio_dev->name = data->model;
946 	indio_dev->info = &nxp_sar_adc_iio_info;
947 	indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
948 	indio_dev->channels = nxp_sar_adc_iio_channels;
949 	indio_dev->num_channels = ARRAY_SIZE(nxp_sar_adc_iio_channels);
950 
951 	nxp_sar_adc_set_default_values(info);
952 
953 	ret = nxp_sar_adc_calibration(info);
954 	if (ret)
955 		dev_err_probe(dev, ret, "Calibration failed\n");
956 
957 	ret = nxp_sar_adc_dma_probe(dev, info);
958 	if (ret)
959 		return dev_err_probe(dev, ret, "Failed to initialize the DMA\n");
960 
961 	ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
962 					      &iio_pollfunc_store_time,
963 					      &nxp_sar_adc_trigger_handler,
964 					      &iio_triggered_buffer_setup_ops);
965 	if (ret < 0)
966 		return dev_err_probe(dev, ret, "Couldn't initialise the buffer\n");
967 
968 	ret = devm_iio_device_register(dev, indio_dev);
969 	if (ret)
970 		return dev_err_probe(dev, ret, "Couldn't register the device\n");
971 
972 	return 0;
973 }
974 
nxp_sar_adc_suspend(struct device * dev)975 static int nxp_sar_adc_suspend(struct device *dev)
976 {
977 	struct nxp_sar_adc *info = iio_priv(dev_get_drvdata(dev));
978 
979 	info->pwdn = nxp_sar_adc_disable(info);
980 	info->inpsamp = nxp_sar_adc_conversion_timing_get(info);
981 
982 	clk_disable_unprepare(info->clk);
983 
984 	return 0;
985 }
986 
nxp_sar_adc_resume(struct device * dev)987 static int nxp_sar_adc_resume(struct device *dev)
988 {
989 	struct nxp_sar_adc *info = iio_priv(dev_get_drvdata(dev));
990 	int ret;
991 
992 	ret = clk_prepare_enable(info->clk);
993 	if (ret)
994 		return ret;
995 
996 	nxp_sar_adc_conversion_timing_set(info, info->inpsamp);
997 
998 	if (!info->pwdn)
999 		nxp_sar_adc_enable(info);
1000 
1001 	return 0;
1002 }
1003 
1004 static DEFINE_SIMPLE_DEV_PM_OPS(nxp_sar_adc_pm_ops, nxp_sar_adc_suspend,
1005 				nxp_sar_adc_resume);
1006 
1007 static const struct nxp_sar_adc_data s32g2_sar_adc_data = {
1008 	.vref_mV = 1800,
1009 	.model = "s32g2-sar-adc",
1010 };
1011 
1012 static const struct of_device_id nxp_sar_adc_match[] = {
1013 	{ .compatible = "nxp,s32g2-sar-adc", .data = &s32g2_sar_adc_data },
1014 	{ }
1015 };
1016 MODULE_DEVICE_TABLE(of, nxp_sar_adc_match);
1017 
1018 static struct platform_driver nxp_sar_adc_driver = {
1019 	.probe = nxp_sar_adc_probe,
1020 	.driver = {
1021 		.name = "nxp-sar-adc",
1022 		.of_match_table = nxp_sar_adc_match,
1023 		.pm = pm_sleep_ptr(&nxp_sar_adc_pm_ops),
1024 	},
1025 };
1026 module_platform_driver(nxp_sar_adc_driver);
1027 
1028 MODULE_AUTHOR("NXP");
1029 MODULE_DESCRIPTION("NXP SAR-ADC driver");
1030 MODULE_LICENSE("GPL");
1031