ASoC: Intel: Fix Audio DSP usage when IOMMU is enabled.
[firefly-linux-kernel-4.4.55.git] / sound / soc / intel / sst-haswell-pcm.c
1 /*
2  * Intel SST Haswell/Broadwell PCM Support
3  *
4  * Copyright (C) 2013, Intel Corporation. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License version
8  * 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16
17 #include <linux/module.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/slab.h>
20 #include <linux/module.h>
21 #include <linux/delay.h>
22 #include <asm/page.h>
23 #include <asm/pgtable.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/dmaengine_pcm.h>
28 #include <sound/soc.h>
29 #include <sound/tlv.h>
30 #include <sound/compress_driver.h>
31
32 #include "sst-haswell-ipc.h"
33 #include "sst-dsp-priv.h"
34 #include "sst-dsp.h"
35
36 #define HSW_PCM_COUNT           6
37 #define HSW_VOLUME_MAX          0x7FFFFFFF      /* 0dB */
38
39 /* simple volume table */
40 static const u32 volume_map[] = {
41         HSW_VOLUME_MAX >> 30,
42         HSW_VOLUME_MAX >> 29,
43         HSW_VOLUME_MAX >> 28,
44         HSW_VOLUME_MAX >> 27,
45         HSW_VOLUME_MAX >> 26,
46         HSW_VOLUME_MAX >> 25,
47         HSW_VOLUME_MAX >> 24,
48         HSW_VOLUME_MAX >> 23,
49         HSW_VOLUME_MAX >> 22,
50         HSW_VOLUME_MAX >> 21,
51         HSW_VOLUME_MAX >> 20,
52         HSW_VOLUME_MAX >> 19,
53         HSW_VOLUME_MAX >> 18,
54         HSW_VOLUME_MAX >> 17,
55         HSW_VOLUME_MAX >> 16,
56         HSW_VOLUME_MAX >> 15,
57         HSW_VOLUME_MAX >> 14,
58         HSW_VOLUME_MAX >> 13,
59         HSW_VOLUME_MAX >> 12,
60         HSW_VOLUME_MAX >> 11,
61         HSW_VOLUME_MAX >> 10,
62         HSW_VOLUME_MAX >> 9,
63         HSW_VOLUME_MAX >> 8,
64         HSW_VOLUME_MAX >> 7,
65         HSW_VOLUME_MAX >> 6,
66         HSW_VOLUME_MAX >> 5,
67         HSW_VOLUME_MAX >> 4,
68         HSW_VOLUME_MAX >> 3,
69         HSW_VOLUME_MAX >> 2,
70         HSW_VOLUME_MAX >> 1,
71         HSW_VOLUME_MAX >> 0,
72 };
73
74 #define HSW_PCM_PERIODS_MAX     64
75 #define HSW_PCM_PERIODS_MIN     2
76
77 static const struct snd_pcm_hardware hsw_pcm_hardware = {
78         .info                   = SNDRV_PCM_INFO_MMAP |
79                                   SNDRV_PCM_INFO_MMAP_VALID |
80                                   SNDRV_PCM_INFO_INTERLEAVED |
81                                   SNDRV_PCM_INFO_PAUSE |
82                                   SNDRV_PCM_INFO_RESUME |
83                                   SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
84         .formats                = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FORMAT_S24_LE |
85                                   SNDRV_PCM_FMTBIT_S32_LE,
86         .period_bytes_min       = PAGE_SIZE,
87         .period_bytes_max       = (HSW_PCM_PERIODS_MAX / HSW_PCM_PERIODS_MIN) * PAGE_SIZE,
88         .periods_min            = HSW_PCM_PERIODS_MIN,
89         .periods_max            = HSW_PCM_PERIODS_MAX,
90         .buffer_bytes_max       = HSW_PCM_PERIODS_MAX * PAGE_SIZE,
91 };
92
93 /* private data for each PCM DSP stream */
94 struct hsw_pcm_data {
95         int dai_id;
96         struct sst_hsw_stream *stream;
97         u32 volume[2];
98         struct snd_pcm_substream *substream;
99         struct snd_compr_stream *cstream;
100         unsigned int wpos;
101         struct mutex mutex;
102 };
103
104 /* private data for the driver */
105 struct hsw_priv_data {
106         /* runtime DSP */
107         struct sst_hsw *hsw;
108
109         /* page tables */
110         struct snd_dma_buffer dmab[HSW_PCM_COUNT][2];
111
112         /* DAI data */
113         struct hsw_pcm_data pcm[HSW_PCM_COUNT];
114 };
115
116 static inline u32 hsw_mixer_to_ipc(unsigned int value)
117 {
118         if (value >= ARRAY_SIZE(volume_map))
119                 return volume_map[0];
120         else
121                 return volume_map[value];
122 }
123
124 static inline unsigned int hsw_ipc_to_mixer(u32 value)
125 {
126         int i;
127
128         for (i = 0; i < ARRAY_SIZE(volume_map); i++) {
129                 if (volume_map[i] >= value)
130                         return i;
131         }
132
133         return i - 1;
134 }
135
136 static int hsw_stream_volume_put(struct snd_kcontrol *kcontrol,
137                                 struct snd_ctl_elem_value *ucontrol)
138 {
139         struct snd_soc_platform *platform = snd_kcontrol_chip(kcontrol);
140         struct soc_mixer_control *mc =
141                 (struct soc_mixer_control *)kcontrol->private_value;
142         struct hsw_priv_data *pdata =
143                 snd_soc_platform_get_drvdata(platform);
144         struct hsw_pcm_data *pcm_data = &pdata->pcm[mc->reg];
145         struct sst_hsw *hsw = pdata->hsw;
146         u32 volume;
147
148         mutex_lock(&pcm_data->mutex);
149
150         if (!pcm_data->stream) {
151                 pcm_data->volume[0] =
152                         hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
153                 pcm_data->volume[1] =
154                         hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
155                 mutex_unlock(&pcm_data->mutex);
156                 return 0;
157         }
158
159         if (ucontrol->value.integer.value[0] ==
160                 ucontrol->value.integer.value[1]) {
161                 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
162                 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, 2, volume);
163         } else {
164                 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
165                 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, 0, volume);
166                 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
167                 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, 1, volume);
168         }
169
170         mutex_unlock(&pcm_data->mutex);
171         return 0;
172 }
173
174 static int hsw_stream_volume_get(struct snd_kcontrol *kcontrol,
175                                 struct snd_ctl_elem_value *ucontrol)
176 {
177         struct snd_soc_platform *platform = snd_kcontrol_chip(kcontrol);
178         struct soc_mixer_control *mc =
179                 (struct soc_mixer_control *)kcontrol->private_value;
180         struct hsw_priv_data *pdata =
181                 snd_soc_platform_get_drvdata(platform);
182         struct hsw_pcm_data *pcm_data = &pdata->pcm[mc->reg];
183         struct sst_hsw *hsw = pdata->hsw;
184         u32 volume;
185
186         mutex_lock(&pcm_data->mutex);
187
188         if (!pcm_data->stream) {
189                 ucontrol->value.integer.value[0] =
190                         hsw_ipc_to_mixer(pcm_data->volume[0]);
191                 ucontrol->value.integer.value[1] =
192                         hsw_ipc_to_mixer(pcm_data->volume[1]);
193                 mutex_unlock(&pcm_data->mutex);
194                 return 0;
195         }
196
197         sst_hsw_stream_get_volume(hsw, pcm_data->stream, 0, 0, &volume);
198         ucontrol->value.integer.value[0] = hsw_ipc_to_mixer(volume);
199         sst_hsw_stream_get_volume(hsw, pcm_data->stream, 0, 1, &volume);
200         ucontrol->value.integer.value[1] = hsw_ipc_to_mixer(volume);
201         mutex_unlock(&pcm_data->mutex);
202
203         return 0;
204 }
205
206 static int hsw_volume_put(struct snd_kcontrol *kcontrol,
207                                 struct snd_ctl_elem_value *ucontrol)
208 {
209         struct snd_soc_platform *platform = snd_kcontrol_chip(kcontrol);
210         struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
211         struct sst_hsw *hsw = pdata->hsw;
212         u32 volume;
213
214         if (ucontrol->value.integer.value[0] ==
215                 ucontrol->value.integer.value[1]) {
216
217                 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
218                 sst_hsw_mixer_set_volume(hsw, 0, 2, volume);
219
220         } else {
221                 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
222                 sst_hsw_mixer_set_volume(hsw, 0, 0, volume);
223
224                 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
225                 sst_hsw_mixer_set_volume(hsw, 0, 1, volume);
226         }
227
228         return 0;
229 }
230
231 static int hsw_volume_get(struct snd_kcontrol *kcontrol,
232                                 struct snd_ctl_elem_value *ucontrol)
233 {
234         struct snd_soc_platform *platform = snd_kcontrol_chip(kcontrol);
235         struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
236         struct sst_hsw *hsw = pdata->hsw;
237         unsigned int volume = 0;
238
239         sst_hsw_mixer_get_volume(hsw, 0, 0, &volume);
240         ucontrol->value.integer.value[0] = hsw_ipc_to_mixer(volume);
241
242         sst_hsw_mixer_get_volume(hsw, 0, 1, &volume);
243         ucontrol->value.integer.value[1] = hsw_ipc_to_mixer(volume);
244
245         return 0;
246 }
247
248 /* TLV used by both global and stream volumes */
249 static const DECLARE_TLV_DB_SCALE(hsw_vol_tlv, -9000, 300, 1);
250
251 /* System Pin has no volume control */
252 static const struct snd_kcontrol_new hsw_volume_controls[] = {
253         /* Global DSP volume */
254         SOC_DOUBLE_EXT_TLV("Master Playback Volume", 0, 0, 8,
255                 ARRAY_SIZE(volume_map) -1, 0,
256                 hsw_volume_get, hsw_volume_put, hsw_vol_tlv),
257         /* Offload 0 volume */
258         SOC_DOUBLE_EXT_TLV("Media0 Playback Volume", 1, 0, 8,
259                 ARRAY_SIZE(volume_map), 0,
260                 hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
261         /* Offload 1 volume */
262         SOC_DOUBLE_EXT_TLV("Media1 Playback Volume", 2, 0, 8,
263                 ARRAY_SIZE(volume_map), 0,
264                 hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
265         /* Loopback volume */
266         SOC_DOUBLE_EXT_TLV("Loopback Capture Volume", 3, 0, 8,
267                 ARRAY_SIZE(volume_map), 0,
268                 hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
269         /* Mic Capture volume */
270         SOC_DOUBLE_EXT_TLV("Mic Capture Volume", 4, 0, 8,
271                 ARRAY_SIZE(volume_map), 0,
272                 hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
273 };
274
275 /* Create DMA buffer page table for DSP */
276 static int create_adsp_page_table(struct snd_pcm_substream *substream,
277         struct hsw_priv_data *pdata, struct snd_soc_pcm_runtime *rtd,
278         unsigned char *dma_area, size_t size, int pcm)
279 {
280         struct snd_dma_buffer *dmab = snd_pcm_get_dma_buf(substream);
281         int i, pages, stream = substream->stream;
282
283         pages = snd_sgbuf_aligned_pages(size);
284
285         dev_dbg(rtd->dev, "generating page table for %p size 0x%zu pages %d\n",
286                 dma_area, size, pages);
287
288         for (i = 0; i < pages; i++) {
289                 u32 idx = (((i << 2) + i)) >> 1;
290                 u32 pfn = snd_sgbuf_get_addr(dmab, i * PAGE_SIZE) >> PAGE_SHIFT;
291                 u32 *pg_table;
292
293                 dev_dbg(rtd->dev, "pfn i %i idx %d pfn %x\n", i, idx, pfn);
294
295                 pg_table = (u32 *)(pdata->dmab[pcm][stream].area + idx);
296
297                 if (i & 1)
298                         *pg_table |= (pfn << 4);
299                 else
300                         *pg_table |= pfn;
301         }
302
303         return 0;
304 }
305
306 /* this may get called several times by oss emulation */
307 static int hsw_pcm_hw_params(struct snd_pcm_substream *substream,
308                               struct snd_pcm_hw_params *params)
309 {
310         struct snd_soc_pcm_runtime *rtd = substream->private_data;
311         struct snd_pcm_runtime *runtime = substream->runtime;
312         struct hsw_priv_data *pdata =
313                 snd_soc_platform_get_drvdata(rtd->platform);
314         struct hsw_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
315         struct sst_hsw *hsw = pdata->hsw;
316         struct sst_module *module_data;
317         struct sst_dsp *dsp;
318         struct snd_dma_buffer *dmab;
319         enum sst_hsw_stream_type stream_type;
320         enum sst_hsw_stream_path_id path_id;
321         u32 rate, bits, map, pages, module_id;
322         u8 channels;
323         int ret;
324
325         /* stream direction */
326         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
327                 path_id = SST_HSW_STREAM_PATH_SSP0_OUT;
328         else
329                 path_id = SST_HSW_STREAM_PATH_SSP0_IN;
330
331         /* DSP stream type depends on DAI ID */
332         switch (rtd->cpu_dai->id) {
333         case 0:
334                 stream_type = SST_HSW_STREAM_TYPE_SYSTEM;
335                 module_id = SST_HSW_MODULE_PCM_SYSTEM;
336                 break;
337         case 1:
338         case 2:
339                 stream_type = SST_HSW_STREAM_TYPE_RENDER;
340                 module_id = SST_HSW_MODULE_PCM;
341                 break;
342         case 3:
343                 /* path ID needs to be OUT for loopback */
344                 stream_type = SST_HSW_STREAM_TYPE_LOOPBACK;
345                 path_id = SST_HSW_STREAM_PATH_SSP0_OUT;
346                 module_id = SST_HSW_MODULE_PCM_REFERENCE;
347                 break;
348         case 4:
349                 stream_type = SST_HSW_STREAM_TYPE_CAPTURE;
350                 module_id = SST_HSW_MODULE_PCM_CAPTURE;
351                 break;
352         default:
353                 dev_err(rtd->dev, "error: invalid DAI ID %d\n",
354                         rtd->cpu_dai->id);
355                 return -EINVAL;
356         };
357
358         ret = sst_hsw_stream_format(hsw, pcm_data->stream,
359                 path_id, stream_type, SST_HSW_STREAM_FORMAT_PCM_FORMAT);
360         if (ret < 0) {
361                 dev_err(rtd->dev, "error: failed to set format %d\n", ret);
362                 return ret;
363         }
364
365         rate = params_rate(params);
366         ret = sst_hsw_stream_set_rate(hsw, pcm_data->stream, rate);
367         if (ret < 0) {
368                 dev_err(rtd->dev, "error: could not set rate %d\n", rate);
369                 return ret;
370         }
371
372         switch (params_format(params)) {
373         case SNDRV_PCM_FORMAT_S16_LE:
374                 bits = SST_HSW_DEPTH_16BIT;
375                 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 16);
376                 break;
377         case SNDRV_PCM_FORMAT_S24_LE:
378                 bits = SST_HSW_DEPTH_24BIT;
379                 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 32);
380                 break;
381         default:
382                 dev_err(rtd->dev, "error: invalid format %d\n",
383                         params_format(params));
384                 return -EINVAL;
385         }
386
387         ret = sst_hsw_stream_set_bits(hsw, pcm_data->stream, bits);
388         if (ret < 0) {
389                 dev_err(rtd->dev, "error: could not set bits %d\n", bits);
390                 return ret;
391         }
392
393         /* we only support stereo atm */
394         channels = params_channels(params);
395         if (channels != 2) {
396                 dev_err(rtd->dev, "error: invalid channels %d\n", channels);
397                 return -EINVAL;
398         }
399
400         map = create_channel_map(SST_HSW_CHANNEL_CONFIG_STEREO);
401         sst_hsw_stream_set_map_config(hsw, pcm_data->stream,
402                         map, SST_HSW_CHANNEL_CONFIG_STEREO);
403
404         ret = sst_hsw_stream_set_channels(hsw, pcm_data->stream, channels);
405         if (ret < 0) {
406                 dev_err(rtd->dev, "error: could not set channels %d\n",
407                         channels);
408                 return ret;
409         }
410
411         ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
412         if (ret < 0) {
413                 dev_err(rtd->dev, "error: could not allocate %d bytes for PCM %d\n",
414                         params_buffer_bytes(params), ret);
415                 return ret;
416         }
417
418         dmab = snd_pcm_get_dma_buf(substream);
419
420         ret = create_adsp_page_table(substream, pdata, rtd, runtime->dma_area,
421                 runtime->dma_bytes, rtd->cpu_dai->id);
422         if (ret < 0)
423                 return ret;
424
425         sst_hsw_stream_set_style(hsw, pcm_data->stream,
426                 SST_HSW_INTERLEAVING_PER_CHANNEL);
427
428         if (runtime->dma_bytes % PAGE_SIZE)
429                 pages = (runtime->dma_bytes / PAGE_SIZE) + 1;
430         else
431                 pages = runtime->dma_bytes / PAGE_SIZE;
432
433         ret = sst_hsw_stream_buffer(hsw, pcm_data->stream,
434                 pdata->dmab[rtd->cpu_dai->id][substream->stream].addr,
435                 pages, runtime->dma_bytes, 0,
436                 snd_sgbuf_get_addr(dmab, 0) >> PAGE_SHIFT);
437         if (ret < 0) {
438                 dev_err(rtd->dev, "error: failed to set DMA buffer %d\n", ret);
439                 return ret;
440         }
441
442         dsp = sst_hsw_get_dsp(hsw);
443
444         module_data = sst_module_get_from_id(dsp, module_id);
445         if (module_data == NULL) {
446                 dev_err(rtd->dev, "error: failed to get module config\n");
447                 return -EINVAL;
448         }
449
450         /* we use hardcoded memory offsets atm, will be updated for new FW */
451         if (stream_type == SST_HSW_STREAM_TYPE_CAPTURE) {
452                 sst_hsw_stream_set_module_info(hsw, pcm_data->stream,
453                         SST_HSW_MODULE_PCM_CAPTURE, module_data->entry);
454                 sst_hsw_stream_set_pmemory_info(hsw, pcm_data->stream,
455                         0x449400, 0x4000);
456                 sst_hsw_stream_set_smemory_info(hsw, pcm_data->stream,
457                         0x400000, 0);
458         } else { /* stream_type == SST_HSW_STREAM_TYPE_SYSTEM */
459                 sst_hsw_stream_set_module_info(hsw, pcm_data->stream,
460                         SST_HSW_MODULE_PCM_SYSTEM, module_data->entry);
461
462                 sst_hsw_stream_set_pmemory_info(hsw, pcm_data->stream,
463                         module_data->offset, module_data->size);
464                 sst_hsw_stream_set_pmemory_info(hsw, pcm_data->stream,
465                         0x44d400, 0x3800);
466
467                 sst_hsw_stream_set_smemory_info(hsw, pcm_data->stream,
468                         module_data->offset, module_data->size);
469                 sst_hsw_stream_set_smemory_info(hsw, pcm_data->stream,
470                         0x400000, 0);
471         }
472
473         ret = sst_hsw_stream_commit(hsw, pcm_data->stream);
474         if (ret < 0) {
475                 dev_err(rtd->dev, "error: failed to commit stream %d\n", ret);
476                 return ret;
477         }
478
479         ret = sst_hsw_stream_pause(hsw, pcm_data->stream, 1);
480         if (ret < 0)
481                 dev_err(rtd->dev, "error: failed to pause %d\n", ret);
482
483         return 0;
484 }
485
486 static int hsw_pcm_hw_free(struct snd_pcm_substream *substream)
487 {
488         snd_pcm_lib_free_pages(substream);
489         return 0;
490 }
491
492 static int hsw_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
493 {
494         struct snd_soc_pcm_runtime *rtd = substream->private_data;
495         struct hsw_priv_data *pdata =
496                 snd_soc_platform_get_drvdata(rtd->platform);
497         struct hsw_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
498         struct sst_hsw *hsw = pdata->hsw;
499
500         switch (cmd) {
501         case SNDRV_PCM_TRIGGER_START:
502         case SNDRV_PCM_TRIGGER_RESUME:
503         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
504                 sst_hsw_stream_resume(hsw, pcm_data->stream, 0);
505                 break;
506         case SNDRV_PCM_TRIGGER_STOP:
507         case SNDRV_PCM_TRIGGER_SUSPEND:
508         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
509                 sst_hsw_stream_pause(hsw, pcm_data->stream, 0);
510                 break;
511         default:
512                 break;
513         }
514
515         return 0;
516 }
517
518 static u32 hsw_notify_pointer(struct sst_hsw_stream *stream, void *data)
519 {
520         struct hsw_pcm_data *pcm_data = data;
521         struct snd_pcm_substream *substream = pcm_data->substream;
522         struct snd_pcm_runtime *runtime = substream->runtime;
523         struct snd_soc_pcm_runtime *rtd = substream->private_data;
524         u32 pos;
525
526         pos = frames_to_bytes(runtime,
527                 (runtime->control->appl_ptr % runtime->buffer_size));
528
529         dev_dbg(rtd->dev, "PCM: App pointer %d bytes\n", pos);
530
531         /* let alsa know we have play a period */
532         snd_pcm_period_elapsed(substream);
533         return pos;
534 }
535
536 static snd_pcm_uframes_t hsw_pcm_pointer(struct snd_pcm_substream *substream)
537 {
538         struct snd_soc_pcm_runtime *rtd = substream->private_data;
539         struct snd_pcm_runtime *runtime = substream->runtime;
540         struct hsw_priv_data *pdata =
541                 snd_soc_platform_get_drvdata(rtd->platform);
542         struct hsw_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
543         struct sst_hsw *hsw = pdata->hsw;
544         snd_pcm_uframes_t offset;
545
546         offset = bytes_to_frames(runtime,
547                 sst_hsw_get_dsp_position(hsw, pcm_data->stream));
548
549         dev_dbg(rtd->dev, "PCM: DMA pointer %zu bytes\n",
550                 frames_to_bytes(runtime, (u32)offset));
551         return offset;
552 }
553
554 static int hsw_pcm_open(struct snd_pcm_substream *substream)
555 {
556         struct snd_soc_pcm_runtime *rtd = substream->private_data;
557         struct hsw_priv_data *pdata =
558                 snd_soc_platform_get_drvdata(rtd->platform);
559         struct hsw_pcm_data *pcm_data;
560         struct sst_hsw *hsw = pdata->hsw;
561
562         pcm_data = &pdata->pcm[rtd->cpu_dai->id];
563
564         mutex_lock(&pcm_data->mutex);
565
566         snd_soc_pcm_set_drvdata(rtd, pcm_data);
567         pcm_data->substream = substream;
568
569         snd_soc_set_runtime_hwparams(substream, &hsw_pcm_hardware);
570
571         pcm_data->stream = sst_hsw_stream_new(hsw, rtd->cpu_dai->id,
572                 hsw_notify_pointer, pcm_data);
573         if (pcm_data->stream == NULL) {
574                 dev_err(rtd->dev, "error: failed to create stream\n");
575                 mutex_unlock(&pcm_data->mutex);
576                 return -EINVAL;
577         }
578
579         /* Set previous saved volume */
580         sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0,
581                         0, pcm_data->volume[0]);
582         sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0,
583                         1, pcm_data->volume[1]);
584
585         mutex_unlock(&pcm_data->mutex);
586         return 0;
587 }
588
589 static int hsw_pcm_close(struct snd_pcm_substream *substream)
590 {
591         struct snd_soc_pcm_runtime *rtd = substream->private_data;
592         struct hsw_priv_data *pdata =
593                 snd_soc_platform_get_drvdata(rtd->platform);
594         struct hsw_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
595         struct sst_hsw *hsw = pdata->hsw;
596         int ret;
597
598         mutex_lock(&pcm_data->mutex);
599         ret = sst_hsw_stream_reset(hsw, pcm_data->stream);
600         if (ret < 0) {
601                 dev_dbg(rtd->dev, "error: reset stream failed %d\n", ret);
602                 goto out;
603         }
604
605         ret = sst_hsw_stream_free(hsw, pcm_data->stream);
606         if (ret < 0) {
607                 dev_dbg(rtd->dev, "error: free stream failed %d\n", ret);
608                 goto out;
609         }
610         pcm_data->stream = NULL;
611
612 out:
613         mutex_unlock(&pcm_data->mutex);
614         return ret;
615 }
616
617 static struct snd_pcm_ops hsw_pcm_ops = {
618         .open           = hsw_pcm_open,
619         .close          = hsw_pcm_close,
620         .ioctl          = snd_pcm_lib_ioctl,
621         .hw_params      = hsw_pcm_hw_params,
622         .hw_free        = hsw_pcm_hw_free,
623         .trigger        = hsw_pcm_trigger,
624         .pointer        = hsw_pcm_pointer,
625         .page           = snd_pcm_sgbuf_ops_page,
626 };
627
628 static void hsw_pcm_free(struct snd_pcm *pcm)
629 {
630         snd_pcm_lib_preallocate_free_for_all(pcm);
631 }
632
633 static int hsw_pcm_new(struct snd_soc_pcm_runtime *rtd)
634 {
635         struct snd_pcm *pcm = rtd->pcm;
636         struct snd_soc_platform *platform = rtd->platform;
637         struct sst_pdata *pdata = dev_get_platdata(platform->dev);
638         struct device *dev = pdata->dma_dev;
639         int ret = 0;
640
641         if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream ||
642                         pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
643                 ret = snd_pcm_lib_preallocate_pages_for_all(pcm,
644                         SNDRV_DMA_TYPE_DEV_SG,
645                         dev,
646                         hsw_pcm_hardware.buffer_bytes_max,
647                         hsw_pcm_hardware.buffer_bytes_max);
648                 if (ret) {
649                         dev_err(rtd->dev, "dma buffer allocation failed %d\n",
650                                 ret);
651                         return ret;
652                 }
653         }
654
655         return ret;
656 }
657
658 #define HSW_FORMATS \
659         (SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S16_LE |\
660          SNDRV_PCM_FMTBIT_S32_LE)
661
662 static struct snd_soc_dai_driver hsw_dais[] = {
663         {
664                 .name  = "System Pin",
665                 .playback = {
666                         .stream_name = "System Playback",
667                         .channels_min = 2,
668                         .channels_max = 2,
669                         .rates = SNDRV_PCM_RATE_48000,
670                         .formats = SNDRV_PCM_FMTBIT_S16_LE,
671                 },
672         },
673         {
674                 /* PCM */
675                 .name  = "Offload0 Pin",
676                 .playback = {
677                         .stream_name = "Offload0 Playback",
678                         .channels_min = 2,
679                         .channels_max = 2,
680                         .rates = SNDRV_PCM_RATE_8000_192000,
681                         .formats = HSW_FORMATS,
682                 },
683         },
684         {
685                 /* PCM */
686                 .name  = "Offload1 Pin",
687                 .playback = {
688                         .stream_name = "Offload1 Playback",
689                         .channels_min = 2,
690                         .channels_max = 2,
691                         .rates = SNDRV_PCM_RATE_8000_192000,
692                         .formats = HSW_FORMATS,
693                 },
694         },
695         {
696                 .name  = "Loopback Pin",
697                 .capture = {
698                         .stream_name = "Loopback Capture",
699                         .channels_min = 2,
700                         .channels_max = 2,
701                         .rates = SNDRV_PCM_RATE_8000_192000,
702                         .formats = HSW_FORMATS,
703                 },
704         },
705         {
706                 .name  = "Capture Pin",
707                 .capture = {
708                         .stream_name = "Analog Capture",
709                         .channels_min = 2,
710                         .channels_max = 2,
711                         .rates = SNDRV_PCM_RATE_8000_192000,
712                         .formats = HSW_FORMATS,
713                 },
714         },
715 };
716
717 static const struct snd_soc_dapm_widget widgets[] = {
718
719         /* Backend DAIs  */
720         SND_SOC_DAPM_AIF_IN("SSP0 CODEC IN", NULL, 0, SND_SOC_NOPM, 0, 0),
721         SND_SOC_DAPM_AIF_OUT("SSP0 CODEC OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
722         SND_SOC_DAPM_AIF_IN("SSP1 BT IN", NULL, 0, SND_SOC_NOPM, 0, 0),
723         SND_SOC_DAPM_AIF_OUT("SSP1 BT OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
724
725         /* Global Playback Mixer */
726         SND_SOC_DAPM_MIXER("Playback VMixer", SND_SOC_NOPM, 0, 0, NULL, 0),
727 };
728
729 static const struct snd_soc_dapm_route graph[] = {
730
731         /* Playback Mixer */
732         {"Playback VMixer", NULL, "System Playback"},
733         {"Playback VMixer", NULL, "Offload0 Playback"},
734         {"Playback VMixer", NULL, "Offload1 Playback"},
735
736         {"SSP0 CODEC OUT", NULL, "Playback VMixer"},
737
738         {"Analog Capture", NULL, "SSP0 CODEC IN"},
739 };
740
741 static int hsw_pcm_probe(struct snd_soc_platform *platform)
742 {
743         struct sst_pdata *pdata = dev_get_platdata(platform->dev);
744         struct hsw_priv_data *priv_data;
745         struct device *dma_dev = pdata->dma_dev;
746         int i, ret = 0;
747
748         if (!pdata)
749                 return -ENODEV;
750
751         priv_data = devm_kzalloc(platform->dev, sizeof(*priv_data), GFP_KERNEL);
752         priv_data->hsw = pdata->dsp;
753         snd_soc_platform_set_drvdata(platform, priv_data);
754
755         /* allocate DSP buffer page tables */
756         for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) {
757
758                 mutex_init(&priv_data->pcm[i].mutex);
759
760                 /* playback */
761                 if (hsw_dais[i].playback.channels_min) {
762                         ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dma_dev,
763                                 PAGE_SIZE, &priv_data->dmab[i][0]);
764                         if (ret < 0)
765                                 goto err;
766                 }
767
768                 /* capture */
769                 if (hsw_dais[i].capture.channels_min) {
770                         ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dma_dev,
771                                 PAGE_SIZE, &priv_data->dmab[i][1]);
772                         if (ret < 0)
773                                 goto err;
774                 }
775         }
776
777         return 0;
778
779 err:
780         for (;i >= 0; i--) {
781                 if (hsw_dais[i].playback.channels_min)
782                         snd_dma_free_pages(&priv_data->dmab[i][0]);
783                 if (hsw_dais[i].capture.channels_min)
784                         snd_dma_free_pages(&priv_data->dmab[i][1]);
785         }
786         return ret;
787 }
788
789 static int hsw_pcm_remove(struct snd_soc_platform *platform)
790 {
791         struct hsw_priv_data *priv_data =
792                 snd_soc_platform_get_drvdata(platform);
793         int i;
794
795         for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) {
796                 if (hsw_dais[i].playback.channels_min)
797                         snd_dma_free_pages(&priv_data->dmab[i][0]);
798                 if (hsw_dais[i].capture.channels_min)
799                         snd_dma_free_pages(&priv_data->dmab[i][1]);
800         }
801
802         return 0;
803 }
804
805 static struct snd_soc_platform_driver hsw_soc_platform = {
806         .probe          = hsw_pcm_probe,
807         .remove         = hsw_pcm_remove,
808         .ops            = &hsw_pcm_ops,
809         .pcm_new        = hsw_pcm_new,
810         .pcm_free       = hsw_pcm_free,
811         .controls       = hsw_volume_controls,
812         .num_controls   = ARRAY_SIZE(hsw_volume_controls),
813         .dapm_widgets   = widgets,
814         .num_dapm_widgets       = ARRAY_SIZE(widgets),
815         .dapm_routes    = graph,
816         .num_dapm_routes        = ARRAY_SIZE(graph),
817 };
818
819 static const struct snd_soc_component_driver hsw_dai_component = {
820         .name           = "haswell-dai",
821 };
822
823 static int hsw_pcm_dev_probe(struct platform_device *pdev)
824 {
825         struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
826         int ret;
827
828         ret = sst_hsw_dsp_init(&pdev->dev, sst_pdata);
829         if (ret < 0)
830                 return -ENODEV;
831
832         ret = snd_soc_register_platform(&pdev->dev, &hsw_soc_platform);
833         if (ret < 0)
834                 goto err_plat;
835
836         ret = snd_soc_register_component(&pdev->dev, &hsw_dai_component,
837                 hsw_dais, ARRAY_SIZE(hsw_dais));
838         if (ret < 0)
839                 goto err_comp;
840
841         return 0;
842
843 err_comp:
844         snd_soc_unregister_platform(&pdev->dev);
845 err_plat:
846         sst_hsw_dsp_free(&pdev->dev, sst_pdata);
847         return 0;
848 }
849
850 static int hsw_pcm_dev_remove(struct platform_device *pdev)
851 {
852         struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
853
854         snd_soc_unregister_platform(&pdev->dev);
855         snd_soc_unregister_component(&pdev->dev);
856         sst_hsw_dsp_free(&pdev->dev, sst_pdata);
857
858         return 0;
859 }
860
861 static struct platform_driver hsw_pcm_driver = {
862         .driver = {
863                 .name = "haswell-pcm-audio",
864                 .owner = THIS_MODULE,
865         },
866
867         .probe = hsw_pcm_dev_probe,
868         .remove = hsw_pcm_dev_remove,
869 };
870 module_platform_driver(hsw_pcm_driver);
871
872 MODULE_AUTHOR("Liam Girdwood, Xingchao Wang");
873 MODULE_DESCRIPTION("Haswell/Lynxpoint + Broadwell/Wildcatpoint PCM");
874 MODULE_LICENSE("GPL v2");
875 MODULE_ALIAS("platform:haswell-pcm-audio");