xref: /linux/sound/soc/sof/ipc3-topology.c (revision 69bfec7548f4c1595bac0e3ddfc0458a5af31f4c)
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 	struct sof_ipc_reply reply;
1631 	int ret;
1632 
1633 	connect.hdr.size = sizeof(connect);
1634 	connect.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_CONNECT;
1635 	connect.source_id = sroute->src_widget->comp_id;
1636 	connect.sink_id = sroute->sink_widget->comp_id;
1637 
1638 	dev_dbg(sdev->dev, "setting up route %s -> %s\n",
1639 		sroute->src_widget->widget->name,
1640 		sroute->sink_widget->widget->name);
1641 
1642 	/* send ipc */
1643 	ret = sof_ipc_tx_message(sdev->ipc, &connect, sizeof(connect), &reply, sizeof(reply));
1644 	if (ret < 0)
1645 		dev_err(sdev->dev, "%s: route %s -> %s failed\n", __func__,
1646 			sroute->src_widget->widget->name, sroute->sink_widget->widget->name);
1647 
1648 	return ret;
1649 }
1650 
1651 static int sof_ipc3_control_load_bytes(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol)
1652 {
1653 	struct sof_ipc_ctrl_data *cdata;
1654 	size_t priv_size_check;
1655 	int ret;
1656 
1657 	if (scontrol->max_size < (sizeof(*cdata) + sizeof(struct sof_abi_hdr))) {
1658 		dev_err(sdev->dev, "%s: insufficient size for a bytes control: %zu.\n",
1659 			__func__, scontrol->max_size);
1660 		return -EINVAL;
1661 	}
1662 
1663 	if (scontrol->priv_size > scontrol->max_size - sizeof(*cdata)) {
1664 		dev_err(sdev->dev,
1665 			"%s: bytes data size %zu exceeds max %zu.\n", __func__,
1666 			scontrol->priv_size, scontrol->max_size - sizeof(*cdata));
1667 		return -EINVAL;
1668 	}
1669 
1670 	scontrol->ipc_control_data = kzalloc(scontrol->max_size, GFP_KERNEL);
1671 	if (!scontrol->ipc_control_data)
1672 		return -ENOMEM;
1673 
1674 	scontrol->size = sizeof(struct sof_ipc_ctrl_data) + scontrol->priv_size;
1675 
1676 	cdata = scontrol->ipc_control_data;
1677 	cdata->cmd = SOF_CTRL_CMD_BINARY;
1678 	cdata->index = scontrol->index;
1679 
1680 	if (scontrol->priv_size > 0) {
1681 		memcpy(cdata->data, scontrol->priv, scontrol->priv_size);
1682 		kfree(scontrol->priv);
1683 		scontrol->priv = NULL;
1684 
1685 		if (cdata->data->magic != SOF_ABI_MAGIC) {
1686 			dev_err(sdev->dev, "Wrong ABI magic 0x%08x.\n", cdata->data->magic);
1687 			ret = -EINVAL;
1688 			goto err;
1689 		}
1690 
1691 		if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, cdata->data->abi)) {
1692 			dev_err(sdev->dev, "Incompatible ABI version 0x%08x.\n",
1693 				cdata->data->abi);
1694 			ret = -EINVAL;
1695 			goto err;
1696 		}
1697 
1698 		priv_size_check = cdata->data->size + sizeof(struct sof_abi_hdr);
1699 		if (priv_size_check != scontrol->priv_size) {
1700 			dev_err(sdev->dev, "Conflict in bytes (%zu) vs. priv size (%zu).\n",
1701 				priv_size_check, scontrol->priv_size);
1702 			ret = -EINVAL;
1703 			goto err;
1704 		}
1705 	}
1706 
1707 	return 0;
1708 err:
1709 	kfree(scontrol->ipc_control_data);
1710 	scontrol->ipc_control_data = NULL;
1711 	return ret;
1712 }
1713 
1714 static int sof_ipc3_control_load_volume(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol)
1715 {
1716 	struct sof_ipc_ctrl_data *cdata;
1717 	int i;
1718 
1719 	/* init the volume get/put data */
1720 	scontrol->size = struct_size(cdata, chanv, scontrol->num_channels);
1721 
1722 	scontrol->ipc_control_data = kzalloc(scontrol->size, GFP_KERNEL);
1723 	if (!scontrol->ipc_control_data)
1724 		return -ENOMEM;
1725 
1726 	cdata = scontrol->ipc_control_data;
1727 	cdata->index = scontrol->index;
1728 
1729 	/* set cmd for mixer control */
1730 	if (scontrol->max == 1) {
1731 		cdata->cmd = SOF_CTRL_CMD_SWITCH;
1732 		return 0;
1733 	}
1734 
1735 	cdata->cmd = SOF_CTRL_CMD_VOLUME;
1736 
1737 	/* set default volume values to 0dB in control */
1738 	for (i = 0; i < scontrol->num_channels; i++) {
1739 		cdata->chanv[i].channel = i;
1740 		cdata->chanv[i].value = VOL_ZERO_DB;
1741 	}
1742 
1743 	return 0;
1744 }
1745 
1746 static int sof_ipc3_control_load_enum(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol)
1747 {
1748 	struct sof_ipc_ctrl_data *cdata;
1749 
1750 	/* init the enum get/put data */
1751 	scontrol->size = struct_size(cdata, chanv, scontrol->num_channels);
1752 
1753 	scontrol->ipc_control_data = kzalloc(scontrol->size, GFP_KERNEL);
1754 	if (!scontrol->ipc_control_data)
1755 		return -ENOMEM;
1756 
1757 	cdata = scontrol->ipc_control_data;
1758 	cdata->index = scontrol->index;
1759 	cdata->cmd = SOF_CTRL_CMD_ENUM;
1760 
1761 	return 0;
1762 }
1763 
1764 static int sof_ipc3_control_setup(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol)
1765 {
1766 	switch (scontrol->info_type) {
1767 	case SND_SOC_TPLG_CTL_VOLSW:
1768 	case SND_SOC_TPLG_CTL_VOLSW_SX:
1769 	case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1770 		return sof_ipc3_control_load_volume(sdev, scontrol);
1771 	case SND_SOC_TPLG_CTL_BYTES:
1772 		return sof_ipc3_control_load_bytes(sdev, scontrol);
1773 	case SND_SOC_TPLG_CTL_ENUM:
1774 	case SND_SOC_TPLG_CTL_ENUM_VALUE:
1775 		return sof_ipc3_control_load_enum(sdev, scontrol);
1776 	default:
1777 		break;
1778 	}
1779 
1780 	return 0;
1781 }
1782 
1783 static int sof_ipc3_control_free(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol)
1784 {
1785 	struct sof_ipc_free fcomp;
1786 
1787 	fcomp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_FREE;
1788 	fcomp.hdr.size = sizeof(fcomp);
1789 	fcomp.id = scontrol->comp_id;
1790 
1791 	/* send IPC to the DSP */
1792 	return sof_ipc_tx_message(sdev->ipc, &fcomp, sizeof(fcomp), NULL, 0);
1793 }
1794 
1795 /* send pcm params ipc */
1796 static int sof_ipc3_keyword_detect_pcm_params(struct snd_sof_widget *swidget, int dir)
1797 {
1798 	struct snd_soc_component *scomp = swidget->scomp;
1799 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1800 	struct sof_ipc_pcm_params_reply ipc_params_reply;
1801 	struct snd_pcm_hw_params *params;
1802 	struct sof_ipc_pcm_params pcm;
1803 	struct snd_sof_pcm *spcm;
1804 	int ret;
1805 
1806 	/* get runtime PCM params using widget's stream name */
1807 	spcm = snd_sof_find_spcm_name(scomp, swidget->widget->sname);
1808 	if (!spcm) {
1809 		dev_err(scomp->dev, "Cannot find PCM for %s\n", swidget->widget->name);
1810 		return -EINVAL;
1811 	}
1812 
1813 	params = &spcm->params[dir];
1814 
1815 	/* set IPC PCM params */
1816 	memset(&pcm, 0, sizeof(pcm));
1817 	pcm.hdr.size = sizeof(pcm);
1818 	pcm.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS;
1819 	pcm.comp_id = swidget->comp_id;
1820 	pcm.params.hdr.size = sizeof(pcm.params);
1821 	pcm.params.direction = dir;
1822 	pcm.params.sample_valid_bytes = params_width(params) >> 3;
1823 	pcm.params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED;
1824 	pcm.params.rate = params_rate(params);
1825 	pcm.params.channels = params_channels(params);
1826 	pcm.params.host_period_bytes = params_period_bytes(params);
1827 
1828 	/* set format */
1829 	switch (params_format(params)) {
1830 	case SNDRV_PCM_FORMAT_S16:
1831 		pcm.params.frame_fmt = SOF_IPC_FRAME_S16_LE;
1832 		break;
1833 	case SNDRV_PCM_FORMAT_S24:
1834 		pcm.params.frame_fmt = SOF_IPC_FRAME_S24_4LE;
1835 		break;
1836 	case SNDRV_PCM_FORMAT_S32:
1837 		pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE;
1838 		break;
1839 	default:
1840 		return -EINVAL;
1841 	}
1842 
1843 	/* send IPC to the DSP */
1844 	ret = sof_ipc_tx_message(sdev->ipc, &pcm, sizeof(pcm),
1845 				 &ipc_params_reply, sizeof(ipc_params_reply));
1846 	if (ret < 0)
1847 		dev_err(scomp->dev, "%s: PCM params failed for %s\n", __func__,
1848 			swidget->widget->name);
1849 
1850 	return ret;
1851 }
1852 
1853  /* send stream trigger ipc */
1854 static int sof_ipc3_keyword_detect_trigger(struct snd_sof_widget *swidget, int cmd)
1855 {
1856 	struct snd_soc_component *scomp = swidget->scomp;
1857 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1858 	struct sof_ipc_stream stream;
1859 	struct sof_ipc_reply reply;
1860 	int ret;
1861 
1862 	/* set IPC stream params */
1863 	stream.hdr.size = sizeof(stream);
1864 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | cmd;
1865 	stream.comp_id = swidget->comp_id;
1866 
1867 	/* send IPC to the DSP */
1868 	ret = sof_ipc_tx_message(sdev->ipc, &stream, sizeof(stream), &reply, sizeof(reply));
1869 	if (ret < 0)
1870 		dev_err(scomp->dev, "%s: Failed to trigger %s\n", __func__, swidget->widget->name);
1871 
1872 	return ret;
1873 }
1874 
1875 static int sof_ipc3_keyword_dapm_event(struct snd_soc_dapm_widget *w,
1876 				       struct snd_kcontrol *k, int event)
1877 {
1878 	struct snd_sof_widget *swidget = w->dobj.private;
1879 	struct snd_soc_component *scomp;
1880 	int stream = SNDRV_PCM_STREAM_CAPTURE;
1881 	struct snd_sof_pcm *spcm;
1882 	int ret = 0;
1883 
1884 	if (!swidget)
1885 		return 0;
1886 
1887 	scomp = swidget->scomp;
1888 
1889 	dev_dbg(scomp->dev, "received event %d for widget %s\n",
1890 		event, w->name);
1891 
1892 	/* get runtime PCM params using widget's stream name */
1893 	spcm = snd_sof_find_spcm_name(scomp, swidget->widget->sname);
1894 	if (!spcm) {
1895 		dev_err(scomp->dev, "%s: Cannot find PCM for %s\n", __func__,
1896 			swidget->widget->name);
1897 		return -EINVAL;
1898 	}
1899 
1900 	/* process events */
1901 	switch (event) {
1902 	case SND_SOC_DAPM_PRE_PMU:
1903 		if (spcm->stream[stream].suspend_ignored) {
1904 			dev_dbg(scomp->dev, "PRE_PMU event ignored, KWD pipeline is already RUNNING\n");
1905 			return 0;
1906 		}
1907 
1908 		/* set pcm params */
1909 		ret = sof_ipc3_keyword_detect_pcm_params(swidget, stream);
1910 		if (ret < 0) {
1911 			dev_err(scomp->dev, "%s: Failed to set pcm params for widget %s\n",
1912 				__func__, swidget->widget->name);
1913 			break;
1914 		}
1915 
1916 		/* start trigger */
1917 		ret = sof_ipc3_keyword_detect_trigger(swidget, SOF_IPC_STREAM_TRIG_START);
1918 		if (ret < 0)
1919 			dev_err(scomp->dev, "%s: Failed to trigger widget %s\n", __func__,
1920 				swidget->widget->name);
1921 		break;
1922 	case SND_SOC_DAPM_POST_PMD:
1923 		if (spcm->stream[stream].suspend_ignored) {
1924 			dev_dbg(scomp->dev,
1925 				"POST_PMD event ignored, KWD pipeline will remain RUNNING\n");
1926 			return 0;
1927 		}
1928 
1929 		/* stop trigger */
1930 		ret = sof_ipc3_keyword_detect_trigger(swidget, SOF_IPC_STREAM_TRIG_STOP);
1931 		if (ret < 0)
1932 			dev_err(scomp->dev, "%s: Failed to trigger widget %s\n", __func__,
1933 				swidget->widget->name);
1934 
1935 		/* pcm free */
1936 		ret = sof_ipc3_keyword_detect_trigger(swidget, SOF_IPC_STREAM_PCM_FREE);
1937 		if (ret < 0)
1938 			dev_err(scomp->dev, "%s: Failed to free PCM for widget %s\n", __func__,
1939 				swidget->widget->name);
1940 		break;
1941 	default:
1942 		break;
1943 	}
1944 
1945 	return ret;
1946 }
1947 
1948 /* event handlers for keyword detect component */
1949 static const struct snd_soc_tplg_widget_events sof_kwd_events[] = {
1950 	{SOF_KEYWORD_DETECT_DAPM_EVENT, sof_ipc3_keyword_dapm_event},
1951 };
1952 
1953 static int sof_ipc3_widget_bind_event(struct snd_soc_component *scomp,
1954 				      struct snd_sof_widget *swidget, u16 event_type)
1955 {
1956 	struct sof_ipc_comp *ipc_comp;
1957 
1958 	/* validate widget event type */
1959 	switch (event_type) {
1960 	case SOF_KEYWORD_DETECT_DAPM_EVENT:
1961 		/* only KEYWORD_DETECT comps should handle this */
1962 		if (swidget->id != snd_soc_dapm_effect)
1963 			break;
1964 
1965 		ipc_comp = swidget->private;
1966 		if (ipc_comp && ipc_comp->type != SOF_COMP_KEYWORD_DETECT)
1967 			break;
1968 
1969 		/* bind event to keyword detect comp */
1970 		return snd_soc_tplg_widget_bind_event(swidget->widget, sof_kwd_events,
1971 						      ARRAY_SIZE(sof_kwd_events), event_type);
1972 	default:
1973 		break;
1974 	}
1975 
1976 	dev_err(scomp->dev, "Invalid event type %d for widget %s\n", event_type,
1977 		swidget->widget->name);
1978 
1979 	return -EINVAL;
1980 }
1981 
1982 static int sof_ipc3_complete_pipeline(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
1983 {
1984 	struct sof_ipc_pipe_ready ready;
1985 	struct sof_ipc_reply reply;
1986 	int ret;
1987 
1988 	dev_dbg(sdev->dev, "tplg: complete pipeline %s id %d\n",
1989 		swidget->widget->name, swidget->comp_id);
1990 
1991 	memset(&ready, 0, sizeof(ready));
1992 	ready.hdr.size = sizeof(ready);
1993 	ready.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_PIPE_COMPLETE;
1994 	ready.comp_id = swidget->comp_id;
1995 
1996 	ret = sof_ipc_tx_message(sdev->ipc, &ready, sizeof(ready), &reply, sizeof(reply));
1997 	if (ret < 0)
1998 		return ret;
1999 
2000 	return 1;
2001 }
2002 
2003 static int sof_ipc3_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
2004 {
2005 	struct sof_ipc_free ipc_free = {
2006 		.hdr = {
2007 			.size = sizeof(ipc_free),
2008 			.cmd = SOF_IPC_GLB_TPLG_MSG,
2009 		},
2010 		.id = swidget->comp_id,
2011 	};
2012 	struct sof_ipc_reply reply;
2013 	int ret;
2014 
2015 	if (!swidget->private)
2016 		return 0;
2017 
2018 	switch (swidget->id) {
2019 	case snd_soc_dapm_scheduler:
2020 	{
2021 		ipc_free.hdr.cmd |= SOF_IPC_TPLG_PIPE_FREE;
2022 		break;
2023 	}
2024 	case snd_soc_dapm_buffer:
2025 		ipc_free.hdr.cmd |= SOF_IPC_TPLG_BUFFER_FREE;
2026 		break;
2027 	default:
2028 		ipc_free.hdr.cmd |= SOF_IPC_TPLG_COMP_FREE;
2029 		break;
2030 	}
2031 
2032 	ret = sof_ipc_tx_message(sdev->ipc, &ipc_free, sizeof(ipc_free),
2033 				 &reply, sizeof(reply));
2034 	if (ret < 0)
2035 		dev_err(sdev->dev, "failed to free widget %s\n", swidget->widget->name);
2036 
2037 	return ret;
2038 }
2039 
2040 static int sof_ipc3_dai_config(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget,
2041 			       unsigned int flags, struct snd_sof_dai_config_data *data)
2042 {
2043 	struct sof_ipc_fw_version *v = &sdev->fw_ready.version;
2044 	struct snd_sof_dai *dai = swidget->private;
2045 	struct sof_dai_private_data *private;
2046 	struct sof_ipc_dai_config *config;
2047 	struct sof_ipc_reply reply;
2048 	int ret = 0;
2049 
2050 	if (!dai || !dai->private) {
2051 		dev_err(sdev->dev, "No private data for DAI %s\n", swidget->widget->name);
2052 		return -EINVAL;
2053 	}
2054 
2055 	private = dai->private;
2056 	if (!private->dai_config) {
2057 		dev_err(sdev->dev, "No config for DAI %s\n", dai->name);
2058 		return -EINVAL;
2059 	}
2060 
2061 	config = &private->dai_config[dai->current_config];
2062 	if (!config) {
2063 		dev_err(sdev->dev, "Invalid current config for DAI %s\n", dai->name);
2064 		return -EINVAL;
2065 	}
2066 
2067 	switch (config->type) {
2068 	case SOF_DAI_INTEL_SSP:
2069 		/*
2070 		 * DAI_CONFIG IPC during hw_params/hw_free for SSP DAI's is not supported in older
2071 		 * firmware
2072 		 */
2073 		if (v->abi_version < SOF_ABI_VER(3, 18, 0) &&
2074 		    ((flags & SOF_DAI_CONFIG_FLAGS_HW_PARAMS) ||
2075 		     (flags & SOF_DAI_CONFIG_FLAGS_HW_FREE)))
2076 			return 0;
2077 		break;
2078 	case SOF_DAI_INTEL_HDA:
2079 		if (data)
2080 			config->hda.link_dma_ch = data->dai_data;
2081 		break;
2082 	case SOF_DAI_INTEL_ALH:
2083 		if (data) {
2084 			config->dai_index = data->dai_index;
2085 			config->alh.stream_id = data->dai_data;
2086 		}
2087 		break;
2088 	default:
2089 		break;
2090 	}
2091 
2092 	config->flags = flags;
2093 
2094 	/* only send the IPC if the widget is set up in the DSP */
2095 	if (swidget->use_count > 0) {
2096 		ret = sof_ipc_tx_message(sdev->ipc, config, config->hdr.size,
2097 					 &reply, sizeof(reply));
2098 		if (ret < 0)
2099 			dev_err(sdev->dev, "Failed to set dai config for %s\n", dai->name);
2100 	}
2101 
2102 	return ret;
2103 }
2104 
2105 static int sof_ipc3_widget_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
2106 {
2107 	struct sof_ipc_comp_reply reply;
2108 	int ret;
2109 
2110 	if (!swidget->private)
2111 		return 0;
2112 
2113 	switch (swidget->id) {
2114 	case snd_soc_dapm_dai_in:
2115 	case snd_soc_dapm_dai_out:
2116 	{
2117 		struct snd_sof_dai *dai = swidget->private;
2118 		struct sof_dai_private_data *dai_data = dai->private;
2119 		struct sof_ipc_comp *comp = &dai_data->comp_dai->comp;
2120 
2121 		ret = sof_ipc_tx_message(sdev->ipc, dai_data->comp_dai,
2122 					 comp->hdr.size, &reply, sizeof(reply));
2123 		break;
2124 	}
2125 	case snd_soc_dapm_scheduler:
2126 	{
2127 		struct sof_ipc_pipe_new *pipeline;
2128 
2129 		pipeline = swidget->private;
2130 		ret = sof_ipc_tx_message(sdev->ipc, pipeline, sizeof(*pipeline),
2131 					 &reply, sizeof(reply));
2132 		break;
2133 	}
2134 	default:
2135 	{
2136 		struct sof_ipc_cmd_hdr *hdr;
2137 
2138 		hdr = swidget->private;
2139 		ret = sof_ipc_tx_message(sdev->ipc, swidget->private, hdr->size,
2140 					 &reply, sizeof(reply));
2141 		break;
2142 	}
2143 	}
2144 	if (ret < 0)
2145 		dev_err(sdev->dev, "Failed to setup widget %s\n", swidget->widget->name);
2146 
2147 	return ret;
2148 }
2149 
2150 static int sof_ipc3_set_up_all_pipelines(struct snd_sof_dev *sdev, bool verify)
2151 {
2152 	struct sof_ipc_fw_version *v = &sdev->fw_ready.version;
2153 	struct snd_sof_widget *swidget;
2154 	struct snd_sof_route *sroute;
2155 	int ret;
2156 
2157 	/* restore pipeline components */
2158 	list_for_each_entry(swidget, &sdev->widget_list, list) {
2159 		/* only set up the widgets belonging to static pipelines */
2160 		if (!verify && swidget->dynamic_pipeline_widget)
2161 			continue;
2162 
2163 		/*
2164 		 * For older firmware, skip scheduler widgets in this loop,
2165 		 * sof_widget_setup() will be called in the 'complete pipeline' loop
2166 		 */
2167 		if (v->abi_version < SOF_ABI_VER(3, 19, 0) &&
2168 		    swidget->id == snd_soc_dapm_scheduler)
2169 			continue;
2170 
2171 		/* update DAI config. The IPC will be sent in sof_widget_setup() */
2172 		if (WIDGET_IS_DAI(swidget->id)) {
2173 			struct snd_sof_dai *dai = swidget->private;
2174 			struct sof_dai_private_data *private;
2175 			struct sof_ipc_dai_config *config;
2176 
2177 			if (!dai || !dai->private)
2178 				continue;
2179 			private = dai->private;
2180 			if (!private->dai_config)
2181 				continue;
2182 
2183 			config = private->dai_config;
2184 			/*
2185 			 * The link DMA channel would be invalidated for running
2186 			 * streams but not for streams that were in the PAUSED
2187 			 * state during suspend. So invalidate it here before setting
2188 			 * the dai config in the DSP.
2189 			 */
2190 			if (config->type == SOF_DAI_INTEL_HDA)
2191 				config->hda.link_dma_ch = DMA_CHAN_INVALID;
2192 		}
2193 
2194 		ret = sof_widget_setup(sdev, swidget);
2195 		if (ret < 0)
2196 			return ret;
2197 	}
2198 
2199 	/* restore pipeline connections */
2200 	list_for_each_entry(sroute, &sdev->route_list, list) {
2201 		/* only set up routes belonging to static pipelines */
2202 		if (!verify && (sroute->src_widget->dynamic_pipeline_widget ||
2203 				sroute->sink_widget->dynamic_pipeline_widget))
2204 			continue;
2205 
2206 		/*
2207 		 * For virtual routes, both sink and source are not buffer. IPC3 only supports
2208 		 * connections between a buffer and a component. Ignore the rest.
2209 		 */
2210 		if (sroute->src_widget->id != snd_soc_dapm_buffer &&
2211 		    sroute->sink_widget->id != snd_soc_dapm_buffer)
2212 			continue;
2213 
2214 		ret = sof_route_setup(sdev, sroute->src_widget->widget,
2215 				      sroute->sink_widget->widget);
2216 		if (ret < 0) {
2217 			dev_err(sdev->dev, "%s: route set up failed\n", __func__);
2218 			return ret;
2219 		}
2220 	}
2221 
2222 	/* complete pipeline */
2223 	list_for_each_entry(swidget, &sdev->widget_list, list) {
2224 		switch (swidget->id) {
2225 		case snd_soc_dapm_scheduler:
2226 			/* only complete static pipelines */
2227 			if (!verify && swidget->dynamic_pipeline_widget)
2228 				continue;
2229 
2230 			if (v->abi_version < SOF_ABI_VER(3, 19, 0)) {
2231 				ret = sof_widget_setup(sdev, swidget);
2232 				if (ret < 0)
2233 					return ret;
2234 			}
2235 
2236 			swidget->spipe->complete = sof_ipc3_complete_pipeline(sdev, swidget);
2237 			if (swidget->spipe->complete < 0)
2238 				return swidget->spipe->complete;
2239 			break;
2240 		default:
2241 			break;
2242 		}
2243 	}
2244 
2245 	return 0;
2246 }
2247 
2248 /*
2249  * Free the PCM, its associated widgets and set the prepared flag to false for all PCMs that
2250  * did not get suspended(ex: paused streams) so the widgets can be set up again during resume.
2251  */
2252 static int sof_tear_down_left_over_pipelines(struct snd_sof_dev *sdev)
2253 {
2254 	struct snd_sof_widget *swidget;
2255 	struct snd_sof_pcm *spcm;
2256 	int dir, ret;
2257 
2258 	/*
2259 	 * free all PCMs and their associated DAPM widgets if their connected DAPM widget
2260 	 * list is not NULL. This should only be true for paused streams at this point.
2261 	 * This is equivalent to the handling of FE DAI suspend trigger for running streams.
2262 	 */
2263 	list_for_each_entry(spcm, &sdev->pcm_list, list) {
2264 		for_each_pcm_streams(dir) {
2265 			struct snd_pcm_substream *substream = spcm->stream[dir].substream;
2266 
2267 			if (!substream || !substream->runtime || spcm->stream[dir].suspend_ignored)
2268 				continue;
2269 
2270 			if (spcm->stream[dir].list) {
2271 				ret = sof_pcm_stream_free(sdev, substream, spcm, dir, true);
2272 				if (ret < 0)
2273 					return ret;
2274 			}
2275 		}
2276 	}
2277 
2278 	/*
2279 	 * free any left over DAI widgets. This is equivalent to the handling of suspend trigger
2280 	 * for the BE DAI for running streams.
2281 	 */
2282 	list_for_each_entry(swidget, &sdev->widget_list, list)
2283 		if (WIDGET_IS_DAI(swidget->id) && swidget->use_count == 1) {
2284 			ret = sof_widget_free(sdev, swidget);
2285 			if (ret < 0)
2286 				return ret;
2287 		}
2288 
2289 	return 0;
2290 }
2291 
2292 /*
2293  * For older firmware, this function doesn't free widgets for static pipelines during suspend.
2294  * It only resets use_count for all widgets.
2295  */
2296 static int sof_ipc3_tear_down_all_pipelines(struct snd_sof_dev *sdev, bool verify)
2297 {
2298 	struct sof_ipc_fw_version *v = &sdev->fw_ready.version;
2299 	struct snd_sof_widget *swidget;
2300 	struct snd_sof_route *sroute;
2301 	bool dyn_widgets = false;
2302 	int ret;
2303 
2304 	/*
2305 	 * This function is called during suspend and for one-time topology verification during
2306 	 * first boot. In both cases, there is no need to protect swidget->use_count and
2307 	 * sroute->setup because during suspend all running streams are suspended and during
2308 	 * topology loading the sound card unavailable to open PCMs.
2309 	 */
2310 	list_for_each_entry(swidget, &sdev->widget_list, list) {
2311 		if (swidget->dynamic_pipeline_widget) {
2312 			dyn_widgets = true;
2313 			continue;
2314 		}
2315 
2316 		/* Do not free widgets for static pipelines with FW older than SOF2.2 */
2317 		if (!verify && !swidget->dynamic_pipeline_widget &&
2318 		    SOF_FW_VER(v->major, v->minor, v->micro) < SOF_FW_VER(2, 2, 0)) {
2319 			mutex_lock(&swidget->setup_mutex);
2320 			swidget->use_count = 0;
2321 			mutex_unlock(&swidget->setup_mutex);
2322 			if (swidget->spipe)
2323 				swidget->spipe->complete = 0;
2324 			continue;
2325 		}
2326 
2327 		ret = sof_widget_free(sdev, swidget);
2328 		if (ret < 0)
2329 			return ret;
2330 	}
2331 
2332 	/*
2333 	 * Tear down all pipelines associated with PCMs that did not get suspended
2334 	 * and unset the prepare flag so that they can be set up again during resume.
2335 	 * Skip this step for older firmware unless topology has any
2336 	 * dynamic pipeline (in which case the step is mandatory).
2337 	 */
2338 	if (!verify && (dyn_widgets || SOF_FW_VER(v->major, v->minor, v->micro) >=
2339 	    SOF_FW_VER(2, 2, 0))) {
2340 		ret = sof_tear_down_left_over_pipelines(sdev);
2341 		if (ret < 0) {
2342 			dev_err(sdev->dev, "failed to tear down paused pipelines\n");
2343 			return ret;
2344 		}
2345 	}
2346 
2347 	list_for_each_entry(sroute, &sdev->route_list, list)
2348 		sroute->setup = false;
2349 
2350 	/*
2351 	 * before suspending, make sure the refcounts are all zeroed out. There's no way
2352 	 * to recover at this point but this will help root cause bad sequences leading to
2353 	 * more issues on resume
2354 	 */
2355 	list_for_each_entry(swidget, &sdev->widget_list, list) {
2356 		if (swidget->use_count != 0) {
2357 			dev_err(sdev->dev, "%s: widget %s is still in use: count %d\n",
2358 				__func__, swidget->widget->name, swidget->use_count);
2359 		}
2360 	}
2361 
2362 	return 0;
2363 }
2364 
2365 static int sof_ipc3_dai_get_clk(struct snd_sof_dev *sdev, struct snd_sof_dai *dai, int clk_type)
2366 {
2367 	struct sof_dai_private_data *private = dai->private;
2368 
2369 	if (!private || !private->dai_config)
2370 		return 0;
2371 
2372 	switch (private->dai_config->type) {
2373 	case SOF_DAI_INTEL_SSP:
2374 		switch (clk_type) {
2375 		case SOF_DAI_CLK_INTEL_SSP_MCLK:
2376 			return private->dai_config->ssp.mclk_rate;
2377 		case SOF_DAI_CLK_INTEL_SSP_BCLK:
2378 			return private->dai_config->ssp.bclk_rate;
2379 		default:
2380 			break;
2381 		}
2382 		dev_err(sdev->dev, "fail to get SSP clk %d rate\n", clk_type);
2383 		break;
2384 	default:
2385 		/* not yet implemented for platforms other than the above */
2386 		dev_err(sdev->dev, "DAI type %d not supported yet!\n", private->dai_config->type);
2387 		break;
2388 	}
2389 
2390 	return -EINVAL;
2391 }
2392 
2393 static int sof_ipc3_parse_manifest(struct snd_soc_component *scomp, int index,
2394 				   struct snd_soc_tplg_manifest *man)
2395 {
2396 	u32 size = le32_to_cpu(man->priv.size);
2397 	u32 abi_version;
2398 
2399 	/* backward compatible with tplg without ABI info */
2400 	if (!size) {
2401 		dev_dbg(scomp->dev, "No topology ABI info\n");
2402 		return 0;
2403 	}
2404 
2405 	if (size != SOF_IPC3_TPLG_ABI_SIZE) {
2406 		dev_err(scomp->dev, "%s: Invalid topology ABI size: %u\n",
2407 			__func__, size);
2408 		return -EINVAL;
2409 	}
2410 
2411 	dev_info(scomp->dev,
2412 		 "Topology: ABI %d:%d:%d Kernel ABI %d:%d:%d\n",
2413 		 man->priv.data[0], man->priv.data[1], man->priv.data[2],
2414 		 SOF_ABI_MAJOR, SOF_ABI_MINOR, SOF_ABI_PATCH);
2415 
2416 	abi_version = SOF_ABI_VER(man->priv.data[0], man->priv.data[1], man->priv.data[2]);
2417 
2418 	if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, abi_version)) {
2419 		dev_err(scomp->dev, "%s: Incompatible topology ABI version\n", __func__);
2420 		return -EINVAL;
2421 	}
2422 
2423 	if (IS_ENABLED(CONFIG_SND_SOC_SOF_STRICT_ABI_CHECKS) &&
2424 	    SOF_ABI_VERSION_MINOR(abi_version) > SOF_ABI_MINOR) {
2425 		dev_err(scomp->dev, "%s: Topology ABI is more recent than kernel\n", __func__);
2426 		return -EINVAL;
2427 	}
2428 
2429 	return 0;
2430 }
2431 
2432 static int sof_ipc3_link_setup(struct snd_sof_dev *sdev, struct snd_soc_dai_link *link)
2433 {
2434 	if (link->no_pcm)
2435 		return 0;
2436 
2437 	/*
2438 	 * set default trigger order for all links. Exceptions to
2439 	 * the rule will be handled in sof_pcm_dai_link_fixup()
2440 	 * For playback, the sequence is the following: start FE,
2441 	 * start BE, stop BE, stop FE; for Capture the sequence is
2442 	 * inverted start BE, start FE, stop FE, stop BE
2443 	 */
2444 	link->trigger[SNDRV_PCM_STREAM_PLAYBACK] = SND_SOC_DPCM_TRIGGER_PRE;
2445 	link->trigger[SNDRV_PCM_STREAM_CAPTURE] = SND_SOC_DPCM_TRIGGER_POST;
2446 
2447 	return 0;
2448 }
2449 
2450 /* token list for each topology object */
2451 static enum sof_tokens host_token_list[] = {
2452 	SOF_CORE_TOKENS,
2453 	SOF_COMP_EXT_TOKENS,
2454 	SOF_PCM_TOKENS,
2455 	SOF_COMP_TOKENS,
2456 };
2457 
2458 static enum sof_tokens comp_generic_token_list[] = {
2459 	SOF_CORE_TOKENS,
2460 	SOF_COMP_EXT_TOKENS,
2461 	SOF_COMP_TOKENS,
2462 };
2463 
2464 static enum sof_tokens buffer_token_list[] = {
2465 	SOF_BUFFER_TOKENS,
2466 };
2467 
2468 static enum sof_tokens pipeline_token_list[] = {
2469 	SOF_CORE_TOKENS,
2470 	SOF_COMP_EXT_TOKENS,
2471 	SOF_PIPELINE_TOKENS,
2472 	SOF_SCHED_TOKENS,
2473 };
2474 
2475 static enum sof_tokens asrc_token_list[] = {
2476 	SOF_CORE_TOKENS,
2477 	SOF_COMP_EXT_TOKENS,
2478 	SOF_ASRC_TOKENS,
2479 	SOF_COMP_TOKENS,
2480 };
2481 
2482 static enum sof_tokens src_token_list[] = {
2483 	SOF_CORE_TOKENS,
2484 	SOF_COMP_EXT_TOKENS,
2485 	SOF_SRC_TOKENS,
2486 	SOF_COMP_TOKENS
2487 };
2488 
2489 static enum sof_tokens pga_token_list[] = {
2490 	SOF_CORE_TOKENS,
2491 	SOF_COMP_EXT_TOKENS,
2492 	SOF_VOLUME_TOKENS,
2493 	SOF_COMP_TOKENS,
2494 };
2495 
2496 static enum sof_tokens dai_token_list[] = {
2497 	SOF_CORE_TOKENS,
2498 	SOF_COMP_EXT_TOKENS,
2499 	SOF_DAI_TOKENS,
2500 	SOF_COMP_TOKENS,
2501 };
2502 
2503 static enum sof_tokens process_token_list[] = {
2504 	SOF_CORE_TOKENS,
2505 	SOF_COMP_EXT_TOKENS,
2506 	SOF_PROCESS_TOKENS,
2507 	SOF_COMP_TOKENS,
2508 };
2509 
2510 static const struct sof_ipc_tplg_widget_ops tplg_ipc3_widget_ops[SND_SOC_DAPM_TYPE_COUNT] = {
2511 	[snd_soc_dapm_aif_in] =  {sof_ipc3_widget_setup_comp_host, sof_ipc3_widget_free_comp,
2512 				  host_token_list, ARRAY_SIZE(host_token_list), NULL},
2513 	[snd_soc_dapm_aif_out] = {sof_ipc3_widget_setup_comp_host, sof_ipc3_widget_free_comp,
2514 				  host_token_list, ARRAY_SIZE(host_token_list), NULL},
2515 
2516 	[snd_soc_dapm_dai_in] = {sof_ipc3_widget_setup_comp_dai, sof_ipc3_widget_free_comp_dai,
2517 				 dai_token_list, ARRAY_SIZE(dai_token_list), NULL},
2518 	[snd_soc_dapm_dai_out] = {sof_ipc3_widget_setup_comp_dai, sof_ipc3_widget_free_comp_dai,
2519 				  dai_token_list, ARRAY_SIZE(dai_token_list), NULL},
2520 	[snd_soc_dapm_buffer] = {sof_ipc3_widget_setup_comp_buffer, sof_ipc3_widget_free_comp,
2521 				 buffer_token_list, ARRAY_SIZE(buffer_token_list), NULL},
2522 	[snd_soc_dapm_mixer] = {sof_ipc3_widget_setup_comp_mixer, sof_ipc3_widget_free_comp,
2523 				comp_generic_token_list, ARRAY_SIZE(comp_generic_token_list),
2524 				NULL},
2525 	[snd_soc_dapm_src] = {sof_ipc3_widget_setup_comp_src, sof_ipc3_widget_free_comp,
2526 			      src_token_list, ARRAY_SIZE(src_token_list), NULL},
2527 	[snd_soc_dapm_asrc] = {sof_ipc3_widget_setup_comp_asrc, sof_ipc3_widget_free_comp,
2528 			       asrc_token_list, ARRAY_SIZE(asrc_token_list), NULL},
2529 	[snd_soc_dapm_siggen] = {sof_ipc3_widget_setup_comp_tone, sof_ipc3_widget_free_comp,
2530 				 comp_generic_token_list, ARRAY_SIZE(comp_generic_token_list),
2531 				 NULL},
2532 	[snd_soc_dapm_scheduler] = {sof_ipc3_widget_setup_comp_pipeline, sof_ipc3_widget_free_comp,
2533 				    pipeline_token_list, ARRAY_SIZE(pipeline_token_list), NULL},
2534 	[snd_soc_dapm_pga] = {sof_ipc3_widget_setup_comp_pga, sof_ipc3_widget_free_comp,
2535 			      pga_token_list, ARRAY_SIZE(pga_token_list), NULL},
2536 	[snd_soc_dapm_mux] = {sof_ipc3_widget_setup_comp_mux, sof_ipc3_widget_free_comp,
2537 			      comp_generic_token_list, ARRAY_SIZE(comp_generic_token_list), NULL},
2538 	[snd_soc_dapm_demux] = {sof_ipc3_widget_setup_comp_mux, sof_ipc3_widget_free_comp,
2539 				 comp_generic_token_list, ARRAY_SIZE(comp_generic_token_list),
2540 				 NULL},
2541 	[snd_soc_dapm_effect] = {sof_widget_update_ipc_comp_process, sof_ipc3_widget_free_comp,
2542 				 process_token_list, ARRAY_SIZE(process_token_list),
2543 				 sof_ipc3_widget_bind_event},
2544 };
2545 
2546 const struct sof_ipc_tplg_ops ipc3_tplg_ops = {
2547 	.widget = tplg_ipc3_widget_ops,
2548 	.control = &tplg_ipc3_control_ops,
2549 	.route_setup = sof_ipc3_route_setup,
2550 	.control_setup = sof_ipc3_control_setup,
2551 	.control_free = sof_ipc3_control_free,
2552 	.pipeline_complete = sof_ipc3_complete_pipeline,
2553 	.token_list = ipc3_token_list,
2554 	.widget_free = sof_ipc3_widget_free,
2555 	.widget_setup = sof_ipc3_widget_setup,
2556 	.dai_config = sof_ipc3_dai_config,
2557 	.dai_get_clk = sof_ipc3_dai_get_clk,
2558 	.set_up_all_pipelines = sof_ipc3_set_up_all_pipelines,
2559 	.tear_down_all_pipelines = sof_ipc3_tear_down_all_pipelines,
2560 	.parse_manifest = sof_ipc3_parse_manifest,
2561 	.link_setup = sof_ipc3_link_setup,
2562 };
2563