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