skl-topology.c 90.2 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8 9 10 11 12 13
/*
 *  skl-topology.c - Implements Platform component ALSA controls/widget
 *  handlers.
 *
 *  Copyright (C) 2014-2015 Intel Corp
 *  Author: Jeeja KP <jeeja.kp@intel.com>
 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */

#include <linux/slab.h>
#include <linux/types.h>
#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 22 23 24
#include "skl-sst-dsp.h"
#include "skl-sst-ipc.h"
#include "skl-topology.h"
#include "skl.h"
25 26
#include "../common/sst-dsp.h"
#include "../common/sst-dsp-priv.h"
27

28 29 30
#define SKL_CH_FIXUP_MASK		(1 << 0)
#define SKL_RATE_FIXUP_MASK		(1 << 1)
#define SKL_FMT_FIXUP_MASK		(1 << 2)
31 32
#define SKL_IN_DIR_BIT_MASK		BIT(0)
#define SKL_PIN_COUNT_MASK		GENMASK(7, 4)
33

34 35 36 37 38 39 40 41 42 43 44 45 46
static const int mic_mono_list[] = {
0, 1, 2, 3,
};
static const int mic_stereo_list[][SKL_CH_STEREO] = {
{0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 3}, {2, 3},
};
static const int mic_trio_list[][SKL_CH_TRIO] = {
{0, 1, 2}, {0, 1, 3}, {0, 2, 3}, {1, 2, 3},
};
static const int mic_quatro_list[][SKL_CH_QUATRO] = {
{0, 1, 2, 3},
};

47 48 49
#define CHECK_HW_PARAMS(ch, freq, bps, prm_ch, prm_freq, prm_bps) \
	((ch == prm_ch) && (bps == prm_bps) && (freq == prm_freq))

50
void skl_tplg_d0i3_get(struct skl_dev *skl, enum d0i3_capability caps)
51
{
52
	struct skl_d0i3_data *d0i3 =  &skl->d0i3;
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

	switch (caps) {
	case SKL_D0I3_NONE:
		d0i3->non_d0i3++;
		break;

	case SKL_D0I3_STREAMING:
		d0i3->streaming++;
		break;

	case SKL_D0I3_NON_STREAMING:
		d0i3->non_streaming++;
		break;
	}
}

69
void skl_tplg_d0i3_put(struct skl_dev *skl, enum d0i3_capability caps)
70
{
71
	struct skl_d0i3_data *d0i3 =  &skl->d0i3;
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

	switch (caps) {
	case SKL_D0I3_NONE:
		d0i3->non_d0i3--;
		break;

	case SKL_D0I3_STREAMING:
		d0i3->streaming--;
		break;

	case SKL_D0I3_NON_STREAMING:
		d0i3->non_streaming--;
		break;
	}
}

88 89 90 91
/*
 * SKL DSP driver modelling uses only few DAPM widgets so for rest we will
 * ignore. This helpers checks if the SKL driver handles this widget type
 */
92 93
static int is_skl_dsp_widget_type(struct snd_soc_dapm_widget *w,
				  struct device *dev)
94
{
95 96 97
	if (w->dapm->dev != dev)
		return false;

98 99 100 101 102 103 104
	switch (w->id) {
	case snd_soc_dapm_dai_link:
	case snd_soc_dapm_dai_in:
	case snd_soc_dapm_aif_in:
	case snd_soc_dapm_aif_out:
	case snd_soc_dapm_dai_out:
	case snd_soc_dapm_switch:
105 106 107
	case snd_soc_dapm_output:
	case snd_soc_dapm_mux:

108 109 110 111 112 113
		return false;
	default:
		return true;
	}
}

114
static void skl_dump_mconfig(struct skl_dev *skl, struct skl_module_cfg *mcfg)
115
{
116 117
	struct skl_module_iface *iface = &mcfg->module->formats[0];

118 119 120 121 122 123
	dev_dbg(skl->dev, "Dumping config\n");
	dev_dbg(skl->dev, "Input Format:\n");
	dev_dbg(skl->dev, "channels = %d\n", iface->inputs[0].fmt.channels);
	dev_dbg(skl->dev, "s_freq = %d\n", iface->inputs[0].fmt.s_freq);
	dev_dbg(skl->dev, "ch_cfg = %d\n", iface->inputs[0].fmt.ch_cfg);
	dev_dbg(skl->dev, "valid bit depth = %d\n",
124
				iface->inputs[0].fmt.valid_bit_depth);
125 126 127 128
	dev_dbg(skl->dev, "Output Format:\n");
	dev_dbg(skl->dev, "channels = %d\n", iface->outputs[0].fmt.channels);
	dev_dbg(skl->dev, "s_freq = %d\n", iface->outputs[0].fmt.s_freq);
	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
}

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
static void skl_tplg_update_chmap(struct skl_module_fmt *fmt, int chs)
{
	int slot_map = 0xFFFFFFFF;
	int start_slot = 0;
	int i;

	for (i = 0; i < chs; i++) {
		/*
		 * For 2 channels with starting slot as 0, slot map will
		 * look like 0xFFFFFF10.
		 */
		slot_map &= (~(0xF << (4 * i)) | (start_slot << (4 * i)));
		start_slot++;
	}
	fmt->ch_map = slot_map;
}

150 151 152 153 154
static void skl_tplg_update_params(struct skl_module_fmt *fmt,
			struct skl_pipe_params *params, int fixup)
{
	if (fixup & SKL_RATE_FIXUP_MASK)
		fmt->s_freq = params->s_freq;
155
	if (fixup & SKL_CH_FIXUP_MASK) {
156
		fmt->channels = params->ch;
157 158
		skl_tplg_update_chmap(fmt, fmt->channels);
	}
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
	if (fixup & SKL_FMT_FIXUP_MASK) {
		fmt->valid_bit_depth = skl_get_bit_depth(params->s_fmt);

		/*
		 * 16 bit is 16 bit container whereas 24 bit is in 32 bit
		 * container so update bit depth accordingly
		 */
		switch (fmt->valid_bit_depth) {
		case SKL_DEPTH_16BIT:
			fmt->bit_depth = fmt->valid_bit_depth;
			break;

		default:
			fmt->bit_depth = SKL_DEPTH_32BIT;
			break;
		}
	}

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
}

/*
 * A pipeline may have modules which impact the pcm parameters, like SRC,
 * channel converter, format converter.
 * We need to calculate the output params by applying the 'fixup'
 * Topology will tell driver which type of fixup is to be applied by
 * supplying the fixup mask, so based on that we calculate the output
 *
 * Now In FE the pcm hw_params is source/target format. Same is applicable
 * for BE with its hw_params invoked.
 * here based on FE, BE pipeline and direction we calculate the input and
 * outfix and then apply that for a module
 */
static void skl_tplg_update_params_fixup(struct skl_module_cfg *m_cfg,
		struct skl_pipe_params *params, bool is_fe)
{
	int in_fixup, out_fixup;
	struct skl_module_fmt *in_fmt, *out_fmt;

197
	/* Fixups will be applied to pin 0 only */
198 199
	in_fmt = &m_cfg->module->formats[0].inputs[0].fmt;
	out_fmt = &m_cfg->module->formats[0].outputs[0].fmt;
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
		if (is_fe) {
			in_fixup = m_cfg->params_fixup;
			out_fixup = (~m_cfg->converter) &
					m_cfg->params_fixup;
		} else {
			out_fixup = m_cfg->params_fixup;
			in_fixup = (~m_cfg->converter) &
					m_cfg->params_fixup;
		}
	} else {
		if (is_fe) {
			out_fixup = m_cfg->params_fixup;
			in_fixup = (~m_cfg->converter) &
					m_cfg->params_fixup;
		} else {
			in_fixup = m_cfg->params_fixup;
			out_fixup = (~m_cfg->converter) &
					m_cfg->params_fixup;
		}
	}

	skl_tplg_update_params(in_fmt, params, in_fixup);
	skl_tplg_update_params(out_fmt, params, out_fixup);
}

/*
 * A module needs input and output buffers, which are dependent upon pcm
 * params, so once we have calculate params, we need buffer calculation as
 * well.
 */
232
static void skl_tplg_update_buffer_size(struct skl_dev *skl,
233 234 235
				struct skl_module_cfg *mcfg)
{
	int multiplier = 1;
236
	struct skl_module_fmt *in_fmt, *out_fmt;
237
	struct skl_module_res *res;
238 239 240 241

	/* Since fixups is applied to pin 0 only, ibs, obs needs
	 * change for pin 0 only
	 */
242 243 244
	res = &mcfg->module->resources[0];
	in_fmt = &mcfg->module->formats[0].inputs[0].fmt;
	out_fmt = &mcfg->module->formats[0].outputs[0].fmt;
245 246 247

	if (mcfg->m_type == SKL_MODULE_TYPE_SRCINT)
		multiplier = 5;
248

249
	res->ibs = DIV_ROUND_UP(in_fmt->s_freq, 1000) *
250
			in_fmt->channels * (in_fmt->bit_depth >> 3) *
251 252
			multiplier;

253
	res->obs = DIV_ROUND_UP(out_fmt->s_freq, 1000) *
254
			out_fmt->channels * (out_fmt->bit_depth >> 3) *
255
			multiplier;
256 257
}

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
static u8 skl_tplg_be_dev_type(int dev_type)
{
	int ret;

	switch (dev_type) {
	case SKL_DEVICE_BT:
		ret = NHLT_DEVICE_BT;
		break;

	case SKL_DEVICE_DMIC:
		ret = NHLT_DEVICE_DMIC;
		break;

	case SKL_DEVICE_I2S:
		ret = NHLT_DEVICE_I2S;
		break;

	default:
		ret = NHLT_DEVICE_INVALID;
		break;
	}

	return ret;
}

283
static int skl_tplg_update_be_blob(struct snd_soc_dapm_widget *w,
284
						struct skl_dev *skl)
285 286 287 288 289
{
	struct skl_module_cfg *m_cfg = w->priv;
	int link_type, dir;
	u32 ch, s_freq, s_fmt;
	struct nhlt_specific_cfg *cfg;
290
	u8 dev_type = skl_tplg_be_dev_type(m_cfg->dev_type);
291 292
	int fmt_idx = m_cfg->fmt_idx;
	struct skl_module_iface *m_iface = &m_cfg->module->formats[fmt_idx];
293 294 295 296 297

	/* check if we already have blob */
	if (m_cfg->formats_config.caps_size > 0)
		return 0;

298
	dev_dbg(skl->dev, "Applying default cfg blob\n");
299 300 301
	switch (m_cfg->dev_type) {
	case SKL_DEVICE_DMIC:
		link_type = NHLT_LINK_DMIC;
302
		dir = SNDRV_PCM_STREAM_CAPTURE;
303 304 305
		s_freq = m_iface->inputs[0].fmt.s_freq;
		s_fmt = m_iface->inputs[0].fmt.bit_depth;
		ch = m_iface->inputs[0].fmt.channels;
306 307 308 309 310
		break;

	case SKL_DEVICE_I2S:
		link_type = NHLT_LINK_SSP;
		if (m_cfg->hw_conn_type == SKL_CONN_SOURCE) {
311
			dir = SNDRV_PCM_STREAM_PLAYBACK;
312 313 314
			s_freq = m_iface->outputs[0].fmt.s_freq;
			s_fmt = m_iface->outputs[0].fmt.bit_depth;
			ch = m_iface->outputs[0].fmt.channels;
315 316
		} else {
			dir = SNDRV_PCM_STREAM_CAPTURE;
317 318 319
			s_freq = m_iface->inputs[0].fmt.s_freq;
			s_fmt = m_iface->inputs[0].fmt.bit_depth;
			ch = m_iface->inputs[0].fmt.channels;
320 321 322 323 324 325 326 327 328
		}
		break;

	default:
		return -EINVAL;
	}

	/* update the blob based on virtual bus_id and default params */
	cfg = skl_get_ep_blob(skl, m_cfg->vbus_id, link_type,
329
					s_fmt, ch, s_freq, dir, dev_type);
330 331 332 333
	if (cfg) {
		m_cfg->formats_config.caps_size = cfg->size;
		m_cfg->formats_config.caps = (u32 *) &cfg->caps;
	} 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 338 339 340 341 342 343
					ch, s_freq, s_fmt);
		return -EIO;
	}

	return 0;
}

344
static void skl_tplg_update_module_params(struct snd_soc_dapm_widget *w,
345
							struct skl_dev *skl)
346 347 348 349 350 351 352 353 354
{
	struct skl_module_cfg *m_cfg = w->priv;
	struct skl_pipe_params *params = m_cfg->pipe->p_params;
	int p_conn_type = m_cfg->pipe->conn_type;
	bool is_fe;

	if (!m_cfg->params_fixup)
		return;

355
	dev_dbg(skl->dev, "Mconfig for widget=%s BEFORE updation\n",
356 357
				w->name);

358
	skl_dump_mconfig(skl, m_cfg);
359 360 361 362 363 364 365

	if (p_conn_type == SKL_PIPE_CONN_TYPE_FE)
		is_fe = true;
	else
		is_fe = false;

	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 370
				w->name);

371
	skl_dump_mconfig(skl, m_cfg);
372 373
}

374 375 376 377 378 379
/*
 * some modules can have multiple params set from user control and
 * need to be set after module is initialized. If set_param flag is
 * set module params will be done after module is initialised.
 */
static int skl_tplg_set_module_params(struct snd_soc_dapm_widget *w,
380
						struct skl_dev *skl)
381 382 383 384 385 386 387 388 389
{
	int i, ret;
	struct skl_module_cfg *mconfig = w->priv;
	const struct snd_kcontrol_new *k;
	struct soc_bytes_ext *sb;
	struct skl_algo_data *bc;
	struct skl_specific_cfg *sp_cfg;

	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 394 395 396 397 398 399 400 401 402 403 404
					sp_cfg->caps_size,
					sp_cfg->param_id, mconfig);
		if (ret < 0)
			return ret;
	}

	for (i = 0; i < w->num_kcontrols; i++) {
		k = &w->kcontrol_news[i];
		if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
			sb = (void *) k->private_value;
			bc = (struct skl_algo_data *)sb->dobj.private;

405
			if (bc->set_params == SKL_PARAM_SET) {
406
				ret = skl_set_module_params(skl,
407
						(u32 *)bc->params, bc->size,
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
						bc->param_id, mconfig);
				if (ret < 0)
					return ret;
			}
		}
	}

	return 0;
}

/*
 * some module param can set from user control and this is required as
 * when module is initailzed. if module param is required in init it is
 * identifed by set_param flag. if set_param flag is not set, then this
 * parameter needs to set as part of module init.
 */
static int skl_tplg_set_module_init_data(struct snd_soc_dapm_widget *w)
{
	const struct snd_kcontrol_new *k;
	struct soc_bytes_ext *sb;
	struct skl_algo_data *bc;
	struct skl_module_cfg *mconfig = w->priv;
	int i;

	for (i = 0; i < w->num_kcontrols; i++) {
		k = &w->kcontrol_news[i];
		if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
			sb = (struct soc_bytes_ext *)k->private_value;
			bc = (struct skl_algo_data *)sb->dobj.private;

438
			if (bc->set_params != SKL_PARAM_INIT)
439 440
				continue;

441
			mconfig->formats_config.caps = (u32 *)bc->params;
442
			mconfig->formats_config.caps_size = bc->size;
443 444 445 446 447 448 449 450

			break;
		}
	}

	return 0;
}

451
static int skl_tplg_module_prepare(struct skl_dev *skl, struct skl_pipe *pipe,
452 453 454 455
		struct snd_soc_dapm_widget *w, struct skl_module_cfg *mcfg)
{
	switch (mcfg->dev_type) {
	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 463 464
	}

	return 0;
}

