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