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; 302f0c8e1d9SSubhransu S. Prusty int in_rate, out_rate; 3034cd9899fSHardik T Shah 3044cd9899fSHardik T Shah 3054cd9899fSHardik T Shah /* Since fixups is applied to pin 0 only, ibs, obs needs 3064cd9899fSHardik T Shah * change for pin 0 only 3074cd9899fSHardik T Shah */ 3084cd9899fSHardik T Shah in_fmt = &mcfg->in_fmt[0]; 3094cd9899fSHardik T Shah out_fmt = &mcfg->out_fmt[0]; 310f7590d4fSJeeja KP 311f7590d4fSJeeja KP if (mcfg->m_type == SKL_MODULE_TYPE_SRCINT) 312f7590d4fSJeeja KP multiplier = 5; 313f0c8e1d9SSubhransu S. Prusty 314f0c8e1d9SSubhransu S. Prusty if (in_fmt->s_freq % 1000) 315f0c8e1d9SSubhransu S. Prusty in_rate = (in_fmt->s_freq / 1000) + 1; 316f0c8e1d9SSubhransu S. Prusty else 317f0c8e1d9SSubhransu S. Prusty in_rate = (in_fmt->s_freq / 1000); 318f0c8e1d9SSubhransu S. Prusty 319f0c8e1d9SSubhransu S. Prusty mcfg->ibs = in_rate * (mcfg->in_fmt->channels) * 3204cd9899fSHardik T Shah (mcfg->in_fmt->bit_depth >> 3) * 321f7590d4fSJeeja KP multiplier; 322f7590d4fSJeeja KP 323f0c8e1d9SSubhransu S. Prusty if (mcfg->out_fmt->s_freq % 1000) 324f0c8e1d9SSubhransu S. Prusty out_rate = (mcfg->out_fmt->s_freq / 1000) + 1; 325f0c8e1d9SSubhransu S. Prusty else 326f0c8e1d9SSubhransu S. Prusty out_rate = (mcfg->out_fmt->s_freq / 1000); 327f0c8e1d9SSubhransu S. Prusty 328f0c8e1d9SSubhransu S. Prusty mcfg->obs = out_rate * (mcfg->out_fmt->channels) * 3294cd9899fSHardik T Shah (mcfg->out_fmt->bit_depth >> 3) * 330f7590d4fSJeeja KP multiplier; 331f7590d4fSJeeja KP } 332f7590d4fSJeeja KP 3332d1419a3SJeeja KP static int skl_tplg_update_be_blob(struct snd_soc_dapm_widget *w, 3342d1419a3SJeeja KP struct skl_sst *ctx) 3352d1419a3SJeeja KP { 3362d1419a3SJeeja KP struct skl_module_cfg *m_cfg = w->priv; 3372d1419a3SJeeja KP int link_type, dir; 3382d1419a3SJeeja KP u32 ch, s_freq, s_fmt; 3392d1419a3SJeeja KP struct nhlt_specific_cfg *cfg; 3402d1419a3SJeeja KP struct skl *skl = get_skl_ctx(ctx->dev); 3412d1419a3SJeeja KP 3422d1419a3SJeeja KP /* check if we already have blob */ 3432d1419a3SJeeja KP if (m_cfg->formats_config.caps_size > 0) 3442d1419a3SJeeja KP return 0; 3452d1419a3SJeeja KP 346c7c6c736SJeeja KP dev_dbg(ctx->dev, "Applying default cfg blob\n"); 3472d1419a3SJeeja KP switch (m_cfg->dev_type) { 3482d1419a3SJeeja KP case SKL_DEVICE_DMIC: 3492d1419a3SJeeja KP link_type = NHLT_LINK_DMIC; 350c7c6c736SJeeja KP dir = SNDRV_PCM_STREAM_CAPTURE; 3512d1419a3SJeeja KP s_freq = m_cfg->in_fmt[0].s_freq; 3522d1419a3SJeeja KP s_fmt = m_cfg->in_fmt[0].bit_depth; 3532d1419a3SJeeja KP ch = m_cfg->in_fmt[0].channels; 3542d1419a3SJeeja KP break; 3552d1419a3SJeeja KP 3562d1419a3SJeeja KP case SKL_DEVICE_I2S: 3572d1419a3SJeeja KP link_type = NHLT_LINK_SSP; 3582d1419a3SJeeja KP if (m_cfg->hw_conn_type == SKL_CONN_SOURCE) { 359c7c6c736SJeeja KP dir = SNDRV_PCM_STREAM_PLAYBACK; 3602d1419a3SJeeja KP s_freq = m_cfg->out_fmt[0].s_freq; 3612d1419a3SJeeja KP s_fmt = m_cfg->out_fmt[0].bit_depth; 3622d1419a3SJeeja KP ch = m_cfg->out_fmt[0].channels; 363c7c6c736SJeeja KP } else { 364c7c6c736SJeeja KP dir = SNDRV_PCM_STREAM_CAPTURE; 365c7c6c736SJeeja KP s_freq = m_cfg->in_fmt[0].s_freq; 366c7c6c736SJeeja KP s_fmt = m_cfg->in_fmt[0].bit_depth; 367c7c6c736SJeeja KP ch = m_cfg->in_fmt[0].channels; 3682d1419a3SJeeja KP } 3692d1419a3SJeeja KP break; 3702d1419a3SJeeja KP 3712d1419a3SJeeja KP default: 3722d1419a3SJeeja KP return -EINVAL; 3732d1419a3SJeeja KP } 3742d1419a3SJeeja KP 3752d1419a3SJeeja KP /* update the blob based on virtual bus_id and default params */ 3762d1419a3SJeeja KP cfg = skl_get_ep_blob(skl, m_cfg->vbus_id, link_type, 3772d1419a3SJeeja KP s_fmt, ch, s_freq, dir); 3782d1419a3SJeeja KP if (cfg) { 3792d1419a3SJeeja KP m_cfg->formats_config.caps_size = cfg->size; 3802d1419a3SJeeja KP m_cfg->formats_config.caps = (u32 *) &cfg->caps; 3812d1419a3SJeeja KP } else { 3822d1419a3SJeeja KP dev_err(ctx->dev, "Blob NULL for id %x type %d dirn %d\n", 3832d1419a3SJeeja KP m_cfg->vbus_id, link_type, dir); 3842d1419a3SJeeja KP dev_err(ctx->dev, "PCM: ch %d, freq %d, fmt %d\n", 3852d1419a3SJeeja KP ch, s_freq, s_fmt); 3862d1419a3SJeeja KP return -EIO; 3872d1419a3SJeeja KP } 3882d1419a3SJeeja KP 3892d1419a3SJeeja KP return 0; 3902d1419a3SJeeja KP } 3912d1419a3SJeeja KP 392f7590d4fSJeeja KP static void skl_tplg_update_module_params(struct snd_soc_dapm_widget *w, 393f7590d4fSJeeja KP struct skl_sst *ctx) 394f7590d4fSJeeja KP { 395f7590d4fSJeeja KP struct skl_module_cfg *m_cfg = w->priv; 396f7590d4fSJeeja KP struct skl_pipe_params *params = m_cfg->pipe->p_params; 397f7590d4fSJeeja KP int p_conn_type = m_cfg->pipe->conn_type; 398f7590d4fSJeeja KP bool is_fe; 399f7590d4fSJeeja KP 400f7590d4fSJeeja KP if (!m_cfg->params_fixup) 401f7590d4fSJeeja KP return; 402f7590d4fSJeeja KP 403f7590d4fSJeeja KP dev_dbg(ctx->dev, "Mconfig for widget=%s BEFORE updation\n", 404f7590d4fSJeeja KP w->name); 405f7590d4fSJeeja KP 406f7590d4fSJeeja KP skl_dump_mconfig(ctx, m_cfg); 407f7590d4fSJeeja KP 408f7590d4fSJeeja KP if (p_conn_type == SKL_PIPE_CONN_TYPE_FE) 409f7590d4fSJeeja KP is_fe = true; 410f7590d4fSJeeja KP else 411f7590d4fSJeeja KP is_fe = false; 412f7590d4fSJeeja KP 413f7590d4fSJeeja KP skl_tplg_update_params_fixup(m_cfg, params, is_fe); 414f7590d4fSJeeja KP skl_tplg_update_buffer_size(ctx, m_cfg); 415f7590d4fSJeeja KP 416f7590d4fSJeeja KP dev_dbg(ctx->dev, "Mconfig for widget=%s AFTER updation\n", 417f7590d4fSJeeja KP w->name); 418f7590d4fSJeeja KP 419f7590d4fSJeeja KP skl_dump_mconfig(ctx, m_cfg); 420f7590d4fSJeeja KP } 421f7590d4fSJeeja KP 422e4e2d2f4SJeeja KP /* 423abb74003SJeeja KP * some modules can have multiple params set from user control and 424abb74003SJeeja KP * need to be set after module is initialized. If set_param flag is 425abb74003SJeeja KP * set module params will be done after module is initialised. 426abb74003SJeeja KP */ 427abb74003SJeeja KP static int skl_tplg_set_module_params(struct snd_soc_dapm_widget *w, 428abb74003SJeeja KP struct skl_sst *ctx) 429abb74003SJeeja KP { 430abb74003SJeeja KP int i, ret; 431abb74003SJeeja KP struct skl_module_cfg *mconfig = w->priv; 432abb74003SJeeja KP const struct snd_kcontrol_new *k; 433abb74003SJeeja KP struct soc_bytes_ext *sb; 434abb74003SJeeja KP struct skl_algo_data *bc; 435abb74003SJeeja KP struct skl_specific_cfg *sp_cfg; 436abb74003SJeeja KP 437abb74003SJeeja KP if (mconfig->formats_config.caps_size > 0 && 4384ced1827SJeeja KP mconfig->formats_config.set_params == SKL_PARAM_SET) { 439abb74003SJeeja KP sp_cfg = &mconfig->formats_config; 440abb74003SJeeja KP ret = skl_set_module_params(ctx, sp_cfg->caps, 441abb74003SJeeja KP sp_cfg->caps_size, 442abb74003SJeeja KP sp_cfg->param_id, mconfig); 443abb74003SJeeja KP if (ret < 0) 444abb74003SJeeja KP return ret; 445abb74003SJeeja KP } 446abb74003SJeeja KP 447abb74003SJeeja KP for (i = 0; i < w->num_kcontrols; i++) { 448abb74003SJeeja KP k = &w->kcontrol_news[i]; 449abb74003SJeeja KP if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { 450abb74003SJeeja KP sb = (void *) k->private_value; 451abb74003SJeeja KP bc = (struct skl_algo_data *)sb->dobj.private; 452abb74003SJeeja KP 4534ced1827SJeeja KP if (bc->set_params == SKL_PARAM_SET) { 454abb74003SJeeja KP ret = skl_set_module_params(ctx, 4550d682104SDharageswari R (u32 *)bc->params, bc->size, 456abb74003SJeeja KP bc->param_id, mconfig); 457abb74003SJeeja KP if (ret < 0) 458abb74003SJeeja KP return ret; 459abb74003SJeeja KP } 460abb74003SJeeja KP } 461abb74003SJeeja KP } 462abb74003SJeeja KP 463abb74003SJeeja KP return 0; 464abb74003SJeeja KP } 465abb74003SJeeja KP 466abb74003SJeeja KP /* 467abb74003SJeeja KP * some module param can set from user control and this is required as 468abb74003SJeeja KP * when module is initailzed. if module param is required in init it is 469abb74003SJeeja KP * identifed by set_param flag. if set_param flag is not set, then this 470abb74003SJeeja KP * parameter needs to set as part of module init. 471abb74003SJeeja KP */ 472abb74003SJeeja KP static int skl_tplg_set_module_init_data(struct snd_soc_dapm_widget *w) 473abb74003SJeeja KP { 474abb74003SJeeja KP const struct snd_kcontrol_new *k; 475abb74003SJeeja KP struct soc_bytes_ext *sb; 476abb74003SJeeja KP struct skl_algo_data *bc; 477abb74003SJeeja KP struct skl_module_cfg *mconfig = w->priv; 478abb74003SJeeja KP int i; 479abb74003SJeeja KP 480abb74003SJeeja KP for (i = 0; i < w->num_kcontrols; i++) { 481abb74003SJeeja KP k = &w->kcontrol_news[i]; 482abb74003SJeeja KP if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { 483abb74003SJeeja KP sb = (struct soc_bytes_ext *)k->private_value; 484abb74003SJeeja KP bc = (struct skl_algo_data *)sb->dobj.private; 485abb74003SJeeja KP 4864ced1827SJeeja KP if (bc->set_params != SKL_PARAM_INIT) 487abb74003SJeeja KP continue; 488abb74003SJeeja KP 489abb74003SJeeja KP mconfig->formats_config.caps = (u32 *)&bc->params; 4900d682104SDharageswari R mconfig->formats_config.caps_size = bc->size; 491abb74003SJeeja KP 492abb74003SJeeja KP break; 493abb74003SJeeja KP } 494abb74003SJeeja KP } 495abb74003SJeeja KP 496abb74003SJeeja KP return 0; 497abb74003SJeeja KP } 498abb74003SJeeja KP 499bb704a73SJeeja KP static int skl_tplg_module_prepare(struct skl_sst *ctx, struct skl_pipe *pipe, 500bb704a73SJeeja KP struct snd_soc_dapm_widget *w, struct skl_module_cfg *mcfg) 501bb704a73SJeeja KP { 502bb704a73SJeeja KP switch (mcfg->dev_type) { 503bb704a73SJeeja KP case SKL_DEVICE_HDAHOST: 504bb704a73SJeeja KP return skl_pcm_host_dma_prepare(ctx->dev, pipe->p_params); 505bb704a73SJeeja KP 506bb704a73SJeeja KP case SKL_DEVICE_HDALINK: 507bb704a73SJeeja KP return skl_pcm_link_dma_prepare(ctx->dev, pipe->p_params); 508bb704a73SJeeja KP } 509bb704a73SJeeja KP 510bb704a73SJeeja KP return 0; 511bb704a73SJeeja KP } 512bb704a73SJeeja KP 513abb74003SJeeja KP /* 514e4e2d2f4SJeeja KP * Inside a pipe instance, we can have various modules. These modules need 515e4e2d2f4SJeeja KP * to instantiated in DSP by invoking INIT_MODULE IPC, which is achieved by 516e4e2d2f4SJeeja KP * skl_init_module() routine, so invoke that for all modules in a pipeline 517e4e2d2f4SJeeja KP */ 518e4e2d2f4SJeeja KP static int 519e4e2d2f4SJeeja KP skl_tplg_init_pipe_modules(struct skl *skl, struct skl_pipe *pipe) 520e4e2d2f4SJeeja KP { 521e4e2d2f4SJeeja KP struct skl_pipe_module *w_module; 522e4e2d2f4SJeeja KP struct snd_soc_dapm_widget *w; 523e4e2d2f4SJeeja KP struct skl_module_cfg *mconfig; 524e4e2d2f4SJeeja KP struct skl_sst *ctx = skl->skl_sst; 525e4e2d2f4SJeeja KP int ret = 0; 526e4e2d2f4SJeeja KP 527e4e2d2f4SJeeja KP list_for_each_entry(w_module, &pipe->w_list, node) { 528e4e2d2f4SJeeja KP w = w_module->w; 529e4e2d2f4SJeeja KP mconfig = w->priv; 530e4e2d2f4SJeeja KP 531b7c50555SVinod Koul /* check if module ids are populated */ 532b7c50555SVinod Koul if (mconfig->id.module_id < 0) { 533b7c50555SVinod Koul dev_err(skl->skl_sst->dev, 534a657ae7eSVinod Koul "module %pUL id not populated\n", 535a657ae7eSVinod Koul (uuid_le *)mconfig->guid); 536a657ae7eSVinod Koul return -EIO; 537b7c50555SVinod Koul } 538b7c50555SVinod Koul 539e4e2d2f4SJeeja KP /* check resource available */ 5409ba8ffefSDharageswari.R if (!skl_is_pipe_mcps_avail(skl, mconfig)) 541e4e2d2f4SJeeja KP return -ENOMEM; 542e4e2d2f4SJeeja KP 5436c5768b3SDharageswari R if (mconfig->is_loadable && ctx->dsp->fw_ops.load_mod) { 5446c5768b3SDharageswari R ret = ctx->dsp->fw_ops.load_mod(ctx->dsp, 5456c5768b3SDharageswari R mconfig->id.module_id, mconfig->guid); 5466c5768b3SDharageswari R if (ret < 0) 5476c5768b3SDharageswari R return ret; 548d643678bSJeeja KP 549d643678bSJeeja KP mconfig->m_state = SKL_MODULE_LOADED; 5506c5768b3SDharageswari R } 5516c5768b3SDharageswari R 552bb704a73SJeeja KP /* prepare the DMA if the module is gateway cpr */ 553bb704a73SJeeja KP ret = skl_tplg_module_prepare(ctx, pipe, w, mconfig); 554bb704a73SJeeja KP if (ret < 0) 555bb704a73SJeeja KP return ret; 556bb704a73SJeeja KP 5572d1419a3SJeeja KP /* update blob if blob is null for be with default value */ 5582d1419a3SJeeja KP skl_tplg_update_be_blob(w, ctx); 5592d1419a3SJeeja KP 560f7590d4fSJeeja KP /* 561f7590d4fSJeeja KP * apply fix/conversion to module params based on 562f7590d4fSJeeja KP * FE/BE params 563f7590d4fSJeeja KP */ 564f7590d4fSJeeja KP skl_tplg_update_module_params(w, ctx); 565ef2a352cSDharageswari R mconfig->id.pvt_id = skl_get_pvt_id(ctx, mconfig); 566ef2a352cSDharageswari R if (mconfig->id.pvt_id < 0) 567ef2a352cSDharageswari R return ret; 568abb74003SJeeja KP skl_tplg_set_module_init_data(w); 5699939a9c3SJeeja KP ret = skl_init_module(ctx, mconfig); 570ef2a352cSDharageswari R if (ret < 0) { 571ef2a352cSDharageswari R skl_put_pvt_id(ctx, mconfig); 572e4e2d2f4SJeeja KP return ret; 573ef2a352cSDharageswari R } 574260eb73aSDharageswari R skl_tplg_alloc_pipe_mcps(skl, mconfig); 575abb74003SJeeja KP ret = skl_tplg_set_module_params(w, ctx); 576e4e2d2f4SJeeja KP if (ret < 0) 577e4e2d2f4SJeeja KP return ret; 578e4e2d2f4SJeeja KP } 579e4e2d2f4SJeeja KP 580e4e2d2f4SJeeja KP return 0; 581e4e2d2f4SJeeja KP } 582d93f8e55SVinod Koul 5836c5768b3SDharageswari R static int skl_tplg_unload_pipe_modules(struct skl_sst *ctx, 5846c5768b3SDharageswari R struct skl_pipe *pipe) 5856c5768b3SDharageswari R { 586b0fab9c6SDharageswari R int ret; 5876c5768b3SDharageswari R struct skl_pipe_module *w_module = NULL; 5886c5768b3SDharageswari R struct skl_module_cfg *mconfig = NULL; 5896c5768b3SDharageswari R 5906c5768b3SDharageswari R list_for_each_entry(w_module, &pipe->w_list, node) { 5916c5768b3SDharageswari R mconfig = w_module->w->priv; 5926c5768b3SDharageswari R 593d643678bSJeeja KP if (mconfig->is_loadable && ctx->dsp->fw_ops.unload_mod && 594b0fab9c6SDharageswari R mconfig->m_state > SKL_MODULE_UNINIT) { 595b0fab9c6SDharageswari R ret = ctx->dsp->fw_ops.unload_mod(ctx->dsp, 5966c5768b3SDharageswari R mconfig->id.module_id); 597b0fab9c6SDharageswari R if (ret < 0) 598b0fab9c6SDharageswari R return -EIO; 599b0fab9c6SDharageswari R } 600ef2a352cSDharageswari R skl_put_pvt_id(ctx, mconfig); 6016c5768b3SDharageswari R } 6026c5768b3SDharageswari R 6036c5768b3SDharageswari R /* no modules to unload in this path, so return */ 6046c5768b3SDharageswari R return 0; 6056c5768b3SDharageswari R } 6066c5768b3SDharageswari R 607d93f8e55SVinod Koul /* 608d93f8e55SVinod Koul * Mixer module represents a pipeline. So in the Pre-PMU event of mixer we 609d93f8e55SVinod Koul * need create the pipeline. So we do following: 610d93f8e55SVinod Koul * - check the resources 611d93f8e55SVinod Koul * - Create the pipeline 612d93f8e55SVinod Koul * - Initialize the modules in pipeline 613d93f8e55SVinod Koul * - finally bind all modules together 614d93f8e55SVinod Koul */ 615d93f8e55SVinod Koul static int skl_tplg_mixer_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w, 616d93f8e55SVinod Koul struct skl *skl) 617d93f8e55SVinod Koul { 618d93f8e55SVinod Koul int ret; 619d93f8e55SVinod Koul struct skl_module_cfg *mconfig = w->priv; 620d93f8e55SVinod Koul struct skl_pipe_module *w_module; 621d93f8e55SVinod Koul struct skl_pipe *s_pipe = mconfig->pipe; 622d93f8e55SVinod Koul struct skl_module_cfg *src_module = NULL, *dst_module; 623d93f8e55SVinod Koul struct skl_sst *ctx = skl->skl_sst; 624d93f8e55SVinod Koul 625d93f8e55SVinod Koul /* check resource available */ 6269ba8ffefSDharageswari.R if (!skl_is_pipe_mcps_avail(skl, mconfig)) 627d93f8e55SVinod Koul return -EBUSY; 628d93f8e55SVinod Koul 6299ba8ffefSDharageswari.R if (!skl_is_pipe_mem_avail(skl, mconfig)) 630d93f8e55SVinod Koul return -ENOMEM; 631d93f8e55SVinod Koul 632d93f8e55SVinod Koul /* 633d93f8e55SVinod Koul * Create a list of modules for pipe. 634d93f8e55SVinod Koul * This list contains modules from source to sink 635d93f8e55SVinod Koul */ 636d93f8e55SVinod Koul ret = skl_create_pipeline(ctx, mconfig->pipe); 637d93f8e55SVinod Koul if (ret < 0) 638d93f8e55SVinod Koul return ret; 639d93f8e55SVinod Koul 640260eb73aSDharageswari R skl_tplg_alloc_pipe_mem(skl, mconfig); 641260eb73aSDharageswari R skl_tplg_alloc_pipe_mcps(skl, mconfig); 642d93f8e55SVinod Koul 643d93f8e55SVinod Koul /* Init all pipe modules from source to sink */ 644d93f8e55SVinod Koul ret = skl_tplg_init_pipe_modules(skl, s_pipe); 645d93f8e55SVinod Koul if (ret < 0) 646d93f8e55SVinod Koul return ret; 647d93f8e55SVinod Koul 648d93f8e55SVinod Koul /* Bind modules from source to sink */ 649d93f8e55SVinod Koul list_for_each_entry(w_module, &s_pipe->w_list, node) { 650d93f8e55SVinod Koul dst_module = w_module->w->priv; 651d93f8e55SVinod Koul 652d93f8e55SVinod Koul if (src_module == NULL) { 653d93f8e55SVinod Koul src_module = dst_module; 654d93f8e55SVinod Koul continue; 655d93f8e55SVinod Koul } 656d93f8e55SVinod Koul 657d93f8e55SVinod Koul ret = skl_bind_modules(ctx, src_module, dst_module); 658d93f8e55SVinod Koul if (ret < 0) 659d93f8e55SVinod Koul return ret; 660d93f8e55SVinod Koul 661d93f8e55SVinod Koul src_module = dst_module; 662d93f8e55SVinod Koul } 663d93f8e55SVinod Koul 664d93f8e55SVinod Koul return 0; 665d93f8e55SVinod Koul } 666d93f8e55SVinod Koul 6675e8f0ee4SDharageswari R static int skl_fill_sink_instance_id(struct skl_sst *ctx, 6685e8f0ee4SDharageswari R struct skl_algo_data *alg_data) 6695e8f0ee4SDharageswari R { 6705e8f0ee4SDharageswari R struct skl_kpb_params *params = (struct skl_kpb_params *)alg_data->params; 6715e8f0ee4SDharageswari R struct skl_mod_inst_map *inst; 6725e8f0ee4SDharageswari R int i, pvt_id; 6735e8f0ee4SDharageswari R 6745e8f0ee4SDharageswari R inst = params->map; 6755e8f0ee4SDharageswari R 6765e8f0ee4SDharageswari R for (i = 0; i < params->num_modules; i++) { 6775e8f0ee4SDharageswari R pvt_id = skl_get_pvt_instance_id_map(ctx, 6785e8f0ee4SDharageswari R inst->mod_id, inst->inst_id); 6795e8f0ee4SDharageswari R if (pvt_id < 0) 6805e8f0ee4SDharageswari R return -EINVAL; 6815e8f0ee4SDharageswari R inst->inst_id = pvt_id; 6825e8f0ee4SDharageswari R inst++; 6835e8f0ee4SDharageswari R } 6845e8f0ee4SDharageswari R return 0; 6855e8f0ee4SDharageswari R } 6865e8f0ee4SDharageswari R 687cc6a4044SJeeja KP /* 688cc6a4044SJeeja KP * Some modules require params to be set after the module is bound to 689cc6a4044SJeeja KP * all pins connected. 690cc6a4044SJeeja KP * 691cc6a4044SJeeja KP * The module provider initializes set_param flag for such modules and we 692cc6a4044SJeeja KP * send params after binding 693cc6a4044SJeeja KP */ 694cc6a4044SJeeja KP static int skl_tplg_set_module_bind_params(struct snd_soc_dapm_widget *w, 695cc6a4044SJeeja KP struct skl_module_cfg *mcfg, struct skl_sst *ctx) 696cc6a4044SJeeja KP { 697cc6a4044SJeeja KP int i, ret; 698cc6a4044SJeeja KP struct skl_module_cfg *mconfig = w->priv; 699cc6a4044SJeeja KP const struct snd_kcontrol_new *k; 700cc6a4044SJeeja KP struct soc_bytes_ext *sb; 701cc6a4044SJeeja KP struct skl_algo_data *bc; 702cc6a4044SJeeja KP struct skl_specific_cfg *sp_cfg; 703cc6a4044SJeeja KP 704cc6a4044SJeeja KP /* 705cc6a4044SJeeja KP * check all out/in pins are in bind state. 706cc6a4044SJeeja KP * if so set the module param 707cc6a4044SJeeja KP */ 708cc6a4044SJeeja KP for (i = 0; i < mcfg->max_out_queue; i++) { 709cc6a4044SJeeja KP if (mcfg->m_out_pin[i].pin_state != SKL_PIN_BIND_DONE) 710cc6a4044SJeeja KP return 0; 711cc6a4044SJeeja KP } 712cc6a4044SJeeja KP 713cc6a4044SJeeja KP for (i = 0; i < mcfg->max_in_queue; i++) { 714cc6a4044SJeeja KP if (mcfg->m_in_pin[i].pin_state != SKL_PIN_BIND_DONE) 715cc6a4044SJeeja KP return 0; 716cc6a4044SJeeja KP } 717cc6a4044SJeeja KP 718cc6a4044SJeeja KP if (mconfig->formats_config.caps_size > 0 && 719cc6a4044SJeeja KP mconfig->formats_config.set_params == SKL_PARAM_BIND) { 720cc6a4044SJeeja KP sp_cfg = &mconfig->formats_config; 721cc6a4044SJeeja KP ret = skl_set_module_params(ctx, sp_cfg->caps, 722cc6a4044SJeeja KP sp_cfg->caps_size, 723cc6a4044SJeeja KP sp_cfg->param_id, mconfig); 724cc6a4044SJeeja KP if (ret < 0) 725cc6a4044SJeeja KP return ret; 726cc6a4044SJeeja KP } 727cc6a4044SJeeja KP 728cc6a4044SJeeja KP for (i = 0; i < w->num_kcontrols; i++) { 729cc6a4044SJeeja KP k = &w->kcontrol_news[i]; 730cc6a4044SJeeja KP if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { 731cc6a4044SJeeja KP sb = (void *) k->private_value; 732cc6a4044SJeeja KP bc = (struct skl_algo_data *)sb->dobj.private; 733cc6a4044SJeeja KP 734cc6a4044SJeeja KP if (bc->set_params == SKL_PARAM_BIND) { 7355e8f0ee4SDharageswari R if (mconfig->m_type == SKL_MODULE_TYPE_KPB) 7365e8f0ee4SDharageswari R skl_fill_sink_instance_id(ctx, bc); 737cc6a4044SJeeja KP ret = skl_set_module_params(ctx, 738cc6a4044SJeeja KP (u32 *)bc->params, bc->max, 739cc6a4044SJeeja KP bc->param_id, mconfig); 740cc6a4044SJeeja KP if (ret < 0) 741cc6a4044SJeeja KP return ret; 742cc6a4044SJeeja KP } 743cc6a4044SJeeja KP } 744cc6a4044SJeeja KP } 745cc6a4044SJeeja KP 746cc6a4044SJeeja KP return 0; 747cc6a4044SJeeja KP } 748cc6a4044SJeeja KP 7498724ff17SJeeja KP static int skl_tplg_bind_sinks(struct snd_soc_dapm_widget *w, 7508724ff17SJeeja KP struct skl *skl, 7516bd4cf85SJeeja KP struct snd_soc_dapm_widget *src_w, 7528724ff17SJeeja KP struct skl_module_cfg *src_mconfig) 753d93f8e55SVinod Koul { 754d93f8e55SVinod Koul struct snd_soc_dapm_path *p; 7550ed95d76SJeeja KP struct snd_soc_dapm_widget *sink = NULL, *next_sink = NULL; 7568724ff17SJeeja KP struct skl_module_cfg *sink_mconfig; 757d93f8e55SVinod Koul struct skl_sst *ctx = skl->skl_sst; 7588724ff17SJeeja KP int ret; 759d93f8e55SVinod Koul 7608724ff17SJeeja KP snd_soc_dapm_widget_for_each_sink_path(w, p) { 761d93f8e55SVinod Koul if (!p->connect) 762d93f8e55SVinod Koul continue; 763d93f8e55SVinod Koul 764d93f8e55SVinod Koul dev_dbg(ctx->dev, "%s: src widget=%s\n", __func__, w->name); 765d93f8e55SVinod Koul dev_dbg(ctx->dev, "%s: sink widget=%s\n", __func__, p->sink->name); 766d93f8e55SVinod Koul 7670ed95d76SJeeja KP next_sink = p->sink; 7686bd4cf85SJeeja KP 7696bd4cf85SJeeja KP if (!is_skl_dsp_widget_type(p->sink)) 7706bd4cf85SJeeja KP return skl_tplg_bind_sinks(p->sink, skl, src_w, src_mconfig); 7716bd4cf85SJeeja KP 772d93f8e55SVinod Koul /* 773d93f8e55SVinod Koul * here we will check widgets in sink pipelines, so that 774d93f8e55SVinod Koul * can be any widgets type and we are only interested if 775d93f8e55SVinod Koul * they are ones used for SKL so check that first 776d93f8e55SVinod Koul */ 777d93f8e55SVinod Koul if ((p->sink->priv != NULL) && 778d93f8e55SVinod Koul is_skl_dsp_widget_type(p->sink)) { 779d93f8e55SVinod Koul 780d93f8e55SVinod Koul sink = p->sink; 781d93f8e55SVinod Koul sink_mconfig = sink->priv; 782d93f8e55SVinod Koul 783cc6a4044SJeeja KP if (src_mconfig->m_state == SKL_MODULE_UNINIT || 784cc6a4044SJeeja KP sink_mconfig->m_state == SKL_MODULE_UNINIT) 785cc6a4044SJeeja KP continue; 786cc6a4044SJeeja KP 787d93f8e55SVinod Koul /* Bind source to sink, mixin is always source */ 788d93f8e55SVinod Koul ret = skl_bind_modules(ctx, src_mconfig, sink_mconfig); 789d93f8e55SVinod Koul if (ret) 790d93f8e55SVinod Koul return ret; 791d93f8e55SVinod Koul 792cc6a4044SJeeja KP /* set module params after bind */ 793cc6a4044SJeeja KP skl_tplg_set_module_bind_params(src_w, src_mconfig, ctx); 794cc6a4044SJeeja KP skl_tplg_set_module_bind_params(sink, sink_mconfig, ctx); 795cc6a4044SJeeja KP 796d93f8e55SVinod Koul /* Start sinks pipe first */ 797d93f8e55SVinod Koul if (sink_mconfig->pipe->state != SKL_PIPE_STARTED) { 798d1730c3dSJeeja KP if (sink_mconfig->pipe->conn_type != 799d1730c3dSJeeja KP SKL_PIPE_CONN_TYPE_FE) 800d1730c3dSJeeja KP ret = skl_run_pipe(ctx, 801d1730c3dSJeeja KP sink_mconfig->pipe); 802d93f8e55SVinod Koul if (ret) 803d93f8e55SVinod Koul return ret; 804d93f8e55SVinod Koul } 805d93f8e55SVinod Koul } 806d93f8e55SVinod Koul } 807d93f8e55SVinod Koul 8088724ff17SJeeja KP if (!sink) 8096bd4cf85SJeeja KP return skl_tplg_bind_sinks(next_sink, skl, src_w, src_mconfig); 8108724ff17SJeeja KP 8118724ff17SJeeja KP return 0; 8128724ff17SJeeja KP } 8138724ff17SJeeja KP 814d93f8e55SVinod Koul /* 815d93f8e55SVinod Koul * A PGA represents a module in a pipeline. So in the Pre-PMU event of PGA 816d93f8e55SVinod Koul * we need to do following: 817d93f8e55SVinod Koul * - Bind to sink pipeline 818d93f8e55SVinod Koul * Since the sink pipes can be running and we don't get mixer event on 819d93f8e55SVinod Koul * connect for already running mixer, we need to find the sink pipes 820d93f8e55SVinod Koul * here and bind to them. This way dynamic connect works. 821d93f8e55SVinod Koul * - Start sink pipeline, if not running 822d93f8e55SVinod Koul * - Then run current pipe 823d93f8e55SVinod Koul */ 824d93f8e55SVinod Koul static int skl_tplg_pga_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w, 825d93f8e55SVinod Koul struct skl *skl) 826d93f8e55SVinod Koul { 8278724ff17SJeeja KP struct skl_module_cfg *src_mconfig; 828d93f8e55SVinod Koul struct skl_sst *ctx = skl->skl_sst; 829d93f8e55SVinod Koul int ret = 0; 830d93f8e55SVinod Koul 8318724ff17SJeeja KP src_mconfig = w->priv; 832d93f8e55SVinod Koul 833d93f8e55SVinod Koul /* 834d93f8e55SVinod Koul * find which sink it is connected to, bind with the sink, 835d93f8e55SVinod Koul * if sink is not started, start sink pipe first, then start 836d93f8e55SVinod Koul * this pipe 837d93f8e55SVinod Koul */ 8386bd4cf85SJeeja KP ret = skl_tplg_bind_sinks(w, skl, w, src_mconfig); 8398724ff17SJeeja KP if (ret) 8408724ff17SJeeja KP return ret; 8418724ff17SJeeja KP 842d93f8e55SVinod Koul /* Start source pipe last after starting all sinks */ 843d1730c3dSJeeja KP if (src_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE) 844d1730c3dSJeeja KP return skl_run_pipe(ctx, src_mconfig->pipe); 845d93f8e55SVinod Koul 846d93f8e55SVinod Koul return 0; 847d93f8e55SVinod Koul } 848d93f8e55SVinod Koul 8498724ff17SJeeja KP static struct snd_soc_dapm_widget *skl_get_src_dsp_widget( 8508724ff17SJeeja KP struct snd_soc_dapm_widget *w, struct skl *skl) 8518724ff17SJeeja KP { 8528724ff17SJeeja KP struct snd_soc_dapm_path *p; 8538724ff17SJeeja KP struct snd_soc_dapm_widget *src_w = NULL; 8548724ff17SJeeja KP struct skl_sst *ctx = skl->skl_sst; 8558724ff17SJeeja KP 856d93f8e55SVinod Koul snd_soc_dapm_widget_for_each_source_path(w, p) { 8578724ff17SJeeja KP src_w = p->source; 858d93f8e55SVinod Koul if (!p->connect) 859d93f8e55SVinod Koul continue; 860d93f8e55SVinod Koul 8618724ff17SJeeja KP dev_dbg(ctx->dev, "sink widget=%s\n", w->name); 8628724ff17SJeeja KP dev_dbg(ctx->dev, "src widget=%s\n", p->source->name); 863d93f8e55SVinod Koul 864d93f8e55SVinod Koul /* 8658724ff17SJeeja KP * here we will check widgets in sink pipelines, so that can 8668724ff17SJeeja KP * be any widgets type and we are only interested if they are 8678724ff17SJeeja KP * ones used for SKL so check that first 868d93f8e55SVinod Koul */ 8698724ff17SJeeja KP if ((p->source->priv != NULL) && 8708724ff17SJeeja KP is_skl_dsp_widget_type(p->source)) { 8718724ff17SJeeja KP return p->source; 872d93f8e55SVinod Koul } 873d93f8e55SVinod Koul } 874d93f8e55SVinod Koul 8758724ff17SJeeja KP if (src_w != NULL) 8768724ff17SJeeja KP return skl_get_src_dsp_widget(src_w, skl); 877d93f8e55SVinod Koul 8788724ff17SJeeja KP return NULL; 879d93f8e55SVinod Koul } 880d93f8e55SVinod Koul 881d93f8e55SVinod Koul /* 882d93f8e55SVinod Koul * in the Post-PMU event of mixer we need to do following: 883d93f8e55SVinod Koul * - Check if this pipe is running 884d93f8e55SVinod Koul * - if not, then 885d93f8e55SVinod Koul * - bind this pipeline to its source pipeline 886d93f8e55SVinod Koul * if source pipe is already running, this means it is a dynamic 887d93f8e55SVinod Koul * connection and we need to bind only to that pipe 888d93f8e55SVinod Koul * - start this pipeline 889d93f8e55SVinod Koul */ 890d93f8e55SVinod Koul static int skl_tplg_mixer_dapm_post_pmu_event(struct snd_soc_dapm_widget *w, 891d93f8e55SVinod Koul struct skl *skl) 892d93f8e55SVinod Koul { 893d93f8e55SVinod Koul int ret = 0; 894d93f8e55SVinod Koul struct snd_soc_dapm_widget *source, *sink; 895d93f8e55SVinod Koul struct skl_module_cfg *src_mconfig, *sink_mconfig; 896d93f8e55SVinod Koul struct skl_sst *ctx = skl->skl_sst; 897d93f8e55SVinod Koul int src_pipe_started = 0; 898d93f8e55SVinod Koul 899d93f8e55SVinod Koul sink = w; 900d93f8e55SVinod Koul sink_mconfig = sink->priv; 901d93f8e55SVinod Koul 902d93f8e55SVinod Koul /* 903d93f8e55SVinod Koul * If source pipe is already started, that means source is driving 904d93f8e55SVinod Koul * one more sink before this sink got connected, Since source is 905d93f8e55SVinod Koul * started, bind this sink to source and start this pipe. 906d93f8e55SVinod Koul */ 9078724ff17SJeeja KP source = skl_get_src_dsp_widget(w, skl); 9088724ff17SJeeja KP if (source != NULL) { 909d93f8e55SVinod Koul src_mconfig = source->priv; 910d93f8e55SVinod Koul sink_mconfig = sink->priv; 911d93f8e55SVinod Koul src_pipe_started = 1; 912d93f8e55SVinod Koul 913d93f8e55SVinod Koul /* 9148724ff17SJeeja KP * check pipe state, then no need to bind or start the 9158724ff17SJeeja KP * pipe 916d93f8e55SVinod Koul */ 917d93f8e55SVinod Koul if (src_mconfig->pipe->state != SKL_PIPE_STARTED) 918d93f8e55SVinod Koul src_pipe_started = 0; 919d93f8e55SVinod Koul } 920d93f8e55SVinod Koul 921d93f8e55SVinod Koul if (src_pipe_started) { 922d93f8e55SVinod Koul ret = skl_bind_modules(ctx, src_mconfig, sink_mconfig); 923d93f8e55SVinod Koul if (ret) 924d93f8e55SVinod Koul return ret; 925d93f8e55SVinod Koul 926cc6a4044SJeeja KP /* set module params after bind */ 927cc6a4044SJeeja KP skl_tplg_set_module_bind_params(source, src_mconfig, ctx); 928cc6a4044SJeeja KP skl_tplg_set_module_bind_params(sink, sink_mconfig, ctx); 929cc6a4044SJeeja KP 930d1730c3dSJeeja KP if (sink_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE) 931d93f8e55SVinod Koul ret = skl_run_pipe(ctx, sink_mconfig->pipe); 932d93f8e55SVinod Koul } 933d93f8e55SVinod Koul 934d93f8e55SVinod Koul return ret; 935d93f8e55SVinod Koul } 936d93f8e55SVinod Koul 937d93f8e55SVinod Koul /* 938d93f8e55SVinod Koul * in the Pre-PMD event of mixer we need to do following: 939d93f8e55SVinod Koul * - Stop the pipe 940d93f8e55SVinod Koul * - find the source connections and remove that from dapm_path_list 941d93f8e55SVinod Koul * - unbind with source pipelines if still connected 942d93f8e55SVinod Koul */ 943d93f8e55SVinod Koul static int skl_tplg_mixer_dapm_pre_pmd_event(struct snd_soc_dapm_widget *w, 944d93f8e55SVinod Koul struct skl *skl) 945d93f8e55SVinod Koul { 946d93f8e55SVinod Koul struct skl_module_cfg *src_mconfig, *sink_mconfig; 947ce1b5551SJeeja KP int ret = 0, i; 948d93f8e55SVinod Koul struct skl_sst *ctx = skl->skl_sst; 949d93f8e55SVinod Koul 950ce1b5551SJeeja KP sink_mconfig = w->priv; 951d93f8e55SVinod Koul 952d93f8e55SVinod Koul /* Stop the pipe */ 953d93f8e55SVinod Koul ret = skl_stop_pipe(ctx, sink_mconfig->pipe); 954d93f8e55SVinod Koul if (ret) 955d93f8e55SVinod Koul return ret; 956d93f8e55SVinod Koul 957ce1b5551SJeeja KP for (i = 0; i < sink_mconfig->max_in_queue; i++) { 958ce1b5551SJeeja KP if (sink_mconfig->m_in_pin[i].pin_state == SKL_PIN_BIND_DONE) { 959ce1b5551SJeeja KP src_mconfig = sink_mconfig->m_in_pin[i].tgt_mcfg; 960ce1b5551SJeeja KP if (!src_mconfig) 961ce1b5551SJeeja KP continue; 962d93f8e55SVinod Koul /* 963ce1b5551SJeeja KP * If path_found == 1, that means pmd for source 964ce1b5551SJeeja KP * pipe has not occurred, source is connected to 965ce1b5551SJeeja KP * some other sink. so its responsibility of sink 966ce1b5551SJeeja KP * to unbind itself from source. 967d93f8e55SVinod Koul */ 968d93f8e55SVinod Koul ret = skl_stop_pipe(ctx, src_mconfig->pipe); 969d93f8e55SVinod Koul if (ret < 0) 970d93f8e55SVinod Koul return ret; 971d93f8e55SVinod Koul 972ce1b5551SJeeja KP ret = skl_unbind_modules(ctx, 973ce1b5551SJeeja KP src_mconfig, sink_mconfig); 974ce1b5551SJeeja KP } 975d93f8e55SVinod Koul } 976d93f8e55SVinod Koul 977d93f8e55SVinod Koul return ret; 978d93f8e55SVinod Koul } 979d93f8e55SVinod Koul 980d93f8e55SVinod Koul /* 981d93f8e55SVinod Koul * in the Post-PMD event of mixer we need to do following: 982d93f8e55SVinod Koul * - Free the mcps used 983d93f8e55SVinod Koul * - Free the mem used 984d93f8e55SVinod Koul * - Unbind the modules within the pipeline 985d93f8e55SVinod Koul * - Delete the pipeline (modules are not required to be explicitly 986d93f8e55SVinod Koul * deleted, pipeline delete is enough here 987d93f8e55SVinod Koul */ 988d93f8e55SVinod Koul static int skl_tplg_mixer_dapm_post_pmd_event(struct snd_soc_dapm_widget *w, 989d93f8e55SVinod Koul struct skl *skl) 990d93f8e55SVinod Koul { 991d93f8e55SVinod Koul struct skl_module_cfg *mconfig = w->priv; 992d93f8e55SVinod Koul struct skl_pipe_module *w_module; 993d93f8e55SVinod Koul struct skl_module_cfg *src_module = NULL, *dst_module; 994d93f8e55SVinod Koul struct skl_sst *ctx = skl->skl_sst; 995d93f8e55SVinod Koul struct skl_pipe *s_pipe = mconfig->pipe; 996d93f8e55SVinod Koul 997260eb73aSDharageswari R if (s_pipe->state == SKL_PIPE_INVALID) 998260eb73aSDharageswari R return -EINVAL; 999260eb73aSDharageswari R 1000d93f8e55SVinod Koul skl_tplg_free_pipe_mcps(skl, mconfig); 100165976878SVinod Koul skl_tplg_free_pipe_mem(skl, mconfig); 1002d93f8e55SVinod Koul 1003d93f8e55SVinod Koul list_for_each_entry(w_module, &s_pipe->w_list, node) { 1004d93f8e55SVinod Koul dst_module = w_module->w->priv; 1005d93f8e55SVinod Koul 1006260eb73aSDharageswari R if (mconfig->m_state >= SKL_MODULE_INIT_DONE) 10077ae3cb15SVinod Koul skl_tplg_free_pipe_mcps(skl, dst_module); 1008d93f8e55SVinod Koul if (src_module == NULL) { 1009d93f8e55SVinod Koul src_module = dst_module; 1010d93f8e55SVinod Koul continue; 1011d93f8e55SVinod Koul } 1012d93f8e55SVinod Koul 10137ca42f5aSGuneshwor Singh skl_unbind_modules(ctx, src_module, dst_module); 1014d93f8e55SVinod Koul src_module = dst_module; 1015d93f8e55SVinod Koul } 1016d93f8e55SVinod Koul 1017547cafa3SVinod Koul skl_delete_pipe(ctx, mconfig->pipe); 1018d93f8e55SVinod Koul 10196c5768b3SDharageswari R return skl_tplg_unload_pipe_modules(ctx, s_pipe); 1020d93f8e55SVinod Koul } 1021d93f8e55SVinod Koul 1022d93f8e55SVinod Koul /* 1023d93f8e55SVinod Koul * in the Post-PMD event of PGA we need to do following: 1024d93f8e55SVinod Koul * - Free the mcps used 1025d93f8e55SVinod Koul * - Stop the pipeline 1026d93f8e55SVinod Koul * - In source pipe is connected, unbind with source pipelines 1027d93f8e55SVinod Koul */ 1028d93f8e55SVinod Koul static int skl_tplg_pga_dapm_post_pmd_event(struct snd_soc_dapm_widget *w, 1029d93f8e55SVinod Koul struct skl *skl) 1030d93f8e55SVinod Koul { 1031d93f8e55SVinod Koul struct skl_module_cfg *src_mconfig, *sink_mconfig; 1032ce1b5551SJeeja KP int ret = 0, i; 1033d93f8e55SVinod Koul struct skl_sst *ctx = skl->skl_sst; 1034d93f8e55SVinod Koul 1035ce1b5551SJeeja KP src_mconfig = w->priv; 1036d93f8e55SVinod Koul 1037d93f8e55SVinod Koul /* Stop the pipe since this is a mixin module */ 1038d93f8e55SVinod Koul ret = skl_stop_pipe(ctx, src_mconfig->pipe); 1039d93f8e55SVinod Koul if (ret) 1040d93f8e55SVinod Koul return ret; 1041d93f8e55SVinod Koul 1042ce1b5551SJeeja KP for (i = 0; i < src_mconfig->max_out_queue; i++) { 1043ce1b5551SJeeja KP if (src_mconfig->m_out_pin[i].pin_state == SKL_PIN_BIND_DONE) { 1044ce1b5551SJeeja KP sink_mconfig = src_mconfig->m_out_pin[i].tgt_mcfg; 1045ce1b5551SJeeja KP if (!sink_mconfig) 1046ce1b5551SJeeja KP continue; 1047d93f8e55SVinod Koul /* 1048ce1b5551SJeeja KP * This is a connecter and if path is found that means 1049d93f8e55SVinod Koul * unbind between source and sink has not happened yet 1050d93f8e55SVinod Koul */ 1051ce1b5551SJeeja KP ret = skl_unbind_modules(ctx, src_mconfig, 1052ce1b5551SJeeja KP sink_mconfig); 1053ce1b5551SJeeja KP } 1054d93f8e55SVinod Koul } 1055d93f8e55SVinod Koul 1056d93f8e55SVinod Koul return ret; 1057d93f8e55SVinod Koul } 1058d93f8e55SVinod Koul 1059d93f8e55SVinod Koul /* 1060d93f8e55SVinod Koul * In modelling, we assume there will be ONLY one mixer in a pipeline. If 1061d93f8e55SVinod Koul * mixer is not required then it is treated as static mixer aka vmixer with 1062d93f8e55SVinod Koul * a hard path to source module 1063d93f8e55SVinod Koul * So we don't need to check if source is started or not as hard path puts 1064d93f8e55SVinod Koul * dependency on each other 1065d93f8e55SVinod Koul */ 1066d93f8e55SVinod Koul static int skl_tplg_vmixer_event(struct snd_soc_dapm_widget *w, 1067d93f8e55SVinod Koul struct snd_kcontrol *k, int event) 1068d93f8e55SVinod Koul { 1069d93f8e55SVinod Koul struct snd_soc_dapm_context *dapm = w->dapm; 1070d93f8e55SVinod Koul struct skl *skl = get_skl_ctx(dapm->dev); 1071d93f8e55SVinod Koul 1072d93f8e55SVinod Koul switch (event) { 1073d93f8e55SVinod Koul case SND_SOC_DAPM_PRE_PMU: 1074d93f8e55SVinod Koul return skl_tplg_mixer_dapm_pre_pmu_event(w, skl); 1075d93f8e55SVinod Koul 1076de1fedf2SJeeja KP case SND_SOC_DAPM_POST_PMU: 1077de1fedf2SJeeja KP return skl_tplg_mixer_dapm_post_pmu_event(w, skl); 1078de1fedf2SJeeja KP 1079de1fedf2SJeeja KP case SND_SOC_DAPM_PRE_PMD: 1080de1fedf2SJeeja KP return skl_tplg_mixer_dapm_pre_pmd_event(w, skl); 1081de1fedf2SJeeja KP 1082d93f8e55SVinod Koul case SND_SOC_DAPM_POST_PMD: 1083d93f8e55SVinod Koul return skl_tplg_mixer_dapm_post_pmd_event(w, skl); 1084d93f8e55SVinod Koul } 1085d93f8e55SVinod Koul 1086d93f8e55SVinod Koul return 0; 1087d93f8e55SVinod Koul } 1088d93f8e55SVinod Koul 1089d93f8e55SVinod Koul /* 1090d93f8e55SVinod Koul * In modelling, we assume there will be ONLY one mixer in a pipeline. If a 1091d93f8e55SVinod Koul * second one is required that is created as another pipe entity. 1092d93f8e55SVinod Koul * The mixer is responsible for pipe management and represent a pipeline 1093d93f8e55SVinod Koul * instance 1094d93f8e55SVinod Koul */ 1095d93f8e55SVinod Koul static int skl_tplg_mixer_event(struct snd_soc_dapm_widget *w, 1096d93f8e55SVinod Koul struct snd_kcontrol *k, int event) 1097d93f8e55SVinod Koul { 1098d93f8e55SVinod Koul struct snd_soc_dapm_context *dapm = w->dapm; 1099d93f8e55SVinod Koul struct skl *skl = get_skl_ctx(dapm->dev); 1100d93f8e55SVinod Koul 1101d93f8e55SVinod Koul switch (event) { 1102d93f8e55SVinod Koul case SND_SOC_DAPM_PRE_PMU: 1103d93f8e55SVinod Koul return skl_tplg_mixer_dapm_pre_pmu_event(w, skl); 1104d93f8e55SVinod Koul 1105d93f8e55SVinod Koul case SND_SOC_DAPM_POST_PMU: 1106d93f8e55SVinod Koul return skl_tplg_mixer_dapm_post_pmu_event(w, skl); 1107d93f8e55SVinod Koul 1108d93f8e55SVinod Koul case SND_SOC_DAPM_PRE_PMD: 1109d93f8e55SVinod Koul return skl_tplg_mixer_dapm_pre_pmd_event(w, skl); 1110d93f8e55SVinod Koul 1111d93f8e55SVinod Koul case SND_SOC_DAPM_POST_PMD: 1112d93f8e55SVinod Koul return skl_tplg_mixer_dapm_post_pmd_event(w, skl); 1113d93f8e55SVinod Koul } 1114d93f8e55SVinod Koul 1115d93f8e55SVinod Koul return 0; 1116d93f8e55SVinod Koul } 1117d93f8e55SVinod Koul 1118d93f8e55SVinod Koul /* 1119d93f8e55SVinod Koul * In modelling, we assumed rest of the modules in pipeline are PGA. But we 1120d93f8e55SVinod Koul * are interested in last PGA (leaf PGA) in a pipeline to disconnect with 1121d93f8e55SVinod Koul * the sink when it is running (two FE to one BE or one FE to two BE) 1122d93f8e55SVinod Koul * scenarios 1123d93f8e55SVinod Koul */ 1124d93f8e55SVinod Koul static int skl_tplg_pga_event(struct snd_soc_dapm_widget *w, 1125d93f8e55SVinod Koul struct snd_kcontrol *k, int event) 1126d93f8e55SVinod Koul 1127d93f8e55SVinod Koul { 1128d93f8e55SVinod Koul struct snd_soc_dapm_context *dapm = w->dapm; 1129d93f8e55SVinod Koul struct skl *skl = get_skl_ctx(dapm->dev); 1130d93f8e55SVinod Koul 1131d93f8e55SVinod Koul switch (event) { 1132d93f8e55SVinod Koul case SND_SOC_DAPM_PRE_PMU: 1133d93f8e55SVinod Koul return skl_tplg_pga_dapm_pre_pmu_event(w, skl); 1134d93f8e55SVinod Koul 1135d93f8e55SVinod Koul case SND_SOC_DAPM_POST_PMD: 1136d93f8e55SVinod Koul return skl_tplg_pga_dapm_post_pmd_event(w, skl); 1137d93f8e55SVinod Koul } 1138d93f8e55SVinod Koul 1139d93f8e55SVinod Koul return 0; 1140d93f8e55SVinod Koul } 1141cfb0a873SVinod Koul 1142140adfbaSJeeja KP static int skl_tplg_tlv_control_get(struct snd_kcontrol *kcontrol, 1143140adfbaSJeeja KP unsigned int __user *data, unsigned int size) 1144140adfbaSJeeja KP { 1145140adfbaSJeeja KP struct soc_bytes_ext *sb = 1146140adfbaSJeeja KP (struct soc_bytes_ext *)kcontrol->private_value; 1147140adfbaSJeeja KP struct skl_algo_data *bc = (struct skl_algo_data *)sb->dobj.private; 11487d9f2911SOmair M Abdullah struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol); 11497d9f2911SOmair M Abdullah struct skl_module_cfg *mconfig = w->priv; 11507d9f2911SOmair M Abdullah struct skl *skl = get_skl_ctx(w->dapm->dev); 11517d9f2911SOmair M Abdullah 11527d9f2911SOmair M Abdullah if (w->power) 11537d9f2911SOmair M Abdullah skl_get_module_params(skl->skl_sst, (u32 *)bc->params, 11540d682104SDharageswari R bc->size, bc->param_id, mconfig); 1155140adfbaSJeeja KP 115641556f68SVinod Koul /* decrement size for TLV header */ 115741556f68SVinod Koul size -= 2 * sizeof(u32); 115841556f68SVinod Koul 115941556f68SVinod Koul /* check size as we don't want to send kernel data */ 116041556f68SVinod Koul if (size > bc->max) 116141556f68SVinod Koul size = bc->max; 116241556f68SVinod Koul 1163140adfbaSJeeja KP if (bc->params) { 1164140adfbaSJeeja KP if (copy_to_user(data, &bc->param_id, sizeof(u32))) 1165140adfbaSJeeja KP return -EFAULT; 1166e8bc3c99SDan Carpenter if (copy_to_user(data + 1, &size, sizeof(u32))) 1167140adfbaSJeeja KP return -EFAULT; 1168e8bc3c99SDan Carpenter if (copy_to_user(data + 2, bc->params, size)) 1169140adfbaSJeeja KP return -EFAULT; 1170140adfbaSJeeja KP } 1171140adfbaSJeeja KP 1172140adfbaSJeeja KP return 0; 1173140adfbaSJeeja KP } 1174140adfbaSJeeja KP 1175140adfbaSJeeja KP #define SKL_PARAM_VENDOR_ID 0xff 1176140adfbaSJeeja KP 1177140adfbaSJeeja KP static int skl_tplg_tlv_control_set(struct snd_kcontrol *kcontrol, 1178140adfbaSJeeja KP const unsigned int __user *data, unsigned int size) 1179140adfbaSJeeja KP { 1180140adfbaSJeeja KP struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol); 1181140adfbaSJeeja KP struct skl_module_cfg *mconfig = w->priv; 1182140adfbaSJeeja KP struct soc_bytes_ext *sb = 1183140adfbaSJeeja KP (struct soc_bytes_ext *)kcontrol->private_value; 1184140adfbaSJeeja KP struct skl_algo_data *ac = (struct skl_algo_data *)sb->dobj.private; 1185140adfbaSJeeja KP struct skl *skl = get_skl_ctx(w->dapm->dev); 1186140adfbaSJeeja KP 1187140adfbaSJeeja KP if (ac->params) { 11880d682104SDharageswari R if (size > ac->max) 11890d682104SDharageswari R return -EINVAL; 11900d682104SDharageswari R 11910d682104SDharageswari R ac->size = size; 1192140adfbaSJeeja KP /* 1193140adfbaSJeeja KP * if the param_is is of type Vendor, firmware expects actual 1194140adfbaSJeeja KP * parameter id and size from the control. 1195140adfbaSJeeja KP */ 1196140adfbaSJeeja KP if (ac->param_id == SKL_PARAM_VENDOR_ID) { 1197140adfbaSJeeja KP if (copy_from_user(ac->params, data, size)) 1198140adfbaSJeeja KP return -EFAULT; 1199140adfbaSJeeja KP } else { 1200140adfbaSJeeja KP if (copy_from_user(ac->params, 120165b4bcb8SAlan data + 2, size)) 1202140adfbaSJeeja KP return -EFAULT; 1203140adfbaSJeeja KP } 1204140adfbaSJeeja KP 1205140adfbaSJeeja KP if (w->power) 1206140adfbaSJeeja KP return skl_set_module_params(skl->skl_sst, 12070d682104SDharageswari R (u32 *)ac->params, ac->size, 1208140adfbaSJeeja KP ac->param_id, mconfig); 1209140adfbaSJeeja KP } 1210140adfbaSJeeja KP 1211140adfbaSJeeja KP return 0; 1212140adfbaSJeeja KP } 1213140adfbaSJeeja KP 1214cfb0a873SVinod Koul /* 12158871dcb9SJeeja KP * Fill the dma id for host and link. In case of passthrough 12168871dcb9SJeeja KP * pipeline, this will both host and link in the same 12178871dcb9SJeeja KP * pipeline, so need to copy the link and host based on dev_type 12188871dcb9SJeeja KP */ 12198871dcb9SJeeja KP static void skl_tplg_fill_dma_id(struct skl_module_cfg *mcfg, 12208871dcb9SJeeja KP struct skl_pipe_params *params) 12218871dcb9SJeeja KP { 12228871dcb9SJeeja KP struct skl_pipe *pipe = mcfg->pipe; 12238871dcb9SJeeja KP 12248871dcb9SJeeja KP if (pipe->passthru) { 12258871dcb9SJeeja KP switch (mcfg->dev_type) { 12268871dcb9SJeeja KP case SKL_DEVICE_HDALINK: 12278871dcb9SJeeja KP pipe->p_params->link_dma_id = params->link_dma_id; 122812c3be0eSJeeja KP pipe->p_params->link_index = params->link_index; 12298871dcb9SJeeja KP break; 12308871dcb9SJeeja KP 12318871dcb9SJeeja KP case SKL_DEVICE_HDAHOST: 12328871dcb9SJeeja KP pipe->p_params->host_dma_id = params->host_dma_id; 12338871dcb9SJeeja KP break; 12348871dcb9SJeeja KP 12358871dcb9SJeeja KP default: 12368871dcb9SJeeja KP break; 12378871dcb9SJeeja KP } 12388871dcb9SJeeja KP pipe->p_params->s_fmt = params->s_fmt; 12398871dcb9SJeeja KP pipe->p_params->ch = params->ch; 12408871dcb9SJeeja KP pipe->p_params->s_freq = params->s_freq; 12418871dcb9SJeeja KP pipe->p_params->stream = params->stream; 124212c3be0eSJeeja KP pipe->p_params->format = params->format; 12438871dcb9SJeeja KP 12448871dcb9SJeeja KP } else { 12458871dcb9SJeeja KP memcpy(pipe->p_params, params, sizeof(*params)); 12468871dcb9SJeeja KP } 12478871dcb9SJeeja KP } 12488871dcb9SJeeja KP 12498871dcb9SJeeja KP /* 1250cfb0a873SVinod Koul * The FE params are passed by hw_params of the DAI. 1251cfb0a873SVinod Koul * On hw_params, the params are stored in Gateway module of the FE and we 1252cfb0a873SVinod Koul * need to calculate the format in DSP module configuration, that 1253cfb0a873SVinod Koul * conversion is done here 1254cfb0a873SVinod Koul */ 1255cfb0a873SVinod Koul int skl_tplg_update_pipe_params(struct device *dev, 1256cfb0a873SVinod Koul struct skl_module_cfg *mconfig, 1257cfb0a873SVinod Koul struct skl_pipe_params *params) 1258cfb0a873SVinod Koul { 1259cfb0a873SVinod Koul struct skl_module_fmt *format = NULL; 1260cfb0a873SVinod Koul 12618871dcb9SJeeja KP skl_tplg_fill_dma_id(mconfig, params); 1262cfb0a873SVinod Koul 1263cfb0a873SVinod Koul if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) 12644cd9899fSHardik T Shah format = &mconfig->in_fmt[0]; 1265cfb0a873SVinod Koul else 12664cd9899fSHardik T Shah format = &mconfig->out_fmt[0]; 1267cfb0a873SVinod Koul 1268cfb0a873SVinod Koul /* set the hw_params */ 1269cfb0a873SVinod Koul format->s_freq = params->s_freq; 1270cfb0a873SVinod Koul format->channels = params->ch; 1271cfb0a873SVinod Koul format->valid_bit_depth = skl_get_bit_depth(params->s_fmt); 1272cfb0a873SVinod Koul 1273cfb0a873SVinod Koul /* 1274cfb0a873SVinod Koul * 16 bit is 16 bit container whereas 24 bit is in 32 bit 1275cfb0a873SVinod Koul * container so update bit depth accordingly 1276cfb0a873SVinod Koul */ 1277cfb0a873SVinod Koul switch (format->valid_bit_depth) { 1278cfb0a873SVinod Koul case SKL_DEPTH_16BIT: 1279cfb0a873SVinod Koul format->bit_depth = format->valid_bit_depth; 1280cfb0a873SVinod Koul break; 1281cfb0a873SVinod Koul 1282cfb0a873SVinod Koul case SKL_DEPTH_24BIT: 12836654f39eSJeeja KP case SKL_DEPTH_32BIT: 1284cfb0a873SVinod Koul format->bit_depth = SKL_DEPTH_32BIT; 1285cfb0a873SVinod Koul break; 1286cfb0a873SVinod Koul 1287cfb0a873SVinod Koul default: 1288cfb0a873SVinod Koul dev_err(dev, "Invalid bit depth %x for pipe\n", 1289cfb0a873SVinod Koul format->valid_bit_depth); 1290cfb0a873SVinod Koul return -EINVAL; 1291cfb0a873SVinod Koul } 1292cfb0a873SVinod Koul 1293cfb0a873SVinod Koul if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) { 1294cfb0a873SVinod Koul mconfig->ibs = (format->s_freq / 1000) * 1295cfb0a873SVinod Koul (format->channels) * 1296cfb0a873SVinod Koul (format->bit_depth >> 3); 1297cfb0a873SVinod Koul } else { 1298cfb0a873SVinod Koul mconfig->obs = (format->s_freq / 1000) * 1299cfb0a873SVinod Koul (format->channels) * 1300cfb0a873SVinod Koul (format->bit_depth >> 3); 1301cfb0a873SVinod Koul } 1302cfb0a873SVinod Koul 1303cfb0a873SVinod Koul return 0; 1304cfb0a873SVinod Koul } 1305cfb0a873SVinod Koul 1306cfb0a873SVinod Koul /* 1307cfb0a873SVinod Koul * Query the module config for the FE DAI 1308cfb0a873SVinod Koul * This is used to find the hw_params set for that DAI and apply to FE 1309cfb0a873SVinod Koul * pipeline 1310cfb0a873SVinod Koul */ 1311cfb0a873SVinod Koul struct skl_module_cfg * 1312cfb0a873SVinod Koul skl_tplg_fe_get_cpr_module(struct snd_soc_dai *dai, int stream) 1313cfb0a873SVinod Koul { 1314cfb0a873SVinod Koul struct snd_soc_dapm_widget *w; 1315cfb0a873SVinod Koul struct snd_soc_dapm_path *p = NULL; 1316cfb0a873SVinod Koul 1317cfb0a873SVinod Koul if (stream == SNDRV_PCM_STREAM_PLAYBACK) { 1318cfb0a873SVinod Koul w = dai->playback_widget; 1319f0900eb2SSubhransu S. Prusty snd_soc_dapm_widget_for_each_sink_path(w, p) { 1320cfb0a873SVinod Koul if (p->connect && p->sink->power && 1321a28f51dbSJeeja KP !is_skl_dsp_widget_type(p->sink)) 1322cfb0a873SVinod Koul continue; 1323cfb0a873SVinod Koul 1324cfb0a873SVinod Koul if (p->sink->priv) { 1325cfb0a873SVinod Koul dev_dbg(dai->dev, "set params for %s\n", 1326cfb0a873SVinod Koul p->sink->name); 1327cfb0a873SVinod Koul return p->sink->priv; 1328cfb0a873SVinod Koul } 1329cfb0a873SVinod Koul } 1330cfb0a873SVinod Koul } else { 1331cfb0a873SVinod Koul w = dai->capture_widget; 1332f0900eb2SSubhransu S. Prusty snd_soc_dapm_widget_for_each_source_path(w, p) { 1333cfb0a873SVinod Koul if (p->connect && p->source->power && 1334a28f51dbSJeeja KP !is_skl_dsp_widget_type(p->source)) 1335cfb0a873SVinod Koul continue; 1336cfb0a873SVinod Koul 1337cfb0a873SVinod Koul if (p->source->priv) { 1338cfb0a873SVinod Koul dev_dbg(dai->dev, "set params for %s\n", 1339cfb0a873SVinod Koul p->source->name); 1340cfb0a873SVinod Koul return p->source->priv; 1341cfb0a873SVinod Koul } 1342cfb0a873SVinod Koul } 1343cfb0a873SVinod Koul } 1344cfb0a873SVinod Koul 1345cfb0a873SVinod Koul return NULL; 1346cfb0a873SVinod Koul } 1347cfb0a873SVinod Koul 1348718a42b5SDharageswari.R static struct skl_module_cfg *skl_get_mconfig_pb_cpr( 1349718a42b5SDharageswari.R struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w) 1350718a42b5SDharageswari.R { 1351718a42b5SDharageswari.R struct snd_soc_dapm_path *p; 1352718a42b5SDharageswari.R struct skl_module_cfg *mconfig = NULL; 1353718a42b5SDharageswari.R 1354718a42b5SDharageswari.R snd_soc_dapm_widget_for_each_source_path(w, p) { 1355718a42b5SDharageswari.R if (w->endpoints[SND_SOC_DAPM_DIR_OUT] > 0) { 1356718a42b5SDharageswari.R if (p->connect && 1357718a42b5SDharageswari.R (p->sink->id == snd_soc_dapm_aif_out) && 1358718a42b5SDharageswari.R p->source->priv) { 1359718a42b5SDharageswari.R mconfig = p->source->priv; 1360718a42b5SDharageswari.R return mconfig; 1361718a42b5SDharageswari.R } 1362718a42b5SDharageswari.R mconfig = skl_get_mconfig_pb_cpr(dai, p->source); 1363718a42b5SDharageswari.R if (mconfig) 1364718a42b5SDharageswari.R return mconfig; 1365718a42b5SDharageswari.R } 1366718a42b5SDharageswari.R } 1367718a42b5SDharageswari.R return mconfig; 1368718a42b5SDharageswari.R } 1369718a42b5SDharageswari.R 1370718a42b5SDharageswari.R static struct skl_module_cfg *skl_get_mconfig_cap_cpr( 1371718a42b5SDharageswari.R struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w) 1372718a42b5SDharageswari.R { 1373718a42b5SDharageswari.R struct snd_soc_dapm_path *p; 1374718a42b5SDharageswari.R struct skl_module_cfg *mconfig = NULL; 1375718a42b5SDharageswari.R 1376718a42b5SDharageswari.R snd_soc_dapm_widget_for_each_sink_path(w, p) { 1377718a42b5SDharageswari.R if (w->endpoints[SND_SOC_DAPM_DIR_IN] > 0) { 1378718a42b5SDharageswari.R if (p->connect && 1379718a42b5SDharageswari.R (p->source->id == snd_soc_dapm_aif_in) && 1380718a42b5SDharageswari.R p->sink->priv) { 1381718a42b5SDharageswari.R mconfig = p->sink->priv; 1382718a42b5SDharageswari.R return mconfig; 1383718a42b5SDharageswari.R } 1384718a42b5SDharageswari.R mconfig = skl_get_mconfig_cap_cpr(dai, p->sink); 1385718a42b5SDharageswari.R if (mconfig) 1386718a42b5SDharageswari.R return mconfig; 1387718a42b5SDharageswari.R } 1388718a42b5SDharageswari.R } 1389718a42b5SDharageswari.R return mconfig; 1390718a42b5SDharageswari.R } 1391718a42b5SDharageswari.R 1392718a42b5SDharageswari.R struct skl_module_cfg * 1393718a42b5SDharageswari.R skl_tplg_be_get_cpr_module(struct snd_soc_dai *dai, int stream) 1394718a42b5SDharageswari.R { 1395718a42b5SDharageswari.R struct snd_soc_dapm_widget *w; 1396718a42b5SDharageswari.R struct skl_module_cfg *mconfig; 1397718a42b5SDharageswari.R 1398718a42b5SDharageswari.R if (stream == SNDRV_PCM_STREAM_PLAYBACK) { 1399718a42b5SDharageswari.R w = dai->playback_widget; 1400718a42b5SDharageswari.R mconfig = skl_get_mconfig_pb_cpr(dai, w); 1401718a42b5SDharageswari.R } else { 1402718a42b5SDharageswari.R w = dai->capture_widget; 1403718a42b5SDharageswari.R mconfig = skl_get_mconfig_cap_cpr(dai, w); 1404718a42b5SDharageswari.R } 1405718a42b5SDharageswari.R return mconfig; 1406718a42b5SDharageswari.R } 1407718a42b5SDharageswari.R 1408cfb0a873SVinod Koul static u8 skl_tplg_be_link_type(int dev_type) 1409cfb0a873SVinod Koul { 1410cfb0a873SVinod Koul int ret; 1411cfb0a873SVinod Koul 1412cfb0a873SVinod Koul switch (dev_type) { 1413cfb0a873SVinod Koul case SKL_DEVICE_BT: 1414cfb0a873SVinod Koul ret = NHLT_LINK_SSP; 1415cfb0a873SVinod Koul break; 1416cfb0a873SVinod Koul 1417cfb0a873SVinod Koul case SKL_DEVICE_DMIC: 1418cfb0a873SVinod Koul ret = NHLT_LINK_DMIC; 1419cfb0a873SVinod Koul break; 1420cfb0a873SVinod Koul 1421cfb0a873SVinod Koul case SKL_DEVICE_I2S: 1422cfb0a873SVinod Koul ret = NHLT_LINK_SSP; 1423cfb0a873SVinod Koul break; 1424cfb0a873SVinod Koul 1425cfb0a873SVinod Koul case SKL_DEVICE_HDALINK: 1426cfb0a873SVinod Koul ret = NHLT_LINK_HDA; 1427cfb0a873SVinod Koul break; 1428cfb0a873SVinod Koul 1429cfb0a873SVinod Koul default: 1430cfb0a873SVinod Koul ret = NHLT_LINK_INVALID; 1431cfb0a873SVinod Koul break; 1432cfb0a873SVinod Koul } 1433cfb0a873SVinod Koul 1434cfb0a873SVinod Koul return ret; 1435cfb0a873SVinod Koul } 1436cfb0a873SVinod Koul 1437cfb0a873SVinod Koul /* 1438cfb0a873SVinod Koul * Fill the BE gateway parameters 1439cfb0a873SVinod Koul * The BE gateway expects a blob of parameters which are kept in the ACPI 1440cfb0a873SVinod Koul * NHLT blob, so query the blob for interface type (i2s/pdm) and instance. 1441cfb0a873SVinod Koul * The port can have multiple settings so pick based on the PCM 1442cfb0a873SVinod Koul * parameters 1443cfb0a873SVinod Koul */ 1444cfb0a873SVinod Koul static int skl_tplg_be_fill_pipe_params(struct snd_soc_dai *dai, 1445cfb0a873SVinod Koul struct skl_module_cfg *mconfig, 1446cfb0a873SVinod Koul struct skl_pipe_params *params) 1447cfb0a873SVinod Koul { 1448cfb0a873SVinod Koul struct nhlt_specific_cfg *cfg; 1449cfb0a873SVinod Koul struct skl *skl = get_skl_ctx(dai->dev); 1450cfb0a873SVinod Koul int link_type = skl_tplg_be_link_type(mconfig->dev_type); 1451cfb0a873SVinod Koul 14528871dcb9SJeeja KP skl_tplg_fill_dma_id(mconfig, params); 1453cfb0a873SVinod Koul 1454b30c275eSJeeja KP if (link_type == NHLT_LINK_HDA) 1455b30c275eSJeeja KP return 0; 1456b30c275eSJeeja KP 1457cfb0a873SVinod Koul /* update the blob based on virtual bus_id*/ 1458cfb0a873SVinod Koul cfg = skl_get_ep_blob(skl, mconfig->vbus_id, link_type, 1459cfb0a873SVinod Koul params->s_fmt, params->ch, 1460cfb0a873SVinod Koul params->s_freq, params->stream); 1461cfb0a873SVinod Koul if (cfg) { 1462cfb0a873SVinod Koul mconfig->formats_config.caps_size = cfg->size; 1463bc03281aSJeeja KP mconfig->formats_config.caps = (u32 *) &cfg->caps; 1464cfb0a873SVinod Koul } else { 1465cfb0a873SVinod Koul dev_err(dai->dev, "Blob NULL for id %x type %d dirn %d\n", 1466cfb0a873SVinod Koul mconfig->vbus_id, link_type, 1467cfb0a873SVinod Koul params->stream); 1468cfb0a873SVinod Koul dev_err(dai->dev, "PCM: ch %d, freq %d, fmt %d\n", 1469cfb0a873SVinod Koul params->ch, params->s_freq, params->s_fmt); 1470cfb0a873SVinod Koul return -EINVAL; 1471cfb0a873SVinod Koul } 1472cfb0a873SVinod Koul 1473cfb0a873SVinod Koul return 0; 1474cfb0a873SVinod Koul } 1475cfb0a873SVinod Koul 1476cfb0a873SVinod Koul static int skl_tplg_be_set_src_pipe_params(struct snd_soc_dai *dai, 1477cfb0a873SVinod Koul struct snd_soc_dapm_widget *w, 1478cfb0a873SVinod Koul struct skl_pipe_params *params) 1479cfb0a873SVinod Koul { 1480cfb0a873SVinod Koul struct snd_soc_dapm_path *p; 14814d8adccbSSubhransu S. Prusty int ret = -EIO; 1482cfb0a873SVinod Koul 1483f0900eb2SSubhransu S. Prusty snd_soc_dapm_widget_for_each_source_path(w, p) { 1484cfb0a873SVinod Koul if (p->connect && is_skl_dsp_widget_type(p->source) && 1485cfb0a873SVinod Koul p->source->priv) { 1486cfb0a873SVinod Koul 14879a03cb49SJeeja KP ret = skl_tplg_be_fill_pipe_params(dai, 14889a03cb49SJeeja KP p->source->priv, params); 14894d8adccbSSubhransu S. Prusty if (ret < 0) 14904d8adccbSSubhransu S. Prusty return ret; 1491cfb0a873SVinod Koul } else { 14929a03cb49SJeeja KP ret = skl_tplg_be_set_src_pipe_params(dai, 14939a03cb49SJeeja KP p->source, params); 14944d8adccbSSubhransu S. Prusty if (ret < 0) 14954d8adccbSSubhransu S. Prusty return ret; 1496cfb0a873SVinod Koul } 1497cfb0a873SVinod Koul } 1498cfb0a873SVinod Koul 14994d8adccbSSubhransu S. Prusty return ret; 1500cfb0a873SVinod Koul } 1501cfb0a873SVinod Koul 1502cfb0a873SVinod Koul static int skl_tplg_be_set_sink_pipe_params(struct snd_soc_dai *dai, 1503cfb0a873SVinod Koul struct snd_soc_dapm_widget *w, struct skl_pipe_params *params) 1504cfb0a873SVinod Koul { 1505cfb0a873SVinod Koul struct snd_soc_dapm_path *p = NULL; 15064d8adccbSSubhransu S. Prusty int ret = -EIO; 1507cfb0a873SVinod Koul 1508f0900eb2SSubhransu S. Prusty snd_soc_dapm_widget_for_each_sink_path(w, p) { 1509cfb0a873SVinod Koul if (p->connect && is_skl_dsp_widget_type(p->sink) && 1510cfb0a873SVinod Koul p->sink->priv) { 1511cfb0a873SVinod Koul 15129a03cb49SJeeja KP ret = skl_tplg_be_fill_pipe_params(dai, 15139a03cb49SJeeja KP p->sink->priv, params); 15144d8adccbSSubhransu S. Prusty if (ret < 0) 15154d8adccbSSubhransu S. Prusty return ret; 15164d8adccbSSubhransu S. Prusty } else { 15174d8adccbSSubhransu S. Prusty ret = skl_tplg_be_set_sink_pipe_params( 1518cfb0a873SVinod Koul dai, p->sink, params); 15194d8adccbSSubhransu S. Prusty if (ret < 0) 15204d8adccbSSubhransu S. Prusty return ret; 1521cfb0a873SVinod Koul } 1522cfb0a873SVinod Koul } 1523cfb0a873SVinod Koul 15244d8adccbSSubhransu S. Prusty return ret; 1525cfb0a873SVinod Koul } 1526cfb0a873SVinod Koul 1527cfb0a873SVinod Koul /* 1528cfb0a873SVinod Koul * BE hw_params can be a source parameters (capture) or sink parameters 1529cfb0a873SVinod Koul * (playback). Based on sink and source we need to either find the source 1530cfb0a873SVinod Koul * list or the sink list and set the pipeline parameters 1531cfb0a873SVinod Koul */ 1532cfb0a873SVinod Koul int skl_tplg_be_update_params(struct snd_soc_dai *dai, 1533cfb0a873SVinod Koul struct skl_pipe_params *params) 1534cfb0a873SVinod Koul { 1535cfb0a873SVinod Koul struct snd_soc_dapm_widget *w; 1536cfb0a873SVinod Koul 1537cfb0a873SVinod Koul if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) { 1538cfb0a873SVinod Koul w = dai->playback_widget; 1539cfb0a873SVinod Koul 1540cfb0a873SVinod Koul return skl_tplg_be_set_src_pipe_params(dai, w, params); 1541cfb0a873SVinod Koul 1542cfb0a873SVinod Koul } else { 1543cfb0a873SVinod Koul w = dai->capture_widget; 1544cfb0a873SVinod Koul 1545cfb0a873SVinod Koul return skl_tplg_be_set_sink_pipe_params(dai, w, params); 1546cfb0a873SVinod Koul } 1547cfb0a873SVinod Koul 1548cfb0a873SVinod Koul return 0; 1549cfb0a873SVinod Koul } 15503af36706SVinod Koul 15513af36706SVinod Koul static const struct snd_soc_tplg_widget_events skl_tplg_widget_ops[] = { 15523af36706SVinod Koul {SKL_MIXER_EVENT, skl_tplg_mixer_event}, 15533af36706SVinod Koul {SKL_VMIXER_EVENT, skl_tplg_vmixer_event}, 15543af36706SVinod Koul {SKL_PGA_EVENT, skl_tplg_pga_event}, 15553af36706SVinod Koul }; 15563af36706SVinod Koul 1557140adfbaSJeeja KP static const struct snd_soc_tplg_bytes_ext_ops skl_tlv_ops[] = { 1558140adfbaSJeeja KP {SKL_CONTROL_TYPE_BYTE_TLV, skl_tplg_tlv_control_get, 1559140adfbaSJeeja KP skl_tplg_tlv_control_set}, 1560140adfbaSJeeja KP }; 1561140adfbaSJeeja KP 15626277e832SShreyas NC static int skl_tplg_fill_pipe_tkn(struct device *dev, 15636277e832SShreyas NC struct skl_pipe *pipe, u32 tkn, 15646277e832SShreyas NC u32 tkn_val) 15653af36706SVinod Koul { 15663af36706SVinod Koul 15676277e832SShreyas NC switch (tkn) { 15686277e832SShreyas NC case SKL_TKN_U32_PIPE_CONN_TYPE: 15696277e832SShreyas NC pipe->conn_type = tkn_val; 15706277e832SShreyas NC break; 15716277e832SShreyas NC 15726277e832SShreyas NC case SKL_TKN_U32_PIPE_PRIORITY: 15736277e832SShreyas NC pipe->pipe_priority = tkn_val; 15746277e832SShreyas NC break; 15756277e832SShreyas NC 15766277e832SShreyas NC case SKL_TKN_U32_PIPE_MEM_PGS: 15776277e832SShreyas NC pipe->memory_pages = tkn_val; 15786277e832SShreyas NC break; 15796277e832SShreyas NC 15808a0cb236SVinod Koul case SKL_TKN_U32_PMODE: 15818a0cb236SVinod Koul pipe->lp_mode = tkn_val; 15828a0cb236SVinod Koul break; 15838a0cb236SVinod Koul 15846277e832SShreyas NC default: 15856277e832SShreyas NC dev_err(dev, "Token not handled %d\n", tkn); 15866277e832SShreyas NC return -EINVAL; 15873af36706SVinod Koul } 15886277e832SShreyas NC 15896277e832SShreyas NC return 0; 15903af36706SVinod Koul } 15913af36706SVinod Koul 15923af36706SVinod Koul /* 15936277e832SShreyas NC * Add pipeline by parsing the relevant tokens 15946277e832SShreyas NC * Return an existing pipe if the pipe already exists. 15953af36706SVinod Koul */ 15966277e832SShreyas NC static int skl_tplg_add_pipe(struct device *dev, 15976277e832SShreyas NC struct skl_module_cfg *mconfig, struct skl *skl, 15986277e832SShreyas NC struct snd_soc_tplg_vendor_value_elem *tkn_elem) 15993af36706SVinod Koul { 16003af36706SVinod Koul struct skl_pipeline *ppl; 16013af36706SVinod Koul struct skl_pipe *pipe; 16023af36706SVinod Koul struct skl_pipe_params *params; 16033af36706SVinod Koul 16043af36706SVinod Koul list_for_each_entry(ppl, &skl->ppl_list, node) { 16056277e832SShreyas NC if (ppl->pipe->ppl_id == tkn_elem->value) { 16066277e832SShreyas NC mconfig->pipe = ppl->pipe; 16076277e832SShreyas NC return EEXIST; 16086277e832SShreyas NC } 16093af36706SVinod Koul } 16103af36706SVinod Koul 16113af36706SVinod Koul ppl = devm_kzalloc(dev, sizeof(*ppl), GFP_KERNEL); 16123af36706SVinod Koul if (!ppl) 16136277e832SShreyas NC return -ENOMEM; 16143af36706SVinod Koul 16153af36706SVinod Koul pipe = devm_kzalloc(dev, sizeof(*pipe), GFP_KERNEL); 16163af36706SVinod Koul if (!pipe) 16176277e832SShreyas NC return -ENOMEM; 16183af36706SVinod Koul 16193af36706SVinod Koul params = devm_kzalloc(dev, sizeof(*params), GFP_KERNEL); 16203af36706SVinod Koul if (!params) 16216277e832SShreyas NC return -ENOMEM; 16223af36706SVinod Koul 16233af36706SVinod Koul pipe->p_params = params; 16246277e832SShreyas NC pipe->ppl_id = tkn_elem->value; 16253af36706SVinod Koul INIT_LIST_HEAD(&pipe->w_list); 16263af36706SVinod Koul 16273af36706SVinod Koul ppl->pipe = pipe; 16283af36706SVinod Koul list_add(&ppl->node, &skl->ppl_list); 16293af36706SVinod Koul 16306277e832SShreyas NC mconfig->pipe = pipe; 16316277e832SShreyas NC mconfig->pipe->state = SKL_PIPE_INVALID; 16326277e832SShreyas NC 16336277e832SShreyas NC return 0; 16343af36706SVinod Koul } 16353af36706SVinod Koul 16366277e832SShreyas NC static int skl_tplg_fill_pin(struct device *dev, u32 tkn, 16376277e832SShreyas NC struct skl_module_pin *m_pin, 16386277e832SShreyas NC int pin_index, u32 value) 16396277e832SShreyas NC { 16406277e832SShreyas NC switch (tkn) { 16416277e832SShreyas NC case SKL_TKN_U32_PIN_MOD_ID: 16426277e832SShreyas NC m_pin[pin_index].id.module_id = value; 16436277e832SShreyas NC break; 16446277e832SShreyas NC 16456277e832SShreyas NC case SKL_TKN_U32_PIN_INST_ID: 16466277e832SShreyas NC m_pin[pin_index].id.instance_id = value; 16476277e832SShreyas NC break; 16486277e832SShreyas NC 16496277e832SShreyas NC default: 16506277e832SShreyas NC dev_err(dev, "%d Not a pin token\n", value); 16516277e832SShreyas NC return -EINVAL; 16526277e832SShreyas NC } 16536277e832SShreyas NC 16546277e832SShreyas NC return 0; 16556277e832SShreyas NC } 16566277e832SShreyas NC 16576277e832SShreyas NC /* 16586277e832SShreyas NC * Parse for pin config specific tokens to fill up the 16596277e832SShreyas NC * module private data 16606277e832SShreyas NC */ 16616277e832SShreyas NC static int skl_tplg_fill_pins_info(struct device *dev, 16626277e832SShreyas NC struct skl_module_cfg *mconfig, 16636277e832SShreyas NC struct snd_soc_tplg_vendor_value_elem *tkn_elem, 16646277e832SShreyas NC int dir, int pin_count) 16656277e832SShreyas NC { 16666277e832SShreyas NC int ret; 16676277e832SShreyas NC struct skl_module_pin *m_pin; 16686277e832SShreyas NC 16696277e832SShreyas NC switch (dir) { 16706277e832SShreyas NC case SKL_DIR_IN: 16716277e832SShreyas NC m_pin = mconfig->m_in_pin; 16726277e832SShreyas NC break; 16736277e832SShreyas NC 16746277e832SShreyas NC case SKL_DIR_OUT: 16756277e832SShreyas NC m_pin = mconfig->m_out_pin; 16766277e832SShreyas NC break; 16776277e832SShreyas NC 16786277e832SShreyas NC default: 1679ecd286a9SColin Ian King dev_err(dev, "Invalid direction value\n"); 16806277e832SShreyas NC return -EINVAL; 16816277e832SShreyas NC } 16826277e832SShreyas NC 16836277e832SShreyas NC ret = skl_tplg_fill_pin(dev, tkn_elem->token, 16846277e832SShreyas NC m_pin, pin_count, tkn_elem->value); 16856277e832SShreyas NC 16866277e832SShreyas NC if (ret < 0) 16876277e832SShreyas NC return ret; 16886277e832SShreyas NC 16896277e832SShreyas NC m_pin[pin_count].in_use = false; 16906277e832SShreyas NC m_pin[pin_count].pin_state = SKL_PIN_UNBIND; 16916277e832SShreyas NC 16926277e832SShreyas NC return 0; 16936277e832SShreyas NC } 16946277e832SShreyas NC 16956277e832SShreyas NC /* 16966277e832SShreyas NC * Fill up input/output module config format based 16976277e832SShreyas NC * on the direction 16986277e832SShreyas NC */ 16996277e832SShreyas NC static int skl_tplg_fill_fmt(struct device *dev, 17006277e832SShreyas NC struct skl_module_cfg *mconfig, u32 tkn, 17016277e832SShreyas NC u32 value, u32 dir, u32 pin_count) 17026277e832SShreyas NC { 17036277e832SShreyas NC struct skl_module_fmt *dst_fmt; 17046277e832SShreyas NC 17056277e832SShreyas NC switch (dir) { 17066277e832SShreyas NC case SKL_DIR_IN: 17076277e832SShreyas NC dst_fmt = mconfig->in_fmt; 17086277e832SShreyas NC dst_fmt += pin_count; 17096277e832SShreyas NC break; 17106277e832SShreyas NC 17116277e832SShreyas NC case SKL_DIR_OUT: 17126277e832SShreyas NC dst_fmt = mconfig->out_fmt; 17136277e832SShreyas NC dst_fmt += pin_count; 17146277e832SShreyas NC break; 17156277e832SShreyas NC 17166277e832SShreyas NC default: 1717ecd286a9SColin Ian King dev_err(dev, "Invalid direction value\n"); 17186277e832SShreyas NC return -EINVAL; 17196277e832SShreyas NC } 17206277e832SShreyas NC 17216277e832SShreyas NC switch (tkn) { 17226277e832SShreyas NC case SKL_TKN_U32_FMT_CH: 17236277e832SShreyas NC dst_fmt->channels = value; 17246277e832SShreyas NC break; 17256277e832SShreyas NC 17266277e832SShreyas NC case SKL_TKN_U32_FMT_FREQ: 17276277e832SShreyas NC dst_fmt->s_freq = value; 17286277e832SShreyas NC break; 17296277e832SShreyas NC 17306277e832SShreyas NC case SKL_TKN_U32_FMT_BIT_DEPTH: 17316277e832SShreyas NC dst_fmt->bit_depth = value; 17326277e832SShreyas NC break; 17336277e832SShreyas NC 17346277e832SShreyas NC case SKL_TKN_U32_FMT_SAMPLE_SIZE: 17356277e832SShreyas NC dst_fmt->valid_bit_depth = value; 17366277e832SShreyas NC break; 17376277e832SShreyas NC 17386277e832SShreyas NC case SKL_TKN_U32_FMT_CH_CONFIG: 17396277e832SShreyas NC dst_fmt->ch_cfg = value; 17406277e832SShreyas NC break; 17416277e832SShreyas NC 17426277e832SShreyas NC case SKL_TKN_U32_FMT_INTERLEAVE: 17436277e832SShreyas NC dst_fmt->interleaving_style = value; 17446277e832SShreyas NC break; 17456277e832SShreyas NC 17466277e832SShreyas NC case SKL_TKN_U32_FMT_SAMPLE_TYPE: 17476277e832SShreyas NC dst_fmt->sample_type = value; 17486277e832SShreyas NC break; 17496277e832SShreyas NC 17506277e832SShreyas NC case SKL_TKN_U32_FMT_CH_MAP: 17516277e832SShreyas NC dst_fmt->ch_map = value; 17526277e832SShreyas NC break; 17536277e832SShreyas NC 17546277e832SShreyas NC default: 1755ecd286a9SColin Ian King dev_err(dev, "Invalid token %d\n", tkn); 17566277e832SShreyas NC return -EINVAL; 17576277e832SShreyas NC } 17586277e832SShreyas NC 17596277e832SShreyas NC return 0; 17606277e832SShreyas NC } 17616277e832SShreyas NC 17626277e832SShreyas NC static int skl_tplg_get_uuid(struct device *dev, struct skl_module_cfg *mconfig, 17636277e832SShreyas NC struct snd_soc_tplg_vendor_uuid_elem *uuid_tkn) 17646277e832SShreyas NC { 17656277e832SShreyas NC if (uuid_tkn->token == SKL_TKN_UUID) 17666277e832SShreyas NC memcpy(&mconfig->guid, &uuid_tkn->uuid, 16); 17676277e832SShreyas NC else { 1768ecd286a9SColin Ian King dev_err(dev, "Not an UUID token tkn %d\n", uuid_tkn->token); 17696277e832SShreyas NC return -EINVAL; 17706277e832SShreyas NC } 17716277e832SShreyas NC 17726277e832SShreyas NC return 0; 17736277e832SShreyas NC } 17746277e832SShreyas NC 17756277e832SShreyas NC static void skl_tplg_fill_pin_dynamic_val( 17766277e832SShreyas NC struct skl_module_pin *mpin, u32 pin_count, u32 value) 17774cd9899fSHardik T Shah { 17784cd9899fSHardik T Shah int i; 17794cd9899fSHardik T Shah 17806277e832SShreyas NC for (i = 0; i < pin_count; i++) 17816277e832SShreyas NC mpin[i].is_dynamic = value; 17824cd9899fSHardik T Shah } 17836277e832SShreyas NC 17846277e832SShreyas NC /* 17856277e832SShreyas NC * Parse tokens to fill up the module private data 17866277e832SShreyas NC */ 17876277e832SShreyas NC static int skl_tplg_get_token(struct device *dev, 17886277e832SShreyas NC struct snd_soc_tplg_vendor_value_elem *tkn_elem, 17896277e832SShreyas NC struct skl *skl, struct skl_module_cfg *mconfig) 17906277e832SShreyas NC { 17916277e832SShreyas NC int tkn_count = 0; 17926277e832SShreyas NC int ret; 17936277e832SShreyas NC static int is_pipe_exists; 17946277e832SShreyas NC static int pin_index, dir; 17956277e832SShreyas NC 17966277e832SShreyas NC if (tkn_elem->token > SKL_TKN_MAX) 17976277e832SShreyas NC return -EINVAL; 17986277e832SShreyas NC 17996277e832SShreyas NC switch (tkn_elem->token) { 18006277e832SShreyas NC case SKL_TKN_U8_IN_QUEUE_COUNT: 18016277e832SShreyas NC mconfig->max_in_queue = tkn_elem->value; 18026277e832SShreyas NC mconfig->m_in_pin = devm_kzalloc(dev, mconfig->max_in_queue * 18036277e832SShreyas NC sizeof(*mconfig->m_in_pin), 18046277e832SShreyas NC GFP_KERNEL); 18056277e832SShreyas NC if (!mconfig->m_in_pin) 18066277e832SShreyas NC return -ENOMEM; 18076277e832SShreyas NC 18086277e832SShreyas NC break; 18096277e832SShreyas NC 18106277e832SShreyas NC case SKL_TKN_U8_OUT_QUEUE_COUNT: 18116277e832SShreyas NC mconfig->max_out_queue = tkn_elem->value; 18126277e832SShreyas NC mconfig->m_out_pin = devm_kzalloc(dev, mconfig->max_out_queue * 18136277e832SShreyas NC sizeof(*mconfig->m_out_pin), 18146277e832SShreyas NC GFP_KERNEL); 18156277e832SShreyas NC 18166277e832SShreyas NC if (!mconfig->m_out_pin) 18176277e832SShreyas NC return -ENOMEM; 18186277e832SShreyas NC 18196277e832SShreyas NC break; 18206277e832SShreyas NC 18216277e832SShreyas NC case SKL_TKN_U8_DYN_IN_PIN: 18226277e832SShreyas NC if (!mconfig->m_in_pin) 18236277e832SShreyas NC return -ENOMEM; 18246277e832SShreyas NC 18256277e832SShreyas NC skl_tplg_fill_pin_dynamic_val(mconfig->m_in_pin, 18266277e832SShreyas NC mconfig->max_in_queue, tkn_elem->value); 18276277e832SShreyas NC 18286277e832SShreyas NC break; 18296277e832SShreyas NC 18306277e832SShreyas NC case SKL_TKN_U8_DYN_OUT_PIN: 18316277e832SShreyas NC if (!mconfig->m_out_pin) 18326277e832SShreyas NC return -ENOMEM; 18336277e832SShreyas NC 18346277e832SShreyas NC skl_tplg_fill_pin_dynamic_val(mconfig->m_out_pin, 18356277e832SShreyas NC mconfig->max_out_queue, tkn_elem->value); 18366277e832SShreyas NC 18376277e832SShreyas NC break; 18386277e832SShreyas NC 18396277e832SShreyas NC case SKL_TKN_U8_TIME_SLOT: 18406277e832SShreyas NC mconfig->time_slot = tkn_elem->value; 18416277e832SShreyas NC break; 18426277e832SShreyas NC 18436277e832SShreyas NC case SKL_TKN_U8_CORE_ID: 18446277e832SShreyas NC mconfig->core_id = tkn_elem->value; 18456277e832SShreyas NC 18466277e832SShreyas NC case SKL_TKN_U8_MOD_TYPE: 18476277e832SShreyas NC mconfig->m_type = tkn_elem->value; 18486277e832SShreyas NC break; 18496277e832SShreyas NC 18506277e832SShreyas NC case SKL_TKN_U8_DEV_TYPE: 18516277e832SShreyas NC mconfig->dev_type = tkn_elem->value; 18526277e832SShreyas NC break; 18536277e832SShreyas NC 18546277e832SShreyas NC case SKL_TKN_U8_HW_CONN_TYPE: 18556277e832SShreyas NC mconfig->hw_conn_type = tkn_elem->value; 18566277e832SShreyas NC break; 18576277e832SShreyas NC 18586277e832SShreyas NC case SKL_TKN_U16_MOD_INST_ID: 18596277e832SShreyas NC mconfig->id.instance_id = 18606277e832SShreyas NC tkn_elem->value; 18616277e832SShreyas NC break; 18626277e832SShreyas NC 18636277e832SShreyas NC case SKL_TKN_U32_MEM_PAGES: 18646277e832SShreyas NC mconfig->mem_pages = tkn_elem->value; 18656277e832SShreyas NC break; 18666277e832SShreyas NC 18676277e832SShreyas NC case SKL_TKN_U32_MAX_MCPS: 18686277e832SShreyas NC mconfig->mcps = tkn_elem->value; 18696277e832SShreyas NC break; 18706277e832SShreyas NC 18716277e832SShreyas NC case SKL_TKN_U32_OBS: 18726277e832SShreyas NC mconfig->obs = tkn_elem->value; 18736277e832SShreyas NC break; 18746277e832SShreyas NC 18756277e832SShreyas NC case SKL_TKN_U32_IBS: 18766277e832SShreyas NC mconfig->ibs = tkn_elem->value; 18776277e832SShreyas NC break; 18786277e832SShreyas NC 18796277e832SShreyas NC case SKL_TKN_U32_VBUS_ID: 18806277e832SShreyas NC mconfig->vbus_id = tkn_elem->value; 18816277e832SShreyas NC break; 18826277e832SShreyas NC 18836277e832SShreyas NC case SKL_TKN_U32_PARAMS_FIXUP: 18846277e832SShreyas NC mconfig->params_fixup = tkn_elem->value; 18856277e832SShreyas NC break; 18866277e832SShreyas NC 18876277e832SShreyas NC case SKL_TKN_U32_CONVERTER: 18886277e832SShreyas NC mconfig->converter = tkn_elem->value; 18896277e832SShreyas NC break; 18906277e832SShreyas NC 18916bd9dcf3SVinod Koul case SKL_TKL_U32_D0I3_CAPS: 18926bd9dcf3SVinod Koul mconfig->d0i3_caps = tkn_elem->value; 18936bd9dcf3SVinod Koul break; 18946bd9dcf3SVinod Koul 18956277e832SShreyas NC case SKL_TKN_U32_PIPE_ID: 18966277e832SShreyas NC ret = skl_tplg_add_pipe(dev, 18976277e832SShreyas NC mconfig, skl, tkn_elem); 18986277e832SShreyas NC 18996277e832SShreyas NC if (ret < 0) 19006277e832SShreyas NC return is_pipe_exists; 19016277e832SShreyas NC 19026277e832SShreyas NC if (ret == EEXIST) 19036277e832SShreyas NC is_pipe_exists = 1; 19046277e832SShreyas NC 19056277e832SShreyas NC break; 19066277e832SShreyas NC 19076277e832SShreyas NC case SKL_TKN_U32_PIPE_CONN_TYPE: 19086277e832SShreyas NC case SKL_TKN_U32_PIPE_PRIORITY: 19096277e832SShreyas NC case SKL_TKN_U32_PIPE_MEM_PGS: 19108a0cb236SVinod Koul case SKL_TKN_U32_PMODE: 19116277e832SShreyas NC if (is_pipe_exists) { 19126277e832SShreyas NC ret = skl_tplg_fill_pipe_tkn(dev, mconfig->pipe, 19136277e832SShreyas NC tkn_elem->token, tkn_elem->value); 19146277e832SShreyas NC if (ret < 0) 19156277e832SShreyas NC return ret; 19166277e832SShreyas NC } 19176277e832SShreyas NC 19186277e832SShreyas NC break; 19196277e832SShreyas NC 19206277e832SShreyas NC /* 19216277e832SShreyas NC * SKL_TKN_U32_DIR_PIN_COUNT token has the value for both 19226277e832SShreyas NC * direction and the pin count. The first four bits represent 19236277e832SShreyas NC * direction and next four the pin count. 19246277e832SShreyas NC */ 19256277e832SShreyas NC case SKL_TKN_U32_DIR_PIN_COUNT: 19266277e832SShreyas NC dir = tkn_elem->value & SKL_IN_DIR_BIT_MASK; 19276277e832SShreyas NC pin_index = (tkn_elem->value & 19286277e832SShreyas NC SKL_PIN_COUNT_MASK) >> 4; 19296277e832SShreyas NC 19306277e832SShreyas NC break; 19316277e832SShreyas NC 19326277e832SShreyas NC case SKL_TKN_U32_FMT_CH: 19336277e832SShreyas NC case SKL_TKN_U32_FMT_FREQ: 19346277e832SShreyas NC case SKL_TKN_U32_FMT_BIT_DEPTH: 19356277e832SShreyas NC case SKL_TKN_U32_FMT_SAMPLE_SIZE: 19366277e832SShreyas NC case SKL_TKN_U32_FMT_CH_CONFIG: 19376277e832SShreyas NC case SKL_TKN_U32_FMT_INTERLEAVE: 19386277e832SShreyas NC case SKL_TKN_U32_FMT_SAMPLE_TYPE: 19396277e832SShreyas NC case SKL_TKN_U32_FMT_CH_MAP: 19406277e832SShreyas NC ret = skl_tplg_fill_fmt(dev, mconfig, tkn_elem->token, 19416277e832SShreyas NC tkn_elem->value, dir, pin_index); 19426277e832SShreyas NC 19436277e832SShreyas NC if (ret < 0) 19446277e832SShreyas NC return ret; 19456277e832SShreyas NC 19466277e832SShreyas NC break; 19476277e832SShreyas NC 19486277e832SShreyas NC case SKL_TKN_U32_PIN_MOD_ID: 19496277e832SShreyas NC case SKL_TKN_U32_PIN_INST_ID: 19506277e832SShreyas NC ret = skl_tplg_fill_pins_info(dev, 19516277e832SShreyas NC mconfig, tkn_elem, dir, 19526277e832SShreyas NC pin_index); 19536277e832SShreyas NC if (ret < 0) 19546277e832SShreyas NC return ret; 19556277e832SShreyas NC 19566277e832SShreyas NC break; 19576277e832SShreyas NC 19586277e832SShreyas NC case SKL_TKN_U32_CAPS_SIZE: 19596277e832SShreyas NC mconfig->formats_config.caps_size = 19606277e832SShreyas NC tkn_elem->value; 19616277e832SShreyas NC 19626277e832SShreyas NC break; 19636277e832SShreyas NC 19646277e832SShreyas NC case SKL_TKN_U32_PROC_DOMAIN: 19656277e832SShreyas NC mconfig->domain = 19666277e832SShreyas NC tkn_elem->value; 19676277e832SShreyas NC 19686277e832SShreyas NC break; 19696277e832SShreyas NC 19706277e832SShreyas NC case SKL_TKN_U8_IN_PIN_TYPE: 19716277e832SShreyas NC case SKL_TKN_U8_OUT_PIN_TYPE: 19726277e832SShreyas NC case SKL_TKN_U8_CONN_TYPE: 19736277e832SShreyas NC break; 19746277e832SShreyas NC 19756277e832SShreyas NC default: 19766277e832SShreyas NC dev_err(dev, "Token %d not handled\n", 19776277e832SShreyas NC tkn_elem->token); 19786277e832SShreyas NC return -EINVAL; 19796277e832SShreyas NC } 19806277e832SShreyas NC 19816277e832SShreyas NC tkn_count++; 19826277e832SShreyas NC 19836277e832SShreyas NC return tkn_count; 19846277e832SShreyas NC } 19856277e832SShreyas NC 19866277e832SShreyas NC /* 19876277e832SShreyas NC * Parse the vendor array for specific tokens to construct 19886277e832SShreyas NC * module private data 19896277e832SShreyas NC */ 19906277e832SShreyas NC static int skl_tplg_get_tokens(struct device *dev, 19916277e832SShreyas NC char *pvt_data, struct skl *skl, 19926277e832SShreyas NC struct skl_module_cfg *mconfig, int block_size) 19936277e832SShreyas NC { 19946277e832SShreyas NC struct snd_soc_tplg_vendor_array *array; 19956277e832SShreyas NC struct snd_soc_tplg_vendor_value_elem *tkn_elem; 19966277e832SShreyas NC int tkn_count = 0, ret; 19976277e832SShreyas NC int off = 0, tuple_size = 0; 19986277e832SShreyas NC 19996277e832SShreyas NC if (block_size <= 0) 20006277e832SShreyas NC return -EINVAL; 20016277e832SShreyas NC 20026277e832SShreyas NC while (tuple_size < block_size) { 20036277e832SShreyas NC array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off); 20046277e832SShreyas NC 20056277e832SShreyas NC off += array->size; 20066277e832SShreyas NC 20076277e832SShreyas NC switch (array->type) { 20086277e832SShreyas NC case SND_SOC_TPLG_TUPLE_TYPE_STRING: 2009ecd286a9SColin Ian King dev_warn(dev, "no string tokens expected for skl tplg\n"); 20106277e832SShreyas NC continue; 20116277e832SShreyas NC 20126277e832SShreyas NC case SND_SOC_TPLG_TUPLE_TYPE_UUID: 20136277e832SShreyas NC ret = skl_tplg_get_uuid(dev, mconfig, array->uuid); 20146277e832SShreyas NC if (ret < 0) 20156277e832SShreyas NC return ret; 20166277e832SShreyas NC 20176277e832SShreyas NC tuple_size += sizeof(*array->uuid); 20186277e832SShreyas NC 20196277e832SShreyas NC continue; 20206277e832SShreyas NC 20216277e832SShreyas NC default: 20226277e832SShreyas NC tkn_elem = array->value; 20236277e832SShreyas NC tkn_count = 0; 20246277e832SShreyas NC break; 20256277e832SShreyas NC } 20266277e832SShreyas NC 20276277e832SShreyas NC while (tkn_count <= (array->num_elems - 1)) { 20286277e832SShreyas NC ret = skl_tplg_get_token(dev, tkn_elem, 20296277e832SShreyas NC skl, mconfig); 20306277e832SShreyas NC 20316277e832SShreyas NC if (ret < 0) 20326277e832SShreyas NC return ret; 20336277e832SShreyas NC 20346277e832SShreyas NC tkn_count = tkn_count + ret; 20356277e832SShreyas NC tkn_elem++; 20366277e832SShreyas NC } 20376277e832SShreyas NC 20386277e832SShreyas NC tuple_size += tkn_count * sizeof(*tkn_elem); 20396277e832SShreyas NC } 20406277e832SShreyas NC 20416277e832SShreyas NC return 0; 20426277e832SShreyas NC } 20436277e832SShreyas NC 20446277e832SShreyas NC /* 20456277e832SShreyas NC * Every data block is preceded by a descriptor to read the number 20466277e832SShreyas NC * of data blocks, they type of the block and it's size 20476277e832SShreyas NC */ 20486277e832SShreyas NC static int skl_tplg_get_desc_blocks(struct device *dev, 20496277e832SShreyas NC struct snd_soc_tplg_vendor_array *array) 20506277e832SShreyas NC { 20516277e832SShreyas NC struct snd_soc_tplg_vendor_value_elem *tkn_elem; 20526277e832SShreyas NC 20536277e832SShreyas NC tkn_elem = array->value; 20546277e832SShreyas NC 20556277e832SShreyas NC switch (tkn_elem->token) { 20566277e832SShreyas NC case SKL_TKN_U8_NUM_BLOCKS: 20576277e832SShreyas NC case SKL_TKN_U8_BLOCK_TYPE: 20586277e832SShreyas NC case SKL_TKN_U16_BLOCK_SIZE: 20596277e832SShreyas NC return tkn_elem->value; 20606277e832SShreyas NC 20616277e832SShreyas NC default: 2062ecd286a9SColin Ian King dev_err(dev, "Invalid descriptor token %d\n", tkn_elem->token); 20636277e832SShreyas NC break; 20646277e832SShreyas NC } 20656277e832SShreyas NC 20666277e832SShreyas NC return -EINVAL; 20676277e832SShreyas NC } 20686277e832SShreyas NC 20696277e832SShreyas NC /* 20706277e832SShreyas NC * Parse the private data for the token and corresponding value. 20716277e832SShreyas NC * The private data can have multiple data blocks. So, a data block 20726277e832SShreyas NC * is preceded by a descriptor for number of blocks and a descriptor 20736277e832SShreyas NC * for the type and size of the suceeding data block. 20746277e832SShreyas NC */ 20756277e832SShreyas NC static int skl_tplg_get_pvt_data(struct snd_soc_tplg_dapm_widget *tplg_w, 20766277e832SShreyas NC struct skl *skl, struct device *dev, 20776277e832SShreyas NC struct skl_module_cfg *mconfig) 20786277e832SShreyas NC { 20796277e832SShreyas NC struct snd_soc_tplg_vendor_array *array; 20806277e832SShreyas NC int num_blocks, block_size = 0, block_type, off = 0; 20816277e832SShreyas NC char *data; 20826277e832SShreyas NC int ret; 20836277e832SShreyas NC 20846277e832SShreyas NC /* Read the NUM_DATA_BLOCKS descriptor */ 20856277e832SShreyas NC array = (struct snd_soc_tplg_vendor_array *)tplg_w->priv.data; 20866277e832SShreyas NC ret = skl_tplg_get_desc_blocks(dev, array); 20876277e832SShreyas NC if (ret < 0) 20886277e832SShreyas NC return ret; 20896277e832SShreyas NC num_blocks = ret; 20906277e832SShreyas NC 20916277e832SShreyas NC off += array->size; 20926277e832SShreyas NC array = (struct snd_soc_tplg_vendor_array *)(tplg_w->priv.data + off); 20936277e832SShreyas NC 20946277e832SShreyas NC /* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */ 20956277e832SShreyas NC while (num_blocks > 0) { 20966277e832SShreyas NC ret = skl_tplg_get_desc_blocks(dev, array); 20976277e832SShreyas NC 20986277e832SShreyas NC if (ret < 0) 20996277e832SShreyas NC return ret; 21006277e832SShreyas NC block_type = ret; 21016277e832SShreyas NC off += array->size; 21026277e832SShreyas NC 21036277e832SShreyas NC array = (struct snd_soc_tplg_vendor_array *) 21046277e832SShreyas NC (tplg_w->priv.data + off); 21056277e832SShreyas NC 21066277e832SShreyas NC ret = skl_tplg_get_desc_blocks(dev, array); 21076277e832SShreyas NC 21086277e832SShreyas NC if (ret < 0) 21096277e832SShreyas NC return ret; 21106277e832SShreyas NC block_size = ret; 21116277e832SShreyas NC off += array->size; 21126277e832SShreyas NC 21136277e832SShreyas NC array = (struct snd_soc_tplg_vendor_array *) 21146277e832SShreyas NC (tplg_w->priv.data + off); 21156277e832SShreyas NC 21166277e832SShreyas NC data = (tplg_w->priv.data + off); 21176277e832SShreyas NC 21186277e832SShreyas NC if (block_type == SKL_TYPE_TUPLE) { 21196277e832SShreyas NC ret = skl_tplg_get_tokens(dev, data, 21206277e832SShreyas NC skl, mconfig, block_size); 21216277e832SShreyas NC 21226277e832SShreyas NC if (ret < 0) 21236277e832SShreyas NC return ret; 21246277e832SShreyas NC 21256277e832SShreyas NC --num_blocks; 21266277e832SShreyas NC } else { 21276277e832SShreyas NC if (mconfig->formats_config.caps_size > 0) 21286277e832SShreyas NC memcpy(mconfig->formats_config.caps, data, 21296277e832SShreyas NC mconfig->formats_config.caps_size); 21306277e832SShreyas NC --num_blocks; 21316277e832SShreyas NC } 21326277e832SShreyas NC } 21336277e832SShreyas NC 21346277e832SShreyas NC return 0; 21354cd9899fSHardik T Shah } 21364cd9899fSHardik T Shah 2137fe3f4442SDharageswari R static void skl_clear_pin_config(struct snd_soc_platform *platform, 2138fe3f4442SDharageswari R struct snd_soc_dapm_widget *w) 2139fe3f4442SDharageswari R { 2140fe3f4442SDharageswari R int i; 2141fe3f4442SDharageswari R struct skl_module_cfg *mconfig; 2142fe3f4442SDharageswari R struct skl_pipe *pipe; 2143fe3f4442SDharageswari R 2144fe3f4442SDharageswari R if (!strncmp(w->dapm->component->name, platform->component.name, 2145fe3f4442SDharageswari R strlen(platform->component.name))) { 2146fe3f4442SDharageswari R mconfig = w->priv; 2147fe3f4442SDharageswari R pipe = mconfig->pipe; 2148fe3f4442SDharageswari R for (i = 0; i < mconfig->max_in_queue; i++) { 2149fe3f4442SDharageswari R mconfig->m_in_pin[i].in_use = false; 2150fe3f4442SDharageswari R mconfig->m_in_pin[i].pin_state = SKL_PIN_UNBIND; 2151fe3f4442SDharageswari R } 2152fe3f4442SDharageswari R for (i = 0; i < mconfig->max_out_queue; i++) { 2153fe3f4442SDharageswari R mconfig->m_out_pin[i].in_use = false; 2154fe3f4442SDharageswari R mconfig->m_out_pin[i].pin_state = SKL_PIN_UNBIND; 2155fe3f4442SDharageswari R } 2156fe3f4442SDharageswari R pipe->state = SKL_PIPE_INVALID; 2157fe3f4442SDharageswari R mconfig->m_state = SKL_MODULE_UNINIT; 2158fe3f4442SDharageswari R } 2159fe3f4442SDharageswari R } 2160fe3f4442SDharageswari R 2161fe3f4442SDharageswari R void skl_cleanup_resources(struct skl *skl) 2162fe3f4442SDharageswari R { 2163fe3f4442SDharageswari R struct skl_sst *ctx = skl->skl_sst; 2164fe3f4442SDharageswari R struct snd_soc_platform *soc_platform = skl->platform; 2165fe3f4442SDharageswari R struct snd_soc_dapm_widget *w; 2166fe3f4442SDharageswari R struct snd_soc_card *card; 2167fe3f4442SDharageswari R 2168fe3f4442SDharageswari R if (soc_platform == NULL) 2169fe3f4442SDharageswari R return; 2170fe3f4442SDharageswari R 2171fe3f4442SDharageswari R card = soc_platform->component.card; 2172fe3f4442SDharageswari R if (!card || !card->instantiated) 2173fe3f4442SDharageswari R return; 2174fe3f4442SDharageswari R 2175fe3f4442SDharageswari R skl->resource.mem = 0; 2176fe3f4442SDharageswari R skl->resource.mcps = 0; 2177fe3f4442SDharageswari R 2178fe3f4442SDharageswari R list_for_each_entry(w, &card->widgets, list) { 2179fe3f4442SDharageswari R if (is_skl_dsp_widget_type(w) && (w->priv != NULL)) 2180fe3f4442SDharageswari R skl_clear_pin_config(soc_platform, w); 2181fe3f4442SDharageswari R } 2182fe3f4442SDharageswari R 2183fe3f4442SDharageswari R skl_clear_module_cnt(ctx->dsp); 2184fe3f4442SDharageswari R } 2185fe3f4442SDharageswari R 21863af36706SVinod Koul /* 21873af36706SVinod Koul * Topology core widget load callback 21883af36706SVinod Koul * 21893af36706SVinod Koul * This is used to save the private data for each widget which gives 21903af36706SVinod Koul * information to the driver about module and pipeline parameters which DSP 21913af36706SVinod Koul * FW expects like ids, resource values, formats etc 21923af36706SVinod Koul */ 21933af36706SVinod Koul static int skl_tplg_widget_load(struct snd_soc_component *cmpnt, 21943af36706SVinod Koul struct snd_soc_dapm_widget *w, 21953af36706SVinod Koul struct snd_soc_tplg_dapm_widget *tplg_w) 21963af36706SVinod Koul { 21973af36706SVinod Koul int ret; 21983af36706SVinod Koul struct hdac_ext_bus *ebus = snd_soc_component_get_drvdata(cmpnt); 21993af36706SVinod Koul struct skl *skl = ebus_to_skl(ebus); 22003af36706SVinod Koul struct hdac_bus *bus = ebus_to_hbus(ebus); 22013af36706SVinod Koul struct skl_module_cfg *mconfig; 22023af36706SVinod Koul 22033af36706SVinod Koul if (!tplg_w->priv.size) 22043af36706SVinod Koul goto bind_event; 22053af36706SVinod Koul 22063af36706SVinod Koul mconfig = devm_kzalloc(bus->dev, sizeof(*mconfig), GFP_KERNEL); 22073af36706SVinod Koul 22083af36706SVinod Koul if (!mconfig) 22093af36706SVinod Koul return -ENOMEM; 22103af36706SVinod Koul 22113af36706SVinod Koul w->priv = mconfig; 221209305da9SShreyas NC 2213b7c50555SVinod Koul /* 2214b7c50555SVinod Koul * module binary can be loaded later, so set it to query when 2215b7c50555SVinod Koul * module is load for a use case 2216b7c50555SVinod Koul */ 2217b7c50555SVinod Koul mconfig->id.module_id = -1; 22184cd9899fSHardik T Shah 22196277e832SShreyas NC /* Parse private data for tuples */ 22206277e832SShreyas NC ret = skl_tplg_get_pvt_data(tplg_w, skl, bus->dev, mconfig); 22216277e832SShreyas NC if (ret < 0) 22226277e832SShreyas NC return ret; 22233af36706SVinod Koul bind_event: 22243af36706SVinod Koul if (tplg_w->event_type == 0) { 22253373f716SVinod Koul dev_dbg(bus->dev, "ASoC: No event handler required\n"); 22263af36706SVinod Koul return 0; 22273af36706SVinod Koul } 22283af36706SVinod Koul 22293af36706SVinod Koul ret = snd_soc_tplg_widget_bind_event(w, skl_tplg_widget_ops, 2230b663a8c5SJeeja KP ARRAY_SIZE(skl_tplg_widget_ops), 2231b663a8c5SJeeja KP tplg_w->event_type); 22323af36706SVinod Koul 22333af36706SVinod Koul if (ret) { 22343af36706SVinod Koul dev_err(bus->dev, "%s: No matching event handlers found for %d\n", 22353af36706SVinod Koul __func__, tplg_w->event_type); 22363af36706SVinod Koul return -EINVAL; 22373af36706SVinod Koul } 22383af36706SVinod Koul 22393af36706SVinod Koul return 0; 22403af36706SVinod Koul } 22413af36706SVinod Koul 2242140adfbaSJeeja KP static int skl_init_algo_data(struct device *dev, struct soc_bytes_ext *be, 2243140adfbaSJeeja KP struct snd_soc_tplg_bytes_control *bc) 2244140adfbaSJeeja KP { 2245140adfbaSJeeja KP struct skl_algo_data *ac; 2246140adfbaSJeeja KP struct skl_dfw_algo_data *dfw_ac = 2247140adfbaSJeeja KP (struct skl_dfw_algo_data *)bc->priv.data; 2248140adfbaSJeeja KP 2249140adfbaSJeeja KP ac = devm_kzalloc(dev, sizeof(*ac), GFP_KERNEL); 2250140adfbaSJeeja KP if (!ac) 2251140adfbaSJeeja KP return -ENOMEM; 2252140adfbaSJeeja KP 2253140adfbaSJeeja KP /* Fill private data */ 2254140adfbaSJeeja KP ac->max = dfw_ac->max; 2255140adfbaSJeeja KP ac->param_id = dfw_ac->param_id; 2256140adfbaSJeeja KP ac->set_params = dfw_ac->set_params; 22570d682104SDharageswari R ac->size = dfw_ac->max; 2258140adfbaSJeeja KP 2259140adfbaSJeeja KP if (ac->max) { 2260140adfbaSJeeja KP ac->params = (char *) devm_kzalloc(dev, ac->max, GFP_KERNEL); 2261140adfbaSJeeja KP if (!ac->params) 2262140adfbaSJeeja KP return -ENOMEM; 2263140adfbaSJeeja KP 2264140adfbaSJeeja KP memcpy(ac->params, dfw_ac->params, ac->max); 2265140adfbaSJeeja KP } 2266140adfbaSJeeja KP 2267140adfbaSJeeja KP be->dobj.private = ac; 2268140adfbaSJeeja KP return 0; 2269140adfbaSJeeja KP } 2270140adfbaSJeeja KP 2271140adfbaSJeeja KP static int skl_tplg_control_load(struct snd_soc_component *cmpnt, 2272140adfbaSJeeja KP struct snd_kcontrol_new *kctl, 2273140adfbaSJeeja KP struct snd_soc_tplg_ctl_hdr *hdr) 2274140adfbaSJeeja KP { 2275140adfbaSJeeja KP struct soc_bytes_ext *sb; 2276140adfbaSJeeja KP struct snd_soc_tplg_bytes_control *tplg_bc; 2277140adfbaSJeeja KP struct hdac_ext_bus *ebus = snd_soc_component_get_drvdata(cmpnt); 2278140adfbaSJeeja KP struct hdac_bus *bus = ebus_to_hbus(ebus); 2279140adfbaSJeeja KP 2280140adfbaSJeeja KP switch (hdr->ops.info) { 2281140adfbaSJeeja KP case SND_SOC_TPLG_CTL_BYTES: 2282140adfbaSJeeja KP tplg_bc = container_of(hdr, 2283140adfbaSJeeja KP struct snd_soc_tplg_bytes_control, hdr); 2284140adfbaSJeeja KP if (kctl->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { 2285140adfbaSJeeja KP sb = (struct soc_bytes_ext *)kctl->private_value; 2286140adfbaSJeeja KP if (tplg_bc->priv.size) 2287140adfbaSJeeja KP return skl_init_algo_data( 2288140adfbaSJeeja KP bus->dev, sb, tplg_bc); 2289140adfbaSJeeja KP } 2290140adfbaSJeeja KP break; 2291140adfbaSJeeja KP 2292140adfbaSJeeja KP default: 2293140adfbaSJeeja KP dev_warn(bus->dev, "Control load not supported %d:%d:%d\n", 2294140adfbaSJeeja KP hdr->ops.get, hdr->ops.put, hdr->ops.info); 2295140adfbaSJeeja KP break; 2296140adfbaSJeeja KP } 2297140adfbaSJeeja KP 2298140adfbaSJeeja KP return 0; 2299140adfbaSJeeja KP } 2300140adfbaSJeeja KP 2301541070ceSShreyas NC static int skl_tplg_fill_str_mfest_tkn(struct device *dev, 2302541070ceSShreyas NC struct snd_soc_tplg_vendor_string_elem *str_elem, 2303*eee0e16fSJeeja KP struct skl *skl) 2304541070ceSShreyas NC { 2305541070ceSShreyas NC int tkn_count = 0; 2306541070ceSShreyas NC static int ref_count; 2307541070ceSShreyas NC 2308541070ceSShreyas NC switch (str_elem->token) { 2309541070ceSShreyas NC case SKL_TKN_STR_LIB_NAME: 2310*eee0e16fSJeeja KP if (ref_count > skl->skl_sst->lib_count - 1) { 2311541070ceSShreyas NC ref_count = 0; 2312541070ceSShreyas NC return -EINVAL; 2313541070ceSShreyas NC } 2314541070ceSShreyas NC 2315*eee0e16fSJeeja KP strncpy(skl->skl_sst->lib_info[ref_count].name, 2316*eee0e16fSJeeja KP str_elem->string, 2317*eee0e16fSJeeja KP ARRAY_SIZE(skl->skl_sst->lib_info[ref_count].name)); 2318541070ceSShreyas NC ref_count++; 2319541070ceSShreyas NC tkn_count++; 2320541070ceSShreyas NC break; 2321541070ceSShreyas NC 2322541070ceSShreyas NC default: 2323ecd286a9SColin Ian King dev_err(dev, "Not a string token %d\n", str_elem->token); 2324541070ceSShreyas NC break; 2325541070ceSShreyas NC } 2326541070ceSShreyas NC 2327541070ceSShreyas NC return tkn_count; 2328541070ceSShreyas NC } 2329541070ceSShreyas NC 2330541070ceSShreyas NC static int skl_tplg_get_str_tkn(struct device *dev, 2331541070ceSShreyas NC struct snd_soc_tplg_vendor_array *array, 2332*eee0e16fSJeeja KP struct skl *skl) 2333541070ceSShreyas NC { 2334541070ceSShreyas NC int tkn_count = 0, ret; 2335541070ceSShreyas NC struct snd_soc_tplg_vendor_string_elem *str_elem; 2336541070ceSShreyas NC 2337541070ceSShreyas NC str_elem = (struct snd_soc_tplg_vendor_string_elem *)array->value; 2338541070ceSShreyas NC while (tkn_count < array->num_elems) { 2339*eee0e16fSJeeja KP ret = skl_tplg_fill_str_mfest_tkn(dev, str_elem, skl); 2340541070ceSShreyas NC str_elem++; 2341541070ceSShreyas NC 2342541070ceSShreyas NC if (ret < 0) 2343541070ceSShreyas NC return ret; 2344541070ceSShreyas NC 2345541070ceSShreyas NC tkn_count = tkn_count + ret; 2346541070ceSShreyas NC } 2347541070ceSShreyas NC 2348541070ceSShreyas NC return tkn_count; 2349541070ceSShreyas NC } 2350541070ceSShreyas NC 2351541070ceSShreyas NC static int skl_tplg_get_int_tkn(struct device *dev, 2352541070ceSShreyas NC struct snd_soc_tplg_vendor_value_elem *tkn_elem, 2353*eee0e16fSJeeja KP struct skl *skl) 2354541070ceSShreyas NC { 2355541070ceSShreyas NC int tkn_count = 0; 2356541070ceSShreyas NC 2357541070ceSShreyas NC switch (tkn_elem->token) { 2358541070ceSShreyas NC case SKL_TKN_U32_LIB_COUNT: 2359*eee0e16fSJeeja KP skl->skl_sst->lib_count = tkn_elem->value; 2360541070ceSShreyas NC tkn_count++; 2361541070ceSShreyas NC break; 2362541070ceSShreyas NC 2363541070ceSShreyas NC default: 2364ecd286a9SColin Ian King dev_err(dev, "Not a manifest token %d\n", tkn_elem->token); 2365541070ceSShreyas NC return -EINVAL; 2366541070ceSShreyas NC } 2367541070ceSShreyas NC 2368541070ceSShreyas NC return tkn_count; 2369541070ceSShreyas NC } 2370541070ceSShreyas NC 2371541070ceSShreyas NC /* 2372541070ceSShreyas NC * Fill the manifest structure by parsing the tokens based on the 2373541070ceSShreyas NC * type. 2374541070ceSShreyas NC */ 2375541070ceSShreyas NC static int skl_tplg_get_manifest_tkn(struct device *dev, 2376*eee0e16fSJeeja KP char *pvt_data, struct skl *skl, 2377541070ceSShreyas NC int block_size) 2378541070ceSShreyas NC { 2379541070ceSShreyas NC int tkn_count = 0, ret; 2380541070ceSShreyas NC int off = 0, tuple_size = 0; 2381541070ceSShreyas NC struct snd_soc_tplg_vendor_array *array; 2382541070ceSShreyas NC struct snd_soc_tplg_vendor_value_elem *tkn_elem; 2383541070ceSShreyas NC 2384541070ceSShreyas NC if (block_size <= 0) 2385541070ceSShreyas NC return -EINVAL; 2386541070ceSShreyas NC 2387541070ceSShreyas NC while (tuple_size < block_size) { 2388541070ceSShreyas NC array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off); 2389541070ceSShreyas NC off += array->size; 2390541070ceSShreyas NC switch (array->type) { 2391541070ceSShreyas NC case SND_SOC_TPLG_TUPLE_TYPE_STRING: 2392*eee0e16fSJeeja KP ret = skl_tplg_get_str_tkn(dev, array, skl); 2393541070ceSShreyas NC 2394541070ceSShreyas NC if (ret < 0) 2395541070ceSShreyas NC return ret; 2396541070ceSShreyas NC tkn_count += ret; 2397541070ceSShreyas NC 2398541070ceSShreyas NC tuple_size += tkn_count * 2399541070ceSShreyas NC sizeof(struct snd_soc_tplg_vendor_string_elem); 2400541070ceSShreyas NC continue; 2401541070ceSShreyas NC 2402541070ceSShreyas NC case SND_SOC_TPLG_TUPLE_TYPE_UUID: 2403ecd286a9SColin Ian King dev_warn(dev, "no uuid tokens for skl tplf manifest\n"); 2404541070ceSShreyas NC continue; 2405541070ceSShreyas NC 2406541070ceSShreyas NC default: 2407541070ceSShreyas NC tkn_elem = array->value; 2408541070ceSShreyas NC tkn_count = 0; 2409541070ceSShreyas NC break; 2410541070ceSShreyas NC } 2411541070ceSShreyas NC 2412541070ceSShreyas NC while (tkn_count <= array->num_elems - 1) { 2413541070ceSShreyas NC ret = skl_tplg_get_int_tkn(dev, 2414*eee0e16fSJeeja KP tkn_elem, skl); 2415541070ceSShreyas NC if (ret < 0) 2416541070ceSShreyas NC return ret; 2417541070ceSShreyas NC 2418541070ceSShreyas NC tkn_count = tkn_count + ret; 2419541070ceSShreyas NC tkn_elem++; 2420541070ceSShreyas NC tuple_size += tkn_count * 2421541070ceSShreyas NC sizeof(struct snd_soc_tplg_vendor_value_elem); 2422541070ceSShreyas NC break; 2423541070ceSShreyas NC } 2424541070ceSShreyas NC tkn_count = 0; 2425541070ceSShreyas NC } 2426541070ceSShreyas NC 2427541070ceSShreyas NC return 0; 2428541070ceSShreyas NC } 2429541070ceSShreyas NC 2430541070ceSShreyas NC /* 2431541070ceSShreyas NC * Parse manifest private data for tokens. The private data block is 2432541070ceSShreyas NC * preceded by descriptors for type and size of data block. 2433541070ceSShreyas NC */ 2434541070ceSShreyas NC static int skl_tplg_get_manifest_data(struct snd_soc_tplg_manifest *manifest, 2435*eee0e16fSJeeja KP struct device *dev, struct skl *skl) 2436541070ceSShreyas NC { 2437541070ceSShreyas NC struct snd_soc_tplg_vendor_array *array; 2438541070ceSShreyas NC int num_blocks, block_size = 0, block_type, off = 0; 2439541070ceSShreyas NC char *data; 2440541070ceSShreyas NC int ret; 2441541070ceSShreyas NC 2442541070ceSShreyas NC /* Read the NUM_DATA_BLOCKS descriptor */ 2443541070ceSShreyas NC array = (struct snd_soc_tplg_vendor_array *)manifest->priv.data; 2444541070ceSShreyas NC ret = skl_tplg_get_desc_blocks(dev, array); 2445541070ceSShreyas NC if (ret < 0) 2446541070ceSShreyas NC return ret; 2447541070ceSShreyas NC num_blocks = ret; 2448541070ceSShreyas NC 2449541070ceSShreyas NC off += array->size; 2450541070ceSShreyas NC array = (struct snd_soc_tplg_vendor_array *) 2451541070ceSShreyas NC (manifest->priv.data + off); 2452541070ceSShreyas NC 2453541070ceSShreyas NC /* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */ 2454541070ceSShreyas NC while (num_blocks > 0) { 2455541070ceSShreyas NC ret = skl_tplg_get_desc_blocks(dev, array); 2456541070ceSShreyas NC 2457541070ceSShreyas NC if (ret < 0) 2458541070ceSShreyas NC return ret; 2459541070ceSShreyas NC block_type = ret; 2460541070ceSShreyas NC off += array->size; 2461541070ceSShreyas NC 2462541070ceSShreyas NC array = (struct snd_soc_tplg_vendor_array *) 2463541070ceSShreyas NC (manifest->priv.data + off); 2464541070ceSShreyas NC 2465541070ceSShreyas NC ret = skl_tplg_get_desc_blocks(dev, array); 2466541070ceSShreyas NC 2467541070ceSShreyas NC if (ret < 0) 2468541070ceSShreyas NC return ret; 2469541070ceSShreyas NC block_size = ret; 2470541070ceSShreyas NC off += array->size; 2471541070ceSShreyas NC 2472541070ceSShreyas NC array = (struct snd_soc_tplg_vendor_array *) 2473541070ceSShreyas NC (manifest->priv.data + off); 2474541070ceSShreyas NC 2475541070ceSShreyas NC data = (manifest->priv.data + off); 2476541070ceSShreyas NC 2477541070ceSShreyas NC if (block_type == SKL_TYPE_TUPLE) { 2478*eee0e16fSJeeja KP ret = skl_tplg_get_manifest_tkn(dev, data, skl, 2479541070ceSShreyas NC block_size); 2480541070ceSShreyas NC 2481541070ceSShreyas NC if (ret < 0) 2482541070ceSShreyas NC return ret; 2483541070ceSShreyas NC 2484541070ceSShreyas NC --num_blocks; 2485541070ceSShreyas NC } else { 2486541070ceSShreyas NC return -EINVAL; 2487541070ceSShreyas NC } 2488541070ceSShreyas NC } 2489541070ceSShreyas NC 2490541070ceSShreyas NC return 0; 2491541070ceSShreyas NC } 2492541070ceSShreyas NC 249315ecaba9SKranthi G static int skl_manifest_load(struct snd_soc_component *cmpnt, 249415ecaba9SKranthi G struct snd_soc_tplg_manifest *manifest) 249515ecaba9SKranthi G { 249615ecaba9SKranthi G struct hdac_ext_bus *ebus = snd_soc_component_get_drvdata(cmpnt); 249715ecaba9SKranthi G struct hdac_bus *bus = ebus_to_hbus(ebus); 249815ecaba9SKranthi G struct skl *skl = ebus_to_skl(ebus); 249915ecaba9SKranthi G 2500c15ad605SVinod Koul /* proceed only if we have private data defined */ 2501c15ad605SVinod Koul if (manifest->priv.size == 0) 2502c15ad605SVinod Koul return 0; 2503c15ad605SVinod Koul 2504*eee0e16fSJeeja KP skl_tplg_get_manifest_data(manifest, bus->dev, skl); 2505541070ceSShreyas NC 2506*eee0e16fSJeeja KP if (skl->skl_sst->lib_count > SKL_MAX_LIB) { 250715ecaba9SKranthi G dev_err(bus->dev, "Exceeding max Library count. Got:%d\n", 2508*eee0e16fSJeeja KP skl->skl_sst->lib_count); 2509*eee0e16fSJeeja KP return -EINVAL; 251015ecaba9SKranthi G } 251115ecaba9SKranthi G 2512*eee0e16fSJeeja KP return 0; 251315ecaba9SKranthi G } 251415ecaba9SKranthi G 25153af36706SVinod Koul static struct snd_soc_tplg_ops skl_tplg_ops = { 25163af36706SVinod Koul .widget_load = skl_tplg_widget_load, 2517140adfbaSJeeja KP .control_load = skl_tplg_control_load, 2518140adfbaSJeeja KP .bytes_ext_ops = skl_tlv_ops, 2519140adfbaSJeeja KP .bytes_ext_ops_count = ARRAY_SIZE(skl_tlv_ops), 252015ecaba9SKranthi G .manifest = skl_manifest_load, 25213af36706SVinod Koul }; 25223af36706SVinod Koul 2523287af4f9SJeeja KP /* 2524287af4f9SJeeja KP * A pipe can have multiple modules, each of them will be a DAPM widget as 2525287af4f9SJeeja KP * well. While managing a pipeline we need to get the list of all the 2526287af4f9SJeeja KP * widgets in a pipelines, so this helper - skl_tplg_create_pipe_widget_list() 2527287af4f9SJeeja KP * helps to get the SKL type widgets in that pipeline 2528287af4f9SJeeja KP */ 2529287af4f9SJeeja KP static int skl_tplg_create_pipe_widget_list(struct snd_soc_platform *platform) 2530287af4f9SJeeja KP { 2531287af4f9SJeeja KP struct snd_soc_dapm_widget *w; 2532287af4f9SJeeja KP struct skl_module_cfg *mcfg = NULL; 2533287af4f9SJeeja KP struct skl_pipe_module *p_module = NULL; 2534287af4f9SJeeja KP struct skl_pipe *pipe; 2535287af4f9SJeeja KP 2536287af4f9SJeeja KP list_for_each_entry(w, &platform->component.card->widgets, list) { 2537287af4f9SJeeja KP if (is_skl_dsp_widget_type(w) && w->priv != NULL) { 2538287af4f9SJeeja KP mcfg = w->priv; 2539287af4f9SJeeja KP pipe = mcfg->pipe; 2540287af4f9SJeeja KP 2541287af4f9SJeeja KP p_module = devm_kzalloc(platform->dev, 2542287af4f9SJeeja KP sizeof(*p_module), GFP_KERNEL); 2543287af4f9SJeeja KP if (!p_module) 2544287af4f9SJeeja KP return -ENOMEM; 2545287af4f9SJeeja KP 2546287af4f9SJeeja KP p_module->w = w; 2547287af4f9SJeeja KP list_add_tail(&p_module->node, &pipe->w_list); 2548287af4f9SJeeja KP } 2549287af4f9SJeeja KP } 2550287af4f9SJeeja KP 2551287af4f9SJeeja KP return 0; 2552287af4f9SJeeja KP } 2553287af4f9SJeeja KP 2554f0aa94faSJeeja KP static void skl_tplg_set_pipe_type(struct skl *skl, struct skl_pipe *pipe) 2555f0aa94faSJeeja KP { 2556f0aa94faSJeeja KP struct skl_pipe_module *w_module; 2557f0aa94faSJeeja KP struct snd_soc_dapm_widget *w; 2558f0aa94faSJeeja KP struct skl_module_cfg *mconfig; 2559f0aa94faSJeeja KP bool host_found = false, link_found = false; 2560f0aa94faSJeeja KP 2561f0aa94faSJeeja KP list_for_each_entry(w_module, &pipe->w_list, node) { 2562f0aa94faSJeeja KP w = w_module->w; 2563f0aa94faSJeeja KP mconfig = w->priv; 2564f0aa94faSJeeja KP 2565f0aa94faSJeeja KP if (mconfig->dev_type == SKL_DEVICE_HDAHOST) 2566f0aa94faSJeeja KP host_found = true; 2567f0aa94faSJeeja KP else if (mconfig->dev_type != SKL_DEVICE_NONE) 2568f0aa94faSJeeja KP link_found = true; 2569f0aa94faSJeeja KP } 2570f0aa94faSJeeja KP 2571f0aa94faSJeeja KP if (host_found && link_found) 2572f0aa94faSJeeja KP pipe->passthru = true; 2573f0aa94faSJeeja KP else 2574f0aa94faSJeeja KP pipe->passthru = false; 2575f0aa94faSJeeja KP } 2576f0aa94faSJeeja KP 25773af36706SVinod Koul /* This will be read from topology manifest, currently defined here */ 25783af36706SVinod Koul #define SKL_MAX_MCPS 30000000 25793af36706SVinod Koul #define SKL_FW_MAX_MEM 1000000 25803af36706SVinod Koul 25813af36706SVinod Koul /* 25823af36706SVinod Koul * SKL topology init routine 25833af36706SVinod Koul */ 25843af36706SVinod Koul int skl_tplg_init(struct snd_soc_platform *platform, struct hdac_ext_bus *ebus) 25853af36706SVinod Koul { 25863af36706SVinod Koul int ret; 25873af36706SVinod Koul const struct firmware *fw; 25883af36706SVinod Koul struct hdac_bus *bus = ebus_to_hbus(ebus); 25893af36706SVinod Koul struct skl *skl = ebus_to_skl(ebus); 2590f0aa94faSJeeja KP struct skl_pipeline *ppl; 25913af36706SVinod Koul 25924b235c43SVinod Koul ret = request_firmware(&fw, skl->tplg_name, bus->dev); 25933af36706SVinod Koul if (ret < 0) { 2594b663a8c5SJeeja KP dev_err(bus->dev, "tplg fw %s load failed with %d\n", 25954b235c43SVinod Koul skl->tplg_name, ret); 25964b235c43SVinod Koul ret = request_firmware(&fw, "dfw_sst.bin", bus->dev); 25974b235c43SVinod Koul if (ret < 0) { 25984b235c43SVinod Koul dev_err(bus->dev, "Fallback tplg fw %s load failed with %d\n", 25993af36706SVinod Koul "dfw_sst.bin", ret); 26003af36706SVinod Koul return ret; 26013af36706SVinod Koul } 26024b235c43SVinod Koul } 26033af36706SVinod Koul 26043af36706SVinod Koul /* 26053af36706SVinod Koul * The complete tplg for SKL is loaded as index 0, we don't use 26063af36706SVinod Koul * any other index 26073af36706SVinod Koul */ 2608b663a8c5SJeeja KP ret = snd_soc_tplg_component_load(&platform->component, 2609b663a8c5SJeeja KP &skl_tplg_ops, fw, 0); 26103af36706SVinod Koul if (ret < 0) { 26113af36706SVinod Koul dev_err(bus->dev, "tplg component load failed%d\n", ret); 2612c14a82c7SSudip Mukherjee release_firmware(fw); 26133af36706SVinod Koul return -EINVAL; 26143af36706SVinod Koul } 26153af36706SVinod Koul 26163af36706SVinod Koul skl->resource.max_mcps = SKL_MAX_MCPS; 26173af36706SVinod Koul skl->resource.max_mem = SKL_FW_MAX_MEM; 26183af36706SVinod Koul 2619d8018361SVinod Koul skl->tplg = fw; 2620287af4f9SJeeja KP ret = skl_tplg_create_pipe_widget_list(platform); 2621287af4f9SJeeja KP if (ret < 0) 2622287af4f9SJeeja KP return ret; 2623d8018361SVinod Koul 2624f0aa94faSJeeja KP list_for_each_entry(ppl, &skl->ppl_list, node) 2625f0aa94faSJeeja KP skl_tplg_set_pipe_type(skl, ppl->pipe); 26263af36706SVinod Koul 26273af36706SVinod Koul return 0; 2628e4e2d2f4SJeeja KP } 2629