465 466 467 468 469 470
/*
 * Inside a pipe instance, we can have various modules. These modules need
 * to instantiated in DSP by invoking INIT_MODULE IPC, which is achieved by
 * skl_init_module() routine, so invoke that for all modules in a pipeline
 */
static int
471
skl_tplg_init_pipe_modules(struct skl_dev *skl, struct skl_pipe *pipe)
472 473 474 475
{
	struct skl_pipe_module *w_module;
	struct snd_soc_dapm_widget *w;
	struct skl_module_cfg *mconfig;
476
	u8 cfg_idx;
477 478 479
	int ret = 0;

	list_for_each_entry(w_module, &pipe->w_list, node) {
480
		guid_t *uuid_mod;
481 482 483
		w = w_module->w;
		mconfig = w->priv;

484 485
		/* check if module ids are populated */
		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 493 494 495
		cfg_idx = mconfig->pipe->cur_config_idx;
		mconfig->fmt_idx = mconfig->mod_cfg[cfg_idx].fmt_idx;
		mconfig->res_idx = mconfig->mod_cfg[cfg_idx].res_idx;

496 497
		if (mconfig->module->loadable && skl->dsp->fw_ops.load_mod) {
			ret = skl->dsp->fw_ops.load_mod(skl->dsp,
498 499 500
				mconfig->id.module_id, mconfig->guid);
			if (ret < 0)
				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 508 509
		if (ret < 0)
			return ret;

510
		/* update blob if blob is null for be with default value */
511
		skl_tplg_update_be_blob(w, skl);
512

513 514 515 516
		/*
		 * apply fix/conversion to module params based on
		 * FE/BE params
		 */
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 522
		if (mconfig->id.pvt_id < 0)
			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 529 530 531
						mconfig->core_id, ret);
			return ret;
		}

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;
544
err:
545
	skl_dsp_put_core(skl->dsp, mconfig->core_id);
546
	return ret;
547
}
548

549
static int skl_tplg_unload_pipe_modules(struct skl_dev *skl,
550 551
	 struct skl_pipe *pipe)
{
552
	int ret = 0;
553 554 555 556
	struct skl_pipe_module *w_module = NULL;
	struct skl_module_cfg *mconfig = NULL;

	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 566 567
			if (ret < 0)
				return -EIO;
		}
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 572
		if (ret < 0) {
			/* don't return; continue with other modules */
573
			dev_err(skl->dev, "Failed to sleep core %d ret=%d\n",
574 575
				mconfig->core_id, ret);
		}
576 577 578
	}

	/* no modules to unload in this path, so return */
579
	return ret;
580 581
}

582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
static bool skl_tplg_is_multi_fmt(struct skl_dev *skl, struct skl_pipe *pipe)
{
	struct skl_pipe_fmt *cur_fmt;
	struct skl_pipe_fmt *next_fmt;
	int i;

	if (pipe->nr_cfgs <= 1)
		return false;

	if (pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
		return true;

	for (i = 0; i < pipe->nr_cfgs - 1; i++) {
		if (pipe->direction == SNDRV_PCM_STREAM_PLAYBACK) {
			cur_fmt = &pipe->configs[i].out_fmt;
			next_fmt = &pipe->configs[i + 1].out_fmt;
		} else {
			cur_fmt = &pipe->configs[i].in_fmt;
			next_fmt = &pipe->configs[i + 1].in_fmt;
		}

		if (!CHECK_HW_PARAMS(cur_fmt->channels, cur_fmt->freq,
				     cur_fmt->bps,
				     next_fmt->channels,
				     next_fmt->freq,
				     next_fmt->bps))
			return true;
	}

	return false;
}

614 615 616 617 618 619 620 621
/*
 * Here, we select pipe format based on the pipe type and pipe
 * direction to determine the current config index for the pipeline.
 * The config index is then used to select proper module resources.
 * Intermediate pipes currently have a fixed format hence we select the
 * 0th configuratation by default for such pipes.
 */
static int
622
skl_tplg_get_pipe_config(struct skl_dev *skl, struct skl_module_cfg *mconfig)
623 624 625 626 627 628 629 630 631 632 633 634 635
{
	struct skl_pipe *pipe = mconfig->pipe;
	struct skl_pipe_params *params = pipe->p_params;
	struct skl_path_config *pconfig = &pipe->configs[0];
	struct skl_pipe_fmt *fmt = NULL;
	bool in_fmt = false;
	int i;

	if (pipe->nr_cfgs == 0) {
		pipe->cur_config_idx = 0;
		return 0;
	}

636 637 638 639 640 641 642 643
	if (skl_tplg_is_multi_fmt(skl, pipe)) {
		pipe->cur_config_idx = pipe->pipe_config_idx;
		pipe->memory_pages = pconfig->mem_pages;
		dev_dbg(skl->dev, "found pipe config idx:%d\n",
			pipe->cur_config_idx);
		return 0;
	}

644
	if (pipe->conn_type == SKL_PIPE_CONN_TYPE_NONE) {
645
		dev_dbg(skl->dev, "No conn_type detected, take 0th config\n");
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
		pipe->cur_config_idx = 0;
		pipe->memory_pages = pconfig->mem_pages;

		return 0;
	}

	if ((pipe->conn_type == SKL_PIPE_CONN_TYPE_FE &&
	     pipe->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
	     (pipe->conn_type == SKL_PIPE_CONN_TYPE_BE &&
	     pipe->direction == SNDRV_PCM_STREAM_CAPTURE))
		in_fmt = true;

	for (i = 0; i < pipe->nr_cfgs; i++) {
		pconfig = &pipe->configs[i];
		if (in_fmt)
			fmt = &pconfig->in_fmt;
		else
			fmt = &pconfig->out_fmt;

		if (CHECK_HW_PARAMS(params->ch, params->s_freq, params->s_fmt,
				    fmt->channels, fmt->freq, fmt->bps)) {
			pipe->cur_config_idx = i;
			pipe->memory_pages = pconfig->mem_pages;
669
			dev_dbg(skl->dev, "Using pipe config: %d\n", i);
670 671 672 673 674

			return 0;
		}
	}

675
	dev_err(skl->dev, "Invalid pipe config: %d %d %d for pipe: %d\n",
676 677 678 679
		params->ch, params->s_freq, params->s_fmt, pipe->ppl_id);
	return -EINVAL;
}

680 681 682 683 684 685 686 687
/*
 * Mixer module represents a pipeline. So in the Pre-PMU event of mixer we
 * need create the pipeline. So we do following:
 *   - Create the pipeline
 *   - Initialize the modules in pipeline
 *   - finally bind all modules together
 */
static int skl_tplg_mixer_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
688
							struct skl_dev *skl)
689 690 691 692 693
{
	int ret;
	struct skl_module_cfg *mconfig = w->priv;
	struct skl_pipe_module *w_module;
	struct skl_pipe *s_pipe = mconfig->pipe;
694 695
	struct skl_module_cfg *src_module = NULL, *dst_module, *module;
	struct skl_module_deferred_bind *modules;
696

697 698 699 700
	ret = skl_tplg_get_pipe_config(skl, mconfig);
	if (ret < 0)
		return ret;

701 702 703 704
	/*
	 * Create a list of modules for pipe.
	 * This list contains modules from source to sink
	 */
705
	ret = skl_create_pipeline(skl, mconfig->pipe);
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
	if (ret < 0)
		return ret;

	/* Init all pipe modules from source to sink */
	ret = skl_tplg_init_pipe_modules(skl, s_pipe);
	if (ret < 0)
		return ret;

	/* Bind modules from source to sink */
	list_for_each_entry(w_module, &s_pipe->w_list, node) {
		dst_module = w_module->w->priv;

		if (src_module == NULL) {
			src_module = dst_module;
			continue;
		}

723
		ret = skl_bind_modules(skl, src_module, dst_module);
724 725 726 727 728 729
		if (ret < 0)
			return ret;

		src_module = dst_module;
	}

730 731 732 733 734 735 736 737 738 739 740
	/*
	 * When the destination module is initialized, check for these modules
	 * in deferred bind list. If found, bind them.
	 */
	list_for_each_entry(w_module, &s_pipe->w_list, node) {
		if (list_empty(&skl->bind_list))
			break;

		list_for_each_entry(modules, &skl->bind_list, node) {
			module = w_module->w->priv;
			if (modules->dst == module)
741
				skl_bind_modules(skl, modules->src,
742 743 744 745
							modules->dst);
		}
	}

746 747 748
	return 0;
}

749
static int skl_fill_sink_instance_id(struct skl_dev *skl, u32 *params,
750
				int size, struct skl_module_cfg *mcfg)
751 752 753
{
	int i, pvt_id;

754 755 756
	if (mcfg->m_type == SKL_MODULE_TYPE_KPB) {
		struct skl_kpb_params *kpb_params =
				(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 762 763 764 765 766 767
								inst->inst_id);
			if (pvt_id < 0)
				return -EINVAL;

			inst->inst_id = pvt_id;
			inst++;
		}
768
	}
769

770 771
	return 0;
}
772 773 774 775 776 777 778 779
/*
 * Some modules require params to be set after the module is bound to
 * all pins connected.
 *
 * The module provider initializes set_param flag for such modules and we
 * send params after binding
 */
static 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 783 784 785 786 787
{
	int i, ret;
	struct skl_module_cfg *mconfig = w->priv;
	const struct snd_kcontrol_new *k;
	struct soc_bytes_ext *sb;
	struct skl_algo_data *bc;
	struct skl_specific_cfg *sp_cfg;
788
	u32 *params;
789 790 791 792 793

	/*
	 * check all out/in pins are in bind state.
	 * if so set the module param
	 */
794
	for (i = 0; i < mcfg->module->max_output_pins; i++) {
795 796 797 798
		if (mcfg->m_out_pin[i].pin_state != SKL_PIN_BIND_DONE)
			return 0;
	}

799
	for (i = 0; i < mcfg->module->max_input_pins; i++) {
800 801 802 803 804 805 806
		if (mcfg->m_in_pin[i].pin_state != SKL_PIN_BIND_DONE)
			return 0;
	}

	if (mconfig->formats_config.caps_size > 0 &&
		mconfig->formats_config.set_params == SKL_PARAM_BIND) {
		sp_cfg = &mconfig->formats_config;
807
		ret = skl_set_module_params(skl, sp_cfg->caps,
808 809 810 811 812 813 814 815 816 817 818 819 820
					sp_cfg->caps_size,
					sp_cfg->param_id, mconfig);
		if (ret < 0)
			return ret;
	}

	for (i = 0; i < w->num_kcontrols; i++) {
		k = &w->kcontrol_news[i];
		if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
			sb = (void *) k->private_value;
			bc = (struct skl_algo_data *)sb->dobj.private;

			if (bc->set_params == SKL_PARAM_BIND) {
821
				params = kmemdup(bc->params, bc->max, GFP_KERNEL);
822 823 824
				if (!params)
					return -ENOMEM;

825
				skl_fill_sink_instance_id(skl, params, bc->max,
826 827
								mconfig);

828
				ret = skl_set_module_params(skl, params,
829 830 831
						bc->max, bc->param_id, mconfig);
				kfree(params);

832 833 834 835 836 837 838 839 840
				if (ret < 0)
					return ret;
			}
		}
	}

	return 0;
}

841
static int skl_get_module_id(struct skl_dev *skl, guid_t *uuid)
842 843 844
{
	struct uuid_module *module;

845
	list_for_each_entry(module, &skl->uuid_list, list) {
846
		if (guid_equal(uuid, &module->uuid))
847 848 849 850 851 852
			return module->id;
	}

	return -EINVAL;
}

853
static int skl_tplg_find_moduleid_from_uuid(struct skl_dev *skl,
854 855 856 857 858
					const struct snd_kcontrol_new *k)
{
	struct soc_bytes_ext *sb = (void *) k->private_value;
	struct skl_algo_data *bc = (struct skl_algo_data *)sb->dobj.private;
	struct skl_kpb_params *uuid_params, *params;
859
	struct hdac_bus *bus = skl_to_bus(skl);
860 861 862 863
	int i, size, module_id;

	if (bc->set_params == SKL_PARAM_BIND && bc->max) {
		uuid_params = (struct skl_kpb_params *)bc->params;
864
		size = struct_size(params, u.map, uuid_params->num_modules);
865 866 867 868 869 870 871 872

		params = devm_kzalloc(bus->dev, size, GFP_KERNEL);
		if (!params)
			return -ENOMEM;

		params->num_modules = uuid_params->num_modules;

		for (i = 0; i < uuid_params->num_modules; i++) {
873
			module_id = skl_get_module_id(skl,
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
				&uuid_params->u.map_uuid[i].mod_uuid);
			if (module_id < 0) {
				devm_kfree(bus->dev, params);
				return -EINVAL;
			}

			params->u.map[i].mod_id = module_id;
			params->u.map[i].inst_id =
				uuid_params->u.map_uuid[i].inst_id;
		}

		devm_kfree(bus->dev, bc->params);
		bc->params = (char *)params;
		bc->max = size;
	}

	return 0;
}

/*
 * Retrieve the module id from UUID mentioned in the
 * post bind params
 */
897
void skl_tplg_add_moduleid_in_bind_params(struct skl_dev *skl,
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
				struct snd_soc_dapm_widget *w)
{
	struct skl_module_cfg *mconfig = w->priv;
	int i;

	/*
	 * Post bind params are used for only for KPB
	 * to set copier instances to drain the data
	 * in fast mode
	 */
	if (mconfig->m_type != SKL_MODULE_TYPE_KPB)
		return;

	for (i = 0; i < w->num_kcontrols; i++)
		if ((w->kcontrol_news[i].access &
			SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) &&
			(skl_tplg_find_moduleid_from_uuid(skl,
			&w->kcontrol_news[i]) < 0))
916
			dev_err(skl->dev,
917 918 919
				"%s: invalid kpb post bind params\n",
				__func__);
}
920

921
static int skl_tplg_module_add_deferred_bind(struct skl_dev *skl,
922 923 924 925 926 927
	struct skl_module_cfg *src, struct skl_module_cfg *dst)
{
	struct skl_module_deferred_bind *m_list, *modules;
	int i;

	/* only supported for module with static pin connection */
928
	for (i = 0; i < dst->module->max_input_pins; i++) {
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
		struct skl_module_pin *pin = &dst->m_in_pin[i];

		if (pin->is_dynamic)
			continue;

		if ((pin->id.module_id  == src->id.module_id) &&
			(pin->id.instance_id  == src->id.instance_id)) {

			if (!list_empty(&skl->bind_list)) {
				list_for_each_entry(modules, &skl->bind_list, node) {
					if (modules->src == src && modules->dst == dst)
						return 0;
				}
			}

			m_list = kzalloc(sizeof(*m_list), GFP_KERNEL);
			if (!m_list)
				return -ENOMEM;

			m_list->src = src;
			m_list->dst = dst;

			list_add(&m_list->node, &skl->bind_list);
		}
	}

	return 0;
}

958
static 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 966
	struct skl_module_cfg *sink_mconfig;
	int ret;
967

