mbox series

[v9,00/13] Add STM32 DFSDM support

Message ID 1513241885-32079-1-git-send-email-arnaud.pouliquen@st.com
Headers show
Series Add STM32 DFSDM support | expand

Message

Arnaud POULIQUEN Dec. 14, 2017, 8:57 a.m. UTC
Hello

New version to fix patch 10/13: IIO: ADC: add stm32 DFSDM support for PDM microphone

Main deltas V9 vs V8:
- Rework probe and remove order for audio configuration in stm32-dfsdm-adc.c.
- Optimization of the filling of the dma_slave_config struct.

Main deltas V8 vs V7:
- Few typos fixes.
- Function return optimizations in sound/soc/stm/stm32_adfsdm.c.

Main deltas V7 vs V6:
- Replaces the custom license information text with the appropriate 
  SPDX identifier.
- Few fixes in sound/soc/stm/stm32_adfsdm.c and stm32-dfsdm-core.c.
- Add missing #interrupt-cells in binding examples.
- Integrate last Jonathan's comments.

Main deltas V6 vs V5:
- Fix warning reported by kbuild test in :
   include/linux/iio/consumer.h
   sound/soc/stm/stm32_adfsdm.c

Main deltas V5 vs V4:
- Integrate ASOC DAI as a subnode of the DFSDM.
- Add in kernel consumer interface to allow to manipulate attribute.

Thanks,
Arnaud

Arnaud Pouliquen (12):
  docs: driver-api: add iio hw consumer section
  IIO: hw_consumer: add devm_iio_hw_consumer_alloc
  IIO: inkern: API for manipulating channel attributes
  IIO: Add DT bindings for sigma delta adc modulator
  IIO: ADC: add sigma delta modulator support
  IIO: add DT bindings for stm32 DFSDM filter
  IIO: ADC: add stm32 DFSDM core support
  IIO: ADC: add STM32 DFSDM sigma delta ADC support
  IIO: ADC: add stm32 DFSDM support for PDM microphone
  IIO: consumer: allow to set buffer sizes
  ASoC: add bindings for stm32 DFSDM filter
  ASoC: stm32: add DFSDM DAI support

Lars-Peter Clausen (1):
  iio: Add hardware consumer buffer support

 .../ABI/testing/sysfs-bus-iio-dfsdm-adc-stm32      |   16 +
 .../bindings/iio/adc/sigma-delta-modulator.txt     |   13 +
 .../bindings/iio/adc/st,stm32-dfsdm-adc.txt        |  128 +++
 .../devicetree/bindings/sound/st,stm32-adfsdm.txt  |   63 +
 Documentation/driver-api/iio/hw-consumer.rst       |   51 +
 Documentation/driver-api/iio/index.rst             |    1 +
 drivers/iio/adc/Kconfig                            |   37 +
 drivers/iio/adc/Makefile                           |    3 +
 drivers/iio/adc/sd_adc_modulator.c                 |   68 ++
 drivers/iio/adc/stm32-dfsdm-adc.c                  | 1216 ++++++++++++++++++++
 drivers/iio/adc/stm32-dfsdm-core.c                 |  309 +++++
 drivers/iio/adc/stm32-dfsdm.h                      |  310 +++++
 drivers/iio/buffer/Kconfig                         |   10 +
 drivers/iio/buffer/Makefile                        |    1 +
 drivers/iio/buffer/industrialio-buffer-cb.c        |   11 +
 drivers/iio/buffer/industrialio-hw-consumer.c      |  247 ++++
 drivers/iio/inkern.c                               |   17 +-
 include/linux/iio/adc/stm32-dfsdm-adc.h            |   18 +
 include/linux/iio/consumer.h                       |   37 +
 include/linux/iio/hw-consumer.h                    |   21 +
 include/linux/iio/iio.h                            |   28 -
 include/linux/iio/types.h                          |   28 +
 sound/soc/stm/Kconfig                              |   11 +
 sound/soc/stm/Makefile                             |    3 +
 sound/soc/stm/stm32_adfsdm.c                       |  347 ++++++
 25 files changed, 2961 insertions(+), 33 deletions(-)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-dfsdm-adc-stm32
 create mode 100644 Documentation/devicetree/bindings/iio/adc/sigma-delta-modulator.txt
 create mode 100644 Documentation/devicetree/bindings/iio/adc/st,stm32-dfsdm-adc.txt
 create mode 100644 Documentation/devicetree/bindings/sound/st,stm32-adfsdm.txt
 create mode 100644 Documentation/driver-api/iio/hw-consumer.rst
 create mode 100644 drivers/iio/adc/sd_adc_modulator.c
 create mode 100644 drivers/iio/adc/stm32-dfsdm-adc.c
 create mode 100644 drivers/iio/adc/stm32-dfsdm-core.c
 create mode 100644 drivers/iio/adc/stm32-dfsdm.h
 create mode 100644 drivers/iio/buffer/industrialio-hw-consumer.c
 create mode 100644 include/linux/iio/adc/stm32-dfsdm-adc.h
 create mode 100644 include/linux/iio/hw-consumer.h
 create mode 100644 sound/soc/stm/stm32_adfsdm.c

Comments

Jonathan Cameron Dec. 17, 2017, 12:54 p.m. UTC | #1
On Thu, 14 Dec 2017 09:58:02 +0100
Arnaud Pouliquen <arnaud.pouliquen@st.com> wrote:

> This code offers a way to handle PDM audio microphones in
> ASOC framework. Audio driver should use consumer API.
> A specific management is implemented for DMA, with a
> callback, to allows to handle audio buffers efficiently.
> 
> Signed-off-by: Arnaud Pouliquen <arnaud.pouliquen@st.com>

Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

Looks good ;)

