xref: /linux/sound/soc/sof/topology.c (revision e03ad65cea610b24c6991aebf432d5c6824cd002)
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
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 <uapi/sound/sof/tokens.h>
18 #include "sof-priv.h"
19 #include "sof-audio.h"
20 #include "ops.h"
21 
22 #define COMP_ID_UNASSIGNED		0xffffffff
23 /*
24  * Constants used in the computation of linear volume gain
25  * from dB gain 20th root of 10 in Q1.16 fixed-point notation
26  */
27 #define VOL_TWENTIETH_ROOT_OF_TEN	73533
28 /* 40th root of 10 in Q1.16 fixed-point notation*/
29 #define VOL_FORTIETH_ROOT_OF_TEN	69419
30 
31 /* 0.5 dB step value in topology TLV */
32 #define VOL_HALF_DB_STEP	50
33 
34 /* TLV data items */
35 #define TLV_MIN		0
36 #define TLV_STEP	1
37 #define TLV_MUTE	2
38 
39 /**
40  * sof_update_ipc_object - Parse multiple sets of tokens within the token array associated with the
41  *			    token ID.
42  * @scomp: pointer to SOC component
43  * @object: target IPC struct to save the parsed values
44  * @token_id: token ID for the token array to be searched
45  * @tuples: pointer to the tuples array
46  * @num_tuples: number of tuples in the tuples array
47  * @object_size: size of the object
48  * @token_instance_num: number of times the same @token_id needs to be parsed i.e. the function
49  *			looks for @token_instance_num of each token in the token array associated
50  *			with the @token_id
51  */
52 int sof_update_ipc_object(struct snd_soc_component *scomp, void *object, enum sof_tokens token_id,
53 			  struct snd_sof_tuple *tuples, int num_tuples,
54 			  size_t object_size, int token_instance_num)
55 {
56 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
57 	const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
58 	const struct sof_token_info *token_list;
59 	const struct sof_topology_token *tokens;
60 	int i, j;
61 
62 	token_list = tplg_ops ? tplg_ops->token_list : NULL;
63 	/* nothing to do if token_list is NULL */
64 	if (!token_list)
65 		return 0;
66 
67 	if (token_list[token_id].count < 0) {
68 		dev_err(scomp->dev, "Invalid token count for token ID: %d\n", token_id);
69 		return -EINVAL;
70 	}
71 
72 	/* No tokens to match */
73 	if (!token_list[token_id].count)
74 		return 0;
75 
76 	tokens = token_list[token_id].tokens;
77 	if (!tokens) {
78 		dev_err(scomp->dev, "Invalid tokens for token id: %d\n", token_id);
79 		return -EINVAL;
80 	}
81 
82 	for (i = 0; i < token_list[token_id].count; i++) {
83 		int offset = 0;
84 		int num_tokens_matched = 0;
85 
86 		for (j = 0; j < num_tuples; j++) {
87 			if (tokens[i].token == tuples[j].token) {
88 				switch (tokens[i].type) {
89 				case SND_SOC_TPLG_TUPLE_TYPE_WORD:
90 				{
91 					u32 *val = (u32 *)((u8 *)object + tokens[i].offset +
92 							   offset);
93 
94 					*val = tuples[j].value.v;
95 					break;
96 				}
97 				case SND_SOC_TPLG_TUPLE_TYPE_SHORT:
98 				case SND_SOC_TPLG_TUPLE_TYPE_BOOL:
99 				{
100 					u16 *val = (u16 *)((u8 *)object + tokens[i].offset +
101 							    offset);
102 
103 					*val = (u16)tuples[j].value.v;
104 					break;
105 				}
106 				case SND_SOC_TPLG_TUPLE_TYPE_STRING:
107 				{
108 					if (!tokens[i].get_token) {
109 						dev_err(scomp->dev,
110 							"get_token not defined for token %d in %s\n",
111 							tokens[i].token, token_list[token_id].name);
112 						return -EINVAL;
113 					}
114 
115 					tokens[i].get_token((void *)tuples[j].value.s, object,
116 							    tokens[i].offset + offset);
117 					break;
118 				}
119 				default:
120 					break;
121 				}
122 
123 				num_tokens_matched++;
124 
125 				/* found all required sets of current token. Move to the next one */
126 				if (!(num_tokens_matched % token_instance_num))
127 					break;
128 
129 				/* move to the next object */
130 				offset += object_size;
131 			}
132 		}
133 	}
134 
135 	return 0;
136 }
137 
138 static inline int get_tlv_data(const int *p, int tlv[SOF_TLV_ITEMS])
139 {
140 	/* we only support dB scale TLV type at the moment */
141 	if ((int)p[SNDRV_CTL_TLVO_TYPE] != SNDRV_CTL_TLVT_DB_SCALE)
142 		return -EINVAL;
143 
144 	/* min value in topology tlv data is multiplied by 100 */
145 	tlv[TLV_MIN] = (int)p[SNDRV_CTL_TLVO_DB_SCALE_MIN] / 100;
146 
147 	/* volume steps */
148 	tlv[TLV_STEP] = (int)(p[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] &
149 				TLV_DB_SCALE_MASK);
150 
151 	/* mute ON/OFF */
152 	if ((p[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] &
153 		TLV_DB_SCALE_MUTE) == 0)
154 		tlv[TLV_MUTE] = 0;
155 	else
156 		tlv[TLV_MUTE] = 1;
157 
158 	return 0;
159 }
160 
161 /*
162  * Function to truncate an unsigned 64-bit number
163  * by x bits and return 32-bit unsigned number. This
164  * function also takes care of rounding while truncating
165  */
166 static inline u32 vol_shift_64(u64 i, u32 x)
167 {
168 	/* do not truncate more than 32 bits */
169 	if (x > 32)
170 		x = 32;
171 
172 	if (x == 0)
173 		return (u32)i;
174 
175 	return (u32)(((i >> (x - 1)) + 1) >> 1);
176 }
177 
178 /*
179  * Function to compute a ^ exp where,
180  * a is a fractional number represented by a fixed-point
181  * integer with a fractional world length of "fwl"
182  * exp is an integer
183  * fwl is the fractional word length
184  * Return value is a fractional number represented by a
185  * fixed-point integer with a fractional word length of "fwl"
186  */
187 static u32 vol_pow32(u32 a, int exp, u32 fwl)
188 {
189 	int i, iter;
190 	u32 power = 1 << fwl;
191 	u64 numerator;
192 
193 	/* if exponent is 0, return 1 */
194 	if (exp == 0)
195 		return power;
196 
197 	/* determine the number of iterations based on the exponent */
198 	if (exp < 0)
199 		iter = exp * -1;
200 	else
201 		iter = exp;
202 
203 	/* mutiply a "iter" times to compute power */
204 	for (i = 0; i < iter; i++) {
205 		/*
206 		 * Product of 2 Qx.fwl fixed-point numbers yields a Q2*x.2*fwl
207 		 * Truncate product back to fwl fractional bits with rounding
208 		 */
209 		power = vol_shift_64((u64)power * a, fwl);
210 	}
211 
212 	if (exp > 0) {
213 		/* if exp is positive, return the result */
214 		return power;
215 	}
216 
217 	/* if exp is negative, return the multiplicative inverse */
218 	numerator = (u64)1 << (fwl << 1);
219 	do_div(numerator, power);
220 
221 	return (u32)numerator;
222 }
223 
224 /*
225  * Function to calculate volume gain from TLV data.
226  * This function can only handle gain steps that are multiples of 0.5 dB
227  */
228 u32 vol_compute_gain(u32 value, int *tlv)
229 {
230 	int dB_gain;
231 	u32 linear_gain;
232 	int f_step;
233 
234 	/* mute volume */
235 	if (value == 0 && tlv[TLV_MUTE])
236 		return 0;
237 
238 	/*
239 	 * compute dB gain from tlv. tlv_step
240 	 * in topology is multiplied by 100
241 	 */
242 	dB_gain = tlv[TLV_MIN] + (value * tlv[TLV_STEP]) / 100;
243 
244 	/*
245 	 * compute linear gain represented by fixed-point
246 	 * int with VOLUME_FWL fractional bits
247 	 */
248 	linear_gain = vol_pow32(VOL_TWENTIETH_ROOT_OF_TEN, dB_gain, VOLUME_FWL);
249 
250 	/* extract the fractional part of volume step */
251 	f_step = tlv[TLV_STEP] - (tlv[TLV_STEP] / 100);
252 
253 	/* if volume step is an odd multiple of 0.5 dB */
254 	if (f_step == VOL_HALF_DB_STEP && (value & 1))
255 		linear_gain = vol_shift_64((u64)linear_gain *
256 						  VOL_FORTIETH_ROOT_OF_TEN,
257 						  VOLUME_FWL);
258 
259 	return linear_gain;
260 }
261 
262 /*
263  * Set up volume table for kcontrols from tlv data
264  * "size" specifies the number of entries in the table
265  */
266 static int set_up_volume_table(struct snd_sof_control *scontrol,
267 			       int tlv[SOF_TLV_ITEMS], int size)
268 {
269 	struct snd_soc_component *scomp = scontrol->scomp;
270 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
271 	const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
272 
273 	if (tplg_ops && tplg_ops->control && tplg_ops->control->set_up_volume_table)
274 		return tplg_ops->control->set_up_volume_table(scontrol, tlv, size);
275 
276 	dev_err(scomp->dev, "Mandatory op %s not set\n", __func__);
277 	return -EINVAL;
278 }
279 
280 struct sof_dai_types {
281 	const char *name;
282 	enum sof_ipc_dai_type type;
283 };
284 
285 static const struct sof_dai_types sof_dais[] = {
286 	{"SSP", SOF_DAI_INTEL_SSP},
287 	{"HDA", SOF_DAI_INTEL_HDA},
288 	{"DMIC", SOF_DAI_INTEL_DMIC},
289 	{"ALH", SOF_DAI_INTEL_ALH},
290 	{"SAI", SOF_DAI_IMX_SAI},
291 	{"ESAI", SOF_DAI_IMX_ESAI},
292 	{"ACPBT", SOF_DAI_AMD_BT},
293 	{"ACPSP", SOF_DAI_AMD_SP},
294 	{"ACPDMIC", SOF_DAI_AMD_DMIC},
295 	{"ACPHS", SOF_DAI_AMD_HS},
296 	{"AFE", SOF_DAI_MEDIATEK_AFE},
297 	{"ACPSP_VIRTUAL", SOF_DAI_AMD_SP_VIRTUAL},
298 	{"ACPHS_VIRTUAL", SOF_DAI_AMD_HS_VIRTUAL},
299 	{"MICFIL", SOF_DAI_IMX_MICFIL},
300 	{"ACP_SDW", SOF_DAI_AMD_SDW},
301 
302 };
303 
304 static enum sof_ipc_dai_type find_dai(const char *name)
305 {
306 	int i;
307 
308 	for (i = 0; i < ARRAY_SIZE(sof_dais); i++) {
309 		if (strcmp(name, sof_dais[i].name) == 0)
310 			return sof_dais[i].type;
311 	}
312 
313 	return SOF_DAI_INTEL_NONE;
314 }
315 
316 /*
317  * Supported Frame format types and lookup, add new ones to end of list.
318  */
319 
320 struct sof_frame_types {
321 	const char *name;
322 	enum sof_ipc_frame frame;
323 };
324 
325 static const struct sof_frame_types sof_frames[] = {
326 	{"s16le", SOF_IPC_FRAME_S16_LE},
327 	{"s24le", SOF_IPC_FRAME_S24_4LE},
328 	{"s32le", SOF_IPC_FRAME_S32_LE},
329 	{"float", SOF_IPC_FRAME_FLOAT},
330 };
331 
332 static enum sof_ipc_frame find_format(const char *name)
333 {
334 	int i;
335 
336 	for (i = 0; i < ARRAY_SIZE(sof_frames); i++) {
337 		if (strcmp(name, sof_frames[i].name) == 0)
338 			return sof_frames[i].frame;
339 	}
340 
341 	/* use s32le if nothing is specified */
342 	return SOF_IPC_FRAME_S32_LE;
343 }
344 
345 int get_token_u32(void *elem, void *object, u32 offset)
346 {
347 	struct snd_soc_tplg_vendor_value_elem *velem = elem;
348 	u32 *val = (u32 *)((u8 *)object + offset);
349 
350 	*val = le32_to_cpu(velem->value);
351 	return 0;
352 }
353 
354 int get_token_u16(void *elem, void *object, u32 offset)
355 {
356 	struct snd_soc_tplg_vendor_value_elem *velem = elem;
357 	u16 *val = (u16 *)((u8 *)object + offset);
358 
359 	*val = (u16)le32_to_cpu(velem->value);
360 	return 0;
361 }
362 
363 int get_token_uuid(void *elem, void *object, u32 offset)
364 {
365 	struct snd_soc_tplg_vendor_uuid_elem *velem = elem;
366 	u8 *dst = (u8 *)object + offset;
367 
368 	memcpy(dst, velem->uuid, UUID_SIZE);
369 
370 	return 0;
371 }
372 
373 /*
374  * The string gets from topology will be stored in heap, the owner only
375  * holds a char* member point to the heap.
376  */
377 int get_token_string(void *elem, void *object, u32 offset)
378 {
379 	/* "dst" here points to the char* member of the owner */
380 	char **dst = (char **)((u8 *)object + offset);
381 
382 	*dst = kstrdup(elem, GFP_KERNEL);
383 	if (!*dst)
384 		return -ENOMEM;
385 	return 0;
386 };
387 
388 int get_token_comp_format(void *elem, void *object, u32 offset)
389 {
390 	u32 *val = (u32 *)((u8 *)object + offset);
391 
392 	*val = find_format((const char *)elem);
393 	return 0;
394 }
395 
396 int get_token_dai_type(void *elem, void *object, u32 offset)
397 {
398 	u32 *val = (u32 *)((u8 *)object + offset);
399 
400 	*val = find_dai((const char *)elem);
401 	return 0;
402 }
403 
404 /* PCM */
405 static const struct sof_topology_token stream_tokens[] = {
406 	{SOF_TKN_STREAM_PLAYBACK_COMPATIBLE_D0I3, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
407 		offsetof(struct snd_sof_pcm, stream[0].d0i3_compatible)},
408 	{SOF_TKN_STREAM_CAPTURE_COMPATIBLE_D0I3, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
409 		offsetof(struct snd_sof_pcm, stream[1].d0i3_compatible)},
410 };
411 
412 /* Leds */
413 static const struct sof_topology_token led_tokens[] = {
414 	{SOF_TKN_MUTE_LED_USE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
415 		offsetof(struct snd_sof_led_control, use_led)},
416 	{SOF_TKN_MUTE_LED_DIRECTION, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
417 		offsetof(struct snd_sof_led_control, direction)},
418 };
419 
420 static const struct sof_topology_token comp_pin_tokens[] = {
421 	{SOF_TKN_COMP_NUM_INPUT_PINS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
422 		offsetof(struct snd_sof_widget, num_input_pins)},
423 	{SOF_TKN_COMP_NUM_OUTPUT_PINS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
424 		offsetof(struct snd_sof_widget, num_output_pins)},
425 };
426 
427 static const struct sof_topology_token comp_input_pin_binding_tokens[] = {
428 	{SOF_TKN_COMP_INPUT_PIN_BINDING_WNAME, SND_SOC_TPLG_TUPLE_TYPE_STRING,
429 		get_token_string, 0},
430 };
431 
432 static const struct sof_topology_token comp_output_pin_binding_tokens[] = {
433 	{SOF_TKN_COMP_OUTPUT_PIN_BINDING_WNAME, SND_SOC_TPLG_TUPLE_TYPE_STRING,
434 		get_token_string, 0},
435 };
436 
437 /**
438  * sof_parse_uuid_tokens - Parse multiple sets of UUID tokens
439  * @scomp: pointer to soc component
440  * @object: target ipc struct for parsed values
441  * @offset: offset within the object pointer
442  * @tokens: array of struct sof_topology_token containing the tokens to be matched
443  * @num_tokens: number of tokens in tokens array
444  * @array: source pointer to consecutive vendor arrays in topology
445  *
446  * This function parses multiple sets of string type tokens in vendor arrays
447  */
448 static int sof_parse_uuid_tokens(struct snd_soc_component *scomp,
449 				  void *object, size_t offset,
450 				  const struct sof_topology_token *tokens, int num_tokens,
451 				  struct snd_soc_tplg_vendor_array *array)
452 {
453 	struct snd_soc_tplg_vendor_uuid_elem *elem;
454 	int found = 0;
455 	int i, j;
456 
457 	/* parse element by element */
458 	for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
459 		elem = &array->uuid[i];
460 
461 		/* search for token */
462 		for (j = 0; j < num_tokens; j++) {
463 			/* match token type */
464 			if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_UUID)
465 				continue;
466 
467 			/* match token id */
468 			if (tokens[j].token != le32_to_cpu(elem->token))
469 				continue;
470 
471 			/* matched - now load token */
472 			tokens[j].get_token(elem, object,
473 					    offset + tokens[j].offset);
474 
475 			found++;
476 		}
477 	}
478 
479 	return found;
480 }
481 
482 /**
483  * sof_copy_tuples - Parse tokens and copy them to the @tuples array
484  * @sdev: pointer to struct snd_sof_dev
485  * @array: source pointer to consecutive vendor arrays in topology
486  * @array_size: size of @array
487  * @token_id: Token ID associated with a token array
488  * @token_instance_num: number of times the same @token_id needs to be parsed i.e. the function
489  *			looks for @token_instance_num of each token in the token array associated
490  *			with the @token_id
491  * @tuples: tuples array to copy the matched tuples to
492  * @tuples_size: size of @tuples
493  * @num_copied_tuples: pointer to the number of copied tuples in the tuples array
494  *
495  */
496 static int sof_copy_tuples(struct snd_sof_dev *sdev, struct snd_soc_tplg_vendor_array *array,
497 			   int array_size, u32 token_id, int token_instance_num,
498 			   struct snd_sof_tuple *tuples, int tuples_size, int *num_copied_tuples)
499 {
500 	const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
501 	const struct sof_token_info *token_list;
502 	const struct sof_topology_token *tokens;
503 	int found = 0;
504 	int num_tokens, asize;
505 	int i, j;
506 
507 	token_list = tplg_ops ? tplg_ops->token_list : NULL;
508 	/* nothing to do if token_list is NULL */
509 	if (!token_list)
510 		return 0;
511 
512 	if (!tuples || !num_copied_tuples) {
513 		dev_err(sdev->dev, "Invalid tuples array\n");
514 		return -EINVAL;
515 	}
516 
517 	tokens = token_list[token_id].tokens;
518 	num_tokens = token_list[token_id].count;
519 
520 	if (!tokens) {
521 		dev_err(sdev->dev, "No token array defined for token ID: %d\n", token_id);
522 		return -EINVAL;
523 	}
524 
525 	/* check if there's space in the tuples array for new tokens */
526 	if (*num_copied_tuples >= tuples_size) {
527 		dev_err(sdev->dev, "No space in tuples array for new tokens from %s",
528 			token_list[token_id].name);
529 		return -EINVAL;
530 	}
531 
532 	while (array_size > 0 && found < num_tokens * token_instance_num) {
533 		asize = le32_to_cpu(array->size);
534 
535 		/* validate asize */
536 		if (asize < 0) {
537 			dev_err(sdev->dev, "Invalid array size 0x%x\n", asize);
538 			return -EINVAL;
539 		}
540 
541 		/* make sure there is enough data before parsing */
542 		array_size -= asize;
543 		if (array_size < 0) {
544 			dev_err(sdev->dev, "Invalid array size 0x%x\n", asize);
545 			return -EINVAL;
546 		}
547 
548 		/* parse element by element */
549 		for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
550 			/* search for token */
551 			for (j = 0; j < num_tokens; j++) {
552 				/* match token type */
553 				if (!(tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_WORD ||
554 				      tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_SHORT ||
555 				      tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BYTE ||
556 				      tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BOOL ||
557 				      tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_STRING))
558 					continue;
559 
560 				if (tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_STRING) {
561 					struct snd_soc_tplg_vendor_string_elem *elem;
562 
563 					elem = &array->string[i];
564 
565 					/* match token id */
566 					if (tokens[j].token != le32_to_cpu(elem->token))
567 						continue;
568 
569 					tuples[*num_copied_tuples].token = tokens[j].token;
570 					tuples[*num_copied_tuples].value.s = elem->string;
571 				} else {
572 					struct snd_soc_tplg_vendor_value_elem *elem;
573 
574 					elem = &array->value[i];
575 
576 					/* match token id */
577 					if (tokens[j].token != le32_to_cpu(elem->token))
578 						continue;
579 
580 					tuples[*num_copied_tuples].token = tokens[j].token;
581 					tuples[*num_copied_tuples].value.v =
582 						le32_to_cpu(elem->value);
583 				}
584 				found++;
585 				(*num_copied_tuples)++;
586 
587 				/* stop if there's no space for any more new tuples */
588 				if (*num_copied_tuples == tuples_size)
589 					return 0;
590 			}
591 
592 			/* stop when we've found the required token instances */
593 			if (found == num_tokens * token_instance_num)
594 				return 0;
595 		}
596 
597 		/* next array */
598 		array = (struct snd_soc_tplg_vendor_array *)((u8 *)array + asize);
599 	}
600 
601 	return 0;
602 }
603 
604 /**
605  * sof_parse_string_tokens - Parse multiple sets of tokens
606  * @scomp: pointer to soc component
607  * @object: target ipc struct for parsed values
608  * @offset: offset within the object pointer
609  * @tokens: array of struct sof_topology_token containing the tokens to be matched
610  * @num_tokens: number of tokens in tokens array
611  * @array: source pointer to consecutive vendor arrays in topology
612  *
613  * This function parses multiple sets of string type tokens in vendor arrays
614  */
615 static int sof_parse_string_tokens(struct snd_soc_component *scomp,
616 				   void *object, int offset,
617 				   const struct sof_topology_token *tokens, int num_tokens,
618 				   struct snd_soc_tplg_vendor_array *array)
619 {
620 	struct snd_soc_tplg_vendor_string_elem *elem;
621 	int found = 0;
622 	int i, j, ret;
623 
624 	/* parse element by element */
625 	for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
626 		elem = &array->string[i];
627 
628 		/* search for token */
629 		for (j = 0; j < num_tokens; j++) {
630 			/* match token type */
631 			if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_STRING)
632 				continue;
633 
634 			/* match token id */
635 			if (tokens[j].token != le32_to_cpu(elem->token))
636 				continue;
637 
638 			/* matched - now load token */
639 			ret = tokens[j].get_token(elem->string, object, offset + tokens[j].offset);
640 			if (ret < 0)
641 				return ret;
642 
643 			found++;
644 		}
645 	}
646 
647 	return found;
648 }
649 
650 /**
651  * sof_parse_word_tokens - Parse multiple sets of tokens
652  * @scomp: pointer to soc component
653  * @object: target ipc struct for parsed values
654  * @offset: offset within the object pointer
655  * @tokens: array of struct sof_topology_token containing the tokens to be matched
656  * @num_tokens: number of tokens in tokens array
657  * @array: source pointer to consecutive vendor arrays in topology
658  *
659  * This function parses multiple sets of word type tokens in vendor arrays
660  */
661 static int sof_parse_word_tokens(struct snd_soc_component *scomp,
662 				  void *object, int offset,
663 				  const struct sof_topology_token *tokens, int num_tokens,
664 				  struct snd_soc_tplg_vendor_array *array)
665 {
666 	struct snd_soc_tplg_vendor_value_elem *elem;
667 	int found = 0;
668 	int i, j;
669 
670 	/* parse element by element */
671 	for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
672 		elem = &array->value[i];
673 
674 		/* search for token */
675 		for (j = 0; j < num_tokens; j++) {
676 			/* match token type */
677 			if (!(tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_WORD ||
678 			      tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_SHORT ||
679 			      tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BYTE ||
680 			      tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BOOL))
681 				continue;
682 
683 			/* match token id */
684 			if (tokens[j].token != le32_to_cpu(elem->token))
685 				continue;
686 
687 			/* load token */
688 			tokens[j].get_token(elem, object, offset + tokens[j].offset);
689 
690 			found++;
691 		}
692 	}
693 
694 	return found;
695 }
696 
697 /**
698  * sof_parse_token_sets - Parse multiple sets of tokens
699  * @scomp: pointer to soc component
700  * @object: target ipc struct for parsed values
701  * @tokens: token definition array describing what tokens to parse
702  * @count: number of tokens in definition array
703  * @array: source pointer to consecutive vendor arrays in topology
704  * @array_size: total size of @array
705  * @token_instance_num: number of times the same tokens needs to be parsed i.e. the function
706  *			looks for @token_instance_num of each token in the @tokens
707  * @object_size: offset to next target ipc struct with multiple sets
708  *
709  * This function parses multiple sets of tokens in vendor arrays into
710  * consecutive ipc structs.
711  */
712 static int sof_parse_token_sets(struct snd_soc_component *scomp,
713 				void *object, const struct sof_topology_token *tokens,
714 				int count, struct snd_soc_tplg_vendor_array *array,
715 				int array_size, int token_instance_num, size_t object_size)
716 {
717 	size_t offset = 0;
718 	int found = 0;
719 	int total = 0;
720 	int asize;
721 	int ret;
722 
723 	while (array_size > 0 && total < count * token_instance_num) {
724 		asize = le32_to_cpu(array->size);
725 
726 		/* validate asize */
727 		if (asize < 0) { /* FIXME: A zero-size array makes no sense */
728 			dev_err(scomp->dev, "error: invalid array size 0x%x\n",
729 				asize);
730 			return -EINVAL;
731 		}
732 
733 		/* make sure there is enough data before parsing */
734 		array_size -= asize;
735 		if (array_size < 0) {
736 			dev_err(scomp->dev, "error: invalid array size 0x%x\n",
737 				asize);
738 			return -EINVAL;
739 		}
740 
741 		/* call correct parser depending on type */
742 		switch (le32_to_cpu(array->type)) {
743 		case SND_SOC_TPLG_TUPLE_TYPE_UUID:
744 			found += sof_parse_uuid_tokens(scomp, object, offset, tokens, count,
745 						       array);
746 			break;
747 		case SND_SOC_TPLG_TUPLE_TYPE_STRING:
748 
749 			ret = sof_parse_string_tokens(scomp, object, offset, tokens, count,
750 						      array);
751 			if (ret < 0) {
752 				dev_err(scomp->dev, "error: no memory to copy string token\n");
753 				return ret;
754 			}
755 
756 			found += ret;
757 			break;
758 		case SND_SOC_TPLG_TUPLE_TYPE_BOOL:
759 		case SND_SOC_TPLG_TUPLE_TYPE_BYTE:
760 		case SND_SOC_TPLG_TUPLE_TYPE_WORD:
761 		case SND_SOC_TPLG_TUPLE_TYPE_SHORT:
762 			found += sof_parse_word_tokens(scomp, object, offset, tokens, count,
763 						       array);
764 			break;
765 		default:
766 			dev_err(scomp->dev, "error: unknown token type %d\n",
767 				array->type);
768 			return -EINVAL;
769 		}
770 
771 		/* next array */
772 		array = (struct snd_soc_tplg_vendor_array *)((u8 *)array
773 			+ asize);
774 
775 		/* move to next target struct */
776 		if (found >= count) {
777 			offset += object_size;
778 			total += found;
779 			found = 0;
780 		}
781 	}
782 
783 	return 0;
784 }
785 
786 /**
787  * sof_parse_tokens - Parse one set of tokens
788  * @scomp: pointer to soc component
789  * @object: target ipc struct for parsed values
790  * @tokens: token definition array describing what tokens to parse
791  * @num_tokens: number of tokens in definition array
792  * @array: source pointer to consecutive vendor arrays in topology
793  * @array_size: total size of @array
794  *
795  * This function parses a single set of tokens in vendor arrays into
796  * consecutive ipc structs.
797  */
798 static int sof_parse_tokens(struct snd_soc_component *scomp,  void *object,
799 			    const struct sof_topology_token *tokens, int num_tokens,
800 			    struct snd_soc_tplg_vendor_array *array,
801 			    int array_size)
802 
803 {
804 	/*
805 	 * sof_parse_tokens is used when topology contains only a single set of
806 	 * identical tuples arrays. So additional parameters to
807 	 * sof_parse_token_sets are sets = 1 (only 1 set) and
808 	 * object_size = 0 (irrelevant).
809 	 */
810 	return sof_parse_token_sets(scomp, object, tokens, num_tokens, array,
811 				    array_size, 1, 0);
812 }
813 
814 /*
815  * Standard Kcontrols.
816  */
817 
818 static int sof_control_load_volume(struct snd_soc_component *scomp,
819 				   struct snd_sof_control *scontrol,
820 				   struct snd_kcontrol_new *kc,
821 				   struct snd_soc_tplg_ctl_hdr *hdr)
822 {
823 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
824 	struct snd_soc_tplg_mixer_control *mc =
825 		container_of(hdr, struct snd_soc_tplg_mixer_control, hdr);
826 	int tlv[SOF_TLV_ITEMS];
827 	unsigned int mask;
828 	int ret;
829 
830 	/* validate topology data */
831 	if (le32_to_cpu(mc->num_channels) > SND_SOC_TPLG_MAX_CHAN)
832 		return -EINVAL;
833 
834 	/*
835 	 * If control has more than 2 channels we need to override the info. This is because even if
836 	 * ASoC layer has defined topology's max channel count to SND_SOC_TPLG_MAX_CHAN = 8, the
837 	 * pre-defined dapm control types (and related functions) creating the actual control
838 	 * restrict the channels only to mono or stereo.
839 	 */
840 	if (le32_to_cpu(mc->num_channels) > 2)
841 		kc->info = snd_sof_volume_info;
842 
843 	scontrol->comp_id = sdev->next_comp_id;
844 	scontrol->min_volume_step = le32_to_cpu(mc->min);
845 	scontrol->max_volume_step = le32_to_cpu(mc->max);
846 	scontrol->num_channels = le32_to_cpu(mc->num_channels);
847 
848 	scontrol->max = le32_to_cpu(mc->max);
849 	if (le32_to_cpu(mc->max) == 1)
850 		goto skip;
851 
852 	/* extract tlv data */
853 	if (!kc->tlv.p || get_tlv_data(kc->tlv.p, tlv) < 0) {
854 		dev_err(scomp->dev, "error: invalid TLV data\n");
855 		return -EINVAL;
856 	}
857 
858 	/* set up volume table */
859 	ret = set_up_volume_table(scontrol, tlv, le32_to_cpu(mc->max) + 1);
860 	if (ret < 0) {
861 		dev_err(scomp->dev, "error: setting up volume table\n");
862 		return ret;
863 	}
864 
865 skip:
866 	/* set up possible led control from mixer private data */
867 	ret = sof_parse_tokens(scomp, &scontrol->led_ctl, led_tokens,
868 			       ARRAY_SIZE(led_tokens), mc->priv.array,
869 			       le32_to_cpu(mc->priv.size));
870 	if (ret != 0) {
871 		dev_err(scomp->dev, "error: parse led tokens failed %d\n",
872 			le32_to_cpu(mc->priv.size));
873 		goto err;
874 	}
875 
876 	if (scontrol->led_ctl.use_led) {
877 		mask = scontrol->led_ctl.direction ? SNDRV_CTL_ELEM_ACCESS_MIC_LED :
878 							SNDRV_CTL_ELEM_ACCESS_SPK_LED;
879 		scontrol->access &= ~SNDRV_CTL_ELEM_ACCESS_LED_MASK;
880 		scontrol->access |= mask;
881 		kc->access &= ~SNDRV_CTL_ELEM_ACCESS_LED_MASK;
882 		kc->access |= mask;
883 		sdev->led_present = true;
884 	}
885 
886 	dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d\n",
887 		scontrol->comp_id, scontrol->num_channels);
888 
889 	return 0;
890 
891 err:
892 	if (le32_to_cpu(mc->max) > 1)
893 		kfree(scontrol->volume_table);
894 
895 	return ret;
896 }
897 
898 static int sof_control_load_enum(struct snd_soc_component *scomp,
899 				 struct snd_sof_control *scontrol,
900 				 struct snd_kcontrol_new *kc,
901 				 struct snd_soc_tplg_ctl_hdr *hdr)
902 {
903 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
904 	struct snd_soc_tplg_enum_control *ec =
905 		container_of(hdr, struct snd_soc_tplg_enum_control, hdr);
906 
907 	/* validate topology data */
908 	if (le32_to_cpu(ec->num_channels) > SND_SOC_TPLG_MAX_CHAN)
909 		return -EINVAL;
910 
911 	scontrol->comp_id = sdev->next_comp_id;
912 	scontrol->num_channels = le32_to_cpu(ec->num_channels);
913 
914 	dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d comp_id %d\n",
915 		scontrol->comp_id, scontrol->num_channels, scontrol->comp_id);
916 
917 	return 0;
918 }
919 
920 static int sof_control_load_bytes(struct snd_soc_component *scomp,
921 				  struct snd_sof_control *scontrol,
922 				  struct snd_kcontrol_new *kc,
923 				  struct snd_soc_tplg_ctl_hdr *hdr)
924 {
925 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
926 	struct snd_soc_tplg_bytes_control *control =
927 		container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
928 	struct soc_bytes_ext *sbe = (struct soc_bytes_ext *)kc->private_value;
929 	size_t priv_size = le32_to_cpu(control->priv.size);
930 
931 	scontrol->max_size = sbe->max;
932 	scontrol->comp_id = sdev->next_comp_id;
933 
934 	dev_dbg(scomp->dev, "tplg: load kcontrol index %d\n", scontrol->comp_id);
935 
936 	/* copy the private data */
937 	if (priv_size > 0) {
938 		scontrol->priv = kmemdup(control->priv.data, priv_size, GFP_KERNEL);
939 		if (!scontrol->priv)
940 			return -ENOMEM;
941 
942 		scontrol->priv_size = priv_size;
943 	}
944 
945 	return 0;
946 }
947 
948 /* external kcontrol init - used for any driver specific init */
949 static int sof_control_load(struct snd_soc_component *scomp, int index,
950 			    struct snd_kcontrol_new *kc,
951 			    struct snd_soc_tplg_ctl_hdr *hdr)
952 {
953 	struct soc_mixer_control *sm;
954 	struct soc_bytes_ext *sbe;
955 	struct soc_enum *se;
956 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
957 	struct snd_soc_dobj *dobj;
958 	struct snd_sof_control *scontrol;
959 	int ret;
960 
961 	dev_dbg(scomp->dev, "tplg: load control type %d name : %s\n",
962 		hdr->type, hdr->name);
963 
964 	scontrol = kzalloc(sizeof(*scontrol), GFP_KERNEL);
965 	if (!scontrol)
966 		return -ENOMEM;
967 
968 	scontrol->name = kstrdup(hdr->name, GFP_KERNEL);
969 	if (!scontrol->name) {
970 		kfree(scontrol);
971 		return -ENOMEM;
972 	}
973 
974 	scontrol->scomp = scomp;
975 	scontrol->access = kc->access;
976 	scontrol->info_type = le32_to_cpu(hdr->ops.info);
977 	scontrol->index = kc->index;
978 
979 	switch (le32_to_cpu(hdr->ops.info)) {
980 	case SND_SOC_TPLG_CTL_VOLSW:
981 	case SND_SOC_TPLG_CTL_VOLSW_SX:
982 	case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
983 		sm = (struct soc_mixer_control *)kc->private_value;
984 		dobj = &sm->dobj;
985 		ret = sof_control_load_volume(scomp, scontrol, kc, hdr);
986 		break;
987 	case SND_SOC_TPLG_CTL_BYTES:
988 		sbe = (struct soc_bytes_ext *)kc->private_value;
989 		dobj = &sbe->dobj;
990 		ret = sof_control_load_bytes(scomp, scontrol, kc, hdr);
991 		break;
992 	case SND_SOC_TPLG_CTL_ENUM:
993 	case SND_SOC_TPLG_CTL_ENUM_VALUE:
994 		se = (struct soc_enum *)kc->private_value;
995 		dobj = &se->dobj;
996 		ret = sof_control_load_enum(scomp, scontrol, kc, hdr);
997 		break;
998 	case SND_SOC_TPLG_CTL_RANGE:
999 	case SND_SOC_TPLG_CTL_STROBE:
1000 	case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1001 	case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1002 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1003 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1004 	case SND_SOC_TPLG_DAPM_CTL_PIN:
1005 	default:
1006 		dev_warn(scomp->dev, "control type not supported %d:%d:%d\n",
1007 			 hdr->ops.get, hdr->ops.put, hdr->ops.info);
1008 		kfree(scontrol->name);
1009 		kfree(scontrol);
1010 		return 0;
1011 	}
1012 
1013 	if (ret < 0) {
1014 		kfree(scontrol->name);
1015 		kfree(scontrol);
1016 		return ret;
1017 	}
1018 
1019 	scontrol->led_ctl.led_value = -1;
1020 
1021 	dobj->private = scontrol;
1022 	list_add(&scontrol->list, &sdev->kcontrol_list);
1023 	return 0;
1024 }
1025 
1026 static int sof_control_unload(struct snd_soc_component *scomp,
1027 			      struct snd_soc_dobj *dobj)
1028 {
1029 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1030 	const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
1031 	struct snd_sof_control *scontrol = dobj->private;
1032 	int ret = 0;
1033 
1034 	dev_dbg(scomp->dev, "tplg: unload control name : %s\n", scontrol->name);
1035 
1036 	if (tplg_ops && tplg_ops->control_free) {
1037 		ret = tplg_ops->control_free(sdev, scontrol);
1038 		if (ret < 0)
1039 			dev_err(scomp->dev, "failed to free control: %s\n", scontrol->name);
1040 	}
1041 
1042 	/* free all data before returning in case of error too */
1043 	kfree(scontrol->ipc_control_data);
1044 	kfree(scontrol->priv);
1045 	kfree(scontrol->name);
1046 	list_del(&scontrol->list);
1047 	kfree(scontrol);
1048 
1049 	return ret;
1050 }
1051 
1052 /*
1053  * DAI Topology
1054  */
1055 
1056 static int sof_connect_dai_widget(struct snd_soc_component *scomp,
1057 				  struct snd_soc_dapm_widget *w,
1058 				  struct snd_soc_tplg_dapm_widget *tw,
1059 				  struct snd_sof_dai *dai)
1060 {
1061 	struct snd_soc_card *card = scomp->card;
1062 	struct snd_soc_pcm_runtime *rtd;
1063 	struct snd_soc_dai *cpu_dai;
1064 	int stream;
1065 	int i;
1066 
1067 	if (!w->sname) {
1068 		dev_err(scomp->dev, "Widget %s does not have stream\n", w->name);
1069 		return -EINVAL;
1070 	}
1071 
1072 	if (w->id == snd_soc_dapm_dai_out)
1073 		stream = SNDRV_PCM_STREAM_CAPTURE;
1074 	else if (w->id == snd_soc_dapm_dai_in)
1075 		stream = SNDRV_PCM_STREAM_PLAYBACK;
1076 	else
1077 		goto end;
1078 
1079 	list_for_each_entry(rtd, &card->rtd_list, list) {
1080 		/* does stream match DAI link ? */
1081 		if (!rtd->dai_link->stream_name ||
1082 		    !strstr(rtd->dai_link->stream_name, w->sname))
1083 			continue;
1084 
1085 		for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1086 			/*
1087 			 * Please create DAI widget in the right order
1088 			 * to ensure BE will connect to the right DAI
1089 			 * widget.
1090 			 */
1091 			if (!snd_soc_dai_get_widget(cpu_dai, stream)) {
1092 				snd_soc_dai_set_widget(cpu_dai, stream, w);
1093 				break;
1094 			}
1095 		}
1096 		if (i == rtd->dai_link->num_cpus) {
1097 			dev_err(scomp->dev, "error: can't find BE for DAI %s\n", w->name);
1098 
1099 			return -EINVAL;
1100 		}
1101 
1102 		dai->name = rtd->dai_link->name;
1103 		dev_dbg(scomp->dev, "tplg: connected widget %s -> DAI link %s\n",
1104 			w->name, rtd->dai_link->name);
1105 	}
1106 end:
1107 	/* check we have a connection */
1108 	if (!dai->name) {
1109 		dev_err(scomp->dev, "error: can't connect DAI %s stream %s\n",
1110 			w->name, w->sname);
1111 		return -EINVAL;
1112 	}
1113 
1114 	return 0;
1115 }
1116 
1117 static void sof_disconnect_dai_widget(struct snd_soc_component *scomp,
1118 				      struct snd_soc_dapm_widget *w)
1119 {
1120 	struct snd_soc_card *card = scomp->card;
1121 	struct snd_soc_pcm_runtime *rtd;
1122 	const char *sname = w->sname;
1123 	struct snd_soc_dai *cpu_dai;
1124 	int i, stream;
1125 
1126 	if (!sname)
1127 		return;
1128 
1129 	if (w->id == snd_soc_dapm_dai_out)
1130 		stream = SNDRV_PCM_STREAM_CAPTURE;
1131 	else if (w->id == snd_soc_dapm_dai_in)
1132 		stream = SNDRV_PCM_STREAM_PLAYBACK;
1133 	else
1134 		return;
1135 
1136 	list_for_each_entry(rtd, &card->rtd_list, list) {
1137 		/* does stream match DAI link ? */
1138 		if (!rtd->dai_link->stream_name ||
1139 		    !strstr(rtd->dai_link->stream_name, sname))
1140 			continue;
1141 
1142 		for_each_rtd_cpu_dais(rtd, i, cpu_dai)
1143 			if (snd_soc_dai_get_widget(cpu_dai, stream) == w) {
1144 				snd_soc_dai_set_widget(cpu_dai, stream, NULL);
1145 				break;
1146 			}
1147 	}
1148 }
1149 
1150 /* bind PCM ID to host component ID */
1151 static int spcm_bind(struct snd_soc_component *scomp, struct snd_sof_pcm *spcm,
1152 		     int dir)
1153 {
1154 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1155 	struct snd_sof_widget *host_widget;
1156 
1157 	if (sdev->dspless_mode_selected)
1158 		return 0;
1159 
1160 	host_widget = snd_sof_find_swidget_sname(scomp,
1161 						 spcm->pcm.caps[dir].name,
1162 						 dir);
1163 	if (!host_widget) {
1164 		dev_err(scomp->dev, "can't find host comp to bind pcm\n");
1165 		return -EINVAL;
1166 	}
1167 
1168 	spcm->stream[dir].comp_id = host_widget->comp_id;
1169 
1170 	return 0;
1171 }
1172 
1173 static int sof_get_token_value(u32 token_id, struct snd_sof_tuple *tuples, int num_tuples)
1174 {
1175 	int i;
1176 
1177 	if (!tuples)
1178 		return -EINVAL;
1179 
1180 	for (i = 0; i < num_tuples; i++) {
1181 		if (tuples[i].token == token_id)
1182 			return tuples[i].value.v;
1183 	}
1184 
1185 	return -EINVAL;
1186 }
1187 
1188 static int sof_widget_parse_tokens(struct snd_soc_component *scomp, struct snd_sof_widget *swidget,
1189 				   struct snd_soc_tplg_dapm_widget *tw,
1190 				   enum sof_tokens *object_token_list, int count)
1191 {
1192 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1193 	const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
1194 	struct snd_soc_tplg_private *private = &tw->priv;
1195 	const struct sof_token_info *token_list;
1196 	int num_tuples = 0;
1197 	int ret, i;
1198 
1199 	token_list = tplg_ops ? tplg_ops->token_list : NULL;
1200 	/* nothing to do if token_list is NULL */
1201 	if (!token_list)
1202 		return 0;
1203 
1204 	if (count > 0 && !object_token_list) {
1205 		dev_err(scomp->dev, "No token list for widget %s\n", swidget->widget->name);
1206 		return -EINVAL;
1207 	}
1208 
1209 	/* calculate max size of tuples array */
1210 	for (i = 0; i < count; i++)
1211 		num_tuples += token_list[object_token_list[i]].count;
1212 
1213 	/* allocate memory for tuples array */
1214 	swidget->tuples = kcalloc(num_tuples, sizeof(*swidget->tuples), GFP_KERNEL);
1215 	if (!swidget->tuples)
1216 		return -ENOMEM;
1217 
1218 	/* parse token list for widget */
1219 	for (i = 0; i < count; i++) {
1220 		int num_sets = 1;
1221 
1222 		if (object_token_list[i] >= SOF_TOKEN_COUNT) {
1223 			dev_err(scomp->dev, "Invalid token id %d for widget %s\n",
1224 				object_token_list[i], swidget->widget->name);
1225 			ret = -EINVAL;
1226 			goto err;
1227 		}
1228 
1229 		switch (object_token_list[i]) {
1230 		case SOF_COMP_EXT_TOKENS:
1231 			/* parse and save UUID in swidget */
1232 			ret = sof_parse_tokens(scomp, swidget,
1233 					       token_list[object_token_list[i]].tokens,
1234 					       token_list[object_token_list[i]].count,
1235 					       private->array, le32_to_cpu(private->size));
1236 			if (ret < 0) {
1237 				dev_err(scomp->dev, "Failed parsing %s for widget %s\n",
1238 					token_list[object_token_list[i]].name,
1239 					swidget->widget->name);
1240 				goto err;
1241 			}
1242 
1243 			continue;
1244 		case SOF_IN_AUDIO_FORMAT_TOKENS:
1245 			num_sets = sof_get_token_value(SOF_TKN_COMP_NUM_INPUT_AUDIO_FORMATS,
1246 						       swidget->tuples, swidget->num_tuples);
1247 			if (num_sets < 0) {
1248 				dev_err(sdev->dev, "Invalid input audio format count for %s\n",
1249 					swidget->widget->name);
1250 				ret = num_sets;
1251 				goto err;
1252 			}
1253 			break;
1254 		case SOF_OUT_AUDIO_FORMAT_TOKENS:
1255 			num_sets = sof_get_token_value(SOF_TKN_COMP_NUM_OUTPUT_AUDIO_FORMATS,
1256 						       swidget->tuples, swidget->num_tuples);
1257 			if (num_sets < 0) {
1258 				dev_err(sdev->dev, "Invalid output audio format count for %s\n",
1259 					swidget->widget->name);
1260 				ret = num_sets;
1261 				goto err;
1262 			}
1263 			break;
1264 		default:
1265 			break;
1266 		}
1267 
1268 		if (num_sets > 1) {
1269 			struct snd_sof_tuple *new_tuples;
1270 
1271 			num_tuples += token_list[object_token_list[i]].count * (num_sets - 1);
1272 			new_tuples = krealloc(swidget->tuples,
1273 					      sizeof(*new_tuples) * num_tuples, GFP_KERNEL);
1274 			if (!new_tuples) {
1275 				ret = -ENOMEM;
1276 				goto err;
1277 			}
1278 
1279 			swidget->tuples = new_tuples;
1280 		}
1281 
1282 		/* copy one set of tuples per token ID into swidget->tuples */
1283 		ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size),
1284 				      object_token_list[i], num_sets, swidget->tuples,
1285 				      num_tuples, &swidget->num_tuples);
1286 		if (ret < 0) {
1287 			dev_err(scomp->dev, "Failed parsing %s for widget %s err: %d\n",
1288 				token_list[object_token_list[i]].name, swidget->widget->name, ret);
1289 			goto err;
1290 		}
1291 	}
1292 
1293 	return 0;
1294 err:
1295 	kfree(swidget->tuples);
1296 	return ret;
1297 }
1298 
1299 static void sof_free_pin_binding(struct snd_sof_widget *swidget,
1300 				 bool pin_type)
1301 {
1302 	char **pin_binding;
1303 	u32 num_pins;
1304 	int i;
1305 
1306 	if (pin_type == SOF_PIN_TYPE_INPUT) {
1307 		pin_binding = swidget->input_pin_binding;
1308 		num_pins = swidget->num_input_pins;
1309 	} else {
1310 		pin_binding = swidget->output_pin_binding;
1311 		num_pins = swidget->num_output_pins;
1312 	}
1313 
1314 	if (pin_binding) {
1315 		for (i = 0; i < num_pins; i++)
1316 			kfree(pin_binding[i]);
1317 	}
1318 
1319 	kfree(pin_binding);
1320 }
1321 
1322 static int sof_parse_pin_binding(struct snd_sof_widget *swidget,
1323 				 struct snd_soc_tplg_private *priv, bool pin_type)
1324 {
1325 	const struct sof_topology_token *pin_binding_token;
1326 	char *pin_binding[SOF_WIDGET_MAX_NUM_PINS];
1327 	int token_count;
1328 	u32 num_pins;
1329 	char **pb;
1330 	int ret;
1331 	int i;
1332 
1333 	if (pin_type == SOF_PIN_TYPE_INPUT) {
1334 		num_pins = swidget->num_input_pins;
1335 		pin_binding_token = comp_input_pin_binding_tokens;
1336 		token_count = ARRAY_SIZE(comp_input_pin_binding_tokens);
1337 	} else {
1338 		num_pins = swidget->num_output_pins;
1339 		pin_binding_token = comp_output_pin_binding_tokens;
1340 		token_count = ARRAY_SIZE(comp_output_pin_binding_tokens);
1341 	}
1342 
1343 	memset(pin_binding, 0, SOF_WIDGET_MAX_NUM_PINS * sizeof(char *));
1344 	ret = sof_parse_token_sets(swidget->scomp, pin_binding, pin_binding_token,
1345 				   token_count, priv->array, le32_to_cpu(priv->size),
1346 				   num_pins, sizeof(char *));
1347 	if (ret < 0)
1348 		goto err;
1349 
1350 	/* copy pin binding array to swidget only if it is defined in topology */
1351 	if (pin_binding[0]) {
1352 		pb = kmemdup(pin_binding, num_pins * sizeof(char *), GFP_KERNEL);
1353 		if (!pb) {
1354 			ret = -ENOMEM;
1355 			goto err;
1356 		}
1357 		if (pin_type == SOF_PIN_TYPE_INPUT)
1358 			swidget->input_pin_binding = pb;
1359 		else
1360 			swidget->output_pin_binding = pb;
1361 	}
1362 
1363 	return 0;
1364 
1365 err:
1366 	for (i = 0; i < num_pins; i++)
1367 		kfree(pin_binding[i]);
1368 
1369 	return ret;
1370 }
1371 
1372 static int get_w_no_wname_in_long_name(void *elem, void *object, u32 offset)
1373 {
1374 	struct snd_soc_tplg_vendor_value_elem *velem = elem;
1375 	struct snd_soc_dapm_widget *w = object;
1376 
1377 	w->no_wname_in_kcontrol_name = !!le32_to_cpu(velem->value);
1378 	return 0;
1379 }
1380 
1381 static const struct sof_topology_token dapm_widget_tokens[] = {
1382 	{SOF_TKN_COMP_NO_WNAME_IN_KCONTROL_NAME, SND_SOC_TPLG_TUPLE_TYPE_BOOL,
1383 	 get_w_no_wname_in_long_name, 0}
1384 };
1385 
1386 /* external widget init - used for any driver specific init */
1387 static int sof_widget_ready(struct snd_soc_component *scomp, int index,
1388 			    struct snd_soc_dapm_widget *w,
1389 			    struct snd_soc_tplg_dapm_widget *tw)
1390 {
1391 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1392 	const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
1393 	const struct sof_ipc_tplg_widget_ops *widget_ops;
1394 	struct snd_soc_tplg_private *priv = &tw->priv;
1395 	enum sof_tokens *token_list = NULL;
1396 	struct snd_sof_widget *swidget;
1397 	struct snd_sof_dai *dai;
1398 	int token_list_size = 0;
1399 	int ret = 0;
1400 
1401 	swidget = kzalloc(sizeof(*swidget), GFP_KERNEL);
1402 	if (!swidget)
1403 		return -ENOMEM;
1404 
1405 	swidget->scomp = scomp;
1406 	swidget->widget = w;
1407 	swidget->comp_id = sdev->next_comp_id++;
1408 	swidget->id = w->id;
1409 	swidget->pipeline_id = index;
1410 	swidget->private = NULL;
1411 	mutex_init(&swidget->setup_mutex);
1412 
1413 	ida_init(&swidget->output_queue_ida);
1414 	ida_init(&swidget->input_queue_ida);
1415 
1416 	ret = sof_parse_tokens(scomp, w, dapm_widget_tokens, ARRAY_SIZE(dapm_widget_tokens),
1417 			       priv->array, le32_to_cpu(priv->size));
1418 	if (ret < 0) {
1419 		dev_err(scomp->dev, "failed to parse dapm widget tokens for %s\n",
1420 			w->name);
1421 		goto widget_free;
1422 	}
1423 
1424 	ret = sof_parse_tokens(scomp, swidget, comp_pin_tokens,
1425 			       ARRAY_SIZE(comp_pin_tokens), priv->array,
1426 			       le32_to_cpu(priv->size));
1427 	if (ret < 0) {
1428 		dev_err(scomp->dev, "failed to parse component pin tokens for %s\n",
1429 			w->name);
1430 		goto widget_free;
1431 	}
1432 
1433 	if (swidget->num_input_pins > SOF_WIDGET_MAX_NUM_PINS ||
1434 	    swidget->num_output_pins > SOF_WIDGET_MAX_NUM_PINS) {
1435 		dev_err(scomp->dev, "invalid pins for %s: [input: %d, output: %d]\n",
1436 			swidget->widget->name, swidget->num_input_pins, swidget->num_output_pins);
1437 		ret = -EINVAL;
1438 		goto widget_free;
1439 	}
1440 
1441 	if (swidget->num_input_pins > 1) {
1442 		ret = sof_parse_pin_binding(swidget, priv, SOF_PIN_TYPE_INPUT);
1443 		/* on parsing error, pin binding is not allocated, nothing to free. */
1444 		if (ret < 0) {
1445 			dev_err(scomp->dev, "failed to parse input pin binding for %s\n",
1446 				w->name);
1447 			goto widget_free;
1448 		}
1449 	}
1450 
1451 	if (swidget->num_output_pins > 1) {
1452 		ret = sof_parse_pin_binding(swidget, priv, SOF_PIN_TYPE_OUTPUT);
1453 		/* on parsing error, pin binding is not allocated, nothing to free. */
1454 		if (ret < 0) {
1455 			dev_err(scomp->dev, "failed to parse output pin binding for %s\n",
1456 				w->name);
1457 			goto widget_free;
1458 		}
1459 	}
1460 
1461 	dev_dbg(scomp->dev,
1462 		"tplg: widget %d (%s) is ready [type: %d, pipe: %d, pins: %d / %d, stream: %s]\n",
1463 		swidget->comp_id, w->name, swidget->id, index,
1464 		swidget->num_input_pins, swidget->num_output_pins,
1465 		strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0 ? w->sname : "none");
1466 
1467 	widget_ops = tplg_ops ? tplg_ops->widget : NULL;
1468 	if (widget_ops) {
1469 		token_list = widget_ops[w->id].token_list;
1470 		token_list_size = widget_ops[w->id].token_list_size;
1471 	}
1472 
1473 	/* handle any special case widgets */
1474 	switch (w->id) {
1475 	case snd_soc_dapm_dai_in:
1476 	case snd_soc_dapm_dai_out:
1477 		dai = kzalloc(sizeof(*dai), GFP_KERNEL);
1478 		if (!dai) {
1479 			ret = -ENOMEM;
1480 			goto widget_free;
1481 		}
1482 
1483 		ret = sof_widget_parse_tokens(scomp, swidget, tw, token_list, token_list_size);
1484 		if (!ret)
1485 			ret = sof_connect_dai_widget(scomp, w, tw, dai);
1486 		if (ret < 0) {
1487 			kfree(dai);
1488 			break;
1489 		}
1490 		list_add(&dai->list, &sdev->dai_list);
1491 		swidget->private = dai;
1492 		break;
1493 	case snd_soc_dapm_effect:
1494 		/* check we have some tokens - we need at least process type */
1495 		if (le32_to_cpu(tw->priv.size) == 0) {
1496 			dev_err(scomp->dev, "error: process tokens not found\n");
1497 			ret = -EINVAL;
1498 			break;
1499 		}
1500 		ret = sof_widget_parse_tokens(scomp, swidget, tw, token_list, token_list_size);
1501 		break;
1502 	case snd_soc_dapm_pga:
1503 		if (!le32_to_cpu(tw->num_kcontrols)) {
1504 			dev_err(scomp->dev, "invalid kcontrol count %d for volume\n",
1505 				tw->num_kcontrols);
1506 			ret = -EINVAL;
1507 			break;
1508 		}
1509 
1510 		fallthrough;
1511 	case snd_soc_dapm_mixer:
1512 	case snd_soc_dapm_buffer:
1513 	case snd_soc_dapm_scheduler:
1514 	case snd_soc_dapm_aif_out:
1515 	case snd_soc_dapm_aif_in:
1516 	case snd_soc_dapm_src:
1517 	case snd_soc_dapm_asrc:
1518 	case snd_soc_dapm_siggen:
1519 	case snd_soc_dapm_mux:
1520 	case snd_soc_dapm_demux:
1521 		ret = sof_widget_parse_tokens(scomp, swidget, tw,  token_list, token_list_size);
1522 		break;
1523 	case snd_soc_dapm_switch:
1524 	case snd_soc_dapm_dai_link:
1525 	case snd_soc_dapm_kcontrol:
1526 	default:
1527 		dev_dbg(scomp->dev, "widget type %d name %s not handled\n", swidget->id, tw->name);
1528 		break;
1529 	}
1530 
1531 	/* check token parsing reply */
1532 	if (ret < 0) {
1533 		dev_err(scomp->dev,
1534 			"failed to add widget type %d name : %s stream %s\n",
1535 			swidget->id, tw->name, strnlen(tw->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0
1536 							? tw->sname : "none");
1537 		goto widget_free;
1538 	}
1539 
1540 	if (sof_debug_check_flag(SOF_DBG_DISABLE_MULTICORE)) {
1541 		swidget->core = SOF_DSP_PRIMARY_CORE;
1542 	} else {
1543 		int core = sof_get_token_value(SOF_TKN_COMP_CORE_ID, swidget->tuples,
1544 					       swidget->num_tuples);
1545 
1546 		if (core >= 0)
1547 			swidget->core = core;
1548 	}
1549 
1550 	/* bind widget to external event */
1551 	if (tw->event_type) {
1552 		if (widget_ops && widget_ops[w->id].bind_event) {
1553 			ret = widget_ops[w->id].bind_event(scomp, swidget,
1554 							   le16_to_cpu(tw->event_type));
1555 			if (ret) {
1556 				dev_err(scomp->dev, "widget event binding failed for %s\n",
1557 					swidget->widget->name);
1558 				goto free;
1559 			}
1560 		}
1561 	}
1562 
1563 	/* create and add pipeline for scheduler type widgets */
1564 	if (w->id == snd_soc_dapm_scheduler) {
1565 		struct snd_sof_pipeline *spipe;
1566 
1567 		spipe = kzalloc(sizeof(*spipe), GFP_KERNEL);
1568 		if (!spipe) {
1569 			ret = -ENOMEM;
1570 			goto free;
1571 		}
1572 
1573 		spipe->pipe_widget = swidget;
1574 		swidget->spipe = spipe;
1575 		list_add(&spipe->list, &sdev->pipeline_list);
1576 	}
1577 
1578 	w->dobj.private = swidget;
1579 	list_add(&swidget->list, &sdev->widget_list);
1580 	return ret;
1581 free:
1582 	kfree(swidget->private);
1583 	kfree(swidget->tuples);
1584 widget_free:
1585 	kfree(swidget);
1586 	return ret;
1587 }
1588 
1589 static int sof_route_unload(struct snd_soc_component *scomp,
1590 			    struct snd_soc_dobj *dobj)
1591 {
1592 	struct snd_sof_route *sroute;
1593 
1594 	sroute = dobj->private;
1595 	if (!sroute)
1596 		return 0;
1597 
1598 	/* free sroute and its private data */
1599 	kfree(sroute->private);
1600 	list_del(&sroute->list);
1601 	kfree(sroute);
1602 
1603 	return 0;
1604 }
1605 
1606 static int sof_widget_unload(struct snd_soc_component *scomp,
1607 			     struct snd_soc_dobj *dobj)
1608 {
1609 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1610 	const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
1611 	const struct sof_ipc_tplg_widget_ops *widget_ops;
1612 	const struct snd_kcontrol_new *kc;
1613 	struct snd_soc_dapm_widget *widget;
1614 	struct snd_sof_control *scontrol;
1615 	struct snd_sof_widget *swidget;
1616 	struct soc_mixer_control *sm;
1617 	struct soc_bytes_ext *sbe;
1618 	struct snd_sof_dai *dai;
1619 	struct soc_enum *se;
1620 	int i;
1621 
1622 	swidget = dobj->private;
1623 	if (!swidget)
1624 		return 0;
1625 
1626 	widget = swidget->widget;
1627 
1628 	switch (swidget->id) {
1629 	case snd_soc_dapm_dai_in:
1630 	case snd_soc_dapm_dai_out:
1631 		dai = swidget->private;
1632 
1633 		if (dai)
1634 			list_del(&dai->list);
1635 
1636 		sof_disconnect_dai_widget(scomp, widget);
1637 
1638 		break;
1639 	case snd_soc_dapm_scheduler:
1640 	{
1641 		struct snd_sof_pipeline *spipe = swidget->spipe;
1642 
1643 		list_del(&spipe->list);
1644 		kfree(spipe);
1645 		swidget->spipe = NULL;
1646 		break;
1647 	}
1648 	default:
1649 		break;
1650 	}
1651 	for (i = 0; i < widget->num_kcontrols; i++) {
1652 		kc = &widget->kcontrol_news[i];
1653 		switch (widget->dobj.widget.kcontrol_type[i]) {
1654 		case SND_SOC_TPLG_TYPE_MIXER:
1655 			sm = (struct soc_mixer_control *)kc->private_value;
1656 			scontrol = sm->dobj.private;
1657 			if (sm->max > 1)
1658 				kfree(scontrol->volume_table);
1659 			break;
1660 		case SND_SOC_TPLG_TYPE_ENUM:
1661 			se = (struct soc_enum *)kc->private_value;
1662 			scontrol = se->dobj.private;
1663 			break;
1664 		case SND_SOC_TPLG_TYPE_BYTES:
1665 			sbe = (struct soc_bytes_ext *)kc->private_value;
1666 			scontrol = sbe->dobj.private;
1667 			break;
1668 		default:
1669 			dev_warn(scomp->dev, "unsupported kcontrol_type\n");
1670 			goto out;
1671 		}
1672 		kfree(scontrol->ipc_control_data);
1673 		list_del(&scontrol->list);
1674 		kfree(scontrol->name);
1675 		kfree(scontrol);
1676 	}
1677 
1678 out:
1679 	/* free IPC related data */
1680 	widget_ops = tplg_ops ? tplg_ops->widget : NULL;
1681 	if (widget_ops && widget_ops[swidget->id].ipc_free)
1682 		widget_ops[swidget->id].ipc_free(swidget);
1683 
1684 	ida_destroy(&swidget->output_queue_ida);
1685 	ida_destroy(&swidget->input_queue_ida);
1686 
1687 	sof_free_pin_binding(swidget, SOF_PIN_TYPE_INPUT);
1688 	sof_free_pin_binding(swidget, SOF_PIN_TYPE_OUTPUT);
1689 
1690 	kfree(swidget->tuples);
1691 
1692 	/* remove and free swidget object */
1693 	list_del(&swidget->list);
1694 	kfree(swidget);
1695 
1696 	return 0;
1697 }
1698 
1699 /*
1700  * DAI HW configuration.
1701  */
1702 
1703 /* FE DAI - used for any driver specific init */
1704 static int sof_dai_load(struct snd_soc_component *scomp, int index,
1705 			struct snd_soc_dai_driver *dai_drv,
1706 			struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
1707 {
1708 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1709 	const struct sof_ipc_pcm_ops *ipc_pcm_ops = sof_ipc_get_ops(sdev, pcm);
1710 	struct snd_soc_tplg_stream_caps *caps;
1711 	struct snd_soc_tplg_private *private = &pcm->priv;
1712 	struct snd_sof_pcm *spcm;
1713 	int stream;
1714 	int ret;
1715 
1716 	/* nothing to do for BEs atm */
1717 	if (!pcm)
1718 		return 0;
1719 
1720 	spcm = kzalloc(sizeof(*spcm), GFP_KERNEL);
1721 	if (!spcm)
1722 		return -ENOMEM;
1723 
1724 	spcm->scomp = scomp;
1725 
1726 	for_each_pcm_streams(stream) {
1727 		spcm->stream[stream].comp_id = COMP_ID_UNASSIGNED;
1728 		if (pcm->compress)
1729 			snd_sof_compr_init_elapsed_work(&spcm->stream[stream].period_elapsed_work);
1730 		else
1731 			snd_sof_pcm_init_elapsed_work(&spcm->stream[stream].period_elapsed_work);
1732 	}
1733 
1734 	spcm->pcm = *pcm;
1735 	dev_dbg(scomp->dev, "tplg: load pcm %s\n", pcm->dai_name);
1736 
1737 	/* perform pcm set op */
1738 	if (ipc_pcm_ops && ipc_pcm_ops->pcm_setup) {
1739 		ret = ipc_pcm_ops->pcm_setup(sdev, spcm);
1740 		if (ret < 0) {
1741 			kfree(spcm);
1742 			return ret;
1743 		}
1744 	}
1745 
1746 	dai_drv->dobj.private = spcm;
1747 	list_add(&spcm->list, &sdev->pcm_list);
1748 
1749 	ret = sof_parse_tokens(scomp, spcm, stream_tokens,
1750 			       ARRAY_SIZE(stream_tokens), private->array,
1751 			       le32_to_cpu(private->size));
1752 	if (ret) {
1753 		dev_err(scomp->dev, "error: parse stream tokens failed %d\n",
1754 			le32_to_cpu(private->size));
1755 		return ret;
1756 	}
1757 
1758 	/* do we need to allocate playback PCM DMA pages */
1759 	if (!spcm->pcm.playback)
1760 		goto capture;
1761 
1762 	stream = SNDRV_PCM_STREAM_PLAYBACK;
1763 
1764 	caps = &spcm->pcm.caps[stream];
1765 
1766 	/* allocate playback page table buffer */
1767 	ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev,
1768 				  PAGE_SIZE, &spcm->stream[stream].page_table);
1769 	if (ret < 0) {
1770 		dev_err(scomp->dev, "error: can't alloc page table for %s %d\n",
1771 			caps->name, ret);
1772 
1773 		return ret;
1774 	}
1775 
1776 	/* bind pcm to host comp */
1777 	ret = spcm_bind(scomp, spcm, stream);
1778 	if (ret) {
1779 		dev_err(scomp->dev,
1780 			"error: can't bind pcm to host\n");
1781 		goto free_playback_tables;
1782 	}
1783 
1784 capture:
1785 	stream = SNDRV_PCM_STREAM_CAPTURE;
1786 
1787 	/* do we need to allocate capture PCM DMA pages */
1788 	if (!spcm->pcm.capture)
1789 		return ret;
1790 
1791 	caps = &spcm->pcm.caps[stream];
1792 
1793 	/* allocate capture page table buffer */
1794 	ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev,
1795 				  PAGE_SIZE, &spcm->stream[stream].page_table);
1796 	if (ret < 0) {
1797 		dev_err(scomp->dev, "error: can't alloc page table for %s %d\n",
1798 			caps->name, ret);
1799 		goto free_playback_tables;
1800 	}
1801 
1802 	/* bind pcm to host comp */
1803 	ret = spcm_bind(scomp, spcm, stream);
1804 	if (ret) {
1805 		dev_err(scomp->dev,
1806 			"error: can't bind pcm to host\n");
1807 		snd_dma_free_pages(&spcm->stream[stream].page_table);
1808 		goto free_playback_tables;
1809 	}
1810 
1811 	return ret;
1812 
1813 free_playback_tables:
1814 	if (spcm->pcm.playback)
1815 		snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table);
1816 
1817 	return ret;
1818 }
1819 
1820 static int sof_dai_unload(struct snd_soc_component *scomp,
1821 			  struct snd_soc_dobj *dobj)
1822 {
1823 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1824 	const struct sof_ipc_pcm_ops *ipc_pcm_ops = sof_ipc_get_ops(sdev, pcm);
1825 	struct snd_sof_pcm *spcm = dobj->private;
1826 
1827 	/* free PCM DMA pages */
1828 	if (spcm->pcm.playback)
1829 		snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table);
1830 
1831 	if (spcm->pcm.capture)
1832 		snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_CAPTURE].page_table);
1833 
1834 	/* perform pcm free op */
1835 	if (ipc_pcm_ops && ipc_pcm_ops->pcm_free)
1836 		ipc_pcm_ops->pcm_free(sdev, spcm);
1837 
1838 	/* remove from list and free spcm */
1839 	list_del(&spcm->list);
1840 	kfree(spcm);
1841 
1842 	return 0;
1843 }
1844 
1845 static const struct sof_topology_token common_dai_link_tokens[] = {
1846 	{SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type,
1847 		offsetof(struct snd_sof_dai_link, type)},
1848 };
1849 
1850 /* DAI link - used for any driver specific init */
1851 static int sof_link_load(struct snd_soc_component *scomp, int index, struct snd_soc_dai_link *link,
1852 			 struct snd_soc_tplg_link_config *cfg)
1853 {
1854 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1855 	const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
1856 	struct snd_soc_tplg_private *private = &cfg->priv;
1857 	const struct sof_token_info *token_list;
1858 	struct snd_sof_dai_link *slink;
1859 	u32 token_id = 0;
1860 	int num_tuples = 0;
1861 	int ret, num_sets;
1862 
1863 	if (!link->platforms) {
1864 		dev_err(scomp->dev, "error: no platforms\n");
1865 		return -EINVAL;
1866 	}
1867 	link->platforms->name = dev_name(scomp->dev);
1868 
1869 	if (tplg_ops && tplg_ops->link_setup) {
1870 		ret = tplg_ops->link_setup(sdev, link);
1871 		if (ret < 0)
1872 			return ret;
1873 	}
1874 
1875 	/* Set nonatomic property for FE dai links as their trigger action involves IPC's */
1876 	if (!link->no_pcm) {
1877 		link->nonatomic = true;
1878 		return 0;
1879 	}
1880 
1881 	/* check we have some tokens - we need at least DAI type */
1882 	if (le32_to_cpu(private->size) == 0) {
1883 		dev_err(scomp->dev, "error: expected tokens for DAI, none found\n");
1884 		return -EINVAL;
1885 	}
1886 
1887 	slink = kzalloc(sizeof(*slink), GFP_KERNEL);
1888 	if (!slink)
1889 		return -ENOMEM;
1890 
1891 	slink->num_hw_configs = le32_to_cpu(cfg->num_hw_configs);
1892 	slink->hw_configs = kmemdup(cfg->hw_config,
1893 				    sizeof(*slink->hw_configs) * slink->num_hw_configs,
1894 				    GFP_KERNEL);
1895 	if (!slink->hw_configs) {
1896 		kfree(slink);
1897 		return -ENOMEM;
1898 	}
1899 
1900 	slink->default_hw_cfg_id = le32_to_cpu(cfg->default_hw_config_id);
1901 	slink->link = link;
1902 
1903 	dev_dbg(scomp->dev, "tplg: %d hw_configs found, default id: %d for dai link %s!\n",
1904 		slink->num_hw_configs, slink->default_hw_cfg_id, link->name);
1905 
1906 	ret = sof_parse_tokens(scomp, slink, common_dai_link_tokens,
1907 			       ARRAY_SIZE(common_dai_link_tokens),
1908 			       private->array, le32_to_cpu(private->size));
1909 	if (ret < 0) {
1910 		dev_err(scomp->dev, "Failed tp parse common DAI link tokens\n");
1911 		kfree(slink->hw_configs);
1912 		kfree(slink);
1913 		return ret;
1914 	}
1915 
1916 	token_list = tplg_ops ? tplg_ops->token_list : NULL;
1917 	if (!token_list)
1918 		goto out;
1919 
1920 	/* calculate size of tuples array */
1921 	num_tuples += token_list[SOF_DAI_LINK_TOKENS].count;
1922 	num_sets = slink->num_hw_configs;
1923 	switch (slink->type) {
1924 	case SOF_DAI_INTEL_SSP:
1925 		token_id = SOF_SSP_TOKENS;
1926 		num_tuples += token_list[SOF_SSP_TOKENS].count * slink->num_hw_configs;
1927 		break;
1928 	case SOF_DAI_INTEL_DMIC:
1929 		token_id = SOF_DMIC_TOKENS;
1930 		num_tuples += token_list[SOF_DMIC_TOKENS].count;
1931 
1932 		/* Allocate memory for max PDM controllers */
1933 		num_tuples += token_list[SOF_DMIC_PDM_TOKENS].count * SOF_DAI_INTEL_DMIC_NUM_CTRL;
1934 		break;
1935 	case SOF_DAI_INTEL_HDA:
1936 		token_id = SOF_HDA_TOKENS;
1937 		num_tuples += token_list[SOF_HDA_TOKENS].count;
1938 		break;
1939 	case SOF_DAI_INTEL_ALH:
1940 		token_id = SOF_ALH_TOKENS;
1941 		num_tuples += token_list[SOF_ALH_TOKENS].count;
1942 		break;
1943 	case SOF_DAI_IMX_SAI:
1944 		token_id = SOF_SAI_TOKENS;
1945 		num_tuples += token_list[SOF_SAI_TOKENS].count;
1946 		break;
1947 	case SOF_DAI_IMX_ESAI:
1948 		token_id = SOF_ESAI_TOKENS;
1949 		num_tuples += token_list[SOF_ESAI_TOKENS].count;
1950 		break;
1951 	case SOF_DAI_MEDIATEK_AFE:
1952 		token_id = SOF_AFE_TOKENS;
1953 		num_tuples += token_list[SOF_AFE_TOKENS].count;
1954 		break;
1955 	case SOF_DAI_AMD_DMIC:
1956 		token_id = SOF_ACPDMIC_TOKENS;
1957 		num_tuples += token_list[SOF_ACPDMIC_TOKENS].count;
1958 		break;
1959 	case SOF_DAI_AMD_BT:
1960 	case SOF_DAI_AMD_SP:
1961 	case SOF_DAI_AMD_HS:
1962 	case SOF_DAI_AMD_SP_VIRTUAL:
1963 	case SOF_DAI_AMD_HS_VIRTUAL:
1964 		token_id = SOF_ACPI2S_TOKENS;
1965 		num_tuples += token_list[SOF_ACPI2S_TOKENS].count;
1966 		break;
1967 	case SOF_DAI_IMX_MICFIL:
1968 		token_id = SOF_MICFIL_TOKENS;
1969 		num_tuples += token_list[SOF_MICFIL_TOKENS].count;
1970 		break;
1971 	case SOF_DAI_AMD_SDW:
1972 		token_id = SOF_ACP_SDW_TOKENS;
1973 		num_tuples += token_list[SOF_ACP_SDW_TOKENS].count;
1974 		break;
1975 	default:
1976 		break;
1977 	}
1978 
1979 	/* allocate memory for tuples array */
1980 	slink->tuples = kcalloc(num_tuples, sizeof(*slink->tuples), GFP_KERNEL);
1981 	if (!slink->tuples) {
1982 		kfree(slink->hw_configs);
1983 		kfree(slink);
1984 		return -ENOMEM;
1985 	}
1986 
1987 	if (token_list[SOF_DAI_LINK_TOKENS].tokens) {
1988 		/* parse one set of DAI link tokens */
1989 		ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size),
1990 				      SOF_DAI_LINK_TOKENS, 1, slink->tuples,
1991 				      num_tuples, &slink->num_tuples);
1992 		if (ret < 0) {
1993 			dev_err(scomp->dev, "failed to parse %s for dai link %s\n",
1994 				token_list[SOF_DAI_LINK_TOKENS].name, link->name);
1995 			goto err;
1996 		}
1997 	}
1998 
1999 	/* nothing more to do if there are no DAI type-specific tokens defined */
2000 	if (!token_id || !token_list[token_id].tokens)
2001 		goto out;
2002 
2003 	/* parse "num_sets" sets of DAI-specific tokens */
2004 	ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size),
2005 			      token_id, num_sets, slink->tuples, num_tuples, &slink->num_tuples);
2006 	if (ret < 0) {
2007 		dev_err(scomp->dev, "failed to parse %s for dai link %s\n",
2008 			token_list[token_id].name, link->name);
2009 		goto err;
2010 	}
2011 
2012 	/* for DMIC, also parse all sets of DMIC PDM tokens based on active PDM count */
2013 	if (token_id == SOF_DMIC_TOKENS) {
2014 		num_sets = sof_get_token_value(SOF_TKN_INTEL_DMIC_NUM_PDM_ACTIVE,
2015 					       slink->tuples, slink->num_tuples);
2016 
2017 		if (num_sets < 0) {
2018 			dev_err(sdev->dev, "Invalid active PDM count for %s\n", link->name);
2019 			ret = num_sets;
2020 			goto err;
2021 		}
2022 
2023 		ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size),
2024 				      SOF_DMIC_PDM_TOKENS, num_sets, slink->tuples,
2025 				      num_tuples, &slink->num_tuples);
2026 		if (ret < 0) {
2027 			dev_err(scomp->dev, "failed to parse %s for dai link %s\n",
2028 				token_list[SOF_DMIC_PDM_TOKENS].name, link->name);
2029 			goto err;
2030 		}
2031 	}
2032 out:
2033 	link->dobj.private = slink;
2034 	list_add(&slink->list, &sdev->dai_link_list);
2035 
2036 	return 0;
2037 
2038 err:
2039 	kfree(slink->tuples);
2040 	kfree(slink->hw_configs);
2041 	kfree(slink);
2042 
2043 	return ret;
2044 }
2045 
2046 static int sof_link_unload(struct snd_soc_component *scomp, struct snd_soc_dobj *dobj)
2047 {
2048 	struct snd_sof_dai_link *slink = dobj->private;
2049 
2050 	if (!slink)
2051 		return 0;
2052 
2053 	slink->link->platforms->name = NULL;
2054 
2055 	kfree(slink->tuples);
2056 	list_del(&slink->list);
2057 	kfree(slink->hw_configs);
2058 	kfree(slink);
2059 	dobj->private = NULL;
2060 
2061 	return 0;
2062 }
2063 
2064 /* DAI link - used for any driver specific init */
2065 static int sof_route_load(struct snd_soc_component *scomp, int index,
2066 			  struct snd_soc_dapm_route *route)
2067 {
2068 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2069 	struct snd_sof_widget *source_swidget, *sink_swidget;
2070 	struct snd_soc_dobj *dobj = &route->dobj;
2071 	struct snd_sof_route *sroute;
2072 	int ret = 0;
2073 
2074 	/* allocate memory for sroute and connect */
2075 	sroute = kzalloc(sizeof(*sroute), GFP_KERNEL);
2076 	if (!sroute)
2077 		return -ENOMEM;
2078 
2079 	sroute->scomp = scomp;
2080 	dev_dbg(scomp->dev, "sink %s control %s source %s\n",
2081 		route->sink, route->control ? route->control : "none",
2082 		route->source);
2083 
2084 	/* source component */
2085 	source_swidget = snd_sof_find_swidget(scomp, (char *)route->source);
2086 	if (!source_swidget) {
2087 		dev_err(scomp->dev, "error: source %s not found\n",
2088 			route->source);
2089 		ret = -EINVAL;
2090 		goto err;
2091 	}
2092 
2093 	/*
2094 	 * Virtual widgets of type output/out_drv may be added in topology
2095 	 * for compatibility. These are not handled by the FW.
2096 	 * So, don't send routes whose source/sink widget is of such types
2097 	 * to the DSP.
2098 	 */
2099 	if (source_swidget->id == snd_soc_dapm_out_drv ||
2100 	    source_swidget->id == snd_soc_dapm_output)
2101 		goto err;
2102 
2103 	/* sink component */
2104 	sink_swidget = snd_sof_find_swidget(scomp, (char *)route->sink);
2105 	if (!sink_swidget) {
2106 		dev_err(scomp->dev, "error: sink %s not found\n",
2107 			route->sink);
2108 		ret = -EINVAL;
2109 		goto err;
2110 	}
2111 
2112 	/*
2113 	 * Don't send routes whose sink widget is of type
2114 	 * output or out_drv to the DSP
2115 	 */
2116 	if (sink_swidget->id == snd_soc_dapm_out_drv ||
2117 	    sink_swidget->id == snd_soc_dapm_output)
2118 		goto err;
2119 
2120 	sroute->route = route;
2121 	dobj->private = sroute;
2122 	sroute->src_widget = source_swidget;
2123 	sroute->sink_widget = sink_swidget;
2124 
2125 	/* add route to route list */
2126 	list_add(&sroute->list, &sdev->route_list);
2127 
2128 	return 0;
2129 err:
2130 	kfree(sroute);
2131 	return ret;
2132 }
2133 
2134 /**
2135  * sof_set_widget_pipeline - Set pipeline for a component
2136  * @sdev: pointer to struct snd_sof_dev
2137  * @spipe: pointer to struct snd_sof_pipeline
2138  * @swidget: pointer to struct snd_sof_widget that has the same pipeline ID as @pipe_widget
2139  *
2140  * Return: 0 if successful, -EINVAL on error.
2141  * The function checks if @swidget is associated with any volatile controls. If so, setting
2142  * the dynamic_pipeline_widget is disallowed.
2143  */
2144 static int sof_set_widget_pipeline(struct snd_sof_dev *sdev, struct snd_sof_pipeline *spipe,
2145 				   struct snd_sof_widget *swidget)
2146 {
2147 	struct snd_sof_widget *pipe_widget = spipe->pipe_widget;
2148 	struct snd_sof_control *scontrol;
2149 
2150 	if (pipe_widget->dynamic_pipeline_widget) {
2151 		/* dynamic widgets cannot have volatile kcontrols */
2152 		list_for_each_entry(scontrol, &sdev->kcontrol_list, list)
2153 			if (scontrol->comp_id == swidget->comp_id &&
2154 			    (scontrol->access & SNDRV_CTL_ELEM_ACCESS_VOLATILE)) {
2155 				dev_err(sdev->dev,
2156 					"error: volatile control found for dynamic widget %s\n",
2157 					swidget->widget->name);
2158 				return -EINVAL;
2159 			}
2160 	}
2161 
2162 	/* set the pipeline and apply the dynamic_pipeline_widget_flag */
2163 	swidget->spipe = spipe;
2164 	swidget->dynamic_pipeline_widget = pipe_widget->dynamic_pipeline_widget;
2165 
2166 	return 0;
2167 }
2168 
2169 /* completion - called at completion of firmware loading */
2170 static int sof_complete(struct snd_soc_component *scomp)
2171 {
2172 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2173 	const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
2174 	const struct sof_ipc_tplg_widget_ops *widget_ops;
2175 	struct snd_sof_control *scontrol;
2176 	struct snd_sof_pipeline *spipe;
2177 	int ret;
2178 
2179 	widget_ops = tplg_ops ? tplg_ops->widget : NULL;
2180 
2181 	/* first update all control IPC structures based on the IPC version */
2182 	if (tplg_ops && tplg_ops->control_setup)
2183 		list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
2184 			ret = tplg_ops->control_setup(sdev, scontrol);
2185 			if (ret < 0) {
2186 				dev_err(sdev->dev, "failed updating IPC struct for control %s\n",
2187 					scontrol->name);
2188 				return ret;
2189 			}
2190 		}
2191 
2192 	/* set up the IPC structures for the pipeline widgets */
2193 	list_for_each_entry(spipe, &sdev->pipeline_list, list) {
2194 		struct snd_sof_widget *pipe_widget = spipe->pipe_widget;
2195 		struct snd_sof_widget *swidget;
2196 
2197 		pipe_widget->instance_id = -EINVAL;
2198 
2199 		/* Update the scheduler widget's IPC structure */
2200 		if (widget_ops && widget_ops[pipe_widget->id].ipc_setup) {
2201 			ret = widget_ops[pipe_widget->id].ipc_setup(pipe_widget);
2202 			if (ret < 0) {
2203 				dev_err(sdev->dev, "failed updating IPC struct for %s\n",
2204 					pipe_widget->widget->name);
2205 				return ret;
2206 			}
2207 		}
2208 
2209 		/* set the pipeline and update the IPC structure for the non scheduler widgets */
2210 		list_for_each_entry(swidget, &sdev->widget_list, list)
2211 			if (swidget->widget->id != snd_soc_dapm_scheduler &&
2212 			    swidget->pipeline_id == pipe_widget->pipeline_id) {
2213 				ret = sof_set_widget_pipeline(sdev, spipe, swidget);
2214 				if (ret < 0)
2215 					return ret;
2216 
2217 				if (widget_ops && widget_ops[swidget->id].ipc_setup) {
2218 					ret = widget_ops[swidget->id].ipc_setup(swidget);
2219 					if (ret < 0) {
2220 						dev_err(sdev->dev,
2221 							"failed updating IPC struct for %s\n",
2222 							swidget->widget->name);
2223 						return ret;
2224 					}
2225 				}
2226 			}
2227 	}
2228 
2229 	/* verify topology components loading including dynamic pipelines */
2230 	if (sof_debug_check_flag(SOF_DBG_VERIFY_TPLG)) {
2231 		if (tplg_ops && tplg_ops->set_up_all_pipelines &&
2232 		    tplg_ops->tear_down_all_pipelines) {
2233 			ret = tplg_ops->set_up_all_pipelines(sdev, true);
2234 			if (ret < 0) {
2235 				dev_err(sdev->dev, "Failed to set up all topology pipelines: %d\n",
2236 					ret);
2237 				return ret;
2238 			}
2239 
2240 			ret = tplg_ops->tear_down_all_pipelines(sdev, true);
2241 			if (ret < 0) {
2242 				dev_err(sdev->dev, "Failed to tear down topology pipelines: %d\n",
2243 					ret);
2244 				return ret;
2245 			}
2246 		}
2247 	}
2248 
2249 	/* set up static pipelines */
2250 	if (tplg_ops && tplg_ops->set_up_all_pipelines)
2251 		return tplg_ops->set_up_all_pipelines(sdev, false);
2252 
2253 	return 0;
2254 }
2255 
2256 /* manifest - optional to inform component of manifest */
2257 static int sof_manifest(struct snd_soc_component *scomp, int index,
2258 			struct snd_soc_tplg_manifest *man)
2259 {
2260 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2261 	const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
2262 
2263 	if (tplg_ops && tplg_ops->parse_manifest)
2264 		return tplg_ops->parse_manifest(scomp, index, man);
2265 
2266 	return 0;
2267 }
2268 
2269 /* vendor specific kcontrol handlers available for binding */
2270 static const struct snd_soc_tplg_kcontrol_ops sof_io_ops[] = {
2271 	{SOF_TPLG_KCTL_VOL_ID, snd_sof_volume_get, snd_sof_volume_put},
2272 	{SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_get, snd_sof_bytes_put},
2273 	{SOF_TPLG_KCTL_ENUM_ID, snd_sof_enum_get, snd_sof_enum_put},
2274 	{SOF_TPLG_KCTL_SWITCH_ID, snd_sof_switch_get, snd_sof_switch_put},
2275 };
2276 
2277 /* vendor specific bytes ext handlers available for binding */
2278 static const struct snd_soc_tplg_bytes_ext_ops sof_bytes_ext_ops[] = {
2279 	{SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_ext_get, snd_sof_bytes_ext_put},
2280 	{SOF_TPLG_KCTL_BYTES_VOLATILE_RO, snd_sof_bytes_ext_volatile_get},
2281 };
2282 
2283 static const struct snd_soc_tplg_ops sof_tplg_ops = {
2284 	/* external kcontrol init - used for any driver specific init */
2285 	.control_load	= sof_control_load,
2286 	.control_unload	= sof_control_unload,
2287 
2288 	/* external kcontrol init - used for any driver specific init */
2289 	.dapm_route_load	= sof_route_load,
2290 	.dapm_route_unload	= sof_route_unload,
2291 
2292 	/* external widget init - used for any driver specific init */
2293 	/* .widget_load is not currently used */
2294 	.widget_ready	= sof_widget_ready,
2295 	.widget_unload	= sof_widget_unload,
2296 
2297 	/* FE DAI - used for any driver specific init */
2298 	.dai_load	= sof_dai_load,
2299 	.dai_unload	= sof_dai_unload,
2300 
2301 	/* DAI link - used for any driver specific init */
2302 	.link_load	= sof_link_load,
2303 	.link_unload	= sof_link_unload,
2304 
2305 	/* completion - called at completion of firmware loading */
2306 	.complete	= sof_complete,
2307 
2308 	/* manifest - optional to inform component of manifest */
2309 	.manifest	= sof_manifest,
2310 
2311 	/* vendor specific kcontrol handlers available for binding */
2312 	.io_ops		= sof_io_ops,
2313 	.io_ops_count	= ARRAY_SIZE(sof_io_ops),
2314 
2315 	/* vendor specific bytes ext handlers available for binding */
2316 	.bytes_ext_ops	= sof_bytes_ext_ops,
2317 	.bytes_ext_ops_count	= ARRAY_SIZE(sof_bytes_ext_ops),
2318 };
2319 
2320 static int snd_sof_dspless_kcontrol(struct snd_kcontrol *kcontrol,
2321 				    struct snd_ctl_elem_value *ucontrol)
2322 {
2323 	return 0;
2324 }
2325 
2326 static const struct snd_soc_tplg_kcontrol_ops sof_dspless_io_ops[] = {
2327 	{SOF_TPLG_KCTL_VOL_ID, snd_sof_dspless_kcontrol, snd_sof_dspless_kcontrol},
2328 	{SOF_TPLG_KCTL_BYTES_ID, snd_sof_dspless_kcontrol, snd_sof_dspless_kcontrol},
2329 	{SOF_TPLG_KCTL_ENUM_ID, snd_sof_dspless_kcontrol, snd_sof_dspless_kcontrol},
2330 	{SOF_TPLG_KCTL_SWITCH_ID, snd_sof_dspless_kcontrol, snd_sof_dspless_kcontrol},
2331 };
2332 
2333 static int snd_sof_dspless_bytes_ext_get(struct snd_kcontrol *kcontrol,
2334 					 unsigned int __user *binary_data,
2335 					 unsigned int size)
2336 {
2337 	return 0;
2338 }
2339 
2340 static int snd_sof_dspless_bytes_ext_put(struct snd_kcontrol *kcontrol,
2341 					 const unsigned int __user *binary_data,
2342 					 unsigned int size)
2343 {
2344 	return 0;
2345 }
2346 
2347 static const struct snd_soc_tplg_bytes_ext_ops sof_dspless_bytes_ext_ops[] = {
2348 	{SOF_TPLG_KCTL_BYTES_ID, snd_sof_dspless_bytes_ext_get, snd_sof_dspless_bytes_ext_put},
2349 	{SOF_TPLG_KCTL_BYTES_VOLATILE_RO, snd_sof_dspless_bytes_ext_get},
2350 };
2351 
2352 /* external widget init - used for any driver specific init */
2353 static int sof_dspless_widget_ready(struct snd_soc_component *scomp, int index,
2354 				    struct snd_soc_dapm_widget *w,
2355 				    struct snd_soc_tplg_dapm_widget *tw)
2356 {
2357 	if (WIDGET_IS_DAI(w->id)) {
2358 		static const struct sof_topology_token dai_tokens[] = {
2359 			{SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type, 0}};
2360 		struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2361 		struct snd_soc_tplg_private *priv = &tw->priv;
2362 		struct snd_sof_widget *swidget;
2363 		struct snd_sof_dai *sdai;
2364 		int ret;
2365 
2366 		swidget = kzalloc(sizeof(*swidget), GFP_KERNEL);
2367 		if (!swidget)
2368 			return -ENOMEM;
2369 
2370 		sdai = kzalloc(sizeof(*sdai), GFP_KERNEL);
2371 		if (!sdai) {
2372 			kfree(swidget);
2373 			return -ENOMEM;
2374 		}
2375 
2376 		ret = sof_parse_tokens(scomp, &sdai->type, dai_tokens, ARRAY_SIZE(dai_tokens),
2377 				       priv->array, le32_to_cpu(priv->size));
2378 		if (ret < 0) {
2379 			dev_err(scomp->dev, "Failed to parse DAI tokens for %s\n", tw->name);
2380 			kfree(swidget);
2381 			kfree(sdai);
2382 			return ret;
2383 		}
2384 
2385 		ret = sof_connect_dai_widget(scomp, w, tw, sdai);
2386 		if (ret) {
2387 			kfree(swidget);
2388 			kfree(sdai);
2389 			return ret;
2390 		}
2391 
2392 		swidget->scomp = scomp;
2393 		swidget->widget = w;
2394 		swidget->private = sdai;
2395 		mutex_init(&swidget->setup_mutex);
2396 		w->dobj.private = swidget;
2397 		list_add(&swidget->list, &sdev->widget_list);
2398 	}
2399 
2400 	return 0;
2401 }
2402 
2403 static int sof_dspless_widget_unload(struct snd_soc_component *scomp,
2404 				     struct snd_soc_dobj *dobj)
2405 {
2406 	struct snd_soc_dapm_widget *w = container_of(dobj, struct snd_soc_dapm_widget, dobj);
2407 
2408 	if (WIDGET_IS_DAI(w->id)) {
2409 		struct snd_sof_widget *swidget = dobj->private;
2410 
2411 		sof_disconnect_dai_widget(scomp, w);
2412 
2413 		if (!swidget)
2414 			return 0;
2415 
2416 		/* remove and free swidget object */
2417 		list_del(&swidget->list);
2418 		kfree(swidget->private);
2419 		kfree(swidget);
2420 	}
2421 
2422 	return 0;
2423 }
2424 
2425 static int sof_dspless_link_load(struct snd_soc_component *scomp, int index,
2426 				 struct snd_soc_dai_link *link,
2427 				 struct snd_soc_tplg_link_config *cfg)
2428 {
2429 	link->platforms->name = dev_name(scomp->dev);
2430 
2431 	/* Set nonatomic property for FE dai links for FE-BE compatibility */
2432 	if (!link->no_pcm)
2433 		link->nonatomic = true;
2434 
2435 	return 0;
2436 }
2437 
2438 static const struct snd_soc_tplg_ops sof_dspless_tplg_ops = {
2439 	/* external widget init - used for any driver specific init */
2440 	.widget_ready	= sof_dspless_widget_ready,
2441 	.widget_unload	= sof_dspless_widget_unload,
2442 
2443 	/* FE DAI - used for any driver specific init */
2444 	.dai_load	= sof_dai_load,
2445 	.dai_unload	= sof_dai_unload,
2446 
2447 	/* DAI link - used for any driver specific init */
2448 	.link_load	= sof_dspless_link_load,
2449 
2450 	/* vendor specific kcontrol handlers available for binding */
2451 	.io_ops		= sof_dspless_io_ops,
2452 	.io_ops_count	= ARRAY_SIZE(sof_dspless_io_ops),
2453 
2454 	/* vendor specific bytes ext handlers available for binding */
2455 	.bytes_ext_ops = sof_dspless_bytes_ext_ops,
2456 	.bytes_ext_ops_count = ARRAY_SIZE(sof_dspless_bytes_ext_ops),
2457 };
2458 
2459 int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file)
2460 {
2461 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2462 	const struct firmware *fw;
2463 	int ret;
2464 
2465 	dev_dbg(scomp->dev, "loading topology:%s\n", file);
2466 
2467 	ret = request_firmware(&fw, file, scomp->dev);
2468 	if (ret < 0) {
2469 		dev_err(scomp->dev, "error: tplg request firmware %s failed err: %d\n",
2470 			file, ret);
2471 		dev_err(scomp->dev,
2472 			"you may need to download the firmware from https://github.com/thesofproject/sof-bin/\n");
2473 		return ret;
2474 	}
2475 
2476 	if (sdev->dspless_mode_selected)
2477 		ret = snd_soc_tplg_component_load(scomp, &sof_dspless_tplg_ops, fw);
2478 	else
2479 		ret = snd_soc_tplg_component_load(scomp, &sof_tplg_ops, fw);
2480 
2481 	if (ret < 0) {
2482 		dev_err(scomp->dev, "error: tplg component load failed %d\n",
2483 			ret);
2484 		ret = -EINVAL;
2485 	}
2486 
2487 	release_firmware(fw);
2488 
2489 	if (ret >= 0 && sdev->led_present)
2490 		ret = snd_ctl_led_request();
2491 
2492 	return ret;
2493 }
2494 EXPORT_SYMBOL(snd_sof_load_topology);
2495