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