xref: /linux/sound/soc/stm/stm32_adfsdm.c (revision 67f8bc848ee31831336bd478e57d2f993551902e)
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 #include <linux/pm_runtime.h>
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 
28 struct 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 
45 static 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 
60 static 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 	guard(mutex)(&priv->lock);
66 	if (priv->iio_active) {
67 		iio_channel_stop_all_cb(priv->iio_cb);
68 		priv->iio_active = false;
69 	}
70 }
71 
72 static int stm32_adfsdm_dai_prepare(struct snd_pcm_substream *substream,
73 				    struct snd_soc_dai *dai)
74 {
75 	struct stm32_adfsdm_priv *priv = snd_soc_dai_get_drvdata(dai);
76 	int ret;
77 
78 	guard(mutex)(&priv->lock);
79 	if (priv->iio_active) {
80 		iio_channel_stop_all_cb(priv->iio_cb);
81 		priv->iio_active = false;
82 	}
83 
84 	ret = iio_write_channel_attribute(priv->iio_ch,
85 					  substream->runtime->rate, 0,
86 					  IIO_CHAN_INFO_SAMP_FREQ);
87 	if (ret < 0) {
88 		dev_err(dai->dev, "%s: Failed to set %d sampling rate\n",
89 			__func__, substream->runtime->rate);
90 		return ret;
91 	}
92 
93 	if (!priv->iio_active) {
94 		ret = iio_channel_start_all_cb(priv->iio_cb);
95 		if (!ret)
96 			priv->iio_active = true;
97 		else
98 			dev_err(dai->dev, "%s: IIO channel start failed (%d)\n",
99 				__func__, ret);
100 	}
101 
102 	return ret;
103 }
104 
105 static int stm32_adfsdm_set_sysclk(struct snd_soc_dai *dai, int clk_id,
106 				   unsigned int freq, int dir)
107 {
108 	struct stm32_adfsdm_priv *priv = snd_soc_dai_get_drvdata(dai);
109 	ssize_t size;
110 	char str_freq[10];
111 
112 	dev_dbg(dai->dev, "%s: Enter for freq %d\n", __func__, freq);
113 
114 	/* Set IIO frequency if CODEC is master as clock comes from SPI_IN */
115 
116 	snprintf(str_freq, sizeof(str_freq), "%u\n", freq);
117 	size = iio_write_channel_ext_info(priv->iio_ch, "spi_clk_freq",
118 					  str_freq, sizeof(str_freq));
119 	if (size != sizeof(str_freq)) {
120 		dev_err(dai->dev, "%s: Failed to set SPI clock\n",
121 			__func__);
122 		return -EINVAL;
123 	}
124 	return 0;
125 }
126 
127 static const struct snd_soc_dai_ops stm32_adfsdm_dai_ops = {
128 	.shutdown = stm32_adfsdm_shutdown,
129 	.prepare = stm32_adfsdm_dai_prepare,
130 	.set_sysclk = stm32_adfsdm_set_sysclk,
131 };
132 
133 static const struct snd_soc_dai_driver stm32_adfsdm_dai = {
134 	.capture = {
135 		    .channels_min = 1,
136 		    .channels_max = 1,
137 		    .formats = SNDRV_PCM_FMTBIT_S16_LE |
138 			       SNDRV_PCM_FMTBIT_S32_LE,
139 		    .rates = SNDRV_PCM_RATE_CONTINUOUS,
140 		    .rate_min = 8000,
141 		    .rate_max = 192000,
142 		    },
143 	.ops = &stm32_adfsdm_dai_ops,
144 };
145 
146 static const struct snd_soc_component_driver stm32_adfsdm_dai_component = {
147 	.name = "stm32_dfsdm_audio",
148 	.legacy_dai_naming = 1,
149 };
150 
151 static void stm32_memcpy_32to16(void *dest, const void *src, size_t n)
152 {
153 	unsigned int i = 0;
154 	u16 *d = (u16 *)dest, *s = (u16 *)src;
155 
156 	s++;
157 	for (i = n >> 1; i > 0; i--) {
158 		*d++ = *s++;
159 		s++;
160 	}
161 }
162 
163 static int stm32_afsdm_pcm_cb(const void *data, size_t size, void *private)
164 {
165 	struct stm32_adfsdm_priv *priv = private;
166 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(priv->substream);
167 	u8 *pcm_buff = priv->pcm_buff;
168 	u8 *src_buff = (u8 *)data;
169 	unsigned int old_pos = priv->pos;
170 	size_t buff_size = snd_pcm_lib_buffer_bytes(priv->substream);
171 	size_t period_size = snd_pcm_lib_period_bytes(priv->substream);
172 	size_t cur_size, src_size = size;
173 	snd_pcm_format_t format = priv->substream->runtime->format;
174 
175 	if (format == SNDRV_PCM_FORMAT_S16_LE)
176 		src_size >>= 1;
177 	cur_size = src_size;
178 
179 	dev_dbg(rtd->dev, "%s: buff_add :%p, pos = %d, size = %zu\n",
180 		__func__, &pcm_buff[priv->pos], priv->pos, src_size);
181 
182 	if ((priv->pos + src_size) > buff_size) {
183 		if (format == SNDRV_PCM_FORMAT_S16_LE)
184 			stm32_memcpy_32to16(&pcm_buff[priv->pos], src_buff,
185 					    buff_size - priv->pos);
186 		else
187 			memcpy(&pcm_buff[priv->pos], src_buff,
188 			       buff_size - priv->pos);
189 		cur_size -= buff_size - priv->pos;
190 		priv->pos = 0;
191 	}
192 
193 	if (format == SNDRV_PCM_FORMAT_S16_LE)
194 		stm32_memcpy_32to16(&pcm_buff[priv->pos],
195 				    &src_buff[src_size - cur_size], cur_size);
196 	else
197 		memcpy(&pcm_buff[priv->pos], &src_buff[src_size - cur_size],
198 		       cur_size);
199 
200 	priv->pos = (priv->pos + cur_size) % buff_size;
201 
202 	if (cur_size != src_size || (old_pos && (old_pos % period_size < size)))
203 		snd_pcm_period_elapsed(priv->substream);
204 
205 	return 0;
206 }
207 
208 static int stm32_adfsdm_trigger(struct snd_soc_component *component,
209 				struct snd_pcm_substream *substream, int cmd)
210 {
211 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
212 	struct stm32_adfsdm_priv *priv =
213 		snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
214 
215 	switch (cmd) {
216 	case SNDRV_PCM_TRIGGER_START:
217 	case SNDRV_PCM_TRIGGER_RESUME:
218 		priv->pos = 0;
219 		return stm32_dfsdm_get_buff_cb(priv->iio_ch->indio_dev,
220 					       stm32_afsdm_pcm_cb, priv);
221 	case SNDRV_PCM_TRIGGER_SUSPEND:
222 	case SNDRV_PCM_TRIGGER_STOP:
223 		return stm32_dfsdm_release_buff_cb(priv->iio_ch->indio_dev);
224 	}
225 
226 	return -EINVAL;
227 }
228 
229 static int stm32_adfsdm_pcm_open(struct snd_soc_component *component,
230 				 struct snd_pcm_substream *substream)
231 {
232 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
233 	struct stm32_adfsdm_priv *priv = snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
234 	int ret;
235 
236 	ret =  snd_soc_set_runtime_hwparams(substream, &stm32_adfsdm_pcm_hw);
237 	if (!ret)
238 		priv->substream = substream;
239 
240 	return ret;
241 }
242 
243 static int stm32_adfsdm_pcm_close(struct snd_soc_component *component,
244 				  struct snd_pcm_substream *substream)
245 {
246 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
247 	struct stm32_adfsdm_priv *priv =
248 		snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
249 
250 	priv->substream = NULL;
251 
252 	return 0;
253 }
254 
255 static snd_pcm_uframes_t stm32_adfsdm_pcm_pointer(
256 					    struct snd_soc_component *component,
257 					    struct snd_pcm_substream *substream)
258 {
259 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
260 	struct stm32_adfsdm_priv *priv =
261 		snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
262 
263 	return bytes_to_frames(substream->runtime, priv->pos);
264 }
265 
266 static int stm32_adfsdm_pcm_hw_params(struct snd_soc_component *component,
267 				      struct snd_pcm_substream *substream,
268 				      struct snd_pcm_hw_params *params)
269 {
270 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
271 	struct stm32_adfsdm_priv *priv =
272 		snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
273 
274 	priv->pcm_buff = substream->runtime->dma_area;
275 
276 	return iio_channel_cb_set_buffer_watermark(priv->iio_cb,
277 						   params_period_size(params));
278 }
279 
280 static int stm32_adfsdm_pcm_new(struct snd_soc_component *component,
281 				struct snd_soc_pcm_runtime *rtd)
282 {
283 	struct snd_pcm *pcm = rtd->pcm;
284 	struct stm32_adfsdm_priv *priv =
285 		snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
286 	unsigned int size = DFSDM_MAX_PERIODS * DFSDM_MAX_PERIOD_SIZE;
287 
288 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
289 				       priv->dev, size, size);
290 	return 0;
291 }
292 
293 static int stm32_adfsdm_dummy_cb(const void *data, void *private)
294 {
295 	/*
296 	 * This dummy callback is requested by iio_channel_get_all_cb() API,
297 	 * but the stm32_dfsdm_get_buff_cb() API is used instead, to optimize
298 	 * DMA transfers.
299 	 */
300 	return 0;
301 }
302 
303 static void stm32_adfsdm_cleanup(void *data)
304 {
305 	iio_channel_release_all_cb(data);
306 }
307 
308 static const struct snd_soc_component_driver stm32_adfsdm_soc_platform = {
309 	.open		= stm32_adfsdm_pcm_open,
310 	.close		= stm32_adfsdm_pcm_close,
311 	.hw_params	= stm32_adfsdm_pcm_hw_params,
312 	.trigger	= stm32_adfsdm_trigger,
313 	.pointer	= stm32_adfsdm_pcm_pointer,
314 	.pcm_new	= stm32_adfsdm_pcm_new,
315 	.debugfs_prefix	= "pcm",
316 };
317 
318 static const struct of_device_id stm32_adfsdm_of_match[] = {
319 	{.compatible = "st,stm32h7-dfsdm-dai"},
320 	{}
321 };
322 MODULE_DEVICE_TABLE(of, stm32_adfsdm_of_match);
323 
324 static int stm32_adfsdm_probe(struct platform_device *pdev)
325 {
326 	struct stm32_adfsdm_priv *priv;
327 	int ret;
328 
329 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
330 	if (!priv)
331 		return -ENOMEM;
332 
333 	priv->dev = &pdev->dev;
334 	priv->dai_drv = stm32_adfsdm_dai;
335 	mutex_init(&priv->lock);
336 
337 	dev_set_drvdata(&pdev->dev, priv);
338 
339 	ret = devm_snd_soc_register_component(&pdev->dev,
340 					      &stm32_adfsdm_dai_component,
341 					      &priv->dai_drv, 1);
342 	if (ret < 0)
343 		return ret;
344 
345 	/* Associate iio channel */
346 	priv->iio_ch  = devm_iio_channel_get_all(&pdev->dev);
347 	if (IS_ERR(priv->iio_ch))
348 		return PTR_ERR(priv->iio_ch);
349 
350 	priv->iio_cb = iio_channel_get_all_cb(&pdev->dev, &stm32_adfsdm_dummy_cb, NULL);
351 	if (IS_ERR(priv->iio_cb))
352 		return PTR_ERR(priv->iio_cb);
353 
354 	ret = devm_add_action_or_reset(&pdev->dev, stm32_adfsdm_cleanup, priv->iio_cb);
355 	if (ret < 0)  {
356 		dev_err(&pdev->dev, "Unable to add action\n");
357 		return ret;
358 	}
359 
360 	ret = devm_snd_soc_register_component(&pdev->dev,
361 					      &stm32_adfsdm_soc_platform,
362 					      NULL, 0);
363 	if (ret < 0)
364 		return ret;
365 
366 	pm_runtime_enable(&pdev->dev);
367 
368 	return ret;
369 }
370 
371 static void stm32_adfsdm_remove(struct platform_device *pdev)
372 {
373 	snd_soc_unregister_component(&pdev->dev);
374 	pm_runtime_disable(&pdev->dev);
375 }
376 
377 static struct platform_driver stm32_adfsdm_driver = {
378 	.driver = {
379 		   .name = STM32_ADFSDM_DRV_NAME,
380 		   .of_match_table = stm32_adfsdm_of_match,
381 		   },
382 	.probe = stm32_adfsdm_probe,
383 	.remove = stm32_adfsdm_remove,
384 };
385 
386 module_platform_driver(stm32_adfsdm_driver);
387 
388 MODULE_DESCRIPTION("stm32 DFSDM DAI driver");
389 MODULE_AUTHOR("Arnaud Pouliquen <arnaud.pouliquen@st.com>");
390 MODULE_LICENSE("GPL v2");
391 MODULE_ALIAS("platform:" STM32_ADFSDM_DRV_NAME);
392 MODULE_IMPORT_NS("IIO_CONSUMER");
393