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