1e4e2d2f4SJeeja KP /* 2e4e2d2f4SJeeja KP * skl-topology.c - Implements Platform component ALSA controls/widget 3e4e2d2f4SJeeja KP * handlers. 4e4e2d2f4SJeeja KP * 5e4e2d2f4SJeeja KP * Copyright (C) 2014-2015 Intel Corp 6e4e2d2f4SJeeja KP * Author: Jeeja KP <jeeja.kp@intel.com> 7e4e2d2f4SJeeja KP * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 8e4e2d2f4SJeeja KP * 9e4e2d2f4SJeeja KP * This program is free software; you can redistribute it and/or modify 10e4e2d2f4SJeeja KP * it under the terms of the GNU General Public License as version 2, as 11e4e2d2f4SJeeja KP * published by the Free Software Foundation. 12e4e2d2f4SJeeja KP * 13e4e2d2f4SJeeja KP * This program is distributed in the hope that it will be useful, but 14e4e2d2f4SJeeja KP * WITHOUT ANY WARRANTY; without even the implied warranty of 15e4e2d2f4SJeeja KP * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16e4e2d2f4SJeeja KP * General Public License for more details. 17e4e2d2f4SJeeja KP */ 18e4e2d2f4SJeeja KP 19e4e2d2f4SJeeja KP #include <linux/slab.h> 20e4e2d2f4SJeeja KP #include <linux/types.h> 21e4e2d2f4SJeeja KP #include <linux/firmware.h> 22e4e2d2f4SJeeja KP #include <sound/soc.h> 23e4e2d2f4SJeeja KP #include <sound/soc-topology.h> 246277e832SShreyas NC #include <uapi/sound/snd_sst_tokens.h> 25e4e2d2f4SJeeja KP #include "skl-sst-dsp.h" 26e4e2d2f4SJeeja KP #include "skl-sst-ipc.h" 27e4e2d2f4SJeeja KP #include "skl-topology.h" 28e4e2d2f4SJeeja KP #include "skl.h" 29e4e2d2f4SJeeja KP #include "skl-tplg-interface.h" 306c5768b3SDharageswari R #include "../common/sst-dsp.h" 316c5768b3SDharageswari R #include "../common/sst-dsp-priv.h" 32e4e2d2f4SJeeja KP 33f7590d4fSJeeja KP #define SKL_CH_FIXUP_MASK (1 << 0) 34f7590d4fSJeeja KP #define SKL_RATE_FIXUP_MASK (1 << 1) 35f7590d4fSJeeja KP #define SKL_FMT_FIXUP_MASK (1 << 2) 366277e832SShreyas NC #define SKL_IN_DIR_BIT_MASK BIT(0) 376277e832SShreyas NC #define SKL_PIN_COUNT_MASK GENMASK(7, 4) 38f7590d4fSJeeja KP 39a83e3b4cSVinod Koul void skl_tplg_d0i3_get(struct skl *skl, enum d0i3_capability caps) 40a83e3b4cSVinod Koul { 41a83e3b4cSVinod Koul struct skl_d0i3_data *d0i3 = &skl->skl_sst->d0i3; 42a83e3b4cSVinod Koul 43a83e3b4cSVinod Koul switch (caps) { 44a83e3b4cSVinod Koul case SKL_D0I3_NONE: 45a83e3b4cSVinod Koul d0i3->non_d0i3++; 46a83e3b4cSVinod Koul break; 47a83e3b4cSVinod Koul 48a83e3b4cSVinod Koul case SKL_D0I3_STREAMING: 49a83e3b4cSVinod Koul d0i3->streaming++; 50a83e3b4cSVinod Koul break; 51a83e3b4cSVinod Koul 52a83e3b4cSVinod Koul case SKL_D0I3_NON_STREAMING: 53a83e3b4cSVinod Koul d0i3->non_streaming++; 54a83e3b4cSVinod Koul break; 55a83e3b4cSVinod Koul } 56a83e3b4cSVinod Koul } 57a83e3b4cSVinod Koul 58a83e3b4cSVinod Koul void skl_tplg_d0i3_put(struct skl *skl, enum d0i3_capability caps) 59a83e3b4cSVinod Koul { 60a83e3b4cSVinod Koul struct skl_d0i3_data *d0i3 = &skl->skl_sst->d0i3; 61a83e3b4cSVinod Koul 62a83e3b4cSVinod Koul switch (caps) { 63a83e3b4cSVinod Koul case SKL_D0I3_NONE: 64a83e3b4cSVinod Koul d0i3->non_d0i3--; 65a83e3b4cSVinod Koul break; 66a83e3b4cSVinod Koul 67a83e3b4cSVinod Koul case SKL_D0I3_STREAMING: 68a83e3b4cSVinod Koul d0i3->streaming--; 69a83e3b4cSVinod Koul break; 70a83e3b4cSVinod Koul 71a83e3b4cSVinod Koul case SKL_D0I3_NON_STREAMING: 72a83e3b4cSVinod Koul d0i3->non_streaming--; 73a83e3b4cSVinod Koul break; 74a83e3b4cSVinod Koul } 75a83e3b4cSVinod Koul } 76a83e3b4cSVinod Koul 77e4e2d2f4SJeeja KP /* 78e4e2d2f4SJeeja KP * SKL DSP driver modelling uses only few DAPM widgets so for rest we will 79e4e2d2f4SJeeja KP * ignore. This helpers checks if the SKL driver handles this widget type 80e4e2d2f4SJeeja KP */ 81e4e2d2f4SJeeja KP static int is_skl_dsp_widget_type(struct snd_soc_dapm_widget *w) 82e4e2d2f4SJeeja KP { 83e4e2d2f4SJeeja KP switch (w->id) { 84e4e2d2f4SJeeja KP case snd_soc_dapm_dai_link: 85e4e2d2f4SJeeja KP case snd_soc_dapm_dai_in: 86e4e2d2f4SJeeja KP case snd_soc_dapm_aif_in: 87e4e2d2f4SJeeja KP case snd_soc_dapm_aif_out: 88e4e2d2f4SJeeja KP case snd_soc_dapm_dai_out: 89e4e2d2f4SJeeja KP case snd_soc_dapm_switch: 90e4e2d2f4SJeeja KP return false; 91e4e2d2f4SJeeja KP default: 92e4e2d2f4SJeeja KP return true; 93e4e2d2f4SJeeja KP } 94e4e2d2f4SJeeja KP } 95e4e2d2f4SJeeja KP 96e4e2d2f4SJeeja KP /* 97e4e2d2f4SJeeja KP * Each pipelines needs memory to be allocated. Check if we have free memory 989ba8ffefSDharageswari.R * from available pool. 99e4e2d2f4SJeeja KP */ 1009ba8ffefSDharageswari.R static bool skl_is_pipe_mem_avail(struct skl *skl, 101e4e2d2f4SJeeja KP struct skl_module_cfg *mconfig) 102e4e2d2f4SJeeja KP { 103e4e2d2f4SJeeja KP struct skl_sst *ctx = skl->skl_sst; 104e4e2d2f4SJeeja KP 105e4e2d2f4SJeeja KP if (skl->resource.mem + mconfig->pipe->memory_pages > 106e4e2d2f4SJeeja KP skl->resource.max_mem) { 107e4e2d2f4SJeeja KP dev_err(ctx->dev, 108e4e2d2f4SJeeja KP "%s: module_id %d instance %d\n", __func__, 109e4e2d2f4SJeeja KP mconfig->id.module_id, 110e4e2d2f4SJeeja KP mconfig->id.instance_id); 111e4e2d2f4SJeeja KP dev_err(ctx->dev, 112e4e2d2f4SJeeja KP "exceeds ppl memory available %d mem %d\n", 113e4e2d2f4SJeeja KP skl->resource.max_mem, skl->resource.mem); 114e4e2d2f4SJeeja KP return false; 1159ba8ffefSDharageswari.R } else { 1169ba8ffefSDharageswari.R return true; 1179ba8ffefSDharageswari.R } 118e4e2d2f4SJeeja KP } 119e4e2d2f4SJeeja KP 1209ba8ffefSDharageswari.R /* 1219ba8ffefSDharageswari.R * Add the mem to the mem pool. This is freed when pipe is deleted. 1229ba8ffefSDharageswari.R * Note: DSP does actual memory management we only keep track for complete 1239ba8ffefSDharageswari.R * pool 1249ba8ffefSDharageswari.R */ 1259ba8ffefSDharageswari.R static void skl_tplg_alloc_pipe_mem(struct skl *skl, 1269ba8ffefSDharageswari.R struct skl_module_cfg *mconfig) 1279ba8ffefSDharageswari.R { 128e4e2d2f4SJeeja KP skl->resource.mem += mconfig->pipe->memory_pages; 129e4e2d2f4SJeeja KP } 130e4e2d2f4SJeeja KP 131e4e2d2f4SJeeja KP /* 132e4e2d2f4SJeeja KP * Pipeline needs needs DSP CPU resources for computation, this is 133e4e2d2f4SJeeja KP * quantified in MCPS (Million Clocks Per Second) required for module/pipe 134e4e2d2f4SJeeja KP * 135e4e2d2f4SJeeja KP * Each pipelines needs mcps to be allocated. Check if we have mcps for this 1369ba8ffefSDharageswari.R * pipe. 137e4e2d2f4SJeeja KP */ 1389ba8ffefSDharageswari.R 1399ba8ffefSDharageswari.R static bool skl_is_pipe_mcps_avail(struct skl *skl, 140e4e2d2f4SJeeja KP struct skl_module_cfg *mconfig) 141e4e2d2f4SJeeja KP { 142e4e2d2f4SJeeja KP struct skl_sst *ctx = skl->skl_sst; 143e4e2d2f4SJeeja KP 144e4e2d2f4SJeeja KP if (skl->resource.mcps + mconfig->mcps > skl->resource.max_mcps) { 145e4e2d2f4SJeeja KP dev_err(ctx->dev, 146e4e2d2f4SJeeja KP "%s: module_id %d instance %d\n", __func__, 147e4e2d2f4SJeeja KP mconfig->id.module_id, mconfig->id.instance_id); 148e4e2d2f4SJeeja KP dev_err(ctx->dev, 1497ca42f5aSGuneshwor Singh "exceeds ppl mcps available %d > mem %d\n", 150e4e2d2f4SJeeja KP skl->resource.max_mcps, skl->resource.mcps); 151e4e2d2f4SJeeja KP return false; 1529ba8ffefSDharageswari.R } else { 1539ba8ffefSDharageswari.R return true; 1549ba8ffefSDharageswari.R } 155e4e2d2f4SJeeja KP } 156e4e2d2f4SJeeja KP 1579ba8ffefSDharageswari.R static void skl_tplg_alloc_pipe_mcps(struct skl *skl, 1589ba8ffefSDharageswari.R struct skl_module_cfg *mconfig) 1599ba8ffefSDharageswari.R { 160e4e2d2f4SJeeja KP skl->resource.mcps += mconfig->mcps; 161e4e2d2f4SJeeja KP } 162e4e2d2f4SJeeja KP 163e4e2d2f4SJeeja KP /* 164e4e2d2f4SJeeja KP * Free the mcps when tearing down 165e4e2d2f4SJeeja KP */ 166e4e2d2f4SJeeja KP static void 167e4e2d2f4SJeeja KP skl_tplg_free_pipe_mcps(struct skl *skl, struct skl_module_cfg *mconfig) 168e4e2d2f4SJeeja KP { 169e4e2d2f4SJeeja KP skl->resource.mcps -= mconfig->mcps; 170e4e2d2f4SJeeja KP } 171e4e2d2f4SJeeja KP 172e4e2d2f4SJeeja KP /* 173e4e2d2f4SJeeja KP * Free the memory when tearing down 174e4e2d2f4SJeeja KP */ 175e4e2d2f4SJeeja KP static void 176e4e2d2f4SJeeja KP skl_tplg_free_pipe_mem(struct skl *skl, struct skl_module_cfg *mconfig) 177e4e2d2f4SJeeja KP { 178e4e2d2f4SJeeja KP skl->resource.mem -= mconfig->pipe->memory_pages; 179e4e2d2f4SJeeja KP } 180e4e2d2f4SJeeja KP 181f7590d4fSJeeja KP 182f7590d4fSJeeja KP static void skl_dump_mconfig(struct skl_sst *ctx, 183f7590d4fSJeeja KP struct skl_module_cfg *mcfg) 184f7590d4fSJeeja KP { 185f7590d4fSJeeja KP dev_dbg(ctx->dev, "Dumping config\n"); 186f7590d4fSJeeja KP dev_dbg(ctx->dev, "Input Format:\n"); 1874cd9899fSHardik T Shah dev_dbg(ctx->dev, "channels = %d\n", mcfg->in_fmt[0].channels); 1884cd9899fSHardik T Shah dev_dbg(ctx->dev, "s_freq = %d\n", mcfg->in_fmt[0].s_freq); 1894cd9899fSHardik T Shah dev_dbg(ctx->dev, "ch_cfg = %d\n", mcfg->in_fmt[0].ch_cfg); 1904cd9899fSHardik T Shah dev_dbg(ctx->dev, "valid bit depth = %d\n", mcfg->in_fmt[0].valid_bit_depth); 191f7590d4fSJeeja KP dev_dbg(ctx->dev, "Output Format:\n"); 1924cd9899fSHardik T Shah dev_dbg(ctx->dev, "channels = %d\n", mcfg->out_fmt[0].channels); 1934cd9899fSHardik T Shah dev_dbg(ctx->dev, "s_freq = %d\n", mcfg->out_fmt[0].s_freq); 1944cd9899fSHardik T Shah dev_dbg(ctx->dev, "valid bit depth = %d\n", mcfg->out_fmt[0].valid_bit_depth); 1954cd9899fSHardik T Shah dev_dbg(ctx->dev, "ch_cfg = %d\n", mcfg->out_fmt[0].ch_cfg); 196f7590d4fSJeeja KP } 197f7590d4fSJeeja KP 198ea5a137dSSubhransu S. Prusty static void skl_tplg_update_chmap(struct skl_module_fmt *fmt, int chs) 199ea5a137dSSubhransu S. Prusty { 200ea5a137dSSubhransu S. Prusty int slot_map = 0xFFFFFFFF; 201ea5a137dSSubhransu S. Prusty int start_slot = 0; 202ea5a137dSSubhransu S. Prusty int i; 203ea5a137dSSubhransu S. Prusty 204ea5a137dSSubhransu S. Prusty for (i = 0; i < chs; i++) { 205ea5a137dSSubhransu S. Prusty /* 206ea5a137dSSubhransu S. Prusty * For 2 channels with starting slot as 0, slot map will 207ea5a137dSSubhransu S. Prusty * look like 0xFFFFFF10. 208ea5a137dSSubhransu S. Prusty */ 209ea5a137dSSubhransu S. Prusty slot_map &= (~(0xF << (4 * i)) | (start_slot << (4 * i))); 210ea5a137dSSubhransu S. Prusty start_slot++; 211ea5a137dSSubhransu S. Prusty } 212ea5a137dSSubhransu S. Prusty fmt->ch_map = slot_map; 213ea5a137dSSubhransu S. Prusty } 214ea5a137dSSubhransu S. Prusty 215f7590d4fSJeeja KP static void skl_tplg_update_params(struct skl_module_fmt *fmt, 216f7590d4fSJeeja KP struct skl_pipe_params *params, int fixup) 217f7590d4fSJeeja KP { 218f7590d4fSJeeja KP if (fixup & SKL_RATE_FIXUP_MASK) 219f7590d4fSJeeja KP fmt->s_freq = params->s_freq; 220ea5a137dSSubhransu S. Prusty if (fixup & SKL_CH_FIXUP_MASK) { 221f7590d4fSJeeja KP fmt->channels = params->ch; 222ea5a137dSSubhransu S. Prusty skl_tplg_update_chmap(fmt, fmt->channels); 223ea5a137dSSubhransu S. Prusty } 22498256f83SJeeja KP if (fixup & SKL_FMT_FIXUP_MASK) { 22598256f83SJeeja KP fmt->valid_bit_depth = skl_get_bit_depth(params->s_fmt); 22698256f83SJeeja KP 22798256f83SJeeja KP /* 22898256f83SJeeja KP * 16 bit is 16 bit container whereas 24 bit is in 32 bit 22998256f83SJeeja KP * container so update bit depth accordingly 23098256f83SJeeja KP */ 23198256f83SJeeja KP switch (fmt->valid_bit_depth) { 23298256f83SJeeja KP case SKL_DEPTH_16BIT: 23398256f83SJeeja KP fmt->bit_depth = fmt->valid_bit_depth; 23498256f83SJeeja KP break; 23598256f83SJeeja KP 23698256f83SJeeja KP default: 23798256f83SJeeja KP fmt->bit_depth = SKL_DEPTH_32BIT; 23898256f83SJeeja KP break; 23998256f83SJeeja KP } 24098256f83SJeeja KP } 24198256f83SJeeja KP 242f7590d4fSJeeja KP } 243f7590d4fSJeeja KP 244f7590d4fSJeeja KP /* 245f7590d4fSJeeja KP * A pipeline may have modules which impact the pcm parameters, like SRC, 246f7590d4fSJeeja KP * channel converter, format converter. 247f7590d4fSJeeja KP * We need to calculate the output params by applying the 'fixup' 248f7590d4fSJeeja KP * Topology will tell driver which type of fixup is to be applied by 249f7590d4fSJeeja KP * supplying the fixup mask, so based on that we calculate the output 250f7590d4fSJeeja KP * 251f7590d4fSJeeja KP * Now In FE the pcm hw_params is source/target format. Same is applicable 252f7590d4fSJeeja KP * for BE with its hw_params invoked. 253f7590d4fSJeeja KP * here based on FE, BE pipeline and direction we calculate the input and 254f7590d4fSJeeja KP * outfix and then apply that for a module 255f7590d4fSJeeja KP */ 256f7590d4fSJeeja KP static void skl_tplg_update_params_fixup(struct skl_module_cfg *m_cfg, 257f7590d4fSJeeja KP struct skl_pipe_params *params, bool is_fe) 258f7590d4fSJeeja KP { 259f7590d4fSJeeja KP int in_fixup, out_fixup; 260f7590d4fSJeeja KP struct skl_module_fmt *in_fmt, *out_fmt; 261f7590d4fSJeeja KP 2624cd9899fSHardik T Shah /* Fixups will be applied to pin 0 only */ 2634cd9899fSHardik T Shah in_fmt = &m_cfg->in_fmt[0]; 2644cd9899fSHardik T Shah out_fmt = &m_cfg->out_fmt[0]; 265f7590d4fSJeeja KP 266f7590d4fSJeeja KP if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) { 267f7590d4fSJeeja KP if (is_fe) { 268f7590d4fSJeeja KP in_fixup = m_cfg->params_fixup; 269f7590d4fSJeeja KP out_fixup = (~m_cfg->converter) & 270f7590d4fSJeeja KP m_cfg->params_fixup; 271f7590d4fSJeeja KP } else { 272f7590d4fSJeeja KP out_fixup = m_cfg->params_fixup; 273f7590d4fSJeeja KP in_fixup = (~m_cfg->converter) & 274f7590d4fSJeeja KP m_cfg->params_fixup; 275f7590d4fSJeeja KP } 276f7590d4fSJeeja KP } else { 277f7590d4fSJeeja KP if (is_fe) { 278f7590d4fSJeeja KP out_fixup = m_cfg->params_fixup; 279f7590d4fSJeeja KP in_fixup = (~m_cfg->converter) & 280f7590d4fSJeeja KP m_cfg->params_fixup; 281f7590d4fSJeeja KP } else { 282f7590d4fSJeeja KP in_fixup = m_cfg->params_fixup; 283f7590d4fSJeeja KP out_fixup = (~m_cfg->converter) & 284f7590d4fSJeeja KP m_cfg->params_fixup; 285f7590d4fSJeeja KP } 286f7590d4fSJeeja KP } 287f7590d4fSJeeja KP 288f7590d4fSJeeja KP skl_tplg_update_params(in_fmt, params, in_fixup); 289f7590d4fSJeeja KP skl_tplg_update_params(out_fmt, params, out_fixup); 290f7590d4fSJeeja KP } 291f7590d4fSJeeja KP 292f7590d4fSJeeja KP /* 293f7590d4fSJeeja KP * A module needs input and output buffers, which are dependent upon pcm 294f7590d4fSJeeja KP * params, so once we have calculate params, we need buffer calculation as 295f7590d4fSJeeja KP * well. 296f7590d4fSJeeja KP */ 297f7590d4fSJeeja KP static void skl_tplg_update_buffer_size(struct skl_sst *ctx, 298f7590d4fSJeeja KP struct skl_module_cfg *mcfg) 299f7590d4fSJeeja KP { 300f7590d4fSJeeja KP int multiplier = 1; 3014cd9899fSHardik T Shah struct skl_module_fmt *in_fmt, *out_fmt; 3024cd9899fSHardik T Shah 3034cd9899fSHardik T Shah /* Since fixups is applied to pin 0 only, ibs, obs needs 3044cd9899fSHardik T Shah * change for pin 0 only 3054cd9899fSHardik T Shah */ 3064cd9899fSHardik T Shah in_fmt = &mcfg->in_fmt[0]; 3074cd9899fSHardik T Shah out_fmt = &mcfg->out_fmt[0]; 308f7590d4fSJeeja KP 309f7590d4fSJeeja KP if (mcfg->m_type == SKL_MODULE_TYPE_SRCINT) 310f7590d4fSJeeja KP multiplier = 5; 311f0c8e1d9SSubhransu S. Prusty 312*8e15e762STakashi Sakamoto mcfg->ibs = DIV_ROUND_UP(in_fmt->s_freq, 1000) * 313*8e15e762STakashi Sakamoto (mcfg->in_fmt->channels) * 3144cd9899fSHardik T Shah (mcfg->in_fmt->bit_depth >> 3) * 315f7590d4fSJeeja KP multiplier; 316f7590d4fSJeeja KP 317*8e15e762STakashi Sakamoto mcfg->obs = DIV_ROUND_UP(mcfg->out_fmt->s_freq, 1000) * 318*8e15e762STakashi Sakamoto (mcfg->out_fmt->channels) * 3194cd9899fSHardik T Shah (mcfg->out_fmt->bit_depth >> 3) * 320f7590d4fSJeeja KP multiplier; 321f7590d4fSJeeja KP } 322f7590d4fSJeeja KP 323db2f586bSSenthilnathan Veppur static u8 skl_tplg_be_dev_type(int dev_type) 324db2f586bSSenthilnathan Veppur { 325db2f586bSSenthilnathan Veppur int ret; 326db2f586bSSenthilnathan Veppur 327db2f586bSSenthilnathan Veppur switch (dev_type) { 328db2f586bSSenthilnathan Veppur case SKL_DEVICE_BT: 329db2f586bSSenthilnathan Veppur ret = NHLT_DEVICE_BT; 330db2f586bSSenthilnathan Veppur break; 331db2f586bSSenthilnathan Veppur 332db2f586bSSenthilnathan Veppur case SKL_DEVICE_DMIC: 333db2f586bSSenthilnathan Veppur ret = NHLT_DEVICE_DMIC; 334db2f586bSSenthilnathan Veppur break; 335db2f586bSSenthilnathan Veppur 336db2f586bSSenthilnathan Veppur case SKL_DEVICE_I2S: 337db2f586bSSenthilnathan Veppur ret = NHLT_DEVICE_I2S; 338db2f586bSSenthilnathan Veppur break; 339db2f586bSSenthilnathan Veppur 340db2f586bSSenthilnathan Veppur default: 341db2f586bSSenthilnathan Veppur ret = NHLT_DEVICE_INVALID; 342db2f586bSSenthilnathan Veppur break; 343db2f586bSSenthilnathan Veppur } 344db2f586bSSenthilnathan Veppur 345db2f586bSSenthilnathan Veppur return ret; 346db2f586bSSenthilnathan Veppur } 347db2f586bSSenthilnathan Veppur 3482d1419a3SJeeja KP static int skl_tplg_update_be_blob(struct snd_soc_dapm_widget *w, 3492d1419a3SJeeja KP struct skl_sst *ctx) 3502d1419a3SJeeja KP { 3512d1419a3SJeeja KP struct skl_module_cfg *m_cfg = w->priv; 3522d1419a3SJeeja KP int link_type, dir; 3532d1419a3SJeeja KP u32 ch, s_freq, s_fmt; 3542d1419a3SJeeja KP struct nhlt_specific_cfg *cfg; 3552d1419a3SJeeja KP struct skl *skl = get_skl_ctx(ctx->dev); 356db2f586bSSenthilnathan Veppur u8 dev_type = skl_tplg_be_dev_type(m_cfg->dev_type); 3572d1419a3SJeeja KP 3582d1419a3SJeeja KP /* check if we already have blob */ 3592d1419a3SJeeja KP if (m_cfg->formats_config.caps_size > 0) 3602d1419a3SJeeja KP return 0; 3612d1419a3SJeeja KP 362c7c6c736SJeeja KP dev_dbg(ctx->dev, "Applying default cfg blob\n"); 3632d1419a3SJeeja KP switch (m_cfg->dev_type) { 3642d1419a3SJeeja KP case SKL_DEVICE_DMIC: 3652d1419a3SJeeja KP link_type = NHLT_LINK_DMIC; 366c7c6c736SJeeja KP dir = SNDRV_PCM_STREAM_CAPTURE; 3672d1419a3SJeeja KP s_freq = m_cfg->in_fmt[0].s_freq; 3682d1419a3SJeeja KP s_fmt = m_cfg->in_fmt[0].bit_depth; 3692d1419a3SJeeja KP ch = m_cfg->in_fmt[0].channels; 3702d1419a3SJeeja KP break; 3712d1419a3SJeeja KP 3722d1419a3SJeeja KP case SKL_DEVICE_I2S: 3732d1419a3SJeeja KP link_type = NHLT_LINK_SSP; 3742d1419a3SJeeja KP if (m_cfg->hw_conn_type == SKL_CONN_SOURCE) { 375c7c6c736SJeeja KP dir = SNDRV_PCM_STREAM_PLAYBACK; 3762d1419a3SJeeja KP s_freq = m_cfg->out_fmt[0].s_freq; 3772d1419a3SJeeja KP s_fmt = m_cfg->out_fmt[0].bit_depth; 3782d1419a3SJeeja KP ch = m_cfg->out_fmt[0].channels; 379c7c6c736SJeeja KP } else { 380c7c6c736SJeeja KP dir = SNDRV_PCM_STREAM_CAPTURE; 381c7c6c736SJeeja KP s_freq = m_cfg->in_fmt[0].s_freq; 382c7c6c736SJeeja KP s_fmt = m_cfg->in_fmt[0].bit_depth; 383c7c6c736SJeeja KP ch = m_cfg->in_fmt[0].channels; 3842d1419a3SJeeja KP } 3852d1419a3SJeeja KP break; 3862d1419a3SJeeja KP 3872d1419a3SJeeja KP default: 3882d1419a3SJeeja KP return -EINVAL; 3892d1419a3SJeeja KP } 3902d1419a3SJeeja KP 3912d1419a3SJeeja KP /* update the blob based on virtual bus_id and default params */ 3922d1419a3SJeeja KP cfg = skl_get_ep_blob(skl, m_cfg->vbus_id, link_type, 393db2f586bSSenthilnathan Veppur s_fmt, ch, s_freq, dir, dev_type); 3942d1419a3SJeeja KP if (cfg) { 3952d1419a3SJeeja KP m_cfg->formats_config.caps_size = cfg->size; 3962d1419a3SJeeja KP m_cfg->formats_config.caps = (u32 *) &cfg->caps; 3972d1419a3SJeeja KP } else { 3982d1419a3SJeeja KP dev_err(ctx->dev, "Blob NULL for id %x type %d dirn %d\n", 3992d1419a3SJeeja KP m_cfg->vbus_id, link_type, dir); 4002d1419a3SJeeja KP dev_err(ctx->dev, "PCM: ch %d, freq %d, fmt %d\n", 4012d1419a3SJeeja KP ch, s_freq, s_fmt); 4022d1419a3SJeeja KP return -EIO; 4032d1419a3SJeeja KP } 4042d1419a3SJeeja KP 4052d1419a3SJeeja KP return 0; 4062d1419a3SJeeja KP } 4072d1419a3SJeeja KP 408f7590d4fSJeeja KP static void skl_tplg_update_module_params(struct snd_soc_dapm_widget *w, 409f7590d4fSJeeja KP struct skl_sst *ctx) 410f7590d4fSJeeja KP { 411f7590d4fSJeeja KP struct skl_module_cfg *m_cfg = w->priv; 412f7590d4fSJeeja KP struct skl_pipe_params *params = m_cfg->pipe->p_params; 413f7590d4fSJeeja KP int p_conn_type = m_cfg->pipe->conn_type; 414f7590d4fSJeeja KP bool is_fe; 415f7590d4fSJeeja KP 416f7590d4fSJeeja KP if (!m_cfg->params_fixup) 417f7590d4fSJeeja KP return; 418f7590d4fSJeeja KP 419f7590d4fSJeeja KP dev_dbg(ctx->dev, "Mconfig for widget=%s BEFORE updation\n", 420f7590d4fSJeeja KP w->name); 421f7590d4fSJeeja KP 422f7590d4fSJeeja KP skl_dump_mconfig(ctx, m_cfg); 423f7590d4fSJeeja KP 424f7590d4fSJeeja KP if (p_conn_type == SKL_PIPE_CONN_TYPE_FE) 425f7590d4fSJeeja KP is_fe = true; 426f7590d4fSJeeja KP else 427f7590d4fSJeeja KP is_fe = false; 428f7590d4fSJeeja KP 429f7590d4fSJeeja KP skl_tplg_update_params_fixup(m_cfg, params, is_fe); 430f7590d4fSJeeja KP skl_tplg_update_buffer_size(ctx, m_cfg); 431f7590d4fSJeeja KP 432f7590d4fSJeeja KP dev_dbg(ctx->dev, "Mconfig for widget=%s AFTER updation\n", 433f7590d4fSJeeja KP w->name); 434f7590d4fSJeeja KP 435f7590d4fSJeeja KP skl_dump_mconfig(ctx, m_cfg); 436f7590d4fSJeeja KP } 437f7590d4fSJeeja KP 438e4e2d2f4SJeeja KP /* 439abb74003SJeeja KP * some modules can have multiple params set from user control and 440abb74003SJeeja KP * need to be set after module is initialized. If set_param flag is 441abb74003SJeeja KP * set module params will be done after module is initialised. 442abb74003SJeeja KP */ 443abb74003SJeeja KP static int skl_tplg_set_module_params(struct snd_soc_dapm_widget *w, 444abb74003SJeeja KP struct skl_sst *ctx) 445abb74003SJeeja KP { 446abb74003SJeeja KP int i, ret; 447abb74003SJeeja KP struct skl_module_cfg *mconfig = w->priv; 448abb74003SJeeja KP const struct snd_kcontrol_new *k; 449abb74003SJeeja KP struct soc_bytes_ext *sb; 450abb74003SJeeja KP struct skl_algo_data *bc; 451abb74003SJeeja KP struct skl_specific_cfg *sp_cfg; 452abb74003SJeeja KP 453abb74003SJeeja KP if (mconfig->formats_config.caps_size > 0 && 4544ced1827SJeeja KP mconfig->formats_config.set_params == SKL_PARAM_SET) { 455abb74003SJeeja KP sp_cfg = &mconfig->formats_config; 456abb74003SJeeja KP ret = skl_set_module_params(ctx, sp_cfg->caps, 457abb74003SJeeja KP sp_cfg->caps_size, 458abb74003SJeeja KP sp_cfg->param_id, mconfig); 459abb74003SJeeja KP if (ret < 0) 460abb74003SJeeja KP return ret; 461abb74003SJeeja KP } 462abb74003SJeeja KP 463abb74003SJeeja KP for (i = 0; i < w->num_kcontrols; i++) { 464abb74003SJeeja KP k = &w->kcontrol_news[i]; 465abb74003SJeeja KP if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { 466abb74003SJeeja KP sb = (void *) k->private_value; 467abb74003SJeeja KP bc = (struct skl_algo_data *)sb->dobj.private; 468abb74003SJeeja KP 4694ced1827SJeeja KP if (bc->set_params == SKL_PARAM_SET) { 470abb74003SJeeja KP ret = skl_set_module_params(ctx, 4710d682104SDharageswari R (u32 *)bc->params, bc->size, 472abb74003SJeeja KP bc->param_id, mconfig); 473abb74003SJeeja KP if (ret < 0) 474abb74003SJeeja KP return ret; 475abb74003SJeeja KP } 476abb74003SJeeja KP } 477abb74003SJeeja KP } 478abb74003SJeeja KP 479abb74003SJeeja KP return 0; 480abb74003SJeeja KP } 481abb74003SJeeja KP 482abb74003SJeeja KP /* 483abb74003SJeeja KP * some module param can set from user control and this is required as 484abb74003SJeeja KP * when module is initailzed. if module param is required in init it is 485abb74003SJeeja KP * identifed by set_param flag. if set_param flag is not set, then this 486abb74003SJeeja KP * parameter needs to set as part of module init. 487abb74003SJeeja KP */ 488abb74003SJeeja KP static int skl_tplg_set_module_init_data(struct snd_soc_dapm_widget *w) 489abb74003SJeeja KP { 490abb74003SJeeja KP const struct snd_kcontrol_new *k; 491abb74003SJeeja KP struct soc_bytes_ext *sb; 492abb74003SJeeja KP struct skl_algo_data *bc; 493abb74003SJeeja KP struct skl_module_cfg *mconfig = w->priv; 494abb74003SJeeja KP int i; 495abb74003SJeeja KP 496abb74003SJeeja KP for (i = 0; i < w->num_kcontrols; i++) { 497abb74003SJeeja KP k = &w->kcontrol_news[i]; 498abb74003SJeeja KP if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { 499abb74003SJeeja KP sb = (struct soc_bytes_ext *)k->private_value; 500abb74003SJeeja KP bc = (struct skl_algo_data *)sb->dobj.private; 501abb74003SJeeja KP 5024ced1827SJeeja KP if (bc->set_params != SKL_PARAM_INIT) 503abb74003SJeeja KP continue; 504abb74003SJeeja KP 505abb74003SJeeja KP mconfig->formats_config.caps = (u32 *)&bc->params; 5060d682104SDharageswari R mconfig->formats_config.caps_size = bc->size; 507abb74003SJeeja KP 508abb74003SJeeja KP break; 509abb74003SJeeja KP } 510abb74003SJeeja KP } 511abb74003SJeeja KP 512abb74003SJeeja KP return 0; 513abb74003SJeeja KP } 514abb74003SJeeja KP 515bb704a73SJeeja KP static int skl_tplg_module_prepare(struct skl_sst *ctx, struct skl_pipe *pipe, 516bb704a73SJeeja KP struct snd_soc_dapm_widget *w, struct skl_module_cfg *mcfg) 517bb704a73SJeeja KP { 518bb704a73SJeeja KP switch (mcfg->dev_type) { 519bb704a73SJeeja KP case SKL_DEVICE_HDAHOST: 520bb704a73SJeeja KP return skl_pcm_host_dma_prepare(ctx->dev, pipe->p_params); 521bb704a73SJeeja KP 522bb704a73SJeeja KP case SKL_DEVICE_HDALINK: 523bb704a73SJeeja KP return skl_pcm_link_dma_prepare(ctx->dev, pipe->p_params); 524bb704a73SJeeja KP } 525bb704a73SJeeja KP 526bb704a73SJeeja KP return 0; 527bb704a73SJeeja KP } 528bb704a73SJeeja KP 529abb74003SJeeja KP /* 530e4e2d2f4SJeeja KP * Inside a pipe instance, we can have various modules. These modules need 531e4e2d2f4SJeeja KP * to instantiated in DSP by invoking INIT_MODULE IPC, which is achieved by 532e4e2d2f4SJeeja KP * skl_init_module() routine, so invoke that for all modules in a pipeline 533e4e2d2f4SJeeja KP */ 534e4e2d2f4SJeeja KP static int 535e4e2d2f4SJeeja KP skl_tplg_init_pipe_modules(struct skl *skl, struct skl_pipe *pipe) 536e4e2d2f4SJeeja KP { 537e4e2d2f4SJeeja KP struct skl_pipe_module *w_module; 538e4e2d2f4SJeeja KP struct snd_soc_dapm_widget *w; 539e4e2d2f4SJeeja KP struct skl_module_cfg *mconfig; 540e4e2d2f4SJeeja KP struct skl_sst *ctx = skl->skl_sst; 541e4e2d2f4SJeeja KP int ret = 0; 542e4e2d2f4SJeeja KP 543e4e2d2f4SJeeja KP list_for_each_entry(w_module, &pipe->w_list, node) { 544e4e2d2f4SJeeja KP w = w_module->w; 545e4e2d2f4SJeeja KP mconfig = w->priv; 546e4e2d2f4SJeeja KP 547b7c50555SVinod Koul /* check if module ids are populated */ 548b7c50555SVinod Koul if (mconfig->id.module_id < 0) { 549b7c50555SVinod Koul dev_err(skl->skl_sst->dev, 550a657ae7eSVinod Koul "module %pUL id not populated\n", 551a657ae7eSVinod Koul (uuid_le *)mconfig->guid); 552a657ae7eSVinod Koul return -EIO; 553b7c50555SVinod Koul } 554b7c50555SVinod Koul 555e4e2d2f4SJeeja KP /* check resource available */ 5569ba8ffefSDharageswari.R if (!skl_is_pipe_mcps_avail(skl, mconfig)) 557e4e2d2f4SJeeja KP return -ENOMEM; 558e4e2d2f4SJeeja KP 5596c5768b3SDharageswari R if (mconfig->is_loadable && ctx->dsp->fw_ops.load_mod) { 5606c5768b3SDharageswari R ret = ctx->dsp->fw_ops.load_mod(ctx->dsp, 5616c5768b3SDharageswari R mconfig->id.module_id, mconfig->guid); 5626c5768b3SDharageswari R if (ret < 0) 5636c5768b3SDharageswari R return ret; 564d643678bSJeeja KP 565d643678bSJeeja KP mconfig->m_state = SKL_MODULE_LOADED; 5666c5768b3SDharageswari R } 5676c5768b3SDharageswari R 568bb704a73SJeeja KP /* prepare the DMA if the module is gateway cpr */ 569bb704a73SJeeja KP ret = skl_tplg_module_prepare(ctx, pipe, w, mconfig); 570bb704a73SJeeja KP if (ret < 0) 571bb704a73SJeeja KP return ret; 572bb704a73SJeeja KP 5732d1419a3SJeeja KP /* update blob if blob is null for be with default value */ 5742d1419a3SJeeja KP skl_tplg_update_be_blob(w, ctx); 5752d1419a3SJeeja KP 576f7590d4fSJeeja KP /* 577f7590d4fSJeeja KP * apply fix/conversion to module params based on 578f7590d4fSJeeja KP * FE/BE params 579f7590d4fSJeeja KP */ 580f7590d4fSJeeja KP skl_tplg_update_module_params(w, ctx); 581ef2a352cSDharageswari R mconfig->id.pvt_id = skl_get_pvt_id(ctx, mconfig); 582ef2a352cSDharageswari R if (mconfig->id.pvt_id < 0) 583ef2a352cSDharageswari R return ret; 584abb74003SJeeja KP skl_tplg_set_module_init_data(w); 5859939a9c3SJeeja KP ret = skl_init_module(ctx, mconfig); 586ef2a352cSDharageswari R if (ret < 0) { 587ef2a352cSDharageswari R skl_put_pvt_id(ctx, mconfig); 588e4e2d2f4SJeeja KP return ret; 589ef2a352cSDharageswari R } 590260eb73aSDharageswari R skl_tplg_alloc_pipe_mcps(skl, mconfig); 591abb74003SJeeja KP ret = skl_tplg_set_module_params(w, ctx); 592e4e2d2f4SJeeja KP if (ret < 0) 593e4e2d2f4SJeeja KP return ret; 594e4e2d2f4SJeeja KP } 595e4e2d2f4SJeeja KP 596e4e2d2f4SJeeja KP return 0; 597e4e2d2f4SJeeja KP } 598d93f8e55SVinod Koul 5996c5768b3SDharageswari R static int skl_tplg_unload_pipe_modules(struct skl_sst *ctx, 6006c5768b3SDharageswari R struct skl_pipe *pipe) 6016c5768b3SDharageswari R { 602b0fab9c6SDharageswari R int ret; 6036c5768b3SDharageswari R struct skl_pipe_module *w_module = NULL; 6046c5768b3SDharageswari R struct skl_module_cfg *mconfig = NULL; 6056c5768b3SDharageswari R 6066c5768b3SDharageswari R list_for_each_entry(w_module, &pipe->w_list, node) { 6076c5768b3SDharageswari R mconfig = w_module->w->priv; 6086c5768b3SDharageswari R 609d643678bSJeeja KP if (mconfig->is_loadable && ctx->dsp->fw_ops.unload_mod && 610b0fab9c6SDharageswari R mconfig->m_state > SKL_MODULE_UNINIT) { 611b0fab9c6SDharageswari R ret = ctx->dsp->fw_ops.unload_mod(ctx->dsp, 6126c5768b3SDharageswari R mconfig->id.module_id); 613b0fab9c6SDharageswari R if (ret < 0) 614b0fab9c6SDharageswari R return -EIO; 615b0fab9c6SDharageswari R } 616ef2a352cSDharageswari R skl_put_pvt_id(ctx, mconfig); 6176c5768b3SDharageswari R } 6186c5768b3SDharageswari R 6196c5768b3SDharageswari R /* no modules to unload in this path, so return */ 6206c5768b3SDharageswari R return 0; 6216c5768b3SDharageswari R } 6226c5768b3SDharageswari R 623d93f8e55SVinod Koul /* 624d93f8e55SVinod Koul * Mixer module represents a pipeline. So in the Pre-PMU event of mixer we 625d93f8e55SVinod Koul * need create the pipeline. So we do following: 626d93f8e55SVinod Koul * - check the resources 627d93f8e55SVinod Koul * - Create the pipeline 628d93f8e55SVinod Koul * - Initialize the modules in pipeline 629d93f8e55SVinod Koul * - finally bind all modules together 630d93f8e55SVinod Koul */ 631d93f8e55SVinod Koul static int skl_tplg_mixer_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w, 632d93f8e55SVinod Koul struct skl *skl) 633d93f8e55SVinod Koul { 634d93f8e55SVinod Koul int ret; 635d93f8e55SVinod Koul struct skl_module_cfg *mconfig = w->priv; 636d93f8e55SVinod Koul struct skl_pipe_module *w_module; 637d93f8e55SVinod Koul struct skl_pipe *s_pipe = mconfig->pipe; 638d93f8e55SVinod Koul struct skl_module_cfg *src_module = NULL, *dst_module; 639d93f8e55SVinod Koul struct skl_sst *ctx = skl->skl_sst; 640d93f8e55SVinod Koul 641d93f8e55SVinod Koul /* check resource available */ 6429ba8ffefSDharageswari.R if (!skl_is_pipe_mcps_avail(skl, mconfig)) 643d93f8e55SVinod Koul return -EBUSY; 644d93f8e55SVinod Koul 6459ba8ffefSDharageswari.R if (!skl_is_pipe_mem_avail(skl, mconfig)) 646d93f8e55SVinod Koul return -ENOMEM; 647d93f8e55SVinod Koul 648d93f8e55SVinod Koul /* 649d93f8e55SVinod Koul * Create a list of modules for pipe. 650d93f8e55SVinod Koul * This list contains modules from source to sink 651d93f8e55SVinod Koul */ 652d93f8e55SVinod Koul ret = skl_create_pipeline(ctx, mconfig->pipe); 653d93f8e55SVinod Koul if (ret < 0) 654d93f8e55SVinod Koul return ret; 655d93f8e55SVinod Koul 656260eb73aSDharageswari R skl_tplg_alloc_pipe_mem(skl, mconfig); 657260eb73aSDharageswari R skl_tplg_alloc_pipe_mcps(skl, mconfig); 658d93f8e55SVinod Koul 659d93f8e55SVinod Koul /* Init all pipe modules from source to sink */ 660d93f8e55SVinod Koul ret = skl_tplg_init_pipe_modules(skl, s_pipe); 661d93f8e55SVinod Koul if (ret < 0) 662d93f8e55SVinod Koul return ret; 663d93f8e55SVinod Koul 664d93f8e55SVinod Koul /* Bind modules from source to sink */ 665d93f8e55SVinod Koul list_for_each_entry(w_module, &s_pipe->w_list, node) { 666d93f8e55SVinod Koul dst_module = w_module->w->priv; 667d93f8e55SVinod Koul 668d93f8e55SVinod Koul if (src_module == NULL) { 669d93f8e55SVinod Koul src_module = dst_module; 670d93f8e55SVinod Koul continue; 671d93f8e55SVinod Koul } 672d93f8e55SVinod Koul 673d93f8e55SVinod Koul ret = skl_bind_modules(ctx, src_module, dst_module); 674d93f8e55SVinod Koul if (ret < 0) 675d93f8e55SVinod Koul return ret; 676d93f8e55SVinod Koul 677d93f8e55SVinod Koul src_module = dst_module; 678d93f8e55SVinod Koul } 679d93f8e55SVinod Koul 680d93f8e55SVinod Koul return 0; 681d93f8e55SVinod Koul } 682d93f8e55SVinod Koul 6835e8f0ee4SDharageswari R static int skl_fill_sink_instance_id(struct skl_sst *ctx, 6845e8f0ee4SDharageswari R struct skl_algo_data *alg_data) 6855e8f0ee4SDharageswari R { 6865e8f0ee4SDharageswari R struct skl_kpb_params *params = (struct skl_kpb_params *)alg_data->params; 6875e8f0ee4SDharageswari R struct skl_mod_inst_map *inst; 6885e8f0ee4SDharageswari R int i, pvt_id; 6895e8f0ee4SDharageswari R 6905e8f0ee4SDharageswari R inst = params->map; 6915e8f0ee4SDharageswari R 6925e8f0ee4SDharageswari R for (i = 0; i < params->num_modules; i++) { 6935e8f0ee4SDharageswari R pvt_id = skl_get_pvt_instance_id_map(ctx, 6945e8f0ee4SDharageswari R inst->mod_id, inst->inst_id); 6955e8f0ee4SDharageswari R if (pvt_id < 0) 6965e8f0ee4SDharageswari R return -EINVAL; 6975e8f0ee4SDharageswari R inst->inst_id = pvt_id; 6985e8f0ee4SDharageswari R inst++; 6995e8f0ee4SDharageswari R } 7005e8f0ee4SDharageswari R return 0; 7015e8f0ee4SDharageswari R } 7025e8f0ee4SDharageswari R 703cc6a4044SJeeja KP /* 704cc6a4044SJeeja KP * Some modules require params to be set after the module is bound to 705cc6a4044SJeeja KP * all pins connected. 706cc6a4044SJeeja KP * 707cc6a4044SJeeja KP * The module provider initializes set_param flag for such modules and we 708cc6a4044SJeeja KP * send params after binding 709cc6a4044SJeeja KP */ 710cc6a4044SJeeja KP static int skl_tplg_set_module_bind_params(struct snd_soc_dapm_widget *w, 711cc6a4044SJeeja KP struct skl_module_cfg *mcfg, struct skl_sst *ctx) 712cc6a4044SJeeja KP { 713cc6a4044SJeeja KP int i, ret; 714cc6a4044SJeeja KP struct skl_module_cfg *mconfig = w->priv; 715cc6a4044SJeeja KP const struct snd_kcontrol_new *k; 716cc6a4044SJeeja KP struct soc_bytes_ext *sb; 717cc6a4044SJeeja KP struct skl_algo_data *bc; 718cc6a4044SJeeja KP struct skl_specific_cfg *sp_cfg; 719cc6a4044SJeeja KP 720cc6a4044SJeeja KP /* 721cc6a4044SJeeja KP * check all out/in pins are in bind state. 722cc6a4044SJeeja KP * if so set the module param 723cc6a4044SJeeja KP */ 724cc6a4044SJeeja KP for (i = 0; i < mcfg->max_out_queue; i++) { 725cc6a4044SJeeja KP if (mcfg->m_out_pin[i].pin_state != SKL_PIN_BIND_DONE) 726cc6a4044SJeeja KP return 0; 727cc6a4044SJeeja KP } 728cc6a4044SJeeja KP 729cc6a4044SJeeja KP for (i = 0; i < mcfg->max_in_queue; i++) { 730cc6a4044SJeeja KP if (mcfg->m_in_pin[i].pin_state != SKL_PIN_BIND_DONE) 731cc6a4044SJeeja KP return 0; 732cc6a4044SJeeja KP } 733cc6a4044SJeeja KP 734cc6a4044SJeeja KP if (mconfig->formats_config.caps_size > 0 && 735cc6a4044SJeeja KP mconfig->formats_config.set_params == SKL_PARAM_BIND) { 736cc6a4044SJeeja KP sp_cfg = &mconfig->formats_config; 737cc6a4044SJeeja KP ret = skl_set_module_params(ctx, sp_cfg->caps, 738cc6a4044SJeeja KP sp_cfg->caps_size, 739cc6a4044SJeeja KP sp_cfg->param_id, mconfig); 740cc6a4044SJeeja KP if (ret < 0) 741cc6a4044SJeeja KP return ret; 742cc6a4044SJeeja KP } 743cc6a4044SJeeja KP 744cc6a4044SJeeja KP for (i = 0; i < w->num_kcontrols; i++) { 745cc6a4044SJeeja KP k = &w->kcontrol_news[i]; 746cc6a4044SJeeja KP if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { 747cc6a4044SJeeja KP sb = (void *) k->private_value; 748cc6a4044SJeeja KP bc = (struct skl_algo_data *)sb->dobj.private; 749cc6a4044SJeeja KP 750cc6a4044SJeeja KP if (bc->set_params == SKL_PARAM_BIND) { 7515e8f0ee4SDharageswari R if (mconfig->m_type == SKL_MODULE_TYPE_KPB) 7525e8f0ee4SDharageswari R skl_fill_sink_instance_id(ctx, bc); 753cc6a4044SJeeja KP ret = skl_set_module_params(ctx, 754cc6a4044SJeeja KP (u32 *)bc->params, bc->max, 755cc6a4044SJeeja KP bc->param_id, mconfig); 756cc6a4044SJeeja KP if (ret < 0) 757cc6a4044SJeeja KP return ret; 758cc6a4044SJeeja KP } 759cc6a4044SJeeja KP } 760cc6a4044SJeeja KP } 761cc6a4044SJeeja KP 762cc6a4044SJeeja KP return 0; 763cc6a4044SJeeja KP } 764cc6a4044SJeeja KP 7658724ff17SJeeja KP static int skl_tplg_bind_sinks(struct snd_soc_dapm_widget *w, 7668724ff17SJeeja KP struct skl *skl, 7676bd4cf85SJeeja KP struct snd_soc_dapm_widget *src_w, 7688724ff17SJeeja KP struct skl_module_cfg *src_mconfig) 769d93f8e55SVinod Koul { 770d93f8e55SVinod Koul struct snd_soc_dapm_path *p; 7710ed95d76SJeeja KP struct snd_soc_dapm_widget *sink = NULL, *next_sink = NULL; 7728724ff17SJeeja KP struct skl_module_cfg *sink_mconfig; 773d93f8e55SVinod Koul struct skl_sst *ctx = skl->skl_sst; 7748724ff17SJeeja KP int ret; 775d93f8e55SVinod Koul 7768724ff17SJeeja KP snd_soc_dapm_widget_for_each_sink_path(w, p) { 777d93f8e55SVinod Koul if (!p->connect) 778d93f8e55SVinod Koul continue; 779d93f8e55SVinod Koul 780d93f8e55SVinod Koul dev_dbg(ctx->dev, "%s: src widget=%s\n", __func__, w->name); 781d93f8e55SVinod Koul dev_dbg(ctx->dev, "%s: sink widget=%s\n", __func__, p->sink->name); 782d93f8e55SVinod Koul 7830ed95d76SJeeja KP next_sink = p->sink; 7846bd4cf85SJeeja KP 7856bd4cf85SJeeja KP if (!is_skl_dsp_widget_type(p->sink)) 7866bd4cf85SJeeja KP return skl_tplg_bind_sinks(p->sink, skl, src_w, src_mconfig); 7876bd4cf85SJeeja KP 788d93f8e55SVinod Koul /* 789d93f8e55SVinod Koul * here we will check widgets in sink pipelines, so that 790d93f8e55SVinod Koul * can be any widgets type and we are only interested if 791d93f8e55SVinod Koul * they are ones used for SKL so check that first 792d93f8e55SVinod Koul */ 793d93f8e55SVinod Koul if ((p->sink->priv != NULL) && 794d93f8e55SVinod Koul is_skl_dsp_widget_type(p->sink)) { 795d93f8e55SVinod Koul 796d93f8e55SVinod Koul sink = p->sink; 797d93f8e55SVinod Koul sink_mconfig = sink->priv; 798d93f8e55SVinod Koul 799cc6a4044SJeeja KP if (src_mconfig->m_state == SKL_MODULE_UNINIT || 800cc6a4044SJeeja KP sink_mconfig->m_state == SKL_MODULE_UNINIT) 801cc6a4044SJeeja KP continue; 802cc6a4044SJeeja KP 803d93f8e55SVinod Koul /* Bind source to sink, mixin is always source */ 804d93f8e55SVinod Koul ret = skl_bind_modules(ctx, src_mconfig, sink_mconfig); 805d93f8e55SVinod Koul if (ret) 806d93f8e55SVinod Koul return ret; 807d93f8e55SVinod Koul 808cc6a4044SJeeja KP /* set module params after bind */ 809cc6a4044SJeeja KP skl_tplg_set_module_bind_params(src_w, src_mconfig, ctx); 810cc6a4044SJeeja KP skl_tplg_set_module_bind_params(sink, sink_mconfig, ctx); 811cc6a4044SJeeja KP 812d93f8e55SVinod Koul /* Start sinks pipe first */ 813d93f8e55SVinod Koul if (sink_mconfig->pipe->state != SKL_PIPE_STARTED) { 814d1730c3dSJeeja KP if (sink_mconfig->pipe->conn_type != 815d1730c3dSJeeja KP SKL_PIPE_CONN_TYPE_FE) 816d1730c3dSJeeja KP ret = skl_run_pipe(ctx, 817d1730c3dSJeeja KP sink_mconfig->pipe); 818d93f8e55SVinod Koul if (ret) 819d93f8e55SVinod Koul return ret; 820d93f8e55SVinod Koul } 821d93f8e55SVinod Koul } 822d93f8e55SVinod Koul } 823d93f8e55SVinod Koul 8248724ff17SJeeja KP if (!sink) 8256bd4cf85SJeeja KP return skl_tplg_bind_sinks(next_sink, skl, src_w, src_mconfig); 8268724ff17SJeeja KP 8278724ff17SJeeja KP return 0; 8288724ff17SJeeja KP } 8298724ff17SJeeja KP 830d93f8e55SVinod Koul /* 831d93f8e55SVinod Koul * A PGA represents a module in a pipeline. So in the Pre-PMU event of PGA 832d93f8e55SVinod Koul * we need to do following: 833d93f8e55SVinod Koul * - Bind to sink pipeline 834d93f8e55SVinod Koul * Since the sink pipes can be running and we don't get mixer event on 835d93f8e55SVinod Koul * connect for already running mixer, we need to find the sink pipes 836d93f8e55SVinod Koul * here and bind to them. This way dynamic connect works. 837d93f8e55SVinod Koul * - Start sink pipeline, if not running 838d93f8e55SVinod Koul * - Then run current pipe 839d93f8e55SVinod Koul */ 840d93f8e55SVinod Koul static int skl_tplg_pga_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w, 841d93f8e55SVinod Koul struct skl *skl) 842d93f8e55SVinod Koul { 8438724ff17SJeeja KP struct skl_module_cfg *src_mconfig; 844d93f8e55SVinod Koul struct skl_sst *ctx = skl->skl_sst; 845d93f8e55SVinod Koul int ret = 0; 846d93f8e55SVinod Koul 8478724ff17SJeeja KP src_mconfig = w->priv; 848d93f8e55SVinod Koul 849d93f8e55SVinod Koul /* 850d93f8e55SVinod Koul * find which sink it is connected to, bind with the sink, 851d93f8e55SVinod Koul * if sink is not started, start sink pipe first, then start 852d93f8e55SVinod Koul * this pipe 853d93f8e55SVinod Koul */ 8546bd4cf85SJeeja KP ret = skl_tplg_bind_sinks(w, skl, w, src_mconfig); 8558724ff17SJeeja KP if (ret) 8568724ff17SJeeja KP return ret; 8578724ff17SJeeja KP 858d93f8e55SVinod Koul /* Start source pipe last after starting all sinks */ 859d1730c3dSJeeja KP if (src_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE) 860d1730c3dSJeeja KP return skl_run_pipe(ctx, src_mconfig->pipe); 861d93f8e55SVinod Koul 862d93f8e55SVinod Koul return 0; 863d93f8e55SVinod Koul } 864d93f8e55SVinod Koul 8658724ff17SJeeja KP static struct snd_soc_dapm_widget *skl_get_src_dsp_widget( 8668724ff17SJeeja KP struct snd_soc_dapm_widget *w, struct skl *skl) 8678724ff17SJeeja KP { 8688724ff17SJeeja KP struct snd_soc_dapm_path *p; 8698724ff17SJeeja KP struct snd_soc_dapm_widget *src_w = NULL; 8708724ff17SJeeja KP struct skl_sst *ctx = skl->skl_sst; 8718724ff17SJeeja KP 872d93f8e55SVinod Koul snd_soc_dapm_widget_for_each_source_path(w, p) { 8738724ff17SJeeja KP src_w = p->source; 874d93f8e55SVinod Koul if (!p->connect) 875d93f8e55SVinod Koul continue; 876d93f8e55SVinod Koul 8778724ff17SJeeja KP dev_dbg(ctx->dev, "sink widget=%s\n", w->name); 8788724ff17SJeeja KP dev_dbg(ctx->dev, "src widget=%s\n", p->source->name); 879d93f8e55SVinod Koul 880d93f8e55SVinod Koul /* 8818724ff17SJeeja KP * here we will check widgets in sink pipelines, so that can 8828724ff17SJeeja KP * be any widgets type and we are only interested if they are 8838724ff17SJeeja KP * ones used for SKL so check that first 884d93f8e55SVinod Koul */ 8858724ff17SJeeja KP if ((p->source->priv != NULL) && 8868724ff17SJeeja KP is_skl_dsp_widget_type(p->source)) { 8878724ff17SJeeja KP return p->source; 888d93f8e55SVinod Koul } 889d93f8e55SVinod Koul } 890d93f8e55SVinod Koul 8918724ff17SJeeja KP if (src_w != NULL) 8928724ff17SJeeja KP return skl_get_src_dsp_widget(src_w, skl); 893d93f8e55SVinod Koul 8948724ff17SJeeja KP return NULL; 895d93f8e55SVinod Koul } 896d93f8e55SVinod Koul 897d93f8e55SVinod Koul /* 898d93f8e55SVinod Koul * in the Post-PMU event of mixer we need to do following: 899d93f8e55SVinod Koul * - Check if this pipe is running 900d93f8e55SVinod Koul * - if not, then 901d93f8e55SVinod Koul * - bind this pipeline to its source pipeline 902d93f8e55SVinod Koul * if source pipe is already running, this means it is a dynamic 903d93f8e55SVinod Koul * connection and we need to bind only to that pipe 904d93f8e55SVinod Koul * - start this pipeline 905d93f8e55SVinod Koul */ 906d93f8e55SVinod Koul static int skl_tplg_mixer_dapm_post_pmu_event(struct snd_soc_dapm_widget *w, 907d93f8e55SVinod Koul struct skl *skl) 908d93f8e55SVinod Koul { 909d93f8e55SVinod Koul int ret = 0; 910d93f8e55SVinod Koul struct snd_soc_dapm_widget *source, *sink; 911d93f8e55SVinod Koul struct skl_module_cfg *src_mconfig, *sink_mconfig; 912d93f8e55SVinod Koul struct skl_sst *ctx = skl->skl_sst; 913d93f8e55SVinod Koul int src_pipe_started = 0; 914d93f8e55SVinod Koul 915d93f8e55SVinod Koul sink = w; 916d93f8e55SVinod Koul sink_mconfig = sink->priv; 917d93f8e55SVinod Koul 918d93f8e55SVinod Koul /* 919d93f8e55SVinod Koul * If source pipe is already started, that means source is driving 920d93f8e55SVinod Koul * one more sink before this sink got connected, Since source is 921d93f8e55SVinod Koul * started, bind this sink to source and start this pipe. 922d93f8e55SVinod Koul */ 9238724ff17SJeeja KP source = skl_get_src_dsp_widget(w, skl); 9248724ff17SJeeja KP if (source != NULL) { 925d93f8e55SVinod Koul src_mconfig = source->priv; 926d93f8e55SVinod Koul sink_mconfig = sink->priv; 927d93f8e55SVinod Koul src_pipe_started = 1; 928d93f8e55SVinod Koul 929d93f8e55SVinod Koul /* 9308724ff17SJeeja KP * check pipe state, then no need to bind or start the 9318724ff17SJeeja KP * pipe 932d93f8e55SVinod Koul */ 933d93f8e55SVinod Koul if (src_mconfig->pipe->state != SKL_PIPE_STARTED) 934d93f8e55SVinod Koul src_pipe_started = 0; 935d93f8e55SVinod Koul } 936d93f8e55SVinod Koul 937d93f8e55SVinod Koul if (src_pipe_started) { 938d93f8e55SVinod Koul ret = skl_bind_modules(ctx, src_mconfig, sink_mconfig); 939d93f8e55SVinod Koul if (ret) 940d93f8e55SVinod Koul return ret; 941d93f8e55SVinod Koul 942cc6a4044SJeeja KP /* set module params after bind */ 943cc6a4044SJeeja KP skl_tplg_set_module_bind_params(source, src_mconfig, ctx); 944cc6a4044SJeeja KP skl_tplg_set_module_bind_params(sink, sink_mconfig, ctx); 945cc6a4044SJeeja KP 946d1730c3dSJeeja KP if (sink_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE) 947d93f8e55SVinod Koul ret = skl_run_pipe(ctx, sink_mconfig->pipe); 948d93f8e55SVinod Koul } 949d93f8e55SVinod Koul 950d93f8e55SVinod Koul return ret; 951d93f8e55SVinod Koul } 952d93f8e55SVinod Koul 953d93f8e55SVinod Koul /* 954d93f8e55SVinod Koul * in the Pre-PMD event of mixer we need to do following: 955d93f8e55SVinod Koul * - Stop the pipe 956d93f8e55SVinod Koul * - find the source connections and remove that from dapm_path_list 957d93f8e55SVinod Koul * - unbind with source pipelines if still connected 958d93f8e55SVinod Koul */ 959d93f8e55SVinod Koul static int skl_tplg_mixer_dapm_pre_pmd_event(struct snd_soc_dapm_widget *w, 960d93f8e55SVinod Koul struct skl *skl) 961d93f8e55SVinod Koul { 962d93f8e55SVinod Koul struct skl_module_cfg *src_mconfig, *sink_mconfig; 963ce1b5551SJeeja KP int ret = 0, i; 964d93f8e55SVinod Koul struct skl_sst *ctx = skl->skl_sst; 965d93f8e55SVinod Koul 966ce1b5551SJeeja KP sink_mconfig = w->priv; 967d93f8e55SVinod Koul 968d93f8e55SVinod Koul /* Stop the pipe */ 969d93f8e55SVinod Koul ret = skl_stop_pipe(ctx, sink_mconfig->pipe); 970d93f8e55SVinod Koul if (ret) 971d93f8e55SVinod Koul return ret; 972d93f8e55SVinod Koul 973ce1b5551SJeeja KP for (i = 0; i < sink_mconfig->max_in_queue; i++) { 974ce1b5551SJeeja KP if (sink_mconfig->m_in_pin[i].pin_state == SKL_PIN_BIND_DONE) { 975ce1b5551SJeeja KP src_mconfig = sink_mconfig->m_in_pin[i].tgt_mcfg; 976ce1b5551SJeeja KP if (!src_mconfig) 977ce1b5551SJeeja KP continue; 978d93f8e55SVinod Koul /* 979ce1b5551SJeeja KP * If path_found == 1, that means pmd for source 980ce1b5551SJeeja KP * pipe has not occurred, source is connected to 981ce1b5551SJeeja KP * some other sink. so its responsibility of sink 982ce1b5551SJeeja KP * to unbind itself from source. 983d93f8e55SVinod Koul */ 984d93f8e55SVinod Koul ret = skl_stop_pipe(ctx, src_mconfig->pipe); 985d93f8e55SVinod Koul if (ret < 0) 986d93f8e55SVinod Koul return ret; 987d93f8e55SVinod Koul 988ce1b5551SJeeja KP ret = skl_unbind_modules(ctx, 989ce1b5551SJeeja KP src_mconfig, sink_mconfig); 990ce1b5551SJeeja KP } 991d93f8e55SVinod Koul } 992d93f8e55SVinod Koul 993d93f8e55SVinod Koul return ret; 994d93f8e55SVinod Koul } 995d93f8e55SVinod Koul 996d93f8e55SVinod Koul /* 997d93f8e55SVinod Koul * in the Post-PMD event of mixer we need to do following: 998d93f8e55SVinod Koul * - Free the mcps used 999d93f8e55SVinod Koul * - Free the mem used 1000d93f8e55SVinod Koul * - Unbind the modules within the pipeline 1001d93f8e55SVinod Koul * - Delete the pipeline (modules are not required to be explicitly 1002d93f8e55SVinod Koul * deleted, pipeline delete is enough here 1003d93f8e55SVinod Koul */ 1004d93f8e55SVinod Koul static int skl_tplg_mixer_dapm_post_pmd_event(struct snd_soc_dapm_widget *w, 1005d93f8e55SVinod Koul struct skl *skl) 1006d93f8e55SVinod Koul { 1007d93f8e55SVinod Koul struct skl_module_cfg *mconfig = w->priv; 1008d93f8e55SVinod Koul struct skl_pipe_module *w_module; 1009d93f8e55SVinod Koul struct skl_module_cfg *src_module = NULL, *dst_module; 1010d93f8e55SVinod Koul struct skl_sst *ctx = skl->skl_sst; 1011d93f8e55SVinod Koul struct skl_pipe *s_pipe = mconfig->pipe; 1012d93f8e55SVinod Koul 1013260eb73aSDharageswari R if (s_pipe->state == SKL_PIPE_INVALID) 1014260eb73aSDharageswari R return -EINVAL; 1015260eb73aSDharageswari R 1016d93f8e55SVinod Koul skl_tplg_free_pipe_mcps(skl, mconfig); 101765976878SVinod Koul skl_tplg_free_pipe_mem(skl, mconfig); 1018d93f8e55SVinod Koul 1019d93f8e55SVinod Koul list_for_each_entry(w_module, &s_pipe->w_list, node) { 1020d93f8e55SVinod Koul dst_module = w_module->w->priv; 1021d93f8e55SVinod Koul 1022260eb73aSDharageswari R if (mconfig->m_state >= SKL_MODULE_INIT_DONE) 10237ae3cb15SVinod Koul skl_tplg_free_pipe_mcps(skl, dst_module); 1024d93f8e55SVinod Koul if (src_module == NULL) { 1025d93f8e55SVinod Koul src_module = dst_module; 1026d93f8e55SVinod Koul continue; 1027d93f8e55SVinod Koul } 1028d93f8e55SVinod Koul 10297ca42f5aSGuneshwor Singh skl_unbind_modules(ctx, src_module, dst_module); 1030d93f8e55SVinod Koul src_module = dst_module; 1031d93f8e55SVinod Koul } 1032d93f8e55SVinod Koul 1033547cafa3SVinod Koul skl_delete_pipe(ctx, mconfig->pipe); 1034d93f8e55SVinod Koul 10356c5768b3SDharageswari R return skl_tplg_unload_pipe_modules(ctx, s_pipe); 1036d93f8e55SVinod Koul } 1037d93f8e55SVinod Koul 1038d93f8e55SVinod Koul /* 1039d93f8e55SVinod Koul * in the Post-PMD event of PGA we need to do following: 1040d93f8e55SVinod Koul * - Free the mcps used 1041d93f8e55SVinod Koul * - Stop the pipeline 1042d93f8e55SVinod Koul * - In source pipe is connected, unbind with source pipelines 1043d93f8e55SVinod Koul */ 1044d93f8e55SVinod Koul static int skl_tplg_pga_dapm_post_pmd_event(struct snd_soc_dapm_widget *w, 1045d93f8e55SVinod Koul struct skl *skl) 1046d93f8e55SVinod Koul { 1047d93f8e55SVinod Koul struct skl_module_cfg *src_mconfig, *sink_mconfig; 1048ce1b5551SJeeja KP int ret = 0, i; 1049d93f8e55SVinod Koul struct skl_sst *ctx = skl->skl_sst; 1050d93f8e55SVinod Koul 1051ce1b5551SJeeja KP src_mconfig = w->priv; 1052d93f8e55SVinod Koul 1053d93f8e55SVinod Koul /* Stop the pipe since this is a mixin module */ 1054d93f8e55SVinod Koul ret = skl_stop_pipe(ctx, src_mconfig->pipe); 1055d93f8e55SVinod Koul if (ret) 1056d93f8e55SVinod Koul return ret; 1057d93f8e55SVinod Koul 1058ce1b5551SJeeja KP for (i = 0; i < src_mconfig->max_out_queue; i++) { 1059ce1b5551SJeeja KP if (src_mconfig->m_out_pin[i].pin_state == SKL_PIN_BIND_DONE) { 1060ce1b5551SJeeja KP sink_mconfig = src_mconfig->m_out_pin[i].tgt_mcfg; 1061ce1b5551SJeeja KP if (!sink_mconfig) 1062ce1b5551SJeeja KP continue; 1063d93f8e55SVinod Koul /* 1064ce1b5551SJeeja KP * This is a connecter and if path is found that means 1065d93f8e55SVinod Koul * unbind between source and sink has not happened yet 1066d93f8e55SVinod Koul */ 1067ce1b5551SJeeja KP ret = skl_unbind_modules(ctx, src_mconfig, 1068ce1b5551SJeeja KP sink_mconfig); 1069ce1b5551SJeeja KP } 1070d93f8e55SVinod Koul } 1071d93f8e55SVinod Koul 1072d93f8e55SVinod Koul return ret; 1073d93f8e55SVinod Koul } 1074d93f8e55SVinod Koul 1075d93f8e55SVinod Koul /* 1076d93f8e55SVinod Koul * In modelling, we assume there will be ONLY one mixer in a pipeline. If 1077d93f8e55SVinod Koul * mixer is not required then it is treated as static mixer aka vmixer with 1078d93f8e55SVinod Koul * a hard path to source module 1079d93f8e55SVinod Koul * So we don't need to check if source is started or not as hard path puts 1080d93f8e55SVinod Koul * dependency on each other 1081d93f8e55SVinod Koul */ 1082d93f8e55SVinod Koul static int skl_tplg_vmixer_event(struct snd_soc_dapm_widget *w, 1083d93f8e55SVinod Koul struct snd_kcontrol *k, int event) 1084d93f8e55SVinod Koul { 1085d93f8e55SVinod Koul struct snd_soc_dapm_context *dapm = w->dapm; 1086d93f8e55SVinod Koul struct skl *skl = get_skl_ctx(dapm->dev); 1087d93f8e55SVinod Koul 1088d93f8e55SVinod Koul switch (event) { 1089d93f8e55SVinod Koul case SND_SOC_DAPM_PRE_PMU: 1090d93f8e55SVinod Koul return skl_tplg_mixer_dapm_pre_pmu_event(w, skl); 1091d93f8e55SVinod Koul 1092de1fedf2SJeeja KP case SND_SOC_DAPM_POST_PMU: 1093de1fedf2SJeeja KP return skl_tplg_mixer_dapm_post_pmu_event(w, skl); 1094de1fedf2SJeeja KP 1095de1fedf2SJeeja KP case SND_SOC_DAPM_PRE_PMD: 1096de1fedf2SJeeja KP return skl_tplg_mixer_dapm_pre_pmd_event(w, skl); 1097de1fedf2SJeeja KP 1098d93f8e55SVinod Koul case SND_SOC_DAPM_POST_PMD: 1099d93f8e55SVinod Koul return skl_tplg_mixer_dapm_post_pmd_event(w, skl); 1100d93f8e55SVinod Koul } 1101d93f8e55SVinod Koul 1102d93f8e55SVinod Koul return 0; 1103d93f8e55SVinod Koul } 1104d93f8e55SVinod Koul 1105d93f8e55SVinod Koul /* 1106d93f8e55SVinod Koul * In modelling, we assume there will be ONLY one mixer in a pipeline. If a 1107d93f8e55SVinod Koul * second one is required that is created as another pipe entity. 1108d93f8e55SVinod Koul * The mixer is responsible for pipe management and represent a pipeline 1109d93f8e55SVinod Koul * instance 1110d93f8e55SVinod Koul */ 1111d93f8e55SVinod Koul static int skl_tplg_mixer_event(struct snd_soc_dapm_widget *w, 1112d93f8e55SVinod Koul struct snd_kcontrol *k, int event) 1113d93f8e55SVinod Koul { 1114d93f8e55SVinod Koul struct snd_soc_dapm_context *dapm = w->dapm; 1115d93f8e55SVinod Koul struct skl *skl = get_skl_ctx(dapm->dev); 1116d93f8e55SVinod Koul 1117d93f8e55SVinod Koul switch (event) { 1118d93f8e55SVinod Koul case SND_SOC_DAPM_PRE_PMU: 1119d93f8e55SVinod Koul return skl_tplg_mixer_dapm_pre_pmu_event(w, skl); 1120d93f8e55SVinod Koul 1121d93f8e55SVinod Koul case SND_SOC_DAPM_POST_PMU: 1122d93f8e55SVinod Koul return skl_tplg_mixer_dapm_post_pmu_event(w, skl); 1123d93f8e55SVinod Koul 1124d93f8e55SVinod Koul case SND_SOC_DAPM_PRE_PMD: 1125d93f8e55SVinod Koul return skl_tplg_mixer_dapm_pre_pmd_event(w, skl); 1126d93f8e55SVinod Koul 1127d93f8e55SVinod Koul case SND_SOC_DAPM_POST_PMD: 1128d93f8e55SVinod Koul return skl_tplg_mixer_dapm_post_pmd_event(w, skl); 1129d93f8e55SVinod Koul } 1130d93f8e55SVinod Koul 1131d93f8e55SVinod Koul return 0; 1132d93f8e55SVinod Koul } 1133d93f8e55SVinod Koul 1134d93f8e55SVinod Koul /* 1135d93f8e55SVinod Koul * In modelling, we assumed rest of the modules in pipeline are PGA. But we 1136d93f8e55SVinod Koul * are interested in last PGA (leaf PGA) in a pipeline to disconnect with 1137d93f8e55SVinod Koul * the sink when it is running (two FE to one BE or one FE to two BE) 1138d93f8e55SVinod Koul * scenarios 1139d93f8e55SVinod Koul */ 1140d93f8e55SVinod Koul static int skl_tplg_pga_event(struct snd_soc_dapm_widget *w, 1141d93f8e55SVinod Koul struct snd_kcontrol *k, int event) 1142d93f8e55SVinod Koul 1143d93f8e55SVinod Koul { 1144d93f8e55SVinod Koul struct snd_soc_dapm_context *dapm = w->dapm; 1145d93f8e55SVinod Koul struct skl *skl = get_skl_ctx(dapm->dev); 1146d93f8e55SVinod Koul 1147d93f8e55SVinod Koul switch (event) { 1148d93f8e55SVinod Koul case SND_SOC_DAPM_PRE_PMU: 1149d93f8e55SVinod Koul return skl_tplg_pga_dapm_pre_pmu_event(w, skl); 1150d93f8e55SVinod Koul 1151d93f8e55SVinod Koul case SND_SOC_DAPM_POST_PMD: 1152d93f8e55SVinod Koul return skl_tplg_pga_dapm_post_pmd_event(w, skl); 1153d93f8e55SVinod Koul } 1154d93f8e55SVinod Koul 1155d93f8e55SVinod Koul return 0; 1156d93f8e55SVinod Koul } 1157cfb0a873SVinod Koul 1158140adfbaSJeeja KP static int skl_tplg_tlv_control_get(struct snd_kcontrol *kcontrol, 1159140adfbaSJeeja KP unsigned int __user *data, unsigned int size) 1160140adfbaSJeeja KP { 1161140adfbaSJeeja KP struct soc_bytes_ext *sb = 1162140adfbaSJeeja KP (struct soc_bytes_ext *)kcontrol->private_value; 1163140adfbaSJeeja KP struct skl_algo_data *bc = (struct skl_algo_data *)sb->dobj.private; 11647d9f2911SOmair M Abdullah struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol); 11657d9f2911SOmair M Abdullah struct skl_module_cfg *mconfig = w->priv; 11667d9f2911SOmair M Abdullah struct skl *skl = get_skl_ctx(w->dapm->dev); 11677d9f2911SOmair M Abdullah 11687d9f2911SOmair M Abdullah if (w->power) 11697d9f2911SOmair M Abdullah skl_get_module_params(skl->skl_sst, (u32 *)bc->params, 11700d682104SDharageswari R bc->size, bc->param_id, mconfig); 1171140adfbaSJeeja KP 117241556f68SVinod Koul /* decrement size for TLV header */ 117341556f68SVinod Koul size -= 2 * sizeof(u32); 117441556f68SVinod Koul 117541556f68SVinod Koul /* check size as we don't want to send kernel data */ 117641556f68SVinod Koul if (size > bc->max) 117741556f68SVinod Koul size = bc->max; 117841556f68SVinod Koul 1179140adfbaSJeeja KP if (bc->params) { 1180140adfbaSJeeja KP if (copy_to_user(data, &bc->param_id, sizeof(u32))) 1181140adfbaSJeeja KP return -EFAULT; 1182e8bc3c99SDan Carpenter if (copy_to_user(data + 1, &size, sizeof(u32))) 1183140adfbaSJeeja KP return -EFAULT; 1184e8bc3c99SDan Carpenter if (copy_to_user(data + 2, bc->params, size)) 1185140adfbaSJeeja KP return -EFAULT; 1186140adfbaSJeeja KP } 1187140adfbaSJeeja KP 1188140adfbaSJeeja KP return 0; 1189140adfbaSJeeja KP } 1190140adfbaSJeeja KP 1191140adfbaSJeeja KP #define SKL_PARAM_VENDOR_ID 0xff 1192140adfbaSJeeja KP 1193140adfbaSJeeja KP static int skl_tplg_tlv_control_set(struct snd_kcontrol *kcontrol, 1194140adfbaSJeeja KP const unsigned int __user *data, unsigned int size) 1195140adfbaSJeeja KP { 1196140adfbaSJeeja KP struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol); 1197140adfbaSJeeja KP struct skl_module_cfg *mconfig = w->priv; 1198140adfbaSJeeja KP struct soc_bytes_ext *sb = 1199140adfbaSJeeja KP (struct soc_bytes_ext *)kcontrol->private_value; 1200140adfbaSJeeja KP struct skl_algo_data *ac = (struct skl_algo_data *)sb->dobj.private; 1201140adfbaSJeeja KP struct skl *skl = get_skl_ctx(w->dapm->dev); 1202140adfbaSJeeja KP 1203140adfbaSJeeja KP if (ac->params) { 12040d682104SDharageswari R if (size > ac->max) 12050d682104SDharageswari R return -EINVAL; 12060d682104SDharageswari R 12070d682104SDharageswari R ac->size = size; 1208140adfbaSJeeja KP /* 1209140adfbaSJeeja KP * if the param_is is of type Vendor, firmware expects actual 1210140adfbaSJeeja KP * parameter id and size from the control. 1211140adfbaSJeeja KP */ 1212140adfbaSJeeja KP if (ac->param_id == SKL_PARAM_VENDOR_ID) { 1213140adfbaSJeeja KP if (copy_from_user(ac->params, data, size)) 1214140adfbaSJeeja KP return -EFAULT; 1215140adfbaSJeeja KP } else { 1216140adfbaSJeeja KP if (copy_from_user(ac->params, 121765b4bcb8SAlan data + 2, size)) 1218140adfbaSJeeja KP return -EFAULT; 1219140adfbaSJeeja KP } 1220140adfbaSJeeja KP 1221140adfbaSJeeja KP if (w->power) 1222140adfbaSJeeja KP return skl_set_module_params(skl->skl_sst, 12230d682104SDharageswari R (u32 *)ac->params, ac->size, 1224140adfbaSJeeja KP ac->param_id, mconfig); 1225140adfbaSJeeja KP } 1226140adfbaSJeeja KP 1227140adfbaSJeeja KP return 0; 1228140adfbaSJeeja KP } 1229140adfbaSJeeja KP 1230cfb0a873SVinod Koul /* 12318871dcb9SJeeja KP * Fill the dma id for host and link. In case of passthrough 12328871dcb9SJeeja KP * pipeline, this will both host and link in the same 12338871dcb9SJeeja KP * pipeline, so need to copy the link and host based on dev_type 12348871dcb9SJeeja KP */ 12358871dcb9SJeeja KP static void skl_tplg_fill_dma_id(struct skl_module_cfg *mcfg, 12368871dcb9SJeeja KP struct skl_pipe_params *params) 12378871dcb9SJeeja KP { 12388871dcb9SJeeja KP struct skl_pipe *pipe = mcfg->pipe; 12398871dcb9SJeeja KP 12408871dcb9SJeeja KP if (pipe->passthru) { 12418871dcb9SJeeja KP switch (mcfg->dev_type) { 12428871dcb9SJeeja KP case SKL_DEVICE_HDALINK: 12438871dcb9SJeeja KP pipe->p_params->link_dma_id = params->link_dma_id; 124412c3be0eSJeeja KP pipe->p_params->link_index = params->link_index; 12458871dcb9SJeeja KP break; 12468871dcb9SJeeja KP 12478871dcb9SJeeja KP case SKL_DEVICE_HDAHOST: 12488871dcb9SJeeja KP pipe->p_params->host_dma_id = params->host_dma_id; 12498871dcb9SJeeja KP break; 12508871dcb9SJeeja KP 12518871dcb9SJeeja KP default: 12528871dcb9SJeeja KP break; 12538871dcb9SJeeja KP } 12548871dcb9SJeeja KP pipe->p_params->s_fmt = params->s_fmt; 12558871dcb9SJeeja KP pipe->p_params->ch = params->ch; 12568871dcb9SJeeja KP pipe->p_params->s_freq = params->s_freq; 12578871dcb9SJeeja KP pipe->p_params->stream = params->stream; 125812c3be0eSJeeja KP pipe->p_params->format = params->format; 12598871dcb9SJeeja KP 12608871dcb9SJeeja KP } else { 12618871dcb9SJeeja KP memcpy(pipe->p_params, params, sizeof(*params)); 12628871dcb9SJeeja KP } 12638871dcb9SJeeja KP } 12648871dcb9SJeeja KP 12658871dcb9SJeeja KP /* 1266cfb0a873SVinod Koul * The FE params are passed by hw_params of the DAI. 1267cfb0a873SVinod Koul * On hw_params, the params are stored in Gateway module of the FE and we 1268cfb0a873SVinod Koul * need to calculate the format in DSP module configuration, that 1269cfb0a873SVinod Koul * conversion is done here 1270cfb0a873SVinod Koul */ 1271cfb0a873SVinod Koul int skl_tplg_update_pipe_params(struct device *dev, 1272cfb0a873SVinod Koul struct skl_module_cfg *mconfig, 1273cfb0a873SVinod Koul struct skl_pipe_params *params) 1274cfb0a873SVinod Koul { 1275cfb0a873SVinod Koul struct skl_module_fmt *format = NULL; 1276cfb0a873SVinod Koul 12778871dcb9SJeeja KP skl_tplg_fill_dma_id(mconfig, params); 1278cfb0a873SVinod Koul 1279cfb0a873SVinod Koul if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) 12804cd9899fSHardik T Shah format = &mconfig->in_fmt[0]; 1281cfb0a873SVinod Koul else 12824cd9899fSHardik T Shah format = &mconfig->out_fmt[0]; 1283cfb0a873SVinod Koul 1284cfb0a873SVinod Koul /* set the hw_params */ 1285cfb0a873SVinod Koul format->s_freq = params->s_freq; 1286cfb0a873SVinod Koul format->channels = params->ch; 1287cfb0a873SVinod Koul format->valid_bit_depth = skl_get_bit_depth(params->s_fmt); 1288cfb0a873SVinod Koul 1289cfb0a873SVinod Koul /* 1290cfb0a873SVinod Koul * 16 bit is 16 bit container whereas 24 bit is in 32 bit 1291cfb0a873SVinod Koul * container so update bit depth accordingly 1292cfb0a873SVinod Koul */ 1293cfb0a873SVinod Koul switch (format->valid_bit_depth) { 1294cfb0a873SVinod Koul case SKL_DEPTH_16BIT: 1295cfb0a873SVinod Koul format->bit_depth = format->valid_bit_depth; 1296cfb0a873SVinod Koul break; 1297cfb0a873SVinod Koul 1298cfb0a873SVinod Koul case SKL_DEPTH_24BIT: 12996654f39eSJeeja KP case SKL_DEPTH_32BIT: 1300cfb0a873SVinod Koul format->bit_depth = SKL_DEPTH_32BIT; 1301cfb0a873SVinod Koul break; 1302cfb0a873SVinod Koul 1303cfb0a873SVinod Koul default: 1304cfb0a873SVinod Koul dev_err(dev, "Invalid bit depth %x for pipe\n", 1305cfb0a873SVinod Koul format->valid_bit_depth); 1306cfb0a873SVinod Koul return -EINVAL; 1307cfb0a873SVinod Koul } 1308cfb0a873SVinod Koul 1309cfb0a873SVinod Koul if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) { 1310cfb0a873SVinod Koul mconfig->ibs = (format->s_freq / 1000) * 1311cfb0a873SVinod Koul (format->channels) * 1312cfb0a873SVinod Koul (format->bit_depth >> 3); 1313cfb0a873SVinod Koul } else { 1314cfb0a873SVinod Koul mconfig->obs = (format->s_freq / 1000) * 1315cfb0a873SVinod Koul (format->channels) * 1316cfb0a873SVinod Koul (format->bit_depth >> 3); 1317cfb0a873SVinod Koul } 1318cfb0a873SVinod Koul 1319cfb0a873SVinod Koul return 0; 1320cfb0a873SVinod Koul } 1321cfb0a873SVinod Koul 1322cfb0a873SVinod Koul /* 1323cfb0a873SVinod Koul * Query the module config for the FE DAI 1324cfb0a873SVinod Koul * This is used to find the hw_params set for that DAI and apply to FE 1325cfb0a873SVinod Koul * pipeline 1326cfb0a873SVinod Koul */ 1327cfb0a873SVinod Koul struct skl_module_cfg * 1328cfb0a873SVinod Koul skl_tplg_fe_get_cpr_module(struct snd_soc_dai *dai, int stream) 1329cfb0a873SVinod Koul { 1330cfb0a873SVinod Koul struct snd_soc_dapm_widget *w; 1331cfb0a873SVinod Koul struct snd_soc_dapm_path *p = NULL; 1332cfb0a873SVinod Koul 1333cfb0a873SVinod Koul if (stream == SNDRV_PCM_STREAM_PLAYBACK) { 1334cfb0a873SVinod Koul w = dai->playback_widget; 1335f0900eb2SSubhransu S. Prusty snd_soc_dapm_widget_for_each_sink_path(w, p) { 1336cfb0a873SVinod Koul if (p->connect && p->sink->power && 1337a28f51dbSJeeja KP !is_skl_dsp_widget_type(p->sink)) 1338cfb0a873SVinod Koul continue; 1339cfb0a873SVinod Koul 1340cfb0a873SVinod Koul if (p->sink->priv) { 1341cfb0a873SVinod Koul dev_dbg(dai->dev, "set params for %s\n", 1342cfb0a873SVinod Koul p->sink->name); 1343cfb0a873SVinod Koul return p->sink->priv; 1344cfb0a873SVinod Koul } 1345cfb0a873SVinod Koul } 1346cfb0a873SVinod Koul } else { 1347cfb0a873SVinod Koul w = dai->capture_widget; 1348f0900eb2SSubhransu S. Prusty snd_soc_dapm_widget_for_each_source_path(w, p) { 1349cfb0a873SVinod Koul if (p->connect && p->source->power && 1350a28f51dbSJeeja KP !is_skl_dsp_widget_type(p->source)) 1351cfb0a873SVinod Koul continue; 1352cfb0a873SVinod Koul 1353cfb0a873SVinod Koul if (p->source->priv) { 1354cfb0a873SVinod Koul dev_dbg(dai->dev, "set params for %s\n", 1355cfb0a873SVinod Koul p->source->name); 1356cfb0a873SVinod Koul return p->source->priv; 1357cfb0a873SVinod Koul } 1358cfb0a873SVinod Koul } 1359cfb0a873SVinod Koul } 1360cfb0a873SVinod Koul 1361cfb0a873SVinod Koul return NULL; 1362cfb0a873SVinod Koul } 1363cfb0a873SVinod Koul 1364718a42b5SDharageswari.R static struct skl_module_cfg *skl_get_mconfig_pb_cpr( 1365718a42b5SDharageswari.R struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w) 1366718a42b5SDharageswari.R { 1367718a42b5SDharageswari.R struct snd_soc_dapm_path *p; 1368718a42b5SDharageswari.R struct skl_module_cfg *mconfig = NULL; 1369718a42b5SDharageswari.R 1370718a42b5SDharageswari.R snd_soc_dapm_widget_for_each_source_path(w, p) { 1371718a42b5SDharageswari.R if (w->endpoints[SND_SOC_DAPM_DIR_OUT] > 0) { 1372718a42b5SDharageswari.R if (p->connect && 1373718a42b5SDharageswari.R (p->sink->id == snd_soc_dapm_aif_out) && 1374718a42b5SDharageswari.R p->source->priv) { 1375718a42b5SDharageswari.R mconfig = p->source->priv; 1376718a42b5SDharageswari.R return mconfig; 1377718a42b5SDharageswari.R } 1378718a42b5SDharageswari.R mconfig = skl_get_mconfig_pb_cpr(dai, p->source); 1379718a42b5SDharageswari.R if (mconfig) 1380718a42b5SDharageswari.R return mconfig; 1381718a42b5SDharageswari.R } 1382718a42b5SDharageswari.R } 1383718a42b5SDharageswari.R return mconfig; 1384718a42b5SDharageswari.R } 1385718a42b5SDharageswari.R 1386718a42b5SDharageswari.R static struct skl_module_cfg *skl_get_mconfig_cap_cpr( 1387718a42b5SDharageswari.R struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w) 1388718a42b5SDharageswari.R { 1389718a42b5SDharageswari.R struct snd_soc_dapm_path *p; 1390718a42b5SDharageswari.R struct skl_module_cfg *mconfig = NULL; 1391718a42b5SDharageswari.R 1392718a42b5SDharageswari.R snd_soc_dapm_widget_for_each_sink_path(w, p) { 1393718a42b5SDharageswari.R if (w->endpoints[SND_SOC_DAPM_DIR_IN] > 0) { 1394718a42b5SDharageswari.R if (p->connect && 1395718a42b5SDharageswari.R (p->source->id == snd_soc_dapm_aif_in) && 1396718a42b5SDharageswari.R p->sink->priv) { 1397718a42b5SDharageswari.R mconfig = p->sink->priv; 1398718a42b5SDharageswari.R return mconfig; 1399718a42b5SDharageswari.R } 1400718a42b5SDharageswari.R mconfig = skl_get_mconfig_cap_cpr(dai, p->sink); 1401718a42b5SDharageswari.R if (mconfig) 1402718a42b5SDharageswari.R return mconfig; 1403718a42b5SDharageswari.R } 1404718a42b5SDharageswari.R } 1405718a42b5SDharageswari.R return mconfig; 1406718a42b5SDharageswari.R } 1407718a42b5SDharageswari.R 1408718a42b5SDharageswari.R struct skl_module_cfg * 1409718a42b5SDharageswari.R skl_tplg_be_get_cpr_module(struct snd_soc_dai *dai, int stream) 1410718a42b5SDharageswari.R { 1411718a42b5SDharageswari.R struct snd_soc_dapm_widget *w; 1412718a42b5SDharageswari.R struct skl_module_cfg *mconfig; 1413718a42b5SDharageswari.R 1414718a42b5SDharageswari.R if (stream == SNDRV_PCM_STREAM_PLAYBACK) { 1415718a42b5SDharageswari.R w = dai->playback_widget; 1416718a42b5SDharageswari.R mconfig = skl_get_mconfig_pb_cpr(dai, w); 1417718a42b5SDharageswari.R } else { 1418718a42b5SDharageswari.R w = dai->capture_widget; 1419718a42b5SDharageswari.R mconfig = skl_get_mconfig_cap_cpr(dai, w); 1420718a42b5SDharageswari.R } 1421718a42b5SDharageswari.R return mconfig; 1422718a42b5SDharageswari.R } 1423718a42b5SDharageswari.R 1424cfb0a873SVinod Koul static u8 skl_tplg_be_link_type(int dev_type) 1425cfb0a873SVinod Koul { 1426cfb0a873SVinod Koul int ret; 1427cfb0a873SVinod Koul 1428cfb0a873SVinod Koul switch (dev_type) { 1429cfb0a873SVinod Koul case SKL_DEVICE_BT: 1430cfb0a873SVinod Koul ret = NHLT_LINK_SSP; 1431cfb0a873SVinod Koul break; 1432cfb0a873SVinod Koul 1433cfb0a873SVinod Koul case SKL_DEVICE_DMIC: 1434cfb0a873SVinod Koul ret = NHLT_LINK_DMIC; 1435cfb0a873SVinod Koul break; 1436cfb0a873SVinod Koul 1437cfb0a873SVinod Koul case SKL_DEVICE_I2S: 1438cfb0a873SVinod Koul ret = NHLT_LINK_SSP; 1439cfb0a873SVinod Koul break; 1440cfb0a873SVinod Koul 1441cfb0a873SVinod Koul case SKL_DEVICE_HDALINK: 1442cfb0a873SVinod Koul ret = NHLT_LINK_HDA; 1443cfb0a873SVinod Koul break; 1444cfb0a873SVinod Koul 1445cfb0a873SVinod Koul default: 1446cfb0a873SVinod Koul ret = NHLT_LINK_INVALID; 1447cfb0a873SVinod Koul break; 1448cfb0a873SVinod Koul } 1449cfb0a873SVinod Koul 1450cfb0a873SVinod Koul return ret; 1451cfb0a873SVinod Koul } 1452cfb0a873SVinod Koul 1453cfb0a873SVinod Koul /* 1454cfb0a873SVinod Koul * Fill the BE gateway parameters 1455cfb0a873SVinod Koul * The BE gateway expects a blob of parameters which are kept in the ACPI 1456cfb0a873SVinod Koul * NHLT blob, so query the blob for interface type (i2s/pdm) and instance. 1457cfb0a873SVinod Koul * The port can have multiple settings so pick based on the PCM 1458cfb0a873SVinod Koul * parameters 1459cfb0a873SVinod Koul */ 1460cfb0a873SVinod Koul static int skl_tplg_be_fill_pipe_params(struct snd_soc_dai *dai, 1461cfb0a873SVinod Koul struct skl_module_cfg *mconfig, 1462cfb0a873SVinod Koul struct skl_pipe_params *params) 1463cfb0a873SVinod Koul { 1464cfb0a873SVinod Koul struct nhlt_specific_cfg *cfg; 1465cfb0a873SVinod Koul struct skl *skl = get_skl_ctx(dai->dev); 1466cfb0a873SVinod Koul int link_type = skl_tplg_be_link_type(mconfig->dev_type); 1467db2f586bSSenthilnathan Veppur u8 dev_type = skl_tplg_be_dev_type(mconfig->dev_type); 1468cfb0a873SVinod Koul 14698871dcb9SJeeja KP skl_tplg_fill_dma_id(mconfig, params); 1470cfb0a873SVinod Koul 1471b30c275eSJeeja KP if (link_type == NHLT_LINK_HDA) 1472b30c275eSJeeja KP return 0; 1473b30c275eSJeeja KP 1474cfb0a873SVinod Koul /* update the blob based on virtual bus_id*/ 1475cfb0a873SVinod Koul cfg = skl_get_ep_blob(skl, mconfig->vbus_id, link_type, 1476cfb0a873SVinod Koul params->s_fmt, params->ch, 1477db2f586bSSenthilnathan Veppur params->s_freq, params->stream, 1478db2f586bSSenthilnathan Veppur dev_type); 1479cfb0a873SVinod Koul if (cfg) { 1480cfb0a873SVinod Koul mconfig->formats_config.caps_size = cfg->size; 1481bc03281aSJeeja KP mconfig->formats_config.caps = (u32 *) &cfg->caps; 1482cfb0a873SVinod Koul } else { 1483cfb0a873SVinod Koul dev_err(dai->dev, "Blob NULL for id %x type %d dirn %d\n", 1484cfb0a873SVinod Koul mconfig->vbus_id, link_type, 1485cfb0a873SVinod Koul params->stream); 1486cfb0a873SVinod Koul dev_err(dai->dev, "PCM: ch %d, freq %d, fmt %d\n", 1487cfb0a873SVinod Koul params->ch, params->s_freq, params->s_fmt); 1488cfb0a873SVinod Koul return -EINVAL; 1489cfb0a873SVinod Koul } 1490cfb0a873SVinod Koul 1491cfb0a873SVinod Koul return 0; 1492cfb0a873SVinod Koul } 1493cfb0a873SVinod Koul 1494cfb0a873SVinod Koul static int skl_tplg_be_set_src_pipe_params(struct snd_soc_dai *dai, 1495cfb0a873SVinod Koul struct snd_soc_dapm_widget *w, 1496cfb0a873SVinod Koul struct skl_pipe_params *params) 1497cfb0a873SVinod Koul { 1498cfb0a873SVinod Koul struct snd_soc_dapm_path *p; 14994d8adccbSSubhransu S. Prusty int ret = -EIO; 1500cfb0a873SVinod Koul 1501f0900eb2SSubhransu S. Prusty snd_soc_dapm_widget_for_each_source_path(w, p) { 1502cfb0a873SVinod Koul if (p->connect && is_skl_dsp_widget_type(p->source) && 1503cfb0a873SVinod Koul p->source->priv) { 1504cfb0a873SVinod Koul 15059a03cb49SJeeja KP ret = skl_tplg_be_fill_pipe_params(dai, 15069a03cb49SJeeja KP p->source->priv, params); 15074d8adccbSSubhransu S. Prusty if (ret < 0) 15084d8adccbSSubhransu S. Prusty return ret; 1509cfb0a873SVinod Koul } else { 15109a03cb49SJeeja KP ret = skl_tplg_be_set_src_pipe_params(dai, 15119a03cb49SJeeja KP p->source, params); 15124d8adccbSSubhransu S. Prusty if (ret < 0) 15134d8adccbSSubhransu S. Prusty return ret; 1514cfb0a873SVinod Koul } 1515cfb0a873SVinod Koul } 1516cfb0a873SVinod Koul 15174d8adccbSSubhransu S. Prusty return ret; 1518cfb0a873SVinod Koul } 1519cfb0a873SVinod Koul 1520cfb0a873SVinod Koul static int skl_tplg_be_set_sink_pipe_params(struct snd_soc_dai *dai, 1521cfb0a873SVinod Koul struct snd_soc_dapm_widget *w, struct skl_pipe_params *params) 1522cfb0a873SVinod Koul { 1523cfb0a873SVinod Koul struct snd_soc_dapm_path *p = NULL; 15244d8adccbSSubhransu S. Prusty int ret = -EIO; 1525cfb0a873SVinod Koul 1526f0900eb2SSubhransu S. Prusty snd_soc_dapm_widget_for_each_sink_path(w, p) { 1527cfb0a873SVinod Koul if (p->connect && is_skl_dsp_widget_type(p->sink) && 1528cfb0a873SVinod Koul p->sink->priv) { 1529cfb0a873SVinod Koul 15309a03cb49SJeeja KP ret = skl_tplg_be_fill_pipe_params(dai, 15319a03cb49SJeeja KP p->sink->priv, params); 15324d8adccbSSubhransu S. Prusty if (ret < 0) 15334d8adccbSSubhransu S. Prusty return ret; 15344d8adccbSSubhransu S. Prusty } else { 15354d8adccbSSubhransu S. Prusty ret = skl_tplg_be_set_sink_pipe_params( 1536cfb0a873SVinod Koul dai, p->sink, params); 15374d8adccbSSubhransu S. Prusty if (ret < 0) 15384d8adccbSSubhransu S. Prusty return ret; 1539cfb0a873SVinod Koul } 1540cfb0a873SVinod Koul } 1541cfb0a873SVinod Koul 15424d8adccbSSubhransu S. Prusty return ret; 1543cfb0a873SVinod Koul } 1544cfb0a873SVinod Koul 1545cfb0a873SVinod Koul /* 1546cfb0a873SVinod Koul * BE hw_params can be a source parameters (capture) or sink parameters 1547cfb0a873SVinod Koul * (playback). Based on sink and source we need to either find the source 1548cfb0a873SVinod Koul * list or the sink list and set the pipeline parameters 1549cfb0a873SVinod Koul */ 1550cfb0a873SVinod Koul int skl_tplg_be_update_params(struct snd_soc_dai *dai, 1551cfb0a873SVinod Koul struct skl_pipe_params *params) 1552cfb0a873SVinod Koul { 1553cfb0a873SVinod Koul struct snd_soc_dapm_widget *w; 1554cfb0a873SVinod Koul 1555cfb0a873SVinod Koul if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) { 1556cfb0a873SVinod Koul w = dai->playback_widget; 1557cfb0a873SVinod Koul 1558cfb0a873SVinod Koul return skl_tplg_be_set_src_pipe_params(dai, w, params); 1559cfb0a873SVinod Koul 1560cfb0a873SVinod Koul } else { 1561cfb0a873SVinod Koul w = dai->capture_widget; 1562cfb0a873SVinod Koul 1563cfb0a873SVinod Koul return skl_tplg_be_set_sink_pipe_params(dai, w, params); 1564cfb0a873SVinod Koul } 1565cfb0a873SVinod Koul 1566cfb0a873SVinod Koul return 0; 1567cfb0a873SVinod Koul } 15683af36706SVinod Koul 15693af36706SVinod Koul static const struct snd_soc_tplg_widget_events skl_tplg_widget_ops[] = { 15703af36706SVinod Koul {SKL_MIXER_EVENT, skl_tplg_mixer_event}, 15713af36706SVinod Koul {SKL_VMIXER_EVENT, skl_tplg_vmixer_event}, 15723af36706SVinod Koul {SKL_PGA_EVENT, skl_tplg_pga_event}, 15733af36706SVinod Koul }; 15743af36706SVinod Koul 1575140adfbaSJeeja KP static const struct snd_soc_tplg_bytes_ext_ops skl_tlv_ops[] = { 1576140adfbaSJeeja KP {SKL_CONTROL_TYPE_BYTE_TLV, skl_tplg_tlv_control_get, 1577140adfbaSJeeja KP skl_tplg_tlv_control_set}, 1578140adfbaSJeeja KP }; 1579140adfbaSJeeja KP 15806277e832SShreyas NC static int skl_tplg_fill_pipe_tkn(struct device *dev, 15816277e832SShreyas NC struct skl_pipe *pipe, u32 tkn, 15826277e832SShreyas NC u32 tkn_val) 15833af36706SVinod Koul { 15843af36706SVinod Koul 15856277e832SShreyas NC switch (tkn) { 15866277e832SShreyas NC case SKL_TKN_U32_PIPE_CONN_TYPE: 15876277e832SShreyas NC pipe->conn_type = tkn_val; 15886277e832SShreyas NC break; 15896277e832SShreyas NC 15906277e832SShreyas NC case SKL_TKN_U32_PIPE_PRIORITY: 15916277e832SShreyas NC pipe->pipe_priority = tkn_val; 15926277e832SShreyas NC break; 15936277e832SShreyas NC 15946277e832SShreyas NC case SKL_TKN_U32_PIPE_MEM_PGS: 15956277e832SShreyas NC pipe->memory_pages = tkn_val; 15966277e832SShreyas NC break; 15976277e832SShreyas NC 15988a0cb236SVinod Koul case SKL_TKN_U32_PMODE: 15998a0cb236SVinod Koul pipe->lp_mode = tkn_val; 16008a0cb236SVinod Koul break; 16018a0cb236SVinod Koul 16026277e832SShreyas NC default: 16036277e832SShreyas NC dev_err(dev, "Token not handled %d\n", tkn); 16046277e832SShreyas NC return -EINVAL; 16053af36706SVinod Koul } 16066277e832SShreyas NC 16076277e832SShreyas NC return 0; 16083af36706SVinod Koul } 16093af36706SVinod Koul 16103af36706SVinod Koul /* 16116277e832SShreyas NC * Add pipeline by parsing the relevant tokens 16126277e832SShreyas NC * Return an existing pipe if the pipe already exists. 16133af36706SVinod Koul */ 16146277e832SShreyas NC static int skl_tplg_add_pipe(struct device *dev, 16156277e832SShreyas NC struct skl_module_cfg *mconfig, struct skl *skl, 16166277e832SShreyas NC struct snd_soc_tplg_vendor_value_elem *tkn_elem) 16173af36706SVinod Koul { 16183af36706SVinod Koul struct skl_pipeline *ppl; 16193af36706SVinod Koul struct skl_pipe *pipe; 16203af36706SVinod Koul struct skl_pipe_params *params; 16213af36706SVinod Koul 16223af36706SVinod Koul list_for_each_entry(ppl, &skl->ppl_list, node) { 16236277e832SShreyas NC if (ppl->pipe->ppl_id == tkn_elem->value) { 16246277e832SShreyas NC mconfig->pipe = ppl->pipe; 16256277e832SShreyas NC return EEXIST; 16266277e832SShreyas NC } 16273af36706SVinod Koul } 16283af36706SVinod Koul 16293af36706SVinod Koul ppl = devm_kzalloc(dev, sizeof(*ppl), GFP_KERNEL); 16303af36706SVinod Koul if (!ppl) 16316277e832SShreyas NC return -ENOMEM; 16323af36706SVinod Koul 16333af36706SVinod Koul pipe = devm_kzalloc(dev, sizeof(*pipe), GFP_KERNEL); 16343af36706SVinod Koul if (!pipe) 16356277e832SShreyas NC return -ENOMEM; 16363af36706SVinod Koul 16373af36706SVinod Koul params = devm_kzalloc(dev, sizeof(*params), GFP_KERNEL); 16383af36706SVinod Koul if (!params) 16396277e832SShreyas NC return -ENOMEM; 16403af36706SVinod Koul 16413af36706SVinod Koul pipe->p_params = params; 16426277e832SShreyas NC pipe->ppl_id = tkn_elem->value; 16433af36706SVinod Koul INIT_LIST_HEAD(&pipe->w_list); 16443af36706SVinod Koul 16453af36706SVinod Koul ppl->pipe = pipe; 16463af36706SVinod Koul list_add(&ppl->node, &skl->ppl_list); 16473af36706SVinod Koul 16486277e832SShreyas NC mconfig->pipe = pipe; 16496277e832SShreyas NC mconfig->pipe->state = SKL_PIPE_INVALID; 16506277e832SShreyas NC 16516277e832SShreyas NC return 0; 16523af36706SVinod Koul } 16533af36706SVinod Koul 16546277e832SShreyas NC static int skl_tplg_fill_pin(struct device *dev, u32 tkn, 16556277e832SShreyas NC struct skl_module_pin *m_pin, 16566277e832SShreyas NC int pin_index, u32 value) 16576277e832SShreyas NC { 16586277e832SShreyas NC switch (tkn) { 16596277e832SShreyas NC case SKL_TKN_U32_PIN_MOD_ID: 16606277e832SShreyas NC m_pin[pin_index].id.module_id = value; 16616277e832SShreyas NC break; 16626277e832SShreyas NC 16636277e832SShreyas NC case SKL_TKN_U32_PIN_INST_ID: 16646277e832SShreyas NC m_pin[pin_index].id.instance_id = value; 16656277e832SShreyas NC break; 16666277e832SShreyas NC 16676277e832SShreyas NC default: 16686277e832SShreyas NC dev_err(dev, "%d Not a pin token\n", value); 16696277e832SShreyas NC return -EINVAL; 16706277e832SShreyas NC } 16716277e832SShreyas NC 16726277e832SShreyas NC return 0; 16736277e832SShreyas NC } 16746277e832SShreyas NC 16756277e832SShreyas NC /* 16766277e832SShreyas NC * Parse for pin config specific tokens to fill up the 16776277e832SShreyas NC * module private data 16786277e832SShreyas NC */ 16796277e832SShreyas NC static int skl_tplg_fill_pins_info(struct device *dev, 16806277e832SShreyas NC struct skl_module_cfg *mconfig, 16816277e832SShreyas NC struct snd_soc_tplg_vendor_value_elem *tkn_elem, 16826277e832SShreyas NC int dir, int pin_count) 16836277e832SShreyas NC { 16846277e832SShreyas NC int ret; 16856277e832SShreyas NC struct skl_module_pin *m_pin; 16866277e832SShreyas NC 16876277e832SShreyas NC switch (dir) { 16886277e832SShreyas NC case SKL_DIR_IN: 16896277e832SShreyas NC m_pin = mconfig->m_in_pin; 16906277e832SShreyas NC break; 16916277e832SShreyas NC 16926277e832SShreyas NC case SKL_DIR_OUT: 16936277e832SShreyas NC m_pin = mconfig->m_out_pin; 16946277e832SShreyas NC break; 16956277e832SShreyas NC 16966277e832SShreyas NC default: 1697ecd286a9SColin Ian King dev_err(dev, "Invalid direction value\n"); 16986277e832SShreyas NC return -EINVAL; 16996277e832SShreyas NC } 17006277e832SShreyas NC 17016277e832SShreyas NC ret = skl_tplg_fill_pin(dev, tkn_elem->token, 17026277e832SShreyas NC m_pin, pin_count, tkn_elem->value); 17036277e832SShreyas NC 17046277e832SShreyas NC if (ret < 0) 17056277e832SShreyas NC return ret; 17066277e832SShreyas NC 17076277e832SShreyas NC m_pin[pin_count].in_use = false; 17086277e832SShreyas NC m_pin[pin_count].pin_state = SKL_PIN_UNBIND; 17096277e832SShreyas NC 17106277e832SShreyas NC return 0; 17116277e832SShreyas NC } 17126277e832SShreyas NC 17136277e832SShreyas NC /* 17146277e832SShreyas NC * Fill up input/output module config format based 17156277e832SShreyas NC * on the direction 17166277e832SShreyas NC */ 17176277e832SShreyas NC static int skl_tplg_fill_fmt(struct device *dev, 17186277e832SShreyas NC struct skl_module_cfg *mconfig, u32 tkn, 17196277e832SShreyas NC u32 value, u32 dir, u32 pin_count) 17206277e832SShreyas NC { 17216277e832SShreyas NC struct skl_module_fmt *dst_fmt; 17226277e832SShreyas NC 17236277e832SShreyas NC switch (dir) { 17246277e832SShreyas NC case SKL_DIR_IN: 17256277e832SShreyas NC dst_fmt = mconfig->in_fmt; 17266277e832SShreyas NC dst_fmt += pin_count; 17276277e832SShreyas NC break; 17286277e832SShreyas NC 17296277e832SShreyas NC case SKL_DIR_OUT: 17306277e832SShreyas NC dst_fmt = mconfig->out_fmt; 17316277e832SShreyas NC dst_fmt += pin_count; 17326277e832SShreyas NC break; 17336277e832SShreyas NC 17346277e832SShreyas NC default: 1735ecd286a9SColin Ian King dev_err(dev, "Invalid direction value\n"); 17366277e832SShreyas NC return -EINVAL; 17376277e832SShreyas NC } 17386277e832SShreyas NC 17396277e832SShreyas NC switch (tkn) { 17406277e832SShreyas NC case SKL_TKN_U32_FMT_CH: 17416277e832SShreyas NC dst_fmt->channels = value; 17426277e832SShreyas NC break; 17436277e832SShreyas NC 17446277e832SShreyas NC case SKL_TKN_U32_FMT_FREQ: 17456277e832SShreyas NC dst_fmt->s_freq = value; 17466277e832SShreyas NC break; 17476277e832SShreyas NC 17486277e832SShreyas NC case SKL_TKN_U32_FMT_BIT_DEPTH: 17496277e832SShreyas NC dst_fmt->bit_depth = value; 17506277e832SShreyas NC break; 17516277e832SShreyas NC 17526277e832SShreyas NC case SKL_TKN_U32_FMT_SAMPLE_SIZE: 17536277e832SShreyas NC dst_fmt->valid_bit_depth = value; 17546277e832SShreyas NC break; 17556277e832SShreyas NC 17566277e832SShreyas NC case SKL_TKN_U32_FMT_CH_CONFIG: 17576277e832SShreyas NC dst_fmt->ch_cfg = value; 17586277e832SShreyas NC break; 17596277e832SShreyas NC 17606277e832SShreyas NC case SKL_TKN_U32_FMT_INTERLEAVE: 17616277e832SShreyas NC dst_fmt->interleaving_style = value; 17626277e832SShreyas NC break; 17636277e832SShreyas NC 17646277e832SShreyas NC case SKL_TKN_U32_FMT_SAMPLE_TYPE: 17656277e832SShreyas NC dst_fmt->sample_type = value; 17666277e832SShreyas NC break; 17676277e832SShreyas NC 17686277e832SShreyas NC case SKL_TKN_U32_FMT_CH_MAP: 17696277e832SShreyas NC dst_fmt->ch_map = value; 17706277e832SShreyas NC break; 17716277e832SShreyas NC 17726277e832SShreyas NC default: 1773ecd286a9SColin Ian King dev_err(dev, "Invalid token %d\n", tkn); 17746277e832SShreyas NC return -EINVAL; 17756277e832SShreyas NC } 17766277e832SShreyas NC 17776277e832SShreyas NC return 0; 17786277e832SShreyas NC } 17796277e832SShreyas NC 17806277e832SShreyas NC static int skl_tplg_get_uuid(struct device *dev, struct skl_module_cfg *mconfig, 17816277e832SShreyas NC struct snd_soc_tplg_vendor_uuid_elem *uuid_tkn) 17826277e832SShreyas NC { 17836277e832SShreyas NC if (uuid_tkn->token == SKL_TKN_UUID) 17846277e832SShreyas NC memcpy(&mconfig->guid, &uuid_tkn->uuid, 16); 17856277e832SShreyas NC else { 1786ecd286a9SColin Ian King dev_err(dev, "Not an UUID token tkn %d\n", uuid_tkn->token); 17876277e832SShreyas NC return -EINVAL; 17886277e832SShreyas NC } 17896277e832SShreyas NC 17906277e832SShreyas NC return 0; 17916277e832SShreyas NC } 17926277e832SShreyas NC 17936277e832SShreyas NC static void skl_tplg_fill_pin_dynamic_val( 17946277e832SShreyas NC struct skl_module_pin *mpin, u32 pin_count, u32 value) 17954cd9899fSHardik T Shah { 17964cd9899fSHardik T Shah int i; 17974cd9899fSHardik T Shah 17986277e832SShreyas NC for (i = 0; i < pin_count; i++) 17996277e832SShreyas NC mpin[i].is_dynamic = value; 18004cd9899fSHardik T Shah } 18016277e832SShreyas NC 18026277e832SShreyas NC /* 18036277e832SShreyas NC * Parse tokens to fill up the module private data 18046277e832SShreyas NC */ 18056277e832SShreyas NC static int skl_tplg_get_token(struct device *dev, 18066277e832SShreyas NC struct snd_soc_tplg_vendor_value_elem *tkn_elem, 18076277e832SShreyas NC struct skl *skl, struct skl_module_cfg *mconfig) 18086277e832SShreyas NC { 18096277e832SShreyas NC int tkn_count = 0; 18106277e832SShreyas NC int ret; 18116277e832SShreyas NC static int is_pipe_exists; 18126277e832SShreyas NC static int pin_index, dir; 18136277e832SShreyas NC 18146277e832SShreyas NC if (tkn_elem->token > SKL_TKN_MAX) 18156277e832SShreyas NC return -EINVAL; 18166277e832SShreyas NC 18176277e832SShreyas NC switch (tkn_elem->token) { 18186277e832SShreyas NC case SKL_TKN_U8_IN_QUEUE_COUNT: 18196277e832SShreyas NC mconfig->max_in_queue = tkn_elem->value; 18206277e832SShreyas NC mconfig->m_in_pin = devm_kzalloc(dev, mconfig->max_in_queue * 18216277e832SShreyas NC sizeof(*mconfig->m_in_pin), 18226277e832SShreyas NC GFP_KERNEL); 18236277e832SShreyas NC if (!mconfig->m_in_pin) 18246277e832SShreyas NC return -ENOMEM; 18256277e832SShreyas NC 18266277e832SShreyas NC break; 18276277e832SShreyas NC 18286277e832SShreyas NC case SKL_TKN_U8_OUT_QUEUE_COUNT: 18296277e832SShreyas NC mconfig->max_out_queue = tkn_elem->value; 18306277e832SShreyas NC mconfig->m_out_pin = devm_kzalloc(dev, mconfig->max_out_queue * 18316277e832SShreyas NC sizeof(*mconfig->m_out_pin), 18326277e832SShreyas NC GFP_KERNEL); 18336277e832SShreyas NC 18346277e832SShreyas NC if (!mconfig->m_out_pin) 18356277e832SShreyas NC return -ENOMEM; 18366277e832SShreyas NC 18376277e832SShreyas NC break; 18386277e832SShreyas NC 18396277e832SShreyas NC case SKL_TKN_U8_DYN_IN_PIN: 18406277e832SShreyas NC if (!mconfig->m_in_pin) 18416277e832SShreyas NC return -ENOMEM; 18426277e832SShreyas NC 18436277e832SShreyas NC skl_tplg_fill_pin_dynamic_val(mconfig->m_in_pin, 18446277e832SShreyas NC mconfig->max_in_queue, tkn_elem->value); 18456277e832SShreyas NC 18466277e832SShreyas NC break; 18476277e832SShreyas NC 18486277e832SShreyas NC case SKL_TKN_U8_DYN_OUT_PIN: 18496277e832SShreyas NC if (!mconfig->m_out_pin) 18506277e832SShreyas NC return -ENOMEM; 18516277e832SShreyas NC 18526277e832SShreyas NC skl_tplg_fill_pin_dynamic_val(mconfig->m_out_pin, 18536277e832SShreyas NC mconfig->max_out_queue, tkn_elem->value); 18546277e832SShreyas NC 18556277e832SShreyas NC break; 18566277e832SShreyas NC 18576277e832SShreyas NC case SKL_TKN_U8_TIME_SLOT: 18586277e832SShreyas NC mconfig->time_slot = tkn_elem->value; 18596277e832SShreyas NC break; 18606277e832SShreyas NC 18616277e832SShreyas NC case SKL_TKN_U8_CORE_ID: 18626277e832SShreyas NC mconfig->core_id = tkn_elem->value; 18636277e832SShreyas NC 18646277e832SShreyas NC case SKL_TKN_U8_MOD_TYPE: 18656277e832SShreyas NC mconfig->m_type = tkn_elem->value; 18666277e832SShreyas NC break; 18676277e832SShreyas NC 18686277e832SShreyas NC case SKL_TKN_U8_DEV_TYPE: 18696277e832SShreyas NC mconfig->dev_type = tkn_elem->value; 18706277e832SShreyas NC break; 18716277e832SShreyas NC 18726277e832SShreyas NC case SKL_TKN_U8_HW_CONN_TYPE: 18736277e832SShreyas NC mconfig->hw_conn_type = tkn_elem->value; 18746277e832SShreyas NC break; 18756277e832SShreyas NC 18766277e832SShreyas NC case SKL_TKN_U16_MOD_INST_ID: 18776277e832SShreyas NC mconfig->id.instance_id = 18786277e832SShreyas NC tkn_elem->value; 18796277e832SShreyas NC break; 18806277e832SShreyas NC 18816277e832SShreyas NC case SKL_TKN_U32_MEM_PAGES: 18826277e832SShreyas NC mconfig->mem_pages = tkn_elem->value; 18836277e832SShreyas NC break; 18846277e832SShreyas NC 18856277e832SShreyas NC case SKL_TKN_U32_MAX_MCPS: 18866277e832SShreyas NC mconfig->mcps = tkn_elem->value; 18876277e832SShreyas NC break; 18886277e832SShreyas NC 18896277e832SShreyas NC case SKL_TKN_U32_OBS: 18906277e832SShreyas NC mconfig->obs = tkn_elem->value; 18916277e832SShreyas NC break; 18926277e832SShreyas NC 18936277e832SShreyas NC case SKL_TKN_U32_IBS: 18946277e832SShreyas NC mconfig->ibs = tkn_elem->value; 18956277e832SShreyas NC break; 18966277e832SShreyas NC 18976277e832SShreyas NC case SKL_TKN_U32_VBUS_ID: 18986277e832SShreyas NC mconfig->vbus_id = tkn_elem->value; 18996277e832SShreyas NC break; 19006277e832SShreyas NC 19016277e832SShreyas NC case SKL_TKN_U32_PARAMS_FIXUP: 19026277e832SShreyas NC mconfig->params_fixup = tkn_elem->value; 19036277e832SShreyas NC break; 19046277e832SShreyas NC 19056277e832SShreyas NC case SKL_TKN_U32_CONVERTER: 19066277e832SShreyas NC mconfig->converter = tkn_elem->value; 19076277e832SShreyas NC break; 19086277e832SShreyas NC 19096bd9dcf3SVinod Koul case SKL_TKL_U32_D0I3_CAPS: 19106bd9dcf3SVinod Koul mconfig->d0i3_caps = tkn_elem->value; 19116bd9dcf3SVinod Koul break; 19126bd9dcf3SVinod Koul 19136277e832SShreyas NC case SKL_TKN_U32_PIPE_ID: 19146277e832SShreyas NC ret = skl_tplg_add_pipe(dev, 19156277e832SShreyas NC mconfig, skl, tkn_elem); 19166277e832SShreyas NC 19176277e832SShreyas NC if (ret < 0) 19186277e832SShreyas NC return is_pipe_exists; 19196277e832SShreyas NC 19206277e832SShreyas NC if (ret == EEXIST) 19216277e832SShreyas NC is_pipe_exists = 1; 19226277e832SShreyas NC 19236277e832SShreyas NC break; 19246277e832SShreyas NC 19256277e832SShreyas NC case SKL_TKN_U32_PIPE_CONN_TYPE: 19266277e832SShreyas NC case SKL_TKN_U32_PIPE_PRIORITY: 19276277e832SShreyas NC case SKL_TKN_U32_PIPE_MEM_PGS: 19288a0cb236SVinod Koul case SKL_TKN_U32_PMODE: 19296277e832SShreyas NC if (is_pipe_exists) { 19306277e832SShreyas NC ret = skl_tplg_fill_pipe_tkn(dev, mconfig->pipe, 19316277e832SShreyas NC tkn_elem->token, tkn_elem->value); 19326277e832SShreyas NC if (ret < 0) 19336277e832SShreyas NC return ret; 19346277e832SShreyas NC } 19356277e832SShreyas NC 19366277e832SShreyas NC break; 19376277e832SShreyas NC 19386277e832SShreyas NC /* 19396277e832SShreyas NC * SKL_TKN_U32_DIR_PIN_COUNT token has the value for both 19406277e832SShreyas NC * direction and the pin count. The first four bits represent 19416277e832SShreyas NC * direction and next four the pin count. 19426277e832SShreyas NC */ 19436277e832SShreyas NC case SKL_TKN_U32_DIR_PIN_COUNT: 19446277e832SShreyas NC dir = tkn_elem->value & SKL_IN_DIR_BIT_MASK; 19456277e832SShreyas NC pin_index = (tkn_elem->value & 19466277e832SShreyas NC SKL_PIN_COUNT_MASK) >> 4; 19476277e832SShreyas NC 19486277e832SShreyas NC break; 19496277e832SShreyas NC 19506277e832SShreyas NC case SKL_TKN_U32_FMT_CH: 19516277e832SShreyas NC case SKL_TKN_U32_FMT_FREQ: 19526277e832SShreyas NC case SKL_TKN_U32_FMT_BIT_DEPTH: 19536277e832SShreyas NC case SKL_TKN_U32_FMT_SAMPLE_SIZE: 19546277e832SShreyas NC case SKL_TKN_U32_FMT_CH_CONFIG: 19556277e832SShreyas NC case SKL_TKN_U32_FMT_INTERLEAVE: 19566277e832SShreyas NC case SKL_TKN_U32_FMT_SAMPLE_TYPE: 19576277e832SShreyas NC case SKL_TKN_U32_FMT_CH_MAP: 19586277e832SShreyas NC ret = skl_tplg_fill_fmt(dev, mconfig, tkn_elem->token, 19596277e832SShreyas NC tkn_elem->value, dir, pin_index); 19606277e832SShreyas NC 19616277e832SShreyas NC if (ret < 0) 19626277e832SShreyas NC return ret; 19636277e832SShreyas NC 19646277e832SShreyas NC break; 19656277e832SShreyas NC 19666277e832SShreyas NC case SKL_TKN_U32_PIN_MOD_ID: 19676277e832SShreyas NC case SKL_TKN_U32_PIN_INST_ID: 19686277e832SShreyas NC ret = skl_tplg_fill_pins_info(dev, 19696277e832SShreyas NC mconfig, tkn_elem, dir, 19706277e832SShreyas NC pin_index); 19716277e832SShreyas NC if (ret < 0) 19726277e832SShreyas NC return ret; 19736277e832SShreyas NC 19746277e832SShreyas NC break; 19756277e832SShreyas NC 19766277e832SShreyas NC case SKL_TKN_U32_CAPS_SIZE: 19776277e832SShreyas NC mconfig->formats_config.caps_size = 19786277e832SShreyas NC tkn_elem->value; 19796277e832SShreyas NC 19806277e832SShreyas NC break; 19816277e832SShreyas NC 19826277e832SShreyas NC case SKL_TKN_U32_PROC_DOMAIN: 19836277e832SShreyas NC mconfig->domain = 19846277e832SShreyas NC tkn_elem->value; 19856277e832SShreyas NC 19866277e832SShreyas NC break; 19876277e832SShreyas NC 19886277e832SShreyas NC case SKL_TKN_U8_IN_PIN_TYPE: 19896277e832SShreyas NC case SKL_TKN_U8_OUT_PIN_TYPE: 19906277e832SShreyas NC case SKL_TKN_U8_CONN_TYPE: 19916277e832SShreyas NC break; 19926277e832SShreyas NC 19936277e832SShreyas NC default: 19946277e832SShreyas NC dev_err(dev, "Token %d not handled\n", 19956277e832SShreyas NC tkn_elem->token); 19966277e832SShreyas NC return -EINVAL; 19976277e832SShreyas NC } 19986277e832SShreyas NC 19996277e832SShreyas NC tkn_count++; 20006277e832SShreyas NC 20016277e832SShreyas NC return tkn_count; 20026277e832SShreyas NC } 20036277e832SShreyas NC 20046277e832SShreyas NC /* 20056277e832SShreyas NC * Parse the vendor array for specific tokens to construct 20066277e832SShreyas NC * module private data 20076277e832SShreyas NC */ 20086277e832SShreyas NC static int skl_tplg_get_tokens(struct device *dev, 20096277e832SShreyas NC char *pvt_data, struct skl *skl, 20106277e832SShreyas NC struct skl_module_cfg *mconfig, int block_size) 20116277e832SShreyas NC { 20126277e832SShreyas NC struct snd_soc_tplg_vendor_array *array; 20136277e832SShreyas NC struct snd_soc_tplg_vendor_value_elem *tkn_elem; 20146277e832SShreyas NC int tkn_count = 0, ret; 20156277e832SShreyas NC int off = 0, tuple_size = 0; 20166277e832SShreyas NC 20176277e832SShreyas NC if (block_size <= 0) 20186277e832SShreyas NC return -EINVAL; 20196277e832SShreyas NC 20206277e832SShreyas NC while (tuple_size < block_size) { 20216277e832SShreyas NC array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off); 20226277e832SShreyas NC 20236277e832SShreyas NC off += array->size; 20246277e832SShreyas NC 20256277e832SShreyas NC switch (array->type) { 20266277e832SShreyas NC case SND_SOC_TPLG_TUPLE_TYPE_STRING: 2027ecd286a9SColin Ian King dev_warn(dev, "no string tokens expected for skl tplg\n"); 20286277e832SShreyas NC continue; 20296277e832SShreyas NC 20306277e832SShreyas NC case SND_SOC_TPLG_TUPLE_TYPE_UUID: 20316277e832SShreyas NC ret = skl_tplg_get_uuid(dev, mconfig, array->uuid); 20326277e832SShreyas NC if (ret < 0) 20336277e832SShreyas NC return ret; 20346277e832SShreyas NC 20356277e832SShreyas NC tuple_size += sizeof(*array->uuid); 20366277e832SShreyas NC 20376277e832SShreyas NC continue; 20386277e832SShreyas NC 20396277e832SShreyas NC default: 20406277e832SShreyas NC tkn_elem = array->value; 20416277e832SShreyas NC tkn_count = 0; 20426277e832SShreyas NC break; 20436277e832SShreyas NC } 20446277e832SShreyas NC 20456277e832SShreyas NC while (tkn_count <= (array->num_elems - 1)) { 20466277e832SShreyas NC ret = skl_tplg_get_token(dev, tkn_elem, 20476277e832SShreyas NC skl, mconfig); 20486277e832SShreyas NC 20496277e832SShreyas NC if (ret < 0) 20506277e832SShreyas NC return ret; 20516277e832SShreyas NC 20526277e832SShreyas NC tkn_count = tkn_count + ret; 20536277e832SShreyas NC tkn_elem++; 20546277e832SShreyas NC } 20556277e832SShreyas NC 20566277e832SShreyas NC tuple_size += tkn_count * sizeof(*tkn_elem); 20576277e832SShreyas NC } 20586277e832SShreyas NC 20596277e832SShreyas NC return 0; 20606277e832SShreyas NC } 20616277e832SShreyas NC 20626277e832SShreyas NC /* 20636277e832SShreyas NC * Every data block is preceded by a descriptor to read the number 20646277e832SShreyas NC * of data blocks, they type of the block and it's size 20656277e832SShreyas NC */ 20666277e832SShreyas NC static int skl_tplg_get_desc_blocks(struct device *dev, 20676277e832SShreyas NC struct snd_soc_tplg_vendor_array *array) 20686277e832SShreyas NC { 20696277e832SShreyas NC struct snd_soc_tplg_vendor_value_elem *tkn_elem; 20706277e832SShreyas NC 20716277e832SShreyas NC tkn_elem = array->value; 20726277e832SShreyas NC 20736277e832SShreyas NC switch (tkn_elem->token) { 20746277e832SShreyas NC case SKL_TKN_U8_NUM_BLOCKS: 20756277e832SShreyas NC case SKL_TKN_U8_BLOCK_TYPE: 20766277e832SShreyas NC case SKL_TKN_U16_BLOCK_SIZE: 20776277e832SShreyas NC return tkn_elem->value; 20786277e832SShreyas NC 20796277e832SShreyas NC default: 2080ecd286a9SColin Ian King dev_err(dev, "Invalid descriptor token %d\n", tkn_elem->token); 20816277e832SShreyas NC break; 20826277e832SShreyas NC } 20836277e832SShreyas NC 20846277e832SShreyas NC return -EINVAL; 20856277e832SShreyas NC } 20866277e832SShreyas NC 20876277e832SShreyas NC /* 20886277e832SShreyas NC * Parse the private data for the token and corresponding value. 20896277e832SShreyas NC * The private data can have multiple data blocks. So, a data block 20906277e832SShreyas NC * is preceded by a descriptor for number of blocks and a descriptor 20916277e832SShreyas NC * for the type and size of the suceeding data block. 20926277e832SShreyas NC */ 20936277e832SShreyas NC static int skl_tplg_get_pvt_data(struct snd_soc_tplg_dapm_widget *tplg_w, 20946277e832SShreyas NC struct skl *skl, struct device *dev, 20956277e832SShreyas NC struct skl_module_cfg *mconfig) 20966277e832SShreyas NC { 20976277e832SShreyas NC struct snd_soc_tplg_vendor_array *array; 20986277e832SShreyas NC int num_blocks, block_size = 0, block_type, off = 0; 20996277e832SShreyas NC char *data; 21006277e832SShreyas NC int ret; 21016277e832SShreyas NC 21026277e832SShreyas NC /* Read the NUM_DATA_BLOCKS descriptor */ 21036277e832SShreyas NC array = (struct snd_soc_tplg_vendor_array *)tplg_w->priv.data; 21046277e832SShreyas NC ret = skl_tplg_get_desc_blocks(dev, array); 21056277e832SShreyas NC if (ret < 0) 21066277e832SShreyas NC return ret; 21076277e832SShreyas NC num_blocks = ret; 21086277e832SShreyas NC 21096277e832SShreyas NC off += array->size; 21106277e832SShreyas NC array = (struct snd_soc_tplg_vendor_array *)(tplg_w->priv.data + off); 21116277e832SShreyas NC 21126277e832SShreyas NC /* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */ 21136277e832SShreyas NC while (num_blocks > 0) { 21146277e832SShreyas NC ret = skl_tplg_get_desc_blocks(dev, array); 21156277e832SShreyas NC 21166277e832SShreyas NC if (ret < 0) 21176277e832SShreyas NC return ret; 21186277e832SShreyas NC block_type = ret; 21196277e832SShreyas NC off += array->size; 21206277e832SShreyas NC 21216277e832SShreyas NC array = (struct snd_soc_tplg_vendor_array *) 21226277e832SShreyas NC (tplg_w->priv.data + off); 21236277e832SShreyas NC 21246277e832SShreyas NC ret = skl_tplg_get_desc_blocks(dev, array); 21256277e832SShreyas NC 21266277e832SShreyas NC if (ret < 0) 21276277e832SShreyas NC return ret; 21286277e832SShreyas NC block_size = ret; 21296277e832SShreyas NC off += array->size; 21306277e832SShreyas NC 21316277e832SShreyas NC array = (struct snd_soc_tplg_vendor_array *) 21326277e832SShreyas NC (tplg_w->priv.data + off); 21336277e832SShreyas NC 21346277e832SShreyas NC data = (tplg_w->priv.data + off); 21356277e832SShreyas NC 21366277e832SShreyas NC if (block_type == SKL_TYPE_TUPLE) { 21376277e832SShreyas NC ret = skl_tplg_get_tokens(dev, data, 21386277e832SShreyas NC skl, mconfig, block_size); 21396277e832SShreyas NC 21406277e832SShreyas NC if (ret < 0) 21416277e832SShreyas NC return ret; 21426277e832SShreyas NC 21436277e832SShreyas NC --num_blocks; 21446277e832SShreyas NC } else { 21456277e832SShreyas NC if (mconfig->formats_config.caps_size > 0) 21466277e832SShreyas NC memcpy(mconfig->formats_config.caps, data, 21476277e832SShreyas NC mconfig->formats_config.caps_size); 21486277e832SShreyas NC --num_blocks; 21496277e832SShreyas NC } 21506277e832SShreyas NC } 21516277e832SShreyas NC 21526277e832SShreyas NC return 0; 21534cd9899fSHardik T Shah } 21544cd9899fSHardik T Shah 2155fe3f4442SDharageswari R static void skl_clear_pin_config(struct snd_soc_platform *platform, 2156fe3f4442SDharageswari R struct snd_soc_dapm_widget *w) 2157fe3f4442SDharageswari R { 2158fe3f4442SDharageswari R int i; 2159fe3f4442SDharageswari R struct skl_module_cfg *mconfig; 2160fe3f4442SDharageswari R struct skl_pipe *pipe; 2161fe3f4442SDharageswari R 2162fe3f4442SDharageswari R if (!strncmp(w->dapm->component->name, platform->component.name, 2163fe3f4442SDharageswari R strlen(platform->component.name))) { 2164fe3f4442SDharageswari R mconfig = w->priv; 2165fe3f4442SDharageswari R pipe = mconfig->pipe; 2166fe3f4442SDharageswari R for (i = 0; i < mconfig->max_in_queue; i++) { 2167fe3f4442SDharageswari R mconfig->m_in_pin[i].in_use = false; 2168fe3f4442SDharageswari R mconfig->m_in_pin[i].pin_state = SKL_PIN_UNBIND; 2169fe3f4442SDharageswari R } 2170fe3f4442SDharageswari R for (i = 0; i < mconfig->max_out_queue; i++) { 2171fe3f4442SDharageswari R mconfig->m_out_pin[i].in_use = false; 2172fe3f4442SDharageswari R mconfig->m_out_pin[i].pin_state = SKL_PIN_UNBIND; 2173fe3f4442SDharageswari R } 2174fe3f4442SDharageswari R pipe->state = SKL_PIPE_INVALID; 2175fe3f4442SDharageswari R mconfig->m_state = SKL_MODULE_UNINIT; 2176fe3f4442SDharageswari R } 2177fe3f4442SDharageswari R } 2178fe3f4442SDharageswari R 2179fe3f4442SDharageswari R void skl_cleanup_resources(struct skl *skl) 2180fe3f4442SDharageswari R { 2181fe3f4442SDharageswari R struct skl_sst *ctx = skl->skl_sst; 2182fe3f4442SDharageswari R struct snd_soc_platform *soc_platform = skl->platform; 2183fe3f4442SDharageswari R struct snd_soc_dapm_widget *w; 2184fe3f4442SDharageswari R struct snd_soc_card *card; 2185fe3f4442SDharageswari R 2186fe3f4442SDharageswari R if (soc_platform == NULL) 2187fe3f4442SDharageswari R return; 2188fe3f4442SDharageswari R 2189fe3f4442SDharageswari R card = soc_platform->component.card; 2190fe3f4442SDharageswari R if (!card || !card->instantiated) 2191fe3f4442SDharageswari R return; 2192fe3f4442SDharageswari R 2193fe3f4442SDharageswari R skl->resource.mem = 0; 2194fe3f4442SDharageswari R skl->resource.mcps = 0; 2195fe3f4442SDharageswari R 2196fe3f4442SDharageswari R list_for_each_entry(w, &card->widgets, list) { 2197fe3f4442SDharageswari R if (is_skl_dsp_widget_type(w) && (w->priv != NULL)) 2198fe3f4442SDharageswari R skl_clear_pin_config(soc_platform, w); 2199fe3f4442SDharageswari R } 2200fe3f4442SDharageswari R 2201fe3f4442SDharageswari R skl_clear_module_cnt(ctx->dsp); 2202fe3f4442SDharageswari R } 2203fe3f4442SDharageswari R 22043af36706SVinod Koul /* 22053af36706SVinod Koul * Topology core widget load callback 22063af36706SVinod Koul * 22073af36706SVinod Koul * This is used to save the private data for each widget which gives 22083af36706SVinod Koul * information to the driver about module and pipeline parameters which DSP 22093af36706SVinod Koul * FW expects like ids, resource values, formats etc 22103af36706SVinod Koul */ 22113af36706SVinod Koul static int skl_tplg_widget_load(struct snd_soc_component *cmpnt, 22123af36706SVinod Koul struct snd_soc_dapm_widget *w, 22133af36706SVinod Koul struct snd_soc_tplg_dapm_widget *tplg_w) 22143af36706SVinod Koul { 22153af36706SVinod Koul int ret; 22163af36706SVinod Koul struct hdac_ext_bus *ebus = snd_soc_component_get_drvdata(cmpnt); 22173af36706SVinod Koul struct skl *skl = ebus_to_skl(ebus); 22183af36706SVinod Koul struct hdac_bus *bus = ebus_to_hbus(ebus); 22193af36706SVinod Koul struct skl_module_cfg *mconfig; 22203af36706SVinod Koul 22213af36706SVinod Koul if (!tplg_w->priv.size) 22223af36706SVinod Koul goto bind_event; 22233af36706SVinod Koul 22243af36706SVinod Koul mconfig = devm_kzalloc(bus->dev, sizeof(*mconfig), GFP_KERNEL); 22253af36706SVinod Koul 22263af36706SVinod Koul if (!mconfig) 22273af36706SVinod Koul return -ENOMEM; 22283af36706SVinod Koul 22293af36706SVinod Koul w->priv = mconfig; 223009305da9SShreyas NC 2231b7c50555SVinod Koul /* 2232b7c50555SVinod Koul * module binary can be loaded later, so set it to query when 2233b7c50555SVinod Koul * module is load for a use case 2234b7c50555SVinod Koul */ 2235b7c50555SVinod Koul mconfig->id.module_id = -1; 22364cd9899fSHardik T Shah 22376277e832SShreyas NC /* Parse private data for tuples */ 22386277e832SShreyas NC ret = skl_tplg_get_pvt_data(tplg_w, skl, bus->dev, mconfig); 22396277e832SShreyas NC if (ret < 0) 22406277e832SShreyas NC return ret; 22413af36706SVinod Koul bind_event: 22423af36706SVinod Koul if (tplg_w->event_type == 0) { 22433373f716SVinod Koul dev_dbg(bus->dev, "ASoC: No event handler required\n"); 22443af36706SVinod Koul return 0; 22453af36706SVinod Koul } 22463af36706SVinod Koul 22473af36706SVinod Koul ret = snd_soc_tplg_widget_bind_event(w, skl_tplg_widget_ops, 2248b663a8c5SJeeja KP ARRAY_SIZE(skl_tplg_widget_ops), 2249b663a8c5SJeeja KP tplg_w->event_type); 22503af36706SVinod Koul 22513af36706SVinod Koul if (ret) { 22523af36706SVinod Koul dev_err(bus->dev, "%s: No matching event handlers found for %d\n", 22533af36706SVinod Koul __func__, tplg_w->event_type); 22543af36706SVinod Koul return -EINVAL; 22553af36706SVinod Koul } 22563af36706SVinod Koul 22573af36706SVinod Koul return 0; 22583af36706SVinod Koul } 22593af36706SVinod Koul 2260140adfbaSJeeja KP static int skl_init_algo_data(struct device *dev, struct soc_bytes_ext *be, 2261140adfbaSJeeja KP struct snd_soc_tplg_bytes_control *bc) 2262140adfbaSJeeja KP { 2263140adfbaSJeeja KP struct skl_algo_data *ac; 2264140adfbaSJeeja KP struct skl_dfw_algo_data *dfw_ac = 2265140adfbaSJeeja KP (struct skl_dfw_algo_data *)bc->priv.data; 2266140adfbaSJeeja KP 2267140adfbaSJeeja KP ac = devm_kzalloc(dev, sizeof(*ac), GFP_KERNEL); 2268140adfbaSJeeja KP if (!ac) 2269140adfbaSJeeja KP return -ENOMEM; 2270140adfbaSJeeja KP 2271140adfbaSJeeja KP /* Fill private data */ 2272140adfbaSJeeja KP ac->max = dfw_ac->max; 2273140adfbaSJeeja KP ac->param_id = dfw_ac->param_id; 2274140adfbaSJeeja KP ac->set_params = dfw_ac->set_params; 22750d682104SDharageswari R ac->size = dfw_ac->max; 2276140adfbaSJeeja KP 2277140adfbaSJeeja KP if (ac->max) { 2278140adfbaSJeeja KP ac->params = (char *) devm_kzalloc(dev, ac->max, GFP_KERNEL); 2279140adfbaSJeeja KP if (!ac->params) 2280140adfbaSJeeja KP return -ENOMEM; 2281140adfbaSJeeja KP 2282140adfbaSJeeja KP memcpy(ac->params, dfw_ac->params, ac->max); 2283140adfbaSJeeja KP } 2284140adfbaSJeeja KP 2285140adfbaSJeeja KP be->dobj.private = ac; 2286140adfbaSJeeja KP return 0; 2287140adfbaSJeeja KP } 2288140adfbaSJeeja KP 2289140adfbaSJeeja KP static int skl_tplg_control_load(struct snd_soc_component *cmpnt, 2290140adfbaSJeeja KP struct snd_kcontrol_new *kctl, 2291140adfbaSJeeja KP struct snd_soc_tplg_ctl_hdr *hdr) 2292140adfbaSJeeja KP { 2293140adfbaSJeeja KP struct soc_bytes_ext *sb; 2294140adfbaSJeeja KP struct snd_soc_tplg_bytes_control *tplg_bc; 2295140adfbaSJeeja KP struct hdac_ext_bus *ebus = snd_soc_component_get_drvdata(cmpnt); 2296140adfbaSJeeja KP struct hdac_bus *bus = ebus_to_hbus(ebus); 2297140adfbaSJeeja KP 2298140adfbaSJeeja KP switch (hdr->ops.info) { 2299140adfbaSJeeja KP case SND_SOC_TPLG_CTL_BYTES: 2300140adfbaSJeeja KP tplg_bc = container_of(hdr, 2301140adfbaSJeeja KP struct snd_soc_tplg_bytes_control, hdr); 2302140adfbaSJeeja KP if (kctl->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { 2303140adfbaSJeeja KP sb = (struct soc_bytes_ext *)kctl->private_value; 2304140adfbaSJeeja KP if (tplg_bc->priv.size) 2305140adfbaSJeeja KP return skl_init_algo_data( 2306140adfbaSJeeja KP bus->dev, sb, tplg_bc); 2307140adfbaSJeeja KP } 2308140adfbaSJeeja KP break; 2309140adfbaSJeeja KP 2310140adfbaSJeeja KP default: 2311140adfbaSJeeja KP dev_warn(bus->dev, "Control load not supported %d:%d:%d\n", 2312140adfbaSJeeja KP hdr->ops.get, hdr->ops.put, hdr->ops.info); 2313140adfbaSJeeja KP break; 2314140adfbaSJeeja KP } 2315140adfbaSJeeja KP 2316140adfbaSJeeja KP return 0; 2317140adfbaSJeeja KP } 2318140adfbaSJeeja KP 2319541070ceSShreyas NC static int skl_tplg_fill_str_mfest_tkn(struct device *dev, 2320541070ceSShreyas NC struct snd_soc_tplg_vendor_string_elem *str_elem, 2321eee0e16fSJeeja KP struct skl *skl) 2322541070ceSShreyas NC { 2323541070ceSShreyas NC int tkn_count = 0; 2324541070ceSShreyas NC static int ref_count; 2325541070ceSShreyas NC 2326541070ceSShreyas NC switch (str_elem->token) { 2327541070ceSShreyas NC case SKL_TKN_STR_LIB_NAME: 2328eee0e16fSJeeja KP if (ref_count > skl->skl_sst->lib_count - 1) { 2329541070ceSShreyas NC ref_count = 0; 2330541070ceSShreyas NC return -EINVAL; 2331541070ceSShreyas NC } 2332541070ceSShreyas NC 2333eee0e16fSJeeja KP strncpy(skl->skl_sst->lib_info[ref_count].name, 2334eee0e16fSJeeja KP str_elem->string, 2335eee0e16fSJeeja KP ARRAY_SIZE(skl->skl_sst->lib_info[ref_count].name)); 2336541070ceSShreyas NC ref_count++; 2337541070ceSShreyas NC tkn_count++; 2338541070ceSShreyas NC break; 2339541070ceSShreyas NC 2340541070ceSShreyas NC default: 2341ecd286a9SColin Ian King dev_err(dev, "Not a string token %d\n", str_elem->token); 2342541070ceSShreyas NC break; 2343541070ceSShreyas NC } 2344541070ceSShreyas NC 2345541070ceSShreyas NC return tkn_count; 2346541070ceSShreyas NC } 2347541070ceSShreyas NC 2348541070ceSShreyas NC static int skl_tplg_get_str_tkn(struct device *dev, 2349541070ceSShreyas NC struct snd_soc_tplg_vendor_array *array, 2350eee0e16fSJeeja KP struct skl *skl) 2351541070ceSShreyas NC { 2352541070ceSShreyas NC int tkn_count = 0, ret; 2353541070ceSShreyas NC struct snd_soc_tplg_vendor_string_elem *str_elem; 2354541070ceSShreyas NC 2355541070ceSShreyas NC str_elem = (struct snd_soc_tplg_vendor_string_elem *)array->value; 2356541070ceSShreyas NC while (tkn_count < array->num_elems) { 2357eee0e16fSJeeja KP ret = skl_tplg_fill_str_mfest_tkn(dev, str_elem, skl); 2358541070ceSShreyas NC str_elem++; 2359541070ceSShreyas NC 2360541070ceSShreyas NC if (ret < 0) 2361541070ceSShreyas NC return ret; 2362541070ceSShreyas NC 2363541070ceSShreyas NC tkn_count = tkn_count + ret; 2364541070ceSShreyas NC } 2365541070ceSShreyas NC 2366541070ceSShreyas NC return tkn_count; 2367541070ceSShreyas NC } 2368541070ceSShreyas NC 2369541070ceSShreyas NC static int skl_tplg_get_int_tkn(struct device *dev, 2370541070ceSShreyas NC struct snd_soc_tplg_vendor_value_elem *tkn_elem, 2371eee0e16fSJeeja KP struct skl *skl) 2372541070ceSShreyas NC { 2373541070ceSShreyas NC int tkn_count = 0; 2374541070ceSShreyas NC 2375541070ceSShreyas NC switch (tkn_elem->token) { 2376541070ceSShreyas NC case SKL_TKN_U32_LIB_COUNT: 2377eee0e16fSJeeja KP skl->skl_sst->lib_count = tkn_elem->value; 2378541070ceSShreyas NC tkn_count++; 2379541070ceSShreyas NC break; 2380541070ceSShreyas NC 2381541070ceSShreyas NC default: 2382ecd286a9SColin Ian King dev_err(dev, "Not a manifest token %d\n", tkn_elem->token); 2383541070ceSShreyas NC return -EINVAL; 2384541070ceSShreyas NC } 2385541070ceSShreyas NC 2386541070ceSShreyas NC return tkn_count; 2387541070ceSShreyas NC } 2388541070ceSShreyas NC 2389541070ceSShreyas NC /* 2390541070ceSShreyas NC * Fill the manifest structure by parsing the tokens based on the 2391541070ceSShreyas NC * type. 2392541070ceSShreyas NC */ 2393541070ceSShreyas NC static int skl_tplg_get_manifest_tkn(struct device *dev, 2394eee0e16fSJeeja KP char *pvt_data, struct skl *skl, 2395541070ceSShreyas NC int block_size) 2396541070ceSShreyas NC { 2397541070ceSShreyas NC int tkn_count = 0, ret; 2398541070ceSShreyas NC int off = 0, tuple_size = 0; 2399541070ceSShreyas NC struct snd_soc_tplg_vendor_array *array; 2400541070ceSShreyas NC struct snd_soc_tplg_vendor_value_elem *tkn_elem; 2401541070ceSShreyas NC 2402541070ceSShreyas NC if (block_size <= 0) 2403541070ceSShreyas NC return -EINVAL; 2404541070ceSShreyas NC 2405541070ceSShreyas NC while (tuple_size < block_size) { 2406541070ceSShreyas NC array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off); 2407541070ceSShreyas NC off += array->size; 2408541070ceSShreyas NC switch (array->type) { 2409541070ceSShreyas NC case SND_SOC_TPLG_TUPLE_TYPE_STRING: 2410eee0e16fSJeeja KP ret = skl_tplg_get_str_tkn(dev, array, skl); 2411541070ceSShreyas NC 2412541070ceSShreyas NC if (ret < 0) 2413541070ceSShreyas NC return ret; 2414541070ceSShreyas NC tkn_count += ret; 2415541070ceSShreyas NC 2416541070ceSShreyas NC tuple_size += tkn_count * 2417541070ceSShreyas NC sizeof(struct snd_soc_tplg_vendor_string_elem); 2418541070ceSShreyas NC continue; 2419541070ceSShreyas NC 2420541070ceSShreyas NC case SND_SOC_TPLG_TUPLE_TYPE_UUID: 2421ecd286a9SColin Ian King dev_warn(dev, "no uuid tokens for skl tplf manifest\n"); 2422541070ceSShreyas NC continue; 2423541070ceSShreyas NC 2424541070ceSShreyas NC default: 2425541070ceSShreyas NC tkn_elem = array->value; 2426541070ceSShreyas NC tkn_count = 0; 2427541070ceSShreyas NC break; 2428541070ceSShreyas NC } 2429541070ceSShreyas NC 2430541070ceSShreyas NC while (tkn_count <= array->num_elems - 1) { 2431541070ceSShreyas NC ret = skl_tplg_get_int_tkn(dev, 2432eee0e16fSJeeja KP tkn_elem, skl); 2433541070ceSShreyas NC if (ret < 0) 2434541070ceSShreyas NC return ret; 2435541070ceSShreyas NC 2436541070ceSShreyas NC tkn_count = tkn_count + ret; 2437541070ceSShreyas NC tkn_elem++; 2438541070ceSShreyas NC tuple_size += tkn_count * 2439541070ceSShreyas NC sizeof(struct snd_soc_tplg_vendor_value_elem); 2440541070ceSShreyas NC break; 2441541070ceSShreyas NC } 2442541070ceSShreyas NC tkn_count = 0; 2443541070ceSShreyas NC } 2444541070ceSShreyas NC 2445541070ceSShreyas NC return 0; 2446541070ceSShreyas NC } 2447541070ceSShreyas NC 2448541070ceSShreyas NC /* 2449541070ceSShreyas NC * Parse manifest private data for tokens. The private data block is 2450541070ceSShreyas NC * preceded by descriptors for type and size of data block. 2451541070ceSShreyas NC */ 2452541070ceSShreyas NC static int skl_tplg_get_manifest_data(struct snd_soc_tplg_manifest *manifest, 2453eee0e16fSJeeja KP struct device *dev, struct skl *skl) 2454541070ceSShreyas NC { 2455541070ceSShreyas NC struct snd_soc_tplg_vendor_array *array; 2456541070ceSShreyas NC int num_blocks, block_size = 0, block_type, off = 0; 2457541070ceSShreyas NC char *data; 2458541070ceSShreyas NC int ret; 2459541070ceSShreyas NC 2460541070ceSShreyas NC /* Read the NUM_DATA_BLOCKS descriptor */ 2461541070ceSShreyas NC array = (struct snd_soc_tplg_vendor_array *)manifest->priv.data; 2462541070ceSShreyas NC ret = skl_tplg_get_desc_blocks(dev, array); 2463541070ceSShreyas NC if (ret < 0) 2464541070ceSShreyas NC return ret; 2465541070ceSShreyas NC num_blocks = ret; 2466541070ceSShreyas NC 2467541070ceSShreyas NC off += array->size; 2468541070ceSShreyas NC array = (struct snd_soc_tplg_vendor_array *) 2469541070ceSShreyas NC (manifest->priv.data + off); 2470541070ceSShreyas NC 2471541070ceSShreyas NC /* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */ 2472541070ceSShreyas NC while (num_blocks > 0) { 2473541070ceSShreyas NC ret = skl_tplg_get_desc_blocks(dev, array); 2474541070ceSShreyas NC 2475541070ceSShreyas NC if (ret < 0) 2476541070ceSShreyas NC return ret; 2477541070ceSShreyas NC block_type = ret; 2478541070ceSShreyas NC off += array->size; 2479541070ceSShreyas NC 2480541070ceSShreyas NC array = (struct snd_soc_tplg_vendor_array *) 2481541070ceSShreyas NC (manifest->priv.data + off); 2482541070ceSShreyas NC 2483541070ceSShreyas NC ret = skl_tplg_get_desc_blocks(dev, array); 2484541070ceSShreyas NC 2485541070ceSShreyas NC if (ret < 0) 2486541070ceSShreyas NC return ret; 2487541070ceSShreyas NC block_size = ret; 2488541070ceSShreyas NC off += array->size; 2489541070ceSShreyas NC 2490541070ceSShreyas NC array = (struct snd_soc_tplg_vendor_array *) 2491541070ceSShreyas NC (manifest->priv.data + off); 2492541070ceSShreyas NC 2493541070ceSShreyas NC data = (manifest->priv.data + off); 2494541070ceSShreyas NC 2495541070ceSShreyas NC if (block_type == SKL_TYPE_TUPLE) { 2496eee0e16fSJeeja KP ret = skl_tplg_get_manifest_tkn(dev, data, skl, 2497541070ceSShreyas NC block_size); 2498541070ceSShreyas NC 2499541070ceSShreyas NC if (ret < 0) 2500541070ceSShreyas NC return ret; 2501541070ceSShreyas NC 2502541070ceSShreyas NC --num_blocks; 2503541070ceSShreyas NC } else { 2504541070ceSShreyas NC return -EINVAL; 2505541070ceSShreyas NC } 2506541070ceSShreyas NC } 2507541070ceSShreyas NC 2508541070ceSShreyas NC return 0; 2509541070ceSShreyas NC } 2510541070ceSShreyas NC 251115ecaba9SKranthi G static int skl_manifest_load(struct snd_soc_component *cmpnt, 251215ecaba9SKranthi G struct snd_soc_tplg_manifest *manifest) 251315ecaba9SKranthi G { 251415ecaba9SKranthi G struct hdac_ext_bus *ebus = snd_soc_component_get_drvdata(cmpnt); 251515ecaba9SKranthi G struct hdac_bus *bus = ebus_to_hbus(ebus); 251615ecaba9SKranthi G struct skl *skl = ebus_to_skl(ebus); 251715ecaba9SKranthi G 2518c15ad605SVinod Koul /* proceed only if we have private data defined */ 2519c15ad605SVinod Koul if (manifest->priv.size == 0) 2520c15ad605SVinod Koul return 0; 2521c15ad605SVinod Koul 2522eee0e16fSJeeja KP skl_tplg_get_manifest_data(manifest, bus->dev, skl); 2523541070ceSShreyas NC 2524eee0e16fSJeeja KP if (skl->skl_sst->lib_count > SKL_MAX_LIB) { 252515ecaba9SKranthi G dev_err(bus->dev, "Exceeding max Library count. Got:%d\n", 2526eee0e16fSJeeja KP skl->skl_sst->lib_count); 2527eee0e16fSJeeja KP return -EINVAL; 252815ecaba9SKranthi G } 252915ecaba9SKranthi G 2530eee0e16fSJeeja KP return 0; 253115ecaba9SKranthi G } 253215ecaba9SKranthi G 25333af36706SVinod Koul static struct snd_soc_tplg_ops skl_tplg_ops = { 25343af36706SVinod Koul .widget_load = skl_tplg_widget_load, 2535140adfbaSJeeja KP .control_load = skl_tplg_control_load, 2536140adfbaSJeeja KP .bytes_ext_ops = skl_tlv_ops, 2537140adfbaSJeeja KP .bytes_ext_ops_count = ARRAY_SIZE(skl_tlv_ops), 253815ecaba9SKranthi G .manifest = skl_manifest_load, 25393af36706SVinod Koul }; 25403af36706SVinod Koul 2541287af4f9SJeeja KP /* 2542287af4f9SJeeja KP * A pipe can have multiple modules, each of them will be a DAPM widget as 2543287af4f9SJeeja KP * well. While managing a pipeline we need to get the list of all the 2544287af4f9SJeeja KP * widgets in a pipelines, so this helper - skl_tplg_create_pipe_widget_list() 2545287af4f9SJeeja KP * helps to get the SKL type widgets in that pipeline 2546287af4f9SJeeja KP */ 2547287af4f9SJeeja KP static int skl_tplg_create_pipe_widget_list(struct snd_soc_platform *platform) 2548287af4f9SJeeja KP { 2549287af4f9SJeeja KP struct snd_soc_dapm_widget *w; 2550287af4f9SJeeja KP struct skl_module_cfg *mcfg = NULL; 2551287af4f9SJeeja KP struct skl_pipe_module *p_module = NULL; 2552287af4f9SJeeja KP struct skl_pipe *pipe; 2553287af4f9SJeeja KP 2554287af4f9SJeeja KP list_for_each_entry(w, &platform->component.card->widgets, list) { 2555287af4f9SJeeja KP if (is_skl_dsp_widget_type(w) && w->priv != NULL) { 2556287af4f9SJeeja KP mcfg = w->priv; 2557287af4f9SJeeja KP pipe = mcfg->pipe; 2558287af4f9SJeeja KP 2559287af4f9SJeeja KP p_module = devm_kzalloc(platform->dev, 2560287af4f9SJeeja KP sizeof(*p_module), GFP_KERNEL); 2561287af4f9SJeeja KP if (!p_module) 2562287af4f9SJeeja KP return -ENOMEM; 2563287af4f9SJeeja KP 2564287af4f9SJeeja KP p_module->w = w; 2565287af4f9SJeeja KP list_add_tail(&p_module->node, &pipe->w_list); 2566287af4f9SJeeja KP } 2567287af4f9SJeeja KP } 2568287af4f9SJeeja KP 2569287af4f9SJeeja KP return 0; 2570287af4f9SJeeja KP } 2571287af4f9SJeeja KP 2572f0aa94faSJeeja KP static void skl_tplg_set_pipe_type(struct skl *skl, struct skl_pipe *pipe) 2573f0aa94faSJeeja KP { 2574f0aa94faSJeeja KP struct skl_pipe_module *w_module; 2575f0aa94faSJeeja KP struct snd_soc_dapm_widget *w; 2576f0aa94faSJeeja KP struct skl_module_cfg *mconfig; 2577f0aa94faSJeeja KP bool host_found = false, link_found = false; 2578f0aa94faSJeeja KP 2579f0aa94faSJeeja KP list_for_each_entry(w_module, &pipe->w_list, node) { 2580f0aa94faSJeeja KP w = w_module->w; 2581f0aa94faSJeeja KP mconfig = w->priv; 2582f0aa94faSJeeja KP 2583f0aa94faSJeeja KP if (mconfig->dev_type == SKL_DEVICE_HDAHOST) 2584f0aa94faSJeeja KP host_found = true; 2585f0aa94faSJeeja KP else if (mconfig->dev_type != SKL_DEVICE_NONE) 2586f0aa94faSJeeja KP link_found = true; 2587f0aa94faSJeeja KP } 2588f0aa94faSJeeja KP 2589f0aa94faSJeeja KP if (host_found && link_found) 2590f0aa94faSJeeja KP pipe->passthru = true; 2591f0aa94faSJeeja KP else 2592f0aa94faSJeeja KP pipe->passthru = false; 2593f0aa94faSJeeja KP } 2594f0aa94faSJeeja KP 25953af36706SVinod Koul /* This will be read from topology manifest, currently defined here */ 25963af36706SVinod Koul #define SKL_MAX_MCPS 30000000 25973af36706SVinod Koul #define SKL_FW_MAX_MEM 1000000 25983af36706SVinod Koul 25993af36706SVinod Koul /* 26003af36706SVinod Koul * SKL topology init routine 26013af36706SVinod Koul */ 26023af36706SVinod Koul int skl_tplg_init(struct snd_soc_platform *platform, struct hdac_ext_bus *ebus) 26033af36706SVinod Koul { 26043af36706SVinod Koul int ret; 26053af36706SVinod Koul const struct firmware *fw; 26063af36706SVinod Koul struct hdac_bus *bus = ebus_to_hbus(ebus); 26073af36706SVinod Koul struct skl *skl = ebus_to_skl(ebus); 2608f0aa94faSJeeja KP struct skl_pipeline *ppl; 26093af36706SVinod Koul 26104b235c43SVinod Koul ret = request_firmware(&fw, skl->tplg_name, bus->dev); 26113af36706SVinod Koul if (ret < 0) { 2612b663a8c5SJeeja KP dev_err(bus->dev, "tplg fw %s load failed with %d\n", 26134b235c43SVinod Koul skl->tplg_name, ret); 26144b235c43SVinod Koul ret = request_firmware(&fw, "dfw_sst.bin", bus->dev); 26154b235c43SVinod Koul if (ret < 0) { 26164b235c43SVinod Koul dev_err(bus->dev, "Fallback tplg fw %s load failed with %d\n", 26173af36706SVinod Koul "dfw_sst.bin", ret); 26183af36706SVinod Koul return ret; 26193af36706SVinod Koul } 26204b235c43SVinod Koul } 26213af36706SVinod Koul 26223af36706SVinod Koul /* 26233af36706SVinod Koul * The complete tplg for SKL is loaded as index 0, we don't use 26243af36706SVinod Koul * any other index 26253af36706SVinod Koul */ 2626b663a8c5SJeeja KP ret = snd_soc_tplg_component_load(&platform->component, 2627b663a8c5SJeeja KP &skl_tplg_ops, fw, 0); 26283af36706SVinod Koul if (ret < 0) { 26293af36706SVinod Koul dev_err(bus->dev, "tplg component load failed%d\n", ret); 2630c14a82c7SSudip Mukherjee release_firmware(fw); 26313af36706SVinod Koul return -EINVAL; 26323af36706SVinod Koul } 26333af36706SVinod Koul 26343af36706SVinod Koul skl->resource.max_mcps = SKL_MAX_MCPS; 26353af36706SVinod Koul skl->resource.max_mem = SKL_FW_MAX_MEM; 26363af36706SVinod Koul 2637d8018361SVinod Koul skl->tplg = fw; 2638287af4f9SJeeja KP ret = skl_tplg_create_pipe_widget_list(platform); 2639287af4f9SJeeja KP if (ret < 0) 2640287af4f9SJeeja KP return ret; 2641d8018361SVinod Koul 2642f0aa94faSJeeja KP list_for_each_entry(ppl, &skl->ppl_list, node) 2643f0aa94faSJeeja KP skl_tplg_set_pipe_type(skl, ppl->pipe); 26443af36706SVinod Koul 26453af36706SVinod Koul return 0; 2646e4e2d2f4SJeeja KP } 2647