Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

Mar 24-27, 2025, special US time zones
Register
Loading...
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * This file is part of STM32 DFSDM ASoC DAI driver
  4 *
  5 * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
  6 * Authors: Arnaud Pouliquen <arnaud.pouliquen@st.com>
  7 *          Olivier Moysan <olivier.moysan@st.com>
  8 */
  9
 10#include <linux/clk.h>
 11#include <linux/module.h>
 12#include <linux/mutex.h>
 13#include <linux/platform_device.h>
 14#include <linux/slab.h>
 15
 16#include <linux/iio/iio.h>
 17#include <linux/iio/consumer.h>
 18#include <linux/iio/adc/stm32-dfsdm-adc.h>
 19
 20#include <sound/pcm.h>
 21#include <sound/soc.h>
 22
 23#define STM32_ADFSDM_DRV_NAME "stm32-adfsdm"
 24
 25#define DFSDM_MAX_PERIOD_SIZE	(PAGE_SIZE / 2)
 26#define DFSDM_MAX_PERIODS	6
 27
 28struct stm32_adfsdm_priv {
 29	struct snd_soc_dai_driver dai_drv;
 30	struct snd_pcm_substream *substream;
 31	struct device *dev;
 32
 33	/* IIO */
 34	struct iio_channel *iio_ch;
 35	struct iio_cb_buffer *iio_cb;
 36	bool iio_active;
 37
 38	/* PCM buffer */
 39	unsigned char *pcm_buff;
 40	unsigned int pos;
 41
 42	struct mutex lock; /* protect against race condition on iio state */
 43};
 44
 45static const struct snd_pcm_hardware stm32_adfsdm_pcm_hw = {
 46	.info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
 47		SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_PAUSE,
 48	.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
 
 
 
 49
 50	.channels_min = 1,
 51	.channels_max = 1,
 52
 53	.periods_min = 2,
 54	.periods_max = DFSDM_MAX_PERIODS,
 55
 56	.period_bytes_max = DFSDM_MAX_PERIOD_SIZE,
 57	.buffer_bytes_max = DFSDM_MAX_PERIODS * DFSDM_MAX_PERIOD_SIZE
 58};
 59
 60static void stm32_adfsdm_shutdown(struct snd_pcm_substream *substream,
 61				  struct snd_soc_dai *dai)
 62{
 63	struct stm32_adfsdm_priv *priv = snd_soc_dai_get_drvdata(dai);
 64
 65	mutex_lock(&priv->lock);
 66	if (priv->iio_active) {
 67		iio_channel_stop_all_cb(priv->iio_cb);
 68		priv->iio_active = false;
 69	}
 70	mutex_unlock(&priv->lock);
 71}
 72
 73static int stm32_adfsdm_dai_prepare(struct snd_pcm_substream *substream,
 74				    struct snd_soc_dai *dai)
 75{
 76	struct stm32_adfsdm_priv *priv = snd_soc_dai_get_drvdata(dai);
 77	int ret;
 78
 79	mutex_lock(&priv->lock);
 80	if (priv->iio_active) {
 81		iio_channel_stop_all_cb(priv->iio_cb);
 82		priv->iio_active = false;
 83	}
 84
 85	ret = iio_write_channel_attribute(priv->iio_ch,
 86					  substream->runtime->rate, 0,
 87					  IIO_CHAN_INFO_SAMP_FREQ);
 88	if (ret < 0) {
 89		dev_err(dai->dev, "%s: Failed to set %d sampling rate\n",
 90			__func__, substream->runtime->rate);
 91		goto out;
 92	}
 93
 94	if (!priv->iio_active) {
 95		ret = iio_channel_start_all_cb(priv->iio_cb);
 96		if (!ret)
 97			priv->iio_active = true;
 98		else
 99			dev_err(dai->dev, "%s: IIO channel start failed (%d)\n",
100				__func__, ret);
101	}
102
103out:
104	mutex_unlock(&priv->lock);
105
106	return ret;
107}
108
109static int stm32_adfsdm_set_sysclk(struct snd_soc_dai *dai, int clk_id,
110				   unsigned int freq, int dir)
111{
112	struct stm32_adfsdm_priv *priv = snd_soc_dai_get_drvdata(dai);
113	ssize_t size;
114	char str_freq[10];
115
116	dev_dbg(dai->dev, "%s: Enter for freq %d\n", __func__, freq);
117
118	/* Set IIO frequency if CODEC is master as clock comes from SPI_IN */
119
120	snprintf(str_freq, sizeof(str_freq), "%u\n", freq);
121	size = iio_write_channel_ext_info(priv->iio_ch, "spi_clk_freq",
122					  str_freq, sizeof(str_freq));
123	if (size != sizeof(str_freq)) {
124		dev_err(dai->dev, "%s: Failed to set SPI clock\n",
125			__func__);
126		return -EINVAL;
127	}
128	return 0;
129}
130
131static const struct snd_soc_dai_ops stm32_adfsdm_dai_ops = {
132	.shutdown = stm32_adfsdm_shutdown,
133	.prepare = stm32_adfsdm_dai_prepare,
134	.set_sysclk = stm32_adfsdm_set_sysclk,
135};
136
137static const struct snd_soc_dai_driver stm32_adfsdm_dai = {
138	.capture = {
139		    .channels_min = 1,
140		    .channels_max = 1,
141		    .formats = SNDRV_PCM_FMTBIT_S16_LE |
142			       SNDRV_PCM_FMTBIT_S32_LE,
143		    .rates = SNDRV_PCM_RATE_CONTINUOUS,
144		    .rate_min = 8000,
145		    .rate_max = 48000,
146		    },
147	.ops = &stm32_adfsdm_dai_ops,
148};
149
150static const struct snd_soc_component_driver stm32_adfsdm_dai_component = {
151	.name = "stm32_dfsdm_audio",
152};
153
154static void stm32_memcpy_32to16(void *dest, const void *src, size_t n)
155{
156	unsigned int i = 0;
157	u16 *d = (u16 *)dest, *s = (u16 *)src;
158
159	s++;
160	for (i = n >> 1; i > 0; i--) {
161		*d++ = *s++;
162		s++;
163	}
164}
165
166static int stm32_afsdm_pcm_cb(const void *data, size_t size, void *private)
167{
168	struct stm32_adfsdm_priv *priv = private;
169	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(priv->substream);
170	u8 *pcm_buff = priv->pcm_buff;
171	u8 *src_buff = (u8 *)data;
 
 
172	unsigned int old_pos = priv->pos;
173	size_t buff_size = snd_pcm_lib_buffer_bytes(priv->substream);
174	size_t period_size = snd_pcm_lib_period_bytes(priv->substream);
175	size_t cur_size, src_size = size;
176	snd_pcm_format_t format = priv->substream->runtime->format;
177
178	if (format == SNDRV_PCM_FORMAT_S16_LE)
179		src_size >>= 1;
180	cur_size = src_size;
181
182	dev_dbg(rtd->dev, "%s: buff_add :%pK, pos = %d, size = %zu\n",
183		__func__, &pcm_buff[priv->pos], priv->pos, src_size);
184
185	if ((priv->pos + src_size) > buff_size) {
186		if (format == SNDRV_PCM_FORMAT_S16_LE)
187			stm32_memcpy_32to16(&pcm_buff[priv->pos], src_buff,
188					    buff_size - priv->pos);
189		else
190			memcpy(&pcm_buff[priv->pos], src_buff,
191			       buff_size - priv->pos);
192		cur_size -= buff_size - priv->pos;
193		priv->pos = 0;
194	}
195
196	if (format == SNDRV_PCM_FORMAT_S16_LE)
197		stm32_memcpy_32to16(&pcm_buff[priv->pos],
198				    &src_buff[src_size - cur_size], cur_size);
199	else
200		memcpy(&pcm_buff[priv->pos], &src_buff[src_size - cur_size],
201		       cur_size);
202
203	priv->pos = (priv->pos + cur_size) % buff_size;
204
205	if (cur_size != src_size || (old_pos && (old_pos % period_size < size)))
206		snd_pcm_period_elapsed(priv->substream);
207
208	return 0;
209}
210
211static int stm32_adfsdm_trigger(struct snd_soc_component *component,
212				struct snd_pcm_substream *substream, int cmd)
213{
214	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
215	struct stm32_adfsdm_priv *priv =
216		snd_soc_dai_get_drvdata(asoc_rtd_to_cpu(rtd, 0));
217
218	switch (cmd) {
219	case SNDRV_PCM_TRIGGER_START:
220	case SNDRV_PCM_TRIGGER_RESUME:
221		priv->pos = 0;
222		return stm32_dfsdm_get_buff_cb(priv->iio_ch->indio_dev,
223					       stm32_afsdm_pcm_cb, priv);
224	case SNDRV_PCM_TRIGGER_SUSPEND:
225	case SNDRV_PCM_TRIGGER_STOP:
226		return stm32_dfsdm_release_buff_cb(priv->iio_ch->indio_dev);
227	}
228
229	return -EINVAL;
230}
231
232static int stm32_adfsdm_pcm_open(struct snd_soc_component *component,
233				 struct snd_pcm_substream *substream)
234{
235	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
236	struct stm32_adfsdm_priv *priv = snd_soc_dai_get_drvdata(asoc_rtd_to_cpu(rtd, 0));
237	int ret;
238
239	ret =  snd_soc_set_runtime_hwparams(substream, &stm32_adfsdm_pcm_hw);
240	if (!ret)
241		priv->substream = substream;
242
243	return ret;
244}
245
246static int stm32_adfsdm_pcm_close(struct snd_soc_component *component,
247				  struct snd_pcm_substream *substream)
248{
249	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
250	struct stm32_adfsdm_priv *priv =
251		snd_soc_dai_get_drvdata(asoc_rtd_to_cpu(rtd, 0));
252
 
253	priv->substream = NULL;
254
255	return 0;
256}
257
258static snd_pcm_uframes_t stm32_adfsdm_pcm_pointer(
259					    struct snd_soc_component *component,
260					    struct snd_pcm_substream *substream)
261{
262	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
263	struct stm32_adfsdm_priv *priv =
264		snd_soc_dai_get_drvdata(asoc_rtd_to_cpu(rtd, 0));
265
266	return bytes_to_frames(substream->runtime, priv->pos);
267}
268
269static int stm32_adfsdm_pcm_hw_params(struct snd_soc_component *component,
270				      struct snd_pcm_substream *substream,
271				      struct snd_pcm_hw_params *params)
272{
273	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
274	struct stm32_adfsdm_priv *priv =
275		snd_soc_dai_get_drvdata(asoc_rtd_to_cpu(rtd, 0));
 
276
 
 
 
277	priv->pcm_buff = substream->runtime->dma_area;
278
279	return iio_channel_cb_set_buffer_watermark(priv->iio_cb,
280						   params_period_size(params));
281}
282
283static int stm32_adfsdm_pcm_new(struct snd_soc_component *component,
284				struct snd_soc_pcm_runtime *rtd)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
285{
286	struct snd_pcm *pcm = rtd->pcm;
287	struct stm32_adfsdm_priv *priv =
288		snd_soc_dai_get_drvdata(asoc_rtd_to_cpu(rtd, 0));
289	unsigned int size = DFSDM_MAX_PERIODS * DFSDM_MAX_PERIOD_SIZE;
290
291	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
292				       priv->dev, size, size);
293	return 0;
294}
295
296static int stm32_adfsdm_dummy_cb(const void *data, void *private)
297{
298	/*
299	 * This dummmy callback is requested by iio_channel_get_all_cb() API,
300	 * but the stm32_dfsdm_get_buff_cb() API is used instead, to optimize
301	 * DMA transfers.
302	 */
303	return 0;
 
 
 
 
 
304}
305
306static struct snd_soc_component_driver stm32_adfsdm_soc_platform = {
307	.open		= stm32_adfsdm_pcm_open,
308	.close		= stm32_adfsdm_pcm_close,
309	.hw_params	= stm32_adfsdm_pcm_hw_params,
310	.trigger	= stm32_adfsdm_trigger,
311	.pointer	= stm32_adfsdm_pcm_pointer,
312	.pcm_construct	= stm32_adfsdm_pcm_new,
313};
314
315static const struct of_device_id stm32_adfsdm_of_match[] = {
316	{.compatible = "st,stm32h7-dfsdm-dai"},
317	{}
318};
319MODULE_DEVICE_TABLE(of, stm32_adfsdm_of_match);
320
321static int stm32_adfsdm_probe(struct platform_device *pdev)
322{
323	struct stm32_adfsdm_priv *priv;
324	struct snd_soc_component *component;
325	int ret;
326
327	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
328	if (!priv)
329		return -ENOMEM;
330
331	priv->dev = &pdev->dev;
332	priv->dai_drv = stm32_adfsdm_dai;
333	mutex_init(&priv->lock);
334
335	dev_set_drvdata(&pdev->dev, priv);
336
337	ret = devm_snd_soc_register_component(&pdev->dev,
338					      &stm32_adfsdm_dai_component,
339					      &priv->dai_drv, 1);
340	if (ret < 0)
341		return ret;
342
343	/* Associate iio channel */
344	priv->iio_ch  = devm_iio_channel_get_all(&pdev->dev);
345	if (IS_ERR(priv->iio_ch))
346		return PTR_ERR(priv->iio_ch);
347
348	priv->iio_cb = iio_channel_get_all_cb(&pdev->dev, &stm32_adfsdm_dummy_cb, NULL);
349	if (IS_ERR(priv->iio_cb))
350		return PTR_ERR(priv->iio_cb);
351
352	component = devm_kzalloc(&pdev->dev, sizeof(*component), GFP_KERNEL);
353	if (!component)
354		return -ENOMEM;
355
356	ret = snd_soc_component_initialize(component,
357					   &stm32_adfsdm_soc_platform,
358					   &pdev->dev);
359	if (ret < 0)
360		return ret;
361#ifdef CONFIG_DEBUG_FS
362	component->debugfs_prefix = "pcm";
363#endif
364
365	ret = snd_soc_add_component(component, NULL, 0);
366	if (ret < 0)
367		dev_err(&pdev->dev, "%s: Failed to register PCM platform\n",
368			__func__);
369
370	return ret;
371}
372
373static int stm32_adfsdm_remove(struct platform_device *pdev)
374{
375	snd_soc_unregister_component(&pdev->dev);
376
377	return 0;
378}
379
380static struct platform_driver stm32_adfsdm_driver = {
381	.driver = {
382		   .name = STM32_ADFSDM_DRV_NAME,
383		   .of_match_table = stm32_adfsdm_of_match,
384		   },
385	.probe = stm32_adfsdm_probe,
386	.remove = stm32_adfsdm_remove,
387};
388
389module_platform_driver(stm32_adfsdm_driver);
390
391MODULE_DESCRIPTION("stm32 DFSDM DAI driver");
392MODULE_AUTHOR("Arnaud Pouliquen <arnaud.pouliquen@st.com>");
393MODULE_LICENSE("GPL v2");
394MODULE_ALIAS("platform:" STM32_ADFSDM_DRV_NAME);
v4.17
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * This file is part of STM32 DFSDM ASoC DAI driver
  4 *
  5 * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
  6 * Authors: Arnaud Pouliquen <arnaud.pouliquen@st.com>
  7 *          Olivier Moysan <olivier.moysan@st.com>
  8 */
  9
 10#include <linux/clk.h>
 11#include <linux/module.h>
 
 12#include <linux/platform_device.h>
 13#include <linux/slab.h>
 14
 15#include <linux/iio/iio.h>
 16#include <linux/iio/consumer.h>
 17#include <linux/iio/adc/stm32-dfsdm-adc.h>
 18
 19#include <sound/pcm.h>
 20#include <sound/soc.h>
 21
 22#define STM32_ADFSDM_DRV_NAME "stm32-adfsdm"
 23
 24#define DFSDM_MAX_PERIOD_SIZE	(PAGE_SIZE / 2)
 25#define DFSDM_MAX_PERIODS	6
 26
 27struct stm32_adfsdm_priv {
 28	struct snd_soc_dai_driver dai_drv;
 29	struct snd_pcm_substream *substream;
 30	struct device *dev;
 31
 32	/* IIO */
 33	struct iio_channel *iio_ch;
 34	struct iio_cb_buffer *iio_cb;
 35	bool iio_active;
 36
 37	/* PCM buffer */
 38	unsigned char *pcm_buff;
 39	unsigned int pos;
 
 
 40};
 41
 42static const struct snd_pcm_hardware stm32_adfsdm_pcm_hw = {
 43	.info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
 44	    SNDRV_PCM_INFO_PAUSE,
 45	.formats = SNDRV_PCM_FMTBIT_S32_LE,
 46
 47	.rate_min = 8000,
 48	.rate_max = 32000,
 49
 50	.channels_min = 1,
 51	.channels_max = 1,
 52
 53	.periods_min = 2,
 54	.periods_max = DFSDM_MAX_PERIODS,
 55
 56	.period_bytes_max = DFSDM_MAX_PERIOD_SIZE,
 57	.buffer_bytes_max = DFSDM_MAX_PERIODS * DFSDM_MAX_PERIOD_SIZE
 58};
 59
 60static void stm32_adfsdm_shutdown(struct snd_pcm_substream *substream,
 61				  struct snd_soc_dai *dai)
 62{
 63	struct stm32_adfsdm_priv *priv = snd_soc_dai_get_drvdata(dai);
 64
 
 65	if (priv->iio_active) {
 66		iio_channel_stop_all_cb(priv->iio_cb);
 67		priv->iio_active = false;
 68	}
 
 69}
 70
 71static int stm32_adfsdm_dai_prepare(struct snd_pcm_substream *substream,
 72				    struct snd_soc_dai *dai)
 73{
 74	struct stm32_adfsdm_priv *priv = snd_soc_dai_get_drvdata(dai);
 75	int ret;
 76
 
 
 
 
 
 
 77	ret = iio_write_channel_attribute(priv->iio_ch,
 78					  substream->runtime->rate, 0,
 79					  IIO_CHAN_INFO_SAMP_FREQ);
 80	if (ret < 0) {
 81		dev_err(dai->dev, "%s: Failed to set %d sampling rate\n",
 82			__func__, substream->runtime->rate);
 83		return ret;
 84	}
 85
 86	if (!priv->iio_active) {
 87		ret = iio_channel_start_all_cb(priv->iio_cb);
 88		if (!ret)
 89			priv->iio_active = true;
 90		else
 91			dev_err(dai->dev, "%s: IIO channel start failed (%d)\n",
 92				__func__, ret);
 93	}
 94
 
 
 
 95	return ret;
 96}
 97
 98static int stm32_adfsdm_set_sysclk(struct snd_soc_dai *dai, int clk_id,
 99				   unsigned int freq, int dir)
