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_PAUSE,
48 .formats = 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
stm32_adfsdm_shutdown(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)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
stm32_adfsdm_dai_prepare(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)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
stm32_adfsdm_set_sysclk(struct snd_soc_dai * dai,int clk_id,unsigned int freq,int dir)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_S32_LE,
145 .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
146 SNDRV_PCM_RATE_32000),
147 },
148 .ops = &stm32_adfsdm_dai_ops,
149 };
150
151 static const struct snd_soc_component_driver stm32_adfsdm_dai_component = {
152 .name = "stm32_dfsdm_audio",
153 };
154
stm32_afsdm_pcm_cb(const void * data,size_t size,void * private)155 static int stm32_afsdm_pcm_cb(const void *data, size_t size, void *private)
156 {
157 struct stm32_adfsdm_priv *priv = private;
158 struct snd_soc_pcm_runtime *rtd = priv->substream->private_data;
159 u8 *pcm_buff = priv->pcm_buff;
160 u8 *src_buff = (u8 *)data;
161 unsigned int buff_size = snd_pcm_lib_buffer_bytes(priv->substream);
162 unsigned int period_size = snd_pcm_lib_period_bytes(priv->substream);
163 unsigned int old_pos = priv->pos;
164 unsigned int cur_size = size;
165
166 dev_dbg(rtd->dev, "%s: buff_add :%pK, pos = %d, size = %zu\n",
167 __func__, &pcm_buff[priv->pos], priv->pos, size);
168
169 if ((priv->pos + size) > buff_size) {
170 memcpy(&pcm_buff[priv->pos], src_buff, buff_size - priv->pos);
171 cur_size -= buff_size - priv->pos;
172 priv->pos = 0;
173 }
174
175 memcpy(&pcm_buff[priv->pos], &src_buff[size - cur_size], cur_size);
176 priv->pos = (priv->pos + cur_size) % buff_size;
177
178 if (cur_size != size || (old_pos && (old_pos % period_size < size)))
179 snd_pcm_period_elapsed(priv->substream);
180
181 return 0;
182 }
183
stm32_adfsdm_trigger(struct snd_pcm_substream * substream,int cmd)184 static int stm32_adfsdm_trigger(struct snd_pcm_substream *substream, int cmd)
185 {
186 struct snd_soc_pcm_runtime *rtd = substream->private_data;
187 struct stm32_adfsdm_priv *priv =
188 snd_soc_dai_get_drvdata(rtd->cpu_dai);
189
190 switch (cmd) {
191 case SNDRV_PCM_TRIGGER_START:
192 case SNDRV_PCM_TRIGGER_RESUME:
193 priv->pos = 0;
194 return stm32_dfsdm_get_buff_cb(priv->iio_ch->indio_dev,
195 stm32_afsdm_pcm_cb, priv);
196 case SNDRV_PCM_TRIGGER_SUSPEND:
197 case SNDRV_PCM_TRIGGER_STOP:
198 return stm32_dfsdm_release_buff_cb(priv->iio_ch->indio_dev);
199 }
200
201 return -EINVAL;
202 }
203
stm32_adfsdm_pcm_open(struct snd_pcm_substream * substream)204 static int stm32_adfsdm_pcm_open(struct snd_pcm_substream *substream)
205 {
206 struct snd_soc_pcm_runtime *rtd = substream->private_data;
207 struct stm32_adfsdm_priv *priv = snd_soc_dai_get_drvdata(rtd->cpu_dai);
208 int ret;
209
210 ret = snd_soc_set_runtime_hwparams(substream, &stm32_adfsdm_pcm_hw);
211 if (!ret)
212 priv->substream = substream;
213
214 return ret;
215 }
216
stm32_adfsdm_pcm_close(struct snd_pcm_substream * substream)217 static int stm32_adfsdm_pcm_close(struct snd_pcm_substream *substream)
218 {
219 struct snd_soc_pcm_runtime *rtd = substream->private_data;
220 struct stm32_adfsdm_priv *priv =
221 snd_soc_dai_get_drvdata(rtd->cpu_dai);
222
223 snd_pcm_lib_free_pages(substream);
224 priv->substream = NULL;
225
226 return 0;
227 }
228
stm32_adfsdm_pcm_pointer(struct snd_pcm_substream * substream)229 static snd_pcm_uframes_t stm32_adfsdm_pcm_pointer(
230 struct snd_pcm_substream *substream)
231 {
232 struct snd_soc_pcm_runtime *rtd = substream->private_data;
233 struct stm32_adfsdm_priv *priv =
234 snd_soc_dai_get_drvdata(rtd->cpu_dai);
235
236 return bytes_to_frames(substream->runtime, priv->pos);
237 }
238
stm32_adfsdm_pcm_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)239 static int stm32_adfsdm_pcm_hw_params(struct snd_pcm_substream *substream,
240 struct snd_pcm_hw_params *params)
241 {
242 struct snd_soc_pcm_runtime *rtd = substream->private_data;
243 struct stm32_adfsdm_priv *priv =
244 snd_soc_dai_get_drvdata(rtd->cpu_dai);
245 int ret;
246
247 ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
248 if (ret < 0)
249 return ret;
250 priv->pcm_buff = substream->runtime->dma_area;
251
252 return iio_channel_cb_set_buffer_watermark(priv->iio_cb,
253 params_period_size(params));
254 }
255
stm32_adfsdm_pcm_hw_free(struct snd_pcm_substream * substream)256 static int stm32_adfsdm_pcm_hw_free(struct snd_pcm_substream *substream)
257 {
258 snd_pcm_lib_free_pages(substream);
259
260 return 0;
261 }
262
263 static struct snd_pcm_ops stm32_adfsdm_pcm_ops = {
264 .open = stm32_adfsdm_pcm_open,
265 .close = stm32_adfsdm_pcm_close,
266 .hw_params = stm32_adfsdm_pcm_hw_params,
267 .hw_free = stm32_adfsdm_pcm_hw_free,
268 .trigger = stm32_adfsdm_trigger,
269 .pointer = stm32_adfsdm_pcm_pointer,
270 };
271
stm32_adfsdm_pcm_new(struct snd_soc_pcm_runtime * rtd)272 static int stm32_adfsdm_pcm_new(struct snd_soc_pcm_runtime *rtd)
273 {
274 struct snd_pcm *pcm = rtd->pcm;
275 struct stm32_adfsdm_priv *priv =
276 snd_soc_dai_get_drvdata(rtd->cpu_dai);
277 unsigned int size = DFSDM_MAX_PERIODS * DFSDM_MAX_PERIOD_SIZE;
278
279 return snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
280 priv->dev, size, size);
281 }
282
stm32_adfsdm_pcm_free(struct snd_pcm * pcm)283 static void stm32_adfsdm_pcm_free(struct snd_pcm *pcm)
284 {
285 struct snd_pcm_substream *substream;
286
287 substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
288 if (substream)
289 snd_pcm_lib_preallocate_free_for_all(pcm);
290 }
291
292 static struct snd_soc_component_driver stm32_adfsdm_soc_platform = {
293 .ops = &stm32_adfsdm_pcm_ops,
294 .pcm_new = stm32_adfsdm_pcm_new,
295 .pcm_free = stm32_adfsdm_pcm_free,
296 };
297
298 static const struct of_device_id stm32_adfsdm_of_match[] = {
299 {.compatible = "st,stm32h7-dfsdm-dai"},
300 {}
301 };
302 MODULE_DEVICE_TABLE(of, stm32_adfsdm_of_match);
303
stm32_adfsdm_probe(struct platform_device * pdev)304 static int stm32_adfsdm_probe(struct platform_device *pdev)
305 {
306 struct stm32_adfsdm_priv *priv;
307 struct snd_soc_component *component;
308 int ret;
309
310 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
311 if (!priv)
312 return -ENOMEM;
313
314 priv->dev = &pdev->dev;
315 priv->dai_drv = stm32_adfsdm_dai;
316 mutex_init(&priv->lock);
317
318 dev_set_drvdata(&pdev->dev, priv);
319
320 ret = devm_snd_soc_register_component(&pdev->dev,
321 &stm32_adfsdm_dai_component,
322 &priv->dai_drv, 1);
323 if (ret < 0)
324 return ret;
325
326 /* Associate iio channel */
327 priv->iio_ch = devm_iio_channel_get_all(&pdev->dev);
328 if (IS_ERR(priv->iio_ch))
329 return PTR_ERR(priv->iio_ch);
330
331 priv->iio_cb = iio_channel_get_all_cb(&pdev->dev, NULL, NULL);
332 if (IS_ERR(priv->iio_cb))
333 return PTR_ERR(priv->iio_cb);
334
335 component = devm_kzalloc(&pdev->dev, sizeof(*component), GFP_KERNEL);
336 if (!component)
337 return -ENOMEM;
338 #ifdef CONFIG_DEBUG_FS
339 component->debugfs_prefix = "pcm";
340 #endif
341
342 ret = snd_soc_add_component(&pdev->dev, component,
343 &stm32_adfsdm_soc_platform, NULL, 0);
344 if (ret < 0)
345 dev_err(&pdev->dev, "%s: Failed to register PCM platform\n",
346 __func__);
347
348 return ret;
349 }
350
stm32_adfsdm_remove(struct platform_device * pdev)351 static int stm32_adfsdm_remove(struct platform_device *pdev)
352 {
353 snd_soc_unregister_component(&pdev->dev);
354
355 return 0;
356 }
357
358 static struct platform_driver stm32_adfsdm_driver = {
359 .driver = {
360 .name = STM32_ADFSDM_DRV_NAME,
361 .of_match_table = stm32_adfsdm_of_match,
362 },
363 .probe = stm32_adfsdm_probe,
364 .remove = stm32_adfsdm_remove,
365 };
366
367 module_platform_driver(stm32_adfsdm_driver);
368
369 MODULE_DESCRIPTION("stm32 DFSDM DAI driver");
370 MODULE_AUTHOR("Arnaud Pouliquen <arnaud.pouliquen@st.com>");
371 MODULE_LICENSE("GPL v2");
372 MODULE_ALIAS("platform:" STM32_ADFSDM_DRV_NAME);
373