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