Merge tag 'clk-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[cascardo/linux.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/delay.h>
21 #include <linux/pm_runtime.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 #define SST_OLD_POSITION(d, r, o) ((d) +                \
40                         frames_to_bytes(r, o))
41 #define SST_SAMPLES(r, x) (bytes_to_samples(r,  \
42                         frames_to_bytes(r, (x))))
43
44 /* simple volume table */
45 static const u32 volume_map[] = {
46         HSW_VOLUME_MAX >> 30,
47         HSW_VOLUME_MAX >> 29,
48         HSW_VOLUME_MAX >> 28,
49         HSW_VOLUME_MAX >> 27,
50         HSW_VOLUME_MAX >> 26,
51         HSW_VOLUME_MAX >> 25,
52         HSW_VOLUME_MAX >> 24,
53         HSW_VOLUME_MAX >> 23,
54         HSW_VOLUME_MAX >> 22,
55         HSW_VOLUME_MAX >> 21,
56         HSW_VOLUME_MAX >> 20,
57         HSW_VOLUME_MAX >> 19,
58         HSW_VOLUME_MAX >> 18,
59         HSW_VOLUME_MAX >> 17,
60         HSW_VOLUME_MAX >> 16,
61         HSW_VOLUME_MAX >> 15,
62         HSW_VOLUME_MAX >> 14,
63         HSW_VOLUME_MAX >> 13,
64         HSW_VOLUME_MAX >> 12,
65         HSW_VOLUME_MAX >> 11,
66         HSW_VOLUME_MAX >> 10,
67         HSW_VOLUME_MAX >> 9,
68         HSW_VOLUME_MAX >> 8,
69         HSW_VOLUME_MAX >> 7,
70         HSW_VOLUME_MAX >> 6,
71         HSW_VOLUME_MAX >> 5,
72         HSW_VOLUME_MAX >> 4,
73         HSW_VOLUME_MAX >> 3,
74         HSW_VOLUME_MAX >> 2,
75         HSW_VOLUME_MAX >> 1,
76         HSW_VOLUME_MAX >> 0,
77 };
78
79 #define HSW_PCM_PERIODS_MAX     64
80 #define HSW_PCM_PERIODS_MIN     2
81
82 #define HSW_PCM_DAI_ID_SYSTEM   0
83 #define HSW_PCM_DAI_ID_OFFLOAD0 1
84 #define HSW_PCM_DAI_ID_OFFLOAD1 2
85 #define HSW_PCM_DAI_ID_LOOPBACK 3
86
87
88 static const struct snd_pcm_hardware hsw_pcm_hardware = {
89         .info                   = SNDRV_PCM_INFO_MMAP |
90                                   SNDRV_PCM_INFO_MMAP_VALID |
91                                   SNDRV_PCM_INFO_INTERLEAVED |
92                                   SNDRV_PCM_INFO_PAUSE |
93                                   SNDRV_PCM_INFO_RESUME |
94                                   SNDRV_PCM_INFO_NO_PERIOD_WAKEUP |
95                                   SNDRV_PCM_INFO_DRAIN_TRIGGER,
96         .formats                = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
97                                   SNDRV_PCM_FMTBIT_S32_LE,
98         .period_bytes_min       = PAGE_SIZE,
99         .period_bytes_max       = (HSW_PCM_PERIODS_MAX / HSW_PCM_PERIODS_MIN) * PAGE_SIZE,
100         .periods_min            = HSW_PCM_PERIODS_MIN,
101         .periods_max            = HSW_PCM_PERIODS_MAX,
102         .buffer_bytes_max       = HSW_PCM_PERIODS_MAX * PAGE_SIZE,
103 };
104
105 struct hsw_pcm_module_map {
106         int dai_id;
107         int stream;
108         enum sst_hsw_module_id mod_id;
109 };
110
111 /* private data for each PCM DSP stream */
112 struct hsw_pcm_data {
113         int dai_id;
114         struct sst_hsw_stream *stream;
115         struct sst_module_runtime *runtime;
116         struct sst_module_runtime_context context;
117         struct snd_pcm *hsw_pcm;
118         u32 volume[2];
119         struct snd_pcm_substream *substream;
120         struct snd_compr_stream *cstream;
121         unsigned int wpos;
122         struct mutex mutex;
123         bool allocated;
124         int persistent_offset;
125 };
126
127 enum hsw_pm_state {
128         HSW_PM_STATE_D0 = 0,
129         HSW_PM_STATE_RTD3 = 1,
130         HSW_PM_STATE_D3 = 2,
131 };
132
133 /* private data for the driver */
134 struct hsw_priv_data {
135         /* runtime DSP */
136         struct sst_hsw *hsw;
137         struct device *dev;
138         enum hsw_pm_state pm_state;
139         struct snd_soc_card *soc_card;
140
141         /* page tables */
142         struct snd_dma_buffer dmab[HSW_PCM_COUNT][2];
143
144         /* DAI data */
145         struct hsw_pcm_data pcm[HSW_PCM_COUNT][2];
146 };
147
148
149 /* static mappings between PCMs and modules - may be dynamic in future */
150 static struct hsw_pcm_module_map mod_map[] = {
151         {HSW_PCM_DAI_ID_SYSTEM, 0, SST_HSW_MODULE_PCM_SYSTEM},
152         {HSW_PCM_DAI_ID_OFFLOAD0, 0, SST_HSW_MODULE_PCM},
153         {HSW_PCM_DAI_ID_OFFLOAD1, 0, SST_HSW_MODULE_PCM},
154         {HSW_PCM_DAI_ID_LOOPBACK, 1, SST_HSW_MODULE_PCM_REFERENCE},
155         {HSW_PCM_DAI_ID_SYSTEM, 1, SST_HSW_MODULE_PCM_CAPTURE},
156 };
157
158 static u32 hsw_notify_pointer(struct sst_hsw_stream *stream, void *data);
159
160 static inline u32 hsw_mixer_to_ipc(unsigned int value)
161 {
162         if (value >= ARRAY_SIZE(volume_map))
163                 return volume_map[0];
164         else
165                 return volume_map[value];
166 }
167
168 static inline unsigned int hsw_ipc_to_mixer(u32 value)
169 {
170         int i;
171
172         for (i = 0; i < ARRAY_SIZE(volume_map); i++) {
173                 if (volume_map[i] >= value)
174                         return i;
175         }
176
177         return i - 1;
178 }
179
180 static int hsw_stream_volume_put(struct snd_kcontrol *kcontrol,
181                                 struct snd_ctl_elem_value *ucontrol)
182 {
183         struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
184         struct soc_mixer_control *mc =
185                 (struct soc_mixer_control *)kcontrol->private_value;
186         struct hsw_priv_data *pdata =
187                 snd_soc_platform_get_drvdata(platform);
188         struct hsw_pcm_data *pcm_data;
189         struct sst_hsw *hsw = pdata->hsw;
190         u32 volume;
191         int dai, stream;
192
193         dai = mod_map[mc->reg].dai_id;
194         stream = mod_map[mc->reg].stream;
195         pcm_data = &pdata->pcm[dai][stream];
196
197         mutex_lock(&pcm_data->mutex);
198         pm_runtime_get_sync(pdata->dev);
199
200         if (!pcm_data->stream) {
201                 pcm_data->volume[0] =
202                         hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
203                 pcm_data->volume[1] =
204                         hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
205                 pm_runtime_mark_last_busy(pdata->dev);
206                 pm_runtime_put_autosuspend(pdata->dev);
207                 mutex_unlock(&pcm_data->mutex);
208                 return 0;
209         }
210
211         if (ucontrol->value.integer.value[0] ==
212                 ucontrol->value.integer.value[1]) {
213                 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
214                 /* apply volume value to all channels */
215                 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, SST_HSW_CHANNELS_ALL, volume);
216         } else {
217                 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
218                 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, 0, volume);
219                 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
220                 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, 1, volume);
221         }
222
223         pm_runtime_mark_last_busy(pdata->dev);
224         pm_runtime_put_autosuspend(pdata->dev);
225         mutex_unlock(&pcm_data->mutex);
226         return 0;
227 }
228
229 static int hsw_stream_volume_get(struct snd_kcontrol *kcontrol,
230                                 struct snd_ctl_elem_value *ucontrol)
231 {
232         struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
233         struct soc_mixer_control *mc =
234                 (struct soc_mixer_control *)kcontrol->private_value;
235         struct hsw_priv_data *pdata =
236                 snd_soc_platform_get_drvdata(platform);
237         struct hsw_pcm_data *pcm_data;
238         struct sst_hsw *hsw = pdata->hsw;
239         u32 volume;
240         int dai, stream;
241
242         dai = mod_map[mc->reg].dai_id;
243         stream = mod_map[mc->reg].stream;
244         pcm_data = &pdata->pcm[dai][stream];
245
246         mutex_lock(&pcm_data->mutex);
247         pm_runtime_get_sync(pdata->dev);
248
249         if (!pcm_data->stream) {
250                 ucontrol->value.integer.value[0] =
251                         hsw_ipc_to_mixer(pcm_data->volume[0]);
252                 ucontrol->value.integer.value[1] =
253                         hsw_ipc_to_mixer(pcm_data->volume[1]);
254                 pm_runtime_mark_last_busy(pdata->dev);
255                 pm_runtime_put_autosuspend(pdata->dev);
256                 mutex_unlock(&pcm_data->mutex);
257                 return 0;
258         }
259
260         sst_hsw_stream_get_volume(hsw, pcm_data->stream, 0, 0, &volume);
261         ucontrol->value.integer.value[0] = hsw_ipc_to_mixer(volume);
262         sst_hsw_stream_get_volume(hsw, pcm_data->stream, 0, 1, &volume);
263         ucontrol->value.integer.value[1] = hsw_ipc_to_mixer(volume);
264
265         pm_runtime_mark_last_busy(pdata->dev);
266         pm_runtime_put_autosuspend(pdata->dev);
267         mutex_unlock(&pcm_data->mutex);
268
269         return 0;
270 }
271
272 static int hsw_volume_put(struct snd_kcontrol *kcontrol,
273                                 struct snd_ctl_elem_value *ucontrol)
274 {
275         struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
276         struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
277         struct sst_hsw *hsw = pdata->hsw;
278         u32 volume;
279
280         pm_runtime_get_sync(pdata->dev);
281
282         if (ucontrol->value.integer.value[0] ==
283                 ucontrol->value.integer.value[1]) {
284
285                 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
286                 sst_hsw_mixer_set_volume(hsw, 0, SST_HSW_CHANNELS_ALL, volume);
287
288         } else {
289                 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
290                 sst_hsw_mixer_set_volume(hsw, 0, 0, volume);
291
292                 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
293                 sst_hsw_mixer_set_volume(hsw, 0, 1, volume);
294         }
295
296         pm_runtime_mark_last_busy(pdata->dev);
297         pm_runtime_put_autosuspend(pdata->dev);
298         return 0;
299 }
300
301 static int hsw_volume_get(struct snd_kcontrol *kcontrol,
302                                 struct snd_ctl_elem_value *ucontrol)
303 {
304         struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
305         struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
306         struct sst_hsw *hsw = pdata->hsw;
307         unsigned int volume = 0;
308
309         pm_runtime_get_sync(pdata->dev);
310         sst_hsw_mixer_get_volume(hsw, 0, 0, &volume);
311         ucontrol->value.integer.value[0] = hsw_ipc_to_mixer(volume);
312
313         sst_hsw_mixer_get_volume(hsw, 0, 1, &volume);
314         ucontrol->value.integer.value[1] = hsw_ipc_to_mixer(volume);
315
316         pm_runtime_mark_last_busy(pdata->dev);
317         pm_runtime_put_autosuspend(pdata->dev);
318         return 0;
319 }
320
321 /* TLV used by both global and stream volumes */
322 static const DECLARE_TLV_DB_SCALE(hsw_vol_tlv, -9000, 300, 1);
323
324 /* System Pin has no volume control */
325 static const struct snd_kcontrol_new hsw_volume_controls[] = {
326         /* Global DSP volume */
327         SOC_DOUBLE_EXT_TLV("Master Playback Volume", 0, 0, 8,
328                 ARRAY_SIZE(volume_map) - 1, 0,
329                 hsw_volume_get, hsw_volume_put, hsw_vol_tlv),
330         /* Offload 0 volume */
331         SOC_DOUBLE_EXT_TLV("Media0 Playback Volume", 1, 0, 8,
332                 ARRAY_SIZE(volume_map) - 1, 0,
333                 hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
334         /* Offload 1 volume */
335         SOC_DOUBLE_EXT_TLV("Media1 Playback Volume", 2, 0, 8,
336                 ARRAY_SIZE(volume_map) - 1, 0,
337                 hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
338         /* Mic Capture volume */
339         SOC_DOUBLE_EXT_TLV("Mic Capture Volume", 4, 0, 8,
340                 ARRAY_SIZE(volume_map) - 1, 0,
341                 hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
342 };
343
344 /* Create DMA buffer page table for DSP */
345 static int create_adsp_page_table(struct snd_pcm_substream *substream,
346         struct hsw_priv_data *pdata, struct snd_soc_pcm_runtime *rtd,
347         unsigned char *dma_area, size_t size, int pcm)
348 {
349         struct snd_dma_buffer *dmab = snd_pcm_get_dma_buf(substream);
350         int i, pages, stream = substream->stream;
351
352         pages = snd_sgbuf_aligned_pages(size);
353
354         dev_dbg(rtd->dev, "generating page table for %p size 0x%zu pages %d\n",
355                 dma_area, size, pages);
356
357         for (i = 0; i < pages; i++) {
358                 u32 idx = (((i << 2) + i)) >> 1;
359                 u32 pfn = snd_sgbuf_get_addr(dmab, i * PAGE_SIZE) >> PAGE_SHIFT;
360                 u32 *pg_table;
361
362                 dev_dbg(rtd->dev, "pfn i %i idx %d pfn %x\n", i, idx, pfn);
363
364                 pg_table = (u32 *)(pdata->dmab[pcm][stream].area + idx);
365
366                 if (i & 1)
367                         *pg_table |= (pfn << 4);
368                 else
369                         *pg_table |= pfn;
370         }
371
372         return 0;
373 }
374
375 /* this may get called several times by oss emulation */
376 static int hsw_pcm_hw_params(struct snd_pcm_substream *substream,
377                               struct snd_pcm_hw_params *params)
378 {
379         struct snd_soc_pcm_runtime *rtd = substream->private_data;
380         struct snd_pcm_runtime *runtime = substream->runtime;
381         struct hsw_priv_data *pdata =
382                 snd_soc_platform_get_drvdata(rtd->platform);
383         struct hsw_pcm_data *pcm_data;
384         struct sst_hsw *hsw = pdata->hsw;
385         struct sst_module *module_data;
386         struct sst_dsp *dsp;
387         struct snd_dma_buffer *dmab;
388         enum sst_hsw_stream_type stream_type;
389         enum sst_hsw_stream_path_id path_id;
390         u32 rate, bits, map, pages, module_id;
391         u8 channels;
392         int ret, dai;
393
394         dai = mod_map[rtd->cpu_dai->id].dai_id;
395         pcm_data = &pdata->pcm[dai][substream->stream];
396
397         /* check if we are being called a subsequent time */
398         if (pcm_data->allocated) {
399                 ret = sst_hsw_stream_reset(hsw, pcm_data->stream);
400                 if (ret < 0)
401                         dev_dbg(rtd->dev, "error: reset stream failed %d\n",
402                                 ret);
403
404                 ret = sst_hsw_stream_free(hsw, pcm_data->stream);
405                 if (ret < 0) {
406                         dev_dbg(rtd->dev, "error: free stream failed %d\n",
407                                 ret);
408                         return ret;
409                 }
410                 pcm_data->allocated = false;
411
412                 pcm_data->stream = sst_hsw_stream_new(hsw, rtd->cpu_dai->id,
413                         hsw_notify_pointer, pcm_data);
414                 if (pcm_data->stream == NULL) {
415                         dev_err(rtd->dev, "error: failed to create stream\n");
416                         return -EINVAL;
417                 }
418         }
419
420         /* stream direction */
421         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
422                 path_id = SST_HSW_STREAM_PATH_SSP0_OUT;
423         else
424                 path_id = SST_HSW_STREAM_PATH_SSP0_IN;
425
426         /* DSP stream type depends on DAI ID */
427         switch (rtd->cpu_dai->id) {
428         case 0:
429                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
430                         stream_type = SST_HSW_STREAM_TYPE_SYSTEM;
431                         module_id = SST_HSW_MODULE_PCM_SYSTEM;
432                 }
433                 else {
434                         stream_type = SST_HSW_STREAM_TYPE_CAPTURE;
435                         module_id = SST_HSW_MODULE_PCM_CAPTURE;
436                 }
437                 break;
438         case 1:
439         case 2:
440                 stream_type = SST_HSW_STREAM_TYPE_RENDER;
441                 module_id = SST_HSW_MODULE_PCM;
442                 break;
443         case 3:
444                 /* path ID needs to be OUT for loopback */
445                 stream_type = SST_HSW_STREAM_TYPE_LOOPBACK;
446                 path_id = SST_HSW_STREAM_PATH_SSP0_OUT;
447                 module_id = SST_HSW_MODULE_PCM_REFERENCE;
448                 break;
449         default:
450                 dev_err(rtd->dev, "error: invalid DAI ID %d\n",
451                         rtd->cpu_dai->id);
452                 return -EINVAL;
453         };
454
455         ret = sst_hsw_stream_format(hsw, pcm_data->stream,
456                 path_id, stream_type, SST_HSW_STREAM_FORMAT_PCM_FORMAT);
457         if (ret < 0) {
458                 dev_err(rtd->dev, "error: failed to set format %d\n", ret);
459                 return ret;
460         }
461
462         rate = params_rate(params);
463         ret = sst_hsw_stream_set_rate(hsw, pcm_data->stream, rate);
464         if (ret < 0) {
465                 dev_err(rtd->dev, "error: could not set rate %d\n", rate);
466                 return ret;
467         }
468
469         switch (params_format(params)) {
470         case SNDRV_PCM_FORMAT_S16_LE:
471                 bits = SST_HSW_DEPTH_16BIT;
472                 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 16);
473                 break;
474         case SNDRV_PCM_FORMAT_S24_LE:
475                 bits = SST_HSW_DEPTH_32BIT;
476                 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 24);
477                 break;
478         case SNDRV_PCM_FORMAT_S8:
479                 bits = SST_HSW_DEPTH_8BIT;
480                 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 8);
481                 break;
482         case SNDRV_PCM_FORMAT_S32_LE:
483                 bits = SST_HSW_DEPTH_32BIT;
484                 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 32);
485                 break;
486         default:
487                 dev_err(rtd->dev, "error: invalid format %d\n",
488                         params_format(params));
489                 return -EINVAL;
490         }
491
492         ret = sst_hsw_stream_set_bits(hsw, pcm_data->stream, bits);
493         if (ret < 0) {
494                 dev_err(rtd->dev, "error: could not set bits %d\n", bits);
495                 return ret;
496         }
497
498         channels = params_channels(params);
499         map = create_channel_map(SST_HSW_CHANNEL_CONFIG_STEREO);
500         sst_hsw_stream_set_map_config(hsw, pcm_data->stream,
501                         map, SST_HSW_CHANNEL_CONFIG_STEREO);
502
503         ret = sst_hsw_stream_set_channels(hsw, pcm_data->stream, channels);
504         if (ret < 0) {
505                 dev_err(rtd->dev, "error: could not set channels %d\n",
506                         channels);
507                 return ret;
508         }
509
510         ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
511         if (ret < 0) {
512                 dev_err(rtd->dev, "error: could not allocate %d bytes for PCM %d\n",
513                         params_buffer_bytes(params), ret);
514                 return ret;
515         }
516
517         dmab = snd_pcm_get_dma_buf(substream);
518
519         ret = create_adsp_page_table(substream, pdata, rtd, runtime->dma_area,
520                 runtime->dma_bytes, rtd->cpu_dai->id);
521         if (ret < 0)
522                 return ret;
523
524         sst_hsw_stream_set_style(hsw, pcm_data->stream,
525                 SST_HSW_INTERLEAVING_PER_CHANNEL);
526
527         if (runtime->dma_bytes % PAGE_SIZE)
528                 pages = (runtime->dma_bytes / PAGE_SIZE) + 1;
529         else
530                 pages = runtime->dma_bytes / PAGE_SIZE;
531
532         ret = sst_hsw_stream_buffer(hsw, pcm_data->stream,
533                 pdata->dmab[rtd->cpu_dai->id][substream->stream].addr,
534                 pages, runtime->dma_bytes, 0,
535                 snd_sgbuf_get_addr(dmab, 0) >> PAGE_SHIFT);
536         if (ret < 0) {
537                 dev_err(rtd->dev, "error: failed to set DMA buffer %d\n", ret);
538                 return ret;
539         }
540
541         dsp = sst_hsw_get_dsp(hsw);
542
543         module_data = sst_module_get_from_id(dsp, module_id);
544         if (module_data == NULL) {
545                 dev_err(rtd->dev, "error: failed to get module config\n");
546                 return -EINVAL;
547         }
548
549         sst_hsw_stream_set_module_info(hsw, pcm_data->stream,
550                 pcm_data->runtime);
551
552         ret = sst_hsw_stream_commit(hsw, pcm_data->stream);
553         if (ret < 0) {
554                 dev_err(rtd->dev, "error: failed to commit stream %d\n", ret);
555                 return ret;
556         }
557
558         if (!pcm_data->allocated) {
559                 /* Set previous saved volume */
560                 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0,
561                                 0, pcm_data->volume[0]);
562                 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0,
563                                 1, pcm_data->volume[1]);
564                 pcm_data->allocated = true;
565         }
566
567         ret = sst_hsw_stream_pause(hsw, pcm_data->stream, 1);
568         if (ret < 0)
569                 dev_err(rtd->dev, "error: failed to pause %d\n", ret);
570
571         return 0;
572 }
573
574 static int hsw_pcm_hw_free(struct snd_pcm_substream *substream)
575 {
576         snd_pcm_lib_free_pages(substream);
577         return 0;
578 }
579
580 static int hsw_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
581 {
582         struct snd_soc_pcm_runtime *rtd = substream->private_data;
583         struct hsw_priv_data *pdata =
584                 snd_soc_platform_get_drvdata(rtd->platform);
585         struct hsw_pcm_data *pcm_data;
586         struct sst_hsw_stream *sst_stream;
587         struct sst_hsw *hsw = pdata->hsw;
588         struct snd_pcm_runtime *runtime = substream->runtime;
589         snd_pcm_uframes_t pos;
590         int dai;
591
592         dai = mod_map[rtd->cpu_dai->id].dai_id;
593         pcm_data = &pdata->pcm[dai][substream->stream];
594         sst_stream = pcm_data->stream;
595
596         switch (cmd) {
597         case SNDRV_PCM_TRIGGER_START:
598         case SNDRV_PCM_TRIGGER_RESUME:
599         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
600                 sst_hsw_stream_set_silence_start(hsw, sst_stream, false);
601                 sst_hsw_stream_resume(hsw, pcm_data->stream, 0);
602                 break;
603         case SNDRV_PCM_TRIGGER_STOP:
604         case SNDRV_PCM_TRIGGER_SUSPEND:
605         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
606                 sst_hsw_stream_set_silence_start(hsw, sst_stream, false);
607                 sst_hsw_stream_pause(hsw, pcm_data->stream, 0);
608                 break;
609         case SNDRV_PCM_TRIGGER_DRAIN:
610                 pos = runtime->control->appl_ptr % runtime->buffer_size;
611                 sst_hsw_stream_set_old_position(hsw, pcm_data->stream, pos);
612                 sst_hsw_stream_set_silence_start(hsw, sst_stream, true);
613                 break;
614         default:
615                 break;
616         }
617
618         return 0;
619 }
620
621 static u32 hsw_notify_pointer(struct sst_hsw_stream *stream, void *data)
622 {
623         struct hsw_pcm_data *pcm_data = data;
624         struct snd_pcm_substream *substream = pcm_data->substream;
625         struct snd_pcm_runtime *runtime = substream->runtime;
626         struct snd_soc_pcm_runtime *rtd = substream->private_data;
627         struct hsw_priv_data *pdata =
628                 snd_soc_platform_get_drvdata(rtd->platform);
629         struct sst_hsw *hsw = pdata->hsw;
630         u32 pos;
631         snd_pcm_uframes_t position = bytes_to_frames(runtime,
632                  sst_hsw_get_dsp_position(hsw, pcm_data->stream));
633         unsigned char *dma_area = runtime->dma_area;
634         snd_pcm_uframes_t dma_frames =
635                 bytes_to_frames(runtime, runtime->dma_bytes);
636         snd_pcm_uframes_t old_position;
637         ssize_t samples;
638
639         pos = frames_to_bytes(runtime,
640                 (runtime->control->appl_ptr % runtime->buffer_size));
641
642         dev_vdbg(rtd->dev, "PCM: App pointer %d bytes\n", pos);
643
644         /* SST fw don't know where to stop dma
645          * So, SST driver need to clean the data which has been consumed
646          */
647         if (dma_area == NULL || dma_frames <= 0
648                 || (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
649                 || !sst_hsw_stream_get_silence_start(hsw, stream)) {
650                 snd_pcm_period_elapsed(substream);
651                 return pos;
652         }
653
654         old_position = sst_hsw_stream_get_old_position(hsw, stream);
655         if (position > old_position) {
656                 if (position < dma_frames) {
657                         samples = SST_SAMPLES(runtime, position - old_position);
658                         snd_pcm_format_set_silence(runtime->format,
659                                 SST_OLD_POSITION(dma_area,
660                                         runtime, old_position),
661                                 samples);
662                 } else
663                         dev_err(rtd->dev, "PCM: position is wrong\n");
664         } else {
665                 if (old_position < dma_frames) {
666                         samples = SST_SAMPLES(runtime,
667                                 dma_frames - old_position);
668                         snd_pcm_format_set_silence(runtime->format,
669                                 SST_OLD_POSITION(dma_area,
670                                         runtime, old_position),
671                                 samples);
672                 } else
673                         dev_err(rtd->dev, "PCM: dma_bytes is wrong\n");
674                 if (position < dma_frames) {
675                         samples = SST_SAMPLES(runtime, position);
676                         snd_pcm_format_set_silence(runtime->format,
677                                 dma_area, samples);
678                 } else
679                         dev_err(rtd->dev, "PCM: position is wrong\n");
680         }
681         sst_hsw_stream_set_old_position(hsw, stream, position);
682
683         /* let alsa know we have play a period */
684         snd_pcm_period_elapsed(substream);
685         return pos;
686 }
687
688 static snd_pcm_uframes_t hsw_pcm_pointer(struct snd_pcm_substream *substream)
689 {
690         struct snd_soc_pcm_runtime *rtd = substream->private_data;
691         struct snd_pcm_runtime *runtime = substream->runtime;
692         struct hsw_priv_data *pdata =
693                 snd_soc_platform_get_drvdata(rtd->platform);
694         struct hsw_pcm_data *pcm_data;
695         struct sst_hsw *hsw = pdata->hsw;
696         snd_pcm_uframes_t offset;
697         uint64_t ppos;
698         u32 position;
699         int dai;
700
701         dai = mod_map[rtd->cpu_dai->id].dai_id;
702         pcm_data = &pdata->pcm[dai][substream->stream];
703         position = sst_hsw_get_dsp_position(hsw, pcm_data->stream);
704
705         offset = bytes_to_frames(runtime, position);
706         ppos = sst_hsw_get_dsp_presentation_position(hsw, pcm_data->stream);
707
708         dev_vdbg(rtd->dev, "PCM: DMA pointer %du bytes, pos %llu\n",
709                 position, ppos);
710         return offset;
711 }
712
713 static int hsw_pcm_open(struct snd_pcm_substream *substream)
714 {
715         struct snd_soc_pcm_runtime *rtd = substream->private_data;
716         struct hsw_priv_data *pdata =
717                 snd_soc_platform_get_drvdata(rtd->platform);
718         struct hsw_pcm_data *pcm_data;
719         struct sst_hsw *hsw = pdata->hsw;
720         int dai;
721
722         dai = mod_map[rtd->cpu_dai->id].dai_id;
723         pcm_data = &pdata->pcm[dai][substream->stream];
724
725         mutex_lock(&pcm_data->mutex);
726         pm_runtime_get_sync(pdata->dev);
727
728         snd_soc_pcm_set_drvdata(rtd, pcm_data);
729         pcm_data->substream = substream;
730
731         snd_soc_set_runtime_hwparams(substream, &hsw_pcm_hardware);
732
733         pcm_data->stream = sst_hsw_stream_new(hsw, rtd->cpu_dai->id,
734                 hsw_notify_pointer, pcm_data);
735         if (pcm_data->stream == NULL) {
736                 dev_err(rtd->dev, "error: failed to create stream\n");
737                 pm_runtime_mark_last_busy(pdata->dev);
738                 pm_runtime_put_autosuspend(pdata->dev);
739                 mutex_unlock(&pcm_data->mutex);
740                 return -EINVAL;
741         }
742
743         mutex_unlock(&pcm_data->mutex);
744         return 0;
745 }
746
747 static int hsw_pcm_close(struct snd_pcm_substream *substream)
748 {
749         struct snd_soc_pcm_runtime *rtd = substream->private_data;
750         struct hsw_priv_data *pdata =
751                 snd_soc_platform_get_drvdata(rtd->platform);
752         struct hsw_pcm_data *pcm_data;
753         struct sst_hsw *hsw = pdata->hsw;
754         int ret, dai;
755
756         dai = mod_map[rtd->cpu_dai->id].dai_id;
757         pcm_data = &pdata->pcm[dai][substream->stream];
758
759         mutex_lock(&pcm_data->mutex);
760         ret = sst_hsw_stream_reset(hsw, pcm_data->stream);
761         if (ret < 0) {
762                 dev_dbg(rtd->dev, "error: reset stream failed %d\n", ret);
763                 goto out;
764         }
765
766         ret = sst_hsw_stream_free(hsw, pcm_data->stream);
767         if (ret < 0) {
768                 dev_dbg(rtd->dev, "error: free stream failed %d\n", ret);
769                 goto out;
770         }
771         pcm_data->allocated = 0;
772         pcm_data->stream = NULL;
773
774 out:
775         pm_runtime_mark_last_busy(pdata->dev);
776         pm_runtime_put_autosuspend(pdata->dev);
777         mutex_unlock(&pcm_data->mutex);
778         return ret;
779 }
780
781 static struct snd_pcm_ops hsw_pcm_ops = {
782         .open           = hsw_pcm_open,
783         .close          = hsw_pcm_close,
784         .ioctl          = snd_pcm_lib_ioctl,
785         .hw_params      = hsw_pcm_hw_params,
786         .hw_free        = hsw_pcm_hw_free,
787         .trigger        = hsw_pcm_trigger,
788         .pointer        = hsw_pcm_pointer,
789         .page           = snd_pcm_sgbuf_ops_page,
790 };
791
792 static int hsw_pcm_create_modules(struct hsw_priv_data *pdata)
793 {
794         struct sst_hsw *hsw = pdata->hsw;
795         struct hsw_pcm_data *pcm_data;
796         int i;
797
798         for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
799                 pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
800
801                 /* create new runtime module, use same offset if recreated */
802                 pcm_data->runtime = sst_hsw_runtime_module_create(hsw,
803                         mod_map[i].mod_id, pcm_data->persistent_offset);
804                 if (pcm_data->runtime == NULL)
805                         goto err;
806                 pcm_data->persistent_offset =
807                         pcm_data->runtime->persistent_offset;
808         }
809
810         return 0;
811
812 err:
813         for (--i; i >= 0; i--) {
814                 pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
815                 sst_hsw_runtime_module_free(pcm_data->runtime);
816         }
817
818         return -ENODEV;
819 }
820
821 static void hsw_pcm_free_modules(struct hsw_priv_data *pdata)
822 {
823         struct hsw_pcm_data *pcm_data;
824         int i;
825
826         for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
827                 pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
828
829                 sst_hsw_runtime_module_free(pcm_data->runtime);
830         }
831 }
832
833 static int hsw_pcm_new(struct snd_soc_pcm_runtime *rtd)
834 {
835         struct snd_pcm *pcm = rtd->pcm;
836         struct snd_soc_platform *platform = rtd->platform;
837         struct sst_pdata *pdata = dev_get_platdata(platform->dev);
838         struct hsw_priv_data *priv_data = dev_get_drvdata(platform->dev);
839         struct device *dev = pdata->dma_dev;
840         int ret = 0;
841
842         if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream ||
843                         pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
844                 ret = snd_pcm_lib_preallocate_pages_for_all(pcm,
845                         SNDRV_DMA_TYPE_DEV_SG,
846                         dev,
847                         hsw_pcm_hardware.buffer_bytes_max,
848                         hsw_pcm_hardware.buffer_bytes_max);
849                 if (ret) {
850                         dev_err(rtd->dev, "dma buffer allocation failed %d\n",
851                                 ret);
852                         return ret;
853                 }
854         }
855         if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
856                 priv_data->pcm[rtd->cpu_dai->id][SNDRV_PCM_STREAM_PLAYBACK].hsw_pcm = pcm;
857         if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
858                 priv_data->pcm[rtd->cpu_dai->id][SNDRV_PCM_STREAM_CAPTURE].hsw_pcm = pcm;
859
860         return ret;
861 }
862
863 #define HSW_FORMATS \
864         (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
865
866 static struct snd_soc_dai_driver hsw_dais[] = {
867         {
868                 .name  = "System Pin",
869                 .id = HSW_PCM_DAI_ID_SYSTEM,
870                 .playback = {
871                         .stream_name = "System Playback",
872                         .channels_min = 2,
873                         .channels_max = 2,
874                         .rates = SNDRV_PCM_RATE_48000,
875                         .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
876                 },
877                 .capture = {
878                         .stream_name = "Analog Capture",
879                         .channels_min = 2,
880                         .channels_max = 4,
881                         .rates = SNDRV_PCM_RATE_48000,
882                         .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
883                 },
884         },
885         {
886                 /* PCM */
887                 .name  = "Offload0 Pin",
888                 .id = HSW_PCM_DAI_ID_OFFLOAD0,
889                 .playback = {
890                         .stream_name = "Offload0 Playback",
891                         .channels_min = 2,
892                         .channels_max = 2,
893                         .rates = SNDRV_PCM_RATE_8000_192000,
894                         .formats = HSW_FORMATS,
895                 },
896         },
897         {
898                 /* PCM */
899                 .name  = "Offload1 Pin",
900                 .id = HSW_PCM_DAI_ID_OFFLOAD1,
901                 .playback = {
902                         .stream_name = "Offload1 Playback",
903                         .channels_min = 2,
904                         .channels_max = 2,
905                         .rates = SNDRV_PCM_RATE_8000_192000,
906                         .formats = HSW_FORMATS,
907                 },
908         },
909         {
910                 .name  = "Loopback Pin",
911                 .id = HSW_PCM_DAI_ID_LOOPBACK,
912                 .capture = {
913                         .stream_name = "Loopback Capture",
914                         .channels_min = 2,
915                         .channels_max = 2,
916                         .rates = SNDRV_PCM_RATE_48000,
917                         .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
918                 },
919         },
920 };
921
922 static const struct snd_soc_dapm_widget widgets[] = {
923
924         /* Backend DAIs  */
925         SND_SOC_DAPM_AIF_IN("SSP0 CODEC IN", NULL, 0, SND_SOC_NOPM, 0, 0),
926         SND_SOC_DAPM_AIF_OUT("SSP0 CODEC OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
927         SND_SOC_DAPM_AIF_IN("SSP1 BT IN", NULL, 0, SND_SOC_NOPM, 0, 0),
928         SND_SOC_DAPM_AIF_OUT("SSP1 BT OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
929
930         /* Global Playback Mixer */
931         SND_SOC_DAPM_MIXER("Playback VMixer", SND_SOC_NOPM, 0, 0, NULL, 0),
932 };
933
934 static const struct snd_soc_dapm_route graph[] = {
935
936         /* Playback Mixer */
937         {"Playback VMixer", NULL, "System Playback"},
938         {"Playback VMixer", NULL, "Offload0 Playback"},
939         {"Playback VMixer", NULL, "Offload1 Playback"},
940
941         {"SSP0 CODEC OUT", NULL, "Playback VMixer"},
942
943         {"Analog Capture", NULL, "SSP0 CODEC IN"},
944 };
945
946 static int hsw_pcm_probe(struct snd_soc_platform *platform)
947 {
948         struct hsw_priv_data *priv_data = snd_soc_platform_get_drvdata(platform);
949         struct sst_pdata *pdata = dev_get_platdata(platform->dev);
950         struct device *dma_dev, *dev;
951         int i, ret = 0;
952
953         if (!pdata)
954                 return -ENODEV;
955
956         dev = platform->dev;
957         dma_dev = pdata->dma_dev;
958
959         priv_data->hsw = pdata->dsp;
960         priv_data->dev = platform->dev;
961         priv_data->pm_state = HSW_PM_STATE_D0;
962         priv_data->soc_card = platform->component.card;
963
964         /* allocate DSP buffer page tables */
965         for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) {
966
967                 /* playback */
968                 if (hsw_dais[i].playback.channels_min) {
969                         mutex_init(&priv_data->pcm[i][SNDRV_PCM_STREAM_PLAYBACK].mutex);
970                         ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dma_dev,
971                                 PAGE_SIZE, &priv_data->dmab[i][0]);
972                         if (ret < 0)
973                                 goto err;
974                 }
975
976                 /* capture */
977                 if (hsw_dais[i].capture.channels_min) {
978                         mutex_init(&priv_data->pcm[i][SNDRV_PCM_STREAM_CAPTURE].mutex);
979                         ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dma_dev,
980                                 PAGE_SIZE, &priv_data->dmab[i][1]);
981                         if (ret < 0)
982                                 goto err;
983                 }
984         }
985
986         /* allocate runtime modules */
987         hsw_pcm_create_modules(priv_data);
988
989         /* enable runtime PM with auto suspend */
990         pm_runtime_set_autosuspend_delay(platform->dev,
991                 SST_RUNTIME_SUSPEND_DELAY);
992         pm_runtime_use_autosuspend(platform->dev);
993         pm_runtime_enable(platform->dev);
994         pm_runtime_idle(platform->dev);
995
996         return 0;
997
998 err:
999         for (;i >= 0; i--) {
1000                 if (hsw_dais[i].playback.channels_min)
1001                         snd_dma_free_pages(&priv_data->dmab[i][0]);
1002                 if (hsw_dais[i].capture.channels_min)
1003                         snd_dma_free_pages(&priv_data->dmab[i][1]);
1004         }
1005         return ret;
1006 }
1007
1008 static int hsw_pcm_remove(struct snd_soc_platform *platform)
1009 {
1010         struct hsw_priv_data *priv_data =
1011                 snd_soc_platform_get_drvdata(platform);
1012         int i;
1013
1014         pm_runtime_disable(platform->dev);
1015         hsw_pcm_free_modules(priv_data);
1016
1017         for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) {
1018                 if (hsw_dais[i].playback.channels_min)
1019                         snd_dma_free_pages(&priv_data->dmab[i][0]);
1020                 if (hsw_dais[i].capture.channels_min)
1021                         snd_dma_free_pages(&priv_data->dmab[i][1]);
1022         }
1023
1024         return 0;
1025 }
1026
1027 static struct snd_soc_platform_driver hsw_soc_platform = {
1028         .probe          = hsw_pcm_probe,
1029         .remove         = hsw_pcm_remove,
1030         .ops            = &hsw_pcm_ops,
1031         .pcm_new        = hsw_pcm_new,
1032 };
1033
1034 static const struct snd_soc_component_driver hsw_dai_component = {
1035         .name = "haswell-dai",
1036         .controls = hsw_volume_controls,
1037         .num_controls = ARRAY_SIZE(hsw_volume_controls),
1038         .dapm_widgets = widgets,
1039         .num_dapm_widgets = ARRAY_SIZE(widgets),
1040         .dapm_routes = graph,
1041         .num_dapm_routes = ARRAY_SIZE(graph),
1042 };
1043
1044 static int hsw_pcm_dev_probe(struct platform_device *pdev)
1045 {
1046         struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
1047         struct hsw_priv_data *priv_data;
1048         int ret;
1049
1050         if (!sst_pdata)
1051                 return -EINVAL;
1052
1053         priv_data = devm_kzalloc(&pdev->dev, sizeof(*priv_data), GFP_KERNEL);
1054         if (!priv_data)
1055                 return -ENOMEM;
1056
1057         ret = sst_hsw_dsp_init(&pdev->dev, sst_pdata);
1058         if (ret < 0)
1059                 return -ENODEV;
1060
1061         priv_data->hsw = sst_pdata->dsp;
1062         platform_set_drvdata(pdev, priv_data);
1063
1064         ret = snd_soc_register_platform(&pdev->dev, &hsw_soc_platform);
1065         if (ret < 0)
1066                 goto err_plat;
1067
1068         ret = snd_soc_register_component(&pdev->dev, &hsw_dai_component,
1069                 hsw_dais, ARRAY_SIZE(hsw_dais));
1070         if (ret < 0)
1071                 goto err_comp;
1072
1073         return 0;
1074
1075 err_comp:
1076         snd_soc_unregister_platform(&pdev->dev);
1077 err_plat:
1078         sst_hsw_dsp_free(&pdev->dev, sst_pdata);
1079         return 0;
1080 }
1081
1082 static int hsw_pcm_dev_remove(struct platform_device *pdev)
1083 {
1084         struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
1085
1086         snd_soc_unregister_platform(&pdev->dev);
1087         snd_soc_unregister_component(&pdev->dev);
1088         sst_hsw_dsp_free(&pdev->dev, sst_pdata);
1089
1090         return 0;
1091 }
1092
1093 #ifdef CONFIG_PM
1094
1095 static int hsw_pcm_runtime_idle(struct device *dev)
1096 {
1097         return 0;
1098 }
1099
1100 static int hsw_pcm_runtime_suspend(struct device *dev)
1101 {
1102         struct hsw_priv_data *pdata = dev_get_drvdata(dev);
1103         struct sst_hsw *hsw = pdata->hsw;
1104
1105         if (pdata->pm_state >= HSW_PM_STATE_RTD3)
1106                 return 0;
1107
1108         sst_hsw_dsp_runtime_suspend(hsw);
1109         sst_hsw_dsp_runtime_sleep(hsw);
1110         pdata->pm_state = HSW_PM_STATE_RTD3;
1111
1112         return 0;
1113 }
1114
1115 static int hsw_pcm_runtime_resume(struct device *dev)
1116 {
1117         struct hsw_priv_data *pdata = dev_get_drvdata(dev);
1118         struct sst_hsw *hsw = pdata->hsw;
1119         int ret;
1120
1121         if (pdata->pm_state != HSW_PM_STATE_RTD3)
1122                 return 0;
1123
1124         ret = sst_hsw_dsp_load(hsw);
1125         if (ret < 0) {
1126                 dev_err(dev, "failed to reload %d\n", ret);
1127                 return ret;
1128         }
1129
1130         ret = hsw_pcm_create_modules(pdata);
1131         if (ret < 0) {
1132                 dev_err(dev, "failed to create modules %d\n", ret);
1133                 return ret;
1134         }
1135
1136         ret = sst_hsw_dsp_runtime_resume(hsw);
1137         if (ret < 0)
1138                 return ret;
1139         else if (ret == 1) /* no action required */
1140                 return 0;
1141
1142         pdata->pm_state = HSW_PM_STATE_D0;
1143         return ret;
1144 }
1145
1146 #else
1147 #define hsw_pcm_runtime_idle            NULL
1148 #define hsw_pcm_runtime_suspend         NULL
1149 #define hsw_pcm_runtime_resume          NULL
1150 #endif
1151
1152 #ifdef CONFIG_PM
1153
1154 static void hsw_pcm_complete(struct device *dev)
1155 {
1156         struct hsw_priv_data *pdata = dev_get_drvdata(dev);
1157         struct sst_hsw *hsw = pdata->hsw;
1158         struct hsw_pcm_data *pcm_data;
1159         int i, err;
1160
1161         if (pdata->pm_state != HSW_PM_STATE_D3)
1162                 return;
1163
1164         err = sst_hsw_dsp_load(hsw);
1165         if (err < 0) {
1166                 dev_err(dev, "failed to reload %d\n", err);
1167                 return;
1168         }
1169
1170         err = hsw_pcm_create_modules(pdata);
1171         if (err < 0) {
1172                 dev_err(dev, "failed to create modules %d\n", err);
1173                 return;
1174         }
1175
1176         for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
1177                 pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
1178
1179                 if (!pcm_data->substream)
1180                         continue;
1181
1182                 err = sst_module_runtime_restore(pcm_data->runtime,
1183                         &pcm_data->context);
1184                 if (err < 0)
1185                         dev_err(dev, "failed to restore context for PCM %d\n", i);
1186         }
1187
1188         snd_soc_resume(pdata->soc_card->dev);
1189
1190         err = sst_hsw_dsp_runtime_resume(hsw);
1191         if (err < 0)
1192                 return;
1193         else if (err == 1) /* no action required */
1194                 return;
1195
1196         pdata->pm_state = HSW_PM_STATE_D0;
1197         return;
1198 }
1199
1200 static int hsw_pcm_prepare(struct device *dev)
1201 {
1202         struct hsw_priv_data *pdata = dev_get_drvdata(dev);
1203         struct sst_hsw *hsw = pdata->hsw;
1204         struct hsw_pcm_data *pcm_data;
1205         int i, err;
1206
1207         if (pdata->pm_state == HSW_PM_STATE_D3)
1208                 return 0;
1209         else if (pdata->pm_state == HSW_PM_STATE_D0) {
1210                 /* suspend all active streams */
1211                 for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
1212                         pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
1213
1214                         if (!pcm_data->substream)
1215                                 continue;
1216                         dev_dbg(dev, "suspending pcm %d\n", i);
1217                         snd_pcm_suspend_all(pcm_data->hsw_pcm);
1218
1219                         /* We need to wait until the DSP FW stops the streams */
1220                         msleep(2);
1221                 }
1222
1223                 /* preserve persistent memory */
1224                 for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
1225                         pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
1226
1227                         if (!pcm_data->substream)
1228                                 continue;
1229
1230                         dev_dbg(dev, "saving context pcm %d\n", i);
1231                         err = sst_module_runtime_save(pcm_data->runtime,
1232                                 &pcm_data->context);
1233                         if (err < 0)
1234                                 dev_err(dev, "failed to save context for PCM %d\n", i);
1235                 }
1236                 /* enter D3 state and stall */
1237                 sst_hsw_dsp_runtime_suspend(hsw);
1238                 /* put the DSP to sleep */
1239                 sst_hsw_dsp_runtime_sleep(hsw);
1240         }
1241
1242         snd_soc_suspend(pdata->soc_card->dev);
1243         snd_soc_poweroff(pdata->soc_card->dev);
1244
1245         pdata->pm_state = HSW_PM_STATE_D3;
1246
1247         return 0;
1248 }
1249
1250 #else
1251 #define hsw_pcm_prepare         NULL
1252 #define hsw_pcm_complete        NULL
1253 #endif
1254
1255 static const struct dev_pm_ops hsw_pcm_pm = {
1256         .runtime_idle = hsw_pcm_runtime_idle,
1257         .runtime_suspend = hsw_pcm_runtime_suspend,
1258         .runtime_resume = hsw_pcm_runtime_resume,
1259         .prepare = hsw_pcm_prepare,
1260         .complete = hsw_pcm_complete,
1261 };
1262
1263 static struct platform_driver hsw_pcm_driver = {
1264         .driver = {
1265                 .name = "haswell-pcm-audio",
1266                 .pm = &hsw_pcm_pm,
1267         },
1268
1269         .probe = hsw_pcm_dev_probe,
1270         .remove = hsw_pcm_dev_remove,
1271 };
1272 module_platform_driver(hsw_pcm_driver);
1273
1274 MODULE_AUTHOR("Liam Girdwood, Xingchao Wang");
1275 MODULE_DESCRIPTION("Haswell/Lynxpoint + Broadwell/Wildcatpoint PCM");
1276 MODULE_LICENSE("GPL v2");
1277 MODULE_ALIAS("platform:haswell-pcm-audio");