Debian Codegraph @

drivers/iio/adc/nxp-sar-adc.c

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