xref: /openbmc/linux/sound/soc/intel/skylake/skl-topology.c (revision d1a6fe41d3c4ff0d26f0b186d774493555ca5282)
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>
22e4e2d2f4SJeeja KP #include <sound/soc.h>
23e4e2d2f4SJeeja KP #include <sound/soc-topology.h>
246277e832SShreyas NC #include <uapi/sound/snd_sst_tokens.h>
25e4e2d2f4SJeeja KP #include "skl-sst-dsp.h"
26e4e2d2f4SJeeja KP #include "skl-sst-ipc.h"
27e4e2d2f4SJeeja KP #include "skl-topology.h"
28e4e2d2f4SJeeja KP #include "skl.h"
29e4e2d2f4SJeeja KP #include "skl-tplg-interface.h"
306c5768b3SDharageswari R #include "../common/sst-dsp.h"
316c5768b3SDharageswari R #include "../common/sst-dsp-priv.h"
32e4e2d2f4SJeeja KP 
33f7590d4fSJeeja KP #define SKL_CH_FIXUP_MASK		(1 << 0)
34f7590d4fSJeeja KP #define SKL_RATE_FIXUP_MASK		(1 << 1)
35f7590d4fSJeeja KP #define SKL_FMT_FIXUP_MASK		(1 << 2)
366277e832SShreyas NC #define SKL_IN_DIR_BIT_MASK		BIT(0)
376277e832SShreyas NC #define SKL_PIN_COUNT_MASK		GENMASK(7, 4)
38f7590d4fSJeeja KP 
39a83e3b4cSVinod Koul void skl_tplg_d0i3_get(struct skl *skl, enum d0i3_capability caps)
40a83e3b4cSVinod Koul {
41a83e3b4cSVinod Koul 	struct skl_d0i3_data *d0i3 =  &skl->skl_sst->d0i3;
42a83e3b4cSVinod Koul 
43a83e3b4cSVinod Koul 	switch (caps) {
44a83e3b4cSVinod Koul 	case SKL_D0I3_NONE:
45a83e3b4cSVinod Koul 		d0i3->non_d0i3++;
46a83e3b4cSVinod Koul 		break;
47a83e3b4cSVinod Koul 
48a83e3b4cSVinod Koul 	case SKL_D0I3_STREAMING:
49a83e3b4cSVinod Koul 		d0i3->streaming++;
50a83e3b4cSVinod Koul 		break;
51a83e3b4cSVinod Koul 
52a83e3b4cSVinod Koul 	case SKL_D0I3_NON_STREAMING:
53a83e3b4cSVinod Koul 		d0i3->non_streaming++;
54a83e3b4cSVinod Koul 		break;
55a83e3b4cSVinod Koul 	}
56a83e3b4cSVinod Koul }
57a83e3b4cSVinod Koul 
58a83e3b4cSVinod Koul void skl_tplg_d0i3_put(struct skl *skl, enum d0i3_capability caps)
59a83e3b4cSVinod Koul {
60a83e3b4cSVinod Koul 	struct skl_d0i3_data *d0i3 =  &skl->skl_sst->d0i3;
61a83e3b4cSVinod Koul 
62a83e3b4cSVinod Koul 	switch (caps) {
63a83e3b4cSVinod Koul 	case SKL_D0I3_NONE:
64a83e3b4cSVinod Koul 		d0i3->non_d0i3--;
65a83e3b4cSVinod Koul 		break;
66a83e3b4cSVinod Koul 
67a83e3b4cSVinod Koul 	case SKL_D0I3_STREAMING:
68a83e3b4cSVinod Koul 		d0i3->streaming--;
69a83e3b4cSVinod Koul 		break;
70a83e3b4cSVinod Koul 
71a83e3b4cSVinod Koul 	case SKL_D0I3_NON_STREAMING:
72a83e3b4cSVinod Koul 		d0i3->non_streaming--;
73a83e3b4cSVinod Koul 		break;
74a83e3b4cSVinod Koul 	}
75a83e3b4cSVinod Koul }
76a83e3b4cSVinod Koul 
77e4e2d2f4SJeeja KP /*
78e4e2d2f4SJeeja KP  * SKL DSP driver modelling uses only few DAPM widgets so for rest we will
79e4e2d2f4SJeeja KP  * ignore. This helpers checks if the SKL driver handles this widget type
80e4e2d2f4SJeeja KP  */
81e4e2d2f4SJeeja KP static int is_skl_dsp_widget_type(struct snd_soc_dapm_widget *w)
82e4e2d2f4SJeeja KP {
83e4e2d2f4SJeeja KP 	switch (w->id) {
84e4e2d2f4SJeeja KP 	case snd_soc_dapm_dai_link:
85e4e2d2f4SJeeja KP 	case snd_soc_dapm_dai_in:
86e4e2d2f4SJeeja KP 	case snd_soc_dapm_aif_in:
87e4e2d2f4SJeeja KP 	case snd_soc_dapm_aif_out:
88e4e2d2f4SJeeja KP 	case snd_soc_dapm_dai_out:
89e4e2d2f4SJeeja KP 	case snd_soc_dapm_switch:
90e4e2d2f4SJeeja KP 		return false;
91e4e2d2f4SJeeja KP 	default:
92e4e2d2f4SJeeja KP 		return true;
93e4e2d2f4SJeeja KP 	}
94e4e2d2f4SJeeja KP }
95e4e2d2f4SJeeja KP 
96e4e2d2f4SJeeja KP /*
97e4e2d2f4SJeeja KP  * Each pipelines needs memory to be allocated. Check if we have free memory
989ba8ffefSDharageswari.R  * from available pool.
99e4e2d2f4SJeeja KP  */
1009ba8ffefSDharageswari.R static bool skl_is_pipe_mem_avail(struct skl *skl,
101e4e2d2f4SJeeja KP 				struct skl_module_cfg *mconfig)
102e4e2d2f4SJeeja KP {
103e4e2d2f4SJeeja KP 	struct skl_sst *ctx = skl->skl_sst;
104e4e2d2f4SJeeja KP 
105e4e2d2f4SJeeja KP 	if (skl->resource.mem + mconfig->pipe->memory_pages >
106e4e2d2f4SJeeja KP 				skl->resource.max_mem) {
107e4e2d2f4SJeeja KP 		dev_err(ctx->dev,
108e4e2d2f4SJeeja KP 				"%s: module_id %d instance %d\n", __func__,
109e4e2d2f4SJeeja KP 				mconfig->id.module_id,
110e4e2d2f4SJeeja KP 				mconfig->id.instance_id);
111e4e2d2f4SJeeja KP 		dev_err(ctx->dev,
112e4e2d2f4SJeeja KP 				"exceeds ppl memory available %d mem %d\n",
113e4e2d2f4SJeeja KP 				skl->resource.max_mem, skl->resource.mem);
114e4e2d2f4SJeeja KP 		return false;
1159ba8ffefSDharageswari.R 	} else {
1169ba8ffefSDharageswari.R 		return true;
1179ba8ffefSDharageswari.R 	}
118e4e2d2f4SJeeja KP }
119e4e2d2f4SJeeja KP 
1209ba8ffefSDharageswari.R /*
1219ba8ffefSDharageswari.R  * Add the mem to the mem pool. This is freed when pipe is deleted.
1229ba8ffefSDharageswari.R  * Note: DSP does actual memory management we only keep track for complete
1239ba8ffefSDharageswari.R  * pool
1249ba8ffefSDharageswari.R  */
1259ba8ffefSDharageswari.R static void skl_tplg_alloc_pipe_mem(struct skl *skl,
1269ba8ffefSDharageswari.R 				struct skl_module_cfg *mconfig)
1279ba8ffefSDharageswari.R {
128e4e2d2f4SJeeja KP 	skl->resource.mem += mconfig->pipe->memory_pages;
129e4e2d2f4SJeeja KP }
130e4e2d2f4SJeeja KP 
131e4e2d2f4SJeeja KP /*
132e4e2d2f4SJeeja KP  * Pipeline needs needs DSP CPU resources for computation, this is
133e4e2d2f4SJeeja KP  * quantified in MCPS (Million Clocks Per Second) required for module/pipe
134e4e2d2f4SJeeja KP  *
135e4e2d2f4SJeeja KP  * Each pipelines needs mcps to be allocated. Check if we have mcps for this
1369ba8ffefSDharageswari.R  * pipe.
137e4e2d2f4SJeeja KP  */
1389ba8ffefSDharageswari.R 
1399ba8ffefSDharageswari.R static bool skl_is_pipe_mcps_avail(struct skl *skl,
140e4e2d2f4SJeeja KP 				struct skl_module_cfg *mconfig)
141e4e2d2f4SJeeja KP {
142e4e2d2f4SJeeja KP 	struct skl_sst *ctx = skl->skl_sst;
143e4e2d2f4SJeeja KP 
144e4e2d2f4SJeeja KP 	if (skl->resource.mcps + mconfig->mcps > skl->resource.max_mcps) {
145e4e2d2f4SJeeja KP 		dev_err(ctx->dev,
146e4e2d2f4SJeeja KP 			"%s: module_id %d instance %d\n", __func__,
147e4e2d2f4SJeeja KP 			mconfig->id.module_id, mconfig->id.instance_id);
148e4e2d2f4SJeeja KP 		dev_err(ctx->dev,
1497ca42f5aSGuneshwor Singh 			"exceeds ppl mcps available %d > mem %d\n",
150e4e2d2f4SJeeja KP 			skl->resource.max_mcps, skl->resource.mcps);
151e4e2d2f4SJeeja KP 		return false;
1529ba8ffefSDharageswari.R 	} else {
1539ba8ffefSDharageswari.R 		return true;
1549ba8ffefSDharageswari.R 	}
155e4e2d2f4SJeeja KP }
156e4e2d2f4SJeeja KP 
1579ba8ffefSDharageswari.R static void skl_tplg_alloc_pipe_mcps(struct skl *skl,
1589ba8ffefSDharageswari.R 				struct skl_module_cfg *mconfig)
1599ba8ffefSDharageswari.R {
160e4e2d2f4SJeeja KP 	skl->resource.mcps += mconfig->mcps;
161e4e2d2f4SJeeja KP }
162e4e2d2f4SJeeja KP 
163e4e2d2f4SJeeja KP /*
164e4e2d2f4SJeeja KP  * Free the mcps when tearing down
165e4e2d2f4SJeeja KP  */
166e4e2d2f4SJeeja KP static void
167e4e2d2f4SJeeja KP skl_tplg_free_pipe_mcps(struct skl *skl, struct skl_module_cfg *mconfig)
168e4e2d2f4SJeeja KP {
169e4e2d2f4SJeeja KP 	skl->resource.mcps -= mconfig->mcps;
170e4e2d2f4SJeeja KP }
171e4e2d2f4SJeeja KP 
172e4e2d2f4SJeeja KP /*
173e4e2d2f4SJeeja KP  * Free the memory when tearing down
174e4e2d2f4SJeeja KP  */
175e4e2d2f4SJeeja KP static void
176e4e2d2f4SJeeja KP skl_tplg_free_pipe_mem(struct skl *skl, struct skl_module_cfg *mconfig)
177e4e2d2f4SJeeja KP {
178e4e2d2f4SJeeja KP 	skl->resource.mem -= mconfig->pipe->memory_pages;
179e4e2d2f4SJeeja KP }
180e4e2d2f4SJeeja KP 
181f7590d4fSJeeja KP 
182f7590d4fSJeeja KP static void skl_dump_mconfig(struct skl_sst *ctx,
183f7590d4fSJeeja KP 					struct skl_module_cfg *mcfg)
184f7590d4fSJeeja KP {
185f7590d4fSJeeja KP 	dev_dbg(ctx->dev, "Dumping config\n");
186f7590d4fSJeeja KP 	dev_dbg(ctx->dev, "Input Format:\n");
1874cd9899fSHardik T Shah 	dev_dbg(ctx->dev, "channels = %d\n", mcfg->in_fmt[0].channels);
1884cd9899fSHardik T Shah 	dev_dbg(ctx->dev, "s_freq = %d\n", mcfg->in_fmt[0].s_freq);
1894cd9899fSHardik T Shah 	dev_dbg(ctx->dev, "ch_cfg = %d\n", mcfg->in_fmt[0].ch_cfg);
1904cd9899fSHardik T Shah 	dev_dbg(ctx->dev, "valid bit depth = %d\n", mcfg->in_fmt[0].valid_bit_depth);
191f7590d4fSJeeja KP 	dev_dbg(ctx->dev, "Output Format:\n");
1924cd9899fSHardik T Shah 	dev_dbg(ctx->dev, "channels = %d\n", mcfg->out_fmt[0].channels);
1934cd9899fSHardik T Shah 	dev_dbg(ctx->dev, "s_freq = %d\n", mcfg->out_fmt[0].s_freq);
1944cd9899fSHardik T Shah 	dev_dbg(ctx->dev, "valid bit depth = %d\n", mcfg->out_fmt[0].valid_bit_depth);
1954cd9899fSHardik T Shah 	dev_dbg(ctx->dev, "ch_cfg = %d\n", mcfg->out_fmt[0].ch_cfg);
196f7590d4fSJeeja KP }
197f7590d4fSJeeja KP 
198ea5a137dSSubhransu S. Prusty static void skl_tplg_update_chmap(struct skl_module_fmt *fmt, int chs)
199ea5a137dSSubhransu S. Prusty {
200ea5a137dSSubhransu S. Prusty 	int slot_map = 0xFFFFFFFF;
201ea5a137dSSubhransu S. Prusty 	int start_slot = 0;
202ea5a137dSSubhransu S. Prusty 	int i;
203ea5a137dSSubhransu S. Prusty 
204ea5a137dSSubhransu S. Prusty 	for (i = 0; i < chs; i++) {
205ea5a137dSSubhransu S. Prusty 		/*
206ea5a137dSSubhransu S. Prusty 		 * For 2 channels with starting slot as 0, slot map will
207ea5a137dSSubhransu S. Prusty 		 * look like 0xFFFFFF10.
208ea5a137dSSubhransu S. Prusty 		 */
209ea5a137dSSubhransu S. Prusty 		slot_map &= (~(0xF << (4 * i)) | (start_slot << (4 * i)));
210ea5a137dSSubhransu S. Prusty 		start_slot++;
211ea5a137dSSubhransu S. Prusty 	}
212ea5a137dSSubhransu S. Prusty 	fmt->ch_map = slot_map;
213ea5a137dSSubhransu S. Prusty }
214ea5a137dSSubhransu S. Prusty 
215f7590d4fSJeeja KP static void skl_tplg_update_params(struct skl_module_fmt *fmt,
216f7590d4fSJeeja KP 			struct skl_pipe_params *params, int fixup)
217f7590d4fSJeeja KP {
218f7590d4fSJeeja KP 	if (fixup & SKL_RATE_FIXUP_MASK)
219f7590d4fSJeeja KP 		fmt->s_freq = params->s_freq;
220ea5a137dSSubhransu S. Prusty 	if (fixup & SKL_CH_FIXUP_MASK) {
221f7590d4fSJeeja KP 		fmt->channels = params->ch;
222ea5a137dSSubhransu S. Prusty 		skl_tplg_update_chmap(fmt, fmt->channels);
223ea5a137dSSubhransu S. Prusty 	}
22498256f83SJeeja KP 	if (fixup & SKL_FMT_FIXUP_MASK) {
22598256f83SJeeja KP 		fmt->valid_bit_depth = skl_get_bit_depth(params->s_fmt);
22698256f83SJeeja KP 
22798256f83SJeeja KP 		/*
22898256f83SJeeja KP 		 * 16 bit is 16 bit container whereas 24 bit is in 32 bit
22998256f83SJeeja KP 		 * container so update bit depth accordingly
23098256f83SJeeja KP 		 */
23198256f83SJeeja KP 		switch (fmt->valid_bit_depth) {
23298256f83SJeeja KP 		case SKL_DEPTH_16BIT:
23398256f83SJeeja KP 			fmt->bit_depth = fmt->valid_bit_depth;
23498256f83SJeeja KP 			break;
23598256f83SJeeja KP 
23698256f83SJeeja KP 		default:
23798256f83SJeeja KP 			fmt->bit_depth = SKL_DEPTH_32BIT;
23898256f83SJeeja KP 			break;
23998256f83SJeeja KP 		}
24098256f83SJeeja KP 	}
24198256f83SJeeja KP 
242f7590d4fSJeeja KP }
243f7590d4fSJeeja KP 
244f7590d4fSJeeja KP /*
245f7590d4fSJeeja KP  * A pipeline may have modules which impact the pcm parameters, like SRC,
246f7590d4fSJeeja KP  * channel converter, format converter.
247f7590d4fSJeeja KP  * We need to calculate the output params by applying the 'fixup'
248f7590d4fSJeeja KP  * Topology will tell driver which type of fixup is to be applied by
249f7590d4fSJeeja KP  * supplying the fixup mask, so based on that we calculate the output
250f7590d4fSJeeja KP  *
251f7590d4fSJeeja KP  * Now In FE the pcm hw_params is source/target format. Same is applicable
252f7590d4fSJeeja KP  * for BE with its hw_params invoked.
253f7590d4fSJeeja KP  * here based on FE, BE pipeline and direction we calculate the input and
254f7590d4fSJeeja KP  * outfix and then apply that for a module
255f7590d4fSJeeja KP  */
256f7590d4fSJeeja KP static void skl_tplg_update_params_fixup(struct skl_module_cfg *m_cfg,
257f7590d4fSJeeja KP 		struct skl_pipe_params *params, bool is_fe)
258f7590d4fSJeeja KP {
259f7590d4fSJeeja KP 	int in_fixup, out_fixup;
260f7590d4fSJeeja KP 	struct skl_module_fmt *in_fmt, *out_fmt;
261f7590d4fSJeeja KP 
2624cd9899fSHardik T Shah 	/* Fixups will be applied to pin 0 only */
2634cd9899fSHardik T Shah 	in_fmt = &m_cfg->in_fmt[0];
2644cd9899fSHardik T Shah 	out_fmt = &m_cfg->out_fmt[0];
265f7590d4fSJeeja KP 
266f7590d4fSJeeja KP 	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
267f7590d4fSJeeja KP 		if (is_fe) {
268f7590d4fSJeeja KP 			in_fixup = m_cfg->params_fixup;
269f7590d4fSJeeja KP 			out_fixup = (~m_cfg->converter) &
270f7590d4fSJeeja KP 					m_cfg->params_fixup;
271f7590d4fSJeeja KP 		} else {
272f7590d4fSJeeja KP 			out_fixup = m_cfg->params_fixup;
273f7590d4fSJeeja KP 			in_fixup = (~m_cfg->converter) &
274f7590d4fSJeeja KP 					m_cfg->params_fixup;
275f7590d4fSJeeja KP 		}
276f7590d4fSJeeja KP 	} else {
277f7590d4fSJeeja KP 		if (is_fe) {
278f7590d4fSJeeja KP 			out_fixup = m_cfg->params_fixup;
279f7590d4fSJeeja KP 			in_fixup = (~m_cfg->converter) &
280f7590d4fSJeeja KP 					m_cfg->params_fixup;
281f7590d4fSJeeja KP 		} else {
282f7590d4fSJeeja KP 			in_fixup = m_cfg->params_fixup;
283f7590d4fSJeeja KP 			out_fixup = (~m_cfg->converter) &
284f7590d4fSJeeja KP 					m_cfg->params_fixup;
285f7590d4fSJeeja KP 		}
286f7590d4fSJeeja KP 	}
287f7590d4fSJeeja KP 
288f7590d4fSJeeja KP 	skl_tplg_update_params(in_fmt, params, in_fixup);
289f7590d4fSJeeja KP 	skl_tplg_update_params(out_fmt, params, out_fixup);
290f7590d4fSJeeja KP }
291f7590d4fSJeeja KP 
292f7590d4fSJeeja KP /*
293f7590d4fSJeeja KP  * A module needs input and output buffers, which are dependent upon pcm
294f7590d4fSJeeja KP  * params, so once we have calculate params, we need buffer calculation as
295f7590d4fSJeeja KP  * well.
296f7590d4fSJeeja KP  */
297f7590d4fSJeeja KP static void skl_tplg_update_buffer_size(struct skl_sst *ctx,
298f7590d4fSJeeja KP 				struct skl_module_cfg *mcfg)
299f7590d4fSJeeja KP {
300f7590d4fSJeeja KP 	int multiplier = 1;
3014cd9899fSHardik T Shah 	struct skl_module_fmt *in_fmt, *out_fmt;
302f0c8e1d9SSubhransu S. Prusty 	int in_rate, out_rate;
3034cd9899fSHardik T Shah 
3044cd9899fSHardik T Shah 
3054cd9899fSHardik T Shah 	/* Since fixups is applied to pin 0 only, ibs, obs needs
3064cd9899fSHardik T Shah 	 * change for pin 0 only
3074cd9899fSHardik T Shah 	 */
3084cd9899fSHardik T Shah 	in_fmt = &mcfg->in_fmt[0];
3094cd9899fSHardik T Shah 	out_fmt = &mcfg->out_fmt[0];
310f7590d4fSJeeja KP 
311f7590d4fSJeeja KP 	if (mcfg->m_type == SKL_MODULE_TYPE_SRCINT)
312f7590d4fSJeeja KP 		multiplier = 5;
313f0c8e1d9SSubhransu S. Prusty 
314f0c8e1d9SSubhransu S. Prusty 	if (in_fmt->s_freq % 1000)
315f0c8e1d9SSubhransu S. Prusty 		in_rate = (in_fmt->s_freq / 1000) + 1;
316f0c8e1d9SSubhransu S. Prusty 	else
317f0c8e1d9SSubhransu S. Prusty 		in_rate = (in_fmt->s_freq / 1000);
318f0c8e1d9SSubhransu S. Prusty 
319f0c8e1d9SSubhransu S. Prusty 	mcfg->ibs = in_rate * (mcfg->in_fmt->channels) *
3204cd9899fSHardik T Shah 			(mcfg->in_fmt->bit_depth >> 3) *
321f7590d4fSJeeja KP 			multiplier;
322f7590d4fSJeeja KP 
323f0c8e1d9SSubhransu S. Prusty 	if (mcfg->out_fmt->s_freq % 1000)
324f0c8e1d9SSubhransu S. Prusty 		out_rate = (mcfg->out_fmt->s_freq / 1000) + 1;
325f0c8e1d9SSubhransu S. Prusty 	else
326f0c8e1d9SSubhransu S. Prusty 		out_rate = (mcfg->out_fmt->s_freq / 1000);
327f0c8e1d9SSubhransu S. Prusty 
328f0c8e1d9SSubhransu S. Prusty 	mcfg->obs = out_rate * (mcfg->out_fmt->channels) *
3294cd9899fSHardik T Shah 			(mcfg->out_fmt->bit_depth >> 3) *
330f7590d4fSJeeja KP 			multiplier;
331f7590d4fSJeeja KP }
332f7590d4fSJeeja KP 
333db2f586bSSenthilnathan Veppur static u8 skl_tplg_be_dev_type(int dev_type)
334db2f586bSSenthilnathan Veppur {
335db2f586bSSenthilnathan Veppur 	int ret;
336db2f586bSSenthilnathan Veppur 
337db2f586bSSenthilnathan Veppur 	switch (dev_type) {
338db2f586bSSenthilnathan Veppur 	case SKL_DEVICE_BT:
339db2f586bSSenthilnathan Veppur 		ret = NHLT_DEVICE_BT;
340db2f586bSSenthilnathan Veppur 		break;
341db2f586bSSenthilnathan Veppur 
342db2f586bSSenthilnathan Veppur 	case SKL_DEVICE_DMIC:
343db2f586bSSenthilnathan Veppur 		ret = NHLT_DEVICE_DMIC;
344db2f586bSSenthilnathan Veppur 		break;
345db2f586bSSenthilnathan Veppur 
346db2f586bSSenthilnathan Veppur 	case SKL_DEVICE_I2S:
347db2f586bSSenthilnathan Veppur 		ret = NHLT_DEVICE_I2S;
348db2f586bSSenthilnathan Veppur 		break;
349db2f586bSSenthilnathan Veppur 
350db2f586bSSenthilnathan Veppur 	default:
351db2f586bSSenthilnathan Veppur 		ret = NHLT_DEVICE_INVALID;
352db2f586bSSenthilnathan Veppur 		break;
353db2f586bSSenthilnathan Veppur 	}
354db2f586bSSenthilnathan Veppur 
355db2f586bSSenthilnathan Veppur 	return ret;
356db2f586bSSenthilnathan Veppur }
357db2f586bSSenthilnathan Veppur 
3582d1419a3SJeeja KP static int skl_tplg_update_be_blob(struct snd_soc_dapm_widget *w,
3592d1419a3SJeeja KP 						struct skl_sst *ctx)
3602d1419a3SJeeja KP {
3612d1419a3SJeeja KP 	struct skl_module_cfg *m_cfg = w->priv;
3622d1419a3SJeeja KP 	int link_type, dir;
3632d1419a3SJeeja KP 	u32 ch, s_freq, s_fmt;
3642d1419a3SJeeja KP 	struct nhlt_specific_cfg *cfg;
3652d1419a3SJeeja KP 	struct skl *skl = get_skl_ctx(ctx->dev);
366db2f586bSSenthilnathan Veppur 	u8 dev_type = skl_tplg_be_dev_type(m_cfg->dev_type);
3672d1419a3SJeeja KP 
3682d1419a3SJeeja KP 	/* check if we already have blob */
3692d1419a3SJeeja KP 	if (m_cfg->formats_config.caps_size > 0)
3702d1419a3SJeeja KP 		return 0;
3712d1419a3SJeeja KP 
372c7c6c736SJeeja KP 	dev_dbg(ctx->dev, "Applying default cfg blob\n");
3732d1419a3SJeeja KP 	switch (m_cfg->dev_type) {
3742d1419a3SJeeja KP 	case SKL_DEVICE_DMIC:
3752d1419a3SJeeja KP 		link_type = NHLT_LINK_DMIC;
376c7c6c736SJeeja KP 		dir = SNDRV_PCM_STREAM_CAPTURE;
3772d1419a3SJeeja KP 		s_freq = m_cfg->in_fmt[0].s_freq;
3782d1419a3SJeeja KP 		s_fmt = m_cfg->in_fmt[0].bit_depth;
3792d1419a3SJeeja KP 		ch = m_cfg->in_fmt[0].channels;
3802d1419a3SJeeja KP 		break;
3812d1419a3SJeeja KP 
3822d1419a3SJeeja KP 	case SKL_DEVICE_I2S:
3832d1419a3SJeeja KP 		link_type = NHLT_LINK_SSP;
3842d1419a3SJeeja KP 		if (m_cfg->hw_conn_type == SKL_CONN_SOURCE) {
385c7c6c736SJeeja KP 			dir = SNDRV_PCM_STREAM_PLAYBACK;
3862d1419a3SJeeja KP 			s_freq = m_cfg->out_fmt[0].s_freq;
3872d1419a3SJeeja KP 			s_fmt = m_cfg->out_fmt[0].bit_depth;
3882d1419a3SJeeja KP 			ch = m_cfg->out_fmt[0].channels;
389c7c6c736SJeeja KP 		} else {
390c7c6c736SJeeja KP 			dir = SNDRV_PCM_STREAM_CAPTURE;
391c7c6c736SJeeja KP 			s_freq = m_cfg->in_fmt[0].s_freq;
392c7c6c736SJeeja KP 			s_fmt = m_cfg->in_fmt[0].bit_depth;
393c7c6c736SJeeja KP 			ch = m_cfg->in_fmt[0].channels;
3942d1419a3SJeeja KP 		}
3952d1419a3SJeeja KP 		break;
3962d1419a3SJeeja KP 
3972d1419a3SJeeja KP 	default:
3982d1419a3SJeeja KP 		return -EINVAL;
3992d1419a3SJeeja KP 	}
4002d1419a3SJeeja KP 
4012d1419a3SJeeja KP 	/* update the blob based on virtual bus_id and default params */
4022d1419a3SJeeja KP 	cfg = skl_get_ep_blob(skl, m_cfg->vbus_id, link_type,
403db2f586bSSenthilnathan Veppur 					s_fmt, ch, s_freq, dir, dev_type);
4042d1419a3SJeeja KP 	if (cfg) {
4052d1419a3SJeeja KP 		m_cfg->formats_config.caps_size = cfg->size;
4062d1419a3SJeeja KP 		m_cfg->formats_config.caps = (u32 *) &cfg->caps;
4072d1419a3SJeeja KP 	} else {
4082d1419a3SJeeja KP 		dev_err(ctx->dev, "Blob NULL for id %x type %d dirn %d\n",
4092d1419a3SJeeja KP 					m_cfg->vbus_id, link_type, dir);
4102d1419a3SJeeja KP 		dev_err(ctx->dev, "PCM: ch %d, freq %d, fmt %d\n",
4112d1419a3SJeeja KP 					ch, s_freq, s_fmt);
4122d1419a3SJeeja KP 		return -EIO;
4132d1419a3SJeeja KP 	}
4142d1419a3SJeeja KP 
4152d1419a3SJeeja KP 	return 0;
4162d1419a3SJeeja KP }
4172d1419a3SJeeja KP 
418f7590d4fSJeeja KP static void skl_tplg_update_module_params(struct snd_soc_dapm_widget *w,
419f7590d4fSJeeja KP 							struct skl_sst *ctx)
420f7590d4fSJeeja KP {
421f7590d4fSJeeja KP 	struct skl_module_cfg *m_cfg = w->priv;
422f7590d4fSJeeja KP 	struct skl_pipe_params *params = m_cfg->pipe->p_params;
423f7590d4fSJeeja KP 	int p_conn_type = m_cfg->pipe->conn_type;
424f7590d4fSJeeja KP 	bool is_fe;
425f7590d4fSJeeja KP 
426f7590d4fSJeeja KP 	if (!m_cfg->params_fixup)
427f7590d4fSJeeja KP 		return;
428f7590d4fSJeeja KP 
429f7590d4fSJeeja KP 	dev_dbg(ctx->dev, "Mconfig for widget=%s BEFORE updation\n",
430f7590d4fSJeeja KP 				w->name);
431f7590d4fSJeeja KP 
432f7590d4fSJeeja KP 	skl_dump_mconfig(ctx, m_cfg);
433f7590d4fSJeeja KP 
434f7590d4fSJeeja KP 	if (p_conn_type == SKL_PIPE_CONN_TYPE_FE)
435f7590d4fSJeeja KP 		is_fe = true;
436f7590d4fSJeeja KP 	else
437f7590d4fSJeeja KP 		is_fe = false;
438f7590d4fSJeeja KP 
439f7590d4fSJeeja KP 	skl_tplg_update_params_fixup(m_cfg, params, is_fe);
440f7590d4fSJeeja KP 	skl_tplg_update_buffer_size(ctx, m_cfg);
441f7590d4fSJeeja KP 
442f7590d4fSJeeja KP 	dev_dbg(ctx->dev, "Mconfig for widget=%s AFTER updation\n",
443f7590d4fSJeeja KP 				w->name);
444f7590d4fSJeeja KP 
445f7590d4fSJeeja KP 	skl_dump_mconfig(ctx, m_cfg);
446f7590d4fSJeeja KP }
447f7590d4fSJeeja KP 
448e4e2d2f4SJeeja KP /*
449abb74003SJeeja KP  * some modules can have multiple params set from user control and
450abb74003SJeeja KP  * need to be set after module is initialized. If set_param flag is
451abb74003SJeeja KP  * set module params will be done after module is initialised.
452abb74003SJeeja KP  */
453abb74003SJeeja KP static int skl_tplg_set_module_params(struct snd_soc_dapm_widget *w,
454abb74003SJeeja KP 						struct skl_sst *ctx)
455abb74003SJeeja KP {
456abb74003SJeeja KP 	int i, ret;
457abb74003SJeeja KP 	struct skl_module_cfg *mconfig = w->priv;
458abb74003SJeeja KP 	const struct snd_kcontrol_new *k;
459abb74003SJeeja KP 	struct soc_bytes_ext *sb;
460abb74003SJeeja KP 	struct skl_algo_data *bc;
461abb74003SJeeja KP 	struct skl_specific_cfg *sp_cfg;
462abb74003SJeeja KP 
463abb74003SJeeja KP 	if (mconfig->formats_config.caps_size > 0 &&
4644ced1827SJeeja KP 		mconfig->formats_config.set_params == SKL_PARAM_SET) {
465abb74003SJeeja KP 		sp_cfg = &mconfig->formats_config;
466abb74003SJeeja KP 		ret = skl_set_module_params(ctx, sp_cfg->caps,
467abb74003SJeeja KP 					sp_cfg->caps_size,
468abb74003SJeeja KP 					sp_cfg->param_id, mconfig);
469abb74003SJeeja KP 		if (ret < 0)
470abb74003SJeeja KP 			return ret;
471abb74003SJeeja KP 	}
472abb74003SJeeja KP 
473abb74003SJeeja KP 	for (i = 0; i < w->num_kcontrols; i++) {
474abb74003SJeeja KP 		k = &w->kcontrol_news[i];
475abb74003SJeeja KP 		if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
476abb74003SJeeja KP 			sb = (void *) k->private_value;
477abb74003SJeeja KP 			bc = (struct skl_algo_data *)sb->dobj.private;
478abb74003SJeeja KP 
4794ced1827SJeeja KP 			if (bc->set_params == SKL_PARAM_SET) {
480abb74003SJeeja KP 				ret = skl_set_module_params(ctx,
4810d682104SDharageswari R 						(u32 *)bc->params, bc->size,
482abb74003SJeeja KP 						bc->param_id, mconfig);
483abb74003SJeeja KP 				if (ret < 0)
484abb74003SJeeja KP 					return ret;
485abb74003SJeeja KP 			}
486abb74003SJeeja KP 		}
487abb74003SJeeja KP 	}
488abb74003SJeeja KP 
489abb74003SJeeja KP 	return 0;
490abb74003SJeeja KP }
491abb74003SJeeja KP 
492abb74003SJeeja KP /*
493abb74003SJeeja KP  * some module param can set from user control and this is required as
494abb74003SJeeja KP  * when module is initailzed. if module param is required in init it is
495abb74003SJeeja KP  * identifed by set_param flag. if set_param flag is not set, then this
496abb74003SJeeja KP  * parameter needs to set as part of module init.
497abb74003SJeeja KP  */
498abb74003SJeeja KP static int skl_tplg_set_module_init_data(struct snd_soc_dapm_widget *w)
499abb74003SJeeja KP {
500abb74003SJeeja KP 	const struct snd_kcontrol_new *k;
501abb74003SJeeja KP 	struct soc_bytes_ext *sb;
502abb74003SJeeja KP 	struct skl_algo_data *bc;
503abb74003SJeeja KP 	struct skl_module_cfg *mconfig = w->priv;
504abb74003SJeeja KP 	int i;
505abb74003SJeeja KP 
506abb74003SJeeja KP 	for (i = 0; i < w->num_kcontrols; i++) {
507abb74003SJeeja KP 		k = &w->kcontrol_news[i];
508abb74003SJeeja KP 		if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
509abb74003SJeeja KP 			sb = (struct soc_bytes_ext *)k->private_value;
510abb74003SJeeja KP 			bc = (struct skl_algo_data *)sb->dobj.private;
511abb74003SJeeja KP 
5124ced1827SJeeja KP 			if (bc->set_params != SKL_PARAM_INIT)
513abb74003SJeeja KP 				continue;
514abb74003SJeeja KP 
515*d1a6fe41STakashi Sakamoto 			mconfig->formats_config.caps = (u32 *)bc->params;
5160d682104SDharageswari R 			mconfig->formats_config.caps_size = bc->size;
517abb74003SJeeja KP 
518abb74003SJeeja KP 			break;
519abb74003SJeeja KP 		}
520abb74003SJeeja KP 	}
521abb74003SJeeja KP 
522abb74003SJeeja KP 	return 0;
523abb74003SJeeja KP }
524abb74003SJeeja KP 
525bb704a73SJeeja KP static int skl_tplg_module_prepare(struct skl_sst *ctx, struct skl_pipe *pipe,
526bb704a73SJeeja KP 		struct snd_soc_dapm_widget *w, struct skl_module_cfg *mcfg)
527bb704a73SJeeja KP {
528bb704a73SJeeja KP 	switch (mcfg->dev_type) {
529bb704a73SJeeja KP 	case SKL_DEVICE_HDAHOST:
530bb704a73SJeeja KP 		return skl_pcm_host_dma_prepare(ctx->dev, pipe->p_params);
531bb704a73SJeeja KP 
532bb704a73SJeeja KP 	case SKL_DEVICE_HDALINK:
533bb704a73SJeeja KP 		return skl_pcm_link_dma_prepare(ctx->dev, pipe->p_params);
534bb704a73SJeeja KP 	}
535bb704a73SJeeja KP 
536bb704a73SJeeja KP 	return 0;
537bb704a73SJeeja KP }
538bb704a73SJeeja KP 
539abb74003SJeeja KP /*
540e4e2d2f4SJeeja KP  * Inside a pipe instance, we can have various modules. These modules need
541e4e2d2f4SJeeja KP  * to instantiated in DSP by invoking INIT_MODULE IPC, which is achieved by
542e4e2d2f4SJeeja KP  * skl_init_module() routine, so invoke that for all modules in a pipeline
543e4e2d2f4SJeeja KP  */
544e4e2d2f4SJeeja KP static int
545e4e2d2f4SJeeja KP skl_tplg_init_pipe_modules(struct skl *skl, struct skl_pipe *pipe)
546e4e2d2f4SJeeja KP {
547e4e2d2f4SJeeja KP 	struct skl_pipe_module *w_module;
548e4e2d2f4SJeeja KP 	struct snd_soc_dapm_widget *w;
549e4e2d2f4SJeeja KP 	struct skl_module_cfg *mconfig;
550e4e2d2f4SJeeja KP 	struct skl_sst *ctx = skl->skl_sst;
551e4e2d2f4SJeeja KP 	int ret = 0;
552e4e2d2f4SJeeja KP 
553e4e2d2f4SJeeja KP 	list_for_each_entry(w_module, &pipe->w_list, node) {
554e4e2d2f4SJeeja KP 		w = w_module->w;
555e4e2d2f4SJeeja KP 		mconfig = w->priv;
556e4e2d2f4SJeeja KP 
557b7c50555SVinod Koul 		/* check if module ids are populated */
558b7c50555SVinod Koul 		if (mconfig->id.module_id < 0) {
559b7c50555SVinod Koul 			dev_err(skl->skl_sst->dev,
560a657ae7eSVinod Koul 					"module %pUL id not populated\n",
561a657ae7eSVinod Koul 					(uuid_le *)mconfig->guid);
562a657ae7eSVinod Koul 			return -EIO;
563b7c50555SVinod Koul 		}
564b7c50555SVinod Koul 
565e4e2d2f4SJeeja KP 		/* check resource available */
5669ba8ffefSDharageswari.R 		if (!skl_is_pipe_mcps_avail(skl, mconfig))
567e4e2d2f4SJeeja KP 			return -ENOMEM;
568e4e2d2f4SJeeja KP 
5696c5768b3SDharageswari R 		if (mconfig->is_loadable && ctx->dsp->fw_ops.load_mod) {
5706c5768b3SDharageswari R 			ret = ctx->dsp->fw_ops.load_mod(ctx->dsp,
5716c5768b3SDharageswari R 				mconfig->id.module_id, mconfig->guid);
5726c5768b3SDharageswari R 			if (ret < 0)
5736c5768b3SDharageswari R 				return ret;
574d643678bSJeeja KP 
575d643678bSJeeja KP 			mconfig->m_state = SKL_MODULE_LOADED;
5766c5768b3SDharageswari R 		}
5776c5768b3SDharageswari R 
578bb704a73SJeeja KP 		/* prepare the DMA if the module is gateway cpr */
579bb704a73SJeeja KP 		ret = skl_tplg_module_prepare(ctx, pipe, w, mconfig);
580bb704a73SJeeja KP 		if (ret < 0)
581bb704a73SJeeja KP 			return ret;
582bb704a73SJeeja KP 
5832d1419a3SJeeja KP 		/* update blob if blob is null for be with default value */
5842d1419a3SJeeja KP 		skl_tplg_update_be_blob(w, ctx);
5852d1419a3SJeeja KP 
586f7590d4fSJeeja KP 		/*
587f7590d4fSJeeja KP 		 * apply fix/conversion to module params based on
588f7590d4fSJeeja KP 		 * FE/BE params
589f7590d4fSJeeja KP 		 */
590f7590d4fSJeeja KP 		skl_tplg_update_module_params(w, ctx);
591ef2a352cSDharageswari R 		mconfig->id.pvt_id = skl_get_pvt_id(ctx, mconfig);
592ef2a352cSDharageswari R 		if (mconfig->id.pvt_id < 0)
593ef2a352cSDharageswari R 			return ret;
594abb74003SJeeja KP 		skl_tplg_set_module_init_data(w);
5959939a9c3SJeeja KP 		ret = skl_init_module(ctx, mconfig);
596ef2a352cSDharageswari R 		if (ret < 0) {
597ef2a352cSDharageswari R 			skl_put_pvt_id(ctx, mconfig);
598e4e2d2f4SJeeja KP 			return ret;
599ef2a352cSDharageswari R 		}
600260eb73aSDharageswari R 		skl_tplg_alloc_pipe_mcps(skl, mconfig);
601abb74003SJeeja KP 		ret = skl_tplg_set_module_params(w, ctx);
602e4e2d2f4SJeeja KP 		if (ret < 0)
603e4e2d2f4SJeeja KP 			return ret;
604e4e2d2f4SJeeja KP 	}
605e4e2d2f4SJeeja KP 
606e4e2d2f4SJeeja KP 	return 0;
607e4e2d2f4SJeeja KP }
608d93f8e55SVinod Koul 
6096c5768b3SDharageswari R static int skl_tplg_unload_pipe_modules(struct skl_sst *ctx,
6106c5768b3SDharageswari R 	 struct skl_pipe *pipe)
6116c5768b3SDharageswari R {
612b0fab9c6SDharageswari R 	int ret;
6136c5768b3SDharageswari R 	struct skl_pipe_module *w_module = NULL;
6146c5768b3SDharageswari R 	struct skl_module_cfg *mconfig = NULL;
6156c5768b3SDharageswari R 
6166c5768b3SDharageswari R 	list_for_each_entry(w_module, &pipe->w_list, node) {
6176c5768b3SDharageswari R 		mconfig  = w_module->w->priv;
6186c5768b3SDharageswari R 
619d643678bSJeeja KP 		if (mconfig->is_loadable && ctx->dsp->fw_ops.unload_mod &&
620b0fab9c6SDharageswari R 			mconfig->m_state > SKL_MODULE_UNINIT) {
621b0fab9c6SDharageswari R 			ret = ctx->dsp->fw_ops.unload_mod(ctx->dsp,
6226c5768b3SDharageswari R 						mconfig->id.module_id);
623b0fab9c6SDharageswari R 			if (ret < 0)
624b0fab9c6SDharageswari R 				return -EIO;
625b0fab9c6SDharageswari R 		}
626ef2a352cSDharageswari R 		skl_put_pvt_id(ctx, mconfig);
6276c5768b3SDharageswari R 	}
6286c5768b3SDharageswari R 
6296c5768b3SDharageswari R 	/* no modules to unload in this path, so return */
6306c5768b3SDharageswari R 	return 0;
6316c5768b3SDharageswari R }
6326c5768b3SDharageswari R 
633d93f8e55SVinod Koul /*
634d93f8e55SVinod Koul  * Mixer module represents a pipeline. So in the Pre-PMU event of mixer we
635d93f8e55SVinod Koul  * need create the pipeline. So we do following:
636d93f8e55SVinod Koul  *   - check the resources
637d93f8e55SVinod Koul  *   - Create the pipeline
638d93f8e55SVinod Koul  *   - Initialize the modules in pipeline
639d93f8e55SVinod Koul  *   - finally bind all modules together
640d93f8e55SVinod Koul  */
641d93f8e55SVinod Koul static int skl_tplg_mixer_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
642d93f8e55SVinod Koul 							struct skl *skl)
643d93f8e55SVinod Koul {
644d93f8e55SVinod Koul 	int ret;
645d93f8e55SVinod Koul 	struct skl_module_cfg *mconfig = w->priv;
646d93f8e55SVinod Koul 	struct skl_pipe_module *w_module;
647d93f8e55SVinod Koul 	struct skl_pipe *s_pipe = mconfig->pipe;
648d93f8e55SVinod Koul 	struct skl_module_cfg *src_module = NULL, *dst_module;
649d93f8e55SVinod Koul 	struct skl_sst *ctx = skl->skl_sst;
650d93f8e55SVinod Koul 
651d93f8e55SVinod Koul 	/* check resource available */
6529ba8ffefSDharageswari.R 	if (!skl_is_pipe_mcps_avail(skl, mconfig))
653d93f8e55SVinod Koul 		return -EBUSY;
654d93f8e55SVinod Koul 
6559ba8ffefSDharageswari.R 	if (!skl_is_pipe_mem_avail(skl, mconfig))
656d93f8e55SVinod Koul 		return -ENOMEM;
657d93f8e55SVinod Koul 
658d93f8e55SVinod Koul 	/*
659d93f8e55SVinod Koul 	 * Create a list of modules for pipe.
660d93f8e55SVinod Koul 	 * This list contains modules from source to sink
661d93f8e55SVinod Koul 	 */
662d93f8e55SVinod Koul 	ret = skl_create_pipeline(ctx, mconfig->pipe);
663d93f8e55SVinod Koul 	if (ret < 0)
664d93f8e55SVinod Koul 		return ret;
665d93f8e55SVinod Koul 
666260eb73aSDharageswari R 	skl_tplg_alloc_pipe_mem(skl, mconfig);
667260eb73aSDharageswari R 	skl_tplg_alloc_pipe_mcps(skl, mconfig);
668d93f8e55SVinod Koul 
669d93f8e55SVinod Koul 	/* Init all pipe modules from source to sink */
670d93f8e55SVinod Koul 	ret = skl_tplg_init_pipe_modules(skl, s_pipe);
671d93f8e55SVinod Koul 	if (ret < 0)
672d93f8e55SVinod Koul 		return ret;
673d93f8e55SVinod Koul 
674d93f8e55SVinod Koul 	/* Bind modules from source to sink */
675d93f8e55SVinod Koul 	list_for_each_entry(w_module, &s_pipe->w_list, node) {
676d93f8e55SVinod Koul 		dst_module = w_module->w->priv;
677d93f8e55SVinod Koul 
678d93f8e55SVinod Koul 		if (src_module == NULL) {
679d93f8e55SVinod Koul 			src_module = dst_module;
680d93f8e55SVinod Koul 			continue;
681d93f8e55SVinod Koul 		}
682d93f8e55SVinod Koul 
683d93f8e55SVinod Koul 		ret = skl_bind_modules(ctx, src_module, dst_module);
684d93f8e55SVinod Koul 		if (ret < 0)
685d93f8e55SVinod Koul 			return ret;
686d93f8e55SVinod Koul 
687d93f8e55SVinod Koul 		src_module = dst_module;
688d93f8e55SVinod Koul 	}
689d93f8e55SVinod Koul 
690d93f8e55SVinod Koul 	return 0;
691d93f8e55SVinod Koul }
692d93f8e55SVinod Koul 
6935e8f0ee4SDharageswari R static int skl_fill_sink_instance_id(struct skl_sst *ctx,
6945e8f0ee4SDharageswari R 				struct skl_algo_data *alg_data)
6955e8f0ee4SDharageswari R {
6965e8f0ee4SDharageswari R 	struct skl_kpb_params *params = (struct skl_kpb_params *)alg_data->params;
6975e8f0ee4SDharageswari R 	struct skl_mod_inst_map *inst;
6985e8f0ee4SDharageswari R 	int i, pvt_id;
6995e8f0ee4SDharageswari R 
7005e8f0ee4SDharageswari R 	inst = params->map;
7015e8f0ee4SDharageswari R 
7025e8f0ee4SDharageswari R 	for (i = 0; i < params->num_modules; i++) {
7035e8f0ee4SDharageswari R 		pvt_id = skl_get_pvt_instance_id_map(ctx,
7045e8f0ee4SDharageswari R 					inst->mod_id, inst->inst_id);
7055e8f0ee4SDharageswari R 		if (pvt_id < 0)
7065e8f0ee4SDharageswari R 			return -EINVAL;
7075e8f0ee4SDharageswari R 		inst->inst_id = pvt_id;
7085e8f0ee4SDharageswari R 		inst++;
7095e8f0ee4SDharageswari R 	}
7105e8f0ee4SDharageswari R 	return 0;
7115e8f0ee4SDharageswari R }
7125e8f0ee4SDharageswari R 
713cc6a4044SJeeja KP /*
714cc6a4044SJeeja KP  * Some modules require params to be set after the module is bound to
715cc6a4044SJeeja KP  * all pins connected.
716cc6a4044SJeeja KP  *
717cc6a4044SJeeja KP  * The module provider initializes set_param flag for such modules and we
718cc6a4044SJeeja KP  * send params after binding
719cc6a4044SJeeja KP  */
720cc6a4044SJeeja KP static int skl_tplg_set_module_bind_params(struct snd_soc_dapm_widget *w,
721cc6a4044SJeeja KP 			struct skl_module_cfg *mcfg, struct skl_sst *ctx)
722cc6a4044SJeeja KP {
723cc6a4044SJeeja KP 	int i, ret;
724cc6a4044SJeeja KP 	struct skl_module_cfg *mconfig = w->priv;
725cc6a4044SJeeja KP 	const struct snd_kcontrol_new *k;
726cc6a4044SJeeja KP 	struct soc_bytes_ext *sb;
727cc6a4044SJeeja KP 	struct skl_algo_data *bc;
728cc6a4044SJeeja KP 	struct skl_specific_cfg *sp_cfg;
729cc6a4044SJeeja KP 
730cc6a4044SJeeja KP 	/*
731cc6a4044SJeeja KP 	 * check all out/in pins are in bind state.
732cc6a4044SJeeja KP 	 * if so set the module param
733cc6a4044SJeeja KP 	 */
734cc6a4044SJeeja KP 	for (i = 0; i < mcfg->max_out_queue; i++) {
735cc6a4044SJeeja KP 		if (mcfg->m_out_pin[i].pin_state != SKL_PIN_BIND_DONE)
736cc6a4044SJeeja KP 			return 0;
737cc6a4044SJeeja KP 	}
738cc6a4044SJeeja KP 
739cc6a4044SJeeja KP 	for (i = 0; i < mcfg->max_in_queue; i++) {
740cc6a4044SJeeja KP 		if (mcfg->m_in_pin[i].pin_state != SKL_PIN_BIND_DONE)
741cc6a4044SJeeja KP 			return 0;
742cc6a4044SJeeja KP 	}
743cc6a4044SJeeja KP 
744cc6a4044SJeeja KP 	if (mconfig->formats_config.caps_size > 0 &&
745cc6a4044SJeeja KP 		mconfig->formats_config.set_params == SKL_PARAM_BIND) {
746cc6a4044SJeeja KP 		sp_cfg = &mconfig->formats_config;
747cc6a4044SJeeja KP 		ret = skl_set_module_params(ctx, sp_cfg->caps,
748cc6a4044SJeeja KP 					sp_cfg->caps_size,
749cc6a4044SJeeja KP 					sp_cfg->param_id, mconfig);
750cc6a4044SJeeja KP 		if (ret < 0)
751cc6a4044SJeeja KP 			return ret;
752cc6a4044SJeeja KP 	}
753cc6a4044SJeeja KP 
754cc6a4044SJeeja KP 	for (i = 0; i < w->num_kcontrols; i++) {
755cc6a4044SJeeja KP 		k = &w->kcontrol_news[i];
756cc6a4044SJeeja KP 		if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
757cc6a4044SJeeja KP 			sb = (void *) k->private_value;
758cc6a4044SJeeja KP 			bc = (struct skl_algo_data *)sb->dobj.private;
759cc6a4044SJeeja KP 
760cc6a4044SJeeja KP 			if (bc->set_params == SKL_PARAM_BIND) {
7615e8f0ee4SDharageswari R 				if (mconfig->m_type == SKL_MODULE_TYPE_KPB)
7625e8f0ee4SDharageswari R 					skl_fill_sink_instance_id(ctx, bc);
763cc6a4044SJeeja KP 				ret = skl_set_module_params(ctx,
764cc6a4044SJeeja KP 						(u32 *)bc->params, bc->max,
765cc6a4044SJeeja KP 						bc->param_id, mconfig);
766cc6a4044SJeeja KP 				if (ret < 0)
767cc6a4044SJeeja KP 					return ret;
768cc6a4044SJeeja KP 			}
769cc6a4044SJeeja KP 		}
770cc6a4044SJeeja KP 	}
771cc6a4044SJeeja KP 
772cc6a4044SJeeja KP 	return 0;
773cc6a4044SJeeja KP }
774cc6a4044SJeeja KP 
7758724ff17SJeeja KP static int skl_tplg_bind_sinks(struct snd_soc_dapm_widget *w,
7768724ff17SJeeja KP 				struct skl *skl,
7776bd4cf85SJeeja KP 				struct snd_soc_dapm_widget *src_w,
7788724ff17SJeeja KP 				struct skl_module_cfg *src_mconfig)
779d93f8e55SVinod Koul {
780d93f8e55SVinod Koul 	struct snd_soc_dapm_path *p;
7810ed95d76SJeeja KP 	struct snd_soc_dapm_widget *sink = NULL, *next_sink = NULL;
7828724ff17SJeeja KP 	struct skl_module_cfg *sink_mconfig;
783d93f8e55SVinod Koul 	struct skl_sst *ctx = skl->skl_sst;
7848724ff17SJeeja KP 	int ret;
785d93f8e55SVinod Koul 
7868724ff17SJeeja KP 	snd_soc_dapm_widget_for_each_sink_path(w, p) {
787d93f8e55SVinod Koul 		if (!p->connect)
788d93f8e55SVinod Koul 			continue;
789d93f8e55SVinod Koul 
790d93f8e55SVinod Koul 		dev_dbg(ctx->dev, "%s: src widget=%s\n", __func__, w->name);
791d93f8e55SVinod Koul 		dev_dbg(ctx->dev, "%s: sink widget=%s\n", __func__, p->sink->name);
792d93f8e55SVinod Koul 
7930ed95d76SJeeja KP 		next_sink = p->sink;
7946bd4cf85SJeeja KP 
7956bd4cf85SJeeja KP 		if (!is_skl_dsp_widget_type(p->sink))
7966bd4cf85SJeeja KP 			return skl_tplg_bind_sinks(p->sink, skl, src_w, src_mconfig);
7976bd4cf85SJeeja KP 
798d93f8e55SVinod Koul 		/*
799d93f8e55SVinod Koul 		 * here we will check widgets in sink pipelines, so that
800d93f8e55SVinod Koul 		 * can be any widgets type and we are only interested if
801d93f8e55SVinod Koul 		 * they are ones used for SKL so check that first
802d93f8e55SVinod Koul 		 */
803d93f8e55SVinod Koul 		if ((p->sink->priv != NULL) &&
804d93f8e55SVinod Koul 					is_skl_dsp_widget_type(p->sink)) {
805d93f8e55SVinod Koul 
806d93f8e55SVinod Koul 			sink = p->sink;
807d93f8e55SVinod Koul 			sink_mconfig = sink->priv;
808d93f8e55SVinod Koul 
809cc6a4044SJeeja KP 			if (src_mconfig->m_state == SKL_MODULE_UNINIT ||
810cc6a4044SJeeja KP 				sink_mconfig->m_state == SKL_MODULE_UNINIT)
811cc6a4044SJeeja KP 				continue;
812cc6a4044SJeeja KP 
813d93f8e55SVinod Koul 			/* Bind source to sink, mixin is always source */
814d93f8e55SVinod Koul 			ret = skl_bind_modules(ctx, src_mconfig, sink_mconfig);
815d93f8e55SVinod Koul 			if (ret)
816d93f8e55SVinod Koul 				return ret;
817d93f8e55SVinod Koul 
818cc6a4044SJeeja KP 			/* set module params after bind */
819cc6a4044SJeeja KP 			skl_tplg_set_module_bind_params(src_w, src_mconfig, ctx);
820cc6a4044SJeeja KP 			skl_tplg_set_module_bind_params(sink, sink_mconfig, ctx);
821cc6a4044SJeeja KP 
822d93f8e55SVinod Koul 			/* Start sinks pipe first */
823d93f8e55SVinod Koul 			if (sink_mconfig->pipe->state != SKL_PIPE_STARTED) {
824d1730c3dSJeeja KP 				if (sink_mconfig->pipe->conn_type !=
825d1730c3dSJeeja KP 							SKL_PIPE_CONN_TYPE_FE)
826d1730c3dSJeeja KP 					ret = skl_run_pipe(ctx,
827d1730c3dSJeeja KP 							sink_mconfig->pipe);
828d93f8e55SVinod Koul 				if (ret)
829d93f8e55SVinod Koul 					return ret;
830d93f8e55SVinod Koul 			}
831d93f8e55SVinod Koul 		}
832d93f8e55SVinod Koul 	}
833d93f8e55SVinod Koul 
8348724ff17SJeeja KP 	if (!sink)
8356bd4cf85SJeeja KP 		return skl_tplg_bind_sinks(next_sink, skl, src_w, src_mconfig);
8368724ff17SJeeja KP 
8378724ff17SJeeja KP 	return 0;
8388724ff17SJeeja KP }
8398724ff17SJeeja KP 
840d93f8e55SVinod Koul /*
841d93f8e55SVinod Koul  * A PGA represents a module in a pipeline. So in the Pre-PMU event of PGA
842d93f8e55SVinod Koul  * we need to do following:
843d93f8e55SVinod Koul  *   - Bind to sink pipeline
844d93f8e55SVinod Koul  *      Since the sink pipes can be running and we don't get mixer event on
845d93f8e55SVinod Koul  *      connect for already running mixer, we need to find the sink pipes
846d93f8e55SVinod Koul  *      here and bind to them. This way dynamic connect works.
847d93f8e55SVinod Koul  *   - Start sink pipeline, if not running
848d93f8e55SVinod Koul  *   - Then run current pipe
849d93f8e55SVinod Koul  */
850d93f8e55SVinod Koul static int skl_tplg_pga_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
851d93f8e55SVinod Koul 								struct skl *skl)
852d93f8e55SVinod Koul {
8538724ff17SJeeja KP 	struct skl_module_cfg *src_mconfig;
854d93f8e55SVinod Koul 	struct skl_sst *ctx = skl->skl_sst;
855d93f8e55SVinod Koul 	int ret = 0;
856d93f8e55SVinod Koul 
8578724ff17SJeeja KP 	src_mconfig = w->priv;
858d93f8e55SVinod Koul 
859d93f8e55SVinod Koul 	/*
860d93f8e55SVinod Koul 	 * find which sink it is connected to, bind with the sink,
861d93f8e55SVinod Koul 	 * if sink is not started, start sink pipe first, then start
862d93f8e55SVinod Koul 	 * this pipe
863d93f8e55SVinod Koul 	 */
8646bd4cf85SJeeja KP 	ret = skl_tplg_bind_sinks(w, skl, w, src_mconfig);
8658724ff17SJeeja KP 	if (ret)
8668724ff17SJeeja KP 		return ret;
8678724ff17SJeeja KP 
868d93f8e55SVinod Koul 	/* Start source pipe last after starting all sinks */
869d1730c3dSJeeja KP 	if (src_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
870d1730c3dSJeeja KP 		return skl_run_pipe(ctx, src_mconfig->pipe);
871d93f8e55SVinod Koul 
872d93f8e55SVinod Koul 	return 0;
873d93f8e55SVinod Koul }
874d93f8e55SVinod Koul 
8758724ff17SJeeja KP static struct snd_soc_dapm_widget *skl_get_src_dsp_widget(
8768724ff17SJeeja KP 		struct snd_soc_dapm_widget *w, struct skl *skl)
8778724ff17SJeeja KP {
8788724ff17SJeeja KP 	struct snd_soc_dapm_path *p;
8798724ff17SJeeja KP 	struct snd_soc_dapm_widget *src_w = NULL;
8808724ff17SJeeja KP 	struct skl_sst *ctx = skl->skl_sst;
8818724ff17SJeeja KP 
882d93f8e55SVinod Koul 	snd_soc_dapm_widget_for_each_source_path(w, p) {
8838724ff17SJeeja KP 		src_w = p->source;
884d93f8e55SVinod Koul 		if (!p->connect)
885d93f8e55SVinod Koul 			continue;
886d93f8e55SVinod Koul 
8878724ff17SJeeja KP 		dev_dbg(ctx->dev, "sink widget=%s\n", w->name);
8888724ff17SJeeja KP 		dev_dbg(ctx->dev, "src widget=%s\n", p->source->name);
889d93f8e55SVinod Koul 
890d93f8e55SVinod Koul 		/*
8918724ff17SJeeja KP 		 * here we will check widgets in sink pipelines, so that can
8928724ff17SJeeja KP 		 * be any widgets type and we are only interested if they are
8938724ff17SJeeja KP 		 * ones used for SKL so check that first
894d93f8e55SVinod Koul 		 */
8958724ff17SJeeja KP 		if ((p->source->priv != NULL) &&
8968724ff17SJeeja KP 					is_skl_dsp_widget_type(p->source)) {
8978724ff17SJeeja KP 			return p->source;
898d93f8e55SVinod Koul 		}
899d93f8e55SVinod Koul 	}
900d93f8e55SVinod Koul 
9018724ff17SJeeja KP 	if (src_w != NULL)
9028724ff17SJeeja KP 		return skl_get_src_dsp_widget(src_w, skl);
903d93f8e55SVinod Koul 
9048724ff17SJeeja KP 	return NULL;
905d93f8e55SVinod Koul }
906d93f8e55SVinod Koul 
907d93f8e55SVinod Koul /*
908d93f8e55SVinod Koul  * in the Post-PMU event of mixer we need to do following:
909d93f8e55SVinod Koul  *   - Check if this pipe is running
910d93f8e55SVinod Koul  *   - if not, then
911d93f8e55SVinod Koul  *	- bind this pipeline to its source pipeline
912d93f8e55SVinod Koul  *	  if source pipe is already running, this means it is a dynamic
913d93f8e55SVinod Koul  *	  connection and we need to bind only to that pipe
914d93f8e55SVinod Koul  *	- start this pipeline
915d93f8e55SVinod Koul  */
916d93f8e55SVinod Koul static int skl_tplg_mixer_dapm_post_pmu_event(struct snd_soc_dapm_widget *w,
917d93f8e55SVinod Koul 							struct skl *skl)
918d93f8e55SVinod Koul {
919d93f8e55SVinod Koul 	int ret = 0;
920d93f8e55SVinod Koul 	struct snd_soc_dapm_widget *source, *sink;
921d93f8e55SVinod Koul 	struct skl_module_cfg *src_mconfig, *sink_mconfig;
922d93f8e55SVinod Koul 	struct skl_sst *ctx = skl->skl_sst;
923d93f8e55SVinod Koul 	int src_pipe_started = 0;
924d93f8e55SVinod Koul 
925d93f8e55SVinod Koul 	sink = w;
926d93f8e55SVinod Koul 	sink_mconfig = sink->priv;
927d93f8e55SVinod Koul 
928d93f8e55SVinod Koul 	/*
929d93f8e55SVinod Koul 	 * If source pipe is already started, that means source is driving
930d93f8e55SVinod Koul 	 * one more sink before this sink got connected, Since source is
931d93f8e55SVinod Koul 	 * started, bind this sink to source and start this pipe.
932d93f8e55SVinod Koul 	 */
9338724ff17SJeeja KP 	source = skl_get_src_dsp_widget(w, skl);
9348724ff17SJeeja KP 	if (source != NULL) {
935d93f8e55SVinod Koul 		src_mconfig = source->priv;
936d93f8e55SVinod Koul 		sink_mconfig = sink->priv;
937d93f8e55SVinod Koul 		src_pipe_started = 1;
938d93f8e55SVinod Koul 
939d93f8e55SVinod Koul 		/*
9408724ff17SJeeja KP 		 * check pipe state, then no need to bind or start the
9418724ff17SJeeja KP 		 * pipe
942d93f8e55SVinod Koul 		 */
943d93f8e55SVinod Koul 		if (src_mconfig->pipe->state != SKL_PIPE_STARTED)
944d93f8e55SVinod Koul 			src_pipe_started = 0;
945d93f8e55SVinod Koul 	}
946d93f8e55SVinod Koul 
947d93f8e55SVinod Koul 	if (src_pipe_started) {
948d93f8e55SVinod Koul 		ret = skl_bind_modules(ctx, src_mconfig, sink_mconfig);
949d93f8e55SVinod Koul 		if (ret)
950d93f8e55SVinod Koul 			return ret;
951d93f8e55SVinod Koul 
952cc6a4044SJeeja KP 		/* set module params after bind */
953cc6a4044SJeeja KP 		skl_tplg_set_module_bind_params(source, src_mconfig, ctx);
954cc6a4044SJeeja KP 		skl_tplg_set_module_bind_params(sink, sink_mconfig, ctx);
955cc6a4044SJeeja KP 
956d1730c3dSJeeja KP 		if (sink_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
957d93f8e55SVinod Koul 			ret = skl_run_pipe(ctx, sink_mconfig->pipe);
958d93f8e55SVinod Koul 	}
959d93f8e55SVinod Koul 
960d93f8e55SVinod Koul 	return ret;
961d93f8e55SVinod Koul }
962d93f8e55SVinod Koul 
963d93f8e55SVinod Koul /*
964d93f8e55SVinod Koul  * in the Pre-PMD event of mixer we need to do following:
965d93f8e55SVinod Koul  *   - Stop the pipe
966d93f8e55SVinod Koul  *   - find the source connections and remove that from dapm_path_list
967d93f8e55SVinod Koul  *   - unbind with source pipelines if still connected
968d93f8e55SVinod Koul  */
969d93f8e55SVinod Koul static int skl_tplg_mixer_dapm_pre_pmd_event(struct snd_soc_dapm_widget *w,
970d93f8e55SVinod Koul 							struct skl *skl)
971d93f8e55SVinod Koul {
972d93f8e55SVinod Koul 	struct skl_module_cfg *src_mconfig, *sink_mconfig;
973ce1b5551SJeeja KP 	int ret = 0, i;
974d93f8e55SVinod Koul 	struct skl_sst *ctx = skl->skl_sst;
975d93f8e55SVinod Koul 
976ce1b5551SJeeja KP 	sink_mconfig = w->priv;
977d93f8e55SVinod Koul 
978d93f8e55SVinod Koul 	/* Stop the pipe */
979d93f8e55SVinod Koul 	ret = skl_stop_pipe(ctx, sink_mconfig->pipe);
980d93f8e55SVinod Koul 	if (ret)
981d93f8e55SVinod Koul 		return ret;
982d93f8e55SVinod Koul 
983ce1b5551SJeeja KP 	for (i = 0; i < sink_mconfig->max_in_queue; i++) {
984ce1b5551SJeeja KP 		if (sink_mconfig->m_in_pin[i].pin_state == SKL_PIN_BIND_DONE) {
985ce1b5551SJeeja KP 			src_mconfig = sink_mconfig->m_in_pin[i].tgt_mcfg;
986ce1b5551SJeeja KP 			if (!src_mconfig)
987ce1b5551SJeeja KP 				continue;
988d93f8e55SVinod Koul 			/*
989ce1b5551SJeeja KP 			 * If path_found == 1, that means pmd for source
990ce1b5551SJeeja KP 			 * pipe has not occurred, source is connected to
991ce1b5551SJeeja KP 			 * some other sink. so its responsibility of sink
992ce1b5551SJeeja KP 			 * to unbind itself from source.
993d93f8e55SVinod Koul 			 */
994d93f8e55SVinod Koul 			ret = skl_stop_pipe(ctx, src_mconfig->pipe);
995d93f8e55SVinod Koul 			if (ret < 0)
996d93f8e55SVinod Koul 				return ret;
997d93f8e55SVinod Koul 
998ce1b5551SJeeja KP 			ret = skl_unbind_modules(ctx,
999ce1b5551SJeeja KP 						src_mconfig, sink_mconfig);
1000ce1b5551SJeeja KP 		}
1001d93f8e55SVinod Koul 	}
1002d93f8e55SVinod Koul 
1003d93f8e55SVinod Koul 	return ret;
1004d93f8e55SVinod Koul }
1005d93f8e55SVinod Koul 
1006d93f8e55SVinod Koul /*
1007d93f8e55SVinod Koul  * in the Post-PMD event of mixer we need to do following:
1008d93f8e55SVinod Koul  *   - Free the mcps used
1009d93f8e55SVinod Koul  *   - Free the mem used
1010d93f8e55SVinod Koul  *   - Unbind the modules within the pipeline
1011d93f8e55SVinod Koul  *   - Delete the pipeline (modules are not required to be explicitly
1012d93f8e55SVinod Koul  *     deleted, pipeline delete is enough here
1013d93f8e55SVinod Koul  */
1014d93f8e55SVinod Koul static int skl_tplg_mixer_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
1015d93f8e55SVinod Koul 							struct skl *skl)
1016d93f8e55SVinod Koul {
1017d93f8e55SVinod Koul 	struct skl_module_cfg *mconfig = w->priv;
1018d93f8e55SVinod Koul 	struct skl_pipe_module *w_module;
1019d93f8e55SVinod Koul 	struct skl_module_cfg *src_module = NULL, *dst_module;
1020d93f8e55SVinod Koul 	struct skl_sst *ctx = skl->skl_sst;
1021d93f8e55SVinod Koul 	struct skl_pipe *s_pipe = mconfig->pipe;
1022d93f8e55SVinod Koul 
1023260eb73aSDharageswari R 	if (s_pipe->state == SKL_PIPE_INVALID)
1024260eb73aSDharageswari R 		return -EINVAL;
1025260eb73aSDharageswari R 
1026d93f8e55SVinod Koul 	skl_tplg_free_pipe_mcps(skl, mconfig);
102765976878SVinod Koul 	skl_tplg_free_pipe_mem(skl, mconfig);
1028d93f8e55SVinod Koul 
1029d93f8e55SVinod Koul 	list_for_each_entry(w_module, &s_pipe->w_list, node) {
1030d93f8e55SVinod Koul 		dst_module = w_module->w->priv;
1031d93f8e55SVinod Koul 
1032260eb73aSDharageswari R 		if (mconfig->m_state >= SKL_MODULE_INIT_DONE)
10337ae3cb15SVinod Koul 			skl_tplg_free_pipe_mcps(skl, dst_module);
1034d93f8e55SVinod Koul 		if (src_module == NULL) {
1035d93f8e55SVinod Koul 			src_module = dst_module;
1036d93f8e55SVinod Koul 			continue;
1037d93f8e55SVinod Koul 		}
1038d93f8e55SVinod Koul 
10397ca42f5aSGuneshwor Singh 		skl_unbind_modules(ctx, src_module, dst_module);
1040d93f8e55SVinod Koul 		src_module = dst_module;
1041d93f8e55SVinod Koul 	}
1042d93f8e55SVinod Koul 
1043547cafa3SVinod Koul 	skl_delete_pipe(ctx, mconfig->pipe);
1044d93f8e55SVinod Koul 
10456c5768b3SDharageswari R 	return skl_tplg_unload_pipe_modules(ctx, s_pipe);
1046d93f8e55SVinod Koul }
1047d93f8e55SVinod Koul 
1048d93f8e55SVinod Koul /*
1049d93f8e55SVinod Koul  * in the Post-PMD event of PGA we need to do following:
1050d93f8e55SVinod Koul  *   - Free the mcps used
1051d93f8e55SVinod Koul  *   - Stop the pipeline
1052d93f8e55SVinod Koul  *   - In source pipe is connected, unbind with source pipelines
1053d93f8e55SVinod Koul  */
1054d93f8e55SVinod Koul static int skl_tplg_pga_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
1055d93f8e55SVinod Koul 								struct skl *skl)
1056d93f8e55SVinod Koul {
1057d93f8e55SVinod Koul 	struct skl_module_cfg *src_mconfig, *sink_mconfig;
1058ce1b5551SJeeja KP 	int ret = 0, i;
1059d93f8e55SVinod Koul 	struct skl_sst *ctx = skl->skl_sst;
1060d93f8e55SVinod Koul 
1061ce1b5551SJeeja KP 	src_mconfig = w->priv;
1062d93f8e55SVinod Koul 
1063d93f8e55SVinod Koul 	/* Stop the pipe since this is a mixin module */
1064d93f8e55SVinod Koul 	ret = skl_stop_pipe(ctx, src_mconfig->pipe);
1065d93f8e55SVinod Koul 	if (ret)
1066d93f8e55SVinod Koul 		return ret;
1067d93f8e55SVinod Koul 
1068ce1b5551SJeeja KP 	for (i = 0; i < src_mconfig->max_out_queue; i++) {
1069ce1b5551SJeeja KP 		if (src_mconfig->m_out_pin[i].pin_state == SKL_PIN_BIND_DONE) {
1070ce1b5551SJeeja KP 			sink_mconfig = src_mconfig->m_out_pin[i].tgt_mcfg;
1071ce1b5551SJeeja KP 			if (!sink_mconfig)
1072ce1b5551SJeeja KP 				continue;
1073d93f8e55SVinod Koul 			/*
1074ce1b5551SJeeja KP 			 * This is a connecter and if path is found that means
1075d93f8e55SVinod Koul 			 * unbind between source and sink has not happened yet
1076d93f8e55SVinod Koul 			 */
1077ce1b5551SJeeja KP 			ret = skl_unbind_modules(ctx, src_mconfig,
1078ce1b5551SJeeja KP 							sink_mconfig);
1079ce1b5551SJeeja KP 		}
1080d93f8e55SVinod Koul 	}
1081d93f8e55SVinod Koul 
1082d93f8e55SVinod Koul 	return ret;
1083d93f8e55SVinod Koul }
1084d93f8e55SVinod Koul 
1085d93f8e55SVinod Koul /*
1086d93f8e55SVinod Koul  * In modelling, we assume there will be ONLY one mixer in a pipeline.  If
1087d93f8e55SVinod Koul  * mixer is not required then it is treated as static mixer aka vmixer with
1088d93f8e55SVinod Koul  * a hard path to source module
1089d93f8e55SVinod Koul  * So we don't need to check if source is started or not as hard path puts
1090d93f8e55SVinod Koul  * dependency on each other
1091d93f8e55SVinod Koul  */
1092d93f8e55SVinod Koul static int skl_tplg_vmixer_event(struct snd_soc_dapm_widget *w,
1093d93f8e55SVinod Koul 				struct snd_kcontrol *k, int event)
1094d93f8e55SVinod Koul {
1095d93f8e55SVinod Koul 	struct snd_soc_dapm_context *dapm = w->dapm;
1096d93f8e55SVinod Koul 	struct skl *skl = get_skl_ctx(dapm->dev);
1097d93f8e55SVinod Koul 
1098d93f8e55SVinod Koul 	switch (event) {
1099d93f8e55SVinod Koul 	case SND_SOC_DAPM_PRE_PMU:
1100d93f8e55SVinod Koul 		return skl_tplg_mixer_dapm_pre_pmu_event(w, skl);
1101d93f8e55SVinod Koul 
1102de1fedf2SJeeja KP 	case SND_SOC_DAPM_POST_PMU:
1103de1fedf2SJeeja KP 		return skl_tplg_mixer_dapm_post_pmu_event(w, skl);
1104de1fedf2SJeeja KP 
1105de1fedf2SJeeja KP 	case SND_SOC_DAPM_PRE_PMD:
1106de1fedf2SJeeja KP 		return skl_tplg_mixer_dapm_pre_pmd_event(w, skl);
1107de1fedf2SJeeja KP 
1108d93f8e55SVinod Koul 	case SND_SOC_DAPM_POST_PMD:
1109d93f8e55SVinod Koul 		return skl_tplg_mixer_dapm_post_pmd_event(w, skl);
1110d93f8e55SVinod Koul 	}
1111d93f8e55SVinod Koul 
1112d93f8e55SVinod Koul 	return 0;
1113d93f8e55SVinod Koul }
1114d93f8e55SVinod Koul 
1115d93f8e55SVinod Koul /*
1116d93f8e55SVinod Koul  * In modelling, we assume there will be ONLY one mixer in a pipeline. If a
1117d93f8e55SVinod Koul  * second one is required that is created as another pipe entity.
1118d93f8e55SVinod Koul  * The mixer is responsible for pipe management and represent a pipeline
1119d93f8e55SVinod Koul  * instance
1120d93f8e55SVinod Koul  */
1121d93f8e55SVinod Koul static int skl_tplg_mixer_event(struct snd_soc_dapm_widget *w,
1122d93f8e55SVinod Koul 				struct snd_kcontrol *k, int event)
1123d93f8e55SVinod Koul {
1124d93f8e55SVinod Koul 	struct snd_soc_dapm_context *dapm = w->dapm;
1125d93f8e55SVinod Koul 	struct skl *skl = get_skl_ctx(dapm->dev);
1126d93f8e55SVinod Koul 
1127d93f8e55SVinod Koul 	switch (event) {
1128d93f8e55SVinod Koul 	case SND_SOC_DAPM_PRE_PMU:
1129d93f8e55SVinod Koul 		return skl_tplg_mixer_dapm_pre_pmu_event(w, skl);
1130d93f8e55SVinod Koul 
1131d93f8e55SVinod Koul 	case SND_SOC_DAPM_POST_PMU:
1132d93f8e55SVinod Koul 		return skl_tplg_mixer_dapm_post_pmu_event(w, skl);
1133d93f8e55SVinod Koul 
1134d93f8e55SVinod Koul 	case SND_SOC_DAPM_PRE_PMD:
1135d93f8e55SVinod Koul 		return skl_tplg_mixer_dapm_pre_pmd_event(w, skl);
1136d93f8e55SVinod Koul 
1137d93f8e55SVinod Koul 	case SND_SOC_DAPM_POST_PMD:
1138d93f8e55SVinod Koul 		return skl_tplg_mixer_dapm_post_pmd_event(w, skl);
1139d93f8e55SVinod Koul 	}
1140d93f8e55SVinod Koul 
1141d93f8e55SVinod Koul 	return 0;
1142d93f8e55SVinod Koul }
1143d93f8e55SVinod Koul 
1144d93f8e55SVinod Koul /*
1145d93f8e55SVinod Koul  * In modelling, we assumed rest of the modules in pipeline are PGA. But we
1146d93f8e55SVinod Koul  * are interested in last PGA (leaf PGA) in a pipeline to disconnect with
1147d93f8e55SVinod Koul  * the sink when it is running (two FE to one BE or one FE to two BE)
1148d93f8e55SVinod Koul  * scenarios
1149d93f8e55SVinod Koul  */
1150d93f8e55SVinod Koul static int skl_tplg_pga_event(struct snd_soc_dapm_widget *w,
1151d93f8e55SVinod Koul 			struct snd_kcontrol *k, int event)
1152d93f8e55SVinod Koul 
1153d93f8e55SVinod Koul {
1154d93f8e55SVinod Koul 	struct snd_soc_dapm_context *dapm = w->dapm;
1155d93f8e55SVinod Koul 	struct skl *skl = get_skl_ctx(dapm->dev);
1156d93f8e55SVinod Koul 
1157d93f8e55SVinod Koul 	switch (event) {
1158d93f8e55SVinod Koul 	case SND_SOC_DAPM_PRE_PMU:
1159d93f8e55SVinod Koul 		return skl_tplg_pga_dapm_pre_pmu_event(w, skl);
1160d93f8e55SVinod Koul 
1161d93f8e55SVinod Koul 	case SND_SOC_DAPM_POST_PMD:
1162d93f8e55SVinod Koul 		return skl_tplg_pga_dapm_post_pmd_event(w, skl);
1163d93f8e55SVinod Koul 	}
1164d93f8e55SVinod Koul 
1165d93f8e55SVinod Koul 	return 0;
1166d93f8e55SVinod Koul }
1167cfb0a873SVinod Koul 
1168140adfbaSJeeja KP static int skl_tplg_tlv_control_get(struct snd_kcontrol *kcontrol,
1169140adfbaSJeeja KP 			unsigned int __user *data, unsigned int size)
1170140adfbaSJeeja KP {
1171140adfbaSJeeja KP 	struct soc_bytes_ext *sb =
1172140adfbaSJeeja KP 			(struct soc_bytes_ext *)kcontrol->private_value;
1173140adfbaSJeeja KP 	struct skl_algo_data *bc = (struct skl_algo_data *)sb->dobj.private;
11747d9f2911SOmair M Abdullah 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
11757d9f2911SOmair M Abdullah 	struct skl_module_cfg *mconfig = w->priv;
11767d9f2911SOmair M Abdullah 	struct skl *skl = get_skl_ctx(w->dapm->dev);
11777d9f2911SOmair M Abdullah 
11787d9f2911SOmair M Abdullah 	if (w->power)
11797d9f2911SOmair M Abdullah 		skl_get_module_params(skl->skl_sst, (u32 *)bc->params,
11800d682104SDharageswari R 				      bc->size, bc->param_id, mconfig);
1181140adfbaSJeeja KP 
118241556f68SVinod Koul 	/* decrement size for TLV header */
118341556f68SVinod Koul 	size -= 2 * sizeof(u32);
118441556f68SVinod Koul 
118541556f68SVinod Koul 	/* check size as we don't want to send kernel data */
118641556f68SVinod Koul 	if (size > bc->max)
118741556f68SVinod Koul 		size = bc->max;
118841556f68SVinod Koul 
1189140adfbaSJeeja KP 	if (bc->params) {
1190140adfbaSJeeja KP 		if (copy_to_user(data, &bc->param_id, sizeof(u32)))
1191140adfbaSJeeja KP 			return -EFAULT;
1192e8bc3c99SDan Carpenter 		if (copy_to_user(data + 1, &size, sizeof(u32)))
1193140adfbaSJeeja KP 			return -EFAULT;
1194e8bc3c99SDan Carpenter 		if (copy_to_user(data + 2, bc->params, size))
1195140adfbaSJeeja KP 			return -EFAULT;
1196140adfbaSJeeja KP 	}
1197140adfbaSJeeja KP 
1198140adfbaSJeeja KP 	return 0;
1199140adfbaSJeeja KP }
1200140adfbaSJeeja KP 
1201140adfbaSJeeja KP #define SKL_PARAM_VENDOR_ID 0xff
1202140adfbaSJeeja KP 
1203140adfbaSJeeja KP static int skl_tplg_tlv_control_set(struct snd_kcontrol *kcontrol,
1204140adfbaSJeeja KP 			const unsigned int __user *data, unsigned int size)
1205140adfbaSJeeja KP {
1206140adfbaSJeeja KP 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1207140adfbaSJeeja KP 	struct skl_module_cfg *mconfig = w->priv;
1208140adfbaSJeeja KP 	struct soc_bytes_ext *sb =
1209140adfbaSJeeja KP 			(struct soc_bytes_ext *)kcontrol->private_value;
1210140adfbaSJeeja KP 	struct skl_algo_data *ac = (struct skl_algo_data *)sb->dobj.private;
1211140adfbaSJeeja KP 	struct skl *skl = get_skl_ctx(w->dapm->dev);
1212140adfbaSJeeja KP 
1213140adfbaSJeeja KP 	if (ac->params) {
12140d682104SDharageswari R 		if (size > ac->max)
12150d682104SDharageswari R 			return -EINVAL;
12160d682104SDharageswari R 
12170d682104SDharageswari R 		ac->size = size;
1218140adfbaSJeeja KP 		/*
1219140adfbaSJeeja KP 		 * if the param_is is of type Vendor, firmware expects actual
1220140adfbaSJeeja KP 		 * parameter id and size from the control.
1221140adfbaSJeeja KP 		 */
1222140adfbaSJeeja KP 		if (ac->param_id == SKL_PARAM_VENDOR_ID) {
1223140adfbaSJeeja KP 			if (copy_from_user(ac->params, data, size))
1224140adfbaSJeeja KP 				return -EFAULT;
1225140adfbaSJeeja KP 		} else {
1226140adfbaSJeeja KP 			if (copy_from_user(ac->params,
122765b4bcb8SAlan 					   data + 2, size))
1228140adfbaSJeeja KP 				return -EFAULT;
1229140adfbaSJeeja KP 		}
1230140adfbaSJeeja KP 
1231140adfbaSJeeja KP 		if (w->power)
1232140adfbaSJeeja KP 			return skl_set_module_params(skl->skl_sst,
12330d682104SDharageswari R 						(u32 *)ac->params, ac->size,
1234140adfbaSJeeja KP 						ac->param_id, mconfig);
1235140adfbaSJeeja KP 	}
1236140adfbaSJeeja KP 
1237140adfbaSJeeja KP 	return 0;
1238140adfbaSJeeja KP }
1239140adfbaSJeeja KP 
1240cfb0a873SVinod Koul /*
12418871dcb9SJeeja KP  * Fill the dma id for host and link. In case of passthrough
12428871dcb9SJeeja KP  * pipeline, this will both host and link in the same
12438871dcb9SJeeja KP  * pipeline, so need to copy the link and host based on dev_type
12448871dcb9SJeeja KP  */
12458871dcb9SJeeja KP static void skl_tplg_fill_dma_id(struct skl_module_cfg *mcfg,
12468871dcb9SJeeja KP 				struct skl_pipe_params *params)
12478871dcb9SJeeja KP {
12488871dcb9SJeeja KP 	struct skl_pipe *pipe = mcfg->pipe;
12498871dcb9SJeeja KP 
12508871dcb9SJeeja KP 	if (pipe->passthru) {
12518871dcb9SJeeja KP 		switch (mcfg->dev_type) {
12528871dcb9SJeeja KP 		case SKL_DEVICE_HDALINK:
12538871dcb9SJeeja KP 			pipe->p_params->link_dma_id = params->link_dma_id;
125412c3be0eSJeeja KP 			pipe->p_params->link_index = params->link_index;
12558871dcb9SJeeja KP 			break;
12568871dcb9SJeeja KP 
12578871dcb9SJeeja KP 		case SKL_DEVICE_HDAHOST:
12588871dcb9SJeeja KP 			pipe->p_params->host_dma_id = params->host_dma_id;
12598871dcb9SJeeja KP 			break;
12608871dcb9SJeeja KP 
12618871dcb9SJeeja KP 		default:
12628871dcb9SJeeja KP 			break;
12638871dcb9SJeeja KP 		}
12648871dcb9SJeeja KP 		pipe->p_params->s_fmt = params->s_fmt;
12658871dcb9SJeeja KP 		pipe->p_params->ch = params->ch;
12668871dcb9SJeeja KP 		pipe->p_params->s_freq = params->s_freq;
12678871dcb9SJeeja KP 		pipe->p_params->stream = params->stream;
126812c3be0eSJeeja KP 		pipe->p_params->format = params->format;
12698871dcb9SJeeja KP 
12708871dcb9SJeeja KP 	} else {
12718871dcb9SJeeja KP 		memcpy(pipe->p_params, params, sizeof(*params));
12728871dcb9SJeeja KP 	}
12738871dcb9SJeeja KP }
12748871dcb9SJeeja KP 
12758871dcb9SJeeja KP /*
1276cfb0a873SVinod Koul  * The FE params are passed by hw_params of the DAI.
1277cfb0a873SVinod Koul  * On hw_params, the params are stored in Gateway module of the FE and we
1278cfb0a873SVinod Koul  * need to calculate the format in DSP module configuration, that
1279cfb0a873SVinod Koul  * conversion is done here
1280cfb0a873SVinod Koul  */
1281cfb0a873SVinod Koul int skl_tplg_update_pipe_params(struct device *dev,
1282cfb0a873SVinod Koul 			struct skl_module_cfg *mconfig,
1283cfb0a873SVinod Koul 			struct skl_pipe_params *params)
1284cfb0a873SVinod Koul {
1285cfb0a873SVinod Koul 	struct skl_module_fmt *format = NULL;
1286cfb0a873SVinod Koul 
12878871dcb9SJeeja KP 	skl_tplg_fill_dma_id(mconfig, params);
1288cfb0a873SVinod Koul 
1289cfb0a873SVinod Koul 	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK)
12904cd9899fSHardik T Shah 		format = &mconfig->in_fmt[0];
1291cfb0a873SVinod Koul 	else
12924cd9899fSHardik T Shah 		format = &mconfig->out_fmt[0];
1293cfb0a873SVinod Koul 
1294cfb0a873SVinod Koul 	/* set the hw_params */
1295cfb0a873SVinod Koul 	format->s_freq = params->s_freq;
1296cfb0a873SVinod Koul 	format->channels = params->ch;
1297cfb0a873SVinod Koul 	format->valid_bit_depth = skl_get_bit_depth(params->s_fmt);
1298cfb0a873SVinod Koul 
1299cfb0a873SVinod Koul 	/*
1300cfb0a873SVinod Koul 	 * 16 bit is 16 bit container whereas 24 bit is in 32 bit
1301cfb0a873SVinod Koul 	 * container so update bit depth accordingly
1302cfb0a873SVinod Koul 	 */
1303cfb0a873SVinod Koul 	switch (format->valid_bit_depth) {
1304cfb0a873SVinod Koul 	case SKL_DEPTH_16BIT:
1305cfb0a873SVinod Koul 		format->bit_depth = format->valid_bit_depth;
1306cfb0a873SVinod Koul 		break;
1307cfb0a873SVinod Koul 
1308cfb0a873SVinod Koul 	case SKL_DEPTH_24BIT:
13096654f39eSJeeja KP 	case SKL_DEPTH_32BIT:
1310cfb0a873SVinod Koul 		format->bit_depth = SKL_DEPTH_32BIT;
1311cfb0a873SVinod Koul 		break;
1312cfb0a873SVinod Koul 
1313cfb0a873SVinod Koul 	default:
1314cfb0a873SVinod Koul 		dev_err(dev, "Invalid bit depth %x for pipe\n",
1315cfb0a873SVinod Koul 				format->valid_bit_depth);
1316cfb0a873SVinod Koul 		return -EINVAL;
1317cfb0a873SVinod Koul 	}
1318cfb0a873SVinod Koul 
1319cfb0a873SVinod Koul 	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1320cfb0a873SVinod Koul 		mconfig->ibs = (format->s_freq / 1000) *
1321cfb0a873SVinod Koul 				(format->channels) *
1322cfb0a873SVinod Koul 				(format->bit_depth >> 3);
1323cfb0a873SVinod Koul 	} else {
1324cfb0a873SVinod Koul 		mconfig->obs = (format->s_freq / 1000) *
1325cfb0a873SVinod Koul 				(format->channels) *
1326cfb0a873SVinod Koul 				(format->bit_depth >> 3);
1327cfb0a873SVinod Koul 	}
1328cfb0a873SVinod Koul 
1329cfb0a873SVinod Koul 	return 0;
1330cfb0a873SVinod Koul }
1331cfb0a873SVinod Koul 
1332cfb0a873SVinod Koul /*
1333cfb0a873SVinod Koul  * Query the module config for the FE DAI
1334cfb0a873SVinod Koul  * This is used to find the hw_params set for that DAI and apply to FE
1335cfb0a873SVinod Koul  * pipeline
1336cfb0a873SVinod Koul  */
1337cfb0a873SVinod Koul struct skl_module_cfg *
1338cfb0a873SVinod Koul skl_tplg_fe_get_cpr_module(struct snd_soc_dai *dai, int stream)
1339cfb0a873SVinod Koul {
1340cfb0a873SVinod Koul 	struct snd_soc_dapm_widget *w;
1341cfb0a873SVinod Koul 	struct snd_soc_dapm_path *p = NULL;
1342cfb0a873SVinod Koul 
1343cfb0a873SVinod Koul 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1344cfb0a873SVinod Koul 		w = dai->playback_widget;
1345f0900eb2SSubhransu S. Prusty 		snd_soc_dapm_widget_for_each_sink_path(w, p) {
1346cfb0a873SVinod Koul 			if (p->connect && p->sink->power &&
1347a28f51dbSJeeja KP 					!is_skl_dsp_widget_type(p->sink))
1348cfb0a873SVinod Koul 				continue;
1349cfb0a873SVinod Koul 
1350cfb0a873SVinod Koul 			if (p->sink->priv) {
1351cfb0a873SVinod Koul 				dev_dbg(dai->dev, "set params for %s\n",
1352cfb0a873SVinod Koul 						p->sink->name);
1353cfb0a873SVinod Koul 				return p->sink->priv;
1354cfb0a873SVinod Koul 			}
1355cfb0a873SVinod Koul 		}
1356cfb0a873SVinod Koul 	} else {
1357cfb0a873SVinod Koul 		w = dai->capture_widget;
1358f0900eb2SSubhransu S. Prusty 		snd_soc_dapm_widget_for_each_source_path(w, p) {
1359cfb0a873SVinod Koul 			if (p->connect && p->source->power &&
1360a28f51dbSJeeja KP 					!is_skl_dsp_widget_type(p->source))
1361cfb0a873SVinod Koul 				continue;
1362cfb0a873SVinod Koul 
1363cfb0a873SVinod Koul 			if (p->source->priv) {
1364cfb0a873SVinod Koul 				dev_dbg(dai->dev, "set params for %s\n",
1365cfb0a873SVinod Koul 						p->source->name);
1366cfb0a873SVinod Koul 				return p->source->priv;
1367cfb0a873SVinod Koul 			}
1368cfb0a873SVinod Koul 		}
1369cfb0a873SVinod Koul 	}
1370cfb0a873SVinod Koul 
1371cfb0a873SVinod Koul 	return NULL;
1372cfb0a873SVinod Koul }
1373cfb0a873SVinod Koul 
1374718a42b5SDharageswari.R static struct skl_module_cfg *skl_get_mconfig_pb_cpr(
1375718a42b5SDharageswari.R 		struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
1376718a42b5SDharageswari.R {
1377718a42b5SDharageswari.R 	struct snd_soc_dapm_path *p;
1378718a42b5SDharageswari.R 	struct skl_module_cfg *mconfig = NULL;
1379718a42b5SDharageswari.R 
1380718a42b5SDharageswari.R 	snd_soc_dapm_widget_for_each_source_path(w, p) {
1381718a42b5SDharageswari.R 		if (w->endpoints[SND_SOC_DAPM_DIR_OUT] > 0) {
1382718a42b5SDharageswari.R 			if (p->connect &&
1383718a42b5SDharageswari.R 				    (p->sink->id == snd_soc_dapm_aif_out) &&
1384718a42b5SDharageswari.R 				    p->source->priv) {
1385718a42b5SDharageswari.R 				mconfig = p->source->priv;
1386718a42b5SDharageswari.R 				return mconfig;
1387718a42b5SDharageswari.R 			}
1388718a42b5SDharageswari.R 			mconfig = skl_get_mconfig_pb_cpr(dai, p->source);
1389718a42b5SDharageswari.R 			if (mconfig)
1390718a42b5SDharageswari.R 				return mconfig;
1391718a42b5SDharageswari.R 		}
1392718a42b5SDharageswari.R 	}
1393718a42b5SDharageswari.R 	return mconfig;
1394718a42b5SDharageswari.R }
1395718a42b5SDharageswari.R 
1396718a42b5SDharageswari.R static struct skl_module_cfg *skl_get_mconfig_cap_cpr(
1397718a42b5SDharageswari.R 		struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
1398718a42b5SDharageswari.R {
1399718a42b5SDharageswari.R 	struct snd_soc_dapm_path *p;
1400718a42b5SDharageswari.R 	struct skl_module_cfg *mconfig = NULL;
1401718a42b5SDharageswari.R 
1402718a42b5SDharageswari.R 	snd_soc_dapm_widget_for_each_sink_path(w, p) {
1403718a42b5SDharageswari.R 		if (w->endpoints[SND_SOC_DAPM_DIR_IN] > 0) {
1404718a42b5SDharageswari.R 			if (p->connect &&
1405718a42b5SDharageswari.R 				    (p->source->id == snd_soc_dapm_aif_in) &&
1406718a42b5SDharageswari.R 				    p->sink->priv) {
1407718a42b5SDharageswari.R 				mconfig = p->sink->priv;
1408718a42b5SDharageswari.R 				return mconfig;
1409718a42b5SDharageswari.R 			}
1410718a42b5SDharageswari.R 			mconfig = skl_get_mconfig_cap_cpr(dai, p->sink);
1411718a42b5SDharageswari.R 			if (mconfig)
1412718a42b5SDharageswari.R 				return mconfig;
1413718a42b5SDharageswari.R 		}
1414718a42b5SDharageswari.R 	}
1415718a42b5SDharageswari.R 	return mconfig;
1416718a42b5SDharageswari.R }
1417718a42b5SDharageswari.R 
1418718a42b5SDharageswari.R struct skl_module_cfg *
1419718a42b5SDharageswari.R skl_tplg_be_get_cpr_module(struct snd_soc_dai *dai, int stream)
1420718a42b5SDharageswari.R {
1421718a42b5SDharageswari.R 	struct snd_soc_dapm_widget *w;
1422718a42b5SDharageswari.R 	struct skl_module_cfg *mconfig;
1423718a42b5SDharageswari.R 
1424718a42b5SDharageswari.R 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1425718a42b5SDharageswari.R 		w = dai->playback_widget;
1426718a42b5SDharageswari.R 		mconfig = skl_get_mconfig_pb_cpr(dai, w);
1427718a42b5SDharageswari.R 	} else {
1428718a42b5SDharageswari.R 		w = dai->capture_widget;
1429718a42b5SDharageswari.R 		mconfig = skl_get_mconfig_cap_cpr(dai, w);
1430718a42b5SDharageswari.R 	}
1431718a42b5SDharageswari.R 	return mconfig;
1432718a42b5SDharageswari.R }
1433718a42b5SDharageswari.R 
1434cfb0a873SVinod Koul static u8 skl_tplg_be_link_type(int dev_type)
1435cfb0a873SVinod Koul {
1436cfb0a873SVinod Koul 	int ret;
1437cfb0a873SVinod Koul 
1438cfb0a873SVinod Koul 	switch (dev_type) {
1439cfb0a873SVinod Koul 	case SKL_DEVICE_BT:
1440cfb0a873SVinod Koul 		ret = NHLT_LINK_SSP;
1441cfb0a873SVinod Koul 		break;
1442cfb0a873SVinod Koul 
1443cfb0a873SVinod Koul 	case SKL_DEVICE_DMIC:
1444cfb0a873SVinod Koul 		ret = NHLT_LINK_DMIC;
1445cfb0a873SVinod Koul 		break;
1446cfb0a873SVinod Koul 
1447cfb0a873SVinod Koul 	case SKL_DEVICE_I2S:
1448cfb0a873SVinod Koul 		ret = NHLT_LINK_SSP;
1449cfb0a873SVinod Koul 		break;
1450cfb0a873SVinod Koul 
1451cfb0a873SVinod Koul 	case SKL_DEVICE_HDALINK:
1452cfb0a873SVinod Koul 		ret = NHLT_LINK_HDA;
1453cfb0a873SVinod Koul 		break;
1454cfb0a873SVinod Koul 
1455cfb0a873SVinod Koul 	default:
1456cfb0a873SVinod Koul 		ret = NHLT_LINK_INVALID;
1457cfb0a873SVinod Koul 		break;
1458cfb0a873SVinod Koul 	}
1459cfb0a873SVinod Koul 
1460cfb0a873SVinod Koul 	return ret;
1461cfb0a873SVinod Koul }
1462cfb0a873SVinod Koul 
1463cfb0a873SVinod Koul /*
1464cfb0a873SVinod Koul  * Fill the BE gateway parameters
1465cfb0a873SVinod Koul  * The BE gateway expects a blob of parameters which are kept in the ACPI
1466cfb0a873SVinod Koul  * NHLT blob, so query the blob for interface type (i2s/pdm) and instance.
1467cfb0a873SVinod Koul  * The port can have multiple settings so pick based on the PCM
1468cfb0a873SVinod Koul  * parameters
1469cfb0a873SVinod Koul  */
1470cfb0a873SVinod Koul static int skl_tplg_be_fill_pipe_params(struct snd_soc_dai *dai,
1471cfb0a873SVinod Koul 				struct skl_module_cfg *mconfig,
1472cfb0a873SVinod Koul 				struct skl_pipe_params *params)
1473cfb0a873SVinod Koul {
1474cfb0a873SVinod Koul 	struct nhlt_specific_cfg *cfg;
1475cfb0a873SVinod Koul 	struct skl *skl = get_skl_ctx(dai->dev);
1476cfb0a873SVinod Koul 	int link_type = skl_tplg_be_link_type(mconfig->dev_type);
1477db2f586bSSenthilnathan Veppur 	u8 dev_type = skl_tplg_be_dev_type(mconfig->dev_type);
1478cfb0a873SVinod Koul 
14798871dcb9SJeeja KP 	skl_tplg_fill_dma_id(mconfig, params);
1480cfb0a873SVinod Koul 
1481b30c275eSJeeja KP 	if (link_type == NHLT_LINK_HDA)
1482b30c275eSJeeja KP 		return 0;
1483b30c275eSJeeja KP 
1484cfb0a873SVinod Koul 	/* update the blob based on virtual bus_id*/
1485cfb0a873SVinod Koul 	cfg = skl_get_ep_blob(skl, mconfig->vbus_id, link_type,
1486cfb0a873SVinod Koul 					params->s_fmt, params->ch,
1487db2f586bSSenthilnathan Veppur 					params->s_freq, params->stream,
1488db2f586bSSenthilnathan Veppur 					dev_type);
1489cfb0a873SVinod Koul 	if (cfg) {
1490cfb0a873SVinod Koul 		mconfig->formats_config.caps_size = cfg->size;
1491bc03281aSJeeja KP 		mconfig->formats_config.caps = (u32 *) &cfg->caps;
1492cfb0a873SVinod Koul 	} else {
1493cfb0a873SVinod Koul 		dev_err(dai->dev, "Blob NULL for id %x type %d dirn %d\n",
1494cfb0a873SVinod Koul 					mconfig->vbus_id, link_type,
1495cfb0a873SVinod Koul 					params->stream);
1496cfb0a873SVinod Koul 		dev_err(dai->dev, "PCM: ch %d, freq %d, fmt %d\n",
1497cfb0a873SVinod Koul 				 params->ch, params->s_freq, params->s_fmt);
1498cfb0a873SVinod Koul 		return -EINVAL;
1499cfb0a873SVinod Koul 	}
1500cfb0a873SVinod Koul 
1501cfb0a873SVinod Koul 	return 0;
1502cfb0a873SVinod Koul }
1503cfb0a873SVinod Koul 
1504cfb0a873SVinod Koul static int skl_tplg_be_set_src_pipe_params(struct snd_soc_dai *dai,
1505cfb0a873SVinod Koul 				struct snd_soc_dapm_widget *w,
1506cfb0a873SVinod Koul 				struct skl_pipe_params *params)
1507cfb0a873SVinod Koul {
1508cfb0a873SVinod Koul 	struct snd_soc_dapm_path *p;
15094d8adccbSSubhransu S. Prusty 	int ret = -EIO;
1510cfb0a873SVinod Koul 
1511f0900eb2SSubhransu S. Prusty 	snd_soc_dapm_widget_for_each_source_path(w, p) {
1512cfb0a873SVinod Koul 		if (p->connect && is_skl_dsp_widget_type(p->source) &&
1513cfb0a873SVinod Koul 						p->source->priv) {
1514cfb0a873SVinod Koul 
15159a03cb49SJeeja KP 			ret = skl_tplg_be_fill_pipe_params(dai,
15169a03cb49SJeeja KP 						p->source->priv, params);
15174d8adccbSSubhransu S. Prusty 			if (ret < 0)
15184d8adccbSSubhransu S. Prusty 				return ret;
1519cfb0a873SVinod Koul 		} else {
15209a03cb49SJeeja KP 			ret = skl_tplg_be_set_src_pipe_params(dai,
15219a03cb49SJeeja KP 						p->source, params);
15224d8adccbSSubhransu S. Prusty 			if (ret < 0)
15234d8adccbSSubhransu S. Prusty 				return ret;
1524cfb0a873SVinod Koul 		}
1525cfb0a873SVinod Koul 	}
1526cfb0a873SVinod Koul 
15274d8adccbSSubhransu S. Prusty 	return ret;
1528cfb0a873SVinod Koul }
1529cfb0a873SVinod Koul 
1530cfb0a873SVinod Koul static int skl_tplg_be_set_sink_pipe_params(struct snd_soc_dai *dai,
1531cfb0a873SVinod Koul 	struct snd_soc_dapm_widget *w, struct skl_pipe_params *params)
1532cfb0a873SVinod Koul {
1533cfb0a873SVinod Koul 	struct snd_soc_dapm_path *p = NULL;
15344d8adccbSSubhransu S. Prusty 	int ret = -EIO;
1535cfb0a873SVinod Koul 
1536f0900eb2SSubhransu S. Prusty 	snd_soc_dapm_widget_for_each_sink_path(w, p) {
1537cfb0a873SVinod Koul 		if (p->connect && is_skl_dsp_widget_type(p->sink) &&
1538cfb0a873SVinod Koul 						p->sink->priv) {
1539cfb0a873SVinod Koul 
15409a03cb49SJeeja KP 			ret = skl_tplg_be_fill_pipe_params(dai,
15419a03cb49SJeeja KP 						p->sink->priv, params);
15424d8adccbSSubhransu S. Prusty 			if (ret < 0)
15434d8adccbSSubhransu S. Prusty 				return ret;
15444d8adccbSSubhransu S. Prusty 		} else {
15454d8adccbSSubhransu S. Prusty 			ret = skl_tplg_be_set_sink_pipe_params(
1546cfb0a873SVinod Koul 						dai, p->sink, params);
15474d8adccbSSubhransu S. Prusty 			if (ret < 0)
15484d8adccbSSubhransu S. Prusty 				return ret;
1549cfb0a873SVinod Koul 		}
1550cfb0a873SVinod Koul 	}
1551cfb0a873SVinod Koul 
15524d8adccbSSubhransu S. Prusty 	return ret;
1553cfb0a873SVinod Koul }
1554cfb0a873SVinod Koul 
1555cfb0a873SVinod Koul /*
1556cfb0a873SVinod Koul  * BE hw_params can be a source parameters (capture) or sink parameters
1557cfb0a873SVinod Koul  * (playback). Based on sink and source we need to either find the source
1558cfb0a873SVinod Koul  * list or the sink list and set the pipeline parameters
1559cfb0a873SVinod Koul  */
1560cfb0a873SVinod Koul int skl_tplg_be_update_params(struct snd_soc_dai *dai,
1561cfb0a873SVinod Koul 				struct skl_pipe_params *params)
1562cfb0a873SVinod Koul {
1563cfb0a873SVinod Koul 	struct snd_soc_dapm_widget *w;
1564cfb0a873SVinod Koul 
1565cfb0a873SVinod Koul 	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1566cfb0a873SVinod Koul 		w = dai->playback_widget;
1567cfb0a873SVinod Koul 
1568cfb0a873SVinod Koul 		return skl_tplg_be_set_src_pipe_params(dai, w, params);
1569cfb0a873SVinod Koul 
1570cfb0a873SVinod Koul 	} else {
1571cfb0a873SVinod Koul 		w = dai->capture_widget;
1572cfb0a873SVinod Koul 
1573cfb0a873SVinod Koul 		return skl_tplg_be_set_sink_pipe_params(dai, w, params);
1574cfb0a873SVinod Koul 	}
1575cfb0a873SVinod Koul 
1576cfb0a873SVinod Koul 	return 0;
1577cfb0a873SVinod Koul }
15783af36706SVinod Koul 
15793af36706SVinod Koul static const struct snd_soc_tplg_widget_events skl_tplg_widget_ops[] = {
15803af36706SVinod Koul 	{SKL_MIXER_EVENT, skl_tplg_mixer_event},
15813af36706SVinod Koul 	{SKL_VMIXER_EVENT, skl_tplg_vmixer_event},
15823af36706SVinod Koul 	{SKL_PGA_EVENT, skl_tplg_pga_event},
15833af36706SVinod Koul };
15843af36706SVinod Koul 
1585140adfbaSJeeja KP static const struct snd_soc_tplg_bytes_ext_ops skl_tlv_ops[] = {
1586140adfbaSJeeja KP 	{SKL_CONTROL_TYPE_BYTE_TLV, skl_tplg_tlv_control_get,
1587140adfbaSJeeja KP 					skl_tplg_tlv_control_set},
1588140adfbaSJeeja KP };
1589140adfbaSJeeja KP 
15906277e832SShreyas NC static int skl_tplg_fill_pipe_tkn(struct device *dev,
15916277e832SShreyas NC 			struct skl_pipe *pipe, u32 tkn,
15926277e832SShreyas NC 			u32 tkn_val)
15933af36706SVinod Koul {
15943af36706SVinod Koul 
15956277e832SShreyas NC 	switch (tkn) {
15966277e832SShreyas NC 	case SKL_TKN_U32_PIPE_CONN_TYPE:
15976277e832SShreyas NC 		pipe->conn_type = tkn_val;
15986277e832SShreyas NC 		break;
15996277e832SShreyas NC 
16006277e832SShreyas NC 	case SKL_TKN_U32_PIPE_PRIORITY:
16016277e832SShreyas NC 		pipe->pipe_priority = tkn_val;
16026277e832SShreyas NC 		break;
16036277e832SShreyas NC 
16046277e832SShreyas NC 	case SKL_TKN_U32_PIPE_MEM_PGS:
16056277e832SShreyas NC 		pipe->memory_pages = tkn_val;
16066277e832SShreyas NC 		break;
16076277e832SShreyas NC 
16088a0cb236SVinod Koul 	case SKL_TKN_U32_PMODE:
16098a0cb236SVinod Koul 		pipe->lp_mode = tkn_val;
16108a0cb236SVinod Koul 		break;
16118a0cb236SVinod Koul 
16126277e832SShreyas NC 	default:
16136277e832SShreyas NC 		dev_err(dev, "Token not handled %d\n", tkn);
16146277e832SShreyas NC 		return -EINVAL;
16153af36706SVinod Koul 	}
16166277e832SShreyas NC 
16176277e832SShreyas NC 	return 0;
16183af36706SVinod Koul }
16193af36706SVinod Koul 
16203af36706SVinod Koul /*
16216277e832SShreyas NC  * Add pipeline by parsing the relevant tokens
16226277e832SShreyas NC  * Return an existing pipe if the pipe already exists.
16233af36706SVinod Koul  */
16246277e832SShreyas NC static int skl_tplg_add_pipe(struct device *dev,
16256277e832SShreyas NC 		struct skl_module_cfg *mconfig, struct skl *skl,
16266277e832SShreyas NC 		struct snd_soc_tplg_vendor_value_elem *tkn_elem)
16273af36706SVinod Koul {
16283af36706SVinod Koul 	struct skl_pipeline *ppl;
16293af36706SVinod Koul 	struct skl_pipe *pipe;
16303af36706SVinod Koul 	struct skl_pipe_params *params;
16313af36706SVinod Koul 
16323af36706SVinod Koul 	list_for_each_entry(ppl, &skl->ppl_list, node) {
16336277e832SShreyas NC 		if (ppl->pipe->ppl_id == tkn_elem->value) {
16346277e832SShreyas NC 			mconfig->pipe = ppl->pipe;
16356277e832SShreyas NC 			return EEXIST;
16366277e832SShreyas NC 		}
16373af36706SVinod Koul 	}
16383af36706SVinod Koul 
16393af36706SVinod Koul 	ppl = devm_kzalloc(dev, sizeof(*ppl), GFP_KERNEL);
16403af36706SVinod Koul 	if (!ppl)
16416277e832SShreyas NC 		return -ENOMEM;
16423af36706SVinod Koul 
16433af36706SVinod Koul 	pipe = devm_kzalloc(dev, sizeof(*pipe), GFP_KERNEL);
16443af36706SVinod Koul 	if (!pipe)
16456277e832SShreyas NC 		return -ENOMEM;
16463af36706SVinod Koul 
16473af36706SVinod Koul 	params = devm_kzalloc(dev, sizeof(*params), GFP_KERNEL);
16483af36706SVinod Koul 	if (!params)
16496277e832SShreyas NC 		return -ENOMEM;
16503af36706SVinod Koul 
16513af36706SVinod Koul 	pipe->p_params = params;
16526277e832SShreyas NC 	pipe->ppl_id = tkn_elem->value;
16533af36706SVinod Koul 	INIT_LIST_HEAD(&pipe->w_list);
16543af36706SVinod Koul 
16553af36706SVinod Koul 	ppl->pipe = pipe;
16563af36706SVinod Koul 	list_add(&ppl->node, &skl->ppl_list);
16573af36706SVinod Koul 
16586277e832SShreyas NC 	mconfig->pipe = pipe;
16596277e832SShreyas NC 	mconfig->pipe->state = SKL_PIPE_INVALID;
16606277e832SShreyas NC 
16616277e832SShreyas NC 	return 0;
16623af36706SVinod Koul }
16633af36706SVinod Koul 
16646277e832SShreyas NC static int skl_tplg_fill_pin(struct device *dev, u32 tkn,
16656277e832SShreyas NC 			struct skl_module_pin *m_pin,
16666277e832SShreyas NC 			int pin_index, u32 value)
16676277e832SShreyas NC {
16686277e832SShreyas NC 	switch (tkn) {
16696277e832SShreyas NC 	case SKL_TKN_U32_PIN_MOD_ID:
16706277e832SShreyas NC 		m_pin[pin_index].id.module_id = value;
16716277e832SShreyas NC 		break;
16726277e832SShreyas NC 
16736277e832SShreyas NC 	case SKL_TKN_U32_PIN_INST_ID:
16746277e832SShreyas NC 		m_pin[pin_index].id.instance_id = value;
16756277e832SShreyas NC 		break;
16766277e832SShreyas NC 
16776277e832SShreyas NC 	default:
16786277e832SShreyas NC 		dev_err(dev, "%d Not a pin token\n", value);
16796277e832SShreyas NC 		return -EINVAL;
16806277e832SShreyas NC 	}
16816277e832SShreyas NC 
16826277e832SShreyas NC 	return 0;
16836277e832SShreyas NC }
16846277e832SShreyas NC 
16856277e832SShreyas NC /*
16866277e832SShreyas NC  * Parse for pin config specific tokens to fill up the
16876277e832SShreyas NC  * module private data
16886277e832SShreyas NC  */
16896277e832SShreyas NC static int skl_tplg_fill_pins_info(struct device *dev,
16906277e832SShreyas NC 		struct skl_module_cfg *mconfig,
16916277e832SShreyas NC 		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
16926277e832SShreyas NC 		int dir, int pin_count)
16936277e832SShreyas NC {
16946277e832SShreyas NC 	int ret;
16956277e832SShreyas NC 	struct skl_module_pin *m_pin;
16966277e832SShreyas NC 
16976277e832SShreyas NC 	switch (dir) {
16986277e832SShreyas NC 	case SKL_DIR_IN:
16996277e832SShreyas NC 		m_pin = mconfig->m_in_pin;
17006277e832SShreyas NC 		break;
17016277e832SShreyas NC 
17026277e832SShreyas NC 	case SKL_DIR_OUT:
17036277e832SShreyas NC 		m_pin = mconfig->m_out_pin;
17046277e832SShreyas NC 		break;
17056277e832SShreyas NC 
17066277e832SShreyas NC 	default:
1707ecd286a9SColin Ian King 		dev_err(dev, "Invalid direction value\n");
17086277e832SShreyas NC 		return -EINVAL;
17096277e832SShreyas NC 	}
17106277e832SShreyas NC 
17116277e832SShreyas NC 	ret = skl_tplg_fill_pin(dev, tkn_elem->token,
17126277e832SShreyas NC 			m_pin, pin_count, tkn_elem->value);
17136277e832SShreyas NC 
17146277e832SShreyas NC 	if (ret < 0)
17156277e832SShreyas NC 		return ret;
17166277e832SShreyas NC 
17176277e832SShreyas NC 	m_pin[pin_count].in_use = false;
17186277e832SShreyas NC 	m_pin[pin_count].pin_state = SKL_PIN_UNBIND;
17196277e832SShreyas NC 
17206277e832SShreyas NC 	return 0;
17216277e832SShreyas NC }
17226277e832SShreyas NC 
17236277e832SShreyas NC /*
17246277e832SShreyas NC  * Fill up input/output module config format based
17256277e832SShreyas NC  * on the direction
17266277e832SShreyas NC  */
17276277e832SShreyas NC static int skl_tplg_fill_fmt(struct device *dev,
17286277e832SShreyas NC 		struct skl_module_cfg *mconfig,	u32 tkn,
17296277e832SShreyas NC 		u32 value, u32 dir, u32 pin_count)
17306277e832SShreyas NC {
17316277e832SShreyas NC 	struct skl_module_fmt *dst_fmt;
17326277e832SShreyas NC 
17336277e832SShreyas NC 	switch (dir) {
17346277e832SShreyas NC 	case SKL_DIR_IN:
17356277e832SShreyas NC 		dst_fmt = mconfig->in_fmt;
17366277e832SShreyas NC 		dst_fmt += pin_count;
17376277e832SShreyas NC 		break;
17386277e832SShreyas NC 
17396277e832SShreyas NC 	case SKL_DIR_OUT:
17406277e832SShreyas NC 		dst_fmt = mconfig->out_fmt;
17416277e832SShreyas NC 		dst_fmt += pin_count;
17426277e832SShreyas NC 		break;
17436277e832SShreyas NC 
17446277e832SShreyas NC 	default:
1745ecd286a9SColin Ian King 		dev_err(dev, "Invalid direction value\n");
17466277e832SShreyas NC 		return -EINVAL;
17476277e832SShreyas NC 	}
17486277e832SShreyas NC 
17496277e832SShreyas NC 	switch (tkn) {
17506277e832SShreyas NC 	case SKL_TKN_U32_FMT_CH:
17516277e832SShreyas NC 		dst_fmt->channels  = value;
17526277e832SShreyas NC 		break;
17536277e832SShreyas NC 
17546277e832SShreyas NC 	case SKL_TKN_U32_FMT_FREQ:
17556277e832SShreyas NC 		dst_fmt->s_freq = value;
17566277e832SShreyas NC 		break;
17576277e832SShreyas NC 
17586277e832SShreyas NC 	case SKL_TKN_U32_FMT_BIT_DEPTH:
17596277e832SShreyas NC 		dst_fmt->bit_depth = value;
17606277e832SShreyas NC 		break;
17616277e832SShreyas NC 
17626277e832SShreyas NC 	case SKL_TKN_U32_FMT_SAMPLE_SIZE:
17636277e832SShreyas NC 		dst_fmt->valid_bit_depth = value;
17646277e832SShreyas NC 		break;
17656277e832SShreyas NC 
17666277e832SShreyas NC 	case SKL_TKN_U32_FMT_CH_CONFIG:
17676277e832SShreyas NC 		dst_fmt->ch_cfg = value;
17686277e832SShreyas NC 		break;
17696277e832SShreyas NC 
17706277e832SShreyas NC 	case SKL_TKN_U32_FMT_INTERLEAVE:
17716277e832SShreyas NC 		dst_fmt->interleaving_style = value;
17726277e832SShreyas NC 		break;
17736277e832SShreyas NC 
17746277e832SShreyas NC 	case SKL_TKN_U32_FMT_SAMPLE_TYPE:
17756277e832SShreyas NC 		dst_fmt->sample_type = value;
17766277e832SShreyas NC 		break;
17776277e832SShreyas NC 
17786277e832SShreyas NC 	case SKL_TKN_U32_FMT_CH_MAP:
17796277e832SShreyas NC 		dst_fmt->ch_map = value;
17806277e832SShreyas NC 		break;
17816277e832SShreyas NC 
17826277e832SShreyas NC 	default:
1783ecd286a9SColin Ian King 		dev_err(dev, "Invalid token %d\n", tkn);
17846277e832SShreyas NC 		return -EINVAL;
17856277e832SShreyas NC 	}
17866277e832SShreyas NC 
17876277e832SShreyas NC 	return 0;
17886277e832SShreyas NC }
17896277e832SShreyas NC 
17906277e832SShreyas NC static int skl_tplg_get_uuid(struct device *dev, struct skl_module_cfg *mconfig,
17916277e832SShreyas NC 	      struct snd_soc_tplg_vendor_uuid_elem *uuid_tkn)
17926277e832SShreyas NC {
17936277e832SShreyas NC 	if (uuid_tkn->token == SKL_TKN_UUID)
17946277e832SShreyas NC 		memcpy(&mconfig->guid, &uuid_tkn->uuid, 16);
17956277e832SShreyas NC 	else {
1796ecd286a9SColin Ian King 		dev_err(dev, "Not an UUID token tkn %d\n", uuid_tkn->token);
17976277e832SShreyas NC 		return -EINVAL;
17986277e832SShreyas NC 	}
17996277e832SShreyas NC 
18006277e832SShreyas NC 	return 0;
18016277e832SShreyas NC }
18026277e832SShreyas NC 
18036277e832SShreyas NC static void skl_tplg_fill_pin_dynamic_val(
18046277e832SShreyas NC 		struct skl_module_pin *mpin, u32 pin_count, u32 value)
18054cd9899fSHardik T Shah {
18064cd9899fSHardik T Shah 	int i;
18074cd9899fSHardik T Shah 
18086277e832SShreyas NC 	for (i = 0; i < pin_count; i++)
18096277e832SShreyas NC 		mpin[i].is_dynamic = value;
18104cd9899fSHardik T Shah }
18116277e832SShreyas NC 
18126277e832SShreyas NC /*
18136277e832SShreyas NC  * Parse tokens to fill up the module private data
18146277e832SShreyas NC  */
18156277e832SShreyas NC static int skl_tplg_get_token(struct device *dev,
18166277e832SShreyas NC 		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
18176277e832SShreyas NC 		struct skl *skl, struct skl_module_cfg *mconfig)
18186277e832SShreyas NC {
18196277e832SShreyas NC 	int tkn_count = 0;
18206277e832SShreyas NC 	int ret;
18216277e832SShreyas NC 	static int is_pipe_exists;
18226277e832SShreyas NC 	static int pin_index, dir;
18236277e832SShreyas NC 
18246277e832SShreyas NC 	if (tkn_elem->token > SKL_TKN_MAX)
18256277e832SShreyas NC 		return -EINVAL;
18266277e832SShreyas NC 
18276277e832SShreyas NC 	switch (tkn_elem->token) {
18286277e832SShreyas NC 	case SKL_TKN_U8_IN_QUEUE_COUNT:
18296277e832SShreyas NC 		mconfig->max_in_queue = tkn_elem->value;
18306277e832SShreyas NC 		mconfig->m_in_pin = devm_kzalloc(dev, mconfig->max_in_queue *
18316277e832SShreyas NC 					sizeof(*mconfig->m_in_pin),
18326277e832SShreyas NC 					GFP_KERNEL);
18336277e832SShreyas NC 		if (!mconfig->m_in_pin)
18346277e832SShreyas NC 			return -ENOMEM;
18356277e832SShreyas NC 
18366277e832SShreyas NC 		break;
18376277e832SShreyas NC 
18386277e832SShreyas NC 	case SKL_TKN_U8_OUT_QUEUE_COUNT:
18396277e832SShreyas NC 		mconfig->max_out_queue = tkn_elem->value;
18406277e832SShreyas NC 		mconfig->m_out_pin = devm_kzalloc(dev, mconfig->max_out_queue *
18416277e832SShreyas NC 					sizeof(*mconfig->m_out_pin),
18426277e832SShreyas NC 					GFP_KERNEL);
18436277e832SShreyas NC 
18446277e832SShreyas NC 		if (!mconfig->m_out_pin)
18456277e832SShreyas NC 			return -ENOMEM;
18466277e832SShreyas NC 
18476277e832SShreyas NC 		break;
18486277e832SShreyas NC 
18496277e832SShreyas NC 	case SKL_TKN_U8_DYN_IN_PIN:
18506277e832SShreyas NC 		if (!mconfig->m_in_pin)
18516277e832SShreyas NC 			return -ENOMEM;
18526277e832SShreyas NC 
18536277e832SShreyas NC 		skl_tplg_fill_pin_dynamic_val(mconfig->m_in_pin,
18546277e832SShreyas NC 			mconfig->max_in_queue, tkn_elem->value);
18556277e832SShreyas NC 
18566277e832SShreyas NC 		break;
18576277e832SShreyas NC 
18586277e832SShreyas NC 	case SKL_TKN_U8_DYN_OUT_PIN:
18596277e832SShreyas NC 		if (!mconfig->m_out_pin)
18606277e832SShreyas NC 			return -ENOMEM;
18616277e832SShreyas NC 
18626277e832SShreyas NC 		skl_tplg_fill_pin_dynamic_val(mconfig->m_out_pin,
18636277e832SShreyas NC 			mconfig->max_out_queue, tkn_elem->value);
18646277e832SShreyas NC 
18656277e832SShreyas NC 		break;
18666277e832SShreyas NC 
18676277e832SShreyas NC 	case SKL_TKN_U8_TIME_SLOT:
18686277e832SShreyas NC 		mconfig->time_slot = tkn_elem->value;
18696277e832SShreyas NC 		break;
18706277e832SShreyas NC 
18716277e832SShreyas NC 	case SKL_TKN_U8_CORE_ID:
18726277e832SShreyas NC 		mconfig->core_id = tkn_elem->value;
18736277e832SShreyas NC 
18746277e832SShreyas NC 	case SKL_TKN_U8_MOD_TYPE:
18756277e832SShreyas NC 		mconfig->m_type = tkn_elem->value;
18766277e832SShreyas NC 		break;
18776277e832SShreyas NC 
18786277e832SShreyas NC 	case SKL_TKN_U8_DEV_TYPE:
18796277e832SShreyas NC 		mconfig->dev_type = tkn_elem->value;
18806277e832SShreyas NC 		break;
18816277e832SShreyas NC 
18826277e832SShreyas NC 	case SKL_TKN_U8_HW_CONN_TYPE:
18836277e832SShreyas NC 		mconfig->hw_conn_type = tkn_elem->value;
18846277e832SShreyas NC 		break;
18856277e832SShreyas NC 
18866277e832SShreyas NC 	case SKL_TKN_U16_MOD_INST_ID:
18876277e832SShreyas NC 		mconfig->id.instance_id =
18886277e832SShreyas NC 		tkn_elem->value;
18896277e832SShreyas NC 		break;
18906277e832SShreyas NC 
18916277e832SShreyas NC 	case SKL_TKN_U32_MEM_PAGES:
18926277e832SShreyas NC 		mconfig->mem_pages = tkn_elem->value;
18936277e832SShreyas NC 		break;
18946277e832SShreyas NC 
18956277e832SShreyas NC 	case SKL_TKN_U32_MAX_MCPS:
18966277e832SShreyas NC 		mconfig->mcps = tkn_elem->value;
18976277e832SShreyas NC 		break;
18986277e832SShreyas NC 
18996277e832SShreyas NC 	case SKL_TKN_U32_OBS:
19006277e832SShreyas NC 		mconfig->obs = tkn_elem->value;
19016277e832SShreyas NC 		break;
19026277e832SShreyas NC 
19036277e832SShreyas NC 	case SKL_TKN_U32_IBS:
19046277e832SShreyas NC 		mconfig->ibs = tkn_elem->value;
19056277e832SShreyas NC 		break;
19066277e832SShreyas NC 
19076277e832SShreyas NC 	case SKL_TKN_U32_VBUS_ID:
19086277e832SShreyas NC 		mconfig->vbus_id = tkn_elem->value;
19096277e832SShreyas NC 		break;
19106277e832SShreyas NC 
19116277e832SShreyas NC 	case SKL_TKN_U32_PARAMS_FIXUP:
19126277e832SShreyas NC 		mconfig->params_fixup = tkn_elem->value;
19136277e832SShreyas NC 		break;
19146277e832SShreyas NC 
19156277e832SShreyas NC 	case SKL_TKN_U32_CONVERTER:
19166277e832SShreyas NC 		mconfig->converter = tkn_elem->value;
19176277e832SShreyas NC 		break;
19186277e832SShreyas NC 
19196bd9dcf3SVinod Koul 	case SKL_TKL_U32_D0I3_CAPS:
19206bd9dcf3SVinod Koul 		mconfig->d0i3_caps = tkn_elem->value;
19216bd9dcf3SVinod Koul 		break;
19226bd9dcf3SVinod Koul 
19236277e832SShreyas NC 	case SKL_TKN_U32_PIPE_ID:
19246277e832SShreyas NC 		ret = skl_tplg_add_pipe(dev,
19256277e832SShreyas NC 				mconfig, skl, tkn_elem);
19266277e832SShreyas NC 
19276277e832SShreyas NC 		if (ret < 0)
19286277e832SShreyas NC 			return is_pipe_exists;
19296277e832SShreyas NC 
19306277e832SShreyas NC 		if (ret == EEXIST)
19316277e832SShreyas NC 			is_pipe_exists = 1;
19326277e832SShreyas NC 
19336277e832SShreyas NC 		break;
19346277e832SShreyas NC 
19356277e832SShreyas NC 	case SKL_TKN_U32_PIPE_CONN_TYPE:
19366277e832SShreyas NC 	case SKL_TKN_U32_PIPE_PRIORITY:
19376277e832SShreyas NC 	case SKL_TKN_U32_PIPE_MEM_PGS:
19388a0cb236SVinod Koul 	case SKL_TKN_U32_PMODE:
19396277e832SShreyas NC 		if (is_pipe_exists) {
19406277e832SShreyas NC 			ret = skl_tplg_fill_pipe_tkn(dev, mconfig->pipe,
19416277e832SShreyas NC 					tkn_elem->token, tkn_elem->value);
19426277e832SShreyas NC 			if (ret < 0)
19436277e832SShreyas NC 				return ret;
19446277e832SShreyas NC 		}
19456277e832SShreyas NC 
19466277e832SShreyas NC 		break;
19476277e832SShreyas NC 
19486277e832SShreyas NC 	/*
19496277e832SShreyas NC 	 * SKL_TKN_U32_DIR_PIN_COUNT token has the value for both
19506277e832SShreyas NC 	 * direction and the pin count. The first four bits represent
19516277e832SShreyas NC 	 * direction and next four the pin count.
19526277e832SShreyas NC 	 */
19536277e832SShreyas NC 	case SKL_TKN_U32_DIR_PIN_COUNT:
19546277e832SShreyas NC 		dir = tkn_elem->value & SKL_IN_DIR_BIT_MASK;
19556277e832SShreyas NC 		pin_index = (tkn_elem->value &
19566277e832SShreyas NC 			SKL_PIN_COUNT_MASK) >> 4;
19576277e832SShreyas NC 
19586277e832SShreyas NC 		break;
19596277e832SShreyas NC 
19606277e832SShreyas NC 	case SKL_TKN_U32_FMT_CH:
19616277e832SShreyas NC 	case SKL_TKN_U32_FMT_FREQ:
19626277e832SShreyas NC 	case SKL_TKN_U32_FMT_BIT_DEPTH:
19636277e832SShreyas NC 	case SKL_TKN_U32_FMT_SAMPLE_SIZE:
19646277e832SShreyas NC 	case SKL_TKN_U32_FMT_CH_CONFIG:
19656277e832SShreyas NC 	case SKL_TKN_U32_FMT_INTERLEAVE:
19666277e832SShreyas NC 	case SKL_TKN_U32_FMT_SAMPLE_TYPE:
19676277e832SShreyas NC 	case SKL_TKN_U32_FMT_CH_MAP:
19686277e832SShreyas NC 		ret = skl_tplg_fill_fmt(dev, mconfig, tkn_elem->token,
19696277e832SShreyas NC 				tkn_elem->value, dir, pin_index);
19706277e832SShreyas NC 
19716277e832SShreyas NC 		if (ret < 0)
19726277e832SShreyas NC 			return ret;
19736277e832SShreyas NC 
19746277e832SShreyas NC 		break;
19756277e832SShreyas NC 
19766277e832SShreyas NC 	case SKL_TKN_U32_PIN_MOD_ID:
19776277e832SShreyas NC 	case SKL_TKN_U32_PIN_INST_ID:
19786277e832SShreyas NC 		ret = skl_tplg_fill_pins_info(dev,
19796277e832SShreyas NC 				mconfig, tkn_elem, dir,
19806277e832SShreyas NC 				pin_index);
19816277e832SShreyas NC 		if (ret < 0)
19826277e832SShreyas NC 			return ret;
19836277e832SShreyas NC 
19846277e832SShreyas NC 		break;
19856277e832SShreyas NC 
19866277e832SShreyas NC 	case SKL_TKN_U32_CAPS_SIZE:
19876277e832SShreyas NC 		mconfig->formats_config.caps_size =
19886277e832SShreyas NC 			tkn_elem->value;
19896277e832SShreyas NC 
19906277e832SShreyas NC 		break;
19916277e832SShreyas NC 
19926277e832SShreyas NC 	case SKL_TKN_U32_PROC_DOMAIN:
19936277e832SShreyas NC 		mconfig->domain =
19946277e832SShreyas NC 			tkn_elem->value;
19956277e832SShreyas NC 
19966277e832SShreyas NC 		break;
19976277e832SShreyas NC 
19986277e832SShreyas NC 	case SKL_TKN_U8_IN_PIN_TYPE:
19996277e832SShreyas NC 	case SKL_TKN_U8_OUT_PIN_TYPE:
20006277e832SShreyas NC 	case SKL_TKN_U8_CONN_TYPE:
20016277e832SShreyas NC 		break;
20026277e832SShreyas NC 
20036277e832SShreyas NC 	default:
20046277e832SShreyas NC 		dev_err(dev, "Token %d not handled\n",
20056277e832SShreyas NC 				tkn_elem->token);
20066277e832SShreyas NC 		return -EINVAL;
20076277e832SShreyas NC 	}
20086277e832SShreyas NC 
20096277e832SShreyas NC 	tkn_count++;
20106277e832SShreyas NC 
20116277e832SShreyas NC 	return tkn_count;
20126277e832SShreyas NC }
20136277e832SShreyas NC 
20146277e832SShreyas NC /*
20156277e832SShreyas NC  * Parse the vendor array for specific tokens to construct
20166277e832SShreyas NC  * module private data
20176277e832SShreyas NC  */
20186277e832SShreyas NC static int skl_tplg_get_tokens(struct device *dev,
20196277e832SShreyas NC 		char *pvt_data,	struct skl *skl,
20206277e832SShreyas NC 		struct skl_module_cfg *mconfig, int block_size)
20216277e832SShreyas NC {
20226277e832SShreyas NC 	struct snd_soc_tplg_vendor_array *array;
20236277e832SShreyas NC 	struct snd_soc_tplg_vendor_value_elem *tkn_elem;
20246277e832SShreyas NC 	int tkn_count = 0, ret;
20256277e832SShreyas NC 	int off = 0, tuple_size = 0;
20266277e832SShreyas NC 
20276277e832SShreyas NC 	if (block_size <= 0)
20286277e832SShreyas NC 		return -EINVAL;
20296277e832SShreyas NC 
20306277e832SShreyas NC 	while (tuple_size < block_size) {
20316277e832SShreyas NC 		array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off);
20326277e832SShreyas NC 
20336277e832SShreyas NC 		off += array->size;
20346277e832SShreyas NC 
20356277e832SShreyas NC 		switch (array->type) {
20366277e832SShreyas NC 		case SND_SOC_TPLG_TUPLE_TYPE_STRING:
2037ecd286a9SColin Ian King 			dev_warn(dev, "no string tokens expected for skl tplg\n");
20386277e832SShreyas NC 			continue;
20396277e832SShreyas NC 
20406277e832SShreyas NC 		case SND_SOC_TPLG_TUPLE_TYPE_UUID:
20416277e832SShreyas NC 			ret = skl_tplg_get_uuid(dev, mconfig, array->uuid);
20426277e832SShreyas NC 			if (ret < 0)
20436277e832SShreyas NC 				return ret;
20446277e832SShreyas NC 
20456277e832SShreyas NC 			tuple_size += sizeof(*array->uuid);
20466277e832SShreyas NC 
20476277e832SShreyas NC 			continue;
20486277e832SShreyas NC 
20496277e832SShreyas NC 		default:
20506277e832SShreyas NC 			tkn_elem = array->value;
20516277e832SShreyas NC 			tkn_count = 0;
20526277e832SShreyas NC 			break;
20536277e832SShreyas NC 		}
20546277e832SShreyas NC 
20556277e832SShreyas NC 		while (tkn_count <= (array->num_elems - 1)) {
20566277e832SShreyas NC 			ret = skl_tplg_get_token(dev, tkn_elem,
20576277e832SShreyas NC 					skl, mconfig);
20586277e832SShreyas NC 
20596277e832SShreyas NC 			if (ret < 0)
20606277e832SShreyas NC 				return ret;
20616277e832SShreyas NC 
20626277e832SShreyas NC 			tkn_count = tkn_count + ret;
20636277e832SShreyas NC 			tkn_elem++;
20646277e832SShreyas NC 		}
20656277e832SShreyas NC 
20666277e832SShreyas NC 		tuple_size += tkn_count * sizeof(*tkn_elem);
20676277e832SShreyas NC 	}
20686277e832SShreyas NC 
20696277e832SShreyas NC 	return 0;
20706277e832SShreyas NC }
20716277e832SShreyas NC 
20726277e832SShreyas NC /*
20736277e832SShreyas NC  * Every data block is preceded by a descriptor to read the number
20746277e832SShreyas NC  * of data blocks, they type of the block and it's size
20756277e832SShreyas NC  */
20766277e832SShreyas NC static int skl_tplg_get_desc_blocks(struct device *dev,
20776277e832SShreyas NC 		struct snd_soc_tplg_vendor_array *array)
20786277e832SShreyas NC {
20796277e832SShreyas NC 	struct snd_soc_tplg_vendor_value_elem *tkn_elem;
20806277e832SShreyas NC 
20816277e832SShreyas NC 	tkn_elem = array->value;
20826277e832SShreyas NC 
20836277e832SShreyas NC 	switch (tkn_elem->token) {
20846277e832SShreyas NC 	case SKL_TKN_U8_NUM_BLOCKS:
20856277e832SShreyas NC 	case SKL_TKN_U8_BLOCK_TYPE:
20866277e832SShreyas NC 	case SKL_TKN_U16_BLOCK_SIZE:
20876277e832SShreyas NC 		return tkn_elem->value;
20886277e832SShreyas NC 
20896277e832SShreyas NC 	default:
2090ecd286a9SColin Ian King 		dev_err(dev, "Invalid descriptor token %d\n", tkn_elem->token);
20916277e832SShreyas NC 		break;
20926277e832SShreyas NC 	}
20936277e832SShreyas NC 
20946277e832SShreyas NC 	return -EINVAL;
20956277e832SShreyas NC }
20966277e832SShreyas NC 
20976277e832SShreyas NC /*
20986277e832SShreyas NC  * Parse the private data for the token and corresponding value.
20996277e832SShreyas NC  * The private data can have multiple data blocks. So, a data block
21006277e832SShreyas NC  * is preceded by a descriptor for number of blocks and a descriptor
21016277e832SShreyas NC  * for the type and size of the suceeding data block.
21026277e832SShreyas NC  */
21036277e832SShreyas NC static int skl_tplg_get_pvt_data(struct snd_soc_tplg_dapm_widget *tplg_w,
21046277e832SShreyas NC 				struct skl *skl, struct device *dev,
21056277e832SShreyas NC 				struct skl_module_cfg *mconfig)
21066277e832SShreyas NC {
21076277e832SShreyas NC 	struct snd_soc_tplg_vendor_array *array;
21086277e832SShreyas NC 	int num_blocks, block_size = 0, block_type, off = 0;
21096277e832SShreyas NC 	char *data;
21106277e832SShreyas NC 	int ret;
21116277e832SShreyas NC 
21126277e832SShreyas NC 	/* Read the NUM_DATA_BLOCKS descriptor */
21136277e832SShreyas NC 	array = (struct snd_soc_tplg_vendor_array *)tplg_w->priv.data;
21146277e832SShreyas NC 	ret = skl_tplg_get_desc_blocks(dev, array);
21156277e832SShreyas NC 	if (ret < 0)
21166277e832SShreyas NC 		return ret;
21176277e832SShreyas NC 	num_blocks = ret;
21186277e832SShreyas NC 
21196277e832SShreyas NC 	off += array->size;
21206277e832SShreyas NC 	array = (struct snd_soc_tplg_vendor_array *)(tplg_w->priv.data + off);
21216277e832SShreyas NC 
21226277e832SShreyas NC 	/* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */
21236277e832SShreyas NC 	while (num_blocks > 0) {
21246277e832SShreyas NC 		ret = skl_tplg_get_desc_blocks(dev, array);
21256277e832SShreyas NC 
21266277e832SShreyas NC 		if (ret < 0)
21276277e832SShreyas NC 			return ret;
21286277e832SShreyas NC 		block_type = ret;
21296277e832SShreyas NC 		off += array->size;
21306277e832SShreyas NC 
21316277e832SShreyas NC 		array = (struct snd_soc_tplg_vendor_array *)
21326277e832SShreyas NC 			(tplg_w->priv.data + off);
21336277e832SShreyas NC 
21346277e832SShreyas NC 		ret = skl_tplg_get_desc_blocks(dev, array);
21356277e832SShreyas NC 
21366277e832SShreyas NC 		if (ret < 0)
21376277e832SShreyas NC 			return ret;
21386277e832SShreyas NC 		block_size = ret;
21396277e832SShreyas NC 		off += array->size;
21406277e832SShreyas NC 
21416277e832SShreyas NC 		array = (struct snd_soc_tplg_vendor_array *)
21426277e832SShreyas NC 			(tplg_w->priv.data + off);
21436277e832SShreyas NC 
21446277e832SShreyas NC 		data = (tplg_w->priv.data + off);
21456277e832SShreyas NC 
21466277e832SShreyas NC 		if (block_type == SKL_TYPE_TUPLE) {
21476277e832SShreyas NC 			ret = skl_tplg_get_tokens(dev, data,
21486277e832SShreyas NC 					skl, mconfig, block_size);
21496277e832SShreyas NC 
21506277e832SShreyas NC 			if (ret < 0)
21516277e832SShreyas NC 				return ret;
21526277e832SShreyas NC 
21536277e832SShreyas NC 			--num_blocks;
21546277e832SShreyas NC 		} else {
21556277e832SShreyas NC 			if (mconfig->formats_config.caps_size > 0)
21566277e832SShreyas NC 				memcpy(mconfig->formats_config.caps, data,
21576277e832SShreyas NC 					mconfig->formats_config.caps_size);
21586277e832SShreyas NC 			--num_blocks;
21596277e832SShreyas NC 		}
21606277e832SShreyas NC 	}
21616277e832SShreyas NC 
21626277e832SShreyas NC 	return 0;
21634cd9899fSHardik T Shah }
21644cd9899fSHardik T Shah 
2165fe3f4442SDharageswari R static void skl_clear_pin_config(struct snd_soc_platform *platform,
2166fe3f4442SDharageswari R 				struct snd_soc_dapm_widget *w)
2167fe3f4442SDharageswari R {
2168fe3f4442SDharageswari R 	int i;
2169fe3f4442SDharageswari R 	struct skl_module_cfg *mconfig;
2170fe3f4442SDharageswari R 	struct skl_pipe *pipe;
2171fe3f4442SDharageswari R 
2172fe3f4442SDharageswari R 	if (!strncmp(w->dapm->component->name, platform->component.name,
2173fe3f4442SDharageswari R 					strlen(platform->component.name))) {
2174fe3f4442SDharageswari R 		mconfig = w->priv;
2175fe3f4442SDharageswari R 		pipe = mconfig->pipe;
2176fe3f4442SDharageswari R 		for (i = 0; i < mconfig->max_in_queue; i++) {
2177fe3f4442SDharageswari R 			mconfig->m_in_pin[i].in_use = false;
2178fe3f4442SDharageswari R 			mconfig->m_in_pin[i].pin_state = SKL_PIN_UNBIND;
2179fe3f4442SDharageswari R 		}
2180fe3f4442SDharageswari R 		for (i = 0; i < mconfig->max_out_queue; i++) {
2181fe3f4442SDharageswari R 			mconfig->m_out_pin[i].in_use = false;
2182fe3f4442SDharageswari R 			mconfig->m_out_pin[i].pin_state = SKL_PIN_UNBIND;
2183fe3f4442SDharageswari R 		}
2184fe3f4442SDharageswari R 		pipe->state = SKL_PIPE_INVALID;
2185fe3f4442SDharageswari R 		mconfig->m_state = SKL_MODULE_UNINIT;
2186fe3f4442SDharageswari R 	}
2187fe3f4442SDharageswari R }
2188fe3f4442SDharageswari R 
2189fe3f4442SDharageswari R void skl_cleanup_resources(struct skl *skl)
2190fe3f4442SDharageswari R {
2191fe3f4442SDharageswari R 	struct skl_sst *ctx = skl->skl_sst;
2192fe3f4442SDharageswari R 	struct snd_soc_platform *soc_platform = skl->platform;
2193fe3f4442SDharageswari R 	struct snd_soc_dapm_widget *w;
2194fe3f4442SDharageswari R 	struct snd_soc_card *card;
2195fe3f4442SDharageswari R 
2196fe3f4442SDharageswari R 	if (soc_platform == NULL)
2197fe3f4442SDharageswari R 		return;
2198fe3f4442SDharageswari R 
2199fe3f4442SDharageswari R 	card = soc_platform->component.card;
2200fe3f4442SDharageswari R 	if (!card || !card->instantiated)
2201fe3f4442SDharageswari R 		return;
2202fe3f4442SDharageswari R 
2203fe3f4442SDharageswari R 	skl->resource.mem = 0;
2204fe3f4442SDharageswari R 	skl->resource.mcps = 0;
2205fe3f4442SDharageswari R 
2206fe3f4442SDharageswari R 	list_for_each_entry(w, &card->widgets, list) {
2207fe3f4442SDharageswari R 		if (is_skl_dsp_widget_type(w) && (w->priv != NULL))
2208fe3f4442SDharageswari R 			skl_clear_pin_config(soc_platform, w);
2209fe3f4442SDharageswari R 	}
2210fe3f4442SDharageswari R 
2211fe3f4442SDharageswari R 	skl_clear_module_cnt(ctx->dsp);
2212fe3f4442SDharageswari R }
2213fe3f4442SDharageswari R 
22143af36706SVinod Koul /*
22153af36706SVinod Koul  * Topology core widget load callback
22163af36706SVinod Koul  *
22173af36706SVinod Koul  * This is used to save the private data for each widget which gives
22183af36706SVinod Koul  * information to the driver about module and pipeline parameters which DSP
22193af36706SVinod Koul  * FW expects like ids, resource values, formats etc
22203af36706SVinod Koul  */
22213af36706SVinod Koul static int skl_tplg_widget_load(struct snd_soc_component *cmpnt,
22223af36706SVinod Koul 				struct snd_soc_dapm_widget *w,
22233af36706SVinod Koul 				struct snd_soc_tplg_dapm_widget *tplg_w)
22243af36706SVinod Koul {
22253af36706SVinod Koul 	int ret;
22263af36706SVinod Koul 	struct hdac_ext_bus *ebus = snd_soc_component_get_drvdata(cmpnt);
22273af36706SVinod Koul 	struct skl *skl = ebus_to_skl(ebus);
22283af36706SVinod Koul 	struct hdac_bus *bus = ebus_to_hbus(ebus);
22293af36706SVinod Koul 	struct skl_module_cfg *mconfig;
22303af36706SVinod Koul 
22313af36706SVinod Koul 	if (!tplg_w->priv.size)
22323af36706SVinod Koul 		goto bind_event;
22333af36706SVinod Koul 
22343af36706SVinod Koul 	mconfig = devm_kzalloc(bus->dev, sizeof(*mconfig), GFP_KERNEL);
22353af36706SVinod Koul 
22363af36706SVinod Koul 	if (!mconfig)
22373af36706SVinod Koul 		return -ENOMEM;
22383af36706SVinod Koul 
22393af36706SVinod Koul 	w->priv = mconfig;
224009305da9SShreyas NC 
2241b7c50555SVinod Koul 	/*
2242b7c50555SVinod Koul 	 * module binary can be loaded later, so set it to query when
2243b7c50555SVinod Koul 	 * module is load for a use case
2244b7c50555SVinod Koul 	 */
2245b7c50555SVinod Koul 	mconfig->id.module_id = -1;
22464cd9899fSHardik T Shah 
22476277e832SShreyas NC 	/* Parse private data for tuples */
22486277e832SShreyas NC 	ret = skl_tplg_get_pvt_data(tplg_w, skl, bus->dev, mconfig);
22496277e832SShreyas NC 	if (ret < 0)
22506277e832SShreyas NC 		return ret;
22513af36706SVinod Koul bind_event:
22523af36706SVinod Koul 	if (tplg_w->event_type == 0) {
22533373f716SVinod Koul 		dev_dbg(bus->dev, "ASoC: No event handler required\n");
22543af36706SVinod Koul 		return 0;
22553af36706SVinod Koul 	}
22563af36706SVinod Koul 
22573af36706SVinod Koul 	ret = snd_soc_tplg_widget_bind_event(w, skl_tplg_widget_ops,
2258b663a8c5SJeeja KP 					ARRAY_SIZE(skl_tplg_widget_ops),
2259b663a8c5SJeeja KP 					tplg_w->event_type);
22603af36706SVinod Koul 
22613af36706SVinod Koul 	if (ret) {
22623af36706SVinod Koul 		dev_err(bus->dev, "%s: No matching event handlers found for %d\n",
22633af36706SVinod Koul 					__func__, tplg_w->event_type);
22643af36706SVinod Koul 		return -EINVAL;
22653af36706SVinod Koul 	}
22663af36706SVinod Koul 
22673af36706SVinod Koul 	return 0;
22683af36706SVinod Koul }
22693af36706SVinod Koul 
2270140adfbaSJeeja KP static int skl_init_algo_data(struct device *dev, struct soc_bytes_ext *be,
2271140adfbaSJeeja KP 					struct snd_soc_tplg_bytes_control *bc)
2272140adfbaSJeeja KP {
2273140adfbaSJeeja KP 	struct skl_algo_data *ac;
2274140adfbaSJeeja KP 	struct skl_dfw_algo_data *dfw_ac =
2275140adfbaSJeeja KP 				(struct skl_dfw_algo_data *)bc->priv.data;
2276140adfbaSJeeja KP 
2277140adfbaSJeeja KP 	ac = devm_kzalloc(dev, sizeof(*ac), GFP_KERNEL);
2278140adfbaSJeeja KP 	if (!ac)
2279140adfbaSJeeja KP 		return -ENOMEM;
2280140adfbaSJeeja KP 
2281140adfbaSJeeja KP 	/* Fill private data */
2282140adfbaSJeeja KP 	ac->max = dfw_ac->max;
2283140adfbaSJeeja KP 	ac->param_id = dfw_ac->param_id;
2284140adfbaSJeeja KP 	ac->set_params = dfw_ac->set_params;
22850d682104SDharageswari R 	ac->size = dfw_ac->max;
2286140adfbaSJeeja KP 
2287140adfbaSJeeja KP 	if (ac->max) {
2288140adfbaSJeeja KP 		ac->params = (char *) devm_kzalloc(dev, ac->max, GFP_KERNEL);
2289140adfbaSJeeja KP 		if (!ac->params)
2290140adfbaSJeeja KP 			return -ENOMEM;
2291140adfbaSJeeja KP 
2292140adfbaSJeeja KP 		memcpy(ac->params, dfw_ac->params, ac->max);
2293140adfbaSJeeja KP 	}
2294140adfbaSJeeja KP 
2295140adfbaSJeeja KP 	be->dobj.private  = ac;
2296140adfbaSJeeja KP 	return 0;
2297140adfbaSJeeja KP }
2298140adfbaSJeeja KP 
2299140adfbaSJeeja KP static int skl_tplg_control_load(struct snd_soc_component *cmpnt,
2300140adfbaSJeeja KP 				struct snd_kcontrol_new *kctl,
2301140adfbaSJeeja KP 				struct snd_soc_tplg_ctl_hdr *hdr)
2302140adfbaSJeeja KP {
2303140adfbaSJeeja KP 	struct soc_bytes_ext *sb;
2304140adfbaSJeeja KP 	struct snd_soc_tplg_bytes_control *tplg_bc;
2305140adfbaSJeeja KP 	struct hdac_ext_bus *ebus  = snd_soc_component_get_drvdata(cmpnt);
2306140adfbaSJeeja KP 	struct hdac_bus *bus = ebus_to_hbus(ebus);
2307140adfbaSJeeja KP 
2308140adfbaSJeeja KP 	switch (hdr->ops.info) {
2309140adfbaSJeeja KP 	case SND_SOC_TPLG_CTL_BYTES:
2310140adfbaSJeeja KP 		tplg_bc = container_of(hdr,
2311140adfbaSJeeja KP 				struct snd_soc_tplg_bytes_control, hdr);
2312140adfbaSJeeja KP 		if (kctl->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2313140adfbaSJeeja KP 			sb = (struct soc_bytes_ext *)kctl->private_value;
2314140adfbaSJeeja KP 			if (tplg_bc->priv.size)
2315140adfbaSJeeja KP 				return skl_init_algo_data(
2316140adfbaSJeeja KP 						bus->dev, sb, tplg_bc);
2317140adfbaSJeeja KP 		}
2318140adfbaSJeeja KP 		break;
2319140adfbaSJeeja KP 
2320140adfbaSJeeja KP 	default:
2321140adfbaSJeeja KP 		dev_warn(bus->dev, "Control load not supported %d:%d:%d\n",
2322140adfbaSJeeja KP 			hdr->ops.get, hdr->ops.put, hdr->ops.info);
2323140adfbaSJeeja KP 		break;
2324140adfbaSJeeja KP 	}
2325140adfbaSJeeja KP 
2326140adfbaSJeeja KP 	return 0;
2327140adfbaSJeeja KP }
2328140adfbaSJeeja KP 
2329541070ceSShreyas NC static int skl_tplg_fill_str_mfest_tkn(struct device *dev,
2330541070ceSShreyas NC 		struct snd_soc_tplg_vendor_string_elem *str_elem,
2331eee0e16fSJeeja KP 		struct skl *skl)
2332541070ceSShreyas NC {
2333541070ceSShreyas NC 	int tkn_count = 0;
2334541070ceSShreyas NC 	static int ref_count;
2335541070ceSShreyas NC 
2336541070ceSShreyas NC 	switch (str_elem->token) {
2337541070ceSShreyas NC 	case SKL_TKN_STR_LIB_NAME:
2338eee0e16fSJeeja KP 		if (ref_count > skl->skl_sst->lib_count - 1) {
2339541070ceSShreyas NC 			ref_count = 0;
2340541070ceSShreyas NC 			return -EINVAL;
2341541070ceSShreyas NC 		}
2342541070ceSShreyas NC 
2343eee0e16fSJeeja KP 		strncpy(skl->skl_sst->lib_info[ref_count].name,
2344eee0e16fSJeeja KP 			str_elem->string,
2345eee0e16fSJeeja KP 			ARRAY_SIZE(skl->skl_sst->lib_info[ref_count].name));
2346541070ceSShreyas NC 		ref_count++;
2347541070ceSShreyas NC 		tkn_count++;
2348541070ceSShreyas NC 		break;
2349541070ceSShreyas NC 
2350541070ceSShreyas NC 	default:
2351ecd286a9SColin Ian King 		dev_err(dev, "Not a string token %d\n", str_elem->token);
2352541070ceSShreyas NC 		break;
2353541070ceSShreyas NC 	}
2354541070ceSShreyas NC 
2355541070ceSShreyas NC 	return tkn_count;
2356541070ceSShreyas NC }
2357541070ceSShreyas NC 
2358541070ceSShreyas NC static int skl_tplg_get_str_tkn(struct device *dev,
2359541070ceSShreyas NC 		struct snd_soc_tplg_vendor_array *array,
2360eee0e16fSJeeja KP 		struct skl *skl)
2361541070ceSShreyas NC {
2362541070ceSShreyas NC 	int tkn_count = 0, ret;
2363541070ceSShreyas NC 	struct snd_soc_tplg_vendor_string_elem *str_elem;
2364541070ceSShreyas NC 
2365541070ceSShreyas NC 	str_elem = (struct snd_soc_tplg_vendor_string_elem *)array->value;
2366541070ceSShreyas NC 	while (tkn_count < array->num_elems) {
2367eee0e16fSJeeja KP 		ret = skl_tplg_fill_str_mfest_tkn(dev, str_elem, skl);
2368541070ceSShreyas NC 		str_elem++;
2369541070ceSShreyas NC 
2370541070ceSShreyas NC 		if (ret < 0)
2371541070ceSShreyas NC 			return ret;
2372541070ceSShreyas NC 
2373541070ceSShreyas NC 		tkn_count = tkn_count + ret;
2374541070ceSShreyas NC 	}
2375541070ceSShreyas NC 
2376541070ceSShreyas NC 	return tkn_count;
2377541070ceSShreyas NC }
2378541070ceSShreyas NC 
2379541070ceSShreyas NC static int skl_tplg_get_int_tkn(struct device *dev,
2380541070ceSShreyas NC 		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2381eee0e16fSJeeja KP 		struct skl *skl)
2382541070ceSShreyas NC {
2383541070ceSShreyas NC 	int tkn_count = 0;
2384541070ceSShreyas NC 
2385541070ceSShreyas NC 	switch (tkn_elem->token) {
2386541070ceSShreyas NC 	case SKL_TKN_U32_LIB_COUNT:
2387eee0e16fSJeeja KP 		skl->skl_sst->lib_count = tkn_elem->value;
2388541070ceSShreyas NC 		tkn_count++;
2389541070ceSShreyas NC 		break;
2390541070ceSShreyas NC 
2391541070ceSShreyas NC 	default:
2392ecd286a9SColin Ian King 		dev_err(dev, "Not a manifest token %d\n", tkn_elem->token);
2393541070ceSShreyas NC 		return -EINVAL;
2394541070ceSShreyas NC 	}
2395541070ceSShreyas NC 
2396541070ceSShreyas NC 	return tkn_count;
2397541070ceSShreyas NC }
2398541070ceSShreyas NC 
2399541070ceSShreyas NC /*
2400541070ceSShreyas NC  * Fill the manifest structure by parsing the tokens based on the
2401541070ceSShreyas NC  * type.
2402541070ceSShreyas NC  */
2403541070ceSShreyas NC static int skl_tplg_get_manifest_tkn(struct device *dev,
2404eee0e16fSJeeja KP 		char *pvt_data, struct skl *skl,
2405541070ceSShreyas NC 		int block_size)
2406541070ceSShreyas NC {
2407541070ceSShreyas NC 	int tkn_count = 0, ret;
2408541070ceSShreyas NC 	int off = 0, tuple_size = 0;
2409541070ceSShreyas NC 	struct snd_soc_tplg_vendor_array *array;
2410541070ceSShreyas NC 	struct snd_soc_tplg_vendor_value_elem *tkn_elem;
2411541070ceSShreyas NC 
2412541070ceSShreyas NC 	if (block_size <= 0)
2413541070ceSShreyas NC 		return -EINVAL;
2414541070ceSShreyas NC 
2415541070ceSShreyas NC 	while (tuple_size < block_size) {
2416541070ceSShreyas NC 		array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off);
2417541070ceSShreyas NC 		off += array->size;
2418541070ceSShreyas NC 		switch (array->type) {
2419541070ceSShreyas NC 		case SND_SOC_TPLG_TUPLE_TYPE_STRING:
2420eee0e16fSJeeja KP 			ret = skl_tplg_get_str_tkn(dev, array, skl);
2421541070ceSShreyas NC 
2422541070ceSShreyas NC 			if (ret < 0)
2423541070ceSShreyas NC 				return ret;
2424541070ceSShreyas NC 			tkn_count += ret;
2425541070ceSShreyas NC 
2426541070ceSShreyas NC 			tuple_size += tkn_count *
2427541070ceSShreyas NC 				sizeof(struct snd_soc_tplg_vendor_string_elem);
2428541070ceSShreyas NC 			continue;
2429541070ceSShreyas NC 
2430541070ceSShreyas NC 		case SND_SOC_TPLG_TUPLE_TYPE_UUID:
2431ecd286a9SColin Ian King 			dev_warn(dev, "no uuid tokens for skl tplf manifest\n");
2432541070ceSShreyas NC 			continue;
2433541070ceSShreyas NC 
2434541070ceSShreyas NC 		default:
2435541070ceSShreyas NC 			tkn_elem = array->value;
2436541070ceSShreyas NC 			tkn_count = 0;
2437541070ceSShreyas NC 			break;
2438541070ceSShreyas NC 		}
2439541070ceSShreyas NC 
2440541070ceSShreyas NC 		while (tkn_count <= array->num_elems - 1) {
2441541070ceSShreyas NC 			ret = skl_tplg_get_int_tkn(dev,
2442eee0e16fSJeeja KP 					tkn_elem, skl);
2443541070ceSShreyas NC 			if (ret < 0)
2444541070ceSShreyas NC 				return ret;
2445541070ceSShreyas NC 
2446541070ceSShreyas NC 			tkn_count = tkn_count + ret;
2447541070ceSShreyas NC 			tkn_elem++;
2448541070ceSShreyas NC 			tuple_size += tkn_count *
2449541070ceSShreyas NC 				sizeof(struct snd_soc_tplg_vendor_value_elem);
2450541070ceSShreyas NC 			break;
2451541070ceSShreyas NC 		}
2452541070ceSShreyas NC 		tkn_count = 0;
2453541070ceSShreyas NC 	}
2454541070ceSShreyas NC 
2455541070ceSShreyas NC 	return 0;
2456541070ceSShreyas NC }
2457541070ceSShreyas NC 
2458541070ceSShreyas NC /*
2459541070ceSShreyas NC  * Parse manifest private data for tokens. The private data block is
2460541070ceSShreyas NC  * preceded by descriptors for type and size of data block.
2461541070ceSShreyas NC  */
2462541070ceSShreyas NC static int skl_tplg_get_manifest_data(struct snd_soc_tplg_manifest *manifest,
2463eee0e16fSJeeja KP 			struct device *dev, struct skl *skl)
2464541070ceSShreyas NC {
2465541070ceSShreyas NC 	struct snd_soc_tplg_vendor_array *array;
2466541070ceSShreyas NC 	int num_blocks, block_size = 0, block_type, off = 0;
2467541070ceSShreyas NC 	char *data;
2468541070ceSShreyas NC 	int ret;
2469541070ceSShreyas NC 
2470541070ceSShreyas NC 	/* Read the NUM_DATA_BLOCKS descriptor */
2471541070ceSShreyas NC 	array = (struct snd_soc_tplg_vendor_array *)manifest->priv.data;
2472541070ceSShreyas NC 	ret = skl_tplg_get_desc_blocks(dev, array);
2473541070ceSShreyas NC 	if (ret < 0)
2474541070ceSShreyas NC 		return ret;
2475541070ceSShreyas NC 	num_blocks = ret;
2476541070ceSShreyas NC 
2477541070ceSShreyas NC 	off += array->size;
2478541070ceSShreyas NC 	array = (struct snd_soc_tplg_vendor_array *)
2479541070ceSShreyas NC 			(manifest->priv.data + off);
2480541070ceSShreyas NC 
2481541070ceSShreyas NC 	/* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */
2482541070ceSShreyas NC 	while (num_blocks > 0) {
2483541070ceSShreyas NC 		ret = skl_tplg_get_desc_blocks(dev, array);
2484541070ceSShreyas NC 
2485541070ceSShreyas NC 		if (ret < 0)
2486541070ceSShreyas NC 			return ret;
2487541070ceSShreyas NC 		block_type = ret;
2488541070ceSShreyas NC 		off += array->size;
2489541070ceSShreyas NC 
2490541070ceSShreyas NC 		array = (struct snd_soc_tplg_vendor_array *)
2491541070ceSShreyas NC 			(manifest->priv.data + off);
2492541070ceSShreyas NC 
2493541070ceSShreyas NC 		ret = skl_tplg_get_desc_blocks(dev, array);
2494541070ceSShreyas NC 
2495541070ceSShreyas NC 		if (ret < 0)
2496541070ceSShreyas NC 			return ret;
2497541070ceSShreyas NC 		block_size = ret;
2498541070ceSShreyas NC 		off += array->size;
2499541070ceSShreyas NC 
2500541070ceSShreyas NC 		array = (struct snd_soc_tplg_vendor_array *)
2501541070ceSShreyas NC 			(manifest->priv.data + off);
2502541070ceSShreyas NC 
2503541070ceSShreyas NC 		data = (manifest->priv.data + off);
2504541070ceSShreyas NC 
2505541070ceSShreyas NC 		if (block_type == SKL_TYPE_TUPLE) {
2506eee0e16fSJeeja KP 			ret = skl_tplg_get_manifest_tkn(dev, data, skl,
2507541070ceSShreyas NC 					block_size);
2508541070ceSShreyas NC 
2509541070ceSShreyas NC 			if (ret < 0)
2510541070ceSShreyas NC 				return ret;
2511541070ceSShreyas NC 
2512541070ceSShreyas NC 			--num_blocks;
2513541070ceSShreyas NC 		} else {
2514541070ceSShreyas NC 			return -EINVAL;
2515541070ceSShreyas NC 		}
2516541070ceSShreyas NC 	}
2517541070ceSShreyas NC 
2518541070ceSShreyas NC 	return 0;
2519541070ceSShreyas NC }
2520541070ceSShreyas NC 
252115ecaba9SKranthi G static int skl_manifest_load(struct snd_soc_component *cmpnt,
252215ecaba9SKranthi G 				struct snd_soc_tplg_manifest *manifest)
252315ecaba9SKranthi G {
252415ecaba9SKranthi G 	struct hdac_ext_bus *ebus = snd_soc_component_get_drvdata(cmpnt);
252515ecaba9SKranthi G 	struct hdac_bus *bus = ebus_to_hbus(ebus);
252615ecaba9SKranthi G 	struct skl *skl = ebus_to_skl(ebus);
252715ecaba9SKranthi G 
2528c15ad605SVinod Koul 	/* proceed only if we have private data defined */
2529c15ad605SVinod Koul 	if (manifest->priv.size == 0)
2530c15ad605SVinod Koul 		return 0;
2531c15ad605SVinod Koul 
2532eee0e16fSJeeja KP 	skl_tplg_get_manifest_data(manifest, bus->dev, skl);
2533541070ceSShreyas NC 
2534eee0e16fSJeeja KP 	if (skl->skl_sst->lib_count > SKL_MAX_LIB) {
253515ecaba9SKranthi G 		dev_err(bus->dev, "Exceeding max Library count. Got:%d\n",
2536eee0e16fSJeeja KP 					skl->skl_sst->lib_count);
2537eee0e16fSJeeja KP 		return  -EINVAL;
253815ecaba9SKranthi G 	}
253915ecaba9SKranthi G 
2540eee0e16fSJeeja KP 	return 0;
254115ecaba9SKranthi G }
254215ecaba9SKranthi G 
25433af36706SVinod Koul static struct snd_soc_tplg_ops skl_tplg_ops  = {
25443af36706SVinod Koul 	.widget_load = skl_tplg_widget_load,
2545140adfbaSJeeja KP 	.control_load = skl_tplg_control_load,
2546140adfbaSJeeja KP 	.bytes_ext_ops = skl_tlv_ops,
2547140adfbaSJeeja KP 	.bytes_ext_ops_count = ARRAY_SIZE(skl_tlv_ops),
254815ecaba9SKranthi G 	.manifest = skl_manifest_load,
25493af36706SVinod Koul };
25503af36706SVinod Koul 
2551287af4f9SJeeja KP /*
2552287af4f9SJeeja KP  * A pipe can have multiple modules, each of them will be a DAPM widget as
2553287af4f9SJeeja KP  * well. While managing a pipeline we need to get the list of all the
2554287af4f9SJeeja KP  * widgets in a pipelines, so this helper - skl_tplg_create_pipe_widget_list()
2555287af4f9SJeeja KP  * helps to get the SKL type widgets in that pipeline
2556287af4f9SJeeja KP  */
2557287af4f9SJeeja KP static int skl_tplg_create_pipe_widget_list(struct snd_soc_platform *platform)
2558287af4f9SJeeja KP {
2559287af4f9SJeeja KP 	struct snd_soc_dapm_widget *w;
2560287af4f9SJeeja KP 	struct skl_module_cfg *mcfg = NULL;
2561287af4f9SJeeja KP 	struct skl_pipe_module *p_module = NULL;
2562287af4f9SJeeja KP 	struct skl_pipe *pipe;
2563287af4f9SJeeja KP 
2564287af4f9SJeeja KP 	list_for_each_entry(w, &platform->component.card->widgets, list) {
2565287af4f9SJeeja KP 		if (is_skl_dsp_widget_type(w) && w->priv != NULL) {
2566287af4f9SJeeja KP 			mcfg = w->priv;
2567287af4f9SJeeja KP 			pipe = mcfg->pipe;
2568287af4f9SJeeja KP 
2569287af4f9SJeeja KP 			p_module = devm_kzalloc(platform->dev,
2570287af4f9SJeeja KP 						sizeof(*p_module), GFP_KERNEL);
2571287af4f9SJeeja KP 			if (!p_module)
2572287af4f9SJeeja KP 				return -ENOMEM;
2573287af4f9SJeeja KP 
2574287af4f9SJeeja KP 			p_module->w = w;
2575287af4f9SJeeja KP 			list_add_tail(&p_module->node, &pipe->w_list);
2576287af4f9SJeeja KP 		}
2577287af4f9SJeeja KP 	}
2578287af4f9SJeeja KP 
2579287af4f9SJeeja KP 	return 0;
2580287af4f9SJeeja KP }
2581287af4f9SJeeja KP 
2582f0aa94faSJeeja KP static void skl_tplg_set_pipe_type(struct skl *skl, struct skl_pipe *pipe)
2583f0aa94faSJeeja KP {
2584f0aa94faSJeeja KP 	struct skl_pipe_module *w_module;
2585f0aa94faSJeeja KP 	struct snd_soc_dapm_widget *w;
2586f0aa94faSJeeja KP 	struct skl_module_cfg *mconfig;
2587f0aa94faSJeeja KP 	bool host_found = false, link_found = false;
2588f0aa94faSJeeja KP 
2589f0aa94faSJeeja KP 	list_for_each_entry(w_module, &pipe->w_list, node) {
2590f0aa94faSJeeja KP 		w = w_module->w;
2591f0aa94faSJeeja KP 		mconfig = w->priv;
2592f0aa94faSJeeja KP 
2593f0aa94faSJeeja KP 		if (mconfig->dev_type == SKL_DEVICE_HDAHOST)
2594f0aa94faSJeeja KP 			host_found = true;
2595f0aa94faSJeeja KP 		else if (mconfig->dev_type != SKL_DEVICE_NONE)
2596f0aa94faSJeeja KP 			link_found = true;
2597f0aa94faSJeeja KP 	}
2598f0aa94faSJeeja KP 
2599f0aa94faSJeeja KP 	if (host_found && link_found)
2600f0aa94faSJeeja KP 		pipe->passthru = true;
2601f0aa94faSJeeja KP 	else
2602f0aa94faSJeeja KP 		pipe->passthru = false;
2603f0aa94faSJeeja KP }
2604f0aa94faSJeeja KP 
26053af36706SVinod Koul /* This will be read from topology manifest, currently defined here */
26063af36706SVinod Koul #define SKL_MAX_MCPS 30000000
26073af36706SVinod Koul #define SKL_FW_MAX_MEM 1000000
26083af36706SVinod Koul 
26093af36706SVinod Koul /*
26103af36706SVinod Koul  * SKL topology init routine
26113af36706SVinod Koul  */
26123af36706SVinod Koul int skl_tplg_init(struct snd_soc_platform *platform, struct hdac_ext_bus *ebus)
26133af36706SVinod Koul {
26143af36706SVinod Koul 	int ret;
26153af36706SVinod Koul 	const struct firmware *fw;
26163af36706SVinod Koul 	struct hdac_bus *bus = ebus_to_hbus(ebus);
26173af36706SVinod Koul 	struct skl *skl = ebus_to_skl(ebus);
2618f0aa94faSJeeja KP 	struct skl_pipeline *ppl;
26193af36706SVinod Koul 
26204b235c43SVinod Koul 	ret = request_firmware(&fw, skl->tplg_name, bus->dev);
26213af36706SVinod Koul 	if (ret < 0) {
2622b663a8c5SJeeja KP 		dev_err(bus->dev, "tplg fw %s load failed with %d\n",
26234b235c43SVinod Koul 				skl->tplg_name, ret);
26244b235c43SVinod Koul 		ret = request_firmware(&fw, "dfw_sst.bin", bus->dev);
26254b235c43SVinod Koul 		if (ret < 0) {
26264b235c43SVinod Koul 			dev_err(bus->dev, "Fallback tplg fw %s load failed with %d\n",
26273af36706SVinod Koul 					"dfw_sst.bin", ret);
26283af36706SVinod Koul 			return ret;
26293af36706SVinod Koul 		}
26304b235c43SVinod Koul 	}
26313af36706SVinod Koul 
26323af36706SVinod Koul 	/*
26333af36706SVinod Koul 	 * The complete tplg for SKL is loaded as index 0, we don't use
26343af36706SVinod Koul 	 * any other index
26353af36706SVinod Koul 	 */
2636b663a8c5SJeeja KP 	ret = snd_soc_tplg_component_load(&platform->component,
2637b663a8c5SJeeja KP 					&skl_tplg_ops, fw, 0);
26383af36706SVinod Koul 	if (ret < 0) {
26393af36706SVinod Koul 		dev_err(bus->dev, "tplg component load failed%d\n", ret);
2640c14a82c7SSudip Mukherjee 		release_firmware(fw);
26413af36706SVinod Koul 		return -EINVAL;
26423af36706SVinod Koul 	}
26433af36706SVinod Koul 
26443af36706SVinod Koul 	skl->resource.max_mcps = SKL_MAX_MCPS;
26453af36706SVinod Koul 	skl->resource.max_mem = SKL_FW_MAX_MEM;
26463af36706SVinod Koul 
2647d8018361SVinod Koul 	skl->tplg = fw;
2648287af4f9SJeeja KP 	ret = skl_tplg_create_pipe_widget_list(platform);
2649287af4f9SJeeja KP 	if (ret < 0)
2650287af4f9SJeeja KP 		return ret;
2651d8018361SVinod Koul 
2652f0aa94faSJeeja KP 	list_for_each_entry(ppl, &skl->ppl_list, node)
2653f0aa94faSJeeja KP 		skl_tplg_set_pipe_type(skl, ppl->pipe);
26543af36706SVinod Koul 
26553af36706SVinod Koul 	return 0;
2656e4e2d2f4SJeeja KP }
2657