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