100{
101	struct stm32_adfsdm_priv *priv = snd_soc_dai_get_drvdata(dai);
102	ssize_t size;
103	char str_freq[10];
104
105	dev_dbg(dai->dev, "%s: Enter for freq %d\n", __func__, freq);
106
107	/* Set IIO frequency if CODEC is master as clock comes from SPI_IN */
108
109	snprintf(str_freq, sizeof(str_freq), "%d\n", freq);
110	size = iio_write_channel_ext_info(priv->iio_ch, "spi_clk_freq",
111					  str_freq, sizeof(str_freq));
112	if (size != sizeof(str_freq)) {
113		dev_err(dai->dev, "%s: Failed to set SPI clock\n",
114			__func__);
115		return -EINVAL;
116	}
117	return 0;
118}
119
120static const struct snd_soc_dai_ops stm32_adfsdm_dai_ops = {
121	.shutdown = stm32_adfsdm_shutdown,
122	.prepare = stm32_adfsdm_dai_prepare,
123	.set_sysclk = stm32_adfsdm_set_sysclk,
124};
125
126static const struct snd_soc_dai_driver stm32_adfsdm_dai = {
127	.capture = {
128		    .channels_min = 1,
129		    .channels_max = 1,
130		    .formats = SNDRV_PCM_FMTBIT_S32_LE,
131		    .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
132			      SNDRV_PCM_RATE_32000),
 
 
133		    },
134	.ops = &stm32_adfsdm_dai_ops,
135};
136
137static const struct snd_soc_component_driver stm32_adfsdm_dai_component = {
138	.name = "stm32_dfsdm_audio",
139};
140
 
 
 
 
 
 
 
 
 
 
 
 
141static int stm32_afsdm_pcm_cb(const void *data, size_t size, void *private)
142{
143	struct stm32_adfsdm_priv *priv = private;
144	struct snd_soc_pcm_runtime *rtd = priv->substream->private_data;
145	u8 *pcm_buff = priv->pcm_buff;
146	u8 *src_buff = (u8 *)data;
147	unsigned int buff_size = snd_pcm_lib_buffer_bytes(priv->substream);
148	unsigned int period_size = snd_pcm_lib_period_bytes(priv->substream);
149	unsigned int old_pos = priv->pos;
150	unsigned int cur_size = size;
151
152	dev_dbg(rtd->dev, "%s: buff_add :%p, pos = %d, size = %zu\n",
153		__func__, &pcm_buff[priv->pos], priv->pos, size);
154
155	if ((priv->pos + size) > buff_size) {
156		memcpy(&pcm_buff[priv->pos], src_buff, buff_size - priv->pos);
 
 
 
 
 
 
 
 
 
 
 
 
157		cur_size -= buff_size - priv->pos;
158		priv->pos = 0;
159	}
160
161	memcpy(&pcm_buff[priv->pos], &src_buff[size - cur_size], cur_size);
 
 
 
 
 
 
162	priv->pos = (priv->pos + cur_size) % buff_size;
163
164	if (cur_size != size || (old_pos && (old_pos % period_size < size)))
165		snd_pcm_period_elapsed(priv->substream);
166
167	return 0;
168}
169
170static int stm32_adfsdm_trigger(struct snd_pcm_substream *substream, int cmd)
 
