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