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