GNU Linux-libre 4.19.286-gnu1
[releases.git] / sound / soc / intel / baytrail / sst-baytrail-pcm.c
1 /*
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 "../common/sst-dsp-priv.h"
24 #include "../common/sst-dsp.h"
25
26 #define DRV_NAME "byt-dai"
27 #define BYT_PCM_COUNT           2
28
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,
39         .periods_min            = 2,
40         .periods_max            = 250,
41         .buffer_bytes_max       = 96000,
42 };
43
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;
48         struct mutex mutex;
49
50         /* latest DSP DMA hw pointer */
51         u32 hw_ptr;
52
53         struct work_struct work;
54 };
55
56 /* private data for the driver */
57 struct sst_byt_priv_data {
58         /* runtime DSP */
59         struct sst_byt *byt;
60
61         /* DAI data */
62         struct sst_byt_pcm_data pcm[BYT_PCM_COUNT];
63
64         /* flag indicating is stream context restore needed after suspend */
65         bool restore_stream;
66 };
67
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)
71 {
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;
77         u32 rate, bits;
78         u8 channels;
79         int ret, playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
80
81         dev_dbg(rtd->dev, "PCM: hw_params, pcm_data %p\n", pcm_data);
82
83         ret = sst_byt_stream_type(byt, pcm_data->stream,
84                                   1, 1, !playback);
85         if (ret < 0) {
86                 dev_err(rtd->dev, "failed to set stream format %d\n", ret);
87                 return ret;
88         }
89
90         rate = params_rate(params);
91         ret = sst_byt_stream_set_rate(byt, pcm_data->stream, rate);
92         if (ret < 0) {
93                 dev_err(rtd->dev, "could not set rate %d\n", rate);
94                 return ret;
95         }
96
97         bits = snd_pcm_format_width(params_format(params));
98         ret = sst_byt_stream_set_bits(byt, pcm_data->stream, bits);
99         if (ret < 0) {
100                 dev_err(rtd->dev, "could not set formats %d\n",
101                         params_rate(params));
102                 return ret;
103         }
104
105         channels = (u8)(params_channels(params) & 0xF);
106         ret = sst_byt_stream_set_channels(byt, pcm_data->stream, channels);
107         if (ret < 0) {
108                 dev_err(rtd->dev, "could not set channels %d\n",
109                         params_rate(params));
110                 return ret;
111         }
112
113         snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
114
115         ret = sst_byt_stream_buffer(byt, pcm_data->stream,
116                                     substream->dma_buffer.addr,
117                                     params_buffer_bytes(params));
118         if (ret < 0) {
119                 dev_err(rtd->dev, "PCM: failed to set DMA buffer %d\n", ret);
120                 return ret;
121         }
122
123         ret = sst_byt_stream_commit(byt, pcm_data->stream);
124         if (ret < 0) {
125                 dev_err(rtd->dev, "PCM: failed stream commit %d\n", ret);
126                 return ret;
127         }
128
129         return 0;
130 }
131
132 static int sst_byt_pcm_hw_free(struct snd_pcm_substream *substream)
133 {
134         struct snd_soc_pcm_runtime *rtd = substream->private_data;
135
136         dev_dbg(rtd->dev, "PCM: hw_free\n");
137         snd_pcm_lib_free_pages(substream);
138
139         return 0;
140 }
141
142 static int sst_byt_pcm_restore_stream_context(struct snd_pcm_substream *substream)
143 {
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;
149         int ret;
150
151         /* commit stream using existing stream params */
152         ret = sst_byt_stream_commit(byt, pcm_data->stream);
153         if (ret < 0) {
154                 dev_err(rtd->dev, "PCM: failed stream commit %d\n", ret);
155                 return ret;
156         }
157
158         sst_byt_stream_start(byt, pcm_data->stream, pcm_data->hw_ptr);
159
160         dev_dbg(rtd->dev, "stream context restored at offset %d\n",
161                 pcm_data->hw_ptr);
162
163         return 0;
164 }
165
166 static void sst_byt_pcm_work(struct work_struct *work)
167 {
168         struct sst_byt_pcm_data *pcm_data =
169                 container_of(work, struct sst_byt_pcm_data, work);
170
171         if (snd_pcm_running(pcm_data->substream))
172                 sst_byt_pcm_restore_stream_context(pcm_data->substream);
173 }
174
175 static int sst_byt_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
176 {
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;
182
183         dev_dbg(rtd->dev, "PCM: trigger %d\n", cmd);
184
185         switch (cmd) {
186         case SNDRV_PCM_TRIGGER_START:
187                 pcm_data->hw_ptr = 0;
188                 sst_byt_stream_start(byt, pcm_data->stream, 0);
189                 break;
190         case SNDRV_PCM_TRIGGER_RESUME:
191                 if (pdata->restore_stream == true)
192                         schedule_work(&pcm_data->work);
193                 else
194                         sst_byt_stream_resume(byt, pcm_data->stream);
195                 break;
196         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
197                 sst_byt_stream_resume(byt, pcm_data->stream);
198                 break;
199         case SNDRV_PCM_TRIGGER_STOP:
200                 sst_byt_stream_stop(byt, pcm_data->stream);
201                 break;
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);
206                 break;
207         default:
208                 break;
209         }
210
211         return 0;
212 }
213
214 static u32 byt_notify_pointer(struct sst_byt_stream *stream, void *data)
215 {
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;
223         u32 pos, hw_pos;
224
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));
231
232         dev_dbg(rtd->dev, "PCM: App/DMA pointer %u/%u bytes\n", pos, hw_pos);
233
234         snd_pcm_period_elapsed(substream);
235         return pos;
236 }
237
238 static snd_pcm_uframes_t sst_byt_pcm_pointer(struct snd_pcm_substream *substream)
239 {
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];
245
246         dev_dbg(rtd->dev, "PCM: DMA pointer %u bytes\n", pcm_data->hw_ptr);
247
248         return bytes_to_frames(runtime, pcm_data->hw_ptr);
249 }
250
251 static int sst_byt_pcm_open(struct snd_pcm_substream *substream)
252 {
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;
258
259         dev_dbg(rtd->dev, "PCM: open\n");
260
261         mutex_lock(&pcm_data->mutex);
262
263         pcm_data->substream = substream;
264
265         snd_soc_set_runtime_hwparams(substream, &sst_byt_pcm_hardware);
266
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);
272                 return -EINVAL;
273         }
274
275         mutex_unlock(&pcm_data->mutex);
276         return 0;
277 }
278
279 static int sst_byt_pcm_close(struct snd_pcm_substream *substream)
280 {
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;
286         int ret;
287
288         dev_dbg(rtd->dev, "PCM: close\n");
289
290         cancel_work_sync(&pcm_data->work);
291         mutex_lock(&pcm_data->mutex);
292         ret = sst_byt_stream_free(byt, pcm_data->stream);
293         if (ret < 0) {
294                 dev_dbg(rtd->dev, "Free stream fail\n");
295                 goto out;
296         }
297         pcm_data->stream = NULL;
298
299 out:
300         mutex_unlock(&pcm_data->mutex);
301         return ret;
302 }
303
304 static int sst_byt_pcm_mmap(struct snd_pcm_substream *substream,
305                             struct vm_area_struct *vma)
306 {
307         struct snd_soc_pcm_runtime *rtd = substream->private_data;
308
309         dev_dbg(rtd->dev, "PCM: mmap\n");
310         return snd_pcm_lib_default_mmap(substream, vma);
311 }
312
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,
322 };
323
324 static int sst_byt_pcm_new(struct snd_soc_pcm_runtime *rtd)
325 {
326         struct snd_pcm *pcm = rtd->pcm;
327         size_t size;
328         struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
329         struct sst_pdata *pdata = dev_get_platdata(component->dev);
330         int ret = 0;
331
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,
336                                                             SNDRV_DMA_TYPE_DEV,
337                                                             pdata->dma_dev,
338                                                             size, size);
339                 if (ret) {
340                         dev_err(rtd->dev, "dma buffer allocation failed %d\n",
341                                 ret);
342                         return ret;
343                 }
344         }
345
346         return ret;
347 }
348
349 static struct snd_soc_dai_driver byt_dais[] = {
350         {
351                 .name  = "Baytrail PCM",
352                 .playback = {
353                         .stream_name = "System Playback",
354                         .channels_min = 2,
355                         .channels_max = 2,
356                         .rates = SNDRV_PCM_RATE_48000,
357                         .formats = SNDRV_PCM_FMTBIT_S24_3LE |
358                                    SNDRV_PCM_FMTBIT_S16_LE,
359                 },
360                 .capture = {
361                         .stream_name = "Analog Capture",
362                         .channels_min = 2,
363                         .channels_max = 2,
364                         .rates = SNDRV_PCM_RATE_48000,
365                         .formats = SNDRV_PCM_FMTBIT_S16_LE,
366                 },
367         },
368 };
369
370 static int sst_byt_pcm_probe(struct snd_soc_component *component)
371 {
372         struct sst_pdata *plat_data = dev_get_platdata(component->dev);
373         struct sst_byt_priv_data *priv_data;
374         int i;
375
376         if (!plat_data)
377                 return -ENODEV;
378
379         priv_data = devm_kzalloc(component->dev, sizeof(*priv_data),
380                                  GFP_KERNEL);
381         if (!priv_data)
382                 return -ENOMEM;
383         priv_data->byt = plat_data->dsp;
384         snd_soc_component_set_drvdata(component, priv_data);
385
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);
389         }
390
391         return 0;
392 }
393
394 static const struct snd_soc_component_driver byt_dai_component = {
395         .name           = DRV_NAME,
396         .probe          = sst_byt_pcm_probe,
397         .ops            = &sst_byt_pcm_ops,
398         .pcm_new        = sst_byt_pcm_new,
399 };
400
401 #ifdef CONFIG_PM
402 static int sst_byt_pcm_dev_suspend_late(struct device *dev)
403 {
404         struct sst_pdata *sst_pdata = dev_get_platdata(dev);
405         struct sst_byt_priv_data *priv_data = dev_get_drvdata(dev);
406         int ret;
407
408         dev_dbg(dev, "suspending late\n");
409
410         ret = sst_byt_dsp_suspend_late(dev, sst_pdata);
411         if (ret < 0) {
412                 dev_err(dev, "failed to suspend %d\n", ret);
413                 return ret;
414         }
415
416         priv_data->restore_stream = true;
417
418         return ret;
419 }
420
421 static int sst_byt_pcm_dev_resume_early(struct device *dev)
422 {
423         struct sst_pdata *sst_pdata = dev_get_platdata(dev);
424         int ret;
425
426         dev_dbg(dev, "resume early\n");
427
428         /* load fw and boot DSP */
429         ret = sst_byt_dsp_boot(dev, sst_pdata);
430         if (ret)
431                 return ret;
432
433         /* wait for FW to finish booting */
434         return sst_byt_dsp_wait_for_ready(dev, sst_pdata);
435 }
436
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,
440 };
441
442 #define SST_BYT_PM_OPS  (&sst_byt_pm_ops)
443 #else
444 #define SST_BYT_PM_OPS  NULL
445 #endif
446
447 static int sst_byt_pcm_dev_probe(struct platform_device *pdev)
448 {
449         struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
450         int ret;
451
452         ret = sst_byt_dsp_init(&pdev->dev, sst_pdata);
453         if (ret < 0)
454                 return -ENODEV;
455
456         ret = devm_snd_soc_register_component(&pdev->dev, &byt_dai_component,
457                                          byt_dais, ARRAY_SIZE(byt_dais));
458         if (ret < 0)
459                 goto err_plat;
460
461         return 0;
462
463 err_plat:
464         sst_byt_dsp_free(&pdev->dev, sst_pdata);
465         return ret;
466 }
467
468 static int sst_byt_pcm_dev_remove(struct platform_device *pdev)
469 {
470         struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
471
472         sst_byt_dsp_free(&pdev->dev, sst_pdata);
473
474         return 0;
475 }
476
477 static struct platform_driver sst_byt_pcm_driver = {
478         .driver = {
479                 .name = "baytrail-pcm-audio",
480                 .pm = SST_BYT_PM_OPS,
481         },
482
483         .probe = sst_byt_pcm_dev_probe,
484         .remove = sst_byt_pcm_dev_remove,
485 };
486 module_platform_driver(sst_byt_pcm_driver);
487
488 MODULE_AUTHOR("Jarkko Nikula");
489 MODULE_DESCRIPTION("Baytrail PCM");
490 MODULE_LICENSE("GPL v2");
491 MODULE_ALIAS("platform:baytrail-pcm-audio");