968
	snd_soc_dapm_widget_for_each_sink_path(w, p) {
969 970 971
		if (!p->connect)
			continue;

972 973 974 975
		dev_dbg(skl->dev,
			"%s: src widget=%s\n", __func__, w->name);
		dev_dbg(skl->dev,
			"%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 981
			return skl_tplg_bind_sinks(p->sink, skl, src_w, src_mconfig);

982 983 984 985 986 987
		/*
		 * here we will check widgets in sink pipelines, so that
		 * can be any widgets type and we are only interested if
		 * they are ones used for SKL so check that first
		 */
		if ((p->sink->priv != NULL) &&
988
				is_skl_dsp_widget_type(p->sink, skl->dev)) {
989 990 991 992

			sink = p->sink;
			sink_mconfig = sink->priv;

993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
			/*
			 * Modules other than PGA leaf can be connected
			 * directly or via switch to a module in another
			 * pipeline. EX: reference path
			 * when the path is enabled, the dst module that needs
			 * to be bound may not be initialized. if the module is
			 * not initialized, add these modules in the deferred
			 * bind list and when the dst module is initialised,
			 * bind this module to the dst_module in deferred list.
			 */
			if (((src_mconfig->m_state == SKL_MODULE_INIT_DONE)
				&& (sink_mconfig->m_state == SKL_MODULE_UNINIT))) {

				ret = skl_tplg_module_add_deferred_bind(skl,
						src_mconfig, sink_mconfig);

				if (ret < 0)
					return ret;

			}


1015 1016 1017 1018
			if (src_mconfig->m_state == SKL_MODULE_UNINIT ||
				sink_mconfig->m_state == SKL_MODULE_UNINIT)
				continue;

1019
			/* Bind source to sink, mixin is always source */
1020
			ret = skl_bind_modules(skl, src_mconfig, sink_mconfig);
1021 1022 1023
			if (ret)
				return ret;

1024
			/* set module params after bind */
1025 1026 1027 1028
			skl_tplg_set_module_bind_params(src_w,
					src_mconfig, skl);
			skl_tplg_set_module_bind_params(sink,
					sink_mconfig, skl);
1029

1030 1031
			/* Start sinks pipe first */
			if (sink_mconfig->pipe->state != SKL_PIPE_STARTED) {
1032 1033
				if (sink_mconfig->pipe->conn_type !=
							SKL_PIPE_CONN_TYPE_FE)
1034
					ret = skl_run_pipe(skl,
1035
							sink_mconfig->pipe);
1036 1037 1038 1039 1040 1041
				if (ret)
					return ret;
			}
		}
	}

1042
	if (!sink && next_sink)
1043
		return skl_tplg_bind_sinks(next_sink, skl, src_w, src_mconfig);
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058

	return 0;
}

/*
 * A PGA represents a module in a pipeline. So in the Pre-PMU event of PGA
 * we need to do following:
 *   - Bind to sink pipeline
 *      Since the sink pipes can be running and we don't get mixer event on
 *      connect for already running mixer, we need to find the sink pipes
 *      here and bind to them. This way dynamic connect works.
 *   - Start sink pipeline, if not running
 *   - Then run current pipe
 */
static int skl_tplg_pga_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
1059
							struct skl_dev *skl)
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
{
	struct skl_module_cfg *src_mconfig;
	int ret = 0;

	src_mconfig = w->priv;

	/*
	 * find which sink it is connected to, bind with the sink,
	 * if sink is not started, start sink pipe first, then start
	 * this pipe
	 */
1071
	ret = skl_tplg_bind_sinks(w, skl, w, src_mconfig);
1072 1073 1074 1075
	if (ret)
		return ret;

	/* 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 1080 1081

	return 0;
}

1082
static struct snd_soc_dapm_widget *skl_get_src_dsp_widget(
1083
		struct snd_soc_dapm_widget *w, struct skl_dev *skl)
1084 1085 1086 1087 1088 1089 1090 1091 1092
{
	struct snd_soc_dapm_path *p;
	struct snd_soc_dapm_widget *src_w = NULL;

	snd_soc_dapm_widget_for_each_source_path(w, p) {
		src_w = p->source;
		if (!p->connect)
			continue;

1093 1094
		dev_dbg(skl->dev, "sink widget=%s\n", w->name);
		dev_dbg(skl->dev, "src widget=%s\n", p->source->name);
1095 1096 1097 1098 1099 1100 1101

		/*
		 * here we will check widgets in sink pipelines, so that can
		 * be any widgets type and we are only interested if they are
		 * ones used for SKL so check that first
		 */
		if ((p->source->priv != NULL) &&
1102
				is_skl_dsp_widget_type(p->source, skl->dev)) {
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
			return p->source;
		}
	}

	if (src_w != NULL)
		return skl_get_src_dsp_widget(src_w, skl);

	return NULL;
}

1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
/*
 * in the Post-PMU event of mixer we need to do following:
 *   - Check if this pipe is running
 *   - if not, then
 *	- bind this pipeline to its source pipeline
 *	  if source pipe is already running, this means it is a dynamic
 *	  connection and we need to bind only to that pipe
 *	- start this pipeline
 */
static int skl_tplg_mixer_dapm_post_pmu_event(struct snd_soc_dapm_widget *w,
1123
							struct skl_dev *skl)
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
{
	int ret = 0;
	struct snd_soc_dapm_widget *source, *sink;
	struct skl_module_cfg *src_mconfig, *sink_mconfig;
	int src_pipe_started = 0;

	sink = w;
	sink_mconfig = sink->priv;

	/*
	 * If source pipe is already started, that means source is driving
	 * one more sink before this sink got connected, Since source is
	 * started, bind this sink to source and start this pipe.
	 */
1138 1139 1140 1141 1142
	source = skl_get_src_dsp_widget(w, skl);
	if (source != NULL) {
		src_mconfig = source->priv;
		sink_mconfig = sink->priv;
		src_pipe_started = 1;
1143 1144

		/*
1145 1146
		 * check pipe state, then no need to bind or start the
		 * pipe
1147
		 */
1148 1149
		if (src_mconfig->pipe->state != SKL_PIPE_STARTED)
			src_pipe_started = 0;
1150 1151 1152
	}

	if (src_pipe_started) {
1153
		ret = skl_bind_modules(skl, src_mconfig, sink_mconfig);
1154 1155 1156
		if (ret)
			return ret;

1157
		/* set module params after bind */
1158 1159
		skl_tplg_set_module_bind_params(source, src_mconfig, skl);
		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 1166 1167 1168 1169 1170 1171 1172 1173 1174
	}

	return ret;
}

/*
 * in the Pre-PMD event of mixer we need to do following:
 *   - Stop the pipe
 *   - find the source connections and remove that from dapm_path_list
 *   - unbind with source pipelines if still connected
 */
static 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 1185 1186
	if (ret)
		return ret;

1187
	for (i = 0; i < sink_mconfig->module->max_input_pins; i++) {
1188 1189 1190 1191
		if (sink_mconfig->m_in_pin[i].pin_state == SKL_PIN_BIND_DONE) {
			src_mconfig = sink_mconfig->m_in_pin[i].tgt_mcfg;
			if (!src_mconfig)
				continue;
1192

1193
			ret = skl_unbind_modules(skl,
1194
						src_mconfig, sink_mconfig);
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
		}
	}

	return ret;
}

/*
 * in the Post-PMD event of mixer we need to do following:
 *   - Unbind the modules within the pipeline
 *   - Delete the pipeline (modules are not required to be explicitly
 *     deleted, pipeline delete is enough here
 */
static int skl_tplg_mixer_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
1208
							struct skl_dev *skl)
1209 1210 1211 1212 1213
{
	struct skl_module_cfg *mconfig = w->priv;
	struct skl_pipe_module *w_module;
	struct skl_module_cfg *src_module = NULL, *dst_module;
	struct skl_pipe *s_pipe = mconfig->pipe;
1214
	struct skl_module_deferred_bind *modules, *tmp;
1215

1216 1217 1218
	if (s_pipe->state == SKL_PIPE_INVALID)
		return -EINVAL;

1219 1220 1221 1222 1223 1224
	list_for_each_entry(w_module, &s_pipe->w_list, node) {
		if (list_empty(&skl->bind_list))
			break;

		src_module = w_module->w->priv;

1225
		list_for_each_entry_safe(modules, tmp, &skl->bind_list, node) {
1226 1227 1228 1229 1230
			/*
			 * When the destination module is deleted, Unbind the
			 * modules from deferred bind list.
			 */
			if (modules->dst == src_module) {
1231
				skl_unbind_modules(skl, modules->src,
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
						modules->dst);
			}

			/*
			 * When the source module is deleted, remove this entry
			 * from the deferred bind list.
			 */
			if (modules->src == src_module) {
				list_del(&modules->node);
				modules->src = NULL;
				modules->dst = NULL;
				kfree(modules);
			}
		}
	}

1248 1249 1250 1251 1252 1253 1254 1255
	list_for_each_entry(w_module, &s_pipe->w_list, node) {
		dst_module = w_module->w->priv;

		if (src_module == NULL) {
			src_module = dst_module;
			continue;
		}

1256
		skl_unbind_modules(skl, src_module, dst_module);
1257 1258 1259
		src_module = dst_module;
	}

1260
	skl_delete_pipe(skl, mconfig->pipe);
1261

1262 1263 1264 1265 1266
	list_for_each_entry(w_module, &s_pipe->w_list, node) {
		src_module = w_module->w->priv;
		src_module->m_state = SKL_MODULE_UNINIT;
	}

1267
	return skl_tplg_unload_pipe_modules(skl, s_pipe);
1268 1269 1270 1271 1272 1273 1274 1275
}

/*
 * in the Post-PMD event of PGA we need to do following:
 *   - Stop the pipeline
 *   - In source pipe is connected, unbind with source pipelines
 */
static 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 1286 1287
	if (ret)
		return ret;

1288
	for (i = 0; i < src_mconfig->module->max_output_pins; i++) {
1289 1290 1291 1292 1293 1294 1295 1296
		if (src_mconfig->m_out_pin[i].pin_state == SKL_PIN_BIND_DONE) {
			sink_mconfig = src_mconfig->m_out_pin[i].tgt_mcfg;
			if (!sink_mconfig)
				continue;
			/*
			 * This is a connecter and if path is found that means
			 * unbind between source and sink has not happened yet
			 */
1297
			ret = skl_unbind_modules(skl, src_mconfig,
1298
							sink_mconfig);
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
		}
	}

	return ret;
}

/*
 * In modelling, we assume there will be ONLY one mixer in a pipeline. If a
 * second one is required that is created as another pipe entity.
 * The mixer is responsible for pipe management and represent a pipeline
 * instance
 */
static int skl_tplg_mixer_event(struct snd_soc_dapm_widget *w,
				struct snd_kcontrol *k, int event)
{
	struct snd_soc_dapm_context *dapm = w->dapm;
1315
	struct skl_dev *skl = get_skl_ctx(dapm->dev);
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344

	switch (event) {
	case SND_SOC_DAPM_PRE_PMU:
		return skl_tplg_mixer_dapm_pre_pmu_event(w, skl);

	case SND_SOC_DAPM_POST_PMU:
		return skl_tplg_mixer_dapm_post_pmu_event(w, skl);

	case SND_SOC_DAPM_PRE_PMD:
		return skl_tplg_mixer_dapm_pre_pmd_event(w, skl);

	case SND_SOC_DAPM_POST_PMD:
		return skl_tplg_mixer_dapm_post_pmd_event(w, skl);
	}

	return 0;
}

/*
 * In modelling, we assumed rest of the modules in pipeline are PGA. But we
 * are interested in last PGA (leaf PGA) in a pipeline to disconnect with
 * the sink when it is running (two FE to one BE or one FE to two BE)
 * scenarios
 */
static int skl_tplg_pga_event(struct snd_soc_dapm_widget *w,
			struct snd_kcontrol *k, int event)

{
	struct snd_soc_dapm_context *dapm = w->dapm;
1345
	struct skl_dev *skl = get_skl_ctx(dapm->dev);
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356

	switch (event) {
	case SND_SOC_DAPM_PRE_PMU:
		return skl_tplg_pga_dapm_pre_pmu_event(w, skl);

	case SND_SOC_DAPM_POST_PMD:
		return skl_tplg_pga_dapm_post_pmd_event(w, skl);
	}

	return 0;
}
1357

1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
static int skl_tplg_multi_config_set_get(struct snd_kcontrol *kcontrol,
					 struct snd_ctl_elem_value *ucontrol,
					 bool is_set)
{
	struct snd_soc_component *component =
		snd_soc_kcontrol_component(kcontrol);
	struct hdac_bus *bus = snd_soc_component_get_drvdata(component);
	struct skl_dev *skl = bus_to_skl(bus);
	struct skl_pipeline *ppl;
	struct skl_pipe *pipe = NULL;
	struct soc_enum *ec = (struct soc_enum *)kcontrol->private_value;
	u32 *pipe_id;

	if (!ec)
		return -EINVAL;

	if (is_set && ucontrol->value.enumerated.item[0] > ec->items)
		return -EINVAL;

	pipe_id = ec->dobj.private;

	list_for_each_entry(ppl, &skl->ppl_list, node) {
		if (ppl->pipe->ppl_id == *pipe_id) {
			pipe = ppl->pipe;
			break;
		}
	}
	if (!pipe)
		return -EIO;

	if (is_set)
		pipe->pipe_config_idx = ucontrol->value.enumerated.item[0];
	else
		ucontrol->value.enumerated.item[0]  =  pipe->pipe_config_idx;

	return 0;
}

static int skl_tplg_multi_config_get(struct snd_kcontrol *kcontrol,
				     struct snd_ctl_elem_value *ucontrol)
{
	return skl_tplg_multi_config_set_get(kcontrol, ucontrol, false);
}

static int skl_tplg_multi_config_set(struct snd_kcontrol *kcontrol,
				     struct snd_ctl_elem_value *ucontrol)
{
	return skl_tplg_multi_config_set_get(kcontrol, ucontrol, true);
}

1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
static int skl_tplg_multi_config_get_dmic(struct snd_kcontrol *kcontrol,
					  struct snd_ctl_elem_value *ucontrol)
{
	return skl_tplg_multi_config_set_get(kcontrol, ucontrol, false);
}

static int skl_tplg_multi_config_set_dmic(struct snd_kcontrol *kcontrol,
					  struct snd_ctl_elem_value *ucontrol)
{
	return skl_tplg_multi_config_set_get(kcontrol, ucontrol, true);
}

1420 1421 1422 1423 1424 1425
static int skl_tplg_tlv_control_get(struct snd_kcontrol *kcontrol,
			unsigned int __user *data, unsigned int size)
{
	struct soc_bytes_ext *sb =
			(struct soc_bytes_ext *)kcontrol->private_value;
	struct skl_algo_data *bc = (struct skl_algo_data *)sb->dobj.private;
1426 1427
	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
	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 1435 1436 1437 1438 1439 1440
	/* decrement size for TLV header */
	size -= 2 * sizeof(u32);

	/* check size as we don't want to send kernel data */
	if (size > bc->max)
		size = bc->max;

1441 1442 1443
	if (bc->params) {
		if (copy_to_user(data, &bc->param_id, sizeof(u32)))
			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 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
			return -EFAULT;
	}

	return 0;
}

#define SKL_PARAM_VENDOR_ID 0xff

static int skl_tplg_tlv_control_set(struct snd_kcontrol *kcontrol,
			const unsigned int __user *data, unsigned int size)
{
	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
	struct skl_module_cfg *mconfig = w->priv;
	struct soc_bytes_ext *sb =
			(struct soc_bytes_ext *)kcontrol->private_value;
	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 1467 1468 1469 1470 1471
		/*
		 * Widget data is expected to be stripped of T and L
		 */
		size -= 2 * sizeof(unsigned int);
		data += 2;

1472 1473 1474
		if (size > ac->max)
			return -EINVAL;
		ac->size = size;
1475 1476 1477

		if (copy_from_user(ac->params, data, size))
			return -EFAULT;
1478 1479

		if (w->power)
1480
			return skl_set_module_params(skl,
1481
						(u32 *)ac->params, ac->size,
1482 1483 1484 1485 1486 1487
						ac->param_id, mconfig);
	}

	return 0;
}

