1 // SPDX-License-Identifier: GPL-2.0-only
3 * Intel SST Haswell/Broadwell PCM Support
5 * Copyright (C) 2013, Intel Corporation. All rights reserved.
8 #include <linux/module.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/slab.h>
11 #include <linux/delay.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/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>
23 #include "../haswell/sst-haswell-ipc.h"
24 #include "../common/sst-dsp-priv.h"
25 #include "../common/sst-dsp.h"
27 #define HSW_PCM_COUNT 6
28 #define HSW_VOLUME_MAX 0x7FFFFFFF /* 0dB */
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))))
35 /* simple volume table */
36 static const u32 volume_map[] = {
70 #define HSW_PCM_PERIODS_MAX 64
71 #define HSW_PCM_PERIODS_MIN 2
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
79 static 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 |
85 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP |
86 SNDRV_PCM_INFO_DRAIN_TRIGGER,
87 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
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,
96 struct hsw_pcm_module_map {
99 enum sst_hsw_module_id mod_id;
102 /* private data for each PCM DSP stream */
103 struct hsw_pcm_data {
105 struct sst_hsw_stream *stream;
106 struct sst_module_runtime *runtime;
107 struct sst_module_runtime_context context;
108 struct snd_pcm *hsw_pcm;
110 struct snd_pcm_substream *substream;
111 struct snd_compr_stream *cstream;
115 int persistent_offset;
120 HSW_PM_STATE_RTD3 = 1,
124 /* private data for the driver */
125 struct hsw_priv_data {
129 enum hsw_pm_state pm_state;
130 struct snd_soc_card *soc_card;
131 struct sst_module_runtime *runtime_waves; /* sound effect module */
134 struct snd_dma_buffer dmab[HSW_PCM_COUNT][2];
137 struct hsw_pcm_data pcm[HSW_PCM_COUNT][2];
141 /* static mappings between PCMs and modules - may be dynamic in future */
142 static 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},
150 static u32 hsw_notify_pointer(struct sst_hsw_stream *stream, void *data);
152 static inline u32 hsw_mixer_to_ipc(unsigned int value)
154 if (value >= ARRAY_SIZE(volume_map))
155 return volume_map[0];
157 return volume_map[value];
160 static inline unsigned int hsw_ipc_to_mixer(u32 value)
164 for (i = 0; i < ARRAY_SIZE(volume_map); i++) {
165 if (volume_map[i] >= value)
172 static int hsw_stream_volume_put(struct snd_kcontrol *kcontrol,
173 struct snd_ctl_elem_value *ucontrol)
175 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
176 struct soc_mixer_control *mc =
177 (struct soc_mixer_control *)kcontrol->private_value;
178 struct hsw_priv_data *pdata =
179 snd_soc_component_get_drvdata(component);
180 struct hsw_pcm_data *pcm_data;
181 struct sst_hsw *hsw = pdata->hsw;
185 dai = mod_map[mc->reg].dai_id;
186 stream = mod_map[mc->reg].stream;
187 pcm_data = &pdata->pcm[dai][stream];
189 mutex_lock(&pcm_data->mutex);
190 pm_runtime_get_sync(pdata->dev);
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]);
197 pm_runtime_mark_last_busy(pdata->dev);
198 pm_runtime_put_autosuspend(pdata->dev);
199 mutex_unlock(&pcm_data->mutex);
203 if (ucontrol->value.integer.value[0] ==
204 ucontrol->value.integer.value[1]) {
205 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
206 /* apply volume value to all channels */
207 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, SST_HSW_CHANNELS_ALL, volume);
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);
215 pm_runtime_mark_last_busy(pdata->dev);
216 pm_runtime_put_autosuspend(pdata->dev);
217 mutex_unlock(&pcm_data->mutex);
221 static int hsw_stream_volume_get(struct snd_kcontrol *kcontrol,
222 struct snd_ctl_elem_value *ucontrol)
224 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
225 struct soc_mixer_control *mc =
226 (struct soc_mixer_control *)kcontrol->private_value;
227 struct hsw_priv_data *pdata =
228 snd_soc_component_get_drvdata(component);
229 struct hsw_pcm_data *pcm_data;
230 struct sst_hsw *hsw = pdata->hsw;
234 dai = mod_map[mc->reg].dai_id;
235 stream = mod_map[mc->reg].stream;
236 pcm_data = &pdata->pcm[dai][stream];
238 mutex_lock(&pcm_data->mutex);
239 pm_runtime_get_sync(pdata->dev);
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]);
246 pm_runtime_mark_last_busy(pdata->dev);
247 pm_runtime_put_autosuspend(pdata->dev);
248 mutex_unlock(&pcm_data->mutex);
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);
257 pm_runtime_mark_last_busy(pdata->dev);
258 pm_runtime_put_autosuspend(pdata->dev);
259 mutex_unlock(&pcm_data->mutex);
264 static int hsw_volume_put(struct snd_kcontrol *kcontrol,
265 struct snd_ctl_elem_value *ucontrol)
267 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
268 struct hsw_priv_data *pdata = snd_soc_component_get_drvdata(component);
269 struct sst_hsw *hsw = pdata->hsw;
272 pm_runtime_get_sync(pdata->dev);
274 if (ucontrol->value.integer.value[0] ==
275 ucontrol->value.integer.value[1]) {
277 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
278 sst_hsw_mixer_set_volume(hsw, 0, SST_HSW_CHANNELS_ALL, volume);
281 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
282 sst_hsw_mixer_set_volume(hsw, 0, 0, volume);
284 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
285 sst_hsw_mixer_set_volume(hsw, 0, 1, volume);
288 pm_runtime_mark_last_busy(pdata->dev);
289 pm_runtime_put_autosuspend(pdata->dev);
293 static int hsw_volume_get(struct snd_kcontrol *kcontrol,
294 struct snd_ctl_elem_value *ucontrol)
296 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
297 struct hsw_priv_data *pdata = snd_soc_component_get_drvdata(component);
298 struct sst_hsw *hsw = pdata->hsw;
299 unsigned int volume = 0;
301 pm_runtime_get_sync(pdata->dev);
302 sst_hsw_mixer_get_volume(hsw, 0, 0, &volume);
303 ucontrol->value.integer.value[0] = hsw_ipc_to_mixer(volume);
305 sst_hsw_mixer_get_volume(hsw, 0, 1, &volume);
306 ucontrol->value.integer.value[1] = hsw_ipc_to_mixer(volume);
308 pm_runtime_mark_last_busy(pdata->dev);
309 pm_runtime_put_autosuspend(pdata->dev);
313 static int hsw_waves_switch_get(struct snd_kcontrol *kcontrol,
314 struct snd_ctl_elem_value *ucontrol)
316 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
317 struct hsw_priv_data *pdata = snd_soc_component_get_drvdata(component);
318 struct sst_hsw *hsw = pdata->hsw;
319 enum sst_hsw_module_id id = SST_HSW_MODULE_WAVES;
321 ucontrol->value.integer.value[0] =
322 (sst_hsw_is_module_active(hsw, id) ||
323 sst_hsw_is_module_enabled_rtd3(hsw, id));
327 static int hsw_waves_switch_put(struct snd_kcontrol *kcontrol,
328 struct snd_ctl_elem_value *ucontrol)
330 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
331 struct hsw_priv_data *pdata = snd_soc_component_get_drvdata(component);
332 struct sst_hsw *hsw = pdata->hsw;
334 enum sst_hsw_module_id id = SST_HSW_MODULE_WAVES;
335 bool switch_on = (bool)ucontrol->value.integer.value[0];
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
340 if (sst_hsw_is_module_loaded(hsw, id)) {
341 if (switch_on == sst_hsw_is_module_active(hsw, id))
345 ret = sst_hsw_module_enable(hsw, id, 0);
347 ret = sst_hsw_module_disable(hsw, id, 0);
349 if (switch_on == sst_hsw_is_module_enabled_rtd3(hsw, id))
353 sst_hsw_set_module_enabled_rtd3(hsw, id);
355 sst_hsw_set_module_disabled_rtd3(hsw, id);
361 static int hsw_waves_param_get(struct snd_kcontrol *kcontrol,
362 struct snd_ctl_elem_value *ucontrol)
364 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
365 struct hsw_priv_data *pdata = snd_soc_component_get_drvdata(component);
366 struct sst_hsw *hsw = pdata->hsw;
368 /* return a matching line from param buffer */
369 return sst_hsw_load_param_line(hsw, ucontrol->value.bytes.data);
372 static int hsw_waves_param_put(struct snd_kcontrol *kcontrol,
373 struct snd_ctl_elem_value *ucontrol)
375 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
376 struct hsw_priv_data *pdata = snd_soc_component_get_drvdata(component);
377 struct sst_hsw *hsw = pdata->hsw;
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;
383 /* clear param buffer and reset buffer index */
384 if (param_id == 0xFF) {
385 sst_hsw_reset_param_buf(hsw);
389 /* store params into buffer */
390 ret = sst_hsw_store_param_line(hsw, ucontrol->value.bytes.data);
394 if (sst_hsw_is_module_active(hsw, id))
395 ret = sst_hsw_module_set_param(hsw, id, 0, param_id,
396 param_size, ucontrol->value.bytes.data);
400 /* TLV used by both global and stream volumes */
401 static const DECLARE_TLV_DB_SCALE(hsw_vol_tlv, -9000, 300, 1);
403 /* System Pin has no volume control */
404 static const struct snd_kcontrol_new hsw_volume_controls[] = {
405 /* Global DSP volume */
406 SOC_DOUBLE_EXT_TLV("Master Playback Volume", 0, 0, 8,
407 ARRAY_SIZE(volume_map) - 1, 0,
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,
411 ARRAY_SIZE(volume_map) - 1, 0,
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,
415 ARRAY_SIZE(volume_map) - 1, 0,
416 hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
417 /* Mic Capture volume */
418 SOC_DOUBLE_EXT_TLV("Mic Capture Volume", 4, 0, 8,
419 ARRAY_SIZE(volume_map) - 1, 0,
420 hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
421 /* enable/disable module waves */
422 SOC_SINGLE_BOOL_EXT("Waves Switch", 0,
423 hsw_waves_switch_get, hsw_waves_switch_put),
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),
429 /* Create DMA buffer page table for DSP */
430 static 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)
434 struct snd_dma_buffer *dmab = snd_pcm_get_dma_buf(substream);
435 int i, pages, stream = substream->stream;
437 pages = snd_sgbuf_aligned_pages(size);
439 dev_dbg(rtd->dev, "generating page table for %p size 0x%zx pages %d\n",
440 dma_area, size, pages);
442 for (i = 0; i < pages; i++) {
443 u32 idx = (((i << 2) + i)) >> 1;
444 u32 pfn = snd_sgbuf_get_addr(dmab, i * PAGE_SIZE) >> PAGE_SHIFT;
447 dev_dbg(rtd->dev, "pfn i %i idx %d pfn %x\n", i, idx, pfn);
449 pg_table = (u32 *)(pdata->dmab[pcm][stream].area + idx);
452 *pg_table |= (pfn << 4);
460 /* this may get called several times by oss emulation */
461 static int hsw_pcm_hw_params(struct snd_soc_component *component,
462 struct snd_pcm_substream *substream,
463 struct snd_pcm_hw_params *params)
465 struct snd_soc_pcm_runtime *rtd = substream->private_data;
466 struct snd_pcm_runtime *runtime = substream->runtime;
467 struct hsw_priv_data *pdata = snd_soc_component_get_drvdata(component);
468 struct hsw_pcm_data *pcm_data;
469 struct sst_hsw *hsw = pdata->hsw;
470 struct sst_module *module_data;
472 struct snd_dma_buffer *dmab;
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;
479 dai = mod_map[asoc_rtd_to_cpu(rtd, 0)->id].dai_id;
480 pcm_data = &pdata->pcm[dai][substream->stream];
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);
486 dev_dbg(rtd->dev, "error: reset stream failed %d\n",
489 ret = sst_hsw_stream_free(hsw, pcm_data->stream);
491 dev_dbg(rtd->dev, "error: free stream failed %d\n",
495 pcm_data->allocated = false;
497 pcm_data->stream = sst_hsw_stream_new(hsw, asoc_rtd_to_cpu(rtd, 0)->id,
498 hsw_notify_pointer, pcm_data);
499 if (pcm_data->stream == NULL) {
500 dev_err(rtd->dev, "error: failed to create stream\n");
505 /* stream direction */
506 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
507 path_id = SST_HSW_STREAM_PATH_SSP0_OUT;
509 path_id = SST_HSW_STREAM_PATH_SSP0_IN;
511 /* DSP stream type depends on DAI ID */
512 switch (asoc_rtd_to_cpu(rtd, 0)->id) {
514 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
515 stream_type = SST_HSW_STREAM_TYPE_SYSTEM;
516 module_id = SST_HSW_MODULE_PCM_SYSTEM;
519 stream_type = SST_HSW_STREAM_TYPE_CAPTURE;
520 module_id = SST_HSW_MODULE_PCM_CAPTURE;
525 stream_type = SST_HSW_STREAM_TYPE_RENDER;
526 module_id = SST_HSW_MODULE_PCM;
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;
535 dev_err(rtd->dev, "error: invalid DAI ID %d\n",
536 asoc_rtd_to_cpu(rtd, 0)->id);
540 ret = sst_hsw_stream_format(hsw, pcm_data->stream,
541 path_id, stream_type, SST_HSW_STREAM_FORMAT_PCM_FORMAT);
543 dev_err(rtd->dev, "error: failed to set format %d\n", ret);
547 rate = params_rate(params);
548 ret = sst_hsw_stream_set_rate(hsw, pcm_data->stream, rate);
550 dev_err(rtd->dev, "error: could not set rate %d\n", rate);
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);
559 case SNDRV_PCM_FORMAT_S24_LE:
560 bits = SST_HSW_DEPTH_32BIT;
561 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 24);
563 case SNDRV_PCM_FORMAT_S8:
564 bits = SST_HSW_DEPTH_8BIT;
565 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 8);
567 case SNDRV_PCM_FORMAT_S32_LE:
568 bits = SST_HSW_DEPTH_32BIT;
569 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 32);
572 dev_err(rtd->dev, "error: invalid format %d\n",
573 params_format(params));
577 ret = sst_hsw_stream_set_bits(hsw, pcm_data->stream, bits);
579 dev_err(rtd->dev, "error: could not set bits %d\n", bits);
583 channels = params_channels(params);
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);
588 ret = sst_hsw_stream_set_channels(hsw, pcm_data->stream, channels);
590 dev_err(rtd->dev, "error: could not set channels %d\n",
595 dmab = snd_pcm_get_dma_buf(substream);
597 ret = create_adsp_page_table(substream, pdata, rtd, runtime->dma_area,
598 runtime->dma_bytes, asoc_rtd_to_cpu(rtd, 0)->id);
602 sst_hsw_stream_set_style(hsw, pcm_data->stream,
603 SST_HSW_INTERLEAVING_PER_CHANNEL);
605 if (runtime->dma_bytes % PAGE_SIZE)
606 pages = (runtime->dma_bytes / PAGE_SIZE) + 1;
608 pages = runtime->dma_bytes / PAGE_SIZE;
610 ret = sst_hsw_stream_buffer(hsw, pcm_data->stream,
611 pdata->dmab[asoc_rtd_to_cpu(rtd, 0)->id][substream->stream].addr,
612 pages, runtime->dma_bytes, 0,
613 snd_sgbuf_get_addr(dmab, 0) >> PAGE_SHIFT);
615 dev_err(rtd->dev, "error: failed to set DMA buffer %d\n", ret);
619 dsp = sst_hsw_get_dsp(hsw);
621 module_data = sst_module_get_from_id(dsp, module_id);
622 if (module_data == NULL) {
623 dev_err(rtd->dev, "error: failed to get module config\n");
627 sst_hsw_stream_set_module_info(hsw, pcm_data->stream,
630 ret = sst_hsw_stream_commit(hsw, pcm_data->stream);
632 dev_err(rtd->dev, "error: failed to commit stream %d\n", ret);
636 if (!pcm_data->allocated) {
637 /* Set previous saved volume */
638 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0,
639 0, pcm_data->volume[0]);
640 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0,
641 1, pcm_data->volume[1]);
642 pcm_data->allocated = true;
645 ret = sst_hsw_stream_pause(hsw, pcm_data->stream, 1);
647 dev_err(rtd->dev, "error: failed to pause %d\n", ret);
652 static int hsw_pcm_trigger(struct snd_soc_component *component,
653 struct snd_pcm_substream *substream, int cmd)
655 struct snd_soc_pcm_runtime *rtd = substream->private_data;
656 struct hsw_priv_data *pdata = snd_soc_component_get_drvdata(component);
657 struct hsw_pcm_data *pcm_data;
658 struct sst_hsw_stream *sst_stream;
659 struct sst_hsw *hsw = pdata->hsw;
660 struct snd_pcm_runtime *runtime = substream->runtime;
661 snd_pcm_uframes_t pos;
664 dai = mod_map[asoc_rtd_to_cpu(rtd, 0)->id].dai_id;
665 pcm_data = &pdata->pcm[dai][substream->stream];
666 sst_stream = pcm_data->stream;
669 case SNDRV_PCM_TRIGGER_START:
670 case SNDRV_PCM_TRIGGER_RESUME:
671 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
672 sst_hsw_stream_set_silence_start(hsw, sst_stream, false);
673 sst_hsw_stream_resume(hsw, pcm_data->stream, 0);
675 case SNDRV_PCM_TRIGGER_STOP:
676 case SNDRV_PCM_TRIGGER_SUSPEND:
677 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
678 sst_hsw_stream_set_silence_start(hsw, sst_stream, false);
679 sst_hsw_stream_pause(hsw, pcm_data->stream, 0);
681 case SNDRV_PCM_TRIGGER_DRAIN:
682 pos = runtime->control->appl_ptr % runtime->buffer_size;
683 sst_hsw_stream_set_old_position(hsw, pcm_data->stream, pos);
684 sst_hsw_stream_set_silence_start(hsw, sst_stream, true);
693 static u32 hsw_notify_pointer(struct sst_hsw_stream *stream, void *data)
695 struct hsw_pcm_data *pcm_data = data;
696 struct snd_pcm_substream *substream = pcm_data->substream;
697 struct snd_pcm_runtime *runtime = substream->runtime;
698 struct snd_soc_pcm_runtime *rtd = substream->private_data;
699 struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
700 struct hsw_priv_data *pdata = snd_soc_component_get_drvdata(component);
701 struct sst_hsw *hsw = pdata->hsw;
703 snd_pcm_uframes_t position = bytes_to_frames(runtime,
704 sst_hsw_get_dsp_position(hsw, pcm_data->stream));
705 unsigned char *dma_area = runtime->dma_area;
706 snd_pcm_uframes_t dma_frames =
707 bytes_to_frames(runtime, runtime->dma_bytes);
708 snd_pcm_uframes_t old_position;
711 pos = frames_to_bytes(runtime,
712 (runtime->control->appl_ptr % runtime->buffer_size));
714 dev_vdbg(rtd->dev, "PCM: App pointer %d bytes\n", pos);
716 /* SST fw don't know where to stop dma
717 * So, SST driver need to clean the data which has been consumed
719 if (dma_area == NULL || dma_frames <= 0
720 || (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
721 || !sst_hsw_stream_get_silence_start(hsw, stream)) {
722 snd_pcm_period_elapsed(substream);
726 old_position = sst_hsw_stream_get_old_position(hsw, stream);
727 if (position > old_position) {
728 if (position < dma_frames) {
729 samples = SST_SAMPLES(runtime, position - old_position);
730 snd_pcm_format_set_silence(runtime->format,
731 SST_OLD_POSITION(dma_area,
732 runtime, old_position),
735 dev_err(rtd->dev, "PCM: position is wrong\n");
737 if (old_position < dma_frames) {
738 samples = SST_SAMPLES(runtime,
739 dma_frames - old_position);
740 snd_pcm_format_set_silence(runtime->format,
741 SST_OLD_POSITION(dma_area,
742 runtime, old_position),
745 dev_err(rtd->dev, "PCM: dma_bytes is wrong\n");
746 if (position < dma_frames) {
747 samples = SST_SAMPLES(runtime, position);
748 snd_pcm_format_set_silence(runtime->format,
751 dev_err(rtd->dev, "PCM: position is wrong\n");
753 sst_hsw_stream_set_old_position(hsw, stream, position);
755 /* let alsa know we have play a period */
756 snd_pcm_period_elapsed(substream);
760 static snd_pcm_uframes_t hsw_pcm_pointer(struct snd_soc_component *component,
761 struct snd_pcm_substream *substream)
763 struct snd_soc_pcm_runtime *rtd = substream->private_data;
764 struct snd_pcm_runtime *runtime = substream->runtime;
765 struct hsw_priv_data *pdata = snd_soc_component_get_drvdata(component);
766 struct hsw_pcm_data *pcm_data;
767 struct sst_hsw *hsw = pdata->hsw;
768 snd_pcm_uframes_t offset;
773 dai = mod_map[asoc_rtd_to_cpu(rtd, 0)->id].dai_id;
774 pcm_data = &pdata->pcm[dai][substream->stream];
775 position = sst_hsw_get_dsp_position(hsw, pcm_data->stream);
777 offset = bytes_to_frames(runtime, position);
778 ppos = sst_hsw_get_dsp_presentation_position(hsw, pcm_data->stream);
780 dev_vdbg(rtd->dev, "PCM: DMA pointer %du bytes, pos %llu\n",
785 static int hsw_pcm_open(struct snd_soc_component *component,
786 struct snd_pcm_substream *substream)
788 struct snd_soc_pcm_runtime *rtd = substream->private_data;
789 struct hsw_priv_data *pdata = snd_soc_component_get_drvdata(component);
790 struct hsw_pcm_data *pcm_data;
791 struct sst_hsw *hsw = pdata->hsw;
794 dai = mod_map[asoc_rtd_to_cpu(rtd, 0)->id].dai_id;
795 pcm_data = &pdata->pcm[dai][substream->stream];
797 mutex_lock(&pcm_data->mutex);
798 pm_runtime_get_sync(pdata->dev);
800 pcm_data->substream = substream;
802 snd_soc_set_runtime_hwparams(substream, &hsw_pcm_hardware);
804 pcm_data->stream = sst_hsw_stream_new(hsw, asoc_rtd_to_cpu(rtd, 0)->id,
805 hsw_notify_pointer, pcm_data);
806 if (pcm_data->stream == NULL) {
807 dev_err(rtd->dev, "error: failed to create stream\n");
808 pm_runtime_mark_last_busy(pdata->dev);
809 pm_runtime_put_autosuspend(pdata->dev);
810 mutex_unlock(&pcm_data->mutex);
814 mutex_unlock(&pcm_data->mutex);
818 static int hsw_pcm_close(struct snd_soc_component *component,
819 struct snd_pcm_substream *substream)
821 struct snd_soc_pcm_runtime *rtd = substream->private_data;
822 struct hsw_priv_data *pdata = snd_soc_component_get_drvdata(component);
823 struct hsw_pcm_data *pcm_data;
824 struct sst_hsw *hsw = pdata->hsw;
827 dai = mod_map[asoc_rtd_to_cpu(rtd, 0)->id].dai_id;
828 pcm_data = &pdata->pcm[dai][substream->stream];
830 mutex_lock(&pcm_data->mutex);
831 ret = sst_hsw_stream_reset(hsw, pcm_data->stream);
833 dev_dbg(rtd->dev, "error: reset stream failed %d\n", ret);
837 ret = sst_hsw_stream_free(hsw, pcm_data->stream);
839 dev_dbg(rtd->dev, "error: free stream failed %d\n", ret);
842 pcm_data->allocated = false;
843 pcm_data->stream = NULL;
846 pm_runtime_mark_last_busy(pdata->dev);
847 pm_runtime_put_autosuspend(pdata->dev);
848 mutex_unlock(&pcm_data->mutex);
852 static int hsw_pcm_create_modules(struct hsw_priv_data *pdata)
854 struct sst_hsw *hsw = pdata->hsw;
855 struct hsw_pcm_data *pcm_data;
858 for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
859 pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
861 /* create new runtime module, use same offset if recreated */
862 pcm_data->runtime = sst_hsw_runtime_module_create(hsw,
863 mod_map[i].mod_id, pcm_data->persistent_offset);
864 if (pcm_data->runtime == NULL)
866 pcm_data->persistent_offset =
867 pcm_data->runtime->persistent_offset;
870 /* create runtime blocks for module waves */
871 if (sst_hsw_is_module_loaded(hsw, SST_HSW_MODULE_WAVES)) {
872 pdata->runtime_waves = sst_hsw_runtime_module_create(hsw,
873 SST_HSW_MODULE_WAVES, 0);
874 if (pdata->runtime_waves == NULL)
881 for (--i; i >= 0; i--) {
882 pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
883 sst_hsw_runtime_module_free(pcm_data->runtime);
889 static void hsw_pcm_free_modules(struct hsw_priv_data *pdata)
891 struct sst_hsw *hsw = pdata->hsw;
892 struct hsw_pcm_data *pcm_data;
895 for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
896 pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
897 if (pcm_data->runtime){
898 sst_hsw_runtime_module_free(pcm_data->runtime);
899 pcm_data->runtime = NULL;
902 if (sst_hsw_is_module_loaded(hsw, SST_HSW_MODULE_WAVES) &&
903 pdata->runtime_waves) {
904 sst_hsw_runtime_module_free(pdata->runtime_waves);
905 pdata->runtime_waves = NULL;
909 static int hsw_pcm_new(struct snd_soc_component *component,
910 struct snd_soc_pcm_runtime *rtd)
912 struct snd_pcm *pcm = rtd->pcm;
913 struct sst_pdata *pdata = dev_get_platdata(component->dev);
914 struct hsw_priv_data *priv_data = dev_get_drvdata(component->dev);
915 struct device *dev = pdata->dma_dev;
917 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream ||
918 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
919 snd_pcm_set_managed_buffer_all(pcm,
920 SNDRV_DMA_TYPE_DEV_SG,
922 hsw_pcm_hardware.buffer_bytes_max,
923 hsw_pcm_hardware.buffer_bytes_max);
925 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
926 priv_data->pcm[asoc_rtd_to_cpu(rtd, 0)->id][SNDRV_PCM_STREAM_PLAYBACK].hsw_pcm = pcm;
927 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
928 priv_data->pcm[asoc_rtd_to_cpu(rtd, 0)->id][SNDRV_PCM_STREAM_CAPTURE].hsw_pcm = pcm;
933 #define HSW_FORMATS \
934 (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
936 static struct snd_soc_dai_driver hsw_dais[] = {
938 .name = "System Pin",
939 .id = HSW_PCM_DAI_ID_SYSTEM,
941 .stream_name = "System Playback",
944 .rates = SNDRV_PCM_RATE_48000,
945 .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
948 .stream_name = "Analog Capture",
951 .rates = SNDRV_PCM_RATE_48000,
952 .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
957 .name = "Offload0 Pin",
958 .id = HSW_PCM_DAI_ID_OFFLOAD0,
960 .stream_name = "Offload0 Playback",
963 .rates = SNDRV_PCM_RATE_8000_192000,
964 .formats = HSW_FORMATS,
969 .name = "Offload1 Pin",
970 .id = HSW_PCM_DAI_ID_OFFLOAD1,
972 .stream_name = "Offload1 Playback",
975 .rates = SNDRV_PCM_RATE_8000_192000,
976 .formats = HSW_FORMATS,
980 .name = "Loopback Pin",
981 .id = HSW_PCM_DAI_ID_LOOPBACK,
983 .stream_name = "Loopback Capture",
986 .rates = SNDRV_PCM_RATE_48000,
987 .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
992 static const struct snd_soc_dapm_widget widgets[] = {
995 SND_SOC_DAPM_AIF_IN("SSP0 CODEC IN", NULL, 0, SND_SOC_NOPM, 0, 0),
996 SND_SOC_DAPM_AIF_OUT("SSP0 CODEC OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
997 SND_SOC_DAPM_AIF_IN("SSP1 BT IN", NULL, 0, SND_SOC_NOPM, 0, 0),
998 SND_SOC_DAPM_AIF_OUT("SSP1 BT OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
1000 /* Global Playback Mixer */
1001 SND_SOC_DAPM_MIXER("Playback VMixer", SND_SOC_NOPM, 0, 0, NULL, 0),
1004 static const struct snd_soc_dapm_route graph[] = {
1006 /* Playback Mixer */
1007 {"Playback VMixer", NULL, "System Playback"},
1008 {"Playback VMixer", NULL, "Offload0 Playback"},
1009 {"Playback VMixer", NULL, "Offload1 Playback"},
1011 {"SSP0 CODEC OUT", NULL, "Playback VMixer"},
1013 {"Analog Capture", NULL, "SSP0 CODEC IN"},
1016 static int hsw_pcm_probe(struct snd_soc_component *component)
1018 struct hsw_priv_data *priv_data = snd_soc_component_get_drvdata(component);
1019 struct sst_pdata *pdata = dev_get_platdata(component->dev);
1020 struct device *dma_dev, *dev;
1026 dev = component->dev;
1027 dma_dev = pdata->dma_dev;
1029 priv_data->hsw = pdata->dsp;
1030 priv_data->dev = dev;
1031 priv_data->pm_state = HSW_PM_STATE_D0;
1032 priv_data->soc_card = component->card;
1034 /* allocate DSP buffer page tables */
1035 for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) {
1038 if (hsw_dais[i].playback.channels_min) {
1039 mutex_init(&priv_data->pcm[i][SNDRV_PCM_STREAM_PLAYBACK].mutex);
1040 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dma_dev,
1041 PAGE_SIZE, &priv_data->dmab[i][0]);
1047 if (hsw_dais[i].capture.channels_min) {
1048 mutex_init(&priv_data->pcm[i][SNDRV_PCM_STREAM_CAPTURE].mutex);
1049 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dma_dev,
1050 PAGE_SIZE, &priv_data->dmab[i][1]);
1056 /* allocate runtime modules */
1057 ret = hsw_pcm_create_modules(priv_data);
1061 /* enable runtime PM with auto suspend */
1062 pm_runtime_set_autosuspend_delay(dev, SST_RUNTIME_SUSPEND_DELAY);
1063 pm_runtime_use_autosuspend(dev);
1064 pm_runtime_enable(dev);
1065 pm_runtime_idle(dev);
1070 for (--i; i >= 0; i--) {
1071 if (hsw_dais[i].playback.channels_min)
1072 snd_dma_free_pages(&priv_data->dmab[i][0]);
1073 if (hsw_dais[i].capture.channels_min)
1074 snd_dma_free_pages(&priv_data->dmab[i][1]);
1079 static void hsw_pcm_remove(struct snd_soc_component *component)
1081 struct hsw_priv_data *priv_data =
1082 snd_soc_component_get_drvdata(component);
1085 pm_runtime_disable(component->dev);
1086 hsw_pcm_free_modules(priv_data);
1088 for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) {
1089 if (hsw_dais[i].playback.channels_min)
1090 snd_dma_free_pages(&priv_data->dmab[i][0]);
1091 if (hsw_dais[i].capture.channels_min)
1092 snd_dma_free_pages(&priv_data->dmab[i][1]);
1096 static const struct snd_soc_component_driver hsw_dai_component = {
1098 .probe = hsw_pcm_probe,
1099 .remove = hsw_pcm_remove,
1100 .open = hsw_pcm_open,
1101 .close = hsw_pcm_close,
1102 .hw_params = hsw_pcm_hw_params,
1103 .trigger = hsw_pcm_trigger,
1104 .pointer = hsw_pcm_pointer,
1105 .pcm_construct = hsw_pcm_new,
1106 .controls = hsw_volume_controls,
1107 .num_controls = ARRAY_SIZE(hsw_volume_controls),
1108 .dapm_widgets = widgets,
1109 .num_dapm_widgets = ARRAY_SIZE(widgets),
1110 .dapm_routes = graph,
1111 .num_dapm_routes = ARRAY_SIZE(graph),
1114 static int hsw_pcm_dev_probe(struct platform_device *pdev)
1116 struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
1117 struct hsw_priv_data *priv_data;
1123 priv_data = devm_kzalloc(&pdev->dev, sizeof(*priv_data), GFP_KERNEL);
1127 ret = sst_hsw_dsp_init(&pdev->dev, sst_pdata);
1131 priv_data->hsw = sst_pdata->dsp;
1132 platform_set_drvdata(pdev, priv_data);
1134 ret = devm_snd_soc_register_component(&pdev->dev, &hsw_dai_component,
1135 hsw_dais, ARRAY_SIZE(hsw_dais));
1142 sst_hsw_dsp_free(&pdev->dev, sst_pdata);
1146 static int hsw_pcm_dev_remove(struct platform_device *pdev)
1148 struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
1150 sst_hsw_dsp_free(&pdev->dev, sst_pdata);
1157 static int hsw_pcm_runtime_idle(struct device *dev)
1162 static int hsw_pcm_suspend(struct device *dev)
1164 struct hsw_priv_data *pdata = dev_get_drvdata(dev);
1165 struct sst_hsw *hsw = pdata->hsw;
1167 /* enter D3 state and stall */
1168 sst_hsw_dsp_runtime_suspend(hsw);
1169 /* free all runtime modules */
1170 hsw_pcm_free_modules(pdata);
1171 /* put the DSP to sleep, fw unloaded after runtime modules freed */
1172 sst_hsw_dsp_runtime_sleep(hsw);
1176 static int hsw_pcm_runtime_suspend(struct device *dev)
1178 struct hsw_priv_data *pdata = dev_get_drvdata(dev);
1179 struct sst_hsw *hsw = pdata->hsw;
1182 if (pdata->pm_state >= HSW_PM_STATE_RTD3)
1185 /* fw modules will be unloaded on RTD3, set flag to track */
1186 if (sst_hsw_is_module_active(hsw, SST_HSW_MODULE_WAVES)) {
1187 ret = sst_hsw_module_disable(hsw, SST_HSW_MODULE_WAVES, 0);
1190 sst_hsw_set_module_enabled_rtd3(hsw, SST_HSW_MODULE_WAVES);
1192 hsw_pcm_suspend(dev);
1193 pdata->pm_state = HSW_PM_STATE_RTD3;
1198 static int hsw_pcm_runtime_resume(struct device *dev)
1200 struct hsw_priv_data *pdata = dev_get_drvdata(dev);
1201 struct sst_hsw *hsw = pdata->hsw;
1204 if (pdata->pm_state != HSW_PM_STATE_RTD3)
1207 ret = sst_hsw_dsp_load(hsw);
1209 dev_err(dev, "failed to reload %d\n", ret);
1213 ret = hsw_pcm_create_modules(pdata);
1215 dev_err(dev, "failed to create modules %d\n", ret);
1219 ret = sst_hsw_dsp_runtime_resume(hsw);
1222 else if (ret == 1) /* no action required */
1225 /* check flag when resume */
1226 if (sst_hsw_is_module_enabled_rtd3(hsw, SST_HSW_MODULE_WAVES)) {
1227 ret = sst_hsw_module_enable(hsw, SST_HSW_MODULE_WAVES, 0);
1230 /* put parameters from buffer to dsp */
1231 ret = sst_hsw_launch_param_buf(hsw);
1235 sst_hsw_set_module_disabled_rtd3(hsw, SST_HSW_MODULE_WAVES);
1238 pdata->pm_state = HSW_PM_STATE_D0;
1243 #define hsw_pcm_runtime_idle NULL
1244 #define hsw_pcm_runtime_suspend NULL
1245 #define hsw_pcm_runtime_resume NULL
1250 static void hsw_pcm_complete(struct device *dev)
1252 struct hsw_priv_data *pdata = dev_get_drvdata(dev);
1253 struct sst_hsw *hsw = pdata->hsw;
1254 struct hsw_pcm_data *pcm_data;
1257 if (pdata->pm_state != HSW_PM_STATE_D3)
1260 err = sst_hsw_dsp_load(hsw);
1262 dev_err(dev, "failed to reload %d\n", err);
1266 err = hsw_pcm_create_modules(pdata);
1268 dev_err(dev, "failed to create modules %d\n", err);
1272 for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
1273 pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
1275 if (!pcm_data->substream)
1278 err = sst_module_runtime_restore(pcm_data->runtime,
1279 &pcm_data->context);
1281 dev_err(dev, "failed to restore context for PCM %d\n", i);
1284 snd_soc_resume(pdata->soc_card->dev);
1286 err = sst_hsw_dsp_runtime_resume(hsw);
1289 else if (err == 1) /* no action required */
1292 pdata->pm_state = HSW_PM_STATE_D0;
1296 static int hsw_pcm_prepare(struct device *dev)
1298 struct hsw_priv_data *pdata = dev_get_drvdata(dev);
1299 struct hsw_pcm_data *pcm_data;
1302 if (pdata->pm_state == HSW_PM_STATE_D3)
1304 else if (pdata->pm_state == HSW_PM_STATE_D0) {
1305 /* suspend all active streams */
1306 for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
1307 pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
1309 if (!pcm_data->substream)
1311 dev_dbg(dev, "suspending pcm %d\n", i);
1312 snd_pcm_suspend_all(pcm_data->hsw_pcm);
1314 /* We need to wait until the DSP FW stops the streams */
1318 /* preserve persistent memory */
1319 for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
1320 pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
1322 if (!pcm_data->substream)
1325 dev_dbg(dev, "saving context pcm %d\n", i);
1326 err = sst_module_runtime_save(pcm_data->runtime,
1327 &pcm_data->context);
1329 dev_err(dev, "failed to save context for PCM %d\n", i);
1331 hsw_pcm_suspend(dev);
1334 snd_soc_suspend(pdata->soc_card->dev);
1335 snd_soc_poweroff(pdata->soc_card->dev);
1337 pdata->pm_state = HSW_PM_STATE_D3;
1343 #define hsw_pcm_prepare NULL
1344 #define hsw_pcm_complete NULL
1347 static const struct dev_pm_ops hsw_pcm_pm = {
1348 .runtime_idle = hsw_pcm_runtime_idle,
1349 .runtime_suspend = hsw_pcm_runtime_suspend,
1350 .runtime_resume = hsw_pcm_runtime_resume,
1351 .prepare = hsw_pcm_prepare,
1352 .complete = hsw_pcm_complete,
1355 static struct platform_driver hsw_pcm_driver = {
1357 .name = "haswell-pcm-audio",
1361 .probe = hsw_pcm_dev_probe,
1362 .remove = hsw_pcm_dev_remove,
1364 module_platform_driver(hsw_pcm_driver);
1366 MODULE_AUTHOR("Liam Girdwood, Xingchao Wang");
1367 MODULE_DESCRIPTION("Haswell/Lynxpoint + Broadwell/Wildcatpoint PCM");
1368 MODULE_LICENSE("GPL v2");
1369 MODULE_ALIAS("platform:haswell-pcm-audio");