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