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