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 "../common/sst-dsp-priv.h"
24 #include "../common/sst-dsp.h"
26 #define DRV_NAME "byt-dai"
27 #define BYT_PCM_COUNT 2
29 static const struct snd_pcm_hardware sst_byt_pcm_hardware = {
30 .info = SNDRV_PCM_INFO_MMAP |
31 SNDRV_PCM_INFO_MMAP_VALID |
32 SNDRV_PCM_INFO_INTERLEAVED |
33 SNDRV_PCM_INFO_PAUSE |
34 SNDRV_PCM_INFO_RESUME,
35 .formats = SNDRV_PCM_FMTBIT_S16_LE |
36 SNDRV_PCM_FMTBIT_S24_LE,
37 .period_bytes_min = 384,
38 .period_bytes_max = 48000,
41 .buffer_bytes_max = 96000,
44 /* private data for each PCM DSP stream */
45 struct sst_byt_pcm_data {
46 struct sst_byt_stream *stream;
47 struct snd_pcm_substream *substream;
50 /* latest DSP DMA hw pointer */
53 struct work_struct work;
56 /* private data for the driver */
57 struct sst_byt_priv_data {
62 struct sst_byt_pcm_data pcm[BYT_PCM_COUNT];
64 /* flag indicating is stream context restore needed after suspend */
68 /* this may get called several times by oss emulation */
69 static int sst_byt_pcm_hw_params(struct snd_pcm_substream *substream,
70 struct snd_pcm_hw_params *params)
72 struct snd_soc_pcm_runtime *rtd = substream->private_data;
73 struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
74 struct sst_byt_priv_data *pdata = snd_soc_component_get_drvdata(component);
75 struct sst_byt_pcm_data *pcm_data = &pdata->pcm[substream->stream];
76 struct sst_byt *byt = pdata->byt;
79 int ret, playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
81 dev_dbg(rtd->dev, "PCM: hw_params, pcm_data %p\n", pcm_data);
83 ret = sst_byt_stream_type(byt, pcm_data->stream,
86 dev_err(rtd->dev, "failed to set stream format %d\n", ret);
90 rate = params_rate(params);
91 ret = sst_byt_stream_set_rate(byt, pcm_data->stream, rate);
93 dev_err(rtd->dev, "could not set rate %d\n", rate);
97 bits = snd_pcm_format_width(params_format(params));
98 ret = sst_byt_stream_set_bits(byt, pcm_data->stream, bits);
100 dev_err(rtd->dev, "could not set formats %d\n",
101 params_rate(params));
105 channels = (u8)(params_channels(params) & 0xF);
106 ret = sst_byt_stream_set_channels(byt, pcm_data->stream, channels);
108 dev_err(rtd->dev, "could not set channels %d\n",
109 params_rate(params));
113 snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
115 ret = sst_byt_stream_buffer(byt, pcm_data->stream,
116 substream->dma_buffer.addr,
117 params_buffer_bytes(params));
119 dev_err(rtd->dev, "PCM: failed to set DMA buffer %d\n", ret);
123 ret = sst_byt_stream_commit(byt, pcm_data->stream);
125 dev_err(rtd->dev, "PCM: failed stream commit %d\n", ret);
132 static int sst_byt_pcm_hw_free(struct snd_pcm_substream *substream)
134 struct snd_soc_pcm_runtime *rtd = substream->private_data;
136 dev_dbg(rtd->dev, "PCM: hw_free\n");
137 snd_pcm_lib_free_pages(substream);
142 static int sst_byt_pcm_restore_stream_context(struct snd_pcm_substream *substream)
144 struct snd_soc_pcm_runtime *rtd = substream->private_data;
145 struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
146 struct sst_byt_priv_data *pdata = snd_soc_component_get_drvdata(component);
147 struct sst_byt_pcm_data *pcm_data = &pdata->pcm[substream->stream];
148 struct sst_byt *byt = pdata->byt;
151 /* commit stream using existing stream params */
152 ret = sst_byt_stream_commit(byt, pcm_data->stream);
154 dev_err(rtd->dev, "PCM: failed stream commit %d\n", ret);
158 sst_byt_stream_start(byt, pcm_data->stream, pcm_data->hw_ptr);
160 dev_dbg(rtd->dev, "stream context restored at offset %d\n",
166 static void sst_byt_pcm_work(struct work_struct *work)
168 struct sst_byt_pcm_data *pcm_data =
169 container_of(work, struct sst_byt_pcm_data, work);
171 if (snd_pcm_running(pcm_data->substream))
172 sst_byt_pcm_restore_stream_context(pcm_data->substream);
175 static int sst_byt_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
177 struct snd_soc_pcm_runtime *rtd = substream->private_data;
178 struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
179 struct sst_byt_priv_data *pdata = snd_soc_component_get_drvdata(component);
180 struct sst_byt_pcm_data *pcm_data = &pdata->pcm[substream->stream];
181 struct sst_byt *byt = pdata->byt;
183 dev_dbg(rtd->dev, "PCM: trigger %d\n", cmd);
186 case SNDRV_PCM_TRIGGER_START:
187 pcm_data->hw_ptr = 0;
188 sst_byt_stream_start(byt, pcm_data->stream, 0);
190 case SNDRV_PCM_TRIGGER_RESUME:
191 if (pdata->restore_stream == true)
192 schedule_work(&pcm_data->work);
194 sst_byt_stream_resume(byt, pcm_data->stream);
196 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
197 sst_byt_stream_resume(byt, pcm_data->stream);
199 case SNDRV_PCM_TRIGGER_STOP:
200 sst_byt_stream_stop(byt, pcm_data->stream);
202 case SNDRV_PCM_TRIGGER_SUSPEND:
203 pdata->restore_stream = false;
204 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
205 sst_byt_stream_pause(byt, pcm_data->stream);
214 static u32 byt_notify_pointer(struct sst_byt_stream *stream, void *data)
216 struct sst_byt_pcm_data *pcm_data = data;
217 struct snd_pcm_substream *substream = pcm_data->substream;
218 struct snd_pcm_runtime *runtime = substream->runtime;
219 struct snd_soc_pcm_runtime *rtd = substream->private_data;
220 struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
221 struct sst_byt_priv_data *pdata = snd_soc_component_get_drvdata(component);
222 struct sst_byt *byt = pdata->byt;
225 hw_pos = sst_byt_get_dsp_position(byt, pcm_data->stream,
226 snd_pcm_lib_buffer_bytes(substream));
227 pcm_data->hw_ptr = hw_pos;
228 pos = frames_to_bytes(runtime,
229 (runtime->control->appl_ptr %
230 runtime->buffer_size));
232 dev_dbg(rtd->dev, "PCM: App/DMA pointer %u/%u bytes\n", pos, hw_pos);
234 snd_pcm_period_elapsed(substream);
238 static snd_pcm_uframes_t sst_byt_pcm_pointer(struct snd_pcm_substream *substream)
240 struct snd_soc_pcm_runtime *rtd = substream->private_data;
241 struct snd_pcm_runtime *runtime = substream->runtime;
242 struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
243 struct sst_byt_priv_data *pdata = snd_soc_component_get_drvdata(component);
244 struct sst_byt_pcm_data *pcm_data = &pdata->pcm[substream->stream];
246 dev_dbg(rtd->dev, "PCM: DMA pointer %u bytes\n", pcm_data->hw_ptr);
248 return bytes_to_frames(runtime, pcm_data->hw_ptr);
251 static int sst_byt_pcm_open(struct snd_pcm_substream *substream)
253 struct snd_soc_pcm_runtime *rtd = substream->private_data;
254 struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
255 struct sst_byt_priv_data *pdata = snd_soc_component_get_drvdata(component);
256 struct sst_byt_pcm_data *pcm_data = &pdata->pcm[substream->stream];
257 struct sst_byt *byt = pdata->byt;
259 dev_dbg(rtd->dev, "PCM: open\n");
261 mutex_lock(&pcm_data->mutex);
263 pcm_data->substream = substream;
265 snd_soc_set_runtime_hwparams(substream, &sst_byt_pcm_hardware);
267 pcm_data->stream = sst_byt_stream_new(byt, substream->stream + 1,
268 byt_notify_pointer, pcm_data);
269 if (pcm_data->stream == NULL) {
270 dev_err(rtd->dev, "failed to create stream\n");
271 mutex_unlock(&pcm_data->mutex);
275 mutex_unlock(&pcm_data->mutex);
279 static int sst_byt_pcm_close(struct snd_pcm_substream *substream)
281 struct snd_soc_pcm_runtime *rtd = substream->private_data;
282 struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
283 struct sst_byt_priv_data *pdata = snd_soc_component_get_drvdata(component);
284 struct sst_byt_pcm_data *pcm_data = &pdata->pcm[substream->stream];
285 struct sst_byt *byt = pdata->byt;
288 dev_dbg(rtd->dev, "PCM: close\n");
290 cancel_work_sync(&pcm_data->work);
291 mutex_lock(&pcm_data->mutex);
292 ret = sst_byt_stream_free(byt, pcm_data->stream);
294 dev_dbg(rtd->dev, "Free stream fail\n");
297 pcm_data->stream = NULL;
300 mutex_unlock(&pcm_data->mutex);
304 static int sst_byt_pcm_mmap(struct snd_pcm_substream *substream,
305 struct vm_area_struct *vma)
307 struct snd_soc_pcm_runtime *rtd = substream->private_data;
309 dev_dbg(rtd->dev, "PCM: mmap\n");
310 return snd_pcm_lib_default_mmap(substream, vma);
313 static const struct snd_pcm_ops sst_byt_pcm_ops = {
314 .open = sst_byt_pcm_open,
315 .close = sst_byt_pcm_close,
316 .ioctl = snd_pcm_lib_ioctl,
317 .hw_params = sst_byt_pcm_hw_params,
318 .hw_free = sst_byt_pcm_hw_free,
319 .trigger = sst_byt_pcm_trigger,
320 .pointer = sst_byt_pcm_pointer,
321 .mmap = sst_byt_pcm_mmap,
324 static int sst_byt_pcm_new(struct snd_soc_pcm_runtime *rtd)
326 struct snd_pcm *pcm = rtd->pcm;
328 struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
329 struct sst_pdata *pdata = dev_get_platdata(component->dev);
332 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream ||
333 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
334 size = sst_byt_pcm_hardware.buffer_bytes_max;
335 ret = snd_pcm_lib_preallocate_pages_for_all(pcm,
340 dev_err(rtd->dev, "dma buffer allocation failed %d\n",
349 static struct snd_soc_dai_driver byt_dais[] = {
351 .name = "Baytrail PCM",
353 .stream_name = "System Playback",
356 .rates = SNDRV_PCM_RATE_48000,
357 .formats = SNDRV_PCM_FMTBIT_S24_3LE |
358 SNDRV_PCM_FMTBIT_S16_LE,
361 .stream_name = "Analog Capture",
364 .rates = SNDRV_PCM_RATE_48000,
365 .formats = SNDRV_PCM_FMTBIT_S16_LE,
370 static int sst_byt_pcm_probe(struct snd_soc_component *component)
372 struct sst_pdata *plat_data = dev_get_platdata(component->dev);
373 struct sst_byt_priv_data *priv_data;
379 priv_data = devm_kzalloc(component->dev, sizeof(*priv_data),
383 priv_data->byt = plat_data->dsp;
384 snd_soc_component_set_drvdata(component, priv_data);
386 for (i = 0; i < BYT_PCM_COUNT; i++) {
387 mutex_init(&priv_data->pcm[i].mutex);
388 INIT_WORK(&priv_data->pcm[i].work, sst_byt_pcm_work);
394 static const struct snd_soc_component_driver byt_dai_component = {
396 .probe = sst_byt_pcm_probe,
397 .ops = &sst_byt_pcm_ops,
398 .pcm_new = sst_byt_pcm_new,
402 static int sst_byt_pcm_dev_suspend_late(struct device *dev)
404 struct sst_pdata *sst_pdata = dev_get_platdata(dev);
405 struct sst_byt_priv_data *priv_data = dev_get_drvdata(dev);
408 dev_dbg(dev, "suspending late\n");
410 ret = sst_byt_dsp_suspend_late(dev, sst_pdata);
412 dev_err(dev, "failed to suspend %d\n", ret);
416 priv_data->restore_stream = true;
421 static int sst_byt_pcm_dev_resume_early(struct device *dev)
423 struct sst_pdata *sst_pdata = dev_get_platdata(dev);
426 dev_dbg(dev, "resume early\n");
428 /* load fw and boot DSP */
429 ret = sst_byt_dsp_boot(dev, sst_pdata);
433 /* wait for FW to finish booting */
434 return sst_byt_dsp_wait_for_ready(dev, sst_pdata);
437 static const struct dev_pm_ops sst_byt_pm_ops = {
438 .suspend_late = sst_byt_pcm_dev_suspend_late,
439 .resume_early = sst_byt_pcm_dev_resume_early,
442 #define SST_BYT_PM_OPS (&sst_byt_pm_ops)
444 #define SST_BYT_PM_OPS NULL
447 static int sst_byt_pcm_dev_probe(struct platform_device *pdev)
449 struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
452 ret = sst_byt_dsp_init(&pdev->dev, sst_pdata);
456 ret = devm_snd_soc_register_component(&pdev->dev, &byt_dai_component,
457 byt_dais, ARRAY_SIZE(byt_dais));
464 sst_byt_dsp_free(&pdev->dev, sst_pdata);
468 static int sst_byt_pcm_dev_remove(struct platform_device *pdev)
470 struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
472 sst_byt_dsp_free(&pdev->dev, sst_pdata);
477 static struct platform_driver sst_byt_pcm_driver = {
479 .name = "baytrail-pcm-audio",
480 .pm = SST_BYT_PM_OPS,
483 .probe = sst_byt_pcm_dev_probe,
484 .remove = sst_byt_pcm_dev_remove,
486 module_platform_driver(sst_byt_pcm_driver);
488 MODULE_AUTHOR("Jarkko Nikula");
489 MODULE_DESCRIPTION("Baytrail PCM");
490 MODULE_LICENSE("GPL v2");
491 MODULE_ALIAS("platform:baytrail-pcm-audio");