xref: /openbmc/linux/sound/soc/sof/topology.c (revision 30f4168024e91e85aff9edcef561715ed271b34c)
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  * Mux topology
1797  */
1798 static int sof_widget_load_mux(struct snd_soc_component *scomp, int index,
1799 			       struct snd_sof_widget *swidget,
1800 			       struct snd_soc_tplg_dapm_widget *tw)
1801 {
1802 	struct snd_soc_tplg_private *private = &tw->priv;
1803 	struct sof_ipc_comp_mux *mux;
1804 	size_t ipc_size = sizeof(*mux);
1805 	int ret;
1806 
1807 	mux = (struct sof_ipc_comp_mux *)
1808 	      sof_comp_alloc(swidget, &ipc_size, index);
1809 	if (!mux)
1810 		return -ENOMEM;
1811 
1812 	/* configure mux IPC message */
1813 	mux->comp.type = SOF_COMP_MUX;
1814 	mux->config.hdr.size = sizeof(mux->config);
1815 
1816 	ret = sof_parse_tokens(scomp, &mux->config, comp_tokens,
1817 			       ARRAY_SIZE(comp_tokens), private->array,
1818 			       le32_to_cpu(private->size));
1819 	if (ret != 0) {
1820 		dev_err(scomp->dev, "error: parse mux.cfg tokens failed %d\n",
1821 			private->size);
1822 		kfree(mux);
1823 		return ret;
1824 	}
1825 
1826 	sof_dbg_comp_config(scomp, &mux->config);
1827 
1828 	swidget->private = mux;
1829 
1830 	return 0;
1831 }
1832 
1833 /*
1834  * SRC Topology
1835  */
1836 
1837 static int sof_widget_load_src(struct snd_soc_component *scomp, int index,
1838 			       struct snd_sof_widget *swidget,
1839 			       struct snd_soc_tplg_dapm_widget *tw)
1840 {
1841 	struct snd_soc_tplg_private *private = &tw->priv;
1842 	struct sof_ipc_comp_src *src;
1843 	size_t ipc_size = sizeof(*src);
1844 	int ret;
1845 
1846 	src = (struct sof_ipc_comp_src *)
1847 	      sof_comp_alloc(swidget, &ipc_size, index);
1848 	if (!src)
1849 		return -ENOMEM;
1850 
1851 	/* configure src IPC message */
1852 	src->comp.type = SOF_COMP_SRC;
1853 	src->config.hdr.size = sizeof(src->config);
1854 
1855 	ret = sof_parse_tokens(scomp, src, src_tokens,
1856 			       ARRAY_SIZE(src_tokens), private->array,
1857 			       le32_to_cpu(private->size));
1858 	if (ret != 0) {
1859 		dev_err(scomp->dev, "error: parse src tokens failed %d\n",
1860 			private->size);
1861 		goto err;
1862 	}
1863 
1864 	ret = sof_parse_tokens(scomp, &src->config, comp_tokens,
1865 			       ARRAY_SIZE(comp_tokens), private->array,
1866 			       le32_to_cpu(private->size));
1867 	if (ret != 0) {
1868 		dev_err(scomp->dev, "error: parse src.cfg tokens failed %d\n",
1869 			le32_to_cpu(private->size));
1870 		goto err;
1871 	}
1872 
1873 	dev_dbg(scomp->dev, "src %s: source rate %d sink rate %d\n",
1874 		swidget->widget->name, src->source_rate, src->sink_rate);
1875 	sof_dbg_comp_config(scomp, &src->config);
1876 
1877 	swidget->private = src;
1878 
1879 	return 0;
1880 err:
1881 	kfree(src);
1882 	return ret;
1883 }
1884 
1885 /*
1886  * ASRC Topology
1887  */
1888 
1889 static int sof_widget_load_asrc(struct snd_soc_component *scomp, int index,
1890 				struct snd_sof_widget *swidget,
1891 				struct snd_soc_tplg_dapm_widget *tw)
1892 {
1893 	struct snd_soc_tplg_private *private = &tw->priv;
1894 	struct sof_ipc_comp_asrc *asrc;
1895 	size_t ipc_size = sizeof(*asrc);
1896 	int ret;
1897 
1898 	asrc = (struct sof_ipc_comp_asrc *)
1899 	       sof_comp_alloc(swidget, &ipc_size, index);
1900 	if (!asrc)
1901 		return -ENOMEM;
1902 
1903 	/* configure ASRC IPC message */
1904 	asrc->comp.type = SOF_COMP_ASRC;
1905 	asrc->config.hdr.size = sizeof(asrc->config);
1906 
1907 	ret = sof_parse_tokens(scomp, asrc, asrc_tokens,
1908 			       ARRAY_SIZE(asrc_tokens), private->array,
1909 			       le32_to_cpu(private->size));
1910 	if (ret != 0) {
1911 		dev_err(scomp->dev, "error: parse asrc tokens failed %d\n",
1912 			private->size);
1913 		goto err;
1914 	}
1915 
1916 	ret = sof_parse_tokens(scomp, &asrc->config, comp_tokens,
1917 			       ARRAY_SIZE(comp_tokens), private->array,
1918 			       le32_to_cpu(private->size));
1919 	if (ret != 0) {
1920 		dev_err(scomp->dev, "error: parse asrc.cfg tokens failed %d\n",
1921 			le32_to_cpu(private->size));
1922 		goto err;
1923 	}
1924 
1925 	dev_dbg(scomp->dev, "asrc %s: source rate %d sink rate %d "
1926 		"asynch %d operation %d\n",
1927 		swidget->widget->name, asrc->source_rate, asrc->sink_rate,
1928 		asrc->asynchronous_mode, asrc->operation_mode);
1929 	sof_dbg_comp_config(scomp, &asrc->config);
1930 
1931 	swidget->private = asrc;
1932 
1933 	return 0;
1934 err:
1935 	kfree(asrc);
1936 	return ret;
1937 }
1938 
1939 /*
1940  * Signal Generator Topology
1941  */
1942 
1943 static int sof_widget_load_siggen(struct snd_soc_component *scomp, int index,
1944 				  struct snd_sof_widget *swidget,
1945 				  struct snd_soc_tplg_dapm_widget *tw)
1946 {
1947 	struct snd_soc_tplg_private *private = &tw->priv;
1948 	struct sof_ipc_comp_tone *tone;
1949 	size_t ipc_size = sizeof(*tone);
1950 	int ret;
1951 
1952 	tone = (struct sof_ipc_comp_tone *)
1953 	       sof_comp_alloc(swidget, &ipc_size, index);
1954 	if (!tone)
1955 		return -ENOMEM;
1956 
1957 	/* configure siggen IPC message */
1958 	tone->comp.type = SOF_COMP_TONE;
1959 	tone->config.hdr.size = sizeof(tone->config);
1960 
1961 	ret = sof_parse_tokens(scomp, tone, tone_tokens,
1962 			       ARRAY_SIZE(tone_tokens), private->array,
1963 			       le32_to_cpu(private->size));
1964 	if (ret != 0) {
1965 		dev_err(scomp->dev, "error: parse tone tokens failed %d\n",
1966 			le32_to_cpu(private->size));
1967 		goto err;
1968 	}
1969 
1970 	ret = sof_parse_tokens(scomp, &tone->config, comp_tokens,
1971 			       ARRAY_SIZE(comp_tokens), private->array,
1972 			       le32_to_cpu(private->size));
1973 	if (ret != 0) {
1974 		dev_err(scomp->dev, "error: parse tone.cfg tokens failed %d\n",
1975 			le32_to_cpu(private->size));
1976 		goto err;
1977 	}
1978 
1979 	dev_dbg(scomp->dev, "tone %s: frequency %d amplitude %d\n",
1980 		swidget->widget->name, tone->frequency, tone->amplitude);
1981 	sof_dbg_comp_config(scomp, &tone->config);
1982 
1983 	swidget->private = tone;
1984 
1985 	return 0;
1986 err:
1987 	kfree(tone);
1988 	return ret;
1989 }
1990 
1991 static int sof_get_control_data(struct snd_soc_component *scomp,
1992 				struct snd_soc_dapm_widget *widget,
1993 				struct sof_widget_data *wdata,
1994 				size_t *size)
1995 {
1996 	const struct snd_kcontrol_new *kc;
1997 	struct soc_mixer_control *sm;
1998 	struct soc_bytes_ext *sbe;
1999 	struct soc_enum *se;
2000 	int i;
2001 
2002 	*size = 0;
2003 
2004 	for (i = 0; i < widget->num_kcontrols; i++) {
2005 		kc = &widget->kcontrol_news[i];
2006 
2007 		switch (widget->dobj.widget.kcontrol_type[i]) {
2008 		case SND_SOC_TPLG_TYPE_MIXER:
2009 			sm = (struct soc_mixer_control *)kc->private_value;
2010 			wdata[i].control = sm->dobj.private;
2011 			break;
2012 		case SND_SOC_TPLG_TYPE_BYTES:
2013 			sbe = (struct soc_bytes_ext *)kc->private_value;
2014 			wdata[i].control = sbe->dobj.private;
2015 			break;
2016 		case SND_SOC_TPLG_TYPE_ENUM:
2017 			se = (struct soc_enum *)kc->private_value;
2018 			wdata[i].control = se->dobj.private;
2019 			break;
2020 		default:
2021 			dev_err(scomp->dev, "error: unknown kcontrol type %u in widget %s\n",
2022 				widget->dobj.widget.kcontrol_type[i],
2023 				widget->name);
2024 			return -EINVAL;
2025 		}
2026 
2027 		if (!wdata[i].control) {
2028 			dev_err(scomp->dev, "error: no scontrol for widget %s\n",
2029 				widget->name);
2030 			return -EINVAL;
2031 		}
2032 
2033 		wdata[i].pdata = wdata[i].control->control_data->data;
2034 		if (!wdata[i].pdata)
2035 			return -EINVAL;
2036 
2037 		/* make sure data is valid - data can be updated at runtime */
2038 		if (widget->dobj.widget.kcontrol_type[i] == SND_SOC_TPLG_TYPE_BYTES &&
2039 		    wdata[i].pdata->magic != SOF_ABI_MAGIC)
2040 			return -EINVAL;
2041 
2042 		*size += wdata[i].pdata->size;
2043 
2044 		/* get data type */
2045 		switch (wdata[i].control->control_data->cmd) {
2046 		case SOF_CTRL_CMD_VOLUME:
2047 		case SOF_CTRL_CMD_ENUM:
2048 		case SOF_CTRL_CMD_SWITCH:
2049 			wdata[i].ipc_cmd = SOF_IPC_COMP_SET_VALUE;
2050 			wdata[i].ctrl_type = SOF_CTRL_TYPE_VALUE_CHAN_SET;
2051 			break;
2052 		case SOF_CTRL_CMD_BINARY:
2053 			wdata[i].ipc_cmd = SOF_IPC_COMP_SET_DATA;
2054 			wdata[i].ctrl_type = SOF_CTRL_TYPE_DATA_SET;
2055 			break;
2056 		default:
2057 			break;
2058 		}
2059 	}
2060 
2061 	return 0;
2062 }
2063 
2064 static int sof_process_load(struct snd_soc_component *scomp, int index,
2065 			    struct snd_sof_widget *swidget,
2066 			    struct snd_soc_tplg_dapm_widget *tw,
2067 			    int type)
2068 {
2069 	struct snd_soc_dapm_widget *widget = swidget->widget;
2070 	struct snd_soc_tplg_private *private = &tw->priv;
2071 	struct sof_ipc_comp_process *process;
2072 	struct sof_widget_data *wdata = NULL;
2073 	size_t ipc_data_size = 0;
2074 	size_t ipc_size;
2075 	int offset = 0;
2076 	int ret;
2077 	int i;
2078 
2079 	/* allocate struct for widget control data sizes and types */
2080 	if (widget->num_kcontrols) {
2081 		wdata = kcalloc(widget->num_kcontrols,
2082 				sizeof(*wdata),
2083 				GFP_KERNEL);
2084 
2085 		if (!wdata)
2086 			return -ENOMEM;
2087 
2088 		/* get possible component controls and get size of all pdata */
2089 		ret = sof_get_control_data(scomp, widget, wdata,
2090 					   &ipc_data_size);
2091 
2092 		if (ret < 0)
2093 			goto out;
2094 	}
2095 
2096 	ipc_size = sizeof(struct sof_ipc_comp_process) + ipc_data_size;
2097 
2098 	/* we are exceeding max ipc size, config needs to be sent separately */
2099 	if (ipc_size > SOF_IPC_MSG_MAX_SIZE) {
2100 		ipc_size -= ipc_data_size;
2101 		ipc_data_size = 0;
2102 	}
2103 
2104 	process = (struct sof_ipc_comp_process *)
2105 		  sof_comp_alloc(swidget, &ipc_size, index);
2106 	if (!process) {
2107 		ret = -ENOMEM;
2108 		goto out;
2109 	}
2110 
2111 	/* configure iir IPC message */
2112 	process->comp.type = type;
2113 	process->config.hdr.size = sizeof(process->config);
2114 
2115 	ret = sof_parse_tokens(scomp, &process->config, comp_tokens,
2116 			       ARRAY_SIZE(comp_tokens), private->array,
2117 			       le32_to_cpu(private->size));
2118 	if (ret != 0) {
2119 		dev_err(scomp->dev, "error: parse process.cfg tokens failed %d\n",
2120 			le32_to_cpu(private->size));
2121 		goto err;
2122 	}
2123 
2124 	sof_dbg_comp_config(scomp, &process->config);
2125 
2126 	/*
2127 	 * found private data in control, so copy it.
2128 	 * get possible component controls - get size of all pdata,
2129 	 * then memcpy with headers
2130 	 */
2131 	if (ipc_data_size) {
2132 		for (i = 0; i < widget->num_kcontrols; i++) {
2133 			memcpy(&process->data[offset],
2134 			       wdata[i].pdata->data,
2135 			       wdata[i].pdata->size);
2136 			offset += wdata[i].pdata->size;
2137 		}
2138 	}
2139 
2140 	process->size = ipc_data_size;
2141 	swidget->private = process;
2142 err:
2143 	if (ret < 0)
2144 		kfree(process);
2145 out:
2146 	kfree(wdata);
2147 	return ret;
2148 }
2149 
2150 /*
2151  * Processing Component Topology - can be "effect", "codec", or general
2152  * "processing".
2153  */
2154 
2155 static int sof_widget_load_process(struct snd_soc_component *scomp, int index,
2156 				   struct snd_sof_widget *swidget,
2157 				   struct snd_soc_tplg_dapm_widget *tw)
2158 {
2159 	struct snd_soc_tplg_private *private = &tw->priv;
2160 	struct sof_ipc_comp_process config;
2161 	int ret;
2162 
2163 	/* check we have some tokens - we need at least process type */
2164 	if (le32_to_cpu(private->size) == 0) {
2165 		dev_err(scomp->dev, "error: process tokens not found\n");
2166 		return -EINVAL;
2167 	}
2168 
2169 	memset(&config, 0, sizeof(config));
2170 	config.comp.core = swidget->core;
2171 
2172 	/* get the process token */
2173 	ret = sof_parse_tokens(scomp, &config, process_tokens,
2174 			       ARRAY_SIZE(process_tokens), private->array,
2175 			       le32_to_cpu(private->size));
2176 	if (ret != 0) {
2177 		dev_err(scomp->dev, "error: parse process tokens failed %d\n",
2178 			le32_to_cpu(private->size));
2179 		return ret;
2180 	}
2181 
2182 	/* now load process specific data and send IPC */
2183 	ret = sof_process_load(scomp, index, swidget, tw, find_process_comp_type(config.type));
2184 	if (ret < 0) {
2185 		dev_err(scomp->dev, "error: process loading failed\n");
2186 		return ret;
2187 	}
2188 
2189 	return 0;
2190 }
2191 
2192 static int sof_widget_bind_event(struct snd_soc_component *scomp,
2193 				 struct snd_sof_widget *swidget,
2194 				 u16 event_type)
2195 {
2196 	struct sof_ipc_comp *ipc_comp;
2197 
2198 	/* validate widget event type */
2199 	switch (event_type) {
2200 	case SOF_KEYWORD_DETECT_DAPM_EVENT:
2201 		/* only KEYWORD_DETECT comps should handle this */
2202 		if (swidget->id != snd_soc_dapm_effect)
2203 			break;
2204 
2205 		ipc_comp = swidget->private;
2206 		if (ipc_comp && ipc_comp->type != SOF_COMP_KEYWORD_DETECT)
2207 			break;
2208 
2209 		/* bind event to keyword detect comp */
2210 		return snd_soc_tplg_widget_bind_event(swidget->widget,
2211 						      sof_kwd_events,
2212 						      ARRAY_SIZE(sof_kwd_events),
2213 						      event_type);
2214 	default:
2215 		break;
2216 	}
2217 
2218 	dev_err(scomp->dev,
2219 		"error: invalid event type %d for widget %s\n",
2220 		event_type, swidget->widget->name);
2221 	return -EINVAL;
2222 }
2223 
2224 /* external widget init - used for any driver specific init */
2225 static int sof_widget_ready(struct snd_soc_component *scomp, int index,
2226 			    struct snd_soc_dapm_widget *w,
2227 			    struct snd_soc_tplg_dapm_widget *tw)
2228 {
2229 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2230 	const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
2231 	const struct sof_ipc_tplg_widget_ops *widget_ops = ipc_tplg_ops->widget;
2232 	struct snd_sof_widget *swidget;
2233 	struct snd_sof_dai *dai;
2234 	enum sof_tokens *token_list;
2235 	int token_list_size;
2236 	struct sof_ipc_comp comp = {
2237 		.core = SOF_DSP_PRIMARY_CORE,
2238 	};
2239 	int ret = 0;
2240 
2241 	swidget = kzalloc(sizeof(*swidget), GFP_KERNEL);
2242 	if (!swidget)
2243 		return -ENOMEM;
2244 
2245 	swidget->scomp = scomp;
2246 	swidget->widget = w;
2247 	swidget->comp_id = sdev->next_comp_id++;
2248 	swidget->complete = 0;
2249 	swidget->id = w->id;
2250 	swidget->pipeline_id = index;
2251 	swidget->private = NULL;
2252 
2253 	dev_dbg(scomp->dev, "tplg: ready widget id %d pipe %d type %d name : %s stream %s\n",
2254 		swidget->comp_id, index, swidget->id, tw->name,
2255 		strnlen(tw->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0
2256 			? tw->sname : "none");
2257 
2258 	token_list = widget_ops[w->id].token_list;
2259 	token_list_size = widget_ops[w->id].token_list_size;
2260 
2261 	ret = sof_parse_tokens(scomp, &comp, core_tokens,
2262 			       ARRAY_SIZE(core_tokens), tw->priv.array,
2263 			       le32_to_cpu(tw->priv.size));
2264 	if (ret != 0) {
2265 		dev_err(scomp->dev, "error: parsing core tokens failed %d\n",
2266 			ret);
2267 		kfree(swidget);
2268 		return ret;
2269 	}
2270 
2271 	if (sof_debug_check_flag(SOF_DBG_DISABLE_MULTICORE))
2272 		comp.core = SOF_DSP_PRIMARY_CORE;
2273 
2274 	swidget->core = comp.core;
2275 
2276 	ret = sof_parse_tokens(scomp, swidget, comp_ext_tokens, ARRAY_SIZE(comp_ext_tokens),
2277 			       tw->priv.array, le32_to_cpu(tw->priv.size));
2278 	if (ret != 0) {
2279 		dev_err(scomp->dev, "error: parsing comp_ext_tokens failed %d\n",
2280 			ret);
2281 		kfree(swidget);
2282 		return ret;
2283 	}
2284 
2285 	/* handle any special case widgets */
2286 	switch (w->id) {
2287 	case snd_soc_dapm_dai_in:
2288 	case snd_soc_dapm_dai_out:
2289 		dai = kzalloc(sizeof(*dai), GFP_KERNEL);
2290 		if (!dai) {
2291 			kfree(swidget);
2292 			return -ENOMEM;
2293 		}
2294 
2295 		ret = sof_widget_load_dai(scomp, index, swidget, tw, dai);
2296 		if (!ret)
2297 			ret = sof_connect_dai_widget(scomp, w, tw, dai);
2298 		if (ret < 0) {
2299 			kfree(dai);
2300 			break;
2301 		}
2302 		list_add(&dai->list, &sdev->dai_list);
2303 		swidget->private = dai;
2304 		break;
2305 	case snd_soc_dapm_pga:
2306 		if (!le32_to_cpu(tw->num_kcontrols)) {
2307 			dev_err(scomp->dev, "invalid kcontrol count %d for volume\n",
2308 				tw->num_kcontrols);
2309 			ret = -EINVAL;
2310 			break;
2311 		}
2312 
2313 		fallthrough;
2314 	case snd_soc_dapm_mixer:
2315 	case snd_soc_dapm_buffer:
2316 	case snd_soc_dapm_scheduler:
2317 	case snd_soc_dapm_aif_out:
2318 	case snd_soc_dapm_aif_in:
2319 		ret = sof_widget_parse_tokens(scomp, swidget, tw,  token_list, token_list_size);
2320 		break;
2321 	case snd_soc_dapm_src:
2322 		ret = sof_widget_load_src(scomp, index, swidget, tw);
2323 		break;
2324 	case snd_soc_dapm_asrc:
2325 		ret = sof_widget_load_asrc(scomp, index, swidget, tw);
2326 		break;
2327 	case snd_soc_dapm_siggen:
2328 		ret = sof_widget_load_siggen(scomp, index, swidget, tw);
2329 		break;
2330 	case snd_soc_dapm_effect:
2331 		ret = sof_widget_load_process(scomp, index, swidget, tw);
2332 		break;
2333 	case snd_soc_dapm_mux:
2334 	case snd_soc_dapm_demux:
2335 		ret = sof_widget_load_mux(scomp, index, swidget, tw);
2336 		break;
2337 	case snd_soc_dapm_switch:
2338 	case snd_soc_dapm_dai_link:
2339 	case snd_soc_dapm_kcontrol:
2340 	default:
2341 		dev_dbg(scomp->dev, "widget type %d name %s not handled\n", swidget->id, tw->name);
2342 		break;
2343 	}
2344 
2345 	/* check IPC reply */
2346 	if (ret < 0) {
2347 		dev_err(scomp->dev,
2348 			"error: failed to add widget id %d type %d name : %s stream %s\n",
2349 			tw->shift, swidget->id, tw->name,
2350 			strnlen(tw->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0
2351 				? tw->sname : "none");
2352 		kfree(swidget);
2353 		return ret;
2354 	}
2355 
2356 	/* bind widget to external event */
2357 	if (tw->event_type) {
2358 		ret = sof_widget_bind_event(scomp, swidget,
2359 					    le16_to_cpu(tw->event_type));
2360 		if (ret) {
2361 			dev_err(scomp->dev, "error: widget event binding failed\n");
2362 			kfree(swidget->private);
2363 			kfree(swidget->tuples);
2364 			kfree(swidget);
2365 			return ret;
2366 		}
2367 	}
2368 
2369 	w->dobj.private = swidget;
2370 	list_add(&swidget->list, &sdev->widget_list);
2371 	return ret;
2372 }
2373 
2374 static int sof_route_unload(struct snd_soc_component *scomp,
2375 			    struct snd_soc_dobj *dobj)
2376 {
2377 	struct snd_sof_route *sroute;
2378 
2379 	sroute = dobj->private;
2380 	if (!sroute)
2381 		return 0;
2382 
2383 	/* free sroute and its private data */
2384 	kfree(sroute->private);
2385 	list_del(&sroute->list);
2386 	kfree(sroute);
2387 
2388 	return 0;
2389 }
2390 
2391 static int sof_widget_unload(struct snd_soc_component *scomp,
2392 			     struct snd_soc_dobj *dobj)
2393 {
2394 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2395 	const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
2396 	const struct sof_ipc_tplg_widget_ops *widget_ops = ipc_tplg_ops->widget;
2397 	const struct snd_kcontrol_new *kc;
2398 	struct snd_soc_dapm_widget *widget;
2399 	struct snd_sof_control *scontrol;
2400 	struct snd_sof_widget *swidget;
2401 	struct soc_mixer_control *sm;
2402 	struct soc_bytes_ext *sbe;
2403 	struct snd_sof_dai *dai;
2404 	struct soc_enum *se;
2405 	int ret = 0;
2406 	int i;
2407 
2408 	swidget = dobj->private;
2409 	if (!swidget)
2410 		return 0;
2411 
2412 	widget = swidget->widget;
2413 
2414 	switch (swidget->id) {
2415 	case snd_soc_dapm_dai_in:
2416 	case snd_soc_dapm_dai_out:
2417 		dai = swidget->private;
2418 
2419 		if (dai) {
2420 			struct sof_dai_private_data *dai_data = dai->private;
2421 
2422 			kfree(dai_data->comp_dai);
2423 			kfree(dai_data->dai_config);
2424 			kfree(dai_data);
2425 			list_del(&dai->list);
2426 		}
2427 		break;
2428 	default:
2429 		break;
2430 	}
2431 	for (i = 0; i < widget->num_kcontrols; i++) {
2432 		kc = &widget->kcontrol_news[i];
2433 		switch (widget->dobj.widget.kcontrol_type[i]) {
2434 		case SND_SOC_TPLG_TYPE_MIXER:
2435 			sm = (struct soc_mixer_control *)kc->private_value;
2436 			scontrol = sm->dobj.private;
2437 			if (sm->max > 1)
2438 				kfree(scontrol->volume_table);
2439 			break;
2440 		case SND_SOC_TPLG_TYPE_ENUM:
2441 			se = (struct soc_enum *)kc->private_value;
2442 			scontrol = se->dobj.private;
2443 			break;
2444 		case SND_SOC_TPLG_TYPE_BYTES:
2445 			sbe = (struct soc_bytes_ext *)kc->private_value;
2446 			scontrol = sbe->dobj.private;
2447 			break;
2448 		default:
2449 			dev_warn(scomp->dev, "unsupported kcontrol_type\n");
2450 			goto out;
2451 		}
2452 		kfree(scontrol->control_data);
2453 		list_del(&scontrol->list);
2454 		kfree(scontrol);
2455 	}
2456 
2457 out:
2458 	/* free IPC related data */
2459 	if (widget_ops[swidget->id].ipc_free)
2460 		widget_ops[swidget->id].ipc_free(swidget);
2461 
2462 	/* free private value */
2463 	kfree(swidget->private);
2464 
2465 	kfree(swidget->tuples);
2466 
2467 	/* remove and free swidget object */
2468 	list_del(&swidget->list);
2469 	kfree(swidget);
2470 
2471 	return ret;
2472 }
2473 
2474 /*
2475  * DAI HW configuration.
2476  */
2477 
2478 /* FE DAI - used for any driver specific init */
2479 static int sof_dai_load(struct snd_soc_component *scomp, int index,
2480 			struct snd_soc_dai_driver *dai_drv,
2481 			struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
2482 {
2483 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2484 	struct snd_soc_tplg_stream_caps *caps;
2485 	struct snd_soc_tplg_private *private = &pcm->priv;
2486 	struct snd_sof_pcm *spcm;
2487 	int stream;
2488 	int ret;
2489 
2490 	/* nothing to do for BEs atm */
2491 	if (!pcm)
2492 		return 0;
2493 
2494 	spcm = kzalloc(sizeof(*spcm), GFP_KERNEL);
2495 	if (!spcm)
2496 		return -ENOMEM;
2497 
2498 	spcm->scomp = scomp;
2499 
2500 	for_each_pcm_streams(stream) {
2501 		spcm->stream[stream].comp_id = COMP_ID_UNASSIGNED;
2502 		if (pcm->compress)
2503 			snd_sof_compr_init_elapsed_work(&spcm->stream[stream].period_elapsed_work);
2504 		else
2505 			snd_sof_pcm_init_elapsed_work(&spcm->stream[stream].period_elapsed_work);
2506 	}
2507 
2508 	spcm->pcm = *pcm;
2509 	dev_dbg(scomp->dev, "tplg: load pcm %s\n", pcm->dai_name);
2510 
2511 	dai_drv->dobj.private = spcm;
2512 	list_add(&spcm->list, &sdev->pcm_list);
2513 
2514 	ret = sof_parse_tokens(scomp, spcm, stream_tokens,
2515 			       ARRAY_SIZE(stream_tokens), private->array,
2516 			       le32_to_cpu(private->size));
2517 	if (ret) {
2518 		dev_err(scomp->dev, "error: parse stream tokens failed %d\n",
2519 			le32_to_cpu(private->size));
2520 		return ret;
2521 	}
2522 
2523 	/* do we need to allocate playback PCM DMA pages */
2524 	if (!spcm->pcm.playback)
2525 		goto capture;
2526 
2527 	stream = SNDRV_PCM_STREAM_PLAYBACK;
2528 
2529 	dev_vdbg(scomp->dev, "tplg: pcm %s stream tokens: playback d0i3:%d\n",
2530 		 spcm->pcm.pcm_name, spcm->stream[stream].d0i3_compatible);
2531 
2532 	caps = &spcm->pcm.caps[stream];
2533 
2534 	/* allocate playback page table buffer */
2535 	ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev,
2536 				  PAGE_SIZE, &spcm->stream[stream].page_table);
2537 	if (ret < 0) {
2538 		dev_err(scomp->dev, "error: can't alloc page table for %s %d\n",
2539 			caps->name, ret);
2540 
2541 		return ret;
2542 	}
2543 
2544 	/* bind pcm to host comp */
2545 	ret = spcm_bind(scomp, spcm, stream);
2546 	if (ret) {
2547 		dev_err(scomp->dev,
2548 			"error: can't bind pcm to host\n");
2549 		goto free_playback_tables;
2550 	}
2551 
2552 capture:
2553 	stream = SNDRV_PCM_STREAM_CAPTURE;
2554 
2555 	/* do we need to allocate capture PCM DMA pages */
2556 	if (!spcm->pcm.capture)
2557 		return ret;
2558 
2559 	dev_vdbg(scomp->dev, "tplg: pcm %s stream tokens: capture d0i3:%d\n",
2560 		 spcm->pcm.pcm_name, spcm->stream[stream].d0i3_compatible);
2561 
2562 	caps = &spcm->pcm.caps[stream];
2563 
2564 	/* allocate capture page table buffer */
2565 	ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev,
2566 				  PAGE_SIZE, &spcm->stream[stream].page_table);
2567 	if (ret < 0) {
2568 		dev_err(scomp->dev, "error: can't alloc page table for %s %d\n",
2569 			caps->name, ret);
2570 		goto free_playback_tables;
2571 	}
2572 
2573 	/* bind pcm to host comp */
2574 	ret = spcm_bind(scomp, spcm, stream);
2575 	if (ret) {
2576 		dev_err(scomp->dev,
2577 			"error: can't bind pcm to host\n");
2578 		snd_dma_free_pages(&spcm->stream[stream].page_table);
2579 		goto free_playback_tables;
2580 	}
2581 
2582 	return ret;
2583 
2584 free_playback_tables:
2585 	if (spcm->pcm.playback)
2586 		snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table);
2587 
2588 	return ret;
2589 }
2590 
2591 static int sof_dai_unload(struct snd_soc_component *scomp,
2592 			  struct snd_soc_dobj *dobj)
2593 {
2594 	struct snd_sof_pcm *spcm = dobj->private;
2595 
2596 	/* free PCM DMA pages */
2597 	if (spcm->pcm.playback)
2598 		snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table);
2599 
2600 	if (spcm->pcm.capture)
2601 		snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_CAPTURE].page_table);
2602 
2603 	/* remove from list and free spcm */
2604 	list_del(&spcm->list);
2605 	kfree(spcm);
2606 
2607 	return 0;
2608 }
2609 
2610 static void sof_dai_set_format(struct snd_soc_tplg_hw_config *hw_config,
2611 			       struct sof_ipc_dai_config *config)
2612 {
2613 	/* clock directions wrt codec */
2614 	if (hw_config->bclk_provider == SND_SOC_TPLG_BCLK_CP) {
2615 		/* codec is bclk provider */
2616 		if (hw_config->fsync_provider == SND_SOC_TPLG_FSYNC_CP)
2617 			config->format |= SOF_DAI_FMT_CBP_CFP;
2618 		else
2619 			config->format |= SOF_DAI_FMT_CBP_CFC;
2620 	} else {
2621 		/* codec is bclk consumer */
2622 		if (hw_config->fsync_provider == SND_SOC_TPLG_FSYNC_CP)
2623 			config->format |= SOF_DAI_FMT_CBC_CFP;
2624 		else
2625 			config->format |= SOF_DAI_FMT_CBC_CFC;
2626 	}
2627 
2628 	/* inverted clocks ? */
2629 	if (hw_config->invert_bclk) {
2630 		if (hw_config->invert_fsync)
2631 			config->format |= SOF_DAI_FMT_IB_IF;
2632 		else
2633 			config->format |= SOF_DAI_FMT_IB_NF;
2634 	} else {
2635 		if (hw_config->invert_fsync)
2636 			config->format |= SOF_DAI_FMT_NB_IF;
2637 		else
2638 			config->format |= SOF_DAI_FMT_NB_NF;
2639 	}
2640 }
2641 
2642 /*
2643  * Send IPC and set the same config for all DAIs with name matching the link
2644  * name. Note that the function can only be used for the case that all DAIs
2645  * have a common DAI config for now.
2646  */
2647 static int sof_set_dai_config_multi(struct snd_sof_dev *sdev, u32 size,
2648 				    struct snd_soc_dai_link *link,
2649 				    struct sof_ipc_dai_config *config,
2650 				    int num_conf, int curr_conf)
2651 {
2652 	struct sof_dai_private_data *dai_data;
2653 	struct snd_sof_dai *dai;
2654 	int found = 0;
2655 	int i;
2656 
2657 	list_for_each_entry(dai, &sdev->dai_list, list) {
2658 		dai_data = dai->private;
2659 		if (!dai->name)
2660 			continue;
2661 
2662 		if (strcmp(link->name, dai->name) == 0) {
2663 			/*
2664 			 * the same dai config will be applied to all DAIs in
2665 			 * the same dai link. We have to ensure that the ipc
2666 			 * dai config's dai_index match to the component's
2667 			 * dai_index.
2668 			 */
2669 			for (i = 0; i < num_conf; i++)
2670 				config[i].dai_index = dai_data->comp_dai->dai_index;
2671 
2672 			dev_dbg(sdev->dev, "set DAI config for %s index %d\n",
2673 				dai->name, config[curr_conf].dai_index);
2674 
2675 			dai->number_configs = num_conf;
2676 			dai->current_config = curr_conf;
2677 			dai_data->dai_config = kmemdup(config, size * num_conf, GFP_KERNEL);
2678 			if (!dai_data->dai_config)
2679 				return -ENOMEM;
2680 
2681 			found = 1;
2682 		}
2683 	}
2684 
2685 	/*
2686 	 * machine driver may define a dai link with playback and capture
2687 	 * dai enabled, but the dai link in topology would support both, one
2688 	 * or none of them. Here print a warning message to notify user
2689 	 */
2690 	if (!found) {
2691 		dev_warn(sdev->dev, "warning: failed to find dai for dai link %s",
2692 			 link->name);
2693 	}
2694 
2695 	return 0;
2696 }
2697 
2698 static int sof_set_dai_config(struct snd_sof_dev *sdev, u32 size,
2699 			      struct snd_soc_dai_link *link,
2700 			      struct sof_ipc_dai_config *config)
2701 {
2702 	return sof_set_dai_config_multi(sdev, size, link, config, 1, 0);
2703 }
2704 
2705 static int sof_link_ssp_load(struct snd_soc_component *scomp, int index,
2706 			     struct snd_soc_dai_link *link,
2707 			     struct snd_soc_tplg_link_config *cfg,
2708 			     struct snd_soc_tplg_hw_config *hw_config,
2709 			     struct sof_ipc_dai_config *config, int curr_conf)
2710 {
2711 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2712 	struct snd_soc_tplg_private *private = &cfg->priv;
2713 	int num_conf = le32_to_cpu(cfg->num_hw_configs);
2714 	u32 size = sizeof(*config);
2715 	int ret;
2716 	int i;
2717 
2718 	/*
2719 	 * Parse common data, we should have 1 common data per hw_config.
2720 	 */
2721 	ret = sof_parse_token_sets(scomp, &config->ssp, ssp_tokens,
2722 				   ARRAY_SIZE(ssp_tokens), private->array,
2723 				   le32_to_cpu(private->size),
2724 				   num_conf, size);
2725 
2726 	if (ret != 0) {
2727 		dev_err(scomp->dev, "error: parse ssp tokens failed %d\n",
2728 			le32_to_cpu(private->size));
2729 		return ret;
2730 	}
2731 
2732 	/* process all possible hw configs */
2733 	for (i = 0; i < num_conf; i++) {
2734 
2735 		/* handle master/slave and inverted clocks */
2736 		sof_dai_set_format(&hw_config[i], &config[i]);
2737 
2738 		config[i].hdr.size = size;
2739 
2740 		/* copy differentiating hw configs to ipc structs */
2741 		config[i].ssp.mclk_rate = le32_to_cpu(hw_config[i].mclk_rate);
2742 		config[i].ssp.bclk_rate = le32_to_cpu(hw_config[i].bclk_rate);
2743 		config[i].ssp.fsync_rate = le32_to_cpu(hw_config[i].fsync_rate);
2744 		config[i].ssp.tdm_slots = le32_to_cpu(hw_config[i].tdm_slots);
2745 		config[i].ssp.tdm_slot_width = le32_to_cpu(hw_config[i].tdm_slot_width);
2746 		config[i].ssp.mclk_direction = hw_config[i].mclk_direction;
2747 		config[i].ssp.rx_slots = le32_to_cpu(hw_config[i].rx_slots);
2748 		config[i].ssp.tx_slots = le32_to_cpu(hw_config[i].tx_slots);
2749 
2750 		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",
2751 			config[i].dai_index, config[i].format,
2752 			config[i].ssp.mclk_rate, config[i].ssp.bclk_rate,
2753 			config[i].ssp.fsync_rate, config[i].ssp.sample_valid_bits,
2754 			config[i].ssp.tdm_slot_width, config[i].ssp.tdm_slots,
2755 			config[i].ssp.mclk_id, config[i].ssp.quirks, config[i].ssp.clks_control);
2756 
2757 		/* validate SSP fsync rate and channel count */
2758 		if (config[i].ssp.fsync_rate < 8000 || config[i].ssp.fsync_rate > 192000) {
2759 			dev_err(scomp->dev, "error: invalid fsync rate for SSP%d\n",
2760 				config[i].dai_index);
2761 			return -EINVAL;
2762 		}
2763 
2764 		if (config[i].ssp.tdm_slots < 1 || config[i].ssp.tdm_slots > 8) {
2765 			dev_err(scomp->dev, "error: invalid channel count for SSP%d\n",
2766 				config[i].dai_index);
2767 			return -EINVAL;
2768 		}
2769 	}
2770 
2771 	/* set config for all DAI's with name matching the link name */
2772 	ret = sof_set_dai_config_multi(sdev, size, link, config, num_conf, curr_conf);
2773 	if (ret < 0)
2774 		dev_err(scomp->dev, "error: failed to save DAI config for SSP%d\n",
2775 			config->dai_index);
2776 
2777 	return ret;
2778 }
2779 
2780 static int sof_link_sai_load(struct snd_soc_component *scomp, int index,
2781 			     struct snd_soc_dai_link *link,
2782 			     struct snd_soc_tplg_link_config *cfg,
2783 			     struct snd_soc_tplg_hw_config *hw_config,
2784 			     struct sof_ipc_dai_config *config)
2785 {
2786 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2787 	struct snd_soc_tplg_private *private = &cfg->priv;
2788 	u32 size = sizeof(*config);
2789 	int ret;
2790 
2791 	/* handle master/slave and inverted clocks */
2792 	sof_dai_set_format(hw_config, config);
2793 
2794 	/* init IPC */
2795 	memset(&config->sai, 0, sizeof(struct sof_ipc_dai_sai_params));
2796 	config->hdr.size = size;
2797 
2798 	ret = sof_parse_tokens(scomp, &config->sai, sai_tokens,
2799 			       ARRAY_SIZE(sai_tokens), private->array,
2800 			       le32_to_cpu(private->size));
2801 	if (ret != 0) {
2802 		dev_err(scomp->dev, "error: parse sai tokens failed %d\n",
2803 			le32_to_cpu(private->size));
2804 		return ret;
2805 	}
2806 
2807 	config->sai.mclk_rate = le32_to_cpu(hw_config->mclk_rate);
2808 	config->sai.bclk_rate = le32_to_cpu(hw_config->bclk_rate);
2809 	config->sai.fsync_rate = le32_to_cpu(hw_config->fsync_rate);
2810 	config->sai.mclk_direction = hw_config->mclk_direction;
2811 
2812 	config->sai.tdm_slots = le32_to_cpu(hw_config->tdm_slots);
2813 	config->sai.tdm_slot_width = le32_to_cpu(hw_config->tdm_slot_width);
2814 	config->sai.rx_slots = le32_to_cpu(hw_config->rx_slots);
2815 	config->sai.tx_slots = le32_to_cpu(hw_config->tx_slots);
2816 
2817 	dev_info(scomp->dev,
2818 		 "tplg: config SAI%d fmt 0x%x mclk %d width %d slots %d mclk id %d\n",
2819 		config->dai_index, config->format,
2820 		config->sai.mclk_rate, config->sai.tdm_slot_width,
2821 		config->sai.tdm_slots, config->sai.mclk_id);
2822 
2823 	if (config->sai.tdm_slots < 1 || config->sai.tdm_slots > 8) {
2824 		dev_err(scomp->dev, "error: invalid channel count for SAI%d\n",
2825 			config->dai_index);
2826 		return -EINVAL;
2827 	}
2828 
2829 	/* set config for all DAI's with name matching the link name */
2830 	ret = sof_set_dai_config(sdev, size, link, config);
2831 	if (ret < 0)
2832 		dev_err(scomp->dev, "error: failed to save DAI config for SAI%d\n",
2833 			config->dai_index);
2834 
2835 	return ret;
2836 }
2837 
2838 static int sof_link_esai_load(struct snd_soc_component *scomp, int index,
2839 			      struct snd_soc_dai_link *link,
2840 			      struct snd_soc_tplg_link_config *cfg,
2841 			      struct snd_soc_tplg_hw_config *hw_config,
2842 			      struct sof_ipc_dai_config *config)
2843 {
2844 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2845 	struct snd_soc_tplg_private *private = &cfg->priv;
2846 	u32 size = sizeof(*config);
2847 	int ret;
2848 
2849 	/* handle master/slave and inverted clocks */
2850 	sof_dai_set_format(hw_config, config);
2851 
2852 	/* init IPC */
2853 	memset(&config->esai, 0, sizeof(struct sof_ipc_dai_esai_params));
2854 	config->hdr.size = size;
2855 
2856 	ret = sof_parse_tokens(scomp, &config->esai, esai_tokens,
2857 			       ARRAY_SIZE(esai_tokens), private->array,
2858 			       le32_to_cpu(private->size));
2859 	if (ret != 0) {
2860 		dev_err(scomp->dev, "error: parse esai tokens failed %d\n",
2861 			le32_to_cpu(private->size));
2862 		return ret;
2863 	}
2864 
2865 	config->esai.mclk_rate = le32_to_cpu(hw_config->mclk_rate);
2866 	config->esai.bclk_rate = le32_to_cpu(hw_config->bclk_rate);
2867 	config->esai.fsync_rate = le32_to_cpu(hw_config->fsync_rate);
2868 	config->esai.mclk_direction = hw_config->mclk_direction;
2869 	config->esai.tdm_slots = le32_to_cpu(hw_config->tdm_slots);
2870 	config->esai.tdm_slot_width = le32_to_cpu(hw_config->tdm_slot_width);
2871 	config->esai.rx_slots = le32_to_cpu(hw_config->rx_slots);
2872 	config->esai.tx_slots = le32_to_cpu(hw_config->tx_slots);
2873 
2874 	dev_info(scomp->dev,
2875 		 "tplg: config ESAI%d fmt 0x%x mclk %d width %d slots %d mclk id %d\n",
2876 		config->dai_index, config->format,
2877 		config->esai.mclk_rate, config->esai.tdm_slot_width,
2878 		config->esai.tdm_slots, config->esai.mclk_id);
2879 
2880 	if (config->esai.tdm_slots < 1 || config->esai.tdm_slots > 8) {
2881 		dev_err(scomp->dev, "error: invalid channel count for ESAI%d\n",
2882 			config->dai_index);
2883 		return -EINVAL;
2884 	}
2885 
2886 	/* set config for all DAI's with name matching the link name */
2887 	ret = sof_set_dai_config(sdev, size, link, config);
2888 	if (ret < 0)
2889 		dev_err(scomp->dev, "error: failed to save DAI config for ESAI%d\n",
2890 			config->dai_index);
2891 
2892 	return ret;
2893 }
2894 
2895 static int sof_link_acp_dmic_load(struct snd_soc_component *scomp, int index,
2896 				  struct snd_soc_dai_link *link,
2897 				  struct snd_soc_tplg_link_config *cfg,
2898 				  struct snd_soc_tplg_hw_config *hw_config,
2899 				  struct sof_ipc_dai_config *config)
2900 {
2901 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2902 	u32 size = sizeof(*config);
2903 	int ret;
2904 
2905        /* handle master/slave and inverted clocks */
2906 	sof_dai_set_format(hw_config, config);
2907 
2908 	/* init IPC */
2909 	memset(&config->acpdmic, 0, sizeof(struct sof_ipc_dai_acp_params));
2910 	config->hdr.size = size;
2911 
2912 	config->acpdmic.fsync_rate = le32_to_cpu(hw_config->fsync_rate);
2913 	config->acpdmic.tdm_slots = le32_to_cpu(hw_config->tdm_slots);
2914 
2915 	dev_info(scomp->dev, "ACP_DMIC config ACP%d channel %d rate %d\n",
2916 		 config->dai_index, config->acpdmic.tdm_slots,
2917 		 config->acpdmic.fsync_rate);
2918 
2919 	/* set config for all DAI's with name matching the link name */
2920 	ret = sof_set_dai_config(sdev, size, link, config);
2921 	if (ret < 0)
2922 		dev_err(scomp->dev, "ACP_DMIC failed to save DAI config for ACP%d\n",
2923 			config->dai_index);
2924 	return ret;
2925 }
2926 
2927 static int sof_link_acp_bt_load(struct snd_soc_component *scomp, int index,
2928 				struct snd_soc_dai_link *link,
2929 				struct snd_soc_tplg_link_config *cfg,
2930 				struct snd_soc_tplg_hw_config *hw_config,
2931 				struct sof_ipc_dai_config *config)
2932 {
2933 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2934 	u32 size = sizeof(*config);
2935 	int ret;
2936 
2937 	/* handle master/slave and inverted clocks */
2938 	sof_dai_set_format(hw_config, config);
2939 
2940 	/* init IPC */
2941 	memset(&config->acpbt, 0, sizeof(struct sof_ipc_dai_acp_params));
2942 	config->hdr.size = size;
2943 
2944 	config->acpbt.fsync_rate = le32_to_cpu(hw_config->fsync_rate);
2945 	config->acpbt.tdm_slots = le32_to_cpu(hw_config->tdm_slots);
2946 
2947 	dev_info(scomp->dev, "ACP_BT config ACP%d channel %d rate %d\n",
2948 		 config->dai_index, config->acpbt.tdm_slots,
2949 		 config->acpbt.fsync_rate);
2950 
2951 	/* set config for all DAI's with name matching the link name */
2952 	ret = sof_set_dai_config(sdev, size, link, config);
2953 	if (ret < 0)
2954 		dev_err(scomp->dev, "ACP_BT failed to save DAI config for ACP%d\n",
2955 			config->dai_index);
2956 	return ret;
2957 }
2958 
2959 static int sof_link_acp_sp_load(struct snd_soc_component *scomp, int index,
2960 				struct snd_soc_dai_link *link,
2961 				struct snd_soc_tplg_link_config *cfg,
2962 				struct snd_soc_tplg_hw_config *hw_config,
2963 				struct sof_ipc_dai_config *config)
2964 {
2965 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2966 	u32 size = sizeof(*config);
2967 	int ret;
2968 
2969 	/* handle master/slave and inverted clocks */
2970 	sof_dai_set_format(hw_config, config);
2971 
2972 	/* init IPC */
2973 	memset(&config->acpsp, 0, sizeof(struct sof_ipc_dai_acp_params));
2974 	config->hdr.size = size;
2975 
2976 	config->acpsp.fsync_rate = le32_to_cpu(hw_config->fsync_rate);
2977 	config->acpsp.tdm_slots = le32_to_cpu(hw_config->tdm_slots);
2978 
2979 	dev_info(scomp->dev, "ACP_SP config ACP%d channel %d rate %d\n",
2980 		 config->dai_index, config->acpsp.tdm_slots,
2981 		 config->acpsp.fsync_rate);
2982 
2983 	/* set config for all DAI's with name matching the link name */
2984 	ret = sof_set_dai_config(sdev, size, link, config);
2985 	if (ret < 0)
2986 		dev_err(scomp->dev, "ACP_SP failed to save DAI config for ACP%d\n",
2987 			config->dai_index);
2988 	return ret;
2989 }
2990 
2991 static int sof_link_afe_load(struct snd_soc_component *scomp, int index,
2992 			     struct snd_soc_dai_link *link,
2993 			     struct snd_soc_tplg_link_config *cfg,
2994 			     struct snd_soc_tplg_hw_config *hw_config,
2995 			     struct sof_ipc_dai_config *config)
2996 {
2997 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2998 	struct snd_soc_tplg_private *private = &cfg->priv;
2999 	u32 size = sizeof(*config);
3000 	int ret;
3001 
3002 	config->hdr.size = size;
3003 
3004 	/* get any bespoke DAI tokens */
3005 	ret = sof_parse_tokens(scomp, &config->afe, afe_tokens,
3006 			       ARRAY_SIZE(afe_tokens), private->array,
3007 			       le32_to_cpu(private->size));
3008 	if (ret != 0) {
3009 		dev_err(scomp->dev, "parse afe tokens failed %d\n",
3010 			le32_to_cpu(private->size));
3011 		return ret;
3012 	}
3013 
3014 	dev_dbg(scomp->dev, "AFE config rate %d channels %d format:%d\n",
3015 		config->afe.rate, config->afe.channels, config->afe.format);
3016 
3017 	config->afe.stream_id = DMA_CHAN_INVALID;
3018 
3019 	ret = sof_set_dai_config(sdev, size, link, config);
3020 	if (ret < 0)
3021 		dev_err(scomp->dev, "failed to process afe dai link %s", link->name);
3022 
3023 	return ret;
3024 }
3025 
3026 static int sof_link_dmic_load(struct snd_soc_component *scomp, int index,
3027 			      struct snd_soc_dai_link *link,
3028 			      struct snd_soc_tplg_link_config *cfg,
3029 			      struct snd_soc_tplg_hw_config *hw_config,
3030 			      struct sof_ipc_dai_config *config)
3031 {
3032 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3033 	struct snd_soc_tplg_private *private = &cfg->priv;
3034 	struct sof_ipc_fw_ready *ready = &sdev->fw_ready;
3035 	struct sof_ipc_fw_version *v = &ready->version;
3036 	size_t size = sizeof(*config);
3037 	int ret, j;
3038 
3039 	/* Ensure the entire DMIC config struct is zeros */
3040 	memset(&config->dmic, 0, sizeof(struct sof_ipc_dai_dmic_params));
3041 
3042 	/* get DMIC tokens */
3043 	ret = sof_parse_tokens(scomp, &config->dmic, dmic_tokens,
3044 			       ARRAY_SIZE(dmic_tokens), private->array,
3045 			       le32_to_cpu(private->size));
3046 	if (ret != 0) {
3047 		dev_err(scomp->dev, "error: parse dmic tokens failed %d\n",
3048 			le32_to_cpu(private->size));
3049 		return ret;
3050 	}
3051 
3052 	/* get DMIC PDM tokens */
3053 	ret = sof_parse_token_sets(scomp, &config->dmic.pdm[0], dmic_pdm_tokens,
3054 			       ARRAY_SIZE(dmic_pdm_tokens), private->array,
3055 			       le32_to_cpu(private->size),
3056 			       config->dmic.num_pdm_active,
3057 			       sizeof(struct sof_ipc_dai_dmic_pdm_ctrl));
3058 
3059 	if (ret != 0) {
3060 		dev_err(scomp->dev, "error: parse dmic pdm tokens failed %d\n",
3061 			le32_to_cpu(private->size));
3062 		return ret;
3063 	}
3064 
3065 	/* set IPC header size */
3066 	config->hdr.size = size;
3067 
3068 	/* debug messages */
3069 	dev_dbg(scomp->dev, "tplg: config DMIC%d driver version %d\n",
3070 		config->dai_index, config->dmic.driver_ipc_version);
3071 	dev_dbg(scomp->dev, "pdmclk_min %d pdm_clkmax %d duty_min %hd\n",
3072 		config->dmic.pdmclk_min, config->dmic.pdmclk_max,
3073 		config->dmic.duty_min);
3074 	dev_dbg(scomp->dev, "duty_max %hd fifo_fs %d num_pdms active %d\n",
3075 		config->dmic.duty_max, config->dmic.fifo_fs,
3076 		config->dmic.num_pdm_active);
3077 	dev_dbg(scomp->dev, "fifo word length %hd\n", config->dmic.fifo_bits);
3078 
3079 	for (j = 0; j < config->dmic.num_pdm_active; j++) {
3080 		dev_dbg(scomp->dev, "pdm %hd mic a %hd mic b %hd\n",
3081 			config->dmic.pdm[j].id,
3082 			config->dmic.pdm[j].enable_mic_a,
3083 			config->dmic.pdm[j].enable_mic_b);
3084 		dev_dbg(scomp->dev, "pdm %hd polarity a %hd polarity b %hd\n",
3085 			config->dmic.pdm[j].id,
3086 			config->dmic.pdm[j].polarity_mic_a,
3087 			config->dmic.pdm[j].polarity_mic_b);
3088 		dev_dbg(scomp->dev, "pdm %hd clk_edge %hd skew %hd\n",
3089 			config->dmic.pdm[j].id,
3090 			config->dmic.pdm[j].clk_edge,
3091 			config->dmic.pdm[j].skew);
3092 	}
3093 
3094 	/*
3095 	 * this takes care of backwards compatible handling of fifo_bits_b.
3096 	 * It is deprecated since firmware ABI version 3.0.1.
3097 	 */
3098 	if (SOF_ABI_VER(v->major, v->minor, v->micro) < SOF_ABI_VER(3, 0, 1))
3099 		config->dmic.fifo_bits_b = config->dmic.fifo_bits;
3100 
3101 	/* set config for all DAI's with name matching the link name */
3102 	ret = sof_set_dai_config(sdev, size, link, config);
3103 	if (ret < 0)
3104 		dev_err(scomp->dev, "error: failed to save DAI config for DMIC%d\n",
3105 			config->dai_index);
3106 
3107 	return ret;
3108 }
3109 
3110 static int sof_link_hda_load(struct snd_soc_component *scomp, int index,
3111 			     struct snd_soc_dai_link *link,
3112 			     struct snd_soc_tplg_link_config *cfg,
3113 			     struct snd_soc_tplg_hw_config *hw_config,
3114 			     struct sof_ipc_dai_config *config)
3115 {
3116 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3117 	struct snd_soc_tplg_private *private = &cfg->priv;
3118 	u32 size = sizeof(*config);
3119 	int ret;
3120 
3121 	/* init IPC */
3122 	memset(&config->hda, 0, sizeof(struct sof_ipc_dai_hda_params));
3123 	config->hdr.size = size;
3124 
3125 	/* get any bespoke DAI tokens */
3126 	ret = sof_parse_tokens(scomp, &config->hda, hda_tokens,
3127 			       ARRAY_SIZE(hda_tokens), private->array,
3128 			       le32_to_cpu(private->size));
3129 	if (ret != 0) {
3130 		dev_err(scomp->dev, "error: parse hda tokens failed %d\n",
3131 			le32_to_cpu(private->size));
3132 		return ret;
3133 	}
3134 
3135 	dev_dbg(scomp->dev, "HDA config rate %d channels %d\n",
3136 		config->hda.rate, config->hda.channels);
3137 
3138 	config->hda.link_dma_ch = DMA_CHAN_INVALID;
3139 
3140 	ret = sof_set_dai_config(sdev, size, link, config);
3141 	if (ret < 0)
3142 		dev_err(scomp->dev, "error: failed to process hda dai link %s",
3143 			link->name);
3144 
3145 	return ret;
3146 }
3147 
3148 static int sof_link_alh_load(struct snd_soc_component *scomp, int index,
3149 			     struct snd_soc_dai_link *link,
3150 			     struct snd_soc_tplg_link_config *cfg,
3151 			     struct snd_soc_tplg_hw_config *hw_config,
3152 			     struct sof_ipc_dai_config *config)
3153 {
3154 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3155 	struct snd_soc_tplg_private *private = &cfg->priv;
3156 	u32 size = sizeof(*config);
3157 	int ret;
3158 
3159 	ret = sof_parse_tokens(scomp, &config->alh, alh_tokens,
3160 			       ARRAY_SIZE(alh_tokens), private->array,
3161 			       le32_to_cpu(private->size));
3162 	if (ret != 0) {
3163 		dev_err(scomp->dev, "error: parse alh tokens failed %d\n",
3164 			le32_to_cpu(private->size));
3165 		return ret;
3166 	}
3167 
3168 	/* init IPC */
3169 	config->hdr.size = size;
3170 
3171 	/* set config for all DAI's with name matching the link name */
3172 	ret = sof_set_dai_config(sdev, size, link, config);
3173 	if (ret < 0)
3174 		dev_err(scomp->dev, "error: failed to save DAI config for ALH %d\n",
3175 			config->dai_index);
3176 
3177 	return ret;
3178 }
3179 
3180 /* DAI link - used for any driver specific init */
3181 static int sof_link_load(struct snd_soc_component *scomp, int index,
3182 			 struct snd_soc_dai_link *link,
3183 			 struct snd_soc_tplg_link_config *cfg)
3184 {
3185 	struct snd_soc_tplg_private *private = &cfg->priv;
3186 	struct snd_soc_tplg_hw_config *hw_config;
3187 	struct sof_ipc_dai_config common_config;
3188 	struct sof_ipc_dai_config *config;
3189 	int curr_conf;
3190 	int num_conf;
3191 	int ret;
3192 	int i;
3193 
3194 	if (!link->platforms) {
3195 		dev_err(scomp->dev, "error: no platforms\n");
3196 		return -EINVAL;
3197 	}
3198 	link->platforms->name = dev_name(scomp->dev);
3199 
3200 	/*
3201 	 * Set nonatomic property for FE dai links as their trigger action
3202 	 * involves IPC's.
3203 	 */
3204 	if (!link->no_pcm) {
3205 		link->nonatomic = true;
3206 
3207 		/*
3208 		 * set default trigger order for all links. Exceptions to
3209 		 * the rule will be handled in sof_pcm_dai_link_fixup()
3210 		 * For playback, the sequence is the following: start FE,
3211 		 * start BE, stop BE, stop FE; for Capture the sequence is
3212 		 * inverted start BE, start FE, stop FE, stop BE
3213 		 */
3214 		link->trigger[SNDRV_PCM_STREAM_PLAYBACK] =
3215 					SND_SOC_DPCM_TRIGGER_PRE;
3216 		link->trigger[SNDRV_PCM_STREAM_CAPTURE] =
3217 					SND_SOC_DPCM_TRIGGER_POST;
3218 
3219 		/* nothing more to do for FE dai links */
3220 		return 0;
3221 	}
3222 
3223 	/* check we have some tokens - we need at least DAI type */
3224 	if (le32_to_cpu(private->size) == 0) {
3225 		dev_err(scomp->dev, "error: expected tokens for DAI, none found\n");
3226 		return -EINVAL;
3227 	}
3228 
3229 	memset(&common_config, 0, sizeof(common_config));
3230 
3231 	/* get any common DAI tokens */
3232 	ret = sof_parse_tokens(scomp, &common_config, dai_link_tokens, ARRAY_SIZE(dai_link_tokens),
3233 			       private->array, le32_to_cpu(private->size));
3234 	if (ret != 0) {
3235 		dev_err(scomp->dev, "error: parse link tokens failed %d\n",
3236 			le32_to_cpu(private->size));
3237 		return ret;
3238 	}
3239 
3240 	/*
3241 	 * DAI links are expected to have at least 1 hw_config.
3242 	 * But some older topologies might have no hw_config for HDA dai links.
3243 	 */
3244 	hw_config = cfg->hw_config;
3245 	num_conf = le32_to_cpu(cfg->num_hw_configs);
3246 	if (!num_conf) {
3247 		if (common_config.type != SOF_DAI_INTEL_HDA) {
3248 			dev_err(scomp->dev, "error: unexpected DAI config count %d!\n",
3249 				le32_to_cpu(cfg->num_hw_configs));
3250 			return -EINVAL;
3251 		}
3252 		num_conf = 1;
3253 		curr_conf = 0;
3254 	} else {
3255 		dev_dbg(scomp->dev, "tplg: %d hw_configs found, default id: %d!\n",
3256 			cfg->num_hw_configs, le32_to_cpu(cfg->default_hw_config_id));
3257 
3258 		for (curr_conf = 0; curr_conf < num_conf; curr_conf++) {
3259 			if (hw_config[curr_conf].id == cfg->default_hw_config_id)
3260 				break;
3261 		}
3262 
3263 		if (curr_conf == num_conf) {
3264 			dev_err(scomp->dev, "error: default hw_config id: %d not found!\n",
3265 				le32_to_cpu(cfg->default_hw_config_id));
3266 			return -EINVAL;
3267 		}
3268 	}
3269 
3270 	/* Reserve memory for all hw configs, eventually freed by widget */
3271 	config = kcalloc(num_conf, sizeof(*config), GFP_KERNEL);
3272 	if (!config)
3273 		return -ENOMEM;
3274 
3275 	/* Copy common data to all config ipc structs */
3276 	for (i = 0; i < num_conf; i++) {
3277 		config[i].hdr.cmd = SOF_IPC_GLB_DAI_MSG | SOF_IPC_DAI_CONFIG;
3278 		config[i].format = le32_to_cpu(hw_config[i].fmt);
3279 		config[i].type = common_config.type;
3280 		config[i].dai_index = common_config.dai_index;
3281 	}
3282 
3283 	/* now load DAI specific data and send IPC - type comes from token */
3284 	switch (common_config.type) {
3285 	case SOF_DAI_INTEL_SSP:
3286 		ret = sof_link_ssp_load(scomp, index, link, cfg, hw_config, config, curr_conf);
3287 		break;
3288 	case SOF_DAI_INTEL_DMIC:
3289 		ret = sof_link_dmic_load(scomp, index, link, cfg, hw_config + curr_conf, config);
3290 		break;
3291 	case SOF_DAI_INTEL_HDA:
3292 		ret = sof_link_hda_load(scomp, index, link, cfg, hw_config + curr_conf, config);
3293 		break;
3294 	case SOF_DAI_INTEL_ALH:
3295 		ret = sof_link_alh_load(scomp, index, link, cfg, hw_config + curr_conf, config);
3296 		break;
3297 	case SOF_DAI_IMX_SAI:
3298 		ret = sof_link_sai_load(scomp, index, link, cfg, hw_config + curr_conf, config);
3299 		break;
3300 	case SOF_DAI_IMX_ESAI:
3301 		ret = sof_link_esai_load(scomp, index, link, cfg, hw_config + curr_conf, config);
3302 		break;
3303 	case SOF_DAI_AMD_BT:
3304 		ret = sof_link_acp_bt_load(scomp, index, link, cfg, hw_config + curr_conf, config);
3305 		break;
3306 	case SOF_DAI_AMD_SP:
3307 		ret = sof_link_acp_sp_load(scomp, index, link, cfg, hw_config + curr_conf, config);
3308 		break;
3309 	case SOF_DAI_AMD_DMIC:
3310 		ret = sof_link_acp_dmic_load(scomp, index, link, cfg, hw_config + curr_conf,
3311 					     config);
3312 		break;
3313 	case SOF_DAI_MEDIATEK_AFE:
3314 		ret = sof_link_afe_load(scomp, index, link, cfg, hw_config + curr_conf, config);
3315 		break;
3316 	default:
3317 		dev_err(scomp->dev, "error: invalid DAI type %d\n", common_config.type);
3318 		ret = -EINVAL;
3319 		break;
3320 	}
3321 
3322 	kfree(config);
3323 
3324 	return ret;
3325 }
3326 
3327 /* DAI link - used for any driver specific init */
3328 static int sof_route_load(struct snd_soc_component *scomp, int index,
3329 			  struct snd_soc_dapm_route *route)
3330 {
3331 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3332 	struct snd_sof_widget *source_swidget, *sink_swidget;
3333 	struct snd_soc_dobj *dobj = &route->dobj;
3334 	struct snd_sof_route *sroute;
3335 	int ret = 0;
3336 
3337 	/* allocate memory for sroute and connect */
3338 	sroute = kzalloc(sizeof(*sroute), GFP_KERNEL);
3339 	if (!sroute)
3340 		return -ENOMEM;
3341 
3342 	sroute->scomp = scomp;
3343 	dev_dbg(scomp->dev, "sink %s control %s source %s\n",
3344 		route->sink, route->control ? route->control : "none",
3345 		route->source);
3346 
3347 	/* source component */
3348 	source_swidget = snd_sof_find_swidget(scomp, (char *)route->source);
3349 	if (!source_swidget) {
3350 		dev_err(scomp->dev, "error: source %s not found\n",
3351 			route->source);
3352 		ret = -EINVAL;
3353 		goto err;
3354 	}
3355 
3356 	/*
3357 	 * Virtual widgets of type output/out_drv may be added in topology
3358 	 * for compatibility. These are not handled by the FW.
3359 	 * So, don't send routes whose source/sink widget is of such types
3360 	 * to the DSP.
3361 	 */
3362 	if (source_swidget->id == snd_soc_dapm_out_drv ||
3363 	    source_swidget->id == snd_soc_dapm_output)
3364 		goto err;
3365 
3366 	/* sink component */
3367 	sink_swidget = snd_sof_find_swidget(scomp, (char *)route->sink);
3368 	if (!sink_swidget) {
3369 		dev_err(scomp->dev, "error: sink %s not found\n",
3370 			route->sink);
3371 		ret = -EINVAL;
3372 		goto err;
3373 	}
3374 
3375 	/*
3376 	 * Don't send routes whose sink widget is of type
3377 	 * output or out_drv to the DSP
3378 	 */
3379 	if (sink_swidget->id == snd_soc_dapm_out_drv ||
3380 	    sink_swidget->id == snd_soc_dapm_output)
3381 		goto err;
3382 
3383 	/*
3384 	 * For virtual routes, both sink and source are not
3385 	 * buffer. Since only buffer linked to component is supported by
3386 	 * FW, others are reported as error, add check in route function,
3387 	 * do not send it to FW when both source and sink are not buffer
3388 	 */
3389 	if (source_swidget->id != snd_soc_dapm_buffer &&
3390 	    sink_swidget->id != snd_soc_dapm_buffer) {
3391 		dev_dbg(scomp->dev, "warning: neither Linked source component %s nor sink component %s is of buffer type, ignoring link\n",
3392 			route->source, route->sink);
3393 		goto err;
3394 	} else {
3395 		sroute->route = route;
3396 		dobj->private = sroute;
3397 		sroute->src_widget = source_swidget;
3398 		sroute->sink_widget = sink_swidget;
3399 
3400 		/* add route to route list */
3401 		list_add(&sroute->list, &sdev->route_list);
3402 
3403 		return 0;
3404 	}
3405 
3406 err:
3407 	kfree(sroute);
3408 	return ret;
3409 }
3410 
3411 int snd_sof_complete_pipeline(struct snd_sof_dev *sdev,
3412 			      struct snd_sof_widget *swidget)
3413 {
3414 	struct sof_ipc_pipe_ready ready;
3415 	struct sof_ipc_reply reply;
3416 	int ret;
3417 
3418 	dev_dbg(sdev->dev, "tplg: complete pipeline %s id %d\n",
3419 		swidget->widget->name, swidget->comp_id);
3420 
3421 	memset(&ready, 0, sizeof(ready));
3422 	ready.hdr.size = sizeof(ready);
3423 	ready.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_PIPE_COMPLETE;
3424 	ready.comp_id = swidget->comp_id;
3425 
3426 	ret = sof_ipc_tx_message(sdev->ipc,
3427 				 ready.hdr.cmd, &ready, sizeof(ready), &reply,
3428 				 sizeof(reply));
3429 	if (ret < 0)
3430 		return ret;
3431 	return 1;
3432 }
3433 
3434 /**
3435  * sof_set_pipe_widget - Set pipe_widget for a component
3436  * @sdev: pointer to struct snd_sof_dev
3437  * @pipe_widget: pointer to struct snd_sof_widget of type snd_soc_dapm_scheduler
3438  * @swidget: pointer to struct snd_sof_widget that has the same pipeline ID as @pipe_widget
3439  *
3440  * Return: 0 if successful, -EINVAL on error.
3441  * The function checks if @swidget is associated with any volatile controls. If so, setting
3442  * the dynamic_pipeline_widget is disallowed.
3443  */
3444 static int sof_set_pipe_widget(struct snd_sof_dev *sdev, struct snd_sof_widget *pipe_widget,
3445 			       struct snd_sof_widget *swidget)
3446 {
3447 	struct snd_sof_control *scontrol;
3448 
3449 	if (pipe_widget->dynamic_pipeline_widget) {
3450 		/* dynamic widgets cannot have volatile kcontrols */
3451 		list_for_each_entry(scontrol, &sdev->kcontrol_list, list)
3452 			if (scontrol->comp_id == swidget->comp_id &&
3453 			    (scontrol->access & SNDRV_CTL_ELEM_ACCESS_VOLATILE)) {
3454 				dev_err(sdev->dev,
3455 					"error: volatile control found for dynamic widget %s\n",
3456 					swidget->widget->name);
3457 				return -EINVAL;
3458 			}
3459 	}
3460 
3461 	/* set the pipe_widget and apply the dynamic_pipeline_widget_flag */
3462 	swidget->pipe_widget = pipe_widget;
3463 	swidget->dynamic_pipeline_widget = pipe_widget->dynamic_pipeline_widget;
3464 
3465 	return 0;
3466 }
3467 
3468 /* completion - called at completion of firmware loading */
3469 static int sof_complete(struct snd_soc_component *scomp)
3470 {
3471 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3472 	struct snd_sof_widget *swidget, *comp_swidget;
3473 	const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
3474 	const struct sof_ipc_tplg_widget_ops *widget_ops = ipc_tplg_ops->widget;
3475 	int ret;
3476 
3477 	/*
3478 	 * now update all widget IPC structures. If any of the ipc_setup callbacks fail, the
3479 	 * topology will be removed and all widgets will be unloaded resulting in freeing all
3480 	 * associated memories.
3481 	 */
3482 	list_for_each_entry(swidget, &sdev->widget_list, list) {
3483 		if (widget_ops[swidget->id].ipc_setup) {
3484 			ret = widget_ops[swidget->id].ipc_setup(swidget);
3485 			if (ret < 0) {
3486 				dev_err(sdev->dev, "failed updating IPC struct for %s\n",
3487 					swidget->widget->name);
3488 				return ret;
3489 			}
3490 		}
3491 	}
3492 
3493 	/* set the pipe_widget and apply the dynamic_pipeline_widget_flag */
3494 	list_for_each_entry(swidget, &sdev->widget_list, list) {
3495 		switch (swidget->id) {
3496 		case snd_soc_dapm_scheduler:
3497 			/*
3498 			 * Apply the dynamic_pipeline_widget flag and set the pipe_widget field
3499 			 * for all widgets that have the same pipeline ID as the scheduler widget
3500 			 */
3501 			list_for_each_entry(comp_swidget, &sdev->widget_list, list)
3502 				if (comp_swidget->pipeline_id == swidget->pipeline_id) {
3503 					ret = sof_set_pipe_widget(sdev, swidget, comp_swidget);
3504 					if (ret < 0)
3505 						return ret;
3506 				}
3507 			break;
3508 		default:
3509 			break;
3510 		}
3511 	}
3512 
3513 	/* verify topology components loading including dynamic pipelines */
3514 	if (sof_debug_check_flag(SOF_DBG_VERIFY_TPLG)) {
3515 		ret = sof_set_up_pipelines(sdev, true);
3516 		if (ret < 0) {
3517 			dev_err(sdev->dev, "error: topology verification failed %d\n", ret);
3518 			return ret;
3519 		}
3520 
3521 		ret = sof_tear_down_pipelines(sdev, true);
3522 		if (ret < 0) {
3523 			dev_err(sdev->dev, "error: topology tear down pipelines failed %d\n", ret);
3524 			return ret;
3525 		}
3526 	}
3527 
3528 	/* set up static pipelines */
3529 	return sof_set_up_pipelines(sdev, false);
3530 }
3531 
3532 /* manifest - optional to inform component of manifest */
3533 static int sof_manifest(struct snd_soc_component *scomp, int index,
3534 			struct snd_soc_tplg_manifest *man)
3535 {
3536 	u32 size;
3537 	u32 abi_version;
3538 
3539 	size = le32_to_cpu(man->priv.size);
3540 
3541 	/* backward compatible with tplg without ABI info */
3542 	if (!size) {
3543 		dev_dbg(scomp->dev, "No topology ABI info\n");
3544 		return 0;
3545 	}
3546 
3547 	if (size != SOF_TPLG_ABI_SIZE) {
3548 		dev_err(scomp->dev, "error: invalid topology ABI size\n");
3549 		return -EINVAL;
3550 	}
3551 
3552 	dev_info(scomp->dev,
3553 		 "Topology: ABI %d:%d:%d Kernel ABI %d:%d:%d\n",
3554 		 man->priv.data[0], man->priv.data[1],
3555 		 man->priv.data[2], SOF_ABI_MAJOR, SOF_ABI_MINOR,
3556 		 SOF_ABI_PATCH);
3557 
3558 	abi_version = SOF_ABI_VER(man->priv.data[0],
3559 				  man->priv.data[1],
3560 				  man->priv.data[2]);
3561 
3562 	if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, abi_version)) {
3563 		dev_err(scomp->dev, "error: incompatible topology ABI version\n");
3564 		return -EINVAL;
3565 	}
3566 
3567 	if (SOF_ABI_VERSION_MINOR(abi_version) > SOF_ABI_MINOR) {
3568 		if (!IS_ENABLED(CONFIG_SND_SOC_SOF_STRICT_ABI_CHECKS)) {
3569 			dev_warn(scomp->dev, "warn: topology ABI is more recent than kernel\n");
3570 		} else {
3571 			dev_err(scomp->dev, "error: topology ABI is more recent than kernel\n");
3572 			return -EINVAL;
3573 		}
3574 	}
3575 
3576 	return 0;
3577 }
3578 
3579 /* vendor specific kcontrol handlers available for binding */
3580 static const struct snd_soc_tplg_kcontrol_ops sof_io_ops[] = {
3581 	{SOF_TPLG_KCTL_VOL_ID, snd_sof_volume_get, snd_sof_volume_put},
3582 	{SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_get, snd_sof_bytes_put},
3583 	{SOF_TPLG_KCTL_ENUM_ID, snd_sof_enum_get, snd_sof_enum_put},
3584 	{SOF_TPLG_KCTL_SWITCH_ID, snd_sof_switch_get, snd_sof_switch_put},
3585 };
3586 
3587 /* vendor specific bytes ext handlers available for binding */
3588 static const struct snd_soc_tplg_bytes_ext_ops sof_bytes_ext_ops[] = {
3589 	{SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_ext_get, snd_sof_bytes_ext_put},
3590 	{SOF_TPLG_KCTL_BYTES_VOLATILE_RO, snd_sof_bytes_ext_volatile_get},
3591 };
3592 
3593 static struct snd_soc_tplg_ops sof_tplg_ops = {
3594 	/* external kcontrol init - used for any driver specific init */
3595 	.control_load	= sof_control_load,
3596 	.control_unload	= sof_control_unload,
3597 
3598 	/* external kcontrol init - used for any driver specific init */
3599 	.dapm_route_load	= sof_route_load,
3600 	.dapm_route_unload	= sof_route_unload,
3601 
3602 	/* external widget init - used for any driver specific init */
3603 	/* .widget_load is not currently used */
3604 	.widget_ready	= sof_widget_ready,
3605 	.widget_unload	= sof_widget_unload,
3606 
3607 	/* FE DAI - used for any driver specific init */
3608 	.dai_load	= sof_dai_load,
3609 	.dai_unload	= sof_dai_unload,
3610 
3611 	/* DAI link - used for any driver specific init */
3612 	.link_load	= sof_link_load,
3613 
3614 	/* completion - called at completion of firmware loading */
3615 	.complete	= sof_complete,
3616 
3617 	/* manifest - optional to inform component of manifest */
3618 	.manifest	= sof_manifest,
3619 
3620 	/* vendor specific kcontrol handlers available for binding */
3621 	.io_ops		= sof_io_ops,
3622 	.io_ops_count	= ARRAY_SIZE(sof_io_ops),
3623 
3624 	/* vendor specific bytes ext handlers available for binding */
3625 	.bytes_ext_ops	= sof_bytes_ext_ops,
3626 	.bytes_ext_ops_count	= ARRAY_SIZE(sof_bytes_ext_ops),
3627 };
3628 
3629 int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file)
3630 {
3631 	const struct firmware *fw;
3632 	int ret;
3633 
3634 	dev_dbg(scomp->dev, "loading topology:%s\n", file);
3635 
3636 	ret = request_firmware(&fw, file, scomp->dev);
3637 	if (ret < 0) {
3638 		dev_err(scomp->dev, "error: tplg request firmware %s failed err: %d\n",
3639 			file, ret);
3640 		dev_err(scomp->dev,
3641 			"you may need to download the firmware from https://github.com/thesofproject/sof-bin/\n");
3642 		return ret;
3643 	}
3644 
3645 	ret = snd_soc_tplg_component_load(scomp, &sof_tplg_ops, fw);
3646 	if (ret < 0) {
3647 		dev_err(scomp->dev, "error: tplg component load failed %d\n",
3648 			ret);
3649 		ret = -EINVAL;
3650 	}
3651 
3652 	release_firmware(fw);
3653 	return ret;
3654 }
3655 EXPORT_SYMBOL(snd_sof_load_topology);
3656