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