blob: 3c9a9deec9af4b6baeefac92953d96b134078b74 [file] [log] [blame]
Arnaud Pouliquen55da0942018-01-10 11:13:15 +01001// 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>
Olivier Moysan19441e32019-03-04 15:52:43 +010012#include <linux/mutex.h>
Arnaud Pouliquen55da0942018-01-10 11:13:15 +010013#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;
Olivier Moysan19441e32019-03-04 15:52:43 +010041
42 struct mutex lock; /* protect against race condition on iio state */
Arnaud Pouliquen55da0942018-01-10 11:13:15 +010043};
44
45static const struct snd_pcm_hardware stm32_adfsdm_pcm_hw = {
46 .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
Olivier Moysand151cf82019-03-04 15:52:45 +010047 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_PAUSE,
Olivier Moysan1e7f6e12019-06-19 13:40:02 +020048 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
Arnaud Pouliquen55da0942018-01-10 11:13:15 +010049
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
63static 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
Olivier Moysan19441e32019-03-04 15:52:43 +010068 mutex_lock(&priv->lock);
Arnaud Pouliquen55da0942018-01-10 11:13:15 +010069 if (priv->iio_active) {
70 iio_channel_stop_all_cb(priv->iio_cb);
71 priv->iio_active = false;
72 }
Olivier Moysan19441e32019-03-04 15:52:43 +010073 mutex_unlock(&priv->lock);
Arnaud Pouliquen55da0942018-01-10 11:13:15 +010074}
75
76static 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
Olivier Moysan19441e32019-03-04 15:52:43 +010082 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
Arnaud Pouliquen55da0942018-01-10 11:13:15 +010088 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);
Olivier Moysan19441e32019-03-04 15:52:43 +010094 goto out;
Arnaud Pouliquen55da0942018-01-10 11:13:15 +010095 }
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
Olivier Moysan19441e32019-03-04 15:52:43 +0100106out:
107 mutex_unlock(&priv->lock);
108
Arnaud Pouliquen55da0942018-01-10 11:13:15 +0100109 return ret;
110}
111
112static 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
134static 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
140static const struct snd_soc_dai_driver stm32_adfsdm_dai = {
141 .capture = {
142 .channels_min = 1,
143 .channels_max = 1,
Olivier Moysan1e7f6e12019-06-19 13:40:02 +0200144 .formats = SNDRV_PCM_FMTBIT_S16_LE |
145 SNDRV_PCM_FMTBIT_S32_LE,
Arnaud Pouliquen55da0942018-01-10 11:13:15 +0100146 .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
147 SNDRV_PCM_RATE_32000),
148 },
149 .ops = &stm32_adfsdm_dai_ops,
150};
151
152static const struct snd_soc_component_driver stm32_adfsdm_dai_component = {
153 .name = "stm32_dfsdm_audio",
154};
155
Olivier Moysan1e7f6e12019-06-19 13:40:02 +0200156static void 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; i > 0; i--) {
163 *d++ = *s++;
164 s++;
165 }
166}
167
Arnaud Pouliquen55da0942018-01-10 11:13:15 +0100168static 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 = priv->substream->private_data;
172 u8 *pcm_buff = priv->pcm_buff;
173 u8 *src_buff = (u8 *)data;
Arnaud Pouliquen55da0942018-01-10 11:13:15 +0100174 unsigned int old_pos = priv->pos;
Olivier Moysan1e7f6e12019-06-19 13:40:02 +0200175 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;
Arnaud Pouliquen55da0942018-01-10 11:13:15 +0100183
benjamin.gaignard@linaro.org8db339d2018-07-06 15:07:03 +0200184 dev_dbg(rtd->dev, "%s: buff_add :%pK, pos = %d, size = %zu\n",
Olivier Moysan1e7f6e12019-06-19 13:40:02 +0200185 __func__, &pcm_buff[priv->pos], priv->pos, src_size);
Arnaud Pouliquen55da0942018-01-10 11:13:15 +0100186
Olivier Moysan1e7f6e12019-06-19 13:40:02 +0200187 if ((priv->pos + src_size) > buff_size) {
188 if (format == SNDRV_PCM_FORMAT_S16_LE)
189 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);
Arnaud Pouliquen55da0942018-01-10 11:13:15 +0100194 cur_size -= buff_size - priv->pos;
195 priv->pos = 0;
196 }
197
Olivier Moysan1e7f6e12019-06-19 13:40:02 +0200198 if (format == SNDRV_PCM_FORMAT_S16_LE)
199 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
Arnaud Pouliquen55da0942018-01-10 11:13:15 +0100205 priv->pos = (priv->pos + cur_size) % buff_size;
206
Olivier Moysan1e7f6e12019-06-19 13:40:02 +0200207 if (cur_size != src_size || (old_pos && (old_pos % period_size < size)))
Arnaud Pouliquen55da0942018-01-10 11:13:15 +0100208 snd_pcm_period_elapsed(priv->substream);
209
210 return 0;
211}
212
213static int stm32_adfsdm_trigger(struct snd_pcm_substream *substream, int cmd)
214{
215 struct snd_soc_pcm_runtime *rtd = substream->private_data;
216 struct stm32_adfsdm_priv *priv =
217 snd_soc_dai_get_drvdata(rtd->cpu_dai);
218
219 switch (cmd) {
220 case SNDRV_PCM_TRIGGER_START:
221 case SNDRV_PCM_TRIGGER_RESUME:
222 priv->pos = 0;
223 return stm32_dfsdm_get_buff_cb(priv->iio_ch->indio_dev,
224 stm32_afsdm_pcm_cb, priv);
225 case SNDRV_PCM_TRIGGER_SUSPEND:
226 case SNDRV_PCM_TRIGGER_STOP:
227 return stm32_dfsdm_release_buff_cb(priv->iio_ch->indio_dev);
228 }
229
230 return -EINVAL;
231}
232
233static int stm32_adfsdm_pcm_open(struct snd_pcm_substream *substream)
234{
235 struct snd_soc_pcm_runtime *rtd = substream->private_data;
236 struct stm32_adfsdm_priv *priv = snd_soc_dai_get_drvdata(rtd->cpu_dai);
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_pcm_substream *substream)
247{
248 struct snd_soc_pcm_runtime *rtd = substream->private_data;
249 struct stm32_adfsdm_priv *priv =
250 snd_soc_dai_get_drvdata(rtd->cpu_dai);
251
252 snd_pcm_lib_free_pages(substream);
253 priv->substream = NULL;
254
255 return 0;
256}
257
258static snd_pcm_uframes_t stm32_adfsdm_pcm_pointer(
259 struct snd_pcm_substream *substream)
260{
261 struct snd_soc_pcm_runtime *rtd = substream->private_data;
262 struct stm32_adfsdm_priv *priv =
263 snd_soc_dai_get_drvdata(rtd->cpu_dai);
264
265 return bytes_to_frames(substream->runtime, priv->pos);
266}
267
268static int stm32_adfsdm_pcm_hw_params(struct snd_pcm_substream *substream,
269 struct snd_pcm_hw_params *params)
270{
271 struct snd_soc_pcm_runtime *rtd = substream->private_data;
272 struct stm32_adfsdm_priv *priv =
273 snd_soc_dai_get_drvdata(rtd->cpu_dai);
274 int ret;
275
276 ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
277 if (ret < 0)
278 return ret;
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
285static int stm32_adfsdm_pcm_hw_free(struct snd_pcm_substream *substream)
286{
287 snd_pcm_lib_free_pages(substream);
288
289 return 0;
290}
291
292static struct snd_pcm_ops stm32_adfsdm_pcm_ops = {
293 .open = stm32_adfsdm_pcm_open,
294 .close = stm32_adfsdm_pcm_close,
295 .hw_params = stm32_adfsdm_pcm_hw_params,
296 .hw_free = stm32_adfsdm_pcm_hw_free,
297 .trigger = stm32_adfsdm_trigger,
298 .pointer = stm32_adfsdm_pcm_pointer,
299};
300
301static int stm32_adfsdm_pcm_new(struct snd_soc_pcm_runtime *rtd)
302{
303 struct snd_pcm *pcm = rtd->pcm;
304 struct stm32_adfsdm_priv *priv =
305 snd_soc_dai_get_drvdata(rtd->cpu_dai);
306 unsigned int size = DFSDM_MAX_PERIODS * DFSDM_MAX_PERIOD_SIZE;
307
Takashi Iwai18183ed2019-02-04 16:40:22 +0100308 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
309 priv->dev, size, size);
310 return 0;
Arnaud Pouliquen55da0942018-01-10 11:13:15 +0100311}
312
313static void stm32_adfsdm_pcm_free(struct snd_pcm *pcm)
314{
315 struct snd_pcm_substream *substream;
Arnaud Pouliquen55da0942018-01-10 11:13:15 +0100316
317 substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
Colin Ian King18127742018-08-01 11:47:40 +0100318 if (substream)
Arnaud Pouliquen55da0942018-01-10 11:13:15 +0100319 snd_pcm_lib_preallocate_free_for_all(pcm);
Arnaud Pouliquen55da0942018-01-10 11:13:15 +0100320}
321
Kuninori Morimoto66bf4282018-01-29 02:51:58 +0000322static struct snd_soc_component_driver stm32_adfsdm_soc_platform = {
Arnaud Pouliquen55da0942018-01-10 11:13:15 +0100323 .ops = &stm32_adfsdm_pcm_ops,
324 .pcm_new = stm32_adfsdm_pcm_new,
325 .pcm_free = stm32_adfsdm_pcm_free,
326};
327
328static const struct of_device_id stm32_adfsdm_of_match[] = {
329 {.compatible = "st,stm32h7-dfsdm-dai"},
330 {}
331};
332MODULE_DEVICE_TABLE(of, stm32_adfsdm_of_match);
333
334static int stm32_adfsdm_probe(struct platform_device *pdev)
335{
336 struct stm32_adfsdm_priv *priv;
Olivier Moysanc47255b2019-03-04 15:52:44 +0100337 struct snd_soc_component *component;
Arnaud Pouliquen55da0942018-01-10 11:13:15 +0100338 int ret;
339
340 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
341 if (!priv)
342 return -ENOMEM;
343
344 priv->dev = &pdev->dev;
345 priv->dai_drv = stm32_adfsdm_dai;
Olivier Moysan19441e32019-03-04 15:52:43 +0100346 mutex_init(&priv->lock);
Arnaud Pouliquen55da0942018-01-10 11:13:15 +0100347
348 dev_set_drvdata(&pdev->dev, priv);
349
350 ret = devm_snd_soc_register_component(&pdev->dev,
351 &stm32_adfsdm_dai_component,
352 &priv->dai_drv, 1);
353 if (ret < 0)
354 return ret;
355
356 /* Associate iio channel */
357 priv->iio_ch = devm_iio_channel_get_all(&pdev->dev);
358 if (IS_ERR(priv->iio_ch))
359 return PTR_ERR(priv->iio_ch);
360
361 priv->iio_cb = iio_channel_get_all_cb(&pdev->dev, NULL, NULL);
362 if (IS_ERR(priv->iio_cb))
Wei Yongjun6dee6722018-01-11 11:11:51 +0000363 return PTR_ERR(priv->iio_cb);
Arnaud Pouliquen55da0942018-01-10 11:13:15 +0100364
Olivier Moysanc47255b2019-03-04 15:52:44 +0100365 component = devm_kzalloc(&pdev->dev, sizeof(*component), GFP_KERNEL);
366 if (!component)
367 return -ENOMEM;
368#ifdef CONFIG_DEBUG_FS
369 component->debugfs_prefix = "pcm";
370#endif
371
372 ret = snd_soc_add_component(&pdev->dev, component,
373 &stm32_adfsdm_soc_platform, NULL, 0);
Arnaud Pouliquen55da0942018-01-10 11:13:15 +0100374 if (ret < 0)
375 dev_err(&pdev->dev, "%s: Failed to register PCM platform\n",
376 __func__);
377
378 return ret;
379}
380
Olivier Moysanc47255b2019-03-04 15:52:44 +0100381static int stm32_adfsdm_remove(struct platform_device *pdev)
382{
383 snd_soc_unregister_component(&pdev->dev);
384
385 return 0;
386}
387
Arnaud Pouliquen55da0942018-01-10 11:13:15 +0100388static struct platform_driver stm32_adfsdm_driver = {
389 .driver = {
390 .name = STM32_ADFSDM_DRV_NAME,
391 .of_match_table = stm32_adfsdm_of_match,
392 },
393 .probe = stm32_adfsdm_probe,
Olivier Moysanc47255b2019-03-04 15:52:44 +0100394 .remove = stm32_adfsdm_remove,
Arnaud Pouliquen55da0942018-01-10 11:13:15 +0100395};
396
397module_platform_driver(stm32_adfsdm_driver);
398
399MODULE_DESCRIPTION("stm32 DFSDM DAI driver");
400MODULE_AUTHOR("Arnaud Pouliquen <arnaud.pouliquen@st.com>");
401MODULE_LICENSE("GPL v2");
402MODULE_ALIAS("platform:" STM32_ADFSDM_DRV_NAME);