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