1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
3 // This file is provided under a dual BSD/GPLv2 license. When using or
4 // redistributing this file, you may do so under either license.
6 // Copyright(c) 2018 Intel Corporation. All rights reserved.
8 // Authors: Keyon Jie <yang.jie@linux.intel.com>
11 #include <sound/pcm_params.h>
12 #include <sound/hdaudio_ext.h>
13 #include "../sof-priv.h"
14 #include "../sof-audio.h"
17 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
19 struct hda_pipe_params {
26 snd_pcm_format_t format;
29 unsigned int host_bps;
30 unsigned int link_bps;
34 * This function checks if the host dma channel corresponding
35 * to the link DMA stream_tag argument is assigned to one
36 * of the FEs connected to the BE DAI.
38 static bool hda_check_fes(struct snd_soc_pcm_runtime *rtd,
39 int dir, int stream_tag)
41 struct snd_pcm_substream *fe_substream;
42 struct hdac_stream *fe_hstream;
43 struct snd_soc_dpcm *dpcm;
45 for_each_dpcm_fe(rtd, dir, dpcm) {
46 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, dir);
47 fe_hstream = fe_substream->runtime->private_data;
48 if (fe_hstream->stream_tag == stream_tag)
55 static struct hdac_ext_stream *
56 hda_link_stream_assign(struct hdac_bus *bus,
57 struct snd_pcm_substream *substream)
59 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
60 struct sof_intel_hda_stream *hda_stream;
61 struct hdac_ext_stream *res = NULL;
62 struct hdac_stream *stream = NULL;
64 int stream_dir = substream->stream;
67 dev_err(bus->dev, "stream type not supported\n");
71 spin_lock_irq(&bus->reg_lock);
72 list_for_each_entry(stream, &bus->stream_list, list) {
73 struct hdac_ext_stream *hstream =
74 stream_to_hdac_ext_stream(stream);
75 if (stream->direction != substream->stream)
78 hda_stream = hstream_to_sof_hda_stream(hstream);
80 /* check if link is available */
81 if (!hstream->link_locked) {
84 * check if the stream tag matches the stream
85 * tag of one of the connected FEs
87 if (hda_check_fes(rtd, stream_dir,
88 stream->stream_tag)) {
96 * This must be a hostless stream.
97 * So reserve the host DMA channel.
99 hda_stream->host_reserved = 1;
107 * Decouple host and link DMA. The decoupled flag
108 * is updated in snd_hdac_ext_stream_decouple().
111 snd_hdac_ext_stream_decouple_locked(bus, res, true);
113 res->link_locked = 1;
114 res->link_substream = substream;
116 spin_unlock_irq(&bus->reg_lock);
121 static int hda_link_dma_params(struct hdac_ext_stream *stream,
122 struct hda_pipe_params *params)
124 struct hdac_stream *hstream = &stream->hstream;
125 unsigned char stream_tag = hstream->stream_tag;
126 struct hdac_bus *bus = hstream->bus;
127 struct hdac_ext_link *link;
128 unsigned int format_val;
130 snd_hdac_ext_stream_decouple(bus, stream, true);
131 snd_hdac_ext_link_stream_reset(stream);
133 format_val = snd_hdac_calc_stream_format(params->s_freq, params->ch,
135 params->link_bps, 0);
137 dev_dbg(bus->dev, "format_val=%d, rate=%d, ch=%d, format=%d\n",
138 format_val, params->s_freq, params->ch, params->format);
140 snd_hdac_ext_link_stream_setup(stream, format_val);
142 if (stream->hstream.direction == SNDRV_PCM_STREAM_PLAYBACK) {
143 list_for_each_entry(link, &bus->hlink_list, list) {
144 if (link->index == params->link_index)
145 snd_hdac_ext_link_set_stream_id(link,
150 stream->link_prepared = 1;
155 /* Send DAI_CONFIG IPC to the DAI that matches the dai_name and direction */
156 static int hda_link_config_ipc(struct sof_intel_hda_stream *hda_stream,
157 const char *dai_name, int channel, int dir)
159 struct sof_ipc_dai_config *config;
160 struct snd_sof_dai *sof_dai;
161 struct sof_ipc_reply reply;
164 list_for_each_entry(sof_dai, &hda_stream->sdev->dai_list, list) {
165 if (!sof_dai->cpu_dai_name)
168 if (!strcmp(dai_name, sof_dai->cpu_dai_name) &&
169 dir == sof_dai->comp_dai.direction) {
170 config = sof_dai->dai_config;
173 dev_err(hda_stream->sdev->dev,
174 "error: no config for DAI %s\n",
179 /* update config with stream tag */
180 config->hda.link_dma_ch = channel;
183 ret = sof_ipc_tx_message(hda_stream->sdev->ipc,
187 &reply, sizeof(reply));
190 dev_err(hda_stream->sdev->dev,
191 "error: failed to set dai config for %s\n",
200 static int hda_link_hw_params(struct snd_pcm_substream *substream,
201 struct snd_pcm_hw_params *params,
202 struct snd_soc_dai *dai)
204 struct hdac_stream *hstream = substream->runtime->private_data;
205 struct hdac_bus *bus = hstream->bus;
206 struct hdac_ext_stream *link_dev;
207 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
208 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
209 struct sof_intel_hda_stream *hda_stream;
210 struct hda_pipe_params p_params = {0};
211 struct hdac_ext_link *link;
215 /* get stored dma data if resuming from system suspend */
216 link_dev = snd_soc_dai_get_dma_data(dai, substream);
218 link_dev = hda_link_stream_assign(bus, substream);
222 snd_soc_dai_set_dma_data(dai, substream, (void *)link_dev);
225 stream_tag = hdac_stream(link_dev)->stream_tag;
227 hda_stream = hstream_to_sof_hda_stream(link_dev);
229 /* update the DSP with the new tag */
230 ret = hda_link_config_ipc(hda_stream, dai->name, stream_tag - 1,
235 link = snd_hdac_ext_bus_get_link(bus, codec_dai->component->name);
239 /* set the hdac_stream in the codec dai */
240 snd_soc_dai_set_stream(codec_dai, hdac_stream(link_dev), substream->stream);
242 p_params.s_fmt = snd_pcm_format_width(params_format(params));
243 p_params.ch = params_channels(params);
244 p_params.s_freq = params_rate(params);
245 p_params.stream = substream->stream;
246 p_params.link_dma_id = stream_tag - 1;
247 p_params.link_index = link->index;
248 p_params.format = params_format(params);
250 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
251 p_params.link_bps = codec_dai->driver->playback.sig_bits;
253 p_params.link_bps = codec_dai->driver->capture.sig_bits;
255 return hda_link_dma_params(link_dev, &p_params);
258 static int hda_link_pcm_prepare(struct snd_pcm_substream *substream,
259 struct snd_soc_dai *dai)
261 struct hdac_ext_stream *link_dev =
262 snd_soc_dai_get_dma_data(dai, substream);
263 struct snd_sof_dev *sdev =
264 snd_soc_component_get_drvdata(dai->component);
265 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
266 int stream = substream->stream;
268 if (link_dev->link_prepared)
271 dev_dbg(sdev->dev, "hda: prepare stream dir %d\n", substream->stream);
273 return hda_link_hw_params(substream, &rtd->dpcm[stream].hw_params,
277 static int hda_link_pcm_trigger(struct snd_pcm_substream *substream,
278 int cmd, struct snd_soc_dai *dai)
280 struct hdac_ext_stream *link_dev =
281 snd_soc_dai_get_dma_data(dai, substream);
282 struct sof_intel_hda_stream *hda_stream;
283 struct snd_soc_pcm_runtime *rtd;
284 struct hdac_ext_link *link;
285 struct hdac_stream *hstream;
286 struct hdac_bus *bus;
290 hstream = substream->runtime->private_data;
292 rtd = asoc_substream_to_rtd(substream);
294 link = snd_hdac_ext_bus_get_link(bus, asoc_rtd_to_codec(rtd, 0)->component->name);
298 hda_stream = hstream_to_sof_hda_stream(link_dev);
300 dev_dbg(dai->dev, "In %s cmd=%d\n", __func__, cmd);
302 case SNDRV_PCM_TRIGGER_RESUME:
303 /* set up hw_params */
304 ret = hda_link_pcm_prepare(substream, dai);
307 "error: setting up hw_params during resume\n");
312 case SNDRV_PCM_TRIGGER_START:
313 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
314 snd_hdac_ext_link_stream_start(link_dev);
316 case SNDRV_PCM_TRIGGER_SUSPEND:
317 case SNDRV_PCM_TRIGGER_STOP:
319 * clear link DMA channel. It will be assigned when
320 * hw_params is set up again after resume.
322 ret = hda_link_config_ipc(hda_stream, dai->name,
323 DMA_CHAN_INVALID, substream->stream);
327 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
328 stream_tag = hdac_stream(link_dev)->stream_tag;
329 snd_hdac_ext_link_clear_stream_id(link, stream_tag);
332 link_dev->link_prepared = 0;
335 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
336 snd_hdac_ext_link_stream_clear(link_dev);
344 static int hda_link_hw_free(struct snd_pcm_substream *substream,
345 struct snd_soc_dai *dai)
347 unsigned int stream_tag;
348 struct sof_intel_hda_stream *hda_stream;
349 struct hdac_bus *bus;
350 struct hdac_ext_link *link;
351 struct hdac_stream *hstream;
352 struct snd_soc_pcm_runtime *rtd;
353 struct hdac_ext_stream *link_dev;
356 hstream = substream->runtime->private_data;
358 rtd = asoc_substream_to_rtd(substream);
359 link_dev = snd_soc_dai_get_dma_data(dai, substream);
363 "%s: link_dev is not assigned\n", __func__);
367 hda_stream = hstream_to_sof_hda_stream(link_dev);
369 /* free the link DMA channel in the FW */
370 ret = hda_link_config_ipc(hda_stream, dai->name, DMA_CHAN_INVALID,
375 link = snd_hdac_ext_bus_get_link(bus, asoc_rtd_to_codec(rtd, 0)->component->name);
379 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
380 stream_tag = hdac_stream(link_dev)->stream_tag;
381 snd_hdac_ext_link_clear_stream_id(link, stream_tag);
384 snd_soc_dai_set_dma_data(dai, substream, NULL);
385 snd_hdac_ext_stream_release(link_dev, HDAC_EXT_STREAM_TYPE_LINK);
386 link_dev->link_prepared = 0;
388 /* free the host DMA channel reserved by hostless streams */
389 hda_stream->host_reserved = 0;
394 static const struct snd_soc_dai_ops hda_link_dai_ops = {
395 .hw_params = hda_link_hw_params,
396 .hw_free = hda_link_hw_free,
397 .trigger = hda_link_pcm_trigger,
398 .prepare = hda_link_pcm_prepare,
401 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_PROBES)
402 #include "../compress.h"
404 static struct snd_soc_cdai_ops sof_probe_compr_ops = {
405 .startup = sof_probe_compr_open,
406 .shutdown = sof_probe_compr_free,
407 .set_params = sof_probe_compr_set_params,
408 .trigger = sof_probe_compr_trigger,
409 .pointer = sof_probe_compr_pointer,
415 static int ssp_dai_hw_params(struct snd_pcm_substream *substream,
416 struct snd_pcm_hw_params *params,
417 struct snd_soc_dai *dai)
419 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
420 struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
421 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
422 struct sof_ipc_fw_version *v = &sdev->fw_ready.version;
423 struct sof_ipc_dai_config *config;
424 struct snd_sof_dai *sof_dai;
425 struct sof_ipc_reply reply;
428 /* DAI_CONFIG IPC during hw_params is not supported in older firmware */
429 if (v->abi_version < SOF_ABI_VER(3, 18, 0))
432 list_for_each_entry(sof_dai, &sdev->dai_list, list) {
433 if (!sof_dai->cpu_dai_name || !sof_dai->dai_config)
436 if (!strcmp(dai->name, sof_dai->cpu_dai_name) &&
437 substream->stream == sof_dai->comp_dai.direction) {
438 config = &sof_dai->dai_config[sof_dai->current_config];
441 ret = sof_ipc_tx_message(sdev->ipc, config->hdr.cmd, config,
442 config->hdr.size, &reply, sizeof(reply));
445 dev_err(sdev->dev, "error: failed to set DAI config for %s\n",
454 static const struct snd_soc_dai_ops ssp_dai_ops = {
455 .hw_params = ssp_dai_hw_params,
459 * common dai driver for skl+ platforms.
460 * some products who use this DAI array only physically have a subset of
461 * the DAIs, but no harm is done here by adding the whole set.
463 struct snd_soc_dai_driver skl_dai[] = {
537 .name = "DMIC01 Pin",
544 .name = "DMIC16k Pin",
550 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
552 .name = "iDisp1 Pin",
553 .ops = &hda_link_dai_ops,
560 .name = "iDisp2 Pin",
561 .ops = &hda_link_dai_ops,
568 .name = "iDisp3 Pin",
569 .ops = &hda_link_dai_ops,
576 .name = "iDisp4 Pin",
577 .ops = &hda_link_dai_ops,
584 .name = "Analog CPU DAI",
585 .ops = &hda_link_dai_ops,
596 .name = "Digital CPU DAI",
597 .ops = &hda_link_dai_ops,
608 .name = "Alt Analog CPU DAI",
609 .ops = &hda_link_dai_ops,
619 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_PROBES)
621 .name = "Probe Extraction CPU DAI",
622 .compress_new = snd_soc_new_compress,
623 .cops = &sof_probe_compr_ops,
625 .stream_name = "Probe Extraction",
628 .rates = SNDRV_PCM_RATE_48000,