171{
172	struct snd_soc_pcm_runtime *rtd = substream->private_data;
173	struct stm32_adfsdm_priv *priv =
174		snd_soc_dai_get_drvdata(rtd->cpu_dai);
175
176	switch (cmd) {
177	case SNDRV_PCM_TRIGGER_START:
178	case SNDRV_PCM_TRIGGER_RESUME:
179		priv->pos = 0;
180		return stm32_dfsdm_get_buff_cb(priv->iio_ch->indio_dev,
181					       stm32_afsdm_pcm_cb, priv);
182	case SNDRV_PCM_TRIGGER_SUSPEND:
183	case SNDRV_PCM_TRIGGER_STOP:
184		return stm32_dfsdm_release_buff_cb(priv->iio_ch->indio_dev);
185	}
186
187	return -EINVAL;
188}
189
190static int stm32_adfsdm_pcm_open(struct snd_pcm_substream *substream)
 
191{
192	struct snd_soc_pcm_runtime *rtd = substream->private_data;
193	struct stm32_adfsdm_priv *priv = snd_soc_dai_get_drvdata(rtd->cpu_dai);
194	int ret;
195
196	ret =  snd_soc_set_runtime_hwparams(substream, &stm32_adfsdm_pcm_hw);
197	if (!ret)
198		priv->substream = substream;
199
200	return ret;
201}
202
203static int stm32_adfsdm_pcm_close(struct snd_pcm_substream *substream)
 
