xref: /openbmc/linux/sound/soc/sof/topology.c (revision 5e2421ce)
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  * Volume fractional word length define to 16 sets
33  * the volume linear gain value to use Qx.16 format
34  */
35 #define VOLUME_FWL	16
36 /* 0.5 dB step value in topology TLV */
37 #define VOL_HALF_DB_STEP	50
38 /* Full volume for default values */
39 #define VOL_ZERO_DB	BIT(VOLUME_FWL)
40 
41 /* TLV data items */
42 #define TLV_ITEMS	3
43 #define TLV_MIN		0
44 #define TLV_STEP	1
45 #define TLV_MUTE	2
46 
47 /* size of tplg abi in byte */
48 #define SOF_TPLG_ABI_SIZE 3
49 
50 struct sof_widget_data {
51 	int ctrl_type;
52 	int ipc_cmd;
53 	struct sof_abi_hdr *pdata;
54 	struct snd_sof_control *control;
55 };
56 
57 /* send pcm params ipc */
58 static int ipc_pcm_params(struct snd_sof_widget *swidget, int dir)
59 {
60 	struct sof_ipc_pcm_params_reply ipc_params_reply;
61 	struct snd_soc_component *scomp = swidget->scomp;
62 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
63 	struct sof_ipc_pcm_params pcm;
64 	struct snd_pcm_hw_params *params;
65 	struct snd_sof_pcm *spcm;
66 	int ret;
67 
68 	memset(&pcm, 0, sizeof(pcm));
69 
70 	/* get runtime PCM params using widget's stream name */
71 	spcm = snd_sof_find_spcm_name(scomp, swidget->widget->sname);
72 	if (!spcm) {
73 		dev_err(scomp->dev, "error: cannot find PCM for %s\n",
74 			swidget->widget->name);
75 		return -EINVAL;
76 	}
77 
78 	params = &spcm->params[dir];
79 
80 	/* set IPC PCM params */
81 	pcm.hdr.size = sizeof(pcm);
82 	pcm.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS;
83 	pcm.comp_id = swidget->comp_id;
84 	pcm.params.hdr.size = sizeof(pcm.params);
85 	pcm.params.direction = dir;
86 	pcm.params.sample_valid_bytes = params_width(params) >> 3;
87 	pcm.params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED;
88 	pcm.params.rate = params_rate(params);
89 	pcm.params.channels = params_channels(params);
90 	pcm.params.host_period_bytes = params_period_bytes(params);
91 
92 	/* set format */
93 	switch (params_format(params)) {
94 	case SNDRV_PCM_FORMAT_S16:
95 		pcm.params.frame_fmt = SOF_IPC_FRAME_S16_LE;
96 		break;
97 	case SNDRV_PCM_FORMAT_S24:
98 		pcm.params.frame_fmt = SOF_IPC_FRAME_S24_4LE;
99 		break;
100 	case SNDRV_PCM_FORMAT_S32:
101 		pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE;
102 		break;
103 	default:
104 		return -EINVAL;
105 	}
106 
107 	/* send IPC to the DSP */
108 	ret = sof_ipc_tx_message(sdev->ipc, pcm.hdr.cmd, &pcm, sizeof(pcm),
109 				 &ipc_params_reply, sizeof(ipc_params_reply));
110 	if (ret < 0)
111 		dev_err(scomp->dev, "error: pcm params failed for %s\n",
112 			swidget->widget->name);
113 
114 	return ret;
115 }
116 
117  /* send stream trigger ipc */
118 static int ipc_trigger(struct snd_sof_widget *swidget, int cmd)
119 {
120 	struct snd_soc_component *scomp = swidget->scomp;
121 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
122 	struct sof_ipc_stream stream;
123 	struct sof_ipc_reply reply;
124 	int ret;
125 
126 	/* set IPC stream params */
127 	stream.hdr.size = sizeof(stream);
128 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | cmd;
129 	stream.comp_id = swidget->comp_id;
130 
131 	/* send IPC to the DSP */
132 	ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
133 				 sizeof(stream), &reply, sizeof(reply));
134 	if (ret < 0)
135 		dev_err(scomp->dev, "error: failed to trigger %s\n",
136 			swidget->widget->name);
137 
138 	return ret;
139 }
140 
141 static int sof_keyword_dapm_event(struct snd_soc_dapm_widget *w,
142 				  struct snd_kcontrol *k, int event)
143 {
144 	struct snd_sof_widget *swidget = w->dobj.private;
145 	struct snd_soc_component *scomp;
146 	int stream = SNDRV_PCM_STREAM_CAPTURE;
147 	struct snd_sof_pcm *spcm;
148 	int ret = 0;
149 
150 	if (!swidget)
151 		return 0;
152 
153 	scomp = swidget->scomp;
154 
155 	dev_dbg(scomp->dev, "received event %d for widget %s\n",
156 		event, w->name);
157 
158 	/* get runtime PCM params using widget's stream name */
159 	spcm = snd_sof_find_spcm_name(scomp, swidget->widget->sname);
160 	if (!spcm) {
161 		dev_err(scomp->dev, "error: cannot find PCM for %s\n",
162 			swidget->widget->name);
163 		return -EINVAL;
164 	}
165 
166 	/* process events */
167 	switch (event) {
168 	case SND_SOC_DAPM_PRE_PMU:
169 		if (spcm->stream[stream].suspend_ignored) {
170 			dev_dbg(scomp->dev, "PRE_PMU event ignored, KWD pipeline is already RUNNING\n");
171 			return 0;
172 		}
173 
174 		/* set pcm params */
175 		ret = ipc_pcm_params(swidget, stream);
176 		if (ret < 0) {
177 			dev_err(scomp->dev,
178 				"error: failed to set pcm params for widget %s\n",
179 				swidget->widget->name);
180 			break;
181 		}
182 
183 		/* start trigger */
184 		ret = ipc_trigger(swidget, SOF_IPC_STREAM_TRIG_START);
185 		if (ret < 0)
186 			dev_err(scomp->dev,
187 				"error: failed to trigger widget %s\n",
188 				swidget->widget->name);
189 		break;
190 	case SND_SOC_DAPM_POST_PMD:
191 		if (spcm->stream[stream].suspend_ignored) {
192 			dev_dbg(scomp->dev, "POST_PMD even ignored, KWD pipeline will remain RUNNING\n");
193 			return 0;
194 		}
195 
196 		/* stop trigger */
197 		ret = ipc_trigger(swidget, SOF_IPC_STREAM_TRIG_STOP);
198 		if (ret < 0)
199 			dev_err(scomp->dev,
200 				"error: failed to trigger widget %s\n",
201 				swidget->widget->name);
202 
203 		/* pcm free */
204 		ret = ipc_trigger(swidget, SOF_IPC_STREAM_PCM_FREE);
205 		if (ret < 0)
206 			dev_err(scomp->dev,
207 				"error: failed to trigger widget %s\n",
208 				swidget->widget->name);
209 		break;
210 	default:
211 		break;
212 	}
213 
214 	return ret;
215 }
216 
217 /* event handlers for keyword detect component */
218 static const struct snd_soc_tplg_widget_events sof_kwd_events[] = {
219 	{SOF_KEYWORD_DETECT_DAPM_EVENT, sof_keyword_dapm_event},
220 };
221 
222 static inline int get_tlv_data(const int *p, int tlv[TLV_ITEMS])
223 {
224 	/* we only support dB scale TLV type at the moment */
225 	if ((int)p[SNDRV_CTL_TLVO_TYPE] != SNDRV_CTL_TLVT_DB_SCALE)
226 		return -EINVAL;
227 
228 	/* min value in topology tlv data is multiplied by 100 */
229 	tlv[TLV_MIN] = (int)p[SNDRV_CTL_TLVO_DB_SCALE_MIN] / 100;
230 
231 	/* volume steps */
232 	tlv[TLV_STEP] = (int)(p[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] &
233 				TLV_DB_SCALE_MASK);
234 
235 	/* mute ON/OFF */
236 	if ((p[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] &
237 		TLV_DB_SCALE_MUTE) == 0)
238 		tlv[TLV_MUTE] = 0;
239 	else
240 		tlv[TLV_MUTE] = 1;
241 
242 	return 0;
243 }
244 
245 /*
246  * Function to truncate an unsigned 64-bit number
247  * by x bits and return 32-bit unsigned number. This
248  * function also takes care of rounding while truncating
249  */
250 static inline u32 vol_shift_64(u64 i, u32 x)
251 {
252 	/* do not truncate more than 32 bits */
253 	if (x > 32)
254 		x = 32;
255 
256 	if (x == 0)
257 		return (u32)i;
258 
259 	return (u32)(((i >> (x - 1)) + 1) >> 1);
260 }
261 
262 /*
263  * Function to compute a ^ exp where,
264  * a is a fractional number represented by a fixed-point
265  * integer with a fractional world length of "fwl"
266  * exp is an integer
267  * fwl is the fractional word length
268  * Return value is a fractional number represented by a
269  * fixed-point integer with a fractional word length of "fwl"
270  */
271 static u32 vol_pow32(u32 a, int exp, u32 fwl)
272 {
273 	int i, iter;
274 	u32 power = 1 << fwl;
275 	u64 numerator;
276 
277 	/* if exponent is 0, return 1 */
278 	if (exp == 0)
279 		return power;
280 
281 	/* determine the number of iterations based on the exponent */
282 	if (exp < 0)
283 		iter = exp * -1;
284 	else
285 		iter = exp;
286 
287 	/* mutiply a "iter" times to compute power */
288 	for (i = 0; i < iter; i++) {
289 		/*
290 		 * Product of 2 Qx.fwl fixed-point numbers yields a Q2*x.2*fwl
291 		 * Truncate product back to fwl fractional bits with rounding
292 		 */
293 		power = vol_shift_64((u64)power * a, fwl);
294 	}
295 
296 	if (exp > 0) {
297 		/* if exp is positive, return the result */
298 		return power;
299 	}
300 
301 	/* if exp is negative, return the multiplicative inverse */
302 	numerator = (u64)1 << (fwl << 1);
303 	do_div(numerator, power);
304 
305 	return (u32)numerator;
306 }
307 
308 /*
309  * Function to calculate volume gain from TLV data.
310  * This function can only handle gain steps that are multiples of 0.5 dB
311  */
312 static u32 vol_compute_gain(u32 value, int *tlv)
313 {
314 	int dB_gain;
315 	u32 linear_gain;
316 	int f_step;
317 
318 	/* mute volume */
319 	if (value == 0 && tlv[TLV_MUTE])
320 		return 0;
321 
322 	/*
323 	 * compute dB gain from tlv. tlv_step
324 	 * in topology is multiplied by 100
325 	 */
326 	dB_gain = tlv[TLV_MIN] + (value * tlv[TLV_STEP]) / 100;
327 
328 	/*
329 	 * compute linear gain represented by fixed-point
330 	 * int with VOLUME_FWL fractional bits
331 	 */
332 	linear_gain = vol_pow32(VOL_TWENTIETH_ROOT_OF_TEN, dB_gain, VOLUME_FWL);
333 
334 	/* extract the fractional part of volume step */
335 	f_step = tlv[TLV_STEP] - (tlv[TLV_STEP] / 100);
336 
337 	/* if volume step is an odd multiple of 0.5 dB */
338 	if (f_step == VOL_HALF_DB_STEP && (value & 1))
339 		linear_gain = vol_shift_64((u64)linear_gain *
340 						  VOL_FORTIETH_ROOT_OF_TEN,
341 						  VOLUME_FWL);
342 
343 	return linear_gain;
344 }
345 
346 /*
347  * Set up volume table for kcontrols from tlv data
348  * "size" specifies the number of entries in the table
349  */
350 static int set_up_volume_table(struct snd_sof_control *scontrol,
351 			       int tlv[TLV_ITEMS], int size)
352 {
353 	int j;
354 
355 	/* init the volume table */
356 	scontrol->volume_table = kcalloc(size, sizeof(u32), GFP_KERNEL);
357 	if (!scontrol->volume_table)
358 		return -ENOMEM;
359 
360 	/* populate the volume table */
361 	for (j = 0; j < size ; j++)
362 		scontrol->volume_table[j] = vol_compute_gain(j, tlv);
363 
364 	return 0;
365 }
366 
367 struct sof_dai_types {
368 	const char *name;
369 	enum sof_ipc_dai_type type;
370 };
371 
372 static const struct sof_dai_types sof_dais[] = {
373 	{"SSP", SOF_DAI_INTEL_SSP},
374 	{"HDA", SOF_DAI_INTEL_HDA},
375 	{"DMIC", SOF_DAI_INTEL_DMIC},
376 	{"ALH", SOF_DAI_INTEL_ALH},
377 	{"SAI", SOF_DAI_IMX_SAI},
378 	{"ESAI", SOF_DAI_IMX_ESAI},
379 	{"ACP", SOF_DAI_AMD_BT},
380 	{"ACPSP", SOF_DAI_AMD_SP},
381 	{"ACPDMIC", SOF_DAI_AMD_DMIC},
382 	{"AFE", SOF_DAI_MEDIATEK_AFE},
383 };
384 
385 static enum sof_ipc_dai_type find_dai(const char *name)
386 {
387 	int i;
388 
389 	for (i = 0; i < ARRAY_SIZE(sof_dais); i++) {
390 		if (strcmp(name, sof_dais[i].name) == 0)
391 			return sof_dais[i].type;
392 	}
393 
394 	return SOF_DAI_INTEL_NONE;
395 }
396 
397 /*
398  * Supported Frame format types and lookup, add new ones to end of list.
399  */
400 
401 struct sof_frame_types {
402 	const char *name;
403 	enum sof_ipc_frame frame;
404 };
405 
406 static const struct sof_frame_types sof_frames[] = {
407 	{"s16le", SOF_IPC_FRAME_S16_LE},
408 	{"s24le", SOF_IPC_FRAME_S24_4LE},
409 	{"s32le", SOF_IPC_FRAME_S32_LE},
410 	{"float", SOF_IPC_FRAME_FLOAT},
411 };
412 
413 static enum sof_ipc_frame find_format(const char *name)
414 {
415 	int i;
416 
417 	for (i = 0; i < ARRAY_SIZE(sof_frames); i++) {
418 		if (strcmp(name, sof_frames[i].name) == 0)
419 			return sof_frames[i].frame;
420 	}
421 
422 	/* use s32le if nothing is specified */
423 	return SOF_IPC_FRAME_S32_LE;
424 }
425 
426 struct sof_process_types {
427 	const char *name;
428 	enum sof_ipc_process_type type;
429 	enum sof_comp_type comp_type;
430 };
431 
432 static const struct sof_process_types sof_process[] = {
433 	{"EQFIR", SOF_PROCESS_EQFIR, SOF_COMP_EQ_FIR},
434 	{"EQIIR", SOF_PROCESS_EQIIR, SOF_COMP_EQ_IIR},
435 	{"KEYWORD_DETECT", SOF_PROCESS_KEYWORD_DETECT, SOF_COMP_KEYWORD_DETECT},
436 	{"KPB", SOF_PROCESS_KPB, SOF_COMP_KPB},
437 	{"CHAN_SELECTOR", SOF_PROCESS_CHAN_SELECTOR, SOF_COMP_SELECTOR},
438 	{"MUX", SOF_PROCESS_MUX, SOF_COMP_MUX},
439 	{"DEMUX", SOF_PROCESS_DEMUX, SOF_COMP_DEMUX},
440 	{"DCBLOCK", SOF_PROCESS_DCBLOCK, SOF_COMP_DCBLOCK},
441 	{"SMART_AMP", SOF_PROCESS_SMART_AMP, SOF_COMP_SMART_AMP},
442 };
443 
444 static enum sof_ipc_process_type find_process(const char *name)
445 {
446 	int i;
447 
448 	for (i = 0; i < ARRAY_SIZE(sof_process); i++) {
449 		if (strcmp(name, sof_process[i].name) == 0)
450 			return sof_process[i].type;
451 	}
452 
453 	return SOF_PROCESS_NONE;
454 }
455 
456 static enum sof_comp_type find_process_comp_type(enum sof_ipc_process_type type)
457 {
458 	int i;
459 
460 	for (i = 0; i < ARRAY_SIZE(sof_process); i++) {
461 		if (sof_process[i].type == type)
462 			return sof_process[i].comp_type;
463 	}
464 
465 	return SOF_COMP_NONE;
466 }
467 
468 /*
469  * Topology Token Parsing.
470  * New tokens should be added to headers and parsing tables below.
471  */
472 
473 struct sof_topology_token {
474 	u32 token;
475 	u32 type;
476 	int (*get_token)(void *elem, void *object, u32 offset, u32 size);
477 	u32 offset;
478 	u32 size;
479 };
480 
481 static int get_token_u32(void *elem, void *object, u32 offset, u32 size)
482 {
483 	struct snd_soc_tplg_vendor_value_elem *velem = elem;
484 	u32 *val = (u32 *)((u8 *)object + offset);
485 
486 	*val = le32_to_cpu(velem->value);
487 	return 0;
488 }
489 
490 static int get_token_u16(void *elem, void *object, u32 offset, u32 size)
491 {
492 	struct snd_soc_tplg_vendor_value_elem *velem = elem;
493 	u16 *val = (u16 *)((u8 *)object + offset);
494 
495 	*val = (u16)le32_to_cpu(velem->value);
496 	return 0;
497 }
498 
499 static int get_token_uuid(void *elem, void *object, u32 offset, u32 size)
500 {
501 	struct snd_soc_tplg_vendor_uuid_elem *velem = elem;
502 	u8 *dst = (u8 *)object + offset;
503 
504 	memcpy(dst, velem->uuid, UUID_SIZE);
505 
506 	return 0;
507 }
508 
509 static int get_token_comp_format(void *elem, void *object, u32 offset, u32 size)
510 {
511 	struct snd_soc_tplg_vendor_string_elem *velem = elem;
512 	u32 *val = (u32 *)((u8 *)object + offset);
513 
514 	*val = find_format(velem->string);
515 	return 0;
516 }
517 
518 static int get_token_dai_type(void *elem, void *object, u32 offset, u32 size)
519 {
520 	struct snd_soc_tplg_vendor_string_elem *velem = elem;
521 	u32 *val = (u32 *)((u8 *)object + offset);
522 
523 	*val = find_dai(velem->string);
524 	return 0;
525 }
526 
527 static int get_token_process_type(void *elem, void *object, u32 offset,
528 				  u32 size)
529 {
530 	struct snd_soc_tplg_vendor_string_elem *velem = elem;
531 	u32 *val = (u32 *)((u8 *)object + offset);
532 
533 	*val = find_process(velem->string);
534 	return 0;
535 }
536 
537 /* Buffers */
538 static const struct sof_topology_token buffer_tokens[] = {
539 	{SOF_TKN_BUF_SIZE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
540 		offsetof(struct sof_ipc_buffer, size), 0},
541 	{SOF_TKN_BUF_CAPS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
542 		offsetof(struct sof_ipc_buffer, caps), 0},
543 };
544 
545 /* DAI */
546 static const struct sof_topology_token dai_tokens[] = {
547 	{SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type,
548 		offsetof(struct sof_ipc_comp_dai, type), 0},
549 	{SOF_TKN_DAI_INDEX, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
550 		offsetof(struct sof_ipc_comp_dai, dai_index), 0},
551 	{SOF_TKN_DAI_DIRECTION, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
552 		offsetof(struct sof_ipc_comp_dai, direction), 0},
553 };
554 
555 /* BE DAI link */
556 static const struct sof_topology_token dai_link_tokens[] = {
557 	{SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type,
558 		offsetof(struct sof_ipc_dai_config, type), 0},
559 	{SOF_TKN_DAI_INDEX, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
560 		offsetof(struct sof_ipc_dai_config, dai_index), 0},
561 };
562 
563 /* scheduling */
564 static const struct sof_topology_token sched_tokens[] = {
565 	{SOF_TKN_SCHED_PERIOD, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
566 		offsetof(struct sof_ipc_pipe_new, period), 0},
567 	{SOF_TKN_SCHED_PRIORITY, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
568 		offsetof(struct sof_ipc_pipe_new, priority), 0},
569 	{SOF_TKN_SCHED_MIPS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
570 		offsetof(struct sof_ipc_pipe_new, period_mips), 0},
571 	{SOF_TKN_SCHED_CORE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
572 		offsetof(struct sof_ipc_pipe_new, core), 0},
573 	{SOF_TKN_SCHED_FRAMES, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
574 		offsetof(struct sof_ipc_pipe_new, frames_per_sched), 0},
575 	{SOF_TKN_SCHED_TIME_DOMAIN, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
576 		offsetof(struct sof_ipc_pipe_new, time_domain), 0},
577 };
578 
579 static const struct sof_topology_token pipeline_tokens[] = {
580 	{SOF_TKN_SCHED_DYNAMIC_PIPELINE, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
581 		offsetof(struct snd_sof_widget, dynamic_pipeline_widget), 0},
582 
583 };
584 
585 /* volume */
586 static const struct sof_topology_token volume_tokens[] = {
587 	{SOF_TKN_VOLUME_RAMP_STEP_TYPE, SND_SOC_TPLG_TUPLE_TYPE_WORD,
588 		get_token_u32, offsetof(struct sof_ipc_comp_volume, ramp), 0},
589 	{SOF_TKN_VOLUME_RAMP_STEP_MS,
590 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
591 		offsetof(struct sof_ipc_comp_volume, initial_ramp), 0},
592 };
593 
594 /* SRC */
595 static const struct sof_topology_token src_tokens[] = {
596 	{SOF_TKN_SRC_RATE_IN, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
597 		offsetof(struct sof_ipc_comp_src, source_rate), 0},
598 	{SOF_TKN_SRC_RATE_OUT, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
599 		offsetof(struct sof_ipc_comp_src, sink_rate), 0},
600 };
601 
602 /* ASRC */
603 static const struct sof_topology_token asrc_tokens[] = {
604 	{SOF_TKN_ASRC_RATE_IN, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
605 		offsetof(struct sof_ipc_comp_asrc, source_rate), 0},
606 	{SOF_TKN_ASRC_RATE_OUT, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
607 		offsetof(struct sof_ipc_comp_asrc, sink_rate), 0},
608 	{SOF_TKN_ASRC_ASYNCHRONOUS_MODE, SND_SOC_TPLG_TUPLE_TYPE_WORD,
609 		get_token_u32,
610 		offsetof(struct sof_ipc_comp_asrc, asynchronous_mode), 0},
611 	{SOF_TKN_ASRC_OPERATION_MODE, SND_SOC_TPLG_TUPLE_TYPE_WORD,
612 		get_token_u32,
613 		offsetof(struct sof_ipc_comp_asrc, operation_mode), 0},
614 };
615 
616 /* Tone */
617 static const struct sof_topology_token tone_tokens[] = {
618 };
619 
620 /* EFFECT */
621 static const struct sof_topology_token process_tokens[] = {
622 	{SOF_TKN_PROCESS_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING,
623 		get_token_process_type,
624 		offsetof(struct sof_ipc_comp_process, type), 0},
625 };
626 
627 /* PCM */
628 static const struct sof_topology_token pcm_tokens[] = {
629 	{SOF_TKN_PCM_DMAC_CONFIG, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
630 		offsetof(struct sof_ipc_comp_host, dmac_config), 0},
631 };
632 
633 /* PCM */
634 static const struct sof_topology_token stream_tokens[] = {
635 	{SOF_TKN_STREAM_PLAYBACK_COMPATIBLE_D0I3,
636 		SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
637 		offsetof(struct snd_sof_pcm, stream[0].d0i3_compatible), 0},
638 	{SOF_TKN_STREAM_CAPTURE_COMPATIBLE_D0I3,
639 		SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
640 		offsetof(struct snd_sof_pcm, stream[1].d0i3_compatible), 0},
641 };
642 
643 /* Generic components */
644 static const struct sof_topology_token comp_tokens[] = {
645 	{SOF_TKN_COMP_PERIOD_SINK_COUNT,
646 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
647 		offsetof(struct sof_ipc_comp_config, periods_sink), 0},
648 	{SOF_TKN_COMP_PERIOD_SOURCE_COUNT,
649 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
650 		offsetof(struct sof_ipc_comp_config, periods_source), 0},
651 	{SOF_TKN_COMP_FORMAT,
652 		SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_comp_format,
653 		offsetof(struct sof_ipc_comp_config, frame_fmt), 0},
654 };
655 
656 /* SSP */
657 static const struct sof_topology_token ssp_tokens[] = {
658 	{SOF_TKN_INTEL_SSP_CLKS_CONTROL,
659 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
660 		offsetof(struct sof_ipc_dai_ssp_params, clks_control), 0},
661 	{SOF_TKN_INTEL_SSP_MCLK_ID,
662 		SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
663 		offsetof(struct sof_ipc_dai_ssp_params, mclk_id), 0},
664 	{SOF_TKN_INTEL_SSP_SAMPLE_BITS, SND_SOC_TPLG_TUPLE_TYPE_WORD,
665 		get_token_u32,
666 		offsetof(struct sof_ipc_dai_ssp_params, sample_valid_bits), 0},
667 	{SOF_TKN_INTEL_SSP_FRAME_PULSE_WIDTH, SND_SOC_TPLG_TUPLE_TYPE_SHORT,
668 		get_token_u16,
669 		offsetof(struct sof_ipc_dai_ssp_params, frame_pulse_width), 0},
670 	{SOF_TKN_INTEL_SSP_QUIRKS, SND_SOC_TPLG_TUPLE_TYPE_WORD,
671 		get_token_u32,
672 		offsetof(struct sof_ipc_dai_ssp_params, quirks), 0},
673 	{SOF_TKN_INTEL_SSP_TDM_PADDING_PER_SLOT, SND_SOC_TPLG_TUPLE_TYPE_BOOL,
674 		get_token_u16,
675 		offsetof(struct sof_ipc_dai_ssp_params,
676 			 tdm_per_slot_padding_flag), 0},
677 	{SOF_TKN_INTEL_SSP_BCLK_DELAY, SND_SOC_TPLG_TUPLE_TYPE_WORD,
678 		get_token_u32,
679 		offsetof(struct sof_ipc_dai_ssp_params, bclk_delay), 0},
680 
681 };
682 
683 /* ALH */
684 static const struct sof_topology_token alh_tokens[] = {
685 	{SOF_TKN_INTEL_ALH_RATE,
686 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
687 		offsetof(struct sof_ipc_dai_alh_params, rate), 0},
688 	{SOF_TKN_INTEL_ALH_CH,
689 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
690 		offsetof(struct sof_ipc_dai_alh_params, channels), 0},
691 };
692 
693 /* DMIC */
694 static const struct sof_topology_token dmic_tokens[] = {
695 	{SOF_TKN_INTEL_DMIC_DRIVER_VERSION,
696 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
697 		offsetof(struct sof_ipc_dai_dmic_params, driver_ipc_version),
698 		0},
699 	{SOF_TKN_INTEL_DMIC_CLK_MIN,
700 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
701 		offsetof(struct sof_ipc_dai_dmic_params, pdmclk_min), 0},
702 	{SOF_TKN_INTEL_DMIC_CLK_MAX,
703 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
704 		offsetof(struct sof_ipc_dai_dmic_params, pdmclk_max), 0},
705 	{SOF_TKN_INTEL_DMIC_SAMPLE_RATE,
706 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
707 		offsetof(struct sof_ipc_dai_dmic_params, fifo_fs), 0},
708 	{SOF_TKN_INTEL_DMIC_DUTY_MIN,
709 		SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
710 		offsetof(struct sof_ipc_dai_dmic_params, duty_min), 0},
711 	{SOF_TKN_INTEL_DMIC_DUTY_MAX,
712 		SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
713 		offsetof(struct sof_ipc_dai_dmic_params, duty_max), 0},
714 	{SOF_TKN_INTEL_DMIC_NUM_PDM_ACTIVE,
715 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
716 		offsetof(struct sof_ipc_dai_dmic_params,
717 			 num_pdm_active), 0},
718 	{SOF_TKN_INTEL_DMIC_FIFO_WORD_LENGTH,
719 		SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
720 		offsetof(struct sof_ipc_dai_dmic_params, fifo_bits), 0},
721 	{SOF_TKN_INTEL_DMIC_UNMUTE_RAMP_TIME_MS,
722 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
723 		offsetof(struct sof_ipc_dai_dmic_params, unmute_ramp_time), 0},
724 
725 };
726 
727 /* ESAI */
728 static const struct sof_topology_token esai_tokens[] = {
729 	{SOF_TKN_IMX_ESAI_MCLK_ID,
730 		SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
731 		offsetof(struct sof_ipc_dai_esai_params, mclk_id), 0},
732 };
733 
734 /* SAI */
735 static const struct sof_topology_token sai_tokens[] = {
736 	{SOF_TKN_IMX_SAI_MCLK_ID,
737 		SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
738 		offsetof(struct sof_ipc_dai_sai_params, mclk_id), 0},
739 };
740 
741 /* Core tokens */
742 static const struct sof_topology_token core_tokens[] = {
743 	{SOF_TKN_COMP_CORE_ID,
744 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
745 		offsetof(struct sof_ipc_comp, core), 0},
746 };
747 
748 /* Component extended tokens */
749 static const struct sof_topology_token comp_ext_tokens[] = {
750 	{SOF_TKN_COMP_UUID,
751 		SND_SOC_TPLG_TUPLE_TYPE_UUID, get_token_uuid,
752 		offsetof(struct sof_ipc_comp_ext, uuid), 0},
753 };
754 
755 /*
756  * DMIC PDM Tokens
757  * SOF_TKN_INTEL_DMIC_PDM_CTRL_ID should be the first token
758  * as it increments the index while parsing the array of pdm tokens
759  * and determines the correct offset
760  */
761 static const struct sof_topology_token dmic_pdm_tokens[] = {
762 	{SOF_TKN_INTEL_DMIC_PDM_CTRL_ID,
763 		SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
764 		offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, id),
765 		0},
766 	{SOF_TKN_INTEL_DMIC_PDM_MIC_A_Enable,
767 		SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
768 		offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, enable_mic_a),
769 		0},
770 	{SOF_TKN_INTEL_DMIC_PDM_MIC_B_Enable,
771 		SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
772 		offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, enable_mic_b),
773 		0},
774 	{SOF_TKN_INTEL_DMIC_PDM_POLARITY_A,
775 		SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
776 		offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, polarity_mic_a),
777 		0},
778 	{SOF_TKN_INTEL_DMIC_PDM_POLARITY_B,
779 		SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
780 		offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, polarity_mic_b),
781 		0},
782 	{SOF_TKN_INTEL_DMIC_PDM_CLK_EDGE,
783 		SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
784 		offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, clk_edge),
785 		0},
786 	{SOF_TKN_INTEL_DMIC_PDM_SKEW,
787 		SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
788 		offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, skew),
789 		0},
790 };
791 
792 /* HDA */
793 static const struct sof_topology_token hda_tokens[] = {
794 	{SOF_TKN_INTEL_HDA_RATE,
795 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
796 		offsetof(struct sof_ipc_dai_hda_params, rate), 0},
797 	{SOF_TKN_INTEL_HDA_CH,
798 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
799 		offsetof(struct sof_ipc_dai_hda_params, channels), 0},
800 };
801 
802 /* Leds */
803 static const struct sof_topology_token led_tokens[] = {
804 	{SOF_TKN_MUTE_LED_USE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
805 	 offsetof(struct snd_sof_led_control, use_led), 0},
806 	{SOF_TKN_MUTE_LED_DIRECTION, SND_SOC_TPLG_TUPLE_TYPE_WORD,
807 	 get_token_u32, offsetof(struct snd_sof_led_control, direction), 0},
808 };
809 
810 /* AFE */
811 static const struct sof_topology_token afe_tokens[] = {
812 	{SOF_TKN_MEDIATEK_AFE_RATE,
813 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
814 		offsetof(struct sof_ipc_dai_mtk_afe_params, rate), 0},
815 	{SOF_TKN_MEDIATEK_AFE_CH,
816 		SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
817 		offsetof(struct sof_ipc_dai_mtk_afe_params, channels), 0},
818 	{SOF_TKN_MEDIATEK_AFE_FORMAT,
819 		SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_comp_format,
820 		offsetof(struct sof_ipc_dai_mtk_afe_params, format), 0},
821 };
822 
823 static int sof_parse_uuid_tokens(struct snd_soc_component *scomp,
824 				 void *object,
825 				 const struct sof_topology_token *tokens,
826 				 int count,
827 				 struct snd_soc_tplg_vendor_array *array,
828 				 size_t offset)
829 {
830 	struct snd_soc_tplg_vendor_uuid_elem *elem;
831 	int found = 0;
832 	int i, j;
833 
834 	/* parse element by element */
835 	for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
836 		elem = &array->uuid[i];
837 
838 		/* search for token */
839 		for (j = 0; j < count; j++) {
840 			/* match token type */
841 			if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_UUID)
842 				continue;
843 
844 			/* match token id */
845 			if (tokens[j].token != le32_to_cpu(elem->token))
846 				continue;
847 
848 			/* matched - now load token */
849 			tokens[j].get_token(elem, object,
850 					    offset + tokens[j].offset,
851 					    tokens[j].size);
852 
853 			found++;
854 		}
855 	}
856 
857 	return found;
858 }
859 
860 static int sof_parse_string_tokens(struct snd_soc_component *scomp,
861 				   void *object,
862 				   const struct sof_topology_token *tokens,
863 				   int count,
864 				   struct snd_soc_tplg_vendor_array *array,
865 				   size_t offset)
866 {
867 	struct snd_soc_tplg_vendor_string_elem *elem;
868 	int found = 0;
869 	int i, j;
870 
871 	/* parse element by element */
872 	for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
873 		elem = &array->string[i];
874 
875 		/* search for token */
876 		for (j = 0; j < count; j++) {
877 			/* match token type */
878 			if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_STRING)
879 				continue;
880 
881 			/* match token id */
882 			if (tokens[j].token != le32_to_cpu(elem->token))
883 				continue;
884 
885 			/* matched - now load token */
886 			tokens[j].get_token(elem, object,
887 					    offset + tokens[j].offset,
888 					    tokens[j].size);
889 
890 			found++;
891 		}
892 	}
893 
894 	return found;
895 }
896 
897 static int sof_parse_word_tokens(struct snd_soc_component *scomp,
898 				 void *object,
899 				 const struct sof_topology_token *tokens,
900 				 int count,
901 				 struct snd_soc_tplg_vendor_array *array,
902 				 size_t offset)
903 {
904 	struct snd_soc_tplg_vendor_value_elem *elem;
905 	int found = 0;
906 	int i, j;
907 
908 	/* parse element by element */
909 	for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
910 		elem = &array->value[i];
911 
912 		/* search for token */
913 		for (j = 0; j < count; j++) {
914 			/* match token type */
915 			if (!(tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_WORD ||
916 			      tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_SHORT ||
917 			      tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BYTE ||
918 			      tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BOOL))
919 				continue;
920 
921 			/* match token id */
922 			if (tokens[j].token != le32_to_cpu(elem->token))
923 				continue;
924 
925 			/* load token */
926 			tokens[j].get_token(elem, object,
927 					    offset + tokens[j].offset,
928 					    tokens[j].size);
929 
930 			found++;
931 		}
932 	}
933 
934 	return found;
935 }
936 
937 /**
938  * sof_parse_token_sets - Parse multiple sets of tokens
939  * @scomp: pointer to soc component
940  * @object: target ipc struct for parsed values
941  * @tokens: token definition array describing what tokens to parse
942  * @count: number of tokens in definition array
943  * @array: source pointer to consecutive vendor arrays to be parsed
944  * @priv_size: total size of the consecutive source arrays
945  * @sets: number of similar token sets to be parsed, 1 set has count elements
946  * @object_size: offset to next target ipc struct with multiple sets
947  *
948  * This function parses multiple sets of tokens in vendor arrays into
949  * consecutive ipc structs.
950  */
951 static int sof_parse_token_sets(struct snd_soc_component *scomp,
952 				void *object,
953 				const struct sof_topology_token *tokens,
954 				int count,
955 				struct snd_soc_tplg_vendor_array *array,
956 				int priv_size, int sets, size_t object_size)
957 {
958 	size_t offset = 0;
959 	int found = 0;
960 	int total = 0;
961 	int asize;
962 
963 	while (priv_size > 0 && total < count * sets) {
964 		asize = le32_to_cpu(array->size);
965 
966 		/* validate asize */
967 		if (asize < 0) { /* FIXME: A zero-size array makes no sense */
968 			dev_err(scomp->dev, "error: invalid array size 0x%x\n",
969 				asize);
970 			return -EINVAL;
971 		}
972 
973 		/* make sure there is enough data before parsing */
974 		priv_size -= asize;
975 		if (priv_size < 0) {
976 			dev_err(scomp->dev, "error: invalid array size 0x%x\n",
977 				asize);
978 			return -EINVAL;
979 		}
980 
981 		/* call correct parser depending on type */
982 		switch (le32_to_cpu(array->type)) {
983 		case SND_SOC_TPLG_TUPLE_TYPE_UUID:
984 			found += sof_parse_uuid_tokens(scomp, object, tokens,
985 						       count, array, offset);
986 			break;
987 		case SND_SOC_TPLG_TUPLE_TYPE_STRING:
988 			found += sof_parse_string_tokens(scomp, object, tokens,
989 							 count, array, offset);
990 			break;
991 		case SND_SOC_TPLG_TUPLE_TYPE_BOOL:
992 		case SND_SOC_TPLG_TUPLE_TYPE_BYTE:
993 		case SND_SOC_TPLG_TUPLE_TYPE_WORD:
994 		case SND_SOC_TPLG_TUPLE_TYPE_SHORT:
995 			found += sof_parse_word_tokens(scomp, object, tokens,
996 						       count, array, offset);
997 			break;
998 		default:
999 			dev_err(scomp->dev, "error: unknown token type %d\n",
1000 				array->type);
1001 			return -EINVAL;
1002 		}
1003 
1004 		/* next array */
1005 		array = (struct snd_soc_tplg_vendor_array *)((u8 *)array
1006 			+ asize);
1007 
1008 		/* move to next target struct */
1009 		if (found >= count) {
1010 			offset += object_size;
1011 			total += found;
1012 			found = 0;
1013 		}
1014 	}
1015 
1016 	return 0;
1017 }
1018 
1019 static int sof_parse_tokens(struct snd_soc_component *scomp,
1020 			    void *object,
1021 			    const struct sof_topology_token *tokens,
1022 			    int count,
1023 			    struct snd_soc_tplg_vendor_array *array,
1024 			    int priv_size)
1025 {
1026 	/*
1027 	 * sof_parse_tokens is used when topology contains only a single set of
1028 	 * identical tuples arrays. So additional parameters to
1029 	 * sof_parse_token_sets are sets = 1 (only 1 set) and
1030 	 * object_size = 0 (irrelevant).
1031 	 */
1032 	return sof_parse_token_sets(scomp, object, tokens, count, array,
1033 				    priv_size, 1, 0);
1034 }
1035 
1036 static void sof_dbg_comp_config(struct snd_soc_component *scomp,
1037 				struct sof_ipc_comp_config *config)
1038 {
1039 	dev_dbg(scomp->dev, " config: periods snk %d src %d fmt %d\n",
1040 		config->periods_sink, config->periods_source,
1041 		config->frame_fmt);
1042 }
1043 
1044 /*
1045  * Standard Kcontrols.
1046  */
1047 
1048 static int sof_control_load_volume(struct snd_soc_component *scomp,
1049 				   struct snd_sof_control *scontrol,
1050 				   struct snd_kcontrol_new *kc,
1051 				   struct snd_soc_tplg_ctl_hdr *hdr)
1052 {
1053 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1054 	struct snd_soc_tplg_mixer_control *mc =
1055 		container_of(hdr, struct snd_soc_tplg_mixer_control, hdr);
1056 	struct sof_ipc_ctrl_data *cdata;
1057 	int tlv[TLV_ITEMS];
1058 	unsigned int i;
1059 	int ret;
1060 
1061 	/* validate topology data */
1062 	if (le32_to_cpu(mc->num_channels) > SND_SOC_TPLG_MAX_CHAN) {
1063 		ret = -EINVAL;
1064 		goto out;
1065 	}
1066 
1067 	/*
1068 	 * If control has more than 2 channels we need to override the info. This is because even if
1069 	 * ASoC layer has defined topology's max channel count to SND_SOC_TPLG_MAX_CHAN = 8, the
1070 	 * pre-defined dapm control types (and related functions) creating the actual control
1071 	 * restrict the channels only to mono or stereo.
1072 	 */
1073 	if (le32_to_cpu(mc->num_channels) > 2)
1074 		kc->info = snd_sof_volume_info;
1075 
1076 	/* init the volume get/put data */
1077 	scontrol->size = struct_size(scontrol->control_data, chanv,
1078 				     le32_to_cpu(mc->num_channels));
1079 	scontrol->control_data = kzalloc(scontrol->size, GFP_KERNEL);
1080 	if (!scontrol->control_data) {
1081 		ret = -ENOMEM;
1082 		goto out;
1083 	}
1084 
1085 	scontrol->comp_id = sdev->next_comp_id;
1086 	scontrol->min_volume_step = le32_to_cpu(mc->min);
1087 	scontrol->max_volume_step = le32_to_cpu(mc->max);
1088 	scontrol->num_channels = le32_to_cpu(mc->num_channels);
1089 	scontrol->control_data->index = kc->index;
1090 
1091 	/* set cmd for mixer control */
1092 	if (le32_to_cpu(mc->max) == 1) {
1093 		scontrol->control_data->cmd = SOF_CTRL_CMD_SWITCH;
1094 		goto skip;
1095 	}
1096 
1097 	scontrol->control_data->cmd = SOF_CTRL_CMD_VOLUME;
1098 
1099 	/* extract tlv data */
1100 	if (!kc->tlv.p || get_tlv_data(kc->tlv.p, tlv) < 0) {
1101 		dev_err(scomp->dev, "error: invalid TLV data\n");
1102 		ret = -EINVAL;
1103 		goto out_free;
1104 	}
1105 
1106 	/* set up volume table */
1107 	ret = set_up_volume_table(scontrol, tlv, le32_to_cpu(mc->max) + 1);
1108 	if (ret < 0) {
1109 		dev_err(scomp->dev, "error: setting up volume table\n");
1110 		goto out_free;
1111 	}
1112 
1113 	/* set default volume values to 0dB in control */
1114 	cdata = scontrol->control_data;
1115 	for (i = 0; i < scontrol->num_channels; i++) {
1116 		cdata->chanv[i].channel = i;
1117 		cdata->chanv[i].value = VOL_ZERO_DB;
1118 	}
1119 
1120 skip:
1121 	/* set up possible led control from mixer private data */
1122 	ret = sof_parse_tokens(scomp, &scontrol->led_ctl, led_tokens,
1123 			       ARRAY_SIZE(led_tokens), mc->priv.array,
1124 			       le32_to_cpu(mc->priv.size));
1125 	if (ret != 0) {
1126 		dev_err(scomp->dev, "error: parse led tokens failed %d\n",
1127 			le32_to_cpu(mc->priv.size));
1128 		goto out_free_table;
1129 	}
1130 
1131 	dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d\n",
1132 		scontrol->comp_id, scontrol->num_channels);
1133 
1134 	return 0;
1135 
1136 out_free_table:
1137 	if (le32_to_cpu(mc->max) > 1)
1138 		kfree(scontrol->volume_table);
1139 out_free:
1140 	kfree(scontrol->control_data);
1141 out:
1142 	return ret;
1143 }
1144 
1145 static int sof_control_load_enum(struct snd_soc_component *scomp,
1146 				 struct snd_sof_control *scontrol,
1147 				 struct snd_kcontrol_new *kc,
1148 				 struct snd_soc_tplg_ctl_hdr *hdr)
1149 {
1150 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1151 	struct snd_soc_tplg_enum_control *ec =
1152 		container_of(hdr, struct snd_soc_tplg_enum_control, hdr);
1153 
1154 	/* validate topology data */
1155 	if (le32_to_cpu(ec->num_channels) > SND_SOC_TPLG_MAX_CHAN)
1156 		return -EINVAL;
1157 
1158 	/* init the enum get/put data */
1159 	scontrol->size = struct_size(scontrol->control_data, chanv,
1160 				     le32_to_cpu(ec->num_channels));
1161 	scontrol->control_data = kzalloc(scontrol->size, GFP_KERNEL);
1162 	if (!scontrol->control_data)
1163 		return -ENOMEM;
1164 
1165 	scontrol->comp_id = sdev->next_comp_id;
1166 	scontrol->num_channels = le32_to_cpu(ec->num_channels);
1167 	scontrol->control_data->index = kc->index;
1168 	scontrol->control_data->cmd = SOF_CTRL_CMD_ENUM;
1169 
1170 	dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d comp_id %d\n",
1171 		scontrol->comp_id, scontrol->num_channels, scontrol->comp_id);
1172 
1173 	return 0;
1174 }
1175 
1176 static int sof_control_load_bytes(struct snd_soc_component *scomp,
1177 				  struct snd_sof_control *scontrol,
1178 				  struct snd_kcontrol_new *kc,
1179 				  struct snd_soc_tplg_ctl_hdr *hdr)
1180 {
1181 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1182 	struct sof_ipc_ctrl_data *cdata;
1183 	struct snd_soc_tplg_bytes_control *control =
1184 		container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
1185 	struct soc_bytes_ext *sbe = (struct soc_bytes_ext *)kc->private_value;
1186 	size_t max_size = sbe->max;
1187 	size_t priv_size = le32_to_cpu(control->priv.size);
1188 	int ret;
1189 
1190 	if (max_size < sizeof(struct sof_ipc_ctrl_data) ||
1191 	    max_size < sizeof(struct sof_abi_hdr)) {
1192 		ret = -EINVAL;
1193 		goto out;
1194 	}
1195 
1196 	/* init the get/put bytes data */
1197 	if (priv_size > max_size - sizeof(struct sof_ipc_ctrl_data)) {
1198 		dev_err(scomp->dev, "err: bytes data size %zu exceeds max %zu.\n",
1199 			priv_size, max_size - sizeof(struct sof_ipc_ctrl_data));
1200 		ret = -EINVAL;
1201 		goto out;
1202 	}
1203 
1204 	scontrol->size = sizeof(struct sof_ipc_ctrl_data) + priv_size;
1205 
1206 	scontrol->control_data = kzalloc(max_size, GFP_KERNEL);
1207 	cdata = scontrol->control_data;
1208 	if (!scontrol->control_data) {
1209 		ret = -ENOMEM;
1210 		goto out;
1211 	}
1212 
1213 	scontrol->comp_id = sdev->next_comp_id;
1214 	scontrol->control_data->cmd = SOF_CTRL_CMD_BINARY;
1215 	scontrol->control_data->index = kc->index;
1216 
1217 	dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d\n",
1218 		scontrol->comp_id, scontrol->num_channels);
1219 
1220 	if (le32_to_cpu(control->priv.size) > 0) {
1221 		memcpy(cdata->data, control->priv.data,
1222 		       le32_to_cpu(control->priv.size));
1223 
1224 		if (cdata->data->magic != SOF_ABI_MAGIC) {
1225 			dev_err(scomp->dev, "error: Wrong ABI magic 0x%08x.\n",
1226 				cdata->data->magic);
1227 			ret = -EINVAL;
1228 			goto out_free;
1229 		}
1230 		if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION,
1231 						 cdata->data->abi)) {
1232 			dev_err(scomp->dev,
1233 				"error: Incompatible ABI version 0x%08x.\n",
1234 				cdata->data->abi);
1235 			ret = -EINVAL;
1236 			goto out_free;
1237 		}
1238 		if (cdata->data->size + sizeof(struct sof_abi_hdr) !=
1239 		    le32_to_cpu(control->priv.size)) {
1240 			dev_err(scomp->dev,
1241 				"error: Conflict in bytes vs. priv size.\n");
1242 			ret = -EINVAL;
1243 			goto out_free;
1244 		}
1245 	}
1246 
1247 	return 0;
1248 
1249 out_free:
1250 	kfree(scontrol->control_data);
1251 out:
1252 	return ret;
1253 }
1254 
1255 /* external kcontrol init - used for any driver specific init */
1256 static int sof_control_load(struct snd_soc_component *scomp, int index,
1257 			    struct snd_kcontrol_new *kc,
1258 			    struct snd_soc_tplg_ctl_hdr *hdr)
1259 {
1260 	struct soc_mixer_control *sm;
1261 	struct soc_bytes_ext *sbe;
1262 	struct soc_enum *se;
1263 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1264 	struct snd_soc_dobj *dobj;
1265 	struct snd_sof_control *scontrol;
1266 	int ret;
1267 
1268 	dev_dbg(scomp->dev, "tplg: load control type %d name : %s\n",
1269 		hdr->type, hdr->name);
1270 
1271 	scontrol = kzalloc(sizeof(*scontrol), GFP_KERNEL);
1272 	if (!scontrol)
1273 		return -ENOMEM;
1274 
1275 	scontrol->scomp = scomp;
1276 	scontrol->access = kc->access;
1277 
1278 	switch (le32_to_cpu(hdr->ops.info)) {
1279 	case SND_SOC_TPLG_CTL_VOLSW:
1280 	case SND_SOC_TPLG_CTL_VOLSW_SX:
1281 	case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1282 		sm = (struct soc_mixer_control *)kc->private_value;
1283 		dobj = &sm->dobj;
1284 		ret = sof_control_load_volume(scomp, scontrol, kc, hdr);
1285 		break;
1286 	case SND_SOC_TPLG_CTL_BYTES:
1287 		sbe = (struct soc_bytes_ext *)kc->private_value;
1288 		dobj = &sbe->dobj;
1289 		ret = sof_control_load_bytes(scomp, scontrol, kc, hdr);
1290 		break;
1291 	case SND_SOC_TPLG_CTL_ENUM:
1292 	case SND_SOC_TPLG_CTL_ENUM_VALUE:
1293 		se = (struct soc_enum *)kc->private_value;
1294 		dobj = &se->dobj;
1295 		ret = sof_control_load_enum(scomp, scontrol, kc, hdr);
1296 		break;
1297 	case SND_SOC_TPLG_CTL_RANGE:
1298 	case SND_SOC_TPLG_CTL_STROBE:
1299 	case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1300 	case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1301 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1302 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1303 	case SND_SOC_TPLG_DAPM_CTL_PIN:
1304 	default:
1305 		dev_warn(scomp->dev, "control type not supported %d:%d:%d\n",
1306 			 hdr->ops.get, hdr->ops.put, hdr->ops.info);
1307 		kfree(scontrol);
1308 		return 0;
1309 	}
1310 
1311 	if (ret < 0) {
1312 		kfree(scontrol);
1313 		return ret;
1314 	}
1315 
1316 	scontrol->led_ctl.led_value = -1;
1317 
1318 	dobj->private = scontrol;
1319 	list_add(&scontrol->list, &sdev->kcontrol_list);
1320 	return 0;
1321 }
1322 
1323 static int sof_control_unload(struct snd_soc_component *scomp,
1324 			      struct snd_soc_dobj *dobj)
1325 {
1326 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1327 	struct sof_ipc_free fcomp;
1328 	struct snd_sof_control *scontrol = dobj->private;
1329 
1330 	dev_dbg(scomp->dev, "tplg: unload control name : %s\n", scomp->name);
1331 
1332 	fcomp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_FREE;
1333 	fcomp.hdr.size = sizeof(fcomp);
1334 	fcomp.id = scontrol->comp_id;
1335 
1336 	kfree(scontrol->control_data);
1337 	list_del(&scontrol->list);
1338 	kfree(scontrol);
1339 	/* send IPC to the DSP */
1340 	return sof_ipc_tx_message(sdev->ipc,
1341 				  fcomp.hdr.cmd, &fcomp, sizeof(fcomp),
1342 				  NULL, 0);
1343 }
1344 
1345 /*
1346  * DAI Topology
1347  */
1348 
1349 static int sof_connect_dai_widget(struct snd_soc_component *scomp,
1350 				  struct snd_soc_dapm_widget *w,
1351 				  struct snd_soc_tplg_dapm_widget *tw,
1352 				  struct snd_sof_dai *dai)
1353 {
1354 	struct snd_soc_card *card = scomp->card;
1355 	struct snd_soc_pcm_runtime *rtd;
1356 	struct snd_soc_dai *cpu_dai;
1357 	int i;
1358 
1359 	list_for_each_entry(rtd, &card->rtd_list, list) {
1360 		dev_vdbg(scomp->dev, "tplg: check widget: %s stream: %s dai stream: %s\n",
1361 			 w->name,  w->sname, rtd->dai_link->stream_name);
1362 
1363 		if (!w->sname || !rtd->dai_link->stream_name)
1364 			continue;
1365 
1366 		/* does stream match DAI link ? */
1367 		if (strcmp(w->sname, rtd->dai_link->stream_name))
1368 			continue;
1369 
1370 		switch (w->id) {
1371 		case snd_soc_dapm_dai_out:
1372 			for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1373 				/*
1374 				 * Please create DAI widget in the right order
1375 				 * to ensure BE will connect to the right DAI
1376 				 * widget.
1377 				 */
1378 				if (!cpu_dai->capture_widget) {
1379 					cpu_dai->capture_widget = w;
1380 					break;
1381 				}
1382 			}
1383 			if (i == rtd->num_cpus) {
1384 				dev_err(scomp->dev, "error: can't find BE for DAI %s\n",
1385 					w->name);
1386 
1387 				return -EINVAL;
1388 			}
1389 			dai->name = rtd->dai_link->name;
1390 			dev_dbg(scomp->dev, "tplg: connected widget %s -> DAI link %s\n",
1391 				w->name, rtd->dai_link->name);
1392 			break;
1393 		case snd_soc_dapm_dai_in:
1394 			for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1395 				/*
1396 				 * Please create DAI widget in the right order
1397 				 * to ensure BE will connect to the right DAI
1398 				 * widget.
1399 				 */
1400 				if (!cpu_dai->playback_widget) {
1401 					cpu_dai->playback_widget = w;
1402 					break;
1403 				}
1404 			}
1405 			if (i == rtd->num_cpus) {
1406 				dev_err(scomp->dev, "error: can't find BE for DAI %s\n",
1407 					w->name);
1408 
1409 				return -EINVAL;
1410 			}
1411 			dai->name = rtd->dai_link->name;
1412 			dev_dbg(scomp->dev, "tplg: connected widget %s -> DAI link %s\n",
1413 				w->name, rtd->dai_link->name);
1414 			break;
1415 		default:
1416 			break;
1417 		}
1418 	}
1419 
1420 	/* check we have a connection */
1421 	if (!dai->name) {
1422 		dev_err(scomp->dev, "error: can't connect DAI %s stream %s\n",
1423 			w->name, w->sname);
1424 		return -EINVAL;
1425 	}
1426 
1427 	return 0;
1428 }
1429 
1430 /**
1431  * sof_comp_alloc - allocate and initialize buffer for a new component
1432  * @swidget: pointer to struct snd_sof_widget containing extended data
1433  * @ipc_size: IPC payload size that will be updated depending on valid
1434  *  extended data.
1435  * @index: ID of the pipeline the component belongs to
1436  *
1437  * Return: The pointer to the new allocated component, NULL if failed.
1438  */
1439 static struct sof_ipc_comp *sof_comp_alloc(struct snd_sof_widget *swidget,
1440 					   size_t *ipc_size, int index)
1441 {
1442 	u8 nil_uuid[SOF_UUID_SIZE] = {0};
1443 	struct sof_ipc_comp *comp;
1444 	size_t total_size = *ipc_size;
1445 
1446 	/* only non-zero UUID is valid */
1447 	if (memcmp(&swidget->comp_ext, nil_uuid, SOF_UUID_SIZE))
1448 		total_size += sizeof(swidget->comp_ext);
1449 
1450 	comp = kzalloc(total_size, GFP_KERNEL);
1451 	if (!comp)
1452 		return NULL;
1453 
1454 	/* configure comp new IPC message */
1455 	comp->hdr.size = total_size;
1456 	comp->hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1457 	comp->id = swidget->comp_id;
1458 	comp->pipeline_id = index;
1459 	comp->core = swidget->core;
1460 
1461 	/* handle the extended data if needed */
1462 	if (total_size > *ipc_size) {
1463 		/* append extended data to the end of the component */
1464 		memcpy((u8 *)comp + *ipc_size, &swidget->comp_ext, sizeof(swidget->comp_ext));
1465 		comp->ext_data_length = sizeof(swidget->comp_ext);
1466 	}
1467 
1468 	/* update ipc_size and return */
1469 	*ipc_size = total_size;
1470 	return comp;
1471 }
1472 
1473 static int sof_widget_load_dai(struct snd_soc_component *scomp, int index,
1474 			       struct snd_sof_widget *swidget,
1475 			       struct snd_soc_tplg_dapm_widget *tw,
1476 			       struct snd_sof_dai *dai)
1477 {
1478 	struct snd_soc_tplg_private *private = &tw->priv;
1479 	struct sof_ipc_comp_dai *comp_dai;
1480 	size_t ipc_size = sizeof(*comp_dai);
1481 	int ret;
1482 
1483 	comp_dai = (struct sof_ipc_comp_dai *)
1484 		   sof_comp_alloc(swidget, &ipc_size, index);
1485 	if (!comp_dai)
1486 		return -ENOMEM;
1487 
1488 	/* configure dai IPC message */
1489 	comp_dai->comp.type = SOF_COMP_DAI;
1490 	comp_dai->config.hdr.size = sizeof(comp_dai->config);
1491 
1492 	ret = sof_parse_tokens(scomp, comp_dai, dai_tokens,
1493 			       ARRAY_SIZE(dai_tokens), private->array,
1494 			       le32_to_cpu(private->size));
1495 	if (ret != 0) {
1496 		dev_err(scomp->dev, "error: parse dai tokens failed %d\n",
1497 			le32_to_cpu(private->size));
1498 		goto finish;
1499 	}
1500 
1501 	ret = sof_parse_tokens(scomp, &comp_dai->config, comp_tokens,
1502 			       ARRAY_SIZE(comp_tokens), private->array,
1503 			       le32_to_cpu(private->size));
1504 	if (ret != 0) {
1505 		dev_err(scomp->dev, "error: parse dai.cfg tokens failed %d\n",
1506 			private->size);
1507 		goto finish;
1508 	}
1509 
1510 	dev_dbg(scomp->dev, "dai %s: type %d index %d\n",
1511 		swidget->widget->name, comp_dai->type, comp_dai->dai_index);
1512 	sof_dbg_comp_config(scomp, &comp_dai->config);
1513 
1514 	if (dai) {
1515 		dai->scomp = scomp;
1516 
1517 		/*
1518 		 * copy only the sof_ipc_comp_dai to avoid collapsing
1519 		 * the snd_sof_dai, the extended data is kept in the
1520 		 * snd_sof_widget.
1521 		 */
1522 		memcpy(&dai->comp_dai, comp_dai, sizeof(*comp_dai));
1523 	}
1524 
1525 finish:
1526 	kfree(comp_dai);
1527 	return ret;
1528 }
1529 
1530 /*
1531  * Buffer topology
1532  */
1533 
1534 static int sof_widget_load_buffer(struct snd_soc_component *scomp, int index,
1535 				  struct snd_sof_widget *swidget,
1536 				  struct snd_soc_tplg_dapm_widget *tw)
1537 {
1538 	struct snd_soc_tplg_private *private = &tw->priv;
1539 	struct sof_ipc_buffer *buffer;
1540 	int ret;
1541 
1542 	buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
1543 	if (!buffer)
1544 		return -ENOMEM;
1545 
1546 	/* configure dai IPC message */
1547 	buffer->comp.hdr.size = sizeof(*buffer);
1548 	buffer->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_BUFFER_NEW;
1549 	buffer->comp.id = swidget->comp_id;
1550 	buffer->comp.type = SOF_COMP_BUFFER;
1551 	buffer->comp.pipeline_id = index;
1552 	buffer->comp.core = swidget->core;
1553 
1554 	ret = sof_parse_tokens(scomp, buffer, buffer_tokens,
1555 			       ARRAY_SIZE(buffer_tokens), private->array,
1556 			       le32_to_cpu(private->size));
1557 	if (ret != 0) {
1558 		dev_err(scomp->dev, "error: parse buffer tokens failed %d\n",
1559 			private->size);
1560 		kfree(buffer);
1561 		return ret;
1562 	}
1563 
1564 	dev_dbg(scomp->dev, "buffer %s: size %d caps 0x%x\n",
1565 		swidget->widget->name, buffer->size, buffer->caps);
1566 
1567 	swidget->private = buffer;
1568 
1569 	return 0;
1570 }
1571 
1572 /* bind PCM ID to host component ID */
1573 static int spcm_bind(struct snd_soc_component *scomp, struct snd_sof_pcm *spcm,
1574 		     int dir)
1575 {
1576 	struct snd_sof_widget *host_widget;
1577 
1578 	host_widget = snd_sof_find_swidget_sname(scomp,
1579 						 spcm->pcm.caps[dir].name,
1580 						 dir);
1581 	if (!host_widget) {
1582 		dev_err(scomp->dev, "can't find host comp to bind pcm\n");
1583 		return -EINVAL;
1584 	}
1585 
1586 	spcm->stream[dir].comp_id = host_widget->comp_id;
1587 
1588 	return 0;
1589 }
1590 
1591 /*
1592  * PCM Topology
1593  */
1594 
1595 static int sof_widget_load_pcm(struct snd_soc_component *scomp, int index,
1596 			       struct snd_sof_widget *swidget,
1597 			       enum sof_ipc_stream_direction dir,
1598 			       struct snd_soc_tplg_dapm_widget *tw)
1599 {
1600 	struct snd_soc_tplg_private *private = &tw->priv;
1601 	struct sof_ipc_comp_host *host;
1602 	size_t ipc_size = sizeof(*host);
1603 	int ret;
1604 
1605 	host = (struct sof_ipc_comp_host *)
1606 	       sof_comp_alloc(swidget, &ipc_size, index);
1607 	if (!host)
1608 		return -ENOMEM;
1609 
1610 	/* configure host comp IPC message */
1611 	host->comp.type = SOF_COMP_HOST;
1612 	host->direction = dir;
1613 	host->config.hdr.size = sizeof(host->config);
1614 
1615 	ret = sof_parse_tokens(scomp, host, pcm_tokens,
1616 			       ARRAY_SIZE(pcm_tokens), private->array,
1617 			       le32_to_cpu(private->size));
1618 	if (ret != 0) {
1619 		dev_err(scomp->dev, "error: parse host tokens failed %d\n",
1620 			private->size);
1621 		goto err;
1622 	}
1623 
1624 	ret = sof_parse_tokens(scomp, &host->config, comp_tokens,
1625 			       ARRAY_SIZE(comp_tokens), private->array,
1626 			       le32_to_cpu(private->size));
1627 	if (ret != 0) {
1628 		dev_err(scomp->dev, "error: parse host.cfg tokens failed %d\n",
1629 			le32_to_cpu(private->size));
1630 		goto err;
1631 	}
1632 
1633 	dev_dbg(scomp->dev, "loaded host %s\n", swidget->widget->name);
1634 	sof_dbg_comp_config(scomp, &host->config);
1635 
1636 	swidget->private = host;
1637 
1638 	return 0;
1639 err:
1640 	kfree(host);
1641 	return ret;
1642 }
1643 
1644 /*
1645  * Pipeline Topology
1646  */
1647 static int sof_widget_load_pipeline(struct snd_soc_component *scomp, int index,
1648 				    struct snd_sof_widget *swidget,
1649 				    struct snd_soc_tplg_dapm_widget *tw)
1650 {
1651 	struct snd_soc_tplg_private *private = &tw->priv;
1652 	struct sof_ipc_pipe_new *pipeline;
1653 	struct snd_sof_widget *comp_swidget;
1654 	int ret;
1655 
1656 	pipeline = kzalloc(sizeof(*pipeline), GFP_KERNEL);
1657 	if (!pipeline)
1658 		return -ENOMEM;
1659 
1660 	/* configure dai IPC message */
1661 	pipeline->hdr.size = sizeof(*pipeline);
1662 	pipeline->hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_PIPE_NEW;
1663 	pipeline->pipeline_id = index;
1664 	pipeline->comp_id = swidget->comp_id;
1665 
1666 	/* component at start of pipeline is our stream id */
1667 	comp_swidget = snd_sof_find_swidget(scomp, tw->sname);
1668 	if (!comp_swidget) {
1669 		dev_err(scomp->dev, "error: widget %s refers to non existent widget %s\n",
1670 			tw->name, tw->sname);
1671 		ret = -EINVAL;
1672 		goto err;
1673 	}
1674 
1675 	pipeline->sched_id = comp_swidget->comp_id;
1676 
1677 	dev_dbg(scomp->dev, "tplg: pipeline id %d comp %d scheduling comp id %d\n",
1678 		pipeline->pipeline_id, pipeline->comp_id, pipeline->sched_id);
1679 
1680 	ret = sof_parse_tokens(scomp, pipeline, sched_tokens,
1681 			       ARRAY_SIZE(sched_tokens), private->array,
1682 			       le32_to_cpu(private->size));
1683 	if (ret != 0) {
1684 		dev_err(scomp->dev, "error: parse pipeline tokens failed %d\n",
1685 			private->size);
1686 		goto err;
1687 	}
1688 
1689 	ret = sof_parse_tokens(scomp, swidget, pipeline_tokens,
1690 			       ARRAY_SIZE(pipeline_tokens), private->array,
1691 			       le32_to_cpu(private->size));
1692 	if (ret != 0) {
1693 		dev_err(scomp->dev, "error: parse dynamic pipeline token failed %d\n",
1694 			private->size);
1695 		goto err;
1696 	}
1697 
1698 	if (sof_debug_check_flag(SOF_DBG_DISABLE_MULTICORE))
1699 		pipeline->core = SOF_DSP_PRIMARY_CORE;
1700 
1701 	if (sof_debug_check_flag(SOF_DBG_DYNAMIC_PIPELINES_OVERRIDE))
1702 		swidget->dynamic_pipeline_widget =
1703 			sof_debug_check_flag(SOF_DBG_DYNAMIC_PIPELINES_ENABLE);
1704 
1705 	dev_dbg(scomp->dev, "pipeline %s: period %d pri %d mips %d core %d frames %d dynamic %d\n",
1706 		swidget->widget->name, pipeline->period, pipeline->priority,
1707 		pipeline->period_mips, pipeline->core, pipeline->frames_per_sched,
1708 		swidget->dynamic_pipeline_widget);
1709 
1710 	swidget->private = pipeline;
1711 
1712 	return 0;
1713 err:
1714 	kfree(pipeline);
1715 	return ret;
1716 }
1717 
1718 /*
1719  * Mixer topology
1720  */
1721 
1722 static int sof_widget_load_mixer(struct snd_soc_component *scomp, int index,
1723 				 struct snd_sof_widget *swidget,
1724 				 struct snd_soc_tplg_dapm_widget *tw)
1725 {
1726 	struct snd_soc_tplg_private *private = &tw->priv;
1727 	struct sof_ipc_comp_mixer *mixer;
1728 	size_t ipc_size = sizeof(*mixer);
1729 	int ret;
1730 
1731 	mixer = (struct sof_ipc_comp_mixer *)
1732 		sof_comp_alloc(swidget, &ipc_size, index);
1733 	if (!mixer)
1734 		return -ENOMEM;
1735 
1736 	/* configure mixer IPC message */
1737 	mixer->comp.type = SOF_COMP_MIXER;
1738 	mixer->config.hdr.size = sizeof(mixer->config);
1739 
1740 	ret = sof_parse_tokens(scomp, &mixer->config, comp_tokens,
1741 			       ARRAY_SIZE(comp_tokens), private->array,
1742 			       le32_to_cpu(private->size));
1743 	if (ret != 0) {
1744 		dev_err(scomp->dev, "error: parse mixer.cfg tokens failed %d\n",
1745 			private->size);
1746 		kfree(mixer);
1747 		return ret;
1748 	}
1749 
1750 	sof_dbg_comp_config(scomp, &mixer->config);
1751 
1752 	swidget->private = mixer;
1753 
1754 	return 0;
1755 }
1756 
1757 /*
1758  * Mux topology
1759  */
1760 static int sof_widget_load_mux(struct snd_soc_component *scomp, int index,
1761 			       struct snd_sof_widget *swidget,
1762 			       struct snd_soc_tplg_dapm_widget *tw)
1763 {
1764 	struct snd_soc_tplg_private *private = &tw->priv;
1765 	struct sof_ipc_comp_mux *mux;
1766 	size_t ipc_size = sizeof(*mux);
1767 	int ret;
1768 
1769 	mux = (struct sof_ipc_comp_mux *)
1770 	      sof_comp_alloc(swidget, &ipc_size, index);
1771 	if (!mux)
1772 		return -ENOMEM;
1773 
1774 	/* configure mux IPC message */
1775 	mux->comp.type = SOF_COMP_MUX;
1776 	mux->config.hdr.size = sizeof(mux->config);
1777 
1778 	ret = sof_parse_tokens(scomp, &mux->config, comp_tokens,
1779 			       ARRAY_SIZE(comp_tokens), private->array,
1780 			       le32_to_cpu(private->size));
1781 	if (ret != 0) {
1782 		dev_err(scomp->dev, "error: parse mux.cfg tokens failed %d\n",
1783 			private->size);
1784 		kfree(mux);
1785 		return ret;
1786 	}
1787 
1788 	sof_dbg_comp_config(scomp, &mux->config);
1789 
1790 	swidget->private = mux;
1791 
1792 	return 0;
1793 }
1794 
1795 /*
1796  * PGA Topology
1797  */
1798 
1799 static int sof_widget_load_pga(struct snd_soc_component *scomp, int index,
1800 			       struct snd_sof_widget *swidget,
1801 			       struct snd_soc_tplg_dapm_widget *tw)
1802 {
1803 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1804 	struct snd_soc_tplg_private *private = &tw->priv;
1805 	struct sof_ipc_comp_volume *volume;
1806 	struct snd_sof_control *scontrol;
1807 	size_t ipc_size = sizeof(*volume);
1808 	int min_step;
1809 	int max_step;
1810 	int ret;
1811 
1812 	volume = (struct sof_ipc_comp_volume *)
1813 		 sof_comp_alloc(swidget, &ipc_size, index);
1814 	if (!volume)
1815 		return -ENOMEM;
1816 
1817 	if (!le32_to_cpu(tw->num_kcontrols)) {
1818 		dev_err(scomp->dev, "error: invalid kcontrol count %d for volume\n",
1819 			tw->num_kcontrols);
1820 		ret = -EINVAL;
1821 		goto err;
1822 	}
1823 
1824 	/* configure volume IPC message */
1825 	volume->comp.type = SOF_COMP_VOLUME;
1826 	volume->config.hdr.size = sizeof(volume->config);
1827 
1828 	ret = sof_parse_tokens(scomp, volume, volume_tokens,
1829 			       ARRAY_SIZE(volume_tokens), private->array,
1830 			       le32_to_cpu(private->size));
1831 	if (ret != 0) {
1832 		dev_err(scomp->dev, "error: parse volume tokens failed %d\n",
1833 			private->size);
1834 		goto err;
1835 	}
1836 	ret = sof_parse_tokens(scomp, &volume->config, comp_tokens,
1837 			       ARRAY_SIZE(comp_tokens), private->array,
1838 			       le32_to_cpu(private->size));
1839 	if (ret != 0) {
1840 		dev_err(scomp->dev, "error: parse volume.cfg tokens failed %d\n",
1841 			le32_to_cpu(private->size));
1842 		goto err;
1843 	}
1844 
1845 	sof_dbg_comp_config(scomp, &volume->config);
1846 
1847 	swidget->private = volume;
1848 
1849 	list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
1850 		if (scontrol->comp_id == swidget->comp_id &&
1851 		    scontrol->volume_table) {
1852 			min_step = scontrol->min_volume_step;
1853 			max_step = scontrol->max_volume_step;
1854 			volume->min_value = scontrol->volume_table[min_step];
1855 			volume->max_value = scontrol->volume_table[max_step];
1856 			volume->channels = scontrol->num_channels;
1857 			break;
1858 		}
1859 	}
1860 
1861 	return 0;
1862 err:
1863 	kfree(volume);
1864 	return ret;
1865 }
1866 
1867 /*
1868  * SRC Topology
1869  */
1870 
1871 static int sof_widget_load_src(struct snd_soc_component *scomp, int index,
1872 			       struct snd_sof_widget *swidget,
1873 			       struct snd_soc_tplg_dapm_widget *tw)
1874 {
1875 	struct snd_soc_tplg_private *private = &tw->priv;
1876 	struct sof_ipc_comp_src *src;
1877 	size_t ipc_size = sizeof(*src);
1878 	int ret;
1879 
1880 	src = (struct sof_ipc_comp_src *)
1881 	      sof_comp_alloc(swidget, &ipc_size, index);
1882 	if (!src)
1883 		return -ENOMEM;
1884 
1885 	/* configure src IPC message */
1886 	src->comp.type = SOF_COMP_SRC;
1887 	src->config.hdr.size = sizeof(src->config);
1888 
1889 	ret = sof_parse_tokens(scomp, src, src_tokens,
1890 			       ARRAY_SIZE(src_tokens), private->array,
1891 			       le32_to_cpu(private->size));
1892 	if (ret != 0) {
1893 		dev_err(scomp->dev, "error: parse src tokens failed %d\n",
1894 			private->size);
1895 		goto err;
1896 	}
1897 
1898 	ret = sof_parse_tokens(scomp, &src->config, comp_tokens,
1899 			       ARRAY_SIZE(comp_tokens), private->array,
1900 			       le32_to_cpu(private->size));
1901 	if (ret != 0) {
1902 		dev_err(scomp->dev, "error: parse src.cfg tokens failed %d\n",
1903 			le32_to_cpu(private->size));
1904 		goto err;
1905 	}
1906 
1907 	dev_dbg(scomp->dev, "src %s: source rate %d sink rate %d\n",
1908 		swidget->widget->name, src->source_rate, src->sink_rate);
1909 	sof_dbg_comp_config(scomp, &src->config);
1910 
1911 	swidget->private = src;
1912 
1913 	return 0;
1914 err:
1915 	kfree(src);
1916 	return ret;
1917 }
1918 
1919 /*
1920  * ASRC Topology
1921  */
1922 
1923 static int sof_widget_load_asrc(struct snd_soc_component *scomp, int index,
1924 				struct snd_sof_widget *swidget,
1925 				struct snd_soc_tplg_dapm_widget *tw)
1926 {
1927 	struct snd_soc_tplg_private *private = &tw->priv;
1928 	struct sof_ipc_comp_asrc *asrc;
1929 	size_t ipc_size = sizeof(*asrc);
1930 	int ret;
1931 
1932 	asrc = (struct sof_ipc_comp_asrc *)
1933 	       sof_comp_alloc(swidget, &ipc_size, index);
1934 	if (!asrc)
1935 		return -ENOMEM;
1936 
1937 	/* configure ASRC IPC message */
1938 	asrc->comp.type = SOF_COMP_ASRC;
1939 	asrc->config.hdr.size = sizeof(asrc->config);
1940 
1941 	ret = sof_parse_tokens(scomp, asrc, asrc_tokens,
1942 			       ARRAY_SIZE(asrc_tokens), private->array,
1943 			       le32_to_cpu(private->size));
1944 	if (ret != 0) {
1945 		dev_err(scomp->dev, "error: parse asrc tokens failed %d\n",
1946 			private->size);
1947 		goto err;
1948 	}
1949 
1950 	ret = sof_parse_tokens(scomp, &asrc->config, comp_tokens,
1951 			       ARRAY_SIZE(comp_tokens), private->array,
1952 			       le32_to_cpu(private->size));
1953 	if (ret != 0) {
1954 		dev_err(scomp->dev, "error: parse asrc.cfg tokens failed %d\n",
1955 			le32_to_cpu(private->size));
1956 		goto err;
1957 	}
1958 
1959 	dev_dbg(scomp->dev, "asrc %s: source rate %d sink rate %d "
1960 		"asynch %d operation %d\n",
1961 		swidget->widget->name, asrc->source_rate, asrc->sink_rate,
1962 		asrc->asynchronous_mode, asrc->operation_mode);
1963 	sof_dbg_comp_config(scomp, &asrc->config);
1964 
1965 	swidget->private = asrc;
1966 
1967 	return 0;
1968 err:
1969 	kfree(asrc);
1970 	return ret;
1971 }
1972 
1973 /*
1974  * Signal Generator Topology
1975  */
1976 
1977 static int sof_widget_load_siggen(struct snd_soc_component *scomp, int index,
1978 				  struct snd_sof_widget *swidget,
1979 				  struct snd_soc_tplg_dapm_widget *tw)
1980 {
1981 	struct snd_soc_tplg_private *private = &tw->priv;
1982 	struct sof_ipc_comp_tone *tone;
1983 	size_t ipc_size = sizeof(*tone);
1984 	int ret;
1985 
1986 	tone = (struct sof_ipc_comp_tone *)
1987 	       sof_comp_alloc(swidget, &ipc_size, index);
1988 	if (!tone)
1989 		return -ENOMEM;
1990 
1991 	/* configure siggen IPC message */
1992 	tone->comp.type = SOF_COMP_TONE;
1993 	tone->config.hdr.size = sizeof(tone->config);
1994 
1995 	ret = sof_parse_tokens(scomp, tone, tone_tokens,
1996 			       ARRAY_SIZE(tone_tokens), private->array,
1997 			       le32_to_cpu(private->size));
1998 	if (ret != 0) {
1999 		dev_err(scomp->dev, "error: parse tone tokens failed %d\n",
2000 			le32_to_cpu(private->size));
2001 		goto err;
2002 	}
2003 
2004 	ret = sof_parse_tokens(scomp, &tone->config, comp_tokens,
2005 			       ARRAY_SIZE(comp_tokens), private->array,
2006 			       le32_to_cpu(private->size));
2007 	if (ret != 0) {
2008 		dev_err(scomp->dev, "error: parse tone.cfg tokens failed %d\n",
2009 			le32_to_cpu(private->size));
2010 		goto err;
2011 	}
2012 
2013 	dev_dbg(scomp->dev, "tone %s: frequency %d amplitude %d\n",
2014 		swidget->widget->name, tone->frequency, tone->amplitude);
2015 	sof_dbg_comp_config(scomp, &tone->config);
2016 
2017 	swidget->private = tone;
2018 
2019 	return 0;
2020 err:
2021 	kfree(tone);
2022 	return ret;
2023 }
2024 
2025 static int sof_get_control_data(struct snd_soc_component *scomp,
2026 				struct snd_soc_dapm_widget *widget,
2027 				struct sof_widget_data *wdata,
2028 				size_t *size)
2029 {
2030 	const struct snd_kcontrol_new *kc;
2031 	struct soc_mixer_control *sm;
2032 	struct soc_bytes_ext *sbe;
2033 	struct soc_enum *se;
2034 	int i;
2035 
2036 	*size = 0;
2037 
2038 	for (i = 0; i < widget->num_kcontrols; i++) {
2039 		kc = &widget->kcontrol_news[i];
2040 
2041 		switch (widget->dobj.widget.kcontrol_type[i]) {
2042 		case SND_SOC_TPLG_TYPE_MIXER:
2043 			sm = (struct soc_mixer_control *)kc->private_value;
2044 			wdata[i].control = sm->dobj.private;
2045 			break;
2046 		case SND_SOC_TPLG_TYPE_BYTES:
2047 			sbe = (struct soc_bytes_ext *)kc->private_value;
2048 			wdata[i].control = sbe->dobj.private;
2049 			break;
2050 		case SND_SOC_TPLG_TYPE_ENUM:
2051 			se = (struct soc_enum *)kc->private_value;
2052 			wdata[i].control = se->dobj.private;
2053 			break;
2054 		default:
2055 			dev_err(scomp->dev, "error: unknown kcontrol type %u in widget %s\n",
2056 				widget->dobj.widget.kcontrol_type[i],
2057 				widget->name);
2058 			return -EINVAL;
2059 		}
2060 
2061 		if (!wdata[i].control) {
2062 			dev_err(scomp->dev, "error: no scontrol for widget %s\n",
2063 				widget->name);
2064 			return -EINVAL;
2065 		}
2066 
2067 		wdata[i].pdata = wdata[i].control->control_data->data;
2068 		if (!wdata[i].pdata)
2069 			return -EINVAL;
2070 
2071 		/* make sure data is valid - data can be updated at runtime */
2072 		if (widget->dobj.widget.kcontrol_type[i] == SND_SOC_TPLG_TYPE_BYTES &&
2073 		    wdata[i].pdata->magic != SOF_ABI_MAGIC)
2074 			return -EINVAL;
2075 
2076 		*size += wdata[i].pdata->size;
2077 
2078 		/* get data type */
2079 		switch (wdata[i].control->control_data->cmd) {
2080 		case SOF_CTRL_CMD_VOLUME:
2081 		case SOF_CTRL_CMD_ENUM:
2082 		case SOF_CTRL_CMD_SWITCH:
2083 			wdata[i].ipc_cmd = SOF_IPC_COMP_SET_VALUE;
2084 			wdata[i].ctrl_type = SOF_CTRL_TYPE_VALUE_CHAN_SET;
2085 			break;
2086 		case SOF_CTRL_CMD_BINARY:
2087 			wdata[i].ipc_cmd = SOF_IPC_COMP_SET_DATA;
2088 			wdata[i].ctrl_type = SOF_CTRL_TYPE_DATA_SET;
2089 			break;
2090 		default:
2091 			break;
2092 		}
2093 	}
2094 
2095 	return 0;
2096 }
2097 
2098 static int sof_process_load(struct snd_soc_component *scomp, int index,
2099 			    struct snd_sof_widget *swidget,
2100 			    struct snd_soc_tplg_dapm_widget *tw,
2101 			    int type)
2102 {
2103 	struct snd_soc_dapm_widget *widget = swidget->widget;
2104 	struct snd_soc_tplg_private *private = &tw->priv;
2105 	struct sof_ipc_comp_process *process;
2106 	struct sof_widget_data *wdata = NULL;
2107 	size_t ipc_data_size = 0;
2108 	size_t ipc_size;
2109 	int offset = 0;
2110 	int ret;
2111 	int i;
2112 
2113 	/* allocate struct for widget control data sizes and types */
2114 	if (widget->num_kcontrols) {
2115 		wdata = kcalloc(widget->num_kcontrols,
2116 				sizeof(*wdata),
2117 				GFP_KERNEL);
2118 
2119 		if (!wdata)
2120 			return -ENOMEM;
2121 
2122 		/* get possible component controls and get size of all pdata */
2123 		ret = sof_get_control_data(scomp, widget, wdata,
2124 					   &ipc_data_size);
2125 
2126 		if (ret < 0)
2127 			goto out;
2128 	}
2129 
2130 	ipc_size = sizeof(struct sof_ipc_comp_process) + ipc_data_size;
2131 
2132 	/* we are exceeding max ipc size, config needs to be sent separately */
2133 	if (ipc_size > SOF_IPC_MSG_MAX_SIZE) {
2134 		ipc_size -= ipc_data_size;
2135 		ipc_data_size = 0;
2136 	}
2137 
2138 	process = (struct sof_ipc_comp_process *)
2139 		  sof_comp_alloc(swidget, &ipc_size, index);
2140 	if (!process) {
2141 		ret = -ENOMEM;
2142 		goto out;
2143 	}
2144 
2145 	/* configure iir IPC message */
2146 	process->comp.type = type;
2147 	process->config.hdr.size = sizeof(process->config);
2148 
2149 	ret = sof_parse_tokens(scomp, &process->config, comp_tokens,
2150 			       ARRAY_SIZE(comp_tokens), private->array,
2151 			       le32_to_cpu(private->size));
2152 	if (ret != 0) {
2153 		dev_err(scomp->dev, "error: parse process.cfg tokens failed %d\n",
2154 			le32_to_cpu(private->size));
2155 		goto err;
2156 	}
2157 
2158 	sof_dbg_comp_config(scomp, &process->config);
2159 
2160 	/*
2161 	 * found private data in control, so copy it.
2162 	 * get possible component controls - get size of all pdata,
2163 	 * then memcpy with headers
2164 	 */
2165 	if (ipc_data_size) {
2166 		for (i = 0; i < widget->num_kcontrols; i++) {
2167 			memcpy(&process->data + offset,
2168 			       wdata[i].pdata->data,
2169 			       wdata[i].pdata->size);
2170 			offset += wdata[i].pdata->size;
2171 		}
2172 	}
2173 
2174 	process->size = ipc_data_size;
2175 	swidget->private = process;
2176 err:
2177 	if (ret < 0)
2178 		kfree(process);
2179 out:
2180 	kfree(wdata);
2181 	return ret;
2182 }
2183 
2184 /*
2185  * Processing Component Topology - can be "effect", "codec", or general
2186  * "processing".
2187  */
2188 
2189 static int sof_widget_load_process(struct snd_soc_component *scomp, int index,
2190 				   struct snd_sof_widget *swidget,
2191 				   struct snd_soc_tplg_dapm_widget *tw)
2192 {
2193 	struct snd_soc_tplg_private *private = &tw->priv;
2194 	struct sof_ipc_comp_process config;
2195 	int ret;
2196 
2197 	/* check we have some tokens - we need at least process type */
2198 	if (le32_to_cpu(private->size) == 0) {
2199 		dev_err(scomp->dev, "error: process tokens not found\n");
2200 		return -EINVAL;
2201 	}
2202 
2203 	memset(&config, 0, sizeof(config));
2204 	config.comp.core = swidget->core;
2205 
2206 	/* get the process token */
2207 	ret = sof_parse_tokens(scomp, &config, process_tokens,
2208 			       ARRAY_SIZE(process_tokens), private->array,
2209 			       le32_to_cpu(private->size));
2210 	if (ret != 0) {
2211 		dev_err(scomp->dev, "error: parse process tokens failed %d\n",
2212 			le32_to_cpu(private->size));
2213 		return ret;
2214 	}
2215 
2216 	/* now load process specific data and send IPC */
2217 	ret = sof_process_load(scomp, index, swidget, tw, find_process_comp_type(config.type));
2218 	if (ret < 0) {
2219 		dev_err(scomp->dev, "error: process loading failed\n");
2220 		return ret;
2221 	}
2222 
2223 	return 0;
2224 }
2225 
2226 static int sof_widget_bind_event(struct snd_soc_component *scomp,
2227 				 struct snd_sof_widget *swidget,
2228 				 u16 event_type)
2229 {
2230 	struct sof_ipc_comp *ipc_comp;
2231 
2232 	/* validate widget event type */
2233 	switch (event_type) {
2234 	case SOF_KEYWORD_DETECT_DAPM_EVENT:
2235 		/* only KEYWORD_DETECT comps should handle this */
2236 		if (swidget->id != snd_soc_dapm_effect)
2237 			break;
2238 
2239 		ipc_comp = swidget->private;
2240 		if (ipc_comp && ipc_comp->type != SOF_COMP_KEYWORD_DETECT)
2241 			break;
2242 
2243 		/* bind event to keyword detect comp */
2244 		return snd_soc_tplg_widget_bind_event(swidget->widget,
2245 						      sof_kwd_events,
2246 						      ARRAY_SIZE(sof_kwd_events),
2247 						      event_type);
2248 	default:
2249 		break;
2250 	}
2251 
2252 	dev_err(scomp->dev,
2253 		"error: invalid event type %d for widget %s\n",
2254 		event_type, swidget->widget->name);
2255 	return -EINVAL;
2256 }
2257 
2258 /* external widget init - used for any driver specific init */
2259 static int sof_widget_ready(struct snd_soc_component *scomp, int index,
2260 			    struct snd_soc_dapm_widget *w,
2261 			    struct snd_soc_tplg_dapm_widget *tw)
2262 {
2263 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2264 	struct snd_sof_widget *swidget;
2265 	struct snd_sof_dai *dai;
2266 	struct sof_ipc_comp comp = {
2267 		.core = SOF_DSP_PRIMARY_CORE,
2268 	};
2269 	int ret = 0;
2270 
2271 	swidget = kzalloc(sizeof(*swidget), GFP_KERNEL);
2272 	if (!swidget)
2273 		return -ENOMEM;
2274 
2275 	swidget->scomp = scomp;
2276 	swidget->widget = w;
2277 	swidget->comp_id = sdev->next_comp_id++;
2278 	swidget->complete = 0;
2279 	swidget->id = w->id;
2280 	swidget->pipeline_id = index;
2281 	swidget->private = NULL;
2282 
2283 	dev_dbg(scomp->dev, "tplg: ready widget id %d pipe %d type %d name : %s stream %s\n",
2284 		swidget->comp_id, index, swidget->id, tw->name,
2285 		strnlen(tw->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0
2286 			? tw->sname : "none");
2287 
2288 	ret = sof_parse_tokens(scomp, &comp, core_tokens,
2289 			       ARRAY_SIZE(core_tokens), tw->priv.array,
2290 			       le32_to_cpu(tw->priv.size));
2291 	if (ret != 0) {
2292 		dev_err(scomp->dev, "error: parsing core tokens failed %d\n",
2293 			ret);
2294 		kfree(swidget);
2295 		return ret;
2296 	}
2297 
2298 	if (sof_debug_check_flag(SOF_DBG_DISABLE_MULTICORE))
2299 		comp.core = SOF_DSP_PRIMARY_CORE;
2300 
2301 	swidget->core = comp.core;
2302 
2303 	ret = sof_parse_tokens(scomp, &swidget->comp_ext, comp_ext_tokens,
2304 			       ARRAY_SIZE(comp_ext_tokens), tw->priv.array,
2305 			       le32_to_cpu(tw->priv.size));
2306 	if (ret != 0) {
2307 		dev_err(scomp->dev, "error: parsing comp_ext_tokens failed %d\n",
2308 			ret);
2309 		kfree(swidget);
2310 		return ret;
2311 	}
2312 
2313 	/* handle any special case widgets */
2314 	switch (w->id) {
2315 	case snd_soc_dapm_dai_in:
2316 	case snd_soc_dapm_dai_out:
2317 		dai = kzalloc(sizeof(*dai), GFP_KERNEL);
2318 		if (!dai) {
2319 			kfree(swidget);
2320 			return -ENOMEM;
2321 		}
2322 
2323 		ret = sof_widget_load_dai(scomp, index, swidget, tw, dai);
2324 		if (!ret)
2325 			ret = sof_connect_dai_widget(scomp, w, tw, dai);
2326 		if (ret < 0) {
2327 			kfree(dai);
2328 			break;
2329 		}
2330 		list_add(&dai->list, &sdev->dai_list);
2331 		swidget->private = dai;
2332 		break;
2333 	case snd_soc_dapm_mixer:
2334 		ret = sof_widget_load_mixer(scomp, index, swidget, tw);
2335 		break;
2336 	case snd_soc_dapm_pga:
2337 		ret = sof_widget_load_pga(scomp, index, swidget, tw);
2338 		break;
2339 	case snd_soc_dapm_buffer:
2340 		ret = sof_widget_load_buffer(scomp, index, swidget, tw);
2341 		break;
2342 	case snd_soc_dapm_scheduler:
2343 		ret = sof_widget_load_pipeline(scomp, index, swidget, tw);
2344 		break;
2345 	case snd_soc_dapm_aif_out:
2346 		ret = sof_widget_load_pcm(scomp, index, swidget,
2347 					  SOF_IPC_STREAM_CAPTURE, tw);
2348 		break;
2349 	case snd_soc_dapm_aif_in:
2350 		ret = sof_widget_load_pcm(scomp, index, swidget,
2351 					  SOF_IPC_STREAM_PLAYBACK, tw);
2352 		break;
2353 	case snd_soc_dapm_src:
2354 		ret = sof_widget_load_src(scomp, index, swidget, tw);
2355 		break;
2356 	case snd_soc_dapm_asrc:
2357 		ret = sof_widget_load_asrc(scomp, index, swidget, tw);
2358 		break;
2359 	case snd_soc_dapm_siggen:
2360 		ret = sof_widget_load_siggen(scomp, index, swidget, tw);
2361 		break;
2362 	case snd_soc_dapm_effect:
2363 		ret = sof_widget_load_process(scomp, index, swidget, tw);
2364 		break;
2365 	case snd_soc_dapm_mux:
2366 	case snd_soc_dapm_demux:
2367 		ret = sof_widget_load_mux(scomp, index, swidget, tw);
2368 		break;
2369 	case snd_soc_dapm_switch:
2370 	case snd_soc_dapm_dai_link:
2371 	case snd_soc_dapm_kcontrol:
2372 	default:
2373 		dev_dbg(scomp->dev, "widget type %d name %s not handled\n", swidget->id, tw->name);
2374 		break;
2375 	}
2376 
2377 	/* check IPC reply */
2378 	if (ret < 0) {
2379 		dev_err(scomp->dev,
2380 			"error: failed to add widget id %d type %d name : %s stream %s\n",
2381 			tw->shift, swidget->id, tw->name,
2382 			strnlen(tw->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0
2383 				? tw->sname : "none");
2384 		kfree(swidget);
2385 		return ret;
2386 	}
2387 
2388 	/* bind widget to external event */
2389 	if (tw->event_type) {
2390 		ret = sof_widget_bind_event(scomp, swidget,
2391 					    le16_to_cpu(tw->event_type));
2392 		if (ret) {
2393 			dev_err(scomp->dev, "error: widget event binding failed\n");
2394 			kfree(swidget->private);
2395 			kfree(swidget);
2396 			return ret;
2397 		}
2398 	}
2399 
2400 	w->dobj.private = swidget;
2401 	list_add(&swidget->list, &sdev->widget_list);
2402 	return ret;
2403 }
2404 
2405 static int sof_route_unload(struct snd_soc_component *scomp,
2406 			    struct snd_soc_dobj *dobj)
2407 {
2408 	struct snd_sof_route *sroute;
2409 
2410 	sroute = dobj->private;
2411 	if (!sroute)
2412 		return 0;
2413 
2414 	/* free sroute and its private data */
2415 	kfree(sroute->private);
2416 	list_del(&sroute->list);
2417 	kfree(sroute);
2418 
2419 	return 0;
2420 }
2421 
2422 static int sof_widget_unload(struct snd_soc_component *scomp,
2423 			     struct snd_soc_dobj *dobj)
2424 {
2425 	const struct snd_kcontrol_new *kc;
2426 	struct snd_soc_dapm_widget *widget;
2427 	struct snd_sof_control *scontrol;
2428 	struct snd_sof_widget *swidget;
2429 	struct soc_mixer_control *sm;
2430 	struct soc_bytes_ext *sbe;
2431 	struct snd_sof_dai *dai;
2432 	struct soc_enum *se;
2433 	int ret = 0;
2434 	int i;
2435 
2436 	swidget = dobj->private;
2437 	if (!swidget)
2438 		return 0;
2439 
2440 	widget = swidget->widget;
2441 
2442 	switch (swidget->id) {
2443 	case snd_soc_dapm_dai_in:
2444 	case snd_soc_dapm_dai_out:
2445 		dai = swidget->private;
2446 
2447 		if (dai) {
2448 			/* free dai config */
2449 			kfree(dai->dai_config);
2450 			list_del(&dai->list);
2451 		}
2452 		break;
2453 	default:
2454 		break;
2455 	}
2456 	for (i = 0; i < widget->num_kcontrols; i++) {
2457 		kc = &widget->kcontrol_news[i];
2458 		switch (widget->dobj.widget.kcontrol_type[i]) {
2459 		case SND_SOC_TPLG_TYPE_MIXER:
2460 			sm = (struct soc_mixer_control *)kc->private_value;
2461 			scontrol = sm->dobj.private;
2462 			if (sm->max > 1)
2463 				kfree(scontrol->volume_table);
2464 			break;
2465 		case SND_SOC_TPLG_TYPE_ENUM:
2466 			se = (struct soc_enum *)kc->private_value;
2467 			scontrol = se->dobj.private;
2468 			break;
2469 		case SND_SOC_TPLG_TYPE_BYTES:
2470 			sbe = (struct soc_bytes_ext *)kc->private_value;
2471 			scontrol = sbe->dobj.private;
2472 			break;
2473 		default:
2474 			dev_warn(scomp->dev, "unsupported kcontrol_type\n");
2475 			goto out;
2476 		}
2477 		kfree(scontrol->control_data);
2478 		list_del(&scontrol->list);
2479 		kfree(scontrol);
2480 	}
2481 
2482 out:
2483 	/* free private value */
2484 	kfree(swidget->private);
2485 
2486 	/* remove and free swidget object */
2487 	list_del(&swidget->list);
2488 	kfree(swidget);
2489 
2490 	return ret;
2491 }
2492 
2493 /*
2494  * DAI HW configuration.
2495  */
2496 
2497 /* FE DAI - used for any driver specific init */
2498 static int sof_dai_load(struct snd_soc_component *scomp, int index,
2499 			struct snd_soc_dai_driver *dai_drv,
2500 			struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
2501 {
2502 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2503 	struct snd_soc_tplg_stream_caps *caps;
2504 	struct snd_soc_tplg_private *private = &pcm->priv;
2505 	struct snd_sof_pcm *spcm;
2506 	int stream;
2507 	int ret;
2508 
2509 	/* nothing to do for BEs atm */
2510 	if (!pcm)
2511 		return 0;
2512 
2513 	spcm = kzalloc(sizeof(*spcm), GFP_KERNEL);
2514 	if (!spcm)
2515 		return -ENOMEM;
2516 
2517 	spcm->scomp = scomp;
2518 
2519 	for_each_pcm_streams(stream) {
2520 		spcm->stream[stream].comp_id = COMP_ID_UNASSIGNED;
2521 		if (pcm->compress)
2522 			snd_sof_compr_init_elapsed_work(&spcm->stream[stream].period_elapsed_work);
2523 		else
2524 			snd_sof_pcm_init_elapsed_work(&spcm->stream[stream].period_elapsed_work);
2525 	}
2526 
2527 	spcm->pcm = *pcm;
2528 	dev_dbg(scomp->dev, "tplg: load pcm %s\n", pcm->dai_name);
2529 
2530 	dai_drv->dobj.private = spcm;
2531 	list_add(&spcm->list, &sdev->pcm_list);
2532 
2533 	ret = sof_parse_tokens(scomp, spcm, stream_tokens,
2534 			       ARRAY_SIZE(stream_tokens), private->array,
2535 			       le32_to_cpu(private->size));
2536 	if (ret) {
2537 		dev_err(scomp->dev, "error: parse stream tokens failed %d\n",
2538 			le32_to_cpu(private->size));
2539 		return ret;
2540 	}
2541 
2542 	/* do we need to allocate playback PCM DMA pages */
2543 	if (!spcm->pcm.playback)
2544 		goto capture;
2545 
2546 	stream = SNDRV_PCM_STREAM_PLAYBACK;
2547 
2548 	dev_vdbg(scomp->dev, "tplg: pcm %s stream tokens: playback d0i3:%d\n",
2549 		 spcm->pcm.pcm_name, spcm->stream[stream].d0i3_compatible);
2550 
2551 	caps = &spcm->pcm.caps[stream];
2552 
2553 	/* allocate playback page table buffer */
2554 	ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev,
2555 				  PAGE_SIZE, &spcm->stream[stream].page_table);
2556 	if (ret < 0) {
2557 		dev_err(scomp->dev, "error: can't alloc page table for %s %d\n",
2558 			caps->name, ret);
2559 
2560 		return ret;
2561 	}
2562 
2563 	/* bind pcm to host comp */
2564 	ret = spcm_bind(scomp, spcm, stream);
2565 	if (ret) {
2566 		dev_err(scomp->dev,
2567 			"error: can't bind pcm to host\n");
2568 		goto free_playback_tables;
2569 	}
2570 
2571 capture:
2572 	stream = SNDRV_PCM_STREAM_CAPTURE;
2573 
2574 	/* do we need to allocate capture PCM DMA pages */
2575 	if (!spcm->pcm.capture)
2576 		return ret;
2577 
2578 	dev_vdbg(scomp->dev, "tplg: pcm %s stream tokens: capture d0i3:%d\n",
2579 		 spcm->pcm.pcm_name, spcm->stream[stream].d0i3_compatible);
2580 
2581 	caps = &spcm->pcm.caps[stream];
2582 
2583 	/* allocate capture page table buffer */
2584 	ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev,
2585 				  PAGE_SIZE, &spcm->stream[stream].page_table);
2586 	if (ret < 0) {
2587 		dev_err(scomp->dev, "error: can't alloc page table for %s %d\n",
2588 			caps->name, ret);
2589 		goto free_playback_tables;
2590 	}
2591 
2592 	/* bind pcm to host comp */
2593 	ret = spcm_bind(scomp, spcm, stream);
2594 	if (ret) {
2595 		dev_err(scomp->dev,
2596 			"error: can't bind pcm to host\n");
2597 		snd_dma_free_pages(&spcm->stream[stream].page_table);
2598 		goto free_playback_tables;
2599 	}
2600 
2601 	return ret;
2602 
2603 free_playback_tables:
2604 	if (spcm->pcm.playback)
2605 		snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table);
2606 
2607 	return ret;
2608 }
2609 
2610 static int sof_dai_unload(struct snd_soc_component *scomp,
2611 			  struct snd_soc_dobj *dobj)
2612 {
2613 	struct snd_sof_pcm *spcm = dobj->private;
2614 
2615 	/* free PCM DMA pages */
2616 	if (spcm->pcm.playback)
2617 		snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table);
2618 
2619 	if (spcm->pcm.capture)
2620 		snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_CAPTURE].page_table);
2621 
2622 	/* remove from list and free spcm */
2623 	list_del(&spcm->list);
2624 	kfree(spcm);
2625 
2626 	return 0;
2627 }
2628 
2629 static void sof_dai_set_format(struct snd_soc_tplg_hw_config *hw_config,
2630 			       struct sof_ipc_dai_config *config)
2631 {
2632 	/* clock directions wrt codec */
2633 	if (hw_config->bclk_provider == SND_SOC_TPLG_BCLK_CP) {
2634 		/* codec is bclk provider */
2635 		if (hw_config->fsync_provider == SND_SOC_TPLG_FSYNC_CP)
2636 			config->format |= SOF_DAI_FMT_CBP_CFP;
2637 		else
2638 			config->format |= SOF_DAI_FMT_CBP_CFC;
2639 	} else {
2640 		/* codec is bclk consumer */
2641 		if (hw_config->fsync_provider == SND_SOC_TPLG_FSYNC_CP)
2642 			config->format |= SOF_DAI_FMT_CBC_CFP;
2643 		else
2644 			config->format |= SOF_DAI_FMT_CBC_CFC;
2645 	}
2646 
2647 	/* inverted clocks ? */
2648 	if (hw_config->invert_bclk) {
2649 		if (hw_config->invert_fsync)
2650 			config->format |= SOF_DAI_FMT_IB_IF;
2651 		else
2652 			config->format |= SOF_DAI_FMT_IB_NF;
2653 	} else {
2654 		if (hw_config->invert_fsync)
2655 			config->format |= SOF_DAI_FMT_NB_IF;
2656 		else
2657 			config->format |= SOF_DAI_FMT_NB_NF;
2658 	}
2659 }
2660 
2661 /*
2662  * Send IPC and set the same config for all DAIs with name matching the link
2663  * name. Note that the function can only be used for the case that all DAIs
2664  * have a common DAI config for now.
2665  */
2666 static int sof_set_dai_config_multi(struct snd_sof_dev *sdev, u32 size,
2667 				    struct snd_soc_dai_link *link,
2668 				    struct sof_ipc_dai_config *config,
2669 				    int num_conf, int curr_conf)
2670 {
2671 	struct snd_sof_dai *dai;
2672 	int found = 0;
2673 	int i;
2674 
2675 	list_for_each_entry(dai, &sdev->dai_list, list) {
2676 		if (!dai->name)
2677 			continue;
2678 
2679 		if (strcmp(link->name, dai->name) == 0) {
2680 			/*
2681 			 * the same dai config will be applied to all DAIs in
2682 			 * the same dai link. We have to ensure that the ipc
2683 			 * dai config's dai_index match to the component's
2684 			 * dai_index.
2685 			 */
2686 			for (i = 0; i < num_conf; i++)
2687 				config[i].dai_index = dai->comp_dai.dai_index;
2688 
2689 			dev_dbg(sdev->dev, "set DAI config for %s index %d\n",
2690 				dai->name, config[curr_conf].dai_index);
2691 
2692 			dai->number_configs = num_conf;
2693 			dai->current_config = curr_conf;
2694 			dai->dai_config = kmemdup(config, size * num_conf, GFP_KERNEL);
2695 			if (!dai->dai_config)
2696 				return -ENOMEM;
2697 
2698 			found = 1;
2699 		}
2700 	}
2701 
2702 	/*
2703 	 * machine driver may define a dai link with playback and capture
2704 	 * dai enabled, but the dai link in topology would support both, one
2705 	 * or none of them. Here print a warning message to notify user
2706 	 */
2707 	if (!found) {
2708 		dev_warn(sdev->dev, "warning: failed to find dai for dai link %s",
2709 			 link->name);
2710 	}
2711 
2712 	return 0;
2713 }
2714 
2715 static int sof_set_dai_config(struct snd_sof_dev *sdev, u32 size,
2716 			      struct snd_soc_dai_link *link,
2717 			      struct sof_ipc_dai_config *config)
2718 {
2719 	return sof_set_dai_config_multi(sdev, size, link, config, 1, 0);
2720 }
2721 
2722 static int sof_link_ssp_load(struct snd_soc_component *scomp, int index,
2723 			     struct snd_soc_dai_link *link,
2724 			     struct snd_soc_tplg_link_config *cfg,
2725 			     struct snd_soc_tplg_hw_config *hw_config,
2726 			     struct sof_ipc_dai_config *config, int curr_conf)
2727 {
2728 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2729 	struct snd_soc_tplg_private *private = &cfg->priv;
2730 	int num_conf = le32_to_cpu(cfg->num_hw_configs);
2731 	u32 size = sizeof(*config);
2732 	int ret;
2733 	int i;
2734 
2735 	/*
2736 	 * Parse common data, we should have 1 common data per hw_config.
2737 	 */
2738 	ret = sof_parse_token_sets(scomp, &config->ssp, ssp_tokens,
2739 				   ARRAY_SIZE(ssp_tokens), private->array,
2740 				   le32_to_cpu(private->size),
2741 				   num_conf, size);
2742 
2743 	if (ret != 0) {
2744 		dev_err(scomp->dev, "error: parse ssp tokens failed %d\n",
2745 			le32_to_cpu(private->size));
2746 		return ret;
2747 	}
2748 
2749 	/* process all possible hw configs */
2750 	for (i = 0; i < num_conf; i++) {
2751 
2752 		/* handle master/slave and inverted clocks */
2753 		sof_dai_set_format(&hw_config[i], &config[i]);
2754 
2755 		config[i].hdr.size = size;
2756 
2757 		/* copy differentiating hw configs to ipc structs */
2758 		config[i].ssp.mclk_rate = le32_to_cpu(hw_config[i].mclk_rate);
2759 		config[i].ssp.bclk_rate = le32_to_cpu(hw_config[i].bclk_rate);
2760 		config[i].ssp.fsync_rate = le32_to_cpu(hw_config[i].fsync_rate);
2761 		config[i].ssp.tdm_slots = le32_to_cpu(hw_config[i].tdm_slots);
2762 		config[i].ssp.tdm_slot_width = le32_to_cpu(hw_config[i].tdm_slot_width);
2763 		config[i].ssp.mclk_direction = hw_config[i].mclk_direction;
2764 		config[i].ssp.rx_slots = le32_to_cpu(hw_config[i].rx_slots);
2765 		config[i].ssp.tx_slots = le32_to_cpu(hw_config[i].tx_slots);
2766 
2767 		dev_dbg(scomp->dev, "tplg: config SSP%d fmt %#x mclk %d bclk %d fclk %d width (%d)%d slots %d mclk id %d quirks %d clks_control %#x\n",
2768 			config[i].dai_index, config[i].format,
2769 			config[i].ssp.mclk_rate, config[i].ssp.bclk_rate,
2770 			config[i].ssp.fsync_rate, config[i].ssp.sample_valid_bits,
2771 			config[i].ssp.tdm_slot_width, config[i].ssp.tdm_slots,
2772 			config[i].ssp.mclk_id, config[i].ssp.quirks, config[i].ssp.clks_control);
2773 
2774 		/* validate SSP fsync rate and channel count */
2775 		if (config[i].ssp.fsync_rate < 8000 || config[i].ssp.fsync_rate > 192000) {
2776 			dev_err(scomp->dev, "error: invalid fsync rate for SSP%d\n",
2777 				config[i].dai_index);
2778 			return -EINVAL;
2779 		}
2780 
2781 		if (config[i].ssp.tdm_slots < 1 || config[i].ssp.tdm_slots > 8) {
2782 			dev_err(scomp->dev, "error: invalid channel count for SSP%d\n",
2783 				config[i].dai_index);
2784 			return -EINVAL;
2785 		}
2786 	}
2787 
2788 	/* set config for all DAI's with name matching the link name */
2789 	ret = sof_set_dai_config_multi(sdev, size, link, config, num_conf, curr_conf);
2790 	if (ret < 0)
2791 		dev_err(scomp->dev, "error: failed to save DAI config for SSP%d\n",
2792 			config->dai_index);
2793 
2794 	return ret;
2795 }
2796 
2797 static int sof_link_sai_load(struct snd_soc_component *scomp, int index,
2798 			     struct snd_soc_dai_link *link,
2799 			     struct snd_soc_tplg_link_config *cfg,
2800 			     struct snd_soc_tplg_hw_config *hw_config,
2801 			     struct sof_ipc_dai_config *config)
2802 {
2803 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2804 	struct snd_soc_tplg_private *private = &cfg->priv;
2805 	u32 size = sizeof(*config);
2806 	int ret;
2807 
2808 	/* handle master/slave and inverted clocks */
2809 	sof_dai_set_format(hw_config, config);
2810 
2811 	/* init IPC */
2812 	memset(&config->sai, 0, sizeof(struct sof_ipc_dai_sai_params));
2813 	config->hdr.size = size;
2814 
2815 	ret = sof_parse_tokens(scomp, &config->sai, sai_tokens,
2816 			       ARRAY_SIZE(sai_tokens), private->array,
2817 			       le32_to_cpu(private->size));
2818 	if (ret != 0) {
2819 		dev_err(scomp->dev, "error: parse sai tokens failed %d\n",
2820 			le32_to_cpu(private->size));
2821 		return ret;
2822 	}
2823 
2824 	config->sai.mclk_rate = le32_to_cpu(hw_config->mclk_rate);
2825 	config->sai.bclk_rate = le32_to_cpu(hw_config->bclk_rate);
2826 	config->sai.fsync_rate = le32_to_cpu(hw_config->fsync_rate);
2827 	config->sai.mclk_direction = hw_config->mclk_direction;
2828 
2829 	config->sai.tdm_slots = le32_to_cpu(hw_config->tdm_slots);
2830 	config->sai.tdm_slot_width = le32_to_cpu(hw_config->tdm_slot_width);
2831 	config->sai.rx_slots = le32_to_cpu(hw_config->rx_slots);
2832 	config->sai.tx_slots = le32_to_cpu(hw_config->tx_slots);
2833 
2834 	dev_info(scomp->dev,
2835 		 "tplg: config SAI%d fmt 0x%x mclk %d width %d slots %d mclk id %d\n",
2836 		config->dai_index, config->format,
2837 		config->sai.mclk_rate, config->sai.tdm_slot_width,
2838 		config->sai.tdm_slots, config->sai.mclk_id);
2839 
2840 	if (config->sai.tdm_slots < 1 || config->sai.tdm_slots > 8) {
2841 		dev_err(scomp->dev, "error: invalid channel count for SAI%d\n",
2842 			config->dai_index);
2843 		return -EINVAL;
2844 	}
2845 
2846 	/* set config for all DAI's with name matching the link name */
2847 	ret = sof_set_dai_config(sdev, size, link, config);
2848 	if (ret < 0)
2849 		dev_err(scomp->dev, "error: failed to save DAI config for SAI%d\n",
2850 			config->dai_index);
2851 
2852 	return ret;
2853 }
2854 
2855 static int sof_link_esai_load(struct snd_soc_component *scomp, int index,
2856 			      struct snd_soc_dai_link *link,
2857 			      struct snd_soc_tplg_link_config *cfg,
2858 			      struct snd_soc_tplg_hw_config *hw_config,
2859 			      struct sof_ipc_dai_config *config)
2860 {
2861 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2862 	struct snd_soc_tplg_private *private = &cfg->priv;
2863 	u32 size = sizeof(*config);
2864 	int ret;
2865 
2866 	/* handle master/slave and inverted clocks */
2867 	sof_dai_set_format(hw_config, config);
2868 
2869 	/* init IPC */
2870 	memset(&config->esai, 0, sizeof(struct sof_ipc_dai_esai_params));
2871 	config->hdr.size = size;
2872 
2873 	ret = sof_parse_tokens(scomp, &config->esai, esai_tokens,
2874 			       ARRAY_SIZE(esai_tokens), private->array,
2875 			       le32_to_cpu(private->size));
2876 	if (ret != 0) {
2877 		dev_err(scomp->dev, "error: parse esai tokens failed %d\n",
2878 			le32_to_cpu(private->size));
2879 		return ret;
2880 	}
2881 
2882 	config->esai.mclk_rate = le32_to_cpu(hw_config->mclk_rate);
2883 	config->esai.bclk_rate = le32_to_cpu(hw_config->bclk_rate);
2884 	config->esai.fsync_rate = le32_to_cpu(hw_config->fsync_rate);
2885 	config->esai.mclk_direction = hw_config->mclk_direction;
2886 	config->esai.tdm_slots = le32_to_cpu(hw_config->tdm_slots);
2887 	config->esai.tdm_slot_width = le32_to_cpu(hw_config->tdm_slot_width);
2888 	config->esai.rx_slots = le32_to_cpu(hw_config->rx_slots);
2889 	config->esai.tx_slots = le32_to_cpu(hw_config->tx_slots);
2890 
2891 	dev_info(scomp->dev,
2892 		 "tplg: config ESAI%d fmt 0x%x mclk %d width %d slots %d mclk id %d\n",
2893 		config->dai_index, config->format,
2894 		config->esai.mclk_rate, config->esai.tdm_slot_width,
2895 		config->esai.tdm_slots, config->esai.mclk_id);
2896 
2897 	if (config->esai.tdm_slots < 1 || config->esai.tdm_slots > 8) {
2898 		dev_err(scomp->dev, "error: invalid channel count for ESAI%d\n",
2899 			config->dai_index);
2900 		return -EINVAL;
2901 	}
2902 
2903 	/* set config for all DAI's with name matching the link name */
2904 	ret = sof_set_dai_config(sdev, size, link, config);
2905 	if (ret < 0)
2906 		dev_err(scomp->dev, "error: failed to save DAI config for ESAI%d\n",
2907 			config->dai_index);
2908 
2909 	return ret;
2910 }
2911 
2912 static int sof_link_acp_dmic_load(struct snd_soc_component *scomp, int index,
2913 				  struct snd_soc_dai_link *link,
2914 				  struct snd_soc_tplg_link_config *cfg,
2915 				  struct snd_soc_tplg_hw_config *hw_config,
2916 				  struct sof_ipc_dai_config *config)
2917 {
2918 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2919 	u32 size = sizeof(*config);
2920 	int ret;
2921 
2922        /* handle master/slave and inverted clocks */
2923 	sof_dai_set_format(hw_config, config);
2924 
2925 	/* init IPC */
2926 	memset(&config->acpdmic, 0, sizeof(struct sof_ipc_dai_acp_params));
2927 	config->hdr.size = size;
2928 
2929 	config->acpdmic.fsync_rate = le32_to_cpu(hw_config->fsync_rate);
2930 	config->acpdmic.tdm_slots = le32_to_cpu(hw_config->tdm_slots);
2931 
2932 	dev_info(scomp->dev, "ACP_DMIC config ACP%d channel %d rate %d\n",
2933 		 config->dai_index, config->acpdmic.tdm_slots,
2934 		 config->acpdmic.fsync_rate);
2935 
2936 	/* set config for all DAI's with name matching the link name */
2937 	ret = sof_set_dai_config(sdev, size, link, config);
2938 	if (ret < 0)
2939 		dev_err(scomp->dev, "ACP_DMIC failed to save DAI config for ACP%d\n",
2940 			config->dai_index);
2941 	return ret;
2942 }
2943 
2944 static int sof_link_acp_bt_load(struct snd_soc_component *scomp, int index,
2945 				struct snd_soc_dai_link *link,
2946 				struct snd_soc_tplg_link_config *cfg,
2947 				struct snd_soc_tplg_hw_config *hw_config,
2948 				struct sof_ipc_dai_config *config)
2949 {
2950 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2951 	u32 size = sizeof(*config);
2952 	int ret;
2953 
2954 	/* handle master/slave and inverted clocks */
2955 	sof_dai_set_format(hw_config, config);
2956 
2957 	/* init IPC */
2958 	memset(&config->acpbt, 0, sizeof(struct sof_ipc_dai_acp_params));
2959 	config->hdr.size = size;
2960 
2961 	config->acpbt.fsync_rate = le32_to_cpu(hw_config->fsync_rate);
2962 	config->acpbt.tdm_slots = le32_to_cpu(hw_config->tdm_slots);
2963 
2964 	dev_info(scomp->dev, "ACP_BT config ACP%d channel %d rate %d\n",
2965 		 config->dai_index, config->acpbt.tdm_slots,
2966 		 config->acpbt.fsync_rate);
2967 
2968 	/* set config for all DAI's with name matching the link name */
2969 	ret = sof_set_dai_config(sdev, size, link, config);
2970 	if (ret < 0)
2971 		dev_err(scomp->dev, "ACP_BT failed to save DAI config for ACP%d\n",
2972 			config->dai_index);
2973 	return ret;
2974 }
2975 
2976 static int sof_link_acp_sp_load(struct snd_soc_component *scomp, int index,
2977 				struct snd_soc_dai_link *link,
2978 				struct snd_soc_tplg_link_config *cfg,
2979 				struct snd_soc_tplg_hw_config *hw_config,
2980 				struct sof_ipc_dai_config *config)
2981 {
2982 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2983 	u32 size = sizeof(*config);
2984 	int ret;
2985 
2986 	/* handle master/slave and inverted clocks */
2987 	sof_dai_set_format(hw_config, config);
2988 
2989 	/* init IPC */
2990 	memset(&config->acpsp, 0, sizeof(struct sof_ipc_dai_acp_params));
2991 	config->hdr.size = size;
2992 
2993 	config->acpsp.fsync_rate = le32_to_cpu(hw_config->fsync_rate);
2994 	config->acpsp.tdm_slots = le32_to_cpu(hw_config->tdm_slots);
2995 
2996 	dev_info(scomp->dev, "ACP_SP config ACP%d channel %d rate %d\n",
2997 		 config->dai_index, config->acpsp.tdm_slots,
2998 		 config->acpsp.fsync_rate);
2999 
3000 	/* set config for all DAI's with name matching the link name */
3001 	ret = sof_set_dai_config(sdev, size, link, config);
3002 	if (ret < 0)
3003 		dev_err(scomp->dev, "ACP_SP failed to save DAI config for ACP%d\n",
3004 			config->dai_index);
3005 	return ret;
3006 }
3007 
3008 static int sof_link_afe_load(struct snd_soc_component *scomp, int index,
3009 			     struct snd_soc_dai_link *link,
3010 			     struct snd_soc_tplg_link_config *cfg,
3011 			     struct snd_soc_tplg_hw_config *hw_config,
3012 			     struct sof_ipc_dai_config *config)
3013 {
3014 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3015 	struct snd_soc_tplg_private *private = &cfg->priv;
3016 	struct snd_soc_dai *dai;
3017 	u32 size = sizeof(*config);
3018 	int ret;
3019 
3020 	config->hdr.size = size;
3021 
3022 	/* get any bespoke DAI tokens */
3023 	ret = sof_parse_tokens(scomp, &config->afe, afe_tokens,
3024 			       ARRAY_SIZE(afe_tokens), private->array,
3025 			       le32_to_cpu(private->size));
3026 	if (ret != 0) {
3027 		dev_err(scomp->dev, "parse afe tokens failed %d\n",
3028 			le32_to_cpu(private->size));
3029 		return ret;
3030 	}
3031 
3032 	dev_dbg(scomp->dev, "AFE config rate %d channels %d format:%d\n",
3033 		config->afe.rate, config->afe.channels, config->afe.format);
3034 
3035 	dai = snd_soc_find_dai(link->cpus);
3036 	if (!dai) {
3037 		dev_err(scomp->dev, "%s: failed to find dai %s", __func__, link->cpus->dai_name);
3038 		return -EINVAL;
3039 	}
3040 
3041 	config->afe.stream_id = DMA_CHAN_INVALID;
3042 
3043 	ret = sof_set_dai_config(sdev, size, link, config);
3044 	if (ret < 0)
3045 		dev_err(scomp->dev, "failed to process afe dai link %s", link->name);
3046 
3047 	return ret;
3048 }
3049 
3050 static int sof_link_dmic_load(struct snd_soc_component *scomp, int index,
3051 			      struct snd_soc_dai_link *link,
3052 			      struct snd_soc_tplg_link_config *cfg,
3053 			      struct snd_soc_tplg_hw_config *hw_config,
3054 			      struct sof_ipc_dai_config *config)
3055 {
3056 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3057 	struct snd_soc_tplg_private *private = &cfg->priv;
3058 	struct sof_ipc_fw_ready *ready = &sdev->fw_ready;
3059 	struct sof_ipc_fw_version *v = &ready->version;
3060 	size_t size = sizeof(*config);
3061 	int ret, j;
3062 
3063 	/* Ensure the entire DMIC config struct is zeros */
3064 	memset(&config->dmic, 0, sizeof(struct sof_ipc_dai_dmic_params));
3065 
3066 	/* get DMIC tokens */
3067 	ret = sof_parse_tokens(scomp, &config->dmic, dmic_tokens,
3068 			       ARRAY_SIZE(dmic_tokens), private->array,
3069 			       le32_to_cpu(private->size));
3070 	if (ret != 0) {
3071 		dev_err(scomp->dev, "error: parse dmic tokens failed %d\n",
3072 			le32_to_cpu(private->size));
3073 		return ret;
3074 	}
3075 
3076 	/* get DMIC PDM tokens */
3077 	ret = sof_parse_token_sets(scomp, &config->dmic.pdm[0], dmic_pdm_tokens,
3078 			       ARRAY_SIZE(dmic_pdm_tokens), private->array,
3079 			       le32_to_cpu(private->size),
3080 			       config->dmic.num_pdm_active,
3081 			       sizeof(struct sof_ipc_dai_dmic_pdm_ctrl));
3082 
3083 	if (ret != 0) {
3084 		dev_err(scomp->dev, "error: parse dmic pdm tokens failed %d\n",
3085 			le32_to_cpu(private->size));
3086 		return ret;
3087 	}
3088 
3089 	/* set IPC header size */
3090 	config->hdr.size = size;
3091 
3092 	/* debug messages */
3093 	dev_dbg(scomp->dev, "tplg: config DMIC%d driver version %d\n",
3094 		config->dai_index, config->dmic.driver_ipc_version);
3095 	dev_dbg(scomp->dev, "pdmclk_min %d pdm_clkmax %d duty_min %hd\n",
3096 		config->dmic.pdmclk_min, config->dmic.pdmclk_max,
3097 		config->dmic.duty_min);
3098 	dev_dbg(scomp->dev, "duty_max %hd fifo_fs %d num_pdms active %d\n",
3099 		config->dmic.duty_max, config->dmic.fifo_fs,
3100 		config->dmic.num_pdm_active);
3101 	dev_dbg(scomp->dev, "fifo word length %hd\n", config->dmic.fifo_bits);
3102 
3103 	for (j = 0; j < config->dmic.num_pdm_active; j++) {
3104 		dev_dbg(scomp->dev, "pdm %hd mic a %hd mic b %hd\n",
3105 			config->dmic.pdm[j].id,
3106 			config->dmic.pdm[j].enable_mic_a,
3107 			config->dmic.pdm[j].enable_mic_b);
3108 		dev_dbg(scomp->dev, "pdm %hd polarity a %hd polarity b %hd\n",
3109 			config->dmic.pdm[j].id,
3110 			config->dmic.pdm[j].polarity_mic_a,
3111 			config->dmic.pdm[j].polarity_mic_b);
3112 		dev_dbg(scomp->dev, "pdm %hd clk_edge %hd skew %hd\n",
3113 			config->dmic.pdm[j].id,
3114 			config->dmic.pdm[j].clk_edge,
3115 			config->dmic.pdm[j].skew);
3116 	}
3117 
3118 	/*
3119 	 * this takes care of backwards compatible handling of fifo_bits_b.
3120 	 * It is deprecated since firmware ABI version 3.0.1.
3121 	 */
3122 	if (SOF_ABI_VER(v->major, v->minor, v->micro) < SOF_ABI_VER(3, 0, 1))
3123 		config->dmic.fifo_bits_b = config->dmic.fifo_bits;
3124 
3125 	/* set config for all DAI's with name matching the link name */
3126 	ret = sof_set_dai_config(sdev, size, link, config);
3127 	if (ret < 0)
3128 		dev_err(scomp->dev, "error: failed to save DAI config for DMIC%d\n",
3129 			config->dai_index);
3130 
3131 	return ret;
3132 }
3133 
3134 static int sof_link_hda_load(struct snd_soc_component *scomp, int index,
3135 			     struct snd_soc_dai_link *link,
3136 			     struct snd_soc_tplg_link_config *cfg,
3137 			     struct snd_soc_tplg_hw_config *hw_config,
3138 			     struct sof_ipc_dai_config *config)
3139 {
3140 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3141 	struct snd_soc_tplg_private *private = &cfg->priv;
3142 	struct snd_soc_dai *dai;
3143 	u32 size = sizeof(*config);
3144 	int ret;
3145 
3146 	/* init IPC */
3147 	memset(&config->hda, 0, sizeof(struct sof_ipc_dai_hda_params));
3148 	config->hdr.size = size;
3149 
3150 	/* get any bespoke DAI tokens */
3151 	ret = sof_parse_tokens(scomp, &config->hda, hda_tokens,
3152 			       ARRAY_SIZE(hda_tokens), private->array,
3153 			       le32_to_cpu(private->size));
3154 	if (ret != 0) {
3155 		dev_err(scomp->dev, "error: parse hda tokens failed %d\n",
3156 			le32_to_cpu(private->size));
3157 		return ret;
3158 	}
3159 
3160 	dev_dbg(scomp->dev, "HDA config rate %d channels %d\n",
3161 		config->hda.rate, config->hda.channels);
3162 
3163 	dai = snd_soc_find_dai(link->cpus);
3164 	if (!dai) {
3165 		dev_err(scomp->dev, "error: failed to find dai %s in %s",
3166 			link->cpus->dai_name, __func__);
3167 		return -EINVAL;
3168 	}
3169 
3170 	config->hda.link_dma_ch = DMA_CHAN_INVALID;
3171 
3172 	ret = sof_set_dai_config(sdev, size, link, config);
3173 	if (ret < 0)
3174 		dev_err(scomp->dev, "error: failed to process hda dai link %s",
3175 			link->name);
3176 
3177 	return ret;
3178 }
3179 
3180 static int sof_link_alh_load(struct snd_soc_component *scomp, int index,
3181 			     struct snd_soc_dai_link *link,
3182 			     struct snd_soc_tplg_link_config *cfg,
3183 			     struct snd_soc_tplg_hw_config *hw_config,
3184 			     struct sof_ipc_dai_config *config)
3185 {
3186 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3187 	struct snd_soc_tplg_private *private = &cfg->priv;
3188 	u32 size = sizeof(*config);
3189 	int ret;
3190 
3191 	ret = sof_parse_tokens(scomp, &config->alh, alh_tokens,
3192 			       ARRAY_SIZE(alh_tokens), private->array,
3193 			       le32_to_cpu(private->size));
3194 	if (ret != 0) {
3195 		dev_err(scomp->dev, "error: parse alh tokens failed %d\n",
3196 			le32_to_cpu(private->size));
3197 		return ret;
3198 	}
3199 
3200 	/* init IPC */
3201 	config->hdr.size = size;
3202 
3203 	/* set config for all DAI's with name matching the link name */
3204 	ret = sof_set_dai_config(sdev, size, link, config);
3205 	if (ret < 0)
3206 		dev_err(scomp->dev, "error: failed to save DAI config for ALH %d\n",
3207 			config->dai_index);
3208 
3209 	return ret;
3210 }
3211 
3212 /* DAI link - used for any driver specific init */
3213 static int sof_link_load(struct snd_soc_component *scomp, int index,
3214 			 struct snd_soc_dai_link *link,
3215 			 struct snd_soc_tplg_link_config *cfg)
3216 {
3217 	struct snd_soc_tplg_private *private = &cfg->priv;
3218 	struct snd_soc_tplg_hw_config *hw_config;
3219 	struct sof_ipc_dai_config common_config;
3220 	struct sof_ipc_dai_config *config;
3221 	int curr_conf;
3222 	int num_conf;
3223 	int ret;
3224 	int i;
3225 
3226 	if (!link->platforms) {
3227 		dev_err(scomp->dev, "error: no platforms\n");
3228 		return -EINVAL;
3229 	}
3230 	link->platforms->name = dev_name(scomp->dev);
3231 
3232 	/*
3233 	 * Set nonatomic property for FE dai links as their trigger action
3234 	 * involves IPC's.
3235 	 */
3236 	if (!link->no_pcm) {
3237 		link->nonatomic = true;
3238 
3239 		/*
3240 		 * set default trigger order for all links. Exceptions to
3241 		 * the rule will be handled in sof_pcm_dai_link_fixup()
3242 		 * For playback, the sequence is the following: start FE,
3243 		 * start BE, stop BE, stop FE; for Capture the sequence is
3244 		 * inverted start BE, start FE, stop FE, stop BE
3245 		 */
3246 		link->trigger[SNDRV_PCM_STREAM_PLAYBACK] =
3247 					SND_SOC_DPCM_TRIGGER_PRE;
3248 		link->trigger[SNDRV_PCM_STREAM_CAPTURE] =
3249 					SND_SOC_DPCM_TRIGGER_POST;
3250 
3251 		/* nothing more to do for FE dai links */
3252 		return 0;
3253 	}
3254 
3255 	/* check we have some tokens - we need at least DAI type */
3256 	if (le32_to_cpu(private->size) == 0) {
3257 		dev_err(scomp->dev, "error: expected tokens for DAI, none found\n");
3258 		return -EINVAL;
3259 	}
3260 
3261 	memset(&common_config, 0, sizeof(common_config));
3262 
3263 	/* get any common DAI tokens */
3264 	ret = sof_parse_tokens(scomp, &common_config, dai_link_tokens, ARRAY_SIZE(dai_link_tokens),
3265 			       private->array, le32_to_cpu(private->size));
3266 	if (ret != 0) {
3267 		dev_err(scomp->dev, "error: parse link tokens failed %d\n",
3268 			le32_to_cpu(private->size));
3269 		return ret;
3270 	}
3271 
3272 	/*
3273 	 * DAI links are expected to have at least 1 hw_config.
3274 	 * But some older topologies might have no hw_config for HDA dai links.
3275 	 */
3276 	hw_config = cfg->hw_config;
3277 	num_conf = le32_to_cpu(cfg->num_hw_configs);
3278 	if (!num_conf) {
3279 		if (common_config.type != SOF_DAI_INTEL_HDA) {
3280 			dev_err(scomp->dev, "error: unexpected DAI config count %d!\n",
3281 				le32_to_cpu(cfg->num_hw_configs));
3282 			return -EINVAL;
3283 		}
3284 		num_conf = 1;
3285 		curr_conf = 0;
3286 	} else {
3287 		dev_dbg(scomp->dev, "tplg: %d hw_configs found, default id: %d!\n",
3288 			cfg->num_hw_configs, le32_to_cpu(cfg->default_hw_config_id));
3289 
3290 		for (curr_conf = 0; curr_conf < num_conf; curr_conf++) {
3291 			if (hw_config[curr_conf].id == cfg->default_hw_config_id)
3292 				break;
3293 		}
3294 
3295 		if (curr_conf == num_conf) {
3296 			dev_err(scomp->dev, "error: default hw_config id: %d not found!\n",
3297 				le32_to_cpu(cfg->default_hw_config_id));
3298 			return -EINVAL;
3299 		}
3300 	}
3301 
3302 	/* Reserve memory for all hw configs, eventually freed by widget */
3303 	config = kcalloc(num_conf, sizeof(*config), GFP_KERNEL);
3304 	if (!config)
3305 		return -ENOMEM;
3306 
3307 	/* Copy common data to all config ipc structs */
3308 	for (i = 0; i < num_conf; i++) {
3309 		config[i].hdr.cmd = SOF_IPC_GLB_DAI_MSG | SOF_IPC_DAI_CONFIG;
3310 		config[i].format = le32_to_cpu(hw_config[i].fmt);
3311 		config[i].type = common_config.type;
3312 		config[i].dai_index = common_config.dai_index;
3313 	}
3314 
3315 	/* now load DAI specific data and send IPC - type comes from token */
3316 	switch (common_config.type) {
3317 	case SOF_DAI_INTEL_SSP:
3318 		ret = sof_link_ssp_load(scomp, index, link, cfg, hw_config, config, curr_conf);
3319 		break;
3320 	case SOF_DAI_INTEL_DMIC:
3321 		ret = sof_link_dmic_load(scomp, index, link, cfg, hw_config + curr_conf, config);
3322 		break;
3323 	case SOF_DAI_INTEL_HDA:
3324 		ret = sof_link_hda_load(scomp, index, link, cfg, hw_config + curr_conf, config);
3325 		break;
3326 	case SOF_DAI_INTEL_ALH:
3327 		ret = sof_link_alh_load(scomp, index, link, cfg, hw_config + curr_conf, config);
3328 		break;
3329 	case SOF_DAI_IMX_SAI:
3330 		ret = sof_link_sai_load(scomp, index, link, cfg, hw_config + curr_conf, config);
3331 		break;
3332 	case SOF_DAI_IMX_ESAI:
3333 		ret = sof_link_esai_load(scomp, index, link, cfg, hw_config + curr_conf, config);
3334 		break;
3335 	case SOF_DAI_AMD_BT:
3336 		ret = sof_link_acp_bt_load(scomp, index, link, cfg, hw_config + curr_conf, config);
3337 		break;
3338 	case SOF_DAI_AMD_SP:
3339 		ret = sof_link_acp_sp_load(scomp, index, link, cfg, hw_config + curr_conf, config);
3340 		break;
3341 	case SOF_DAI_AMD_DMIC:
3342 		ret = sof_link_acp_dmic_load(scomp, index, link, cfg, hw_config + curr_conf,
3343 					     config);
3344 		break;
3345 	case SOF_DAI_MEDIATEK_AFE:
3346 		ret = sof_link_afe_load(scomp, index, link, cfg, hw_config + curr_conf, config);
3347 		break;
3348 	default:
3349 		dev_err(scomp->dev, "error: invalid DAI type %d\n", common_config.type);
3350 		ret = -EINVAL;
3351 		break;
3352 	}
3353 
3354 	kfree(config);
3355 
3356 	return ret;
3357 }
3358 
3359 /* DAI link - used for any driver specific init */
3360 static int sof_route_load(struct snd_soc_component *scomp, int index,
3361 			  struct snd_soc_dapm_route *route)
3362 {
3363 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3364 	struct sof_ipc_pipe_comp_connect *connect;
3365 	struct snd_sof_widget *source_swidget, *sink_swidget;
3366 	struct snd_soc_dobj *dobj = &route->dobj;
3367 	struct snd_sof_route *sroute;
3368 	int ret = 0;
3369 
3370 	/* allocate memory for sroute and connect */
3371 	sroute = kzalloc(sizeof(*sroute), GFP_KERNEL);
3372 	if (!sroute)
3373 		return -ENOMEM;
3374 
3375 	sroute->scomp = scomp;
3376 
3377 	connect = kzalloc(sizeof(*connect), GFP_KERNEL);
3378 	if (!connect) {
3379 		kfree(sroute);
3380 		return -ENOMEM;
3381 	}
3382 
3383 	connect->hdr.size = sizeof(*connect);
3384 	connect->hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_CONNECT;
3385 
3386 	dev_dbg(scomp->dev, "sink %s control %s source %s\n",
3387 		route->sink, route->control ? route->control : "none",
3388 		route->source);
3389 
3390 	/* source component */
3391 	source_swidget = snd_sof_find_swidget(scomp, (char *)route->source);
3392 	if (!source_swidget) {
3393 		dev_err(scomp->dev, "error: source %s not found\n",
3394 			route->source);
3395 		ret = -EINVAL;
3396 		goto err;
3397 	}
3398 
3399 	/*
3400 	 * Virtual widgets of type output/out_drv may be added in topology
3401 	 * for compatibility. These are not handled by the FW.
3402 	 * So, don't send routes whose source/sink widget is of such types
3403 	 * to the DSP.
3404 	 */
3405 	if (source_swidget->id == snd_soc_dapm_out_drv ||
3406 	    source_swidget->id == snd_soc_dapm_output)
3407 		goto err;
3408 
3409 	connect->source_id = source_swidget->comp_id;
3410 
3411 	/* sink component */
3412 	sink_swidget = snd_sof_find_swidget(scomp, (char *)route->sink);
3413 	if (!sink_swidget) {
3414 		dev_err(scomp->dev, "error: sink %s not found\n",
3415 			route->sink);
3416 		ret = -EINVAL;
3417 		goto err;
3418 	}
3419 
3420 	/*
3421 	 * Don't send routes whose sink widget is of type
3422 	 * output or out_drv to the DSP
3423 	 */
3424 	if (sink_swidget->id == snd_soc_dapm_out_drv ||
3425 	    sink_swidget->id == snd_soc_dapm_output)
3426 		goto err;
3427 
3428 	connect->sink_id = sink_swidget->comp_id;
3429 
3430 	/*
3431 	 * For virtual routes, both sink and source are not
3432 	 * buffer. Since only buffer linked to component is supported by
3433 	 * FW, others are reported as error, add check in route function,
3434 	 * do not send it to FW when both source and sink are not buffer
3435 	 */
3436 	if (source_swidget->id != snd_soc_dapm_buffer &&
3437 	    sink_swidget->id != snd_soc_dapm_buffer) {
3438 		dev_dbg(scomp->dev, "warning: neither Linked source component %s nor sink component %s is of buffer type, ignoring link\n",
3439 			route->source, route->sink);
3440 		goto err;
3441 	} else {
3442 		sroute->route = route;
3443 		dobj->private = sroute;
3444 		sroute->private = connect;
3445 		sroute->src_widget = source_swidget;
3446 		sroute->sink_widget = sink_swidget;
3447 
3448 		/* add route to route list */
3449 		list_add(&sroute->list, &sdev->route_list);
3450 
3451 		return 0;
3452 	}
3453 
3454 err:
3455 	kfree(connect);
3456 	kfree(sroute);
3457 	return ret;
3458 }
3459 
3460 int snd_sof_complete_pipeline(struct snd_sof_dev *sdev,
3461 			      struct snd_sof_widget *swidget)
3462 {
3463 	struct sof_ipc_pipe_ready ready;
3464 	struct sof_ipc_reply reply;
3465 	int ret;
3466 
3467 	dev_dbg(sdev->dev, "tplg: complete pipeline %s id %d\n",
3468 		swidget->widget->name, swidget->comp_id);
3469 
3470 	memset(&ready, 0, sizeof(ready));
3471 	ready.hdr.size = sizeof(ready);
3472 	ready.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_PIPE_COMPLETE;
3473 	ready.comp_id = swidget->comp_id;
3474 
3475 	ret = sof_ipc_tx_message(sdev->ipc,
3476 				 ready.hdr.cmd, &ready, sizeof(ready), &reply,
3477 				 sizeof(reply));
3478 	if (ret < 0)
3479 		return ret;
3480 	return 1;
3481 }
3482 
3483 /**
3484  * sof_set_pipe_widget - Set pipe_widget for a component
3485  * @sdev: pointer to struct snd_sof_dev
3486  * @pipe_widget: pointer to struct snd_sof_widget of type snd_soc_dapm_scheduler
3487  * @swidget: pointer to struct snd_sof_widget that has the same pipeline ID as @pipe_widget
3488  *
3489  * Return: 0 if successful, -EINVAL on error.
3490  * The function checks if @swidget is associated with any volatile controls. If so, setting
3491  * the dynamic_pipeline_widget is disallowed.
3492  */
3493 static int sof_set_pipe_widget(struct snd_sof_dev *sdev, struct snd_sof_widget *pipe_widget,
3494 			       struct snd_sof_widget *swidget)
3495 {
3496 	struct snd_sof_control *scontrol;
3497 
3498 	if (pipe_widget->dynamic_pipeline_widget) {
3499 		/* dynamic widgets cannot have volatile kcontrols */
3500 		list_for_each_entry(scontrol, &sdev->kcontrol_list, list)
3501 			if (scontrol->comp_id == swidget->comp_id &&
3502 			    (scontrol->access & SNDRV_CTL_ELEM_ACCESS_VOLATILE)) {
3503 				dev_err(sdev->dev,
3504 					"error: volatile control found for dynamic widget %s\n",
3505 					swidget->widget->name);
3506 				return -EINVAL;
3507 			}
3508 	}
3509 
3510 	/* set the pipe_widget and apply the dynamic_pipeline_widget_flag */
3511 	swidget->pipe_widget = pipe_widget;
3512 	swidget->dynamic_pipeline_widget = pipe_widget->dynamic_pipeline_widget;
3513 
3514 	return 0;
3515 }
3516 
3517 /* completion - called at completion of firmware loading */
3518 static int sof_complete(struct snd_soc_component *scomp)
3519 {
3520 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3521 	struct snd_sof_widget *swidget, *comp_swidget;
3522 	int ret;
3523 
3524 	/* set the pipe_widget and apply the dynamic_pipeline_widget_flag */
3525 	list_for_each_entry(swidget, &sdev->widget_list, list) {
3526 		switch (swidget->id) {
3527 		case snd_soc_dapm_scheduler:
3528 			/*
3529 			 * Apply the dynamic_pipeline_widget flag and set the pipe_widget field
3530 			 * for all widgets that have the same pipeline ID as the scheduler widget
3531 			 */
3532 			list_for_each_entry(comp_swidget, &sdev->widget_list, list)
3533 				if (comp_swidget->pipeline_id == swidget->pipeline_id) {
3534 					ret = sof_set_pipe_widget(sdev, swidget, comp_swidget);
3535 					if (ret < 0)
3536 						return ret;
3537 				}
3538 			break;
3539 		default:
3540 			break;
3541 		}
3542 	}
3543 
3544 	/* verify topology components loading including dynamic pipelines */
3545 	if (sof_debug_check_flag(SOF_DBG_VERIFY_TPLG)) {
3546 		ret = sof_set_up_pipelines(sdev, true);
3547 		if (ret < 0) {
3548 			dev_err(sdev->dev, "error: topology verification failed %d\n", ret);
3549 			return ret;
3550 		}
3551 
3552 		ret = sof_tear_down_pipelines(sdev, true);
3553 		if (ret < 0) {
3554 			dev_err(sdev->dev, "error: topology tear down pipelines failed %d\n", ret);
3555 			return ret;
3556 		}
3557 	}
3558 
3559 	/* set up static pipelines */
3560 	return sof_set_up_pipelines(sdev, false);
3561 }
3562 
3563 /* manifest - optional to inform component of manifest */
3564 static int sof_manifest(struct snd_soc_component *scomp, int index,
3565 			struct snd_soc_tplg_manifest *man)
3566 {
3567 	u32 size;
3568 	u32 abi_version;
3569 
3570 	size = le32_to_cpu(man->priv.size);
3571 
3572 	/* backward compatible with tplg without ABI info */
3573 	if (!size) {
3574 		dev_dbg(scomp->dev, "No topology ABI info\n");
3575 		return 0;
3576 	}
3577 
3578 	if (size != SOF_TPLG_ABI_SIZE) {
3579 		dev_err(scomp->dev, "error: invalid topology ABI size\n");
3580 		return -EINVAL;
3581 	}
3582 
3583 	dev_info(scomp->dev,
3584 		 "Topology: ABI %d:%d:%d Kernel ABI %d:%d:%d\n",
3585 		 man->priv.data[0], man->priv.data[1],
3586 		 man->priv.data[2], SOF_ABI_MAJOR, SOF_ABI_MINOR,
3587 		 SOF_ABI_PATCH);
3588 
3589 	abi_version = SOF_ABI_VER(man->priv.data[0],
3590 				  man->priv.data[1],
3591 				  man->priv.data[2]);
3592 
3593 	if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, abi_version)) {
3594 		dev_err(scomp->dev, "error: incompatible topology ABI version\n");
3595 		return -EINVAL;
3596 	}
3597 
3598 	if (SOF_ABI_VERSION_MINOR(abi_version) > SOF_ABI_MINOR) {
3599 		if (!IS_ENABLED(CONFIG_SND_SOC_SOF_STRICT_ABI_CHECKS)) {
3600 			dev_warn(scomp->dev, "warn: topology ABI is more recent than kernel\n");
3601 		} else {
3602 			dev_err(scomp->dev, "error: topology ABI is more recent than kernel\n");
3603 			return -EINVAL;
3604 		}
3605 	}
3606 
3607 	return 0;
3608 }
3609 
3610 /* vendor specific kcontrol handlers available for binding */
3611 static const struct snd_soc_tplg_kcontrol_ops sof_io_ops[] = {
3612 	{SOF_TPLG_KCTL_VOL_ID, snd_sof_volume_get, snd_sof_volume_put},
3613 	{SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_get, snd_sof_bytes_put},
3614 	{SOF_TPLG_KCTL_ENUM_ID, snd_sof_enum_get, snd_sof_enum_put},
3615 	{SOF_TPLG_KCTL_SWITCH_ID, snd_sof_switch_get, snd_sof_switch_put},
3616 };
3617 
3618 /* vendor specific bytes ext handlers available for binding */
3619 static const struct snd_soc_tplg_bytes_ext_ops sof_bytes_ext_ops[] = {
3620 	{SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_ext_get, snd_sof_bytes_ext_put},
3621 	{SOF_TPLG_KCTL_BYTES_VOLATILE_RO, snd_sof_bytes_ext_volatile_get},
3622 };
3623 
3624 static struct snd_soc_tplg_ops sof_tplg_ops = {
3625 	/* external kcontrol init - used for any driver specific init */
3626 	.control_load	= sof_control_load,
3627 	.control_unload	= sof_control_unload,
3628 
3629 	/* external kcontrol init - used for any driver specific init */
3630 	.dapm_route_load	= sof_route_load,
3631 	.dapm_route_unload	= sof_route_unload,
3632 
3633 	/* external widget init - used for any driver specific init */
3634 	/* .widget_load is not currently used */
3635 	.widget_ready	= sof_widget_ready,
3636 	.widget_unload	= sof_widget_unload,
3637 
3638 	/* FE DAI - used for any driver specific init */
3639 	.dai_load	= sof_dai_load,
3640 	.dai_unload	= sof_dai_unload,
3641 
3642 	/* DAI link - used for any driver specific init */
3643 	.link_load	= sof_link_load,
3644 
3645 	/* completion - called at completion of firmware loading */
3646 	.complete	= sof_complete,
3647 
3648 	/* manifest - optional to inform component of manifest */
3649 	.manifest	= sof_manifest,
3650 
3651 	/* vendor specific kcontrol handlers available for binding */
3652 	.io_ops		= sof_io_ops,
3653 	.io_ops_count	= ARRAY_SIZE(sof_io_ops),
3654 
3655 	/* vendor specific bytes ext handlers available for binding */
3656 	.bytes_ext_ops	= sof_bytes_ext_ops,
3657 	.bytes_ext_ops_count	= ARRAY_SIZE(sof_bytes_ext_ops),
3658 };
3659 
3660 int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file)
3661 {
3662 	const struct firmware *fw;
3663 	int ret;
3664 
3665 	dev_dbg(scomp->dev, "loading topology:%s\n", file);
3666 
3667 	ret = request_firmware(&fw, file, scomp->dev);
3668 	if (ret < 0) {
3669 		dev_err(scomp->dev, "error: tplg request firmware %s failed err: %d\n",
3670 			file, ret);
3671 		dev_err(scomp->dev,
3672 			"you may need to download the firmware from https://github.com/thesofproject/sof-bin/\n");
3673 		return ret;
3674 	}
3675 
3676 	ret = snd_soc_tplg_component_load(scomp, &sof_tplg_ops, fw);
3677 	if (ret < 0) {
3678 		dev_err(scomp->dev, "error: tplg component load failed %d\n",
3679 			ret);
3680 		ret = -EINVAL;
3681 	}
3682 
3683 	release_firmware(fw);
3684 	return ret;
3685 }
3686 EXPORT_SYMBOL(snd_sof_load_topology);
3687