1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592
static int skl_tplg_mic_control_get(struct snd_kcontrol *kcontrol,
		struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
	struct skl_module_cfg *mconfig = w->priv;
	struct soc_enum *ec = (struct soc_enum *)kcontrol->private_value;
	u32 ch_type = *((u32 *)ec->dobj.private);

	if (mconfig->dmic_ch_type == ch_type)
		ucontrol->value.enumerated.item[0] =
					mconfig->dmic_ch_combo_index;
	else
		ucontrol->value.enumerated.item[0] = 0;

	return 0;
}

static int skl_fill_mic_sel_params(struct skl_module_cfg *mconfig,
	struct skl_mic_sel_config *mic_cfg, struct device *dev)
{
	struct skl_specific_cfg *sp_cfg = &mconfig->formats_config;

	sp_cfg->caps_size = sizeof(struct skl_mic_sel_config);
	sp_cfg->set_params = SKL_PARAM_SET;
	sp_cfg->param_id = 0x00;
	if (!sp_cfg->caps) {
		sp_cfg->caps = devm_kzalloc(dev, sp_cfg->caps_size, GFP_KERNEL);
		if (!sp_cfg->caps)
			return -ENOMEM;
	}

	mic_cfg->mic_switch = SKL_MIC_SEL_SWITCH;
	mic_cfg->flags = 0;
	memcpy(sp_cfg->caps, mic_cfg, sp_cfg->caps_size);

	return 0;
}

static int skl_tplg_mic_control_set(struct snd_kcontrol *kcontrol,
			struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
	struct skl_module_cfg *mconfig = w->priv;
	struct skl_mic_sel_config mic_cfg = {0};
	struct soc_enum *ec = (struct soc_enum *)kcontrol->private_value;
	u32 ch_type = *((u32 *)ec->dobj.private);
	const int *list;
	u8 in_ch, out_ch, index;

	mconfig->dmic_ch_type = ch_type;
	mconfig->dmic_ch_combo_index = ucontrol->value.enumerated.item[0];

	/* enum control index 0 is INVALID, so no channels to be set */
	if (mconfig->dmic_ch_combo_index == 0)
		return 0;

	/* No valid channel selection map for index 0, so offset by 1 */
	index = mconfig->dmic_ch_combo_index - 1;

	switch (ch_type) {
	case SKL_CH_MONO:
		if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_mono_list))
			return -EINVAL;

		list = &mic_mono_list[index];
		break;

	case SKL_CH_STEREO:
		if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_stereo_list))
			return -EINVAL;

		list = mic_stereo_list[index];
		break;

	case SKL_CH_TRIO:
		if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_trio_list))
			return -EINVAL;

		list = mic_trio_list[index];
		break;

	case SKL_CH_QUATRO:
		if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_quatro_list))
			return -EINVAL;

		list = mic_quatro_list[index];
		break;

	default:
		dev_err(w->dapm->dev,
				"Invalid channel %d for mic_select module\n",
				ch_type);
		return -EINVAL;

	}

	/* channel type enum map to number of chanels for that type */
	for (out_ch = 0; out_ch < ch_type; out_ch++) {
		in_ch = list[out_ch];
		mic_cfg.blob[out_ch][in_ch] = SKL_DEFAULT_MIC_SEL_GAIN;
	}

	return skl_fill_mic_sel_params(mconfig, &mic_cfg, w->dapm->dev);
}

1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606
/*
 * Fill the dma id for host and link. In case of passthrough
 * pipeline, this will both host and link in the same
 * pipeline, so need to copy the link and host based on dev_type
 */
static void skl_tplg_fill_dma_id(struct skl_module_cfg *mcfg,
				struct skl_pipe_params *params)
{
	struct skl_pipe *pipe = mcfg->pipe;

	if (pipe->passthru) {
		switch (mcfg->dev_type) {
		case SKL_DEVICE_HDALINK:
			pipe->p_params->link_dma_id = params->link_dma_id;
1607
			pipe->p_params->link_index = params->link_index;
1608
			pipe->p_params->link_bps = params->link_bps;
1609 1610 1611 1612
			break;

		case SKL_DEVICE_HDAHOST:
			pipe->p_params->host_dma_id = params->host_dma_id;
1613
			pipe->p_params->host_bps = params->host_bps;
1614 1615 1616 1617 1618 1619 1620 1621 1622
			break;

		default:
			break;
		}
		pipe->p_params->s_fmt = params->s_fmt;
		pipe->p_params->ch = params->ch;
		pipe->p_params->s_freq = params->s_freq;
		pipe->p_params->stream = params->stream;
1623
		pipe->p_params->format = params->format;
1624 1625 1626 1627 1628 1629

	} else {
		memcpy(pipe->p_params, params, sizeof(*params));
	}
}

1630 1631 1632 1633 1634 1635 1636 1637 1638 1639
/*
 * The FE params are passed by hw_params of the DAI.
 * On hw_params, the params are stored in Gateway module of the FE and we
 * need to calculate the format in DSP module configuration, that
 * conversion is done here
 */
int skl_tplg_update_pipe_params(struct device *dev,
			struct skl_module_cfg *mconfig,
			struct skl_pipe_params *params)
{
1640
	struct skl_module_res *res = &mconfig->module->resources[0];
1641
	struct skl_dev *skl = get_skl_ctx(dev);
1642
	struct skl_module_fmt *format = NULL;
1643
	u8 cfg_idx = mconfig->pipe->cur_config_idx;
1644

1645
	skl_tplg_fill_dma_id(mconfig, params);
1646 1647 1648 1649 1650
	mconfig->fmt_idx = mconfig->mod_cfg[cfg_idx].fmt_idx;
	mconfig->res_idx = mconfig->mod_cfg[cfg_idx].res_idx;

	if (skl->nr_modules)
		return 0;
1651 1652

	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK)
1653
		format = &mconfig->module->formats[0].inputs[0].fmt;
1654
	else
1655
		format = &mconfig->module->formats[0].outputs[0].fmt;
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671

	/* set the hw_params */
	format->s_freq = params->s_freq;
	format->channels = params->ch;
	format->valid_bit_depth = skl_get_bit_depth(params->s_fmt);

	/*
	 * 16 bit is 16 bit container whereas 24 bit is in 32 bit
	 * container so update bit depth accordingly
	 */
	switch (format->valid_bit_depth) {
	case SKL_DEPTH_16BIT:
		format->bit_depth = format->valid_bit_depth;
		break;

	case SKL_DEPTH_24BIT:
1672
	case SKL_DEPTH_32BIT:
1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
		format->bit_depth = SKL_DEPTH_32BIT;
		break;

	default:
		dev_err(dev, "Invalid bit depth %x for pipe\n",
				format->valid_bit_depth);
		return -EINVAL;
	}

	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1683
		res->ibs = (format->s_freq / 1000) *
1684 1685 1686
				(format->channels) *
				(format->bit_depth >> 3);
	} else {
1687
		res->obs = (format->s_freq / 1000) *
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707
				(format->channels) *
				(format->bit_depth >> 3);
	}

	return 0;
}

/*
 * Query the module config for the FE DAI
 * This is used to find the hw_params set for that DAI and apply to FE
 * pipeline
 */
struct skl_module_cfg *
skl_tplg_fe_get_cpr_module(struct snd_soc_dai *dai, int stream)
{
	struct snd_soc_dapm_widget *w;
	struct snd_soc_dapm_path *p = NULL;

	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
		w = dai->playback_widget;
1708
		snd_soc_dapm_widget_for_each_sink_path(w, p) {
1709
			if (p->connect && p->sink->power &&
1710
				!is_skl_dsp_widget_type(p->sink, dai->dev))
1711 1712 1713 1714 1715 1716 1717 1718 1719 1720
				continue;

			if (p->sink->priv) {
				dev_dbg(dai->dev, "set params for %s\n",
						p->sink->name);
				return p->sink->priv;
			}
		}
	} else {
		w = dai->capture_widget;
1721
		snd_soc_dapm_widget_for_each_source_path(w, p) {
1722
			if (p->connect && p->source->power &&
1723
				!is_skl_dsp_widget_type(p->source, dai->dev))
1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736
				continue;

			if (p->source->priv) {
				dev_dbg(dai->dev, "set params for %s\n",
						p->source->name);
				return p->source->priv;
			}
		}
	}

	return NULL;
}

1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796
static struct skl_module_cfg *skl_get_mconfig_pb_cpr(
		struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
{
	struct snd_soc_dapm_path *p;
	struct skl_module_cfg *mconfig = NULL;

	snd_soc_dapm_widget_for_each_source_path(w, p) {
		if (w->endpoints[SND_SOC_DAPM_DIR_OUT] > 0) {
			if (p->connect &&
				    (p->sink->id == snd_soc_dapm_aif_out) &&
				    p->source->priv) {
				mconfig = p->source->priv;
				return mconfig;
			}
			mconfig = skl_get_mconfig_pb_cpr(dai, p->source);
			if (mconfig)
				return mconfig;
		}
	}
	return mconfig;
}

static struct skl_module_cfg *skl_get_mconfig_cap_cpr(
		struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
{
	struct snd_soc_dapm_path *p;
	struct skl_module_cfg *mconfig = NULL;

	snd_soc_dapm_widget_for_each_sink_path(w, p) {
		if (w->endpoints[SND_SOC_DAPM_DIR_IN] > 0) {
			if (p->connect &&
				    (p->source->id == snd_soc_dapm_aif_in) &&
				    p->sink->priv) {
				mconfig = p->sink->priv;
				return mconfig;
			}
			mconfig = skl_get_mconfig_cap_cpr(dai, p->sink);
			if (mconfig)
				return mconfig;
		}
	}
	return mconfig;
}

struct skl_module_cfg *
skl_tplg_be_get_cpr_module(struct snd_soc_dai *dai, int stream)
{
	struct snd_soc_dapm_widget *w;
	struct skl_module_cfg *mconfig;

	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
		w = dai->playback_widget;
		mconfig = skl_get_mconfig_pb_cpr(dai, w);
	} else {
		w = dai->capture_widget;
		mconfig = skl_get_mconfig_cap_cpr(dai, w);
	}
	return mconfig;
}

1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837
static u8 skl_tplg_be_link_type(int dev_type)
{
	int ret;

	switch (dev_type) {
	case SKL_DEVICE_BT:
		ret = NHLT_LINK_SSP;
		break;

	case SKL_DEVICE_DMIC:
		ret = NHLT_LINK_DMIC;
		break;

	case SKL_DEVICE_I2S:
		ret = NHLT_LINK_SSP;
		break;

	case SKL_DEVICE_HDALINK:
		ret = NHLT_LINK_HDA;
		break;

	default:
		ret = NHLT_LINK_INVALID;
		break;
	}

	return ret;
}

/*
 * Fill the BE gateway parameters
 * The BE gateway expects a blob of parameters which are kept in the ACPI
 * NHLT blob, so query the blob for interface type (i2s/pdm) and instance.
 * The port can have multiple settings so pick based on the PCM
 * parameters
 */
static int skl_tplg_be_fill_pipe_params(struct snd_soc_dai *dai,
				struct skl_module_cfg *mconfig,
				struct skl_pipe_params *params)
{
	struct nhlt_specific_cfg *cfg;
1838
	struct skl_dev *skl = get_skl_ctx(dai->dev);
1839
	int link_type = skl_tplg_be_link_type(mconfig->dev_type);
1840
	u8 dev_type = skl_tplg_be_dev_type(mconfig->dev_type);
1841

1842
	skl_tplg_fill_dma_id(mconfig, params);
1843

1844 1845 1846
	if (link_type == NHLT_LINK_HDA)
		return 0;

1847 1848 1849
	/* update the blob based on virtual bus_id*/
	cfg = skl_get_ep_blob(skl, mconfig->vbus_id, link_type,
					params->s_fmt, params->ch,
1850 1851
					params->s_freq, params->stream,
					dev_type);
1852 1853
	if (cfg) {
		mconfig->formats_config.caps_size = cfg->size;
1854
		mconfig->formats_config.caps = (u32 *) &cfg->caps;
1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871
	} else {
		dev_err(dai->dev, "Blob NULL for id %x type %d dirn %d\n",
					mconfig->vbus_id, link_type,
					params->stream);
		dev_err(dai->dev, "PCM: ch %d, freq %d, fmt %d\n",
				 params->ch, params->s_freq, params->s_fmt);
		return -EINVAL;
	}

	return 0;
}

static int skl_tplg_be_set_src_pipe_params(struct snd_soc_dai *dai,
				struct snd_soc_dapm_widget *w,
				struct skl_pipe_params *params)
{
	struct snd_soc_dapm_path *p;
1872
	int ret = -EIO;
1873

1874
	snd_soc_dapm_widget_for_each_source_path(w, p) {
1875
		if (p->connect && is_skl_dsp_widget_type(p->source, dai->dev) &&
1876 1877
						p->source->priv) {

1878 1879 1880 1881
			ret = skl_tplg_be_fill_pipe_params(dai,
						p->source->priv, params);
			if (ret < 0)
				return ret;
1882
		} else {
1883 1884
			ret = skl_tplg_be_set_src_pipe_params(dai,
						p->source, params);
1885 1886
			if (ret < 0)
				return ret;
1887 1888 1889
		}
	}

1890
	return ret;
1891 1892 1893 1894 1895 1896
}

static int skl_tplg_be_set_sink_pipe_params(struct snd_soc_dai *dai,
	struct snd_soc_dapm_widget *w, struct skl_pipe_params *params)
{
	struct snd_soc_dapm_path *p = NULL;
1897
	int ret = -EIO;
1898

1899
	snd_soc_dapm_widget_for_each_sink_path(w, p) {
1900
		if (p->connect && is_skl_dsp_widget_type(p->sink, dai->dev) &&
1901 1902
						p->sink->priv) {

1903 1904 1905 1906
			ret = skl_tplg_be_fill_pipe_params(dai,
						p->sink->priv, params);
			if (ret < 0)
				return ret;
1907
		} else {
1908
			ret = skl_tplg_be_set_sink_pipe_params(
1909
						dai, p->sink, params);
1910 1911
			if (ret < 0)
				return ret;
1912 1913 1914
		}
	}

1915
	return ret;
1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940
}

/*
 * BE hw_params can be a source parameters (capture) or sink parameters
 * (playback). Based on sink and source we need to either find the source
 * list or the sink list and set the pipeline parameters
 */
int skl_tplg_be_update_params(struct snd_soc_dai *dai,
				struct skl_pipe_params *params)
{
	struct snd_soc_dapm_widget *w;

	if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
		w = dai->playback_widget;

		return skl_tplg_be_set_src_pipe_params(dai, w, params);

	} else {
		w = dai->capture_widget;

		return skl_tplg_be_set_sink_pipe_params(dai, w, params);
	}

	return 0;
}
1941 1942 1943

static const struct snd_soc_tplg_widget_events skl_tplg_widget_ops[] = {
	{SKL_MIXER_EVENT, skl_tplg_mixer_event},
1944
	{SKL_VMIXER_EVENT, skl_tplg_mixer_event},
1945 1946 1947
	{SKL_PGA_EVENT, skl_tplg_pga_event},
};

1948 1949 1950 1951 1952
static const struct snd_soc_tplg_bytes_ext_ops skl_tlv_ops[] = {
	{SKL_CONTROL_TYPE_BYTE_TLV, skl_tplg_tlv_control_get,
					skl_tplg_tlv_control_set},
};

1953 1954 1955 1956 1957 1958
static const struct snd_soc_tplg_kcontrol_ops skl_tplg_kcontrol_ops[] = {
	{
		.id = SKL_CONTROL_TYPE_MIC_SELECT,
		.get = skl_tplg_mic_control_get,
		.put = skl_tplg_mic_control_set,
	},
1959 1960 1961 1962 1963
	{
		.id = SKL_CONTROL_TYPE_MULTI_IO_SELECT,
		.get = skl_tplg_multi_config_get,
		.put = skl_tplg_multi_config_set,
	},
1964 1965 1966 1967 1968
	{
		.id = SKL_CONTROL_TYPE_MULTI_IO_SELECT_DMIC,
		.get = skl_tplg_multi_config_get_dmic,
		.put = skl_tplg_multi_config_set_dmic,
	}
1969 1970
};

