xref: /openbmc/linux/sound/soc/intel/skylake/skl-topology.c (revision e4e95d8291831fa63c32e00b59f544f0812dc0b6)
147d7195dSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e4e2d2f4SJeeja KP /*
3e4e2d2f4SJeeja KP  *  skl-topology.c - Implements Platform component ALSA controls/widget
4e4e2d2f4SJeeja KP  *  handlers.
5e4e2d2f4SJeeja KP  *
6e4e2d2f4SJeeja KP  *  Copyright (C) 2014-2015 Intel Corp
7e4e2d2f4SJeeja KP  *  Author: Jeeja KP <jeeja.kp@intel.com>
8e4e2d2f4SJeeja KP  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9e4e2d2f4SJeeja KP  */
10e4e2d2f4SJeeja KP 
11e4e2d2f4SJeeja KP #include <linux/slab.h>
12e4e2d2f4SJeeja KP #include <linux/types.h>
13e4e2d2f4SJeeja KP #include <linux/firmware.h>
14ac9391daSGuenter Roeck #include <linux/uuid.h>
1563643b59SPierre-Louis Bossart #include <sound/intel-nhlt.h>
16e4e2d2f4SJeeja KP #include <sound/soc.h>
171b290ef0SMateusz Gorski #include <sound/soc-acpi.h>
18e4e2d2f4SJeeja KP #include <sound/soc-topology.h>
196277e832SShreyas NC #include <uapi/sound/snd_sst_tokens.h>
200c24fdc0SGuenter Roeck #include <uapi/sound/skl-tplg-interface.h>
21e4e2d2f4SJeeja KP #include "skl-sst-dsp.h"
22e4e2d2f4SJeeja KP #include "skl-sst-ipc.h"
23e4e2d2f4SJeeja KP #include "skl-topology.h"
24e4e2d2f4SJeeja KP #include "skl.h"
256c5768b3SDharageswari R #include "../common/sst-dsp.h"
266c5768b3SDharageswari R #include "../common/sst-dsp-priv.h"
27e4e2d2f4SJeeja KP 
28f7590d4fSJeeja KP #define SKL_CH_FIXUP_MASK		(1 << 0)
29f7590d4fSJeeja KP #define SKL_RATE_FIXUP_MASK		(1 << 1)
30f7590d4fSJeeja KP #define SKL_FMT_FIXUP_MASK		(1 << 2)
316277e832SShreyas NC #define SKL_IN_DIR_BIT_MASK		BIT(0)
326277e832SShreyas NC #define SKL_PIN_COUNT_MASK		GENMASK(7, 4)
33f7590d4fSJeeja KP 
347a1b749bSDharageswari R static const int mic_mono_list[] = {
357a1b749bSDharageswari R 0, 1, 2, 3,
367a1b749bSDharageswari R };
377a1b749bSDharageswari R static const int mic_stereo_list[][SKL_CH_STEREO] = {
387a1b749bSDharageswari R {0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 3}, {2, 3},
397a1b749bSDharageswari R };
407a1b749bSDharageswari R static const int mic_trio_list[][SKL_CH_TRIO] = {
417a1b749bSDharageswari R {0, 1, 2}, {0, 1, 3}, {0, 2, 3}, {1, 2, 3},
427a1b749bSDharageswari R };
437a1b749bSDharageswari R static const int mic_quatro_list[][SKL_CH_QUATRO] = {
447a1b749bSDharageswari R {0, 1, 2, 3},
457a1b749bSDharageswari R };
467a1b749bSDharageswari R 
47f6fa56e2SRamesh Babu #define CHECK_HW_PARAMS(ch, freq, bps, prm_ch, prm_freq, prm_bps) \
48f6fa56e2SRamesh Babu 	((ch == prm_ch) && (bps == prm_bps) && (freq == prm_freq))
49f6fa56e2SRamesh Babu 
50bcc2a2dcSCezary Rojewski void skl_tplg_d0i3_get(struct skl_dev *skl, enum d0i3_capability caps)
51a83e3b4cSVinod Koul {
52bcc2a2dcSCezary Rojewski 	struct skl_d0i3_data *d0i3 =  &skl->d0i3;
53a83e3b4cSVinod Koul 
54a83e3b4cSVinod Koul 	switch (caps) {
55a83e3b4cSVinod Koul 	case SKL_D0I3_NONE:
56a83e3b4cSVinod Koul 		d0i3->non_d0i3++;
57a83e3b4cSVinod Koul 		break;
58a83e3b4cSVinod Koul 
59a83e3b4cSVinod Koul 	case SKL_D0I3_STREAMING:
60a83e3b4cSVinod Koul 		d0i3->streaming++;
61a83e3b4cSVinod Koul 		break;
62a83e3b4cSVinod Koul 
63a83e3b4cSVinod Koul 	case SKL_D0I3_NON_STREAMING:
64a83e3b4cSVinod Koul 		d0i3->non_streaming++;
65a83e3b4cSVinod Koul 		break;
66a83e3b4cSVinod Koul 	}
67a83e3b4cSVinod Koul }
68a83e3b4cSVinod Koul 
69bcc2a2dcSCezary Rojewski void skl_tplg_d0i3_put(struct skl_dev *skl, enum d0i3_capability caps)
70a83e3b4cSVinod Koul {
71bcc2a2dcSCezary Rojewski 	struct skl_d0i3_data *d0i3 =  &skl->d0i3;
72a83e3b4cSVinod Koul 
73a83e3b4cSVinod Koul 	switch (caps) {
74a83e3b4cSVinod Koul 	case SKL_D0I3_NONE:
75a83e3b4cSVinod Koul 		d0i3->non_d0i3--;
76a83e3b4cSVinod Koul 		break;
77a83e3b4cSVinod Koul 
78a83e3b4cSVinod Koul 	case SKL_D0I3_STREAMING:
79a83e3b4cSVinod Koul 		d0i3->streaming--;
80a83e3b4cSVinod Koul 		break;
81a83e3b4cSVinod Koul 
82a83e3b4cSVinod Koul 	case SKL_D0I3_NON_STREAMING:
83a83e3b4cSVinod Koul 		d0i3->non_streaming--;
84a83e3b4cSVinod Koul 		break;
85a83e3b4cSVinod Koul 	}
86a83e3b4cSVinod Koul }
87a83e3b4cSVinod Koul 
88e4e2d2f4SJeeja KP /*
89e4e2d2f4SJeeja KP  * SKL DSP driver modelling uses only few DAPM widgets so for rest we will
90e4e2d2f4SJeeja KP  * ignore. This helpers checks if the SKL driver handles this widget type
91e4e2d2f4SJeeja KP  */
92cb1f904dSGuneshwor Singh static int is_skl_dsp_widget_type(struct snd_soc_dapm_widget *w,
93cb1f904dSGuneshwor Singh 				  struct device *dev)
94e4e2d2f4SJeeja KP {
95cb1f904dSGuneshwor Singh 	if (w->dapm->dev != dev)
96cb1f904dSGuneshwor Singh 		return false;
97cb1f904dSGuneshwor Singh 
98e4e2d2f4SJeeja KP 	switch (w->id) {
99e4e2d2f4SJeeja KP 	case snd_soc_dapm_dai_link:
100e4e2d2f4SJeeja KP 	case snd_soc_dapm_dai_in:
101e4e2d2f4SJeeja KP 	case snd_soc_dapm_aif_in:
102e4e2d2f4SJeeja KP 	case snd_soc_dapm_aif_out:
103e4e2d2f4SJeeja KP 	case snd_soc_dapm_dai_out:
104e4e2d2f4SJeeja KP 	case snd_soc_dapm_switch:
105fe65324eSRakesh Ughreja 	case snd_soc_dapm_output:
106fe65324eSRakesh Ughreja 	case snd_soc_dapm_mux:
107fe65324eSRakesh Ughreja 
108e4e2d2f4SJeeja KP 		return false;
109e4e2d2f4SJeeja KP 	default:
110e4e2d2f4SJeeja KP 		return true;
111e4e2d2f4SJeeja KP 	}
112e4e2d2f4SJeeja KP }
113e4e2d2f4SJeeja KP 
114bcc2a2dcSCezary Rojewski static void skl_dump_mconfig(struct skl_dev *skl, struct skl_module_cfg *mcfg)
115f7590d4fSJeeja KP {
116e8b374b6SCezary Rojewski 	struct skl_module_iface *iface = &mcfg->module->formats[mcfg->fmt_idx];
117f6fa56e2SRamesh Babu 
118bcc2a2dcSCezary Rojewski 	dev_dbg(skl->dev, "Dumping config\n");
119bcc2a2dcSCezary Rojewski 	dev_dbg(skl->dev, "Input Format:\n");
120bcc2a2dcSCezary Rojewski 	dev_dbg(skl->dev, "channels = %d\n", iface->inputs[0].fmt.channels);
121bcc2a2dcSCezary Rojewski 	dev_dbg(skl->dev, "s_freq = %d\n", iface->inputs[0].fmt.s_freq);
122bcc2a2dcSCezary Rojewski 	dev_dbg(skl->dev, "ch_cfg = %d\n", iface->inputs[0].fmt.ch_cfg);
123bcc2a2dcSCezary Rojewski 	dev_dbg(skl->dev, "valid bit depth = %d\n",
124f6fa56e2SRamesh Babu 				iface->inputs[0].fmt.valid_bit_depth);
125bcc2a2dcSCezary Rojewski 	dev_dbg(skl->dev, "Output Format:\n");
126bcc2a2dcSCezary Rojewski 	dev_dbg(skl->dev, "channels = %d\n", iface->outputs[0].fmt.channels);
127bcc2a2dcSCezary Rojewski 	dev_dbg(skl->dev, "s_freq = %d\n", iface->outputs[0].fmt.s_freq);
128bcc2a2dcSCezary Rojewski 	dev_dbg(skl->dev, "valid bit depth = %d\n",
129f6fa56e2SRamesh Babu 				iface->outputs[0].fmt.valid_bit_depth);
130bcc2a2dcSCezary Rojewski 	dev_dbg(skl->dev, "ch_cfg = %d\n", iface->outputs[0].fmt.ch_cfg);
131f7590d4fSJeeja KP }
132f7590d4fSJeeja KP 
133ea5a137dSSubhransu S. Prusty static void skl_tplg_update_chmap(struct skl_module_fmt *fmt, int chs)
134ea5a137dSSubhransu S. Prusty {
135ea5a137dSSubhransu S. Prusty 	int slot_map = 0xFFFFFFFF;
136ea5a137dSSubhransu S. Prusty 	int start_slot = 0;
137ea5a137dSSubhransu S. Prusty 	int i;
138ea5a137dSSubhransu S. Prusty 
139ea5a137dSSubhransu S. Prusty 	for (i = 0; i < chs; i++) {
140ea5a137dSSubhransu S. Prusty 		/*
141ea5a137dSSubhransu S. Prusty 		 * For 2 channels with starting slot as 0, slot map will
142ea5a137dSSubhransu S. Prusty 		 * look like 0xFFFFFF10.
143ea5a137dSSubhransu S. Prusty 		 */
144ea5a137dSSubhransu S. Prusty 		slot_map &= (~(0xF << (4 * i)) | (start_slot << (4 * i)));
145ea5a137dSSubhransu S. Prusty 		start_slot++;
146ea5a137dSSubhransu S. Prusty 	}
147ea5a137dSSubhransu S. Prusty 	fmt->ch_map = slot_map;
148ea5a137dSSubhransu S. Prusty }
149ea5a137dSSubhransu S. Prusty 
150f7590d4fSJeeja KP static void skl_tplg_update_params(struct skl_module_fmt *fmt,
151f7590d4fSJeeja KP 			struct skl_pipe_params *params, int fixup)
152f7590d4fSJeeja KP {
153f7590d4fSJeeja KP 	if (fixup & SKL_RATE_FIXUP_MASK)
154f7590d4fSJeeja KP 		fmt->s_freq = params->s_freq;
155ea5a137dSSubhransu S. Prusty 	if (fixup & SKL_CH_FIXUP_MASK) {
156f7590d4fSJeeja KP 		fmt->channels = params->ch;
157ea5a137dSSubhransu S. Prusty 		skl_tplg_update_chmap(fmt, fmt->channels);
158ea5a137dSSubhransu S. Prusty 	}
15998256f83SJeeja KP 	if (fixup & SKL_FMT_FIXUP_MASK) {
16098256f83SJeeja KP 		fmt->valid_bit_depth = skl_get_bit_depth(params->s_fmt);
16198256f83SJeeja KP 
16298256f83SJeeja KP 		/*
16398256f83SJeeja KP 		 * 16 bit is 16 bit container whereas 24 bit is in 32 bit
16498256f83SJeeja KP 		 * container so update bit depth accordingly
16598256f83SJeeja KP 		 */
16698256f83SJeeja KP 		switch (fmt->valid_bit_depth) {
16798256f83SJeeja KP 		case SKL_DEPTH_16BIT:
16898256f83SJeeja KP 			fmt->bit_depth = fmt->valid_bit_depth;
16998256f83SJeeja KP 			break;
17098256f83SJeeja KP 
17198256f83SJeeja KP 		default:
17298256f83SJeeja KP 			fmt->bit_depth = SKL_DEPTH_32BIT;
17398256f83SJeeja KP 			break;
17498256f83SJeeja KP 		}
17598256f83SJeeja KP 	}
17698256f83SJeeja KP 
177f7590d4fSJeeja KP }
178f7590d4fSJeeja KP 
179f7590d4fSJeeja KP /*
180f7590d4fSJeeja KP  * A pipeline may have modules which impact the pcm parameters, like SRC,
181f7590d4fSJeeja KP  * channel converter, format converter.
182f7590d4fSJeeja KP  * We need to calculate the output params by applying the 'fixup'
183f7590d4fSJeeja KP  * Topology will tell driver which type of fixup is to be applied by
184f7590d4fSJeeja KP  * supplying the fixup mask, so based on that we calculate the output
185f7590d4fSJeeja KP  *
186f7590d4fSJeeja KP  * Now In FE the pcm hw_params is source/target format. Same is applicable
187f7590d4fSJeeja KP  * for BE with its hw_params invoked.
188f7590d4fSJeeja KP  * here based on FE, BE pipeline and direction we calculate the input and
189f7590d4fSJeeja KP  * outfix and then apply that for a module
190f7590d4fSJeeja KP  */
191f7590d4fSJeeja KP static void skl_tplg_update_params_fixup(struct skl_module_cfg *m_cfg,
192f7590d4fSJeeja KP 		struct skl_pipe_params *params, bool is_fe)
193f7590d4fSJeeja KP {
194f7590d4fSJeeja KP 	int in_fixup, out_fixup;
195f7590d4fSJeeja KP 	struct skl_module_fmt *in_fmt, *out_fmt;
196f7590d4fSJeeja KP 
1974cd9899fSHardik T Shah 	/* Fixups will be applied to pin 0 only */
198e8b374b6SCezary Rojewski 	in_fmt = &m_cfg->module->formats[m_cfg->fmt_idx].inputs[0].fmt;
199e8b374b6SCezary Rojewski 	out_fmt = &m_cfg->module->formats[m_cfg->fmt_idx].outputs[0].fmt;
200f7590d4fSJeeja KP 
201f7590d4fSJeeja KP 	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
202f7590d4fSJeeja KP 		if (is_fe) {
203f7590d4fSJeeja KP 			in_fixup = m_cfg->params_fixup;
204f7590d4fSJeeja KP 			out_fixup = (~m_cfg->converter) &
205f7590d4fSJeeja KP 					m_cfg->params_fixup;
206f7590d4fSJeeja KP 		} else {
207f7590d4fSJeeja KP 			out_fixup = m_cfg->params_fixup;
208f7590d4fSJeeja KP 			in_fixup = (~m_cfg->converter) &
209f7590d4fSJeeja KP 					m_cfg->params_fixup;
210f7590d4fSJeeja KP 		}
211f7590d4fSJeeja KP 	} else {
212f7590d4fSJeeja KP 		if (is_fe) {
213f7590d4fSJeeja KP 			out_fixup = m_cfg->params_fixup;
214f7590d4fSJeeja KP 			in_fixup = (~m_cfg->converter) &
215f7590d4fSJeeja KP 					m_cfg->params_fixup;
216f7590d4fSJeeja KP 		} else {
217f7590d4fSJeeja KP 			in_fixup = m_cfg->params_fixup;
218f7590d4fSJeeja KP 			out_fixup = (~m_cfg->converter) &
219f7590d4fSJeeja KP 					m_cfg->params_fixup;
220f7590d4fSJeeja KP 		}
221f7590d4fSJeeja KP 	}
222f7590d4fSJeeja KP 
223f7590d4fSJeeja KP 	skl_tplg_update_params(in_fmt, params, in_fixup);
224f7590d4fSJeeja KP 	skl_tplg_update_params(out_fmt, params, out_fixup);
225f7590d4fSJeeja KP }
226f7590d4fSJeeja KP 
227f7590d4fSJeeja KP /*
228f7590d4fSJeeja KP  * A module needs input and output buffers, which are dependent upon pcm
229f7590d4fSJeeja KP  * params, so once we have calculate params, we need buffer calculation as
230f7590d4fSJeeja KP  * well.
231f7590d4fSJeeja KP  */
232bcc2a2dcSCezary Rojewski static void skl_tplg_update_buffer_size(struct skl_dev *skl,
233f7590d4fSJeeja KP 				struct skl_module_cfg *mcfg)
234f7590d4fSJeeja KP {
235f7590d4fSJeeja KP 	int multiplier = 1;
2364cd9899fSHardik T Shah 	struct skl_module_fmt *in_fmt, *out_fmt;
237f6fa56e2SRamesh Babu 	struct skl_module_res *res;
2384cd9899fSHardik T Shah 
2394cd9899fSHardik T Shah 	/* Since fixups is applied to pin 0 only, ibs, obs needs
2404cd9899fSHardik T Shah 	 * change for pin 0 only
2414cd9899fSHardik T Shah 	 */
242e8b374b6SCezary Rojewski 	res = &mcfg->module->resources[mcfg->res_idx];
243e8b374b6SCezary Rojewski 	in_fmt = &mcfg->module->formats[mcfg->fmt_idx].inputs[0].fmt;
244e8b374b6SCezary Rojewski 	out_fmt = &mcfg->module->formats[mcfg->fmt_idx].outputs[0].fmt;
245f7590d4fSJeeja KP 
246f7590d4fSJeeja KP 	if (mcfg->m_type == SKL_MODULE_TYPE_SRCINT)
247f7590d4fSJeeja KP 		multiplier = 5;
248f0c8e1d9SSubhransu S. Prusty 
249f6fa56e2SRamesh Babu 	res->ibs = DIV_ROUND_UP(in_fmt->s_freq, 1000) *
250998d6fb5STakashi Sakamoto 			in_fmt->channels * (in_fmt->bit_depth >> 3) *
251f7590d4fSJeeja KP 			multiplier;
252f7590d4fSJeeja KP 
253f6fa56e2SRamesh Babu 	res->obs = DIV_ROUND_UP(out_fmt->s_freq, 1000) *
254998d6fb5STakashi Sakamoto 			out_fmt->channels * (out_fmt->bit_depth >> 3) *
255f7590d4fSJeeja KP 			multiplier;
256f7590d4fSJeeja KP }
257f7590d4fSJeeja KP 
258db2f586bSSenthilnathan Veppur static u8 skl_tplg_be_dev_type(int dev_type)
259db2f586bSSenthilnathan Veppur {
260db2f586bSSenthilnathan Veppur 	int ret;
261db2f586bSSenthilnathan Veppur 
262db2f586bSSenthilnathan Veppur 	switch (dev_type) {
263db2f586bSSenthilnathan Veppur 	case SKL_DEVICE_BT:
264db2f586bSSenthilnathan Veppur 		ret = NHLT_DEVICE_BT;
265db2f586bSSenthilnathan Veppur 		break;
266db2f586bSSenthilnathan Veppur 
267db2f586bSSenthilnathan Veppur 	case SKL_DEVICE_DMIC:
268db2f586bSSenthilnathan Veppur 		ret = NHLT_DEVICE_DMIC;
269db2f586bSSenthilnathan Veppur 		break;
270db2f586bSSenthilnathan Veppur 
271db2f586bSSenthilnathan Veppur 	case SKL_DEVICE_I2S:
272db2f586bSSenthilnathan Veppur 		ret = NHLT_DEVICE_I2S;
273db2f586bSSenthilnathan Veppur 		break;
274db2f586bSSenthilnathan Veppur 
275db2f586bSSenthilnathan Veppur 	default:
276db2f586bSSenthilnathan Veppur 		ret = NHLT_DEVICE_INVALID;
277db2f586bSSenthilnathan Veppur 		break;
278db2f586bSSenthilnathan Veppur 	}
279db2f586bSSenthilnathan Veppur 
280db2f586bSSenthilnathan Veppur 	return ret;
281db2f586bSSenthilnathan Veppur }
282db2f586bSSenthilnathan Veppur 
2832d1419a3SJeeja KP static int skl_tplg_update_be_blob(struct snd_soc_dapm_widget *w,
284bcc2a2dcSCezary Rojewski 						struct skl_dev *skl)
2852d1419a3SJeeja KP {
2862d1419a3SJeeja KP 	struct skl_module_cfg *m_cfg = w->priv;
2872d1419a3SJeeja KP 	int link_type, dir;
2882d1419a3SJeeja KP 	u32 ch, s_freq, s_fmt;
2892d1419a3SJeeja KP 	struct nhlt_specific_cfg *cfg;
290db2f586bSSenthilnathan Veppur 	u8 dev_type = skl_tplg_be_dev_type(m_cfg->dev_type);
291f6fa56e2SRamesh Babu 	int fmt_idx = m_cfg->fmt_idx;
292f6fa56e2SRamesh Babu 	struct skl_module_iface *m_iface = &m_cfg->module->formats[fmt_idx];
2932d1419a3SJeeja KP 
2942d1419a3SJeeja KP 	/* check if we already have blob */
2952d1419a3SJeeja KP 	if (m_cfg->formats_config.caps_size > 0)
2962d1419a3SJeeja KP 		return 0;
2972d1419a3SJeeja KP 
298bcc2a2dcSCezary Rojewski 	dev_dbg(skl->dev, "Applying default cfg blob\n");
2992d1419a3SJeeja KP 	switch (m_cfg->dev_type) {
3002d1419a3SJeeja KP 	case SKL_DEVICE_DMIC:
3012d1419a3SJeeja KP 		link_type = NHLT_LINK_DMIC;
302c7c6c736SJeeja KP 		dir = SNDRV_PCM_STREAM_CAPTURE;
303f6fa56e2SRamesh Babu 		s_freq = m_iface->inputs[0].fmt.s_freq;
304f6fa56e2SRamesh Babu 		s_fmt = m_iface->inputs[0].fmt.bit_depth;
305f6fa56e2SRamesh Babu 		ch = m_iface->inputs[0].fmt.channels;
3062d1419a3SJeeja KP 		break;
3072d1419a3SJeeja KP 
3082d1419a3SJeeja KP 	case SKL_DEVICE_I2S:
3092d1419a3SJeeja KP 		link_type = NHLT_LINK_SSP;
3102d1419a3SJeeja KP 		if (m_cfg->hw_conn_type == SKL_CONN_SOURCE) {
311c7c6c736SJeeja KP 			dir = SNDRV_PCM_STREAM_PLAYBACK;
312f6fa56e2SRamesh Babu 			s_freq = m_iface->outputs[0].fmt.s_freq;
313f6fa56e2SRamesh Babu 			s_fmt = m_iface->outputs[0].fmt.bit_depth;
314f6fa56e2SRamesh Babu 			ch = m_iface->outputs[0].fmt.channels;
315c7c6c736SJeeja KP 		} else {
316c7c6c736SJeeja KP 			dir = SNDRV_PCM_STREAM_CAPTURE;
317f6fa56e2SRamesh Babu 			s_freq = m_iface->inputs[0].fmt.s_freq;
318f6fa56e2SRamesh Babu 			s_fmt = m_iface->inputs[0].fmt.bit_depth;
319f6fa56e2SRamesh Babu 			ch = m_iface->inputs[0].fmt.channels;
3202d1419a3SJeeja KP 		}
3212d1419a3SJeeja KP 		break;
3222d1419a3SJeeja KP 
3232d1419a3SJeeja KP 	default:
3242d1419a3SJeeja KP 		return -EINVAL;
3252d1419a3SJeeja KP 	}
3262d1419a3SJeeja KP 
3272d1419a3SJeeja KP 	/* update the blob based on virtual bus_id and default params */
3282d1419a3SJeeja KP 	cfg = skl_get_ep_blob(skl, m_cfg->vbus_id, link_type,
329db2f586bSSenthilnathan Veppur 					s_fmt, ch, s_freq, dir, dev_type);
3302d1419a3SJeeja KP 	if (cfg) {
3312d1419a3SJeeja KP 		m_cfg->formats_config.caps_size = cfg->size;
3322d1419a3SJeeja KP 		m_cfg->formats_config.caps = (u32 *) &cfg->caps;
3332d1419a3SJeeja KP 	} else {
334bcc2a2dcSCezary Rojewski 		dev_err(skl->dev, "Blob NULL for id %x type %d dirn %d\n",
3352d1419a3SJeeja KP 					m_cfg->vbus_id, link_type, dir);
336bcc2a2dcSCezary Rojewski 		dev_err(skl->dev, "PCM: ch %d, freq %d, fmt %d\n",
3372d1419a3SJeeja KP 					ch, s_freq, s_fmt);
3382d1419a3SJeeja KP 		return -EIO;
3392d1419a3SJeeja KP 	}
3402d1419a3SJeeja KP 
3412d1419a3SJeeja KP 	return 0;
3422d1419a3SJeeja KP }
3432d1419a3SJeeja KP 
344f7590d4fSJeeja KP static void skl_tplg_update_module_params(struct snd_soc_dapm_widget *w,
345bcc2a2dcSCezary Rojewski 							struct skl_dev *skl)
346f7590d4fSJeeja KP {
347f7590d4fSJeeja KP 	struct skl_module_cfg *m_cfg = w->priv;
348f7590d4fSJeeja KP 	struct skl_pipe_params *params = m_cfg->pipe->p_params;
349f7590d4fSJeeja KP 	int p_conn_type = m_cfg->pipe->conn_type;
350f7590d4fSJeeja KP 	bool is_fe;
351f7590d4fSJeeja KP 
352f7590d4fSJeeja KP 	if (!m_cfg->params_fixup)
353f7590d4fSJeeja KP 		return;
354f7590d4fSJeeja KP 
355bcc2a2dcSCezary Rojewski 	dev_dbg(skl->dev, "Mconfig for widget=%s BEFORE updation\n",
356f7590d4fSJeeja KP 				w->name);
357f7590d4fSJeeja KP 
358bcc2a2dcSCezary Rojewski 	skl_dump_mconfig(skl, m_cfg);
359f7590d4fSJeeja KP 
360f7590d4fSJeeja KP 	if (p_conn_type == SKL_PIPE_CONN_TYPE_FE)
361f7590d4fSJeeja KP 		is_fe = true;
362f7590d4fSJeeja KP 	else
363f7590d4fSJeeja KP 		is_fe = false;
364f7590d4fSJeeja KP 
365f7590d4fSJeeja KP 	skl_tplg_update_params_fixup(m_cfg, params, is_fe);
366bcc2a2dcSCezary Rojewski 	skl_tplg_update_buffer_size(skl, m_cfg);
367f7590d4fSJeeja KP 
368bcc2a2dcSCezary Rojewski 	dev_dbg(skl->dev, "Mconfig for widget=%s AFTER updation\n",
369f7590d4fSJeeja KP 				w->name);
370f7590d4fSJeeja KP 
371bcc2a2dcSCezary Rojewski 	skl_dump_mconfig(skl, m_cfg);
372f7590d4fSJeeja KP }
373f7590d4fSJeeja KP 
374e4e2d2f4SJeeja KP /*
375abb74003SJeeja KP  * some modules can have multiple params set from user control and
376abb74003SJeeja KP  * need to be set after module is initialized. If set_param flag is
377abb74003SJeeja KP  * set module params will be done after module is initialised.
378abb74003SJeeja KP  */
379abb74003SJeeja KP static int skl_tplg_set_module_params(struct snd_soc_dapm_widget *w,
380bcc2a2dcSCezary Rojewski 						struct skl_dev *skl)
381abb74003SJeeja KP {
382abb74003SJeeja KP 	int i, ret;
383abb74003SJeeja KP 	struct skl_module_cfg *mconfig = w->priv;
384abb74003SJeeja KP 	const struct snd_kcontrol_new *k;
385abb74003SJeeja KP 	struct soc_bytes_ext *sb;
386abb74003SJeeja KP 	struct skl_algo_data *bc;
387abb74003SJeeja KP 	struct skl_specific_cfg *sp_cfg;
388abb74003SJeeja KP 
389abb74003SJeeja KP 	if (mconfig->formats_config.caps_size > 0 &&
3904ced1827SJeeja KP 		mconfig->formats_config.set_params == SKL_PARAM_SET) {
391abb74003SJeeja KP 		sp_cfg = &mconfig->formats_config;
392bcc2a2dcSCezary Rojewski 		ret = skl_set_module_params(skl, sp_cfg->caps,
393abb74003SJeeja KP 					sp_cfg->caps_size,
394abb74003SJeeja KP 					sp_cfg->param_id, mconfig);
395abb74003SJeeja KP 		if (ret < 0)
396abb74003SJeeja KP 			return ret;
397abb74003SJeeja KP 	}
398abb74003SJeeja KP 
399abb74003SJeeja KP 	for (i = 0; i < w->num_kcontrols; i++) {
400abb74003SJeeja KP 		k = &w->kcontrol_news[i];
401abb74003SJeeja KP 		if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
402abb74003SJeeja KP 			sb = (void *) k->private_value;
403abb74003SJeeja KP 			bc = (struct skl_algo_data *)sb->dobj.private;
404abb74003SJeeja KP 
4054ced1827SJeeja KP 			if (bc->set_params == SKL_PARAM_SET) {
406bcc2a2dcSCezary Rojewski 				ret = skl_set_module_params(skl,
4070d682104SDharageswari R 						(u32 *)bc->params, bc->size,
408abb74003SJeeja KP 						bc->param_id, mconfig);
409abb74003SJeeja KP 				if (ret < 0)
410abb74003SJeeja KP 					return ret;
411abb74003SJeeja KP 			}
412abb74003SJeeja KP 		}
413abb74003SJeeja KP 	}
414abb74003SJeeja KP 
415abb74003SJeeja KP 	return 0;
416abb74003SJeeja KP }
417abb74003SJeeja KP 
418abb74003SJeeja KP /*
419abb74003SJeeja KP  * some module param can set from user control and this is required as
420abb74003SJeeja KP  * when module is initailzed. if module param is required in init it is
421abb74003SJeeja KP  * identifed by set_param flag. if set_param flag is not set, then this
422abb74003SJeeja KP  * parameter needs to set as part of module init.
423abb74003SJeeja KP  */
424abb74003SJeeja KP static int skl_tplg_set_module_init_data(struct snd_soc_dapm_widget *w)
425abb74003SJeeja KP {
426abb74003SJeeja KP 	const struct snd_kcontrol_new *k;
427abb74003SJeeja KP 	struct soc_bytes_ext *sb;
428abb74003SJeeja KP 	struct skl_algo_data *bc;
429abb74003SJeeja KP 	struct skl_module_cfg *mconfig = w->priv;
430abb74003SJeeja KP 	int i;
431abb74003SJeeja KP 
432abb74003SJeeja KP 	for (i = 0; i < w->num_kcontrols; i++) {
433abb74003SJeeja KP 		k = &w->kcontrol_news[i];
434abb74003SJeeja KP 		if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
435abb74003SJeeja KP 			sb = (struct soc_bytes_ext *)k->private_value;
436abb74003SJeeja KP 			bc = (struct skl_algo_data *)sb->dobj.private;
437abb74003SJeeja KP 
4384ced1827SJeeja KP 			if (bc->set_params != SKL_PARAM_INIT)
439abb74003SJeeja KP 				continue;
440abb74003SJeeja KP 
441d1a6fe41STakashi Sakamoto 			mconfig->formats_config.caps = (u32 *)bc->params;
4420d682104SDharageswari R 			mconfig->formats_config.caps_size = bc->size;
443abb74003SJeeja KP 
444abb74003SJeeja KP 			break;
445abb74003SJeeja KP 		}
446abb74003SJeeja KP 	}
447abb74003SJeeja KP 
448abb74003SJeeja KP 	return 0;
449abb74003SJeeja KP }
450abb74003SJeeja KP 
451bcc2a2dcSCezary Rojewski static int skl_tplg_module_prepare(struct skl_dev *skl, struct skl_pipe *pipe,
452bb704a73SJeeja KP 		struct snd_soc_dapm_widget *w, struct skl_module_cfg *mcfg)
453bb704a73SJeeja KP {
454bb704a73SJeeja KP 	switch (mcfg->dev_type) {
455bb704a73SJeeja KP 	case SKL_DEVICE_HDAHOST:
456bcc2a2dcSCezary Rojewski 		return skl_pcm_host_dma_prepare(skl->dev, pipe->p_params);
457bb704a73SJeeja KP 
458bb704a73SJeeja KP 	case SKL_DEVICE_HDALINK:
459bcc2a2dcSCezary Rojewski 		return skl_pcm_link_dma_prepare(skl->dev, pipe->p_params);
460bb704a73SJeeja KP 	}
461bb704a73SJeeja KP 
462bb704a73SJeeja KP 	return 0;
463bb704a73SJeeja KP }
464bb704a73SJeeja KP 
465abb74003SJeeja KP /*
466e4e2d2f4SJeeja KP  * Inside a pipe instance, we can have various modules. These modules need
467e4e2d2f4SJeeja KP  * to instantiated in DSP by invoking INIT_MODULE IPC, which is achieved by
468e4e2d2f4SJeeja KP  * skl_init_module() routine, so invoke that for all modules in a pipeline
469e4e2d2f4SJeeja KP  */
470e4e2d2f4SJeeja KP static int
471bcc2a2dcSCezary Rojewski skl_tplg_init_pipe_modules(struct skl_dev *skl, struct skl_pipe *pipe)
472e4e2d2f4SJeeja KP {
473e4e2d2f4SJeeja KP 	struct skl_pipe_module *w_module;
474e4e2d2f4SJeeja KP 	struct snd_soc_dapm_widget *w;
475e4e2d2f4SJeeja KP 	struct skl_module_cfg *mconfig;
476f6fa56e2SRamesh Babu 	u8 cfg_idx;
477e4e2d2f4SJeeja KP 	int ret = 0;
478e4e2d2f4SJeeja KP 
479e4e2d2f4SJeeja KP 	list_for_each_entry(w_module, &pipe->w_list, node) {
4809e0784d0SAndy Shevchenko 		guid_t *uuid_mod;
481e4e2d2f4SJeeja KP 		w = w_module->w;
482e4e2d2f4SJeeja KP 		mconfig = w->priv;
483e4e2d2f4SJeeja KP 
484b7c50555SVinod Koul 		/* check if module ids are populated */
485b7c50555SVinod Koul 		if (mconfig->id.module_id < 0) {
486bcc2a2dcSCezary Rojewski 			dev_err(skl->dev,
487a657ae7eSVinod Koul 					"module %pUL id not populated\n",
4889e0784d0SAndy Shevchenko 					(guid_t *)mconfig->guid);
489a657ae7eSVinod Koul 			return -EIO;
490b7c50555SVinod Koul 		}
491b7c50555SVinod Koul 
492f6fa56e2SRamesh Babu 		cfg_idx = mconfig->pipe->cur_config_idx;
493f6fa56e2SRamesh Babu 		mconfig->fmt_idx = mconfig->mod_cfg[cfg_idx].fmt_idx;
494f6fa56e2SRamesh Babu 		mconfig->res_idx = mconfig->mod_cfg[cfg_idx].res_idx;
495f6fa56e2SRamesh Babu 
496bcc2a2dcSCezary Rojewski 		if (mconfig->module->loadable && skl->dsp->fw_ops.load_mod) {
497bcc2a2dcSCezary Rojewski 			ret = skl->dsp->fw_ops.load_mod(skl->dsp,
4986c5768b3SDharageswari R 				mconfig->id.module_id, mconfig->guid);
4996c5768b3SDharageswari R 			if (ret < 0)
5006c5768b3SDharageswari R 				return ret;
5016c5768b3SDharageswari R 		}
5026c5768b3SDharageswari R 
503bb704a73SJeeja KP 		/* prepare the DMA if the module is gateway cpr */
504bcc2a2dcSCezary Rojewski 		ret = skl_tplg_module_prepare(skl, pipe, w, mconfig);
505bb704a73SJeeja KP 		if (ret < 0)
506bb704a73SJeeja KP 			return ret;
507bb704a73SJeeja KP 
5082d1419a3SJeeja KP 		/* update blob if blob is null for be with default value */
509bcc2a2dcSCezary Rojewski 		skl_tplg_update_be_blob(w, skl);
5102d1419a3SJeeja KP 
511f7590d4fSJeeja KP 		/*
512f7590d4fSJeeja KP 		 * apply fix/conversion to module params based on
513f7590d4fSJeeja KP 		 * FE/BE params
514f7590d4fSJeeja KP 		 */
515bcc2a2dcSCezary Rojewski 		skl_tplg_update_module_params(w, skl);
5169e0784d0SAndy Shevchenko 		uuid_mod = (guid_t *)mconfig->guid;
517bcc2a2dcSCezary Rojewski 		mconfig->id.pvt_id = skl_get_pvt_id(skl, uuid_mod,
518b26199eaSJeeja KP 						mconfig->id.instance_id);
519ef2a352cSDharageswari R 		if (mconfig->id.pvt_id < 0)
520ef2a352cSDharageswari R 			return ret;
521abb74003SJeeja KP 		skl_tplg_set_module_init_data(w);
5224147a6e5SPardha Saradhi K 
523bcc2a2dcSCezary Rojewski 		ret = skl_dsp_get_core(skl->dsp, mconfig->core_id);
5244147a6e5SPardha Saradhi K 		if (ret < 0) {
525bcc2a2dcSCezary Rojewski 			dev_err(skl->dev, "Failed to wake up core %d ret=%d\n",
5264147a6e5SPardha Saradhi K 						mconfig->core_id, ret);
5274147a6e5SPardha Saradhi K 			return ret;
5284147a6e5SPardha Saradhi K 		}
5294147a6e5SPardha Saradhi K 
530bcc2a2dcSCezary Rojewski 		ret = skl_init_module(skl, mconfig);
531ef2a352cSDharageswari R 		if (ret < 0) {
532bcc2a2dcSCezary Rojewski 			skl_put_pvt_id(skl, uuid_mod, &mconfig->id.pvt_id);
5334147a6e5SPardha Saradhi K 			goto err;
534ef2a352cSDharageswari R 		}
535f2a167caSCezary Rojewski 
536bcc2a2dcSCezary Rojewski 		ret = skl_tplg_set_module_params(w, skl);
537e4e2d2f4SJeeja KP 		if (ret < 0)
5384147a6e5SPardha Saradhi K 			goto err;
539e4e2d2f4SJeeja KP 	}
540e4e2d2f4SJeeja KP 
541e4e2d2f4SJeeja KP 	return 0;
5424147a6e5SPardha Saradhi K err:
543bcc2a2dcSCezary Rojewski 	skl_dsp_put_core(skl->dsp, mconfig->core_id);
5444147a6e5SPardha Saradhi K 	return ret;
545e4e2d2f4SJeeja KP }
546d93f8e55SVinod Koul 
547bcc2a2dcSCezary Rojewski static int skl_tplg_unload_pipe_modules(struct skl_dev *skl,
5486c5768b3SDharageswari R 	 struct skl_pipe *pipe)
5496c5768b3SDharageswari R {
5504147a6e5SPardha Saradhi K 	int ret = 0;
55125722cf6SPierre-Louis Bossart 	struct skl_pipe_module *w_module;
55225722cf6SPierre-Louis Bossart 	struct skl_module_cfg *mconfig;
5536c5768b3SDharageswari R 
5546c5768b3SDharageswari R 	list_for_each_entry(w_module, &pipe->w_list, node) {
5559e0784d0SAndy Shevchenko 		guid_t *uuid_mod;
5566c5768b3SDharageswari R 		mconfig  = w_module->w->priv;
5579e0784d0SAndy Shevchenko 		uuid_mod = (guid_t *)mconfig->guid;
5586c5768b3SDharageswari R 
559*e4e95d82SGustaw Lewandowski 		if (mconfig->module->loadable && skl->dsp->fw_ops.unload_mod) {
560bcc2a2dcSCezary Rojewski 			ret = skl->dsp->fw_ops.unload_mod(skl->dsp,
5616c5768b3SDharageswari R 						mconfig->id.module_id);
562b0fab9c6SDharageswari R 			if (ret < 0)
563b0fab9c6SDharageswari R 				return -EIO;
564b0fab9c6SDharageswari R 		}
565bcc2a2dcSCezary Rojewski 		skl_put_pvt_id(skl, uuid_mod, &mconfig->id.pvt_id);
5664147a6e5SPardha Saradhi K 
567bcc2a2dcSCezary Rojewski 		ret = skl_dsp_put_core(skl->dsp, mconfig->core_id);
5684147a6e5SPardha Saradhi K 		if (ret < 0) {
5694147a6e5SPardha Saradhi K 			/* don't return; continue with other modules */
570bcc2a2dcSCezary Rojewski 			dev_err(skl->dev, "Failed to sleep core %d ret=%d\n",
5714147a6e5SPardha Saradhi K 				mconfig->core_id, ret);
5724147a6e5SPardha Saradhi K 		}
5736c5768b3SDharageswari R 	}
5746c5768b3SDharageswari R 
5756c5768b3SDharageswari R 	/* no modules to unload in this path, so return */
5764147a6e5SPardha Saradhi K 	return ret;
5776c5768b3SDharageswari R }
5786c5768b3SDharageswari R 
5791b450791SMateusz Gorski static bool skl_tplg_is_multi_fmt(struct skl_dev *skl, struct skl_pipe *pipe)
5801b450791SMateusz Gorski {
5811b450791SMateusz Gorski 	struct skl_pipe_fmt *cur_fmt;
5821b450791SMateusz Gorski 	struct skl_pipe_fmt *next_fmt;
5831b450791SMateusz Gorski 	int i;
5841b450791SMateusz Gorski 
5851b450791SMateusz Gorski 	if (pipe->nr_cfgs <= 1)
5861b450791SMateusz Gorski 		return false;
5871b450791SMateusz Gorski 
5881b450791SMateusz Gorski 	if (pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
5891b450791SMateusz Gorski 		return true;
5901b450791SMateusz Gorski 
5911b450791SMateusz Gorski 	for (i = 0; i < pipe->nr_cfgs - 1; i++) {
5921b450791SMateusz Gorski 		if (pipe->direction == SNDRV_PCM_STREAM_PLAYBACK) {
5931b450791SMateusz Gorski 			cur_fmt = &pipe->configs[i].out_fmt;
5941b450791SMateusz Gorski 			next_fmt = &pipe->configs[i + 1].out_fmt;
5951b450791SMateusz Gorski 		} else {
5961b450791SMateusz Gorski 			cur_fmt = &pipe->configs[i].in_fmt;
5971b450791SMateusz Gorski 			next_fmt = &pipe->configs[i + 1].in_fmt;
5981b450791SMateusz Gorski 		}
5991b450791SMateusz Gorski 
6001b450791SMateusz Gorski 		if (!CHECK_HW_PARAMS(cur_fmt->channels, cur_fmt->freq,
6011b450791SMateusz Gorski 				     cur_fmt->bps,
6021b450791SMateusz Gorski 				     next_fmt->channels,
6031b450791SMateusz Gorski 				     next_fmt->freq,
6041b450791SMateusz Gorski 				     next_fmt->bps))
6051b450791SMateusz Gorski 			return true;
6061b450791SMateusz Gorski 	}
6071b450791SMateusz Gorski 
6081b450791SMateusz Gorski 	return false;
6091b450791SMateusz Gorski }
6101b450791SMateusz Gorski 
611d93f8e55SVinod Koul /*
612f6fa56e2SRamesh Babu  * Here, we select pipe format based on the pipe type and pipe
613f6fa56e2SRamesh Babu  * direction to determine the current config index for the pipeline.
614f6fa56e2SRamesh Babu  * The config index is then used to select proper module resources.
615f6fa56e2SRamesh Babu  * Intermediate pipes currently have a fixed format hence we select the
616f6fa56e2SRamesh Babu  * 0th configuratation by default for such pipes.
617f6fa56e2SRamesh Babu  */
618f6fa56e2SRamesh Babu static int
619bcc2a2dcSCezary Rojewski skl_tplg_get_pipe_config(struct skl_dev *skl, struct skl_module_cfg *mconfig)
620f6fa56e2SRamesh Babu {
621f6fa56e2SRamesh Babu 	struct skl_pipe *pipe = mconfig->pipe;
622f6fa56e2SRamesh Babu 	struct skl_pipe_params *params = pipe->p_params;
623f6fa56e2SRamesh Babu 	struct skl_path_config *pconfig = &pipe->configs[0];
624f6fa56e2SRamesh Babu 	struct skl_pipe_fmt *fmt = NULL;
625f6fa56e2SRamesh Babu 	bool in_fmt = false;
626f6fa56e2SRamesh Babu 	int i;
627f6fa56e2SRamesh Babu 
628f6fa56e2SRamesh Babu 	if (pipe->nr_cfgs == 0) {
629f6fa56e2SRamesh Babu 		pipe->cur_config_idx = 0;
630f6fa56e2SRamesh Babu 		return 0;
631f6fa56e2SRamesh Babu 	}
632f6fa56e2SRamesh Babu 
6331b450791SMateusz Gorski 	if (skl_tplg_is_multi_fmt(skl, pipe)) {
6341b450791SMateusz Gorski 		pipe->cur_config_idx = pipe->pipe_config_idx;
6351b450791SMateusz Gorski 		pipe->memory_pages = pconfig->mem_pages;
6361b450791SMateusz Gorski 		dev_dbg(skl->dev, "found pipe config idx:%d\n",
6371b450791SMateusz Gorski 			pipe->cur_config_idx);
6381b450791SMateusz Gorski 		return 0;
6391b450791SMateusz Gorski 	}
6401b450791SMateusz Gorski 
641f6fa56e2SRamesh Babu 	if (pipe->conn_type == SKL_PIPE_CONN_TYPE_NONE) {
642bcc2a2dcSCezary Rojewski 		dev_dbg(skl->dev, "No conn_type detected, take 0th config\n");
643f6fa56e2SRamesh Babu 		pipe->cur_config_idx = 0;
644f6fa56e2SRamesh Babu 		pipe->memory_pages = pconfig->mem_pages;
645f6fa56e2SRamesh Babu 
646f6fa56e2SRamesh Babu 		return 0;
647f6fa56e2SRamesh Babu 	}
648f6fa56e2SRamesh Babu 
649f6fa56e2SRamesh Babu 	if ((pipe->conn_type == SKL_PIPE_CONN_TYPE_FE &&
650f6fa56e2SRamesh Babu 	     pipe->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
651f6fa56e2SRamesh Babu 	     (pipe->conn_type == SKL_PIPE_CONN_TYPE_BE &&
652f6fa56e2SRamesh Babu 	     pipe->direction == SNDRV_PCM_STREAM_CAPTURE))
653f6fa56e2SRamesh Babu 		in_fmt = true;
654f6fa56e2SRamesh Babu 
655f6fa56e2SRamesh Babu 	for (i = 0; i < pipe->nr_cfgs; i++) {
656f6fa56e2SRamesh Babu 		pconfig = &pipe->configs[i];
657f6fa56e2SRamesh Babu 		if (in_fmt)
658f6fa56e2SRamesh Babu 			fmt = &pconfig->in_fmt;
659f6fa56e2SRamesh Babu 		else
660f6fa56e2SRamesh Babu 			fmt = &pconfig->out_fmt;
661f6fa56e2SRamesh Babu 
662f6fa56e2SRamesh Babu 		if (CHECK_HW_PARAMS(params->ch, params->s_freq, params->s_fmt,
663f6fa56e2SRamesh Babu 				    fmt->channels, fmt->freq, fmt->bps)) {
664f6fa56e2SRamesh Babu 			pipe->cur_config_idx = i;
665f6fa56e2SRamesh Babu 			pipe->memory_pages = pconfig->mem_pages;
666bcc2a2dcSCezary Rojewski 			dev_dbg(skl->dev, "Using pipe config: %d\n", i);
667f6fa56e2SRamesh Babu 
668f6fa56e2SRamesh Babu 			return 0;
669f6fa56e2SRamesh Babu 		}
670f6fa56e2SRamesh Babu 	}
671f6fa56e2SRamesh Babu 
672bcc2a2dcSCezary Rojewski 	dev_err(skl->dev, "Invalid pipe config: %d %d %d for pipe: %d\n",
673f6fa56e2SRamesh Babu 		params->ch, params->s_freq, params->s_fmt, pipe->ppl_id);
674f6fa56e2SRamesh Babu 	return -EINVAL;
675f6fa56e2SRamesh Babu }
676f6fa56e2SRamesh Babu 
677f6fa56e2SRamesh Babu /*
678d93f8e55SVinod Koul  * Mixer module represents a pipeline. So in the Pre-PMU event of mixer we
679d93f8e55SVinod Koul  * need create the pipeline. So we do following:
680d93f8e55SVinod Koul  *   - Create the pipeline
681d93f8e55SVinod Koul  *   - Initialize the modules in pipeline
682d93f8e55SVinod Koul  *   - finally bind all modules together
683d93f8e55SVinod Koul  */
684d93f8e55SVinod Koul static int skl_tplg_mixer_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
685bcc2a2dcSCezary Rojewski 							struct skl_dev *skl)
686d93f8e55SVinod Koul {
687d93f8e55SVinod Koul 	int ret;
688d93f8e55SVinod Koul 	struct skl_module_cfg *mconfig = w->priv;
689d93f8e55SVinod Koul 	struct skl_pipe_module *w_module;
690d93f8e55SVinod Koul 	struct skl_pipe *s_pipe = mconfig->pipe;
691b8c722ddSJeeja KP 	struct skl_module_cfg *src_module = NULL, *dst_module, *module;
692b8c722ddSJeeja KP 	struct skl_module_deferred_bind *modules;
693d93f8e55SVinod Koul 
694f6fa56e2SRamesh Babu 	ret = skl_tplg_get_pipe_config(skl, mconfig);
695f6fa56e2SRamesh Babu 	if (ret < 0)
696f6fa56e2SRamesh Babu 		return ret;
697f6fa56e2SRamesh Babu 
698d93f8e55SVinod Koul 	/*
699d93f8e55SVinod Koul 	 * Create a list of modules for pipe.
700d93f8e55SVinod Koul 	 * This list contains modules from source to sink
701d93f8e55SVinod Koul 	 */
702bcc2a2dcSCezary Rojewski 	ret = skl_create_pipeline(skl, mconfig->pipe);
703d93f8e55SVinod Koul 	if (ret < 0)
704d93f8e55SVinod Koul 		return ret;
705d93f8e55SVinod Koul 
706d93f8e55SVinod Koul 	/* Init all pipe modules from source to sink */
707d93f8e55SVinod Koul 	ret = skl_tplg_init_pipe_modules(skl, s_pipe);
708d93f8e55SVinod Koul 	if (ret < 0)
709d93f8e55SVinod Koul 		return ret;
710d93f8e55SVinod Koul 
711d93f8e55SVinod Koul 	/* Bind modules from source to sink */
712d93f8e55SVinod Koul 	list_for_each_entry(w_module, &s_pipe->w_list, node) {
713d93f8e55SVinod Koul 		dst_module = w_module->w->priv;
714d93f8e55SVinod Koul 
715d93f8e55SVinod Koul 		if (src_module == NULL) {
716d93f8e55SVinod Koul 			src_module = dst_module;
717d93f8e55SVinod Koul 			continue;
718d93f8e55SVinod Koul 		}
719d93f8e55SVinod Koul 
720bcc2a2dcSCezary Rojewski 		ret = skl_bind_modules(skl, src_module, dst_module);
721d93f8e55SVinod Koul 		if (ret < 0)
722d93f8e55SVinod Koul 			return ret;
723d93f8e55SVinod Koul 
724d93f8e55SVinod Koul 		src_module = dst_module;
725d93f8e55SVinod Koul 	}
726d93f8e55SVinod Koul 
727b8c722ddSJeeja KP 	/*
728b8c722ddSJeeja KP 	 * When the destination module is initialized, check for these modules
729b8c722ddSJeeja KP 	 * in deferred bind list. If found, bind them.
730b8c722ddSJeeja KP 	 */
731b8c722ddSJeeja KP 	list_for_each_entry(w_module, &s_pipe->w_list, node) {
732b8c722ddSJeeja KP 		if (list_empty(&skl->bind_list))
733b8c722ddSJeeja KP 			break;
734b8c722ddSJeeja KP 
735b8c722ddSJeeja KP 		list_for_each_entry(modules, &skl->bind_list, node) {
736b8c722ddSJeeja KP 			module = w_module->w->priv;
737b8c722ddSJeeja KP 			if (modules->dst == module)
738bcc2a2dcSCezary Rojewski 				skl_bind_modules(skl, modules->src,
739b8c722ddSJeeja KP 							modules->dst);
740b8c722ddSJeeja KP 		}
741b8c722ddSJeeja KP 	}
742b8c722ddSJeeja KP 
743d93f8e55SVinod Koul 	return 0;
744d93f8e55SVinod Koul }
745d93f8e55SVinod Koul 
746bcc2a2dcSCezary Rojewski static int skl_fill_sink_instance_id(struct skl_dev *skl, u32 *params,
747bf3e5ef5SDharageswari R 				int size, struct skl_module_cfg *mcfg)
7485e8f0ee4SDharageswari R {
7495e8f0ee4SDharageswari R 	int i, pvt_id;
7505e8f0ee4SDharageswari R 
751bf3e5ef5SDharageswari R 	if (mcfg->m_type == SKL_MODULE_TYPE_KPB) {
752bf3e5ef5SDharageswari R 		struct skl_kpb_params *kpb_params =
753bf3e5ef5SDharageswari R 				(struct skl_kpb_params *)params;
754f7a9f772SSriram Periyasamy 		struct skl_mod_inst_map *inst = kpb_params->u.map;
7555e8f0ee4SDharageswari R 
756bf3e5ef5SDharageswari R 		for (i = 0; i < kpb_params->num_modules; i++) {
757bcc2a2dcSCezary Rojewski 			pvt_id = skl_get_pvt_instance_id_map(skl, inst->mod_id,
758bf3e5ef5SDharageswari R 								inst->inst_id);
7595e8f0ee4SDharageswari R 			if (pvt_id < 0)
7605e8f0ee4SDharageswari R 				return -EINVAL;
761bf3e5ef5SDharageswari R 
7625e8f0ee4SDharageswari R 			inst->inst_id = pvt_id;
7635e8f0ee4SDharageswari R 			inst++;
7645e8f0ee4SDharageswari R 		}
7655e8f0ee4SDharageswari R 	}
7665e8f0ee4SDharageswari R 
767bf3e5ef5SDharageswari R 	return 0;
768bf3e5ef5SDharageswari R }
769cc6a4044SJeeja KP /*
770cc6a4044SJeeja KP  * Some modules require params to be set after the module is bound to
771cc6a4044SJeeja KP  * all pins connected.
772cc6a4044SJeeja KP  *
773cc6a4044SJeeja KP  * The module provider initializes set_param flag for such modules and we
774cc6a4044SJeeja KP  * send params after binding
775cc6a4044SJeeja KP  */
776cc6a4044SJeeja KP static int skl_tplg_set_module_bind_params(struct snd_soc_dapm_widget *w,
777bcc2a2dcSCezary Rojewski 			struct skl_module_cfg *mcfg, struct skl_dev *skl)
778cc6a4044SJeeja KP {
779cc6a4044SJeeja KP 	int i, ret;
780cc6a4044SJeeja KP 	struct skl_module_cfg *mconfig = w->priv;
781cc6a4044SJeeja KP 	const struct snd_kcontrol_new *k;
782cc6a4044SJeeja KP 	struct soc_bytes_ext *sb;
783cc6a4044SJeeja KP 	struct skl_algo_data *bc;
784cc6a4044SJeeja KP 	struct skl_specific_cfg *sp_cfg;
785bf3e5ef5SDharageswari R 	u32 *params;
786cc6a4044SJeeja KP 
787cc6a4044SJeeja KP 	/*
788cc6a4044SJeeja KP 	 * check all out/in pins are in bind state.
789cc6a4044SJeeja KP 	 * if so set the module param
790cc6a4044SJeeja KP 	 */
791f6fa56e2SRamesh Babu 	for (i = 0; i < mcfg->module->max_output_pins; i++) {
792cc6a4044SJeeja KP 		if (mcfg->m_out_pin[i].pin_state != SKL_PIN_BIND_DONE)
793cc6a4044SJeeja KP 			return 0;
794cc6a4044SJeeja KP 	}
795cc6a4044SJeeja KP 
796f6fa56e2SRamesh Babu 	for (i = 0; i < mcfg->module->max_input_pins; i++) {
797cc6a4044SJeeja KP 		if (mcfg->m_in_pin[i].pin_state != SKL_PIN_BIND_DONE)
798cc6a4044SJeeja KP 			return 0;
799cc6a4044SJeeja KP 	}
800cc6a4044SJeeja KP 
801cc6a4044SJeeja KP 	if (mconfig->formats_config.caps_size > 0 &&
802cc6a4044SJeeja KP 		mconfig->formats_config.set_params == SKL_PARAM_BIND) {
803cc6a4044SJeeja KP 		sp_cfg = &mconfig->formats_config;
804bcc2a2dcSCezary Rojewski 		ret = skl_set_module_params(skl, sp_cfg->caps,
805cc6a4044SJeeja KP 					sp_cfg->caps_size,
806cc6a4044SJeeja KP 					sp_cfg->param_id, mconfig);
807cc6a4044SJeeja KP 		if (ret < 0)
808cc6a4044SJeeja KP 			return ret;
809cc6a4044SJeeja KP 	}
810cc6a4044SJeeja KP 
811cc6a4044SJeeja KP 	for (i = 0; i < w->num_kcontrols; i++) {
812cc6a4044SJeeja KP 		k = &w->kcontrol_news[i];
813cc6a4044SJeeja KP 		if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
814cc6a4044SJeeja KP 			sb = (void *) k->private_value;
815cc6a4044SJeeja KP 			bc = (struct skl_algo_data *)sb->dobj.private;
816cc6a4044SJeeja KP 
817cc6a4044SJeeja KP 			if (bc->set_params == SKL_PARAM_BIND) {
818ca92cc46Szhong jiang 				params = kmemdup(bc->params, bc->max, GFP_KERNEL);
819bf3e5ef5SDharageswari R 				if (!params)
820bf3e5ef5SDharageswari R 					return -ENOMEM;
821bf3e5ef5SDharageswari R 
822bcc2a2dcSCezary Rojewski 				skl_fill_sink_instance_id(skl, params, bc->max,
823bf3e5ef5SDharageswari R 								mconfig);
824bf3e5ef5SDharageswari R 
825bcc2a2dcSCezary Rojewski 				ret = skl_set_module_params(skl, params,
826bf3e5ef5SDharageswari R 						bc->max, bc->param_id, mconfig);
827bf3e5ef5SDharageswari R 				kfree(params);
828bf3e5ef5SDharageswari R 
829cc6a4044SJeeja KP 				if (ret < 0)
830cc6a4044SJeeja KP 					return ret;
831cc6a4044SJeeja KP 			}
832cc6a4044SJeeja KP 		}
833cc6a4044SJeeja KP 	}
834cc6a4044SJeeja KP 
835cc6a4044SJeeja KP 	return 0;
836cc6a4044SJeeja KP }
837cc6a4044SJeeja KP 
838bcc2a2dcSCezary Rojewski static int skl_get_module_id(struct skl_dev *skl, guid_t *uuid)
839f7a9f772SSriram Periyasamy {
840f7a9f772SSriram Periyasamy 	struct uuid_module *module;
841f7a9f772SSriram Periyasamy 
842bcc2a2dcSCezary Rojewski 	list_for_each_entry(module, &skl->uuid_list, list) {
8439e0784d0SAndy Shevchenko 		if (guid_equal(uuid, &module->uuid))
844f7a9f772SSriram Periyasamy 			return module->id;
845f7a9f772SSriram Periyasamy 	}
846f7a9f772SSriram Periyasamy 
847f7a9f772SSriram Periyasamy 	return -EINVAL;
848f7a9f772SSriram Periyasamy }
849f7a9f772SSriram Periyasamy 
850bcc2a2dcSCezary Rojewski static int skl_tplg_find_moduleid_from_uuid(struct skl_dev *skl,
851f7a9f772SSriram Periyasamy 					const struct snd_kcontrol_new *k)
852f7a9f772SSriram Periyasamy {
853f7a9f772SSriram Periyasamy 	struct soc_bytes_ext *sb = (void *) k->private_value;
854f7a9f772SSriram Periyasamy 	struct skl_algo_data *bc = (struct skl_algo_data *)sb->dobj.private;
855f7a9f772SSriram Periyasamy 	struct skl_kpb_params *uuid_params, *params;
85676f56faeSRakesh Ughreja 	struct hdac_bus *bus = skl_to_bus(skl);
857f7a9f772SSriram Periyasamy 	int i, size, module_id;
858f7a9f772SSriram Periyasamy 
859f7a9f772SSriram Periyasamy 	if (bc->set_params == SKL_PARAM_BIND && bc->max) {
860f7a9f772SSriram Periyasamy 		uuid_params = (struct skl_kpb_params *)bc->params;
861d00cc2f1SGustavo A. R. Silva 		size = struct_size(params, u.map, uuid_params->num_modules);
862f7a9f772SSriram Periyasamy 
863f7a9f772SSriram Periyasamy 		params = devm_kzalloc(bus->dev, size, GFP_KERNEL);
864f7a9f772SSriram Periyasamy 		if (!params)
865f7a9f772SSriram Periyasamy 			return -ENOMEM;
866f7a9f772SSriram Periyasamy 
867f7a9f772SSriram Periyasamy 		params->num_modules = uuid_params->num_modules;
868f7a9f772SSriram Periyasamy 
869f7a9f772SSriram Periyasamy 		for (i = 0; i < uuid_params->num_modules; i++) {
870bcc2a2dcSCezary Rojewski 			module_id = skl_get_module_id(skl,
871f7a9f772SSriram Periyasamy 				&uuid_params->u.map_uuid[i].mod_uuid);
872f7a9f772SSriram Periyasamy 			if (module_id < 0) {
873f7a9f772SSriram Periyasamy 				devm_kfree(bus->dev, params);
874f7a9f772SSriram Periyasamy 				return -EINVAL;
875f7a9f772SSriram Periyasamy 			}
876f7a9f772SSriram Periyasamy 
877f7a9f772SSriram Periyasamy 			params->u.map[i].mod_id = module_id;
878f7a9f772SSriram Periyasamy 			params->u.map[i].inst_id =
879f7a9f772SSriram Periyasamy 				uuid_params->u.map_uuid[i].inst_id;
880f7a9f772SSriram Periyasamy 		}
881f7a9f772SSriram Periyasamy 
882f7a9f772SSriram Periyasamy 		devm_kfree(bus->dev, bc->params);
883f7a9f772SSriram Periyasamy 		bc->params = (char *)params;
884f7a9f772SSriram Periyasamy 		bc->max = size;
885f7a9f772SSriram Periyasamy 	}
886f7a9f772SSriram Periyasamy 
887f7a9f772SSriram Periyasamy 	return 0;
888f7a9f772SSriram Periyasamy }
889f7a9f772SSriram Periyasamy 
890f7a9f772SSriram Periyasamy /*
891f7a9f772SSriram Periyasamy  * Retrieve the module id from UUID mentioned in the
892f7a9f772SSriram Periyasamy  * post bind params
893f7a9f772SSriram Periyasamy  */
894bcc2a2dcSCezary Rojewski void skl_tplg_add_moduleid_in_bind_params(struct skl_dev *skl,
895f7a9f772SSriram Periyasamy 				struct snd_soc_dapm_widget *w)
896f7a9f772SSriram Periyasamy {
897f7a9f772SSriram Periyasamy 	struct skl_module_cfg *mconfig = w->priv;
898f7a9f772SSriram Periyasamy 	int i;
899f7a9f772SSriram Periyasamy 
900f7a9f772SSriram Periyasamy 	/*
901f7a9f772SSriram Periyasamy 	 * Post bind params are used for only for KPB
902f7a9f772SSriram Periyasamy 	 * to set copier instances to drain the data
903f7a9f772SSriram Periyasamy 	 * in fast mode
904f7a9f772SSriram Periyasamy 	 */
905f7a9f772SSriram Periyasamy 	if (mconfig->m_type != SKL_MODULE_TYPE_KPB)
906f7a9f772SSriram Periyasamy 		return;
907f7a9f772SSriram Periyasamy 
908f7a9f772SSriram Periyasamy 	for (i = 0; i < w->num_kcontrols; i++)
909f7a9f772SSriram Periyasamy 		if ((w->kcontrol_news[i].access &
910f7a9f772SSriram Periyasamy 			SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) &&
911f7a9f772SSriram Periyasamy 			(skl_tplg_find_moduleid_from_uuid(skl,
912f7a9f772SSriram Periyasamy 			&w->kcontrol_news[i]) < 0))
913bcc2a2dcSCezary Rojewski 			dev_err(skl->dev,
914f7a9f772SSriram Periyasamy 				"%s: invalid kpb post bind params\n",
915f7a9f772SSriram Periyasamy 				__func__);
916f7a9f772SSriram Periyasamy }
917b8c722ddSJeeja KP 
918bcc2a2dcSCezary Rojewski static int skl_tplg_module_add_deferred_bind(struct skl_dev *skl,
919b8c722ddSJeeja KP 	struct skl_module_cfg *src, struct skl_module_cfg *dst)
920b8c722ddSJeeja KP {
921b8c722ddSJeeja KP 	struct skl_module_deferred_bind *m_list, *modules;
922b8c722ddSJeeja KP 	int i;
923b8c722ddSJeeja KP 
924b8c722ddSJeeja KP 	/* only supported for module with static pin connection */
925f6fa56e2SRamesh Babu 	for (i = 0; i < dst->module->max_input_pins; i++) {
926b8c722ddSJeeja KP 		struct skl_module_pin *pin = &dst->m_in_pin[i];
927b8c722ddSJeeja KP 
928b8c722ddSJeeja KP 		if (pin->is_dynamic)
929b8c722ddSJeeja KP 			continue;
930b8c722ddSJeeja KP 
931b8c722ddSJeeja KP 		if ((pin->id.module_id  == src->id.module_id) &&
932b8c722ddSJeeja KP 			(pin->id.instance_id  == src->id.instance_id)) {
933b8c722ddSJeeja KP 
934b8c722ddSJeeja KP 			if (!list_empty(&skl->bind_list)) {
935b8c722ddSJeeja KP 				list_for_each_entry(modules, &skl->bind_list, node) {
936b8c722ddSJeeja KP 					if (modules->src == src && modules->dst == dst)
937b8c722ddSJeeja KP 						return 0;
938b8c722ddSJeeja KP 				}
939b8c722ddSJeeja KP 			}
940b8c722ddSJeeja KP 
941b8c722ddSJeeja KP 			m_list = kzalloc(sizeof(*m_list), GFP_KERNEL);
942b8c722ddSJeeja KP 			if (!m_list)
943b8c722ddSJeeja KP 				return -ENOMEM;
944b8c722ddSJeeja KP 
945b8c722ddSJeeja KP 			m_list->src = src;
946b8c722ddSJeeja KP 			m_list->dst = dst;
947b8c722ddSJeeja KP 
948b8c722ddSJeeja KP 			list_add(&m_list->node, &skl->bind_list);
949b8c722ddSJeeja KP 		}
950b8c722ddSJeeja KP 	}
951b8c722ddSJeeja KP 
952b8c722ddSJeeja KP 	return 0;
953b8c722ddSJeeja KP }
954b8c722ddSJeeja KP 
9558724ff17SJeeja KP static int skl_tplg_bind_sinks(struct snd_soc_dapm_widget *w,
956bcc2a2dcSCezary Rojewski 				struct skl_dev *skl,
9576bd4cf85SJeeja KP 				struct snd_soc_dapm_widget *src_w,
9588724ff17SJeeja KP 				struct skl_module_cfg *src_mconfig)
959d93f8e55SVinod Koul {
960d93f8e55SVinod Koul 	struct snd_soc_dapm_path *p;
9610ed95d76SJeeja KP 	struct snd_soc_dapm_widget *sink = NULL, *next_sink = NULL;
9628724ff17SJeeja KP 	struct skl_module_cfg *sink_mconfig;
9638724ff17SJeeja KP 	int ret;
964d93f8e55SVinod Koul 
9658724ff17SJeeja KP 	snd_soc_dapm_widget_for_each_sink_path(w, p) {
966d93f8e55SVinod Koul 		if (!p->connect)
967d93f8e55SVinod Koul 			continue;
968d93f8e55SVinod Koul 
969bcc2a2dcSCezary Rojewski 		dev_dbg(skl->dev,
970bcc2a2dcSCezary Rojewski 			"%s: src widget=%s\n", __func__, w->name);
971bcc2a2dcSCezary Rojewski 		dev_dbg(skl->dev,
972bcc2a2dcSCezary Rojewski 			"%s: sink widget=%s\n", __func__, p->sink->name);
973d93f8e55SVinod Koul 
9740ed95d76SJeeja KP 		next_sink = p->sink;
9756bd4cf85SJeeja KP 
976bcc2a2dcSCezary Rojewski 		if (!is_skl_dsp_widget_type(p->sink, skl->dev))
9776bd4cf85SJeeja KP 			return skl_tplg_bind_sinks(p->sink, skl, src_w, src_mconfig);
9786bd4cf85SJeeja KP 
979d93f8e55SVinod Koul 		/*
980d93f8e55SVinod Koul 		 * here we will check widgets in sink pipelines, so that
981d93f8e55SVinod Koul 		 * can be any widgets type and we are only interested if
982d93f8e55SVinod Koul 		 * they are ones used for SKL so check that first
983d93f8e55SVinod Koul 		 */
984d93f8e55SVinod Koul 		if ((p->sink->priv != NULL) &&
985bcc2a2dcSCezary Rojewski 				is_skl_dsp_widget_type(p->sink, skl->dev)) {
986d93f8e55SVinod Koul 
987d93f8e55SVinod Koul 			sink = p->sink;
988d93f8e55SVinod Koul 			sink_mconfig = sink->priv;
989d93f8e55SVinod Koul 
990b8c722ddSJeeja KP 			/*
991b8c722ddSJeeja KP 			 * Modules other than PGA leaf can be connected
992b8c722ddSJeeja KP 			 * directly or via switch to a module in another
993b8c722ddSJeeja KP 			 * pipeline. EX: reference path
994b8c722ddSJeeja KP 			 * when the path is enabled, the dst module that needs
995b8c722ddSJeeja KP 			 * to be bound may not be initialized. if the module is
996b8c722ddSJeeja KP 			 * not initialized, add these modules in the deferred
997b8c722ddSJeeja KP 			 * bind list and when the dst module is initialised,
998b8c722ddSJeeja KP 			 * bind this module to the dst_module in deferred list.
999b8c722ddSJeeja KP 			 */
1000b8c722ddSJeeja KP 			if (((src_mconfig->m_state == SKL_MODULE_INIT_DONE)
1001b8c722ddSJeeja KP 				&& (sink_mconfig->m_state == SKL_MODULE_UNINIT))) {
1002b8c722ddSJeeja KP 
1003b8c722ddSJeeja KP 				ret = skl_tplg_module_add_deferred_bind(skl,
1004b8c722ddSJeeja KP 						src_mconfig, sink_mconfig);
1005b8c722ddSJeeja KP 
1006b8c722ddSJeeja KP 				if (ret < 0)
1007b8c722ddSJeeja KP 					return ret;
1008b8c722ddSJeeja KP 
1009b8c722ddSJeeja KP 			}
1010b8c722ddSJeeja KP 
1011b8c722ddSJeeja KP 
1012cc6a4044SJeeja KP 			if (src_mconfig->m_state == SKL_MODULE_UNINIT ||
1013cc6a4044SJeeja KP 				sink_mconfig->m_state == SKL_MODULE_UNINIT)
1014cc6a4044SJeeja KP 				continue;
1015cc6a4044SJeeja KP 
1016d93f8e55SVinod Koul 			/* Bind source to sink, mixin is always source */
1017bcc2a2dcSCezary Rojewski 			ret = skl_bind_modules(skl, src_mconfig, sink_mconfig);
1018d93f8e55SVinod Koul 			if (ret)
1019d93f8e55SVinod Koul 				return ret;
1020d93f8e55SVinod Koul 
1021cc6a4044SJeeja KP 			/* set module params after bind */
1022bcc2a2dcSCezary Rojewski 			skl_tplg_set_module_bind_params(src_w,
1023bcc2a2dcSCezary Rojewski 					src_mconfig, skl);
1024bcc2a2dcSCezary Rojewski 			skl_tplg_set_module_bind_params(sink,
1025bcc2a2dcSCezary Rojewski 					sink_mconfig, skl);
1026cc6a4044SJeeja KP 
1027d93f8e55SVinod Koul 			/* Start sinks pipe first */
1028d93f8e55SVinod Koul 			if (sink_mconfig->pipe->state != SKL_PIPE_STARTED) {
1029d1730c3dSJeeja KP 				if (sink_mconfig->pipe->conn_type !=
1030d1730c3dSJeeja KP 							SKL_PIPE_CONN_TYPE_FE)
1031bcc2a2dcSCezary Rojewski 					ret = skl_run_pipe(skl,
1032d1730c3dSJeeja KP 							sink_mconfig->pipe);
1033d93f8e55SVinod Koul 				if (ret)
1034d93f8e55SVinod Koul 					return ret;
1035d93f8e55SVinod Koul 			}
1036d93f8e55SVinod Koul 		}
1037d93f8e55SVinod Koul 	}
1038d93f8e55SVinod Koul 
103910a5439fSguneshwor.o.singh@intel.com 	if (!sink && next_sink)
10406bd4cf85SJeeja KP 		return skl_tplg_bind_sinks(next_sink, skl, src_w, src_mconfig);
10418724ff17SJeeja KP 
10428724ff17SJeeja KP 	return 0;
10438724ff17SJeeja KP }
10448724ff17SJeeja KP 
1045d93f8e55SVinod Koul /*
1046d93f8e55SVinod Koul  * A PGA represents a module in a pipeline. So in the Pre-PMU event of PGA
1047d93f8e55SVinod Koul  * we need to do following:
1048d93f8e55SVinod Koul  *   - Bind to sink pipeline
1049d93f8e55SVinod Koul  *      Since the sink pipes can be running and we don't get mixer event on
1050d93f8e55SVinod Koul  *      connect for already running mixer, we need to find the sink pipes
1051d93f8e55SVinod Koul  *      here and bind to them. This way dynamic connect works.
1052d93f8e55SVinod Koul  *   - Start sink pipeline, if not running
1053d93f8e55SVinod Koul  *   - Then run current pipe
1054d93f8e55SVinod Koul  */
1055d93f8e55SVinod Koul static int skl_tplg_pga_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
1056bcc2a2dcSCezary Rojewski 							struct skl_dev *skl)
1057d93f8e55SVinod Koul {
10588724ff17SJeeja KP 	struct skl_module_cfg *src_mconfig;
1059d93f8e55SVinod Koul 	int ret = 0;
1060d93f8e55SVinod Koul 
10618724ff17SJeeja KP 	src_mconfig = w->priv;
1062d93f8e55SVinod Koul 
1063d93f8e55SVinod Koul 	/*
1064d93f8e55SVinod Koul 	 * find which sink it is connected to, bind with the sink,
1065d93f8e55SVinod Koul 	 * if sink is not started, start sink pipe first, then start
1066d93f8e55SVinod Koul 	 * this pipe
1067d93f8e55SVinod Koul 	 */
10686bd4cf85SJeeja KP 	ret = skl_tplg_bind_sinks(w, skl, w, src_mconfig);
10698724ff17SJeeja KP 	if (ret)
10708724ff17SJeeja KP 		return ret;
10718724ff17SJeeja KP 
1072d93f8e55SVinod Koul 	/* Start source pipe last after starting all sinks */
1073d1730c3dSJeeja KP 	if (src_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
1074bcc2a2dcSCezary Rojewski 		return skl_run_pipe(skl, src_mconfig->pipe);
1075d93f8e55SVinod Koul 
1076d93f8e55SVinod Koul 	return 0;
1077d93f8e55SVinod Koul }
1078d93f8e55SVinod Koul 
10798724ff17SJeeja KP static struct snd_soc_dapm_widget *skl_get_src_dsp_widget(
1080bcc2a2dcSCezary Rojewski 		struct snd_soc_dapm_widget *w, struct skl_dev *skl)
10818724ff17SJeeja KP {
10828724ff17SJeeja KP 	struct snd_soc_dapm_path *p;
10838724ff17SJeeja KP 	struct snd_soc_dapm_widget *src_w = NULL;
10848724ff17SJeeja KP 
1085d93f8e55SVinod Koul 	snd_soc_dapm_widget_for_each_source_path(w, p) {
10868724ff17SJeeja KP 		src_w = p->source;
1087d93f8e55SVinod Koul 		if (!p->connect)
1088d93f8e55SVinod Koul 			continue;
1089d93f8e55SVinod Koul 
1090bcc2a2dcSCezary Rojewski 		dev_dbg(skl->dev, "sink widget=%s\n", w->name);
1091bcc2a2dcSCezary Rojewski 		dev_dbg(skl->dev, "src widget=%s\n", p->source->name);
1092d93f8e55SVinod Koul 
1093d93f8e55SVinod Koul 		/*
10948724ff17SJeeja KP 		 * here we will check widgets in sink pipelines, so that can
10958724ff17SJeeja KP 		 * be any widgets type and we are only interested if they are
10968724ff17SJeeja KP 		 * ones used for SKL so check that first
1097d93f8e55SVinod Koul 		 */
10988724ff17SJeeja KP 		if ((p->source->priv != NULL) &&
1099bcc2a2dcSCezary Rojewski 				is_skl_dsp_widget_type(p->source, skl->dev)) {
11008724ff17SJeeja KP 			return p->source;
1101d93f8e55SVinod Koul 		}
1102d93f8e55SVinod Koul 	}
1103d93f8e55SVinod Koul 
11048724ff17SJeeja KP 	if (src_w != NULL)
11058724ff17SJeeja KP 		return skl_get_src_dsp_widget(src_w, skl);
1106d93f8e55SVinod Koul 
11078724ff17SJeeja KP 	return NULL;
1108d93f8e55SVinod Koul }
1109d93f8e55SVinod Koul 
1110d93f8e55SVinod Koul /*
1111d93f8e55SVinod Koul  * in the Post-PMU event of mixer we need to do following:
1112d93f8e55SVinod Koul  *   - Check if this pipe is running
1113d93f8e55SVinod Koul  *   - if not, then
1114d93f8e55SVinod Koul  *	- bind this pipeline to its source pipeline
1115d93f8e55SVinod Koul  *	  if source pipe is already running, this means it is a dynamic
1116d93f8e55SVinod Koul  *	  connection and we need to bind only to that pipe
1117d93f8e55SVinod Koul  *	- start this pipeline
1118d93f8e55SVinod Koul  */
1119d93f8e55SVinod Koul static int skl_tplg_mixer_dapm_post_pmu_event(struct snd_soc_dapm_widget *w,
1120bcc2a2dcSCezary Rojewski 							struct skl_dev *skl)
1121d93f8e55SVinod Koul {
1122d93f8e55SVinod Koul 	int ret = 0;
1123d93f8e55SVinod Koul 	struct snd_soc_dapm_widget *source, *sink;
1124d93f8e55SVinod Koul 	struct skl_module_cfg *src_mconfig, *sink_mconfig;
1125d93f8e55SVinod Koul 	int src_pipe_started = 0;
1126d93f8e55SVinod Koul 
1127d93f8e55SVinod Koul 	sink = w;
1128d93f8e55SVinod Koul 	sink_mconfig = sink->priv;
1129d93f8e55SVinod Koul 
1130d93f8e55SVinod Koul 	/*
1131d93f8e55SVinod Koul 	 * If source pipe is already started, that means source is driving
1132d93f8e55SVinod Koul 	 * one more sink before this sink got connected, Since source is
1133d93f8e55SVinod Koul 	 * started, bind this sink to source and start this pipe.
1134d93f8e55SVinod Koul 	 */
11358724ff17SJeeja KP 	source = skl_get_src_dsp_widget(w, skl);
11368724ff17SJeeja KP 	if (source != NULL) {
1137d93f8e55SVinod Koul 		src_mconfig = source->priv;
1138d93f8e55SVinod Koul 		sink_mconfig = sink->priv;
1139d93f8e55SVinod Koul 		src_pipe_started = 1;
1140d93f8e55SVinod Koul 
1141d93f8e55SVinod Koul 		/*
11428724ff17SJeeja KP 		 * check pipe state, then no need to bind or start the
11438724ff17SJeeja KP 		 * pipe
1144d93f8e55SVinod Koul 		 */
1145d93f8e55SVinod Koul 		if (src_mconfig->pipe->state != SKL_PIPE_STARTED)
1146d93f8e55SVinod Koul 			src_pipe_started = 0;
1147d93f8e55SVinod Koul 	}
1148d93f8e55SVinod Koul 
1149d93f8e55SVinod Koul 	if (src_pipe_started) {
1150bcc2a2dcSCezary Rojewski 		ret = skl_bind_modules(skl, src_mconfig, sink_mconfig);
1151d93f8e55SVinod Koul 		if (ret)
1152d93f8e55SVinod Koul 			return ret;
1153d93f8e55SVinod Koul 
1154cc6a4044SJeeja KP 		/* set module params after bind */
1155bcc2a2dcSCezary Rojewski 		skl_tplg_set_module_bind_params(source, src_mconfig, skl);
1156bcc2a2dcSCezary Rojewski 		skl_tplg_set_module_bind_params(sink, sink_mconfig, skl);
1157cc6a4044SJeeja KP 
1158d1730c3dSJeeja KP 		if (sink_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
1159bcc2a2dcSCezary Rojewski 			ret = skl_run_pipe(skl, sink_mconfig->pipe);
1160d93f8e55SVinod Koul 	}
1161d93f8e55SVinod Koul 
1162d93f8e55SVinod Koul 	return ret;
1163d93f8e55SVinod Koul }
1164d93f8e55SVinod Koul 
1165d93f8e55SVinod Koul /*
1166d93f8e55SVinod Koul  * in the Pre-PMD event of mixer we need to do following:
1167d93f8e55SVinod Koul  *   - Stop the pipe
1168d93f8e55SVinod Koul  *   - find the source connections and remove that from dapm_path_list
1169d93f8e55SVinod Koul  *   - unbind with source pipelines if still connected
1170d93f8e55SVinod Koul  */
1171d93f8e55SVinod Koul static int skl_tplg_mixer_dapm_pre_pmd_event(struct snd_soc_dapm_widget *w,
1172bcc2a2dcSCezary Rojewski 							struct skl_dev *skl)
1173d93f8e55SVinod Koul {
1174d93f8e55SVinod Koul 	struct skl_module_cfg *src_mconfig, *sink_mconfig;
1175ce1b5551SJeeja KP 	int ret = 0, i;
1176d93f8e55SVinod Koul 
1177ce1b5551SJeeja KP 	sink_mconfig = w->priv;
1178d93f8e55SVinod Koul 
1179d93f8e55SVinod Koul 	/* Stop the pipe */
1180bcc2a2dcSCezary Rojewski 	ret = skl_stop_pipe(skl, sink_mconfig->pipe);
1181d93f8e55SVinod Koul 	if (ret)
1182d93f8e55SVinod Koul 		return ret;
1183d93f8e55SVinod Koul 
1184f6fa56e2SRamesh Babu 	for (i = 0; i < sink_mconfig->module->max_input_pins; i++) {
1185ce1b5551SJeeja KP 		if (sink_mconfig->m_in_pin[i].pin_state == SKL_PIN_BIND_DONE) {
1186ce1b5551SJeeja KP 			src_mconfig = sink_mconfig->m_in_pin[i].tgt_mcfg;
1187ce1b5551SJeeja KP 			if (!src_mconfig)
1188ce1b5551SJeeja KP 				continue;
1189d93f8e55SVinod Koul 
1190bcc2a2dcSCezary Rojewski 			ret = skl_unbind_modules(skl,
1191ce1b5551SJeeja KP 						src_mconfig, sink_mconfig);
1192ce1b5551SJeeja KP 		}
1193d93f8e55SVinod Koul 	}
1194d93f8e55SVinod Koul 
1195d93f8e55SVinod Koul 	return ret;
1196d93f8e55SVinod Koul }
1197d93f8e55SVinod Koul 
1198d93f8e55SVinod Koul /*
1199d93f8e55SVinod Koul  * in the Post-PMD event of mixer we need to do following:
1200d93f8e55SVinod Koul  *   - Unbind the modules within the pipeline
1201d93f8e55SVinod Koul  *   - Delete the pipeline (modules are not required to be explicitly
1202d93f8e55SVinod Koul  *     deleted, pipeline delete is enough here
1203d93f8e55SVinod Koul  */
1204d93f8e55SVinod Koul static int skl_tplg_mixer_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
1205bcc2a2dcSCezary Rojewski 							struct skl_dev *skl)
1206d93f8e55SVinod Koul {
1207d93f8e55SVinod Koul 	struct skl_module_cfg *mconfig = w->priv;
1208d93f8e55SVinod Koul 	struct skl_pipe_module *w_module;
1209d93f8e55SVinod Koul 	struct skl_module_cfg *src_module = NULL, *dst_module;
1210d93f8e55SVinod Koul 	struct skl_pipe *s_pipe = mconfig->pipe;
1211550b349aSDan Carpenter 	struct skl_module_deferred_bind *modules, *tmp;
1212d93f8e55SVinod Koul 
1213260eb73aSDharageswari R 	if (s_pipe->state == SKL_PIPE_INVALID)
1214260eb73aSDharageswari R 		return -EINVAL;
1215260eb73aSDharageswari R 
1216d93f8e55SVinod Koul 	list_for_each_entry(w_module, &s_pipe->w_list, node) {
1217b8c722ddSJeeja KP 		if (list_empty(&skl->bind_list))
1218b8c722ddSJeeja KP 			break;
1219b8c722ddSJeeja KP 
1220b8c722ddSJeeja KP 		src_module = w_module->w->priv;
1221b8c722ddSJeeja KP 
1222550b349aSDan Carpenter 		list_for_each_entry_safe(modules, tmp, &skl->bind_list, node) {
1223b8c722ddSJeeja KP 			/*
1224b8c722ddSJeeja KP 			 * When the destination module is deleted, Unbind the
1225b8c722ddSJeeja KP 			 * modules from deferred bind list.
1226b8c722ddSJeeja KP 			 */
1227b8c722ddSJeeja KP 			if (modules->dst == src_module) {
1228bcc2a2dcSCezary Rojewski 				skl_unbind_modules(skl, modules->src,
1229b8c722ddSJeeja KP 						modules->dst);
1230b8c722ddSJeeja KP 			}
1231b8c722ddSJeeja KP 
1232b8c722ddSJeeja KP 			/*
1233b8c722ddSJeeja KP 			 * When the source module is deleted, remove this entry
1234b8c722ddSJeeja KP 			 * from the deferred bind list.
1235b8c722ddSJeeja KP 			 */
1236b8c722ddSJeeja KP 			if (modules->src == src_module) {
1237b8c722ddSJeeja KP 				list_del(&modules->node);
1238b8c722ddSJeeja KP 				modules->src = NULL;
1239b8c722ddSJeeja KP 				modules->dst = NULL;
1240b8c722ddSJeeja KP 				kfree(modules);
1241b8c722ddSJeeja KP 			}
1242b8c722ddSJeeja KP 		}
1243b8c722ddSJeeja KP 	}
1244b8c722ddSJeeja KP 
1245b8c722ddSJeeja KP 	list_for_each_entry(w_module, &s_pipe->w_list, node) {
1246d93f8e55SVinod Koul 		dst_module = w_module->w->priv;
1247d93f8e55SVinod Koul 
1248d93f8e55SVinod Koul 		if (src_module == NULL) {
1249d93f8e55SVinod Koul 			src_module = dst_module;
1250d93f8e55SVinod Koul 			continue;
1251d93f8e55SVinod Koul 		}
1252d93f8e55SVinod Koul 
1253bcc2a2dcSCezary Rojewski 		skl_unbind_modules(skl, src_module, dst_module);
1254d93f8e55SVinod Koul 		src_module = dst_module;
1255d93f8e55SVinod Koul 	}
1256d93f8e55SVinod Koul 
1257bcc2a2dcSCezary Rojewski 	skl_delete_pipe(skl, mconfig->pipe);
1258d93f8e55SVinod Koul 
1259473a4d51SJeeja KP 	list_for_each_entry(w_module, &s_pipe->w_list, node) {
1260473a4d51SJeeja KP 		src_module = w_module->w->priv;
1261473a4d51SJeeja KP 		src_module->m_state = SKL_MODULE_UNINIT;
1262473a4d51SJeeja KP 	}
1263473a4d51SJeeja KP 
1264bcc2a2dcSCezary Rojewski 	return skl_tplg_unload_pipe_modules(skl, s_pipe);
1265d93f8e55SVinod Koul }
1266d93f8e55SVinod Koul 
1267d93f8e55SVinod Koul /*
1268d93f8e55SVinod Koul  * in the Post-PMD event of PGA we need to do following:
1269d93f8e55SVinod Koul  *   - Stop the pipeline
1270d93f8e55SVinod Koul  *   - In source pipe is connected, unbind with source pipelines
1271d93f8e55SVinod Koul  */
1272d93f8e55SVinod Koul static int skl_tplg_pga_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
1273bcc2a2dcSCezary Rojewski 							struct skl_dev *skl)
1274d93f8e55SVinod Koul {
1275d93f8e55SVinod Koul 	struct skl_module_cfg *src_mconfig, *sink_mconfig;
1276ce1b5551SJeeja KP 	int ret = 0, i;
1277d93f8e55SVinod Koul 
1278ce1b5551SJeeja KP 	src_mconfig = w->priv;
1279d93f8e55SVinod Koul 
1280d93f8e55SVinod Koul 	/* Stop the pipe since this is a mixin module */
1281bcc2a2dcSCezary Rojewski 	ret = skl_stop_pipe(skl, src_mconfig->pipe);
1282d93f8e55SVinod Koul 	if (ret)
1283d93f8e55SVinod Koul 		return ret;
1284d93f8e55SVinod Koul 
1285f6fa56e2SRamesh Babu 	for (i = 0; i < src_mconfig->module->max_output_pins; i++) {
1286ce1b5551SJeeja KP 		if (src_mconfig->m_out_pin[i].pin_state == SKL_PIN_BIND_DONE) {
1287ce1b5551SJeeja KP 			sink_mconfig = src_mconfig->m_out_pin[i].tgt_mcfg;
1288ce1b5551SJeeja KP 			if (!sink_mconfig)
1289ce1b5551SJeeja KP 				continue;
1290d93f8e55SVinod Koul 			/*
1291ce1b5551SJeeja KP 			 * This is a connecter and if path is found that means
1292d93f8e55SVinod Koul 			 * unbind between source and sink has not happened yet
1293d93f8e55SVinod Koul 			 */
1294bcc2a2dcSCezary Rojewski 			ret = skl_unbind_modules(skl, src_mconfig,
1295ce1b5551SJeeja KP 							sink_mconfig);
1296ce1b5551SJeeja KP 		}
1297d93f8e55SVinod Koul 	}
1298d93f8e55SVinod Koul 
1299d93f8e55SVinod Koul 	return ret;
1300d93f8e55SVinod Koul }
1301d93f8e55SVinod Koul 
1302d93f8e55SVinod Koul /*
1303d93f8e55SVinod Koul  * In modelling, we assume there will be ONLY one mixer in a pipeline. If a
1304d93f8e55SVinod Koul  * second one is required that is created as another pipe entity.
1305d93f8e55SVinod Koul  * The mixer is responsible for pipe management and represent a pipeline
1306d93f8e55SVinod Koul  * instance
1307d93f8e55SVinod Koul  */
1308d93f8e55SVinod Koul static int skl_tplg_mixer_event(struct snd_soc_dapm_widget *w,
1309d93f8e55SVinod Koul 				struct snd_kcontrol *k, int event)
1310d93f8e55SVinod Koul {
1311d93f8e55SVinod Koul 	struct snd_soc_dapm_context *dapm = w->dapm;
1312bcc2a2dcSCezary Rojewski 	struct skl_dev *skl = get_skl_ctx(dapm->dev);
1313d93f8e55SVinod Koul 
1314d93f8e55SVinod Koul 	switch (event) {
1315d93f8e55SVinod Koul 	case SND_SOC_DAPM_PRE_PMU:
1316d93f8e55SVinod Koul 		return skl_tplg_mixer_dapm_pre_pmu_event(w, skl);
1317d93f8e55SVinod Koul 
1318d93f8e55SVinod Koul 	case SND_SOC_DAPM_POST_PMU:
1319d93f8e55SVinod Koul 		return skl_tplg_mixer_dapm_post_pmu_event(w, skl);
1320d93f8e55SVinod Koul 
1321d93f8e55SVinod Koul 	case SND_SOC_DAPM_PRE_PMD:
1322d93f8e55SVinod Koul 		return skl_tplg_mixer_dapm_pre_pmd_event(w, skl);
1323d93f8e55SVinod Koul 
1324d93f8e55SVinod Koul 	case SND_SOC_DAPM_POST_PMD:
1325d93f8e55SVinod Koul 		return skl_tplg_mixer_dapm_post_pmd_event(w, skl);
1326d93f8e55SVinod Koul 	}
1327d93f8e55SVinod Koul 
1328d93f8e55SVinod Koul 	return 0;
1329d93f8e55SVinod Koul }
1330d93f8e55SVinod Koul 
1331d93f8e55SVinod Koul /*
1332d93f8e55SVinod Koul  * In modelling, we assumed rest of the modules in pipeline are PGA. But we
1333d93f8e55SVinod Koul  * are interested in last PGA (leaf PGA) in a pipeline to disconnect with
1334d93f8e55SVinod Koul  * the sink when it is running (two FE to one BE or one FE to two BE)
1335d93f8e55SVinod Koul  * scenarios
1336d93f8e55SVinod Koul  */
1337d93f8e55SVinod Koul static int skl_tplg_pga_event(struct snd_soc_dapm_widget *w,
1338d93f8e55SVinod Koul 			struct snd_kcontrol *k, int event)
1339d93f8e55SVinod Koul 
1340d93f8e55SVinod Koul {
1341d93f8e55SVinod Koul 	struct snd_soc_dapm_context *dapm = w->dapm;
1342bcc2a2dcSCezary Rojewski 	struct skl_dev *skl = get_skl_ctx(dapm->dev);
1343d93f8e55SVinod Koul 
1344d93f8e55SVinod Koul 	switch (event) {
1345d93f8e55SVinod Koul 	case SND_SOC_DAPM_PRE_PMU:
1346d93f8e55SVinod Koul 		return skl_tplg_pga_dapm_pre_pmu_event(w, skl);
1347d93f8e55SVinod Koul 
1348d93f8e55SVinod Koul 	case SND_SOC_DAPM_POST_PMD:
1349d93f8e55SVinod Koul 		return skl_tplg_pga_dapm_post_pmd_event(w, skl);
1350d93f8e55SVinod Koul 	}
1351d93f8e55SVinod Koul 
1352d93f8e55SVinod Koul 	return 0;
1353d93f8e55SVinod Koul }
1354cfb0a873SVinod Koul 
13551b450791SMateusz Gorski static int skl_tplg_multi_config_set_get(struct snd_kcontrol *kcontrol,
13561b450791SMateusz Gorski 					 struct snd_ctl_elem_value *ucontrol,
13571b450791SMateusz Gorski 					 bool is_set)
13581b450791SMateusz Gorski {
13591b450791SMateusz Gorski 	struct snd_soc_component *component =
13601b450791SMateusz Gorski 		snd_soc_kcontrol_component(kcontrol);
13611b450791SMateusz Gorski 	struct hdac_bus *bus = snd_soc_component_get_drvdata(component);
13621b450791SMateusz Gorski 	struct skl_dev *skl = bus_to_skl(bus);
13631b450791SMateusz Gorski 	struct skl_pipeline *ppl;
13641b450791SMateusz Gorski 	struct skl_pipe *pipe = NULL;
13651b450791SMateusz Gorski 	struct soc_enum *ec = (struct soc_enum *)kcontrol->private_value;
13661b450791SMateusz Gorski 	u32 *pipe_id;
13671b450791SMateusz Gorski 
13681b450791SMateusz Gorski 	if (!ec)
13691b450791SMateusz Gorski 		return -EINVAL;
13701b450791SMateusz Gorski 
13711b450791SMateusz Gorski 	if (is_set && ucontrol->value.enumerated.item[0] > ec->items)
13721b450791SMateusz Gorski 		return -EINVAL;
13731b450791SMateusz Gorski 
13741b450791SMateusz Gorski 	pipe_id = ec->dobj.private;
13751b450791SMateusz Gorski 
13761b450791SMateusz Gorski 	list_for_each_entry(ppl, &skl->ppl_list, node) {
13771b450791SMateusz Gorski 		if (ppl->pipe->ppl_id == *pipe_id) {
13781b450791SMateusz Gorski 			pipe = ppl->pipe;
13791b450791SMateusz Gorski 			break;
13801b450791SMateusz Gorski 		}
13811b450791SMateusz Gorski 	}
13821b450791SMateusz Gorski 	if (!pipe)
13831b450791SMateusz Gorski 		return -EIO;
13841b450791SMateusz Gorski 
13851b450791SMateusz Gorski 	if (is_set)
13861b450791SMateusz Gorski 		pipe->pipe_config_idx = ucontrol->value.enumerated.item[0];
13871b450791SMateusz Gorski 	else
13881b450791SMateusz Gorski 		ucontrol->value.enumerated.item[0]  =  pipe->pipe_config_idx;
13891b450791SMateusz Gorski 
13901b450791SMateusz Gorski 	return 0;
13911b450791SMateusz Gorski }
13921b450791SMateusz Gorski 
13931b450791SMateusz Gorski static int skl_tplg_multi_config_get(struct snd_kcontrol *kcontrol,
13941b450791SMateusz Gorski 				     struct snd_ctl_elem_value *ucontrol)
13951b450791SMateusz Gorski {
13961b450791SMateusz Gorski 	return skl_tplg_multi_config_set_get(kcontrol, ucontrol, false);
13971b450791SMateusz Gorski }
13981b450791SMateusz Gorski 
13991b450791SMateusz Gorski static int skl_tplg_multi_config_set(struct snd_kcontrol *kcontrol,
14001b450791SMateusz Gorski 				     struct snd_ctl_elem_value *ucontrol)
14011b450791SMateusz Gorski {
14021b450791SMateusz Gorski 	return skl_tplg_multi_config_set_get(kcontrol, ucontrol, true);
14031b450791SMateusz Gorski }
14041b450791SMateusz Gorski 
14052d744ecfSMateusz Gorski static int skl_tplg_multi_config_get_dmic(struct snd_kcontrol *kcontrol,
14062d744ecfSMateusz Gorski 					  struct snd_ctl_elem_value *ucontrol)
14072d744ecfSMateusz Gorski {
14082d744ecfSMateusz Gorski 	return skl_tplg_multi_config_set_get(kcontrol, ucontrol, false);
14092d744ecfSMateusz Gorski }
14102d744ecfSMateusz Gorski 
14112d744ecfSMateusz Gorski static int skl_tplg_multi_config_set_dmic(struct snd_kcontrol *kcontrol,
14122d744ecfSMateusz Gorski 					  struct snd_ctl_elem_value *ucontrol)
14132d744ecfSMateusz Gorski {
14142d744ecfSMateusz Gorski 	return skl_tplg_multi_config_set_get(kcontrol, ucontrol, true);
14152d744ecfSMateusz Gorski }
14162d744ecfSMateusz Gorski 
1417140adfbaSJeeja KP static int skl_tplg_tlv_control_get(struct snd_kcontrol *kcontrol,
1418140adfbaSJeeja KP 			unsigned int __user *data, unsigned int size)
1419140adfbaSJeeja KP {
1420140adfbaSJeeja KP 	struct soc_bytes_ext *sb =
1421140adfbaSJeeja KP 			(struct soc_bytes_ext *)kcontrol->private_value;
1422140adfbaSJeeja KP 	struct skl_algo_data *bc = (struct skl_algo_data *)sb->dobj.private;
14237d9f2911SOmair M Abdullah 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
14247d9f2911SOmair M Abdullah 	struct skl_module_cfg *mconfig = w->priv;
1425bcc2a2dcSCezary Rojewski 	struct skl_dev *skl = get_skl_ctx(w->dapm->dev);
14267d9f2911SOmair M Abdullah 
14277d9f2911SOmair M Abdullah 	if (w->power)
1428bcc2a2dcSCezary Rojewski 		skl_get_module_params(skl, (u32 *)bc->params,
14290d682104SDharageswari R 				      bc->size, bc->param_id, mconfig);
1430140adfbaSJeeja KP 
143141556f68SVinod Koul 	/* decrement size for TLV header */
143241556f68SVinod Koul 	size -= 2 * sizeof(u32);
143341556f68SVinod Koul 
143441556f68SVinod Koul 	/* check size as we don't want to send kernel data */
143541556f68SVinod Koul 	if (size > bc->max)
143641556f68SVinod Koul 		size = bc->max;
143741556f68SVinod Koul 
1438140adfbaSJeeja KP 	if (bc->params) {
1439140adfbaSJeeja KP 		if (copy_to_user(data, &bc->param_id, sizeof(u32)))
1440140adfbaSJeeja KP 			return -EFAULT;
1441e8bc3c99SDan Carpenter 		if (copy_to_user(data + 1, &size, sizeof(u32)))
1442140adfbaSJeeja KP 			return -EFAULT;
1443e8bc3c99SDan Carpenter 		if (copy_to_user(data + 2, bc->params, size))
1444140adfbaSJeeja KP 			return -EFAULT;
1445140adfbaSJeeja KP 	}
1446140adfbaSJeeja KP 
1447140adfbaSJeeja KP 	return 0;
1448140adfbaSJeeja KP }
1449140adfbaSJeeja KP 
1450140adfbaSJeeja KP #define SKL_PARAM_VENDOR_ID 0xff
1451140adfbaSJeeja KP 
1452140adfbaSJeeja KP static int skl_tplg_tlv_control_set(struct snd_kcontrol *kcontrol,
1453140adfbaSJeeja KP 			const unsigned int __user *data, unsigned int size)
1454140adfbaSJeeja KP {
1455140adfbaSJeeja KP 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1456140adfbaSJeeja KP 	struct skl_module_cfg *mconfig = w->priv;
1457140adfbaSJeeja KP 	struct soc_bytes_ext *sb =
1458140adfbaSJeeja KP 			(struct soc_bytes_ext *)kcontrol->private_value;
1459140adfbaSJeeja KP 	struct skl_algo_data *ac = (struct skl_algo_data *)sb->dobj.private;
1460bcc2a2dcSCezary Rojewski 	struct skl_dev *skl = get_skl_ctx(w->dapm->dev);
1461140adfbaSJeeja KP 
1462140adfbaSJeeja KP 	if (ac->params) {
14630d682104SDharageswari R 		if (size > ac->max)
14640d682104SDharageswari R 			return -EINVAL;
14650d682104SDharageswari R 		ac->size = size;
1466a8cd7066SKamil Lulko 
1467140adfbaSJeeja KP 		if (copy_from_user(ac->params, data, size))
1468140adfbaSJeeja KP 			return -EFAULT;
1469140adfbaSJeeja KP 
1470140adfbaSJeeja KP 		if (w->power)
1471bcc2a2dcSCezary Rojewski 			return skl_set_module_params(skl,
14720d682104SDharageswari R 						(u32 *)ac->params, ac->size,
1473140adfbaSJeeja KP 						ac->param_id, mconfig);
1474140adfbaSJeeja KP 	}
1475140adfbaSJeeja KP 
1476140adfbaSJeeja KP 	return 0;
1477140adfbaSJeeja KP }
1478140adfbaSJeeja KP 
14797a1b749bSDharageswari R static int skl_tplg_mic_control_get(struct snd_kcontrol *kcontrol,
14807a1b749bSDharageswari R 		struct snd_ctl_elem_value *ucontrol)
14817a1b749bSDharageswari R {
14827a1b749bSDharageswari R 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
14837a1b749bSDharageswari R 	struct skl_module_cfg *mconfig = w->priv;
14847a1b749bSDharageswari R 	struct soc_enum *ec = (struct soc_enum *)kcontrol->private_value;
14857a1b749bSDharageswari R 	u32 ch_type = *((u32 *)ec->dobj.private);
14867a1b749bSDharageswari R 
14877a1b749bSDharageswari R 	if (mconfig->dmic_ch_type == ch_type)
14887a1b749bSDharageswari R 		ucontrol->value.enumerated.item[0] =
14897a1b749bSDharageswari R 					mconfig->dmic_ch_combo_index;
14907a1b749bSDharageswari R 	else
14917a1b749bSDharageswari R 		ucontrol->value.enumerated.item[0] = 0;
14927a1b749bSDharageswari R 
14937a1b749bSDharageswari R 	return 0;
14947a1b749bSDharageswari R }
14957a1b749bSDharageswari R 
14967a1b749bSDharageswari R static int skl_fill_mic_sel_params(struct skl_module_cfg *mconfig,
14977a1b749bSDharageswari R 	struct skl_mic_sel_config *mic_cfg, struct device *dev)
14987a1b749bSDharageswari R {
14997a1b749bSDharageswari R 	struct skl_specific_cfg *sp_cfg = &mconfig->formats_config;
15007a1b749bSDharageswari R 
15017a1b749bSDharageswari R 	sp_cfg->caps_size = sizeof(struct skl_mic_sel_config);
15027a1b749bSDharageswari R 	sp_cfg->set_params = SKL_PARAM_SET;
15037a1b749bSDharageswari R 	sp_cfg->param_id = 0x00;
15047a1b749bSDharageswari R 	if (!sp_cfg->caps) {
15057a1b749bSDharageswari R 		sp_cfg->caps = devm_kzalloc(dev, sp_cfg->caps_size, GFP_KERNEL);
15067a1b749bSDharageswari R 		if (!sp_cfg->caps)
15077a1b749bSDharageswari R 			return -ENOMEM;
15087a1b749bSDharageswari R 	}
15097a1b749bSDharageswari R 
15107a1b749bSDharageswari R 	mic_cfg->mic_switch = SKL_MIC_SEL_SWITCH;
15117a1b749bSDharageswari R 	mic_cfg->flags = 0;
15127a1b749bSDharageswari R 	memcpy(sp_cfg->caps, mic_cfg, sp_cfg->caps_size);
15137a1b749bSDharageswari R 
15147a1b749bSDharageswari R 	return 0;
15157a1b749bSDharageswari R }
15167a1b749bSDharageswari R 
15177a1b749bSDharageswari R static int skl_tplg_mic_control_set(struct snd_kcontrol *kcontrol,
15187a1b749bSDharageswari R 			struct snd_ctl_elem_value *ucontrol)
15197a1b749bSDharageswari R {
15207a1b749bSDharageswari R 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
15217a1b749bSDharageswari R 	struct skl_module_cfg *mconfig = w->priv;
15227a1b749bSDharageswari R 	struct skl_mic_sel_config mic_cfg = {0};
15237a1b749bSDharageswari R 	struct soc_enum *ec = (struct soc_enum *)kcontrol->private_value;
15247a1b749bSDharageswari R 	u32 ch_type = *((u32 *)ec->dobj.private);
15257a1b749bSDharageswari R 	const int *list;
15267a1b749bSDharageswari R 	u8 in_ch, out_ch, index;
15277a1b749bSDharageswari R 
15287a1b749bSDharageswari R 	mconfig->dmic_ch_type = ch_type;
15297a1b749bSDharageswari R 	mconfig->dmic_ch_combo_index = ucontrol->value.enumerated.item[0];
15307a1b749bSDharageswari R 
15317a1b749bSDharageswari R 	/* enum control index 0 is INVALID, so no channels to be set */
15327a1b749bSDharageswari R 	if (mconfig->dmic_ch_combo_index == 0)
15337a1b749bSDharageswari R 		return 0;
15347a1b749bSDharageswari R 
15357a1b749bSDharageswari R 	/* No valid channel selection map for index 0, so offset by 1 */
15367a1b749bSDharageswari R 	index = mconfig->dmic_ch_combo_index - 1;
15377a1b749bSDharageswari R 
15387a1b749bSDharageswari R 	switch (ch_type) {
15397a1b749bSDharageswari R 	case SKL_CH_MONO:
15407a1b749bSDharageswari R 		if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_mono_list))
15417a1b749bSDharageswari R 			return -EINVAL;
15427a1b749bSDharageswari R 
15437a1b749bSDharageswari R 		list = &mic_mono_list[index];
15447a1b749bSDharageswari R 		break;
15457a1b749bSDharageswari R 
15467a1b749bSDharageswari R 	case SKL_CH_STEREO:
15477a1b749bSDharageswari R 		if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_stereo_list))
15487a1b749bSDharageswari R 			return -EINVAL;
15497a1b749bSDharageswari R 
15507a1b749bSDharageswari R 		list = mic_stereo_list[index];
15517a1b749bSDharageswari R 		break;
15527a1b749bSDharageswari R 
15537a1b749bSDharageswari R 	case SKL_CH_TRIO:
15547a1b749bSDharageswari R 		if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_trio_list))
15557a1b749bSDharageswari R 			return -EINVAL;
15567a1b749bSDharageswari R 
15577a1b749bSDharageswari R 		list = mic_trio_list[index];
15587a1b749bSDharageswari R 		break;
15597a1b749bSDharageswari R 
15607a1b749bSDharageswari R 	case SKL_CH_QUATRO:
15617a1b749bSDharageswari R 		if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_quatro_list))
15627a1b749bSDharageswari R 			return -EINVAL;
15637a1b749bSDharageswari R 
15647a1b749bSDharageswari R 		list = mic_quatro_list[index];
15657a1b749bSDharageswari R 		break;
15667a1b749bSDharageswari R 
15677a1b749bSDharageswari R 	default:
15687a1b749bSDharageswari R 		dev_err(w->dapm->dev,
15697a1b749bSDharageswari R 				"Invalid channel %d for mic_select module\n",
15707a1b749bSDharageswari R 				ch_type);
15717a1b749bSDharageswari R 		return -EINVAL;
15727a1b749bSDharageswari R 
15737a1b749bSDharageswari R 	}
15747a1b749bSDharageswari R 
15757a1b749bSDharageswari R 	/* channel type enum map to number of chanels for that type */
15767a1b749bSDharageswari R 	for (out_ch = 0; out_ch < ch_type; out_ch++) {
15777a1b749bSDharageswari R 		in_ch = list[out_ch];
15787a1b749bSDharageswari R 		mic_cfg.blob[out_ch][in_ch] = SKL_DEFAULT_MIC_SEL_GAIN;
15797a1b749bSDharageswari R 	}
15807a1b749bSDharageswari R 
15817a1b749bSDharageswari R 	return skl_fill_mic_sel_params(mconfig, &mic_cfg, w->dapm->dev);
15827a1b749bSDharageswari R }
15837a1b749bSDharageswari R 
1584cfb0a873SVinod Koul /*
15858871dcb9SJeeja KP  * Fill the dma id for host and link. In case of passthrough
15868871dcb9SJeeja KP  * pipeline, this will both host and link in the same
15878871dcb9SJeeja KP  * pipeline, so need to copy the link and host based on dev_type
15888871dcb9SJeeja KP  */
15898871dcb9SJeeja KP static void skl_tplg_fill_dma_id(struct skl_module_cfg *mcfg,
15908871dcb9SJeeja KP 				struct skl_pipe_params *params)
15918871dcb9SJeeja KP {
15928871dcb9SJeeja KP 	struct skl_pipe *pipe = mcfg->pipe;
15938871dcb9SJeeja KP 
15948871dcb9SJeeja KP 	if (pipe->passthru) {
15958871dcb9SJeeja KP 		switch (mcfg->dev_type) {
15968871dcb9SJeeja KP 		case SKL_DEVICE_HDALINK:
15978871dcb9SJeeja KP 			pipe->p_params->link_dma_id = params->link_dma_id;
159812c3be0eSJeeja KP 			pipe->p_params->link_index = params->link_index;
15997f975a38SJeeja KP 			pipe->p_params->link_bps = params->link_bps;
16008871dcb9SJeeja KP 			break;
16018871dcb9SJeeja KP 
16028871dcb9SJeeja KP 		case SKL_DEVICE_HDAHOST:
16038871dcb9SJeeja KP 			pipe->p_params->host_dma_id = params->host_dma_id;
16047f975a38SJeeja KP 			pipe->p_params->host_bps = params->host_bps;
16058871dcb9SJeeja KP 			break;
16068871dcb9SJeeja KP 
16078871dcb9SJeeja KP 		default:
16088871dcb9SJeeja KP 			break;
16098871dcb9SJeeja KP 		}
16108871dcb9SJeeja KP 		pipe->p_params->s_fmt = params->s_fmt;
16118871dcb9SJeeja KP 		pipe->p_params->ch = params->ch;
16128871dcb9SJeeja KP 		pipe->p_params->s_freq = params->s_freq;
16138871dcb9SJeeja KP 		pipe->p_params->stream = params->stream;
161412c3be0eSJeeja KP 		pipe->p_params->format = params->format;
16158871dcb9SJeeja KP 
16168871dcb9SJeeja KP 	} else {
16178871dcb9SJeeja KP 		memcpy(pipe->p_params, params, sizeof(*params));
16188871dcb9SJeeja KP 	}
16198871dcb9SJeeja KP }
16208871dcb9SJeeja KP 
16218871dcb9SJeeja KP /*
1622cfb0a873SVinod Koul  * The FE params are passed by hw_params of the DAI.
1623cfb0a873SVinod Koul  * On hw_params, the params are stored in Gateway module of the FE and we
1624cfb0a873SVinod Koul  * need to calculate the format in DSP module configuration, that
1625cfb0a873SVinod Koul  * conversion is done here
1626cfb0a873SVinod Koul  */
1627cfb0a873SVinod Koul int skl_tplg_update_pipe_params(struct device *dev,
1628cfb0a873SVinod Koul 			struct skl_module_cfg *mconfig,
1629cfb0a873SVinod Koul 			struct skl_pipe_params *params)
1630cfb0a873SVinod Koul {
1631e8b374b6SCezary Rojewski 	struct skl_module_res *res;
1632bcc2a2dcSCezary Rojewski 	struct skl_dev *skl = get_skl_ctx(dev);
1633cfb0a873SVinod Koul 	struct skl_module_fmt *format = NULL;
1634f6fa56e2SRamesh Babu 	u8 cfg_idx = mconfig->pipe->cur_config_idx;
1635cfb0a873SVinod Koul 
1636e8b374b6SCezary Rojewski 	res = &mconfig->module->resources[mconfig->res_idx];
16378871dcb9SJeeja KP 	skl_tplg_fill_dma_id(mconfig, params);
1638f6fa56e2SRamesh Babu 	mconfig->fmt_idx = mconfig->mod_cfg[cfg_idx].fmt_idx;
1639f6fa56e2SRamesh Babu 	mconfig->res_idx = mconfig->mod_cfg[cfg_idx].res_idx;
1640f6fa56e2SRamesh Babu 
1641f6fa56e2SRamesh Babu 	if (skl->nr_modules)
1642f6fa56e2SRamesh Babu 		return 0;
1643cfb0a873SVinod Koul 
1644cfb0a873SVinod Koul 	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK)
1645e8b374b6SCezary Rojewski 		format = &mconfig->module->formats[mconfig->fmt_idx].inputs[0].fmt;
1646cfb0a873SVinod Koul 	else
1647e8b374b6SCezary Rojewski 		format = &mconfig->module->formats[mconfig->fmt_idx].outputs[0].fmt;
1648cfb0a873SVinod Koul 
1649cfb0a873SVinod Koul 	/* set the hw_params */
1650cfb0a873SVinod Koul 	format->s_freq = params->s_freq;
1651cfb0a873SVinod Koul 	format->channels = params->ch;
1652cfb0a873SVinod Koul 	format->valid_bit_depth = skl_get_bit_depth(params->s_fmt);
1653cfb0a873SVinod Koul 
1654cfb0a873SVinod Koul 	/*
1655cfb0a873SVinod Koul 	 * 16 bit is 16 bit container whereas 24 bit is in 32 bit
1656cfb0a873SVinod Koul 	 * container so update bit depth accordingly
1657cfb0a873SVinod Koul 	 */
1658cfb0a873SVinod Koul 	switch (format->valid_bit_depth) {
1659cfb0a873SVinod Koul 	case SKL_DEPTH_16BIT:
1660cfb0a873SVinod Koul 		format->bit_depth = format->valid_bit_depth;
1661cfb0a873SVinod Koul 		break;
1662cfb0a873SVinod Koul 
1663cfb0a873SVinod Koul 	case SKL_DEPTH_24BIT:
16646654f39eSJeeja KP 	case SKL_DEPTH_32BIT:
1665cfb0a873SVinod Koul 		format->bit_depth = SKL_DEPTH_32BIT;
1666cfb0a873SVinod Koul 		break;
1667cfb0a873SVinod Koul 
1668cfb0a873SVinod Koul 	default:
1669cfb0a873SVinod Koul 		dev_err(dev, "Invalid bit depth %x for pipe\n",
1670cfb0a873SVinod Koul 				format->valid_bit_depth);
1671cfb0a873SVinod Koul 		return -EINVAL;
1672cfb0a873SVinod Koul 	}
1673cfb0a873SVinod Koul 
1674cfb0a873SVinod Koul 	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1675f6fa56e2SRamesh Babu 		res->ibs = (format->s_freq / 1000) *
1676cfb0a873SVinod Koul 				(format->channels) *
1677cfb0a873SVinod Koul 				(format->bit_depth >> 3);
1678cfb0a873SVinod Koul 	} else {
1679f6fa56e2SRamesh Babu 		res->obs = (format->s_freq / 1000) *
1680cfb0a873SVinod Koul 				(format->channels) *
1681cfb0a873SVinod Koul 				(format->bit_depth >> 3);
1682cfb0a873SVinod Koul 	}
1683cfb0a873SVinod Koul 
1684cfb0a873SVinod Koul 	return 0;
1685cfb0a873SVinod Koul }
1686cfb0a873SVinod Koul 
1687cfb0a873SVinod Koul /*
1688cfb0a873SVinod Koul  * Query the module config for the FE DAI
1689cfb0a873SVinod Koul  * This is used to find the hw_params set for that DAI and apply to FE
1690cfb0a873SVinod Koul  * pipeline
1691cfb0a873SVinod Koul  */
1692cfb0a873SVinod Koul struct skl_module_cfg *
1693cfb0a873SVinod Koul skl_tplg_fe_get_cpr_module(struct snd_soc_dai *dai, int stream)
1694cfb0a873SVinod Koul {
1695cfb0a873SVinod Koul 	struct snd_soc_dapm_widget *w;
1696cfb0a873SVinod Koul 	struct snd_soc_dapm_path *p = NULL;
1697cfb0a873SVinod Koul 
1698cfb0a873SVinod Koul 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1699cfb0a873SVinod Koul 		w = dai->playback_widget;
1700f0900eb2SSubhransu S. Prusty 		snd_soc_dapm_widget_for_each_sink_path(w, p) {
1701cfb0a873SVinod Koul 			if (p->connect && p->sink->power &&
1702cb1f904dSGuneshwor Singh 				!is_skl_dsp_widget_type(p->sink, dai->dev))
1703cfb0a873SVinod Koul 				continue;
1704cfb0a873SVinod Koul 
1705cfb0a873SVinod Koul 			if (p->sink->priv) {
1706cfb0a873SVinod Koul 				dev_dbg(dai->dev, "set params for %s\n",
1707cfb0a873SVinod Koul 						p->sink->name);
1708cfb0a873SVinod Koul 				return p->sink->priv;
1709cfb0a873SVinod Koul 			}
1710cfb0a873SVinod Koul 		}
1711cfb0a873SVinod Koul 	} else {
1712cfb0a873SVinod Koul 		w = dai->capture_widget;
1713f0900eb2SSubhransu S. Prusty 		snd_soc_dapm_widget_for_each_source_path(w, p) {
1714cfb0a873SVinod Koul 			if (p->connect && p->source->power &&
1715cb1f904dSGuneshwor Singh 				!is_skl_dsp_widget_type(p->source, dai->dev))
1716cfb0a873SVinod Koul 				continue;
1717cfb0a873SVinod Koul 
1718cfb0a873SVinod Koul 			if (p->source->priv) {
1719cfb0a873SVinod Koul 				dev_dbg(dai->dev, "set params for %s\n",
1720cfb0a873SVinod Koul 						p->source->name);
1721cfb0a873SVinod Koul 				return p->source->priv;
1722cfb0a873SVinod Koul 			}
1723cfb0a873SVinod Koul 		}
1724cfb0a873SVinod Koul 	}
1725cfb0a873SVinod Koul 
1726cfb0a873SVinod Koul 	return NULL;
1727cfb0a873SVinod Koul }
1728cfb0a873SVinod Koul 
1729718a42b5SDharageswari.R static struct skl_module_cfg *skl_get_mconfig_pb_cpr(
1730718a42b5SDharageswari.R 		struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
1731718a42b5SDharageswari.R {
1732718a42b5SDharageswari.R 	struct snd_soc_dapm_path *p;
1733718a42b5SDharageswari.R 	struct skl_module_cfg *mconfig = NULL;
1734718a42b5SDharageswari.R 
1735718a42b5SDharageswari.R 	snd_soc_dapm_widget_for_each_source_path(w, p) {
1736718a42b5SDharageswari.R 		if (w->endpoints[SND_SOC_DAPM_DIR_OUT] > 0) {
1737718a42b5SDharageswari.R 			if (p->connect &&
1738718a42b5SDharageswari.R 				    (p->sink->id == snd_soc_dapm_aif_out) &&
1739718a42b5SDharageswari.R 				    p->source->priv) {
1740718a42b5SDharageswari.R 				mconfig = p->source->priv;
1741718a42b5SDharageswari.R 				return mconfig;
1742718a42b5SDharageswari.R 			}
1743718a42b5SDharageswari.R 			mconfig = skl_get_mconfig_pb_cpr(dai, p->source);
1744718a42b5SDharageswari.R 			if (mconfig)
1745718a42b5SDharageswari.R 				return mconfig;
1746718a42b5SDharageswari.R 		}
1747718a42b5SDharageswari.R 	}
1748718a42b5SDharageswari.R 	return mconfig;
1749718a42b5SDharageswari.R }
1750718a42b5SDharageswari.R 
1751718a42b5SDharageswari.R static struct skl_module_cfg *skl_get_mconfig_cap_cpr(
1752718a42b5SDharageswari.R 		struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
1753718a42b5SDharageswari.R {
1754718a42b5SDharageswari.R 	struct snd_soc_dapm_path *p;
1755718a42b5SDharageswari.R 	struct skl_module_cfg *mconfig = NULL;
1756718a42b5SDharageswari.R 
1757718a42b5SDharageswari.R 	snd_soc_dapm_widget_for_each_sink_path(w, p) {
1758718a42b5SDharageswari.R 		if (w->endpoints[SND_SOC_DAPM_DIR_IN] > 0) {
1759718a42b5SDharageswari.R 			if (p->connect &&
1760718a42b5SDharageswari.R 				    (p->source->id == snd_soc_dapm_aif_in) &&
1761718a42b5SDharageswari.R 				    p->sink->priv) {
1762718a42b5SDharageswari.R 				mconfig = p->sink->priv;
1763718a42b5SDharageswari.R 				return mconfig;
1764718a42b5SDharageswari.R 			}
1765718a42b5SDharageswari.R 			mconfig = skl_get_mconfig_cap_cpr(dai, p->sink);
1766718a42b5SDharageswari.R 			if (mconfig)
1767718a42b5SDharageswari.R 				return mconfig;
1768718a42b5SDharageswari.R 		}
1769718a42b5SDharageswari.R 	}
1770718a42b5SDharageswari.R 	return mconfig;
1771718a42b5SDharageswari.R }
1772718a42b5SDharageswari.R 
1773718a42b5SDharageswari.R struct skl_module_cfg *
1774718a42b5SDharageswari.R skl_tplg_be_get_cpr_module(struct snd_soc_dai *dai, int stream)
1775718a42b5SDharageswari.R {
1776718a42b5SDharageswari.R 	struct snd_soc_dapm_widget *w;
1777718a42b5SDharageswari.R 	struct skl_module_cfg *mconfig;
1778718a42b5SDharageswari.R 
1779718a42b5SDharageswari.R 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1780718a42b5SDharageswari.R 		w = dai->playback_widget;
1781718a42b5SDharageswari.R 		mconfig = skl_get_mconfig_pb_cpr(dai, w);
1782718a42b5SDharageswari.R 	} else {
1783718a42b5SDharageswari.R 		w = dai->capture_widget;
1784718a42b5SDharageswari.R 		mconfig = skl_get_mconfig_cap_cpr(dai, w);
1785718a42b5SDharageswari.R 	}
1786718a42b5SDharageswari.R 	return mconfig;
1787718a42b5SDharageswari.R }
1788718a42b5SDharageswari.R 
1789cfb0a873SVinod Koul static u8 skl_tplg_be_link_type(int dev_type)
1790cfb0a873SVinod Koul {
1791cfb0a873SVinod Koul 	int ret;
1792cfb0a873SVinod Koul 
1793cfb0a873SVinod Koul 	switch (dev_type) {
1794cfb0a873SVinod Koul 	case SKL_DEVICE_BT:
1795cfb0a873SVinod Koul 		ret = NHLT_LINK_SSP;
1796cfb0a873SVinod Koul 		break;
1797cfb0a873SVinod Koul 
1798cfb0a873SVinod Koul 	case SKL_DEVICE_DMIC:
1799cfb0a873SVinod Koul 		ret = NHLT_LINK_DMIC;
1800cfb0a873SVinod Koul 		break;
1801cfb0a873SVinod Koul 
1802cfb0a873SVinod Koul 	case SKL_DEVICE_I2S:
1803cfb0a873SVinod Koul 		ret = NHLT_LINK_SSP;
1804cfb0a873SVinod Koul 		break;
1805cfb0a873SVinod Koul 
1806cfb0a873SVinod Koul 	case SKL_DEVICE_HDALINK:
1807cfb0a873SVinod Koul 		ret = NHLT_LINK_HDA;
1808cfb0a873SVinod Koul 		break;
1809cfb0a873SVinod Koul 
1810cfb0a873SVinod Koul 	default:
1811cfb0a873SVinod Koul 		ret = NHLT_LINK_INVALID;
1812cfb0a873SVinod Koul 		break;
1813cfb0a873SVinod Koul 	}
1814cfb0a873SVinod Koul 
1815cfb0a873SVinod Koul 	return ret;
1816cfb0a873SVinod Koul }
1817cfb0a873SVinod Koul 
1818cfb0a873SVinod Koul /*
1819cfb0a873SVinod Koul  * Fill the BE gateway parameters
1820cfb0a873SVinod Koul  * The BE gateway expects a blob of parameters which are kept in the ACPI
1821cfb0a873SVinod Koul  * NHLT blob, so query the blob for interface type (i2s/pdm) and instance.
182287b26526SPiotr Maziarz  * The port can have multiple settings so pick based on the pipeline
1823cfb0a873SVinod Koul  * parameters
1824cfb0a873SVinod Koul  */
1825cfb0a873SVinod Koul static int skl_tplg_be_fill_pipe_params(struct snd_soc_dai *dai,
1826cfb0a873SVinod Koul 				struct skl_module_cfg *mconfig,
1827cfb0a873SVinod Koul 				struct skl_pipe_params *params)
1828cfb0a873SVinod Koul {
1829cfb0a873SVinod Koul 	struct nhlt_specific_cfg *cfg;
183087b26526SPiotr Maziarz 	struct skl_pipe *pipe = mconfig->pipe;
183187b26526SPiotr Maziarz 	struct skl_pipe_fmt *pipe_fmt;
1832bcc2a2dcSCezary Rojewski 	struct skl_dev *skl = get_skl_ctx(dai->dev);
1833cfb0a873SVinod Koul 	int link_type = skl_tplg_be_link_type(mconfig->dev_type);
1834db2f586bSSenthilnathan Veppur 	u8 dev_type = skl_tplg_be_dev_type(mconfig->dev_type);
1835cfb0a873SVinod Koul 
18368871dcb9SJeeja KP 	skl_tplg_fill_dma_id(mconfig, params);
1837cfb0a873SVinod Koul 
1838b30c275eSJeeja KP 	if (link_type == NHLT_LINK_HDA)
1839b30c275eSJeeja KP 		return 0;
1840b30c275eSJeeja KP 
184187b26526SPiotr Maziarz 	if (pipe->direction == SNDRV_PCM_STREAM_PLAYBACK)
184287b26526SPiotr Maziarz 		pipe_fmt = &pipe->configs[pipe->pipe_config_idx].out_fmt;
184387b26526SPiotr Maziarz 	else
184487b26526SPiotr Maziarz 		pipe_fmt = &pipe->configs[pipe->pipe_config_idx].in_fmt;
184587b26526SPiotr Maziarz 
1846cfb0a873SVinod Koul 	/* update the blob based on virtual bus_id*/
1847cfb0a873SVinod Koul 	cfg = skl_get_ep_blob(skl, mconfig->vbus_id, link_type,
184887b26526SPiotr Maziarz 					pipe_fmt->bps, pipe_fmt->channels,
184987b26526SPiotr Maziarz 					pipe_fmt->freq, pipe->direction,
1850db2f586bSSenthilnathan Veppur 					dev_type);
1851cfb0a873SVinod Koul 	if (cfg) {
1852cfb0a873SVinod Koul 		mconfig->formats_config.caps_size = cfg->size;
1853bc03281aSJeeja KP 		mconfig->formats_config.caps = (u32 *) &cfg->caps;
1854cfb0a873SVinod Koul 	} else {
185587b26526SPiotr Maziarz 		dev_err(dai->dev, "Blob NULL for id:%d type:%d dirn:%d ch:%d, freq:%d, fmt:%d\n",
185687b26526SPiotr Maziarz 			mconfig->vbus_id, link_type, params->stream,
1857cfb0a873SVinod Koul 			params->ch, params->s_freq, params->s_fmt);
1858cfb0a873SVinod Koul 		return -EINVAL;
1859cfb0a873SVinod Koul 	}
1860cfb0a873SVinod Koul 
1861cfb0a873SVinod Koul 	return 0;
1862cfb0a873SVinod Koul }
1863cfb0a873SVinod Koul 
1864cfb0a873SVinod Koul static int skl_tplg_be_set_src_pipe_params(struct snd_soc_dai *dai,
1865cfb0a873SVinod Koul 				struct snd_soc_dapm_widget *w,
1866cfb0a873SVinod Koul 				struct skl_pipe_params *params)
1867cfb0a873SVinod Koul {
1868cfb0a873SVinod Koul 	struct snd_soc_dapm_path *p;
18694d8adccbSSubhransu S. Prusty 	int ret = -EIO;
1870cfb0a873SVinod Koul 
1871f0900eb2SSubhransu S. Prusty 	snd_soc_dapm_widget_for_each_source_path(w, p) {
1872cb1f904dSGuneshwor Singh 		if (p->connect && is_skl_dsp_widget_type(p->source, dai->dev) &&
1873cfb0a873SVinod Koul 						p->source->priv) {
1874cfb0a873SVinod Koul 
18759a03cb49SJeeja KP 			ret = skl_tplg_be_fill_pipe_params(dai,
18769a03cb49SJeeja KP 						p->source->priv, params);
18774d8adccbSSubhransu S. Prusty 			if (ret < 0)
18784d8adccbSSubhransu S. Prusty 				return ret;
1879cfb0a873SVinod Koul 		} else {
18809a03cb49SJeeja KP 			ret = skl_tplg_be_set_src_pipe_params(dai,
18819a03cb49SJeeja KP 						p->source, params);
18824d8adccbSSubhransu S. Prusty 			if (ret < 0)
18834d8adccbSSubhransu S. Prusty 				return ret;
1884cfb0a873SVinod Koul 		}
1885cfb0a873SVinod Koul 	}
1886cfb0a873SVinod Koul 
18874d8adccbSSubhransu S. Prusty 	return ret;
1888cfb0a873SVinod Koul }
1889cfb0a873SVinod Koul 
1890cfb0a873SVinod Koul static int skl_tplg_be_set_sink_pipe_params(struct snd_soc_dai *dai,
1891cfb0a873SVinod Koul 	struct snd_soc_dapm_widget *w, struct skl_pipe_params *params)
1892cfb0a873SVinod Koul {
189325722cf6SPierre-Louis Bossart 	struct snd_soc_dapm_path *p;
18944d8adccbSSubhransu S. Prusty 	int ret = -EIO;
1895cfb0a873SVinod Koul 
1896f0900eb2SSubhransu S. Prusty 	snd_soc_dapm_widget_for_each_sink_path(w, p) {
1897cb1f904dSGuneshwor Singh 		if (p->connect && is_skl_dsp_widget_type(p->sink, dai->dev) &&
1898cfb0a873SVinod Koul 						p->sink->priv) {
1899cfb0a873SVinod Koul 
19009a03cb49SJeeja KP 			ret = skl_tplg_be_fill_pipe_params(dai,
19019a03cb49SJeeja KP 						p->sink->priv, params);
19024d8adccbSSubhransu S. Prusty 			if (ret < 0)
19034d8adccbSSubhransu S. Prusty 				return ret;
19044d8adccbSSubhransu S. Prusty 		} else {
19054d8adccbSSubhransu S. Prusty 			ret = skl_tplg_be_set_sink_pipe_params(
1906cfb0a873SVinod Koul 						dai, p->sink, params);
19074d8adccbSSubhransu S. Prusty 			if (ret < 0)
19084d8adccbSSubhransu S. Prusty 				return ret;
1909cfb0a873SVinod Koul 		}
1910cfb0a873SVinod Koul 	}
1911cfb0a873SVinod Koul 
19124d8adccbSSubhransu S. Prusty 	return ret;
1913cfb0a873SVinod Koul }
1914cfb0a873SVinod Koul 
1915cfb0a873SVinod Koul /*
1916cfb0a873SVinod Koul  * BE hw_params can be a source parameters (capture) or sink parameters
1917cfb0a873SVinod Koul  * (playback). Based on sink and source we need to either find the source
1918cfb0a873SVinod Koul  * list or the sink list and set the pipeline parameters
1919cfb0a873SVinod Koul  */
1920cfb0a873SVinod Koul int skl_tplg_be_update_params(struct snd_soc_dai *dai,
1921cfb0a873SVinod Koul 				struct skl_pipe_params *params)
1922cfb0a873SVinod Koul {
1923cfb0a873SVinod Koul 	struct snd_soc_dapm_widget *w;
1924cfb0a873SVinod Koul 
1925cfb0a873SVinod Koul 	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1926cfb0a873SVinod Koul 		w = dai->playback_widget;
1927cfb0a873SVinod Koul 
1928cfb0a873SVinod Koul 		return skl_tplg_be_set_src_pipe_params(dai, w, params);
1929cfb0a873SVinod Koul 
1930cfb0a873SVinod Koul 	} else {
1931cfb0a873SVinod Koul 		w = dai->capture_widget;
1932cfb0a873SVinod Koul 
1933cfb0a873SVinod Koul 		return skl_tplg_be_set_sink_pipe_params(dai, w, params);
1934cfb0a873SVinod Koul 	}
1935cfb0a873SVinod Koul 
1936cfb0a873SVinod Koul 	return 0;
1937cfb0a873SVinod Koul }
19383af36706SVinod Koul 
19393af36706SVinod Koul static const struct snd_soc_tplg_widget_events skl_tplg_widget_ops[] = {
19403af36706SVinod Koul 	{SKL_MIXER_EVENT, skl_tplg_mixer_event},
19419a1e3507SVinod Koul 	{SKL_VMIXER_EVENT, skl_tplg_mixer_event},
19423af36706SVinod Koul 	{SKL_PGA_EVENT, skl_tplg_pga_event},
19433af36706SVinod Koul };
19443af36706SVinod Koul 
1945140adfbaSJeeja KP static const struct snd_soc_tplg_bytes_ext_ops skl_tlv_ops[] = {
1946140adfbaSJeeja KP 	{SKL_CONTROL_TYPE_BYTE_TLV, skl_tplg_tlv_control_get,
1947140adfbaSJeeja KP 					skl_tplg_tlv_control_set},
1948140adfbaSJeeja KP };
1949140adfbaSJeeja KP 
19507a1b749bSDharageswari R static const struct snd_soc_tplg_kcontrol_ops skl_tplg_kcontrol_ops[] = {
19517a1b749bSDharageswari R 	{
19527a1b749bSDharageswari R 		.id = SKL_CONTROL_TYPE_MIC_SELECT,
19537a1b749bSDharageswari R 		.get = skl_tplg_mic_control_get,
19547a1b749bSDharageswari R 		.put = skl_tplg_mic_control_set,
19557a1b749bSDharageswari R 	},
19561b450791SMateusz Gorski 	{
19571b450791SMateusz Gorski 		.id = SKL_CONTROL_TYPE_MULTI_IO_SELECT,
19581b450791SMateusz Gorski 		.get = skl_tplg_multi_config_get,
19591b450791SMateusz Gorski 		.put = skl_tplg_multi_config_set,
19601b450791SMateusz Gorski 	},
19612d744ecfSMateusz Gorski 	{
19622d744ecfSMateusz Gorski 		.id = SKL_CONTROL_TYPE_MULTI_IO_SELECT_DMIC,
19632d744ecfSMateusz Gorski 		.get = skl_tplg_multi_config_get_dmic,
19642d744ecfSMateusz Gorski 		.put = skl_tplg_multi_config_set_dmic,
19652d744ecfSMateusz Gorski 	}
19667a1b749bSDharageswari R };
19677a1b749bSDharageswari R 
1968f6fa56e2SRamesh Babu static int skl_tplg_fill_pipe_cfg(struct device *dev,
1969f6fa56e2SRamesh Babu 			struct skl_pipe *pipe, u32 tkn,
1970f6fa56e2SRamesh Babu 			u32 tkn_val, int conf_idx, int dir)
1971f6fa56e2SRamesh Babu {
1972f6fa56e2SRamesh Babu 	struct skl_pipe_fmt *fmt;
1973f6fa56e2SRamesh Babu 	struct skl_path_config *config;
1974f6fa56e2SRamesh Babu 
1975f6fa56e2SRamesh Babu 	switch (dir) {
1976f6fa56e2SRamesh Babu 	case SKL_DIR_IN:
1977f6fa56e2SRamesh Babu 		fmt = &pipe->configs[conf_idx].in_fmt;
1978f6fa56e2SRamesh Babu 		break;
1979f6fa56e2SRamesh Babu 
1980f6fa56e2SRamesh Babu 	case SKL_DIR_OUT:
1981f6fa56e2SRamesh Babu 		fmt = &pipe->configs[conf_idx].out_fmt;
1982f6fa56e2SRamesh Babu 		break;
1983f6fa56e2SRamesh Babu 
1984f6fa56e2SRamesh Babu 	default:
1985f6fa56e2SRamesh Babu 		dev_err(dev, "Invalid direction: %d\n", dir);
1986f6fa56e2SRamesh Babu 		return -EINVAL;
1987f6fa56e2SRamesh Babu 	}
1988f6fa56e2SRamesh Babu 
1989f6fa56e2SRamesh Babu 	config = &pipe->configs[conf_idx];
1990f6fa56e2SRamesh Babu 
1991f6fa56e2SRamesh Babu 	switch (tkn) {
1992f6fa56e2SRamesh Babu 	case SKL_TKN_U32_CFG_FREQ:
1993f6fa56e2SRamesh Babu 		fmt->freq = tkn_val;
1994f6fa56e2SRamesh Babu 		break;
1995f6fa56e2SRamesh Babu 
1996f6fa56e2SRamesh Babu 	case SKL_TKN_U8_CFG_CHAN:
1997f6fa56e2SRamesh Babu 		fmt->channels = tkn_val;
1998f6fa56e2SRamesh Babu 		break;
1999f6fa56e2SRamesh Babu 
2000f6fa56e2SRamesh Babu 	case SKL_TKN_U8_CFG_BPS:
2001f6fa56e2SRamesh Babu 		fmt->bps = tkn_val;
2002f6fa56e2SRamesh Babu 		break;
2003f6fa56e2SRamesh Babu 
2004f6fa56e2SRamesh Babu 	case SKL_TKN_U32_PATH_MEM_PGS:
2005f6fa56e2SRamesh Babu 		config->mem_pages = tkn_val;
2006f6fa56e2SRamesh Babu 		break;
2007f6fa56e2SRamesh Babu 
2008f6fa56e2SRamesh Babu 	default:
2009f6fa56e2SRamesh Babu 		dev_err(dev, "Invalid token config: %d\n", tkn);
2010f6fa56e2SRamesh Babu 		return -EINVAL;
2011f6fa56e2SRamesh Babu 	}
2012f6fa56e2SRamesh Babu 
2013f6fa56e2SRamesh Babu 	return 0;
2014f6fa56e2SRamesh Babu }
2015f6fa56e2SRamesh Babu 
20166277e832SShreyas NC static int skl_tplg_fill_pipe_tkn(struct device *dev,
20176277e832SShreyas NC 			struct skl_pipe *pipe, u32 tkn,
20186277e832SShreyas NC 			u32 tkn_val)
20193af36706SVinod Koul {
20203af36706SVinod Koul 
20216277e832SShreyas NC 	switch (tkn) {
20226277e832SShreyas NC 	case SKL_TKN_U32_PIPE_CONN_TYPE:
20236277e832SShreyas NC 		pipe->conn_type = tkn_val;
20246277e832SShreyas NC 		break;
20256277e832SShreyas NC 
20266277e832SShreyas NC 	case SKL_TKN_U32_PIPE_PRIORITY:
20276277e832SShreyas NC 		pipe->pipe_priority = tkn_val;
20286277e832SShreyas NC 		break;
20296277e832SShreyas NC 
20306277e832SShreyas NC 	case SKL_TKN_U32_PIPE_MEM_PGS:
20316277e832SShreyas NC 		pipe->memory_pages = tkn_val;
20326277e832SShreyas NC 		break;
20336277e832SShreyas NC 
20348a0cb236SVinod Koul 	case SKL_TKN_U32_PMODE:
20358a0cb236SVinod Koul 		pipe->lp_mode = tkn_val;
20368a0cb236SVinod Koul 		break;
20378a0cb236SVinod Koul 
2038f6fa56e2SRamesh Babu 	case SKL_TKN_U32_PIPE_DIRECTION:
2039f6fa56e2SRamesh Babu 		pipe->direction = tkn_val;
2040f6fa56e2SRamesh Babu 		break;
2041f6fa56e2SRamesh Babu 
2042f6fa56e2SRamesh Babu 	case SKL_TKN_U32_NUM_CONFIGS:
2043f6fa56e2SRamesh Babu 		pipe->nr_cfgs = tkn_val;
2044f6fa56e2SRamesh Babu 		break;
2045f6fa56e2SRamesh Babu 
20466277e832SShreyas NC 	default:
20476277e832SShreyas NC 		dev_err(dev, "Token not handled %d\n", tkn);
20486277e832SShreyas NC 		return -EINVAL;
20493af36706SVinod Koul 	}
20506277e832SShreyas NC 
20516277e832SShreyas NC 	return 0;
20523af36706SVinod Koul }
20533af36706SVinod Koul 
20543af36706SVinod Koul /*
20556277e832SShreyas NC  * Add pipeline by parsing the relevant tokens
20566277e832SShreyas NC  * Return an existing pipe if the pipe already exists.
20573af36706SVinod Koul  */
20586277e832SShreyas NC static int skl_tplg_add_pipe(struct device *dev,
2059bcc2a2dcSCezary Rojewski 		struct skl_module_cfg *mconfig, struct skl_dev *skl,
20606277e832SShreyas NC 		struct snd_soc_tplg_vendor_value_elem *tkn_elem)
20613af36706SVinod Koul {
20623af36706SVinod Koul 	struct skl_pipeline *ppl;
20633af36706SVinod Koul 	struct skl_pipe *pipe;
20643af36706SVinod Koul 	struct skl_pipe_params *params;
20653af36706SVinod Koul 
20663af36706SVinod Koul 	list_for_each_entry(ppl, &skl->ppl_list, node) {
20676277e832SShreyas NC 		if (ppl->pipe->ppl_id == tkn_elem->value) {
20686277e832SShreyas NC 			mconfig->pipe = ppl->pipe;
2069081dc8abSGuneshwor Singh 			return -EEXIST;
20706277e832SShreyas NC 		}
20713af36706SVinod Koul 	}
20723af36706SVinod Koul 
20733af36706SVinod Koul 	ppl = devm_kzalloc(dev, sizeof(*ppl), GFP_KERNEL);
20743af36706SVinod Koul 	if (!ppl)
20756277e832SShreyas NC 		return -ENOMEM;
20763af36706SVinod Koul 
20773af36706SVinod Koul 	pipe = devm_kzalloc(dev, sizeof(*pipe), GFP_KERNEL);
20783af36706SVinod Koul 	if (!pipe)
20796277e832SShreyas NC 		return -ENOMEM;
20803af36706SVinod Koul 
20813af36706SVinod Koul 	params = devm_kzalloc(dev, sizeof(*params), GFP_KERNEL);
20823af36706SVinod Koul 	if (!params)
20836277e832SShreyas NC 		return -ENOMEM;
20843af36706SVinod Koul 
20853af36706SVinod Koul 	pipe->p_params = params;
20866277e832SShreyas NC 	pipe->ppl_id = tkn_elem->value;
20873af36706SVinod Koul 	INIT_LIST_HEAD(&pipe->w_list);
20883af36706SVinod Koul 
20893af36706SVinod Koul 	ppl->pipe = pipe;
20903af36706SVinod Koul 	list_add(&ppl->node, &skl->ppl_list);
20913af36706SVinod Koul 
20926277e832SShreyas NC 	mconfig->pipe = pipe;
20936277e832SShreyas NC 	mconfig->pipe->state = SKL_PIPE_INVALID;
20946277e832SShreyas NC 
20956277e832SShreyas NC 	return 0;
20963af36706SVinod Koul }
20973af36706SVinod Koul 
20989e0784d0SAndy Shevchenko static int skl_tplg_get_uuid(struct device *dev, guid_t *guid,
209922ebd666SSriram Periyasamy 	      struct snd_soc_tplg_vendor_uuid_elem *uuid_tkn)
21006277e832SShreyas NC {
210122ebd666SSriram Periyasamy 	if (uuid_tkn->token == SKL_TKN_UUID) {
2102cade2f59SAndy Shevchenko 		import_guid(guid, uuid_tkn->uuid);
210322ebd666SSriram Periyasamy 		return 0;
210422ebd666SSriram Periyasamy 	}
210522ebd666SSriram Periyasamy 
210622ebd666SSriram Periyasamy 	dev_err(dev, "Not an UUID token %d\n", uuid_tkn->token);
210722ebd666SSriram Periyasamy 
210822ebd666SSriram Periyasamy 	return -EINVAL;
210922ebd666SSriram Periyasamy }
211022ebd666SSriram Periyasamy 
211122ebd666SSriram Periyasamy static int skl_tplg_fill_pin(struct device *dev,
211222ebd666SSriram Periyasamy 			struct snd_soc_tplg_vendor_value_elem *tkn_elem,
211322ebd666SSriram Periyasamy 			struct skl_module_pin *m_pin,
211422ebd666SSriram Periyasamy 			int pin_index)
211522ebd666SSriram Periyasamy {
2116d9561474SSriram Periyasamy 	int ret;
2117d9561474SSriram Periyasamy 
211822ebd666SSriram Periyasamy 	switch (tkn_elem->token) {
21196277e832SShreyas NC 	case SKL_TKN_U32_PIN_MOD_ID:
212022ebd666SSriram Periyasamy 		m_pin[pin_index].id.module_id = tkn_elem->value;
21216277e832SShreyas NC 		break;
21226277e832SShreyas NC 
21236277e832SShreyas NC 	case SKL_TKN_U32_PIN_INST_ID:
212422ebd666SSriram Periyasamy 		m_pin[pin_index].id.instance_id = tkn_elem->value;
21256277e832SShreyas NC 		break;
21266277e832SShreyas NC 
2127d9561474SSriram Periyasamy 	case SKL_TKN_UUID:
21289e0784d0SAndy Shevchenko 		ret = skl_tplg_get_uuid(dev, &m_pin[pin_index].id.mod_uuid,
2129d9561474SSriram Periyasamy 			(struct snd_soc_tplg_vendor_uuid_elem *)tkn_elem);
2130d9561474SSriram Periyasamy 		if (ret < 0)
2131d9561474SSriram Periyasamy 			return ret;
2132d9561474SSriram Periyasamy 
21336277e832SShreyas NC 		break;
21346277e832SShreyas NC 
21356277e832SShreyas NC 	default:
213622ebd666SSriram Periyasamy 		dev_err(dev, "%d Not a pin token\n", tkn_elem->token);
21376277e832SShreyas NC 		return -EINVAL;
21386277e832SShreyas NC 	}
21396277e832SShreyas NC 
21406277e832SShreyas NC 	return 0;
21416277e832SShreyas NC }
21426277e832SShreyas NC 
21436277e832SShreyas NC /*
21446277e832SShreyas NC  * Parse for pin config specific tokens to fill up the
21456277e832SShreyas NC  * module private data
21466277e832SShreyas NC  */
21476277e832SShreyas NC static int skl_tplg_fill_pins_info(struct device *dev,
21486277e832SShreyas NC 		struct skl_module_cfg *mconfig,
21496277e832SShreyas NC 		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
21506277e832SShreyas NC 		int dir, int pin_count)
21516277e832SShreyas NC {
21526277e832SShreyas NC 	int ret;
21536277e832SShreyas NC 	struct skl_module_pin *m_pin;
21546277e832SShreyas NC 
21556277e832SShreyas NC 	switch (dir) {
21566277e832SShreyas NC 	case SKL_DIR_IN:
21576277e832SShreyas NC 		m_pin = mconfig->m_in_pin;
21586277e832SShreyas NC 		break;
21596277e832SShreyas NC 
21606277e832SShreyas NC 	case SKL_DIR_OUT:
21616277e832SShreyas NC 		m_pin = mconfig->m_out_pin;
21626277e832SShreyas NC 		break;
21636277e832SShreyas NC 
21646277e832SShreyas NC 	default:
2165ecd286a9SColin Ian King 		dev_err(dev, "Invalid direction value\n");
21666277e832SShreyas NC 		return -EINVAL;
21676277e832SShreyas NC 	}
21686277e832SShreyas NC 
216922ebd666SSriram Periyasamy 	ret = skl_tplg_fill_pin(dev, tkn_elem, m_pin, pin_count);
21706277e832SShreyas NC 	if (ret < 0)
21716277e832SShreyas NC 		return ret;
21726277e832SShreyas NC 
21736277e832SShreyas NC 	m_pin[pin_count].in_use = false;
21746277e832SShreyas NC 	m_pin[pin_count].pin_state = SKL_PIN_UNBIND;
21756277e832SShreyas NC 
21766277e832SShreyas NC 	return 0;
21776277e832SShreyas NC }
21786277e832SShreyas NC 
21796277e832SShreyas NC /*
21806277e832SShreyas NC  * Fill up input/output module config format based
21816277e832SShreyas NC  * on the direction
21826277e832SShreyas NC  */
21836277e832SShreyas NC static int skl_tplg_fill_fmt(struct device *dev,
2184ca312fdaSShreyas NC 		struct skl_module_fmt *dst_fmt,
2185ca312fdaSShreyas NC 		u32 tkn, u32 value)
21866277e832SShreyas NC {
21876277e832SShreyas NC 	switch (tkn) {
21886277e832SShreyas NC 	case SKL_TKN_U32_FMT_CH:
21896277e832SShreyas NC 		dst_fmt->channels  = value;
21906277e832SShreyas NC 		break;
21916277e832SShreyas NC 
21926277e832SShreyas NC 	case SKL_TKN_U32_FMT_FREQ:
21936277e832SShreyas NC 		dst_fmt->s_freq = value;
21946277e832SShreyas NC 		break;
21956277e832SShreyas NC 
21966277e832SShreyas NC 	case SKL_TKN_U32_FMT_BIT_DEPTH:
21976277e832SShreyas NC 		dst_fmt->bit_depth = value;
21986277e832SShreyas NC 		break;
21996277e832SShreyas NC 
22006277e832SShreyas NC 	case SKL_TKN_U32_FMT_SAMPLE_SIZE:
22016277e832SShreyas NC 		dst_fmt->valid_bit_depth = value;
22026277e832SShreyas NC 		break;
22036277e832SShreyas NC 
22046277e832SShreyas NC 	case SKL_TKN_U32_FMT_CH_CONFIG:
22056277e832SShreyas NC 		dst_fmt->ch_cfg = value;
22066277e832SShreyas NC 		break;
22076277e832SShreyas NC 
22086277e832SShreyas NC 	case SKL_TKN_U32_FMT_INTERLEAVE:
22096277e832SShreyas NC 		dst_fmt->interleaving_style = value;
22106277e832SShreyas NC 		break;
22116277e832SShreyas NC 
22126277e832SShreyas NC 	case SKL_TKN_U32_FMT_SAMPLE_TYPE:
22136277e832SShreyas NC 		dst_fmt->sample_type = value;
22146277e832SShreyas NC 		break;
22156277e832SShreyas NC 
22166277e832SShreyas NC 	case SKL_TKN_U32_FMT_CH_MAP:
22176277e832SShreyas NC 		dst_fmt->ch_map = value;
22186277e832SShreyas NC 		break;
22196277e832SShreyas NC 
22206277e832SShreyas NC 	default:
2221ecd286a9SColin Ian King 		dev_err(dev, "Invalid token %d\n", tkn);
22226277e832SShreyas NC 		return -EINVAL;
22236277e832SShreyas NC 	}
22246277e832SShreyas NC 
22256277e832SShreyas NC 	return 0;
22266277e832SShreyas NC }
22276277e832SShreyas NC 
2228ca312fdaSShreyas NC static int skl_tplg_widget_fill_fmt(struct device *dev,
2229f6fa56e2SRamesh Babu 		struct skl_module_iface *fmt,
2230ca312fdaSShreyas NC 		u32 tkn, u32 val, u32 dir, int fmt_idx)
2231ca312fdaSShreyas NC {
2232ca312fdaSShreyas NC 	struct skl_module_fmt *dst_fmt;
2233ca312fdaSShreyas NC 
2234f6fa56e2SRamesh Babu 	if (!fmt)
2235f6fa56e2SRamesh Babu 		return -EINVAL;
2236f6fa56e2SRamesh Babu 
2237ca312fdaSShreyas NC 	switch (dir) {
2238ca312fdaSShreyas NC 	case SKL_DIR_IN:
2239f6fa56e2SRamesh Babu 		dst_fmt = &fmt->inputs[fmt_idx].fmt;
2240ca312fdaSShreyas NC 		break;
2241ca312fdaSShreyas NC 
2242ca312fdaSShreyas NC 	case SKL_DIR_OUT:
2243f6fa56e2SRamesh Babu 		dst_fmt = &fmt->outputs[fmt_idx].fmt;
2244ca312fdaSShreyas NC 		break;
2245ca312fdaSShreyas NC 
2246ca312fdaSShreyas NC 	default:
2247ca312fdaSShreyas NC 		dev_err(dev, "Invalid direction: %d\n", dir);
2248ca312fdaSShreyas NC 		return -EINVAL;
2249ca312fdaSShreyas NC 	}
2250ca312fdaSShreyas NC 
2251ca312fdaSShreyas NC 	return skl_tplg_fill_fmt(dev, dst_fmt, tkn, val);
2252ca312fdaSShreyas NC }
2253ca312fdaSShreyas NC 
22546277e832SShreyas NC static void skl_tplg_fill_pin_dynamic_val(
22556277e832SShreyas NC 		struct skl_module_pin *mpin, u32 pin_count, u32 value)
22564cd9899fSHardik T Shah {
22574cd9899fSHardik T Shah 	int i;
22584cd9899fSHardik T Shah 
22596277e832SShreyas NC 	for (i = 0; i < pin_count; i++)
22606277e832SShreyas NC 		mpin[i].is_dynamic = value;
22614cd9899fSHardik T Shah }
22626277e832SShreyas NC 
22636277e832SShreyas NC /*
2264db6ed55dSShreyas NC  * Resource table in the manifest has pin specific resources
2265db6ed55dSShreyas NC  * like pin and pin buffer size
2266db6ed55dSShreyas NC  */
2267db6ed55dSShreyas NC static int skl_tplg_manifest_pin_res_tkn(struct device *dev,
2268db6ed55dSShreyas NC 		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2269db6ed55dSShreyas NC 		struct skl_module_res *res, int pin_idx, int dir)
2270db6ed55dSShreyas NC {
2271db6ed55dSShreyas NC 	struct skl_module_pin_resources *m_pin;
2272db6ed55dSShreyas NC 
2273db6ed55dSShreyas NC 	switch (dir) {
2274db6ed55dSShreyas NC 	case SKL_DIR_IN:
2275db6ed55dSShreyas NC 		m_pin = &res->input[pin_idx];
2276db6ed55dSShreyas NC 		break;
2277db6ed55dSShreyas NC 
2278db6ed55dSShreyas NC 	case SKL_DIR_OUT:
2279db6ed55dSShreyas NC 		m_pin = &res->output[pin_idx];
2280db6ed55dSShreyas NC 		break;
2281db6ed55dSShreyas NC 
2282db6ed55dSShreyas NC 	default:
2283db6ed55dSShreyas NC 		dev_err(dev, "Invalid pin direction: %d\n", dir);
2284db6ed55dSShreyas NC 		return -EINVAL;
2285db6ed55dSShreyas NC 	}
2286db6ed55dSShreyas NC 
2287db6ed55dSShreyas NC 	switch (tkn_elem->token) {
2288db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_RES_PIN_ID:
2289db6ed55dSShreyas NC 		m_pin->pin_index = tkn_elem->value;
2290db6ed55dSShreyas NC 		break;
2291db6ed55dSShreyas NC 
2292db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_PIN_BUF:
2293db6ed55dSShreyas NC 		m_pin->buf_size = tkn_elem->value;
2294db6ed55dSShreyas NC 		break;
2295db6ed55dSShreyas NC 
2296db6ed55dSShreyas NC 	default:
2297db6ed55dSShreyas NC 		dev_err(dev, "Invalid token: %d\n", tkn_elem->token);
2298db6ed55dSShreyas NC 		return -EINVAL;
2299db6ed55dSShreyas NC 	}
2300db6ed55dSShreyas NC 
2301db6ed55dSShreyas NC 	return 0;
2302db6ed55dSShreyas NC }
2303db6ed55dSShreyas NC 
2304db6ed55dSShreyas NC /*
2305db6ed55dSShreyas NC  * Fill module specific resources from the manifest's resource
2306db6ed55dSShreyas NC  * table like CPS, DMA size, mem_pages.
2307db6ed55dSShreyas NC  */
2308db6ed55dSShreyas NC static int skl_tplg_fill_res_tkn(struct device *dev,
2309db6ed55dSShreyas NC 		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2310db6ed55dSShreyas NC 		struct skl_module_res *res,
2311db6ed55dSShreyas NC 		int pin_idx, int dir)
2312db6ed55dSShreyas NC {
2313db6ed55dSShreyas NC 	int ret, tkn_count = 0;
2314db6ed55dSShreyas NC 
2315db6ed55dSShreyas NC 	if (!res)
2316db6ed55dSShreyas NC 		return -EINVAL;
2317db6ed55dSShreyas NC 
2318db6ed55dSShreyas NC 	switch (tkn_elem->token) {
2319db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_DMA_SIZE:
2320db6ed55dSShreyas NC 		res->dma_buffer_size = tkn_elem->value;
2321db6ed55dSShreyas NC 		break;
2322db6ed55dSShreyas NC 
2323db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_CPC:
2324db6ed55dSShreyas NC 		res->cpc = tkn_elem->value;
2325db6ed55dSShreyas NC 		break;
2326db6ed55dSShreyas NC 
2327db6ed55dSShreyas NC 	case SKL_TKN_U32_MEM_PAGES:
2328db6ed55dSShreyas NC 		res->is_pages = tkn_elem->value;
2329db6ed55dSShreyas NC 		break;
2330db6ed55dSShreyas NC 
2331db6ed55dSShreyas NC 	case SKL_TKN_U32_OBS:
2332db6ed55dSShreyas NC 		res->obs = tkn_elem->value;
2333db6ed55dSShreyas NC 		break;
2334db6ed55dSShreyas NC 
2335db6ed55dSShreyas NC 	case SKL_TKN_U32_IBS:
2336db6ed55dSShreyas NC 		res->ibs = tkn_elem->value;
2337db6ed55dSShreyas NC 		break;
2338db6ed55dSShreyas NC 
2339db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_RES_PIN_ID:
2340db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_PIN_BUF:
2341db6ed55dSShreyas NC 		ret = skl_tplg_manifest_pin_res_tkn(dev, tkn_elem, res,
2342db6ed55dSShreyas NC 						    pin_idx, dir);
2343db6ed55dSShreyas NC 		if (ret < 0)
2344db6ed55dSShreyas NC 			return ret;
2345db6ed55dSShreyas NC 		break;
2346db6ed55dSShreyas NC 
234784b71067SCezary Rojewski 	case SKL_TKN_MM_U32_CPS:
234884b71067SCezary Rojewski 	case SKL_TKN_U32_MAX_MCPS:
234984b71067SCezary Rojewski 		/* ignore unused tokens */
235084b71067SCezary Rojewski 		break;
235184b71067SCezary Rojewski 
2352db6ed55dSShreyas NC 	default:
2353db6ed55dSShreyas NC 		dev_err(dev, "Not a res type token: %d", tkn_elem->token);
2354db6ed55dSShreyas NC 		return -EINVAL;
2355db6ed55dSShreyas NC 
2356db6ed55dSShreyas NC 	}
2357db6ed55dSShreyas NC 	tkn_count++;
2358db6ed55dSShreyas NC 
2359db6ed55dSShreyas NC 	return tkn_count;
2360db6ed55dSShreyas NC }
2361db6ed55dSShreyas NC 
2362db6ed55dSShreyas NC /*
23636277e832SShreyas NC  * Parse tokens to fill up the module private data
23646277e832SShreyas NC  */
23656277e832SShreyas NC static int skl_tplg_get_token(struct device *dev,
23666277e832SShreyas NC 		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2367bcc2a2dcSCezary Rojewski 		struct skl_dev *skl, struct skl_module_cfg *mconfig)
23686277e832SShreyas NC {
23696277e832SShreyas NC 	int tkn_count = 0;
23706277e832SShreyas NC 	int ret;
23716277e832SShreyas NC 	static int is_pipe_exists;
2372f6fa56e2SRamesh Babu 	static int pin_index, dir, conf_idx;
2373f6fa56e2SRamesh Babu 	struct skl_module_iface *iface = NULL;
2374f6fa56e2SRamesh Babu 	struct skl_module_res *res = NULL;
2375f6fa56e2SRamesh Babu 	int res_idx = mconfig->res_idx;
2376f6fa56e2SRamesh Babu 	int fmt_idx = mconfig->fmt_idx;
2377f6fa56e2SRamesh Babu 
2378f6fa56e2SRamesh Babu 	/*
2379f6fa56e2SRamesh Babu 	 * If the manifest structure contains no modules, fill all
2380f6fa56e2SRamesh Babu 	 * the module data to 0th index.
2381f6fa56e2SRamesh Babu 	 * res_idx and fmt_idx are default set to 0.
2382f6fa56e2SRamesh Babu 	 */
2383f6fa56e2SRamesh Babu 	if (skl->nr_modules == 0) {
2384f6fa56e2SRamesh Babu 		res = &mconfig->module->resources[res_idx];
2385f6fa56e2SRamesh Babu 		iface = &mconfig->module->formats[fmt_idx];
2386f6fa56e2SRamesh Babu 	}
23876277e832SShreyas NC 
23886277e832SShreyas NC 	if (tkn_elem->token > SKL_TKN_MAX)
23896277e832SShreyas NC 		return -EINVAL;
23906277e832SShreyas NC 
23916277e832SShreyas NC 	switch (tkn_elem->token) {
23926277e832SShreyas NC 	case SKL_TKN_U8_IN_QUEUE_COUNT:
2393f6fa56e2SRamesh Babu 		mconfig->module->max_input_pins = tkn_elem->value;
23946277e832SShreyas NC 		break;
23956277e832SShreyas NC 
23966277e832SShreyas NC 	case SKL_TKN_U8_OUT_QUEUE_COUNT:
2397f6fa56e2SRamesh Babu 		mconfig->module->max_output_pins = tkn_elem->value;
23986277e832SShreyas NC 		break;
23996277e832SShreyas NC 
24006277e832SShreyas NC 	case SKL_TKN_U8_DYN_IN_PIN:
24016277e832SShreyas NC 		if (!mconfig->m_in_pin)
2402a86854d0SKees Cook 			mconfig->m_in_pin =
2403a86854d0SKees Cook 				devm_kcalloc(dev, MAX_IN_QUEUE,
2404a86854d0SKees Cook 					     sizeof(*mconfig->m_in_pin),
2405a86854d0SKees Cook 					     GFP_KERNEL);
2406f6fa56e2SRamesh Babu 		if (!mconfig->m_in_pin)
24076277e832SShreyas NC 			return -ENOMEM;
24086277e832SShreyas NC 
2409f6fa56e2SRamesh Babu 		skl_tplg_fill_pin_dynamic_val(mconfig->m_in_pin, MAX_IN_QUEUE,
2410f6fa56e2SRamesh Babu 					      tkn_elem->value);
24116277e832SShreyas NC 		break;
24126277e832SShreyas NC 
24136277e832SShreyas NC 	case SKL_TKN_U8_DYN_OUT_PIN:
24146277e832SShreyas NC 		if (!mconfig->m_out_pin)
2415a86854d0SKees Cook 			mconfig->m_out_pin =
2416a86854d0SKees Cook 				devm_kcalloc(dev, MAX_IN_QUEUE,
2417a86854d0SKees Cook 					     sizeof(*mconfig->m_in_pin),
2418a86854d0SKees Cook 					     GFP_KERNEL);
2419f6fa56e2SRamesh Babu 		if (!mconfig->m_out_pin)
24206277e832SShreyas NC 			return -ENOMEM;
24216277e832SShreyas NC 
2422f6fa56e2SRamesh Babu 		skl_tplg_fill_pin_dynamic_val(mconfig->m_out_pin, MAX_OUT_QUEUE,
2423f6fa56e2SRamesh Babu 					      tkn_elem->value);
24246277e832SShreyas NC 		break;
24256277e832SShreyas NC 
24266277e832SShreyas NC 	case SKL_TKN_U8_TIME_SLOT:
24276277e832SShreyas NC 		mconfig->time_slot = tkn_elem->value;
24286277e832SShreyas NC 		break;
24296277e832SShreyas NC 
24306277e832SShreyas NC 	case SKL_TKN_U8_CORE_ID:
24316277e832SShreyas NC 		mconfig->core_id = tkn_elem->value;
24329c80c5a8STakashi Iwai 		break;
24336277e832SShreyas NC 
24346277e832SShreyas NC 	case SKL_TKN_U8_MOD_TYPE:
24356277e832SShreyas NC 		mconfig->m_type = tkn_elem->value;
24366277e832SShreyas NC 		break;
24376277e832SShreyas NC 
24386277e832SShreyas NC 	case SKL_TKN_U8_DEV_TYPE:
24396277e832SShreyas NC 		mconfig->dev_type = tkn_elem->value;
24406277e832SShreyas NC 		break;
24416277e832SShreyas NC 
24426277e832SShreyas NC 	case SKL_TKN_U8_HW_CONN_TYPE:
24436277e832SShreyas NC 		mconfig->hw_conn_type = tkn_elem->value;
24446277e832SShreyas NC 		break;
24456277e832SShreyas NC 
24466277e832SShreyas NC 	case SKL_TKN_U16_MOD_INST_ID:
24476277e832SShreyas NC 		mconfig->id.instance_id =
24486277e832SShreyas NC 		tkn_elem->value;
24496277e832SShreyas NC 		break;
24506277e832SShreyas NC 
24516277e832SShreyas NC 	case SKL_TKN_U32_MEM_PAGES:
24526277e832SShreyas NC 	case SKL_TKN_U32_MAX_MCPS:
24536277e832SShreyas NC 	case SKL_TKN_U32_OBS:
24546277e832SShreyas NC 	case SKL_TKN_U32_IBS:
24552b79b15cSColin Ian King 		ret = skl_tplg_fill_res_tkn(dev, tkn_elem, res, pin_index, dir);
2456f6fa56e2SRamesh Babu 		if (ret < 0)
2457f6fa56e2SRamesh Babu 			return ret;
2458f6fa56e2SRamesh Babu 
24596277e832SShreyas NC 		break;
24606277e832SShreyas NC 
24616277e832SShreyas NC 	case SKL_TKN_U32_VBUS_ID:
24626277e832SShreyas NC 		mconfig->vbus_id = tkn_elem->value;
24636277e832SShreyas NC 		break;
24646277e832SShreyas NC 
24656277e832SShreyas NC 	case SKL_TKN_U32_PARAMS_FIXUP:
24666277e832SShreyas NC 		mconfig->params_fixup = tkn_elem->value;
24676277e832SShreyas NC 		break;
24686277e832SShreyas NC 
24696277e832SShreyas NC 	case SKL_TKN_U32_CONVERTER:
24706277e832SShreyas NC 		mconfig->converter = tkn_elem->value;
24716277e832SShreyas NC 		break;
24726277e832SShreyas NC 
2473c0116be3SSubhransu S. Prusty 	case SKL_TKN_U32_D0I3_CAPS:
24746bd9dcf3SVinod Koul 		mconfig->d0i3_caps = tkn_elem->value;
24756bd9dcf3SVinod Koul 		break;
24766bd9dcf3SVinod Koul 
24776277e832SShreyas NC 	case SKL_TKN_U32_PIPE_ID:
24786277e832SShreyas NC 		ret = skl_tplg_add_pipe(dev,
24796277e832SShreyas NC 				mconfig, skl, tkn_elem);
24806277e832SShreyas NC 
2481081dc8abSGuneshwor Singh 		if (ret < 0) {
2482081dc8abSGuneshwor Singh 			if (ret == -EEXIST) {
24836277e832SShreyas NC 				is_pipe_exists = 1;
2484081dc8abSGuneshwor Singh 				break;
2485081dc8abSGuneshwor Singh 			}
2486081dc8abSGuneshwor Singh 			return is_pipe_exists;
2487081dc8abSGuneshwor Singh 		}
24886277e832SShreyas NC 
24896277e832SShreyas NC 		break;
24906277e832SShreyas NC 
2491f6fa56e2SRamesh Babu 	case SKL_TKN_U32_PIPE_CONFIG_ID:
2492f6fa56e2SRamesh Babu 		conf_idx = tkn_elem->value;
2493f6fa56e2SRamesh Babu 		break;
2494f6fa56e2SRamesh Babu 
24956277e832SShreyas NC 	case SKL_TKN_U32_PIPE_CONN_TYPE:
24966277e832SShreyas NC 	case SKL_TKN_U32_PIPE_PRIORITY:
24976277e832SShreyas NC 	case SKL_TKN_U32_PIPE_MEM_PGS:
24988a0cb236SVinod Koul 	case SKL_TKN_U32_PMODE:
2499f6fa56e2SRamesh Babu 	case SKL_TKN_U32_PIPE_DIRECTION:
2500f6fa56e2SRamesh Babu 	case SKL_TKN_U32_NUM_CONFIGS:
25016277e832SShreyas NC 		if (is_pipe_exists) {
25026277e832SShreyas NC 			ret = skl_tplg_fill_pipe_tkn(dev, mconfig->pipe,
25036277e832SShreyas NC 					tkn_elem->token, tkn_elem->value);
25046277e832SShreyas NC 			if (ret < 0)
25056277e832SShreyas NC 				return ret;
25066277e832SShreyas NC 		}
25076277e832SShreyas NC 
25086277e832SShreyas NC 		break;
25096277e832SShreyas NC 
2510f6fa56e2SRamesh Babu 	case SKL_TKN_U32_PATH_MEM_PGS:
2511f6fa56e2SRamesh Babu 	case SKL_TKN_U32_CFG_FREQ:
2512f6fa56e2SRamesh Babu 	case SKL_TKN_U8_CFG_CHAN:
2513f6fa56e2SRamesh Babu 	case SKL_TKN_U8_CFG_BPS:
2514f6fa56e2SRamesh Babu 		if (mconfig->pipe->nr_cfgs) {
2515f6fa56e2SRamesh Babu 			ret = skl_tplg_fill_pipe_cfg(dev, mconfig->pipe,
2516f6fa56e2SRamesh Babu 					tkn_elem->token, tkn_elem->value,
2517f6fa56e2SRamesh Babu 					conf_idx, dir);
2518f6fa56e2SRamesh Babu 			if (ret < 0)
2519f6fa56e2SRamesh Babu 				return ret;
2520f6fa56e2SRamesh Babu 		}
2521f6fa56e2SRamesh Babu 		break;
2522f6fa56e2SRamesh Babu 
2523f6fa56e2SRamesh Babu 	case SKL_TKN_CFG_MOD_RES_ID:
2524f6fa56e2SRamesh Babu 		mconfig->mod_cfg[conf_idx].res_idx = tkn_elem->value;
2525f6fa56e2SRamesh Babu 		break;
2526f6fa56e2SRamesh Babu 
2527f6fa56e2SRamesh Babu 	case SKL_TKN_CFG_MOD_FMT_ID:
2528f6fa56e2SRamesh Babu 		mconfig->mod_cfg[conf_idx].fmt_idx = tkn_elem->value;
2529f6fa56e2SRamesh Babu 		break;
2530f6fa56e2SRamesh Babu 
25316277e832SShreyas NC 	/*
25326277e832SShreyas NC 	 * SKL_TKN_U32_DIR_PIN_COUNT token has the value for both
25336277e832SShreyas NC 	 * direction and the pin count. The first four bits represent
25346277e832SShreyas NC 	 * direction and next four the pin count.
25356277e832SShreyas NC 	 */
25366277e832SShreyas NC 	case SKL_TKN_U32_DIR_PIN_COUNT:
25376277e832SShreyas NC 		dir = tkn_elem->value & SKL_IN_DIR_BIT_MASK;
25386277e832SShreyas NC 		pin_index = (tkn_elem->value &
25396277e832SShreyas NC 			SKL_PIN_COUNT_MASK) >> 4;
25406277e832SShreyas NC 
25416277e832SShreyas NC 		break;
25426277e832SShreyas NC 
25436277e832SShreyas NC 	case SKL_TKN_U32_FMT_CH:
25446277e832SShreyas NC 	case SKL_TKN_U32_FMT_FREQ:
25456277e832SShreyas NC 	case SKL_TKN_U32_FMT_BIT_DEPTH:
25466277e832SShreyas NC 	case SKL_TKN_U32_FMT_SAMPLE_SIZE:
25476277e832SShreyas NC 	case SKL_TKN_U32_FMT_CH_CONFIG:
25486277e832SShreyas NC 	case SKL_TKN_U32_FMT_INTERLEAVE:
25496277e832SShreyas NC 	case SKL_TKN_U32_FMT_SAMPLE_TYPE:
25506277e832SShreyas NC 	case SKL_TKN_U32_FMT_CH_MAP:
2551f6fa56e2SRamesh Babu 		ret = skl_tplg_widget_fill_fmt(dev, iface, tkn_elem->token,
25526277e832SShreyas NC 				tkn_elem->value, dir, pin_index);
25536277e832SShreyas NC 
25546277e832SShreyas NC 		if (ret < 0)
25556277e832SShreyas NC 			return ret;
25566277e832SShreyas NC 
25576277e832SShreyas NC 		break;
25586277e832SShreyas NC 
25596277e832SShreyas NC 	case SKL_TKN_U32_PIN_MOD_ID:
25606277e832SShreyas NC 	case SKL_TKN_U32_PIN_INST_ID:
2561d9561474SSriram Periyasamy 	case SKL_TKN_UUID:
25626277e832SShreyas NC 		ret = skl_tplg_fill_pins_info(dev,
25636277e832SShreyas NC 				mconfig, tkn_elem, dir,
25646277e832SShreyas NC 				pin_index);
25656277e832SShreyas NC 		if (ret < 0)
25666277e832SShreyas NC 			return ret;
25676277e832SShreyas NC 
25686277e832SShreyas NC 		break;
25696277e832SShreyas NC 
25706277e832SShreyas NC 	case SKL_TKN_U32_CAPS_SIZE:
25716277e832SShreyas NC 		mconfig->formats_config.caps_size =
25726277e832SShreyas NC 			tkn_elem->value;
25736277e832SShreyas NC 
25746277e832SShreyas NC 		break;
25756277e832SShreyas NC 
2576133e6e5cSShreyas NC 	case SKL_TKN_U32_CAPS_SET_PARAMS:
2577133e6e5cSShreyas NC 		mconfig->formats_config.set_params =
2578133e6e5cSShreyas NC 				tkn_elem->value;
2579133e6e5cSShreyas NC 		break;
2580133e6e5cSShreyas NC 
2581133e6e5cSShreyas NC 	case SKL_TKN_U32_CAPS_PARAMS_ID:
2582133e6e5cSShreyas NC 		mconfig->formats_config.param_id =
2583133e6e5cSShreyas NC 				tkn_elem->value;
2584133e6e5cSShreyas NC 		break;
2585133e6e5cSShreyas NC 
25866277e832SShreyas NC 	case SKL_TKN_U32_PROC_DOMAIN:
25876277e832SShreyas NC 		mconfig->domain =
25886277e832SShreyas NC 			tkn_elem->value;
25896277e832SShreyas NC 
25906277e832SShreyas NC 		break;
25916277e832SShreyas NC 
2592939df3adSRamesh Babu 	case SKL_TKN_U32_DMA_BUF_SIZE:
2593939df3adSRamesh Babu 		mconfig->dma_buffer_size = tkn_elem->value;
2594939df3adSRamesh Babu 		break;
2595939df3adSRamesh Babu 
25966277e832SShreyas NC 	case SKL_TKN_U8_IN_PIN_TYPE:
25976277e832SShreyas NC 	case SKL_TKN_U8_OUT_PIN_TYPE:
25986277e832SShreyas NC 	case SKL_TKN_U8_CONN_TYPE:
25996277e832SShreyas NC 		break;
26006277e832SShreyas NC 
26016277e832SShreyas NC 	default:
26026277e832SShreyas NC 		dev_err(dev, "Token %d not handled\n",
26036277e832SShreyas NC 				tkn_elem->token);
26046277e832SShreyas NC 		return -EINVAL;
26056277e832SShreyas NC 	}
26066277e832SShreyas NC 
26076277e832SShreyas NC 	tkn_count++;
26086277e832SShreyas NC 
26096277e832SShreyas NC 	return tkn_count;
26106277e832SShreyas NC }
26116277e832SShreyas NC 
26126277e832SShreyas NC /*
26136277e832SShreyas NC  * Parse the vendor array for specific tokens to construct
26146277e832SShreyas NC  * module private data
26156277e832SShreyas NC  */
26166277e832SShreyas NC static int skl_tplg_get_tokens(struct device *dev,
2617bcc2a2dcSCezary Rojewski 		char *pvt_data,	struct skl_dev *skl,
26186277e832SShreyas NC 		struct skl_module_cfg *mconfig, int block_size)
26196277e832SShreyas NC {
26206277e832SShreyas NC 	struct snd_soc_tplg_vendor_array *array;
26216277e832SShreyas NC 	struct snd_soc_tplg_vendor_value_elem *tkn_elem;
26226277e832SShreyas NC 	int tkn_count = 0, ret;
26236277e832SShreyas NC 	int off = 0, tuple_size = 0;
2624d9561474SSriram Periyasamy 	bool is_module_guid = true;
26256277e832SShreyas NC 
26266277e832SShreyas NC 	if (block_size <= 0)
26276277e832SShreyas NC 		return -EINVAL;
26286277e832SShreyas NC 
26296277e832SShreyas NC 	while (tuple_size < block_size) {
26306277e832SShreyas NC 		array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off);
26316277e832SShreyas NC 
26326277e832SShreyas NC 		off += array->size;
26336277e832SShreyas NC 
26346277e832SShreyas NC 		switch (array->type) {
26356277e832SShreyas NC 		case SND_SOC_TPLG_TUPLE_TYPE_STRING:
2636ecd286a9SColin Ian King 			dev_warn(dev, "no string tokens expected for skl tplg\n");
26376277e832SShreyas NC 			continue;
26386277e832SShreyas NC 
26396277e832SShreyas NC 		case SND_SOC_TPLG_TUPLE_TYPE_UUID:
2640d9561474SSriram Periyasamy 			if (is_module_guid) {
26419e0784d0SAndy Shevchenko 				ret = skl_tplg_get_uuid(dev, (guid_t *)mconfig->guid,
264222ebd666SSriram Periyasamy 							array->uuid);
2643d9561474SSriram Periyasamy 				is_module_guid = false;
2644d9561474SSriram Periyasamy 			} else {
2645d9561474SSriram Periyasamy 				ret = skl_tplg_get_token(dev, array->value, skl,
2646d9561474SSriram Periyasamy 							 mconfig);
2647d9561474SSriram Periyasamy 			}
2648d9561474SSriram Periyasamy 
26496277e832SShreyas NC 			if (ret < 0)
26506277e832SShreyas NC 				return ret;
26516277e832SShreyas NC 
26526277e832SShreyas NC 			tuple_size += sizeof(*array->uuid);
26536277e832SShreyas NC 
26546277e832SShreyas NC 			continue;
26556277e832SShreyas NC 
26566277e832SShreyas NC 		default:
26576277e832SShreyas NC 			tkn_elem = array->value;
26586277e832SShreyas NC 			tkn_count = 0;
26596277e832SShreyas NC 			break;
26606277e832SShreyas NC 		}
26616277e832SShreyas NC 
26626277e832SShreyas NC 		while (tkn_count <= (array->num_elems - 1)) {
26636277e832SShreyas NC 			ret = skl_tplg_get_token(dev, tkn_elem,
26646277e832SShreyas NC 					skl, mconfig);
26656277e832SShreyas NC 
26666277e832SShreyas NC 			if (ret < 0)
26676277e832SShreyas NC 				return ret;
26686277e832SShreyas NC 
26696277e832SShreyas NC 			tkn_count = tkn_count + ret;
26706277e832SShreyas NC 			tkn_elem++;
26716277e832SShreyas NC 		}
26726277e832SShreyas NC 
26736277e832SShreyas NC 		tuple_size += tkn_count * sizeof(*tkn_elem);
26746277e832SShreyas NC 	}
26756277e832SShreyas NC 
2676133e6e5cSShreyas NC 	return off;
26776277e832SShreyas NC }
26786277e832SShreyas NC 
26796277e832SShreyas NC /*
26806277e832SShreyas NC  * Every data block is preceded by a descriptor to read the number
26816277e832SShreyas NC  * of data blocks, they type of the block and it's size
26826277e832SShreyas NC  */
26836277e832SShreyas NC static int skl_tplg_get_desc_blocks(struct device *dev,
26846277e832SShreyas NC 		struct snd_soc_tplg_vendor_array *array)
26856277e832SShreyas NC {
26866277e832SShreyas NC 	struct snd_soc_tplg_vendor_value_elem *tkn_elem;
26876277e832SShreyas NC 
26886277e832SShreyas NC 	tkn_elem = array->value;
26896277e832SShreyas NC 
26906277e832SShreyas NC 	switch (tkn_elem->token) {
26916277e832SShreyas NC 	case SKL_TKN_U8_NUM_BLOCKS:
26926277e832SShreyas NC 	case SKL_TKN_U8_BLOCK_TYPE:
26936277e832SShreyas NC 	case SKL_TKN_U16_BLOCK_SIZE:
26946277e832SShreyas NC 		return tkn_elem->value;
26956277e832SShreyas NC 
26966277e832SShreyas NC 	default:
2697ecd286a9SColin Ian King 		dev_err(dev, "Invalid descriptor token %d\n", tkn_elem->token);
26986277e832SShreyas NC 		break;
26996277e832SShreyas NC 	}
27006277e832SShreyas NC 
27016277e832SShreyas NC 	return -EINVAL;
27026277e832SShreyas NC }
27036277e832SShreyas NC 
2704ac9391daSGuenter Roeck /* Functions to parse private data from configuration file format v4 */
2705ac9391daSGuenter Roeck 
2706ac9391daSGuenter Roeck /*
2707ac9391daSGuenter Roeck  * Add pipeline from topology binary into driver pipeline list
2708ac9391daSGuenter Roeck  *
2709ac9391daSGuenter Roeck  * If already added we return that instance
2710ac9391daSGuenter Roeck  * Otherwise we create a new instance and add into driver list
2711ac9391daSGuenter Roeck  */
2712ac9391daSGuenter Roeck static int skl_tplg_add_pipe_v4(struct device *dev,
2713bcc2a2dcSCezary Rojewski 			struct skl_module_cfg *mconfig, struct skl_dev *skl,
2714ac9391daSGuenter Roeck 			struct skl_dfw_v4_pipe *dfw_pipe)
2715ac9391daSGuenter Roeck {
2716ac9391daSGuenter Roeck 	struct skl_pipeline *ppl;
2717ac9391daSGuenter Roeck 	struct skl_pipe *pipe;
2718ac9391daSGuenter Roeck 	struct skl_pipe_params *params;
2719ac9391daSGuenter Roeck 
2720ac9391daSGuenter Roeck 	list_for_each_entry(ppl, &skl->ppl_list, node) {
2721ac9391daSGuenter Roeck 		if (ppl->pipe->ppl_id == dfw_pipe->pipe_id) {
2722ac9391daSGuenter Roeck 			mconfig->pipe = ppl->pipe;
2723ac9391daSGuenter Roeck 			return 0;
2724ac9391daSGuenter Roeck 		}
2725ac9391daSGuenter Roeck 	}
2726ac9391daSGuenter Roeck 
2727ac9391daSGuenter Roeck 	ppl = devm_kzalloc(dev, sizeof(*ppl), GFP_KERNEL);
2728ac9391daSGuenter Roeck 	if (!ppl)
2729ac9391daSGuenter Roeck 		return -ENOMEM;
2730ac9391daSGuenter Roeck 
2731ac9391daSGuenter Roeck 	pipe = devm_kzalloc(dev, sizeof(*pipe), GFP_KERNEL);
2732ac9391daSGuenter Roeck 	if (!pipe)
2733ac9391daSGuenter Roeck 		return -ENOMEM;
2734ac9391daSGuenter Roeck 
2735ac9391daSGuenter Roeck 	params = devm_kzalloc(dev, sizeof(*params), GFP_KERNEL);
2736ac9391daSGuenter Roeck 	if (!params)
2737ac9391daSGuenter Roeck 		return -ENOMEM;
2738ac9391daSGuenter Roeck 
2739ac9391daSGuenter Roeck 	pipe->ppl_id = dfw_pipe->pipe_id;
2740ac9391daSGuenter Roeck 	pipe->memory_pages = dfw_pipe->memory_pages;
2741ac9391daSGuenter Roeck 	pipe->pipe_priority = dfw_pipe->pipe_priority;
2742ac9391daSGuenter Roeck 	pipe->conn_type = dfw_pipe->conn_type;
2743ac9391daSGuenter Roeck 	pipe->state = SKL_PIPE_INVALID;
2744ac9391daSGuenter Roeck 	pipe->p_params = params;
2745ac9391daSGuenter Roeck 	INIT_LIST_HEAD(&pipe->w_list);
2746ac9391daSGuenter Roeck 
2747ac9391daSGuenter Roeck 	ppl->pipe = pipe;
2748ac9391daSGuenter Roeck 	list_add(&ppl->node, &skl->ppl_list);
2749ac9391daSGuenter Roeck 
2750ac9391daSGuenter Roeck 	mconfig->pipe = pipe;
2751ac9391daSGuenter Roeck 
2752ac9391daSGuenter Roeck 	return 0;
2753ac9391daSGuenter Roeck }
2754ac9391daSGuenter Roeck 
2755ac9391daSGuenter Roeck static void skl_fill_module_pin_info_v4(struct skl_dfw_v4_module_pin *dfw_pin,
2756ac9391daSGuenter Roeck 					struct skl_module_pin *m_pin,
2757ac9391daSGuenter Roeck 					bool is_dynamic, int max_pin)
2758ac9391daSGuenter Roeck {
2759ac9391daSGuenter Roeck 	int i;
2760ac9391daSGuenter Roeck 
2761ac9391daSGuenter Roeck 	for (i = 0; i < max_pin; i++) {
2762ac9391daSGuenter Roeck 		m_pin[i].id.module_id = dfw_pin[i].module_id;
2763ac9391daSGuenter Roeck 		m_pin[i].id.instance_id = dfw_pin[i].instance_id;
2764ac9391daSGuenter Roeck 		m_pin[i].in_use = false;
2765ac9391daSGuenter Roeck 		m_pin[i].is_dynamic = is_dynamic;
2766ac9391daSGuenter Roeck 		m_pin[i].pin_state = SKL_PIN_UNBIND;
2767ac9391daSGuenter Roeck 	}
2768ac9391daSGuenter Roeck }
2769ac9391daSGuenter Roeck 
2770ac9391daSGuenter Roeck static void skl_tplg_fill_fmt_v4(struct skl_module_pin_fmt *dst_fmt,
2771ac9391daSGuenter Roeck 				 struct skl_dfw_v4_module_fmt *src_fmt,
2772ac9391daSGuenter Roeck 				 int pins)
2773ac9391daSGuenter Roeck {
2774ac9391daSGuenter Roeck 	int i;
2775ac9391daSGuenter Roeck 
2776ac9391daSGuenter Roeck 	for (i = 0; i < pins; i++) {
2777ac9391daSGuenter Roeck 		dst_fmt[i].fmt.channels  = src_fmt[i].channels;
2778ac9391daSGuenter Roeck 		dst_fmt[i].fmt.s_freq = src_fmt[i].freq;
2779ac9391daSGuenter Roeck 		dst_fmt[i].fmt.bit_depth = src_fmt[i].bit_depth;
2780ac9391daSGuenter Roeck 		dst_fmt[i].fmt.valid_bit_depth = src_fmt[i].valid_bit_depth;
2781ac9391daSGuenter Roeck 		dst_fmt[i].fmt.ch_cfg = src_fmt[i].ch_cfg;
2782ac9391daSGuenter Roeck 		dst_fmt[i].fmt.ch_map = src_fmt[i].ch_map;
2783ac9391daSGuenter Roeck 		dst_fmt[i].fmt.interleaving_style =
2784ac9391daSGuenter Roeck 						src_fmt[i].interleaving_style;
2785ac9391daSGuenter Roeck 		dst_fmt[i].fmt.sample_type = src_fmt[i].sample_type;
2786ac9391daSGuenter Roeck 	}
2787ac9391daSGuenter Roeck }
2788ac9391daSGuenter Roeck 
2789ac9391daSGuenter Roeck static int skl_tplg_get_pvt_data_v4(struct snd_soc_tplg_dapm_widget *tplg_w,
2790bcc2a2dcSCezary Rojewski 				    struct skl_dev *skl, struct device *dev,
2791ac9391daSGuenter Roeck 				    struct skl_module_cfg *mconfig)
2792ac9391daSGuenter Roeck {
2793ac9391daSGuenter Roeck 	struct skl_dfw_v4_module *dfw =
2794ac9391daSGuenter Roeck 				(struct skl_dfw_v4_module *)tplg_w->priv.data;
2795ac9391daSGuenter Roeck 	int ret;
2796ac9391daSGuenter Roeck 
2797ac9391daSGuenter Roeck 	dev_dbg(dev, "Parsing Skylake v4 widget topology data\n");
2798ac9391daSGuenter Roeck 
2799ac9391daSGuenter Roeck 	ret = guid_parse(dfw->uuid, (guid_t *)mconfig->guid);
2800ac9391daSGuenter Roeck 	if (ret)
2801ac9391daSGuenter Roeck 		return ret;
2802ac9391daSGuenter Roeck 	mconfig->id.module_id = -1;
2803ac9391daSGuenter Roeck 	mconfig->id.instance_id = dfw->instance_id;
280484b71067SCezary Rojewski 	mconfig->module->resources[0].cpc = dfw->max_mcps / 1000;
2805ac9391daSGuenter Roeck 	mconfig->module->resources[0].ibs = dfw->ibs;
2806ac9391daSGuenter Roeck 	mconfig->module->resources[0].obs = dfw->obs;
2807ac9391daSGuenter Roeck 	mconfig->core_id = dfw->core_id;
2808ac9391daSGuenter Roeck 	mconfig->module->max_input_pins = dfw->max_in_queue;
2809ac9391daSGuenter Roeck 	mconfig->module->max_output_pins = dfw->max_out_queue;
2810ac9391daSGuenter Roeck 	mconfig->module->loadable = dfw->is_loadable;
2811ac9391daSGuenter Roeck 	skl_tplg_fill_fmt_v4(mconfig->module->formats[0].inputs, dfw->in_fmt,
2812ac9391daSGuenter Roeck 			     MAX_IN_QUEUE);
2813ac9391daSGuenter Roeck 	skl_tplg_fill_fmt_v4(mconfig->module->formats[0].outputs, dfw->out_fmt,
2814ac9391daSGuenter Roeck 			     MAX_OUT_QUEUE);
2815ac9391daSGuenter Roeck 
2816ac9391daSGuenter Roeck 	mconfig->params_fixup = dfw->params_fixup;
2817ac9391daSGuenter Roeck 	mconfig->converter = dfw->converter;
2818ac9391daSGuenter Roeck 	mconfig->m_type = dfw->module_type;
2819ac9391daSGuenter Roeck 	mconfig->vbus_id = dfw->vbus_id;
2820ac9391daSGuenter Roeck 	mconfig->module->resources[0].is_pages = dfw->mem_pages;
2821ac9391daSGuenter Roeck 
2822ac9391daSGuenter Roeck 	ret = skl_tplg_add_pipe_v4(dev, mconfig, skl, &dfw->pipe);
2823ac9391daSGuenter Roeck 	if (ret)
2824ac9391daSGuenter Roeck 		return ret;
2825ac9391daSGuenter Roeck 
2826ac9391daSGuenter Roeck 	mconfig->dev_type = dfw->dev_type;
2827ac9391daSGuenter Roeck 	mconfig->hw_conn_type = dfw->hw_conn_type;
2828ac9391daSGuenter Roeck 	mconfig->time_slot = dfw->time_slot;
2829ac9391daSGuenter Roeck 	mconfig->formats_config.caps_size = dfw->caps.caps_size;
2830ac9391daSGuenter Roeck 
2831a86854d0SKees Cook 	mconfig->m_in_pin = devm_kcalloc(dev,
2832a86854d0SKees Cook 				MAX_IN_QUEUE, sizeof(*mconfig->m_in_pin),
2833ac9391daSGuenter Roeck 				GFP_KERNEL);
2834ac9391daSGuenter Roeck 	if (!mconfig->m_in_pin)
2835ac9391daSGuenter Roeck 		return -ENOMEM;
2836ac9391daSGuenter Roeck 
2837a86854d0SKees Cook 	mconfig->m_out_pin = devm_kcalloc(dev,
2838a86854d0SKees Cook 				MAX_OUT_QUEUE, sizeof(*mconfig->m_out_pin),
2839ac9391daSGuenter Roeck 				GFP_KERNEL);
2840ac9391daSGuenter Roeck 	if (!mconfig->m_out_pin)
2841ac9391daSGuenter Roeck 		return -ENOMEM;
2842ac9391daSGuenter Roeck 
2843ac9391daSGuenter Roeck 	skl_fill_module_pin_info_v4(dfw->in_pin, mconfig->m_in_pin,
2844ac9391daSGuenter Roeck 				    dfw->is_dynamic_in_pin,
2845ac9391daSGuenter Roeck 				    mconfig->module->max_input_pins);
2846ac9391daSGuenter Roeck 	skl_fill_module_pin_info_v4(dfw->out_pin, mconfig->m_out_pin,
2847ac9391daSGuenter Roeck 				    dfw->is_dynamic_out_pin,
2848ac9391daSGuenter Roeck 				    mconfig->module->max_output_pins);
2849ac9391daSGuenter Roeck 
2850ac9391daSGuenter Roeck 	if (mconfig->formats_config.caps_size) {
2851ac9391daSGuenter Roeck 		mconfig->formats_config.set_params = dfw->caps.set_params;
2852ac9391daSGuenter Roeck 		mconfig->formats_config.param_id = dfw->caps.param_id;
2853ac9391daSGuenter Roeck 		mconfig->formats_config.caps =
2854ac9391daSGuenter Roeck 		devm_kzalloc(dev, mconfig->formats_config.caps_size,
2855ac9391daSGuenter Roeck 			     GFP_KERNEL);
2856ac9391daSGuenter Roeck 		if (!mconfig->formats_config.caps)
2857ac9391daSGuenter Roeck 			return -ENOMEM;
2858ac9391daSGuenter Roeck 		memcpy(mconfig->formats_config.caps, dfw->caps.caps,
2859ac9391daSGuenter Roeck 		       dfw->caps.caps_size);
2860ac9391daSGuenter Roeck 	}
2861ac9391daSGuenter Roeck 
2862ac9391daSGuenter Roeck 	return 0;
2863ac9391daSGuenter Roeck }
2864ac9391daSGuenter Roeck 
28656277e832SShreyas NC /*
28666277e832SShreyas NC  * Parse the private data for the token and corresponding value.
28676277e832SShreyas NC  * The private data can have multiple data blocks. So, a data block
28686277e832SShreyas NC  * is preceded by a descriptor for number of blocks and a descriptor
28696277e832SShreyas NC  * for the type and size of the suceeding data block.
28706277e832SShreyas NC  */
28716277e832SShreyas NC static int skl_tplg_get_pvt_data(struct snd_soc_tplg_dapm_widget *tplg_w,
2872bcc2a2dcSCezary Rojewski 				struct skl_dev *skl, struct device *dev,
28736277e832SShreyas NC 				struct skl_module_cfg *mconfig)
28746277e832SShreyas NC {
28756277e832SShreyas NC 	struct snd_soc_tplg_vendor_array *array;
287611a790f9SPierre-Louis Bossart 	int num_blocks, block_size, block_type, off = 0;
28776277e832SShreyas NC 	char *data;
28786277e832SShreyas NC 	int ret;
28796277e832SShreyas NC 
2880ac9391daSGuenter Roeck 	/*
2881ac9391daSGuenter Roeck 	 * v4 configuration files have a valid UUID at the start of
2882ac9391daSGuenter Roeck 	 * the widget's private data.
2883ac9391daSGuenter Roeck 	 */
2884ac9391daSGuenter Roeck 	if (uuid_is_valid((char *)tplg_w->priv.data))
2885ac9391daSGuenter Roeck 		return skl_tplg_get_pvt_data_v4(tplg_w, skl, dev, mconfig);
2886ac9391daSGuenter Roeck 
28876277e832SShreyas NC 	/* Read the NUM_DATA_BLOCKS descriptor */
28886277e832SShreyas NC 	array = (struct snd_soc_tplg_vendor_array *)tplg_w->priv.data;
28896277e832SShreyas NC 	ret = skl_tplg_get_desc_blocks(dev, array);
28906277e832SShreyas NC 	if (ret < 0)
28916277e832SShreyas NC 		return ret;
28926277e832SShreyas NC 	num_blocks = ret;
28936277e832SShreyas NC 
28946277e832SShreyas NC 	off += array->size;
28956277e832SShreyas NC 	/* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */
28966277e832SShreyas NC 	while (num_blocks > 0) {
2897133e6e5cSShreyas NC 		array = (struct snd_soc_tplg_vendor_array *)
2898133e6e5cSShreyas NC 				(tplg_w->priv.data + off);
2899133e6e5cSShreyas NC 
29006277e832SShreyas NC 		ret = skl_tplg_get_desc_blocks(dev, array);
29016277e832SShreyas NC 
29026277e832SShreyas NC 		if (ret < 0)
29036277e832SShreyas NC 			return ret;
29046277e832SShreyas NC 		block_type = ret;
29056277e832SShreyas NC 		off += array->size;
29066277e832SShreyas NC 
29076277e832SShreyas NC 		array = (struct snd_soc_tplg_vendor_array *)
29086277e832SShreyas NC 			(tplg_w->priv.data + off);
29096277e832SShreyas NC 
29106277e832SShreyas NC 		ret = skl_tplg_get_desc_blocks(dev, array);
29116277e832SShreyas NC 
29126277e832SShreyas NC 		if (ret < 0)
29136277e832SShreyas NC 			return ret;
29146277e832SShreyas NC 		block_size = ret;
29156277e832SShreyas NC 		off += array->size;
29166277e832SShreyas NC 
29176277e832SShreyas NC 		array = (struct snd_soc_tplg_vendor_array *)
29186277e832SShreyas NC 			(tplg_w->priv.data + off);
29196277e832SShreyas NC 
29206277e832SShreyas NC 		data = (tplg_w->priv.data + off);
29216277e832SShreyas NC 
29226277e832SShreyas NC 		if (block_type == SKL_TYPE_TUPLE) {
29236277e832SShreyas NC 			ret = skl_tplg_get_tokens(dev, data,
29246277e832SShreyas NC 					skl, mconfig, block_size);
29256277e832SShreyas NC 
29266277e832SShreyas NC 			if (ret < 0)
29276277e832SShreyas NC 				return ret;
29286277e832SShreyas NC 
29296277e832SShreyas NC 			--num_blocks;
29306277e832SShreyas NC 		} else {
29316277e832SShreyas NC 			if (mconfig->formats_config.caps_size > 0)
29326277e832SShreyas NC 				memcpy(mconfig->formats_config.caps, data,
29336277e832SShreyas NC 					mconfig->formats_config.caps_size);
29346277e832SShreyas NC 			--num_blocks;
2935133e6e5cSShreyas NC 			ret = mconfig->formats_config.caps_size;
29366277e832SShreyas NC 		}
2937133e6e5cSShreyas NC 		off += ret;
29386277e832SShreyas NC 	}
29396277e832SShreyas NC 
29406277e832SShreyas NC 	return 0;
29414cd9899fSHardik T Shah }
29424cd9899fSHardik T Shah 
294356b03b4cSKuninori Morimoto static void skl_clear_pin_config(struct snd_soc_component *component,
2944fe3f4442SDharageswari R 				struct snd_soc_dapm_widget *w)
2945fe3f4442SDharageswari R {
2946fe3f4442SDharageswari R 	int i;
2947fe3f4442SDharageswari R 	struct skl_module_cfg *mconfig;
2948fe3f4442SDharageswari R 	struct skl_pipe *pipe;
2949fe3f4442SDharageswari R 
295056b03b4cSKuninori Morimoto 	if (!strncmp(w->dapm->component->name, component->name,
295156b03b4cSKuninori Morimoto 					strlen(component->name))) {
2952fe3f4442SDharageswari R 		mconfig = w->priv;
2953fe3f4442SDharageswari R 		pipe = mconfig->pipe;
2954f6fa56e2SRamesh Babu 		for (i = 0; i < mconfig->module->max_input_pins; i++) {
2955fe3f4442SDharageswari R 			mconfig->m_in_pin[i].in_use = false;
2956fe3f4442SDharageswari R 			mconfig->m_in_pin[i].pin_state = SKL_PIN_UNBIND;
2957fe3f4442SDharageswari R 		}
2958f6fa56e2SRamesh Babu 		for (i = 0; i < mconfig->module->max_output_pins; i++) {
2959fe3f4442SDharageswari R 			mconfig->m_out_pin[i].in_use = false;
2960fe3f4442SDharageswari R 			mconfig->m_out_pin[i].pin_state = SKL_PIN_UNBIND;
2961fe3f4442SDharageswari R 		}
2962fe3f4442SDharageswari R 		pipe->state = SKL_PIPE_INVALID;
2963fe3f4442SDharageswari R 		mconfig->m_state = SKL_MODULE_UNINIT;
2964fe3f4442SDharageswari R 	}
2965fe3f4442SDharageswari R }
2966fe3f4442SDharageswari R 
2967bcc2a2dcSCezary Rojewski void skl_cleanup_resources(struct skl_dev *skl)
2968fe3f4442SDharageswari R {
296956b03b4cSKuninori Morimoto 	struct snd_soc_component *soc_component = skl->component;
2970fe3f4442SDharageswari R 	struct snd_soc_dapm_widget *w;
2971fe3f4442SDharageswari R 	struct snd_soc_card *card;
2972fe3f4442SDharageswari R 
297356b03b4cSKuninori Morimoto 	if (soc_component == NULL)
2974fe3f4442SDharageswari R 		return;
2975fe3f4442SDharageswari R 
297656b03b4cSKuninori Morimoto 	card = soc_component->card;
2977fe3f4442SDharageswari R 	if (!card || !card->instantiated)
2978fe3f4442SDharageswari R 		return;
2979fe3f4442SDharageswari R 
2980fe3f4442SDharageswari R 	list_for_each_entry(w, &card->widgets, list) {
2981bcc2a2dcSCezary Rojewski 		if (is_skl_dsp_widget_type(w, skl->dev) && w->priv != NULL)
298256b03b4cSKuninori Morimoto 			skl_clear_pin_config(soc_component, w);
2983fe3f4442SDharageswari R 	}
2984fe3f4442SDharageswari R 
2985bcc2a2dcSCezary Rojewski 	skl_clear_module_cnt(skl->dsp);
2986fe3f4442SDharageswari R }
2987fe3f4442SDharageswari R 
29883af36706SVinod Koul /*
29893af36706SVinod Koul  * Topology core widget load callback
29903af36706SVinod Koul  *
29913af36706SVinod Koul  * This is used to save the private data for each widget which gives
29923af36706SVinod Koul  * information to the driver about module and pipeline parameters which DSP
29933af36706SVinod Koul  * FW expects like ids, resource values, formats etc
29943af36706SVinod Koul  */
2995c60b613aSLiam Girdwood static int skl_tplg_widget_load(struct snd_soc_component *cmpnt, int index,
29963af36706SVinod Koul 				struct snd_soc_dapm_widget *w,
29973af36706SVinod Koul 				struct snd_soc_tplg_dapm_widget *tplg_w)
29983af36706SVinod Koul {
29993af36706SVinod Koul 	int ret;
300076f56faeSRakesh Ughreja 	struct hdac_bus *bus = snd_soc_component_get_drvdata(cmpnt);
3001bcc2a2dcSCezary Rojewski 	struct skl_dev *skl = bus_to_skl(bus);
30023af36706SVinod Koul 	struct skl_module_cfg *mconfig;
30033af36706SVinod Koul 
30043af36706SVinod Koul 	if (!tplg_w->priv.size)
30053af36706SVinod Koul 		goto bind_event;
30063af36706SVinod Koul 
30073af36706SVinod Koul 	mconfig = devm_kzalloc(bus->dev, sizeof(*mconfig), GFP_KERNEL);
30083af36706SVinod Koul 
30093af36706SVinod Koul 	if (!mconfig)
30103af36706SVinod Koul 		return -ENOMEM;
30113af36706SVinod Koul 
3012f6fa56e2SRamesh Babu 	if (skl->nr_modules == 0) {
3013f6fa56e2SRamesh Babu 		mconfig->module = devm_kzalloc(bus->dev,
3014f6fa56e2SRamesh Babu 				sizeof(*mconfig->module), GFP_KERNEL);
3015f6fa56e2SRamesh Babu 		if (!mconfig->module)
3016f6fa56e2SRamesh Babu 			return -ENOMEM;
3017f6fa56e2SRamesh Babu 	}
3018f6fa56e2SRamesh Babu 
30193af36706SVinod Koul 	w->priv = mconfig;
302009305da9SShreyas NC 
3021b7c50555SVinod Koul 	/*
3022b7c50555SVinod Koul 	 * module binary can be loaded later, so set it to query when
3023b7c50555SVinod Koul 	 * module is load for a use case
3024b7c50555SVinod Koul 	 */
3025b7c50555SVinod Koul 	mconfig->id.module_id = -1;
30264cd9899fSHardik T Shah 
30276277e832SShreyas NC 	/* Parse private data for tuples */
30286277e832SShreyas NC 	ret = skl_tplg_get_pvt_data(tplg_w, skl, bus->dev, mconfig);
30296277e832SShreyas NC 	if (ret < 0)
30306277e832SShreyas NC 		return ret;
3031d14700a0SVinod Koul 
3032d14700a0SVinod Koul 	skl_debug_init_module(skl->debugfs, w, mconfig);
3033d14700a0SVinod Koul 
30343af36706SVinod Koul bind_event:
30353af36706SVinod Koul 	if (tplg_w->event_type == 0) {
30363373f716SVinod Koul 		dev_dbg(bus->dev, "ASoC: No event handler required\n");
30373af36706SVinod Koul 		return 0;
30383af36706SVinod Koul 	}
30393af36706SVinod Koul 
30403af36706SVinod Koul 	ret = snd_soc_tplg_widget_bind_event(w, skl_tplg_widget_ops,
3041b663a8c5SJeeja KP 					ARRAY_SIZE(skl_tplg_widget_ops),
3042b663a8c5SJeeja KP 					tplg_w->event_type);
30433af36706SVinod Koul 
30443af36706SVinod Koul 	if (ret) {
30453af36706SVinod Koul 		dev_err(bus->dev, "%s: No matching event handlers found for %d\n",
30463af36706SVinod Koul 					__func__, tplg_w->event_type);
30473af36706SVinod Koul 		return -EINVAL;
30483af36706SVinod Koul 	}
30493af36706SVinod Koul 
30503af36706SVinod Koul 	return 0;
30513af36706SVinod Koul }
30523af36706SVinod Koul 
3053140adfbaSJeeja KP static int skl_init_algo_data(struct device *dev, struct soc_bytes_ext *be,
3054140adfbaSJeeja KP 					struct snd_soc_tplg_bytes_control *bc)
3055140adfbaSJeeja KP {
3056140adfbaSJeeja KP 	struct skl_algo_data *ac;
3057140adfbaSJeeja KP 	struct skl_dfw_algo_data *dfw_ac =
3058140adfbaSJeeja KP 				(struct skl_dfw_algo_data *)bc->priv.data;
3059140adfbaSJeeja KP 
3060140adfbaSJeeja KP 	ac = devm_kzalloc(dev, sizeof(*ac), GFP_KERNEL);
3061140adfbaSJeeja KP 	if (!ac)
3062140adfbaSJeeja KP 		return -ENOMEM;
3063140adfbaSJeeja KP 
3064140adfbaSJeeja KP 	/* Fill private data */
3065140adfbaSJeeja KP 	ac->max = dfw_ac->max;
3066140adfbaSJeeja KP 	ac->param_id = dfw_ac->param_id;
3067140adfbaSJeeja KP 	ac->set_params = dfw_ac->set_params;
30680d682104SDharageswari R 	ac->size = dfw_ac->max;
3069140adfbaSJeeja KP 
3070140adfbaSJeeja KP 	if (ac->max) {
3071431b67c2SPierre-Louis Bossart 		ac->params = devm_kzalloc(dev, ac->max, GFP_KERNEL);
3072140adfbaSJeeja KP 		if (!ac->params)
3073140adfbaSJeeja KP 			return -ENOMEM;
3074140adfbaSJeeja KP 
3075140adfbaSJeeja KP 		memcpy(ac->params, dfw_ac->params, ac->max);
3076140adfbaSJeeja KP 	}
3077140adfbaSJeeja KP 
3078140adfbaSJeeja KP 	be->dobj.private  = ac;
3079140adfbaSJeeja KP 	return 0;
3080140adfbaSJeeja KP }
3081140adfbaSJeeja KP 
30827a1b749bSDharageswari R static int skl_init_enum_data(struct device *dev, struct soc_enum *se,
30837a1b749bSDharageswari R 				struct snd_soc_tplg_enum_control *ec)
30847a1b749bSDharageswari R {
30857a1b749bSDharageswari R 
30867a1b749bSDharageswari R 	void *data;
30877a1b749bSDharageswari R 
30887a1b749bSDharageswari R 	if (ec->priv.size) {
30897a1b749bSDharageswari R 		data = devm_kzalloc(dev, sizeof(ec->priv.size), GFP_KERNEL);
30907a1b749bSDharageswari R 		if (!data)
30917a1b749bSDharageswari R 			return -ENOMEM;
30927a1b749bSDharageswari R 		memcpy(data, ec->priv.data, ec->priv.size);
30937a1b749bSDharageswari R 		se->dobj.private = data;
30947a1b749bSDharageswari R 	}
30957a1b749bSDharageswari R 
30967a1b749bSDharageswari R 	return 0;
30977a1b749bSDharageswari R 
30987a1b749bSDharageswari R }
30997a1b749bSDharageswari R 
3100140adfbaSJeeja KP static int skl_tplg_control_load(struct snd_soc_component *cmpnt,
3101c60b613aSLiam Girdwood 				int index,
3102140adfbaSJeeja KP 				struct snd_kcontrol_new *kctl,
3103140adfbaSJeeja KP 				struct snd_soc_tplg_ctl_hdr *hdr)
3104140adfbaSJeeja KP {
3105140adfbaSJeeja KP 	struct soc_bytes_ext *sb;
3106140adfbaSJeeja KP 	struct snd_soc_tplg_bytes_control *tplg_bc;
31077a1b749bSDharageswari R 	struct snd_soc_tplg_enum_control *tplg_ec;
310876f56faeSRakesh Ughreja 	struct hdac_bus *bus  = snd_soc_component_get_drvdata(cmpnt);
31097a1b749bSDharageswari R 	struct soc_enum *se;
3110140adfbaSJeeja KP 
3111140adfbaSJeeja KP 	switch (hdr->ops.info) {
3112140adfbaSJeeja KP 	case SND_SOC_TPLG_CTL_BYTES:
3113140adfbaSJeeja KP 		tplg_bc = container_of(hdr,
3114140adfbaSJeeja KP 				struct snd_soc_tplg_bytes_control, hdr);
3115140adfbaSJeeja KP 		if (kctl->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
3116140adfbaSJeeja KP 			sb = (struct soc_bytes_ext *)kctl->private_value;
3117140adfbaSJeeja KP 			if (tplg_bc->priv.size)
3118140adfbaSJeeja KP 				return skl_init_algo_data(
3119140adfbaSJeeja KP 						bus->dev, sb, tplg_bc);
3120140adfbaSJeeja KP 		}
3121140adfbaSJeeja KP 		break;
3122140adfbaSJeeja KP 
31237a1b749bSDharageswari R 	case SND_SOC_TPLG_CTL_ENUM:
31247a1b749bSDharageswari R 		tplg_ec = container_of(hdr,
31257a1b749bSDharageswari R 				struct snd_soc_tplg_enum_control, hdr);
31262d744ecfSMateusz Gorski 		if (kctl->access & SNDRV_CTL_ELEM_ACCESS_READ) {
31277a1b749bSDharageswari R 			se = (struct soc_enum *)kctl->private_value;
31287a1b749bSDharageswari R 			if (tplg_ec->priv.size)
31292d744ecfSMateusz Gorski 				skl_init_enum_data(bus->dev, se, tplg_ec);
31307a1b749bSDharageswari R 		}
31312d744ecfSMateusz Gorski 
31322d744ecfSMateusz Gorski 		/*
31332d744ecfSMateusz Gorski 		 * now that the control initializations are done, remove
31342d744ecfSMateusz Gorski 		 * write permission for the DMIC configuration enums to
31352d744ecfSMateusz Gorski 		 * avoid conflicts between NHLT settings and user interaction
31362d744ecfSMateusz Gorski 		 */
31372d744ecfSMateusz Gorski 
31382d744ecfSMateusz Gorski 		if (hdr->ops.get == SKL_CONTROL_TYPE_MULTI_IO_SELECT_DMIC)
31392d744ecfSMateusz Gorski 			kctl->access = SNDRV_CTL_ELEM_ACCESS_READ;
31402d744ecfSMateusz Gorski 
31417a1b749bSDharageswari R 		break;
31427a1b749bSDharageswari R 
3143140adfbaSJeeja KP 	default:
31444362934aSNaveen Manohar 		dev_dbg(bus->dev, "Control load not supported %d:%d:%d\n",
3145140adfbaSJeeja KP 			hdr->ops.get, hdr->ops.put, hdr->ops.info);
3146140adfbaSJeeja KP 		break;
3147140adfbaSJeeja KP 	}
3148140adfbaSJeeja KP 
3149140adfbaSJeeja KP 	return 0;
3150140adfbaSJeeja KP }
3151140adfbaSJeeja KP 
3152541070ceSShreyas NC static int skl_tplg_fill_str_mfest_tkn(struct device *dev,
3153541070ceSShreyas NC 		struct snd_soc_tplg_vendor_string_elem *str_elem,
3154bcc2a2dcSCezary Rojewski 		struct skl_dev *skl)
3155541070ceSShreyas NC {
3156541070ceSShreyas NC 	int tkn_count = 0;
3157541070ceSShreyas NC 	static int ref_count;
3158541070ceSShreyas NC 
3159541070ceSShreyas NC 	switch (str_elem->token) {
3160541070ceSShreyas NC 	case SKL_TKN_STR_LIB_NAME:
3161bcc2a2dcSCezary Rojewski 		if (ref_count > skl->lib_count - 1) {
3162541070ceSShreyas NC 			ref_count = 0;
3163541070ceSShreyas NC 			return -EINVAL;
3164541070ceSShreyas NC 		}
3165541070ceSShreyas NC 
3166bcc2a2dcSCezary Rojewski 		strncpy(skl->lib_info[ref_count].name,
3167eee0e16fSJeeja KP 			str_elem->string,
3168bcc2a2dcSCezary Rojewski 			ARRAY_SIZE(skl->lib_info[ref_count].name));
3169541070ceSShreyas NC 		ref_count++;
3170541070ceSShreyas NC 		break;
3171541070ceSShreyas NC 
3172541070ceSShreyas NC 	default:
3173ecd286a9SColin Ian King 		dev_err(dev, "Not a string token %d\n", str_elem->token);
3174541070ceSShreyas NC 		break;
3175541070ceSShreyas NC 	}
3176db6ed55dSShreyas NC 	tkn_count++;
3177541070ceSShreyas NC 
3178541070ceSShreyas NC 	return tkn_count;
3179541070ceSShreyas NC }
3180541070ceSShreyas NC 
3181541070ceSShreyas NC static int skl_tplg_get_str_tkn(struct device *dev,
3182541070ceSShreyas NC 		struct snd_soc_tplg_vendor_array *array,
3183bcc2a2dcSCezary Rojewski 		struct skl_dev *skl)
3184541070ceSShreyas NC {
3185541070ceSShreyas NC 	int tkn_count = 0, ret;
3186541070ceSShreyas NC 	struct snd_soc_tplg_vendor_string_elem *str_elem;
3187541070ceSShreyas NC 
3188541070ceSShreyas NC 	str_elem = (struct snd_soc_tplg_vendor_string_elem *)array->value;
3189541070ceSShreyas NC 	while (tkn_count < array->num_elems) {
3190eee0e16fSJeeja KP 		ret = skl_tplg_fill_str_mfest_tkn(dev, str_elem, skl);
3191541070ceSShreyas NC 		str_elem++;
3192541070ceSShreyas NC 
3193541070ceSShreyas NC 		if (ret < 0)
3194541070ceSShreyas NC 			return ret;
3195541070ceSShreyas NC 
3196541070ceSShreyas NC 		tkn_count = tkn_count + ret;
3197541070ceSShreyas NC 	}
3198541070ceSShreyas NC 
3199541070ceSShreyas NC 	return tkn_count;
3200541070ceSShreyas NC }
3201541070ceSShreyas NC 
3202db6ed55dSShreyas NC static int skl_tplg_manifest_fill_fmt(struct device *dev,
3203db6ed55dSShreyas NC 		struct skl_module_iface *fmt,
3204db6ed55dSShreyas NC 		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
3205db6ed55dSShreyas NC 		u32 dir, int fmt_idx)
3206db6ed55dSShreyas NC {
3207db6ed55dSShreyas NC 	struct skl_module_pin_fmt *dst_fmt;
3208db6ed55dSShreyas NC 	struct skl_module_fmt *mod_fmt;
3209db6ed55dSShreyas NC 	int ret;
3210db6ed55dSShreyas NC 
3211db6ed55dSShreyas NC 	if (!fmt)
3212db6ed55dSShreyas NC 		return -EINVAL;
3213db6ed55dSShreyas NC 
3214db6ed55dSShreyas NC 	switch (dir) {
3215db6ed55dSShreyas NC 	case SKL_DIR_IN:
3216db6ed55dSShreyas NC 		dst_fmt = &fmt->inputs[fmt_idx];
3217db6ed55dSShreyas NC 		break;
3218db6ed55dSShreyas NC 
3219db6ed55dSShreyas NC 	case SKL_DIR_OUT:
3220db6ed55dSShreyas NC 		dst_fmt = &fmt->outputs[fmt_idx];
3221db6ed55dSShreyas NC 		break;
3222db6ed55dSShreyas NC 
3223db6ed55dSShreyas NC 	default:
3224db6ed55dSShreyas NC 		dev_err(dev, "Invalid direction: %d\n", dir);
3225db6ed55dSShreyas NC 		return -EINVAL;
3226db6ed55dSShreyas NC 	}
3227db6ed55dSShreyas NC 
3228db6ed55dSShreyas NC 	mod_fmt = &dst_fmt->fmt;
3229db6ed55dSShreyas NC 
3230db6ed55dSShreyas NC 	switch (tkn_elem->token) {
3231db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_INTF_PIN_ID:
3232db6ed55dSShreyas NC 		dst_fmt->id = tkn_elem->value;
3233db6ed55dSShreyas NC 		break;
3234db6ed55dSShreyas NC 
3235db6ed55dSShreyas NC 	default:
3236db6ed55dSShreyas NC 		ret = skl_tplg_fill_fmt(dev, mod_fmt, tkn_elem->token,
3237db6ed55dSShreyas NC 					tkn_elem->value);
3238db6ed55dSShreyas NC 		if (ret < 0)
3239db6ed55dSShreyas NC 			return ret;
3240db6ed55dSShreyas NC 		break;
3241db6ed55dSShreyas NC 	}
3242db6ed55dSShreyas NC 
3243db6ed55dSShreyas NC 	return 0;
3244db6ed55dSShreyas NC }
3245db6ed55dSShreyas NC 
3246db6ed55dSShreyas NC static int skl_tplg_fill_mod_info(struct device *dev,
3247db6ed55dSShreyas NC 		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
3248db6ed55dSShreyas NC 		struct skl_module *mod)
3249db6ed55dSShreyas NC {
3250db6ed55dSShreyas NC 
3251db6ed55dSShreyas NC 	if (!mod)
3252db6ed55dSShreyas NC 		return -EINVAL;
3253db6ed55dSShreyas NC 
3254db6ed55dSShreyas NC 	switch (tkn_elem->token) {
3255db6ed55dSShreyas NC 	case SKL_TKN_U8_IN_PIN_TYPE:
3256db6ed55dSShreyas NC 		mod->input_pin_type = tkn_elem->value;
3257db6ed55dSShreyas NC 		break;
3258db6ed55dSShreyas NC 
3259db6ed55dSShreyas NC 	case SKL_TKN_U8_OUT_PIN_TYPE:
3260db6ed55dSShreyas NC 		mod->output_pin_type = tkn_elem->value;
3261db6ed55dSShreyas NC 		break;
3262db6ed55dSShreyas NC 
3263db6ed55dSShreyas NC 	case SKL_TKN_U8_IN_QUEUE_COUNT:
3264db6ed55dSShreyas NC 		mod->max_input_pins = tkn_elem->value;
3265db6ed55dSShreyas NC 		break;
3266db6ed55dSShreyas NC 
3267db6ed55dSShreyas NC 	case SKL_TKN_U8_OUT_QUEUE_COUNT:
3268db6ed55dSShreyas NC 		mod->max_output_pins = tkn_elem->value;
3269db6ed55dSShreyas NC 		break;
3270db6ed55dSShreyas NC 
3271db6ed55dSShreyas NC 	case SKL_TKN_MM_U8_NUM_RES:
3272db6ed55dSShreyas NC 		mod->nr_resources = tkn_elem->value;
3273db6ed55dSShreyas NC 		break;
3274db6ed55dSShreyas NC 
3275db6ed55dSShreyas NC 	case SKL_TKN_MM_U8_NUM_INTF:
3276db6ed55dSShreyas NC 		mod->nr_interfaces = tkn_elem->value;
3277db6ed55dSShreyas NC 		break;
3278db6ed55dSShreyas NC 
3279db6ed55dSShreyas NC 	default:
3280db6ed55dSShreyas NC 		dev_err(dev, "Invalid mod info token %d", tkn_elem->token);
3281db6ed55dSShreyas NC 		return -EINVAL;
3282db6ed55dSShreyas NC 	}
3283db6ed55dSShreyas NC 
3284db6ed55dSShreyas NC 	return 0;
3285db6ed55dSShreyas NC }
3286db6ed55dSShreyas NC 
3287db6ed55dSShreyas NC 
3288541070ceSShreyas NC static int skl_tplg_get_int_tkn(struct device *dev,
3289541070ceSShreyas NC 		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
3290bcc2a2dcSCezary Rojewski 		struct skl_dev *skl)
3291541070ceSShreyas NC {
3292d00cc2f1SGustavo A. R. Silva 	int tkn_count = 0, ret;
3293db6ed55dSShreyas NC 	static int mod_idx, res_val_idx, intf_val_idx, dir, pin_idx;
3294db6ed55dSShreyas NC 	struct skl_module_res *res = NULL;
3295db6ed55dSShreyas NC 	struct skl_module_iface *fmt = NULL;
3296db6ed55dSShreyas NC 	struct skl_module *mod = NULL;
329743762355SPradeep Tewani 	static struct skl_astate_param *astate_table;
329843762355SPradeep Tewani 	static int astate_cfg_idx, count;
3299db6ed55dSShreyas NC 	int i;
3300d00cc2f1SGustavo A. R. Silva 	size_t size;
3301db6ed55dSShreyas NC 
3302db6ed55dSShreyas NC 	if (skl->modules) {
3303db6ed55dSShreyas NC 		mod = skl->modules[mod_idx];
3304db6ed55dSShreyas NC 		res = &mod->resources[res_val_idx];
3305db6ed55dSShreyas NC 		fmt = &mod->formats[intf_val_idx];
3306db6ed55dSShreyas NC 	}
3307541070ceSShreyas NC 
3308541070ceSShreyas NC 	switch (tkn_elem->token) {
3309541070ceSShreyas NC 	case SKL_TKN_U32_LIB_COUNT:
3310bcc2a2dcSCezary Rojewski 		skl->lib_count = tkn_elem->value;
3311db6ed55dSShreyas NC 		break;
3312db6ed55dSShreyas NC 
3313db6ed55dSShreyas NC 	case SKL_TKN_U8_NUM_MOD:
3314db6ed55dSShreyas NC 		skl->nr_modules = tkn_elem->value;
3315db6ed55dSShreyas NC 		skl->modules = devm_kcalloc(dev, skl->nr_modules,
3316db6ed55dSShreyas NC 				sizeof(*skl->modules), GFP_KERNEL);
3317db6ed55dSShreyas NC 		if (!skl->modules)
3318db6ed55dSShreyas NC 			return -ENOMEM;
3319db6ed55dSShreyas NC 
3320db6ed55dSShreyas NC 		for (i = 0; i < skl->nr_modules; i++) {
3321db6ed55dSShreyas NC 			skl->modules[i] = devm_kzalloc(dev,
3322db6ed55dSShreyas NC 					sizeof(struct skl_module), GFP_KERNEL);
3323db6ed55dSShreyas NC 			if (!skl->modules[i])
3324db6ed55dSShreyas NC 				return -ENOMEM;
3325db6ed55dSShreyas NC 		}
3326db6ed55dSShreyas NC 		break;
3327db6ed55dSShreyas NC 
3328db6ed55dSShreyas NC 	case SKL_TKN_MM_U8_MOD_IDX:
3329db6ed55dSShreyas NC 		mod_idx = tkn_elem->value;
3330db6ed55dSShreyas NC 		break;
3331db6ed55dSShreyas NC 
333243762355SPradeep Tewani 	case SKL_TKN_U32_ASTATE_COUNT:
333343762355SPradeep Tewani 		if (astate_table != NULL) {
333443762355SPradeep Tewani 			dev_err(dev, "More than one entry for A-State count");
333543762355SPradeep Tewani 			return -EINVAL;
333643762355SPradeep Tewani 		}
333743762355SPradeep Tewani 
333843762355SPradeep Tewani 		if (tkn_elem->value > SKL_MAX_ASTATE_CFG) {
333943762355SPradeep Tewani 			dev_err(dev, "Invalid A-State count %d\n",
334043762355SPradeep Tewani 				tkn_elem->value);
334143762355SPradeep Tewani 			return -EINVAL;
334243762355SPradeep Tewani 		}
334343762355SPradeep Tewani 
3344d00cc2f1SGustavo A. R. Silva 		size = struct_size(skl->cfg.astate_cfg, astate_table,
3345d00cc2f1SGustavo A. R. Silva 				   tkn_elem->value);
334643762355SPradeep Tewani 		skl->cfg.astate_cfg = devm_kzalloc(dev, size, GFP_KERNEL);
334743762355SPradeep Tewani 		if (!skl->cfg.astate_cfg)
334843762355SPradeep Tewani 			return -ENOMEM;
334943762355SPradeep Tewani 
335043762355SPradeep Tewani 		astate_table = skl->cfg.astate_cfg->astate_table;
335143762355SPradeep Tewani 		count = skl->cfg.astate_cfg->count = tkn_elem->value;
335243762355SPradeep Tewani 		break;
335343762355SPradeep Tewani 
335443762355SPradeep Tewani 	case SKL_TKN_U32_ASTATE_IDX:
335543762355SPradeep Tewani 		if (tkn_elem->value >= count) {
335643762355SPradeep Tewani 			dev_err(dev, "Invalid A-State index %d\n",
335743762355SPradeep Tewani 				tkn_elem->value);
335843762355SPradeep Tewani 			return -EINVAL;
335943762355SPradeep Tewani 		}
336043762355SPradeep Tewani 
336143762355SPradeep Tewani 		astate_cfg_idx = tkn_elem->value;
336243762355SPradeep Tewani 		break;
336343762355SPradeep Tewani 
336443762355SPradeep Tewani 	case SKL_TKN_U32_ASTATE_KCPS:
336543762355SPradeep Tewani 		astate_table[astate_cfg_idx].kcps = tkn_elem->value;
336643762355SPradeep Tewani 		break;
336743762355SPradeep Tewani 
336843762355SPradeep Tewani 	case SKL_TKN_U32_ASTATE_CLK_SRC:
336943762355SPradeep Tewani 		astate_table[astate_cfg_idx].clk_src = tkn_elem->value;
337043762355SPradeep Tewani 		break;
337143762355SPradeep Tewani 
3372db6ed55dSShreyas NC 	case SKL_TKN_U8_IN_PIN_TYPE:
3373db6ed55dSShreyas NC 	case SKL_TKN_U8_OUT_PIN_TYPE:
3374db6ed55dSShreyas NC 	case SKL_TKN_U8_IN_QUEUE_COUNT:
3375db6ed55dSShreyas NC 	case SKL_TKN_U8_OUT_QUEUE_COUNT:
3376db6ed55dSShreyas NC 	case SKL_TKN_MM_U8_NUM_RES:
3377db6ed55dSShreyas NC 	case SKL_TKN_MM_U8_NUM_INTF:
3378db6ed55dSShreyas NC 		ret = skl_tplg_fill_mod_info(dev, tkn_elem, mod);
3379db6ed55dSShreyas NC 		if (ret < 0)
3380db6ed55dSShreyas NC 			return ret;
3381db6ed55dSShreyas NC 		break;
3382db6ed55dSShreyas NC 
3383db6ed55dSShreyas NC 	case SKL_TKN_U32_DIR_PIN_COUNT:
3384db6ed55dSShreyas NC 		dir = tkn_elem->value & SKL_IN_DIR_BIT_MASK;
3385db6ed55dSShreyas NC 		pin_idx = (tkn_elem->value & SKL_PIN_COUNT_MASK) >> 4;
3386db6ed55dSShreyas NC 		break;
3387db6ed55dSShreyas NC 
3388db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_RES_ID:
3389db6ed55dSShreyas NC 		if (!res)
3390db6ed55dSShreyas NC 			return -EINVAL;
3391db6ed55dSShreyas NC 
3392db6ed55dSShreyas NC 		res->id = tkn_elem->value;
3393db6ed55dSShreyas NC 		res_val_idx = tkn_elem->value;
3394db6ed55dSShreyas NC 		break;
3395db6ed55dSShreyas NC 
3396db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_FMT_ID:
3397db6ed55dSShreyas NC 		if (!fmt)
3398db6ed55dSShreyas NC 			return -EINVAL;
3399db6ed55dSShreyas NC 
3400db6ed55dSShreyas NC 		fmt->fmt_idx = tkn_elem->value;
3401db6ed55dSShreyas NC 		intf_val_idx = tkn_elem->value;
3402db6ed55dSShreyas NC 		break;
3403db6ed55dSShreyas NC 
3404db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_CPS:
3405db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_DMA_SIZE:
3406db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_CPC:
3407db6ed55dSShreyas NC 	case SKL_TKN_U32_MEM_PAGES:
3408db6ed55dSShreyas NC 	case SKL_TKN_U32_OBS:
3409db6ed55dSShreyas NC 	case SKL_TKN_U32_IBS:
3410db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_RES_PIN_ID:
3411db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_PIN_BUF:
3412db6ed55dSShreyas NC 		ret = skl_tplg_fill_res_tkn(dev, tkn_elem, res, pin_idx, dir);
3413db6ed55dSShreyas NC 		if (ret < 0)
3414db6ed55dSShreyas NC 			return ret;
3415db6ed55dSShreyas NC 
3416db6ed55dSShreyas NC 		break;
3417db6ed55dSShreyas NC 
3418db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_NUM_IN_FMT:
3419db6ed55dSShreyas NC 		if (!fmt)
3420db6ed55dSShreyas NC 			return -EINVAL;
3421db6ed55dSShreyas NC 
3422db6ed55dSShreyas NC 		res->nr_input_pins = tkn_elem->value;
3423db6ed55dSShreyas NC 		break;
3424db6ed55dSShreyas NC 
3425db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_NUM_OUT_FMT:
3426db6ed55dSShreyas NC 		if (!fmt)
3427db6ed55dSShreyas NC 			return -EINVAL;
3428db6ed55dSShreyas NC 
3429db6ed55dSShreyas NC 		res->nr_output_pins = tkn_elem->value;
3430db6ed55dSShreyas NC 		break;
3431db6ed55dSShreyas NC 
3432db6ed55dSShreyas NC 	case SKL_TKN_U32_FMT_CH:
3433db6ed55dSShreyas NC 	case SKL_TKN_U32_FMT_FREQ:
3434db6ed55dSShreyas NC 	case SKL_TKN_U32_FMT_BIT_DEPTH:
3435db6ed55dSShreyas NC 	case SKL_TKN_U32_FMT_SAMPLE_SIZE:
3436db6ed55dSShreyas NC 	case SKL_TKN_U32_FMT_CH_CONFIG:
3437db6ed55dSShreyas NC 	case SKL_TKN_U32_FMT_INTERLEAVE:
3438db6ed55dSShreyas NC 	case SKL_TKN_U32_FMT_SAMPLE_TYPE:
3439db6ed55dSShreyas NC 	case SKL_TKN_U32_FMT_CH_MAP:
3440db6ed55dSShreyas NC 	case SKL_TKN_MM_U32_INTF_PIN_ID:
3441db6ed55dSShreyas NC 		ret = skl_tplg_manifest_fill_fmt(dev, fmt, tkn_elem,
3442db6ed55dSShreyas NC 						 dir, pin_idx);
3443db6ed55dSShreyas NC 		if (ret < 0)
3444db6ed55dSShreyas NC 			return ret;
3445541070ceSShreyas NC 		break;
3446541070ceSShreyas NC 
3447541070ceSShreyas NC 	default:
3448ecd286a9SColin Ian King 		dev_err(dev, "Not a manifest token %d\n", tkn_elem->token);
3449541070ceSShreyas NC 		return -EINVAL;
3450541070ceSShreyas NC 	}
3451db6ed55dSShreyas NC 	tkn_count++;
3452541070ceSShreyas NC 
3453541070ceSShreyas NC 	return tkn_count;
3454541070ceSShreyas NC }
3455541070ceSShreyas NC 
3456541070ceSShreyas NC /*
3457541070ceSShreyas NC  * Fill the manifest structure by parsing the tokens based on the
3458541070ceSShreyas NC  * type.
3459541070ceSShreyas NC  */
3460541070ceSShreyas NC static int skl_tplg_get_manifest_tkn(struct device *dev,
3461bcc2a2dcSCezary Rojewski 		char *pvt_data, struct skl_dev *skl,
3462541070ceSShreyas NC 		int block_size)
3463541070ceSShreyas NC {
3464541070ceSShreyas NC 	int tkn_count = 0, ret;
3465541070ceSShreyas NC 	int off = 0, tuple_size = 0;
3466096769eaSAmadeusz Sławiński 	u8 uuid_index = 0;
3467541070ceSShreyas NC 	struct snd_soc_tplg_vendor_array *array;
3468541070ceSShreyas NC 	struct snd_soc_tplg_vendor_value_elem *tkn_elem;
3469541070ceSShreyas NC 
3470541070ceSShreyas NC 	if (block_size <= 0)
3471541070ceSShreyas NC 		return -EINVAL;
3472541070ceSShreyas NC 
3473541070ceSShreyas NC 	while (tuple_size < block_size) {
3474541070ceSShreyas NC 		array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off);
3475541070ceSShreyas NC 		off += array->size;
3476541070ceSShreyas NC 		switch (array->type) {
3477541070ceSShreyas NC 		case SND_SOC_TPLG_TUPLE_TYPE_STRING:
3478eee0e16fSJeeja KP 			ret = skl_tplg_get_str_tkn(dev, array, skl);
3479541070ceSShreyas NC 
3480541070ceSShreyas NC 			if (ret < 0)
3481541070ceSShreyas NC 				return ret;
34820a716776SShreyas NC 			tkn_count = ret;
3483541070ceSShreyas NC 
3484541070ceSShreyas NC 			tuple_size += tkn_count *
3485541070ceSShreyas NC 				sizeof(struct snd_soc_tplg_vendor_string_elem);
3486541070ceSShreyas NC 			continue;
3487541070ceSShreyas NC 
3488541070ceSShreyas NC 		case SND_SOC_TPLG_TUPLE_TYPE_UUID:
3489096769eaSAmadeusz Sławiński 			if (array->uuid->token != SKL_TKN_UUID) {
3490096769eaSAmadeusz Sławiński 				dev_err(dev, "Not an UUID token: %d\n",
3491096769eaSAmadeusz Sławiński 					array->uuid->token);
3492096769eaSAmadeusz Sławiński 				return -EINVAL;
3493096769eaSAmadeusz Sławiński 			}
3494096769eaSAmadeusz Sławiński 			if (uuid_index >= skl->nr_modules) {
3495096769eaSAmadeusz Sławiński 				dev_err(dev, "Too many UUID tokens\n");
3496096769eaSAmadeusz Sławiński 				return -EINVAL;
3497096769eaSAmadeusz Sławiński 			}
3498cade2f59SAndy Shevchenko 			import_guid(&skl->modules[uuid_index++]->uuid,
3499cade2f59SAndy Shevchenko 				    array->uuid->uuid);
3500db6ed55dSShreyas NC 
3501db6ed55dSShreyas NC 			tuple_size += sizeof(*array->uuid);
3502541070ceSShreyas NC 			continue;
3503541070ceSShreyas NC 
3504541070ceSShreyas NC 		default:
3505541070ceSShreyas NC 			tkn_elem = array->value;
3506541070ceSShreyas NC 			tkn_count = 0;
3507541070ceSShreyas NC 			break;
3508541070ceSShreyas NC 		}
3509541070ceSShreyas NC 
3510541070ceSShreyas NC 		while (tkn_count <= array->num_elems - 1) {
3511541070ceSShreyas NC 			ret = skl_tplg_get_int_tkn(dev,
3512eee0e16fSJeeja KP 					tkn_elem, skl);
3513541070ceSShreyas NC 			if (ret < 0)
3514541070ceSShreyas NC 				return ret;
3515541070ceSShreyas NC 
3516541070ceSShreyas NC 			tkn_count = tkn_count + ret;
3517541070ceSShreyas NC 			tkn_elem++;
3518541070ceSShreyas NC 		}
35199fc129f6SShreyas NC 		tuple_size += (tkn_count * sizeof(*tkn_elem));
3520541070ceSShreyas NC 		tkn_count = 0;
3521541070ceSShreyas NC 	}
3522541070ceSShreyas NC 
35239fc129f6SShreyas NC 	return off;
3524541070ceSShreyas NC }
3525541070ceSShreyas NC 
3526541070ceSShreyas NC /*
3527541070ceSShreyas NC  * Parse manifest private data for tokens. The private data block is
3528541070ceSShreyas NC  * preceded by descriptors for type and size of data block.
3529541070ceSShreyas NC  */
3530541070ceSShreyas NC static int skl_tplg_get_manifest_data(struct snd_soc_tplg_manifest *manifest,
3531bcc2a2dcSCezary Rojewski 			struct device *dev, struct skl_dev *skl)
3532541070ceSShreyas NC {
3533541070ceSShreyas NC 	struct snd_soc_tplg_vendor_array *array;
3534541070ceSShreyas NC 	int num_blocks, block_size = 0, block_type, off = 0;
3535541070ceSShreyas NC 	char *data;
3536541070ceSShreyas NC 	int ret;
3537541070ceSShreyas NC 
3538541070ceSShreyas NC 	/* Read the NUM_DATA_BLOCKS descriptor */
3539541070ceSShreyas NC 	array = (struct snd_soc_tplg_vendor_array *)manifest->priv.data;
3540541070ceSShreyas NC 	ret = skl_tplg_get_desc_blocks(dev, array);
3541541070ceSShreyas NC 	if (ret < 0)
3542541070ceSShreyas NC 		return ret;
3543541070ceSShreyas NC 	num_blocks = ret;
3544541070ceSShreyas NC 
3545541070ceSShreyas NC 	off += array->size;
3546541070ceSShreyas NC 	/* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */
3547541070ceSShreyas NC 	while (num_blocks > 0) {
35489fc129f6SShreyas NC 		array = (struct snd_soc_tplg_vendor_array *)
35499fc129f6SShreyas NC 				(manifest->priv.data + off);
3550541070ceSShreyas NC 		ret = skl_tplg_get_desc_blocks(dev, array);
3551541070ceSShreyas NC 
3552541070ceSShreyas NC 		if (ret < 0)
3553541070ceSShreyas NC 			return ret;
3554541070ceSShreyas NC 		block_type = ret;
3555541070ceSShreyas NC 		off += array->size;
3556541070ceSShreyas NC 
3557541070ceSShreyas NC 		array = (struct snd_soc_tplg_vendor_array *)
3558541070ceSShreyas NC 			(manifest->priv.data + off);
3559541070ceSShreyas NC 
3560541070ceSShreyas NC 		ret = skl_tplg_get_desc_blocks(dev, array);
3561541070ceSShreyas NC 
3562541070ceSShreyas NC 		if (ret < 0)
3563541070ceSShreyas NC 			return ret;
3564541070ceSShreyas NC 		block_size = ret;
3565541070ceSShreyas NC 		off += array->size;
3566541070ceSShreyas NC 
3567541070ceSShreyas NC 		array = (struct snd_soc_tplg_vendor_array *)
3568541070ceSShreyas NC 			(manifest->priv.data + off);
3569541070ceSShreyas NC 
3570541070ceSShreyas NC 		data = (manifest->priv.data + off);
3571541070ceSShreyas NC 
3572541070ceSShreyas NC 		if (block_type == SKL_TYPE_TUPLE) {
3573eee0e16fSJeeja KP 			ret = skl_tplg_get_manifest_tkn(dev, data, skl,
3574541070ceSShreyas NC 					block_size);
3575541070ceSShreyas NC 
3576541070ceSShreyas NC 			if (ret < 0)
3577541070ceSShreyas NC 				return ret;
3578541070ceSShreyas NC 
3579541070ceSShreyas NC 			--num_blocks;
3580541070ceSShreyas NC 		} else {
3581541070ceSShreyas NC 			return -EINVAL;
3582541070ceSShreyas NC 		}
35839fc129f6SShreyas NC 		off += ret;
3584541070ceSShreyas NC 	}
3585541070ceSShreyas NC 
3586541070ceSShreyas NC 	return 0;
3587541070ceSShreyas NC }
3588541070ceSShreyas NC 
3589c60b613aSLiam Girdwood static int skl_manifest_load(struct snd_soc_component *cmpnt, int index,
359015ecaba9SKranthi G 				struct snd_soc_tplg_manifest *manifest)
359115ecaba9SKranthi G {
359276f56faeSRakesh Ughreja 	struct hdac_bus *bus = snd_soc_component_get_drvdata(cmpnt);
3593bcc2a2dcSCezary Rojewski 	struct skl_dev *skl = bus_to_skl(bus);
359415ecaba9SKranthi G 
3595c15ad605SVinod Koul 	/* proceed only if we have private data defined */
3596c15ad605SVinod Koul 	if (manifest->priv.size == 0)
3597c15ad605SVinod Koul 		return 0;
3598c15ad605SVinod Koul 
3599eee0e16fSJeeja KP 	skl_tplg_get_manifest_data(manifest, bus->dev, skl);
3600541070ceSShreyas NC 
3601bcc2a2dcSCezary Rojewski 	if (skl->lib_count > SKL_MAX_LIB) {
360215ecaba9SKranthi G 		dev_err(bus->dev, "Exceeding max Library count. Got:%d\n",
3603bcc2a2dcSCezary Rojewski 					skl->lib_count);
3604eee0e16fSJeeja KP 		return  -EINVAL;
360515ecaba9SKranthi G 	}
360615ecaba9SKranthi G 
3607eee0e16fSJeeja KP 	return 0;
360815ecaba9SKranthi G }
360915ecaba9SKranthi G 
36102d744ecfSMateusz Gorski static void skl_tplg_complete(struct snd_soc_component *component)
36112d744ecfSMateusz Gorski {
36122d744ecfSMateusz Gorski 	struct snd_soc_dobj *dobj;
3613bef2897dSNick Desaulniers 	struct snd_soc_acpi_mach *mach;
3614bef2897dSNick Desaulniers 	struct snd_ctl_elem_value *val;
36152d744ecfSMateusz Gorski 	int i;
36162d744ecfSMateusz Gorski 
3617bef2897dSNick Desaulniers 	val = kmalloc(sizeof(*val), GFP_KERNEL);
3618bef2897dSNick Desaulniers 	if (!val)
3619bef2897dSNick Desaulniers 		return;
3620bef2897dSNick Desaulniers 
3621bef2897dSNick Desaulniers 	mach = dev_get_platdata(component->card->dev);
36222d744ecfSMateusz Gorski 	list_for_each_entry(dobj, &component->dobj_list, list) {
36232d744ecfSMateusz Gorski 		struct snd_kcontrol *kcontrol = dobj->control.kcontrol;
3624c1c3ba1fSRicardo Ribalda 		struct soc_enum *se;
3625c1c3ba1fSRicardo Ribalda 		char **texts;
36262d744ecfSMateusz Gorski 		char chan_text[4];
36272d744ecfSMateusz Gorski 
3628c1c3ba1fSRicardo Ribalda 		if (dobj->type != SND_SOC_DOBJ_ENUM || !kcontrol ||
3629c1c3ba1fSRicardo Ribalda 		    kcontrol->put != skl_tplg_multi_config_set_dmic)
36302d744ecfSMateusz Gorski 			continue;
3631c1c3ba1fSRicardo Ribalda 
3632c1c3ba1fSRicardo Ribalda 		se = (struct soc_enum *)kcontrol->private_value;
3633c1c3ba1fSRicardo Ribalda 		texts = dobj->control.dtexts;
36342d744ecfSMateusz Gorski 		sprintf(chan_text, "c%d", mach->mach_params.dmic_num);
36352d744ecfSMateusz Gorski 
36362d744ecfSMateusz Gorski 		for (i = 0; i < se->items; i++) {
36372d744ecfSMateusz Gorski 			if (strstr(texts[i], chan_text)) {
3638bef2897dSNick Desaulniers 				memset(val, 0, sizeof(*val));
3639bef2897dSNick Desaulniers 				val->value.enumerated.item[0] = i;
3640bef2897dSNick Desaulniers 				kcontrol->put(kcontrol, val);
36412d744ecfSMateusz Gorski 			}
36422d744ecfSMateusz Gorski 		}
36432d744ecfSMateusz Gorski 	}
3644bef2897dSNick Desaulniers 	kfree(val);
36452d744ecfSMateusz Gorski }
36462d744ecfSMateusz Gorski 
36473af36706SVinod Koul static struct snd_soc_tplg_ops skl_tplg_ops  = {
36483af36706SVinod Koul 	.widget_load = skl_tplg_widget_load,
3649140adfbaSJeeja KP 	.control_load = skl_tplg_control_load,
3650140adfbaSJeeja KP 	.bytes_ext_ops = skl_tlv_ops,
3651140adfbaSJeeja KP 	.bytes_ext_ops_count = ARRAY_SIZE(skl_tlv_ops),
36527a1b749bSDharageswari R 	.io_ops = skl_tplg_kcontrol_ops,
36537a1b749bSDharageswari R 	.io_ops_count = ARRAY_SIZE(skl_tplg_kcontrol_ops),
365415ecaba9SKranthi G 	.manifest = skl_manifest_load,
3655606e21fdSGuneshwor Singh 	.dai_load = skl_dai_load,
36562d744ecfSMateusz Gorski 	.complete = skl_tplg_complete,
36573af36706SVinod Koul };
36583af36706SVinod Koul 
3659287af4f9SJeeja KP /*
3660287af4f9SJeeja KP  * A pipe can have multiple modules, each of them will be a DAPM widget as
3661287af4f9SJeeja KP  * well. While managing a pipeline we need to get the list of all the
3662287af4f9SJeeja KP  * widgets in a pipelines, so this helper - skl_tplg_create_pipe_widget_list()
3663287af4f9SJeeja KP  * helps to get the SKL type widgets in that pipeline
3664287af4f9SJeeja KP  */
366556b03b4cSKuninori Morimoto static int skl_tplg_create_pipe_widget_list(struct snd_soc_component *component)
3666287af4f9SJeeja KP {
3667287af4f9SJeeja KP 	struct snd_soc_dapm_widget *w;
3668287af4f9SJeeja KP 	struct skl_module_cfg *mcfg = NULL;
3669287af4f9SJeeja KP 	struct skl_pipe_module *p_module = NULL;
3670287af4f9SJeeja KP 	struct skl_pipe *pipe;
3671287af4f9SJeeja KP 
367256b03b4cSKuninori Morimoto 	list_for_each_entry(w, &component->card->widgets, list) {
3673a1f362d8SMark Brown 		if (is_skl_dsp_widget_type(w, component->dev) && w->priv) {
3674287af4f9SJeeja KP 			mcfg = w->priv;
3675287af4f9SJeeja KP 			pipe = mcfg->pipe;
3676287af4f9SJeeja KP 
367756b03b4cSKuninori Morimoto 			p_module = devm_kzalloc(component->dev,
3678287af4f9SJeeja KP 						sizeof(*p_module), GFP_KERNEL);
3679287af4f9SJeeja KP 			if (!p_module)
3680287af4f9SJeeja KP 				return -ENOMEM;
3681287af4f9SJeeja KP 
3682287af4f9SJeeja KP 			p_module->w = w;
3683287af4f9SJeeja KP 			list_add_tail(&p_module->node, &pipe->w_list);
3684287af4f9SJeeja KP 		}
3685287af4f9SJeeja KP 	}
3686287af4f9SJeeja KP 
3687287af4f9SJeeja KP 	return 0;
3688287af4f9SJeeja KP }
3689287af4f9SJeeja KP 
3690bcc2a2dcSCezary Rojewski static void skl_tplg_set_pipe_type(struct skl_dev *skl, struct skl_pipe *pipe)
3691f0aa94faSJeeja KP {
3692f0aa94faSJeeja KP 	struct skl_pipe_module *w_module;
3693f0aa94faSJeeja KP 	struct snd_soc_dapm_widget *w;
3694f0aa94faSJeeja KP 	struct skl_module_cfg *mconfig;
3695f0aa94faSJeeja KP 	bool host_found = false, link_found = false;
3696f0aa94faSJeeja KP 
3697f0aa94faSJeeja KP 	list_for_each_entry(w_module, &pipe->w_list, node) {
3698f0aa94faSJeeja KP 		w = w_module->w;
3699f0aa94faSJeeja KP 		mconfig = w->priv;
3700f0aa94faSJeeja KP 
3701f0aa94faSJeeja KP 		if (mconfig->dev_type == SKL_DEVICE_HDAHOST)
3702f0aa94faSJeeja KP 			host_found = true;
3703f0aa94faSJeeja KP 		else if (mconfig->dev_type != SKL_DEVICE_NONE)
3704f0aa94faSJeeja KP 			link_found = true;
3705f0aa94faSJeeja KP 	}
3706f0aa94faSJeeja KP 
3707f0aa94faSJeeja KP 	if (host_found && link_found)
3708f0aa94faSJeeja KP 		pipe->passthru = true;
3709f0aa94faSJeeja KP 	else
3710f0aa94faSJeeja KP 		pipe->passthru = false;
3711f0aa94faSJeeja KP }
3712f0aa94faSJeeja KP 
37133af36706SVinod Koul /*
37143af36706SVinod Koul  * SKL topology init routine
37153af36706SVinod Koul  */
371676f56faeSRakesh Ughreja int skl_tplg_init(struct snd_soc_component *component, struct hdac_bus *bus)
37173af36706SVinod Koul {
37183af36706SVinod Koul 	int ret;
37193af36706SVinod Koul 	const struct firmware *fw;
3720bcc2a2dcSCezary Rojewski 	struct skl_dev *skl = bus_to_skl(bus);
3721f0aa94faSJeeja KP 	struct skl_pipeline *ppl;
37223af36706SVinod Koul 
37234b235c43SVinod Koul 	ret = request_firmware(&fw, skl->tplg_name, bus->dev);
37243af36706SVinod Koul 	if (ret < 0) {
37251b290ef0SMateusz Gorski 		char alt_tplg_name[64];
37261b290ef0SMateusz Gorski 
37271b290ef0SMateusz Gorski 		snprintf(alt_tplg_name, sizeof(alt_tplg_name), "%s-tplg.bin",
37281b290ef0SMateusz Gorski 			 skl->mach->drv_name);
37291b290ef0SMateusz Gorski 		dev_info(bus->dev, "tplg fw %s load failed with %d, trying alternative tplg name %s",
37301b290ef0SMateusz Gorski 			 skl->tplg_name, ret, alt_tplg_name);
37311b290ef0SMateusz Gorski 
37321b290ef0SMateusz Gorski 		ret = request_firmware(&fw, alt_tplg_name, bus->dev);
37331b290ef0SMateusz Gorski 		if (!ret)
37341b290ef0SMateusz Gorski 			goto component_load;
37351b290ef0SMateusz Gorski 
37361b290ef0SMateusz Gorski 		dev_info(bus->dev, "tplg %s failed with %d, falling back to dfw_sst.bin",
37371b290ef0SMateusz Gorski 			 alt_tplg_name, ret);
37381b290ef0SMateusz Gorski 
37394b235c43SVinod Koul 		ret = request_firmware(&fw, "dfw_sst.bin", bus->dev);
37404b235c43SVinod Koul 		if (ret < 0) {
37414b235c43SVinod Koul 			dev_err(bus->dev, "Fallback tplg fw %s load failed with %d\n",
37423af36706SVinod Koul 					"dfw_sst.bin", ret);
37433af36706SVinod Koul 			return ret;
37443af36706SVinod Koul 		}
37454b235c43SVinod Koul 	}
37463af36706SVinod Koul 
37471b290ef0SMateusz Gorski component_load:
3748a5b8f71cSAmadeusz Sławiński 	ret = snd_soc_tplg_component_load(component, &skl_tplg_ops, fw);
37493af36706SVinod Koul 	if (ret < 0) {
37503af36706SVinod Koul 		dev_err(bus->dev, "tplg component load failed%d\n", ret);
37516f437917SAmadeusz Sławiński 		goto err;
37523af36706SVinod Koul 	}
37533af36706SVinod Koul 
375456b03b4cSKuninori Morimoto 	ret = skl_tplg_create_pipe_widget_list(component);
37556f437917SAmadeusz Sławiński 	if (ret < 0) {
37566f437917SAmadeusz Sławiński 		dev_err(bus->dev, "tplg create pipe widget list failed%d\n",
37576f437917SAmadeusz Sławiński 				ret);
37586f437917SAmadeusz Sławiński 		goto err;
37596f437917SAmadeusz Sławiński 	}
3760d8018361SVinod Koul 
3761f0aa94faSJeeja KP 	list_for_each_entry(ppl, &skl->ppl_list, node)
3762f0aa94faSJeeja KP 		skl_tplg_set_pipe_type(skl, ppl->pipe);
37633af36706SVinod Koul 
37646f437917SAmadeusz Sławiński err:
37656f437917SAmadeusz Sławiński 	release_firmware(fw);
37666f437917SAmadeusz Sławiński 	return ret;
37673af36706SVinod Koul }
3768e79986ceSAmadeusz Sławiński 
3769e79986ceSAmadeusz Sławiński void skl_tplg_exit(struct snd_soc_component *component, struct hdac_bus *bus)
3770e79986ceSAmadeusz Sławiński {
3771bcc2a2dcSCezary Rojewski 	struct skl_dev *skl = bus_to_skl(bus);
3772e79986ceSAmadeusz Sławiński 	struct skl_pipeline *ppl, *tmp;
3773e79986ceSAmadeusz Sławiński 
3774e79986ceSAmadeusz Sławiński 	list_for_each_entry_safe(ppl, tmp, &skl->ppl_list, node)
3775e79986ceSAmadeusz Sławiński 		list_del(&ppl->node);
3776e79986ceSAmadeusz Sławiński 
3777e79986ceSAmadeusz Sławiński 	/* clean up topology */
3778a5b8f71cSAmadeusz Sławiński 	snd_soc_tplg_component_remove(component);
3779e79986ceSAmadeusz Sławiński }
3780