204{
205	struct snd_soc_pcm_runtime *rtd = substream->private_data;
206	struct stm32_adfsdm_priv *priv =
207		snd_soc_dai_get_drvdata(rtd->cpu_dai);
208
209	snd_pcm_lib_free_pages(substream);
210	priv->substream = NULL;
211
212	return 0;
213}
214
215static snd_pcm_uframes_t stm32_adfsdm_pcm_pointer(
 
216					    struct snd_pcm_substream *substream)
217{
218	struct snd_soc_pcm_runtime *rtd = substream->private_data;
219	struct stm32_adfsdm_priv *priv =
220		snd_soc_dai_get_drvdata(rtd->cpu_dai);
221
222	return bytes_to_frames(substream->runtime, priv->pos);
223}
224
225static int stm32_adfsdm_pcm_hw_params(struct snd_pcm_substream *substream,
 
226				      struct snd_pcm_hw_params *params)
227{
228	struct snd_soc_pcm_runtime *rtd = substream->private_data;
229	struct stm32_adfsdm_priv *priv =
230		snd_soc_dai_get_drvdata(rtd->cpu_dai);
231	int ret;
232
233	ret =  snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
234	if (ret < 0)
235		return ret;
236	priv->pcm_buff = substream->runtime->dma_area;
237
238	return iio_channel_cb_set_buffer_watermark(priv->iio_cb,
239						   params_period_size(params));
240}
241
242static int stm32_adfsdm_pcm_hw_free(struct snd_pcm_substream *substream)
243{
244	snd_pcm_lib_free_pages(substream);
245
246	return 0;
247}
248
249static struct snd_pcm_ops stm32_adfsdm_pcm_ops = {
250	.open		= stm32_adfsdm_pcm_open,
251	.close		= stm32_adfsdm_pcm_close,
252	.hw_params	= stm32_adfsdm_pcm_hw_params,
253	.hw_free	= stm32_adfsdm_pcm_hw_free,
254	.trigger	= stm32_adfsdm_trigger,
255	.pointer	= stm32_adfsdm_pcm_pointer,
256};
257
258static int stm32_adfsdm_pcm_new(struct snd_soc_pcm_runtime *rtd)
259{
260	struct snd_pcm *pcm = rtd->pcm;
261	struct stm32_adfsdm_priv *priv =
262		snd_soc_dai_get_drvdata(rtd->cpu_dai);
263	unsigned int size = DFSDM_MAX_PERIODS * DFSDM_MAX_PERIOD_SIZE;
264
265	return snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
266						     priv->dev, size, size);
 
