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 
397a1b749bSDharageswari R static const int mic_mono_list[] = {
407a1b749bSDharageswari R 0, 1, 2, 3,
417a1b749bSDharageswari R };
427a1b749bSDharageswari R static const int mic_stereo_list[][SKL_CH_STEREO] = {
437a1b749bSDharageswari R {0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 3}, {2, 3},
447a1b749bSDharageswari R };
457a1b749bSDharageswari R static const int mic_trio_list[][SKL_CH_TRIO] = {
467a1b749bSDharageswari R {0, 1, 2}, {0, 1, 3}, {0, 2, 3}, {1, 2, 3},
477a1b749bSDharageswari R };
487a1b749bSDharageswari R static const int mic_quatro_list[][SKL_CH_QUATRO] = {
497a1b749bSDharageswari R {0, 1, 2, 3},
507a1b749bSDharageswari R };
517a1b749bSDharageswari R 
52a83e3b4cSVinod Koul void skl_tplg_d0i3_get(struct skl *skl, enum d0i3_capability caps)
53a83e3b4cSVinod Koul {
54a83e3b4cSVinod Koul 	struct skl_d0i3_data *d0i3 =  &skl->skl_sst->d0i3;
55a83e3b4cSVinod Koul 
56a83e3b4cSVinod Koul 	switch (caps) {
57a83e3b4cSVinod Koul 	case SKL_D0I3_NONE:
58a83e3b4cSVinod Koul 		d0i3->non_d0i3++;
59a83e3b4cSVinod Koul 		break;
60a83e3b4cSVinod Koul 
61a83e3b4cSVinod Koul 	case SKL_D0I3_STREAMING:
62a83e3b4cSVinod Koul 		d0i3->streaming++;
63a83e3b4cSVinod Koul 		break;
64a83e3b4cSVinod Koul 
65a83e3b4cSVinod Koul 	case SKL_D0I3_NON_STREAMING:
66a83e3b4cSVinod Koul 		d0i3->non_streaming++;
67a83e3b4cSVinod Koul 		break;
68a83e3b4cSVinod Koul 	}
69a83e3b4cSVinod Koul }
70a83e3b4cSVinod Koul 
71a83e3b4cSVinod Koul void skl_tplg_d0i3_put(struct skl *skl, enum d0i3_capability caps)
72a83e3b4cSVinod Koul {
73a83e3b4cSVinod Koul 	struct skl_d0i3_data *d0i3 =  &skl->skl_sst->d0i3;
74a83e3b4cSVinod Koul 
75a83e3b4cSVinod Koul 	switch (caps) {
76a83e3b4cSVinod Koul 	case SKL_D0I3_NONE:
77a83e3b4cSVinod Koul 		d0i3->non_d0i3--;
78a83e3b4cSVinod Koul 		break;
79a83e3b4cSVinod Koul 
80a83e3b4cSVinod Koul 	case SKL_D0I3_STREAMING:
81a83e3b4cSVinod Koul 		d0i3->streaming--;
82a83e3b4cSVinod Koul 		break;
83a83e3b4cSVinod Koul 
84a83e3b4cSVinod Koul 	case SKL_D0I3_NON_STREAMING:
85a83e3b4cSVinod Koul 		d0i3->non_streaming--;
86a83e3b4cSVinod Koul 		break;
87a83e3b4cSVinod Koul 	}
88a83e3b4cSVinod Koul }
89a83e3b4cSVinod Koul 
90e4e2d2f4SJeeja KP /*
91e4e2d2f4SJeeja KP  * SKL DSP driver modelling uses only few DAPM widgets so for rest we will
92e4e2d2f4SJeeja KP  * ignore. This helpers checks if the SKL driver handles this widget type
93e4e2d2f4SJeeja KP  */
94e4e2d2f4SJeeja KP static int is_skl_dsp_widget_type(struct snd_soc_dapm_widget *w)
95e4e2d2f4SJeeja KP {
96e4e2d2f4SJeeja KP 	switch (w->id) {
97e4e2d2f4SJeeja KP 	case snd_soc_dapm_dai_link:
98e4e2d2f4SJeeja KP 	case snd_soc_dapm_dai_in:
99e4e2d2f4SJeeja KP 	case snd_soc_dapm_aif_in:
100e4e2d2f4SJeeja KP 	case snd_soc_dapm_aif_out:
101e4e2d2f4SJeeja KP 	case snd_soc_dapm_dai_out:
102e4e2d2f4SJeeja KP 	case snd_soc_dapm_switch:
103e4e2d2f4SJeeja KP 		return false;
104e4e2d2f4SJeeja KP 	default:
105e4e2d2f4SJeeja KP 		return true;
106e4e2d2f4SJeeja KP 	}
107e4e2d2f4SJeeja KP }
108e4e2d2f4SJeeja KP 
109e4e2d2f4SJeeja KP /*
110e4e2d2f4SJeeja KP  * Each pipelines needs memory to be allocated. Check if we have free memory
1119ba8ffefSDharageswari.R  * from available pool.
112e4e2d2f4SJeeja KP  */
1139ba8ffefSDharageswari.R static bool skl_is_pipe_mem_avail(struct skl *skl,
114e4e2d2f4SJeeja KP 				struct skl_module_cfg *mconfig)
115e4e2d2f4SJeeja KP {
116e4e2d2f4SJeeja KP 	struct skl_sst *ctx = skl->skl_sst;
117e4e2d2f4SJeeja KP 
118e4e2d2f4SJeeja KP 	if (skl->resource.mem + mconfig->pipe->memory_pages >
119e4e2d2f4SJeeja KP 				skl->resource.max_mem) {
120e4e2d2f4SJeeja KP 		dev_err(ctx->dev,
121e4e2d2f4SJeeja KP 				"%s: module_id %d instance %d\n", __func__,
122e4e2d2f4SJeeja KP 				mconfig->id.module_id,
123e4e2d2f4SJeeja KP 				mconfig->id.instance_id);
124e4e2d2f4SJeeja KP 		dev_err(ctx->dev,
125e4e2d2f4SJeeja KP 				"exceeds ppl memory available %d mem %d\n",
126e4e2d2f4SJeeja KP 				skl->resource.max_mem, skl->resource.mem);
127e4e2d2f4SJeeja KP 		return false;
1289ba8ffefSDharageswari.R 	} else {
1299ba8ffefSDharageswari.R 		return true;
1309ba8ffefSDharageswari.R 	}
131e4e2d2f4SJeeja KP }
132e4e2d2f4SJeeja KP 
1339ba8ffefSDharageswari.R /*
1349ba8ffefSDharageswari.R  * Add the mem to the mem pool. This is freed when pipe is deleted.
1359ba8ffefSDharageswari.R  * Note: DSP does actual memory management we only keep track for complete
1369ba8ffefSDharageswari.R  * pool
1379ba8ffefSDharageswari.R  */
1389ba8ffefSDharageswari.R static void skl_tplg_alloc_pipe_mem(struct skl *skl,
1399ba8ffefSDharageswari.R 				struct skl_module_cfg *mconfig)
1409ba8ffefSDharageswari.R {
141e4e2d2f4SJeeja KP 	skl->resource.mem += mconfig->pipe->memory_pages;
142e4e2d2f4SJeeja KP }
143e4e2d2f4SJeeja KP 
144e4e2d2f4SJeeja KP /*
145e4e2d2f4SJeeja KP  * Pipeline needs needs DSP CPU resources for computation, this is
146e4e2d2f4SJeeja KP  * quantified in MCPS (Million Clocks Per Second) required for module/pipe
147e4e2d2f4SJeeja KP  *
148e4e2d2f4SJeeja KP  * Each pipelines needs mcps to be allocated. Check if we have mcps for this
1499ba8ffefSDharageswari.R  * pipe.
150e4e2d2f4SJeeja KP  */
1519ba8ffefSDharageswari.R 
1529ba8ffefSDharageswari.R static bool skl_is_pipe_mcps_avail(struct skl *skl,
153e4e2d2f4SJeeja KP 				struct skl_module_cfg *mconfig)
154e4e2d2f4SJeeja KP {
155e4e2d2f4SJeeja KP 	struct skl_sst *ctx = skl->skl_sst;
156e4e2d2f4SJeeja KP 
157e4e2d2f4SJeeja KP 	if (skl->resource.mcps + mconfig->mcps > skl->resource.max_mcps) {
158e4e2d2f4SJeeja KP 		dev_err(ctx->dev,
159e4e2d2f4SJeeja KP 			"%s: module_id %d instance %d\n", __func__,
160e4e2d2f4SJeeja KP 			mconfig->id.module_id, mconfig->id.instance_id);
161e4e2d2f4SJeeja KP 		dev_err(ctx->dev,
1627ca42f5aSGuneshwor Singh 			"exceeds ppl mcps available %d > mem %d\n",
163e4e2d2f4SJeeja KP 			skl->resource.max_mcps, skl->resource.mcps);
164e4e2d2f4SJeeja KP 		return false;
1659ba8ffefSDharageswari.R 	} else {
1669ba8ffefSDharageswari.R 		return true;
1679ba8ffefSDharageswari.R 	}
168e4e2d2f4SJeeja KP }
169e4e2d2f4SJeeja KP 
1709ba8ffefSDharageswari.R static void skl_tplg_alloc_pipe_mcps(struct skl *skl,
1719ba8ffefSDharageswari.R 				struct skl_module_cfg *mconfig)
1729ba8ffefSDharageswari.R {
173e4e2d2f4SJeeja KP 	skl->resource.mcps += mconfig->mcps;
174e4e2d2f4SJeeja KP }
175e4e2d2f4SJeeja KP 
176e4e2d2f4SJeeja KP /*
177e4e2d2f4SJeeja KP  * Free the mcps when tearing down
178e4e2d2f4SJeeja KP  */
179e4e2d2f4SJeeja KP static void
180e4e2d2f4SJeeja KP skl_tplg_free_pipe_mcps(struct skl *skl, struct skl_module_cfg *mconfig)
181e4e2d2f4SJeeja KP {
182e4e2d2f4SJeeja KP 	skl->resource.mcps -= mconfig->mcps;
183e4e2d2f4SJeeja KP }
184e4e2d2f4SJeeja KP 
185e4e2d2f4SJeeja KP /*
186e4e2d2f4SJeeja KP  * Free the memory when tearing down
187e4e2d2f4SJeeja KP  */
188e4e2d2f4SJeeja KP static void
189e4e2d2f4SJeeja KP skl_tplg_free_pipe_mem(struct skl *skl, struct skl_module_cfg *mconfig)
190e4e2d2f4SJeeja KP {
191e4e2d2f4SJeeja KP 	skl->resource.mem -= mconfig->pipe->memory_pages;
192e4e2d2f4SJeeja KP }
193e4e2d2f4SJeeja KP 
194f7590d4fSJeeja KP 
195f7590d4fSJeeja KP static void skl_dump_mconfig(struct skl_sst *ctx,
196f7590d4fSJeeja KP 					struct skl_module_cfg *mcfg)
197f7590d4fSJeeja KP {
198f7590d4fSJeeja KP 	dev_dbg(ctx->dev, "Dumping config\n");
199f7590d4fSJeeja KP 	dev_dbg(ctx->dev, "Input Format:\n");
2004cd9899fSHardik T Shah 	dev_dbg(ctx->dev, "channels = %d\n", mcfg->in_fmt[0].channels);
2014cd9899fSHardik T Shah 	dev_dbg(ctx->dev, "s_freq = %d\n", mcfg->in_fmt[0].s_freq);
2024cd9899fSHardik T Shah 	dev_dbg(ctx->dev, "ch_cfg = %d\n", mcfg->in_fmt[0].ch_cfg);
2034cd9899fSHardik T Shah 	dev_dbg(ctx->dev, "valid bit depth = %d\n", mcfg->in_fmt[0].valid_bit_depth);
204f7590d4fSJeeja KP 	dev_dbg(ctx->dev, "Output Format:\n");
2054cd9899fSHardik T Shah 	dev_dbg(ctx->dev, "channels = %d\n", mcfg->out_fmt[0].channels);
2064cd9899fSHardik T Shah 	dev_dbg(ctx->dev, "s_freq = %d\n", mcfg->out_fmt[0].s_freq);
2074cd9899fSHardik T Shah 	dev_dbg(ctx->dev, "valid bit depth = %d\n", mcfg->out_fmt[0].valid_bit_depth);
2084cd9899fSHardik T Shah 	dev_dbg(ctx->dev, "ch_cfg = %d\n", mcfg->out_fmt[0].ch_cfg);
209f7590d4fSJeeja KP }
210f7590d4fSJeeja KP 
211ea5a137dSSubhransu S. Prusty static void skl_tplg_update_chmap(struct skl_module_fmt *fmt, int chs)
212ea5a137dSSubhransu S. Prusty {
213ea5a137dSSubhransu S. Prusty 	int slot_map = 0xFFFFFFFF;
214ea5a137dSSubhransu S. Prusty 	int start_slot = 0;
215ea5a137dSSubhransu S. Prusty 	int i;
216ea5a137dSSubhransu S. Prusty 
217ea5a137dSSubhransu S. Prusty 	for (i = 0; i < chs; i++) {
218ea5a137dSSubhransu S. Prusty 		/*
219ea5a137dSSubhransu S. Prusty 		 * For 2 channels with starting slot as 0, slot map will
220ea5a137dSSubhransu S. Prusty 		 * look like 0xFFFFFF10.
221ea5a137dSSubhransu S. Prusty 		 */
222ea5a137dSSubhransu S. Prusty 		slot_map &= (~(0xF << (4 * i)) | (start_slot << (4 * i)));
223ea5a137dSSubhransu S. Prusty 		start_slot++;
224ea5a137dSSubhransu S. Prusty 	}
225ea5a137dSSubhransu S. Prusty 	fmt->ch_map = slot_map;
226ea5a137dSSubhransu S. Prusty }
227ea5a137dSSubhransu S. Prusty 
228f7590d4fSJeeja KP static void skl_tplg_update_params(struct skl_module_fmt *fmt,
229f7590d4fSJeeja KP 			struct skl_pipe_params *params, int fixup)
230f7590d4fSJeeja KP {
231f7590d4fSJeeja KP 	if (fixup & SKL_RATE_FIXUP_MASK)
232f7590d4fSJeeja KP 		fmt->s_freq = params->s_freq;
233ea5a137dSSubhransu S. Prusty 	if (fixup & SKL_CH_FIXUP_MASK) {
234f7590d4fSJeeja KP 		fmt->channels = params->ch;
235ea5a137dSSubhransu S. Prusty 		skl_tplg_update_chmap(fmt, fmt->channels);
236ea5a137dSSubhransu S. Prusty 	}
23798256f83SJeeja KP 	if (fixup & SKL_FMT_FIXUP_MASK) {
23898256f83SJeeja KP 		fmt->valid_bit_depth = skl_get_bit_depth(params->s_fmt);
23998256f83SJeeja KP 
24098256f83SJeeja KP 		/*
24198256f83SJeeja KP 		 * 16 bit is 16 bit container whereas 24 bit is in 32 bit
24298256f83SJeeja KP 		 * container so update bit depth accordingly
24398256f83SJeeja KP 		 */
24498256f83SJeeja KP 		switch (fmt->valid_bit_depth) {
24598256f83SJeeja KP 		case SKL_DEPTH_16BIT:
24698256f83SJeeja KP 			fmt->bit_depth = fmt->valid_bit_depth;
24798256f83SJeeja KP 			break;
24898256f83SJeeja KP 
24998256f83SJeeja KP 		default:
25098256f83SJeeja KP 			fmt->bit_depth = SKL_DEPTH_32BIT;
25198256f83SJeeja KP 			break;
25298256f83SJeeja KP 		}
25398256f83SJeeja KP 	}
25498256f83SJeeja KP 
255f7590d4fSJeeja KP }
256f7590d4fSJeeja KP 
257f7590d4fSJeeja KP /*
258f7590d4fSJeeja KP  * A pipeline may have modules which impact the pcm parameters, like SRC,
259f7590d4fSJeeja KP  * channel converter, format converter.
260f7590d4fSJeeja KP  * We need to calculate the output params by applying the 'fixup'
261f7590d4fSJeeja KP  * Topology will tell driver which type of fixup is to be applied by
262f7590d4fSJeeja KP  * supplying the fixup mask, so based on that we calculate the output
263f7590d4fSJeeja KP  *
264f7590d4fSJeeja KP  * Now In FE the pcm hw_params is source/target format. Same is applicable
265f7590d4fSJeeja KP  * for BE with its hw_params invoked.
266f7590d4fSJeeja KP  * here based on FE, BE pipeline and direction we calculate the input and
267f7590d4fSJeeja KP  * outfix and then apply that for a module
268f7590d4fSJeeja KP  */
269f7590d4fSJeeja KP static void skl_tplg_update_params_fixup(struct skl_module_cfg *m_cfg,
270f7590d4fSJeeja KP 		struct skl_pipe_params *params, bool is_fe)
271f7590d4fSJeeja KP {
272f7590d4fSJeeja KP 	int in_fixup, out_fixup;
273f7590d4fSJeeja KP 	struct skl_module_fmt *in_fmt, *out_fmt;
274f7590d4fSJeeja KP 
2754cd9899fSHardik T Shah 	/* Fixups will be applied to pin 0 only */
2764cd9899fSHardik T Shah 	in_fmt = &m_cfg->in_fmt[0];
2774cd9899fSHardik T Shah 	out_fmt = &m_cfg->out_fmt[0];
278f7590d4fSJeeja KP 
279f7590d4fSJeeja KP 	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
280f7590d4fSJeeja KP 		if (is_fe) {
281f7590d4fSJeeja KP 			in_fixup = m_cfg->params_fixup;
282f7590d4fSJeeja KP 			out_fixup = (~m_cfg->converter) &
283f7590d4fSJeeja KP 					m_cfg->params_fixup;
284f7590d4fSJeeja KP 		} else {
285f7590d4fSJeeja KP 			out_fixup = m_cfg->params_fixup;
286f7590d4fSJeeja KP 			in_fixup = (~m_cfg->converter) &
287f7590d4fSJeeja KP 					m_cfg->params_fixup;
288f7590d4fSJeeja KP 		}
289f7590d4fSJeeja KP 	} else {
290f7590d4fSJeeja KP 		if (is_fe) {
291f7590d4fSJeeja KP 			out_fixup = m_cfg->params_fixup;
292f7590d4fSJeeja KP 			in_fixup = (~m_cfg->converter) &
293f7590d4fSJeeja KP 					m_cfg->params_fixup;
294f7590d4fSJeeja KP 		} else {
295f7590d4fSJeeja KP 			in_fixup = m_cfg->params_fixup;
296f7590d4fSJeeja KP 			out_fixup = (~m_cfg->converter) &
297f7590d4fSJeeja KP 					m_cfg->params_fixup;
298f7590d4fSJeeja KP 		}
299f7590d4fSJeeja KP 	}
300f7590d4fSJeeja KP 
301f7590d4fSJeeja KP 	skl_tplg_update_params(in_fmt, params, in_fixup);
302f7590d4fSJeeja KP 	skl_tplg_update_params(out_fmt, params, out_fixup);
303f7590d4fSJeeja KP }
304f7590d4fSJeeja KP 
305f7590d4fSJeeja KP /*
306f7590d4fSJeeja KP  * A module needs input and output buffers, which are dependent upon pcm
307f7590d4fSJeeja KP  * params, so once we have calculate params, we need buffer calculation as
308f7590d4fSJeeja KP  * well.
309f7590d4fSJeeja KP  */
310f7590d4fSJeeja KP static void skl_tplg_update_buffer_size(struct skl_sst *ctx,
311f7590d4fSJeeja KP 				struct skl_module_cfg *mcfg)
312f7590d4fSJeeja KP {
313f7590d4fSJeeja KP 	int multiplier = 1;
3144cd9899fSHardik T Shah 	struct skl_module_fmt *in_fmt, *out_fmt;
3154cd9899fSHardik T Shah 
3164cd9899fSHardik T Shah 	/* Since fixups is applied to pin 0 only, ibs, obs needs
3174cd9899fSHardik T Shah 	 * change for pin 0 only
3184cd9899fSHardik T Shah 	 */
3194cd9899fSHardik T Shah 	in_fmt = &mcfg->in_fmt[0];
3204cd9899fSHardik T Shah 	out_fmt = &mcfg->out_fmt[0];
321f7590d4fSJeeja KP 
322f7590d4fSJeeja KP 	if (mcfg->m_type == SKL_MODULE_TYPE_SRCINT)
323f7590d4fSJeeja KP 		multiplier = 5;
324f0c8e1d9SSubhransu S. Prusty 
3258e15e762STakashi Sakamoto 	mcfg->ibs = DIV_ROUND_UP(in_fmt->s_freq, 1000) *
326998d6fb5STakashi Sakamoto 			in_fmt->channels * (in_fmt->bit_depth >> 3) *
327f7590d4fSJeeja KP 			multiplier;
328f7590d4fSJeeja KP 
329998d6fb5STakashi Sakamoto 	mcfg->obs = DIV_ROUND_UP(out_fmt->s_freq, 1000) *
330998d6fb5STakashi Sakamoto 			out_fmt->channels * (out_fmt->bit_depth >> 3) *
331f7590d4fSJeeja KP 			multiplier;
332f7590d4fSJeeja KP }
333f7590d4fSJeeja KP 
334db2f586bSSenthilnathan Veppur static u8 skl_tplg_be_dev_type(int dev_type)
335db2f586bSSenthilnathan Veppur {
336db2f586bSSenthilnathan Veppur 	int ret;
337db2f586bSSenthilnathan Veppur 
338db2f586bSSenthilnathan Veppur 	switch (dev_type) {
339db2f586bSSenthilnathan Veppur 	case SKL_DEVICE_BT:
340db2f586bSSenthilnathan Veppur 		ret = NHLT_DEVICE_BT;
341db2f586bSSenthilnathan Veppur 		break;
342db2f586bSSenthilnathan Veppur 
343db2f586bSSenthilnathan Veppur 	case SKL_DEVICE_DMIC:
344db2f586bSSenthilnathan Veppur 		ret = NHLT_DEVICE_DMIC;
345db2f586bSSenthilnathan Veppur 		break;
346db2f586bSSenthilnathan Veppur 
347db2f586bSSenthilnathan Veppur 	case SKL_DEVICE_I2S:
348db2f586bSSenthilnathan Veppur 		ret = NHLT_DEVICE_I2S;
349db2f586bSSenthilnathan Veppur 		break;
350db2f586bSSenthilnathan Veppur 
351db2f586bSSenthilnathan Veppur 	default:
352db2f586bSSenthilnathan Veppur 		ret = NHLT_DEVICE_INVALID;
353db2f586bSSenthilnathan Veppur 		break;
354db2f586bSSenthilnathan Veppur 	}
355db2f586bSSenthilnathan Veppur 
356db2f586bSSenthilnathan Veppur 	return ret;
357db2f586bSSenthilnathan Veppur }
358db2f586bSSenthilnathan Veppur 
3592d1419a3SJeeja KP static int skl_tplg_update_be_blob(struct snd_soc_dapm_widget *w,
3602d1419a3SJeeja KP 						struct skl_sst *ctx)
3612d1419a3SJeeja KP {
3622d1419a3SJeeja KP 	struct skl_module_cfg *m_cfg = w->priv;
3632d1419a3SJeeja KP 	int link_type, dir;
3642d1419a3SJeeja KP 	u32 ch, s_freq, s_fmt;
3652d1419a3SJeeja KP 	struct nhlt_specific_cfg *cfg;
3662d1419a3SJeeja KP 	struct skl *skl = get_skl_ctx(ctx->dev);
367db2f586bSSenthilnathan Veppur 	u8 dev_type = skl_tplg_be_dev_type(m_cfg->dev_type);
3682d1419a3SJeeja KP 
3692d1419a3SJeeja KP 	/* check if we already have blob */
3702d1419a3SJeeja KP 	if (m_cfg->formats_config.caps_size > 0)
3712d1419a3SJeeja KP 		return 0;
3722d1419a3SJeeja KP 
373c7c6c736SJeeja KP 	dev_dbg(ctx->dev, "Applying default cfg blob\n");
3742d1419a3SJeeja KP 	switch (m_cfg->dev_type) {
3752d1419a3SJeeja KP 	case SKL_DEVICE_DMIC:
3762d1419a3SJeeja KP 		link_type = NHLT_LINK_DMIC;
377c7c6c736SJeeja KP 		dir = SNDRV_PCM_STREAM_CAPTURE;
3782d1419a3SJeeja KP 		s_freq = m_cfg->in_fmt[0].s_freq;
3792d1419a3SJeeja KP 		s_fmt = m_cfg->in_fmt[0].bit_depth;
3802d1419a3SJeeja KP 		ch = m_cfg->in_fmt[0].channels;
3812d1419a3SJeeja KP 		break;
3822d1419a3SJeeja KP 
3832d1419a3SJeeja KP 	case SKL_DEVICE_I2S:
3842d1419a3SJeeja KP 		link_type = NHLT_LINK_SSP;
3852d1419a3SJeeja KP 		if (m_cfg->hw_conn_type == SKL_CONN_SOURCE) {
386c7c6c736SJeeja KP 			dir = SNDRV_PCM_STREAM_PLAYBACK;
3872d1419a3SJeeja KP 			s_freq = m_cfg->out_fmt[0].s_freq;
3882d1419a3SJeeja KP 			s_fmt = m_cfg->out_fmt[0].bit_depth;
3892d1419a3SJeeja KP 			ch = m_cfg->out_fmt[0].channels;
390c7c6c736SJeeja KP 		} else {
391c7c6c736SJeeja KP 			dir = SNDRV_PCM_STREAM_CAPTURE;
392c7c6c736SJeeja KP 			s_freq = m_cfg->in_fmt[0].s_freq;
393c7c6c736SJeeja KP 			s_fmt = m_cfg->in_fmt[0].bit_depth;
394c7c6c736SJeeja KP 			ch = m_cfg->in_fmt[0].channels;
3952d1419a3SJeeja KP 		}
3962d1419a3SJeeja KP 		break;
3972d1419a3SJeeja KP 
3982d1419a3SJeeja KP 	default:
3992d1419a3SJeeja KP 		return -EINVAL;
4002d1419a3SJeeja KP 	}
4012d1419a3SJeeja KP 
4022d1419a3SJeeja KP 	/* update the blob based on virtual bus_id and default params */
4032d1419a3SJeeja KP 	cfg = skl_get_ep_blob(skl, m_cfg->vbus_id, link_type,
404db2f586bSSenthilnathan Veppur 					s_fmt, ch, s_freq, dir, dev_type);
4052d1419a3SJeeja KP 	if (cfg) {
4062d1419a3SJeeja KP 		m_cfg->formats_config.caps_size = cfg->size;
4072d1419a3SJeeja KP 		m_cfg->formats_config.caps = (u32 *) &cfg->caps;
4082d1419a3SJeeja KP 	} else {
4092d1419a3SJeeja KP 		dev_err(ctx->dev, "Blob NULL for id %x type %d dirn %d\n",
4102d1419a3SJeeja KP 					m_cfg->vbus_id, link_type, dir);
4112d1419a3SJeeja KP 		dev_err(ctx->dev, "PCM: ch %d, freq %d, fmt %d\n",
4122d1419a3SJeeja KP 					ch, s_freq, s_fmt);
4132d1419a3SJeeja KP 		return -EIO;
4142d1419a3SJeeja KP 	}
4152d1419a3SJeeja KP 
4162d1419a3SJeeja KP 	return 0;
4172d1419a3SJeeja KP }
4182d1419a3SJeeja KP 
419f7590d4fSJeeja KP static void skl_tplg_update_module_params(struct snd_soc_dapm_widget *w,
420f7590d4fSJeeja KP 							struct skl_sst *ctx)
421f7590d4fSJeeja KP {
422f7590d4fSJeeja KP 	struct skl_module_cfg *m_cfg = w->priv;
423f7590d4fSJeeja KP 	struct skl_pipe_params *params = m_cfg->pipe->p_params;
424f7590d4fSJeeja KP 	int p_conn_type = m_cfg->pipe->conn_type;
425f7590d4fSJeeja KP 	bool is_fe;
426f7590d4fSJeeja KP 
427f7590d4fSJeeja KP 	if (!m_cfg->params_fixup)
428f7590d4fSJeeja KP 		return;
429f7590d4fSJeeja KP 
430f7590d4fSJeeja KP 	dev_dbg(ctx->dev, "Mconfig for widget=%s BEFORE updation\n",
431f7590d4fSJeeja KP 				w->name);
432f7590d4fSJeeja KP 
433f7590d4fSJeeja KP 	skl_dump_mconfig(ctx, m_cfg);
434f7590d4fSJeeja KP 
435f7590d4fSJeeja KP 	if (p_conn_type == SKL_PIPE_CONN_TYPE_FE)
436f7590d4fSJeeja KP 		is_fe = true;
437f7590d4fSJeeja KP 	else
438f7590d4fSJeeja KP 		is_fe = false;
439f7590d4fSJeeja KP 
440f7590d4fSJeeja KP 	skl_tplg_update_params_fixup(m_cfg, params, is_fe);
441f7590d4fSJeeja KP 	skl_tplg_update_buffer_size(ctx, m_cfg);
442f7590d4fSJeeja KP 
443f7590d4fSJeeja KP 	dev_dbg(ctx->dev, "Mconfig for widget=%s AFTER updation\n",
444f7590d4fSJeeja KP 				w->name);
445f7590d4fSJeeja KP 
446f7590d4fSJeeja KP 	skl_dump_mconfig(ctx, m_cfg);
447f7590d4fSJeeja KP }
448f7590d4fSJeeja KP 
449e4e2d2f4SJeeja KP /*
450abb74003SJeeja KP  * some modules can have multiple params set from user control and
451abb74003SJeeja KP  * need to be set after module is initialized. If set_param flag is
452abb74003SJeeja KP  * set module params will be done after module is initialised.
453abb74003SJeeja KP  */
454abb74003SJeeja KP static int skl_tplg_set_module_params(struct snd_soc_dapm_widget *w,
455abb74003SJeeja KP 						struct skl_sst *ctx)
456abb74003SJeeja KP {
457abb74003SJeeja KP 	int i, ret;
458abb74003SJeeja KP 	struct skl_module_cfg *mconfig = w->priv;
459abb74003SJeeja KP 	const struct snd_kcontrol_new *k;
460abb74003SJeeja KP 	struct soc_bytes_ext *sb;
461abb74003SJeeja KP 	struct skl_algo_data *bc;
462abb74003SJeeja KP 	struct skl_specific_cfg *sp_cfg;
463abb74003SJeeja KP 
464abb74003SJeeja KP 	if (mconfig->formats_config.caps_size > 0 &&
4654ced1827SJeeja KP 		mconfig->formats_config.set_params == SKL_PARAM_SET) {
466abb74003SJeeja KP 		sp_cfg = &mconfig->formats_config;
467abb74003SJeeja KP 		ret = skl_set_module_params(ctx, sp_cfg->caps,
468abb74003SJeeja KP 					sp_cfg->caps_size,
469abb74003SJeeja KP 					sp_cfg->param_id, mconfig);
470abb74003SJeeja KP 		if (ret < 0)
471abb74003SJeeja KP 			return ret;
472abb74003SJeeja KP 	}
473abb74003SJeeja KP 
474abb74003SJeeja KP 	for (i = 0; i < w->num_kcontrols; i++) {
475abb74003SJeeja KP 		k = &w->kcontrol_news[i];
476abb74003SJeeja KP 		if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
477abb74003SJeeja KP 			sb = (void *) k->private_value;
478abb74003SJeeja KP 			bc = (struct skl_algo_data *)sb->dobj.private;
479abb74003SJeeja KP 
4804ced1827SJeeja KP 			if (bc->set_params == SKL_PARAM_SET) {
481abb74003SJeeja KP 				ret = skl_set_module_params(ctx,
4820d682104SDharageswari R 						(u32 *)bc->params, bc->size,
483abb74003SJeeja KP 						bc->param_id, mconfig);
484abb74003SJeeja KP 				if (ret < 0)
485abb74003SJeeja KP 					return ret;
486abb74003SJeeja KP 			}
487abb74003SJeeja KP 		}
488abb74003SJeeja KP 	}
489abb74003SJeeja KP 
490abb74003SJeeja KP 	return 0;
491abb74003SJeeja KP }
492abb74003SJeeja KP 
493abb74003SJeeja KP /*
494abb74003SJeeja KP  * some module param can set from user control and this is required as
495abb74003SJeeja KP  * when module is initailzed. if module param is required in init it is
496abb74003SJeeja KP  * identifed by set_param flag. if set_param flag is not set, then this
497abb74003SJeeja KP  * parameter needs to set as part of module init.
498abb74003SJeeja KP  */
499abb74003SJeeja KP static int skl_tplg_set_module_init_data(struct snd_soc_dapm_widget *w)
500abb74003SJeeja KP {
501abb74003SJeeja KP 	const struct snd_kcontrol_new *k;
502abb74003SJeeja KP 	struct soc_bytes_ext *sb;
503abb74003SJeeja KP 	struct skl_algo_data *bc;
504abb74003SJeeja KP 	struct skl_module_cfg *mconfig = w->priv;
505abb74003SJeeja KP 	int i;
506abb74003SJeeja KP 
507abb74003SJeeja KP 	for (i = 0; i < w->num_kcontrols; i++) {
508abb74003SJeeja KP 		k = &w->kcontrol_news[i];
509abb74003SJeeja KP 		if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
510abb74003SJeeja KP 			sb = (struct soc_bytes_ext *)k->private_value;
511abb74003SJeeja KP 			bc = (struct skl_algo_data *)sb->dobj.private;
512abb74003SJeeja KP 
5134ced1827SJeeja KP 			if (bc->set_params != SKL_PARAM_INIT)
514abb74003SJeeja KP 				continue;
515abb74003SJeeja KP 
516d1a6fe41STakashi Sakamoto 			mconfig->formats_config.caps = (u32 *)bc->params;
5170d682104SDharageswari R 			mconfig->formats_config.caps_size = bc->size;
518abb74003SJeeja KP 
519abb74003SJeeja KP 			break;
520abb74003SJeeja KP 		}
521abb74003SJeeja KP 	}
522abb74003SJeeja KP 
523abb74003SJeeja KP 	return 0;
524abb74003SJeeja KP }
525abb74003SJeeja KP 
526bb704a73SJeeja KP static int skl_tplg_module_prepare(struct skl_sst *ctx, struct skl_pipe *pipe,
527bb704a73SJeeja KP 		struct snd_soc_dapm_widget *w, struct skl_module_cfg *mcfg)
528bb704a73SJeeja KP {
529bb704a73SJeeja KP 	switch (mcfg->dev_type) {
530bb704a73SJeeja KP 	case SKL_DEVICE_HDAHOST:
531bb704a73SJeeja KP 		return skl_pcm_host_dma_prepare(ctx->dev, pipe->p_params);
532bb704a73SJeeja KP 
533bb704a73SJeeja KP 	case SKL_DEVICE_HDALINK:
534bb704a73SJeeja KP 		return skl_pcm_link_dma_prepare(ctx->dev, pipe->p_params);
535bb704a73SJeeja KP 	}
536bb704a73SJeeja KP 
537bb704a73SJeeja KP 	return 0;
538bb704a73SJeeja KP }
539bb704a73SJeeja KP 
540abb74003SJeeja KP /*
541e4e2d2f4SJeeja KP  * Inside a pipe instance, we can have various modules. These modules need
542e4e2d2f4SJeeja KP  * to instantiated in DSP by invoking INIT_MODULE IPC, which is achieved by
543e4e2d2f4SJeeja KP  * skl_init_module() routine, so invoke that for all modules in a pipeline
544e4e2d2f4SJeeja KP  */
545e4e2d2f4SJeeja KP static int
546e4e2d2f4SJeeja KP skl_tplg_init_pipe_modules(struct skl *skl, struct skl_pipe *pipe)
547e4e2d2f4SJeeja KP {
548e4e2d2f4SJeeja KP 	struct skl_pipe_module *w_module;
549e4e2d2f4SJeeja KP 	struct snd_soc_dapm_widget *w;
550e4e2d2f4SJeeja KP 	struct skl_module_cfg *mconfig;
551e4e2d2f4SJeeja KP 	struct skl_sst *ctx = skl->skl_sst;
552e4e2d2f4SJeeja KP 	int ret = 0;
553e4e2d2f4SJeeja KP 
554e4e2d2f4SJeeja KP 	list_for_each_entry(w_module, &pipe->w_list, node) {
555b26199eaSJeeja KP 		uuid_le *uuid_mod;
556e4e2d2f4SJeeja KP 		w = w_module->w;
557e4e2d2f4SJeeja KP 		mconfig = w->priv;
558e4e2d2f4SJeeja KP 
559b7c50555SVinod Koul 		/* check if module ids are populated */
560b7c50555SVinod Koul 		if (mconfig->id.module_id < 0) {
561b7c50555SVinod Koul 			dev_err(skl->skl_sst->dev,
562a657ae7eSVinod Koul 					"module %pUL id not populated\n",
563a657ae7eSVinod Koul 					(uuid_le *)mconfig->guid);
564a657ae7eSVinod Koul 			return -EIO;
565b7c50555SVinod Koul 		}
566b7c50555SVinod Koul 
567e4e2d2f4SJeeja KP 		/* check resource available */
5689ba8ffefSDharageswari.R 		if (!skl_is_pipe_mcps_avail(skl, mconfig))
569e4e2d2f4SJeeja KP 			return -ENOMEM;
570e4e2d2f4SJeeja KP 
5716c5768b3SDharageswari R 		if (mconfig->is_loadable && ctx->dsp->fw_ops.load_mod) {
5726c5768b3SDharageswari R 			ret = ctx->dsp->fw_ops.load_mod(ctx->dsp,
5736c5768b3SDharageswari R 				mconfig->id.module_id, mconfig->guid);
5746c5768b3SDharageswari R 			if (ret < 0)
5756c5768b3SDharageswari R 				return ret;
576d643678bSJeeja KP 
577d643678bSJeeja KP 			mconfig->m_state = SKL_MODULE_LOADED;
5786c5768b3SDharageswari R 		}
5796c5768b3SDharageswari R 
580bb704a73SJeeja KP 		/* prepare the DMA if the module is gateway cpr */
581bb704a73SJeeja KP 		ret = skl_tplg_module_prepare(ctx, pipe, w, mconfig);
582bb704a73SJeeja KP 		if (ret < 0)
583bb704a73SJeeja KP 			return ret;
584bb704a73SJeeja KP 
5852d1419a3SJeeja KP 		/* update blob if blob is null for be with default value */
5862d1419a3SJeeja KP 		skl_tplg_update_be_blob(w, ctx);
5872d1419a3SJeeja KP 
588f7590d4fSJeeja KP 		/*
589f7590d4fSJeeja KP 		 * apply fix/conversion to module params based on
590f7590d4fSJeeja KP 		 * FE/BE params
591f7590d4fSJeeja KP 		 */
592f7590d4fSJeeja KP 		skl_tplg_update_module_params(w, ctx);
593b26199eaSJeeja KP 		uuid_mod = (uuid_le *)mconfig->guid;
594b26199eaSJeeja KP 		mconfig->id.pvt_id = skl_get_pvt_id(ctx, uuid_mod,
595b26199eaSJeeja KP 						mconfig->id.instance_id);
596ef2a352cSDharageswari R 		if (mconfig->id.pvt_id < 0)
597ef2a352cSDharageswari R 			return ret;
598abb74003SJeeja KP 		skl_tplg_set_module_init_data(w);
5999939a9c3SJeeja KP 		ret = skl_init_module(ctx, mconfig);
600ef2a352cSDharageswari R 		if (ret < 0) {
601b26199eaSJeeja KP 			skl_put_pvt_id(ctx, uuid_mod, &mconfig->id.pvt_id);
602e4e2d2f4SJeeja KP 			return ret;
603ef2a352cSDharageswari R 		}
604260eb73aSDharageswari R 		skl_tplg_alloc_pipe_mcps(skl, mconfig);
605abb74003SJeeja KP 		ret = skl_tplg_set_module_params(w, ctx);
606e4e2d2f4SJeeja KP 		if (ret < 0)
607e4e2d2f4SJeeja KP 			return ret;
608e4e2d2f4SJeeja KP 	}
609e4e2d2f4SJeeja KP 
610e4e2d2f4SJeeja KP 	return 0;
611e4e2d2f4SJeeja KP }
612d93f8e55SVinod Koul 
6136c5768b3SDharageswari R static int skl_tplg_unload_pipe_modules(struct skl_sst *ctx,
6146c5768b3SDharageswari R 	 struct skl_pipe *pipe)
6156c5768b3SDharageswari R {
616b0fab9c6SDharageswari R 	int ret;
6176c5768b3SDharageswari R 	struct skl_pipe_module *w_module = NULL;
6186c5768b3SDharageswari R 	struct skl_module_cfg *mconfig = NULL;
6196c5768b3SDharageswari R 
6206c5768b3SDharageswari R 	list_for_each_entry(w_module, &pipe->w_list, node) {
621b26199eaSJeeja KP 		uuid_le *uuid_mod;
6226c5768b3SDharageswari R 		mconfig  = w_module->w->priv;
623b26199eaSJeeja KP 		uuid_mod = (uuid_le *)mconfig->guid;
6246c5768b3SDharageswari R 
625d643678bSJeeja KP 		if (mconfig->is_loadable && ctx->dsp->fw_ops.unload_mod &&
626b0fab9c6SDharageswari R 			mconfig->m_state > SKL_MODULE_UNINIT) {
627b0fab9c6SDharageswari R 			ret = ctx->dsp->fw_ops.unload_mod(ctx->dsp,
6286c5768b3SDharageswari R 						mconfig->id.module_id);
629b0fab9c6SDharageswari R 			if (ret < 0)
630b0fab9c6SDharageswari R 				return -EIO;
631b0fab9c6SDharageswari R 		}
632b26199eaSJeeja KP 		skl_put_pvt_id(ctx, uuid_mod, &mconfig->id.pvt_id);
6336c5768b3SDharageswari R 	}
6346c5768b3SDharageswari R 
6356c5768b3SDharageswari R 	/* no modules to unload in this path, so return */
6366c5768b3SDharageswari R 	return 0;
6376c5768b3SDharageswari R }
6386c5768b3SDharageswari R 
639d93f8e55SVinod Koul /*
640d93f8e55SVinod Koul  * Mixer module represents a pipeline. So in the Pre-PMU event of mixer we
641d93f8e55SVinod Koul  * need create the pipeline. So we do following:
642d93f8e55SVinod Koul  *   - check the resources
643d93f8e55SVinod Koul  *   - Create the pipeline
644d93f8e55SVinod Koul  *   - Initialize the modules in pipeline
645d93f8e55SVinod Koul  *   - finally bind all modules together
646d93f8e55SVinod Koul  */
647d93f8e55SVinod Koul static int skl_tplg_mixer_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
648d93f8e55SVinod Koul 							struct skl *skl)
649d93f8e55SVinod Koul {
650d93f8e55SVinod Koul 	int ret;
651d93f8e55SVinod Koul 	struct skl_module_cfg *mconfig = w->priv;
652d93f8e55SVinod Koul 	struct skl_pipe_module *w_module;
653d93f8e55SVinod Koul 	struct skl_pipe *s_pipe = mconfig->pipe;
654b8c722ddSJeeja KP 	struct skl_module_cfg *src_module = NULL, *dst_module, *module;
655d93f8e55SVinod Koul 	struct skl_sst *ctx = skl->skl_sst;
656b8c722ddSJeeja KP 	struct skl_module_deferred_bind *modules;
657d93f8e55SVinod Koul 
658d93f8e55SVinod Koul 	/* check resource available */
6599ba8ffefSDharageswari.R 	if (!skl_is_pipe_mcps_avail(skl, mconfig))
660d93f8e55SVinod Koul 		return -EBUSY;
661d93f8e55SVinod Koul 
6629ba8ffefSDharageswari.R 	if (!skl_is_pipe_mem_avail(skl, mconfig))
663d93f8e55SVinod Koul 		return -ENOMEM;
664d93f8e55SVinod Koul 
665d93f8e55SVinod Koul 	/*
666d93f8e55SVinod Koul 	 * Create a list of modules for pipe.
667d93f8e55SVinod Koul 	 * This list contains modules from source to sink
668d93f8e55SVinod Koul 	 */
669d93f8e55SVinod Koul 	ret = skl_create_pipeline(ctx, mconfig->pipe);
670d93f8e55SVinod Koul 	if (ret < 0)
671d93f8e55SVinod Koul 		return ret;
672d93f8e55SVinod Koul 
673260eb73aSDharageswari R 	skl_tplg_alloc_pipe_mem(skl, mconfig);
674260eb73aSDharageswari R 	skl_tplg_alloc_pipe_mcps(skl, mconfig);
675d93f8e55SVinod Koul 
676d93f8e55SVinod Koul 	/* Init all pipe modules from source to sink */
677d93f8e55SVinod Koul 	ret = skl_tplg_init_pipe_modules(skl, s_pipe);
678d93f8e55SVinod Koul 	if (ret < 0)
679d93f8e55SVinod Koul 		return ret;
680d93f8e55SVinod Koul 
681d93f8e55SVinod Koul 	/* Bind modules from source to sink */
682d93f8e55SVinod Koul 	list_for_each_entry(w_module, &s_pipe->w_list, node) {
683d93f8e55SVinod Koul 		dst_module = w_module->w->priv;
684d93f8e55SVinod Koul 
685d93f8e55SVinod Koul 		if (src_module == NULL) {
686d93f8e55SVinod Koul 			src_module = dst_module;
687d93f8e55SVinod Koul 			continue;
688d93f8e55SVinod Koul 		}
689d93f8e55SVinod Koul 
690d93f8e55SVinod Koul 		ret = skl_bind_modules(ctx, src_module, dst_module);
691d93f8e55SVinod Koul 		if (ret < 0)
692d93f8e55SVinod Koul 			return ret;
693d93f8e55SVinod Koul 
694d93f8e55SVinod Koul 		src_module = dst_module;
695d93f8e55SVinod Koul 	}
696d93f8e55SVinod Koul 
697b8c722ddSJeeja KP 	/*
698b8c722ddSJeeja KP 	 * When the destination module is initialized, check for these modules
699b8c722ddSJeeja KP 	 * in deferred bind list. If found, bind them.
700b8c722ddSJeeja KP 	 */
701b8c722ddSJeeja KP 	list_for_each_entry(w_module, &s_pipe->w_list, node) {
702b8c722ddSJeeja KP 		if (list_empty(&skl->bind_list))
703b8c722ddSJeeja KP 			break;
704b8c722ddSJeeja KP 
705b8c722ddSJeeja KP 		list_for_each_entry(modules, &skl->bind_list, node) {
706b8c722ddSJeeja KP 			module = w_module->w->priv;
707b8c722ddSJeeja KP 			if (modules->dst == module)
708b8c722ddSJeeja KP 				skl_bind_modules(ctx, modules->src,
709b8c722ddSJeeja KP 							modules->dst);
710b8c722ddSJeeja KP 		}
711b8c722ddSJeeja KP 	}
712b8c722ddSJeeja KP 
713d93f8e55SVinod Koul 	return 0;
714d93f8e55SVinod Koul }
715d93f8e55SVinod Koul 
716bf3e5ef5SDharageswari R static int skl_fill_sink_instance_id(struct skl_sst *ctx, u32 *params,
717bf3e5ef5SDharageswari R 				int size, struct skl_module_cfg *mcfg)
7185e8f0ee4SDharageswari R {
7195e8f0ee4SDharageswari R 	int i, pvt_id;
7205e8f0ee4SDharageswari R 
721bf3e5ef5SDharageswari R 	if (mcfg->m_type == SKL_MODULE_TYPE_KPB) {
722bf3e5ef5SDharageswari R 		struct skl_kpb_params *kpb_params =
723bf3e5ef5SDharageswari R 				(struct skl_kpb_params *)params;
724bf3e5ef5SDharageswari R 		struct skl_mod_inst_map *inst = kpb_params->map;
7255e8f0ee4SDharageswari R 
726bf3e5ef5SDharageswari R 		for (i = 0; i < kpb_params->num_modules; i++) {
727bf3e5ef5SDharageswari R 			pvt_id = skl_get_pvt_instance_id_map(ctx, inst->mod_id,
728bf3e5ef5SDharageswari R 								inst->inst_id);
7295e8f0ee4SDharageswari R 			if (pvt_id < 0)
7305e8f0ee4SDharageswari R 				return -EINVAL;
731bf3e5ef5SDharageswari R 
7325e8f0ee4SDharageswari R 			inst->inst_id = pvt_id;
7335e8f0ee4SDharageswari R 			inst++;
7345e8f0ee4SDharageswari R 		}
7355e8f0ee4SDharageswari R 	}
7365e8f0ee4SDharageswari R 
737bf3e5ef5SDharageswari R 	return 0;
738bf3e5ef5SDharageswari R }
739cc6a4044SJeeja KP /*
740cc6a4044SJeeja KP  * Some modules require params to be set after the module is bound to
741cc6a4044SJeeja KP  * all pins connected.
742cc6a4044SJeeja KP  *
743cc6a4044SJeeja KP  * The module provider initializes set_param flag for such modules and we
744cc6a4044SJeeja KP  * send params after binding
745cc6a4044SJeeja KP  */
746cc6a4044SJeeja KP static int skl_tplg_set_module_bind_params(struct snd_soc_dapm_widget *w,
747cc6a4044SJeeja KP 			struct skl_module_cfg *mcfg, struct skl_sst *ctx)
748cc6a4044SJeeja KP {
749cc6a4044SJeeja KP 	int i, ret;
750cc6a4044SJeeja KP 	struct skl_module_cfg *mconfig = w->priv;
751cc6a4044SJeeja KP 	const struct snd_kcontrol_new *k;
752cc6a4044SJeeja KP 	struct soc_bytes_ext *sb;
753cc6a4044SJeeja KP 	struct skl_algo_data *bc;
754cc6a4044SJeeja KP 	struct skl_specific_cfg *sp_cfg;
755bf3e5ef5SDharageswari R 	u32 *params;
756cc6a4044SJeeja KP 
757cc6a4044SJeeja KP 	/*
758cc6a4044SJeeja KP 	 * check all out/in pins are in bind state.
759cc6a4044SJeeja KP 	 * if so set the module param
760cc6a4044SJeeja KP 	 */
761cc6a4044SJeeja KP 	for (i = 0; i < mcfg->max_out_queue; i++) {
762cc6a4044SJeeja KP 		if (mcfg->m_out_pin[i].pin_state != SKL_PIN_BIND_DONE)
763cc6a4044SJeeja KP 			return 0;
764cc6a4044SJeeja KP 	}
765cc6a4044SJeeja KP 
766cc6a4044SJeeja KP 	for (i = 0; i < mcfg->max_in_queue; i++) {
767cc6a4044SJeeja KP 		if (mcfg->m_in_pin[i].pin_state != SKL_PIN_BIND_DONE)
768cc6a4044SJeeja KP 			return 0;
769cc6a4044SJeeja KP 	}
770cc6a4044SJeeja KP 
771cc6a4044SJeeja KP 	if (mconfig->formats_config.caps_size > 0 &&
772cc6a4044SJeeja KP 		mconfig->formats_config.set_params == SKL_PARAM_BIND) {
773cc6a4044SJeeja KP 		sp_cfg = &mconfig->formats_config;
774cc6a4044SJeeja KP 		ret = skl_set_module_params(ctx, sp_cfg->caps,
775cc6a4044SJeeja KP 					sp_cfg->caps_size,
776cc6a4044SJeeja KP 					sp_cfg->param_id, mconfig);
777cc6a4044SJeeja KP 		if (ret < 0)
778cc6a4044SJeeja KP 			return ret;
779cc6a4044SJeeja KP 	}
780cc6a4044SJeeja KP 
781cc6a4044SJeeja KP 	for (i = 0; i < w->num_kcontrols; i++) {
782cc6a4044SJeeja KP 		k = &w->kcontrol_news[i];
783cc6a4044SJeeja KP 		if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
784cc6a4044SJeeja KP 			sb = (void *) k->private_value;
785cc6a4044SJeeja KP 			bc = (struct skl_algo_data *)sb->dobj.private;
786cc6a4044SJeeja KP 
787cc6a4044SJeeja KP 			if (bc->set_params == SKL_PARAM_BIND) {
788bf3e5ef5SDharageswari R 				params = kzalloc(bc->max, GFP_KERNEL);
789bf3e5ef5SDharageswari R 				if (!params)
790bf3e5ef5SDharageswari R 					return -ENOMEM;
791bf3e5ef5SDharageswari R 
792bf3e5ef5SDharageswari R 				memcpy(params, bc->params, bc->max);
793bf3e5ef5SDharageswari R 				skl_fill_sink_instance_id(ctx, params, bc->max,
794bf3e5ef5SDharageswari R 								mconfig);
795bf3e5ef5SDharageswari R 
796bf3e5ef5SDharageswari R 				ret = skl_set_module_params(ctx, params,
797bf3e5ef5SDharageswari R 						bc->max, bc->param_id, mconfig);
798bf3e5ef5SDharageswari R 				kfree(params);
799bf3e5ef5SDharageswari R 
800cc6a4044SJeeja KP 				if (ret < 0)
801cc6a4044SJeeja KP 					return ret;
802cc6a4044SJeeja KP 			}
803cc6a4044SJeeja KP 		}
804cc6a4044SJeeja KP 	}
805cc6a4044SJeeja KP 
806cc6a4044SJeeja KP 	return 0;
807cc6a4044SJeeja KP }
808cc6a4044SJeeja KP 
809b8c722ddSJeeja KP 
810b8c722ddSJeeja KP static int skl_tplg_module_add_deferred_bind(struct skl *skl,
811b8c722ddSJeeja KP 	struct skl_module_cfg *src, struct skl_module_cfg *dst)
812b8c722ddSJeeja KP {
813b8c722ddSJeeja KP 	struct skl_module_deferred_bind *m_list, *modules;
814b8c722ddSJeeja KP 	int i;
815b8c722ddSJeeja KP 
816b8c722ddSJeeja KP 	/* only supported for module with static pin connection */
817b8c722ddSJeeja KP 	for (i = 0; i < dst->max_in_queue; i++) {
818b8c722ddSJeeja KP 		struct skl_module_pin *pin = &dst->m_in_pin[i];
819b8c722ddSJeeja KP 
820b8c722ddSJeeja KP 		if (pin->is_dynamic)
821b8c722ddSJeeja KP 			continue;
822b8c722ddSJeeja KP 
823b8c722ddSJeeja KP 		if ((pin->id.module_id  == src->id.module_id) &&
824b8c722ddSJeeja KP 			(pin->id.instance_id  == src->id.instance_id)) {
825b8c722ddSJeeja KP 
826b8c722ddSJeeja KP 			if (!list_empty(&skl->bind_list)) {
827b8c722ddSJeeja KP 				list_for_each_entry(modules, &skl->bind_list, node) {
828b8c722ddSJeeja KP 					if (modules->src == src && modules->dst == dst)
829b8c722ddSJeeja KP 						return 0;
830b8c722ddSJeeja KP 				}
831b8c722ddSJeeja KP 			}
832b8c722ddSJeeja KP 
833b8c722ddSJeeja KP 			m_list = kzalloc(sizeof(*m_list), GFP_KERNEL);
834b8c722ddSJeeja KP 			if (!m_list)
835b8c722ddSJeeja KP 				return -ENOMEM;
836b8c722ddSJeeja KP 
837b8c722ddSJeeja KP 			m_list->src = src;
838b8c722ddSJeeja KP 			m_list->dst = dst;
839b8c722ddSJeeja KP 
840b8c722ddSJeeja KP 			list_add(&m_list->node, &skl->bind_list);
841b8c722ddSJeeja KP 		}
842b8c722ddSJeeja KP 	}
843b8c722ddSJeeja KP 
844b8c722ddSJeeja KP 	return 0;
845b8c722ddSJeeja KP }
846b8c722ddSJeeja KP 
8478724ff17SJeeja KP static int skl_tplg_bind_sinks(struct snd_soc_dapm_widget *w,
8488724ff17SJeeja KP 				struct skl *skl,
8496bd4cf85SJeeja KP 				struct snd_soc_dapm_widget *src_w,
8508724ff17SJeeja KP 				struct skl_module_cfg *src_mconfig)
851d93f8e55SVinod Koul {
852d93f8e55SVinod Koul 	struct snd_soc_dapm_path *p;
8530ed95d76SJeeja KP 	struct snd_soc_dapm_widget *sink = NULL, *next_sink = NULL;
8548724ff17SJeeja KP 	struct skl_module_cfg *sink_mconfig;
855d93f8e55SVinod Koul 	struct skl_sst *ctx = skl->skl_sst;
8568724ff17SJeeja KP 	int ret;
857d93f8e55SVinod Koul 
8588724ff17SJeeja KP 	snd_soc_dapm_widget_for_each_sink_path(w, p) {
859d93f8e55SVinod Koul 		if (!p->connect)
860d93f8e55SVinod Koul 			continue;
861d93f8e55SVinod Koul 
862d93f8e55SVinod Koul 		dev_dbg(ctx->dev, "%s: src widget=%s\n", __func__, w->name);
863d93f8e55SVinod Koul 		dev_dbg(ctx->dev, "%s: sink widget=%s\n", __func__, p->sink->name);
864d93f8e55SVinod Koul 
8650ed95d76SJeeja KP 		next_sink = p->sink;
8666bd4cf85SJeeja KP 
8676bd4cf85SJeeja KP 		if (!is_skl_dsp_widget_type(p->sink))
8686bd4cf85SJeeja KP 			return skl_tplg_bind_sinks(p->sink, skl, src_w, src_mconfig);
8696bd4cf85SJeeja KP 
870d93f8e55SVinod Koul 		/*
871d93f8e55SVinod Koul 		 * here we will check widgets in sink pipelines, so that
872d93f8e55SVinod Koul 		 * can be any widgets type and we are only interested if
873d93f8e55SVinod Koul 		 * they are ones used for SKL so check that first
874d93f8e55SVinod Koul 		 */
875d93f8e55SVinod Koul 		if ((p->sink->priv != NULL) &&
876d93f8e55SVinod Koul 					is_skl_dsp_widget_type(p->sink)) {
877d93f8e55SVinod Koul 
878d93f8e55SVinod Koul 			sink = p->sink;
879d93f8e55SVinod Koul 			sink_mconfig = sink->priv;
880d93f8e55SVinod Koul 
881b8c722ddSJeeja KP 			/*
882b8c722ddSJeeja KP 			 * Modules other than PGA leaf can be connected
883b8c722ddSJeeja KP 			 * directly or via switch to a module in another
884b8c722ddSJeeja KP 			 * pipeline. EX: reference path
885b8c722ddSJeeja KP 			 * when the path is enabled, the dst module that needs
886b8c722ddSJeeja KP 			 * to be bound may not be initialized. if the module is
887b8c722ddSJeeja KP 			 * not initialized, add these modules in the deferred
888b8c722ddSJeeja KP 			 * bind list and when the dst module is initialised,
889b8c722ddSJeeja KP 			 * bind this module to the dst_module in deferred list.
890b8c722ddSJeeja KP 			 */
891b8c722ddSJeeja KP 			if (((src_mconfig->m_state == SKL_MODULE_INIT_DONE)
892b8c722ddSJeeja KP 				&& (sink_mconfig->m_state == SKL_MODULE_UNINIT))) {
893b8c722ddSJeeja KP 
894b8c722ddSJeeja KP 				ret = skl_tplg_module_add_deferred_bind(skl,
895b8c722ddSJeeja KP 						src_mconfig, sink_mconfig);
896b8c722ddSJeeja KP 
897b8c722ddSJeeja KP 				if (ret < 0)
898b8c722ddSJeeja KP 					return ret;
899b8c722ddSJeeja KP 
900b8c722ddSJeeja KP 			}
901b8c722ddSJeeja KP 
902b8c722ddSJeeja KP 
903cc6a4044SJeeja KP 			if (src_mconfig->m_state == SKL_MODULE_UNINIT ||
904cc6a4044SJeeja KP 				sink_mconfig->m_state == SKL_MODULE_UNINIT)
905cc6a4044SJeeja KP 				continue;
906cc6a4044SJeeja KP 
907d93f8e55SVinod Koul 			/* Bind source to sink, mixin is always source */
908d93f8e55SVinod Koul 			ret = skl_bind_modules(ctx, src_mconfig, sink_mconfig);
909d93f8e55SVinod Koul 			if (ret)
910d93f8e55SVinod Koul 				return ret;
911d93f8e55SVinod Koul 
912cc6a4044SJeeja KP 			/* set module params after bind */
913cc6a4044SJeeja KP 			skl_tplg_set_module_bind_params(src_w, src_mconfig, ctx);
914cc6a4044SJeeja KP 			skl_tplg_set_module_bind_params(sink, sink_mconfig, ctx);
915cc6a4044SJeeja KP 
916d93f8e55SVinod Koul 			/* Start sinks pipe first */
917d93f8e55SVinod Koul 			if (sink_mconfig->pipe->state != SKL_PIPE_STARTED) {
918d1730c3dSJeeja KP 				if (sink_mconfig->pipe->conn_type !=
919d1730c3dSJeeja KP 							SKL_PIPE_CONN_TYPE_FE)
920d1730c3dSJeeja KP 					ret = skl_run_pipe(ctx,
921d1730c3dSJeeja KP 							sink_mconfig->pipe);
922d93f8e55SVinod Koul 				if (ret)
923d93f8e55SVinod Koul 					return ret;
924d93f8e55SVinod Koul 			}
925d93f8e55SVinod Koul 		}
926d93f8e55SVinod Koul 	}
927d93f8e55SVinod Koul 
9288724ff17SJeeja KP 	if (!sink)
9296bd4cf85SJeeja KP 		return skl_tplg_bind_sinks(next_sink, skl, src_w, src_mconfig);
9308724ff17SJeeja KP 
9318724ff17SJeeja KP 	return 0;
9328724ff17SJeeja KP }
9338724ff17SJeeja KP 
934d93f8e55SVinod Koul /*
935d93f8e55SVinod Koul  * A PGA represents a module in a pipeline. So in the Pre-PMU event of PGA
936d93f8e55SVinod Koul  * we need to do following:
937d93f8e55SVinod Koul  *   - Bind to sink pipeline
938d93f8e55SVinod Koul  *      Since the sink pipes can be running and we don't get mixer event on
939d93f8e55SVinod Koul  *      connect for already running mixer, we need to find the sink pipes
940d93f8e55SVinod Koul  *      here and bind to them. This way dynamic connect works.
941d93f8e55SVinod Koul  *   - Start sink pipeline, if not running
942d93f8e55SVinod Koul  *   - Then run current pipe
943d93f8e55SVinod Koul  */
944d93f8e55SVinod Koul static int skl_tplg_pga_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
945d93f8e55SVinod Koul 								struct skl *skl)
946d93f8e55SVinod Koul {
9478724ff17SJeeja KP 	struct skl_module_cfg *src_mconfig;
948d93f8e55SVinod Koul 	struct skl_sst *ctx = skl->skl_sst;
949d93f8e55SVinod Koul 	int ret = 0;
950d93f8e55SVinod Koul 
9518724ff17SJeeja KP 	src_mconfig = w->priv;
952d93f8e55SVinod Koul 
953d93f8e55SVinod Koul 	/*
954d93f8e55SVinod Koul 	 * find which sink it is connected to, bind with the sink,
955d93f8e55SVinod Koul 	 * if sink is not started, start sink pipe first, then start
956d93f8e55SVinod Koul 	 * this pipe
957d93f8e55SVinod Koul 	 */
9586bd4cf85SJeeja KP 	ret = skl_tplg_bind_sinks(w, skl, w, src_mconfig);
9598724ff17SJeeja KP 	if (ret)
9608724ff17SJeeja KP 		return ret;
9618724ff17SJeeja KP 
962d93f8e55SVinod Koul 	/* Start source pipe last after starting all sinks */
963d1730c3dSJeeja KP 	if (src_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
964d1730c3dSJeeja KP 		return skl_run_pipe(ctx, src_mconfig->pipe);
965d93f8e55SVinod Koul 
966d93f8e55SVinod Koul 	return 0;
967d93f8e55SVinod Koul }
968d93f8e55SVinod Koul 
9698724ff17SJeeja KP static struct snd_soc_dapm_widget *skl_get_src_dsp_widget(
9708724ff17SJeeja KP 		struct snd_soc_dapm_widget *w, struct skl *skl)
9718724ff17SJeeja KP {
9728724ff17SJeeja KP 	struct snd_soc_dapm_path *p;
9738724ff17SJeeja KP 	struct snd_soc_dapm_widget *src_w = NULL;
9748724ff17SJeeja KP 	struct skl_sst *ctx = skl->skl_sst;
9758724ff17SJeeja KP 
976d93f8e55SVinod Koul 	snd_soc_dapm_widget_for_each_source_path(w, p) {
9778724ff17SJeeja KP 		src_w = p->source;
978d93f8e55SVinod Koul 		if (!p->connect)
979d93f8e55SVinod Koul 			continue;
980d93f8e55SVinod Koul 
9818724ff17SJeeja KP 		dev_dbg(ctx->dev, "sink widget=%s\n", w->name);
9828724ff17SJeeja KP 		dev_dbg(ctx->dev, "src widget=%s\n", p->source->name);
983d93f8e55SVinod Koul 
984d93f8e55SVinod Koul 		/*
9858724ff17SJeeja KP 		 * here we will check widgets in sink pipelines, so that can
9868724ff17SJeeja KP 		 * be any widgets type and we are only interested if they are
9878724ff17SJeeja KP 		 * ones used for SKL so check that first
988d93f8e55SVinod Koul 		 */
9898724ff17SJeeja KP 		if ((p->source->priv != NULL) &&
9908724ff17SJeeja KP 					is_skl_dsp_widget_type(p->source)) {
9918724ff17SJeeja KP 			return p->source;
992d93f8e55SVinod Koul 		}
993d93f8e55SVinod Koul 	}
994d93f8e55SVinod Koul 
9958724ff17SJeeja KP 	if (src_w != NULL)
9968724ff17SJeeja KP 		return skl_get_src_dsp_widget(src_w, skl);
997d93f8e55SVinod Koul 
9988724ff17SJeeja KP 	return NULL;
999d93f8e55SVinod Koul }
1000d93f8e55SVinod Koul 
1001d93f8e55SVinod Koul /*
1002d93f8e55SVinod Koul  * in the Post-PMU event of mixer we need to do following:
1003d93f8e55SVinod Koul  *   - Check if this pipe is running
1004d93f8e55SVinod Koul  *   - if not, then
1005d93f8e55SVinod Koul  *	- bind this pipeline to its source pipeline
1006d93f8e55SVinod Koul  *	  if source pipe is already running, this means it is a dynamic
1007d93f8e55SVinod Koul  *	  connection and we need to bind only to that pipe
1008d93f8e55SVinod Koul  *	- start this pipeline
1009d93f8e55SVinod Koul  */
1010d93f8e55SVinod Koul static int skl_tplg_mixer_dapm_post_pmu_event(struct snd_soc_dapm_widget *w,
1011d93f8e55SVinod Koul 							struct skl *skl)
1012d93f8e55SVinod Koul {
1013d93f8e55SVinod Koul 	int ret = 0;
1014d93f8e55SVinod Koul 	struct snd_soc_dapm_widget *source, *sink;
1015d93f8e55SVinod Koul 	struct skl_module_cfg *src_mconfig, *sink_mconfig;
1016d93f8e55SVinod Koul 	struct skl_sst *ctx = skl->skl_sst;
1017d93f8e55SVinod Koul 	int src_pipe_started = 0;
1018d93f8e55SVinod Koul 
1019d93f8e55SVinod Koul 	sink = w;
1020d93f8e55SVinod Koul 	sink_mconfig = sink->priv;
1021d93f8e55SVinod Koul 
1022d93f8e55SVinod Koul 	/*
1023d93f8e55SVinod Koul 	 * If source pipe is already started, that means source is driving
1024d93f8e55SVinod Koul 	 * one more sink before this sink got connected, Since source is
1025d93f8e55SVinod Koul 	 * started, bind this sink to source and start this pipe.
1026d93f8e55SVinod Koul 	 */
10278724ff17SJeeja KP 	source = skl_get_src_dsp_widget(w, skl);
10288724ff17SJeeja KP 	if (source != NULL) {
1029d93f8e55SVinod Koul 		src_mconfig = source->priv;
1030d93f8e55SVinod Koul 		sink_mconfig = sink->priv;
1031d93f8e55SVinod Koul 		src_pipe_started = 1;
1032d93f8e55SVinod Koul 
1033d93f8e55SVinod Koul 		/*
10348724ff17SJeeja KP 		 * check pipe state, then no need to bind or start the
10358724ff17SJeeja KP 		 * pipe
1036d93f8e55SVinod Koul 		 */
1037d93f8e55SVinod Koul 		if (src_mconfig->pipe->state != SKL_PIPE_STARTED)
1038d93f8e55SVinod Koul 			src_pipe_started = 0;
1039d93f8e55SVinod Koul 	}
1040d93f8e55SVinod Koul 
1041d93f8e55SVinod Koul 	if (src_pipe_started) {
1042d93f8e55SVinod Koul 		ret = skl_bind_modules(ctx, src_mconfig, sink_mconfig);
1043d93f8e55SVinod Koul 		if (ret)
1044d93f8e55SVinod Koul 			return ret;
1045d93f8e55SVinod Koul 
1046cc6a4044SJeeja KP 		/* set module params after bind */
1047cc6a4044SJeeja KP 		skl_tplg_set_module_bind_params(source, src_mconfig, ctx);
1048cc6a4044SJeeja KP 		skl_tplg_set_module_bind_params(sink, sink_mconfig, ctx);
1049cc6a4044SJeeja KP 
1050d1730c3dSJeeja KP 		if (sink_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
1051d93f8e55SVinod Koul 			ret = skl_run_pipe(ctx, sink_mconfig->pipe);
1052d93f8e55SVinod Koul 	}
1053d93f8e55SVinod Koul 
1054d93f8e55SVinod Koul 	return ret;
1055d93f8e55SVinod Koul }
1056d93f8e55SVinod Koul 
1057d93f8e55SVinod Koul /*
1058d93f8e55SVinod Koul  * in the Pre-PMD event of mixer we need to do following:
1059d93f8e55SVinod Koul  *   - Stop the pipe
1060d93f8e55SVinod Koul  *   - find the source connections and remove that from dapm_path_list
1061d93f8e55SVinod Koul  *   - unbind with source pipelines if still connected
1062d93f8e55SVinod Koul  */
1063d93f8e55SVinod Koul static int skl_tplg_mixer_dapm_pre_pmd_event(struct snd_soc_dapm_widget *w,
1064d93f8e55SVinod Koul 							struct skl *skl)
1065d93f8e55SVinod Koul {
1066d93f8e55SVinod Koul 	struct skl_module_cfg *src_mconfig, *sink_mconfig;
1067ce1b5551SJeeja KP 	int ret = 0, i;
1068d93f8e55SVinod Koul 	struct skl_sst *ctx = skl->skl_sst;
1069d93f8e55SVinod Koul 
1070ce1b5551SJeeja KP 	sink_mconfig = w->priv;
1071d93f8e55SVinod Koul 
1072d93f8e55SVinod Koul 	/* Stop the pipe */
1073d93f8e55SVinod Koul 	ret = skl_stop_pipe(ctx, sink_mconfig->pipe);
1074d93f8e55SVinod Koul 	if (ret)
1075d93f8e55SVinod Koul 		return ret;
1076d93f8e55SVinod Koul 
1077ce1b5551SJeeja KP 	for (i = 0; i < sink_mconfig->max_in_queue; i++) {
1078ce1b5551SJeeja KP 		if (sink_mconfig->m_in_pin[i].pin_state == SKL_PIN_BIND_DONE) {
1079ce1b5551SJeeja KP 			src_mconfig = sink_mconfig->m_in_pin[i].tgt_mcfg;
1080ce1b5551SJeeja KP 			if (!src_mconfig)
1081ce1b5551SJeeja KP 				continue;
1082d93f8e55SVinod Koul 
1083ce1b5551SJeeja KP 			ret = skl_unbind_modules(ctx,
1084ce1b5551SJeeja KP 						src_mconfig, sink_mconfig);
1085ce1b5551SJeeja KP 		}
1086d93f8e55SVinod Koul 	}
1087d93f8e55SVinod Koul 
1088d93f8e55SVinod Koul 	return ret;
1089d93f8e55SVinod Koul }
1090d93f8e55SVinod Koul 
1091d93f8e55SVinod Koul /*
1092d93f8e55SVinod Koul  * in the Post-PMD event of mixer we need to do following:
1093d93f8e55SVinod Koul  *   - Free the mcps used
1094d93f8e55SVinod Koul  *   - Free the mem used
1095d93f8e55SVinod Koul  *   - Unbind the modules within the pipeline
1096d93f8e55SVinod Koul  *   - Delete the pipeline (modules are not required to be explicitly
1097d93f8e55SVinod Koul  *     deleted, pipeline delete is enough here
1098d93f8e55SVinod Koul  */
1099d93f8e55SVinod Koul static int skl_tplg_mixer_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
1100d93f8e55SVinod Koul 							struct skl *skl)
1101d93f8e55SVinod Koul {
1102d93f8e55SVinod Koul 	struct skl_module_cfg *mconfig = w->priv;
1103d93f8e55SVinod Koul 	struct skl_pipe_module *w_module;
1104d93f8e55SVinod Koul 	struct skl_module_cfg *src_module = NULL, *dst_module;
1105d93f8e55SVinod Koul 	struct skl_sst *ctx = skl->skl_sst;
1106d93f8e55SVinod Koul 	struct skl_pipe *s_pipe = mconfig->pipe;
1107550b349aSDan Carpenter 	struct skl_module_deferred_bind *modules, *tmp;
1108d93f8e55SVinod Koul 
1109260eb73aSDharageswari R 	if (s_pipe->state == SKL_PIPE_INVALID)
1110260eb73aSDharageswari R 		return -EINVAL;
1111260eb73aSDharageswari R 
1112d93f8e55SVinod Koul 	skl_tplg_free_pipe_mcps(skl, mconfig);
111365976878SVinod Koul 	skl_tplg_free_pipe_mem(skl, mconfig);
1114d93f8e55SVinod Koul 
1115d93f8e55SVinod Koul 	list_for_each_entry(w_module, &s_pipe->w_list, node) {
1116b8c722ddSJeeja KP 		if (list_empty(&skl->bind_list))
1117b8c722ddSJeeja KP 			break;
1118b8c722ddSJeeja KP 
1119b8c722ddSJeeja KP 		src_module = w_module->w->priv;
1120b8c722ddSJeeja KP 
1121550b349aSDan Carpenter 		list_for_each_entry_safe(modules, tmp, &skl->bind_list, node) {
1122b8c722ddSJeeja KP 			/*
1123b8c722ddSJeeja KP 			 * When the destination module is deleted, Unbind the
1124b8c722ddSJeeja KP 			 * modules from deferred bind list.
1125b8c722ddSJeeja KP 			 */
1126b8c722ddSJeeja KP 			if (modules->dst == src_module) {
1127b8c722ddSJeeja KP 				skl_unbind_modules(ctx, modules->src,
1128b8c722ddSJeeja KP 						modules->dst);
1129b8c722ddSJeeja KP 			}
1130b8c722ddSJeeja KP 
1131b8c722ddSJeeja KP 			/*
1132b8c722ddSJeeja KP 			 * When the source module is deleted, remove this entry
1133b8c722ddSJeeja KP 			 * from the deferred bind list.
1134b8c722ddSJeeja KP 			 */
1135b8c722ddSJeeja KP 			if (modules->src == src_module) {
1136b8c722ddSJeeja KP 				list_del(&modules->node);
1137b8c722ddSJeeja KP 				modules->src = NULL;
1138b8c722ddSJeeja KP 				modules->dst = NULL;
1139b8c722ddSJeeja KP 				kfree(modules);
1140b8c722ddSJeeja KP 			}
1141b8c722ddSJeeja KP 		}
1142b8c722ddSJeeja KP 	}
1143b8c722ddSJeeja KP 
1144b8c722ddSJeeja KP 	list_for_each_entry(w_module, &s_pipe->w_list, node) {
1145d93f8e55SVinod Koul 		dst_module = w_module->w->priv;
1146d93f8e55SVinod Koul 
1147260eb73aSDharageswari R 		if (mconfig->m_state >= SKL_MODULE_INIT_DONE)
11487ae3cb15SVinod Koul 			skl_tplg_free_pipe_mcps(skl, dst_module);
1149d93f8e55SVinod Koul 		if (src_module == NULL) {
1150d93f8e55SVinod Koul 			src_module = dst_module;
1151d93f8e55SVinod Koul 			continue;
1152d93f8e55SVinod Koul 		}
1153d93f8e55SVinod Koul 
11547ca42f5aSGuneshwor Singh 		skl_unbind_modules(ctx, src_module, dst_module);
1155d93f8e55SVinod Koul 		src_module = dst_module;
1156d93f8e55SVinod Koul 	}
1157d93f8e55SVinod Koul 
1158547cafa3SVinod Koul 	skl_delete_pipe(ctx, mconfig->pipe);
1159d93f8e55SVinod Koul 
1160473a4d51SJeeja KP 	list_for_each_entry(w_module, &s_pipe->w_list, node) {
1161473a4d51SJeeja KP 		src_module = w_module->w->priv;
1162473a4d51SJeeja KP 		src_module->m_state = SKL_MODULE_UNINIT;
1163473a4d51SJeeja KP 	}
1164473a4d51SJeeja KP 
11656c5768b3SDharageswari R 	return skl_tplg_unload_pipe_modules(ctx, s_pipe);
1166d93f8e55SVinod Koul }
1167d93f8e55SVinod Koul 
1168d93f8e55SVinod Koul /*
1169d93f8e55SVinod Koul  * in the Post-PMD event of PGA we need to do following:
1170d93f8e55SVinod Koul  *   - Free the mcps used
1171d93f8e55SVinod Koul  *   - Stop the pipeline
1172d93f8e55SVinod Koul  *   - In source pipe is connected, unbind with source pipelines
1173d93f8e55SVinod Koul  */
1174d93f8e55SVinod Koul static int skl_tplg_pga_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
1175d93f8e55SVinod Koul 								struct skl *skl)
1176d93f8e55SVinod Koul {
1177d93f8e55SVinod Koul 	struct skl_module_cfg *src_mconfig, *sink_mconfig;
1178ce1b5551SJeeja KP 	int ret = 0, i;
1179d93f8e55SVinod Koul 	struct skl_sst *ctx = skl->skl_sst;
1180d93f8e55SVinod Koul 
1181ce1b5551SJeeja KP 	src_mconfig = w->priv;
1182d93f8e55SVinod Koul 
1183d93f8e55SVinod Koul 	/* Stop the pipe since this is a mixin module */
1184d93f8e55SVinod Koul 	ret = skl_stop_pipe(ctx, src_mconfig->pipe);
1185d93f8e55SVinod Koul 	if (ret)
1186d93f8e55SVinod Koul 		return ret;
1187d93f8e55SVinod Koul 
1188ce1b5551SJeeja KP 	for (i = 0; i < src_mconfig->max_out_queue; i++) {
1189ce1b5551SJeeja KP 		if (src_mconfig->m_out_pin[i].pin_state == SKL_PIN_BIND_DONE) {
1190ce1b5551SJeeja KP 			sink_mconfig = src_mconfig->m_out_pin[i].tgt_mcfg;
1191ce1b5551SJeeja KP 			if (!sink_mconfig)
1192ce1b5551SJeeja KP 				continue;
1193d93f8e55SVinod Koul 			/*
1194ce1b5551SJeeja KP 			 * This is a connecter and if path is found that means
1195d93f8e55SVinod Koul 			 * unbind between source and sink has not happened yet
1196d93f8e55SVinod Koul 			 */
1197ce1b5551SJeeja KP 			ret = skl_unbind_modules(ctx, src_mconfig,
1198ce1b5551SJeeja KP 							sink_mconfig);
1199ce1b5551SJeeja KP 		}
1200d93f8e55SVinod Koul 	}
1201d93f8e55SVinod Koul 
1202d93f8e55SVinod Koul 	return ret;
1203d93f8e55SVinod Koul }
1204d93f8e55SVinod Koul 
1205d93f8e55SVinod Koul /*
1206d93f8e55SVinod Koul  * In modelling, we assume there will be ONLY one mixer in a pipeline. If a
1207d93f8e55SVinod Koul  * second one is required that is created as another pipe entity.
1208d93f8e55SVinod Koul  * The mixer is responsible for pipe management and represent a pipeline
1209d93f8e55SVinod Koul  * instance
1210d93f8e55SVinod Koul  */
1211d93f8e55SVinod Koul static int skl_tplg_mixer_event(struct snd_soc_dapm_widget *w,
1212d93f8e55SVinod Koul 				struct snd_kcontrol *k, int event)
1213d93f8e55SVinod Koul {
1214d93f8e55SVinod Koul 	struct snd_soc_dapm_context *dapm = w->dapm;
1215d93f8e55SVinod Koul 	struct skl *skl = get_skl_ctx(dapm->dev);
1216d93f8e55SVinod Koul 
1217d93f8e55SVinod Koul 	switch (event) {
1218d93f8e55SVinod Koul 	case SND_SOC_DAPM_PRE_PMU:
1219d93f8e55SVinod Koul 		return skl_tplg_mixer_dapm_pre_pmu_event(w, skl);
1220d93f8e55SVinod Koul 
1221d93f8e55SVinod Koul 	case SND_SOC_DAPM_POST_PMU:
1222d93f8e55SVinod Koul 		return skl_tplg_mixer_dapm_post_pmu_event(w, skl);
1223d93f8e55SVinod Koul 
1224d93f8e55SVinod Koul 	case SND_SOC_DAPM_PRE_PMD:
1225d93f8e55SVinod Koul 		return skl_tplg_mixer_dapm_pre_pmd_event(w, skl);
1226d93f8e55SVinod Koul 
1227d93f8e55SVinod Koul 	case SND_SOC_DAPM_POST_PMD:
1228d93f8e55SVinod Koul 		return skl_tplg_mixer_dapm_post_pmd_event(w, skl);
1229d93f8e55SVinod Koul 	}
1230d93f8e55SVinod Koul 
1231d93f8e55SVinod Koul 	return 0;
1232d93f8e55SVinod Koul }
1233d93f8e55SVinod Koul 
1234d93f8e55SVinod Koul /*
1235d93f8e55SVinod Koul  * In modelling, we assumed rest of the modules in pipeline are PGA. But we
1236d93f8e55SVinod Koul  * are interested in last PGA (leaf PGA) in a pipeline to disconnect with
1237d93f8e55SVinod Koul  * the sink when it is running (two FE to one BE or one FE to two BE)
1238d93f8e55SVinod Koul  * scenarios
1239d93f8e55SVinod Koul  */
1240d93f8e55SVinod Koul static int skl_tplg_pga_event(struct snd_soc_dapm_widget *w,
1241d93f8e55SVinod Koul 			struct snd_kcontrol *k, int event)
1242d93f8e55SVinod Koul 
1243d93f8e55SVinod Koul {
1244d93f8e55SVinod Koul 	struct snd_soc_dapm_context *dapm = w->dapm;
1245d93f8e55SVinod Koul 	struct skl *skl = get_skl_ctx(dapm->dev);
1246d93f8e55SVinod Koul 
1247d93f8e55SVinod Koul 	switch (event) {
1248d93f8e55SVinod Koul 	case SND_SOC_DAPM_PRE_PMU:
1249d93f8e55SVinod Koul 		return skl_tplg_pga_dapm_pre_pmu_event(w, skl);
1250d93f8e55SVinod Koul 
1251d93f8e55SVinod Koul 	case SND_SOC_DAPM_POST_PMD:
1252d93f8e55SVinod Koul 		return skl_tplg_pga_dapm_post_pmd_event(w, skl);
1253d93f8e55SVinod Koul 	}
1254d93f8e55SVinod Koul 
1255d93f8e55SVinod Koul 	return 0;
1256d93f8e55SVinod Koul }
1257cfb0a873SVinod Koul 
1258140adfbaSJeeja KP static int skl_tplg_tlv_control_get(struct snd_kcontrol *kcontrol,
1259140adfbaSJeeja KP 			unsigned int __user *data, unsigned int size)
1260140adfbaSJeeja KP {
1261140adfbaSJeeja KP 	struct soc_bytes_ext *sb =
1262140adfbaSJeeja KP 			(struct soc_bytes_ext *)kcontrol->private_value;
1263140adfbaSJeeja KP 	struct skl_algo_data *bc = (struct skl_algo_data *)sb->dobj.private;
12647d9f2911SOmair M Abdullah 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
12657d9f2911SOmair M Abdullah 	struct skl_module_cfg *mconfig = w->priv;
12667d9f2911SOmair M Abdullah 	struct skl *skl = get_skl_ctx(w->dapm->dev);
12677d9f2911SOmair M Abdullah 
12687d9f2911SOmair M Abdullah 	if (w->power)
12697d9f2911SOmair M Abdullah 		skl_get_module_params(skl->skl_sst, (u32 *)bc->params,
12700d682104SDharageswari R 				      bc->size, bc->param_id, mconfig);
1271140adfbaSJeeja KP 
127241556f68SVinod Koul 	/* decrement size for TLV header */
127341556f68SVinod Koul 	size -= 2 * sizeof(u32);
127441556f68SVinod Koul 
127541556f68SVinod Koul 	/* check size as we don't want to send kernel data */
127641556f68SVinod Koul 	if (size > bc->max)
127741556f68SVinod Koul 		size = bc->max;
127841556f68SVinod Koul 
1279140adfbaSJeeja KP 	if (bc->params) {
1280140adfbaSJeeja KP 		if (copy_to_user(data, &bc->param_id, sizeof(u32)))
1281140adfbaSJeeja KP 			return -EFAULT;
1282e8bc3c99SDan Carpenter 		if (copy_to_user(data + 1, &size, sizeof(u32)))
1283140adfbaSJeeja KP 			return -EFAULT;
1284e8bc3c99SDan Carpenter 		if (copy_to_user(data + 2, bc->params, size))
1285140adfbaSJeeja KP 			return -EFAULT;
1286140adfbaSJeeja KP 	}
1287140adfbaSJeeja KP 
1288140adfbaSJeeja KP 	return 0;
1289140adfbaSJeeja KP }
1290140adfbaSJeeja KP 
1291140adfbaSJeeja KP #define SKL_PARAM_VENDOR_ID 0xff
1292140adfbaSJeeja KP 
1293140adfbaSJeeja KP static int skl_tplg_tlv_control_set(struct snd_kcontrol *kcontrol,
1294140adfbaSJeeja KP 			const unsigned int __user *data, unsigned int size)
1295140adfbaSJeeja KP {
1296140adfbaSJeeja KP 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1297140adfbaSJeeja KP 	struct skl_module_cfg *mconfig = w->priv;
1298140adfbaSJeeja KP 	struct soc_bytes_ext *sb =
1299140adfbaSJeeja KP 			(struct soc_bytes_ext *)kcontrol->private_value;
1300140adfbaSJeeja KP 	struct skl_algo_data *ac = (struct skl_algo_data *)sb->dobj.private;
1301140adfbaSJeeja KP 	struct skl *skl = get_skl_ctx(w->dapm->dev);
1302140adfbaSJeeja KP 
1303140adfbaSJeeja KP 	if (ac->params) {
13040d682104SDharageswari R 		if (size > ac->max)
13050d682104SDharageswari R 			return -EINVAL;
13060d682104SDharageswari R 
13070d682104SDharageswari R 		ac->size = size;
1308140adfbaSJeeja KP 		/*
1309140adfbaSJeeja KP 		 * if the param_is is of type Vendor, firmware expects actual
1310140adfbaSJeeja KP 		 * parameter id and size from the control.
1311140adfbaSJeeja KP 		 */
1312140adfbaSJeeja KP 		if (ac->param_id == SKL_PARAM_VENDOR_ID) {
1313140adfbaSJeeja KP 			if (copy_from_user(ac->params, data, size))
1314140adfbaSJeeja KP 				return -EFAULT;
1315140adfbaSJeeja KP 		} else {
1316140adfbaSJeeja KP 			if (copy_from_user(ac->params,
131765b4bcb8SAlan 					   data + 2, size))
1318140adfbaSJeeja KP 				return -EFAULT;
1319140adfbaSJeeja KP 		}
1320140adfbaSJeeja KP 
1321140adfbaSJeeja KP 		if (w->power)
1322140adfbaSJeeja KP 			return skl_set_module_params(skl->skl_sst,
13230d682104SDharageswari R 						(u32 *)ac->params, ac->size,
1324140adfbaSJeeja KP 						ac->param_id, mconfig);
1325140adfbaSJeeja KP 	}
1326140adfbaSJeeja KP 
1327140adfbaSJeeja KP 	return 0;
1328140adfbaSJeeja KP }
1329140adfbaSJeeja KP 
13307a1b749bSDharageswari R static int skl_tplg_mic_control_get(struct snd_kcontrol *kcontrol,
13317a1b749bSDharageswari R 		struct snd_ctl_elem_value *ucontrol)
13327a1b749bSDharageswari R {
13337a1b749bSDharageswari R 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
13347a1b749bSDharageswari R 	struct skl_module_cfg *mconfig = w->priv;
13357a1b749bSDharageswari R 	struct soc_enum *ec = (struct soc_enum *)kcontrol->private_value;
13367a1b749bSDharageswari R 	u32 ch_type = *((u32 *)ec->dobj.private);
13377a1b749bSDharageswari R 
13387a1b749bSDharageswari R 	if (mconfig->dmic_ch_type == ch_type)
13397a1b749bSDharageswari R 		ucontrol->value.enumerated.item[0] =
13407a1b749bSDharageswari R 					mconfig->dmic_ch_combo_index;
13417a1b749bSDharageswari R 	else
13427a1b749bSDharageswari R 		ucontrol->value.enumerated.item[0] = 0;
13437a1b749bSDharageswari R 
13447a1b749bSDharageswari R 	return 0;
13457a1b749bSDharageswari R }
13467a1b749bSDharageswari R 
13477a1b749bSDharageswari R static int skl_fill_mic_sel_params(struct skl_module_cfg *mconfig,
13487a1b749bSDharageswari R 	struct skl_mic_sel_config *mic_cfg, struct device *dev)
13497a1b749bSDharageswari R {
13507a1b749bSDharageswari R 	struct skl_specific_cfg *sp_cfg = &mconfig->formats_config;
13517a1b749bSDharageswari R 
13527a1b749bSDharageswari R 	sp_cfg->caps_size = sizeof(struct skl_mic_sel_config);
13537a1b749bSDharageswari R 	sp_cfg->set_params = SKL_PARAM_SET;
13547a1b749bSDharageswari R 	sp_cfg->param_id = 0x00;
13557a1b749bSDharageswari R 	if (!sp_cfg->caps) {
13567a1b749bSDharageswari R 		sp_cfg->caps = devm_kzalloc(dev, sp_cfg->caps_size, GFP_KERNEL);
13577a1b749bSDharageswari R 		if (!sp_cfg->caps)
13587a1b749bSDharageswari R 			return -ENOMEM;
13597a1b749bSDharageswari R 	}
13607a1b749bSDharageswari R 
13617a1b749bSDharageswari R 	mic_cfg->mic_switch = SKL_MIC_SEL_SWITCH;
13627a1b749bSDharageswari R 	mic_cfg->flags = 0;
13637a1b749bSDharageswari R 	memcpy(sp_cfg->caps, mic_cfg, sp_cfg->caps_size);
13647a1b749bSDharageswari R 
13657a1b749bSDharageswari R 	return 0;
13667a1b749bSDharageswari R }
13677a1b749bSDharageswari R 
13687a1b749bSDharageswari R static int skl_tplg_mic_control_set(struct snd_kcontrol *kcontrol,
13697a1b749bSDharageswari R 			struct snd_ctl_elem_value *ucontrol)
13707a1b749bSDharageswari R {
13717a1b749bSDharageswari R 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
13727a1b749bSDharageswari R 	struct skl_module_cfg *mconfig = w->priv;
13737a1b749bSDharageswari R 	struct skl_mic_sel_config mic_cfg = {0};
13747a1b749bSDharageswari R 	struct soc_enum *ec = (struct soc_enum *)kcontrol->private_value;
13757a1b749bSDharageswari R 	u32 ch_type = *((u32 *)ec->dobj.private);
13767a1b749bSDharageswari R 	const int *list;
13777a1b749bSDharageswari R 	u8 in_ch, out_ch, index;
13787a1b749bSDharageswari R 
13797a1b749bSDharageswari R 	mconfig->dmic_ch_type = ch_type;
13807a1b749bSDharageswari R 	mconfig->dmic_ch_combo_index = ucontrol->value.enumerated.item[0];
13817a1b749bSDharageswari R 
13827a1b749bSDharageswari R 	/* enum control index 0 is INVALID, so no channels to be set */
13837a1b749bSDharageswari R 	if (mconfig->dmic_ch_combo_index == 0)
13847a1b749bSDharageswari R 		return 0;
13857a1b749bSDharageswari R 
13867a1b749bSDharageswari R 	/* No valid channel selection map for index 0, so offset by 1 */
13877a1b749bSDharageswari R 	index = mconfig->dmic_ch_combo_index - 1;
13887a1b749bSDharageswari R 
13897a1b749bSDharageswari R 	switch (ch_type) {
13907a1b749bSDharageswari R 	case SKL_CH_MONO:
13917a1b749bSDharageswari R 		if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_mono_list))
13927a1b749bSDharageswari R 			return -EINVAL;
13937a1b749bSDharageswari R 
13947a1b749bSDharageswari R 		list = &mic_mono_list[index];
13957a1b749bSDharageswari R 		break;
13967a1b749bSDharageswari R 
13977a1b749bSDharageswari R 	case SKL_CH_STEREO:
13987a1b749bSDharageswari R 		if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_stereo_list))
13997a1b749bSDharageswari R 			return -EINVAL;
14007a1b749bSDharageswari R 
14017a1b749bSDharageswari R 		list = mic_stereo_list[index];
14027a1b749bSDharageswari R 		break;
14037a1b749bSDharageswari R 
14047a1b749bSDharageswari R 	case SKL_CH_TRIO:
14057a1b749bSDharageswari R 		if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_trio_list))
14067a1b749bSDharageswari R 			return -EINVAL;
14077a1b749bSDharageswari R 
14087a1b749bSDharageswari R 		list = mic_trio_list[index];
14097a1b749bSDharageswari R 		break;
14107a1b749bSDharageswari R 
14117a1b749bSDharageswari R 	case SKL_CH_QUATRO:
14127a1b749bSDharageswari R 		if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_quatro_list))
14137a1b749bSDharageswari R 			return -EINVAL;
14147a1b749bSDharageswari R 
14157a1b749bSDharageswari R 		list = mic_quatro_list[index];
14167a1b749bSDharageswari R 		break;
14177a1b749bSDharageswari R 
14187a1b749bSDharageswari R 	default:
14197a1b749bSDharageswari R 		dev_err(w->dapm->dev,
14207a1b749bSDharageswari R 				"Invalid channel %d for mic_select module\n",
14217a1b749bSDharageswari R 				ch_type);
14227a1b749bSDharageswari R 		return -EINVAL;
14237a1b749bSDharageswari R 
14247a1b749bSDharageswari R 	}
14257a1b749bSDharageswari R 
14267a1b749bSDharageswari R 	/* channel type enum map to number of chanels for that type */
14277a1b749bSDharageswari R 	for (out_ch = 0; out_ch < ch_type; out_ch++) {
14287a1b749bSDharageswari R 		in_ch = list[out_ch];
14297a1b749bSDharageswari R 		mic_cfg.blob[out_ch][in_ch] = SKL_DEFAULT_MIC_SEL_GAIN;
14307a1b749bSDharageswari R 	}
14317a1b749bSDharageswari R 
14327a1b749bSDharageswari R 	return skl_fill_mic_sel_params(mconfig, &mic_cfg, w->dapm->dev);
14337a1b749bSDharageswari R }
14347a1b749bSDharageswari R 
1435cfb0a873SVinod Koul /*
14368871dcb9SJeeja KP  * Fill the dma id for host and link. In case of passthrough
14378871dcb9SJeeja KP  * pipeline, this will both host and link in the same
14388871dcb9SJeeja KP  * pipeline, so need to copy the link and host based on dev_type
14398871dcb9SJeeja KP  */
14408871dcb9SJeeja KP static void skl_tplg_fill_dma_id(struct skl_module_cfg *mcfg,
14418871dcb9SJeeja KP 				struct skl_pipe_params *params)
14428871dcb9SJeeja KP {
14438871dcb9SJeeja KP 	struct skl_pipe *pipe = mcfg->pipe;
14448871dcb9SJeeja KP 
14458871dcb9SJeeja KP 	if (pipe->passthru) {
14468871dcb9SJeeja KP 		switch (mcfg->dev_type) {
14478871dcb9SJeeja KP 		case SKL_DEVICE_HDALINK:
14488871dcb9SJeeja KP 			pipe->p_params->link_dma_id = params->link_dma_id;
144912c3be0eSJeeja KP 			pipe->p_params->link_index = params->link_index;
14507f975a38SJeeja KP 			pipe->p_params->link_bps = params->link_bps;
14518871dcb9SJeeja KP 			break;
14528871dcb9SJeeja KP 
14538871dcb9SJeeja KP 		case SKL_DEVICE_HDAHOST:
14548871dcb9SJeeja KP 			pipe->p_params->host_dma_id = params->host_dma_id;
14557f975a38SJeeja KP 			pipe->p_params->host_bps = params->host_bps;
14568871dcb9SJeeja KP 			break;
14578871dcb9SJeeja KP 
14588871dcb9SJeeja KP 		default:
14598871dcb9SJeeja KP 			break;
14608871dcb9SJeeja KP 		}
14618871dcb9SJeeja KP 		pipe->p_params->s_fmt = params->s_fmt;
14628871dcb9SJeeja KP 		pipe->p_params->ch = params->ch;
14638871dcb9SJeeja KP 		pipe->p_params->s_freq = params->s_freq;
14648871dcb9SJeeja KP 		pipe->p_params->stream = params->stream;
146512c3be0eSJeeja KP 		pipe->p_params->format = params->format;
14668871dcb9SJeeja KP 
14678871dcb9SJeeja KP 	} else {
14688871dcb9SJeeja KP 		memcpy(pipe->p_params, params, sizeof(*params));
14698871dcb9SJeeja KP 	}
14708871dcb9SJeeja KP }
14718871dcb9SJeeja KP 
14728871dcb9SJeeja KP /*
1473cfb0a873SVinod Koul  * The FE params are passed by hw_params of the DAI.
1474cfb0a873SVinod Koul  * On hw_params, the params are stored in Gateway module of the FE and we
1475cfb0a873SVinod Koul  * need to calculate the format in DSP module configuration, that
1476cfb0a873SVinod Koul  * conversion is done here
1477cfb0a873SVinod Koul  */
1478cfb0a873SVinod Koul int skl_tplg_update_pipe_params(struct device *dev,
1479cfb0a873SVinod Koul 			struct skl_module_cfg *mconfig,
1480cfb0a873SVinod Koul 			struct skl_pipe_params *params)
1481cfb0a873SVinod Koul {
1482cfb0a873SVinod Koul 	struct skl_module_fmt *format = NULL;
1483cfb0a873SVinod Koul 
14848871dcb9SJeeja KP 	skl_tplg_fill_dma_id(mconfig, params);
1485cfb0a873SVinod Koul 
1486cfb0a873SVinod Koul 	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK)
14874cd9899fSHardik T Shah 		format = &mconfig->in_fmt[0];
1488cfb0a873SVinod Koul 	else
14894cd9899fSHardik T Shah 		format = &mconfig->out_fmt[0];
1490cfb0a873SVinod Koul 
1491cfb0a873SVinod Koul 	/* set the hw_params */
1492cfb0a873SVinod Koul 	format->s_freq = params->s_freq;
1493cfb0a873SVinod Koul 	format->channels = params->ch;
1494cfb0a873SVinod Koul 	format->valid_bit_depth = skl_get_bit_depth(params->s_fmt);
1495cfb0a873SVinod Koul 
1496cfb0a873SVinod Koul 	/*
1497cfb0a873SVinod Koul 	 * 16 bit is 16 bit container whereas 24 bit is in 32 bit
1498cfb0a873SVinod Koul 	 * container so update bit depth accordingly
1499cfb0a873SVinod Koul 	 */
1500cfb0a873SVinod Koul 	switch (format->valid_bit_depth) {
1501cfb0a873SVinod Koul 	case SKL_DEPTH_16BIT:
1502cfb0a873SVinod Koul 		format->bit_depth = format->valid_bit_depth;
1503cfb0a873SVinod Koul 		break;
1504cfb0a873SVinod Koul 
1505cfb0a873SVinod Koul 	case SKL_DEPTH_24BIT:
15066654f39eSJeeja KP 	case SKL_DEPTH_32BIT:
1507cfb0a873SVinod Koul 		format->bit_depth = SKL_DEPTH_32BIT;
1508cfb0a873SVinod Koul 		break;
1509cfb0a873SVinod Koul 
1510cfb0a873SVinod Koul 	default:
1511cfb0a873SVinod Koul 		dev_err(dev, "Invalid bit depth %x for pipe\n",
1512cfb0a873SVinod Koul 				format->valid_bit_depth);
1513cfb0a873SVinod Koul 		return -EINVAL;
1514cfb0a873SVinod Koul 	}
1515cfb0a873SVinod Koul 
1516cfb0a873SVinod Koul 	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1517cfb0a873SVinod Koul 		mconfig->ibs = (format->s_freq / 1000) *
1518cfb0a873SVinod Koul 				(format->channels) *
1519cfb0a873SVinod Koul 				(format->bit_depth >> 3);
1520cfb0a873SVinod Koul 	} else {
1521cfb0a873SVinod Koul 		mconfig->obs = (format->s_freq / 1000) *
1522cfb0a873SVinod Koul 				(format->channels) *
1523cfb0a873SVinod Koul 				(format->bit_depth >> 3);
1524cfb0a873SVinod Koul 	}
1525cfb0a873SVinod Koul 
1526cfb0a873SVinod Koul 	return 0;
1527cfb0a873SVinod Koul }
1528cfb0a873SVinod Koul 
1529cfb0a873SVinod Koul /*
1530cfb0a873SVinod Koul  * Query the module config for the FE DAI
1531cfb0a873SVinod Koul  * This is used to find the hw_params set for that DAI and apply to FE
1532cfb0a873SVinod Koul  * pipeline
1533cfb0a873SVinod Koul  */
1534cfb0a873SVinod Koul struct skl_module_cfg *
1535cfb0a873SVinod Koul skl_tplg_fe_get_cpr_module(struct snd_soc_dai *dai, int stream)
1536cfb0a873SVinod Koul {
1537cfb0a873SVinod Koul 	struct snd_soc_dapm_widget *w;
1538cfb0a873SVinod Koul 	struct snd_soc_dapm_path *p = NULL;
1539cfb0a873SVinod Koul 
1540cfb0a873SVinod Koul 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1541cfb0a873SVinod Koul 		w = dai->playback_widget;
1542f0900eb2SSubhransu S. Prusty 		snd_soc_dapm_widget_for_each_sink_path(w, p) {
1543cfb0a873SVinod Koul 			if (p->connect && p->sink->power &&
1544a28f51dbSJeeja KP 					!is_skl_dsp_widget_type(p->sink))
1545cfb0a873SVinod Koul 				continue;
1546cfb0a873SVinod Koul 
1547cfb0a873SVinod Koul 			if (p->sink->priv) {
1548cfb0a873SVinod Koul 				dev_dbg(dai->dev, "set params for %s\n",
1549cfb0a873SVinod Koul 						p->sink->name);
1550cfb0a873SVinod Koul 				return p->sink->priv;
1551cfb0a873SVinod Koul 			}
1552cfb0a873SVinod Koul 		}
1553cfb0a873SVinod Koul 	} else {
1554cfb0a873SVinod Koul 		w = dai->capture_widget;
1555f0900eb2SSubhransu S. Prusty 		snd_soc_dapm_widget_for_each_source_path(w, p) {
1556cfb0a873SVinod Koul 			if (p->connect && p->source->power &&
1557a28f51dbSJeeja KP 					!is_skl_dsp_widget_type(p->source))
1558cfb0a873SVinod Koul 				continue;
1559cfb0a873SVinod Koul 
1560cfb0a873SVinod Koul 			if (p->source->priv) {
1561cfb0a873SVinod Koul 				dev_dbg(dai->dev, "set params for %s\n",
1562cfb0a873SVinod Koul 						p->source->name);
1563cfb0a873SVinod Koul 				return p->source->priv;
1564cfb0a873SVinod Koul 			}
1565cfb0a873SVinod Koul 		}
1566cfb0a873SVinod Koul 	}
1567cfb0a873SVinod Koul 
1568cfb0a873SVinod Koul 	return NULL;
1569cfb0a873SVinod Koul }
1570cfb0a873SVinod Koul 
1571718a42b5SDharageswari.R static struct skl_module_cfg *skl_get_mconfig_pb_cpr(
1572718a42b5SDharageswari.R 		struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
1573718a42b5SDharageswari.R {
1574718a42b5SDharageswari.R 	struct snd_soc_dapm_path *p;
1575718a42b5SDharageswari.R 	struct skl_module_cfg *mconfig = NULL;
1576718a42b5SDharageswari.R 
1577718a42b5SDharageswari.R 	snd_soc_dapm_widget_for_each_source_path(w, p) {
1578718a42b5SDharageswari.R 		if (w->endpoints[SND_SOC_DAPM_DIR_OUT] > 0) {
1579718a42b5SDharageswari.R 			if (p->connect &&
1580718a42b5SDharageswari.R 				    (p->sink->id == snd_soc_dapm_aif_out) &&
1581718a42b5SDharageswari.R 				    p->source->priv) {
1582718a42b5SDharageswari.R 				mconfig = p->source->priv;
1583718a42b5SDharageswari.R 				return mconfig;
1584718a42b5SDharageswari.R 			}
1585718a42b5SDharageswari.R 			mconfig = skl_get_mconfig_pb_cpr(dai, p->source);
1586718a42b5SDharageswari.R 			if (mconfig)
1587718a42b5SDharageswari.R 				return mconfig;
1588718a42b5SDharageswari.R 		}
1589718a42b5SDharageswari.R 	}
1590718a42b5SDharageswari.R 	return mconfig;
1591718a42b5SDharageswari.R }
1592718a42b5SDharageswari.R 
1593718a42b5SDharageswari.R static struct skl_module_cfg *skl_get_mconfig_cap_cpr(
1594718a42b5SDharageswari.R 		struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
1595718a42b5SDharageswari.R {
1596718a42b5SDharageswari.R 	struct snd_soc_dapm_path *p;
1597718a42b5SDharageswari.R 	struct skl_module_cfg *mconfig = NULL;
1598718a42b5SDharageswari.R 
1599718a42b5SDharageswari.R 	snd_soc_dapm_widget_for_each_sink_path(w, p) {
1600718a42b5SDharageswari.R 		if (w->endpoints[SND_SOC_DAPM_DIR_IN] > 0) {
1601718a42b5SDharageswari.R 			if (p->connect &&
1602718a42b5SDharageswari.R 				    (p->source->id == snd_soc_dapm_aif_in) &&
1603718a42b5SDharageswari.R 				    p->sink->priv) {
1604718a42b5SDharageswari.R 				mconfig = p->sink->priv;
1605718a42b5SDharageswari.R 				return mconfig;
1606718a42b5SDharageswari.R 			}
1607718a42b5SDharageswari.R 			mconfig = skl_get_mconfig_cap_cpr(dai, p->sink);
1608718a42b5SDharageswari.R 			if (mconfig)
1609718a42b5SDharageswari.R 				return mconfig;
1610718a42b5SDharageswari.R 		}
1611718a42b5SDharageswari.R 	}
1612718a42b5SDharageswari.R 	return mconfig;
1613718a42b5SDharageswari.R }
1614718a42b5SDharageswari.R 
1615718a42b5SDharageswari.R struct skl_module_cfg *
1616718a42b5SDharageswari.R skl_tplg_be_get_cpr_module(struct snd_soc_dai *dai, int stream)
1617718a42b5SDharageswari.R {
1618718a42b5SDharageswari.R 	struct snd_soc_dapm_widget *w;
1619718a42b5SDharageswari.R 	struct skl_module_cfg *mconfig;
1620718a42b5SDharageswari.R 
1621718a42b5SDharageswari.R 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1622718a42b5SDharageswari.R 		w = dai->playback_widget;
1623718a42b5SDharageswari.R 		mconfig = skl_get_mconfig_pb_cpr(dai, w);
1624718a42b5SDharageswari.R 	} else {
1625718a42b5SDharageswari.R 		w = dai->capture_widget;
1626718a42b5SDharageswari.R 		mconfig = skl_get_mconfig_cap_cpr(dai, w);
1627718a42b5SDharageswari.R 	}
1628718a42b5SDharageswari.R 	return mconfig;
1629718a42b5SDharageswari.R }
1630718a42b5SDharageswari.R 
1631cfb0a873SVinod Koul static u8 skl_tplg_be_link_type(int dev_type)
1632cfb0a873SVinod Koul {
1633cfb0a873SVinod Koul 	int ret;
1634cfb0a873SVinod Koul 
1635cfb0a873SVinod Koul 	switch (dev_type) {
1636cfb0a873SVinod Koul 	case SKL_DEVICE_BT:
1637cfb0a873SVinod Koul 		ret = NHLT_LINK_SSP;
1638cfb0a873SVinod Koul 		break;
1639cfb0a873SVinod Koul 
1640cfb0a873SVinod Koul 	case SKL_DEVICE_DMIC:
1641cfb0a873SVinod Koul 		ret = NHLT_LINK_DMIC;
1642cfb0a873SVinod Koul 		break;
1643cfb0a873SVinod Koul 
1644cfb0a873SVinod Koul 	case SKL_DEVICE_I2S:
1645cfb0a873SVinod Koul 		ret = NHLT_LINK_SSP;
1646cfb0a873SVinod Koul 		break;
1647cfb0a873SVinod Koul 
1648cfb0a873SVinod Koul 	case SKL_DEVICE_HDALINK:
1649cfb0a873SVinod Koul 		ret = NHLT_LINK_HDA;
1650cfb0a873SVinod Koul 		break;
1651cfb0a873SVinod Koul 
1652cfb0a873SVinod Koul 	default:
1653cfb0a873SVinod Koul 		ret = NHLT_LINK_INVALID;
1654cfb0a873SVinod Koul 		break;
1655cfb0a873SVinod Koul 	}
1656cfb0a873SVinod Koul 
1657cfb0a873SVinod Koul 	return ret;
1658cfb0a873SVinod Koul }
1659cfb0a873SVinod Koul 
1660cfb0a873SVinod Koul /*
1661cfb0a873SVinod Koul  * Fill the BE gateway parameters
1662cfb0a873SVinod Koul  * The BE gateway expects a blob of parameters which are kept in the ACPI
1663cfb0a873SVinod Koul  * NHLT blob, so query the blob for interface type (i2s/pdm) and instance.
1664cfb0a873SVinod Koul  * The port can have multiple settings so pick based on the PCM
1665cfb0a873SVinod Koul  * parameters
1666cfb0a873SVinod Koul  */
1667cfb0a873SVinod Koul static int skl_tplg_be_fill_pipe_params(struct snd_soc_dai *dai,
1668cfb0a873SVinod Koul 				struct skl_module_cfg *mconfig,
1669cfb0a873SVinod Koul 				struct skl_pipe_params *params)
1670cfb0a873SVinod Koul {
1671cfb0a873SVinod Koul 	struct nhlt_specific_cfg *cfg;
1672cfb0a873SVinod Koul 	struct skl *skl = get_skl_ctx(dai->dev);
1673cfb0a873SVinod Koul 	int link_type = skl_tplg_be_link_type(mconfig->dev_type);
1674db2f586bSSenthilnathan Veppur 	u8 dev_type = skl_tplg_be_dev_type(mconfig->dev_type);
1675cfb0a873SVinod Koul 
16768871dcb9SJeeja KP 	skl_tplg_fill_dma_id(mconfig, params);
1677cfb0a873SVinod Koul 
1678b30c275eSJeeja KP 	if (link_type == NHLT_LINK_HDA)
1679b30c275eSJeeja KP 		return 0;
1680b30c275eSJeeja KP 
1681cfb0a873SVinod Koul 	/* update the blob based on virtual bus_id*/
1682cfb0a873SVinod Koul 	cfg = skl_get_ep_blob(skl, mconfig->vbus_id, link_type,
1683cfb0a873SVinod Koul 					params->s_fmt, params->ch,
1684db2f586bSSenthilnathan Veppur 					params->s_freq, params->stream,
1685db2f586bSSenthilnathan Veppur 					dev_type);
1686cfb0a873SVinod Koul 	if (cfg) {
1687cfb0a873SVinod Koul 		mconfig->formats_config.caps_size = cfg->size;
1688bc03281aSJeeja KP 		mconfig->formats_config.caps = (u32 *) &cfg->caps;
1689cfb0a873SVinod Koul 	} else {
1690cfb0a873SVinod Koul 		dev_err(dai->dev, "Blob NULL for id %x type %d dirn %d\n",
1691cfb0a873SVinod Koul 					mconfig->vbus_id, link_type,
1692cfb0a873SVinod Koul 					params->stream);
1693cfb0a873SVinod Koul 		dev_err(dai->dev, "PCM: ch %d, freq %d, fmt %d\n",
1694cfb0a873SVinod Koul 				 params->ch, params->s_freq, params->s_fmt);
1695cfb0a873SVinod Koul 		return -EINVAL;
1696cfb0a873SVinod Koul 	}
1697cfb0a873SVinod Koul 
1698cfb0a873SVinod Koul 	return 0;
1699cfb0a873SVinod Koul }
1700cfb0a873SVinod Koul 
1701cfb0a873SVinod Koul static int skl_tplg_be_set_src_pipe_params(struct snd_soc_dai *dai,
1702cfb0a873SVinod Koul 				struct snd_soc_dapm_widget *w,
1703cfb0a873SVinod Koul 				struct skl_pipe_params *params)
1704cfb0a873SVinod Koul {
1705cfb0a873SVinod Koul 	struct snd_soc_dapm_path *p;
17064d8adccbSSubhransu S. Prusty 	int ret = -EIO;
1707cfb0a873SVinod Koul 
1708f0900eb2SSubhransu S. Prusty 	snd_soc_dapm_widget_for_each_source_path(w, p) {
1709cfb0a873SVinod Koul 		if (p->connect && is_skl_dsp_widget_type(p->source) &&
1710cfb0a873SVinod Koul 						p->source->priv) {
1711cfb0a873SVinod Koul 
17129a03cb49SJeeja KP 			ret = skl_tplg_be_fill_pipe_params(dai,
17139a03cb49SJeeja KP 						p->source->priv, params);
17144d8adccbSSubhransu S. Prusty 			if (ret < 0)
17154d8adccbSSubhransu S. Prusty 				return ret;
1716cfb0a873SVinod Koul 		} else {
17179a03cb49SJeeja KP 			ret = skl_tplg_be_set_src_pipe_params(dai,
17189a03cb49SJeeja KP 						p->source, params);
17194d8adccbSSubhransu S. Prusty 			if (ret < 0)
17204d8adccbSSubhransu S. Prusty 				return ret;
1721cfb0a873SVinod Koul 		}
1722cfb0a873SVinod Koul 	}
1723cfb0a873SVinod Koul 
17244d8adccbSSubhransu S. Prusty 	return ret;
1725cfb0a873SVinod Koul }
1726cfb0a873SVinod Koul 
1727cfb0a873SVinod Koul static int skl_tplg_be_set_sink_pipe_params(struct snd_soc_dai *dai,
1728cfb0a873SVinod Koul 	struct snd_soc_dapm_widget *w, struct skl_pipe_params *params)
1729cfb0a873SVinod Koul {
1730cfb0a873SVinod Koul 	struct snd_soc_dapm_path *p = NULL;
17314d8adccbSSubhransu S. Prusty 	int ret = -EIO;
1732cfb0a873SVinod Koul 
1733f0900eb2SSubhransu S. Prusty 	snd_soc_dapm_widget_for_each_sink_path(w, p) {
1734cfb0a873SVinod Koul 		if (p->connect && is_skl_dsp_widget_type(p->sink) &&
1735cfb0a873SVinod Koul 						p->sink->priv) {
1736cfb0a873SVinod Koul 
17379a03cb49SJeeja KP 			ret = skl_tplg_be_fill_pipe_params(dai,
17389a03cb49SJeeja KP 						p->sink->priv, params);
17394d8adccbSSubhransu S. Prusty 			if (ret < 0)
17404d8adccbSSubhransu S. Prusty 				return ret;
17414d8adccbSSubhransu S. Prusty 		} else {
17424d8adccbSSubhransu S. Prusty 			ret = skl_tplg_be_set_sink_pipe_params(
1743cfb0a873SVinod Koul 						dai, p->sink, params);
17444d8adccbSSubhransu S. Prusty 			if (ret < 0)
17454d8adccbSSubhransu S. Prusty 				return ret;
1746cfb0a873SVinod Koul 		}
1747cfb0a873SVinod Koul 	}
1748cfb0a873SVinod Koul 
17494d8adccbSSubhransu S. Prusty 	return ret;
1750cfb0a873SVinod Koul }
1751cfb0a873SVinod Koul 
1752cfb0a873SVinod Koul /*
1753cfb0a873SVinod Koul  * BE hw_params can be a source parameters (capture) or sink parameters
1754cfb0a873SVinod Koul  * (playback). Based on sink and source we need to either find the source
1755cfb0a873SVinod Koul  * list or the sink list and set the pipeline parameters
1756cfb0a873SVinod Koul  */
1757cfb0a873SVinod Koul int skl_tplg_be_update_params(struct snd_soc_dai *dai,
1758cfb0a873SVinod Koul 				struct skl_pipe_params *params)
1759cfb0a873SVinod Koul {
1760cfb0a873SVinod Koul 	struct snd_soc_dapm_widget *w;
1761cfb0a873SVinod Koul 
1762cfb0a873SVinod Koul 	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1763cfb0a873SVinod Koul 		w = dai->playback_widget;
1764cfb0a873SVinod Koul 
1765cfb0a873SVinod Koul 		return skl_tplg_be_set_src_pipe_params(dai, w, params);
1766cfb0a873SVinod Koul 
1767cfb0a873SVinod Koul 	} else {
1768cfb0a873SVinod Koul 		w = dai->capture_widget;
1769cfb0a873SVinod Koul 
1770cfb0a873SVinod Koul 		return skl_tplg_be_set_sink_pipe_params(dai, w, params);
1771cfb0a873SVinod Koul 	}
1772cfb0a873SVinod Koul 
1773cfb0a873SVinod Koul 	return 0;
1774cfb0a873SVinod Koul }
17753af36706SVinod Koul 
17763af36706SVinod Koul static const struct snd_soc_tplg_widget_events skl_tplg_widget_ops[] = {
17773af36706SVinod Koul 	{SKL_MIXER_EVENT, skl_tplg_mixer_event},
17789a1e3507SVinod Koul 	{SKL_VMIXER_EVENT, skl_tplg_mixer_event},
17793af36706SVinod Koul 	{SKL_PGA_EVENT, skl_tplg_pga_event},
17803af36706SVinod Koul };
17813af36706SVinod Koul 
1782140adfbaSJeeja KP static const struct snd_soc_tplg_bytes_ext_ops skl_tlv_ops[] = {
1783140adfbaSJeeja KP 	{SKL_CONTROL_TYPE_BYTE_TLV, skl_tplg_tlv_control_get,
1784140adfbaSJeeja KP 					skl_tplg_tlv_control_set},
1785140adfbaSJeeja KP };
1786140adfbaSJeeja KP 
17877a1b749bSDharageswari R static const struct snd_soc_tplg_kcontrol_ops skl_tplg_kcontrol_ops[] = {
17887a1b749bSDharageswari R 	{
17897a1b749bSDharageswari R 		.id = SKL_CONTROL_TYPE_MIC_SELECT,
17907a1b749bSDharageswari R 		.get = skl_tplg_mic_control_get,
17917a1b749bSDharageswari R 		.put = skl_tplg_mic_control_set,
17927a1b749bSDharageswari R 	},
17937a1b749bSDharageswari R };
17947a1b749bSDharageswari R 
17956277e832SShreyas NC static int skl_tplg_fill_pipe_tkn(struct device *dev,
17966277e832SShreyas NC 			struct skl_pipe *pipe, u32 tkn,
17976277e832SShreyas NC 			u32 tkn_val)
17983af36706SVinod Koul {
17993af36706SVinod Koul 
18006277e832SShreyas NC 	switch (tkn) {
18016277e832SShreyas NC 	case SKL_TKN_U32_PIPE_CONN_TYPE:
18026277e832SShreyas NC 		pipe->conn_type = tkn_val;
18036277e832SShreyas NC 		break;
18046277e832SShreyas NC 
18056277e832SShreyas NC 	case SKL_TKN_U32_PIPE_PRIORITY:
18066277e832SShreyas NC 		pipe->pipe_priority = tkn_val;
18076277e832SShreyas NC 		break;
18086277e832SShreyas NC 
18096277e832SShreyas NC 	case SKL_TKN_U32_PIPE_MEM_PGS:
18106277e832SShreyas NC 		pipe->memory_pages = tkn_val;
18116277e832SShreyas NC 		break;
18126277e832SShreyas NC 
18138a0cb236SVinod Koul 	case SKL_TKN_U32_PMODE:
18148a0cb236SVinod Koul 		pipe->lp_mode = tkn_val;
18158a0cb236SVinod Koul 		break;
18168a0cb236SVinod Koul 
18176277e832SShreyas NC 	default:
18186277e832SShreyas NC 		dev_err(dev, "Token not handled %d\n", tkn);
18196277e832SShreyas NC 		return -EINVAL;
18203af36706SVinod Koul 	}
18216277e832SShreyas NC 
18226277e832SShreyas NC 	return 0;
18233af36706SVinod Koul }
18243af36706SVinod Koul 
18253af36706SVinod Koul /*
18266277e832SShreyas NC  * Add pipeline by parsing the relevant tokens
18276277e832SShreyas NC  * Return an existing pipe if the pipe already exists.
18283af36706SVinod Koul  */
18296277e832SShreyas NC static int skl_tplg_add_pipe(struct device *dev,
18306277e832SShreyas NC 		struct skl_module_cfg *mconfig, struct skl *skl,
18316277e832SShreyas NC 		struct snd_soc_tplg_vendor_value_elem *tkn_elem)
18323af36706SVinod Koul {
18333af36706SVinod Koul 	struct skl_pipeline *ppl;
18343af36706SVinod Koul 	struct skl_pipe *pipe;
18353af36706SVinod Koul 	struct skl_pipe_params *params;
18363af36706SVinod Koul 
18373af36706SVinod Koul 	list_for_each_entry(ppl, &skl->ppl_list, node) {
18386277e832SShreyas NC 		if (ppl->pipe->ppl_id == tkn_elem->value) {
18396277e832SShreyas NC 			mconfig->pipe = ppl->pipe;
1840081dc8abSGuneshwor Singh 			return -EEXIST;
18416277e832SShreyas NC 		}
18423af36706SVinod Koul 	}
18433af36706SVinod Koul 
18443af36706SVinod Koul 	ppl = devm_kzalloc(dev, sizeof(*ppl), GFP_KERNEL);
18453af36706SVinod Koul 	if (!ppl)
18466277e832SShreyas NC 		return -ENOMEM;
18473af36706SVinod Koul 
18483af36706SVinod Koul 	pipe = devm_kzalloc(dev, sizeof(*pipe), GFP_KERNEL);
18493af36706SVinod Koul 	if (!pipe)
18506277e832SShreyas NC 		return -ENOMEM;
18513af36706SVinod Koul 
18523af36706SVinod Koul 	params = devm_kzalloc(dev, sizeof(*params), GFP_KERNEL);
18533af36706SVinod Koul 	if (!params)
18546277e832SShreyas NC 		return -ENOMEM;
18553af36706SVinod Koul 
18563af36706SVinod Koul 	pipe->p_params = params;
18576277e832SShreyas NC 	pipe->ppl_id = tkn_elem->value;
18583af36706SVinod Koul 	INIT_LIST_HEAD(&pipe->w_list);
18593af36706SVinod Koul 
18603af36706SVinod Koul 	ppl->pipe = pipe;
18613af36706SVinod Koul 	list_add(&ppl->node, &skl->ppl_list);
18623af36706SVinod Koul 
18636277e832SShreyas NC 	mconfig->pipe = pipe;
18646277e832SShreyas NC 	mconfig->pipe->state = SKL_PIPE_INVALID;
18656277e832SShreyas NC 
18666277e832SShreyas NC 	return 0;
18673af36706SVinod Koul }
18683af36706SVinod Koul 
18696277e832SShreyas NC static int skl_tplg_fill_pin(struct device *dev, u32 tkn,
18706277e832SShreyas NC 			struct skl_module_pin *m_pin,
18716277e832SShreyas NC 			int pin_index, u32 value)
18726277e832SShreyas NC {
18736277e832SShreyas NC 	switch (tkn) {
18746277e832SShreyas NC 	case SKL_TKN_U32_PIN_MOD_ID:
18756277e832SShreyas NC 		m_pin[pin_index].id.module_id = value;
18766277e832SShreyas NC 		break;
18776277e832SShreyas NC 
18786277e832SShreyas NC 	case SKL_TKN_U32_PIN_INST_ID:
18796277e832SShreyas NC 		m_pin[pin_index].id.instance_id = value;
18806277e832SShreyas NC 		break;
18816277e832SShreyas NC 
18826277e832SShreyas NC 	default:
18836277e832SShreyas NC 		dev_err(dev, "%d Not a pin token\n", value);
18846277e832SShreyas NC 		return -EINVAL;
18856277e832SShreyas NC 	}
18866277e832SShreyas NC 
18876277e832SShreyas NC 	return 0;
18886277e832SShreyas NC }
18896277e832SShreyas NC 
18906277e832SShreyas NC /*
18916277e832SShreyas NC  * Parse for pin config specific tokens to fill up the
18926277e832SShreyas NC  * module private data
18936277e832SShreyas NC  */
18946277e832SShreyas NC static int skl_tplg_fill_pins_info(struct device *dev,
18956277e832SShreyas NC 		struct skl_module_cfg *mconfig,
18966277e832SShreyas NC 		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
18976277e832SShreyas NC 		int dir, int pin_count)
18986277e832SShreyas NC {
18996277e832SShreyas NC 	int ret;
19006277e832SShreyas NC 	struct skl_module_pin *m_pin;
19016277e832SShreyas NC 
19026277e832SShreyas NC 	switch (dir) {
19036277e832SShreyas NC 	case SKL_DIR_IN:
19046277e832SShreyas NC 		m_pin = mconfig->m_in_pin;
19056277e832SShreyas NC 		break;
19066277e832SShreyas NC 
19076277e832SShreyas NC 	case SKL_DIR_OUT:
19086277e832SShreyas NC 		m_pin = mconfig->m_out_pin;
19096277e832SShreyas NC 		break;
19106277e832SShreyas NC 
19116277e832SShreyas NC 	default:
1912ecd286a9SColin Ian King 		dev_err(dev, "Invalid direction value\n");
19136277e832SShreyas NC 		return -EINVAL;
19146277e832SShreyas NC 	}
19156277e832SShreyas NC 
19166277e832SShreyas NC 	ret = skl_tplg_fill_pin(dev, tkn_elem->token,
19176277e832SShreyas NC 			m_pin, pin_count, tkn_elem->value);
19186277e832SShreyas NC 
19196277e832SShreyas NC 	if (ret < 0)
19206277e832SShreyas NC 		return ret;
19216277e832SShreyas NC 
19226277e832SShreyas NC 	m_pin[pin_count].in_use = false;
19236277e832SShreyas NC 	m_pin[pin_count].pin_state = SKL_PIN_UNBIND;
19246277e832SShreyas NC 
19256277e832SShreyas NC 	return 0;
19266277e832SShreyas NC }
19276277e832SShreyas NC 
19286277e832SShreyas NC /*
19296277e832SShreyas NC  * Fill up input/output module config format based
19306277e832SShreyas NC  * on the direction
19316277e832SShreyas NC  */
19326277e832SShreyas NC static int skl_tplg_fill_fmt(struct device *dev,
19336277e832SShreyas NC 		struct skl_module_cfg *mconfig,	u32 tkn,
19346277e832SShreyas NC 		u32 value, u32 dir, u32 pin_count)
19356277e832SShreyas NC {
19366277e832SShreyas NC 	struct skl_module_fmt *dst_fmt;
19376277e832SShreyas NC 
19386277e832SShreyas NC 	switch (dir) {
19396277e832SShreyas NC 	case SKL_DIR_IN:
19406277e832SShreyas NC 		dst_fmt = mconfig->in_fmt;
19416277e832SShreyas NC 		dst_fmt += pin_count;
19426277e832SShreyas NC 		break;
19436277e832SShreyas NC 
19446277e832SShreyas NC 	case SKL_DIR_OUT:
19456277e832SShreyas NC 		dst_fmt = mconfig->out_fmt;
19466277e832SShreyas NC 		dst_fmt += pin_count;
19476277e832SShreyas NC 		break;
19486277e832SShreyas NC 
19496277e832SShreyas NC 	default:
1950ecd286a9SColin Ian King 		dev_err(dev, "Invalid direction value\n");
19516277e832SShreyas NC 		return -EINVAL;
19526277e832SShreyas NC 	}
19536277e832SShreyas NC 
19546277e832SShreyas NC 	switch (tkn) {
19556277e832SShreyas NC 	case SKL_TKN_U32_FMT_CH:
19566277e832SShreyas NC 		dst_fmt->channels  = value;
19576277e832SShreyas NC 		break;
19586277e832SShreyas NC 
19596277e832SShreyas NC 	case SKL_TKN_U32_FMT_FREQ:
19606277e832SShreyas NC 		dst_fmt->s_freq = value;
19616277e832SShreyas NC 		break;
19626277e832SShreyas NC 
19636277e832SShreyas NC 	case SKL_TKN_U32_FMT_BIT_DEPTH:
19646277e832SShreyas NC 		dst_fmt->bit_depth = value;
19656277e832SShreyas NC 		break;
19666277e832SShreyas NC 
19676277e832SShreyas NC 	case SKL_TKN_U32_FMT_SAMPLE_SIZE:
19686277e832SShreyas NC 		dst_fmt->valid_bit_depth = value;
19696277e832SShreyas NC 		break;
19706277e832SShreyas NC 
19716277e832SShreyas NC 	case SKL_TKN_U32_FMT_CH_CONFIG:
19726277e832SShreyas NC 		dst_fmt->ch_cfg = value;
19736277e832SShreyas NC 		break;
19746277e832SShreyas NC 
19756277e832SShreyas NC 	case SKL_TKN_U32_FMT_INTERLEAVE:
19766277e832SShreyas NC 		dst_fmt->interleaving_style = value;
19776277e832SShreyas NC 		break;
19786277e832SShreyas NC 
19796277e832SShreyas NC 	case SKL_TKN_U32_FMT_SAMPLE_TYPE:
19806277e832SShreyas NC 		dst_fmt->sample_type = value;
19816277e832SShreyas NC 		break;
19826277e832SShreyas NC 
19836277e832SShreyas NC 	case SKL_TKN_U32_FMT_CH_MAP:
19846277e832SShreyas NC 		dst_fmt->ch_map = value;
19856277e832SShreyas NC 		break;
19866277e832SShreyas NC 
19876277e832SShreyas NC 	default:
1988ecd286a9SColin Ian King 		dev_err(dev, "Invalid token %d\n", tkn);
19896277e832SShreyas NC 		return -EINVAL;
19906277e832SShreyas NC 	}
19916277e832SShreyas NC 
19926277e832SShreyas NC 	return 0;
19936277e832SShreyas NC }
19946277e832SShreyas NC 
19956277e832SShreyas NC static int skl_tplg_get_uuid(struct device *dev, struct skl_module_cfg *mconfig,
19966277e832SShreyas NC 	      struct snd_soc_tplg_vendor_uuid_elem *uuid_tkn)
19976277e832SShreyas NC {
19986277e832SShreyas NC 	if (uuid_tkn->token == SKL_TKN_UUID)
19996277e832SShreyas NC 		memcpy(&mconfig->guid, &uuid_tkn->uuid, 16);
20006277e832SShreyas NC 	else {
2001ecd286a9SColin Ian King 		dev_err(dev, "Not an UUID token tkn %d\n", uuid_tkn->token);
20026277e832SShreyas NC 		return -EINVAL;
20036277e832SShreyas NC 	}
20046277e832SShreyas NC 
20056277e832SShreyas NC 	return 0;
20066277e832SShreyas NC }
20076277e832SShreyas NC 
20086277e832SShreyas NC static void skl_tplg_fill_pin_dynamic_val(
20096277e832SShreyas NC 		struct skl_module_pin *mpin, u32 pin_count, u32 value)
20104cd9899fSHardik T Shah {
20114cd9899fSHardik T Shah 	int i;
20124cd9899fSHardik T Shah 
20136277e832SShreyas NC 	for (i = 0; i < pin_count; i++)
20146277e832SShreyas NC 		mpin[i].is_dynamic = value;
20154cd9899fSHardik T Shah }
20166277e832SShreyas NC 
20176277e832SShreyas NC /*
20186277e832SShreyas NC  * Parse tokens to fill up the module private data
20196277e832SShreyas NC  */
20206277e832SShreyas NC static int skl_tplg_get_token(struct device *dev,
20216277e832SShreyas NC 		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
20226277e832SShreyas NC 		struct skl *skl, struct skl_module_cfg *mconfig)
20236277e832SShreyas NC {
20246277e832SShreyas NC 	int tkn_count = 0;
20256277e832SShreyas NC 	int ret;
20266277e832SShreyas NC 	static int is_pipe_exists;
20276277e832SShreyas NC 	static int pin_index, dir;
20286277e832SShreyas NC 
20296277e832SShreyas NC 	if (tkn_elem->token > SKL_TKN_MAX)
20306277e832SShreyas NC 		return -EINVAL;
20316277e832SShreyas NC 
20326277e832SShreyas NC 	switch (tkn_elem->token) {
20336277e832SShreyas NC 	case SKL_TKN_U8_IN_QUEUE_COUNT:
20346277e832SShreyas NC 		mconfig->max_in_queue = tkn_elem->value;
20356277e832SShreyas NC 		mconfig->m_in_pin = devm_kzalloc(dev, mconfig->max_in_queue *
20366277e832SShreyas NC 					sizeof(*mconfig->m_in_pin),
20376277e832SShreyas NC 					GFP_KERNEL);
20386277e832SShreyas NC 		if (!mconfig->m_in_pin)
20396277e832SShreyas NC 			return -ENOMEM;
20406277e832SShreyas NC 
20416277e832SShreyas NC 		break;
20426277e832SShreyas NC 
20436277e832SShreyas NC 	case SKL_TKN_U8_OUT_QUEUE_COUNT:
20446277e832SShreyas NC 		mconfig->max_out_queue = tkn_elem->value;
20456277e832SShreyas NC 		mconfig->m_out_pin = devm_kzalloc(dev, mconfig->max_out_queue *
20466277e832SShreyas NC 					sizeof(*mconfig->m_out_pin),
20476277e832SShreyas NC 					GFP_KERNEL);
20486277e832SShreyas NC 
20496277e832SShreyas NC 		if (!mconfig->m_out_pin)
20506277e832SShreyas NC 			return -ENOMEM;
20516277e832SShreyas NC 
20526277e832SShreyas NC 		break;
20536277e832SShreyas NC 
20546277e832SShreyas NC 	case SKL_TKN_U8_DYN_IN_PIN:
20556277e832SShreyas NC 		if (!mconfig->m_in_pin)
20566277e832SShreyas NC 			return -ENOMEM;
20576277e832SShreyas NC 
20586277e832SShreyas NC 		skl_tplg_fill_pin_dynamic_val(mconfig->m_in_pin,
20596277e832SShreyas NC 			mconfig->max_in_queue, tkn_elem->value);
20606277e832SShreyas NC 
20616277e832SShreyas NC 		break;
20626277e832SShreyas NC 
20636277e832SShreyas NC 	case SKL_TKN_U8_DYN_OUT_PIN:
20646277e832SShreyas NC 		if (!mconfig->m_out_pin)
20656277e832SShreyas NC 			return -ENOMEM;
20666277e832SShreyas NC 
20676277e832SShreyas NC 		skl_tplg_fill_pin_dynamic_val(mconfig->m_out_pin,
20686277e832SShreyas NC 			mconfig->max_out_queue, tkn_elem->value);
20696277e832SShreyas NC 
20706277e832SShreyas NC 		break;
20716277e832SShreyas NC 
20726277e832SShreyas NC 	case SKL_TKN_U8_TIME_SLOT:
20736277e832SShreyas NC 		mconfig->time_slot = tkn_elem->value;
20746277e832SShreyas NC 		break;
20756277e832SShreyas NC 
20766277e832SShreyas NC 	case SKL_TKN_U8_CORE_ID:
20776277e832SShreyas NC 		mconfig->core_id = tkn_elem->value;
20786277e832SShreyas NC 
20796277e832SShreyas NC 	case SKL_TKN_U8_MOD_TYPE:
20806277e832SShreyas NC 		mconfig->m_type = tkn_elem->value;
20816277e832SShreyas NC 		break;
20826277e832SShreyas NC 
20836277e832SShreyas NC 	case SKL_TKN_U8_DEV_TYPE:
20846277e832SShreyas NC 		mconfig->dev_type = tkn_elem->value;
20856277e832SShreyas NC 		break;
20866277e832SShreyas NC 
20876277e832SShreyas NC 	case SKL_TKN_U8_HW_CONN_TYPE:
20886277e832SShreyas NC 		mconfig->hw_conn_type = tkn_elem->value;
20896277e832SShreyas NC 		break;
20906277e832SShreyas NC 
20916277e832SShreyas NC 	case SKL_TKN_U16_MOD_INST_ID:
20926277e832SShreyas NC 		mconfig->id.instance_id =
20936277e832SShreyas NC 		tkn_elem->value;
20946277e832SShreyas NC 		break;
20956277e832SShreyas NC 
20966277e832SShreyas NC 	case SKL_TKN_U32_MEM_PAGES:
20976277e832SShreyas NC 		mconfig->mem_pages = tkn_elem->value;
20986277e832SShreyas NC 		break;
20996277e832SShreyas NC 
21006277e832SShreyas NC 	case SKL_TKN_U32_MAX_MCPS:
21016277e832SShreyas NC 		mconfig->mcps = tkn_elem->value;
21026277e832SShreyas NC 		break;
21036277e832SShreyas NC 
21046277e832SShreyas NC 	case SKL_TKN_U32_OBS:
21056277e832SShreyas NC 		mconfig->obs = tkn_elem->value;
21066277e832SShreyas NC 		break;
21076277e832SShreyas NC 
21086277e832SShreyas NC 	case SKL_TKN_U32_IBS:
21096277e832SShreyas NC 		mconfig->ibs = tkn_elem->value;
21106277e832SShreyas NC 		break;
21116277e832SShreyas NC 
21126277e832SShreyas NC 	case SKL_TKN_U32_VBUS_ID:
21136277e832SShreyas NC 		mconfig->vbus_id = tkn_elem->value;
21146277e832SShreyas NC 		break;
21156277e832SShreyas NC 
21166277e832SShreyas NC 	case SKL_TKN_U32_PARAMS_FIXUP:
21176277e832SShreyas NC 		mconfig->params_fixup = tkn_elem->value;
21186277e832SShreyas NC 		break;
21196277e832SShreyas NC 
21206277e832SShreyas NC 	case SKL_TKN_U32_CONVERTER:
21216277e832SShreyas NC 		mconfig->converter = tkn_elem->value;
21226277e832SShreyas NC 		break;
21236277e832SShreyas NC 
2124c0116be3SSubhransu S. Prusty 	case SKL_TKN_U32_D0I3_CAPS:
21256bd9dcf3SVinod Koul 		mconfig->d0i3_caps = tkn_elem->value;
21266bd9dcf3SVinod Koul 		break;
21276bd9dcf3SVinod Koul 
21286277e832SShreyas NC 	case SKL_TKN_U32_PIPE_ID:
21296277e832SShreyas NC 		ret = skl_tplg_add_pipe(dev,
21306277e832SShreyas NC 				mconfig, skl, tkn_elem);
21316277e832SShreyas NC 
2132081dc8abSGuneshwor Singh 		if (ret < 0) {
2133081dc8abSGuneshwor Singh 			if (ret == -EEXIST) {
21346277e832SShreyas NC 				is_pipe_exists = 1;
2135081dc8abSGuneshwor Singh 				break;
2136081dc8abSGuneshwor Singh 			}
2137081dc8abSGuneshwor Singh 			return is_pipe_exists;
2138081dc8abSGuneshwor Singh 		}
21396277e832SShreyas NC 
21406277e832SShreyas NC 		break;
21416277e832SShreyas NC 
21426277e832SShreyas NC 	case SKL_TKN_U32_PIPE_CONN_TYPE:
21436277e832SShreyas NC 	case SKL_TKN_U32_PIPE_PRIORITY:
21446277e832SShreyas NC 	case SKL_TKN_U32_PIPE_MEM_PGS:
21458a0cb236SVinod Koul 	case SKL_TKN_U32_PMODE:
21466277e832SShreyas NC 		if (is_pipe_exists) {
21476277e832SShreyas NC 			ret = skl_tplg_fill_pipe_tkn(dev, mconfig->pipe,
21486277e832SShreyas NC 					tkn_elem->token, tkn_elem->value);
21496277e832SShreyas NC 			if (ret < 0)
21506277e832SShreyas NC 				return ret;
21516277e832SShreyas NC 		}
21526277e832SShreyas NC 
21536277e832SShreyas NC 		break;
21546277e832SShreyas NC 
21556277e832SShreyas NC 	/*
21566277e832SShreyas NC 	 * SKL_TKN_U32_DIR_PIN_COUNT token has the value for both
21576277e832SShreyas NC 	 * direction and the pin count. The first four bits represent
21586277e832SShreyas NC 	 * direction and next four the pin count.
21596277e832SShreyas NC 	 */
21606277e832SShreyas NC 	case SKL_TKN_U32_DIR_PIN_COUNT:
21616277e832SShreyas NC 		dir = tkn_elem->value & SKL_IN_DIR_BIT_MASK;
21626277e832SShreyas NC 		pin_index = (tkn_elem->value &
21636277e832SShreyas NC 			SKL_PIN_COUNT_MASK) >> 4;
21646277e832SShreyas NC 
21656277e832SShreyas NC 		break;
21666277e832SShreyas NC 
21676277e832SShreyas NC 	case SKL_TKN_U32_FMT_CH:
21686277e832SShreyas NC 	case SKL_TKN_U32_FMT_FREQ:
21696277e832SShreyas NC 	case SKL_TKN_U32_FMT_BIT_DEPTH:
21706277e832SShreyas NC 	case SKL_TKN_U32_FMT_SAMPLE_SIZE:
21716277e832SShreyas NC 	case SKL_TKN_U32_FMT_CH_CONFIG:
21726277e832SShreyas NC 	case SKL_TKN_U32_FMT_INTERLEAVE:
21736277e832SShreyas NC 	case SKL_TKN_U32_FMT_SAMPLE_TYPE:
21746277e832SShreyas NC 	case SKL_TKN_U32_FMT_CH_MAP:
21756277e832SShreyas NC 		ret = skl_tplg_fill_fmt(dev, mconfig, tkn_elem->token,
21766277e832SShreyas NC 				tkn_elem->value, dir, pin_index);
21776277e832SShreyas NC 
21786277e832SShreyas NC 		if (ret < 0)
21796277e832SShreyas NC 			return ret;
21806277e832SShreyas NC 
21816277e832SShreyas NC 		break;
21826277e832SShreyas NC 
21836277e832SShreyas NC 	case SKL_TKN_U32_PIN_MOD_ID:
21846277e832SShreyas NC 	case SKL_TKN_U32_PIN_INST_ID:
21856277e832SShreyas NC 		ret = skl_tplg_fill_pins_info(dev,
21866277e832SShreyas NC 				mconfig, tkn_elem, dir,
21876277e832SShreyas NC 				pin_index);
21886277e832SShreyas NC 		if (ret < 0)
21896277e832SShreyas NC 			return ret;
21906277e832SShreyas NC 
21916277e832SShreyas NC 		break;
21926277e832SShreyas NC 
21936277e832SShreyas NC 	case SKL_TKN_U32_CAPS_SIZE:
21946277e832SShreyas NC 		mconfig->formats_config.caps_size =
21956277e832SShreyas NC 			tkn_elem->value;
21966277e832SShreyas NC 
21976277e832SShreyas NC 		break;
21986277e832SShreyas NC 
2199133e6e5cSShreyas NC 	case SKL_TKN_U32_CAPS_SET_PARAMS:
2200133e6e5cSShreyas NC 		mconfig->formats_config.set_params =
2201133e6e5cSShreyas NC 				tkn_elem->value;
2202133e6e5cSShreyas NC 		break;
2203133e6e5cSShreyas NC 
2204133e6e5cSShreyas NC 	case SKL_TKN_U32_CAPS_PARAMS_ID:
2205133e6e5cSShreyas NC 		mconfig->formats_config.param_id =
2206133e6e5cSShreyas NC 				tkn_elem->value;
2207133e6e5cSShreyas NC 		break;
2208133e6e5cSShreyas NC 
22096277e832SShreyas NC 	case SKL_TKN_U32_PROC_DOMAIN:
22106277e832SShreyas NC 		mconfig->domain =
22116277e832SShreyas NC 			tkn_elem->value;
22126277e832SShreyas NC 
22136277e832SShreyas NC 		break;
22146277e832SShreyas NC 
2215939df3adSRamesh Babu 	case SKL_TKN_U32_DMA_BUF_SIZE:
2216939df3adSRamesh Babu 		mconfig->dma_buffer_size = tkn_elem->value;
2217939df3adSRamesh Babu 		break;
2218939df3adSRamesh Babu 
22196277e832SShreyas NC 	case SKL_TKN_U8_IN_PIN_TYPE:
22206277e832SShreyas NC 	case SKL_TKN_U8_OUT_PIN_TYPE:
22216277e832SShreyas NC 	case SKL_TKN_U8_CONN_TYPE:
22226277e832SShreyas NC 		break;
22236277e832SShreyas NC 
22246277e832SShreyas NC 	default:
22256277e832SShreyas NC 		dev_err(dev, "Token %d not handled\n",
22266277e832SShreyas NC 				tkn_elem->token);
22276277e832SShreyas NC 		return -EINVAL;
22286277e832SShreyas NC 	}
22296277e832SShreyas NC 
22306277e832SShreyas NC 	tkn_count++;
22316277e832SShreyas NC 
22326277e832SShreyas NC 	return tkn_count;
22336277e832SShreyas NC }
22346277e832SShreyas NC 
22356277e832SShreyas NC /*
22366277e832SShreyas NC  * Parse the vendor array for specific tokens to construct
22376277e832SShreyas NC  * module private data
22386277e832SShreyas NC  */
22396277e832SShreyas NC static int skl_tplg_get_tokens(struct device *dev,
22406277e832SShreyas NC 		char *pvt_data,	struct skl *skl,
22416277e832SShreyas NC 		struct skl_module_cfg *mconfig, int block_size)
22426277e832SShreyas NC {
22436277e832SShreyas NC 	struct snd_soc_tplg_vendor_array *array;
22446277e832SShreyas NC 	struct snd_soc_tplg_vendor_value_elem *tkn_elem;
22456277e832SShreyas NC 	int tkn_count = 0, ret;
22466277e832SShreyas NC 	int off = 0, tuple_size = 0;
22476277e832SShreyas NC 
22486277e832SShreyas NC 	if (block_size <= 0)
22496277e832SShreyas NC 		return -EINVAL;
22506277e832SShreyas NC 
22516277e832SShreyas NC 	while (tuple_size < block_size) {
22526277e832SShreyas NC 		array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off);
22536277e832SShreyas NC 
22546277e832SShreyas NC 		off += array->size;
22556277e832SShreyas NC 
22566277e832SShreyas NC 		switch (array->type) {
22576277e832SShreyas NC 		case SND_SOC_TPLG_TUPLE_TYPE_STRING:
2258ecd286a9SColin Ian King 			dev_warn(dev, "no string tokens expected for skl tplg\n");
22596277e832SShreyas NC 			continue;
22606277e832SShreyas NC 
22616277e832SShreyas NC 		case SND_SOC_TPLG_TUPLE_TYPE_UUID:
22626277e832SShreyas NC 			ret = skl_tplg_get_uuid(dev, mconfig, array->uuid);
22636277e832SShreyas NC 			if (ret < 0)
22646277e832SShreyas NC 				return ret;
22656277e832SShreyas NC 
22666277e832SShreyas NC 			tuple_size += sizeof(*array->uuid);
22676277e832SShreyas NC 
22686277e832SShreyas NC 			continue;
22696277e832SShreyas NC 
22706277e832SShreyas NC 		default:
22716277e832SShreyas NC 			tkn_elem = array->value;
22726277e832SShreyas NC 			tkn_count = 0;
22736277e832SShreyas NC 			break;
22746277e832SShreyas NC 		}
22756277e832SShreyas NC 
22766277e832SShreyas NC 		while (tkn_count <= (array->num_elems - 1)) {
22776277e832SShreyas NC 			ret = skl_tplg_get_token(dev, tkn_elem,
22786277e832SShreyas NC 					skl, mconfig);
22796277e832SShreyas NC 
22806277e832SShreyas NC 			if (ret < 0)
22816277e832SShreyas NC 				return ret;
22826277e832SShreyas NC 
22836277e832SShreyas NC 			tkn_count = tkn_count + ret;
22846277e832SShreyas NC 			tkn_elem++;
22856277e832SShreyas NC 		}
22866277e832SShreyas NC 
22876277e832SShreyas NC 		tuple_size += tkn_count * sizeof(*tkn_elem);
22886277e832SShreyas NC 	}
22896277e832SShreyas NC 
2290133e6e5cSShreyas NC 	return off;
22916277e832SShreyas NC }
22926277e832SShreyas NC 
22936277e832SShreyas NC /*
22946277e832SShreyas NC  * Every data block is preceded by a descriptor to read the number
22956277e832SShreyas NC  * of data blocks, they type of the block and it's size
22966277e832SShreyas NC  */
22976277e832SShreyas NC static int skl_tplg_get_desc_blocks(struct device *dev,
22986277e832SShreyas NC 		struct snd_soc_tplg_vendor_array *array)
22996277e832SShreyas NC {
23006277e832SShreyas NC 	struct snd_soc_tplg_vendor_value_elem *tkn_elem;
23016277e832SShreyas NC 
23026277e832SShreyas NC 	tkn_elem = array->value;
23036277e832SShreyas NC 
23046277e832SShreyas NC 	switch (tkn_elem->token) {
23056277e832SShreyas NC 	case SKL_TKN_U8_NUM_BLOCKS:
23066277e832SShreyas NC 	case SKL_TKN_U8_BLOCK_TYPE:
23076277e832SShreyas NC 	case SKL_TKN_U16_BLOCK_SIZE:
23086277e832SShreyas NC 		return tkn_elem->value;
23096277e832SShreyas NC 
23106277e832SShreyas NC 	default:
2311ecd286a9SColin Ian King 		dev_err(dev, "Invalid descriptor token %d\n", tkn_elem->token);
23126277e832SShreyas NC 		break;
23136277e832SShreyas NC 	}
23146277e832SShreyas NC 
23156277e832SShreyas NC 	return -EINVAL;
23166277e832SShreyas NC }
23176277e832SShreyas NC 
23186277e832SShreyas NC /*
23196277e832SShreyas NC  * Parse the private data for the token and corresponding value.
23206277e832SShreyas NC  * The private data can have multiple data blocks. So, a data block
23216277e832SShreyas NC  * is preceded by a descriptor for number of blocks and a descriptor
23226277e832SShreyas NC  * for the type and size of the suceeding data block.
23236277e832SShreyas NC  */
23246277e832SShreyas NC static int skl_tplg_get_pvt_data(struct snd_soc_tplg_dapm_widget *tplg_w,
23256277e832SShreyas NC 				struct skl *skl, struct device *dev,
23266277e832SShreyas NC 				struct skl_module_cfg *mconfig)
23276277e832SShreyas NC {
23286277e832SShreyas NC 	struct snd_soc_tplg_vendor_array *array;
23296277e832SShreyas NC 	int num_blocks, block_size = 0, block_type, off = 0;
23306277e832SShreyas NC 	char *data;
23316277e832SShreyas NC 	int ret;
23326277e832SShreyas NC 
23336277e832SShreyas NC 	/* Read the NUM_DATA_BLOCKS descriptor */
23346277e832SShreyas NC 	array = (struct snd_soc_tplg_vendor_array *)tplg_w->priv.data;
23356277e832SShreyas NC 	ret = skl_tplg_get_desc_blocks(dev, array);
23366277e832SShreyas NC 	if (ret < 0)
23376277e832SShreyas NC 		return ret;
23386277e832SShreyas NC 	num_blocks = ret;
23396277e832SShreyas NC 
23406277e832SShreyas NC 	off += array->size;
23416277e832SShreyas NC 	/* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */
23426277e832SShreyas NC 	while (num_blocks > 0) {
2343133e6e5cSShreyas NC 		array = (struct snd_soc_tplg_vendor_array *)
2344133e6e5cSShreyas NC 				(tplg_w->priv.data + off);
2345133e6e5cSShreyas NC 
23466277e832SShreyas NC 		ret = skl_tplg_get_desc_blocks(dev, array);
23476277e832SShreyas NC 
23486277e832SShreyas NC 		if (ret < 0)
23496277e832SShreyas NC 			return ret;
23506277e832SShreyas NC 		block_type = ret;
23516277e832SShreyas NC 		off += array->size;
23526277e832SShreyas NC 
23536277e832SShreyas NC 		array = (struct snd_soc_tplg_vendor_array *)
23546277e832SShreyas NC 			(tplg_w->priv.data + off);
23556277e832SShreyas NC 
23566277e832SShreyas NC 		ret = skl_tplg_get_desc_blocks(dev, array);
23576277e832SShreyas NC 
23586277e832SShreyas NC 		if (ret < 0)
23596277e832SShreyas NC 			return ret;
23606277e832SShreyas NC 		block_size = ret;
23616277e832SShreyas NC 		off += array->size;
23626277e832SShreyas NC 
23636277e832SShreyas NC 		array = (struct snd_soc_tplg_vendor_array *)
23646277e832SShreyas NC 			(tplg_w->priv.data + off);
23656277e832SShreyas NC 
23666277e832SShreyas NC 		data = (tplg_w->priv.data + off);
23676277e832SShreyas NC 
23686277e832SShreyas NC 		if (block_type == SKL_TYPE_TUPLE) {
23696277e832SShreyas NC 			ret = skl_tplg_get_tokens(dev, data,
23706277e832SShreyas NC 					skl, mconfig, block_size);
23716277e832SShreyas NC 
23726277e832SShreyas NC 			if (ret < 0)
23736277e832SShreyas NC 				return ret;
23746277e832SShreyas NC 
23756277e832SShreyas NC 			--num_blocks;
23766277e832SShreyas NC 		} else {
23776277e832SShreyas NC 			if (mconfig->formats_config.caps_size > 0)
23786277e832SShreyas NC 				memcpy(mconfig->formats_config.caps, data,
23796277e832SShreyas NC 					mconfig->formats_config.caps_size);
23806277e832SShreyas NC 			--num_blocks;
2381133e6e5cSShreyas NC 			ret = mconfig->formats_config.caps_size;
23826277e832SShreyas NC 		}
2383133e6e5cSShreyas NC 		off += ret;
23846277e832SShreyas NC 	}
23856277e832SShreyas NC 
23866277e832SShreyas NC 	return 0;
23874cd9899fSHardik T Shah }
23884cd9899fSHardik T Shah 
2389fe3f4442SDharageswari R static void skl_clear_pin_config(struct snd_soc_platform *platform,
2390fe3f4442SDharageswari R 				struct snd_soc_dapm_widget *w)
2391fe3f4442SDharageswari R {
2392fe3f4442SDharageswari R 	int i;
2393fe3f4442SDharageswari R 	struct skl_module_cfg *mconfig;
2394fe3f4442SDharageswari R 	struct skl_pipe *pipe;
2395fe3f4442SDharageswari R 
2396fe3f4442SDharageswari R 	if (!strncmp(w->dapm->component->name, platform->component.name,
2397fe3f4442SDharageswari R 					strlen(platform->component.name))) {
2398fe3f4442SDharageswari R 		mconfig = w->priv;
2399fe3f4442SDharageswari R 		pipe = mconfig->pipe;
2400fe3f4442SDharageswari R 		for (i = 0; i < mconfig->max_in_queue; i++) {
2401fe3f4442SDharageswari R 			mconfig->m_in_pin[i].in_use = false;
2402fe3f4442SDharageswari R 			mconfig->m_in_pin[i].pin_state = SKL_PIN_UNBIND;
2403fe3f4442SDharageswari R 		}
2404fe3f4442SDharageswari R 		for (i = 0; i < mconfig->max_out_queue; i++) {
2405fe3f4442SDharageswari R 			mconfig->m_out_pin[i].in_use = false;
2406fe3f4442SDharageswari R 			mconfig->m_out_pin[i].pin_state = SKL_PIN_UNBIND;
2407fe3f4442SDharageswari R 		}
2408fe3f4442SDharageswari R 		pipe->state = SKL_PIPE_INVALID;
2409fe3f4442SDharageswari R 		mconfig->m_state = SKL_MODULE_UNINIT;
2410fe3f4442SDharageswari R 	}
2411fe3f4442SDharageswari R }
2412fe3f4442SDharageswari R 
2413fe3f4442SDharageswari R void skl_cleanup_resources(struct skl *skl)
2414fe3f4442SDharageswari R {
2415fe3f4442SDharageswari R 	struct skl_sst *ctx = skl->skl_sst;
2416fe3f4442SDharageswari R 	struct snd_soc_platform *soc_platform = skl->platform;
2417fe3f4442SDharageswari R 	struct snd_soc_dapm_widget *w;
2418fe3f4442SDharageswari R 	struct snd_soc_card *card;
2419fe3f4442SDharageswari R 
2420fe3f4442SDharageswari R 	if (soc_platform == NULL)
2421fe3f4442SDharageswari R 		return;
2422fe3f4442SDharageswari R 
2423fe3f4442SDharageswari R 	card = soc_platform->component.card;
2424fe3f4442SDharageswari R 	if (!card || !card->instantiated)
2425fe3f4442SDharageswari R 		return;
2426fe3f4442SDharageswari R 
2427fe3f4442SDharageswari R 	skl->resource.mem = 0;
2428fe3f4442SDharageswari R 	skl->resource.mcps = 0;
2429fe3f4442SDharageswari R 
2430fe3f4442SDharageswari R 	list_for_each_entry(w, &card->widgets, list) {
2431fe3f4442SDharageswari R 		if (is_skl_dsp_widget_type(w) && (w->priv != NULL))
2432fe3f4442SDharageswari R 			skl_clear_pin_config(soc_platform, w);
2433fe3f4442SDharageswari R 	}
2434fe3f4442SDharageswari R 
2435fe3f4442SDharageswari R 	skl_clear_module_cnt(ctx->dsp);
2436fe3f4442SDharageswari R }
2437fe3f4442SDharageswari R 
24383af36706SVinod Koul /*
24393af36706SVinod Koul  * Topology core widget load callback
24403af36706SVinod Koul  *
24413af36706SVinod Koul  * This is used to save the private data for each widget which gives
24423af36706SVinod Koul  * information to the driver about module and pipeline parameters which DSP
24433af36706SVinod Koul  * FW expects like ids, resource values, formats etc
24443af36706SVinod Koul  */
24453af36706SVinod Koul static int skl_tplg_widget_load(struct snd_soc_component *cmpnt,
24463af36706SVinod Koul 				struct snd_soc_dapm_widget *w,
24473af36706SVinod Koul 				struct snd_soc_tplg_dapm_widget *tplg_w)
24483af36706SVinod Koul {
24493af36706SVinod Koul 	int ret;
24503af36706SVinod Koul 	struct hdac_ext_bus *ebus = snd_soc_component_get_drvdata(cmpnt);
24513af36706SVinod Koul 	struct skl *skl = ebus_to_skl(ebus);
24523af36706SVinod Koul 	struct hdac_bus *bus = ebus_to_hbus(ebus);
24533af36706SVinod Koul 	struct skl_module_cfg *mconfig;
24543af36706SVinod Koul 
24553af36706SVinod Koul 	if (!tplg_w->priv.size)
24563af36706SVinod Koul 		goto bind_event;
24573af36706SVinod Koul 
24583af36706SVinod Koul 	mconfig = devm_kzalloc(bus->dev, sizeof(*mconfig), GFP_KERNEL);
24593af36706SVinod Koul 
24603af36706SVinod Koul 	if (!mconfig)
24613af36706SVinod Koul 		return -ENOMEM;
24623af36706SVinod Koul 
24633af36706SVinod Koul 	w->priv = mconfig;
246409305da9SShreyas NC 
2465b7c50555SVinod Koul 	/*
2466b7c50555SVinod Koul 	 * module binary can be loaded later, so set it to query when
2467b7c50555SVinod Koul 	 * module is load for a use case
2468b7c50555SVinod Koul 	 */
2469b7c50555SVinod Koul 	mconfig->id.module_id = -1;
24704cd9899fSHardik T Shah 
24716277e832SShreyas NC 	/* Parse private data for tuples */
24726277e832SShreyas NC 	ret = skl_tplg_get_pvt_data(tplg_w, skl, bus->dev, mconfig);
24736277e832SShreyas NC 	if (ret < 0)
24746277e832SShreyas NC 		return ret;
24753af36706SVinod Koul bind_event:
24763af36706SVinod Koul 	if (tplg_w->event_type == 0) {
24773373f716SVinod Koul 		dev_dbg(bus->dev, "ASoC: No event handler required\n");
24783af36706SVinod Koul 		return 0;
24793af36706SVinod Koul 	}
24803af36706SVinod Koul 
24813af36706SVinod Koul 	ret = snd_soc_tplg_widget_bind_event(w, skl_tplg_widget_ops,
2482b663a8c5SJeeja KP 					ARRAY_SIZE(skl_tplg_widget_ops),
2483b663a8c5SJeeja KP 					tplg_w->event_type);
24843af36706SVinod Koul 
24853af36706SVinod Koul 	if (ret) {
24863af36706SVinod Koul 		dev_err(bus->dev, "%s: No matching event handlers found for %d\n",
24873af36706SVinod Koul 					__func__, tplg_w->event_type);
24883af36706SVinod Koul 		return -EINVAL;
24893af36706SVinod Koul 	}
24903af36706SVinod Koul 
24913af36706SVinod Koul 	return 0;
24923af36706SVinod Koul }
24933af36706SVinod Koul 
2494140adfbaSJeeja KP static int skl_init_algo_data(struct device *dev, struct soc_bytes_ext *be,
2495140adfbaSJeeja KP 					struct snd_soc_tplg_bytes_control *bc)
2496140adfbaSJeeja KP {
2497140adfbaSJeeja KP 	struct skl_algo_data *ac;
2498140adfbaSJeeja KP 	struct skl_dfw_algo_data *dfw_ac =
2499140adfbaSJeeja KP 				(struct skl_dfw_algo_data *)bc->priv.data;
2500140adfbaSJeeja KP 
2501140adfbaSJeeja KP 	ac = devm_kzalloc(dev, sizeof(*ac), GFP_KERNEL);
2502140adfbaSJeeja KP 	if (!ac)
2503140adfbaSJeeja KP 		return -ENOMEM;
2504140adfbaSJeeja KP 
2505140adfbaSJeeja KP 	/* Fill private data */
2506140adfbaSJeeja KP 	ac->max = dfw_ac->max;
2507140adfbaSJeeja KP 	ac->param_id = dfw_ac->param_id;
2508140adfbaSJeeja KP 	ac->set_params = dfw_ac->set_params;
25090d682104SDharageswari R 	ac->size = dfw_ac->max;
2510140adfbaSJeeja KP 
2511140adfbaSJeeja KP 	if (ac->max) {
2512140adfbaSJeeja KP 		ac->params = (char *) devm_kzalloc(dev, ac->max, GFP_KERNEL);
2513140adfbaSJeeja KP 		if (!ac->params)
2514140adfbaSJeeja KP 			return -ENOMEM;
2515140adfbaSJeeja KP 
2516140adfbaSJeeja KP 		memcpy(ac->params, dfw_ac->params, ac->max);
2517140adfbaSJeeja KP 	}
2518140adfbaSJeeja KP 
2519140adfbaSJeeja KP 	be->dobj.private  = ac;
2520140adfbaSJeeja KP 	return 0;
2521140adfbaSJeeja KP }
2522140adfbaSJeeja KP 
25237a1b749bSDharageswari R static int skl_init_enum_data(struct device *dev, struct soc_enum *se,
25247a1b749bSDharageswari R 				struct snd_soc_tplg_enum_control *ec)
25257a1b749bSDharageswari R {
25267a1b749bSDharageswari R 
25277a1b749bSDharageswari R 	void *data;
25287a1b749bSDharageswari R 
25297a1b749bSDharageswari R 	if (ec->priv.size) {
25307a1b749bSDharageswari R 		data = devm_kzalloc(dev, sizeof(ec->priv.size), GFP_KERNEL);
25317a1b749bSDharageswari R 		if (!data)
25327a1b749bSDharageswari R 			return -ENOMEM;
25337a1b749bSDharageswari R 		memcpy(data, ec->priv.data, ec->priv.size);
25347a1b749bSDharageswari R 		se->dobj.private = data;
25357a1b749bSDharageswari R 	}
25367a1b749bSDharageswari R 
25377a1b749bSDharageswari R 	return 0;
25387a1b749bSDharageswari R 
25397a1b749bSDharageswari R }
25407a1b749bSDharageswari R 
2541140adfbaSJeeja KP static int skl_tplg_control_load(struct snd_soc_component *cmpnt,
2542140adfbaSJeeja KP 				struct snd_kcontrol_new *kctl,
2543140adfbaSJeeja KP 				struct snd_soc_tplg_ctl_hdr *hdr)
2544140adfbaSJeeja KP {
2545140adfbaSJeeja KP 	struct soc_bytes_ext *sb;
2546140adfbaSJeeja KP 	struct snd_soc_tplg_bytes_control *tplg_bc;
25477a1b749bSDharageswari R 	struct snd_soc_tplg_enum_control *tplg_ec;
2548140adfbaSJeeja KP 	struct hdac_ext_bus *ebus  = snd_soc_component_get_drvdata(cmpnt);
2549140adfbaSJeeja KP 	struct hdac_bus *bus = ebus_to_hbus(ebus);
25507a1b749bSDharageswari R 	struct soc_enum *se;
2551140adfbaSJeeja KP 
2552140adfbaSJeeja KP 	switch (hdr->ops.info) {
2553140adfbaSJeeja KP 	case SND_SOC_TPLG_CTL_BYTES:
2554140adfbaSJeeja KP 		tplg_bc = container_of(hdr,
2555140adfbaSJeeja KP 				struct snd_soc_tplg_bytes_control, hdr);
2556140adfbaSJeeja KP 		if (kctl->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2557140adfbaSJeeja KP 			sb = (struct soc_bytes_ext *)kctl->private_value;
2558140adfbaSJeeja KP 			if (tplg_bc->priv.size)
2559140adfbaSJeeja KP 				return skl_init_algo_data(
2560140adfbaSJeeja KP 						bus->dev, sb, tplg_bc);
2561140adfbaSJeeja KP 		}
2562140adfbaSJeeja KP 		break;
2563140adfbaSJeeja KP 
25647a1b749bSDharageswari R 	case SND_SOC_TPLG_CTL_ENUM:
25657a1b749bSDharageswari R 		tplg_ec = container_of(hdr,
25667a1b749bSDharageswari R 				struct snd_soc_tplg_enum_control, hdr);
25677a1b749bSDharageswari R 		if (kctl->access & SNDRV_CTL_ELEM_ACCESS_READWRITE) {
25687a1b749bSDharageswari R 			se = (struct soc_enum *)kctl->private_value;
25697a1b749bSDharageswari R 			if (tplg_ec->priv.size)
25707a1b749bSDharageswari R 				return skl_init_enum_data(bus->dev, se,
25717a1b749bSDharageswari R 						tplg_ec);
25727a1b749bSDharageswari R 		}
25737a1b749bSDharageswari R 		break;
25747a1b749bSDharageswari R 
2575140adfbaSJeeja KP 	default:
2576140adfbaSJeeja KP 		dev_warn(bus->dev, "Control load not supported %d:%d:%d\n",
2577140adfbaSJeeja KP 			hdr->ops.get, hdr->ops.put, hdr->ops.info);
2578140adfbaSJeeja KP 		break;
2579140adfbaSJeeja KP 	}
2580140adfbaSJeeja KP 
2581140adfbaSJeeja KP 	return 0;
2582140adfbaSJeeja KP }
2583140adfbaSJeeja KP 
2584541070ceSShreyas NC static int skl_tplg_fill_str_mfest_tkn(struct device *dev,
2585541070ceSShreyas NC 		struct snd_soc_tplg_vendor_string_elem *str_elem,
2586eee0e16fSJeeja KP 		struct skl *skl)
2587541070ceSShreyas NC {
2588541070ceSShreyas NC 	int tkn_count = 0;
2589541070ceSShreyas NC 	static int ref_count;
2590541070ceSShreyas NC 
2591541070ceSShreyas NC 	switch (str_elem->token) {
2592541070ceSShreyas NC 	case SKL_TKN_STR_LIB_NAME:
2593eee0e16fSJeeja KP 		if (ref_count > skl->skl_sst->lib_count - 1) {
2594541070ceSShreyas NC 			ref_count = 0;
2595541070ceSShreyas NC 			return -EINVAL;
2596541070ceSShreyas NC 		}
2597541070ceSShreyas NC 
2598eee0e16fSJeeja KP 		strncpy(skl->skl_sst->lib_info[ref_count].name,
2599eee0e16fSJeeja KP 			str_elem->string,
2600eee0e16fSJeeja KP 			ARRAY_SIZE(skl->skl_sst->lib_info[ref_count].name));
2601541070ceSShreyas NC 		ref_count++;
2602541070ceSShreyas NC 		tkn_count++;
2603541070ceSShreyas NC 		break;
2604541070ceSShreyas NC 
2605541070ceSShreyas NC 	default:
2606ecd286a9SColin Ian King 		dev_err(dev, "Not a string token %d\n", str_elem->token);
2607541070ceSShreyas NC 		break;
2608541070ceSShreyas NC 	}
2609541070ceSShreyas NC 
2610541070ceSShreyas NC 	return tkn_count;
2611541070ceSShreyas NC }
2612541070ceSShreyas NC 
2613541070ceSShreyas NC static int skl_tplg_get_str_tkn(struct device *dev,
2614541070ceSShreyas NC 		struct snd_soc_tplg_vendor_array *array,
2615eee0e16fSJeeja KP 		struct skl *skl)
2616541070ceSShreyas NC {
2617541070ceSShreyas NC 	int tkn_count = 0, ret;
2618541070ceSShreyas NC 	struct snd_soc_tplg_vendor_string_elem *str_elem;
2619541070ceSShreyas NC 
2620541070ceSShreyas NC 	str_elem = (struct snd_soc_tplg_vendor_string_elem *)array->value;
2621541070ceSShreyas NC 	while (tkn_count < array->num_elems) {
2622eee0e16fSJeeja KP 		ret = skl_tplg_fill_str_mfest_tkn(dev, str_elem, skl);
2623541070ceSShreyas NC 		str_elem++;
2624541070ceSShreyas NC 
2625541070ceSShreyas NC 		if (ret < 0)
2626541070ceSShreyas NC 			return ret;
2627541070ceSShreyas NC 
2628541070ceSShreyas NC 		tkn_count = tkn_count + ret;
2629541070ceSShreyas NC 	}
2630541070ceSShreyas NC 
2631541070ceSShreyas NC 	return tkn_count;
2632541070ceSShreyas NC }
2633541070ceSShreyas NC 
2634541070ceSShreyas NC static int skl_tplg_get_int_tkn(struct device *dev,
2635541070ceSShreyas NC 		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2636eee0e16fSJeeja KP 		struct skl *skl)
2637541070ceSShreyas NC {
2638541070ceSShreyas NC 	int tkn_count = 0;
2639541070ceSShreyas NC 
2640541070ceSShreyas NC 	switch (tkn_elem->token) {
2641541070ceSShreyas NC 	case SKL_TKN_U32_LIB_COUNT:
2642eee0e16fSJeeja KP 		skl->skl_sst->lib_count = tkn_elem->value;
2643541070ceSShreyas NC 		tkn_count++;
2644541070ceSShreyas NC 		break;
2645541070ceSShreyas NC 
2646541070ceSShreyas NC 	default:
2647ecd286a9SColin Ian King 		dev_err(dev, "Not a manifest token %d\n", tkn_elem->token);
2648541070ceSShreyas NC 		return -EINVAL;
2649541070ceSShreyas NC 	}
2650541070ceSShreyas NC 
2651541070ceSShreyas NC 	return tkn_count;
2652541070ceSShreyas NC }
2653541070ceSShreyas NC 
2654541070ceSShreyas NC /*
2655541070ceSShreyas NC  * Fill the manifest structure by parsing the tokens based on the
2656541070ceSShreyas NC  * type.
2657541070ceSShreyas NC  */
2658541070ceSShreyas NC static int skl_tplg_get_manifest_tkn(struct device *dev,
2659eee0e16fSJeeja KP 		char *pvt_data, struct skl *skl,
2660541070ceSShreyas NC 		int block_size)
2661541070ceSShreyas NC {
2662541070ceSShreyas NC 	int tkn_count = 0, ret;
2663541070ceSShreyas NC 	int off = 0, tuple_size = 0;
2664541070ceSShreyas NC 	struct snd_soc_tplg_vendor_array *array;
2665541070ceSShreyas NC 	struct snd_soc_tplg_vendor_value_elem *tkn_elem;
2666541070ceSShreyas NC 
2667541070ceSShreyas NC 	if (block_size <= 0)
2668541070ceSShreyas NC 		return -EINVAL;
2669541070ceSShreyas NC 
2670541070ceSShreyas NC 	while (tuple_size < block_size) {
2671541070ceSShreyas NC 		array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off);
2672541070ceSShreyas NC 		off += array->size;
2673541070ceSShreyas NC 		switch (array->type) {
2674541070ceSShreyas NC 		case SND_SOC_TPLG_TUPLE_TYPE_STRING:
2675eee0e16fSJeeja KP 			ret = skl_tplg_get_str_tkn(dev, array, skl);
2676541070ceSShreyas NC 
2677541070ceSShreyas NC 			if (ret < 0)
2678541070ceSShreyas NC 				return ret;
2679541070ceSShreyas NC 			tkn_count += ret;
2680541070ceSShreyas NC 
2681541070ceSShreyas NC 			tuple_size += tkn_count *
2682541070ceSShreyas NC 				sizeof(struct snd_soc_tplg_vendor_string_elem);
2683541070ceSShreyas NC 			continue;
2684541070ceSShreyas NC 
2685541070ceSShreyas NC 		case SND_SOC_TPLG_TUPLE_TYPE_UUID:
2686ecd286a9SColin Ian King 			dev_warn(dev, "no uuid tokens for skl tplf manifest\n");
2687541070ceSShreyas NC 			continue;
2688541070ceSShreyas NC 
2689541070ceSShreyas NC 		default:
2690541070ceSShreyas NC 			tkn_elem = array->value;
2691541070ceSShreyas NC 			tkn_count = 0;
2692541070ceSShreyas NC 			break;
2693541070ceSShreyas NC 		}
2694541070ceSShreyas NC 
2695541070ceSShreyas NC 		while (tkn_count <= array->num_elems - 1) {
2696541070ceSShreyas NC 			ret = skl_tplg_get_int_tkn(dev,
2697eee0e16fSJeeja KP 					tkn_elem, skl);
2698541070ceSShreyas NC 			if (ret < 0)
2699541070ceSShreyas NC 				return ret;
2700541070ceSShreyas NC 
2701541070ceSShreyas NC 			tkn_count = tkn_count + ret;
2702541070ceSShreyas NC 			tkn_elem++;
2703541070ceSShreyas NC 			tuple_size += tkn_count *
2704541070ceSShreyas NC 				sizeof(struct snd_soc_tplg_vendor_value_elem);
2705541070ceSShreyas NC 			break;
2706541070ceSShreyas NC 		}
2707541070ceSShreyas NC 		tkn_count = 0;
2708541070ceSShreyas NC 	}
2709541070ceSShreyas NC 
2710541070ceSShreyas NC 	return 0;
2711541070ceSShreyas NC }
2712541070ceSShreyas NC 
2713541070ceSShreyas NC /*
2714541070ceSShreyas NC  * Parse manifest private data for tokens. The private data block is
2715541070ceSShreyas NC  * preceded by descriptors for type and size of data block.
2716541070ceSShreyas NC  */
2717541070ceSShreyas NC static int skl_tplg_get_manifest_data(struct snd_soc_tplg_manifest *manifest,
2718eee0e16fSJeeja KP 			struct device *dev, struct skl *skl)
2719541070ceSShreyas NC {
2720541070ceSShreyas NC 	struct snd_soc_tplg_vendor_array *array;
2721541070ceSShreyas NC 	int num_blocks, block_size = 0, block_type, off = 0;
2722541070ceSShreyas NC 	char *data;
2723541070ceSShreyas NC 	int ret;
2724541070ceSShreyas NC 
2725541070ceSShreyas NC 	/* Read the NUM_DATA_BLOCKS descriptor */
2726541070ceSShreyas NC 	array = (struct snd_soc_tplg_vendor_array *)manifest->priv.data;
2727541070ceSShreyas NC 	ret = skl_tplg_get_desc_blocks(dev, array);
2728541070ceSShreyas NC 	if (ret < 0)
2729541070ceSShreyas NC 		return ret;
2730541070ceSShreyas NC 	num_blocks = ret;
2731541070ceSShreyas NC 
2732541070ceSShreyas NC 	off += array->size;
2733541070ceSShreyas NC 	array = (struct snd_soc_tplg_vendor_array *)
2734541070ceSShreyas NC 			(manifest->priv.data + off);
2735541070ceSShreyas NC 
2736541070ceSShreyas NC 	/* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */
2737541070ceSShreyas NC 	while (num_blocks > 0) {
2738541070ceSShreyas NC 		ret = skl_tplg_get_desc_blocks(dev, array);
2739541070ceSShreyas NC 
2740541070ceSShreyas NC 		if (ret < 0)
2741541070ceSShreyas NC 			return ret;
2742541070ceSShreyas NC 		block_type = ret;
2743541070ceSShreyas NC 		off += array->size;
2744541070ceSShreyas NC 
2745541070ceSShreyas NC 		array = (struct snd_soc_tplg_vendor_array *)
2746541070ceSShreyas NC 			(manifest->priv.data + off);
2747541070ceSShreyas NC 
2748541070ceSShreyas NC 		ret = skl_tplg_get_desc_blocks(dev, array);
2749541070ceSShreyas NC 
2750541070ceSShreyas NC 		if (ret < 0)
2751541070ceSShreyas NC 			return ret;
2752541070ceSShreyas NC 		block_size = ret;
2753541070ceSShreyas NC 		off += array->size;
2754541070ceSShreyas NC 
2755541070ceSShreyas NC 		array = (struct snd_soc_tplg_vendor_array *)
2756541070ceSShreyas NC 			(manifest->priv.data + off);
2757541070ceSShreyas NC 
2758541070ceSShreyas NC 		data = (manifest->priv.data + off);
2759541070ceSShreyas NC 
2760541070ceSShreyas NC 		if (block_type == SKL_TYPE_TUPLE) {
2761eee0e16fSJeeja KP 			ret = skl_tplg_get_manifest_tkn(dev, data, skl,
2762541070ceSShreyas NC 					block_size);
2763541070ceSShreyas NC 
2764541070ceSShreyas NC 			if (ret < 0)
2765541070ceSShreyas NC 				return ret;
2766541070ceSShreyas NC 
2767541070ceSShreyas NC 			--num_blocks;
2768541070ceSShreyas NC 		} else {
2769541070ceSShreyas NC 			return -EINVAL;
2770541070ceSShreyas NC 		}
2771541070ceSShreyas NC 	}
2772541070ceSShreyas NC 
2773541070ceSShreyas NC 	return 0;
2774541070ceSShreyas NC }
2775541070ceSShreyas NC 
277615ecaba9SKranthi G static int skl_manifest_load(struct snd_soc_component *cmpnt,
277715ecaba9SKranthi G 				struct snd_soc_tplg_manifest *manifest)
277815ecaba9SKranthi G {
277915ecaba9SKranthi G 	struct hdac_ext_bus *ebus = snd_soc_component_get_drvdata(cmpnt);
278015ecaba9SKranthi G 	struct hdac_bus *bus = ebus_to_hbus(ebus);
278115ecaba9SKranthi G 	struct skl *skl = ebus_to_skl(ebus);
278215ecaba9SKranthi G 
2783c15ad605SVinod Koul 	/* proceed only if we have private data defined */
2784c15ad605SVinod Koul 	if (manifest->priv.size == 0)
2785c15ad605SVinod Koul 		return 0;
2786c15ad605SVinod Koul 
2787eee0e16fSJeeja KP 	skl_tplg_get_manifest_data(manifest, bus->dev, skl);
2788541070ceSShreyas NC 
2789eee0e16fSJeeja KP 	if (skl->skl_sst->lib_count > SKL_MAX_LIB) {
279015ecaba9SKranthi G 		dev_err(bus->dev, "Exceeding max Library count. Got:%d\n",
2791eee0e16fSJeeja KP 					skl->skl_sst->lib_count);
2792eee0e16fSJeeja KP 		return  -EINVAL;
279315ecaba9SKranthi G 	}
279415ecaba9SKranthi G 
2795eee0e16fSJeeja KP 	return 0;
279615ecaba9SKranthi G }
279715ecaba9SKranthi G 
27983af36706SVinod Koul static struct snd_soc_tplg_ops skl_tplg_ops  = {
27993af36706SVinod Koul 	.widget_load = skl_tplg_widget_load,
2800140adfbaSJeeja KP 	.control_load = skl_tplg_control_load,
2801140adfbaSJeeja KP 	.bytes_ext_ops = skl_tlv_ops,
2802140adfbaSJeeja KP 	.bytes_ext_ops_count = ARRAY_SIZE(skl_tlv_ops),
28037a1b749bSDharageswari R 	.io_ops = skl_tplg_kcontrol_ops,
28047a1b749bSDharageswari R 	.io_ops_count = ARRAY_SIZE(skl_tplg_kcontrol_ops),
280515ecaba9SKranthi G 	.manifest = skl_manifest_load,
28063af36706SVinod Koul };
28073af36706SVinod Koul 
2808287af4f9SJeeja KP /*
2809287af4f9SJeeja KP  * A pipe can have multiple modules, each of them will be a DAPM widget as
2810287af4f9SJeeja KP  * well. While managing a pipeline we need to get the list of all the
2811287af4f9SJeeja KP  * widgets in a pipelines, so this helper - skl_tplg_create_pipe_widget_list()
2812287af4f9SJeeja KP  * helps to get the SKL type widgets in that pipeline
2813287af4f9SJeeja KP  */
2814287af4f9SJeeja KP static int skl_tplg_create_pipe_widget_list(struct snd_soc_platform *platform)
2815287af4f9SJeeja KP {
2816287af4f9SJeeja KP 	struct snd_soc_dapm_widget *w;
2817287af4f9SJeeja KP 	struct skl_module_cfg *mcfg = NULL;
2818287af4f9SJeeja KP 	struct skl_pipe_module *p_module = NULL;
2819287af4f9SJeeja KP 	struct skl_pipe *pipe;
2820287af4f9SJeeja KP 
2821287af4f9SJeeja KP 	list_for_each_entry(w, &platform->component.card->widgets, list) {
2822287af4f9SJeeja KP 		if (is_skl_dsp_widget_type(w) && w->priv != NULL) {
2823287af4f9SJeeja KP 			mcfg = w->priv;
2824287af4f9SJeeja KP 			pipe = mcfg->pipe;
2825287af4f9SJeeja KP 
2826287af4f9SJeeja KP 			p_module = devm_kzalloc(platform->dev,
2827287af4f9SJeeja KP 						sizeof(*p_module), GFP_KERNEL);
2828287af4f9SJeeja KP 			if (!p_module)
2829287af4f9SJeeja KP 				return -ENOMEM;
2830287af4f9SJeeja KP 
2831287af4f9SJeeja KP 			p_module->w = w;
2832287af4f9SJeeja KP 			list_add_tail(&p_module->node, &pipe->w_list);
2833287af4f9SJeeja KP 		}
2834287af4f9SJeeja KP 	}
2835287af4f9SJeeja KP 
2836287af4f9SJeeja KP 	return 0;
2837287af4f9SJeeja KP }
2838287af4f9SJeeja KP 
2839f0aa94faSJeeja KP static void skl_tplg_set_pipe_type(struct skl *skl, struct skl_pipe *pipe)
2840f0aa94faSJeeja KP {
2841f0aa94faSJeeja KP 	struct skl_pipe_module *w_module;
2842f0aa94faSJeeja KP 	struct snd_soc_dapm_widget *w;
2843f0aa94faSJeeja KP 	struct skl_module_cfg *mconfig;
2844f0aa94faSJeeja KP 	bool host_found = false, link_found = false;
2845f0aa94faSJeeja KP 
2846f0aa94faSJeeja KP 	list_for_each_entry(w_module, &pipe->w_list, node) {
2847f0aa94faSJeeja KP 		w = w_module->w;
2848f0aa94faSJeeja KP 		mconfig = w->priv;
2849f0aa94faSJeeja KP 
2850f0aa94faSJeeja KP 		if (mconfig->dev_type == SKL_DEVICE_HDAHOST)
2851f0aa94faSJeeja KP 			host_found = true;
2852f0aa94faSJeeja KP 		else if (mconfig->dev_type != SKL_DEVICE_NONE)
2853f0aa94faSJeeja KP 			link_found = true;
2854f0aa94faSJeeja KP 	}
2855f0aa94faSJeeja KP 
2856f0aa94faSJeeja KP 	if (host_found && link_found)
2857f0aa94faSJeeja KP 		pipe->passthru = true;
2858f0aa94faSJeeja KP 	else
2859f0aa94faSJeeja KP 		pipe->passthru = false;
2860f0aa94faSJeeja KP }
2861f0aa94faSJeeja KP 
28623af36706SVinod Koul /* This will be read from topology manifest, currently defined here */
28633af36706SVinod Koul #define SKL_MAX_MCPS 30000000
28643af36706SVinod Koul #define SKL_FW_MAX_MEM 1000000
28653af36706SVinod Koul 
28663af36706SVinod Koul /*
28673af36706SVinod Koul  * SKL topology init routine
28683af36706SVinod Koul  */
28693af36706SVinod Koul int skl_tplg_init(struct snd_soc_platform *platform, struct hdac_ext_bus *ebus)
28703af36706SVinod Koul {
28713af36706SVinod Koul 	int ret;
28723af36706SVinod Koul 	const struct firmware *fw;
28733af36706SVinod Koul 	struct hdac_bus *bus = ebus_to_hbus(ebus);
28743af36706SVinod Koul 	struct skl *skl = ebus_to_skl(ebus);
2875f0aa94faSJeeja KP 	struct skl_pipeline *ppl;
28763af36706SVinod Koul 
28774b235c43SVinod Koul 	ret = request_firmware(&fw, skl->tplg_name, bus->dev);
28783af36706SVinod Koul 	if (ret < 0) {
2879b663a8c5SJeeja KP 		dev_err(bus->dev, "tplg fw %s load failed with %d\n",
28804b235c43SVinod Koul 				skl->tplg_name, ret);
28814b235c43SVinod Koul 		ret = request_firmware(&fw, "dfw_sst.bin", bus->dev);
28824b235c43SVinod Koul 		if (ret < 0) {
28834b235c43SVinod Koul 			dev_err(bus->dev, "Fallback tplg fw %s load failed with %d\n",
28843af36706SVinod Koul 					"dfw_sst.bin", ret);
28853af36706SVinod Koul 			return ret;
28863af36706SVinod Koul 		}
28874b235c43SVinod Koul 	}
28883af36706SVinod Koul 
28893af36706SVinod Koul 	/*
28903af36706SVinod Koul 	 * The complete tplg for SKL is loaded as index 0, we don't use
28913af36706SVinod Koul 	 * any other index
28923af36706SVinod Koul 	 */
2893b663a8c5SJeeja KP 	ret = snd_soc_tplg_component_load(&platform->component,
2894b663a8c5SJeeja KP 					&skl_tplg_ops, fw, 0);
28953af36706SVinod Koul 	if (ret < 0) {
28963af36706SVinod Koul 		dev_err(bus->dev, "tplg component load failed%d\n", ret);
2897c14a82c7SSudip Mukherjee 		release_firmware(fw);
28983af36706SVinod Koul 		return -EINVAL;
28993af36706SVinod Koul 	}
29003af36706SVinod Koul 
29013af36706SVinod Koul 	skl->resource.max_mcps = SKL_MAX_MCPS;
29023af36706SVinod Koul 	skl->resource.max_mem = SKL_FW_MAX_MEM;
29033af36706SVinod Koul 
2904d8018361SVinod Koul 	skl->tplg = fw;
2905287af4f9SJeeja KP 	ret = skl_tplg_create_pipe_widget_list(platform);
2906287af4f9SJeeja KP 	if (ret < 0)
2907287af4f9SJeeja KP 		return ret;
2908d8018361SVinod Koul 
2909f0aa94faSJeeja KP 	list_for_each_entry(ppl, &skl->ppl_list, node)
2910f0aa94faSJeeja KP 		skl_tplg_set_pipe_type(skl, ppl->pipe);
29113af36706SVinod Koul 
29123af36706SVinod Koul 	return 0;
2913e4e2d2f4SJeeja KP }
2914