1 // SPDX-License-Identifier: GPL-2.0-only
3 * skl-topology.c - Implements Platform component ALSA controls/widget
6 * Copyright (C) 2014-2015 Intel Corp
7 * Author: Jeeja KP <jeeja.kp@intel.com>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 #include <linux/slab.h>
12 #include <linux/types.h>
13 #include <linux/firmware.h>
14 #include <linux/uuid.h>
15 #include <sound/intel-nhlt.h>
16 #include <sound/soc.h>
17 #include <sound/soc-acpi.h>
18 #include <sound/soc-topology.h>
19 #include <uapi/sound/snd_sst_tokens.h>
20 #include <uapi/sound/skl-tplg-interface.h>
21 #include "skl-sst-dsp.h"
22 #include "skl-sst-ipc.h"
23 #include "skl-topology.h"
25 #include "../common/sst-dsp.h"
26 #include "../common/sst-dsp-priv.h"
28 #define SKL_CH_FIXUP_MASK (1 << 0)
29 #define SKL_RATE_FIXUP_MASK (1 << 1)
30 #define SKL_FMT_FIXUP_MASK (1 << 2)
31 #define SKL_IN_DIR_BIT_MASK BIT(0)
32 #define SKL_PIN_COUNT_MASK GENMASK(7, 4)
34 static const int mic_mono_list[] = {
37 static const int mic_stereo_list[][SKL_CH_STEREO] = {
38 {0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 3}, {2, 3},
40 static const int mic_trio_list[][SKL_CH_TRIO] = {
41 {0, 1, 2}, {0, 1, 3}, {0, 2, 3}, {1, 2, 3},
43 static const int mic_quatro_list[][SKL_CH_QUATRO] = {
47 #define CHECK_HW_PARAMS(ch, freq, bps, prm_ch, prm_freq, prm_bps) \
48 ((ch == prm_ch) && (bps == prm_bps) && (freq == prm_freq))
50 void skl_tplg_d0i3_get(struct skl_dev *skl, enum d0i3_capability caps)
52 struct skl_d0i3_data *d0i3 = &skl->d0i3;
59 case SKL_D0I3_STREAMING:
63 case SKL_D0I3_NON_STREAMING:
64 d0i3->non_streaming++;
69 void skl_tplg_d0i3_put(struct skl_dev *skl, enum d0i3_capability caps)
71 struct skl_d0i3_data *d0i3 = &skl->d0i3;
78 case SKL_D0I3_STREAMING:
82 case SKL_D0I3_NON_STREAMING:
83 d0i3->non_streaming--;
89 * SKL DSP driver modelling uses only few DAPM widgets so for rest we will
90 * ignore. This helpers checks if the SKL driver handles this widget type
92 static int is_skl_dsp_widget_type(struct snd_soc_dapm_widget *w,
95 if (w->dapm->dev != dev)
99 case snd_soc_dapm_dai_link:
100 case snd_soc_dapm_dai_in:
101 case snd_soc_dapm_aif_in:
102 case snd_soc_dapm_aif_out:
103 case snd_soc_dapm_dai_out:
104 case snd_soc_dapm_switch:
105 case snd_soc_dapm_output:
106 case snd_soc_dapm_mux:
114 static void skl_dump_mconfig(struct skl_dev *skl, struct skl_module_cfg *mcfg)
116 struct skl_module_iface *iface = &mcfg->module->formats[mcfg->fmt_idx];
118 dev_dbg(skl->dev, "Dumping config\n");
119 dev_dbg(skl->dev, "Input Format:\n");
120 dev_dbg(skl->dev, "channels = %d\n", iface->inputs[0].fmt.channels);
121 dev_dbg(skl->dev, "s_freq = %d\n", iface->inputs[0].fmt.s_freq);
122 dev_dbg(skl->dev, "ch_cfg = %d\n", iface->inputs[0].fmt.ch_cfg);
123 dev_dbg(skl->dev, "valid bit depth = %d\n",
124 iface->inputs[0].fmt.valid_bit_depth);
125 dev_dbg(skl->dev, "Output Format:\n");
126 dev_dbg(skl->dev, "channels = %d\n", iface->outputs[0].fmt.channels);
127 dev_dbg(skl->dev, "s_freq = %d\n", iface->outputs[0].fmt.s_freq);
128 dev_dbg(skl->dev, "valid bit depth = %d\n",
129 iface->outputs[0].fmt.valid_bit_depth);
130 dev_dbg(skl->dev, "ch_cfg = %d\n", iface->outputs[0].fmt.ch_cfg);
133 static void skl_tplg_update_chmap(struct skl_module_fmt *fmt, int chs)
135 int slot_map = 0xFFFFFFFF;
139 for (i = 0; i < chs; i++) {
141 * For 2 channels with starting slot as 0, slot map will
142 * look like 0xFFFFFF10.
144 slot_map &= (~(0xF << (4 * i)) | (start_slot << (4 * i)));
147 fmt->ch_map = slot_map;
150 static void skl_tplg_update_params(struct skl_module_fmt *fmt,
151 struct skl_pipe_params *params, int fixup)
153 if (fixup & SKL_RATE_FIXUP_MASK)
154 fmt->s_freq = params->s_freq;
155 if (fixup & SKL_CH_FIXUP_MASK) {
156 fmt->channels = params->ch;
157 skl_tplg_update_chmap(fmt, fmt->channels);
159 if (fixup & SKL_FMT_FIXUP_MASK) {
160 fmt->valid_bit_depth = skl_get_bit_depth(params->s_fmt);
163 * 16 bit is 16 bit container whereas 24 bit is in 32 bit
164 * container so update bit depth accordingly
166 switch (fmt->valid_bit_depth) {
167 case SKL_DEPTH_16BIT:
168 fmt->bit_depth = fmt->valid_bit_depth;
172 fmt->bit_depth = SKL_DEPTH_32BIT;
180 * A pipeline may have modules which impact the pcm parameters, like SRC,
181 * channel converter, format converter.
182 * We need to calculate the output params by applying the 'fixup'
183 * Topology will tell driver which type of fixup is to be applied by
184 * supplying the fixup mask, so based on that we calculate the output
186 * Now In FE the pcm hw_params is source/target format. Same is applicable
187 * for BE with its hw_params invoked.
188 * here based on FE, BE pipeline and direction we calculate the input and
189 * outfix and then apply that for a module
191 static void skl_tplg_update_params_fixup(struct skl_module_cfg *m_cfg,
192 struct skl_pipe_params *params, bool is_fe)
194 int in_fixup, out_fixup;
195 struct skl_module_fmt *in_fmt, *out_fmt;
197 /* Fixups will be applied to pin 0 only */
198 in_fmt = &m_cfg->module->formats[m_cfg->fmt_idx].inputs[0].fmt;
199 out_fmt = &m_cfg->module->formats[m_cfg->fmt_idx].outputs[0].fmt;
201 if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
203 in_fixup = m_cfg->params_fixup;
204 out_fixup = (~m_cfg->converter) &
207 out_fixup = m_cfg->params_fixup;
208 in_fixup = (~m_cfg->converter) &
213 out_fixup = m_cfg->params_fixup;
214 in_fixup = (~m_cfg->converter) &
217 in_fixup = m_cfg->params_fixup;
218 out_fixup = (~m_cfg->converter) &
223 skl_tplg_update_params(in_fmt, params, in_fixup);
224 skl_tplg_update_params(out_fmt, params, out_fixup);
228 * A module needs input and output buffers, which are dependent upon pcm
229 * params, so once we have calculate params, we need buffer calculation as
232 static void skl_tplg_update_buffer_size(struct skl_dev *skl,
233 struct skl_module_cfg *mcfg)
236 struct skl_module_fmt *in_fmt, *out_fmt;
237 struct skl_module_res *res;
239 /* Since fixups is applied to pin 0 only, ibs, obs needs
240 * change for pin 0 only
242 res = &mcfg->module->resources[mcfg->res_idx];
243 in_fmt = &mcfg->module->formats[mcfg->fmt_idx].inputs[0].fmt;
244 out_fmt = &mcfg->module->formats[mcfg->fmt_idx].outputs[0].fmt;
246 if (mcfg->m_type == SKL_MODULE_TYPE_SRCINT)
249 res->ibs = DIV_ROUND_UP(in_fmt->s_freq, 1000) *
250 in_fmt->channels * (in_fmt->bit_depth >> 3) *
253 res->obs = DIV_ROUND_UP(out_fmt->s_freq, 1000) *
254 out_fmt->channels * (out_fmt->bit_depth >> 3) *
258 static u8 skl_tplg_be_dev_type(int dev_type)
264 ret = NHLT_DEVICE_BT;
267 case SKL_DEVICE_DMIC:
268 ret = NHLT_DEVICE_DMIC;
272 ret = NHLT_DEVICE_I2S;
276 ret = NHLT_DEVICE_INVALID;
283 static int skl_tplg_update_be_blob(struct snd_soc_dapm_widget *w,
286 struct skl_module_cfg *m_cfg = w->priv;
288 u32 ch, s_freq, s_fmt;
289 struct nhlt_specific_cfg *cfg;
290 u8 dev_type = skl_tplg_be_dev_type(m_cfg->dev_type);
291 int fmt_idx = m_cfg->fmt_idx;
292 struct skl_module_iface *m_iface = &m_cfg->module->formats[fmt_idx];
294 /* check if we already have blob */
295 if (m_cfg->formats_config.caps_size > 0)
298 dev_dbg(skl->dev, "Applying default cfg blob\n");
299 switch (m_cfg->dev_type) {
300 case SKL_DEVICE_DMIC:
301 link_type = NHLT_LINK_DMIC;
302 dir = SNDRV_PCM_STREAM_CAPTURE;
303 s_freq = m_iface->inputs[0].fmt.s_freq;
304 s_fmt = m_iface->inputs[0].fmt.bit_depth;
305 ch = m_iface->inputs[0].fmt.channels;
309 link_type = NHLT_LINK_SSP;
310 if (m_cfg->hw_conn_type == SKL_CONN_SOURCE) {
311 dir = SNDRV_PCM_STREAM_PLAYBACK;
312 s_freq = m_iface->outputs[0].fmt.s_freq;
313 s_fmt = m_iface->outputs[0].fmt.bit_depth;
314 ch = m_iface->outputs[0].fmt.channels;
316 dir = SNDRV_PCM_STREAM_CAPTURE;
317 s_freq = m_iface->inputs[0].fmt.s_freq;
318 s_fmt = m_iface->inputs[0].fmt.bit_depth;
319 ch = m_iface->inputs[0].fmt.channels;
327 /* update the blob based on virtual bus_id and default params */
328 cfg = skl_get_ep_blob(skl, m_cfg->vbus_id, link_type,
329 s_fmt, ch, s_freq, dir, dev_type);
331 m_cfg->formats_config.caps_size = cfg->size;
332 m_cfg->formats_config.caps = (u32 *) &cfg->caps;
334 dev_err(skl->dev, "Blob NULL for id %x type %d dirn %d\n",
335 m_cfg->vbus_id, link_type, dir);
336 dev_err(skl->dev, "PCM: ch %d, freq %d, fmt %d\n",
344 static void skl_tplg_update_module_params(struct snd_soc_dapm_widget *w,
347 struct skl_module_cfg *m_cfg = w->priv;
348 struct skl_pipe_params *params = m_cfg->pipe->p_params;
349 int p_conn_type = m_cfg->pipe->conn_type;
352 if (!m_cfg->params_fixup)
355 dev_dbg(skl->dev, "Mconfig for widget=%s BEFORE updation\n",
358 skl_dump_mconfig(skl, m_cfg);
360 if (p_conn_type == SKL_PIPE_CONN_TYPE_FE)
365 skl_tplg_update_params_fixup(m_cfg, params, is_fe);
366 skl_tplg_update_buffer_size(skl, m_cfg);
368 dev_dbg(skl->dev, "Mconfig for widget=%s AFTER updation\n",
371 skl_dump_mconfig(skl, m_cfg);
375 * some modules can have multiple params set from user control and
376 * need to be set after module is initialized. If set_param flag is
377 * set module params will be done after module is initialised.
379 static int skl_tplg_set_module_params(struct snd_soc_dapm_widget *w,
383 struct skl_module_cfg *mconfig = w->priv;
384 const struct snd_kcontrol_new *k;
385 struct soc_bytes_ext *sb;
386 struct skl_algo_data *bc;
387 struct skl_specific_cfg *sp_cfg;
389 if (mconfig->formats_config.caps_size > 0 &&
390 mconfig->formats_config.set_params == SKL_PARAM_SET) {
391 sp_cfg = &mconfig->formats_config;
392 ret = skl_set_module_params(skl, sp_cfg->caps,
394 sp_cfg->param_id, mconfig);
399 for (i = 0; i < w->num_kcontrols; i++) {
400 k = &w->kcontrol_news[i];
401 if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
402 sb = (void *) k->private_value;
403 bc = (struct skl_algo_data *)sb->dobj.private;
405 if (bc->set_params == SKL_PARAM_SET) {
406 ret = skl_set_module_params(skl,
407 (u32 *)bc->params, bc->size,
408 bc->param_id, mconfig);
419 * some module param can set from user control and this is required as
420 * when module is initailzed. if module param is required in init it is
421 * identifed by set_param flag. if set_param flag is not set, then this
422 * parameter needs to set as part of module init.
424 static int skl_tplg_set_module_init_data(struct snd_soc_dapm_widget *w)
426 const struct snd_kcontrol_new *k;
427 struct soc_bytes_ext *sb;
428 struct skl_algo_data *bc;
429 struct skl_module_cfg *mconfig = w->priv;
432 for (i = 0; i < w->num_kcontrols; i++) {
433 k = &w->kcontrol_news[i];
434 if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
435 sb = (struct soc_bytes_ext *)k->private_value;
436 bc = (struct skl_algo_data *)sb->dobj.private;
438 if (bc->set_params != SKL_PARAM_INIT)
441 mconfig->formats_config.caps = (u32 *)bc->params;
442 mconfig->formats_config.caps_size = bc->size;
451 static int skl_tplg_module_prepare(struct skl_dev *skl, struct skl_pipe *pipe,
452 struct snd_soc_dapm_widget *w, struct skl_module_cfg *mcfg)
454 switch (mcfg->dev_type) {
455 case SKL_DEVICE_HDAHOST:
456 return skl_pcm_host_dma_prepare(skl->dev, pipe->p_params);
458 case SKL_DEVICE_HDALINK:
459 return skl_pcm_link_dma_prepare(skl->dev, pipe->p_params);
466 * Inside a pipe instance, we can have various modules. These modules need
467 * to instantiated in DSP by invoking INIT_MODULE IPC, which is achieved by
468 * skl_init_module() routine, so invoke that for all modules in a pipeline
471 skl_tplg_init_pipe_modules(struct skl_dev *skl, struct skl_pipe *pipe)
473 struct skl_pipe_module *w_module;
474 struct snd_soc_dapm_widget *w;
475 struct skl_module_cfg *mconfig;
479 list_for_each_entry(w_module, &pipe->w_list, node) {
484 /* check if module ids are populated */
485 if (mconfig->id.module_id < 0) {
487 "module %pUL id not populated\n",
488 (guid_t *)mconfig->guid);
492 cfg_idx = mconfig->pipe->cur_config_idx;
493 mconfig->fmt_idx = mconfig->mod_cfg[cfg_idx].fmt_idx;
494 mconfig->res_idx = mconfig->mod_cfg[cfg_idx].res_idx;
496 if (mconfig->module->loadable && skl->dsp->fw_ops.load_mod) {
497 ret = skl->dsp->fw_ops.load_mod(skl->dsp,
498 mconfig->id.module_id, mconfig->guid);
502 mconfig->m_state = SKL_MODULE_LOADED;
505 /* prepare the DMA if the module is gateway cpr */
506 ret = skl_tplg_module_prepare(skl, pipe, w, mconfig);
510 /* update blob if blob is null for be with default value */
511 skl_tplg_update_be_blob(w, skl);
514 * apply fix/conversion to module params based on
517 skl_tplg_update_module_params(w, skl);
518 uuid_mod = (guid_t *)mconfig->guid;
519 mconfig->id.pvt_id = skl_get_pvt_id(skl, uuid_mod,
520 mconfig->id.instance_id);
521 if (mconfig->id.pvt_id < 0)
523 skl_tplg_set_module_init_data(w);
525 ret = skl_dsp_get_core(skl->dsp, mconfig->core_id);
527 dev_err(skl->dev, "Failed to wake up core %d ret=%d\n",
528 mconfig->core_id, ret);
532 ret = skl_init_module(skl, mconfig);
534 skl_put_pvt_id(skl, uuid_mod, &mconfig->id.pvt_id);
538 ret = skl_tplg_set_module_params(w, skl);
545 skl_dsp_put_core(skl->dsp, mconfig->core_id);
549 static int skl_tplg_unload_pipe_modules(struct skl_dev *skl,
550 struct skl_pipe *pipe)
553 struct skl_pipe_module *w_module;
554 struct skl_module_cfg *mconfig;
556 list_for_each_entry(w_module, &pipe->w_list, node) {
558 mconfig = w_module->w->priv;
559 uuid_mod = (guid_t *)mconfig->guid;
561 if (mconfig->module->loadable && skl->dsp->fw_ops.unload_mod &&
562 mconfig->m_state > SKL_MODULE_UNINIT) {
563 ret = skl->dsp->fw_ops.unload_mod(skl->dsp,
564 mconfig->id.module_id);
568 skl_put_pvt_id(skl, uuid_mod, &mconfig->id.pvt_id);
570 ret = skl_dsp_put_core(skl->dsp, mconfig->core_id);
572 /* don't return; continue with other modules */
573 dev_err(skl->dev, "Failed to sleep core %d ret=%d\n",
574 mconfig->core_id, ret);
578 /* no modules to unload in this path, so return */
582 static bool skl_tplg_is_multi_fmt(struct skl_dev *skl, struct skl_pipe *pipe)
584 struct skl_pipe_fmt *cur_fmt;
585 struct skl_pipe_fmt *next_fmt;
588 if (pipe->nr_cfgs <= 1)
591 if (pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
594 for (i = 0; i < pipe->nr_cfgs - 1; i++) {
595 if (pipe->direction == SNDRV_PCM_STREAM_PLAYBACK) {
596 cur_fmt = &pipe->configs[i].out_fmt;
597 next_fmt = &pipe->configs[i + 1].out_fmt;
599 cur_fmt = &pipe->configs[i].in_fmt;
600 next_fmt = &pipe->configs[i + 1].in_fmt;
603 if (!CHECK_HW_PARAMS(cur_fmt->channels, cur_fmt->freq,
615 * Here, we select pipe format based on the pipe type and pipe
616 * direction to determine the current config index for the pipeline.
617 * The config index is then used to select proper module resources.
618 * Intermediate pipes currently have a fixed format hence we select the
619 * 0th configuratation by default for such pipes.
622 skl_tplg_get_pipe_config(struct skl_dev *skl, struct skl_module_cfg *mconfig)
624 struct skl_pipe *pipe = mconfig->pipe;
625 struct skl_pipe_params *params = pipe->p_params;
626 struct skl_path_config *pconfig = &pipe->configs[0];
627 struct skl_pipe_fmt *fmt = NULL;
631 if (pipe->nr_cfgs == 0) {
632 pipe->cur_config_idx = 0;
636 if (skl_tplg_is_multi_fmt(skl, pipe)) {
637 pipe->cur_config_idx = pipe->pipe_config_idx;
638 pipe->memory_pages = pconfig->mem_pages;
639 dev_dbg(skl->dev, "found pipe config idx:%d\n",
640 pipe->cur_config_idx);
644 if (pipe->conn_type == SKL_PIPE_CONN_TYPE_NONE) {
645 dev_dbg(skl->dev, "No conn_type detected, take 0th config\n");
646 pipe->cur_config_idx = 0;
647 pipe->memory_pages = pconfig->mem_pages;
652 if ((pipe->conn_type == SKL_PIPE_CONN_TYPE_FE &&
653 pipe->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
654 (pipe->conn_type == SKL_PIPE_CONN_TYPE_BE &&
655 pipe->direction == SNDRV_PCM_STREAM_CAPTURE))
658 for (i = 0; i < pipe->nr_cfgs; i++) {
659 pconfig = &pipe->configs[i];
661 fmt = &pconfig->in_fmt;
663 fmt = &pconfig->out_fmt;
665 if (CHECK_HW_PARAMS(params->ch, params->s_freq, params->s_fmt,
666 fmt->channels, fmt->freq, fmt->bps)) {
667 pipe->cur_config_idx = i;
668 pipe->memory_pages = pconfig->mem_pages;
669 dev_dbg(skl->dev, "Using pipe config: %d\n", i);
675 dev_err(skl->dev, "Invalid pipe config: %d %d %d for pipe: %d\n",
676 params->ch, params->s_freq, params->s_fmt, pipe->ppl_id);
681 * Mixer module represents a pipeline. So in the Pre-PMU event of mixer we
682 * need create the pipeline. So we do following:
683 * - Create the pipeline
684 * - Initialize the modules in pipeline
685 * - finally bind all modules together
687 static int skl_tplg_mixer_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
691 struct skl_module_cfg *mconfig = w->priv;
692 struct skl_pipe_module *w_module;
693 struct skl_pipe *s_pipe = mconfig->pipe;
694 struct skl_module_cfg *src_module = NULL, *dst_module, *module;
695 struct skl_module_deferred_bind *modules;
697 ret = skl_tplg_get_pipe_config(skl, mconfig);
702 * Create a list of modules for pipe.
703 * This list contains modules from source to sink
705 ret = skl_create_pipeline(skl, mconfig->pipe);
709 /* Init all pipe modules from source to sink */
710 ret = skl_tplg_init_pipe_modules(skl, s_pipe);
714 /* Bind modules from source to sink */
715 list_for_each_entry(w_module, &s_pipe->w_list, node) {
716 dst_module = w_module->w->priv;
718 if (src_module == NULL) {
719 src_module = dst_module;
723 ret = skl_bind_modules(skl, src_module, dst_module);
727 src_module = dst_module;
731 * When the destination module is initialized, check for these modules
732 * in deferred bind list. If found, bind them.
734 list_for_each_entry(w_module, &s_pipe->w_list, node) {
735 if (list_empty(&skl->bind_list))
738 list_for_each_entry(modules, &skl->bind_list, node) {
739 module = w_module->w->priv;
740 if (modules->dst == module)
741 skl_bind_modules(skl, modules->src,
749 static int skl_fill_sink_instance_id(struct skl_dev *skl, u32 *params,
750 int size, struct skl_module_cfg *mcfg)
754 if (mcfg->m_type == SKL_MODULE_TYPE_KPB) {
755 struct skl_kpb_params *kpb_params =
756 (struct skl_kpb_params *)params;
757 struct skl_mod_inst_map *inst = kpb_params->u.map;
759 for (i = 0; i < kpb_params->num_modules; i++) {
760 pvt_id = skl_get_pvt_instance_id_map(skl, inst->mod_id,
765 inst->inst_id = pvt_id;
773 * Some modules require params to be set after the module is bound to
774 * all pins connected.
776 * The module provider initializes set_param flag for such modules and we
777 * send params after binding
779 static int skl_tplg_set_module_bind_params(struct snd_soc_dapm_widget *w,
780 struct skl_module_cfg *mcfg, struct skl_dev *skl)
783 struct skl_module_cfg *mconfig = w->priv;
784 const struct snd_kcontrol_new *k;
785 struct soc_bytes_ext *sb;
786 struct skl_algo_data *bc;
787 struct skl_specific_cfg *sp_cfg;
791 * check all out/in pins are in bind state.
792 * if so set the module param
794 for (i = 0; i < mcfg->module->max_output_pins; i++) {
795 if (mcfg->m_out_pin[i].pin_state != SKL_PIN_BIND_DONE)
799 for (i = 0; i < mcfg->module->max_input_pins; i++) {
800 if (mcfg->m_in_pin[i].pin_state != SKL_PIN_BIND_DONE)
804 if (mconfig->formats_config.caps_size > 0 &&
805 mconfig->formats_config.set_params == SKL_PARAM_BIND) {
806 sp_cfg = &mconfig->formats_config;
807 ret = skl_set_module_params(skl, sp_cfg->caps,
809 sp_cfg->param_id, mconfig);
814 for (i = 0; i < w->num_kcontrols; i++) {
815 k = &w->kcontrol_news[i];
816 if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
817 sb = (void *) k->private_value;
818 bc = (struct skl_algo_data *)sb->dobj.private;
820 if (bc->set_params == SKL_PARAM_BIND) {
821 params = kmemdup(bc->params, bc->max, GFP_KERNEL);
825 skl_fill_sink_instance_id(skl, params, bc->max,
828 ret = skl_set_module_params(skl, params,
829 bc->max, bc->param_id, mconfig);
841 static int skl_get_module_id(struct skl_dev *skl, guid_t *uuid)
843 struct uuid_module *module;
845 list_for_each_entry(module, &skl->uuid_list, list) {
846 if (guid_equal(uuid, &module->uuid))
853 static int skl_tplg_find_moduleid_from_uuid(struct skl_dev *skl,
854 const struct snd_kcontrol_new *k)
856 struct soc_bytes_ext *sb = (void *) k->private_value;
857 struct skl_algo_data *bc = (struct skl_algo_data *)sb->dobj.private;
858 struct skl_kpb_params *uuid_params, *params;
859 struct hdac_bus *bus = skl_to_bus(skl);
860 int i, size, module_id;
862 if (bc->set_params == SKL_PARAM_BIND && bc->max) {
863 uuid_params = (struct skl_kpb_params *)bc->params;
864 size = struct_size(params, u.map, uuid_params->num_modules);
866 params = devm_kzalloc(bus->dev, size, GFP_KERNEL);
870 params->num_modules = uuid_params->num_modules;
872 for (i = 0; i < uuid_params->num_modules; i++) {
873 module_id = skl_get_module_id(skl,
874 &uuid_params->u.map_uuid[i].mod_uuid);
876 devm_kfree(bus->dev, params);
880 params->u.map[i].mod_id = module_id;
881 params->u.map[i].inst_id =
882 uuid_params->u.map_uuid[i].inst_id;
885 devm_kfree(bus->dev, bc->params);
886 bc->params = (char *)params;
894 * Retrieve the module id from UUID mentioned in the
897 void skl_tplg_add_moduleid_in_bind_params(struct skl_dev *skl,
898 struct snd_soc_dapm_widget *w)
900 struct skl_module_cfg *mconfig = w->priv;
904 * Post bind params are used for only for KPB
905 * to set copier instances to drain the data
908 if (mconfig->m_type != SKL_MODULE_TYPE_KPB)
911 for (i = 0; i < w->num_kcontrols; i++)
912 if ((w->kcontrol_news[i].access &
913 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) &&
914 (skl_tplg_find_moduleid_from_uuid(skl,
915 &w->kcontrol_news[i]) < 0))
917 "%s: invalid kpb post bind params\n",
921 static int skl_tplg_module_add_deferred_bind(struct skl_dev *skl,
922 struct skl_module_cfg *src, struct skl_module_cfg *dst)
924 struct skl_module_deferred_bind *m_list, *modules;
927 /* only supported for module with static pin connection */
928 for (i = 0; i < dst->module->max_input_pins; i++) {
929 struct skl_module_pin *pin = &dst->m_in_pin[i];
934 if ((pin->id.module_id == src->id.module_id) &&
935 (pin->id.instance_id == src->id.instance_id)) {
937 if (!list_empty(&skl->bind_list)) {
938 list_for_each_entry(modules, &skl->bind_list, node) {
939 if (modules->src == src && modules->dst == dst)
944 m_list = kzalloc(sizeof(*m_list), GFP_KERNEL);
951 list_add(&m_list->node, &skl->bind_list);
958 static int skl_tplg_bind_sinks(struct snd_soc_dapm_widget *w,
960 struct snd_soc_dapm_widget *src_w,
961 struct skl_module_cfg *src_mconfig)
963 struct snd_soc_dapm_path *p;
964 struct snd_soc_dapm_widget *sink = NULL, *next_sink = NULL;
965 struct skl_module_cfg *sink_mconfig;
968 snd_soc_dapm_widget_for_each_sink_path(w, p) {
973 "%s: src widget=%s\n", __func__, w->name);
975 "%s: sink widget=%s\n", __func__, p->sink->name);
979 if (!is_skl_dsp_widget_type(p->sink, skl->dev))
980 return skl_tplg_bind_sinks(p->sink, skl, src_w, src_mconfig);
983 * here we will check widgets in sink pipelines, so that
984 * can be any widgets type and we are only interested if
985 * they are ones used for SKL so check that first
987 if ((p->sink->priv != NULL) &&
988 is_skl_dsp_widget_type(p->sink, skl->dev)) {
991 sink_mconfig = sink->priv;
994 * Modules other than PGA leaf can be connected
995 * directly or via switch to a module in another
996 * pipeline. EX: reference path
997 * when the path is enabled, the dst module that needs
998 * to be bound may not be initialized. if the module is
999 * not initialized, add these modules in the deferred
1000 * bind list and when the dst module is initialised,
1001 * bind this module to the dst_module in deferred list.
1003 if (((src_mconfig->m_state == SKL_MODULE_INIT_DONE)
1004 && (sink_mconfig->m_state == SKL_MODULE_UNINIT))) {
1006 ret = skl_tplg_module_add_deferred_bind(skl,
1007 src_mconfig, sink_mconfig);
1015 if (src_mconfig->m_state == SKL_MODULE_UNINIT ||
1016 sink_mconfig->m_state == SKL_MODULE_UNINIT)
1019 /* Bind source to sink, mixin is always source */
1020 ret = skl_bind_modules(skl, src_mconfig, sink_mconfig);
1024 /* set module params after bind */
1025 skl_tplg_set_module_bind_params(src_w,
1027 skl_tplg_set_module_bind_params(sink,
1030 /* Start sinks pipe first */
1031 if (sink_mconfig->pipe->state != SKL_PIPE_STARTED) {
1032 if (sink_mconfig->pipe->conn_type !=
1033 SKL_PIPE_CONN_TYPE_FE)
1034 ret = skl_run_pipe(skl,
1035 sink_mconfig->pipe);
1042 if (!sink && next_sink)
1043 return skl_tplg_bind_sinks(next_sink, skl, src_w, src_mconfig);
1049 * A PGA represents a module in a pipeline. So in the Pre-PMU event of PGA
1050 * we need to do following:
1051 * - Bind to sink pipeline
1052 * Since the sink pipes can be running and we don't get mixer event on
1053 * connect for already running mixer, we need to find the sink pipes
1054 * here and bind to them. This way dynamic connect works.
1055 * - Start sink pipeline, if not running
1056 * - Then run current pipe
1058 static int skl_tplg_pga_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
1059 struct skl_dev *skl)
1061 struct skl_module_cfg *src_mconfig;
1064 src_mconfig = w->priv;
1067 * find which sink it is connected to, bind with the sink,
1068 * if sink is not started, start sink pipe first, then start
1071 ret = skl_tplg_bind_sinks(w, skl, w, src_mconfig);
1075 /* Start source pipe last after starting all sinks */
1076 if (src_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
1077 return skl_run_pipe(skl, src_mconfig->pipe);
1082 static struct snd_soc_dapm_widget *skl_get_src_dsp_widget(
1083 struct snd_soc_dapm_widget *w, struct skl_dev *skl)
1085 struct snd_soc_dapm_path *p;
1086 struct snd_soc_dapm_widget *src_w = NULL;
1088 snd_soc_dapm_widget_for_each_source_path(w, p) {
1093 dev_dbg(skl->dev, "sink widget=%s\n", w->name);
1094 dev_dbg(skl->dev, "src widget=%s\n", p->source->name);
1097 * here we will check widgets in sink pipelines, so that can
1098 * be any widgets type and we are only interested if they are
1099 * ones used for SKL so check that first
1101 if ((p->source->priv != NULL) &&
1102 is_skl_dsp_widget_type(p->source, skl->dev)) {
1108 return skl_get_src_dsp_widget(src_w, skl);
1114 * in the Post-PMU event of mixer we need to do following:
1115 * - Check if this pipe is running
1117 * - bind this pipeline to its source pipeline
1118 * if source pipe is already running, this means it is a dynamic
1119 * connection and we need to bind only to that pipe
1120 * - start this pipeline
1122 static int skl_tplg_mixer_dapm_post_pmu_event(struct snd_soc_dapm_widget *w,
1123 struct skl_dev *skl)
1126 struct snd_soc_dapm_widget *source, *sink;
1127 struct skl_module_cfg *src_mconfig, *sink_mconfig;
1128 int src_pipe_started = 0;
1131 sink_mconfig = sink->priv;
1134 * If source pipe is already started, that means source is driving
1135 * one more sink before this sink got connected, Since source is
1136 * started, bind this sink to source and start this pipe.
1138 source = skl_get_src_dsp_widget(w, skl);
1139 if (source != NULL) {
1140 src_mconfig = source->priv;
1141 sink_mconfig = sink->priv;
1142 src_pipe_started = 1;
1145 * check pipe state, then no need to bind or start the
1148 if (src_mconfig->pipe->state != SKL_PIPE_STARTED)
1149 src_pipe_started = 0;
1152 if (src_pipe_started) {
1153 ret = skl_bind_modules(skl, src_mconfig, sink_mconfig);
1157 /* set module params after bind */
1158 skl_tplg_set_module_bind_params(source, src_mconfig, skl);
1159 skl_tplg_set_module_bind_params(sink, sink_mconfig, skl);
1161 if (sink_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
1162 ret = skl_run_pipe(skl, sink_mconfig->pipe);
1169 * in the Pre-PMD event of mixer we need to do following:
1171 * - find the source connections and remove that from dapm_path_list
1172 * - unbind with source pipelines if still connected
1174 static int skl_tplg_mixer_dapm_pre_pmd_event(struct snd_soc_dapm_widget *w,
1175 struct skl_dev *skl)
1177 struct skl_module_cfg *src_mconfig, *sink_mconfig;
1180 sink_mconfig = w->priv;
1183 ret = skl_stop_pipe(skl, sink_mconfig->pipe);
1187 for (i = 0; i < sink_mconfig->module->max_input_pins; i++) {
1188 if (sink_mconfig->m_in_pin[i].pin_state == SKL_PIN_BIND_DONE) {
1189 src_mconfig = sink_mconfig->m_in_pin[i].tgt_mcfg;
1193 ret = skl_unbind_modules(skl,
1194 src_mconfig, sink_mconfig);
1202 * in the Post-PMD event of mixer we need to do following:
1203 * - Unbind the modules within the pipeline
1204 * - Delete the pipeline (modules are not required to be explicitly
1205 * deleted, pipeline delete is enough here
1207 static int skl_tplg_mixer_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
1208 struct skl_dev *skl)
1210 struct skl_module_cfg *mconfig = w->priv;
1211 struct skl_pipe_module *w_module;
1212 struct skl_module_cfg *src_module = NULL, *dst_module;
1213 struct skl_pipe *s_pipe = mconfig->pipe;
1214 struct skl_module_deferred_bind *modules, *tmp;
1216 if (s_pipe->state == SKL_PIPE_INVALID)
1219 list_for_each_entry(w_module, &s_pipe->w_list, node) {
1220 if (list_empty(&skl->bind_list))
1223 src_module = w_module->w->priv;
1225 list_for_each_entry_safe(modules, tmp, &skl->bind_list, node) {
1227 * When the destination module is deleted, Unbind the
1228 * modules from deferred bind list.
1230 if (modules->dst == src_module) {
1231 skl_unbind_modules(skl, modules->src,
1236 * When the source module is deleted, remove this entry
1237 * from the deferred bind list.
1239 if (modules->src == src_module) {
1240 list_del(&modules->node);
1241 modules->src = NULL;
1242 modules->dst = NULL;
1248 list_for_each_entry(w_module, &s_pipe->w_list, node) {
1249 dst_module = w_module->w->priv;
1251 if (src_module == NULL) {
1252 src_module = dst_module;
1256 skl_unbind_modules(skl, src_module, dst_module);
1257 src_module = dst_module;
1260 skl_delete_pipe(skl, mconfig->pipe);
1262 list_for_each_entry(w_module, &s_pipe->w_list, node) {
1263 src_module = w_module->w->priv;
1264 src_module->m_state = SKL_MODULE_UNINIT;
1267 return skl_tplg_unload_pipe_modules(skl, s_pipe);
1271 * in the Post-PMD event of PGA we need to do following:
1272 * - Stop the pipeline
1273 * - In source pipe is connected, unbind with source pipelines
1275 static int skl_tplg_pga_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
1276 struct skl_dev *skl)
1278 struct skl_module_cfg *src_mconfig, *sink_mconfig;
1281 src_mconfig = w->priv;
1283 /* Stop the pipe since this is a mixin module */
1284 ret = skl_stop_pipe(skl, src_mconfig->pipe);
1288 for (i = 0; i < src_mconfig->module->max_output_pins; i++) {
1289 if (src_mconfig->m_out_pin[i].pin_state == SKL_PIN_BIND_DONE) {
1290 sink_mconfig = src_mconfig->m_out_pin[i].tgt_mcfg;
1294 * This is a connecter and if path is found that means
1295 * unbind between source and sink has not happened yet
1297 ret = skl_unbind_modules(skl, src_mconfig,
1306 * In modelling, we assume there will be ONLY one mixer in a pipeline. If a
1307 * second one is required that is created as another pipe entity.
1308 * The mixer is responsible for pipe management and represent a pipeline
1311 static int skl_tplg_mixer_event(struct snd_soc_dapm_widget *w,
1312 struct snd_kcontrol *k, int event)
1314 struct snd_soc_dapm_context *dapm = w->dapm;
1315 struct skl_dev *skl = get_skl_ctx(dapm->dev);
1318 case SND_SOC_DAPM_PRE_PMU:
1319 return skl_tplg_mixer_dapm_pre_pmu_event(w, skl);
1321 case SND_SOC_DAPM_POST_PMU:
1322 return skl_tplg_mixer_dapm_post_pmu_event(w, skl);
1324 case SND_SOC_DAPM_PRE_PMD:
1325 return skl_tplg_mixer_dapm_pre_pmd_event(w, skl);
1327 case SND_SOC_DAPM_POST_PMD:
1328 return skl_tplg_mixer_dapm_post_pmd_event(w, skl);
1335 * In modelling, we assumed rest of the modules in pipeline are PGA. But we
1336 * are interested in last PGA (leaf PGA) in a pipeline to disconnect with
1337 * the sink when it is running (two FE to one BE or one FE to two BE)
1340 static int skl_tplg_pga_event(struct snd_soc_dapm_widget *w,
1341 struct snd_kcontrol *k, int event)
1344 struct snd_soc_dapm_context *dapm = w->dapm;
1345 struct skl_dev *skl = get_skl_ctx(dapm->dev);
1348 case SND_SOC_DAPM_PRE_PMU:
1349 return skl_tplg_pga_dapm_pre_pmu_event(w, skl);
1351 case SND_SOC_DAPM_POST_PMD:
1352 return skl_tplg_pga_dapm_post_pmd_event(w, skl);
1358 static int skl_tplg_multi_config_set_get(struct snd_kcontrol *kcontrol,
1359 struct snd_ctl_elem_value *ucontrol,
1362 struct snd_soc_component *component =
1363 snd_soc_kcontrol_component(kcontrol);
1364 struct hdac_bus *bus = snd_soc_component_get_drvdata(component);
1365 struct skl_dev *skl = bus_to_skl(bus);
1366 struct skl_pipeline *ppl;
1367 struct skl_pipe *pipe = NULL;
1368 struct soc_enum *ec = (struct soc_enum *)kcontrol->private_value;
1374 if (is_set && ucontrol->value.enumerated.item[0] > ec->items)
1377 pipe_id = ec->dobj.private;
1379 list_for_each_entry(ppl, &skl->ppl_list, node) {
1380 if (ppl->pipe->ppl_id == *pipe_id) {
1389 pipe->pipe_config_idx = ucontrol->value.enumerated.item[0];
1391 ucontrol->value.enumerated.item[0] = pipe->pipe_config_idx;
1396 static int skl_tplg_multi_config_get(struct snd_kcontrol *kcontrol,
1397 struct snd_ctl_elem_value *ucontrol)
1399 return skl_tplg_multi_config_set_get(kcontrol, ucontrol, false);
1402 static int skl_tplg_multi_config_set(struct snd_kcontrol *kcontrol,
1403 struct snd_ctl_elem_value *ucontrol)
1405 return skl_tplg_multi_config_set_get(kcontrol, ucontrol, true);
1408 static int skl_tplg_multi_config_get_dmic(struct snd_kcontrol *kcontrol,
1409 struct snd_ctl_elem_value *ucontrol)
1411 return skl_tplg_multi_config_set_get(kcontrol, ucontrol, false);
1414 static int skl_tplg_multi_config_set_dmic(struct snd_kcontrol *kcontrol,
1415 struct snd_ctl_elem_value *ucontrol)
1417 return skl_tplg_multi_config_set_get(kcontrol, ucontrol, true);
1420 static int skl_tplg_tlv_control_get(struct snd_kcontrol *kcontrol,
1421 unsigned int __user *data, unsigned int size)
1423 struct soc_bytes_ext *sb =
1424 (struct soc_bytes_ext *)kcontrol->private_value;
1425 struct skl_algo_data *bc = (struct skl_algo_data *)sb->dobj.private;
1426 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1427 struct skl_module_cfg *mconfig = w->priv;
1428 struct skl_dev *skl = get_skl_ctx(w->dapm->dev);
1431 skl_get_module_params(skl, (u32 *)bc->params,
1432 bc->size, bc->param_id, mconfig);
1434 /* decrement size for TLV header */
1435 size -= 2 * sizeof(u32);
1437 /* check size as we don't want to send kernel data */
1442 if (copy_to_user(data, &bc->param_id, sizeof(u32)))
1444 if (copy_to_user(data + 1, &size, sizeof(u32)))
1446 if (copy_to_user(data + 2, bc->params, size))
1453 #define SKL_PARAM_VENDOR_ID 0xff
1455 static int skl_tplg_tlv_control_set(struct snd_kcontrol *kcontrol,
1456 const unsigned int __user *data, unsigned int size)
1458 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1459 struct skl_module_cfg *mconfig = w->priv;
1460 struct soc_bytes_ext *sb =
1461 (struct soc_bytes_ext *)kcontrol->private_value;
1462 struct skl_algo_data *ac = (struct skl_algo_data *)sb->dobj.private;
1463 struct skl_dev *skl = get_skl_ctx(w->dapm->dev);
1470 if (copy_from_user(ac->params, data, size))
1474 return skl_set_module_params(skl,
1475 (u32 *)ac->params, ac->size,
1476 ac->param_id, mconfig);
1482 static int skl_tplg_mic_control_get(struct snd_kcontrol *kcontrol,
1483 struct snd_ctl_elem_value *ucontrol)
1485 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1486 struct skl_module_cfg *mconfig = w->priv;
1487 struct soc_enum *ec = (struct soc_enum *)kcontrol->private_value;
1488 u32 ch_type = *((u32 *)ec->dobj.private);
1490 if (mconfig->dmic_ch_type == ch_type)
1491 ucontrol->value.enumerated.item[0] =
1492 mconfig->dmic_ch_combo_index;
1494 ucontrol->value.enumerated.item[0] = 0;
1499 static int skl_fill_mic_sel_params(struct skl_module_cfg *mconfig,
1500 struct skl_mic_sel_config *mic_cfg, struct device *dev)
1502 struct skl_specific_cfg *sp_cfg = &mconfig->formats_config;
1504 sp_cfg->caps_size = sizeof(struct skl_mic_sel_config);
1505 sp_cfg->set_params = SKL_PARAM_SET;
1506 sp_cfg->param_id = 0x00;
1507 if (!sp_cfg->caps) {
1508 sp_cfg->caps = devm_kzalloc(dev, sp_cfg->caps_size, GFP_KERNEL);
1513 mic_cfg->mic_switch = SKL_MIC_SEL_SWITCH;
1515 memcpy(sp_cfg->caps, mic_cfg, sp_cfg->caps_size);
1520 static int skl_tplg_mic_control_set(struct snd_kcontrol *kcontrol,
1521 struct snd_ctl_elem_value *ucontrol)
1523 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1524 struct skl_module_cfg *mconfig = w->priv;
1525 struct skl_mic_sel_config mic_cfg = {0};
1526 struct soc_enum *ec = (struct soc_enum *)kcontrol->private_value;
1527 u32 ch_type = *((u32 *)ec->dobj.private);
1529 u8 in_ch, out_ch, index;
1531 mconfig->dmic_ch_type = ch_type;
1532 mconfig->dmic_ch_combo_index = ucontrol->value.enumerated.item[0];
1534 /* enum control index 0 is INVALID, so no channels to be set */
1535 if (mconfig->dmic_ch_combo_index == 0)
1538 /* No valid channel selection map for index 0, so offset by 1 */
1539 index = mconfig->dmic_ch_combo_index - 1;
1543 if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_mono_list))
1546 list = &mic_mono_list[index];
1550 if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_stereo_list))
1553 list = mic_stereo_list[index];
1557 if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_trio_list))
1560 list = mic_trio_list[index];
1564 if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_quatro_list))
1567 list = mic_quatro_list[index];
1571 dev_err(w->dapm->dev,
1572 "Invalid channel %d for mic_select module\n",
1578 /* channel type enum map to number of chanels for that type */
1579 for (out_ch = 0; out_ch < ch_type; out_ch++) {
1580 in_ch = list[out_ch];
1581 mic_cfg.blob[out_ch][in_ch] = SKL_DEFAULT_MIC_SEL_GAIN;
1584 return skl_fill_mic_sel_params(mconfig, &mic_cfg, w->dapm->dev);
1588 * Fill the dma id for host and link. In case of passthrough
1589 * pipeline, this will both host and link in the same
1590 * pipeline, so need to copy the link and host based on dev_type
1592 static void skl_tplg_fill_dma_id(struct skl_module_cfg *mcfg,
1593 struct skl_pipe_params *params)
1595 struct skl_pipe *pipe = mcfg->pipe;
1597 if (pipe->passthru) {
1598 switch (mcfg->dev_type) {
1599 case SKL_DEVICE_HDALINK:
1600 pipe->p_params->link_dma_id = params->link_dma_id;
1601 pipe->p_params->link_index = params->link_index;
1602 pipe->p_params->link_bps = params->link_bps;
1605 case SKL_DEVICE_HDAHOST:
1606 pipe->p_params->host_dma_id = params->host_dma_id;
1607 pipe->p_params->host_bps = params->host_bps;
1613 pipe->p_params->s_fmt = params->s_fmt;
1614 pipe->p_params->ch = params->ch;
1615 pipe->p_params->s_freq = params->s_freq;
1616 pipe->p_params->stream = params->stream;
1617 pipe->p_params->format = params->format;
1620 memcpy(pipe->p_params, params, sizeof(*params));
1625 * The FE params are passed by hw_params of the DAI.
1626 * On hw_params, the params are stored in Gateway module of the FE and we
1627 * need to calculate the format in DSP module configuration, that
1628 * conversion is done here
1630 int skl_tplg_update_pipe_params(struct device *dev,
1631 struct skl_module_cfg *mconfig,
1632 struct skl_pipe_params *params)
1634 struct skl_module_res *res;
1635 struct skl_dev *skl = get_skl_ctx(dev);
1636 struct skl_module_fmt *format = NULL;
1637 u8 cfg_idx = mconfig->pipe->cur_config_idx;
1639 res = &mconfig->module->resources[mconfig->res_idx];
1640 skl_tplg_fill_dma_id(mconfig, params);
1641 mconfig->fmt_idx = mconfig->mod_cfg[cfg_idx].fmt_idx;
1642 mconfig->res_idx = mconfig->mod_cfg[cfg_idx].res_idx;
1644 if (skl->nr_modules)
1647 if (params->stream == SNDRV_PCM_STREAM_PLAYBACK)
1648 format = &mconfig->module->formats[mconfig->fmt_idx].inputs[0].fmt;
1650 format = &mconfig->module->formats[mconfig->fmt_idx].outputs[0].fmt;
1652 /* set the hw_params */
1653 format->s_freq = params->s_freq;
1654 format->channels = params->ch;
1655 format->valid_bit_depth = skl_get_bit_depth(params->s_fmt);
1658 * 16 bit is 16 bit container whereas 24 bit is in 32 bit
1659 * container so update bit depth accordingly
1661 switch (format->valid_bit_depth) {
1662 case SKL_DEPTH_16BIT:
1663 format->bit_depth = format->valid_bit_depth;
1666 case SKL_DEPTH_24BIT:
1667 case SKL_DEPTH_32BIT:
1668 format->bit_depth = SKL_DEPTH_32BIT;
1672 dev_err(dev, "Invalid bit depth %x for pipe\n",
1673 format->valid_bit_depth);
1677 if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1678 res->ibs = (format->s_freq / 1000) *
1679 (format->channels) *
1680 (format->bit_depth >> 3);
1682 res->obs = (format->s_freq / 1000) *
1683 (format->channels) *
1684 (format->bit_depth >> 3);
1691 * Query the module config for the FE DAI
1692 * This is used to find the hw_params set for that DAI and apply to FE
1695 struct skl_module_cfg *
1696 skl_tplg_fe_get_cpr_module(struct snd_soc_dai *dai, int stream)
1698 struct snd_soc_dapm_widget *w;
1699 struct snd_soc_dapm_path *p = NULL;
1701 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1702 w = dai->playback_widget;
1703 snd_soc_dapm_widget_for_each_sink_path(w, p) {
1704 if (p->connect && p->sink->power &&
1705 !is_skl_dsp_widget_type(p->sink, dai->dev))
1708 if (p->sink->priv) {
1709 dev_dbg(dai->dev, "set params for %s\n",
1711 return p->sink->priv;
1715 w = dai->capture_widget;
1716 snd_soc_dapm_widget_for_each_source_path(w, p) {
1717 if (p->connect && p->source->power &&
1718 !is_skl_dsp_widget_type(p->source, dai->dev))
1721 if (p->source->priv) {
1722 dev_dbg(dai->dev, "set params for %s\n",
1724 return p->source->priv;
1732 static struct skl_module_cfg *skl_get_mconfig_pb_cpr(
1733 struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
1735 struct snd_soc_dapm_path *p;
1736 struct skl_module_cfg *mconfig = NULL;
1738 snd_soc_dapm_widget_for_each_source_path(w, p) {
1739 if (w->endpoints[SND_SOC_DAPM_DIR_OUT] > 0) {
1741 (p->sink->id == snd_soc_dapm_aif_out) &&
1743 mconfig = p->source->priv;
1746 mconfig = skl_get_mconfig_pb_cpr(dai, p->source);
1754 static struct skl_module_cfg *skl_get_mconfig_cap_cpr(
1755 struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
1757 struct snd_soc_dapm_path *p;
1758 struct skl_module_cfg *mconfig = NULL;
1760 snd_soc_dapm_widget_for_each_sink_path(w, p) {
1761 if (w->endpoints[SND_SOC_DAPM_DIR_IN] > 0) {
1763 (p->source->id == snd_soc_dapm_aif_in) &&
1765 mconfig = p->sink->priv;
1768 mconfig = skl_get_mconfig_cap_cpr(dai, p->sink);
1776 struct skl_module_cfg *
1777 skl_tplg_be_get_cpr_module(struct snd_soc_dai *dai, int stream)
1779 struct snd_soc_dapm_widget *w;
1780 struct skl_module_cfg *mconfig;
1782 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1783 w = dai->playback_widget;
1784 mconfig = skl_get_mconfig_pb_cpr(dai, w);
1786 w = dai->capture_widget;
1787 mconfig = skl_get_mconfig_cap_cpr(dai, w);
1792 static u8 skl_tplg_be_link_type(int dev_type)
1798 ret = NHLT_LINK_SSP;
1801 case SKL_DEVICE_DMIC:
1802 ret = NHLT_LINK_DMIC;
1805 case SKL_DEVICE_I2S:
1806 ret = NHLT_LINK_SSP;
1809 case SKL_DEVICE_HDALINK:
1810 ret = NHLT_LINK_HDA;
1814 ret = NHLT_LINK_INVALID;
1822 * Fill the BE gateway parameters
1823 * The BE gateway expects a blob of parameters which are kept in the ACPI
1824 * NHLT blob, so query the blob for interface type (i2s/pdm) and instance.
1825 * The port can have multiple settings so pick based on the PCM
1828 static int skl_tplg_be_fill_pipe_params(struct snd_soc_dai *dai,
1829 struct skl_module_cfg *mconfig,
1830 struct skl_pipe_params *params)
1832 struct nhlt_specific_cfg *cfg;
1833 struct skl_dev *skl = get_skl_ctx(dai->dev);
1834 int link_type = skl_tplg_be_link_type(mconfig->dev_type);
1835 u8 dev_type = skl_tplg_be_dev_type(mconfig->dev_type);
1837 skl_tplg_fill_dma_id(mconfig, params);
1839 if (link_type == NHLT_LINK_HDA)
1842 /* update the blob based on virtual bus_id*/
1843 cfg = skl_get_ep_blob(skl, mconfig->vbus_id, link_type,
1844 params->s_fmt, params->ch,
1845 params->s_freq, params->stream,
1848 mconfig->formats_config.caps_size = cfg->size;
1849 mconfig->formats_config.caps = (u32 *) &cfg->caps;
1851 dev_err(dai->dev, "Blob NULL for id %x type %d dirn %d\n",
1852 mconfig->vbus_id, link_type,
1854 dev_err(dai->dev, "PCM: ch %d, freq %d, fmt %d\n",
1855 params->ch, params->s_freq, params->s_fmt);
1862 static int skl_tplg_be_set_src_pipe_params(struct snd_soc_dai *dai,
1863 struct snd_soc_dapm_widget *w,
1864 struct skl_pipe_params *params)
1866 struct snd_soc_dapm_path *p;
1869 snd_soc_dapm_widget_for_each_source_path(w, p) {
1870 if (p->connect && is_skl_dsp_widget_type(p->source, dai->dev) &&
1873 ret = skl_tplg_be_fill_pipe_params(dai,
1874 p->source->priv, params);
1878 ret = skl_tplg_be_set_src_pipe_params(dai,
1888 static int skl_tplg_be_set_sink_pipe_params(struct snd_soc_dai *dai,
1889 struct snd_soc_dapm_widget *w, struct skl_pipe_params *params)
1891 struct snd_soc_dapm_path *p;
1894 snd_soc_dapm_widget_for_each_sink_path(w, p) {
1895 if (p->connect && is_skl_dsp_widget_type(p->sink, dai->dev) &&
1898 ret = skl_tplg_be_fill_pipe_params(dai,
1899 p->sink->priv, params);
1903 ret = skl_tplg_be_set_sink_pipe_params(
1904 dai, p->sink, params);
1914 * BE hw_params can be a source parameters (capture) or sink parameters
1915 * (playback). Based on sink and source we need to either find the source
1916 * list or the sink list and set the pipeline parameters
1918 int skl_tplg_be_update_params(struct snd_soc_dai *dai,
1919 struct skl_pipe_params *params)
1921 struct snd_soc_dapm_widget *w;
1923 if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1924 w = dai->playback_widget;
1926 return skl_tplg_be_set_src_pipe_params(dai, w, params);
1929 w = dai->capture_widget;
1931 return skl_tplg_be_set_sink_pipe_params(dai, w, params);
1937 static const struct snd_soc_tplg_widget_events skl_tplg_widget_ops[] = {
1938 {SKL_MIXER_EVENT, skl_tplg_mixer_event},
1939 {SKL_VMIXER_EVENT, skl_tplg_mixer_event},
1940 {SKL_PGA_EVENT, skl_tplg_pga_event},
1943 static const struct snd_soc_tplg_bytes_ext_ops skl_tlv_ops[] = {
1944 {SKL_CONTROL_TYPE_BYTE_TLV, skl_tplg_tlv_control_get,
1945 skl_tplg_tlv_control_set},
1948 static const struct snd_soc_tplg_kcontrol_ops skl_tplg_kcontrol_ops[] = {
1950 .id = SKL_CONTROL_TYPE_MIC_SELECT,
1951 .get = skl_tplg_mic_control_get,
1952 .put = skl_tplg_mic_control_set,
1955 .id = SKL_CONTROL_TYPE_MULTI_IO_SELECT,
1956 .get = skl_tplg_multi_config_get,
1957 .put = skl_tplg_multi_config_set,
1960 .id = SKL_CONTROL_TYPE_MULTI_IO_SELECT_DMIC,
1961 .get = skl_tplg_multi_config_get_dmic,
1962 .put = skl_tplg_multi_config_set_dmic,
1966 static int skl_tplg_fill_pipe_cfg(struct device *dev,
1967 struct skl_pipe *pipe, u32 tkn,
1968 u32 tkn_val, int conf_idx, int dir)
1970 struct skl_pipe_fmt *fmt;
1971 struct skl_path_config *config;
1975 fmt = &pipe->configs[conf_idx].in_fmt;
1979 fmt = &pipe->configs[conf_idx].out_fmt;
1983 dev_err(dev, "Invalid direction: %d\n", dir);
1987 config = &pipe->configs[conf_idx];
1990 case SKL_TKN_U32_CFG_FREQ:
1991 fmt->freq = tkn_val;
1994 case SKL_TKN_U8_CFG_CHAN:
1995 fmt->channels = tkn_val;
1998 case SKL_TKN_U8_CFG_BPS:
2002 case SKL_TKN_U32_PATH_MEM_PGS:
2003 config->mem_pages = tkn_val;
2007 dev_err(dev, "Invalid token config: %d\n", tkn);
2014 static int skl_tplg_fill_pipe_tkn(struct device *dev,
2015 struct skl_pipe *pipe, u32 tkn,
2020 case SKL_TKN_U32_PIPE_CONN_TYPE:
2021 pipe->conn_type = tkn_val;
2024 case SKL_TKN_U32_PIPE_PRIORITY:
2025 pipe->pipe_priority = tkn_val;
2028 case SKL_TKN_U32_PIPE_MEM_PGS:
2029 pipe->memory_pages = tkn_val;
2032 case SKL_TKN_U32_PMODE:
2033 pipe->lp_mode = tkn_val;
2036 case SKL_TKN_U32_PIPE_DIRECTION:
2037 pipe->direction = tkn_val;
2040 case SKL_TKN_U32_NUM_CONFIGS:
2041 pipe->nr_cfgs = tkn_val;
2045 dev_err(dev, "Token not handled %d\n", tkn);
2053 * Add pipeline by parsing the relevant tokens
2054 * Return an existing pipe if the pipe already exists.
2056 static int skl_tplg_add_pipe(struct device *dev,
2057 struct skl_module_cfg *mconfig, struct skl_dev *skl,
2058 struct snd_soc_tplg_vendor_value_elem *tkn_elem)
2060 struct skl_pipeline *ppl;
2061 struct skl_pipe *pipe;
2062 struct skl_pipe_params *params;
2064 list_for_each_entry(ppl, &skl->ppl_list, node) {
2065 if (ppl->pipe->ppl_id == tkn_elem->value) {
2066 mconfig->pipe = ppl->pipe;
2071 ppl = devm_kzalloc(dev, sizeof(*ppl), GFP_KERNEL);
2075 pipe = devm_kzalloc(dev, sizeof(*pipe), GFP_KERNEL);
2079 params = devm_kzalloc(dev, sizeof(*params), GFP_KERNEL);
2083 pipe->p_params = params;
2084 pipe->ppl_id = tkn_elem->value;
2085 INIT_LIST_HEAD(&pipe->w_list);
2088 list_add(&ppl->node, &skl->ppl_list);
2090 mconfig->pipe = pipe;
2091 mconfig->pipe->state = SKL_PIPE_INVALID;
2096 static int skl_tplg_get_uuid(struct device *dev, guid_t *guid,
2097 struct snd_soc_tplg_vendor_uuid_elem *uuid_tkn)
2099 if (uuid_tkn->token == SKL_TKN_UUID) {
2100 import_guid(guid, uuid_tkn->uuid);
2104 dev_err(dev, "Not an UUID token %d\n", uuid_tkn->token);
2109 static int skl_tplg_fill_pin(struct device *dev,
2110 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2111 struct skl_module_pin *m_pin,
2116 switch (tkn_elem->token) {
2117 case SKL_TKN_U32_PIN_MOD_ID:
2118 m_pin[pin_index].id.module_id = tkn_elem->value;
2121 case SKL_TKN_U32_PIN_INST_ID:
2122 m_pin[pin_index].id.instance_id = tkn_elem->value;
2126 ret = skl_tplg_get_uuid(dev, &m_pin[pin_index].id.mod_uuid,
2127 (struct snd_soc_tplg_vendor_uuid_elem *)tkn_elem);
2134 dev_err(dev, "%d Not a pin token\n", tkn_elem->token);
2142 * Parse for pin config specific tokens to fill up the
2143 * module private data
2145 static int skl_tplg_fill_pins_info(struct device *dev,
2146 struct skl_module_cfg *mconfig,
2147 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2148 int dir, int pin_count)
2151 struct skl_module_pin *m_pin;
2155 m_pin = mconfig->m_in_pin;
2159 m_pin = mconfig->m_out_pin;
2163 dev_err(dev, "Invalid direction value\n");
2167 ret = skl_tplg_fill_pin(dev, tkn_elem, m_pin, pin_count);
2171 m_pin[pin_count].in_use = false;
2172 m_pin[pin_count].pin_state = SKL_PIN_UNBIND;
2178 * Fill up input/output module config format based
2181 static int skl_tplg_fill_fmt(struct device *dev,
2182 struct skl_module_fmt *dst_fmt,
2186 case SKL_TKN_U32_FMT_CH:
2187 dst_fmt->channels = value;
2190 case SKL_TKN_U32_FMT_FREQ:
2191 dst_fmt->s_freq = value;
2194 case SKL_TKN_U32_FMT_BIT_DEPTH:
2195 dst_fmt->bit_depth = value;
2198 case SKL_TKN_U32_FMT_SAMPLE_SIZE:
2199 dst_fmt->valid_bit_depth = value;
2202 case SKL_TKN_U32_FMT_CH_CONFIG:
2203 dst_fmt->ch_cfg = value;
2206 case SKL_TKN_U32_FMT_INTERLEAVE:
2207 dst_fmt->interleaving_style = value;
2210 case SKL_TKN_U32_FMT_SAMPLE_TYPE:
2211 dst_fmt->sample_type = value;
2214 case SKL_TKN_U32_FMT_CH_MAP:
2215 dst_fmt->ch_map = value;
2219 dev_err(dev, "Invalid token %d\n", tkn);
2226 static int skl_tplg_widget_fill_fmt(struct device *dev,
2227 struct skl_module_iface *fmt,
2228 u32 tkn, u32 val, u32 dir, int fmt_idx)
2230 struct skl_module_fmt *dst_fmt;
2237 dst_fmt = &fmt->inputs[fmt_idx].fmt;
2241 dst_fmt = &fmt->outputs[fmt_idx].fmt;
2245 dev_err(dev, "Invalid direction: %d\n", dir);
2249 return skl_tplg_fill_fmt(dev, dst_fmt, tkn, val);
2252 static void skl_tplg_fill_pin_dynamic_val(
2253 struct skl_module_pin *mpin, u32 pin_count, u32 value)
2257 for (i = 0; i < pin_count; i++)
2258 mpin[i].is_dynamic = value;
2262 * Resource table in the manifest has pin specific resources
2263 * like pin and pin buffer size
2265 static int skl_tplg_manifest_pin_res_tkn(struct device *dev,
2266 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2267 struct skl_module_res *res, int pin_idx, int dir)
2269 struct skl_module_pin_resources *m_pin;
2273 m_pin = &res->input[pin_idx];
2277 m_pin = &res->output[pin_idx];
2281 dev_err(dev, "Invalid pin direction: %d\n", dir);
2285 switch (tkn_elem->token) {
2286 case SKL_TKN_MM_U32_RES_PIN_ID:
2287 m_pin->pin_index = tkn_elem->value;
2290 case SKL_TKN_MM_U32_PIN_BUF:
2291 m_pin->buf_size = tkn_elem->value;
2295 dev_err(dev, "Invalid token: %d\n", tkn_elem->token);
2303 * Fill module specific resources from the manifest's resource
2304 * table like CPS, DMA size, mem_pages.
2306 static int skl_tplg_fill_res_tkn(struct device *dev,
2307 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2308 struct skl_module_res *res,
2309 int pin_idx, int dir)
2311 int ret, tkn_count = 0;
2316 switch (tkn_elem->token) {
2317 case SKL_TKN_MM_U32_DMA_SIZE:
2318 res->dma_buffer_size = tkn_elem->value;
2321 case SKL_TKN_MM_U32_CPC:
2322 res->cpc = tkn_elem->value;
2325 case SKL_TKN_U32_MEM_PAGES:
2326 res->is_pages = tkn_elem->value;
2329 case SKL_TKN_U32_OBS:
2330 res->obs = tkn_elem->value;
2333 case SKL_TKN_U32_IBS:
2334 res->ibs = tkn_elem->value;
2337 case SKL_TKN_MM_U32_RES_PIN_ID:
2338 case SKL_TKN_MM_U32_PIN_BUF:
2339 ret = skl_tplg_manifest_pin_res_tkn(dev, tkn_elem, res,
2345 case SKL_TKN_MM_U32_CPS:
2346 case SKL_TKN_U32_MAX_MCPS:
2347 /* ignore unused tokens */
2351 dev_err(dev, "Not a res type token: %d", tkn_elem->token);
2361 * Parse tokens to fill up the module private data
2363 static int skl_tplg_get_token(struct device *dev,
2364 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2365 struct skl_dev *skl, struct skl_module_cfg *mconfig)
2369 static int is_pipe_exists;
2370 static int pin_index, dir, conf_idx;
2371 struct skl_module_iface *iface = NULL;
2372 struct skl_module_res *res = NULL;
2373 int res_idx = mconfig->res_idx;
2374 int fmt_idx = mconfig->fmt_idx;
2377 * If the manifest structure contains no modules, fill all
2378 * the module data to 0th index.
2379 * res_idx and fmt_idx are default set to 0.
2381 if (skl->nr_modules == 0) {
2382 res = &mconfig->module->resources[res_idx];
2383 iface = &mconfig->module->formats[fmt_idx];
2386 if (tkn_elem->token > SKL_TKN_MAX)
2389 switch (tkn_elem->token) {
2390 case SKL_TKN_U8_IN_QUEUE_COUNT:
2391 mconfig->module->max_input_pins = tkn_elem->value;
2394 case SKL_TKN_U8_OUT_QUEUE_COUNT:
2395 mconfig->module->max_output_pins = tkn_elem->value;
2398 case SKL_TKN_U8_DYN_IN_PIN:
2399 if (!mconfig->m_in_pin)
2401 devm_kcalloc(dev, MAX_IN_QUEUE,
2402 sizeof(*mconfig->m_in_pin),
2404 if (!mconfig->m_in_pin)
2407 skl_tplg_fill_pin_dynamic_val(mconfig->m_in_pin, MAX_IN_QUEUE,
2411 case SKL_TKN_U8_DYN_OUT_PIN:
2412 if (!mconfig->m_out_pin)
2413 mconfig->m_out_pin =
2414 devm_kcalloc(dev, MAX_IN_QUEUE,
2415 sizeof(*mconfig->m_in_pin),
2417 if (!mconfig->m_out_pin)
2420 skl_tplg_fill_pin_dynamic_val(mconfig->m_out_pin, MAX_OUT_QUEUE,
2424 case SKL_TKN_U8_TIME_SLOT:
2425 mconfig->time_slot = tkn_elem->value;
2428 case SKL_TKN_U8_CORE_ID:
2429 mconfig->core_id = tkn_elem->value;
2432 case SKL_TKN_U8_MOD_TYPE:
2433 mconfig->m_type = tkn_elem->value;
2436 case SKL_TKN_U8_DEV_TYPE:
2437 mconfig->dev_type = tkn_elem->value;
2440 case SKL_TKN_U8_HW_CONN_TYPE:
2441 mconfig->hw_conn_type = tkn_elem->value;
2444 case SKL_TKN_U16_MOD_INST_ID:
2445 mconfig->id.instance_id =
2449 case SKL_TKN_U32_MEM_PAGES:
2450 case SKL_TKN_U32_MAX_MCPS:
2451 case SKL_TKN_U32_OBS:
2452 case SKL_TKN_U32_IBS:
2453 ret = skl_tplg_fill_res_tkn(dev, tkn_elem, res, pin_index, dir);
2459 case SKL_TKN_U32_VBUS_ID:
2460 mconfig->vbus_id = tkn_elem->value;
2463 case SKL_TKN_U32_PARAMS_FIXUP:
2464 mconfig->params_fixup = tkn_elem->value;
2467 case SKL_TKN_U32_CONVERTER:
2468 mconfig->converter = tkn_elem->value;
2471 case SKL_TKN_U32_D0I3_CAPS:
2472 mconfig->d0i3_caps = tkn_elem->value;
2475 case SKL_TKN_U32_PIPE_ID:
2476 ret = skl_tplg_add_pipe(dev,
2477 mconfig, skl, tkn_elem);
2480 if (ret == -EEXIST) {
2484 return is_pipe_exists;
2489 case SKL_TKN_U32_PIPE_CONFIG_ID:
2490 conf_idx = tkn_elem->value;
2493 case SKL_TKN_U32_PIPE_CONN_TYPE:
2494 case SKL_TKN_U32_PIPE_PRIORITY:
2495 case SKL_TKN_U32_PIPE_MEM_PGS:
2496 case SKL_TKN_U32_PMODE:
2497 case SKL_TKN_U32_PIPE_DIRECTION:
2498 case SKL_TKN_U32_NUM_CONFIGS:
2499 if (is_pipe_exists) {
2500 ret = skl_tplg_fill_pipe_tkn(dev, mconfig->pipe,
2501 tkn_elem->token, tkn_elem->value);
2508 case SKL_TKN_U32_PATH_MEM_PGS:
2509 case SKL_TKN_U32_CFG_FREQ:
2510 case SKL_TKN_U8_CFG_CHAN:
2511 case SKL_TKN_U8_CFG_BPS:
2512 if (mconfig->pipe->nr_cfgs) {
2513 ret = skl_tplg_fill_pipe_cfg(dev, mconfig->pipe,
2514 tkn_elem->token, tkn_elem->value,
2521 case SKL_TKN_CFG_MOD_RES_ID:
2522 mconfig->mod_cfg[conf_idx].res_idx = tkn_elem->value;
2525 case SKL_TKN_CFG_MOD_FMT_ID:
2526 mconfig->mod_cfg[conf_idx].fmt_idx = tkn_elem->value;
2530 * SKL_TKN_U32_DIR_PIN_COUNT token has the value for both
2531 * direction and the pin count. The first four bits represent
2532 * direction and next four the pin count.
2534 case SKL_TKN_U32_DIR_PIN_COUNT:
2535 dir = tkn_elem->value & SKL_IN_DIR_BIT_MASK;
2536 pin_index = (tkn_elem->value &
2537 SKL_PIN_COUNT_MASK) >> 4;
2541 case SKL_TKN_U32_FMT_CH:
2542 case SKL_TKN_U32_FMT_FREQ:
2543 case SKL_TKN_U32_FMT_BIT_DEPTH:
2544 case SKL_TKN_U32_FMT_SAMPLE_SIZE:
2545 case SKL_TKN_U32_FMT_CH_CONFIG:
2546 case SKL_TKN_U32_FMT_INTERLEAVE:
2547 case SKL_TKN_U32_FMT_SAMPLE_TYPE:
2548 case SKL_TKN_U32_FMT_CH_MAP:
2549 ret = skl_tplg_widget_fill_fmt(dev, iface, tkn_elem->token,
2550 tkn_elem->value, dir, pin_index);
2557 case SKL_TKN_U32_PIN_MOD_ID:
2558 case SKL_TKN_U32_PIN_INST_ID:
2560 ret = skl_tplg_fill_pins_info(dev,
2561 mconfig, tkn_elem, dir,
2568 case SKL_TKN_U32_CAPS_SIZE:
2569 mconfig->formats_config.caps_size =
2574 case SKL_TKN_U32_CAPS_SET_PARAMS:
2575 mconfig->formats_config.set_params =
2579 case SKL_TKN_U32_CAPS_PARAMS_ID:
2580 mconfig->formats_config.param_id =
2584 case SKL_TKN_U32_PROC_DOMAIN:
2590 case SKL_TKN_U32_DMA_BUF_SIZE:
2591 mconfig->dma_buffer_size = tkn_elem->value;
2594 case SKL_TKN_U8_IN_PIN_TYPE:
2595 case SKL_TKN_U8_OUT_PIN_TYPE:
2596 case SKL_TKN_U8_CONN_TYPE:
2600 dev_err(dev, "Token %d not handled\n",
2611 * Parse the vendor array for specific tokens to construct
2612 * module private data
2614 static int skl_tplg_get_tokens(struct device *dev,
2615 char *pvt_data, struct skl_dev *skl,
2616 struct skl_module_cfg *mconfig, int block_size)
2618 struct snd_soc_tplg_vendor_array *array;
2619 struct snd_soc_tplg_vendor_value_elem *tkn_elem;
2620 int tkn_count = 0, ret;
2621 int off = 0, tuple_size = 0;
2622 bool is_module_guid = true;
2624 if (block_size <= 0)
2627 while (tuple_size < block_size) {
2628 array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off);
2632 switch (array->type) {
2633 case SND_SOC_TPLG_TUPLE_TYPE_STRING:
2634 dev_warn(dev, "no string tokens expected for skl tplg\n");
2637 case SND_SOC_TPLG_TUPLE_TYPE_UUID:
2638 if (is_module_guid) {
2639 ret = skl_tplg_get_uuid(dev, (guid_t *)mconfig->guid,
2641 is_module_guid = false;
2643 ret = skl_tplg_get_token(dev, array->value, skl,
2650 tuple_size += sizeof(*array->uuid);
2655 tkn_elem = array->value;
2660 while (tkn_count <= (array->num_elems - 1)) {
2661 ret = skl_tplg_get_token(dev, tkn_elem,
2667 tkn_count = tkn_count + ret;
2671 tuple_size += tkn_count * sizeof(*tkn_elem);
2678 * Every data block is preceded by a descriptor to read the number
2679 * of data blocks, they type of the block and it's size
2681 static int skl_tplg_get_desc_blocks(struct device *dev,
2682 struct snd_soc_tplg_vendor_array *array)
2684 struct snd_soc_tplg_vendor_value_elem *tkn_elem;
2686 tkn_elem = array->value;
2688 switch (tkn_elem->token) {
2689 case SKL_TKN_U8_NUM_BLOCKS:
2690 case SKL_TKN_U8_BLOCK_TYPE:
2691 case SKL_TKN_U16_BLOCK_SIZE:
2692 return tkn_elem->value;
2695 dev_err(dev, "Invalid descriptor token %d\n", tkn_elem->token);
2702 /* Functions to parse private data from configuration file format v4 */
2705 * Add pipeline from topology binary into driver pipeline list
2707 * If already added we return that instance
2708 * Otherwise we create a new instance and add into driver list
2710 static int skl_tplg_add_pipe_v4(struct device *dev,
2711 struct skl_module_cfg *mconfig, struct skl_dev *skl,
2712 struct skl_dfw_v4_pipe *dfw_pipe)
2714 struct skl_pipeline *ppl;
2715 struct skl_pipe *pipe;
2716 struct skl_pipe_params *params;
2718 list_for_each_entry(ppl, &skl->ppl_list, node) {
2719 if (ppl->pipe->ppl_id == dfw_pipe->pipe_id) {
2720 mconfig->pipe = ppl->pipe;
2725 ppl = devm_kzalloc(dev, sizeof(*ppl), GFP_KERNEL);
2729 pipe = devm_kzalloc(dev, sizeof(*pipe), GFP_KERNEL);
2733 params = devm_kzalloc(dev, sizeof(*params), GFP_KERNEL);
2737 pipe->ppl_id = dfw_pipe->pipe_id;
2738 pipe->memory_pages = dfw_pipe->memory_pages;
2739 pipe->pipe_priority = dfw_pipe->pipe_priority;
2740 pipe->conn_type = dfw_pipe->conn_type;
2741 pipe->state = SKL_PIPE_INVALID;
2742 pipe->p_params = params;
2743 INIT_LIST_HEAD(&pipe->w_list);
2746 list_add(&ppl->node, &skl->ppl_list);
2748 mconfig->pipe = pipe;
2753 static void skl_fill_module_pin_info_v4(struct skl_dfw_v4_module_pin *dfw_pin,
2754 struct skl_module_pin *m_pin,
2755 bool is_dynamic, int max_pin)
2759 for (i = 0; i < max_pin; i++) {
2760 m_pin[i].id.module_id = dfw_pin[i].module_id;
2761 m_pin[i].id.instance_id = dfw_pin[i].instance_id;
2762 m_pin[i].in_use = false;
2763 m_pin[i].is_dynamic = is_dynamic;
2764 m_pin[i].pin_state = SKL_PIN_UNBIND;
2768 static void skl_tplg_fill_fmt_v4(struct skl_module_pin_fmt *dst_fmt,
2769 struct skl_dfw_v4_module_fmt *src_fmt,
2774 for (i = 0; i < pins; i++) {
2775 dst_fmt[i].fmt.channels = src_fmt[i].channels;
2776 dst_fmt[i].fmt.s_freq = src_fmt[i].freq;
2777 dst_fmt[i].fmt.bit_depth = src_fmt[i].bit_depth;
2778 dst_fmt[i].fmt.valid_bit_depth = src_fmt[i].valid_bit_depth;
2779 dst_fmt[i].fmt.ch_cfg = src_fmt[i].ch_cfg;
2780 dst_fmt[i].fmt.ch_map = src_fmt[i].ch_map;
2781 dst_fmt[i].fmt.interleaving_style =
2782 src_fmt[i].interleaving_style;
2783 dst_fmt[i].fmt.sample_type = src_fmt[i].sample_type;
2787 static int skl_tplg_get_pvt_data_v4(struct snd_soc_tplg_dapm_widget *tplg_w,
2788 struct skl_dev *skl, struct device *dev,
2789 struct skl_module_cfg *mconfig)
2791 struct skl_dfw_v4_module *dfw =
2792 (struct skl_dfw_v4_module *)tplg_w->priv.data;
2795 dev_dbg(dev, "Parsing Skylake v4 widget topology data\n");
2797 ret = guid_parse(dfw->uuid, (guid_t *)mconfig->guid);
2800 mconfig->id.module_id = -1;
2801 mconfig->id.instance_id = dfw->instance_id;
2802 mconfig->module->resources[0].cpc = dfw->max_mcps / 1000;
2803 mconfig->module->resources[0].ibs = dfw->ibs;
2804 mconfig->module->resources[0].obs = dfw->obs;
2805 mconfig->core_id = dfw->core_id;
2806 mconfig->module->max_input_pins = dfw->max_in_queue;
2807 mconfig->module->max_output_pins = dfw->max_out_queue;
2808 mconfig->module->loadable = dfw->is_loadable;
2809 skl_tplg_fill_fmt_v4(mconfig->module->formats[0].inputs, dfw->in_fmt,
2811 skl_tplg_fill_fmt_v4(mconfig->module->formats[0].outputs, dfw->out_fmt,
2814 mconfig->params_fixup = dfw->params_fixup;
2815 mconfig->converter = dfw->converter;
2816 mconfig->m_type = dfw->module_type;
2817 mconfig->vbus_id = dfw->vbus_id;
2818 mconfig->module->resources[0].is_pages = dfw->mem_pages;
2820 ret = skl_tplg_add_pipe_v4(dev, mconfig, skl, &dfw->pipe);
2824 mconfig->dev_type = dfw->dev_type;
2825 mconfig->hw_conn_type = dfw->hw_conn_type;
2826 mconfig->time_slot = dfw->time_slot;
2827 mconfig->formats_config.caps_size = dfw->caps.caps_size;
2829 mconfig->m_in_pin = devm_kcalloc(dev,
2830 MAX_IN_QUEUE, sizeof(*mconfig->m_in_pin),
2832 if (!mconfig->m_in_pin)
2835 mconfig->m_out_pin = devm_kcalloc(dev,
2836 MAX_OUT_QUEUE, sizeof(*mconfig->m_out_pin),
2838 if (!mconfig->m_out_pin)
2841 skl_fill_module_pin_info_v4(dfw->in_pin, mconfig->m_in_pin,
2842 dfw->is_dynamic_in_pin,
2843 mconfig->module->max_input_pins);
2844 skl_fill_module_pin_info_v4(dfw->out_pin, mconfig->m_out_pin,
2845 dfw->is_dynamic_out_pin,
2846 mconfig->module->max_output_pins);
2848 if (mconfig->formats_config.caps_size) {
2849 mconfig->formats_config.set_params = dfw->caps.set_params;
2850 mconfig->formats_config.param_id = dfw->caps.param_id;
2851 mconfig->formats_config.caps =
2852 devm_kzalloc(dev, mconfig->formats_config.caps_size,
2854 if (!mconfig->formats_config.caps)
2856 memcpy(mconfig->formats_config.caps, dfw->caps.caps,
2857 dfw->caps.caps_size);
2864 * Parse the private data for the token and corresponding value.
2865 * The private data can have multiple data blocks. So, a data block
2866 * is preceded by a descriptor for number of blocks and a descriptor
2867 * for the type and size of the suceeding data block.
2869 static int skl_tplg_get_pvt_data(struct snd_soc_tplg_dapm_widget *tplg_w,
2870 struct skl_dev *skl, struct device *dev,
2871 struct skl_module_cfg *mconfig)
2873 struct snd_soc_tplg_vendor_array *array;
2874 int num_blocks, block_size, block_type, off = 0;
2879 * v4 configuration files have a valid UUID at the start of
2880 * the widget's private data.
2882 if (uuid_is_valid((char *)tplg_w->priv.data))
2883 return skl_tplg_get_pvt_data_v4(tplg_w, skl, dev, mconfig);
2885 /* Read the NUM_DATA_BLOCKS descriptor */
2886 array = (struct snd_soc_tplg_vendor_array *)tplg_w->priv.data;
2887 ret = skl_tplg_get_desc_blocks(dev, array);
2893 /* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */
2894 while (num_blocks > 0) {
2895 array = (struct snd_soc_tplg_vendor_array *)
2896 (tplg_w->priv.data + off);
2898 ret = skl_tplg_get_desc_blocks(dev, array);
2905 array = (struct snd_soc_tplg_vendor_array *)
2906 (tplg_w->priv.data + off);
2908 ret = skl_tplg_get_desc_blocks(dev, array);
2915 array = (struct snd_soc_tplg_vendor_array *)
2916 (tplg_w->priv.data + off);
2918 data = (tplg_w->priv.data + off);
2920 if (block_type == SKL_TYPE_TUPLE) {
2921 ret = skl_tplg_get_tokens(dev, data,
2922 skl, mconfig, block_size);
2929 if (mconfig->formats_config.caps_size > 0)
2930 memcpy(mconfig->formats_config.caps, data,
2931 mconfig->formats_config.caps_size);
2933 ret = mconfig->formats_config.caps_size;
2941 static void skl_clear_pin_config(struct snd_soc_component *component,
2942 struct snd_soc_dapm_widget *w)
2945 struct skl_module_cfg *mconfig;
2946 struct skl_pipe *pipe;
2948 if (!strncmp(w->dapm->component->name, component->name,
2949 strlen(component->name))) {
2951 pipe = mconfig->pipe;
2952 for (i = 0; i < mconfig->module->max_input_pins; i++) {
2953 mconfig->m_in_pin[i].in_use = false;
2954 mconfig->m_in_pin[i].pin_state = SKL_PIN_UNBIND;
2956 for (i = 0; i < mconfig->module->max_output_pins; i++) {
2957 mconfig->m_out_pin[i].in_use = false;
2958 mconfig->m_out_pin[i].pin_state = SKL_PIN_UNBIND;
2960 pipe->state = SKL_PIPE_INVALID;
2961 mconfig->m_state = SKL_MODULE_UNINIT;
2965 void skl_cleanup_resources(struct skl_dev *skl)
2967 struct snd_soc_component *soc_component = skl->component;
2968 struct snd_soc_dapm_widget *w;
2969 struct snd_soc_card *card;
2971 if (soc_component == NULL)
2974 card = soc_component->card;
2975 if (!card || !card->instantiated)
2978 list_for_each_entry(w, &card->widgets, list) {
2979 if (is_skl_dsp_widget_type(w, skl->dev) && w->priv != NULL)
2980 skl_clear_pin_config(soc_component, w);
2983 skl_clear_module_cnt(skl->dsp);
2987 * Topology core widget load callback
2989 * This is used to save the private data for each widget which gives
2990 * information to the driver about module and pipeline parameters which DSP
2991 * FW expects like ids, resource values, formats etc
2993 static int skl_tplg_widget_load(struct snd_soc_component *cmpnt, int index,
2994 struct snd_soc_dapm_widget *w,
2995 struct snd_soc_tplg_dapm_widget *tplg_w)
2998 struct hdac_bus *bus = snd_soc_component_get_drvdata(cmpnt);
2999 struct skl_dev *skl = bus_to_skl(bus);
3000 struct skl_module_cfg *mconfig;
3002 if (!tplg_w->priv.size)
3005 mconfig = devm_kzalloc(bus->dev, sizeof(*mconfig), GFP_KERNEL);
3010 if (skl->nr_modules == 0) {
3011 mconfig->module = devm_kzalloc(bus->dev,
3012 sizeof(*mconfig->module), GFP_KERNEL);
3013 if (!mconfig->module)
3020 * module binary can be loaded later, so set it to query when
3021 * module is load for a use case
3023 mconfig->id.module_id = -1;
3025 /* Parse private data for tuples */
3026 ret = skl_tplg_get_pvt_data(tplg_w, skl, bus->dev, mconfig);
3030 skl_debug_init_module(skl->debugfs, w, mconfig);
3033 if (tplg_w->event_type == 0) {
3034 dev_dbg(bus->dev, "ASoC: No event handler required\n");
3038 ret = snd_soc_tplg_widget_bind_event(w, skl_tplg_widget_ops,
3039 ARRAY_SIZE(skl_tplg_widget_ops),
3040 tplg_w->event_type);
3043 dev_err(bus->dev, "%s: No matching event handlers found for %d\n",
3044 __func__, tplg_w->event_type);
3051 static int skl_init_algo_data(struct device *dev, struct soc_bytes_ext *be,
3052 struct snd_soc_tplg_bytes_control *bc)
3054 struct skl_algo_data *ac;
3055 struct skl_dfw_algo_data *dfw_ac =
3056 (struct skl_dfw_algo_data *)bc->priv.data;
3058 ac = devm_kzalloc(dev, sizeof(*ac), GFP_KERNEL);
3062 /* Fill private data */
3063 ac->max = dfw_ac->max;
3064 ac->param_id = dfw_ac->param_id;
3065 ac->set_params = dfw_ac->set_params;
3066 ac->size = dfw_ac->max;
3069 ac->params = devm_kzalloc(dev, ac->max, GFP_KERNEL);
3073 memcpy(ac->params, dfw_ac->params, ac->max);
3076 be->dobj.private = ac;
3080 static int skl_init_enum_data(struct device *dev, struct soc_enum *se,
3081 struct snd_soc_tplg_enum_control *ec)
3086 if (ec->priv.size) {
3087 data = devm_kzalloc(dev, sizeof(ec->priv.size), GFP_KERNEL);
3090 memcpy(data, ec->priv.data, ec->priv.size);
3091 se->dobj.private = data;
3098 static int skl_tplg_control_load(struct snd_soc_component *cmpnt,
3100 struct snd_kcontrol_new *kctl,
3101 struct snd_soc_tplg_ctl_hdr *hdr)
3103 struct soc_bytes_ext *sb;
3104 struct snd_soc_tplg_bytes_control *tplg_bc;
3105 struct snd_soc_tplg_enum_control *tplg_ec;
3106 struct hdac_bus *bus = snd_soc_component_get_drvdata(cmpnt);
3107 struct soc_enum *se;
3109 switch (hdr->ops.info) {
3110 case SND_SOC_TPLG_CTL_BYTES:
3111 tplg_bc = container_of(hdr,
3112 struct snd_soc_tplg_bytes_control, hdr);
3113 if (kctl->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
3114 sb = (struct soc_bytes_ext *)kctl->private_value;
3115 if (tplg_bc->priv.size)
3116 return skl_init_algo_data(
3117 bus->dev, sb, tplg_bc);
3121 case SND_SOC_TPLG_CTL_ENUM:
3122 tplg_ec = container_of(hdr,
3123 struct snd_soc_tplg_enum_control, hdr);
3124 if (kctl->access & SNDRV_CTL_ELEM_ACCESS_READ) {
3125 se = (struct soc_enum *)kctl->private_value;
3126 if (tplg_ec->priv.size)
3127 skl_init_enum_data(bus->dev, se, tplg_ec);
3131 * now that the control initializations are done, remove
3132 * write permission for the DMIC configuration enums to
3133 * avoid conflicts between NHLT settings and user interaction
3136 if (hdr->ops.get == SKL_CONTROL_TYPE_MULTI_IO_SELECT_DMIC)
3137 kctl->access = SNDRV_CTL_ELEM_ACCESS_READ;
3142 dev_dbg(bus->dev, "Control load not supported %d:%d:%d\n",
3143 hdr->ops.get, hdr->ops.put, hdr->ops.info);
3150 static int skl_tplg_fill_str_mfest_tkn(struct device *dev,
3151 struct snd_soc_tplg_vendor_string_elem *str_elem,
3152 struct skl_dev *skl)
3155 static int ref_count;
3157 switch (str_elem->token) {
3158 case SKL_TKN_STR_LIB_NAME:
3159 if (ref_count > skl->lib_count - 1) {
3164 strncpy(skl->lib_info[ref_count].name,
3166 ARRAY_SIZE(skl->lib_info[ref_count].name));
3171 dev_err(dev, "Not a string token %d\n", str_elem->token);
3179 static int skl_tplg_get_str_tkn(struct device *dev,
3180 struct snd_soc_tplg_vendor_array *array,
3181 struct skl_dev *skl)
3183 int tkn_count = 0, ret;
3184 struct snd_soc_tplg_vendor_string_elem *str_elem;
3186 str_elem = (struct snd_soc_tplg_vendor_string_elem *)array->value;
3187 while (tkn_count < array->num_elems) {
3188 ret = skl_tplg_fill_str_mfest_tkn(dev, str_elem, skl);
3194 tkn_count = tkn_count + ret;
3200 static int skl_tplg_manifest_fill_fmt(struct device *dev,
3201 struct skl_module_iface *fmt,
3202 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
3203 u32 dir, int fmt_idx)
3205 struct skl_module_pin_fmt *dst_fmt;
3206 struct skl_module_fmt *mod_fmt;
3214 dst_fmt = &fmt->inputs[fmt_idx];
3218 dst_fmt = &fmt->outputs[fmt_idx];
3222 dev_err(dev, "Invalid direction: %d\n", dir);
3226 mod_fmt = &dst_fmt->fmt;
3228 switch (tkn_elem->token) {
3229 case SKL_TKN_MM_U32_INTF_PIN_ID:
3230 dst_fmt->id = tkn_elem->value;
3234 ret = skl_tplg_fill_fmt(dev, mod_fmt, tkn_elem->token,
3244 static int skl_tplg_fill_mod_info(struct device *dev,
3245 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
3246 struct skl_module *mod)
3252 switch (tkn_elem->token) {
3253 case SKL_TKN_U8_IN_PIN_TYPE:
3254 mod->input_pin_type = tkn_elem->value;
3257 case SKL_TKN_U8_OUT_PIN_TYPE:
3258 mod->output_pin_type = tkn_elem->value;
3261 case SKL_TKN_U8_IN_QUEUE_COUNT:
3262 mod->max_input_pins = tkn_elem->value;
3265 case SKL_TKN_U8_OUT_QUEUE_COUNT:
3266 mod->max_output_pins = tkn_elem->value;
3269 case SKL_TKN_MM_U8_NUM_RES:
3270 mod->nr_resources = tkn_elem->value;
3273 case SKL_TKN_MM_U8_NUM_INTF:
3274 mod->nr_interfaces = tkn_elem->value;
3278 dev_err(dev, "Invalid mod info token %d", tkn_elem->token);
3286 static int skl_tplg_get_int_tkn(struct device *dev,
3287 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
3288 struct skl_dev *skl)
3290 int tkn_count = 0, ret;
3291 static int mod_idx, res_val_idx, intf_val_idx, dir, pin_idx;
3292 struct skl_module_res *res = NULL;
3293 struct skl_module_iface *fmt = NULL;
3294 struct skl_module *mod = NULL;
3295 static struct skl_astate_param *astate_table;
3296 static int astate_cfg_idx, count;
3301 mod = skl->modules[mod_idx];
3302 res = &mod->resources[res_val_idx];
3303 fmt = &mod->formats[intf_val_idx];
3306 switch (tkn_elem->token) {
3307 case SKL_TKN_U32_LIB_COUNT:
3308 skl->lib_count = tkn_elem->value;
3311 case SKL_TKN_U8_NUM_MOD:
3312 skl->nr_modules = tkn_elem->value;
3313 skl->modules = devm_kcalloc(dev, skl->nr_modules,
3314 sizeof(*skl->modules), GFP_KERNEL);
3318 for (i = 0; i < skl->nr_modules; i++) {
3319 skl->modules[i] = devm_kzalloc(dev,
3320 sizeof(struct skl_module), GFP_KERNEL);
3321 if (!skl->modules[i])
3326 case SKL_TKN_MM_U8_MOD_IDX:
3327 mod_idx = tkn_elem->value;
3330 case SKL_TKN_U32_ASTATE_COUNT:
3331 if (astate_table != NULL) {
3332 dev_err(dev, "More than one entry for A-State count");
3336 if (tkn_elem->value > SKL_MAX_ASTATE_CFG) {
3337 dev_err(dev, "Invalid A-State count %d\n",
3342 size = struct_size(skl->cfg.astate_cfg, astate_table,
3344 skl->cfg.astate_cfg = devm_kzalloc(dev, size, GFP_KERNEL);
3345 if (!skl->cfg.astate_cfg)
3348 astate_table = skl->cfg.astate_cfg->astate_table;
3349 count = skl->cfg.astate_cfg->count = tkn_elem->value;
3352 case SKL_TKN_U32_ASTATE_IDX:
3353 if (tkn_elem->value >= count) {
3354 dev_err(dev, "Invalid A-State index %d\n",
3359 astate_cfg_idx = tkn_elem->value;
3362 case SKL_TKN_U32_ASTATE_KCPS:
3363 astate_table[astate_cfg_idx].kcps = tkn_elem->value;
3366 case SKL_TKN_U32_ASTATE_CLK_SRC:
3367 astate_table[astate_cfg_idx].clk_src = tkn_elem->value;
3370 case SKL_TKN_U8_IN_PIN_TYPE:
3371 case SKL_TKN_U8_OUT_PIN_TYPE:
3372 case SKL_TKN_U8_IN_QUEUE_COUNT:
3373 case SKL_TKN_U8_OUT_QUEUE_COUNT:
3374 case SKL_TKN_MM_U8_NUM_RES:
3375 case SKL_TKN_MM_U8_NUM_INTF:
3376 ret = skl_tplg_fill_mod_info(dev, tkn_elem, mod);
3381 case SKL_TKN_U32_DIR_PIN_COUNT:
3382 dir = tkn_elem->value & SKL_IN_DIR_BIT_MASK;
3383 pin_idx = (tkn_elem->value & SKL_PIN_COUNT_MASK) >> 4;
3386 case SKL_TKN_MM_U32_RES_ID:
3390 res->id = tkn_elem->value;
3391 res_val_idx = tkn_elem->value;
3394 case SKL_TKN_MM_U32_FMT_ID:
3398 fmt->fmt_idx = tkn_elem->value;
3399 intf_val_idx = tkn_elem->value;
3402 case SKL_TKN_MM_U32_CPS:
3403 case SKL_TKN_MM_U32_DMA_SIZE:
3404 case SKL_TKN_MM_U32_CPC:
3405 case SKL_TKN_U32_MEM_PAGES:
3406 case SKL_TKN_U32_OBS:
3407 case SKL_TKN_U32_IBS:
3408 case SKL_TKN_MM_U32_RES_PIN_ID:
3409 case SKL_TKN_MM_U32_PIN_BUF:
3410 ret = skl_tplg_fill_res_tkn(dev, tkn_elem, res, pin_idx, dir);
3416 case SKL_TKN_MM_U32_NUM_IN_FMT:
3420 res->nr_input_pins = tkn_elem->value;
3423 case SKL_TKN_MM_U32_NUM_OUT_FMT:
3427 res->nr_output_pins = tkn_elem->value;
3430 case SKL_TKN_U32_FMT_CH:
3431 case SKL_TKN_U32_FMT_FREQ:
3432 case SKL_TKN_U32_FMT_BIT_DEPTH:
3433 case SKL_TKN_U32_FMT_SAMPLE_SIZE:
3434 case SKL_TKN_U32_FMT_CH_CONFIG:
3435 case SKL_TKN_U32_FMT_INTERLEAVE:
3436 case SKL_TKN_U32_FMT_SAMPLE_TYPE:
3437 case SKL_TKN_U32_FMT_CH_MAP:
3438 case SKL_TKN_MM_U32_INTF_PIN_ID:
3439 ret = skl_tplg_manifest_fill_fmt(dev, fmt, tkn_elem,
3446 dev_err(dev, "Not a manifest token %d\n", tkn_elem->token);
3455 * Fill the manifest structure by parsing the tokens based on the
3458 static int skl_tplg_get_manifest_tkn(struct device *dev,
3459 char *pvt_data, struct skl_dev *skl,
3462 int tkn_count = 0, ret;
3463 int off = 0, tuple_size = 0;
3465 struct snd_soc_tplg_vendor_array *array;
3466 struct snd_soc_tplg_vendor_value_elem *tkn_elem;
3468 if (block_size <= 0)
3471 while (tuple_size < block_size) {
3472 array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off);
3474 switch (array->type) {
3475 case SND_SOC_TPLG_TUPLE_TYPE_STRING:
3476 ret = skl_tplg_get_str_tkn(dev, array, skl);
3482 tuple_size += tkn_count *
3483 sizeof(struct snd_soc_tplg_vendor_string_elem);
3486 case SND_SOC_TPLG_TUPLE_TYPE_UUID:
3487 if (array->uuid->token != SKL_TKN_UUID) {
3488 dev_err(dev, "Not an UUID token: %d\n",
3489 array->uuid->token);
3492 if (uuid_index >= skl->nr_modules) {
3493 dev_err(dev, "Too many UUID tokens\n");
3496 import_guid(&skl->modules[uuid_index++]->uuid,
3499 tuple_size += sizeof(*array->uuid);
3503 tkn_elem = array->value;
3508 while (tkn_count <= array->num_elems - 1) {
3509 ret = skl_tplg_get_int_tkn(dev,
3514 tkn_count = tkn_count + ret;
3517 tuple_size += (tkn_count * sizeof(*tkn_elem));
3525 * Parse manifest private data for tokens. The private data block is
3526 * preceded by descriptors for type and size of data block.
3528 static int skl_tplg_get_manifest_data(struct snd_soc_tplg_manifest *manifest,
3529 struct device *dev, struct skl_dev *skl)
3531 struct snd_soc_tplg_vendor_array *array;
3532 int num_blocks, block_size = 0, block_type, off = 0;
3536 /* Read the NUM_DATA_BLOCKS descriptor */
3537 array = (struct snd_soc_tplg_vendor_array *)manifest->priv.data;
3538 ret = skl_tplg_get_desc_blocks(dev, array);
3544 /* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */
3545 while (num_blocks > 0) {
3546 array = (struct snd_soc_tplg_vendor_array *)
3547 (manifest->priv.data + off);
3548 ret = skl_tplg_get_desc_blocks(dev, array);
3555 array = (struct snd_soc_tplg_vendor_array *)
3556 (manifest->priv.data + off);
3558 ret = skl_tplg_get_desc_blocks(dev, array);
3565 array = (struct snd_soc_tplg_vendor_array *)
3566 (manifest->priv.data + off);
3568 data = (manifest->priv.data + off);
3570 if (block_type == SKL_TYPE_TUPLE) {
3571 ret = skl_tplg_get_manifest_tkn(dev, data, skl,
3587 static int skl_manifest_load(struct snd_soc_component *cmpnt, int index,
3588 struct snd_soc_tplg_manifest *manifest)
3590 struct hdac_bus *bus = snd_soc_component_get_drvdata(cmpnt);
3591 struct skl_dev *skl = bus_to_skl(bus);
3593 /* proceed only if we have private data defined */
3594 if (manifest->priv.size == 0)
3597 skl_tplg_get_manifest_data(manifest, bus->dev, skl);
3599 if (skl->lib_count > SKL_MAX_LIB) {
3600 dev_err(bus->dev, "Exceeding max Library count. Got:%d\n",
3608 static void skl_tplg_complete(struct snd_soc_component *component)
3610 struct snd_soc_dobj *dobj;
3611 struct snd_soc_acpi_mach *mach =
3612 dev_get_platdata(component->card->dev);
3615 list_for_each_entry(dobj, &component->dobj_list, list) {
3616 struct snd_kcontrol *kcontrol = dobj->control.kcontrol;
3617 struct soc_enum *se;
3621 if (dobj->type != SND_SOC_DOBJ_ENUM || !kcontrol ||
3622 kcontrol->put != skl_tplg_multi_config_set_dmic)
3625 se = (struct soc_enum *)kcontrol->private_value;
3626 texts = dobj->control.dtexts;
3627 sprintf(chan_text, "c%d", mach->mach_params.dmic_num);
3629 for (i = 0; i < se->items; i++) {
3630 struct snd_ctl_elem_value val = {};
3632 if (strstr(texts[i], chan_text)) {
3633 val.value.enumerated.item[0] = i;
3634 kcontrol->put(kcontrol, &val);
3640 static struct snd_soc_tplg_ops skl_tplg_ops = {
3641 .widget_load = skl_tplg_widget_load,
3642 .control_load = skl_tplg_control_load,
3643 .bytes_ext_ops = skl_tlv_ops,
3644 .bytes_ext_ops_count = ARRAY_SIZE(skl_tlv_ops),
3645 .io_ops = skl_tplg_kcontrol_ops,
3646 .io_ops_count = ARRAY_SIZE(skl_tplg_kcontrol_ops),
3647 .manifest = skl_manifest_load,
3648 .dai_load = skl_dai_load,
3649 .complete = skl_tplg_complete,
3653 * A pipe can have multiple modules, each of them will be a DAPM widget as
3654 * well. While managing a pipeline we need to get the list of all the
3655 * widgets in a pipelines, so this helper - skl_tplg_create_pipe_widget_list()
3656 * helps to get the SKL type widgets in that pipeline
3658 static int skl_tplg_create_pipe_widget_list(struct snd_soc_component *component)
3660 struct snd_soc_dapm_widget *w;
3661 struct skl_module_cfg *mcfg = NULL;
3662 struct skl_pipe_module *p_module = NULL;
3663 struct skl_pipe *pipe;
3665 list_for_each_entry(w, &component->card->widgets, list) {
3666 if (is_skl_dsp_widget_type(w, component->dev) && w->priv) {
3670 p_module = devm_kzalloc(component->dev,
3671 sizeof(*p_module), GFP_KERNEL);
3676 list_add_tail(&p_module->node, &pipe->w_list);
3683 static void skl_tplg_set_pipe_type(struct skl_dev *skl, struct skl_pipe *pipe)
3685 struct skl_pipe_module *w_module;
3686 struct snd_soc_dapm_widget *w;
3687 struct skl_module_cfg *mconfig;
3688 bool host_found = false, link_found = false;
3690 list_for_each_entry(w_module, &pipe->w_list, node) {
3694 if (mconfig->dev_type == SKL_DEVICE_HDAHOST)
3696 else if (mconfig->dev_type != SKL_DEVICE_NONE)
3700 if (host_found && link_found)
3701 pipe->passthru = true;
3703 pipe->passthru = false;
3707 * SKL topology init routine
3709 int skl_tplg_init(struct snd_soc_component *component, struct hdac_bus *bus)
3712 const struct firmware *fw;
3713 struct skl_dev *skl = bus_to_skl(bus);
3714 struct skl_pipeline *ppl;
3716 ret = reject_firmware(&fw, skl->tplg_name, bus->dev);
3718 char alt_tplg_name[64];
3720 snprintf(alt_tplg_name, sizeof(alt_tplg_name), "/*(DEBLOBBED)*/",
3721 skl->mach->drv_name);
3722 dev_info(bus->dev, "tplg fw %s load failed with %d, trying alternative tplg name %s",
3723 skl->tplg_name, ret, alt_tplg_name);
3725 ret = reject_firmware(&fw, alt_tplg_name, bus->dev);
3727 goto component_load;
3729 dev_info(bus->dev, "tplg %s failed with %d, falling back to /*(DEBLOBBED)*/",
3730 alt_tplg_name, ret);
3732 ret = reject_firmware(&fw, "/*(DEBLOBBED)*/", bus->dev);
3734 dev_err(bus->dev, "Fallback tplg fw %s load failed with %d\n",
3735 "/*(DEBLOBBED)*/", ret);
3743 * The complete tplg for SKL is loaded as index 0, we don't use
3746 ret = snd_soc_tplg_component_load(component, &skl_tplg_ops, fw, 0);
3748 dev_err(bus->dev, "tplg component load failed%d\n", ret);
3752 ret = skl_tplg_create_pipe_widget_list(component);
3754 dev_err(bus->dev, "tplg create pipe widget list failed%d\n",
3759 list_for_each_entry(ppl, &skl->ppl_list, node)
3760 skl_tplg_set_pipe_type(skl, ppl->pipe);
3763 release_firmware(fw);
3767 void skl_tplg_exit(struct snd_soc_component *component, struct hdac_bus *bus)
3769 struct skl_dev *skl = bus_to_skl(bus);
3770 struct skl_pipeline *ppl, *tmp;
3772 list_for_each_entry_safe(ppl, tmp, &skl->ppl_list, node)
3773 list_del(&ppl->node);
3775 /* clean up topology */
3776 snd_soc_tplg_component_remove(component, SND_SOC_TPLG_INDEX_ALL);