267}
268
269static void stm32_adfsdm_pcm_free(struct snd_pcm *pcm)
270{
271	struct snd_pcm_substream *substream;
272	struct snd_soc_pcm_runtime *rtd;
273	struct stm32_adfsdm_priv *priv;
274
275	substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
276	if (substream) {
277		rtd = substream->private_data;
278		priv = snd_soc_dai_get_drvdata(rtd->cpu_dai);
279
280		snd_pcm_lib_preallocate_free_for_all(pcm);
281	}
282}
283
284static struct snd_soc_component_driver stm32_adfsdm_soc_platform = {
285	.ops		= &stm32_adfsdm_pcm_ops,
286	.pcm_new	= stm32_adfsdm_pcm_new,
287	.pcm_free	= stm32_adfsdm_pcm_free,
 
 
 
288};
289
290static const struct of_device_id stm32_adfsdm_of_match[] = {
291	{.compatible = "st,stm32h7-dfsdm-dai"},
292	{}
293};
294MODULE_DEVICE_TABLE(of, stm32_adfsdm_of_match);
295
296static int stm32_adfsdm_probe(struct platform_device *pdev)
297{
298	struct stm32_adfsdm_priv *priv;
 
299	int ret;
300
301	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
302	if (!priv)
303		return -ENOMEM;
304
305	priv->dev = &pdev->dev;
306	priv->dai_drv = stm32_adfsdm_dai;
 
307
308	dev_set_drvdata(&pdev->dev, priv);
309
310	ret = devm_snd_soc_register_component(&pdev->dev,
311					      &stm32_adfsdm_dai_component,
312					      &priv->dai_drv, 1);
313	if (ret < 0)
314		return ret;
315
316	/* Associate iio channel */
317	priv->iio_ch  = devm_iio_channel_get_all(&pdev->dev);
318	if (IS_ERR(priv->iio_ch))
319		return PTR_ERR(priv->iio_ch);
320
321	priv->iio_cb = iio_channel_get_all_cb(&pdev->dev, NULL, NULL);
322	if (IS_ERR(priv->iio_cb))
323		return PTR_ERR(priv->iio_cb);
324
325	ret = devm_snd_soc_register_component(&pdev->dev,
326					      &stm32_adfsdm_soc_platform,
327					      NULL, 0);
 
 
 
 
 
 
 
 
 
 
 
328	if (ret < 0)
329		dev_err(&pdev->dev, "%s: Failed to register PCM platform\n",
330			__func__);
331
332	return ret;
333}
334
 
 
 
 
 
 
 
335static struct platform_driver stm32_adfsdm_driver = {
336	.driver = {
337		   .name = STM32_ADFSDM_DRV_NAME,
338		   .of_match_table = stm32_adfsdm_of_match,
339		   },
340	.probe = stm32_adfsdm_probe,
 
341};
342
343module_platform_driver(stm32_adfsdm_driver);
344
345MODULE_DESCRIPTION("stm32 DFSDM DAI driver");
346MODULE_AUTHOR("Arnaud Pouliquen <arnaud.pouliquen@st.com>");
347MODULE_LICENSE("GPL v2");
348MODULE_ALIAS("platform:" STM32_ADFSDM_DRV_NAME);