1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
static int skl_tplg_fill_pipe_cfg(struct device *dev,
			struct skl_pipe *pipe, u32 tkn,
			u32 tkn_val, int conf_idx, int dir)
{
	struct skl_pipe_fmt *fmt;
	struct skl_path_config *config;

	switch (dir) {
	case SKL_DIR_IN:
		fmt = &pipe->configs[conf_idx].in_fmt;
		break;

	case SKL_DIR_OUT:
		fmt = &pipe->configs[conf_idx].out_fmt;
		break;

	default:
		dev_err(dev, "Invalid direction: %d\n", dir);
		return -EINVAL;
	}

	config = &pipe->configs[conf_idx];

	switch (tkn) {
	case SKL_TKN_U32_CFG_FREQ:
		fmt->freq = tkn_val;
		break;

	case SKL_TKN_U8_CFG_CHAN:
		fmt->channels = tkn_val;
		break;

	case SKL_TKN_U8_CFG_BPS:
		fmt->bps = tkn_val;
		break;

	case SKL_TKN_U32_PATH_MEM_PGS:
		config->mem_pages = tkn_val;
		break;

	default:
		dev_err(dev, "Invalid token config: %d\n", tkn);
		return -EINVAL;
	}

	return 0;
}

2019 2020 2021
static int skl_tplg_fill_pipe_tkn(struct device *dev,
			struct skl_pipe *pipe, u32 tkn,
			u32 tkn_val)
2022 2023
{

2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036
	switch (tkn) {
	case SKL_TKN_U32_PIPE_CONN_TYPE:
		pipe->conn_type = tkn_val;
		break;

	case SKL_TKN_U32_PIPE_PRIORITY:
		pipe->pipe_priority = tkn_val;
		break;

	case SKL_TKN_U32_PIPE_MEM_PGS:
		pipe->memory_pages = tkn_val;
		break;

2037 2038 2039 2040
	case SKL_TKN_U32_PMODE:
		pipe->lp_mode = tkn_val;
		break;

2041 2042 2043 2044 2045 2046 2047 2048
	case SKL_TKN_U32_PIPE_DIRECTION:
		pipe->direction = tkn_val;
		break;

	case SKL_TKN_U32_NUM_CONFIGS:
		pipe->nr_cfgs = tkn_val;
		break;

2049 2050 2051
	default:
		dev_err(dev, "Token not handled %d\n", tkn);
		return -EINVAL;
2052
	}
2053 2054

	return 0;
2055 2056 2057
}

/*
2058 2059
 * Add pipeline by parsing the relevant tokens
 * Return an existing pipe if the pipe already exists.
2060
 */
2061
static int skl_tplg_add_pipe(struct device *dev,
2062
		struct skl_module_cfg *mconfig, struct skl_dev *skl,
2063
		struct snd_soc_tplg_vendor_value_elem *tkn_elem)
2064 2065 2066 2067 2068 2069
{
	struct skl_pipeline *ppl;
	struct skl_pipe *pipe;
	struct skl_pipe_params *params;

	list_for_each_entry(ppl, &skl->ppl_list, node) {
2070 2071
		if (ppl->pipe->ppl_id == tkn_elem->value) {
			mconfig->pipe = ppl->pipe;
2072
			return -EEXIST;
2073
		}
2074 2075 2076 2077
	}

	ppl = devm_kzalloc(dev, sizeof(*ppl), GFP_KERNEL);
	if (!ppl)
2078
		return -ENOMEM;
2079 2080 2081

	pipe = devm_kzalloc(dev, sizeof(*pipe), GFP_KERNEL);
	if (!pipe)
2082
		return -ENOMEM;
2083 2084 2085

	params = devm_kzalloc(dev, sizeof(*params), GFP_KERNEL);
	if (!params)
2086
		return -ENOMEM;
2087 2088

	pipe->p_params = params;
2089
	pipe->ppl_id = tkn_elem->value;
2090 2091 2092 2093 2094
	INIT_LIST_HEAD(&pipe->w_list);

	ppl->pipe = pipe;
	list_add(&ppl->node, &skl->ppl_list);

2095 2096 2097 2098 2099 2100
	mconfig->pipe = pipe;
	mconfig->pipe->state = SKL_PIPE_INVALID;

	return 0;
}

2101
static int skl_tplg_get_uuid(struct device *dev, guid_t *guid,
2102 2103 2104
	      struct snd_soc_tplg_vendor_uuid_elem *uuid_tkn)
{
	if (uuid_tkn->token == SKL_TKN_UUID) {
2105
		import_guid(guid, uuid_tkn->uuid);
2106 2107 2108 2109 2110 2111 2112 2113 2114 2115
		return 0;
	}

	dev_err(dev, "Not an UUID token %d\n", uuid_tkn->token);

	return -EINVAL;
}

static int skl_tplg_fill_pin(struct device *dev,
			struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2116
			struct skl_module_pin *m_pin,
2117
			int pin_index)
2118
{
2119 2120
	int ret;

2121
	switch (tkn_elem->token) {
2122
	case SKL_TKN_U32_PIN_MOD_ID:
2123
		m_pin[pin_index].id.module_id = tkn_elem->value;
2124 2125 2126
		break;

	case SKL_TKN_U32_PIN_INST_ID:
2127
		m_pin[pin_index].id.instance_id = tkn_elem->value;
2128 2129
		break;

2130
	case SKL_TKN_UUID:
2131
		ret = skl_tplg_get_uuid(dev, &m_pin[pin_index].id.mod_uuid,
2132 2133 2134 2135
			(struct snd_soc_tplg_vendor_uuid_elem *)tkn_elem);
		if (ret < 0)
			return ret;

2136 2137 2138
		break;

	default:
2139
		dev_err(dev, "%d Not a pin token\n", tkn_elem->token);
2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167
		return -EINVAL;
	}

	return 0;
}

/*
 * Parse for pin config specific tokens to fill up the
 * module private data
 */
static int skl_tplg_fill_pins_info(struct device *dev,
		struct skl_module_cfg *mconfig,
		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
		int dir, int pin_count)
{
	int ret;
	struct skl_module_pin *m_pin;

	switch (dir) {
	case SKL_DIR_IN:
		m_pin = mconfig->m_in_pin;
		break;

	case SKL_DIR_OUT:
		m_pin = mconfig->m_out_pin;
		break;

	default:
2168
		dev_err(dev, "Invalid direction value\n");
2169 2170 2171
		return -EINVAL;
	}

2172
	ret = skl_tplg_fill_pin(dev, tkn_elem, m_pin, pin_count);
2173 2174 2175 2176 2177 2178 2179
	if (ret < 0)
		return ret;

	m_pin[pin_count].in_use = false;
	m_pin[pin_count].pin_state = SKL_PIN_UNBIND;

	return 0;
2180 2181
}

2182 2183 2184 2185 2186
/*
 * Fill up input/output module config format based
 * on the direction
 */
static int skl_tplg_fill_fmt(struct device *dev,
2187 2188
		struct skl_module_fmt *dst_fmt,
		u32 tkn, u32 value)
2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223
{
	switch (tkn) {
	case SKL_TKN_U32_FMT_CH:
		dst_fmt->channels  = value;
		break;

	case SKL_TKN_U32_FMT_FREQ:
		dst_fmt->s_freq = value;
		break;

	case SKL_TKN_U32_FMT_BIT_DEPTH:
		dst_fmt->bit_depth = value;
		break;

	case SKL_TKN_U32_FMT_SAMPLE_SIZE:
		dst_fmt->valid_bit_depth = value;
		break;

	case SKL_TKN_U32_FMT_CH_CONFIG:
		dst_fmt->ch_cfg = value;
		break;

	case SKL_TKN_U32_FMT_INTERLEAVE:
		dst_fmt->interleaving_style = value;
		break;

	case SKL_TKN_U32_FMT_SAMPLE_TYPE:
		dst_fmt->sample_type = value;
		break;

	case SKL_TKN_U32_FMT_CH_MAP:
		dst_fmt->ch_map = value;
		break;

	default:
2224
		dev_err(dev, "Invalid token %d\n", tkn);
2225 2226 2227 2228 2229 2230
		return -EINVAL;
	}

	return 0;
}

2231
static int skl_tplg_widget_fill_fmt(struct device *dev,
2232
		struct skl_module_iface *fmt,
2233 2234 2235 2236
		u32 tkn, u32 val, u32 dir, int fmt_idx)
{
	struct skl_module_fmt *dst_fmt;

2237 2238 2239
	if (!fmt)
		return -EINVAL;

2240 2241
	switch (dir) {
	case SKL_DIR_IN:
2242
		dst_fmt = &fmt->inputs[fmt_idx].fmt;
2243 2244 2245
		break;

	case SKL_DIR_OUT:
2246
		dst_fmt = &fmt->outputs[fmt_idx].fmt;
2247 2248 2249 2250 2251 2252 2253 2254 2255 2256
		break;

	default:
		dev_err(dev, "Invalid direction: %d\n", dir);
		return -EINVAL;
	}

	return skl_tplg_fill_fmt(dev, dst_fmt, tkn, val);
}

2257 2258
static void skl_tplg_fill_pin_dynamic_val(
		struct skl_module_pin *mpin, u32 pin_count, u32 value)
2259 2260 2261
{
	int i;

2262 2263 2264 2265
	for (i = 0; i < pin_count; i++)
		mpin[i].is_dynamic = value;
}

2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349
/*
 * Resource table in the manifest has pin specific resources
 * like pin and pin buffer size
 */
static int skl_tplg_manifest_pin_res_tkn(struct device *dev,
		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
		struct skl_module_res *res, int pin_idx, int dir)
{
	struct skl_module_pin_resources *m_pin;

	switch (dir) {
	case SKL_DIR_IN:
		m_pin = &res->input[pin_idx];
		break;

	case SKL_DIR_OUT:
		m_pin = &res->output[pin_idx];
		break;

	default:
		dev_err(dev, "Invalid pin direction: %d\n", dir);
		return -EINVAL;
	}

	switch (tkn_elem->token) {
	case SKL_TKN_MM_U32_RES_PIN_ID:
		m_pin->pin_index = tkn_elem->value;
		break;

	case SKL_TKN_MM_U32_PIN_BUF:
		m_pin->buf_size = tkn_elem->value;
		break;

	default:
		dev_err(dev, "Invalid token: %d\n", tkn_elem->token);
		return -EINVAL;
	}

	return 0;
}

/*
 * Fill module specific resources from the manifest's resource
 * table like CPS, DMA size, mem_pages.
 */
static int skl_tplg_fill_res_tkn(struct device *dev,
		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
		struct skl_module_res *res,
		int pin_idx, int dir)
{
	int ret, tkn_count = 0;

	if (!res)
		return -EINVAL;

	switch (tkn_elem->token) {
	case SKL_TKN_MM_U32_DMA_SIZE:
		res->dma_buffer_size = tkn_elem->value;
		break;

	case SKL_TKN_MM_U32_CPC:
		res->cpc = tkn_elem->value;
		break;

	case SKL_TKN_U32_MEM_PAGES:
		res->is_pages = tkn_elem->value;
		break;

	case SKL_TKN_U32_OBS:
		res->obs = tkn_elem->value;
		break;

	case SKL_TKN_U32_IBS:
		res->ibs = tkn_elem->value;
		break;

	case SKL_TKN_MM_U32_RES_PIN_ID:
	case SKL_TKN_MM_U32_PIN_BUF:
		ret = skl_tplg_manifest_pin_res_tkn(dev, tkn_elem, res,
						    pin_idx, dir);
		if (ret < 0)
			return ret;
		break;

2350 2351 2352 2353 2354
	case SKL_TKN_MM_U32_CPS:
	case SKL_TKN_U32_MAX_MCPS:
		/* ignore unused tokens */
		break;

2355 2356 2357 2358 2359 2360 2361 2362 2363 2364
	default:
		dev_err(dev, "Not a res type token: %d", tkn_elem->token);
		return -EINVAL;

	}
	tkn_count++;

	return tkn_count;
}

2365 2366 2367 2368 2369
/*
 * Parse tokens to fill up the module private data
 */
static int skl_tplg_get_token(struct device *dev,
		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2370
		struct skl_dev *skl, struct skl_module_cfg *mconfig)
