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