xref: /openbmc/linux/sound/soc/intel/skylake/skl-topology.c (revision d643678b9a4e432a574b2ec601216afd720c69b6)
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>
24e4e2d2f4SJeeja KP #include "skl-sst-dsp.h"
25e4e2d2f4SJeeja KP #include "skl-sst-ipc.h"
26e4e2d2f4SJeeja KP #include "skl-topology.h"
27e4e2d2f4SJeeja KP #include "skl.h"
28e4e2d2f4SJeeja KP #include "skl-tplg-interface.h"
296c5768b3SDharageswari R #include "../common/sst-dsp.h"
306c5768b3SDharageswari R #include "../common/sst-dsp-priv.h"
31e4e2d2f4SJeeja KP 
32f7590d4fSJeeja KP #define SKL_CH_FIXUP_MASK		(1 << 0)
33f7590d4fSJeeja KP #define SKL_RATE_FIXUP_MASK		(1 << 1)
34f7590d4fSJeeja KP #define SKL_FMT_FIXUP_MASK		(1 << 2)
35f7590d4fSJeeja KP 
36e4e2d2f4SJeeja KP /*
37e4e2d2f4SJeeja KP  * SKL DSP driver modelling uses only few DAPM widgets so for rest we will
38e4e2d2f4SJeeja KP  * ignore. This helpers checks if the SKL driver handles this widget type
39e4e2d2f4SJeeja KP  */
40e4e2d2f4SJeeja KP static int is_skl_dsp_widget_type(struct snd_soc_dapm_widget *w)
41e4e2d2f4SJeeja KP {
42e4e2d2f4SJeeja KP 	switch (w->id) {
43e4e2d2f4SJeeja KP 	case snd_soc_dapm_dai_link:
44e4e2d2f4SJeeja KP 	case snd_soc_dapm_dai_in:
45e4e2d2f4SJeeja KP 	case snd_soc_dapm_aif_in:
46e4e2d2f4SJeeja KP 	case snd_soc_dapm_aif_out:
47e4e2d2f4SJeeja KP 	case snd_soc_dapm_dai_out:
48e4e2d2f4SJeeja KP 	case snd_soc_dapm_switch:
49e4e2d2f4SJeeja KP 		return false;
50e4e2d2f4SJeeja KP 	default:
51e4e2d2f4SJeeja KP 		return true;
52e4e2d2f4SJeeja KP 	}
53e4e2d2f4SJeeja KP }
54e4e2d2f4SJeeja KP 
55e4e2d2f4SJeeja KP /*
56e4e2d2f4SJeeja KP  * Each pipelines needs memory to be allocated. Check if we have free memory
579ba8ffefSDharageswari.R  * from available pool.
58e4e2d2f4SJeeja KP  */
599ba8ffefSDharageswari.R static bool skl_is_pipe_mem_avail(struct skl *skl,
60e4e2d2f4SJeeja KP 				struct skl_module_cfg *mconfig)
61e4e2d2f4SJeeja KP {
62e4e2d2f4SJeeja KP 	struct skl_sst *ctx = skl->skl_sst;
63e4e2d2f4SJeeja KP 
64e4e2d2f4SJeeja KP 	if (skl->resource.mem + mconfig->pipe->memory_pages >
65e4e2d2f4SJeeja KP 				skl->resource.max_mem) {
66e4e2d2f4SJeeja KP 		dev_err(ctx->dev,
67e4e2d2f4SJeeja KP 				"%s: module_id %d instance %d\n", __func__,
68e4e2d2f4SJeeja KP 				mconfig->id.module_id,
69e4e2d2f4SJeeja KP 				mconfig->id.instance_id);
70e4e2d2f4SJeeja KP 		dev_err(ctx->dev,
71e4e2d2f4SJeeja KP 				"exceeds ppl memory available %d mem %d\n",
72e4e2d2f4SJeeja KP 				skl->resource.max_mem, skl->resource.mem);
73e4e2d2f4SJeeja KP 		return false;
749ba8ffefSDharageswari.R 	} else {
759ba8ffefSDharageswari.R 		return true;
769ba8ffefSDharageswari.R 	}
77e4e2d2f4SJeeja KP }
78e4e2d2f4SJeeja KP 
799ba8ffefSDharageswari.R /*
809ba8ffefSDharageswari.R  * Add the mem to the mem pool. This is freed when pipe is deleted.
819ba8ffefSDharageswari.R  * Note: DSP does actual memory management we only keep track for complete
829ba8ffefSDharageswari.R  * pool
839ba8ffefSDharageswari.R  */
849ba8ffefSDharageswari.R static void skl_tplg_alloc_pipe_mem(struct skl *skl,
859ba8ffefSDharageswari.R 				struct skl_module_cfg *mconfig)
869ba8ffefSDharageswari.R {
87e4e2d2f4SJeeja KP 	skl->resource.mem += mconfig->pipe->memory_pages;
88e4e2d2f4SJeeja KP }
89e4e2d2f4SJeeja KP 
90e4e2d2f4SJeeja KP /*
91e4e2d2f4SJeeja KP  * Pipeline needs needs DSP CPU resources for computation, this is
92e4e2d2f4SJeeja KP  * quantified in MCPS (Million Clocks Per Second) required for module/pipe
93e4e2d2f4SJeeja KP  *
94e4e2d2f4SJeeja KP  * Each pipelines needs mcps to be allocated. Check if we have mcps for this
959ba8ffefSDharageswari.R  * pipe.
96e4e2d2f4SJeeja KP  */
979ba8ffefSDharageswari.R 
989ba8ffefSDharageswari.R static bool skl_is_pipe_mcps_avail(struct skl *skl,
99e4e2d2f4SJeeja KP 				struct skl_module_cfg *mconfig)
100e4e2d2f4SJeeja KP {
101e4e2d2f4SJeeja KP 	struct skl_sst *ctx = skl->skl_sst;
102e4e2d2f4SJeeja KP 
103e4e2d2f4SJeeja KP 	if (skl->resource.mcps + mconfig->mcps > skl->resource.max_mcps) {
104e4e2d2f4SJeeja KP 		dev_err(ctx->dev,
105e4e2d2f4SJeeja KP 			"%s: module_id %d instance %d\n", __func__,
106e4e2d2f4SJeeja KP 			mconfig->id.module_id, mconfig->id.instance_id);
107e4e2d2f4SJeeja KP 		dev_err(ctx->dev,
1087ca42f5aSGuneshwor Singh 			"exceeds ppl mcps available %d > mem %d\n",
109e4e2d2f4SJeeja KP 			skl->resource.max_mcps, skl->resource.mcps);
110e4e2d2f4SJeeja KP 		return false;
1119ba8ffefSDharageswari.R 	} else {
1129ba8ffefSDharageswari.R 		return true;
1139ba8ffefSDharageswari.R 	}
114e4e2d2f4SJeeja KP }
115e4e2d2f4SJeeja KP 
1169ba8ffefSDharageswari.R static void skl_tplg_alloc_pipe_mcps(struct skl *skl,
1179ba8ffefSDharageswari.R 				struct skl_module_cfg *mconfig)
1189ba8ffefSDharageswari.R {
119e4e2d2f4SJeeja KP 	skl->resource.mcps += mconfig->mcps;
120e4e2d2f4SJeeja KP }
121e4e2d2f4SJeeja KP 
122e4e2d2f4SJeeja KP /*
123e4e2d2f4SJeeja KP  * Free the mcps when tearing down
124e4e2d2f4SJeeja KP  */
125e4e2d2f4SJeeja KP static void
126e4e2d2f4SJeeja KP skl_tplg_free_pipe_mcps(struct skl *skl, struct skl_module_cfg *mconfig)
127e4e2d2f4SJeeja KP {
128e4e2d2f4SJeeja KP 	skl->resource.mcps -= mconfig->mcps;
129e4e2d2f4SJeeja KP }
130e4e2d2f4SJeeja KP 
131e4e2d2f4SJeeja KP /*
132e4e2d2f4SJeeja KP  * Free the memory when tearing down
133e4e2d2f4SJeeja KP  */
134e4e2d2f4SJeeja KP static void
135e4e2d2f4SJeeja KP skl_tplg_free_pipe_mem(struct skl *skl, struct skl_module_cfg *mconfig)
136e4e2d2f4SJeeja KP {
137e4e2d2f4SJeeja KP 	skl->resource.mem -= mconfig->pipe->memory_pages;
138e4e2d2f4SJeeja KP }
139e4e2d2f4SJeeja KP 
140f7590d4fSJeeja KP 
141f7590d4fSJeeja KP static void skl_dump_mconfig(struct skl_sst *ctx,
142f7590d4fSJeeja KP 					struct skl_module_cfg *mcfg)
143f7590d4fSJeeja KP {
144f7590d4fSJeeja KP 	dev_dbg(ctx->dev, "Dumping config\n");
145f7590d4fSJeeja KP 	dev_dbg(ctx->dev, "Input Format:\n");
1464cd9899fSHardik T Shah 	dev_dbg(ctx->dev, "channels = %d\n", mcfg->in_fmt[0].channels);
1474cd9899fSHardik T Shah 	dev_dbg(ctx->dev, "s_freq = %d\n", mcfg->in_fmt[0].s_freq);
1484cd9899fSHardik T Shah 	dev_dbg(ctx->dev, "ch_cfg = %d\n", mcfg->in_fmt[0].ch_cfg);
1494cd9899fSHardik T Shah 	dev_dbg(ctx->dev, "valid bit depth = %d\n", mcfg->in_fmt[0].valid_bit_depth);
150f7590d4fSJeeja KP 	dev_dbg(ctx->dev, "Output Format:\n");
1514cd9899fSHardik T Shah 	dev_dbg(ctx->dev, "channels = %d\n", mcfg->out_fmt[0].channels);
1524cd9899fSHardik T Shah 	dev_dbg(ctx->dev, "s_freq = %d\n", mcfg->out_fmt[0].s_freq);
1534cd9899fSHardik T Shah 	dev_dbg(ctx->dev, "valid bit depth = %d\n", mcfg->out_fmt[0].valid_bit_depth);
1544cd9899fSHardik T Shah 	dev_dbg(ctx->dev, "ch_cfg = %d\n", mcfg->out_fmt[0].ch_cfg);
155f7590d4fSJeeja KP }
156f7590d4fSJeeja KP 
157f7590d4fSJeeja KP static void skl_tplg_update_params(struct skl_module_fmt *fmt,
158f7590d4fSJeeja KP 			struct skl_pipe_params *params, int fixup)
159f7590d4fSJeeja KP {
160f7590d4fSJeeja KP 	if (fixup & SKL_RATE_FIXUP_MASK)
161f7590d4fSJeeja KP 		fmt->s_freq = params->s_freq;
162f7590d4fSJeeja KP 	if (fixup & SKL_CH_FIXUP_MASK)
163f7590d4fSJeeja KP 		fmt->channels = params->ch;
16498256f83SJeeja KP 	if (fixup & SKL_FMT_FIXUP_MASK) {
16598256f83SJeeja KP 		fmt->valid_bit_depth = skl_get_bit_depth(params->s_fmt);
16698256f83SJeeja KP 
16798256f83SJeeja KP 		/*
16898256f83SJeeja KP 		 * 16 bit is 16 bit container whereas 24 bit is in 32 bit
16998256f83SJeeja KP 		 * container so update bit depth accordingly
17098256f83SJeeja KP 		 */
17198256f83SJeeja KP 		switch (fmt->valid_bit_depth) {
17298256f83SJeeja KP 		case SKL_DEPTH_16BIT:
17398256f83SJeeja KP 			fmt->bit_depth = fmt->valid_bit_depth;
17498256f83SJeeja KP 			break;
17598256f83SJeeja KP 
17698256f83SJeeja KP 		default:
17798256f83SJeeja KP 			fmt->bit_depth = SKL_DEPTH_32BIT;
17898256f83SJeeja KP 			break;
17998256f83SJeeja KP 		}
18098256f83SJeeja KP 	}
18198256f83SJeeja KP 
182f7590d4fSJeeja KP }
183f7590d4fSJeeja KP 
184f7590d4fSJeeja KP /*
185f7590d4fSJeeja KP  * A pipeline may have modules which impact the pcm parameters, like SRC,
186f7590d4fSJeeja KP  * channel converter, format converter.
187f7590d4fSJeeja KP  * We need to calculate the output params by applying the 'fixup'
188f7590d4fSJeeja KP  * Topology will tell driver which type of fixup is to be applied by
189f7590d4fSJeeja KP  * supplying the fixup mask, so based on that we calculate the output
190f7590d4fSJeeja KP  *
191f7590d4fSJeeja KP  * Now In FE the pcm hw_params is source/target format. Same is applicable
192f7590d4fSJeeja KP  * for BE with its hw_params invoked.
193f7590d4fSJeeja KP  * here based on FE, BE pipeline and direction we calculate the input and
194f7590d4fSJeeja KP  * outfix and then apply that for a module
195f7590d4fSJeeja KP  */
196f7590d4fSJeeja KP static void skl_tplg_update_params_fixup(struct skl_module_cfg *m_cfg,
197f7590d4fSJeeja KP 		struct skl_pipe_params *params, bool is_fe)
198f7590d4fSJeeja KP {
199f7590d4fSJeeja KP 	int in_fixup, out_fixup;
200f7590d4fSJeeja KP 	struct skl_module_fmt *in_fmt, *out_fmt;
201f7590d4fSJeeja KP 
2024cd9899fSHardik T Shah 	/* Fixups will be applied to pin 0 only */
2034cd9899fSHardik T Shah 	in_fmt = &m_cfg->in_fmt[0];
2044cd9899fSHardik T Shah 	out_fmt = &m_cfg->out_fmt[0];
205f7590d4fSJeeja KP 
206f7590d4fSJeeja KP 	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
207f7590d4fSJeeja KP 		if (is_fe) {
208f7590d4fSJeeja KP 			in_fixup = m_cfg->params_fixup;
209f7590d4fSJeeja KP 			out_fixup = (~m_cfg->converter) &
210f7590d4fSJeeja KP 					m_cfg->params_fixup;
211f7590d4fSJeeja KP 		} else {
212f7590d4fSJeeja KP 			out_fixup = m_cfg->params_fixup;
213f7590d4fSJeeja KP 			in_fixup = (~m_cfg->converter) &
214f7590d4fSJeeja KP 					m_cfg->params_fixup;
215f7590d4fSJeeja KP 		}
216f7590d4fSJeeja KP 	} else {
217f7590d4fSJeeja KP 		if (is_fe) {
218f7590d4fSJeeja KP 			out_fixup = m_cfg->params_fixup;
219f7590d4fSJeeja KP 			in_fixup = (~m_cfg->converter) &
220f7590d4fSJeeja KP 					m_cfg->params_fixup;
221f7590d4fSJeeja KP 		} else {
222f7590d4fSJeeja KP 			in_fixup = m_cfg->params_fixup;
223f7590d4fSJeeja KP 			out_fixup = (~m_cfg->converter) &
224f7590d4fSJeeja KP 					m_cfg->params_fixup;
225f7590d4fSJeeja KP 		}
226f7590d4fSJeeja KP 	}
227f7590d4fSJeeja KP 
228f7590d4fSJeeja KP 	skl_tplg_update_params(in_fmt, params, in_fixup);
229f7590d4fSJeeja KP 	skl_tplg_update_params(out_fmt, params, out_fixup);
230f7590d4fSJeeja KP }
231f7590d4fSJeeja KP 
232f7590d4fSJeeja KP /*
233f7590d4fSJeeja KP  * A module needs input and output buffers, which are dependent upon pcm
234f7590d4fSJeeja KP  * params, so once we have calculate params, we need buffer calculation as
235f7590d4fSJeeja KP  * well.
236f7590d4fSJeeja KP  */
237f7590d4fSJeeja KP static void skl_tplg_update_buffer_size(struct skl_sst *ctx,
238f7590d4fSJeeja KP 				struct skl_module_cfg *mcfg)
239f7590d4fSJeeja KP {
240f7590d4fSJeeja KP 	int multiplier = 1;
2414cd9899fSHardik T Shah 	struct skl_module_fmt *in_fmt, *out_fmt;
2424cd9899fSHardik T Shah 
2434cd9899fSHardik T Shah 
2444cd9899fSHardik T Shah 	/* Since fixups is applied to pin 0 only, ibs, obs needs
2454cd9899fSHardik T Shah 	 * change for pin 0 only
2464cd9899fSHardik T Shah 	 */
2474cd9899fSHardik T Shah 	in_fmt = &mcfg->in_fmt[0];
2484cd9899fSHardik T Shah 	out_fmt = &mcfg->out_fmt[0];
249f7590d4fSJeeja KP 
250f7590d4fSJeeja KP 	if (mcfg->m_type == SKL_MODULE_TYPE_SRCINT)
251f7590d4fSJeeja KP 		multiplier = 5;
2524cd9899fSHardik T Shah 	mcfg->ibs = (in_fmt->s_freq / 1000) *
2534cd9899fSHardik T Shah 				(mcfg->in_fmt->channels) *
2544cd9899fSHardik T Shah 				(mcfg->in_fmt->bit_depth >> 3) *
255f7590d4fSJeeja KP 				multiplier;
256f7590d4fSJeeja KP 
2574cd9899fSHardik T Shah 	mcfg->obs = (mcfg->out_fmt->s_freq / 1000) *
2584cd9899fSHardik T Shah 				(mcfg->out_fmt->channels) *
2594cd9899fSHardik T Shah 				(mcfg->out_fmt->bit_depth >> 3) *
260f7590d4fSJeeja KP 				multiplier;
261f7590d4fSJeeja KP }
262f7590d4fSJeeja KP 
2632d1419a3SJeeja KP static int skl_tplg_update_be_blob(struct snd_soc_dapm_widget *w,
2642d1419a3SJeeja KP 						struct skl_sst *ctx)
2652d1419a3SJeeja KP {
2662d1419a3SJeeja KP 	struct skl_module_cfg *m_cfg = w->priv;
2672d1419a3SJeeja KP 	int link_type, dir;
2682d1419a3SJeeja KP 	u32 ch, s_freq, s_fmt;
2692d1419a3SJeeja KP 	struct nhlt_specific_cfg *cfg;
2702d1419a3SJeeja KP 	struct skl *skl = get_skl_ctx(ctx->dev);
2712d1419a3SJeeja KP 
2722d1419a3SJeeja KP 	/* check if we already have blob */
2732d1419a3SJeeja KP 	if (m_cfg->formats_config.caps_size > 0)
2742d1419a3SJeeja KP 		return 0;
2752d1419a3SJeeja KP 
276c7c6c736SJeeja KP 	dev_dbg(ctx->dev, "Applying default cfg blob\n");
2772d1419a3SJeeja KP 	switch (m_cfg->dev_type) {
2782d1419a3SJeeja KP 	case SKL_DEVICE_DMIC:
2792d1419a3SJeeja KP 		link_type = NHLT_LINK_DMIC;
280c7c6c736SJeeja KP 		dir = SNDRV_PCM_STREAM_CAPTURE;
2812d1419a3SJeeja KP 		s_freq = m_cfg->in_fmt[0].s_freq;
2822d1419a3SJeeja KP 		s_fmt = m_cfg->in_fmt[0].bit_depth;
2832d1419a3SJeeja KP 		ch = m_cfg->in_fmt[0].channels;
2842d1419a3SJeeja KP 		break;
2852d1419a3SJeeja KP 
2862d1419a3SJeeja KP 	case SKL_DEVICE_I2S:
2872d1419a3SJeeja KP 		link_type = NHLT_LINK_SSP;
2882d1419a3SJeeja KP 		if (m_cfg->hw_conn_type == SKL_CONN_SOURCE) {
289c7c6c736SJeeja KP 			dir = SNDRV_PCM_STREAM_PLAYBACK;
2902d1419a3SJeeja KP 			s_freq = m_cfg->out_fmt[0].s_freq;
2912d1419a3SJeeja KP 			s_fmt = m_cfg->out_fmt[0].bit_depth;
2922d1419a3SJeeja KP 			ch = m_cfg->out_fmt[0].channels;
293c7c6c736SJeeja KP 		} else {
294c7c6c736SJeeja KP 			dir = SNDRV_PCM_STREAM_CAPTURE;
295c7c6c736SJeeja KP 			s_freq = m_cfg->in_fmt[0].s_freq;
296c7c6c736SJeeja KP 			s_fmt = m_cfg->in_fmt[0].bit_depth;
297c7c6c736SJeeja KP 			ch = m_cfg->in_fmt[0].channels;
2982d1419a3SJeeja KP 		}
2992d1419a3SJeeja KP 		break;
3002d1419a3SJeeja KP 
3012d1419a3SJeeja KP 	default:
3022d1419a3SJeeja KP 		return -EINVAL;
3032d1419a3SJeeja KP 	}
3042d1419a3SJeeja KP 
3052d1419a3SJeeja KP 	/* update the blob based on virtual bus_id and default params */
3062d1419a3SJeeja KP 	cfg = skl_get_ep_blob(skl, m_cfg->vbus_id, link_type,
3072d1419a3SJeeja KP 					s_fmt, ch, s_freq, dir);
3082d1419a3SJeeja KP 	if (cfg) {
3092d1419a3SJeeja KP 		m_cfg->formats_config.caps_size = cfg->size;
3102d1419a3SJeeja KP 		m_cfg->formats_config.caps = (u32 *) &cfg->caps;
3112d1419a3SJeeja KP 	} else {
3122d1419a3SJeeja KP 		dev_err(ctx->dev, "Blob NULL for id %x type %d dirn %d\n",
3132d1419a3SJeeja KP 					m_cfg->vbus_id, link_type, dir);
3142d1419a3SJeeja KP 		dev_err(ctx->dev, "PCM: ch %d, freq %d, fmt %d\n",
3152d1419a3SJeeja KP 					ch, s_freq, s_fmt);
3162d1419a3SJeeja KP 		return -EIO;
3172d1419a3SJeeja KP 	}
3182d1419a3SJeeja KP 
3192d1419a3SJeeja KP 	return 0;
3202d1419a3SJeeja KP }
3212d1419a3SJeeja KP 
322f7590d4fSJeeja KP static void skl_tplg_update_module_params(struct snd_soc_dapm_widget *w,
323f7590d4fSJeeja KP 							struct skl_sst *ctx)
324f7590d4fSJeeja KP {
325f7590d4fSJeeja KP 	struct skl_module_cfg *m_cfg = w->priv;
326f7590d4fSJeeja KP 	struct skl_pipe_params *params = m_cfg->pipe->p_params;
327f7590d4fSJeeja KP 	int p_conn_type = m_cfg->pipe->conn_type;
328f7590d4fSJeeja KP 	bool is_fe;
329f7590d4fSJeeja KP 
330f7590d4fSJeeja KP 	if (!m_cfg->params_fixup)
331f7590d4fSJeeja KP 		return;
332f7590d4fSJeeja KP 
333f7590d4fSJeeja KP 	dev_dbg(ctx->dev, "Mconfig for widget=%s BEFORE updation\n",
334f7590d4fSJeeja KP 				w->name);
335f7590d4fSJeeja KP 
336f7590d4fSJeeja KP 	skl_dump_mconfig(ctx, m_cfg);
337f7590d4fSJeeja KP 
338f7590d4fSJeeja KP 	if (p_conn_type == SKL_PIPE_CONN_TYPE_FE)
339f7590d4fSJeeja KP 		is_fe = true;
340f7590d4fSJeeja KP 	else
341f7590d4fSJeeja KP 		is_fe = false;
342f7590d4fSJeeja KP 
343f7590d4fSJeeja KP 	skl_tplg_update_params_fixup(m_cfg, params, is_fe);
344f7590d4fSJeeja KP 	skl_tplg_update_buffer_size(ctx, m_cfg);
345f7590d4fSJeeja KP 
346f7590d4fSJeeja KP 	dev_dbg(ctx->dev, "Mconfig for widget=%s AFTER updation\n",
347f7590d4fSJeeja KP 				w->name);
348f7590d4fSJeeja KP 
349f7590d4fSJeeja KP 	skl_dump_mconfig(ctx, m_cfg);
350f7590d4fSJeeja KP }
351f7590d4fSJeeja KP 
352e4e2d2f4SJeeja KP /*
353e4e2d2f4SJeeja KP  * A pipe can have multiple modules, each of them will be a DAPM widget as
354e4e2d2f4SJeeja KP  * well. While managing a pipeline we need to get the list of all the
355e4e2d2f4SJeeja KP  * widgets in a pipelines, so this helper - skl_tplg_get_pipe_widget() helps
356e4e2d2f4SJeeja KP  * to get the SKL type widgets in that pipeline
357e4e2d2f4SJeeja KP  */
358e4e2d2f4SJeeja KP static int skl_tplg_alloc_pipe_widget(struct device *dev,
359e4e2d2f4SJeeja KP 	struct snd_soc_dapm_widget *w, struct skl_pipe *pipe)
360e4e2d2f4SJeeja KP {
361e4e2d2f4SJeeja KP 	struct skl_module_cfg *src_module = NULL;
362e4e2d2f4SJeeja KP 	struct snd_soc_dapm_path *p = NULL;
363e4e2d2f4SJeeja KP 	struct skl_pipe_module *p_module = NULL;
364e4e2d2f4SJeeja KP 
365e4e2d2f4SJeeja KP 	p_module = devm_kzalloc(dev, sizeof(*p_module), GFP_KERNEL);
366e4e2d2f4SJeeja KP 	if (!p_module)
367e4e2d2f4SJeeja KP 		return -ENOMEM;
368e4e2d2f4SJeeja KP 
369e4e2d2f4SJeeja KP 	p_module->w = w;
370e4e2d2f4SJeeja KP 	list_add_tail(&p_module->node, &pipe->w_list);
371e4e2d2f4SJeeja KP 
372e4e2d2f4SJeeja KP 	snd_soc_dapm_widget_for_each_sink_path(w, p) {
373e4e2d2f4SJeeja KP 		if ((p->sink->priv == NULL)
374e4e2d2f4SJeeja KP 				&& (!is_skl_dsp_widget_type(w)))
375e4e2d2f4SJeeja KP 			continue;
376e4e2d2f4SJeeja KP 
377e4e2d2f4SJeeja KP 		if ((p->sink->priv != NULL) && p->connect
378e4e2d2f4SJeeja KP 				&& is_skl_dsp_widget_type(p->sink)) {
379e4e2d2f4SJeeja KP 
380e4e2d2f4SJeeja KP 			src_module = p->sink->priv;
381e4e2d2f4SJeeja KP 			if (pipe->ppl_id == src_module->pipe->ppl_id)
382e4e2d2f4SJeeja KP 				skl_tplg_alloc_pipe_widget(dev,
383e4e2d2f4SJeeja KP 							p->sink, pipe);
384e4e2d2f4SJeeja KP 		}
385e4e2d2f4SJeeja KP 	}
386e4e2d2f4SJeeja KP 	return 0;
387e4e2d2f4SJeeja KP }
388e4e2d2f4SJeeja KP 
389e4e2d2f4SJeeja KP /*
390abb74003SJeeja KP  * some modules can have multiple params set from user control and
391abb74003SJeeja KP  * need to be set after module is initialized. If set_param flag is
392abb74003SJeeja KP  * set module params will be done after module is initialised.
393abb74003SJeeja KP  */
394abb74003SJeeja KP static int skl_tplg_set_module_params(struct snd_soc_dapm_widget *w,
395abb74003SJeeja KP 						struct skl_sst *ctx)
396abb74003SJeeja KP {
397abb74003SJeeja KP 	int i, ret;
398abb74003SJeeja KP 	struct skl_module_cfg *mconfig = w->priv;
399abb74003SJeeja KP 	const struct snd_kcontrol_new *k;
400abb74003SJeeja KP 	struct soc_bytes_ext *sb;
401abb74003SJeeja KP 	struct skl_algo_data *bc;
402abb74003SJeeja KP 	struct skl_specific_cfg *sp_cfg;
403abb74003SJeeja KP 
404abb74003SJeeja KP 	if (mconfig->formats_config.caps_size > 0 &&
4054ced1827SJeeja KP 		mconfig->formats_config.set_params == SKL_PARAM_SET) {
406abb74003SJeeja KP 		sp_cfg = &mconfig->formats_config;
407abb74003SJeeja KP 		ret = skl_set_module_params(ctx, sp_cfg->caps,
408abb74003SJeeja KP 					sp_cfg->caps_size,
409abb74003SJeeja KP 					sp_cfg->param_id, mconfig);
410abb74003SJeeja KP 		if (ret < 0)
411abb74003SJeeja KP 			return ret;
412abb74003SJeeja KP 	}
413abb74003SJeeja KP 
414abb74003SJeeja KP 	for (i = 0; i < w->num_kcontrols; i++) {
415abb74003SJeeja KP 		k = &w->kcontrol_news[i];
416abb74003SJeeja KP 		if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
417abb74003SJeeja KP 			sb = (void *) k->private_value;
418abb74003SJeeja KP 			bc = (struct skl_algo_data *)sb->dobj.private;
419abb74003SJeeja KP 
4204ced1827SJeeja KP 			if (bc->set_params == SKL_PARAM_SET) {
421abb74003SJeeja KP 				ret = skl_set_module_params(ctx,
422abb74003SJeeja KP 						(u32 *)bc->params, bc->max,
423abb74003SJeeja KP 						bc->param_id, mconfig);
424abb74003SJeeja KP 				if (ret < 0)
425abb74003SJeeja KP 					return ret;
426abb74003SJeeja KP 			}
427abb74003SJeeja KP 		}
428abb74003SJeeja KP 	}
429abb74003SJeeja KP 
430abb74003SJeeja KP 	return 0;
431abb74003SJeeja KP }
432abb74003SJeeja KP 
433abb74003SJeeja KP /*
434abb74003SJeeja KP  * some module param can set from user control and this is required as
435abb74003SJeeja KP  * when module is initailzed. if module param is required in init it is
436abb74003SJeeja KP  * identifed by set_param flag. if set_param flag is not set, then this
437abb74003SJeeja KP  * parameter needs to set as part of module init.
438abb74003SJeeja KP  */
439abb74003SJeeja KP static int skl_tplg_set_module_init_data(struct snd_soc_dapm_widget *w)
440abb74003SJeeja KP {
441abb74003SJeeja KP 	const struct snd_kcontrol_new *k;
442abb74003SJeeja KP 	struct soc_bytes_ext *sb;
443abb74003SJeeja KP 	struct skl_algo_data *bc;
444abb74003SJeeja KP 	struct skl_module_cfg *mconfig = w->priv;
445abb74003SJeeja KP 	int i;
446abb74003SJeeja KP 
447abb74003SJeeja KP 	for (i = 0; i < w->num_kcontrols; i++) {
448abb74003SJeeja KP 		k = &w->kcontrol_news[i];
449abb74003SJeeja KP 		if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
450abb74003SJeeja KP 			sb = (struct soc_bytes_ext *)k->private_value;
451abb74003SJeeja KP 			bc = (struct skl_algo_data *)sb->dobj.private;
452abb74003SJeeja KP 
4534ced1827SJeeja KP 			if (bc->set_params != SKL_PARAM_INIT)
454abb74003SJeeja KP 				continue;
455abb74003SJeeja KP 
456abb74003SJeeja KP 			mconfig->formats_config.caps = (u32 *)&bc->params;
457abb74003SJeeja KP 			mconfig->formats_config.caps_size = bc->max;
458abb74003SJeeja KP 
459abb74003SJeeja KP 			break;
460abb74003SJeeja KP 		}
461abb74003SJeeja KP 	}
462abb74003SJeeja KP 
463abb74003SJeeja KP 	return 0;
464abb74003SJeeja KP }
465abb74003SJeeja KP 
466abb74003SJeeja KP /*
467e4e2d2f4SJeeja KP  * Inside a pipe instance, we can have various modules. These modules need
468e4e2d2f4SJeeja KP  * to instantiated in DSP by invoking INIT_MODULE IPC, which is achieved by
469e4e2d2f4SJeeja KP  * skl_init_module() routine, so invoke that for all modules in a pipeline
470e4e2d2f4SJeeja KP  */
471e4e2d2f4SJeeja KP static int
472e4e2d2f4SJeeja KP skl_tplg_init_pipe_modules(struct skl *skl, struct skl_pipe *pipe)
473e4e2d2f4SJeeja KP {
474e4e2d2f4SJeeja KP 	struct skl_pipe_module *w_module;
475e4e2d2f4SJeeja KP 	struct snd_soc_dapm_widget *w;
476e4e2d2f4SJeeja KP 	struct skl_module_cfg *mconfig;
477e4e2d2f4SJeeja KP 	struct skl_sst *ctx = skl->skl_sst;
478e4e2d2f4SJeeja KP 	int ret = 0;
479e4e2d2f4SJeeja KP 
480e4e2d2f4SJeeja KP 	list_for_each_entry(w_module, &pipe->w_list, node) {
481e4e2d2f4SJeeja KP 		w = w_module->w;
482e4e2d2f4SJeeja KP 		mconfig = w->priv;
483e4e2d2f4SJeeja KP 
484e4e2d2f4SJeeja KP 		/* check resource available */
4859ba8ffefSDharageswari.R 		if (!skl_is_pipe_mcps_avail(skl, mconfig))
486e4e2d2f4SJeeja KP 			return -ENOMEM;
487e4e2d2f4SJeeja KP 
4886c5768b3SDharageswari R 		if (mconfig->is_loadable && ctx->dsp->fw_ops.load_mod) {
4896c5768b3SDharageswari R 			ret = ctx->dsp->fw_ops.load_mod(ctx->dsp,
4906c5768b3SDharageswari R 				mconfig->id.module_id, mconfig->guid);
4916c5768b3SDharageswari R 			if (ret < 0)
4926c5768b3SDharageswari R 				return ret;
493*d643678bSJeeja KP 
494*d643678bSJeeja KP 			mconfig->m_state = SKL_MODULE_LOADED;
4956c5768b3SDharageswari R 		}
4966c5768b3SDharageswari R 
4972d1419a3SJeeja KP 		/* update blob if blob is null for be with default value */
4982d1419a3SJeeja KP 		skl_tplg_update_be_blob(w, ctx);
4992d1419a3SJeeja KP 
500f7590d4fSJeeja KP 		/*
501f7590d4fSJeeja KP 		 * apply fix/conversion to module params based on
502f7590d4fSJeeja KP 		 * FE/BE params
503f7590d4fSJeeja KP 		 */
504f7590d4fSJeeja KP 		skl_tplg_update_module_params(w, ctx);
505abb74003SJeeja KP 
506abb74003SJeeja KP 		skl_tplg_set_module_init_data(w);
5079939a9c3SJeeja KP 		ret = skl_init_module(ctx, mconfig);
508e4e2d2f4SJeeja KP 		if (ret < 0)
509e4e2d2f4SJeeja KP 			return ret;
510abb74003SJeeja KP 
511abb74003SJeeja KP 		ret = skl_tplg_set_module_params(w, ctx);
512e4e2d2f4SJeeja KP 		if (ret < 0)
513e4e2d2f4SJeeja KP 			return ret;
5149ba8ffefSDharageswari.R 		skl_tplg_alloc_pipe_mcps(skl, mconfig);
515e4e2d2f4SJeeja KP 	}
516e4e2d2f4SJeeja KP 
517e4e2d2f4SJeeja KP 	return 0;
518e4e2d2f4SJeeja KP }
519d93f8e55SVinod Koul 
5206c5768b3SDharageswari R static int skl_tplg_unload_pipe_modules(struct skl_sst *ctx,
5216c5768b3SDharageswari R 	 struct skl_pipe *pipe)
5226c5768b3SDharageswari R {
5236c5768b3SDharageswari R 	struct skl_pipe_module *w_module = NULL;
5246c5768b3SDharageswari R 	struct skl_module_cfg *mconfig = NULL;
5256c5768b3SDharageswari R 
5266c5768b3SDharageswari R 	list_for_each_entry(w_module, &pipe->w_list, node) {
5276c5768b3SDharageswari R 		mconfig  = w_module->w->priv;
5286c5768b3SDharageswari R 
529*d643678bSJeeja KP 		if (mconfig->is_loadable && ctx->dsp->fw_ops.unload_mod &&
530*d643678bSJeeja KP 			mconfig->m_state > SKL_MODULE_UNINIT)
5316c5768b3SDharageswari R 			return ctx->dsp->fw_ops.unload_mod(ctx->dsp,
5326c5768b3SDharageswari R 						mconfig->id.module_id);
5336c5768b3SDharageswari R 	}
5346c5768b3SDharageswari R 
5356c5768b3SDharageswari R 	/* no modules to unload in this path, so return */
5366c5768b3SDharageswari R 	return 0;
5376c5768b3SDharageswari R }
5386c5768b3SDharageswari R 
539d93f8e55SVinod Koul /*
540d93f8e55SVinod Koul  * Mixer module represents a pipeline. So in the Pre-PMU event of mixer we
541d93f8e55SVinod Koul  * need create the pipeline. So we do following:
542d93f8e55SVinod Koul  *   - check the resources
543d93f8e55SVinod Koul  *   - Create the pipeline
544d93f8e55SVinod Koul  *   - Initialize the modules in pipeline
545d93f8e55SVinod Koul  *   - finally bind all modules together
546d93f8e55SVinod Koul  */
547d93f8e55SVinod Koul static int skl_tplg_mixer_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
548d93f8e55SVinod Koul 							struct skl *skl)
549d93f8e55SVinod Koul {
550d93f8e55SVinod Koul 	int ret;
551d93f8e55SVinod Koul 	struct skl_module_cfg *mconfig = w->priv;
552d93f8e55SVinod Koul 	struct skl_pipe_module *w_module;
553d93f8e55SVinod Koul 	struct skl_pipe *s_pipe = mconfig->pipe;
554d93f8e55SVinod Koul 	struct skl_module_cfg *src_module = NULL, *dst_module;
555d93f8e55SVinod Koul 	struct skl_sst *ctx = skl->skl_sst;
556d93f8e55SVinod Koul 
557d93f8e55SVinod Koul 	/* check resource available */
5589ba8ffefSDharageswari.R 	if (!skl_is_pipe_mcps_avail(skl, mconfig))
559d93f8e55SVinod Koul 		return -EBUSY;
560d93f8e55SVinod Koul 
5619ba8ffefSDharageswari.R 	if (!skl_is_pipe_mem_avail(skl, mconfig))
562d93f8e55SVinod Koul 		return -ENOMEM;
563d93f8e55SVinod Koul 
564d93f8e55SVinod Koul 	/*
565d93f8e55SVinod Koul 	 * Create a list of modules for pipe.
566d93f8e55SVinod Koul 	 * This list contains modules from source to sink
567d93f8e55SVinod Koul 	 */
568d93f8e55SVinod Koul 	ret = skl_create_pipeline(ctx, mconfig->pipe);
569d93f8e55SVinod Koul 	if (ret < 0)
570d93f8e55SVinod Koul 		return ret;
571d93f8e55SVinod Koul 
572d93f8e55SVinod Koul 	/*
573d93f8e55SVinod Koul 	 * we create a w_list of all widgets in that pipe. This list is not
574d93f8e55SVinod Koul 	 * freed on PMD event as widgets within a pipe are static. This
575d93f8e55SVinod Koul 	 * saves us cycles to get widgets in pipe every time.
576d93f8e55SVinod Koul 	 *
577d93f8e55SVinod Koul 	 * So if we have already initialized all the widgets of a pipeline
578d93f8e55SVinod Koul 	 * we skip, so check for list_empty and create the list if empty
579d93f8e55SVinod Koul 	 */
580d93f8e55SVinod Koul 	if (list_empty(&s_pipe->w_list)) {
581d93f8e55SVinod Koul 		ret = skl_tplg_alloc_pipe_widget(ctx->dev, w, s_pipe);
582d93f8e55SVinod Koul 		if (ret < 0)
583d93f8e55SVinod Koul 			return ret;
584d93f8e55SVinod Koul 	}
585d93f8e55SVinod Koul 
586d93f8e55SVinod Koul 	/* Init all pipe modules from source to sink */
587d93f8e55SVinod Koul 	ret = skl_tplg_init_pipe_modules(skl, s_pipe);
588d93f8e55SVinod Koul 	if (ret < 0)
589d93f8e55SVinod Koul 		return ret;
590d93f8e55SVinod Koul 
591d93f8e55SVinod Koul 	/* Bind modules from source to sink */
592d93f8e55SVinod Koul 	list_for_each_entry(w_module, &s_pipe->w_list, node) {
593d93f8e55SVinod Koul 		dst_module = w_module->w->priv;
594d93f8e55SVinod Koul 
595d93f8e55SVinod Koul 		if (src_module == NULL) {
596d93f8e55SVinod Koul 			src_module = dst_module;
597d93f8e55SVinod Koul 			continue;
598d93f8e55SVinod Koul 		}
599d93f8e55SVinod Koul 
600d93f8e55SVinod Koul 		ret = skl_bind_modules(ctx, src_module, dst_module);
601d93f8e55SVinod Koul 		if (ret < 0)
602d93f8e55SVinod Koul 			return ret;
603d93f8e55SVinod Koul 
604d93f8e55SVinod Koul 		src_module = dst_module;
605d93f8e55SVinod Koul 	}
606d93f8e55SVinod Koul 
6079ba8ffefSDharageswari.R 	skl_tplg_alloc_pipe_mem(skl, mconfig);
6089ba8ffefSDharageswari.R 	skl_tplg_alloc_pipe_mcps(skl, mconfig);
6099ba8ffefSDharageswari.R 
610d93f8e55SVinod Koul 	return 0;
611d93f8e55SVinod Koul }
612d93f8e55SVinod Koul 
613cc6a4044SJeeja KP /*
614cc6a4044SJeeja KP  * Some modules require params to be set after the module is bound to
615cc6a4044SJeeja KP  * all pins connected.
616cc6a4044SJeeja KP  *
617cc6a4044SJeeja KP  * The module provider initializes set_param flag for such modules and we
618cc6a4044SJeeja KP  * send params after binding
619cc6a4044SJeeja KP  */
620cc6a4044SJeeja KP static int skl_tplg_set_module_bind_params(struct snd_soc_dapm_widget *w,
621cc6a4044SJeeja KP 			struct skl_module_cfg *mcfg, struct skl_sst *ctx)
622cc6a4044SJeeja KP {
623cc6a4044SJeeja KP 	int i, ret;
624cc6a4044SJeeja KP 	struct skl_module_cfg *mconfig = w->priv;
625cc6a4044SJeeja KP 	const struct snd_kcontrol_new *k;
626cc6a4044SJeeja KP 	struct soc_bytes_ext *sb;
627cc6a4044SJeeja KP 	struct skl_algo_data *bc;
628cc6a4044SJeeja KP 	struct skl_specific_cfg *sp_cfg;
629cc6a4044SJeeja KP 
630cc6a4044SJeeja KP 	/*
631cc6a4044SJeeja KP 	 * check all out/in pins are in bind state.
632cc6a4044SJeeja KP 	 * if so set the module param
633cc6a4044SJeeja KP 	 */
634cc6a4044SJeeja KP 	for (i = 0; i < mcfg->max_out_queue; i++) {
635cc6a4044SJeeja KP 		if (mcfg->m_out_pin[i].pin_state != SKL_PIN_BIND_DONE)
636cc6a4044SJeeja KP 			return 0;
637cc6a4044SJeeja KP 	}
638cc6a4044SJeeja KP 
639cc6a4044SJeeja KP 	for (i = 0; i < mcfg->max_in_queue; i++) {
640cc6a4044SJeeja KP 		if (mcfg->m_in_pin[i].pin_state != SKL_PIN_BIND_DONE)
641cc6a4044SJeeja KP 			return 0;
642cc6a4044SJeeja KP 	}
643cc6a4044SJeeja KP 
644cc6a4044SJeeja KP 	if (mconfig->formats_config.caps_size > 0 &&
645cc6a4044SJeeja KP 		mconfig->formats_config.set_params == SKL_PARAM_BIND) {
646cc6a4044SJeeja KP 		sp_cfg = &mconfig->formats_config;
647cc6a4044SJeeja KP 		ret = skl_set_module_params(ctx, sp_cfg->caps,
648cc6a4044SJeeja KP 					sp_cfg->caps_size,
649cc6a4044SJeeja KP 					sp_cfg->param_id, mconfig);
650cc6a4044SJeeja KP 		if (ret < 0)
651cc6a4044SJeeja KP 			return ret;
652cc6a4044SJeeja KP 	}
653cc6a4044SJeeja KP 
654cc6a4044SJeeja KP 	for (i = 0; i < w->num_kcontrols; i++) {
655cc6a4044SJeeja KP 		k = &w->kcontrol_news[i];
656cc6a4044SJeeja KP 		if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
657cc6a4044SJeeja KP 			sb = (void *) k->private_value;
658cc6a4044SJeeja KP 			bc = (struct skl_algo_data *)sb->dobj.private;
659cc6a4044SJeeja KP 
660cc6a4044SJeeja KP 			if (bc->set_params == SKL_PARAM_BIND) {
661cc6a4044SJeeja KP 				ret = skl_set_module_params(ctx,
662cc6a4044SJeeja KP 						(u32 *)bc->params, bc->max,
663cc6a4044SJeeja KP 						bc->param_id, mconfig);
664cc6a4044SJeeja KP 				if (ret < 0)
665cc6a4044SJeeja KP 					return ret;
666cc6a4044SJeeja KP 			}
667cc6a4044SJeeja KP 		}
668cc6a4044SJeeja KP 	}
669cc6a4044SJeeja KP 
670cc6a4044SJeeja KP 	return 0;
671cc6a4044SJeeja KP }
672cc6a4044SJeeja KP 
6738724ff17SJeeja KP static int skl_tplg_bind_sinks(struct snd_soc_dapm_widget *w,
6748724ff17SJeeja KP 				struct skl *skl,
6756bd4cf85SJeeja KP 				struct snd_soc_dapm_widget *src_w,
6768724ff17SJeeja KP 				struct skl_module_cfg *src_mconfig)
677d93f8e55SVinod Koul {
678d93f8e55SVinod Koul 	struct snd_soc_dapm_path *p;
6790ed95d76SJeeja KP 	struct snd_soc_dapm_widget *sink = NULL, *next_sink = NULL;
6808724ff17SJeeja KP 	struct skl_module_cfg *sink_mconfig;
681d93f8e55SVinod Koul 	struct skl_sst *ctx = skl->skl_sst;
6828724ff17SJeeja KP 	int ret;
683d93f8e55SVinod Koul 
6848724ff17SJeeja KP 	snd_soc_dapm_widget_for_each_sink_path(w, p) {
685d93f8e55SVinod Koul 		if (!p->connect)
686d93f8e55SVinod Koul 			continue;
687d93f8e55SVinod Koul 
688d93f8e55SVinod Koul 		dev_dbg(ctx->dev, "%s: src widget=%s\n", __func__, w->name);
689d93f8e55SVinod Koul 		dev_dbg(ctx->dev, "%s: sink widget=%s\n", __func__, p->sink->name);
690d93f8e55SVinod Koul 
6910ed95d76SJeeja KP 		next_sink = p->sink;
6926bd4cf85SJeeja KP 
6936bd4cf85SJeeja KP 		if (!is_skl_dsp_widget_type(p->sink))
6946bd4cf85SJeeja KP 			return skl_tplg_bind_sinks(p->sink, skl, src_w, src_mconfig);
6956bd4cf85SJeeja KP 
696d93f8e55SVinod Koul 		/*
697d93f8e55SVinod Koul 		 * here we will check widgets in sink pipelines, so that
698d93f8e55SVinod Koul 		 * can be any widgets type and we are only interested if
699d93f8e55SVinod Koul 		 * they are ones used for SKL so check that first
700d93f8e55SVinod Koul 		 */
701d93f8e55SVinod Koul 		if ((p->sink->priv != NULL) &&
702d93f8e55SVinod Koul 					is_skl_dsp_widget_type(p->sink)) {
703d93f8e55SVinod Koul 
704d93f8e55SVinod Koul 			sink = p->sink;
705d93f8e55SVinod Koul 			sink_mconfig = sink->priv;
706d93f8e55SVinod Koul 
707cc6a4044SJeeja KP 			if (src_mconfig->m_state == SKL_MODULE_UNINIT ||
708cc6a4044SJeeja KP 				sink_mconfig->m_state == SKL_MODULE_UNINIT)
709cc6a4044SJeeja KP 				continue;
710cc6a4044SJeeja KP 
711d93f8e55SVinod Koul 			/* Bind source to sink, mixin is always source */
712d93f8e55SVinod Koul 			ret = skl_bind_modules(ctx, src_mconfig, sink_mconfig);
713d93f8e55SVinod Koul 			if (ret)
714d93f8e55SVinod Koul 				return ret;
715d93f8e55SVinod Koul 
716cc6a4044SJeeja KP 			/* set module params after bind */
717cc6a4044SJeeja KP 			skl_tplg_set_module_bind_params(src_w, src_mconfig, ctx);
718cc6a4044SJeeja KP 			skl_tplg_set_module_bind_params(sink, sink_mconfig, ctx);
719cc6a4044SJeeja KP 
720d93f8e55SVinod Koul 			/* Start sinks pipe first */
721d93f8e55SVinod Koul 			if (sink_mconfig->pipe->state != SKL_PIPE_STARTED) {
722d1730c3dSJeeja KP 				if (sink_mconfig->pipe->conn_type !=
723d1730c3dSJeeja KP 							SKL_PIPE_CONN_TYPE_FE)
724d1730c3dSJeeja KP 					ret = skl_run_pipe(ctx,
725d1730c3dSJeeja KP 							sink_mconfig->pipe);
726d93f8e55SVinod Koul 				if (ret)
727d93f8e55SVinod Koul 					return ret;
728d93f8e55SVinod Koul 			}
729d93f8e55SVinod Koul 		}
730d93f8e55SVinod Koul 	}
731d93f8e55SVinod Koul 
7328724ff17SJeeja KP 	if (!sink)
7336bd4cf85SJeeja KP 		return skl_tplg_bind_sinks(next_sink, skl, src_w, src_mconfig);
7348724ff17SJeeja KP 
7358724ff17SJeeja KP 	return 0;
7368724ff17SJeeja KP }
7378724ff17SJeeja KP 
738d93f8e55SVinod Koul /*
739d93f8e55SVinod Koul  * A PGA represents a module in a pipeline. So in the Pre-PMU event of PGA
740d93f8e55SVinod Koul  * we need to do following:
741d93f8e55SVinod Koul  *   - Bind to sink pipeline
742d93f8e55SVinod Koul  *      Since the sink pipes can be running and we don't get mixer event on
743d93f8e55SVinod Koul  *      connect for already running mixer, we need to find the sink pipes
744d93f8e55SVinod Koul  *      here and bind to them. This way dynamic connect works.
745d93f8e55SVinod Koul  *   - Start sink pipeline, if not running
746d93f8e55SVinod Koul  *   - Then run current pipe
747d93f8e55SVinod Koul  */
748d93f8e55SVinod Koul static int skl_tplg_pga_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
749d93f8e55SVinod Koul 								struct skl *skl)
750d93f8e55SVinod Koul {
7518724ff17SJeeja KP 	struct skl_module_cfg *src_mconfig;
752d93f8e55SVinod Koul 	struct skl_sst *ctx = skl->skl_sst;
753d93f8e55SVinod Koul 	int ret = 0;
754d93f8e55SVinod Koul 
7558724ff17SJeeja KP 	src_mconfig = w->priv;
756d93f8e55SVinod Koul 
757d93f8e55SVinod Koul 	/*
758d93f8e55SVinod Koul 	 * find which sink it is connected to, bind with the sink,
759d93f8e55SVinod Koul 	 * if sink is not started, start sink pipe first, then start
760d93f8e55SVinod Koul 	 * this pipe
761d93f8e55SVinod Koul 	 */
7626bd4cf85SJeeja KP 	ret = skl_tplg_bind_sinks(w, skl, w, src_mconfig);
7638724ff17SJeeja KP 	if (ret)
7648724ff17SJeeja KP 		return ret;
7658724ff17SJeeja KP 
766d93f8e55SVinod Koul 	/* Start source pipe last after starting all sinks */
767d1730c3dSJeeja KP 	if (src_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
768d1730c3dSJeeja KP 		return skl_run_pipe(ctx, src_mconfig->pipe);
769d93f8e55SVinod Koul 
770d93f8e55SVinod Koul 	return 0;
771d93f8e55SVinod Koul }
772d93f8e55SVinod Koul 
7738724ff17SJeeja KP static struct snd_soc_dapm_widget *skl_get_src_dsp_widget(
7748724ff17SJeeja KP 		struct snd_soc_dapm_widget *w, struct skl *skl)
7758724ff17SJeeja KP {
7768724ff17SJeeja KP 	struct snd_soc_dapm_path *p;
7778724ff17SJeeja KP 	struct snd_soc_dapm_widget *src_w = NULL;
7788724ff17SJeeja KP 	struct skl_sst *ctx = skl->skl_sst;
7798724ff17SJeeja KP 
780d93f8e55SVinod Koul 	snd_soc_dapm_widget_for_each_source_path(w, p) {
7818724ff17SJeeja KP 		src_w = p->source;
782d93f8e55SVinod Koul 		if (!p->connect)
783d93f8e55SVinod Koul 			continue;
784d93f8e55SVinod Koul 
7858724ff17SJeeja KP 		dev_dbg(ctx->dev, "sink widget=%s\n", w->name);
7868724ff17SJeeja KP 		dev_dbg(ctx->dev, "src widget=%s\n", p->source->name);
787d93f8e55SVinod Koul 
788d93f8e55SVinod Koul 		/*
7898724ff17SJeeja KP 		 * here we will check widgets in sink pipelines, so that can
7908724ff17SJeeja KP 		 * be any widgets type and we are only interested if they are
7918724ff17SJeeja KP 		 * ones used for SKL so check that first
792d93f8e55SVinod Koul 		 */
7938724ff17SJeeja KP 		if ((p->source->priv != NULL) &&
7948724ff17SJeeja KP 					is_skl_dsp_widget_type(p->source)) {
7958724ff17SJeeja KP 			return p->source;
796d93f8e55SVinod Koul 		}
797d93f8e55SVinod Koul 	}
798d93f8e55SVinod Koul 
7998724ff17SJeeja KP 	if (src_w != NULL)
8008724ff17SJeeja KP 		return skl_get_src_dsp_widget(src_w, skl);
801d93f8e55SVinod Koul 
8028724ff17SJeeja KP 	return NULL;
803d93f8e55SVinod Koul }
804d93f8e55SVinod Koul 
805d93f8e55SVinod Koul /*
806d93f8e55SVinod Koul  * in the Post-PMU event of mixer we need to do following:
807d93f8e55SVinod Koul  *   - Check if this pipe is running
808d93f8e55SVinod Koul  *   - if not, then
809d93f8e55SVinod Koul  *	- bind this pipeline to its source pipeline
810d93f8e55SVinod Koul  *	  if source pipe is already running, this means it is a dynamic
811d93f8e55SVinod Koul  *	  connection and we need to bind only to that pipe
812d93f8e55SVinod Koul  *	- start this pipeline
813d93f8e55SVinod Koul  */
814d93f8e55SVinod Koul static int skl_tplg_mixer_dapm_post_pmu_event(struct snd_soc_dapm_widget *w,
815d93f8e55SVinod Koul 							struct skl *skl)
816d93f8e55SVinod Koul {
817d93f8e55SVinod Koul 	int ret = 0;
818d93f8e55SVinod Koul 	struct snd_soc_dapm_widget *source, *sink;
819d93f8e55SVinod Koul 	struct skl_module_cfg *src_mconfig, *sink_mconfig;
820d93f8e55SVinod Koul 	struct skl_sst *ctx = skl->skl_sst;
821d93f8e55SVinod Koul 	int src_pipe_started = 0;
822d93f8e55SVinod Koul 
823d93f8e55SVinod Koul 	sink = w;
824d93f8e55SVinod Koul 	sink_mconfig = sink->priv;
825d93f8e55SVinod Koul 
826d93f8e55SVinod Koul 	/*
827d93f8e55SVinod Koul 	 * If source pipe is already started, that means source is driving
828d93f8e55SVinod Koul 	 * one more sink before this sink got connected, Since source is
829d93f8e55SVinod Koul 	 * started, bind this sink to source and start this pipe.
830d93f8e55SVinod Koul 	 */
8318724ff17SJeeja KP 	source = skl_get_src_dsp_widget(w, skl);
8328724ff17SJeeja KP 	if (source != NULL) {
833d93f8e55SVinod Koul 		src_mconfig = source->priv;
834d93f8e55SVinod Koul 		sink_mconfig = sink->priv;
835d93f8e55SVinod Koul 		src_pipe_started = 1;
836d93f8e55SVinod Koul 
837d93f8e55SVinod Koul 		/*
8388724ff17SJeeja KP 		 * check pipe state, then no need to bind or start the
8398724ff17SJeeja KP 		 * pipe
840d93f8e55SVinod Koul 		 */
841d93f8e55SVinod Koul 		if (src_mconfig->pipe->state != SKL_PIPE_STARTED)
842d93f8e55SVinod Koul 			src_pipe_started = 0;
843d93f8e55SVinod Koul 	}
844d93f8e55SVinod Koul 
845d93f8e55SVinod Koul 	if (src_pipe_started) {
846d93f8e55SVinod Koul 		ret = skl_bind_modules(ctx, src_mconfig, sink_mconfig);
847d93f8e55SVinod Koul 		if (ret)
848d93f8e55SVinod Koul 			return ret;
849d93f8e55SVinod Koul 
850cc6a4044SJeeja KP 		/* set module params after bind */
851cc6a4044SJeeja KP 		skl_tplg_set_module_bind_params(source, src_mconfig, ctx);
852cc6a4044SJeeja KP 		skl_tplg_set_module_bind_params(sink, sink_mconfig, ctx);
853cc6a4044SJeeja KP 
854d1730c3dSJeeja KP 		if (sink_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
855d93f8e55SVinod Koul 			ret = skl_run_pipe(ctx, sink_mconfig->pipe);
856d93f8e55SVinod Koul 	}
857d93f8e55SVinod Koul 
858d93f8e55SVinod Koul 	return ret;
859d93f8e55SVinod Koul }
860d93f8e55SVinod Koul 
861d93f8e55SVinod Koul /*
862d93f8e55SVinod Koul  * in the Pre-PMD event of mixer we need to do following:
863d93f8e55SVinod Koul  *   - Stop the pipe
864d93f8e55SVinod Koul  *   - find the source connections and remove that from dapm_path_list
865d93f8e55SVinod Koul  *   - unbind with source pipelines if still connected
866d93f8e55SVinod Koul  */
867d93f8e55SVinod Koul static int skl_tplg_mixer_dapm_pre_pmd_event(struct snd_soc_dapm_widget *w,
868d93f8e55SVinod Koul 							struct skl *skl)
869d93f8e55SVinod Koul {
870d93f8e55SVinod Koul 	struct skl_module_cfg *src_mconfig, *sink_mconfig;
871ce1b5551SJeeja KP 	int ret = 0, i;
872d93f8e55SVinod Koul 	struct skl_sst *ctx = skl->skl_sst;
873d93f8e55SVinod Koul 
874ce1b5551SJeeja KP 	sink_mconfig = w->priv;
875d93f8e55SVinod Koul 
876d93f8e55SVinod Koul 	/* Stop the pipe */
877d93f8e55SVinod Koul 	ret = skl_stop_pipe(ctx, sink_mconfig->pipe);
878d93f8e55SVinod Koul 	if (ret)
879d93f8e55SVinod Koul 		return ret;
880d93f8e55SVinod Koul 
881ce1b5551SJeeja KP 	for (i = 0; i < sink_mconfig->max_in_queue; i++) {
882ce1b5551SJeeja KP 		if (sink_mconfig->m_in_pin[i].pin_state == SKL_PIN_BIND_DONE) {
883ce1b5551SJeeja KP 			src_mconfig = sink_mconfig->m_in_pin[i].tgt_mcfg;
884ce1b5551SJeeja KP 			if (!src_mconfig)
885ce1b5551SJeeja KP 				continue;
886d93f8e55SVinod Koul 			/*
887ce1b5551SJeeja KP 			 * If path_found == 1, that means pmd for source
888ce1b5551SJeeja KP 			 * pipe has not occurred, source is connected to
889ce1b5551SJeeja KP 			 * some other sink. so its responsibility of sink
890ce1b5551SJeeja KP 			 * to unbind itself from source.
891d93f8e55SVinod Koul 			 */
892d93f8e55SVinod Koul 			ret = skl_stop_pipe(ctx, src_mconfig->pipe);
893d93f8e55SVinod Koul 			if (ret < 0)
894d93f8e55SVinod Koul 				return ret;
895d93f8e55SVinod Koul 
896ce1b5551SJeeja KP 			ret = skl_unbind_modules(ctx,
897ce1b5551SJeeja KP 						src_mconfig, sink_mconfig);
898ce1b5551SJeeja KP 		}
899d93f8e55SVinod Koul 	}
900d93f8e55SVinod Koul 
901d93f8e55SVinod Koul 	return ret;
902d93f8e55SVinod Koul }
903d93f8e55SVinod Koul 
904d93f8e55SVinod Koul /*
905d93f8e55SVinod Koul  * in the Post-PMD event of mixer we need to do following:
906d93f8e55SVinod Koul  *   - Free the mcps used
907d93f8e55SVinod Koul  *   - Free the mem used
908d93f8e55SVinod Koul  *   - Unbind the modules within the pipeline
909d93f8e55SVinod Koul  *   - Delete the pipeline (modules are not required to be explicitly
910d93f8e55SVinod Koul  *     deleted, pipeline delete is enough here
911d93f8e55SVinod Koul  */
912d93f8e55SVinod Koul static int skl_tplg_mixer_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
913d93f8e55SVinod Koul 							struct skl *skl)
914d93f8e55SVinod Koul {
915d93f8e55SVinod Koul 	struct skl_module_cfg *mconfig = w->priv;
916d93f8e55SVinod Koul 	struct skl_pipe_module *w_module;
917d93f8e55SVinod Koul 	struct skl_module_cfg *src_module = NULL, *dst_module;
918d93f8e55SVinod Koul 	struct skl_sst *ctx = skl->skl_sst;
919d93f8e55SVinod Koul 	struct skl_pipe *s_pipe = mconfig->pipe;
920d93f8e55SVinod Koul 	int ret = 0;
921d93f8e55SVinod Koul 
922d93f8e55SVinod Koul 	skl_tplg_free_pipe_mcps(skl, mconfig);
92365976878SVinod Koul 	skl_tplg_free_pipe_mem(skl, mconfig);
924d93f8e55SVinod Koul 
925d93f8e55SVinod Koul 	list_for_each_entry(w_module, &s_pipe->w_list, node) {
926d93f8e55SVinod Koul 		dst_module = w_module->w->priv;
927d93f8e55SVinod Koul 
9287ae3cb15SVinod Koul 		skl_tplg_free_pipe_mcps(skl, dst_module);
929d93f8e55SVinod Koul 		if (src_module == NULL) {
930d93f8e55SVinod Koul 			src_module = dst_module;
931d93f8e55SVinod Koul 			continue;
932d93f8e55SVinod Koul 		}
933d93f8e55SVinod Koul 
9347ca42f5aSGuneshwor Singh 		skl_unbind_modules(ctx, src_module, dst_module);
935d93f8e55SVinod Koul 		src_module = dst_module;
936d93f8e55SVinod Koul 	}
937d93f8e55SVinod Koul 
938d93f8e55SVinod Koul 	ret = skl_delete_pipe(ctx, mconfig->pipe);
939d93f8e55SVinod Koul 
9406c5768b3SDharageswari R 	return skl_tplg_unload_pipe_modules(ctx, s_pipe);
941d93f8e55SVinod Koul }
942d93f8e55SVinod Koul 
943d93f8e55SVinod Koul /*
944d93f8e55SVinod Koul  * in the Post-PMD event of PGA we need to do following:
945d93f8e55SVinod Koul  *   - Free the mcps used
946d93f8e55SVinod Koul  *   - Stop the pipeline
947d93f8e55SVinod Koul  *   - In source pipe is connected, unbind with source pipelines
948d93f8e55SVinod Koul  */
949d93f8e55SVinod Koul static int skl_tplg_pga_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
950d93f8e55SVinod Koul 								struct skl *skl)
951d93f8e55SVinod Koul {
952d93f8e55SVinod Koul 	struct skl_module_cfg *src_mconfig, *sink_mconfig;
953ce1b5551SJeeja KP 	int ret = 0, i;
954d93f8e55SVinod Koul 	struct skl_sst *ctx = skl->skl_sst;
955d93f8e55SVinod Koul 
956ce1b5551SJeeja KP 	src_mconfig = w->priv;
957d93f8e55SVinod Koul 
958d93f8e55SVinod Koul 	/* Stop the pipe since this is a mixin module */
959d93f8e55SVinod Koul 	ret = skl_stop_pipe(ctx, src_mconfig->pipe);
960d93f8e55SVinod Koul 	if (ret)
961d93f8e55SVinod Koul 		return ret;
962d93f8e55SVinod Koul 
963ce1b5551SJeeja KP 	for (i = 0; i < src_mconfig->max_out_queue; i++) {
964ce1b5551SJeeja KP 		if (src_mconfig->m_out_pin[i].pin_state == SKL_PIN_BIND_DONE) {
965ce1b5551SJeeja KP 			sink_mconfig = src_mconfig->m_out_pin[i].tgt_mcfg;
966ce1b5551SJeeja KP 			if (!sink_mconfig)
967ce1b5551SJeeja KP 				continue;
968d93f8e55SVinod Koul 			/*
969ce1b5551SJeeja KP 			 * This is a connecter and if path is found that means
970d93f8e55SVinod Koul 			 * unbind between source and sink has not happened yet
971d93f8e55SVinod Koul 			 */
972ce1b5551SJeeja KP 			ret = skl_unbind_modules(ctx, src_mconfig,
973ce1b5551SJeeja KP 							sink_mconfig);
974ce1b5551SJeeja KP 		}
975d93f8e55SVinod Koul 	}
976d93f8e55SVinod Koul 
977d93f8e55SVinod Koul 	return ret;
978d93f8e55SVinod Koul }
979d93f8e55SVinod Koul 
980d93f8e55SVinod Koul /*
981d93f8e55SVinod Koul  * In modelling, we assume there will be ONLY one mixer in a pipeline.  If
982d93f8e55SVinod Koul  * mixer is not required then it is treated as static mixer aka vmixer with
983d93f8e55SVinod Koul  * a hard path to source module
984d93f8e55SVinod Koul  * So we don't need to check if source is started or not as hard path puts
985d93f8e55SVinod Koul  * dependency on each other
986d93f8e55SVinod Koul  */
987d93f8e55SVinod Koul static int skl_tplg_vmixer_event(struct snd_soc_dapm_widget *w,
988d93f8e55SVinod Koul 				struct snd_kcontrol *k, int event)
989d93f8e55SVinod Koul {
990d93f8e55SVinod Koul 	struct snd_soc_dapm_context *dapm = w->dapm;
991d93f8e55SVinod Koul 	struct skl *skl = get_skl_ctx(dapm->dev);
992d93f8e55SVinod Koul 
993d93f8e55SVinod Koul 	switch (event) {
994d93f8e55SVinod Koul 	case SND_SOC_DAPM_PRE_PMU:
995d93f8e55SVinod Koul 		return skl_tplg_mixer_dapm_pre_pmu_event(w, skl);
996d93f8e55SVinod Koul 
997de1fedf2SJeeja KP 	case SND_SOC_DAPM_POST_PMU:
998de1fedf2SJeeja KP 		return skl_tplg_mixer_dapm_post_pmu_event(w, skl);
999de1fedf2SJeeja KP 
1000de1fedf2SJeeja KP 	case SND_SOC_DAPM_PRE_PMD:
1001de1fedf2SJeeja KP 		return skl_tplg_mixer_dapm_pre_pmd_event(w, skl);
1002de1fedf2SJeeja KP 
1003d93f8e55SVinod Koul 	case SND_SOC_DAPM_POST_PMD:
1004d93f8e55SVinod Koul 		return skl_tplg_mixer_dapm_post_pmd_event(w, skl);
1005d93f8e55SVinod Koul 	}
1006d93f8e55SVinod Koul 
1007d93f8e55SVinod Koul 	return 0;
1008d93f8e55SVinod Koul }
1009d93f8e55SVinod Koul 
1010d93f8e55SVinod Koul /*
1011d93f8e55SVinod Koul  * In modelling, we assume there will be ONLY one mixer in a pipeline. If a
1012d93f8e55SVinod Koul  * second one is required that is created as another pipe entity.
1013d93f8e55SVinod Koul  * The mixer is responsible for pipe management and represent a pipeline
1014d93f8e55SVinod Koul  * instance
1015d93f8e55SVinod Koul  */
1016d93f8e55SVinod Koul static int skl_tplg_mixer_event(struct snd_soc_dapm_widget *w,
1017d93f8e55SVinod Koul 				struct snd_kcontrol *k, int event)
1018d93f8e55SVinod Koul {
1019d93f8e55SVinod Koul 	struct snd_soc_dapm_context *dapm = w->dapm;
1020d93f8e55SVinod Koul 	struct skl *skl = get_skl_ctx(dapm->dev);
1021d93f8e55SVinod Koul 
1022d93f8e55SVinod Koul 	switch (event) {
1023d93f8e55SVinod Koul 	case SND_SOC_DAPM_PRE_PMU:
1024d93f8e55SVinod Koul 		return skl_tplg_mixer_dapm_pre_pmu_event(w, skl);
1025d93f8e55SVinod Koul 
1026d93f8e55SVinod Koul 	case SND_SOC_DAPM_POST_PMU:
1027d93f8e55SVinod Koul 		return skl_tplg_mixer_dapm_post_pmu_event(w, skl);
1028d93f8e55SVinod Koul 
1029d93f8e55SVinod Koul 	case SND_SOC_DAPM_PRE_PMD:
1030d93f8e55SVinod Koul 		return skl_tplg_mixer_dapm_pre_pmd_event(w, skl);
1031d93f8e55SVinod Koul 
1032d93f8e55SVinod Koul 	case SND_SOC_DAPM_POST_PMD:
1033d93f8e55SVinod Koul 		return skl_tplg_mixer_dapm_post_pmd_event(w, skl);
1034d93f8e55SVinod Koul 	}
1035d93f8e55SVinod Koul 
1036d93f8e55SVinod Koul 	return 0;
1037d93f8e55SVinod Koul }
1038d93f8e55SVinod Koul 
1039d93f8e55SVinod Koul /*
1040d93f8e55SVinod Koul  * In modelling, we assumed rest of the modules in pipeline are PGA. But we
1041d93f8e55SVinod Koul  * are interested in last PGA (leaf PGA) in a pipeline to disconnect with
1042d93f8e55SVinod Koul  * the sink when it is running (two FE to one BE or one FE to two BE)
1043d93f8e55SVinod Koul  * scenarios
1044d93f8e55SVinod Koul  */
1045d93f8e55SVinod Koul static int skl_tplg_pga_event(struct snd_soc_dapm_widget *w,
1046d93f8e55SVinod Koul 			struct snd_kcontrol *k, int event)
1047d93f8e55SVinod Koul 
1048d93f8e55SVinod Koul {
1049d93f8e55SVinod Koul 	struct snd_soc_dapm_context *dapm = w->dapm;
1050d93f8e55SVinod Koul 	struct skl *skl = get_skl_ctx(dapm->dev);
1051d93f8e55SVinod Koul 
1052d93f8e55SVinod Koul 	switch (event) {
1053d93f8e55SVinod Koul 	case SND_SOC_DAPM_PRE_PMU:
1054d93f8e55SVinod Koul 		return skl_tplg_pga_dapm_pre_pmu_event(w, skl);
1055d93f8e55SVinod Koul 
1056d93f8e55SVinod Koul 	case SND_SOC_DAPM_POST_PMD:
1057d93f8e55SVinod Koul 		return skl_tplg_pga_dapm_post_pmd_event(w, skl);
1058d93f8e55SVinod Koul 	}
1059d93f8e55SVinod Koul 
1060d93f8e55SVinod Koul 	return 0;
1061d93f8e55SVinod Koul }
1062cfb0a873SVinod Koul 
1063140adfbaSJeeja KP static int skl_tplg_tlv_control_get(struct snd_kcontrol *kcontrol,
1064140adfbaSJeeja KP 			unsigned int __user *data, unsigned int size)
1065140adfbaSJeeja KP {
1066140adfbaSJeeja KP 	struct soc_bytes_ext *sb =
1067140adfbaSJeeja KP 			(struct soc_bytes_ext *)kcontrol->private_value;
1068140adfbaSJeeja KP 	struct skl_algo_data *bc = (struct skl_algo_data *)sb->dobj.private;
10697d9f2911SOmair M Abdullah 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
10707d9f2911SOmair M Abdullah 	struct skl_module_cfg *mconfig = w->priv;
10717d9f2911SOmair M Abdullah 	struct skl *skl = get_skl_ctx(w->dapm->dev);
10727d9f2911SOmair M Abdullah 
10737d9f2911SOmair M Abdullah 	if (w->power)
10747d9f2911SOmair M Abdullah 		skl_get_module_params(skl->skl_sst, (u32 *)bc->params,
10757d9f2911SOmair M Abdullah 				      bc->max, bc->param_id, mconfig);
1076140adfbaSJeeja KP 
107741556f68SVinod Koul 	/* decrement size for TLV header */
107841556f68SVinod Koul 	size -= 2 * sizeof(u32);
107941556f68SVinod Koul 
108041556f68SVinod Koul 	/* check size as we don't want to send kernel data */
108141556f68SVinod Koul 	if (size > bc->max)
108241556f68SVinod Koul 		size = bc->max;
108341556f68SVinod Koul 
1084140adfbaSJeeja KP 	if (bc->params) {
1085140adfbaSJeeja KP 		if (copy_to_user(data, &bc->param_id, sizeof(u32)))
1086140adfbaSJeeja KP 			return -EFAULT;
1087e8bc3c99SDan Carpenter 		if (copy_to_user(data + 1, &size, sizeof(u32)))
1088140adfbaSJeeja KP 			return -EFAULT;
1089e8bc3c99SDan Carpenter 		if (copy_to_user(data + 2, bc->params, size))
1090140adfbaSJeeja KP 			return -EFAULT;
1091140adfbaSJeeja KP 	}
1092140adfbaSJeeja KP 
1093140adfbaSJeeja KP 	return 0;
1094140adfbaSJeeja KP }
1095140adfbaSJeeja KP 
1096140adfbaSJeeja KP #define SKL_PARAM_VENDOR_ID 0xff
1097140adfbaSJeeja KP 
1098140adfbaSJeeja KP static int skl_tplg_tlv_control_set(struct snd_kcontrol *kcontrol,
1099140adfbaSJeeja KP 			const unsigned int __user *data, unsigned int size)
1100140adfbaSJeeja KP {
1101140adfbaSJeeja KP 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1102140adfbaSJeeja KP 	struct skl_module_cfg *mconfig = w->priv;
1103140adfbaSJeeja KP 	struct soc_bytes_ext *sb =
1104140adfbaSJeeja KP 			(struct soc_bytes_ext *)kcontrol->private_value;
1105140adfbaSJeeja KP 	struct skl_algo_data *ac = (struct skl_algo_data *)sb->dobj.private;
1106140adfbaSJeeja KP 	struct skl *skl = get_skl_ctx(w->dapm->dev);
1107140adfbaSJeeja KP 
1108140adfbaSJeeja KP 	if (ac->params) {
1109140adfbaSJeeja KP 		/*
1110140adfbaSJeeja KP 		 * if the param_is is of type Vendor, firmware expects actual
1111140adfbaSJeeja KP 		 * parameter id and size from the control.
1112140adfbaSJeeja KP 		 */
1113140adfbaSJeeja KP 		if (ac->param_id == SKL_PARAM_VENDOR_ID) {
1114140adfbaSJeeja KP 			if (copy_from_user(ac->params, data, size))
1115140adfbaSJeeja KP 				return -EFAULT;
1116140adfbaSJeeja KP 		} else {
1117140adfbaSJeeja KP 			if (copy_from_user(ac->params,
111865b4bcb8SAlan 					   data + 2, size))
1119140adfbaSJeeja KP 				return -EFAULT;
1120140adfbaSJeeja KP 		}
1121140adfbaSJeeja KP 
1122140adfbaSJeeja KP 		if (w->power)
1123140adfbaSJeeja KP 			return skl_set_module_params(skl->skl_sst,
1124140adfbaSJeeja KP 						(u32 *)ac->params, ac->max,
1125140adfbaSJeeja KP 						ac->param_id, mconfig);
1126140adfbaSJeeja KP 	}
1127140adfbaSJeeja KP 
1128140adfbaSJeeja KP 	return 0;
1129140adfbaSJeeja KP }
1130140adfbaSJeeja KP 
1131cfb0a873SVinod Koul /*
1132cfb0a873SVinod Koul  * The FE params are passed by hw_params of the DAI.
1133cfb0a873SVinod Koul  * On hw_params, the params are stored in Gateway module of the FE and we
1134cfb0a873SVinod Koul  * need to calculate the format in DSP module configuration, that
1135cfb0a873SVinod Koul  * conversion is done here
1136cfb0a873SVinod Koul  */
1137cfb0a873SVinod Koul int skl_tplg_update_pipe_params(struct device *dev,
1138cfb0a873SVinod Koul 			struct skl_module_cfg *mconfig,
1139cfb0a873SVinod Koul 			struct skl_pipe_params *params)
1140cfb0a873SVinod Koul {
1141cfb0a873SVinod Koul 	struct skl_pipe *pipe = mconfig->pipe;
1142cfb0a873SVinod Koul 	struct skl_module_fmt *format = NULL;
1143cfb0a873SVinod Koul 
1144cfb0a873SVinod Koul 	memcpy(pipe->p_params, params, sizeof(*params));
1145cfb0a873SVinod Koul 
1146cfb0a873SVinod Koul 	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK)
11474cd9899fSHardik T Shah 		format = &mconfig->in_fmt[0];
1148cfb0a873SVinod Koul 	else
11494cd9899fSHardik T Shah 		format = &mconfig->out_fmt[0];
1150cfb0a873SVinod Koul 
1151cfb0a873SVinod Koul 	/* set the hw_params */
1152cfb0a873SVinod Koul 	format->s_freq = params->s_freq;
1153cfb0a873SVinod Koul 	format->channels = params->ch;
1154cfb0a873SVinod Koul 	format->valid_bit_depth = skl_get_bit_depth(params->s_fmt);
1155cfb0a873SVinod Koul 
1156cfb0a873SVinod Koul 	/*
1157cfb0a873SVinod Koul 	 * 16 bit is 16 bit container whereas 24 bit is in 32 bit
1158cfb0a873SVinod Koul 	 * container so update bit depth accordingly
1159cfb0a873SVinod Koul 	 */
1160cfb0a873SVinod Koul 	switch (format->valid_bit_depth) {
1161cfb0a873SVinod Koul 	case SKL_DEPTH_16BIT:
1162cfb0a873SVinod Koul 		format->bit_depth = format->valid_bit_depth;
1163cfb0a873SVinod Koul 		break;
1164cfb0a873SVinod Koul 
1165cfb0a873SVinod Koul 	case SKL_DEPTH_24BIT:
11666654f39eSJeeja KP 	case SKL_DEPTH_32BIT:
1167cfb0a873SVinod Koul 		format->bit_depth = SKL_DEPTH_32BIT;
1168cfb0a873SVinod Koul 		break;
1169cfb0a873SVinod Koul 
1170cfb0a873SVinod Koul 	default:
1171cfb0a873SVinod Koul 		dev_err(dev, "Invalid bit depth %x for pipe\n",
1172cfb0a873SVinod Koul 				format->valid_bit_depth);
1173cfb0a873SVinod Koul 		return -EINVAL;
1174cfb0a873SVinod Koul 	}
1175cfb0a873SVinod Koul 
1176cfb0a873SVinod Koul 	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1177cfb0a873SVinod Koul 		mconfig->ibs = (format->s_freq / 1000) *
1178cfb0a873SVinod Koul 				(format->channels) *
1179cfb0a873SVinod Koul 				(format->bit_depth >> 3);
1180cfb0a873SVinod Koul 	} else {
1181cfb0a873SVinod Koul 		mconfig->obs = (format->s_freq / 1000) *
1182cfb0a873SVinod Koul 				(format->channels) *
1183cfb0a873SVinod Koul 				(format->bit_depth >> 3);
1184cfb0a873SVinod Koul 	}
1185cfb0a873SVinod Koul 
1186cfb0a873SVinod Koul 	return 0;
1187cfb0a873SVinod Koul }
1188cfb0a873SVinod Koul 
1189cfb0a873SVinod Koul /*
1190cfb0a873SVinod Koul  * Query the module config for the FE DAI
1191cfb0a873SVinod Koul  * This is used to find the hw_params set for that DAI and apply to FE
1192cfb0a873SVinod Koul  * pipeline
1193cfb0a873SVinod Koul  */
1194cfb0a873SVinod Koul struct skl_module_cfg *
1195cfb0a873SVinod Koul skl_tplg_fe_get_cpr_module(struct snd_soc_dai *dai, int stream)
1196cfb0a873SVinod Koul {
1197cfb0a873SVinod Koul 	struct snd_soc_dapm_widget *w;
1198cfb0a873SVinod Koul 	struct snd_soc_dapm_path *p = NULL;
1199cfb0a873SVinod Koul 
1200cfb0a873SVinod Koul 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1201cfb0a873SVinod Koul 		w = dai->playback_widget;
1202f0900eb2SSubhransu S. Prusty 		snd_soc_dapm_widget_for_each_sink_path(w, p) {
1203cfb0a873SVinod Koul 			if (p->connect && p->sink->power &&
1204a28f51dbSJeeja KP 					!is_skl_dsp_widget_type(p->sink))
1205cfb0a873SVinod Koul 				continue;
1206cfb0a873SVinod Koul 
1207cfb0a873SVinod Koul 			if (p->sink->priv) {
1208cfb0a873SVinod Koul 				dev_dbg(dai->dev, "set params for %s\n",
1209cfb0a873SVinod Koul 						p->sink->name);
1210cfb0a873SVinod Koul 				return p->sink->priv;
1211cfb0a873SVinod Koul 			}
1212cfb0a873SVinod Koul 		}
1213cfb0a873SVinod Koul 	} else {
1214cfb0a873SVinod Koul 		w = dai->capture_widget;
1215f0900eb2SSubhransu S. Prusty 		snd_soc_dapm_widget_for_each_source_path(w, p) {
1216cfb0a873SVinod Koul 			if (p->connect && p->source->power &&
1217a28f51dbSJeeja KP 					!is_skl_dsp_widget_type(p->source))
1218cfb0a873SVinod Koul 				continue;
1219cfb0a873SVinod Koul 
1220cfb0a873SVinod Koul 			if (p->source->priv) {
1221cfb0a873SVinod Koul 				dev_dbg(dai->dev, "set params for %s\n",
1222cfb0a873SVinod Koul 						p->source->name);
1223cfb0a873SVinod Koul 				return p->source->priv;
1224cfb0a873SVinod Koul 			}
1225cfb0a873SVinod Koul 		}
1226cfb0a873SVinod Koul 	}
1227cfb0a873SVinod Koul 
1228cfb0a873SVinod Koul 	return NULL;
1229cfb0a873SVinod Koul }
1230cfb0a873SVinod Koul 
1231718a42b5SDharageswari.R static struct skl_module_cfg *skl_get_mconfig_pb_cpr(
1232718a42b5SDharageswari.R 		struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
1233718a42b5SDharageswari.R {
1234718a42b5SDharageswari.R 	struct snd_soc_dapm_path *p;
1235718a42b5SDharageswari.R 	struct skl_module_cfg *mconfig = NULL;
1236718a42b5SDharageswari.R 
1237718a42b5SDharageswari.R 	snd_soc_dapm_widget_for_each_source_path(w, p) {
1238718a42b5SDharageswari.R 		if (w->endpoints[SND_SOC_DAPM_DIR_OUT] > 0) {
1239718a42b5SDharageswari.R 			if (p->connect &&
1240718a42b5SDharageswari.R 				    (p->sink->id == snd_soc_dapm_aif_out) &&
1241718a42b5SDharageswari.R 				    p->source->priv) {
1242718a42b5SDharageswari.R 				mconfig = p->source->priv;
1243718a42b5SDharageswari.R 				return mconfig;
1244718a42b5SDharageswari.R 			}
1245718a42b5SDharageswari.R 			mconfig = skl_get_mconfig_pb_cpr(dai, p->source);
1246718a42b5SDharageswari.R 			if (mconfig)
1247718a42b5SDharageswari.R 				return mconfig;
1248718a42b5SDharageswari.R 		}
1249718a42b5SDharageswari.R 	}
1250718a42b5SDharageswari.R 	return mconfig;
1251718a42b5SDharageswari.R }
1252718a42b5SDharageswari.R 
1253718a42b5SDharageswari.R static struct skl_module_cfg *skl_get_mconfig_cap_cpr(
1254718a42b5SDharageswari.R 		struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
1255718a42b5SDharageswari.R {
1256718a42b5SDharageswari.R 	struct snd_soc_dapm_path *p;
1257718a42b5SDharageswari.R 	struct skl_module_cfg *mconfig = NULL;
1258718a42b5SDharageswari.R 
1259718a42b5SDharageswari.R 	snd_soc_dapm_widget_for_each_sink_path(w, p) {
1260718a42b5SDharageswari.R 		if (w->endpoints[SND_SOC_DAPM_DIR_IN] > 0) {
1261718a42b5SDharageswari.R 			if (p->connect &&
1262718a42b5SDharageswari.R 				    (p->source->id == snd_soc_dapm_aif_in) &&
1263718a42b5SDharageswari.R 				    p->sink->priv) {
1264718a42b5SDharageswari.R 				mconfig = p->sink->priv;
1265718a42b5SDharageswari.R 				return mconfig;
1266718a42b5SDharageswari.R 			}
1267718a42b5SDharageswari.R 			mconfig = skl_get_mconfig_cap_cpr(dai, p->sink);
1268718a42b5SDharageswari.R 			if (mconfig)
1269718a42b5SDharageswari.R 				return mconfig;
1270718a42b5SDharageswari.R 		}
1271718a42b5SDharageswari.R 	}
1272718a42b5SDharageswari.R 	return mconfig;
1273718a42b5SDharageswari.R }
1274718a42b5SDharageswari.R 
1275718a42b5SDharageswari.R struct skl_module_cfg *
1276718a42b5SDharageswari.R skl_tplg_be_get_cpr_module(struct snd_soc_dai *dai, int stream)
1277718a42b5SDharageswari.R {
1278718a42b5SDharageswari.R 	struct snd_soc_dapm_widget *w;
1279718a42b5SDharageswari.R 	struct skl_module_cfg *mconfig;
1280718a42b5SDharageswari.R 
1281718a42b5SDharageswari.R 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1282718a42b5SDharageswari.R 		w = dai->playback_widget;
1283718a42b5SDharageswari.R 		mconfig = skl_get_mconfig_pb_cpr(dai, w);
1284718a42b5SDharageswari.R 	} else {
1285718a42b5SDharageswari.R 		w = dai->capture_widget;
1286718a42b5SDharageswari.R 		mconfig = skl_get_mconfig_cap_cpr(dai, w);
1287718a42b5SDharageswari.R 	}
1288718a42b5SDharageswari.R 	return mconfig;
1289718a42b5SDharageswari.R }
1290718a42b5SDharageswari.R 
1291cfb0a873SVinod Koul static u8 skl_tplg_be_link_type(int dev_type)
1292cfb0a873SVinod Koul {
1293cfb0a873SVinod Koul 	int ret;
1294cfb0a873SVinod Koul 
1295cfb0a873SVinod Koul 	switch (dev_type) {
1296cfb0a873SVinod Koul 	case SKL_DEVICE_BT:
1297cfb0a873SVinod Koul 		ret = NHLT_LINK_SSP;
1298cfb0a873SVinod Koul 		break;
1299cfb0a873SVinod Koul 
1300cfb0a873SVinod Koul 	case SKL_DEVICE_DMIC:
1301cfb0a873SVinod Koul 		ret = NHLT_LINK_DMIC;
1302cfb0a873SVinod Koul 		break;
1303cfb0a873SVinod Koul 
1304cfb0a873SVinod Koul 	case SKL_DEVICE_I2S:
1305cfb0a873SVinod Koul 		ret = NHLT_LINK_SSP;
1306cfb0a873SVinod Koul 		break;
1307cfb0a873SVinod Koul 
1308cfb0a873SVinod Koul 	case SKL_DEVICE_HDALINK:
1309cfb0a873SVinod Koul 		ret = NHLT_LINK_HDA;
1310cfb0a873SVinod Koul 		break;
1311cfb0a873SVinod Koul 
1312cfb0a873SVinod Koul 	default:
1313cfb0a873SVinod Koul 		ret = NHLT_LINK_INVALID;
1314cfb0a873SVinod Koul 		break;
1315cfb0a873SVinod Koul 	}
1316cfb0a873SVinod Koul 
1317cfb0a873SVinod Koul 	return ret;
1318cfb0a873SVinod Koul }
1319cfb0a873SVinod Koul 
1320cfb0a873SVinod Koul /*
1321cfb0a873SVinod Koul  * Fill the BE gateway parameters
1322cfb0a873SVinod Koul  * The BE gateway expects a blob of parameters which are kept in the ACPI
1323cfb0a873SVinod Koul  * NHLT blob, so query the blob for interface type (i2s/pdm) and instance.
1324cfb0a873SVinod Koul  * The port can have multiple settings so pick based on the PCM
1325cfb0a873SVinod Koul  * parameters
1326cfb0a873SVinod Koul  */
1327cfb0a873SVinod Koul static int skl_tplg_be_fill_pipe_params(struct snd_soc_dai *dai,
1328cfb0a873SVinod Koul 				struct skl_module_cfg *mconfig,
1329cfb0a873SVinod Koul 				struct skl_pipe_params *params)
1330cfb0a873SVinod Koul {
1331cfb0a873SVinod Koul 	struct skl_pipe *pipe = mconfig->pipe;
1332cfb0a873SVinod Koul 	struct nhlt_specific_cfg *cfg;
1333cfb0a873SVinod Koul 	struct skl *skl = get_skl_ctx(dai->dev);
1334cfb0a873SVinod Koul 	int link_type = skl_tplg_be_link_type(mconfig->dev_type);
1335cfb0a873SVinod Koul 
1336cfb0a873SVinod Koul 	memcpy(pipe->p_params, params, sizeof(*params));
1337cfb0a873SVinod Koul 
1338b30c275eSJeeja KP 	if (link_type == NHLT_LINK_HDA)
1339b30c275eSJeeja KP 		return 0;
1340b30c275eSJeeja KP 
1341cfb0a873SVinod Koul 	/* update the blob based on virtual bus_id*/
1342cfb0a873SVinod Koul 	cfg = skl_get_ep_blob(skl, mconfig->vbus_id, link_type,
1343cfb0a873SVinod Koul 					params->s_fmt, params->ch,
1344cfb0a873SVinod Koul 					params->s_freq, params->stream);
1345cfb0a873SVinod Koul 	if (cfg) {
1346cfb0a873SVinod Koul 		mconfig->formats_config.caps_size = cfg->size;
1347bc03281aSJeeja KP 		mconfig->formats_config.caps = (u32 *) &cfg->caps;
1348cfb0a873SVinod Koul 	} else {
1349cfb0a873SVinod Koul 		dev_err(dai->dev, "Blob NULL for id %x type %d dirn %d\n",
1350cfb0a873SVinod Koul 					mconfig->vbus_id, link_type,
1351cfb0a873SVinod Koul 					params->stream);
1352cfb0a873SVinod Koul 		dev_err(dai->dev, "PCM: ch %d, freq %d, fmt %d\n",
1353cfb0a873SVinod Koul 				 params->ch, params->s_freq, params->s_fmt);
1354cfb0a873SVinod Koul 		return -EINVAL;
1355cfb0a873SVinod Koul 	}
1356cfb0a873SVinod Koul 
1357cfb0a873SVinod Koul 	return 0;
1358cfb0a873SVinod Koul }
1359cfb0a873SVinod Koul 
1360cfb0a873SVinod Koul static int skl_tplg_be_set_src_pipe_params(struct snd_soc_dai *dai,
1361cfb0a873SVinod Koul 				struct snd_soc_dapm_widget *w,
1362cfb0a873SVinod Koul 				struct skl_pipe_params *params)
1363cfb0a873SVinod Koul {
1364cfb0a873SVinod Koul 	struct snd_soc_dapm_path *p;
13654d8adccbSSubhransu S. Prusty 	int ret = -EIO;
1366cfb0a873SVinod Koul 
1367f0900eb2SSubhransu S. Prusty 	snd_soc_dapm_widget_for_each_source_path(w, p) {
1368cfb0a873SVinod Koul 		if (p->connect && is_skl_dsp_widget_type(p->source) &&
1369cfb0a873SVinod Koul 						p->source->priv) {
1370cfb0a873SVinod Koul 
13719a03cb49SJeeja KP 			ret = skl_tplg_be_fill_pipe_params(dai,
13729a03cb49SJeeja KP 						p->source->priv, params);
13734d8adccbSSubhransu S. Prusty 			if (ret < 0)
13744d8adccbSSubhransu S. Prusty 				return ret;
1375cfb0a873SVinod Koul 		} else {
13769a03cb49SJeeja KP 			ret = skl_tplg_be_set_src_pipe_params(dai,
13779a03cb49SJeeja KP 						p->source, params);
13784d8adccbSSubhransu S. Prusty 			if (ret < 0)
13794d8adccbSSubhransu S. Prusty 				return ret;
1380cfb0a873SVinod Koul 		}
1381cfb0a873SVinod Koul 	}
1382cfb0a873SVinod Koul 
13834d8adccbSSubhransu S. Prusty 	return ret;
1384cfb0a873SVinod Koul }
1385cfb0a873SVinod Koul 
1386cfb0a873SVinod Koul static int skl_tplg_be_set_sink_pipe_params(struct snd_soc_dai *dai,
1387cfb0a873SVinod Koul 	struct snd_soc_dapm_widget *w, struct skl_pipe_params *params)
1388cfb0a873SVinod Koul {
1389cfb0a873SVinod Koul 	struct snd_soc_dapm_path *p = NULL;
13904d8adccbSSubhransu S. Prusty 	int ret = -EIO;
1391cfb0a873SVinod Koul 
1392f0900eb2SSubhransu S. Prusty 	snd_soc_dapm_widget_for_each_sink_path(w, p) {
1393cfb0a873SVinod Koul 		if (p->connect && is_skl_dsp_widget_type(p->sink) &&
1394cfb0a873SVinod Koul 						p->sink->priv) {
1395cfb0a873SVinod Koul 
13969a03cb49SJeeja KP 			ret = skl_tplg_be_fill_pipe_params(dai,
13979a03cb49SJeeja KP 						p->sink->priv, params);
13984d8adccbSSubhransu S. Prusty 			if (ret < 0)
13994d8adccbSSubhransu S. Prusty 				return ret;
14004d8adccbSSubhransu S. Prusty 		} else {
14014d8adccbSSubhransu S. Prusty 			ret = skl_tplg_be_set_sink_pipe_params(
1402cfb0a873SVinod Koul 						dai, p->sink, params);
14034d8adccbSSubhransu S. Prusty 			if (ret < 0)
14044d8adccbSSubhransu S. Prusty 				return ret;
1405cfb0a873SVinod Koul 		}
1406cfb0a873SVinod Koul 	}
1407cfb0a873SVinod Koul 
14084d8adccbSSubhransu S. Prusty 	return ret;
1409cfb0a873SVinod Koul }
1410cfb0a873SVinod Koul 
1411cfb0a873SVinod Koul /*
1412cfb0a873SVinod Koul  * BE hw_params can be a source parameters (capture) or sink parameters
1413cfb0a873SVinod Koul  * (playback). Based on sink and source we need to either find the source
1414cfb0a873SVinod Koul  * list or the sink list and set the pipeline parameters
1415cfb0a873SVinod Koul  */
1416cfb0a873SVinod Koul int skl_tplg_be_update_params(struct snd_soc_dai *dai,
1417cfb0a873SVinod Koul 				struct skl_pipe_params *params)
1418cfb0a873SVinod Koul {
1419cfb0a873SVinod Koul 	struct snd_soc_dapm_widget *w;
1420cfb0a873SVinod Koul 
1421cfb0a873SVinod Koul 	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1422cfb0a873SVinod Koul 		w = dai->playback_widget;
1423cfb0a873SVinod Koul 
1424cfb0a873SVinod Koul 		return skl_tplg_be_set_src_pipe_params(dai, w, params);
1425cfb0a873SVinod Koul 
1426cfb0a873SVinod Koul 	} else {
1427cfb0a873SVinod Koul 		w = dai->capture_widget;
1428cfb0a873SVinod Koul 
1429cfb0a873SVinod Koul 		return skl_tplg_be_set_sink_pipe_params(dai, w, params);
1430cfb0a873SVinod Koul 	}
1431cfb0a873SVinod Koul 
1432cfb0a873SVinod Koul 	return 0;
1433cfb0a873SVinod Koul }
14343af36706SVinod Koul 
14353af36706SVinod Koul static const struct snd_soc_tplg_widget_events skl_tplg_widget_ops[] = {
14363af36706SVinod Koul 	{SKL_MIXER_EVENT, skl_tplg_mixer_event},
14373af36706SVinod Koul 	{SKL_VMIXER_EVENT, skl_tplg_vmixer_event},
14383af36706SVinod Koul 	{SKL_PGA_EVENT, skl_tplg_pga_event},
14393af36706SVinod Koul };
14403af36706SVinod Koul 
1441140adfbaSJeeja KP static const struct snd_soc_tplg_bytes_ext_ops skl_tlv_ops[] = {
1442140adfbaSJeeja KP 	{SKL_CONTROL_TYPE_BYTE_TLV, skl_tplg_tlv_control_get,
1443140adfbaSJeeja KP 					skl_tplg_tlv_control_set},
1444140adfbaSJeeja KP };
1445140adfbaSJeeja KP 
14463af36706SVinod Koul /*
14473af36706SVinod Koul  * The topology binary passes the pin info for a module so initialize the pin
14483af36706SVinod Koul  * info passed into module instance
14493af36706SVinod Koul  */
14506abca1d7SJeeja KP static void skl_fill_module_pin_info(struct skl_dfw_module_pin *dfw_pin,
14513af36706SVinod Koul 						struct skl_module_pin *m_pin,
14526abca1d7SJeeja KP 						bool is_dynamic, int max_pin)
14533af36706SVinod Koul {
14543af36706SVinod Koul 	int i;
14553af36706SVinod Koul 
14563af36706SVinod Koul 	for (i = 0; i < max_pin; i++) {
14576abca1d7SJeeja KP 		m_pin[i].id.module_id = dfw_pin[i].module_id;
14586abca1d7SJeeja KP 		m_pin[i].id.instance_id = dfw_pin[i].instance_id;
14593af36706SVinod Koul 		m_pin[i].in_use = false;
14606abca1d7SJeeja KP 		m_pin[i].is_dynamic = is_dynamic;
14614f745708SJeeja KP 		m_pin[i].pin_state = SKL_PIN_UNBIND;
14623af36706SVinod Koul 	}
14633af36706SVinod Koul }
14643af36706SVinod Koul 
14653af36706SVinod Koul /*
14663af36706SVinod Koul  * Add pipeline from topology binary into driver pipeline list
14673af36706SVinod Koul  *
14683af36706SVinod Koul  * If already added we return that instance
14693af36706SVinod Koul  * Otherwise we create a new instance and add into driver list
14703af36706SVinod Koul  */
14713af36706SVinod Koul static struct skl_pipe *skl_tplg_add_pipe(struct device *dev,
14723af36706SVinod Koul 			struct skl *skl, struct skl_dfw_pipe *dfw_pipe)
14733af36706SVinod Koul {
14743af36706SVinod Koul 	struct skl_pipeline *ppl;
14753af36706SVinod Koul 	struct skl_pipe *pipe;
14763af36706SVinod Koul 	struct skl_pipe_params *params;
14773af36706SVinod Koul 
14783af36706SVinod Koul 	list_for_each_entry(ppl, &skl->ppl_list, node) {
14793af36706SVinod Koul 		if (ppl->pipe->ppl_id == dfw_pipe->pipe_id)
14803af36706SVinod Koul 			return ppl->pipe;
14813af36706SVinod Koul 	}
14823af36706SVinod Koul 
14833af36706SVinod Koul 	ppl = devm_kzalloc(dev, sizeof(*ppl), GFP_KERNEL);
14843af36706SVinod Koul 	if (!ppl)
14853af36706SVinod Koul 		return NULL;
14863af36706SVinod Koul 
14873af36706SVinod Koul 	pipe = devm_kzalloc(dev, sizeof(*pipe), GFP_KERNEL);
14883af36706SVinod Koul 	if (!pipe)
14893af36706SVinod Koul 		return NULL;
14903af36706SVinod Koul 
14913af36706SVinod Koul 	params = devm_kzalloc(dev, sizeof(*params), GFP_KERNEL);
14923af36706SVinod Koul 	if (!params)
14933af36706SVinod Koul 		return NULL;
14943af36706SVinod Koul 
14953af36706SVinod Koul 	pipe->ppl_id = dfw_pipe->pipe_id;
14963af36706SVinod Koul 	pipe->memory_pages = dfw_pipe->memory_pages;
14973af36706SVinod Koul 	pipe->pipe_priority = dfw_pipe->pipe_priority;
14983af36706SVinod Koul 	pipe->conn_type = dfw_pipe->conn_type;
14993af36706SVinod Koul 	pipe->state = SKL_PIPE_INVALID;
15003af36706SVinod Koul 	pipe->p_params = params;
15013af36706SVinod Koul 	INIT_LIST_HEAD(&pipe->w_list);
15023af36706SVinod Koul 
15033af36706SVinod Koul 	ppl->pipe = pipe;
15043af36706SVinod Koul 	list_add(&ppl->node, &skl->ppl_list);
15053af36706SVinod Koul 
15063af36706SVinod Koul 	return ppl->pipe;
15073af36706SVinod Koul }
15083af36706SVinod Koul 
15094cd9899fSHardik T Shah static void skl_tplg_fill_fmt(struct skl_module_fmt *dst_fmt,
15104cd9899fSHardik T Shah 				struct skl_dfw_module_fmt *src_fmt,
15114cd9899fSHardik T Shah 				int pins)
15124cd9899fSHardik T Shah {
15134cd9899fSHardik T Shah 	int i;
15144cd9899fSHardik T Shah 
15154cd9899fSHardik T Shah 	for (i = 0; i < pins; i++) {
15164cd9899fSHardik T Shah 		dst_fmt[i].channels  = src_fmt[i].channels;
15174cd9899fSHardik T Shah 		dst_fmt[i].s_freq = src_fmt[i].freq;
15184cd9899fSHardik T Shah 		dst_fmt[i].bit_depth = src_fmt[i].bit_depth;
15194cd9899fSHardik T Shah 		dst_fmt[i].valid_bit_depth = src_fmt[i].valid_bit_depth;
15204cd9899fSHardik T Shah 		dst_fmt[i].ch_cfg = src_fmt[i].ch_cfg;
15214cd9899fSHardik T Shah 		dst_fmt[i].ch_map = src_fmt[i].ch_map;
15224cd9899fSHardik T Shah 		dst_fmt[i].interleaving_style = src_fmt[i].interleaving_style;
15234cd9899fSHardik T Shah 		dst_fmt[i].sample_type = src_fmt[i].sample_type;
15244cd9899fSHardik T Shah 	}
15254cd9899fSHardik T Shah }
15264cd9899fSHardik T Shah 
15273af36706SVinod Koul /*
15283af36706SVinod Koul  * Topology core widget load callback
15293af36706SVinod Koul  *
15303af36706SVinod Koul  * This is used to save the private data for each widget which gives
15313af36706SVinod Koul  * information to the driver about module and pipeline parameters which DSP
15323af36706SVinod Koul  * FW expects like ids, resource values, formats etc
15333af36706SVinod Koul  */
15343af36706SVinod Koul static int skl_tplg_widget_load(struct snd_soc_component *cmpnt,
15353af36706SVinod Koul 				struct snd_soc_dapm_widget *w,
15363af36706SVinod Koul 				struct snd_soc_tplg_dapm_widget *tplg_w)
15373af36706SVinod Koul {
15383af36706SVinod Koul 	int ret;
15393af36706SVinod Koul 	struct hdac_ext_bus *ebus = snd_soc_component_get_drvdata(cmpnt);
15403af36706SVinod Koul 	struct skl *skl = ebus_to_skl(ebus);
15413af36706SVinod Koul 	struct hdac_bus *bus = ebus_to_hbus(ebus);
15423af36706SVinod Koul 	struct skl_module_cfg *mconfig;
15433af36706SVinod Koul 	struct skl_pipe *pipe;
1544b663a8c5SJeeja KP 	struct skl_dfw_module *dfw_config =
1545b663a8c5SJeeja KP 				(struct skl_dfw_module *)tplg_w->priv.data;
15463af36706SVinod Koul 
15473af36706SVinod Koul 	if (!tplg_w->priv.size)
15483af36706SVinod Koul 		goto bind_event;
15493af36706SVinod Koul 
15503af36706SVinod Koul 	mconfig = devm_kzalloc(bus->dev, sizeof(*mconfig), GFP_KERNEL);
15513af36706SVinod Koul 
15523af36706SVinod Koul 	if (!mconfig)
15533af36706SVinod Koul 		return -ENOMEM;
15543af36706SVinod Koul 
15553af36706SVinod Koul 	w->priv = mconfig;
15563af36706SVinod Koul 	mconfig->id.module_id = dfw_config->module_id;
15573af36706SVinod Koul 	mconfig->id.instance_id = dfw_config->instance_id;
15583af36706SVinod Koul 	mconfig->mcps = dfw_config->max_mcps;
15593af36706SVinod Koul 	mconfig->ibs = dfw_config->ibs;
15603af36706SVinod Koul 	mconfig->obs = dfw_config->obs;
15613af36706SVinod Koul 	mconfig->core_id = dfw_config->core_id;
15623af36706SVinod Koul 	mconfig->max_in_queue = dfw_config->max_in_queue;
15633af36706SVinod Koul 	mconfig->max_out_queue = dfw_config->max_out_queue;
15643af36706SVinod Koul 	mconfig->is_loadable = dfw_config->is_loadable;
15654cd9899fSHardik T Shah 	skl_tplg_fill_fmt(mconfig->in_fmt, dfw_config->in_fmt,
15664cd9899fSHardik T Shah 						MODULE_MAX_IN_PINS);
15674cd9899fSHardik T Shah 	skl_tplg_fill_fmt(mconfig->out_fmt, dfw_config->out_fmt,
15684cd9899fSHardik T Shah 						MODULE_MAX_OUT_PINS);
15694cd9899fSHardik T Shah 
15703af36706SVinod Koul 	mconfig->params_fixup = dfw_config->params_fixup;
15713af36706SVinod Koul 	mconfig->converter = dfw_config->converter;
15723af36706SVinod Koul 	mconfig->m_type = dfw_config->module_type;
15733af36706SVinod Koul 	mconfig->vbus_id = dfw_config->vbus_id;
1574b18c458dSJeeja KP 	mconfig->mem_pages = dfw_config->mem_pages;
15753af36706SVinod Koul 
15763af36706SVinod Koul 	pipe = skl_tplg_add_pipe(bus->dev, skl, &dfw_config->pipe);
15773af36706SVinod Koul 	if (pipe)
15783af36706SVinod Koul 		mconfig->pipe = pipe;
15793af36706SVinod Koul 
15803af36706SVinod Koul 	mconfig->dev_type = dfw_config->dev_type;
15813af36706SVinod Koul 	mconfig->hw_conn_type = dfw_config->hw_conn_type;
15823af36706SVinod Koul 	mconfig->time_slot = dfw_config->time_slot;
15833af36706SVinod Koul 	mconfig->formats_config.caps_size = dfw_config->caps.caps_size;
15843af36706SVinod Koul 
158565aecfa8SHardik T Shah 	if (dfw_config->is_loadable)
158665aecfa8SHardik T Shah 		memcpy(mconfig->guid, dfw_config->uuid,
158765aecfa8SHardik T Shah 					ARRAY_SIZE(dfw_config->uuid));
158865aecfa8SHardik T Shah 
15894cd9899fSHardik T Shah 	mconfig->m_in_pin = devm_kzalloc(bus->dev, (mconfig->max_in_queue) *
15903af36706SVinod Koul 						sizeof(*mconfig->m_in_pin),
15913af36706SVinod Koul 						GFP_KERNEL);
15923af36706SVinod Koul 	if (!mconfig->m_in_pin)
15933af36706SVinod Koul 		return -ENOMEM;
15943af36706SVinod Koul 
15956abca1d7SJeeja KP 	mconfig->m_out_pin = devm_kzalloc(bus->dev, (mconfig->max_out_queue) *
15963af36706SVinod Koul 						sizeof(*mconfig->m_out_pin),
15973af36706SVinod Koul 						GFP_KERNEL);
15983af36706SVinod Koul 	if (!mconfig->m_out_pin)
15993af36706SVinod Koul 		return -ENOMEM;
16003af36706SVinod Koul 
16016abca1d7SJeeja KP 	skl_fill_module_pin_info(dfw_config->in_pin, mconfig->m_in_pin,
16026abca1d7SJeeja KP 						dfw_config->is_dynamic_in_pin,
16033af36706SVinod Koul 						mconfig->max_in_queue);
16046abca1d7SJeeja KP 
16056abca1d7SJeeja KP 	skl_fill_module_pin_info(dfw_config->out_pin, mconfig->m_out_pin,
16066abca1d7SJeeja KP 						 dfw_config->is_dynamic_out_pin,
16073af36706SVinod Koul 							mconfig->max_out_queue);
16083af36706SVinod Koul 
16096abca1d7SJeeja KP 
16103af36706SVinod Koul 	if (mconfig->formats_config.caps_size == 0)
16113af36706SVinod Koul 		goto bind_event;
16123af36706SVinod Koul 
16133af36706SVinod Koul 	mconfig->formats_config.caps = (u32 *)devm_kzalloc(bus->dev,
16143af36706SVinod Koul 			mconfig->formats_config.caps_size, GFP_KERNEL);
16153af36706SVinod Koul 
16163af36706SVinod Koul 	if (mconfig->formats_config.caps == NULL)
16173af36706SVinod Koul 		return -ENOMEM;
16183af36706SVinod Koul 
16193af36706SVinod Koul 	memcpy(mconfig->formats_config.caps, dfw_config->caps.caps,
16203af36706SVinod Koul 						 dfw_config->caps.caps_size);
1621abb74003SJeeja KP 	mconfig->formats_config.param_id = dfw_config->caps.param_id;
1622abb74003SJeeja KP 	mconfig->formats_config.set_params = dfw_config->caps.set_params;
16233af36706SVinod Koul 
16243af36706SVinod Koul bind_event:
16253af36706SVinod Koul 	if (tplg_w->event_type == 0) {
16263373f716SVinod Koul 		dev_dbg(bus->dev, "ASoC: No event handler required\n");
16273af36706SVinod Koul 		return 0;
16283af36706SVinod Koul 	}
16293af36706SVinod Koul 
16303af36706SVinod Koul 	ret = snd_soc_tplg_widget_bind_event(w, skl_tplg_widget_ops,
1631b663a8c5SJeeja KP 					ARRAY_SIZE(skl_tplg_widget_ops),
1632b663a8c5SJeeja KP 					tplg_w->event_type);
16333af36706SVinod Koul 
16343af36706SVinod Koul 	if (ret) {
16353af36706SVinod Koul 		dev_err(bus->dev, "%s: No matching event handlers found for %d\n",
16363af36706SVinod Koul 					__func__, tplg_w->event_type);
16373af36706SVinod Koul 		return -EINVAL;
16383af36706SVinod Koul 	}
16393af36706SVinod Koul 
16403af36706SVinod Koul 	return 0;
16413af36706SVinod Koul }
16423af36706SVinod Koul 
1643140adfbaSJeeja KP static int skl_init_algo_data(struct device *dev, struct soc_bytes_ext *be,
1644140adfbaSJeeja KP 					struct snd_soc_tplg_bytes_control *bc)
1645140adfbaSJeeja KP {
1646140adfbaSJeeja KP 	struct skl_algo_data *ac;
1647140adfbaSJeeja KP 	struct skl_dfw_algo_data *dfw_ac =
1648140adfbaSJeeja KP 				(struct skl_dfw_algo_data *)bc->priv.data;
1649140adfbaSJeeja KP 
1650140adfbaSJeeja KP 	ac = devm_kzalloc(dev, sizeof(*ac), GFP_KERNEL);
1651140adfbaSJeeja KP 	if (!ac)
1652140adfbaSJeeja KP 		return -ENOMEM;
1653140adfbaSJeeja KP 
1654140adfbaSJeeja KP 	/* Fill private data */
1655140adfbaSJeeja KP 	ac->max = dfw_ac->max;
1656140adfbaSJeeja KP 	ac->param_id = dfw_ac->param_id;
1657140adfbaSJeeja KP 	ac->set_params = dfw_ac->set_params;
1658140adfbaSJeeja KP 
1659140adfbaSJeeja KP 	if (ac->max) {
1660140adfbaSJeeja KP 		ac->params = (char *) devm_kzalloc(dev, ac->max, GFP_KERNEL);
1661140adfbaSJeeja KP 		if (!ac->params)
1662140adfbaSJeeja KP 			return -ENOMEM;
1663140adfbaSJeeja KP 
1664140adfbaSJeeja KP 		memcpy(ac->params, dfw_ac->params, ac->max);
1665140adfbaSJeeja KP 	}
1666140adfbaSJeeja KP 
1667140adfbaSJeeja KP 	be->dobj.private  = ac;
1668140adfbaSJeeja KP 	return 0;
1669140adfbaSJeeja KP }
1670140adfbaSJeeja KP 
1671140adfbaSJeeja KP static int skl_tplg_control_load(struct snd_soc_component *cmpnt,
1672140adfbaSJeeja KP 				struct snd_kcontrol_new *kctl,
1673140adfbaSJeeja KP 				struct snd_soc_tplg_ctl_hdr *hdr)
1674140adfbaSJeeja KP {
1675140adfbaSJeeja KP 	struct soc_bytes_ext *sb;
1676140adfbaSJeeja KP 	struct snd_soc_tplg_bytes_control *tplg_bc;
1677140adfbaSJeeja KP 	struct hdac_ext_bus *ebus  = snd_soc_component_get_drvdata(cmpnt);
1678140adfbaSJeeja KP 	struct hdac_bus *bus = ebus_to_hbus(ebus);
1679140adfbaSJeeja KP 
1680140adfbaSJeeja KP 	switch (hdr->ops.info) {
1681140adfbaSJeeja KP 	case SND_SOC_TPLG_CTL_BYTES:
1682140adfbaSJeeja KP 		tplg_bc = container_of(hdr,
1683140adfbaSJeeja KP 				struct snd_soc_tplg_bytes_control, hdr);
1684140adfbaSJeeja KP 		if (kctl->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1685140adfbaSJeeja KP 			sb = (struct soc_bytes_ext *)kctl->private_value;
1686140adfbaSJeeja KP 			if (tplg_bc->priv.size)
1687140adfbaSJeeja KP 				return skl_init_algo_data(
1688140adfbaSJeeja KP 						bus->dev, sb, tplg_bc);
1689140adfbaSJeeja KP 		}
1690140adfbaSJeeja KP 		break;
1691140adfbaSJeeja KP 
1692140adfbaSJeeja KP 	default:
1693140adfbaSJeeja KP 		dev_warn(bus->dev, "Control load not supported %d:%d:%d\n",
1694140adfbaSJeeja KP 			hdr->ops.get, hdr->ops.put, hdr->ops.info);
1695140adfbaSJeeja KP 		break;
1696140adfbaSJeeja KP 	}
1697140adfbaSJeeja KP 
1698140adfbaSJeeja KP 	return 0;
1699140adfbaSJeeja KP }
1700140adfbaSJeeja KP 
17013af36706SVinod Koul static struct snd_soc_tplg_ops skl_tplg_ops  = {
17023af36706SVinod Koul 	.widget_load = skl_tplg_widget_load,
1703140adfbaSJeeja KP 	.control_load = skl_tplg_control_load,
1704140adfbaSJeeja KP 	.bytes_ext_ops = skl_tlv_ops,
1705140adfbaSJeeja KP 	.bytes_ext_ops_count = ARRAY_SIZE(skl_tlv_ops),
17063af36706SVinod Koul };
17073af36706SVinod Koul 
17083af36706SVinod Koul /* This will be read from topology manifest, currently defined here */
17093af36706SVinod Koul #define SKL_MAX_MCPS 30000000
17103af36706SVinod Koul #define SKL_FW_MAX_MEM 1000000
17113af36706SVinod Koul 
17123af36706SVinod Koul /*
17133af36706SVinod Koul  * SKL topology init routine
17143af36706SVinod Koul  */
17153af36706SVinod Koul int skl_tplg_init(struct snd_soc_platform *platform, struct hdac_ext_bus *ebus)
17163af36706SVinod Koul {
17173af36706SVinod Koul 	int ret;
17183af36706SVinod Koul 	const struct firmware *fw;
17193af36706SVinod Koul 	struct hdac_bus *bus = ebus_to_hbus(ebus);
17203af36706SVinod Koul 	struct skl *skl = ebus_to_skl(ebus);
17213af36706SVinod Koul 
17224b235c43SVinod Koul 	ret = request_firmware(&fw, skl->tplg_name, bus->dev);
17233af36706SVinod Koul 	if (ret < 0) {
1724b663a8c5SJeeja KP 		dev_err(bus->dev, "tplg fw %s load failed with %d\n",
17254b235c43SVinod Koul 				skl->tplg_name, ret);
17264b235c43SVinod Koul 		ret = request_firmware(&fw, "dfw_sst.bin", bus->dev);
17274b235c43SVinod Koul 		if (ret < 0) {
17284b235c43SVinod Koul 			dev_err(bus->dev, "Fallback tplg fw %s load failed with %d\n",
17293af36706SVinod Koul 					"dfw_sst.bin", ret);
17303af36706SVinod Koul 			return ret;
17313af36706SVinod Koul 		}
17324b235c43SVinod Koul 	}
17333af36706SVinod Koul 
17343af36706SVinod Koul 	/*
17353af36706SVinod Koul 	 * The complete tplg for SKL is loaded as index 0, we don't use
17363af36706SVinod Koul 	 * any other index
17373af36706SVinod Koul 	 */
1738b663a8c5SJeeja KP 	ret = snd_soc_tplg_component_load(&platform->component,
1739b663a8c5SJeeja KP 					&skl_tplg_ops, fw, 0);
17403af36706SVinod Koul 	if (ret < 0) {
17413af36706SVinod Koul 		dev_err(bus->dev, "tplg component load failed%d\n", ret);
1742c14a82c7SSudip Mukherjee 		release_firmware(fw);
17433af36706SVinod Koul 		return -EINVAL;
17443af36706SVinod Koul 	}
17453af36706SVinod Koul 
17463af36706SVinod Koul 	skl->resource.max_mcps = SKL_MAX_MCPS;
17473af36706SVinod Koul 	skl->resource.max_mem = SKL_FW_MAX_MEM;
17483af36706SVinod Koul 
1749d8018361SVinod Koul 	skl->tplg = fw;
1750d8018361SVinod Koul 
17513af36706SVinod Koul 	return 0;
17523af36706SVinod Koul }
1753