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[0];
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[0].inputs[0].fmt;
199 out_fmt = &m_cfg->module->formats[0].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[0];
243 in_fmt = &mcfg->module->formats[0].inputs[0].fmt;
244 out_fmt = &mcfg->module->formats[0].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 = NULL;
554 struct skl_module_cfg *mconfig = NULL;
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);
1467 * Widget data is expected to be stripped of T and L
1469 size -= 2 * sizeof(unsigned int);
1476 if (copy_from_user(ac->params, data, size))
1480 return skl_set_module_params(skl,
1481 (u32 *)ac->params, ac->size,
1482 ac->param_id, mconfig);
1488 static int skl_tplg_mic_control_get(struct snd_kcontrol *kcontrol,
1489 struct snd_ctl_elem_value *ucontrol)
1491 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1492 struct skl_module_cfg *mconfig = w->priv;
1493 struct soc_enum *ec = (struct soc_enum *)kcontrol->private_value;
1494 u32 ch_type = *((u32 *)ec->dobj.private);
1496 if (mconfig->dmic_ch_type == ch_type)
1497 ucontrol->value.enumerated.item[0] =
1498 mconfig->dmic_ch_combo_index;
1500 ucontrol->value.enumerated.item[0] = 0;
1505 static int skl_fill_mic_sel_params(struct skl_module_cfg *mconfig,
1506 struct skl_mic_sel_config *mic_cfg, struct device *dev)
1508 struct skl_specific_cfg *sp_cfg = &mconfig->formats_config;
1510 sp_cfg->caps_size = sizeof(struct skl_mic_sel_config);
1511 sp_cfg->set_params = SKL_PARAM_SET;
1512 sp_cfg->param_id = 0x00;
1513 if (!sp_cfg->caps) {
1514 sp_cfg->caps = devm_kzalloc(dev, sp_cfg->caps_size, GFP_KERNEL);
1519 mic_cfg->mic_switch = SKL_MIC_SEL_SWITCH;
1521 memcpy(sp_cfg->caps, mic_cfg, sp_cfg->caps_size);
1526 static int skl_tplg_mic_control_set(struct snd_kcontrol *kcontrol,
1527 struct snd_ctl_elem_value *ucontrol)
1529 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1530 struct skl_module_cfg *mconfig = w->priv;
1531 struct skl_mic_sel_config mic_cfg = {0};
1532 struct soc_enum *ec = (struct soc_enum *)kcontrol->private_value;
1533 u32 ch_type = *((u32 *)ec->dobj.private);
1535 u8 in_ch, out_ch, index;
1537 mconfig->dmic_ch_type = ch_type;
1538 mconfig->dmic_ch_combo_index = ucontrol->value.enumerated.item[0];
1540 /* enum control index 0 is INVALID, so no channels to be set */
1541 if (mconfig->dmic_ch_combo_index == 0)
1544 /* No valid channel selection map for index 0, so offset by 1 */
1545 index = mconfig->dmic_ch_combo_index - 1;
1549 if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_mono_list))
1552 list = &mic_mono_list[index];
1556 if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_stereo_list))
1559 list = mic_stereo_list[index];
1563 if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_trio_list))
1566 list = mic_trio_list[index];
1570 if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_quatro_list))
1573 list = mic_quatro_list[index];
1577 dev_err(w->dapm->dev,
1578 "Invalid channel %d for mic_select module\n",
1584 /* channel type enum map to number of chanels for that type */
1585 for (out_ch = 0; out_ch < ch_type; out_ch++) {
1586 in_ch = list[out_ch];
1587 mic_cfg.blob[out_ch][in_ch] = SKL_DEFAULT_MIC_SEL_GAIN;
1590 return skl_fill_mic_sel_params(mconfig, &mic_cfg, w->dapm->dev);
1594 * Fill the dma id for host and link. In case of passthrough
1595 * pipeline, this will both host and link in the same
1596 * pipeline, so need to copy the link and host based on dev_type
1598 static void skl_tplg_fill_dma_id(struct skl_module_cfg *mcfg,
1599 struct skl_pipe_params *params)
1601 struct skl_pipe *pipe = mcfg->pipe;
1603 if (pipe->passthru) {
1604 switch (mcfg->dev_type) {
1605 case SKL_DEVICE_HDALINK:
1606 pipe->p_params->link_dma_id = params->link_dma_id;
1607 pipe->p_params->link_index = params->link_index;
1608 pipe->p_params->link_bps = params->link_bps;
1611 case SKL_DEVICE_HDAHOST:
1612 pipe->p_params->host_dma_id = params->host_dma_id;
1613 pipe->p_params->host_bps = params->host_bps;
1619 pipe->p_params->s_fmt = params->s_fmt;
1620 pipe->p_params->ch = params->ch;
1621 pipe->p_params->s_freq = params->s_freq;
1622 pipe->p_params->stream = params->stream;
1623 pipe->p_params->format = params->format;
1626 memcpy(pipe->p_params, params, sizeof(*params));
1631 * The FE params are passed by hw_params of the DAI.
1632 * On hw_params, the params are stored in Gateway module of the FE and we
1633 * need to calculate the format in DSP module configuration, that
1634 * conversion is done here
1636 int skl_tplg_update_pipe_params(struct device *dev,
1637 struct skl_module_cfg *mconfig,
1638 struct skl_pipe_params *params)
1640 struct skl_module_res *res = &mconfig->module->resources[0];
1641 struct skl_dev *skl = get_skl_ctx(dev);
1642 struct skl_module_fmt *format = NULL;
1643 u8 cfg_idx = mconfig->pipe->cur_config_idx;
1645 skl_tplg_fill_dma_id(mconfig, params);
1646 mconfig->fmt_idx = mconfig->mod_cfg[cfg_idx].fmt_idx;
1647 mconfig->res_idx = mconfig->mod_cfg[cfg_idx].res_idx;
1649 if (skl->nr_modules)
1652 if (params->stream == SNDRV_PCM_STREAM_PLAYBACK)
1653 format = &mconfig->module->formats[0].inputs[0].fmt;
1655 format = &mconfig->module->formats[0].outputs[0].fmt;
1657 /* set the hw_params */
1658 format->s_freq = params->s_freq;
1659 format->channels = params->ch;
1660 format->valid_bit_depth = skl_get_bit_depth(params->s_fmt);
1663 * 16 bit is 16 bit container whereas 24 bit is in 32 bit
1664 * container so update bit depth accordingly
1666 switch (format->valid_bit_depth) {
1667 case SKL_DEPTH_16BIT:
1668 format->bit_depth = format->valid_bit_depth;
1671 case SKL_DEPTH_24BIT:
1672 case SKL_DEPTH_32BIT:
1673 format->bit_depth = SKL_DEPTH_32BIT;
1677 dev_err(dev, "Invalid bit depth %x for pipe\n",
1678 format->valid_bit_depth);
1682 if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1683 res->ibs = (format->s_freq / 1000) *
1684 (format->channels) *
1685 (format->bit_depth >> 3);
1687 res->obs = (format->s_freq / 1000) *
1688 (format->channels) *
1689 (format->bit_depth >> 3);
1696 * Query the module config for the FE DAI
1697 * This is used to find the hw_params set for that DAI and apply to FE
1700 struct skl_module_cfg *
1701 skl_tplg_fe_get_cpr_module(struct snd_soc_dai *dai, int stream)
1703 struct snd_soc_dapm_widget *w;
1704 struct snd_soc_dapm_path *p = NULL;
1706 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1707 w = dai->playback_widget;
1708 snd_soc_dapm_widget_for_each_sink_path(w, p) {
1709 if (p->connect && p->sink->power &&
1710 !is_skl_dsp_widget_type(p->sink, dai->dev))
1713 if (p->sink->priv) {
1714 dev_dbg(dai->dev, "set params for %s\n",
1716 return p->sink->priv;
1720 w = dai->capture_widget;
1721 snd_soc_dapm_widget_for_each_source_path(w, p) {
1722 if (p->connect && p->source->power &&
1723 !is_skl_dsp_widget_type(p->source, dai->dev))
1726 if (p->source->priv) {
1727 dev_dbg(dai->dev, "set params for %s\n",
1729 return p->source->priv;
1737 static struct skl_module_cfg *skl_get_mconfig_pb_cpr(
1738 struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
1740 struct snd_soc_dapm_path *p;
1741 struct skl_module_cfg *mconfig = NULL;
1743 snd_soc_dapm_widget_for_each_source_path(w, p) {
1744 if (w->endpoints[SND_SOC_DAPM_DIR_OUT] > 0) {
1746 (p->sink->id == snd_soc_dapm_aif_out) &&
1748 mconfig = p->source->priv;
1751 mconfig = skl_get_mconfig_pb_cpr(dai, p->source);
1759 static struct skl_module_cfg *skl_get_mconfig_cap_cpr(
1760 struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
1762 struct snd_soc_dapm_path *p;
1763 struct skl_module_cfg *mconfig = NULL;
1765 snd_soc_dapm_widget_for_each_sink_path(w, p) {
1766 if (w->endpoints[SND_SOC_DAPM_DIR_IN] > 0) {
1768 (p->source->id == snd_soc_dapm_aif_in) &&
1770 mconfig = p->sink->priv;
1773 mconfig = skl_get_mconfig_cap_cpr(dai, p->sink);
1781 struct skl_module_cfg *
1782 skl_tplg_be_get_cpr_module(struct snd_soc_dai *dai, int stream)
1784 struct snd_soc_dapm_widget *w;
1785 struct skl_module_cfg *mconfig;
1787 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1788 w = dai->playback_widget;
1789 mconfig = skl_get_mconfig_pb_cpr(dai, w);
1791 w = dai->capture_widget;
1792 mconfig = skl_get_mconfig_cap_cpr(dai, w);
1797 static u8 skl_tplg_be_link_type(int dev_type)
1803 ret = NHLT_LINK_SSP;
1806 case SKL_DEVICE_DMIC:
1807 ret = NHLT_LINK_DMIC;
1810 case SKL_DEVICE_I2S:
1811 ret = NHLT_LINK_SSP;
1814 case SKL_DEVICE_HDALINK:
1815 ret = NHLT_LINK_HDA;
1819 ret = NHLT_LINK_INVALID;
1827 * Fill the BE gateway parameters
1828 * The BE gateway expects a blob of parameters which are kept in the ACPI
1829 * NHLT blob, so query the blob for interface type (i2s/pdm) and instance.
1830 * The port can have multiple settings so pick based on the PCM
1833 static int skl_tplg_be_fill_pipe_params(struct snd_soc_dai *dai,
1834 struct skl_module_cfg *mconfig,
1835 struct skl_pipe_params *params)
1837 struct nhlt_specific_cfg *cfg;
1838 struct skl_dev *skl = get_skl_ctx(dai->dev);
1839 int link_type = skl_tplg_be_link_type(mconfig->dev_type);
1840 u8 dev_type = skl_tplg_be_dev_type(mconfig->dev_type);
1842 skl_tplg_fill_dma_id(mconfig, params);
1844 if (link_type == NHLT_LINK_HDA)
1847 /* update the blob based on virtual bus_id*/
1848 cfg = skl_get_ep_blob(skl, mconfig->vbus_id, link_type,
1849 params->s_fmt, params->ch,
1850 params->s_freq, params->stream,
1853 mconfig->formats_config.caps_size = cfg->size;
1854 mconfig->formats_config.caps = (u32 *) &cfg->caps;
1856 dev_err(dai->dev, "Blob NULL for id %x type %d dirn %d\n",
1857 mconfig->vbus_id, link_type,
1859 dev_err(dai->dev, "PCM: ch %d, freq %d, fmt %d\n",
1860 params->ch, params->s_freq, params->s_fmt);
1867 static int skl_tplg_be_set_src_pipe_params(struct snd_soc_dai *dai,
1868 struct snd_soc_dapm_widget *w,
1869 struct skl_pipe_params *params)
1871 struct snd_soc_dapm_path *p;
1874 snd_soc_dapm_widget_for_each_source_path(w, p) {
1875 if (p->connect && is_skl_dsp_widget_type(p->source, dai->dev) &&
1878 ret = skl_tplg_be_fill_pipe_params(dai,
1879 p->source->priv, params);
1883 ret = skl_tplg_be_set_src_pipe_params(dai,
1893 static int skl_tplg_be_set_sink_pipe_params(struct snd_soc_dai *dai,
1894 struct snd_soc_dapm_widget *w, struct skl_pipe_params *params)
1896 struct snd_soc_dapm_path *p = NULL;
1899 snd_soc_dapm_widget_for_each_sink_path(w, p) {
1900 if (p->connect && is_skl_dsp_widget_type(p->sink, dai->dev) &&
1903 ret = skl_tplg_be_fill_pipe_params(dai,
1904 p->sink->priv, params);
1908 ret = skl_tplg_be_set_sink_pipe_params(
1909 dai, p->sink, params);
1919 * BE hw_params can be a source parameters (capture) or sink parameters
1920 * (playback). Based on sink and source we need to either find the source
1921 * list or the sink list and set the pipeline parameters
1923 int skl_tplg_be_update_params(struct snd_soc_dai *dai,
1924 struct skl_pipe_params *params)
1926 struct snd_soc_dapm_widget *w;
1928 if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1929 w = dai->playback_widget;
1931 return skl_tplg_be_set_src_pipe_params(dai, w, params);
1934 w = dai->capture_widget;
1936 return skl_tplg_be_set_sink_pipe_params(dai, w, params);
1942 static const struct snd_soc_tplg_widget_events skl_tplg_widget_ops[] = {
1943 {SKL_MIXER_EVENT, skl_tplg_mixer_event},
1944 {SKL_VMIXER_EVENT, skl_tplg_mixer_event},
1945 {SKL_PGA_EVENT, skl_tplg_pga_event},
1948 static const struct snd_soc_tplg_bytes_ext_ops skl_tlv_ops[] = {
1949 {SKL_CONTROL_TYPE_BYTE_TLV, skl_tplg_tlv_control_get,
1950 skl_tplg_tlv_control_set},
1953 static const struct snd_soc_tplg_kcontrol_ops skl_tplg_kcontrol_ops[] = {
1955 .id = SKL_CONTROL_TYPE_MIC_SELECT,
1956 .get = skl_tplg_mic_control_get,
1957 .put = skl_tplg_mic_control_set,
1960 .id = SKL_CONTROL_TYPE_MULTI_IO_SELECT,
1961 .get = skl_tplg_multi_config_get,
1962 .put = skl_tplg_multi_config_set,
1965 .id = SKL_CONTROL_TYPE_MULTI_IO_SELECT_DMIC,
1966 .get = skl_tplg_multi_config_get_dmic,
1967 .put = skl_tplg_multi_config_set_dmic,
1971 static int skl_tplg_fill_pipe_cfg(struct device *dev,
1972 struct skl_pipe *pipe, u32 tkn,
1973 u32 tkn_val, int conf_idx, int dir)
1975 struct skl_pipe_fmt *fmt;
1976 struct skl_path_config *config;
1980 fmt = &pipe->configs[conf_idx].in_fmt;
1984 fmt = &pipe->configs[conf_idx].out_fmt;
1988 dev_err(dev, "Invalid direction: %d\n", dir);
1992 config = &pipe->configs[conf_idx];
1995 case SKL_TKN_U32_CFG_FREQ:
1996 fmt->freq = tkn_val;
1999 case SKL_TKN_U8_CFG_CHAN:
2000 fmt->channels = tkn_val;
2003 case SKL_TKN_U8_CFG_BPS:
2007 case SKL_TKN_U32_PATH_MEM_PGS:
2008 config->mem_pages = tkn_val;
2012 dev_err(dev, "Invalid token config: %d\n", tkn);
2019 static int skl_tplg_fill_pipe_tkn(struct device *dev,
2020 struct skl_pipe *pipe, u32 tkn,
2025 case SKL_TKN_U32_PIPE_CONN_TYPE:
2026 pipe->conn_type = tkn_val;
2029 case SKL_TKN_U32_PIPE_PRIORITY:
2030 pipe->pipe_priority = tkn_val;
2033 case SKL_TKN_U32_PIPE_MEM_PGS:
2034 pipe->memory_pages = tkn_val;
2037 case SKL_TKN_U32_PMODE:
2038 pipe->lp_mode = tkn_val;
2041 case SKL_TKN_U32_PIPE_DIRECTION:
2042 pipe->direction = tkn_val;
2045 case SKL_TKN_U32_NUM_CONFIGS:
2046 pipe->nr_cfgs = tkn_val;
2050 dev_err(dev, "Token not handled %d\n", tkn);
2058 * Add pipeline by parsing the relevant tokens
2059 * Return an existing pipe if the pipe already exists.
2061 static int skl_tplg_add_pipe(struct device *dev,
2062 struct skl_module_cfg *mconfig, struct skl_dev *skl,
2063 struct snd_soc_tplg_vendor_value_elem *tkn_elem)
2065 struct skl_pipeline *ppl;
2066 struct skl_pipe *pipe;
2067 struct skl_pipe_params *params;
2069 list_for_each_entry(ppl, &skl->ppl_list, node) {
2070 if (ppl->pipe->ppl_id == tkn_elem->value) {
2071 mconfig->pipe = ppl->pipe;
2076 ppl = devm_kzalloc(dev, sizeof(*ppl), GFP_KERNEL);
2080 pipe = devm_kzalloc(dev, sizeof(*pipe), GFP_KERNEL);
2084 params = devm_kzalloc(dev, sizeof(*params), GFP_KERNEL);
2088 pipe->p_params = params;
2089 pipe->ppl_id = tkn_elem->value;
2090 INIT_LIST_HEAD(&pipe->w_list);
2093 list_add(&ppl->node, &skl->ppl_list);
2095 mconfig->pipe = pipe;
2096 mconfig->pipe->state = SKL_PIPE_INVALID;
2101 static int skl_tplg_get_uuid(struct device *dev, guid_t *guid,
2102 struct snd_soc_tplg_vendor_uuid_elem *uuid_tkn)
2104 if (uuid_tkn->token == SKL_TKN_UUID) {
2105 import_guid(guid, uuid_tkn->uuid);
2109 dev_err(dev, "Not an UUID token %d\n", uuid_tkn->token);
2114 static int skl_tplg_fill_pin(struct device *dev,
2115 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2116 struct skl_module_pin *m_pin,
2121 switch (tkn_elem->token) {
2122 case SKL_TKN_U32_PIN_MOD_ID:
2123 m_pin[pin_index].id.module_id = tkn_elem->value;
2126 case SKL_TKN_U32_PIN_INST_ID:
2127 m_pin[pin_index].id.instance_id = tkn_elem->value;
2131 ret = skl_tplg_get_uuid(dev, &m_pin[pin_index].id.mod_uuid,
2132 (struct snd_soc_tplg_vendor_uuid_elem *)tkn_elem);
2139 dev_err(dev, "%d Not a pin token\n", tkn_elem->token);
2147 * Parse for pin config specific tokens to fill up the
2148 * module private data
2150 static int skl_tplg_fill_pins_info(struct device *dev,
2151 struct skl_module_cfg *mconfig,
2152 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2153 int dir, int pin_count)
2156 struct skl_module_pin *m_pin;
2160 m_pin = mconfig->m_in_pin;
2164 m_pin = mconfig->m_out_pin;
2168 dev_err(dev, "Invalid direction value\n");
2172 ret = skl_tplg_fill_pin(dev, tkn_elem, m_pin, pin_count);
2176 m_pin[pin_count].in_use = false;
2177 m_pin[pin_count].pin_state = SKL_PIN_UNBIND;
2183 * Fill up input/output module config format based
2186 static int skl_tplg_fill_fmt(struct device *dev,
2187 struct skl_module_fmt *dst_fmt,
2191 case SKL_TKN_U32_FMT_CH:
2192 dst_fmt->channels = value;
2195 case SKL_TKN_U32_FMT_FREQ:
2196 dst_fmt->s_freq = value;
2199 case SKL_TKN_U32_FMT_BIT_DEPTH:
2200 dst_fmt->bit_depth = value;
2203 case SKL_TKN_U32_FMT_SAMPLE_SIZE:
2204 dst_fmt->valid_bit_depth = value;
2207 case SKL_TKN_U32_FMT_CH_CONFIG:
2208 dst_fmt->ch_cfg = value;
2211 case SKL_TKN_U32_FMT_INTERLEAVE:
2212 dst_fmt->interleaving_style = value;
2215 case SKL_TKN_U32_FMT_SAMPLE_TYPE:
2216 dst_fmt->sample_type = value;
2219 case SKL_TKN_U32_FMT_CH_MAP:
2220 dst_fmt->ch_map = value;
2224 dev_err(dev, "Invalid token %d\n", tkn);
2231 static int skl_tplg_widget_fill_fmt(struct device *dev,
2232 struct skl_module_iface *fmt,
2233 u32 tkn, u32 val, u32 dir, int fmt_idx)
2235 struct skl_module_fmt *dst_fmt;
2242 dst_fmt = &fmt->inputs[fmt_idx].fmt;
2246 dst_fmt = &fmt->outputs[fmt_idx].fmt;
2250 dev_err(dev, "Invalid direction: %d\n", dir);
2254 return skl_tplg_fill_fmt(dev, dst_fmt, tkn, val);
2257 static void skl_tplg_fill_pin_dynamic_val(
2258 struct skl_module_pin *mpin, u32 pin_count, u32 value)
2262 for (i = 0; i < pin_count; i++)
2263 mpin[i].is_dynamic = value;
2267 * Resource table in the manifest has pin specific resources
2268 * like pin and pin buffer size
2270 static int skl_tplg_manifest_pin_res_tkn(struct device *dev,
2271 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2272 struct skl_module_res *res, int pin_idx, int dir)
2274 struct skl_module_pin_resources *m_pin;
2278 m_pin = &res->input[pin_idx];
2282 m_pin = &res->output[pin_idx];
2286 dev_err(dev, "Invalid pin direction: %d\n", dir);
2290 switch (tkn_elem->token) {
2291 case SKL_TKN_MM_U32_RES_PIN_ID:
2292 m_pin->pin_index = tkn_elem->value;
2295 case SKL_TKN_MM_U32_PIN_BUF:
2296 m_pin->buf_size = tkn_elem->value;
2300 dev_err(dev, "Invalid token: %d\n", tkn_elem->token);
2308 * Fill module specific resources from the manifest's resource
2309 * table like CPS, DMA size, mem_pages.
2311 static int skl_tplg_fill_res_tkn(struct device *dev,
2312 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2313 struct skl_module_res *res,
2314 int pin_idx, int dir)
2316 int ret, tkn_count = 0;
2321 switch (tkn_elem->token) {
2322 case SKL_TKN_MM_U32_DMA_SIZE:
2323 res->dma_buffer_size = tkn_elem->value;
2326 case SKL_TKN_MM_U32_CPC:
2327 res->cpc = tkn_elem->value;
2330 case SKL_TKN_U32_MEM_PAGES:
2331 res->is_pages = tkn_elem->value;
2334 case SKL_TKN_U32_OBS:
2335 res->obs = tkn_elem->value;
2338 case SKL_TKN_U32_IBS:
2339 res->ibs = tkn_elem->value;
2342 case SKL_TKN_MM_U32_RES_PIN_ID:
2343 case SKL_TKN_MM_U32_PIN_BUF:
2344 ret = skl_tplg_manifest_pin_res_tkn(dev, tkn_elem, res,
2350 case SKL_TKN_MM_U32_CPS:
2351 case SKL_TKN_U32_MAX_MCPS:
2352 /* ignore unused tokens */
2356 dev_err(dev, "Not a res type token: %d", tkn_elem->token);
2366 * Parse tokens to fill up the module private data
2368 static int skl_tplg_get_token(struct device *dev,
2369 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2370 struct skl_dev *skl, struct skl_module_cfg *mconfig)
2374 static int is_pipe_exists;
2375 static int pin_index, dir, conf_idx;
2376 struct skl_module_iface *iface = NULL;
2377 struct skl_module_res *res = NULL;
2378 int res_idx = mconfig->res_idx;
2379 int fmt_idx = mconfig->fmt_idx;
2382 * If the manifest structure contains no modules, fill all
2383 * the module data to 0th index.
2384 * res_idx and fmt_idx are default set to 0.
2386 if (skl->nr_modules == 0) {
2387 res = &mconfig->module->resources[res_idx];
2388 iface = &mconfig->module->formats[fmt_idx];
2391 if (tkn_elem->token > SKL_TKN_MAX)
2394 switch (tkn_elem->token) {
2395 case SKL_TKN_U8_IN_QUEUE_COUNT:
2396 mconfig->module->max_input_pins = tkn_elem->value;
2399 case SKL_TKN_U8_OUT_QUEUE_COUNT:
2400 mconfig->module->max_output_pins = tkn_elem->value;
2403 case SKL_TKN_U8_DYN_IN_PIN:
2404 if (!mconfig->m_in_pin)
2406 devm_kcalloc(dev, MAX_IN_QUEUE,
2407 sizeof(*mconfig->m_in_pin),
2409 if (!mconfig->m_in_pin)
2412 skl_tplg_fill_pin_dynamic_val(mconfig->m_in_pin, MAX_IN_QUEUE,
2416 case SKL_TKN_U8_DYN_OUT_PIN:
2417 if (!mconfig->m_out_pin)
2418 mconfig->m_out_pin =
2419 devm_kcalloc(dev, MAX_IN_QUEUE,
2420 sizeof(*mconfig->m_in_pin),
2422 if (!mconfig->m_out_pin)
2425 skl_tplg_fill_pin_dynamic_val(mconfig->m_out_pin, MAX_OUT_QUEUE,
2429 case SKL_TKN_U8_TIME_SLOT:
2430 mconfig->time_slot = tkn_elem->value;
2433 case SKL_TKN_U8_CORE_ID:
2434 mconfig->core_id = tkn_elem->value;
2437 case SKL_TKN_U8_MOD_TYPE:
2438 mconfig->m_type = tkn_elem->value;
2441 case SKL_TKN_U8_DEV_TYPE:
2442 mconfig->dev_type = tkn_elem->value;
2445 case SKL_TKN_U8_HW_CONN_TYPE:
2446 mconfig->hw_conn_type = tkn_elem->value;
2449 case SKL_TKN_U16_MOD_INST_ID:
2450 mconfig->id.instance_id =
2454 case SKL_TKN_U32_MEM_PAGES:
2455 case SKL_TKN_U32_MAX_MCPS:
2456 case SKL_TKN_U32_OBS:
2457 case SKL_TKN_U32_IBS:
2458 ret = skl_tplg_fill_res_tkn(dev, tkn_elem, res, pin_index, dir);
2464 case SKL_TKN_U32_VBUS_ID:
2465 mconfig->vbus_id = tkn_elem->value;
2468 case SKL_TKN_U32_PARAMS_FIXUP:
2469 mconfig->params_fixup = tkn_elem->value;
2472 case SKL_TKN_U32_CONVERTER:
2473 mconfig->converter = tkn_elem->value;
2476 case SKL_TKN_U32_D0I3_CAPS:
2477 mconfig->d0i3_caps = tkn_elem->value;
2480 case SKL_TKN_U32_PIPE_ID:
2481 ret = skl_tplg_add_pipe(dev,
2482 mconfig, skl, tkn_elem);
2485 if (ret == -EEXIST) {
2489 return is_pipe_exists;
2494 case SKL_TKN_U32_PIPE_CONFIG_ID:
2495 conf_idx = tkn_elem->value;
2498 case SKL_TKN_U32_PIPE_CONN_TYPE:
2499 case SKL_TKN_U32_PIPE_PRIORITY:
2500 case SKL_TKN_U32_PIPE_MEM_PGS:
2501 case SKL_TKN_U32_PMODE:
2502 case SKL_TKN_U32_PIPE_DIRECTION:
2503 case SKL_TKN_U32_NUM_CONFIGS:
2504 if (is_pipe_exists) {
2505 ret = skl_tplg_fill_pipe_tkn(dev, mconfig->pipe,
2506 tkn_elem->token, tkn_elem->value);
2513 case SKL_TKN_U32_PATH_MEM_PGS:
2514 case SKL_TKN_U32_CFG_FREQ:
2515 case SKL_TKN_U8_CFG_CHAN:
2516 case SKL_TKN_U8_CFG_BPS:
2517 if (mconfig->pipe->nr_cfgs) {
2518 ret = skl_tplg_fill_pipe_cfg(dev, mconfig->pipe,
2519 tkn_elem->token, tkn_elem->value,
2526 case SKL_TKN_CFG_MOD_RES_ID:
2527 mconfig->mod_cfg[conf_idx].res_idx = tkn_elem->value;
2530 case SKL_TKN_CFG_MOD_FMT_ID:
2531 mconfig->mod_cfg[conf_idx].fmt_idx = tkn_elem->value;
2535 * SKL_TKN_U32_DIR_PIN_COUNT token has the value for both
2536 * direction and the pin count. The first four bits represent
2537 * direction and next four the pin count.
2539 case SKL_TKN_U32_DIR_PIN_COUNT:
2540 dir = tkn_elem->value & SKL_IN_DIR_BIT_MASK;
2541 pin_index = (tkn_elem->value &
2542 SKL_PIN_COUNT_MASK) >> 4;
2546 case SKL_TKN_U32_FMT_CH:
2547 case SKL_TKN_U32_FMT_FREQ:
2548 case SKL_TKN_U32_FMT_BIT_DEPTH:
2549 case SKL_TKN_U32_FMT_SAMPLE_SIZE:
2550 case SKL_TKN_U32_FMT_CH_CONFIG:
2551 case SKL_TKN_U32_FMT_INTERLEAVE:
2552 case SKL_TKN_U32_FMT_SAMPLE_TYPE:
2553 case SKL_TKN_U32_FMT_CH_MAP:
2554 ret = skl_tplg_widget_fill_fmt(dev, iface, tkn_elem->token,
2555 tkn_elem->value, dir, pin_index);
2562 case SKL_TKN_U32_PIN_MOD_ID:
2563 case SKL_TKN_U32_PIN_INST_ID:
2565 ret = skl_tplg_fill_pins_info(dev,
2566 mconfig, tkn_elem, dir,
2573 case SKL_TKN_U32_CAPS_SIZE:
2574 mconfig->formats_config.caps_size =
2579 case SKL_TKN_U32_CAPS_SET_PARAMS:
2580 mconfig->formats_config.set_params =
2584 case SKL_TKN_U32_CAPS_PARAMS_ID:
2585 mconfig->formats_config.param_id =
2589 case SKL_TKN_U32_PROC_DOMAIN:
2595 case SKL_TKN_U32_DMA_BUF_SIZE:
2596 mconfig->dma_buffer_size = tkn_elem->value;
2599 case SKL_TKN_U8_IN_PIN_TYPE:
2600 case SKL_TKN_U8_OUT_PIN_TYPE:
2601 case SKL_TKN_U8_CONN_TYPE:
2605 dev_err(dev, "Token %d not handled\n",
2616 * Parse the vendor array for specific tokens to construct
2617 * module private data
2619 static int skl_tplg_get_tokens(struct device *dev,
2620 char *pvt_data, struct skl_dev *skl,
2621 struct skl_module_cfg *mconfig, int block_size)
2623 struct snd_soc_tplg_vendor_array *array;
2624 struct snd_soc_tplg_vendor_value_elem *tkn_elem;
2625 int tkn_count = 0, ret;
2626 int off = 0, tuple_size = 0;
2627 bool is_module_guid = true;
2629 if (block_size <= 0)
2632 while (tuple_size < block_size) {
2633 array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off);
2637 switch (array->type) {
2638 case SND_SOC_TPLG_TUPLE_TYPE_STRING:
2639 dev_warn(dev, "no string tokens expected for skl tplg\n");
2642 case SND_SOC_TPLG_TUPLE_TYPE_UUID:
2643 if (is_module_guid) {
2644 ret = skl_tplg_get_uuid(dev, (guid_t *)mconfig->guid,
2646 is_module_guid = false;
2648 ret = skl_tplg_get_token(dev, array->value, skl,
2655 tuple_size += sizeof(*array->uuid);
2660 tkn_elem = array->value;
2665 while (tkn_count <= (array->num_elems - 1)) {
2666 ret = skl_tplg_get_token(dev, tkn_elem,
2672 tkn_count = tkn_count + ret;
2676 tuple_size += tkn_count * sizeof(*tkn_elem);
2683 * Every data block is preceded by a descriptor to read the number
2684 * of data blocks, they type of the block and it's size
2686 static int skl_tplg_get_desc_blocks(struct device *dev,
2687 struct snd_soc_tplg_vendor_array *array)
2689 struct snd_soc_tplg_vendor_value_elem *tkn_elem;
2691 tkn_elem = array->value;
2693 switch (tkn_elem->token) {
2694 case SKL_TKN_U8_NUM_BLOCKS:
2695 case SKL_TKN_U8_BLOCK_TYPE:
2696 case SKL_TKN_U16_BLOCK_SIZE:
2697 return tkn_elem->value;
2700 dev_err(dev, "Invalid descriptor token %d\n", tkn_elem->token);
2707 /* Functions to parse private data from configuration file format v4 */
2710 * Add pipeline from topology binary into driver pipeline list
2712 * If already added we return that instance
2713 * Otherwise we create a new instance and add into driver list
2715 static int skl_tplg_add_pipe_v4(struct device *dev,
2716 struct skl_module_cfg *mconfig, struct skl_dev *skl,
2717 struct skl_dfw_v4_pipe *dfw_pipe)
2719 struct skl_pipeline *ppl;
2720 struct skl_pipe *pipe;
2721 struct skl_pipe_params *params;
2723 list_for_each_entry(ppl, &skl->ppl_list, node) {
2724 if (ppl->pipe->ppl_id == dfw_pipe->pipe_id) {
2725 mconfig->pipe = ppl->pipe;
2730 ppl = devm_kzalloc(dev, sizeof(*ppl), GFP_KERNEL);
2734 pipe = devm_kzalloc(dev, sizeof(*pipe), GFP_KERNEL);
2738 params = devm_kzalloc(dev, sizeof(*params), GFP_KERNEL);
2742 pipe->ppl_id = dfw_pipe->pipe_id;
2743 pipe->memory_pages = dfw_pipe->memory_pages;
2744 pipe->pipe_priority = dfw_pipe->pipe_priority;
2745 pipe->conn_type = dfw_pipe->conn_type;
2746 pipe->state = SKL_PIPE_INVALID;
2747 pipe->p_params = params;
2748 INIT_LIST_HEAD(&pipe->w_list);
2751 list_add(&ppl->node, &skl->ppl_list);
2753 mconfig->pipe = pipe;
2758 static void skl_fill_module_pin_info_v4(struct skl_dfw_v4_module_pin *dfw_pin,
2759 struct skl_module_pin *m_pin,
2760 bool is_dynamic, int max_pin)
2764 for (i = 0; i < max_pin; i++) {
2765 m_pin[i].id.module_id = dfw_pin[i].module_id;
2766 m_pin[i].id.instance_id = dfw_pin[i].instance_id;
2767 m_pin[i].in_use = false;
2768 m_pin[i].is_dynamic = is_dynamic;
2769 m_pin[i].pin_state = SKL_PIN_UNBIND;
2773 static void skl_tplg_fill_fmt_v4(struct skl_module_pin_fmt *dst_fmt,
2774 struct skl_dfw_v4_module_fmt *src_fmt,
2779 for (i = 0; i < pins; i++) {
2780 dst_fmt[i].fmt.channels = src_fmt[i].channels;
2781 dst_fmt[i].fmt.s_freq = src_fmt[i].freq;
2782 dst_fmt[i].fmt.bit_depth = src_fmt[i].bit_depth;
2783 dst_fmt[i].fmt.valid_bit_depth = src_fmt[i].valid_bit_depth;
2784 dst_fmt[i].fmt.ch_cfg = src_fmt[i].ch_cfg;
2785 dst_fmt[i].fmt.ch_map = src_fmt[i].ch_map;
2786 dst_fmt[i].fmt.interleaving_style =
2787 src_fmt[i].interleaving_style;
2788 dst_fmt[i].fmt.sample_type = src_fmt[i].sample_type;
2792 static int skl_tplg_get_pvt_data_v4(struct snd_soc_tplg_dapm_widget *tplg_w,
2793 struct skl_dev *skl, struct device *dev,
2794 struct skl_module_cfg *mconfig)
2796 struct skl_dfw_v4_module *dfw =
2797 (struct skl_dfw_v4_module *)tplg_w->priv.data;
2800 dev_dbg(dev, "Parsing Skylake v4 widget topology data\n");
2802 ret = guid_parse(dfw->uuid, (guid_t *)mconfig->guid);
2805 mconfig->id.module_id = -1;
2806 mconfig->id.instance_id = dfw->instance_id;
2807 mconfig->module->resources[0].cpc = dfw->max_mcps / 1000;
2808 mconfig->module->resources[0].ibs = dfw->ibs;
2809 mconfig->module->resources[0].obs = dfw->obs;
2810 mconfig->core_id = dfw->core_id;
2811 mconfig->module->max_input_pins = dfw->max_in_queue;
2812 mconfig->module->max_output_pins = dfw->max_out_queue;
2813 mconfig->module->loadable = dfw->is_loadable;
2814 skl_tplg_fill_fmt_v4(mconfig->module->formats[0].inputs, dfw->in_fmt,
2816 skl_tplg_fill_fmt_v4(mconfig->module->formats[0].outputs, dfw->out_fmt,
2819 mconfig->params_fixup = dfw->params_fixup;
2820 mconfig->converter = dfw->converter;
2821 mconfig->m_type = dfw->module_type;
2822 mconfig->vbus_id = dfw->vbus_id;
2823 mconfig->module->resources[0].is_pages = dfw->mem_pages;
2825 ret = skl_tplg_add_pipe_v4(dev, mconfig, skl, &dfw->pipe);
2829 mconfig->dev_type = dfw->dev_type;
2830 mconfig->hw_conn_type = dfw->hw_conn_type;
2831 mconfig->time_slot = dfw->time_slot;
2832 mconfig->formats_config.caps_size = dfw->caps.caps_size;
2834 mconfig->m_in_pin = devm_kcalloc(dev,
2835 MAX_IN_QUEUE, sizeof(*mconfig->m_in_pin),
2837 if (!mconfig->m_in_pin)
2840 mconfig->m_out_pin = devm_kcalloc(dev,
2841 MAX_OUT_QUEUE, sizeof(*mconfig->m_out_pin),
2843 if (!mconfig->m_out_pin)
2846 skl_fill_module_pin_info_v4(dfw->in_pin, mconfig->m_in_pin,
2847 dfw->is_dynamic_in_pin,
2848 mconfig->module->max_input_pins);
2849 skl_fill_module_pin_info_v4(dfw->out_pin, mconfig->m_out_pin,
2850 dfw->is_dynamic_out_pin,
2851 mconfig->module->max_output_pins);
2853 if (mconfig->formats_config.caps_size) {
2854 mconfig->formats_config.set_params = dfw->caps.set_params;
2855 mconfig->formats_config.param_id = dfw->caps.param_id;
2856 mconfig->formats_config.caps =
2857 devm_kzalloc(dev, mconfig->formats_config.caps_size,
2859 if (!mconfig->formats_config.caps)
2861 memcpy(mconfig->formats_config.caps, dfw->caps.caps,
2862 dfw->caps.caps_size);
2869 * Parse the private data for the token and corresponding value.
2870 * The private data can have multiple data blocks. So, a data block
2871 * is preceded by a descriptor for number of blocks and a descriptor
2872 * for the type and size of the suceeding data block.
2874 static int skl_tplg_get_pvt_data(struct snd_soc_tplg_dapm_widget *tplg_w,
2875 struct skl_dev *skl, struct device *dev,
2876 struct skl_module_cfg *mconfig)
2878 struct snd_soc_tplg_vendor_array *array;
2879 int num_blocks, block_size = 0, block_type, off = 0;
2884 * v4 configuration files have a valid UUID at the start of
2885 * the widget's private data.
2887 if (uuid_is_valid((char *)tplg_w->priv.data))
2888 return skl_tplg_get_pvt_data_v4(tplg_w, skl, dev, mconfig);
2890 /* Read the NUM_DATA_BLOCKS descriptor */
2891 array = (struct snd_soc_tplg_vendor_array *)tplg_w->priv.data;
2892 ret = skl_tplg_get_desc_blocks(dev, array);
2898 /* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */
2899 while (num_blocks > 0) {
2900 array = (struct snd_soc_tplg_vendor_array *)
2901 (tplg_w->priv.data + off);
2903 ret = skl_tplg_get_desc_blocks(dev, array);
2910 array = (struct snd_soc_tplg_vendor_array *)
2911 (tplg_w->priv.data + off);
2913 ret = skl_tplg_get_desc_blocks(dev, array);
2920 array = (struct snd_soc_tplg_vendor_array *)
2921 (tplg_w->priv.data + off);
2923 data = (tplg_w->priv.data + off);
2925 if (block_type == SKL_TYPE_TUPLE) {
2926 ret = skl_tplg_get_tokens(dev, data,
2927 skl, mconfig, block_size);
2934 if (mconfig->formats_config.caps_size > 0)
2935 memcpy(mconfig->formats_config.caps, data,
2936 mconfig->formats_config.caps_size);
2938 ret = mconfig->formats_config.caps_size;
2946 static void skl_clear_pin_config(struct snd_soc_component *component,
2947 struct snd_soc_dapm_widget *w)
2950 struct skl_module_cfg *mconfig;
2951 struct skl_pipe *pipe;
2953 if (!strncmp(w->dapm->component->name, component->name,
2954 strlen(component->name))) {
2956 pipe = mconfig->pipe;
2957 for (i = 0; i < mconfig->module->max_input_pins; i++) {
2958 mconfig->m_in_pin[i].in_use = false;
2959 mconfig->m_in_pin[i].pin_state = SKL_PIN_UNBIND;
2961 for (i = 0; i < mconfig->module->max_output_pins; i++) {
2962 mconfig->m_out_pin[i].in_use = false;
2963 mconfig->m_out_pin[i].pin_state = SKL_PIN_UNBIND;
2965 pipe->state = SKL_PIPE_INVALID;
2966 mconfig->m_state = SKL_MODULE_UNINIT;
2970 void skl_cleanup_resources(struct skl_dev *skl)
2972 struct snd_soc_component *soc_component = skl->component;
2973 struct snd_soc_dapm_widget *w;
2974 struct snd_soc_card *card;
2976 if (soc_component == NULL)
2979 card = soc_component->card;
2980 if (!card || !card->instantiated)
2983 list_for_each_entry(w, &card->widgets, list) {
2984 if (is_skl_dsp_widget_type(w, skl->dev) && w->priv != NULL)
2985 skl_clear_pin_config(soc_component, w);
2988 skl_clear_module_cnt(skl->dsp);
2992 * Topology core widget load callback
2994 * This is used to save the private data for each widget which gives
2995 * information to the driver about module and pipeline parameters which DSP
2996 * FW expects like ids, resource values, formats etc
2998 static int skl_tplg_widget_load(struct snd_soc_component *cmpnt, int index,
2999 struct snd_soc_dapm_widget *w,
3000 struct snd_soc_tplg_dapm_widget *tplg_w)
3003 struct hdac_bus *bus = snd_soc_component_get_drvdata(cmpnt);
3004 struct skl_dev *skl = bus_to_skl(bus);
3005 struct skl_module_cfg *mconfig;
3007 if (!tplg_w->priv.size)
3010 mconfig = devm_kzalloc(bus->dev, sizeof(*mconfig), GFP_KERNEL);
3015 if (skl->nr_modules == 0) {
3016 mconfig->module = devm_kzalloc(bus->dev,
3017 sizeof(*mconfig->module), GFP_KERNEL);
3018 if (!mconfig->module)
3025 * module binary can be loaded later, so set it to query when
3026 * module is load for a use case
3028 mconfig->id.module_id = -1;
3030 /* Parse private data for tuples */
3031 ret = skl_tplg_get_pvt_data(tplg_w, skl, bus->dev, mconfig);
3035 skl_debug_init_module(skl->debugfs, w, mconfig);
3038 if (tplg_w->event_type == 0) {
3039 dev_dbg(bus->dev, "ASoC: No event handler required\n");
3043 ret = snd_soc_tplg_widget_bind_event(w, skl_tplg_widget_ops,
3044 ARRAY_SIZE(skl_tplg_widget_ops),
3045 tplg_w->event_type);
3048 dev_err(bus->dev, "%s: No matching event handlers found for %d\n",
3049 __func__, tplg_w->event_type);
3056 static int skl_init_algo_data(struct device *dev, struct soc_bytes_ext *be,
3057 struct snd_soc_tplg_bytes_control *bc)
3059 struct skl_algo_data *ac;
3060 struct skl_dfw_algo_data *dfw_ac =
3061 (struct skl_dfw_algo_data *)bc->priv.data;
3063 ac = devm_kzalloc(dev, sizeof(*ac), GFP_KERNEL);
3067 /* Fill private data */
3068 ac->max = dfw_ac->max;
3069 ac->param_id = dfw_ac->param_id;
3070 ac->set_params = dfw_ac->set_params;
3071 ac->size = dfw_ac->max;
3074 ac->params = devm_kzalloc(dev, ac->max, GFP_KERNEL);
3078 memcpy(ac->params, dfw_ac->params, ac->max);
3081 be->dobj.private = ac;
3085 static int skl_init_enum_data(struct device *dev, struct soc_enum *se,
3086 struct snd_soc_tplg_enum_control *ec)
3091 if (ec->priv.size) {
3092 data = devm_kzalloc(dev, sizeof(ec->priv.size), GFP_KERNEL);
3095 memcpy(data, ec->priv.data, ec->priv.size);
3096 se->dobj.private = data;
3103 static int skl_tplg_control_load(struct snd_soc_component *cmpnt,
3105 struct snd_kcontrol_new *kctl,
3106 struct snd_soc_tplg_ctl_hdr *hdr)
3108 struct soc_bytes_ext *sb;
3109 struct snd_soc_tplg_bytes_control *tplg_bc;
3110 struct snd_soc_tplg_enum_control *tplg_ec;
3111 struct hdac_bus *bus = snd_soc_component_get_drvdata(cmpnt);
3112 struct soc_enum *se;
3114 switch (hdr->ops.info) {
3115 case SND_SOC_TPLG_CTL_BYTES:
3116 tplg_bc = container_of(hdr,
3117 struct snd_soc_tplg_bytes_control, hdr);
3118 if (kctl->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
3119 sb = (struct soc_bytes_ext *)kctl->private_value;
3120 if (tplg_bc->priv.size)
3121 return skl_init_algo_data(
3122 bus->dev, sb, tplg_bc);
3126 case SND_SOC_TPLG_CTL_ENUM:
3127 tplg_ec = container_of(hdr,
3128 struct snd_soc_tplg_enum_control, hdr);
3129 if (kctl->access & SNDRV_CTL_ELEM_ACCESS_READ) {
3130 se = (struct soc_enum *)kctl->private_value;
3131 if (tplg_ec->priv.size)
3132 skl_init_enum_data(bus->dev, se, tplg_ec);
3136 * now that the control initializations are done, remove
3137 * write permission for the DMIC configuration enums to
3138 * avoid conflicts between NHLT settings and user interaction
3141 if (hdr->ops.get == SKL_CONTROL_TYPE_MULTI_IO_SELECT_DMIC)
3142 kctl->access = SNDRV_CTL_ELEM_ACCESS_READ;
3147 dev_dbg(bus->dev, "Control load not supported %d:%d:%d\n",
3148 hdr->ops.get, hdr->ops.put, hdr->ops.info);
3155 static int skl_tplg_fill_str_mfest_tkn(struct device *dev,
3156 struct snd_soc_tplg_vendor_string_elem *str_elem,
3157 struct skl_dev *skl)
3160 static int ref_count;
3162 switch (str_elem->token) {
3163 case SKL_TKN_STR_LIB_NAME:
3164 if (ref_count > skl->lib_count - 1) {
3169 strncpy(skl->lib_info[ref_count].name,
3171 ARRAY_SIZE(skl->lib_info[ref_count].name));
3176 dev_err(dev, "Not a string token %d\n", str_elem->token);
3184 static int skl_tplg_get_str_tkn(struct device *dev,
3185 struct snd_soc_tplg_vendor_array *array,
3186 struct skl_dev *skl)
3188 int tkn_count = 0, ret;
3189 struct snd_soc_tplg_vendor_string_elem *str_elem;
3191 str_elem = (struct snd_soc_tplg_vendor_string_elem *)array->value;
3192 while (tkn_count < array->num_elems) {
3193 ret = skl_tplg_fill_str_mfest_tkn(dev, str_elem, skl);
3199 tkn_count = tkn_count + ret;
3205 static int skl_tplg_manifest_fill_fmt(struct device *dev,
3206 struct skl_module_iface *fmt,
3207 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
3208 u32 dir, int fmt_idx)
3210 struct skl_module_pin_fmt *dst_fmt;
3211 struct skl_module_fmt *mod_fmt;
3219 dst_fmt = &fmt->inputs[fmt_idx];
3223 dst_fmt = &fmt->outputs[fmt_idx];
3227 dev_err(dev, "Invalid direction: %d\n", dir);
3231 mod_fmt = &dst_fmt->fmt;
3233 switch (tkn_elem->token) {
3234 case SKL_TKN_MM_U32_INTF_PIN_ID:
3235 dst_fmt->id = tkn_elem->value;
3239 ret = skl_tplg_fill_fmt(dev, mod_fmt, tkn_elem->token,
3249 static int skl_tplg_fill_mod_info(struct device *dev,
3250 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
3251 struct skl_module *mod)
3257 switch (tkn_elem->token) {
3258 case SKL_TKN_U8_IN_PIN_TYPE:
3259 mod->input_pin_type = tkn_elem->value;
3262 case SKL_TKN_U8_OUT_PIN_TYPE:
3263 mod->output_pin_type = tkn_elem->value;
3266 case SKL_TKN_U8_IN_QUEUE_COUNT:
3267 mod->max_input_pins = tkn_elem->value;
3270 case SKL_TKN_U8_OUT_QUEUE_COUNT:
3271 mod->max_output_pins = tkn_elem->value;
3274 case SKL_TKN_MM_U8_NUM_RES:
3275 mod->nr_resources = tkn_elem->value;
3278 case SKL_TKN_MM_U8_NUM_INTF:
3279 mod->nr_interfaces = tkn_elem->value;
3283 dev_err(dev, "Invalid mod info token %d", tkn_elem->token);
3291 static int skl_tplg_get_int_tkn(struct device *dev,
3292 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
3293 struct skl_dev *skl)
3295 int tkn_count = 0, ret;
3296 static int mod_idx, res_val_idx, intf_val_idx, dir, pin_idx;
3297 struct skl_module_res *res = NULL;
3298 struct skl_module_iface *fmt = NULL;
3299 struct skl_module *mod = NULL;
3300 static struct skl_astate_param *astate_table;
3301 static int astate_cfg_idx, count;
3306 mod = skl->modules[mod_idx];
3307 res = &mod->resources[res_val_idx];
3308 fmt = &mod->formats[intf_val_idx];
3311 switch (tkn_elem->token) {
3312 case SKL_TKN_U32_LIB_COUNT:
3313 skl->lib_count = tkn_elem->value;
3316 case SKL_TKN_U8_NUM_MOD:
3317 skl->nr_modules = tkn_elem->value;
3318 skl->modules = devm_kcalloc(dev, skl->nr_modules,
3319 sizeof(*skl->modules), GFP_KERNEL);
3323 for (i = 0; i < skl->nr_modules; i++) {
3324 skl->modules[i] = devm_kzalloc(dev,
3325 sizeof(struct skl_module), GFP_KERNEL);
3326 if (!skl->modules[i])
3331 case SKL_TKN_MM_U8_MOD_IDX:
3332 mod_idx = tkn_elem->value;
3335 case SKL_TKN_U32_ASTATE_COUNT:
3336 if (astate_table != NULL) {
3337 dev_err(dev, "More than one entry for A-State count");
3341 if (tkn_elem->value > SKL_MAX_ASTATE_CFG) {
3342 dev_err(dev, "Invalid A-State count %d\n",
3347 size = struct_size(skl->cfg.astate_cfg, astate_table,
3349 skl->cfg.astate_cfg = devm_kzalloc(dev, size, GFP_KERNEL);
3350 if (!skl->cfg.astate_cfg)
3353 astate_table = skl->cfg.astate_cfg->astate_table;
3354 count = skl->cfg.astate_cfg->count = tkn_elem->value;
3357 case SKL_TKN_U32_ASTATE_IDX:
3358 if (tkn_elem->value >= count) {
3359 dev_err(dev, "Invalid A-State index %d\n",
3364 astate_cfg_idx = tkn_elem->value;
3367 case SKL_TKN_U32_ASTATE_KCPS:
3368 astate_table[astate_cfg_idx].kcps = tkn_elem->value;
3371 case SKL_TKN_U32_ASTATE_CLK_SRC:
3372 astate_table[astate_cfg_idx].clk_src = tkn_elem->value;
3375 case SKL_TKN_U8_IN_PIN_TYPE:
3376 case SKL_TKN_U8_OUT_PIN_TYPE:
3377 case SKL_TKN_U8_IN_QUEUE_COUNT:
3378 case SKL_TKN_U8_OUT_QUEUE_COUNT:
3379 case SKL_TKN_MM_U8_NUM_RES:
3380 case SKL_TKN_MM_U8_NUM_INTF:
3381 ret = skl_tplg_fill_mod_info(dev, tkn_elem, mod);
3386 case SKL_TKN_U32_DIR_PIN_COUNT:
3387 dir = tkn_elem->value & SKL_IN_DIR_BIT_MASK;
3388 pin_idx = (tkn_elem->value & SKL_PIN_COUNT_MASK) >> 4;
3391 case SKL_TKN_MM_U32_RES_ID:
3395 res->id = tkn_elem->value;
3396 res_val_idx = tkn_elem->value;
3399 case SKL_TKN_MM_U32_FMT_ID:
3403 fmt->fmt_idx = tkn_elem->value;
3404 intf_val_idx = tkn_elem->value;
3407 case SKL_TKN_MM_U32_CPS:
3408 case SKL_TKN_MM_U32_DMA_SIZE:
3409 case SKL_TKN_MM_U32_CPC:
3410 case SKL_TKN_U32_MEM_PAGES:
3411 case SKL_TKN_U32_OBS:
3412 case SKL_TKN_U32_IBS:
3413 case SKL_TKN_MM_U32_RES_PIN_ID:
3414 case SKL_TKN_MM_U32_PIN_BUF:
3415 ret = skl_tplg_fill_res_tkn(dev, tkn_elem, res, pin_idx, dir);
3421 case SKL_TKN_MM_U32_NUM_IN_FMT:
3425 res->nr_input_pins = tkn_elem->value;
3428 case SKL_TKN_MM_U32_NUM_OUT_FMT:
3432 res->nr_output_pins = tkn_elem->value;
3435 case SKL_TKN_U32_FMT_CH:
3436 case SKL_TKN_U32_FMT_FREQ:
3437 case SKL_TKN_U32_FMT_BIT_DEPTH:
3438 case SKL_TKN_U32_FMT_SAMPLE_SIZE:
3439 case SKL_TKN_U32_FMT_CH_CONFIG:
3440 case SKL_TKN_U32_FMT_INTERLEAVE:
3441 case SKL_TKN_U32_FMT_SAMPLE_TYPE:
3442 case SKL_TKN_U32_FMT_CH_MAP:
3443 case SKL_TKN_MM_U32_INTF_PIN_ID:
3444 ret = skl_tplg_manifest_fill_fmt(dev, fmt, tkn_elem,
3451 dev_err(dev, "Not a manifest token %d\n", tkn_elem->token);
3460 * Fill the manifest structure by parsing the tokens based on the
3463 static int skl_tplg_get_manifest_tkn(struct device *dev,
3464 char *pvt_data, struct skl_dev *skl,
3467 int tkn_count = 0, ret;
3468 int off = 0, tuple_size = 0;
3470 struct snd_soc_tplg_vendor_array *array;
3471 struct snd_soc_tplg_vendor_value_elem *tkn_elem;
3473 if (block_size <= 0)
3476 while (tuple_size < block_size) {
3477 array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off);
3479 switch (array->type) {
3480 case SND_SOC_TPLG_TUPLE_TYPE_STRING:
3481 ret = skl_tplg_get_str_tkn(dev, array, skl);
3487 tuple_size += tkn_count *
3488 sizeof(struct snd_soc_tplg_vendor_string_elem);
3491 case SND_SOC_TPLG_TUPLE_TYPE_UUID:
3492 if (array->uuid->token != SKL_TKN_UUID) {
3493 dev_err(dev, "Not an UUID token: %d\n",
3494 array->uuid->token);
3497 if (uuid_index >= skl->nr_modules) {
3498 dev_err(dev, "Too many UUID tokens\n");
3501 import_guid(&skl->modules[uuid_index++]->uuid,
3504 tuple_size += sizeof(*array->uuid);
3508 tkn_elem = array->value;
3513 while (tkn_count <= array->num_elems - 1) {
3514 ret = skl_tplg_get_int_tkn(dev,
3519 tkn_count = tkn_count + ret;
3522 tuple_size += (tkn_count * sizeof(*tkn_elem));
3530 * Parse manifest private data for tokens. The private data block is
3531 * preceded by descriptors for type and size of data block.
3533 static int skl_tplg_get_manifest_data(struct snd_soc_tplg_manifest *manifest,
3534 struct device *dev, struct skl_dev *skl)
3536 struct snd_soc_tplg_vendor_array *array;
3537 int num_blocks, block_size = 0, block_type, off = 0;
3541 /* Read the NUM_DATA_BLOCKS descriptor */
3542 array = (struct snd_soc_tplg_vendor_array *)manifest->priv.data;
3543 ret = skl_tplg_get_desc_blocks(dev, array);
3549 /* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */
3550 while (num_blocks > 0) {
3551 array = (struct snd_soc_tplg_vendor_array *)
3552 (manifest->priv.data + off);
3553 ret = skl_tplg_get_desc_blocks(dev, array);
3560 array = (struct snd_soc_tplg_vendor_array *)
3561 (manifest->priv.data + off);
3563 ret = skl_tplg_get_desc_blocks(dev, array);
3570 array = (struct snd_soc_tplg_vendor_array *)
3571 (manifest->priv.data + off);
3573 data = (manifest->priv.data + off);
3575 if (block_type == SKL_TYPE_TUPLE) {
3576 ret = skl_tplg_get_manifest_tkn(dev, data, skl,
3592 static int skl_manifest_load(struct snd_soc_component *cmpnt, int index,
3593 struct snd_soc_tplg_manifest *manifest)
3595 struct hdac_bus *bus = snd_soc_component_get_drvdata(cmpnt);
3596 struct skl_dev *skl = bus_to_skl(bus);
3598 /* proceed only if we have private data defined */
3599 if (manifest->priv.size == 0)
3602 skl_tplg_get_manifest_data(manifest, bus->dev, skl);
3604 if (skl->lib_count > SKL_MAX_LIB) {
3605 dev_err(bus->dev, "Exceeding max Library count. Got:%d\n",
3613 static void skl_tplg_complete(struct snd_soc_component *component)
3615 struct snd_soc_dobj *dobj;
3616 struct snd_soc_acpi_mach *mach =
3617 dev_get_platdata(component->card->dev);
3620 list_for_each_entry(dobj, &component->dobj_list, list) {
3621 struct snd_kcontrol *kcontrol = dobj->control.kcontrol;
3622 struct soc_enum *se =
3623 (struct soc_enum *)kcontrol->private_value;
3624 char **texts = dobj->control.dtexts;
3627 if (dobj->type != SND_SOC_DOBJ_ENUM ||
3628 dobj->control.kcontrol->put !=
3629 skl_tplg_multi_config_set_dmic)
3631 sprintf(chan_text, "c%d", mach->mach_params.dmic_num);
3633 for (i = 0; i < se->items; i++) {
3634 struct snd_ctl_elem_value val;
3636 if (strstr(texts[i], chan_text)) {
3637 val.value.enumerated.item[0] = i;
3638 kcontrol->put(kcontrol, &val);
3644 static struct snd_soc_tplg_ops skl_tplg_ops = {
3645 .widget_load = skl_tplg_widget_load,
3646 .control_load = skl_tplg_control_load,
3647 .bytes_ext_ops = skl_tlv_ops,
3648 .bytes_ext_ops_count = ARRAY_SIZE(skl_tlv_ops),
3649 .io_ops = skl_tplg_kcontrol_ops,
3650 .io_ops_count = ARRAY_SIZE(skl_tplg_kcontrol_ops),
3651 .manifest = skl_manifest_load,
3652 .dai_load = skl_dai_load,
3653 .complete = skl_tplg_complete,
3657 * A pipe can have multiple modules, each of them will be a DAPM widget as
3658 * well. While managing a pipeline we need to get the list of all the
3659 * widgets in a pipelines, so this helper - skl_tplg_create_pipe_widget_list()
3660 * helps to get the SKL type widgets in that pipeline
3662 static int skl_tplg_create_pipe_widget_list(struct snd_soc_component *component)
3664 struct snd_soc_dapm_widget *w;
3665 struct skl_module_cfg *mcfg = NULL;
3666 struct skl_pipe_module *p_module = NULL;
3667 struct skl_pipe *pipe;
3669 list_for_each_entry(w, &component->card->widgets, list) {
3670 if (is_skl_dsp_widget_type(w, component->dev) && w->priv) {
3674 p_module = devm_kzalloc(component->dev,
3675 sizeof(*p_module), GFP_KERNEL);
3680 list_add_tail(&p_module->node, &pipe->w_list);
3687 static void skl_tplg_set_pipe_type(struct skl_dev *skl, struct skl_pipe *pipe)
3689 struct skl_pipe_module *w_module;
3690 struct snd_soc_dapm_widget *w;
3691 struct skl_module_cfg *mconfig;
3692 bool host_found = false, link_found = false;
3694 list_for_each_entry(w_module, &pipe->w_list, node) {
3698 if (mconfig->dev_type == SKL_DEVICE_HDAHOST)
3700 else if (mconfig->dev_type != SKL_DEVICE_NONE)
3704 if (host_found && link_found)
3705 pipe->passthru = true;
3707 pipe->passthru = false;
3711 * SKL topology init routine
3713 int skl_tplg_init(struct snd_soc_component *component, struct hdac_bus *bus)
3716 const struct firmware *fw;
3717 struct skl_dev *skl = bus_to_skl(bus);
3718 struct skl_pipeline *ppl;
3720 ret = request_firmware(&fw, skl->tplg_name, bus->dev);
3722 char alt_tplg_name[64];
3724 snprintf(alt_tplg_name, sizeof(alt_tplg_name), "%s-tplg.bin",
3725 skl->mach->drv_name);
3726 dev_info(bus->dev, "tplg fw %s load failed with %d, trying alternative tplg name %s",
3727 skl->tplg_name, ret, alt_tplg_name);
3729 ret = request_firmware(&fw, alt_tplg_name, bus->dev);
3731 goto component_load;
3733 dev_info(bus->dev, "tplg %s failed with %d, falling back to dfw_sst.bin",
3734 alt_tplg_name, ret);
3736 ret = request_firmware(&fw, "dfw_sst.bin", bus->dev);
3738 dev_err(bus->dev, "Fallback tplg fw %s load failed with %d\n",
3739 "dfw_sst.bin", ret);
3747 * The complete tplg for SKL is loaded as index 0, we don't use
3750 ret = snd_soc_tplg_component_load(component, &skl_tplg_ops, fw, 0);
3752 dev_err(bus->dev, "tplg component load failed%d\n", ret);
3756 ret = skl_tplg_create_pipe_widget_list(component);
3758 dev_err(bus->dev, "tplg create pipe widget list failed%d\n",
3763 list_for_each_entry(ppl, &skl->ppl_list, node)
3764 skl_tplg_set_pipe_type(skl, ppl->pipe);
3767 release_firmware(fw);
3771 void skl_tplg_exit(struct snd_soc_component *component, struct hdac_bus *bus)
3773 struct skl_dev *skl = bus_to_skl(bus);
3774 struct skl_pipeline *ppl, *tmp;
3776 if (!list_empty(&skl->ppl_list))
3777 list_for_each_entry_safe(ppl, tmp, &skl->ppl_list, node)
3778 list_del(&ppl->node);
3780 /* clean up topology */
3781 snd_soc_tplg_component_remove(component, SND_SOC_TPLG_INDEX_ALL);