> ---
> V8 to V9
> - Create stm32_dfsdm_dma_request
> - Replace devm_of_platform_populate by of_platform_populate.
> - Reorder probe and remove sequences.
> 
>  .../ABI/testing/sysfs-bus-iio-dfsdm-adc-stm32      |  16 +
>  drivers/iio/adc/stm32-dfsdm-adc.c                  | 502 ++++++++++++++++++++-
>  include/linux/iio/adc/stm32-dfsdm-adc.h            |  18 +
>  3 files changed, 529 insertions(+), 7 deletions(-)
>  create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-dfsdm-adc-stm32
>  create mode 100644 include/linux/iio/adc/stm32-dfsdm-adc.h
> 
> diff --git a/Documentation/ABI/testing/sysfs-bus-iio-dfsdm-adc-stm32 b/Documentation/ABI/testing/sysfs-bus-iio-dfsdm-adc-stm32
> new file mode 100644
> index 0000000..da98223
> --- /dev/null
> +++ b/Documentation/ABI/testing/sysfs-bus-iio-dfsdm-adc-stm32
> @@ -0,0 +1,16 @@
> +What:		/sys/bus/iio/devices/iio:deviceX/in_voltage_spi_clk_freq
> +KernelVersion:	4.14
> +Contact:	arnaud.pouliquen@st.com
> +Description:
> +		For audio purpose only.
> +		Used by audio driver to set/get the spi input frequency.
> +		This is mandatory if DFSDM is slave on SPI bus, to
> +		provide information on the SPI clock frequency during runtime
> +		Notice that the SPI frequency should be a multiple of sample
> +		frequency to ensure the precision.
> +		if DFSDM input is SPI master
> +			Reading  SPI clkout frequency,
> +			error on writing
> +		If DFSDM input is SPI Slave:
> +			Reading returns value previously set.
> +			Writing value before starting conversions.
> \ No newline at end of file
> diff --git a/drivers/iio/adc/stm32-dfsdm-adc.c b/drivers/iio/adc/stm32-dfsdm-adc.c
> index 68b5920..b03ca3f 100644
> --- a/drivers/iio/adc/stm32-dfsdm-adc.c
> +++ b/drivers/iio/adc/stm32-dfsdm-adc.c
> @@ -6,19 +6,23 @@
>   * Author: Arnaud Pouliquen <arnaud.pouliquen@st.com>.
>   */
>  
> +#include <linux/dmaengine.h>
> +#include <linux/dma-mapping.h>
>  #include <linux/interrupt.h>
>  #include <linux/iio/buffer.h>
>  #include <linux/iio/hw-consumer.h>
>  #include <linux/iio/iio.h>
>  #include <linux/iio/sysfs.h>
>  #include <linux/module.h>
> -#include <linux/of.h>
> +#include <linux/of_device.h>
>  #include <linux/platform_device.h>
>  #include <linux/regmap.h>
>  #include <linux/slab.h>
>  
>  #include "stm32-dfsdm.h"
>  
> +#define DFSDM_DMA_BUFFER_SIZE (4 * PAGE_SIZE)
> +
>  /* Conversion timeout */
>  #define DFSDM_TIMEOUT_US 100000
>  #define DFSDM_TIMEOUT (msecs_to_jiffies(DFSDM_TIMEOUT_US / 1000))
> @@ -58,6 +62,18 @@ struct stm32_dfsdm_adc {
>  	struct completion completion;
>  	u32 *buffer;
>  
> +	/* Audio specific */
> +	unsigned int spi_freq;  /* SPI bus clock frequency */
> +	unsigned int sample_freq; /* Sample frequency after filter decimation */
> +	int (*cb)(const void *data, size_t size, void *cb_priv);
> +	void *cb_priv;
> +
> +	/* DMA */
> +	u8 *rx_buf;
> +	unsigned int bufi; /* Buffer current position */
> +	unsigned int buf_sz; /* Buffer size */
> +	struct dma_chan	*dma_chan;
> +	dma_addr_t dma_buf;
>  };
>  
>  struct stm32_dfsdm_str2field {
> @@ -351,10 +367,63 @@ int stm32_dfsdm_channel_parse_of(struct stm32_dfsdm *dfsdm,
>  	return 0;
>  }
>  
> +static ssize_t dfsdm_adc_audio_get_spiclk(struct iio_dev *indio_dev,
> +					  uintptr_t priv,
> +					  const struct iio_chan_spec *chan,
> +					  char *buf)
> +{
> +	struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
> +
> +	return snprintf(buf, PAGE_SIZE, "%d\n", adc->spi_freq);
> +}
> +
> +static ssize_t dfsdm_adc_audio_set_spiclk(struct iio_dev *indio_dev,
> +					  uintptr_t priv,
> +					  const struct iio_chan_spec *chan,
> +					  const char *buf, size_t len)
> +{
> +	struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
> +	struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[adc->fl_id];
> +	struct stm32_dfsdm_channel *ch = &adc->dfsdm->ch_list[adc->ch_id];
> +	unsigned int sample_freq = adc->sample_freq;
> +	unsigned int spi_freq;
> +	int ret;
> +
> +	dev_err(&indio_dev->dev, "enter %s\n", __func__);
> +	/* If DFSDM is master on SPI, SPI freq can not be updated */
> +	if (ch->src != DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL)
> +		return -EPERM;
> +
> +	ret = kstrtoint(buf, 0, &spi_freq);
> +	if (ret)
> +		return ret;
> +
> +	if (!spi_freq)
> +		return -EINVAL;
> +
> +	if (sample_freq) {
> +		if (spi_freq % sample_freq)
> +			dev_warn(&indio_dev->dev,
> +				 "Sampling rate not accurate (%d)\n",
> +				 spi_freq / (spi_freq / sample_freq));
> +
> +		ret = stm32_dfsdm_set_osrs(fl, 0, (spi_freq / sample_freq));
> +		if (ret < 0) {
> +			dev_err(&indio_dev->dev,
> +				"No filter parameters that match!\n");
> +			return ret;
> +		}
> +	}
> +	adc->spi_freq = spi_freq;
> +
> +	return len;
> +}
> +
>  static int stm32_dfsdm_start_conv(struct stm32_dfsdm_adc *adc, bool dma)
>  {
>  	struct regmap *regmap = adc->dfsdm->regmap;
>  	int ret;
> +	unsigned int dma_en = 0, cont_en = 0;
>  
>  	ret = stm32_dfsdm_start_channel(adc->dfsdm, adc->ch_id);
>  	if (ret < 0)
> @@ -365,6 +434,24 @@ static int stm32_dfsdm_start_conv(struct stm32_dfsdm_adc *adc, bool dma)
>  	if (ret < 0)
>  		goto stop_channels;
>  
> +	if (dma) {
> +		/* Enable DMA transfer*/
> +		dma_en =  DFSDM_CR1_RDMAEN(1);
> +		/* Enable conversion triggered by SPI clock*/
> +		cont_en = DFSDM_CR1_RCONT(1);
> +	}
> +	/* Enable DMA transfer*/
> +	ret = regmap_update_bits(regmap, DFSDM_CR1(adc->fl_id),
> +				 DFSDM_CR1_RDMAEN_MASK, dma_en);
> +	if (ret < 0)
> +		goto stop_channels;
> +
> +	/* Enable conversion triggered by SPI clock*/
> +	ret = regmap_update_bits(regmap, DFSDM_CR1(adc->fl_id),
> +				 DFSDM_CR1_RCONT_MASK, cont_en);
> +	if (ret < 0)
> +		goto stop_channels;
> +
>  	ret = stm32_dfsdm_start_filter(adc->dfsdm, adc->fl_id);
>  	if (ret < 0)
>  		goto stop_channels;
> @@ -398,6 +485,231 @@ static void stm32_dfsdm_stop_conv(struct stm32_dfsdm_adc *adc)
>  	stm32_dfsdm_stop_channel(adc->dfsdm, adc->ch_id);
>  }
>  
> +static int stm32_dfsdm_set_watermark(struct iio_dev *indio_dev,
> +				     unsigned int val)
> +{
> +	struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
> +	unsigned int watermark = DFSDM_DMA_BUFFER_SIZE / 2;
> +
> +	/*
> +	 * DMA cyclic transfers are used, buffer is split into two periods.
> +	 * There should be :
> +	 * - always one buffer (period) DMA is working on
> +	 * - one buffer (period) driver pushed to ASoC side.
> +	 */
> +	watermark = min(watermark, val * (unsigned int)(sizeof(u32)));
> +	adc->buf_sz = watermark * 2;
> +
> +	return 0;
> +}
> +
> +static unsigned int stm32_dfsdm_adc_dma_residue(struct stm32_dfsdm_adc *adc)
> +{
> +	struct dma_tx_state state;
> +	enum dma_status status;
> +
> +	status = dmaengine_tx_status(adc->dma_chan,
> +				     adc->dma_chan->cookie,
> +				     &state);
> +	if (status == DMA_IN_PROGRESS) {
> +		/* Residue is size in bytes from end of buffer */
> +		unsigned int i = adc->buf_sz - state.residue;
> +		unsigned int size;
> +
> +		/* Return available bytes */
> +		if (i >= adc->bufi)
> +			size = i - adc->bufi;
> +		else
> +			size = adc->buf_sz + i - adc->bufi;
> +
> +		return size;
> +	}
> +
> +	return 0;
> +}
> +
> +static void stm32_dfsdm_audio_dma_buffer_done(void *data)
> +{
> +	struct iio_dev *indio_dev = data;
> +	struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
> +	int available = stm32_dfsdm_adc_dma_residue(adc);
> +	size_t old_pos;
> +
> +	/*
> +	 * FIXME: In Kernel interface does not support cyclic DMA buffer,and
> +	 * offers only an interface to push data samples per samples.
> +	 * For this reason IIO buffer interface is not used and interface is
> +	 * bypassed using a private callback registered by ASoC.
> +	 * This should be a temporary solution waiting a cyclic DMA engine
> +	 * support in IIO.
> +	 */
> +
> +	dev_dbg(&indio_dev->dev, "%s: pos = %d, available = %d\n", __func__,
> +		adc->bufi, available);
> +	old_pos = adc->bufi;
> +
> +	while (available >= indio_dev->scan_bytes) {
> +		u32 *buffer = (u32 *)&adc->rx_buf[adc->bufi];
> +
> +		/* Mask 8 LSB that contains the channel ID */
> +		*buffer = (*buffer & 0xFFFFFF00) << 8;
> +		available -= indio_dev->scan_bytes;
> +		adc->bufi += indio_dev->scan_bytes;
> +		if (adc->bufi >= adc->buf_sz) {
> +			if (adc->cb)
> +				adc->cb(&adc->rx_buf[old_pos],
> +					 adc->buf_sz - old_pos, adc->cb_priv);
> +			adc->bufi = 0;
> +			old_pos = 0;
> +		}
> +	}
> +	if (adc->cb)
> +		adc->cb(&adc->rx_buf[old_pos], adc->bufi - old_pos,
> +			adc->cb_priv);
> +}
> +
> +static int stm32_dfsdm_adc_dma_start(struct iio_dev *indio_dev)
> +{
> +	struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
> +	struct dma_async_tx_descriptor *desc;
> +	dma_cookie_t cookie;
> +	int ret;
> +
> +	if (!adc->dma_chan)
> +		return -EINVAL;
> +
> +	dev_dbg(&indio_dev->dev, "%s size=%d watermark=%d\n", __func__,
> +		adc->buf_sz, adc->buf_sz / 2);
> +
> +	/* Prepare a DMA cyclic transaction */
> +	desc = dmaengine_prep_dma_cyclic(adc->dma_chan,
> +					 adc->dma_buf,
> +					 adc->buf_sz, adc->buf_sz / 2,
> +					 DMA_DEV_TO_MEM,
> +					 DMA_PREP_INTERRUPT);
> +	if (!desc)
> +		return -EBUSY;
> +
> +	desc->callback = stm32_dfsdm_audio_dma_buffer_done;
> +	desc->callback_param = indio_dev;
> +
> +	cookie = dmaengine_submit(desc);
> +	ret = dma_submit_error(cookie);
> +	if (ret) {
> +		dmaengine_terminate_all(adc->dma_chan);
> +		return ret;
> +	}
> +
> +	/* Issue pending DMA requests */
> +	dma_async_issue_pending(adc->dma_chan);
> +
> +	return 0;
> +}
> +
> +static int stm32_dfsdm_postenable(struct iio_dev *indio_dev)
> +{
> +	struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
> +	int ret;
> +
> +	/* Reset adc buffer index */
> +	adc->bufi = 0;
> +
> +	ret = stm32_dfsdm_start_dfsdm(adc->dfsdm);
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = stm32_dfsdm_start_conv(adc, true);
> +	if (ret) {
> +		dev_err(&indio_dev->dev, "Can't start conversion\n");
> +		goto stop_dfsdm;
> +	}
> +
> +	if (adc->dma_chan) {
> +		ret = stm32_dfsdm_adc_dma_start(indio_dev);
> +		if (ret) {
> +			dev_err(&indio_dev->dev, "Can't start DMA\n");
> +			goto err_stop_conv;
> +		}
> +	}
> +
> +	return 0;
> +
> +err_stop_conv:
> +	stm32_dfsdm_stop_conv(adc);
> +stop_dfsdm:
> +	stm32_dfsdm_stop_dfsdm(adc->dfsdm);
> +
> +	return ret;
> +}
> +
> +static int stm32_dfsdm_predisable(struct iio_dev *indio_dev)
> +{
> +	struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
> +
> +	if (adc->dma_chan)
> +		dmaengine_terminate_all(adc->dma_chan);
> +
> +	stm32_dfsdm_stop_conv(adc);
> +
> +	stm32_dfsdm_stop_dfsdm(adc->dfsdm);
> +
> +	return 0;
> +}
> +
> +static const struct iio_buffer_setup_ops stm32_dfsdm_buffer_setup_ops = {
> +	.postenable = &stm32_dfsdm_postenable,
> +	.predisable = &stm32_dfsdm_predisable,
> +};
> +
> +/**
> + * stm32_dfsdm_get_buff_cb() - register a callback that will be called when
> + *                             DMA transfer period is achieved.
> + *
> + * @iio_dev: Handle to IIO device.
> + * @cb: Pointer to callback function:
> + *      - data: pointer to data buffer
> + *      - size: size in byte of the data buffer
> + *      - private: pointer to consumer private structure.
> + * @private: Pointer to consumer private structure.
> + */
> +int stm32_dfsdm_get_buff_cb(struct iio_dev *iio_dev,
> +			    int (*cb)(const void *data, size_t size,
> +				      void *private),
> +			    void *private)
> +{
> +	struct stm32_dfsdm_adc *adc;
> +
> +	if (!iio_dev)
> +		return -EINVAL;
> +	adc = iio_priv(iio_dev);
> +
> +	adc->cb = cb;
> +	adc->cb_priv = private;
> +
> +	return 0;
> +}
> +EXPORT_SYMBOL_GPL(stm32_dfsdm_get_buff_cb);
> +
> +/**
> + * stm32_dfsdm_release_buff_cb - unregister buffer callback
> + *
> + * @iio_dev: Handle to IIO device.
> + */
> +int stm32_dfsdm_release_buff_cb(struct iio_dev *iio_dev)
> +{
> +	struct stm32_dfsdm_adc *adc;
> +
> +	if (!iio_dev)
> +		return -EINVAL;
> +	adc = iio_priv(iio_dev);
> +
> +	adc->cb = NULL;
> +	adc->cb_priv = NULL;
> +
> +	return 0;
> +}
> +EXPORT_SYMBOL_GPL(stm32_dfsdm_release_buff_cb);
> +
>  static int stm32_dfsdm_single_conv(struct iio_dev *indio_dev,
>  				   const struct iio_chan_spec *chan, int *res)
>  {
> @@ -453,15 +765,41 @@ static int stm32_dfsdm_write_raw(struct iio_dev *indio_dev,
>  {
>  	struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
>  	struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[adc->fl_id];
> +	struct stm32_dfsdm_channel *ch = &adc->dfsdm->ch_list[adc->ch_id];
> +	unsigned int spi_freq = adc->spi_freq;
>  	int ret = -EINVAL;
>  
> -	if (mask == IIO_CHAN_INFO_OVERSAMPLING_RATIO) {
> +	switch (mask) {
> +	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
>  		ret = stm32_dfsdm_set_osrs(fl, 0, val);
>  		if (!ret)
>  			adc->oversamp = val;
> +
> +		return ret;
> +
> +	case IIO_CHAN_INFO_SAMP_FREQ:
> +		if (!val)
> +			return -EINVAL;
> +		if (ch->src != DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL)
> +			spi_freq = adc->dfsdm->spi_master_freq;
> +
> +		if (spi_freq % val)
> +			dev_warn(&indio_dev->dev,
> +				 "Sampling rate not accurate (%d)\n",
> +				 spi_freq / (spi_freq / val));
> +
> +		ret = stm32_dfsdm_set_osrs(fl, 0, (spi_freq / val));
> +		if (ret < 0) {
> +			dev_err(&indio_dev->dev,
> +				"Not able to find parameter that match!\n");
> +			return ret;
> +		}
> +		adc->sample_freq = val;
> +
> +		return 0;
>  	}
>  
> -	return ret;
> +	return -EINVAL;
>  }
>  
>  static int stm32_dfsdm_read_raw(struct iio_dev *indio_dev,
> @@ -494,11 +832,22 @@ static int stm32_dfsdm_read_raw(struct iio_dev *indio_dev,
>  		*val = adc->oversamp;
>  
>  		return IIO_VAL_INT;
> +
> +	case IIO_CHAN_INFO_SAMP_FREQ:
> +		*val = adc->sample_freq;
> +
> +		return IIO_VAL_INT;
>  	}
>  
>  	return -EINVAL;
>  }
>  
> +static const struct iio_info stm32_dfsdm_info_audio = {
> +	.hwfifo_set_watermark = stm32_dfsdm_set_watermark,
> +	.read_raw = stm32_dfsdm_read_raw,
> +	.write_raw = stm32_dfsdm_write_raw,
> +};
> +
>  static const struct iio_info stm32_dfsdm_info_adc = {
>  	.read_raw = stm32_dfsdm_read_raw,
>  	.write_raw = stm32_dfsdm_write_raw,
> @@ -531,6 +880,70 @@ static irqreturn_t stm32_dfsdm_irq(int irq, void *arg)
>  	return IRQ_HANDLED;
>  }
>  
> +/*
> + * Define external info for SPI Frequency and audio sampling rate that can be
> + * configured by ASoC driver through consumer.h API
> + */
> +static const struct iio_chan_spec_ext_info dfsdm_adc_audio_ext_info[] = {
> +	/* spi_clk_freq : clock freq on SPI/manchester bus used by channel */
> +	{
> +		.name = "spi_clk_freq",
> +		.shared = IIO_SHARED_BY_TYPE,
> +		.read = dfsdm_adc_audio_get_spiclk,
> +		.write = dfsdm_adc_audio_set_spiclk,
> +	},
> +	{},
> +};
> +
> +static void stm32_dfsdm_dma_release(struct iio_dev *indio_dev)
> +{
> +	struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
> +
> +	if (adc->dma_chan) {
> +		dma_free_coherent(adc->dma_chan->device->dev,
> +				  DFSDM_DMA_BUFFER_SIZE,
> +				  adc->rx_buf, adc->dma_buf);
> +		dma_release_channel(adc->dma_chan);
> +	}
> +}
> +
> +static int stm32_dfsdm_dma_request(struct iio_dev *indio_dev)
> +{
> +	struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
> +	struct dma_slave_config config = {
> +		.src_addr = (dma_addr_t)adc->dfsdm->phys_base +
> +			DFSDM_RDATAR(adc->fl_id),
> +		.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
> +	};
> +	int ret;
> +
> +	adc->dma_chan = dma_request_slave_channel(&indio_dev->dev, "rx");
> +	if (!adc->dma_chan)
> +		return -EINVAL;
> +
> +	adc->rx_buf = dma_alloc_coherent(adc->dma_chan->device->dev,
> +					 DFSDM_DMA_BUFFER_SIZE,
> +					 &adc->dma_buf, GFP_KERNEL);
> +	if (!adc->rx_buf) {
> +		ret = -ENOMEM;
> +		goto err_release;
> +	}
> +
> +	ret = dmaengine_slave_config(adc->dma_chan, &config);
> +	if (ret)
> +		goto err_free;
> +
> +	return 0;
> +
> +err_free:
> +	dma_free_coherent(adc->dma_chan->device->dev, DFSDM_DMA_BUFFER_SIZE,
> +			  adc->rx_buf, adc->dma_buf);
> +err_release:
> +	dma_release_channel(adc->dma_chan);
> +
> +	return ret;
> +}
> +
>  static int stm32_dfsdm_adc_chan_init_one(struct iio_dev *indio_dev,
>  					 struct iio_chan_spec *ch)
>  {
> @@ -551,7 +964,12 @@ static int stm32_dfsdm_adc_chan_init_one(struct iio_dev *indio_dev,
>  	ch->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
>  	ch->info_mask_shared_by_all = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO);
>  
> -	ch->scan_type.sign = 'u';
> +	if (adc->dev_data->type == DFSDM_AUDIO) {
> +		ch->scan_type.sign = 's';
> +		ch->ext_info = dfsdm_adc_audio_ext_info;
> +	} else {
> +		ch->scan_type.sign = 'u';
> +	}
>  	ch->scan_type.realbits = 24;
>  	ch->scan_type.storagebits = 32;
>  	adc->ch_id = ch->channel;
> @@ -560,6 +978,39 @@ static int stm32_dfsdm_adc_chan_init_one(struct iio_dev *indio_dev,
>  					  &adc->dfsdm->ch_list[ch->channel]);
>  }
>  
> +static int stm32_dfsdm_audio_init(struct iio_dev *indio_dev)
> +{
> +	struct iio_chan_spec *ch;
> +	struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
> +	struct stm32_dfsdm_channel *d_ch;
> +	int ret;
> +
> +	indio_dev->modes |= INDIO_BUFFER_SOFTWARE;
> +	indio_dev->setup_ops = &stm32_dfsdm_buffer_setup_ops;
> +
> +	ch = devm_kzalloc(&indio_dev->dev, sizeof(*ch), GFP_KERNEL);
> +	if (!ch)
> +		return -ENOMEM;
> +
> +	ch->scan_index = 0;
> +
> +	ret = stm32_dfsdm_adc_chan_init_one(indio_dev, ch);
> +	if (ret < 0) {
> +		dev_err(&indio_dev->dev, "Channels init failed\n");
> +		return ret;
> +	}
> +	ch->info_mask_separate = BIT(IIO_CHAN_INFO_SAMP_FREQ);
> +
> +	d_ch = &adc->dfsdm->ch_list[adc->ch_id];
> +	if (d_ch->src != DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL)
> +		adc->spi_freq = adc->dfsdm->spi_master_freq;
> +
> +	indio_dev->num_channels = 1;
> +	indio_dev->channels = ch;
> +
> +	return stm32_dfsdm_dma_request(indio_dev);
> +}
> +
>  static int stm32_dfsdm_adc_init(struct iio_dev *indio_dev)
>  {
>  	struct iio_chan_spec *ch;
> @@ -612,11 +1063,20 @@ static const struct stm32_dfsdm_dev_data stm32h7_dfsdm_adc_data = {
>  	.init = stm32_dfsdm_adc_init,
>  };
>  
> +static const struct stm32_dfsdm_dev_data stm32h7_dfsdm_audio_data = {
> +	.type = DFSDM_AUDIO,
> +	.init = stm32_dfsdm_audio_init,
> +};
> +
>  static const struct of_device_id stm32_dfsdm_adc_match[] = {
>  	{
>  		.compatible = "st,stm32-dfsdm-adc",
>  		.data = &stm32h7_dfsdm_adc_data,
>  	},
> +	{
> +		.compatible = "st,stm32-dfsdm-dmic",
> +		.data = &stm32h7_dfsdm_audio_data,
> +	},
>  	{}
>  };
>  
> @@ -667,8 +1127,13 @@ static int stm32_dfsdm_adc_probe(struct platform_device *pdev)
>  	name = devm_kzalloc(dev, sizeof("dfsdm-adc0"), GFP_KERNEL);
>  	if (!name)
>  		return -ENOMEM;
> -	iio->info = &stm32_dfsdm_info_adc;
> -	snprintf(name, sizeof("dfsdm-adc0"), "dfsdm-adc%d", adc->fl_id);
> +	if (dev_data->type == DFSDM_AUDIO) {
> +		iio->info = &stm32_dfsdm_info_audio;
> +		snprintf(name, sizeof("dfsdm-pdm0"), "dfsdm-pdm%d", adc->fl_id);
> +	} else {
> +		iio->info = &stm32_dfsdm_info_adc;
> +		snprintf(name, sizeof("dfsdm-adc0"), "dfsdm-adc%d", adc->fl_id);
> +	}
>  	iio->name = name;
>  
>  	/*
> @@ -700,7 +1165,27 @@ static int stm32_dfsdm_adc_probe(struct platform_device *pdev)
>  	if (ret < 0)
>  		return ret;
>  
> -	return iio_device_register(iio);
> +	ret = iio_device_register(iio);
> +	if (ret < 0)
> +		goto err_cleanup;
> +
> +	dev_err(dev, "of_platform_populate\n");
> +	if (dev_data->type == DFSDM_AUDIO) {
> +		ret = of_platform_populate(np, NULL, NULL, dev);
> +		if (ret < 0) {
> +			dev_err(dev, "Failed to find an audio DAI\n");
> +			goto err_unregister;
> +		}
> +	}
> +
> +	return 0;
> +
> +err_unregister:
> +	iio_device_unregister(iio);
> +err_cleanup:
> +	stm32_dfsdm_dma_release(iio);
> +
> +	return ret;
>  }
>  
>  static int stm32_dfsdm_adc_remove(struct platform_device *pdev)
> @@ -708,7 +1193,10 @@ static int stm32_dfsdm_adc_remove(struct platform_device *pdev)
>  	struct stm32_dfsdm_adc *adc = platform_get_drvdata(pdev);
>  	struct iio_dev *indio_dev = iio_priv_to_dev(adc);
>  
> +	if (adc->dev_data->type == DFSDM_AUDIO)
> +		of_platform_depopulate(&pdev->dev);
>  	iio_device_unregister(indio_dev);
> +	stm32_dfsdm_dma_release(indio_dev);
>  
>  	return 0;
>  }
> diff --git a/include/linux/iio/adc/stm32-dfsdm-adc.h b/include/linux/iio/adc/stm32-dfsdm-adc.h
> new file mode 100644
> index 0000000..e7dc7a5
> --- /dev/null
> +++ b/include/linux/iio/adc/stm32-dfsdm-adc.h
> @@ -0,0 +1,18 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +/*
> + * This file discribe the STM32 DFSDM IIO driver API for audio part
> + *
> + * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
> + * Author(s): Arnaud Pouliquen <arnaud.pouliquen@st.com>.
> + */
> +
> +#ifndef STM32_DFSDM_ADC_H
> +#define STM32_DFSDM_ADC_H
> +
> +int stm32_dfsdm_get_buff_cb(struct iio_dev *iio_dev,
> +			    int (*cb)(const void *data, size_t size,
> +				      void *private),
> +			    void *private);
> +int stm32_dfsdm_release_buff_cb(struct iio_dev *iio_dev);
> +
> +#endif

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Jonathan Cameron Dec. 17, 2017, 12:59 p.m. UTC | #2
On Thu, 14 Dec 2017 09:58:05 +0100
Arnaud Pouliquen <arnaud.pouliquen@st.com> wrote:

> Add driver to handle DAI interface for PDM microphones connected
> to Digital Filter for Sigma Delta Modulators IP.
> 
> Signed-off-by: Arnaud Pouliquen <arnaud.pouliquen@st.com>

For the IIO parts (I'm not knowledgeable enough about the sound side to
offer a proper review!)
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

So Mark, over to you.  I'm happy if you want to do the immutable branch
once any issue you find are fixed, or I can do it.  I'm just looking
for acks or a pull request now.

Jonathan

> ---
>  sound/soc/stm/Kconfig        |  11 ++
>  sound/soc/stm/Makefile       |   3 +
>  sound/soc/stm/stm32_adfsdm.c | 347 +++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 361 insertions(+)
>  create mode 100644 sound/soc/stm/stm32_adfsdm.c
> 
> diff --git a/sound/soc/stm/Kconfig b/sound/soc/stm/Kconfig
> index 3398e6c..a78f770 100644
> --- a/sound/soc/stm/Kconfig
> +++ b/sound/soc/stm/Kconfig
> @@ -28,4 +28,15 @@ config SND_SOC_STM32_SPDIFRX
>  	help
>  	  Say Y if you want to enable S/PDIF capture for STM32
>  
> +config SND_SOC_STM32_DFSDM
> +	tristate "SoC Audio support for STM32 DFSDM"
> +	depends on (ARCH_STM32 && OF && STM32_DFSDM_ADC) || COMPILE_TEST
> +	depends on SND_SOC
> +	select SND_SOC_GENERIC_DMAENGINE_PCM
> +	select SND_SOC_DMIC
> +	select IIO_BUFFER_CB
> +	help
> +	  Select this option to enable the STM32 Digital Filter
> +	  for Sigma Delta Modulators (DFSDM) driver used
> +	  in various STM32 series for digital microphone capture.
>  endmenu
> diff --git a/sound/soc/stm/Makefile b/sound/soc/stm/Makefile
> index 4ed22e6..53e90e6 100644
> --- a/sound/soc/stm/Makefile
> +++ b/sound/soc/stm/Makefile
> @@ -12,3 +12,6 @@ obj-$(CONFIG_SND_SOC_STM32_I2S) += snd-soc-stm32-i2s.o
>  # SPDIFRX
>  snd-soc-stm32-spdifrx-objs := stm32_spdifrx.o
>  obj-$(CONFIG_SND_SOC_STM32_SPDIFRX) += snd-soc-stm32-spdifrx.o
> +
> +#DFSDM
> +obj-$(CONFIG_SND_SOC_STM32_DFSDM) += stm32_adfsdm.o
> diff --git a/sound/soc/stm/stm32_adfsdm.c b/sound/soc/stm/stm32_adfsdm.c
> new file mode 100644
> index 0000000..af50891
> --- /dev/null
> +++ b/sound/soc/stm/stm32_adfsdm.c
> @@ -0,0 +1,347 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * This file is part of STM32 DFSDM ASoC DAI driver
> + *
> + * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
> + * Authors: Arnaud Pouliquen <arnaud.pouliquen@st.com>
> + *          Olivier Moysan <olivier.moysan@st.com>
> + */
> +
> +#include <linux/clk.h>
> +#include <linux/module.h>
> +#include <linux/platform_device.h>
> +#include <linux/slab.h>
> +
> +#include <linux/iio/iio.h>
> +#include <linux/iio/consumer.h>
> +#include <linux/iio/adc/stm32-dfsdm-adc.h>
> +
> +#include <sound/pcm.h>
> +#include <sound/soc.h>
> +
> +#define STM32_ADFSDM_DRV_NAME "stm32-adfsdm"
> +
> +#define DFSDM_MAX_PERIOD_SIZE	(PAGE_SIZE / 2)
> +#define DFSDM_MAX_PERIODS	6
> +
> +struct stm32_adfsdm_priv {
> +	struct snd_soc_dai_driver dai_drv;
> +	struct snd_pcm_substream *substream;
> +	struct device *dev;
> +
> +	/* IIO */
> +	struct iio_channel *iio_ch;
> +	struct iio_cb_buffer *iio_cb;
> +	bool iio_active;
> +
> +	/* PCM buffer */
> +	unsigned char *pcm_buff;
> +	unsigned int pos;
> +};
> +
> +static const struct snd_pcm_hardware stm32_adfsdm_pcm_hw = {
> +	.info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
> +	    SNDRV_PCM_INFO_PAUSE,
> +	.formats = SNDRV_PCM_FMTBIT_S32_LE,
> +
> +	.rate_min = 8000,
> +	.rate_max = 32000,
> +
> +	.channels_min = 1,
> +	.channels_max = 1,
> +
> +	.periods_min = 2,
> +	.periods_max = DFSDM_MAX_PERIODS,
> +
> +	.period_bytes_max = DFSDM_MAX_PERIOD_SIZE,
> +	.buffer_bytes_max = DFSDM_MAX_PERIODS * DFSDM_MAX_PERIOD_SIZE
> +};
> +
> +static void stm32_adfsdm_shutdown(struct snd_pcm_substream *substream,
> +				  struct snd_soc_dai *dai)
> +{
> +	struct stm32_adfsdm_priv *priv = snd_soc_dai_get_drvdata(dai);
> +
> +	if (priv->iio_active) {
> +		iio_channel_stop_all_cb(priv->iio_cb);
> +		priv->iio_active = false;
> +	}
> +}
> +
> +static int stm32_adfsdm_dai_prepare(struct snd_pcm_substream *substream,
> +				    struct snd_soc_dai *dai)
> +{
> +	struct stm32_adfsdm_priv *priv = snd_soc_dai_get_drvdata(dai);
> +	int ret;
> +
> +	ret = iio_write_channel_attribute(priv->iio_ch,
> +					  substream->runtime->rate, 0,
> +					  IIO_CHAN_INFO_SAMP_FREQ);
> +	if (ret < 0) {
> +		dev_err(dai->dev, "%s: Failed to set %d sampling rate\n",
> +			__func__, substream->runtime->rate);
> +		return ret;
> +	}
> +
> +	if (!priv->iio_active) {
> +		ret = iio_channel_start_all_cb(priv->iio_cb);
> +		if (!ret)
> +			priv->iio_active = true;
> +		else
> +			dev_err(dai->dev, "%s: IIO channel start failed (%d)\n",
> +				__func__, ret);
> +	}
> +
> +	return ret;
> +}
> +
> +static int stm32_adfsdm_set_sysclk(struct snd_soc_dai *dai, int clk_id,
> +				   unsigned int freq, int dir)
> +{
> +	struct stm32_adfsdm_priv *priv = snd_soc_dai_get_drvdata(dai);
> +	ssize_t size;
> +	char str_freq[10];
> +
> +	dev_dbg(dai->dev, "%s: Enter for freq %d\n", __func__, freq);
> +
> +	/* Set IIO frequency if CODEC is master as clock comes from SPI_IN */
> +
> +	snprintf(str_freq, sizeof(str_freq), "%d\n", freq);
> +	size = iio_write_channel_ext_info(priv->iio_ch, "spi_clk_freq",
> +					  str_freq, sizeof(str_freq));
> +	if (size != sizeof(str_freq)) {
> +		dev_err(dai->dev, "%s: Failed to set SPI clock\n",
> +			__func__);
> +		return -EINVAL;
> +	}
> +	return 0;
> +}
> +
> +static const struct snd_soc_dai_ops stm32_adfsdm_dai_ops = {
> +	.shutdown = stm32_adfsdm_shutdown,
> +	.prepare = stm32_adfsdm_dai_prepare,
> +	.set_sysclk = stm32_adfsdm_set_sysclk,
> +};
> +
> +static const struct snd_soc_dai_driver stm32_adfsdm_dai = {
> +	.capture = {
> +		    .channels_min = 1,
> +		    .channels_max = 1,
> +		    .formats = SNDRV_PCM_FMTBIT_S32_LE,
> +		    .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
> +			      SNDRV_PCM_RATE_32000),
> +		    },
> +	.ops = &stm32_adfsdm_dai_ops,
> +};
> +
> +static const struct snd_soc_component_driver stm32_adfsdm_dai_component = {
> +	.name = "stm32_dfsdm_audio",
> +};
> +
> +static int stm32_afsdm_pcm_cb(const void *data, size_t size, void *private)
> +{
> +	struct stm32_adfsdm_priv *priv = private;
> +	struct snd_soc_pcm_runtime *rtd = priv->substream->private_data;
> +	u8 *pcm_buff = priv->pcm_buff;
> +	u8 *src_buff = (u8 *)data;
> +	unsigned int buff_size = snd_pcm_lib_buffer_bytes(priv->substream);
> +	unsigned int period_size = snd_pcm_lib_period_bytes(priv->substream);
> +	unsigned int old_pos = priv->pos;
> +	unsigned int cur_size = size;
> +
> +	dev_dbg(rtd->dev, "%s: buff_add :%p, pos = %d, size = %zu\n",
> +		__func__, &pcm_buff[priv->pos], priv->pos, size);
> +
> +	if ((priv->pos + size) > buff_size) {
> +		memcpy(&pcm_buff[priv->pos], src_buff, buff_size - priv->pos);
> +		cur_size -= buff_size - priv->pos;
> +		priv->pos = 0;
> +	}
> +
> +	memcpy(&pcm_buff[priv->pos], &src_buff[size - cur_size], cur_size);
> +	priv->pos = (priv->pos + cur_size) % buff_size;
> +
> +	if (cur_size != size || (old_pos && (old_pos % period_size < size)))
> +		snd_pcm_period_elapsed(priv->substream);
> +
> +	return 0;
> +}
> +
> +static int stm32_adfsdm_trigger(struct snd_pcm_substream *substream, int cmd)
> +{
> +	struct snd_soc_pcm_runtime *rtd = substream->private_data;
> +	struct stm32_adfsdm_priv *priv =
> +		snd_soc_dai_get_drvdata(rtd->cpu_dai);
> +
> +	switch (cmd) {
> +	case SNDRV_PCM_TRIGGER_START:
> +	case SNDRV_PCM_TRIGGER_RESUME:
> +		priv->pos = 0;
> +		return stm32_dfsdm_get_buff_cb(priv->iio_ch->indio_dev,
> +					       stm32_afsdm_pcm_cb, priv);
> +	case SNDRV_PCM_TRIGGER_SUSPEND:
> +	case SNDRV_PCM_TRIGGER_STOP:
> +		return stm32_dfsdm_release_buff_cb(priv->iio_ch->indio_dev);
> +	}
> +
> +	return -EINVAL;
> +}
> +
> +static int stm32_adfsdm_pcm_open(struct snd_pcm_substream *substream)
> +{
> +	struct snd_soc_pcm_runtime *rtd = substream->private_data;
> +	struct stm32_adfsdm_priv *priv = snd_soc_dai_get_drvdata(rtd->cpu_dai);
> +	int ret;
> +
> +	ret =  snd_soc_set_runtime_hwparams(substream, &stm32_adfsdm_pcm_hw);
> +	if (!ret)
> +		priv->substream = substream;
> +
> +	return ret;
> +}
> +
> +static int stm32_adfsdm_pcm_close(struct snd_pcm_substream *substream)
> +{
> +	struct snd_soc_pcm_runtime *rtd = substream->private_data;
> +	struct stm32_adfsdm_priv *priv =
> +		snd_soc_dai_get_drvdata(rtd->cpu_dai);
> +
> +	snd_pcm_lib_free_pages(substream);
> +	priv->substream = NULL;
> +
> +	return 0;
> +}
> +
> +static snd_pcm_uframes_t stm32_adfsdm_pcm_pointer(
> +					    struct snd_pcm_substream *substream)
> +{
> +	struct snd_soc_pcm_runtime *rtd = substream->private_data;
> +	struct stm32_adfsdm_priv *priv =
> +		snd_soc_dai_get_drvdata(rtd->cpu_dai);
> +
> +	return bytes_to_frames(substream->runtime, priv->pos);
> +}
> +
> +static int stm32_adfsdm_pcm_hw_params(struct snd_pcm_substream *substream,
> +				      struct snd_pcm_hw_params *params)
> +{
> +	struct snd_soc_pcm_runtime *rtd = substream->private_data;
> +	struct stm32_adfsdm_priv *priv =
> +		snd_soc_dai_get_drvdata(rtd->cpu_dai);
> +	int ret;
> +
> +	ret =  snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
> +	if (ret < 0)
> +		return ret;
> +	priv->pcm_buff = substream->runtime->dma_area;
> +
> +	return iio_channel_cb_set_buffer_watermark(priv->iio_cb,
> +						   params_period_size(params));
> +}
> +
> +static int stm32_adfsdm_pcm_hw_free(struct snd_pcm_substream *substream)
> +{
> +	snd_pcm_lib_free_pages(substream);
> +
> +	return 0;
> +}
> +
> +static struct snd_pcm_ops stm32_adfsdm_pcm_ops = {
> +	.open		= stm32_adfsdm_pcm_open,
> +	.close		= stm32_adfsdm_pcm_close,
> +	.hw_params	= stm32_adfsdm_pcm_hw_params,
> +	.hw_free	= stm32_adfsdm_pcm_hw_free,
> +	.trigger	= stm32_adfsdm_trigger,
> +	.pointer	= stm32_adfsdm_pcm_pointer,
> +};
> +
> +static int stm32_adfsdm_pcm_new(struct snd_soc_pcm_runtime *rtd)
> +{
> +	struct snd_pcm *pcm = rtd->pcm;
> +	struct stm32_adfsdm_priv *priv =
> +		snd_soc_dai_get_drvdata(rtd->cpu_dai);
> +	unsigned int size = DFSDM_MAX_PERIODS * DFSDM_MAX_PERIOD_SIZE;
> +
> +	return snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
> +						     priv->dev, size, size);
> +}
> +
> +static void stm32_adfsdm_pcm_free(struct snd_pcm *pcm)
> +{
> +	struct snd_pcm_substream *substream;
> +	struct snd_soc_pcm_runtime *rtd;
> +	struct stm32_adfsdm_priv *priv;
> +
> +	substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
> +	if (substream) {
> +		rtd = substream->private_data;
> +		priv = snd_soc_dai_get_drvdata(rtd->cpu_dai);
> +
> +		snd_pcm_lib_preallocate_free_for_all(pcm);
> +	}
> +}
> +
> +static struct snd_soc_platform_driver stm32_adfsdm_soc_platform = {
> +	.ops		= &stm32_adfsdm_pcm_ops,
> +	.pcm_new	= stm32_adfsdm_pcm_new,
> +	.pcm_free	= stm32_adfsdm_pcm_free,
> +};
> +
> +static const struct of_device_id stm32_adfsdm_of_match[] = {
> +	{.compatible = "st,stm32h7-dfsdm-dai"},
> +	{}
> +};
> +MODULE_DEVICE_TABLE(of, stm32_adfsdm_of_match);
> +
> +static int stm32_adfsdm_probe(struct platform_device *pdev)
> +{
> +	struct stm32_adfsdm_priv *priv;
> +	int ret;
> +
> +	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
> +	if (!priv)
> +		return -ENOMEM;
> +
> +	priv->dev = &pdev->dev;
> +	priv->dai_drv = stm32_adfsdm_dai;
> +
> +	dev_set_drvdata(&pdev->dev, priv);
> +
> +	ret = devm_snd_soc_register_component(&pdev->dev,
> +					      &stm32_adfsdm_dai_component,
> +					      &priv->dai_drv, 1);
> +	if (ret < 0)
> +		return ret;
> +
> +	/* Associate iio channel */
> +	priv->iio_ch  = devm_iio_channel_get_all(&pdev->dev);
> +	if (IS_ERR(priv->iio_ch))
> +		return PTR_ERR(priv->iio_ch);
> +
> +	priv->iio_cb = iio_channel_get_all_cb(&pdev->dev, NULL, NULL);
> +	if (IS_ERR(priv->iio_cb))
> +		return PTR_ERR(priv->iio_ch);
> +
> +	ret = devm_snd_soc_register_platform(&pdev->dev,
> +					     &stm32_adfsdm_soc_platform);
> +	if (ret < 0)
> +		dev_err(&pdev->dev, "%s: Failed to register PCM platform\n",
> +			__func__);
> +
> +	return ret;
> +}
> +
> +static struct platform_driver stm32_adfsdm_driver = {
> +	.driver = {
> +		   .name = STM32_ADFSDM_DRV_NAME,
> +		   .of_match_table = stm32_adfsdm_of_match,
> +		   },
> +	.probe = stm32_adfsdm_probe,
> +};
> +
> +module_platform_driver(stm32_adfsdm_driver);
> +
> +MODULE_DESCRIPTION("stm32 DFSDM DAI driver");
> +MODULE_AUTHOR("Arnaud Pouliquen <arnaud.pouliquen@st.com>");
> +MODULE_LICENSE("GPL v2");
> +MODULE_ALIAS("platform:" STM32_ADFSDM_DRV_NAME);

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html