treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / sound / soc / intel / skylake / skl-topology.c
blob69cd7a81bf2ac26062948dd415cc7950033787ec
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * skl-topology.c - Implements Platform component ALSA controls/widget
4 * handlers.
6 * Copyright (C) 2014-2015 Intel Corp
7 * Author: Jeeja KP <jeeja.kp@intel.com>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 */
11 #include <linux/slab.h>
12 #include <linux/types.h>
13 #include <linux/firmware.h>
14 #include <linux/uuid.h>
15 #include <sound/intel-nhlt.h>
16 #include <sound/soc.h>
17 #include <sound/soc-topology.h>
18 #include <uapi/sound/snd_sst_tokens.h>
19 #include <uapi/sound/skl-tplg-interface.h>
20 #include "skl-sst-dsp.h"
21 #include "skl-sst-ipc.h"
22 #include "skl-topology.h"
23 #include "skl.h"
24 #include "../common/sst-dsp.h"
25 #include "../common/sst-dsp-priv.h"
27 #define SKL_CH_FIXUP_MASK (1 << 0)
28 #define SKL_RATE_FIXUP_MASK (1 << 1)
29 #define SKL_FMT_FIXUP_MASK (1 << 2)
30 #define SKL_IN_DIR_BIT_MASK BIT(0)
31 #define SKL_PIN_COUNT_MASK GENMASK(7, 4)
33 static const int mic_mono_list[] = {
34 0, 1, 2, 3,
36 static const int mic_stereo_list[][SKL_CH_STEREO] = {
37 {0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 3}, {2, 3},
39 static const int mic_trio_list[][SKL_CH_TRIO] = {
40 {0, 1, 2}, {0, 1, 3}, {0, 2, 3}, {1, 2, 3},
42 static const int mic_quatro_list[][SKL_CH_QUATRO] = {
43 {0, 1, 2, 3},
46 #define CHECK_HW_PARAMS(ch, freq, bps, prm_ch, prm_freq, prm_bps) \
47 ((ch == prm_ch) && (bps == prm_bps) && (freq == prm_freq))
49 void skl_tplg_d0i3_get(struct skl_dev *skl, enum d0i3_capability caps)
51 struct skl_d0i3_data *d0i3 = &skl->d0i3;
53 switch (caps) {
54 case SKL_D0I3_NONE:
55 d0i3->non_d0i3++;
56 break;
58 case SKL_D0I3_STREAMING:
59 d0i3->streaming++;
60 break;
62 case SKL_D0I3_NON_STREAMING:
63 d0i3->non_streaming++;
64 break;
68 void skl_tplg_d0i3_put(struct skl_dev *skl, enum d0i3_capability caps)
70 struct skl_d0i3_data *d0i3 = &skl->d0i3;
72 switch (caps) {
73 case SKL_D0I3_NONE:
74 d0i3->non_d0i3--;
75 break;
77 case SKL_D0I3_STREAMING:
78 d0i3->streaming--;
79 break;
81 case SKL_D0I3_NON_STREAMING:
82 d0i3->non_streaming--;
83 break;
88 * SKL DSP driver modelling uses only few DAPM widgets so for rest we will
89 * ignore. This helpers checks if the SKL driver handles this widget type
91 static int is_skl_dsp_widget_type(struct snd_soc_dapm_widget *w,
92 struct device *dev)
94 if (w->dapm->dev != dev)
95 return false;
97 switch (w->id) {
98 case snd_soc_dapm_dai_link:
99 case snd_soc_dapm_dai_in:
100 case snd_soc_dapm_aif_in:
101 case snd_soc_dapm_aif_out:
102 case snd_soc_dapm_dai_out:
103 case snd_soc_dapm_switch:
104 case snd_soc_dapm_output:
105 case snd_soc_dapm_mux:
107 return false;
108 default:
109 return true;
113 static void skl_dump_mconfig(struct skl_dev *skl, struct skl_module_cfg *mcfg)
115 struct skl_module_iface *iface = &mcfg->module->formats[0];
117 dev_dbg(skl->dev, "Dumping config\n");
118 dev_dbg(skl->dev, "Input Format:\n");
119 dev_dbg(skl->dev, "channels = %d\n", iface->inputs[0].fmt.channels);
120 dev_dbg(skl->dev, "s_freq = %d\n", iface->inputs[0].fmt.s_freq);
121 dev_dbg(skl->dev, "ch_cfg = %d\n", iface->inputs[0].fmt.ch_cfg);
122 dev_dbg(skl->dev, "valid bit depth = %d\n",
123 iface->inputs[0].fmt.valid_bit_depth);
124 dev_dbg(skl->dev, "Output Format:\n");
125 dev_dbg(skl->dev, "channels = %d\n", iface->outputs[0].fmt.channels);
126 dev_dbg(skl->dev, "s_freq = %d\n", iface->outputs[0].fmt.s_freq);
127 dev_dbg(skl->dev, "valid bit depth = %d\n",
128 iface->outputs[0].fmt.valid_bit_depth);
129 dev_dbg(skl->dev, "ch_cfg = %d\n", iface->outputs[0].fmt.ch_cfg);
132 static void skl_tplg_update_chmap(struct skl_module_fmt *fmt, int chs)
134 int slot_map = 0xFFFFFFFF;
135 int start_slot = 0;
136 int i;
138 for (i = 0; i < chs; i++) {
140 * For 2 channels with starting slot as 0, slot map will
141 * look like 0xFFFFFF10.
143 slot_map &= (~(0xF << (4 * i)) | (start_slot << (4 * i)));
144 start_slot++;
146 fmt->ch_map = slot_map;
149 static void skl_tplg_update_params(struct skl_module_fmt *fmt,
150 struct skl_pipe_params *params, int fixup)
152 if (fixup & SKL_RATE_FIXUP_MASK)
153 fmt->s_freq = params->s_freq;
154 if (fixup & SKL_CH_FIXUP_MASK) {
155 fmt->channels = params->ch;
156 skl_tplg_update_chmap(fmt, fmt->channels);
158 if (fixup & SKL_FMT_FIXUP_MASK) {
159 fmt->valid_bit_depth = skl_get_bit_depth(params->s_fmt);
162 * 16 bit is 16 bit container whereas 24 bit is in 32 bit
163 * container so update bit depth accordingly
165 switch (fmt->valid_bit_depth) {
166 case SKL_DEPTH_16BIT:
167 fmt->bit_depth = fmt->valid_bit_depth;
168 break;
170 default:
171 fmt->bit_depth = SKL_DEPTH_32BIT;
172 break;
179 * A pipeline may have modules which impact the pcm parameters, like SRC,
180 * channel converter, format converter.
181 * We need to calculate the output params by applying the 'fixup'
182 * Topology will tell driver which type of fixup is to be applied by
183 * supplying the fixup mask, so based on that we calculate the output
185 * Now In FE the pcm hw_params is source/target format. Same is applicable
186 * for BE with its hw_params invoked.
187 * here based on FE, BE pipeline and direction we calculate the input and
188 * outfix and then apply that for a module
190 static void skl_tplg_update_params_fixup(struct skl_module_cfg *m_cfg,
191 struct skl_pipe_params *params, bool is_fe)
193 int in_fixup, out_fixup;
194 struct skl_module_fmt *in_fmt, *out_fmt;
196 /* Fixups will be applied to pin 0 only */
197 in_fmt = &m_cfg->module->formats[0].inputs[0].fmt;
198 out_fmt = &m_cfg->module->formats[0].outputs[0].fmt;
200 if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
201 if (is_fe) {
202 in_fixup = m_cfg->params_fixup;
203 out_fixup = (~m_cfg->converter) &
204 m_cfg->params_fixup;
205 } else {
206 out_fixup = m_cfg->params_fixup;
207 in_fixup = (~m_cfg->converter) &
208 m_cfg->params_fixup;
210 } else {
211 if (is_fe) {
212 out_fixup = m_cfg->params_fixup;
213 in_fixup = (~m_cfg->converter) &
214 m_cfg->params_fixup;
215 } else {
216 in_fixup = m_cfg->params_fixup;
217 out_fixup = (~m_cfg->converter) &
218 m_cfg->params_fixup;
222 skl_tplg_update_params(in_fmt, params, in_fixup);
223 skl_tplg_update_params(out_fmt, params, out_fixup);
227 * A module needs input and output buffers, which are dependent upon pcm
228 * params, so once we have calculate params, we need buffer calculation as
229 * well.
231 static void skl_tplg_update_buffer_size(struct skl_dev *skl,
232 struct skl_module_cfg *mcfg)
234 int multiplier = 1;
235 struct skl_module_fmt *in_fmt, *out_fmt;
236 struct skl_module_res *res;
238 /* Since fixups is applied to pin 0 only, ibs, obs needs
239 * change for pin 0 only
241 res = &mcfg->module->resources[0];
242 in_fmt = &mcfg->module->formats[0].inputs[0].fmt;
243 out_fmt = &mcfg->module->formats[0].outputs[0].fmt;
245 if (mcfg->m_type == SKL_MODULE_TYPE_SRCINT)
246 multiplier = 5;
248 res->ibs = DIV_ROUND_UP(in_fmt->s_freq, 1000) *
249 in_fmt->channels * (in_fmt->bit_depth >> 3) *
250 multiplier;
252 res->obs = DIV_ROUND_UP(out_fmt->s_freq, 1000) *
253 out_fmt->channels * (out_fmt->bit_depth >> 3) *
254 multiplier;
257 static u8 skl_tplg_be_dev_type(int dev_type)
259 int ret;
261 switch (dev_type) {
262 case SKL_DEVICE_BT:
263 ret = NHLT_DEVICE_BT;
264 break;
266 case SKL_DEVICE_DMIC:
267 ret = NHLT_DEVICE_DMIC;
268 break;
270 case SKL_DEVICE_I2S:
271 ret = NHLT_DEVICE_I2S;
272 break;
274 default:
275 ret = NHLT_DEVICE_INVALID;
276 break;
279 return ret;
282 static int skl_tplg_update_be_blob(struct snd_soc_dapm_widget *w,
283 struct skl_dev *skl)
285 struct skl_module_cfg *m_cfg = w->priv;
286 int link_type, dir;
287 u32 ch, s_freq, s_fmt;
288 struct nhlt_specific_cfg *cfg;
289 u8 dev_type = skl_tplg_be_dev_type(m_cfg->dev_type);
290 int fmt_idx = m_cfg->fmt_idx;
291 struct skl_module_iface *m_iface = &m_cfg->module->formats[fmt_idx];
293 /* check if we already have blob */
294 if (m_cfg->formats_config.caps_size > 0)
295 return 0;
297 dev_dbg(skl->dev, "Applying default cfg blob\n");
298 switch (m_cfg->dev_type) {
299 case SKL_DEVICE_DMIC:
300 link_type = NHLT_LINK_DMIC;
301 dir = SNDRV_PCM_STREAM_CAPTURE;
302 s_freq = m_iface->inputs[0].fmt.s_freq;
303 s_fmt = m_iface->inputs[0].fmt.bit_depth;
304 ch = m_iface->inputs[0].fmt.channels;
305 break;
307 case SKL_DEVICE_I2S:
308 link_type = NHLT_LINK_SSP;
309 if (m_cfg->hw_conn_type == SKL_CONN_SOURCE) {
310 dir = SNDRV_PCM_STREAM_PLAYBACK;
311 s_freq = m_iface->outputs[0].fmt.s_freq;
312 s_fmt = m_iface->outputs[0].fmt.bit_depth;
313 ch = m_iface->outputs[0].fmt.channels;
314 } else {
315 dir = SNDRV_PCM_STREAM_CAPTURE;
316 s_freq = m_iface->inputs[0].fmt.s_freq;
317 s_fmt = m_iface->inputs[0].fmt.bit_depth;
318 ch = m_iface->inputs[0].fmt.channels;
320 break;
322 default:
323 return -EINVAL;
326 /* update the blob based on virtual bus_id and default params */
327 cfg = skl_get_ep_blob(skl, m_cfg->vbus_id, link_type,
328 s_fmt, ch, s_freq, dir, dev_type);
329 if (cfg) {
330 m_cfg->formats_config.caps_size = cfg->size;
331 m_cfg->formats_config.caps = (u32 *) &cfg->caps;
332 } else {
333 dev_err(skl->dev, "Blob NULL for id %x type %d dirn %d\n",
334 m_cfg->vbus_id, link_type, dir);
335 dev_err(skl->dev, "PCM: ch %d, freq %d, fmt %d\n",
336 ch, s_freq, s_fmt);
337 return -EIO;
340 return 0;
343 static void skl_tplg_update_module_params(struct snd_soc_dapm_widget *w,
344 struct skl_dev *skl)
346 struct skl_module_cfg *m_cfg = w->priv;
347 struct skl_pipe_params *params = m_cfg->pipe->p_params;
348 int p_conn_type = m_cfg->pipe->conn_type;
349 bool is_fe;
351 if (!m_cfg->params_fixup)
352 return;
354 dev_dbg(skl->dev, "Mconfig for widget=%s BEFORE updation\n",
355 w->name);
357 skl_dump_mconfig(skl, m_cfg);
359 if (p_conn_type == SKL_PIPE_CONN_TYPE_FE)
360 is_fe = true;
361 else
362 is_fe = false;
364 skl_tplg_update_params_fixup(m_cfg, params, is_fe);
365 skl_tplg_update_buffer_size(skl, m_cfg);
367 dev_dbg(skl->dev, "Mconfig for widget=%s AFTER updation\n",
368 w->name);
370 skl_dump_mconfig(skl, m_cfg);
374 * some modules can have multiple params set from user control and
375 * need to be set after module is initialized. If set_param flag is
376 * set module params will be done after module is initialised.
378 static int skl_tplg_set_module_params(struct snd_soc_dapm_widget *w,
379 struct skl_dev *skl)
381 int i, ret;
382 struct skl_module_cfg *mconfig = w->priv;
383 const struct snd_kcontrol_new *k;
384 struct soc_bytes_ext *sb;
385 struct skl_algo_data *bc;
386 struct skl_specific_cfg *sp_cfg;
388 if (mconfig->formats_config.caps_size > 0 &&
389 mconfig->formats_config.set_params == SKL_PARAM_SET) {
390 sp_cfg = &mconfig->formats_config;
391 ret = skl_set_module_params(skl, sp_cfg->caps,
392 sp_cfg->caps_size,
393 sp_cfg->param_id, mconfig);
394 if (ret < 0)
395 return ret;
398 for (i = 0; i < w->num_kcontrols; i++) {
399 k = &w->kcontrol_news[i];
400 if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
401 sb = (void *) k->private_value;
402 bc = (struct skl_algo_data *)sb->dobj.private;
404 if (bc->set_params == SKL_PARAM_SET) {
405 ret = skl_set_module_params(skl,
406 (u32 *)bc->params, bc->size,
407 bc->param_id, mconfig);
408 if (ret < 0)
409 return ret;
414 return 0;
418 * some module param can set from user control and this is required as
419 * when module is initailzed. if module param is required in init it is
420 * identifed by set_param flag. if set_param flag is not set, then this
421 * parameter needs to set as part of module init.
423 static int skl_tplg_set_module_init_data(struct snd_soc_dapm_widget *w)
425 const struct snd_kcontrol_new *k;
426 struct soc_bytes_ext *sb;
427 struct skl_algo_data *bc;
428 struct skl_module_cfg *mconfig = w->priv;
429 int i;
431 for (i = 0; i < w->num_kcontrols; i++) {
432 k = &w->kcontrol_news[i];
433 if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
434 sb = (struct soc_bytes_ext *)k->private_value;
435 bc = (struct skl_algo_data *)sb->dobj.private;
437 if (bc->set_params != SKL_PARAM_INIT)
438 continue;
440 mconfig->formats_config.caps = (u32 *)bc->params;
441 mconfig->formats_config.caps_size = bc->size;
443 break;
447 return 0;
450 static int skl_tplg_module_prepare(struct skl_dev *skl, struct skl_pipe *pipe,
451 struct snd_soc_dapm_widget *w, struct skl_module_cfg *mcfg)
453 switch (mcfg->dev_type) {
454 case SKL_DEVICE_HDAHOST:
455 return skl_pcm_host_dma_prepare(skl->dev, pipe->p_params);
457 case SKL_DEVICE_HDALINK:
458 return skl_pcm_link_dma_prepare(skl->dev, pipe->p_params);
461 return 0;
465 * Inside a pipe instance, we can have various modules. These modules need
466 * to instantiated in DSP by invoking INIT_MODULE IPC, which is achieved by
467 * skl_init_module() routine, so invoke that for all modules in a pipeline
469 static int
470 skl_tplg_init_pipe_modules(struct skl_dev *skl, struct skl_pipe *pipe)
472 struct skl_pipe_module *w_module;
473 struct snd_soc_dapm_widget *w;
474 struct skl_module_cfg *mconfig;
475 u8 cfg_idx;
476 int ret = 0;
478 list_for_each_entry(w_module, &pipe->w_list, node) {
479 guid_t *uuid_mod;
480 w = w_module->w;
481 mconfig = w->priv;
483 /* check if module ids are populated */
484 if (mconfig->id.module_id < 0) {
485 dev_err(skl->dev,
486 "module %pUL id not populated\n",
487 (guid_t *)mconfig->guid);
488 return -EIO;
491 cfg_idx = mconfig->pipe->cur_config_idx;
492 mconfig->fmt_idx = mconfig->mod_cfg[cfg_idx].fmt_idx;
493 mconfig->res_idx = mconfig->mod_cfg[cfg_idx].res_idx;
495 if (mconfig->module->loadable && skl->dsp->fw_ops.load_mod) {
496 ret = skl->dsp->fw_ops.load_mod(skl->dsp,
497 mconfig->id.module_id, mconfig->guid);
498 if (ret < 0)
499 return ret;
501 mconfig->m_state = SKL_MODULE_LOADED;
504 /* prepare the DMA if the module is gateway cpr */
505 ret = skl_tplg_module_prepare(skl, pipe, w, mconfig);
506 if (ret < 0)
507 return ret;
509 /* update blob if blob is null for be with default value */
510 skl_tplg_update_be_blob(w, skl);
513 * apply fix/conversion to module params based on
514 * FE/BE params
516 skl_tplg_update_module_params(w, skl);
517 uuid_mod = (guid_t *)mconfig->guid;
518 mconfig->id.pvt_id = skl_get_pvt_id(skl, uuid_mod,
519 mconfig->id.instance_id);
520 if (mconfig->id.pvt_id < 0)
521 return ret;
522 skl_tplg_set_module_init_data(w);
524 ret = skl_dsp_get_core(skl->dsp, mconfig->core_id);
525 if (ret < 0) {
526 dev_err(skl->dev, "Failed to wake up core %d ret=%d\n",
527 mconfig->core_id, ret);
528 return ret;
531 ret = skl_init_module(skl, mconfig);
532 if (ret < 0) {
533 skl_put_pvt_id(skl, uuid_mod, &mconfig->id.pvt_id);
534 goto err;
537 ret = skl_tplg_set_module_params(w, skl);
538 if (ret < 0)
539 goto err;
542 return 0;
543 err:
544 skl_dsp_put_core(skl->dsp, mconfig->core_id);
545 return ret;
548 static int skl_tplg_unload_pipe_modules(struct skl_dev *skl,
549 struct skl_pipe *pipe)
551 int ret = 0;
552 struct skl_pipe_module *w_module = NULL;
553 struct skl_module_cfg *mconfig = NULL;
555 list_for_each_entry(w_module, &pipe->w_list, node) {
556 guid_t *uuid_mod;
557 mconfig = w_module->w->priv;
558 uuid_mod = (guid_t *)mconfig->guid;
560 if (mconfig->module->loadable && skl->dsp->fw_ops.unload_mod &&
561 mconfig->m_state > SKL_MODULE_UNINIT) {
562 ret = skl->dsp->fw_ops.unload_mod(skl->dsp,
563 mconfig->id.module_id);
564 if (ret < 0)
565 return -EIO;
567 skl_put_pvt_id(skl, uuid_mod, &mconfig->id.pvt_id);
569 ret = skl_dsp_put_core(skl->dsp, mconfig->core_id);
570 if (ret < 0) {
571 /* don't return; continue with other modules */
572 dev_err(skl->dev, "Failed to sleep core %d ret=%d\n",
573 mconfig->core_id, ret);
577 /* no modules to unload in this path, so return */
578 return ret;
582 * Here, we select pipe format based on the pipe type and pipe
583 * direction to determine the current config index for the pipeline.
584 * The config index is then used to select proper module resources.
585 * Intermediate pipes currently have a fixed format hence we select the
586 * 0th configuratation by default for such pipes.
588 static int
589 skl_tplg_get_pipe_config(struct skl_dev *skl, struct skl_module_cfg *mconfig)
591 struct skl_pipe *pipe = mconfig->pipe;
592 struct skl_pipe_params *params = pipe->p_params;
593 struct skl_path_config *pconfig = &pipe->configs[0];
594 struct skl_pipe_fmt *fmt = NULL;
595 bool in_fmt = false;
596 int i;
598 if (pipe->nr_cfgs == 0) {
599 pipe->cur_config_idx = 0;
600 return 0;
603 if (pipe->conn_type == SKL_PIPE_CONN_TYPE_NONE) {
604 dev_dbg(skl->dev, "No conn_type detected, take 0th config\n");
605 pipe->cur_config_idx = 0;
606 pipe->memory_pages = pconfig->mem_pages;
608 return 0;
611 if ((pipe->conn_type == SKL_PIPE_CONN_TYPE_FE &&
612 pipe->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
613 (pipe->conn_type == SKL_PIPE_CONN_TYPE_BE &&
614 pipe->direction == SNDRV_PCM_STREAM_CAPTURE))
615 in_fmt = true;
617 for (i = 0; i < pipe->nr_cfgs; i++) {
618 pconfig = &pipe->configs[i];
619 if (in_fmt)
620 fmt = &pconfig->in_fmt;
621 else
622 fmt = &pconfig->out_fmt;
624 if (CHECK_HW_PARAMS(params->ch, params->s_freq, params->s_fmt,
625 fmt->channels, fmt->freq, fmt->bps)) {
626 pipe->cur_config_idx = i;
627 pipe->memory_pages = pconfig->mem_pages;
628 dev_dbg(skl->dev, "Using pipe config: %d\n", i);
630 return 0;
634 dev_err(skl->dev, "Invalid pipe config: %d %d %d for pipe: %d\n",
635 params->ch, params->s_freq, params->s_fmt, pipe->ppl_id);
636 return -EINVAL;
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 * - Create the pipeline
643 * - Initialize the modules in pipeline
644 * - finally bind all modules together
646 static int skl_tplg_mixer_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
647 struct skl_dev *skl)
649 int ret;
650 struct skl_module_cfg *mconfig = w->priv;
651 struct skl_pipe_module *w_module;
652 struct skl_pipe *s_pipe = mconfig->pipe;
653 struct skl_module_cfg *src_module = NULL, *dst_module, *module;
654 struct skl_module_deferred_bind *modules;
656 ret = skl_tplg_get_pipe_config(skl, mconfig);
657 if (ret < 0)
658 return ret;
661 * Create a list of modules for pipe.
662 * This list contains modules from source to sink
664 ret = skl_create_pipeline(skl, mconfig->pipe);
665 if (ret < 0)
666 return ret;
668 /* Init all pipe modules from source to sink */
669 ret = skl_tplg_init_pipe_modules(skl, s_pipe);
670 if (ret < 0)
671 return ret;
673 /* Bind modules from source to sink */
674 list_for_each_entry(w_module, &s_pipe->w_list, node) {
675 dst_module = w_module->w->priv;
677 if (src_module == NULL) {
678 src_module = dst_module;
679 continue;
682 ret = skl_bind_modules(skl, src_module, dst_module);
683 if (ret < 0)
684 return ret;
686 src_module = dst_module;
690 * When the destination module is initialized, check for these modules
691 * in deferred bind list. If found, bind them.
693 list_for_each_entry(w_module, &s_pipe->w_list, node) {
694 if (list_empty(&skl->bind_list))
695 break;
697 list_for_each_entry(modules, &skl->bind_list, node) {
698 module = w_module->w->priv;
699 if (modules->dst == module)
700 skl_bind_modules(skl, modules->src,
701 modules->dst);
705 return 0;
708 static int skl_fill_sink_instance_id(struct skl_dev *skl, u32 *params,
709 int size, struct skl_module_cfg *mcfg)
711 int i, pvt_id;
713 if (mcfg->m_type == SKL_MODULE_TYPE_KPB) {
714 struct skl_kpb_params *kpb_params =
715 (struct skl_kpb_params *)params;
716 struct skl_mod_inst_map *inst = kpb_params->u.map;
718 for (i = 0; i < kpb_params->num_modules; i++) {
719 pvt_id = skl_get_pvt_instance_id_map(skl, inst->mod_id,
720 inst->inst_id);
721 if (pvt_id < 0)
722 return -EINVAL;
724 inst->inst_id = pvt_id;
725 inst++;
729 return 0;
732 * Some modules require params to be set after the module is bound to
733 * all pins connected.
735 * The module provider initializes set_param flag for such modules and we
736 * send params after binding
738 static int skl_tplg_set_module_bind_params(struct snd_soc_dapm_widget *w,
739 struct skl_module_cfg *mcfg, struct skl_dev *skl)
741 int i, ret;
742 struct skl_module_cfg *mconfig = w->priv;
743 const struct snd_kcontrol_new *k;
744 struct soc_bytes_ext *sb;
745 struct skl_algo_data *bc;
746 struct skl_specific_cfg *sp_cfg;
747 u32 *params;
750 * check all out/in pins are in bind state.
751 * if so set the module param
753 for (i = 0; i < mcfg->module->max_output_pins; i++) {
754 if (mcfg->m_out_pin[i].pin_state != SKL_PIN_BIND_DONE)
755 return 0;
758 for (i = 0; i < mcfg->module->max_input_pins; i++) {
759 if (mcfg->m_in_pin[i].pin_state != SKL_PIN_BIND_DONE)
760 return 0;
763 if (mconfig->formats_config.caps_size > 0 &&
764 mconfig->formats_config.set_params == SKL_PARAM_BIND) {
765 sp_cfg = &mconfig->formats_config;
766 ret = skl_set_module_params(skl, sp_cfg->caps,
767 sp_cfg->caps_size,
768 sp_cfg->param_id, mconfig);
769 if (ret < 0)
770 return ret;
773 for (i = 0; i < w->num_kcontrols; i++) {
774 k = &w->kcontrol_news[i];
775 if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
776 sb = (void *) k->private_value;
777 bc = (struct skl_algo_data *)sb->dobj.private;
779 if (bc->set_params == SKL_PARAM_BIND) {
780 params = kmemdup(bc->params, bc->max, GFP_KERNEL);
781 if (!params)
782 return -ENOMEM;
784 skl_fill_sink_instance_id(skl, params, bc->max,
785 mconfig);
787 ret = skl_set_module_params(skl, params,
788 bc->max, bc->param_id, mconfig);
789 kfree(params);
791 if (ret < 0)
792 return ret;
797 return 0;
800 static int skl_get_module_id(struct skl_dev *skl, guid_t *uuid)
802 struct uuid_module *module;
804 list_for_each_entry(module, &skl->uuid_list, list) {
805 if (guid_equal(uuid, &module->uuid))
806 return module->id;
809 return -EINVAL;
812 static int skl_tplg_find_moduleid_from_uuid(struct skl_dev *skl,
813 const struct snd_kcontrol_new *k)
815 struct soc_bytes_ext *sb = (void *) k->private_value;
816 struct skl_algo_data *bc = (struct skl_algo_data *)sb->dobj.private;
817 struct skl_kpb_params *uuid_params, *params;
818 struct hdac_bus *bus = skl_to_bus(skl);
819 int i, size, module_id;
821 if (bc->set_params == SKL_PARAM_BIND && bc->max) {
822 uuid_params = (struct skl_kpb_params *)bc->params;
823 size = struct_size(params, u.map, uuid_params->num_modules);
825 params = devm_kzalloc(bus->dev, size, GFP_KERNEL);
826 if (!params)
827 return -ENOMEM;
829 params->num_modules = uuid_params->num_modules;
831 for (i = 0; i < uuid_params->num_modules; i++) {
832 module_id = skl_get_module_id(skl,
833 &uuid_params->u.map_uuid[i].mod_uuid);
834 if (module_id < 0) {
835 devm_kfree(bus->dev, params);
836 return -EINVAL;
839 params->u.map[i].mod_id = module_id;
840 params->u.map[i].inst_id =
841 uuid_params->u.map_uuid[i].inst_id;
844 devm_kfree(bus->dev, bc->params);
845 bc->params = (char *)params;
846 bc->max = size;
849 return 0;
853 * Retrieve the module id from UUID mentioned in the
854 * post bind params
856 void skl_tplg_add_moduleid_in_bind_params(struct skl_dev *skl,
857 struct snd_soc_dapm_widget *w)
859 struct skl_module_cfg *mconfig = w->priv;
860 int i;
863 * Post bind params are used for only for KPB
864 * to set copier instances to drain the data
865 * in fast mode
867 if (mconfig->m_type != SKL_MODULE_TYPE_KPB)
868 return;
870 for (i = 0; i < w->num_kcontrols; i++)
871 if ((w->kcontrol_news[i].access &
872 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) &&
873 (skl_tplg_find_moduleid_from_uuid(skl,
874 &w->kcontrol_news[i]) < 0))
875 dev_err(skl->dev,
876 "%s: invalid kpb post bind params\n",
877 __func__);
880 static int skl_tplg_module_add_deferred_bind(struct skl_dev *skl,
881 struct skl_module_cfg *src, struct skl_module_cfg *dst)
883 struct skl_module_deferred_bind *m_list, *modules;
884 int i;
886 /* only supported for module with static pin connection */
887 for (i = 0; i < dst->module->max_input_pins; i++) {
888 struct skl_module_pin *pin = &dst->m_in_pin[i];
890 if (pin->is_dynamic)
891 continue;
893 if ((pin->id.module_id == src->id.module_id) &&
894 (pin->id.instance_id == src->id.instance_id)) {
896 if (!list_empty(&skl->bind_list)) {
897 list_for_each_entry(modules, &skl->bind_list, node) {
898 if (modules->src == src && modules->dst == dst)
899 return 0;
903 m_list = kzalloc(sizeof(*m_list), GFP_KERNEL);
904 if (!m_list)
905 return -ENOMEM;
907 m_list->src = src;
908 m_list->dst = dst;
910 list_add(&m_list->node, &skl->bind_list);
914 return 0;
917 static int skl_tplg_bind_sinks(struct snd_soc_dapm_widget *w,
918 struct skl_dev *skl,
919 struct snd_soc_dapm_widget *src_w,
920 struct skl_module_cfg *src_mconfig)
922 struct snd_soc_dapm_path *p;
923 struct snd_soc_dapm_widget *sink = NULL, *next_sink = NULL;
924 struct skl_module_cfg *sink_mconfig;
925 int ret;
927 snd_soc_dapm_widget_for_each_sink_path(w, p) {
928 if (!p->connect)
929 continue;
931 dev_dbg(skl->dev,
932 "%s: src widget=%s\n", __func__, w->name);
933 dev_dbg(skl->dev,
934 "%s: sink widget=%s\n", __func__, p->sink->name);
936 next_sink = p->sink;
938 if (!is_skl_dsp_widget_type(p->sink, skl->dev))
939 return skl_tplg_bind_sinks(p->sink, skl, src_w, src_mconfig);
942 * here we will check widgets in sink pipelines, so that
943 * can be any widgets type and we are only interested if
944 * they are ones used for SKL so check that first
946 if ((p->sink->priv != NULL) &&
947 is_skl_dsp_widget_type(p->sink, skl->dev)) {
949 sink = p->sink;
950 sink_mconfig = sink->priv;
953 * Modules other than PGA leaf can be connected
954 * directly or via switch to a module in another
955 * pipeline. EX: reference path
956 * when the path is enabled, the dst module that needs
957 * to be bound may not be initialized. if the module is
958 * not initialized, add these modules in the deferred
959 * bind list and when the dst module is initialised,
960 * bind this module to the dst_module in deferred list.
962 if (((src_mconfig->m_state == SKL_MODULE_INIT_DONE)
963 && (sink_mconfig->m_state == SKL_MODULE_UNINIT))) {
965 ret = skl_tplg_module_add_deferred_bind(skl,
966 src_mconfig, sink_mconfig);
968 if (ret < 0)
969 return ret;
974 if (src_mconfig->m_state == SKL_MODULE_UNINIT ||
975 sink_mconfig->m_state == SKL_MODULE_UNINIT)
976 continue;
978 /* Bind source to sink, mixin is always source */
979 ret = skl_bind_modules(skl, src_mconfig, sink_mconfig);
980 if (ret)
981 return ret;
983 /* set module params after bind */
984 skl_tplg_set_module_bind_params(src_w,
985 src_mconfig, skl);
986 skl_tplg_set_module_bind_params(sink,
987 sink_mconfig, skl);
989 /* Start sinks pipe first */
990 if (sink_mconfig->pipe->state != SKL_PIPE_STARTED) {
991 if (sink_mconfig->pipe->conn_type !=
992 SKL_PIPE_CONN_TYPE_FE)
993 ret = skl_run_pipe(skl,
994 sink_mconfig->pipe);
995 if (ret)
996 return ret;
1001 if (!sink && next_sink)
1002 return skl_tplg_bind_sinks(next_sink, skl, src_w, src_mconfig);
1004 return 0;
1008 * A PGA represents a module in a pipeline. So in the Pre-PMU event of PGA
1009 * we need to do following:
1010 * - Bind to sink pipeline
1011 * Since the sink pipes can be running and we don't get mixer event on
1012 * connect for already running mixer, we need to find the sink pipes
1013 * here and bind to them. This way dynamic connect works.
1014 * - Start sink pipeline, if not running
1015 * - Then run current pipe
1017 static int skl_tplg_pga_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
1018 struct skl_dev *skl)
1020 struct skl_module_cfg *src_mconfig;
1021 int ret = 0;
1023 src_mconfig = w->priv;
1026 * find which sink it is connected to, bind with the sink,
1027 * if sink is not started, start sink pipe first, then start
1028 * this pipe
1030 ret = skl_tplg_bind_sinks(w, skl, w, src_mconfig);
1031 if (ret)
1032 return ret;
1034 /* Start source pipe last after starting all sinks */
1035 if (src_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
1036 return skl_run_pipe(skl, src_mconfig->pipe);
1038 return 0;
1041 static struct snd_soc_dapm_widget *skl_get_src_dsp_widget(
1042 struct snd_soc_dapm_widget *w, struct skl_dev *skl)
1044 struct snd_soc_dapm_path *p;
1045 struct snd_soc_dapm_widget *src_w = NULL;
1047 snd_soc_dapm_widget_for_each_source_path(w, p) {
1048 src_w = p->source;
1049 if (!p->connect)
1050 continue;
1052 dev_dbg(skl->dev, "sink widget=%s\n", w->name);
1053 dev_dbg(skl->dev, "src widget=%s\n", p->source->name);
1056 * here we will check widgets in sink pipelines, so that can
1057 * be any widgets type and we are only interested if they are
1058 * ones used for SKL so check that first
1060 if ((p->source->priv != NULL) &&
1061 is_skl_dsp_widget_type(p->source, skl->dev)) {
1062 return p->source;
1066 if (src_w != NULL)
1067 return skl_get_src_dsp_widget(src_w, skl);
1069 return NULL;
1073 * in the Post-PMU event of mixer we need to do following:
1074 * - Check if this pipe is running
1075 * - if not, then
1076 * - bind this pipeline to its source pipeline
1077 * if source pipe is already running, this means it is a dynamic
1078 * connection and we need to bind only to that pipe
1079 * - start this pipeline
1081 static int skl_tplg_mixer_dapm_post_pmu_event(struct snd_soc_dapm_widget *w,
1082 struct skl_dev *skl)
1084 int ret = 0;
1085 struct snd_soc_dapm_widget *source, *sink;
1086 struct skl_module_cfg *src_mconfig, *sink_mconfig;
1087 int src_pipe_started = 0;
1089 sink = w;
1090 sink_mconfig = sink->priv;
1093 * If source pipe is already started, that means source is driving
1094 * one more sink before this sink got connected, Since source is
1095 * started, bind this sink to source and start this pipe.
1097 source = skl_get_src_dsp_widget(w, skl);
1098 if (source != NULL) {
1099 src_mconfig = source->priv;
1100 sink_mconfig = sink->priv;
1101 src_pipe_started = 1;
1104 * check pipe state, then no need to bind or start the
1105 * pipe
1107 if (src_mconfig->pipe->state != SKL_PIPE_STARTED)
1108 src_pipe_started = 0;
1111 if (src_pipe_started) {
1112 ret = skl_bind_modules(skl, src_mconfig, sink_mconfig);
1113 if (ret)
1114 return ret;
1116 /* set module params after bind */
1117 skl_tplg_set_module_bind_params(source, src_mconfig, skl);
1118 skl_tplg_set_module_bind_params(sink, sink_mconfig, skl);
1120 if (sink_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
1121 ret = skl_run_pipe(skl, sink_mconfig->pipe);
1124 return ret;
1128 * in the Pre-PMD event of mixer we need to do following:
1129 * - Stop the pipe
1130 * - find the source connections and remove that from dapm_path_list
1131 * - unbind with source pipelines if still connected
1133 static int skl_tplg_mixer_dapm_pre_pmd_event(struct snd_soc_dapm_widget *w,
1134 struct skl_dev *skl)
1136 struct skl_module_cfg *src_mconfig, *sink_mconfig;
1137 int ret = 0, i;
1139 sink_mconfig = w->priv;
1141 /* Stop the pipe */
1142 ret = skl_stop_pipe(skl, sink_mconfig->pipe);
1143 if (ret)
1144 return ret;
1146 for (i = 0; i < sink_mconfig->module->max_input_pins; i++) {
1147 if (sink_mconfig->m_in_pin[i].pin_state == SKL_PIN_BIND_DONE) {
1148 src_mconfig = sink_mconfig->m_in_pin[i].tgt_mcfg;
1149 if (!src_mconfig)
1150 continue;
1152 ret = skl_unbind_modules(skl,
1153 src_mconfig, sink_mconfig);
1157 return ret;
1161 * in the Post-PMD event of mixer we need to do following:
1162 * - Unbind the modules within the pipeline
1163 * - Delete the pipeline (modules are not required to be explicitly
1164 * deleted, pipeline delete is enough here
1166 static int skl_tplg_mixer_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
1167 struct skl_dev *skl)
1169 struct skl_module_cfg *mconfig = w->priv;
1170 struct skl_pipe_module *w_module;
1171 struct skl_module_cfg *src_module = NULL, *dst_module;
1172 struct skl_pipe *s_pipe = mconfig->pipe;
1173 struct skl_module_deferred_bind *modules, *tmp;
1175 if (s_pipe->state == SKL_PIPE_INVALID)
1176 return -EINVAL;
1178 list_for_each_entry(w_module, &s_pipe->w_list, node) {
1179 if (list_empty(&skl->bind_list))
1180 break;
1182 src_module = w_module->w->priv;
1184 list_for_each_entry_safe(modules, tmp, &skl->bind_list, node) {
1186 * When the destination module is deleted, Unbind the
1187 * modules from deferred bind list.
1189 if (modules->dst == src_module) {
1190 skl_unbind_modules(skl, modules->src,
1191 modules->dst);
1195 * When the source module is deleted, remove this entry
1196 * from the deferred bind list.
1198 if (modules->src == src_module) {
1199 list_del(&modules->node);
1200 modules->src = NULL;
1201 modules->dst = NULL;
1202 kfree(modules);
1207 list_for_each_entry(w_module, &s_pipe->w_list, node) {
1208 dst_module = w_module->w->priv;
1210 if (src_module == NULL) {
1211 src_module = dst_module;
1212 continue;
1215 skl_unbind_modules(skl, src_module, dst_module);
1216 src_module = dst_module;
1219 skl_delete_pipe(skl, mconfig->pipe);
1221 list_for_each_entry(w_module, &s_pipe->w_list, node) {
1222 src_module = w_module->w->priv;
1223 src_module->m_state = SKL_MODULE_UNINIT;
1226 return skl_tplg_unload_pipe_modules(skl, s_pipe);
1230 * in the Post-PMD event of PGA we need to do following:
1231 * - Stop the pipeline
1232 * - In source pipe is connected, unbind with source pipelines
1234 static int skl_tplg_pga_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
1235 struct skl_dev *skl)
1237 struct skl_module_cfg *src_mconfig, *sink_mconfig;
1238 int ret = 0, i;
1240 src_mconfig = w->priv;
1242 /* Stop the pipe since this is a mixin module */
1243 ret = skl_stop_pipe(skl, src_mconfig->pipe);
1244 if (ret)
1245 return ret;
1247 for (i = 0; i < src_mconfig->module->max_output_pins; i++) {
1248 if (src_mconfig->m_out_pin[i].pin_state == SKL_PIN_BIND_DONE) {
1249 sink_mconfig = src_mconfig->m_out_pin[i].tgt_mcfg;
1250 if (!sink_mconfig)
1251 continue;
1253 * This is a connecter and if path is found that means
1254 * unbind between source and sink has not happened yet
1256 ret = skl_unbind_modules(skl, src_mconfig,
1257 sink_mconfig);
1261 return ret;
1265 * In modelling, we assume there will be ONLY one mixer in a pipeline. If a
1266 * second one is required that is created as another pipe entity.
1267 * The mixer is responsible for pipe management and represent a pipeline
1268 * instance
1270 static int skl_tplg_mixer_event(struct snd_soc_dapm_widget *w,
1271 struct snd_kcontrol *k, int event)
1273 struct snd_soc_dapm_context *dapm = w->dapm;
1274 struct skl_dev *skl = get_skl_ctx(dapm->dev);
1276 switch (event) {
1277 case SND_SOC_DAPM_PRE_PMU:
1278 return skl_tplg_mixer_dapm_pre_pmu_event(w, skl);
1280 case SND_SOC_DAPM_POST_PMU:
1281 return skl_tplg_mixer_dapm_post_pmu_event(w, skl);
1283 case SND_SOC_DAPM_PRE_PMD:
1284 return skl_tplg_mixer_dapm_pre_pmd_event(w, skl);
1286 case SND_SOC_DAPM_POST_PMD:
1287 return skl_tplg_mixer_dapm_post_pmd_event(w, skl);
1290 return 0;
1294 * In modelling, we assumed rest of the modules in pipeline are PGA. But we
1295 * are interested in last PGA (leaf PGA) in a pipeline to disconnect with
1296 * the sink when it is running (two FE to one BE or one FE to two BE)
1297 * scenarios
1299 static int skl_tplg_pga_event(struct snd_soc_dapm_widget *w,
1300 struct snd_kcontrol *k, int event)
1303 struct snd_soc_dapm_context *dapm = w->dapm;
1304 struct skl_dev *skl = get_skl_ctx(dapm->dev);
1306 switch (event) {
1307 case SND_SOC_DAPM_PRE_PMU:
1308 return skl_tplg_pga_dapm_pre_pmu_event(w, skl);
1310 case SND_SOC_DAPM_POST_PMD:
1311 return skl_tplg_pga_dapm_post_pmd_event(w, skl);
1314 return 0;
1317 static int skl_tplg_tlv_control_get(struct snd_kcontrol *kcontrol,
1318 unsigned int __user *data, unsigned int size)
1320 struct soc_bytes_ext *sb =
1321 (struct soc_bytes_ext *)kcontrol->private_value;
1322 struct skl_algo_data *bc = (struct skl_algo_data *)sb->dobj.private;
1323 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1324 struct skl_module_cfg *mconfig = w->priv;
1325 struct skl_dev *skl = get_skl_ctx(w->dapm->dev);
1327 if (w->power)
1328 skl_get_module_params(skl, (u32 *)bc->params,
1329 bc->size, bc->param_id, mconfig);
1331 /* decrement size for TLV header */
1332 size -= 2 * sizeof(u32);
1334 /* check size as we don't want to send kernel data */
1335 if (size > bc->max)
1336 size = bc->max;
1338 if (bc->params) {
1339 if (copy_to_user(data, &bc->param_id, sizeof(u32)))
1340 return -EFAULT;
1341 if (copy_to_user(data + 1, &size, sizeof(u32)))
1342 return -EFAULT;
1343 if (copy_to_user(data + 2, bc->params, size))
1344 return -EFAULT;
1347 return 0;
1350 #define SKL_PARAM_VENDOR_ID 0xff
1352 static int skl_tplg_tlv_control_set(struct snd_kcontrol *kcontrol,
1353 const unsigned int __user *data, unsigned int size)
1355 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1356 struct skl_module_cfg *mconfig = w->priv;
1357 struct soc_bytes_ext *sb =
1358 (struct soc_bytes_ext *)kcontrol->private_value;
1359 struct skl_algo_data *ac = (struct skl_algo_data *)sb->dobj.private;
1360 struct skl_dev *skl = get_skl_ctx(w->dapm->dev);
1362 if (ac->params) {
1364 * Widget data is expected to be stripped of T and L
1366 size -= 2 * sizeof(unsigned int);
1367 data += 2;
1369 if (size > ac->max)
1370 return -EINVAL;
1371 ac->size = size;
1373 if (copy_from_user(ac->params, data, size))
1374 return -EFAULT;
1376 if (w->power)
1377 return skl_set_module_params(skl,
1378 (u32 *)ac->params, ac->size,
1379 ac->param_id, mconfig);
1382 return 0;
1385 static int skl_tplg_mic_control_get(struct snd_kcontrol *kcontrol,
1386 struct snd_ctl_elem_value *ucontrol)
1388 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1389 struct skl_module_cfg *mconfig = w->priv;
1390 struct soc_enum *ec = (struct soc_enum *)kcontrol->private_value;
1391 u32 ch_type = *((u32 *)ec->dobj.private);
1393 if (mconfig->dmic_ch_type == ch_type)
1394 ucontrol->value.enumerated.item[0] =
1395 mconfig->dmic_ch_combo_index;
1396 else
1397 ucontrol->value.enumerated.item[0] = 0;
1399 return 0;
1402 static int skl_fill_mic_sel_params(struct skl_module_cfg *mconfig,
1403 struct skl_mic_sel_config *mic_cfg, struct device *dev)
1405 struct skl_specific_cfg *sp_cfg = &mconfig->formats_config;
1407 sp_cfg->caps_size = sizeof(struct skl_mic_sel_config);
1408 sp_cfg->set_params = SKL_PARAM_SET;
1409 sp_cfg->param_id = 0x00;
1410 if (!sp_cfg->caps) {
1411 sp_cfg->caps = devm_kzalloc(dev, sp_cfg->caps_size, GFP_KERNEL);
1412 if (!sp_cfg->caps)
1413 return -ENOMEM;
1416 mic_cfg->mic_switch = SKL_MIC_SEL_SWITCH;
1417 mic_cfg->flags = 0;
1418 memcpy(sp_cfg->caps, mic_cfg, sp_cfg->caps_size);
1420 return 0;
1423 static int skl_tplg_mic_control_set(struct snd_kcontrol *kcontrol,
1424 struct snd_ctl_elem_value *ucontrol)
1426 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1427 struct skl_module_cfg *mconfig = w->priv;
1428 struct skl_mic_sel_config mic_cfg = {0};
1429 struct soc_enum *ec = (struct soc_enum *)kcontrol->private_value;
1430 u32 ch_type = *((u32 *)ec->dobj.private);
1431 const int *list;
1432 u8 in_ch, out_ch, index;
1434 mconfig->dmic_ch_type = ch_type;
1435 mconfig->dmic_ch_combo_index = ucontrol->value.enumerated.item[0];
1437 /* enum control index 0 is INVALID, so no channels to be set */
1438 if (mconfig->dmic_ch_combo_index == 0)
1439 return 0;
1441 /* No valid channel selection map for index 0, so offset by 1 */
1442 index = mconfig->dmic_ch_combo_index - 1;
1444 switch (ch_type) {
1445 case SKL_CH_MONO:
1446 if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_mono_list))
1447 return -EINVAL;
1449 list = &mic_mono_list[index];
1450 break;
1452 case SKL_CH_STEREO:
1453 if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_stereo_list))
1454 return -EINVAL;
1456 list = mic_stereo_list[index];
1457 break;
1459 case SKL_CH_TRIO:
1460 if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_trio_list))
1461 return -EINVAL;
1463 list = mic_trio_list[index];
1464 break;
1466 case SKL_CH_QUATRO:
1467 if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_quatro_list))
1468 return -EINVAL;
1470 list = mic_quatro_list[index];
1471 break;
1473 default:
1474 dev_err(w->dapm->dev,
1475 "Invalid channel %d for mic_select module\n",
1476 ch_type);
1477 return -EINVAL;
1481 /* channel type enum map to number of chanels for that type */
1482 for (out_ch = 0; out_ch < ch_type; out_ch++) {
1483 in_ch = list[out_ch];
1484 mic_cfg.blob[out_ch][in_ch] = SKL_DEFAULT_MIC_SEL_GAIN;
1487 return skl_fill_mic_sel_params(mconfig, &mic_cfg, w->dapm->dev);
1491 * Fill the dma id for host and link. In case of passthrough
1492 * pipeline, this will both host and link in the same
1493 * pipeline, so need to copy the link and host based on dev_type
1495 static void skl_tplg_fill_dma_id(struct skl_module_cfg *mcfg,
1496 struct skl_pipe_params *params)
1498 struct skl_pipe *pipe = mcfg->pipe;
1500 if (pipe->passthru) {
1501 switch (mcfg->dev_type) {
1502 case SKL_DEVICE_HDALINK:
1503 pipe->p_params->link_dma_id = params->link_dma_id;
1504 pipe->p_params->link_index = params->link_index;
1505 pipe->p_params->link_bps = params->link_bps;
1506 break;
1508 case SKL_DEVICE_HDAHOST:
1509 pipe->p_params->host_dma_id = params->host_dma_id;
1510 pipe->p_params->host_bps = params->host_bps;
1511 break;
1513 default:
1514 break;
1516 pipe->p_params->s_fmt = params->s_fmt;
1517 pipe->p_params->ch = params->ch;
1518 pipe->p_params->s_freq = params->s_freq;
1519 pipe->p_params->stream = params->stream;
1520 pipe->p_params->format = params->format;
1522 } else {
1523 memcpy(pipe->p_params, params, sizeof(*params));
1528 * The FE params are passed by hw_params of the DAI.
1529 * On hw_params, the params are stored in Gateway module of the FE and we
1530 * need to calculate the format in DSP module configuration, that
1531 * conversion is done here
1533 int skl_tplg_update_pipe_params(struct device *dev,
1534 struct skl_module_cfg *mconfig,
1535 struct skl_pipe_params *params)
1537 struct skl_module_res *res = &mconfig->module->resources[0];
1538 struct skl_dev *skl = get_skl_ctx(dev);
1539 struct skl_module_fmt *format = NULL;
1540 u8 cfg_idx = mconfig->pipe->cur_config_idx;
1542 skl_tplg_fill_dma_id(mconfig, params);
1543 mconfig->fmt_idx = mconfig->mod_cfg[cfg_idx].fmt_idx;
1544 mconfig->res_idx = mconfig->mod_cfg[cfg_idx].res_idx;
1546 if (skl->nr_modules)
1547 return 0;
1549 if (params->stream == SNDRV_PCM_STREAM_PLAYBACK)
1550 format = &mconfig->module->formats[0].inputs[0].fmt;
1551 else
1552 format = &mconfig->module->formats[0].outputs[0].fmt;
1554 /* set the hw_params */
1555 format->s_freq = params->s_freq;
1556 format->channels = params->ch;
1557 format->valid_bit_depth = skl_get_bit_depth(params->s_fmt);
1560 * 16 bit is 16 bit container whereas 24 bit is in 32 bit
1561 * container so update bit depth accordingly
1563 switch (format->valid_bit_depth) {
1564 case SKL_DEPTH_16BIT:
1565 format->bit_depth = format->valid_bit_depth;
1566 break;
1568 case SKL_DEPTH_24BIT:
1569 case SKL_DEPTH_32BIT:
1570 format->bit_depth = SKL_DEPTH_32BIT;
1571 break;
1573 default:
1574 dev_err(dev, "Invalid bit depth %x for pipe\n",
1575 format->valid_bit_depth);
1576 return -EINVAL;
1579 if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1580 res->ibs = (format->s_freq / 1000) *
1581 (format->channels) *
1582 (format->bit_depth >> 3);
1583 } else {
1584 res->obs = (format->s_freq / 1000) *
1585 (format->channels) *
1586 (format->bit_depth >> 3);
1589 return 0;
1593 * Query the module config for the FE DAI
1594 * This is used to find the hw_params set for that DAI and apply to FE
1595 * pipeline
1597 struct skl_module_cfg *
1598 skl_tplg_fe_get_cpr_module(struct snd_soc_dai *dai, int stream)
1600 struct snd_soc_dapm_widget *w;
1601 struct snd_soc_dapm_path *p = NULL;
1603 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1604 w = dai->playback_widget;
1605 snd_soc_dapm_widget_for_each_sink_path(w, p) {
1606 if (p->connect && p->sink->power &&
1607 !is_skl_dsp_widget_type(p->sink, dai->dev))
1608 continue;
1610 if (p->sink->priv) {
1611 dev_dbg(dai->dev, "set params for %s\n",
1612 p->sink->name);
1613 return p->sink->priv;
1616 } else {
1617 w = dai->capture_widget;
1618 snd_soc_dapm_widget_for_each_source_path(w, p) {
1619 if (p->connect && p->source->power &&
1620 !is_skl_dsp_widget_type(p->source, dai->dev))
1621 continue;
1623 if (p->source->priv) {
1624 dev_dbg(dai->dev, "set params for %s\n",
1625 p->source->name);
1626 return p->source->priv;
1631 return NULL;
1634 static struct skl_module_cfg *skl_get_mconfig_pb_cpr(
1635 struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
1637 struct snd_soc_dapm_path *p;
1638 struct skl_module_cfg *mconfig = NULL;
1640 snd_soc_dapm_widget_for_each_source_path(w, p) {
1641 if (w->endpoints[SND_SOC_DAPM_DIR_OUT] > 0) {
1642 if (p->connect &&
1643 (p->sink->id == snd_soc_dapm_aif_out) &&
1644 p->source->priv) {
1645 mconfig = p->source->priv;
1646 return mconfig;
1648 mconfig = skl_get_mconfig_pb_cpr(dai, p->source);
1649 if (mconfig)
1650 return mconfig;
1653 return mconfig;
1656 static struct skl_module_cfg *skl_get_mconfig_cap_cpr(
1657 struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
1659 struct snd_soc_dapm_path *p;
1660 struct skl_module_cfg *mconfig = NULL;
1662 snd_soc_dapm_widget_for_each_sink_path(w, p) {
1663 if (w->endpoints[SND_SOC_DAPM_DIR_IN] > 0) {
1664 if (p->connect &&
1665 (p->source->id == snd_soc_dapm_aif_in) &&
1666 p->sink->priv) {
1667 mconfig = p->sink->priv;
1668 return mconfig;
1670 mconfig = skl_get_mconfig_cap_cpr(dai, p->sink);
1671 if (mconfig)
1672 return mconfig;
1675 return mconfig;
1678 struct skl_module_cfg *
1679 skl_tplg_be_get_cpr_module(struct snd_soc_dai *dai, int stream)
1681 struct snd_soc_dapm_widget *w;
1682 struct skl_module_cfg *mconfig;
1684 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1685 w = dai->playback_widget;
1686 mconfig = skl_get_mconfig_pb_cpr(dai, w);
1687 } else {
1688 w = dai->capture_widget;
1689 mconfig = skl_get_mconfig_cap_cpr(dai, w);
1691 return mconfig;
1694 static u8 skl_tplg_be_link_type(int dev_type)
1696 int ret;
1698 switch (dev_type) {
1699 case SKL_DEVICE_BT:
1700 ret = NHLT_LINK_SSP;
1701 break;
1703 case SKL_DEVICE_DMIC:
1704 ret = NHLT_LINK_DMIC;
1705 break;
1707 case SKL_DEVICE_I2S:
1708 ret = NHLT_LINK_SSP;
1709 break;
1711 case SKL_DEVICE_HDALINK:
1712 ret = NHLT_LINK_HDA;
1713 break;
1715 default:
1716 ret = NHLT_LINK_INVALID;
1717 break;
1720 return ret;
1724 * Fill the BE gateway parameters
1725 * The BE gateway expects a blob of parameters which are kept in the ACPI
1726 * NHLT blob, so query the blob for interface type (i2s/pdm) and instance.
1727 * The port can have multiple settings so pick based on the PCM
1728 * parameters
1730 static int skl_tplg_be_fill_pipe_params(struct snd_soc_dai *dai,
1731 struct skl_module_cfg *mconfig,
1732 struct skl_pipe_params *params)
1734 struct nhlt_specific_cfg *cfg;
1735 struct skl_dev *skl = get_skl_ctx(dai->dev);
1736 int link_type = skl_tplg_be_link_type(mconfig->dev_type);
1737 u8 dev_type = skl_tplg_be_dev_type(mconfig->dev_type);
1739 skl_tplg_fill_dma_id(mconfig, params);
1741 if (link_type == NHLT_LINK_HDA)
1742 return 0;
1744 /* update the blob based on virtual bus_id*/
1745 cfg = skl_get_ep_blob(skl, mconfig->vbus_id, link_type,
1746 params->s_fmt, params->ch,
1747 params->s_freq, params->stream,
1748 dev_type);
1749 if (cfg) {
1750 mconfig->formats_config.caps_size = cfg->size;
1751 mconfig->formats_config.caps = (u32 *) &cfg->caps;
1752 } else {
1753 dev_err(dai->dev, "Blob NULL for id %x type %d dirn %d\n",
1754 mconfig->vbus_id, link_type,
1755 params->stream);
1756 dev_err(dai->dev, "PCM: ch %d, freq %d, fmt %d\n",
1757 params->ch, params->s_freq, params->s_fmt);
1758 return -EINVAL;
1761 return 0;
1764 static int skl_tplg_be_set_src_pipe_params(struct snd_soc_dai *dai,
1765 struct snd_soc_dapm_widget *w,
1766 struct skl_pipe_params *params)
1768 struct snd_soc_dapm_path *p;
1769 int ret = -EIO;
1771 snd_soc_dapm_widget_for_each_source_path(w, p) {
1772 if (p->connect && is_skl_dsp_widget_type(p->source, dai->dev) &&
1773 p->source->priv) {
1775 ret = skl_tplg_be_fill_pipe_params(dai,
1776 p->source->priv, params);
1777 if (ret < 0)
1778 return ret;
1779 } else {
1780 ret = skl_tplg_be_set_src_pipe_params(dai,
1781 p->source, params);
1782 if (ret < 0)
1783 return ret;
1787 return ret;
1790 static int skl_tplg_be_set_sink_pipe_params(struct snd_soc_dai *dai,
1791 struct snd_soc_dapm_widget *w, struct skl_pipe_params *params)
1793 struct snd_soc_dapm_path *p = NULL;
1794 int ret = -EIO;
1796 snd_soc_dapm_widget_for_each_sink_path(w, p) {
1797 if (p->connect && is_skl_dsp_widget_type(p->sink, dai->dev) &&
1798 p->sink->priv) {
1800 ret = skl_tplg_be_fill_pipe_params(dai,
1801 p->sink->priv, params);
1802 if (ret < 0)
1803 return ret;
1804 } else {
1805 ret = skl_tplg_be_set_sink_pipe_params(
1806 dai, p->sink, params);
1807 if (ret < 0)
1808 return ret;
1812 return ret;
1816 * BE hw_params can be a source parameters (capture) or sink parameters
1817 * (playback). Based on sink and source we need to either find the source
1818 * list or the sink list and set the pipeline parameters
1820 int skl_tplg_be_update_params(struct snd_soc_dai *dai,
1821 struct skl_pipe_params *params)
1823 struct snd_soc_dapm_widget *w;
1825 if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1826 w = dai->playback_widget;
1828 return skl_tplg_be_set_src_pipe_params(dai, w, params);
1830 } else {
1831 w = dai->capture_widget;
1833 return skl_tplg_be_set_sink_pipe_params(dai, w, params);
1836 return 0;
1839 static const struct snd_soc_tplg_widget_events skl_tplg_widget_ops[] = {
1840 {SKL_MIXER_EVENT, skl_tplg_mixer_event},
1841 {SKL_VMIXER_EVENT, skl_tplg_mixer_event},
1842 {SKL_PGA_EVENT, skl_tplg_pga_event},
1845 static const struct snd_soc_tplg_bytes_ext_ops skl_tlv_ops[] = {
1846 {SKL_CONTROL_TYPE_BYTE_TLV, skl_tplg_tlv_control_get,
1847 skl_tplg_tlv_control_set},
1850 static const struct snd_soc_tplg_kcontrol_ops skl_tplg_kcontrol_ops[] = {
1852 .id = SKL_CONTROL_TYPE_MIC_SELECT,
1853 .get = skl_tplg_mic_control_get,
1854 .put = skl_tplg_mic_control_set,
1858 static int skl_tplg_fill_pipe_cfg(struct device *dev,
1859 struct skl_pipe *pipe, u32 tkn,
1860 u32 tkn_val, int conf_idx, int dir)
1862 struct skl_pipe_fmt *fmt;
1863 struct skl_path_config *config;
1865 switch (dir) {
1866 case SKL_DIR_IN:
1867 fmt = &pipe->configs[conf_idx].in_fmt;
1868 break;
1870 case SKL_DIR_OUT:
1871 fmt = &pipe->configs[conf_idx].out_fmt;
1872 break;
1874 default:
1875 dev_err(dev, "Invalid direction: %d\n", dir);
1876 return -EINVAL;
1879 config = &pipe->configs[conf_idx];
1881 switch (tkn) {
1882 case SKL_TKN_U32_CFG_FREQ:
1883 fmt->freq = tkn_val;
1884 break;
1886 case SKL_TKN_U8_CFG_CHAN:
1887 fmt->channels = tkn_val;
1888 break;
1890 case SKL_TKN_U8_CFG_BPS:
1891 fmt->bps = tkn_val;
1892 break;
1894 case SKL_TKN_U32_PATH_MEM_PGS:
1895 config->mem_pages = tkn_val;
1896 break;
1898 default:
1899 dev_err(dev, "Invalid token config: %d\n", tkn);
1900 return -EINVAL;
1903 return 0;
1906 static int skl_tplg_fill_pipe_tkn(struct device *dev,
1907 struct skl_pipe *pipe, u32 tkn,
1908 u32 tkn_val)
1911 switch (tkn) {
1912 case SKL_TKN_U32_PIPE_CONN_TYPE:
1913 pipe->conn_type = tkn_val;
1914 break;
1916 case SKL_TKN_U32_PIPE_PRIORITY:
1917 pipe->pipe_priority = tkn_val;
1918 break;
1920 case SKL_TKN_U32_PIPE_MEM_PGS:
1921 pipe->memory_pages = tkn_val;
1922 break;
1924 case SKL_TKN_U32_PMODE:
1925 pipe->lp_mode = tkn_val;
1926 break;
1928 case SKL_TKN_U32_PIPE_DIRECTION:
1929 pipe->direction = tkn_val;
1930 break;
1932 case SKL_TKN_U32_NUM_CONFIGS:
1933 pipe->nr_cfgs = tkn_val;
1934 break;
1936 default:
1937 dev_err(dev, "Token not handled %d\n", tkn);
1938 return -EINVAL;
1941 return 0;
1945 * Add pipeline by parsing the relevant tokens
1946 * Return an existing pipe if the pipe already exists.
1948 static int skl_tplg_add_pipe(struct device *dev,
1949 struct skl_module_cfg *mconfig, struct skl_dev *skl,
1950 struct snd_soc_tplg_vendor_value_elem *tkn_elem)
1952 struct skl_pipeline *ppl;
1953 struct skl_pipe *pipe;
1954 struct skl_pipe_params *params;
1956 list_for_each_entry(ppl, &skl->ppl_list, node) {
1957 if (ppl->pipe->ppl_id == tkn_elem->value) {
1958 mconfig->pipe = ppl->pipe;
1959 return -EEXIST;
1963 ppl = devm_kzalloc(dev, sizeof(*ppl), GFP_KERNEL);
1964 if (!ppl)
1965 return -ENOMEM;
1967 pipe = devm_kzalloc(dev, sizeof(*pipe), GFP_KERNEL);
1968 if (!pipe)
1969 return -ENOMEM;
1971 params = devm_kzalloc(dev, sizeof(*params), GFP_KERNEL);
1972 if (!params)
1973 return -ENOMEM;
1975 pipe->p_params = params;
1976 pipe->ppl_id = tkn_elem->value;
1977 INIT_LIST_HEAD(&pipe->w_list);
1979 ppl->pipe = pipe;
1980 list_add(&ppl->node, &skl->ppl_list);
1982 mconfig->pipe = pipe;
1983 mconfig->pipe->state = SKL_PIPE_INVALID;
1985 return 0;
1988 static int skl_tplg_get_uuid(struct device *dev, guid_t *guid,
1989 struct snd_soc_tplg_vendor_uuid_elem *uuid_tkn)
1991 if (uuid_tkn->token == SKL_TKN_UUID) {
1992 guid_copy(guid, (guid_t *)&uuid_tkn->uuid);
1993 return 0;
1996 dev_err(dev, "Not an UUID token %d\n", uuid_tkn->token);
1998 return -EINVAL;
2001 static int skl_tplg_fill_pin(struct device *dev,
2002 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2003 struct skl_module_pin *m_pin,
2004 int pin_index)
2006 int ret;
2008 switch (tkn_elem->token) {
2009 case SKL_TKN_U32_PIN_MOD_ID:
2010 m_pin[pin_index].id.module_id = tkn_elem->value;
2011 break;
2013 case SKL_TKN_U32_PIN_INST_ID:
2014 m_pin[pin_index].id.instance_id = tkn_elem->value;
2015 break;
2017 case SKL_TKN_UUID:
2018 ret = skl_tplg_get_uuid(dev, &m_pin[pin_index].id.mod_uuid,
2019 (struct snd_soc_tplg_vendor_uuid_elem *)tkn_elem);
2020 if (ret < 0)
2021 return ret;
2023 break;
2025 default:
2026 dev_err(dev, "%d Not a pin token\n", tkn_elem->token);
2027 return -EINVAL;
2030 return 0;
2034 * Parse for pin config specific tokens to fill up the
2035 * module private data
2037 static int skl_tplg_fill_pins_info(struct device *dev,
2038 struct skl_module_cfg *mconfig,
2039 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2040 int dir, int pin_count)
2042 int ret;
2043 struct skl_module_pin *m_pin;
2045 switch (dir) {
2046 case SKL_DIR_IN:
2047 m_pin = mconfig->m_in_pin;
2048 break;
2050 case SKL_DIR_OUT:
2051 m_pin = mconfig->m_out_pin;
2052 break;
2054 default:
2055 dev_err(dev, "Invalid direction value\n");
2056 return -EINVAL;
2059 ret = skl_tplg_fill_pin(dev, tkn_elem, m_pin, pin_count);
2060 if (ret < 0)
2061 return ret;
2063 m_pin[pin_count].in_use = false;
2064 m_pin[pin_count].pin_state = SKL_PIN_UNBIND;
2066 return 0;
2070 * Fill up input/output module config format based
2071 * on the direction
2073 static int skl_tplg_fill_fmt(struct device *dev,
2074 struct skl_module_fmt *dst_fmt,
2075 u32 tkn, u32 value)
2077 switch (tkn) {
2078 case SKL_TKN_U32_FMT_CH:
2079 dst_fmt->channels = value;
2080 break;
2082 case SKL_TKN_U32_FMT_FREQ:
2083 dst_fmt->s_freq = value;
2084 break;
2086 case SKL_TKN_U32_FMT_BIT_DEPTH:
2087 dst_fmt->bit_depth = value;
2088 break;
2090 case SKL_TKN_U32_FMT_SAMPLE_SIZE:
2091 dst_fmt->valid_bit_depth = value;
2092 break;
2094 case SKL_TKN_U32_FMT_CH_CONFIG:
2095 dst_fmt->ch_cfg = value;
2096 break;
2098 case SKL_TKN_U32_FMT_INTERLEAVE:
2099 dst_fmt->interleaving_style = value;
2100 break;
2102 case SKL_TKN_U32_FMT_SAMPLE_TYPE:
2103 dst_fmt->sample_type = value;
2104 break;
2106 case SKL_TKN_U32_FMT_CH_MAP:
2107 dst_fmt->ch_map = value;
2108 break;
2110 default:
2111 dev_err(dev, "Invalid token %d\n", tkn);
2112 return -EINVAL;
2115 return 0;
2118 static int skl_tplg_widget_fill_fmt(struct device *dev,
2119 struct skl_module_iface *fmt,
2120 u32 tkn, u32 val, u32 dir, int fmt_idx)
2122 struct skl_module_fmt *dst_fmt;
2124 if (!fmt)
2125 return -EINVAL;
2127 switch (dir) {
2128 case SKL_DIR_IN:
2129 dst_fmt = &fmt->inputs[fmt_idx].fmt;
2130 break;
2132 case SKL_DIR_OUT:
2133 dst_fmt = &fmt->outputs[fmt_idx].fmt;
2134 break;
2136 default:
2137 dev_err(dev, "Invalid direction: %d\n", dir);
2138 return -EINVAL;
2141 return skl_tplg_fill_fmt(dev, dst_fmt, tkn, val);
2144 static void skl_tplg_fill_pin_dynamic_val(
2145 struct skl_module_pin *mpin, u32 pin_count, u32 value)
2147 int i;
2149 for (i = 0; i < pin_count; i++)
2150 mpin[i].is_dynamic = value;
2154 * Resource table in the manifest has pin specific resources
2155 * like pin and pin buffer size
2157 static int skl_tplg_manifest_pin_res_tkn(struct device *dev,
2158 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2159 struct skl_module_res *res, int pin_idx, int dir)
2161 struct skl_module_pin_resources *m_pin;
2163 switch (dir) {
2164 case SKL_DIR_IN:
2165 m_pin = &res->input[pin_idx];
2166 break;
2168 case SKL_DIR_OUT:
2169 m_pin = &res->output[pin_idx];
2170 break;
2172 default:
2173 dev_err(dev, "Invalid pin direction: %d\n", dir);
2174 return -EINVAL;
2177 switch (tkn_elem->token) {
2178 case SKL_TKN_MM_U32_RES_PIN_ID:
2179 m_pin->pin_index = tkn_elem->value;
2180 break;
2182 case SKL_TKN_MM_U32_PIN_BUF:
2183 m_pin->buf_size = tkn_elem->value;
2184 break;
2186 default:
2187 dev_err(dev, "Invalid token: %d\n", tkn_elem->token);
2188 return -EINVAL;
2191 return 0;
2195 * Fill module specific resources from the manifest's resource
2196 * table like CPS, DMA size, mem_pages.
2198 static int skl_tplg_fill_res_tkn(struct device *dev,
2199 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2200 struct skl_module_res *res,
2201 int pin_idx, int dir)
2203 int ret, tkn_count = 0;
2205 if (!res)
2206 return -EINVAL;
2208 switch (tkn_elem->token) {
2209 case SKL_TKN_MM_U32_DMA_SIZE:
2210 res->dma_buffer_size = tkn_elem->value;
2211 break;
2213 case SKL_TKN_MM_U32_CPC:
2214 res->cpc = tkn_elem->value;
2215 break;
2217 case SKL_TKN_U32_MEM_PAGES:
2218 res->is_pages = tkn_elem->value;
2219 break;
2221 case SKL_TKN_U32_OBS:
2222 res->obs = tkn_elem->value;
2223 break;
2225 case SKL_TKN_U32_IBS:
2226 res->ibs = tkn_elem->value;
2227 break;
2229 case SKL_TKN_MM_U32_RES_PIN_ID:
2230 case SKL_TKN_MM_U32_PIN_BUF:
2231 ret = skl_tplg_manifest_pin_res_tkn(dev, tkn_elem, res,
2232 pin_idx, dir);
2233 if (ret < 0)
2234 return ret;
2235 break;
2237 case SKL_TKN_MM_U32_CPS:
2238 case SKL_TKN_U32_MAX_MCPS:
2239 /* ignore unused tokens */
2240 break;
2242 default:
2243 dev_err(dev, "Not a res type token: %d", tkn_elem->token);
2244 return -EINVAL;
2247 tkn_count++;
2249 return tkn_count;
2253 * Parse tokens to fill up the module private data
2255 static int skl_tplg_get_token(struct device *dev,
2256 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2257 struct skl_dev *skl, struct skl_module_cfg *mconfig)
2259 int tkn_count = 0;
2260 int ret;
2261 static int is_pipe_exists;
2262 static int pin_index, dir, conf_idx;
2263 struct skl_module_iface *iface = NULL;
2264 struct skl_module_res *res = NULL;
2265 int res_idx = mconfig->res_idx;
2266 int fmt_idx = mconfig->fmt_idx;
2269 * If the manifest structure contains no modules, fill all
2270 * the module data to 0th index.
2271 * res_idx and fmt_idx are default set to 0.
2273 if (skl->nr_modules == 0) {
2274 res = &mconfig->module->resources[res_idx];
2275 iface = &mconfig->module->formats[fmt_idx];
2278 if (tkn_elem->token > SKL_TKN_MAX)
2279 return -EINVAL;
2281 switch (tkn_elem->token) {
2282 case SKL_TKN_U8_IN_QUEUE_COUNT:
2283 mconfig->module->max_input_pins = tkn_elem->value;
2284 break;
2286 case SKL_TKN_U8_OUT_QUEUE_COUNT:
2287 mconfig->module->max_output_pins = tkn_elem->value;
2288 break;
2290 case SKL_TKN_U8_DYN_IN_PIN:
2291 if (!mconfig->m_in_pin)
2292 mconfig->m_in_pin =
2293 devm_kcalloc(dev, MAX_IN_QUEUE,
2294 sizeof(*mconfig->m_in_pin),
2295 GFP_KERNEL);
2296 if (!mconfig->m_in_pin)
2297 return -ENOMEM;
2299 skl_tplg_fill_pin_dynamic_val(mconfig->m_in_pin, MAX_IN_QUEUE,
2300 tkn_elem->value);
2301 break;
2303 case SKL_TKN_U8_DYN_OUT_PIN:
2304 if (!mconfig->m_out_pin)
2305 mconfig->m_out_pin =
2306 devm_kcalloc(dev, MAX_IN_QUEUE,
2307 sizeof(*mconfig->m_in_pin),
2308 GFP_KERNEL);
2309 if (!mconfig->m_out_pin)
2310 return -ENOMEM;
2312 skl_tplg_fill_pin_dynamic_val(mconfig->m_out_pin, MAX_OUT_QUEUE,
2313 tkn_elem->value);
2314 break;
2316 case SKL_TKN_U8_TIME_SLOT:
2317 mconfig->time_slot = tkn_elem->value;
2318 break;
2320 case SKL_TKN_U8_CORE_ID:
2321 mconfig->core_id = tkn_elem->value;
2322 break;
2324 case SKL_TKN_U8_MOD_TYPE:
2325 mconfig->m_type = tkn_elem->value;
2326 break;
2328 case SKL_TKN_U8_DEV_TYPE:
2329 mconfig->dev_type = tkn_elem->value;
2330 break;
2332 case SKL_TKN_U8_HW_CONN_TYPE:
2333 mconfig->hw_conn_type = tkn_elem->value;
2334 break;
2336 case SKL_TKN_U16_MOD_INST_ID:
2337 mconfig->id.instance_id =
2338 tkn_elem->value;
2339 break;
2341 case SKL_TKN_U32_MEM_PAGES:
2342 case SKL_TKN_U32_MAX_MCPS:
2343 case SKL_TKN_U32_OBS:
2344 case SKL_TKN_U32_IBS:
2345 ret = skl_tplg_fill_res_tkn(dev, tkn_elem, res, pin_index, dir);
2346 if (ret < 0)
2347 return ret;
2349 break;
2351 case SKL_TKN_U32_VBUS_ID:
2352 mconfig->vbus_id = tkn_elem->value;
2353 break;
2355 case SKL_TKN_U32_PARAMS_FIXUP:
2356 mconfig->params_fixup = tkn_elem->value;
2357 break;
2359 case SKL_TKN_U32_CONVERTER:
2360 mconfig->converter = tkn_elem->value;
2361 break;
2363 case SKL_TKN_U32_D0I3_CAPS:
2364 mconfig->d0i3_caps = tkn_elem->value;
2365 break;
2367 case SKL_TKN_U32_PIPE_ID:
2368 ret = skl_tplg_add_pipe(dev,
2369 mconfig, skl, tkn_elem);
2371 if (ret < 0) {
2372 if (ret == -EEXIST) {
2373 is_pipe_exists = 1;
2374 break;
2376 return is_pipe_exists;
2379 break;
2381 case SKL_TKN_U32_PIPE_CONFIG_ID:
2382 conf_idx = tkn_elem->value;
2383 break;
2385 case SKL_TKN_U32_PIPE_CONN_TYPE:
2386 case SKL_TKN_U32_PIPE_PRIORITY:
2387 case SKL_TKN_U32_PIPE_MEM_PGS:
2388 case SKL_TKN_U32_PMODE:
2389 case SKL_TKN_U32_PIPE_DIRECTION:
2390 case SKL_TKN_U32_NUM_CONFIGS:
2391 if (is_pipe_exists) {
2392 ret = skl_tplg_fill_pipe_tkn(dev, mconfig->pipe,
2393 tkn_elem->token, tkn_elem->value);
2394 if (ret < 0)
2395 return ret;
2398 break;
2400 case SKL_TKN_U32_PATH_MEM_PGS:
2401 case SKL_TKN_U32_CFG_FREQ:
2402 case SKL_TKN_U8_CFG_CHAN:
2403 case SKL_TKN_U8_CFG_BPS:
2404 if (mconfig->pipe->nr_cfgs) {
2405 ret = skl_tplg_fill_pipe_cfg(dev, mconfig->pipe,
2406 tkn_elem->token, tkn_elem->value,
2407 conf_idx, dir);
2408 if (ret < 0)
2409 return ret;
2411 break;
2413 case SKL_TKN_CFG_MOD_RES_ID:
2414 mconfig->mod_cfg[conf_idx].res_idx = tkn_elem->value;
2415 break;
2417 case SKL_TKN_CFG_MOD_FMT_ID:
2418 mconfig->mod_cfg[conf_idx].fmt_idx = tkn_elem->value;
2419 break;
2422 * SKL_TKN_U32_DIR_PIN_COUNT token has the value for both
2423 * direction and the pin count. The first four bits represent
2424 * direction and next four the pin count.
2426 case SKL_TKN_U32_DIR_PIN_COUNT:
2427 dir = tkn_elem->value & SKL_IN_DIR_BIT_MASK;
2428 pin_index = (tkn_elem->value &
2429 SKL_PIN_COUNT_MASK) >> 4;
2431 break;
2433 case SKL_TKN_U32_FMT_CH:
2434 case SKL_TKN_U32_FMT_FREQ:
2435 case SKL_TKN_U32_FMT_BIT_DEPTH:
2436 case SKL_TKN_U32_FMT_SAMPLE_SIZE:
2437 case SKL_TKN_U32_FMT_CH_CONFIG:
2438 case SKL_TKN_U32_FMT_INTERLEAVE:
2439 case SKL_TKN_U32_FMT_SAMPLE_TYPE:
2440 case SKL_TKN_U32_FMT_CH_MAP:
2441 ret = skl_tplg_widget_fill_fmt(dev, iface, tkn_elem->token,
2442 tkn_elem->value, dir, pin_index);
2444 if (ret < 0)
2445 return ret;
2447 break;
2449 case SKL_TKN_U32_PIN_MOD_ID:
2450 case SKL_TKN_U32_PIN_INST_ID:
2451 case SKL_TKN_UUID:
2452 ret = skl_tplg_fill_pins_info(dev,
2453 mconfig, tkn_elem, dir,
2454 pin_index);
2455 if (ret < 0)
2456 return ret;
2458 break;
2460 case SKL_TKN_U32_CAPS_SIZE:
2461 mconfig->formats_config.caps_size =
2462 tkn_elem->value;
2464 break;
2466 case SKL_TKN_U32_CAPS_SET_PARAMS:
2467 mconfig->formats_config.set_params =
2468 tkn_elem->value;
2469 break;
2471 case SKL_TKN_U32_CAPS_PARAMS_ID:
2472 mconfig->formats_config.param_id =
2473 tkn_elem->value;
2474 break;
2476 case SKL_TKN_U32_PROC_DOMAIN:
2477 mconfig->domain =
2478 tkn_elem->value;
2480 break;
2482 case SKL_TKN_U32_DMA_BUF_SIZE:
2483 mconfig->dma_buffer_size = tkn_elem->value;
2484 break;
2486 case SKL_TKN_U8_IN_PIN_TYPE:
2487 case SKL_TKN_U8_OUT_PIN_TYPE:
2488 case SKL_TKN_U8_CONN_TYPE:
2489 break;
2491 default:
2492 dev_err(dev, "Token %d not handled\n",
2493 tkn_elem->token);
2494 return -EINVAL;
2497 tkn_count++;
2499 return tkn_count;
2503 * Parse the vendor array for specific tokens to construct
2504 * module private data
2506 static int skl_tplg_get_tokens(struct device *dev,
2507 char *pvt_data, struct skl_dev *skl,
2508 struct skl_module_cfg *mconfig, int block_size)
2510 struct snd_soc_tplg_vendor_array *array;
2511 struct snd_soc_tplg_vendor_value_elem *tkn_elem;
2512 int tkn_count = 0, ret;
2513 int off = 0, tuple_size = 0;
2514 bool is_module_guid = true;
2516 if (block_size <= 0)
2517 return -EINVAL;
2519 while (tuple_size < block_size) {
2520 array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off);
2522 off += array->size;
2524 switch (array->type) {
2525 case SND_SOC_TPLG_TUPLE_TYPE_STRING:
2526 dev_warn(dev, "no string tokens expected for skl tplg\n");
2527 continue;
2529 case SND_SOC_TPLG_TUPLE_TYPE_UUID:
2530 if (is_module_guid) {
2531 ret = skl_tplg_get_uuid(dev, (guid_t *)mconfig->guid,
2532 array->uuid);
2533 is_module_guid = false;
2534 } else {
2535 ret = skl_tplg_get_token(dev, array->value, skl,
2536 mconfig);
2539 if (ret < 0)
2540 return ret;
2542 tuple_size += sizeof(*array->uuid);
2544 continue;
2546 default:
2547 tkn_elem = array->value;
2548 tkn_count = 0;
2549 break;
2552 while (tkn_count <= (array->num_elems - 1)) {
2553 ret = skl_tplg_get_token(dev, tkn_elem,
2554 skl, mconfig);
2556 if (ret < 0)
2557 return ret;
2559 tkn_count = tkn_count + ret;
2560 tkn_elem++;
2563 tuple_size += tkn_count * sizeof(*tkn_elem);
2566 return off;
2570 * Every data block is preceded by a descriptor to read the number
2571 * of data blocks, they type of the block and it's size
2573 static int skl_tplg_get_desc_blocks(struct device *dev,
2574 struct snd_soc_tplg_vendor_array *array)
2576 struct snd_soc_tplg_vendor_value_elem *tkn_elem;
2578 tkn_elem = array->value;
2580 switch (tkn_elem->token) {
2581 case SKL_TKN_U8_NUM_BLOCKS:
2582 case SKL_TKN_U8_BLOCK_TYPE:
2583 case SKL_TKN_U16_BLOCK_SIZE:
2584 return tkn_elem->value;
2586 default:
2587 dev_err(dev, "Invalid descriptor token %d\n", tkn_elem->token);
2588 break;
2591 return -EINVAL;
2594 /* Functions to parse private data from configuration file format v4 */
2597 * Add pipeline from topology binary into driver pipeline list
2599 * If already added we return that instance
2600 * Otherwise we create a new instance and add into driver list
2602 static int skl_tplg_add_pipe_v4(struct device *dev,
2603 struct skl_module_cfg *mconfig, struct skl_dev *skl,
2604 struct skl_dfw_v4_pipe *dfw_pipe)
2606 struct skl_pipeline *ppl;
2607 struct skl_pipe *pipe;
2608 struct skl_pipe_params *params;
2610 list_for_each_entry(ppl, &skl->ppl_list, node) {
2611 if (ppl->pipe->ppl_id == dfw_pipe->pipe_id) {
2612 mconfig->pipe = ppl->pipe;
2613 return 0;
2617 ppl = devm_kzalloc(dev, sizeof(*ppl), GFP_KERNEL);
2618 if (!ppl)
2619 return -ENOMEM;
2621 pipe = devm_kzalloc(dev, sizeof(*pipe), GFP_KERNEL);
2622 if (!pipe)
2623 return -ENOMEM;
2625 params = devm_kzalloc(dev, sizeof(*params), GFP_KERNEL);
2626 if (!params)
2627 return -ENOMEM;
2629 pipe->ppl_id = dfw_pipe->pipe_id;
2630 pipe->memory_pages = dfw_pipe->memory_pages;
2631 pipe->pipe_priority = dfw_pipe->pipe_priority;
2632 pipe->conn_type = dfw_pipe->conn_type;
2633 pipe->state = SKL_PIPE_INVALID;
2634 pipe->p_params = params;
2635 INIT_LIST_HEAD(&pipe->w_list);
2637 ppl->pipe = pipe;
2638 list_add(&ppl->node, &skl->ppl_list);
2640 mconfig->pipe = pipe;
2642 return 0;
2645 static void skl_fill_module_pin_info_v4(struct skl_dfw_v4_module_pin *dfw_pin,
2646 struct skl_module_pin *m_pin,
2647 bool is_dynamic, int max_pin)
2649 int i;
2651 for (i = 0; i < max_pin; i++) {
2652 m_pin[i].id.module_id = dfw_pin[i].module_id;
2653 m_pin[i].id.instance_id = dfw_pin[i].instance_id;
2654 m_pin[i].in_use = false;
2655 m_pin[i].is_dynamic = is_dynamic;
2656 m_pin[i].pin_state = SKL_PIN_UNBIND;
2660 static void skl_tplg_fill_fmt_v4(struct skl_module_pin_fmt *dst_fmt,
2661 struct skl_dfw_v4_module_fmt *src_fmt,
2662 int pins)
2664 int i;
2666 for (i = 0; i < pins; i++) {
2667 dst_fmt[i].fmt.channels = src_fmt[i].channels;
2668 dst_fmt[i].fmt.s_freq = src_fmt[i].freq;
2669 dst_fmt[i].fmt.bit_depth = src_fmt[i].bit_depth;
2670 dst_fmt[i].fmt.valid_bit_depth = src_fmt[i].valid_bit_depth;
2671 dst_fmt[i].fmt.ch_cfg = src_fmt[i].ch_cfg;
2672 dst_fmt[i].fmt.ch_map = src_fmt[i].ch_map;
2673 dst_fmt[i].fmt.interleaving_style =
2674 src_fmt[i].interleaving_style;
2675 dst_fmt[i].fmt.sample_type = src_fmt[i].sample_type;
2679 static int skl_tplg_get_pvt_data_v4(struct snd_soc_tplg_dapm_widget *tplg_w,
2680 struct skl_dev *skl, struct device *dev,
2681 struct skl_module_cfg *mconfig)
2683 struct skl_dfw_v4_module *dfw =
2684 (struct skl_dfw_v4_module *)tplg_w->priv.data;
2685 int ret;
2687 dev_dbg(dev, "Parsing Skylake v4 widget topology data\n");
2689 ret = guid_parse(dfw->uuid, (guid_t *)mconfig->guid);
2690 if (ret)
2691 return ret;
2692 mconfig->id.module_id = -1;
2693 mconfig->id.instance_id = dfw->instance_id;
2694 mconfig->module->resources[0].cpc = dfw->max_mcps / 1000;
2695 mconfig->module->resources[0].ibs = dfw->ibs;
2696 mconfig->module->resources[0].obs = dfw->obs;
2697 mconfig->core_id = dfw->core_id;
2698 mconfig->module->max_input_pins = dfw->max_in_queue;
2699 mconfig->module->max_output_pins = dfw->max_out_queue;
2700 mconfig->module->loadable = dfw->is_loadable;
2701 skl_tplg_fill_fmt_v4(mconfig->module->formats[0].inputs, dfw->in_fmt,
2702 MAX_IN_QUEUE);
2703 skl_tplg_fill_fmt_v4(mconfig->module->formats[0].outputs, dfw->out_fmt,
2704 MAX_OUT_QUEUE);
2706 mconfig->params_fixup = dfw->params_fixup;
2707 mconfig->converter = dfw->converter;
2708 mconfig->m_type = dfw->module_type;
2709 mconfig->vbus_id = dfw->vbus_id;
2710 mconfig->module->resources[0].is_pages = dfw->mem_pages;
2712 ret = skl_tplg_add_pipe_v4(dev, mconfig, skl, &dfw->pipe);
2713 if (ret)
2714 return ret;
2716 mconfig->dev_type = dfw->dev_type;
2717 mconfig->hw_conn_type = dfw->hw_conn_type;
2718 mconfig->time_slot = dfw->time_slot;
2719 mconfig->formats_config.caps_size = dfw->caps.caps_size;
2721 mconfig->m_in_pin = devm_kcalloc(dev,
2722 MAX_IN_QUEUE, sizeof(*mconfig->m_in_pin),
2723 GFP_KERNEL);
2724 if (!mconfig->m_in_pin)
2725 return -ENOMEM;
2727 mconfig->m_out_pin = devm_kcalloc(dev,
2728 MAX_OUT_QUEUE, sizeof(*mconfig->m_out_pin),
2729 GFP_KERNEL);
2730 if (!mconfig->m_out_pin)
2731 return -ENOMEM;
2733 skl_fill_module_pin_info_v4(dfw->in_pin, mconfig->m_in_pin,
2734 dfw->is_dynamic_in_pin,
2735 mconfig->module->max_input_pins);
2736 skl_fill_module_pin_info_v4(dfw->out_pin, mconfig->m_out_pin,
2737 dfw->is_dynamic_out_pin,
2738 mconfig->module->max_output_pins);
2740 if (mconfig->formats_config.caps_size) {
2741 mconfig->formats_config.set_params = dfw->caps.set_params;
2742 mconfig->formats_config.param_id = dfw->caps.param_id;
2743 mconfig->formats_config.caps =
2744 devm_kzalloc(dev, mconfig->formats_config.caps_size,
2745 GFP_KERNEL);
2746 if (!mconfig->formats_config.caps)
2747 return -ENOMEM;
2748 memcpy(mconfig->formats_config.caps, dfw->caps.caps,
2749 dfw->caps.caps_size);
2752 return 0;
2756 * Parse the private data for the token and corresponding value.
2757 * The private data can have multiple data blocks. So, a data block
2758 * is preceded by a descriptor for number of blocks and a descriptor
2759 * for the type and size of the suceeding data block.
2761 static int skl_tplg_get_pvt_data(struct snd_soc_tplg_dapm_widget *tplg_w,
2762 struct skl_dev *skl, struct device *dev,
2763 struct skl_module_cfg *mconfig)
2765 struct snd_soc_tplg_vendor_array *array;
2766 int num_blocks, block_size = 0, block_type, off = 0;
2767 char *data;
2768 int ret;
2771 * v4 configuration files have a valid UUID at the start of
2772 * the widget's private data.
2774 if (uuid_is_valid((char *)tplg_w->priv.data))
2775 return skl_tplg_get_pvt_data_v4(tplg_w, skl, dev, mconfig);
2777 /* Read the NUM_DATA_BLOCKS descriptor */
2778 array = (struct snd_soc_tplg_vendor_array *)tplg_w->priv.data;
2779 ret = skl_tplg_get_desc_blocks(dev, array);
2780 if (ret < 0)
2781 return ret;
2782 num_blocks = ret;
2784 off += array->size;
2785 /* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */
2786 while (num_blocks > 0) {
2787 array = (struct snd_soc_tplg_vendor_array *)
2788 (tplg_w->priv.data + off);
2790 ret = skl_tplg_get_desc_blocks(dev, array);
2792 if (ret < 0)
2793 return ret;
2794 block_type = ret;
2795 off += array->size;
2797 array = (struct snd_soc_tplg_vendor_array *)
2798 (tplg_w->priv.data + off);
2800 ret = skl_tplg_get_desc_blocks(dev, array);
2802 if (ret < 0)
2803 return ret;
2804 block_size = ret;
2805 off += array->size;
2807 array = (struct snd_soc_tplg_vendor_array *)
2808 (tplg_w->priv.data + off);
2810 data = (tplg_w->priv.data + off);
2812 if (block_type == SKL_TYPE_TUPLE) {
2813 ret = skl_tplg_get_tokens(dev, data,
2814 skl, mconfig, block_size);
2816 if (ret < 0)
2817 return ret;
2819 --num_blocks;
2820 } else {
2821 if (mconfig->formats_config.caps_size > 0)
2822 memcpy(mconfig->formats_config.caps, data,
2823 mconfig->formats_config.caps_size);
2824 --num_blocks;
2825 ret = mconfig->formats_config.caps_size;
2827 off += ret;
2830 return 0;
2833 static void skl_clear_pin_config(struct snd_soc_component *component,
2834 struct snd_soc_dapm_widget *w)
2836 int i;
2837 struct skl_module_cfg *mconfig;
2838 struct skl_pipe *pipe;
2840 if (!strncmp(w->dapm->component->name, component->name,
2841 strlen(component->name))) {
2842 mconfig = w->priv;
2843 pipe = mconfig->pipe;
2844 for (i = 0; i < mconfig->module->max_input_pins; i++) {
2845 mconfig->m_in_pin[i].in_use = false;
2846 mconfig->m_in_pin[i].pin_state = SKL_PIN_UNBIND;
2848 for (i = 0; i < mconfig->module->max_output_pins; i++) {
2849 mconfig->m_out_pin[i].in_use = false;
2850 mconfig->m_out_pin[i].pin_state = SKL_PIN_UNBIND;
2852 pipe->state = SKL_PIPE_INVALID;
2853 mconfig->m_state = SKL_MODULE_UNINIT;
2857 void skl_cleanup_resources(struct skl_dev *skl)
2859 struct snd_soc_component *soc_component = skl->component;
2860 struct snd_soc_dapm_widget *w;
2861 struct snd_soc_card *card;
2863 if (soc_component == NULL)
2864 return;
2866 card = soc_component->card;
2867 if (!card || !card->instantiated)
2868 return;
2870 list_for_each_entry(w, &card->widgets, list) {
2871 if (is_skl_dsp_widget_type(w, skl->dev) && w->priv != NULL)
2872 skl_clear_pin_config(soc_component, w);
2875 skl_clear_module_cnt(skl->dsp);
2879 * Topology core widget load callback
2881 * This is used to save the private data for each widget which gives
2882 * information to the driver about module and pipeline parameters which DSP
2883 * FW expects like ids, resource values, formats etc
2885 static int skl_tplg_widget_load(struct snd_soc_component *cmpnt, int index,
2886 struct snd_soc_dapm_widget *w,
2887 struct snd_soc_tplg_dapm_widget *tplg_w)
2889 int ret;
2890 struct hdac_bus *bus = snd_soc_component_get_drvdata(cmpnt);
2891 struct skl_dev *skl = bus_to_skl(bus);
2892 struct skl_module_cfg *mconfig;
2894 if (!tplg_w->priv.size)
2895 goto bind_event;
2897 mconfig = devm_kzalloc(bus->dev, sizeof(*mconfig), GFP_KERNEL);
2899 if (!mconfig)
2900 return -ENOMEM;
2902 if (skl->nr_modules == 0) {
2903 mconfig->module = devm_kzalloc(bus->dev,
2904 sizeof(*mconfig->module), GFP_KERNEL);
2905 if (!mconfig->module)
2906 return -ENOMEM;
2909 w->priv = mconfig;
2912 * module binary can be loaded later, so set it to query when
2913 * module is load for a use case
2915 mconfig->id.module_id = -1;
2917 /* Parse private data for tuples */
2918 ret = skl_tplg_get_pvt_data(tplg_w, skl, bus->dev, mconfig);
2919 if (ret < 0)
2920 return ret;
2922 skl_debug_init_module(skl->debugfs, w, mconfig);
2924 bind_event:
2925 if (tplg_w->event_type == 0) {
2926 dev_dbg(bus->dev, "ASoC: No event handler required\n");
2927 return 0;
2930 ret = snd_soc_tplg_widget_bind_event(w, skl_tplg_widget_ops,
2931 ARRAY_SIZE(skl_tplg_widget_ops),
2932 tplg_w->event_type);
2934 if (ret) {
2935 dev_err(bus->dev, "%s: No matching event handlers found for %d\n",
2936 __func__, tplg_w->event_type);
2937 return -EINVAL;
2940 return 0;
2943 static int skl_init_algo_data(struct device *dev, struct soc_bytes_ext *be,
2944 struct snd_soc_tplg_bytes_control *bc)
2946 struct skl_algo_data *ac;
2947 struct skl_dfw_algo_data *dfw_ac =
2948 (struct skl_dfw_algo_data *)bc->priv.data;
2950 ac = devm_kzalloc(dev, sizeof(*ac), GFP_KERNEL);
2951 if (!ac)
2952 return -ENOMEM;
2954 /* Fill private data */
2955 ac->max = dfw_ac->max;
2956 ac->param_id = dfw_ac->param_id;
2957 ac->set_params = dfw_ac->set_params;
2958 ac->size = dfw_ac->max;
2960 if (ac->max) {
2961 ac->params = devm_kzalloc(dev, ac->max, GFP_KERNEL);
2962 if (!ac->params)
2963 return -ENOMEM;
2965 memcpy(ac->params, dfw_ac->params, ac->max);
2968 be->dobj.private = ac;
2969 return 0;
2972 static int skl_init_enum_data(struct device *dev, struct soc_enum *se,
2973 struct snd_soc_tplg_enum_control *ec)
2976 void *data;
2978 if (ec->priv.size) {
2979 data = devm_kzalloc(dev, sizeof(ec->priv.size), GFP_KERNEL);
2980 if (!data)
2981 return -ENOMEM;
2982 memcpy(data, ec->priv.data, ec->priv.size);
2983 se->dobj.private = data;
2986 return 0;
2990 static int skl_tplg_control_load(struct snd_soc_component *cmpnt,
2991 int index,
2992 struct snd_kcontrol_new *kctl,
2993 struct snd_soc_tplg_ctl_hdr *hdr)
2995 struct soc_bytes_ext *sb;
2996 struct snd_soc_tplg_bytes_control *tplg_bc;
2997 struct snd_soc_tplg_enum_control *tplg_ec;
2998 struct hdac_bus *bus = snd_soc_component_get_drvdata(cmpnt);
2999 struct soc_enum *se;
3001 switch (hdr->ops.info) {
3002 case SND_SOC_TPLG_CTL_BYTES:
3003 tplg_bc = container_of(hdr,
3004 struct snd_soc_tplg_bytes_control, hdr);
3005 if (kctl->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
3006 sb = (struct soc_bytes_ext *)kctl->private_value;
3007 if (tplg_bc->priv.size)
3008 return skl_init_algo_data(
3009 bus->dev, sb, tplg_bc);
3011 break;
3013 case SND_SOC_TPLG_CTL_ENUM:
3014 tplg_ec = container_of(hdr,
3015 struct snd_soc_tplg_enum_control, hdr);
3016 if (kctl->access & SNDRV_CTL_ELEM_ACCESS_READWRITE) {
3017 se = (struct soc_enum *)kctl->private_value;
3018 if (tplg_ec->priv.size)
3019 return skl_init_enum_data(bus->dev, se,
3020 tplg_ec);
3022 break;
3024 default:
3025 dev_dbg(bus->dev, "Control load not supported %d:%d:%d\n",
3026 hdr->ops.get, hdr->ops.put, hdr->ops.info);
3027 break;
3030 return 0;
3033 static int skl_tplg_fill_str_mfest_tkn(struct device *dev,
3034 struct snd_soc_tplg_vendor_string_elem *str_elem,
3035 struct skl_dev *skl)
3037 int tkn_count = 0;
3038 static int ref_count;
3040 switch (str_elem->token) {
3041 case SKL_TKN_STR_LIB_NAME:
3042 if (ref_count > skl->lib_count - 1) {
3043 ref_count = 0;
3044 return -EINVAL;
3047 strncpy(skl->lib_info[ref_count].name,
3048 str_elem->string,
3049 ARRAY_SIZE(skl->lib_info[ref_count].name));
3050 ref_count++;
3051 break;
3053 default:
3054 dev_err(dev, "Not a string token %d\n", str_elem->token);
3055 break;
3057 tkn_count++;
3059 return tkn_count;
3062 static int skl_tplg_get_str_tkn(struct device *dev,
3063 struct snd_soc_tplg_vendor_array *array,
3064 struct skl_dev *skl)
3066 int tkn_count = 0, ret;
3067 struct snd_soc_tplg_vendor_string_elem *str_elem;
3069 str_elem = (struct snd_soc_tplg_vendor_string_elem *)array->value;
3070 while (tkn_count < array->num_elems) {
3071 ret = skl_tplg_fill_str_mfest_tkn(dev, str_elem, skl);
3072 str_elem++;
3074 if (ret < 0)
3075 return ret;
3077 tkn_count = tkn_count + ret;
3080 return tkn_count;
3083 static int skl_tplg_manifest_fill_fmt(struct device *dev,
3084 struct skl_module_iface *fmt,
3085 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
3086 u32 dir, int fmt_idx)
3088 struct skl_module_pin_fmt *dst_fmt;
3089 struct skl_module_fmt *mod_fmt;
3090 int ret;
3092 if (!fmt)
3093 return -EINVAL;
3095 switch (dir) {
3096 case SKL_DIR_IN:
3097 dst_fmt = &fmt->inputs[fmt_idx];
3098 break;
3100 case SKL_DIR_OUT:
3101 dst_fmt = &fmt->outputs[fmt_idx];
3102 break;
3104 default:
3105 dev_err(dev, "Invalid direction: %d\n", dir);
3106 return -EINVAL;
3109 mod_fmt = &dst_fmt->fmt;
3111 switch (tkn_elem->token) {
3112 case SKL_TKN_MM_U32_INTF_PIN_ID:
3113 dst_fmt->id = tkn_elem->value;
3114 break;
3116 default:
3117 ret = skl_tplg_fill_fmt(dev, mod_fmt, tkn_elem->token,
3118 tkn_elem->value);
3119 if (ret < 0)
3120 return ret;
3121 break;
3124 return 0;
3127 static int skl_tplg_fill_mod_info(struct device *dev,
3128 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
3129 struct skl_module *mod)
3132 if (!mod)
3133 return -EINVAL;
3135 switch (tkn_elem->token) {
3136 case SKL_TKN_U8_IN_PIN_TYPE:
3137 mod->input_pin_type = tkn_elem->value;
3138 break;
3140 case SKL_TKN_U8_OUT_PIN_TYPE:
3141 mod->output_pin_type = tkn_elem->value;
3142 break;
3144 case SKL_TKN_U8_IN_QUEUE_COUNT:
3145 mod->max_input_pins = tkn_elem->value;
3146 break;
3148 case SKL_TKN_U8_OUT_QUEUE_COUNT:
3149 mod->max_output_pins = tkn_elem->value;
3150 break;
3152 case SKL_TKN_MM_U8_NUM_RES:
3153 mod->nr_resources = tkn_elem->value;
3154 break;
3156 case SKL_TKN_MM_U8_NUM_INTF:
3157 mod->nr_interfaces = tkn_elem->value;
3158 break;
3160 default:
3161 dev_err(dev, "Invalid mod info token %d", tkn_elem->token);
3162 return -EINVAL;
3165 return 0;
3169 static int skl_tplg_get_int_tkn(struct device *dev,
3170 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
3171 struct skl_dev *skl)
3173 int tkn_count = 0, ret;
3174 static int mod_idx, res_val_idx, intf_val_idx, dir, pin_idx;
3175 struct skl_module_res *res = NULL;
3176 struct skl_module_iface *fmt = NULL;
3177 struct skl_module *mod = NULL;
3178 static struct skl_astate_param *astate_table;
3179 static int astate_cfg_idx, count;
3180 int i;
3181 size_t size;
3183 if (skl->modules) {
3184 mod = skl->modules[mod_idx];
3185 res = &mod->resources[res_val_idx];
3186 fmt = &mod->formats[intf_val_idx];
3189 switch (tkn_elem->token) {
3190 case SKL_TKN_U32_LIB_COUNT:
3191 skl->lib_count = tkn_elem->value;
3192 break;
3194 case SKL_TKN_U8_NUM_MOD:
3195 skl->nr_modules = tkn_elem->value;
3196 skl->modules = devm_kcalloc(dev, skl->nr_modules,
3197 sizeof(*skl->modules), GFP_KERNEL);
3198 if (!skl->modules)
3199 return -ENOMEM;
3201 for (i = 0; i < skl->nr_modules; i++) {
3202 skl->modules[i] = devm_kzalloc(dev,
3203 sizeof(struct skl_module), GFP_KERNEL);
3204 if (!skl->modules[i])
3205 return -ENOMEM;
3207 break;
3209 case SKL_TKN_MM_U8_MOD_IDX:
3210 mod_idx = tkn_elem->value;
3211 break;
3213 case SKL_TKN_U32_ASTATE_COUNT:
3214 if (astate_table != NULL) {
3215 dev_err(dev, "More than one entry for A-State count");
3216 return -EINVAL;
3219 if (tkn_elem->value > SKL_MAX_ASTATE_CFG) {
3220 dev_err(dev, "Invalid A-State count %d\n",
3221 tkn_elem->value);
3222 return -EINVAL;
3225 size = struct_size(skl->cfg.astate_cfg, astate_table,
3226 tkn_elem->value);
3227 skl->cfg.astate_cfg = devm_kzalloc(dev, size, GFP_KERNEL);
3228 if (!skl->cfg.astate_cfg)
3229 return -ENOMEM;
3231 astate_table = skl->cfg.astate_cfg->astate_table;
3232 count = skl->cfg.astate_cfg->count = tkn_elem->value;
3233 break;
3235 case SKL_TKN_U32_ASTATE_IDX:
3236 if (tkn_elem->value >= count) {
3237 dev_err(dev, "Invalid A-State index %d\n",
3238 tkn_elem->value);
3239 return -EINVAL;
3242 astate_cfg_idx = tkn_elem->value;
3243 break;
3245 case SKL_TKN_U32_ASTATE_KCPS:
3246 astate_table[astate_cfg_idx].kcps = tkn_elem->value;
3247 break;
3249 case SKL_TKN_U32_ASTATE_CLK_SRC:
3250 astate_table[astate_cfg_idx].clk_src = tkn_elem->value;
3251 break;
3253 case SKL_TKN_U8_IN_PIN_TYPE:
3254 case SKL_TKN_U8_OUT_PIN_TYPE:
3255 case SKL_TKN_U8_IN_QUEUE_COUNT:
3256 case SKL_TKN_U8_OUT_QUEUE_COUNT:
3257 case SKL_TKN_MM_U8_NUM_RES:
3258 case SKL_TKN_MM_U8_NUM_INTF:
3259 ret = skl_tplg_fill_mod_info(dev, tkn_elem, mod);
3260 if (ret < 0)
3261 return ret;
3262 break;
3264 case SKL_TKN_U32_DIR_PIN_COUNT:
3265 dir = tkn_elem->value & SKL_IN_DIR_BIT_MASK;
3266 pin_idx = (tkn_elem->value & SKL_PIN_COUNT_MASK) >> 4;
3267 break;
3269 case SKL_TKN_MM_U32_RES_ID:
3270 if (!res)
3271 return -EINVAL;
3273 res->id = tkn_elem->value;
3274 res_val_idx = tkn_elem->value;
3275 break;
3277 case SKL_TKN_MM_U32_FMT_ID:
3278 if (!fmt)
3279 return -EINVAL;
3281 fmt->fmt_idx = tkn_elem->value;
3282 intf_val_idx = tkn_elem->value;
3283 break;
3285 case SKL_TKN_MM_U32_CPS:
3286 case SKL_TKN_MM_U32_DMA_SIZE:
3287 case SKL_TKN_MM_U32_CPC:
3288 case SKL_TKN_U32_MEM_PAGES:
3289 case SKL_TKN_U32_OBS:
3290 case SKL_TKN_U32_IBS:
3291 case SKL_TKN_MM_U32_RES_PIN_ID:
3292 case SKL_TKN_MM_U32_PIN_BUF:
3293 ret = skl_tplg_fill_res_tkn(dev, tkn_elem, res, pin_idx, dir);
3294 if (ret < 0)
3295 return ret;
3297 break;
3299 case SKL_TKN_MM_U32_NUM_IN_FMT:
3300 if (!fmt)
3301 return -EINVAL;
3303 res->nr_input_pins = tkn_elem->value;
3304 break;
3306 case SKL_TKN_MM_U32_NUM_OUT_FMT:
3307 if (!fmt)
3308 return -EINVAL;
3310 res->nr_output_pins = tkn_elem->value;
3311 break;
3313 case SKL_TKN_U32_FMT_CH:
3314 case SKL_TKN_U32_FMT_FREQ:
3315 case SKL_TKN_U32_FMT_BIT_DEPTH:
3316 case SKL_TKN_U32_FMT_SAMPLE_SIZE:
3317 case SKL_TKN_U32_FMT_CH_CONFIG:
3318 case SKL_TKN_U32_FMT_INTERLEAVE:
3319 case SKL_TKN_U32_FMT_SAMPLE_TYPE:
3320 case SKL_TKN_U32_FMT_CH_MAP:
3321 case SKL_TKN_MM_U32_INTF_PIN_ID:
3322 ret = skl_tplg_manifest_fill_fmt(dev, fmt, tkn_elem,
3323 dir, pin_idx);
3324 if (ret < 0)
3325 return ret;
3326 break;
3328 default:
3329 dev_err(dev, "Not a manifest token %d\n", tkn_elem->token);
3330 return -EINVAL;
3332 tkn_count++;
3334 return tkn_count;
3338 * Fill the manifest structure by parsing the tokens based on the
3339 * type.
3341 static int skl_tplg_get_manifest_tkn(struct device *dev,
3342 char *pvt_data, struct skl_dev *skl,
3343 int block_size)
3345 int tkn_count = 0, ret;
3346 int off = 0, tuple_size = 0;
3347 u8 uuid_index = 0;
3348 struct snd_soc_tplg_vendor_array *array;
3349 struct snd_soc_tplg_vendor_value_elem *tkn_elem;
3351 if (block_size <= 0)
3352 return -EINVAL;
3354 while (tuple_size < block_size) {
3355 array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off);
3356 off += array->size;
3357 switch (array->type) {
3358 case SND_SOC_TPLG_TUPLE_TYPE_STRING:
3359 ret = skl_tplg_get_str_tkn(dev, array, skl);
3361 if (ret < 0)
3362 return ret;
3363 tkn_count = ret;
3365 tuple_size += tkn_count *
3366 sizeof(struct snd_soc_tplg_vendor_string_elem);
3367 continue;
3369 case SND_SOC_TPLG_TUPLE_TYPE_UUID:
3370 if (array->uuid->token != SKL_TKN_UUID) {
3371 dev_err(dev, "Not an UUID token: %d\n",
3372 array->uuid->token);
3373 return -EINVAL;
3375 if (uuid_index >= skl->nr_modules) {
3376 dev_err(dev, "Too many UUID tokens\n");
3377 return -EINVAL;
3379 guid_copy(&skl->modules[uuid_index++]->uuid,
3380 (guid_t *)&array->uuid->uuid);
3382 tuple_size += sizeof(*array->uuid);
3383 continue;
3385 default:
3386 tkn_elem = array->value;
3387 tkn_count = 0;
3388 break;
3391 while (tkn_count <= array->num_elems - 1) {
3392 ret = skl_tplg_get_int_tkn(dev,
3393 tkn_elem, skl);
3394 if (ret < 0)
3395 return ret;
3397 tkn_count = tkn_count + ret;
3398 tkn_elem++;
3400 tuple_size += (tkn_count * sizeof(*tkn_elem));
3401 tkn_count = 0;
3404 return off;
3408 * Parse manifest private data for tokens. The private data block is
3409 * preceded by descriptors for type and size of data block.
3411 static int skl_tplg_get_manifest_data(struct snd_soc_tplg_manifest *manifest,
3412 struct device *dev, struct skl_dev *skl)
3414 struct snd_soc_tplg_vendor_array *array;
3415 int num_blocks, block_size = 0, block_type, off = 0;
3416 char *data;
3417 int ret;
3419 /* Read the NUM_DATA_BLOCKS descriptor */
3420 array = (struct snd_soc_tplg_vendor_array *)manifest->priv.data;
3421 ret = skl_tplg_get_desc_blocks(dev, array);
3422 if (ret < 0)
3423 return ret;
3424 num_blocks = ret;
3426 off += array->size;
3427 /* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */
3428 while (num_blocks > 0) {
3429 array = (struct snd_soc_tplg_vendor_array *)
3430 (manifest->priv.data + off);
3431 ret = skl_tplg_get_desc_blocks(dev, array);
3433 if (ret < 0)
3434 return ret;
3435 block_type = ret;
3436 off += array->size;
3438 array = (struct snd_soc_tplg_vendor_array *)
3439 (manifest->priv.data + off);
3441 ret = skl_tplg_get_desc_blocks(dev, array);
3443 if (ret < 0)
3444 return ret;
3445 block_size = ret;
3446 off += array->size;
3448 array = (struct snd_soc_tplg_vendor_array *)
3449 (manifest->priv.data + off);
3451 data = (manifest->priv.data + off);
3453 if (block_type == SKL_TYPE_TUPLE) {
3454 ret = skl_tplg_get_manifest_tkn(dev, data, skl,
3455 block_size);
3457 if (ret < 0)
3458 return ret;
3460 --num_blocks;
3461 } else {
3462 return -EINVAL;
3464 off += ret;
3467 return 0;
3470 static int skl_manifest_load(struct snd_soc_component *cmpnt, int index,
3471 struct snd_soc_tplg_manifest *manifest)
3473 struct hdac_bus *bus = snd_soc_component_get_drvdata(cmpnt);
3474 struct skl_dev *skl = bus_to_skl(bus);
3476 /* proceed only if we have private data defined */
3477 if (manifest->priv.size == 0)
3478 return 0;
3480 skl_tplg_get_manifest_data(manifest, bus->dev, skl);
3482 if (skl->lib_count > SKL_MAX_LIB) {
3483 dev_err(bus->dev, "Exceeding max Library count. Got:%d\n",
3484 skl->lib_count);
3485 return -EINVAL;
3488 return 0;
3491 static struct snd_soc_tplg_ops skl_tplg_ops = {
3492 .widget_load = skl_tplg_widget_load,
3493 .control_load = skl_tplg_control_load,
3494 .bytes_ext_ops = skl_tlv_ops,
3495 .bytes_ext_ops_count = ARRAY_SIZE(skl_tlv_ops),
3496 .io_ops = skl_tplg_kcontrol_ops,
3497 .io_ops_count = ARRAY_SIZE(skl_tplg_kcontrol_ops),
3498 .manifest = skl_manifest_load,
3499 .dai_load = skl_dai_load,
3503 * A pipe can have multiple modules, each of them will be a DAPM widget as
3504 * well. While managing a pipeline we need to get the list of all the
3505 * widgets in a pipelines, so this helper - skl_tplg_create_pipe_widget_list()
3506 * helps to get the SKL type widgets in that pipeline
3508 static int skl_tplg_create_pipe_widget_list(struct snd_soc_component *component)
3510 struct snd_soc_dapm_widget *w;
3511 struct skl_module_cfg *mcfg = NULL;
3512 struct skl_pipe_module *p_module = NULL;
3513 struct skl_pipe *pipe;
3515 list_for_each_entry(w, &component->card->widgets, list) {
3516 if (is_skl_dsp_widget_type(w, component->dev) && w->priv) {
3517 mcfg = w->priv;
3518 pipe = mcfg->pipe;
3520 p_module = devm_kzalloc(component->dev,
3521 sizeof(*p_module), GFP_KERNEL);
3522 if (!p_module)
3523 return -ENOMEM;
3525 p_module->w = w;
3526 list_add_tail(&p_module->node, &pipe->w_list);
3530 return 0;
3533 static void skl_tplg_set_pipe_type(struct skl_dev *skl, struct skl_pipe *pipe)
3535 struct skl_pipe_module *w_module;
3536 struct snd_soc_dapm_widget *w;
3537 struct skl_module_cfg *mconfig;
3538 bool host_found = false, link_found = false;
3540 list_for_each_entry(w_module, &pipe->w_list, node) {
3541 w = w_module->w;
3542 mconfig = w->priv;
3544 if (mconfig->dev_type == SKL_DEVICE_HDAHOST)
3545 host_found = true;
3546 else if (mconfig->dev_type != SKL_DEVICE_NONE)
3547 link_found = true;
3550 if (host_found && link_found)
3551 pipe->passthru = true;
3552 else
3553 pipe->passthru = false;
3557 * SKL topology init routine
3559 int skl_tplg_init(struct snd_soc_component *component, struct hdac_bus *bus)
3561 int ret;
3562 const struct firmware *fw;
3563 struct skl_dev *skl = bus_to_skl(bus);
3564 struct skl_pipeline *ppl;
3566 ret = request_firmware(&fw, skl->tplg_name, bus->dev);
3567 if (ret < 0) {
3568 dev_info(bus->dev, "tplg fw %s load failed with %d, falling back to dfw_sst.bin",
3569 skl->tplg_name, ret);
3570 ret = request_firmware(&fw, "dfw_sst.bin", bus->dev);
3571 if (ret < 0) {
3572 dev_err(bus->dev, "Fallback tplg fw %s load failed with %d\n",
3573 "dfw_sst.bin", ret);
3574 return ret;
3579 * The complete tplg for SKL is loaded as index 0, we don't use
3580 * any other index
3582 ret = snd_soc_tplg_component_load(component, &skl_tplg_ops, fw, 0);
3583 if (ret < 0) {
3584 dev_err(bus->dev, "tplg component load failed%d\n", ret);
3585 goto err;
3588 ret = skl_tplg_create_pipe_widget_list(component);
3589 if (ret < 0) {
3590 dev_err(bus->dev, "tplg create pipe widget list failed%d\n",
3591 ret);
3592 goto err;
3595 list_for_each_entry(ppl, &skl->ppl_list, node)
3596 skl_tplg_set_pipe_type(skl, ppl->pipe);
3598 err:
3599 release_firmware(fw);
3600 return ret;
3603 void skl_tplg_exit(struct snd_soc_component *component, struct hdac_bus *bus)
3605 struct skl_dev *skl = bus_to_skl(bus);
3606 struct skl_pipeline *ppl, *tmp;
3608 if (!list_empty(&skl->ppl_list))
3609 list_for_each_entry_safe(ppl, tmp, &skl->ppl_list, node)
3610 list_del(&ppl->node);
3612 /* clean up topology */
3613 snd_soc_tplg_component_remove(component, SND_SOC_TPLG_INDEX_ALL);