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