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