2371 2372 2373 2374
{
	int tkn_count = 0;
	int ret;
	static int is_pipe_exists;
2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389
	static int pin_index, dir, conf_idx;
	struct skl_module_iface *iface = NULL;
	struct skl_module_res *res = NULL;
	int res_idx = mconfig->res_idx;
	int fmt_idx = mconfig->fmt_idx;

	/*
	 * If the manifest structure contains no modules, fill all
	 * the module data to 0th index.
	 * res_idx and fmt_idx are default set to 0.
	 */
	if (skl->nr_modules == 0) {
		res = &mconfig->module->resources[res_idx];
		iface = &mconfig->module->formats[fmt_idx];
	}
2390 2391 2392 2393 2394 2395

	if (tkn_elem->token > SKL_TKN_MAX)
		return -EINVAL;

	switch (tkn_elem->token) {
	case SKL_TKN_U8_IN_QUEUE_COUNT:
2396
		mconfig->module->max_input_pins = tkn_elem->value;
2397 2398 2399
		break;

	case SKL_TKN_U8_OUT_QUEUE_COUNT:
2400
		mconfig->module->max_output_pins = tkn_elem->value;
2401 2402 2403
		break;

	case SKL_TKN_U8_DYN_IN_PIN:
2404
		if (!mconfig->m_in_pin)
2405 2406 2407 2408
			mconfig->m_in_pin =
				devm_kcalloc(dev, MAX_IN_QUEUE,
					     sizeof(*mconfig->m_in_pin),
					     GFP_KERNEL);
2409 2410 2411
		if (!mconfig->m_in_pin)
			return -ENOMEM;

2412 2413
		skl_tplg_fill_pin_dynamic_val(mconfig->m_in_pin, MAX_IN_QUEUE,
					      tkn_elem->value);
2414 2415 2416
		break;

	case SKL_TKN_U8_DYN_OUT_PIN:
2417
		if (!mconfig->m_out_pin)
2418 2419 2420 2421
			mconfig->m_out_pin =
				devm_kcalloc(dev, MAX_IN_QUEUE,
					     sizeof(*mconfig->m_in_pin),
					     GFP_KERNEL);
2422 2423 2424
		if (!mconfig->m_out_pin)
			return -ENOMEM;

2425 2426
		skl_tplg_fill_pin_dynamic_val(mconfig->m_out_pin, MAX_OUT_QUEUE,
					      tkn_elem->value);
2427 2428 2429 2430 2431 2432 2433 2434
		break;

	case SKL_TKN_U8_TIME_SLOT:
		mconfig->time_slot = tkn_elem->value;
		break;

	case SKL_TKN_U8_CORE_ID:
		mconfig->core_id = tkn_elem->value;
2435
		break;
2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457

	case SKL_TKN_U8_MOD_TYPE:
		mconfig->m_type = tkn_elem->value;
		break;

	case SKL_TKN_U8_DEV_TYPE:
		mconfig->dev_type = tkn_elem->value;
		break;

	case SKL_TKN_U8_HW_CONN_TYPE:
		mconfig->hw_conn_type = tkn_elem->value;
		break;

	case SKL_TKN_U16_MOD_INST_ID:
		mconfig->id.instance_id =
		tkn_elem->value;
		break;

	case SKL_TKN_U32_MEM_PAGES:
	case SKL_TKN_U32_MAX_MCPS:
	case SKL_TKN_U32_OBS:
	case SKL_TKN_U32_IBS:
2458
		ret = skl_tplg_fill_res_tkn(dev, tkn_elem, res, pin_index, dir);
2459 2460 2461
		if (ret < 0)
			return ret;

2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475
		break;

	case SKL_TKN_U32_VBUS_ID:
		mconfig->vbus_id = tkn_elem->value;
		break;

	case SKL_TKN_U32_PARAMS_FIXUP:
		mconfig->params_fixup = tkn_elem->value;
		break;

	case SKL_TKN_U32_CONVERTER:
		mconfig->converter = tkn_elem->value;
		break;

2476
	case SKL_TKN_U32_D0I3_CAPS:
2477 2478 2479
		mconfig->d0i3_caps = tkn_elem->value;
		break;

2480 2481 2482 2483
	case SKL_TKN_U32_PIPE_ID:
		ret = skl_tplg_add_pipe(dev,
				mconfig, skl, tkn_elem);

2484 2485 2486 2487 2488
		if (ret < 0) {
			if (ret == -EEXIST) {
				is_pipe_exists = 1;
				break;
			}
2489
			return is_pipe_exists;
2490
		}
2491 2492 2493

		break;

2494 2495 2496 2497
	case SKL_TKN_U32_PIPE_CONFIG_ID:
		conf_idx = tkn_elem->value;
		break;

2498 2499 2500
	case SKL_TKN_U32_PIPE_CONN_TYPE:
	case SKL_TKN_U32_PIPE_PRIORITY:
	case SKL_TKN_U32_PIPE_MEM_PGS:
2501
	case SKL_TKN_U32_PMODE:
2502 2503
	case SKL_TKN_U32_PIPE_DIRECTION:
	case SKL_TKN_U32_NUM_CONFIGS:
2504 2505 2506 2507 2508 2509 2510 2511 2512
		if (is_pipe_exists) {
			ret = skl_tplg_fill_pipe_tkn(dev, mconfig->pipe,
					tkn_elem->token, tkn_elem->value);
			if (ret < 0)
				return ret;
		}

		break;

2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533
	case SKL_TKN_U32_PATH_MEM_PGS:
	case SKL_TKN_U32_CFG_FREQ:
	case SKL_TKN_U8_CFG_CHAN:
	case SKL_TKN_U8_CFG_BPS:
		if (mconfig->pipe->nr_cfgs) {
			ret = skl_tplg_fill_pipe_cfg(dev, mconfig->pipe,
					tkn_elem->token, tkn_elem->value,
					conf_idx, dir);
			if (ret < 0)
				return ret;
		}
		break;

	case SKL_TKN_CFG_MOD_RES_ID:
		mconfig->mod_cfg[conf_idx].res_idx = tkn_elem->value;
		break;

	case SKL_TKN_CFG_MOD_FMT_ID:
		mconfig->mod_cfg[conf_idx].fmt_idx = tkn_elem->value;
		break;

2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553
	/*
	 * SKL_TKN_U32_DIR_PIN_COUNT token has the value for both
	 * direction and the pin count. The first four bits represent
	 * direction and next four the pin count.
	 */
	case SKL_TKN_U32_DIR_PIN_COUNT:
		dir = tkn_elem->value & SKL_IN_DIR_BIT_MASK;
		pin_index = (tkn_elem->value &
			SKL_PIN_COUNT_MASK) >> 4;

		break;

	case SKL_TKN_U32_FMT_CH:
	case SKL_TKN_U32_FMT_FREQ:
	case SKL_TKN_U32_FMT_BIT_DEPTH:
	case SKL_TKN_U32_FMT_SAMPLE_SIZE:
	case SKL_TKN_U32_FMT_CH_CONFIG:
	case SKL_TKN_U32_FMT_INTERLEAVE:
	case SKL_TKN_U32_FMT_SAMPLE_TYPE:
	case SKL_TKN_U32_FMT_CH_MAP:
2554
		ret = skl_tplg_widget_fill_fmt(dev, iface, tkn_elem->token,
2555 2556 2557 2558 2559 2560 2561 2562 2563
				tkn_elem->value, dir, pin_index);

		if (ret < 0)
			return ret;

		break;

	case SKL_TKN_U32_PIN_MOD_ID:
	case SKL_TKN_U32_PIN_INST_ID:
2564
	case SKL_TKN_UUID:
2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578
		ret = skl_tplg_fill_pins_info(dev,
				mconfig, tkn_elem, dir,
				pin_index);
		if (ret < 0)
			return ret;

		break;

	case SKL_TKN_U32_CAPS_SIZE:
		mconfig->formats_config.caps_size =
			tkn_elem->value;

		break;

2579 2580 2581 2582 2583 2584 2585 2586 2587 2588
	case SKL_TKN_U32_CAPS_SET_PARAMS:
		mconfig->formats_config.set_params =
				tkn_elem->value;
		break;

	case SKL_TKN_U32_CAPS_PARAMS_ID:
		mconfig->formats_config.param_id =
				tkn_elem->value;
		break;

2589 2590 2591 2592 2593 2594
	case SKL_TKN_U32_PROC_DOMAIN:
		mconfig->domain =
			tkn_elem->value;

		break;

2595 2596 2597
	case SKL_TKN_U32_DMA_BUF_SIZE:
		mconfig->dma_buffer_size = tkn_elem->value;
		break;
2598 2599 2600 2601 2602 2603 2604 2605 2606 2607

	case SKL_TKN_U8_IN_PIN_TYPE:
	case SKL_TKN_U8_OUT_PIN_TYPE:
	case SKL_TKN_U8_CONN_TYPE:
		break;

	default:
		dev_err(dev, "Token %d not handled\n",
				tkn_elem->token);
		return -EINVAL;
2608
	}
2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619

	tkn_count++;

	return tkn_count;
}

/*
 * Parse the vendor array for specific tokens to construct
 * module private data
 */
static int skl_tplg_get_tokens(struct device *dev,
2620
		char *pvt_data,	struct skl_dev *skl,
2621 2622 2623 2624 2625 2626
		struct skl_module_cfg *mconfig, int block_size)
{
	struct snd_soc_tplg_vendor_array *array;
	struct snd_soc_tplg_vendor_value_elem *tkn_elem;
	int tkn_count = 0, ret;
	int off = 0, tuple_size = 0;
2627
	bool is_module_guid = true;
2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638

	if (block_size <= 0)
		return -EINVAL;

	while (tuple_size < block_size) {
		array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off);

		off += array->size;

		switch (array->type) {
		case SND_SOC_TPLG_TUPLE_TYPE_STRING:
2639
			dev_warn(dev, "no string tokens expected for skl tplg\n");
2640 2641 2642
			continue;

		case SND_SOC_TPLG_TUPLE_TYPE_UUID:
2643
			if (is_module_guid) {
2644
				ret = skl_tplg_get_uuid(dev, (guid_t *)mconfig->guid,
2645 2646 2647 2648 2649 2650 2651
							array->uuid);
				is_module_guid = false;
			} else {
				ret = skl_tplg_get_token(dev, array->value, skl,
							 mconfig);
			}

2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678
			if (ret < 0)
				return ret;

			tuple_size += sizeof(*array->uuid);

			continue;

		default:
			tkn_elem = array->value;
			tkn_count = 0;
			break;
		}

		while (tkn_count <= (array->num_elems - 1)) {
			ret = skl_tplg_get_token(dev, tkn_elem,
					skl, mconfig);

			if (ret < 0)
				return ret;

			tkn_count = tkn_count + ret;
			tkn_elem++;
		}

		tuple_size += tkn_count * sizeof(*tkn_elem);
	}

2679
	return off;
2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699
}

/*
 * Every data block is preceded by a descriptor to read the number
 * of data blocks, they type of the block and it's size
 */
static int skl_tplg_get_desc_blocks(struct device *dev,
		struct snd_soc_tplg_vendor_array *array)
{
	struct snd_soc_tplg_vendor_value_elem *tkn_elem;

	tkn_elem = array->value;

	switch (tkn_elem->token) {
	case SKL_TKN_U8_NUM_BLOCKS:
	case SKL_TKN_U8_BLOCK_TYPE:
	case SKL_TKN_U16_BLOCK_SIZE:
		return tkn_elem->value;

	default:
2700
		dev_err(dev, "Invalid descriptor token %d\n", tkn_elem->token);
2701 2702 2703 2704 2705 2706
		break;
	}

	return -EINVAL;
}

2707 2708 2709 2710 2711 2712 2713 2714 2715
/* Functions to parse private data from configuration file format v4 */

/*
 * Add pipeline from topology binary into driver pipeline list
 *
 * If already added we return that instance
 * Otherwise we create a new instance and add into driver list
 */
static int skl_tplg_add_pipe_v4(struct device *dev,
2716 2717
			struct skl_module_cfg *mconfig, struct skl_dev *skl,
			struct skl_dfw_v4_pipe *dfw_pipe)
2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792
{
	struct skl_pipeline *ppl;
	struct skl_pipe *pipe;
	struct skl_pipe_params *params;

	list_for_each_entry(ppl, &skl->ppl_list, node) {
		if (ppl->pipe->ppl_id == dfw_pipe->pipe_id) {
			mconfig->pipe = ppl->pipe;
			return 0;
		}
	}

	ppl = devm_kzalloc(dev, sizeof(*ppl), GFP_KERNEL);
	if (!ppl)
		return -ENOMEM;

	pipe = devm_kzalloc(dev, sizeof(*pipe), GFP_KERNEL);
	if (!pipe)
		return -ENOMEM;

	params = devm_kzalloc(dev, sizeof(*params), GFP_KERNEL);
	if (!params)
		return -ENOMEM;

	pipe->ppl_id = dfw_pipe->pipe_id;
	pipe->memory_pages = dfw_pipe->memory_pages;
	pipe->pipe_priority = dfw_pipe->pipe_priority;
	pipe->conn_type = dfw_pipe->conn_type;
	pipe->state = SKL_PIPE_INVALID;
	pipe->p_params = params;
	INIT_LIST_HEAD(&pipe->w_list);

	ppl->pipe = pipe;
	list_add(&ppl->node, &skl->ppl_list);

	mconfig->pipe = pipe;

	return 0;
}

static void skl_fill_module_pin_info_v4(struct skl_dfw_v4_module_pin *dfw_pin,
					struct skl_module_pin *m_pin,
					bool is_dynamic, int max_pin)
{
	int i;

	for (i = 0; i < max_pin; i++) {
		m_pin[i].id.module_id = dfw_pin[i].module_id;
		m_pin[i].id.instance_id = dfw_pin[i].instance_id;
		m_pin[i].in_use = false;
		m_pin[i].is_dynamic = is_dynamic;
		m_pin[i].pin_state = SKL_PIN_UNBIND;
	}
}

static void skl_tplg_fill_fmt_v4(struct skl_module_pin_fmt *dst_fmt,
				 struct skl_dfw_v4_module_fmt *src_fmt,
				 int pins)
{
	int i;

	for (i = 0; i < pins; i++) {
		dst_fmt[i].fmt.channels  = src_fmt[i].channels;
		dst_fmt[i].fmt.s_freq = src_fmt[i].freq;
		dst_fmt[i].fmt.bit_depth = src_fmt[i].bit_depth;
		dst_fmt[i].fmt.valid_bit_depth = src_fmt[i].valid_bit_depth;
		dst_fmt[i].fmt.ch_cfg = src_fmt[i].ch_cfg;
		dst_fmt[i].fmt.ch_map = src_fmt[i].ch_map;
		dst_fmt[i].fmt.interleaving_style =
						src_fmt[i].interleaving_style;
		dst_fmt[i].fmt.sample_type = src_fmt[i].sample_type;
	}
}

static int skl_tplg_get_pvt_data_v4(struct snd_soc_tplg_dapm_widget *tplg_w,
2793
				    struct skl_dev *skl, struct device *dev,
2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806
				    struct skl_module_cfg *mconfig)
{
	struct skl_dfw_v4_module *dfw =
				(struct skl_dfw_v4_module *)tplg_w->priv.data;
	int ret;

	dev_dbg(dev, "Parsing Skylake v4 widget topology data\n");

	ret = guid_parse(dfw->uuid, (guid_t *)mconfig->guid);
	if (ret)
		return ret;
	mconfig->id.module_id = -1;
	mconfig->id.instance_id = dfw->instance_id;
2807
	mconfig->module->resources[0].cpc = dfw->max_mcps / 1000;
2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833
	mconfig->module->resources[0].ibs = dfw->ibs;
	mconfig->module->resources[0].obs = dfw->obs;
	mconfig->core_id = dfw->core_id;
	mconfig->module->max_input_pins = dfw->max_in_queue;
	mconfig->module->max_output_pins = dfw->max_out_queue;
	mconfig->module->loadable = dfw->is_loadable;
	skl_tplg_fill_fmt_v4(mconfig->module->formats[0].inputs, dfw->in_fmt,
			     MAX_IN_QUEUE);
	skl_tplg_fill_fmt_v4(mconfig->module->formats[0].outputs, dfw->out_fmt,
			     MAX_OUT_QUEUE);

	mconfig->params_fixup = dfw->params_fixup;
	mconfig->converter = dfw->converter;
	mconfig->m_type = dfw->module_type;
	mconfig->vbus_id = dfw->vbus_id;
	mconfig->module->resources[0].is_pages = dfw->mem_pages;

	ret = skl_tplg_add_pipe_v4(dev, mconfig, skl, &dfw->pipe);
	if (ret)
		return ret;

	mconfig->dev_type = dfw->dev_type;
	mconfig->hw_conn_type = dfw->hw_conn_type;
	mconfig->time_slot = dfw->time_slot;
	mconfig->formats_config.caps_size = dfw->caps.caps_size;

2834 2835
	mconfig->m_in_pin = devm_kcalloc(dev,
				MAX_IN_QUEUE, sizeof(*mconfig->m_in_pin),
2836 2837 2838 2839
				GFP_KERNEL);
	if (!mconfig->m_in_pin)
		return -ENOMEM;

2840 2841
	mconfig->m_out_pin = devm_kcalloc(dev,
				MAX_OUT_QUEUE, sizeof(*mconfig->m_out_pin),
2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867
				GFP_KERNEL);
	if (!mconfig->m_out_pin)
		return -ENOMEM;

	skl_fill_module_pin_info_v4(dfw->in_pin, mconfig->m_in_pin,
				    dfw->is_dynamic_in_pin,
				    mconfig->module->max_input_pins);
	skl_fill_module_pin_info_v4(dfw->out_pin, mconfig->m_out_pin,
				    dfw->is_dynamic_out_pin,
				    mconfig->module->max_output_pins);

	if (mconfig->formats_config.caps_size) {
		mconfig->formats_config.set_params = dfw->caps.set_params;
		mconfig->formats_config.param_id = dfw->caps.param_id;
		mconfig->formats_config.caps =
		devm_kzalloc(dev, mconfig->formats_config.caps_size,
			     GFP_KERNEL);
		if (!mconfig->formats_config.caps)
			return -ENOMEM;
		memcpy(mconfig->formats_config.caps, dfw->caps.caps,
		       dfw->caps.caps_size);
	}

	return 0;
}

2868 2869 2870 2871 2872 2873 2874
/*
 * Parse the private data for the token and corresponding value.
 * The private data can have multiple data blocks. So, a data block
 * is preceded by a descriptor for number of blocks and a descriptor
 * for the type and size of the suceeding data block.
 */
