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