blob: 1cfefb3b5e56cd44018332cda266288e8e62c973 [file] [log] [blame]
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001// SPDX-License-Identifier: GPL-2.0
2/*
3 * This file is the ADC part of the STM32 DFSDM driver
4 *
5 * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
6 * Author: Arnaud Pouliquen <arnaud.pouliquen@st.com>.
7 */
8
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01009#include <linux/dmaengine.h>
10#include <linux/dma-mapping.h>
Fabrice Gasniered582db2018-05-15 17:19:17 +020011#include <linux/iio/adc/stm32-dfsdm-adc.h>
Arnaud Pouliquene2e67712018-01-10 11:13:11 +010012#include <linux/iio/buffer.h>
13#include <linux/iio/hw-consumer.h>
Arnaud Pouliquene2e67712018-01-10 11:13:11 +010014#include <linux/iio/sysfs.h>
Fabrice Gasnier11646e82019-03-21 17:47:28 +010015#include <linux/iio/timer/stm32-lptim-trigger.h>
16#include <linux/iio/timer/stm32-timer-trigger.h>
17#include <linux/iio/trigger.h>
18#include <linux/iio/trigger_consumer.h>
19#include <linux/iio/triggered_buffer.h>
Fabrice Gasniered582db2018-05-15 17:19:17 +020020#include <linux/interrupt.h>
Arnaud Pouliquene2e67712018-01-10 11:13:11 +010021#include <linux/module.h>
Arnaud Pouliqueneca94982018-01-10 11:13:12 +010022#include <linux/of_device.h>
Arnaud Pouliquene2e67712018-01-10 11:13:11 +010023#include <linux/platform_device.h>
24#include <linux/regmap.h>
25#include <linux/slab.h>
26
27#include "stm32-dfsdm.h"
28
Arnaud Pouliqueneca94982018-01-10 11:13:12 +010029#define DFSDM_DMA_BUFFER_SIZE (4 * PAGE_SIZE)
30
Arnaud Pouliquene2e67712018-01-10 11:13:11 +010031/* Conversion timeout */
32#define DFSDM_TIMEOUT_US 100000
33#define DFSDM_TIMEOUT (msecs_to_jiffies(DFSDM_TIMEOUT_US / 1000))
34
35/* Oversampling attribute default */
36#define DFSDM_DEFAULT_OVERSAMPLING 100
37
38/* Oversampling max values */
39#define DFSDM_MAX_INT_OVERSAMPLING 256
40#define DFSDM_MAX_FL_OVERSAMPLING 1024
41
Olivier Moysan12c83982019-06-19 15:03:47 +020042/* Limit filter output resolution to 31 bits. (i.e. sample range is +/-2^30) */
43#define DFSDM_DATA_MAX BIT(30)
44/*
45 * Data are output as two's complement data in a 24 bit field.
46 * Data from filters are in the range +/-2^(n-1)
47 * 2^(n-1) maximum positive value cannot be coded in 2's complement n bits
48 * An extra bit is required to avoid wrap-around of the binary code for 2^(n-1)
49 * So, the resolution of samples from filter is actually limited to 23 bits
50 */
51#define DFSDM_DATA_RES 24
Arnaud Pouliquene2e67712018-01-10 11:13:11 +010052
Fabrice Gasnier6f2c4a592019-03-21 17:47:23 +010053/* Filter configuration */
54#define DFSDM_CR1_CFG_MASK (DFSDM_CR1_RCH_MASK | DFSDM_CR1_RCONT_MASK | \
Fabrice Gasniera6096762019-03-21 17:47:27 +010055 DFSDM_CR1_RSYNC_MASK | DFSDM_CR1_JSYNC_MASK | \
56 DFSDM_CR1_JSCAN_MASK)
Fabrice Gasnier6f2c4a592019-03-21 17:47:23 +010057
Arnaud Pouliquene2e67712018-01-10 11:13:11 +010058enum sd_converter_type {
59 DFSDM_AUDIO,
60 DFSDM_IIO,
61};
62
63struct stm32_dfsdm_dev_data {
64 int type;
Fabrice Gasnierb455d062020-04-30 11:28:46 +020065 int (*init)(struct device *dev, struct iio_dev *indio_dev);
Arnaud Pouliquene2e67712018-01-10 11:13:11 +010066 unsigned int num_channels;
67 const struct regmap_config *regmap_cfg;
68};
69
70struct stm32_dfsdm_adc {
71 struct stm32_dfsdm *dfsdm;
72 const struct stm32_dfsdm_dev_data *dev_data;
73 unsigned int fl_id;
Fabrice Gasniera6096762019-03-21 17:47:27 +010074 unsigned int nconv;
75 unsigned long smask;
Arnaud Pouliquene2e67712018-01-10 11:13:11 +010076
77 /* ADC specific */
78 unsigned int oversamp;
79 struct iio_hw_consumer *hwc;
80 struct completion completion;
81 u32 *buffer;
82
Arnaud Pouliqueneca94982018-01-10 11:13:12 +010083 /* Audio specific */
84 unsigned int spi_freq; /* SPI bus clock frequency */
85 unsigned int sample_freq; /* Sample frequency after filter decimation */
86 int (*cb)(const void *data, size_t size, void *cb_priv);
87 void *cb_priv;
88
89 /* DMA */
90 u8 *rx_buf;
91 unsigned int bufi; /* Buffer current position */
92 unsigned int buf_sz; /* Buffer size */
93 struct dma_chan *dma_chan;
94 dma_addr_t dma_buf;
Arnaud Pouliquene2e67712018-01-10 11:13:11 +010095};
96
97struct stm32_dfsdm_str2field {
98 const char *name;
99 unsigned int val;
100};
101
102/* DFSDM channel serial interface type */
103static const struct stm32_dfsdm_str2field stm32_dfsdm_chan_type[] = {
104 { "SPI_R", 0 }, /* SPI with data on rising edge */
105 { "SPI_F", 1 }, /* SPI with data on falling edge */
106 { "MANCH_R", 2 }, /* Manchester codec, rising edge = logic 0 */
107 { "MANCH_F", 3 }, /* Manchester codec, falling edge = logic 1 */
108 {},
109};
110
111/* DFSDM channel clock source */
112static const struct stm32_dfsdm_str2field stm32_dfsdm_chan_src[] = {
113 /* External SPI clock (CLKIN x) */
114 { "CLKIN", DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL },
115 /* Internal SPI clock (CLKOUT) */
116 { "CLKOUT", DFSDM_CHANNEL_SPI_CLOCK_INTERNAL },
117 /* Internal SPI clock divided by 2 (falling edge) */
118 { "CLKOUT_F", DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_FALLING },
119 /* Internal SPI clock divided by 2 (falling edge) */
120 { "CLKOUT_R", DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_RISING },
121 {},
122};
123
124static int stm32_dfsdm_str2val(const char *str,
125 const struct stm32_dfsdm_str2field *list)
126{
127 const struct stm32_dfsdm_str2field *p = list;
128
129 for (p = list; p && p->name; p++)
130 if (!strcmp(p->name, str))
131 return p->val;
132
133 return -EINVAL;
134}
135
Fabrice Gasnier11646e82019-03-21 17:47:28 +0100136/**
137 * struct stm32_dfsdm_trig_info - DFSDM trigger info
138 * @name: name of the trigger, corresponding to its source
139 * @jextsel: trigger signal selection
140 */
141struct stm32_dfsdm_trig_info {
142 const char *name;
143 unsigned int jextsel;
144};
145
146/* hardware injected trigger enable, edge selection */
147enum stm32_dfsdm_jexten {
148 STM32_DFSDM_JEXTEN_DISABLED,
149 STM32_DFSDM_JEXTEN_RISING_EDGE,
150 STM32_DFSDM_JEXTEN_FALLING_EDGE,
151 STM32_DFSDM_EXTEN_BOTH_EDGES,
152};
153
154static const struct stm32_dfsdm_trig_info stm32_dfsdm_trigs[] = {
155 { TIM1_TRGO, 0 },
156 { TIM1_TRGO2, 1 },
157 { TIM8_TRGO, 2 },
158 { TIM8_TRGO2, 3 },
159 { TIM3_TRGO, 4 },
160 { TIM4_TRGO, 5 },
161 { TIM16_OC1, 6 },
162 { TIM6_TRGO, 7 },
163 { TIM7_TRGO, 8 },
164 { LPTIM1_OUT, 26 },
165 { LPTIM2_OUT, 27 },
166 { LPTIM3_OUT, 28 },
167 {},
168};
169
170static int stm32_dfsdm_get_jextsel(struct iio_dev *indio_dev,
171 struct iio_trigger *trig)
172{
173 int i;
174
175 /* lookup triggers registered by stm32 timer trigger driver */
176 for (i = 0; stm32_dfsdm_trigs[i].name; i++) {
177 /**
178 * Checking both stm32 timer trigger type and trig name
179 * should be safe against arbitrary trigger names.
180 */
181 if ((is_stm32_timer_trigger(trig) ||
182 is_stm32_lptim_trigger(trig)) &&
183 !strcmp(stm32_dfsdm_trigs[i].name, trig->name)) {
184 return stm32_dfsdm_trigs[i].jextsel;
185 }
186 }
187
188 return -EINVAL;
189}
190
Olivier Moysan12c83982019-06-19 15:03:47 +0200191static int stm32_dfsdm_compute_osrs(struct stm32_dfsdm_filter *fl,
192 unsigned int fast, unsigned int oversamp)
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100193{
194 unsigned int i, d, fosr, iosr;
Olivier Moysan12c83982019-06-19 15:03:47 +0200195 u64 res, max;
196 int bits, shift;
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100197 unsigned int m = 1; /* multiplication factor */
198 unsigned int p = fl->ford; /* filter order (ford) */
Olivier Moysand7162042019-06-19 15:03:50 +0200199 struct stm32_dfsdm_filter_osr *flo = &fl->flo[fast];
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100200
Mugilraj Dhavachelvanbb142d42021-04-01 21:13:43 +0530201 pr_debug("Requested oversampling: %d\n", oversamp);
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100202 /*
203 * This function tries to compute filter oversampling and integrator
204 * oversampling, base on oversampling ratio requested by user.
205 *
206 * Decimation d depends on the filter order and the oversampling ratios.
207 * ford: filter order
208 * fosr: filter over sampling ratio
209 * iosr: integrator over sampling ratio
210 */
211 if (fl->ford == DFSDM_FASTSINC_ORDER) {
212 m = 2;
213 p = 2;
214 }
215
216 /*
217 * Look for filter and integrator oversampling ratios which allows
Olivier Moysan12c83982019-06-19 15:03:47 +0200218 * to maximize data output resolution.
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100219 */
220 for (fosr = 1; fosr <= DFSDM_MAX_FL_OVERSAMPLING; fosr++) {
221 for (iosr = 1; iosr <= DFSDM_MAX_INT_OVERSAMPLING; iosr++) {
222 if (fast)
223 d = fosr * iosr;
224 else if (fl->ford == DFSDM_FASTSINC_ORDER)
225 d = fosr * (iosr + 3) + 2;
226 else
227 d = fosr * (iosr - 1 + p) + p;
228
229 if (d > oversamp)
230 break;
231 else if (d != oversamp)
232 continue;
233 /*
234 * Check resolution (limited to signed 32 bits)
235 * res <= 2^31
236 * Sincx filters:
237 * res = m * fosr^p x iosr (with m=1, p=ford)
238 * FastSinc filter
239 * res = m * fosr^p x iosr (with m=2, p=2)
240 */
241 res = fosr;
242 for (i = p - 1; i > 0; i--) {
243 res = res * (u64)fosr;
Olivier Moysan12c83982019-06-19 15:03:47 +0200244 if (res > DFSDM_DATA_MAX)
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100245 break;
246 }
Olivier Moysan12c83982019-06-19 15:03:47 +0200247 if (res > DFSDM_DATA_MAX)
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100248 continue;
Olivier Moysan12c83982019-06-19 15:03:47 +0200249
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100250 res = res * (u64)m * (u64)iosr;
Olivier Moysan12c83982019-06-19 15:03:47 +0200251 if (res > DFSDM_DATA_MAX)
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100252 continue;
253
Olivier Moysan12c83982019-06-19 15:03:47 +0200254 if (res >= flo->res) {
255 flo->res = res;
256 flo->fosr = fosr;
257 flo->iosr = iosr;
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100258
Olivier Moysan12c83982019-06-19 15:03:47 +0200259 bits = fls(flo->res);
260 /* 8 LBSs in data register contain chan info */
261 max = flo->res << 8;
262
263 /* if resolution is not a power of two */
264 if (flo->res > BIT(bits - 1))
265 bits++;
266 else
267 max--;
268
269 shift = DFSDM_DATA_RES - bits;
270 /*
271 * Compute right/left shift
272 * Right shift is performed by hardware
273 * when transferring samples to data register.
274 * Left shift is done by software on buffer
275 */
276 if (shift > 0) {
277 /* Resolution is lower than 24 bits */
278 flo->rshift = 0;
279 flo->lshift = shift;
280 } else {
281 /*
282 * If resolution is 24 bits or more,
283 * max positive value may be ambiguous
284 * (equal to max negative value as sign
285 * bit is dropped).
286 * Reduce resolution to 23 bits (rshift)
287 * to keep the sign on bit 23 and treat
288 * saturation before rescaling on 24
289 * bits (lshift).
290 */
291 flo->rshift = 1 - shift;
292 flo->lshift = 1;
293 max >>= flo->rshift;
294 }
295 flo->max = (s32)max;
Olivier Moysan41bceb12020-10-07 17:34:59 +0200296 flo->bits = bits;
Olivier Moysan12c83982019-06-19 15:03:47 +0200297
Mugilraj Dhavachelvanbb142d42021-04-01 21:13:43 +0530298 pr_debug("fast %d, fosr %d, iosr %d, res 0x%llx/%d bits, rshift %d, lshift %d\n",
299 fast, flo->fosr, flo->iosr,
Olivier Moysan12c83982019-06-19 15:03:47 +0200300 flo->res, bits, flo->rshift,
301 flo->lshift);
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100302 }
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100303 }
304 }
305
Olivier Moysan12c83982019-06-19 15:03:47 +0200306 if (!flo->res)
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100307 return -EINVAL;
308
309 return 0;
310}
311
Olivier Moysand7162042019-06-19 15:03:50 +0200312static int stm32_dfsdm_compute_all_osrs(struct iio_dev *indio_dev,
313 unsigned int oversamp)
314{
315 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
316 struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[adc->fl_id];
317 int ret0, ret1;
318
319 memset(&fl->flo[0], 0, sizeof(fl->flo[0]));
320 memset(&fl->flo[1], 0, sizeof(fl->flo[1]));
321
322 ret0 = stm32_dfsdm_compute_osrs(fl, 0, oversamp);
323 ret1 = stm32_dfsdm_compute_osrs(fl, 1, oversamp);
324 if (ret0 < 0 && ret1 < 0) {
325 dev_err(&indio_dev->dev,
326 "Filter parameters not found: errors %d/%d\n",
327 ret0, ret1);
328 return -EINVAL;
329 }
330
331 return 0;
332}
333
Alexandru Ardelean07b6c9d2020-05-25 11:26:48 +0300334static int stm32_dfsdm_start_channel(struct iio_dev *indio_dev)
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100335{
Alexandru Ardelean07b6c9d2020-05-25 11:26:48 +0300336 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
Fabrice Gasniera6096762019-03-21 17:47:27 +0100337 struct regmap *regmap = adc->dfsdm->regmap;
338 const struct iio_chan_spec *chan;
339 unsigned int bit;
340 int ret;
341
342 for_each_set_bit(bit, &adc->smask, sizeof(adc->smask) * BITS_PER_BYTE) {
343 chan = indio_dev->channels + bit;
344 ret = regmap_update_bits(regmap, DFSDM_CHCFGR1(chan->channel),
345 DFSDM_CHCFGR1_CHEN_MASK,
346 DFSDM_CHCFGR1_CHEN(1));
347 if (ret < 0)
348 return ret;
349 }
350
351 return 0;
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100352}
353
Alexandru Ardelean07b6c9d2020-05-25 11:26:48 +0300354static void stm32_dfsdm_stop_channel(struct iio_dev *indio_dev)
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100355{
Alexandru Ardelean07b6c9d2020-05-25 11:26:48 +0300356 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
Fabrice Gasniera6096762019-03-21 17:47:27 +0100357 struct regmap *regmap = adc->dfsdm->regmap;
358 const struct iio_chan_spec *chan;
359 unsigned int bit;
360
361 for_each_set_bit(bit, &adc->smask, sizeof(adc->smask) * BITS_PER_BYTE) {
362 chan = indio_dev->channels + bit;
363 regmap_update_bits(regmap, DFSDM_CHCFGR1(chan->channel),
364 DFSDM_CHCFGR1_CHEN_MASK,
365 DFSDM_CHCFGR1_CHEN(0));
366 }
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100367}
368
369static int stm32_dfsdm_chan_configure(struct stm32_dfsdm *dfsdm,
370 struct stm32_dfsdm_channel *ch)
371{
372 unsigned int id = ch->id;
373 struct regmap *regmap = dfsdm->regmap;
374 int ret;
375
376 ret = regmap_update_bits(regmap, DFSDM_CHCFGR1(id),
377 DFSDM_CHCFGR1_SITP_MASK,
378 DFSDM_CHCFGR1_SITP(ch->type));
379 if (ret < 0)
380 return ret;
381 ret = regmap_update_bits(regmap, DFSDM_CHCFGR1(id),
382 DFSDM_CHCFGR1_SPICKSEL_MASK,
383 DFSDM_CHCFGR1_SPICKSEL(ch->src));
384 if (ret < 0)
385 return ret;
386 return regmap_update_bits(regmap, DFSDM_CHCFGR1(id),
387 DFSDM_CHCFGR1_CHINSEL_MASK,
388 DFSDM_CHCFGR1_CHINSEL(ch->alt_si));
389}
390
Fabrice Gasniera6096762019-03-21 17:47:27 +0100391static int stm32_dfsdm_start_filter(struct stm32_dfsdm_adc *adc,
Fabrice Gasnier11646e82019-03-21 17:47:28 +0100392 unsigned int fl_id,
393 struct iio_trigger *trig)
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100394{
Fabrice Gasniera6096762019-03-21 17:47:27 +0100395 struct stm32_dfsdm *dfsdm = adc->dfsdm;
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100396 int ret;
397
398 /* Enable filter */
399 ret = regmap_update_bits(dfsdm->regmap, DFSDM_CR1(fl_id),
400 DFSDM_CR1_DFEN_MASK, DFSDM_CR1_DFEN(1));
401 if (ret < 0)
402 return ret;
403
Fabrice Gasniera6096762019-03-21 17:47:27 +0100404 /* Nothing more to do for injected (scan mode/triggered) conversions */
Fabrice Gasnier11646e82019-03-21 17:47:28 +0100405 if (adc->nconv > 1 || trig)
Fabrice Gasniera6096762019-03-21 17:47:27 +0100406 return 0;
407
408 /* Software start (single or continuous) regular conversion */
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100409 return regmap_update_bits(dfsdm->regmap, DFSDM_CR1(fl_id),
410 DFSDM_CR1_RSWSTART_MASK,
411 DFSDM_CR1_RSWSTART(1));
412}
413
Fabrice Gasnierc620da32018-02-23 13:50:59 +0100414static void stm32_dfsdm_stop_filter(struct stm32_dfsdm *dfsdm,
415 unsigned int fl_id)
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100416{
417 /* Disable conversion */
418 regmap_update_bits(dfsdm->regmap, DFSDM_CR1(fl_id),
419 DFSDM_CR1_DFEN_MASK, DFSDM_CR1_DFEN(0));
420}
421
Alexandru Ardelean07b6c9d2020-05-25 11:26:48 +0300422static int stm32_dfsdm_filter_set_trig(struct iio_dev *indio_dev,
Fabrice Gasnier11646e82019-03-21 17:47:28 +0100423 unsigned int fl_id,
424 struct iio_trigger *trig)
425{
Alexandru Ardelean07b6c9d2020-05-25 11:26:48 +0300426 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
Fabrice Gasnier11646e82019-03-21 17:47:28 +0100427 struct regmap *regmap = adc->dfsdm->regmap;
428 u32 jextsel = 0, jexten = STM32_DFSDM_JEXTEN_DISABLED;
429 int ret;
430
431 if (trig) {
432 ret = stm32_dfsdm_get_jextsel(indio_dev, trig);
433 if (ret < 0)
434 return ret;
435
436 /* set trigger source and polarity (default to rising edge) */
437 jextsel = ret;
438 jexten = STM32_DFSDM_JEXTEN_RISING_EDGE;
439 }
440
441 ret = regmap_update_bits(regmap, DFSDM_CR1(fl_id),
442 DFSDM_CR1_JEXTSEL_MASK | DFSDM_CR1_JEXTEN_MASK,
443 DFSDM_CR1_JEXTSEL(jextsel) |
444 DFSDM_CR1_JEXTEN(jexten));
445 if (ret < 0)
446 return ret;
447
448 return 0;
449}
450
Alexandru Ardelean07b6c9d2020-05-25 11:26:48 +0300451static int stm32_dfsdm_channels_configure(struct iio_dev *indio_dev,
Olivier Moysan12c83982019-06-19 15:03:47 +0200452 unsigned int fl_id,
453 struct iio_trigger *trig)
454{
Alexandru Ardelean07b6c9d2020-05-25 11:26:48 +0300455 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
Olivier Moysan12c83982019-06-19 15:03:47 +0200456 struct regmap *regmap = adc->dfsdm->regmap;
457 struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[fl_id];
Olivier Moysand7162042019-06-19 15:03:50 +0200458 struct stm32_dfsdm_filter_osr *flo = &fl->flo[0];
Olivier Moysan12c83982019-06-19 15:03:47 +0200459 const struct iio_chan_spec *chan;
460 unsigned int bit;
461 int ret;
462
Olivier Moysand7162042019-06-19 15:03:50 +0200463 fl->fast = 0;
464
465 /*
466 * In continuous mode, use fast mode configuration,
467 * if it provides a better resolution.
468 */
469 if (adc->nconv == 1 && !trig &&
470 (indio_dev->currentmode & INDIO_BUFFER_SOFTWARE)) {
471 if (fl->flo[1].res >= fl->flo[0].res) {
472 fl->fast = 1;
473 flo = &fl->flo[1];
474 }
475 }
476
Olivier Moysan12c83982019-06-19 15:03:47 +0200477 if (!flo->res)
478 return -EINVAL;
479
Olivier Moysan41bceb12020-10-07 17:34:59 +0200480 dev_dbg(&indio_dev->dev, "Samples actual resolution: %d bits",
481 min(flo->bits, (u32)DFSDM_DATA_RES - 1));
482
Olivier Moysan12c83982019-06-19 15:03:47 +0200483 for_each_set_bit(bit, &adc->smask,
484 sizeof(adc->smask) * BITS_PER_BYTE) {
485 chan = indio_dev->channels + bit;
486
487 ret = regmap_update_bits(regmap,
488 DFSDM_CHCFGR2(chan->channel),
489 DFSDM_CHCFGR2_DTRBS_MASK,
490 DFSDM_CHCFGR2_DTRBS(flo->rshift));
491 if (ret)
492 return ret;
493 }
494
495 return 0;
496}
497
Alexandru Ardelean07b6c9d2020-05-25 11:26:48 +0300498static int stm32_dfsdm_filter_configure(struct iio_dev *indio_dev,
Fabrice Gasnier11646e82019-03-21 17:47:28 +0100499 unsigned int fl_id,
500 struct iio_trigger *trig)
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100501{
Alexandru Ardelean07b6c9d2020-05-25 11:26:48 +0300502 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
Fabrice Gasnier6f2c4a592019-03-21 17:47:23 +0100503 struct regmap *regmap = adc->dfsdm->regmap;
504 struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[fl_id];
Olivier Moysand7162042019-06-19 15:03:50 +0200505 struct stm32_dfsdm_filter_osr *flo = &fl->flo[fl->fast];
Fabrice Gasnier6f2c4a592019-03-21 17:47:23 +0100506 u32 cr1;
Fabrice Gasniera6096762019-03-21 17:47:27 +0100507 const struct iio_chan_spec *chan;
508 unsigned int bit, jchg = 0;
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100509 int ret;
510
511 /* Average integrator oversampling */
512 ret = regmap_update_bits(regmap, DFSDM_FCR(fl_id), DFSDM_FCR_IOSR_MASK,
Olivier Moysan12c83982019-06-19 15:03:47 +0200513 DFSDM_FCR_IOSR(flo->iosr - 1));
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100514 if (ret)
515 return ret;
516
517 /* Filter order and Oversampling */
518 ret = regmap_update_bits(regmap, DFSDM_FCR(fl_id), DFSDM_FCR_FOSR_MASK,
Olivier Moysan12c83982019-06-19 15:03:47 +0200519 DFSDM_FCR_FOSR(flo->fosr - 1));
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100520 if (ret)
521 return ret;
522
523 ret = regmap_update_bits(regmap, DFSDM_FCR(fl_id), DFSDM_FCR_FORD_MASK,
524 DFSDM_FCR_FORD(fl->ford));
525 if (ret)
526 return ret;
527
Alexandru Ardelean07b6c9d2020-05-25 11:26:48 +0300528 ret = stm32_dfsdm_filter_set_trig(indio_dev, fl_id, trig);
Fabrice Gasnier11646e82019-03-21 17:47:28 +0100529 if (ret)
530 return ret;
531
Olivier Moysand7162042019-06-19 15:03:50 +0200532 ret = regmap_update_bits(regmap, DFSDM_CR1(fl_id),
533 DFSDM_CR1_FAST_MASK,
534 DFSDM_CR1_FAST(fl->fast));
535 if (ret)
536 return ret;
537
Fabrice Gasniera6096762019-03-21 17:47:27 +0100538 /*
539 * DFSDM modes configuration W.R.T audio/iio type modes
540 * ----------------------------------------------------------------
541 * Modes | regular | regular | injected | injected |
542 * | | continuous | | + scan |
543 * --------------|---------|--------------|----------|------------|
544 * single conv | x | | | |
545 * (1 chan) | | | | |
546 * --------------|---------|--------------|----------|------------|
547 * 1 Audio chan | | sample freq | | |
548 * | | or sync_mode | | |
549 * --------------|---------|--------------|----------|------------|
550 * 1 IIO chan | | sample freq | trigger | |
551 * | | or sync_mode | | |
552 * --------------|---------|--------------|----------|------------|
553 * 2+ IIO chans | | | | trigger or |
554 * | | | | sync_mode |
555 * ----------------------------------------------------------------
556 */
Fabrice Gasnier11646e82019-03-21 17:47:28 +0100557 if (adc->nconv == 1 && !trig) {
Fabrice Gasniera6096762019-03-21 17:47:27 +0100558 bit = __ffs(adc->smask);
559 chan = indio_dev->channels + bit;
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100560
Fabrice Gasniera6096762019-03-21 17:47:27 +0100561 /* Use regular conversion for single channel without trigger */
562 cr1 = DFSDM_CR1_RCH(chan->channel);
Fabrice Gasnier6f2c4a592019-03-21 17:47:23 +0100563
Fabrice Gasniera6096762019-03-21 17:47:27 +0100564 /* Continuous conversions triggered by SPI clk in buffer mode */
565 if (indio_dev->currentmode & INDIO_BUFFER_SOFTWARE)
566 cr1 |= DFSDM_CR1_RCONT(1);
567
568 cr1 |= DFSDM_CR1_RSYNC(fl->sync_mode);
569 } else {
570 /* Use injected conversion for multiple channels */
571 for_each_set_bit(bit, &adc->smask,
572 sizeof(adc->smask) * BITS_PER_BYTE) {
573 chan = indio_dev->channels + bit;
574 jchg |= BIT(chan->channel);
575 }
576 ret = regmap_write(regmap, DFSDM_JCHGR(fl_id), jchg);
577 if (ret < 0)
578 return ret;
579
580 /* Use scan mode for multiple channels */
Fabrice Gasnier11646e82019-03-21 17:47:28 +0100581 cr1 = DFSDM_CR1_JSCAN((adc->nconv > 1) ? 1 : 0);
Fabrice Gasniera6096762019-03-21 17:47:27 +0100582
583 /*
Fabrice Gasnier11646e82019-03-21 17:47:28 +0100584 * Continuous conversions not supported in injected mode,
585 * either use:
586 * - conversions in sync with filter 0
587 * - triggered conversions
Fabrice Gasniera6096762019-03-21 17:47:27 +0100588 */
Fabrice Gasnier11646e82019-03-21 17:47:28 +0100589 if (!fl->sync_mode && !trig)
Fabrice Gasniera6096762019-03-21 17:47:27 +0100590 return -EINVAL;
591 cr1 |= DFSDM_CR1_JSYNC(fl->sync_mode);
592 }
Fabrice Gasnier6f2c4a592019-03-21 17:47:23 +0100593
594 return regmap_update_bits(regmap, DFSDM_CR1(fl_id), DFSDM_CR1_CFG_MASK,
595 cr1);
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100596}
597
kbuild test robot9ae148f2018-01-10 23:23:05 +0800598static int stm32_dfsdm_channel_parse_of(struct stm32_dfsdm *dfsdm,
599 struct iio_dev *indio_dev,
600 struct iio_chan_spec *ch)
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100601{
602 struct stm32_dfsdm_channel *df_ch;
603 const char *of_str;
604 int chan_idx = ch->scan_index;
605 int ret, val;
606
607 ret = of_property_read_u32_index(indio_dev->dev.of_node,
608 "st,adc-channels", chan_idx,
609 &ch->channel);
610 if (ret < 0) {
611 dev_err(&indio_dev->dev,
612 " Error parsing 'st,adc-channels' for idx %d\n",
613 chan_idx);
614 return ret;
615 }
616 if (ch->channel >= dfsdm->num_chs) {
617 dev_err(&indio_dev->dev,
618 " Error bad channel number %d (max = %d)\n",
619 ch->channel, dfsdm->num_chs);
620 return -EINVAL;
621 }
622
623 ret = of_property_read_string_index(indio_dev->dev.of_node,
624 "st,adc-channel-names", chan_idx,
625 &ch->datasheet_name);
626 if (ret < 0) {
627 dev_err(&indio_dev->dev,
628 " Error parsing 'st,adc-channel-names' for idx %d\n",
629 chan_idx);
630 return ret;
631 }
632
633 df_ch = &dfsdm->ch_list[ch->channel];
634 df_ch->id = ch->channel;
635
636 ret = of_property_read_string_index(indio_dev->dev.of_node,
637 "st,adc-channel-types", chan_idx,
638 &of_str);
639 if (!ret) {
Fabrice Gasnierc620da32018-02-23 13:50:59 +0100640 val = stm32_dfsdm_str2val(of_str, stm32_dfsdm_chan_type);
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100641 if (val < 0)
642 return val;
643 } else {
644 val = 0;
645 }
646 df_ch->type = val;
647
648 ret = of_property_read_string_index(indio_dev->dev.of_node,
649 "st,adc-channel-clk-src", chan_idx,
650 &of_str);
651 if (!ret) {
Fabrice Gasnierc620da32018-02-23 13:50:59 +0100652 val = stm32_dfsdm_str2val(of_str, stm32_dfsdm_chan_src);
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100653 if (val < 0)
654 return val;
655 } else {
656 val = 0;
657 }
658 df_ch->src = val;
659
660 ret = of_property_read_u32_index(indio_dev->dev.of_node,
661 "st,adc-alt-channel", chan_idx,
662 &df_ch->alt_si);
663 if (ret < 0)
664 df_ch->alt_si = 0;
665
666 return 0;
667}
668
Arnaud Pouliqueneca94982018-01-10 11:13:12 +0100669static ssize_t dfsdm_adc_audio_get_spiclk(struct iio_dev *indio_dev,
670 uintptr_t priv,
671 const struct iio_chan_spec *chan,
672 char *buf)
673{
674 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
675
676 return snprintf(buf, PAGE_SIZE, "%d\n", adc->spi_freq);
677}
678
Fabrice Gasnier9f571102019-03-25 15:24:01 +0100679static int dfsdm_adc_set_samp_freq(struct iio_dev *indio_dev,
680 unsigned int sample_freq,
681 unsigned int spi_freq)
682{
683 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
Fabrice Gasnier9f571102019-03-25 15:24:01 +0100684 unsigned int oversamp;
685 int ret;
686
687 oversamp = DIV_ROUND_CLOSEST(spi_freq, sample_freq);
688 if (spi_freq % sample_freq)
689 dev_dbg(&indio_dev->dev,
690 "Rate not accurate. requested (%u), actual (%u)\n",
691 sample_freq, spi_freq / oversamp);
692
Olivier Moysand7162042019-06-19 15:03:50 +0200693 ret = stm32_dfsdm_compute_all_osrs(indio_dev, oversamp);
694 if (ret < 0)
Fabrice Gasnier9f571102019-03-25 15:24:01 +0100695 return ret;
Olivier Moysand7162042019-06-19 15:03:50 +0200696
Fabrice Gasnier9f571102019-03-25 15:24:01 +0100697 adc->sample_freq = spi_freq / oversamp;
698 adc->oversamp = oversamp;
699
700 return 0;
701}
702
Arnaud Pouliqueneca94982018-01-10 11:13:12 +0100703static ssize_t dfsdm_adc_audio_set_spiclk(struct iio_dev *indio_dev,
704 uintptr_t priv,
705 const struct iio_chan_spec *chan,
706 const char *buf, size_t len)
707{
708 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
Fabrice Gasnier0645af12018-02-23 13:50:58 +0100709 struct stm32_dfsdm_channel *ch = &adc->dfsdm->ch_list[chan->channel];
Arnaud Pouliqueneca94982018-01-10 11:13:12 +0100710 unsigned int sample_freq = adc->sample_freq;
711 unsigned int spi_freq;
712 int ret;
713
714 dev_err(&indio_dev->dev, "enter %s\n", __func__);
715 /* If DFSDM is master on SPI, SPI freq can not be updated */
716 if (ch->src != DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL)
717 return -EPERM;
718
719 ret = kstrtoint(buf, 0, &spi_freq);
720 if (ret)
721 return ret;
722
723 if (!spi_freq)
724 return -EINVAL;
725
726 if (sample_freq) {
Fabrice Gasnier9f571102019-03-25 15:24:01 +0100727 ret = dfsdm_adc_set_samp_freq(indio_dev, sample_freq, spi_freq);
728 if (ret < 0)
Arnaud Pouliqueneca94982018-01-10 11:13:12 +0100729 return ret;
Arnaud Pouliqueneca94982018-01-10 11:13:12 +0100730 }
731 adc->spi_freq = spi_freq;
732
733 return len;
734}
735
Alexandru Ardelean07b6c9d2020-05-25 11:26:48 +0300736static int stm32_dfsdm_start_conv(struct iio_dev *indio_dev,
Fabrice Gasnier11646e82019-03-21 17:47:28 +0100737 struct iio_trigger *trig)
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100738{
Alexandru Ardelean07b6c9d2020-05-25 11:26:48 +0300739 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100740 struct regmap *regmap = adc->dfsdm->regmap;
741 int ret;
742
Alexandru Ardelean07b6c9d2020-05-25 11:26:48 +0300743 ret = stm32_dfsdm_channels_configure(indio_dev, adc->fl_id, trig);
Olivier Moysan12c83982019-06-19 15:03:47 +0200744 if (ret < 0)
745 return ret;
746
Alexandru Ardelean07b6c9d2020-05-25 11:26:48 +0300747 ret = stm32_dfsdm_start_channel(indio_dev);
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100748 if (ret < 0)
749 return ret;
750
Alexandru Ardelean07b6c9d2020-05-25 11:26:48 +0300751 ret = stm32_dfsdm_filter_configure(indio_dev, adc->fl_id, trig);
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100752 if (ret < 0)
753 goto stop_channels;
754
Fabrice Gasnier11646e82019-03-21 17:47:28 +0100755 ret = stm32_dfsdm_start_filter(adc, adc->fl_id, trig);
Arnaud Pouliqueneca94982018-01-10 11:13:12 +0100756 if (ret < 0)
Fabrice Gasnier6f2c4a592019-03-21 17:47:23 +0100757 goto filter_unconfigure;
Arnaud Pouliqueneca94982018-01-10 11:13:12 +0100758
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100759 return 0;
760
Fabrice Gasnier6f2c4a592019-03-21 17:47:23 +0100761filter_unconfigure:
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100762 regmap_update_bits(regmap, DFSDM_CR1(adc->fl_id),
Fabrice Gasnier6f2c4a592019-03-21 17:47:23 +0100763 DFSDM_CR1_CFG_MASK, 0);
764stop_channels:
Alexandru Ardelean07b6c9d2020-05-25 11:26:48 +0300765 stm32_dfsdm_stop_channel(indio_dev);
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100766
767 return ret;
768}
769
Alexandru Ardelean07b6c9d2020-05-25 11:26:48 +0300770static void stm32_dfsdm_stop_conv(struct iio_dev *indio_dev)
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100771{
Alexandru Ardelean07b6c9d2020-05-25 11:26:48 +0300772 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100773 struct regmap *regmap = adc->dfsdm->regmap;
774
775 stm32_dfsdm_stop_filter(adc->dfsdm, adc->fl_id);
776
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100777 regmap_update_bits(regmap, DFSDM_CR1(adc->fl_id),
Fabrice Gasnier6f2c4a592019-03-21 17:47:23 +0100778 DFSDM_CR1_CFG_MASK, 0);
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100779
Alexandru Ardelean07b6c9d2020-05-25 11:26:48 +0300780 stm32_dfsdm_stop_channel(indio_dev);
Arnaud Pouliquene2e67712018-01-10 11:13:11 +0100781}
782
Arnaud Pouliqueneca94982018-01-10 11:13:12 +0100783static int stm32_dfsdm_set_watermark(struct iio_dev *indio_dev,
784 unsigned int val)
785{
786 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
787 unsigned int watermark = DFSDM_DMA_BUFFER_SIZE / 2;
Fabrice Gasnier11646e82019-03-21 17:47:28 +0100788 unsigned int rx_buf_sz = DFSDM_DMA_BUFFER_SIZE;
Arnaud Pouliqueneca94982018-01-10 11:13:12 +0100789
790 /*
791 * DMA cyclic transfers are used, buffer is split into two periods.
792 * There should be :
793 * - always one buffer (period) DMA is working on
794 * - one buffer (period) driver pushed to ASoC side.
795 */
796 watermark = min(watermark, val * (unsigned int)(sizeof(u32)));
Fabrice Gasnier11646e82019-03-21 17:47:28 +0100797 adc->buf_sz = min(rx_buf_sz, watermark * 2 * adc->nconv);
Arnaud Pouliqueneca94982018-01-10 11:13:12 +0100798
799 return 0;
800}
801
802static unsigned int stm32_dfsdm_adc_dma_residue(struct stm32_dfsdm_adc *adc)
803{
804 struct dma_tx_state state;
805 enum dma_status status;
806
807 status = dmaengine_tx_status(adc->dma_chan,
808 adc->dma_chan->cookie,
809 &state);
810 if (status == DMA_IN_PROGRESS) {
811 /* Residue is size in bytes from end of buffer */
812 unsigned int i = adc->buf_sz - state.residue;
813 unsigned int size;
814
815 /* Return available bytes */
816 if (i >= adc->bufi)
817 size = i - adc->bufi;
818 else
819 size = adc->buf_sz + i - adc->bufi;
820
821 return size;
822 }
823
824 return 0;
825}
826
Olivier Moysan102afde2019-06-19 15:03:49 +0200827static inline void stm32_dfsdm_process_data(struct stm32_dfsdm_adc *adc,
828 s32 *buffer)
829{
830 struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[adc->fl_id];
Olivier Moysand7162042019-06-19 15:03:50 +0200831 struct stm32_dfsdm_filter_osr *flo = &fl->flo[fl->fast];
Olivier Moysan102afde2019-06-19 15:03:49 +0200832 unsigned int i = adc->nconv;
833 s32 *ptr = buffer;
834
835 while (i--) {
836 /* Mask 8 LSB that contains the channel ID */
837 *ptr &= 0xFFFFFF00;
838 /* Convert 2^(n-1) sample to 2^(n-1)-1 to avoid wrap-around */
839 if (*ptr > flo->max)
840 *ptr -= 1;
841 /*
842 * Samples from filter are retrieved with 23 bits resolution
843 * or less. Shift left to align MSB on 24 bits.
844 */
845 *ptr <<= flo->lshift;
846
847 ptr++;
848 }
849}
850
Fabrice Gasnier11646e82019-03-21 17:47:28 +0100851static void stm32_dfsdm_dma_buffer_done(void *data)
Arnaud Pouliqueneca94982018-01-10 11:13:12 +0100852{
853 struct iio_dev *indio_dev = data;
854 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
855 int available = stm32_dfsdm_adc_dma_residue(adc);
856 size_t old_pos;
857
858 /*
859 * FIXME: In Kernel interface does not support cyclic DMA buffer,and
860 * offers only an interface to push data samples per samples.
861 * For this reason IIO buffer interface is not used and interface is
862 * bypassed using a private callback registered by ASoC.
863 * This should be a temporary solution waiting a cyclic DMA engine
864 * support in IIO.
865 */
866
Mugilraj Dhavachelvanbb142d42021-04-01 21:13:43 +0530867 dev_dbg(&indio_dev->dev, "pos = %d, available = %d\n",
Arnaud Pouliqueneca94982018-01-10 11:13:12 +0100868 adc->bufi, available);
869 old_pos = adc->bufi;
870
871 while (available >= indio_dev->scan_bytes) {
Olivier Moysan12c83982019-06-19 15:03:47 +0200872 s32 *buffer = (s32 *)&adc->rx_buf[adc->bufi];
Arnaud Pouliqueneca94982018-01-10 11:13:12 +0100873
Olivier Moysan102afde2019-06-19 15:03:49 +0200874 stm32_dfsdm_process_data(adc, buffer);
Olivier Moysan12c83982019-06-19 15:03:47 +0200875
Arnaud Pouliqueneca94982018-01-10 11:13:12 +0100876 available -= indio_dev->scan_bytes;
877 adc->bufi += indio_dev->scan_bytes;
878 if (adc->bufi >= adc->buf_sz) {
879 if (adc->cb)
880 adc->cb(&adc->rx_buf[old_pos],
881 adc->buf_sz - old_pos, adc->cb_priv);
882 adc->bufi = 0;
883 old_pos = 0;
884 }
Olivier Moysane19ac9d2020-01-21 12:02:56 +0100885 /*
886 * In DMA mode the trigger services of IIO are not used
887 * (e.g. no call to iio_trigger_poll).
888 * Calling irq handler associated to the hardware trigger is not
889 * relevant as the conversions have already been done. Data
890 * transfers are performed directly in DMA callback instead.
891 * This implementation avoids to call trigger irq handler that
892 * may sleep, in an atomic context (DMA irq handler context).
893 */
Fabrice Gasnier11646e82019-03-21 17:47:28 +0100894 if (adc->dev_data->type == DFSDM_IIO)
895 iio_push_to_buffers(indio_dev, buffer);
Arnaud Pouliqueneca94982018-01-10 11:13:12 +0100896 }
897 if (adc->cb)
898 adc->cb(&adc->rx_buf[old_pos], adc->bufi - old_pos,
899 adc->cb_priv);
900}
901
902static int stm32_dfsdm_adc_dma_start(struct iio_dev *indio_dev)
903{
904 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
Olivier Moysan18eaffa2019-06-19 15:03:51 +0200905 /*
906 * The DFSDM supports half-word transfers. However, for 16 bits record,
907 * 4 bytes buswidth is kept, to avoid losing samples LSBs when left
908 * shift is required.
909 */
Fabrice Gasnier74648502019-03-21 17:47:25 +0100910 struct dma_slave_config config = {
Fabrice Gasniera6096762019-03-21 17:47:27 +0100911 .src_addr = (dma_addr_t)adc->dfsdm->phys_base,
Fabrice Gasnier74648502019-03-21 17:47:25 +0100912 .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
913 };
Arnaud Pouliqueneca94982018-01-10 11:13:12 +0100914 struct dma_async_tx_descriptor *desc;
915 dma_cookie_t cookie;
916 int ret;
917
918 if (!adc->dma_chan)
919 return -EINVAL;
920
Mugilraj Dhavachelvanbb142d42021-04-01 21:13:43 +0530921 dev_dbg(&indio_dev->dev, "size=%d watermark=%d\n",
Arnaud Pouliqueneca94982018-01-10 11:13:12 +0100922 adc->buf_sz, adc->buf_sz / 2);
923
Fabrice Gasnier11646e82019-03-21 17:47:28 +0100924 if (adc->nconv == 1 && !indio_dev->trig)
Fabrice Gasniera6096762019-03-21 17:47:27 +0100925 config.src_addr += DFSDM_RDATAR(adc->fl_id);
926 else
927 config.src_addr += DFSDM_JDATAR(adc->fl_id);
Fabrice Gasnier74648502019-03-21 17:47:25 +0100928 ret = dmaengine_slave_config(adc->dma_chan, &config);
929 if (ret)
930 return ret;
931
Arnaud Pouliqueneca94982018-01-10 11:13:12 +0100932 /* Prepare a DMA cyclic transaction */
933 desc = dmaengine_prep_dma_cyclic(adc->dma_chan,
934 adc->dma_buf,
935 adc->buf_sz, adc->buf_sz / 2,
936 DMA_DEV_TO_MEM,
937 DMA_PREP_INTERRUPT);
938 if (!desc)
939 return -EBUSY;
940
Fabrice Gasnier11646e82019-03-21 17:47:28 +0100941 desc->callback = stm32_dfsdm_dma_buffer_done;
Arnaud Pouliqueneca94982018-01-10 11:13:12 +0100942 desc->callback_param = indio_dev;
943
944 cookie = dmaengine_submit(desc);
945 ret = dma_submit_error(cookie);
Fabrice Gasniercaf9c1e2019-03-21 17:47:24 +0100946 if (ret)
947 goto err_stop_dma;
Arnaud Pouliqueneca94982018-01-10 11:13:12 +0100948
949 /* Issue pending DMA requests */
950 dma_async_issue_pending(adc->dma_chan);
951
Fabrice Gasnier11646e82019-03-21 17:47:28 +0100952 if (adc->nconv == 1 && !indio_dev->trig) {
Fabrice Gasniera6096762019-03-21 17:47:27 +0100953 /* Enable regular DMA transfer*/
954 ret = regmap_update_bits(adc->dfsdm->regmap,
955 DFSDM_CR1(adc->fl_id),
956 DFSDM_CR1_RDMAEN_MASK,
957 DFSDM_CR1_RDMAEN_MASK);
958 } else {
959 /* Enable injected DMA transfer*/
960 ret = regmap_update_bits(adc->dfsdm->regmap,
961 DFSDM_CR1(adc->fl_id),
962 DFSDM_CR1_JDMAEN_MASK,
963 DFSDM_CR1_JDMAEN_MASK);
964 }
965
Fabrice Gasniercaf9c1e2019-03-21 17:47:24 +0100966 if (ret < 0)
967 goto err_stop_dma;
968
Arnaud Pouliqueneca94982018-01-10 11:13:12 +0100969 return 0;
Fabrice Gasniercaf9c1e2019-03-21 17:47:24 +0100970
971err_stop_dma:
972 dmaengine_terminate_all(adc->dma_chan);
973
974 return ret;
975}
976
977static void stm32_dfsdm_adc_dma_stop(struct iio_dev *indio_dev)
978{
979 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
980
981 if (!adc->dma_chan)
982 return;
983
984 regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR1(adc->fl_id),
Fabrice Gasniera6096762019-03-21 17:47:27 +0100985 DFSDM_CR1_RDMAEN_MASK | DFSDM_CR1_JDMAEN_MASK, 0);
Fabrice Gasniercaf9c1e2019-03-21 17:47:24 +0100986 dmaengine_terminate_all(adc->dma_chan);
Arnaud Pouliqueneca94982018-01-10 11:13:12 +0100987}
988
Fabrice Gasniera6096762019-03-21 17:47:27 +0100989static int stm32_dfsdm_update_scan_mode(struct iio_dev *indio_dev,
990 const unsigned long *scan_mask)
991{
992 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
993
994 adc->nconv = bitmap_weight(scan_mask, indio_dev->masklength);
995 adc->smask = *scan_mask;
996
997 dev_dbg(&indio_dev->dev, "nconv=%d mask=%lx\n", adc->nconv, *scan_mask);
998
999 return 0;
1000}
1001
Lars-Peter Clausenf11d59d2020-05-25 14:38:53 +03001002static int stm32_dfsdm_postenable(struct iio_dev *indio_dev)
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001003{
1004 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
1005 int ret;
1006
1007 /* Reset adc buffer index */
1008 adc->bufi = 0;
1009
Fabrice Gasnier9491f75f2019-03-21 17:47:26 +01001010 if (adc->hwc) {
1011 ret = iio_hw_consumer_enable(adc->hwc);
1012 if (ret < 0)
Fabrice Gasnier6ec417d2019-03-25 15:49:28 +01001013 return ret;
Fabrice Gasnier9491f75f2019-03-21 17:47:26 +01001014 }
1015
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001016 ret = stm32_dfsdm_start_dfsdm(adc->dfsdm);
1017 if (ret < 0)
Fabrice Gasnier9491f75f2019-03-21 17:47:26 +01001018 goto err_stop_hwc;
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001019
Fabrice Gasniercaf9c1e2019-03-21 17:47:24 +01001020 ret = stm32_dfsdm_adc_dma_start(indio_dev);
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001021 if (ret) {
Fabrice Gasniercaf9c1e2019-03-21 17:47:24 +01001022 dev_err(&indio_dev->dev, "Can't start DMA\n");
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001023 goto stop_dfsdm;
1024 }
1025
Alexandru Ardelean07b6c9d2020-05-25 11:26:48 +03001026 ret = stm32_dfsdm_start_conv(indio_dev, indio_dev->trig);
Fabrice Gasniercaf9c1e2019-03-21 17:47:24 +01001027 if (ret) {
1028 dev_err(&indio_dev->dev, "Can't start conversion\n");
1029 goto err_stop_dma;
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001030 }
1031
1032 return 0;
1033
Fabrice Gasniercaf9c1e2019-03-21 17:47:24 +01001034err_stop_dma:
1035 stm32_dfsdm_adc_dma_stop(indio_dev);
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001036stop_dfsdm:
1037 stm32_dfsdm_stop_dfsdm(adc->dfsdm);
Fabrice Gasnier9491f75f2019-03-21 17:47:26 +01001038err_stop_hwc:
1039 if (adc->hwc)
1040 iio_hw_consumer_disable(adc->hwc);
Fabrice Gasnier6ec417d2019-03-25 15:49:28 +01001041
1042 return ret;
1043}
1044
Lars-Peter Clausenf11d59d2020-05-25 14:38:53 +03001045static int stm32_dfsdm_predisable(struct iio_dev *indio_dev)
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001046{
1047 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
1048
Alexandru Ardelean07b6c9d2020-05-25 11:26:48 +03001049 stm32_dfsdm_stop_conv(indio_dev);
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001050
Fabrice Gasniercaf9c1e2019-03-21 17:47:24 +01001051 stm32_dfsdm_adc_dma_stop(indio_dev);
1052
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001053 stm32_dfsdm_stop_dfsdm(adc->dfsdm);
1054
Fabrice Gasnier9491f75f2019-03-21 17:47:26 +01001055 if (adc->hwc)
1056 iio_hw_consumer_disable(adc->hwc);
Fabrice Gasnier11646e82019-03-21 17:47:28 +01001057
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001058 return 0;
1059}
1060
1061static const struct iio_buffer_setup_ops stm32_dfsdm_buffer_setup_ops = {
1062 .postenable = &stm32_dfsdm_postenable,
1063 .predisable = &stm32_dfsdm_predisable,
1064};
1065
1066/**
1067 * stm32_dfsdm_get_buff_cb() - register a callback that will be called when
1068 * DMA transfer period is achieved.
1069 *
1070 * @iio_dev: Handle to IIO device.
1071 * @cb: Pointer to callback function:
1072 * - data: pointer to data buffer
1073 * - size: size in byte of the data buffer
1074 * - private: pointer to consumer private structure.
1075 * @private: Pointer to consumer private structure.
1076 */
1077int stm32_dfsdm_get_buff_cb(struct iio_dev *iio_dev,
1078 int (*cb)(const void *data, size_t size,
1079 void *private),
1080 void *private)
1081{
1082 struct stm32_dfsdm_adc *adc;
1083
1084 if (!iio_dev)
1085 return -EINVAL;
1086 adc = iio_priv(iio_dev);
1087
1088 adc->cb = cb;
1089 adc->cb_priv = private;
1090
1091 return 0;
1092}
1093EXPORT_SYMBOL_GPL(stm32_dfsdm_get_buff_cb);
1094
1095/**
1096 * stm32_dfsdm_release_buff_cb - unregister buffer callback
1097 *
1098 * @iio_dev: Handle to IIO device.
1099 */
1100int stm32_dfsdm_release_buff_cb(struct iio_dev *iio_dev)
1101{
1102 struct stm32_dfsdm_adc *adc;
1103
1104 if (!iio_dev)
1105 return -EINVAL;
1106 adc = iio_priv(iio_dev);
1107
1108 adc->cb = NULL;
1109 adc->cb_priv = NULL;
1110
1111 return 0;
1112}
1113EXPORT_SYMBOL_GPL(stm32_dfsdm_release_buff_cb);
1114
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001115static int stm32_dfsdm_single_conv(struct iio_dev *indio_dev,
1116 const struct iio_chan_spec *chan, int *res)
1117{
1118 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
1119 long timeout;
1120 int ret;
1121
1122 reinit_completion(&adc->completion);
1123
1124 adc->buffer = res;
1125
1126 ret = stm32_dfsdm_start_dfsdm(adc->dfsdm);
1127 if (ret < 0)
1128 return ret;
1129
1130 ret = regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR2(adc->fl_id),
1131 DFSDM_CR2_REOCIE_MASK, DFSDM_CR2_REOCIE(1));
1132 if (ret < 0)
1133 goto stop_dfsdm;
1134
Fabrice Gasniera6096762019-03-21 17:47:27 +01001135 adc->nconv = 1;
1136 adc->smask = BIT(chan->scan_index);
Alexandru Ardelean07b6c9d2020-05-25 11:26:48 +03001137 ret = stm32_dfsdm_start_conv(indio_dev, NULL);
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001138 if (ret < 0) {
1139 regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR2(adc->fl_id),
1140 DFSDM_CR2_REOCIE_MASK, DFSDM_CR2_REOCIE(0));
1141 goto stop_dfsdm;
1142 }
1143
1144 timeout = wait_for_completion_interruptible_timeout(&adc->completion,
1145 DFSDM_TIMEOUT);
1146
1147 /* Mask IRQ for regular conversion achievement*/
1148 regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR2(adc->fl_id),
1149 DFSDM_CR2_REOCIE_MASK, DFSDM_CR2_REOCIE(0));
1150
1151 if (timeout == 0)
1152 ret = -ETIMEDOUT;
1153 else if (timeout < 0)
1154 ret = timeout;
1155 else
1156 ret = IIO_VAL_INT;
1157
Alexandru Ardelean07b6c9d2020-05-25 11:26:48 +03001158 stm32_dfsdm_stop_conv(indio_dev);
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001159
Olivier Moysandc269352019-11-27 14:07:29 +01001160 stm32_dfsdm_process_data(adc, res);
1161
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001162stop_dfsdm:
1163 stm32_dfsdm_stop_dfsdm(adc->dfsdm);
1164
1165 return ret;
1166}
1167
1168static int stm32_dfsdm_write_raw(struct iio_dev *indio_dev,
1169 struct iio_chan_spec const *chan,
1170 int val, int val2, long mask)
1171{
1172 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
Fabrice Gasnier0645af12018-02-23 13:50:58 +01001173 struct stm32_dfsdm_channel *ch = &adc->dfsdm->ch_list[chan->channel];
Fabrice Gasnierd58109d2018-03-13 15:23:06 +01001174 unsigned int spi_freq;
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001175 int ret = -EINVAL;
1176
Olivier Moysanf81ec5b2019-11-27 14:10:08 +01001177 switch (ch->src) {
1178 case DFSDM_CHANNEL_SPI_CLOCK_INTERNAL:
1179 spi_freq = adc->dfsdm->spi_master_freq;
1180 break;
1181 case DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_FALLING:
1182 case DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_RISING:
1183 spi_freq = adc->dfsdm->spi_master_freq / 2;
1184 break;
1185 default:
1186 spi_freq = adc->spi_freq;
1187 }
1188
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001189 switch (mask) {
1190 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
Fabrice Gasnier37ada022019-03-21 17:47:29 +01001191 ret = iio_device_claim_direct_mode(indio_dev);
1192 if (ret)
1193 return ret;
Olivier Moysanf81ec5b2019-11-27 14:10:08 +01001194
Olivier Moysand7162042019-06-19 15:03:50 +02001195 ret = stm32_dfsdm_compute_all_osrs(indio_dev, val);
Olivier Moysanf81ec5b2019-11-27 14:10:08 +01001196 if (!ret) {
1197 dev_dbg(&indio_dev->dev,
1198 "Sampling rate changed from (%u) to (%u)\n",
1199 adc->sample_freq, spi_freq / val);
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001200 adc->oversamp = val;
Olivier Moysanf81ec5b2019-11-27 14:10:08 +01001201 adc->sample_freq = spi_freq / val;
1202 }
Fabrice Gasnier37ada022019-03-21 17:47:29 +01001203 iio_device_release_direct_mode(indio_dev);
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001204 return ret;
1205
1206 case IIO_CHAN_INFO_SAMP_FREQ:
1207 if (!val)
1208 return -EINVAL;
Fabrice Gasnierd58109d2018-03-13 15:23:06 +01001209
Fabrice Gasnier37ada022019-03-21 17:47:29 +01001210 ret = iio_device_claim_direct_mode(indio_dev);
1211 if (ret)
1212 return ret;
1213
Fabrice Gasnier9f571102019-03-25 15:24:01 +01001214 ret = dfsdm_adc_set_samp_freq(indio_dev, val, spi_freq);
Fabrice Gasnier37ada022019-03-21 17:47:29 +01001215 iio_device_release_direct_mode(indio_dev);
Fabrice Gasnier9f571102019-03-25 15:24:01 +01001216 return ret;
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001217 }
1218
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001219 return -EINVAL;
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001220}
1221
1222static int stm32_dfsdm_read_raw(struct iio_dev *indio_dev,
1223 struct iio_chan_spec const *chan, int *val,
1224 int *val2, long mask)
1225{
1226 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
1227 int ret;
1228
1229 switch (mask) {
1230 case IIO_CHAN_INFO_RAW:
Fabrice Gasnier37ada022019-03-21 17:47:29 +01001231 ret = iio_device_claim_direct_mode(indio_dev);
1232 if (ret)
1233 return ret;
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001234 ret = iio_hw_consumer_enable(adc->hwc);
1235 if (ret < 0) {
1236 dev_err(&indio_dev->dev,
1237 "%s: IIO enable failed (channel %d)\n",
1238 __func__, chan->channel);
Fabrice Gasnier37ada022019-03-21 17:47:29 +01001239 iio_device_release_direct_mode(indio_dev);
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001240 return ret;
1241 }
1242 ret = stm32_dfsdm_single_conv(indio_dev, chan, val);
1243 iio_hw_consumer_disable(adc->hwc);
1244 if (ret < 0) {
1245 dev_err(&indio_dev->dev,
1246 "%s: Conversion failed (channel %d)\n",
1247 __func__, chan->channel);
Fabrice Gasnier37ada022019-03-21 17:47:29 +01001248 iio_device_release_direct_mode(indio_dev);
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001249 return ret;
1250 }
Fabrice Gasnier37ada022019-03-21 17:47:29 +01001251 iio_device_release_direct_mode(indio_dev);
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001252 return IIO_VAL_INT;
1253
1254 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
1255 *val = adc->oversamp;
1256
1257 return IIO_VAL_INT;
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001258
1259 case IIO_CHAN_INFO_SAMP_FREQ:
1260 *val = adc->sample_freq;
1261
1262 return IIO_VAL_INT;
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001263 }
1264
1265 return -EINVAL;
1266}
1267
Fabrice Gasnier11646e82019-03-21 17:47:28 +01001268static int stm32_dfsdm_validate_trigger(struct iio_dev *indio_dev,
1269 struct iio_trigger *trig)
1270{
1271 return stm32_dfsdm_get_jextsel(indio_dev, trig) < 0 ? -EINVAL : 0;
1272}
1273
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001274static const struct iio_info stm32_dfsdm_info_audio = {
1275 .hwfifo_set_watermark = stm32_dfsdm_set_watermark,
1276 .read_raw = stm32_dfsdm_read_raw,
1277 .write_raw = stm32_dfsdm_write_raw,
Fabrice Gasniera6096762019-03-21 17:47:27 +01001278 .update_scan_mode = stm32_dfsdm_update_scan_mode,
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001279};
1280
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001281static const struct iio_info stm32_dfsdm_info_adc = {
Fabrice Gasnier11646e82019-03-21 17:47:28 +01001282 .hwfifo_set_watermark = stm32_dfsdm_set_watermark,
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001283 .read_raw = stm32_dfsdm_read_raw,
1284 .write_raw = stm32_dfsdm_write_raw,
Fabrice Gasniera6096762019-03-21 17:47:27 +01001285 .update_scan_mode = stm32_dfsdm_update_scan_mode,
Fabrice Gasnier11646e82019-03-21 17:47:28 +01001286 .validate_trigger = stm32_dfsdm_validate_trigger,
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001287};
1288
1289static irqreturn_t stm32_dfsdm_irq(int irq, void *arg)
1290{
Alexandru Ardelean07b6c9d2020-05-25 11:26:48 +03001291 struct iio_dev *indio_dev = arg;
1292 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001293 struct regmap *regmap = adc->dfsdm->regmap;
1294 unsigned int status, int_en;
1295
1296 regmap_read(regmap, DFSDM_ISR(adc->fl_id), &status);
1297 regmap_read(regmap, DFSDM_CR2(adc->fl_id), &int_en);
1298
1299 if (status & DFSDM_ISR_REOCF_MASK) {
1300 /* Read the data register clean the IRQ status */
1301 regmap_read(regmap, DFSDM_RDATAR(adc->fl_id), adc->buffer);
1302 complete(&adc->completion);
1303 }
1304
1305 if (status & DFSDM_ISR_ROVRF_MASK) {
1306 if (int_en & DFSDM_CR2_ROVRIE_MASK)
1307 dev_warn(&indio_dev->dev, "Overrun detected\n");
1308 regmap_update_bits(regmap, DFSDM_ICR(adc->fl_id),
1309 DFSDM_ICR_CLRROVRF_MASK,
1310 DFSDM_ICR_CLRROVRF_MASK);
1311 }
1312
1313 return IRQ_HANDLED;
1314}
1315
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001316/*
1317 * Define external info for SPI Frequency and audio sampling rate that can be
1318 * configured by ASoC driver through consumer.h API
1319 */
1320static const struct iio_chan_spec_ext_info dfsdm_adc_audio_ext_info[] = {
1321 /* spi_clk_freq : clock freq on SPI/manchester bus used by channel */
1322 {
1323 .name = "spi_clk_freq",
1324 .shared = IIO_SHARED_BY_TYPE,
1325 .read = dfsdm_adc_audio_get_spiclk,
1326 .write = dfsdm_adc_audio_set_spiclk,
1327 },
1328 {},
1329};
1330
1331static void stm32_dfsdm_dma_release(struct iio_dev *indio_dev)
1332{
1333 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
1334
1335 if (adc->dma_chan) {
1336 dma_free_coherent(adc->dma_chan->device->dev,
1337 DFSDM_DMA_BUFFER_SIZE,
1338 adc->rx_buf, adc->dma_buf);
1339 dma_release_channel(adc->dma_chan);
1340 }
1341}
1342
Fabrice Gasnierb455d062020-04-30 11:28:46 +02001343static int stm32_dfsdm_dma_request(struct device *dev,
1344 struct iio_dev *indio_dev)
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001345{
1346 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001347
Fabrice Gasnierb455d062020-04-30 11:28:46 +02001348 adc->dma_chan = dma_request_chan(dev, "rx");
Peter Ujfalusia9ab6242020-01-07 13:45:32 +02001349 if (IS_ERR(adc->dma_chan)) {
1350 int ret = PTR_ERR(adc->dma_chan);
1351
1352 adc->dma_chan = NULL;
1353 return ret;
1354 }
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001355
1356 adc->rx_buf = dma_alloc_coherent(adc->dma_chan->device->dev,
1357 DFSDM_DMA_BUFFER_SIZE,
1358 &adc->dma_buf, GFP_KERNEL);
1359 if (!adc->rx_buf) {
Fabrice Gasnier74648502019-03-21 17:47:25 +01001360 dma_release_channel(adc->dma_chan);
1361 return -ENOMEM;
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001362 }
1363
Fabrice Gasnier11646e82019-03-21 17:47:28 +01001364 indio_dev->modes |= INDIO_BUFFER_SOFTWARE;
1365 indio_dev->setup_ops = &stm32_dfsdm_buffer_setup_ops;
1366
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001367 return 0;
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001368}
1369
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001370static int stm32_dfsdm_adc_chan_init_one(struct iio_dev *indio_dev,
1371 struct iio_chan_spec *ch)
1372{
1373 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
1374 int ret;
1375
1376 ret = stm32_dfsdm_channel_parse_of(adc->dfsdm, indio_dev, ch);
1377 if (ret < 0)
1378 return ret;
1379
1380 ch->type = IIO_VOLTAGE;
1381 ch->indexed = 1;
1382
1383 /*
1384 * IIO_CHAN_INFO_RAW: used to compute regular conversion
1385 * IIO_CHAN_INFO_OVERSAMPLING_RATIO: used to set oversampling
1386 */
1387 ch->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
Fabrice Gasnier11646e82019-03-21 17:47:28 +01001388 ch->info_mask_shared_by_all = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) |
1389 BIT(IIO_CHAN_INFO_SAMP_FREQ);
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001390
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001391 if (adc->dev_data->type == DFSDM_AUDIO) {
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001392 ch->ext_info = dfsdm_adc_audio_ext_info;
1393 } else {
Olivier Moysanc6013bf2019-06-19 15:03:48 +02001394 ch->scan_type.shift = 8;
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001395 }
Olivier Moysanc6013bf2019-06-19 15:03:48 +02001396 ch->scan_type.sign = 's';
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001397 ch->scan_type.realbits = 24;
1398 ch->scan_type.storagebits = 32;
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001399
1400 return stm32_dfsdm_chan_configure(adc->dfsdm,
1401 &adc->dfsdm->ch_list[ch->channel]);
1402}
1403
Fabrice Gasnierb455d062020-04-30 11:28:46 +02001404static int stm32_dfsdm_audio_init(struct device *dev, struct iio_dev *indio_dev)
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001405{
1406 struct iio_chan_spec *ch;
1407 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
1408 struct stm32_dfsdm_channel *d_ch;
1409 int ret;
1410
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001411 ch = devm_kzalloc(&indio_dev->dev, sizeof(*ch), GFP_KERNEL);
1412 if (!ch)
1413 return -ENOMEM;
1414
1415 ch->scan_index = 0;
1416
1417 ret = stm32_dfsdm_adc_chan_init_one(indio_dev, ch);
1418 if (ret < 0) {
1419 dev_err(&indio_dev->dev, "Channels init failed\n");
1420 return ret;
1421 }
1422 ch->info_mask_separate = BIT(IIO_CHAN_INFO_SAMP_FREQ);
1423
Fabrice Gasnier0645af12018-02-23 13:50:58 +01001424 d_ch = &adc->dfsdm->ch_list[ch->channel];
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001425 if (d_ch->src != DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL)
1426 adc->spi_freq = adc->dfsdm->spi_master_freq;
1427
1428 indio_dev->num_channels = 1;
1429 indio_dev->channels = ch;
1430
Fabrice Gasnierb455d062020-04-30 11:28:46 +02001431 return stm32_dfsdm_dma_request(dev, indio_dev);
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001432}
1433
Fabrice Gasnierb455d062020-04-30 11:28:46 +02001434static int stm32_dfsdm_adc_init(struct device *dev, struct iio_dev *indio_dev)
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001435{
1436 struct iio_chan_spec *ch;
1437 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
1438 int num_ch;
1439 int ret, chan_idx;
1440
1441 adc->oversamp = DFSDM_DEFAULT_OVERSAMPLING;
Olivier Moysand7162042019-06-19 15:03:50 +02001442 ret = stm32_dfsdm_compute_all_osrs(indio_dev, adc->oversamp);
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001443 if (ret < 0)
1444 return ret;
1445
1446 num_ch = of_property_count_u32_elems(indio_dev->dev.of_node,
1447 "st,adc-channels");
1448 if (num_ch < 0 || num_ch > adc->dfsdm->num_chs) {
1449 dev_err(&indio_dev->dev, "Bad st,adc-channels\n");
1450 return num_ch < 0 ? num_ch : -EINVAL;
1451 }
1452
1453 /* Bind to SD modulator IIO device */
1454 adc->hwc = devm_iio_hw_consumer_alloc(&indio_dev->dev);
1455 if (IS_ERR(adc->hwc))
1456 return -EPROBE_DEFER;
1457
1458 ch = devm_kcalloc(&indio_dev->dev, num_ch, sizeof(*ch),
1459 GFP_KERNEL);
1460 if (!ch)
1461 return -ENOMEM;
1462
1463 for (chan_idx = 0; chan_idx < num_ch; chan_idx++) {
Fabrice Gasnier0645af12018-02-23 13:50:58 +01001464 ch[chan_idx].scan_index = chan_idx;
1465 ret = stm32_dfsdm_adc_chan_init_one(indio_dev, &ch[chan_idx]);
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001466 if (ret < 0) {
1467 dev_err(&indio_dev->dev, "Channels init failed\n");
1468 return ret;
1469 }
1470 }
1471
1472 indio_dev->num_channels = num_ch;
1473 indio_dev->channels = ch;
1474
1475 init_completion(&adc->completion);
1476
Fabrice Gasnier11646e82019-03-21 17:47:28 +01001477 /* Optionally request DMA */
Fabrice Gasnierb455d062020-04-30 11:28:46 +02001478 ret = stm32_dfsdm_dma_request(dev, indio_dev);
Peter Ujfalusia9ab6242020-01-07 13:45:32 +02001479 if (ret) {
Krzysztof Kozlowskice30eeb2020-08-29 08:47:16 +02001480 if (ret != -ENODEV)
1481 return dev_err_probe(dev, ret,
1482 "DMA channel request failed with\n");
Peter Ujfalusia9ab6242020-01-07 13:45:32 +02001483
Fabrice Gasnierb455d062020-04-30 11:28:46 +02001484 dev_dbg(dev, "No DMA support\n");
Fabrice Gasnier11646e82019-03-21 17:47:28 +01001485 return 0;
1486 }
1487
1488 ret = iio_triggered_buffer_setup(indio_dev,
Olivier Moysane19ac9d2020-01-21 12:02:56 +01001489 &iio_pollfunc_store_time, NULL,
Fabrice Gasnier11646e82019-03-21 17:47:28 +01001490 &stm32_dfsdm_buffer_setup_ops);
1491 if (ret) {
1492 stm32_dfsdm_dma_release(indio_dev);
1493 dev_err(&indio_dev->dev, "buffer setup failed\n");
1494 return ret;
1495 }
1496
1497 /* lptimer/timer hardware triggers */
1498 indio_dev->modes |= INDIO_HARDWARE_TRIGGERED;
1499
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001500 return 0;
1501}
1502
1503static const struct stm32_dfsdm_dev_data stm32h7_dfsdm_adc_data = {
1504 .type = DFSDM_IIO,
1505 .init = stm32_dfsdm_adc_init,
1506};
1507
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001508static const struct stm32_dfsdm_dev_data stm32h7_dfsdm_audio_data = {
1509 .type = DFSDM_AUDIO,
1510 .init = stm32_dfsdm_audio_init,
1511};
1512
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001513static const struct of_device_id stm32_dfsdm_adc_match[] = {
1514 {
1515 .compatible = "st,stm32-dfsdm-adc",
1516 .data = &stm32h7_dfsdm_adc_data,
1517 },
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001518 {
1519 .compatible = "st,stm32-dfsdm-dmic",
1520 .data = &stm32h7_dfsdm_audio_data,
1521 },
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001522 {}
1523};
1524
1525static int stm32_dfsdm_adc_probe(struct platform_device *pdev)
1526{
1527 struct device *dev = &pdev->dev;
1528 struct stm32_dfsdm_adc *adc;
1529 struct device_node *np = dev->of_node;
1530 const struct stm32_dfsdm_dev_data *dev_data;
1531 struct iio_dev *iio;
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001532 char *name;
1533 int ret, irq, val;
1534
Arnaud Pouliquenabaca802018-01-15 09:57:39 +01001535 dev_data = of_device_get_match_data(dev);
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001536 iio = devm_iio_device_alloc(dev, sizeof(*adc));
Wei Yongjund5ff18b2018-01-11 11:12:41 +00001537 if (!iio) {
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001538 dev_err(dev, "%s: Failed to allocate IIO\n", __func__);
Wei Yongjund5ff18b2018-01-11 11:12:41 +00001539 return -ENOMEM;
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001540 }
1541
1542 adc = iio_priv(iio);
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001543 adc->dfsdm = dev_get_drvdata(dev->parent);
1544
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001545 iio->dev.of_node = np;
Fabrice Gasnier11646e82019-03-21 17:47:28 +01001546 iio->modes = INDIO_DIRECT_MODE;
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001547
Alexandru Ardelean07b6c9d2020-05-25 11:26:48 +03001548 platform_set_drvdata(pdev, iio);
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001549
1550 ret = of_property_read_u32(dev->of_node, "reg", &adc->fl_id);
Fabrice Gasnierdfa105b2018-02-23 13:51:00 +01001551 if (ret != 0 || adc->fl_id >= adc->dfsdm->num_fls) {
1552 dev_err(dev, "Missing or bad reg property\n");
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001553 return -EINVAL;
1554 }
1555
1556 name = devm_kzalloc(dev, sizeof("dfsdm-adc0"), GFP_KERNEL);
1557 if (!name)
1558 return -ENOMEM;
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001559 if (dev_data->type == DFSDM_AUDIO) {
1560 iio->info = &stm32_dfsdm_info_audio;
1561 snprintf(name, sizeof("dfsdm-pdm0"), "dfsdm-pdm%d", adc->fl_id);
1562 } else {
1563 iio->info = &stm32_dfsdm_info_adc;
1564 snprintf(name, sizeof("dfsdm-adc0"), "dfsdm-adc%d", adc->fl_id);
1565 }
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001566 iio->name = name;
1567
1568 /*
1569 * In a first step IRQs generated for channels are not treated.
1570 * So IRQ associated to filter instance 0 is dedicated to the Filter 0.
1571 */
1572 irq = platform_get_irq(pdev, 0);
Stephen Boyd7c279222019-07-30 11:15:19 -07001573 if (irq < 0)
Fabien Dessenne3e53ef92019-04-24 14:51:25 +02001574 return irq;
Fabien Dessenne3e53ef92019-04-24 14:51:25 +02001575
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001576 ret = devm_request_irq(dev, irq, stm32_dfsdm_irq,
Alexandru Ardelean07b6c9d2020-05-25 11:26:48 +03001577 0, pdev->name, iio);
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001578 if (ret < 0) {
1579 dev_err(dev, "Failed to request IRQ\n");
1580 return ret;
1581 }
1582
1583 ret = of_property_read_u32(dev->of_node, "st,filter-order", &val);
1584 if (ret < 0) {
1585 dev_err(dev, "Failed to set filter order\n");
1586 return ret;
1587 }
1588
1589 adc->dfsdm->fl_list[adc->fl_id].ford = val;
1590
1591 ret = of_property_read_u32(dev->of_node, "st,filter0-sync", &val);
1592 if (!ret)
1593 adc->dfsdm->fl_list[adc->fl_id].sync_mode = val;
1594
1595 adc->dev_data = dev_data;
Fabrice Gasnierb455d062020-04-30 11:28:46 +02001596 ret = dev_data->init(dev, iio);
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001597 if (ret < 0)
1598 return ret;
1599
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001600 ret = iio_device_register(iio);
1601 if (ret < 0)
1602 goto err_cleanup;
1603
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001604 if (dev_data->type == DFSDM_AUDIO) {
1605 ret = of_platform_populate(np, NULL, NULL, dev);
1606 if (ret < 0) {
1607 dev_err(dev, "Failed to find an audio DAI\n");
1608 goto err_unregister;
1609 }
1610 }
1611
1612 return 0;
1613
1614err_unregister:
1615 iio_device_unregister(iio);
1616err_cleanup:
1617 stm32_dfsdm_dma_release(iio);
1618
1619 return ret;
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001620}
1621
1622static int stm32_dfsdm_adc_remove(struct platform_device *pdev)
1623{
Alexandru Ardelean07b6c9d2020-05-25 11:26:48 +03001624 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
1625 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001626
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001627 if (adc->dev_data->type == DFSDM_AUDIO)
1628 of_platform_depopulate(&pdev->dev);
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001629 iio_device_unregister(indio_dev);
Arnaud Pouliqueneca94982018-01-10 11:13:12 +01001630 stm32_dfsdm_dma_release(indio_dev);
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001631
1632 return 0;
1633}
1634
Fabrice Gasnier6ec417d2019-03-25 15:49:28 +01001635static int __maybe_unused stm32_dfsdm_adc_suspend(struct device *dev)
1636{
Alexandru Ardelean07b6c9d2020-05-25 11:26:48 +03001637 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Fabrice Gasnier6ec417d2019-03-25 15:49:28 +01001638
1639 if (iio_buffer_enabled(indio_dev))
Lars-Peter Clausenf11d59d2020-05-25 14:38:53 +03001640 stm32_dfsdm_predisable(indio_dev);
Fabrice Gasnier6ec417d2019-03-25 15:49:28 +01001641
1642 return 0;
1643}
1644
1645static int __maybe_unused stm32_dfsdm_adc_resume(struct device *dev)
1646{
Alexandru Ardelean07b6c9d2020-05-25 11:26:48 +03001647 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1648 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
Fabrice Gasnier6ec417d2019-03-25 15:49:28 +01001649 const struct iio_chan_spec *chan;
1650 struct stm32_dfsdm_channel *ch;
1651 int i, ret;
1652
1653 /* restore channels configuration */
1654 for (i = 0; i < indio_dev->num_channels; i++) {
1655 chan = indio_dev->channels + i;
1656 ch = &adc->dfsdm->ch_list[chan->channel];
1657 ret = stm32_dfsdm_chan_configure(adc->dfsdm, ch);
1658 if (ret)
1659 return ret;
1660 }
1661
1662 if (iio_buffer_enabled(indio_dev))
Lars-Peter Clausenf11d59d2020-05-25 14:38:53 +03001663 stm32_dfsdm_postenable(indio_dev);
Fabrice Gasnier6ec417d2019-03-25 15:49:28 +01001664
1665 return 0;
1666}
1667
1668static SIMPLE_DEV_PM_OPS(stm32_dfsdm_adc_pm_ops,
1669 stm32_dfsdm_adc_suspend, stm32_dfsdm_adc_resume);
1670
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001671static struct platform_driver stm32_dfsdm_adc_driver = {
1672 .driver = {
1673 .name = "stm32-dfsdm-adc",
1674 .of_match_table = stm32_dfsdm_adc_match,
Fabrice Gasnier6ec417d2019-03-25 15:49:28 +01001675 .pm = &stm32_dfsdm_adc_pm_ops,
Arnaud Pouliquene2e67712018-01-10 11:13:11 +01001676 },
1677 .probe = stm32_dfsdm_adc_probe,
1678 .remove = stm32_dfsdm_adc_remove,
1679};
1680module_platform_driver(stm32_dfsdm_adc_driver);
1681
1682MODULE_DESCRIPTION("STM32 sigma delta ADC");
1683MODULE_AUTHOR("Arnaud Pouliquen <arnaud.pouliquen@st.com>");
1684MODULE_LICENSE("GPL v2");