blob: b8a89e9f753d40d7cb3842cc94d73d0bd80df235 [file] [log] [blame]
Mark Browna4b12992014-03-12 23:04:35 +00001/*
2 * Intel Baytrail SST PCM Support
3 * Copyright (c) 2014, Intel Corporation.
4 *
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.
8 *
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
12 * more details.
13 */
14
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"
24#include "sst-dsp.h"
25
26#define BYT_PCM_COUNT 2
27
28static 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,
38 .periods_min = 2,
39 .periods_max = 250,
40 .buffer_bytes_max = 96000,
41};
42
43/* private data for each PCM DSP stream */
44struct sst_byt_pcm_data {
45 struct sst_byt_stream *stream;
46 struct snd_pcm_substream *substream;
47 struct mutex mutex;
Jarkko Nikulac83649e2014-05-08 16:07:21 +030048
49 /* latest DSP DMA hw pointer */
50 u32 hw_ptr;
Mark Browna4b12992014-03-12 23:04:35 +000051};
52
53/* private data for the driver */
54struct sst_byt_priv_data {
55 /* runtime DSP */
56 struct sst_byt *byt;
57
58 /* DAI data */
59 struct sst_byt_pcm_data pcm[BYT_PCM_COUNT];
60};
61
62/* this may get called several times by oss emulation */
63static int sst_byt_pcm_hw_params(struct snd_pcm_substream *substream,
64 struct snd_pcm_hw_params *params)
65{
66 struct snd_soc_pcm_runtime *rtd = substream->private_data;
67 struct sst_byt_priv_data *pdata =
68 snd_soc_platform_get_drvdata(rtd->platform);
69 struct sst_byt_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
70 struct sst_byt *byt = pdata->byt;
71 u32 rate, bits;
72 u8 channels;
73 int ret, playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
74
75 dev_dbg(rtd->dev, "PCM: hw_params, pcm_data %p\n", pcm_data);
76
77 ret = sst_byt_stream_type(byt, pcm_data->stream,
78 1, 1, !playback);
79 if (ret < 0) {
80 dev_err(rtd->dev, "failed to set stream format %d\n", ret);
81 return ret;
82 }
83
84 rate = params_rate(params);
85 ret = sst_byt_stream_set_rate(byt, pcm_data->stream, rate);
86 if (ret < 0) {
87 dev_err(rtd->dev, "could not set rate %d\n", rate);
88 return ret;
89 }
90
91 bits = snd_pcm_format_width(params_format(params));
92 ret = sst_byt_stream_set_bits(byt, pcm_data->stream, bits);
93 if (ret < 0) {
94 dev_err(rtd->dev, "could not set formats %d\n",
95 params_rate(params));
96 return ret;
97 }
98
99 channels = (u8)(params_channels(params) & 0xF);
100 ret = sst_byt_stream_set_channels(byt, pcm_data->stream, channels);
101 if (ret < 0) {
102 dev_err(rtd->dev, "could not set channels %d\n",
103 params_rate(params));
104 return ret;
105 }
106
107 snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
108
109 ret = sst_byt_stream_buffer(byt, pcm_data->stream,
110 substream->dma_buffer.addr,
111 params_buffer_bytes(params));
112 if (ret < 0) {
113 dev_err(rtd->dev, "PCM: failed to set DMA buffer %d\n", ret);
114 return ret;
115 }
116
117 ret = sst_byt_stream_commit(byt, pcm_data->stream);
118 if (ret < 0) {
119 dev_err(rtd->dev, "PCM: failed stream commit %d\n", ret);
120 return ret;
121 }
122
123 return 0;
124}
125
126static int sst_byt_pcm_hw_free(struct snd_pcm_substream *substream)
127{
128 struct snd_soc_pcm_runtime *rtd = substream->private_data;
129
130 dev_dbg(rtd->dev, "PCM: hw_free\n");
131 snd_pcm_lib_free_pages(substream);
132
133 return 0;
134}
135
136static int sst_byt_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
137{
138 struct snd_soc_pcm_runtime *rtd = substream->private_data;
139 struct sst_byt_priv_data *pdata =
140 snd_soc_platform_get_drvdata(rtd->platform);
141 struct sst_byt_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
142 struct sst_byt *byt = pdata->byt;
143
144 dev_dbg(rtd->dev, "PCM: trigger %d\n", cmd);
145
146 switch (cmd) {
147 case SNDRV_PCM_TRIGGER_START:
148 sst_byt_stream_start(byt, pcm_data->stream);
149 break;
150 case SNDRV_PCM_TRIGGER_RESUME:
151 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
152 sst_byt_stream_resume(byt, pcm_data->stream);
153 break;
154 case SNDRV_PCM_TRIGGER_STOP:
155 sst_byt_stream_stop(byt, pcm_data->stream);
156 break;
157 case SNDRV_PCM_TRIGGER_SUSPEND:
158 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
159 sst_byt_stream_pause(byt, pcm_data->stream);
160 break;
161 default:
162 break;
163 }
164
165 return 0;
166}
167
168static u32 byt_notify_pointer(struct sst_byt_stream *stream, void *data)
169{
170 struct sst_byt_pcm_data *pcm_data = data;
171 struct snd_pcm_substream *substream = pcm_data->substream;
172 struct snd_pcm_runtime *runtime = substream->runtime;
173 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Jarkko Nikulac83649e2014-05-08 16:07:21 +0300174 struct sst_byt_priv_data *pdata =
175 snd_soc_platform_get_drvdata(rtd->platform);
176 struct sst_byt *byt = pdata->byt;
177 u32 pos, hw_pos;
Mark Browna4b12992014-03-12 23:04:35 +0000178
Jarkko Nikulac83649e2014-05-08 16:07:21 +0300179 hw_pos = sst_byt_get_dsp_position(byt, pcm_data->stream,
180 snd_pcm_lib_buffer_bytes(substream));
181 pcm_data->hw_ptr = hw_pos;
Mark Browna4b12992014-03-12 23:04:35 +0000182 pos = frames_to_bytes(runtime,
183 (runtime->control->appl_ptr %
184 runtime->buffer_size));
185
Jarkko Nikulac83649e2014-05-08 16:07:21 +0300186 dev_dbg(rtd->dev, "PCM: App/DMA pointer %u/%u bytes\n", pos, hw_pos);
Mark Browna4b12992014-03-12 23:04:35 +0000187
188 snd_pcm_period_elapsed(substream);
189 return pos;
190}
191
192static snd_pcm_uframes_t sst_byt_pcm_pointer(struct snd_pcm_substream *substream)
193{
194 struct snd_soc_pcm_runtime *rtd = substream->private_data;
195 struct snd_pcm_runtime *runtime = substream->runtime;
Mark Browna4b12992014-03-12 23:04:35 +0000196 struct sst_byt_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
Mark Browna4b12992014-03-12 23:04:35 +0000197
Jarkko Nikulac83649e2014-05-08 16:07:21 +0300198 dev_dbg(rtd->dev, "PCM: DMA pointer %u bytes\n", pcm_data->hw_ptr);
Mark Browna4b12992014-03-12 23:04:35 +0000199
Jarkko Nikulac83649e2014-05-08 16:07:21 +0300200 return bytes_to_frames(runtime, pcm_data->hw_ptr);
Mark Browna4b12992014-03-12 23:04:35 +0000201}
202
203static int sst_byt_pcm_open(struct snd_pcm_substream *substream)
204{
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;
210
211 dev_dbg(rtd->dev, "PCM: open\n");
212
213 pcm_data = &pdata->pcm[rtd->cpu_dai->id];
214 mutex_lock(&pcm_data->mutex);
215
216 snd_soc_pcm_set_drvdata(rtd, pcm_data);
217 pcm_data->substream = substream;
218
219 snd_soc_set_runtime_hwparams(substream, &sst_byt_pcm_hardware);
220
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);
226 return -EINVAL;
227 }
228
229 mutex_unlock(&pcm_data->mutex);
230 return 0;
231}
232
233static int sst_byt_pcm_close(struct snd_pcm_substream *substream)
234{
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;
240 int ret;
241
242 dev_dbg(rtd->dev, "PCM: close\n");
243
244 mutex_lock(&pcm_data->mutex);
245 ret = sst_byt_stream_free(byt, pcm_data->stream);
246 if (ret < 0) {
247 dev_dbg(rtd->dev, "Free stream fail\n");
248 goto out;
249 }
250 pcm_data->stream = NULL;
251
252out:
253 mutex_unlock(&pcm_data->mutex);
254 return ret;
255}
256
257static int sst_byt_pcm_mmap(struct snd_pcm_substream *substream,
258 struct vm_area_struct *vma)
259{
260 struct snd_soc_pcm_runtime *rtd = substream->private_data;
261
262 dev_dbg(rtd->dev, "PCM: mmap\n");
263 return snd_pcm_lib_default_mmap(substream, vma);
264}
265
266static 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,
275};
276
277static void sst_byt_pcm_free(struct snd_pcm *pcm)
278{
279 snd_pcm_lib_preallocate_free_for_all(pcm);
280}
281
282static int sst_byt_pcm_new(struct snd_soc_pcm_runtime *rtd)
283{
284 struct snd_pcm *pcm = rtd->pcm;
285 size_t size;
286 int ret = 0;
287
288 ret = dma_coerce_mask_and_coherent(rtd->card->dev, DMA_BIT_MASK(32));
289 if (ret)
290 return ret;
291
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,
296 SNDRV_DMA_TYPE_DEV,
297 rtd->card->dev,
298 size, size);
299 if (ret) {
300 dev_err(rtd->dev, "dma buffer allocation failed %d\n",
301 ret);
302 return ret;
303 }
304 }
305
306 return ret;
307}
308
309static struct snd_soc_dai_driver byt_dais[] = {
310 {
311 .name = "Front-cpu-dai",
312 .playback = {
313 .stream_name = "System Playback",
314 .channels_min = 2,
315 .channels_max = 2,
316 .rates = SNDRV_PCM_RATE_48000,
317 .formats = SNDRV_PCM_FMTBIT_S24_3LE |
318 SNDRV_PCM_FMTBIT_S16_LE,
319 },
320 },
321 {
322 .name = "Mic1-cpu-dai",
323 .capture = {
324 .stream_name = "Analog Capture",
325 .channels_min = 2,
326 .channels_max = 2,
327 .rates = SNDRV_PCM_RATE_48000,
328 .formats = SNDRV_PCM_FMTBIT_S16_LE,
329 },
330 },
331};
332
333static int sst_byt_pcm_probe(struct snd_soc_platform *platform)
334{
335 struct sst_pdata *plat_data = dev_get_platdata(platform->dev);
336 struct sst_byt_priv_data *priv_data;
337 int i;
338
339 if (!plat_data)
340 return -ENODEV;
341
342 priv_data = devm_kzalloc(platform->dev, sizeof(*priv_data),
343 GFP_KERNEL);
344 priv_data->byt = plat_data->dsp;
345 snd_soc_platform_set_drvdata(platform, priv_data);
346
347 for (i = 0; i < ARRAY_SIZE(byt_dais); i++)
348 mutex_init(&priv_data->pcm[i].mutex);
349
350 return 0;
351}
352
353static int sst_byt_pcm_remove(struct snd_soc_platform *platform)
354{
355 return 0;
356}
357
358static 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,
364};
365
366static const struct snd_soc_component_driver byt_dai_component = {
367 .name = "byt-dai",
368};
369
370static int sst_byt_pcm_dev_probe(struct platform_device *pdev)
371{
372 struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
373 int ret;
374
375 ret = sst_byt_dsp_init(&pdev->dev, sst_pdata);
376 if (ret < 0)
377 return -ENODEV;
378
379 ret = snd_soc_register_platform(&pdev->dev, &byt_soc_platform);
380 if (ret < 0)
381 goto err_plat;
382
383 ret = snd_soc_register_component(&pdev->dev, &byt_dai_component,
384 byt_dais, ARRAY_SIZE(byt_dais));
385 if (ret < 0)
386 goto err_comp;
387
388 return 0;
389
390err_comp:
391 snd_soc_unregister_platform(&pdev->dev);
392err_plat:
393 sst_byt_dsp_free(&pdev->dev, sst_pdata);
394 return ret;
395}
396
397static int sst_byt_pcm_dev_remove(struct platform_device *pdev)
398{
399 struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
400
401 snd_soc_unregister_platform(&pdev->dev);
402 snd_soc_unregister_component(&pdev->dev);
403 sst_byt_dsp_free(&pdev->dev, sst_pdata);
404
405 return 0;
406}
407
408static struct platform_driver sst_byt_pcm_driver = {
409 .driver = {
410 .name = "baytrail-pcm-audio",
411 .owner = THIS_MODULE,
412 },
413
414 .probe = sst_byt_pcm_dev_probe,
415 .remove = sst_byt_pcm_dev_remove,
416};
417module_platform_driver(sst_byt_pcm_driver);
418
419MODULE_AUTHOR("Jarkko Nikula");
420MODULE_DESCRIPTION("Baytrail PCM");
421MODULE_LICENSE("GPL v2");
422MODULE_ALIAS("platform:baytrail-pcm-audio");