static int skl_tplg_get_pvt_data(struct snd_soc_tplg_dapm_widget *tplg_w,
2875
				struct skl_dev *skl, struct device *dev,
2876 2877 2878 2879 2880 2881 2882
				struct skl_module_cfg *mconfig)
{
	struct snd_soc_tplg_vendor_array *array;
	int num_blocks, block_size = 0, block_type, off = 0;
	char *data;
	int ret;

2883 2884 2885 2886 2887 2888 2889
	/*
	 * v4 configuration files have a valid UUID at the start of
	 * the widget's private data.
	 */
	if (uuid_is_valid((char *)tplg_w->priv.data))
		return skl_tplg_get_pvt_data_v4(tplg_w, skl, dev, mconfig);

2890 2891 2892 2893 2894 2895 2896 2897 2898 2899
	/* Read the NUM_DATA_BLOCKS descriptor */
	array = (struct snd_soc_tplg_vendor_array *)tplg_w->priv.data;
	ret = skl_tplg_get_desc_blocks(dev, array);
	if (ret < 0)
		return ret;
	num_blocks = ret;

	off += array->size;
	/* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */
	while (num_blocks > 0) {
2900 2901 2902
		array = (struct snd_soc_tplg_vendor_array *)
				(tplg_w->priv.data + off);

2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937
		ret = skl_tplg_get_desc_blocks(dev, array);

		if (ret < 0)
			return ret;
		block_type = ret;
		off += array->size;

		array = (struct snd_soc_tplg_vendor_array *)
			(tplg_w->priv.data + off);

		ret = skl_tplg_get_desc_blocks(dev, array);

		if (ret < 0)
			return ret;
		block_size = ret;
		off += array->size;

		array = (struct snd_soc_tplg_vendor_array *)
			(tplg_w->priv.data + off);

		data = (tplg_w->priv.data + off);

		if (block_type == SKL_TYPE_TUPLE) {
			ret = skl_tplg_get_tokens(dev, data,
					skl, mconfig, block_size);

			if (ret < 0)
				return ret;

			--num_blocks;
		} else {
			if (mconfig->formats_config.caps_size > 0)
				memcpy(mconfig->formats_config.caps, data,
					mconfig->formats_config.caps_size);
			--num_blocks;
2938
			ret = mconfig->formats_config.caps_size;
2939
		}
2940
		off += ret;
2941 2942 2943
	}

	return 0;
2944 2945
}

2946
static void skl_clear_pin_config(struct snd_soc_component *component,
2947 2948 2949 2950 2951 2952
				struct snd_soc_dapm_widget *w)
{
	int i;
	struct skl_module_cfg *mconfig;
	struct skl_pipe *pipe;

2953 2954
	if (!strncmp(w->dapm->component->name, component->name,
					strlen(component->name))) {
2955 2956
		mconfig = w->priv;
		pipe = mconfig->pipe;
2957
		for (i = 0; i < mconfig->module->max_input_pins; i++) {
2958 2959 2960
			mconfig->m_in_pin[i].in_use = false;
			mconfig->m_in_pin[i].pin_state = SKL_PIN_UNBIND;
		}
2961
		for (i = 0; i < mconfig->module->max_output_pins; i++) {
2962 2963 2964 2965 2966 2967 2968 2969
			mconfig->m_out_pin[i].in_use = false;
			mconfig->m_out_pin[i].pin_state = SKL_PIN_UNBIND;
		}
		pipe->state = SKL_PIPE_INVALID;
		mconfig->m_state = SKL_MODULE_UNINIT;
	}
}

2970
void skl_cleanup_resources(struct skl_dev *skl)
2971
{
2972
	struct snd_soc_component *soc_component = skl->component;
2973 2974 2975
	struct snd_soc_dapm_widget *w;
	struct snd_soc_card *card;

2976
	if (soc_component == NULL)
2977 2978
		return;

2979
	card = soc_component->card;
2980 2981 2982 2983
	if (!card || !card->instantiated)
		return;

	list_for_each_entry(w, &card->widgets, list) {
2984
		if (is_skl_dsp_widget_type(w, skl->dev) && w->priv != NULL)
2985
			skl_clear_pin_config(soc_component, w);
2986 2987
	}

2988
	skl_clear_module_cnt(skl->dsp);
2989 2990
}

2991 2992 2993 2994 2995 2996 2997
/*
 * Topology core widget load callback
 *
 * This is used to save the private data for each widget which gives
 * information to the driver about module and pipeline parameters which DSP
 * FW expects like ids, resource values, formats etc
 */
2998
static int skl_tplg_widget_load(struct snd_soc_component *cmpnt, int index,
2999 3000
				struct snd_soc_dapm_widget *w,
				struct snd_soc_tplg_dapm_widget *tplg_w)
3001 3002
{
	int ret;
3003
	struct hdac_bus *bus = snd_soc_component_get_drvdata(cmpnt);
3004
	struct skl_dev *skl = bus_to_skl(bus);
3005 3006 3007 3008 3009 3010 3011 3012 3013 3014
	struct skl_module_cfg *mconfig;

	if (!tplg_w->priv.size)
		goto bind_event;

	mconfig = devm_kzalloc(bus->dev, sizeof(*mconfig), GFP_KERNEL);

	if (!mconfig)
		return -ENOMEM;

3015 3016 3017 3018 3019 3020 3021
	if (skl->nr_modules == 0) {
		mconfig->module = devm_kzalloc(bus->dev,
				sizeof(*mconfig->module), GFP_KERNEL);
		if (!mconfig->module)
			return -ENOMEM;
	}

3022
	w->priv = mconfig;
3023

3024 3025 3026 3027 3028
	/*
	 * module binary can be loaded later, so set it to query when
	 * module is load for a use case
	 */
	mconfig->id.module_id = -1;
3029

3030 3031 3032 3033
	/* Parse private data for tuples */
	ret = skl_tplg_get_pvt_data(tplg_w, skl, bus->dev, mconfig);
	if (ret < 0)
		return ret;
3034 3035 3036

	skl_debug_init_module(skl->debugfs, w, mconfig);

3037 3038
bind_event:
	if (tplg_w->event_type == 0) {
3039
		dev_dbg(bus->dev, "ASoC: No event handler required\n");
3040 3041 3042 3043
		return 0;
	}

	ret = snd_soc_tplg_widget_bind_event(w, skl_tplg_widget_ops,
3044 3045
					ARRAY_SIZE(skl_tplg_widget_ops),
					tplg_w->event_type);
3046 3047 3048 3049 3050 3051 3052 3053 3054 3055

	if (ret) {
		dev_err(bus->dev, "%s: No matching event handlers found for %d\n",
					__func__, tplg_w->event_type);
		return -EINVAL;
	}

	return 0;
}

3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070
static int skl_init_algo_data(struct device *dev, struct soc_bytes_ext *be,
					struct snd_soc_tplg_bytes_control *bc)
{
	struct skl_algo_data *ac;
	struct skl_dfw_algo_data *dfw_ac =
				(struct skl_dfw_algo_data *)bc->priv.data;

	ac = devm_kzalloc(dev, sizeof(*ac), GFP_KERNEL);
	if (!ac)
		return -ENOMEM;

	/* Fill private data */
	ac->max = dfw_ac->max;
	ac->param_id = dfw_ac->param_id;
	ac->set_params = dfw_ac->set_params;
3071
	ac->size = dfw_ac->max;
3072 3073

	if (ac->max) {
3074
		ac->params = devm_kzalloc(dev, ac->max, GFP_KERNEL);
3075 3076 3077
		if (!ac->params)
			return -ENOMEM;

3078
		memcpy(ac->params, dfw_ac->params, ac->max);
3079 3080 3081 3082 3083 3084
	}

	be->dobj.private  = ac;
	return 0;
}

3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102
static int skl_init_enum_data(struct device *dev, struct soc_enum *se,
				struct snd_soc_tplg_enum_control *ec)
{

	void *data;

	if (ec->priv.size) {
		data = devm_kzalloc(dev, sizeof(ec->priv.size), GFP_KERNEL);
		if (!data)
			return -ENOMEM;
		memcpy(data, ec->priv.data, ec->priv.size);
		se->dobj.private = data;
	}

	return 0;

}

3103
static int skl_tplg_control_load(struct snd_soc_component *cmpnt,
3104
				int index,
3105 3106 3107 3108 3109
				struct snd_kcontrol_new *kctl,
				struct snd_soc_tplg_ctl_hdr *hdr)
{
	struct soc_bytes_ext *sb;
	struct snd_soc_tplg_bytes_control *tplg_bc;
3110
	struct snd_soc_tplg_enum_control *tplg_ec;
3111
	struct hdac_bus *bus  = snd_soc_component_get_drvdata(cmpnt);
3112
	struct soc_enum *se;
3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125

	switch (hdr->ops.info) {
	case SND_SOC_TPLG_CTL_BYTES:
		tplg_bc = container_of(hdr,
				struct snd_soc_tplg_bytes_control, hdr);
		if (kctl->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
			sb = (struct soc_bytes_ext *)kctl->private_value;
			if (tplg_bc->priv.size)
				return skl_init_algo_data(
						bus->dev, sb, tplg_bc);
		}
		break;

3126 3127 3128
	case SND_SOC_TPLG_CTL_ENUM:
		tplg_ec = container_of(hdr,
				struct snd_soc_tplg_enum_control, hdr);
3129
		if (kctl->access & SNDRV_CTL_ELEM_ACCESS_READ) {
3130 3131
			se = (struct soc_enum *)kctl->private_value;
			if (tplg_ec->priv.size)
3132
				skl_init_enum_data(bus->dev, se, tplg_ec);
3133
		}
3134 3135 3136 3137 3138 3139 3140 3141 3142 3143

		/*
		 * now that the control initializations are done, remove
		 * write permission for the DMIC configuration enums to
		 * avoid conflicts between NHLT settings and user interaction
		 */

		if (hdr->ops.get == SKL_CONTROL_TYPE_MULTI_IO_SELECT_DMIC)
			kctl->access = SNDRV_CTL_ELEM_ACCESS_READ;

3144 3145
		break;

3146
	default:
3147
		dev_dbg(bus->dev, "Control load not supported %d:%d:%d\n",
3148 3149 3150 3151 3152 3153 3154
			hdr->ops.get, hdr->ops.put, hdr->ops.info);
		break;
	}

	return 0;
}

3155 3156
static int skl_tplg_fill_str_mfest_tkn(struct device *dev,
		struct snd_soc_tplg_vendor_string_elem *str_elem,
3157
		struct skl_dev *skl)
3158 3159 3160 3161 3162 3163
{
	int tkn_count = 0;
	static int ref_count;

	switch (str_elem->token) {
	case SKL_TKN_STR_LIB_NAME:
3164
		if (ref_count > skl->lib_count - 1) {
3165 3166 3167 3168
			ref_count = 0;
			return -EINVAL;
		}

3169
		strncpy(skl->lib_info[ref_count].name,
3170
			str_elem->string,
3171
			ARRAY_SIZE(skl->lib_info[ref_count].name));
3172 3173 3174 3175
		ref_count++;
		break;

	default:
3176
		dev_err(dev, "Not a string token %d\n", str_elem->token);
3177 3178
		break;
	}
3179
	tkn_count++;
3180 3181 3182 3183 3184 3185

	return tkn_count;
}

static int skl_tplg_get_str_tkn(struct device *dev,
		struct snd_soc_tplg_vendor_array *array,
3186
		struct skl_dev *skl)
3187 3188 3189 3190 3191 3192
{
	int tkn_count = 0, ret;
	struct snd_soc_tplg_vendor_string_elem *str_elem;

	str_elem = (struct snd_soc_tplg_vendor_string_elem *)array->value;
	while (tkn_count < array->num_elems) {
3193
		ret = skl_tplg_fill_str_mfest_tkn(dev, str_elem, skl);
3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204
		str_elem++;

		if (ret < 0)
			return ret;

		tkn_count = tkn_count + ret;
	}

	return tkn_count;
}

3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290
static int skl_tplg_manifest_fill_fmt(struct device *dev,
		struct skl_module_iface *fmt,
		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
		u32 dir, int fmt_idx)
{
	struct skl_module_pin_fmt *dst_fmt;
	struct skl_module_fmt *mod_fmt;
	int ret;

	if (!fmt)
		return -EINVAL;

	switch (dir) {
	case SKL_DIR_IN:
		dst_fmt = &fmt->inputs[fmt_idx];
		break;

	case SKL_DIR_OUT:
		dst_fmt = &fmt->outputs[fmt_idx];
		break;

	default:
		dev_err(dev, "Invalid direction: %d\n", dir);
		return -EINVAL;
	}

	mod_fmt = &dst_fmt->fmt;

	switch (tkn_elem->token) {
	case SKL_TKN_MM_U32_INTF_PIN_ID:
		dst_fmt->id = tkn_elem->value;
		break;

	default:
		ret = skl_tplg_fill_fmt(dev, mod_fmt, tkn_elem->token,
					tkn_elem->value);
		if (ret < 0)
			return ret;
		break;
	}

	return 0;
}

static int skl_tplg_fill_mod_info(struct device *dev,
		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
		struct skl_module *mod)
{

	if (!mod)
		return -EINVAL;

	switch (tkn_elem->token) {
	case SKL_TKN_U8_IN_PIN_TYPE:
		mod->input_pin_type = tkn_elem->value;
		break;

	case SKL_TKN_U8_OUT_PIN_TYPE:
		mod->output_pin_type = tkn_elem->value;
		break;

	case SKL_TKN_U8_IN_QUEUE_COUNT:
		mod->max_input_pins = tkn_elem->value;
		break;

	case SKL_TKN_U8_OUT_QUEUE_COUNT:
		mod->max_output_pins = tkn_elem->value;
		break;

	case SKL_TKN_MM_U8_NUM_RES:
		mod->nr_resources = tkn_elem->value;
		break;

	case SKL_TKN_MM_U8_NUM_INTF:
		mod->nr_interfaces = tkn_elem->value;
		break;

	default:
		dev_err(dev, "Invalid mod info token %d", tkn_elem->token);
		return -EINVAL;
	}

	return 0;
}


3291 3292
static int skl_tplg_get_int_tkn(struct device *dev,
		struct snd_soc_tplg_vendor_value_elem *tkn_elem,
3293
		struct skl_dev *skl)
