xref: /openbmc/linux/sound/soc/sof/topology.c (revision 683b54ef603825328859d867aabf9a6d973238a2)
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) 2018 Intel Corporation. All rights reserved.
7 //
8 // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9 //
10 
11 #include <linux/bits.h>
12 #include <linux/device.h>
13 #include <linux/errno.h>
14 #include <linux/firmware.h>
15 #include <linux/workqueue.h>
16 #include <sound/tlv.h>
17 #include <sound/pcm_params.h>
18 #include <uapi/sound/sof/tokens.h>
19 #include "sof-priv.h"
20 #include "sof-audio.h"
21 #include "ops.h"
22 
23 #define COMP_ID_UNASSIGNED		0xffffffff
24 /*
25  * Constants used in the computation of linear volume gain
26  * from dB gain 20th root of 10 in Q1.16 fixed-point notation
27  */
28 #define VOL_TWENTIETH_ROOT_OF_TEN	73533
29 /* 40th root of 10 in Q1.16 fixed-point notation*/
30 #define VOL_FORTIETH_ROOT_OF_TEN	69419
31 /*
32  * Volume fractional word length define to 16 sets
33  * the volume linear gain value to use Qx.16 format
34  */
35 #define VOLUME_FWL	16
36 /* 0.5 dB step value in topology TLV */
37 #define VOL_HALF_DB_STEP	50
38 /* Full volume for default values */
39 #define VOL_ZERO_DB	BIT(VOLUME_FWL)
40 
41 /* TLV data items */
42 #define TLV_ITEMS	3
43 #define TLV_MIN		0
44 #define TLV_STEP	1
45 #define TLV_MUTE	2
46 
47 /* size of tplg abi in byte */
48 #define SOF_TPLG_ABI_SIZE 3
49 
50 /**
51  * sof_update_ipc_object - Parse multiple sets of tokens within the token array associated with the
52  *			    token ID.
53  * @scomp: pointer to SOC component
54  * @object: target IPC struct to save the parsed values
55  * @token_id: token ID for the token array to be searched
56  * @tuples: pointer to the tuples array
57  * @num_tuples: number of tuples in the tuples array
58  * @object_size: size of the object
59  * @token_instance_num: number of times the same @token_id needs to be parsed i.e. the function
60  *			looks for @token_instance_num of each token in the token array associated
61  *			with the @token_id
62  */
63 int sof_update_ipc_object(struct snd_soc_component *scomp, void *object, enum sof_tokens token_id,
64 			  struct snd_sof_tuple *tuples, int num_tuples,
65 			  size_t object_size, int token_instance_num)
66 {
67 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
68 	const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
69 	const struct sof_token_info *token_list = ipc_tplg_ops->token_list;
70 	const struct sof_topology_token *tokens;
71 	int i, j;
72 
73 	if (token_list[token_id].count < 0) {
74 		dev_err(scomp->dev, "Invalid token count for token ID: %d\n", token_id);
75 		return -EINVAL;
76 	}
77 
78 	/* No tokens to match */
79 	if (!token_list[token_id].count)
80 		return 0;
81 
82 	tokens = token_list[token_id].tokens;
83 	if (!tokens) {
84 		dev_err(scomp->dev, "Invalid tokens for token id: %d\n", token_id);
85 		return -EINVAL;
86 	}
87 
88 	for (i = 0; i < token_list[token_id].count; i++) {
89 		int offset = 0;
90 		int num_tokens_matched = 0;
91 
92 		for (j = 0; j < num_tuples; j++) {
93 			if (tokens[i].token == tuples[j].token) {
94 				switch (tokens[i].type) {
95 				case SND_SOC_TPLG_TUPLE_TYPE_WORD:
96 				{
97 					u32 *val = (u32 *)((u8 *)object + tokens[i].offset +
98 							   offset);
99 
100 					*val = tuples[j].value.v;
101 					break;
102 				}
103 				case SND_SOC_TPLG_TUPLE_TYPE_SHORT:
104 				case SND_SOC_TPLG_TUPLE_TYPE_BOOL:
105 				{
106 					u16 *val = (u16 *)((u8 *)object + tokens[i].offset +
107 							    offset);
108 
109 					*val = (u16)tuples[j].value.v;
110 					break;
111 				}
112 				case SND_SOC_TPLG_TUPLE_TYPE_STRING:
113 				{
114 					if (!tokens[i].get_token) {
115 						dev_err(scomp->dev,
116 							"get_token not defined for token %d in %s\n",
117 							tokens[i].token, token_list[token_id].name);
118 						return -EINVAL;
119 					}
120 
121 					tokens[i].get_token((void *)tuples[j].value.s, object,
122 							    tokens[i].offset + offset);
123 					break;
124 				}
125 				default:
126 					break;
127 				}
128 
129 				num_tokens_matched++;
130 
131 				/* found all required sets of current token. Move to the next one */
132 				if (!(num_tokens_matched % token_instance_num))
133 					break;
134 
135 				/* move to the next object */
136 				offset += object_size;
137 			}
138 		}
139 	}
140 
141 	return 0;
142 }
143 
144 struct sof_widget_data {
145 	int ctrl_type;
146 	int ipc_cmd;
147 	struct sof_abi_hdr *pdata;
148 	struct snd_sof_control *control;
149 };
150 
151 /* send pcm params ipc */
152 static int ipc_pcm_params(struct snd_sof_widget *swidget, int dir)
153 {
154 	struct sof_ipc_pcm_params_reply ipc_params_reply;
155 	struct snd_soc_component *scomp = swidget->scomp;
156 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
157 	struct sof_ipc_pcm_params pcm;
158 	struct snd_pcm_hw_params *params;
159 	struct snd_sof_pcm *spcm;
160 	int ret;
161 
162 	memset(&pcm, 0, sizeof(pcm));
163 
164 	/* get runtime PCM params using widget's stream name */
165 	spcm = snd_sof_find_spcm_name(scomp, swidget->widget->sname);
166 	if (!spcm) {
167 		dev_err(scomp->dev, "error: cannot find PCM for %s\n",
168 			swidget->widget->name);
169 		return -EINVAL;
170 	}
171 
172 	params = &spcm->params[dir];
173 
174 	/* set IPC PCM params */
175 	pcm.hdr.size = sizeof(pcm);
176 	pcm.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS;
177 	pcm.comp_id = swidget->comp_id;
178 	pcm.params.hdr.size = sizeof(pcm.params);
179 	pcm.params.direction = dir;
180 	pcm.params.sample_valid_bytes = params_width(params) >> 3;
181 	pcm.params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED;
182 	pcm.params.rate = params_rate(params);
183 	pcm.params.channels = params_channels(params);
184 	pcm.params.host_period_bytes = params_period_bytes(params);
185 
186 	/* set format */
187 	switch (params_format(params)) {
188 	case SNDRV_PCM_FORMAT_S16:
189 		pcm.params.frame_fmt = SOF_IPC_FRAME_S16_LE;
190 		break;
191 	case SNDRV_PCM_FORMAT_S24:
192 		pcm.params.frame_fmt = SOF_IPC_FRAME_S24_4LE;
193 		break;
194 	case SNDRV_PCM_FORMAT_S32:
195 		pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE;
196 		break;
197 	default:
198 		return -EINVAL;
199 	}
200 
201 	/* send IPC to the DSP */
202 	ret = sof_ipc_tx_message(sdev->ipc, pcm.hdr.cmd, &pcm, sizeof(pcm),
203 				 &ipc_params_reply, sizeof(ipc_params_reply));
204 	if (ret < 0)
205 		dev_err(scomp->dev, "error: pcm params failed for %s\n",
206 			swidget->widget->name);
207 
208 	return ret;
209 }
210 
211  /* send stream trigger ipc */
212 static int ipc_trigger(struct snd_sof_widget *swidget, int cmd)
213 {
214 	struct snd_soc_component *scomp = swidget->scomp;
215 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
216 	struct sof_ipc_stream stream;
217 	struct sof_ipc_reply reply;
218 	int ret;
219 
220 	/* set IPC stream params */
221 	stream.hdr.size = sizeof(stream);
222 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | cmd;
223 	stream.comp_id = swidget->comp_id;
224 
225 	/* send IPC to the DSP */
226 	ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
227 				 sizeof(stream), &reply, sizeof(reply));
228 	if (ret < 0)
229 		dev_err(scomp->dev, "error: failed to trigger %s\n",
230 			swidget->widget->name);
231 
232 	return ret;
233 }
234 
235 static int sof_keyword_dapm_event(struct snd_soc_dapm_widget *w,
236 				  struct snd_kcontrol *k, int event)
237 {
238 	struct snd_sof_widget *swidget = w->dobj.private;
239 	struct snd_soc_component *scomp;
240 	int stream = SNDRV_PCM_STREAM_CAPTURE;
241 	struct snd_sof_pcm *spcm;
242 	int ret = 0;
243 
244 	if (!swidget)
245 		return 0;
246 
247 	scomp = swidget->scomp;
248 
249 	dev_dbg(scomp->dev, "received event %d for widget %s\n",
250 		event, w->name);
251 
252 	/* get runtime PCM params using widget's stream name */
253 	spcm = snd_sof_find_spcm_name(scomp, swidget->widget->sname);
254 	if (!spcm) {
255 		dev_err(scomp->dev, "error: cannot find PCM for %s\n",
256 			swidget->widget->name);
257 		return -EINVAL;
258 	}
259 
260 	/* process events */
261 	switch (event) {
262 	case SND_SOC_DAPM_PRE_PMU:
263 		if (spcm->stream[stream].suspend_ignored) {
264 			dev_dbg(scomp->dev, "PRE_PMU event ignored, KWD pipeline is already RUNNING\n");
265 			return 0;
266 		}
267 
268 		/* set pcm params */
269 		ret = ipc_pcm_params(swidget, stream);
270 		if (ret < 0) {
271 			dev_err(scomp->dev,
272 				"error: failed to set pcm params for widget %s\n",
273 				swidget->widget->name);
274 			break;
275 		}
276 
277 		/* start trigger */
278 		ret = ipc_trigger(swidget, SOF_IPC_STREAM_TRIG_START);
279 		if (ret < 0)
280 			dev_err(scomp->dev,
281 				"error: failed to trigger widget %s\n",
282 				swidget->widget->name);
283 		break;
284 	case SND_SOC_DAPM_POST_PMD:
285 		if (spcm->stream[stream].suspend_ignored) {
286 			dev_dbg(scomp->dev, "POST_PMD even ignored, KWD pipeline will remain RUNNING\n");
287 			return 0;
288 		}
289 
290 		/* stop trigger */
291 		ret = ipc_trigger(swidget, SOF_IPC_STREAM_TRIG_STOP);
292 		if (ret < 0)
293 			dev_err(scomp->dev,
294 				"error: failed to trigger widget %s\n",
295 				swidget->widget->name);
296 
297 		/* pcm free */
298 		ret = ipc_trigger(swidget, SOF_IPC_STREAM_PCM_FREE);
299 		if (ret < 0)
300 			dev_err(scomp->dev,
301 				"error: failed to trigger widget %s\n",
302 				swidget->widget->name);
303 		break;
304 	default:
305 		break;
306 	}
307 
308 	return ret;
309 }
310 
311 /* event handlers for keyword detect component */
312 static const struct snd_soc_tplg_widget_events sof_kwd_events[] = {
313 	{SOF_KEYWORD_DETECT_DAPM_EVENT, sof_keyword_dapm_event},
314 };
315 
316 static inline int get_tlv_data(const int *p, int tlv[TLV_ITEMS])
317 {
318 	/* we only support dB scale TLV type at the moment */
319 	if ((int)p[SNDRV_CTL_TLVO_TYPE] != SNDRV_CTL_TLVT_DB_SCALE)
320 		return -EINVAL;
321 
322 	/* min value in topology tlv data is multiplied by 100 */
323 	tlv[TLV_MIN] = (int)p[SNDRV_CTL_TLVO_DB_SCALE_MIN] / 100;
324 
325 	/* volume steps */
326 	tlv[TLV_STEP] = (int)(p[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] &
327 				TLV_DB_SCALE_MASK);
328 
329 	/* mute ON/OFF */
330 	if ((p[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] &
331 		TLV_DB_SCALE_MUTE) == 0)
332 		tlv[TLV_MUTE] = 0;
333 	else
334 		tlv[TLV_MUTE] = 1;
335 
336 	return 0;
337 }
338 
339 /*
340  * Function to truncate an unsigned 64-bit number
341  * by x bits and return 32-bit unsigned number. This
342  * function also takes care of rounding while truncating
343  */
344 static inline u32 vol_shift_64(u64 i, u32 x)
345 {
346 	/* do not truncate more than 32 bits */
347 	if (x > 32)
348 		x = 32;
349 
350 	if (x == 0)
351 		return (u32)i;
352 
353 	return (u32)(((i >> (x - 1)) + 1) >> 1);
354 }
355 
356 /*
357  * Function to compute a ^ exp where,
358  * a is a fractional number represented by a fixed-point
359  * integer with a fractional world length of "fwl"
360  * exp is an integer
361  * fwl is the fractional word length
362  * Return value is a fractional number represented by a
363  * fixed-point integer with a fractional word length of "fwl"
364  */
365 static u32 vol_pow32(u32 a, int exp, u32 fwl)
366 {
367 	int i, iter;
368 	u32 power = 1 << fwl;
369 	u64 numerator;
370 
371 	/* if exponent is 0, return 1 */
372 	if (exp == 0)
373 		return power;
374 
375 	/* determine the number of iterations based on the exponent */
376 	if (exp < 0)
377 		iter = exp * -1;
378 	else
379 		iter = exp;
380 
381 	/* mutiply a "iter" times to compute power */
382 	for (i = 0; i < iter; i++) {
383 		/*
384 		 * Product of 2 Qx.fwl fixed-point numbers yields a Q2*x.2*fwl
385 		 * Truncate product back to fwl fractional bits with rounding
386 		 */
387 		power = vol_shift_64((u64)power * a, fwl);
388 	}
389 
390 	if (exp > 0) {
391 		/* if exp is positive, return the result */
392 		return power;
393 	}
394 
395 	/* if exp is negative, return the multiplicative inverse */
396 	numerator = (u64)1 << (fwl << 1);
397 	do_div(numerator, power);
398 
399 	return (u32)numerator;
400 }
401 
402 /*
403  * Function to calculate volume gain from TLV data.
404  * This function can only handle gain steps that are multiples of 0.5 dB
405  */
406 static u32 vol_compute_gain(u32 value, int *tlv)
407 {
408 	int dB_gain;
409 	u32 linear_gain;
410 	int f_step;
411 
412 	/* mute volume */
413 	if (value == 0 && tlv[TLV_MUTE])
414 		return 0;
415 
416 	/*
417 	 * compute dB gain from tlv. tlv_step
418 	 * in topology is multiplied by 100
419 	 */
420 	dB_gain = tlv[TLV_MIN] + (value * tlv[TLV_STEP]) / 100;
421 
422 	/*
423 	 * compute linear gain represented by fixed-point
424 	 * int with VOLUME_FWL fractional bits
425 	 */
426 	linear_gain = vol_pow32(VOL_TWENTIETH_ROOT_OF_TEN, dB_gain, VOLUME_FWL);
427 
428 	/* extract the fractional part of volume step */
429 	f_step = tlv[TLV_STEP] - (tlv[TLV_STEP] / 100);
430 
431 	/* if volume step is an odd multiple of 0.5 dB */
432 	if (f_step == VOL_HALF_DB_STEP && (value & 1))
433 		linear_gain = vol_shift_64((u64)linear_gain *
434 						  VOL_FORTIETH_ROOT_OF_TEN,
435 						  VOLUME_FWL);
436 
437 	return linear_gain;
438 }
439 
440 /*
441  * Set up volume table for kcontrols from tlv data
442  * "size" specifies the number of entries in the table
443  */
444 static int set_up_volume_table(struct snd_sof_control *scontrol,
445 			       int tlv[TLV_ITEMS], int size)
446 {
447 	int j;
448 
449 	/* init the volume table */
450 	scontrol->volume_table = kcalloc(size, sizeof(u32), GFP_KERNEL);
451 	if (!scontrol->volume_table)
452 		return -ENOMEM;
453 
454 	/* populate the volume table */
455 	for (j = 0; j < size ; j++)
456 		scontrol->volume_table[j] = vol_compute_gain(j, tlv);
457 
458 	return 0;
459 }
460 
461 struct sof_dai_types {
462 	const char *name;
463 	enum sof_ipc_dai_type type;
464 };
465 
466 static const struct sof_dai_types sof_dais[] = {
467 	{"SSP", SOF_DAI_INTEL_SSP},
468 	{"HDA", SOF_DAI_INTEL_HDA},
469 	{"DMIC", SOF_DAI_INTEL_DMIC},
470 	{"ALH", SOF_DAI_INTEL_ALH},
471 	{"SAI", SOF_DAI_IMX_SAI},
472 	{"ESAI", SOF_DAI_IMX_ESAI},
473 	{"ACP", SOF_DAI_AMD_BT},
474 	{"ACPSP", SOF_DAI_AMD_SP},
475 	{"ACPDMIC", SOF_DAI_AMD_DMIC},
476 	{"AFE", SOF_DAI_MEDIATEK_AFE},
477 };
478 
479 static enum sof_ipc_dai_type find_dai(const char *name)
480 {
481 	int i;
482 
483 	for (i = 0; i < ARRAY_SIZE(sof_dais); i++) {
484 		if (strcmp(name, sof_dais[i].name) == 0)
485 			return sof_dais[i].type;
486 	}
487 
488 	return SOF_DAI_INTEL_NONE;
489 }
490 
491 /*
492  * Supported Frame format types and lookup, add new ones to end of list.
493  */
494 
495 struct sof_frame_types {
496 	const char *name;
497 	enum sof_ipc_frame frame;
498 };
499 
500 static const struct sof_frame_types sof_frames[] = {
501 	{"s16le", SOF_IPC_FRAME_S16_LE},
502 	{"s24le", SOF_IPC_FRAME_S24_4LE},
503 	{"s32le", SOF_IPC_FRAME_S32_LE},
504 	{"float", SOF_IPC_FRAME_FLOAT},
505 };
506 
507 static enum sof_ipc_frame find_format(const char *name)
508 {
509 	int i;
510 
511 	for (i = 0; i < ARRAY_SIZE(sof_frames); i++) {
512 		if (strcmp(name, sof_frames[i].name) == 0)
513 			return sof_frames[i].frame;
514 	}
515 
516 	/* use s32le if nothing is specified */
517 	return SOF_IPC_FRAME_S32_LE;
518 }
519 
520 struct sof_process_types {
521 	const char *name;
522 	enum sof_ipc_process_type type;
523 	enum sof_comp_type comp_type;
524 };
525 
526 static const struct sof_process_types sof_process[] = {
527 	{"EQFIR", SOF_PROCESS_EQFIR, SOF_COMP_EQ_FIR},
528 	{"EQIIR", SOF_PROCESS_EQIIR, SOF_COMP_EQ_IIR},
529 	{"KEYWORD_DETECT", SOF_PROCESS_KEYWORD_DETECT, SOF_COMP_KEYWORD_DETECT},
530 	{"KPB", SOF_PROCESS_KPB, SOF_COMP_KPB},
531 	{"CHAN_SELECTOR", SOF_PROCESS_CHAN_SELECTOR, SOF_COMP_SELECTOR},
532 	{"MUX", SOF_PROCESS_MUX, SOF_COMP_MUX},
533 	{"DEMUX", SOF_PROCESS_DEMUX, SOF_COMP_DEMUX},
534 	{"DCBLOCK", SOF_PROCESS_DCBLOCK, SOF_COMP_DCBLOCK},
535 	{"SMART_AMP", SOF_PROCESS_SMART_AMP, SOF_COMP_SMART_AMP},
536 };
537 
538 static enum sof_ipc_process_type find_process(const char *name)
539 {
540 	int i;
541 
542 	for (i = 0; i < ARRAY_SIZE(sof_process); i++) {
543 		if (strcmp(name, sof_process[i].name) == 0)
544 			return sof_process[i].type;
545 	}
546 
547 	return SOF_PROCESS_NONE;
548 }
549 
550 static enum sof_comp_type find_process_comp_type(enum sof_ipc_process_type type)
551 {
552 	int i;
553 
554 	for (i = 0; i < ARRAY_SIZE(sof_process); i++) {
555 		if (sof_process[i].type == type)
556 			return sof_process[i].comp_type;
557 	}
558 
559 	return SOF_COMP_NONE;
560 }
561 
562 int get_token_u32(void *elem, void *object, u32 offset)
563 {
564 	struct snd_soc_tplg_vendor_value_elem *velem = elem;
565 	u32 *val = (u32 *)((u8 *)object + offset);
566 
567 	*val = le32_to_cpu(velem->value);
568 	return 0;
569 }
570 
571 int get_token_u16(void *elem, void *object, u32 offset)
572 {
573 	struct snd_soc_tplg_vendor_value_elem *velem = elem;
574 	u16 *val = (u16 *)((u8 *)object + offset);
575 
576 	*val = (u16)le32_to_cpu(velem->value);
577 	return 0;
578 }
579 
580 int get_token_uuid(void *elem, void *object, u32 offset)
581 {
582 	struct snd_soc_tplg_vendor_uuid_elem *velem = elem;
583 	u8 *dst = (u8 *)object + offset;
584 
585 	memcpy(dst, velem->uuid, UUID_SIZE);
586 
587 	return 0;
588 }
589 
590 int get_token_comp_format(void *elem, void *object, u32 offset)
591 {
592 	u32 *val = (u32 *)((u8 *)object + offset);
593 
594 	*val = find_format((const char *)elem);
595 	return 0;
596 }
597 
598 int get_token_dai_type(void *elem, void *object, u32 offset)
599 {
600 	u32 *val = (u32 *)((u8 *)object + offset);
601 
602 	*val = find_dai((const char *)elem);
603 	return 0;
604 }
605 
606 static int get_token_process_type(void *elem, void *object, u32 offset)
607 {
608 	u32 *val = (u32 *)((u8 *)object + offset);
609 
610 	*val = find_process((const char *)elem);
611 	return 0;
612 }
613 
614 /* DAI */
615 static const struct sof_topology_token dai_tokens[] = {
616 	{SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type,
617 		offsetof(struct sof_ipc_comp_dai, type)},
618 	{SOF_TKN_DAI_INDEX, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
619 		offsetof(struct sof_ipc_comp_dai, dai_index)},
620 	{SOF_TKN_DAI_DIRECTION, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
621 		offsetof(struct sof_ipc_comp_dai, direction)},
622 };
623 
624 /* BE DAI link */
625 static const struct sof_topology_token dai_link_tokens[] = {
626 	{SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type,
627 		offsetof(struct sof_ipc_dai_config, type)},
628 	{SOF_TKN_DAI_INDEX, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
629 		offsetof(struct sof_ipc_dai_config, dai_index)},
630 };
631 
632 /* SRC */
633 static const struct sof_topology_token src_tokens[] = {
634 	{SOF_TKN_SRC_RATE_IN, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
635 		offsetof(struct sof_ipc_comp_src, source_rate)},
636 	{SOF_TKN_SRC_RATE_OUT, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
637 		offsetof(struct sof_ipc_comp_src, sink_rate)},
638 };
639 
640 /* ASRC */
641 static const struct sof_topology_token asrc_tokens[] = {
642 	{SOF_TKN_ASRC_RATE_IN, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
643 		offsetof(struct sof_ipc_comp_asrc, source_rate)},
644 	{SOF_TKN_ASRC_RATE_OUT, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
645 		offsetof(struct sof_ipc_comp_asrc, sink_rate)},
646 	{SOF_TKN_ASRC_ASYNCHRONOUS_MODE, SND_SOC_TPLG_TUPLE_TYPE_WORD,
647 		get_token_u32,
648 		offsetof(struct sof_ipc_comp_asrc, asynchronous_mode)},
649 	{SOF_TKN_ASRC_OPERATION_MODE, SND_SOC_TPLG_TUPLE_TYPE_WORD,
650 		get_token_u32,
651 		offsetof(struct sof_ipc_comp_asrc, operation_mode)},
652 };
653 
654 /* Tone */
655 static const struct sof_topology_token tone_tokens[] = {
656 };
657 
658 /* EFFECT */
659 static const struct sof_topology_token process_tokens[] = {
660 	{SOF_TKN_PROCESS_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING,
661 		get_token_process_type,
662 		offsetof(struct sof_ipc_comp_process, type)},
663 };
664 
665 /* PCM */
666 static const struct sof_topology_token stream_tokens[] = {
667 	{SOF_TKN_STREAM_PLAYBACK_COMPATIBLE_D0I3,
668 		SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
669 		offsetof(struct snd_sof_pcm, stream[0].d0i3_compatible)},
670 	{SOF_TKN_STREAM_CAPTURE_COMPATIBLE_D0I3,
671 		SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
672 		offsetof(struct snd_sof_pcm, stream[1].d0i3_compatible)},
673 };
674 
675 /* Generic components */
676 static const struct sof_topology_token comp_tokens[] = {
677 	{SOF_TKN_COMP_PERIOD_SINK_COUNT,
678 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
679 		offsetof(struct sof_ipc_comp_config, periods_sink)},
680 	{SOF_TKN_COMP_PERIOD_SOURCE_COUNT,
681 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
682 		offsetof(struct sof_ipc_comp_config, periods_source)},
683 	{SOF_TKN_COMP_FORMAT,
684 		SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_comp_format,
685 		offsetof(struct sof_ipc_comp_config, frame_fmt)},
686 };
687 
688 /* SSP */
689 static const struct sof_topology_token ssp_tokens[] = {
690 	{SOF_TKN_INTEL_SSP_CLKS_CONTROL,
691 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
692 		offsetof(struct sof_ipc_dai_ssp_params, clks_control)},
693 	{SOF_TKN_INTEL_SSP_MCLK_ID,
694 		SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
695 		offsetof(struct sof_ipc_dai_ssp_params, mclk_id)},
696 	{SOF_TKN_INTEL_SSP_SAMPLE_BITS, SND_SOC_TPLG_TUPLE_TYPE_WORD,
697 		get_token_u32,
698 		offsetof(struct sof_ipc_dai_ssp_params, sample_valid_bits)},
699 	{SOF_TKN_INTEL_SSP_FRAME_PULSE_WIDTH, SND_SOC_TPLG_TUPLE_TYPE_SHORT,
700 		get_token_u16,
701 		offsetof(struct sof_ipc_dai_ssp_params, frame_pulse_width)},
702 	{SOF_TKN_INTEL_SSP_QUIRKS, SND_SOC_TPLG_TUPLE_TYPE_WORD,
703 		get_token_u32,
704 		offsetof(struct sof_ipc_dai_ssp_params, quirks)},
705 	{SOF_TKN_INTEL_SSP_TDM_PADDING_PER_SLOT, SND_SOC_TPLG_TUPLE_TYPE_BOOL,
706 		get_token_u16,
707 		offsetof(struct sof_ipc_dai_ssp_params,
708 			 tdm_per_slot_padding_flag)},
709 	{SOF_TKN_INTEL_SSP_BCLK_DELAY, SND_SOC_TPLG_TUPLE_TYPE_WORD,
710 		get_token_u32,
711 		offsetof(struct sof_ipc_dai_ssp_params, bclk_delay)},
712 
713 };
714 
715 /* ALH */
716 static const struct sof_topology_token alh_tokens[] = {
717 	{SOF_TKN_INTEL_ALH_RATE,
718 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
719 		offsetof(struct sof_ipc_dai_alh_params, rate)},
720 	{SOF_TKN_INTEL_ALH_CH,
721 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
722 		offsetof(struct sof_ipc_dai_alh_params, channels)},
723 };
724 
725 /* DMIC */
726 static const struct sof_topology_token dmic_tokens[] = {
727 	{SOF_TKN_INTEL_DMIC_DRIVER_VERSION,
728 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
729 		offsetof(struct sof_ipc_dai_dmic_params, driver_ipc_version)},
730 	{SOF_TKN_INTEL_DMIC_CLK_MIN,
731 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
732 		offsetof(struct sof_ipc_dai_dmic_params, pdmclk_min)},
733 	{SOF_TKN_INTEL_DMIC_CLK_MAX,
734 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
735 		offsetof(struct sof_ipc_dai_dmic_params, pdmclk_max)},
736 	{SOF_TKN_INTEL_DMIC_SAMPLE_RATE,
737 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
738 		offsetof(struct sof_ipc_dai_dmic_params, fifo_fs)},
739 	{SOF_TKN_INTEL_DMIC_DUTY_MIN,
740 		SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
741 		offsetof(struct sof_ipc_dai_dmic_params, duty_min)},
742 	{SOF_TKN_INTEL_DMIC_DUTY_MAX,
743 		SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
744 		offsetof(struct sof_ipc_dai_dmic_params, duty_max)},
745 	{SOF_TKN_INTEL_DMIC_NUM_PDM_ACTIVE,
746 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
747 		offsetof(struct sof_ipc_dai_dmic_params,
748 			 num_pdm_active)},
749 	{SOF_TKN_INTEL_DMIC_FIFO_WORD_LENGTH,
750 		SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
751 		offsetof(struct sof_ipc_dai_dmic_params, fifo_bits)},
752 	{SOF_TKN_INTEL_DMIC_UNMUTE_RAMP_TIME_MS,
753 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
754 		offsetof(struct sof_ipc_dai_dmic_params, unmute_ramp_time)},
755 
756 };
757 
758 /* ESAI */
759 static const struct sof_topology_token esai_tokens[] = {
760 	{SOF_TKN_IMX_ESAI_MCLK_ID,
761 		SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
762 		offsetof(struct sof_ipc_dai_esai_params, mclk_id)},
763 };
764 
765 /* SAI */
766 static const struct sof_topology_token sai_tokens[] = {
767 	{SOF_TKN_IMX_SAI_MCLK_ID,
768 		SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
769 		offsetof(struct sof_ipc_dai_sai_params, mclk_id)},
770 };
771 
772 /* Core tokens */
773 static const struct sof_topology_token core_tokens[] = {
774 	{SOF_TKN_COMP_CORE_ID,
775 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
776 		offsetof(struct sof_ipc_comp, core)},
777 };
778 
779 /* Component extended tokens */
780 static const struct sof_topology_token comp_ext_tokens[] = {
781 	{SOF_TKN_COMP_UUID,
782 		SND_SOC_TPLG_TUPLE_TYPE_UUID, get_token_uuid,
783 		offsetof(struct snd_sof_widget, uuid)},
784 };
785 
786 /*
787  * DMIC PDM Tokens
788  * SOF_TKN_INTEL_DMIC_PDM_CTRL_ID should be the first token
789  * as it increments the index while parsing the array of pdm tokens
790  * and determines the correct offset
791  */
792 static const struct sof_topology_token dmic_pdm_tokens[] = {
793 	{SOF_TKN_INTEL_DMIC_PDM_CTRL_ID,
794 		SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
795 		offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, id),},
796 	{SOF_TKN_INTEL_DMIC_PDM_MIC_A_Enable,
797 		SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
798 		offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, enable_mic_a)},
799 	{SOF_TKN_INTEL_DMIC_PDM_MIC_B_Enable,
800 		SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
801 		offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, enable_mic_b)},
802 	{SOF_TKN_INTEL_DMIC_PDM_POLARITY_A,
803 		SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
804 		offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, polarity_mic_a)},
805 	{SOF_TKN_INTEL_DMIC_PDM_POLARITY_B,
806 		SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
807 		offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, polarity_mic_b)},
808 	{SOF_TKN_INTEL_DMIC_PDM_CLK_EDGE,
809 		SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
810 		offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, clk_edge)},
811 	{SOF_TKN_INTEL_DMIC_PDM_SKEW,
812 		SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
813 		offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, skew)},
814 };
815 
816 /* HDA */
817 static const struct sof_topology_token hda_tokens[] = {
818 	{SOF_TKN_INTEL_HDA_RATE,
819 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
820 		offsetof(struct sof_ipc_dai_hda_params, rate)},
821 	{SOF_TKN_INTEL_HDA_CH,
822 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
823 		offsetof(struct sof_ipc_dai_hda_params, channels)},
824 };
825 
826 /* Leds */
827 static const struct sof_topology_token led_tokens[] = {
828 	{SOF_TKN_MUTE_LED_USE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
829 	 offsetof(struct snd_sof_led_control, use_led)},
830 	{SOF_TKN_MUTE_LED_DIRECTION, SND_SOC_TPLG_TUPLE_TYPE_WORD,
831 	 get_token_u32, offsetof(struct snd_sof_led_control, direction)},
832 };
833 
834 /* AFE */
835 static const struct sof_topology_token afe_tokens[] = {
836 	{SOF_TKN_MEDIATEK_AFE_RATE,
837 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
838 		offsetof(struct sof_ipc_dai_mtk_afe_params, rate)},
839 	{SOF_TKN_MEDIATEK_AFE_CH,
840 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
841 		offsetof(struct sof_ipc_dai_mtk_afe_params, channels)},
842 	{SOF_TKN_MEDIATEK_AFE_FORMAT,
843 		SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_comp_format,
844 		offsetof(struct sof_ipc_dai_mtk_afe_params, format)},
845 };
846 
847 /**
848  * sof_parse_uuid_tokens - Parse multiple sets of UUID tokens
849  * @scomp: pointer to soc component
850  * @object: target ipc struct for parsed values
851  * @offset: offset within the object pointer
852  * @tokens: array of struct sof_topology_token containing the tokens to be matched
853  * @num_tokens: number of tokens in tokens array
854  * @array: source pointer to consecutive vendor arrays in topology
855  *
856  * This function parses multiple sets of string type tokens in vendor arrays
857  */
858 static int sof_parse_uuid_tokens(struct snd_soc_component *scomp,
859 				  void *object, size_t offset,
860 				  const struct sof_topology_token *tokens, int num_tokens,
861 				  struct snd_soc_tplg_vendor_array *array)
862 {
863 	struct snd_soc_tplg_vendor_uuid_elem *elem;
864 	int found = 0;
865 	int i, j;
866 
867 	/* parse element by element */
868 	for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
869 		elem = &array->uuid[i];
870 
871 		/* search for token */
872 		for (j = 0; j < num_tokens; j++) {
873 			/* match token type */
874 			if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_UUID)
875 				continue;
876 
877 			/* match token id */
878 			if (tokens[j].token != le32_to_cpu(elem->token))
879 				continue;
880 
881 			/* matched - now load token */
882 			tokens[j].get_token(elem, object,
883 					    offset + tokens[j].offset);
884 
885 			found++;
886 		}
887 	}
888 
889 	return found;
890 }
891 
892 /**
893  * sof_copy_tuples - Parse tokens and copy them to the @tuples array
894  * @sdev: pointer to struct snd_sof_dev
895  * @array: source pointer to consecutive vendor arrays in topology
896  * @array_size: size of @array
897  * @token_id: Token ID associated with a token array
898  * @token_instance_num: number of times the same @token_id needs to be parsed i.e. the function
899  *			looks for @token_instance_num of each token in the token array associated
900  *			with the @token_id
901  * @tuples: tuples array to copy the matched tuples to
902  * @tuples_size: size of @tuples
903  * @num_copied_tuples: pointer to the number of copied tuples in the tuples array
904  *
905  */
906 static int sof_copy_tuples(struct snd_sof_dev *sdev, struct snd_soc_tplg_vendor_array *array,
907 			   int array_size, u32 token_id, int token_instance_num,
908 			   struct snd_sof_tuple *tuples, int tuples_size, int *num_copied_tuples)
909 {
910 	const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
911 	const struct sof_token_info *token_list = ipc_tplg_ops->token_list;
912 	const struct sof_topology_token *tokens;
913 	int found = 0;
914 	int num_tokens, asize;
915 	int i, j;
916 
917 	/* nothing to do if token_list is NULL */
918 	if (!token_list)
919 		return 0;
920 
921 	if (!tuples || !num_copied_tuples) {
922 		dev_err(sdev->dev, "Invalid tuples array\n");
923 		return -EINVAL;
924 	}
925 
926 	tokens = token_list[token_id].tokens;
927 	num_tokens = token_list[token_id].count;
928 
929 	if (!tokens) {
930 		dev_err(sdev->dev, "No token array defined for token ID: %d\n", token_id);
931 		return -EINVAL;
932 	}
933 
934 	/* check if there's space in the tuples array for new tokens */
935 	if (*num_copied_tuples >= tuples_size) {
936 		dev_err(sdev->dev, "No space in tuples array for new tokens from %s",
937 			token_list[token_id].name);
938 		return -EINVAL;
939 	}
940 
941 	while (array_size > 0 && found < num_tokens * token_instance_num) {
942 		asize = le32_to_cpu(array->size);
943 
944 		/* validate asize */
945 		if (asize < 0) {
946 			dev_err(sdev->dev, "Invalid array size 0x%x\n", asize);
947 			return -EINVAL;
948 		}
949 
950 		/* make sure there is enough data before parsing */
951 		array_size -= asize;
952 		if (array_size < 0) {
953 			dev_err(sdev->dev, "Invalid array size 0x%x\n", asize);
954 			return -EINVAL;
955 		}
956 
957 		/* parse element by element */
958 		for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
959 			/* search for token */
960 			for (j = 0; j < num_tokens; j++) {
961 				/* match token type */
962 				if (!(tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_WORD ||
963 				      tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_SHORT ||
964 				      tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BYTE ||
965 				      tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BOOL ||
966 				      tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_STRING))
967 					continue;
968 
969 				if (tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_STRING) {
970 					struct snd_soc_tplg_vendor_string_elem *elem;
971 
972 					elem = &array->string[i];
973 
974 					/* match token id */
975 					if (tokens[j].token != le32_to_cpu(elem->token))
976 						continue;
977 
978 					tuples[*num_copied_tuples].token = tokens[j].token;
979 					tuples[*num_copied_tuples].value.s = elem->string;
980 				} else {
981 					struct snd_soc_tplg_vendor_value_elem *elem;
982 
983 					elem = &array->value[i];
984 
985 					/* match token id */
986 					if (tokens[j].token != le32_to_cpu(elem->token))
987 						continue;
988 
989 					tuples[*num_copied_tuples].token = tokens[j].token;
990 					tuples[*num_copied_tuples].value.v =
991 						le32_to_cpu(elem->value);
992 				}
993 				found++;
994 				(*num_copied_tuples)++;
995 
996 				/* stop if there's no space for any more new tuples */
997 				if (*num_copied_tuples == tuples_size)
998 					return 0;
999 			}
1000 		}
1001 
1002 		/* next array */
1003 		array = (struct snd_soc_tplg_vendor_array *)((u8 *)array + asize);
1004 	}
1005 
1006 	return 0;
1007 }
1008 
1009 /**
1010  * sof_parse_string_tokens - Parse multiple sets of tokens
1011  * @scomp: pointer to soc component
1012  * @object: target ipc struct for parsed values
1013  * @offset: offset within the object pointer
1014  * @tokens: array of struct sof_topology_token containing the tokens to be matched
1015  * @num_tokens: number of tokens in tokens array
1016  * @array: source pointer to consecutive vendor arrays in topology
1017  *
1018  * This function parses multiple sets of string type tokens in vendor arrays
1019  */
1020 static int sof_parse_string_tokens(struct snd_soc_component *scomp,
1021 				   void *object, int offset,
1022 				   const struct sof_topology_token *tokens, int num_tokens,
1023 				   struct snd_soc_tplg_vendor_array *array)
1024 {
1025 	struct snd_soc_tplg_vendor_string_elem *elem;
1026 	int found = 0;
1027 	int i, j;
1028 
1029 	/* parse element by element */
1030 	for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
1031 		elem = &array->string[i];
1032 
1033 		/* search for token */
1034 		for (j = 0; j < num_tokens; j++) {
1035 			/* match token type */
1036 			if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_STRING)
1037 				continue;
1038 
1039 			/* match token id */
1040 			if (tokens[j].token != le32_to_cpu(elem->token))
1041 				continue;
1042 
1043 			/* matched - now load token */
1044 			tokens[j].get_token(elem->string, object, offset + tokens[j].offset);
1045 
1046 			found++;
1047 		}
1048 	}
1049 
1050 	return found;
1051 }
1052 
1053 /**
1054  * sof_parse_word_tokens - Parse multiple sets of tokens
1055  * @scomp: pointer to soc component
1056  * @object: target ipc struct for parsed values
1057  * @offset: offset within the object pointer
1058  * @tokens: array of struct sof_topology_token containing the tokens to be matched
1059  * @num_tokens: number of tokens in tokens array
1060  * @array: source pointer to consecutive vendor arrays in topology
1061  *
1062  * This function parses multiple sets of word type tokens in vendor arrays
1063  */
1064 static int sof_parse_word_tokens(struct snd_soc_component *scomp,
1065 				  void *object, int offset,
1066 				  const struct sof_topology_token *tokens, int num_tokens,
1067 				  struct snd_soc_tplg_vendor_array *array)
1068 {
1069 	struct snd_soc_tplg_vendor_value_elem *elem;
1070 	int found = 0;
1071 	int i, j;
1072 
1073 	/* parse element by element */
1074 	for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
1075 		elem = &array->value[i];
1076 
1077 		/* search for token */
1078 		for (j = 0; j < num_tokens; j++) {
1079 			/* match token type */
1080 			if (!(tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_WORD ||
1081 			      tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_SHORT ||
1082 			      tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BYTE ||
1083 			      tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BOOL))
1084 				continue;
1085 
1086 			/* match token id */
1087 			if (tokens[j].token != le32_to_cpu(elem->token))
1088 				continue;
1089 
1090 			/* load token */
1091 			tokens[j].get_token(elem, object, offset + tokens[j].offset);
1092 
1093 			found++;
1094 		}
1095 	}
1096 
1097 	return found;
1098 }
1099 
1100 /**
1101  * sof_parse_token_sets - Parse multiple sets of tokens
1102  * @scomp: pointer to soc component
1103  * @object: target ipc struct for parsed values
1104  * @tokens: token definition array describing what tokens to parse
1105  * @count: number of tokens in definition array
1106  * @array: source pointer to consecutive vendor arrays in topology
1107  * @array_size: total size of @array
1108  * @token_instance_num: number of times the same tokens needs to be parsed i.e. the function
1109  *			looks for @token_instance_num of each token in the @tokens
1110  * @object_size: offset to next target ipc struct with multiple sets
1111  *
1112  * This function parses multiple sets of tokens in vendor arrays into
1113  * consecutive ipc structs.
1114  */
1115 static int sof_parse_token_sets(struct snd_soc_component *scomp,
1116 				void *object, const struct sof_topology_token *tokens,
1117 				int count, struct snd_soc_tplg_vendor_array *array,
1118 				int array_size, int token_instance_num, size_t object_size)
1119 {
1120 	size_t offset = 0;
1121 	int found = 0;
1122 	int total = 0;
1123 	int asize;
1124 
1125 	while (array_size > 0 && total < count * token_instance_num) {
1126 		asize = le32_to_cpu(array->size);
1127 
1128 		/* validate asize */
1129 		if (asize < 0) { /* FIXME: A zero-size array makes no sense */
1130 			dev_err(scomp->dev, "error: invalid array size 0x%x\n",
1131 				asize);
1132 			return -EINVAL;
1133 		}
1134 
1135 		/* make sure there is enough data before parsing */
1136 		array_size -= asize;
1137 		if (array_size < 0) {
1138 			dev_err(scomp->dev, "error: invalid array size 0x%x\n",
1139 				asize);
1140 			return -EINVAL;
1141 		}
1142 
1143 		/* call correct parser depending on type */
1144 		switch (le32_to_cpu(array->type)) {
1145 		case SND_SOC_TPLG_TUPLE_TYPE_UUID:
1146 			found += sof_parse_uuid_tokens(scomp, object, offset, tokens, count,
1147 						       array);
1148 			break;
1149 		case SND_SOC_TPLG_TUPLE_TYPE_STRING:
1150 			found += sof_parse_string_tokens(scomp, object, offset, tokens, count,
1151 							 array);
1152 			break;
1153 		case SND_SOC_TPLG_TUPLE_TYPE_BOOL:
1154 		case SND_SOC_TPLG_TUPLE_TYPE_BYTE:
1155 		case SND_SOC_TPLG_TUPLE_TYPE_WORD:
1156 		case SND_SOC_TPLG_TUPLE_TYPE_SHORT:
1157 			found += sof_parse_word_tokens(scomp, object, offset, tokens, count,
1158 						       array);
1159 			break;
1160 		default:
1161 			dev_err(scomp->dev, "error: unknown token type %d\n",
1162 				array->type);
1163 			return -EINVAL;
1164 		}
1165 
1166 		/* next array */
1167 		array = (struct snd_soc_tplg_vendor_array *)((u8 *)array
1168 			+ asize);
1169 
1170 		/* move to next target struct */
1171 		if (found >= count) {
1172 			offset += object_size;
1173 			total += found;
1174 			found = 0;
1175 		}
1176 	}
1177 
1178 	return 0;
1179 }
1180 
1181 /**
1182  * sof_parse_tokens - Parse one set of tokens
1183  * @scomp: pointer to soc component
1184  * @object: target ipc struct for parsed values
1185  * @tokens: token definition array describing what tokens to parse
1186  * @num_tokens: number of tokens in definition array
1187  * @array: source pointer to consecutive vendor arrays in topology
1188  * @array_size: total size of @array
1189  *
1190  * This function parses a single set of tokens in vendor arrays into
1191  * consecutive ipc structs.
1192  */
1193 static int sof_parse_tokens(struct snd_soc_component *scomp,  void *object,
1194 			    const struct sof_topology_token *tokens, int num_tokens,
1195 			    struct snd_soc_tplg_vendor_array *array,
1196 			    int array_size)
1197 
1198 {
1199 	/*
1200 	 * sof_parse_tokens is used when topology contains only a single set of
1201 	 * identical tuples arrays. So additional parameters to
1202 	 * sof_parse_token_sets are sets = 1 (only 1 set) and
1203 	 * object_size = 0 (irrelevant).
1204 	 */
1205 	return sof_parse_token_sets(scomp, object, tokens, num_tokens, array,
1206 				    array_size, 1, 0);
1207 }
1208 
1209 static void sof_dbg_comp_config(struct snd_soc_component *scomp,
1210 				struct sof_ipc_comp_config *config)
1211 {
1212 	dev_dbg(scomp->dev, " config: periods snk %d src %d fmt %d\n",
1213 		config->periods_sink, config->periods_source,
1214 		config->frame_fmt);
1215 }
1216 
1217 /*
1218  * Standard Kcontrols.
1219  */
1220 
1221 static int sof_control_load_volume(struct snd_soc_component *scomp,
1222 				   struct snd_sof_control *scontrol,
1223 				   struct snd_kcontrol_new *kc,
1224 				   struct snd_soc_tplg_ctl_hdr *hdr)
1225 {
1226 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1227 	struct snd_soc_tplg_mixer_control *mc =
1228 		container_of(hdr, struct snd_soc_tplg_mixer_control, hdr);
1229 	struct sof_ipc_ctrl_data *cdata;
1230 	int tlv[TLV_ITEMS];
1231 	unsigned int i;
1232 	int ret;
1233 
1234 	/* validate topology data */
1235 	if (le32_to_cpu(mc->num_channels) > SND_SOC_TPLG_MAX_CHAN) {
1236 		ret = -EINVAL;
1237 		goto out;
1238 	}
1239 
1240 	/*
1241 	 * If control has more than 2 channels we need to override the info. This is because even if
1242 	 * ASoC layer has defined topology's max channel count to SND_SOC_TPLG_MAX_CHAN = 8, the
1243 	 * pre-defined dapm control types (and related functions) creating the actual control
1244 	 * restrict the channels only to mono or stereo.
1245 	 */
1246 	if (le32_to_cpu(mc->num_channels) > 2)
1247 		kc->info = snd_sof_volume_info;
1248 
1249 	/* init the volume get/put data */
1250 	scontrol->size = struct_size(scontrol->control_data, chanv,
1251 				     le32_to_cpu(mc->num_channels));
1252 	scontrol->control_data = kzalloc(scontrol->size, GFP_KERNEL);
1253 	if (!scontrol->control_data) {
1254 		ret = -ENOMEM;
1255 		goto out;
1256 	}
1257 
1258 	scontrol->comp_id = sdev->next_comp_id;
1259 	scontrol->min_volume_step = le32_to_cpu(mc->min);
1260 	scontrol->max_volume_step = le32_to_cpu(mc->max);
1261 	scontrol->num_channels = le32_to_cpu(mc->num_channels);
1262 	scontrol->control_data->index = kc->index;
1263 
1264 	/* set cmd for mixer control */
1265 	if (le32_to_cpu(mc->max) == 1) {
1266 		scontrol->control_data->cmd = SOF_CTRL_CMD_SWITCH;
1267 		goto skip;
1268 	}
1269 
1270 	scontrol->control_data->cmd = SOF_CTRL_CMD_VOLUME;
1271 
1272 	/* extract tlv data */
1273 	if (!kc->tlv.p || get_tlv_data(kc->tlv.p, tlv) < 0) {
1274 		dev_err(scomp->dev, "error: invalid TLV data\n");
1275 		ret = -EINVAL;
1276 		goto out_free;
1277 	}
1278 
1279 	/* set up volume table */
1280 	ret = set_up_volume_table(scontrol, tlv, le32_to_cpu(mc->max) + 1);
1281 	if (ret < 0) {
1282 		dev_err(scomp->dev, "error: setting up volume table\n");
1283 		goto out_free;
1284 	}
1285 
1286 	/* set default volume values to 0dB in control */
1287 	cdata = scontrol->control_data;
1288 	for (i = 0; i < scontrol->num_channels; i++) {
1289 		cdata->chanv[i].channel = i;
1290 		cdata->chanv[i].value = VOL_ZERO_DB;
1291 	}
1292 
1293 skip:
1294 	/* set up possible led control from mixer private data */
1295 	ret = sof_parse_tokens(scomp, &scontrol->led_ctl, led_tokens,
1296 			       ARRAY_SIZE(led_tokens), mc->priv.array,
1297 			       le32_to_cpu(mc->priv.size));
1298 	if (ret != 0) {
1299 		dev_err(scomp->dev, "error: parse led tokens failed %d\n",
1300 			le32_to_cpu(mc->priv.size));
1301 		goto out_free_table;
1302 	}
1303 
1304 	dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d\n",
1305 		scontrol->comp_id, scontrol->num_channels);
1306 
1307 	return 0;
1308 
1309 out_free_table:
1310 	if (le32_to_cpu(mc->max) > 1)
1311 		kfree(scontrol->volume_table);
1312 out_free:
1313 	kfree(scontrol->control_data);
1314 out:
1315 	return ret;
1316 }
1317 
1318 static int sof_control_load_enum(struct snd_soc_component *scomp,
1319 				 struct snd_sof_control *scontrol,
1320 				 struct snd_kcontrol_new *kc,
1321 				 struct snd_soc_tplg_ctl_hdr *hdr)
1322 {
1323 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1324 	struct snd_soc_tplg_enum_control *ec =
1325 		container_of(hdr, struct snd_soc_tplg_enum_control, hdr);
1326 
1327 	/* validate topology data */
1328 	if (le32_to_cpu(ec->num_channels) > SND_SOC_TPLG_MAX_CHAN)
1329 		return -EINVAL;
1330 
1331 	/* init the enum get/put data */
1332 	scontrol->size = struct_size(scontrol->control_data, chanv,
1333 				     le32_to_cpu(ec->num_channels));
1334 	scontrol->control_data = kzalloc(scontrol->size, GFP_KERNEL);
1335 	if (!scontrol->control_data)
1336 		return -ENOMEM;
1337 
1338 	scontrol->comp_id = sdev->next_comp_id;
1339 	scontrol->num_channels = le32_to_cpu(ec->num_channels);
1340 	scontrol->control_data->index = kc->index;
1341 	scontrol->control_data->cmd = SOF_CTRL_CMD_ENUM;
1342 
1343 	dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d comp_id %d\n",
1344 		scontrol->comp_id, scontrol->num_channels, scontrol->comp_id);
1345 
1346 	return 0;
1347 }
1348 
1349 static int sof_control_load_bytes(struct snd_soc_component *scomp,
1350 				  struct snd_sof_control *scontrol,
1351 				  struct snd_kcontrol_new *kc,
1352 				  struct snd_soc_tplg_ctl_hdr *hdr)
1353 {
1354 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1355 	struct sof_ipc_ctrl_data *cdata;
1356 	struct snd_soc_tplg_bytes_control *control =
1357 		container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
1358 	struct soc_bytes_ext *sbe = (struct soc_bytes_ext *)kc->private_value;
1359 	size_t max_size = sbe->max;
1360 	size_t priv_size = le32_to_cpu(control->priv.size);
1361 	int ret;
1362 
1363 	if (max_size < sizeof(struct sof_ipc_ctrl_data) ||
1364 	    max_size < sizeof(struct sof_abi_hdr)) {
1365 		ret = -EINVAL;
1366 		goto out;
1367 	}
1368 
1369 	/* init the get/put bytes data */
1370 	if (priv_size > max_size - sizeof(struct sof_ipc_ctrl_data)) {
1371 		dev_err(scomp->dev, "err: bytes data size %zu exceeds max %zu.\n",
1372 			priv_size, max_size - sizeof(struct sof_ipc_ctrl_data));
1373 		ret = -EINVAL;
1374 		goto out;
1375 	}
1376 
1377 	scontrol->size = sizeof(struct sof_ipc_ctrl_data) + priv_size;
1378 
1379 	scontrol->control_data = kzalloc(max_size, GFP_KERNEL);
1380 	cdata = scontrol->control_data;
1381 	if (!scontrol->control_data) {
1382 		ret = -ENOMEM;
1383 		goto out;
1384 	}
1385 
1386 	scontrol->comp_id = sdev->next_comp_id;
1387 	scontrol->control_data->cmd = SOF_CTRL_CMD_BINARY;
1388 	scontrol->control_data->index = kc->index;
1389 
1390 	dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d\n",
1391 		scontrol->comp_id, scontrol->num_channels);
1392 
1393 	if (le32_to_cpu(control->priv.size) > 0) {
1394 		memcpy(cdata->data, control->priv.data,
1395 		       le32_to_cpu(control->priv.size));
1396 
1397 		if (cdata->data->magic != SOF_ABI_MAGIC) {
1398 			dev_err(scomp->dev, "error: Wrong ABI magic 0x%08x.\n",
1399 				cdata->data->magic);
1400 			ret = -EINVAL;
1401 			goto out_free;
1402 		}
1403 		if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION,
1404 						 cdata->data->abi)) {
1405 			dev_err(scomp->dev,
1406 				"error: Incompatible ABI version 0x%08x.\n",
1407 				cdata->data->abi);
1408 			ret = -EINVAL;
1409 			goto out_free;
1410 		}
1411 		if (cdata->data->size + sizeof(struct sof_abi_hdr) !=
1412 		    le32_to_cpu(control->priv.size)) {
1413 			dev_err(scomp->dev,
1414 				"error: Conflict in bytes vs. priv size.\n");
1415 			ret = -EINVAL;
1416 			goto out_free;
1417 		}
1418 	}
1419 
1420 	return 0;
1421 
1422 out_free:
1423 	kfree(scontrol->control_data);
1424 out:
1425 	return ret;
1426 }
1427 
1428 /* external kcontrol init - used for any driver specific init */
1429 static int sof_control_load(struct snd_soc_component *scomp, int index,
1430 			    struct snd_kcontrol_new *kc,
1431 			    struct snd_soc_tplg_ctl_hdr *hdr)
1432 {
1433 	struct soc_mixer_control *sm;
1434 	struct soc_bytes_ext *sbe;
1435 	struct soc_enum *se;
1436 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1437 	struct snd_soc_dobj *dobj;
1438 	struct snd_sof_control *scontrol;
1439 	int ret;
1440 
1441 	dev_dbg(scomp->dev, "tplg: load control type %d name : %s\n",
1442 		hdr->type, hdr->name);
1443 
1444 	scontrol = kzalloc(sizeof(*scontrol), GFP_KERNEL);
1445 	if (!scontrol)
1446 		return -ENOMEM;
1447 
1448 	scontrol->scomp = scomp;
1449 	scontrol->access = kc->access;
1450 
1451 	switch (le32_to_cpu(hdr->ops.info)) {
1452 	case SND_SOC_TPLG_CTL_VOLSW:
1453 	case SND_SOC_TPLG_CTL_VOLSW_SX:
1454 	case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1455 		sm = (struct soc_mixer_control *)kc->private_value;
1456 		dobj = &sm->dobj;
1457 		ret = sof_control_load_volume(scomp, scontrol, kc, hdr);
1458 		break;
1459 	case SND_SOC_TPLG_CTL_BYTES:
1460 		sbe = (struct soc_bytes_ext *)kc->private_value;
1461 		dobj = &sbe->dobj;
1462 		ret = sof_control_load_bytes(scomp, scontrol, kc, hdr);
1463 		break;
1464 	case SND_SOC_TPLG_CTL_ENUM:
1465 	case SND_SOC_TPLG_CTL_ENUM_VALUE:
1466 		se = (struct soc_enum *)kc->private_value;
1467 		dobj = &se->dobj;
1468 		ret = sof_control_load_enum(scomp, scontrol, kc, hdr);
1469 		break;
1470 	case SND_SOC_TPLG_CTL_RANGE:
1471 	case SND_SOC_TPLG_CTL_STROBE:
1472 	case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1473 	case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1474 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1475 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1476 	case SND_SOC_TPLG_DAPM_CTL_PIN:
1477 	default:
1478 		dev_warn(scomp->dev, "control type not supported %d:%d:%d\n",
1479 			 hdr->ops.get, hdr->ops.put, hdr->ops.info);
1480 		kfree(scontrol);
1481 		return 0;
1482 	}
1483 
1484 	if (ret < 0) {
1485 		kfree(scontrol);
1486 		return ret;
1487 	}
1488 
1489 	scontrol->led_ctl.led_value = -1;
1490 
1491 	dobj->private = scontrol;
1492 	list_add(&scontrol->list, &sdev->kcontrol_list);
1493 	return 0;
1494 }
1495 
1496 static int sof_control_unload(struct snd_soc_component *scomp,
1497 			      struct snd_soc_dobj *dobj)
1498 {
1499 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1500 	struct sof_ipc_free fcomp;
1501 	struct snd_sof_control *scontrol = dobj->private;
1502 
1503 	dev_dbg(scomp->dev, "tplg: unload control name : %s\n", scomp->name);
1504 
1505 	fcomp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_FREE;
1506 	fcomp.hdr.size = sizeof(fcomp);
1507 	fcomp.id = scontrol->comp_id;
1508 
1509 	kfree(scontrol->control_data);
1510 	list_del(&scontrol->list);
1511 	kfree(scontrol);
1512 	/* send IPC to the DSP */
1513 	return sof_ipc_tx_message(sdev->ipc,
1514 				  fcomp.hdr.cmd, &fcomp, sizeof(fcomp),
1515 				  NULL, 0);
1516 }
1517 
1518 /*
1519  * DAI Topology
1520  */
1521 
1522 static int sof_connect_dai_widget(struct snd_soc_component *scomp,
1523 				  struct snd_soc_dapm_widget *w,
1524 				  struct snd_soc_tplg_dapm_widget *tw,
1525 				  struct snd_sof_dai *dai)
1526 {
1527 	struct snd_soc_card *card = scomp->card;
1528 	struct snd_soc_pcm_runtime *rtd;
1529 	struct snd_soc_dai *cpu_dai;
1530 	int i;
1531 
1532 	list_for_each_entry(rtd, &card->rtd_list, list) {
1533 		dev_vdbg(scomp->dev, "tplg: check widget: %s stream: %s dai stream: %s\n",
1534 			 w->name,  w->sname, rtd->dai_link->stream_name);
1535 
1536 		if (!w->sname || !rtd->dai_link->stream_name)
1537 			continue;
1538 
1539 		/* does stream match DAI link ? */
1540 		if (strcmp(w->sname, rtd->dai_link->stream_name))
1541 			continue;
1542 
1543 		switch (w->id) {
1544 		case snd_soc_dapm_dai_out:
1545 			for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1546 				/*
1547 				 * Please create DAI widget in the right order
1548 				 * to ensure BE will connect to the right DAI
1549 				 * widget.
1550 				 */
1551 				if (!cpu_dai->capture_widget) {
1552 					cpu_dai->capture_widget = w;
1553 					break;
1554 				}
1555 			}
1556 			if (i == rtd->num_cpus) {
1557 				dev_err(scomp->dev, "error: can't find BE for DAI %s\n",
1558 					w->name);
1559 
1560 				return -EINVAL;
1561 			}
1562 			dai->name = rtd->dai_link->name;
1563 			dev_dbg(scomp->dev, "tplg: connected widget %s -> DAI link %s\n",
1564 				w->name, rtd->dai_link->name);
1565 			break;
1566 		case snd_soc_dapm_dai_in:
1567 			for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1568 				/*
1569 				 * Please create DAI widget in the right order
1570 				 * to ensure BE will connect to the right DAI
1571 				 * widget.
1572 				 */
1573 				if (!cpu_dai->playback_widget) {
1574 					cpu_dai->playback_widget = w;
1575 					break;
1576 				}
1577 			}
1578 			if (i == rtd->num_cpus) {
1579 				dev_err(scomp->dev, "error: can't find BE for DAI %s\n",
1580 					w->name);
1581 
1582 				return -EINVAL;
1583 			}
1584 			dai->name = rtd->dai_link->name;
1585 			dev_dbg(scomp->dev, "tplg: connected widget %s -> DAI link %s\n",
1586 				w->name, rtd->dai_link->name);
1587 			break;
1588 		default:
1589 			break;
1590 		}
1591 	}
1592 
1593 	/* check we have a connection */
1594 	if (!dai->name) {
1595 		dev_err(scomp->dev, "error: can't connect DAI %s stream %s\n",
1596 			w->name, w->sname);
1597 		return -EINVAL;
1598 	}
1599 
1600 	return 0;
1601 }
1602 
1603 /**
1604  * sof_comp_alloc - allocate and initialize buffer for a new component
1605  * @swidget: pointer to struct snd_sof_widget containing extended data
1606  * @ipc_size: IPC payload size that will be updated depending on valid
1607  *  extended data.
1608  * @index: ID of the pipeline the component belongs to
1609  *
1610  * Return: The pointer to the new allocated component, NULL if failed.
1611  */
1612 static struct sof_ipc_comp *sof_comp_alloc(struct snd_sof_widget *swidget, size_t *ipc_size,
1613 					   int index)
1614 {
1615 	struct sof_ipc_comp *comp;
1616 	size_t total_size = *ipc_size;
1617 	size_t ext_size = sizeof(swidget->uuid);
1618 
1619 	/* only non-zero UUID is valid */
1620 	if (!guid_is_null(&swidget->uuid))
1621 		total_size += ext_size;
1622 
1623 	comp = kzalloc(total_size, GFP_KERNEL);
1624 	if (!comp)
1625 		return NULL;
1626 
1627 	/* configure comp new IPC message */
1628 	comp->hdr.size = total_size;
1629 	comp->hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1630 	comp->id = swidget->comp_id;
1631 	comp->pipeline_id = index;
1632 	comp->core = swidget->core;
1633 
1634 	/* handle the extended data if needed */
1635 	if (total_size > *ipc_size) {
1636 		/* append extended data to the end of the component */
1637 		memcpy((u8 *)comp + *ipc_size, &swidget->uuid, ext_size);
1638 		comp->ext_data_length = ext_size;
1639 	}
1640 
1641 	/* update ipc_size and return */
1642 	*ipc_size = total_size;
1643 	return comp;
1644 }
1645 
1646 static int sof_widget_load_dai(struct snd_soc_component *scomp, int index,
1647 			       struct snd_sof_widget *swidget,
1648 			       struct snd_soc_tplg_dapm_widget *tw,
1649 			       struct snd_sof_dai *dai)
1650 {
1651 	struct snd_soc_tplg_private *private = &tw->priv;
1652 	struct sof_dai_private_data *dai_data;
1653 	struct sof_ipc_comp_dai *comp_dai;
1654 	size_t ipc_size = sizeof(*comp_dai);
1655 	int ret;
1656 
1657 	dai_data = kzalloc(sizeof(*dai_data), GFP_KERNEL);
1658 	if (!dai_data)
1659 		return -ENOMEM;
1660 
1661 	comp_dai = (struct sof_ipc_comp_dai *)
1662 		   sof_comp_alloc(swidget, &ipc_size, index);
1663 	if (!comp_dai) {
1664 		ret = -ENOMEM;
1665 		goto free;
1666 	}
1667 
1668 	/* configure dai IPC message */
1669 	comp_dai->comp.type = SOF_COMP_DAI;
1670 	comp_dai->config.hdr.size = sizeof(comp_dai->config);
1671 
1672 	ret = sof_parse_tokens(scomp, comp_dai, dai_tokens,
1673 			       ARRAY_SIZE(dai_tokens), private->array,
1674 			       le32_to_cpu(private->size));
1675 	if (ret != 0) {
1676 		dev_err(scomp->dev, "error: parse dai tokens failed %d\n",
1677 			le32_to_cpu(private->size));
1678 		goto free;
1679 	}
1680 
1681 	ret = sof_parse_tokens(scomp, &comp_dai->config, comp_tokens,
1682 			       ARRAY_SIZE(comp_tokens), private->array,
1683 			       le32_to_cpu(private->size));
1684 	if (ret != 0) {
1685 		dev_err(scomp->dev, "error: parse dai.cfg tokens failed %d\n",
1686 			private->size);
1687 		goto free;
1688 	}
1689 
1690 	dev_dbg(scomp->dev, "dai %s: type %d index %d\n",
1691 		swidget->widget->name, comp_dai->type, comp_dai->dai_index);
1692 	sof_dbg_comp_config(scomp, &comp_dai->config);
1693 
1694 	if (dai) {
1695 		dai->scomp = scomp;
1696 		dai_data->comp_dai = comp_dai;
1697 		dai->private = dai_data;
1698 	}
1699 
1700 	return 0;
1701 
1702 free:
1703 	kfree(dai_data);
1704 	return ret;
1705 }
1706 
1707 /* bind PCM ID to host component ID */
1708 static int spcm_bind(struct snd_soc_component *scomp, struct snd_sof_pcm *spcm,
1709 		     int dir)
1710 {
1711 	struct snd_sof_widget *host_widget;
1712 
1713 	host_widget = snd_sof_find_swidget_sname(scomp,
1714 						 spcm->pcm.caps[dir].name,
1715 						 dir);
1716 	if (!host_widget) {
1717 		dev_err(scomp->dev, "can't find host comp to bind pcm\n");
1718 		return -EINVAL;
1719 	}
1720 
1721 	spcm->stream[dir].comp_id = host_widget->comp_id;
1722 
1723 	return 0;
1724 }
1725 
1726 static int sof_widget_parse_tokens(struct snd_soc_component *scomp, struct snd_sof_widget *swidget,
1727 				   struct snd_soc_tplg_dapm_widget *tw,
1728 				   enum sof_tokens *object_token_list, int count)
1729 {
1730 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1731 	const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
1732 	const struct sof_token_info *token_list = ipc_tplg_ops->token_list;
1733 	struct snd_soc_tplg_private *private = &tw->priv;
1734 	int num_tuples = 0;
1735 	size_t size;
1736 	int ret, i;
1737 
1738 	if (count > 0 && !object_token_list) {
1739 		dev_err(scomp->dev, "No token list for widget %s\n", swidget->widget->name);
1740 		return -EINVAL;
1741 	}
1742 
1743 	/* calculate max size of tuples array */
1744 	for (i = 0; i < count; i++)
1745 		num_tuples += token_list[object_token_list[i]].count;
1746 
1747 	/* allocate memory for tuples array */
1748 	size = sizeof(struct snd_sof_tuple) * num_tuples;
1749 	swidget->tuples = kzalloc(size, GFP_KERNEL);
1750 	if (!swidget->tuples)
1751 		return -ENOMEM;
1752 
1753 	/* parse token list for widget */
1754 	for (i = 0; i < count; i++) {
1755 		if (object_token_list[i] >= SOF_TOKEN_COUNT) {
1756 			dev_err(scomp->dev, "Invalid token id %d for widget %s\n",
1757 				object_token_list[i], swidget->widget->name);
1758 			ret = -EINVAL;
1759 			goto err;
1760 		}
1761 
1762 		/* parse and save UUID in swidget */
1763 		if (object_token_list[i] == SOF_COMP_EXT_TOKENS) {
1764 			ret = sof_parse_tokens(scomp, swidget,
1765 					       token_list[object_token_list[i]].tokens,
1766 					       token_list[object_token_list[i]].count,
1767 					       private->array, le32_to_cpu(private->size));
1768 			if (ret < 0) {
1769 				dev_err(scomp->dev, "Failed parsing %s for widget %s\n",
1770 					token_list[object_token_list[i]].name,
1771 					swidget->widget->name);
1772 				goto err;
1773 			}
1774 
1775 			continue;
1776 		}
1777 
1778 		/* copy one set of tuples per token ID into swidget->tuples */
1779 		ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size),
1780 				      object_token_list[i], 1, swidget->tuples,
1781 				      num_tuples, &swidget->num_tuples);
1782 		if (ret < 0) {
1783 			dev_err(scomp->dev, "Failed parsing %s for widget %s err: %d\n",
1784 				token_list[object_token_list[i]].name, swidget->widget->name, ret);
1785 			goto err;
1786 		}
1787 	}
1788 
1789 	return 0;
1790 err:
1791 	kfree(swidget->tuples);
1792 	return ret;
1793 }
1794 
1795 /*
1796  * SRC Topology
1797  */
1798 
1799 static int sof_widget_load_src(struct snd_soc_component *scomp, int index,
1800 			       struct snd_sof_widget *swidget,
1801 			       struct snd_soc_tplg_dapm_widget *tw)
1802 {
1803 	struct snd_soc_tplg_private *private = &tw->priv;
1804 	struct sof_ipc_comp_src *src;
1805 	size_t ipc_size = sizeof(*src);
1806 	int ret;
1807 
1808 	src = (struct sof_ipc_comp_src *)
1809 	      sof_comp_alloc(swidget, &ipc_size, index);
1810 	if (!src)
1811 		return -ENOMEM;
1812 
1813 	/* configure src IPC message */
1814 	src->comp.type = SOF_COMP_SRC;
1815 	src->config.hdr.size = sizeof(src->config);
1816 
1817 	ret = sof_parse_tokens(scomp, src, src_tokens,
1818 			       ARRAY_SIZE(src_tokens), private->array,
1819 			       le32_to_cpu(private->size));
1820 	if (ret != 0) {
1821 		dev_err(scomp->dev, "error: parse src tokens failed %d\n",
1822 			private->size);
1823 		goto err;
1824 	}
1825 
1826 	ret = sof_parse_tokens(scomp, &src->config, comp_tokens,
1827 			       ARRAY_SIZE(comp_tokens), private->array,
1828 			       le32_to_cpu(private->size));
1829 	if (ret != 0) {
1830 		dev_err(scomp->dev, "error: parse src.cfg tokens failed %d\n",
1831 			le32_to_cpu(private->size));
1832 		goto err;
1833 	}
1834 
1835 	dev_dbg(scomp->dev, "src %s: source rate %d sink rate %d\n",
1836 		swidget->widget->name, src->source_rate, src->sink_rate);
1837 	sof_dbg_comp_config(scomp, &src->config);
1838 
1839 	swidget->private = src;
1840 
1841 	return 0;
1842 err:
1843 	kfree(src);
1844 	return ret;
1845 }
1846 
1847 /*
1848  * ASRC Topology
1849  */
1850 
1851 static int sof_widget_load_asrc(struct snd_soc_component *scomp, int index,
1852 				struct snd_sof_widget *swidget,
1853 				struct snd_soc_tplg_dapm_widget *tw)
1854 {
1855 	struct snd_soc_tplg_private *private = &tw->priv;
1856 	struct sof_ipc_comp_asrc *asrc;
1857 	size_t ipc_size = sizeof(*asrc);
1858 	int ret;
1859 
1860 	asrc = (struct sof_ipc_comp_asrc *)
1861 	       sof_comp_alloc(swidget, &ipc_size, index);
1862 	if (!asrc)
1863 		return -ENOMEM;
1864 
1865 	/* configure ASRC IPC message */
1866 	asrc->comp.type = SOF_COMP_ASRC;
1867 	asrc->config.hdr.size = sizeof(asrc->config);
1868 
1869 	ret = sof_parse_tokens(scomp, asrc, asrc_tokens,
1870 			       ARRAY_SIZE(asrc_tokens), private->array,
1871 			       le32_to_cpu(private->size));
1872 	if (ret != 0) {
1873 		dev_err(scomp->dev, "error: parse asrc tokens failed %d\n",
1874 			private->size);
1875 		goto err;
1876 	}
1877 
1878 	ret = sof_parse_tokens(scomp, &asrc->config, comp_tokens,
1879 			       ARRAY_SIZE(comp_tokens), private->array,
1880 			       le32_to_cpu(private->size));
1881 	if (ret != 0) {
1882 		dev_err(scomp->dev, "error: parse asrc.cfg tokens failed %d\n",
1883 			le32_to_cpu(private->size));
1884 		goto err;
1885 	}
1886 
1887 	dev_dbg(scomp->dev, "asrc %s: source rate %d sink rate %d "
1888 		"asynch %d operation %d\n",
1889 		swidget->widget->name, asrc->source_rate, asrc->sink_rate,
1890 		asrc->asynchronous_mode, asrc->operation_mode);
1891 	sof_dbg_comp_config(scomp, &asrc->config);
1892 
1893 	swidget->private = asrc;
1894 
1895 	return 0;
1896 err:
1897 	kfree(asrc);
1898 	return ret;
1899 }
1900 
1901 /*
1902  * Signal Generator Topology
1903  */
1904 
1905 static int sof_widget_load_siggen(struct snd_soc_component *scomp, int index,
1906 				  struct snd_sof_widget *swidget,
1907 				  struct snd_soc_tplg_dapm_widget *tw)
1908 {
1909 	struct snd_soc_tplg_private *private = &tw->priv;
1910 	struct sof_ipc_comp_tone *tone;
1911 	size_t ipc_size = sizeof(*tone);
1912 	int ret;
1913 
1914 	tone = (struct sof_ipc_comp_tone *)
1915 	       sof_comp_alloc(swidget, &ipc_size, index);
1916 	if (!tone)
1917 		return -ENOMEM;
1918 
1919 	/* configure siggen IPC message */
1920 	tone->comp.type = SOF_COMP_TONE;
1921 	tone->config.hdr.size = sizeof(tone->config);
1922 
1923 	ret = sof_parse_tokens(scomp, tone, tone_tokens,
1924 			       ARRAY_SIZE(tone_tokens), private->array,
1925 			       le32_to_cpu(private->size));
1926 	if (ret != 0) {
1927 		dev_err(scomp->dev, "error: parse tone tokens failed %d\n",
1928 			le32_to_cpu(private->size));
1929 		goto err;
1930 	}
1931 
1932 	ret = sof_parse_tokens(scomp, &tone->config, comp_tokens,
1933 			       ARRAY_SIZE(comp_tokens), private->array,
1934 			       le32_to_cpu(private->size));
1935 	if (ret != 0) {
1936 		dev_err(scomp->dev, "error: parse tone.cfg tokens failed %d\n",
1937 			le32_to_cpu(private->size));
1938 		goto err;
1939 	}
1940 
1941 	dev_dbg(scomp->dev, "tone %s: frequency %d amplitude %d\n",
1942 		swidget->widget->name, tone->frequency, tone->amplitude);
1943 	sof_dbg_comp_config(scomp, &tone->config);
1944 
1945 	swidget->private = tone;
1946 
1947 	return 0;
1948 err:
1949 	kfree(tone);
1950 	return ret;
1951 }
1952 
1953 static int sof_get_control_data(struct snd_soc_component *scomp,
1954 				struct snd_soc_dapm_widget *widget,
1955 				struct sof_widget_data *wdata,
1956 				size_t *size)
1957 {
1958 	const struct snd_kcontrol_new *kc;
1959 	struct soc_mixer_control *sm;
1960 	struct soc_bytes_ext *sbe;
1961 	struct soc_enum *se;
1962 	int i;
1963 
1964 	*size = 0;
1965 
1966 	for (i = 0; i < widget->num_kcontrols; i++) {
1967 		kc = &widget->kcontrol_news[i];
1968 
1969 		switch (widget->dobj.widget.kcontrol_type[i]) {
1970 		case SND_SOC_TPLG_TYPE_MIXER:
1971 			sm = (struct soc_mixer_control *)kc->private_value;
1972 			wdata[i].control = sm->dobj.private;
1973 			break;
1974 		case SND_SOC_TPLG_TYPE_BYTES:
1975 			sbe = (struct soc_bytes_ext *)kc->private_value;
1976 			wdata[i].control = sbe->dobj.private;
1977 			break;
1978 		case SND_SOC_TPLG_TYPE_ENUM:
1979 			se = (struct soc_enum *)kc->private_value;
1980 			wdata[i].control = se->dobj.private;
1981 			break;
1982 		default:
1983 			dev_err(scomp->dev, "error: unknown kcontrol type %u in widget %s\n",
1984 				widget->dobj.widget.kcontrol_type[i],
1985 				widget->name);
1986 			return -EINVAL;
1987 		}
1988 
1989 		if (!wdata[i].control) {
1990 			dev_err(scomp->dev, "error: no scontrol for widget %s\n",
1991 				widget->name);
1992 			return -EINVAL;
1993 		}
1994 
1995 		wdata[i].pdata = wdata[i].control->control_data->data;
1996 		if (!wdata[i].pdata)
1997 			return -EINVAL;
1998 
1999 		/* make sure data is valid - data can be updated at runtime */
2000 		if (widget->dobj.widget.kcontrol_type[i] == SND_SOC_TPLG_TYPE_BYTES &&
2001 		    wdata[i].pdata->magic != SOF_ABI_MAGIC)
2002 			return -EINVAL;
2003 
2004 		*size += wdata[i].pdata->size;
2005 
2006 		/* get data type */
2007 		switch (wdata[i].control->control_data->cmd) {
2008 		case SOF_CTRL_CMD_VOLUME:
2009 		case SOF_CTRL_CMD_ENUM:
2010 		case SOF_CTRL_CMD_SWITCH:
2011 			wdata[i].ipc_cmd = SOF_IPC_COMP_SET_VALUE;
2012 			wdata[i].ctrl_type = SOF_CTRL_TYPE_VALUE_CHAN_SET;
2013 			break;
2014 		case SOF_CTRL_CMD_BINARY:
2015 			wdata[i].ipc_cmd = SOF_IPC_COMP_SET_DATA;
2016 			wdata[i].ctrl_type = SOF_CTRL_TYPE_DATA_SET;
2017 			break;
2018 		default:
2019 			break;
2020 		}
2021 	}
2022 
2023 	return 0;
2024 }
2025 
2026 static int sof_process_load(struct snd_soc_component *scomp, int index,
2027 			    struct snd_sof_widget *swidget,
2028 			    struct snd_soc_tplg_dapm_widget *tw,
2029 			    int type)
2030 {
2031 	struct snd_soc_dapm_widget *widget = swidget->widget;
2032 	struct snd_soc_tplg_private *private = &tw->priv;
2033 	struct sof_ipc_comp_process *process;
2034 	struct sof_widget_data *wdata = NULL;
2035 	size_t ipc_data_size = 0;
2036 	size_t ipc_size;
2037 	int offset = 0;
2038 	int ret;
2039 	int i;
2040 
2041 	/* allocate struct for widget control data sizes and types */
2042 	if (widget->num_kcontrols) {
2043 		wdata = kcalloc(widget->num_kcontrols,
2044 				sizeof(*wdata),
2045 				GFP_KERNEL);
2046 
2047 		if (!wdata)
2048 			return -ENOMEM;
2049 
2050 		/* get possible component controls and get size of all pdata */
2051 		ret = sof_get_control_data(scomp, widget, wdata,
2052 					   &ipc_data_size);
2053 
2054 		if (ret < 0)
2055 			goto out;
2056 	}
2057 
2058 	ipc_size = sizeof(struct sof_ipc_comp_process) + ipc_data_size;
2059 
2060 	/* we are exceeding max ipc size, config needs to be sent separately */
2061 	if (ipc_size > SOF_IPC_MSG_MAX_SIZE) {
2062 		ipc_size -= ipc_data_size;
2063 		ipc_data_size = 0;
2064 	}
2065 
2066 	process = (struct sof_ipc_comp_process *)
2067 		  sof_comp_alloc(swidget, &ipc_size, index);
2068 	if (!process) {
2069 		ret = -ENOMEM;
2070 		goto out;
2071 	}
2072 
2073 	/* configure iir IPC message */
2074 	process->comp.type = type;
2075 	process->config.hdr.size = sizeof(process->config);
2076 
2077 	ret = sof_parse_tokens(scomp, &process->config, comp_tokens,
2078 			       ARRAY_SIZE(comp_tokens), private->array,
2079 			       le32_to_cpu(private->size));
2080 	if (ret != 0) {
2081 		dev_err(scomp->dev, "error: parse process.cfg tokens failed %d\n",
2082 			le32_to_cpu(private->size));
2083 		goto err;
2084 	}
2085 
2086 	sof_dbg_comp_config(scomp, &process->config);
2087 
2088 	/*
2089 	 * found private data in control, so copy it.
2090 	 * get possible component controls - get size of all pdata,
2091 	 * then memcpy with headers
2092 	 */
2093 	if (ipc_data_size) {
2094 		for (i = 0; i < widget->num_kcontrols; i++) {
2095 			memcpy(&process->data[offset],
2096 			       wdata[i].pdata->data,
2097 			       wdata[i].pdata->size);
2098 			offset += wdata[i].pdata->size;
2099 		}
2100 	}
2101 
2102 	process->size = ipc_data_size;
2103 	swidget->private = process;
2104 err:
2105 	if (ret < 0)
2106 		kfree(process);
2107 out:
2108 	kfree(wdata);
2109 	return ret;
2110 }
2111 
2112 /*
2113  * Processing Component Topology - can be "effect", "codec", or general
2114  * "processing".
2115  */
2116 
2117 static int sof_widget_load_process(struct snd_soc_component *scomp, int index,
2118 				   struct snd_sof_widget *swidget,
2119 				   struct snd_soc_tplg_dapm_widget *tw)
2120 {
2121 	struct snd_soc_tplg_private *private = &tw->priv;
2122 	struct sof_ipc_comp_process config;
2123 	int ret;
2124 
2125 	/* check we have some tokens - we need at least process type */
2126 	if (le32_to_cpu(private->size) == 0) {
2127 		dev_err(scomp->dev, "error: process tokens not found\n");
2128 		return -EINVAL;
2129 	}
2130 
2131 	memset(&config, 0, sizeof(config));
2132 	config.comp.core = swidget->core;
2133 
2134 	/* get the process token */
2135 	ret = sof_parse_tokens(scomp, &config, process_tokens,
2136 			       ARRAY_SIZE(process_tokens), private->array,
2137 			       le32_to_cpu(private->size));
2138 	if (ret != 0) {
2139 		dev_err(scomp->dev, "error: parse process tokens failed %d\n",
2140 			le32_to_cpu(private->size));
2141 		return ret;
2142 	}
2143 
2144 	/* now load process specific data and send IPC */
2145 	ret = sof_process_load(scomp, index, swidget, tw, find_process_comp_type(config.type));
2146 	if (ret < 0) {
2147 		dev_err(scomp->dev, "error: process loading failed\n");
2148 		return ret;
2149 	}
2150 
2151 	return 0;
2152 }
2153 
2154 static int sof_widget_bind_event(struct snd_soc_component *scomp,
2155 				 struct snd_sof_widget *swidget,
2156 				 u16 event_type)
2157 {
2158 	struct sof_ipc_comp *ipc_comp;
2159 
2160 	/* validate widget event type */
2161 	switch (event_type) {
2162 	case SOF_KEYWORD_DETECT_DAPM_EVENT:
2163 		/* only KEYWORD_DETECT comps should handle this */
2164 		if (swidget->id != snd_soc_dapm_effect)
2165 			break;
2166 
2167 		ipc_comp = swidget->private;
2168 		if (ipc_comp && ipc_comp->type != SOF_COMP_KEYWORD_DETECT)
2169 			break;
2170 
2171 		/* bind event to keyword detect comp */
2172 		return snd_soc_tplg_widget_bind_event(swidget->widget,
2173 						      sof_kwd_events,
2174 						      ARRAY_SIZE(sof_kwd_events),
2175 						      event_type);
2176 	default:
2177 		break;
2178 	}
2179 
2180 	dev_err(scomp->dev,
2181 		"error: invalid event type %d for widget %s\n",
2182 		event_type, swidget->widget->name);
2183 	return -EINVAL;
2184 }
2185 
2186 /* external widget init - used for any driver specific init */
2187 static int sof_widget_ready(struct snd_soc_component *scomp, int index,
2188 			    struct snd_soc_dapm_widget *w,
2189 			    struct snd_soc_tplg_dapm_widget *tw)
2190 {
2191 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2192 	const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
2193 	const struct sof_ipc_tplg_widget_ops *widget_ops = ipc_tplg_ops->widget;
2194 	struct snd_sof_widget *swidget;
2195 	struct snd_sof_dai *dai;
2196 	enum sof_tokens *token_list;
2197 	int token_list_size;
2198 	struct sof_ipc_comp comp = {
2199 		.core = SOF_DSP_PRIMARY_CORE,
2200 	};
2201 	int ret = 0;
2202 
2203 	swidget = kzalloc(sizeof(*swidget), GFP_KERNEL);
2204 	if (!swidget)
2205 		return -ENOMEM;
2206 
2207 	swidget->scomp = scomp;
2208 	swidget->widget = w;
2209 	swidget->comp_id = sdev->next_comp_id++;
2210 	swidget->complete = 0;
2211 	swidget->id = w->id;
2212 	swidget->pipeline_id = index;
2213 	swidget->private = NULL;
2214 
2215 	dev_dbg(scomp->dev, "tplg: ready widget id %d pipe %d type %d name : %s stream %s\n",
2216 		swidget->comp_id, index, swidget->id, tw->name,
2217 		strnlen(tw->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0
2218 			? tw->sname : "none");
2219 
2220 	token_list = widget_ops[w->id].token_list;
2221 	token_list_size = widget_ops[w->id].token_list_size;
2222 
2223 	ret = sof_parse_tokens(scomp, &comp, core_tokens,
2224 			       ARRAY_SIZE(core_tokens), tw->priv.array,
2225 			       le32_to_cpu(tw->priv.size));
2226 	if (ret != 0) {
2227 		dev_err(scomp->dev, "error: parsing core tokens failed %d\n",
2228 			ret);
2229 		kfree(swidget);
2230 		return ret;
2231 	}
2232 
2233 	if (sof_debug_check_flag(SOF_DBG_DISABLE_MULTICORE))
2234 		comp.core = SOF_DSP_PRIMARY_CORE;
2235 
2236 	swidget->core = comp.core;
2237 
2238 	ret = sof_parse_tokens(scomp, swidget, comp_ext_tokens, ARRAY_SIZE(comp_ext_tokens),
2239 			       tw->priv.array, le32_to_cpu(tw->priv.size));
2240 	if (ret != 0) {
2241 		dev_err(scomp->dev, "error: parsing comp_ext_tokens failed %d\n",
2242 			ret);
2243 		kfree(swidget);
2244 		return ret;
2245 	}
2246 
2247 	/* handle any special case widgets */
2248 	switch (w->id) {
2249 	case snd_soc_dapm_dai_in:
2250 	case snd_soc_dapm_dai_out:
2251 		dai = kzalloc(sizeof(*dai), GFP_KERNEL);
2252 		if (!dai) {
2253 			kfree(swidget);
2254 			return -ENOMEM;
2255 		}
2256 
2257 		ret = sof_widget_load_dai(scomp, index, swidget, tw, dai);
2258 		if (!ret)
2259 			ret = sof_connect_dai_widget(scomp, w, tw, dai);
2260 		if (ret < 0) {
2261 			kfree(dai);
2262 			break;
2263 		}
2264 		list_add(&dai->list, &sdev->dai_list);
2265 		swidget->private = dai;
2266 		break;
2267 	case snd_soc_dapm_pga:
2268 		if (!le32_to_cpu(tw->num_kcontrols)) {
2269 			dev_err(scomp->dev, "invalid kcontrol count %d for volume\n",
2270 				tw->num_kcontrols);
2271 			ret = -EINVAL;
2272 			break;
2273 		}
2274 
2275 		fallthrough;
2276 	case snd_soc_dapm_mixer:
2277 	case snd_soc_dapm_buffer:
2278 	case snd_soc_dapm_scheduler:
2279 	case snd_soc_dapm_aif_out:
2280 	case snd_soc_dapm_aif_in:
2281 	case snd_soc_dapm_mux:
2282 	case snd_soc_dapm_demux:
2283 		ret = sof_widget_parse_tokens(scomp, swidget, tw,  token_list, token_list_size);
2284 		break;
2285 	case snd_soc_dapm_src:
2286 		ret = sof_widget_load_src(scomp, index, swidget, tw);
2287 		break;
2288 	case snd_soc_dapm_asrc:
2289 		ret = sof_widget_load_asrc(scomp, index, swidget, tw);
2290 		break;
2291 	case snd_soc_dapm_siggen:
2292 		ret = sof_widget_load_siggen(scomp, index, swidget, tw);
2293 		break;
2294 	case snd_soc_dapm_effect:
2295 		ret = sof_widget_load_process(scomp, index, swidget, tw);
2296 		break;
2297 	case snd_soc_dapm_switch:
2298 	case snd_soc_dapm_dai_link:
2299 	case snd_soc_dapm_kcontrol:
2300 	default:
2301 		dev_dbg(scomp->dev, "widget type %d name %s not handled\n", swidget->id, tw->name);
2302 		break;
2303 	}
2304 
2305 	/* check IPC reply */
2306 	if (ret < 0) {
2307 		dev_err(scomp->dev,
2308 			"error: failed to add widget id %d type %d name : %s stream %s\n",
2309 			tw->shift, swidget->id, tw->name,
2310 			strnlen(tw->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0
2311 				? tw->sname : "none");
2312 		kfree(swidget);
2313 		return ret;
2314 	}
2315 
2316 	/* bind widget to external event */
2317 	if (tw->event_type) {
2318 		ret = sof_widget_bind_event(scomp, swidget,
2319 					    le16_to_cpu(tw->event_type));
2320 		if (ret) {
2321 			dev_err(scomp->dev, "error: widget event binding failed\n");
2322 			kfree(swidget->private);
2323 			kfree(swidget->tuples);
2324 			kfree(swidget);
2325 			return ret;
2326 		}
2327 	}
2328 
2329 	w->dobj.private = swidget;
2330 	list_add(&swidget->list, &sdev->widget_list);
2331 	return ret;
2332 }
2333 
2334 static int sof_route_unload(struct snd_soc_component *scomp,
2335 			    struct snd_soc_dobj *dobj)
2336 {
2337 	struct snd_sof_route *sroute;
2338 
2339 	sroute = dobj->private;
2340 	if (!sroute)
2341 		return 0;
2342 
2343 	/* free sroute and its private data */
2344 	kfree(sroute->private);
2345 	list_del(&sroute->list);
2346 	kfree(sroute);
2347 
2348 	return 0;
2349 }
2350 
2351 static int sof_widget_unload(struct snd_soc_component *scomp,
2352 			     struct snd_soc_dobj *dobj)
2353 {
2354 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2355 	const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
2356 	const struct sof_ipc_tplg_widget_ops *widget_ops = ipc_tplg_ops->widget;
2357 	const struct snd_kcontrol_new *kc;
2358 	struct snd_soc_dapm_widget *widget;
2359 	struct snd_sof_control *scontrol;
2360 	struct snd_sof_widget *swidget;
2361 	struct soc_mixer_control *sm;
2362 	struct soc_bytes_ext *sbe;
2363 	struct snd_sof_dai *dai;
2364 	struct soc_enum *se;
2365 	int ret = 0;
2366 	int i;
2367 
2368 	swidget = dobj->private;
2369 	if (!swidget)
2370 		return 0;
2371 
2372 	widget = swidget->widget;
2373 
2374 	switch (swidget->id) {
2375 	case snd_soc_dapm_dai_in:
2376 	case snd_soc_dapm_dai_out:
2377 		dai = swidget->private;
2378 
2379 		if (dai) {
2380 			struct sof_dai_private_data *dai_data = dai->private;
2381 
2382 			kfree(dai_data->comp_dai);
2383 			kfree(dai_data->dai_config);
2384 			kfree(dai_data);
2385 			list_del(&dai->list);
2386 		}
2387 		break;
2388 	default:
2389 		break;
2390 	}
2391 	for (i = 0; i < widget->num_kcontrols; i++) {
2392 		kc = &widget->kcontrol_news[i];
2393 		switch (widget->dobj.widget.kcontrol_type[i]) {
2394 		case SND_SOC_TPLG_TYPE_MIXER:
2395 			sm = (struct soc_mixer_control *)kc->private_value;
2396 			scontrol = sm->dobj.private;
2397 			if (sm->max > 1)
2398 				kfree(scontrol->volume_table);
2399 			break;
2400 		case SND_SOC_TPLG_TYPE_ENUM:
2401 			se = (struct soc_enum *)kc->private_value;
2402 			scontrol = se->dobj.private;
2403 			break;
2404 		case SND_SOC_TPLG_TYPE_BYTES:
2405 			sbe = (struct soc_bytes_ext *)kc->private_value;
2406 			scontrol = sbe->dobj.private;
2407 			break;
2408 		default:
2409 			dev_warn(scomp->dev, "unsupported kcontrol_type\n");
2410 			goto out;
2411 		}
2412 		kfree(scontrol->control_data);
2413 		list_del(&scontrol->list);
2414 		kfree(scontrol);
2415 	}
2416 
2417 out:
2418 	/* free IPC related data */
2419 	if (widget_ops[swidget->id].ipc_free)
2420 		widget_ops[swidget->id].ipc_free(swidget);
2421 
2422 	/* free private value */
2423 	kfree(swidget->private);
2424 
2425 	kfree(swidget->tuples);
2426 
2427 	/* remove and free swidget object */
2428 	list_del(&swidget->list);
2429 	kfree(swidget);
2430 
2431 	return ret;
2432 }
2433 
2434 /*
2435  * DAI HW configuration.
2436  */
2437 
2438 /* FE DAI - used for any driver specific init */
2439 static int sof_dai_load(struct snd_soc_component *scomp, int index,
2440 			struct snd_soc_dai_driver *dai_drv,
2441 			struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
2442 {
2443 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2444 	struct snd_soc_tplg_stream_caps *caps;
2445 	struct snd_soc_tplg_private *private = &pcm->priv;
2446 	struct snd_sof_pcm *spcm;
2447 	int stream;
2448 	int ret;
2449 
2450 	/* nothing to do for BEs atm */
2451 	if (!pcm)
2452 		return 0;
2453 
2454 	spcm = kzalloc(sizeof(*spcm), GFP_KERNEL);
2455 	if (!spcm)
2456 		return -ENOMEM;
2457 
2458 	spcm->scomp = scomp;
2459 
2460 	for_each_pcm_streams(stream) {
2461 		spcm->stream[stream].comp_id = COMP_ID_UNASSIGNED;
2462 		if (pcm->compress)
2463 			snd_sof_compr_init_elapsed_work(&spcm->stream[stream].period_elapsed_work);
2464 		else
2465 			snd_sof_pcm_init_elapsed_work(&spcm->stream[stream].period_elapsed_work);
2466 	}
2467 
2468 	spcm->pcm = *pcm;
2469 	dev_dbg(scomp->dev, "tplg: load pcm %s\n", pcm->dai_name);
2470 
2471 	dai_drv->dobj.private = spcm;
2472 	list_add(&spcm->list, &sdev->pcm_list);
2473 
2474 	ret = sof_parse_tokens(scomp, spcm, stream_tokens,
2475 			       ARRAY_SIZE(stream_tokens), private->array,
2476 			       le32_to_cpu(private->size));
2477 	if (ret) {
2478 		dev_err(scomp->dev, "error: parse stream tokens failed %d\n",
2479 			le32_to_cpu(private->size));
2480 		return ret;
2481 	}
2482 
2483 	/* do we need to allocate playback PCM DMA pages */
2484 	if (!spcm->pcm.playback)
2485 		goto capture;
2486 
2487 	stream = SNDRV_PCM_STREAM_PLAYBACK;
2488 
2489 	dev_vdbg(scomp->dev, "tplg: pcm %s stream tokens: playback d0i3:%d\n",
2490 		 spcm->pcm.pcm_name, spcm->stream[stream].d0i3_compatible);
2491 
2492 	caps = &spcm->pcm.caps[stream];
2493 
2494 	/* allocate playback page table buffer */
2495 	ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev,
2496 				  PAGE_SIZE, &spcm->stream[stream].page_table);
2497 	if (ret < 0) {
2498 		dev_err(scomp->dev, "error: can't alloc page table for %s %d\n",
2499 			caps->name, ret);
2500 
2501 		return ret;
2502 	}
2503 
2504 	/* bind pcm to host comp */
2505 	ret = spcm_bind(scomp, spcm, stream);
2506 	if (ret) {
2507 		dev_err(scomp->dev,
2508 			"error: can't bind pcm to host\n");
2509 		goto free_playback_tables;
2510 	}
2511 
2512 capture:
2513 	stream = SNDRV_PCM_STREAM_CAPTURE;
2514 
2515 	/* do we need to allocate capture PCM DMA pages */
2516 	if (!spcm->pcm.capture)
2517 		return ret;
2518 
2519 	dev_vdbg(scomp->dev, "tplg: pcm %s stream tokens: capture d0i3:%d\n",
2520 		 spcm->pcm.pcm_name, spcm->stream[stream].d0i3_compatible);
2521 
2522 	caps = &spcm->pcm.caps[stream];
2523 
2524 	/* allocate capture page table buffer */
2525 	ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev,
2526 				  PAGE_SIZE, &spcm->stream[stream].page_table);
2527 	if (ret < 0) {
2528 		dev_err(scomp->dev, "error: can't alloc page table for %s %d\n",
2529 			caps->name, ret);
2530 		goto free_playback_tables;
2531 	}
2532 
2533 	/* bind pcm to host comp */
2534 	ret = spcm_bind(scomp, spcm, stream);
2535 	if (ret) {
2536 		dev_err(scomp->dev,
2537 			"error: can't bind pcm to host\n");
2538 		snd_dma_free_pages(&spcm->stream[stream].page_table);
2539 		goto free_playback_tables;
2540 	}
2541 
2542 	return ret;
2543 
2544 free_playback_tables:
2545 	if (spcm->pcm.playback)
2546 		snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table);
2547 
2548 	return ret;
2549 }
2550 
2551 static int sof_dai_unload(struct snd_soc_component *scomp,
2552 			  struct snd_soc_dobj *dobj)
2553 {
2554 	struct snd_sof_pcm *spcm = dobj->private;
2555 
2556 	/* free PCM DMA pages */
2557 	if (spcm->pcm.playback)
2558 		snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table);
2559 
2560 	if (spcm->pcm.capture)
2561 		snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_CAPTURE].page_table);
2562 
2563 	/* remove from list and free spcm */
2564 	list_del(&spcm->list);
2565 	kfree(spcm);
2566 
2567 	return 0;
2568 }
2569 
2570 static void sof_dai_set_format(struct snd_soc_tplg_hw_config *hw_config,
2571 			       struct sof_ipc_dai_config *config)
2572 {
2573 	/* clock directions wrt codec */
2574 	if (hw_config->bclk_provider == SND_SOC_TPLG_BCLK_CP) {
2575 		/* codec is bclk provider */
2576 		if (hw_config->fsync_provider == SND_SOC_TPLG_FSYNC_CP)
2577 			config->format |= SOF_DAI_FMT_CBP_CFP;
2578 		else
2579 			config->format |= SOF_DAI_FMT_CBP_CFC;
2580 	} else {
2581 		/* codec is bclk consumer */
2582 		if (hw_config->fsync_provider == SND_SOC_TPLG_FSYNC_CP)
2583 			config->format |= SOF_DAI_FMT_CBC_CFP;
2584 		else
2585 			config->format |= SOF_DAI_FMT_CBC_CFC;
2586 	}
2587 
2588 	/* inverted clocks ? */
2589 	if (hw_config->invert_bclk) {
2590 		if (hw_config->invert_fsync)
2591 			config->format |= SOF_DAI_FMT_IB_IF;
2592 		else
2593 			config->format |= SOF_DAI_FMT_IB_NF;
2594 	} else {
2595 		if (hw_config->invert_fsync)
2596 			config->format |= SOF_DAI_FMT_NB_IF;
2597 		else
2598 			config->format |= SOF_DAI_FMT_NB_NF;
2599 	}
2600 }
2601 
2602 /*
2603  * Send IPC and set the same config for all DAIs with name matching the link
2604  * name. Note that the function can only be used for the case that all DAIs
2605  * have a common DAI config for now.
2606  */
2607 static int sof_set_dai_config_multi(struct snd_sof_dev *sdev, u32 size,
2608 				    struct snd_soc_dai_link *link,
2609 				    struct sof_ipc_dai_config *config,
2610 				    int num_conf, int curr_conf)
2611 {
2612 	struct sof_dai_private_data *dai_data;
2613 	struct snd_sof_dai *dai;
2614 	int found = 0;
2615 	int i;
2616 
2617 	list_for_each_entry(dai, &sdev->dai_list, list) {
2618 		dai_data = dai->private;
2619 		if (!dai->name)
2620 			continue;
2621 
2622 		if (strcmp(link->name, dai->name) == 0) {
2623 			/*
2624 			 * the same dai config will be applied to all DAIs in
2625 			 * the same dai link. We have to ensure that the ipc
2626 			 * dai config's dai_index match to the component's
2627 			 * dai_index.
2628 			 */
2629 			for (i = 0; i < num_conf; i++)
2630 				config[i].dai_index = dai_data->comp_dai->dai_index;
2631 
2632 			dev_dbg(sdev->dev, "set DAI config for %s index %d\n",
2633 				dai->name, config[curr_conf].dai_index);
2634 
2635 			dai->number_configs = num_conf;
2636 			dai->current_config = curr_conf;
2637 			dai_data->dai_config = kmemdup(config, size * num_conf, GFP_KERNEL);
2638 			if (!dai_data->dai_config)
2639 				return -ENOMEM;
2640 
2641 			found = 1;
2642 		}
2643 	}
2644 
2645 	/*
2646 	 * machine driver may define a dai link with playback and capture
2647 	 * dai enabled, but the dai link in topology would support both, one
2648 	 * or none of them. Here print a warning message to notify user
2649 	 */
2650 	if (!found) {
2651 		dev_warn(sdev->dev, "warning: failed to find dai for dai link %s",
2652 			 link->name);
2653 	}
2654 
2655 	return 0;
2656 }
2657 
2658 static int sof_set_dai_config(struct snd_sof_dev *sdev, u32 size,
2659 			      struct snd_soc_dai_link *link,
2660 			      struct sof_ipc_dai_config *config)
2661 {
2662 	return sof_set_dai_config_multi(sdev, size, link, config, 1, 0);
2663 }
2664 
2665 static int sof_link_ssp_load(struct snd_soc_component *scomp, int index,
2666 			     struct snd_soc_dai_link *link,
2667 			     struct snd_soc_tplg_link_config *cfg,
2668 			     struct snd_soc_tplg_hw_config *hw_config,
2669 			     struct sof_ipc_dai_config *config, int curr_conf)
2670 {
2671 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2672 	struct snd_soc_tplg_private *private = &cfg->priv;
2673 	int num_conf = le32_to_cpu(cfg->num_hw_configs);
2674 	u32 size = sizeof(*config);
2675 	int ret;
2676 	int i;
2677 
2678 	/*
2679 	 * Parse common data, we should have 1 common data per hw_config.
2680 	 */
2681 	ret = sof_parse_token_sets(scomp, &config->ssp, ssp_tokens,
2682 				   ARRAY_SIZE(ssp_tokens), private->array,
2683 				   le32_to_cpu(private->size),
2684 				   num_conf, size);
2685 
2686 	if (ret != 0) {
2687 		dev_err(scomp->dev, "error: parse ssp tokens failed %d\n",
2688 			le32_to_cpu(private->size));
2689 		return ret;
2690 	}
2691 
2692 	/* process all possible hw configs */
2693 	for (i = 0; i < num_conf; i++) {
2694 
2695 		/* handle master/slave and inverted clocks */
2696 		sof_dai_set_format(&hw_config[i], &config[i]);
2697 
2698 		config[i].hdr.size = size;
2699 
2700 		/* copy differentiating hw configs to ipc structs */
2701 		config[i].ssp.mclk_rate = le32_to_cpu(hw_config[i].mclk_rate);
2702 		config[i].ssp.bclk_rate = le32_to_cpu(hw_config[i].bclk_rate);
2703 		config[i].ssp.fsync_rate = le32_to_cpu(hw_config[i].fsync_rate);
2704 		config[i].ssp.tdm_slots = le32_to_cpu(hw_config[i].tdm_slots);
2705 		config[i].ssp.tdm_slot_width = le32_to_cpu(hw_config[i].tdm_slot_width);
2706 		config[i].ssp.mclk_direction = hw_config[i].mclk_direction;
2707 		config[i].ssp.rx_slots = le32_to_cpu(hw_config[i].rx_slots);
2708 		config[i].ssp.tx_slots = le32_to_cpu(hw_config[i].tx_slots);
2709 
2710 		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",
2711 			config[i].dai_index, config[i].format,
2712 			config[i].ssp.mclk_rate, config[i].ssp.bclk_rate,
2713 			config[i].ssp.fsync_rate, config[i].ssp.sample_valid_bits,
2714 			config[i].ssp.tdm_slot_width, config[i].ssp.tdm_slots,
2715 			config[i].ssp.mclk_id, config[i].ssp.quirks, config[i].ssp.clks_control);
2716 
2717 		/* validate SSP fsync rate and channel count */
2718 		if (config[i].ssp.fsync_rate < 8000 || config[i].ssp.fsync_rate > 192000) {
2719 			dev_err(scomp->dev, "error: invalid fsync rate for SSP%d\n",
2720 				config[i].dai_index);
2721 			return -EINVAL;
2722 		}
2723 
2724 		if (config[i].ssp.tdm_slots < 1 || config[i].ssp.tdm_slots > 8) {
2725 			dev_err(scomp->dev, "error: invalid channel count for SSP%d\n",
2726 				config[i].dai_index);
2727 			return -EINVAL;
2728 		}
2729 	}
2730 
2731 	/* set config for all DAI's with name matching the link name */
2732 	ret = sof_set_dai_config_multi(sdev, size, link, config, num_conf, curr_conf);
2733 	if (ret < 0)
2734 		dev_err(scomp->dev, "error: failed to save DAI config for SSP%d\n",
2735 			config->dai_index);
2736 
2737 	return ret;
2738 }
2739 
2740 static int sof_link_sai_load(struct snd_soc_component *scomp, int index,
2741 			     struct snd_soc_dai_link *link,
2742 			     struct snd_soc_tplg_link_config *cfg,
2743 			     struct snd_soc_tplg_hw_config *hw_config,
2744 			     struct sof_ipc_dai_config *config)
2745 {
2746 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2747 	struct snd_soc_tplg_private *private = &cfg->priv;
2748 	u32 size = sizeof(*config);
2749 	int ret;
2750 
2751 	/* handle master/slave and inverted clocks */
2752 	sof_dai_set_format(hw_config, config);
2753 
2754 	/* init IPC */
2755 	memset(&config->sai, 0, sizeof(struct sof_ipc_dai_sai_params));
2756 	config->hdr.size = size;
2757 
2758 	ret = sof_parse_tokens(scomp, &config->sai, sai_tokens,
2759 			       ARRAY_SIZE(sai_tokens), private->array,
2760 			       le32_to_cpu(private->size));
2761 	if (ret != 0) {
2762 		dev_err(scomp->dev, "error: parse sai tokens failed %d\n",
2763 			le32_to_cpu(private->size));
2764 		return ret;
2765 	}
2766 
2767 	config->sai.mclk_rate = le32_to_cpu(hw_config->mclk_rate);
2768 	config->sai.bclk_rate = le32_to_cpu(hw_config->bclk_rate);
2769 	config->sai.fsync_rate = le32_to_cpu(hw_config->fsync_rate);
2770 	config->sai.mclk_direction = hw_config->mclk_direction;
2771 
2772 	config->sai.tdm_slots = le32_to_cpu(hw_config->tdm_slots);
2773 	config->sai.tdm_slot_width = le32_to_cpu(hw_config->tdm_slot_width);
2774 	config->sai.rx_slots = le32_to_cpu(hw_config->rx_slots);
2775 	config->sai.tx_slots = le32_to_cpu(hw_config->tx_slots);
2776 
2777 	dev_info(scomp->dev,
2778 		 "tplg: config SAI%d fmt 0x%x mclk %d width %d slots %d mclk id %d\n",
2779 		config->dai_index, config->format,
2780 		config->sai.mclk_rate, config->sai.tdm_slot_width,
2781 		config->sai.tdm_slots, config->sai.mclk_id);
2782 
2783 	if (config->sai.tdm_slots < 1 || config->sai.tdm_slots > 8) {
2784 		dev_err(scomp->dev, "error: invalid channel count for SAI%d\n",
2785 			config->dai_index);
2786 		return -EINVAL;
2787 	}
2788 
2789 	/* set config for all DAI's with name matching the link name */
2790 	ret = sof_set_dai_config(sdev, size, link, config);
2791 	if (ret < 0)
2792 		dev_err(scomp->dev, "error: failed to save DAI config for SAI%d\n",
2793 			config->dai_index);
2794 
2795 	return ret;
2796 }
2797 
2798 static int sof_link_esai_load(struct snd_soc_component *scomp, int index,
2799 			      struct snd_soc_dai_link *link,
2800 			      struct snd_soc_tplg_link_config *cfg,
2801 			      struct snd_soc_tplg_hw_config *hw_config,
2802 			      struct sof_ipc_dai_config *config)
2803 {
2804 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2805 	struct snd_soc_tplg_private *private = &cfg->priv;
2806 	u32 size = sizeof(*config);
2807 	int ret;
2808 
2809 	/* handle master/slave and inverted clocks */
2810 	sof_dai_set_format(hw_config, config);
2811 
2812 	/* init IPC */
2813 	memset(&config->esai, 0, sizeof(struct sof_ipc_dai_esai_params));
2814 	config->hdr.size = size;
2815 
2816 	ret = sof_parse_tokens(scomp, &config->esai, esai_tokens,
2817 			       ARRAY_SIZE(esai_tokens), private->array,
2818 			       le32_to_cpu(private->size));
2819 	if (ret != 0) {
2820 		dev_err(scomp->dev, "error: parse esai tokens failed %d\n",
2821 			le32_to_cpu(private->size));
2822 		return ret;
2823 	}
2824 
2825 	config->esai.mclk_rate = le32_to_cpu(hw_config->mclk_rate);
2826 	config->esai.bclk_rate = le32_to_cpu(hw_config->bclk_rate);
2827 	config->esai.fsync_rate = le32_to_cpu(hw_config->fsync_rate);
2828 	config->esai.mclk_direction = hw_config->mclk_direction;
2829 	config->esai.tdm_slots = le32_to_cpu(hw_config->tdm_slots);
2830 	config->esai.tdm_slot_width = le32_to_cpu(hw_config->tdm_slot_width);
2831 	config->esai.rx_slots = le32_to_cpu(hw_config->rx_slots);
2832 	config->esai.tx_slots = le32_to_cpu(hw_config->tx_slots);
2833 
2834 	dev_info(scomp->dev,
2835 		 "tplg: config ESAI%d fmt 0x%x mclk %d width %d slots %d mclk id %d\n",
2836 		config->dai_index, config->format,
2837 		config->esai.mclk_rate, config->esai.tdm_slot_width,
2838 		config->esai.tdm_slots, config->esai.mclk_id);
2839 
2840 	if (config->esai.tdm_slots < 1 || config->esai.tdm_slots > 8) {
2841 		dev_err(scomp->dev, "error: invalid channel count for ESAI%d\n",
2842 			config->dai_index);
2843 		return -EINVAL;
2844 	}
2845 
2846 	/* set config for all DAI's with name matching the link name */
2847 	ret = sof_set_dai_config(sdev, size, link, config);
2848 	if (ret < 0)
2849 		dev_err(scomp->dev, "error: failed to save DAI config for ESAI%d\n",
2850 			config->dai_index);
2851 
2852 	return ret;
2853 }
2854 
2855 static int sof_link_acp_dmic_load(struct snd_soc_component *scomp, int index,
2856 				  struct snd_soc_dai_link *link,
2857 				  struct snd_soc_tplg_link_config *cfg,
2858 				  struct snd_soc_tplg_hw_config *hw_config,
2859 				  struct sof_ipc_dai_config *config)
2860 {
2861 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2862 	u32 size = sizeof(*config);
2863 	int ret;
2864 
2865        /* handle master/slave and inverted clocks */
2866 	sof_dai_set_format(hw_config, config);
2867 
2868 	/* init IPC */
2869 	memset(&config->acpdmic, 0, sizeof(struct sof_ipc_dai_acp_params));
2870 	config->hdr.size = size;
2871 
2872 	config->acpdmic.fsync_rate = le32_to_cpu(hw_config->fsync_rate);
2873 	config->acpdmic.tdm_slots = le32_to_cpu(hw_config->tdm_slots);
2874 
2875 	dev_info(scomp->dev, "ACP_DMIC config ACP%d channel %d rate %d\n",
2876 		 config->dai_index, config->acpdmic.tdm_slots,
2877 		 config->acpdmic.fsync_rate);
2878 
2879 	/* set config for all DAI's with name matching the link name */
2880 	ret = sof_set_dai_config(sdev, size, link, config);
2881 	if (ret < 0)
2882 		dev_err(scomp->dev, "ACP_DMIC failed to save DAI config for ACP%d\n",
2883 			config->dai_index);
2884 	return ret;
2885 }
2886 
2887 static int sof_link_acp_bt_load(struct snd_soc_component *scomp, int index,
2888 				struct snd_soc_dai_link *link,
2889 				struct snd_soc_tplg_link_config *cfg,
2890 				struct snd_soc_tplg_hw_config *hw_config,
2891 				struct sof_ipc_dai_config *config)
2892 {
2893 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2894 	u32 size = sizeof(*config);
2895 	int ret;
2896 
2897 	/* handle master/slave and inverted clocks */
2898 	sof_dai_set_format(hw_config, config);
2899 
2900 	/* init IPC */
2901 	memset(&config->acpbt, 0, sizeof(struct sof_ipc_dai_acp_params));
2902 	config->hdr.size = size;
2903 
2904 	config->acpbt.fsync_rate = le32_to_cpu(hw_config->fsync_rate);
2905 	config->acpbt.tdm_slots = le32_to_cpu(hw_config->tdm_slots);
2906 
2907 	dev_info(scomp->dev, "ACP_BT config ACP%d channel %d rate %d\n",
2908 		 config->dai_index, config->acpbt.tdm_slots,
2909 		 config->acpbt.fsync_rate);
2910 
2911 	/* set config for all DAI's with name matching the link name */
2912 	ret = sof_set_dai_config(sdev, size, link, config);
2913 	if (ret < 0)
2914 		dev_err(scomp->dev, "ACP_BT failed to save DAI config for ACP%d\n",
2915 			config->dai_index);
2916 	return ret;
2917 }
2918 
2919 static int sof_link_acp_sp_load(struct snd_soc_component *scomp, int index,
2920 				struct snd_soc_dai_link *link,
2921 				struct snd_soc_tplg_link_config *cfg,
2922 				struct snd_soc_tplg_hw_config *hw_config,
2923 				struct sof_ipc_dai_config *config)
2924 {
2925 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2926 	u32 size = sizeof(*config);
2927 	int ret;
2928 
2929 	/* handle master/slave and inverted clocks */
2930 	sof_dai_set_format(hw_config, config);
2931 
2932 	/* init IPC */
2933 	memset(&config->acpsp, 0, sizeof(struct sof_ipc_dai_acp_params));
2934 	config->hdr.size = size;
2935 
2936 	config->acpsp.fsync_rate = le32_to_cpu(hw_config->fsync_rate);
2937 	config->acpsp.tdm_slots = le32_to_cpu(hw_config->tdm_slots);
2938 
2939 	dev_info(scomp->dev, "ACP_SP config ACP%d channel %d rate %d\n",
2940 		 config->dai_index, config->acpsp.tdm_slots,
2941 		 config->acpsp.fsync_rate);
2942 
2943 	/* set config for all DAI's with name matching the link name */
2944 	ret = sof_set_dai_config(sdev, size, link, config);
2945 	if (ret < 0)
2946 		dev_err(scomp->dev, "ACP_SP failed to save DAI config for ACP%d\n",
2947 			config->dai_index);
2948 	return ret;
2949 }
2950 
2951 static int sof_link_afe_load(struct snd_soc_component *scomp, int index,
2952 			     struct snd_soc_dai_link *link,
2953 			     struct snd_soc_tplg_link_config *cfg,
2954 			     struct snd_soc_tplg_hw_config *hw_config,
2955 			     struct sof_ipc_dai_config *config)
2956 {
2957 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2958 	struct snd_soc_tplg_private *private = &cfg->priv;
2959 	u32 size = sizeof(*config);
2960 	int ret;
2961 
2962 	config->hdr.size = size;
2963 
2964 	/* get any bespoke DAI tokens */
2965 	ret = sof_parse_tokens(scomp, &config->afe, afe_tokens,
2966 			       ARRAY_SIZE(afe_tokens), private->array,
2967 			       le32_to_cpu(private->size));
2968 	if (ret != 0) {
2969 		dev_err(scomp->dev, "parse afe tokens failed %d\n",
2970 			le32_to_cpu(private->size));
2971 		return ret;
2972 	}
2973 
2974 	dev_dbg(scomp->dev, "AFE config rate %d channels %d format:%d\n",
2975 		config->afe.rate, config->afe.channels, config->afe.format);
2976 
2977 	config->afe.stream_id = DMA_CHAN_INVALID;
2978 
2979 	ret = sof_set_dai_config(sdev, size, link, config);
2980 	if (ret < 0)
2981 		dev_err(scomp->dev, "failed to process afe dai link %s", link->name);
2982 
2983 	return ret;
2984 }
2985 
2986 static int sof_link_dmic_load(struct snd_soc_component *scomp, int index,
2987 			      struct snd_soc_dai_link *link,
2988 			      struct snd_soc_tplg_link_config *cfg,
2989 			      struct snd_soc_tplg_hw_config *hw_config,
2990 			      struct sof_ipc_dai_config *config)
2991 {
2992 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2993 	struct snd_soc_tplg_private *private = &cfg->priv;
2994 	struct sof_ipc_fw_ready *ready = &sdev->fw_ready;
2995 	struct sof_ipc_fw_version *v = &ready->version;
2996 	size_t size = sizeof(*config);
2997 	int ret, j;
2998 
2999 	/* Ensure the entire DMIC config struct is zeros */
3000 	memset(&config->dmic, 0, sizeof(struct sof_ipc_dai_dmic_params));
3001 
3002 	/* get DMIC tokens */
3003 	ret = sof_parse_tokens(scomp, &config->dmic, dmic_tokens,
3004 			       ARRAY_SIZE(dmic_tokens), private->array,
3005 			       le32_to_cpu(private->size));
3006 	if (ret != 0) {
3007 		dev_err(scomp->dev, "error: parse dmic tokens failed %d\n",
3008 			le32_to_cpu(private->size));
3009 		return ret;
3010 	}
3011 
3012 	/* get DMIC PDM tokens */
3013 	ret = sof_parse_token_sets(scomp, &config->dmic.pdm[0], dmic_pdm_tokens,
3014 			       ARRAY_SIZE(dmic_pdm_tokens), private->array,
3015 			       le32_to_cpu(private->size),
3016 			       config->dmic.num_pdm_active,
3017 			       sizeof(struct sof_ipc_dai_dmic_pdm_ctrl));
3018 
3019 	if (ret != 0) {
3020 		dev_err(scomp->dev, "error: parse dmic pdm tokens failed %d\n",
3021 			le32_to_cpu(private->size));
3022 		return ret;
3023 	}
3024 
3025 	/* set IPC header size */
3026 	config->hdr.size = size;
3027 
3028 	/* debug messages */
3029 	dev_dbg(scomp->dev, "tplg: config DMIC%d driver version %d\n",
3030 		config->dai_index, config->dmic.driver_ipc_version);
3031 	dev_dbg(scomp->dev, "pdmclk_min %d pdm_clkmax %d duty_min %hd\n",
3032 		config->dmic.pdmclk_min, config->dmic.pdmclk_max,
3033 		config->dmic.duty_min);
3034 	dev_dbg(scomp->dev, "duty_max %hd fifo_fs %d num_pdms active %d\n",
3035 		config->dmic.duty_max, config->dmic.fifo_fs,
3036 		config->dmic.num_pdm_active);
3037 	dev_dbg(scomp->dev, "fifo word length %hd\n", config->dmic.fifo_bits);
3038 
3039 	for (j = 0; j < config->dmic.num_pdm_active; j++) {
3040 		dev_dbg(scomp->dev, "pdm %hd mic a %hd mic b %hd\n",
3041 			config->dmic.pdm[j].id,
3042 			config->dmic.pdm[j].enable_mic_a,
3043 			config->dmic.pdm[j].enable_mic_b);
3044 		dev_dbg(scomp->dev, "pdm %hd polarity a %hd polarity b %hd\n",
3045 			config->dmic.pdm[j].id,
3046 			config->dmic.pdm[j].polarity_mic_a,
3047 			config->dmic.pdm[j].polarity_mic_b);
3048 		dev_dbg(scomp->dev, "pdm %hd clk_edge %hd skew %hd\n",
3049 			config->dmic.pdm[j].id,
3050 			config->dmic.pdm[j].clk_edge,
3051 			config->dmic.pdm[j].skew);
3052 	}
3053 
3054 	/*
3055 	 * this takes care of backwards compatible handling of fifo_bits_b.
3056 	 * It is deprecated since firmware ABI version 3.0.1.
3057 	 */
3058 	if (SOF_ABI_VER(v->major, v->minor, v->micro) < SOF_ABI_VER(3, 0, 1))
3059 		config->dmic.fifo_bits_b = config->dmic.fifo_bits;
3060 
3061 	/* set config for all DAI's with name matching the link name */
3062 	ret = sof_set_dai_config(sdev, size, link, config);
3063 	if (ret < 0)
3064 		dev_err(scomp->dev, "error: failed to save DAI config for DMIC%d\n",
3065 			config->dai_index);
3066 
3067 	return ret;
3068 }
3069 
3070 static int sof_link_hda_load(struct snd_soc_component *scomp, int index,
3071 			     struct snd_soc_dai_link *link,
3072 			     struct snd_soc_tplg_link_config *cfg,
3073 			     struct snd_soc_tplg_hw_config *hw_config,
3074 			     struct sof_ipc_dai_config *config)
3075 {
3076 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3077 	struct snd_soc_tplg_private *private = &cfg->priv;
3078 	u32 size = sizeof(*config);
3079 	int ret;
3080 
3081 	/* init IPC */
3082 	memset(&config->hda, 0, sizeof(struct sof_ipc_dai_hda_params));
3083 	config->hdr.size = size;
3084 
3085 	/* get any bespoke DAI tokens */
3086 	ret = sof_parse_tokens(scomp, &config->hda, hda_tokens,
3087 			       ARRAY_SIZE(hda_tokens), private->array,
3088 			       le32_to_cpu(private->size));
3089 	if (ret != 0) {
3090 		dev_err(scomp->dev, "error: parse hda tokens failed %d\n",
3091 			le32_to_cpu(private->size));
3092 		return ret;
3093 	}
3094 
3095 	dev_dbg(scomp->dev, "HDA config rate %d channels %d\n",
3096 		config->hda.rate, config->hda.channels);
3097 
3098 	config->hda.link_dma_ch = DMA_CHAN_INVALID;
3099 
3100 	ret = sof_set_dai_config(sdev, size, link, config);
3101 	if (ret < 0)
3102 		dev_err(scomp->dev, "error: failed to process hda dai link %s",
3103 			link->name);
3104 
3105 	return ret;
3106 }
3107 
3108 static int sof_link_alh_load(struct snd_soc_component *scomp, int index,
3109 			     struct snd_soc_dai_link *link,
3110 			     struct snd_soc_tplg_link_config *cfg,
3111 			     struct snd_soc_tplg_hw_config *hw_config,
3112 			     struct sof_ipc_dai_config *config)
3113 {
3114 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3115 	struct snd_soc_tplg_private *private = &cfg->priv;
3116 	u32 size = sizeof(*config);
3117 	int ret;
3118 
3119 	ret = sof_parse_tokens(scomp, &config->alh, alh_tokens,
3120 			       ARRAY_SIZE(alh_tokens), private->array,
3121 			       le32_to_cpu(private->size));
3122 	if (ret != 0) {
3123 		dev_err(scomp->dev, "error: parse alh tokens failed %d\n",
3124 			le32_to_cpu(private->size));
3125 		return ret;
3126 	}
3127 
3128 	/* init IPC */
3129 	config->hdr.size = size;
3130 
3131 	/* set config for all DAI's with name matching the link name */
3132 	ret = sof_set_dai_config(sdev, size, link, config);
3133 	if (ret < 0)
3134 		dev_err(scomp->dev, "error: failed to save DAI config for ALH %d\n",
3135 			config->dai_index);
3136 
3137 	return ret;
3138 }
3139 
3140 /* DAI link - used for any driver specific init */
3141 static int sof_link_load(struct snd_soc_component *scomp, int index,
3142 			 struct snd_soc_dai_link *link,
3143 			 struct snd_soc_tplg_link_config *cfg)
3144 {
3145 	struct snd_soc_tplg_private *private = &cfg->priv;
3146 	struct snd_soc_tplg_hw_config *hw_config;
3147 	struct sof_ipc_dai_config common_config;
3148 	struct sof_ipc_dai_config *config;
3149 	int curr_conf;
3150 	int num_conf;
3151 	int ret;
3152 	int i;
3153 
3154 	if (!link->platforms) {
3155 		dev_err(scomp->dev, "error: no platforms\n");
3156 		return -EINVAL;
3157 	}
3158 	link->platforms->name = dev_name(scomp->dev);
3159 
3160 	/*
3161 	 * Set nonatomic property for FE dai links as their trigger action
3162 	 * involves IPC's.
3163 	 */
3164 	if (!link->no_pcm) {
3165 		link->nonatomic = true;
3166 
3167 		/*
3168 		 * set default trigger order for all links. Exceptions to
3169 		 * the rule will be handled in sof_pcm_dai_link_fixup()
3170 		 * For playback, the sequence is the following: start FE,
3171 		 * start BE, stop BE, stop FE; for Capture the sequence is
3172 		 * inverted start BE, start FE, stop FE, stop BE
3173 		 */
3174 		link->trigger[SNDRV_PCM_STREAM_PLAYBACK] =
3175 					SND_SOC_DPCM_TRIGGER_PRE;
3176 		link->trigger[SNDRV_PCM_STREAM_CAPTURE] =
3177 					SND_SOC_DPCM_TRIGGER_POST;
3178 
3179 		/* nothing more to do for FE dai links */
3180 		return 0;
3181 	}
3182 
3183 	/* check we have some tokens - we need at least DAI type */
3184 	if (le32_to_cpu(private->size) == 0) {
3185 		dev_err(scomp->dev, "error: expected tokens for DAI, none found\n");
3186 		return -EINVAL;
3187 	}
3188 
3189 	memset(&common_config, 0, sizeof(common_config));
3190 
3191 	/* get any common DAI tokens */
3192 	ret = sof_parse_tokens(scomp, &common_config, dai_link_tokens, ARRAY_SIZE(dai_link_tokens),
3193 			       private->array, le32_to_cpu(private->size));
3194 	if (ret != 0) {
3195 		dev_err(scomp->dev, "error: parse link tokens failed %d\n",
3196 			le32_to_cpu(private->size));
3197 		return ret;
3198 	}
3199 
3200 	/*
3201 	 * DAI links are expected to have at least 1 hw_config.
3202 	 * But some older topologies might have no hw_config for HDA dai links.
3203 	 */
3204 	hw_config = cfg->hw_config;
3205 	num_conf = le32_to_cpu(cfg->num_hw_configs);
3206 	if (!num_conf) {
3207 		if (common_config.type != SOF_DAI_INTEL_HDA) {
3208 			dev_err(scomp->dev, "error: unexpected DAI config count %d!\n",
3209 				le32_to_cpu(cfg->num_hw_configs));
3210 			return -EINVAL;
3211 		}
3212 		num_conf = 1;
3213 		curr_conf = 0;
3214 	} else {
3215 		dev_dbg(scomp->dev, "tplg: %d hw_configs found, default id: %d!\n",
3216 			cfg->num_hw_configs, le32_to_cpu(cfg->default_hw_config_id));
3217 
3218 		for (curr_conf = 0; curr_conf < num_conf; curr_conf++) {
3219 			if (hw_config[curr_conf].id == cfg->default_hw_config_id)
3220 				break;
3221 		}
3222 
3223 		if (curr_conf == num_conf) {
3224 			dev_err(scomp->dev, "error: default hw_config id: %d not found!\n",
3225 				le32_to_cpu(cfg->default_hw_config_id));
3226 			return -EINVAL;
3227 		}
3228 	}
3229 
3230 	/* Reserve memory for all hw configs, eventually freed by widget */
3231 	config = kcalloc(num_conf, sizeof(*config), GFP_KERNEL);
3232 	if (!config)
3233 		return -ENOMEM;
3234 
3235 	/* Copy common data to all config ipc structs */
3236 	for (i = 0; i < num_conf; i++) {
3237 		config[i].hdr.cmd = SOF_IPC_GLB_DAI_MSG | SOF_IPC_DAI_CONFIG;
3238 		config[i].format = le32_to_cpu(hw_config[i].fmt);
3239 		config[i].type = common_config.type;
3240 		config[i].dai_index = common_config.dai_index;
3241 	}
3242 
3243 	/* now load DAI specific data and send IPC - type comes from token */
3244 	switch (common_config.type) {
3245 	case SOF_DAI_INTEL_SSP:
3246 		ret = sof_link_ssp_load(scomp, index, link, cfg, hw_config, config, curr_conf);
3247 		break;
3248 	case SOF_DAI_INTEL_DMIC:
3249 		ret = sof_link_dmic_load(scomp, index, link, cfg, hw_config + curr_conf, config);
3250 		break;
3251 	case SOF_DAI_INTEL_HDA:
3252 		ret = sof_link_hda_load(scomp, index, link, cfg, hw_config + curr_conf, config);
3253 		break;
3254 	case SOF_DAI_INTEL_ALH:
3255 		ret = sof_link_alh_load(scomp, index, link, cfg, hw_config + curr_conf, config);
3256 		break;
3257 	case SOF_DAI_IMX_SAI:
3258 		ret = sof_link_sai_load(scomp, index, link, cfg, hw_config + curr_conf, config);
3259 		break;
3260 	case SOF_DAI_IMX_ESAI:
3261 		ret = sof_link_esai_load(scomp, index, link, cfg, hw_config + curr_conf, config);
3262 		break;
3263 	case SOF_DAI_AMD_BT:
3264 		ret = sof_link_acp_bt_load(scomp, index, link, cfg, hw_config + curr_conf, config);
3265 		break;
3266 	case SOF_DAI_AMD_SP:
3267 		ret = sof_link_acp_sp_load(scomp, index, link, cfg, hw_config + curr_conf, config);
3268 		break;
3269 	case SOF_DAI_AMD_DMIC:
3270 		ret = sof_link_acp_dmic_load(scomp, index, link, cfg, hw_config + curr_conf,
3271 					     config);
3272 		break;
3273 	case SOF_DAI_MEDIATEK_AFE:
3274 		ret = sof_link_afe_load(scomp, index, link, cfg, hw_config + curr_conf, config);
3275 		break;
3276 	default:
3277 		dev_err(scomp->dev, "error: invalid DAI type %d\n", common_config.type);
3278 		ret = -EINVAL;
3279 		break;
3280 	}
3281 
3282 	kfree(config);
3283 
3284 	return ret;
3285 }
3286 
3287 /* DAI link - used for any driver specific init */
3288 static int sof_route_load(struct snd_soc_component *scomp, int index,
3289 			  struct snd_soc_dapm_route *route)
3290 {
3291 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3292 	struct snd_sof_widget *source_swidget, *sink_swidget;
3293 	struct snd_soc_dobj *dobj = &route->dobj;
3294 	struct snd_sof_route *sroute;
3295 	int ret = 0;
3296 
3297 	/* allocate memory for sroute and connect */
3298 	sroute = kzalloc(sizeof(*sroute), GFP_KERNEL);
3299 	if (!sroute)
3300 		return -ENOMEM;
3301 
3302 	sroute->scomp = scomp;
3303 	dev_dbg(scomp->dev, "sink %s control %s source %s\n",
3304 		route->sink, route->control ? route->control : "none",
3305 		route->source);
3306 
3307 	/* source component */
3308 	source_swidget = snd_sof_find_swidget(scomp, (char *)route->source);
3309 	if (!source_swidget) {
3310 		dev_err(scomp->dev, "error: source %s not found\n",
3311 			route->source);
3312 		ret = -EINVAL;
3313 		goto err;
3314 	}
3315 
3316 	/*
3317 	 * Virtual widgets of type output/out_drv may be added in topology
3318 	 * for compatibility. These are not handled by the FW.
3319 	 * So, don't send routes whose source/sink widget is of such types
3320 	 * to the DSP.
3321 	 */
3322 	if (source_swidget->id == snd_soc_dapm_out_drv ||
3323 	    source_swidget->id == snd_soc_dapm_output)
3324 		goto err;
3325 
3326 	/* sink component */
3327 	sink_swidget = snd_sof_find_swidget(scomp, (char *)route->sink);
3328 	if (!sink_swidget) {
3329 		dev_err(scomp->dev, "error: sink %s not found\n",
3330 			route->sink);
3331 		ret = -EINVAL;
3332 		goto err;
3333 	}
3334 
3335 	/*
3336 	 * Don't send routes whose sink widget is of type
3337 	 * output or out_drv to the DSP
3338 	 */
3339 	if (sink_swidget->id == snd_soc_dapm_out_drv ||
3340 	    sink_swidget->id == snd_soc_dapm_output)
3341 		goto err;
3342 
3343 	/*
3344 	 * For virtual routes, both sink and source are not
3345 	 * buffer. Since only buffer linked to component is supported by
3346 	 * FW, others are reported as error, add check in route function,
3347 	 * do not send it to FW when both source and sink are not buffer
3348 	 */
3349 	if (source_swidget->id != snd_soc_dapm_buffer &&
3350 	    sink_swidget->id != snd_soc_dapm_buffer) {
3351 		dev_dbg(scomp->dev, "warning: neither Linked source component %s nor sink component %s is of buffer type, ignoring link\n",
3352 			route->source, route->sink);
3353 		goto err;
3354 	} else {
3355 		sroute->route = route;
3356 		dobj->private = sroute;
3357 		sroute->src_widget = source_swidget;
3358 		sroute->sink_widget = sink_swidget;
3359 
3360 		/* add route to route list */
3361 		list_add(&sroute->list, &sdev->route_list);
3362 
3363 		return 0;
3364 	}
3365 
3366 err:
3367 	kfree(sroute);
3368 	return ret;
3369 }
3370 
3371 int snd_sof_complete_pipeline(struct snd_sof_dev *sdev,
3372 			      struct snd_sof_widget *swidget)
3373 {
3374 	struct sof_ipc_pipe_ready ready;
3375 	struct sof_ipc_reply reply;
3376 	int ret;
3377 
3378 	dev_dbg(sdev->dev, "tplg: complete pipeline %s id %d\n",
3379 		swidget->widget->name, swidget->comp_id);
3380 
3381 	memset(&ready, 0, sizeof(ready));
3382 	ready.hdr.size = sizeof(ready);
3383 	ready.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_PIPE_COMPLETE;
3384 	ready.comp_id = swidget->comp_id;
3385 
3386 	ret = sof_ipc_tx_message(sdev->ipc,
3387 				 ready.hdr.cmd, &ready, sizeof(ready), &reply,
3388 				 sizeof(reply));
3389 	if (ret < 0)
3390 		return ret;
3391 	return 1;
3392 }
3393 
3394 /**
3395  * sof_set_pipe_widget - Set pipe_widget for a component
3396  * @sdev: pointer to struct snd_sof_dev
3397  * @pipe_widget: pointer to struct snd_sof_widget of type snd_soc_dapm_scheduler
3398  * @swidget: pointer to struct snd_sof_widget that has the same pipeline ID as @pipe_widget
3399  *
3400  * Return: 0 if successful, -EINVAL on error.
3401  * The function checks if @swidget is associated with any volatile controls. If so, setting
3402  * the dynamic_pipeline_widget is disallowed.
3403  */
3404 static int sof_set_pipe_widget(struct snd_sof_dev *sdev, struct snd_sof_widget *pipe_widget,
3405 			       struct snd_sof_widget *swidget)
3406 {
3407 	struct snd_sof_control *scontrol;
3408 
3409 	if (pipe_widget->dynamic_pipeline_widget) {
3410 		/* dynamic widgets cannot have volatile kcontrols */
3411 		list_for_each_entry(scontrol, &sdev->kcontrol_list, list)
3412 			if (scontrol->comp_id == swidget->comp_id &&
3413 			    (scontrol->access & SNDRV_CTL_ELEM_ACCESS_VOLATILE)) {
3414 				dev_err(sdev->dev,
3415 					"error: volatile control found for dynamic widget %s\n",
3416 					swidget->widget->name);
3417 				return -EINVAL;
3418 			}
3419 	}
3420 
3421 	/* set the pipe_widget and apply the dynamic_pipeline_widget_flag */
3422 	swidget->pipe_widget = pipe_widget;
3423 	swidget->dynamic_pipeline_widget = pipe_widget->dynamic_pipeline_widget;
3424 
3425 	return 0;
3426 }
3427 
3428 /* completion - called at completion of firmware loading */
3429 static int sof_complete(struct snd_soc_component *scomp)
3430 {
3431 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3432 	struct snd_sof_widget *swidget, *comp_swidget;
3433 	const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
3434 	const struct sof_ipc_tplg_widget_ops *widget_ops = ipc_tplg_ops->widget;
3435 	int ret;
3436 
3437 	/*
3438 	 * now update all widget IPC structures. If any of the ipc_setup callbacks fail, the
3439 	 * topology will be removed and all widgets will be unloaded resulting in freeing all
3440 	 * associated memories.
3441 	 */
3442 	list_for_each_entry(swidget, &sdev->widget_list, list) {
3443 		if (widget_ops[swidget->id].ipc_setup) {
3444 			ret = widget_ops[swidget->id].ipc_setup(swidget);
3445 			if (ret < 0) {
3446 				dev_err(sdev->dev, "failed updating IPC struct for %s\n",
3447 					swidget->widget->name);
3448 				return ret;
3449 			}
3450 		}
3451 	}
3452 
3453 	/* set the pipe_widget and apply the dynamic_pipeline_widget_flag */
3454 	list_for_each_entry(swidget, &sdev->widget_list, list) {
3455 		switch (swidget->id) {
3456 		case snd_soc_dapm_scheduler:
3457 			/*
3458 			 * Apply the dynamic_pipeline_widget flag and set the pipe_widget field
3459 			 * for all widgets that have the same pipeline ID as the scheduler widget
3460 			 */
3461 			list_for_each_entry(comp_swidget, &sdev->widget_list, list)
3462 				if (comp_swidget->pipeline_id == swidget->pipeline_id) {
3463 					ret = sof_set_pipe_widget(sdev, swidget, comp_swidget);
3464 					if (ret < 0)
3465 						return ret;
3466 				}
3467 			break;
3468 		default:
3469 			break;
3470 		}
3471 	}
3472 
3473 	/* verify topology components loading including dynamic pipelines */
3474 	if (sof_debug_check_flag(SOF_DBG_VERIFY_TPLG)) {
3475 		ret = sof_set_up_pipelines(sdev, true);
3476 		if (ret < 0) {
3477 			dev_err(sdev->dev, "error: topology verification failed %d\n", ret);
3478 			return ret;
3479 		}
3480 
3481 		ret = sof_tear_down_pipelines(sdev, true);
3482 		if (ret < 0) {
3483 			dev_err(sdev->dev, "error: topology tear down pipelines failed %d\n", ret);
3484 			return ret;
3485 		}
3486 	}
3487 
3488 	/* set up static pipelines */
3489 	return sof_set_up_pipelines(sdev, false);
3490 }
3491 
3492 /* manifest - optional to inform component of manifest */
3493 static int sof_manifest(struct snd_soc_component *scomp, int index,
3494 			struct snd_soc_tplg_manifest *man)
3495 {
3496 	u32 size;
3497 	u32 abi_version;
3498 
3499 	size = le32_to_cpu(man->priv.size);
3500 
3501 	/* backward compatible with tplg without ABI info */
3502 	if (!size) {
3503 		dev_dbg(scomp->dev, "No topology ABI info\n");
3504 		return 0;
3505 	}
3506 
3507 	if (size != SOF_TPLG_ABI_SIZE) {
3508 		dev_err(scomp->dev, "error: invalid topology ABI size\n");
3509 		return -EINVAL;
3510 	}
3511 
3512 	dev_info(scomp->dev,
3513 		 "Topology: ABI %d:%d:%d Kernel ABI %d:%d:%d\n",
3514 		 man->priv.data[0], man->priv.data[1],
3515 		 man->priv.data[2], SOF_ABI_MAJOR, SOF_ABI_MINOR,
3516 		 SOF_ABI_PATCH);
3517 
3518 	abi_version = SOF_ABI_VER(man->priv.data[0],
3519 				  man->priv.data[1],
3520 				  man->priv.data[2]);
3521 
3522 	if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, abi_version)) {
3523 		dev_err(scomp->dev, "error: incompatible topology ABI version\n");
3524 		return -EINVAL;
3525 	}
3526 
3527 	if (SOF_ABI_VERSION_MINOR(abi_version) > SOF_ABI_MINOR) {
3528 		if (!IS_ENABLED(CONFIG_SND_SOC_SOF_STRICT_ABI_CHECKS)) {
3529 			dev_warn(scomp->dev, "warn: topology ABI is more recent than kernel\n");
3530 		} else {
3531 			dev_err(scomp->dev, "error: topology ABI is more recent than kernel\n");
3532 			return -EINVAL;
3533 		}
3534 	}
3535 
3536 	return 0;
3537 }
3538 
3539 /* vendor specific kcontrol handlers available for binding */
3540 static const struct snd_soc_tplg_kcontrol_ops sof_io_ops[] = {
3541 	{SOF_TPLG_KCTL_VOL_ID, snd_sof_volume_get, snd_sof_volume_put},
3542 	{SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_get, snd_sof_bytes_put},
3543 	{SOF_TPLG_KCTL_ENUM_ID, snd_sof_enum_get, snd_sof_enum_put},
3544 	{SOF_TPLG_KCTL_SWITCH_ID, snd_sof_switch_get, snd_sof_switch_put},
3545 };
3546 
3547 /* vendor specific bytes ext handlers available for binding */
3548 static const struct snd_soc_tplg_bytes_ext_ops sof_bytes_ext_ops[] = {
3549 	{SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_ext_get, snd_sof_bytes_ext_put},
3550 	{SOF_TPLG_KCTL_BYTES_VOLATILE_RO, snd_sof_bytes_ext_volatile_get},
3551 };
3552 
3553 static struct snd_soc_tplg_ops sof_tplg_ops = {
3554 	/* external kcontrol init - used for any driver specific init */
3555 	.control_load	= sof_control_load,
3556 	.control_unload	= sof_control_unload,
3557 
3558 	/* external kcontrol init - used for any driver specific init */
3559 	.dapm_route_load	= sof_route_load,
3560 	.dapm_route_unload	= sof_route_unload,
3561 
3562 	/* external widget init - used for any driver specific init */
3563 	/* .widget_load is not currently used */
3564 	.widget_ready	= sof_widget_ready,
3565 	.widget_unload	= sof_widget_unload,
3566 
3567 	/* FE DAI - used for any driver specific init */
3568 	.dai_load	= sof_dai_load,
3569 	.dai_unload	= sof_dai_unload,
3570 
3571 	/* DAI link - used for any driver specific init */
3572 	.link_load	= sof_link_load,
3573 
3574 	/* completion - called at completion of firmware loading */
3575 	.complete	= sof_complete,
3576 
3577 	/* manifest - optional to inform component of manifest */
3578 	.manifest	= sof_manifest,
3579 
3580 	/* vendor specific kcontrol handlers available for binding */
3581 	.io_ops		= sof_io_ops,
3582 	.io_ops_count	= ARRAY_SIZE(sof_io_ops),
3583 
3584 	/* vendor specific bytes ext handlers available for binding */
3585 	.bytes_ext_ops	= sof_bytes_ext_ops,
3586 	.bytes_ext_ops_count	= ARRAY_SIZE(sof_bytes_ext_ops),
3587 };
3588 
3589 int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file)
3590 {
3591 	const struct firmware *fw;
3592 	int ret;
3593 
3594 	dev_dbg(scomp->dev, "loading topology:%s\n", file);
3595 
3596 	ret = request_firmware(&fw, file, scomp->dev);
3597 	if (ret < 0) {
3598 		dev_err(scomp->dev, "error: tplg request firmware %s failed err: %d\n",
3599 			file, ret);
3600 		dev_err(scomp->dev,
3601 			"you may need to download the firmware from https://github.com/thesofproject/sof-bin/\n");
3602 		return ret;
3603 	}
3604 
3605 	ret = snd_soc_tplg_component_load(scomp, &sof_tplg_ops, fw);
3606 	if (ret < 0) {
3607 		dev_err(scomp->dev, "error: tplg component load failed %d\n",
3608 			ret);
3609 		ret = -EINVAL;
3610 	}
3611 
3612 	release_firmware(fw);
3613 	return ret;
3614 }
3615 EXPORT_SYMBOL(snd_sof_load_topology);
3616