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