2 * Intel Baytrail SST PCM Support
3 * Copyright (c) 2014, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 #include <linux/module.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/slab.h>
18 #include <sound/core.h>
19 #include <sound/pcm.h>
20 #include <sound/pcm_params.h>
21 #include <sound/soc.h>
22 #include "sst-baytrail-ipc.h"
23 #include "sst-dsp-priv.h"
26 #define BYT_PCM_COUNT 2
28 static const struct snd_pcm_hardware sst_byt_pcm_hardware = {
29 .info = SNDRV_PCM_INFO_MMAP |
30 SNDRV_PCM_INFO_MMAP_VALID |
31 SNDRV_PCM_INFO_INTERLEAVED |
32 SNDRV_PCM_INFO_PAUSE |
33 SNDRV_PCM_INFO_RESUME,
34 .formats = SNDRV_PCM_FMTBIT_S16_LE |
35 SNDRV_PCM_FORMAT_S24_LE,
36 .period_bytes_min = 384,
37 .period_bytes_max = 48000,
40 .buffer_bytes_max = 96000,
43 /* private data for each PCM DSP stream */
44 struct sst_byt_pcm_data {
45 struct sst_byt_stream *stream;
46 struct snd_pcm_substream *substream;
50 /* private data for the driver */
51 struct sst_byt_priv_data {
56 struct sst_byt_pcm_data pcm[BYT_PCM_COUNT];
59 /* this may get called several times by oss emulation */
60 static int sst_byt_pcm_hw_params(struct snd_pcm_substream *substream,
61 struct snd_pcm_hw_params *params)
63 struct snd_soc_pcm_runtime *rtd = substream->private_data;
64 struct sst_byt_priv_data *pdata =
65 snd_soc_platform_get_drvdata(rtd->platform);
66 struct sst_byt_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
67 struct sst_byt *byt = pdata->byt;
70 int ret, playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
72 dev_dbg(rtd->dev, "PCM: hw_params, pcm_data %p\n", pcm_data);
74 ret = sst_byt_stream_type(byt, pcm_data->stream,
77 dev_err(rtd->dev, "failed to set stream format %d\n", ret);
81 rate = params_rate(params);
82 ret = sst_byt_stream_set_rate(byt, pcm_data->stream, rate);
84 dev_err(rtd->dev, "could not set rate %d\n", rate);
88 bits = snd_pcm_format_width(params_format(params));
89 ret = sst_byt_stream_set_bits(byt, pcm_data->stream, bits);
91 dev_err(rtd->dev, "could not set formats %d\n",
96 channels = (u8)(params_channels(params) & 0xF);
97 ret = sst_byt_stream_set_channels(byt, pcm_data->stream, channels);
99 dev_err(rtd->dev, "could not set channels %d\n",
100 params_rate(params));
104 snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
106 ret = sst_byt_stream_buffer(byt, pcm_data->stream,
107 substream->dma_buffer.addr,
108 params_buffer_bytes(params));
110 dev_err(rtd->dev, "PCM: failed to set DMA buffer %d\n", ret);
114 ret = sst_byt_stream_commit(byt, pcm_data->stream);
116 dev_err(rtd->dev, "PCM: failed stream commit %d\n", ret);
123 static int sst_byt_pcm_hw_free(struct snd_pcm_substream *substream)
125 struct snd_soc_pcm_runtime *rtd = substream->private_data;
127 dev_dbg(rtd->dev, "PCM: hw_free\n");
128 snd_pcm_lib_free_pages(substream);
133 static int sst_byt_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
135 struct snd_soc_pcm_runtime *rtd = substream->private_data;
136 struct sst_byt_priv_data *pdata =
137 snd_soc_platform_get_drvdata(rtd->platform);
138 struct sst_byt_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
139 struct sst_byt *byt = pdata->byt;
141 dev_dbg(rtd->dev, "PCM: trigger %d\n", cmd);
144 case SNDRV_PCM_TRIGGER_START:
145 sst_byt_stream_start(byt, pcm_data->stream);
147 case SNDRV_PCM_TRIGGER_RESUME:
148 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
149 sst_byt_stream_resume(byt, pcm_data->stream);
151 case SNDRV_PCM_TRIGGER_STOP:
152 sst_byt_stream_stop(byt, pcm_data->stream);
154 case SNDRV_PCM_TRIGGER_SUSPEND:
155 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
156 sst_byt_stream_pause(byt, pcm_data->stream);
165 static u32 byt_notify_pointer(struct sst_byt_stream *stream, void *data)
167 struct sst_byt_pcm_data *pcm_data = data;
168 struct snd_pcm_substream *substream = pcm_data->substream;
169 struct snd_pcm_runtime *runtime = substream->runtime;
170 struct snd_soc_pcm_runtime *rtd = substream->private_data;
173 pos = frames_to_bytes(runtime,
174 (runtime->control->appl_ptr %
175 runtime->buffer_size));
177 dev_dbg(rtd->dev, "PCM: App pointer %d bytes\n", pos);
179 snd_pcm_period_elapsed(substream);
183 static snd_pcm_uframes_t sst_byt_pcm_pointer(struct snd_pcm_substream *substream)
185 struct snd_soc_pcm_runtime *rtd = substream->private_data;
186 struct snd_pcm_runtime *runtime = substream->runtime;
187 struct sst_byt_priv_data *pdata =
188 snd_soc_platform_get_drvdata(rtd->platform);
189 struct sst_byt_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
190 struct sst_byt *byt = pdata->byt;
191 snd_pcm_uframes_t offset;
194 pos = sst_byt_get_dsp_position(byt, pcm_data->stream,
195 snd_pcm_lib_buffer_bytes(substream));
196 offset = bytes_to_frames(runtime, pos);
198 dev_dbg(rtd->dev, "PCM: DMA pointer %zu bytes\n",
199 frames_to_bytes(runtime, (u32)offset));
203 static int sst_byt_pcm_open(struct snd_pcm_substream *substream)
205 struct snd_soc_pcm_runtime *rtd = substream->private_data;
206 struct sst_byt_priv_data *pdata =
207 snd_soc_platform_get_drvdata(rtd->platform);
208 struct sst_byt_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
209 struct sst_byt *byt = pdata->byt;
211 dev_dbg(rtd->dev, "PCM: open\n");
213 pcm_data = &pdata->pcm[rtd->cpu_dai->id];
214 mutex_lock(&pcm_data->mutex);
216 snd_soc_pcm_set_drvdata(rtd, pcm_data);
217 pcm_data->substream = substream;
219 snd_soc_set_runtime_hwparams(substream, &sst_byt_pcm_hardware);
221 pcm_data->stream = sst_byt_stream_new(byt, rtd->cpu_dai->id + 1,
222 byt_notify_pointer, pcm_data);
223 if (pcm_data->stream == NULL) {
224 dev_err(rtd->dev, "failed to create stream\n");
225 mutex_unlock(&pcm_data->mutex);
229 mutex_unlock(&pcm_data->mutex);
233 static int sst_byt_pcm_close(struct snd_pcm_substream *substream)
235 struct snd_soc_pcm_runtime *rtd = substream->private_data;
236 struct sst_byt_priv_data *pdata =
237 snd_soc_platform_get_drvdata(rtd->platform);
238 struct sst_byt_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
239 struct sst_byt *byt = pdata->byt;
242 dev_dbg(rtd->dev, "PCM: close\n");
244 mutex_lock(&pcm_data->mutex);
245 ret = sst_byt_stream_free(byt, pcm_data->stream);
247 dev_dbg(rtd->dev, "Free stream fail\n");
250 pcm_data->stream = NULL;
253 mutex_unlock(&pcm_data->mutex);
257 static int sst_byt_pcm_mmap(struct snd_pcm_substream *substream,
258 struct vm_area_struct *vma)
260 struct snd_soc_pcm_runtime *rtd = substream->private_data;
262 dev_dbg(rtd->dev, "PCM: mmap\n");
263 return snd_pcm_lib_default_mmap(substream, vma);
266 static struct snd_pcm_ops sst_byt_pcm_ops = {
267 .open = sst_byt_pcm_open,
268 .close = sst_byt_pcm_close,
269 .ioctl = snd_pcm_lib_ioctl,
270 .hw_params = sst_byt_pcm_hw_params,
271 .hw_free = sst_byt_pcm_hw_free,
272 .trigger = sst_byt_pcm_trigger,
273 .pointer = sst_byt_pcm_pointer,
274 .mmap = sst_byt_pcm_mmap,
277 static void sst_byt_pcm_free(struct snd_pcm *pcm)
279 snd_pcm_lib_preallocate_free_for_all(pcm);
282 static int sst_byt_pcm_new(struct snd_soc_pcm_runtime *rtd)
284 struct snd_pcm *pcm = rtd->pcm;
288 ret = dma_coerce_mask_and_coherent(rtd->card->dev, DMA_BIT_MASK(32));
292 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream ||
293 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
294 size = sst_byt_pcm_hardware.buffer_bytes_max;
295 ret = snd_pcm_lib_preallocate_pages_for_all(pcm,
300 dev_err(rtd->dev, "dma buffer allocation failed %d\n",
309 static struct snd_soc_dai_driver byt_dais[] = {
311 .name = "Front-cpu-dai",
313 .stream_name = "System Playback",
316 .rates = SNDRV_PCM_RATE_48000,
317 .formats = SNDRV_PCM_FMTBIT_S24_3LE |
318 SNDRV_PCM_FMTBIT_S16_LE,
322 .name = "Mic1-cpu-dai",
324 .stream_name = "Analog Capture",
327 .rates = SNDRV_PCM_RATE_48000,
328 .formats = SNDRV_PCM_FMTBIT_S16_LE,
333 static int sst_byt_pcm_probe(struct snd_soc_platform *platform)
335 struct sst_pdata *plat_data = dev_get_platdata(platform->dev);
336 struct sst_byt_priv_data *priv_data;
342 priv_data = devm_kzalloc(platform->dev, sizeof(*priv_data),
344 priv_data->byt = plat_data->dsp;
345 snd_soc_platform_set_drvdata(platform, priv_data);
347 for (i = 0; i < ARRAY_SIZE(byt_dais); i++)
348 mutex_init(&priv_data->pcm[i].mutex);
353 static int sst_byt_pcm_remove(struct snd_soc_platform *platform)
358 static struct snd_soc_platform_driver byt_soc_platform = {
359 .probe = sst_byt_pcm_probe,
360 .remove = sst_byt_pcm_remove,
361 .ops = &sst_byt_pcm_ops,
362 .pcm_new = sst_byt_pcm_new,
363 .pcm_free = sst_byt_pcm_free,
366 static const struct snd_soc_component_driver byt_dai_component = {
370 static int sst_byt_pcm_dev_probe(struct platform_device *pdev)
372 struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
375 ret = sst_byt_dsp_init(&pdev->dev, sst_pdata);
379 ret = snd_soc_register_platform(&pdev->dev, &byt_soc_platform);
383 ret = snd_soc_register_component(&pdev->dev, &byt_dai_component,
384 byt_dais, ARRAY_SIZE(byt_dais));
391 snd_soc_unregister_platform(&pdev->dev);
393 sst_byt_dsp_free(&pdev->dev, sst_pdata);
397 static int sst_byt_pcm_dev_remove(struct platform_device *pdev)
399 struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
401 snd_soc_unregister_platform(&pdev->dev);
402 snd_soc_unregister_component(&pdev->dev);
403 sst_byt_dsp_free(&pdev->dev, sst_pdata);
408 static struct platform_driver sst_byt_pcm_driver = {
410 .name = "baytrail-pcm-audio",
411 .owner = THIS_MODULE,
414 .probe = sst_byt_pcm_dev_probe,
415 .remove = sst_byt_pcm_dev_remove,
417 module_platform_driver(sst_byt_pcm_driver);
419 MODULE_AUTHOR("Jarkko Nikula");
420 MODULE_DESCRIPTION("Baytrail PCM");
421 MODULE_LICENSE("GPL v2");
422 MODULE_ALIAS("platform:baytrail-pcm-audio");