3294
{
3295
	int tkn_count = 0, ret;
3296 3297 3298 3299
	static int mod_idx, res_val_idx, intf_val_idx, dir, pin_idx;
	struct skl_module_res *res = NULL;
	struct skl_module_iface *fmt = NULL;
	struct skl_module *mod = NULL;
3300 3301
	static struct skl_astate_param *astate_table;
	static int astate_cfg_idx, count;
3302
	int i;
3303
	size_t size;
3304 3305 3306 3307 3308 3309

	if (skl->modules) {
		mod = skl->modules[mod_idx];
		res = &mod->resources[res_val_idx];
		fmt = &mod->formats[intf_val_idx];
	}
3310 3311 3312

	switch (tkn_elem->token) {
	case SKL_TKN_U32_LIB_COUNT:
3313
		skl->lib_count = tkn_elem->value;
3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334
		break;

	case SKL_TKN_U8_NUM_MOD:
		skl->nr_modules = tkn_elem->value;
		skl->modules = devm_kcalloc(dev, skl->nr_modules,
				sizeof(*skl->modules), GFP_KERNEL);
		if (!skl->modules)
			return -ENOMEM;

		for (i = 0; i < skl->nr_modules; i++) {
			skl->modules[i] = devm_kzalloc(dev,
					sizeof(struct skl_module), GFP_KERNEL);
			if (!skl->modules[i])
				return -ENOMEM;
		}
		break;

	case SKL_TKN_MM_U8_MOD_IDX:
		mod_idx = tkn_elem->value;
		break;

3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346
	case SKL_TKN_U32_ASTATE_COUNT:
		if (astate_table != NULL) {
			dev_err(dev, "More than one entry for A-State count");
			return -EINVAL;
		}

		if (tkn_elem->value > SKL_MAX_ASTATE_CFG) {
			dev_err(dev, "Invalid A-State count %d\n",
				tkn_elem->value);
			return -EINVAL;
		}

3347 3348
		size = struct_size(skl->cfg.astate_cfg, astate_table,
				   tkn_elem->value);
3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374
		skl->cfg.astate_cfg = devm_kzalloc(dev, size, GFP_KERNEL);
		if (!skl->cfg.astate_cfg)
			return -ENOMEM;

		astate_table = skl->cfg.astate_cfg->astate_table;
		count = skl->cfg.astate_cfg->count = tkn_elem->value;
		break;

	case SKL_TKN_U32_ASTATE_IDX:
		if (tkn_elem->value >= count) {
			dev_err(dev, "Invalid A-State index %d\n",
				tkn_elem->value);
			return -EINVAL;
		}

		astate_cfg_idx = tkn_elem->value;
		break;

	case SKL_TKN_U32_ASTATE_KCPS:
		astate_table[astate_cfg_idx].kcps = tkn_elem->value;
		break;

	case SKL_TKN_U32_ASTATE_CLK_SRC:
		astate_table[astate_cfg_idx].clk_src = tkn_elem->value;
		break;

3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447
	case SKL_TKN_U8_IN_PIN_TYPE:
	case SKL_TKN_U8_OUT_PIN_TYPE:
	case SKL_TKN_U8_IN_QUEUE_COUNT:
	case SKL_TKN_U8_OUT_QUEUE_COUNT:
	case SKL_TKN_MM_U8_NUM_RES:
	case SKL_TKN_MM_U8_NUM_INTF:
		ret = skl_tplg_fill_mod_info(dev, tkn_elem, mod);
		if (ret < 0)
			return ret;
		break;

	case SKL_TKN_U32_DIR_PIN_COUNT:
		dir = tkn_elem->value & SKL_IN_DIR_BIT_MASK;
		pin_idx = (tkn_elem->value & SKL_PIN_COUNT_MASK) >> 4;
		break;

	case SKL_TKN_MM_U32_RES_ID:
		if (!res)
			return -EINVAL;

		res->id = tkn_elem->value;
		res_val_idx = tkn_elem->value;
		break;

	case SKL_TKN_MM_U32_FMT_ID:
		if (!fmt)
			return -EINVAL;

		fmt->fmt_idx = tkn_elem->value;
		intf_val_idx = tkn_elem->value;
		break;

	case SKL_TKN_MM_U32_CPS:
	case SKL_TKN_MM_U32_DMA_SIZE:
	case SKL_TKN_MM_U32_CPC:
	case SKL_TKN_U32_MEM_PAGES:
	case SKL_TKN_U32_OBS:
	case SKL_TKN_U32_IBS:
	case SKL_TKN_MM_U32_RES_PIN_ID:
	case SKL_TKN_MM_U32_PIN_BUF:
		ret = skl_tplg_fill_res_tkn(dev, tkn_elem, res, pin_idx, dir);
		if (ret < 0)
			return ret;

		break;

	case SKL_TKN_MM_U32_NUM_IN_FMT:
		if (!fmt)
			return -EINVAL;

		res->nr_input_pins = tkn_elem->value;
		break;

	case SKL_TKN_MM_U32_NUM_OUT_FMT:
		if (!fmt)
			return -EINVAL;

		res->nr_output_pins = tkn_elem->value;
		break;

	case SKL_TKN_U32_FMT_CH:
	case SKL_TKN_U32_FMT_FREQ:
	case SKL_TKN_U32_FMT_BIT_DEPTH:
	case SKL_TKN_U32_FMT_SAMPLE_SIZE:
	case SKL_TKN_U32_FMT_CH_CONFIG:
	case SKL_TKN_U32_FMT_INTERLEAVE:
	case SKL_TKN_U32_FMT_SAMPLE_TYPE:
	case SKL_TKN_U32_FMT_CH_MAP:
	case SKL_TKN_MM_U32_INTF_PIN_ID:
		ret = skl_tplg_manifest_fill_fmt(dev, fmt, tkn_elem,
						 dir, pin_idx);
		if (ret < 0)
			return ret;
3448 3449 3450
		break;

	default:
3451
		dev_err(dev, "Not a manifest token %d\n", tkn_elem->token);
3452 3453
		return -EINVAL;
	}
3454
	tkn_count++;
3455 3456 3457 3458 3459 3460 3461 3462 3463

	return tkn_count;
}

/*
 * Fill the manifest structure by parsing the tokens based on the
 * type.
 */
static int skl_tplg_get_manifest_tkn(struct device *dev,
3464
		char *pvt_data, struct skl_dev *skl,
3465 3466 3467 3468
		int block_size)
{
	int tkn_count = 0, ret;
	int off = 0, tuple_size = 0;
3469
	u8 uuid_index = 0;
3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480
	struct snd_soc_tplg_vendor_array *array;
	struct snd_soc_tplg_vendor_value_elem *tkn_elem;

	if (block_size <= 0)
		return -EINVAL;

	while (tuple_size < block_size) {
		array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off);
		off += array->size;
		switch (array->type) {
		case SND_SOC_TPLG_TUPLE_TYPE_STRING:
3481
			ret = skl_tplg_get_str_tkn(dev, array, skl);
3482 3483 3484

			if (ret < 0)
				return ret;
3485
			tkn_count = ret;
3486 3487 3488 3489 3490 3491

			tuple_size += tkn_count *
				sizeof(struct snd_soc_tplg_vendor_string_elem);
			continue;

		case SND_SOC_TPLG_TUPLE_TYPE_UUID:
3492 3493 3494 3495 3496 3497 3498 3499 3500
			if (array->uuid->token != SKL_TKN_UUID) {
				dev_err(dev, "Not an UUID token: %d\n",
					array->uuid->token);
				return -EINVAL;
			}
			if (uuid_index >= skl->nr_modules) {
				dev_err(dev, "Too many UUID tokens\n");
				return -EINVAL;
			}
3501 3502
			import_guid(&skl->modules[uuid_index++]->uuid,
				    array->uuid->uuid);
3503 3504

			tuple_size += sizeof(*array->uuid);
3505 3506 3507 3508 3509 3510 3511 3512 3513 3514
			continue;

		default:
			tkn_elem = array->value;
			tkn_count = 0;
			break;
		}

		while (tkn_count <= array->num_elems - 1) {
			ret = skl_tplg_get_int_tkn(dev,
3515
					tkn_elem, skl);
3516 3517 3518 3519 3520 3521
			if (ret < 0)
				return ret;

			tkn_count = tkn_count + ret;
			tkn_elem++;
		}
3522
		tuple_size += (tkn_count * sizeof(*tkn_elem));
3523 3524 3525
		tkn_count = 0;
	}

3526
	return off;
3527 3528 3529 3530 3531 3532 3533
}

/*
 * Parse manifest private data for tokens. The private data block is
 * preceded by descriptors for type and size of data block.
 */
static int skl_tplg_get_manifest_data(struct snd_soc_tplg_manifest *manifest,
3534
			struct device *dev, struct skl_dev *skl)
3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550
{
	struct snd_soc_tplg_vendor_array *array;
	int num_blocks, block_size = 0, block_type, off = 0;
	char *data;
	int ret;

	/* Read the NUM_DATA_BLOCKS descriptor */
	array = (struct snd_soc_tplg_vendor_array *)manifest->priv.data;
	ret = skl_tplg_get_desc_blocks(dev, array);
	if (ret < 0)
		return ret;
	num_blocks = ret;

	off += array->size;
	/* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */
	while (num_blocks > 0) {
3551 3552
		array = (struct snd_soc_tplg_vendor_array *)
				(manifest->priv.data + off);
3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575
		ret = skl_tplg_get_desc_blocks(dev, array);

		if (ret < 0)
			return ret;
		block_type = ret;
		off += array->size;

		array = (struct snd_soc_tplg_vendor_array *)
			(manifest->priv.data + off);

		ret = skl_tplg_get_desc_blocks(dev, array);

		if (ret < 0)
			return ret;
		block_size = ret;
		off += array->size;

		array = (struct snd_soc_tplg_vendor_array *)
			(manifest->priv.data + off);

		data = (manifest->priv.data + off);

		if (block_type == SKL_TYPE_TUPLE) {
3576
			ret = skl_tplg_get_manifest_tkn(dev, data, skl,
3577 3578 3579 3580 3581 3582 3583 3584 3585
					block_size);

			if (ret < 0)
				return ret;

			--num_blocks;
		} else {
			return -EINVAL;
		}
3586
		off += ret;
3587 3588 3589 3590 3591
	}

	return 0;
}

3592
static int skl_manifest_load(struct snd_soc_component *cmpnt, int index,
3593 3594
				struct snd_soc_tplg_manifest *manifest)
{
3595
	struct hdac_bus *bus = snd_soc_component_get_drvdata(cmpnt);
3596
	struct skl_dev *skl = bus_to_skl(bus);
3597

3598 3599 3600 3601
	/* proceed only if we have private data defined */
	if (manifest->priv.size == 0)
		return 0;

3602
	skl_tplg_get_manifest_data(manifest, bus->dev, skl);
3603

3604
	if (skl->lib_count > SKL_MAX_LIB) {
3605
		dev_err(bus->dev, "Exceeding max Library count. Got:%d\n",
3606
					skl->lib_count);
3607
		return  -EINVAL;
3608 3609
	}

3610
	return 0;
3611 3612
}

3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643
static void skl_tplg_complete(struct snd_soc_component *component)
{
	struct snd_soc_dobj *dobj;
	struct snd_soc_acpi_mach *mach =
		dev_get_platdata(component->card->dev);
	int i;

	list_for_each_entry(dobj, &component->dobj_list, list) {
		struct snd_kcontrol *kcontrol = dobj->control.kcontrol;
		struct soc_enum *se =
			(struct soc_enum *)kcontrol->private_value;
		char **texts = dobj->control.dtexts;
		char chan_text[4];

		if (dobj->type != SND_SOC_DOBJ_ENUM ||
		    dobj->control.kcontrol->put !=
		    skl_tplg_multi_config_set_dmic)
			continue;
		sprintf(chan_text, "c%d", mach->mach_params.dmic_num);

		for (i = 0; i < se->items; i++) {
			struct snd_ctl_elem_value val;

			if (strstr(texts[i], chan_text)) {
				val.value.enumerated.item[0] = i;
				kcontrol->put(kcontrol, &val);
			}
		}
	}
}

3644 3645
static struct snd_soc_tplg_ops skl_tplg_ops  = {
	.widget_load = skl_tplg_widget_load,
3646 3647 3648
	.control_load = skl_tplg_control_load,
	.bytes_ext_ops = skl_tlv_ops,
	.bytes_ext_ops_count = ARRAY_SIZE(skl_tlv_ops),
3649 3650
	.io_ops = skl_tplg_kcontrol_ops,
	.io_ops_count = ARRAY_SIZE(skl_tplg_kcontrol_ops),
3651
	.manifest = skl_manifest_load,
3652
	.dai_load = skl_dai_load,
3653
	.complete = skl_tplg_complete,
3654 3655
};

3656 3657 3658 3659 3660 3661
/*
 * A pipe can have multiple modules, each of them will be a DAPM widget as
 * well. While managing a pipeline we need to get the list of all the
 * widgets in a pipelines, so this helper - skl_tplg_create_pipe_widget_list()
 * helps to get the SKL type widgets in that pipeline
 */
3662
static int skl_tplg_create_pipe_widget_list(struct snd_soc_component *component)
3663 3664 3665 3666 3667 3668
{
	struct snd_soc_dapm_widget *w;
	struct skl_module_cfg *mcfg = NULL;
	struct skl_pipe_module *p_module = NULL;
	struct skl_pipe *pipe;

3669
	list_for_each_entry(w, &component->card->widgets, list) {
3670
		if (is_skl_dsp_widget_type(w, component->dev) && w->priv) {
3671 3672 3673
			mcfg = w->priv;
			pipe = mcfg->pipe;

3674
			p_module = devm_kzalloc(component->dev,
3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686
						sizeof(*p_module), GFP_KERNEL);
			if (!p_module)
				return -ENOMEM;

			p_module->w = w;
			list_add_tail(&p_module->node, &pipe->w_list);
		}
	}

	return 0;
}

3687
static void skl_tplg_set_pipe_type(struct skl_dev *skl, struct skl_pipe *pipe)
3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709
{
	struct skl_pipe_module *w_module;
	struct snd_soc_dapm_widget *w;
	struct skl_module_cfg *mconfig;
	bool host_found = false, link_found = false;

	list_for_each_entry(w_module, &pipe->w_list, node) {
		w = w_module->w;
		mconfig = w->priv;

		if (mconfig->dev_type == SKL_DEVICE_HDAHOST)
			host_found = true;
		else if (mconfig->dev_type != SKL_DEVICE_NONE)
			link_found = true;
	}

	if (host_found && link_found)
		pipe->passthru = true;
	else
		pipe->passthru = false;
}

3710 3711 3712
/*
 * SKL topology init routine
 */
3713
int skl_tplg_init(struct snd_soc_component *component, struct hdac_bus *bus)
3714 3715 3716
{
	int ret;
	const struct firmware *fw;
3717
	struct skl_dev *skl = bus_to_skl(bus);
3718
	struct skl_pipeline *ppl;
3719

3720
	ret = request_firmware(&fw, skl->tplg_name, bus->dev);
3721
	if (ret < 0) {
3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735
		char alt_tplg_name[64];

		snprintf(alt_tplg_name, sizeof(alt_tplg_name), "%s-tplg.bin",
			 skl->mach->drv_name);
		dev_info(bus->dev, "tplg fw %s load failed with %d, trying alternative tplg name %s",
			 skl->tplg_name, ret, alt_tplg_name);

		ret = request_firmware(&fw, alt_tplg_name, bus->dev);
		if (!ret)
			goto component_load;

		dev_info(bus->dev, "tplg %s failed with %d, falling back to dfw_sst.bin",
			 alt_tplg_name, ret);

3736 3737 3738 3739 3740 3741
		ret = request_firmware(&fw, "dfw_sst.bin", bus->dev);
		if (ret < 0) {
			dev_err(bus->dev, "Fallback tplg fw %s load failed with %d\n",
					"dfw_sst.bin", ret);
			return ret;
		}
3742 3743
	}

3744 3745
component_load:

3746 3747 3748 3749
	/*
	 * The complete tplg for SKL is loaded as index 0, we don't use
	 * any other index
	 */
3750
	ret = snd_soc_tplg_component_load(component, &skl_tplg_ops, fw, 0);
3751 3752
	if (ret < 0) {
		dev_err(bus->dev, "tplg component load failed%d\n", ret);
3753
		goto err;
3754 3755
	}

3756
	ret = skl_tplg_create_pipe_widget_list(component);
3757 3758 3759 3760 3761
	if (ret < 0) {
		dev_err(bus->dev, "tplg create pipe widget list failed%d\n",
				ret);
		goto err;
	}
3762

3763 3764
	list_for_each_entry(ppl, &skl->ppl_list, node)
		skl_tplg_set_pipe_type(skl, ppl->pipe);
3765

3766 3767 3768
err:
	release_firmware(fw);
	return ret;
3769
}
3770 3771 3772

void skl_tplg_exit(struct snd_soc_component *component, struct hdac_bus *bus)
{
3773
	struct skl_dev *skl = bus_to_skl(bus);
3774 3775 3776 3777 3778 3779 3780 3781 3782
	struct skl_pipeline *ppl, *tmp;

	if (!list_empty(&skl->ppl_list))
		list_for_each_entry_safe(ppl, tmp, &skl->ppl_list, node)
			list_del(&ppl->node);

	/* clean up topology */
	snd_soc_tplg_component_remove(component, SND_SOC_TPLG_INDEX_ALL);
}