1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  skl-topology.c - Implements Platform component ALSA controls/widget
4  *  handlers.
5  *
6  *  Copyright (C) 2014-2015 Intel Corp
7  *  Author: Jeeja KP <jeeja.kp@intel.com>
8  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9  */
10 
11 #include <linux/slab.h>
12 #include <linux/types.h>
13 #include <linux/firmware.h>
14 #include <linux/uuid.h>
15 #include <sound/intel-nhlt.h>
16 #include <sound/soc.h>
17 #include <sound/soc-acpi.h>
18 #include <sound/soc-topology.h>
19 #include <uapi/sound/snd_sst_tokens.h>
20 #include <uapi/sound/skl-tplg-interface.h>
21 #include "skl-sst-dsp.h"
22 #include "skl-sst-ipc.h"
23 #include "skl-topology.h"
24 #include "skl.h"
25 #include "../common/sst-dsp.h"
26 #include "../common/sst-dsp-priv.h"
27 
28 #define SKL_CH_FIXUP_MASK		(1 << 0)
29 #define SKL_RATE_FIXUP_MASK		(1 << 1)
30 #define SKL_FMT_FIXUP_MASK		(1 << 2)
31 #define SKL_IN_DIR_BIT_MASK		BIT(0)
32 #define SKL_PIN_COUNT_MASK		GENMASK(7, 4)
33 
34 static const int mic_mono_list[] = {
35 0, 1, 2, 3,
36 };
37 static const int mic_stereo_list[][SKL_CH_STEREO] = {
38 {0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 3}, {2, 3},
39 };
40 static const int mic_trio_list[][SKL_CH_TRIO] = {
41 {0, 1, 2}, {0, 1, 3}, {0, 2, 3}, {1, 2, 3},
42 };
43 static const int mic_quatro_list[][SKL_CH_QUATRO] = {
44 {0, 1, 2, 3},
45 };
46 
47 #define CHECK_HW_PARAMS(ch, freq, bps, prm_ch, prm_freq, prm_bps) \
48 	((ch == prm_ch) && (bps == prm_bps) && (freq == prm_freq))
49 
50 void skl_tplg_d0i3_get(struct skl_dev *skl, enum d0i3_capability caps)
51 {
52 	struct skl_d0i3_data *d0i3 =  &skl->d0i3;
53 
54 	switch (caps) {
55 	case SKL_D0I3_NONE:
56 		d0i3->non_d0i3++;
57 		break;
58 
59 	case SKL_D0I3_STREAMING:
60 		d0i3->streaming++;
61 		break;
62 
63 	case SKL_D0I3_NON_STREAMING:
64 		d0i3->non_streaming++;
65 		break;
66 	}
67 }
68 
69 void skl_tplg_d0i3_put(struct skl_dev *skl, enum d0i3_capability caps)
70 {
71 	struct skl_d0i3_data *d0i3 =  &skl->d0i3;
72 
73 	switch (caps) {
74 	case SKL_D0I3_NONE:
75 		d0i3->non_d0i3--;
76 		break;
77 
78 	case SKL_D0I3_STREAMING:
79 		d0i3->streaming--;
80 		break;
81 
82 	case SKL_D0I3_NON_STREAMING:
83 		d0i3->non_streaming--;
84 		break;
85 	}
86 }
87 
88 /*
89  * SKL DSP driver modelling uses only few DAPM widgets so for rest we will
90  * ignore. This helpers checks if the SKL driver handles this widget type
91  */
92 static int is_skl_dsp_widget_type(struct snd_soc_dapm_widget *w,
93 				  struct device *dev)
94 {
95 	if (w->dapm->dev != dev)
96 		return false;
97 
98 	switch (w->id) {
99 	case snd_soc_dapm_dai_link:
100 	case snd_soc_dapm_dai_in:
101 	case snd_soc_dapm_aif_in:
102 	case snd_soc_dapm_aif_out:
103 	case snd_soc_dapm_dai_out:
104 	case snd_soc_dapm_switch:
105 	case snd_soc_dapm_output:
106 	case snd_soc_dapm_mux:
107 
108 		return false;
109 	default:
110 		return true;
111 	}
112 }
113 
114 static void skl_dump_mconfig(struct skl_dev *skl, struct skl_module_cfg *mcfg)
115 {
116 	struct skl_module_iface *iface = &mcfg->module->formats[0];
117 
118 	dev_dbg(skl->dev, "Dumping config\n");
119 	dev_dbg(skl->dev, "Input Format:\n");
120 	dev_dbg(skl->dev, "channels = %d\n", iface->inputs[0].fmt.channels);
121 	dev_dbg(skl->dev, "s_freq = %d\n", iface->inputs[0].fmt.s_freq);
122 	dev_dbg(skl->dev, "ch_cfg = %d\n", iface->inputs[0].fmt.ch_cfg);
123 	dev_dbg(skl->dev, "valid bit depth = %d\n",
124 				iface->inputs[0].fmt.valid_bit_depth);
125 	dev_dbg(skl->dev, "Output Format:\n");
126 	dev_dbg(skl->dev, "channels = %d\n", iface->outputs[0].fmt.channels);
127 	dev_dbg(skl->dev, "s_freq = %d\n", iface->outputs[0].fmt.s_freq);
128 	dev_dbg(skl->dev, "valid bit depth = %d\n",
129 				iface->outputs[0].fmt.valid_bit_depth);
130 	dev_dbg(skl->dev, "ch_cfg = %d\n", iface->outputs[0].fmt.ch_cfg);
131 }
132 
133 static void skl_tplg_update_chmap(struct skl_module_fmt *fmt, int chs)
134 {
135 	int slot_map = 0xFFFFFFFF;
136 	int start_slot = 0;
137 	int i;
138 
139 	for (i = 0; i < chs; i++) {
140 		/*
141 		 * For 2 channels with starting slot as 0, slot map will
142 		 * look like 0xFFFFFF10.
143 		 */
144 		slot_map &= (~(0xF << (4 * i)) | (start_slot << (4 * i)));
145 		start_slot++;
146 	}
147 	fmt->ch_map = slot_map;
148 }
149 
150 static void skl_tplg_update_params(struct skl_module_fmt *fmt,
151 			struct skl_pipe_params *params, int fixup)
152 {
153 	if (fixup & SKL_RATE_FIXUP_MASK)
154 		fmt->s_freq = params->s_freq;
155 	if (fixup & SKL_CH_FIXUP_MASK) {
156 		fmt->channels = params->ch;
157 		skl_tplg_update_chmap(fmt, fmt->channels);
158 	}
159 	if (fixup & SKL_FMT_FIXUP_MASK) {
160 		fmt->valid_bit_depth = skl_get_bit_depth(params->s_fmt);
161 
162 		/*
163 		 * 16 bit is 16 bit container whereas 24 bit is in 32 bit
164 		 * container so update bit depth accordingly
165 		 */
166 		switch (fmt->valid_bit_depth) {
167 		case SKL_DEPTH_16BIT:
168 			fmt->bit_depth = fmt->valid_bit_depth;
169 			break;
170 
171 		default:
172 			fmt->bit_depth = SKL_DEPTH_32BIT;
173 			break;
174 		}
175 	}
176 
177 }
178 
179 /*
180  * A pipeline may have modules which impact the pcm parameters, like SRC,
181  * channel converter, format converter.
182  * We need to calculate the output params by applying the 'fixup'
183  * Topology will tell driver which type of fixup is to be applied by
184  * supplying the fixup mask, so based on that we calculate the output
185  *
186  * Now In FE the pcm hw_params is source/target format. Same is applicable
187  * for BE with its hw_params invoked.
188  * here based on FE, BE pipeline and direction we calculate the input and
189  * outfix and then apply that for a module
190  */
191 static void skl_tplg_update_params_fixup(struct skl_module_cfg *m_cfg,
192 		struct skl_pipe_params *params, bool is_fe)
193 {
194 	int in_fixup, out_fixup;
195 	struct skl_module_fmt *in_fmt, *out_fmt;
196 
197 	/* Fixups will be applied to pin 0 only */
198 	in_fmt = &m_cfg->module->formats[0].inputs[0].fmt;
199 	out_fmt = &m_cfg->module->formats[0].outputs[0].fmt;
200 
201 	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
202 		if (is_fe) {
203 			in_fixup = m_cfg->params_fixup;
204 			out_fixup = (~m_cfg->converter) &
205 					m_cfg->params_fixup;
206 		} else {
207 			out_fixup = m_cfg->params_fixup;
208 			in_fixup = (~m_cfg->converter) &
209 					m_cfg->params_fixup;
210 		}
211 	} else {
212 		if (is_fe) {
213 			out_fixup = m_cfg->params_fixup;
214 			in_fixup = (~m_cfg->converter) &
215 					m_cfg->params_fixup;
216 		} else {
217 			in_fixup = m_cfg->params_fixup;
218 			out_fixup = (~m_cfg->converter) &
219 					m_cfg->params_fixup;
220 		}
221 	}
222 
223 	skl_tplg_update_params(in_fmt, params, in_fixup);
224 	skl_tplg_update_params(out_fmt, params, out_fixup);
225 }
226 
227 /*
228  * A module needs input and output buffers, which are dependent upon pcm
229  * params, so once we have calculate params, we need buffer calculation as
230  * well.
231  */
232 static void skl_tplg_update_buffer_size(struct skl_dev *skl,
233 				struct skl_module_cfg *mcfg)
234 {
235 	int multiplier = 1;
236 	struct skl_module_fmt *in_fmt, *out_fmt;
237 	struct skl_module_res *res;
238 
239 	/* Since fixups is applied to pin 0 only, ibs, obs needs
240 	 * change for pin 0 only
241 	 */
242 	res = &mcfg->module->resources[0];
243 	in_fmt = &mcfg->module->formats[0].inputs[0].fmt;
244 	out_fmt = &mcfg->module->formats[0].outputs[0].fmt;
245 
246 	if (mcfg->m_type == SKL_MODULE_TYPE_SRCINT)
247 		multiplier = 5;
248 
249 	res->ibs = DIV_ROUND_UP(in_fmt->s_freq, 1000) *
250 			in_fmt->channels * (in_fmt->bit_depth >> 3) *
251 			multiplier;
252 
253 	res->obs = DIV_ROUND_UP(out_fmt->s_freq, 1000) *
254 			out_fmt->channels * (out_fmt->bit_depth >> 3) *
255 			multiplier;
256 }
257 
258 static u8 skl_tplg_be_dev_type(int dev_type)
259 {
260 	int ret;
261 
262 	switch (dev_type) {
263 	case SKL_DEVICE_BT:
264 		ret = NHLT_DEVICE_BT;
265 		break;
266 
267 	case SKL_DEVICE_DMIC:
268 		ret = NHLT_DEVICE_DMIC;
269 		break;
270 
271 	case SKL_DEVICE_I2S:
272 		ret = NHLT_DEVICE_I2S;
273 		break;
274 
275 	default:
276 		ret = NHLT_DEVICE_INVALID;
277 		break;
278 	}
279 
280 	return ret;
281 }
282 
283 static int skl_tplg_update_be_blob(struct snd_soc_dapm_widget *w,
284 						struct skl_dev *skl)
285 {
286 	struct skl_module_cfg *m_cfg = w->priv;
287 	int link_type, dir;
288 	u32 ch, s_freq, s_fmt;
289 	struct nhlt_specific_cfg *cfg;
290 	u8 dev_type = skl_tplg_be_dev_type(m_cfg->dev_type);
291 	int fmt_idx = m_cfg->fmt_idx;
292 	struct skl_module_iface *m_iface = &m_cfg->module->formats[fmt_idx];
293 
294 	/* check if we already have blob */
295 	if (m_cfg->formats_config.caps_size > 0)
296 		return 0;
297 
298 	dev_dbg(skl->dev, "Applying default cfg blob\n");
299 	switch (m_cfg->dev_type) {
300 	case SKL_DEVICE_DMIC:
301 		link_type = NHLT_LINK_DMIC;
302 		dir = SNDRV_PCM_STREAM_CAPTURE;
303 		s_freq = m_iface->inputs[0].fmt.s_freq;
304 		s_fmt = m_iface->inputs[0].fmt.bit_depth;
305 		ch = m_iface->inputs[0].fmt.channels;
306 		break;
307 
308 	case SKL_DEVICE_I2S:
309 		link_type = NHLT_LINK_SSP;
310 		if (m_cfg->hw_conn_type == SKL_CONN_SOURCE) {
311 			dir = SNDRV_PCM_STREAM_PLAYBACK;
312 			s_freq = m_iface->outputs[0].fmt.s_freq;
313 			s_fmt = m_iface->outputs[0].fmt.bit_depth;
314 			ch = m_iface->outputs[0].fmt.channels;
315 		} else {
316 			dir = SNDRV_PCM_STREAM_CAPTURE;
317 			s_freq = m_iface->inputs[0].fmt.s_freq;
318 			s_fmt = m_iface->inputs[0].fmt.bit_depth;
319 			ch = m_iface->inputs[0].fmt.channels;
320 		}
321 		break;
322 
323 	default:
324 		return -EINVAL;
325 	}
326 
327 	/* update the blob based on virtual bus_id and default params */
328 	cfg = skl_get_ep_blob(skl, m_cfg->vbus_id, link_type,
329 					s_fmt, ch, s_freq, dir, dev_type);
330 	if (cfg) {
331 		m_cfg->formats_config.caps_size = cfg->size;
332 		m_cfg->formats_config.caps = (u32 *) &cfg->caps;
333 	} else {
334 		dev_err(skl->dev, "Blob NULL for id %x type %d dirn %d\n",
335 					m_cfg->vbus_id, link_type, dir);
336 		dev_err(skl->dev, "PCM: ch %d, freq %d, fmt %d\n",
337 					ch, s_freq, s_fmt);
338 		return -EIO;
339 	}
340 
341 	return 0;
342 }
343 
344 static void skl_tplg_update_module_params(struct snd_soc_dapm_widget *w,
345 							struct skl_dev *skl)
346 {
347 	struct skl_module_cfg *m_cfg = w->priv;
348 	struct skl_pipe_params *params = m_cfg->pipe->p_params;
349 	int p_conn_type = m_cfg->pipe->conn_type;
350 	bool is_fe;
351 
352 	if (!m_cfg->params_fixup)
353 		return;
354 
355 	dev_dbg(skl->dev, "Mconfig for widget=%s BEFORE updation\n",
356 				w->name);
357 
358 	skl_dump_mconfig(skl, m_cfg);
359 
360 	if (p_conn_type == SKL_PIPE_CONN_TYPE_FE)
361 		is_fe = true;
362 	else
363 		is_fe = false;
364 
365 	skl_tplg_update_params_fixup(m_cfg, params, is_fe);
366 	skl_tplg_update_buffer_size(skl, m_cfg);
367 
368 	dev_dbg(skl->dev, "Mconfig for widget=%s AFTER updation\n",
369 				w->name);
370 
371 	skl_dump_mconfig(skl, m_cfg);
372 }
373 
374 /*
375  * some modules can have multiple params set from user control and
376  * need to be set after module is initialized. If set_param flag is
377  * set module params will be done after module is initialised.
378  */
379 static int skl_tplg_set_module_params(struct snd_soc_dapm_widget *w,
380 						struct skl_dev *skl)
381 {
382 	int i, ret;
383 	struct skl_module_cfg *mconfig = w->priv;
384 	const struct snd_kcontrol_new *k;
385 	struct soc_bytes_ext *sb;
386 	struct skl_algo_data *bc;
387 	struct skl_specific_cfg *sp_cfg;
388 
389 	if (mconfig->formats_config.caps_size > 0 &&
390 		mconfig->formats_config.set_params == SKL_PARAM_SET) {
391 		sp_cfg = &mconfig->formats_config;
392 		ret = skl_set_module_params(skl, sp_cfg->caps,
393 					sp_cfg->caps_size,
394 					sp_cfg->param_id, mconfig);
395 		if (ret < 0)
396 			return ret;
397 	}
398 
399 	for (i = 0; i < w->num_kcontrols; i++) {
400 		k = &w->kcontrol_news[i];
401 		if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
402 			sb = (void *) k->private_value;
403 			bc = (struct skl_algo_data *)sb->dobj.private;
404 
405 			if (bc->set_params == SKL_PARAM_SET) {
406 				ret = skl_set_module_params(skl,
407 						(u32 *)bc->params, bc->size,
408 						bc->param_id, mconfig);
409 				if (ret < 0)
410 					return ret;
411 			}
412 		}
413 	}
414 
415 	return 0;
416 }
417 
418 /*
419  * some module param can set from user control and this is required as
420  * when module is initailzed. if module param is required in init it is
421  * identifed by set_param flag. if set_param flag is not set, then this
422  * parameter needs to set as part of module init.
423  */
424 static int skl_tplg_set_module_init_data(struct snd_soc_dapm_widget *w)
425 {
426 	const struct snd_kcontrol_new *k;
427 	struct soc_bytes_ext *sb;
428 	struct skl_algo_data *bc;
429 	struct skl_module_cfg *mconfig = w->priv;
430 	int i;
431 
432 	for (i = 0; i < w->num_kcontrols; i++) {
433 		k = &w->kcontrol_news[i];
434 		if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
435 			sb = (struct soc_bytes_ext *)k->private_value;
436 			bc = (struct skl_algo_data *)sb->dobj.private;
437 
438 			if (bc->set_params != SKL_PARAM_INIT)
439 				continue;
440 
441 			mconfig->formats_config.caps = (u32 *)bc->params;
442 			mconfig->formats_config.caps_size = bc->size;
443 
444 			break;
445 		}
446 	}
447 
448 	return 0;
449 }
450 
451 static int skl_tplg_module_prepare(struct skl_dev *skl, struct skl_pipe *pipe,
452 		struct snd_soc_dapm_widget *w, struct skl_module_cfg *mcfg)
453 {
454 	switch (mcfg->dev_type) {
455 	case SKL_DEVICE_HDAHOST:
456 		return skl_pcm_host_dma_prepare(skl->dev, pipe->p_params);
457 
458 	case SKL_DEVICE_HDALINK:
459 		return skl_pcm_link_dma_prepare(skl->dev, pipe->p_params);
460 	}
461 
462 	return 0;
463 }
464 
465 /*
466  * Inside a pipe instance, we can have various modules. These modules need
467  * to instantiated in DSP by invoking INIT_MODULE IPC, which is achieved by
468  * skl_init_module() routine, so invoke that for all modules in a pipeline
469  */
470 static int
471 skl_tplg_init_pipe_modules(struct skl_dev *skl, struct skl_pipe *pipe)
472 {
473 	struct skl_pipe_module *w_module;
474 	struct snd_soc_dapm_widget *w;
475 	struct skl_module_cfg *mconfig;
476 	u8 cfg_idx;
477 	int ret = 0;
478 
479 	list_for_each_entry(w_module, &pipe->w_list, node) {
480 		guid_t *uuid_mod;
481 		w = w_module->w;
482 		mconfig = w->priv;
483 
484 		/* check if module ids are populated */
485 		if (mconfig->id.module_id < 0) {
486 			dev_err(skl->dev,
487 					"module %pUL id not populated\n",
488 					(guid_t *)mconfig->guid);
489 			return -EIO;
490 		}
491 
492 		cfg_idx = mconfig->pipe->cur_config_idx;
493 		mconfig->fmt_idx = mconfig->mod_cfg[cfg_idx].fmt_idx;
494 		mconfig->res_idx = mconfig->mod_cfg[cfg_idx].res_idx;
495 
496 		if (mconfig->module->loadable && skl->dsp->fw_ops.load_mod) {
497 			ret = skl->dsp->fw_ops.load_mod(skl->dsp,
498 				mconfig->id.module_id, mconfig->guid);
499 			if (ret < 0)
500 				return ret;
501 
502 			mconfig->m_state = SKL_MODULE_LOADED;
503 		}
504 
505 		/* prepare the DMA if the module is gateway cpr */
506 		ret = skl_tplg_module_prepare(skl, pipe, w, mconfig);
507 		if (ret < 0)
508 			return ret;
509 
510 		/* update blob if blob is null for be with default value */
511 		skl_tplg_update_be_blob(w, skl);
512 
513 		/*
514 		 * apply fix/conversion to module params based on
515 		 * FE/BE params
516 		 */
517 		skl_tplg_update_module_params(w, skl);
518 		uuid_mod = (guid_t *)mconfig->guid;
519 		mconfig->id.pvt_id = skl_get_pvt_id(skl, uuid_mod,
520 						mconfig->id.instance_id);
521 		if (mconfig->id.pvt_id < 0)
522 			return ret;
523 		skl_tplg_set_module_init_data(w);
524 
525 		ret = skl_dsp_get_core(skl->dsp, mconfig->core_id);
526 		if (ret < 0) {
527 			dev_err(skl->dev, "Failed to wake up core %d ret=%d\n",
528 						mconfig->core_id, ret);
529 			return ret;
530 		}
531 
532 		ret = skl_init_module(skl, mconfig);
533 		if (ret < 0) {
534 			skl_put_pvt_id(skl, uuid_mod, &mconfig->id.pvt_id);
535 			goto err;
536 		}
537 
538 		ret = skl_tplg_set_module_params(w, skl);
539 		if (ret < 0)
540 			goto err;
541 	}
542 
543 	return 0;
544 err:
545 	skl_dsp_put_core(skl->dsp, mconfig->core_id);
546 	return ret;
547 }
548 
549 static int skl_tplg_unload_pipe_modules(struct skl_dev *skl,
550 	 struct skl_pipe *pipe)
551 {
552 	int ret = 0;
553 	struct skl_pipe_module *w_module;
554 	struct skl_module_cfg *mconfig;
555 
556 	list_for_each_entry(w_module, &pipe->w_list, node) {
557 		guid_t *uuid_mod;
558 		mconfig  = w_module->w->priv;
559 		uuid_mod = (guid_t *)mconfig->guid;
560 
561 		if (mconfig->module->loadable && skl->dsp->fw_ops.unload_mod &&
562 			mconfig->m_state > SKL_MODULE_UNINIT) {
563 			ret = skl->dsp->fw_ops.unload_mod(skl->dsp,
564 						mconfig->id.module_id);
565 			if (ret < 0)
566 				return -EIO;
567 		}
568 		skl_put_pvt_id(skl, uuid_mod, &mconfig->id.pvt_id);
569 
570 		ret = skl_dsp_put_core(skl->dsp, mconfig->core_id);
571 		if (ret < 0) {
572 			/* don't return; continue with other modules */
573 			dev_err(skl->dev, "Failed to sleep core %d ret=%d\n",
574 				mconfig->core_id, ret);
575 		}
576 	}
577 
578 	/* no modules to unload in this path, so return */
579 	return ret;
580 }
581 
582 static bool skl_tplg_is_multi_fmt(struct skl_dev *skl, struct skl_pipe *pipe)
583 {
584 	struct skl_pipe_fmt *cur_fmt;
585 	struct skl_pipe_fmt *next_fmt;
586 	int i;
587 
588 	if (pipe->nr_cfgs <= 1)
589 		return false;
590 
591 	if (pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
592 		return true;
593 
594 	for (i = 0; i < pipe->nr_cfgs - 1; i++) {
595 		if (pipe->direction == SNDRV_PCM_STREAM_PLAYBACK) {
596 			cur_fmt = &pipe->configs[i].out_fmt;
597 			next_fmt = &pipe->configs[i + 1].out_fmt;
598 		} else {
599 			cur_fmt = &pipe->configs[i].in_fmt;
600 			next_fmt = &pipe->configs[i + 1].in_fmt;
601 		}
602 
603 		if (!CHECK_HW_PARAMS(cur_fmt->channels, cur_fmt->freq,
604 				     cur_fmt->bps,
605 				     next_fmt->channels,
606 				     next_fmt->freq,
607 				     next_fmt->bps))
608 			return true;
609 	}
610 
611 	return false;
612 }
613 
614 /*
615  * Here, we select pipe format based on the pipe type and pipe
616  * direction to determine the current config index for the pipeline.
617  * The config index is then used to select proper module resources.
618  * Intermediate pipes currently have a fixed format hence we select the
619  * 0th configuratation by default for such pipes.
620  */
621 static int
622 skl_tplg_get_pipe_config(struct skl_dev *skl, struct skl_module_cfg *mconfig)
623 {
624 	struct skl_pipe *pipe = mconfig->pipe;
625 	struct skl_pipe_params *params = pipe->p_params;
626 	struct skl_path_config *pconfig = &pipe->configs[0];
627 	struct skl_pipe_fmt *fmt = NULL;
628 	bool in_fmt = false;
629 	int i;
630 
631 	if (pipe->nr_cfgs == 0) {
632 		pipe->cur_config_idx = 0;
633 		return 0;
634 	}
635 
636 	if (skl_tplg_is_multi_fmt(skl, pipe)) {
637 		pipe->cur_config_idx = pipe->pipe_config_idx;
638 		pipe->memory_pages = pconfig->mem_pages;
639 		dev_dbg(skl->dev, "found pipe config idx:%d\n",
640 			pipe->cur_config_idx);
641 		return 0;
642 	}
643 
644 	if (pipe->conn_type == SKL_PIPE_CONN_TYPE_NONE) {
645 		dev_dbg(skl->dev, "No conn_type detected, take 0th config\n");
646 		pipe->cur_config_idx = 0;
647 		pipe->memory_pages = pconfig->mem_pages;
648 
649 		return 0;
650 	}
651 
652 	if ((pipe->conn_type == SKL_PIPE_CONN_TYPE_FE &&
653 	     pipe->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
654 	     (pipe->conn_type == SKL_PIPE_CONN_TYPE_BE &&
655 	     pipe->direction == SNDRV_PCM_STREAM_CAPTURE))
656 		in_fmt = true;
657 
658 	for (i = 0; i < pipe->nr_cfgs; i++) {
659 		pconfig = &pipe->configs[i];
660 		if (in_fmt)
661 			fmt = &pconfig->in_fmt;
662 		else
663 			fmt = &pconfig->out_fmt;
664 
665 		if (CHECK_HW_PARAMS(params->ch, params->s_freq, params->s_fmt,
666 				    fmt->channels, fmt->freq, fmt->bps)) {
667 			pipe->cur_config_idx = i;
668 			pipe->memory_pages = pconfig->mem_pages;
669 			dev_dbg(skl->dev, "Using pipe config: %d\n", i);
670 
671 			return 0;
672 		}
673 	}
674 
675 	dev_err(skl->dev, "Invalid pipe config: %d %d %d for pipe: %d\n",
676 		params->ch, params->s_freq, params->s_fmt, pipe->ppl_id);
677 	return -EINVAL;
678 }
679 
680 /*
681  * Mixer module represents a pipeline. So in the Pre-PMU event of mixer we
682  * need create the pipeline. So we do following:
683  *   - Create the pipeline
684  *   - Initialize the modules in pipeline
685  *   - finally bind all modules together
686  */
687 static int skl_tplg_mixer_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
688 							struct skl_dev *skl)
689 {
690 	int ret;
691 	struct skl_module_cfg *mconfig = w->priv;
692 	struct skl_pipe_module *w_module;
693 	struct skl_pipe *s_pipe = mconfig->pipe;
694 	struct skl_module_cfg *src_module = NULL, *dst_module, *module;
695 	struct skl_module_deferred_bind *modules;
696 
697 	ret = skl_tplg_get_pipe_config(skl, mconfig);
698 	if (ret < 0)
699 		return ret;
700 
701 	/*
702 	 * Create a list of modules for pipe.
703 	 * This list contains modules from source to sink
704 	 */
705 	ret = skl_create_pipeline(skl, mconfig->pipe);
706 	if (ret < 0)
707 		return ret;
708 
709 	/* Init all pipe modules from source to sink */
710 	ret = skl_tplg_init_pipe_modules(skl, s_pipe);
711 	if (ret < 0)
712 		return ret;
713 
714 	/* Bind modules from source to sink */
715 	list_for_each_entry(w_module, &s_pipe->w_list, node) {
716 		dst_module = w_module->w->priv;
717 
718 		if (src_module == NULL) {
719 			src_module = dst_module;
720 			continue;
721 		}
722 
723 		ret = skl_bind_modules(skl, src_module, dst_module);
724 		if (ret < 0)
725 			return ret;
726 
727 		src_module = dst_module;
728 	}
729 
730 	/*
731 	 * When the destination module is initialized, check for these modules
732 	 * in deferred bind list. If found, bind them.
733 	 */
734 	list_for_each_entry(w_module, &s_pipe->w_list, node) {
735 		if (list_empty(&skl->bind_list))
736 			break;
737 
738 		list_for_each_entry(modules, &skl->bind_list, node) {
739 			module = w_module->w->priv;
740 			if (modules->dst == module)
741 				skl_bind_modules(skl, modules->src,
742 							modules->dst);
743 		}
744 	}
745 
746 	return 0;
747 }
748 
749 static int skl_fill_sink_instance_id(struct skl_dev *skl, u32 *params,
750 				int size, struct skl_module_cfg *mcfg)
751 {
752 	int i, pvt_id;
753 
754 	if (mcfg->m_type == SKL_MODULE_TYPE_KPB) {
755 		struct skl_kpb_params *kpb_params =
756 				(struct skl_kpb_params *)params;
757 		struct skl_mod_inst_map *inst = kpb_params->u.map;
758 
759 		for (i = 0; i < kpb_params->num_modules; i++) {
760 			pvt_id = skl_get_pvt_instance_id_map(skl, inst->mod_id,
761 								inst->inst_id);
762 			if (pvt_id < 0)
763 				return -EINVAL;
764 
765 			inst->inst_id = pvt_id;
766 			inst++;
767 		}
768 	}
769 
770 	return 0;
771 }
772 /*
773  * Some modules require params to be set after the module is bound to
774  * all pins connected.
775  *
776  * The module provider initializes set_param flag for such modules and we
777  * send params after binding
778  */
779 static int skl_tplg_set_module_bind_params(struct snd_soc_dapm_widget *w,
780 			struct skl_module_cfg *mcfg, struct skl_dev *skl)
781 {
782 	int i, ret;
783 	struct skl_module_cfg *mconfig = w->priv;
784 	const struct snd_kcontrol_new *k;
785 	struct soc_bytes_ext *sb;
786 	struct skl_algo_data *bc;
787 	struct skl_specific_cfg *sp_cfg;
788 	u32 *params;
789 
790 	/*
791 	 * check all out/in pins are in bind state.
792 	 * if so set the module param
793 	 */
794 	for (i = 0; i < mcfg->module->max_output_pins; i++) {
795 		if (mcfg->m_out_pin[i].pin_state != SKL_PIN_BIND_DONE)
796 			return 0;
797 	}
798 
799 	for (i = 0; i < mcfg->module->max_input_pins; i++) {
800 		if (mcfg->m_in_pin[i].pin_state != SKL_PIN_BIND_DONE)
801 			return 0;
802 	}
803 
804 	if (mconfig->formats_config.caps_size > 0 &&
805 		mconfig->formats_config.set_params == SKL_PARAM_BIND) {
806 		sp_cfg = &mconfig->formats_config;
807 		ret = skl_set_module_params(skl, sp_cfg->caps,
808 					sp_cfg->caps_size,
809 					sp_cfg->param_id, mconfig);
810 		if (ret < 0)
811 			return ret;
812 	}
813 
814 	for (i = 0; i < w->num_kcontrols; i++) {
815 		k = &w->kcontrol_news[i];
816 		if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
817 			sb = (void *) k->private_value;
818 			bc = (struct skl_algo_data *)sb->dobj.private;
819 
820 			if (bc->set_params == SKL_PARAM_BIND) {
821 				params = kmemdup(bc->params, bc->max, GFP_KERNEL);
822 				if (!params)
823 					return -ENOMEM;
824 
825 				skl_fill_sink_instance_id(skl, params, bc->max,
826 								mconfig);
827 
828 				ret = skl_set_module_params(skl, params,
829 						bc->max, bc->param_id, mconfig);
830 				kfree(params);
831 
832 				if (ret < 0)
833 					return ret;
834 			}
835 		}
836 	}
837 
838 	return 0;
839 }
840 
841 static int skl_get_module_id(struct skl_dev *skl, guid_t *uuid)
842 {
843 	struct uuid_module *module;
844 
845 	list_for_each_entry(module, &skl->uuid_list, list) {
846 		if (guid_equal(uuid, &module->uuid))
847 			return module->id;
848 	}
849 
850 	return -EINVAL;
851 }
852 
853 static int skl_tplg_find_moduleid_from_uuid(struct skl_dev *skl,
854 					const struct snd_kcontrol_new *k)
855 {
856 	struct soc_bytes_ext *sb = (void *) k->private_value;
857 	struct skl_algo_data *bc = (struct skl_algo_data *)sb->dobj.private;
858 	struct skl_kpb_params *uuid_params, *params;
859 	struct hdac_bus *bus = skl_to_bus(skl);
860 	int i, size, module_id;
861 
862 	if (bc->set_params == SKL_PARAM_BIND && bc->max) {
863 		uuid_params = (struct skl_kpb_params *)bc->params;
864 		size = struct_size(params, u.map, uuid_params->num_modules);
865 
866 		params = devm_kzalloc(bus->dev, size, GFP_KERNEL);
867 		if (!params)
868 			return -ENOMEM;
869 
870 		params->num_modules = uuid_params->num_modules;
871 
872 		for (i = 0; i < uuid_params->num_modules; i++) {
873 			module_id = skl_get_module_id(skl,
874 				&uuid_params->u.map_uuid[i].mod_uuid);
875 			if (module_id < 0) {
876 				devm_kfree(bus->dev, params);
877 				return -EINVAL;
878 			}
879 
880 			params->u.map[i].mod_id = module_id;
881 			params->u.map[i].inst_id =
882 				uuid_params->u.map_uuid[i].inst_id;
883 		}
884 
885 		devm_kfree(bus->dev, bc->params);
886 		bc->params = (char *)params;
887 		bc->max = size;
888 	}
889 
890 	return 0;
891 }
892 
893 /*
894  * Retrieve the module id from UUID mentioned in the
895  * post bind params
896  */
897 void skl_tplg_add_moduleid_in_bind_params(struct skl_dev *skl,
898 				struct snd_soc_dapm_widget *w)
899 {
900 	struct skl_module_cfg *mconfig = w->priv;
901 	int i;
902 
903 	/*
904 	 * Post bind params are used for only for KPB
905 	 * to set copier instances to drain the data
906 	 * in fast mode
907 	 */
908 	if (mconfig->m_type != SKL_MODULE_TYPE_KPB)
909 		return;
910 
911 	for (i = 0; i < w->num_kcontrols; i++)
912 		if ((w->kcontrol_news[i].access &
913 			SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) &&
914 			(skl_tplg_find_moduleid_from_uuid(skl,
915 			&w->kcontrol_news[i]) < 0))
916 			dev_err(skl->dev,
917 				"%s: invalid kpb post bind params\n",
918 				__func__);
919 }
920 
921 static int skl_tplg_module_add_deferred_bind(struct skl_dev *skl,
922 	struct skl_module_cfg *src, struct skl_module_cfg *dst)
923 {
924 	struct skl_module_deferred_bind *m_list, *modules;
925 	int i;
926 
927 	/* only supported for module with static pin connection */
928 	for (i = 0; i < dst->module->max_input_pins; i++) {
929 		struct skl_module_pin *pin = &dst->m_in_pin[i];
930 
931 		if (pin->is_dynamic)
932 			continue;
933 
934 		if ((pin->id.module_id  == src->id.module_id) &&
935 			(pin->id.instance_id  == src->id.instance_id)) {
936 
937 			if (!list_empty(&skl->bind_list)) {
938 				list_for_each_entry(modules, &skl->bind_list, node) {
939 					if (modules->src == src && modules->dst == dst)
940 						return 0;
941 				}
942 			}
943 
944 			m_list = kzalloc(sizeof(*m_list), GFP_KERNEL);
945 			if (!m_list)
946 				return -ENOMEM;
947 
948 			m_list->src = src;
949 			m_list->dst = dst;
950 
951 			list_add(&m_list->node, &skl->bind_list);
952 		}
953 	}
954 
955 	return 0;
956 }
957 
958 static int skl_tplg_bind_sinks(struct snd_soc_dapm_widget *w,
959 				struct skl_dev *skl,
960 				struct snd_soc_dapm_widget *src_w,
961 				struct skl_module_cfg *src_mconfig)
962 {
963 	struct snd_soc_dapm_path *p;
964 	struct snd_soc_dapm_widget *sink = NULL, *next_sink = NULL;
965 	struct skl_module_cfg *sink_mconfig;
966 	int ret;
967 
968 	snd_soc_dapm_widget_for_each_sink_path(w, p) {
969 		if (!p->connect)
970 			continue;
971 
972 		dev_dbg(skl->dev,
973 			"%s: src widget=%s\n", __func__, w->name);
974 		dev_dbg(skl->dev,
975 			"%s: sink widget=%s\n", __func__, p->sink->name);
976 
977 		next_sink = p->sink;
978 
979 		if (!is_skl_dsp_widget_type(p->sink, skl->dev))
980 			return skl_tplg_bind_sinks(p->sink, skl, src_w, src_mconfig);
981 
982 		/*
983 		 * here we will check widgets in sink pipelines, so that
984 		 * can be any widgets type and we are only interested if
985 		 * they are ones used for SKL so check that first
986 		 */
987 		if ((p->sink->priv != NULL) &&
988 				is_skl_dsp_widget_type(p->sink, skl->dev)) {
989 
990 			sink = p->sink;
991 			sink_mconfig = sink->priv;
992 
993 			/*
994 			 * Modules other than PGA leaf can be connected
995 			 * directly or via switch to a module in another
996 			 * pipeline. EX: reference path
997 			 * when the path is enabled, the dst module that needs
998 			 * to be bound may not be initialized. if the module is
999 			 * not initialized, add these modules in the deferred
1000 			 * bind list and when the dst module is initialised,
1001 			 * bind this module to the dst_module in deferred list.
1002 			 */
1003 			if (((src_mconfig->m_state == SKL_MODULE_INIT_DONE)
1004 				&& (sink_mconfig->m_state == SKL_MODULE_UNINIT))) {
1005 
1006 				ret = skl_tplg_module_add_deferred_bind(skl,
1007 						src_mconfig, sink_mconfig);
1008 
1009 				if (ret < 0)
1010 					return ret;
1011 
1012 			}
1013 
1014 
1015 			if (src_mconfig->m_state == SKL_MODULE_UNINIT ||
1016 				sink_mconfig->m_state == SKL_MODULE_UNINIT)
1017 				continue;
1018 
1019 			/* Bind source to sink, mixin is always source */
1020 			ret = skl_bind_modules(skl, src_mconfig, sink_mconfig);
1021 			if (ret)
1022 				return ret;
1023 
1024 			/* set module params after bind */
1025 			skl_tplg_set_module_bind_params(src_w,
1026 					src_mconfig, skl);
1027 			skl_tplg_set_module_bind_params(sink,
1028 					sink_mconfig, skl);
1029 
1030 			/* Start sinks pipe first */
1031 			if (sink_mconfig->pipe->state != SKL_PIPE_STARTED) {
1032 				if (sink_mconfig->pipe->conn_type !=
1033 							SKL_PIPE_CONN_TYPE_FE)
1034 					ret = skl_run_pipe(skl,
1035 							sink_mconfig->pipe);
1036 				if (ret)
1037 					return ret;
1038 			}
1039 		}
1040 	}
1041 
1042 	if (!sink && next_sink)
1043 		return skl_tplg_bind_sinks(next_sink, skl, src_w, src_mconfig);
1044 
1045 	return 0;
1046 }
1047 
1048 /*
1049  * A PGA represents a module in a pipeline. So in the Pre-PMU event of PGA
1050  * we need to do following:
1051  *   - Bind to sink pipeline
1052  *      Since the sink pipes can be running and we don't get mixer event on
1053  *      connect for already running mixer, we need to find the sink pipes
1054  *      here and bind to them. This way dynamic connect works.
1055  *   - Start sink pipeline, if not running
1056  *   - Then run current pipe
1057  */
1058 static int skl_tplg_pga_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
1059 							struct skl_dev *skl)
1060 {
1061 	struct skl_module_cfg *src_mconfig;
1062 	int ret = 0;
1063 
1064 	src_mconfig = w->priv;
1065 
1066 	/*
1067 	 * find which sink it is connected to, bind with the sink,
1068 	 * if sink is not started, start sink pipe first, then start
1069 	 * this pipe
1070 	 */
1071 	ret = skl_tplg_bind_sinks(w, skl, w, src_mconfig);
1072 	if (ret)
1073 		return ret;
1074 
1075 	/* Start source pipe last after starting all sinks */
1076 	if (src_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
1077 		return skl_run_pipe(skl, src_mconfig->pipe);
1078 
1079 	return 0;
1080 }
1081 
1082 static struct snd_soc_dapm_widget *skl_get_src_dsp_widget(
1083 		struct snd_soc_dapm_widget *w, struct skl_dev *skl)
1084 {
1085 	struct snd_soc_dapm_path *p;
1086 	struct snd_soc_dapm_widget *src_w = NULL;
1087 
1088 	snd_soc_dapm_widget_for_each_source_path(w, p) {
1089 		src_w = p->source;
1090 		if (!p->connect)
1091 			continue;
1092 
1093 		dev_dbg(skl->dev, "sink widget=%s\n", w->name);
1094 		dev_dbg(skl->dev, "src widget=%s\n", p->source->name);
1095 
1096 		/*
1097 		 * here we will check widgets in sink pipelines, so that can
1098 		 * be any widgets type and we are only interested if they are
1099 		 * ones used for SKL so check that first
1100 		 */
1101 		if ((p->source->priv != NULL) &&
1102 				is_skl_dsp_widget_type(p->source, skl->dev)) {
1103 			return p->source;
1104 		}
1105 	}
1106 
1107 	if (src_w != NULL)
1108 		return skl_get_src_dsp_widget(src_w, skl);
1109 
1110 	return NULL;
1111 }
1112 
1113 /*
1114  * in the Post-PMU event of mixer we need to do following:
1115  *   - Check if this pipe is running
1116  *   - if not, then
1117  *	- bind this pipeline to its source pipeline
1118  *	  if source pipe is already running, this means it is a dynamic
1119  *	  connection and we need to bind only to that pipe
1120  *	- start this pipeline
1121  */
1122 static int skl_tplg_mixer_dapm_post_pmu_event(struct snd_soc_dapm_widget *w,
1123 							struct skl_dev *skl)
1124 {
1125 	int ret = 0;
1126 	struct snd_soc_dapm_widget *source, *sink;
1127 	struct skl_module_cfg *src_mconfig, *sink_mconfig;
1128 	int src_pipe_started = 0;
1129 
1130 	sink = w;
1131 	sink_mconfig = sink->priv;
1132 
1133 	/*
1134 	 * If source pipe is already started, that means source is driving
1135 	 * one more sink before this sink got connected, Since source is
1136 	 * started, bind this sink to source and start this pipe.
1137 	 */
1138 	source = skl_get_src_dsp_widget(w, skl);
1139 	if (source != NULL) {
1140 		src_mconfig = source->priv;
1141 		sink_mconfig = sink->priv;
1142 		src_pipe_started = 1;
1143 
1144 		/*
1145 		 * check pipe state, then no need to bind or start the
1146 		 * pipe
1147 		 */
1148 		if (src_mconfig->pipe->state != SKL_PIPE_STARTED)
1149 			src_pipe_started = 0;
1150 	}
1151 
1152 	if (src_pipe_started) {
1153 		ret = skl_bind_modules(skl, src_mconfig, sink_mconfig);
1154 		if (ret)
1155 			return ret;
1156 
1157 		/* set module params after bind */
1158 		skl_tplg_set_module_bind_params(source, src_mconfig, skl);
1159 		skl_tplg_set_module_bind_params(sink, sink_mconfig, skl);
1160 
1161 		if (sink_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
1162 			ret = skl_run_pipe(skl, sink_mconfig->pipe);
1163 	}
1164 
1165 	return ret;
1166 }
1167 
1168 /*
1169  * in the Pre-PMD event of mixer we need to do following:
1170  *   - Stop the pipe
1171  *   - find the source connections and remove that from dapm_path_list
1172  *   - unbind with source pipelines if still connected
1173  */
1174 static int skl_tplg_mixer_dapm_pre_pmd_event(struct snd_soc_dapm_widget *w,
1175 							struct skl_dev *skl)
1176 {
1177 	struct skl_module_cfg *src_mconfig, *sink_mconfig;
1178 	int ret = 0, i;
1179 
1180 	sink_mconfig = w->priv;
1181 
1182 	/* Stop the pipe */
1183 	ret = skl_stop_pipe(skl, sink_mconfig->pipe);
1184 	if (ret)
1185 		return ret;
1186 
1187 	for (i = 0; i < sink_mconfig->module->max_input_pins; i++) {
1188 		if (sink_mconfig->m_in_pin[i].pin_state == SKL_PIN_BIND_DONE) {
1189 			src_mconfig = sink_mconfig->m_in_pin[i].tgt_mcfg;
1190 			if (!src_mconfig)
1191 				continue;
1192 
1193 			ret = skl_unbind_modules(skl,
1194 						src_mconfig, sink_mconfig);
1195 		}
1196 	}
1197 
1198 	return ret;
1199 }
1200 
1201 /*
1202  * in the Post-PMD event of mixer we need to do following:
1203  *   - Unbind the modules within the pipeline
1204  *   - Delete the pipeline (modules are not required to be explicitly
1205  *     deleted, pipeline delete is enough here
1206  */
1207 static int skl_tplg_mixer_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
1208 							struct skl_dev *skl)
1209 {
1210 	struct skl_module_cfg *mconfig = w->priv;
1211 	struct skl_pipe_module *w_module;
1212 	struct skl_module_cfg *src_module = NULL, *dst_module;
1213 	struct skl_pipe *s_pipe = mconfig->pipe;
1214 	struct skl_module_deferred_bind *modules, *tmp;
1215 
1216 	if (s_pipe->state == SKL_PIPE_INVALID)
1217 		return -EINVAL;
1218 
1219 	list_for_each_entry(w_module, &s_pipe->w_list, node) {
1220 		if (list_empty(&skl->bind_list))
1221 			break;
1222 
1223 		src_module = w_module->w->priv;
1224 
1225 		list_for_each_entry_safe(modules, tmp, &skl->bind_list, node) {
1226 			/*
1227 			 * When the destination module is deleted, Unbind the
1228 			 * modules from deferred bind list.
1229 			 */
1230 			if (modules->dst == src_module) {
1231 				skl_unbind_modules(skl, modules->src,
1232 						modules->dst);
1233 			}
1234 
1235 			/*
1236 			 * When the source module is deleted, remove this entry
1237 			 * from the deferred bind list.
1238 			 */
1239 			if (modules->src == src_module) {
1240 				list_del(&modules->node);
1241 				modules->src = NULL;
1242 				modules->dst = NULL;
1243 				kfree(modules);
1244 			}
1245 		}
1246 	}
1247 
1248 	list_for_each_entry(w_module, &s_pipe->w_list, node) {
1249 		dst_module = w_module->w->priv;
1250 
1251 		if (src_module == NULL) {
1252 			src_module = dst_module;
1253 			continue;
1254 		}
1255 
1256 		skl_unbind_modules(skl, src_module, dst_module);
1257 		src_module = dst_module;
1258 	}
1259 
1260 	skl_delete_pipe(skl, mconfig->pipe);
1261 
1262 	list_for_each_entry(w_module, &s_pipe->w_list, node) {
1263 		src_module = w_module->w->priv;
1264 		src_module->m_state = SKL_MODULE_UNINIT;
1265 	}
1266 
1267 	return skl_tplg_unload_pipe_modules(skl, s_pipe);
1268 }
1269 
1270 /*
1271  * in the Post-PMD event of PGA we need to do following:
1272  *   - Stop the pipeline
1273  *   - In source pipe is connected, unbind with source pipelines
1274  */
1275 static int skl_tplg_pga_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
1276 							struct skl_dev *skl)
1277 {
1278 	struct skl_module_cfg *src_mconfig, *sink_mconfig;
1279 	int ret = 0, i;
1280 
1281 	src_mconfig = w->priv;
1282 
1283 	/* Stop the pipe since this is a mixin module */
1284 	ret = skl_stop_pipe(skl, src_mconfig->pipe);
1285 	if (ret)
1286 		return ret;
1287 
1288 	for (i = 0; i < src_mconfig->module->max_output_pins; i++) {
1289 		if (src_mconfig->m_out_pin[i].pin_state == SKL_PIN_BIND_DONE) {
1290 			sink_mconfig = src_mconfig->m_out_pin[i].tgt_mcfg;
1291 			if (!sink_mconfig)
1292 				continue;
1293 			/*
1294 			 * This is a connecter and if path is found that means
1295 			 * unbind between source and sink has not happened yet
1296 			 */
1297 			ret = skl_unbind_modules(skl, src_mconfig,
1298 							sink_mconfig);
1299 		}
1300 	}
1301 
1302 	return ret;
1303 }
1304 
1305 /*
1306  * In modelling, we assume there will be ONLY one mixer in a pipeline. If a
1307  * second one is required that is created as another pipe entity.
1308  * The mixer is responsible for pipe management and represent a pipeline
1309  * instance
1310  */
1311 static int skl_tplg_mixer_event(struct snd_soc_dapm_widget *w,
1312 				struct snd_kcontrol *k, int event)
1313 {
1314 	struct snd_soc_dapm_context *dapm = w->dapm;
1315 	struct skl_dev *skl = get_skl_ctx(dapm->dev);
1316 
1317 	switch (event) {
1318 	case SND_SOC_DAPM_PRE_PMU:
1319 		return skl_tplg_mixer_dapm_pre_pmu_event(w, skl);
1320 
1321 	case SND_SOC_DAPM_POST_PMU:
1322 		return skl_tplg_mixer_dapm_post_pmu_event(w, skl);
1323 
1324 	case SND_SOC_DAPM_PRE_PMD:
1325 		return skl_tplg_mixer_dapm_pre_pmd_event(w, skl);
1326 
1327 	case SND_SOC_DAPM_POST_PMD:
1328 		return skl_tplg_mixer_dapm_post_pmd_event(w, skl);
1329 	}
1330 
1331 	return 0;
1332 }
1333 
1334 /*
1335  * In modelling, we assumed rest of the modules in pipeline are PGA. But we
1336  * are interested in last PGA (leaf PGA) in a pipeline to disconnect with
1337  * the sink when it is running (two FE to one BE or one FE to two BE)
1338  * scenarios
1339  */
1340 static int skl_tplg_pga_event(struct snd_soc_dapm_widget *w,
1341 			struct snd_kcontrol *k, int event)
1342 
1343 {
1344 	struct snd_soc_dapm_context *dapm = w->dapm;
1345 	struct skl_dev *skl = get_skl_ctx(dapm->dev);
1346 
1347 	switch (event) {
1348 	case SND_SOC_DAPM_PRE_PMU:
1349 		return skl_tplg_pga_dapm_pre_pmu_event(w, skl);
1350 
1351 	case SND_SOC_DAPM_POST_PMD:
1352 		return skl_tplg_pga_dapm_post_pmd_event(w, skl);
1353 	}
1354 
1355 	return 0;
1356 }
1357 
1358 static int skl_tplg_multi_config_set_get(struct snd_kcontrol *kcontrol,
1359 					 struct snd_ctl_elem_value *ucontrol,
1360 					 bool is_set)
1361 {
1362 	struct snd_soc_component *component =
1363 		snd_soc_kcontrol_component(kcontrol);
1364 	struct hdac_bus *bus = snd_soc_component_get_drvdata(component);
1365 	struct skl_dev *skl = bus_to_skl(bus);
1366 	struct skl_pipeline *ppl;
1367 	struct skl_pipe *pipe = NULL;
1368 	struct soc_enum *ec = (struct soc_enum *)kcontrol->private_value;
1369 	u32 *pipe_id;
1370 
1371 	if (!ec)
1372 		return -EINVAL;
1373 
1374 	if (is_set && ucontrol->value.enumerated.item[0] > ec->items)
1375 		return -EINVAL;
1376 
1377 	pipe_id = ec->dobj.private;
1378 
1379 	list_for_each_entry(ppl, &skl->ppl_list, node) {
1380 		if (ppl->pipe->ppl_id == *pipe_id) {
1381 			pipe = ppl->pipe;
1382 			break;
1383 		}
1384 	}
1385 	if (!pipe)
1386 		return -EIO;
1387 
1388 	if (is_set)
1389 		pipe->pipe_config_idx = ucontrol->value.enumerated.item[0];
1390 	else
1391 		ucontrol->value.enumerated.item[0]  =  pipe->pipe_config_idx;
1392 
1393 	return 0;
1394 }
1395 
1396 static int skl_tplg_multi_config_get(struct snd_kcontrol *kcontrol,
1397 				     struct snd_ctl_elem_value *ucontrol)
1398 {
1399 	return skl_tplg_multi_config_set_get(kcontrol, ucontrol, false);
1400 }
1401 
1402 static int skl_tplg_multi_config_set(struct snd_kcontrol *kcontrol,
1403 				     struct snd_ctl_elem_value *ucontrol)
1404 {
1405 	return skl_tplg_multi_config_set_get(kcontrol, ucontrol, true);
1406 }
1407 
1408 static int skl_tplg_multi_config_get_dmic(struct snd_kcontrol *kcontrol,
1409 					  struct snd_ctl_elem_value *ucontrol)
1410 {
1411 	return skl_tplg_multi_config_set_get(kcontrol, ucontrol, false);
1412 }
1413 
1414 static int skl_tplg_multi_config_set_dmic(struct snd_kcontrol *kcontrol,
1415 					  struct snd_ctl_elem_value *ucontrol)
1416 {
1417 	return skl_tplg_multi_config_set_get(kcontrol, ucontrol, true);
1418 }
1419 
1420 static int skl_tplg_tlv_control_get(struct snd_kcontrol *kcontrol,
1421 			unsigned int __user *data, unsigned int size)
1422 {
1423 	struct soc_bytes_ext *sb =
1424 			(struct soc_bytes_ext *)kcontrol->private_value;
1425 	struct skl_algo_data *bc = (struct skl_algo_data *)sb->dobj.private;
1426 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1427 	struct skl_module_cfg *mconfig = w->priv;
1428 	struct skl_dev *skl = get_skl_ctx(w->dapm->dev);
1429 
1430 	if (w->power)
1431 		skl_get_module_params(skl, (u32 *)bc->params,
1432 				      bc->size, bc->param_id, mconfig);
1433 
1434 	/* decrement size for TLV header */
1435 	size -= 2 * sizeof(u32);
1436 
1437 	/* check size as we don't want to send kernel data */
1438 	if (size > bc->max)
1439 		size = bc->max;
1440 
1441 	if (bc->params) {
1442 		if (copy_to_user(data, &bc->param_id, sizeof(u32)))
1443 			return -EFAULT;
1444 		if (copy_to_user(data + 1, &size, sizeof(u32)))
1445 			return -EFAULT;
1446 		if (copy_to_user(data + 2, bc->params, size))
1447 			return -EFAULT;
1448 	}
1449 
1450 	return 0;
1451 }
1452 
1453 #define SKL_PARAM_VENDOR_ID 0xff
1454 
1455 static int skl_tplg_tlv_control_set(struct snd_kcontrol *kcontrol,
1456 			const unsigned int __user *data, unsigned int size)
1457 {
1458 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1459 	struct skl_module_cfg *mconfig = w->priv;
1460 	struct soc_bytes_ext *sb =
1461 			(struct soc_bytes_ext *)kcontrol->private_value;
1462 	struct skl_algo_data *ac = (struct skl_algo_data *)sb->dobj.private;
1463 	struct skl_dev *skl = get_skl_ctx(w->dapm->dev);
1464 
1465 	if (ac->params) {
1466 		if (size > ac->max)
1467 			return -EINVAL;
1468 		ac->size = size;
1469 
1470 		if (copy_from_user(ac->params, data, size))
1471 			return -EFAULT;
1472 
1473 		if (w->power)
1474 			return skl_set_module_params(skl,
1475 						(u32 *)ac->params, ac->size,
1476 						ac->param_id, mconfig);
1477 	}
1478 
1479 	return 0;
1480 }
1481 
1482 static int skl_tplg_mic_control_get(struct snd_kcontrol *kcontrol,
1483 		struct snd_ctl_elem_value *ucontrol)
1484 {
1485 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1486 	struct skl_module_cfg *mconfig = w->priv;
1487 	struct soc_enum *ec = (struct soc_enum *)kcontrol->private_value;
1488 	u32 ch_type = *((u32 *)ec->dobj.private);
1489 
1490 	if (mconfig->dmic_ch_type == ch_type)
1491 		ucontrol->value.enumerated.item[0] =
1492 					mconfig->dmic_ch_combo_index;
1493 	else
1494 		ucontrol->value.enumerated.item[0] = 0;
1495 
1496 	return 0;
1497 }
1498 
1499 static int skl_fill_mic_sel_params(struct skl_module_cfg *mconfig,
1500 	struct skl_mic_sel_config *mic_cfg, struct device *dev)
1501 {
1502 	struct skl_specific_cfg *sp_cfg = &mconfig->formats_config;
1503 
1504 	sp_cfg->caps_size = sizeof(struct skl_mic_sel_config);
1505 	sp_cfg->set_params = SKL_PARAM_SET;
1506 	sp_cfg->param_id = 0x00;
1507 	if (!sp_cfg->caps) {
1508 		sp_cfg->caps = devm_kzalloc(dev, sp_cfg->caps_size, GFP_KERNEL);
1509 		if (!sp_cfg->caps)
1510 			return -ENOMEM;
1511 	}
1512 
1513 	mic_cfg->mic_switch = SKL_MIC_SEL_SWITCH;
1514 	mic_cfg->flags = 0;
1515 	memcpy(sp_cfg->caps, mic_cfg, sp_cfg->caps_size);
1516 
1517 	return 0;
1518 }
1519 
1520 static int skl_tplg_mic_control_set(struct snd_kcontrol *kcontrol,
1521 			struct snd_ctl_elem_value *ucontrol)
1522 {
1523 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1524 	struct skl_module_cfg *mconfig = w->priv;
1525 	struct skl_mic_sel_config mic_cfg = {0};
1526 	struct soc_enum *ec = (struct soc_enum *)kcontrol->private_value;
1527 	u32 ch_type = *((u32 *)ec->dobj.private);
1528 	const int *list;
1529 	u8 in_ch, out_ch, index;
1530 
1531 	mconfig->dmic_ch_type = ch_type;
1532 	mconfig->dmic_ch_combo_index = ucontrol->value.enumerated.item[0];
1533 
1534 	/* enum control index 0 is INVALID, so no channels to be set */
1535 	if (mconfig->dmic_ch_combo_index == 0)
1536 		return 0;
1537 
1538 	/* No valid channel selection map for index 0, so offset by 1 */
1539 	index = mconfig->dmic_ch_combo_index - 1;
1540 
1541 	switch (ch_type) {
1542 	case SKL_CH_MONO:
1543 		if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_mono_list))
1544 			return -EINVAL;
1545 
1546 		list = &mic_mono_list[index];
1547 		break;
1548 
1549 	case SKL_CH_STEREO:
1550 		if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_stereo_list))
1551 			return -EINVAL;
1552 
1553 		list = mic_stereo_list[index];
1554 		break;
1555 
1556 	case SKL_CH_TRIO:
1557 		if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_trio_list))
1558 			return -EINVAL;
1559 
1560 		list = mic_trio_list[index];
1561 		break;
1562 
1563 	case SKL_CH_QUATRO:
1564 		if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_quatro_list))
1565 			return -EINVAL;
1566 
1567 		list = mic_quatro_list[index];
1568 		break;
1569 
1570 	default:
1571 		dev_err(w->dapm->dev,
1572 				"Invalid channel %d for mic_select module\n",
1573 				ch_type);
1574 		return -EINVAL;
1575 
1576 	}
1577 
1578 	/* channel type enum map to number of chanels for that type */
1579 	for (out_ch = 0; out_ch < ch_type; out_ch++) {
1580 		in_ch = list[out_ch];
1581 		mic_cfg.blob[out_ch][in_ch] = SKL_DEFAULT_MIC_SEL_GAIN;
1582 	}
1583 
1584 	return skl_fill_mic_sel_params(mconfig, &mic_cfg, w->dapm->dev);
1585 }
1586 
1587 /*
1588  * Fill the dma id for host and link. In case of passthrough
1589  * pipeline, this will both host and link in the same
1590  * pipeline, so need to copy the link and host based on dev_type
1591  */
1592 static void skl_tplg_fill_dma_id(struct skl_module_cfg *mcfg,
1593 				struct skl_pipe_params *params)
1594 {
1595 	struct skl_pipe *pipe = mcfg->pipe;
1596 
1597 	if (pipe->passthru) {
1598 		switch (mcfg->dev_type) {
1599 		case SKL_DEVICE_HDALINK:
1600 			pipe->p_params->link_dma_id = params->link_dma_id;
1601 			pipe->p_params->link_index = params->link_index;
1602 			pipe->p_params->link_bps = params->link_bps;
1603 			break;
1604 
1605 		case SKL_DEVICE_HDAHOST:
1606 			pipe->p_params->host_dma_id = params->host_dma_id;
1607 			pipe->p_params->host_bps = params->host_bps;
1608 			break;
1609 
1610 		default:
1611 			break;
1612 		}
1613 		pipe->p_params->s_fmt = params->s_fmt;
1614 		pipe->p_params->ch = params->ch;
1615 		pipe->p_params->s_freq = params->s_freq;
1616 		pipe->p_params->stream = params->stream;
1617 		pipe->p_params->format = params->format;
1618 
1619 	} else {
1620 		memcpy(pipe->p_params, params, sizeof(*params));
1621 	}
1622 }
1623 
1624 /*
1625  * The FE params are passed by hw_params of the DAI.
1626  * On hw_params, the params are stored in Gateway module of the FE and we
1627  * need to calculate the format in DSP module configuration, that
1628  * conversion is done here
1629  */
1630 int skl_tplg_update_pipe_params(struct device *dev,
1631 			struct skl_module_cfg *mconfig,
1632 			struct skl_pipe_params *params)
1633 {
1634 	struct skl_module_res *res = &mconfig->module->resources[0];
1635 	struct skl_dev *skl = get_skl_ctx(dev);
1636 	struct skl_module_fmt *format = NULL;
1637 	u8 cfg_idx = mconfig->pipe->cur_config_idx;
1638 
1639 	skl_tplg_fill_dma_id(mconfig, params);
1640 	mconfig->fmt_idx = mconfig->mod_cfg[cfg_idx].fmt_idx;
1641 	mconfig->res_idx = mconfig->mod_cfg[cfg_idx].res_idx;
1642 
1643 	if (skl->nr_modules)
1644 		return 0;
1645 
1646 	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK)
1647 		format = &mconfig->module->formats[0].inputs[0].fmt;
1648 	else
1649 		format = &mconfig->module->formats[0].outputs[0].fmt;
1650 
1651 	/* set the hw_params */
1652 	format->s_freq = params->s_freq;
1653 	format->channels = params->ch;
1654 	format->valid_bit_depth = skl_get_bit_depth(params->s_fmt);
1655 
1656 	/*
1657 	 * 16 bit is 16 bit container whereas 24 bit is in 32 bit
1658 	 * container so update bit depth accordingly
1659 	 */
1660 	switch (format->valid_bit_depth) {
1661 	case SKL_DEPTH_16BIT:
1662 		format->bit_depth = format->valid_bit_depth;
1663 		break;
1664 
1665 	case SKL_DEPTH_24BIT:
1666 	case SKL_DEPTH_32BIT:
1667 		format->bit_depth = SKL_DEPTH_32BIT;
1668 		break;
1669 
1670 	default:
1671 		dev_err(dev, "Invalid bit depth %x for pipe\n",
1672 				format->valid_bit_depth);
1673 		return -EINVAL;
1674 	}
1675 
1676 	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1677 		res->ibs = (format->s_freq / 1000) *
1678 				(format->channels) *
1679 				(format->bit_depth >> 3);
1680 	} else {
1681 		res->obs = (format->s_freq / 1000) *
1682 				(format->channels) *
1683 				(format->bit_depth >> 3);
1684 	}
1685 
1686 	return 0;
1687 }
1688 
1689 /*
1690  * Query the module config for the FE DAI
1691  * This is used to find the hw_params set for that DAI and apply to FE
1692  * pipeline
1693  */
1694 struct skl_module_cfg *
1695 skl_tplg_fe_get_cpr_module(struct snd_soc_dai *dai, int stream)
1696 {
1697 	struct snd_soc_dapm_widget *w;
1698 	struct snd_soc_dapm_path *p = NULL;
1699 
1700 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1701 		w = dai->playback_widget;
1702 		snd_soc_dapm_widget_for_each_sink_path(w, p) {
1703 			if (p->connect && p->sink->power &&
1704 				!is_skl_dsp_widget_type(p->sink, dai->dev))
1705 				continue;
1706 
1707 			if (p->sink->priv) {
1708 				dev_dbg(dai->dev, "set params for %s\n",
1709 						p->sink->name);
1710 				return p->sink->priv;
1711 			}
1712 		}
1713 	} else {
1714 		w = dai->capture_widget;
1715 		snd_soc_dapm_widget_for_each_source_path(w, p) {
1716 			if (p->connect && p->source->power &&
1717 				!is_skl_dsp_widget_type(p->source, dai->dev))
1718 				continue;
1719 
1720 			if (p->source->priv) {
1721 				dev_dbg(dai->dev, "set params for %s\n",
1722 						p->source->name);
1723 				return p->source->priv;
1724 			}
1725 		}
1726 	}
1727 
1728 	return NULL;
1729 }
1730 
1731 static struct skl_module_cfg *skl_get_mconfig_pb_cpr(
1732 		struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
1733 {
1734 	struct snd_soc_dapm_path *p;
1735 	struct skl_module_cfg *mconfig = NULL;
1736 
1737 	snd_soc_dapm_widget_for_each_source_path(w, p) {
1738 		if (w->endpoints[SND_SOC_DAPM_DIR_OUT] > 0) {
1739 			if (p->connect &&
1740 				    (p->sink->id == snd_soc_dapm_aif_out) &&
1741 				    p->source->priv) {
1742 				mconfig = p->source->priv;
1743 				return mconfig;
1744 			}
1745 			mconfig = skl_get_mconfig_pb_cpr(dai, p->source);
1746 			if (mconfig)
1747 				return mconfig;
1748 		}
1749 	}
1750 	return mconfig;
1751 }
1752 
1753 static struct skl_module_cfg *skl_get_mconfig_cap_cpr(
1754 		struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
1755 {
1756 	struct snd_soc_dapm_path *p;
1757 	struct skl_module_cfg *mconfig = NULL;
1758 
1759 	snd_soc_dapm_widget_for_each_sink_path(w, p) {
1760 		if (w->endpoints[SND_SOC_DAPM_DIR_IN] > 0) {
1761 			if (p->connect &&
1762 				    (p->source->id == snd_soc_dapm_aif_in) &&
1763 				    p->sink->priv) {
1764 				mconfig = p->sink->priv;
1765 				return mconfig;
1766 			}
1767 			mconfig = skl_get_mconfig_cap_cpr(dai, p->sink);
1768 			if (mconfig)
1769 				return mconfig;
1770 		}
1771 	}
1772 	return mconfig;
1773 }
1774 
1775 struct skl_module_cfg *
1776 skl_tplg_be_get_cpr_module(struct snd_soc_dai *dai, int stream)
1777 {
1778 	struct snd_soc_dapm_widget *w;
1779 	struct skl_module_cfg *mconfig;
1780 
1781 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1782 		w = dai->playback_widget;
1783 		mconfig = skl_get_mconfig_pb_cpr(dai, w);
1784 	} else {
1785 		w = dai->capture_widget;
1786 		mconfig = skl_get_mconfig_cap_cpr(dai, w);
1787 	}
1788 	return mconfig;
1789 }
1790 
1791 static u8 skl_tplg_be_link_type(int dev_type)
1792 {
1793 	int ret;
1794 
1795 	switch (dev_type) {
1796 	case SKL_DEVICE_BT:
1797 		ret = NHLT_LINK_SSP;
1798 		break;
1799 
1800 	case SKL_DEVICE_DMIC:
1801 		ret = NHLT_LINK_DMIC;
1802 		break;
1803 
1804 	case SKL_DEVICE_I2S:
1805 		ret = NHLT_LINK_SSP;
1806 		break;
1807 
1808 	case SKL_DEVICE_HDALINK:
1809 		ret = NHLT_LINK_HDA;
1810 		break;
1811 
1812 	default:
1813 		ret = NHLT_LINK_INVALID;
1814 		break;
1815 	}
1816 
1817 	return ret;
1818 }
1819 
1820 /*
1821  * Fill the BE gateway parameters
1822  * The BE gateway expects a blob of parameters which are kept in the ACPI
1823  * NHLT blob, so query the blob for interface type (i2s/pdm) and instance.
1824  * The port can have multiple settings so pick based on the PCM
1825  * parameters
1826  */
1827 static int skl_tplg_be_fill_pipe_params(struct snd_soc_dai *dai,
1828 				struct skl_module_cfg *mconfig,
1829 				struct skl_pipe_params *params)
1830 {
1831 	struct nhlt_specific_cfg *cfg;
1832 	struct skl_dev *skl = get_skl_ctx(dai->dev);
1833 	int link_type = skl_tplg_be_link_type(mconfig->dev_type);
1834 	u8 dev_type = skl_tplg_be_dev_type(mconfig->dev_type);
1835 
1836 	skl_tplg_fill_dma_id(mconfig, params);
1837 
1838 	if (link_type == NHLT_LINK_HDA)
1839 		return 0;
1840 
1841 	/* update the blob based on virtual bus_id*/
1842 	cfg = skl_get_ep_blob(skl, mconfig->vbus_id, link_type,
1843 					params->s_fmt, params->ch,
1844 					params->s_freq, params->stream,
1845 					dev_type);
1846 	if (cfg) {
1847 		mconfig->formats_config.caps_size = cfg->size;
1848 		mconfig->formats_config.caps = (u32 *) &cfg->caps;
1849 	} else {
1850 		dev_err(dai->dev, "Blob NULL for id %x type %d dirn %d\n",
1851 					mconfig->vbus_id, link_type,
1852 					params->stream);
1853 		dev_err(dai->dev, "PCM: ch %d, freq %d, fmt %d\n",
1854 				 params->ch, params->s_freq, params->s_fmt);
1855 		return -EINVAL;
1856 	}
1857 
1858 	return 0;
1859 }
1860 
1861 static int skl_tplg_be_set_src_pipe_params(struct snd_soc_dai *dai,
1862 				struct snd_soc_dapm_widget *w,
1863 				struct skl_pipe_params *params)
1864 {
1865 	struct snd_soc_dapm_path *p;
1866 	int ret = -EIO;
1867 
1868 	snd_soc_dapm_widget_for_each_source_path(w, p) {
1869 		if (p->connect && is_skl_dsp_widget_type(p->source, dai->dev) &&
1870 						p->source->priv) {
1871 
1872 			ret = skl_tplg_be_fill_pipe_params(dai,
1873 						p->source->priv, params);
1874 			if (ret < 0)
1875 				return ret;
1876 		} else {
1877 			ret = skl_tplg_be_set_src_pipe_params(dai,
1878 						p->source, params);
1879 			if (ret < 0)
1880 				return ret;
1881 		}
1882 	}
1883 
1884 	return ret;
1885 }
1886 
1887 static int skl_tplg_be_set_sink_pipe_params(struct snd_soc_dai *dai,
1888 	struct snd_soc_dapm_widget *w, struct skl_pipe_params *params)
1889 {
1890 	struct snd_soc_dapm_path *p;
1891 	int ret = -EIO;
1892 
1893 	snd_soc_dapm_widget_for_each_sink_path(w, p) {
1894 		if (p->connect && is_skl_dsp_widget_type(p->sink, dai->dev) &&
1895 						p->sink->priv) {
1896 
1897 			ret = skl_tplg_be_fill_pipe_params(dai,
1898 						p->sink->priv, params);
1899 			if (ret < 0)
1900 				return ret;
1901 		} else {
1902 			ret = skl_tplg_be_set_sink_pipe_params(
1903 						dai, p->sink, params);
1904 			if (ret < 0)
1905 				return ret;
1906 		}
1907 	}
1908 
1909 	return ret;
1910 }
1911 
1912 /*
1913  * BE hw_params can be a source parameters (capture) or sink parameters
1914  * (playback). Based on sink and source we need to either find the source
1915  * list or the sink list and set the pipeline parameters
1916  */
1917 int skl_tplg_be_update_params(struct snd_soc_dai *dai,
1918 				struct skl_pipe_params *params)
1919 {
1920 	struct snd_soc_dapm_widget *w;
1921 
1922 	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1923 		w = dai->playback_widget;
1924 
1925 		return skl_tplg_be_set_src_pipe_params(dai, w, params);
1926 
1927 	} else {
1928 		w = dai->capture_widget;
1929 
1930 		return skl_tplg_be_set_sink_pipe_params(dai, w, params);
1931 	}
1932 
1933 	return 0;
1934 }
1935 
1936 static const struct snd_soc_tplg_widget_events skl_tplg_widget_ops[] = {
1937 	{SKL_MIXER_EVENT, skl_tplg_mixer_event},
1938 	{SKL_VMIXER_EVENT, skl_tplg_mixer_event},
1939 	{SKL_PGA_EVENT, skl_tplg_pga_event},
1940 };
1941 
1942 static const struct snd_soc_tplg_bytes_ext_ops skl_tlv_ops[] = {
1943 	{SKL_CONTROL_TYPE_BYTE_TLV, skl_tplg_tlv_control_get,
1944 					skl_tplg_tlv_control_set},
1945 };
1946 
1947 static const struct snd_soc_tplg_kcontrol_ops skl_tplg_kcontrol_ops[] = {
1948 	{
1949 		.id = SKL_CONTROL_TYPE_MIC_SELECT,
1950 		.get = skl_tplg_mic_control_get,
1951 		.put = skl_tplg_mic_control_set,
1952 	},
1953 	{
1954 		.id = SKL_CONTROL_TYPE_MULTI_IO_SELECT,
1955 		.get = skl_tplg_multi_config_get,
1956 		.put = skl_tplg_multi_config_set,
1957 	},
1958 	{
1959 		.id = SKL_CONTROL_TYPE_MULTI_IO_SELECT_DMIC,
1960 		.get = skl_tplg_multi_config_get_dmic,
1961 		.put = skl_tplg_multi_config_set_dmic,
1962 	}
1963 };
1964 
1965 static int skl_tplg_fill_pipe_cfg(struct device *dev,
1966 			struct skl_pipe *pipe, u32 tkn,
1967 			u32 tkn_val, int conf_idx, int dir)
1968 {
1969 	struct skl_pipe_fmt *fmt;
1970 	struct skl_path_config *config;
1971 
1972 	switch (dir) {
1973 	case SKL_DIR_IN:
1974 		fmt = &pipe->configs[conf_idx].in_fmt;
1975 		break;
1976 
1977 	case SKL_DIR_OUT:
1978 		fmt = &pipe->configs[conf_idx].out_fmt;
1979 		break;
1980 
1981 	default:
1982 		dev_err(dev, "Invalid direction: %d\n", dir);
1983 		return -EINVAL;
1984 	}
1985 
1986 	config = &pipe->configs[conf_idx];
1987 
1988 	switch (tkn) {
1989 	case SKL_TKN_U32_CFG_FREQ:
1990 		fmt->freq = tkn_val;
1991 		break;
1992 
1993 	case SKL_TKN_U8_CFG_CHAN:
1994 		fmt->channels = tkn_val;
1995 		break;
1996 
1997 	case SKL_TKN_U8_CFG_BPS:
1998 		fmt->bps = tkn_val;
1999 		break;
2000 
2001 	case SKL_TKN_U32_PATH_MEM_PGS:
2002 		config->mem_pages = tkn_val;
2003 		break;
2004 
2005 	default:
2006 		dev_err(dev, "Invalid token config: %d\n", tkn);
2007 		return -EINVAL;
2008 	}
2009 
2010 	return 0;
2011 }
2012 
2013 static int skl_tplg_fill_pipe_tkn(struct device *dev,
2014 			struct skl_pipe *pipe, u32 tkn,
2015 			u32 tkn_val)
2016 {
2017 
2018 	switch (tkn) {
2019 	case SKL_TKN_U32_PIPE_CONN_TYPE:
2020 		pipe->conn_type = tkn_val;
2021 		break;
2022 
2023 	case SKL_TKN_U32_PIPE_PRIORITY:
2024 		pipe->pipe_priority = tkn_val;
2025 		break;
2026 
2027 	case SKL_TKN_U32_PIPE_MEM_PGS:
2028 		pipe->memory_pages = tkn_val;
2029 		break;
2030 
2031 	case SKL_TKN_U32_PMODE:
2032 		pipe->lp_mode = tkn_val;
2033 		break;
2034 
2035 	case SKL_TKN_U32_PIPE_DIRECTION:
2036 		pipe->direction = tkn_val;
2037 		break;
2038 
2039 	case SKL_TKN_U32_NUM_CONFIGS:
2040 		pipe->nr_cfgs = tkn_val;
2041 		break;
2042 
2043 	default:
2044 		dev_err(dev, "Token not handled %d\n", tkn);
2045 		return -EINVAL;
2046 	}
2047 
2048 	return 0;
2049 }
2050 
2051 /*
2052  * Add pipeline by parsing the relevant tokens
2053  * Return an existing pipe if the pipe already exists.
2054  */
2055 static int skl_tplg_add_pipe(struct device *dev,
2056 		struct skl_module_cfg *mconfig, struct skl_dev *skl,
2057 		struct snd_soc_tplg_vendor_value_elem *tkn_elem)
2058 {
2059 	struct skl_pipeline *ppl;
2060 	struct skl_pipe *pipe;
2061 	struct skl_pipe_params *params;
2062 
2063 	list_for_each_entry(ppl, &skl->ppl_list, node) {
2064 		if (ppl->pipe->ppl_id == tkn_elem->value) {
2065 			mconfig->pipe = ppl->pipe;
2066 			return -EEXIST;
2067 		}
2068 	}
2069 
2070 	ppl = devm_kzalloc(dev, sizeof(*ppl), GFP_KERNEL);
2071 	if (!ppl)
2072 		return -ENOMEM;
2073 
2074 	pipe = devm_kzalloc(dev, sizeof(*pipe), GFP_KERNEL);
2075 	if (!pipe)
2076 		return -ENOMEM;
2077 
2078 	params = devm_kzalloc(dev, sizeof(*params), GFP_KERNEL);
2079 	if (!params)
2080 		return -ENOMEM;
2081 
2082 	pipe->p_params = params;
2083 	pipe->ppl_id = tkn_elem->value;
2084 	INIT_LIST_HEAD(&pipe->w_list);
2085 
2086 	ppl->pipe = pipe;
2087 	list_add(&ppl->node, &skl->ppl_list);
2088 
2089 	mconfig->pipe = pipe;
2090 	mconfig->pipe->state = SKL_PIPE_INVALID;
2091 
2092 	return 0;
2093 }
2094 
2095 static int skl_tplg_get_uuid(struct device *dev, guid_t *guid,
2096 	      struct snd_soc_tplg_vendor_uuid_elem *uuid_tkn)
2097 {
2098 	if (uuid_tkn->token == SKL_TKN_UUID) {
2099 		import_guid(guid, uuid_tkn->uuid);
2100 		return 0;
2101 	}
2102 
2103 	dev_err(dev, "Not an UUID token %d\n", uuid_tkn->token);
2104 
2105 	return -EINVAL;
2106 }
2107 
2108 static int skl_tplg_fill_pin(struct device *dev,
2109 			struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2110 			struct skl_module_pin *m_pin,
2111 			int pin_index)
2112 {
2113 	int ret;
2114 
2115 	switch (tkn_elem->token) {
2116 	case SKL_TKN_U32_PIN_MOD_ID:
2117 		m_pin[pin_index].id.module_id = tkn_elem->value;
2118 		break;
2119 
2120 	case SKL_TKN_U32_PIN_INST_ID:
2121 		m_pin[pin_index].id.instance_id = tkn_elem->value;
2122 		break;
2123 
2124 	case SKL_TKN_UUID:
2125 		ret = skl_tplg_get_uuid(dev, &m_pin[pin_index].id.mod_uuid,
2126 			(struct snd_soc_tplg_vendor_uuid_elem *)tkn_elem);
2127 		if (ret < 0)
2128 			return ret;
2129 
2130 		break;
2131 
2132 	default:
2133 		dev_err(dev, "%d Not a pin token\n", tkn_elem->token);
2134 		return -EINVAL;
2135 	}
2136 
2137 	return 0;
2138 }
2139 
2140 /*
2141  * Parse for pin config specific tokens to fill up the
2142  * module private data
2143  */
2144 static int skl_tplg_fill_pins_info(struct device *dev,
2145 		struct skl_module_cfg *mconfig,
2146 		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2147 		int dir, int pin_count)
2148 {
2149 	int ret;
2150 	struct skl_module_pin *m_pin;
2151 
2152 	switch (dir) {
2153 	case SKL_DIR_IN:
2154 		m_pin = mconfig->m_in_pin;
2155 		break;
2156 
2157 	case SKL_DIR_OUT:
2158 		m_pin = mconfig->m_out_pin;
2159 		break;
2160 
2161 	default:
2162 		dev_err(dev, "Invalid direction value\n");
2163 		return -EINVAL;
2164 	}
2165 
2166 	ret = skl_tplg_fill_pin(dev, tkn_elem, m_pin, pin_count);
2167 	if (ret < 0)
2168 		return ret;
2169 
2170 	m_pin[pin_count].in_use = false;
2171 	m_pin[pin_count].pin_state = SKL_PIN_UNBIND;
2172 
2173 	return 0;
2174 }
2175 
2176 /*
2177  * Fill up input/output module config format based
2178  * on the direction
2179  */
2180 static int skl_tplg_fill_fmt(struct device *dev,
2181 		struct skl_module_fmt *dst_fmt,
2182 		u32 tkn, u32 value)
2183 {
2184 	switch (tkn) {
2185 	case SKL_TKN_U32_FMT_CH:
2186 		dst_fmt->channels  = value;
2187 		break;
2188 
2189 	case SKL_TKN_U32_FMT_FREQ:
2190 		dst_fmt->s_freq = value;
2191 		break;
2192 
2193 	case SKL_TKN_U32_FMT_BIT_DEPTH:
2194 		dst_fmt->bit_depth = value;
2195 		break;
2196 
2197 	case SKL_TKN_U32_FMT_SAMPLE_SIZE:
2198 		dst_fmt->valid_bit_depth = value;
2199 		break;
2200 
2201 	case SKL_TKN_U32_FMT_CH_CONFIG:
2202 		dst_fmt->ch_cfg = value;
2203 		break;
2204 
2205 	case SKL_TKN_U32_FMT_INTERLEAVE:
2206 		dst_fmt->interleaving_style = value;
2207 		break;
2208 
2209 	case SKL_TKN_U32_FMT_SAMPLE_TYPE:
2210 		dst_fmt->sample_type = value;
2211 		break;
2212 
2213 	case SKL_TKN_U32_FMT_CH_MAP:
2214 		dst_fmt->ch_map = value;
2215 		break;
2216 
2217 	default:
2218 		dev_err(dev, "Invalid token %d\n", tkn);
2219 		return -EINVAL;
2220 	}
2221 
2222 	return 0;
2223 }
2224 
2225 static int skl_tplg_widget_fill_fmt(struct device *dev,
2226 		struct skl_module_iface *fmt,
2227 		u32 tkn, u32 val, u32 dir, int fmt_idx)
2228 {
2229 	struct skl_module_fmt *dst_fmt;
2230 
2231 	if (!fmt)
2232 		return -EINVAL;
2233 
2234 	switch (dir) {
2235 	case SKL_DIR_IN:
2236 		dst_fmt = &fmt->inputs[fmt_idx].fmt;
2237 		break;
2238 
2239 	case SKL_DIR_OUT:
2240 		dst_fmt = &fmt->outputs[fmt_idx].fmt;
2241 		break;
2242 
2243 	default:
2244 		dev_err(dev, "Invalid direction: %d\n", dir);
2245 		return -EINVAL;
2246 	}
2247 
2248 	return skl_tplg_fill_fmt(dev, dst_fmt, tkn, val);
2249 }
2250 
2251 static void skl_tplg_fill_pin_dynamic_val(
2252 		struct skl_module_pin *mpin, u32 pin_count, u32 value)
2253 {
2254 	int i;
2255 
2256 	for (i = 0; i < pin_count; i++)
2257 		mpin[i].is_dynamic = value;
2258 }
2259 
2260 /*
2261  * Resource table in the manifest has pin specific resources
2262  * like pin and pin buffer size
2263  */
2264 static int skl_tplg_manifest_pin_res_tkn(struct device *dev,
2265 		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2266 		struct skl_module_res *res, int pin_idx, int dir)
2267 {
2268 	struct skl_module_pin_resources *m_pin;
2269 
2270 	switch (dir) {
2271 	case SKL_DIR_IN:
2272 		m_pin = &res->input[pin_idx];
2273 		break;
2274 
2275 	case SKL_DIR_OUT:
2276 		m_pin = &res->output[pin_idx];
2277 		break;
2278 
2279 	default:
2280 		dev_err(dev, "Invalid pin direction: %d\n", dir);
2281 		return -EINVAL;
2282 	}
2283 
2284 	switch (tkn_elem->token) {
2285 	case SKL_TKN_MM_U32_RES_PIN_ID:
2286 		m_pin->pin_index = tkn_elem->value;
2287 		break;
2288 
2289 	case SKL_TKN_MM_U32_PIN_BUF:
2290 		m_pin->buf_size = tkn_elem->value;
2291 		break;
2292 
2293 	default:
2294 		dev_err(dev, "Invalid token: %d\n", tkn_elem->token);
2295 		return -EINVAL;
2296 	}
2297 
2298 	return 0;
2299 }
2300 
2301 /*
2302  * Fill module specific resources from the manifest's resource
2303  * table like CPS, DMA size, mem_pages.
2304  */
2305 static int skl_tplg_fill_res_tkn(struct device *dev,
2306 		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2307 		struct skl_module_res *res,
2308 		int pin_idx, int dir)
2309 {
2310 	int ret, tkn_count = 0;
2311 
2312 	if (!res)
2313 		return -EINVAL;
2314 
2315 	switch (tkn_elem->token) {
2316 	case SKL_TKN_MM_U32_DMA_SIZE:
2317 		res->dma_buffer_size = tkn_elem->value;
2318 		break;
2319 
2320 	case SKL_TKN_MM_U32_CPC:
2321 		res->cpc = tkn_elem->value;
2322 		break;
2323 
2324 	case SKL_TKN_U32_MEM_PAGES:
2325 		res->is_pages = tkn_elem->value;
2326 		break;
2327 
2328 	case SKL_TKN_U32_OBS:
2329 		res->obs = tkn_elem->value;
2330 		break;
2331 
2332 	case SKL_TKN_U32_IBS:
2333 		res->ibs = tkn_elem->value;
2334 		break;
2335 
2336 	case SKL_TKN_MM_U32_RES_PIN_ID:
2337 	case SKL_TKN_MM_U32_PIN_BUF:
2338 		ret = skl_tplg_manifest_pin_res_tkn(dev, tkn_elem, res,
2339 						    pin_idx, dir);
2340 		if (ret < 0)
2341 			return ret;
2342 		break;
2343 
2344 	case SKL_TKN_MM_U32_CPS:
2345 	case SKL_TKN_U32_MAX_MCPS:
2346 		/* ignore unused tokens */
2347 		break;
2348 
2349 	default:
2350 		dev_err(dev, "Not a res type token: %d", tkn_elem->token);
2351 		return -EINVAL;
2352 
2353 	}
2354 	tkn_count++;
2355 
2356 	return tkn_count;
2357 }
2358 
2359 /*
2360  * Parse tokens to fill up the module private data
2361  */
2362 static int skl_tplg_get_token(struct device *dev,
2363 		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2364 		struct skl_dev *skl, struct skl_module_cfg *mconfig)
2365 {
2366 	int tkn_count = 0;
2367 	int ret;
2368 	static int is_pipe_exists;
2369 	static int pin_index, dir, conf_idx;
2370 	struct skl_module_iface *iface = NULL;
2371 	struct skl_module_res *res = NULL;
2372 	int res_idx = mconfig->res_idx;
2373 	int fmt_idx = mconfig->fmt_idx;
2374 
2375 	/*
2376 	 * If the manifest structure contains no modules, fill all
2377 	 * the module data to 0th index.
2378 	 * res_idx and fmt_idx are default set to 0.
2379 	 */
2380 	if (skl->nr_modules == 0) {
2381 		res = &mconfig->module->resources[res_idx];
2382 		iface = &mconfig->module->formats[fmt_idx];
2383 	}
2384 
2385 	if (tkn_elem->token > SKL_TKN_MAX)
2386 		return -EINVAL;
2387 
2388 	switch (tkn_elem->token) {
2389 	case SKL_TKN_U8_IN_QUEUE_COUNT:
2390 		mconfig->module->max_input_pins = tkn_elem->value;
2391 		break;
2392 
2393 	case SKL_TKN_U8_OUT_QUEUE_COUNT:
2394 		mconfig->module->max_output_pins = tkn_elem->value;
2395 		break;
2396 
2397 	case SKL_TKN_U8_DYN_IN_PIN:
2398 		if (!mconfig->m_in_pin)
2399 			mconfig->m_in_pin =
2400 				devm_kcalloc(dev, MAX_IN_QUEUE,
2401 					     sizeof(*mconfig->m_in_pin),
2402 					     GFP_KERNEL);
2403 		if (!mconfig->m_in_pin)
2404 			return -ENOMEM;
2405 
2406 		skl_tplg_fill_pin_dynamic_val(mconfig->m_in_pin, MAX_IN_QUEUE,
2407 					      tkn_elem->value);
2408 		break;
2409 
2410 	case SKL_TKN_U8_DYN_OUT_PIN:
2411 		if (!mconfig->m_out_pin)
2412 			mconfig->m_out_pin =
2413 				devm_kcalloc(dev, MAX_IN_QUEUE,
2414 					     sizeof(*mconfig->m_in_pin),
2415 					     GFP_KERNEL);
2416 		if (!mconfig->m_out_pin)
2417 			return -ENOMEM;
2418 
2419 		skl_tplg_fill_pin_dynamic_val(mconfig->m_out_pin, MAX_OUT_QUEUE,
2420 					      tkn_elem->value);
2421 		break;
2422 
2423 	case SKL_TKN_U8_TIME_SLOT:
2424 		mconfig->time_slot = tkn_elem->value;
2425 		break;
2426 
2427 	case SKL_TKN_U8_CORE_ID:
2428 		mconfig->core_id = tkn_elem->value;
2429 		break;
2430 
2431 	case SKL_TKN_U8_MOD_TYPE:
2432 		mconfig->m_type = tkn_elem->value;
2433 		break;
2434 
2435 	case SKL_TKN_U8_DEV_TYPE:
2436 		mconfig->dev_type = tkn_elem->value;
2437 		break;
2438 
2439 	case SKL_TKN_U8_HW_CONN_TYPE:
2440 		mconfig->hw_conn_type = tkn_elem->value;
2441 		break;
2442 
2443 	case SKL_TKN_U16_MOD_INST_ID:
2444 		mconfig->id.instance_id =
2445 		tkn_elem->value;
2446 		break;
2447 
2448 	case SKL_TKN_U32_MEM_PAGES:
2449 	case SKL_TKN_U32_MAX_MCPS:
2450 	case SKL_TKN_U32_OBS:
2451 	case SKL_TKN_U32_IBS:
2452 		ret = skl_tplg_fill_res_tkn(dev, tkn_elem, res, pin_index, dir);
2453 		if (ret < 0)
2454 			return ret;
2455 
2456 		break;
2457 
2458 	case SKL_TKN_U32_VBUS_ID:
2459 		mconfig->vbus_id = tkn_elem->value;
2460 		break;
2461 
2462 	case SKL_TKN_U32_PARAMS_FIXUP:
2463 		mconfig->params_fixup = tkn_elem->value;
2464 		break;
2465 
2466 	case SKL_TKN_U32_CONVERTER:
2467 		mconfig->converter = tkn_elem->value;
2468 		break;
2469 
2470 	case SKL_TKN_U32_D0I3_CAPS:
2471 		mconfig->d0i3_caps = tkn_elem->value;
2472 		break;
2473 
2474 	case SKL_TKN_U32_PIPE_ID:
2475 		ret = skl_tplg_add_pipe(dev,
2476 				mconfig, skl, tkn_elem);
2477 
2478 		if (ret < 0) {
2479 			if (ret == -EEXIST) {
2480 				is_pipe_exists = 1;
2481 				break;
2482 			}
2483 			return is_pipe_exists;
2484 		}
2485 
2486 		break;
2487 
2488 	case SKL_TKN_U32_PIPE_CONFIG_ID:
2489 		conf_idx = tkn_elem->value;
2490 		break;
2491 
2492 	case SKL_TKN_U32_PIPE_CONN_TYPE:
2493 	case SKL_TKN_U32_PIPE_PRIORITY:
2494 	case SKL_TKN_U32_PIPE_MEM_PGS:
2495 	case SKL_TKN_U32_PMODE:
2496 	case SKL_TKN_U32_PIPE_DIRECTION:
2497 	case SKL_TKN_U32_NUM_CONFIGS:
2498 		if (is_pipe_exists) {
2499 			ret = skl_tplg_fill_pipe_tkn(dev, mconfig->pipe,
2500 					tkn_elem->token, tkn_elem->value);
2501 			if (ret < 0)
2502 				return ret;
2503 		}
2504 
2505 		break;
2506 
2507 	case SKL_TKN_U32_PATH_MEM_PGS:
2508 	case SKL_TKN_U32_CFG_FREQ:
2509 	case SKL_TKN_U8_CFG_CHAN:
2510 	case SKL_TKN_U8_CFG_BPS:
2511 		if (mconfig->pipe->nr_cfgs) {
2512 			ret = skl_tplg_fill_pipe_cfg(dev, mconfig->pipe,
2513 					tkn_elem->token, tkn_elem->value,
2514 					conf_idx, dir);
2515 			if (ret < 0)
2516 				return ret;
2517 		}
2518 		break;
2519 
2520 	case SKL_TKN_CFG_MOD_RES_ID:
2521 		mconfig->mod_cfg[conf_idx].res_idx = tkn_elem->value;
2522 		break;
2523 
2524 	case SKL_TKN_CFG_MOD_FMT_ID:
2525 		mconfig->mod_cfg[conf_idx].fmt_idx = tkn_elem->value;
2526 		break;
2527 
2528 	/*
2529 	 * SKL_TKN_U32_DIR_PIN_COUNT token has the value for both
2530 	 * direction and the pin count. The first four bits represent
2531 	 * direction and next four the pin count.
2532 	 */
2533 	case SKL_TKN_U32_DIR_PIN_COUNT:
2534 		dir = tkn_elem->value & SKL_IN_DIR_BIT_MASK;
2535 		pin_index = (tkn_elem->value &
2536 			SKL_PIN_COUNT_MASK) >> 4;
2537 
2538 		break;
2539 
2540 	case SKL_TKN_U32_FMT_CH:
2541 	case SKL_TKN_U32_FMT_FREQ:
2542 	case SKL_TKN_U32_FMT_BIT_DEPTH:
2543 	case SKL_TKN_U32_FMT_SAMPLE_SIZE:
2544 	case SKL_TKN_U32_FMT_CH_CONFIG:
2545 	case SKL_TKN_U32_FMT_INTERLEAVE:
2546 	case SKL_TKN_U32_FMT_SAMPLE_TYPE:
2547 	case SKL_TKN_U32_FMT_CH_MAP:
2548 		ret = skl_tplg_widget_fill_fmt(dev, iface, tkn_elem->token,
2549 				tkn_elem->value, dir, pin_index);
2550 
2551 		if (ret < 0)
2552 			return ret;
2553 
2554 		break;
2555 
2556 	case SKL_TKN_U32_PIN_MOD_ID:
2557 	case SKL_TKN_U32_PIN_INST_ID:
2558 	case SKL_TKN_UUID:
2559 		ret = skl_tplg_fill_pins_info(dev,
2560 				mconfig, tkn_elem, dir,
2561 				pin_index);
2562 		if (ret < 0)
2563 			return ret;
2564 
2565 		break;
2566 
2567 	case SKL_TKN_U32_CAPS_SIZE:
2568 		mconfig->formats_config.caps_size =
2569 			tkn_elem->value;
2570 
2571 		break;
2572 
2573 	case SKL_TKN_U32_CAPS_SET_PARAMS:
2574 		mconfig->formats_config.set_params =
2575 				tkn_elem->value;
2576 		break;
2577 
2578 	case SKL_TKN_U32_CAPS_PARAMS_ID:
2579 		mconfig->formats_config.param_id =
2580 				tkn_elem->value;
2581 		break;
2582 
2583 	case SKL_TKN_U32_PROC_DOMAIN:
2584 		mconfig->domain =
2585 			tkn_elem->value;
2586 
2587 		break;
2588 
2589 	case SKL_TKN_U32_DMA_BUF_SIZE:
2590 		mconfig->dma_buffer_size = tkn_elem->value;
2591 		break;
2592 
2593 	case SKL_TKN_U8_IN_PIN_TYPE:
2594 	case SKL_TKN_U8_OUT_PIN_TYPE:
2595 	case SKL_TKN_U8_CONN_TYPE:
2596 		break;
2597 
2598 	default:
2599 		dev_err(dev, "Token %d not handled\n",
2600 				tkn_elem->token);
2601 		return -EINVAL;
2602 	}
2603 
2604 	tkn_count++;
2605 
2606 	return tkn_count;
2607 }
2608 
2609 /*
2610  * Parse the vendor array for specific tokens to construct
2611  * module private data
2612  */
2613 static int skl_tplg_get_tokens(struct device *dev,
2614 		char *pvt_data,	struct skl_dev *skl,
2615 		struct skl_module_cfg *mconfig, int block_size)
2616 {
2617 	struct snd_soc_tplg_vendor_array *array;
2618 	struct snd_soc_tplg_vendor_value_elem *tkn_elem;
2619 	int tkn_count = 0, ret;
2620 	int off = 0, tuple_size = 0;
2621 	bool is_module_guid = true;
2622 
2623 	if (block_size <= 0)
2624 		return -EINVAL;
2625 
2626 	while (tuple_size < block_size) {
2627 		array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off);
2628 
2629 		off += array->size;
2630 
2631 		switch (array->type) {
2632 		case SND_SOC_TPLG_TUPLE_TYPE_STRING:
2633 			dev_warn(dev, "no string tokens expected for skl tplg\n");
2634 			continue;
2635 
2636 		case SND_SOC_TPLG_TUPLE_TYPE_UUID:
2637 			if (is_module_guid) {
2638 				ret = skl_tplg_get_uuid(dev, (guid_t *)mconfig->guid,
2639 							array->uuid);
2640 				is_module_guid = false;
2641 			} else {
2642 				ret = skl_tplg_get_token(dev, array->value, skl,
2643 							 mconfig);
2644 			}
2645 
2646 			if (ret < 0)
2647 				return ret;
2648 
2649 			tuple_size += sizeof(*array->uuid);
2650 
2651 			continue;
2652 
2653 		default:
2654 			tkn_elem = array->value;
2655 			tkn_count = 0;
2656 			break;
2657 		}
2658 
2659 		while (tkn_count <= (array->num_elems - 1)) {
2660 			ret = skl_tplg_get_token(dev, tkn_elem,
2661 					skl, mconfig);
2662 
2663 			if (ret < 0)
2664 				return ret;
2665 
2666 			tkn_count = tkn_count + ret;
2667 			tkn_elem++;
2668 		}
2669 
2670 		tuple_size += tkn_count * sizeof(*tkn_elem);
2671 	}
2672 
2673 	return off;
2674 }
2675 
2676 /*
2677  * Every data block is preceded by a descriptor to read the number
2678  * of data blocks, they type of the block and it's size
2679  */
2680 static int skl_tplg_get_desc_blocks(struct device *dev,
2681 		struct snd_soc_tplg_vendor_array *array)
2682 {
2683 	struct snd_soc_tplg_vendor_value_elem *tkn_elem;
2684 
2685 	tkn_elem = array->value;
2686 
2687 	switch (tkn_elem->token) {
2688 	case SKL_TKN_U8_NUM_BLOCKS:
2689 	case SKL_TKN_U8_BLOCK_TYPE:
2690 	case SKL_TKN_U16_BLOCK_SIZE:
2691 		return tkn_elem->value;
2692 
2693 	default:
2694 		dev_err(dev, "Invalid descriptor token %d\n", tkn_elem->token);
2695 		break;
2696 	}
2697 
2698 	return -EINVAL;
2699 }
2700 
2701 /* Functions to parse private data from configuration file format v4 */
2702 
2703 /*
2704  * Add pipeline from topology binary into driver pipeline list
2705  *
2706  * If already added we return that instance
2707  * Otherwise we create a new instance and add into driver list
2708  */
2709 static int skl_tplg_add_pipe_v4(struct device *dev,
2710 			struct skl_module_cfg *mconfig, struct skl_dev *skl,
2711 			struct skl_dfw_v4_pipe *dfw_pipe)
2712 {
2713 	struct skl_pipeline *ppl;
2714 	struct skl_pipe *pipe;
2715 	struct skl_pipe_params *params;
2716 
2717 	list_for_each_entry(ppl, &skl->ppl_list, node) {
2718 		if (ppl->pipe->ppl_id == dfw_pipe->pipe_id) {
2719 			mconfig->pipe = ppl->pipe;
2720 			return 0;
2721 		}
2722 	}
2723 
2724 	ppl = devm_kzalloc(dev, sizeof(*ppl), GFP_KERNEL);
2725 	if (!ppl)
2726 		return -ENOMEM;
2727 
2728 	pipe = devm_kzalloc(dev, sizeof(*pipe), GFP_KERNEL);
2729 	if (!pipe)
2730 		return -ENOMEM;
2731 
2732 	params = devm_kzalloc(dev, sizeof(*params), GFP_KERNEL);
2733 	if (!params)
2734 		return -ENOMEM;
2735 
2736 	pipe->ppl_id = dfw_pipe->pipe_id;
2737 	pipe->memory_pages = dfw_pipe->memory_pages;
2738 	pipe->pipe_priority = dfw_pipe->pipe_priority;
2739 	pipe->conn_type = dfw_pipe->conn_type;
2740 	pipe->state = SKL_PIPE_INVALID;
2741 	pipe->p_params = params;
2742 	INIT_LIST_HEAD(&pipe->w_list);
2743 
2744 	ppl->pipe = pipe;
2745 	list_add(&ppl->node, &skl->ppl_list);
2746 
2747 	mconfig->pipe = pipe;
2748 
2749 	return 0;
2750 }
2751 
2752 static void skl_fill_module_pin_info_v4(struct skl_dfw_v4_module_pin *dfw_pin,
2753 					struct skl_module_pin *m_pin,
2754 					bool is_dynamic, int max_pin)
2755 {
2756 	int i;
2757 
2758 	for (i = 0; i < max_pin; i++) {
2759 		m_pin[i].id.module_id = dfw_pin[i].module_id;
2760 		m_pin[i].id.instance_id = dfw_pin[i].instance_id;
2761 		m_pin[i].in_use = false;
2762 		m_pin[i].is_dynamic = is_dynamic;
2763 		m_pin[i].pin_state = SKL_PIN_UNBIND;
2764 	}
2765 }
2766 
2767 static void skl_tplg_fill_fmt_v4(struct skl_module_pin_fmt *dst_fmt,
2768 				 struct skl_dfw_v4_module_fmt *src_fmt,
2769 				 int pins)
2770 {
2771 	int i;
2772 
2773 	for (i = 0; i < pins; i++) {
2774 		dst_fmt[i].fmt.channels  = src_fmt[i].channels;
2775 		dst_fmt[i].fmt.s_freq = src_fmt[i].freq;
2776 		dst_fmt[i].fmt.bit_depth = src_fmt[i].bit_depth;
2777 		dst_fmt[i].fmt.valid_bit_depth = src_fmt[i].valid_bit_depth;
2778 		dst_fmt[i].fmt.ch_cfg = src_fmt[i].ch_cfg;
2779 		dst_fmt[i].fmt.ch_map = src_fmt[i].ch_map;
2780 		dst_fmt[i].fmt.interleaving_style =
2781 						src_fmt[i].interleaving_style;
2782 		dst_fmt[i].fmt.sample_type = src_fmt[i].sample_type;
2783 	}
2784 }
2785 
2786 static int skl_tplg_get_pvt_data_v4(struct snd_soc_tplg_dapm_widget *tplg_w,
2787 				    struct skl_dev *skl, struct device *dev,
2788 				    struct skl_module_cfg *mconfig)
2789 {
2790 	struct skl_dfw_v4_module *dfw =
2791 				(struct skl_dfw_v4_module *)tplg_w->priv.data;
2792 	int ret;
2793 
2794 	dev_dbg(dev, "Parsing Skylake v4 widget topology data\n");
2795 
2796 	ret = guid_parse(dfw->uuid, (guid_t *)mconfig->guid);
2797 	if (ret)
2798 		return ret;
2799 	mconfig->id.module_id = -1;
2800 	mconfig->id.instance_id = dfw->instance_id;
2801 	mconfig->module->resources[0].cpc = dfw->max_mcps / 1000;
2802 	mconfig->module->resources[0].ibs = dfw->ibs;
2803 	mconfig->module->resources[0].obs = dfw->obs;
2804 	mconfig->core_id = dfw->core_id;
2805 	mconfig->module->max_input_pins = dfw->max_in_queue;
2806 	mconfig->module->max_output_pins = dfw->max_out_queue;
2807 	mconfig->module->loadable = dfw->is_loadable;
2808 	skl_tplg_fill_fmt_v4(mconfig->module->formats[0].inputs, dfw->in_fmt,
2809 			     MAX_IN_QUEUE);
2810 	skl_tplg_fill_fmt_v4(mconfig->module->formats[0].outputs, dfw->out_fmt,
2811 			     MAX_OUT_QUEUE);
2812 
2813 	mconfig->params_fixup = dfw->params_fixup;
2814 	mconfig->converter = dfw->converter;
2815 	mconfig->m_type = dfw->module_type;
2816 	mconfig->vbus_id = dfw->vbus_id;
2817 	mconfig->module->resources[0].is_pages = dfw->mem_pages;
2818 
2819 	ret = skl_tplg_add_pipe_v4(dev, mconfig, skl, &dfw->pipe);
2820 	if (ret)
2821 		return ret;
2822 
2823 	mconfig->dev_type = dfw->dev_type;
2824 	mconfig->hw_conn_type = dfw->hw_conn_type;
2825 	mconfig->time_slot = dfw->time_slot;
2826 	mconfig->formats_config.caps_size = dfw->caps.caps_size;
2827 
2828 	mconfig->m_in_pin = devm_kcalloc(dev,
2829 				MAX_IN_QUEUE, sizeof(*mconfig->m_in_pin),
2830 				GFP_KERNEL);
2831 	if (!mconfig->m_in_pin)
2832 		return -ENOMEM;
2833 
2834 	mconfig->m_out_pin = devm_kcalloc(dev,
2835 				MAX_OUT_QUEUE, sizeof(*mconfig->m_out_pin),
2836 				GFP_KERNEL);
2837 	if (!mconfig->m_out_pin)
2838 		return -ENOMEM;
2839 
2840 	skl_fill_module_pin_info_v4(dfw->in_pin, mconfig->m_in_pin,
2841 				    dfw->is_dynamic_in_pin,
2842 				    mconfig->module->max_input_pins);
2843 	skl_fill_module_pin_info_v4(dfw->out_pin, mconfig->m_out_pin,
2844 				    dfw->is_dynamic_out_pin,
2845 				    mconfig->module->max_output_pins);
2846 
2847 	if (mconfig->formats_config.caps_size) {
2848 		mconfig->formats_config.set_params = dfw->caps.set_params;
2849 		mconfig->formats_config.param_id = dfw->caps.param_id;
2850 		mconfig->formats_config.caps =
2851 		devm_kzalloc(dev, mconfig->formats_config.caps_size,
2852 			     GFP_KERNEL);
2853 		if (!mconfig->formats_config.caps)
2854 			return -ENOMEM;
2855 		memcpy(mconfig->formats_config.caps, dfw->caps.caps,
2856 		       dfw->caps.caps_size);
2857 	}
2858 
2859 	return 0;
2860 }
2861 
2862 /*
2863  * Parse the private data for the token and corresponding value.
2864  * The private data can have multiple data blocks. So, a data block
2865  * is preceded by a descriptor for number of blocks and a descriptor
2866  * for the type and size of the suceeding data block.
2867  */
2868 static int skl_tplg_get_pvt_data(struct snd_soc_tplg_dapm_widget *tplg_w,
2869 				struct skl_dev *skl, struct device *dev,
2870 				struct skl_module_cfg *mconfig)
2871 {
2872 	struct snd_soc_tplg_vendor_array *array;
2873 	int num_blocks, block_size, block_type, off = 0;
2874 	char *data;
2875 	int ret;
2876 
2877 	/*
2878 	 * v4 configuration files have a valid UUID at the start of
2879 	 * the widget's private data.
2880 	 */
2881 	if (uuid_is_valid((char *)tplg_w->priv.data))
2882 		return skl_tplg_get_pvt_data_v4(tplg_w, skl, dev, mconfig);
2883 
2884 	/* Read the NUM_DATA_BLOCKS descriptor */
2885 	array = (struct snd_soc_tplg_vendor_array *)tplg_w->priv.data;
2886 	ret = skl_tplg_get_desc_blocks(dev, array);
2887 	if (ret < 0)
2888 		return ret;
2889 	num_blocks = ret;
2890 
2891 	off += array->size;
2892 	/* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */
2893 	while (num_blocks > 0) {
2894 		array = (struct snd_soc_tplg_vendor_array *)
2895 				(tplg_w->priv.data + off);
2896 
2897 		ret = skl_tplg_get_desc_blocks(dev, array);
2898 
2899 		if (ret < 0)
2900 			return ret;
2901 		block_type = ret;
2902 		off += array->size;
2903 
2904 		array = (struct snd_soc_tplg_vendor_array *)
2905 			(tplg_w->priv.data + off);
2906 
2907 		ret = skl_tplg_get_desc_blocks(dev, array);
2908 
2909 		if (ret < 0)
2910 			return ret;
2911 		block_size = ret;
2912 		off += array->size;
2913 
2914 		array = (struct snd_soc_tplg_vendor_array *)
2915 			(tplg_w->priv.data + off);
2916 
2917 		data = (tplg_w->priv.data + off);
2918 
2919 		if (block_type == SKL_TYPE_TUPLE) {
2920 			ret = skl_tplg_get_tokens(dev, data,
2921 					skl, mconfig, block_size);
2922 
2923 			if (ret < 0)
2924 				return ret;
2925 
2926 			--num_blocks;
2927 		} else {
2928 			if (mconfig->formats_config.caps_size > 0)
2929 				memcpy(mconfig->formats_config.caps, data,
2930 					mconfig->formats_config.caps_size);
2931 			--num_blocks;
2932 			ret = mconfig->formats_config.caps_size;
2933 		}
2934 		off += ret;
2935 	}
2936 
2937 	return 0;
2938 }
2939 
2940 static void skl_clear_pin_config(struct snd_soc_component *component,
2941 				struct snd_soc_dapm_widget *w)
2942 {
2943 	int i;
2944 	struct skl_module_cfg *mconfig;
2945 	struct skl_pipe *pipe;
2946 
2947 	if (!strncmp(w->dapm->component->name, component->name,
2948 					strlen(component->name))) {
2949 		mconfig = w->priv;
2950 		pipe = mconfig->pipe;
2951 		for (i = 0; i < mconfig->module->max_input_pins; i++) {
2952 			mconfig->m_in_pin[i].in_use = false;
2953 			mconfig->m_in_pin[i].pin_state = SKL_PIN_UNBIND;
2954 		}
2955 		for (i = 0; i < mconfig->module->max_output_pins; i++) {
2956 			mconfig->m_out_pin[i].in_use = false;
2957 			mconfig->m_out_pin[i].pin_state = SKL_PIN_UNBIND;
2958 		}
2959 		pipe->state = SKL_PIPE_INVALID;
2960 		mconfig->m_state = SKL_MODULE_UNINIT;
2961 	}
2962 }
2963 
2964 void skl_cleanup_resources(struct skl_dev *skl)
2965 {
2966 	struct snd_soc_component *soc_component = skl->component;
2967 	struct snd_soc_dapm_widget *w;
2968 	struct snd_soc_card *card;
2969 
2970 	if (soc_component == NULL)
2971 		return;
2972 
2973 	card = soc_component->card;
2974 	if (!card || !card->instantiated)
2975 		return;
2976 
2977 	list_for_each_entry(w, &card->widgets, list) {
2978 		if (is_skl_dsp_widget_type(w, skl->dev) && w->priv != NULL)
2979 			skl_clear_pin_config(soc_component, w);
2980 	}
2981 
2982 	skl_clear_module_cnt(skl->dsp);
2983 }
2984 
2985 /*
2986  * Topology core widget load callback
2987  *
2988  * This is used to save the private data for each widget which gives
2989  * information to the driver about module and pipeline parameters which DSP
2990  * FW expects like ids, resource values, formats etc
2991  */
2992 static int skl_tplg_widget_load(struct snd_soc_component *cmpnt, int index,
2993 				struct snd_soc_dapm_widget *w,
2994 				struct snd_soc_tplg_dapm_widget *tplg_w)
2995 {
2996 	int ret;
2997 	struct hdac_bus *bus = snd_soc_component_get_drvdata(cmpnt);
2998 	struct skl_dev *skl = bus_to_skl(bus);
2999 	struct skl_module_cfg *mconfig;
3000 
3001 	if (!tplg_w->priv.size)
3002 		goto bind_event;
3003 
3004 	mconfig = devm_kzalloc(bus->dev, sizeof(*mconfig), GFP_KERNEL);
3005 
3006 	if (!mconfig)
3007 		return -ENOMEM;
3008 
3009 	if (skl->nr_modules == 0) {
3010 		mconfig->module = devm_kzalloc(bus->dev,
3011 				sizeof(*mconfig->module), GFP_KERNEL);
3012 		if (!mconfig->module)
3013 			return -ENOMEM;
3014 	}
3015 
3016 	w->priv = mconfig;
3017 
3018 	/*
3019 	 * module binary can be loaded later, so set it to query when
3020 	 * module is load for a use case
3021 	 */
3022 	mconfig->id.module_id = -1;
3023 
3024 	/* Parse private data for tuples */
3025 	ret = skl_tplg_get_pvt_data(tplg_w, skl, bus->dev, mconfig);
3026 	if (ret < 0)
3027 		return ret;
3028 
3029 	skl_debug_init_module(skl->debugfs, w, mconfig);
3030 
3031 bind_event:
3032 	if (tplg_w->event_type == 0) {
3033 		dev_dbg(bus->dev, "ASoC: No event handler required\n");
3034 		return 0;
3035 	}
3036 
3037 	ret = snd_soc_tplg_widget_bind_event(w, skl_tplg_widget_ops,
3038 					ARRAY_SIZE(skl_tplg_widget_ops),
3039 					tplg_w->event_type);
3040 
3041 	if (ret) {
3042 		dev_err(bus->dev, "%s: No matching event handlers found for %d\n",
3043 					__func__, tplg_w->event_type);
3044 		return -EINVAL;
3045 	}
3046 
3047 	return 0;
3048 }
3049 
3050 static int skl_init_algo_data(struct device *dev, struct soc_bytes_ext *be,
3051 					struct snd_soc_tplg_bytes_control *bc)
3052 {
3053 	struct skl_algo_data *ac;
3054 	struct skl_dfw_algo_data *dfw_ac =
3055 				(struct skl_dfw_algo_data *)bc->priv.data;
3056 
3057 	ac = devm_kzalloc(dev, sizeof(*ac), GFP_KERNEL);
3058 	if (!ac)
3059 		return -ENOMEM;
3060 
3061 	/* Fill private data */
3062 	ac->max = dfw_ac->max;
3063 	ac->param_id = dfw_ac->param_id;
3064 	ac->set_params = dfw_ac->set_params;
3065 	ac->size = dfw_ac->max;
3066 
3067 	if (ac->max) {
3068 		ac->params = devm_kzalloc(dev, ac->max, GFP_KERNEL);
3069 		if (!ac->params)
3070 			return -ENOMEM;
3071 
3072 		memcpy(ac->params, dfw_ac->params, ac->max);
3073 	}
3074 
3075 	be->dobj.private  = ac;
3076 	return 0;
3077 }
3078 
3079 static int skl_init_enum_data(struct device *dev, struct soc_enum *se,
3080 				struct snd_soc_tplg_enum_control *ec)
3081 {
3082 
3083 	void *data;
3084 
3085 	if (ec->priv.size) {
3086 		data = devm_kzalloc(dev, sizeof(ec->priv.size), GFP_KERNEL);
3087 		if (!data)
3088 			return -ENOMEM;
3089 		memcpy(data, ec->priv.data, ec->priv.size);
3090 		se->dobj.private = data;
3091 	}
3092 
3093 	return 0;
3094 
3095 }
3096 
3097 static int skl_tplg_control_load(struct snd_soc_component *cmpnt,
3098 				int index,
3099 				struct snd_kcontrol_new *kctl,
3100 				struct snd_soc_tplg_ctl_hdr *hdr)
3101 {
3102 	struct soc_bytes_ext *sb;
3103 	struct snd_soc_tplg_bytes_control *tplg_bc;
3104 	struct snd_soc_tplg_enum_control *tplg_ec;
3105 	struct hdac_bus *bus  = snd_soc_component_get_drvdata(cmpnt);
3106 	struct soc_enum *se;
3107 
3108 	switch (hdr->ops.info) {
3109 	case SND_SOC_TPLG_CTL_BYTES:
3110 		tplg_bc = container_of(hdr,
3111 				struct snd_soc_tplg_bytes_control, hdr);
3112 		if (kctl->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
3113 			sb = (struct soc_bytes_ext *)kctl->private_value;
3114 			if (tplg_bc->priv.size)
3115 				return skl_init_algo_data(
3116 						bus->dev, sb, tplg_bc);
3117 		}
3118 		break;
3119 
3120 	case SND_SOC_TPLG_CTL_ENUM:
3121 		tplg_ec = container_of(hdr,
3122 				struct snd_soc_tplg_enum_control, hdr);
3123 		if (kctl->access & SNDRV_CTL_ELEM_ACCESS_READ) {
3124 			se = (struct soc_enum *)kctl->private_value;
3125 			if (tplg_ec->priv.size)
3126 				skl_init_enum_data(bus->dev, se, tplg_ec);
3127 		}
3128 
3129 		/*
3130 		 * now that the control initializations are done, remove
3131 		 * write permission for the DMIC configuration enums to
3132 		 * avoid conflicts between NHLT settings and user interaction
3133 		 */
3134 
3135 		if (hdr->ops.get == SKL_CONTROL_TYPE_MULTI_IO_SELECT_DMIC)
3136 			kctl->access = SNDRV_CTL_ELEM_ACCESS_READ;
3137 
3138 		break;
3139 
3140 	default:
3141 		dev_dbg(bus->dev, "Control load not supported %d:%d:%d\n",
3142 			hdr->ops.get, hdr->ops.put, hdr->ops.info);
3143 		break;
3144 	}
3145 
3146 	return 0;
3147 }
3148 
3149 static int skl_tplg_fill_str_mfest_tkn(struct device *dev,
3150 		struct snd_soc_tplg_vendor_string_elem *str_elem,
3151 		struct skl_dev *skl)
3152 {
3153 	int tkn_count = 0;
3154 	static int ref_count;
3155 
3156 	switch (str_elem->token) {
3157 	case SKL_TKN_STR_LIB_NAME:
3158 		if (ref_count > skl->lib_count - 1) {
3159 			ref_count = 0;
3160 			return -EINVAL;
3161 		}
3162 
3163 		strncpy(skl->lib_info[ref_count].name,
3164 			str_elem->string,
3165 			ARRAY_SIZE(skl->lib_info[ref_count].name));
3166 		ref_count++;
3167 		break;
3168 
3169 	default:
3170 		dev_err(dev, "Not a string token %d\n", str_elem->token);
3171 		break;
3172 	}
3173 	tkn_count++;
3174 
3175 	return tkn_count;
3176 }
3177 
3178 static int skl_tplg_get_str_tkn(struct device *dev,
3179 		struct snd_soc_tplg_vendor_array *array,
3180 		struct skl_dev *skl)
3181 {
3182 	int tkn_count = 0, ret;
3183 	struct snd_soc_tplg_vendor_string_elem *str_elem;
3184 
3185 	str_elem = (struct snd_soc_tplg_vendor_string_elem *)array->value;
3186 	while (tkn_count < array->num_elems) {
3187 		ret = skl_tplg_fill_str_mfest_tkn(dev, str_elem, skl);
3188 		str_elem++;
3189 
3190 		if (ret < 0)
3191 			return ret;
3192 
3193 		tkn_count = tkn_count + ret;
3194 	}
3195 
3196 	return tkn_count;
3197 }
3198 
3199 static int skl_tplg_manifest_fill_fmt(struct device *dev,
3200 		struct skl_module_iface *fmt,
3201 		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
3202 		u32 dir, int fmt_idx)
3203 {
3204 	struct skl_module_pin_fmt *dst_fmt;
3205 	struct skl_module_fmt *mod_fmt;
3206 	int ret;
3207 
3208 	if (!fmt)
3209 		return -EINVAL;
3210 
3211 	switch (dir) {
3212 	case SKL_DIR_IN:
3213 		dst_fmt = &fmt->inputs[fmt_idx];
3214 		break;
3215 
3216 	case SKL_DIR_OUT:
3217 		dst_fmt = &fmt->outputs[fmt_idx];
3218 		break;
3219 
3220 	default:
3221 		dev_err(dev, "Invalid direction: %d\n", dir);
3222 		return -EINVAL;
3223 	}
3224 
3225 	mod_fmt = &dst_fmt->fmt;
3226 
3227 	switch (tkn_elem->token) {
3228 	case SKL_TKN_MM_U32_INTF_PIN_ID:
3229 		dst_fmt->id = tkn_elem->value;
3230 		break;
3231 
3232 	default:
3233 		ret = skl_tplg_fill_fmt(dev, mod_fmt, tkn_elem->token,
3234 					tkn_elem->value);
3235 		if (ret < 0)
3236 			return ret;
3237 		break;
3238 	}
3239 
3240 	return 0;
3241 }
3242 
3243 static int skl_tplg_fill_mod_info(struct device *dev,
3244 		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
3245 		struct skl_module *mod)
3246 {
3247 
3248 	if (!mod)
3249 		return -EINVAL;
3250 
3251 	switch (tkn_elem->token) {
3252 	case SKL_TKN_U8_IN_PIN_TYPE:
3253 		mod->input_pin_type = tkn_elem->value;
3254 		break;
3255 
3256 	case SKL_TKN_U8_OUT_PIN_TYPE:
3257 		mod->output_pin_type = tkn_elem->value;
3258 		break;
3259 
3260 	case SKL_TKN_U8_IN_QUEUE_COUNT:
3261 		mod->max_input_pins = tkn_elem->value;
3262 		break;
3263 
3264 	case SKL_TKN_U8_OUT_QUEUE_COUNT:
3265 		mod->max_output_pins = tkn_elem->value;
3266 		break;
3267 
3268 	case SKL_TKN_MM_U8_NUM_RES:
3269 		mod->nr_resources = tkn_elem->value;
3270 		break;
3271 
3272 	case SKL_TKN_MM_U8_NUM_INTF:
3273 		mod->nr_interfaces = tkn_elem->value;
3274 		break;
3275 
3276 	default:
3277 		dev_err(dev, "Invalid mod info token %d", tkn_elem->token);
3278 		return -EINVAL;
3279 	}
3280 
3281 	return 0;
3282 }
3283 
3284 
3285 static int skl_tplg_get_int_tkn(struct device *dev,
3286 		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
3287 		struct skl_dev *skl)
3288 {
3289 	int tkn_count = 0, ret;
3290 	static int mod_idx, res_val_idx, intf_val_idx, dir, pin_idx;
3291 	struct skl_module_res *res = NULL;
3292 	struct skl_module_iface *fmt = NULL;
3293 	struct skl_module *mod = NULL;
3294 	static struct skl_astate_param *astate_table;
3295 	static int astate_cfg_idx, count;
3296 	int i;
3297 	size_t size;
3298 
3299 	if (skl->modules) {
3300 		mod = skl->modules[mod_idx];
3301 		res = &mod->resources[res_val_idx];
3302 		fmt = &mod->formats[intf_val_idx];
3303 	}
3304 
3305 	switch (tkn_elem->token) {
3306 	case SKL_TKN_U32_LIB_COUNT:
3307 		skl->lib_count = tkn_elem->value;
3308 		break;
3309 
3310 	case SKL_TKN_U8_NUM_MOD:
3311 		skl->nr_modules = tkn_elem->value;
3312 		skl->modules = devm_kcalloc(dev, skl->nr_modules,
3313 				sizeof(*skl->modules), GFP_KERNEL);
3314 		if (!skl->modules)
3315 			return -ENOMEM;
3316 
3317 		for (i = 0; i < skl->nr_modules; i++) {
3318 			skl->modules[i] = devm_kzalloc(dev,
3319 					sizeof(struct skl_module), GFP_KERNEL);
3320 			if (!skl->modules[i])
3321 				return -ENOMEM;
3322 		}
3323 		break;
3324 
3325 	case SKL_TKN_MM_U8_MOD_IDX:
3326 		mod_idx = tkn_elem->value;
3327 		break;
3328 
3329 	case SKL_TKN_U32_ASTATE_COUNT:
3330 		if (astate_table != NULL) {
3331 			dev_err(dev, "More than one entry for A-State count");
3332 			return -EINVAL;
3333 		}
3334 
3335 		if (tkn_elem->value > SKL_MAX_ASTATE_CFG) {
3336 			dev_err(dev, "Invalid A-State count %d\n",
3337 				tkn_elem->value);
3338 			return -EINVAL;
3339 		}
3340 
3341 		size = struct_size(skl->cfg.astate_cfg, astate_table,
3342 				   tkn_elem->value);
3343 		skl->cfg.astate_cfg = devm_kzalloc(dev, size, GFP_KERNEL);
3344 		if (!skl->cfg.astate_cfg)
3345 			return -ENOMEM;
3346 
3347 		astate_table = skl->cfg.astate_cfg->astate_table;
3348 		count = skl->cfg.astate_cfg->count = tkn_elem->value;
3349 		break;
3350 
3351 	case SKL_TKN_U32_ASTATE_IDX:
3352 		if (tkn_elem->value >= count) {
3353 			dev_err(dev, "Invalid A-State index %d\n",
3354 				tkn_elem->value);
3355 			return -EINVAL;
3356 		}
3357 
3358 		astate_cfg_idx = tkn_elem->value;
3359 		break;
3360 
3361 	case SKL_TKN_U32_ASTATE_KCPS:
3362 		astate_table[astate_cfg_idx].kcps = tkn_elem->value;
3363 		break;
3364 
3365 	case SKL_TKN_U32_ASTATE_CLK_SRC:
3366 		astate_table[astate_cfg_idx].clk_src = tkn_elem->value;
3367 		break;
3368 
3369 	case SKL_TKN_U8_IN_PIN_TYPE:
3370 	case SKL_TKN_U8_OUT_PIN_TYPE:
3371 	case SKL_TKN_U8_IN_QUEUE_COUNT:
3372 	case SKL_TKN_U8_OUT_QUEUE_COUNT:
3373 	case SKL_TKN_MM_U8_NUM_RES:
3374 	case SKL_TKN_MM_U8_NUM_INTF:
3375 		ret = skl_tplg_fill_mod_info(dev, tkn_elem, mod);
3376 		if (ret < 0)
3377 			return ret;
3378 		break;
3379 
3380 	case SKL_TKN_U32_DIR_PIN_COUNT:
3381 		dir = tkn_elem->value & SKL_IN_DIR_BIT_MASK;
3382 		pin_idx = (tkn_elem->value & SKL_PIN_COUNT_MASK) >> 4;
3383 		break;
3384 
3385 	case SKL_TKN_MM_U32_RES_ID:
3386 		if (!res)
3387 			return -EINVAL;
3388 
3389 		res->id = tkn_elem->value;
3390 		res_val_idx = tkn_elem->value;
3391 		break;
3392 
3393 	case SKL_TKN_MM_U32_FMT_ID:
3394 		if (!fmt)
3395 			return -EINVAL;
3396 
3397 		fmt->fmt_idx = tkn_elem->value;
3398 		intf_val_idx = tkn_elem->value;
3399 		break;
3400 
3401 	case SKL_TKN_MM_U32_CPS:
3402 	case SKL_TKN_MM_U32_DMA_SIZE:
3403 	case SKL_TKN_MM_U32_CPC:
3404 	case SKL_TKN_U32_MEM_PAGES:
3405 	case SKL_TKN_U32_OBS:
3406 	case SKL_TKN_U32_IBS:
3407 	case SKL_TKN_MM_U32_RES_PIN_ID:
3408 	case SKL_TKN_MM_U32_PIN_BUF:
3409 		ret = skl_tplg_fill_res_tkn(dev, tkn_elem, res, pin_idx, dir);
3410 		if (ret < 0)
3411 			return ret;
3412 
3413 		break;
3414 
3415 	case SKL_TKN_MM_U32_NUM_IN_FMT:
3416 		if (!fmt)
3417 			return -EINVAL;
3418 
3419 		res->nr_input_pins = tkn_elem->value;
3420 		break;
3421 
3422 	case SKL_TKN_MM_U32_NUM_OUT_FMT:
3423 		if (!fmt)
3424 			return -EINVAL;
3425 
3426 		res->nr_output_pins = tkn_elem->value;
3427 		break;
3428 
3429 	case SKL_TKN_U32_FMT_CH:
3430 	case SKL_TKN_U32_FMT_FREQ:
3431 	case SKL_TKN_U32_FMT_BIT_DEPTH:
3432 	case SKL_TKN_U32_FMT_SAMPLE_SIZE:
3433 	case SKL_TKN_U32_FMT_CH_CONFIG:
3434 	case SKL_TKN_U32_FMT_INTERLEAVE:
3435 	case SKL_TKN_U32_FMT_SAMPLE_TYPE:
3436 	case SKL_TKN_U32_FMT_CH_MAP:
3437 	case SKL_TKN_MM_U32_INTF_PIN_ID:
3438 		ret = skl_tplg_manifest_fill_fmt(dev, fmt, tkn_elem,
3439 						 dir, pin_idx);
3440 		if (ret < 0)
3441 			return ret;
3442 		break;
3443 
3444 	default:
3445 		dev_err(dev, "Not a manifest token %d\n", tkn_elem->token);
3446 		return -EINVAL;
3447 	}
3448 	tkn_count++;
3449 
3450 	return tkn_count;
3451 }
3452 
3453 /*
3454  * Fill the manifest structure by parsing the tokens based on the
3455  * type.
3456  */
3457 static int skl_tplg_get_manifest_tkn(struct device *dev,
3458 		char *pvt_data, struct skl_dev *skl,
3459 		int block_size)
3460 {
3461 	int tkn_count = 0, ret;
3462 	int off = 0, tuple_size = 0;
3463 	u8 uuid_index = 0;
3464 	struct snd_soc_tplg_vendor_array *array;
3465 	struct snd_soc_tplg_vendor_value_elem *tkn_elem;
3466 
3467 	if (block_size <= 0)
3468 		return -EINVAL;
3469 
3470 	while (tuple_size < block_size) {
3471 		array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off);
3472 		off += array->size;
3473 		switch (array->type) {
3474 		case SND_SOC_TPLG_TUPLE_TYPE_STRING:
3475 			ret = skl_tplg_get_str_tkn(dev, array, skl);
3476 
3477 			if (ret < 0)
3478 				return ret;
3479 			tkn_count = ret;
3480 
3481 			tuple_size += tkn_count *
3482 				sizeof(struct snd_soc_tplg_vendor_string_elem);
3483 			continue;
3484 
3485 		case SND_SOC_TPLG_TUPLE_TYPE_UUID:
3486 			if (array->uuid->token != SKL_TKN_UUID) {
3487 				dev_err(dev, "Not an UUID token: %d\n",
3488 					array->uuid->token);
3489 				return -EINVAL;
3490 			}
3491 			if (uuid_index >= skl->nr_modules) {
3492 				dev_err(dev, "Too many UUID tokens\n");
3493 				return -EINVAL;
3494 			}
3495 			import_guid(&skl->modules[uuid_index++]->uuid,
3496 				    array->uuid->uuid);
3497 
3498 			tuple_size += sizeof(*array->uuid);
3499 			continue;
3500 
3501 		default:
3502 			tkn_elem = array->value;
3503 			tkn_count = 0;
3504 			break;
3505 		}
3506 
3507 		while (tkn_count <= array->num_elems - 1) {
3508 			ret = skl_tplg_get_int_tkn(dev,
3509 					tkn_elem, skl);
3510 			if (ret < 0)
3511 				return ret;
3512 
3513 			tkn_count = tkn_count + ret;
3514 			tkn_elem++;
3515 		}
3516 		tuple_size += (tkn_count * sizeof(*tkn_elem));
3517 		tkn_count = 0;
3518 	}
3519 
3520 	return off;
3521 }
3522 
3523 /*
3524  * Parse manifest private data for tokens. The private data block is
3525  * preceded by descriptors for type and size of data block.
3526  */
3527 static int skl_tplg_get_manifest_data(struct snd_soc_tplg_manifest *manifest,
3528 			struct device *dev, struct skl_dev *skl)
3529 {
3530 	struct snd_soc_tplg_vendor_array *array;
3531 	int num_blocks, block_size = 0, block_type, off = 0;
3532 	char *data;
3533 	int ret;
3534 
3535 	/* Read the NUM_DATA_BLOCKS descriptor */
3536 	array = (struct snd_soc_tplg_vendor_array *)manifest->priv.data;
3537 	ret = skl_tplg_get_desc_blocks(dev, array);
3538 	if (ret < 0)
3539 		return ret;
3540 	num_blocks = ret;
3541 
3542 	off += array->size;
3543 	/* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */
3544 	while (num_blocks > 0) {
3545 		array = (struct snd_soc_tplg_vendor_array *)
3546 				(manifest->priv.data + off);
3547 		ret = skl_tplg_get_desc_blocks(dev, array);
3548 
3549 		if (ret < 0)
3550 			return ret;
3551 		block_type = ret;
3552 		off += array->size;
3553 
3554 		array = (struct snd_soc_tplg_vendor_array *)
3555 			(manifest->priv.data + off);
3556 
3557 		ret = skl_tplg_get_desc_blocks(dev, array);
3558 
3559 		if (ret < 0)
3560 			return ret;
3561 		block_size = ret;
3562 		off += array->size;
3563 
3564 		array = (struct snd_soc_tplg_vendor_array *)
3565 			(manifest->priv.data + off);
3566 
3567 		data = (manifest->priv.data + off);
3568 
3569 		if (block_type == SKL_TYPE_TUPLE) {
3570 			ret = skl_tplg_get_manifest_tkn(dev, data, skl,
3571 					block_size);
3572 
3573 			if (ret < 0)
3574 				return ret;
3575 
3576 			--num_blocks;
3577 		} else {
3578 			return -EINVAL;
3579 		}
3580 		off += ret;
3581 	}
3582 
3583 	return 0;
3584 }
3585 
3586 static int skl_manifest_load(struct snd_soc_component *cmpnt, int index,
3587 				struct snd_soc_tplg_manifest *manifest)
3588 {
3589 	struct hdac_bus *bus = snd_soc_component_get_drvdata(cmpnt);
3590 	struct skl_dev *skl = bus_to_skl(bus);
3591 
3592 	/* proceed only if we have private data defined */
3593 	if (manifest->priv.size == 0)
3594 		return 0;
3595 
3596 	skl_tplg_get_manifest_data(manifest, bus->dev, skl);
3597 
3598 	if (skl->lib_count > SKL_MAX_LIB) {
3599 		dev_err(bus->dev, "Exceeding max Library count. Got:%d\n",
3600 					skl->lib_count);
3601 		return  -EINVAL;
3602 	}
3603 
3604 	return 0;
3605 }
3606 
3607 static void skl_tplg_complete(struct snd_soc_component *component)
3608 {
3609 	struct snd_soc_dobj *dobj;
3610 	struct snd_soc_acpi_mach *mach;
3611 	struct snd_ctl_elem_value *val;
3612 	int i;
3613 
3614 	val = kmalloc(sizeof(*val), GFP_KERNEL);
3615 	if (!val)
3616 		return;
3617 
3618 	mach = dev_get_platdata(component->card->dev);
3619 	list_for_each_entry(dobj, &component->dobj_list, list) {
3620 		struct snd_kcontrol *kcontrol = dobj->control.kcontrol;
3621 		struct soc_enum *se;
3622 		char **texts;
3623 		char chan_text[4];
3624 
3625 		if (dobj->type != SND_SOC_DOBJ_ENUM || !kcontrol ||
3626 		    kcontrol->put != skl_tplg_multi_config_set_dmic)
3627 			continue;
3628 
3629 		se = (struct soc_enum *)kcontrol->private_value;
3630 		texts = dobj->control.dtexts;
3631 		sprintf(chan_text, "c%d", mach->mach_params.dmic_num);
3632 
3633 		for (i = 0; i < se->items; i++) {
3634 			if (strstr(texts[i], chan_text)) {
3635 				memset(val, 0, sizeof(*val));
3636 				val->value.enumerated.item[0] = i;
3637 				kcontrol->put(kcontrol, val);
3638 			}
3639 		}
3640 	}
3641 	kfree(val);
3642 }
3643 
3644 static struct snd_soc_tplg_ops skl_tplg_ops  = {
3645 	.widget_load = skl_tplg_widget_load,
3646 	.control_load = skl_tplg_control_load,
3647 	.bytes_ext_ops = skl_tlv_ops,
3648 	.bytes_ext_ops_count = ARRAY_SIZE(skl_tlv_ops),
3649 	.io_ops = skl_tplg_kcontrol_ops,
3650 	.io_ops_count = ARRAY_SIZE(skl_tplg_kcontrol_ops),
3651 	.manifest = skl_manifest_load,
3652 	.dai_load = skl_dai_load,
3653 	.complete = skl_tplg_complete,
3654 };
3655 
3656 /*
3657  * A pipe can have multiple modules, each of them will be a DAPM widget as
3658  * well. While managing a pipeline we need to get the list of all the
3659  * widgets in a pipelines, so this helper - skl_tplg_create_pipe_widget_list()
3660  * helps to get the SKL type widgets in that pipeline
3661  */
3662 static int skl_tplg_create_pipe_widget_list(struct snd_soc_component *component)
3663 {
3664 	struct snd_soc_dapm_widget *w;
3665 	struct skl_module_cfg *mcfg = NULL;
3666 	struct skl_pipe_module *p_module = NULL;
3667 	struct skl_pipe *pipe;
3668 
3669 	list_for_each_entry(w, &component->card->widgets, list) {
3670 		if (is_skl_dsp_widget_type(w, component->dev) && w->priv) {
3671 			mcfg = w->priv;
3672 			pipe = mcfg->pipe;
3673 
3674 			p_module = devm_kzalloc(component->dev,
3675 						sizeof(*p_module), GFP_KERNEL);
3676 			if (!p_module)
3677 				return -ENOMEM;
3678 
3679 			p_module->w = w;
3680 			list_add_tail(&p_module->node, &pipe->w_list);
3681 		}
3682 	}
3683 
3684 	return 0;
3685 }
3686 
3687 static void skl_tplg_set_pipe_type(struct skl_dev *skl, struct skl_pipe *pipe)
3688 {
3689 	struct skl_pipe_module *w_module;
3690 	struct snd_soc_dapm_widget *w;
3691 	struct skl_module_cfg *mconfig;
3692 	bool host_found = false, link_found = false;
3693 
3694 	list_for_each_entry(w_module, &pipe->w_list, node) {
3695 		w = w_module->w;
3696 		mconfig = w->priv;
3697 
3698 		if (mconfig->dev_type == SKL_DEVICE_HDAHOST)
3699 			host_found = true;
3700 		else if (mconfig->dev_type != SKL_DEVICE_NONE)
3701 			link_found = true;
3702 	}
3703 
3704 	if (host_found && link_found)
3705 		pipe->passthru = true;
3706 	else
3707 		pipe->passthru = false;
3708 }
3709 
3710 /*
3711  * SKL topology init routine
3712  */
3713 int skl_tplg_init(struct snd_soc_component *component, struct hdac_bus *bus)
3714 {
3715 	int ret;
3716 	const struct firmware *fw;
3717 	struct skl_dev *skl = bus_to_skl(bus);
3718 	struct skl_pipeline *ppl;
3719 
3720 	ret = request_firmware(&fw, skl->tplg_name, bus->dev);
3721 	if (ret < 0) {
3722 		char alt_tplg_name[64];
3723 
3724 		snprintf(alt_tplg_name, sizeof(alt_tplg_name), "%s-tplg.bin",
3725 			 skl->mach->drv_name);
3726 		dev_info(bus->dev, "tplg fw %s load failed with %d, trying alternative tplg name %s",
3727 			 skl->tplg_name, ret, alt_tplg_name);
3728 
3729 		ret = request_firmware(&fw, alt_tplg_name, bus->dev);
3730 		if (!ret)
3731 			goto component_load;
3732 
3733 		dev_info(bus->dev, "tplg %s failed with %d, falling back to dfw_sst.bin",
3734 			 alt_tplg_name, ret);
3735 
3736 		ret = request_firmware(&fw, "dfw_sst.bin", bus->dev);
3737 		if (ret < 0) {
3738 			dev_err(bus->dev, "Fallback tplg fw %s load failed with %d\n",
3739 					"dfw_sst.bin", ret);
3740 			return ret;
3741 		}
3742 	}
3743 
3744 component_load:
3745 	ret = snd_soc_tplg_component_load(component, &skl_tplg_ops, fw);
3746 	if (ret < 0) {
3747 		dev_err(bus->dev, "tplg component load failed%d\n", ret);
3748 		goto err;
3749 	}
3750 
3751 	ret = skl_tplg_create_pipe_widget_list(component);
3752 	if (ret < 0) {
3753 		dev_err(bus->dev, "tplg create pipe widget list failed%d\n",
3754 				ret);
3755 		goto err;
3756 	}
3757 
3758 	list_for_each_entry(ppl, &skl->ppl_list, node)
3759 		skl_tplg_set_pipe_type(skl, ppl->pipe);
3760 
3761 err:
3762 	release_firmware(fw);
3763 	return ret;
3764 }
3765 
3766 void skl_tplg_exit(struct snd_soc_component *component, struct hdac_bus *bus)
3767 {
3768 	struct skl_dev *skl = bus_to_skl(bus);
3769 	struct skl_pipeline *ppl, *tmp;
3770 
3771 	list_for_each_entry_safe(ppl, tmp, &skl->ppl_list, node)
3772 		list_del(&ppl->node);
3773 
3774 	/* clean up topology */
3775 	snd_soc_tplg_component_remove(component);
3776 }
3777