1// SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2//
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.
5//
6// Copyright(c) 2021 Intel Corporation. All rights reserved.
7//
8//
9
10#include <uapi/sound/sof/tokens.h>
11#include <sound/pcm_params.h>
12#include "sof-priv.h"
13#include "sof-audio.h"
14#include "ipc3-priv.h"
15#include "ops.h"
16
17/* Full volume for default values */
18#define VOL_ZERO_DB	BIT(VOLUME_FWL)
19
20/* size of tplg ABI in bytes */
21#define SOF_IPC3_TPLG_ABI_SIZE 3
22
23struct sof_widget_data {
24	int ctrl_type;
25	int ipc_cmd;
26	void *pdata;
27	size_t pdata_size;
28	struct snd_sof_control *control;
29};
30
31struct sof_process_types {
32	const char *name;
33	enum sof_ipc_process_type type;
34	enum sof_comp_type comp_type;
35};
36
37static const struct sof_process_types sof_process[] = {
38	{"EQFIR", SOF_PROCESS_EQFIR, SOF_COMP_EQ_FIR},
39	{"EQIIR", SOF_PROCESS_EQIIR, SOF_COMP_EQ_IIR},
40	{"KEYWORD_DETECT", SOF_PROCESS_KEYWORD_DETECT, SOF_COMP_KEYWORD_DETECT},
41	{"KPB", SOF_PROCESS_KPB, SOF_COMP_KPB},
42	{"CHAN_SELECTOR", SOF_PROCESS_CHAN_SELECTOR, SOF_COMP_SELECTOR},
43	{"MUX", SOF_PROCESS_MUX, SOF_COMP_MUX},
44	{"DEMUX", SOF_PROCESS_DEMUX, SOF_COMP_DEMUX},
45	{"DCBLOCK", SOF_PROCESS_DCBLOCK, SOF_COMP_DCBLOCK},
46	{"SMART_AMP", SOF_PROCESS_SMART_AMP, SOF_COMP_SMART_AMP},
47};
48
49static enum sof_ipc_process_type find_process(const char *name)
50{
51	int i;
52
53	for (i = 0; i < ARRAY_SIZE(sof_process); i++) {
54		if (strcmp(name, sof_process[i].name) == 0)
55			return sof_process[i].type;
56	}
57
58	return SOF_PROCESS_NONE;
59}
60
61static int get_token_process_type(void *elem, void *object, u32 offset)
62{
63	u32 *val = (u32 *)((u8 *)object + offset);
64
65	*val = find_process((const char *)elem);
66	return 0;
67}
68
69/* Buffers */
70static const struct sof_topology_token buffer_tokens[] = {
71	{SOF_TKN_BUF_SIZE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
72		offsetof(struct sof_ipc_buffer, size)},
73	{SOF_TKN_BUF_CAPS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
74		offsetof(struct sof_ipc_buffer, caps)},
75};
76
77/* DAI */
78static const struct sof_topology_token dai_tokens[] = {
79	{SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type,
80		offsetof(struct sof_ipc_comp_dai, type)},
81	{SOF_TKN_DAI_INDEX, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
82		offsetof(struct sof_ipc_comp_dai, dai_index)},
83	{SOF_TKN_DAI_DIRECTION, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
84		offsetof(struct sof_ipc_comp_dai, direction)},
85};
86
87/* BE DAI link */
88static const struct sof_topology_token dai_link_tokens[] = {
89	{SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type,
90		offsetof(struct sof_ipc_dai_config, type)},
91	{SOF_TKN_DAI_INDEX, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
92		offsetof(struct sof_ipc_dai_config, dai_index)},
93};
94
95/* scheduling */
96static const struct sof_topology_token sched_tokens[] = {
97	{SOF_TKN_SCHED_PERIOD, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
98		offsetof(struct sof_ipc_pipe_new, period)},
99	{SOF_TKN_SCHED_PRIORITY, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
100		offsetof(struct sof_ipc_pipe_new, priority)},
101	{SOF_TKN_SCHED_MIPS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
102		offsetof(struct sof_ipc_pipe_new, period_mips)},
103	{SOF_TKN_SCHED_CORE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
104		offsetof(struct sof_ipc_pipe_new, core)},
105	{SOF_TKN_SCHED_FRAMES, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
106		offsetof(struct sof_ipc_pipe_new, frames_per_sched)},
107	{SOF_TKN_SCHED_TIME_DOMAIN, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
108		offsetof(struct sof_ipc_pipe_new, time_domain)},
109};
110
111static const struct sof_topology_token pipeline_tokens[] = {
112	{SOF_TKN_SCHED_DYNAMIC_PIPELINE, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
113		offsetof(struct snd_sof_widget, dynamic_pipeline_widget)},
114
115};
116
117/* volume */
118static const struct sof_topology_token volume_tokens[] = {
119	{SOF_TKN_VOLUME_RAMP_STEP_TYPE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
120		offsetof(struct sof_ipc_comp_volume, ramp)},
121	{SOF_TKN_VOLUME_RAMP_STEP_MS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
122		offsetof(struct sof_ipc_comp_volume, initial_ramp)},
123};
124
125/* SRC */
126static const struct sof_topology_token src_tokens[] = {
127	{SOF_TKN_SRC_RATE_IN, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
128		offsetof(struct sof_ipc_comp_src, source_rate)},
129	{SOF_TKN_SRC_RATE_OUT, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
130		offsetof(struct sof_ipc_comp_src, sink_rate)},
131};
132
133/* ASRC */
134static const struct sof_topology_token asrc_tokens[] = {
135	{SOF_TKN_ASRC_RATE_IN, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
136		offsetof(struct sof_ipc_comp_asrc, source_rate)},
137	{SOF_TKN_ASRC_RATE_OUT, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
138		offsetof(struct sof_ipc_comp_asrc, sink_rate)},
139	{SOF_TKN_ASRC_ASYNCHRONOUS_MODE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
140		offsetof(struct sof_ipc_comp_asrc, asynchronous_mode)},
141	{SOF_TKN_ASRC_OPERATION_MODE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
142		offsetof(struct sof_ipc_comp_asrc, operation_mode)},
143};
144
145/* EFFECT */
146static const struct sof_topology_token process_tokens[] = {
147	{SOF_TKN_PROCESS_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_process_type,
148		offsetof(struct sof_ipc_comp_process, type)},
149};
150
151/* PCM */
152static const struct sof_topology_token pcm_tokens[] = {
153	{SOF_TKN_PCM_DMAC_CONFIG, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
154		offsetof(struct sof_ipc_comp_host, dmac_config)},
155};
156
157/* Generic components */
158static const struct sof_topology_token comp_tokens[] = {
159	{SOF_TKN_COMP_PERIOD_SINK_COUNT, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
160		offsetof(struct sof_ipc_comp_config, periods_sink)},
161	{SOF_TKN_COMP_PERIOD_SOURCE_COUNT, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
162		offsetof(struct sof_ipc_comp_config, periods_source)},
163	{SOF_TKN_COMP_FORMAT,
164		SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_comp_format,
165		offsetof(struct sof_ipc_comp_config, frame_fmt)},
166};
167
168/* SSP */
169static const struct sof_topology_token ssp_tokens[] = {
170	{SOF_TKN_INTEL_SSP_CLKS_CONTROL, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
171		offsetof(struct sof_ipc_dai_ssp_params, clks_control)},
172	{SOF_TKN_INTEL_SSP_MCLK_ID, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
173		offsetof(struct sof_ipc_dai_ssp_params, mclk_id)},
174	{SOF_TKN_INTEL_SSP_SAMPLE_BITS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
175		offsetof(struct sof_ipc_dai_ssp_params, sample_valid_bits)},
176	{SOF_TKN_INTEL_SSP_FRAME_PULSE_WIDTH, SND_SOC_TPLG_TUPLE_TYPE_SHORT,	get_token_u16,
177		offsetof(struct sof_ipc_dai_ssp_params, frame_pulse_width)},
178	{SOF_TKN_INTEL_SSP_QUIRKS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
179		offsetof(struct sof_ipc_dai_ssp_params, quirks)},
180	{SOF_TKN_INTEL_SSP_TDM_PADDING_PER_SLOT, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
181		offsetof(struct sof_ipc_dai_ssp_params, tdm_per_slot_padding_flag)},
182	{SOF_TKN_INTEL_SSP_BCLK_DELAY, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
183		offsetof(struct sof_ipc_dai_ssp_params, bclk_delay)},
184};
185
186/* ALH */
187static const struct sof_topology_token alh_tokens[] = {
188	{SOF_TKN_INTEL_ALH_RATE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
189		offsetof(struct sof_ipc_dai_alh_params, rate)},
190	{SOF_TKN_INTEL_ALH_CH,	SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
191		offsetof(struct sof_ipc_dai_alh_params, channels)},
192};
193
194/* DMIC */
195static const struct sof_topology_token dmic_tokens[] = {
196	{SOF_TKN_INTEL_DMIC_DRIVER_VERSION, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
197		offsetof(struct sof_ipc_dai_dmic_params, driver_ipc_version)},
198	{SOF_TKN_INTEL_DMIC_CLK_MIN, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
199		offsetof(struct sof_ipc_dai_dmic_params, pdmclk_min)},
200	{SOF_TKN_INTEL_DMIC_CLK_MAX, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
201		offsetof(struct sof_ipc_dai_dmic_params, pdmclk_max)},
202	{SOF_TKN_INTEL_DMIC_SAMPLE_RATE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
203		offsetof(struct sof_ipc_dai_dmic_params, fifo_fs)},
204	{SOF_TKN_INTEL_DMIC_DUTY_MIN, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
205		offsetof(struct sof_ipc_dai_dmic_params, duty_min)},
206	{SOF_TKN_INTEL_DMIC_DUTY_MAX, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
207		offsetof(struct sof_ipc_dai_dmic_params, duty_max)},
208	{SOF_TKN_INTEL_DMIC_NUM_PDM_ACTIVE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
209		offsetof(struct sof_ipc_dai_dmic_params, num_pdm_active)},
210	{SOF_TKN_INTEL_DMIC_FIFO_WORD_LENGTH, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
211		offsetof(struct sof_ipc_dai_dmic_params, fifo_bits)},
212	{SOF_TKN_INTEL_DMIC_UNMUTE_RAMP_TIME_MS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
213		offsetof(struct sof_ipc_dai_dmic_params, unmute_ramp_time)},
214};
215
216/* ESAI */
217static const struct sof_topology_token esai_tokens[] = {
218	{SOF_TKN_IMX_ESAI_MCLK_ID, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
219		offsetof(struct sof_ipc_dai_esai_params, mclk_id)},
220};
221
222/* SAI */
223static const struct sof_topology_token sai_tokens[] = {
224	{SOF_TKN_IMX_SAI_MCLK_ID, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
225		offsetof(struct sof_ipc_dai_sai_params, mclk_id)},
226};
227
228/*
229 * DMIC PDM Tokens
230 * SOF_TKN_INTEL_DMIC_PDM_CTRL_ID should be the first token
231 * as it increments the index while parsing the array of pdm tokens
232 * and determines the correct offset
233 */
234static const struct sof_topology_token dmic_pdm_tokens[] = {
235	{SOF_TKN_INTEL_DMIC_PDM_CTRL_ID, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
236		offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, id)},
237	{SOF_TKN_INTEL_DMIC_PDM_MIC_A_Enable, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
238		offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, enable_mic_a)},
239	{SOF_TKN_INTEL_DMIC_PDM_MIC_B_Enable, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
240		offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, enable_mic_b)},
241	{SOF_TKN_INTEL_DMIC_PDM_POLARITY_A, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
242		offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, polarity_mic_a)},
243	{SOF_TKN_INTEL_DMIC_PDM_POLARITY_B, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
244		offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, polarity_mic_b)},
245	{SOF_TKN_INTEL_DMIC_PDM_CLK_EDGE, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
246		offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, clk_edge)},
247	{SOF_TKN_INTEL_DMIC_PDM_SKEW, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
248		offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, skew)},
249};
250
251/* HDA */
252static const struct sof_topology_token hda_tokens[] = {
253	{SOF_TKN_INTEL_HDA_RATE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
254		offsetof(struct sof_ipc_dai_hda_params, rate)},
255	{SOF_TKN_INTEL_HDA_CH, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
256		offsetof(struct sof_ipc_dai_hda_params, channels)},
257};
258
259/* AFE */
260static const struct sof_topology_token afe_tokens[] = {
261	{SOF_TKN_MEDIATEK_AFE_RATE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
262		offsetof(struct sof_ipc_dai_mtk_afe_params, rate)},
263	{SOF_TKN_MEDIATEK_AFE_CH, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
264		offsetof(struct sof_ipc_dai_mtk_afe_params, channels)},
265	{SOF_TKN_MEDIATEK_AFE_FORMAT, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_comp_format,
266		offsetof(struct sof_ipc_dai_mtk_afe_params, format)},
267};
268
269/* ACPDMIC */
270static const struct sof_topology_token acpdmic_tokens[] = {
271	{SOF_TKN_AMD_ACPDMIC_RATE,
272		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
273		offsetof(struct sof_ipc_dai_acpdmic_params, pdm_rate)},
274	{SOF_TKN_AMD_ACPDMIC_CH,
275		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
276		offsetof(struct sof_ipc_dai_acpdmic_params, pdm_ch)},
277};
278
279/* ACPI2S */
280static const struct sof_topology_token acpi2s_tokens[] = {
281	{SOF_TKN_AMD_ACPI2S_RATE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
282		offsetof(struct sof_ipc_dai_acp_params, fsync_rate)},
283	{SOF_TKN_AMD_ACPI2S_CH, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
284		offsetof(struct sof_ipc_dai_acp_params, tdm_slots)},
285	{SOF_TKN_AMD_ACPI2S_TDM_MODE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
286		offsetof(struct sof_ipc_dai_acp_params, tdm_mode)},
287};
288
289/* Core tokens */
290static const struct sof_topology_token core_tokens[] = {
291	{SOF_TKN_COMP_CORE_ID, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
292		offsetof(struct sof_ipc_comp, core)},
293};
294
295/* Component extended tokens */
296static const struct sof_topology_token comp_ext_tokens[] = {
297	{SOF_TKN_COMP_UUID, SND_SOC_TPLG_TUPLE_TYPE_UUID, get_token_uuid,
298		offsetof(struct snd_sof_widget, uuid)},
299};
300
301static const struct sof_token_info ipc3_token_list[SOF_TOKEN_COUNT] = {
302	[SOF_PCM_TOKENS] = {"PCM tokens", pcm_tokens, ARRAY_SIZE(pcm_tokens)},
303	[SOF_PIPELINE_TOKENS] = {"Pipeline tokens", pipeline_tokens, ARRAY_SIZE(pipeline_tokens)},
304	[SOF_SCHED_TOKENS] = {"Scheduler tokens", sched_tokens, ARRAY_SIZE(sched_tokens)},
305	[SOF_COMP_TOKENS] = {"Comp tokens", comp_tokens, ARRAY_SIZE(comp_tokens)},
306	[SOF_CORE_TOKENS] = {"Core tokens", core_tokens, ARRAY_SIZE(core_tokens)},
307	[SOF_COMP_EXT_TOKENS] = {"AFE tokens", comp_ext_tokens, ARRAY_SIZE(comp_ext_tokens)},
308	[SOF_BUFFER_TOKENS] = {"Buffer tokens", buffer_tokens, ARRAY_SIZE(buffer_tokens)},
309	[SOF_VOLUME_TOKENS] = {"Volume tokens", volume_tokens, ARRAY_SIZE(volume_tokens)},
310	[SOF_SRC_TOKENS] = {"SRC tokens", src_tokens, ARRAY_SIZE(src_tokens)},
311	[SOF_ASRC_TOKENS] = {"ASRC tokens", asrc_tokens, ARRAY_SIZE(asrc_tokens)},
312	[SOF_PROCESS_TOKENS] = {"Process tokens", process_tokens, ARRAY_SIZE(process_tokens)},
313	[SOF_DAI_TOKENS] = {"DAI tokens", dai_tokens, ARRAY_SIZE(dai_tokens)},
314	[SOF_DAI_LINK_TOKENS] = {"DAI link tokens", dai_link_tokens, ARRAY_SIZE(dai_link_tokens)},
315	[SOF_HDA_TOKENS] = {"HDA tokens", hda_tokens, ARRAY_SIZE(hda_tokens)},
316	[SOF_SSP_TOKENS] = {"SSP tokens", ssp_tokens, ARRAY_SIZE(ssp_tokens)},
317	[SOF_ALH_TOKENS] = {"ALH tokens", alh_tokens, ARRAY_SIZE(alh_tokens)},
318	[SOF_DMIC_TOKENS] = {"DMIC tokens", dmic_tokens, ARRAY_SIZE(dmic_tokens)},
319	[SOF_DMIC_PDM_TOKENS] = {"DMIC PDM tokens", dmic_pdm_tokens, ARRAY_SIZE(dmic_pdm_tokens)},
320	[SOF_ESAI_TOKENS] = {"ESAI tokens", esai_tokens, ARRAY_SIZE(esai_tokens)},
321	[SOF_SAI_TOKENS] = {"SAI tokens", sai_tokens, ARRAY_SIZE(sai_tokens)},
322	[SOF_AFE_TOKENS] = {"AFE tokens", afe_tokens, ARRAY_SIZE(afe_tokens)},
323	[SOF_ACPDMIC_TOKENS] = {"ACPDMIC tokens", acpdmic_tokens, ARRAY_SIZE(acpdmic_tokens)},
324	[SOF_ACPI2S_TOKENS]   = {"ACPI2S tokens", acpi2s_tokens, ARRAY_SIZE(acpi2s_tokens)},
325};
326
327/**
328 * sof_comp_alloc - allocate and initialize buffer for a new component
329 * @swidget: pointer to struct snd_sof_widget containing extended data
330 * @ipc_size: IPC payload size that will be updated depending on valid
331 *  extended data.
332 * @index: ID of the pipeline the component belongs to
333 *
334 * Return: The pointer to the new allocated component, NULL if failed.
335 */
336static void *sof_comp_alloc(struct snd_sof_widget *swidget, size_t *ipc_size,
337			    int index)
338{
339	struct sof_ipc_comp *comp;
340	size_t total_size = *ipc_size;
341	size_t ext_size = sizeof(swidget->uuid);
342
343	/* only non-zero UUID is valid */
344	if (!guid_is_null(&swidget->uuid))
345		total_size += ext_size;
346
347	comp = kzalloc(total_size, GFP_KERNEL);
348	if (!comp)
349		return NULL;
350
351	/* configure comp new IPC message */
352	comp->hdr.size = total_size;
353	comp->hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
354	comp->id = swidget->comp_id;
355	comp->pipeline_id = index;
356	comp->core = swidget->core;
357
358	/* handle the extended data if needed */
359	if (total_size > *ipc_size) {
360		/* append extended data to the end of the component */
361		memcpy((u8 *)comp + *ipc_size, &swidget->uuid, ext_size);
362		comp->ext_data_length = ext_size;
363	}
364
365	/* update ipc_size and return */
366	*ipc_size = total_size;
367	return comp;
368}
369
370static void sof_dbg_comp_config(struct snd_soc_component *scomp, struct sof_ipc_comp_config *config)
371{
372	dev_dbg(scomp->dev, " config: periods snk %d src %d fmt %d\n",
373		config->periods_sink, config->periods_source,
374		config->frame_fmt);
375}
376
377static int sof_ipc3_widget_setup_comp_host(struct snd_sof_widget *swidget)
378{
379	struct snd_soc_component *scomp = swidget->scomp;
380	struct sof_ipc_comp_host *host;
381	size_t ipc_size = sizeof(*host);
382	int ret;
383
384	host = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id);
385	if (!host)
386		return -ENOMEM;
387	swidget->private = host;
388
389	/* configure host comp IPC message */
390	host->comp.type = SOF_COMP_HOST;
391	host->config.hdr.size = sizeof(host->config);
392
393	if (swidget->id == snd_soc_dapm_aif_out)
394		host->direction = SOF_IPC_STREAM_CAPTURE;
395	else
396		host->direction = SOF_IPC_STREAM_PLAYBACK;
397
398	/* parse one set of pcm_tokens */
399	ret = sof_update_ipc_object(scomp, host, SOF_PCM_TOKENS, swidget->tuples,
400				    swidget->num_tuples, sizeof(*host), 1);
401	if (ret < 0)
402		goto err;
403
404	/* parse one set of comp_tokens */
405	ret = sof_update_ipc_object(scomp, &host->config, SOF_COMP_TOKENS, swidget->tuples,
406				    swidget->num_tuples, sizeof(host->config), 1);
407	if (ret < 0)
408		goto err;
409
410	dev_dbg(scomp->dev, "loaded host %s\n", swidget->widget->name);
411	sof_dbg_comp_config(scomp, &host->config);
412
413	return 0;
414err:
415	kfree(swidget->private);
416	swidget->private = NULL;
417
418	return ret;
419}
420
421static void sof_ipc3_widget_free_comp(struct snd_sof_widget *swidget)
422{
423	kfree(swidget->private);
424}
425
426static int sof_ipc3_widget_setup_comp_tone(struct snd_sof_widget *swidget)
427{
428	struct snd_soc_component *scomp = swidget->scomp;
429	struct sof_ipc_comp_tone *tone;
430	size_t ipc_size = sizeof(*tone);
431	int ret;
432
433	tone = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id);
434	if (!tone)
435		return -ENOMEM;
436
437	swidget->private = tone;
438
439	/* configure siggen IPC message */
440	tone->comp.type = SOF_COMP_TONE;
441	tone->config.hdr.size = sizeof(tone->config);
442
443	/* parse one set of comp tokens */
444	ret = sof_update_ipc_object(scomp, &tone->config, SOF_COMP_TOKENS, swidget->tuples,
445				    swidget->num_tuples, sizeof(tone->config), 1);
446	if (ret < 0) {
447		kfree(swidget->private);
448		swidget->private = NULL;
449		return ret;
450	}
451
452	dev_dbg(scomp->dev, "tone %s: frequency %d amplitude %d\n",
453		swidget->widget->name, tone->frequency, tone->amplitude);
454	sof_dbg_comp_config(scomp, &tone->config);
455
456	return 0;
457}
458
459static int sof_ipc3_widget_setup_comp_mixer(struct snd_sof_widget *swidget)
460{
461	struct snd_soc_component *scomp = swidget->scomp;
462	struct sof_ipc_comp_mixer *mixer;
463	size_t ipc_size = sizeof(*mixer);
464	int ret;
465
466	mixer = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id);
467	if (!mixer)
468		return -ENOMEM;
469
470	swidget->private = mixer;
471
472	/* configure mixer IPC message */
473	mixer->comp.type = SOF_COMP_MIXER;
474	mixer->config.hdr.size = sizeof(mixer->config);
475
476	/* parse one set of comp tokens */
477	ret = sof_update_ipc_object(scomp, &mixer->config, SOF_COMP_TOKENS,
478				    swidget->tuples, swidget->num_tuples,
479				    sizeof(mixer->config), 1);
480	if (ret < 0) {
481		kfree(swidget->private);
482		swidget->private = NULL;
483
484		return ret;
485	}
486
487	dev_dbg(scomp->dev, "loaded mixer %s\n", swidget->widget->name);
488	sof_dbg_comp_config(scomp, &mixer->config);
489
490	return 0;
491}
492
493static int sof_ipc3_widget_setup_comp_pipeline(struct snd_sof_widget *swidget)
494{
495	struct snd_soc_component *scomp = swidget->scomp;
496	struct snd_sof_pipeline *spipe = swidget->spipe;
497	struct sof_ipc_pipe_new *pipeline;
498	struct snd_sof_widget *comp_swidget;
499	int ret;
500
501	pipeline = kzalloc(sizeof(*pipeline), GFP_KERNEL);
502	if (!pipeline)
503		return -ENOMEM;
504
505	/* configure pipeline IPC message */
506	pipeline->hdr.size = sizeof(*pipeline);
507	pipeline->hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_PIPE_NEW;
508	pipeline->pipeline_id = swidget->pipeline_id;
509	pipeline->comp_id = swidget->comp_id;
510
511	swidget->private = pipeline;
512
513	/* component at start of pipeline is our stream id */
514	comp_swidget = snd_sof_find_swidget(scomp, swidget->widget->sname);
515	if (!comp_swidget) {
516		dev_err(scomp->dev, "scheduler %s refers to non existent widget %s\n",
517			swidget->widget->name, swidget->widget->sname);
518		ret = -EINVAL;
519		goto err;
520	}
521
522	pipeline->sched_id = comp_swidget->comp_id;
523
524	/* parse one set of scheduler tokens */
525	ret = sof_update_ipc_object(scomp, pipeline, SOF_SCHED_TOKENS, swidget->tuples,
526				    swidget->num_tuples, sizeof(*pipeline), 1);
527	if (ret < 0)
528		goto err;
529
530	/* parse one set of pipeline tokens */
531	ret = sof_update_ipc_object(scomp, swidget, SOF_PIPELINE_TOKENS, swidget->tuples,
532				    swidget->num_tuples, sizeof(*swidget), 1);
533	if (ret < 0)
534		goto err;
535
536	if (sof_debug_check_flag(SOF_DBG_DISABLE_MULTICORE))
537		pipeline->core = SOF_DSP_PRIMARY_CORE;
538
539	if (sof_debug_check_flag(SOF_DBG_DYNAMIC_PIPELINES_OVERRIDE))
540		swidget->dynamic_pipeline_widget =
541			sof_debug_check_flag(SOF_DBG_DYNAMIC_PIPELINES_ENABLE);
542
543	dev_dbg(scomp->dev, "pipeline %s: period %d pri %d mips %d core %d frames %d dynamic %d\n",
544		swidget->widget->name, pipeline->period, pipeline->priority,
545		pipeline->period_mips, pipeline->core, pipeline->frames_per_sched,
546		swidget->dynamic_pipeline_widget);
547
548	swidget->core = pipeline->core;
549	spipe->core_mask |= BIT(pipeline->core);
550
551	return 0;
552
553err:
554	kfree(swidget->private);
555	swidget->private = NULL;
556
557	return ret;
558}
559
560static int sof_ipc3_widget_setup_comp_buffer(struct snd_sof_widget *swidget)
561{
562	struct snd_soc_component *scomp = swidget->scomp;
563	struct sof_ipc_buffer *buffer;
564	int ret;
565
566	buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
567	if (!buffer)
568		return -ENOMEM;
569
570	swidget->private = buffer;
571
572	/* configure dai IPC message */
573	buffer->comp.hdr.size = sizeof(*buffer);
574	buffer->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_BUFFER_NEW;
575	buffer->comp.id = swidget->comp_id;
576	buffer->comp.type = SOF_COMP_BUFFER;
577	buffer->comp.pipeline_id = swidget->pipeline_id;
578	buffer->comp.core = swidget->core;
579
580	/* parse one set of buffer tokens */
581	ret = sof_update_ipc_object(scomp, buffer, SOF_BUFFER_TOKENS, swidget->tuples,
582				    swidget->num_tuples, sizeof(*buffer), 1);
583	if (ret < 0) {
584		kfree(swidget->private);
585		swidget->private = NULL;
586		return ret;
587	}
588
589	dev_dbg(scomp->dev, "buffer %s: size %d caps 0x%x\n",
590		swidget->widget->name, buffer->size, buffer->caps);
591
592	return 0;
593}
594
595static int sof_ipc3_widget_setup_comp_src(struct snd_sof_widget *swidget)
596{
597	struct snd_soc_component *scomp = swidget->scomp;
598	struct sof_ipc_comp_src *src;
599	size_t ipc_size = sizeof(*src);
600	int ret;
601
602	src = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id);
603	if (!src)
604		return -ENOMEM;
605
606	swidget->private = src;
607
608	/* configure src IPC message */
609	src->comp.type = SOF_COMP_SRC;
610	src->config.hdr.size = sizeof(src->config);
611
612	/* parse one set of src tokens */
613	ret = sof_update_ipc_object(scomp, src, SOF_SRC_TOKENS, swidget->tuples,
614				    swidget->num_tuples, sizeof(*src), 1);
615	if (ret < 0)
616		goto err;
617
618	/* parse one set of comp tokens */
619	ret = sof_update_ipc_object(scomp, &src->config, SOF_COMP_TOKENS,
620				    swidget->tuples, swidget->num_tuples, sizeof(src->config), 1);
621	if (ret < 0)
622		goto err;
623
624	dev_dbg(scomp->dev, "src %s: source rate %d sink rate %d\n",
625		swidget->widget->name, src->source_rate, src->sink_rate);
626	sof_dbg_comp_config(scomp, &src->config);
627
628	return 0;
629err:
630	kfree(swidget->private);
631	swidget->private = NULL;
632
633	return ret;
634}
635
636static int sof_ipc3_widget_setup_comp_asrc(struct snd_sof_widget *swidget)
637{
638	struct snd_soc_component *scomp = swidget->scomp;
639	struct sof_ipc_comp_asrc *asrc;
640	size_t ipc_size = sizeof(*asrc);
641	int ret;
642
643	asrc = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id);
644	if (!asrc)
645		return -ENOMEM;
646
647	swidget->private = asrc;
648
649	/* configure ASRC IPC message */
650	asrc->comp.type = SOF_COMP_ASRC;
651	asrc->config.hdr.size = sizeof(asrc->config);
652
653	/* parse one set of asrc tokens */
654	ret = sof_update_ipc_object(scomp, asrc, SOF_ASRC_TOKENS, swidget->tuples,
655				    swidget->num_tuples, sizeof(*asrc), 1);
656	if (ret < 0)
657		goto err;
658
659	/* parse one set of comp tokens */
660	ret = sof_update_ipc_object(scomp, &asrc->config, SOF_COMP_TOKENS,
661				    swidget->tuples, swidget->num_tuples, sizeof(asrc->config), 1);
662	if (ret < 0)
663		goto err;
664
665	dev_dbg(scomp->dev, "asrc %s: source rate %d sink rate %d asynch %d operation %d\n",
666		swidget->widget->name, asrc->source_rate, asrc->sink_rate,
667		asrc->asynchronous_mode, asrc->operation_mode);
668
669	sof_dbg_comp_config(scomp, &asrc->config);
670
671	return 0;
672err:
673	kfree(swidget->private);
674	swidget->private = NULL;
675
676	return ret;
677}
678
679/*
680 * Mux topology
681 */
682static int sof_ipc3_widget_setup_comp_mux(struct snd_sof_widget *swidget)
683{
684	struct snd_soc_component *scomp = swidget->scomp;
685	struct sof_ipc_comp_mux *mux;
686	size_t ipc_size = sizeof(*mux);
687	int ret;
688
689	mux = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id);
690	if (!mux)
691		return -ENOMEM;
692
693	swidget->private = mux;
694
695	/* configure mux IPC message */
696	mux->comp.type = SOF_COMP_MUX;
697	mux->config.hdr.size = sizeof(mux->config);
698
699	/* parse one set of comp tokens */
700	ret = sof_update_ipc_object(scomp, &mux->config, SOF_COMP_TOKENS,
701				    swidget->tuples, swidget->num_tuples, sizeof(mux->config), 1);
702	if (ret < 0) {
703		kfree(swidget->private);
704		swidget->private = NULL;
705		return ret;
706	}
707
708	dev_dbg(scomp->dev, "loaded mux %s\n", swidget->widget->name);
709	sof_dbg_comp_config(scomp, &mux->config);
710
711	return 0;
712}
713
714/*
715 * PGA Topology
716 */
717
718static int sof_ipc3_widget_setup_comp_pga(struct snd_sof_widget *swidget)
719{
720	struct snd_soc_component *scomp = swidget->scomp;
721	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
722	struct sof_ipc_comp_volume *volume;
723	struct snd_sof_control *scontrol;
724	size_t ipc_size = sizeof(*volume);
725	int min_step, max_step;
726	int ret;
727
728	volume = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id);
729	if (!volume)
730		return -ENOMEM;
731
732	swidget->private = volume;
733
734	/* configure volume IPC message */
735	volume->comp.type = SOF_COMP_VOLUME;
736	volume->config.hdr.size = sizeof(volume->config);
737
738	/* parse one set of volume tokens */
739	ret = sof_update_ipc_object(scomp, volume, SOF_VOLUME_TOKENS, swidget->tuples,
740				    swidget->num_tuples, sizeof(*volume), 1);
741	if (ret < 0)
742		goto err;
743
744	/* parse one set of comp tokens */
745	ret = sof_update_ipc_object(scomp, &volume->config, SOF_COMP_TOKENS,
746				    swidget->tuples, swidget->num_tuples,
747				    sizeof(volume->config), 1);
748	if (ret < 0)
749		goto err;
750
751	dev_dbg(scomp->dev, "loaded PGA %s\n", swidget->widget->name);
752	sof_dbg_comp_config(scomp, &volume->config);
753
754	list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
755		if (scontrol->comp_id == swidget->comp_id &&
756		    scontrol->volume_table) {
757			min_step = scontrol->min_volume_step;
758			max_step = scontrol->max_volume_step;
759			volume->min_value = scontrol->volume_table[min_step];
760			volume->max_value = scontrol->volume_table[max_step];
761			volume->channels = scontrol->num_channels;
762			break;
763		}
764	}
765
766	return 0;
767err:
768	kfree(swidget->private);
769	swidget->private = NULL;
770
771	return ret;
772}
773
774static int sof_get_control_data(struct snd_soc_component *scomp,
775				struct snd_soc_dapm_widget *widget,
776				struct sof_widget_data *wdata, size_t *size)
777{
778	const struct snd_kcontrol_new *kc;
779	struct sof_ipc_ctrl_data *cdata;
780	struct soc_mixer_control *sm;
781	struct soc_bytes_ext *sbe;
782	struct soc_enum *se;
783	int i;
784
785	*size = 0;
786
787	for (i = 0; i < widget->num_kcontrols; i++) {
788		kc = &widget->kcontrol_news[i];
789
790		switch (widget->dobj.widget.kcontrol_type[i]) {
791		case SND_SOC_TPLG_TYPE_MIXER:
792			sm = (struct soc_mixer_control *)kc->private_value;
793			wdata[i].control = sm->dobj.private;
794			break;
795		case SND_SOC_TPLG_TYPE_BYTES:
796			sbe = (struct soc_bytes_ext *)kc->private_value;
797			wdata[i].control = sbe->dobj.private;
798			break;
799		case SND_SOC_TPLG_TYPE_ENUM:
800			se = (struct soc_enum *)kc->private_value;
801			wdata[i].control = se->dobj.private;
802			break;
803		default:
804			dev_err(scomp->dev, "Unknown kcontrol type %u in widget %s\n",
805				widget->dobj.widget.kcontrol_type[i], widget->name);
806			return -EINVAL;
807		}
808
809		if (!wdata[i].control) {
810			dev_err(scomp->dev, "No scontrol for widget %s\n", widget->name);
811			return -EINVAL;
812		}
813
814		cdata = wdata[i].control->ipc_control_data;
815
816		if (widget->dobj.widget.kcontrol_type[i] == SND_SOC_TPLG_TYPE_BYTES) {
817			/* make sure data is valid - data can be updated at runtime */
818			if (cdata->data->magic != SOF_ABI_MAGIC)
819				return -EINVAL;
820
821			wdata[i].pdata = cdata->data->data;
822			wdata[i].pdata_size = cdata->data->size;
823		} else {
824			/* points to the control data union */
825			wdata[i].pdata = cdata->chanv;
826			/*
827			 * wdata[i].control->size is calculated with struct_size
828			 * and includes the size of struct sof_ipc_ctrl_data
829			 */
830			wdata[i].pdata_size = wdata[i].control->size -
831					      sizeof(struct sof_ipc_ctrl_data);
832		}
833
834		*size += wdata[i].pdata_size;
835
836		/* get data type */
837		switch (cdata->cmd) {
838		case SOF_CTRL_CMD_VOLUME:
839		case SOF_CTRL_CMD_ENUM:
840		case SOF_CTRL_CMD_SWITCH:
841			wdata[i].ipc_cmd = SOF_IPC_COMP_SET_VALUE;
842			wdata[i].ctrl_type = SOF_CTRL_TYPE_VALUE_CHAN_SET;
843			break;
844		case SOF_CTRL_CMD_BINARY:
845			wdata[i].ipc_cmd = SOF_IPC_COMP_SET_DATA;
846			wdata[i].ctrl_type = SOF_CTRL_TYPE_DATA_SET;
847			break;
848		default:
849			break;
850		}
851	}
852
853	return 0;
854}
855
856static int sof_process_load(struct snd_soc_component *scomp,
857			    struct snd_sof_widget *swidget, int type)
858{
859	struct snd_soc_dapm_widget *widget = swidget->widget;
860	struct sof_ipc_comp_process *process;
861	struct sof_widget_data *wdata = NULL;
862	size_t ipc_data_size = 0;
863	size_t ipc_size;
864	int offset = 0;
865	int ret;
866	int i;
867
868	/* allocate struct for widget control data sizes and types */
869	if (widget->num_kcontrols) {
870		wdata = kcalloc(widget->num_kcontrols, sizeof(*wdata), GFP_KERNEL);
871		if (!wdata)
872			return -ENOMEM;
873
874		/* get possible component controls and get size of all pdata */
875		ret = sof_get_control_data(scomp, widget, wdata, &ipc_data_size);
876		if (ret < 0)
877			goto out;
878	}
879
880	ipc_size = sizeof(struct sof_ipc_comp_process) + ipc_data_size;
881
882	/* we are exceeding max ipc size, config needs to be sent separately */
883	if (ipc_size > SOF_IPC_MSG_MAX_SIZE) {
884		ipc_size -= ipc_data_size;
885		ipc_data_size = 0;
886	}
887
888	process = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id);
889	if (!process) {
890		ret = -ENOMEM;
891		goto out;
892	}
893
894	swidget->private = process;
895
896	/* configure iir IPC message */
897	process->comp.type = type;
898	process->config.hdr.size = sizeof(process->config);
899
900	/* parse one set of comp tokens */
901	ret = sof_update_ipc_object(scomp, &process->config, SOF_COMP_TOKENS,
902				    swidget->tuples, swidget->num_tuples,
903				    sizeof(process->config), 1);
904	if (ret < 0)
905		goto err;
906
907	dev_dbg(scomp->dev, "loaded process %s\n", swidget->widget->name);
908	sof_dbg_comp_config(scomp, &process->config);
909
910	/*
911	 * found private data in control, so copy it.
912	 * get possible component controls - get size of all pdata,
913	 * then memcpy with headers
914	 */
915	if (ipc_data_size) {
916		for (i = 0; i < widget->num_kcontrols; i++) {
917			if (!wdata[i].pdata_size)
918				continue;
919
920			memcpy(&process->data[offset], wdata[i].pdata,
921			       wdata[i].pdata_size);
922			offset += wdata[i].pdata_size;
923		}
924	}
925
926	process->size = ipc_data_size;
927
928	kfree(wdata);
929
930	return 0;
931err:
932	kfree(swidget->private);
933	swidget->private = NULL;
934out:
935	kfree(wdata);
936	return ret;
937}
938
939static enum sof_comp_type find_process_comp_type(enum sof_ipc_process_type type)
940{
941	int i;
942
943	for (i = 0; i < ARRAY_SIZE(sof_process); i++) {
944		if (sof_process[i].type == type)
945			return sof_process[i].comp_type;
946	}
947
948	return SOF_COMP_NONE;
949}
950
951/*
952 * Processing Component Topology - can be "effect", "codec", or general
953 * "processing".
954 */
955
956static int sof_widget_update_ipc_comp_process(struct snd_sof_widget *swidget)
957{
958	struct snd_soc_component *scomp = swidget->scomp;
959	struct sof_ipc_comp_process config;
960	int ret;
961
962	memset(&config, 0, sizeof(config));
963	config.comp.core = swidget->core;
964
965	/* parse one set of process tokens */
966	ret = sof_update_ipc_object(scomp, &config, SOF_PROCESS_TOKENS, swidget->tuples,
967				    swidget->num_tuples, sizeof(config), 1);
968	if (ret < 0)
969		return ret;
970
971	/* now load process specific data and send IPC */
972	return sof_process_load(scomp, swidget, find_process_comp_type(config.type));
973}
974
975static int sof_link_hda_load(struct snd_soc_component *scomp, struct snd_sof_dai_link *slink,
976			     struct sof_ipc_dai_config *config, struct snd_sof_dai *dai)
977{
978	struct sof_dai_private_data *private = dai->private;
979	u32 size = sizeof(*config);
980	int ret;
981
982	/* init IPC */
983	memset(&config->hda, 0, sizeof(config->hda));
984	config->hdr.size = size;
985
986	/* parse one set of HDA tokens */
987	ret = sof_update_ipc_object(scomp, &config->hda, SOF_HDA_TOKENS, slink->tuples,
988				    slink->num_tuples, size, 1);
989	if (ret < 0)
990		return ret;
991
992	dev_dbg(scomp->dev, "HDA config rate %d channels %d\n",
993		config->hda.rate, config->hda.channels);
994
995	config->hda.link_dma_ch = DMA_CHAN_INVALID;
996
997	dai->number_configs = 1;
998	dai->current_config = 0;
999	private->dai_config = kmemdup(config, size, GFP_KERNEL);
1000	if (!private->dai_config)
1001		return -ENOMEM;
1002
1003	return 0;
1004}
1005
1006static void sof_dai_set_format(struct snd_soc_tplg_hw_config *hw_config,
1007			       struct sof_ipc_dai_config *config)
1008{
1009	/* clock directions wrt codec */
1010	config->format &= ~SOF_DAI_FMT_CLOCK_PROVIDER_MASK;
1011	if (hw_config->bclk_provider == SND_SOC_TPLG_BCLK_CP) {
1012		/* codec is bclk provider */
1013		if (hw_config->fsync_provider == SND_SOC_TPLG_FSYNC_CP)
1014			config->format |= SOF_DAI_FMT_CBP_CFP;
1015		else
1016			config->format |= SOF_DAI_FMT_CBP_CFC;
1017	} else {
1018		/* codec is bclk consumer */
1019		if (hw_config->fsync_provider == SND_SOC_TPLG_FSYNC_CP)
1020			config->format |= SOF_DAI_FMT_CBC_CFP;
1021		else
1022			config->format |= SOF_DAI_FMT_CBC_CFC;
1023	}
1024
1025	/* inverted clocks ? */
1026	config->format &= ~SOF_DAI_FMT_INV_MASK;
1027	if (hw_config->invert_bclk) {
1028		if (hw_config->invert_fsync)
1029			config->format |= SOF_DAI_FMT_IB_IF;
1030		else
1031			config->format |= SOF_DAI_FMT_IB_NF;
1032	} else {
1033		if (hw_config->invert_fsync)
1034			config->format |= SOF_DAI_FMT_NB_IF;
1035		else
1036			config->format |= SOF_DAI_FMT_NB_NF;
1037	}
1038}
1039
1040static int sof_link_sai_load(struct snd_soc_component *scomp, struct snd_sof_dai_link *slink,
1041			     struct sof_ipc_dai_config *config, struct snd_sof_dai *dai)
1042{
1043	struct snd_soc_tplg_hw_config *hw_config = slink->hw_configs;
1044	struct sof_dai_private_data *private = dai->private;
1045	u32 size = sizeof(*config);
1046	int ret;
1047
1048	/* handle master/slave and inverted clocks */
1049	sof_dai_set_format(hw_config, config);
1050
1051	/* init IPC */
1052	memset(&config->sai, 0, sizeof(config->sai));
1053	config->hdr.size = size;
1054
1055	/* parse one set of SAI tokens */
1056	ret = sof_update_ipc_object(scomp, &config->sai, SOF_SAI_TOKENS, slink->tuples,
1057				    slink->num_tuples, size, 1);
1058	if (ret < 0)
1059		return ret;
1060
1061	config->sai.mclk_rate = le32_to_cpu(hw_config->mclk_rate);
1062	config->sai.bclk_rate = le32_to_cpu(hw_config->bclk_rate);
1063	config->sai.fsync_rate = le32_to_cpu(hw_config->fsync_rate);
1064	config->sai.mclk_direction = hw_config->mclk_direction;
1065
1066	config->sai.tdm_slots = le32_to_cpu(hw_config->tdm_slots);
1067	config->sai.tdm_slot_width = le32_to_cpu(hw_config->tdm_slot_width);
1068	config->sai.rx_slots = le32_to_cpu(hw_config->rx_slots);
1069	config->sai.tx_slots = le32_to_cpu(hw_config->tx_slots);
1070
1071	dev_info(scomp->dev,
1072		 "tplg: config SAI%d fmt 0x%x mclk %d width %d slots %d mclk id %d\n",
1073		config->dai_index, config->format,
1074		config->sai.mclk_rate, config->sai.tdm_slot_width,
1075		config->sai.tdm_slots, config->sai.mclk_id);
1076
1077	if (config->sai.tdm_slots < 1 || config->sai.tdm_slots > 8) {
1078		dev_err(scomp->dev, "Invalid channel count for SAI%d\n", config->dai_index);
1079		return -EINVAL;
1080	}
1081
1082	dai->number_configs = 1;
1083	dai->current_config = 0;
1084	private->dai_config = kmemdup(config, size, GFP_KERNEL);
1085	if (!private->dai_config)
1086		return -ENOMEM;
1087
1088	return 0;
1089}
1090
1091static int sof_link_esai_load(struct snd_soc_component *scomp, struct snd_sof_dai_link *slink,
1092			      struct sof_ipc_dai_config *config, struct snd_sof_dai *dai)
1093{
1094	struct snd_soc_tplg_hw_config *hw_config = slink->hw_configs;
1095	struct sof_dai_private_data *private = dai->private;
1096	u32 size = sizeof(*config);
1097	int ret;
1098
1099	/* handle master/slave and inverted clocks */
1100	sof_dai_set_format(hw_config, config);
1101
1102	/* init IPC */
1103	memset(&config->esai, 0, sizeof(config->esai));
1104	config->hdr.size = size;
1105
1106	/* parse one set of ESAI tokens */
1107	ret = sof_update_ipc_object(scomp, &config->esai, SOF_ESAI_TOKENS, slink->tuples,
1108				    slink->num_tuples, size, 1);
1109	if (ret < 0)
1110		return ret;
1111
1112	config->esai.mclk_rate = le32_to_cpu(hw_config->mclk_rate);
1113	config->esai.bclk_rate = le32_to_cpu(hw_config->bclk_rate);
1114	config->esai.fsync_rate = le32_to_cpu(hw_config->fsync_rate);
1115	config->esai.mclk_direction = hw_config->mclk_direction;
1116	config->esai.tdm_slots = le32_to_cpu(hw_config->tdm_slots);
1117	config->esai.tdm_slot_width = le32_to_cpu(hw_config->tdm_slot_width);
1118	config->esai.rx_slots = le32_to_cpu(hw_config->rx_slots);
1119	config->esai.tx_slots = le32_to_cpu(hw_config->tx_slots);
1120
1121	dev_info(scomp->dev,
1122		 "tplg: config ESAI%d fmt 0x%x mclk %d width %d slots %d mclk id %d\n",
1123		config->dai_index, config->format,
1124		config->esai.mclk_rate, config->esai.tdm_slot_width,
1125		config->esai.tdm_slots, config->esai.mclk_id);
1126
1127	if (config->esai.tdm_slots < 1 || config->esai.tdm_slots > 8) {
1128		dev_err(scomp->dev, "Invalid channel count for ESAI%d\n", config->dai_index);
1129		return -EINVAL;
1130	}
1131
1132	dai->number_configs = 1;
1133	dai->current_config = 0;
1134	private->dai_config = kmemdup(config, size, GFP_KERNEL);
1135	if (!private->dai_config)
1136		return -ENOMEM;
1137
1138	return 0;
1139}
1140
1141static int sof_link_acp_dmic_load(struct snd_soc_component *scomp, struct snd_sof_dai_link *slink,
1142				  struct sof_ipc_dai_config *config, struct snd_sof_dai *dai)
1143{
1144	struct snd_soc_tplg_hw_config *hw_config = slink->hw_configs;
1145	struct sof_dai_private_data *private = dai->private;
1146	u32 size = sizeof(*config);
1147	int ret;
1148
1149       /* handle master/slave and inverted clocks */
1150	sof_dai_set_format(hw_config, config);
1151
1152	config->hdr.size = size;
1153
1154	/* parse the required set of ACPDMIC tokens based on num_hw_cfgs */
1155	ret = sof_update_ipc_object(scomp, &config->acpdmic, SOF_ACPDMIC_TOKENS, slink->tuples,
1156				    slink->num_tuples, size, slink->num_hw_configs);
1157	if (ret < 0)
1158		return ret;
1159
1160	dev_info(scomp->dev, "ACP_DMIC config ACP%d channel %d rate %d\n",
1161		 config->dai_index, config->acpdmic.pdm_ch,
1162		 config->acpdmic.pdm_rate);
1163
1164	dai->number_configs = 1;
1165	dai->current_config = 0;
1166	private->dai_config = kmemdup(config, size, GFP_KERNEL);
1167	if (!private->dai_config)
1168		return -ENOMEM;
1169
1170	return 0;
1171}
1172
1173static int sof_link_acp_bt_load(struct snd_soc_component *scomp, struct snd_sof_dai_link *slink,
1174				struct sof_ipc_dai_config *config, struct snd_sof_dai *dai)
1175{
1176	struct snd_soc_tplg_hw_config *hw_config = slink->hw_configs;
1177	struct sof_dai_private_data *private = dai->private;
1178	u32 size = sizeof(*config);
1179
1180	/* handle master/slave and inverted clocks */
1181	sof_dai_set_format(hw_config, config);
1182
1183	/* init IPC */
1184	memset(&config->acpbt, 0, sizeof(config->acpbt));
1185	config->hdr.size = size;
1186
1187	config->acpbt.fsync_rate = le32_to_cpu(hw_config->fsync_rate);
1188	config->acpbt.tdm_slots = le32_to_cpu(hw_config->tdm_slots);
1189
1190	dev_info(scomp->dev, "ACP_BT config ACP%d channel %d rate %d\n",
1191		 config->dai_index, config->acpbt.tdm_slots,
1192		 config->acpbt.fsync_rate);
1193
1194	dai->number_configs = 1;
1195	dai->current_config = 0;
1196	private->dai_config = kmemdup(config, size, GFP_KERNEL);
1197	if (!private->dai_config)
1198		return -ENOMEM;
1199
1200	return 0;
1201}
1202
1203static int sof_link_acp_sp_load(struct snd_soc_component *scomp, struct snd_sof_dai_link *slink,
1204				struct sof_ipc_dai_config *config, struct snd_sof_dai *dai)
1205{
1206	struct snd_soc_tplg_hw_config *hw_config = slink->hw_configs;
1207	struct sof_dai_private_data *private = dai->private;
1208	u32 size = sizeof(*config);
1209	int ret;
1210
1211	/* handle master/slave and inverted clocks */
1212	sof_dai_set_format(hw_config, config);
1213
1214	/* init IPC */
1215	memset(&config->acpsp, 0, sizeof(config->acpsp));
1216	config->hdr.size = size;
1217
1218	ret = sof_update_ipc_object(scomp, &config->acpsp, SOF_ACPI2S_TOKENS, slink->tuples,
1219				    slink->num_tuples, size, slink->num_hw_configs);
1220	if (ret < 0)
1221		return ret;
1222
1223
1224	dev_info(scomp->dev, "ACP_SP config ACP%d channel %d rate %d tdm_mode %d\n",
1225		 config->dai_index, config->acpsp.tdm_slots,
1226		 config->acpsp.fsync_rate, config->acpsp.tdm_mode);
1227
1228	dai->number_configs = 1;
1229	dai->current_config = 0;
1230	private->dai_config = kmemdup(config, size, GFP_KERNEL);
1231	if (!private->dai_config)
1232		return -ENOMEM;
1233
1234	return 0;
1235}
1236
1237static int sof_link_acp_hs_load(struct snd_soc_component *scomp, struct snd_sof_dai_link *slink,
1238				struct sof_ipc_dai_config *config, struct snd_sof_dai *dai)
1239{
1240	struct snd_soc_tplg_hw_config *hw_config = slink->hw_configs;
1241	struct sof_dai_private_data *private = dai->private;
1242	u32 size = sizeof(*config);
1243	int ret;
1244
1245	/* Configures the DAI hardware format and inverted clocks */
1246	sof_dai_set_format(hw_config, config);
1247
1248	/* init IPC */
1249	memset(&config->acphs, 0, sizeof(config->acphs));
1250	config->hdr.size = size;
1251
1252	ret = sof_update_ipc_object(scomp, &config->acphs, SOF_ACPI2S_TOKENS, slink->tuples,
1253				    slink->num_tuples, size, slink->num_hw_configs);
1254	if (ret < 0)
1255		return ret;
1256
1257	dev_info(scomp->dev, "ACP_HS config ACP%d channel %d rate %d tdm_mode %d\n",
1258		 config->dai_index, config->acphs.tdm_slots,
1259		 config->acphs.fsync_rate, config->acphs.tdm_mode);
1260
1261	dai->number_configs = 1;
1262	dai->current_config = 0;
1263	private->dai_config = kmemdup(config, size, GFP_KERNEL);
1264	if (!private->dai_config)
1265		return -ENOMEM;
1266
1267	return 0;
1268}
1269
1270static int sof_link_afe_load(struct snd_soc_component *scomp, struct snd_sof_dai_link *slink,
1271			     struct sof_ipc_dai_config *config, struct snd_sof_dai *dai)
1272{
1273	struct sof_dai_private_data *private = dai->private;
1274	u32 size = sizeof(*config);
1275	int ret;
1276
1277	config->hdr.size = size;
1278
1279	/* parse the required set of AFE tokens based on num_hw_cfgs */
1280	ret = sof_update_ipc_object(scomp, &config->afe, SOF_AFE_TOKENS, slink->tuples,
1281				    slink->num_tuples, size, slink->num_hw_configs);
1282	if (ret < 0)
1283		return ret;
1284
1285	dev_dbg(scomp->dev, "AFE config rate %d channels %d format:%d\n",
1286		config->afe.rate, config->afe.channels, config->afe.format);
1287
1288	config->afe.stream_id = DMA_CHAN_INVALID;
1289
1290	dai->number_configs = 1;
1291	dai->current_config = 0;
1292	private->dai_config = kmemdup(config, size, GFP_KERNEL);
1293	if (!private->dai_config)
1294		return -ENOMEM;
1295
1296	return 0;
1297}
1298
1299static int sof_link_ssp_load(struct snd_soc_component *scomp, struct snd_sof_dai_link *slink,
1300			     struct sof_ipc_dai_config *config, struct snd_sof_dai *dai)
1301{
1302	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1303	struct snd_soc_tplg_hw_config *hw_config = slink->hw_configs;
1304	struct sof_dai_private_data *private = dai->private;
1305	u32 size = sizeof(*config);
1306	int current_config = 0;
1307	int i, ret;
1308
1309	/*
1310	 * Parse common data, we should have 1 common data per hw_config.
1311	 */
1312	ret = sof_update_ipc_object(scomp, &config->ssp, SOF_SSP_TOKENS, slink->tuples,
1313				    slink->num_tuples, size, slink->num_hw_configs);
1314	if (ret < 0)
1315		return ret;
1316
1317	/* process all possible hw configs */
1318	for (i = 0; i < slink->num_hw_configs; i++) {
1319		if (le32_to_cpu(hw_config[i].id) == slink->default_hw_cfg_id)
1320			current_config = i;
1321
1322		/* handle master/slave and inverted clocks */
1323		sof_dai_set_format(&hw_config[i], &config[i]);
1324
1325		config[i].hdr.size = size;
1326
1327		if (sdev->mclk_id_override) {
1328			dev_dbg(scomp->dev, "tplg: overriding topology mclk_id %d by quirk %d\n",
1329				config[i].ssp.mclk_id, sdev->mclk_id_quirk);
1330			config[i].ssp.mclk_id = sdev->mclk_id_quirk;
1331		}
1332
1333		/* copy differentiating hw configs to ipc structs */
1334		config[i].ssp.mclk_rate = le32_to_cpu(hw_config[i].mclk_rate);
1335		config[i].ssp.bclk_rate = le32_to_cpu(hw_config[i].bclk_rate);
1336		config[i].ssp.fsync_rate = le32_to_cpu(hw_config[i].fsync_rate);
1337		config[i].ssp.tdm_slots = le32_to_cpu(hw_config[i].tdm_slots);
1338		config[i].ssp.tdm_slot_width = le32_to_cpu(hw_config[i].tdm_slot_width);
1339		config[i].ssp.mclk_direction = hw_config[i].mclk_direction;
1340		config[i].ssp.rx_slots = le32_to_cpu(hw_config[i].rx_slots);
1341		config[i].ssp.tx_slots = le32_to_cpu(hw_config[i].tx_slots);
1342
1343		dev_dbg(scomp->dev, "tplg: config SSP%d fmt %#x mclk %d bclk %d fclk %d width (%d)%d slots %d mclk id %d quirks %d clks_control %#x\n",
1344			config[i].dai_index, config[i].format,
1345			config[i].ssp.mclk_rate, config[i].ssp.bclk_rate,
1346			config[i].ssp.fsync_rate, config[i].ssp.sample_valid_bits,
1347			config[i].ssp.tdm_slot_width, config[i].ssp.tdm_slots,
1348			config[i].ssp.mclk_id, config[i].ssp.quirks, config[i].ssp.clks_control);
1349
1350		/* validate SSP fsync rate and channel count */
1351		if (config[i].ssp.fsync_rate < 8000 || config[i].ssp.fsync_rate > 192000) {
1352			dev_err(scomp->dev, "Invalid fsync rate for SSP%d\n", config[i].dai_index);
1353			return -EINVAL;
1354		}
1355
1356		if (config[i].ssp.tdm_slots < 1 || config[i].ssp.tdm_slots > 8) {
1357			dev_err(scomp->dev, "Invalid channel count for SSP%d\n",
1358				config[i].dai_index);
1359			return -EINVAL;
1360		}
1361	}
1362
1363	dai->number_configs = slink->num_hw_configs;
1364	dai->current_config = current_config;
1365	private->dai_config = kmemdup(config, size * slink->num_hw_configs, GFP_KERNEL);
1366	if (!private->dai_config)
1367		return -ENOMEM;
1368
1369	return 0;
1370}
1371
1372static int sof_link_dmic_load(struct snd_soc_component *scomp, struct snd_sof_dai_link *slink,
1373			      struct sof_ipc_dai_config *config, struct snd_sof_dai *dai)
1374{
1375	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1376	struct sof_dai_private_data *private = dai->private;
1377	struct sof_ipc_fw_ready *ready = &sdev->fw_ready;
1378	struct sof_ipc_fw_version *v = &ready->version;
1379	size_t size = sizeof(*config);
1380	int i, ret;
1381
1382	/* Ensure the entire DMIC config struct is zeros */
1383	memset(&config->dmic, 0, sizeof(config->dmic));
1384
1385	/* parse the required set of DMIC tokens based on num_hw_cfgs */
1386	ret = sof_update_ipc_object(scomp, &config->dmic, SOF_DMIC_TOKENS, slink->tuples,
1387				    slink->num_tuples, size, slink->num_hw_configs);
1388	if (ret < 0)
1389		return ret;
1390
1391	/* parse the required set of DMIC PDM tokens based on number of active PDM's */
1392	ret = sof_update_ipc_object(scomp, &config->dmic.pdm[0], SOF_DMIC_PDM_TOKENS,
1393				    slink->tuples, slink->num_tuples,
1394				    sizeof(struct sof_ipc_dai_dmic_pdm_ctrl),
1395				    config->dmic.num_pdm_active);
1396	if (ret < 0)
1397		return ret;
1398
1399	/* set IPC header size */
1400	config->hdr.size = size;
1401
1402	/* debug messages */
1403	dev_dbg(scomp->dev, "tplg: config DMIC%d driver version %d\n",
1404		config->dai_index, config->dmic.driver_ipc_version);
1405	dev_dbg(scomp->dev, "pdmclk_min %d pdm_clkmax %d duty_min %d\n",
1406		config->dmic.pdmclk_min, config->dmic.pdmclk_max,
1407		config->dmic.duty_min);
1408	dev_dbg(scomp->dev, "duty_max %d fifo_fs %d num_pdms active %d\n",
1409		config->dmic.duty_max, config->dmic.fifo_fs,
1410		config->dmic.num_pdm_active);
1411	dev_dbg(scomp->dev, "fifo word length %d\n", config->dmic.fifo_bits);
1412
1413	for (i = 0; i < config->dmic.num_pdm_active; i++) {
1414		dev_dbg(scomp->dev, "pdm %d mic a %d mic b %d\n",
1415			config->dmic.pdm[i].id,
1416			config->dmic.pdm[i].enable_mic_a,
1417			config->dmic.pdm[i].enable_mic_b);
1418		dev_dbg(scomp->dev, "pdm %d polarity a %d polarity b %d\n",
1419			config->dmic.pdm[i].id,
1420			config->dmic.pdm[i].polarity_mic_a,
1421			config->dmic.pdm[i].polarity_mic_b);
1422		dev_dbg(scomp->dev, "pdm %d clk_edge %d skew %d\n",
1423			config->dmic.pdm[i].id,
1424			config->dmic.pdm[i].clk_edge,
1425			config->dmic.pdm[i].skew);
1426	}
1427
1428	/*
1429	 * this takes care of backwards compatible handling of fifo_bits_b.
1430	 * It is deprecated since firmware ABI version 3.0.1.
1431	 */
1432	if (SOF_ABI_VER(v->major, v->minor, v->micro) < SOF_ABI_VER(3, 0, 1))
1433		config->dmic.fifo_bits_b = config->dmic.fifo_bits;
1434
1435	dai->number_configs = 1;
1436	dai->current_config = 0;
1437	private->dai_config = kmemdup(config, size, GFP_KERNEL);
1438	if (!private->dai_config)
1439		return -ENOMEM;
1440
1441	return 0;
1442}
1443
1444static int sof_link_alh_load(struct snd_soc_component *scomp, struct snd_sof_dai_link *slink,
1445			     struct sof_ipc_dai_config *config, struct snd_sof_dai *dai)
1446{
1447	struct sof_dai_private_data *private = dai->private;
1448	u32 size = sizeof(*config);
1449	int ret;
1450
1451	/* parse the required set of ALH tokens based on num_hw_cfgs */
1452	ret = sof_update_ipc_object(scomp, &config->alh, SOF_ALH_TOKENS, slink->tuples,
1453				    slink->num_tuples, size, slink->num_hw_configs);
1454	if (ret < 0)
1455		return ret;
1456
1457	/* init IPC */
1458	config->hdr.size = size;
1459
1460	/* set config for all DAI's with name matching the link name */
1461	dai->number_configs = 1;
1462	dai->current_config = 0;
1463	private->dai_config = kmemdup(config, size, GFP_KERNEL);
1464	if (!private->dai_config)
1465		return -ENOMEM;
1466
1467	return 0;
1468}
1469
1470static int sof_ipc3_widget_setup_comp_dai(struct snd_sof_widget *swidget)
1471{
1472	struct snd_soc_component *scomp = swidget->scomp;
1473	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1474	struct snd_sof_dai *dai = swidget->private;
1475	struct sof_dai_private_data *private;
1476	struct sof_ipc_comp_dai *comp_dai;
1477	size_t ipc_size = sizeof(*comp_dai);
1478	struct sof_ipc_dai_config *config;
1479	struct snd_sof_dai_link *slink;
1480	int ret;
1481
1482	private = kzalloc(sizeof(*private), GFP_KERNEL);
1483	if (!private)
1484		return -ENOMEM;
1485
1486	dai->private = private;
1487
1488	private->comp_dai = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id);
1489	if (!private->comp_dai) {
1490		ret = -ENOMEM;
1491		goto free;
1492	}
1493
1494	/* configure dai IPC message */
1495	comp_dai = private->comp_dai;
1496	comp_dai->comp.type = SOF_COMP_DAI;
1497	comp_dai->config.hdr.size = sizeof(comp_dai->config);
1498
1499	/* parse one set of DAI tokens */
1500	ret = sof_update_ipc_object(scomp, comp_dai, SOF_DAI_TOKENS, swidget->tuples,
1501				    swidget->num_tuples, sizeof(*comp_dai), 1);
1502	if (ret < 0)
1503		goto free;
1504
1505	/* update comp_tokens */
1506	ret = sof_update_ipc_object(scomp, &comp_dai->config, SOF_COMP_TOKENS,
1507				    swidget->tuples, swidget->num_tuples,
1508				    sizeof(comp_dai->config), 1);
1509	if (ret < 0)
1510		goto free;
1511
1512	dev_dbg(scomp->dev, "dai %s: type %d index %d\n",
1513		swidget->widget->name, comp_dai->type, comp_dai->dai_index);
1514	sof_dbg_comp_config(scomp, &comp_dai->config);
1515
1516	/* now update DAI config */
1517	list_for_each_entry(slink, &sdev->dai_link_list, list) {
1518		struct sof_ipc_dai_config common_config;
1519		int i;
1520
1521		if (strcmp(slink->link->name, dai->name))
1522			continue;
1523
1524		/* Reserve memory for all hw configs, eventually freed by widget */
1525		config = kcalloc(slink->num_hw_configs, sizeof(*config), GFP_KERNEL);
1526		if (!config) {
1527			ret = -ENOMEM;
1528			goto free_comp;
1529		}
1530
1531		/* parse one set of DAI link tokens */
1532		ret = sof_update_ipc_object(scomp, &common_config, SOF_DAI_LINK_TOKENS,
1533					    slink->tuples, slink->num_tuples,
1534					    sizeof(common_config), 1);
1535		if (ret < 0)
1536			goto free_config;
1537
1538		for (i = 0; i < slink->num_hw_configs; i++) {
1539			config[i].hdr.cmd = SOF_IPC_GLB_DAI_MSG | SOF_IPC_DAI_CONFIG;
1540			config[i].format = le32_to_cpu(slink->hw_configs[i].fmt);
1541			config[i].type = common_config.type;
1542			config[i].dai_index = comp_dai->dai_index;
1543		}
1544
1545		switch (common_config.type) {
1546		case SOF_DAI_INTEL_SSP:
1547			ret = sof_link_ssp_load(scomp, slink, config, dai);
1548			break;
1549		case SOF_DAI_INTEL_DMIC:
1550			ret = sof_link_dmic_load(scomp, slink, config, dai);
1551			break;
1552		case SOF_DAI_INTEL_HDA:
1553			ret = sof_link_hda_load(scomp, slink, config, dai);
1554			break;
1555		case SOF_DAI_INTEL_ALH:
1556			ret = sof_link_alh_load(scomp, slink, config, dai);
1557			break;
1558		case SOF_DAI_IMX_SAI:
1559			ret = sof_link_sai_load(scomp, slink, config, dai);
1560			break;
1561		case SOF_DAI_IMX_ESAI:
1562			ret = sof_link_esai_load(scomp, slink, config, dai);
1563			break;
1564		case SOF_DAI_AMD_BT:
1565			ret = sof_link_acp_bt_load(scomp, slink, config, dai);
1566			break;
1567		case SOF_DAI_AMD_SP:
1568		case SOF_DAI_AMD_SP_VIRTUAL:
1569			ret = sof_link_acp_sp_load(scomp, slink, config, dai);
1570			break;
1571		case SOF_DAI_AMD_HS:
1572		case SOF_DAI_AMD_HS_VIRTUAL:
1573			ret = sof_link_acp_hs_load(scomp, slink, config, dai);
1574			break;
1575		case SOF_DAI_AMD_DMIC:
1576			ret = sof_link_acp_dmic_load(scomp, slink, config, dai);
1577			break;
1578		case SOF_DAI_MEDIATEK_AFE:
1579			ret = sof_link_afe_load(scomp, slink, config, dai);
1580			break;
1581		default:
1582			break;
1583		}
1584		if (ret < 0) {
1585			dev_err(scomp->dev, "failed to load config for dai %s\n", dai->name);
1586			goto free_config;
1587		}
1588
1589		kfree(config);
1590	}
1591
1592	return 0;
1593free_config:
1594	kfree(config);
1595free_comp:
1596	kfree(comp_dai);
1597free:
1598	kfree(private);
1599	dai->private = NULL;
1600	return ret;
1601}
1602
1603static void sof_ipc3_widget_free_comp_dai(struct snd_sof_widget *swidget)
1604{
1605	switch (swidget->id) {
1606	case snd_soc_dapm_dai_in:
1607	case snd_soc_dapm_dai_out:
1608	{
1609		struct snd_sof_dai *dai = swidget->private;
1610		struct sof_dai_private_data *dai_data;
1611
1612		if (!dai)
1613			return;
1614
1615		dai_data = dai->private;
1616		if (dai_data) {
1617			kfree(dai_data->comp_dai);
1618			kfree(dai_data->dai_config);
1619			kfree(dai_data);
1620		}
1621		kfree(dai);
1622		break;
1623	}
1624	default:
1625		break;
1626	}
1627}
1628
1629static int sof_ipc3_route_setup(struct snd_sof_dev *sdev, struct snd_sof_route *sroute)
1630{
1631	struct sof_ipc_pipe_comp_connect connect;
1632	int ret;
1633
1634	connect.hdr.size = sizeof(connect);
1635	connect.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_CONNECT;
1636	connect.source_id = sroute->src_widget->comp_id;
1637	connect.sink_id = sroute->sink_widget->comp_id;
1638
1639	dev_dbg(sdev->dev, "setting up route %s -> %s\n",
1640		sroute->src_widget->widget->name,
1641		sroute->sink_widget->widget->name);
1642
1643	/* send ipc */
1644	ret = sof_ipc_tx_message_no_reply(sdev->ipc, &connect, sizeof(connect));
1645	if (ret < 0)
1646		dev_err(sdev->dev, "%s: route %s -> %s failed\n", __func__,
1647			sroute->src_widget->widget->name, sroute->sink_widget->widget->name);
1648
1649	return ret;
1650}
1651
1652static int sof_ipc3_control_load_bytes(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol)
1653{
1654	struct sof_ipc_ctrl_data *cdata;
1655	size_t priv_size_check;
1656	int ret;
1657
1658	if (scontrol->max_size < (sizeof(*cdata) + sizeof(struct sof_abi_hdr))) {
1659		dev_err(sdev->dev, "%s: insufficient size for a bytes control: %zu.\n",
1660			__func__, scontrol->max_size);
1661		return -EINVAL;
1662	}
1663
1664	if (scontrol->priv_size > scontrol->max_size - sizeof(*cdata)) {
1665		dev_err(sdev->dev,
1666			"%s: bytes data size %zu exceeds max %zu.\n", __func__,
1667			scontrol->priv_size, scontrol->max_size - sizeof(*cdata));
1668		return -EINVAL;
1669	}
1670
1671	scontrol->ipc_control_data = kzalloc(scontrol->max_size, GFP_KERNEL);
1672	if (!scontrol->ipc_control_data)
1673		return -ENOMEM;
1674
1675	scontrol->size = sizeof(struct sof_ipc_ctrl_data) + scontrol->priv_size;
1676
1677	cdata = scontrol->ipc_control_data;
1678	cdata->cmd = SOF_CTRL_CMD_BINARY;
1679	cdata->index = scontrol->index;
1680
1681	if (scontrol->priv_size > 0) {
1682		memcpy(cdata->data, scontrol->priv, scontrol->priv_size);
1683		kfree(scontrol->priv);
1684		scontrol->priv = NULL;
1685
1686		if (cdata->data->magic != SOF_ABI_MAGIC) {
1687			dev_err(sdev->dev, "Wrong ABI magic 0x%08x.\n", cdata->data->magic);
1688			ret = -EINVAL;
1689			goto err;
1690		}
1691
1692		if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, cdata->data->abi)) {
1693			dev_err(sdev->dev, "Incompatible ABI version 0x%08x.\n",
1694				cdata->data->abi);
1695			ret = -EINVAL;
1696			goto err;
1697		}
1698
1699		priv_size_check = cdata->data->size + sizeof(struct sof_abi_hdr);
1700		if (priv_size_check != scontrol->priv_size) {
1701			dev_err(sdev->dev, "Conflict in bytes (%zu) vs. priv size (%zu).\n",
1702				priv_size_check, scontrol->priv_size);
1703			ret = -EINVAL;
1704			goto err;
1705		}
1706	}
1707
1708	return 0;
1709err:
1710	kfree(scontrol->ipc_control_data);
1711	scontrol->ipc_control_data = NULL;
1712	return ret;
1713}
1714
1715static int sof_ipc3_control_load_volume(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol)
1716{
1717	struct sof_ipc_ctrl_data *cdata;
1718	int i;
1719
1720	/* init the volume get/put data */
1721	scontrol->size = struct_size(cdata, chanv, scontrol->num_channels);
1722
1723	scontrol->ipc_control_data = kzalloc(scontrol->size, GFP_KERNEL);
1724	if (!scontrol->ipc_control_data)
1725		return -ENOMEM;
1726
1727	cdata = scontrol->ipc_control_data;
1728	cdata->index = scontrol->index;
1729
1730	/* set cmd for mixer control */
1731	if (scontrol->max == 1) {
1732		cdata->cmd = SOF_CTRL_CMD_SWITCH;
1733		return 0;
1734	}
1735
1736	cdata->cmd = SOF_CTRL_CMD_VOLUME;
1737
1738	/* set default volume values to 0dB in control */
1739	for (i = 0; i < scontrol->num_channels; i++) {
1740		cdata->chanv[i].channel = i;
1741		cdata->chanv[i].value = VOL_ZERO_DB;
1742	}
1743
1744	return 0;
1745}
1746
1747static int sof_ipc3_control_load_enum(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol)
1748{
1749	struct sof_ipc_ctrl_data *cdata;
1750
1751	/* init the enum get/put data */
1752	scontrol->size = struct_size(cdata, chanv, scontrol->num_channels);
1753
1754	scontrol->ipc_control_data = kzalloc(scontrol->size, GFP_KERNEL);
1755	if (!scontrol->ipc_control_data)
1756		return -ENOMEM;
1757
1758	cdata = scontrol->ipc_control_data;
1759	cdata->index = scontrol->index;
1760	cdata->cmd = SOF_CTRL_CMD_ENUM;
1761
1762	return 0;
1763}
1764
1765static int sof_ipc3_control_setup(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol)
1766{
1767	switch (scontrol->info_type) {
1768	case SND_SOC_TPLG_CTL_VOLSW:
1769	case SND_SOC_TPLG_CTL_VOLSW_SX:
1770	case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1771		return sof_ipc3_control_load_volume(sdev, scontrol);
1772	case SND_SOC_TPLG_CTL_BYTES:
1773		return sof_ipc3_control_load_bytes(sdev, scontrol);
1774	case SND_SOC_TPLG_CTL_ENUM:
1775	case SND_SOC_TPLG_CTL_ENUM_VALUE:
1776		return sof_ipc3_control_load_enum(sdev, scontrol);
1777	default:
1778		break;
1779	}
1780
1781	return 0;
1782}
1783
1784static int sof_ipc3_control_free(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol)
1785{
1786	struct sof_ipc_free fcomp;
1787
1788	fcomp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_FREE;
1789	fcomp.hdr.size = sizeof(fcomp);
1790	fcomp.id = scontrol->comp_id;
1791
1792	/* send IPC to the DSP */
1793	return sof_ipc_tx_message_no_reply(sdev->ipc, &fcomp, sizeof(fcomp));
1794}
1795
1796/* send pcm params ipc */
1797static int sof_ipc3_keyword_detect_pcm_params(struct snd_sof_widget *swidget, int dir)
1798{
1799	struct snd_soc_component *scomp = swidget->scomp;
1800	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1801	struct snd_pcm_hw_params *params;
1802	struct sof_ipc_pcm_params pcm;
1803	struct snd_sof_pcm *spcm;
1804	int ret;
1805
1806	/* get runtime PCM params using widget's stream name */
1807	spcm = snd_sof_find_spcm_name(scomp, swidget->widget->sname);
1808	if (!spcm) {
1809		dev_err(scomp->dev, "Cannot find PCM for %s\n", swidget->widget->name);
1810		return -EINVAL;
1811	}
1812
1813	params = &spcm->params[dir];
1814
1815	/* set IPC PCM params */
1816	memset(&pcm, 0, sizeof(pcm));
1817	pcm.hdr.size = sizeof(pcm);
1818	pcm.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS;
1819	pcm.comp_id = swidget->comp_id;
1820	pcm.params.hdr.size = sizeof(pcm.params);
1821	pcm.params.direction = dir;
1822	pcm.params.sample_valid_bytes = params_width(params) >> 3;
1823	pcm.params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED;
1824	pcm.params.rate = params_rate(params);
1825	pcm.params.channels = params_channels(params);
1826	pcm.params.host_period_bytes = params_period_bytes(params);
1827
1828	/* set format */
1829	switch (params_format(params)) {
1830	case SNDRV_PCM_FORMAT_S16:
1831		pcm.params.frame_fmt = SOF_IPC_FRAME_S16_LE;
1832		break;
1833	case SNDRV_PCM_FORMAT_S24:
1834		pcm.params.frame_fmt = SOF_IPC_FRAME_S24_4LE;
1835		break;
1836	case SNDRV_PCM_FORMAT_S32:
1837		pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE;
1838		break;
1839	default:
1840		return -EINVAL;
1841	}
1842
1843	/* send IPC to the DSP */
1844	ret = sof_ipc_tx_message_no_reply(sdev->ipc, &pcm, sizeof(pcm));
1845	if (ret < 0)
1846		dev_err(scomp->dev, "%s: PCM params failed for %s\n", __func__,
1847			swidget->widget->name);
1848
1849	return ret;
1850}
1851
1852 /* send stream trigger ipc */
1853static int sof_ipc3_keyword_detect_trigger(struct snd_sof_widget *swidget, int cmd)
1854{
1855	struct snd_soc_component *scomp = swidget->scomp;
1856	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1857	struct sof_ipc_stream stream;
1858	int ret;
1859
1860	/* set IPC stream params */
1861	stream.hdr.size = sizeof(stream);
1862	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | cmd;
1863	stream.comp_id = swidget->comp_id;
1864
1865	/* send IPC to the DSP */
1866	ret = sof_ipc_tx_message_no_reply(sdev->ipc, &stream, sizeof(stream));
1867	if (ret < 0)
1868		dev_err(scomp->dev, "%s: Failed to trigger %s\n", __func__, swidget->widget->name);
1869
1870	return ret;
1871}
1872
1873static int sof_ipc3_keyword_dapm_event(struct snd_soc_dapm_widget *w,
1874				       struct snd_kcontrol *k, int event)
1875{
1876	struct snd_sof_widget *swidget = w->dobj.private;
1877	struct snd_soc_component *scomp;
1878	int stream = SNDRV_PCM_STREAM_CAPTURE;
1879	struct snd_sof_pcm *spcm;
1880	int ret = 0;
1881
1882	if (!swidget)
1883		return 0;
1884
1885	scomp = swidget->scomp;
1886
1887	dev_dbg(scomp->dev, "received event %d for widget %s\n",
1888		event, w->name);
1889
1890	/* get runtime PCM params using widget's stream name */
1891	spcm = snd_sof_find_spcm_name(scomp, swidget->widget->sname);
1892	if (!spcm) {
1893		dev_err(scomp->dev, "%s: Cannot find PCM for %s\n", __func__,
1894			swidget->widget->name);
1895		return -EINVAL;
1896	}
1897
1898	/* process events */
1899	switch (event) {
1900	case SND_SOC_DAPM_PRE_PMU:
1901		if (spcm->stream[stream].suspend_ignored) {
1902			dev_dbg(scomp->dev, "PRE_PMU event ignored, KWD pipeline is already RUNNING\n");
1903			return 0;
1904		}
1905
1906		/* set pcm params */
1907		ret = sof_ipc3_keyword_detect_pcm_params(swidget, stream);
1908		if (ret < 0) {
1909			dev_err(scomp->dev, "%s: Failed to set pcm params for widget %s\n",
1910				__func__, swidget->widget->name);
1911			break;
1912		}
1913
1914		/* start trigger */
1915		ret = sof_ipc3_keyword_detect_trigger(swidget, SOF_IPC_STREAM_TRIG_START);
1916		if (ret < 0)
1917			dev_err(scomp->dev, "%s: Failed to trigger widget %s\n", __func__,
1918				swidget->widget->name);
1919		break;
1920	case SND_SOC_DAPM_POST_PMD:
1921		if (spcm->stream[stream].suspend_ignored) {
1922			dev_dbg(scomp->dev,
1923				"POST_PMD event ignored, KWD pipeline will remain RUNNING\n");
1924			return 0;
1925		}
1926
1927		/* stop trigger */
1928		ret = sof_ipc3_keyword_detect_trigger(swidget, SOF_IPC_STREAM_TRIG_STOP);
1929		if (ret < 0)
1930			dev_err(scomp->dev, "%s: Failed to trigger widget %s\n", __func__,
1931				swidget->widget->name);
1932
1933		/* pcm free */
1934		ret = sof_ipc3_keyword_detect_trigger(swidget, SOF_IPC_STREAM_PCM_FREE);
1935		if (ret < 0)
1936			dev_err(scomp->dev, "%s: Failed to free PCM for widget %s\n", __func__,
1937				swidget->widget->name);
1938		break;
1939	default:
1940		break;
1941	}
1942
1943	return ret;
1944}
1945
1946/* event handlers for keyword detect component */
1947static const struct snd_soc_tplg_widget_events sof_kwd_events[] = {
1948	{SOF_KEYWORD_DETECT_DAPM_EVENT, sof_ipc3_keyword_dapm_event},
1949};
1950
1951static int sof_ipc3_widget_bind_event(struct snd_soc_component *scomp,
1952				      struct snd_sof_widget *swidget, u16 event_type)
1953{
1954	struct sof_ipc_comp *ipc_comp;
1955
1956	/* validate widget event type */
1957	switch (event_type) {
1958	case SOF_KEYWORD_DETECT_DAPM_EVENT:
1959		/* only KEYWORD_DETECT comps should handle this */
1960		if (swidget->id != snd_soc_dapm_effect)
1961			break;
1962
1963		ipc_comp = swidget->private;
1964		if (ipc_comp && ipc_comp->type != SOF_COMP_KEYWORD_DETECT)
1965			break;
1966
1967		/* bind event to keyword detect comp */
1968		return snd_soc_tplg_widget_bind_event(swidget->widget, sof_kwd_events,
1969						      ARRAY_SIZE(sof_kwd_events), event_type);
1970	default:
1971		break;
1972	}
1973
1974	dev_err(scomp->dev, "Invalid event type %d for widget %s\n", event_type,
1975		swidget->widget->name);
1976
1977	return -EINVAL;
1978}
1979
1980static int sof_ipc3_complete_pipeline(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
1981{
1982	struct sof_ipc_pipe_ready ready;
1983	int ret;
1984
1985	dev_dbg(sdev->dev, "tplg: complete pipeline %s id %d\n",
1986		swidget->widget->name, swidget->comp_id);
1987
1988	memset(&ready, 0, sizeof(ready));
1989	ready.hdr.size = sizeof(ready);
1990	ready.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_PIPE_COMPLETE;
1991	ready.comp_id = swidget->comp_id;
1992
1993	ret = sof_ipc_tx_message_no_reply(sdev->ipc, &ready, sizeof(ready));
1994	if (ret < 0)
1995		return ret;
1996
1997	return 1;
1998}
1999
2000static int sof_ipc3_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
2001{
2002	struct sof_ipc_free ipc_free = {
2003		.hdr = {
2004			.size = sizeof(ipc_free),
2005			.cmd = SOF_IPC_GLB_TPLG_MSG,
2006		},
2007		.id = swidget->comp_id,
2008	};
2009	int ret;
2010
2011	if (!swidget->private)
2012		return 0;
2013
2014	switch (swidget->id) {
2015	case snd_soc_dapm_scheduler:
2016	{
2017		ipc_free.hdr.cmd |= SOF_IPC_TPLG_PIPE_FREE;
2018		break;
2019	}
2020	case snd_soc_dapm_buffer:
2021		ipc_free.hdr.cmd |= SOF_IPC_TPLG_BUFFER_FREE;
2022		break;
2023	default:
2024		ipc_free.hdr.cmd |= SOF_IPC_TPLG_COMP_FREE;
2025		break;
2026	}
2027
2028	ret = sof_ipc_tx_message_no_reply(sdev->ipc, &ipc_free, sizeof(ipc_free));
2029	if (ret < 0)
2030		dev_err(sdev->dev, "failed to free widget %s\n", swidget->widget->name);
2031
2032	return ret;
2033}
2034
2035static int sof_ipc3_dai_config(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget,
2036			       unsigned int flags, struct snd_sof_dai_config_data *data)
2037{
2038	struct sof_ipc_fw_version *v = &sdev->fw_ready.version;
2039	struct snd_sof_dai *dai = swidget->private;
2040	struct sof_dai_private_data *private;
2041	struct sof_ipc_dai_config *config;
2042	int ret = 0;
2043
2044	if (!dai || !dai->private) {
2045		dev_err(sdev->dev, "No private data for DAI %s\n", swidget->widget->name);
2046		return -EINVAL;
2047	}
2048
2049	private = dai->private;
2050	if (!private->dai_config) {
2051		dev_err(sdev->dev, "No config for DAI %s\n", dai->name);
2052		return -EINVAL;
2053	}
2054
2055	config = &private->dai_config[dai->current_config];
2056	if (!config) {
2057		dev_err(sdev->dev, "Invalid current config for DAI %s\n", dai->name);
2058		return -EINVAL;
2059	}
2060
2061	switch (config->type) {
2062	case SOF_DAI_INTEL_SSP:
2063		/*
2064		 * DAI_CONFIG IPC during hw_params/hw_free for SSP DAI's is not supported in older
2065		 * firmware
2066		 */
2067		if (v->abi_version < SOF_ABI_VER(3, 18, 0) &&
2068		    ((flags & SOF_DAI_CONFIG_FLAGS_HW_PARAMS) ||
2069		     (flags & SOF_DAI_CONFIG_FLAGS_HW_FREE)))
2070			return 0;
2071		break;
2072	case SOF_DAI_INTEL_HDA:
2073		if (data)
2074			config->hda.link_dma_ch = data->dai_data;
2075		break;
2076	case SOF_DAI_INTEL_ALH:
2077		if (data) {
2078			/* save the dai_index during hw_params and reuse it for hw_free */
2079			if (flags & SOF_DAI_CONFIG_FLAGS_HW_PARAMS)
2080				config->dai_index = data->dai_index;
2081			config->alh.stream_id = data->dai_data;
2082		}
2083		break;
2084	default:
2085		break;
2086	}
2087
2088	/*
2089	 * The dai_config op is invoked several times and the flags argument varies as below:
2090	 * BE DAI hw_params: When the op is invoked during the BE DAI hw_params, flags contains
2091	 * SOF_DAI_CONFIG_FLAGS_HW_PARAMS along with quirks
2092	 * FE DAI hw_params: When invoked during FE DAI hw_params after the DAI widget has
2093	 * just been set up in the DSP, flags is set to SOF_DAI_CONFIG_FLAGS_HW_PARAMS with no
2094	 * quirks
2095	 * BE DAI trigger: When invoked during the BE DAI trigger, flags is set to
2096	 * SOF_DAI_CONFIG_FLAGS_PAUSE and contains no quirks
2097	 * BE DAI hw_free: When invoked during the BE DAI hw_free, flags is set to
2098	 * SOF_DAI_CONFIG_FLAGS_HW_FREE and contains no quirks
2099	 * FE DAI hw_free: When invoked during the FE DAI hw_free, flags is set to
2100	 * SOF_DAI_CONFIG_FLAGS_HW_FREE and contains no quirks
2101	 *
2102	 * The DAI_CONFIG IPC is sent to the DSP, only after the widget is set up during the FE
2103	 * DAI hw_params. But since the BE DAI hw_params precedes the FE DAI hw_params, the quirks
2104	 * need to be preserved when assigning the flags before sending the IPC.
2105	 * For the case of PAUSE/HW_FREE, since there are no quirks, flags can be used as is.
2106	 */
2107
2108	if (flags & SOF_DAI_CONFIG_FLAGS_HW_PARAMS) {
2109		/* Clear stale command */
2110		config->flags &= ~SOF_DAI_CONFIG_FLAGS_CMD_MASK;
2111		config->flags |= flags;
2112	} else {
2113		config->flags = flags;
2114	}
2115
2116	/* only send the IPC if the widget is set up in the DSP */
2117	if (swidget->use_count > 0) {
2118		ret = sof_ipc_tx_message_no_reply(sdev->ipc, config, config->hdr.size);
2119		if (ret < 0)
2120			dev_err(sdev->dev, "Failed to set dai config for %s\n", dai->name);
2121
2122		/* clear the flags once the IPC has been sent even if it fails */
2123		config->flags = SOF_DAI_CONFIG_FLAGS_NONE;
2124	}
2125
2126	return ret;
2127}
2128
2129static int sof_ipc3_widget_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
2130{
2131	int ret;
2132
2133	if (!swidget->private)
2134		return 0;
2135
2136	switch (swidget->id) {
2137	case snd_soc_dapm_dai_in:
2138	case snd_soc_dapm_dai_out:
2139	{
2140		struct snd_sof_dai *dai = swidget->private;
2141		struct sof_dai_private_data *dai_data = dai->private;
2142		struct sof_ipc_comp *comp = &dai_data->comp_dai->comp;
2143
2144		ret = sof_ipc_tx_message_no_reply(sdev->ipc, dai_data->comp_dai, comp->hdr.size);
2145		break;
2146	}
2147	case snd_soc_dapm_scheduler:
2148	{
2149		struct sof_ipc_pipe_new *pipeline;
2150
2151		pipeline = swidget->private;
2152		ret = sof_ipc_tx_message_no_reply(sdev->ipc, pipeline, sizeof(*pipeline));
2153		break;
2154	}
2155	default:
2156	{
2157		struct sof_ipc_cmd_hdr *hdr;
2158
2159		hdr = swidget->private;
2160		ret = sof_ipc_tx_message_no_reply(sdev->ipc, swidget->private, hdr->size);
2161		break;
2162	}
2163	}
2164	if (ret < 0)
2165		dev_err(sdev->dev, "Failed to setup widget %s\n", swidget->widget->name);
2166
2167	return ret;
2168}
2169
2170static int sof_ipc3_set_up_all_pipelines(struct snd_sof_dev *sdev, bool verify)
2171{
2172	struct sof_ipc_fw_version *v = &sdev->fw_ready.version;
2173	struct snd_sof_widget *swidget;
2174	struct snd_sof_route *sroute;
2175	int ret;
2176
2177	/* restore pipeline components */
2178	list_for_each_entry(swidget, &sdev->widget_list, list) {
2179		/* only set up the widgets belonging to static pipelines */
2180		if (!verify && swidget->dynamic_pipeline_widget)
2181			continue;
2182
2183		/*
2184		 * For older firmware, skip scheduler widgets in this loop,
2185		 * sof_widget_setup() will be called in the 'complete pipeline' loop
2186		 */
2187		if (v->abi_version < SOF_ABI_VER(3, 19, 0) &&
2188		    swidget->id == snd_soc_dapm_scheduler)
2189			continue;
2190
2191		/* update DAI config. The IPC will be sent in sof_widget_setup() */
2192		if (WIDGET_IS_DAI(swidget->id)) {
2193			struct snd_sof_dai *dai = swidget->private;
2194			struct sof_dai_private_data *private;
2195			struct sof_ipc_dai_config *config;
2196
2197			if (!dai || !dai->private)
2198				continue;
2199			private = dai->private;
2200			if (!private->dai_config)
2201				continue;
2202
2203			config = private->dai_config;
2204			/*
2205			 * The link DMA channel would be invalidated for running
2206			 * streams but not for streams that were in the PAUSED
2207			 * state during suspend. So invalidate it here before setting
2208			 * the dai config in the DSP.
2209			 */
2210			if (config->type == SOF_DAI_INTEL_HDA)
2211				config->hda.link_dma_ch = DMA_CHAN_INVALID;
2212		}
2213
2214		ret = sof_widget_setup(sdev, swidget);
2215		if (ret < 0)
2216			return ret;
2217	}
2218
2219	/* restore pipeline connections */
2220	list_for_each_entry(sroute, &sdev->route_list, list) {
2221		/* only set up routes belonging to static pipelines */
2222		if (!verify && (sroute->src_widget->dynamic_pipeline_widget ||
2223				sroute->sink_widget->dynamic_pipeline_widget))
2224			continue;
2225
2226		/*
2227		 * For virtual routes, both sink and source are not buffer. IPC3 only supports
2228		 * connections between a buffer and a component. Ignore the rest.
2229		 */
2230		if (sroute->src_widget->id != snd_soc_dapm_buffer &&
2231		    sroute->sink_widget->id != snd_soc_dapm_buffer)
2232			continue;
2233
2234		ret = sof_route_setup(sdev, sroute->src_widget->widget,
2235				      sroute->sink_widget->widget);
2236		if (ret < 0) {
2237			dev_err(sdev->dev, "%s: route set up failed\n", __func__);
2238			return ret;
2239		}
2240	}
2241
2242	/* complete pipeline */
2243	list_for_each_entry(swidget, &sdev->widget_list, list) {
2244		switch (swidget->id) {
2245		case snd_soc_dapm_scheduler:
2246			/* only complete static pipelines */
2247			if (!verify && swidget->dynamic_pipeline_widget)
2248				continue;
2249
2250			if (v->abi_version < SOF_ABI_VER(3, 19, 0)) {
2251				ret = sof_widget_setup(sdev, swidget);
2252				if (ret < 0)
2253					return ret;
2254			}
2255
2256			swidget->spipe->complete = sof_ipc3_complete_pipeline(sdev, swidget);
2257			if (swidget->spipe->complete < 0)
2258				return swidget->spipe->complete;
2259			break;
2260		default:
2261			break;
2262		}
2263	}
2264
2265	return 0;
2266}
2267
2268/*
2269 * Free the PCM, its associated widgets and set the prepared flag to false for all PCMs that
2270 * did not get suspended(ex: paused streams) so the widgets can be set up again during resume.
2271 */
2272static int sof_tear_down_left_over_pipelines(struct snd_sof_dev *sdev)
2273{
2274	struct snd_sof_widget *swidget;
2275	struct snd_sof_pcm *spcm;
2276	int dir, ret;
2277
2278	/*
2279	 * free all PCMs and their associated DAPM widgets if their connected DAPM widget
2280	 * list is not NULL. This should only be true for paused streams at this point.
2281	 * This is equivalent to the handling of FE DAI suspend trigger for running streams.
2282	 */
2283	list_for_each_entry(spcm, &sdev->pcm_list, list) {
2284		for_each_pcm_streams(dir) {
2285			struct snd_pcm_substream *substream = spcm->stream[dir].substream;
2286
2287			if (!substream || !substream->runtime || spcm->stream[dir].suspend_ignored)
2288				continue;
2289
2290			if (spcm->stream[dir].list) {
2291				ret = sof_pcm_stream_free(sdev, substream, spcm, dir, true);
2292				if (ret < 0)
2293					return ret;
2294			}
2295		}
2296	}
2297
2298	/*
2299	 * free any left over DAI widgets. This is equivalent to the handling of suspend trigger
2300	 * for the BE DAI for running streams.
2301	 */
2302	list_for_each_entry(swidget, &sdev->widget_list, list)
2303		if (WIDGET_IS_DAI(swidget->id) && swidget->use_count == 1) {
2304			ret = sof_widget_free(sdev, swidget);
2305			if (ret < 0)
2306				return ret;
2307		}
2308
2309	return 0;
2310}
2311
2312static int sof_ipc3_free_widgets_in_list(struct snd_sof_dev *sdev, bool include_scheduler,
2313					 bool *dyn_widgets, bool verify)
2314{
2315	struct sof_ipc_fw_version *v = &sdev->fw_ready.version;
2316	struct snd_sof_widget *swidget;
2317	int ret;
2318
2319	list_for_each_entry(swidget, &sdev->widget_list, list) {
2320		if (swidget->dynamic_pipeline_widget) {
2321			*dyn_widgets = true;
2322			continue;
2323		}
2324
2325		/* Do not free widgets for static pipelines with FW older than SOF2.2 */
2326		if (!verify && !swidget->dynamic_pipeline_widget &&
2327		    SOF_FW_VER(v->major, v->minor, v->micro) < SOF_FW_VER(2, 2, 0)) {
2328			mutex_lock(&swidget->setup_mutex);
2329			swidget->use_count = 0;
2330			mutex_unlock(&swidget->setup_mutex);
2331			if (swidget->spipe)
2332				swidget->spipe->complete = 0;
2333			continue;
2334		}
2335
2336		if (include_scheduler && swidget->id != snd_soc_dapm_scheduler)
2337			continue;
2338
2339		if (!include_scheduler && swidget->id == snd_soc_dapm_scheduler)
2340			continue;
2341
2342		ret = sof_widget_free(sdev, swidget);
2343		if (ret < 0)
2344			return ret;
2345	}
2346
2347	return 0;
2348}
2349
2350/*
2351 * For older firmware, this function doesn't free widgets for static pipelines during suspend.
2352 * It only resets use_count for all widgets.
2353 */
2354static int sof_ipc3_tear_down_all_pipelines(struct snd_sof_dev *sdev, bool verify)
2355{
2356	struct sof_ipc_fw_version *v = &sdev->fw_ready.version;
2357	struct snd_sof_widget *swidget;
2358	struct snd_sof_route *sroute;
2359	bool dyn_widgets = false;
2360	int ret;
2361
2362	/*
2363	 * This function is called during suspend and for one-time topology verification during
2364	 * first boot. In both cases, there is no need to protect swidget->use_count and
2365	 * sroute->setup because during suspend all running streams are suspended and during
2366	 * topology loading the sound card unavailable to open PCMs. Do not free the scheduler
2367	 * widgets yet so that the secondary cores do not get powered down before all the widgets
2368	 * associated with the scheduler are freed.
2369	 */
2370	ret = sof_ipc3_free_widgets_in_list(sdev, false, &dyn_widgets, verify);
2371	if (ret < 0)
2372		return ret;
2373
2374	/* free all the scheduler widgets now */
2375	ret = sof_ipc3_free_widgets_in_list(sdev, true, &dyn_widgets, verify);
2376	if (ret < 0)
2377		return ret;
2378
2379	/*
2380	 * Tear down all pipelines associated with PCMs that did not get suspended
2381	 * and unset the prepare flag so that they can be set up again during resume.
2382	 * Skip this step for older firmware unless topology has any
2383	 * dynamic pipeline (in which case the step is mandatory).
2384	 */
2385	if (!verify && (dyn_widgets || SOF_FW_VER(v->major, v->minor, v->micro) >=
2386	    SOF_FW_VER(2, 2, 0))) {
2387		ret = sof_tear_down_left_over_pipelines(sdev);
2388		if (ret < 0) {
2389			dev_err(sdev->dev, "failed to tear down paused pipelines\n");
2390			return ret;
2391		}
2392	}
2393
2394	list_for_each_entry(sroute, &sdev->route_list, list)
2395		sroute->setup = false;
2396
2397	/*
2398	 * before suspending, make sure the refcounts are all zeroed out. There's no way
2399	 * to recover at this point but this will help root cause bad sequences leading to
2400	 * more issues on resume
2401	 */
2402	list_for_each_entry(swidget, &sdev->widget_list, list) {
2403		if (swidget->use_count != 0) {
2404			dev_err(sdev->dev, "%s: widget %s is still in use: count %d\n",
2405				__func__, swidget->widget->name, swidget->use_count);
2406		}
2407	}
2408
2409	return 0;
2410}
2411
2412static int sof_ipc3_dai_get_clk(struct snd_sof_dev *sdev, struct snd_sof_dai *dai, int clk_type)
2413{
2414	struct sof_dai_private_data *private = dai->private;
2415
2416	if (!private || !private->dai_config)
2417		return 0;
2418
2419	switch (private->dai_config->type) {
2420	case SOF_DAI_INTEL_SSP:
2421		switch (clk_type) {
2422		case SOF_DAI_CLK_INTEL_SSP_MCLK:
2423			return private->dai_config->ssp.mclk_rate;
2424		case SOF_DAI_CLK_INTEL_SSP_BCLK:
2425			return private->dai_config->ssp.bclk_rate;
2426		default:
2427			break;
2428		}
2429		dev_err(sdev->dev, "fail to get SSP clk %d rate\n", clk_type);
2430		break;
2431	default:
2432		/* not yet implemented for platforms other than the above */
2433		dev_err(sdev->dev, "DAI type %d not supported yet!\n", private->dai_config->type);
2434		break;
2435	}
2436
2437	return -EINVAL;
2438}
2439
2440static int sof_ipc3_parse_manifest(struct snd_soc_component *scomp, int index,
2441				   struct snd_soc_tplg_manifest *man)
2442{
2443	u32 size = le32_to_cpu(man->priv.size);
2444	u32 abi_version;
2445
2446	/* backward compatible with tplg without ABI info */
2447	if (!size) {
2448		dev_dbg(scomp->dev, "No topology ABI info\n");
2449		return 0;
2450	}
2451
2452	if (size != SOF_IPC3_TPLG_ABI_SIZE) {
2453		dev_err(scomp->dev, "%s: Invalid topology ABI size: %u\n",
2454			__func__, size);
2455		return -EINVAL;
2456	}
2457
2458	dev_info(scomp->dev,
2459		 "Topology: ABI %d:%d:%d Kernel ABI %d:%d:%d\n",
2460		 man->priv.data[0], man->priv.data[1], man->priv.data[2],
2461		 SOF_ABI_MAJOR, SOF_ABI_MINOR, SOF_ABI_PATCH);
2462
2463	abi_version = SOF_ABI_VER(man->priv.data[0], man->priv.data[1], man->priv.data[2]);
2464
2465	if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, abi_version)) {
2466		dev_err(scomp->dev, "%s: Incompatible topology ABI version\n", __func__);
2467		return -EINVAL;
2468	}
2469
2470	if (IS_ENABLED(CONFIG_SND_SOC_SOF_STRICT_ABI_CHECKS) &&
2471	    SOF_ABI_VERSION_MINOR(abi_version) > SOF_ABI_MINOR) {
2472		dev_err(scomp->dev, "%s: Topology ABI is more recent than kernel\n", __func__);
2473		return -EINVAL;
2474	}
2475
2476	return 0;
2477}
2478
2479static int sof_ipc3_link_setup(struct snd_sof_dev *sdev, struct snd_soc_dai_link *link)
2480{
2481	if (link->no_pcm)
2482		return 0;
2483
2484	/*
2485	 * set default trigger order for all links. Exceptions to
2486	 * the rule will be handled in sof_pcm_dai_link_fixup()
2487	 * For playback, the sequence is the following: start FE,
2488	 * start BE, stop BE, stop FE; for Capture the sequence is
2489	 * inverted start BE, start FE, stop FE, stop BE
2490	 */
2491	link->trigger[SNDRV_PCM_STREAM_PLAYBACK] = SND_SOC_DPCM_TRIGGER_PRE;
2492	link->trigger[SNDRV_PCM_STREAM_CAPTURE] = SND_SOC_DPCM_TRIGGER_POST;
2493
2494	return 0;
2495}
2496
2497/* token list for each topology object */
2498static enum sof_tokens host_token_list[] = {
2499	SOF_CORE_TOKENS,
2500	SOF_COMP_EXT_TOKENS,
2501	SOF_PCM_TOKENS,
2502	SOF_COMP_TOKENS,
2503};
2504
2505static enum sof_tokens comp_generic_token_list[] = {
2506	SOF_CORE_TOKENS,
2507	SOF_COMP_EXT_TOKENS,
2508	SOF_COMP_TOKENS,
2509};
2510
2511static enum sof_tokens buffer_token_list[] = {
2512	SOF_BUFFER_TOKENS,
2513};
2514
2515static enum sof_tokens pipeline_token_list[] = {
2516	SOF_CORE_TOKENS,
2517	SOF_COMP_EXT_TOKENS,
2518	SOF_PIPELINE_TOKENS,
2519	SOF_SCHED_TOKENS,
2520};
2521
2522static enum sof_tokens asrc_token_list[] = {
2523	SOF_CORE_TOKENS,
2524	SOF_COMP_EXT_TOKENS,
2525	SOF_ASRC_TOKENS,
2526	SOF_COMP_TOKENS,
2527};
2528
2529static enum sof_tokens src_token_list[] = {
2530	SOF_CORE_TOKENS,
2531	SOF_COMP_EXT_TOKENS,
2532	SOF_SRC_TOKENS,
2533	SOF_COMP_TOKENS
2534};
2535
2536static enum sof_tokens pga_token_list[] = {
2537	SOF_CORE_TOKENS,
2538	SOF_COMP_EXT_TOKENS,
2539	SOF_VOLUME_TOKENS,
2540	SOF_COMP_TOKENS,
2541};
2542
2543static enum sof_tokens dai_token_list[] = {
2544	SOF_CORE_TOKENS,
2545	SOF_COMP_EXT_TOKENS,
2546	SOF_DAI_TOKENS,
2547	SOF_COMP_TOKENS,
2548};
2549
2550static enum sof_tokens process_token_list[] = {
2551	SOF_CORE_TOKENS,
2552	SOF_COMP_EXT_TOKENS,
2553	SOF_PROCESS_TOKENS,
2554	SOF_COMP_TOKENS,
2555};
2556
2557static const struct sof_ipc_tplg_widget_ops tplg_ipc3_widget_ops[SND_SOC_DAPM_TYPE_COUNT] = {
2558	[snd_soc_dapm_aif_in] =  {sof_ipc3_widget_setup_comp_host, sof_ipc3_widget_free_comp,
2559				  host_token_list, ARRAY_SIZE(host_token_list), NULL},
2560	[snd_soc_dapm_aif_out] = {sof_ipc3_widget_setup_comp_host, sof_ipc3_widget_free_comp,
2561				  host_token_list, ARRAY_SIZE(host_token_list), NULL},
2562
2563	[snd_soc_dapm_dai_in] = {sof_ipc3_widget_setup_comp_dai, sof_ipc3_widget_free_comp_dai,
2564				 dai_token_list, ARRAY_SIZE(dai_token_list), NULL},
2565	[snd_soc_dapm_dai_out] = {sof_ipc3_widget_setup_comp_dai, sof_ipc3_widget_free_comp_dai,
2566				  dai_token_list, ARRAY_SIZE(dai_token_list), NULL},
2567	[snd_soc_dapm_buffer] = {sof_ipc3_widget_setup_comp_buffer, sof_ipc3_widget_free_comp,
2568				 buffer_token_list, ARRAY_SIZE(buffer_token_list), NULL},
2569	[snd_soc_dapm_mixer] = {sof_ipc3_widget_setup_comp_mixer, sof_ipc3_widget_free_comp,
2570				comp_generic_token_list, ARRAY_SIZE(comp_generic_token_list),
2571				NULL},
2572	[snd_soc_dapm_src] = {sof_ipc3_widget_setup_comp_src, sof_ipc3_widget_free_comp,
2573			      src_token_list, ARRAY_SIZE(src_token_list), NULL},
2574	[snd_soc_dapm_asrc] = {sof_ipc3_widget_setup_comp_asrc, sof_ipc3_widget_free_comp,
2575			       asrc_token_list, ARRAY_SIZE(asrc_token_list), NULL},
2576	[snd_soc_dapm_siggen] = {sof_ipc3_widget_setup_comp_tone, sof_ipc3_widget_free_comp,
2577				 comp_generic_token_list, ARRAY_SIZE(comp_generic_token_list),
2578				 NULL},
2579	[snd_soc_dapm_scheduler] = {sof_ipc3_widget_setup_comp_pipeline, sof_ipc3_widget_free_comp,
2580				    pipeline_token_list, ARRAY_SIZE(pipeline_token_list), NULL},
2581	[snd_soc_dapm_pga] = {sof_ipc3_widget_setup_comp_pga, sof_ipc3_widget_free_comp,
2582			      pga_token_list, ARRAY_SIZE(pga_token_list), NULL},
2583	[snd_soc_dapm_mux] = {sof_ipc3_widget_setup_comp_mux, sof_ipc3_widget_free_comp,
2584			      comp_generic_token_list, ARRAY_SIZE(comp_generic_token_list), NULL},
2585	[snd_soc_dapm_demux] = {sof_ipc3_widget_setup_comp_mux, sof_ipc3_widget_free_comp,
2586				 comp_generic_token_list, ARRAY_SIZE(comp_generic_token_list),
2587				 NULL},
2588	[snd_soc_dapm_effect] = {sof_widget_update_ipc_comp_process, sof_ipc3_widget_free_comp,
2589				 process_token_list, ARRAY_SIZE(process_token_list),
2590				 sof_ipc3_widget_bind_event},
2591};
2592
2593const struct sof_ipc_tplg_ops ipc3_tplg_ops = {
2594	.widget = tplg_ipc3_widget_ops,
2595	.control = &tplg_ipc3_control_ops,
2596	.route_setup = sof_ipc3_route_setup,
2597	.control_setup = sof_ipc3_control_setup,
2598	.control_free = sof_ipc3_control_free,
2599	.pipeline_complete = sof_ipc3_complete_pipeline,
2600	.token_list = ipc3_token_list,
2601	.widget_free = sof_ipc3_widget_free,
2602	.widget_setup = sof_ipc3_widget_setup,
2603	.dai_config = sof_ipc3_dai_config,
2604	.dai_get_clk = sof_ipc3_dai_get_clk,
2605	.set_up_all_pipelines = sof_ipc3_set_up_all_pipelines,
2606	.tear_down_all_pipelines = sof_ipc3_tear_down_all_pipelines,
2607	.parse_manifest = sof_ipc3_parse_manifest,
2608	.link_setup = sof_ipc3_link_setup,
2609};
2610