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