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