2 * Intel SST Haswell/Broadwell PCM Support
4 * Copyright (C) 2013, Intel Corporation. All rights reserved.
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.
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.
17 #include <linux/module.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/slab.h>
20 #include <linux/delay.h>
21 #include <linux/pm_runtime.h>
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>
32 #include "sst-haswell-ipc.h"
33 #include "sst-dsp-priv.h"
36 #define HSW_PCM_COUNT 6
37 #define HSW_VOLUME_MAX 0x7FFFFFFF /* 0dB */
39 #define SST_OLD_POSITION(d, r, o) ((d) + \
40 frames_to_bytes(r, o))
41 #define SST_SAMPLES(r, x) (bytes_to_samples(r, \
42 frames_to_bytes(r, (x))))
44 /* simple volume table */
45 static const u32 volume_map[] = {
79 #define HSW_PCM_PERIODS_MAX 64
80 #define HSW_PCM_PERIODS_MIN 2
82 #define HSW_PCM_DAI_ID_SYSTEM 0
83 #define HSW_PCM_DAI_ID_OFFLOAD0 1
84 #define HSW_PCM_DAI_ID_OFFLOAD1 2
85 #define HSW_PCM_DAI_ID_LOOPBACK 3
88 static const struct snd_pcm_hardware hsw_pcm_hardware = {
89 .info = SNDRV_PCM_INFO_MMAP |
90 SNDRV_PCM_INFO_MMAP_VALID |
91 SNDRV_PCM_INFO_INTERLEAVED |
92 SNDRV_PCM_INFO_PAUSE |
93 SNDRV_PCM_INFO_RESUME |
94 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP |
95 SNDRV_PCM_INFO_DRAIN_TRIGGER,
96 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
97 SNDRV_PCM_FMTBIT_S32_LE,
98 .period_bytes_min = PAGE_SIZE,
99 .period_bytes_max = (HSW_PCM_PERIODS_MAX / HSW_PCM_PERIODS_MIN) * PAGE_SIZE,
100 .periods_min = HSW_PCM_PERIODS_MIN,
101 .periods_max = HSW_PCM_PERIODS_MAX,
102 .buffer_bytes_max = HSW_PCM_PERIODS_MAX * PAGE_SIZE,
105 struct hsw_pcm_module_map {
108 enum sst_hsw_module_id mod_id;
111 /* private data for each PCM DSP stream */
112 struct hsw_pcm_data {
114 struct sst_hsw_stream *stream;
115 struct sst_module_runtime *runtime;
116 struct sst_module_runtime_context context;
117 struct snd_pcm *hsw_pcm;
119 struct snd_pcm_substream *substream;
120 struct snd_compr_stream *cstream;
124 int persistent_offset;
129 HSW_PM_STATE_RTD3 = 1,
133 /* private data for the driver */
134 struct hsw_priv_data {
138 enum hsw_pm_state pm_state;
139 struct snd_soc_card *soc_card;
142 struct snd_dma_buffer dmab[HSW_PCM_COUNT][2];
145 struct hsw_pcm_data pcm[HSW_PCM_COUNT][2];
149 /* static mappings between PCMs and modules - may be dynamic in future */
150 static struct hsw_pcm_module_map mod_map[] = {
151 {HSW_PCM_DAI_ID_SYSTEM, 0, SST_HSW_MODULE_PCM_SYSTEM},
152 {HSW_PCM_DAI_ID_OFFLOAD0, 0, SST_HSW_MODULE_PCM},
153 {HSW_PCM_DAI_ID_OFFLOAD1, 0, SST_HSW_MODULE_PCM},
154 {HSW_PCM_DAI_ID_LOOPBACK, 1, SST_HSW_MODULE_PCM_REFERENCE},
155 {HSW_PCM_DAI_ID_SYSTEM, 1, SST_HSW_MODULE_PCM_CAPTURE},
158 static u32 hsw_notify_pointer(struct sst_hsw_stream *stream, void *data);
160 static inline u32 hsw_mixer_to_ipc(unsigned int value)
162 if (value >= ARRAY_SIZE(volume_map))
163 return volume_map[0];
165 return volume_map[value];
168 static inline unsigned int hsw_ipc_to_mixer(u32 value)
172 for (i = 0; i < ARRAY_SIZE(volume_map); i++) {
173 if (volume_map[i] >= value)
180 static int hsw_stream_volume_put(struct snd_kcontrol *kcontrol,
181 struct snd_ctl_elem_value *ucontrol)
183 struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
184 struct soc_mixer_control *mc =
185 (struct soc_mixer_control *)kcontrol->private_value;
186 struct hsw_priv_data *pdata =
187 snd_soc_platform_get_drvdata(platform);
188 struct hsw_pcm_data *pcm_data;
189 struct sst_hsw *hsw = pdata->hsw;
193 dai = mod_map[mc->reg].dai_id;
194 stream = mod_map[mc->reg].stream;
195 pcm_data = &pdata->pcm[dai][stream];
197 mutex_lock(&pcm_data->mutex);
198 pm_runtime_get_sync(pdata->dev);
200 if (!pcm_data->stream) {
201 pcm_data->volume[0] =
202 hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
203 pcm_data->volume[1] =
204 hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
205 pm_runtime_mark_last_busy(pdata->dev);
206 pm_runtime_put_autosuspend(pdata->dev);
207 mutex_unlock(&pcm_data->mutex);
211 if (ucontrol->value.integer.value[0] ==
212 ucontrol->value.integer.value[1]) {
213 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
214 /* apply volume value to all channels */
215 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, SST_HSW_CHANNELS_ALL, volume);
217 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
218 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, 0, volume);
219 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
220 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, 1, volume);
223 pm_runtime_mark_last_busy(pdata->dev);
224 pm_runtime_put_autosuspend(pdata->dev);
225 mutex_unlock(&pcm_data->mutex);
229 static int hsw_stream_volume_get(struct snd_kcontrol *kcontrol,
230 struct snd_ctl_elem_value *ucontrol)
232 struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
233 struct soc_mixer_control *mc =
234 (struct soc_mixer_control *)kcontrol->private_value;
235 struct hsw_priv_data *pdata =
236 snd_soc_platform_get_drvdata(platform);
237 struct hsw_pcm_data *pcm_data;
238 struct sst_hsw *hsw = pdata->hsw;
242 dai = mod_map[mc->reg].dai_id;
243 stream = mod_map[mc->reg].stream;
244 pcm_data = &pdata->pcm[dai][stream];
246 mutex_lock(&pcm_data->mutex);
247 pm_runtime_get_sync(pdata->dev);
249 if (!pcm_data->stream) {
250 ucontrol->value.integer.value[0] =
251 hsw_ipc_to_mixer(pcm_data->volume[0]);
252 ucontrol->value.integer.value[1] =
253 hsw_ipc_to_mixer(pcm_data->volume[1]);
254 pm_runtime_mark_last_busy(pdata->dev);
255 pm_runtime_put_autosuspend(pdata->dev);
256 mutex_unlock(&pcm_data->mutex);
260 sst_hsw_stream_get_volume(hsw, pcm_data->stream, 0, 0, &volume);
261 ucontrol->value.integer.value[0] = hsw_ipc_to_mixer(volume);
262 sst_hsw_stream_get_volume(hsw, pcm_data->stream, 0, 1, &volume);
263 ucontrol->value.integer.value[1] = hsw_ipc_to_mixer(volume);
265 pm_runtime_mark_last_busy(pdata->dev);
266 pm_runtime_put_autosuspend(pdata->dev);
267 mutex_unlock(&pcm_data->mutex);
272 static int hsw_volume_put(struct snd_kcontrol *kcontrol,
273 struct snd_ctl_elem_value *ucontrol)
275 struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
276 struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
277 struct sst_hsw *hsw = pdata->hsw;
280 pm_runtime_get_sync(pdata->dev);
282 if (ucontrol->value.integer.value[0] ==
283 ucontrol->value.integer.value[1]) {
285 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
286 sst_hsw_mixer_set_volume(hsw, 0, SST_HSW_CHANNELS_ALL, volume);
289 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
290 sst_hsw_mixer_set_volume(hsw, 0, 0, volume);
292 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
293 sst_hsw_mixer_set_volume(hsw, 0, 1, volume);
296 pm_runtime_mark_last_busy(pdata->dev);
297 pm_runtime_put_autosuspend(pdata->dev);
301 static int hsw_volume_get(struct snd_kcontrol *kcontrol,
302 struct snd_ctl_elem_value *ucontrol)
304 struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
305 struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
306 struct sst_hsw *hsw = pdata->hsw;
307 unsigned int volume = 0;
309 pm_runtime_get_sync(pdata->dev);
310 sst_hsw_mixer_get_volume(hsw, 0, 0, &volume);
311 ucontrol->value.integer.value[0] = hsw_ipc_to_mixer(volume);
313 sst_hsw_mixer_get_volume(hsw, 0, 1, &volume);
314 ucontrol->value.integer.value[1] = hsw_ipc_to_mixer(volume);
316 pm_runtime_mark_last_busy(pdata->dev);
317 pm_runtime_put_autosuspend(pdata->dev);
321 /* TLV used by both global and stream volumes */
322 static const DECLARE_TLV_DB_SCALE(hsw_vol_tlv, -9000, 300, 1);
324 /* System Pin has no volume control */
325 static const struct snd_kcontrol_new hsw_volume_controls[] = {
326 /* Global DSP volume */
327 SOC_DOUBLE_EXT_TLV("Master Playback Volume", 0, 0, 8,
328 ARRAY_SIZE(volume_map) - 1, 0,
329 hsw_volume_get, hsw_volume_put, hsw_vol_tlv),
330 /* Offload 0 volume */
331 SOC_DOUBLE_EXT_TLV("Media0 Playback Volume", 1, 0, 8,
332 ARRAY_SIZE(volume_map) - 1, 0,
333 hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
334 /* Offload 1 volume */
335 SOC_DOUBLE_EXT_TLV("Media1 Playback Volume", 2, 0, 8,
336 ARRAY_SIZE(volume_map) - 1, 0,
337 hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
338 /* Mic Capture volume */
339 SOC_DOUBLE_EXT_TLV("Mic Capture Volume", 4, 0, 8,
340 ARRAY_SIZE(volume_map) - 1, 0,
341 hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
344 /* Create DMA buffer page table for DSP */
345 static int create_adsp_page_table(struct snd_pcm_substream *substream,
346 struct hsw_priv_data *pdata, struct snd_soc_pcm_runtime *rtd,
347 unsigned char *dma_area, size_t size, int pcm)
349 struct snd_dma_buffer *dmab = snd_pcm_get_dma_buf(substream);
350 int i, pages, stream = substream->stream;
352 pages = snd_sgbuf_aligned_pages(size);
354 dev_dbg(rtd->dev, "generating page table for %p size 0x%zu pages %d\n",
355 dma_area, size, pages);
357 for (i = 0; i < pages; i++) {
358 u32 idx = (((i << 2) + i)) >> 1;
359 u32 pfn = snd_sgbuf_get_addr(dmab, i * PAGE_SIZE) >> PAGE_SHIFT;
362 dev_dbg(rtd->dev, "pfn i %i idx %d pfn %x\n", i, idx, pfn);
364 pg_table = (u32 *)(pdata->dmab[pcm][stream].area + idx);
367 *pg_table |= (pfn << 4);
375 /* this may get called several times by oss emulation */
376 static int hsw_pcm_hw_params(struct snd_pcm_substream *substream,
377 struct snd_pcm_hw_params *params)
379 struct snd_soc_pcm_runtime *rtd = substream->private_data;
380 struct snd_pcm_runtime *runtime = substream->runtime;
381 struct hsw_priv_data *pdata =
382 snd_soc_platform_get_drvdata(rtd->platform);
383 struct hsw_pcm_data *pcm_data;
384 struct sst_hsw *hsw = pdata->hsw;
385 struct sst_module *module_data;
387 struct snd_dma_buffer *dmab;
388 enum sst_hsw_stream_type stream_type;
389 enum sst_hsw_stream_path_id path_id;
390 u32 rate, bits, map, pages, module_id;
394 dai = mod_map[rtd->cpu_dai->id].dai_id;
395 pcm_data = &pdata->pcm[dai][substream->stream];
397 /* check if we are being called a subsequent time */
398 if (pcm_data->allocated) {
399 ret = sst_hsw_stream_reset(hsw, pcm_data->stream);
401 dev_dbg(rtd->dev, "error: reset stream failed %d\n",
404 ret = sst_hsw_stream_free(hsw, pcm_data->stream);
406 dev_dbg(rtd->dev, "error: free stream failed %d\n",
410 pcm_data->allocated = false;
412 pcm_data->stream = sst_hsw_stream_new(hsw, rtd->cpu_dai->id,
413 hsw_notify_pointer, pcm_data);
414 if (pcm_data->stream == NULL) {
415 dev_err(rtd->dev, "error: failed to create stream\n");
420 /* stream direction */
421 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
422 path_id = SST_HSW_STREAM_PATH_SSP0_OUT;
424 path_id = SST_HSW_STREAM_PATH_SSP0_IN;
426 /* DSP stream type depends on DAI ID */
427 switch (rtd->cpu_dai->id) {
429 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
430 stream_type = SST_HSW_STREAM_TYPE_SYSTEM;
431 module_id = SST_HSW_MODULE_PCM_SYSTEM;
434 stream_type = SST_HSW_STREAM_TYPE_CAPTURE;
435 module_id = SST_HSW_MODULE_PCM_CAPTURE;
440 stream_type = SST_HSW_STREAM_TYPE_RENDER;
441 module_id = SST_HSW_MODULE_PCM;
444 /* path ID needs to be OUT for loopback */
445 stream_type = SST_HSW_STREAM_TYPE_LOOPBACK;
446 path_id = SST_HSW_STREAM_PATH_SSP0_OUT;
447 module_id = SST_HSW_MODULE_PCM_REFERENCE;
450 dev_err(rtd->dev, "error: invalid DAI ID %d\n",
455 ret = sst_hsw_stream_format(hsw, pcm_data->stream,
456 path_id, stream_type, SST_HSW_STREAM_FORMAT_PCM_FORMAT);
458 dev_err(rtd->dev, "error: failed to set format %d\n", ret);
462 rate = params_rate(params);
463 ret = sst_hsw_stream_set_rate(hsw, pcm_data->stream, rate);
465 dev_err(rtd->dev, "error: could not set rate %d\n", rate);
469 switch (params_format(params)) {
470 case SNDRV_PCM_FORMAT_S16_LE:
471 bits = SST_HSW_DEPTH_16BIT;
472 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 16);
474 case SNDRV_PCM_FORMAT_S24_LE:
475 bits = SST_HSW_DEPTH_32BIT;
476 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 24);
478 case SNDRV_PCM_FORMAT_S8:
479 bits = SST_HSW_DEPTH_8BIT;
480 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 8);
482 case SNDRV_PCM_FORMAT_S32_LE:
483 bits = SST_HSW_DEPTH_32BIT;
484 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 32);
487 dev_err(rtd->dev, "error: invalid format %d\n",
488 params_format(params));
492 ret = sst_hsw_stream_set_bits(hsw, pcm_data->stream, bits);
494 dev_err(rtd->dev, "error: could not set bits %d\n", bits);
498 channels = params_channels(params);
499 map = create_channel_map(SST_HSW_CHANNEL_CONFIG_STEREO);
500 sst_hsw_stream_set_map_config(hsw, pcm_data->stream,
501 map, SST_HSW_CHANNEL_CONFIG_STEREO);
503 ret = sst_hsw_stream_set_channels(hsw, pcm_data->stream, channels);
505 dev_err(rtd->dev, "error: could not set channels %d\n",
510 ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
512 dev_err(rtd->dev, "error: could not allocate %d bytes for PCM %d\n",
513 params_buffer_bytes(params), ret);
517 dmab = snd_pcm_get_dma_buf(substream);
519 ret = create_adsp_page_table(substream, pdata, rtd, runtime->dma_area,
520 runtime->dma_bytes, rtd->cpu_dai->id);
524 sst_hsw_stream_set_style(hsw, pcm_data->stream,
525 SST_HSW_INTERLEAVING_PER_CHANNEL);
527 if (runtime->dma_bytes % PAGE_SIZE)
528 pages = (runtime->dma_bytes / PAGE_SIZE) + 1;
530 pages = runtime->dma_bytes / PAGE_SIZE;
532 ret = sst_hsw_stream_buffer(hsw, pcm_data->stream,
533 pdata->dmab[rtd->cpu_dai->id][substream->stream].addr,
534 pages, runtime->dma_bytes, 0,
535 snd_sgbuf_get_addr(dmab, 0) >> PAGE_SHIFT);
537 dev_err(rtd->dev, "error: failed to set DMA buffer %d\n", ret);
541 dsp = sst_hsw_get_dsp(hsw);
543 module_data = sst_module_get_from_id(dsp, module_id);
544 if (module_data == NULL) {
545 dev_err(rtd->dev, "error: failed to get module config\n");
549 sst_hsw_stream_set_module_info(hsw, pcm_data->stream,
552 ret = sst_hsw_stream_commit(hsw, pcm_data->stream);
554 dev_err(rtd->dev, "error: failed to commit stream %d\n", ret);
558 if (!pcm_data->allocated) {
559 /* Set previous saved volume */
560 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0,
561 0, pcm_data->volume[0]);
562 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0,
563 1, pcm_data->volume[1]);
564 pcm_data->allocated = true;
567 ret = sst_hsw_stream_pause(hsw, pcm_data->stream, 1);
569 dev_err(rtd->dev, "error: failed to pause %d\n", ret);
574 static int hsw_pcm_hw_free(struct snd_pcm_substream *substream)
576 snd_pcm_lib_free_pages(substream);
580 static int hsw_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
582 struct snd_soc_pcm_runtime *rtd = substream->private_data;
583 struct hsw_priv_data *pdata =
584 snd_soc_platform_get_drvdata(rtd->platform);
585 struct hsw_pcm_data *pcm_data;
586 struct sst_hsw_stream *sst_stream;
587 struct sst_hsw *hsw = pdata->hsw;
588 struct snd_pcm_runtime *runtime = substream->runtime;
589 snd_pcm_uframes_t pos;
592 dai = mod_map[rtd->cpu_dai->id].dai_id;
593 pcm_data = &pdata->pcm[dai][substream->stream];
594 sst_stream = pcm_data->stream;
597 case SNDRV_PCM_TRIGGER_START:
598 case SNDRV_PCM_TRIGGER_RESUME:
599 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
600 sst_hsw_stream_set_silence_start(hsw, sst_stream, false);
601 sst_hsw_stream_resume(hsw, pcm_data->stream, 0);
603 case SNDRV_PCM_TRIGGER_STOP:
604 case SNDRV_PCM_TRIGGER_SUSPEND:
605 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
606 sst_hsw_stream_set_silence_start(hsw, sst_stream, false);
607 sst_hsw_stream_pause(hsw, pcm_data->stream, 0);
609 case SNDRV_PCM_TRIGGER_DRAIN:
610 pos = runtime->control->appl_ptr % runtime->buffer_size;
611 sst_hsw_stream_set_old_position(hsw, pcm_data->stream, pos);
612 sst_hsw_stream_set_silence_start(hsw, sst_stream, true);
621 static u32 hsw_notify_pointer(struct sst_hsw_stream *stream, void *data)
623 struct hsw_pcm_data *pcm_data = data;
624 struct snd_pcm_substream *substream = pcm_data->substream;
625 struct snd_pcm_runtime *runtime = substream->runtime;
626 struct snd_soc_pcm_runtime *rtd = substream->private_data;
627 struct hsw_priv_data *pdata =
628 snd_soc_platform_get_drvdata(rtd->platform);
629 struct sst_hsw *hsw = pdata->hsw;
631 snd_pcm_uframes_t position = bytes_to_frames(runtime,
632 sst_hsw_get_dsp_position(hsw, pcm_data->stream));
633 unsigned char *dma_area = runtime->dma_area;
634 snd_pcm_uframes_t dma_frames =
635 bytes_to_frames(runtime, runtime->dma_bytes);
636 snd_pcm_uframes_t old_position;
639 pos = frames_to_bytes(runtime,
640 (runtime->control->appl_ptr % runtime->buffer_size));
642 dev_vdbg(rtd->dev, "PCM: App pointer %d bytes\n", pos);
644 /* SST fw don't know where to stop dma
645 * So, SST driver need to clean the data which has been consumed
647 if (dma_area == NULL || dma_frames <= 0
648 || (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
649 || !sst_hsw_stream_get_silence_start(hsw, stream)) {
650 snd_pcm_period_elapsed(substream);
654 old_position = sst_hsw_stream_get_old_position(hsw, stream);
655 if (position > old_position) {
656 if (position < dma_frames) {
657 samples = SST_SAMPLES(runtime, position - old_position);
658 snd_pcm_format_set_silence(runtime->format,
659 SST_OLD_POSITION(dma_area,
660 runtime, old_position),
663 dev_err(rtd->dev, "PCM: position is wrong\n");
665 if (old_position < dma_frames) {
666 samples = SST_SAMPLES(runtime,
667 dma_frames - old_position);
668 snd_pcm_format_set_silence(runtime->format,
669 SST_OLD_POSITION(dma_area,
670 runtime, old_position),
673 dev_err(rtd->dev, "PCM: dma_bytes is wrong\n");
674 if (position < dma_frames) {
675 samples = SST_SAMPLES(runtime, position);
676 snd_pcm_format_set_silence(runtime->format,
679 dev_err(rtd->dev, "PCM: position is wrong\n");
681 sst_hsw_stream_set_old_position(hsw, stream, position);
683 /* let alsa know we have play a period */
684 snd_pcm_period_elapsed(substream);
688 static snd_pcm_uframes_t hsw_pcm_pointer(struct snd_pcm_substream *substream)
690 struct snd_soc_pcm_runtime *rtd = substream->private_data;
691 struct snd_pcm_runtime *runtime = substream->runtime;
692 struct hsw_priv_data *pdata =
693 snd_soc_platform_get_drvdata(rtd->platform);
694 struct hsw_pcm_data *pcm_data;
695 struct sst_hsw *hsw = pdata->hsw;
696 snd_pcm_uframes_t offset;
701 dai = mod_map[rtd->cpu_dai->id].dai_id;
702 pcm_data = &pdata->pcm[dai][substream->stream];
703 position = sst_hsw_get_dsp_position(hsw, pcm_data->stream);
705 offset = bytes_to_frames(runtime, position);
706 ppos = sst_hsw_get_dsp_presentation_position(hsw, pcm_data->stream);
708 dev_vdbg(rtd->dev, "PCM: DMA pointer %du bytes, pos %llu\n",
713 static int hsw_pcm_open(struct snd_pcm_substream *substream)
715 struct snd_soc_pcm_runtime *rtd = substream->private_data;
716 struct hsw_priv_data *pdata =
717 snd_soc_platform_get_drvdata(rtd->platform);
718 struct hsw_pcm_data *pcm_data;
719 struct sst_hsw *hsw = pdata->hsw;
722 dai = mod_map[rtd->cpu_dai->id].dai_id;
723 pcm_data = &pdata->pcm[dai][substream->stream];
725 mutex_lock(&pcm_data->mutex);
726 pm_runtime_get_sync(pdata->dev);
728 snd_soc_pcm_set_drvdata(rtd, pcm_data);
729 pcm_data->substream = substream;
731 snd_soc_set_runtime_hwparams(substream, &hsw_pcm_hardware);
733 pcm_data->stream = sst_hsw_stream_new(hsw, rtd->cpu_dai->id,
734 hsw_notify_pointer, pcm_data);
735 if (pcm_data->stream == NULL) {
736 dev_err(rtd->dev, "error: failed to create stream\n");
737 pm_runtime_mark_last_busy(pdata->dev);
738 pm_runtime_put_autosuspend(pdata->dev);
739 mutex_unlock(&pcm_data->mutex);
743 mutex_unlock(&pcm_data->mutex);
747 static int hsw_pcm_close(struct snd_pcm_substream *substream)
749 struct snd_soc_pcm_runtime *rtd = substream->private_data;
750 struct hsw_priv_data *pdata =
751 snd_soc_platform_get_drvdata(rtd->platform);
752 struct hsw_pcm_data *pcm_data;
753 struct sst_hsw *hsw = pdata->hsw;
756 dai = mod_map[rtd->cpu_dai->id].dai_id;
757 pcm_data = &pdata->pcm[dai][substream->stream];
759 mutex_lock(&pcm_data->mutex);
760 ret = sst_hsw_stream_reset(hsw, pcm_data->stream);
762 dev_dbg(rtd->dev, "error: reset stream failed %d\n", ret);
766 ret = sst_hsw_stream_free(hsw, pcm_data->stream);
768 dev_dbg(rtd->dev, "error: free stream failed %d\n", ret);
771 pcm_data->allocated = 0;
772 pcm_data->stream = NULL;
775 pm_runtime_mark_last_busy(pdata->dev);
776 pm_runtime_put_autosuspend(pdata->dev);
777 mutex_unlock(&pcm_data->mutex);
781 static struct snd_pcm_ops hsw_pcm_ops = {
782 .open = hsw_pcm_open,
783 .close = hsw_pcm_close,
784 .ioctl = snd_pcm_lib_ioctl,
785 .hw_params = hsw_pcm_hw_params,
786 .hw_free = hsw_pcm_hw_free,
787 .trigger = hsw_pcm_trigger,
788 .pointer = hsw_pcm_pointer,
789 .page = snd_pcm_sgbuf_ops_page,
792 static int hsw_pcm_create_modules(struct hsw_priv_data *pdata)
794 struct sst_hsw *hsw = pdata->hsw;
795 struct hsw_pcm_data *pcm_data;
798 for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
799 pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
801 /* create new runtime module, use same offset if recreated */
802 pcm_data->runtime = sst_hsw_runtime_module_create(hsw,
803 mod_map[i].mod_id, pcm_data->persistent_offset);
804 if (pcm_data->runtime == NULL)
806 pcm_data->persistent_offset =
807 pcm_data->runtime->persistent_offset;
813 for (--i; i >= 0; i--) {
814 pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
815 sst_hsw_runtime_module_free(pcm_data->runtime);
821 static void hsw_pcm_free_modules(struct hsw_priv_data *pdata)
823 struct hsw_pcm_data *pcm_data;
826 for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
827 pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
829 sst_hsw_runtime_module_free(pcm_data->runtime);
833 static int hsw_pcm_new(struct snd_soc_pcm_runtime *rtd)
835 struct snd_pcm *pcm = rtd->pcm;
836 struct snd_soc_platform *platform = rtd->platform;
837 struct sst_pdata *pdata = dev_get_platdata(platform->dev);
838 struct hsw_priv_data *priv_data = dev_get_drvdata(platform->dev);
839 struct device *dev = pdata->dma_dev;
842 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream ||
843 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
844 ret = snd_pcm_lib_preallocate_pages_for_all(pcm,
845 SNDRV_DMA_TYPE_DEV_SG,
847 hsw_pcm_hardware.buffer_bytes_max,
848 hsw_pcm_hardware.buffer_bytes_max);
850 dev_err(rtd->dev, "dma buffer allocation failed %d\n",
855 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
856 priv_data->pcm[rtd->cpu_dai->id][SNDRV_PCM_STREAM_PLAYBACK].hsw_pcm = pcm;
857 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
858 priv_data->pcm[rtd->cpu_dai->id][SNDRV_PCM_STREAM_CAPTURE].hsw_pcm = pcm;
863 #define HSW_FORMATS \
864 (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
866 static struct snd_soc_dai_driver hsw_dais[] = {
868 .name = "System Pin",
869 .id = HSW_PCM_DAI_ID_SYSTEM,
871 .stream_name = "System Playback",
874 .rates = SNDRV_PCM_RATE_48000,
875 .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
878 .stream_name = "Analog Capture",
881 .rates = SNDRV_PCM_RATE_48000,
882 .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
887 .name = "Offload0 Pin",
888 .id = HSW_PCM_DAI_ID_OFFLOAD0,
890 .stream_name = "Offload0 Playback",
893 .rates = SNDRV_PCM_RATE_8000_192000,
894 .formats = HSW_FORMATS,
899 .name = "Offload1 Pin",
900 .id = HSW_PCM_DAI_ID_OFFLOAD1,
902 .stream_name = "Offload1 Playback",
905 .rates = SNDRV_PCM_RATE_8000_192000,
906 .formats = HSW_FORMATS,
910 .name = "Loopback Pin",
911 .id = HSW_PCM_DAI_ID_LOOPBACK,
913 .stream_name = "Loopback Capture",
916 .rates = SNDRV_PCM_RATE_48000,
917 .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
922 static const struct snd_soc_dapm_widget widgets[] = {
925 SND_SOC_DAPM_AIF_IN("SSP0 CODEC IN", NULL, 0, SND_SOC_NOPM, 0, 0),
926 SND_SOC_DAPM_AIF_OUT("SSP0 CODEC OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
927 SND_SOC_DAPM_AIF_IN("SSP1 BT IN", NULL, 0, SND_SOC_NOPM, 0, 0),
928 SND_SOC_DAPM_AIF_OUT("SSP1 BT OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
930 /* Global Playback Mixer */
931 SND_SOC_DAPM_MIXER("Playback VMixer", SND_SOC_NOPM, 0, 0, NULL, 0),
934 static const struct snd_soc_dapm_route graph[] = {
937 {"Playback VMixer", NULL, "System Playback"},
938 {"Playback VMixer", NULL, "Offload0 Playback"},
939 {"Playback VMixer", NULL, "Offload1 Playback"},
941 {"SSP0 CODEC OUT", NULL, "Playback VMixer"},
943 {"Analog Capture", NULL, "SSP0 CODEC IN"},
946 static int hsw_pcm_probe(struct snd_soc_platform *platform)
948 struct hsw_priv_data *priv_data = snd_soc_platform_get_drvdata(platform);
949 struct sst_pdata *pdata = dev_get_platdata(platform->dev);
950 struct device *dma_dev, *dev;
957 dma_dev = pdata->dma_dev;
959 priv_data->hsw = pdata->dsp;
960 priv_data->dev = platform->dev;
961 priv_data->pm_state = HSW_PM_STATE_D0;
962 priv_data->soc_card = platform->component.card;
964 /* allocate DSP buffer page tables */
965 for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) {
968 if (hsw_dais[i].playback.channels_min) {
969 mutex_init(&priv_data->pcm[i][SNDRV_PCM_STREAM_PLAYBACK].mutex);
970 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dma_dev,
971 PAGE_SIZE, &priv_data->dmab[i][0]);
977 if (hsw_dais[i].capture.channels_min) {
978 mutex_init(&priv_data->pcm[i][SNDRV_PCM_STREAM_CAPTURE].mutex);
979 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dma_dev,
980 PAGE_SIZE, &priv_data->dmab[i][1]);
986 /* allocate runtime modules */
987 hsw_pcm_create_modules(priv_data);
989 /* enable runtime PM with auto suspend */
990 pm_runtime_set_autosuspend_delay(platform->dev,
991 SST_RUNTIME_SUSPEND_DELAY);
992 pm_runtime_use_autosuspend(platform->dev);
993 pm_runtime_enable(platform->dev);
994 pm_runtime_idle(platform->dev);
1000 if (hsw_dais[i].playback.channels_min)
1001 snd_dma_free_pages(&priv_data->dmab[i][0]);
1002 if (hsw_dais[i].capture.channels_min)
1003 snd_dma_free_pages(&priv_data->dmab[i][1]);
1008 static int hsw_pcm_remove(struct snd_soc_platform *platform)
1010 struct hsw_priv_data *priv_data =
1011 snd_soc_platform_get_drvdata(platform);
1014 pm_runtime_disable(platform->dev);
1015 hsw_pcm_free_modules(priv_data);
1017 for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) {
1018 if (hsw_dais[i].playback.channels_min)
1019 snd_dma_free_pages(&priv_data->dmab[i][0]);
1020 if (hsw_dais[i].capture.channels_min)
1021 snd_dma_free_pages(&priv_data->dmab[i][1]);
1027 static struct snd_soc_platform_driver hsw_soc_platform = {
1028 .probe = hsw_pcm_probe,
1029 .remove = hsw_pcm_remove,
1030 .ops = &hsw_pcm_ops,
1031 .pcm_new = hsw_pcm_new,
1034 static const struct snd_soc_component_driver hsw_dai_component = {
1035 .name = "haswell-dai",
1036 .controls = hsw_volume_controls,
1037 .num_controls = ARRAY_SIZE(hsw_volume_controls),
1038 .dapm_widgets = widgets,
1039 .num_dapm_widgets = ARRAY_SIZE(widgets),
1040 .dapm_routes = graph,
1041 .num_dapm_routes = ARRAY_SIZE(graph),
1044 static int hsw_pcm_dev_probe(struct platform_device *pdev)
1046 struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
1047 struct hsw_priv_data *priv_data;
1053 priv_data = devm_kzalloc(&pdev->dev, sizeof(*priv_data), GFP_KERNEL);
1057 ret = sst_hsw_dsp_init(&pdev->dev, sst_pdata);
1061 priv_data->hsw = sst_pdata->dsp;
1062 platform_set_drvdata(pdev, priv_data);
1064 ret = snd_soc_register_platform(&pdev->dev, &hsw_soc_platform);
1068 ret = snd_soc_register_component(&pdev->dev, &hsw_dai_component,
1069 hsw_dais, ARRAY_SIZE(hsw_dais));
1076 snd_soc_unregister_platform(&pdev->dev);
1078 sst_hsw_dsp_free(&pdev->dev, sst_pdata);
1082 static int hsw_pcm_dev_remove(struct platform_device *pdev)
1084 struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
1086 snd_soc_unregister_platform(&pdev->dev);
1087 snd_soc_unregister_component(&pdev->dev);
1088 sst_hsw_dsp_free(&pdev->dev, sst_pdata);
1095 static int hsw_pcm_runtime_idle(struct device *dev)
1100 static int hsw_pcm_runtime_suspend(struct device *dev)
1102 struct hsw_priv_data *pdata = dev_get_drvdata(dev);
1103 struct sst_hsw *hsw = pdata->hsw;
1105 if (pdata->pm_state >= HSW_PM_STATE_RTD3)
1108 sst_hsw_dsp_runtime_suspend(hsw);
1109 sst_hsw_dsp_runtime_sleep(hsw);
1110 pdata->pm_state = HSW_PM_STATE_RTD3;
1115 static int hsw_pcm_runtime_resume(struct device *dev)
1117 struct hsw_priv_data *pdata = dev_get_drvdata(dev);
1118 struct sst_hsw *hsw = pdata->hsw;
1121 if (pdata->pm_state != HSW_PM_STATE_RTD3)
1124 ret = sst_hsw_dsp_load(hsw);
1126 dev_err(dev, "failed to reload %d\n", ret);
1130 ret = hsw_pcm_create_modules(pdata);
1132 dev_err(dev, "failed to create modules %d\n", ret);
1136 ret = sst_hsw_dsp_runtime_resume(hsw);
1139 else if (ret == 1) /* no action required */
1142 pdata->pm_state = HSW_PM_STATE_D0;
1147 #define hsw_pcm_runtime_idle NULL
1148 #define hsw_pcm_runtime_suspend NULL
1149 #define hsw_pcm_runtime_resume NULL
1154 static void hsw_pcm_complete(struct device *dev)
1156 struct hsw_priv_data *pdata = dev_get_drvdata(dev);
1157 struct sst_hsw *hsw = pdata->hsw;
1158 struct hsw_pcm_data *pcm_data;
1161 if (pdata->pm_state != HSW_PM_STATE_D3)
1164 err = sst_hsw_dsp_load(hsw);
1166 dev_err(dev, "failed to reload %d\n", err);
1170 err = hsw_pcm_create_modules(pdata);
1172 dev_err(dev, "failed to create modules %d\n", err);
1176 for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
1177 pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
1179 if (!pcm_data->substream)
1182 err = sst_module_runtime_restore(pcm_data->runtime,
1183 &pcm_data->context);
1185 dev_err(dev, "failed to restore context for PCM %d\n", i);
1188 snd_soc_resume(pdata->soc_card->dev);
1190 err = sst_hsw_dsp_runtime_resume(hsw);
1193 else if (err == 1) /* no action required */
1196 pdata->pm_state = HSW_PM_STATE_D0;
1200 static int hsw_pcm_prepare(struct device *dev)
1202 struct hsw_priv_data *pdata = dev_get_drvdata(dev);
1203 struct sst_hsw *hsw = pdata->hsw;
1204 struct hsw_pcm_data *pcm_data;
1207 if (pdata->pm_state == HSW_PM_STATE_D3)
1209 else if (pdata->pm_state == HSW_PM_STATE_D0) {
1210 /* suspend all active streams */
1211 for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
1212 pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
1214 if (!pcm_data->substream)
1216 dev_dbg(dev, "suspending pcm %d\n", i);
1217 snd_pcm_suspend_all(pcm_data->hsw_pcm);
1219 /* We need to wait until the DSP FW stops the streams */
1223 /* preserve persistent memory */
1224 for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
1225 pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
1227 if (!pcm_data->substream)
1230 dev_dbg(dev, "saving context pcm %d\n", i);
1231 err = sst_module_runtime_save(pcm_data->runtime,
1232 &pcm_data->context);
1234 dev_err(dev, "failed to save context for PCM %d\n", i);
1236 /* enter D3 state and stall */
1237 sst_hsw_dsp_runtime_suspend(hsw);
1238 /* put the DSP to sleep */
1239 sst_hsw_dsp_runtime_sleep(hsw);
1242 snd_soc_suspend(pdata->soc_card->dev);
1243 snd_soc_poweroff(pdata->soc_card->dev);
1245 pdata->pm_state = HSW_PM_STATE_D3;
1251 #define hsw_pcm_prepare NULL
1252 #define hsw_pcm_complete NULL
1255 static const struct dev_pm_ops hsw_pcm_pm = {
1256 .runtime_idle = hsw_pcm_runtime_idle,
1257 .runtime_suspend = hsw_pcm_runtime_suspend,
1258 .runtime_resume = hsw_pcm_runtime_resume,
1259 .prepare = hsw_pcm_prepare,
1260 .complete = hsw_pcm_complete,
1263 static struct platform_driver hsw_pcm_driver = {
1265 .name = "haswell-pcm-audio",
1269 .probe = hsw_pcm_dev_probe,
1270 .remove = hsw_pcm_dev_remove,
1272 module_platform_driver(hsw_pcm_driver);
1274 MODULE_AUTHOR("Liam Girdwood, Xingchao Wang");
1275 MODULE_DESCRIPTION("Haswell/Lynxpoint + Broadwell/Wildcatpoint PCM");
1276 MODULE_LICENSE("GPL v2");
1277 MODULE_ALIAS("platform:haswell-pcm-audio");