Unverified Commit dc8384d8 authored by Mark Brown's avatar Mark Brown
Browse files

ASoC: SOF: Support for echoref (virtual DAI)

Merge series from Peter Ujfalusi <peter.ujfalusi@linux.intel.com>:

The series adds support for echo reference functionality by allowing
the capturing of playback audio right before it leaves the DSP.
For this to work correctly we need a virtual DAI that is also connected
to the echo reference capture device and in absence of playback a
signal generator generates silence to allow the capture to run.
When the real playback starts, application will start to receive the
playback audio to be usable for echo reference.
parents 6f0fce21 6c52fda4
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -56,6 +56,9 @@
#define SOF_TKN_SCHED_LP_MODE			207
#define SOF_TKN_SCHED_MEM_USAGE			208
#define SOF_TKN_SCHED_USE_CHAIN_DMA		209
#define SOF_TKN_SCHED_KCPS			210
#define SOF_TKN_SCHED_DIRECTION			211
#define SOF_TKN_SCHED_DIRECTION_VALID		212

/* volume */
#define SOF_TKN_VOLUME_RAMP_STEP_TYPE		250
+41 −2
Original line number Diff line number Diff line
@@ -1186,6 +1186,34 @@ static int create_bt_dailinks(struct snd_soc_card *card,
	return 0;
}

static int create_echoref_dailink(struct snd_soc_card *card,
				  struct snd_soc_dai_link **dai_links, int *be_id)
{
	struct device *dev = card->dev;
	int ret;
	char *name = devm_kasprintf(dev, GFP_KERNEL, "Loopback_Virtual");

	if (!name)
		return -ENOMEM;

	/*
	 * use dummy DAI names as this won't be connected to an actual DAI but just to establish a
	 * fe <-> be connection for loopback capture for echo reference
	 */
	ret = asoc_sdw_init_simple_dai_link(dev, *dai_links, be_id, name,
					    0, 1, "Loopback Virtual Pin", "dummy",
					    snd_soc_dummy_dlc.name, snd_soc_dummy_dlc.dai_name,
					    1, NULL, NULL);
	if (ret)
		return ret;

	(*dai_links)++;

	dev_dbg(dev, "Added echo reference DAI link\n");

	return 0;
}

static int sof_card_dai_links_create(struct snd_soc_card *card)
{
	struct device *dev = card->dev;
@@ -1294,8 +1322,12 @@ static int sof_card_dai_links_create(struct snd_soc_card *card)
		goto err_end;
	}

	/* allocate BE dailinks */
	num_links = sdw_be_num + ssp_num + dmic_num + hdmi_num + bt_num;
	/*
	 * allocate BE dailinks, add an extra DAI link for echo reference capture.
	 * This should be the last DAI link and it is expected both for monolithic
	 * and functional SOF topologies to support echo reference.
	 */
	num_links = sdw_be_num + ssp_num + dmic_num + hdmi_num + bt_num + 1;
	dai_links = devm_kcalloc(dev, num_links, sizeof(*dai_links), GFP_KERNEL);
	if (!dai_links) {
		ret = -ENOMEM;
@@ -1344,6 +1376,13 @@ static int sof_card_dai_links_create(struct snd_soc_card *card)
			goto err_end;
	}

	/* dummy echo ref link. keep this as the last DAI link. The DAI link ID does not matter */
	ret = create_echoref_dailink(card, &dai_links, &be_id);
	if (ret) {
		dev_err(dev, "failed to create echo ref dai link: %d\n", ret);
		goto err_end;
	}

	WARN_ON(codec_conf != card->codec_conf + card->num_configs);
	WARN_ON(dai_links != card->dai_link + card->num_links);

