Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/trivial
[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         unsigned char *pcm_pg[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 hsw_priv_data *pdata,
277         struct snd_soc_pcm_runtime *rtd,
278         unsigned char *dma_area, size_t size, int pcm, int stream)
279 {
280         int i, pages;
281
282         if (size % PAGE_SIZE)
283                 pages = (size / PAGE_SIZE) + 1;
284         else
285                 pages = size / PAGE_SIZE;
286
287         dev_dbg(rtd->dev, "generating page table for %p size 0x%zu pages %d\n",
288                 dma_area, size, pages);
289
290         for (i = 0; i < pages; i++) {
291                 u32 idx = (((i << 2) + i)) >> 1;
292                 u32 pfn = (virt_to_phys(dma_area + i * PAGE_SIZE)) >> PAGE_SHIFT;
293                 u32 *pg_table;
294
295                 dev_dbg(rtd->dev, "pfn i %i idx %d pfn %x\n", i, idx, pfn);
296
297                 pg_table = (u32*)(pdata->pcm_pg[pcm][stream] + idx);
298
299                 if (i & 1)
300                         *pg_table |= (pfn << 4);
301                 else
302                         *pg_table |= pfn;
303         }
304
305         return 0;
306 }
307
308 /* this may get called several times by oss emulation */
309 static int hsw_pcm_hw_params(struct snd_pcm_substream *substream,
310                               struct snd_pcm_hw_params *params)
311 {
312         struct snd_soc_pcm_runtime *rtd = substream->private_data;
313         struct snd_pcm_runtime *runtime = substream->runtime;
314         struct hsw_priv_data *pdata =
315                 snd_soc_platform_get_drvdata(rtd->platform);
316         struct hsw_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
317         struct sst_hsw *hsw = pdata->hsw;
318         struct sst_module *module_data;
319         struct sst_dsp *dsp;
320         enum sst_hsw_stream_type stream_type;
321         enum sst_hsw_stream_path_id path_id;
322         u32 rate, bits, map, pages, module_id;
323         u8 channels;
324         int ret;
325
326         /* stream direction */
327         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
328                 path_id = SST_HSW_STREAM_PATH_SSP0_OUT;
329         else
330                 path_id = SST_HSW_STREAM_PATH_SSP0_IN;
331
332         /* DSP stream type depends on DAI ID */
333         switch (rtd->cpu_dai->id) {
334         case 0:
335                 stream_type = SST_HSW_STREAM_TYPE_SYSTEM;
336                 module_id = SST_HSW_MODULE_PCM_SYSTEM;
337                 break;
338         case 1:
339         case 2:
340                 stream_type = SST_HSW_STREAM_TYPE_RENDER;
341                 module_id = SST_HSW_MODULE_PCM;
342                 break;
343         case 3:
344                 /* path ID needs to be OUT for loopback */
345                 stream_type = SST_HSW_STREAM_TYPE_LOOPBACK;
346                 path_id = SST_HSW_STREAM_PATH_SSP0_OUT;
347                 module_id = SST_HSW_MODULE_PCM_REFERENCE;
348                 break;
349         case 4:
350                 stream_type = SST_HSW_STREAM_TYPE_CAPTURE;
351                 module_id = SST_HSW_MODULE_PCM_CAPTURE;
352                 break;
353         default:
354                 dev_err(rtd->dev, "error: invalid DAI ID %d\n",
355                         rtd->cpu_dai->id);
356                 return -EINVAL;
357         };
358
359         ret = sst_hsw_stream_format(hsw, pcm_data->stream,
360                 path_id, stream_type, SST_HSW_STREAM_FORMAT_PCM_FORMAT);
361         if (ret < 0) {
362                 dev_err(rtd->dev, "error: failed to set format %d\n", ret);
363                 return ret;
364         }
365
366         rate = params_rate(params);
367         ret = sst_hsw_stream_set_rate(hsw, pcm_data->stream, rate);
368         if (ret < 0) {
369                 dev_err(rtd->dev, "error: could not set rate %d\n", rate);
370                 return ret;
371         }
372
373         switch (params_format(params)) {
374         case SNDRV_PCM_FORMAT_S16_LE:
375                 bits = SST_HSW_DEPTH_16BIT;
376                 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 16);
377                 break;
378         case SNDRV_PCM_FORMAT_S24_LE:
379                 bits = SST_HSW_DEPTH_24BIT;
380                 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 32);
381                 break;
382         default:
383                 dev_err(rtd->dev, "error: invalid format %d\n",
384                         params_format(params));
385                 return -EINVAL;
386         }
387
388         ret = sst_hsw_stream_set_bits(hsw, pcm_data->stream, bits);
389         if (ret < 0) {
390                 dev_err(rtd->dev, "error: could not set bits %d\n", bits);
391                 return ret;
392         }
393
394         /* we only support stereo atm */
395         channels = params_channels(params);
396         if (channels != 2) {
397                 dev_err(rtd->dev, "error: invalid channels %d\n", channels);
398                 return -EINVAL;
399         }
400
401         map = create_channel_map(SST_HSW_CHANNEL_CONFIG_STEREO);
402         sst_hsw_stream_set_map_config(hsw, pcm_data->stream,
403                         map, SST_HSW_CHANNEL_CONFIG_STEREO);
404
405         ret = sst_hsw_stream_set_channels(hsw, pcm_data->stream, channels);
406         if (ret < 0) {
407                 dev_err(rtd->dev, "error: could not set channels %d\n",
408                         channels);
409                 return ret;
410         }
411
412         ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
413         if (ret < 0) {
414                 dev_err(rtd->dev, "error: could not allocate %d bytes for PCM %d\n",
415                         params_buffer_bytes(params), ret);
416                 return ret;
417         }
418
419         ret = create_adsp_page_table(pdata, rtd, runtime->dma_area,
420                 runtime->dma_bytes, rtd->cpu_dai->id, substream->stream);
421         if (ret < 0)
422                 return ret;
423
424         sst_hsw_stream_set_style(hsw, pcm_data->stream,
425                 SST_HSW_INTERLEAVING_PER_CHANNEL);
426
427         if (runtime->dma_bytes % PAGE_SIZE)
428                 pages = (runtime->dma_bytes / PAGE_SIZE) + 1;
429         else
430                 pages = runtime->dma_bytes / PAGE_SIZE;
431
432         ret = sst_hsw_stream_buffer(hsw, pcm_data->stream,
433                 virt_to_phys(pdata->pcm_pg[rtd->cpu_dai->id][substream->stream]),
434                 pages, runtime->dma_bytes, 0,
435                 (u32)(virt_to_phys(runtime->dma_area) >> PAGE_SHIFT));
436         if (ret < 0) {
437                 dev_err(rtd->dev, "error: failed to set DMA buffer %d\n", ret);
438                 return ret;
439         }
440
441         dsp = sst_hsw_get_dsp(hsw);
442
443         module_data = sst_module_get_from_id(dsp, module_id);
444         if (module_data == NULL) {
445                 dev_err(rtd->dev, "error: failed to get module config\n");
446                 return -EINVAL;
447         }
448
449         /* we use hardcoded memory offsets atm, will be updated for new FW */
450         if (stream_type == SST_HSW_STREAM_TYPE_CAPTURE) {
451                 sst_hsw_stream_set_module_info(hsw, pcm_data->stream,
452                         SST_HSW_MODULE_PCM_CAPTURE, module_data->entry);
453                 sst_hsw_stream_set_pmemory_info(hsw, pcm_data->stream,
454                         0x449400, 0x4000);
455                 sst_hsw_stream_set_smemory_info(hsw, pcm_data->stream,
456                         0x400000, 0);
457         } else { /* stream_type == SST_HSW_STREAM_TYPE_SYSTEM */
458                 sst_hsw_stream_set_module_info(hsw, pcm_data->stream,
459                         SST_HSW_MODULE_PCM_SYSTEM, module_data->entry);
460
461                 sst_hsw_stream_set_pmemory_info(hsw, pcm_data->stream,
462                         module_data->offset, module_data->size);
463                 sst_hsw_stream_set_pmemory_info(hsw, pcm_data->stream,
464                         0x44d400, 0x3800);
465
466                 sst_hsw_stream_set_smemory_info(hsw, pcm_data->stream,
467                         module_data->offset, module_data->size);
468                 sst_hsw_stream_set_smemory_info(hsw, pcm_data->stream,
469                         0x400000, 0);
470         }
471
472         ret = sst_hsw_stream_commit(hsw, pcm_data->stream);
473         if (ret < 0) {
474                 dev_err(rtd->dev, "error: failed to commit stream %d\n", ret);
475                 return ret;
476         }
477
478         ret = sst_hsw_stream_pause(hsw, pcm_data->stream, 1);
479         if (ret < 0)
480                 dev_err(rtd->dev, "error: failed to pause %d\n", ret);
481
482         return 0;
483 }
484
485 static int hsw_pcm_hw_free(struct snd_pcm_substream *substream)
486 {
487         snd_pcm_lib_free_pages(substream);
488         return 0;
489 }
490
491 static int hsw_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
492 {
493         struct snd_soc_pcm_runtime *rtd = substream->private_data;
494         struct hsw_priv_data *pdata =
495                 snd_soc_platform_get_drvdata(rtd->platform);
496         struct hsw_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
497         struct sst_hsw *hsw = pdata->hsw;
498
499         switch (cmd) {
500         case SNDRV_PCM_TRIGGER_START:
501         case SNDRV_PCM_TRIGGER_RESUME:
502         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
503                 sst_hsw_stream_resume(hsw, pcm_data->stream, 0);
504                 break;
505         case SNDRV_PCM_TRIGGER_STOP:
506         case SNDRV_PCM_TRIGGER_SUSPEND:
507         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
508                 sst_hsw_stream_pause(hsw, pcm_data->stream, 0);
509                 break;
510         default:
511                 break;
512         }
513
514         return 0;
515 }
516
517 static u32 hsw_notify_pointer(struct sst_hsw_stream *stream, void *data)
518 {
519         struct hsw_pcm_data *pcm_data = data;
520         struct snd_pcm_substream *substream = pcm_data->substream;
521         struct snd_pcm_runtime *runtime = substream->runtime;
522         struct snd_soc_pcm_runtime *rtd = substream->private_data;
523         u32 pos;
524
525         pos = frames_to_bytes(runtime,
526                 (runtime->control->appl_ptr % runtime->buffer_size));
527
528         dev_dbg(rtd->dev, "PCM: App pointer %d bytes\n", pos);
529
530         /* let alsa know we have play a period */
531         snd_pcm_period_elapsed(substream);
532         return pos;
533 }
534
535 static snd_pcm_uframes_t hsw_pcm_pointer(struct snd_pcm_substream *substream)
536 {
537         struct snd_soc_pcm_runtime *rtd = substream->private_data;
538         struct snd_pcm_runtime *runtime = substream->runtime;
539         struct hsw_priv_data *pdata =
540                 snd_soc_platform_get_drvdata(rtd->platform);
541         struct hsw_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
542         struct sst_hsw *hsw = pdata->hsw;
543         snd_pcm_uframes_t offset;
544
545         offset = bytes_to_frames(runtime,
546                 sst_hsw_get_dsp_position(hsw, pcm_data->stream));
547
548         dev_dbg(rtd->dev, "PCM: DMA pointer %zu bytes\n",
549                 frames_to_bytes(runtime, (u32)offset));
550         return offset;
551 }
552
553 static int hsw_pcm_open(struct snd_pcm_substream *substream)
554 {
555         struct snd_soc_pcm_runtime *rtd = substream->private_data;
556         struct hsw_priv_data *pdata =
557                 snd_soc_platform_get_drvdata(rtd->platform);
558         struct hsw_pcm_data *pcm_data;
559         struct sst_hsw *hsw = pdata->hsw;
560
561         pcm_data = &pdata->pcm[rtd->cpu_dai->id];
562
563         mutex_lock(&pcm_data->mutex);
564
565         snd_soc_pcm_set_drvdata(rtd, pcm_data);
566         pcm_data->substream = substream;
567
568         snd_soc_set_runtime_hwparams(substream, &hsw_pcm_hardware);
569
570         pcm_data->stream = sst_hsw_stream_new(hsw, rtd->cpu_dai->id,
571                 hsw_notify_pointer, pcm_data);
572         if (pcm_data->stream == NULL) {
573                 dev_err(rtd->dev, "error: failed to create stream\n");
574                 mutex_unlock(&pcm_data->mutex);
575                 return -EINVAL;
576         }
577
578         /* Set previous saved volume */
579         sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0,
580                         0, pcm_data->volume[0]);
581         sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0,
582                         1, pcm_data->volume[1]);
583
584         mutex_unlock(&pcm_data->mutex);
585         return 0;
586 }
587
588 static int hsw_pcm_close(struct snd_pcm_substream *substream)
589 {
590         struct snd_soc_pcm_runtime *rtd = substream->private_data;
591         struct hsw_priv_data *pdata =
592                 snd_soc_platform_get_drvdata(rtd->platform);
593         struct hsw_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
594         struct sst_hsw *hsw = pdata->hsw;
595         int ret;
596
597         mutex_lock(&pcm_data->mutex);
598         ret = sst_hsw_stream_reset(hsw, pcm_data->stream);
599         if (ret < 0) {
600                 dev_dbg(rtd->dev, "error: reset stream failed %d\n", ret);
601                 goto out;
602         }
603
604         ret = sst_hsw_stream_free(hsw, pcm_data->stream);
605         if (ret < 0) {
606                 dev_dbg(rtd->dev, "error: free stream failed %d\n", ret);
607                 goto out;
608         }
609         pcm_data->stream = NULL;
610
611 out:
612         mutex_unlock(&pcm_data->mutex);
613         return ret;
614 }
615
616 static struct snd_pcm_ops hsw_pcm_ops = {
617         .open           = hsw_pcm_open,
618         .close          = hsw_pcm_close,
619         .ioctl          = snd_pcm_lib_ioctl,
620         .hw_params      = hsw_pcm_hw_params,
621         .hw_free        = hsw_pcm_hw_free,
622         .trigger        = hsw_pcm_trigger,
623         .pointer        = hsw_pcm_pointer,
624         .mmap           = snd_pcm_lib_default_mmap,
625 };
626
627 static void hsw_pcm_free(struct snd_pcm *pcm)
628 {
629         snd_pcm_lib_preallocate_free_for_all(pcm);
630 }
631
632 static int hsw_pcm_new(struct snd_soc_pcm_runtime *rtd)
633 {
634         struct snd_pcm *pcm = rtd->pcm;
635         int ret = 0;
636
637         ret = dma_coerce_mask_and_coherent(rtd->card->dev, DMA_BIT_MASK(32));
638         if (ret)
639                 return ret;
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,
645                         rtd->card->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         int i;
746
747         if (!pdata)
748                 return -ENODEV;
749
750         priv_data = devm_kzalloc(platform->dev, sizeof(*priv_data), GFP_KERNEL);
751         priv_data->hsw = pdata->dsp;
752         snd_soc_platform_set_drvdata(platform, priv_data);
753
754         /* allocate DSP buffer page tables */
755         for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) {
756
757                 mutex_init(&priv_data->pcm[i].mutex);
758
759                 /* playback */
760                 if (hsw_dais[i].playback.channels_min) {
761                         priv_data->pcm_pg[i][0] = kzalloc(PAGE_SIZE, GFP_DMA);
762                         if (priv_data->pcm_pg[i][0] == NULL)
763                                 goto err;
764                 }
765
766                 /* capture */
767                 if (hsw_dais[i].capture.channels_min) {
768                         priv_data->pcm_pg[i][1] = kzalloc(PAGE_SIZE, GFP_DMA);
769                         if (priv_data->pcm_pg[i][1] == NULL)
770                                 goto err;
771                 }
772         }
773
774         return 0;
775
776 err:
777         for (;i >= 0; i--) {
778                 if (hsw_dais[i].playback.channels_min)
779                         kfree(priv_data->pcm_pg[i][0]);
780                 if (hsw_dais[i].capture.channels_min)
781                         kfree(priv_data->pcm_pg[i][1]);
782         }
783         return -ENOMEM;
784 }
785
786 static int hsw_pcm_remove(struct snd_soc_platform *platform)
787 {
788         struct hsw_priv_data *priv_data =
789                 snd_soc_platform_get_drvdata(platform);
790         int i;
791
792         for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) {
793                 if (hsw_dais[i].playback.channels_min)
794                         kfree(priv_data->pcm_pg[i][0]);
795                 if (hsw_dais[i].capture.channels_min)
796                         kfree(priv_data->pcm_pg[i][1]);
797         }
798
799         return 0;
800 }
801
802 static struct snd_soc_platform_driver hsw_soc_platform = {
803         .probe          = hsw_pcm_probe,
804         .remove         = hsw_pcm_remove,
805         .ops            = &hsw_pcm_ops,
806         .pcm_new        = hsw_pcm_new,
807         .pcm_free       = hsw_pcm_free,
808         .controls       = hsw_volume_controls,
809         .num_controls   = ARRAY_SIZE(hsw_volume_controls),
810         .dapm_widgets   = widgets,
811         .num_dapm_widgets       = ARRAY_SIZE(widgets),
812         .dapm_routes    = graph,
813         .num_dapm_routes        = ARRAY_SIZE(graph),
814 };
815
816 static const struct snd_soc_component_driver hsw_dai_component = {
817         .name           = "haswell-dai",
818 };
819
820 static int hsw_pcm_dev_probe(struct platform_device *pdev)
821 {
822         struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
823         int ret;
824
825         ret = sst_hsw_dsp_init(&pdev->dev, sst_pdata);
826         if (ret < 0)
827                 return -ENODEV;
828
829         ret = snd_soc_register_platform(&pdev->dev, &hsw_soc_platform);
830         if (ret < 0)
831                 goto err_plat;
832
833         ret = snd_soc_register_component(&pdev->dev, &hsw_dai_component,
834                 hsw_dais, ARRAY_SIZE(hsw_dais));
835         if (ret < 0)
836                 goto err_comp;
837
838         return 0;
839
840 err_comp:
841         snd_soc_unregister_platform(&pdev->dev);
842 err_plat:
843         sst_hsw_dsp_free(&pdev->dev, sst_pdata);
844         return 0;
845 }
846
847 static int hsw_pcm_dev_remove(struct platform_device *pdev)
848 {
849         struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
850
851         snd_soc_unregister_platform(&pdev->dev);
852         snd_soc_unregister_component(&pdev->dev);
853         sst_hsw_dsp_free(&pdev->dev, sst_pdata);
854
855         return 0;
856 }
857
858 static struct platform_driver hsw_pcm_driver = {
859         .driver = {
860                 .name = "haswell-pcm-audio",
861                 .owner = THIS_MODULE,
862         },
863
864         .probe = hsw_pcm_dev_probe,
865         .remove = hsw_pcm_dev_remove,
866 };
867 module_platform_driver(hsw_pcm_driver);
868
869 MODULE_AUTHOR("Liam Girdwood, Xingchao Wang");
870 MODULE_DESCRIPTION("Haswell/Lynxpoint + Broadwell/Wildcatpoint PCM");
871 MODULE_LICENSE("GPL v2");
872 MODULE_ALIAS("platform:haswell-pcm-audio");