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