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