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