+20 −2
Original line number Diff line number Diff line
@@ -70,12 +70,22 @@ static const struct hda_dai_widget_dma_ops *
hda_dai_get_ops(struct snd_pcm_substream *substream, struct snd_soc_dai *cpu_dai)
{
	struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(cpu_dai, substream->stream);
	struct snd_sof_widget *swidget = w->dobj.private;
	struct snd_sof_widget *swidget;
	struct snd_sof_dev *sdev;
	struct snd_sof_dai *sdai;

	sdev = widget_to_sdev(w);
	/*
	 * this is unlikely if the topology and the machine driver DAI links match.
	 * But if there's a missing DAI link in topology, this will prevent a NULL pointer
	 * dereference later on.
	 */
	if (!w) {
		dev_err(cpu_dai->dev, "%s: widget is NULL\n", __func__);
		return NULL;
	}

	sdev = widget_to_sdev(w);
	swidget = w->dobj.private;
	if (!swidget) {
		dev_err(sdev->dev, "%s: swidget is NULL\n", __func__);
		return NULL;
@@ -856,6 +866,14 @@ struct snd_soc_dai_driver skl_dai[] = {
		.channels_max = 4,
	},
},
{
	/* Virtual CPU DAI for Echo reference */
	.name = "Loopback Virtual Pin",
	.capture = {
		.channels_min = 1,
		.channels_max = 2,
	},
},
#if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
{
	.name = "iDisp1 Pin",
+2 −2
Original line number Diff line number Diff line
@@ -418,10 +418,10 @@
	(HDA_DSP_BDL_SIZE / sizeof(struct sof_intel_dsp_bdl))

/* Number of DAIs */
#define SOF_SKL_NUM_DAIS_NOCODEC	8
#define SOF_SKL_NUM_DAIS_NOCODEC	9

#if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
#define SOF_SKL_NUM_DAIS		15
#define SOF_SKL_NUM_DAIS		16
#else
#define SOF_SKL_NUM_DAIS		SOF_SKL_NUM_DAIS_NOCODEC
#endif
+69 −13
Original line number Diff line number Diff line
@@ -76,6 +76,10 @@ static const struct sof_topology_token ipc4_sched_tokens[] = {
		offsetof(struct sof_ipc4_pipeline, core_id)},
	{SOF_TKN_SCHED_PRIORITY, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
		offsetof(struct sof_ipc4_pipeline, priority)},
	{SOF_TKN_SCHED_DIRECTION, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
		offsetof(struct sof_ipc4_pipeline, direction)},
	{SOF_TKN_SCHED_DIRECTION, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
		offsetof(struct sof_ipc4_pipeline, direction_valid)},
};

static const struct sof_topology_token pipeline_tokens[] = {
@@ -939,6 +943,10 @@ static int sof_ipc4_widget_setup_comp_pipeline(struct snd_sof_widget *swidget)

	swidget->core = pipeline->core_id;
	spipe->core_mask |= BIT(pipeline->core_id);
	if (pipeline->direction_valid) {
		spipe->direction = pipeline->direction;
		spipe->direction_valid = true;
	}

	if (pipeline->use_chain_dma) {
		dev_dbg(scomp->dev, "Set up chain DMA for %s\n", swidget->widget->name);
@@ -954,9 +962,9 @@ static int sof_ipc4_widget_setup_comp_pipeline(struct snd_sof_widget *swidget)
		goto err;
	}

	dev_dbg(scomp->dev, "pipeline '%s': id %d, pri %d, core_id %u, lp mode %d\n",
	dev_dbg(scomp->dev, "pipeline '%s': id %d, pri %d, core_id %u, lp mode %d direction %d\n",
		swidget->widget->name, swidget->pipeline_id,
		pipeline->priority, pipeline->core_id, pipeline->lp_mode);
		pipeline->priority, pipeline->core_id, pipeline->lp_mode, pipeline->direction);

	swidget->private = pipeline;

@@ -2004,6 +2012,25 @@ sof_ipc4_prepare_dai_copier(struct snd_sof_dev *sdev, struct snd_sof_dai *dai,
	return ret;
}

static void sof_ipc4_host_config(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget,
				 struct snd_sof_platform_stream_params *platform_params)
{
	struct sof_ipc4_copier *ipc4_copier = (struct sof_ipc4_copier *)swidget->private;
	struct snd_sof_widget *pipe_widget = swidget->spipe->pipe_widget;
	struct sof_ipc4_copier_data *copier_data = &ipc4_copier->data;
	struct sof_ipc4_pipeline *pipeline = pipe_widget->private;
	u32 host_dma_id = platform_params->stream_tag - 1;

	if (pipeline->use_chain_dma) {
		pipeline->msg.primary &= ~SOF_IPC4_GLB_CHAIN_DMA_HOST_ID_MASK;
		pipeline->msg.primary |= SOF_IPC4_GLB_CHAIN_DMA_HOST_ID(host_dma_id);
		return;
	}

	copier_data->gtw_cfg.node_id &= ~SOF_IPC4_NODE_INDEX_MASK;
	copier_data->gtw_cfg.node_id |=	SOF_IPC4_NODE_INDEX(host_dma_id);
}

static int
sof_ipc4_prepare_copier_module(struct snd_sof_widget *swidget,
			       struct snd_pcm_hw_params *fe_params,
@@ -2726,12 +2753,14 @@ static int sof_ipc4_prepare_process_module(struct snd_sof_widget *swidget,
	int input_fmt_index = 0;
	int ret;

	if (available_fmt->num_input_formats) {
		input_fmt_index = sof_ipc4_init_input_audio_fmt(sdev, swidget,
								&process->base_config,
								pipeline_params,
								available_fmt);
		if (input_fmt_index < 0)
			return input_fmt_index;
	}

	/* Configure output audio format only if the module supports output */
	if (available_fmt->num_output_formats) {
@@ -2740,12 +2769,28 @@ static int sof_ipc4_prepare_process_module(struct snd_sof_widget *swidget,
		u32 out_ref_rate, out_ref_channels;
		int out_ref_valid_bits, out_ref_type;

		if (available_fmt->num_input_formats) {
			in_fmt = &available_fmt->input_pin_fmts[input_fmt_index].audio_fmt;

			out_ref_rate = in_fmt->sampling_frequency;
		out_ref_channels = SOF_IPC4_AUDIO_FORMAT_CFG_CHANNELS_COUNT(in_fmt->fmt_cfg);
		out_ref_valid_bits = SOF_IPC4_AUDIO_FORMAT_CFG_V_BIT_DEPTH(in_fmt->fmt_cfg);
			out_ref_channels =
				SOF_IPC4_AUDIO_FORMAT_CFG_CHANNELS_COUNT(in_fmt->fmt_cfg);
			out_ref_valid_bits =
				SOF_IPC4_AUDIO_FORMAT_CFG_V_BIT_DEPTH(in_fmt->fmt_cfg);
			out_ref_type = sof_ipc4_fmt_cfg_to_type(in_fmt->fmt_cfg);
		} else {
			/* for modules without input formats, use FE params as reference */
			out_ref_rate = params_rate(fe_params);
			out_ref_channels = params_channels(fe_params);
			ret = sof_ipc4_get_sample_type(sdev, fe_params);
			if (ret < 0)
				return ret;
			out_ref_type = (u32)ret;

			out_ref_valid_bits = sof_ipc4_get_valid_bits(sdev, fe_params);
			if (out_ref_valid_bits < 0)
				return out_ref_valid_bits;
		}

		output_fmt_index = sof_ipc4_init_output_audio_fmt(sdev, swidget,
								  &process->base_config,
@@ -2773,6 +2818,16 @@ static int sof_ipc4_prepare_process_module(struct snd_sof_widget *swidget,
			if (ret)
				return ret;
		}

		/* set base cfg to match the first output format if there are no input formats */
		if (!available_fmt->num_input_formats) {
			struct sof_ipc4_audio_format *out_fmt;

			out_fmt = &available_fmt->output_pin_fmts[0].audio_fmt;

			/* copy output format */
			memcpy(&process->base_config.audio_fmt, out_fmt, sizeof(*out_fmt));
		}
	}

	sof_ipc4_dbg_module_audio_format(sdev->dev, swidget, available_fmt,
@@ -3929,4 +3984,5 @@ const struct sof_ipc_tplg_ops ipc4_tplg_ops = {
	.dai_get_param = sof_ipc4_dai_get_param,
	.tear_down_all_pipelines = sof_ipc4_tear_down_all_pipelines,
	.link_setup = sof_ipc4_link_setup,
	.host_config = sof_ipc4_host_config,
};
Loading