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)
40 * SKL DSP driver modelling uses only few DAPM widgets so for rest we will
41 * ignore. This helpers checks if the SKL driver handles this widget type
43 static int is_skl_dsp_widget_type(struct snd_soc_dapm_widget *w)
46 case snd_soc_dapm_dai_link:
47 case snd_soc_dapm_dai_in:
48 case snd_soc_dapm_aif_in:
49 case snd_soc_dapm_aif_out:
50 case snd_soc_dapm_dai_out:
51 case snd_soc_dapm_switch:
59 * Each pipelines needs memory to be allocated. Check if we have free memory
60 * from available pool.
62 static bool skl_is_pipe_mem_avail(struct skl *skl,
63 struct skl_module_cfg *mconfig)
65 struct skl_sst *ctx = skl->skl_sst;
67 if (skl->resource.mem + mconfig->pipe->memory_pages >
68 skl->resource.max_mem) {
70 "%s: module_id %d instance %d\n", __func__,
71 mconfig->id.module_id,
72 mconfig->id.instance_id);
74 "exceeds ppl memory available %d mem %d\n",
75 skl->resource.max_mem, skl->resource.mem);
83 * Add the mem to the mem pool. This is freed when pipe is deleted.
84 * Note: DSP does actual memory management we only keep track for complete
87 static void skl_tplg_alloc_pipe_mem(struct skl *skl,
88 struct skl_module_cfg *mconfig)
90 skl->resource.mem += mconfig->pipe->memory_pages;
94 * Pipeline needs needs DSP CPU resources for computation, this is
95 * quantified in MCPS (Million Clocks Per Second) required for module/pipe
97 * Each pipelines needs mcps to be allocated. Check if we have mcps for this
101 static bool skl_is_pipe_mcps_avail(struct skl *skl,
102 struct skl_module_cfg *mconfig)
104 struct skl_sst *ctx = skl->skl_sst;
106 if (skl->resource.mcps + mconfig->mcps > skl->resource.max_mcps) {
108 "%s: module_id %d instance %d\n", __func__,
109 mconfig->id.module_id, mconfig->id.instance_id);
111 "exceeds ppl mcps available %d > mem %d\n",
112 skl->resource.max_mcps, skl->resource.mcps);
119 static void skl_tplg_alloc_pipe_mcps(struct skl *skl,
120 struct skl_module_cfg *mconfig)
122 skl->resource.mcps += mconfig->mcps;
126 * Free the mcps when tearing down
129 skl_tplg_free_pipe_mcps(struct skl *skl, struct skl_module_cfg *mconfig)
131 skl->resource.mcps -= mconfig->mcps;
135 * Free the memory when tearing down
138 skl_tplg_free_pipe_mem(struct skl *skl, struct skl_module_cfg *mconfig)
140 skl->resource.mem -= mconfig->pipe->memory_pages;
144 static void skl_dump_mconfig(struct skl_sst *ctx,
145 struct skl_module_cfg *mcfg)
147 dev_dbg(ctx->dev, "Dumping config\n");
148 dev_dbg(ctx->dev, "Input Format:\n");
149 dev_dbg(ctx->dev, "channels = %d\n", mcfg->in_fmt[0].channels);
150 dev_dbg(ctx->dev, "s_freq = %d\n", mcfg->in_fmt[0].s_freq);
151 dev_dbg(ctx->dev, "ch_cfg = %d\n", mcfg->in_fmt[0].ch_cfg);
152 dev_dbg(ctx->dev, "valid bit depth = %d\n", mcfg->in_fmt[0].valid_bit_depth);
153 dev_dbg(ctx->dev, "Output Format:\n");
154 dev_dbg(ctx->dev, "channels = %d\n", mcfg->out_fmt[0].channels);
155 dev_dbg(ctx->dev, "s_freq = %d\n", mcfg->out_fmt[0].s_freq);
156 dev_dbg(ctx->dev, "valid bit depth = %d\n", mcfg->out_fmt[0].valid_bit_depth);
157 dev_dbg(ctx->dev, "ch_cfg = %d\n", mcfg->out_fmt[0].ch_cfg);
160 static void skl_tplg_update_chmap(struct skl_module_fmt *fmt, int chs)
162 int slot_map = 0xFFFFFFFF;
166 for (i = 0; i < chs; i++) {
168 * For 2 channels with starting slot as 0, slot map will
169 * look like 0xFFFFFF10.
171 slot_map &= (~(0xF << (4 * i)) | (start_slot << (4 * i)));
174 fmt->ch_map = slot_map;
177 static void skl_tplg_update_params(struct skl_module_fmt *fmt,
178 struct skl_pipe_params *params, int fixup)
180 if (fixup & SKL_RATE_FIXUP_MASK)
181 fmt->s_freq = params->s_freq;
182 if (fixup & SKL_CH_FIXUP_MASK) {
183 fmt->channels = params->ch;
184 skl_tplg_update_chmap(fmt, fmt->channels);
186 if (fixup & SKL_FMT_FIXUP_MASK) {
187 fmt->valid_bit_depth = skl_get_bit_depth(params->s_fmt);
190 * 16 bit is 16 bit container whereas 24 bit is in 32 bit
191 * container so update bit depth accordingly
193 switch (fmt->valid_bit_depth) {
194 case SKL_DEPTH_16BIT:
195 fmt->bit_depth = fmt->valid_bit_depth;
199 fmt->bit_depth = SKL_DEPTH_32BIT;
207 * A pipeline may have modules which impact the pcm parameters, like SRC,
208 * channel converter, format converter.
209 * We need to calculate the output params by applying the 'fixup'
210 * Topology will tell driver which type of fixup is to be applied by
211 * supplying the fixup mask, so based on that we calculate the output
213 * Now In FE the pcm hw_params is source/target format. Same is applicable
214 * for BE with its hw_params invoked.
215 * here based on FE, BE pipeline and direction we calculate the input and
216 * outfix and then apply that for a module
218 static void skl_tplg_update_params_fixup(struct skl_module_cfg *m_cfg,
219 struct skl_pipe_params *params, bool is_fe)
221 int in_fixup, out_fixup;
222 struct skl_module_fmt *in_fmt, *out_fmt;
224 /* Fixups will be applied to pin 0 only */
225 in_fmt = &m_cfg->in_fmt[0];
226 out_fmt = &m_cfg->out_fmt[0];
228 if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
230 in_fixup = m_cfg->params_fixup;
231 out_fixup = (~m_cfg->converter) &
234 out_fixup = m_cfg->params_fixup;
235 in_fixup = (~m_cfg->converter) &
240 out_fixup = m_cfg->params_fixup;
241 in_fixup = (~m_cfg->converter) &
244 in_fixup = m_cfg->params_fixup;
245 out_fixup = (~m_cfg->converter) &
250 skl_tplg_update_params(in_fmt, params, in_fixup);
251 skl_tplg_update_params(out_fmt, params, out_fixup);
255 * A module needs input and output buffers, which are dependent upon pcm
256 * params, so once we have calculate params, we need buffer calculation as
259 static void skl_tplg_update_buffer_size(struct skl_sst *ctx,
260 struct skl_module_cfg *mcfg)
263 struct skl_module_fmt *in_fmt, *out_fmt;
264 int in_rate, out_rate;
267 /* Since fixups is applied to pin 0 only, ibs, obs needs
268 * change for pin 0 only
270 in_fmt = &mcfg->in_fmt[0];
271 out_fmt = &mcfg->out_fmt[0];
273 if (mcfg->m_type == SKL_MODULE_TYPE_SRCINT)
276 if (in_fmt->s_freq % 1000)
277 in_rate = (in_fmt->s_freq / 1000) + 1;
279 in_rate = (in_fmt->s_freq / 1000);
281 mcfg->ibs = in_rate * (mcfg->in_fmt->channels) *
282 (mcfg->in_fmt->bit_depth >> 3) *
285 if (mcfg->out_fmt->s_freq % 1000)
286 out_rate = (mcfg->out_fmt->s_freq / 1000) + 1;
288 out_rate = (mcfg->out_fmt->s_freq / 1000);
290 mcfg->obs = out_rate * (mcfg->out_fmt->channels) *
291 (mcfg->out_fmt->bit_depth >> 3) *
295 static int skl_tplg_update_be_blob(struct snd_soc_dapm_widget *w,
298 struct skl_module_cfg *m_cfg = w->priv;
300 u32 ch, s_freq, s_fmt;
301 struct nhlt_specific_cfg *cfg;
302 struct skl *skl = get_skl_ctx(ctx->dev);
304 /* check if we already have blob */
305 if (m_cfg->formats_config.caps_size > 0)
308 dev_dbg(ctx->dev, "Applying default cfg blob\n");
309 switch (m_cfg->dev_type) {
310 case SKL_DEVICE_DMIC:
311 link_type = NHLT_LINK_DMIC;
312 dir = SNDRV_PCM_STREAM_CAPTURE;
313 s_freq = m_cfg->in_fmt[0].s_freq;
314 s_fmt = m_cfg->in_fmt[0].bit_depth;
315 ch = m_cfg->in_fmt[0].channels;
319 link_type = NHLT_LINK_SSP;
320 if (m_cfg->hw_conn_type == SKL_CONN_SOURCE) {
321 dir = SNDRV_PCM_STREAM_PLAYBACK;
322 s_freq = m_cfg->out_fmt[0].s_freq;
323 s_fmt = m_cfg->out_fmt[0].bit_depth;
324 ch = m_cfg->out_fmt[0].channels;
326 dir = SNDRV_PCM_STREAM_CAPTURE;
327 s_freq = m_cfg->in_fmt[0].s_freq;
328 s_fmt = m_cfg->in_fmt[0].bit_depth;
329 ch = m_cfg->in_fmt[0].channels;
337 /* update the blob based on virtual bus_id and default params */
338 cfg = skl_get_ep_blob(skl, m_cfg->vbus_id, link_type,
339 s_fmt, ch, s_freq, dir);
341 m_cfg->formats_config.caps_size = cfg->size;
342 m_cfg->formats_config.caps = (u32 *) &cfg->caps;
344 dev_err(ctx->dev, "Blob NULL for id %x type %d dirn %d\n",
345 m_cfg->vbus_id, link_type, dir);
346 dev_err(ctx->dev, "PCM: ch %d, freq %d, fmt %d\n",
354 static void skl_tplg_update_module_params(struct snd_soc_dapm_widget *w,
357 struct skl_module_cfg *m_cfg = w->priv;
358 struct skl_pipe_params *params = m_cfg->pipe->p_params;
359 int p_conn_type = m_cfg->pipe->conn_type;
362 if (!m_cfg->params_fixup)
365 dev_dbg(ctx->dev, "Mconfig for widget=%s BEFORE updation\n",
368 skl_dump_mconfig(ctx, m_cfg);
370 if (p_conn_type == SKL_PIPE_CONN_TYPE_FE)
375 skl_tplg_update_params_fixup(m_cfg, params, is_fe);
376 skl_tplg_update_buffer_size(ctx, m_cfg);
378 dev_dbg(ctx->dev, "Mconfig for widget=%s AFTER updation\n",
381 skl_dump_mconfig(ctx, m_cfg);
385 * some modules can have multiple params set from user control and
386 * need to be set after module is initialized. If set_param flag is
387 * set module params will be done after module is initialised.
389 static int skl_tplg_set_module_params(struct snd_soc_dapm_widget *w,
393 struct skl_module_cfg *mconfig = w->priv;
394 const struct snd_kcontrol_new *k;
395 struct soc_bytes_ext *sb;
396 struct skl_algo_data *bc;
397 struct skl_specific_cfg *sp_cfg;
399 if (mconfig->formats_config.caps_size > 0 &&
400 mconfig->formats_config.set_params == SKL_PARAM_SET) {
401 sp_cfg = &mconfig->formats_config;
402 ret = skl_set_module_params(ctx, sp_cfg->caps,
404 sp_cfg->param_id, mconfig);
409 for (i = 0; i < w->num_kcontrols; i++) {
410 k = &w->kcontrol_news[i];
411 if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
412 sb = (void *) k->private_value;
413 bc = (struct skl_algo_data *)sb->dobj.private;
415 if (bc->set_params == SKL_PARAM_SET) {
416 ret = skl_set_module_params(ctx,
417 (u32 *)bc->params, bc->size,
418 bc->param_id, mconfig);
429 * some module param can set from user control and this is required as
430 * when module is initailzed. if module param is required in init it is
431 * identifed by set_param flag. if set_param flag is not set, then this
432 * parameter needs to set as part of module init.
434 static int skl_tplg_set_module_init_data(struct snd_soc_dapm_widget *w)
436 const struct snd_kcontrol_new *k;
437 struct soc_bytes_ext *sb;
438 struct skl_algo_data *bc;
439 struct skl_module_cfg *mconfig = w->priv;
442 for (i = 0; i < w->num_kcontrols; i++) {
443 k = &w->kcontrol_news[i];
444 if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
445 sb = (struct soc_bytes_ext *)k->private_value;
446 bc = (struct skl_algo_data *)sb->dobj.private;
448 if (bc->set_params != SKL_PARAM_INIT)
451 mconfig->formats_config.caps = (u32 *)&bc->params;
452 mconfig->formats_config.caps_size = bc->size;
462 * Inside a pipe instance, we can have various modules. These modules need
463 * to instantiated in DSP by invoking INIT_MODULE IPC, which is achieved by
464 * skl_init_module() routine, so invoke that for all modules in a pipeline
467 skl_tplg_init_pipe_modules(struct skl *skl, struct skl_pipe *pipe)
469 struct skl_pipe_module *w_module;
470 struct snd_soc_dapm_widget *w;
471 struct skl_module_cfg *mconfig;
472 struct skl_sst *ctx = skl->skl_sst;
475 list_for_each_entry(w_module, &pipe->w_list, node) {
479 /* check if module ids are populated */
480 if (mconfig->id.module_id < 0) {
481 dev_err(skl->skl_sst->dev,
482 "module %pUL id not populated\n",
483 (uuid_le *)mconfig->guid);
487 /* check resource available */
488 if (!skl_is_pipe_mcps_avail(skl, mconfig))
491 if (mconfig->is_loadable && ctx->dsp->fw_ops.load_mod) {
492 ret = ctx->dsp->fw_ops.load_mod(ctx->dsp,
493 mconfig->id.module_id, mconfig->guid);
497 mconfig->m_state = SKL_MODULE_LOADED;
500 /* update blob if blob is null for be with default value */
501 skl_tplg_update_be_blob(w, ctx);
504 * apply fix/conversion to module params based on
507 skl_tplg_update_module_params(w, ctx);
509 skl_tplg_set_module_init_data(w);
510 ret = skl_init_module(ctx, mconfig);
514 skl_tplg_alloc_pipe_mcps(skl, mconfig);
515 ret = skl_tplg_set_module_params(w, ctx);
523 static int skl_tplg_unload_pipe_modules(struct skl_sst *ctx,
524 struct skl_pipe *pipe)
527 struct skl_pipe_module *w_module = NULL;
528 struct skl_module_cfg *mconfig = NULL;
530 list_for_each_entry(w_module, &pipe->w_list, node) {
531 mconfig = w_module->w->priv;
533 if (mconfig->is_loadable && ctx->dsp->fw_ops.unload_mod &&
534 mconfig->m_state > SKL_MODULE_UNINIT) {
535 ret = ctx->dsp->fw_ops.unload_mod(ctx->dsp,
536 mconfig->id.module_id);
542 /* no modules to unload in this path, so return */
547 * Mixer module represents a pipeline. So in the Pre-PMU event of mixer we
548 * need create the pipeline. So we do following:
549 * - check the resources
550 * - Create the pipeline
551 * - Initialize the modules in pipeline
552 * - finally bind all modules together
554 static int skl_tplg_mixer_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
558 struct skl_module_cfg *mconfig = w->priv;
559 struct skl_pipe_module *w_module;
560 struct skl_pipe *s_pipe = mconfig->pipe;
561 struct skl_module_cfg *src_module = NULL, *dst_module;
562 struct skl_sst *ctx = skl->skl_sst;
564 /* check resource available */
565 if (!skl_is_pipe_mcps_avail(skl, mconfig))
568 if (!skl_is_pipe_mem_avail(skl, mconfig))
572 * Create a list of modules for pipe.
573 * This list contains modules from source to sink
575 ret = skl_create_pipeline(ctx, mconfig->pipe);
579 skl_tplg_alloc_pipe_mem(skl, mconfig);
580 skl_tplg_alloc_pipe_mcps(skl, mconfig);
582 /* Init all pipe modules from source to sink */
583 ret = skl_tplg_init_pipe_modules(skl, s_pipe);
587 /* Bind modules from source to sink */
588 list_for_each_entry(w_module, &s_pipe->w_list, node) {
589 dst_module = w_module->w->priv;
591 if (src_module == NULL) {
592 src_module = dst_module;
596 ret = skl_bind_modules(ctx, src_module, dst_module);
600 src_module = dst_module;
607 * Some modules require params to be set after the module is bound to
608 * all pins connected.
610 * The module provider initializes set_param flag for such modules and we
611 * send params after binding
613 static int skl_tplg_set_module_bind_params(struct snd_soc_dapm_widget *w,
614 struct skl_module_cfg *mcfg, struct skl_sst *ctx)
617 struct skl_module_cfg *mconfig = w->priv;
618 const struct snd_kcontrol_new *k;
619 struct soc_bytes_ext *sb;
620 struct skl_algo_data *bc;
621 struct skl_specific_cfg *sp_cfg;
624 * check all out/in pins are in bind state.
625 * if so set the module param
627 for (i = 0; i < mcfg->max_out_queue; i++) {
628 if (mcfg->m_out_pin[i].pin_state != SKL_PIN_BIND_DONE)
632 for (i = 0; i < mcfg->max_in_queue; i++) {
633 if (mcfg->m_in_pin[i].pin_state != SKL_PIN_BIND_DONE)
637 if (mconfig->formats_config.caps_size > 0 &&
638 mconfig->formats_config.set_params == SKL_PARAM_BIND) {
639 sp_cfg = &mconfig->formats_config;
640 ret = skl_set_module_params(ctx, sp_cfg->caps,
642 sp_cfg->param_id, mconfig);
647 for (i = 0; i < w->num_kcontrols; i++) {
648 k = &w->kcontrol_news[i];
649 if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
650 sb = (void *) k->private_value;
651 bc = (struct skl_algo_data *)sb->dobj.private;
653 if (bc->set_params == SKL_PARAM_BIND) {
654 ret = skl_set_module_params(ctx,
655 (u32 *)bc->params, bc->max,
656 bc->param_id, mconfig);
666 static int skl_tplg_bind_sinks(struct snd_soc_dapm_widget *w,
668 struct snd_soc_dapm_widget *src_w,
669 struct skl_module_cfg *src_mconfig)
671 struct snd_soc_dapm_path *p;
672 struct snd_soc_dapm_widget *sink = NULL, *next_sink = NULL;
673 struct skl_module_cfg *sink_mconfig;
674 struct skl_sst *ctx = skl->skl_sst;
677 snd_soc_dapm_widget_for_each_sink_path(w, p) {
681 dev_dbg(ctx->dev, "%s: src widget=%s\n", __func__, w->name);
682 dev_dbg(ctx->dev, "%s: sink widget=%s\n", __func__, p->sink->name);
686 if (!is_skl_dsp_widget_type(p->sink))
687 return skl_tplg_bind_sinks(p->sink, skl, src_w, src_mconfig);
690 * here we will check widgets in sink pipelines, so that
691 * can be any widgets type and we are only interested if
692 * they are ones used for SKL so check that first
694 if ((p->sink->priv != NULL) &&
695 is_skl_dsp_widget_type(p->sink)) {
698 sink_mconfig = sink->priv;
700 if (src_mconfig->m_state == SKL_MODULE_UNINIT ||
701 sink_mconfig->m_state == SKL_MODULE_UNINIT)
704 /* Bind source to sink, mixin is always source */
705 ret = skl_bind_modules(ctx, src_mconfig, sink_mconfig);
709 /* set module params after bind */
710 skl_tplg_set_module_bind_params(src_w, src_mconfig, ctx);
711 skl_tplg_set_module_bind_params(sink, sink_mconfig, ctx);
713 /* Start sinks pipe first */
714 if (sink_mconfig->pipe->state != SKL_PIPE_STARTED) {
715 if (sink_mconfig->pipe->conn_type !=
716 SKL_PIPE_CONN_TYPE_FE)
717 ret = skl_run_pipe(ctx,
726 return skl_tplg_bind_sinks(next_sink, skl, src_w, src_mconfig);
732 * A PGA represents a module in a pipeline. So in the Pre-PMU event of PGA
733 * we need to do following:
734 * - Bind to sink pipeline
735 * Since the sink pipes can be running and we don't get mixer event on
736 * connect for already running mixer, we need to find the sink pipes
737 * here and bind to them. This way dynamic connect works.
738 * - Start sink pipeline, if not running
739 * - Then run current pipe
741 static int skl_tplg_pga_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
744 struct skl_module_cfg *src_mconfig;
745 struct skl_sst *ctx = skl->skl_sst;
748 src_mconfig = w->priv;
751 * find which sink it is connected to, bind with the sink,
752 * if sink is not started, start sink pipe first, then start
755 ret = skl_tplg_bind_sinks(w, skl, w, src_mconfig);
759 /* Start source pipe last after starting all sinks */
760 if (src_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
761 return skl_run_pipe(ctx, src_mconfig->pipe);
766 static struct snd_soc_dapm_widget *skl_get_src_dsp_widget(
767 struct snd_soc_dapm_widget *w, struct skl *skl)
769 struct snd_soc_dapm_path *p;
770 struct snd_soc_dapm_widget *src_w = NULL;
771 struct skl_sst *ctx = skl->skl_sst;
773 snd_soc_dapm_widget_for_each_source_path(w, p) {
778 dev_dbg(ctx->dev, "sink widget=%s\n", w->name);
779 dev_dbg(ctx->dev, "src widget=%s\n", p->source->name);
782 * here we will check widgets in sink pipelines, so that can
783 * be any widgets type and we are only interested if they are
784 * ones used for SKL so check that first
786 if ((p->source->priv != NULL) &&
787 is_skl_dsp_widget_type(p->source)) {
793 return skl_get_src_dsp_widget(src_w, skl);
799 * in the Post-PMU event of mixer we need to do following:
800 * - Check if this pipe is running
802 * - bind this pipeline to its source pipeline
803 * if source pipe is already running, this means it is a dynamic
804 * connection and we need to bind only to that pipe
805 * - start this pipeline
807 static int skl_tplg_mixer_dapm_post_pmu_event(struct snd_soc_dapm_widget *w,
811 struct snd_soc_dapm_widget *source, *sink;
812 struct skl_module_cfg *src_mconfig, *sink_mconfig;
813 struct skl_sst *ctx = skl->skl_sst;
814 int src_pipe_started = 0;
817 sink_mconfig = sink->priv;
820 * If source pipe is already started, that means source is driving
821 * one more sink before this sink got connected, Since source is
822 * started, bind this sink to source and start this pipe.
824 source = skl_get_src_dsp_widget(w, skl);
825 if (source != NULL) {
826 src_mconfig = source->priv;
827 sink_mconfig = sink->priv;
828 src_pipe_started = 1;
831 * check pipe state, then no need to bind or start the
834 if (src_mconfig->pipe->state != SKL_PIPE_STARTED)
835 src_pipe_started = 0;
838 if (src_pipe_started) {
839 ret = skl_bind_modules(ctx, src_mconfig, sink_mconfig);
843 /* set module params after bind */
844 skl_tplg_set_module_bind_params(source, src_mconfig, ctx);
845 skl_tplg_set_module_bind_params(sink, sink_mconfig, ctx);
847 if (sink_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
848 ret = skl_run_pipe(ctx, sink_mconfig->pipe);
855 * in the Pre-PMD event of mixer we need to do following:
857 * - find the source connections and remove that from dapm_path_list
858 * - unbind with source pipelines if still connected
860 static int skl_tplg_mixer_dapm_pre_pmd_event(struct snd_soc_dapm_widget *w,
863 struct skl_module_cfg *src_mconfig, *sink_mconfig;
865 struct skl_sst *ctx = skl->skl_sst;
867 sink_mconfig = w->priv;
870 ret = skl_stop_pipe(ctx, sink_mconfig->pipe);
874 for (i = 0; i < sink_mconfig->max_in_queue; i++) {
875 if (sink_mconfig->m_in_pin[i].pin_state == SKL_PIN_BIND_DONE) {
876 src_mconfig = sink_mconfig->m_in_pin[i].tgt_mcfg;
880 * If path_found == 1, that means pmd for source
881 * pipe has not occurred, source is connected to
882 * some other sink. so its responsibility of sink
883 * to unbind itself from source.
885 ret = skl_stop_pipe(ctx, src_mconfig->pipe);
889 ret = skl_unbind_modules(ctx,
890 src_mconfig, sink_mconfig);
898 * in the Post-PMD event of mixer we need to do following:
899 * - Free the mcps used
900 * - Free the mem used
901 * - Unbind the modules within the pipeline
902 * - Delete the pipeline (modules are not required to be explicitly
903 * deleted, pipeline delete is enough here
905 static int skl_tplg_mixer_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
908 struct skl_module_cfg *mconfig = w->priv;
909 struct skl_pipe_module *w_module;
910 struct skl_module_cfg *src_module = NULL, *dst_module;
911 struct skl_sst *ctx = skl->skl_sst;
912 struct skl_pipe *s_pipe = mconfig->pipe;
915 if (s_pipe->state == SKL_PIPE_INVALID)
918 skl_tplg_free_pipe_mcps(skl, mconfig);
919 skl_tplg_free_pipe_mem(skl, mconfig);
921 list_for_each_entry(w_module, &s_pipe->w_list, node) {
922 dst_module = w_module->w->priv;
924 if (mconfig->m_state >= SKL_MODULE_INIT_DONE)
925 skl_tplg_free_pipe_mcps(skl, dst_module);
926 if (src_module == NULL) {
927 src_module = dst_module;
931 skl_unbind_modules(ctx, src_module, dst_module);
932 src_module = dst_module;
935 ret = skl_delete_pipe(ctx, mconfig->pipe);
937 return skl_tplg_unload_pipe_modules(ctx, s_pipe);
941 * in the Post-PMD event of PGA we need to do following:
942 * - Free the mcps used
943 * - Stop the pipeline
944 * - In source pipe is connected, unbind with source pipelines
946 static int skl_tplg_pga_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
949 struct skl_module_cfg *src_mconfig, *sink_mconfig;
951 struct skl_sst *ctx = skl->skl_sst;
953 src_mconfig = w->priv;
955 /* Stop the pipe since this is a mixin module */
956 ret = skl_stop_pipe(ctx, src_mconfig->pipe);
960 for (i = 0; i < src_mconfig->max_out_queue; i++) {
961 if (src_mconfig->m_out_pin[i].pin_state == SKL_PIN_BIND_DONE) {
962 sink_mconfig = src_mconfig->m_out_pin[i].tgt_mcfg;
966 * This is a connecter and if path is found that means
967 * unbind between source and sink has not happened yet
969 ret = skl_unbind_modules(ctx, src_mconfig,
978 * In modelling, we assume there will be ONLY one mixer in a pipeline. If
979 * mixer is not required then it is treated as static mixer aka vmixer with
980 * a hard path to source module
981 * So we don't need to check if source is started or not as hard path puts
982 * dependency on each other
984 static int skl_tplg_vmixer_event(struct snd_soc_dapm_widget *w,
985 struct snd_kcontrol *k, int event)
987 struct snd_soc_dapm_context *dapm = w->dapm;
988 struct skl *skl = get_skl_ctx(dapm->dev);
991 case SND_SOC_DAPM_PRE_PMU:
992 return skl_tplg_mixer_dapm_pre_pmu_event(w, skl);
994 case SND_SOC_DAPM_POST_PMU:
995 return skl_tplg_mixer_dapm_post_pmu_event(w, skl);
997 case SND_SOC_DAPM_PRE_PMD:
998 return skl_tplg_mixer_dapm_pre_pmd_event(w, skl);
1000 case SND_SOC_DAPM_POST_PMD:
1001 return skl_tplg_mixer_dapm_post_pmd_event(w, skl);
1008 * In modelling, we assume there will be ONLY one mixer in a pipeline. If a
1009 * second one is required that is created as another pipe entity.
1010 * The mixer is responsible for pipe management and represent a pipeline
1013 static int skl_tplg_mixer_event(struct snd_soc_dapm_widget *w,
1014 struct snd_kcontrol *k, int event)
1016 struct snd_soc_dapm_context *dapm = w->dapm;
1017 struct skl *skl = get_skl_ctx(dapm->dev);
1020 case SND_SOC_DAPM_PRE_PMU:
1021 return skl_tplg_mixer_dapm_pre_pmu_event(w, skl);
1023 case SND_SOC_DAPM_POST_PMU:
1024 return skl_tplg_mixer_dapm_post_pmu_event(w, skl);
1026 case SND_SOC_DAPM_PRE_PMD:
1027 return skl_tplg_mixer_dapm_pre_pmd_event(w, skl);
1029 case SND_SOC_DAPM_POST_PMD:
1030 return skl_tplg_mixer_dapm_post_pmd_event(w, skl);
1037 * In modelling, we assumed rest of the modules in pipeline are PGA. But we
1038 * are interested in last PGA (leaf PGA) in a pipeline to disconnect with
1039 * the sink when it is running (two FE to one BE or one FE to two BE)
1042 static int skl_tplg_pga_event(struct snd_soc_dapm_widget *w,
1043 struct snd_kcontrol *k, int event)
1046 struct snd_soc_dapm_context *dapm = w->dapm;
1047 struct skl *skl = get_skl_ctx(dapm->dev);
1050 case SND_SOC_DAPM_PRE_PMU:
1051 return skl_tplg_pga_dapm_pre_pmu_event(w, skl);
1053 case SND_SOC_DAPM_POST_PMD:
1054 return skl_tplg_pga_dapm_post_pmd_event(w, skl);
1060 static int skl_tplg_tlv_control_get(struct snd_kcontrol *kcontrol,
1061 unsigned int __user *data, unsigned int size)
1063 struct soc_bytes_ext *sb =
1064 (struct soc_bytes_ext *)kcontrol->private_value;
1065 struct skl_algo_data *bc = (struct skl_algo_data *)sb->dobj.private;
1066 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1067 struct skl_module_cfg *mconfig = w->priv;
1068 struct skl *skl = get_skl_ctx(w->dapm->dev);
1071 skl_get_module_params(skl->skl_sst, (u32 *)bc->params,
1072 bc->size, bc->param_id, mconfig);
1074 /* decrement size for TLV header */
1075 size -= 2 * sizeof(u32);
1077 /* check size as we don't want to send kernel data */
1082 if (copy_to_user(data, &bc->param_id, sizeof(u32)))
1084 if (copy_to_user(data + 1, &size, sizeof(u32)))
1086 if (copy_to_user(data + 2, bc->params, size))
1093 #define SKL_PARAM_VENDOR_ID 0xff
1095 static int skl_tplg_tlv_control_set(struct snd_kcontrol *kcontrol,
1096 const unsigned int __user *data, unsigned int size)
1098 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1099 struct skl_module_cfg *mconfig = w->priv;
1100 struct soc_bytes_ext *sb =
1101 (struct soc_bytes_ext *)kcontrol->private_value;
1102 struct skl_algo_data *ac = (struct skl_algo_data *)sb->dobj.private;
1103 struct skl *skl = get_skl_ctx(w->dapm->dev);
1111 * if the param_is is of type Vendor, firmware expects actual
1112 * parameter id and size from the control.
1114 if (ac->param_id == SKL_PARAM_VENDOR_ID) {
1115 if (copy_from_user(ac->params, data, size))
1118 if (copy_from_user(ac->params,
1124 return skl_set_module_params(skl->skl_sst,
1125 (u32 *)ac->params, ac->size,
1126 ac->param_id, mconfig);
1133 * Fill the dma id for host and link. In case of passthrough
1134 * pipeline, this will both host and link in the same
1135 * pipeline, so need to copy the link and host based on dev_type
1137 static void skl_tplg_fill_dma_id(struct skl_module_cfg *mcfg,
1138 struct skl_pipe_params *params)
1140 struct skl_pipe *pipe = mcfg->pipe;
1142 if (pipe->passthru) {
1143 switch (mcfg->dev_type) {
1144 case SKL_DEVICE_HDALINK:
1145 pipe->p_params->link_dma_id = params->link_dma_id;
1148 case SKL_DEVICE_HDAHOST:
1149 pipe->p_params->host_dma_id = params->host_dma_id;
1155 pipe->p_params->s_fmt = params->s_fmt;
1156 pipe->p_params->ch = params->ch;
1157 pipe->p_params->s_freq = params->s_freq;
1158 pipe->p_params->stream = params->stream;
1161 memcpy(pipe->p_params, params, sizeof(*params));
1166 * The FE params are passed by hw_params of the DAI.
1167 * On hw_params, the params are stored in Gateway module of the FE and we
1168 * need to calculate the format in DSP module configuration, that
1169 * conversion is done here
1171 int skl_tplg_update_pipe_params(struct device *dev,
1172 struct skl_module_cfg *mconfig,
1173 struct skl_pipe_params *params)
1175 struct skl_module_fmt *format = NULL;
1177 skl_tplg_fill_dma_id(mconfig, params);
1179 if (params->stream == SNDRV_PCM_STREAM_PLAYBACK)
1180 format = &mconfig->in_fmt[0];
1182 format = &mconfig->out_fmt[0];
1184 /* set the hw_params */
1185 format->s_freq = params->s_freq;
1186 format->channels = params->ch;
1187 format->valid_bit_depth = skl_get_bit_depth(params->s_fmt);
1190 * 16 bit is 16 bit container whereas 24 bit is in 32 bit
1191 * container so update bit depth accordingly
1193 switch (format->valid_bit_depth) {
1194 case SKL_DEPTH_16BIT:
1195 format->bit_depth = format->valid_bit_depth;
1198 case SKL_DEPTH_24BIT:
1199 case SKL_DEPTH_32BIT:
1200 format->bit_depth = SKL_DEPTH_32BIT;
1204 dev_err(dev, "Invalid bit depth %x for pipe\n",
1205 format->valid_bit_depth);
1209 if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1210 mconfig->ibs = (format->s_freq / 1000) *
1211 (format->channels) *
1212 (format->bit_depth >> 3);
1214 mconfig->obs = (format->s_freq / 1000) *
1215 (format->channels) *
1216 (format->bit_depth >> 3);
1223 * Query the module config for the FE DAI
1224 * This is used to find the hw_params set for that DAI and apply to FE
1227 struct skl_module_cfg *
1228 skl_tplg_fe_get_cpr_module(struct snd_soc_dai *dai, int stream)
1230 struct snd_soc_dapm_widget *w;
1231 struct snd_soc_dapm_path *p = NULL;
1233 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1234 w = dai->playback_widget;
1235 snd_soc_dapm_widget_for_each_sink_path(w, p) {
1236 if (p->connect && p->sink->power &&
1237 !is_skl_dsp_widget_type(p->sink))
1240 if (p->sink->priv) {
1241 dev_dbg(dai->dev, "set params for %s\n",
1243 return p->sink->priv;
1247 w = dai->capture_widget;
1248 snd_soc_dapm_widget_for_each_source_path(w, p) {
1249 if (p->connect && p->source->power &&
1250 !is_skl_dsp_widget_type(p->source))
1253 if (p->source->priv) {
1254 dev_dbg(dai->dev, "set params for %s\n",
1256 return p->source->priv;
1264 static struct skl_module_cfg *skl_get_mconfig_pb_cpr(
1265 struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
1267 struct snd_soc_dapm_path *p;
1268 struct skl_module_cfg *mconfig = NULL;
1270 snd_soc_dapm_widget_for_each_source_path(w, p) {
1271 if (w->endpoints[SND_SOC_DAPM_DIR_OUT] > 0) {
1273 (p->sink->id == snd_soc_dapm_aif_out) &&
1275 mconfig = p->source->priv;
1278 mconfig = skl_get_mconfig_pb_cpr(dai, p->source);
1286 static struct skl_module_cfg *skl_get_mconfig_cap_cpr(
1287 struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
1289 struct snd_soc_dapm_path *p;
1290 struct skl_module_cfg *mconfig = NULL;
1292 snd_soc_dapm_widget_for_each_sink_path(w, p) {
1293 if (w->endpoints[SND_SOC_DAPM_DIR_IN] > 0) {
1295 (p->source->id == snd_soc_dapm_aif_in) &&
1297 mconfig = p->sink->priv;
1300 mconfig = skl_get_mconfig_cap_cpr(dai, p->sink);
1308 struct skl_module_cfg *
1309 skl_tplg_be_get_cpr_module(struct snd_soc_dai *dai, int stream)
1311 struct snd_soc_dapm_widget *w;
1312 struct skl_module_cfg *mconfig;
1314 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1315 w = dai->playback_widget;
1316 mconfig = skl_get_mconfig_pb_cpr(dai, w);
1318 w = dai->capture_widget;
1319 mconfig = skl_get_mconfig_cap_cpr(dai, w);
1324 static u8 skl_tplg_be_link_type(int dev_type)
1330 ret = NHLT_LINK_SSP;
1333 case SKL_DEVICE_DMIC:
1334 ret = NHLT_LINK_DMIC;
1337 case SKL_DEVICE_I2S:
1338 ret = NHLT_LINK_SSP;
1341 case SKL_DEVICE_HDALINK:
1342 ret = NHLT_LINK_HDA;
1346 ret = NHLT_LINK_INVALID;
1354 * Fill the BE gateway parameters
1355 * The BE gateway expects a blob of parameters which are kept in the ACPI
1356 * NHLT blob, so query the blob for interface type (i2s/pdm) and instance.
1357 * The port can have multiple settings so pick based on the PCM
1360 static int skl_tplg_be_fill_pipe_params(struct snd_soc_dai *dai,
1361 struct skl_module_cfg *mconfig,
1362 struct skl_pipe_params *params)
1364 struct nhlt_specific_cfg *cfg;
1365 struct skl *skl = get_skl_ctx(dai->dev);
1366 int link_type = skl_tplg_be_link_type(mconfig->dev_type);
1368 skl_tplg_fill_dma_id(mconfig, params);
1370 if (link_type == NHLT_LINK_HDA)
1373 /* update the blob based on virtual bus_id*/
1374 cfg = skl_get_ep_blob(skl, mconfig->vbus_id, link_type,
1375 params->s_fmt, params->ch,
1376 params->s_freq, params->stream);
1378 mconfig->formats_config.caps_size = cfg->size;
1379 mconfig->formats_config.caps = (u32 *) &cfg->caps;
1381 dev_err(dai->dev, "Blob NULL for id %x type %d dirn %d\n",
1382 mconfig->vbus_id, link_type,
1384 dev_err(dai->dev, "PCM: ch %d, freq %d, fmt %d\n",
1385 params->ch, params->s_freq, params->s_fmt);
1392 static int skl_tplg_be_set_src_pipe_params(struct snd_soc_dai *dai,
1393 struct snd_soc_dapm_widget *w,
1394 struct skl_pipe_params *params)
1396 struct snd_soc_dapm_path *p;
1399 snd_soc_dapm_widget_for_each_source_path(w, p) {
1400 if (p->connect && is_skl_dsp_widget_type(p->source) &&
1403 ret = skl_tplg_be_fill_pipe_params(dai,
1404 p->source->priv, params);
1408 ret = skl_tplg_be_set_src_pipe_params(dai,
1418 static int skl_tplg_be_set_sink_pipe_params(struct snd_soc_dai *dai,
1419 struct snd_soc_dapm_widget *w, struct skl_pipe_params *params)
1421 struct snd_soc_dapm_path *p = NULL;
1424 snd_soc_dapm_widget_for_each_sink_path(w, p) {
1425 if (p->connect && is_skl_dsp_widget_type(p->sink) &&
1428 ret = skl_tplg_be_fill_pipe_params(dai,
1429 p->sink->priv, params);
1433 ret = skl_tplg_be_set_sink_pipe_params(
1434 dai, p->sink, params);
1444 * BE hw_params can be a source parameters (capture) or sink parameters
1445 * (playback). Based on sink and source we need to either find the source
1446 * list or the sink list and set the pipeline parameters
1448 int skl_tplg_be_update_params(struct snd_soc_dai *dai,
1449 struct skl_pipe_params *params)
1451 struct snd_soc_dapm_widget *w;
1453 if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1454 w = dai->playback_widget;
1456 return skl_tplg_be_set_src_pipe_params(dai, w, params);
1459 w = dai->capture_widget;
1461 return skl_tplg_be_set_sink_pipe_params(dai, w, params);
1467 static const struct snd_soc_tplg_widget_events skl_tplg_widget_ops[] = {
1468 {SKL_MIXER_EVENT, skl_tplg_mixer_event},
1469 {SKL_VMIXER_EVENT, skl_tplg_vmixer_event},
1470 {SKL_PGA_EVENT, skl_tplg_pga_event},
1473 static const struct snd_soc_tplg_bytes_ext_ops skl_tlv_ops[] = {
1474 {SKL_CONTROL_TYPE_BYTE_TLV, skl_tplg_tlv_control_get,
1475 skl_tplg_tlv_control_set},
1478 static int skl_tplg_fill_pipe_tkn(struct device *dev,
1479 struct skl_pipe *pipe, u32 tkn,
1484 case SKL_TKN_U32_PIPE_CONN_TYPE:
1485 pipe->conn_type = tkn_val;
1488 case SKL_TKN_U32_PIPE_PRIORITY:
1489 pipe->pipe_priority = tkn_val;
1492 case SKL_TKN_U32_PIPE_MEM_PGS:
1493 pipe->memory_pages = tkn_val;
1497 dev_err(dev, "Token not handled %d\n", tkn);
1505 * Add pipeline by parsing the relevant tokens
1506 * Return an existing pipe if the pipe already exists.
1508 static int skl_tplg_add_pipe(struct device *dev,
1509 struct skl_module_cfg *mconfig, struct skl *skl,
1510 struct snd_soc_tplg_vendor_value_elem *tkn_elem)
1512 struct skl_pipeline *ppl;
1513 struct skl_pipe *pipe;
1514 struct skl_pipe_params *params;
1516 list_for_each_entry(ppl, &skl->ppl_list, node) {
1517 if (ppl->pipe->ppl_id == tkn_elem->value) {
1518 mconfig->pipe = ppl->pipe;
1523 ppl = devm_kzalloc(dev, sizeof(*ppl), GFP_KERNEL);
1527 pipe = devm_kzalloc(dev, sizeof(*pipe), GFP_KERNEL);
1531 params = devm_kzalloc(dev, sizeof(*params), GFP_KERNEL);
1535 pipe->p_params = params;
1536 pipe->ppl_id = tkn_elem->value;
1537 INIT_LIST_HEAD(&pipe->w_list);
1540 list_add(&ppl->node, &skl->ppl_list);
1542 mconfig->pipe = pipe;
1543 mconfig->pipe->state = SKL_PIPE_INVALID;
1548 static int skl_tplg_fill_pin(struct device *dev, u32 tkn,
1549 struct skl_module_pin *m_pin,
1550 int pin_index, u32 value)
1553 case SKL_TKN_U32_PIN_MOD_ID:
1554 m_pin[pin_index].id.module_id = value;
1557 case SKL_TKN_U32_PIN_INST_ID:
1558 m_pin[pin_index].id.instance_id = value;
1562 dev_err(dev, "%d Not a pin token\n", value);
1570 * Parse for pin config specific tokens to fill up the
1571 * module private data
1573 static int skl_tplg_fill_pins_info(struct device *dev,
1574 struct skl_module_cfg *mconfig,
1575 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
1576 int dir, int pin_count)
1579 struct skl_module_pin *m_pin;
1583 m_pin = mconfig->m_in_pin;
1587 m_pin = mconfig->m_out_pin;
1591 dev_err(dev, "Invalid direction value");
1595 ret = skl_tplg_fill_pin(dev, tkn_elem->token,
1596 m_pin, pin_count, tkn_elem->value);
1601 m_pin[pin_count].in_use = false;
1602 m_pin[pin_count].pin_state = SKL_PIN_UNBIND;
1608 * Fill up input/output module config format based
1611 static int skl_tplg_fill_fmt(struct device *dev,
1612 struct skl_module_cfg *mconfig, u32 tkn,
1613 u32 value, u32 dir, u32 pin_count)
1615 struct skl_module_fmt *dst_fmt;
1619 dst_fmt = mconfig->in_fmt;
1620 dst_fmt += pin_count;
1624 dst_fmt = mconfig->out_fmt;
1625 dst_fmt += pin_count;
1629 dev_err(dev, "Invalid direction value");
1634 case SKL_TKN_U32_FMT_CH:
1635 dst_fmt->channels = value;
1638 case SKL_TKN_U32_FMT_FREQ:
1639 dst_fmt->s_freq = value;
1642 case SKL_TKN_U32_FMT_BIT_DEPTH:
1643 dst_fmt->bit_depth = value;
1646 case SKL_TKN_U32_FMT_SAMPLE_SIZE:
1647 dst_fmt->valid_bit_depth = value;
1650 case SKL_TKN_U32_FMT_CH_CONFIG:
1651 dst_fmt->ch_cfg = value;
1654 case SKL_TKN_U32_FMT_INTERLEAVE:
1655 dst_fmt->interleaving_style = value;
1658 case SKL_TKN_U32_FMT_SAMPLE_TYPE:
1659 dst_fmt->sample_type = value;
1662 case SKL_TKN_U32_FMT_CH_MAP:
1663 dst_fmt->ch_map = value;
1667 dev_err(dev, "Invalid token %d", tkn);
1674 static int skl_tplg_get_uuid(struct device *dev, struct skl_module_cfg *mconfig,
1675 struct snd_soc_tplg_vendor_uuid_elem *uuid_tkn)
1677 if (uuid_tkn->token == SKL_TKN_UUID)
1678 memcpy(&mconfig->guid, &uuid_tkn->uuid, 16);
1680 dev_err(dev, "Not an UUID token tkn %d", uuid_tkn->token);
1687 static void skl_tplg_fill_pin_dynamic_val(
1688 struct skl_module_pin *mpin, u32 pin_count, u32 value)
1692 for (i = 0; i < pin_count; i++)
1693 mpin[i].is_dynamic = value;
1697 * Parse tokens to fill up the module private data
1699 static int skl_tplg_get_token(struct device *dev,
1700 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
1701 struct skl *skl, struct skl_module_cfg *mconfig)
1705 static int is_pipe_exists;
1706 static int pin_index, dir;
1708 if (tkn_elem->token > SKL_TKN_MAX)
1711 switch (tkn_elem->token) {
1712 case SKL_TKN_U8_IN_QUEUE_COUNT:
1713 mconfig->max_in_queue = tkn_elem->value;
1714 mconfig->m_in_pin = devm_kzalloc(dev, mconfig->max_in_queue *
1715 sizeof(*mconfig->m_in_pin),
1717 if (!mconfig->m_in_pin)
1722 case SKL_TKN_U8_OUT_QUEUE_COUNT:
1723 mconfig->max_out_queue = tkn_elem->value;
1724 mconfig->m_out_pin = devm_kzalloc(dev, mconfig->max_out_queue *
1725 sizeof(*mconfig->m_out_pin),
1728 if (!mconfig->m_out_pin)
1733 case SKL_TKN_U8_DYN_IN_PIN:
1734 if (!mconfig->m_in_pin)
1737 skl_tplg_fill_pin_dynamic_val(mconfig->m_in_pin,
1738 mconfig->max_in_queue, tkn_elem->value);
1742 case SKL_TKN_U8_DYN_OUT_PIN:
1743 if (!mconfig->m_out_pin)
1746 skl_tplg_fill_pin_dynamic_val(mconfig->m_out_pin,
1747 mconfig->max_out_queue, tkn_elem->value);
1751 case SKL_TKN_U8_TIME_SLOT:
1752 mconfig->time_slot = tkn_elem->value;
1755 case SKL_TKN_U8_CORE_ID:
1756 mconfig->core_id = tkn_elem->value;
1758 case SKL_TKN_U8_MOD_TYPE:
1759 mconfig->m_type = tkn_elem->value;
1762 case SKL_TKN_U8_DEV_TYPE:
1763 mconfig->dev_type = tkn_elem->value;
1766 case SKL_TKN_U8_HW_CONN_TYPE:
1767 mconfig->hw_conn_type = tkn_elem->value;
1770 case SKL_TKN_U16_MOD_INST_ID:
1771 mconfig->id.instance_id =
1775 case SKL_TKN_U32_MEM_PAGES:
1776 mconfig->mem_pages = tkn_elem->value;
1779 case SKL_TKN_U32_MAX_MCPS:
1780 mconfig->mcps = tkn_elem->value;
1783 case SKL_TKN_U32_OBS:
1784 mconfig->obs = tkn_elem->value;
1787 case SKL_TKN_U32_IBS:
1788 mconfig->ibs = tkn_elem->value;
1791 case SKL_TKN_U32_VBUS_ID:
1792 mconfig->vbus_id = tkn_elem->value;
1795 case SKL_TKN_U32_PARAMS_FIXUP:
1796 mconfig->params_fixup = tkn_elem->value;
1799 case SKL_TKN_U32_CONVERTER:
1800 mconfig->converter = tkn_elem->value;
1803 case SKL_TKN_U32_PIPE_ID:
1804 ret = skl_tplg_add_pipe(dev,
1805 mconfig, skl, tkn_elem);
1808 return is_pipe_exists;
1815 case SKL_TKN_U32_PIPE_CONN_TYPE:
1816 case SKL_TKN_U32_PIPE_PRIORITY:
1817 case SKL_TKN_U32_PIPE_MEM_PGS:
1818 if (is_pipe_exists) {
1819 ret = skl_tplg_fill_pipe_tkn(dev, mconfig->pipe,
1820 tkn_elem->token, tkn_elem->value);
1828 * SKL_TKN_U32_DIR_PIN_COUNT token has the value for both
1829 * direction and the pin count. The first four bits represent
1830 * direction and next four the pin count.
1832 case SKL_TKN_U32_DIR_PIN_COUNT:
1833 dir = tkn_elem->value & SKL_IN_DIR_BIT_MASK;
1834 pin_index = (tkn_elem->value &
1835 SKL_PIN_COUNT_MASK) >> 4;
1839 case SKL_TKN_U32_FMT_CH:
1840 case SKL_TKN_U32_FMT_FREQ:
1841 case SKL_TKN_U32_FMT_BIT_DEPTH:
1842 case SKL_TKN_U32_FMT_SAMPLE_SIZE:
1843 case SKL_TKN_U32_FMT_CH_CONFIG:
1844 case SKL_TKN_U32_FMT_INTERLEAVE:
1845 case SKL_TKN_U32_FMT_SAMPLE_TYPE:
1846 case SKL_TKN_U32_FMT_CH_MAP:
1847 ret = skl_tplg_fill_fmt(dev, mconfig, tkn_elem->token,
1848 tkn_elem->value, dir, pin_index);
1855 case SKL_TKN_U32_PIN_MOD_ID:
1856 case SKL_TKN_U32_PIN_INST_ID:
1857 ret = skl_tplg_fill_pins_info(dev,
1858 mconfig, tkn_elem, dir,
1865 case SKL_TKN_U32_CAPS_SIZE:
1866 mconfig->formats_config.caps_size =
1871 case SKL_TKN_U32_PROC_DOMAIN:
1877 case SKL_TKN_U8_IN_PIN_TYPE:
1878 case SKL_TKN_U8_OUT_PIN_TYPE:
1879 case SKL_TKN_U8_CONN_TYPE:
1883 dev_err(dev, "Token %d not handled\n",
1894 * Parse the vendor array for specific tokens to construct
1895 * module private data
1897 static int skl_tplg_get_tokens(struct device *dev,
1898 char *pvt_data, struct skl *skl,
1899 struct skl_module_cfg *mconfig, int block_size)
1901 struct snd_soc_tplg_vendor_array *array;
1902 struct snd_soc_tplg_vendor_value_elem *tkn_elem;
1903 int tkn_count = 0, ret;
1904 int off = 0, tuple_size = 0;
1906 if (block_size <= 0)
1909 while (tuple_size < block_size) {
1910 array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off);
1914 switch (array->type) {
1915 case SND_SOC_TPLG_TUPLE_TYPE_STRING:
1916 dev_warn(dev, "no string tokens expected for skl tplg");
1919 case SND_SOC_TPLG_TUPLE_TYPE_UUID:
1920 ret = skl_tplg_get_uuid(dev, mconfig, array->uuid);
1924 tuple_size += sizeof(*array->uuid);
1929 tkn_elem = array->value;
1934 while (tkn_count <= (array->num_elems - 1)) {
1935 ret = skl_tplg_get_token(dev, tkn_elem,
1941 tkn_count = tkn_count + ret;
1945 tuple_size += tkn_count * sizeof(*tkn_elem);
1952 * Every data block is preceded by a descriptor to read the number
1953 * of data blocks, they type of the block and it's size
1955 static int skl_tplg_get_desc_blocks(struct device *dev,
1956 struct snd_soc_tplg_vendor_array *array)
1958 struct snd_soc_tplg_vendor_value_elem *tkn_elem;
1960 tkn_elem = array->value;
1962 switch (tkn_elem->token) {
1963 case SKL_TKN_U8_NUM_BLOCKS:
1964 case SKL_TKN_U8_BLOCK_TYPE:
1965 case SKL_TKN_U16_BLOCK_SIZE:
1966 return tkn_elem->value;
1969 dev_err(dev, "Invalid descriptor token %d", tkn_elem->token);
1977 * Parse the private data for the token and corresponding value.
1978 * The private data can have multiple data blocks. So, a data block
1979 * is preceded by a descriptor for number of blocks and a descriptor
1980 * for the type and size of the suceeding data block.
1982 static int skl_tplg_get_pvt_data(struct snd_soc_tplg_dapm_widget *tplg_w,
1983 struct skl *skl, struct device *dev,
1984 struct skl_module_cfg *mconfig)
1986 struct snd_soc_tplg_vendor_array *array;
1987 int num_blocks, block_size = 0, block_type, off = 0;
1991 /* Read the NUM_DATA_BLOCKS descriptor */
1992 array = (struct snd_soc_tplg_vendor_array *)tplg_w->priv.data;
1993 ret = skl_tplg_get_desc_blocks(dev, array);
1999 array = (struct snd_soc_tplg_vendor_array *)(tplg_w->priv.data + off);
2001 /* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */
2002 while (num_blocks > 0) {
2003 ret = skl_tplg_get_desc_blocks(dev, array);
2010 array = (struct snd_soc_tplg_vendor_array *)
2011 (tplg_w->priv.data + off);
2013 ret = skl_tplg_get_desc_blocks(dev, array);
2020 array = (struct snd_soc_tplg_vendor_array *)
2021 (tplg_w->priv.data + off);
2023 data = (tplg_w->priv.data + off);
2025 if (block_type == SKL_TYPE_TUPLE) {
2026 ret = skl_tplg_get_tokens(dev, data,
2027 skl, mconfig, block_size);
2034 if (mconfig->formats_config.caps_size > 0)
2035 memcpy(mconfig->formats_config.caps, data,
2036 mconfig->formats_config.caps_size);
2044 static void skl_clear_pin_config(struct snd_soc_platform *platform,
2045 struct snd_soc_dapm_widget *w)
2048 struct skl_module_cfg *mconfig;
2049 struct skl_pipe *pipe;
2051 if (!strncmp(w->dapm->component->name, platform->component.name,
2052 strlen(platform->component.name))) {
2054 pipe = mconfig->pipe;
2055 for (i = 0; i < mconfig->max_in_queue; i++) {
2056 mconfig->m_in_pin[i].in_use = false;
2057 mconfig->m_in_pin[i].pin_state = SKL_PIN_UNBIND;
2059 for (i = 0; i < mconfig->max_out_queue; i++) {
2060 mconfig->m_out_pin[i].in_use = false;
2061 mconfig->m_out_pin[i].pin_state = SKL_PIN_UNBIND;
2063 pipe->state = SKL_PIPE_INVALID;
2064 mconfig->m_state = SKL_MODULE_UNINIT;
2068 void skl_cleanup_resources(struct skl *skl)
2070 struct skl_sst *ctx = skl->skl_sst;
2071 struct snd_soc_platform *soc_platform = skl->platform;
2072 struct snd_soc_dapm_widget *w;
2073 struct snd_soc_card *card;
2075 if (soc_platform == NULL)
2078 card = soc_platform->component.card;
2079 if (!card || !card->instantiated)
2082 skl->resource.mem = 0;
2083 skl->resource.mcps = 0;
2085 list_for_each_entry(w, &card->widgets, list) {
2086 if (is_skl_dsp_widget_type(w) && (w->priv != NULL))
2087 skl_clear_pin_config(soc_platform, w);
2090 skl_clear_module_cnt(ctx->dsp);
2094 * Topology core widget load callback
2096 * This is used to save the private data for each widget which gives
2097 * information to the driver about module and pipeline parameters which DSP
2098 * FW expects like ids, resource values, formats etc
2100 static int skl_tplg_widget_load(struct snd_soc_component *cmpnt,
2101 struct snd_soc_dapm_widget *w,
2102 struct snd_soc_tplg_dapm_widget *tplg_w)
2105 struct hdac_ext_bus *ebus = snd_soc_component_get_drvdata(cmpnt);
2106 struct skl *skl = ebus_to_skl(ebus);
2107 struct hdac_bus *bus = ebus_to_hbus(ebus);
2108 struct skl_module_cfg *mconfig;
2110 if (!tplg_w->priv.size)
2113 mconfig = devm_kzalloc(bus->dev, sizeof(*mconfig), GFP_KERNEL);
2121 * module binary can be loaded later, so set it to query when
2122 * module is load for a use case
2124 mconfig->id.module_id = -1;
2126 /* Parse private data for tuples */
2127 ret = skl_tplg_get_pvt_data(tplg_w, skl, bus->dev, mconfig);
2131 if (tplg_w->event_type == 0) {
2132 dev_dbg(bus->dev, "ASoC: No event handler required\n");
2136 ret = snd_soc_tplg_widget_bind_event(w, skl_tplg_widget_ops,
2137 ARRAY_SIZE(skl_tplg_widget_ops),
2138 tplg_w->event_type);
2141 dev_err(bus->dev, "%s: No matching event handlers found for %d\n",
2142 __func__, tplg_w->event_type);
2149 static int skl_init_algo_data(struct device *dev, struct soc_bytes_ext *be,
2150 struct snd_soc_tplg_bytes_control *bc)
2152 struct skl_algo_data *ac;
2153 struct skl_dfw_algo_data *dfw_ac =
2154 (struct skl_dfw_algo_data *)bc->priv.data;
2156 ac = devm_kzalloc(dev, sizeof(*ac), GFP_KERNEL);
2160 /* Fill private data */
2161 ac->max = dfw_ac->max;
2162 ac->param_id = dfw_ac->param_id;
2163 ac->set_params = dfw_ac->set_params;
2164 ac->size = dfw_ac->max;
2167 ac->params = (char *) devm_kzalloc(dev, ac->max, GFP_KERNEL);
2171 memcpy(ac->params, dfw_ac->params, ac->max);
2174 be->dobj.private = ac;
2178 static int skl_tplg_control_load(struct snd_soc_component *cmpnt,
2179 struct snd_kcontrol_new *kctl,
2180 struct snd_soc_tplg_ctl_hdr *hdr)
2182 struct soc_bytes_ext *sb;
2183 struct snd_soc_tplg_bytes_control *tplg_bc;
2184 struct hdac_ext_bus *ebus = snd_soc_component_get_drvdata(cmpnt);
2185 struct hdac_bus *bus = ebus_to_hbus(ebus);
2187 switch (hdr->ops.info) {
2188 case SND_SOC_TPLG_CTL_BYTES:
2189 tplg_bc = container_of(hdr,
2190 struct snd_soc_tplg_bytes_control, hdr);
2191 if (kctl->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2192 sb = (struct soc_bytes_ext *)kctl->private_value;
2193 if (tplg_bc->priv.size)
2194 return skl_init_algo_data(
2195 bus->dev, sb, tplg_bc);
2200 dev_warn(bus->dev, "Control load not supported %d:%d:%d\n",
2201 hdr->ops.get, hdr->ops.put, hdr->ops.info);
2208 static int skl_tplg_fill_str_mfest_tkn(struct device *dev,
2209 struct snd_soc_tplg_vendor_string_elem *str_elem,
2210 struct skl_dfw_manifest *minfo)
2213 static int ref_count;
2215 switch (str_elem->token) {
2216 case SKL_TKN_STR_LIB_NAME:
2217 if (ref_count > minfo->lib_count - 1) {
2222 strncpy(minfo->lib[ref_count].name, str_elem->string,
2223 ARRAY_SIZE(minfo->lib[ref_count].name));
2229 dev_err(dev, "Not a string token %d", str_elem->token);
2236 static int skl_tplg_get_str_tkn(struct device *dev,
2237 struct snd_soc_tplg_vendor_array *array,
2238 struct skl_dfw_manifest *minfo)
2240 int tkn_count = 0, ret;
2241 struct snd_soc_tplg_vendor_string_elem *str_elem;
2243 str_elem = (struct snd_soc_tplg_vendor_string_elem *)array->value;
2244 while (tkn_count < array->num_elems) {
2245 ret = skl_tplg_fill_str_mfest_tkn(dev, str_elem, minfo);
2251 tkn_count = tkn_count + ret;
2257 static int skl_tplg_get_int_tkn(struct device *dev,
2258 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2259 struct skl_dfw_manifest *minfo)
2263 switch (tkn_elem->token) {
2264 case SKL_TKN_U32_LIB_COUNT:
2265 minfo->lib_count = tkn_elem->value;
2270 dev_err(dev, "Not a manifest token %d", tkn_elem->token);
2278 * Fill the manifest structure by parsing the tokens based on the
2281 static int skl_tplg_get_manifest_tkn(struct device *dev,
2282 char *pvt_data, struct skl_dfw_manifest *minfo,
2285 int tkn_count = 0, ret;
2286 int off = 0, tuple_size = 0;
2287 struct snd_soc_tplg_vendor_array *array;
2288 struct snd_soc_tplg_vendor_value_elem *tkn_elem;
2290 if (block_size <= 0)
2293 while (tuple_size < block_size) {
2294 array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off);
2296 switch (array->type) {
2297 case SND_SOC_TPLG_TUPLE_TYPE_STRING:
2298 ret = skl_tplg_get_str_tkn(dev, array, minfo);
2304 tuple_size += tkn_count *
2305 sizeof(struct snd_soc_tplg_vendor_string_elem);
2308 case SND_SOC_TPLG_TUPLE_TYPE_UUID:
2309 dev_warn(dev, "no uuid tokens for skl tplf manifest");
2313 tkn_elem = array->value;
2318 while (tkn_count <= array->num_elems - 1) {
2319 ret = skl_tplg_get_int_tkn(dev,
2324 tkn_count = tkn_count + ret;
2326 tuple_size += tkn_count *
2327 sizeof(struct snd_soc_tplg_vendor_value_elem);
2337 * Parse manifest private data for tokens. The private data block is
2338 * preceded by descriptors for type and size of data block.
2340 static int skl_tplg_get_manifest_data(struct snd_soc_tplg_manifest *manifest,
2341 struct device *dev, struct skl_dfw_manifest *minfo)
2343 struct snd_soc_tplg_vendor_array *array;
2344 int num_blocks, block_size = 0, block_type, off = 0;
2348 /* Read the NUM_DATA_BLOCKS descriptor */
2349 array = (struct snd_soc_tplg_vendor_array *)manifest->priv.data;
2350 ret = skl_tplg_get_desc_blocks(dev, array);
2356 array = (struct snd_soc_tplg_vendor_array *)
2357 (manifest->priv.data + off);
2359 /* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */
2360 while (num_blocks > 0) {
2361 ret = skl_tplg_get_desc_blocks(dev, array);
2368 array = (struct snd_soc_tplg_vendor_array *)
2369 (manifest->priv.data + off);
2371 ret = skl_tplg_get_desc_blocks(dev, array);
2378 array = (struct snd_soc_tplg_vendor_array *)
2379 (manifest->priv.data + off);
2381 data = (manifest->priv.data + off);
2383 if (block_type == SKL_TYPE_TUPLE) {
2384 ret = skl_tplg_get_manifest_tkn(dev, data, minfo,
2399 static int skl_manifest_load(struct snd_soc_component *cmpnt,
2400 struct snd_soc_tplg_manifest *manifest)
2402 struct skl_dfw_manifest *minfo;
2403 struct hdac_ext_bus *ebus = snd_soc_component_get_drvdata(cmpnt);
2404 struct hdac_bus *bus = ebus_to_hbus(ebus);
2405 struct skl *skl = ebus_to_skl(ebus);
2408 /* proceed only if we have private data defined */
2409 if (manifest->priv.size == 0)
2412 minfo = &skl->skl_sst->manifest;
2414 skl_tplg_get_manifest_data(manifest, bus->dev, minfo);
2416 if (minfo->lib_count > HDA_MAX_LIB) {
2417 dev_err(bus->dev, "Exceeding max Library count. Got:%d\n",
2425 static struct snd_soc_tplg_ops skl_tplg_ops = {
2426 .widget_load = skl_tplg_widget_load,
2427 .control_load = skl_tplg_control_load,
2428 .bytes_ext_ops = skl_tlv_ops,
2429 .bytes_ext_ops_count = ARRAY_SIZE(skl_tlv_ops),
2430 .manifest = skl_manifest_load,
2434 * A pipe can have multiple modules, each of them will be a DAPM widget as
2435 * well. While managing a pipeline we need to get the list of all the
2436 * widgets in a pipelines, so this helper - skl_tplg_create_pipe_widget_list()
2437 * helps to get the SKL type widgets in that pipeline
2439 static int skl_tplg_create_pipe_widget_list(struct snd_soc_platform *platform)
2441 struct snd_soc_dapm_widget *w;
2442 struct skl_module_cfg *mcfg = NULL;
2443 struct skl_pipe_module *p_module = NULL;
2444 struct skl_pipe *pipe;
2446 list_for_each_entry(w, &platform->component.card->widgets, list) {
2447 if (is_skl_dsp_widget_type(w) && w->priv != NULL) {
2451 p_module = devm_kzalloc(platform->dev,
2452 sizeof(*p_module), GFP_KERNEL);
2457 list_add_tail(&p_module->node, &pipe->w_list);
2464 static void skl_tplg_set_pipe_type(struct skl *skl, struct skl_pipe *pipe)
2466 struct skl_pipe_module *w_module;
2467 struct snd_soc_dapm_widget *w;
2468 struct skl_module_cfg *mconfig;
2469 bool host_found = false, link_found = false;
2471 list_for_each_entry(w_module, &pipe->w_list, node) {
2475 if (mconfig->dev_type == SKL_DEVICE_HDAHOST)
2477 else if (mconfig->dev_type != SKL_DEVICE_NONE)
2481 if (host_found && link_found)
2482 pipe->passthru = true;
2484 pipe->passthru = false;
2487 /* This will be read from topology manifest, currently defined here */
2488 #define SKL_MAX_MCPS 30000000
2489 #define SKL_FW_MAX_MEM 1000000
2492 * SKL topology init routine
2494 int skl_tplg_init(struct snd_soc_platform *platform, struct hdac_ext_bus *ebus)
2497 const struct firmware *fw;
2498 struct hdac_bus *bus = ebus_to_hbus(ebus);
2499 struct skl *skl = ebus_to_skl(ebus);
2500 struct skl_pipeline *ppl;
2502 ret = request_firmware(&fw, skl->tplg_name, bus->dev);
2504 dev_err(bus->dev, "tplg fw %s load failed with %d\n",
2505 skl->tplg_name, ret);
2506 ret = request_firmware(&fw, "dfw_sst.bin", bus->dev);
2508 dev_err(bus->dev, "Fallback tplg fw %s load failed with %d\n",
2509 "dfw_sst.bin", ret);
2515 * The complete tplg for SKL is loaded as index 0, we don't use
2518 ret = snd_soc_tplg_component_load(&platform->component,
2519 &skl_tplg_ops, fw, 0);
2521 dev_err(bus->dev, "tplg component load failed%d\n", ret);
2522 release_firmware(fw);
2526 skl->resource.max_mcps = SKL_MAX_MCPS;
2527 skl->resource.max_mem = SKL_FW_MAX_MEM;
2530 ret = skl_tplg_create_pipe_widget_list(platform);
2534 list_for_each_entry(ppl, &skl->ppl_list, node)
2535 skl_tplg_set_pipe_type(skl, ppl->pipe);