1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 *  skl-topology.c - Implements Platform component ALSA controls/widget
4 *  handlers.
5 *
6 *  Copyright (C) 2014-2015 Intel Corp
7 *  Author: Jeeja KP <jeeja.kp@intel.com>
8 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 */
10
11#include <linux/slab.h>
12#include <linux/types.h>
13#include <linux/firmware.h>
14#include <linux/uuid.h>
15#include <sound/intel-nhlt.h>
16#include <sound/soc.h>
17#include <sound/soc-acpi.h>
18#include <sound/soc-topology.h>
19#include <uapi/sound/snd_sst_tokens.h>
20#include <uapi/sound/skl-tplg-interface.h>
21#include "skl-sst-dsp.h"
22#include "skl-sst-ipc.h"
23#include "skl-topology.h"
24#include "skl.h"
25#include "../common/sst-dsp.h"
26#include "../common/sst-dsp-priv.h"
27
28#define SKL_CH_FIXUP_MASK		(1 << 0)
29#define SKL_RATE_FIXUP_MASK		(1 << 1)
30#define SKL_FMT_FIXUP_MASK		(1 << 2)
31#define SKL_IN_DIR_BIT_MASK		BIT(0)
32#define SKL_PIN_COUNT_MASK		GENMASK(7, 4)
33
34static const int mic_mono_list[] = {
350, 1, 2, 3,
36};
37static const int mic_stereo_list[][SKL_CH_STEREO] = {
38{0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 3}, {2, 3},
39};
40static const int mic_trio_list[][SKL_CH_TRIO] = {
41{0, 1, 2}, {0, 1, 3}, {0, 2, 3}, {1, 2, 3},
42};
43static const int mic_quatro_list[][SKL_CH_QUATRO] = {
44{0, 1, 2, 3},
45};
46
47#define CHECK_HW_PARAMS(ch, freq, bps, prm_ch, prm_freq, prm_bps) \
48	((ch == prm_ch) && (bps == prm_bps) && (freq == prm_freq))
49
50void skl_tplg_d0i3_get(struct skl_dev *skl, enum d0i3_capability caps)
51{
52	struct skl_d0i3_data *d0i3 =  &skl->d0i3;
53
54	switch (caps) {
55	case SKL_D0I3_NONE:
56		d0i3->non_d0i3++;
57		break;
58
59	case SKL_D0I3_STREAMING:
60		d0i3->streaming++;
61		break;
62
63	case SKL_D0I3_NON_STREAMING:
64		d0i3->non_streaming++;
65		break;
66	}
67}
68
69void skl_tplg_d0i3_put(struct skl_dev *skl, enum d0i3_capability caps)
70{
71	struct skl_d0i3_data *d0i3 =  &skl->d0i3;
72
73	switch (caps) {
74	case SKL_D0I3_NONE:
75		d0i3->non_d0i3--;
76		break;
77
78	case SKL_D0I3_STREAMING:
79		d0i3->streaming--;
80		break;
81
82	case SKL_D0I3_NON_STREAMING:
83		d0i3->non_streaming--;
84		break;
85	}
86}
87
88/*
89 * SKL DSP driver modelling uses only few DAPM widgets so for rest we will
90 * ignore. This helpers checks if the SKL driver handles this widget type
91 */
92static int is_skl_dsp_widget_type(struct snd_soc_dapm_widget *w,
93				  struct device *dev)
94{
95	if (w->dapm->dev != dev)
96		return false;
97
98	switch (w->id) {
99	case snd_soc_dapm_dai_link:
100	case snd_soc_dapm_dai_in:
101	case snd_soc_dapm_aif_in:
102	case snd_soc_dapm_aif_out:
103	case snd_soc_dapm_dai_out:
104	case snd_soc_dapm_switch:
105	case snd_soc_dapm_output:
106	case snd_soc_dapm_mux:
107
108		return false;
109	default:
110		return true;
111	}
112}
113
114static void skl_dump_mconfig(struct skl_dev *skl, struct skl_module_cfg *mcfg)
115{
116	struct skl_module_iface *iface = &mcfg->module->formats[mcfg->fmt_idx];
117
118	dev_dbg(skl->dev, "Dumping config\n");
119	dev_dbg(skl->dev, "Input Format:\n");
120	dev_dbg(skl->dev, "channels = %d\n", iface->inputs[0].fmt.channels);
121	dev_dbg(skl->dev, "s_freq = %d\n", iface->inputs[0].fmt.s_freq);
122	dev_dbg(skl->dev, "ch_cfg = %d\n", iface->inputs[0].fmt.ch_cfg);
123	dev_dbg(skl->dev, "valid bit depth = %d\n",
124				iface->inputs[0].fmt.valid_bit_depth);
125	dev_dbg(skl->dev, "Output Format:\n");
126	dev_dbg(skl->dev, "channels = %d\n", iface->outputs[0].fmt.channels);
127	dev_dbg(skl->dev, "s_freq = %d\n", iface->outputs[0].fmt.s_freq);
128	dev_dbg(skl->dev, "valid bit depth = %d\n",
129				iface->outputs[0].fmt.valid_bit_depth);
130	dev_dbg(skl->dev, "ch_cfg = %d\n", iface->outputs[0].fmt.ch_cfg);
131}
132
133static void skl_tplg_update_chmap(struct skl_module_fmt *fmt, int chs)
134{
135	int slot_map = 0xFFFFFFFF;
136	int start_slot = 0;
137	int i;
138
139	for (i = 0; i < chs; i++) {
140		/*
141		 * For 2 channels with starting slot as 0, slot map will
142		 * look like 0xFFFFFF10.
143		 */
144		slot_map &= (~(0xF << (4 * i)) | (start_slot << (4 * i)));
145		start_slot++;
146	}
147	fmt->ch_map = slot_map;
148}
149
150static void skl_tplg_update_params(struct skl_module_fmt *fmt,
151			struct skl_pipe_params *params, int fixup)
152{
153	if (fixup & SKL_RATE_FIXUP_MASK)
154		fmt->s_freq = params->s_freq;
155	if (fixup & SKL_CH_FIXUP_MASK) {
156		fmt->channels = params->ch;
157		skl_tplg_update_chmap(fmt, fmt->channels);
158	}
159	if (fixup & SKL_FMT_FIXUP_MASK) {
160		fmt->valid_bit_depth = skl_get_bit_depth(params->s_fmt);
161
162		/*
163		 * 16 bit is 16 bit container whereas 24 bit is in 32 bit
164		 * container so update bit depth accordingly
165		 */
166		switch (fmt->valid_bit_depth) {
167		case SKL_DEPTH_16BIT:
168			fmt->bit_depth = fmt->valid_bit_depth;
169			break;
170
171		default:
172			fmt->bit_depth = SKL_DEPTH_32BIT;
173			break;
174		}
175	}
176
177}
178
179/*
180 * A pipeline may have modules which impact the pcm parameters, like SRC,
181 * channel converter, format converter.
182 * We need to calculate the output params by applying the 'fixup'
183 * Topology will tell driver which type of fixup is to be applied by
184 * supplying the fixup mask, so based on that we calculate the output
185 *
186 * Now In FE the pcm hw_params is source/target format. Same is applicable
187 * for BE with its hw_params invoked.
188 * here based on FE, BE pipeline and direction we calculate the input and
189 * outfix and then apply that for a module
190 */
191static void skl_tplg_update_params_fixup(struct skl_module_cfg *m_cfg,
192		struct skl_pipe_params *params, bool is_fe)
193{
194	int in_fixup, out_fixup;
195	struct skl_module_fmt *in_fmt, *out_fmt;
196
197	/* Fixups will be applied to pin 0 only */
198	in_fmt = &m_cfg->module->formats[m_cfg->fmt_idx].inputs[0].fmt;
199	out_fmt = &m_cfg->module->formats[m_cfg->fmt_idx].outputs[0].fmt;
200
201	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
202		if (is_fe) {
203			in_fixup = m_cfg->params_fixup;
204			out_fixup = (~m_cfg->converter) &
205					m_cfg->params_fixup;
206		} else {
207			out_fixup = m_cfg->params_fixup;
208			in_fixup = (~m_cfg->converter) &
209					m_cfg->params_fixup;
210		}
211	} else {
212		if (is_fe) {
213			out_fixup = m_cfg->params_fixup;
214			in_fixup = (~m_cfg->converter) &
215					m_cfg->params_fixup;
216		} else {
217			in_fixup = m_cfg->params_fixup;
218			out_fixup = (~m_cfg->converter) &
219					m_cfg->params_fixup;
220		}
221	}
222
223	skl_tplg_update_params(in_fmt, params, in_fixup);
224	skl_tplg_update_params(out_fmt, params, out_fixup);
225}
226
227/*
228 * A module needs input and output buffers, which are dependent upon pcm
229 * params, so once we have calculate params, we need buffer calculation as
230 * well.
231 */
232static void skl_tplg_update_buffer_size(struct skl_dev *skl,
233				struct skl_module_cfg *mcfg)
234{
235	int multiplier = 1;
236	struct skl_module_fmt *in_fmt, *out_fmt;
237	struct skl_module_res *res;
238
239	/* Since fixups is applied to pin 0 only, ibs, obs needs
240	 * change for pin 0 only
241	 */
242	res = &mcfg->module->resources[mcfg->res_idx];
243	in_fmt = &mcfg->module->formats[mcfg->fmt_idx].inputs[0].fmt;
244	out_fmt = &mcfg->module->formats[mcfg->fmt_idx].outputs[0].fmt;
245
246	if (mcfg->m_type == SKL_MODULE_TYPE_SRCINT)
247		multiplier = 5;
248
249	res->ibs = DIV_ROUND_UP(in_fmt->s_freq, 1000) *
250			in_fmt->channels * (in_fmt->bit_depth >> 3) *
251			multiplier;
252
253	res->obs = DIV_ROUND_UP(out_fmt->s_freq, 1000) *
254			out_fmt->channels * (out_fmt->bit_depth >> 3) *
255			multiplier;
256}
257
258static u8 skl_tplg_be_dev_type(int dev_type)
259{
260	int ret;
261
262	switch (dev_type) {
263	case SKL_DEVICE_BT:
264		ret = NHLT_DEVICE_BT;
265		break;
266
267	case SKL_DEVICE_DMIC:
268		ret = NHLT_DEVICE_DMIC;
269		break;
270
271	case SKL_DEVICE_I2S:
272		ret = NHLT_DEVICE_I2S;
273		break;
274
275	default:
276		ret = NHLT_DEVICE_INVALID;
277		break;
278	}
279
280	return ret;
281}
282
283static int skl_tplg_update_be_blob(struct snd_soc_dapm_widget *w,
284						struct skl_dev *skl)
285{
286	struct skl_module_cfg *m_cfg = w->priv;
287	int link_type, dir;
288	u32 ch, s_freq, s_fmt;
289	struct nhlt_specific_cfg *cfg;
290	u8 dev_type = skl_tplg_be_dev_type(m_cfg->dev_type);
291	int fmt_idx = m_cfg->fmt_idx;
292	struct skl_module_iface *m_iface = &m_cfg->module->formats[fmt_idx];
293
294	/* check if we already have blob */
295	if (m_cfg->formats_config.caps_size > 0)
296		return 0;
297
298	dev_dbg(skl->dev, "Applying default cfg blob\n");
299	switch (m_cfg->dev_type) {
300	case SKL_DEVICE_DMIC:
301		link_type = NHLT_LINK_DMIC;
302		dir = SNDRV_PCM_STREAM_CAPTURE;
303		s_freq = m_iface->inputs[0].fmt.s_freq;
304		s_fmt = m_iface->inputs[0].fmt.bit_depth;
305		ch = m_iface->inputs[0].fmt.channels;
306		break;
307
308	case SKL_DEVICE_I2S:
309		link_type = NHLT_LINK_SSP;
310		if (m_cfg->hw_conn_type == SKL_CONN_SOURCE) {
311			dir = SNDRV_PCM_STREAM_PLAYBACK;
312			s_freq = m_iface->outputs[0].fmt.s_freq;
313			s_fmt = m_iface->outputs[0].fmt.bit_depth;
314			ch = m_iface->outputs[0].fmt.channels;
315		} else {
316			dir = SNDRV_PCM_STREAM_CAPTURE;
317			s_freq = m_iface->inputs[0].fmt.s_freq;
318			s_fmt = m_iface->inputs[0].fmt.bit_depth;
319			ch = m_iface->inputs[0].fmt.channels;
320		}
321		break;
322
323	default:
324		return -EINVAL;
325	}
326
327	/* update the blob based on virtual bus_id and default params */
328	cfg = skl_get_ep_blob(skl, m_cfg->vbus_id, link_type,
329					s_fmt, ch, s_freq, dir, dev_type);
330	if (cfg) {
331		m_cfg->formats_config.caps_size = cfg->size;
332		m_cfg->formats_config.caps = (u32 *) &cfg->caps;
333	} else {
334		dev_err(skl->dev, "Blob NULL for id %x type %d dirn %d\n",
335					m_cfg->vbus_id, link_type, dir);
336		dev_err(skl->dev, "PCM: ch %d, freq %d, fmt %d\n",
337					ch, s_freq, s_fmt);
338		return -EIO;
339	}
340
341	return 0;
342}
343
344static void skl_tplg_update_module_params(struct snd_soc_dapm_widget *w,
345							struct skl_dev *skl)
346{
347	struct skl_module_cfg *m_cfg = w->priv;
348	struct skl_pipe_params *params = m_cfg->pipe->p_params;
349	int p_conn_type = m_cfg->pipe->conn_type;
350	bool is_fe;
351
352	if (!m_cfg->params_fixup)
353		return;
354
355	dev_dbg(skl->dev, "Mconfig for widget=%s BEFORE updation\n",
356				w->name);
357
358	skl_dump_mconfig(skl, m_cfg);
359
360	if (p_conn_type == SKL_PIPE_CONN_TYPE_FE)
361		is_fe = true;
362	else
363		is_fe = false;
364
365	skl_tplg_update_params_fixup(m_cfg, params, is_fe);
366	skl_tplg_update_buffer_size(skl, m_cfg);
367
368	dev_dbg(skl->dev, "Mconfig for widget=%s AFTER updation\n",
369				w->name);
370
371	skl_dump_mconfig(skl, m_cfg);
372}
373
374/*
375 * some modules can have multiple params set from user control and
376 * need to be set after module is initialized. If set_param flag is
377 * set module params will be done after module is initialised.
378 */
379static int skl_tplg_set_module_params(struct snd_soc_dapm_widget *w,
380						struct skl_dev *skl)
381{
382	int i, ret;
383	struct skl_module_cfg *mconfig = w->priv;
384	const struct snd_kcontrol_new *k;
385	struct soc_bytes_ext *sb;
386	struct skl_algo_data *bc;
387	struct skl_specific_cfg *sp_cfg;
388
389	if (mconfig->formats_config.caps_size > 0 &&
390		mconfig->formats_config.set_params == SKL_PARAM_SET) {
391		sp_cfg = &mconfig->formats_config;
392		ret = skl_set_module_params(skl, sp_cfg->caps,
393					sp_cfg->caps_size,
394					sp_cfg->param_id, mconfig);
395		if (ret < 0)
396			return ret;
397	}
398
399	for (i = 0; i < w->num_kcontrols; i++) {
400		k = &w->kcontrol_news[i];
401		if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
402			sb = (void *) k->private_value;
403			bc = (struct skl_algo_data *)sb->dobj.private;
404
405			if (bc->set_params == SKL_PARAM_SET) {
406				ret = skl_set_module_params(skl,
407						(u32 *)bc->params, bc->size,
408						bc->param_id, mconfig);
409				if (ret < 0)
410					return ret;
411			}
412		}
413	}
414
415	return 0;
416}
417
418/*
419 * some module param can set from user control and this is required as
420 * when module is initailzed. if module param is required in init it is
421 * identifed by set_param flag. if set_param flag is not set, then this
422 * parameter needs to set as part of module init.
423 */
424static int skl_tplg_set_module_init_data(struct snd_soc_dapm_widget *w)
425{
426	const struct snd_kcontrol_new *k;
427	struct soc_bytes_ext *sb;
428	struct skl_algo_data *bc;
429	struct skl_module_cfg *mconfig = w->priv;
430	int i;
431
432	for (i = 0; i < w->num_kcontrols; i++) {
433		k = &w->kcontrol_news[i];
434		if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
435			sb = (struct soc_bytes_ext *)k->private_value;
436			bc = (struct skl_algo_data *)sb->dobj.private;
437
438			if (bc->set_params != SKL_PARAM_INIT)
439				continue;
440
441			mconfig->formats_config.caps = (u32 *)bc->params;
442			mconfig->formats_config.caps_size = bc->size;
443
444			break;
445		}
446	}
447
448	return 0;
449}
450
451static int skl_tplg_module_prepare(struct skl_dev *skl, struct skl_pipe *pipe,
452		struct snd_soc_dapm_widget *w, struct skl_module_cfg *mcfg)
453{
454	switch (mcfg->dev_type) {
455	case SKL_DEVICE_HDAHOST:
456		return skl_pcm_host_dma_prepare(skl->dev, pipe->p_params);
457
458	case SKL_DEVICE_HDALINK:
459		return skl_pcm_link_dma_prepare(skl->dev, pipe->p_params);
460	}
461
462	return 0;
463}
464
465/*
466 * Inside a pipe instance, we can have various modules. These modules need
467 * to instantiated in DSP by invoking INIT_MODULE IPC, which is achieved by
468 * skl_init_module() routine, so invoke that for all modules in a pipeline
469 */
470static int
471skl_tplg_init_pipe_modules(struct skl_dev *skl, struct skl_pipe *pipe)
472{
473	struct skl_pipe_module *w_module;
474	struct snd_soc_dapm_widget *w;
475	struct skl_module_cfg *mconfig;
476	u8 cfg_idx;
477	int ret = 0;
478
479	list_for_each_entry(w_module, &pipe->w_list, node) {
480		guid_t *uuid_mod;
481		w = w_module->w;
482		mconfig = w->priv;
483
484		/* check if module ids are populated */
485		if (mconfig->id.module_id < 0) {
486			dev_err(skl->dev,
487					"module %pUL id not populated\n",
488					(guid_t *)mconfig->guid);
489			return -EIO;
490		}
491
492		cfg_idx = mconfig->pipe->cur_config_idx;
493		mconfig->fmt_idx = mconfig->mod_cfg[cfg_idx].fmt_idx;
494		mconfig->res_idx = mconfig->mod_cfg[cfg_idx].res_idx;
495
496		if (mconfig->module->loadable && skl->dsp->fw_ops.load_mod) {
497			ret = skl->dsp->fw_ops.load_mod(skl->dsp,
498				mconfig->id.module_id, mconfig->guid);
499			if (ret < 0)
500				return ret;
501
502			mconfig->m_state = SKL_MODULE_LOADED;
503		}
504
505		/* prepare the DMA if the module is gateway cpr */
506		ret = skl_tplg_module_prepare(skl, pipe, w, mconfig);
507		if (ret < 0)
508			return ret;
509
510		/* update blob if blob is null for be with default value */
511		skl_tplg_update_be_blob(w, skl);
512
513		/*
514		 * apply fix/conversion to module params based on
515		 * FE/BE params
516		 */
517		skl_tplg_update_module_params(w, skl);
518		uuid_mod = (guid_t *)mconfig->guid;
519		mconfig->id.pvt_id = skl_get_pvt_id(skl, uuid_mod,
520						mconfig->id.instance_id);
521		if (mconfig->id.pvt_id < 0)
522			return ret;
523		skl_tplg_set_module_init_data(w);
524
525		ret = skl_dsp_get_core(skl->dsp, mconfig->core_id);
526		if (ret < 0) {
527			dev_err(skl->dev, "Failed to wake up core %d ret=%d\n",
528						mconfig->core_id, ret);
529			return ret;
530		}
531
532		ret = skl_init_module(skl, mconfig);
533		if (ret < 0) {
534			skl_put_pvt_id(skl, uuid_mod, &mconfig->id.pvt_id);
535			goto err;
536		}
537
538		ret = skl_tplg_set_module_params(w, skl);
539		if (ret < 0)
540			goto err;
541	}
542
543	return 0;
544err:
545	skl_dsp_put_core(skl->dsp, mconfig->core_id);
546	return ret;
547}
548
549static int skl_tplg_unload_pipe_modules(struct skl_dev *skl,
550	 struct skl_pipe *pipe)
551{
552	int ret = 0;
553	struct skl_pipe_module *w_module;
554	struct skl_module_cfg *mconfig;
555
556	list_for_each_entry(w_module, &pipe->w_list, node) {
557		guid_t *uuid_mod;
558		mconfig  = w_module->w->priv;
559		uuid_mod = (guid_t *)mconfig->guid;
560
561		if (mconfig->module->loadable && skl->dsp->fw_ops.unload_mod &&
562			mconfig->m_state > SKL_MODULE_UNINIT) {
563			ret = skl->dsp->fw_ops.unload_mod(skl->dsp,
564						mconfig->id.module_id);
565			if (ret < 0)
566				return -EIO;
567		}
568		skl_put_pvt_id(skl, uuid_mod, &mconfig->id.pvt_id);
569
570		ret = skl_dsp_put_core(skl->dsp, mconfig->core_id);
571		if (ret < 0) {
572			/* don't return; continue with other modules */
573			dev_err(skl->dev, "Failed to sleep core %d ret=%d\n",
574				mconfig->core_id, ret);
575		}
576	}
577
578	/* no modules to unload in this path, so return */
579	return ret;
580}
581
582static bool skl_tplg_is_multi_fmt(struct skl_dev *skl, struct skl_pipe *pipe)
583{
584	struct skl_pipe_fmt *cur_fmt;
585	struct skl_pipe_fmt *next_fmt;
586	int i;
587
588	if (pipe->nr_cfgs <= 1)
589		return false;
590
591	if (pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
592		return true;
593
594	for (i = 0; i < pipe->nr_cfgs - 1; i++) {
595		if (pipe->direction == SNDRV_PCM_STREAM_PLAYBACK) {
596			cur_fmt = &pipe->configs[i].out_fmt;
597			next_fmt = &pipe->configs[i + 1].out_fmt;
598		} else {
599			cur_fmt = &pipe->configs[i].in_fmt;
600			next_fmt = &pipe->configs[i + 1].in_fmt;
601		}
602
603		if (!CHECK_HW_PARAMS(cur_fmt->channels, cur_fmt->freq,
604				     cur_fmt->bps,
605				     next_fmt->channels,
606				     next_fmt->freq,
607				     next_fmt->bps))
608			return true;
609	}
610
611	return false;
612}
613
614/*
615 * Here, we select pipe format based on the pipe type and pipe
616 * direction to determine the current config index for the pipeline.
617 * The config index is then used to select proper module resources.
618 * Intermediate pipes currently have a fixed format hence we select the
619 * 0th configuratation by default for such pipes.
620 */
621static int
622skl_tplg_get_pipe_config(struct skl_dev *skl, struct skl_module_cfg *mconfig)
623{
624	struct skl_pipe *pipe = mconfig->pipe;
625	struct skl_pipe_params *params = pipe->p_params;
626	struct skl_path_config *pconfig = &pipe->configs[0];
627	struct skl_pipe_fmt *fmt = NULL;
628	bool in_fmt = false;
629	int i;
630
631	if (pipe->nr_cfgs == 0) {
632		pipe->cur_config_idx = 0;
633		return 0;
634	}
635
636	if (skl_tplg_is_multi_fmt(skl, pipe)) {
637		pipe->cur_config_idx = pipe->pipe_config_idx;
638		pipe->memory_pages = pconfig->mem_pages;
639		dev_dbg(skl->dev, "found pipe config idx:%d\n",
640			pipe->cur_config_idx);
641		return 0;
642	}
643
644	if (pipe->conn_type == SKL_PIPE_CONN_TYPE_NONE) {
645		dev_dbg(skl->dev, "No conn_type detected, take 0th config\n");
646		pipe->cur_config_idx = 0;
647		pipe->memory_pages = pconfig->mem_pages;
648
649		return 0;
650	}
651
652	if ((pipe->conn_type == SKL_PIPE_CONN_TYPE_FE &&
653	     pipe->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
654	     (pipe->conn_type == SKL_PIPE_CONN_TYPE_BE &&
655	     pipe->direction == SNDRV_PCM_STREAM_CAPTURE))
656		in_fmt = true;
657
658	for (i = 0; i < pipe->nr_cfgs; i++) {
659		pconfig = &pipe->configs[i];
660		if (in_fmt)
661			fmt = &pconfig->in_fmt;
662		else
663			fmt = &pconfig->out_fmt;
664
665		if (CHECK_HW_PARAMS(params->ch, params->s_freq, params->s_fmt,
666				    fmt->channels, fmt->freq, fmt->bps)) {
667			pipe->cur_config_idx = i;
668			pipe->memory_pages = pconfig->mem_pages;
669			dev_dbg(skl->dev, "Using pipe config: %d\n", i);
670
671			return 0;
672		}
673	}
674
675	dev_err(skl->dev, "Invalid pipe config: %d %d %d for pipe: %d\n",
676		params->ch, params->s_freq, params->s_fmt, pipe->ppl_id);
677	return -EINVAL;
678}
679
680/*
681 * Mixer module represents a pipeline. So in the Pre-PMU event of mixer we
682 * need create the pipeline. So we do following:
683 *   - Create the pipeline
684 *   - Initialize the modules in pipeline
685 *   - finally bind all modules together
686 */
687static int skl_tplg_mixer_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
688							struct skl_dev *skl)
689{
690	int ret;
691	struct skl_module_cfg *mconfig = w->priv;
692	struct skl_pipe_module *w_module;
693	struct skl_pipe *s_pipe = mconfig->pipe;
694	struct skl_module_cfg *src_module = NULL, *dst_module, *module;
695	struct skl_module_deferred_bind *modules;
696
697	ret = skl_tplg_get_pipe_config(skl, mconfig);
698	if (ret < 0)
699		return ret;
700
701	/*
702	 * Create a list of modules for pipe.
703	 * This list contains modules from source to sink
704	 */
705	ret = skl_create_pipeline(skl, mconfig->pipe);
706	if (ret < 0)
707		return ret;
708
709	/* Init all pipe modules from source to sink */
710	ret = skl_tplg_init_pipe_modules(skl, s_pipe);
711	if (ret < 0)
712		return ret;
713
714	/* Bind modules from source to sink */
715	list_for_each_entry(w_module, &s_pipe->w_list, node) {
716		dst_module = w_module->w->priv;
717
718		if (src_module == NULL) {
719			src_module = dst_module;
720			continue;
721		}
722
723		ret = skl_bind_modules(skl, src_module, dst_module);
724		if (ret < 0)
725			return ret;
726
727		src_module = dst_module;
728	}
729
730	/*
731	 * When the destination module is initialized, check for these modules
732	 * in deferred bind list. If found, bind them.
733	 */
734	list_for_each_entry(w_module, &s_pipe->w_list, node) {
735		if (list_empty(&skl->bind_list))
736			break;
737
738		list_for_each_entry(modules, &skl->bind_list, node) {
739			module = w_module->w->priv;
740			if (modules->dst == module)
741				skl_bind_modules(skl, modules->src,
742							modules->dst);
743		}
744	}
745
746	return 0;
747}
748
749static int skl_fill_sink_instance_id(struct skl_dev *skl, u32 *params,
750				int size, struct skl_module_cfg *mcfg)
751{
752	int i, pvt_id;
753
754	if (mcfg->m_type == SKL_MODULE_TYPE_KPB) {
755		struct skl_kpb_params *kpb_params =
756				(struct skl_kpb_params *)params;
757		struct skl_mod_inst_map *inst = kpb_params->u.map;
758
759		for (i = 0; i < kpb_params->num_modules; i++) {
760			pvt_id = skl_get_pvt_instance_id_map(skl, inst->mod_id,
761								inst->inst_id);
762			if (pvt_id < 0)
763				return -EINVAL;
764
765			inst->inst_id = pvt_id;
766			inst++;
767		}
768	}
769
770	return 0;
771}
772/*
773 * Some modules require params to be set after the module is bound to
774 * all pins connected.
775 *
776 * The module provider initializes set_param flag for such modules and we
777 * send params after binding
778 */
779static int skl_tplg_set_module_bind_params(struct snd_soc_dapm_widget *w,
780			struct skl_module_cfg *mcfg, struct skl_dev *skl)
781{
782	int i, ret;
783	struct skl_module_cfg *mconfig = w->priv;
784	const struct snd_kcontrol_new *k;
785	struct soc_bytes_ext *sb;
786	struct skl_algo_data *bc;
787	struct skl_specific_cfg *sp_cfg;
788	u32 *params;
789
790	/*
791	 * check all out/in pins are in bind state.
792	 * if so set the module param
793	 */
794	for (i = 0; i < mcfg->module->max_output_pins; i++) {
795		if (mcfg->m_out_pin[i].pin_state != SKL_PIN_BIND_DONE)
796			return 0;
797	}
798
799	for (i = 0; i < mcfg->module->max_input_pins; i++) {
800		if (mcfg->m_in_pin[i].pin_state != SKL_PIN_BIND_DONE)
801			return 0;
802	}
803
804	if (mconfig->formats_config.caps_size > 0 &&
805		mconfig->formats_config.set_params == SKL_PARAM_BIND) {
806		sp_cfg = &mconfig->formats_config;
807		ret = skl_set_module_params(skl, sp_cfg->caps,
808					sp_cfg->caps_size,
809					sp_cfg->param_id, mconfig);
810		if (ret < 0)
811			return ret;
812	}
813
814	for (i = 0; i < w->num_kcontrols; i++) {
815		k = &w->kcontrol_news[i];
816		if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
817			sb = (void *) k->private_value;
818			bc = (struct skl_algo_data *)sb->dobj.private;
819
820			if (bc->set_params == SKL_PARAM_BIND) {
821				params = kmemdup(bc->params, bc->max, GFP_KERNEL);
822				if (!params)
823					return -ENOMEM;
824
825				skl_fill_sink_instance_id(skl, params, bc->max,
826								mconfig);
827
828				ret = skl_set_module_params(skl, params,
829						bc->max, bc->param_id, mconfig);
830				kfree(params);
831
832				if (ret < 0)
833					return ret;
834			}
835		}
836	}
837
838	return 0;
839}
840
841static int skl_get_module_id(struct skl_dev *skl, guid_t *uuid)
842{
843	struct uuid_module *module;
844
845	list_for_each_entry(module, &skl->uuid_list, list) {
846		if (guid_equal(uuid, &module->uuid))
847			return module->id;
848	}
849
850	return -EINVAL;
851}
852
853static int skl_tplg_find_moduleid_from_uuid(struct skl_dev *skl,
854					const struct snd_kcontrol_new *k)
855{
856	struct soc_bytes_ext *sb = (void *) k->private_value;
857	struct skl_algo_data *bc = (struct skl_algo_data *)sb->dobj.private;
858	struct skl_kpb_params *uuid_params, *params;
859	struct hdac_bus *bus = skl_to_bus(skl);
860	int i, size, module_id;
861
862	if (bc->set_params == SKL_PARAM_BIND && bc->max) {
863		uuid_params = (struct skl_kpb_params *)bc->params;
864		size = struct_size(params, u.map, uuid_params->num_modules);
865
866		params = devm_kzalloc(bus->dev, size, GFP_KERNEL);
867		if (!params)
868			return -ENOMEM;
869
870		params->num_modules = uuid_params->num_modules;
871
872		for (i = 0; i < uuid_params->num_modules; i++) {
873			module_id = skl_get_module_id(skl,
874				&uuid_params->u.map_uuid[i].mod_uuid);
875			if (module_id < 0) {
876				devm_kfree(bus->dev, params);
877				return -EINVAL;
878			}
879
880			params->u.map[i].mod_id = module_id;
881			params->u.map[i].inst_id =
882				uuid_params->u.map_uuid[i].inst_id;
883		}
884
885		devm_kfree(bus->dev, bc->params);
886		bc->params = (char *)params;
887		bc->max = size;
888	}
889
890	return 0;
891}
892
893/*
894 * Retrieve the module id from UUID mentioned in the
895 * post bind params
896 */
897void skl_tplg_add_moduleid_in_bind_params(struct skl_dev *skl,
898				struct snd_soc_dapm_widget *w)
899{
900	struct skl_module_cfg *mconfig = w->priv;
901	int i;
902
903	/*
904	 * Post bind params are used for only for KPB
905	 * to set copier instances to drain the data
906	 * in fast mode
907	 */
908	if (mconfig->m_type != SKL_MODULE_TYPE_KPB)
909		return;
910
911	for (i = 0; i < w->num_kcontrols; i++)
912		if ((w->kcontrol_news[i].access &
913			SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) &&
914			(skl_tplg_find_moduleid_from_uuid(skl,
915			&w->kcontrol_news[i]) < 0))
916			dev_err(skl->dev,
917				"%s: invalid kpb post bind params\n",
918				__func__);
919}
920
921static int skl_tplg_module_add_deferred_bind(struct skl_dev *skl,
922	struct skl_module_cfg *src, struct skl_module_cfg *dst)
923{
924	struct skl_module_deferred_bind *m_list, *modules;
925	int i;
926
927	/* only supported for module with static pin connection */
928	for (i = 0; i < dst->module->max_input_pins; i++) {
929		struct skl_module_pin *pin = &dst->m_in_pin[i];
930
931		if (pin->is_dynamic)
932			continue;
933
934		if ((pin->id.module_id  == src->id.module_id) &&
935			(pin->id.instance_id  == src->id.instance_id)) {
936
937			if (!list_empty(&skl->bind_list)) {
938				list_for_each_entry(modules, &skl->bind_list, node) {
939					if (modules->src == src && modules->dst == dst)
940						return 0;
941				}
942			}
943
944			m_list = kzalloc(sizeof(*m_list), GFP_KERNEL);
945			if (!m_list)
946				return -ENOMEM;
947
948			m_list->src = src;
949			m_list->dst = dst;
950
951			list_add(&m_list->node, &skl->bind_list);
952		}
953	}
954
955	return 0;
956}
957
958static int skl_tplg_bind_sinks(struct snd_soc_dapm_widget *w,
959				struct skl_dev *skl,
960				struct snd_soc_dapm_widget *src_w,
961				struct skl_module_cfg *src_mconfig)
962{
963	struct snd_soc_dapm_path *p;
964	struct snd_soc_dapm_widget *sink = NULL, *next_sink = NULL;
965	struct skl_module_cfg *sink_mconfig;
966	int ret;
967
968	snd_soc_dapm_widget_for_each_sink_path(w, p) {
969		if (!p->connect)
970			continue;
971
972		dev_dbg(skl->dev,
973			"%s: src widget=%s\n", __func__, w->name);
974		dev_dbg(skl->dev,
975			"%s: sink widget=%s\n", __func__, p->sink->name);
976
977		next_sink = p->sink;
978
979		if (!is_skl_dsp_widget_type(p->sink, skl->dev))
980			return skl_tplg_bind_sinks(p->sink, skl, src_w, src_mconfig);
981
982		/*
983		 * here we will check widgets in sink pipelines, so that
984		 * can be any widgets type and we are only interested if
985		 * they are ones used for SKL so check that first
986		 */
987		if ((p->sink->priv != NULL) &&
988				is_skl_dsp_widget_type(p->sink, skl->dev)) {
989
990			sink = p->sink;
991			sink_mconfig = sink->priv;
992
993			/*
994			 * Modules other than PGA leaf can be connected
995			 * directly or via switch to a module in another
996			 * pipeline. EX: reference path
997			 * when the path is enabled, the dst module that needs
998			 * to be bound may not be initialized. if the module is
999			 * not initialized, add these modules in the deferred
1000			 * bind list and when the dst module is initialised,
1001			 * bind this module to the dst_module in deferred list.
1002			 */
1003			if (((src_mconfig->m_state == SKL_MODULE_INIT_DONE)
1004				&& (sink_mconfig->m_state == SKL_MODULE_UNINIT))) {
1005
1006				ret = skl_tplg_module_add_deferred_bind(skl,
1007						src_mconfig, sink_mconfig);
1008
1009				if (ret < 0)
1010					return ret;
1011
1012			}
1013
1014
1015			if (src_mconfig->m_state == SKL_MODULE_UNINIT ||
1016				sink_mconfig->m_state == SKL_MODULE_UNINIT)
1017				continue;
1018
1019			/* Bind source to sink, mixin is always source */
1020			ret = skl_bind_modules(skl, src_mconfig, sink_mconfig);
1021			if (ret)
1022				return ret;
1023
1024			/* set module params after bind */
1025			skl_tplg_set_module_bind_params(src_w,
1026					src_mconfig, skl);
1027			skl_tplg_set_module_bind_params(sink,
1028					sink_mconfig, skl);
1029
1030			/* Start sinks pipe first */
1031			if (sink_mconfig->pipe->state != SKL_PIPE_STARTED) {
1032				if (sink_mconfig->pipe->conn_type !=
1033							SKL_PIPE_CONN_TYPE_FE)
1034					ret = skl_run_pipe(skl,
1035							sink_mconfig->pipe);
1036				if (ret)
1037					return ret;
1038			}
1039		}
1040	}
1041
1042	if (!sink && next_sink)
1043		return skl_tplg_bind_sinks(next_sink, skl, src_w, src_mconfig);
1044
1045	return 0;
1046}
1047
1048/*
1049 * A PGA represents a module in a pipeline. So in the Pre-PMU event of PGA
1050 * we need to do following:
1051 *   - Bind to sink pipeline
1052 *      Since the sink pipes can be running and we don't get mixer event on
1053 *      connect for already running mixer, we need to find the sink pipes
1054 *      here and bind to them. This way dynamic connect works.
1055 *   - Start sink pipeline, if not running
1056 *   - Then run current pipe
1057 */
1058static int skl_tplg_pga_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
1059							struct skl_dev *skl)
1060{
1061	struct skl_module_cfg *src_mconfig;
1062	int ret = 0;
1063
1064	src_mconfig = w->priv;
1065
1066	/*
1067	 * find which sink it is connected to, bind with the sink,
1068	 * if sink is not started, start sink pipe first, then start
1069	 * this pipe
1070	 */
1071	ret = skl_tplg_bind_sinks(w, skl, w, src_mconfig);
1072	if (ret)
1073		return ret;
1074
1075	/* Start source pipe last after starting all sinks */
1076	if (src_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
1077		return skl_run_pipe(skl, src_mconfig->pipe);
1078
1079	return 0;
1080}
1081
1082static struct snd_soc_dapm_widget *skl_get_src_dsp_widget(
1083		struct snd_soc_dapm_widget *w, struct skl_dev *skl)
1084{
1085	struct snd_soc_dapm_path *p;
1086	struct snd_soc_dapm_widget *src_w = NULL;
1087
1088	snd_soc_dapm_widget_for_each_source_path(w, p) {
1089		src_w = p->source;
1090		if (!p->connect)
1091			continue;
1092
1093		dev_dbg(skl->dev, "sink widget=%s\n", w->name);
1094		dev_dbg(skl->dev, "src widget=%s\n", p->source->name);
1095
1096		/*
1097		 * here we will check widgets in sink pipelines, so that can
1098		 * be any widgets type and we are only interested if they are
1099		 * ones used for SKL so check that first
1100		 */
1101		if ((p->source->priv != NULL) &&
1102				is_skl_dsp_widget_type(p->source, skl->dev)) {
1103			return p->source;
1104		}
1105	}
1106
1107	if (src_w != NULL)
1108		return skl_get_src_dsp_widget(src_w, skl);
1109
1110	return NULL;
1111}
1112
1113/*
1114 * in the Post-PMU event of mixer we need to do following:
1115 *   - Check if this pipe is running
1116 *   - if not, then
1117 *	- bind this pipeline to its source pipeline
1118 *	  if source pipe is already running, this means it is a dynamic
1119 *	  connection and we need to bind only to that pipe
1120 *	- start this pipeline
1121 */
1122static int skl_tplg_mixer_dapm_post_pmu_event(struct snd_soc_dapm_widget *w,
1123							struct skl_dev *skl)
1124{
1125	int ret = 0;
1126	struct snd_soc_dapm_widget *source, *sink;
1127	struct skl_module_cfg *src_mconfig, *sink_mconfig;
1128	int src_pipe_started = 0;
1129
1130	sink = w;
1131	sink_mconfig = sink->priv;
1132
1133	/*
1134	 * If source pipe is already started, that means source is driving
1135	 * one more sink before this sink got connected, Since source is
1136	 * started, bind this sink to source and start this pipe.
1137	 */
1138	source = skl_get_src_dsp_widget(w, skl);
1139	if (source != NULL) {
1140		src_mconfig = source->priv;
1141		sink_mconfig = sink->priv;
1142		src_pipe_started = 1;
1143
1144		/*
1145		 * check pipe state, then no need to bind or start the
1146		 * pipe
1147		 */
1148		if (src_mconfig->pipe->state != SKL_PIPE_STARTED)
1149			src_pipe_started = 0;
1150	}
1151
1152	if (src_pipe_started) {
1153		ret = skl_bind_modules(skl, src_mconfig, sink_mconfig);
1154		if (ret)
1155			return ret;
1156
1157		/* set module params after bind */
1158		skl_tplg_set_module_bind_params(source, src_mconfig, skl);
1159		skl_tplg_set_module_bind_params(sink, sink_mconfig, skl);
1160
1161		if (sink_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
1162			ret = skl_run_pipe(skl, sink_mconfig->pipe);
1163	}
1164
1165	return ret;
1166}
1167
1168/*
1169 * in the Pre-PMD event of mixer we need to do following:
1170 *   - Stop the pipe
1171 *   - find the source connections and remove that from dapm_path_list
1172 *   - unbind with source pipelines if still connected
1173 */
1174static int skl_tplg_mixer_dapm_pre_pmd_event(struct snd_soc_dapm_widget *w,
1175							struct skl_dev *skl)
1176{
1177	struct skl_module_cfg *src_mconfig, *sink_mconfig;
1178	int ret = 0, i;
1179
1180	sink_mconfig = w->priv;
1181
1182	/* Stop the pipe */
1183	ret = skl_stop_pipe(skl, sink_mconfig->pipe);
1184	if (ret)
1185		return ret;
1186
1187	for (i = 0; i < sink_mconfig->module->max_input_pins; i++) {
1188		if (sink_mconfig->m_in_pin[i].pin_state == SKL_PIN_BIND_DONE) {
1189			src_mconfig = sink_mconfig->m_in_pin[i].tgt_mcfg;
1190			if (!src_mconfig)
1191				continue;
1192
1193			ret = skl_unbind_modules(skl,
1194						src_mconfig, sink_mconfig);
1195		}
1196	}
1197
1198	return ret;
1199}
1200
1201/*
1202 * in the Post-PMD event of mixer we need to do following:
1203 *   - Unbind the modules within the pipeline
1204 *   - Delete the pipeline (modules are not required to be explicitly
1205 *     deleted, pipeline delete is enough here
1206 */
1207static int skl_tplg_mixer_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
1208							struct skl_dev *skl)
1209{
1210	struct skl_module_cfg *mconfig = w->priv;
1211	struct skl_pipe_module *w_module;
1212	struct skl_module_cfg *src_module = NULL, *dst_module;
1213	struct skl_pipe *s_pipe = mconfig->pipe;
1214	struct skl_module_deferred_bind *modules, *tmp;
1215
1216	if (s_pipe->state == SKL_PIPE_INVALID)
1217		return -EINVAL;
1218
1219	list_for_each_entry(w_module, &s_pipe->w_list, node) {
1220		if (list_empty(&skl->bind_list))
1221			break;
1222
1223		src_module = w_module->w->priv;
1224
1225		list_for_each_entry_safe(modules, tmp, &skl->bind_list, node) {
1226			/*
1227			 * When the destination module is deleted, Unbind the
1228			 * modules from deferred bind list.
1229			 */
1230			if (modules->dst == src_module) {
1231				skl_unbind_modules(skl, modules->src,
1232						modules->dst);
1233			}
1234
1235			/*
1236			 * When the source module is deleted, remove this entry
1237			 * from the deferred bind list.
1238			 */
1239			if (modules->src == src_module) {
1240				list_del(&modules->node);
1241				modules->src = NULL;
1242				modules->dst = NULL;
1243				kfree(modules);
1244			}
1245		}
1246	}
1247
1248	list_for_each_entry(w_module, &s_pipe->w_list, node) {
1249		dst_module = w_module->w->priv;
1250
1251		if (src_module == NULL) {
1252			src_module = dst_module;
1253			continue;
1254		}
1255
1256		skl_unbind_modules(skl, src_module, dst_module);
1257		src_module = dst_module;
1258	}
1259
1260	skl_delete_pipe(skl, mconfig->pipe);
1261
1262	list_for_each_entry(w_module, &s_pipe->w_list, node) {
1263		src_module = w_module->w->priv;
1264		src_module->m_state = SKL_MODULE_UNINIT;
1265	}
1266
1267	return skl_tplg_unload_pipe_modules(skl, s_pipe);
1268}
1269
1270/*
1271 * in the Post-PMD event of PGA we need to do following:
1272 *   - Stop the pipeline
1273 *   - In source pipe is connected, unbind with source pipelines
1274 */
1275static int skl_tplg_pga_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
1276							struct skl_dev *skl)
1277{
1278	struct skl_module_cfg *src_mconfig, *sink_mconfig;
1279	int ret = 0, i;
1280
1281	src_mconfig = w->priv;
1282
1283	/* Stop the pipe since this is a mixin module */
1284	ret = skl_stop_pipe(skl, src_mconfig->pipe);
1285	if (ret)
1286		return ret;
1287
1288	for (i = 0; i < src_mconfig->module->max_output_pins; i++) {
1289		if (src_mconfig->m_out_pin[i].pin_state == SKL_PIN_BIND_DONE) {
1290			sink_mconfig = src_mconfig->m_out_pin[i].tgt_mcfg;
1291			if (!sink_mconfig)
1292				continue;
1293			/*
1294			 * This is a connecter and if path is found that means
1295			 * unbind between source and sink has not happened yet
1296			 */
1297			ret = skl_unbind_modules(skl, src_mconfig,
1298							sink_mconfig);
1299		}
1300	}
1301
1302	return ret;
1303}
1304
1305/*
1306 * In modelling, we assume there will be ONLY one mixer in a pipeline. If a
1307 * second one is required that is created as another pipe entity.
1308 * The mixer is responsible for pipe management and represent a pipeline
1309 * instance
1310 */
1311static int skl_tplg_mixer_event(struct snd_soc_dapm_widget *w,
1312				struct snd_kcontrol *k, int event)
1313{
1314	struct snd_soc_dapm_context *dapm = w->dapm;
1315	struct skl_dev *skl = get_skl_ctx(dapm->dev);
1316
1317	switch (event) {
1318	case SND_SOC_DAPM_PRE_PMU:
1319		return skl_tplg_mixer_dapm_pre_pmu_event(w, skl);
1320
1321	case SND_SOC_DAPM_POST_PMU:
1322		return skl_tplg_mixer_dapm_post_pmu_event(w, skl);
1323
1324	case SND_SOC_DAPM_PRE_PMD:
1325		return skl_tplg_mixer_dapm_pre_pmd_event(w, skl);
1326
1327	case SND_SOC_DAPM_POST_PMD:
1328		return skl_tplg_mixer_dapm_post_pmd_event(w, skl);
1329	}
1330
1331	return 0;
1332}
1333
1334/*
1335 * In modelling, we assumed rest of the modules in pipeline are PGA. But we
1336 * are interested in last PGA (leaf PGA) in a pipeline to disconnect with
1337 * the sink when it is running (two FE to one BE or one FE to two BE)
1338 * scenarios
1339 */
1340static int skl_tplg_pga_event(struct snd_soc_dapm_widget *w,
1341			struct snd_kcontrol *k, int event)
1342
1343{
1344	struct snd_soc_dapm_context *dapm = w->dapm;
1345	struct skl_dev *skl = get_skl_ctx(dapm->dev);
1346
1347	switch (event) {
1348	case SND_SOC_DAPM_PRE_PMU:
1349		return skl_tplg_pga_dapm_pre_pmu_event(w, skl);
1350
1351	case SND_SOC_DAPM_POST_PMD:
1352		return skl_tplg_pga_dapm_post_pmd_event(w, skl);
1353	}
1354
1355	return 0;
1356}
1357
1358static int skl_tplg_multi_config_set_get(struct snd_kcontrol *kcontrol,
1359					 struct snd_ctl_elem_value *ucontrol,
1360					 bool is_set)
1361{
1362	struct snd_soc_component *component =
1363		snd_soc_kcontrol_component(kcontrol);
1364	struct hdac_bus *bus = snd_soc_component_get_drvdata(component);
1365	struct skl_dev *skl = bus_to_skl(bus);
1366	struct skl_pipeline *ppl;
1367	struct skl_pipe *pipe = NULL;
1368	struct soc_enum *ec = (struct soc_enum *)kcontrol->private_value;
1369	u32 *pipe_id;
1370
1371	if (!ec)
1372		return -EINVAL;
1373
1374	if (is_set && ucontrol->value.enumerated.item[0] > ec->items)
1375		return -EINVAL;
1376
1377	pipe_id = ec->dobj.private;
1378
1379	list_for_each_entry(ppl, &skl->ppl_list, node) {
1380		if (ppl->pipe->ppl_id == *pipe_id) {
1381			pipe = ppl->pipe;
1382			break;
1383		}
1384	}
1385	if (!pipe)
1386		return -EIO;
1387
1388	if (is_set)
1389		pipe->pipe_config_idx = ucontrol->value.enumerated.item[0];
1390	else
1391		ucontrol->value.enumerated.item[0]  =  pipe->pipe_config_idx;
1392
1393	return 0;
1394}
1395
1396static int skl_tplg_multi_config_get(struct snd_kcontrol *kcontrol,
1397				     struct snd_ctl_elem_value *ucontrol)
1398{
1399	return skl_tplg_multi_config_set_get(kcontrol, ucontrol, false);
1400}
1401
1402static int skl_tplg_multi_config_set(struct snd_kcontrol *kcontrol,
1403				     struct snd_ctl_elem_value *ucontrol)
1404{
1405	return skl_tplg_multi_config_set_get(kcontrol, ucontrol, true);
1406}
1407
1408static int skl_tplg_multi_config_get_dmic(struct snd_kcontrol *kcontrol,
1409					  struct snd_ctl_elem_value *ucontrol)
1410{
1411	return skl_tplg_multi_config_set_get(kcontrol, ucontrol, false);
1412}
1413
1414static int skl_tplg_multi_config_set_dmic(struct snd_kcontrol *kcontrol,
1415					  struct snd_ctl_elem_value *ucontrol)
1416{
1417	return skl_tplg_multi_config_set_get(kcontrol, ucontrol, true);
1418}
1419
1420static int skl_tplg_tlv_control_get(struct snd_kcontrol *kcontrol,
1421			unsigned int __user *data, unsigned int size)
1422{
1423	struct soc_bytes_ext *sb =
1424			(struct soc_bytes_ext *)kcontrol->private_value;
1425	struct skl_algo_data *bc = (struct skl_algo_data *)sb->dobj.private;
1426	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1427	struct skl_module_cfg *mconfig = w->priv;
1428	struct skl_dev *skl = get_skl_ctx(w->dapm->dev);
1429
1430	if (w->power)
1431		skl_get_module_params(skl, (u32 *)bc->params,
1432				      bc->size, bc->param_id, mconfig);
1433
1434	/* decrement size for TLV header */
1435	size -= 2 * sizeof(u32);
1436
1437	/* check size as we don't want to send kernel data */
1438	if (size > bc->max)
1439		size = bc->max;
1440
1441	if (bc->params) {
1442		if (copy_to_user(data, &bc->param_id, sizeof(u32)))
1443			return -EFAULT;
1444		if (copy_to_user(data + 1, &size, sizeof(u32)))
1445			return -EFAULT;
1446		if (copy_to_user(data + 2, bc->params, size))
1447			return -EFAULT;
1448	}
1449
1450	return 0;
1451}
1452
1453#define SKL_PARAM_VENDOR_ID 0xff
1454
1455static int skl_tplg_tlv_control_set(struct snd_kcontrol *kcontrol,
1456			const unsigned int __user *data, unsigned int size)
1457{
1458	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1459	struct skl_module_cfg *mconfig = w->priv;
1460	struct soc_bytes_ext *sb =
1461			(struct soc_bytes_ext *)kcontrol->private_value;
1462	struct skl_algo_data *ac = (struct skl_algo_data *)sb->dobj.private;
1463	struct skl_dev *skl = get_skl_ctx(w->dapm->dev);
1464
1465	if (ac->params) {
1466		if (size > ac->max)
1467			return -EINVAL;
1468		ac->size = size;
1469
1470		if (copy_from_user(ac->params, data, size))
1471			return -EFAULT;
1472
1473		if (w->power)
1474			return skl_set_module_params(skl,
1475						(u32 *)ac->params, ac->size,
1476						ac->param_id, mconfig);
1477	}
1478
1479	return 0;
1480}
1481
1482static int skl_tplg_mic_control_get(struct snd_kcontrol *kcontrol,
1483		struct snd_ctl_elem_value *ucontrol)
1484{
1485	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1486	struct skl_module_cfg *mconfig = w->priv;
1487	struct soc_enum *ec = (struct soc_enum *)kcontrol->private_value;
1488	u32 ch_type = *((u32 *)ec->dobj.private);
1489
1490	if (mconfig->dmic_ch_type == ch_type)
1491		ucontrol->value.enumerated.item[0] =
1492					mconfig->dmic_ch_combo_index;
1493	else
1494		ucontrol->value.enumerated.item[0] = 0;
1495
1496	return 0;
1497}
1498
1499static int skl_fill_mic_sel_params(struct skl_module_cfg *mconfig,
1500	struct skl_mic_sel_config *mic_cfg, struct device *dev)
1501{
1502	struct skl_specific_cfg *sp_cfg = &mconfig->formats_config;
1503
1504	sp_cfg->caps_size = sizeof(struct skl_mic_sel_config);
1505	sp_cfg->set_params = SKL_PARAM_SET;
1506	sp_cfg->param_id = 0x00;
1507	if (!sp_cfg->caps) {
1508		sp_cfg->caps = devm_kzalloc(dev, sp_cfg->caps_size, GFP_KERNEL);
1509		if (!sp_cfg->caps)
1510			return -ENOMEM;
1511	}
1512
1513	mic_cfg->mic_switch = SKL_MIC_SEL_SWITCH;
1514	mic_cfg->flags = 0;
1515	memcpy(sp_cfg->caps, mic_cfg, sp_cfg->caps_size);
1516
1517	return 0;
1518}
1519
1520static int skl_tplg_mic_control_set(struct snd_kcontrol *kcontrol,
1521			struct snd_ctl_elem_value *ucontrol)
1522{
1523	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1524	struct skl_module_cfg *mconfig = w->priv;
1525	struct skl_mic_sel_config mic_cfg = {0};
1526	struct soc_enum *ec = (struct soc_enum *)kcontrol->private_value;
1527	u32 ch_type = *((u32 *)ec->dobj.private);
1528	const int *list;
1529	u8 in_ch, out_ch, index;
1530
1531	mconfig->dmic_ch_type = ch_type;
1532	mconfig->dmic_ch_combo_index = ucontrol->value.enumerated.item[0];
1533
1534	/* enum control index 0 is INVALID, so no channels to be set */
1535	if (mconfig->dmic_ch_combo_index == 0)
1536		return 0;
1537
1538	/* No valid channel selection map for index 0, so offset by 1 */
1539	index = mconfig->dmic_ch_combo_index - 1;
1540
1541	switch (ch_type) {
1542	case SKL_CH_MONO:
1543		if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_mono_list))
1544			return -EINVAL;
1545
1546		list = &mic_mono_list[index];
1547		break;
1548
1549	case SKL_CH_STEREO:
1550		if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_stereo_list))
1551			return -EINVAL;
1552
1553		list = mic_stereo_list[index];
1554		break;
1555
1556	case SKL_CH_TRIO:
1557		if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_trio_list))
1558			return -EINVAL;
1559
1560		list = mic_trio_list[index];
1561		break;
1562
1563	case SKL_CH_QUATRO:
1564		if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_quatro_list))
1565			return -EINVAL;
1566
1567		list = mic_quatro_list[index];
1568		break;
1569
1570	default:
1571		dev_err(w->dapm->dev,
1572				"Invalid channel %d for mic_select module\n",
1573				ch_type);
1574		return -EINVAL;
1575
1576	}
1577
1578	/* channel type enum map to number of chanels for that type */
1579	for (out_ch = 0; out_ch < ch_type; out_ch++) {
1580		in_ch = list[out_ch];
1581		mic_cfg.blob[out_ch][in_ch] = SKL_DEFAULT_MIC_SEL_GAIN;
1582	}
1583
1584	return skl_fill_mic_sel_params(mconfig, &mic_cfg, w->dapm->dev);
1585}
1586
1587/*
1588 * Fill the dma id for host and link. In case of passthrough
1589 * pipeline, this will both host and link in the same
1590 * pipeline, so need to copy the link and host based on dev_type
1591 */
1592static void skl_tplg_fill_dma_id(struct skl_module_cfg *mcfg,
1593				struct skl_pipe_params *params)
1594{
1595	struct skl_pipe *pipe = mcfg->pipe;
1596
1597	if (pipe->passthru) {
1598		switch (mcfg->dev_type) {
1599		case SKL_DEVICE_HDALINK:
1600			pipe->p_params->link_dma_id = params->link_dma_id;
1601			pipe->p_params->link_index = params->link_index;
1602			pipe->p_params->link_bps = params->link_bps;
1603			break;
1604
1605		case SKL_DEVICE_HDAHOST:
1606			pipe->p_params->host_dma_id = params->host_dma_id;
1607			pipe->p_params->host_bps = params->host_bps;
1608			break;
1609
1610		default:
1611			break;
1612		}
1613		pipe->p_params->s_fmt = params->s_fmt;
1614		pipe->p_params->ch = params->ch;
1615		pipe->p_params->s_freq = params->s_freq;
1616		pipe->p_params->stream = params->stream;
1617		pipe->p_params->format = params->format;
1618
1619	} else {
1620		memcpy(pipe->p_params, params, sizeof(*params));
1621	}
1622}
1623
1624/*
1625 * The FE params are passed by hw_params of the DAI.
1626 * On hw_params, the params are stored in Gateway module of the FE and we
1627 * need to calculate the format in DSP module configuration, that
1628 * conversion is done here
1629 */
1630int skl_tplg_update_pipe_params(struct device *dev,
1631			struct skl_module_cfg *mconfig,
1632			struct skl_pipe_params *params)
1633{
1634	struct skl_module_res *res;
1635	struct skl_dev *skl = get_skl_ctx(dev);
1636	struct skl_module_fmt *format = NULL;
1637	u8 cfg_idx = mconfig->pipe->cur_config_idx;
1638
1639	res = &mconfig->module->resources[mconfig->res_idx];
1640	skl_tplg_fill_dma_id(mconfig, params);
1641	mconfig->fmt_idx = mconfig->mod_cfg[cfg_idx].fmt_idx;
1642	mconfig->res_idx = mconfig->mod_cfg[cfg_idx].res_idx;
1643
1644	if (skl->nr_modules)
1645		return 0;
1646
1647	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK)
1648		format = &mconfig->module->formats[mconfig->fmt_idx].inputs[0].fmt;
1649	else
1650		format = &mconfig->module->formats[mconfig->fmt_idx].outputs[0].fmt;
1651
1652	/* set the hw_params */
1653	format->s_freq = params->s_freq;
1654	format->channels = params->ch;
1655	format->valid_bit_depth = skl_get_bit_depth(params->s_fmt);
1656
1657	/*
1658	 * 16 bit is 16 bit container whereas 24 bit is in 32 bit
1659	 * container so update bit depth accordingly
1660	 */
1661	switch (format->valid_bit_depth) {
1662	case SKL_DEPTH_16BIT:
1663		format->bit_depth = format->valid_bit_depth;
1664		break;
1665
1666	case SKL_DEPTH_24BIT:
1667	case SKL_DEPTH_32BIT:
1668		format->bit_depth = SKL_DEPTH_32BIT;
1669		break;
1670
1671	default:
1672		dev_err(dev, "Invalid bit depth %x for pipe\n",
1673				format->valid_bit_depth);
1674		return -EINVAL;
1675	}
1676
1677	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1678		res->ibs = (format->s_freq / 1000) *
1679				(format->channels) *
1680				(format->bit_depth >> 3);
1681	} else {
1682		res->obs = (format->s_freq / 1000) *
1683				(format->channels) *
1684				(format->bit_depth >> 3);
1685	}
1686
1687	return 0;
1688}
1689
1690/*
1691 * Query the module config for the FE DAI
1692 * This is used to find the hw_params set for that DAI and apply to FE
1693 * pipeline
1694 */
1695struct skl_module_cfg *
1696skl_tplg_fe_get_cpr_module(struct snd_soc_dai *dai, int stream)
1697{
1698	struct snd_soc_dapm_widget *w;
1699	struct snd_soc_dapm_path *p = NULL;
1700
1701	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1702		w = dai->playback_widget;
1703		snd_soc_dapm_widget_for_each_sink_path(w, p) {
1704			if (p->connect && p->sink->power &&
1705				!is_skl_dsp_widget_type(p->sink, dai->dev))
1706				continue;
1707
1708			if (p->sink->priv) {
1709				dev_dbg(dai->dev, "set params for %s\n",
1710						p->sink->name);
1711				return p->sink->priv;
1712			}
1713		}
1714	} else {
1715		w = dai->capture_widget;
1716		snd_soc_dapm_widget_for_each_source_path(w, p) {
1717			if (p->connect && p->source->power &&
1718				!is_skl_dsp_widget_type(p->source, dai->dev))
1719				continue;
1720
1721			if (p->source->priv) {
1722				dev_dbg(dai->dev, "set params for %s\n",
1723						p->source->name);
1724				return p->source->priv;
1725			}
1726		}
1727	}
1728
1729	return NULL;
1730}
1731
1732static struct skl_module_cfg *skl_get_mconfig_pb_cpr(
1733		struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
1734{
1735	struct snd_soc_dapm_path *p;
1736	struct skl_module_cfg *mconfig = NULL;
1737
1738	snd_soc_dapm_widget_for_each_source_path(w, p) {
1739		if (w->endpoints[SND_SOC_DAPM_DIR_OUT] > 0) {
1740			if (p->connect &&
1741				    (p->sink->id == snd_soc_dapm_aif_out) &&
1742				    p->source->priv) {
1743				mconfig = p->source->priv;
1744				return mconfig;
1745			}
1746			mconfig = skl_get_mconfig_pb_cpr(dai, p->source);
1747			if (mconfig)
1748				return mconfig;
1749		}
1750	}
1751	return mconfig;
1752}
1753
1754static struct skl_module_cfg *skl_get_mconfig_cap_cpr(
1755		struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
1756{
1757	struct snd_soc_dapm_path *p;
1758	struct skl_module_cfg *mconfig = NULL;
1759
1760	snd_soc_dapm_widget_for_each_sink_path(w, p) {
1761		if (w->endpoints[SND_SOC_DAPM_DIR_IN] > 0) {
1762			if (p->connect &&
1763				    (p->source->id == snd_soc_dapm_aif_in) &&
1764				    p->sink->priv) {
1765				mconfig = p->sink->priv;
1766				return mconfig;
1767			}
1768			mconfig = skl_get_mconfig_cap_cpr(dai, p->sink);
1769			if (mconfig)
1770				return mconfig;
1771		}
1772	}
1773	return mconfig;
1774}
1775
1776struct skl_module_cfg *
1777skl_tplg_be_get_cpr_module(struct snd_soc_dai *dai, int stream)
1778{
1779	struct snd_soc_dapm_widget *w;
1780	struct skl_module_cfg *mconfig;
1781
1782	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1783		w = dai->playback_widget;
1784		mconfig = skl_get_mconfig_pb_cpr(dai, w);
1785	} else {
1786		w = dai->capture_widget;
1787		mconfig = skl_get_mconfig_cap_cpr(dai, w);
1788	}
1789	return mconfig;
1790}
1791
1792static u8 skl_tplg_be_link_type(int dev_type)
1793{
1794	int ret;
1795
1796	switch (dev_type) {
1797	case SKL_DEVICE_BT:
1798		ret = NHLT_LINK_SSP;
1799		break;
1800
1801	case SKL_DEVICE_DMIC:
1802		ret = NHLT_LINK_DMIC;
1803		break;
1804
1805	case SKL_DEVICE_I2S:
1806		ret = NHLT_LINK_SSP;
1807		break;
1808
1809	case SKL_DEVICE_HDALINK:
1810		ret = NHLT_LINK_HDA;
1811		break;
1812
1813	default:
1814		ret = NHLT_LINK_INVALID;
1815		break;
1816	}
1817
1818	return ret;
1819}
1820
1821/*
1822 * Fill the BE gateway parameters
1823 * The BE gateway expects a blob of parameters which are kept in the ACPI
1824 * NHLT blob, so query the blob for interface type (i2s/pdm) and instance.
1825 * The port can have multiple settings so pick based on the PCM
1826 * parameters
1827 */
1828static int skl_tplg_be_fill_pipe_params(struct snd_soc_dai *dai,
1829				struct skl_module_cfg *mconfig,
1830				struct skl_pipe_params *params)
1831{
1832	struct nhlt_specific_cfg *cfg;
1833	struct skl_dev *skl = get_skl_ctx(dai->dev);
1834	int link_type = skl_tplg_be_link_type(mconfig->dev_type);
1835	u8 dev_type = skl_tplg_be_dev_type(mconfig->dev_type);
1836
1837	skl_tplg_fill_dma_id(mconfig, params);
1838
1839	if (link_type == NHLT_LINK_HDA)
1840		return 0;
1841
1842	/* update the blob based on virtual bus_id*/
1843	cfg = skl_get_ep_blob(skl, mconfig->vbus_id, link_type,
1844					params->s_fmt, params->ch,
1845					params->s_freq, params->stream,
1846					dev_type);
1847	if (cfg) {
1848		mconfig->formats_config.caps_size = cfg->size;
1849		mconfig->formats_config.caps = (u32 *) &cfg->caps;
1850	} else {
1851		dev_err(dai->dev, "Blob NULL for id %x type %d dirn %d\n",
1852					mconfig->vbus_id, link_type,
1853					params->stream);
1854		dev_err(dai->dev, "PCM: ch %d, freq %d, fmt %d\n",
1855				 params->ch, params->s_freq, params->s_fmt);
1856		return -EINVAL;
1857	}
1858
1859	return 0;
1860}
1861
1862static int skl_tplg_be_set_src_pipe_params(struct snd_soc_dai *dai,
1863				struct snd_soc_dapm_widget *w,
1864				struct skl_pipe_params *params)
1865{
1866	struct snd_soc_dapm_path *p;
1867	int ret = -EIO;
1868
1869	snd_soc_dapm_widget_for_each_source_path(w, p) {
1870		if (p->connect && is_skl_dsp_widget_type(p->source, dai->dev) &&
1871						p->source->priv) {
1872
1873			ret = skl_tplg_be_fill_pipe_params(dai,
1874						p->source->priv, params);
1875			if (ret < 0)
1876				return ret;
1877		} else {
1878			ret = skl_tplg_be_set_src_pipe_params(dai,
1879						p->source, params);
1880			if (ret < 0)
1881				return ret;
1882		}
1883	}
1884
1885	return ret;
1886}
1887
1888static int skl_tplg_be_set_sink_pipe_params(struct snd_soc_dai *dai,
1889	struct snd_soc_dapm_widget *w, struct skl_pipe_params *params)
1890{
1891	struct snd_soc_dapm_path *p;
1892	int ret = -EIO;
1893
1894	snd_soc_dapm_widget_for_each_sink_path(w, p) {
1895		if (p->connect && is_skl_dsp_widget_type(p->sink, dai->dev) &&
1896						p->sink->priv) {
1897
1898			ret = skl_tplg_be_fill_pipe_params(dai,
1899						p->sink->priv, params);
1900			if (ret < 0)
1901				return ret;
1902		} else {
1903			ret = skl_tplg_be_set_sink_pipe_params(
1904						dai, p->sink, params);
1905			if (ret < 0)
1906				return ret;
1907		}
1908	}
1909
1910	return ret;
1911}
1912
1913/*
1914 * BE hw_params can be a source parameters (capture) or sink parameters
1915 * (playback). Based on sink and source we need to either find the source
1916 * list or the sink list and set the pipeline parameters
1917 */
1918int skl_tplg_be_update_params(struct snd_soc_dai *dai,
1919				struct skl_pipe_params *params)
1920{
1921	struct snd_soc_dapm_widget *w;
1922
1923	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1924		w = dai->playback_widget;
1925
1926		return skl_tplg_be_set_src_pipe_params(dai, w, params);
1927
1928	} else {
1929		w = dai->capture_widget;
1930
1931		return skl_tplg_be_set_sink_pipe_params(dai, w, params);
1932	}
1933
1934	return 0;
1935}
1936
1937static const struct snd_soc_tplg_widget_events skl_tplg_widget_ops[] = {
1938	{SKL_MIXER_EVENT, skl_tplg_mixer_event},
1939	{SKL_VMIXER_EVENT, skl_tplg_mixer_event},
1940	{SKL_PGA_EVENT, skl_tplg_pga_event},
1941};
1942
1943static const struct snd_soc_tplg_bytes_ext_ops skl_tlv_ops[] = {
1944	{SKL_CONTROL_TYPE_BYTE_TLV, skl_tplg_tlv_control_get,
1945					skl_tplg_tlv_control_set},
1946};
1947
1948static const struct snd_soc_tplg_kcontrol_ops skl_tplg_kcontrol_ops[] = {
1949	{
1950		.id = SKL_CONTROL_TYPE_MIC_SELECT,
1951		.get = skl_tplg_mic_control_get,
1952		.put = skl_tplg_mic_control_set,
1953	},
1954	{
1955		.id = SKL_CONTROL_TYPE_MULTI_IO_SELECT,
1956		.get = skl_tplg_multi_config_get,
1957		.put = skl_tplg_multi_config_set,
1958	},
1959	{
1960		.id = SKL_CONTROL_TYPE_MULTI_IO_SELECT_DMIC,
1961		.get = skl_tplg_multi_config_get_dmic,
1962		.put = skl_tplg_multi_config_set_dmic,
1963	}
1964};
1965
1966static int skl_tplg_fill_pipe_cfg(struct device *dev,
1967			struct skl_pipe *pipe, u32 tkn,
1968			u32 tkn_val, int conf_idx, int dir)
1969{
1970	struct skl_pipe_fmt *fmt;
1971	struct skl_path_config *config;
1972
1973	switch (dir) {
1974	case SKL_DIR_IN:
1975		fmt = &pipe->configs[conf_idx].in_fmt;
1976		break;
1977
1978	case SKL_DIR_OUT:
1979		fmt = &pipe->configs[conf_idx].out_fmt;
1980		break;
1981
1982	default:
1983		dev_err(dev, "Invalid direction: %d\n", dir);
1984		return -EINVAL;
1985	}
1986
1987	config = &pipe->configs[conf_idx];
1988
1989	switch (tkn) {
1990	case SKL_TKN_U32_CFG_FREQ:
1991		fmt->freq = tkn_val;
1992		break;
1993
1994	case SKL_TKN_U8_CFG_CHAN:
1995		fmt->channels = tkn_val;
1996		break;
1997
1998	case SKL_TKN_U8_CFG_BPS:
1999		fmt->bps = tkn_val;
2000		break;
2001
2002	case SKL_TKN_U32_PATH_MEM_PGS:
2003		config->mem_pages = tkn_val;
2004		break;
2005
2006	default:
2007		dev_err(dev, "Invalid token config: %d\n", tkn);
2008		return -EINVAL;
2009	}
2010
2011	return 0;
2012}
2013
2014static int skl_tplg_fill_pipe_tkn(struct device *dev,
2015			struct skl_pipe *pipe, u32 tkn,
2016			u32 tkn_val)
2017{
2018
2019	switch (tkn) {
2020	case SKL_TKN_U32_PIPE_CONN_TYPE:
2021		pipe->conn_type = tkn_val;
2022		break;
2023
2024	case SKL_TKN_U32_PIPE_PRIORITY:
2025		pipe->pipe_priority = tkn_val;
2026		break;
2027
2028	case SKL_TKN_U32_PIPE_MEM_PGS:
2029		pipe->memory_pages = tkn_val;
2030		break;
2031
2032	case SKL_TKN_U32_PMODE:
2033		pipe->lp_mode = tkn_val;
2034		break;
2035
2036	case SKL_TKN_U32_PIPE_DIRECTION:
2037		pipe->direction = tkn_val;
2038		break;
2039
2040	case SKL_TKN_U32_NUM_CONFIGS:
2041		pipe->nr_cfgs = tkn_val;
2042		break;
2043
2044	default:
2045		dev_err(dev, "Token not handled %d\n", tkn);
2046		return -EINVAL;
2047	}
2048
2049	return 0;
2050}
2051
2052/*
2053 * Add pipeline by parsing the relevant tokens
2054 * Return an existing pipe if the pipe already exists.
2055 */
2056static int skl_tplg_add_pipe(struct device *dev,
2057		struct skl_module_cfg *mconfig, struct skl_dev *skl,
2058		struct snd_soc_tplg_vendor_value_elem *tkn_elem)
2059{
2060	struct skl_pipeline *ppl;
2061	struct skl_pipe *pipe;
2062	struct skl_pipe_params *params;
2063
2064	list_for_each_entry(ppl, &skl->ppl_list, node) {
2065		if (ppl->pipe->ppl_id == tkn_elem->value) {
2066			mconfig->pipe = ppl->pipe;
2067			return -EEXIST;
2068		}
2069	}
2070
2071	ppl = devm_kzalloc(dev, sizeof(*ppl), GFP_KERNEL);
2072	if (!ppl)
2073		return -ENOMEM;
2074
2075	pipe = devm_kzalloc(dev, sizeof(*pipe), GFP_KERNEL);
2076	if (!pipe)
2077		return -ENOMEM;
2078
2079	params = devm_kzalloc(dev, sizeof(*params), GFP_KERNEL);
2080	if (!params)
2081		return -ENOMEM;
2082
2083	pipe->p_params = params;
2084	pipe->ppl_id = tkn_elem->value;
2085	INIT_LIST_HEAD(&pipe->w_list);
2086
2087	ppl->pipe = pipe;
2088	list_add(&ppl->node, &skl->ppl_list);
2089
2090	mconfig->pipe = pipe;
2091	mconfig->pipe->state = SKL_PIPE_INVALID;
2092
2093	return 0;
2094}
2095
2096static int skl_tplg_get_uuid(struct device *dev, guid_t *guid,
2097	      struct snd_soc_tplg_vendor_uuid_elem *uuid_tkn)
2098{
2099	if (uuid_tkn->token == SKL_TKN_UUID) {
2100		import_guid(guid, uuid_tkn->uuid);
2101		return 0;
2102	}
2103
2104	dev_err(dev, "Not an UUID token %d\n", uuid_tkn->token);
2105
2106	return -EINVAL;
2107}
2108
2109static int skl_tplg_fill_pin(struct device *dev,
2110			struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2111			struct skl_module_pin *m_pin,
2112			int pin_index)
2113{
2114	int ret;
2115
2116	switch (tkn_elem->token) {
2117	case SKL_TKN_U32_PIN_MOD_ID:
2118		m_pin[pin_index].id.module_id = tkn_elem->value;
2119		break;
2120
2121	case SKL_TKN_U32_PIN_INST_ID:
2122		m_pin[pin_index].id.instance_id = tkn_elem->value;
2123		break;
2124
2125	case SKL_TKN_UUID:
2126		ret = skl_tplg_get_uuid(dev, &m_pin[pin_index].id.mod_uuid,
2127			(struct snd_soc_tplg_vendor_uuid_elem *)tkn_elem);
2128		if (ret < 0)
2129			return ret;
2130
2131		break;
2132
2133	default:
2134		dev_err(dev, "%d Not a pin token\n", tkn_elem->token);
2135		return -EINVAL;
2136	}
2137
2138	return 0;
2139}
2140
2141/*
2142 * Parse for pin config specific tokens to fill up the
2143 * module private data
2144 */
2145static int skl_tplg_fill_pins_info(struct device *dev,
2146		struct skl_module_cfg *mconfig,
2147		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2148		int dir, int pin_count)
2149{
2150	int ret;
2151	struct skl_module_pin *m_pin;
2152
2153	switch (dir) {
2154	case SKL_DIR_IN:
2155		m_pin = mconfig->m_in_pin;
2156		break;
2157
2158	case SKL_DIR_OUT:
2159		m_pin = mconfig->m_out_pin;
2160		break;
2161
2162	default:
2163		dev_err(dev, "Invalid direction value\n");
2164		return -EINVAL;
2165	}
2166
2167	ret = skl_tplg_fill_pin(dev, tkn_elem, m_pin, pin_count);
2168	if (ret < 0)
2169		return ret;
2170
2171	m_pin[pin_count].in_use = false;
2172	m_pin[pin_count].pin_state = SKL_PIN_UNBIND;
2173
2174	return 0;
2175}
2176
2177/*
2178 * Fill up input/output module config format based
2179 * on the direction
2180 */
2181static int skl_tplg_fill_fmt(struct device *dev,
2182		struct skl_module_fmt *dst_fmt,
2183		u32 tkn, u32 value)
2184{
2185	switch (tkn) {
2186	case SKL_TKN_U32_FMT_CH:
2187		dst_fmt->channels  = value;
2188		break;
2189
2190	case SKL_TKN_U32_FMT_FREQ:
2191		dst_fmt->s_freq = value;
2192		break;
2193
2194	case SKL_TKN_U32_FMT_BIT_DEPTH:
2195		dst_fmt->bit_depth = value;
2196		break;
2197
2198	case SKL_TKN_U32_FMT_SAMPLE_SIZE:
2199		dst_fmt->valid_bit_depth = value;
2200		break;
2201
2202	case SKL_TKN_U32_FMT_CH_CONFIG:
2203		dst_fmt->ch_cfg = value;
2204		break;
2205
2206	case SKL_TKN_U32_FMT_INTERLEAVE:
2207		dst_fmt->interleaving_style = value;
2208		break;
2209
2210	case SKL_TKN_U32_FMT_SAMPLE_TYPE:
2211		dst_fmt->sample_type = value;
2212		break;
2213
2214	case SKL_TKN_U32_FMT_CH_MAP:
2215		dst_fmt->ch_map = value;
2216		break;
2217
2218	default:
2219		dev_err(dev, "Invalid token %d\n", tkn);
2220		return -EINVAL;
2221	}
2222
2223	return 0;
2224}
2225
2226static int skl_tplg_widget_fill_fmt(struct device *dev,
2227		struct skl_module_iface *fmt,
2228		u32 tkn, u32 val, u32 dir, int fmt_idx)
2229{
2230	struct skl_module_fmt *dst_fmt;
2231
2232	if (!fmt)
2233		return -EINVAL;
2234
2235	switch (dir) {
2236	case SKL_DIR_IN:
2237		dst_fmt = &fmt->inputs[fmt_idx].fmt;
2238		break;
2239
2240	case SKL_DIR_OUT:
2241		dst_fmt = &fmt->outputs[fmt_idx].fmt;
2242		break;
2243
2244	default:
2245		dev_err(dev, "Invalid direction: %d\n", dir);
2246		return -EINVAL;
2247	}
2248
2249	return skl_tplg_fill_fmt(dev, dst_fmt, tkn, val);
2250}
2251
2252static void skl_tplg_fill_pin_dynamic_val(
2253		struct skl_module_pin *mpin, u32 pin_count, u32 value)
2254{
2255	int i;
2256
2257	for (i = 0; i < pin_count; i++)
2258		mpin[i].is_dynamic = value;
2259}
2260
2261/*
2262 * Resource table in the manifest has pin specific resources
2263 * like pin and pin buffer size
2264 */
2265static int skl_tplg_manifest_pin_res_tkn(struct device *dev,
2266		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2267		struct skl_module_res *res, int pin_idx, int dir)
2268{
2269	struct skl_module_pin_resources *m_pin;
2270
2271	switch (dir) {
2272	case SKL_DIR_IN:
2273		m_pin = &res->input[pin_idx];
2274		break;
2275
2276	case SKL_DIR_OUT:
2277		m_pin = &res->output[pin_idx];
2278		break;
2279
2280	default:
2281		dev_err(dev, "Invalid pin direction: %d\n", dir);
2282		return -EINVAL;
2283	}
2284
2285	switch (tkn_elem->token) {
2286	case SKL_TKN_MM_U32_RES_PIN_ID:
2287		m_pin->pin_index = tkn_elem->value;
2288		break;
2289
2290	case SKL_TKN_MM_U32_PIN_BUF:
2291		m_pin->buf_size = tkn_elem->value;
2292		break;
2293
2294	default:
2295		dev_err(dev, "Invalid token: %d\n", tkn_elem->token);
2296		return -EINVAL;
2297	}
2298
2299	return 0;
2300}
2301
2302/*
2303 * Fill module specific resources from the manifest's resource
2304 * table like CPS, DMA size, mem_pages.
2305 */
2306static int skl_tplg_fill_res_tkn(struct device *dev,
2307		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2308		struct skl_module_res *res,
2309		int pin_idx, int dir)
2310{
2311	int ret, tkn_count = 0;
2312
2313	if (!res)
2314		return -EINVAL;
2315
2316	switch (tkn_elem->token) {
2317	case SKL_TKN_MM_U32_DMA_SIZE:
2318		res->dma_buffer_size = tkn_elem->value;
2319		break;
2320
2321	case SKL_TKN_MM_U32_CPC:
2322		res->cpc = tkn_elem->value;
2323		break;
2324
2325	case SKL_TKN_U32_MEM_PAGES:
2326		res->is_pages = tkn_elem->value;
2327		break;
2328
2329	case SKL_TKN_U32_OBS:
2330		res->obs = tkn_elem->value;
2331		break;
2332
2333	case SKL_TKN_U32_IBS:
2334		res->ibs = tkn_elem->value;
2335		break;
2336
2337	case SKL_TKN_MM_U32_RES_PIN_ID:
2338	case SKL_TKN_MM_U32_PIN_BUF:
2339		ret = skl_tplg_manifest_pin_res_tkn(dev, tkn_elem, res,
2340						    pin_idx, dir);
2341		if (ret < 0)
2342			return ret;
2343		break;
2344
2345	case SKL_TKN_MM_U32_CPS:
2346	case SKL_TKN_U32_MAX_MCPS:
2347		/* ignore unused tokens */
2348		break;
2349
2350	default:
2351		dev_err(dev, "Not a res type token: %d", tkn_elem->token);
2352		return -EINVAL;
2353
2354	}
2355	tkn_count++;
2356
2357	return tkn_count;
2358}
2359
2360/*
2361 * Parse tokens to fill up the module private data
2362 */
2363static int skl_tplg_get_token(struct device *dev,
2364		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2365		struct skl_dev *skl, struct skl_module_cfg *mconfig)
2366{
2367	int tkn_count = 0;
2368	int ret;
2369	static int is_pipe_exists;
2370	static int pin_index, dir, conf_idx;
2371	struct skl_module_iface *iface = NULL;
2372	struct skl_module_res *res = NULL;
2373	int res_idx = mconfig->res_idx;
2374	int fmt_idx = mconfig->fmt_idx;
2375
2376	/*
2377	 * If the manifest structure contains no modules, fill all
2378	 * the module data to 0th index.
2379	 * res_idx and fmt_idx are default set to 0.
2380	 */
2381	if (skl->nr_modules == 0) {
2382		res = &mconfig->module->resources[res_idx];
2383		iface = &mconfig->module->formats[fmt_idx];
2384	}
2385
2386	if (tkn_elem->token > SKL_TKN_MAX)
2387		return -EINVAL;
2388
2389	switch (tkn_elem->token) {
2390	case SKL_TKN_U8_IN_QUEUE_COUNT:
2391		mconfig->module->max_input_pins = tkn_elem->value;
2392		break;
2393
2394	case SKL_TKN_U8_OUT_QUEUE_COUNT:
2395		mconfig->module->max_output_pins = tkn_elem->value;
2396		break;
2397
2398	case SKL_TKN_U8_DYN_IN_PIN:
2399		if (!mconfig->m_in_pin)
2400			mconfig->m_in_pin =
2401				devm_kcalloc(dev, MAX_IN_QUEUE,
2402					     sizeof(*mconfig->m_in_pin),
2403					     GFP_KERNEL);
2404		if (!mconfig->m_in_pin)
2405			return -ENOMEM;
2406
2407		skl_tplg_fill_pin_dynamic_val(mconfig->m_in_pin, MAX_IN_QUEUE,
2408					      tkn_elem->value);
2409		break;
2410
2411	case SKL_TKN_U8_DYN_OUT_PIN:
2412		if (!mconfig->m_out_pin)
2413			mconfig->m_out_pin =
2414				devm_kcalloc(dev, MAX_IN_QUEUE,
2415					     sizeof(*mconfig->m_in_pin),
2416					     GFP_KERNEL);
2417		if (!mconfig->m_out_pin)
2418			return -ENOMEM;
2419
2420		skl_tplg_fill_pin_dynamic_val(mconfig->m_out_pin, MAX_OUT_QUEUE,
2421					      tkn_elem->value);
2422		break;
2423
2424	case SKL_TKN_U8_TIME_SLOT:
2425		mconfig->time_slot = tkn_elem->value;
2426		break;
2427
2428	case SKL_TKN_U8_CORE_ID:
2429		mconfig->core_id = tkn_elem->value;
2430		break;
2431
2432	case SKL_TKN_U8_MOD_TYPE:
2433		mconfig->m_type = tkn_elem->value;
2434		break;
2435
2436	case SKL_TKN_U8_DEV_TYPE:
2437		mconfig->dev_type = tkn_elem->value;
2438		break;
2439
2440	case SKL_TKN_U8_HW_CONN_TYPE:
2441		mconfig->hw_conn_type = tkn_elem->value;
2442		break;
2443
2444	case SKL_TKN_U16_MOD_INST_ID:
2445		mconfig->id.instance_id =
2446		tkn_elem->value;
2447		break;
2448
2449	case SKL_TKN_U32_MEM_PAGES:
2450	case SKL_TKN_U32_MAX_MCPS:
2451	case SKL_TKN_U32_OBS:
2452	case SKL_TKN_U32_IBS:
2453		ret = skl_tplg_fill_res_tkn(dev, tkn_elem, res, pin_index, dir);
2454		if (ret < 0)
2455			return ret;
2456
2457		break;
2458
2459	case SKL_TKN_U32_VBUS_ID:
2460		mconfig->vbus_id = tkn_elem->value;
2461		break;
2462
2463	case SKL_TKN_U32_PARAMS_FIXUP:
2464		mconfig->params_fixup = tkn_elem->value;
2465		break;
2466
2467	case SKL_TKN_U32_CONVERTER:
2468		mconfig->converter = tkn_elem->value;
2469		break;
2470
2471	case SKL_TKN_U32_D0I3_CAPS:
2472		mconfig->d0i3_caps = tkn_elem->value;
2473		break;
2474
2475	case SKL_TKN_U32_PIPE_ID:
2476		ret = skl_tplg_add_pipe(dev,
2477				mconfig, skl, tkn_elem);
2478
2479		if (ret < 0) {
2480			if (ret == -EEXIST) {
2481				is_pipe_exists = 1;
2482				break;
2483			}
2484			return is_pipe_exists;
2485		}
2486
2487		break;
2488
2489	case SKL_TKN_U32_PIPE_CONFIG_ID:
2490		conf_idx = tkn_elem->value;
2491		break;
2492
2493	case SKL_TKN_U32_PIPE_CONN_TYPE:
2494	case SKL_TKN_U32_PIPE_PRIORITY:
2495	case SKL_TKN_U32_PIPE_MEM_PGS:
2496	case SKL_TKN_U32_PMODE:
2497	case SKL_TKN_U32_PIPE_DIRECTION:
2498	case SKL_TKN_U32_NUM_CONFIGS:
2499		if (is_pipe_exists) {
2500			ret = skl_tplg_fill_pipe_tkn(dev, mconfig->pipe,
2501					tkn_elem->token, tkn_elem->value);
2502			if (ret < 0)
2503				return ret;
2504		}
2505
2506		break;
2507
2508	case SKL_TKN_U32_PATH_MEM_PGS:
2509	case SKL_TKN_U32_CFG_FREQ:
2510	case SKL_TKN_U8_CFG_CHAN:
2511	case SKL_TKN_U8_CFG_BPS:
2512		if (mconfig->pipe->nr_cfgs) {
2513			ret = skl_tplg_fill_pipe_cfg(dev, mconfig->pipe,
2514					tkn_elem->token, tkn_elem->value,
2515					conf_idx, dir);
2516			if (ret < 0)
2517				return ret;
2518		}
2519		break;
2520
2521	case SKL_TKN_CFG_MOD_RES_ID:
2522		mconfig->mod_cfg[conf_idx].res_idx = tkn_elem->value;
2523		break;
2524
2525	case SKL_TKN_CFG_MOD_FMT_ID:
2526		mconfig->mod_cfg[conf_idx].fmt_idx = tkn_elem->value;
2527		break;
2528
2529	/*
2530	 * SKL_TKN_U32_DIR_PIN_COUNT token has the value for both
2531	 * direction and the pin count. The first four bits represent
2532	 * direction and next four the pin count.
2533	 */
2534	case SKL_TKN_U32_DIR_PIN_COUNT:
2535		dir = tkn_elem->value & SKL_IN_DIR_BIT_MASK;
2536		pin_index = (tkn_elem->value &
2537			SKL_PIN_COUNT_MASK) >> 4;
2538
2539		break;
2540
2541	case SKL_TKN_U32_FMT_CH:
2542	case SKL_TKN_U32_FMT_FREQ:
2543	case SKL_TKN_U32_FMT_BIT_DEPTH:
2544	case SKL_TKN_U32_FMT_SAMPLE_SIZE:
2545	case SKL_TKN_U32_FMT_CH_CONFIG:
2546	case SKL_TKN_U32_FMT_INTERLEAVE:
2547	case SKL_TKN_U32_FMT_SAMPLE_TYPE:
2548	case SKL_TKN_U32_FMT_CH_MAP:
2549		ret = skl_tplg_widget_fill_fmt(dev, iface, tkn_elem->token,
2550				tkn_elem->value, dir, pin_index);
2551
2552		if (ret < 0)
2553			return ret;
2554
2555		break;
2556
2557	case SKL_TKN_U32_PIN_MOD_ID:
2558	case SKL_TKN_U32_PIN_INST_ID:
2559	case SKL_TKN_UUID:
2560		ret = skl_tplg_fill_pins_info(dev,
2561				mconfig, tkn_elem, dir,
2562				pin_index);
2563		if (ret < 0)
2564			return ret;
2565
2566		break;
2567
2568	case SKL_TKN_U32_CAPS_SIZE:
2569		mconfig->formats_config.caps_size =
2570			tkn_elem->value;
2571
2572		break;
2573
2574	case SKL_TKN_U32_CAPS_SET_PARAMS:
2575		mconfig->formats_config.set_params =
2576				tkn_elem->value;
2577		break;
2578
2579	case SKL_TKN_U32_CAPS_PARAMS_ID:
2580		mconfig->formats_config.param_id =
2581				tkn_elem->value;
2582		break;
2583
2584	case SKL_TKN_U32_PROC_DOMAIN:
2585		mconfig->domain =
2586			tkn_elem->value;
2587
2588		break;
2589
2590	case SKL_TKN_U32_DMA_BUF_SIZE:
2591		mconfig->dma_buffer_size = tkn_elem->value;
2592		break;
2593
2594	case SKL_TKN_U8_IN_PIN_TYPE:
2595	case SKL_TKN_U8_OUT_PIN_TYPE:
2596	case SKL_TKN_U8_CONN_TYPE:
2597		break;
2598
2599	default:
2600		dev_err(dev, "Token %d not handled\n",
2601				tkn_elem->token);
2602		return -EINVAL;
2603	}
2604
2605	tkn_count++;
2606
2607	return tkn_count;
2608}
2609
2610/*
2611 * Parse the vendor array for specific tokens to construct
2612 * module private data
2613 */
2614static int skl_tplg_get_tokens(struct device *dev,
2615		char *pvt_data,	struct skl_dev *skl,
2616		struct skl_module_cfg *mconfig, int block_size)
2617{
2618	struct snd_soc_tplg_vendor_array *array;
2619	struct snd_soc_tplg_vendor_value_elem *tkn_elem;
2620	int tkn_count = 0, ret;
2621	int off = 0, tuple_size = 0;
2622	bool is_module_guid = true;
2623
2624	if (block_size <= 0)
2625		return -EINVAL;
2626
2627	while (tuple_size < block_size) {
2628		array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off);
2629
2630		off += array->size;
2631
2632		switch (array->type) {
2633		case SND_SOC_TPLG_TUPLE_TYPE_STRING:
2634			dev_warn(dev, "no string tokens expected for skl tplg\n");
2635			continue;
2636
2637		case SND_SOC_TPLG_TUPLE_TYPE_UUID:
2638			if (is_module_guid) {
2639				ret = skl_tplg_get_uuid(dev, (guid_t *)mconfig->guid,
2640							array->uuid);
2641				is_module_guid = false;
2642			} else {
2643				ret = skl_tplg_get_token(dev, array->value, skl,
2644							 mconfig);
2645			}
2646
2647			if (ret < 0)
2648				return ret;
2649
2650			tuple_size += sizeof(*array->uuid);
2651
2652			continue;
2653
2654		default:
2655			tkn_elem = array->value;
2656			tkn_count = 0;
2657			break;
2658		}
2659
2660		while (tkn_count <= (array->num_elems - 1)) {
2661			ret = skl_tplg_get_token(dev, tkn_elem,
2662					skl, mconfig);
2663
2664			if (ret < 0)
2665				return ret;
2666
2667			tkn_count = tkn_count + ret;
2668			tkn_elem++;
2669		}
2670
2671		tuple_size += tkn_count * sizeof(*tkn_elem);
2672	}
2673
2674	return off;
2675}
2676
2677/*
2678 * Every data block is preceded by a descriptor to read the number
2679 * of data blocks, they type of the block and it's size
2680 */
2681static int skl_tplg_get_desc_blocks(struct device *dev,
2682		struct snd_soc_tplg_vendor_array *array)
2683{
2684	struct snd_soc_tplg_vendor_value_elem *tkn_elem;
2685
2686	tkn_elem = array->value;
2687
2688	switch (tkn_elem->token) {
2689	case SKL_TKN_U8_NUM_BLOCKS:
2690	case SKL_TKN_U8_BLOCK_TYPE:
2691	case SKL_TKN_U16_BLOCK_SIZE:
2692		return tkn_elem->value;
2693
2694	default:
2695		dev_err(dev, "Invalid descriptor token %d\n", tkn_elem->token);
2696		break;
2697	}
2698
2699	return -EINVAL;
2700}
2701
2702/* Functions to parse private data from configuration file format v4 */
2703
2704/*
2705 * Add pipeline from topology binary into driver pipeline list
2706 *
2707 * If already added we return that instance
2708 * Otherwise we create a new instance and add into driver list
2709 */
2710static int skl_tplg_add_pipe_v4(struct device *dev,
2711			struct skl_module_cfg *mconfig, struct skl_dev *skl,
2712			struct skl_dfw_v4_pipe *dfw_pipe)
2713{
2714	struct skl_pipeline *ppl;
2715	struct skl_pipe *pipe;
2716	struct skl_pipe_params *params;
2717
2718	list_for_each_entry(ppl, &skl->ppl_list, node) {
2719		if (ppl->pipe->ppl_id == dfw_pipe->pipe_id) {
2720			mconfig->pipe = ppl->pipe;
2721			return 0;
2722		}
2723	}
2724
2725	ppl = devm_kzalloc(dev, sizeof(*ppl), GFP_KERNEL);
2726	if (!ppl)
2727		return -ENOMEM;
2728
2729	pipe = devm_kzalloc(dev, sizeof(*pipe), GFP_KERNEL);
2730	if (!pipe)
2731		return -ENOMEM;
2732
2733	params = devm_kzalloc(dev, sizeof(*params), GFP_KERNEL);
2734	if (!params)
2735		return -ENOMEM;
2736
2737	pipe->ppl_id = dfw_pipe->pipe_id;
2738	pipe->memory_pages = dfw_pipe->memory_pages;
2739	pipe->pipe_priority = dfw_pipe->pipe_priority;
2740	pipe->conn_type = dfw_pipe->conn_type;
2741	pipe->state = SKL_PIPE_INVALID;
2742	pipe->p_params = params;
2743	INIT_LIST_HEAD(&pipe->w_list);
2744
2745	ppl->pipe = pipe;
2746	list_add(&ppl->node, &skl->ppl_list);
2747
2748	mconfig->pipe = pipe;
2749
2750	return 0;
2751}
2752
2753static void skl_fill_module_pin_info_v4(struct skl_dfw_v4_module_pin *dfw_pin,
2754					struct skl_module_pin *m_pin,
2755					bool is_dynamic, int max_pin)
2756{
2757	int i;
2758
2759	for (i = 0; i < max_pin; i++) {
2760		m_pin[i].id.module_id = dfw_pin[i].module_id;
2761		m_pin[i].id.instance_id = dfw_pin[i].instance_id;
2762		m_pin[i].in_use = false;
2763		m_pin[i].is_dynamic = is_dynamic;
2764		m_pin[i].pin_state = SKL_PIN_UNBIND;
2765	}
2766}
2767
2768static void skl_tplg_fill_fmt_v4(struct skl_module_pin_fmt *dst_fmt,
2769				 struct skl_dfw_v4_module_fmt *src_fmt,
2770				 int pins)
2771{
2772	int i;
2773
2774	for (i = 0; i < pins; i++) {
2775		dst_fmt[i].fmt.channels  = src_fmt[i].channels;
2776		dst_fmt[i].fmt.s_freq = src_fmt[i].freq;
2777		dst_fmt[i].fmt.bit_depth = src_fmt[i].bit_depth;
2778		dst_fmt[i].fmt.valid_bit_depth = src_fmt[i].valid_bit_depth;
2779		dst_fmt[i].fmt.ch_cfg = src_fmt[i].ch_cfg;
2780		dst_fmt[i].fmt.ch_map = src_fmt[i].ch_map;
2781		dst_fmt[i].fmt.interleaving_style =
2782						src_fmt[i].interleaving_style;
2783		dst_fmt[i].fmt.sample_type = src_fmt[i].sample_type;
2784	}
2785}
2786
2787static int skl_tplg_get_pvt_data_v4(struct snd_soc_tplg_dapm_widget *tplg_w,
2788				    struct skl_dev *skl, struct device *dev,
2789				    struct skl_module_cfg *mconfig)
2790{
2791	struct skl_dfw_v4_module *dfw =
2792				(struct skl_dfw_v4_module *)tplg_w->priv.data;
2793	int ret;
2794
2795	dev_dbg(dev, "Parsing Skylake v4 widget topology data\n");
2796
2797	ret = guid_parse(dfw->uuid, (guid_t *)mconfig->guid);
2798	if (ret)
2799		return ret;
2800	mconfig->id.module_id = -1;
2801	mconfig->id.instance_id = dfw->instance_id;
2802	mconfig->module->resources[0].cpc = dfw->max_mcps / 1000;
2803	mconfig->module->resources[0].ibs = dfw->ibs;
2804	mconfig->module->resources[0].obs = dfw->obs;
2805	mconfig->core_id = dfw->core_id;
2806	mconfig->module->max_input_pins = dfw->max_in_queue;
2807	mconfig->module->max_output_pins = dfw->max_out_queue;
2808	mconfig->module->loadable = dfw->is_loadable;
2809	skl_tplg_fill_fmt_v4(mconfig->module->formats[0].inputs, dfw->in_fmt,
2810			     MAX_IN_QUEUE);
2811	skl_tplg_fill_fmt_v4(mconfig->module->formats[0].outputs, dfw->out_fmt,
2812			     MAX_OUT_QUEUE);
2813
2814	mconfig->params_fixup = dfw->params_fixup;
2815	mconfig->converter = dfw->converter;
2816	mconfig->m_type = dfw->module_type;
2817	mconfig->vbus_id = dfw->vbus_id;
2818	mconfig->module->resources[0].is_pages = dfw->mem_pages;
2819
2820	ret = skl_tplg_add_pipe_v4(dev, mconfig, skl, &dfw->pipe);
2821	if (ret)
2822		return ret;
2823
2824	mconfig->dev_type = dfw->dev_type;
2825	mconfig->hw_conn_type = dfw->hw_conn_type;
2826	mconfig->time_slot = dfw->time_slot;
2827	mconfig->formats_config.caps_size = dfw->caps.caps_size;
2828
2829	mconfig->m_in_pin = devm_kcalloc(dev,
2830				MAX_IN_QUEUE, sizeof(*mconfig->m_in_pin),
2831				GFP_KERNEL);
2832	if (!mconfig->m_in_pin)
2833		return -ENOMEM;
2834
2835	mconfig->m_out_pin = devm_kcalloc(dev,
2836				MAX_OUT_QUEUE, sizeof(*mconfig->m_out_pin),
2837				GFP_KERNEL);
2838	if (!mconfig->m_out_pin)
2839		return -ENOMEM;
2840
2841	skl_fill_module_pin_info_v4(dfw->in_pin, mconfig->m_in_pin,
2842				    dfw->is_dynamic_in_pin,
2843				    mconfig->module->max_input_pins);
2844	skl_fill_module_pin_info_v4(dfw->out_pin, mconfig->m_out_pin,
2845				    dfw->is_dynamic_out_pin,
2846				    mconfig->module->max_output_pins);
2847
2848	if (mconfig->formats_config.caps_size) {
2849		mconfig->formats_config.set_params = dfw->caps.set_params;
2850		mconfig->formats_config.param_id = dfw->caps.param_id;
2851		mconfig->formats_config.caps =
2852		devm_kzalloc(dev, mconfig->formats_config.caps_size,
2853			     GFP_KERNEL);
2854		if (!mconfig->formats_config.caps)
2855			return -ENOMEM;
2856		memcpy(mconfig->formats_config.caps, dfw->caps.caps,
2857		       dfw->caps.caps_size);
2858	}
2859
2860	return 0;
2861}
2862
2863/*
2864 * Parse the private data for the token and corresponding value.
2865 * The private data can have multiple data blocks. So, a data block
2866 * is preceded by a descriptor for number of blocks and a descriptor
2867 * for the type and size of the suceeding data block.
2868 */
2869static int skl_tplg_get_pvt_data(struct snd_soc_tplg_dapm_widget *tplg_w,
2870				struct skl_dev *skl, struct device *dev,
2871				struct skl_module_cfg *mconfig)
2872{
2873	struct snd_soc_tplg_vendor_array *array;
2874	int num_blocks, block_size, block_type, off = 0;
2875	char *data;
2876	int ret;
2877
2878	/*
2879	 * v4 configuration files have a valid UUID at the start of
2880	 * the widget's private data.
2881	 */
2882	if (uuid_is_valid((char *)tplg_w->priv.data))
2883		return skl_tplg_get_pvt_data_v4(tplg_w, skl, dev, mconfig);
2884
2885	/* Read the NUM_DATA_BLOCKS descriptor */
2886	array = (struct snd_soc_tplg_vendor_array *)tplg_w->priv.data;
2887	ret = skl_tplg_get_desc_blocks(dev, array);
2888	if (ret < 0)
2889		return ret;
2890	num_blocks = ret;
2891
2892	off += array->size;
2893	/* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */
2894	while (num_blocks > 0) {
2895		array = (struct snd_soc_tplg_vendor_array *)
2896				(tplg_w->priv.data + off);
2897
2898		ret = skl_tplg_get_desc_blocks(dev, array);
2899
2900		if (ret < 0)
2901			return ret;
2902		block_type = ret;
2903		off += array->size;
2904
2905		array = (struct snd_soc_tplg_vendor_array *)
2906			(tplg_w->priv.data + off);
2907
2908		ret = skl_tplg_get_desc_blocks(dev, array);
2909
2910		if (ret < 0)
2911			return ret;
2912		block_size = ret;
2913		off += array->size;
2914
2915		array = (struct snd_soc_tplg_vendor_array *)
2916			(tplg_w->priv.data + off);
2917
2918		data = (tplg_w->priv.data + off);
2919
2920		if (block_type == SKL_TYPE_TUPLE) {
2921			ret = skl_tplg_get_tokens(dev, data,
2922					skl, mconfig, block_size);
2923
2924			if (ret < 0)
2925				return ret;
2926
2927			--num_blocks;
2928		} else {
2929			if (mconfig->formats_config.caps_size > 0)
2930				memcpy(mconfig->formats_config.caps, data,
2931					mconfig->formats_config.caps_size);
2932			--num_blocks;
2933			ret = mconfig->formats_config.caps_size;
2934		}
2935		off += ret;
2936	}
2937
2938	return 0;
2939}
2940
2941static void skl_clear_pin_config(struct snd_soc_component *component,
2942				struct snd_soc_dapm_widget *w)
2943{
2944	int i;
2945	struct skl_module_cfg *mconfig;
2946	struct skl_pipe *pipe;
2947
2948	if (!strncmp(w->dapm->component->name, component->name,
2949					strlen(component->name))) {
2950		mconfig = w->priv;
2951		pipe = mconfig->pipe;
2952		for (i = 0; i < mconfig->module->max_input_pins; i++) {
2953			mconfig->m_in_pin[i].in_use = false;
2954			mconfig->m_in_pin[i].pin_state = SKL_PIN_UNBIND;
2955		}
2956		for (i = 0; i < mconfig->module->max_output_pins; i++) {
2957			mconfig->m_out_pin[i].in_use = false;
2958			mconfig->m_out_pin[i].pin_state = SKL_PIN_UNBIND;
2959		}
2960		pipe->state = SKL_PIPE_INVALID;
2961		mconfig->m_state = SKL_MODULE_UNINIT;
2962	}
2963}
2964
2965void skl_cleanup_resources(struct skl_dev *skl)
2966{
2967	struct snd_soc_component *soc_component = skl->component;
2968	struct snd_soc_dapm_widget *w;
2969	struct snd_soc_card *card;
2970
2971	if (soc_component == NULL)
2972		return;
2973
2974	card = soc_component->card;
2975	if (!card || !card->instantiated)
2976		return;
2977
2978	list_for_each_entry(w, &card->widgets, list) {
2979		if (is_skl_dsp_widget_type(w, skl->dev) && w->priv != NULL)
2980			skl_clear_pin_config(soc_component, w);
2981	}
2982
2983	skl_clear_module_cnt(skl->dsp);
2984}
2985
2986/*
2987 * Topology core widget load callback
2988 *
2989 * This is used to save the private data for each widget which gives
2990 * information to the driver about module and pipeline parameters which DSP
2991 * FW expects like ids, resource values, formats etc
2992 */
2993static int skl_tplg_widget_load(struct snd_soc_component *cmpnt, int index,
2994				struct snd_soc_dapm_widget *w,
2995				struct snd_soc_tplg_dapm_widget *tplg_w)
2996{
2997	int ret;
2998	struct hdac_bus *bus = snd_soc_component_get_drvdata(cmpnt);
2999	struct skl_dev *skl = bus_to_skl(bus);
3000	struct skl_module_cfg *mconfig;
3001
3002	if (!tplg_w->priv.size)
3003		goto bind_event;
3004
3005	mconfig = devm_kzalloc(bus->dev, sizeof(*mconfig), GFP_KERNEL);
3006
3007	if (!mconfig)
3008		return -ENOMEM;
3009
3010	if (skl->nr_modules == 0) {
3011		mconfig->module = devm_kzalloc(bus->dev,
3012				sizeof(*mconfig->module), GFP_KERNEL);
3013		if (!mconfig->module)
3014			return -ENOMEM;
3015	}
3016
3017	w->priv = mconfig;
3018
3019	/*
3020	 * module binary can be loaded later, so set it to query when
3021	 * module is load for a use case
3022	 */
3023	mconfig->id.module_id = -1;
3024
3025	/* Parse private data for tuples */
3026	ret = skl_tplg_get_pvt_data(tplg_w, skl, bus->dev, mconfig);
3027	if (ret < 0)
3028		return ret;
3029
3030	skl_debug_init_module(skl->debugfs, w, mconfig);
3031
3032bind_event:
3033	if (tplg_w->event_type == 0) {
3034		dev_dbg(bus->dev, "ASoC: No event handler required\n");
3035		return 0;
3036	}
3037
3038	ret = snd_soc_tplg_widget_bind_event(w, skl_tplg_widget_ops,
3039					ARRAY_SIZE(skl_tplg_widget_ops),
3040					tplg_w->event_type);
3041
3042	if (ret) {
3043		dev_err(bus->dev, "%s: No matching event handlers found for %d\n",
3044					__func__, tplg_w->event_type);
3045		return -EINVAL;
3046	}
3047
3048	return 0;
3049}
3050
3051static int skl_init_algo_data(struct device *dev, struct soc_bytes_ext *be,
3052					struct snd_soc_tplg_bytes_control *bc)
3053{
3054	struct skl_algo_data *ac;
3055	struct skl_dfw_algo_data *dfw_ac =
3056				(struct skl_dfw_algo_data *)bc->priv.data;
3057
3058	ac = devm_kzalloc(dev, sizeof(*ac), GFP_KERNEL);
3059	if (!ac)
3060		return -ENOMEM;
3061
3062	/* Fill private data */
3063	ac->max = dfw_ac->max;
3064	ac->param_id = dfw_ac->param_id;
3065	ac->set_params = dfw_ac->set_params;
3066	ac->size = dfw_ac->max;
3067
3068	if (ac->max) {
3069		ac->params = devm_kzalloc(dev, ac->max, GFP_KERNEL);
3070		if (!ac->params)
3071			return -ENOMEM;
3072
3073		memcpy(ac->params, dfw_ac->params, ac->max);
3074	}
3075
3076	be->dobj.private  = ac;
3077	return 0;
3078}
3079
3080static int skl_init_enum_data(struct device *dev, struct soc_enum *se,
3081				struct snd_soc_tplg_enum_control *ec)
3082{
3083
3084	void *data;
3085
3086	if (ec->priv.size) {
3087		data = devm_kzalloc(dev, sizeof(ec->priv.size), GFP_KERNEL);
3088		if (!data)
3089			return -ENOMEM;
3090		memcpy(data, ec->priv.data, ec->priv.size);
3091		se->dobj.private = data;
3092	}
3093
3094	return 0;
3095
3096}
3097
3098static int skl_tplg_control_load(struct snd_soc_component *cmpnt,
3099				int index,
3100				struct snd_kcontrol_new *kctl,
3101				struct snd_soc_tplg_ctl_hdr *hdr)
3102{
3103	struct soc_bytes_ext *sb;
3104	struct snd_soc_tplg_bytes_control *tplg_bc;
3105	struct snd_soc_tplg_enum_control *tplg_ec;
3106	struct hdac_bus *bus  = snd_soc_component_get_drvdata(cmpnt);
3107	struct soc_enum *se;
3108
3109	switch (hdr->ops.info) {
3110	case SND_SOC_TPLG_CTL_BYTES:
3111		tplg_bc = container_of(hdr,
3112				struct snd_soc_tplg_bytes_control, hdr);
3113		if (kctl->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
3114			sb = (struct soc_bytes_ext *)kctl->private_value;
3115			if (tplg_bc->priv.size)
3116				return skl_init_algo_data(
3117						bus->dev, sb, tplg_bc);
3118		}
3119		break;
3120
3121	case SND_SOC_TPLG_CTL_ENUM:
3122		tplg_ec = container_of(hdr,
3123				struct snd_soc_tplg_enum_control, hdr);
3124		if (kctl->access & SNDRV_CTL_ELEM_ACCESS_READ) {
3125			se = (struct soc_enum *)kctl->private_value;
3126			if (tplg_ec->priv.size)
3127				skl_init_enum_data(bus->dev, se, tplg_ec);
3128		}
3129
3130		/*
3131		 * now that the control initializations are done, remove
3132		 * write permission for the DMIC configuration enums to
3133		 * avoid conflicts between NHLT settings and user interaction
3134		 */
3135
3136		if (hdr->ops.get == SKL_CONTROL_TYPE_MULTI_IO_SELECT_DMIC)
3137			kctl->access = SNDRV_CTL_ELEM_ACCESS_READ;
3138
3139		break;
3140
3141	default:
3142		dev_dbg(bus->dev, "Control load not supported %d:%d:%d\n",
3143			hdr->ops.get, hdr->ops.put, hdr->ops.info);
3144		break;
3145	}
3146
3147	return 0;
3148}
3149
3150static int skl_tplg_fill_str_mfest_tkn(struct device *dev,
3151		struct snd_soc_tplg_vendor_string_elem *str_elem,
3152		struct skl_dev *skl)
3153{
3154	int tkn_count = 0;
3155	static int ref_count;
3156
3157	switch (str_elem->token) {
3158	case SKL_TKN_STR_LIB_NAME:
3159		if (ref_count > skl->lib_count - 1) {
3160			ref_count = 0;
3161			return -EINVAL;
3162		}
3163
3164		strncpy(skl->lib_info[ref_count].name,
3165			str_elem->string,
3166			ARRAY_SIZE(skl->lib_info[ref_count].name));
3167		ref_count++;
3168		break;
3169
3170	default:
3171		dev_err(dev, "Not a string token %d\n", str_elem->token);
3172		break;
3173	}
3174	tkn_count++;
3175
3176	return tkn_count;
3177}
3178
3179static int skl_tplg_get_str_tkn(struct device *dev,
3180		struct snd_soc_tplg_vendor_array *array,
3181		struct skl_dev *skl)
3182{
3183	int tkn_count = 0, ret;
3184	struct snd_soc_tplg_vendor_string_elem *str_elem;
3185
3186	str_elem = (struct snd_soc_tplg_vendor_string_elem *)array->value;
3187	while (tkn_count < array->num_elems) {
3188		ret = skl_tplg_fill_str_mfest_tkn(dev, str_elem, skl);
3189		str_elem++;
3190
3191		if (ret < 0)
3192			return ret;
3193
3194		tkn_count = tkn_count + ret;
3195	}
3196
3197	return tkn_count;
3198}
3199
3200static int skl_tplg_manifest_fill_fmt(struct device *dev,
3201		struct skl_module_iface *fmt,
3202		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
3203		u32 dir, int fmt_idx)
3204{
3205	struct skl_module_pin_fmt *dst_fmt;
3206	struct skl_module_fmt *mod_fmt;
3207	int ret;
3208
3209	if (!fmt)
3210		return -EINVAL;
3211
3212	switch (dir) {
3213	case SKL_DIR_IN:
3214		dst_fmt = &fmt->inputs[fmt_idx];
3215		break;
3216
3217	case SKL_DIR_OUT:
3218		dst_fmt = &fmt->outputs[fmt_idx];
3219		break;
3220
3221	default:
3222		dev_err(dev, "Invalid direction: %d\n", dir);
3223		return -EINVAL;
3224	}
3225
3226	mod_fmt = &dst_fmt->fmt;
3227
3228	switch (tkn_elem->token) {
3229	case SKL_TKN_MM_U32_INTF_PIN_ID:
3230		dst_fmt->id = tkn_elem->value;
3231		break;
3232
3233	default:
3234		ret = skl_tplg_fill_fmt(dev, mod_fmt, tkn_elem->token,
3235					tkn_elem->value);
3236		if (ret < 0)
3237			return ret;
3238		break;
3239	}
3240
3241	return 0;
3242}
3243
3244static int skl_tplg_fill_mod_info(struct device *dev,
3245		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
3246		struct skl_module *mod)
3247{
3248
3249	if (!mod)
3250		return -EINVAL;
3251
3252	switch (tkn_elem->token) {
3253	case SKL_TKN_U8_IN_PIN_TYPE:
3254		mod->input_pin_type = tkn_elem->value;
3255		break;
3256
3257	case SKL_TKN_U8_OUT_PIN_TYPE:
3258		mod->output_pin_type = tkn_elem->value;
3259		break;
3260
3261	case SKL_TKN_U8_IN_QUEUE_COUNT:
3262		mod->max_input_pins = tkn_elem->value;
3263		break;
3264
3265	case SKL_TKN_U8_OUT_QUEUE_COUNT:
3266		mod->max_output_pins = tkn_elem->value;
3267		break;
3268
3269	case SKL_TKN_MM_U8_NUM_RES:
3270		mod->nr_resources = tkn_elem->value;
3271		break;
3272
3273	case SKL_TKN_MM_U8_NUM_INTF:
3274		mod->nr_interfaces = tkn_elem->value;
3275		break;
3276
3277	default:
3278		dev_err(dev, "Invalid mod info token %d", tkn_elem->token);
3279		return -EINVAL;
3280	}
3281
3282	return 0;
3283}
3284
3285
3286static int skl_tplg_get_int_tkn(struct device *dev,
3287		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
3288		struct skl_dev *skl)
3289{
3290	int tkn_count = 0, ret;
3291	static int mod_idx, res_val_idx, intf_val_idx, dir, pin_idx;
3292	struct skl_module_res *res = NULL;
3293	struct skl_module_iface *fmt = NULL;
3294	struct skl_module *mod = NULL;
3295	static struct skl_astate_param *astate_table;
3296	static int astate_cfg_idx, count;
3297	int i;
3298	size_t size;
3299
3300	if (skl->modules) {
3301		mod = skl->modules[mod_idx];
3302		res = &mod->resources[res_val_idx];
3303		fmt = &mod->formats[intf_val_idx];
3304	}
3305
3306	switch (tkn_elem->token) {
3307	case SKL_TKN_U32_LIB_COUNT:
3308		skl->lib_count = tkn_elem->value;
3309		break;
3310
3311	case SKL_TKN_U8_NUM_MOD:
3312		skl->nr_modules = tkn_elem->value;
3313		skl->modules = devm_kcalloc(dev, skl->nr_modules,
3314				sizeof(*skl->modules), GFP_KERNEL);
3315		if (!skl->modules)
3316			return -ENOMEM;
3317
3318		for (i = 0; i < skl->nr_modules; i++) {
3319			skl->modules[i] = devm_kzalloc(dev,
3320					sizeof(struct skl_module), GFP_KERNEL);
3321			if (!skl->modules[i])
3322				return -ENOMEM;
3323		}
3324		break;
3325
3326	case SKL_TKN_MM_U8_MOD_IDX:
3327		mod_idx = tkn_elem->value;
3328		break;
3329
3330	case SKL_TKN_U32_ASTATE_COUNT:
3331		if (astate_table != NULL) {
3332			dev_err(dev, "More than one entry for A-State count");
3333			return -EINVAL;
3334		}
3335
3336		if (tkn_elem->value > SKL_MAX_ASTATE_CFG) {
3337			dev_err(dev, "Invalid A-State count %d\n",
3338				tkn_elem->value);
3339			return -EINVAL;
3340		}
3341
3342		size = struct_size(skl->cfg.astate_cfg, astate_table,
3343				   tkn_elem->value);
3344		skl->cfg.astate_cfg = devm_kzalloc(dev, size, GFP_KERNEL);
3345		if (!skl->cfg.astate_cfg)
3346			return -ENOMEM;
3347
3348		astate_table = skl->cfg.astate_cfg->astate_table;
3349		count = skl->cfg.astate_cfg->count = tkn_elem->value;
3350		break;
3351
3352	case SKL_TKN_U32_ASTATE_IDX:
3353		if (tkn_elem->value >= count) {
3354			dev_err(dev, "Invalid A-State index %d\n",
3355				tkn_elem->value);
3356			return -EINVAL;
3357		}
3358
3359		astate_cfg_idx = tkn_elem->value;
3360		break;
3361
3362	case SKL_TKN_U32_ASTATE_KCPS:
3363		astate_table[astate_cfg_idx].kcps = tkn_elem->value;
3364		break;
3365
3366	case SKL_TKN_U32_ASTATE_CLK_SRC:
3367		astate_table[astate_cfg_idx].clk_src = tkn_elem->value;
3368		break;
3369
3370	case SKL_TKN_U8_IN_PIN_TYPE:
3371	case SKL_TKN_U8_OUT_PIN_TYPE:
3372	case SKL_TKN_U8_IN_QUEUE_COUNT:
3373	case SKL_TKN_U8_OUT_QUEUE_COUNT:
3374	case SKL_TKN_MM_U8_NUM_RES:
3375	case SKL_TKN_MM_U8_NUM_INTF:
3376		ret = skl_tplg_fill_mod_info(dev, tkn_elem, mod);
3377		if (ret < 0)
3378			return ret;
3379		break;
3380
3381	case SKL_TKN_U32_DIR_PIN_COUNT:
3382		dir = tkn_elem->value & SKL_IN_DIR_BIT_MASK;
3383		pin_idx = (tkn_elem->value & SKL_PIN_COUNT_MASK) >> 4;
3384		break;
3385
3386	case SKL_TKN_MM_U32_RES_ID:
3387		if (!res)
3388			return -EINVAL;
3389
3390		res->id = tkn_elem->value;
3391		res_val_idx = tkn_elem->value;
3392		break;
3393
3394	case SKL_TKN_MM_U32_FMT_ID:
3395		if (!fmt)
3396			return -EINVAL;
3397
3398		fmt->fmt_idx = tkn_elem->value;
3399		intf_val_idx = tkn_elem->value;
3400		break;
3401
3402	case SKL_TKN_MM_U32_CPS:
3403	case SKL_TKN_MM_U32_DMA_SIZE:
3404	case SKL_TKN_MM_U32_CPC:
3405	case SKL_TKN_U32_MEM_PAGES:
3406	case SKL_TKN_U32_OBS:
3407	case SKL_TKN_U32_IBS:
3408	case SKL_TKN_MM_U32_RES_PIN_ID:
3409	case SKL_TKN_MM_U32_PIN_BUF:
3410		ret = skl_tplg_fill_res_tkn(dev, tkn_elem, res, pin_idx, dir);
3411		if (ret < 0)
3412			return ret;
3413
3414		break;
3415
3416	case SKL_TKN_MM_U32_NUM_IN_FMT:
3417		if (!fmt)
3418			return -EINVAL;
3419
3420		res->nr_input_pins = tkn_elem->value;
3421		break;
3422
3423	case SKL_TKN_MM_U32_NUM_OUT_FMT:
3424		if (!fmt)
3425			return -EINVAL;
3426
3427		res->nr_output_pins = tkn_elem->value;
3428		break;
3429
3430	case SKL_TKN_U32_FMT_CH:
3431	case SKL_TKN_U32_FMT_FREQ:
3432	case SKL_TKN_U32_FMT_BIT_DEPTH:
3433	case SKL_TKN_U32_FMT_SAMPLE_SIZE:
3434	case SKL_TKN_U32_FMT_CH_CONFIG:
3435	case SKL_TKN_U32_FMT_INTERLEAVE:
3436	case SKL_TKN_U32_FMT_SAMPLE_TYPE:
3437	case SKL_TKN_U32_FMT_CH_MAP:
3438	case SKL_TKN_MM_U32_INTF_PIN_ID:
3439		ret = skl_tplg_manifest_fill_fmt(dev, fmt, tkn_elem,
3440						 dir, pin_idx);
3441		if (ret < 0)
3442			return ret;
3443		break;
3444
3445	default:
3446		dev_err(dev, "Not a manifest token %d\n", tkn_elem->token);
3447		return -EINVAL;
3448	}
3449	tkn_count++;
3450
3451	return tkn_count;
3452}
3453
3454/*
3455 * Fill the manifest structure by parsing the tokens based on the
3456 * type.
3457 */
3458static int skl_tplg_get_manifest_tkn(struct device *dev,
3459		char *pvt_data, struct skl_dev *skl,
3460		int block_size)
3461{
3462	int tkn_count = 0, ret;
3463	int off = 0, tuple_size = 0;
3464	u8 uuid_index = 0;
3465	struct snd_soc_tplg_vendor_array *array;
3466	struct snd_soc_tplg_vendor_value_elem *tkn_elem;
3467
3468	if (block_size <= 0)
3469		return -EINVAL;
3470
3471	while (tuple_size < block_size) {
3472		array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off);
3473		off += array->size;
3474		switch (array->type) {
3475		case SND_SOC_TPLG_TUPLE_TYPE_STRING:
3476			ret = skl_tplg_get_str_tkn(dev, array, skl);
3477
3478			if (ret < 0)
3479				return ret;
3480			tkn_count = ret;
3481
3482			tuple_size += tkn_count *
3483				sizeof(struct snd_soc_tplg_vendor_string_elem);
3484			continue;
3485
3486		case SND_SOC_TPLG_TUPLE_TYPE_UUID:
3487			if (array->uuid->token != SKL_TKN_UUID) {
3488				dev_err(dev, "Not an UUID token: %d\n",
3489					array->uuid->token);
3490				return -EINVAL;
3491			}
3492			if (uuid_index >= skl->nr_modules) {
3493				dev_err(dev, "Too many UUID tokens\n");
3494				return -EINVAL;
3495			}
3496			import_guid(&skl->modules[uuid_index++]->uuid,
3497				    array->uuid->uuid);
3498
3499			tuple_size += sizeof(*array->uuid);
3500			continue;
3501
3502		default:
3503			tkn_elem = array->value;
3504			tkn_count = 0;
3505			break;
3506		}
3507
3508		while (tkn_count <= array->num_elems - 1) {
3509			ret = skl_tplg_get_int_tkn(dev,
3510					tkn_elem, skl);
3511			if (ret < 0)
3512				return ret;
3513
3514			tkn_count = tkn_count + ret;
3515			tkn_elem++;
3516		}
3517		tuple_size += (tkn_count * sizeof(*tkn_elem));
3518		tkn_count = 0;
3519	}
3520
3521	return off;
3522}
3523
3524/*
3525 * Parse manifest private data for tokens. The private data block is
3526 * preceded by descriptors for type and size of data block.
3527 */
3528static int skl_tplg_get_manifest_data(struct snd_soc_tplg_manifest *manifest,
3529			struct device *dev, struct skl_dev *skl)
3530{
3531	struct snd_soc_tplg_vendor_array *array;
3532	int num_blocks, block_size = 0, block_type, off = 0;
3533	char *data;
3534	int ret;
3535
3536	/* Read the NUM_DATA_BLOCKS descriptor */
3537	array = (struct snd_soc_tplg_vendor_array *)manifest->priv.data;
3538	ret = skl_tplg_get_desc_blocks(dev, array);
3539	if (ret < 0)
3540		return ret;
3541	num_blocks = ret;
3542
3543	off += array->size;
3544	/* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */
3545	while (num_blocks > 0) {
3546		array = (struct snd_soc_tplg_vendor_array *)
3547				(manifest->priv.data + off);
3548		ret = skl_tplg_get_desc_blocks(dev, array);
3549
3550		if (ret < 0)
3551			return ret;
3552		block_type = ret;
3553		off += array->size;
3554
3555		array = (struct snd_soc_tplg_vendor_array *)
3556			(manifest->priv.data + off);
3557
3558		ret = skl_tplg_get_desc_blocks(dev, array);
3559
3560		if (ret < 0)
3561			return ret;
3562		block_size = ret;
3563		off += array->size;
3564
3565		array = (struct snd_soc_tplg_vendor_array *)
3566			(manifest->priv.data + off);
3567
3568		data = (manifest->priv.data + off);
3569
3570		if (block_type == SKL_TYPE_TUPLE) {
3571			ret = skl_tplg_get_manifest_tkn(dev, data, skl,
3572					block_size);
3573
3574			if (ret < 0)
3575				return ret;
3576
3577			--num_blocks;
3578		} else {
3579			return -EINVAL;
3580		}
3581		off += ret;
3582	}
3583
3584	return 0;
3585}
3586
3587static int skl_manifest_load(struct snd_soc_component *cmpnt, int index,
3588				struct snd_soc_tplg_manifest *manifest)
3589{
3590	struct hdac_bus *bus = snd_soc_component_get_drvdata(cmpnt);
3591	struct skl_dev *skl = bus_to_skl(bus);
3592
3593	/* proceed only if we have private data defined */
3594	if (manifest->priv.size == 0)
3595		return 0;
3596
3597	skl_tplg_get_manifest_data(manifest, bus->dev, skl);
3598
3599	if (skl->lib_count > SKL_MAX_LIB) {
3600		dev_err(bus->dev, "Exceeding max Library count. Got:%d\n",
3601					skl->lib_count);
3602		return  -EINVAL;
3603	}
3604
3605	return 0;
3606}
3607
3608static void skl_tplg_complete(struct snd_soc_component *component)
3609{
3610	struct snd_soc_dobj *dobj;
3611	struct snd_soc_acpi_mach *mach =
3612		dev_get_platdata(component->card->dev);
3613	int i;
3614
3615	list_for_each_entry(dobj, &component->dobj_list, list) {
3616		struct snd_kcontrol *kcontrol = dobj->control.kcontrol;
3617		struct soc_enum *se;
3618		char **texts;
3619		char chan_text[4];
3620
3621		if (dobj->type != SND_SOC_DOBJ_ENUM || !kcontrol ||
3622		    kcontrol->put != skl_tplg_multi_config_set_dmic)
3623			continue;
3624
3625		se = (struct soc_enum *)kcontrol->private_value;
3626		texts = dobj->control.dtexts;
3627		sprintf(chan_text, "c%d", mach->mach_params.dmic_num);
3628
3629		for (i = 0; i < se->items; i++) {
3630			struct snd_ctl_elem_value val = {};
3631
3632			if (strstr(texts[i], chan_text)) {
3633				val.value.enumerated.item[0] = i;
3634				kcontrol->put(kcontrol, &val);
3635			}
3636		}
3637	}
3638}
3639
3640static struct snd_soc_tplg_ops skl_tplg_ops  = {
3641	.widget_load = skl_tplg_widget_load,
3642	.control_load = skl_tplg_control_load,
3643	.bytes_ext_ops = skl_tlv_ops,
3644	.bytes_ext_ops_count = ARRAY_SIZE(skl_tlv_ops),
3645	.io_ops = skl_tplg_kcontrol_ops,
3646	.io_ops_count = ARRAY_SIZE(skl_tplg_kcontrol_ops),
3647	.manifest = skl_manifest_load,
3648	.dai_load = skl_dai_load,
3649	.complete = skl_tplg_complete,
3650};
3651
3652/*
3653 * A pipe can have multiple modules, each of them will be a DAPM widget as
3654 * well. While managing a pipeline we need to get the list of all the
3655 * widgets in a pipelines, so this helper - skl_tplg_create_pipe_widget_list()
3656 * helps to get the SKL type widgets in that pipeline
3657 */
3658static int skl_tplg_create_pipe_widget_list(struct snd_soc_component *component)
3659{
3660	struct snd_soc_dapm_widget *w;
3661	struct skl_module_cfg *mcfg = NULL;
3662	struct skl_pipe_module *p_module = NULL;
3663	struct skl_pipe *pipe;
3664
3665	list_for_each_entry(w, &component->card->widgets, list) {
3666		if (is_skl_dsp_widget_type(w, component->dev) && w->priv) {
3667			mcfg = w->priv;
3668			pipe = mcfg->pipe;
3669
3670			p_module = devm_kzalloc(component->dev,
3671						sizeof(*p_module), GFP_KERNEL);
3672			if (!p_module)
3673				return -ENOMEM;
3674
3675			p_module->w = w;
3676			list_add_tail(&p_module->node, &pipe->w_list);
3677		}
3678	}
3679
3680	return 0;
3681}
3682
3683static void skl_tplg_set_pipe_type(struct skl_dev *skl, struct skl_pipe *pipe)
3684{
3685	struct skl_pipe_module *w_module;
3686	struct snd_soc_dapm_widget *w;
3687	struct skl_module_cfg *mconfig;
3688	bool host_found = false, link_found = false;
3689
3690	list_for_each_entry(w_module, &pipe->w_list, node) {
3691		w = w_module->w;
3692		mconfig = w->priv;
3693
3694		if (mconfig->dev_type == SKL_DEVICE_HDAHOST)
3695			host_found = true;
3696		else if (mconfig->dev_type != SKL_DEVICE_NONE)
3697			link_found = true;
3698	}
3699
3700	if (host_found && link_found)
3701		pipe->passthru = true;
3702	else
3703		pipe->passthru = false;
3704}
3705
3706/*
3707 * SKL topology init routine
3708 */
3709int skl_tplg_init(struct snd_soc_component *component, struct hdac_bus *bus)
3710{
3711	int ret;
3712	const struct firmware *fw;
3713	struct skl_dev *skl = bus_to_skl(bus);
3714	struct skl_pipeline *ppl;
3715
3716	ret = request_firmware(&fw, skl->tplg_name, bus->dev);
3717	if (ret < 0) {
3718		char alt_tplg_name[64];
3719
3720		snprintf(alt_tplg_name, sizeof(alt_tplg_name), "%s-tplg.bin",
3721			 skl->mach->drv_name);
3722		dev_info(bus->dev, "tplg fw %s load failed with %d, trying alternative tplg name %s",
3723			 skl->tplg_name, ret, alt_tplg_name);
3724
3725		ret = request_firmware(&fw, alt_tplg_name, bus->dev);
3726		if (!ret)
3727			goto component_load;
3728
3729		dev_info(bus->dev, "tplg %s failed with %d, falling back to dfw_sst.bin",
3730			 alt_tplg_name, ret);
3731
3732		ret = request_firmware(&fw, "dfw_sst.bin", bus->dev);
3733		if (ret < 0) {
3734			dev_err(bus->dev, "Fallback tplg fw %s load failed with %d\n",
3735					"dfw_sst.bin", ret);
3736			return ret;
3737		}
3738	}
3739
3740component_load:
3741
3742	/*
3743	 * The complete tplg for SKL is loaded as index 0, we don't use
3744	 * any other index
3745	 */
3746	ret = snd_soc_tplg_component_load(component, &skl_tplg_ops, fw, 0);
3747	if (ret < 0) {
3748		dev_err(bus->dev, "tplg component load failed%d\n", ret);
3749		goto err;
3750	}
3751
3752	ret = skl_tplg_create_pipe_widget_list(component);
3753	if (ret < 0) {
3754		dev_err(bus->dev, "tplg create pipe widget list failed%d\n",
3755				ret);
3756		goto err;
3757	}
3758
3759	list_for_each_entry(ppl, &skl->ppl_list, node)
3760		skl_tplg_set_pipe_type(skl, ppl->pipe);
3761
3762err:
3763	release_firmware(fw);
3764	return ret;
3765}
3766
3767void skl_tplg_exit(struct snd_soc_component *component, struct hdac_bus *bus)
3768{
3769	struct skl_dev *skl = bus_to_skl(bus);
3770	struct skl_pipeline *ppl, *tmp;
3771
3772	list_for_each_entry_safe(ppl, tmp, &skl->ppl_list, node)
3773		list_del(&ppl->node);
3774
3775	/* clean up topology */
3776	snd_soc_tplg_component_remove(component, SND_SOC_TPLG_INDEX_ALL);
3777}
3778