xref: /openbmc/linux/sound/soc/intel/skylake/skl-topology.c (revision 76f56fae1cf9040325a58d1375291baf71dfaf03)
1e4e2d2f4SJeeja KP /*
2e4e2d2f4SJeeja KP  *  skl-topology.c - Implements Platform component ALSA controls/widget
3e4e2d2f4SJeeja KP  *  handlers.
4e4e2d2f4SJeeja KP  *
5e4e2d2f4SJeeja KP  *  Copyright (C) 2014-2015 Intel Corp
6e4e2d2f4SJeeja KP  *  Author: Jeeja KP <jeeja.kp@intel.com>
7e4e2d2f4SJeeja KP  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8e4e2d2f4SJeeja KP  *
9e4e2d2f4SJeeja KP  * This program is free software; you can redistribute it and/or modify
10e4e2d2f4SJeeja KP  * it under the terms of the GNU General Public License as version 2, as
11e4e2d2f4SJeeja KP  * published by the Free Software Foundation.
12e4e2d2f4SJeeja KP  *
13e4e2d2f4SJeeja KP  * This program is distributed in the hope that it will be useful, but
14e4e2d2f4SJeeja KP  * WITHOUT ANY WARRANTY; without even the implied warranty of
15e4e2d2f4SJeeja KP  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16e4e2d2f4SJeeja KP  * General Public License for more details.
17e4e2d2f4SJeeja KP  */
18e4e2d2f4SJeeja KP 
19e4e2d2f4SJeeja KP #include <linux/slab.h>
20e4e2d2f4SJeeja KP #include <linux/types.h>
21e4e2d2f4SJeeja KP #include <linux/firmware.h>
22ac9391daSGuenter Roeck #include <linux/uuid.h>
23e4e2d2f4SJeeja KP #include <sound/soc.h>
24e4e2d2f4SJeeja KP #include <sound/soc-topology.h>
256277e832SShreyas NC #include <uapi/sound/snd_sst_tokens.h>
260c24fdc0SGuenter Roeck #include <uapi/sound/skl-tplg-interface.h>
27e4e2d2f4SJeeja KP #include "skl-sst-dsp.h"
28e4e2d2f4SJeeja KP #include "skl-sst-ipc.h"
29e4e2d2f4SJeeja KP #include "skl-topology.h"
30e4e2d2f4SJeeja KP #include "skl.h"
316c5768b3SDharageswari R #include "../common/sst-dsp.h"
326c5768b3SDharageswari R #include "../common/sst-dsp-priv.h"
33e4e2d2f4SJeeja KP 
34f7590d4fSJeeja KP #define SKL_CH_FIXUP_MASK		(1 << 0)
35f7590d4fSJeeja KP #define SKL_RATE_FIXUP_MASK		(1 << 1)
36f7590d4fSJeeja KP #define SKL_FMT_FIXUP_MASK		(1 << 2)
376277e832SShreyas NC #define SKL_IN_DIR_BIT_MASK		BIT(0)
386277e832SShreyas NC #define SKL_PIN_COUNT_MASK		GENMASK(7, 4)
39f7590d4fSJeeja KP 
407a1b749bSDharageswari R static const int mic_mono_list[] = {
417a1b749bSDharageswari R 0, 1, 2, 3,
427a1b749bSDharageswari R };
437a1b749bSDharageswari R static const int mic_stereo_list[][SKL_CH_STEREO] = {
447a1b749bSDharageswari R {0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 3}, {2, 3},
457a1b749bSDharageswari R };
467a1b749bSDharageswari R static const int mic_trio_list[][SKL_CH_TRIO] = {
477a1b749bSDharageswari R {0, 1, 2}, {0, 1, 3}, {0, 2, 3}, {1, 2, 3},
487a1b749bSDharageswari R };
497a1b749bSDharageswari R static const int mic_quatro_list[][SKL_CH_QUATRO] = {
507a1b749bSDharageswari R {0, 1, 2, 3},
517a1b749bSDharageswari R };
527a1b749bSDharageswari R 
53f6fa56e2SRamesh Babu #define CHECK_HW_PARAMS(ch, freq, bps, prm_ch, prm_freq, prm_bps) \
54f6fa56e2SRamesh Babu 	((ch == prm_ch) && (bps == prm_bps) && (freq == prm_freq))
55f6fa56e2SRamesh Babu 
56a83e3b4cSVinod Koul void skl_tplg_d0i3_get(struct skl *skl, enum d0i3_capability caps)
57a83e3b4cSVinod Koul {
58a83e3b4cSVinod Koul 	struct skl_d0i3_data *d0i3 =  &skl->skl_sst->d0i3;
59a83e3b4cSVinod Koul 
60a83e3b4cSVinod Koul 	switch (caps) {
61a83e3b4cSVinod Koul 	case SKL_D0I3_NONE:
62a83e3b4cSVinod Koul 		d0i3->non_d0i3++;
63a83e3b4cSVinod Koul 		break;
64a83e3b4cSVinod Koul 
65a83e3b4cSVinod Koul 	case SKL_D0I3_STREAMING:
66a83e3b4cSVinod Koul 		d0i3->streaming++;
67a83e3b4cSVinod Koul 		break;
68a83e3b4cSVinod Koul 
69a83e3b4cSVinod Koul 	case SKL_D0I3_NON_STREAMING:
70a83e3b4cSVinod Koul 		d0i3->non_streaming++;
71a83e3b4cSVinod Koul 		break;
72a83e3b4cSVinod Koul 	}
73a83e3b4cSVinod Koul }
74a83e3b4cSVinod Koul 
75a83e3b4cSVinod Koul void skl_tplg_d0i3_put(struct skl *skl, enum d0i3_capability caps)
76a83e3b4cSVinod Koul {
77a83e3b4cSVinod Koul 	struct skl_d0i3_data *d0i3 =  &skl->skl_sst->d0i3;
78a83e3b4cSVinod Koul 
79a83e3b4cSVinod Koul 	switch (caps) {
80a83e3b4cSVinod Koul 	case SKL_D0I3_NONE:
81a83e3b4cSVinod Koul 		d0i3->non_d0i3--;
82a83e3b4cSVinod Koul 		break;
83a83e3b4cSVinod Koul 
84a83e3b4cSVinod Koul 	case SKL_D0I3_STREAMING:
85a83e3b4cSVinod Koul 		d0i3->streaming--;
86a83e3b4cSVinod Koul 		break;
87a83e3b4cSVinod Koul 
88a83e3b4cSVinod Koul 	case SKL_D0I3_NON_STREAMING:
89a83e3b4cSVinod Koul 		d0i3->non_streaming--;
90a83e3b4cSVinod Koul 		break;
91a83e3b4cSVinod Koul 	}
92a83e3b4cSVinod Koul }
93a83e3b4cSVinod Koul 
94e4e2d2f4SJeeja KP /*
95e4e2d2f4SJeeja KP  * SKL DSP driver modelling uses only few DAPM widgets so for rest we will
96e4e2d2f4SJeeja KP  * ignore. This helpers checks if the SKL driver handles this widget type
97e4e2d2f4SJeeja KP  */
98cb1f904dSGuneshwor Singh static int is_skl_dsp_widget_type(struct snd_soc_dapm_widget *w,
99cb1f904dSGuneshwor Singh 				  struct device *dev)
100e4e2d2f4SJeeja KP {
101cb1f904dSGuneshwor Singh 	if (w->dapm->dev != dev)
102cb1f904dSGuneshwor Singh 		return false;
103cb1f904dSGuneshwor Singh 
104e4e2d2f4SJeeja KP 	switch (w->id) {
105e4e2d2f4SJeeja KP 	case snd_soc_dapm_dai_link:
106e4e2d2f4SJeeja KP 	case snd_soc_dapm_dai_in:
107e4e2d2f4SJeeja KP 	case snd_soc_dapm_aif_in:
108e4e2d2f4SJeeja KP 	case snd_soc_dapm_aif_out:
109e4e2d2f4SJeeja KP 	case snd_soc_dapm_dai_out:
110e4e2d2f4SJeeja KP 	case snd_soc_dapm_switch:
111e4e2d2f4SJeeja KP 		return false;
112e4e2d2f4SJeeja KP 	default:
113e4e2d2f4SJeeja KP 		return true;
114e4e2d2f4SJeeja KP 	}
115e4e2d2f4SJeeja KP }
116e4e2d2f4SJeeja KP 
117e4e2d2f4SJeeja KP /*
118e4e2d2f4SJeeja KP  * Each pipelines needs memory to be allocated. Check if we have free memory
1199ba8ffefSDharageswari.R  * from available pool.
120e4e2d2f4SJeeja KP  */
1219ba8ffefSDharageswari.R static bool skl_is_pipe_mem_avail(struct skl *skl,
122e4e2d2f4SJeeja KP 				struct skl_module_cfg *mconfig)
123e4e2d2f4SJeeja KP {
124e4e2d2f4SJeeja KP 	struct skl_sst *ctx = skl->skl_sst;
125e4e2d2f4SJeeja KP 
126e4e2d2f4SJeeja KP 	if (skl->resource.mem + mconfig->pipe->memory_pages >
127e4e2d2f4SJeeja KP 				skl->resource.max_mem) {
128e4e2d2f4SJeeja KP 		dev_err(ctx->dev,
129e4e2d2f4SJeeja KP 				"%s: module_id %d instance %d\n", __func__,
130e4e2d2f4SJeeja KP 				mconfig->id.module_id,
131e4e2d2f4SJeeja KP 				mconfig->id.instance_id);
132e4e2d2f4SJeeja KP 		dev_err(ctx->dev,
133e4e2d2f4SJeeja KP 				"exceeds ppl memory available %d mem %d\n",
134e4e2d2f4SJeeja KP 				skl->resource.max_mem, skl->resource.mem);
135e4e2d2f4SJeeja KP 		return false;
1369ba8ffefSDharageswari.R 	} else {
1379ba8ffefSDharageswari.R 		return true;
1389ba8ffefSDharageswari.R 	}
139e4e2d2f4SJeeja KP }
140e4e2d2f4SJeeja KP 
1419ba8ffefSDharageswari.R /*
1429ba8ffefSDharageswari.R  * Add the mem to the mem pool. This is freed when pipe is deleted.
1439ba8ffefSDharageswari.R  * Note: DSP does actual memory management we only keep track for complete
1449ba8ffefSDharageswari.R  * pool
1459ba8ffefSDharageswari.R  */
1469ba8ffefSDharageswari.R static void skl_tplg_alloc_pipe_mem(struct skl *skl,
1479ba8ffefSDharageswari.R 				struct skl_module_cfg *mconfig)
1489ba8ffefSDharageswari.R {
149e4e2d2f4SJeeja KP 	skl->resource.mem += mconfig->pipe->memory_pages;
150e4e2d2f4SJeeja KP }
151e4e2d2f4SJeeja KP 
152e4e2d2f4SJeeja KP /*
153e4e2d2f4SJeeja KP  * Pipeline needs needs DSP CPU resources for computation, this is
154e4e2d2f4SJeeja KP  * quantified in MCPS (Million Clocks Per Second) required for module/pipe
155e4e2d2f4SJeeja KP  *
156e4e2d2f4SJeeja KP  * Each pipelines needs mcps to be allocated. Check if we have mcps for this
1579ba8ffefSDharageswari.R  * pipe.
158e4e2d2f4SJeeja KP  */
1599ba8ffefSDharageswari.R 
1609ba8ffefSDharageswari.R static bool skl_is_pipe_mcps_avail(struct skl *skl,
161e4e2d2f4SJeeja KP 				struct skl_module_cfg *mconfig)
162e4e2d2f4SJeeja KP {
163e4e2d2f4SJeeja KP 	struct skl_sst *ctx = skl->skl_sst;
164f6fa56e2SRamesh Babu 	u8 res_idx = mconfig->res_idx;
165f6fa56e2SRamesh Babu 	struct skl_module_res *res = &mconfig->module->resources[res_idx];
166e4e2d2f4SJeeja KP 
167f6fa56e2SRamesh Babu 	if (skl->resource.mcps + res->cps > skl->resource.max_mcps) {
168e4e2d2f4SJeeja KP 		dev_err(ctx->dev,
169e4e2d2f4SJeeja KP 			"%s: module_id %d instance %d\n", __func__,
170e4e2d2f4SJeeja KP 			mconfig->id.module_id, mconfig->id.instance_id);
171e4e2d2f4SJeeja KP 		dev_err(ctx->dev,
1727ca42f5aSGuneshwor Singh 			"exceeds ppl mcps available %d > mem %d\n",
173e4e2d2f4SJeeja KP 			skl->resource.max_mcps, skl->resource.mcps);
174e4e2d2f4SJeeja KP 		return false;
1759ba8ffefSDharageswari.R 	} else {
1769ba8ffefSDharageswari.R 		return true;
1779ba8ffefSDharageswari.R 	}
178e4e2d2f4SJeeja KP }
179e4e2d2f4SJeeja KP 
1809ba8ffefSDharageswari.R static void skl_tplg_alloc_pipe_mcps(struct skl *skl,
1819ba8ffefSDharageswari.R 				struct skl_module_cfg *mconfig)
1829ba8ffefSDharageswari.R {
183f6fa56e2SRamesh Babu 	u8 res_idx = mconfig->res_idx;
184f6fa56e2SRamesh Babu 	struct skl_module_res *res = &mconfig->module->resources[res_idx];
185f6fa56e2SRamesh Babu 
186f6fa56e2SRamesh Babu 	skl->resource.mcps += res->cps;
187e4e2d2f4SJeeja KP }
188e4e2d2f4SJeeja KP 
189e4e2d2f4SJeeja KP /*
190e4e2d2f4SJeeja KP  * Free the mcps when tearing down
191e4e2d2f4SJeeja KP  */
192e4e2d2f4SJeeja KP static void
193e4e2d2f4SJeeja KP skl_tplg_free_pipe_mcps(struct skl *skl, struct skl_module_cfg *mconfig)
194e4e2d2f4SJeeja KP {
195f6fa56e2SRamesh Babu 	u8 res_idx = mconfig->res_idx;
196f6fa56e2SRamesh Babu 	struct skl_module_res *res = &mconfig->module->resources[res_idx];
197f6fa56e2SRamesh Babu 
198f6fa56e2SRamesh Babu 	skl->resource.mcps -= res->cps;
199e4e2d2f4SJeeja KP }
200e4e2d2f4SJeeja KP 
201e4e2d2f4SJeeja KP /*
202e4e2d2f4SJeeja KP  * Free the memory when tearing down
203e4e2d2f4SJeeja KP  */
204e4e2d2f4SJeeja KP static void
205e4e2d2f4SJeeja KP skl_tplg_free_pipe_mem(struct skl *skl, struct skl_module_cfg *mconfig)
206e4e2d2f4SJeeja KP {
207e4e2d2f4SJeeja KP 	skl->resource.mem -= mconfig->pipe->memory_pages;
208e4e2d2f4SJeeja KP }
209e4e2d2f4SJeeja KP 
210f7590d4fSJeeja KP 
211f7590d4fSJeeja KP static void skl_dump_mconfig(struct skl_sst *ctx,
212f7590d4fSJeeja KP 					struct skl_module_cfg *mcfg)
213f7590d4fSJeeja KP {
214f6fa56e2SRamesh Babu 	struct skl_module_iface *iface = &mcfg->module->formats[0];
215f6fa56e2SRamesh Babu 
216f7590d4fSJeeja KP 	dev_dbg(ctx->dev, "Dumping config\n");
217f7590d4fSJeeja KP 	dev_dbg(ctx->dev, "Input Format:\n");
218f6fa56e2SRamesh Babu 	dev_dbg(ctx->dev, "channels = %d\n", iface->inputs[0].fmt.channels);
219f6fa56e2SRamesh Babu 	dev_dbg(ctx->dev, "s_freq = %d\n", iface->inputs[0].fmt.s_freq);
220f6fa56e2SRamesh Babu 	dev_dbg(ctx->dev, "ch_cfg = %d\n", iface->inputs[0].fmt.ch_cfg);
221f6fa56e2SRamesh Babu 	dev_dbg(ctx->dev, "valid bit depth = %d\n",
222f6fa56e2SRamesh Babu 				iface->inputs[0].fmt.valid_bit_depth);
223f7590d4fSJeeja KP 	dev_dbg(ctx->dev, "Output Format:\n");
224f6fa56e2SRamesh Babu 	dev_dbg(ctx->dev, "channels = %d\n", iface->outputs[0].fmt.channels);
225f6fa56e2SRamesh Babu 	dev_dbg(ctx->dev, "s_freq = %d\n", iface->outputs[0].fmt.s_freq);
226f6fa56e2SRamesh Babu 	dev_dbg(ctx->dev, "valid bit depth = %d\n",
227f6fa56e2SRamesh Babu 				iface->outputs[0].fmt.valid_bit_depth);
228f6fa56e2SRamesh Babu 	dev_dbg(ctx->dev, "ch_cfg = %d\n", iface->outputs[0].fmt.ch_cfg);
229f7590d4fSJeeja KP }
230f7590d4fSJeeja KP 
231ea5a137dSSubhransu S. Prusty static void skl_tplg_update_chmap(struct skl_module_fmt *fmt, int chs)
232ea5a137dSSubhransu S. Prusty {
233ea5a137dSSubhransu S. Prusty 	int slot_map = 0xFFFFFFFF;
234ea5a137dSSubhransu S. Prusty 	int start_slot = 0;
235ea5a137dSSubhransu S. Prusty 	int i;
236ea5a137dSSubhransu S. Prusty 
237ea5a137dSSubhransu S. Prusty 	for (i = 0; i < chs; i++) {
238ea5a137dSSubhransu S. Prusty 		/*
239ea5a137dSSubhransu S. Prusty 		 * For 2 channels with starting slot as 0, slot map will
240ea5a137dSSubhransu S. Prusty 		 * look like 0xFFFFFF10.
241ea5a137dSSubhransu S. Prusty 		 */
242ea5a137dSSubhransu S. Prusty 		slot_map &= (~(0xF << (4 * i)) | (start_slot << (4 * i)));
243ea5a137dSSubhransu S. Prusty 		start_slot++;
244ea5a137dSSubhransu S. Prusty 	}
245ea5a137dSSubhransu S. Prusty 	fmt->ch_map = slot_map;
246ea5a137dSSubhransu S. Prusty }
247ea5a137dSSubhransu S. Prusty 
248f7590d4fSJeeja KP static void skl_tplg_update_params(struct skl_module_fmt *fmt,
249f7590d4fSJeeja KP 			struct skl_pipe_params *params, int fixup)
250f7590d4fSJeeja KP {
251f7590d4fSJeeja KP 	if (fixup & SKL_RATE_FIXUP_MASK)
252f7590d4fSJeeja KP 		fmt->s_freq = params->s_freq;
253ea5a137dSSubhransu S. Prusty 	if (fixup & SKL_CH_FIXUP_MASK) {
254f7590d4fSJeeja KP 		fmt->channels = params->ch;
255ea5a137dSSubhransu S. Prusty 		skl_tplg_update_chmap(fmt, fmt->channels);
256ea5a137dSSubhransu S. Prusty 	}
25798256f83SJeeja KP 	if (fixup & SKL_FMT_FIXUP_MASK) {
25898256f83SJeeja KP 		fmt->valid_bit_depth = skl_get_bit_depth(params->s_fmt);
25998256f83SJeeja KP 
26098256f83SJeeja KP 		/*
26198256f83SJeeja KP 		 * 16 bit is 16 bit container whereas 24 bit is in 32 bit
26298256f83SJeeja KP 		 * container so update bit depth accordingly
26398256f83SJeeja KP 		 */
26498256f83SJeeja KP 		switch (fmt->valid_bit_depth) {
26598256f83SJeeja KP 		case SKL_DEPTH_16BIT:
26698256f83SJeeja KP 			fmt->bit_depth = fmt->valid_bit_depth;
26798256f83SJeeja KP 			break;
26898256f83SJeeja KP 
26998256f83SJeeja KP 		default:
27098256f83SJeeja KP 			fmt->bit_depth = SKL_DEPTH_32BIT;
27198256f83SJeeja KP 			break;
27298256f83SJeeja KP 		}
27398256f83SJeeja KP 	}
27498256f83SJeeja KP 
275f7590d4fSJeeja KP }
276f7590d4fSJeeja KP 
277f7590d4fSJeeja KP /*
278f7590d4fSJeeja KP  * A pipeline may have modules which impact the pcm parameters, like SRC,
279f7590d4fSJeeja KP  * channel converter, format converter.
280f7590d4fSJeeja KP  * We need to calculate the output params by applying the 'fixup'
281f7590d4fSJeeja KP  * Topology will tell driver which type of fixup is to be applied by
282f7590d4fSJeeja KP  * supplying the fixup mask, so based on that we calculate the output
283f7590d4fSJeeja KP  *
284f7590d4fSJeeja KP  * Now In FE the pcm hw_params is source/target format. Same is applicable
285f7590d4fSJeeja KP  * for BE with its hw_params invoked.
286f7590d4fSJeeja KP  * here based on FE, BE pipeline and direction we calculate the input and
287f7590d4fSJeeja KP  * outfix and then apply that for a module
288f7590d4fSJeeja KP  */
289f7590d4fSJeeja KP static void skl_tplg_update_params_fixup(struct skl_module_cfg *m_cfg,
290f7590d4fSJeeja KP 		struct skl_pipe_params *params, bool is_fe)
291f7590d4fSJeeja KP {
292f7590d4fSJeeja KP 	int in_fixup, out_fixup;
293f7590d4fSJeeja KP 	struct skl_module_fmt *in_fmt, *out_fmt;
294f7590d4fSJeeja KP 
2954cd9899fSHardik T Shah 	/* Fixups will be applied to pin 0 only */
296f6fa56e2SRamesh Babu 	in_fmt = &m_cfg->module->formats[0].inputs[0].fmt;
297f6fa56e2SRamesh Babu 	out_fmt = &m_cfg->module->formats[0].outputs[0].fmt;
298f7590d4fSJeeja KP 
299f7590d4fSJeeja KP 	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
300f7590d4fSJeeja KP 		if (is_fe) {
301f7590d4fSJeeja KP 			in_fixup = m_cfg->params_fixup;
302f7590d4fSJeeja KP 			out_fixup = (~m_cfg->converter) &
303f7590d4fSJeeja KP 					m_cfg->params_fixup;
304f7590d4fSJeeja KP 		} else {
305f7590d4fSJeeja KP 			out_fixup = m_cfg->params_fixup;
306f7590d4fSJeeja KP 			in_fixup = (~m_cfg->converter) &
307f7590d4fSJeeja KP 					m_cfg->params_fixup;
308f7590d4fSJeeja KP 		}
309f7590d4fSJeeja KP 	} else {
310f7590d4fSJeeja KP 		if (is_fe) {
311f7590d4fSJeeja KP 			out_fixup = m_cfg->params_fixup;
312f7590d4fSJeeja KP 			in_fixup = (~m_cfg->converter) &
313f7590d4fSJeeja KP 					m_cfg->params_fixup;
314f7590d4fSJeeja KP 		} else {
315f7590d4fSJeeja KP 			in_fixup = m_cfg->params_fixup;
316f7590d4fSJeeja KP 			out_fixup = (~m_cfg->converter) &
317f7590d4fSJeeja KP 					m_cfg->params_fixup;
318f7590d4fSJeeja KP 		}
319f7590d4fSJeeja KP 	}
320f7590d4fSJeeja KP 
321f7590d4fSJeeja KP 	skl_tplg_update_params(in_fmt, params, in_fixup);
322f7590d4fSJeeja KP 	skl_tplg_update_params(out_fmt, params, out_fixup);
323f7590d4fSJeeja KP }
324f7590d4fSJeeja KP 
325f7590d4fSJeeja KP /*
326f7590d4fSJeeja KP  * A module needs input and output buffers, which are dependent upon pcm
327f7590d4fSJeeja KP  * params, so once we have calculate params, we need buffer calculation as
328f7590d4fSJeeja KP  * well.
329f7590d4fSJeeja KP  */
330f7590d4fSJeeja KP static void skl_tplg_update_buffer_size(struct skl_sst *ctx,
331f7590d4fSJeeja KP 				struct skl_module_cfg *mcfg)
332f7590d4fSJeeja KP {
333f7590d4fSJeeja KP 	int multiplier = 1;
3344cd9899fSHardik T Shah 	struct skl_module_fmt *in_fmt, *out_fmt;
335f6fa56e2SRamesh Babu 	struct skl_module_res *res;
3364cd9899fSHardik T Shah 
3374cd9899fSHardik T Shah 	/* Since fixups is applied to pin 0 only, ibs, obs needs
3384cd9899fSHardik T Shah 	 * change for pin 0 only
3394cd9899fSHardik T Shah 	 */
340f6fa56e2SRamesh Babu 	res = &mcfg->module->resources[0];
341f6fa56e2SRamesh Babu 	in_fmt = &mcfg->module->formats[0].inputs[0].fmt;
342f6fa56e2SRamesh Babu 	out_fmt = &mcfg->module->formats[0].outputs[0].fmt;
343f7590d4fSJeeja KP 
344f7590d4fSJeeja KP 	if (mcfg->m_type == SKL_MODULE_TYPE_SRCINT)
345f7590d4fSJeeja KP 		multiplier = 5;
346f0c8e1d9SSubhransu S. Prusty 
347f6fa56e2SRamesh Babu 	res->ibs = DIV_ROUND_UP(in_fmt->s_freq, 1000) *
348998d6fb5STakashi Sakamoto 			in_fmt->channels * (in_fmt->bit_depth >> 3) *
349f7590d4fSJeeja KP 			multiplier;
350f7590d4fSJeeja KP 
351f6fa56e2SRamesh Babu 	res->obs = DIV_ROUND_UP(out_fmt->s_freq, 1000) *
352998d6fb5STakashi Sakamoto 			out_fmt->channels * (out_fmt->bit_depth >> 3) *
353f7590d4fSJeeja KP 			multiplier;
354f7590d4fSJeeja KP }
355f7590d4fSJeeja KP 
356db2f586bSSenthilnathan Veppur static u8 skl_tplg_be_dev_type(int dev_type)
357db2f586bSSenthilnathan Veppur {
358db2f586bSSenthilnathan Veppur 	int ret;
359db2f586bSSenthilnathan Veppur 
360db2f586bSSenthilnathan Veppur 	switch (dev_type) {
361db2f586bSSenthilnathan Veppur 	case SKL_DEVICE_BT:
362db2f586bSSenthilnathan Veppur 		ret = NHLT_DEVICE_BT;
363db2f586bSSenthilnathan Veppur 		break;
364db2f586bSSenthilnathan Veppur 
365db2f586bSSenthilnathan Veppur 	case SKL_DEVICE_DMIC:
366db2f586bSSenthilnathan Veppur 		ret = NHLT_DEVICE_DMIC;
367db2f586bSSenthilnathan Veppur 		break;
368db2f586bSSenthilnathan Veppur 
369db2f586bSSenthilnathan Veppur 	case SKL_DEVICE_I2S:
370db2f586bSSenthilnathan Veppur 		ret = NHLT_DEVICE_I2S;
371db2f586bSSenthilnathan Veppur 		break;
372db2f586bSSenthilnathan Veppur 
373db2f586bSSenthilnathan Veppur 	default:
374db2f586bSSenthilnathan Veppur 		ret = NHLT_DEVICE_INVALID;
375db2f586bSSenthilnathan Veppur 		break;
376db2f586bSSenthilnathan Veppur 	}
377db2f586bSSenthilnathan Veppur 
378db2f586bSSenthilnathan Veppur 	return ret;
379db2f586bSSenthilnathan Veppur }
380db2f586bSSenthilnathan Veppur 
3812d1419a3SJeeja KP static int skl_tplg_update_be_blob(struct snd_soc_dapm_widget *w,
3822d1419a3SJeeja KP 						struct skl_sst *ctx)
3832d1419a3SJeeja KP {
3842d1419a3SJeeja KP 	struct skl_module_cfg *m_cfg = w->priv;
3852d1419a3SJeeja KP 	int link_type, dir;
3862d1419a3SJeeja KP 	u32 ch, s_freq, s_fmt;
3872d1419a3SJeeja KP 	struct nhlt_specific_cfg *cfg;
3882d1419a3SJeeja KP 	struct skl *skl = get_skl_ctx(ctx->dev);
389db2f586bSSenthilnathan Veppur 	u8 dev_type = skl_tplg_be_dev_type(m_cfg->dev_type);
390f6fa56e2SRamesh Babu 	int fmt_idx = m_cfg->fmt_idx;
391f6fa56e2SRamesh Babu 	struct skl_module_iface *m_iface = &m_cfg->module->formats[fmt_idx];
3922d1419a3SJeeja KP 
3932d1419a3SJeeja KP 	/* check if we already have blob */
3942d1419a3SJeeja KP 	if (m_cfg->formats_config.caps_size > 0)
3952d1419a3SJeeja KP 		return 0;
3962d1419a3SJeeja KP 
397c7c6c736SJeeja KP 	dev_dbg(ctx->dev, "Applying default cfg blob\n");
3982d1419a3SJeeja KP 	switch (m_cfg->dev_type) {
3992d1419a3SJeeja KP 	case SKL_DEVICE_DMIC:
4002d1419a3SJeeja KP 		link_type = NHLT_LINK_DMIC;
401c7c6c736SJeeja KP 		dir = SNDRV_PCM_STREAM_CAPTURE;
402f6fa56e2SRamesh Babu 		s_freq = m_iface->inputs[0].fmt.s_freq;
403f6fa56e2SRamesh Babu 		s_fmt = m_iface->inputs[0].fmt.bit_depth;
404f6fa56e2SRamesh Babu 		ch = m_iface->inputs[0].fmt.channels;
4052d1419a3SJeeja KP 		break;
4062d1419a3SJeeja KP 
4072d1419a3SJeeja KP 	case SKL_DEVICE_I2S:
4082d1419a3SJeeja KP 		link_type = NHLT_LINK_SSP;
4092d1419a3SJeeja KP 		if (m_cfg->hw_conn_type == SKL_CONN_SOURCE) {
410c7c6c736SJeeja KP 			dir = SNDRV_PCM_STREAM_PLAYBACK;
411f6fa56e2SRamesh Babu 			s_freq = m_iface->outputs[0].fmt.s_freq;
412f6fa56e2SRamesh Babu 			s_fmt = m_iface->outputs[0].fmt.bit_depth;
413f6fa56e2SRamesh Babu 			ch = m_iface->outputs[0].fmt.channels;
414c7c6c736SJeeja KP 		} else {
415c7c6c736SJeeja KP 			dir = SNDRV_PCM_STREAM_CAPTURE;
416f6fa56e2SRamesh Babu 			s_freq = m_iface->inputs[0].fmt.s_freq;
417f6fa56e2SRamesh Babu 			s_fmt = m_iface->inputs[0].fmt.bit_depth;
418f6fa56e2SRamesh Babu 			ch = m_iface->inputs[0].fmt.channels;
4192d1419a3SJeeja KP 		}
4202d1419a3SJeeja KP 		break;
4212d1419a3SJeeja KP 
4222d1419a3SJeeja KP 	default:
4232d1419a3SJeeja KP 		return -EINVAL;
4242d1419a3SJeeja KP 	}
4252d1419a3SJeeja KP 
4262d1419a3SJeeja KP 	/* update the blob based on virtual bus_id and default params */
4272d1419a3SJeeja KP 	cfg = skl_get_ep_blob(skl, m_cfg->vbus_id, link_type,
428db2f586bSSenthilnathan Veppur 					s_fmt, ch, s_freq, dir, dev_type);
4292d1419a3SJeeja KP 	if (cfg) {
4302d1419a3SJeeja KP 		m_cfg->formats_config.caps_size = cfg->size;
4312d1419a3SJeeja KP 		m_cfg->formats_config.caps = (u32 *) &cfg->caps;
4322d1419a3SJeeja KP 	} else {
4332d1419a3SJeeja KP 		dev_err(ctx->dev, "Blob NULL for id %x type %d dirn %d\n",
4342d1419a3SJeeja KP 					m_cfg->vbus_id, link_type, dir);
4352d1419a3SJeeja KP 		dev_err(ctx->dev, "PCM: ch %d, freq %d, fmt %d\n",
4362d1419a3SJeeja KP 					ch, s_freq, s_fmt);
4372d1419a3SJeeja KP 		return -EIO;
4382d1419a3SJeeja KP 	}
4392d1419a3SJeeja KP 
4402d1419a3SJeeja KP 	return 0;
4412d1419a3SJeeja KP }
4422d1419a3SJeeja KP 
443f7590d4fSJeeja KP static void skl_tplg_update_module_params(struct snd_soc_dapm_widget *w,
444f7590d4fSJeeja KP 							struct skl_sst *ctx)
445f7590d4fSJeeja KP {
446f7590d4fSJeeja KP 	struct skl_module_cfg *m_cfg = w->priv;
447f7590d4fSJeeja KP 	struct skl_pipe_params *params = m_cfg->pipe->p_params;
448f7590d4fSJeeja KP 	int p_conn_type = m_cfg->pipe->conn_type;
449f7590d4fSJeeja KP 	bool is_fe;
450f7590d4fSJeeja KP 
451f7590d4fSJeeja KP 	if (!m_cfg->params_fixup)
452f7590d4fSJeeja KP 		return;
453f7590d4fSJeeja KP 
454f7590d4fSJeeja KP 	dev_dbg(ctx->dev, "Mconfig for widget=%s BEFORE updation\n",
455f7590d4fSJeeja KP 				w->name);
456f7590d4fSJeeja KP 
457f7590d4fSJeeja KP 	skl_dump_mconfig(ctx, m_cfg);
458f7590d4fSJeeja KP 
459f7590d4fSJeeja KP 	if (p_conn_type == SKL_PIPE_CONN_TYPE_FE)
460f7590d4fSJeeja KP 		is_fe = true;
461f7590d4fSJeeja KP 	else
462f7590d4fSJeeja KP 		is_fe = false;
463f7590d4fSJeeja KP 
464f7590d4fSJeeja KP 	skl_tplg_update_params_fixup(m_cfg, params, is_fe);
465f7590d4fSJeeja KP 	skl_tplg_update_buffer_size(ctx, m_cfg);
466f7590d4fSJeeja KP 
467f7590d4fSJeeja KP 	dev_dbg(ctx->dev, "Mconfig for widget=%s AFTER updation\n",
468f7590d4fSJeeja KP 				w->name);
469f7590d4fSJeeja KP 
470f7590d4fSJeeja KP 	skl_dump_mconfig(ctx, m_cfg);
471f7590d4fSJeeja KP }
472f7590d4fSJeeja KP 
473e4e2d2f4SJeeja KP /*
474abb74003SJeeja KP  * some modules can have multiple params set from user control and
475abb74003SJeeja KP  * need to be set after module is initialized. If set_param flag is
476abb74003SJeeja KP  * set module params will be done after module is initialised.
477abb74003SJeeja KP  */
478abb74003SJeeja KP static int skl_tplg_set_module_params(struct snd_soc_dapm_widget *w,
479abb74003SJeeja KP 						struct skl_sst *ctx)
480abb74003SJeeja KP {
481abb74003SJeeja KP 	int i, ret;
482abb74003SJeeja KP 	struct skl_module_cfg *mconfig = w->priv;
483abb74003SJeeja KP 	const struct snd_kcontrol_new *k;
484abb74003SJeeja KP 	struct soc_bytes_ext *sb;
485abb74003SJeeja KP 	struct skl_algo_data *bc;
486abb74003SJeeja KP 	struct skl_specific_cfg *sp_cfg;
487abb74003SJeeja KP 
488abb74003SJeeja KP 	if (mconfig->formats_config.caps_size > 0 &&
4894ced1827SJeeja KP 		mconfig->formats_config.set_params == SKL_PARAM_SET) {
490abb74003SJeeja KP 		sp_cfg = &mconfig->formats_config;
491abb74003SJeeja KP 		ret = skl_set_module_params(ctx, sp_cfg->caps,
492abb74003SJeeja KP 					sp_cfg->caps_size,
493abb74003SJeeja KP 					sp_cfg->param_id, mconfig);
494abb74003SJeeja KP 		if (ret < 0)
495abb74003SJeeja KP 			return ret;
496abb74003SJeeja KP 	}
497abb74003SJeeja KP 
498abb74003SJeeja KP 	for (i = 0; i < w->num_kcontrols; i++) {
499abb74003SJeeja KP 		k = &w->kcontrol_news[i];
500abb74003SJeeja KP 		if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
501abb74003SJeeja KP 			sb = (void *) k->private_value;
502abb74003SJeeja KP 			bc = (struct skl_algo_data *)sb->dobj.private;
503abb74003SJeeja KP 
5044ced1827SJeeja KP 			if (bc->set_params == SKL_PARAM_SET) {
505abb74003SJeeja KP 				ret = skl_set_module_params(ctx,
5060d682104SDharageswari R 						(u32 *)bc->params, bc->size,
507abb74003SJeeja KP 						bc->param_id, mconfig);
508abb74003SJeeja KP 				if (ret < 0)
509abb74003SJeeja KP 					return ret;
510abb74003SJeeja KP 			}
511abb74003SJeeja KP 		}
512abb74003SJeeja KP 	}
513abb74003SJeeja KP 
514abb74003SJeeja KP 	return 0;
515abb74003SJeeja KP }
516abb74003SJeeja KP 
517abb74003SJeeja KP /*
518abb74003SJeeja KP  * some module param can set from user control and this is required as
519abb74003SJeeja KP  * when module is initailzed. if module param is required in init it is
520abb74003SJeeja KP  * identifed by set_param flag. if set_param flag is not set, then this
521abb74003SJeeja KP  * parameter needs to set as part of module init.
522abb74003SJeeja KP  */
523abb74003SJeeja KP static int skl_tplg_set_module_init_data(struct snd_soc_dapm_widget *w)
524abb74003SJeeja KP {
525abb74003SJeeja KP 	const struct snd_kcontrol_new *k;
526abb74003SJeeja KP 	struct soc_bytes_ext *sb;
527abb74003SJeeja KP 	struct skl_algo_data *bc;
528abb74003SJeeja KP 	struct skl_module_cfg *mconfig = w->priv;
529abb74003SJeeja KP 	int i;
530abb74003SJeeja KP 
531abb74003SJeeja KP 	for (i = 0; i < w->num_kcontrols; i++) {
532abb74003SJeeja KP 		k = &w->kcontrol_news[i];
533abb74003SJeeja KP 		if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
534abb74003SJeeja KP 			sb = (struct soc_bytes_ext *)k->private_value;
535abb74003SJeeja KP 			bc = (struct skl_algo_data *)sb->dobj.private;
536abb74003SJeeja KP 
5374ced1827SJeeja KP 			if (bc->set_params != SKL_PARAM_INIT)
538abb74003SJeeja KP 				continue;
539abb74003SJeeja KP 
540d1a6fe41STakashi Sakamoto 			mconfig->formats_config.caps = (u32 *)bc->params;
5410d682104SDharageswari R 			mconfig->formats_config.caps_size = bc->size;
542abb74003SJeeja KP 
543abb74003SJeeja KP 			break;
544abb74003SJeeja KP 		}
545abb74003SJeeja KP 	}
546abb74003SJeeja KP 
547abb74003SJeeja KP 	return 0;
548abb74003SJeeja KP }
549abb74003SJeeja KP 
550bb704a73SJeeja KP static int skl_tplg_module_prepare(struct skl_sst *ctx, struct skl_pipe *pipe,
551bb704a73SJeeja KP 		struct snd_soc_dapm_widget *w, struct skl_module_cfg *mcfg)
552bb704a73SJeeja KP {
553bb704a73SJeeja KP 	switch (mcfg->dev_type) {
554bb704a73SJeeja KP 	case SKL_DEVICE_HDAHOST:
555bb704a73SJeeja KP 		return skl_pcm_host_dma_prepare(ctx->dev, pipe->p_params);
556bb704a73SJeeja KP 
557bb704a73SJeeja KP 	case SKL_DEVICE_HDALINK:
558bb704a73SJeeja KP 		return skl_pcm_link_dma_prepare(ctx->dev, pipe->p_params);
559bb704a73SJeeja KP 	}
560bb704a73SJeeja KP 
561bb704a73SJeeja KP 	return 0;
562bb704a73SJeeja KP }
563bb704a73SJeeja KP 
564abb74003SJeeja KP /*
565e4e2d2f4SJeeja KP  * Inside a pipe instance, we can have various modules. These modules need
566e4e2d2f4SJeeja KP  * to instantiated in DSP by invoking INIT_MODULE IPC, which is achieved by
567e4e2d2f4SJeeja KP  * skl_init_module() routine, so invoke that for all modules in a pipeline
568e4e2d2f4SJeeja KP  */
569e4e2d2f4SJeeja KP static int
570e4e2d2f4SJeeja KP skl_tplg_init_pipe_modules(struct skl *skl, struct skl_pipe *pipe)
571e4e2d2f4SJeeja KP {
572e4e2d2f4SJeeja KP 	struct skl_pipe_module *w_module;
573e4e2d2f4SJeeja KP 	struct snd_soc_dapm_widget *w;
574e4e2d2f4SJeeja KP 	struct skl_module_cfg *mconfig;
575e4e2d2f4SJeeja KP 	struct skl_sst *ctx = skl->skl_sst;
576f6fa56e2SRamesh Babu 	u8 cfg_idx;
577e4e2d2f4SJeeja KP 	int ret = 0;
578e4e2d2f4SJeeja KP 
579e4e2d2f4SJeeja KP 	list_for_each_entry(w_module, &pipe->w_list, node) {
580b26199eaSJeeja KP 		uuid_le *uuid_mod;
581e4e2d2f4SJeeja KP 		w = w_module->w;
582e4e2d2f4SJeeja KP 		mconfig = w->priv;
583e4e2d2f4SJeeja KP 
584b7c50555SVinod Koul 		/* check if module ids are populated */
585b7c50555SVinod Koul 		if (mconfig->id.module_id < 0) {
586b7c50555SVinod Koul 			dev_err(skl->skl_sst->dev,
587a657ae7eSVinod Koul 					"module %pUL id not populated\n",
588a657ae7eSVinod Koul 					(uuid_le *)mconfig->guid);
589a657ae7eSVinod Koul 			return -EIO;
590b7c50555SVinod Koul 		}
591b7c50555SVinod Koul 
592f6fa56e2SRamesh Babu 		cfg_idx = mconfig->pipe->cur_config_idx;
593f6fa56e2SRamesh Babu 		mconfig->fmt_idx = mconfig->mod_cfg[cfg_idx].fmt_idx;
594f6fa56e2SRamesh Babu 		mconfig->res_idx = mconfig->mod_cfg[cfg_idx].res_idx;
595f6fa56e2SRamesh Babu 
596e4e2d2f4SJeeja KP 		/* check resource available */
5979ba8ffefSDharageswari.R 		if (!skl_is_pipe_mcps_avail(skl, mconfig))
598e4e2d2f4SJeeja KP 			return -ENOMEM;
599e4e2d2f4SJeeja KP 
600f6fa56e2SRamesh Babu 		if (mconfig->module->loadable && ctx->dsp->fw_ops.load_mod) {
6016c5768b3SDharageswari R 			ret = ctx->dsp->fw_ops.load_mod(ctx->dsp,
6026c5768b3SDharageswari R 				mconfig->id.module_id, mconfig->guid);
6036c5768b3SDharageswari R 			if (ret < 0)
6046c5768b3SDharageswari R 				return ret;
605d643678bSJeeja KP 
606d643678bSJeeja KP 			mconfig->m_state = SKL_MODULE_LOADED;
6076c5768b3SDharageswari R 		}
6086c5768b3SDharageswari R 
609bb704a73SJeeja KP 		/* prepare the DMA if the module is gateway cpr */
610bb704a73SJeeja KP 		ret = skl_tplg_module_prepare(ctx, pipe, w, mconfig);
611bb704a73SJeeja KP 		if (ret < 0)
612bb704a73SJeeja KP 			return ret;
613bb704a73SJeeja KP 
6142d1419a3SJeeja KP 		/* update blob if blob is null for be with default value */
6152d1419a3SJeeja KP 		skl_tplg_update_be_blob(w, ctx);
6162d1419a3SJeeja KP 
617f7590d4fSJeeja KP 		/*
618f7590d4fSJeeja KP 		 * apply fix/conversion to module params based on
619f7590d4fSJeeja KP 		 * FE/BE params
620f7590d4fSJeeja KP 		 */
621f7590d4fSJeeja KP 		skl_tplg_update_module_params(w, ctx);
622b26199eaSJeeja KP 		uuid_mod = (uuid_le *)mconfig->guid;
623b26199eaSJeeja KP 		mconfig->id.pvt_id = skl_get_pvt_id(ctx, uuid_mod,
624b26199eaSJeeja KP 						mconfig->id.instance_id);
625ef2a352cSDharageswari R 		if (mconfig->id.pvt_id < 0)
626ef2a352cSDharageswari R 			return ret;
627abb74003SJeeja KP 		skl_tplg_set_module_init_data(w);
6284147a6e5SPardha Saradhi K 
6294147a6e5SPardha Saradhi K 		ret = skl_dsp_get_core(ctx->dsp, mconfig->core_id);
6304147a6e5SPardha Saradhi K 		if (ret < 0) {
6314147a6e5SPardha Saradhi K 			dev_err(ctx->dev, "Failed to wake up core %d ret=%d\n",
6324147a6e5SPardha Saradhi K 						mconfig->core_id, ret);
6334147a6e5SPardha Saradhi K 			return ret;
6344147a6e5SPardha Saradhi K 		}
6354147a6e5SPardha Saradhi K 
6369939a9c3SJeeja KP 		ret = skl_init_module(ctx, mconfig);
637ef2a352cSDharageswari R 		if (ret < 0) {
638b26199eaSJeeja KP 			skl_put_pvt_id(ctx, uuid_mod, &mconfig->id.pvt_id);
6394147a6e5SPardha Saradhi K 			goto err;
640ef2a352cSDharageswari R 		}
641260eb73aSDharageswari R 		skl_tplg_alloc_pipe_mcps(skl, mconfig);
642abb74003SJeeja KP 		ret = skl_tplg_set_module_params(w, ctx);
643e4e2d2f4SJeeja KP 		if (ret < 0)
6444147a6e5SPardha Saradhi K 			goto err;
645e4e2d2f4SJeeja KP 	}
646e4e2d2f4SJeeja KP 
647e4e2d2f4SJeeja KP 	return 0;
6484147a6e5SPardha Saradhi K err:
6494147a6e5SPardha Saradhi K 	skl_dsp_put_core(ctx->dsp, mconfig->core_id);
6504147a6e5SPardha Saradhi K 	return ret;
651e4e2d2f4SJeeja KP }
652d93f8e55SVinod Koul 
6536c5768b3SDharageswari R static int skl_tplg_unload_pipe_modules(struct skl_sst *ctx,
6546c5768b3SDharageswari R 	 struct skl_pipe *pipe)
6556c5768b3SDharageswari R {
6564147a6e5SPardha Saradhi K 	int ret = 0;
6576c5768b3SDharageswari R 	struct skl_pipe_module *w_module = NULL;
6586c5768b3SDharageswari R 	struct skl_module_cfg *mconfig = NULL;
6596c5768b3SDharageswari R 
6606c5768b3SDharageswari R 	list_for_each_entry(w_module, &pipe->w_list, node) {
661b26199eaSJeeja KP 		uuid_le *uuid_mod;
6626c5768b3SDharageswari R 		mconfig  = w_module->w->priv;
663b26199eaSJeeja KP 		uuid_mod = (uuid_le *)mconfig->guid;
6646c5768b3SDharageswari R 
665f6fa56e2SRamesh Babu 		if (mconfig->module->loadable && ctx->dsp->fw_ops.unload_mod &&
666b0fab9c6SDharageswari R 			mconfig->m_state > SKL_MODULE_UNINIT) {
667b0fab9c6SDharageswari R 			ret = ctx->dsp->fw_ops.unload_mod(ctx->dsp,
6686c5768b3SDharageswari R 						mconfig->id.module_id);
669b0fab9c6SDharageswari R 			if (ret < 0)
670b0fab9c6SDharageswari R 				return -EIO;
671b0fab9c6SDharageswari R 		}
672b26199eaSJeeja KP 		skl_put_pvt_id(ctx, uuid_mod, &mconfig->id.pvt_id);
6734147a6e5SPardha Saradhi K 
6744147a6e5SPardha Saradhi K 		ret = skl_dsp_put_core(ctx->dsp, mconfig->core_id);
6754147a6e5SPardha Saradhi K 		if (ret < 0) {
6764147a6e5SPardha Saradhi K 			/* don't return; continue with other modules */
6774147a6e5SPardha Saradhi K 			dev_err(ctx->dev, "Failed to sleep core %d ret=%d\n",
6784147a6e5SPardha Saradhi K 				mconfig->core_id, ret);
6794147a6e5SPardha Saradhi K 		}
6806c5768b3SDharageswari R 	}
6816c5768b3SDharageswari R 
6826c5768b3SDharageswari R 	/* no modules to unload in this path, so return */
6834147a6e5SPardha Saradhi K 	return ret;
6846c5768b3SDharageswari R }
6856c5768b3SDharageswari R 
686d93f8e55SVinod Koul /*
687f6fa56e2SRamesh Babu  * Here, we select pipe format based on the pipe type and pipe
688f6fa56e2SRamesh Babu  * direction to determine the current config index for the pipeline.
689f6fa56e2SRamesh Babu  * The config index is then used to select proper module resources.
690f6fa56e2SRamesh Babu  * Intermediate pipes currently have a fixed format hence we select the
691f6fa56e2SRamesh Babu  * 0th configuratation by default for such pipes.
692f6fa56e2SRamesh Babu  */
693f6fa56e2SRamesh Babu static int
694f6fa56e2SRamesh Babu skl_tplg_get_pipe_config(struct skl *skl, struct skl_module_cfg *mconfig)
695f6fa56e2SRamesh Babu {
696f6fa56e2SRamesh Babu 	struct skl_sst *ctx = skl->skl_sst;
697f6fa56e2SRamesh Babu 	struct skl_pipe *pipe = mconfig->pipe;
698f6fa56e2SRamesh Babu 	struct skl_pipe_params *params = pipe->p_params;
699f6fa56e2SRamesh Babu 	struct skl_path_config *pconfig = &pipe->configs[0];
700f6fa56e2SRamesh Babu 	struct skl_pipe_fmt *fmt = NULL;
701f6fa56e2SRamesh Babu 	bool in_fmt = false;
702f6fa56e2SRamesh Babu 	int i;
703f6fa56e2SRamesh Babu 
704f6fa56e2SRamesh Babu 	if (pipe->nr_cfgs == 0) {
705f6fa56e2SRamesh Babu 		pipe->cur_config_idx = 0;
706f6fa56e2SRamesh Babu 		return 0;
707f6fa56e2SRamesh Babu 	}
708f6fa56e2SRamesh Babu 
709f6fa56e2SRamesh Babu 	if (pipe->conn_type == SKL_PIPE_CONN_TYPE_NONE) {
710f6fa56e2SRamesh Babu 		dev_dbg(ctx->dev, "No conn_type detected, take 0th config\n");
711f6fa56e2SRamesh Babu 		pipe->cur_config_idx = 0;
712f6fa56e2SRamesh Babu 		pipe->memory_pages = pconfig->mem_pages;
713f6fa56e2SRamesh Babu 
714f6fa56e2SRamesh Babu 		return 0;
715f6fa56e2SRamesh Babu 	}
716f6fa56e2SRamesh Babu 
717f6fa56e2SRamesh Babu 	if ((pipe->conn_type == SKL_PIPE_CONN_TYPE_FE &&
718f6fa56e2SRamesh Babu 	     pipe->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
719f6fa56e2SRamesh Babu 	     (pipe->conn_type == SKL_PIPE_CONN_TYPE_BE &&
720f6fa56e2SRamesh Babu 	     pipe->direction == SNDRV_PCM_STREAM_CAPTURE))
721f6fa56e2SRamesh Babu 		in_fmt = true;
722f6fa56e2SRamesh Babu 
723f6fa56e2SRamesh Babu 	for (i = 0; i < pipe->nr_cfgs; i++) {
724f6fa56e2SRamesh Babu 		pconfig = &pipe->configs[i];
725f6fa56e2SRamesh Babu 		if (in_fmt)
726f6fa56e2SRamesh Babu 			fmt = &pconfig->in_fmt;
727f6fa56e2SRamesh Babu 		else
728f6fa56e2SRamesh Babu 			fmt = &pconfig->out_fmt;
729f6fa56e2SRamesh Babu 
730f6fa56e2SRamesh Babu 		if (CHECK_HW_PARAMS(params->ch, params->s_freq, params->s_fmt,
731f6fa56e2SRamesh Babu 				    fmt->channels, fmt->freq, fmt->bps)) {
732f6fa56e2SRamesh Babu 			pipe->cur_config_idx = i;
733f6fa56e2SRamesh Babu 			pipe->memory_pages = pconfig->mem_pages;
734f6fa56e2SRamesh Babu 			dev_dbg(ctx->dev, "Using pipe config: %d\n", i);
735f6fa56e2SRamesh Babu 
736f6fa56e2SRamesh Babu 			return 0;
737f6fa56e2SRamesh Babu 		}
738f6fa56e2SRamesh Babu 	}
739f6fa56e2SRamesh Babu 
740f6fa56e2SRamesh Babu 	dev_err(ctx->dev, "Invalid pipe config: %d %d %d for pipe: %d\n",
741f6fa56e2SRamesh Babu 		params->ch, params->s_freq, params->s_fmt, pipe->ppl_id);
742f6fa56e2SRamesh Babu 	return -EINVAL;
743f6fa56e2SRamesh Babu }
744f6fa56e2SRamesh Babu 
745f6fa56e2SRamesh Babu /*
746d93f8e55SVinod Koul  * Mixer module represents a pipeline. So in the Pre-PMU event of mixer we
747d93f8e55SVinod Koul  * need create the pipeline. So we do following:
748d93f8e55SVinod Koul  *   - check the resources
749d93f8e55SVinod Koul  *   - Create the pipeline
750d93f8e55SVinod Koul  *   - Initialize the modules in pipeline
751d93f8e55SVinod Koul  *   - finally bind all modules together
752d93f8e55SVinod Koul  */
753d93f8e55SVinod Koul static int skl_tplg_mixer_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
754d93f8e55SVinod Koul 							struct skl *skl)
755d93f8e55SVinod Koul {
756d93f8e55SVinod Koul 	int ret;
757d93f8e55SVinod Koul 	struct skl_module_cfg *mconfig = w->priv;
758d93f8e55SVinod Koul 	struct skl_pipe_module *w_module;
759d93f8e55SVinod Koul 	struct skl_pipe *s_pipe = mconfig->pipe;
760b8c722ddSJeeja KP 	struct skl_module_cfg *src_module = NULL, *dst_module, *module;
761d93f8e55SVinod Koul 	struct skl_sst *ctx = skl->skl_sst;
762b8c722ddSJeeja KP 	struct skl_module_deferred_bind *modules;
763d93f8e55SVinod Koul 
764f6fa56e2SRamesh Babu 	ret = skl_tplg_get_pipe_config(skl, mconfig);
765f6fa56e2SRamesh Babu 	if (ret < 0)
766f6fa56e2SRamesh Babu 		return ret;
767f6fa56e2SRamesh Babu 
768d93f8e55SVinod Koul 	/* check resource available */
7699ba8ffefSDharageswari.R 	if (!skl_is_pipe_mcps_avail(skl, mconfig))
770d93f8e55SVinod Koul 		return -EBUSY;
771d93f8e55SVinod Koul 
7729ba8ffefSDharageswari.R 	if (!skl_is_pipe_mem_avail(skl, mconfig))
773d93f8e55SVinod Koul 		return -ENOMEM;
774d93f8e55SVinod Koul 
775d93f8e55SVinod Koul 	/*
776d93f8e55SVinod Koul 	 * Create a list of modules for pipe.
777d93f8e55SVinod Koul 	 * This list contains modules from source to sink
778d93f8e55SVinod Koul 	 */
779d93f8e55SVinod Koul 	ret = skl_create_pipeline(ctx, mconfig->pipe);
780d93f8e55SVinod Koul 	if (ret < 0)
781d93f8e55SVinod Koul 		return ret;
782d93f8e55SVinod Koul 
783260eb73aSDharageswari R 	skl_tplg_alloc_pipe_mem(skl, mconfig);
784260eb73aSDharageswari R 	skl_tplg_alloc_pipe_mcps(skl, mconfig);
785d93f8e55SVinod Koul 
786d93f8e55SVinod Koul 	/* Init all pipe modules from source to sink */
787d93f8e55SVinod Koul 	ret = skl_tplg_init_pipe_modules(skl, s_pipe);
788d93f8e55SVinod Koul 	if (ret < 0)
789d93f8e55SVinod Koul 		return ret;
790d93f8e55SVinod Koul 
791d93f8e55SVinod Koul 	/* Bind modules from source to sink */
792d93f8e55SVinod Koul 	list_for_each_entry(w_module, &s_pipe->w_list, node) {
793d93f8e55SVinod Koul 		dst_module = w_module->w->priv;
794d93f8e55SVinod Koul 
795d93f8e55SVinod Koul 		if (src_module == NULL) {
796d93f8e55SVinod Koul 			src_module = dst_module;
797d93f8e55SVinod Koul 			continue;
798d93f8e55SVinod Koul 		}
799d93f8e55SVinod Koul 
800d93f8e55SVinod Koul 		ret = skl_bind_modules(ctx, src_module, dst_module);
801d93f8e55SVinod Koul 		if (ret < 0)
802d93f8e55SVinod Koul 			return ret;
803d93f8e55SVinod Koul 
804d93f8e55SVinod Koul 		src_module = dst_module;
805d93f8e55SVinod Koul 	}
806d93f8e55SVinod Koul 
807b8c722ddSJeeja KP 	/*
808b8c722ddSJeeja KP 	 * When the destination module is initialized, check for these modules
809b8c722ddSJeeja KP 	 * in deferred bind list. If found, bind them.
810b8c722ddSJeeja KP 	 */
811b8c722ddSJeeja KP 	list_for_each_entry(w_module, &s_pipe->w_list, node) {
812b8c722ddSJeeja KP 		if (list_empty(&skl->bind_list))
813b8c722ddSJeeja KP 			break;
814b8c722ddSJeeja KP 
815b8c722ddSJeeja KP 		list_for_each_entry(modules, &skl->bind_list, node) {
816b8c722ddSJeeja KP 			module = w_module->w->priv;
817b8c722ddSJeeja KP 			if (modules->dst == module)
818b8c722ddSJeeja KP 				skl_bind_modules(ctx, modules->src,
819b8c722ddSJeeja KP 							modules->dst);
820b8c722ddSJeeja KP 		}
821b8c722ddSJeeja KP 	}
822b8c722ddSJeeja KP 
823d93f8e55SVinod Koul 	return 0;
824d93f8e55SVinod Koul }
825d93f8e55SVinod Koul 
826bf3e5ef5SDharageswari R static int skl_fill_sink_instance_id(struct skl_sst *ctx, u32 *params,
827bf3e5ef5SDharageswari R 				int size, struct skl_module_cfg *mcfg)
8285e8f0ee4SDharageswari R {
8295e8f0ee4SDharageswari R 	int i, pvt_id;
8305e8f0ee4SDharageswari R 
831bf3e5ef5SDharageswari R 	if (mcfg->m_type == SKL_MODULE_TYPE_KPB) {
832bf3e5ef5SDharageswari R 		struct skl_kpb_params *kpb_params =
833bf3e5ef5SDharageswari R 				(struct skl_kpb_params *)params;
834f7a9f772SSriram Periyasamy 		struct skl_mod_inst_map *inst = kpb_params->u.map;
8355e8f0ee4SDharageswari R 
836bf3e5ef5SDharageswari R 		for (i = 0; i < kpb_params->num_modules; i++) {
837bf3e5ef5SDharageswari R 			pvt_id = skl_get_pvt_instance_id_map(ctx, inst->mod_id,
838bf3e5ef5SDharageswari R 								inst->inst_id);
8395e8f0ee4SDharageswari R 			if (pvt_id < 0)
8405e8f0ee4SDharageswari R 				return -EINVAL;
841bf3e5ef5SDharageswari R 
8425e8f0ee4SDharageswari R 			inst->inst_id = pvt_id;
8435e8f0ee4SDharageswari R 			inst++;
8445e8f0ee4SDharageswari R 		}
8455e8f0ee4SDharageswari R 	}
8465e8f0ee4SDharageswari R 
847bf3e5ef5SDharageswari R 	return 0;
848bf3e5ef5SDharageswari R }
849cc6a4044SJeeja KP /*
850cc6a4044SJeeja KP  * Some modules require params to be set after the module is bound to
851cc6a4044SJeeja KP  * all pins connected.
852cc6a4044SJeeja KP  *
853cc6a4044SJeeja KP  * The module provider initializes set_param flag for such modules and we
854cc6a4044SJeeja KP  * send params after binding
855cc6a4044SJeeja KP  */
856cc6a4044SJeeja KP static int skl_tplg_set_module_bind_params(struct snd_soc_dapm_widget *w,
857cc6a4044SJeeja KP 			struct skl_module_cfg *mcfg, struct skl_sst *ctx)
858cc6a4044SJeeja KP {
859cc6a4044SJeeja KP 	int i, ret;
860cc6a4044SJeeja KP 	struct skl_module_cfg *mconfig = w->priv;
861cc6a4044SJeeja KP 	const struct snd_kcontrol_new *k;
862cc6a4044SJeeja KP 	struct soc_bytes_ext *sb;
863cc6a4044SJeeja KP 	struct skl_algo_data *bc;
864cc6a4044SJeeja KP 	struct skl_specific_cfg *sp_cfg;
865bf3e5ef5SDharageswari R 	u32 *params;
866cc6a4044SJeeja KP 
867cc6a4044SJeeja KP 	/*
868cc6a4044SJeeja KP 	 * check all out/in pins are in bind state.
869cc6a4044SJeeja KP 	 * if so set the module param
870cc6a4044SJeeja KP 	 */
871f6fa56e2SRamesh Babu 	for (i = 0; i < mcfg->module->max_output_pins; i++) {
872cc6a4044SJeeja KP 		if (mcfg->m_out_pin[i].pin_state != SKL_PIN_BIND_DONE)
873cc6a4044SJeeja KP 			return 0;
874cc6a4044SJeeja KP 	}
875cc6a4044SJeeja KP 
876f6fa56e2SRamesh Babu 	for (i = 0; i < mcfg->module->max_input_pins; i++) {
877cc6a4044SJeeja KP 		if (mcfg->m_in_pin[i].pin_state != SKL_PIN_BIND_DONE)
878cc6a4044SJeeja KP 			return 0;
879cc6a4044SJeeja KP 	}
880cc6a4044SJeeja KP 
881cc6a4044SJeeja KP 	if (mconfig->formats_config.caps_size > 0 &&
882cc6a4044SJeeja KP 		mconfig->formats_config.set_params == SKL_PARAM_BIND) {
883cc6a4044SJeeja KP 		sp_cfg = &mconfig->formats_config;
884cc6a4044SJeeja KP 		ret = skl_set_module_params(ctx, sp_cfg->caps,
885cc6a4044SJeeja KP 					sp_cfg->caps_size,
886cc6a4044SJeeja KP 					sp_cfg->param_id, mconfig);
887cc6a4044SJeeja KP 		if (ret < 0)
888cc6a4044SJeeja KP 			return ret;
889cc6a4044SJeeja KP 	}
890cc6a4044SJeeja KP 
891cc6a4044SJeeja KP 	for (i = 0; i < w->num_kcontrols; i++) {
892cc6a4044SJeeja KP 		k = &w->kcontrol_news[i];
893cc6a4044SJeeja KP 		if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
894cc6a4044SJeeja KP 			sb = (void *) k->private_value;
895cc6a4044SJeeja KP 			bc = (struct skl_algo_data *)sb->dobj.private;
896cc6a4044SJeeja KP 
897cc6a4044SJeeja KP 			if (bc->set_params == SKL_PARAM_BIND) {
898bf3e5ef5SDharageswari R 				params = kzalloc(bc->max, GFP_KERNEL);
899bf3e5ef5SDharageswari R 				if (!params)
900bf3e5ef5SDharageswari R 					return -ENOMEM;
901bf3e5ef5SDharageswari R 
902bf3e5ef5SDharageswari R 				memcpy(params, bc->params, bc->max);
903bf3e5ef5SDharageswari R 				skl_fill_sink_instance_id(ctx, params, bc->max,
904bf3e5ef5SDharageswari R 								mconfig);
905bf3e5ef5SDharageswari R 
906bf3e5ef5SDharageswari R 				ret = skl_set_module_params(ctx, params,
907bf3e5ef5SDharageswari R 						bc->max, bc->param_id, mconfig);
908bf3e5ef5SDharageswari R 				kfree(params);
909bf3e5ef5SDharageswari R 
910cc6a4044SJeeja KP 				if (ret < 0)
911cc6a4044SJeeja KP 					return ret;
912cc6a4044SJeeja KP 			}
913cc6a4044SJeeja KP 		}
914cc6a4044SJeeja KP 	}
915cc6a4044SJeeja KP 
916cc6a4044SJeeja KP 	return 0;
917cc6a4044SJeeja KP }
918cc6a4044SJeeja KP 
919f7a9f772SSriram Periyasamy static int skl_get_module_id(struct skl_sst *ctx, uuid_le *uuid)
920f7a9f772SSriram Periyasamy {
921f7a9f772SSriram Periyasamy 	struct uuid_module *module;
922f7a9f772SSriram Periyasamy 
923f7a9f772SSriram Periyasamy 	list_for_each_entry(module, &ctx->uuid_list, list) {
924f7a9f772SSriram Periyasamy 		if (uuid_le_cmp(*uuid, module->uuid) == 0)
925f7a9f772SSriram Periyasamy 			return module->id;
926f7a9f772SSriram Periyasamy 	}
927f7a9f772SSriram Periyasamy 
928f7a9f772SSriram Periyasamy 	return -EINVAL;
929f7a9f772SSriram Periyasamy }
930f7a9f772SSriram Periyasamy 
931f7a9f772SSriram Periyasamy static int skl_tplg_find_moduleid_from_uuid(struct skl *skl,
932f7a9f772SSriram Periyasamy 					const struct snd_kcontrol_new *k)
933f7a9f772SSriram Periyasamy {
934f7a9f772SSriram Periyasamy 	struct soc_bytes_ext *sb = (void *) k->private_value;
935f7a9f772SSriram Periyasamy 	struct skl_algo_data *bc = (struct skl_algo_data *)sb->dobj.private;
936f7a9f772SSriram Periyasamy 	struct skl_kpb_params *uuid_params, *params;
937*76f56faeSRakesh Ughreja 	struct hdac_bus *bus = skl_to_bus(skl);
938f7a9f772SSriram Periyasamy 	int i, size, module_id;
939f7a9f772SSriram Periyasamy 
940f7a9f772SSriram Periyasamy 	if (bc->set_params == SKL_PARAM_BIND && bc->max) {
941f7a9f772SSriram Periyasamy 		uuid_params = (struct skl_kpb_params *)bc->params;
942f7a9f772SSriram Periyasamy 		size = uuid_params->num_modules *
943f7a9f772SSriram Periyasamy 			sizeof(struct skl_mod_inst_map) +
944f7a9f772SSriram Periyasamy 			sizeof(uuid_params->num_modules);
945f7a9f772SSriram Periyasamy 
946f7a9f772SSriram Periyasamy 		params = devm_kzalloc(bus->dev, size, GFP_KERNEL);
947f7a9f772SSriram Periyasamy 		if (!params)
948f7a9f772SSriram Periyasamy 			return -ENOMEM;
949f7a9f772SSriram Periyasamy 
950f7a9f772SSriram Periyasamy 		params->num_modules = uuid_params->num_modules;
951f7a9f772SSriram Periyasamy 
952f7a9f772SSriram Periyasamy 		for (i = 0; i < uuid_params->num_modules; i++) {
953f7a9f772SSriram Periyasamy 			module_id = skl_get_module_id(skl->skl_sst,
954f7a9f772SSriram Periyasamy 				&uuid_params->u.map_uuid[i].mod_uuid);
955f7a9f772SSriram Periyasamy 			if (module_id < 0) {
956f7a9f772SSriram Periyasamy 				devm_kfree(bus->dev, params);
957f7a9f772SSriram Periyasamy 				return -EINVAL;
958f7a9f772SSriram Periyasamy 			}
959f7a9f772SSriram Periyasamy 
960f7a9f772SSriram Periyasamy 			params->u.map[i].mod_id = module_id;
961f7a9f772SSriram Periyasamy 			params->u.map[i].inst_id =
962f7a9f772SSriram Periyasamy 				uuid_params->u.map_uuid[i].inst_id;
963f7a9f772SSriram Periyasamy 		}
964f7a9f772SSriram Periyasamy 
965f7a9f772SSriram Periyasamy 		devm_kfree(bus->dev, bc->params);
966f7a9f772SSriram Periyasamy 		bc->params = (char *)params;
967f7a9f772SSriram Periyasamy 		bc->max = size;
968f7a9f772SSriram Periyasamy 	}
969f7a9f772SSriram Periyasamy 
970f7a9f772SSriram Periyasamy 	return 0;
971f7a9f772SSriram Periyasamy }
972f7a9f772SSriram Periyasamy 
973f7a9f772SSriram Periyasamy /*
974f7a9f772SSriram Periyasamy  * Retrieve the module id from UUID mentioned in the
975f7a9f772SSriram Periyasamy  * post bind params
976f7a9f772SSriram Periyasamy  */
977f7a9f772SSriram Periyasamy void skl_tplg_add_moduleid_in_bind_params(struct skl *skl,
978f7a9f772SSriram Periyasamy 				struct snd_soc_dapm_widget *w)
979f7a9f772SSriram Periyasamy {
980f7a9f772SSriram Periyasamy 	struct skl_module_cfg *mconfig = w->priv;
981f7a9f772SSriram Periyasamy 	int i;
982f7a9f772SSriram Periyasamy 
983f7a9f772SSriram Periyasamy 	/*
984f7a9f772SSriram Periyasamy 	 * Post bind params are used for only for KPB
985f7a9f772SSriram Periyasamy 	 * to set copier instances to drain the data
986f7a9f772SSriram Periyasamy 	 * in fast mode
987f7a9f772SSriram Periyasamy 	 */
988f7a9f772SSriram Periyasamy 	if (mconfig->m_type != SKL_MODULE_TYPE_KPB)
989f7a9f772SSriram Periyasamy 		return;
990f7a9f772SSriram Periyasamy 
991f7a9f772SSriram Periyasamy 	for (i = 0; i < w->num_kcontrols; i++)
992f7a9f772SSriram Periyasamy 		if ((w->kcontrol_news[i].access &
993f7a9f772SSriram Periyasamy 			SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) &&
994f7a9f772SSriram Periyasamy 			(skl_tplg_find_moduleid_from_uuid(skl,
995f7a9f772SSriram Periyasamy 			&w->kcontrol_news[i]) < 0))
996f7a9f772SSriram Periyasamy 			dev_err(skl->skl_sst->dev,
997f7a9f772SSriram Periyasamy 				"%s: invalid kpb post bind params\n",
998f7a9f772SSriram Periyasamy 				__func__);
999f7a9f772SSriram Periyasamy }
1000b8c722ddSJeeja KP 
1001b8c722ddSJeeja KP static int skl_tplg_module_add_deferred_bind(struct skl *skl,
1002b8c722ddSJeeja KP 	struct skl_module_cfg *src, struct skl_module_cfg *dst)
1003b8c722ddSJeeja KP {
1004b8c722ddSJeeja KP 	struct skl_module_deferred_bind *m_list, *modules;
1005b8c722ddSJeeja KP 	int i;
1006b8c722ddSJeeja KP 
1007b8c722ddSJeeja KP 	/* only supported for module with static pin connection */
1008f6fa56e2SRamesh Babu 	for (i = 0; i < dst->module->max_input_pins; i++) {
1009b8c722ddSJeeja KP 		struct skl_module_pin *pin = &dst->m_in_pin[i];
1010b8c722ddSJeeja KP 
1011b8c722ddSJeeja KP 		if (pin->is_dynamic)
1012b8c722ddSJeeja KP 			continue;
1013b8c722ddSJeeja KP 
1014b8c722ddSJeeja KP 		if ((pin->id.module_id  == src->id.module_id) &&
1015b8c722ddSJeeja KP 			(pin->id.instance_id  == src->id.instance_id)) {
1016b8c722ddSJeeja KP 
1017b8c722ddSJeeja KP 			if (!list_empty(&skl->bind_list)) {
1018b8c722ddSJeeja KP 				list_for_each_entry(modules, &skl->bind_list, node) {
1019b8c722ddSJeeja KP 					if (modules->src == src && modules->dst == dst)
1020b8c722ddSJeeja KP 						return 0;
1021b8c722ddSJeeja KP 				}
1022b8c722ddSJeeja KP 			}
1023b8c722ddSJeeja KP 
1024b8c722ddSJeeja KP 			m_list = kzalloc(sizeof(*m_list), GFP_KERNEL);
1025b8c722ddSJeeja KP 			if (!m_list)
1026b8c722ddSJeeja KP 				return -ENOMEM;
1027b8c722ddSJeeja KP 
1028b8c722ddSJeeja KP 			m_list->src = src;
1029b8c722ddSJeeja KP 			m_list->dst = dst;
1030b8c722ddSJeeja KP 
1031b8c722ddSJeeja KP 			list_add(&m_list->node, &skl->bind_list);
1032b8c722ddSJeeja KP 		}
1033b8c722ddSJeeja KP 	}
1034b8c722ddSJeeja KP 
1035b8c722ddSJeeja KP 	return 0;
1036b8c722ddSJeeja KP }
1037b8c722ddSJeeja KP 
10388724ff17SJeeja KP static int skl_tplg_bind_sinks(struct snd_soc_dapm_widget *w,
10398724ff17SJeeja KP 				struct skl *skl,
10406bd4cf85SJeeja KP 				struct snd_soc_dapm_widget *src_w,
10418724ff17SJeeja KP 				struct skl_module_cfg *src_mconfig)
1042d93f8e55SVinod Koul {
1043d93f8e55SVinod Koul 	struct snd_soc_dapm_path *p;
10440ed95d76SJeeja KP 	struct snd_soc_dapm_widget *sink = NULL, *next_sink = NULL;
10458724ff17SJeeja KP 	struct skl_module_cfg *sink_mconfig;
1046d93f8e55SVinod Koul 	struct skl_sst *ctx = skl->skl_sst;
10478724ff17SJeeja KP 	int ret;
1048d93f8e55SVinod Koul 
10498724ff17SJeeja KP 	snd_soc_dapm_widget_for_each_sink_path(w, p) {
1050d93f8e55SVinod Koul 		if (!p->connect)
1051d93f8e55SVinod Koul 			continue;
1052d93f8e55SVinod Koul 
1053d93f8e55SVinod Koul 		dev_dbg(ctx->dev, "%s: src widget=%s\n", __func__, w->name);
1054d93f8e55SVinod Koul 		dev_dbg(ctx->dev, "%s: sink widget=%s\n", __func__, p->sink->name);
1055d93f8e55SVinod Koul 
10560ed95d76SJeeja KP 		next_sink = p->sink;
10576bd4cf85SJeeja KP 
1058cb1f904dSGuneshwor Singh 		if (!is_skl_dsp_widget_type(p->sink, ctx->dev))
10596bd4cf85SJeeja KP 			return skl_tplg_bind_sinks(p->sink, skl, src_w, src_mconfig);
10606bd4cf85SJeeja KP 
1061d93f8e55SVinod Koul 		/*
1062d93f8e55SVinod Koul 		 * here we will check widgets in sink pipelines, so that
1063d93f8e55SVinod Koul 		 * can be any widgets type and we are only interested if
1064d93f8e55SVinod Koul 		 * they are ones used for SKL so check that first
1065d93f8e55SVinod Koul 		 */
1066d93f8e55SVinod Koul 		if ((p->sink->priv != NULL) &&
1067cb1f904dSGuneshwor Singh 				is_skl_dsp_widget_type(p->sink, ctx->dev)) {
1068d93f8e55SVinod Koul 
1069d93f8e55SVinod Koul 			sink = p->sink;
1070d93f8e55SVinod Koul 			sink_mconfig = sink->priv;
1071d93f8e55SVinod Koul 
1072b8c722ddSJeeja KP 			/*
1073b8c722ddSJeeja KP 			 * Modules other than PGA leaf can be connected
1074b8c722ddSJeeja KP 			 * directly or via switch to a module in another
1075b8c722ddSJeeja KP 			 * pipeline. EX: reference path
1076b8c722ddSJeeja KP 			 * when the path is enabled, the dst module that needs
1077b8c722ddSJeeja KP 			 * to be bound may not be initialized. if the module is
1078b8c722ddSJeeja KP 			 * not initialized, add these modules in the deferred
1079b8c722ddSJeeja KP 			 * bind list and when the dst module is initialised,
1080b8c722ddSJeeja KP 			 * bind this module to the dst_module in deferred list.
1081b8c722ddSJeeja KP 			 */
1082b8c722ddSJeeja KP 			if (((src_mconfig->m_state == SKL_MODULE_INIT_DONE)
1083b8c722ddSJeeja KP 				&& (sink_mconfig->m_state == SKL_MODULE_UNINIT))) {
1084b8c722ddSJeeja KP 
1085b8c722ddSJeeja KP 				ret = skl_tplg_module_add_deferred_bind(skl,
1086b8c722ddSJeeja KP 						src_mconfig, sink_mconfig);
1087b8c722ddSJeeja KP 
1088b8c722ddSJeeja KP 				if (ret < 0)
1089b8c722ddSJeeja KP 					return ret;
1090b8c722ddSJeeja KP 
1091b8c722ddSJeeja KP 			}
1092b8c722ddSJeeja KP 
1093b8c722ddSJeeja KP 
1094cc6a4044SJeeja KP 			if (src_mconfig->m_state == SKL_MODULE_UNINIT ||
1095cc6a4044SJeeja KP 				sink_mconfig->m_state == SKL_MODULE_UNINIT)
1096cc6a4044SJeeja KP 				continue;
1097cc6a4044SJeeja KP 
1098d93f8e55SVinod Koul 			/* Bind source to sink, mixin is always source */
1099d93f8e55SVinod Koul 			ret = skl_bind_modules(ctx, src_mconfig, sink_mconfig);
1100d93f8e55SVinod Koul 			if (ret)
1101d93f8e55SVinod Koul 				return ret;
1102d93f8e55SVinod Koul 
1103cc6a4044SJeeja KP 			/* set module params after bind */
1104cc6a4044SJeeja KP 			skl_tplg_set_module_bind_params(src_w, src_mconfig, ctx);
1105cc6a4044SJeeja KP 			skl_tplg_set_module_bind_params(sink, sink_mconfig, ctx);
1106cc6a4044SJeeja KP 
1107d93f8e55SVinod Koul 			/* Start sinks pipe first */
1108d93f8e55SVinod Koul 			if (sink_mconfig->pipe->state != SKL_PIPE_STARTED) {
1109d1730c3dSJeeja KP 				if (sink_mconfig->pipe->conn_type !=
1110d1730c3dSJeeja KP 							SKL_PIPE_CONN_TYPE_FE)
1111d1730c3dSJeeja KP 					ret = skl_run_pipe(ctx,
1112d1730c3dSJeeja KP 							sink_mconfig->pipe);
1113d93f8e55SVinod Koul 				if (ret)
1114d93f8e55SVinod Koul 					return ret;
1115d93f8e55SVinod Koul 			}
1116d93f8e55SVinod Koul 		}
1117d93f8e55SVinod Koul 	}
1118d93f8e55SVinod Koul 
111910a5439fSguneshwor.o.singh@intel.com 	if (!sink && next_sink)
11206bd4cf85SJeeja KP 		return skl_tplg_bind_sinks(next_sink, skl, src_w, src_mconfig);
11218724ff17SJeeja KP 
11228724ff17SJeeja KP 	return 0;
11238724ff17SJeeja KP }
11248724ff17SJeeja KP 
1125d93f8e55SVinod Koul /*
1126d93f8e55SVinod Koul  * A PGA represents a module in a pipeline. So in the Pre-PMU event of PGA
1127d93f8e55SVinod Koul  * we need to do following:
1128d93f8e55SVinod Koul  *   - Bind to sink pipeline
1129d93f8e55SVinod Koul  *      Since the sink pipes can be running and we don't get mixer event on
1130d93f8e55SVinod Koul  *      connect for already running mixer, we need to find the sink pipes
1131d93f8e55SVinod Koul  *      here and bind to them. This way dynamic connect works.
1132d93f8e55SVinod Koul  *   - Start sink pipeline, if not running
1133d93f8e55SVinod Koul  *   - Then run current pipe
1134d93f8e55SVinod Koul  */
1135d93f8e55SVinod Koul static int skl_tplg_pga_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
1136d93f8e55SVinod Koul 								struct skl *skl)
1137d93f8e55SVinod Koul {
11388724ff17SJeeja KP 	struct skl_module_cfg *src_mconfig;
1139d93f8e55SVinod Koul 	struct skl_sst *ctx = skl->skl_sst;
1140d93f8e55SVinod Koul 	int ret = 0;
1141d93f8e55SVinod Koul 
11428724ff17SJeeja KP 	src_mconfig = w->priv;
1143d93f8e55SVinod Koul 
1144d93f8e55SVinod Koul 	/*
1145d93f8e55SVinod Koul 	 * find which sink it is connected to, bind with the sink,
1146d93f8e55SVinod Koul 	 * if sink is not started, start sink pipe first, then start
1147d93f8e55SVinod Koul 	 * this pipe
1148d93f8e55SVinod Koul 	 */
11496bd4cf85SJeeja KP 	ret = skl_tplg_bind_sinks(w, skl, w, src_mconfig);
11508724ff17SJeeja KP 	if (ret)
11518724ff17SJeeja KP 		return ret;
11528724ff17SJeeja KP 
1153d93f8e55SVinod Koul 	/* Start source pipe last after starting all sinks */
1154d1730c3dSJeeja KP 	if (src_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
1155d1730c3dSJeeja KP 		return skl_run_pipe(ctx, src_mconfig->pipe);
1156d93f8e55SVinod Koul 
1157d93f8e55SVinod Koul 	return 0;
1158d93f8e55SVinod Koul }
1159d93f8e55SVinod Koul 
11608724ff17SJeeja KP static struct snd_soc_dapm_widget *skl_get_src_dsp_widget(
11618724ff17SJeeja KP 		struct snd_soc_dapm_widget *w, struct skl *skl)
11628724ff17SJeeja KP {
11638724ff17SJeeja KP 	struct snd_soc_dapm_path *p;
11648724ff17SJeeja KP 	struct snd_soc_dapm_widget *src_w = NULL;
11658724ff17SJeeja KP 	struct skl_sst *ctx = skl->skl_sst;
11668724ff17SJeeja KP 
1167d93f8e55SVinod Koul 	snd_soc_dapm_widget_for_each_source_path(w, p) {
11688724ff17SJeeja KP 		src_w = p->source;
1169d93f8e55SVinod Koul 		if (!p->connect)
1170d93f8e55SVinod Koul 			continue;
1171d93f8e55SVinod Koul 
11728724ff17SJeeja KP 		dev_dbg(ctx->dev, "sink widget=%s\n", w->name);
11738724ff17SJeeja KP 		dev_dbg(ctx->dev, "src widget=%s\n", p->source->name);
1174d93f8e55SVinod Koul 
1175d93f8e55SVinod Koul 		/*
11768724ff17SJeeja KP 		 * here we will check widgets in sink pipelines, so that can
11778724ff17SJeeja KP 		 * be any widgets type and we are only interested if they are
11788724ff17SJeeja KP 		 * ones used for SKL so check that first
1179d93f8e55SVinod Koul 		 */
11808724ff17SJeeja KP 		if ((p->source->priv != NULL) &&
1181cb1f904dSGuneshwor Singh 				is_skl_dsp_widget_type(p->source, ctx->dev)) {
11828724ff17SJeeja KP 			return p->source;
1183d93f8e55SVinod Koul 		}
1184d93f8e55SVinod Koul 	}
1185d93f8e55SVinod Koul 
11868724ff17SJeeja KP 	if (src_w != NULL)
11878724ff17SJeeja KP 		return skl_get_src_dsp_widget(src_w, skl);
1188d93f8e55SVinod Koul 
11898724ff17SJeeja KP 	return NULL;
1190d93f8e55SVinod Koul }
1191d93f8e55SVinod Koul 
1192d93f8e55SVinod Koul /*
1193d93f8e55SVinod Koul  * in the Post-PMU event of mixer we need to do following:
1194d93f8e55SVinod Koul  *   - Check if this pipe is running
1195d93f8e55SVinod Koul  *   - if not, then
1196d93f8e55SVinod Koul  *	- bind this pipeline to its source pipeline
1197d93f8e55SVinod Koul  *	  if source pipe is already running, this means it is a dynamic
1198d93f8e55SVinod Koul  *	  connection and we need to bind only to that pipe
1199d93f8e55SVinod Koul  *	- start this pipeline
1200d93f8e55SVinod Koul  */
1201d93f8e55SVinod Koul static int skl_tplg_mixer_dapm_post_pmu_event(struct snd_soc_dapm_widget *w,
1202d93f8e55SVinod Koul 							struct skl *skl)
1203d93f8e55SVinod Koul {
1204d93f8e55SVinod Koul 	int ret = 0;
1205d93f8e55SVinod Koul 	struct snd_soc_dapm_widget *source, *sink;
1206d93f8e55SVinod Koul 	struct skl_module_cfg *src_mconfig, *sink_mconfig;
1207d93f8e55SVinod Koul 	struct skl_sst *ctx = skl->skl_sst;
1208d93f8e55SVinod Koul 	int src_pipe_started = 0;
1209d93f8e55SVinod Koul 
1210d93f8e55SVinod Koul 	sink = w;
1211d93f8e55SVinod Koul 	sink_mconfig = sink->priv;
1212d93f8e55SVinod Koul 
1213d93f8e55SVinod Koul 	/*
1214d93f8e55SVinod Koul 	 * If source pipe is already started, that means source is driving
1215d93f8e55SVinod Koul 	 * one more sink before this sink got connected, Since source is
1216d93f8e55SVinod Koul 	 * started, bind this sink to source and start this pipe.
1217d93f8e55SVinod Koul 	 */
12188724ff17SJeeja KP 	source = skl_get_src_dsp_widget(w, skl);
12198724ff17SJeeja KP 	if (source != NULL) {
1220d93f8e55SVinod Koul 		src_mconfig = source->priv;
1221d93f8e55SVinod Koul 		sink_mconfig = sink->priv;
1222d93f8e55SVinod Koul 		src_pipe_started = 1;
1223d93f8e55SVinod Koul 
1224d93f8e55SVinod Koul 		/*
12258724ff17SJeeja KP 		 * check pipe state, then no need to bind or start the
12268724ff17SJeeja KP 		 * pipe
1227d93f8e55SVinod Koul 		 */
1228d93f8e55SVinod Koul 		if (src_mconfig->pipe->state != SKL_PIPE_STARTED)
1229d93f8e55SVinod Koul 			src_pipe_started = 0;
1230d93f8e55SVinod Koul 	}
1231d93f8e55SVinod Koul 
1232d93f8e55SVinod Koul 	if (src_pipe_started) {
1233d93f8e55SVinod Koul 		ret = skl_bind_modules(ctx, src_mconfig, sink_mconfig);
1234d93f8e55SVinod Koul 		if (ret)
1235d93f8e55SVinod Koul 			return ret;
1236d93f8e55SVinod Koul 
1237cc6a4044SJeeja KP 		/* set module params after bind */
1238cc6a4044SJeeja KP 		skl_tplg_set_module_bind_params(source, src_mconfig, ctx);
1239cc6a4044SJeeja KP 		skl_tplg_set_module_bind_params(sink, sink_mconfig, ctx);
1240cc6a4044SJeeja KP 
1241d1730c3dSJeeja KP 		if (sink_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
1242d93f8e55SVinod Koul 			ret = skl_run_pipe(ctx, sink_mconfig->pipe);
1243d93f8e55SVinod Koul 	}
1244d93f8e55SVinod Koul 
1245d93f8e55SVinod Koul 	return ret;
1246d93f8e55SVinod Koul }
1247d93f8e55SVinod Koul 
1248d93f8e55SVinod Koul /*
1249d93f8e55SVinod Koul  * in the Pre-PMD event of mixer we need to do following:
1250d93f8e55SVinod Koul  *   - Stop the pipe
1251d93f8e55SVinod Koul  *   - find the source connections and remove that from dapm_path_list
1252d93f8e55SVinod Koul  *   - unbind with source pipelines if still connected
1253d93f8e55SVinod Koul  */
1254d93f8e55SVinod Koul static int skl_tplg_mixer_dapm_pre_pmd_event(struct snd_soc_dapm_widget *w,
1255d93f8e55SVinod Koul 							struct skl *skl)
1256d93f8e55SVinod Koul {
1257d93f8e55SVinod Koul 	struct skl_module_cfg *src_mconfig, *sink_mconfig;
1258ce1b5551SJeeja KP 	int ret = 0, i;
1259d93f8e55SVinod Koul 	struct skl_sst *ctx = skl->skl_sst;
1260d93f8e55SVinod Koul 
1261ce1b5551SJeeja KP 	sink_mconfig = w->priv;
1262d93f8e55SVinod Koul 
1263d93f8e55SVinod Koul 	/* Stop the pipe */
1264d93f8e55SVinod Koul 	ret = skl_stop_pipe(ctx, sink_mconfig->pipe);
1265d93f8e55SVinod Koul 	if (ret)
1266d93f8e55SVinod Koul 		return ret;
1267d93f8e55SVinod Koul 
1268f6fa56e2SRamesh Babu 	for (i = 0; i < sink_mconfig->module->max_input_pins; i++) {
1269ce1b5551SJeeja KP 		if (sink_mconfig->m_in_pin[i].pin_state == SKL_PIN_BIND_DONE) {
1270ce1b5551SJeeja KP 			src_mconfig = sink_mconfig->m_in_pin[i].tgt_mcfg;
1271ce1b5551SJeeja KP 			if (!src_mconfig)
1272ce1b5551SJeeja KP 				continue;
1273d93f8e55SVinod Koul 
1274ce1b5551SJeeja KP 			ret = skl_unbind_modules(ctx,
1275ce1b5551SJeeja KP 						src_mconfig, sink_mconfig);
1276ce1b5551SJeeja KP 		}
1277d93f8e55SVinod Koul 	}
1278d93f8e55SVinod Koul 
1279d93f8e55SVinod Koul 	return ret;
1280d93f8e55SVinod Koul }
1281d93f8e55SVinod Koul 
1282d93f8e55SVinod Koul /*
1283d93f8e55SVinod Koul  * in the Post-PMD event of mixer we need to do following:
1284d93f8e55SVinod Koul  *   - Free the mcps used
1285d93f8e55SVinod Koul  *   - Free the mem used
1286d93f8e55SVinod Koul  *   - Unbind the modules within the pipeline
1287d93f8e55SVinod Koul  *   - Delete the pipeline (modules are not required to be explicitly
1288d93f8e55SVinod Koul  *     deleted, pipeline delete is enough here
1289d93f8e55SVinod Koul  */
1290d93f8e55SVinod Koul static int skl_tplg_mixer_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
1291d93f8e55SVinod Koul 							struct skl *skl)
1292d93f8e55SVinod Koul {
1293d93f8e55SVinod Koul 	struct skl_module_cfg *mconfig = w->priv;
1294d93f8e55SVinod Koul 	struct skl_pipe_module *w_module;
1295d93f8e55SVinod Koul 	struct skl_module_cfg *src_module = NULL, *dst_module;
1296d93f8e55SVinod Koul 	struct skl_sst *ctx = skl->skl_sst;
1297d93f8e55SVinod Koul 	struct skl_pipe *s_pipe = mconfig->pipe;
1298550b349aSDan Carpenter 	struct skl_module_deferred_bind *modules, *tmp;
1299d93f8e55SVinod Koul 
1300260eb73aSDharageswari R 	if (s_pipe->state == SKL_PIPE_INVALID)
1301260eb73aSDharageswari R 		return -EINVAL;
1302260eb73aSDharageswari R 
1303d93f8e55SVinod Koul 	skl_tplg_free_pipe_mcps(skl, mconfig);
130465976878SVinod Koul 	skl_tplg_free_pipe_mem(skl, mconfig);
1305d93f8e55SVinod Koul 
1306d93f8e55SVinod Koul 	list_for_each_entry(w_module, &s_pipe->w_list, node) {
1307b8c722ddSJeeja KP 		if (list_empty(&skl->bind_list))
1308b8c722ddSJeeja KP 			break;
1309b8c722ddSJeeja KP 
1310b8c722ddSJeeja KP 		src_module = w_module->w->priv;
1311b8c722ddSJeeja KP 
1312550b349aSDan Carpenter 		list_for_each_entry_safe(modules, tmp, &skl->bind_list, node) {
1313b8c722ddSJeeja KP 			/*
1314b8c722ddSJeeja KP 			 * When the destination module is deleted, Unbind the
1315b8c722ddSJeeja KP 			 * modules from deferred bind list.
1316b8c722ddSJeeja KP 			 */
1317b8c722ddSJeeja KP 			if (modules->dst == src_module) {
1318b8c722ddSJeeja KP 				skl_unbind_modules(ctx, modules->src,
1319b8c722ddSJeeja KP 						modules->dst);
1320b8c722ddSJeeja KP 			}
1321b8c722ddSJeeja KP 
1322b8c722ddSJeeja KP 			/*
1323b8c722ddSJeeja KP 			 * When the source module is deleted, remove this entry
1324b8c722ddSJeeja KP 			 * from the deferred bind list.
1325b8c722ddSJeeja KP 			 */
1326b8c722ddSJeeja KP 			if (modules->src == src_module) {
1327b8c722ddSJeeja KP 				list_del(&modules->node);
1328b8c722ddSJeeja KP 				modules->src = NULL;
1329b8c722ddSJeeja KP 				modules->dst = NULL;
1330b8c722ddSJeeja KP 				kfree(modules);
1331b8c722ddSJeeja KP 			}
1332b8c722ddSJeeja KP 		}
1333b8c722ddSJeeja KP 	}
1334b8c722ddSJeeja KP 
1335b8c722ddSJeeja KP 	list_for_each_entry(w_module, &s_pipe->w_list, node) {
1336d93f8e55SVinod Koul 		dst_module = w_module->w->priv;
1337d93f8e55SVinod Koul 
1338260eb73aSDharageswari R 		if (mconfig->m_state >= SKL_MODULE_INIT_DONE)
13397ae3cb15SVinod Koul 			skl_tplg_free_pipe_mcps(skl, dst_module);
1340d93f8e55SVinod Koul 		if (src_module == NULL) {
1341d93f8e55SVinod Koul 			src_module = dst_module;
1342d93f8e55SVinod Koul 			continue;
1343d93f8e55SVinod Koul 		}
1344d93f8e55SVinod Koul 
13457ca42f5aSGuneshwor Singh 		skl_unbind_modules(ctx, src_module, dst_module);
1346d93f8e55SVinod Koul 		src_module = dst_module;
1347d93f8e55SVinod Koul 	}
1348d93f8e55SVinod Koul 
1349547cafa3SVinod Koul 	skl_delete_pipe(ctx, mconfig->pipe);
1350d93f8e55SVinod Koul 
1351473a4d51SJeeja KP 	list_for_each_entry(w_module, &s_pipe->w_list, node) {
1352473a4d51SJeeja KP 		src_module = w_module->w->priv;
1353473a4d51SJeeja KP 		src_module->m_state = SKL_MODULE_UNINIT;
1354473a4d51SJeeja KP 	}
1355473a4d51SJeeja KP 
13566c5768b3SDharageswari R 	return skl_tplg_unload_pipe_modules(ctx, s_pipe);
1357d93f8e55SVinod Koul }
1358d93f8e55SVinod Koul 
1359d93f8e55SVinod Koul /*
1360d93f8e55SVinod Koul  * in the Post-PMD event of PGA we need to do following:
1361d93f8e55SVinod Koul  *   - Free the mcps used
1362d93f8e55SVinod Koul  *   - Stop the pipeline
1363d93f8e55SVinod Koul  *   - In source pipe is connected, unbind with source pipelines
1364d93f8e55SVinod Koul  */
1365d93f8e55SVinod Koul static int skl_tplg_pga_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
1366d93f8e55SVinod Koul 								struct skl *skl)
1367d93f8e55SVinod Koul {
1368d93f8e55SVinod Koul 	struct skl_module_cfg *src_mconfig, *sink_mconfig;
1369ce1b5551SJeeja KP 	int ret = 0, i;
1370d93f8e55SVinod Koul 	struct skl_sst *ctx = skl->skl_sst;
1371d93f8e55SVinod Koul 
1372ce1b5551SJeeja KP 	src_mconfig = w->priv;
1373d93f8e55SVinod Koul 
1374d93f8e55SVinod Koul 	/* Stop the pipe since this is a mixin module */
1375d93f8e55SVinod Koul 	ret = skl_stop_pipe(ctx, src_mconfig->pipe);
1376d93f8e55SVinod Koul 	if (ret)
1377d93f8e55SVinod Koul 		return ret;
1378d93f8e55SVinod Koul 
1379f6fa56e2SRamesh Babu 	for (i = 0; i < src_mconfig->module->max_output_pins; i++) {
1380ce1b5551SJeeja KP 		if (src_mconfig->m_out_pin[i].pin_state == SKL_PIN_BIND_DONE) {
1381ce1b5551SJeeja KP 			sink_mconfig = src_mconfig->m_out_pin[i].tgt_mcfg;
1382ce1b5551SJeeja KP 			if (!sink_mconfig)
1383ce1b5551SJeeja KP 				continue;
1384d93f8e55SVinod Koul 			/*
1385ce1b5551SJeeja KP 			 * This is a connecter and if path is found that means
1386d93f8e55SVinod Koul 			 * unbind between source and sink has not happened yet
1387d93f8e55SVinod Koul 			 */
1388ce1b5551SJeeja KP 			ret = skl_unbind_modules(ctx, src_mconfig,
1389ce1b5551SJeeja KP 							sink_mconfig);
1390ce1b5551SJeeja KP 		}
1391d93f8e55SVinod Koul 	}
1392d93f8e55SVinod Koul 
1393d93f8e55SVinod Koul 	return ret;
1394d93f8e55SVinod Koul }
1395d93f8e55SVinod Koul 
1396d93f8e55SVinod Koul /*
1397d93f8e55SVinod Koul  * In modelling, we assume there will be ONLY one mixer in a pipeline. If a
1398d93f8e55SVinod Koul  * second one is required that is created as another pipe entity.
1399d93f8e55SVinod Koul  * The mixer is responsible for pipe management and represent a pipeline
1400d93f8e55SVinod Koul  * instance
1401d93f8e55SVinod Koul  */
1402d93f8e55SVinod Koul static int skl_tplg_mixer_event(struct snd_soc_dapm_widget *w,
1403d93f8e55SVinod Koul 				struct snd_kcontrol *k, int event)
1404d93f8e55SVinod Koul {
1405d93f8e55SVinod Koul 	struct snd_soc_dapm_context *dapm = w->dapm;
1406d93f8e55SVinod Koul 	struct skl *skl = get_skl_ctx(dapm->dev);
1407d93f8e55SVinod Koul 
1408d93f8e55SVinod Koul 	switch (event) {
1409d93f8e55SVinod Koul 	case SND_SOC_DAPM_PRE_PMU:
1410d93f8e55SVinod Koul 		return skl_tplg_mixer_dapm_pre_pmu_event(w, skl);
1411d93f8e55SVinod Koul 
1412d93f8e55SVinod Koul 	case SND_SOC_DAPM_POST_PMU:
1413d93f8e55SVinod Koul 		return skl_tplg_mixer_dapm_post_pmu_event(w, skl);
1414d93f8e55SVinod Koul 
1415d93f8e55SVinod Koul 	case SND_SOC_DAPM_PRE_PMD:
1416d93f8e55SVinod Koul 		return skl_tplg_mixer_dapm_pre_pmd_event(w, skl);
1417d93f8e55SVinod Koul 
1418d93f8e55SVinod Koul 	case SND_SOC_DAPM_POST_PMD:
1419d93f8e55SVinod Koul 		return skl_tplg_mixer_dapm_post_pmd_event(w, skl);
1420d93f8e55SVinod Koul 	}
1421d93f8e55SVinod Koul 
1422d93f8e55SVinod Koul 	return 0;
1423d93f8e55SVinod Koul }
1424d93f8e55SVinod Koul 
1425d93f8e55SVinod Koul /*
1426d93f8e55SVinod Koul  * In modelling, we assumed rest of the modules in pipeline are PGA. But we
1427d93f8e55SVinod Koul  * are interested in last PGA (leaf PGA) in a pipeline to disconnect with
1428d93f8e55SVinod Koul  * the sink when it is running (two FE to one BE or one FE to two BE)
1429d93f8e55SVinod Koul  * scenarios
1430d93f8e55SVinod Koul  */
1431d93f8e55SVinod Koul static int skl_tplg_pga_event(struct snd_soc_dapm_widget *w,
1432d93f8e55SVinod Koul 			struct snd_kcontrol *k, int event)
1433d93f8e55SVinod Koul 
1434d93f8e55SVinod Koul {
1435d93f8e55SVinod Koul 	struct snd_soc_dapm_context *dapm = w->dapm;
1436d93f8e55SVinod Koul 	struct skl *skl = get_skl_ctx(dapm->dev);
1437d93f8e55SVinod Koul 
1438d93f8e55SVinod Koul 	switch (event) {
1439d93f8e55SVinod Koul 	case SND_SOC_DAPM_PRE_PMU:
1440d93f8e55SVinod Koul 		return skl_tplg_pga_dapm_pre_pmu_event(w, skl);
1441d93f8e55SVinod Koul 
1442d93f8e55SVinod Koul 	case SND_SOC_DAPM_POST_PMD:
1443d93f8e55SVinod Koul 		return skl_tplg_pga_dapm_post_pmd_event(w, skl);
1444d93f8e55SVinod Koul 	}
1445d93f8e55SVinod Koul 
1446d93f8e55SVinod Koul 	return 0;
1447d93f8e55SVinod Koul }
1448cfb0a873SVinod Koul 
1449140adfbaSJeeja KP static int skl_tplg_tlv_control_get(struct snd_kcontrol *kcontrol,
1450140adfbaSJeeja KP 			unsigned int __user *data, unsigned int size)
1451140adfbaSJeeja KP {
1452140adfbaSJeeja KP 	struct soc_bytes_ext *sb =
1453140adfbaSJeeja KP 			(struct soc_bytes_ext *)kcontrol->private_value;
1454140adfbaSJeeja KP 	struct skl_algo_data *bc = (struct skl_algo_data *)sb->dobj.private;
14557d9f2911SOmair M Abdullah 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
14567d9f2911SOmair M Abdullah 	struct skl_module_cfg *mconfig = w->priv;
14577d9f2911SOmair M Abdullah 	struct skl *skl = get_skl_ctx(w->dapm->dev);
14587d9f2911SOmair M Abdullah 
14597d9f2911SOmair M Abdullah 	if (w->power)
14607d9f2911SOmair M Abdullah 		skl_get_module_params(skl->skl_sst, (u32 *)bc->params,
14610d682104SDharageswari R 				      bc->size, bc->param_id, mconfig);
1462140adfbaSJeeja KP 
146341556f68SVinod Koul 	/* decrement size for TLV header */
146441556f68SVinod Koul 	size -= 2 * sizeof(u32);
146541556f68SVinod Koul 
146641556f68SVinod Koul 	/* check size as we don't want to send kernel data */
146741556f68SVinod Koul 	if (size > bc->max)
146841556f68SVinod Koul 		size = bc->max;
146941556f68SVinod Koul 
1470140adfbaSJeeja KP 	if (bc->params) {
1471140adfbaSJeeja KP 		if (copy_to_user(data, &bc->param_id, sizeof(u32)))
1472140adfbaSJeeja KP 			return -EFAULT;
1473e8bc3c99SDan Carpenter 		if (copy_to_user(data + 1, &size, sizeof(u32)))
1474140adfbaSJeeja KP 			return -EFAULT;
1475e8bc3c99SDan Carpenter 		if (copy_to_user(data + 2, bc->params, size))
1476140adfbaSJeeja KP 			return -EFAULT;
1477140adfbaSJeeja KP 	}
1478140adfbaSJeeja KP 
1479140adfbaSJeeja KP 	return 0;
1480140adfbaSJeeja KP }
1481140adfbaSJeeja KP 
1482140adfbaSJeeja KP #define SKL_PARAM_VENDOR_ID 0xff
1483140adfbaSJeeja KP 
1484140adfbaSJeeja KP static int skl_tplg_tlv_control_set(struct snd_kcontrol *kcontrol,
1485140adfbaSJeeja KP 			const unsigned int __user *data, unsigned int size)
1486140adfbaSJeeja KP {
1487140adfbaSJeeja KP 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1488140adfbaSJeeja KP 	struct skl_module_cfg *mconfig = w->priv;
1489140adfbaSJeeja KP 	struct soc_bytes_ext *sb =
1490140adfbaSJeeja KP 			(struct soc_bytes_ext *)kcontrol->private_value;
1491140adfbaSJeeja KP 	struct skl_algo_data *ac = (struct skl_algo_data *)sb->dobj.private;
1492140adfbaSJeeja KP 	struct skl *skl = get_skl_ctx(w->dapm->dev);
1493140adfbaSJeeja KP 
1494140adfbaSJeeja KP 	if (ac->params) {
14950d682104SDharageswari R 		if (size > ac->max)
14960d682104SDharageswari R 			return -EINVAL;
14970d682104SDharageswari R 
14980d682104SDharageswari R 		ac->size = size;
1499140adfbaSJeeja KP 		/*
1500140adfbaSJeeja KP 		 * if the param_is is of type Vendor, firmware expects actual
1501140adfbaSJeeja KP 		 * parameter id and size from the control.
1502140adfbaSJeeja KP 		 */
1503140adfbaSJeeja KP 		if (ac->param_id == SKL_PARAM_VENDOR_ID) {
1504140adfbaSJeeja KP 			if (copy_from_user(ac->params, data, size))
1505140adfbaSJeeja KP 				return -EFAULT;
1506140adfbaSJeeja KP 		} else {
1507140adfbaSJeeja KP 			if (copy_from_user(ac->params,
150865b4bcb8SAlan 					   data + 2, size))
1509140adfbaSJeeja KP 				return -EFAULT;
1510140adfbaSJeeja KP 		}
1511140adfbaSJeeja KP 
1512140adfbaSJeeja KP 		if (w->power)
1513140adfbaSJeeja KP 			return skl_set_module_params(skl->skl_sst,
15140d682104SDharageswari R 						(u32 *)ac->params, ac->size,
1515140adfbaSJeeja KP 						ac->param_id, mconfig);
1516140adfbaSJeeja KP 	}
1517140adfbaSJeeja KP 
1518140adfbaSJeeja KP 	return 0;
1519140adfbaSJeeja KP }
1520140adfbaSJeeja KP 
15217a1b749bSDharageswari R static int skl_tplg_mic_control_get(struct snd_kcontrol *kcontrol,
15227a1b749bSDharageswari R 		struct snd_ctl_elem_value *ucontrol)
15237a1b749bSDharageswari R {
15247a1b749bSDharageswari R 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
15257a1b749bSDharageswari R 	struct skl_module_cfg *mconfig = w->priv;
15267a1b749bSDharageswari R 	struct soc_enum *ec = (struct soc_enum *)kcontrol->private_value;
15277a1b749bSDharageswari R 	u32 ch_type = *((u32 *)ec->dobj.private);
15287a1b749bSDharageswari R 
15297a1b749bSDharageswari R 	if (mconfig->dmic_ch_type == ch_type)
15307a1b749bSDharageswari R 		ucontrol->value.enumerated.item[0] =
15317a1b749bSDharageswari R 					mconfig->dmic_ch_combo_index;
15327a1b749bSDharageswari R 	else
15337a1b749bSDharageswari R 		ucontrol->value.enumerated.item[0] = 0;
15347a1b749bSDharageswari R 
15357a1b749bSDharageswari R 	return 0;
15367a1b749bSDharageswari R }
15377a1b749bSDharageswari R 
15387a1b749bSDharageswari R static int skl_fill_mic_sel_params(struct skl_module_cfg *mconfig,
15397a1b749bSDharageswari R 	struct skl_mic_sel_config *mic_cfg, struct device *dev)
15407a1b749bSDharageswari R {
15417a1b749bSDharageswari R 	struct skl_specific_cfg *sp_cfg = &mconfig->formats_config;
15427a1b749bSDharageswari R 
15437a1b749bSDharageswari R 	sp_cfg->caps_size = sizeof(struct skl_mic_sel_config);
15447a1b749bSDharageswari R 	sp_cfg->set_params = SKL_PARAM_SET;
15457a1b749bSDharageswari R 	sp_cfg->param_id = 0x00;
15467a1b749bSDharageswari R 	if (!sp_cfg->caps) {
15477a1b749bSDharageswari R 		sp_cfg->caps = devm_kzalloc(dev, sp_cfg->caps_size, GFP_KERNEL);
15487a1b749bSDharageswari R 		if (!sp_cfg->caps)
15497a1b749bSDharageswari R 			return -ENOMEM;
15507a1b749bSDharageswari R 	}
15517a1b749bSDharageswari R 
15527a1b749bSDharageswari R 	mic_cfg->mic_switch = SKL_MIC_SEL_SWITCH;
15537a1b749bSDharageswari R 	mic_cfg->flags = 0;
15547a1b749bSDharageswari R 	memcpy(sp_cfg->caps, mic_cfg, sp_cfg->caps_size);
15557a1b749bSDharageswari R 
15567a1b749bSDharageswari R 	return 0;
15577a1b749bSDharageswari R }
15587a1b749bSDharageswari R 
15597a1b749bSDharageswari R static int skl_tplg_mic_control_set(struct snd_kcontrol *kcontrol,
15607a1b749bSDharageswari R 			struct snd_ctl_elem_value *ucontrol)
15617a1b749bSDharageswari R {
15627a1b749bSDharageswari R 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
15637a1b749bSDharageswari R 	struct skl_module_cfg *mconfig = w->priv;
15647a1b749bSDharageswari R 	struct skl_mic_sel_config mic_cfg = {0};
15657a1b749bSDharageswari R 	struct soc_enum *ec = (struct soc_enum *)kcontrol->private_value;
15667a1b749bSDharageswari R 	u32 ch_type = *((u32 *)ec->dobj.private);
15677a1b749bSDharageswari R 	const int *list;
15687a1b749bSDharageswari R 	u8 in_ch, out_ch, index;
15697a1b749bSDharageswari R 
15707a1b749bSDharageswari R 	mconfig->dmic_ch_type = ch_type;
15717a1b749bSDharageswari R 	mconfig->dmic_ch_combo_index = ucontrol->value.enumerated.item[0];
15727a1b749bSDharageswari R 
15737a1b749bSDharageswari R 	/* enum control index 0 is INVALID, so no channels to be set */
15747a1b749bSDharageswari R 	if (mconfig->dmic_ch_combo_index == 0)
15757a1b749bSDharageswari R 		return 0;
15767a1b749bSDharageswari R 
15777a1b749bSDharageswari R 	/* No valid channel selection map for index 0, so offset by 1 */
15787a1b749bSDharageswari R 	index = mconfig->dmic_ch_combo_index - 1;
15797a1b749bSDharageswari R 
15807a1b749bSDharageswari R 	switch (ch_type) {
15817a1b749bSDharageswari R 	case SKL_CH_MONO:
15827a1b749bSDharageswari R 		if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_mono_list))
15837a1b749bSDharageswari R 			return -EINVAL;
15847a1b749bSDharageswari R 
15857a1b749bSDharageswari R 		list = &mic_mono_list[index];
15867a1b749bSDharageswari R 		break;
15877a1b749bSDharageswari R 
15887a1b749bSDharageswari R 	case SKL_CH_STEREO:
15897a1b749bSDharageswari R 		if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_stereo_list))
15907a1b749bSDharageswari R 			return -EINVAL;
15917a1b749bSDharageswari R 
15927a1b749bSDharageswari R 		list = mic_stereo_list[index];
15937a1b749bSDharageswari R 		break;
15947a1b749bSDharageswari R 
15957a1b749bSDharageswari R 	case SKL_CH_TRIO:
15967a1b749bSDharageswari R 		if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_trio_list))
15977a1b749bSDharageswari R 			return -EINVAL;
15987a1b749bSDharageswari R 
15997a1b749bSDharageswari R 		list = mic_trio_list[index];
16007a1b749bSDharageswari R 		break;
16017a1b749bSDharageswari R 
16027a1b749bSDharageswari R 	case SKL_CH_QUATRO:
16037a1b749bSDharageswari R 		if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_quatro_list))
16047a1b749bSDharageswari R 			return -EINVAL;
16057a1b749bSDharageswari R 
16067a1b749bSDharageswari R 		list = mic_quatro_list[index];
16077a1b749bSDharageswari R 		break;
16087a1b749bSDharageswari R 
16097a1b749bSDharageswari R 	default:
16107a1b749bSDharageswari R 		dev_err(w->dapm->dev,
16117a1b749bSDharageswari R 				"Invalid channel %d for mic_select module\n",
16127a1b749bSDharageswari R 				ch_type);
16137a1b749bSDharageswari R 		return -EINVAL;
16147a1b749bSDharageswari R 
16157a1b749bSDharageswari R 	}
16167a1b749bSDharageswari R 
16177a1b749bSDharageswari R 	/* channel type enum map to number of chanels for that type */
16187a1b749bSDharageswari R 	for (out_ch = 0; out_ch < ch_type; out_ch++) {
16197a1b749bSDharageswari R 		in_ch = list[out_ch];
16207a1b749bSDharageswari R 		mic_cfg.blob[out_ch][in_ch] = SKL_DEFAULT_MIC_SEL_GAIN;
16217a1b749bSDharageswari R 	}
16227a1b749bSDharageswari R 
16237a1b749bSDharageswari R 	return skl_fill_mic_sel_params(mconfig, &mic_cfg, w->dapm->dev);
16247a1b749bSDharageswari R }
16257a1b749bSDharageswari R 
1626cfb0a873SVinod Koul /*
16278871dcb9SJeeja KP  * Fill the dma id for host and link. In case of passthrough
16288871dcb9SJeeja KP  * pipeline, this will both host and link in the same
16298871dcb9SJeeja KP  * pipeline, so need to copy the link and host based on dev_type
16308871dcb9SJeeja KP  */
16318871dcb9SJeeja KP static void skl_tplg_fill_dma_id(struct skl_module_cfg *mcfg,
16328871dcb9SJeeja KP 				struct skl_pipe_params *params)
16338871dcb9SJeeja KP {
16348871dcb9SJeeja KP 	struct skl_pipe *pipe = mcfg->pipe;
16358871dcb9SJeeja KP 
16368871dcb9SJeeja KP 	if (pipe->passthru) {
16378871dcb9SJeeja KP 		switch (mcfg->dev_type) {
16388871dcb9SJeeja KP 		case SKL_DEVICE_HDALINK:
16398871dcb9SJeeja KP 			pipe->p_params->link_dma_id = params->link_dma_id;
164012c3be0eSJeeja KP 			pipe->p_params->link_index = params->link_index;
16417f975a38SJeeja KP 			pipe->p_params->link_bps = params->link_bps;
16428871dcb9SJeeja KP 			break;
16438871dcb9SJeeja KP 
16448871dcb9SJeeja KP 		case SKL_DEVICE_HDAHOST:
16458871dcb9SJeeja KP 			pipe->p_params->host_dma_id = params->host_dma_id;
16467f975a38SJeeja KP 			pipe->p_params->host_bps = params->host_bps;
16478871dcb9SJeeja KP 			break;
16488871dcb9SJeeja KP 
16498871dcb9SJeeja KP 		default:
16508871dcb9SJeeja KP 			break;
16518871dcb9SJeeja KP 		}
16528871dcb9SJeeja KP 		pipe->p_params->s_fmt = params->s_fmt;
16538871dcb9SJeeja KP 		pipe->p_params->ch = params->ch;
16548871dcb9SJeeja KP 		pipe->p_params->s_freq = params->s_freq;
16558871dcb9SJeeja KP 		pipe->p_params->stream = params->stream;
165612c3be0eSJeeja KP 		pipe->p_params->format = params->format;
16578871dcb9SJeeja KP 
16588871dcb9SJeeja KP 	} else {
16598871dcb9SJeeja KP 		memcpy(pipe->p_params, params, sizeof(*params));
16608871dcb9SJeeja KP 	}
16618871dcb9SJeeja KP }
16628871dcb9SJeeja KP 
16638871dcb9SJeeja KP /*
1664cfb0a873SVinod Koul  * The FE params are passed by hw_params of the DAI.
1665cfb0a873SVinod Koul  * On hw_params, the params are stored in Gateway module of the FE and we
1666cfb0a873SVinod Koul  * need to calculate the format in DSP module configuration, that
1667cfb0a873SVinod Koul  * conversion is done here
1668cfb0a873SVinod Koul  */
1669cfb0a873SVinod Koul int skl_tplg_update_pipe_params(struct device *dev,
1670cfb0a873SVinod Koul 			struct skl_module_cfg *mconfig,
1671cfb0a873SVinod Koul 			struct skl_pipe_params *params)
1672cfb0a873SVinod Koul {
1673f6fa56e2SRamesh Babu 	struct skl_module_res *res = &mconfig->module->resources[0];
1674f6fa56e2SRamesh Babu 	struct skl *skl = get_skl_ctx(dev);
1675cfb0a873SVinod Koul 	struct skl_module_fmt *format = NULL;
1676f6fa56e2SRamesh Babu 	u8 cfg_idx = mconfig->pipe->cur_config_idx;
1677cfb0a873SVinod Koul 
16788871dcb9SJeeja KP 	skl_tplg_fill_dma_id(mconfig, params);
1679f6fa56e2SRamesh Babu 	mconfig->fmt_idx = mconfig->mod_cfg[cfg_idx].fmt_idx;
1680f6fa56e2SRamesh Babu 	mconfig->res_idx = mconfig->mod_cfg[cfg_idx].res_idx;
1681f6fa56e2SRamesh Babu 
1682f6fa56e2SRamesh Babu 	if (skl->nr_modules)
1683f6fa56e2SRamesh Babu 		return 0;
1684cfb0a873SVinod Koul 
1685cfb0a873SVinod Koul 	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK)
1686f6fa56e2SRamesh Babu 		format = &mconfig->module->formats[0].inputs[0].fmt;
1687cfb0a873SVinod Koul 	else
1688f6fa56e2SRamesh Babu 		format = &mconfig->module->formats[0].outputs[0].fmt;
1689cfb0a873SVinod Koul 
1690cfb0a873SVinod Koul 	/* set the hw_params */
1691cfb0a873SVinod Koul 	format->s_freq = params->s_freq;
1692cfb0a873SVinod Koul 	format->channels = params->ch;
1693cfb0a873SVinod Koul 	format->valid_bit_depth = skl_get_bit_depth(params->s_fmt);
1694cfb0a873SVinod Koul 
1695cfb0a873SVinod Koul 	/*
1696cfb0a873SVinod Koul 	 * 16 bit is 16 bit container whereas 24 bit is in 32 bit
1697cfb0a873SVinod Koul 	 * container so update bit depth accordingly
1698cfb0a873SVinod Koul 	 */
1699cfb0a873SVinod Koul 	switch (format->valid_bit_depth) {
1700cfb0a873SVinod Koul 	case SKL_DEPTH_16BIT:
1701cfb0a873SVinod Koul 		format->bit_depth = format->valid_bit_depth;
1702cfb0a873SVinod Koul 		break;
1703cfb0a873SVinod Koul 
1704cfb0a873SVinod Koul 	case SKL_DEPTH_24BIT:
17056654f39eSJeeja KP 	case SKL_DEPTH_32BIT:
1706cfb0a873SVinod Koul 		format->bit_depth = SKL_DEPTH_32BIT;
1707cfb0a873SVinod Koul 		break;
1708cfb0a873SVinod Koul 
1709cfb0a873SVinod Koul 	default:
1710cfb0a873SVinod Koul 		dev_err(dev, "Invalid bit depth %x for pipe\n",
1711cfb0a873SVinod Koul 				format->valid_bit_depth);
1712cfb0a873SVinod Koul 		return -EINVAL;
1713cfb0a873SVinod Koul 	}
1714cfb0a873SVinod Koul 
1715cfb0a873SVinod Koul 	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1716f6fa56e2SRamesh Babu 		res->ibs = (format->s_freq / 1000) *
1717cfb0a873SVinod Koul 				(format->channels) *
1718cfb0a873SVinod Koul 				(format->bit_depth >> 3);
1719cfb0a873SVinod Koul 	} else {
1720f6fa56e2SRamesh Babu 		res->obs = (format->s_freq / 1000) *
1721cfb0a873SVinod Koul 				(format->channels) *
1722cfb0a873SVinod Koul 				(format->bit_depth >> 3);
1723cfb0a873SVinod Koul 	}
1724cfb0a873SVinod Koul 
1725cfb0a873SVinod Koul 	return 0;
1726cfb0a873SVinod Koul }
1727cfb0a873SVinod Koul 
1728cfb0a873SVinod Koul /*
1729cfb0a873SVinod Koul  * Query the module config for the FE DAI
1730cfb0a873SVinod Koul  * This is used to find the hw_params set for that DAI and apply to FE
1731cfb0a873SVinod Koul  * pipeline
1732cfb0a873SVinod Koul  */
1733cfb0a873SVinod Koul struct skl_module_cfg *
1734cfb0a873SVinod Koul skl_tplg_fe_get_cpr_module(struct snd_soc_dai *dai, int stream)
1735cfb0a873SVinod Koul {
1736cfb0a873SVinod Koul 	struct snd_soc_dapm_widget *w;
1737cfb0a873SVinod Koul 	struct snd_soc_dapm_path *p = NULL;
1738cfb0a873SVinod Koul 
1739cfb0a873SVinod Koul 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1740cfb0a873SVinod Koul 		w = dai->playback_widget;
1741f0900eb2SSubhransu S. Prusty 		snd_soc_dapm_widget_for_each_sink_path(w, p) {
1742cfb0a873SVinod Koul 			if (p->connect && p->sink->power &&
1743cb1f904dSGuneshwor Singh 				!is_skl_dsp_widget_type(p->sink, dai->dev))
1744cfb0a873SVinod Koul 				continue;
1745cfb0a873SVinod Koul 
1746cfb0a873SVinod Koul 			if (p->sink->priv) {
1747cfb0a873SVinod Koul 				dev_dbg(dai->dev, "set params for %s\n",
1748cfb0a873SVinod Koul 						p->sink->name);
1749cfb0a873SVinod Koul 				return p->sink->priv;
1750cfb0a873SVinod Koul 			}
1751cfb0a873SVinod Koul 		}
1752cfb0a873SVinod Koul 	} else {
1753cfb0a873SVinod Koul 		w = dai->capture_widget;
1754f0900eb2SSubhransu S. Prusty 		snd_soc_dapm_widget_for_each_source_path(w, p) {
1755cfb0a873SVinod Koul 			if (p->connect && p->source->power &&
1756cb1f904dSGuneshwor Singh 				!is_skl_dsp_widget_type(p->source, dai->dev))
1757cfb0a873SVinod Koul 				continue;
1758cfb0a873SVinod Koul 
1759cfb0a873SVinod Koul 			if (p->source->priv) {
1760cfb0a873SVinod Koul 				dev_dbg(dai->dev, "set params for %s\n",
1761cfb0a873SVinod Koul 						p->source->name);
1762cfb0a873SVinod Koul 				return p->source->priv;
1763cfb0a873SVinod Koul 			}
1764cfb0a873SVinod Koul 		}
1765cfb0a873SVinod Koul 	}
1766cfb0a873SVinod Koul 
1767cfb0a873SVinod Koul 	return NULL;
1768cfb0a873SVinod Koul }
1769cfb0a873SVinod Koul 
1770718a42b5SDharageswari.R static struct skl_module_cfg *skl_get_mconfig_pb_cpr(
1771718a42b5SDharageswari.R 		struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
1772718a42b5SDharageswari.R {
1773718a42b5SDharageswari.R 	struct snd_soc_dapm_path *p;
1774718a42b5SDharageswari.R 	struct skl_module_cfg *mconfig = NULL;
1775718a42b5SDharageswari.R 
1776718a42b5SDharageswari.R 	snd_soc_dapm_widget_for_each_source_path(w, p) {
1777718a42b5SDharageswari.R 		if (w->endpoints[SND_SOC_DAPM_DIR_OUT] > 0) {
1778718a42b5SDharageswari.R 			if (p->connect &&
1779718a42b5SDharageswari.R 				    (p->sink->id == snd_soc_dapm_aif_out) &&
1780718a42b5SDharageswari.R 				    p->source->priv) {
1781718a42b5SDharageswari.R 				mconfig = p->source->priv;
1782718a42b5SDharageswari.R 				return mconfig;
1783718a42b5SDharageswari.R 			}
1784718a42b5SDharageswari.R 			mconfig = skl_get_mconfig_pb_cpr(dai, p->source);
1785718a42b5SDharageswari.R 			if (mconfig)
1786718a42b5SDharageswari.R 				return mconfig;
1787718a42b5SDharageswari.R 		}
1788718a42b5SDharageswari.R 	}
1789718a42b5SDharageswari.R 	return mconfig;
1790718a42b5SDharageswari.R }
1791718a42b5SDharageswari.R 
1792718a42b5SDharageswari.R static struct skl_module_cfg *skl_get_mconfig_cap_cpr(
1793718a42b5SDharageswari.R 		struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
1794718a42b5SDharageswari.R {
1795718a42b5SDharageswari.R 	struct snd_soc_dapm_path *p;
1796718a42b5SDharageswari.R 	struct skl_module_cfg *mconfig = NULL;
1797718a42b5SDharageswari.R 
1798718a42b5SDharageswari.R 	snd_soc_dapm_widget_for_each_sink_path(w, p) {
1799718a42b5SDharageswari.R 		if (w->endpoints[SND_SOC_DAPM_DIR_IN] > 0) {
1800718a42b5SDharageswari.R 			if (p->connect &&
1801718a42b5SDharageswari.R 				    (p->source->id == snd_soc_dapm_aif_in) &&
1802718a42b5SDharageswari.R 				    p->sink->priv) {
1803718a42b5SDharageswari.R 				mconfig = p->sink->priv;
1804718a42b5SDharageswari.R 				return mconfig;
1805718a42b5SDharageswari.R 			}
1806718a42b5SDharageswari.R 			mconfig = skl_get_mconfig_cap_cpr(dai, p->sink);
1807718a42b5SDharageswari.R 			if (mconfig)
1808718a42b5SDharageswari.R 				return mconfig;
1809718a42b5SDharageswari.R 		}
1810718a42b5SDharageswari.R 	}
1811718a42b5SDharageswari.R 	return mconfig;
1812718a42b5SDharageswari.R }
1813718a42b5SDharageswari.R 
1814718a42b5SDharageswari.R struct skl_module_cfg *
1815718a42b5SDharageswari.R skl_tplg_be_get_cpr_module(struct snd_soc_dai *dai, int stream)
1816718a42b5SDharageswari.R {
1817718a42b5SDharageswari.R 	struct snd_soc_dapm_widget *w;
1818718a42b5SDharageswari.R 	struct skl_module_cfg *mconfig;
1819718a42b5SDharageswari.R 
1820718a42b5SDharageswari.R 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1821718a42b5SDharageswari.R 		w = dai->playback_widget;
1822718a42b5SDharageswari.R 		mconfig = skl_get_mconfig_pb_cpr(dai, w);
1823718a42b5SDharageswari.R 	} else {
1824718a42b5SDharageswari.R 		w = dai->capture_widget;
1825718a42b5SDharageswari.R 		mconfig = skl_get_mconfig_cap_cpr(dai, w);
1826718a42b5SDharageswari.R 	}
1827718a42b5SDharageswari.R 	return mconfig;
1828718a42b5SDharageswari.R }
1829718a42b5SDharageswari.R 
1830cfb0a873SVinod Koul static u8 skl_tplg_be_link_type(int dev_type)
1831cfb0a873SVinod Koul {
1832cfb0a873SVinod Koul 	int ret;
1833cfb0a873SVinod Koul 
1834cfb0a873SVinod Koul 	switch (dev_type) {
1835cfb0a873SVinod Koul 	case SKL_DEVICE_BT:
1836cfb0a873SVinod Koul 		ret = NHLT_LINK_SSP;
1837cfb0a873SVinod Koul 		break;
1838cfb0a873SVinod Koul 
1839cfb0a873SVinod Koul 	case SKL_DEVICE_DMIC:
1840cfb0a873SVinod Koul 		ret = NHLT_LINK_DMIC;
1841cfb0a873SVinod Koul 		break;
1842cfb0a873SVinod Koul 
1843cfb0a873SVinod Koul 	case SKL_DEVICE_I2S:
1844cfb0a873SVinod Koul 		ret = NHLT_LINK_SSP;
1845cfb0a873SVinod Koul 		break;
1846cfb0a873SVinod Koul 
1847cfb0a873SVinod Koul 	case SKL_DEVICE_HDALINK:
1848cfb0a873SVinod Koul 		ret = NHLT_LINK_HDA;
1849cfb0a873SVinod Koul 		break;
1850cfb0a873SVinod Koul 
1851cfb0a873SVinod Koul 	default:
1852cfb0a873SVinod Koul 		ret = NHLT_LINK_INVALID;
1853cfb0a873SVinod Koul 		break;
1854cfb0a873SVinod Koul 	}
1855cfb0a873SVinod Koul 
1856cfb0a873SVinod Koul 	return ret;
1857cfb0a873SVinod Koul }
1858cfb0a873SVinod Koul 
1859cfb0a873SVinod Koul /*
1860cfb0a873SVinod Koul  * Fill the BE gateway parameters
1861cfb0a873SVinod Koul  * The BE gateway expects a blob of parameters which are kept in the ACPI
1862cfb0a873SVinod Koul  * NHLT blob, so query the blob for interface type (i2s/pdm) and instance.
1863cfb0a873SVinod Koul  * The port can have multiple settings so pick based on the PCM
1864cfb0a873SVinod Koul  * parameters
1865cfb0a873SVinod Koul  */
1866cfb0a873SVinod Koul static int skl_tplg_be_fill_pipe_params(struct snd_soc_dai *dai,
1867cfb0a873SVinod Koul 				struct skl_module_cfg *mconfig,
1868cfb0a873SVinod Koul 				struct skl_pipe_params *params)
1869cfb0a873SVinod Koul {
1870cfb0a873SVinod Koul 	struct nhlt_specific_cfg *cfg;
1871cfb0a873SVinod Koul 	struct skl *skl = get_skl_ctx(dai->dev);
1872cfb0a873SVinod Koul 	int link_type = skl_tplg_be_link_type(mconfig->dev_type);
1873db2f586bSSenthilnathan Veppur 	u8 dev_type = skl_tplg_be_dev_type(mconfig->dev_type);
1874cfb0a873SVinod Koul 
18758871dcb9SJeeja KP 	skl_tplg_fill_dma_id(mconfig, params);
1876cfb0a873SVinod Koul 
1877b30c275eSJeeja KP 	if (link_type == NHLT_LINK_HDA)
1878b30c275eSJeeja KP 		return 0;
1879b30c275eSJeeja KP 
1880cfb0a873SVinod Koul 	/* update the blob based on virtual bus_id*/
1881cfb0a873SVinod Koul 	cfg = skl_get_ep_blob(skl, mconfig->vbus_id, link_type,
1882cfb0a873SVinod Koul 					params->s_fmt, params->ch,
1883db2f586bSSenthilnathan Veppur 					params->s_freq, params->stream,
1884db2f586bSSenthilnathan Veppur 					dev_type);
1885cfb0a873SVinod Koul 	if (cfg) {
1886cfb0a873SVinod Koul 		mconfig->formats_config.caps_size = cfg->size;
1887bc03281aSJeeja KP 		mconfig->formats_config.caps = (u32 *) &cfg->caps;
1888cfb0a873SVinod Koul 	} else {
1889cfb0a873SVinod Koul 		dev_err(dai->dev, "Blob NULL for id %x type %d dirn %d\n",
1890cfb0a873SVinod Koul 					mconfig->vbus_id, link_type,
1891cfb0a873SVinod Koul 					params->stream);
1892cfb0a873SVinod Koul 		dev_err(dai->dev, "PCM: ch %d, freq %d, fmt %d\n",
1893cfb0a873SVinod Koul 				 params->ch, params->s_freq, params->s_fmt);
1894cfb0a873SVinod Koul 		return -EINVAL;
1895cfb0a873SVinod Koul 	}
1896cfb0a873SVinod Koul 
1897cfb0a873SVinod Koul 	return 0;
1898cfb0a873SVinod Koul }
1899cfb0a873SVinod Koul 
1900cfb0a873SVinod Koul static int skl_tplg_be_set_src_pipe_params(struct snd_soc_dai *dai,
1901cfb0a873SVinod Koul 				struct snd_soc_dapm_widget *w,
1902cfb0a873SVinod Koul 				struct skl_pipe_params *params)
1903cfb0a873SVinod Koul {
1904cfb0a873SVinod Koul 	struct snd_soc_dapm_path *p;
19054d8adccbSSubhransu S. Prusty 	int ret = -EIO;
1906cfb0a873SVinod Koul 
1907f0900eb2SSubhransu S. Prusty 	snd_soc_dapm_widget_for_each_source_path(w, p) {
1908cb1f904dSGuneshwor Singh 		if (p->connect && is_skl_dsp_widget_type(p->source, dai->dev) &&
1909cfb0a873SVinod Koul 						p->source->priv) {
1910cfb0a873SVinod Koul 
19119a03cb49SJeeja KP 			ret = skl_tplg_be_fill_pipe_params(dai,
19129a03cb49SJeeja KP 						p->source->priv, params);
19134d8adccbSSubhransu S. Prusty 			if (ret < 0)
19144d8adccbSSubhransu S. Prusty 				return ret;
1915cfb0a873SVinod Koul 		} else {
19169a03cb49SJeeja KP 			ret = skl_tplg_be_set_src_pipe_params(dai,
19179a03cb49SJeeja KP 						p->source, params);
19184d8adccbSSubhransu S. Prusty 			if (ret < 0)
19194d8adccbSSubhransu S. Prusty 				return ret;
1920cfb0a873SVinod Koul 		}
1921cfb0a873SVinod Koul 	}
1922cfb0a873SVinod Koul 
19234d8adccbSSubhransu S. Prusty 	return ret;
1924cfb0a873SVinod Koul }
1925cfb0a873SVinod Koul 
1926cfb0a873SVinod Koul static int skl_tplg_be_set_sink_pipe_params(struct snd_soc_dai *dai,
1927cfb0a873SVinod Koul 	struct snd_soc_dapm_widget *w, struct skl_pipe_params *params)
1928cfb0a873SVinod Koul {
1929cfb0a873SVinod Koul 	struct snd_soc_dapm_path *p = NULL;
19304d8adccbSSubhransu S. Prusty 	int ret = -EIO;
1931cfb0a873SVinod Koul 
1932f0900eb2SSubhransu S. Prusty 	snd_soc_dapm_widget_for_each_sink_path(w, p) {
1933cb1f904dSGuneshwor Singh 		if (p->connect && is_skl_dsp_widget_type(p->sink, dai->dev) &&
1934cfb0a873SVinod Koul 						p->sink->priv) {
1935cfb0a873SVinod Koul 
19369a03cb49SJeeja KP 			ret = skl_tplg_be_fill_pipe_params(dai,
19379a03cb49SJeeja KP 						p->sink->priv, params);
19384d8adccbSSubhransu S. Prusty 			if (ret < 0)
19394d8adccbSSubhransu S. Prusty 				return ret;
19404d8adccbSSubhransu S. Prusty 		} else {
19414d8adccbSSubhransu S. Prusty 			ret = skl_tplg_be_set_sink_pipe_params(
1942cfb0a873SVinod Koul 						dai, p->sink, params);
19434d8adccbSSubhransu S. Prusty 			if (ret < 0)
19444d8adccbSSubhransu S. Prusty 				return ret;
1945cfb0a873SVinod Koul 		}
1946cfb0a873SVinod Koul 	}
1947cfb0a873SVinod Koul 
19484d8adccbSSubhransu S. Prusty 	return ret;
1949cfb0a873SVinod Koul }
1950cfb0a873SVinod Koul 
1951cfb0a873SVinod Koul /*
1952cfb0a873SVinod Koul  * BE hw_params can be a source parameters (capture) or sink parameters
1953cfb0a873SVinod Koul  * (playback). Based on sink and source we need to either find the source
1954cfb0a873SVinod Koul  * list or the sink list and set the pipeline parameters
1955cfb0a873SVinod Koul  */
1956cfb0a873SVinod Koul int skl_tplg_be_update_params(struct snd_soc_dai *dai,
1957cfb0a873SVinod Koul 				struct skl_pipe_params *params)
1958cfb0a873SVinod Koul {
1959cfb0a873SVinod Koul 	struct snd_soc_dapm_widget *w;
1960cfb0a873SVinod Koul 
1961cfb0a873SVinod Koul 	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1962cfb0a873SVinod Koul 		w = dai->playback_widget;
1963cfb0a873SVinod Koul 
1964cfb0a873SVinod Koul 		return skl_tplg_be_set_src_pipe_params(dai, w, params);
1965cfb0a873SVinod Koul 
1966cfb0a873SVinod Koul 	} else {
1967cfb0a873SVinod Koul 		w = dai->capture_widget;
1968cfb0a873SVinod Koul 
1969cfb0a873SVinod Koul 		return skl_tplg_be_set_sink_pipe_params(dai, w, params);
1970cfb0a873SVinod Koul 	}
1971cfb0a873SVinod Koul 
1972cfb0a873SVinod Koul 	return 0;
1973cfb0a873SVinod Koul }
19743af36706SVinod Koul 
19753af36706SVinod Koul static const struct snd_soc_tplg_widget_events skl_tplg_widget_ops[] = {
19763af36706SVinod Koul 	{SKL_MIXER_EVENT, skl_tplg_mixer_event},
19779a1e3507SVinod Koul 	{SKL_VMIXER_EVENT, skl_tplg_mixer_event},
19783af36706SVinod Koul 	{SKL_PGA_EVENT, skl_tplg_pga_event},
19793af36706SVinod Koul };
19803af36706SVinod Koul 
1981140adfbaSJeeja KP static const struct snd_soc_tplg_bytes_ext_ops skl_tlv_ops[] = {
1982140adfbaSJeeja KP 	{SKL_CONTROL_TYPE_BYTE_TLV, skl_tplg_tlv_control_get,
1983140adfbaSJeeja KP 					skl_tplg_tlv_control_set},
1984140adfbaSJeeja KP };
1985140adfbaSJeeja KP 
19867a1b749bSDharageswari R static const struct snd_soc_tplg_kcontrol_ops skl_tplg_kcontrol_ops[] = {
19877a1b749bSDharageswari R 	{
19887a1b749bSDharageswari R 		.id = SKL_CONTROL_TYPE_MIC_SELECT,
19897a1b749bSDharageswari R 		.get = skl_tplg_mic_control_get,
19907a1b749bSDharageswari R 		.put = skl_tplg_mic_control_set,
19917a1b749bSDharageswari R 	},
19927a1b749bSDharageswari R };
19937a1b749bSDharageswari R 
1994f6fa56e2SRamesh Babu static int skl_tplg_fill_pipe_cfg(struct device *dev,
1995f6fa56e2SRamesh Babu 			struct skl_pipe *pipe, u32 tkn,
1996f6fa56e2SRamesh Babu 			u32 tkn_val, int conf_idx, int dir)
1997f6fa56e2SRamesh Babu {
1998f6fa56e2SRamesh Babu 	struct skl_pipe_fmt *fmt;
1999f6fa56e2SRamesh Babu 	struct skl_path_config *config;
2000f6fa56e2SRamesh Babu 
2001f6fa56e2SRamesh Babu 	switch (dir) {
2002f6fa56e2SRamesh Babu 	case SKL_DIR_IN:
2003f6fa56e2SRamesh Babu 		fmt = &pipe->configs[conf_idx].in_fmt;
2004f6fa56e2SRamesh Babu 		break;
2005f6fa56e2SRamesh Babu 
2006f6fa56e2SRamesh Babu 	case SKL_DIR_OUT:
2007f6fa56e2SRamesh Babu 		fmt = &pipe->configs[conf_idx].out_fmt;
2008f6fa56e2SRamesh Babu 		break;
2009f6fa56e2SRamesh Babu 
2010f6fa56e2SRamesh Babu 	default:
2011f6fa56e2SRamesh Babu 		dev_err(dev, "Invalid direction: %d\n", dir);
2012f6fa56e2SRamesh Babu 		return -EINVAL;
2013f6fa56e2SRamesh Babu 	}
2014f6fa56e2SRamesh Babu 
2015f6fa56e2SRamesh Babu 	config = &pipe->configs[conf_idx];
2016f6fa56e2SRamesh Babu 
2017f6fa56e2SRamesh Babu 	switch (tkn) {
2018f6fa56e2SRamesh Babu 	case SKL_TKN_U32_CFG_FREQ:
2019f6fa56e2SRamesh Babu 		fmt->freq = tkn_val;
2020f6fa56e2SRamesh Babu 		break;
2021f6fa56e2SRamesh Babu 
2022f6fa56e2SRamesh Babu 	case SKL_TKN_U8_CFG_CHAN:
2023f6fa56e2SRamesh Babu 		fmt->channels = tkn_val;
2024f6fa56e2SRamesh Babu 		break;
2025f6fa56e2SRamesh Babu 
2026f6fa56e2SRamesh Babu 	case SKL_TKN_U8_CFG_BPS:
2027f6fa56e2SRamesh Babu 		fmt->bps = tkn_val;
2028f6fa56e2SRamesh Babu 		break;
2029f6fa56e2SRamesh Babu 
2030f6fa56e2SRamesh Babu 	case SKL_TKN_U32_PATH_MEM_PGS:
2031f6fa56e2SRamesh Babu 		config->mem_pages = tkn_val;
2032f6fa56e2SRamesh Babu 		break;
2033f6fa56e2SRamesh Babu 
2034f6fa56e2SRamesh Babu 	default:
2035f6fa56e2SRamesh Babu 		dev_err(dev, "Invalid token config: %d\n", tkn);
2036f6fa56e2SRamesh Babu 		return -EINVAL;
2037f6fa56e2SRamesh Babu 	}
2038f6fa56e2SRamesh Babu 
2039f6fa56e2SRamesh Babu 	return 0;
2040f6fa56e2SRamesh Babu }
2041f6fa56e2SRamesh Babu 
20426277e832SShreyas NC static int skl_tplg_fill_pipe_tkn(struct device *dev,
20436277e832SShreyas NC 			struct skl_pipe *pipe, u32 tkn,
20446277e832SShreyas NC 			u32 tkn_val)
20453af36706SVinod Koul {
20463af36706SVinod Koul 
20476277e832SShreyas NC 	switch (tkn) {
20486277e832SShreyas NC 	case SKL_TKN_U32_PIPE_CONN_TYPE:
20496277e832SShreyas NC 		pipe->conn_type = tkn_val;
20506277e832SShreyas NC 		break;
20516277e832SShreyas NC 
20526277e832SShreyas NC 	case SKL_TKN_U32_PIPE_PRIORITY:
20536277e832SShreyas NC 		pipe->pipe_priority = tkn_val;
20546277e832SShreyas NC 		break;
20556277e832SShreyas NC 
20566277e832SShreyas NC 	case SKL_TKN_U32_PIPE_MEM_PGS:
20576277e832SShreyas NC 		pipe->memory_pages = tkn_val;
20586277e832SShreyas NC 		break;
20596277e832SShreyas NC 
20608a0cb236SVinod Koul 	case SKL_TKN_U32_PMODE:
20618a0cb236SVinod Koul 		pipe->lp_mode = tkn_val;
20628a0cb236SVinod Koul 		break;
20638a0cb236SVinod Koul 
2064f6fa56e2SRamesh Babu 	case SKL_TKN_U32_PIPE_DIRECTION:
2065f6fa56e2SRamesh Babu 		pipe->direction = tkn_val;
2066f6fa56e2SRamesh Babu 		break;
2067f6fa56e2SRamesh Babu 
2068f6fa56e2SRamesh Babu 	case SKL_TKN_U32_NUM_CONFIGS:
2069f6fa56e2SRamesh Babu 		pipe->nr_cfgs = tkn_val;
2070f6fa56e2SRamesh Babu 		break;
2071f6fa56e2SRamesh Babu 
20726277e832SShreyas NC 	default:
20736277e832SShreyas NC 		dev_err(dev, "Token not handled %d\n", tkn);
20746277e832SShreyas NC 		return -EINVAL;
20753af36706SVinod Koul 	}
20766277e832SShreyas NC 
20776277e832SShreyas NC 	return 0;
20783af36706SVinod Koul }
20793af36706SVinod Koul 
20803af36706SVinod Koul /*
20816277e832SShreyas NC  * Add pipeline by parsing the relevant tokens
20826277e832SShreyas NC  * Return an existing pipe if the pipe already exists.
20833af36706SVinod Koul  */
20846277e832SShreyas NC static int skl_tplg_add_pipe(struct device *dev,
20856277e832SShreyas NC 		struct skl_module_cfg *mconfig, struct skl *skl,
20866277e832SShreyas NC 		struct snd_soc_tplg_vendor_value_elem *tkn_elem)
20873af36706SVinod Koul {
20883af36706SVinod Koul 	struct skl_pipeline *ppl;
20893af36706SVinod Koul 	struct skl_pipe *pipe;
20903af36706SVinod Koul 	struct skl_pipe_params *params;
20913af36706SVinod Koul 
20923af36706SVinod Koul 	list_for_each_entry(ppl, &skl->ppl_list, node) {
20936277e832SShreyas NC 		if (ppl->pipe->ppl_id == tkn_elem->value) {
20946277e832SShreyas NC 			mconfig->pipe = ppl->pipe;
2095081dc8abSGuneshwor Singh 			return -EEXIST;
20966277e832SShreyas NC 		}
20973af36706SVinod Koul 	}
20983af36706SVinod Koul 
20993af36706SVinod Koul 	ppl = devm_kzalloc(dev, sizeof(*ppl), GFP_KERNEL);
21003af36706SVinod Koul 	if (!ppl)
21016277e832SShreyas NC 		return -ENOMEM;
21023af36706SVinod Koul 
21033af36706SVinod Koul 	pipe = devm_kzalloc(dev, sizeof(*pipe), GFP_KERNEL);
21043af36706SVinod Koul 	if (!pipe)
21056277e832SShreyas NC 		return -ENOMEM;
21063af36706SVinod Koul 
21073af36706SVinod Koul 	params = devm_kzalloc(dev, sizeof(*params), GFP_KERNEL);
21083af36706SVinod Koul 	if (!params)
21096277e832SShreyas NC 		return -ENOMEM;
21103af36706SVinod Koul 
21113af36706SVinod Koul 	pipe->p_params = params;
21126277e832SShreyas NC 	pipe->ppl_id = tkn_elem->value;
21133af36706SVinod Koul 	INIT_LIST_HEAD(&pipe->w_list);
21143af36706SVinod Koul 
21153af36706SVinod Koul 	ppl->pipe = pipe;
21163af36706SVinod Koul 	list_add(&ppl->node, &skl->ppl_list);
21173af36706SVinod Koul 
21186277e832SShreyas NC 	mconfig->pipe = pipe;
21196277e832SShreyas NC 	mconfig->pipe->state = SKL_PIPE_INVALID;
21206277e832SShreyas NC 
21216277e832SShreyas NC 	return 0;
21223af36706SVinod Koul }
21233af36706SVinod Koul 
212422ebd666SSriram Periyasamy static int skl_tplg_get_uuid(struct device *dev, u8 *guid,
212522ebd666SSriram Periyasamy 	      struct snd_soc_tplg_vendor_uuid_elem *uuid_tkn)
21266277e832SShreyas NC {
212722ebd666SSriram Periyasamy 	if (uuid_tkn->token == SKL_TKN_UUID) {
212822ebd666SSriram Periyasamy 		memcpy(guid, &uuid_tkn->uuid, 16);
212922ebd666SSriram Periyasamy 		return 0;
213022ebd666SSriram Periyasamy 	}
213122ebd666SSriram Periyasamy 
213222ebd666SSriram Periyasamy 	dev_err(dev, "Not an UUID token %d\n", uuid_tkn->token);
213322ebd666SSriram Periyasamy 
213422ebd666SSriram Periyasamy 	return -EINVAL;
213522ebd666SSriram Periyasamy }
213622ebd666SSriram Periyasamy 
213722ebd666SSriram Periyasamy static int skl_tplg_fill_pin(struct device *dev,
213822ebd666SSriram Periyasamy 			struct snd_soc_tplg_vendor_value_elem *tkn_elem,
213922ebd666SSriram Periyasamy 			struct skl_module_pin *m_pin,
214022ebd666SSriram Periyasamy 			int pin_index)
214122ebd666SSriram Periyasamy {
2142d9561474SSriram Periyasamy 	int ret;
2143d9561474SSriram Periyasamy 
214422ebd666SSriram Periyasamy 	switch (tkn_elem->token) {
21456277e832SShreyas NC 	case SKL_TKN_U32_PIN_MOD_ID:
214622ebd666SSriram Periyasamy 		m_pin[pin_index].id.module_id = tkn_elem->value;
21476277e832SShreyas NC 		break;
21486277e832SShreyas NC 
21496277e832SShreyas NC 	case SKL_TKN_U32_PIN_INST_ID:
215022ebd666SSriram Periyasamy 		m_pin[pin_index].id.instance_id = tkn_elem->value;
21516277e832SShreyas NC 		break;
21526277e832SShreyas NC 
2153d9561474SSriram Periyasamy 	case SKL_TKN_UUID:
2154d9561474SSriram Periyasamy 		ret = skl_tplg_get_uuid(dev, m_pin[pin_index].id.mod_uuid.b,
2155d9561474SSriram Periyasamy 			(struct snd_soc_tplg_vendor_uuid_elem *)tkn_elem);
2156d9561474SSriram Periyasamy 		if (ret < 0)
2157d9561474SSriram Periyasamy 			return ret;
2158d9561474SSriram Periyasamy 
21596277e832SShreyas NC 		break;
21606277e832SShreyas NC 
21616277e832SShreyas NC 	default:
216222ebd666SSriram Periyasamy 		dev_err(dev, "%d Not a pin token\n", tkn_elem->token);
21636277e832SShreyas NC 		return -EINVAL;
21646277e832SShreyas NC 	}
21656277e832SShreyas NC 
21666277e832SShreyas NC 	return 0;
21676277e832SShreyas NC }
21686277e832SShreyas NC 
21696277e832SShreyas NC /*
21706277e832SShreyas NC  * Parse for pin config specific tokens to fill up the
21716277e832SShreyas NC  * module private data
21726277e832SShreyas NC  */
21736277e832SShreyas NC static int skl_tplg_fill_pins_info(struct device *dev,
21746277e832SShreyas NC 		struct skl_module_cfg *mconfig,
21756277e832SShreyas NC 		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
21766277e832SShreyas NC 		int dir, int pin_count)
21776277e832SShreyas NC {
21786277e832SShreyas NC 	int ret;
21796277e832SShreyas NC 	struct skl_module_pin *m_pin;
21806277e832SShreyas NC 
21816277e832SShreyas NC 	switch (dir) {
21826277e832SShreyas NC 	case SKL_DIR_IN:
21836277e832SShreyas NC 		m_pin = mconfig->m_in_pin;
21846277e832SShreyas NC 		break;
21856277e832SShreyas NC 
21866277e832SShreyas NC 	case SKL_DIR_OUT:
21876277e832SShreyas NC 		m_pin = mconfig->m_out_pin;
21886277e832SShreyas NC 		break;
21896277e832SShreyas NC 
21906277e832SShreyas NC 	default:
2191ecd286a9SColin Ian King 		dev_err(dev, "Invalid direction value\n");
21926277e832SShreyas NC 		return -EINVAL;
21936277e832SShreyas NC 	}
21946277e832SShreyas NC 
219522ebd666SSriram Periyasamy 	ret = skl_tplg_fill_pin(dev, tkn_elem, m_pin, pin_count);
21966277e832SShreyas NC 	if (ret < 0)
21976277e832SShreyas NC 		return ret;
21986277e832SShreyas NC 
21996277e832SShreyas NC 	m_pin[pin_count].in_use = false;
22006277e832SShreyas NC 	m_pin[pin_count].pin_state = SKL_PIN_UNBIND;
22016277e832SShreyas NC 
22026277e832SShreyas NC 	return 0;
22036277e832SShreyas NC }
22046277e832SShreyas NC 
22056277e832SShreyas NC /*
22066277e832SShreyas NC  * Fill up input/output module config format based
22076277e832SShreyas NC  * on the direction
22086277e832SShreyas NC  */
22096277e832SShreyas NC static int skl_tplg_fill_fmt(struct device *dev,
2210ca312fdaSShreyas NC 		struct skl_module_fmt *dst_fmt,
2211ca312fdaSShreyas NC 		u32 tkn, u32 value)
22126277e832SShreyas NC {
22136277e832SShreyas NC 	switch (tkn) {
22146277e832SShreyas NC 	case SKL_TKN_U32_FMT_CH:
22156277e832SShreyas NC 		dst_fmt->channels  = value;
22166277e832SShreyas NC 		break;
22176277e832SShreyas NC 
22186277e832SShreyas NC 	case SKL_TKN_U32_FMT_FREQ:
22196277e832SShreyas NC 		dst_fmt->s_freq = value;
22206277e832SShreyas NC 		break;
22216277e832SShreyas NC 
22226277e832SShreyas NC 	case SKL_TKN_U32_FMT_BIT_DEPTH:
22236277e832SShreyas NC 		dst_fmt->bit_depth = value;
22246277e832SShreyas NC 		break;
22256277e832SShreyas NC 
22266277e832SShreyas NC 	case SKL_TKN_U32_FMT_SAMPLE_SIZE:
22276277e832SShreyas NC 		dst_fmt->valid_bit_depth = value;
22286277e832SShreyas NC 		break;
22296277e832SShreyas NC 
22306277e832SShreyas NC 	case SKL_TKN_U32_FMT_CH_CONFIG:
22316277e832SShreyas NC 		dst_fmt->ch_cfg = value;
22326277e832SShreyas NC 		break;
22336277e832SShreyas NC 
22346277e832SShreyas NC 	case SKL_TKN_U32_FMT_INTERLEAVE:
22356277e832SShreyas NC 		dst_fmt->interleaving_style = value;
22366277e832SShreyas NC 		break;
22376277e832SShreyas NC 
22386277e832SShreyas NC 	case SKL_TKN_U32_FMT_SAMPLE_TYPE:
22396277e832SShreyas NC 		dst_fmt->sample_type = value;
22406277e832SShreyas NC 		break;
22416277e832SShreyas NC 
22426277e832SShreyas NC 	case SKL_TKN_U32_FMT_CH_MAP:
22436277e832SShreyas NC 		dst_fmt->ch_map = value;
22446277e832SShreyas NC 		break;
22456277e832SShreyas NC 
22466277e832SShreyas NC 	default:
2247ecd286a9SColin Ian King 		dev_err(dev, "Invalid token %d\n", tkn);
22486277e832SShreyas NC 		return -EINVAL;
22496277e832SShreyas NC 	}
22506277e832SShreyas NC 
22516277e832SShreyas NC 	return 0;
22526277e832SShreyas NC }
22536277e832SShreyas NC 
2254ca312fdaSShreyas NC static int skl_tplg_widget_fill_fmt(struct device *dev,
2255f6fa56e2SRamesh Babu 		struct skl_module_iface *fmt,
2256ca312fdaSShreyas NC 		u32 tkn, u32 val, u32 dir, int fmt_idx)
2257ca312fdaSShreyas NC {
2258ca312fdaSShreyas NC 	struct skl_module_fmt *dst_fmt;
2259ca312fdaSShreyas NC 
2260f6fa56e2SRamesh Babu 	if (!fmt)
2261f6fa56e2SRamesh Babu 		return -EINVAL;
2262f6fa56e2SRamesh Babu 
2263ca312fdaSShreyas NC 	switch (dir) {
2264ca312fdaSShreyas NC 	case SKL_DIR_IN:
2265f6fa56e2SRamesh Babu 		dst_fmt = &fmt->inputs[fmt_idx].fmt;
2266ca312fdaSShreyas NC 		break;
2267ca312fdaSShreyas NC 
2268ca312fdaSShreyas NC 	case SKL_DIR_OUT:
2269f6fa56e2SRamesh Babu 		dst_fmt = &fmt->outputs[fmt_idx].fmt;
2270ca312fdaSShreyas NC 		break;
2271ca312fdaSShreyas NC 
2272ca312fdaSShreyas NC 	default:
2273ca312fdaSShreyas NC 		dev_err(dev, "Invalid direction: %d\n", dir);
2274ca312fdaSShreyas NC 		return -EINVAL;
2275ca312fdaSShreyas NC 	}
2276ca312fdaSShreyas NC 
2277ca312fdaSShreyas NC 	return skl_tplg_fill_fmt(dev, dst_fmt, tkn, val);
2278ca312fdaSShreyas NC }
2279ca312fdaSShreyas NC 
22806277e832SShreyas NC static void skl_tplg_fill_pin_dynamic_val(
22816277e832SShreyas NC 		struct skl_module_pin *mpin, u32 pin_count, u32 value)
22824cd9899fSHardik T Shah {
22834cd9899fSHardik T Shah 	int i;
22844cd9899fSHardik T Shah 
22856277e832SShreyas NC 	for (i = 0; i < pin_count; i++)
22866277e832SShreyas NC 		mpin[i].is_dynamic = value;
22874cd9899fSHardik T Shah }
22886277e832SShreyas NC 
22896277e832SShreyas NC /*
2290db6ed55dSShreyas NC  * Resource table in the manifest has pin specific resources
2291db6ed55dSShreyas NC  * like pin and pin buffer size
2292db6ed55dSShreyas NC  */
2293db6ed55dSShreyas NC static int skl_tplg_manifest_pin_res_tkn(struct device *dev,
2294db6ed55dSShreyas NC 		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2295db6ed55dSShreyas NC 		struct skl_module_res *res, int pin_idx, int dir)
2296db6ed55dSShreyas NC {
2297db6ed55dSShreyas NC 	struct skl_module_pin_resources *m_pin;
2298db6ed55dSShreyas NC 
2299db6ed55dSShreyas NC 	switch (dir) {
2300db6ed55dSShreyas NC 	case SKL_DIR_IN:
2301db6ed55dSShreyas NC 		m_pin = &res->input[pin_idx];
2302db6ed55dSShreyas NC 		break;
2303db6ed55dSShreyas NC 
2304db6ed55dSShreyas NC 	case SKL_DIR_OUT:
2305db6ed55dSShreyas NC 		m_pin = &res->output[pin_idx];
2306db6ed55dSShreyas NC 		break;
2307db6ed55dSShreyas NC 
2308db6ed55dSShreyas NC 	default:
2309db6ed55dSShreyas NC 		dev_err(dev, "Invalid pin direction: %d\n", dir);
2310db6ed55dSShreyas NC 		return -EINVAL;
2311db6ed55dSShreyas NC 	}
2312db6ed55dSShreyas NC 
2313db6ed55dSShreyas NC 	switch (tkn_elem->token) {
2314db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_RES_PIN_ID:
2315db6ed55dSShreyas NC 		m_pin->pin_index = tkn_elem->value;
2316db6ed55dSShreyas NC 		break;
2317db6ed55dSShreyas NC 
2318db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_PIN_BUF:
2319db6ed55dSShreyas NC 		m_pin->buf_size = tkn_elem->value;
2320db6ed55dSShreyas NC 		break;
2321db6ed55dSShreyas NC 
2322db6ed55dSShreyas NC 	default:
2323db6ed55dSShreyas NC 		dev_err(dev, "Invalid token: %d\n", tkn_elem->token);
2324db6ed55dSShreyas NC 		return -EINVAL;
2325db6ed55dSShreyas NC 	}
2326db6ed55dSShreyas NC 
2327db6ed55dSShreyas NC 	return 0;
2328db6ed55dSShreyas NC }
2329db6ed55dSShreyas NC 
2330db6ed55dSShreyas NC /*
2331db6ed55dSShreyas NC  * Fill module specific resources from the manifest's resource
2332db6ed55dSShreyas NC  * table like CPS, DMA size, mem_pages.
2333db6ed55dSShreyas NC  */
2334db6ed55dSShreyas NC static int skl_tplg_fill_res_tkn(struct device *dev,
2335db6ed55dSShreyas NC 		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2336db6ed55dSShreyas NC 		struct skl_module_res *res,
2337db6ed55dSShreyas NC 		int pin_idx, int dir)
2338db6ed55dSShreyas NC {
2339db6ed55dSShreyas NC 	int ret, tkn_count = 0;
2340db6ed55dSShreyas NC 
2341db6ed55dSShreyas NC 	if (!res)
2342db6ed55dSShreyas NC 		return -EINVAL;
2343db6ed55dSShreyas NC 
2344db6ed55dSShreyas NC 	switch (tkn_elem->token) {
2345db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_CPS:
2346db6ed55dSShreyas NC 		res->cps = tkn_elem->value;
2347db6ed55dSShreyas NC 		break;
2348db6ed55dSShreyas NC 
2349db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_DMA_SIZE:
2350db6ed55dSShreyas NC 		res->dma_buffer_size = tkn_elem->value;
2351db6ed55dSShreyas NC 		break;
2352db6ed55dSShreyas NC 
2353db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_CPC:
2354db6ed55dSShreyas NC 		res->cpc = tkn_elem->value;
2355db6ed55dSShreyas NC 		break;
2356db6ed55dSShreyas NC 
2357db6ed55dSShreyas NC 	case SKL_TKN_U32_MEM_PAGES:
2358db6ed55dSShreyas NC 		res->is_pages = tkn_elem->value;
2359db6ed55dSShreyas NC 		break;
2360db6ed55dSShreyas NC 
2361db6ed55dSShreyas NC 	case SKL_TKN_U32_OBS:
2362db6ed55dSShreyas NC 		res->obs = tkn_elem->value;
2363db6ed55dSShreyas NC 		break;
2364db6ed55dSShreyas NC 
2365db6ed55dSShreyas NC 	case SKL_TKN_U32_IBS:
2366db6ed55dSShreyas NC 		res->ibs = tkn_elem->value;
2367db6ed55dSShreyas NC 		break;
2368db6ed55dSShreyas NC 
2369f6fa56e2SRamesh Babu 	case SKL_TKN_U32_MAX_MCPS:
2370f6fa56e2SRamesh Babu 		res->cps = tkn_elem->value;
2371f6fa56e2SRamesh Babu 		break;
2372f6fa56e2SRamesh Babu 
2373db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_RES_PIN_ID:
2374db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_PIN_BUF:
2375db6ed55dSShreyas NC 		ret = skl_tplg_manifest_pin_res_tkn(dev, tkn_elem, res,
2376db6ed55dSShreyas NC 						    pin_idx, dir);
2377db6ed55dSShreyas NC 		if (ret < 0)
2378db6ed55dSShreyas NC 			return ret;
2379db6ed55dSShreyas NC 		break;
2380db6ed55dSShreyas NC 
2381db6ed55dSShreyas NC 	default:
2382db6ed55dSShreyas NC 		dev_err(dev, "Not a res type token: %d", tkn_elem->token);
2383db6ed55dSShreyas NC 		return -EINVAL;
2384db6ed55dSShreyas NC 
2385db6ed55dSShreyas NC 	}
2386db6ed55dSShreyas NC 	tkn_count++;
2387db6ed55dSShreyas NC 
2388db6ed55dSShreyas NC 	return tkn_count;
2389db6ed55dSShreyas NC }
2390db6ed55dSShreyas NC 
2391db6ed55dSShreyas NC /*
23926277e832SShreyas NC  * Parse tokens to fill up the module private data
23936277e832SShreyas NC  */
23946277e832SShreyas NC static int skl_tplg_get_token(struct device *dev,
23956277e832SShreyas NC 		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
23966277e832SShreyas NC 		struct skl *skl, struct skl_module_cfg *mconfig)
23976277e832SShreyas NC {
23986277e832SShreyas NC 	int tkn_count = 0;
23996277e832SShreyas NC 	int ret;
24006277e832SShreyas NC 	static int is_pipe_exists;
2401f6fa56e2SRamesh Babu 	static int pin_index, dir, conf_idx;
2402f6fa56e2SRamesh Babu 	struct skl_module_iface *iface = NULL;
2403f6fa56e2SRamesh Babu 	struct skl_module_res *res = NULL;
2404f6fa56e2SRamesh Babu 	int res_idx = mconfig->res_idx;
2405f6fa56e2SRamesh Babu 	int fmt_idx = mconfig->fmt_idx;
2406f6fa56e2SRamesh Babu 
2407f6fa56e2SRamesh Babu 	/*
2408f6fa56e2SRamesh Babu 	 * If the manifest structure contains no modules, fill all
2409f6fa56e2SRamesh Babu 	 * the module data to 0th index.
2410f6fa56e2SRamesh Babu 	 * res_idx and fmt_idx are default set to 0.
2411f6fa56e2SRamesh Babu 	 */
2412f6fa56e2SRamesh Babu 	if (skl->nr_modules == 0) {
2413f6fa56e2SRamesh Babu 		res = &mconfig->module->resources[res_idx];
2414f6fa56e2SRamesh Babu 		iface = &mconfig->module->formats[fmt_idx];
2415f6fa56e2SRamesh Babu 	}
24166277e832SShreyas NC 
24176277e832SShreyas NC 	if (tkn_elem->token > SKL_TKN_MAX)
24186277e832SShreyas NC 		return -EINVAL;
24196277e832SShreyas NC 
24206277e832SShreyas NC 	switch (tkn_elem->token) {
24216277e832SShreyas NC 	case SKL_TKN_U8_IN_QUEUE_COUNT:
2422f6fa56e2SRamesh Babu 		mconfig->module->max_input_pins = tkn_elem->value;
24236277e832SShreyas NC 		break;
24246277e832SShreyas NC 
24256277e832SShreyas NC 	case SKL_TKN_U8_OUT_QUEUE_COUNT:
2426f6fa56e2SRamesh Babu 		mconfig->module->max_output_pins = tkn_elem->value;
24276277e832SShreyas NC 		break;
24286277e832SShreyas NC 
24296277e832SShreyas NC 	case SKL_TKN_U8_DYN_IN_PIN:
24306277e832SShreyas NC 		if (!mconfig->m_in_pin)
2431a86854d0SKees Cook 			mconfig->m_in_pin =
2432a86854d0SKees Cook 				devm_kcalloc(dev, MAX_IN_QUEUE,
2433a86854d0SKees Cook 					     sizeof(*mconfig->m_in_pin),
2434a86854d0SKees Cook 					     GFP_KERNEL);
2435f6fa56e2SRamesh Babu 		if (!mconfig->m_in_pin)
24366277e832SShreyas NC 			return -ENOMEM;
24376277e832SShreyas NC 
2438f6fa56e2SRamesh Babu 		skl_tplg_fill_pin_dynamic_val(mconfig->m_in_pin, MAX_IN_QUEUE,
2439f6fa56e2SRamesh Babu 					      tkn_elem->value);
24406277e832SShreyas NC 		break;
24416277e832SShreyas NC 
24426277e832SShreyas NC 	case SKL_TKN_U8_DYN_OUT_PIN:
24436277e832SShreyas NC 		if (!mconfig->m_out_pin)
2444a86854d0SKees Cook 			mconfig->m_out_pin =
2445a86854d0SKees Cook 				devm_kcalloc(dev, MAX_IN_QUEUE,
2446a86854d0SKees Cook 					     sizeof(*mconfig->m_in_pin),
2447a86854d0SKees Cook 					     GFP_KERNEL);
2448f6fa56e2SRamesh Babu 		if (!mconfig->m_out_pin)
24496277e832SShreyas NC 			return -ENOMEM;
24506277e832SShreyas NC 
2451f6fa56e2SRamesh Babu 		skl_tplg_fill_pin_dynamic_val(mconfig->m_out_pin, MAX_OUT_QUEUE,
2452f6fa56e2SRamesh Babu 					      tkn_elem->value);
24536277e832SShreyas NC 		break;
24546277e832SShreyas NC 
24556277e832SShreyas NC 	case SKL_TKN_U8_TIME_SLOT:
24566277e832SShreyas NC 		mconfig->time_slot = tkn_elem->value;
24576277e832SShreyas NC 		break;
24586277e832SShreyas NC 
24596277e832SShreyas NC 	case SKL_TKN_U8_CORE_ID:
24606277e832SShreyas NC 		mconfig->core_id = tkn_elem->value;
24616277e832SShreyas NC 
24626277e832SShreyas NC 	case SKL_TKN_U8_MOD_TYPE:
24636277e832SShreyas NC 		mconfig->m_type = tkn_elem->value;
24646277e832SShreyas NC 		break;
24656277e832SShreyas NC 
24666277e832SShreyas NC 	case SKL_TKN_U8_DEV_TYPE:
24676277e832SShreyas NC 		mconfig->dev_type = tkn_elem->value;
24686277e832SShreyas NC 		break;
24696277e832SShreyas NC 
24706277e832SShreyas NC 	case SKL_TKN_U8_HW_CONN_TYPE:
24716277e832SShreyas NC 		mconfig->hw_conn_type = tkn_elem->value;
24726277e832SShreyas NC 		break;
24736277e832SShreyas NC 
24746277e832SShreyas NC 	case SKL_TKN_U16_MOD_INST_ID:
24756277e832SShreyas NC 		mconfig->id.instance_id =
24766277e832SShreyas NC 		tkn_elem->value;
24776277e832SShreyas NC 		break;
24786277e832SShreyas NC 
24796277e832SShreyas NC 	case SKL_TKN_U32_MEM_PAGES:
24806277e832SShreyas NC 	case SKL_TKN_U32_MAX_MCPS:
24816277e832SShreyas NC 	case SKL_TKN_U32_OBS:
24826277e832SShreyas NC 	case SKL_TKN_U32_IBS:
24832b79b15cSColin Ian King 		ret = skl_tplg_fill_res_tkn(dev, tkn_elem, res, pin_index, dir);
2484f6fa56e2SRamesh Babu 		if (ret < 0)
2485f6fa56e2SRamesh Babu 			return ret;
2486f6fa56e2SRamesh Babu 
24876277e832SShreyas NC 		break;
24886277e832SShreyas NC 
24896277e832SShreyas NC 	case SKL_TKN_U32_VBUS_ID:
24906277e832SShreyas NC 		mconfig->vbus_id = tkn_elem->value;
24916277e832SShreyas NC 		break;
24926277e832SShreyas NC 
24936277e832SShreyas NC 	case SKL_TKN_U32_PARAMS_FIXUP:
24946277e832SShreyas NC 		mconfig->params_fixup = tkn_elem->value;
24956277e832SShreyas NC 		break;
24966277e832SShreyas NC 
24976277e832SShreyas NC 	case SKL_TKN_U32_CONVERTER:
24986277e832SShreyas NC 		mconfig->converter = tkn_elem->value;
24996277e832SShreyas NC 		break;
25006277e832SShreyas NC 
2501c0116be3SSubhransu S. Prusty 	case SKL_TKN_U32_D0I3_CAPS:
25026bd9dcf3SVinod Koul 		mconfig->d0i3_caps = tkn_elem->value;
25036bd9dcf3SVinod Koul 		break;
25046bd9dcf3SVinod Koul 
25056277e832SShreyas NC 	case SKL_TKN_U32_PIPE_ID:
25066277e832SShreyas NC 		ret = skl_tplg_add_pipe(dev,
25076277e832SShreyas NC 				mconfig, skl, tkn_elem);
25086277e832SShreyas NC 
2509081dc8abSGuneshwor Singh 		if (ret < 0) {
2510081dc8abSGuneshwor Singh 			if (ret == -EEXIST) {
25116277e832SShreyas NC 				is_pipe_exists = 1;
2512081dc8abSGuneshwor Singh 				break;
2513081dc8abSGuneshwor Singh 			}
2514081dc8abSGuneshwor Singh 			return is_pipe_exists;
2515081dc8abSGuneshwor Singh 		}
25166277e832SShreyas NC 
25176277e832SShreyas NC 		break;
25186277e832SShreyas NC 
2519f6fa56e2SRamesh Babu 	case SKL_TKN_U32_PIPE_CONFIG_ID:
2520f6fa56e2SRamesh Babu 		conf_idx = tkn_elem->value;
2521f6fa56e2SRamesh Babu 		break;
2522f6fa56e2SRamesh Babu 
25236277e832SShreyas NC 	case SKL_TKN_U32_PIPE_CONN_TYPE:
25246277e832SShreyas NC 	case SKL_TKN_U32_PIPE_PRIORITY:
25256277e832SShreyas NC 	case SKL_TKN_U32_PIPE_MEM_PGS:
25268a0cb236SVinod Koul 	case SKL_TKN_U32_PMODE:
2527f6fa56e2SRamesh Babu 	case SKL_TKN_U32_PIPE_DIRECTION:
2528f6fa56e2SRamesh Babu 	case SKL_TKN_U32_NUM_CONFIGS:
25296277e832SShreyas NC 		if (is_pipe_exists) {
25306277e832SShreyas NC 			ret = skl_tplg_fill_pipe_tkn(dev, mconfig->pipe,
25316277e832SShreyas NC 					tkn_elem->token, tkn_elem->value);
25326277e832SShreyas NC 			if (ret < 0)
25336277e832SShreyas NC 				return ret;
25346277e832SShreyas NC 		}
25356277e832SShreyas NC 
25366277e832SShreyas NC 		break;
25376277e832SShreyas NC 
2538f6fa56e2SRamesh Babu 	case SKL_TKN_U32_PATH_MEM_PGS:
2539f6fa56e2SRamesh Babu 	case SKL_TKN_U32_CFG_FREQ:
2540f6fa56e2SRamesh Babu 	case SKL_TKN_U8_CFG_CHAN:
2541f6fa56e2SRamesh Babu 	case SKL_TKN_U8_CFG_BPS:
2542f6fa56e2SRamesh Babu 		if (mconfig->pipe->nr_cfgs) {
2543f6fa56e2SRamesh Babu 			ret = skl_tplg_fill_pipe_cfg(dev, mconfig->pipe,
2544f6fa56e2SRamesh Babu 					tkn_elem->token, tkn_elem->value,
2545f6fa56e2SRamesh Babu 					conf_idx, dir);
2546f6fa56e2SRamesh Babu 			if (ret < 0)
2547f6fa56e2SRamesh Babu 				return ret;
2548f6fa56e2SRamesh Babu 		}
2549f6fa56e2SRamesh Babu 		break;
2550f6fa56e2SRamesh Babu 
2551f6fa56e2SRamesh Babu 	case SKL_TKN_CFG_MOD_RES_ID:
2552f6fa56e2SRamesh Babu 		mconfig->mod_cfg[conf_idx].res_idx = tkn_elem->value;
2553f6fa56e2SRamesh Babu 		break;
2554f6fa56e2SRamesh Babu 
2555f6fa56e2SRamesh Babu 	case SKL_TKN_CFG_MOD_FMT_ID:
2556f6fa56e2SRamesh Babu 		mconfig->mod_cfg[conf_idx].fmt_idx = tkn_elem->value;
2557f6fa56e2SRamesh Babu 		break;
2558f6fa56e2SRamesh Babu 
25596277e832SShreyas NC 	/*
25606277e832SShreyas NC 	 * SKL_TKN_U32_DIR_PIN_COUNT token has the value for both
25616277e832SShreyas NC 	 * direction and the pin count. The first four bits represent
25626277e832SShreyas NC 	 * direction and next four the pin count.
25636277e832SShreyas NC 	 */
25646277e832SShreyas NC 	case SKL_TKN_U32_DIR_PIN_COUNT:
25656277e832SShreyas NC 		dir = tkn_elem->value & SKL_IN_DIR_BIT_MASK;
25666277e832SShreyas NC 		pin_index = (tkn_elem->value &
25676277e832SShreyas NC 			SKL_PIN_COUNT_MASK) >> 4;
25686277e832SShreyas NC 
25696277e832SShreyas NC 		break;
25706277e832SShreyas NC 
25716277e832SShreyas NC 	case SKL_TKN_U32_FMT_CH:
25726277e832SShreyas NC 	case SKL_TKN_U32_FMT_FREQ:
25736277e832SShreyas NC 	case SKL_TKN_U32_FMT_BIT_DEPTH:
25746277e832SShreyas NC 	case SKL_TKN_U32_FMT_SAMPLE_SIZE:
25756277e832SShreyas NC 	case SKL_TKN_U32_FMT_CH_CONFIG:
25766277e832SShreyas NC 	case SKL_TKN_U32_FMT_INTERLEAVE:
25776277e832SShreyas NC 	case SKL_TKN_U32_FMT_SAMPLE_TYPE:
25786277e832SShreyas NC 	case SKL_TKN_U32_FMT_CH_MAP:
2579f6fa56e2SRamesh Babu 		ret = skl_tplg_widget_fill_fmt(dev, iface, tkn_elem->token,
25806277e832SShreyas NC 				tkn_elem->value, dir, pin_index);
25816277e832SShreyas NC 
25826277e832SShreyas NC 		if (ret < 0)
25836277e832SShreyas NC 			return ret;
25846277e832SShreyas NC 
25856277e832SShreyas NC 		break;
25866277e832SShreyas NC 
25876277e832SShreyas NC 	case SKL_TKN_U32_PIN_MOD_ID:
25886277e832SShreyas NC 	case SKL_TKN_U32_PIN_INST_ID:
2589d9561474SSriram Periyasamy 	case SKL_TKN_UUID:
25906277e832SShreyas NC 		ret = skl_tplg_fill_pins_info(dev,
25916277e832SShreyas NC 				mconfig, tkn_elem, dir,
25926277e832SShreyas NC 				pin_index);
25936277e832SShreyas NC 		if (ret < 0)
25946277e832SShreyas NC 			return ret;
25956277e832SShreyas NC 
25966277e832SShreyas NC 		break;
25976277e832SShreyas NC 
25986277e832SShreyas NC 	case SKL_TKN_U32_CAPS_SIZE:
25996277e832SShreyas NC 		mconfig->formats_config.caps_size =
26006277e832SShreyas NC 			tkn_elem->value;
26016277e832SShreyas NC 
26026277e832SShreyas NC 		break;
26036277e832SShreyas NC 
2604133e6e5cSShreyas NC 	case SKL_TKN_U32_CAPS_SET_PARAMS:
2605133e6e5cSShreyas NC 		mconfig->formats_config.set_params =
2606133e6e5cSShreyas NC 				tkn_elem->value;
2607133e6e5cSShreyas NC 		break;
2608133e6e5cSShreyas NC 
2609133e6e5cSShreyas NC 	case SKL_TKN_U32_CAPS_PARAMS_ID:
2610133e6e5cSShreyas NC 		mconfig->formats_config.param_id =
2611133e6e5cSShreyas NC 				tkn_elem->value;
2612133e6e5cSShreyas NC 		break;
2613133e6e5cSShreyas NC 
26146277e832SShreyas NC 	case SKL_TKN_U32_PROC_DOMAIN:
26156277e832SShreyas NC 		mconfig->domain =
26166277e832SShreyas NC 			tkn_elem->value;
26176277e832SShreyas NC 
26186277e832SShreyas NC 		break;
26196277e832SShreyas NC 
2620939df3adSRamesh Babu 	case SKL_TKN_U32_DMA_BUF_SIZE:
2621939df3adSRamesh Babu 		mconfig->dma_buffer_size = tkn_elem->value;
2622939df3adSRamesh Babu 		break;
2623939df3adSRamesh Babu 
26246277e832SShreyas NC 	case SKL_TKN_U8_IN_PIN_TYPE:
26256277e832SShreyas NC 	case SKL_TKN_U8_OUT_PIN_TYPE:
26266277e832SShreyas NC 	case SKL_TKN_U8_CONN_TYPE:
26276277e832SShreyas NC 		break;
26286277e832SShreyas NC 
26296277e832SShreyas NC 	default:
26306277e832SShreyas NC 		dev_err(dev, "Token %d not handled\n",
26316277e832SShreyas NC 				tkn_elem->token);
26326277e832SShreyas NC 		return -EINVAL;
26336277e832SShreyas NC 	}
26346277e832SShreyas NC 
26356277e832SShreyas NC 	tkn_count++;
26366277e832SShreyas NC 
26376277e832SShreyas NC 	return tkn_count;
26386277e832SShreyas NC }
26396277e832SShreyas NC 
26406277e832SShreyas NC /*
26416277e832SShreyas NC  * Parse the vendor array for specific tokens to construct
26426277e832SShreyas NC  * module private data
26436277e832SShreyas NC  */
26446277e832SShreyas NC static int skl_tplg_get_tokens(struct device *dev,
26456277e832SShreyas NC 		char *pvt_data,	struct skl *skl,
26466277e832SShreyas NC 		struct skl_module_cfg *mconfig, int block_size)
26476277e832SShreyas NC {
26486277e832SShreyas NC 	struct snd_soc_tplg_vendor_array *array;
26496277e832SShreyas NC 	struct snd_soc_tplg_vendor_value_elem *tkn_elem;
26506277e832SShreyas NC 	int tkn_count = 0, ret;
26516277e832SShreyas NC 	int off = 0, tuple_size = 0;
2652d9561474SSriram Periyasamy 	bool is_module_guid = true;
26536277e832SShreyas NC 
26546277e832SShreyas NC 	if (block_size <= 0)
26556277e832SShreyas NC 		return -EINVAL;
26566277e832SShreyas NC 
26576277e832SShreyas NC 	while (tuple_size < block_size) {
26586277e832SShreyas NC 		array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off);
26596277e832SShreyas NC 
26606277e832SShreyas NC 		off += array->size;
26616277e832SShreyas NC 
26626277e832SShreyas NC 		switch (array->type) {
26636277e832SShreyas NC 		case SND_SOC_TPLG_TUPLE_TYPE_STRING:
2664ecd286a9SColin Ian King 			dev_warn(dev, "no string tokens expected for skl tplg\n");
26656277e832SShreyas NC 			continue;
26666277e832SShreyas NC 
26676277e832SShreyas NC 		case SND_SOC_TPLG_TUPLE_TYPE_UUID:
2668d9561474SSriram Periyasamy 			if (is_module_guid) {
266922ebd666SSriram Periyasamy 				ret = skl_tplg_get_uuid(dev, mconfig->guid,
267022ebd666SSriram Periyasamy 							array->uuid);
2671d9561474SSriram Periyasamy 				is_module_guid = false;
2672d9561474SSriram Periyasamy 			} else {
2673d9561474SSriram Periyasamy 				ret = skl_tplg_get_token(dev, array->value, skl,
2674d9561474SSriram Periyasamy 							 mconfig);
2675d9561474SSriram Periyasamy 			}
2676d9561474SSriram Periyasamy 
26776277e832SShreyas NC 			if (ret < 0)
26786277e832SShreyas NC 				return ret;
26796277e832SShreyas NC 
26806277e832SShreyas NC 			tuple_size += sizeof(*array->uuid);
26816277e832SShreyas NC 
26826277e832SShreyas NC 			continue;
26836277e832SShreyas NC 
26846277e832SShreyas NC 		default:
26856277e832SShreyas NC 			tkn_elem = array->value;
26866277e832SShreyas NC 			tkn_count = 0;
26876277e832SShreyas NC 			break;
26886277e832SShreyas NC 		}
26896277e832SShreyas NC 
26906277e832SShreyas NC 		while (tkn_count <= (array->num_elems - 1)) {
26916277e832SShreyas NC 			ret = skl_tplg_get_token(dev, tkn_elem,
26926277e832SShreyas NC 					skl, mconfig);
26936277e832SShreyas NC 
26946277e832SShreyas NC 			if (ret < 0)
26956277e832SShreyas NC 				return ret;
26966277e832SShreyas NC 
26976277e832SShreyas NC 			tkn_count = tkn_count + ret;
26986277e832SShreyas NC 			tkn_elem++;
26996277e832SShreyas NC 		}
27006277e832SShreyas NC 
27016277e832SShreyas NC 		tuple_size += tkn_count * sizeof(*tkn_elem);
27026277e832SShreyas NC 	}
27036277e832SShreyas NC 
2704133e6e5cSShreyas NC 	return off;
27056277e832SShreyas NC }
27066277e832SShreyas NC 
27076277e832SShreyas NC /*
27086277e832SShreyas NC  * Every data block is preceded by a descriptor to read the number
27096277e832SShreyas NC  * of data blocks, they type of the block and it's size
27106277e832SShreyas NC  */
27116277e832SShreyas NC static int skl_tplg_get_desc_blocks(struct device *dev,
27126277e832SShreyas NC 		struct snd_soc_tplg_vendor_array *array)
27136277e832SShreyas NC {
27146277e832SShreyas NC 	struct snd_soc_tplg_vendor_value_elem *tkn_elem;
27156277e832SShreyas NC 
27166277e832SShreyas NC 	tkn_elem = array->value;
27176277e832SShreyas NC 
27186277e832SShreyas NC 	switch (tkn_elem->token) {
27196277e832SShreyas NC 	case SKL_TKN_U8_NUM_BLOCKS:
27206277e832SShreyas NC 	case SKL_TKN_U8_BLOCK_TYPE:
27216277e832SShreyas NC 	case SKL_TKN_U16_BLOCK_SIZE:
27226277e832SShreyas NC 		return tkn_elem->value;
27236277e832SShreyas NC 
27246277e832SShreyas NC 	default:
2725ecd286a9SColin Ian King 		dev_err(dev, "Invalid descriptor token %d\n", tkn_elem->token);
27266277e832SShreyas NC 		break;
27276277e832SShreyas NC 	}
27286277e832SShreyas NC 
27296277e832SShreyas NC 	return -EINVAL;
27306277e832SShreyas NC }
27316277e832SShreyas NC 
2732ac9391daSGuenter Roeck /* Functions to parse private data from configuration file format v4 */
2733ac9391daSGuenter Roeck 
2734ac9391daSGuenter Roeck /*
2735ac9391daSGuenter Roeck  * Add pipeline from topology binary into driver pipeline list
2736ac9391daSGuenter Roeck  *
2737ac9391daSGuenter Roeck  * If already added we return that instance
2738ac9391daSGuenter Roeck  * Otherwise we create a new instance and add into driver list
2739ac9391daSGuenter Roeck  */
2740ac9391daSGuenter Roeck static int skl_tplg_add_pipe_v4(struct device *dev,
2741ac9391daSGuenter Roeck 				struct skl_module_cfg *mconfig, struct skl *skl,
2742ac9391daSGuenter Roeck 				struct skl_dfw_v4_pipe *dfw_pipe)
2743ac9391daSGuenter Roeck {
2744ac9391daSGuenter Roeck 	struct skl_pipeline *ppl;
2745ac9391daSGuenter Roeck 	struct skl_pipe *pipe;
2746ac9391daSGuenter Roeck 	struct skl_pipe_params *params;
2747ac9391daSGuenter Roeck 
2748ac9391daSGuenter Roeck 	list_for_each_entry(ppl, &skl->ppl_list, node) {
2749ac9391daSGuenter Roeck 		if (ppl->pipe->ppl_id == dfw_pipe->pipe_id) {
2750ac9391daSGuenter Roeck 			mconfig->pipe = ppl->pipe;
2751ac9391daSGuenter Roeck 			return 0;
2752ac9391daSGuenter Roeck 		}
2753ac9391daSGuenter Roeck 	}
2754ac9391daSGuenter Roeck 
2755ac9391daSGuenter Roeck 	ppl = devm_kzalloc(dev, sizeof(*ppl), GFP_KERNEL);
2756ac9391daSGuenter Roeck 	if (!ppl)
2757ac9391daSGuenter Roeck 		return -ENOMEM;
2758ac9391daSGuenter Roeck 
2759ac9391daSGuenter Roeck 	pipe = devm_kzalloc(dev, sizeof(*pipe), GFP_KERNEL);
2760ac9391daSGuenter Roeck 	if (!pipe)
2761ac9391daSGuenter Roeck 		return -ENOMEM;
2762ac9391daSGuenter Roeck 
2763ac9391daSGuenter Roeck 	params = devm_kzalloc(dev, sizeof(*params), GFP_KERNEL);
2764ac9391daSGuenter Roeck 	if (!params)
2765ac9391daSGuenter Roeck 		return -ENOMEM;
2766ac9391daSGuenter Roeck 
2767ac9391daSGuenter Roeck 	pipe->ppl_id = dfw_pipe->pipe_id;
2768ac9391daSGuenter Roeck 	pipe->memory_pages = dfw_pipe->memory_pages;
2769ac9391daSGuenter Roeck 	pipe->pipe_priority = dfw_pipe->pipe_priority;
2770ac9391daSGuenter Roeck 	pipe->conn_type = dfw_pipe->conn_type;
2771ac9391daSGuenter Roeck 	pipe->state = SKL_PIPE_INVALID;
2772ac9391daSGuenter Roeck 	pipe->p_params = params;
2773ac9391daSGuenter Roeck 	INIT_LIST_HEAD(&pipe->w_list);
2774ac9391daSGuenter Roeck 
2775ac9391daSGuenter Roeck 	ppl->pipe = pipe;
2776ac9391daSGuenter Roeck 	list_add(&ppl->node, &skl->ppl_list);
2777ac9391daSGuenter Roeck 
2778ac9391daSGuenter Roeck 	mconfig->pipe = pipe;
2779ac9391daSGuenter Roeck 
2780ac9391daSGuenter Roeck 	return 0;
2781ac9391daSGuenter Roeck }
2782ac9391daSGuenter Roeck 
2783ac9391daSGuenter Roeck static void skl_fill_module_pin_info_v4(struct skl_dfw_v4_module_pin *dfw_pin,
2784ac9391daSGuenter Roeck 					struct skl_module_pin *m_pin,
2785ac9391daSGuenter Roeck 					bool is_dynamic, int max_pin)
2786ac9391daSGuenter Roeck {
2787ac9391daSGuenter Roeck 	int i;
2788ac9391daSGuenter Roeck 
2789ac9391daSGuenter Roeck 	for (i = 0; i < max_pin; i++) {
2790ac9391daSGuenter Roeck 		m_pin[i].id.module_id = dfw_pin[i].module_id;
2791ac9391daSGuenter Roeck 		m_pin[i].id.instance_id = dfw_pin[i].instance_id;
2792ac9391daSGuenter Roeck 		m_pin[i].in_use = false;
2793ac9391daSGuenter Roeck 		m_pin[i].is_dynamic = is_dynamic;
2794ac9391daSGuenter Roeck 		m_pin[i].pin_state = SKL_PIN_UNBIND;
2795ac9391daSGuenter Roeck 	}
2796ac9391daSGuenter Roeck }
2797ac9391daSGuenter Roeck 
2798ac9391daSGuenter Roeck static void skl_tplg_fill_fmt_v4(struct skl_module_pin_fmt *dst_fmt,
2799ac9391daSGuenter Roeck 				 struct skl_dfw_v4_module_fmt *src_fmt,
2800ac9391daSGuenter Roeck 				 int pins)
2801ac9391daSGuenter Roeck {
2802ac9391daSGuenter Roeck 	int i;
2803ac9391daSGuenter Roeck 
2804ac9391daSGuenter Roeck 	for (i = 0; i < pins; i++) {
2805ac9391daSGuenter Roeck 		dst_fmt[i].fmt.channels  = src_fmt[i].channels;
2806ac9391daSGuenter Roeck 		dst_fmt[i].fmt.s_freq = src_fmt[i].freq;
2807ac9391daSGuenter Roeck 		dst_fmt[i].fmt.bit_depth = src_fmt[i].bit_depth;
2808ac9391daSGuenter Roeck 		dst_fmt[i].fmt.valid_bit_depth = src_fmt[i].valid_bit_depth;
2809ac9391daSGuenter Roeck 		dst_fmt[i].fmt.ch_cfg = src_fmt[i].ch_cfg;
2810ac9391daSGuenter Roeck 		dst_fmt[i].fmt.ch_map = src_fmt[i].ch_map;
2811ac9391daSGuenter Roeck 		dst_fmt[i].fmt.interleaving_style =
2812ac9391daSGuenter Roeck 						src_fmt[i].interleaving_style;
2813ac9391daSGuenter Roeck 		dst_fmt[i].fmt.sample_type = src_fmt[i].sample_type;
2814ac9391daSGuenter Roeck 	}
2815ac9391daSGuenter Roeck }
2816ac9391daSGuenter Roeck 
2817ac9391daSGuenter Roeck static int skl_tplg_get_pvt_data_v4(struct snd_soc_tplg_dapm_widget *tplg_w,
2818ac9391daSGuenter Roeck 				    struct skl *skl, struct device *dev,
2819ac9391daSGuenter Roeck 				    struct skl_module_cfg *mconfig)
2820ac9391daSGuenter Roeck {
2821ac9391daSGuenter Roeck 	struct skl_dfw_v4_module *dfw =
2822ac9391daSGuenter Roeck 				(struct skl_dfw_v4_module *)tplg_w->priv.data;
2823ac9391daSGuenter Roeck 	int ret;
2824ac9391daSGuenter Roeck 
2825ac9391daSGuenter Roeck 	dev_dbg(dev, "Parsing Skylake v4 widget topology data\n");
2826ac9391daSGuenter Roeck 
2827ac9391daSGuenter Roeck 	ret = guid_parse(dfw->uuid, (guid_t *)mconfig->guid);
2828ac9391daSGuenter Roeck 	if (ret)
2829ac9391daSGuenter Roeck 		return ret;
2830ac9391daSGuenter Roeck 	mconfig->id.module_id = -1;
2831ac9391daSGuenter Roeck 	mconfig->id.instance_id = dfw->instance_id;
2832ac9391daSGuenter Roeck 	mconfig->module->resources[0].cps = dfw->max_mcps;
2833ac9391daSGuenter Roeck 	mconfig->module->resources[0].ibs = dfw->ibs;
2834ac9391daSGuenter Roeck 	mconfig->module->resources[0].obs = dfw->obs;
2835ac9391daSGuenter Roeck 	mconfig->core_id = dfw->core_id;
2836ac9391daSGuenter Roeck 	mconfig->module->max_input_pins = dfw->max_in_queue;
2837ac9391daSGuenter Roeck 	mconfig->module->max_output_pins = dfw->max_out_queue;
2838ac9391daSGuenter Roeck 	mconfig->module->loadable = dfw->is_loadable;
2839ac9391daSGuenter Roeck 	skl_tplg_fill_fmt_v4(mconfig->module->formats[0].inputs, dfw->in_fmt,
2840ac9391daSGuenter Roeck 			     MAX_IN_QUEUE);
2841ac9391daSGuenter Roeck 	skl_tplg_fill_fmt_v4(mconfig->module->formats[0].outputs, dfw->out_fmt,
2842ac9391daSGuenter Roeck 			     MAX_OUT_QUEUE);
2843ac9391daSGuenter Roeck 
2844ac9391daSGuenter Roeck 	mconfig->params_fixup = dfw->params_fixup;
2845ac9391daSGuenter Roeck 	mconfig->converter = dfw->converter;
2846ac9391daSGuenter Roeck 	mconfig->m_type = dfw->module_type;
2847ac9391daSGuenter Roeck 	mconfig->vbus_id = dfw->vbus_id;
2848ac9391daSGuenter Roeck 	mconfig->module->resources[0].is_pages = dfw->mem_pages;
2849ac9391daSGuenter Roeck 
2850ac9391daSGuenter Roeck 	ret = skl_tplg_add_pipe_v4(dev, mconfig, skl, &dfw->pipe);
2851ac9391daSGuenter Roeck 	if (ret)
2852ac9391daSGuenter Roeck 		return ret;
2853ac9391daSGuenter Roeck 
2854ac9391daSGuenter Roeck 	mconfig->dev_type = dfw->dev_type;
2855ac9391daSGuenter Roeck 	mconfig->hw_conn_type = dfw->hw_conn_type;
2856ac9391daSGuenter Roeck 	mconfig->time_slot = dfw->time_slot;
2857ac9391daSGuenter Roeck 	mconfig->formats_config.caps_size = dfw->caps.caps_size;
2858ac9391daSGuenter Roeck 
2859a86854d0SKees Cook 	mconfig->m_in_pin = devm_kcalloc(dev,
2860a86854d0SKees Cook 				MAX_IN_QUEUE, sizeof(*mconfig->m_in_pin),
2861ac9391daSGuenter Roeck 				GFP_KERNEL);
2862ac9391daSGuenter Roeck 	if (!mconfig->m_in_pin)
2863ac9391daSGuenter Roeck 		return -ENOMEM;
2864ac9391daSGuenter Roeck 
2865a86854d0SKees Cook 	mconfig->m_out_pin = devm_kcalloc(dev,
2866a86854d0SKees Cook 				MAX_OUT_QUEUE, sizeof(*mconfig->m_out_pin),
2867ac9391daSGuenter Roeck 				GFP_KERNEL);
2868ac9391daSGuenter Roeck 	if (!mconfig->m_out_pin)
2869ac9391daSGuenter Roeck 		return -ENOMEM;
2870ac9391daSGuenter Roeck 
2871ac9391daSGuenter Roeck 	skl_fill_module_pin_info_v4(dfw->in_pin, mconfig->m_in_pin,
2872ac9391daSGuenter Roeck 				    dfw->is_dynamic_in_pin,
2873ac9391daSGuenter Roeck 				    mconfig->module->max_input_pins);
2874ac9391daSGuenter Roeck 	skl_fill_module_pin_info_v4(dfw->out_pin, mconfig->m_out_pin,
2875ac9391daSGuenter Roeck 				    dfw->is_dynamic_out_pin,
2876ac9391daSGuenter Roeck 				    mconfig->module->max_output_pins);
2877ac9391daSGuenter Roeck 
2878ac9391daSGuenter Roeck 	if (mconfig->formats_config.caps_size) {
2879ac9391daSGuenter Roeck 		mconfig->formats_config.set_params = dfw->caps.set_params;
2880ac9391daSGuenter Roeck 		mconfig->formats_config.param_id = dfw->caps.param_id;
2881ac9391daSGuenter Roeck 		mconfig->formats_config.caps =
2882ac9391daSGuenter Roeck 		devm_kzalloc(dev, mconfig->formats_config.caps_size,
2883ac9391daSGuenter Roeck 			     GFP_KERNEL);
2884ac9391daSGuenter Roeck 		if (!mconfig->formats_config.caps)
2885ac9391daSGuenter Roeck 			return -ENOMEM;
2886ac9391daSGuenter Roeck 		memcpy(mconfig->formats_config.caps, dfw->caps.caps,
2887ac9391daSGuenter Roeck 		       dfw->caps.caps_size);
2888ac9391daSGuenter Roeck 	}
2889ac9391daSGuenter Roeck 
2890ac9391daSGuenter Roeck 	return 0;
2891ac9391daSGuenter Roeck }
2892ac9391daSGuenter Roeck 
28936277e832SShreyas NC /*
28946277e832SShreyas NC  * Parse the private data for the token and corresponding value.
28956277e832SShreyas NC  * The private data can have multiple data blocks. So, a data block
28966277e832SShreyas NC  * is preceded by a descriptor for number of blocks and a descriptor
28976277e832SShreyas NC  * for the type and size of the suceeding data block.
28986277e832SShreyas NC  */
28996277e832SShreyas NC static int skl_tplg_get_pvt_data(struct snd_soc_tplg_dapm_widget *tplg_w,
29006277e832SShreyas NC 				struct skl *skl, struct device *dev,
29016277e832SShreyas NC 				struct skl_module_cfg *mconfig)
29026277e832SShreyas NC {
29036277e832SShreyas NC 	struct snd_soc_tplg_vendor_array *array;
29046277e832SShreyas NC 	int num_blocks, block_size = 0, block_type, off = 0;
29056277e832SShreyas NC 	char *data;
29066277e832SShreyas NC 	int ret;
29076277e832SShreyas NC 
2908ac9391daSGuenter Roeck 	/*
2909ac9391daSGuenter Roeck 	 * v4 configuration files have a valid UUID at the start of
2910ac9391daSGuenter Roeck 	 * the widget's private data.
2911ac9391daSGuenter Roeck 	 */
2912ac9391daSGuenter Roeck 	if (uuid_is_valid((char *)tplg_w->priv.data))
2913ac9391daSGuenter Roeck 		return skl_tplg_get_pvt_data_v4(tplg_w, skl, dev, mconfig);
2914ac9391daSGuenter Roeck 
29156277e832SShreyas NC 	/* Read the NUM_DATA_BLOCKS descriptor */
29166277e832SShreyas NC 	array = (struct snd_soc_tplg_vendor_array *)tplg_w->priv.data;
29176277e832SShreyas NC 	ret = skl_tplg_get_desc_blocks(dev, array);
29186277e832SShreyas NC 	if (ret < 0)
29196277e832SShreyas NC 		return ret;
29206277e832SShreyas NC 	num_blocks = ret;
29216277e832SShreyas NC 
29226277e832SShreyas NC 	off += array->size;
29236277e832SShreyas NC 	/* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */
29246277e832SShreyas NC 	while (num_blocks > 0) {
2925133e6e5cSShreyas NC 		array = (struct snd_soc_tplg_vendor_array *)
2926133e6e5cSShreyas NC 				(tplg_w->priv.data + off);
2927133e6e5cSShreyas NC 
29286277e832SShreyas NC 		ret = skl_tplg_get_desc_blocks(dev, array);
29296277e832SShreyas NC 
29306277e832SShreyas NC 		if (ret < 0)
29316277e832SShreyas NC 			return ret;
29326277e832SShreyas NC 		block_type = ret;
29336277e832SShreyas NC 		off += array->size;
29346277e832SShreyas NC 
29356277e832SShreyas NC 		array = (struct snd_soc_tplg_vendor_array *)
29366277e832SShreyas NC 			(tplg_w->priv.data + off);
29376277e832SShreyas NC 
29386277e832SShreyas NC 		ret = skl_tplg_get_desc_blocks(dev, array);
29396277e832SShreyas NC 
29406277e832SShreyas NC 		if (ret < 0)
29416277e832SShreyas NC 			return ret;
29426277e832SShreyas NC 		block_size = ret;
29436277e832SShreyas NC 		off += array->size;
29446277e832SShreyas NC 
29456277e832SShreyas NC 		array = (struct snd_soc_tplg_vendor_array *)
29466277e832SShreyas NC 			(tplg_w->priv.data + off);
29476277e832SShreyas NC 
29486277e832SShreyas NC 		data = (tplg_w->priv.data + off);
29496277e832SShreyas NC 
29506277e832SShreyas NC 		if (block_type == SKL_TYPE_TUPLE) {
29516277e832SShreyas NC 			ret = skl_tplg_get_tokens(dev, data,
29526277e832SShreyas NC 					skl, mconfig, block_size);
29536277e832SShreyas NC 
29546277e832SShreyas NC 			if (ret < 0)
29556277e832SShreyas NC 				return ret;
29566277e832SShreyas NC 
29576277e832SShreyas NC 			--num_blocks;
29586277e832SShreyas NC 		} else {
29596277e832SShreyas NC 			if (mconfig->formats_config.caps_size > 0)
29606277e832SShreyas NC 				memcpy(mconfig->formats_config.caps, data,
29616277e832SShreyas NC 					mconfig->formats_config.caps_size);
29626277e832SShreyas NC 			--num_blocks;
2963133e6e5cSShreyas NC 			ret = mconfig->formats_config.caps_size;
29646277e832SShreyas NC 		}
2965133e6e5cSShreyas NC 		off += ret;
29666277e832SShreyas NC 	}
29676277e832SShreyas NC 
29686277e832SShreyas NC 	return 0;
29694cd9899fSHardik T Shah }
29704cd9899fSHardik T Shah 
297156b03b4cSKuninori Morimoto static void skl_clear_pin_config(struct snd_soc_component *component,
2972fe3f4442SDharageswari R 				struct snd_soc_dapm_widget *w)
2973fe3f4442SDharageswari R {
2974fe3f4442SDharageswari R 	int i;
2975fe3f4442SDharageswari R 	struct skl_module_cfg *mconfig;
2976fe3f4442SDharageswari R 	struct skl_pipe *pipe;
2977fe3f4442SDharageswari R 
297856b03b4cSKuninori Morimoto 	if (!strncmp(w->dapm->component->name, component->name,
297956b03b4cSKuninori Morimoto 					strlen(component->name))) {
2980fe3f4442SDharageswari R 		mconfig = w->priv;
2981fe3f4442SDharageswari R 		pipe = mconfig->pipe;
2982f6fa56e2SRamesh Babu 		for (i = 0; i < mconfig->module->max_input_pins; i++) {
2983fe3f4442SDharageswari R 			mconfig->m_in_pin[i].in_use = false;
2984fe3f4442SDharageswari R 			mconfig->m_in_pin[i].pin_state = SKL_PIN_UNBIND;
2985fe3f4442SDharageswari R 		}
2986f6fa56e2SRamesh Babu 		for (i = 0; i < mconfig->module->max_output_pins; i++) {
2987fe3f4442SDharageswari R 			mconfig->m_out_pin[i].in_use = false;
2988fe3f4442SDharageswari R 			mconfig->m_out_pin[i].pin_state = SKL_PIN_UNBIND;
2989fe3f4442SDharageswari R 		}
2990fe3f4442SDharageswari R 		pipe->state = SKL_PIPE_INVALID;
2991fe3f4442SDharageswari R 		mconfig->m_state = SKL_MODULE_UNINIT;
2992fe3f4442SDharageswari R 	}
2993fe3f4442SDharageswari R }
2994fe3f4442SDharageswari R 
2995fe3f4442SDharageswari R void skl_cleanup_resources(struct skl *skl)
2996fe3f4442SDharageswari R {
2997fe3f4442SDharageswari R 	struct skl_sst *ctx = skl->skl_sst;
299856b03b4cSKuninori Morimoto 	struct snd_soc_component *soc_component = skl->component;
2999fe3f4442SDharageswari R 	struct snd_soc_dapm_widget *w;
3000fe3f4442SDharageswari R 	struct snd_soc_card *card;
3001fe3f4442SDharageswari R 
300256b03b4cSKuninori Morimoto 	if (soc_component == NULL)
3003fe3f4442SDharageswari R 		return;
3004fe3f4442SDharageswari R 
300556b03b4cSKuninori Morimoto 	card = soc_component->card;
3006fe3f4442SDharageswari R 	if (!card || !card->instantiated)
3007fe3f4442SDharageswari R 		return;
3008fe3f4442SDharageswari R 
3009fe3f4442SDharageswari R 	skl->resource.mem = 0;
3010fe3f4442SDharageswari R 	skl->resource.mcps = 0;
3011fe3f4442SDharageswari R 
3012fe3f4442SDharageswari R 	list_for_each_entry(w, &card->widgets, list) {
3013cb1f904dSGuneshwor Singh 		if (is_skl_dsp_widget_type(w, ctx->dev) && w->priv != NULL)
301456b03b4cSKuninori Morimoto 			skl_clear_pin_config(soc_component, w);
3015fe3f4442SDharageswari R 	}
3016fe3f4442SDharageswari R 
3017fe3f4442SDharageswari R 	skl_clear_module_cnt(ctx->dsp);
3018fe3f4442SDharageswari R }
3019fe3f4442SDharageswari R 
30203af36706SVinod Koul /*
30213af36706SVinod Koul  * Topology core widget load callback
30223af36706SVinod Koul  *
30233af36706SVinod Koul  * This is used to save the private data for each widget which gives
30243af36706SVinod Koul  * information to the driver about module and pipeline parameters which DSP
30253af36706SVinod Koul  * FW expects like ids, resource values, formats etc
30263af36706SVinod Koul  */
302724ada035SMark Brown static int skl_tplg_widget_load(struct snd_soc_component *cmpnt,
30283af36706SVinod Koul 				struct snd_soc_dapm_widget *w,
30293af36706SVinod Koul 				struct snd_soc_tplg_dapm_widget *tplg_w)
30303af36706SVinod Koul {
30313af36706SVinod Koul 	int ret;
3032*76f56faeSRakesh Ughreja 	struct hdac_bus *bus = snd_soc_component_get_drvdata(cmpnt);
3033*76f56faeSRakesh Ughreja 	struct skl *skl = bus_to_skl(bus);
30343af36706SVinod Koul 	struct skl_module_cfg *mconfig;
30353af36706SVinod Koul 
30363af36706SVinod Koul 	if (!tplg_w->priv.size)
30373af36706SVinod Koul 		goto bind_event;
30383af36706SVinod Koul 
30393af36706SVinod Koul 	mconfig = devm_kzalloc(bus->dev, sizeof(*mconfig), GFP_KERNEL);
30403af36706SVinod Koul 
30413af36706SVinod Koul 	if (!mconfig)
30423af36706SVinod Koul 		return -ENOMEM;
30433af36706SVinod Koul 
3044f6fa56e2SRamesh Babu 	if (skl->nr_modules == 0) {
3045f6fa56e2SRamesh Babu 		mconfig->module = devm_kzalloc(bus->dev,
3046f6fa56e2SRamesh Babu 				sizeof(*mconfig->module), GFP_KERNEL);
3047f6fa56e2SRamesh Babu 		if (!mconfig->module)
3048f6fa56e2SRamesh Babu 			return -ENOMEM;
3049f6fa56e2SRamesh Babu 	}
3050f6fa56e2SRamesh Babu 
30513af36706SVinod Koul 	w->priv = mconfig;
305209305da9SShreyas NC 
3053b7c50555SVinod Koul 	/*
3054b7c50555SVinod Koul 	 * module binary can be loaded later, so set it to query when
3055b7c50555SVinod Koul 	 * module is load for a use case
3056b7c50555SVinod Koul 	 */
3057b7c50555SVinod Koul 	mconfig->id.module_id = -1;
30584cd9899fSHardik T Shah 
30596277e832SShreyas NC 	/* Parse private data for tuples */
30606277e832SShreyas NC 	ret = skl_tplg_get_pvt_data(tplg_w, skl, bus->dev, mconfig);
30616277e832SShreyas NC 	if (ret < 0)
30626277e832SShreyas NC 		return ret;
3063d14700a0SVinod Koul 
3064d14700a0SVinod Koul 	skl_debug_init_module(skl->debugfs, w, mconfig);
3065d14700a0SVinod Koul 
30663af36706SVinod Koul bind_event:
30673af36706SVinod Koul 	if (tplg_w->event_type == 0) {
30683373f716SVinod Koul 		dev_dbg(bus->dev, "ASoC: No event handler required\n");
30693af36706SVinod Koul 		return 0;
30703af36706SVinod Koul 	}
30713af36706SVinod Koul 
30723af36706SVinod Koul 	ret = snd_soc_tplg_widget_bind_event(w, skl_tplg_widget_ops,
3073b663a8c5SJeeja KP 					ARRAY_SIZE(skl_tplg_widget_ops),
3074b663a8c5SJeeja KP 					tplg_w->event_type);
30753af36706SVinod Koul 
30763af36706SVinod Koul 	if (ret) {
30773af36706SVinod Koul 		dev_err(bus->dev, "%s: No matching event handlers found for %d\n",
30783af36706SVinod Koul 					__func__, tplg_w->event_type);
30793af36706SVinod Koul 		return -EINVAL;
30803af36706SVinod Koul 	}
30813af36706SVinod Koul 
30823af36706SVinod Koul 	return 0;
30833af36706SVinod Koul }
30843af36706SVinod Koul 
3085140adfbaSJeeja KP static int skl_init_algo_data(struct device *dev, struct soc_bytes_ext *be,
3086140adfbaSJeeja KP 					struct snd_soc_tplg_bytes_control *bc)
3087140adfbaSJeeja KP {
3088140adfbaSJeeja KP 	struct skl_algo_data *ac;
3089140adfbaSJeeja KP 	struct skl_dfw_algo_data *dfw_ac =
3090140adfbaSJeeja KP 				(struct skl_dfw_algo_data *)bc->priv.data;
3091140adfbaSJeeja KP 
3092140adfbaSJeeja KP 	ac = devm_kzalloc(dev, sizeof(*ac), GFP_KERNEL);
3093140adfbaSJeeja KP 	if (!ac)
3094140adfbaSJeeja KP 		return -ENOMEM;
3095140adfbaSJeeja KP 
3096140adfbaSJeeja KP 	/* Fill private data */
3097140adfbaSJeeja KP 	ac->max = dfw_ac->max;
3098140adfbaSJeeja KP 	ac->param_id = dfw_ac->param_id;
3099140adfbaSJeeja KP 	ac->set_params = dfw_ac->set_params;
31000d682104SDharageswari R 	ac->size = dfw_ac->max;
3101140adfbaSJeeja KP 
3102140adfbaSJeeja KP 	if (ac->max) {
3103140adfbaSJeeja KP 		ac->params = (char *) devm_kzalloc(dev, ac->max, GFP_KERNEL);
3104140adfbaSJeeja KP 		if (!ac->params)
3105140adfbaSJeeja KP 			return -ENOMEM;
3106140adfbaSJeeja KP 
3107140adfbaSJeeja KP 		memcpy(ac->params, dfw_ac->params, ac->max);
3108140adfbaSJeeja KP 	}
3109140adfbaSJeeja KP 
3110140adfbaSJeeja KP 	be->dobj.private  = ac;
3111140adfbaSJeeja KP 	return 0;
3112140adfbaSJeeja KP }
3113140adfbaSJeeja KP 
31147a1b749bSDharageswari R static int skl_init_enum_data(struct device *dev, struct soc_enum *se,
31157a1b749bSDharageswari R 				struct snd_soc_tplg_enum_control *ec)
31167a1b749bSDharageswari R {
31177a1b749bSDharageswari R 
31187a1b749bSDharageswari R 	void *data;
31197a1b749bSDharageswari R 
31207a1b749bSDharageswari R 	if (ec->priv.size) {
31217a1b749bSDharageswari R 		data = devm_kzalloc(dev, sizeof(ec->priv.size), GFP_KERNEL);
31227a1b749bSDharageswari R 		if (!data)
31237a1b749bSDharageswari R 			return -ENOMEM;
31247a1b749bSDharageswari R 		memcpy(data, ec->priv.data, ec->priv.size);
31257a1b749bSDharageswari R 		se->dobj.private = data;
31267a1b749bSDharageswari R 	}
31277a1b749bSDharageswari R 
31287a1b749bSDharageswari R 	return 0;
31297a1b749bSDharageswari R 
31307a1b749bSDharageswari R }
31317a1b749bSDharageswari R 
3132140adfbaSJeeja KP static int skl_tplg_control_load(struct snd_soc_component *cmpnt,
3133140adfbaSJeeja KP 				struct snd_kcontrol_new *kctl,
3134140adfbaSJeeja KP 				struct snd_soc_tplg_ctl_hdr *hdr)
3135140adfbaSJeeja KP {
3136140adfbaSJeeja KP 	struct soc_bytes_ext *sb;
3137140adfbaSJeeja KP 	struct snd_soc_tplg_bytes_control *tplg_bc;
31387a1b749bSDharageswari R 	struct snd_soc_tplg_enum_control *tplg_ec;
3139*76f56faeSRakesh Ughreja 	struct hdac_bus *bus  = snd_soc_component_get_drvdata(cmpnt);
31407a1b749bSDharageswari R 	struct soc_enum *se;
3141140adfbaSJeeja KP 
3142140adfbaSJeeja KP 	switch (hdr->ops.info) {
3143140adfbaSJeeja KP 	case SND_SOC_TPLG_CTL_BYTES:
3144140adfbaSJeeja KP 		tplg_bc = container_of(hdr,
3145140adfbaSJeeja KP 				struct snd_soc_tplg_bytes_control, hdr);
3146140adfbaSJeeja KP 		if (kctl->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
3147140adfbaSJeeja KP 			sb = (struct soc_bytes_ext *)kctl->private_value;
3148140adfbaSJeeja KP 			if (tplg_bc->priv.size)
3149140adfbaSJeeja KP 				return skl_init_algo_data(
3150140adfbaSJeeja KP 						bus->dev, sb, tplg_bc);
3151140adfbaSJeeja KP 		}
3152140adfbaSJeeja KP 		break;
3153140adfbaSJeeja KP 
31547a1b749bSDharageswari R 	case SND_SOC_TPLG_CTL_ENUM:
31557a1b749bSDharageswari R 		tplg_ec = container_of(hdr,
31567a1b749bSDharageswari R 				struct snd_soc_tplg_enum_control, hdr);
31577a1b749bSDharageswari R 		if (kctl->access & SNDRV_CTL_ELEM_ACCESS_READWRITE) {
31587a1b749bSDharageswari R 			se = (struct soc_enum *)kctl->private_value;
31597a1b749bSDharageswari R 			if (tplg_ec->priv.size)
31607a1b749bSDharageswari R 				return skl_init_enum_data(bus->dev, se,
31617a1b749bSDharageswari R 						tplg_ec);
31627a1b749bSDharageswari R 		}
31637a1b749bSDharageswari R 		break;
31647a1b749bSDharageswari R 
3165140adfbaSJeeja KP 	default:
31664362934aSNaveen Manohar 		dev_dbg(bus->dev, "Control load not supported %d:%d:%d\n",
3167140adfbaSJeeja KP 			hdr->ops.get, hdr->ops.put, hdr->ops.info);
3168140adfbaSJeeja KP 		break;
3169140adfbaSJeeja KP 	}
3170140adfbaSJeeja KP 
3171140adfbaSJeeja KP 	return 0;
3172140adfbaSJeeja KP }
3173140adfbaSJeeja KP 
3174541070ceSShreyas NC static int skl_tplg_fill_str_mfest_tkn(struct device *dev,
3175541070ceSShreyas NC 		struct snd_soc_tplg_vendor_string_elem *str_elem,
3176eee0e16fSJeeja KP 		struct skl *skl)
3177541070ceSShreyas NC {
3178541070ceSShreyas NC 	int tkn_count = 0;
3179541070ceSShreyas NC 	static int ref_count;
3180541070ceSShreyas NC 
3181541070ceSShreyas NC 	switch (str_elem->token) {
3182541070ceSShreyas NC 	case SKL_TKN_STR_LIB_NAME:
3183eee0e16fSJeeja KP 		if (ref_count > skl->skl_sst->lib_count - 1) {
3184541070ceSShreyas NC 			ref_count = 0;
3185541070ceSShreyas NC 			return -EINVAL;
3186541070ceSShreyas NC 		}
3187541070ceSShreyas NC 
3188eee0e16fSJeeja KP 		strncpy(skl->skl_sst->lib_info[ref_count].name,
3189eee0e16fSJeeja KP 			str_elem->string,
3190eee0e16fSJeeja KP 			ARRAY_SIZE(skl->skl_sst->lib_info[ref_count].name));
3191541070ceSShreyas NC 		ref_count++;
3192541070ceSShreyas NC 		break;
3193541070ceSShreyas NC 
3194541070ceSShreyas NC 	default:
3195ecd286a9SColin Ian King 		dev_err(dev, "Not a string token %d\n", str_elem->token);
3196541070ceSShreyas NC 		break;
3197541070ceSShreyas NC 	}
3198db6ed55dSShreyas NC 	tkn_count++;
3199541070ceSShreyas NC 
3200541070ceSShreyas NC 	return tkn_count;
3201541070ceSShreyas NC }
3202541070ceSShreyas NC 
3203541070ceSShreyas NC static int skl_tplg_get_str_tkn(struct device *dev,
3204541070ceSShreyas NC 		struct snd_soc_tplg_vendor_array *array,
3205eee0e16fSJeeja KP 		struct skl *skl)
3206541070ceSShreyas NC {
3207541070ceSShreyas NC 	int tkn_count = 0, ret;
3208541070ceSShreyas NC 	struct snd_soc_tplg_vendor_string_elem *str_elem;
3209541070ceSShreyas NC 
3210541070ceSShreyas NC 	str_elem = (struct snd_soc_tplg_vendor_string_elem *)array->value;
3211541070ceSShreyas NC 	while (tkn_count < array->num_elems) {
3212eee0e16fSJeeja KP 		ret = skl_tplg_fill_str_mfest_tkn(dev, str_elem, skl);
3213541070ceSShreyas NC 		str_elem++;
3214541070ceSShreyas NC 
3215541070ceSShreyas NC 		if (ret < 0)
3216541070ceSShreyas NC 			return ret;
3217541070ceSShreyas NC 
3218541070ceSShreyas NC 		tkn_count = tkn_count + ret;
3219541070ceSShreyas NC 	}
3220541070ceSShreyas NC 
3221541070ceSShreyas NC 	return tkn_count;
3222541070ceSShreyas NC }
3223541070ceSShreyas NC 
3224db6ed55dSShreyas NC static int skl_tplg_manifest_fill_fmt(struct device *dev,
3225db6ed55dSShreyas NC 		struct skl_module_iface *fmt,
3226db6ed55dSShreyas NC 		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
3227db6ed55dSShreyas NC 		u32 dir, int fmt_idx)
3228db6ed55dSShreyas NC {
3229db6ed55dSShreyas NC 	struct skl_module_pin_fmt *dst_fmt;
3230db6ed55dSShreyas NC 	struct skl_module_fmt *mod_fmt;
3231db6ed55dSShreyas NC 	int ret;
3232db6ed55dSShreyas NC 
3233db6ed55dSShreyas NC 	if (!fmt)
3234db6ed55dSShreyas NC 		return -EINVAL;
3235db6ed55dSShreyas NC 
3236db6ed55dSShreyas NC 	switch (dir) {
3237db6ed55dSShreyas NC 	case SKL_DIR_IN:
3238db6ed55dSShreyas NC 		dst_fmt = &fmt->inputs[fmt_idx];
3239db6ed55dSShreyas NC 		break;
3240db6ed55dSShreyas NC 
3241db6ed55dSShreyas NC 	case SKL_DIR_OUT:
3242db6ed55dSShreyas NC 		dst_fmt = &fmt->outputs[fmt_idx];
3243db6ed55dSShreyas NC 		break;
3244db6ed55dSShreyas NC 
3245db6ed55dSShreyas NC 	default:
3246db6ed55dSShreyas NC 		dev_err(dev, "Invalid direction: %d\n", dir);
3247db6ed55dSShreyas NC 		return -EINVAL;
3248db6ed55dSShreyas NC 	}
3249db6ed55dSShreyas NC 
3250db6ed55dSShreyas NC 	mod_fmt = &dst_fmt->fmt;
3251db6ed55dSShreyas NC 
3252db6ed55dSShreyas NC 	switch (tkn_elem->token) {
3253db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_INTF_PIN_ID:
3254db6ed55dSShreyas NC 		dst_fmt->id = tkn_elem->value;
3255db6ed55dSShreyas NC 		break;
3256db6ed55dSShreyas NC 
3257db6ed55dSShreyas NC 	default:
3258db6ed55dSShreyas NC 		ret = skl_tplg_fill_fmt(dev, mod_fmt, tkn_elem->token,
3259db6ed55dSShreyas NC 					tkn_elem->value);
3260db6ed55dSShreyas NC 		if (ret < 0)
3261db6ed55dSShreyas NC 			return ret;
3262db6ed55dSShreyas NC 		break;
3263db6ed55dSShreyas NC 	}
3264db6ed55dSShreyas NC 
3265db6ed55dSShreyas NC 	return 0;
3266db6ed55dSShreyas NC }
3267db6ed55dSShreyas NC 
3268db6ed55dSShreyas NC static int skl_tplg_fill_mod_info(struct device *dev,
3269db6ed55dSShreyas NC 		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
3270db6ed55dSShreyas NC 		struct skl_module *mod)
3271db6ed55dSShreyas NC {
3272db6ed55dSShreyas NC 
3273db6ed55dSShreyas NC 	if (!mod)
3274db6ed55dSShreyas NC 		return -EINVAL;
3275db6ed55dSShreyas NC 
3276db6ed55dSShreyas NC 	switch (tkn_elem->token) {
3277db6ed55dSShreyas NC 	case SKL_TKN_U8_IN_PIN_TYPE:
3278db6ed55dSShreyas NC 		mod->input_pin_type = tkn_elem->value;
3279db6ed55dSShreyas NC 		break;
3280db6ed55dSShreyas NC 
3281db6ed55dSShreyas NC 	case SKL_TKN_U8_OUT_PIN_TYPE:
3282db6ed55dSShreyas NC 		mod->output_pin_type = tkn_elem->value;
3283db6ed55dSShreyas NC 		break;
3284db6ed55dSShreyas NC 
3285db6ed55dSShreyas NC 	case SKL_TKN_U8_IN_QUEUE_COUNT:
3286db6ed55dSShreyas NC 		mod->max_input_pins = tkn_elem->value;
3287db6ed55dSShreyas NC 		break;
3288db6ed55dSShreyas NC 
3289db6ed55dSShreyas NC 	case SKL_TKN_U8_OUT_QUEUE_COUNT:
3290db6ed55dSShreyas NC 		mod->max_output_pins = tkn_elem->value;
3291db6ed55dSShreyas NC 		break;
3292db6ed55dSShreyas NC 
3293db6ed55dSShreyas NC 	case SKL_TKN_MM_U8_NUM_RES:
3294db6ed55dSShreyas NC 		mod->nr_resources = tkn_elem->value;
3295db6ed55dSShreyas NC 		break;
3296db6ed55dSShreyas NC 
3297db6ed55dSShreyas NC 	case SKL_TKN_MM_U8_NUM_INTF:
3298db6ed55dSShreyas NC 		mod->nr_interfaces = tkn_elem->value;
3299db6ed55dSShreyas NC 		break;
3300db6ed55dSShreyas NC 
3301db6ed55dSShreyas NC 	default:
3302db6ed55dSShreyas NC 		dev_err(dev, "Invalid mod info token %d", tkn_elem->token);
3303db6ed55dSShreyas NC 		return -EINVAL;
3304db6ed55dSShreyas NC 	}
3305db6ed55dSShreyas NC 
3306db6ed55dSShreyas NC 	return 0;
3307db6ed55dSShreyas NC }
3308db6ed55dSShreyas NC 
3309db6ed55dSShreyas NC 
3310541070ceSShreyas NC static int skl_tplg_get_int_tkn(struct device *dev,
3311541070ceSShreyas NC 		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
3312eee0e16fSJeeja KP 		struct skl *skl)
3313541070ceSShreyas NC {
331443762355SPradeep Tewani 	int tkn_count = 0, ret, size;
3315db6ed55dSShreyas NC 	static int mod_idx, res_val_idx, intf_val_idx, dir, pin_idx;
3316db6ed55dSShreyas NC 	struct skl_module_res *res = NULL;
3317db6ed55dSShreyas NC 	struct skl_module_iface *fmt = NULL;
3318db6ed55dSShreyas NC 	struct skl_module *mod = NULL;
331943762355SPradeep Tewani 	static struct skl_astate_param *astate_table;
332043762355SPradeep Tewani 	static int astate_cfg_idx, count;
3321db6ed55dSShreyas NC 	int i;
3322db6ed55dSShreyas NC 
3323db6ed55dSShreyas NC 	if (skl->modules) {
3324db6ed55dSShreyas NC 		mod = skl->modules[mod_idx];
3325db6ed55dSShreyas NC 		res = &mod->resources[res_val_idx];
3326db6ed55dSShreyas NC 		fmt = &mod->formats[intf_val_idx];
3327db6ed55dSShreyas NC 	}
3328541070ceSShreyas NC 
3329541070ceSShreyas NC 	switch (tkn_elem->token) {
3330541070ceSShreyas NC 	case SKL_TKN_U32_LIB_COUNT:
3331eee0e16fSJeeja KP 		skl->skl_sst->lib_count = tkn_elem->value;
3332db6ed55dSShreyas NC 		break;
3333db6ed55dSShreyas NC 
3334db6ed55dSShreyas NC 	case SKL_TKN_U8_NUM_MOD:
3335db6ed55dSShreyas NC 		skl->nr_modules = tkn_elem->value;
3336db6ed55dSShreyas NC 		skl->modules = devm_kcalloc(dev, skl->nr_modules,
3337db6ed55dSShreyas NC 				sizeof(*skl->modules), GFP_KERNEL);
3338db6ed55dSShreyas NC 		if (!skl->modules)
3339db6ed55dSShreyas NC 			return -ENOMEM;
3340db6ed55dSShreyas NC 
3341db6ed55dSShreyas NC 		for (i = 0; i < skl->nr_modules; i++) {
3342db6ed55dSShreyas NC 			skl->modules[i] = devm_kzalloc(dev,
3343db6ed55dSShreyas NC 					sizeof(struct skl_module), GFP_KERNEL);
3344db6ed55dSShreyas NC 			if (!skl->modules[i])
3345db6ed55dSShreyas NC 				return -ENOMEM;
3346db6ed55dSShreyas NC 		}
3347db6ed55dSShreyas NC 		break;
3348db6ed55dSShreyas NC 
3349db6ed55dSShreyas NC 	case SKL_TKN_MM_U8_MOD_IDX:
3350db6ed55dSShreyas NC 		mod_idx = tkn_elem->value;
3351db6ed55dSShreyas NC 		break;
3352db6ed55dSShreyas NC 
335343762355SPradeep Tewani 	case SKL_TKN_U32_ASTATE_COUNT:
335443762355SPradeep Tewani 		if (astate_table != NULL) {
335543762355SPradeep Tewani 			dev_err(dev, "More than one entry for A-State count");
335643762355SPradeep Tewani 			return -EINVAL;
335743762355SPradeep Tewani 		}
335843762355SPradeep Tewani 
335943762355SPradeep Tewani 		if (tkn_elem->value > SKL_MAX_ASTATE_CFG) {
336043762355SPradeep Tewani 			dev_err(dev, "Invalid A-State count %d\n",
336143762355SPradeep Tewani 				tkn_elem->value);
336243762355SPradeep Tewani 			return -EINVAL;
336343762355SPradeep Tewani 		}
336443762355SPradeep Tewani 
336543762355SPradeep Tewani 		size = tkn_elem->value * sizeof(struct skl_astate_param) +
336643762355SPradeep Tewani 				sizeof(count);
336743762355SPradeep Tewani 		skl->cfg.astate_cfg = devm_kzalloc(dev, size, GFP_KERNEL);
336843762355SPradeep Tewani 		if (!skl->cfg.astate_cfg)
336943762355SPradeep Tewani 			return -ENOMEM;
337043762355SPradeep Tewani 
337143762355SPradeep Tewani 		astate_table = skl->cfg.astate_cfg->astate_table;
337243762355SPradeep Tewani 		count = skl->cfg.astate_cfg->count = tkn_elem->value;
337343762355SPradeep Tewani 		break;
337443762355SPradeep Tewani 
337543762355SPradeep Tewani 	case SKL_TKN_U32_ASTATE_IDX:
337643762355SPradeep Tewani 		if (tkn_elem->value >= count) {
337743762355SPradeep Tewani 			dev_err(dev, "Invalid A-State index %d\n",
337843762355SPradeep Tewani 				tkn_elem->value);
337943762355SPradeep Tewani 			return -EINVAL;
338043762355SPradeep Tewani 		}
338143762355SPradeep Tewani 
338243762355SPradeep Tewani 		astate_cfg_idx = tkn_elem->value;
338343762355SPradeep Tewani 		break;
338443762355SPradeep Tewani 
338543762355SPradeep Tewani 	case SKL_TKN_U32_ASTATE_KCPS:
338643762355SPradeep Tewani 		astate_table[astate_cfg_idx].kcps = tkn_elem->value;
338743762355SPradeep Tewani 		break;
338843762355SPradeep Tewani 
338943762355SPradeep Tewani 	case SKL_TKN_U32_ASTATE_CLK_SRC:
339043762355SPradeep Tewani 		astate_table[astate_cfg_idx].clk_src = tkn_elem->value;
339143762355SPradeep Tewani 		break;
339243762355SPradeep Tewani 
3393db6ed55dSShreyas NC 	case SKL_TKN_U8_IN_PIN_TYPE:
3394db6ed55dSShreyas NC 	case SKL_TKN_U8_OUT_PIN_TYPE:
3395db6ed55dSShreyas NC 	case SKL_TKN_U8_IN_QUEUE_COUNT:
3396db6ed55dSShreyas NC 	case SKL_TKN_U8_OUT_QUEUE_COUNT:
3397db6ed55dSShreyas NC 	case SKL_TKN_MM_U8_NUM_RES:
3398db6ed55dSShreyas NC 	case SKL_TKN_MM_U8_NUM_INTF:
3399db6ed55dSShreyas NC 		ret = skl_tplg_fill_mod_info(dev, tkn_elem, mod);
3400db6ed55dSShreyas NC 		if (ret < 0)
3401db6ed55dSShreyas NC 			return ret;
3402db6ed55dSShreyas NC 		break;
3403db6ed55dSShreyas NC 
3404db6ed55dSShreyas NC 	case SKL_TKN_U32_DIR_PIN_COUNT:
3405db6ed55dSShreyas NC 		dir = tkn_elem->value & SKL_IN_DIR_BIT_MASK;
3406db6ed55dSShreyas NC 		pin_idx = (tkn_elem->value & SKL_PIN_COUNT_MASK) >> 4;
3407db6ed55dSShreyas NC 		break;
3408db6ed55dSShreyas NC 
3409db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_RES_ID:
3410db6ed55dSShreyas NC 		if (!res)
3411db6ed55dSShreyas NC 			return -EINVAL;
3412db6ed55dSShreyas NC 
3413db6ed55dSShreyas NC 		res->id = tkn_elem->value;
3414db6ed55dSShreyas NC 		res_val_idx = tkn_elem->value;
3415db6ed55dSShreyas NC 		break;
3416db6ed55dSShreyas NC 
3417db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_FMT_ID:
3418db6ed55dSShreyas NC 		if (!fmt)
3419db6ed55dSShreyas NC 			return -EINVAL;
3420db6ed55dSShreyas NC 
3421db6ed55dSShreyas NC 		fmt->fmt_idx = tkn_elem->value;
3422db6ed55dSShreyas NC 		intf_val_idx = tkn_elem->value;
3423db6ed55dSShreyas NC 		break;
3424db6ed55dSShreyas NC 
3425db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_CPS:
3426db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_DMA_SIZE:
3427db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_CPC:
3428db6ed55dSShreyas NC 	case SKL_TKN_U32_MEM_PAGES:
3429db6ed55dSShreyas NC 	case SKL_TKN_U32_OBS:
3430db6ed55dSShreyas NC 	case SKL_TKN_U32_IBS:
3431db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_RES_PIN_ID:
3432db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_PIN_BUF:
3433db6ed55dSShreyas NC 		ret = skl_tplg_fill_res_tkn(dev, tkn_elem, res, pin_idx, dir);
3434db6ed55dSShreyas NC 		if (ret < 0)
3435db6ed55dSShreyas NC 			return ret;
3436db6ed55dSShreyas NC 
3437db6ed55dSShreyas NC 		break;
3438db6ed55dSShreyas NC 
3439db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_NUM_IN_FMT:
3440db6ed55dSShreyas NC 		if (!fmt)
3441db6ed55dSShreyas NC 			return -EINVAL;
3442db6ed55dSShreyas NC 
3443db6ed55dSShreyas NC 		res->nr_input_pins = tkn_elem->value;
3444db6ed55dSShreyas NC 		break;
3445db6ed55dSShreyas NC 
3446db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_NUM_OUT_FMT:
3447db6ed55dSShreyas NC 		if (!fmt)
3448db6ed55dSShreyas NC 			return -EINVAL;
3449db6ed55dSShreyas NC 
3450db6ed55dSShreyas NC 		res->nr_output_pins = tkn_elem->value;
3451db6ed55dSShreyas NC 		break;
3452db6ed55dSShreyas NC 
3453db6ed55dSShreyas NC 	case SKL_TKN_U32_FMT_CH:
3454db6ed55dSShreyas NC 	case SKL_TKN_U32_FMT_FREQ:
3455db6ed55dSShreyas NC 	case SKL_TKN_U32_FMT_BIT_DEPTH:
3456db6ed55dSShreyas NC 	case SKL_TKN_U32_FMT_SAMPLE_SIZE:
3457db6ed55dSShreyas NC 	case SKL_TKN_U32_FMT_CH_CONFIG:
3458db6ed55dSShreyas NC 	case SKL_TKN_U32_FMT_INTERLEAVE:
3459db6ed55dSShreyas NC 	case SKL_TKN_U32_FMT_SAMPLE_TYPE:
3460db6ed55dSShreyas NC 	case SKL_TKN_U32_FMT_CH_MAP:
3461db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_INTF_PIN_ID:
3462db6ed55dSShreyas NC 		ret = skl_tplg_manifest_fill_fmt(dev, fmt, tkn_elem,
3463db6ed55dSShreyas NC 						 dir, pin_idx);
3464db6ed55dSShreyas NC 		if (ret < 0)
3465db6ed55dSShreyas NC 			return ret;
3466541070ceSShreyas NC 		break;
3467541070ceSShreyas NC 
3468541070ceSShreyas NC 	default:
3469ecd286a9SColin Ian King 		dev_err(dev, "Not a manifest token %d\n", tkn_elem->token);
3470541070ceSShreyas NC 		return -EINVAL;
3471541070ceSShreyas NC 	}
3472db6ed55dSShreyas NC 	tkn_count++;
3473541070ceSShreyas NC 
3474541070ceSShreyas NC 	return tkn_count;
3475541070ceSShreyas NC }
3476541070ceSShreyas NC 
3477db6ed55dSShreyas NC static int skl_tplg_get_manifest_uuid(struct device *dev,
3478db6ed55dSShreyas NC 				struct skl *skl,
3479db6ed55dSShreyas NC 				struct snd_soc_tplg_vendor_uuid_elem *uuid_tkn)
3480db6ed55dSShreyas NC {
3481db6ed55dSShreyas NC 	static int ref_count;
3482db6ed55dSShreyas NC 	struct skl_module *mod;
3483db6ed55dSShreyas NC 
3484db6ed55dSShreyas NC 	if (uuid_tkn->token == SKL_TKN_UUID) {
3485db6ed55dSShreyas NC 		mod = skl->modules[ref_count];
3486db6ed55dSShreyas NC 		memcpy(&mod->uuid, &uuid_tkn->uuid, sizeof(uuid_tkn->uuid));
3487db6ed55dSShreyas NC 		ref_count++;
3488db6ed55dSShreyas NC 	} else {
3489db6ed55dSShreyas NC 		dev_err(dev, "Not an UUID token tkn %d\n", uuid_tkn->token);
3490db6ed55dSShreyas NC 		return -EINVAL;
3491db6ed55dSShreyas NC 	}
3492db6ed55dSShreyas NC 
3493db6ed55dSShreyas NC 	return 0;
3494db6ed55dSShreyas NC }
3495db6ed55dSShreyas NC 
3496541070ceSShreyas NC /*
3497541070ceSShreyas NC  * Fill the manifest structure by parsing the tokens based on the
3498541070ceSShreyas NC  * type.
3499541070ceSShreyas NC  */
3500541070ceSShreyas NC static int skl_tplg_get_manifest_tkn(struct device *dev,
3501eee0e16fSJeeja KP 		char *pvt_data, struct skl *skl,
3502541070ceSShreyas NC 		int block_size)
3503541070ceSShreyas NC {
3504541070ceSShreyas NC 	int tkn_count = 0, ret;
3505541070ceSShreyas NC 	int off = 0, tuple_size = 0;
3506541070ceSShreyas NC 	struct snd_soc_tplg_vendor_array *array;
3507541070ceSShreyas NC 	struct snd_soc_tplg_vendor_value_elem *tkn_elem;
3508541070ceSShreyas NC 
3509541070ceSShreyas NC 	if (block_size <= 0)
3510541070ceSShreyas NC 		return -EINVAL;
3511541070ceSShreyas NC 
3512541070ceSShreyas NC 	while (tuple_size < block_size) {
3513541070ceSShreyas NC 		array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off);
3514541070ceSShreyas NC 		off += array->size;
3515541070ceSShreyas NC 		switch (array->type) {
3516541070ceSShreyas NC 		case SND_SOC_TPLG_TUPLE_TYPE_STRING:
3517eee0e16fSJeeja KP 			ret = skl_tplg_get_str_tkn(dev, array, skl);
3518541070ceSShreyas NC 
3519541070ceSShreyas NC 			if (ret < 0)
3520541070ceSShreyas NC 				return ret;
35210a716776SShreyas NC 			tkn_count = ret;
3522541070ceSShreyas NC 
3523541070ceSShreyas NC 			tuple_size += tkn_count *
3524541070ceSShreyas NC 				sizeof(struct snd_soc_tplg_vendor_string_elem);
3525541070ceSShreyas NC 			continue;
3526541070ceSShreyas NC 
3527541070ceSShreyas NC 		case SND_SOC_TPLG_TUPLE_TYPE_UUID:
3528db6ed55dSShreyas NC 			ret = skl_tplg_get_manifest_uuid(dev, skl, array->uuid);
3529db6ed55dSShreyas NC 			if (ret < 0)
3530db6ed55dSShreyas NC 				return ret;
3531db6ed55dSShreyas NC 
3532db6ed55dSShreyas NC 			tuple_size += sizeof(*array->uuid);
3533541070ceSShreyas NC 			continue;
3534541070ceSShreyas NC 
3535541070ceSShreyas NC 		default:
3536541070ceSShreyas NC 			tkn_elem = array->value;
3537541070ceSShreyas NC 			tkn_count = 0;
3538541070ceSShreyas NC 			break;
3539541070ceSShreyas NC 		}
3540541070ceSShreyas NC 
3541541070ceSShreyas NC 		while (tkn_count <= array->num_elems - 1) {
3542541070ceSShreyas NC 			ret = skl_tplg_get_int_tkn(dev,
3543eee0e16fSJeeja KP 					tkn_elem, skl);
3544541070ceSShreyas NC 			if (ret < 0)
3545541070ceSShreyas NC 				return ret;
3546541070ceSShreyas NC 
3547541070ceSShreyas NC 			tkn_count = tkn_count + ret;
3548541070ceSShreyas NC 			tkn_elem++;
3549541070ceSShreyas NC 		}
35509fc129f6SShreyas NC 		tuple_size += (tkn_count * sizeof(*tkn_elem));
3551541070ceSShreyas NC 		tkn_count = 0;
3552541070ceSShreyas NC 	}
3553541070ceSShreyas NC 
35549fc129f6SShreyas NC 	return off;
3555541070ceSShreyas NC }
3556541070ceSShreyas NC 
3557541070ceSShreyas NC /*
3558541070ceSShreyas NC  * Parse manifest private data for tokens. The private data block is
3559541070ceSShreyas NC  * preceded by descriptors for type and size of data block.
3560541070ceSShreyas NC  */
3561541070ceSShreyas NC static int skl_tplg_get_manifest_data(struct snd_soc_tplg_manifest *manifest,
3562eee0e16fSJeeja KP 			struct device *dev, struct skl *skl)
3563541070ceSShreyas NC {
3564541070ceSShreyas NC 	struct snd_soc_tplg_vendor_array *array;
3565541070ceSShreyas NC 	int num_blocks, block_size = 0, block_type, off = 0;
3566541070ceSShreyas NC 	char *data;
3567541070ceSShreyas NC 	int ret;
3568541070ceSShreyas NC 
3569541070ceSShreyas NC 	/* Read the NUM_DATA_BLOCKS descriptor */
3570541070ceSShreyas NC 	array = (struct snd_soc_tplg_vendor_array *)manifest->priv.data;
3571541070ceSShreyas NC 	ret = skl_tplg_get_desc_blocks(dev, array);
3572541070ceSShreyas NC 	if (ret < 0)
3573541070ceSShreyas NC 		return ret;
3574541070ceSShreyas NC 	num_blocks = ret;
3575541070ceSShreyas NC 
3576541070ceSShreyas NC 	off += array->size;
3577541070ceSShreyas NC 	/* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */
3578541070ceSShreyas NC 	while (num_blocks > 0) {
35799fc129f6SShreyas NC 		array = (struct snd_soc_tplg_vendor_array *)
35809fc129f6SShreyas NC 				(manifest->priv.data + off);
3581541070ceSShreyas NC 		ret = skl_tplg_get_desc_blocks(dev, array);
3582541070ceSShreyas NC 
3583541070ceSShreyas NC 		if (ret < 0)
3584541070ceSShreyas NC 			return ret;
3585541070ceSShreyas NC 		block_type = ret;
3586541070ceSShreyas NC 		off += array->size;
3587541070ceSShreyas NC 
3588541070ceSShreyas NC 		array = (struct snd_soc_tplg_vendor_array *)
3589541070ceSShreyas NC 			(manifest->priv.data + off);
3590541070ceSShreyas NC 
3591541070ceSShreyas NC 		ret = skl_tplg_get_desc_blocks(dev, array);
3592541070ceSShreyas NC 
3593541070ceSShreyas NC 		if (ret < 0)
3594541070ceSShreyas NC 			return ret;
3595541070ceSShreyas NC 		block_size = ret;
3596541070ceSShreyas NC 		off += array->size;
3597541070ceSShreyas NC 
3598541070ceSShreyas NC 		array = (struct snd_soc_tplg_vendor_array *)
3599541070ceSShreyas NC 			(manifest->priv.data + off);
3600541070ceSShreyas NC 
3601541070ceSShreyas NC 		data = (manifest->priv.data + off);
3602541070ceSShreyas NC 
3603541070ceSShreyas NC 		if (block_type == SKL_TYPE_TUPLE) {
3604eee0e16fSJeeja KP 			ret = skl_tplg_get_manifest_tkn(dev, data, skl,
3605541070ceSShreyas NC 					block_size);
3606541070ceSShreyas NC 
3607541070ceSShreyas NC 			if (ret < 0)
3608541070ceSShreyas NC 				return ret;
3609541070ceSShreyas NC 
3610541070ceSShreyas NC 			--num_blocks;
3611541070ceSShreyas NC 		} else {
3612541070ceSShreyas NC 			return -EINVAL;
3613541070ceSShreyas NC 		}
36149fc129f6SShreyas NC 		off += ret;
3615541070ceSShreyas NC 	}
3616541070ceSShreyas NC 
3617541070ceSShreyas NC 	return 0;
3618541070ceSShreyas NC }
3619541070ceSShreyas NC 
362024ada035SMark Brown static int skl_manifest_load(struct snd_soc_component *cmpnt,
362115ecaba9SKranthi G 				struct snd_soc_tplg_manifest *manifest)
362215ecaba9SKranthi G {
3623*76f56faeSRakesh Ughreja 	struct hdac_bus *bus = snd_soc_component_get_drvdata(cmpnt);
3624*76f56faeSRakesh Ughreja 	struct skl *skl = bus_to_skl(bus);
362515ecaba9SKranthi G 
3626c15ad605SVinod Koul 	/* proceed only if we have private data defined */
3627c15ad605SVinod Koul 	if (manifest->priv.size == 0)
3628c15ad605SVinod Koul 		return 0;
3629c15ad605SVinod Koul 
3630eee0e16fSJeeja KP 	skl_tplg_get_manifest_data(manifest, bus->dev, skl);
3631541070ceSShreyas NC 
3632eee0e16fSJeeja KP 	if (skl->skl_sst->lib_count > SKL_MAX_LIB) {
363315ecaba9SKranthi G 		dev_err(bus->dev, "Exceeding max Library count. Got:%d\n",
3634eee0e16fSJeeja KP 					skl->skl_sst->lib_count);
3635eee0e16fSJeeja KP 		return  -EINVAL;
363615ecaba9SKranthi G 	}
363715ecaba9SKranthi G 
3638eee0e16fSJeeja KP 	return 0;
363915ecaba9SKranthi G }
364015ecaba9SKranthi G 
36413af36706SVinod Koul static struct snd_soc_tplg_ops skl_tplg_ops  = {
36423af36706SVinod Koul 	.widget_load = skl_tplg_widget_load,
3643140adfbaSJeeja KP 	.control_load = skl_tplg_control_load,
3644140adfbaSJeeja KP 	.bytes_ext_ops = skl_tlv_ops,
3645140adfbaSJeeja KP 	.bytes_ext_ops_count = ARRAY_SIZE(skl_tlv_ops),
36467a1b749bSDharageswari R 	.io_ops = skl_tplg_kcontrol_ops,
36477a1b749bSDharageswari R 	.io_ops_count = ARRAY_SIZE(skl_tplg_kcontrol_ops),
364815ecaba9SKranthi G 	.manifest = skl_manifest_load,
3649606e21fdSGuneshwor Singh 	.dai_load = skl_dai_load,
36503af36706SVinod Koul };
36513af36706SVinod Koul 
3652287af4f9SJeeja KP /*
3653287af4f9SJeeja KP  * A pipe can have multiple modules, each of them will be a DAPM widget as
3654287af4f9SJeeja KP  * well. While managing a pipeline we need to get the list of all the
3655287af4f9SJeeja KP  * widgets in a pipelines, so this helper - skl_tplg_create_pipe_widget_list()
3656287af4f9SJeeja KP  * helps to get the SKL type widgets in that pipeline
3657287af4f9SJeeja KP  */
365856b03b4cSKuninori Morimoto static int skl_tplg_create_pipe_widget_list(struct snd_soc_component *component)
3659287af4f9SJeeja KP {
3660287af4f9SJeeja KP 	struct snd_soc_dapm_widget *w;
3661287af4f9SJeeja KP 	struct skl_module_cfg *mcfg = NULL;
3662287af4f9SJeeja KP 	struct skl_pipe_module *p_module = NULL;
3663287af4f9SJeeja KP 	struct skl_pipe *pipe;
3664287af4f9SJeeja KP 
366556b03b4cSKuninori Morimoto 	list_for_each_entry(w, &component->card->widgets, list) {
3666a1f362d8SMark Brown 		if (is_skl_dsp_widget_type(w, component->dev) && w->priv) {
3667287af4f9SJeeja KP 			mcfg = w->priv;
3668287af4f9SJeeja KP 			pipe = mcfg->pipe;
3669287af4f9SJeeja KP 
367056b03b4cSKuninori Morimoto 			p_module = devm_kzalloc(component->dev,
3671287af4f9SJeeja KP 						sizeof(*p_module), GFP_KERNEL);
3672287af4f9SJeeja KP 			if (!p_module)
3673287af4f9SJeeja KP 				return -ENOMEM;
3674287af4f9SJeeja KP 
3675287af4f9SJeeja KP 			p_module->w = w;
3676287af4f9SJeeja KP 			list_add_tail(&p_module->node, &pipe->w_list);
3677287af4f9SJeeja KP 		}
3678287af4f9SJeeja KP 	}
3679287af4f9SJeeja KP 
3680287af4f9SJeeja KP 	return 0;
3681287af4f9SJeeja KP }
3682287af4f9SJeeja KP 
3683f0aa94faSJeeja KP static void skl_tplg_set_pipe_type(struct skl *skl, struct skl_pipe *pipe)
3684f0aa94faSJeeja KP {
3685f0aa94faSJeeja KP 	struct skl_pipe_module *w_module;
3686f0aa94faSJeeja KP 	struct snd_soc_dapm_widget *w;
3687f0aa94faSJeeja KP 	struct skl_module_cfg *mconfig;
3688f0aa94faSJeeja KP 	bool host_found = false, link_found = false;
3689f0aa94faSJeeja KP 
3690f0aa94faSJeeja KP 	list_for_each_entry(w_module, &pipe->w_list, node) {
3691f0aa94faSJeeja KP 		w = w_module->w;
3692f0aa94faSJeeja KP 		mconfig = w->priv;
3693f0aa94faSJeeja KP 
3694f0aa94faSJeeja KP 		if (mconfig->dev_type == SKL_DEVICE_HDAHOST)
3695f0aa94faSJeeja KP 			host_found = true;
3696f0aa94faSJeeja KP 		else if (mconfig->dev_type != SKL_DEVICE_NONE)
3697f0aa94faSJeeja KP 			link_found = true;
3698f0aa94faSJeeja KP 	}
3699f0aa94faSJeeja KP 
3700f0aa94faSJeeja KP 	if (host_found && link_found)
3701f0aa94faSJeeja KP 		pipe->passthru = true;
3702f0aa94faSJeeja KP 	else
3703f0aa94faSJeeja KP 		pipe->passthru = false;
3704f0aa94faSJeeja KP }
3705f0aa94faSJeeja KP 
37063af36706SVinod Koul /* This will be read from topology manifest, currently defined here */
37073af36706SVinod Koul #define SKL_MAX_MCPS 30000000
37083af36706SVinod Koul #define SKL_FW_MAX_MEM 1000000
37093af36706SVinod Koul 
37103af36706SVinod Koul /*
37113af36706SVinod Koul  * SKL topology init routine
37123af36706SVinod Koul  */
3713*76f56faeSRakesh Ughreja int skl_tplg_init(struct snd_soc_component *component, struct hdac_bus *bus)
37143af36706SVinod Koul {
37153af36706SVinod Koul 	int ret;
37163af36706SVinod Koul 	const struct firmware *fw;
3717*76f56faeSRakesh Ughreja 	struct skl *skl = bus_to_skl(bus);
3718f0aa94faSJeeja KP 	struct skl_pipeline *ppl;
37193af36706SVinod Koul 
37204b235c43SVinod Koul 	ret = request_firmware(&fw, skl->tplg_name, bus->dev);
37213af36706SVinod Koul 	if (ret < 0) {
372219de7179SChintan Patel 		dev_info(bus->dev, "tplg fw %s load failed with %d, falling back to dfw_sst.bin",
37234b235c43SVinod Koul 				skl->tplg_name, ret);
37244b235c43SVinod Koul 		ret = request_firmware(&fw, "dfw_sst.bin", bus->dev);
37254b235c43SVinod Koul 		if (ret < 0) {
37264b235c43SVinod Koul 			dev_err(bus->dev, "Fallback tplg fw %s load failed with %d\n",
37273af36706SVinod Koul 					"dfw_sst.bin", ret);
37283af36706SVinod Koul 			return ret;
37293af36706SVinod Koul 		}
37304b235c43SVinod Koul 	}
37313af36706SVinod Koul 
37323af36706SVinod Koul 	/*
37333af36706SVinod Koul 	 * The complete tplg for SKL is loaded as index 0, we don't use
37343af36706SVinod Koul 	 * any other index
37353af36706SVinod Koul 	 */
373656b03b4cSKuninori Morimoto 	ret = snd_soc_tplg_component_load(component,
3737b663a8c5SJeeja KP 					&skl_tplg_ops, fw, 0);
37383af36706SVinod Koul 	if (ret < 0) {
37393af36706SVinod Koul 		dev_err(bus->dev, "tplg component load failed%d\n", ret);
3740c14a82c7SSudip Mukherjee 		release_firmware(fw);
37413af36706SVinod Koul 		return -EINVAL;
37423af36706SVinod Koul 	}
37433af36706SVinod Koul 
37443af36706SVinod Koul 	skl->resource.max_mcps = SKL_MAX_MCPS;
37453af36706SVinod Koul 	skl->resource.max_mem = SKL_FW_MAX_MEM;
37463af36706SVinod Koul 
3747d8018361SVinod Koul 	skl->tplg = fw;
374856b03b4cSKuninori Morimoto 	ret = skl_tplg_create_pipe_widget_list(component);
3749287af4f9SJeeja KP 	if (ret < 0)
3750287af4f9SJeeja KP 		return ret;
3751d8018361SVinod Koul 
3752f0aa94faSJeeja KP 	list_for_each_entry(ppl, &skl->ppl_list, node)
3753f0aa94faSJeeja KP 		skl_tplg_set_pipe_type(skl, ppl->pipe);
37543af36706SVinod Koul 
37553af36706SVinod Koul 	return 0;
3756e4e2d2f4SJeeja KP }
3757