2 * skl-topology.c - Implements Platform component ALSA controls/widget
5 * Copyright (C) 2014-2015 Intel Corp
6 * Author: Jeeja KP <jeeja.kp@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as version 2, as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
19 #include <linux/slab.h>
20 #include <linux/types.h>
21 #include <linux/firmware.h>
22 #include <sound/soc.h>
23 #include <sound/soc-topology.h>
24 #include <uapi/sound/snd_sst_tokens.h>
25 #include "skl-sst-dsp.h"
26 #include "skl-sst-ipc.h"
27 #include "skl-topology.h"
29 #include "skl-tplg-interface.h"
30 #include "../common/sst-dsp.h"
31 #include "../common/sst-dsp-priv.h"
33 #define SKL_CH_FIXUP_MASK (1 << 0)
34 #define SKL_RATE_FIXUP_MASK (1 << 1)
35 #define SKL_FMT_FIXUP_MASK (1 << 2)
36 #define SKL_IN_DIR_BIT_MASK BIT(0)
37 #define SKL_PIN_COUNT_MASK GENMASK(7, 4)
39 static const int mic_mono_list[] = {
42 static const int mic_stereo_list[][SKL_CH_STEREO] = {
43 {0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 3}, {2, 3},
45 static const int mic_trio_list[][SKL_CH_TRIO] = {
46 {0, 1, 2}, {0, 1, 3}, {0, 2, 3}, {1, 2, 3},
48 static const int mic_quatro_list[][SKL_CH_QUATRO] = {
52 void skl_tplg_d0i3_get(struct skl *skl, enum d0i3_capability caps)
54 struct skl_d0i3_data *d0i3 = &skl->skl_sst->d0i3;
61 case SKL_D0I3_STREAMING:
65 case SKL_D0I3_NON_STREAMING:
66 d0i3->non_streaming++;
71 void skl_tplg_d0i3_put(struct skl *skl, enum d0i3_capability caps)
73 struct skl_d0i3_data *d0i3 = &skl->skl_sst->d0i3;
80 case SKL_D0I3_STREAMING:
84 case SKL_D0I3_NON_STREAMING:
85 d0i3->non_streaming--;
91 * SKL DSP driver modelling uses only few DAPM widgets so for rest we will
92 * ignore. This helpers checks if the SKL driver handles this widget type
94 static int is_skl_dsp_widget_type(struct snd_soc_dapm_widget *w)
97 case snd_soc_dapm_dai_link:
98 case snd_soc_dapm_dai_in:
99 case snd_soc_dapm_aif_in:
100 case snd_soc_dapm_aif_out:
101 case snd_soc_dapm_dai_out:
102 case snd_soc_dapm_switch:
110 * Each pipelines needs memory to be allocated. Check if we have free memory
111 * from available pool.
113 static bool skl_is_pipe_mem_avail(struct skl *skl,
114 struct skl_module_cfg *mconfig)
116 struct skl_sst *ctx = skl->skl_sst;
118 if (skl->resource.mem + mconfig->pipe->memory_pages >
119 skl->resource.max_mem) {
121 "%s: module_id %d instance %d\n", __func__,
122 mconfig->id.module_id,
123 mconfig->id.instance_id);
125 "exceeds ppl memory available %d mem %d\n",
126 skl->resource.max_mem, skl->resource.mem);
134 * Add the mem to the mem pool. This is freed when pipe is deleted.
135 * Note: DSP does actual memory management we only keep track for complete
138 static void skl_tplg_alloc_pipe_mem(struct skl *skl,
139 struct skl_module_cfg *mconfig)
141 skl->resource.mem += mconfig->pipe->memory_pages;
145 * Pipeline needs needs DSP CPU resources for computation, this is
146 * quantified in MCPS (Million Clocks Per Second) required for module/pipe
148 * Each pipelines needs mcps to be allocated. Check if we have mcps for this
152 static bool skl_is_pipe_mcps_avail(struct skl *skl,
153 struct skl_module_cfg *mconfig)
155 struct skl_sst *ctx = skl->skl_sst;
157 if (skl->resource.mcps + mconfig->mcps > skl->resource.max_mcps) {
159 "%s: module_id %d instance %d\n", __func__,
160 mconfig->id.module_id, mconfig->id.instance_id);
162 "exceeds ppl mcps available %d > mem %d\n",
163 skl->resource.max_mcps, skl->resource.mcps);
170 static void skl_tplg_alloc_pipe_mcps(struct skl *skl,
171 struct skl_module_cfg *mconfig)
173 skl->resource.mcps += mconfig->mcps;
177 * Free the mcps when tearing down
180 skl_tplg_free_pipe_mcps(struct skl *skl, struct skl_module_cfg *mconfig)
182 skl->resource.mcps -= mconfig->mcps;
186 * Free the memory when tearing down
189 skl_tplg_free_pipe_mem(struct skl *skl, struct skl_module_cfg *mconfig)
191 skl->resource.mem -= mconfig->pipe->memory_pages;
195 static void skl_dump_mconfig(struct skl_sst *ctx,
196 struct skl_module_cfg *mcfg)
198 dev_dbg(ctx->dev, "Dumping config\n");
199 dev_dbg(ctx->dev, "Input Format:\n");
200 dev_dbg(ctx->dev, "channels = %d\n", mcfg->in_fmt[0].channels);
201 dev_dbg(ctx->dev, "s_freq = %d\n", mcfg->in_fmt[0].s_freq);
202 dev_dbg(ctx->dev, "ch_cfg = %d\n", mcfg->in_fmt[0].ch_cfg);
203 dev_dbg(ctx->dev, "valid bit depth = %d\n", mcfg->in_fmt[0].valid_bit_depth);
204 dev_dbg(ctx->dev, "Output Format:\n");
205 dev_dbg(ctx->dev, "channels = %d\n", mcfg->out_fmt[0].channels);
206 dev_dbg(ctx->dev, "s_freq = %d\n", mcfg->out_fmt[0].s_freq);
207 dev_dbg(ctx->dev, "valid bit depth = %d\n", mcfg->out_fmt[0].valid_bit_depth);
208 dev_dbg(ctx->dev, "ch_cfg = %d\n", mcfg->out_fmt[0].ch_cfg);
211 static void skl_tplg_update_chmap(struct skl_module_fmt *fmt, int chs)
213 int slot_map = 0xFFFFFFFF;
217 for (i = 0; i < chs; i++) {
219 * For 2 channels with starting slot as 0, slot map will
220 * look like 0xFFFFFF10.
222 slot_map &= (~(0xF << (4 * i)) | (start_slot << (4 * i)));
225 fmt->ch_map = slot_map;
228 static void skl_tplg_update_params(struct skl_module_fmt *fmt,
229 struct skl_pipe_params *params, int fixup)
231 if (fixup & SKL_RATE_FIXUP_MASK)
232 fmt->s_freq = params->s_freq;
233 if (fixup & SKL_CH_FIXUP_MASK) {
234 fmt->channels = params->ch;
235 skl_tplg_update_chmap(fmt, fmt->channels);
237 if (fixup & SKL_FMT_FIXUP_MASK) {
238 fmt->valid_bit_depth = skl_get_bit_depth(params->s_fmt);
241 * 16 bit is 16 bit container whereas 24 bit is in 32 bit
242 * container so update bit depth accordingly
244 switch (fmt->valid_bit_depth) {
245 case SKL_DEPTH_16BIT:
246 fmt->bit_depth = fmt->valid_bit_depth;
250 fmt->bit_depth = SKL_DEPTH_32BIT;
258 * A pipeline may have modules which impact the pcm parameters, like SRC,
259 * channel converter, format converter.
260 * We need to calculate the output params by applying the 'fixup'
261 * Topology will tell driver which type of fixup is to be applied by
262 * supplying the fixup mask, so based on that we calculate the output
264 * Now In FE the pcm hw_params is source/target format. Same is applicable
265 * for BE with its hw_params invoked.
266 * here based on FE, BE pipeline and direction we calculate the input and
267 * outfix and then apply that for a module
269 static void skl_tplg_update_params_fixup(struct skl_module_cfg *m_cfg,
270 struct skl_pipe_params *params, bool is_fe)
272 int in_fixup, out_fixup;
273 struct skl_module_fmt *in_fmt, *out_fmt;
275 /* Fixups will be applied to pin 0 only */
276 in_fmt = &m_cfg->in_fmt[0];
277 out_fmt = &m_cfg->out_fmt[0];
279 if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
281 in_fixup = m_cfg->params_fixup;
282 out_fixup = (~m_cfg->converter) &
285 out_fixup = m_cfg->params_fixup;
286 in_fixup = (~m_cfg->converter) &
291 out_fixup = m_cfg->params_fixup;
292 in_fixup = (~m_cfg->converter) &
295 in_fixup = m_cfg->params_fixup;
296 out_fixup = (~m_cfg->converter) &
301 skl_tplg_update_params(in_fmt, params, in_fixup);
302 skl_tplg_update_params(out_fmt, params, out_fixup);
306 * A module needs input and output buffers, which are dependent upon pcm
307 * params, so once we have calculate params, we need buffer calculation as
310 static void skl_tplg_update_buffer_size(struct skl_sst *ctx,
311 struct skl_module_cfg *mcfg)
314 struct skl_module_fmt *in_fmt, *out_fmt;
316 /* Since fixups is applied to pin 0 only, ibs, obs needs
317 * change for pin 0 only
319 in_fmt = &mcfg->in_fmt[0];
320 out_fmt = &mcfg->out_fmt[0];
322 if (mcfg->m_type == SKL_MODULE_TYPE_SRCINT)
325 mcfg->ibs = DIV_ROUND_UP(in_fmt->s_freq, 1000) *
326 in_fmt->channels * (in_fmt->bit_depth >> 3) *
329 mcfg->obs = DIV_ROUND_UP(out_fmt->s_freq, 1000) *
330 out_fmt->channels * (out_fmt->bit_depth >> 3) *
334 static u8 skl_tplg_be_dev_type(int dev_type)
340 ret = NHLT_DEVICE_BT;
343 case SKL_DEVICE_DMIC:
344 ret = NHLT_DEVICE_DMIC;
348 ret = NHLT_DEVICE_I2S;
352 ret = NHLT_DEVICE_INVALID;
359 static int skl_tplg_update_be_blob(struct snd_soc_dapm_widget *w,
362 struct skl_module_cfg *m_cfg = w->priv;
364 u32 ch, s_freq, s_fmt;
365 struct nhlt_specific_cfg *cfg;
366 struct skl *skl = get_skl_ctx(ctx->dev);
367 u8 dev_type = skl_tplg_be_dev_type(m_cfg->dev_type);
369 /* check if we already have blob */
370 if (m_cfg->formats_config.caps_size > 0)
373 dev_dbg(ctx->dev, "Applying default cfg blob\n");
374 switch (m_cfg->dev_type) {
375 case SKL_DEVICE_DMIC:
376 link_type = NHLT_LINK_DMIC;
377 dir = SNDRV_PCM_STREAM_CAPTURE;
378 s_freq = m_cfg->in_fmt[0].s_freq;
379 s_fmt = m_cfg->in_fmt[0].bit_depth;
380 ch = m_cfg->in_fmt[0].channels;
384 link_type = NHLT_LINK_SSP;
385 if (m_cfg->hw_conn_type == SKL_CONN_SOURCE) {
386 dir = SNDRV_PCM_STREAM_PLAYBACK;
387 s_freq = m_cfg->out_fmt[0].s_freq;
388 s_fmt = m_cfg->out_fmt[0].bit_depth;
389 ch = m_cfg->out_fmt[0].channels;
391 dir = SNDRV_PCM_STREAM_CAPTURE;
392 s_freq = m_cfg->in_fmt[0].s_freq;
393 s_fmt = m_cfg->in_fmt[0].bit_depth;
394 ch = m_cfg->in_fmt[0].channels;
402 /* update the blob based on virtual bus_id and default params */
403 cfg = skl_get_ep_blob(skl, m_cfg->vbus_id, link_type,
404 s_fmt, ch, s_freq, dir, dev_type);
406 m_cfg->formats_config.caps_size = cfg->size;
407 m_cfg->formats_config.caps = (u32 *) &cfg->caps;
409 dev_err(ctx->dev, "Blob NULL for id %x type %d dirn %d\n",
410 m_cfg->vbus_id, link_type, dir);
411 dev_err(ctx->dev, "PCM: ch %d, freq %d, fmt %d\n",
419 static void skl_tplg_update_module_params(struct snd_soc_dapm_widget *w,
422 struct skl_module_cfg *m_cfg = w->priv;
423 struct skl_pipe_params *params = m_cfg->pipe->p_params;
424 int p_conn_type = m_cfg->pipe->conn_type;
427 if (!m_cfg->params_fixup)
430 dev_dbg(ctx->dev, "Mconfig for widget=%s BEFORE updation\n",
433 skl_dump_mconfig(ctx, m_cfg);
435 if (p_conn_type == SKL_PIPE_CONN_TYPE_FE)
440 skl_tplg_update_params_fixup(m_cfg, params, is_fe);
441 skl_tplg_update_buffer_size(ctx, m_cfg);
443 dev_dbg(ctx->dev, "Mconfig for widget=%s AFTER updation\n",
446 skl_dump_mconfig(ctx, m_cfg);
450 * some modules can have multiple params set from user control and
451 * need to be set after module is initialized. If set_param flag is
452 * set module params will be done after module is initialised.
454 static int skl_tplg_set_module_params(struct snd_soc_dapm_widget *w,
458 struct skl_module_cfg *mconfig = w->priv;
459 const struct snd_kcontrol_new *k;
460 struct soc_bytes_ext *sb;
461 struct skl_algo_data *bc;
462 struct skl_specific_cfg *sp_cfg;
464 if (mconfig->formats_config.caps_size > 0 &&
465 mconfig->formats_config.set_params == SKL_PARAM_SET) {
466 sp_cfg = &mconfig->formats_config;
467 ret = skl_set_module_params(ctx, sp_cfg->caps,
469 sp_cfg->param_id, mconfig);
474 for (i = 0; i < w->num_kcontrols; i++) {
475 k = &w->kcontrol_news[i];
476 if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
477 sb = (void *) k->private_value;
478 bc = (struct skl_algo_data *)sb->dobj.private;
480 if (bc->set_params == SKL_PARAM_SET) {
481 ret = skl_set_module_params(ctx,
482 (u32 *)bc->params, bc->size,
483 bc->param_id, mconfig);
494 * some module param can set from user control and this is required as
495 * when module is initailzed. if module param is required in init it is
496 * identifed by set_param flag. if set_param flag is not set, then this
497 * parameter needs to set as part of module init.
499 static int skl_tplg_set_module_init_data(struct snd_soc_dapm_widget *w)
501 const struct snd_kcontrol_new *k;
502 struct soc_bytes_ext *sb;
503 struct skl_algo_data *bc;
504 struct skl_module_cfg *mconfig = w->priv;
507 for (i = 0; i < w->num_kcontrols; i++) {
508 k = &w->kcontrol_news[i];
509 if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
510 sb = (struct soc_bytes_ext *)k->private_value;
511 bc = (struct skl_algo_data *)sb->dobj.private;
513 if (bc->set_params != SKL_PARAM_INIT)
516 mconfig->formats_config.caps = (u32 *)bc->params;
517 mconfig->formats_config.caps_size = bc->size;
526 static int skl_tplg_module_prepare(struct skl_sst *ctx, struct skl_pipe *pipe,
527 struct snd_soc_dapm_widget *w, struct skl_module_cfg *mcfg)
529 switch (mcfg->dev_type) {
530 case SKL_DEVICE_HDAHOST:
531 return skl_pcm_host_dma_prepare(ctx->dev, pipe->p_params);
533 case SKL_DEVICE_HDALINK:
534 return skl_pcm_link_dma_prepare(ctx->dev, pipe->p_params);
541 * Inside a pipe instance, we can have various modules. These modules need
542 * to instantiated in DSP by invoking INIT_MODULE IPC, which is achieved by
543 * skl_init_module() routine, so invoke that for all modules in a pipeline
546 skl_tplg_init_pipe_modules(struct skl *skl, struct skl_pipe *pipe)
548 struct skl_pipe_module *w_module;
549 struct snd_soc_dapm_widget *w;
550 struct skl_module_cfg *mconfig;
551 struct skl_sst *ctx = skl->skl_sst;
554 list_for_each_entry(w_module, &pipe->w_list, node) {
559 /* check if module ids are populated */
560 if (mconfig->id.module_id < 0) {
561 dev_err(skl->skl_sst->dev,
562 "module %pUL id not populated\n",
563 (uuid_le *)mconfig->guid);
567 /* check resource available */
568 if (!skl_is_pipe_mcps_avail(skl, mconfig))
571 if (mconfig->is_loadable && ctx->dsp->fw_ops.load_mod) {
572 ret = ctx->dsp->fw_ops.load_mod(ctx->dsp,
573 mconfig->id.module_id, mconfig->guid);
577 mconfig->m_state = SKL_MODULE_LOADED;
580 /* prepare the DMA if the module is gateway cpr */
581 ret = skl_tplg_module_prepare(ctx, pipe, w, mconfig);
585 /* update blob if blob is null for be with default value */
586 skl_tplg_update_be_blob(w, ctx);
589 * apply fix/conversion to module params based on
592 skl_tplg_update_module_params(w, ctx);
593 uuid_mod = (uuid_le *)mconfig->guid;
594 mconfig->id.pvt_id = skl_get_pvt_id(ctx, uuid_mod,
595 mconfig->id.instance_id);
596 if (mconfig->id.pvt_id < 0)
598 skl_tplg_set_module_init_data(w);
599 ret = skl_init_module(ctx, mconfig);
601 skl_put_pvt_id(ctx, uuid_mod, &mconfig->id.pvt_id);
604 skl_tplg_alloc_pipe_mcps(skl, mconfig);
605 ret = skl_tplg_set_module_params(w, ctx);
613 static int skl_tplg_unload_pipe_modules(struct skl_sst *ctx,
614 struct skl_pipe *pipe)
617 struct skl_pipe_module *w_module = NULL;
618 struct skl_module_cfg *mconfig = NULL;
620 list_for_each_entry(w_module, &pipe->w_list, node) {
622 mconfig = w_module->w->priv;
623 uuid_mod = (uuid_le *)mconfig->guid;
625 if (mconfig->is_loadable && ctx->dsp->fw_ops.unload_mod &&
626 mconfig->m_state > SKL_MODULE_UNINIT) {
627 ret = ctx->dsp->fw_ops.unload_mod(ctx->dsp,
628 mconfig->id.module_id);
632 skl_put_pvt_id(ctx, uuid_mod, &mconfig->id.pvt_id);
635 /* no modules to unload in this path, so return */
640 * Mixer module represents a pipeline. So in the Pre-PMU event of mixer we
641 * need create the pipeline. So we do following:
642 * - check the resources
643 * - Create the pipeline
644 * - Initialize the modules in pipeline
645 * - finally bind all modules together
647 static int skl_tplg_mixer_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
651 struct skl_module_cfg *mconfig = w->priv;
652 struct skl_pipe_module *w_module;
653 struct skl_pipe *s_pipe = mconfig->pipe;
654 struct skl_module_cfg *src_module = NULL, *dst_module, *module;
655 struct skl_sst *ctx = skl->skl_sst;
656 struct skl_module_deferred_bind *modules;
658 /* check resource available */
659 if (!skl_is_pipe_mcps_avail(skl, mconfig))
662 if (!skl_is_pipe_mem_avail(skl, mconfig))
666 * Create a list of modules for pipe.
667 * This list contains modules from source to sink
669 ret = skl_create_pipeline(ctx, mconfig->pipe);
673 skl_tplg_alloc_pipe_mem(skl, mconfig);
674 skl_tplg_alloc_pipe_mcps(skl, mconfig);
676 /* Init all pipe modules from source to sink */
677 ret = skl_tplg_init_pipe_modules(skl, s_pipe);
681 /* Bind modules from source to sink */
682 list_for_each_entry(w_module, &s_pipe->w_list, node) {
683 dst_module = w_module->w->priv;
685 if (src_module == NULL) {
686 src_module = dst_module;
690 ret = skl_bind_modules(ctx, src_module, dst_module);
694 src_module = dst_module;
698 * When the destination module is initialized, check for these modules
699 * in deferred bind list. If found, bind them.
701 list_for_each_entry(w_module, &s_pipe->w_list, node) {
702 if (list_empty(&skl->bind_list))
705 list_for_each_entry(modules, &skl->bind_list, node) {
706 module = w_module->w->priv;
707 if (modules->dst == module)
708 skl_bind_modules(ctx, modules->src,
716 static int skl_fill_sink_instance_id(struct skl_sst *ctx, u32 *params,
717 int size, struct skl_module_cfg *mcfg)
721 if (mcfg->m_type == SKL_MODULE_TYPE_KPB) {
722 struct skl_kpb_params *kpb_params =
723 (struct skl_kpb_params *)params;
724 struct skl_mod_inst_map *inst = kpb_params->map;
726 for (i = 0; i < kpb_params->num_modules; i++) {
727 pvt_id = skl_get_pvt_instance_id_map(ctx, inst->mod_id,
732 inst->inst_id = pvt_id;
740 * Some modules require params to be set after the module is bound to
741 * all pins connected.
743 * The module provider initializes set_param flag for such modules and we
744 * send params after binding
746 static int skl_tplg_set_module_bind_params(struct snd_soc_dapm_widget *w,
747 struct skl_module_cfg *mcfg, struct skl_sst *ctx)
750 struct skl_module_cfg *mconfig = w->priv;
751 const struct snd_kcontrol_new *k;
752 struct soc_bytes_ext *sb;
753 struct skl_algo_data *bc;
754 struct skl_specific_cfg *sp_cfg;
758 * check all out/in pins are in bind state.
759 * if so set the module param
761 for (i = 0; i < mcfg->max_out_queue; i++) {
762 if (mcfg->m_out_pin[i].pin_state != SKL_PIN_BIND_DONE)
766 for (i = 0; i < mcfg->max_in_queue; i++) {
767 if (mcfg->m_in_pin[i].pin_state != SKL_PIN_BIND_DONE)
771 if (mconfig->formats_config.caps_size > 0 &&
772 mconfig->formats_config.set_params == SKL_PARAM_BIND) {
773 sp_cfg = &mconfig->formats_config;
774 ret = skl_set_module_params(ctx, sp_cfg->caps,
776 sp_cfg->param_id, mconfig);
781 for (i = 0; i < w->num_kcontrols; i++) {
782 k = &w->kcontrol_news[i];
783 if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
784 sb = (void *) k->private_value;
785 bc = (struct skl_algo_data *)sb->dobj.private;
787 if (bc->set_params == SKL_PARAM_BIND) {
788 params = kzalloc(bc->max, GFP_KERNEL);
792 memcpy(params, bc->params, bc->max);
793 skl_fill_sink_instance_id(ctx, params, bc->max,
796 ret = skl_set_module_params(ctx, params,
797 bc->max, bc->param_id, mconfig);
810 static int skl_tplg_module_add_deferred_bind(struct skl *skl,
811 struct skl_module_cfg *src, struct skl_module_cfg *dst)
813 struct skl_module_deferred_bind *m_list, *modules;
816 /* only supported for module with static pin connection */
817 for (i = 0; i < dst->max_in_queue; i++) {
818 struct skl_module_pin *pin = &dst->m_in_pin[i];
823 if ((pin->id.module_id == src->id.module_id) &&
824 (pin->id.instance_id == src->id.instance_id)) {
826 if (!list_empty(&skl->bind_list)) {
827 list_for_each_entry(modules, &skl->bind_list, node) {
828 if (modules->src == src && modules->dst == dst)
833 m_list = kzalloc(sizeof(*m_list), GFP_KERNEL);
840 list_add(&m_list->node, &skl->bind_list);
847 static int skl_tplg_bind_sinks(struct snd_soc_dapm_widget *w,
849 struct snd_soc_dapm_widget *src_w,
850 struct skl_module_cfg *src_mconfig)
852 struct snd_soc_dapm_path *p;
853 struct snd_soc_dapm_widget *sink = NULL, *next_sink = NULL;
854 struct skl_module_cfg *sink_mconfig;
855 struct skl_sst *ctx = skl->skl_sst;
858 snd_soc_dapm_widget_for_each_sink_path(w, p) {
862 dev_dbg(ctx->dev, "%s: src widget=%s\n", __func__, w->name);
863 dev_dbg(ctx->dev, "%s: sink widget=%s\n", __func__, p->sink->name);
867 if (!is_skl_dsp_widget_type(p->sink))
868 return skl_tplg_bind_sinks(p->sink, skl, src_w, src_mconfig);
871 * here we will check widgets in sink pipelines, so that
872 * can be any widgets type and we are only interested if
873 * they are ones used for SKL so check that first
875 if ((p->sink->priv != NULL) &&
876 is_skl_dsp_widget_type(p->sink)) {
879 sink_mconfig = sink->priv;
882 * Modules other than PGA leaf can be connected
883 * directly or via switch to a module in another
884 * pipeline. EX: reference path
885 * when the path is enabled, the dst module that needs
886 * to be bound may not be initialized. if the module is
887 * not initialized, add these modules in the deferred
888 * bind list and when the dst module is initialised,
889 * bind this module to the dst_module in deferred list.
891 if (((src_mconfig->m_state == SKL_MODULE_INIT_DONE)
892 && (sink_mconfig->m_state == SKL_MODULE_UNINIT))) {
894 ret = skl_tplg_module_add_deferred_bind(skl,
895 src_mconfig, sink_mconfig);
903 if (src_mconfig->m_state == SKL_MODULE_UNINIT ||
904 sink_mconfig->m_state == SKL_MODULE_UNINIT)
907 /* Bind source to sink, mixin is always source */
908 ret = skl_bind_modules(ctx, src_mconfig, sink_mconfig);
912 /* set module params after bind */
913 skl_tplg_set_module_bind_params(src_w, src_mconfig, ctx);
914 skl_tplg_set_module_bind_params(sink, sink_mconfig, ctx);
916 /* Start sinks pipe first */
917 if (sink_mconfig->pipe->state != SKL_PIPE_STARTED) {
918 if (sink_mconfig->pipe->conn_type !=
919 SKL_PIPE_CONN_TYPE_FE)
920 ret = skl_run_pipe(ctx,
929 return skl_tplg_bind_sinks(next_sink, skl, src_w, src_mconfig);
935 * A PGA represents a module in a pipeline. So in the Pre-PMU event of PGA
936 * we need to do following:
937 * - Bind to sink pipeline
938 * Since the sink pipes can be running and we don't get mixer event on
939 * connect for already running mixer, we need to find the sink pipes
940 * here and bind to them. This way dynamic connect works.
941 * - Start sink pipeline, if not running
942 * - Then run current pipe
944 static int skl_tplg_pga_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
947 struct skl_module_cfg *src_mconfig;
948 struct skl_sst *ctx = skl->skl_sst;
951 src_mconfig = w->priv;
954 * find which sink it is connected to, bind with the sink,
955 * if sink is not started, start sink pipe first, then start
958 ret = skl_tplg_bind_sinks(w, skl, w, src_mconfig);
962 /* Start source pipe last after starting all sinks */
963 if (src_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
964 return skl_run_pipe(ctx, src_mconfig->pipe);
969 static struct snd_soc_dapm_widget *skl_get_src_dsp_widget(
970 struct snd_soc_dapm_widget *w, struct skl *skl)
972 struct snd_soc_dapm_path *p;
973 struct snd_soc_dapm_widget *src_w = NULL;
974 struct skl_sst *ctx = skl->skl_sst;
976 snd_soc_dapm_widget_for_each_source_path(w, p) {
981 dev_dbg(ctx->dev, "sink widget=%s\n", w->name);
982 dev_dbg(ctx->dev, "src widget=%s\n", p->source->name);
985 * here we will check widgets in sink pipelines, so that can
986 * be any widgets type and we are only interested if they are
987 * ones used for SKL so check that first
989 if ((p->source->priv != NULL) &&
990 is_skl_dsp_widget_type(p->source)) {
996 return skl_get_src_dsp_widget(src_w, skl);
1002 * in the Post-PMU event of mixer we need to do following:
1003 * - Check if this pipe is running
1005 * - bind this pipeline to its source pipeline
1006 * if source pipe is already running, this means it is a dynamic
1007 * connection and we need to bind only to that pipe
1008 * - start this pipeline
1010 static int skl_tplg_mixer_dapm_post_pmu_event(struct snd_soc_dapm_widget *w,
1014 struct snd_soc_dapm_widget *source, *sink;
1015 struct skl_module_cfg *src_mconfig, *sink_mconfig;
1016 struct skl_sst *ctx = skl->skl_sst;
1017 int src_pipe_started = 0;
1020 sink_mconfig = sink->priv;
1023 * If source pipe is already started, that means source is driving
1024 * one more sink before this sink got connected, Since source is
1025 * started, bind this sink to source and start this pipe.
1027 source = skl_get_src_dsp_widget(w, skl);
1028 if (source != NULL) {
1029 src_mconfig = source->priv;
1030 sink_mconfig = sink->priv;
1031 src_pipe_started = 1;
1034 * check pipe state, then no need to bind or start the
1037 if (src_mconfig->pipe->state != SKL_PIPE_STARTED)
1038 src_pipe_started = 0;
1041 if (src_pipe_started) {
1042 ret = skl_bind_modules(ctx, src_mconfig, sink_mconfig);
1046 /* set module params after bind */
1047 skl_tplg_set_module_bind_params(source, src_mconfig, ctx);
1048 skl_tplg_set_module_bind_params(sink, sink_mconfig, ctx);
1050 if (sink_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
1051 ret = skl_run_pipe(ctx, sink_mconfig->pipe);
1058 * in the Pre-PMD event of mixer we need to do following:
1060 * - find the source connections and remove that from dapm_path_list
1061 * - unbind with source pipelines if still connected
1063 static int skl_tplg_mixer_dapm_pre_pmd_event(struct snd_soc_dapm_widget *w,
1066 struct skl_module_cfg *src_mconfig, *sink_mconfig;
1068 struct skl_sst *ctx = skl->skl_sst;
1070 sink_mconfig = w->priv;
1073 ret = skl_stop_pipe(ctx, sink_mconfig->pipe);
1077 for (i = 0; i < sink_mconfig->max_in_queue; i++) {
1078 if (sink_mconfig->m_in_pin[i].pin_state == SKL_PIN_BIND_DONE) {
1079 src_mconfig = sink_mconfig->m_in_pin[i].tgt_mcfg;
1083 ret = skl_unbind_modules(ctx,
1084 src_mconfig, sink_mconfig);
1092 * in the Post-PMD event of mixer we need to do following:
1093 * - Free the mcps used
1094 * - Free the mem used
1095 * - Unbind the modules within the pipeline
1096 * - Delete the pipeline (modules are not required to be explicitly
1097 * deleted, pipeline delete is enough here
1099 static int skl_tplg_mixer_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
1102 struct skl_module_cfg *mconfig = w->priv;
1103 struct skl_pipe_module *w_module;
1104 struct skl_module_cfg *src_module = NULL, *dst_module;
1105 struct skl_sst *ctx = skl->skl_sst;
1106 struct skl_pipe *s_pipe = mconfig->pipe;
1107 struct skl_module_deferred_bind *modules, *tmp;
1109 if (s_pipe->state == SKL_PIPE_INVALID)
1112 skl_tplg_free_pipe_mcps(skl, mconfig);
1113 skl_tplg_free_pipe_mem(skl, mconfig);
1115 list_for_each_entry(w_module, &s_pipe->w_list, node) {
1116 if (list_empty(&skl->bind_list))
1119 src_module = w_module->w->priv;
1121 list_for_each_entry_safe(modules, tmp, &skl->bind_list, node) {
1123 * When the destination module is deleted, Unbind the
1124 * modules from deferred bind list.
1126 if (modules->dst == src_module) {
1127 skl_unbind_modules(ctx, modules->src,
1132 * When the source module is deleted, remove this entry
1133 * from the deferred bind list.
1135 if (modules->src == src_module) {
1136 list_del(&modules->node);
1137 modules->src = NULL;
1138 modules->dst = NULL;
1144 list_for_each_entry(w_module, &s_pipe->w_list, node) {
1145 dst_module = w_module->w->priv;
1147 if (mconfig->m_state >= SKL_MODULE_INIT_DONE)
1148 skl_tplg_free_pipe_mcps(skl, dst_module);
1149 if (src_module == NULL) {
1150 src_module = dst_module;
1154 skl_unbind_modules(ctx, src_module, dst_module);
1155 src_module = dst_module;
1158 skl_delete_pipe(ctx, mconfig->pipe);
1160 list_for_each_entry(w_module, &s_pipe->w_list, node) {
1161 src_module = w_module->w->priv;
1162 src_module->m_state = SKL_MODULE_UNINIT;
1165 return skl_tplg_unload_pipe_modules(ctx, s_pipe);
1169 * in the Post-PMD event of PGA we need to do following:
1170 * - Free the mcps used
1171 * - Stop the pipeline
1172 * - In source pipe is connected, unbind with source pipelines
1174 static int skl_tplg_pga_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
1177 struct skl_module_cfg *src_mconfig, *sink_mconfig;
1179 struct skl_sst *ctx = skl->skl_sst;
1181 src_mconfig = w->priv;
1183 /* Stop the pipe since this is a mixin module */
1184 ret = skl_stop_pipe(ctx, src_mconfig->pipe);
1188 for (i = 0; i < src_mconfig->max_out_queue; i++) {
1189 if (src_mconfig->m_out_pin[i].pin_state == SKL_PIN_BIND_DONE) {
1190 sink_mconfig = src_mconfig->m_out_pin[i].tgt_mcfg;
1194 * This is a connecter and if path is found that means
1195 * unbind between source and sink has not happened yet
1197 ret = skl_unbind_modules(ctx, src_mconfig,
1206 * In modelling, we assume there will be ONLY one mixer in a pipeline. If a
1207 * second one is required that is created as another pipe entity.
1208 * The mixer is responsible for pipe management and represent a pipeline
1211 static int skl_tplg_mixer_event(struct snd_soc_dapm_widget *w,
1212 struct snd_kcontrol *k, int event)
1214 struct snd_soc_dapm_context *dapm = w->dapm;
1215 struct skl *skl = get_skl_ctx(dapm->dev);
1218 case SND_SOC_DAPM_PRE_PMU:
1219 return skl_tplg_mixer_dapm_pre_pmu_event(w, skl);
1221 case SND_SOC_DAPM_POST_PMU:
1222 return skl_tplg_mixer_dapm_post_pmu_event(w, skl);
1224 case SND_SOC_DAPM_PRE_PMD:
1225 return skl_tplg_mixer_dapm_pre_pmd_event(w, skl);
1227 case SND_SOC_DAPM_POST_PMD:
1228 return skl_tplg_mixer_dapm_post_pmd_event(w, skl);
1235 * In modelling, we assumed rest of the modules in pipeline are PGA. But we
1236 * are interested in last PGA (leaf PGA) in a pipeline to disconnect with
1237 * the sink when it is running (two FE to one BE or one FE to two BE)
1240 static int skl_tplg_pga_event(struct snd_soc_dapm_widget *w,
1241 struct snd_kcontrol *k, int event)
1244 struct snd_soc_dapm_context *dapm = w->dapm;
1245 struct skl *skl = get_skl_ctx(dapm->dev);
1248 case SND_SOC_DAPM_PRE_PMU:
1249 return skl_tplg_pga_dapm_pre_pmu_event(w, skl);
1251 case SND_SOC_DAPM_POST_PMD:
1252 return skl_tplg_pga_dapm_post_pmd_event(w, skl);
1258 static int skl_tplg_tlv_control_get(struct snd_kcontrol *kcontrol,
1259 unsigned int __user *data, unsigned int size)
1261 struct soc_bytes_ext *sb =
1262 (struct soc_bytes_ext *)kcontrol->private_value;
1263 struct skl_algo_data *bc = (struct skl_algo_data *)sb->dobj.private;
1264 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1265 struct skl_module_cfg *mconfig = w->priv;
1266 struct skl *skl = get_skl_ctx(w->dapm->dev);
1269 skl_get_module_params(skl->skl_sst, (u32 *)bc->params,
1270 bc->size, bc->param_id, mconfig);
1272 /* decrement size for TLV header */
1273 size -= 2 * sizeof(u32);
1275 /* check size as we don't want to send kernel data */
1280 if (copy_to_user(data, &bc->param_id, sizeof(u32)))
1282 if (copy_to_user(data + 1, &size, sizeof(u32)))
1284 if (copy_to_user(data + 2, bc->params, size))
1291 #define SKL_PARAM_VENDOR_ID 0xff
1293 static int skl_tplg_tlv_control_set(struct snd_kcontrol *kcontrol,
1294 const unsigned int __user *data, unsigned int size)
1296 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1297 struct skl_module_cfg *mconfig = w->priv;
1298 struct soc_bytes_ext *sb =
1299 (struct soc_bytes_ext *)kcontrol->private_value;
1300 struct skl_algo_data *ac = (struct skl_algo_data *)sb->dobj.private;
1301 struct skl *skl = get_skl_ctx(w->dapm->dev);
1309 * if the param_is is of type Vendor, firmware expects actual
1310 * parameter id and size from the control.
1312 if (ac->param_id == SKL_PARAM_VENDOR_ID) {
1313 if (copy_from_user(ac->params, data, size))
1316 if (copy_from_user(ac->params,
1322 return skl_set_module_params(skl->skl_sst,
1323 (u32 *)ac->params, ac->size,
1324 ac->param_id, mconfig);
1330 static int skl_tplg_mic_control_get(struct snd_kcontrol *kcontrol,
1331 struct snd_ctl_elem_value *ucontrol)
1333 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1334 struct skl_module_cfg *mconfig = w->priv;
1335 struct soc_enum *ec = (struct soc_enum *)kcontrol->private_value;
1336 u32 ch_type = *((u32 *)ec->dobj.private);
1338 if (mconfig->dmic_ch_type == ch_type)
1339 ucontrol->value.enumerated.item[0] =
1340 mconfig->dmic_ch_combo_index;
1342 ucontrol->value.enumerated.item[0] = 0;
1347 static int skl_fill_mic_sel_params(struct skl_module_cfg *mconfig,
1348 struct skl_mic_sel_config *mic_cfg, struct device *dev)
1350 struct skl_specific_cfg *sp_cfg = &mconfig->formats_config;
1352 sp_cfg->caps_size = sizeof(struct skl_mic_sel_config);
1353 sp_cfg->set_params = SKL_PARAM_SET;
1354 sp_cfg->param_id = 0x00;
1355 if (!sp_cfg->caps) {
1356 sp_cfg->caps = devm_kzalloc(dev, sp_cfg->caps_size, GFP_KERNEL);
1361 mic_cfg->mic_switch = SKL_MIC_SEL_SWITCH;
1363 memcpy(sp_cfg->caps, mic_cfg, sp_cfg->caps_size);
1368 static int skl_tplg_mic_control_set(struct snd_kcontrol *kcontrol,
1369 struct snd_ctl_elem_value *ucontrol)
1371 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1372 struct skl_module_cfg *mconfig = w->priv;
1373 struct skl_mic_sel_config mic_cfg = {0};
1374 struct soc_enum *ec = (struct soc_enum *)kcontrol->private_value;
1375 u32 ch_type = *((u32 *)ec->dobj.private);
1377 u8 in_ch, out_ch, index;
1379 mconfig->dmic_ch_type = ch_type;
1380 mconfig->dmic_ch_combo_index = ucontrol->value.enumerated.item[0];
1382 /* enum control index 0 is INVALID, so no channels to be set */
1383 if (mconfig->dmic_ch_combo_index == 0)
1386 /* No valid channel selection map for index 0, so offset by 1 */
1387 index = mconfig->dmic_ch_combo_index - 1;
1391 if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_mono_list))
1394 list = &mic_mono_list[index];
1398 if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_stereo_list))
1401 list = mic_stereo_list[index];
1405 if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_trio_list))
1408 list = mic_trio_list[index];
1412 if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_quatro_list))
1415 list = mic_quatro_list[index];
1419 dev_err(w->dapm->dev,
1420 "Invalid channel %d for mic_select module\n",
1426 /* channel type enum map to number of chanels for that type */
1427 for (out_ch = 0; out_ch < ch_type; out_ch++) {
1428 in_ch = list[out_ch];
1429 mic_cfg.blob[out_ch][in_ch] = SKL_DEFAULT_MIC_SEL_GAIN;
1432 return skl_fill_mic_sel_params(mconfig, &mic_cfg, w->dapm->dev);
1436 * Fill the dma id for host and link. In case of passthrough
1437 * pipeline, this will both host and link in the same
1438 * pipeline, so need to copy the link and host based on dev_type
1440 static void skl_tplg_fill_dma_id(struct skl_module_cfg *mcfg,
1441 struct skl_pipe_params *params)
1443 struct skl_pipe *pipe = mcfg->pipe;
1445 if (pipe->passthru) {
1446 switch (mcfg->dev_type) {
1447 case SKL_DEVICE_HDALINK:
1448 pipe->p_params->link_dma_id = params->link_dma_id;
1449 pipe->p_params->link_index = params->link_index;
1450 pipe->p_params->link_bps = params->link_bps;
1453 case SKL_DEVICE_HDAHOST:
1454 pipe->p_params->host_dma_id = params->host_dma_id;
1455 pipe->p_params->host_bps = params->host_bps;
1461 pipe->p_params->s_fmt = params->s_fmt;
1462 pipe->p_params->ch = params->ch;
1463 pipe->p_params->s_freq = params->s_freq;
1464 pipe->p_params->stream = params->stream;
1465 pipe->p_params->format = params->format;
1468 memcpy(pipe->p_params, params, sizeof(*params));
1473 * The FE params are passed by hw_params of the DAI.
1474 * On hw_params, the params are stored in Gateway module of the FE and we
1475 * need to calculate the format in DSP module configuration, that
1476 * conversion is done here
1478 int skl_tplg_update_pipe_params(struct device *dev,
1479 struct skl_module_cfg *mconfig,
1480 struct skl_pipe_params *params)
1482 struct skl_module_fmt *format = NULL;
1484 skl_tplg_fill_dma_id(mconfig, params);
1486 if (params->stream == SNDRV_PCM_STREAM_PLAYBACK)
1487 format = &mconfig->in_fmt[0];
1489 format = &mconfig->out_fmt[0];
1491 /* set the hw_params */
1492 format->s_freq = params->s_freq;
1493 format->channels = params->ch;
1494 format->valid_bit_depth = skl_get_bit_depth(params->s_fmt);
1497 * 16 bit is 16 bit container whereas 24 bit is in 32 bit
1498 * container so update bit depth accordingly
1500 switch (format->valid_bit_depth) {
1501 case SKL_DEPTH_16BIT:
1502 format->bit_depth = format->valid_bit_depth;
1505 case SKL_DEPTH_24BIT:
1506 case SKL_DEPTH_32BIT:
1507 format->bit_depth = SKL_DEPTH_32BIT;
1511 dev_err(dev, "Invalid bit depth %x for pipe\n",
1512 format->valid_bit_depth);
1516 if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1517 mconfig->ibs = (format->s_freq / 1000) *
1518 (format->channels) *
1519 (format->bit_depth >> 3);
1521 mconfig->obs = (format->s_freq / 1000) *
1522 (format->channels) *
1523 (format->bit_depth >> 3);
1530 * Query the module config for the FE DAI
1531 * This is used to find the hw_params set for that DAI and apply to FE
1534 struct skl_module_cfg *
1535 skl_tplg_fe_get_cpr_module(struct snd_soc_dai *dai, int stream)
1537 struct snd_soc_dapm_widget *w;
1538 struct snd_soc_dapm_path *p = NULL;
1540 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1541 w = dai->playback_widget;
1542 snd_soc_dapm_widget_for_each_sink_path(w, p) {
1543 if (p->connect && p->sink->power &&
1544 !is_skl_dsp_widget_type(p->sink))
1547 if (p->sink->priv) {
1548 dev_dbg(dai->dev, "set params for %s\n",
1550 return p->sink->priv;
1554 w = dai->capture_widget;
1555 snd_soc_dapm_widget_for_each_source_path(w, p) {
1556 if (p->connect && p->source->power &&
1557 !is_skl_dsp_widget_type(p->source))
1560 if (p->source->priv) {
1561 dev_dbg(dai->dev, "set params for %s\n",
1563 return p->source->priv;
1571 static struct skl_module_cfg *skl_get_mconfig_pb_cpr(
1572 struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
1574 struct snd_soc_dapm_path *p;
1575 struct skl_module_cfg *mconfig = NULL;
1577 snd_soc_dapm_widget_for_each_source_path(w, p) {
1578 if (w->endpoints[SND_SOC_DAPM_DIR_OUT] > 0) {
1580 (p->sink->id == snd_soc_dapm_aif_out) &&
1582 mconfig = p->source->priv;
1585 mconfig = skl_get_mconfig_pb_cpr(dai, p->source);
1593 static struct skl_module_cfg *skl_get_mconfig_cap_cpr(
1594 struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
1596 struct snd_soc_dapm_path *p;
1597 struct skl_module_cfg *mconfig = NULL;
1599 snd_soc_dapm_widget_for_each_sink_path(w, p) {
1600 if (w->endpoints[SND_SOC_DAPM_DIR_IN] > 0) {
1602 (p->source->id == snd_soc_dapm_aif_in) &&
1604 mconfig = p->sink->priv;
1607 mconfig = skl_get_mconfig_cap_cpr(dai, p->sink);
1615 struct skl_module_cfg *
1616 skl_tplg_be_get_cpr_module(struct snd_soc_dai *dai, int stream)
1618 struct snd_soc_dapm_widget *w;
1619 struct skl_module_cfg *mconfig;
1621 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1622 w = dai->playback_widget;
1623 mconfig = skl_get_mconfig_pb_cpr(dai, w);
1625 w = dai->capture_widget;
1626 mconfig = skl_get_mconfig_cap_cpr(dai, w);
1631 static u8 skl_tplg_be_link_type(int dev_type)
1637 ret = NHLT_LINK_SSP;
1640 case SKL_DEVICE_DMIC:
1641 ret = NHLT_LINK_DMIC;
1644 case SKL_DEVICE_I2S:
1645 ret = NHLT_LINK_SSP;
1648 case SKL_DEVICE_HDALINK:
1649 ret = NHLT_LINK_HDA;
1653 ret = NHLT_LINK_INVALID;
1661 * Fill the BE gateway parameters
1662 * The BE gateway expects a blob of parameters which are kept in the ACPI
1663 * NHLT blob, so query the blob for interface type (i2s/pdm) and instance.
1664 * The port can have multiple settings so pick based on the PCM
1667 static int skl_tplg_be_fill_pipe_params(struct snd_soc_dai *dai,
1668 struct skl_module_cfg *mconfig,
1669 struct skl_pipe_params *params)
1671 struct nhlt_specific_cfg *cfg;
1672 struct skl *skl = get_skl_ctx(dai->dev);
1673 int link_type = skl_tplg_be_link_type(mconfig->dev_type);
1674 u8 dev_type = skl_tplg_be_dev_type(mconfig->dev_type);
1676 skl_tplg_fill_dma_id(mconfig, params);
1678 if (link_type == NHLT_LINK_HDA)
1681 /* update the blob based on virtual bus_id*/
1682 cfg = skl_get_ep_blob(skl, mconfig->vbus_id, link_type,
1683 params->s_fmt, params->ch,
1684 params->s_freq, params->stream,
1687 mconfig->formats_config.caps_size = cfg->size;
1688 mconfig->formats_config.caps = (u32 *) &cfg->caps;
1690 dev_err(dai->dev, "Blob NULL for id %x type %d dirn %d\n",
1691 mconfig->vbus_id, link_type,
1693 dev_err(dai->dev, "PCM: ch %d, freq %d, fmt %d\n",
1694 params->ch, params->s_freq, params->s_fmt);
1701 static int skl_tplg_be_set_src_pipe_params(struct snd_soc_dai *dai,
1702 struct snd_soc_dapm_widget *w,
1703 struct skl_pipe_params *params)
1705 struct snd_soc_dapm_path *p;
1708 snd_soc_dapm_widget_for_each_source_path(w, p) {
1709 if (p->connect && is_skl_dsp_widget_type(p->source) &&
1712 ret = skl_tplg_be_fill_pipe_params(dai,
1713 p->source->priv, params);
1717 ret = skl_tplg_be_set_src_pipe_params(dai,
1727 static int skl_tplg_be_set_sink_pipe_params(struct snd_soc_dai *dai,
1728 struct snd_soc_dapm_widget *w, struct skl_pipe_params *params)
1730 struct snd_soc_dapm_path *p = NULL;
1733 snd_soc_dapm_widget_for_each_sink_path(w, p) {
1734 if (p->connect && is_skl_dsp_widget_type(p->sink) &&
1737 ret = skl_tplg_be_fill_pipe_params(dai,
1738 p->sink->priv, params);
1742 ret = skl_tplg_be_set_sink_pipe_params(
1743 dai, p->sink, params);
1753 * BE hw_params can be a source parameters (capture) or sink parameters
1754 * (playback). Based on sink and source we need to either find the source
1755 * list or the sink list and set the pipeline parameters
1757 int skl_tplg_be_update_params(struct snd_soc_dai *dai,
1758 struct skl_pipe_params *params)
1760 struct snd_soc_dapm_widget *w;
1762 if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1763 w = dai->playback_widget;
1765 return skl_tplg_be_set_src_pipe_params(dai, w, params);
1768 w = dai->capture_widget;
1770 return skl_tplg_be_set_sink_pipe_params(dai, w, params);
1776 static const struct snd_soc_tplg_widget_events skl_tplg_widget_ops[] = {
1777 {SKL_MIXER_EVENT, skl_tplg_mixer_event},
1778 {SKL_VMIXER_EVENT, skl_tplg_mixer_event},
1779 {SKL_PGA_EVENT, skl_tplg_pga_event},
1782 static const struct snd_soc_tplg_bytes_ext_ops skl_tlv_ops[] = {
1783 {SKL_CONTROL_TYPE_BYTE_TLV, skl_tplg_tlv_control_get,
1784 skl_tplg_tlv_control_set},
1787 static const struct snd_soc_tplg_kcontrol_ops skl_tplg_kcontrol_ops[] = {
1789 .id = SKL_CONTROL_TYPE_MIC_SELECT,
1790 .get = skl_tplg_mic_control_get,
1791 .put = skl_tplg_mic_control_set,
1795 static int skl_tplg_fill_pipe_tkn(struct device *dev,
1796 struct skl_pipe *pipe, u32 tkn,
1801 case SKL_TKN_U32_PIPE_CONN_TYPE:
1802 pipe->conn_type = tkn_val;
1805 case SKL_TKN_U32_PIPE_PRIORITY:
1806 pipe->pipe_priority = tkn_val;
1809 case SKL_TKN_U32_PIPE_MEM_PGS:
1810 pipe->memory_pages = tkn_val;
1813 case SKL_TKN_U32_PMODE:
1814 pipe->lp_mode = tkn_val;
1818 dev_err(dev, "Token not handled %d\n", tkn);
1826 * Add pipeline by parsing the relevant tokens
1827 * Return an existing pipe if the pipe already exists.
1829 static int skl_tplg_add_pipe(struct device *dev,
1830 struct skl_module_cfg *mconfig, struct skl *skl,
1831 struct snd_soc_tplg_vendor_value_elem *tkn_elem)
1833 struct skl_pipeline *ppl;
1834 struct skl_pipe *pipe;
1835 struct skl_pipe_params *params;
1837 list_for_each_entry(ppl, &skl->ppl_list, node) {
1838 if (ppl->pipe->ppl_id == tkn_elem->value) {
1839 mconfig->pipe = ppl->pipe;
1844 ppl = devm_kzalloc(dev, sizeof(*ppl), GFP_KERNEL);
1848 pipe = devm_kzalloc(dev, sizeof(*pipe), GFP_KERNEL);
1852 params = devm_kzalloc(dev, sizeof(*params), GFP_KERNEL);
1856 pipe->p_params = params;
1857 pipe->ppl_id = tkn_elem->value;
1858 INIT_LIST_HEAD(&pipe->w_list);
1861 list_add(&ppl->node, &skl->ppl_list);
1863 mconfig->pipe = pipe;
1864 mconfig->pipe->state = SKL_PIPE_INVALID;
1869 static int skl_tplg_fill_pin(struct device *dev, u32 tkn,
1870 struct skl_module_pin *m_pin,
1871 int pin_index, u32 value)
1874 case SKL_TKN_U32_PIN_MOD_ID:
1875 m_pin[pin_index].id.module_id = value;
1878 case SKL_TKN_U32_PIN_INST_ID:
1879 m_pin[pin_index].id.instance_id = value;
1883 dev_err(dev, "%d Not a pin token\n", value);
1891 * Parse for pin config specific tokens to fill up the
1892 * module private data
1894 static int skl_tplg_fill_pins_info(struct device *dev,
1895 struct skl_module_cfg *mconfig,
1896 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
1897 int dir, int pin_count)
1900 struct skl_module_pin *m_pin;
1904 m_pin = mconfig->m_in_pin;
1908 m_pin = mconfig->m_out_pin;
1912 dev_err(dev, "Invalid direction value\n");
1916 ret = skl_tplg_fill_pin(dev, tkn_elem->token,
1917 m_pin, pin_count, tkn_elem->value);
1922 m_pin[pin_count].in_use = false;
1923 m_pin[pin_count].pin_state = SKL_PIN_UNBIND;
1929 * Fill up input/output module config format based
1932 static int skl_tplg_fill_fmt(struct device *dev,
1933 struct skl_module_cfg *mconfig, u32 tkn,
1934 u32 value, u32 dir, u32 pin_count)
1936 struct skl_module_fmt *dst_fmt;
1940 dst_fmt = mconfig->in_fmt;
1941 dst_fmt += pin_count;
1945 dst_fmt = mconfig->out_fmt;
1946 dst_fmt += pin_count;
1950 dev_err(dev, "Invalid direction value\n");
1955 case SKL_TKN_U32_FMT_CH:
1956 dst_fmt->channels = value;
1959 case SKL_TKN_U32_FMT_FREQ:
1960 dst_fmt->s_freq = value;
1963 case SKL_TKN_U32_FMT_BIT_DEPTH:
1964 dst_fmt->bit_depth = value;
1967 case SKL_TKN_U32_FMT_SAMPLE_SIZE:
1968 dst_fmt->valid_bit_depth = value;
1971 case SKL_TKN_U32_FMT_CH_CONFIG:
1972 dst_fmt->ch_cfg = value;
1975 case SKL_TKN_U32_FMT_INTERLEAVE:
1976 dst_fmt->interleaving_style = value;
1979 case SKL_TKN_U32_FMT_SAMPLE_TYPE:
1980 dst_fmt->sample_type = value;
1983 case SKL_TKN_U32_FMT_CH_MAP:
1984 dst_fmt->ch_map = value;
1988 dev_err(dev, "Invalid token %d\n", tkn);
1995 static int skl_tplg_get_uuid(struct device *dev, struct skl_module_cfg *mconfig,
1996 struct snd_soc_tplg_vendor_uuid_elem *uuid_tkn)
1998 if (uuid_tkn->token == SKL_TKN_UUID)
1999 memcpy(&mconfig->guid, &uuid_tkn->uuid, 16);
2001 dev_err(dev, "Not an UUID token tkn %d\n", uuid_tkn->token);
2008 static void skl_tplg_fill_pin_dynamic_val(
2009 struct skl_module_pin *mpin, u32 pin_count, u32 value)
2013 for (i = 0; i < pin_count; i++)
2014 mpin[i].is_dynamic = value;
2018 * Parse tokens to fill up the module private data
2020 static int skl_tplg_get_token(struct device *dev,
2021 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2022 struct skl *skl, struct skl_module_cfg *mconfig)
2026 static int is_pipe_exists;
2027 static int pin_index, dir;
2029 if (tkn_elem->token > SKL_TKN_MAX)
2032 switch (tkn_elem->token) {
2033 case SKL_TKN_U8_IN_QUEUE_COUNT:
2034 mconfig->max_in_queue = tkn_elem->value;
2035 mconfig->m_in_pin = devm_kzalloc(dev, mconfig->max_in_queue *
2036 sizeof(*mconfig->m_in_pin),
2038 if (!mconfig->m_in_pin)
2043 case SKL_TKN_U8_OUT_QUEUE_COUNT:
2044 mconfig->max_out_queue = tkn_elem->value;
2045 mconfig->m_out_pin = devm_kzalloc(dev, mconfig->max_out_queue *
2046 sizeof(*mconfig->m_out_pin),
2049 if (!mconfig->m_out_pin)
2054 case SKL_TKN_U8_DYN_IN_PIN:
2055 if (!mconfig->m_in_pin)
2058 skl_tplg_fill_pin_dynamic_val(mconfig->m_in_pin,
2059 mconfig->max_in_queue, tkn_elem->value);
2063 case SKL_TKN_U8_DYN_OUT_PIN:
2064 if (!mconfig->m_out_pin)
2067 skl_tplg_fill_pin_dynamic_val(mconfig->m_out_pin,
2068 mconfig->max_out_queue, tkn_elem->value);
2072 case SKL_TKN_U8_TIME_SLOT:
2073 mconfig->time_slot = tkn_elem->value;
2076 case SKL_TKN_U8_CORE_ID:
2077 mconfig->core_id = tkn_elem->value;
2079 case SKL_TKN_U8_MOD_TYPE:
2080 mconfig->m_type = tkn_elem->value;
2083 case SKL_TKN_U8_DEV_TYPE:
2084 mconfig->dev_type = tkn_elem->value;
2087 case SKL_TKN_U8_HW_CONN_TYPE:
2088 mconfig->hw_conn_type = tkn_elem->value;
2091 case SKL_TKN_U16_MOD_INST_ID:
2092 mconfig->id.instance_id =
2096 case SKL_TKN_U32_MEM_PAGES:
2097 mconfig->mem_pages = tkn_elem->value;
2100 case SKL_TKN_U32_MAX_MCPS:
2101 mconfig->mcps = tkn_elem->value;
2104 case SKL_TKN_U32_OBS:
2105 mconfig->obs = tkn_elem->value;
2108 case SKL_TKN_U32_IBS:
2109 mconfig->ibs = tkn_elem->value;
2112 case SKL_TKN_U32_VBUS_ID:
2113 mconfig->vbus_id = tkn_elem->value;
2116 case SKL_TKN_U32_PARAMS_FIXUP:
2117 mconfig->params_fixup = tkn_elem->value;
2120 case SKL_TKN_U32_CONVERTER:
2121 mconfig->converter = tkn_elem->value;
2124 case SKL_TKN_U32_D0I3_CAPS:
2125 mconfig->d0i3_caps = tkn_elem->value;
2128 case SKL_TKN_U32_PIPE_ID:
2129 ret = skl_tplg_add_pipe(dev,
2130 mconfig, skl, tkn_elem);
2133 if (ret == -EEXIST) {
2137 return is_pipe_exists;
2142 case SKL_TKN_U32_PIPE_CONN_TYPE:
2143 case SKL_TKN_U32_PIPE_PRIORITY:
2144 case SKL_TKN_U32_PIPE_MEM_PGS:
2145 case SKL_TKN_U32_PMODE:
2146 if (is_pipe_exists) {
2147 ret = skl_tplg_fill_pipe_tkn(dev, mconfig->pipe,
2148 tkn_elem->token, tkn_elem->value);
2156 * SKL_TKN_U32_DIR_PIN_COUNT token has the value for both
2157 * direction and the pin count. The first four bits represent
2158 * direction and next four the pin count.
2160 case SKL_TKN_U32_DIR_PIN_COUNT:
2161 dir = tkn_elem->value & SKL_IN_DIR_BIT_MASK;
2162 pin_index = (tkn_elem->value &
2163 SKL_PIN_COUNT_MASK) >> 4;
2167 case SKL_TKN_U32_FMT_CH:
2168 case SKL_TKN_U32_FMT_FREQ:
2169 case SKL_TKN_U32_FMT_BIT_DEPTH:
2170 case SKL_TKN_U32_FMT_SAMPLE_SIZE:
2171 case SKL_TKN_U32_FMT_CH_CONFIG:
2172 case SKL_TKN_U32_FMT_INTERLEAVE:
2173 case SKL_TKN_U32_FMT_SAMPLE_TYPE:
2174 case SKL_TKN_U32_FMT_CH_MAP:
2175 ret = skl_tplg_fill_fmt(dev, mconfig, tkn_elem->token,
2176 tkn_elem->value, dir, pin_index);
2183 case SKL_TKN_U32_PIN_MOD_ID:
2184 case SKL_TKN_U32_PIN_INST_ID:
2185 ret = skl_tplg_fill_pins_info(dev,
2186 mconfig, tkn_elem, dir,
2193 case SKL_TKN_U32_CAPS_SIZE:
2194 mconfig->formats_config.caps_size =
2199 case SKL_TKN_U32_CAPS_SET_PARAMS:
2200 mconfig->formats_config.set_params =
2204 case SKL_TKN_U32_CAPS_PARAMS_ID:
2205 mconfig->formats_config.param_id =
2209 case SKL_TKN_U32_PROC_DOMAIN:
2215 case SKL_TKN_U32_DMA_BUF_SIZE:
2216 mconfig->dma_buffer_size = tkn_elem->value;
2219 case SKL_TKN_U8_IN_PIN_TYPE:
2220 case SKL_TKN_U8_OUT_PIN_TYPE:
2221 case SKL_TKN_U8_CONN_TYPE:
2225 dev_err(dev, "Token %d not handled\n",
2236 * Parse the vendor array for specific tokens to construct
2237 * module private data
2239 static int skl_tplg_get_tokens(struct device *dev,
2240 char *pvt_data, struct skl *skl,
2241 struct skl_module_cfg *mconfig, int block_size)
2243 struct snd_soc_tplg_vendor_array *array;
2244 struct snd_soc_tplg_vendor_value_elem *tkn_elem;
2245 int tkn_count = 0, ret;
2246 int off = 0, tuple_size = 0;
2248 if (block_size <= 0)
2251 while (tuple_size < block_size) {
2252 array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off);
2256 switch (array->type) {
2257 case SND_SOC_TPLG_TUPLE_TYPE_STRING:
2258 dev_warn(dev, "no string tokens expected for skl tplg\n");
2261 case SND_SOC_TPLG_TUPLE_TYPE_UUID:
2262 ret = skl_tplg_get_uuid(dev, mconfig, array->uuid);
2266 tuple_size += sizeof(*array->uuid);
2271 tkn_elem = array->value;
2276 while (tkn_count <= (array->num_elems - 1)) {
2277 ret = skl_tplg_get_token(dev, tkn_elem,
2283 tkn_count = tkn_count + ret;
2287 tuple_size += tkn_count * sizeof(*tkn_elem);
2294 * Every data block is preceded by a descriptor to read the number
2295 * of data blocks, they type of the block and it's size
2297 static int skl_tplg_get_desc_blocks(struct device *dev,
2298 struct snd_soc_tplg_vendor_array *array)
2300 struct snd_soc_tplg_vendor_value_elem *tkn_elem;
2302 tkn_elem = array->value;
2304 switch (tkn_elem->token) {
2305 case SKL_TKN_U8_NUM_BLOCKS:
2306 case SKL_TKN_U8_BLOCK_TYPE:
2307 case SKL_TKN_U16_BLOCK_SIZE:
2308 return tkn_elem->value;
2311 dev_err(dev, "Invalid descriptor token %d\n", tkn_elem->token);
2319 * Parse the private data for the token and corresponding value.
2320 * The private data can have multiple data blocks. So, a data block
2321 * is preceded by a descriptor for number of blocks and a descriptor
2322 * for the type and size of the suceeding data block.
2324 static int skl_tplg_get_pvt_data(struct snd_soc_tplg_dapm_widget *tplg_w,
2325 struct skl *skl, struct device *dev,
2326 struct skl_module_cfg *mconfig)
2328 struct snd_soc_tplg_vendor_array *array;
2329 int num_blocks, block_size = 0, block_type, off = 0;
2333 /* Read the NUM_DATA_BLOCKS descriptor */
2334 array = (struct snd_soc_tplg_vendor_array *)tplg_w->priv.data;
2335 ret = skl_tplg_get_desc_blocks(dev, array);
2341 /* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */
2342 while (num_blocks > 0) {
2343 array = (struct snd_soc_tplg_vendor_array *)
2344 (tplg_w->priv.data + off);
2346 ret = skl_tplg_get_desc_blocks(dev, array);
2353 array = (struct snd_soc_tplg_vendor_array *)
2354 (tplg_w->priv.data + off);
2356 ret = skl_tplg_get_desc_blocks(dev, array);
2363 array = (struct snd_soc_tplg_vendor_array *)
2364 (tplg_w->priv.data + off);
2366 data = (tplg_w->priv.data + off);
2368 if (block_type == SKL_TYPE_TUPLE) {
2369 ret = skl_tplg_get_tokens(dev, data,
2370 skl, mconfig, block_size);
2377 if (mconfig->formats_config.caps_size > 0)
2378 memcpy(mconfig->formats_config.caps, data,
2379 mconfig->formats_config.caps_size);
2381 ret = mconfig->formats_config.caps_size;
2389 static void skl_clear_pin_config(struct snd_soc_platform *platform,
2390 struct snd_soc_dapm_widget *w)
2393 struct skl_module_cfg *mconfig;
2394 struct skl_pipe *pipe;
2396 if (!strncmp(w->dapm->component->name, platform->component.name,
2397 strlen(platform->component.name))) {
2399 pipe = mconfig->pipe;
2400 for (i = 0; i < mconfig->max_in_queue; i++) {
2401 mconfig->m_in_pin[i].in_use = false;
2402 mconfig->m_in_pin[i].pin_state = SKL_PIN_UNBIND;
2404 for (i = 0; i < mconfig->max_out_queue; i++) {
2405 mconfig->m_out_pin[i].in_use = false;
2406 mconfig->m_out_pin[i].pin_state = SKL_PIN_UNBIND;
2408 pipe->state = SKL_PIPE_INVALID;
2409 mconfig->m_state = SKL_MODULE_UNINIT;
2413 void skl_cleanup_resources(struct skl *skl)
2415 struct skl_sst *ctx = skl->skl_sst;
2416 struct snd_soc_platform *soc_platform = skl->platform;
2417 struct snd_soc_dapm_widget *w;
2418 struct snd_soc_card *card;
2420 if (soc_platform == NULL)
2423 card = soc_platform->component.card;
2424 if (!card || !card->instantiated)
2427 skl->resource.mem = 0;
2428 skl->resource.mcps = 0;
2430 list_for_each_entry(w, &card->widgets, list) {
2431 if (is_skl_dsp_widget_type(w) && (w->priv != NULL))
2432 skl_clear_pin_config(soc_platform, w);
2435 skl_clear_module_cnt(ctx->dsp);
2439 * Topology core widget load callback
2441 * This is used to save the private data for each widget which gives
2442 * information to the driver about module and pipeline parameters which DSP
2443 * FW expects like ids, resource values, formats etc
2445 static int skl_tplg_widget_load(struct snd_soc_component *cmpnt,
2446 struct snd_soc_dapm_widget *w,
2447 struct snd_soc_tplg_dapm_widget *tplg_w)
2450 struct hdac_ext_bus *ebus = snd_soc_component_get_drvdata(cmpnt);
2451 struct skl *skl = ebus_to_skl(ebus);
2452 struct hdac_bus *bus = ebus_to_hbus(ebus);
2453 struct skl_module_cfg *mconfig;
2455 if (!tplg_w->priv.size)
2458 mconfig = devm_kzalloc(bus->dev, sizeof(*mconfig), GFP_KERNEL);
2466 * module binary can be loaded later, so set it to query when
2467 * module is load for a use case
2469 mconfig->id.module_id = -1;
2471 /* Parse private data for tuples */
2472 ret = skl_tplg_get_pvt_data(tplg_w, skl, bus->dev, mconfig);
2476 skl_debug_init_module(skl->debugfs, w, mconfig);
2479 if (tplg_w->event_type == 0) {
2480 dev_dbg(bus->dev, "ASoC: No event handler required\n");
2484 ret = snd_soc_tplg_widget_bind_event(w, skl_tplg_widget_ops,
2485 ARRAY_SIZE(skl_tplg_widget_ops),
2486 tplg_w->event_type);
2489 dev_err(bus->dev, "%s: No matching event handlers found for %d\n",
2490 __func__, tplg_w->event_type);
2497 static int skl_init_algo_data(struct device *dev, struct soc_bytes_ext *be,
2498 struct snd_soc_tplg_bytes_control *bc)
2500 struct skl_algo_data *ac;
2501 struct skl_dfw_algo_data *dfw_ac =
2502 (struct skl_dfw_algo_data *)bc->priv.data;
2504 ac = devm_kzalloc(dev, sizeof(*ac), GFP_KERNEL);
2508 /* Fill private data */
2509 ac->max = dfw_ac->max;
2510 ac->param_id = dfw_ac->param_id;
2511 ac->set_params = dfw_ac->set_params;
2512 ac->size = dfw_ac->max;
2515 ac->params = (char *) devm_kzalloc(dev, ac->max, GFP_KERNEL);
2519 memcpy(ac->params, dfw_ac->params, ac->max);
2522 be->dobj.private = ac;
2526 static int skl_init_enum_data(struct device *dev, struct soc_enum *se,
2527 struct snd_soc_tplg_enum_control *ec)
2532 if (ec->priv.size) {
2533 data = devm_kzalloc(dev, sizeof(ec->priv.size), GFP_KERNEL);
2536 memcpy(data, ec->priv.data, ec->priv.size);
2537 se->dobj.private = data;
2544 static int skl_tplg_control_load(struct snd_soc_component *cmpnt,
2545 struct snd_kcontrol_new *kctl,
2546 struct snd_soc_tplg_ctl_hdr *hdr)
2548 struct soc_bytes_ext *sb;
2549 struct snd_soc_tplg_bytes_control *tplg_bc;
2550 struct snd_soc_tplg_enum_control *tplg_ec;
2551 struct hdac_ext_bus *ebus = snd_soc_component_get_drvdata(cmpnt);
2552 struct hdac_bus *bus = ebus_to_hbus(ebus);
2553 struct soc_enum *se;
2555 switch (hdr->ops.info) {
2556 case SND_SOC_TPLG_CTL_BYTES:
2557 tplg_bc = container_of(hdr,
2558 struct snd_soc_tplg_bytes_control, hdr);
2559 if (kctl->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2560 sb = (struct soc_bytes_ext *)kctl->private_value;
2561 if (tplg_bc->priv.size)
2562 return skl_init_algo_data(
2563 bus->dev, sb, tplg_bc);
2567 case SND_SOC_TPLG_CTL_ENUM:
2568 tplg_ec = container_of(hdr,
2569 struct snd_soc_tplg_enum_control, hdr);
2570 if (kctl->access & SNDRV_CTL_ELEM_ACCESS_READWRITE) {
2571 se = (struct soc_enum *)kctl->private_value;
2572 if (tplg_ec->priv.size)
2573 return skl_init_enum_data(bus->dev, se,
2579 dev_warn(bus->dev, "Control load not supported %d:%d:%d\n",
2580 hdr->ops.get, hdr->ops.put, hdr->ops.info);
2587 static int skl_tplg_fill_str_mfest_tkn(struct device *dev,
2588 struct snd_soc_tplg_vendor_string_elem *str_elem,
2592 static int ref_count;
2594 switch (str_elem->token) {
2595 case SKL_TKN_STR_LIB_NAME:
2596 if (ref_count > skl->skl_sst->lib_count - 1) {
2601 strncpy(skl->skl_sst->lib_info[ref_count].name,
2603 ARRAY_SIZE(skl->skl_sst->lib_info[ref_count].name));
2609 dev_err(dev, "Not a string token %d\n", str_elem->token);
2616 static int skl_tplg_get_str_tkn(struct device *dev,
2617 struct snd_soc_tplg_vendor_array *array,
2620 int tkn_count = 0, ret;
2621 struct snd_soc_tplg_vendor_string_elem *str_elem;
2623 str_elem = (struct snd_soc_tplg_vendor_string_elem *)array->value;
2624 while (tkn_count < array->num_elems) {
2625 ret = skl_tplg_fill_str_mfest_tkn(dev, str_elem, skl);
2631 tkn_count = tkn_count + ret;
2637 static int skl_tplg_get_int_tkn(struct device *dev,
2638 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2643 switch (tkn_elem->token) {
2644 case SKL_TKN_U32_LIB_COUNT:
2645 skl->skl_sst->lib_count = tkn_elem->value;
2650 dev_err(dev, "Not a manifest token %d\n", tkn_elem->token);
2658 * Fill the manifest structure by parsing the tokens based on the
2661 static int skl_tplg_get_manifest_tkn(struct device *dev,
2662 char *pvt_data, struct skl *skl,
2665 int tkn_count = 0, ret;
2666 int off = 0, tuple_size = 0;
2667 struct snd_soc_tplg_vendor_array *array;
2668 struct snd_soc_tplg_vendor_value_elem *tkn_elem;
2670 if (block_size <= 0)
2673 while (tuple_size < block_size) {
2674 array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off);
2676 switch (array->type) {
2677 case SND_SOC_TPLG_TUPLE_TYPE_STRING:
2678 ret = skl_tplg_get_str_tkn(dev, array, skl);
2684 tuple_size += tkn_count *
2685 sizeof(struct snd_soc_tplg_vendor_string_elem);
2688 case SND_SOC_TPLG_TUPLE_TYPE_UUID:
2689 dev_warn(dev, "no uuid tokens for skl tplf manifest\n");
2693 tkn_elem = array->value;
2698 while (tkn_count <= array->num_elems - 1) {
2699 ret = skl_tplg_get_int_tkn(dev,
2704 tkn_count = tkn_count + ret;
2706 tuple_size += tkn_count *
2707 sizeof(struct snd_soc_tplg_vendor_value_elem);
2717 * Parse manifest private data for tokens. The private data block is
2718 * preceded by descriptors for type and size of data block.
2720 static int skl_tplg_get_manifest_data(struct snd_soc_tplg_manifest *manifest,
2721 struct device *dev, struct skl *skl)
2723 struct snd_soc_tplg_vendor_array *array;
2724 int num_blocks, block_size = 0, block_type, off = 0;
2728 /* Read the NUM_DATA_BLOCKS descriptor */
2729 array = (struct snd_soc_tplg_vendor_array *)manifest->priv.data;
2730 ret = skl_tplg_get_desc_blocks(dev, array);
2736 array = (struct snd_soc_tplg_vendor_array *)
2737 (manifest->priv.data + off);
2739 /* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */
2740 while (num_blocks > 0) {
2741 ret = skl_tplg_get_desc_blocks(dev, array);
2748 array = (struct snd_soc_tplg_vendor_array *)
2749 (manifest->priv.data + off);
2751 ret = skl_tplg_get_desc_blocks(dev, array);
2758 array = (struct snd_soc_tplg_vendor_array *)
2759 (manifest->priv.data + off);
2761 data = (manifest->priv.data + off);
2763 if (block_type == SKL_TYPE_TUPLE) {
2764 ret = skl_tplg_get_manifest_tkn(dev, data, skl,
2779 static int skl_manifest_load(struct snd_soc_component *cmpnt,
2780 struct snd_soc_tplg_manifest *manifest)
2782 struct hdac_ext_bus *ebus = snd_soc_component_get_drvdata(cmpnt);
2783 struct hdac_bus *bus = ebus_to_hbus(ebus);
2784 struct skl *skl = ebus_to_skl(ebus);
2786 /* proceed only if we have private data defined */
2787 if (manifest->priv.size == 0)
2790 skl_tplg_get_manifest_data(manifest, bus->dev, skl);
2792 if (skl->skl_sst->lib_count > SKL_MAX_LIB) {
2793 dev_err(bus->dev, "Exceeding max Library count. Got:%d\n",
2794 skl->skl_sst->lib_count);
2801 static struct snd_soc_tplg_ops skl_tplg_ops = {
2802 .widget_load = skl_tplg_widget_load,
2803 .control_load = skl_tplg_control_load,
2804 .bytes_ext_ops = skl_tlv_ops,
2805 .bytes_ext_ops_count = ARRAY_SIZE(skl_tlv_ops),
2806 .io_ops = skl_tplg_kcontrol_ops,
2807 .io_ops_count = ARRAY_SIZE(skl_tplg_kcontrol_ops),
2808 .manifest = skl_manifest_load,
2812 * A pipe can have multiple modules, each of them will be a DAPM widget as
2813 * well. While managing a pipeline we need to get the list of all the
2814 * widgets in a pipelines, so this helper - skl_tplg_create_pipe_widget_list()
2815 * helps to get the SKL type widgets in that pipeline
2817 static int skl_tplg_create_pipe_widget_list(struct snd_soc_platform *platform)
2819 struct snd_soc_dapm_widget *w;
2820 struct skl_module_cfg *mcfg = NULL;
2821 struct skl_pipe_module *p_module = NULL;
2822 struct skl_pipe *pipe;
2824 list_for_each_entry(w, &platform->component.card->widgets, list) {
2825 if (is_skl_dsp_widget_type(w) && w->priv != NULL) {
2829 p_module = devm_kzalloc(platform->dev,
2830 sizeof(*p_module), GFP_KERNEL);
2835 list_add_tail(&p_module->node, &pipe->w_list);
2842 static void skl_tplg_set_pipe_type(struct skl *skl, struct skl_pipe *pipe)
2844 struct skl_pipe_module *w_module;
2845 struct snd_soc_dapm_widget *w;
2846 struct skl_module_cfg *mconfig;
2847 bool host_found = false, link_found = false;
2849 list_for_each_entry(w_module, &pipe->w_list, node) {
2853 if (mconfig->dev_type == SKL_DEVICE_HDAHOST)
2855 else if (mconfig->dev_type != SKL_DEVICE_NONE)
2859 if (host_found && link_found)
2860 pipe->passthru = true;
2862 pipe->passthru = false;
2865 /* This will be read from topology manifest, currently defined here */
2866 #define SKL_MAX_MCPS 30000000
2867 #define SKL_FW_MAX_MEM 1000000
2870 * SKL topology init routine
2872 int skl_tplg_init(struct snd_soc_platform *platform, struct hdac_ext_bus *ebus)
2875 const struct firmware *fw;
2876 struct hdac_bus *bus = ebus_to_hbus(ebus);
2877 struct skl *skl = ebus_to_skl(ebus);
2878 struct skl_pipeline *ppl;
2880 ret = request_firmware(&fw, skl->tplg_name, bus->dev);
2882 dev_err(bus->dev, "tplg fw %s load failed with %d\n",
2883 skl->tplg_name, ret);
2884 ret = request_firmware(&fw, "dfw_sst.bin", bus->dev);
2886 dev_err(bus->dev, "Fallback tplg fw %s load failed with %d\n",
2887 "dfw_sst.bin", ret);
2893 * The complete tplg for SKL is loaded as index 0, we don't use
2896 ret = snd_soc_tplg_component_load(&platform->component,
2897 &skl_tplg_ops, fw, 0);
2899 dev_err(bus->dev, "tplg component load failed%d\n", ret);
2900 release_firmware(fw);
2904 skl->resource.max_mcps = SKL_MAX_MCPS;
2905 skl->resource.max_mem = SKL_FW_MAX_MEM;
2908 ret = skl_tplg_create_pipe_widget_list(platform);
2912 list_for_each_entry(ppl, &skl->ppl_list, node)
2913 skl_tplg_set_pipe_type(skl, ppl->pipe);