Merge tag 'amd-drm-fixes-5.11-2021-01-28' of https://gitlab.freedesktop.org/agd5f...
[linux-2.6-microblaze.git] / sound / soc / intel / skylake / skl-topology.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  skl-topology.c - Implements Platform component ALSA controls/widget
4  *  handlers.
5  *
6  *  Copyright (C) 2014-2015 Intel Corp
7  *  Author: Jeeja KP <jeeja.kp@intel.com>
8  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9  */
10
11 #include <linux/slab.h>
12 #include <linux/types.h>
13 #include <linux/firmware.h>
14 #include <linux/uuid.h>
15 #include <sound/intel-nhlt.h>
16 #include <sound/soc.h>
17 #include <sound/soc-acpi.h>
18 #include <sound/soc-topology.h>
19 #include <uapi/sound/snd_sst_tokens.h>
20 #include <uapi/sound/skl-tplg-interface.h>
21 #include "skl-sst-dsp.h"
22 #include "skl-sst-ipc.h"
23 #include "skl-topology.h"
24 #include "skl.h"
25 #include "../common/sst-dsp.h"
26 #include "../common/sst-dsp-priv.h"
27
28 #define SKL_CH_FIXUP_MASK               (1 << 0)
29 #define SKL_RATE_FIXUP_MASK             (1 << 1)
30 #define SKL_FMT_FIXUP_MASK              (1 << 2)
31 #define SKL_IN_DIR_BIT_MASK             BIT(0)
32 #define SKL_PIN_COUNT_MASK              GENMASK(7, 4)
33
34 static const int mic_mono_list[] = {
35 0, 1, 2, 3,
36 };
37 static const int mic_stereo_list[][SKL_CH_STEREO] = {
38 {0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 3}, {2, 3},
39 };
40 static const int mic_trio_list[][SKL_CH_TRIO] = {
41 {0, 1, 2}, {0, 1, 3}, {0, 2, 3}, {1, 2, 3},
42 };
43 static const int mic_quatro_list[][SKL_CH_QUATRO] = {
44 {0, 1, 2, 3},
45 };
46
47 #define CHECK_HW_PARAMS(ch, freq, bps, prm_ch, prm_freq, prm_bps) \
48         ((ch == prm_ch) && (bps == prm_bps) && (freq == prm_freq))
49
50 void skl_tplg_d0i3_get(struct skl_dev *skl, enum d0i3_capability caps)
51 {
52         struct skl_d0i3_data *d0i3 =  &skl->d0i3;
53
54         switch (caps) {
55         case SKL_D0I3_NONE:
56                 d0i3->non_d0i3++;
57                 break;
58
59         case SKL_D0I3_STREAMING:
60                 d0i3->streaming++;
61                 break;
62
63         case SKL_D0I3_NON_STREAMING:
64                 d0i3->non_streaming++;
65                 break;
66         }
67 }
68
69 void skl_tplg_d0i3_put(struct skl_dev *skl, enum d0i3_capability caps)
70 {
71         struct skl_d0i3_data *d0i3 =  &skl->d0i3;
72
73         switch (caps) {
74         case SKL_D0I3_NONE:
75                 d0i3->non_d0i3--;
76                 break;
77
78         case SKL_D0I3_STREAMING:
79                 d0i3->streaming--;
80                 break;
81
82         case SKL_D0I3_NON_STREAMING:
83                 d0i3->non_streaming--;
84                 break;
85         }
86 }
87
88 /*
89  * SKL DSP driver modelling uses only few DAPM widgets so for rest we will
90  * ignore. This helpers checks if the SKL driver handles this widget type
91  */
92 static int is_skl_dsp_widget_type(struct snd_soc_dapm_widget *w,
93                                   struct device *dev)
94 {
95         if (w->dapm->dev != dev)
96                 return false;
97
98         switch (w->id) {
99         case snd_soc_dapm_dai_link:
100         case snd_soc_dapm_dai_in:
101         case snd_soc_dapm_aif_in:
102         case snd_soc_dapm_aif_out:
103         case snd_soc_dapm_dai_out:
104         case snd_soc_dapm_switch:
105         case snd_soc_dapm_output:
106         case snd_soc_dapm_mux:
107
108                 return false;
109         default:
110                 return true;
111         }
112 }
113
114 static void skl_dump_mconfig(struct skl_dev *skl, struct skl_module_cfg *mcfg)
115 {
116         struct skl_module_iface *iface = &mcfg->module->formats[0];
117
118         dev_dbg(skl->dev, "Dumping config\n");
119         dev_dbg(skl->dev, "Input Format:\n");
120         dev_dbg(skl->dev, "channels = %d\n", iface->inputs[0].fmt.channels);
121         dev_dbg(skl->dev, "s_freq = %d\n", iface->inputs[0].fmt.s_freq);
122         dev_dbg(skl->dev, "ch_cfg = %d\n", iface->inputs[0].fmt.ch_cfg);
123         dev_dbg(skl->dev, "valid bit depth = %d\n",
124                                 iface->inputs[0].fmt.valid_bit_depth);
125         dev_dbg(skl->dev, "Output Format:\n");
126         dev_dbg(skl->dev, "channels = %d\n", iface->outputs[0].fmt.channels);
127         dev_dbg(skl->dev, "s_freq = %d\n", iface->outputs[0].fmt.s_freq);
128         dev_dbg(skl->dev, "valid bit depth = %d\n",
129                                 iface->outputs[0].fmt.valid_bit_depth);
130         dev_dbg(skl->dev, "ch_cfg = %d\n", iface->outputs[0].fmt.ch_cfg);
131 }
132
133 static void skl_tplg_update_chmap(struct skl_module_fmt *fmt, int chs)
134 {
135         int slot_map = 0xFFFFFFFF;
136         int start_slot = 0;
137         int i;
138
139         for (i = 0; i < chs; i++) {
140                 /*
141                  * For 2 channels with starting slot as 0, slot map will
142                  * look like 0xFFFFFF10.
143                  */
144                 slot_map &= (~(0xF << (4 * i)) | (start_slot << (4 * i)));
145                 start_slot++;
146         }
147         fmt->ch_map = slot_map;
148 }
149
150 static void skl_tplg_update_params(struct skl_module_fmt *fmt,
151                         struct skl_pipe_params *params, int fixup)
152 {
153         if (fixup & SKL_RATE_FIXUP_MASK)
154                 fmt->s_freq = params->s_freq;
155         if (fixup & SKL_CH_FIXUP_MASK) {
156                 fmt->channels = params->ch;
157                 skl_tplg_update_chmap(fmt, fmt->channels);
158         }
159         if (fixup & SKL_FMT_FIXUP_MASK) {
160                 fmt->valid_bit_depth = skl_get_bit_depth(params->s_fmt);
161
162                 /*
163                  * 16 bit is 16 bit container whereas 24 bit is in 32 bit
164                  * container so update bit depth accordingly
165                  */
166                 switch (fmt->valid_bit_depth) {
167                 case SKL_DEPTH_16BIT:
168                         fmt->bit_depth = fmt->valid_bit_depth;
169                         break;
170
171                 default:
172                         fmt->bit_depth = SKL_DEPTH_32BIT;
173                         break;
174                 }
175         }
176
177 }
178
179 /*
180  * A pipeline may have modules which impact the pcm parameters, like SRC,
181  * channel converter, format converter.
182  * We need to calculate the output params by applying the 'fixup'
183  * Topology will tell driver which type of fixup is to be applied by
184  * supplying the fixup mask, so based on that we calculate the output
185  *
186  * Now In FE the pcm hw_params is source/target format. Same is applicable
187  * for BE with its hw_params invoked.
188  * here based on FE, BE pipeline and direction we calculate the input and
189  * outfix and then apply that for a module
190  */
191 static void skl_tplg_update_params_fixup(struct skl_module_cfg *m_cfg,
192                 struct skl_pipe_params *params, bool is_fe)
193 {
194         int in_fixup, out_fixup;
195         struct skl_module_fmt *in_fmt, *out_fmt;
196
197         /* Fixups will be applied to pin 0 only */
198         in_fmt = &m_cfg->module->formats[0].inputs[0].fmt;
199         out_fmt = &m_cfg->module->formats[0].outputs[0].fmt;
200
201         if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
202                 if (is_fe) {
203                         in_fixup = m_cfg->params_fixup;
204                         out_fixup = (~m_cfg->converter) &
205                                         m_cfg->params_fixup;
206                 } else {
207                         out_fixup = m_cfg->params_fixup;
208                         in_fixup = (~m_cfg->converter) &
209                                         m_cfg->params_fixup;
210                 }
211         } else {
212                 if (is_fe) {
213                         out_fixup = m_cfg->params_fixup;
214                         in_fixup = (~m_cfg->converter) &
215                                         m_cfg->params_fixup;
216                 } else {
217                         in_fixup = m_cfg->params_fixup;
218                         out_fixup = (~m_cfg->converter) &
219                                         m_cfg->params_fixup;
220                 }
221         }
222
223         skl_tplg_update_params(in_fmt, params, in_fixup);
224         skl_tplg_update_params(out_fmt, params, out_fixup);
225 }
226
227 /*
228  * A module needs input and output buffers, which are dependent upon pcm
229  * params, so once we have calculate params, we need buffer calculation as
230  * well.
231  */
232 static void skl_tplg_update_buffer_size(struct skl_dev *skl,
233                                 struct skl_module_cfg *mcfg)
234 {
235         int multiplier = 1;
236         struct skl_module_fmt *in_fmt, *out_fmt;
237         struct skl_module_res *res;
238
239         /* Since fixups is applied to pin 0 only, ibs, obs needs
240          * change for pin 0 only
241          */
242         res = &mcfg->module->resources[0];
243         in_fmt = &mcfg->module->formats[0].inputs[0].fmt;
244         out_fmt = &mcfg->module->formats[0].outputs[0].fmt;
245
246         if (mcfg->m_type == SKL_MODULE_TYPE_SRCINT)
247                 multiplier = 5;
248
249         res->ibs = DIV_ROUND_UP(in_fmt->s_freq, 1000) *
250                         in_fmt->channels * (in_fmt->bit_depth >> 3) *
251                         multiplier;
252
253         res->obs = DIV_ROUND_UP(out_fmt->s_freq, 1000) *
254                         out_fmt->channels * (out_fmt->bit_depth >> 3) *
255                         multiplier;
256 }
257
258 static u8 skl_tplg_be_dev_type(int dev_type)
259 {
260         int ret;
261
262         switch (dev_type) {
263         case SKL_DEVICE_BT:
264                 ret = NHLT_DEVICE_BT;
265                 break;
266
267         case SKL_DEVICE_DMIC:
268                 ret = NHLT_DEVICE_DMIC;
269                 break;
270
271         case SKL_DEVICE_I2S:
272                 ret = NHLT_DEVICE_I2S;
273                 break;
274
275         default:
276                 ret = NHLT_DEVICE_INVALID;
277                 break;
278         }
279
280         return ret;
281 }
282
283 static int skl_tplg_update_be_blob(struct snd_soc_dapm_widget *w,
284                                                 struct skl_dev *skl)
285 {
286         struct skl_module_cfg *m_cfg = w->priv;
287         int link_type, dir;
288         u32 ch, s_freq, s_fmt;
289         struct nhlt_specific_cfg *cfg;
290         u8 dev_type = skl_tplg_be_dev_type(m_cfg->dev_type);
291         int fmt_idx = m_cfg->fmt_idx;
292         struct skl_module_iface *m_iface = &m_cfg->module->formats[fmt_idx];
293
294         /* check if we already have blob */
295         if (m_cfg->formats_config.caps_size > 0)
296                 return 0;
297
298         dev_dbg(skl->dev, "Applying default cfg blob\n");
299         switch (m_cfg->dev_type) {
300         case SKL_DEVICE_DMIC:
301                 link_type = NHLT_LINK_DMIC;
302                 dir = SNDRV_PCM_STREAM_CAPTURE;
303                 s_freq = m_iface->inputs[0].fmt.s_freq;
304                 s_fmt = m_iface->inputs[0].fmt.bit_depth;
305                 ch = m_iface->inputs[0].fmt.channels;
306                 break;
307
308         case SKL_DEVICE_I2S:
309                 link_type = NHLT_LINK_SSP;
310                 if (m_cfg->hw_conn_type == SKL_CONN_SOURCE) {
311                         dir = SNDRV_PCM_STREAM_PLAYBACK;
312                         s_freq = m_iface->outputs[0].fmt.s_freq;
313                         s_fmt = m_iface->outputs[0].fmt.bit_depth;
314                         ch = m_iface->outputs[0].fmt.channels;
315                 } else {
316                         dir = SNDRV_PCM_STREAM_CAPTURE;
317                         s_freq = m_iface->inputs[0].fmt.s_freq;
318                         s_fmt = m_iface->inputs[0].fmt.bit_depth;
319                         ch = m_iface->inputs[0].fmt.channels;
320                 }
321                 break;
322
323         default:
324                 return -EINVAL;
325         }
326
327         /* update the blob based on virtual bus_id and default params */
328         cfg = skl_get_ep_blob(skl, m_cfg->vbus_id, link_type,
329                                         s_fmt, ch, s_freq, dir, dev_type);
330         if (cfg) {
331                 m_cfg->formats_config.caps_size = cfg->size;
332                 m_cfg->formats_config.caps = (u32 *) &cfg->caps;
333         } else {
334                 dev_err(skl->dev, "Blob NULL for id %x type %d dirn %d\n",
335                                         m_cfg->vbus_id, link_type, dir);
336                 dev_err(skl->dev, "PCM: ch %d, freq %d, fmt %d\n",
337                                         ch, s_freq, s_fmt);
338                 return -EIO;
339         }
340
341         return 0;
342 }
343
344 static void skl_tplg_update_module_params(struct snd_soc_dapm_widget *w,
345                                                         struct skl_dev *skl)
346 {
347         struct skl_module_cfg *m_cfg = w->priv;
348         struct skl_pipe_params *params = m_cfg->pipe->p_params;
349         int p_conn_type = m_cfg->pipe->conn_type;
350         bool is_fe;
351
352         if (!m_cfg->params_fixup)
353                 return;
354
355         dev_dbg(skl->dev, "Mconfig for widget=%s BEFORE updation\n",
356                                 w->name);
357
358         skl_dump_mconfig(skl, m_cfg);
359
360         if (p_conn_type == SKL_PIPE_CONN_TYPE_FE)
361                 is_fe = true;
362         else
363                 is_fe = false;
364
365         skl_tplg_update_params_fixup(m_cfg, params, is_fe);
366         skl_tplg_update_buffer_size(skl, m_cfg);
367
368         dev_dbg(skl->dev, "Mconfig for widget=%s AFTER updation\n",
369                                 w->name);
370
371         skl_dump_mconfig(skl, m_cfg);
372 }
373
374 /*
375  * some modules can have multiple params set from user control and
376  * need to be set after module is initialized. If set_param flag is
377  * set module params will be done after module is initialised.
378  */
379 static int skl_tplg_set_module_params(struct snd_soc_dapm_widget *w,
380                                                 struct skl_dev *skl)
381 {
382         int i, ret;
383         struct skl_module_cfg *mconfig = w->priv;
384         const struct snd_kcontrol_new *k;
385         struct soc_bytes_ext *sb;
386         struct skl_algo_data *bc;
387         struct skl_specific_cfg *sp_cfg;
388
389         if (mconfig->formats_config.caps_size > 0 &&
390                 mconfig->formats_config.set_params == SKL_PARAM_SET) {
391                 sp_cfg = &mconfig->formats_config;
392                 ret = skl_set_module_params(skl, sp_cfg->caps,
393                                         sp_cfg->caps_size,
394                                         sp_cfg->param_id, mconfig);
395                 if (ret < 0)
396                         return ret;
397         }
398
399         for (i = 0; i < w->num_kcontrols; i++) {
400                 k = &w->kcontrol_news[i];
401                 if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
402                         sb = (void *) k->private_value;
403                         bc = (struct skl_algo_data *)sb->dobj.private;
404
405                         if (bc->set_params == SKL_PARAM_SET) {
406                                 ret = skl_set_module_params(skl,
407                                                 (u32 *)bc->params, bc->size,
408                                                 bc->param_id, mconfig);
409                                 if (ret < 0)
410                                         return ret;
411                         }
412                 }
413         }
414
415         return 0;
416 }
417
418 /*
419  * some module param can set from user control and this is required as
420  * when module is initailzed. if module param is required in init it is
421  * identifed by set_param flag. if set_param flag is not set, then this
422  * parameter needs to set as part of module init.
423  */
424 static int skl_tplg_set_module_init_data(struct snd_soc_dapm_widget *w)
425 {
426         const struct snd_kcontrol_new *k;
427         struct soc_bytes_ext *sb;
428         struct skl_algo_data *bc;
429         struct skl_module_cfg *mconfig = w->priv;
430         int i;
431
432         for (i = 0; i < w->num_kcontrols; i++) {
433                 k = &w->kcontrol_news[i];
434                 if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
435                         sb = (struct soc_bytes_ext *)k->private_value;
436                         bc = (struct skl_algo_data *)sb->dobj.private;
437
438                         if (bc->set_params != SKL_PARAM_INIT)
439                                 continue;
440
441                         mconfig->formats_config.caps = (u32 *)bc->params;
442                         mconfig->formats_config.caps_size = bc->size;
443
444                         break;
445                 }
446         }
447
448         return 0;
449 }
450
451 static int skl_tplg_module_prepare(struct skl_dev *skl, struct skl_pipe *pipe,
452                 struct snd_soc_dapm_widget *w, struct skl_module_cfg *mcfg)
453 {
454         switch (mcfg->dev_type) {
455         case SKL_DEVICE_HDAHOST:
456                 return skl_pcm_host_dma_prepare(skl->dev, pipe->p_params);
457
458         case SKL_DEVICE_HDALINK:
459                 return skl_pcm_link_dma_prepare(skl->dev, pipe->p_params);
460         }
461
462         return 0;
463 }
464
465 /*
466  * Inside a pipe instance, we can have various modules. These modules need
467  * to instantiated in DSP by invoking INIT_MODULE IPC, which is achieved by
468  * skl_init_module() routine, so invoke that for all modules in a pipeline
469  */
470 static int
471 skl_tplg_init_pipe_modules(struct skl_dev *skl, struct skl_pipe *pipe)
472 {
473         struct skl_pipe_module *w_module;
474         struct snd_soc_dapm_widget *w;
475         struct skl_module_cfg *mconfig;
476         u8 cfg_idx;
477         int ret = 0;
478
479         list_for_each_entry(w_module, &pipe->w_list, node) {
480                 guid_t *uuid_mod;
481                 w = w_module->w;
482                 mconfig = w->priv;
483
484                 /* check if module ids are populated */
485                 if (mconfig->id.module_id < 0) {
486                         dev_err(skl->dev,
487                                         "module %pUL id not populated\n",
488                                         (guid_t *)mconfig->guid);
489                         return -EIO;
490                 }
491
492                 cfg_idx = mconfig->pipe->cur_config_idx;
493                 mconfig->fmt_idx = mconfig->mod_cfg[cfg_idx].fmt_idx;
494                 mconfig->res_idx = mconfig->mod_cfg[cfg_idx].res_idx;
495
496                 if (mconfig->module->loadable && skl->dsp->fw_ops.load_mod) {
497                         ret = skl->dsp->fw_ops.load_mod(skl->dsp,
498                                 mconfig->id.module_id, mconfig->guid);
499                         if (ret < 0)
500                                 return ret;
501
502                         mconfig->m_state = SKL_MODULE_LOADED;
503                 }
504
505                 /* prepare the DMA if the module is gateway cpr */
506                 ret = skl_tplg_module_prepare(skl, pipe, w, mconfig);
507                 if (ret < 0)
508                         return ret;
509
510                 /* update blob if blob is null for be with default value */
511                 skl_tplg_update_be_blob(w, skl);
512
513                 /*
514                  * apply fix/conversion to module params based on
515                  * FE/BE params
516                  */
517                 skl_tplg_update_module_params(w, skl);
518                 uuid_mod = (guid_t *)mconfig->guid;
519                 mconfig->id.pvt_id = skl_get_pvt_id(skl, uuid_mod,
520                                                 mconfig->id.instance_id);
521                 if (mconfig->id.pvt_id < 0)
522                         return ret;
523                 skl_tplg_set_module_init_data(w);
524
525                 ret = skl_dsp_get_core(skl->dsp, mconfig->core_id);
526                 if (ret < 0) {
527                         dev_err(skl->dev, "Failed to wake up core %d ret=%d\n",
528                                                 mconfig->core_id, ret);
529                         return ret;
530                 }
531
532                 ret = skl_init_module(skl, mconfig);
533                 if (ret < 0) {
534                         skl_put_pvt_id(skl, uuid_mod, &mconfig->id.pvt_id);
535                         goto err;
536                 }
537
538                 ret = skl_tplg_set_module_params(w, skl);
539                 if (ret < 0)
540                         goto err;
541         }
542
543         return 0;
544 err:
545         skl_dsp_put_core(skl->dsp, mconfig->core_id);
546         return ret;
547 }
548
549 static int skl_tplg_unload_pipe_modules(struct skl_dev *skl,
550          struct skl_pipe *pipe)
551 {
552         int ret = 0;
553         struct skl_pipe_module *w_module;
554         struct skl_module_cfg *mconfig;
555
556         list_for_each_entry(w_module, &pipe->w_list, node) {
557                 guid_t *uuid_mod;
558                 mconfig  = w_module->w->priv;
559                 uuid_mod = (guid_t *)mconfig->guid;
560
561                 if (mconfig->module->loadable && skl->dsp->fw_ops.unload_mod &&
562                         mconfig->m_state > SKL_MODULE_UNINIT) {
563                         ret = skl->dsp->fw_ops.unload_mod(skl->dsp,
564                                                 mconfig->id.module_id);
565                         if (ret < 0)
566                                 return -EIO;
567                 }
568                 skl_put_pvt_id(skl, uuid_mod, &mconfig->id.pvt_id);
569
570                 ret = skl_dsp_put_core(skl->dsp, mconfig->core_id);
571                 if (ret < 0) {
572                         /* don't return; continue with other modules */
573                         dev_err(skl->dev, "Failed to sleep core %d ret=%d\n",
574                                 mconfig->core_id, ret);
575                 }
576         }
577
578         /* no modules to unload in this path, so return */
579         return ret;
580 }
581
582 static bool skl_tplg_is_multi_fmt(struct skl_dev *skl, struct skl_pipe *pipe)
583 {
584         struct skl_pipe_fmt *cur_fmt;
585         struct skl_pipe_fmt *next_fmt;
586         int i;
587
588         if (pipe->nr_cfgs <= 1)
589                 return false;
590
591         if (pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
592                 return true;
593
594         for (i = 0; i < pipe->nr_cfgs - 1; i++) {
595                 if (pipe->direction == SNDRV_PCM_STREAM_PLAYBACK) {
596                         cur_fmt = &pipe->configs[i].out_fmt;
597                         next_fmt = &pipe->configs[i + 1].out_fmt;
598                 } else {
599                         cur_fmt = &pipe->configs[i].in_fmt;
600                         next_fmt = &pipe->configs[i + 1].in_fmt;
601                 }
602
603                 if (!CHECK_HW_PARAMS(cur_fmt->channels, cur_fmt->freq,
604                                      cur_fmt->bps,
605                                      next_fmt->channels,
606                                      next_fmt->freq,
607                                      next_fmt->bps))
608                         return true;
609         }
610
611         return false;
612 }
613
614 /*
615  * Here, we select pipe format based on the pipe type and pipe
616  * direction to determine the current config index for the pipeline.
617  * The config index is then used to select proper module resources.
618  * Intermediate pipes currently have a fixed format hence we select the
619  * 0th configuratation by default for such pipes.
620  */
621 static int
622 skl_tplg_get_pipe_config(struct skl_dev *skl, struct skl_module_cfg *mconfig)
623 {
624         struct skl_pipe *pipe = mconfig->pipe;
625         struct skl_pipe_params *params = pipe->p_params;
626         struct skl_path_config *pconfig = &pipe->configs[0];
627         struct skl_pipe_fmt *fmt = NULL;
628         bool in_fmt = false;
629         int i;
630
631         if (pipe->nr_cfgs == 0) {
632                 pipe->cur_config_idx = 0;
633                 return 0;
634         }
635
636         if (skl_tplg_is_multi_fmt(skl, pipe)) {
637                 pipe->cur_config_idx = pipe->pipe_config_idx;
638                 pipe->memory_pages = pconfig->mem_pages;
639                 dev_dbg(skl->dev, "found pipe config idx:%d\n",
640                         pipe->cur_config_idx);
641                 return 0;
642         }
643
644         if (pipe->conn_type == SKL_PIPE_CONN_TYPE_NONE) {
645                 dev_dbg(skl->dev, "No conn_type detected, take 0th config\n");
646                 pipe->cur_config_idx = 0;
647                 pipe->memory_pages = pconfig->mem_pages;
648
649                 return 0;
650         }
651
652         if ((pipe->conn_type == SKL_PIPE_CONN_TYPE_FE &&
653              pipe->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
654              (pipe->conn_type == SKL_PIPE_CONN_TYPE_BE &&
655              pipe->direction == SNDRV_PCM_STREAM_CAPTURE))
656                 in_fmt = true;
657
658         for (i = 0; i < pipe->nr_cfgs; i++) {
659                 pconfig = &pipe->configs[i];
660                 if (in_fmt)
661                         fmt = &pconfig->in_fmt;
662                 else
663                         fmt = &pconfig->out_fmt;
664
665                 if (CHECK_HW_PARAMS(params->ch, params->s_freq, params->s_fmt,
666                                     fmt->channels, fmt->freq, fmt->bps)) {
667                         pipe->cur_config_idx = i;
668                         pipe->memory_pages = pconfig->mem_pages;
669                         dev_dbg(skl->dev, "Using pipe config: %d\n", i);
670
671                         return 0;
672                 }
673         }
674
675         dev_err(skl->dev, "Invalid pipe config: %d %d %d for pipe: %d\n",
676                 params->ch, params->s_freq, params->s_fmt, pipe->ppl_id);
677         return -EINVAL;
678 }
679
680 /*
681  * Mixer module represents a pipeline. So in the Pre-PMU event of mixer we
682  * need create the pipeline. So we do following:
683  *   - Create the pipeline
684  *   - Initialize the modules in pipeline
685  *   - finally bind all modules together
686  */
687 static int skl_tplg_mixer_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
688                                                         struct skl_dev *skl)
689 {
690         int ret;
691         struct skl_module_cfg *mconfig = w->priv;
692         struct skl_pipe_module *w_module;
693         struct skl_pipe *s_pipe = mconfig->pipe;
694         struct skl_module_cfg *src_module = NULL, *dst_module, *module;
695         struct skl_module_deferred_bind *modules;
696
697         ret = skl_tplg_get_pipe_config(skl, mconfig);
698         if (ret < 0)
699                 return ret;
700
701         /*
702          * Create a list of modules for pipe.
703          * This list contains modules from source to sink
704          */
705         ret = skl_create_pipeline(skl, mconfig->pipe);
706         if (ret < 0)
707                 return ret;
708
709         /* Init all pipe modules from source to sink */
710         ret = skl_tplg_init_pipe_modules(skl, s_pipe);
711         if (ret < 0)
712                 return ret;
713
714         /* Bind modules from source to sink */
715         list_for_each_entry(w_module, &s_pipe->w_list, node) {
716                 dst_module = w_module->w->priv;
717
718                 if (src_module == NULL) {
719                         src_module = dst_module;
720                         continue;
721                 }
722
723                 ret = skl_bind_modules(skl, src_module, dst_module);
724                 if (ret < 0)
725                         return ret;
726
727                 src_module = dst_module;
728         }
729
730         /*
731          * When the destination module is initialized, check for these modules
732          * in deferred bind list. If found, bind them.
733          */
734         list_for_each_entry(w_module, &s_pipe->w_list, node) {
735                 if (list_empty(&skl->bind_list))
736                         break;
737
738                 list_for_each_entry(modules, &skl->bind_list, node) {
739                         module = w_module->w->priv;
740                         if (modules->dst == module)
741                                 skl_bind_modules(skl, modules->src,
742                                                         modules->dst);
743                 }
744         }
745
746         return 0;
747 }
748
749 static int skl_fill_sink_instance_id(struct skl_dev *skl, u32 *params,
750                                 int size, struct skl_module_cfg *mcfg)
751 {
752         int i, pvt_id;
753
754         if (mcfg->m_type == SKL_MODULE_TYPE_KPB) {
755                 struct skl_kpb_params *kpb_params =
756                                 (struct skl_kpb_params *)params;
757                 struct skl_mod_inst_map *inst = kpb_params->u.map;
758
759                 for (i = 0; i < kpb_params->num_modules; i++) {
760                         pvt_id = skl_get_pvt_instance_id_map(skl, inst->mod_id,
761                                                                 inst->inst_id);
762                         if (pvt_id < 0)
763                                 return -EINVAL;
764
765                         inst->inst_id = pvt_id;
766                         inst++;
767                 }
768         }
769
770         return 0;
771 }
772 /*
773  * Some modules require params to be set after the module is bound to
774  * all pins connected.
775  *
776  * The module provider initializes set_param flag for such modules and we
777  * send params after binding
778  */
779 static int skl_tplg_set_module_bind_params(struct snd_soc_dapm_widget *w,
780                         struct skl_module_cfg *mcfg, struct skl_dev *skl)
781 {
782         int i, ret;
783         struct skl_module_cfg *mconfig = w->priv;
784         const struct snd_kcontrol_new *k;
785         struct soc_bytes_ext *sb;
786         struct skl_algo_data *bc;
787         struct skl_specific_cfg *sp_cfg;
788         u32 *params;
789
790         /*
791          * check all out/in pins are in bind state.
792          * if so set the module param
793          */
794         for (i = 0; i < mcfg->module->max_output_pins; i++) {
795                 if (mcfg->m_out_pin[i].pin_state != SKL_PIN_BIND_DONE)
796                         return 0;
797         }
798
799         for (i = 0; i < mcfg->module->max_input_pins; i++) {
800                 if (mcfg->m_in_pin[i].pin_state != SKL_PIN_BIND_DONE)
801                         return 0;
802         }
803
804         if (mconfig->formats_config.caps_size > 0 &&
805                 mconfig->formats_config.set_params == SKL_PARAM_BIND) {
806                 sp_cfg = &mconfig->formats_config;
807                 ret = skl_set_module_params(skl, sp_cfg->caps,
808                                         sp_cfg->caps_size,
809                                         sp_cfg->param_id, mconfig);
810                 if (ret < 0)
811                         return ret;
812         }
813
814         for (i = 0; i < w->num_kcontrols; i++) {
815                 k = &w->kcontrol_news[i];
816                 if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
817                         sb = (void *) k->private_value;
818                         bc = (struct skl_algo_data *)sb->dobj.private;
819
820                         if (bc->set_params == SKL_PARAM_BIND) {
821                                 params = kmemdup(bc->params, bc->max, GFP_KERNEL);
822                                 if (!params)
823                                         return -ENOMEM;
824
825                                 skl_fill_sink_instance_id(skl, params, bc->max,
826                                                                 mconfig);
827
828                                 ret = skl_set_module_params(skl, params,
829                                                 bc->max, bc->param_id, mconfig);
830                                 kfree(params);
831
832                                 if (ret < 0)
833                                         return ret;
834                         }
835                 }
836         }
837
838         return 0;
839 }
840
841 static int skl_get_module_id(struct skl_dev *skl, guid_t *uuid)
842 {
843         struct uuid_module *module;
844
845         list_for_each_entry(module, &skl->uuid_list, list) {
846                 if (guid_equal(uuid, &module->uuid))
847                         return module->id;
848         }
849
850         return -EINVAL;
851 }
852
853 static int skl_tplg_find_moduleid_from_uuid(struct skl_dev *skl,
854                                         const struct snd_kcontrol_new *k)
855 {
856         struct soc_bytes_ext *sb = (void *) k->private_value;
857         struct skl_algo_data *bc = (struct skl_algo_data *)sb->dobj.private;
858         struct skl_kpb_params *uuid_params, *params;
859         struct hdac_bus *bus = skl_to_bus(skl);
860         int i, size, module_id;
861
862         if (bc->set_params == SKL_PARAM_BIND && bc->max) {
863                 uuid_params = (struct skl_kpb_params *)bc->params;
864                 size = struct_size(params, u.map, uuid_params->num_modules);
865
866                 params = devm_kzalloc(bus->dev, size, GFP_KERNEL);
867                 if (!params)
868                         return -ENOMEM;
869
870                 params->num_modules = uuid_params->num_modules;
871
872                 for (i = 0; i < uuid_params->num_modules; i++) {
873                         module_id = skl_get_module_id(skl,
874                                 &uuid_params->u.map_uuid[i].mod_uuid);
875                         if (module_id < 0) {
876                                 devm_kfree(bus->dev, params);
877                                 return -EINVAL;
878                         }
879
880                         params->u.map[i].mod_id = module_id;
881                         params->u.map[i].inst_id =
882                                 uuid_params->u.map_uuid[i].inst_id;
883                 }
884
885                 devm_kfree(bus->dev, bc->params);
886                 bc->params = (char *)params;
887                 bc->max = size;
888         }
889
890         return 0;
891 }
892
893 /*
894  * Retrieve the module id from UUID mentioned in the
895  * post bind params
896  */
897 void skl_tplg_add_moduleid_in_bind_params(struct skl_dev *skl,
898                                 struct snd_soc_dapm_widget *w)
899 {
900         struct skl_module_cfg *mconfig = w->priv;
901         int i;
902
903         /*
904          * Post bind params are used for only for KPB
905          * to set copier instances to drain the data
906          * in fast mode
907          */
908         if (mconfig->m_type != SKL_MODULE_TYPE_KPB)
909                 return;
910
911         for (i = 0; i < w->num_kcontrols; i++)
912                 if ((w->kcontrol_news[i].access &
913                         SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) &&
914                         (skl_tplg_find_moduleid_from_uuid(skl,
915                         &w->kcontrol_news[i]) < 0))
916                         dev_err(skl->dev,
917                                 "%s: invalid kpb post bind params\n",
918                                 __func__);
919 }
920
921 static int skl_tplg_module_add_deferred_bind(struct skl_dev *skl,
922         struct skl_module_cfg *src, struct skl_module_cfg *dst)
923 {
924         struct skl_module_deferred_bind *m_list, *modules;
925         int i;
926
927         /* only supported for module with static pin connection */
928         for (i = 0; i < dst->module->max_input_pins; i++) {
929                 struct skl_module_pin *pin = &dst->m_in_pin[i];
930
931                 if (pin->is_dynamic)
932                         continue;
933
934                 if ((pin->id.module_id  == src->id.module_id) &&
935                         (pin->id.instance_id  == src->id.instance_id)) {
936
937                         if (!list_empty(&skl->bind_list)) {
938                                 list_for_each_entry(modules, &skl->bind_list, node) {
939                                         if (modules->src == src && modules->dst == dst)
940                                                 return 0;
941                                 }
942                         }
943
944                         m_list = kzalloc(sizeof(*m_list), GFP_KERNEL);
945                         if (!m_list)
946                                 return -ENOMEM;
947
948                         m_list->src = src;
949                         m_list->dst = dst;
950
951                         list_add(&m_list->node, &skl->bind_list);
952                 }
953         }
954
955         return 0;
956 }
957
958 static int skl_tplg_bind_sinks(struct snd_soc_dapm_widget *w,
959                                 struct skl_dev *skl,
960                                 struct snd_soc_dapm_widget *src_w,
961                                 struct skl_module_cfg *src_mconfig)
962 {
963         struct snd_soc_dapm_path *p;
964         struct snd_soc_dapm_widget *sink = NULL, *next_sink = NULL;
965         struct skl_module_cfg *sink_mconfig;
966         int ret;
967
968         snd_soc_dapm_widget_for_each_sink_path(w, p) {
969                 if (!p->connect)
970                         continue;
971
972                 dev_dbg(skl->dev,
973                         "%s: src widget=%s\n", __func__, w->name);
974                 dev_dbg(skl->dev,
975                         "%s: sink widget=%s\n", __func__, p->sink->name);
976
977                 next_sink = p->sink;
978
979                 if (!is_skl_dsp_widget_type(p->sink, skl->dev))
980                         return skl_tplg_bind_sinks(p->sink, skl, src_w, src_mconfig);
981
982                 /*
983                  * here we will check widgets in sink pipelines, so that
984                  * can be any widgets type and we are only interested if
985                  * they are ones used for SKL so check that first
986                  */
987                 if ((p->sink->priv != NULL) &&
988                                 is_skl_dsp_widget_type(p->sink, skl->dev)) {
989
990                         sink = p->sink;
991                         sink_mconfig = sink->priv;
992
993                         /*
994                          * Modules other than PGA leaf can be connected
995                          * directly or via switch to a module in another
996                          * pipeline. EX: reference path
997                          * when the path is enabled, the dst module that needs
998                          * to be bound may not be initialized. if the module is
999                          * not initialized, add these modules in the deferred
1000                          * bind list and when the dst module is initialised,
1001                          * bind this module to the dst_module in deferred list.
1002                          */
1003                         if (((src_mconfig->m_state == SKL_MODULE_INIT_DONE)
1004                                 && (sink_mconfig->m_state == SKL_MODULE_UNINIT))) {
1005
1006                                 ret = skl_tplg_module_add_deferred_bind(skl,
1007                                                 src_mconfig, sink_mconfig);
1008
1009                                 if (ret < 0)
1010                                         return ret;
1011
1012                         }
1013
1014
1015                         if (src_mconfig->m_state == SKL_MODULE_UNINIT ||
1016                                 sink_mconfig->m_state == SKL_MODULE_UNINIT)
1017                                 continue;
1018
1019                         /* Bind source to sink, mixin is always source */
1020                         ret = skl_bind_modules(skl, src_mconfig, sink_mconfig);
1021                         if (ret)
1022                                 return ret;
1023
1024                         /* set module params after bind */
1025                         skl_tplg_set_module_bind_params(src_w,
1026                                         src_mconfig, skl);
1027                         skl_tplg_set_module_bind_params(sink,
1028                                         sink_mconfig, skl);
1029
1030                         /* Start sinks pipe first */
1031                         if (sink_mconfig->pipe->state != SKL_PIPE_STARTED) {
1032                                 if (sink_mconfig->pipe->conn_type !=
1033                                                         SKL_PIPE_CONN_TYPE_FE)
1034                                         ret = skl_run_pipe(skl,
1035                                                         sink_mconfig->pipe);
1036                                 if (ret)
1037                                         return ret;
1038                         }
1039                 }
1040         }
1041
1042         if (!sink && next_sink)
1043                 return skl_tplg_bind_sinks(next_sink, skl, src_w, src_mconfig);
1044
1045         return 0;
1046 }
1047
1048 /*
1049  * A PGA represents a module in a pipeline. So in the Pre-PMU event of PGA
1050  * we need to do following:
1051  *   - Bind to sink pipeline
1052  *      Since the sink pipes can be running and we don't get mixer event on
1053  *      connect for already running mixer, we need to find the sink pipes
1054  *      here and bind to them. This way dynamic connect works.
1055  *   - Start sink pipeline, if not running
1056  *   - Then run current pipe
1057  */
1058 static int skl_tplg_pga_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
1059                                                         struct skl_dev *skl)
1060 {
1061         struct skl_module_cfg *src_mconfig;
1062         int ret = 0;
1063
1064         src_mconfig = w->priv;
1065
1066         /*
1067          * find which sink it is connected to, bind with the sink,
1068          * if sink is not started, start sink pipe first, then start
1069          * this pipe
1070          */
1071         ret = skl_tplg_bind_sinks(w, skl, w, src_mconfig);
1072         if (ret)
1073                 return ret;
1074
1075         /* Start source pipe last after starting all sinks */
1076         if (src_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
1077                 return skl_run_pipe(skl, src_mconfig->pipe);
1078
1079         return 0;
1080 }
1081
1082 static struct snd_soc_dapm_widget *skl_get_src_dsp_widget(
1083                 struct snd_soc_dapm_widget *w, struct skl_dev *skl)
1084 {
1085         struct snd_soc_dapm_path *p;
1086         struct snd_soc_dapm_widget *src_w = NULL;
1087
1088         snd_soc_dapm_widget_for_each_source_path(w, p) {
1089                 src_w = p->source;
1090                 if (!p->connect)
1091                         continue;
1092
1093                 dev_dbg(skl->dev, "sink widget=%s\n", w->name);
1094                 dev_dbg(skl->dev, "src widget=%s\n", p->source->name);
1095
1096                 /*
1097                  * here we will check widgets in sink pipelines, so that can
1098                  * be any widgets type and we are only interested if they are
1099                  * ones used for SKL so check that first
1100                  */
1101                 if ((p->source->priv != NULL) &&
1102                                 is_skl_dsp_widget_type(p->source, skl->dev)) {
1103                         return p->source;
1104                 }
1105         }
1106
1107         if (src_w != NULL)
1108                 return skl_get_src_dsp_widget(src_w, skl);
1109
1110         return NULL;
1111 }
1112
1113 /*
1114  * in the Post-PMU event of mixer we need to do following:
1115  *   - Check if this pipe is running
1116  *   - if not, then
1117  *      - bind this pipeline to its source pipeline
1118  *        if source pipe is already running, this means it is a dynamic
1119  *        connection and we need to bind only to that pipe
1120  *      - start this pipeline
1121  */
1122 static int skl_tplg_mixer_dapm_post_pmu_event(struct snd_soc_dapm_widget *w,
1123                                                         struct skl_dev *skl)
1124 {
1125         int ret = 0;
1126         struct snd_soc_dapm_widget *source, *sink;
1127         struct skl_module_cfg *src_mconfig, *sink_mconfig;
1128         int src_pipe_started = 0;
1129
1130         sink = w;
1131         sink_mconfig = sink->priv;
1132
1133         /*
1134          * If source pipe is already started, that means source is driving
1135          * one more sink before this sink got connected, Since source is
1136          * started, bind this sink to source and start this pipe.
1137          */
1138         source = skl_get_src_dsp_widget(w, skl);
1139         if (source != NULL) {
1140                 src_mconfig = source->priv;
1141                 sink_mconfig = sink->priv;
1142                 src_pipe_started = 1;
1143
1144                 /*
1145                  * check pipe state, then no need to bind or start the
1146                  * pipe
1147                  */
1148                 if (src_mconfig->pipe->state != SKL_PIPE_STARTED)
1149                         src_pipe_started = 0;
1150         }
1151
1152         if (src_pipe_started) {
1153                 ret = skl_bind_modules(skl, src_mconfig, sink_mconfig);
1154                 if (ret)
1155                         return ret;
1156
1157                 /* set module params after bind */
1158                 skl_tplg_set_module_bind_params(source, src_mconfig, skl);
1159                 skl_tplg_set_module_bind_params(sink, sink_mconfig, skl);
1160
1161                 if (sink_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
1162                         ret = skl_run_pipe(skl, sink_mconfig->pipe);
1163         }
1164
1165         return ret;
1166 }
1167
1168 /*
1169  * in the Pre-PMD event of mixer we need to do following:
1170  *   - Stop the pipe
1171  *   - find the source connections and remove that from dapm_path_list
1172  *   - unbind with source pipelines if still connected
1173  */
1174 static int skl_tplg_mixer_dapm_pre_pmd_event(struct snd_soc_dapm_widget *w,
1175                                                         struct skl_dev *skl)
1176 {
1177         struct skl_module_cfg *src_mconfig, *sink_mconfig;
1178         int ret = 0, i;
1179
1180         sink_mconfig = w->priv;
1181
1182         /* Stop the pipe */
1183         ret = skl_stop_pipe(skl, sink_mconfig->pipe);
1184         if (ret)
1185                 return ret;
1186
1187         for (i = 0; i < sink_mconfig->module->max_input_pins; i++) {
1188                 if (sink_mconfig->m_in_pin[i].pin_state == SKL_PIN_BIND_DONE) {
1189                         src_mconfig = sink_mconfig->m_in_pin[i].tgt_mcfg;
1190                         if (!src_mconfig)
1191                                 continue;
1192
1193                         ret = skl_unbind_modules(skl,
1194                                                 src_mconfig, sink_mconfig);
1195                 }
1196         }
1197
1198         return ret;
1199 }
1200
1201 /*
1202  * in the Post-PMD event of mixer we need to do following:
1203  *   - Unbind the modules within the pipeline
1204  *   - Delete the pipeline (modules are not required to be explicitly
1205  *     deleted, pipeline delete is enough here
1206  */
1207 static int skl_tplg_mixer_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
1208                                                         struct skl_dev *skl)
1209 {
1210         struct skl_module_cfg *mconfig = w->priv;
1211         struct skl_pipe_module *w_module;
1212         struct skl_module_cfg *src_module = NULL, *dst_module;
1213         struct skl_pipe *s_pipe = mconfig->pipe;
1214         struct skl_module_deferred_bind *modules, *tmp;
1215
1216         if (s_pipe->state == SKL_PIPE_INVALID)
1217                 return -EINVAL;
1218
1219         list_for_each_entry(w_module, &s_pipe->w_list, node) {
1220                 if (list_empty(&skl->bind_list))
1221                         break;
1222
1223                 src_module = w_module->w->priv;
1224
1225                 list_for_each_entry_safe(modules, tmp, &skl->bind_list, node) {
1226                         /*
1227                          * When the destination module is deleted, Unbind the
1228                          * modules from deferred bind list.
1229                          */
1230                         if (modules->dst == src_module) {
1231                                 skl_unbind_modules(skl, modules->src,
1232                                                 modules->dst);
1233                         }
1234
1235                         /*
1236                          * When the source module is deleted, remove this entry
1237                          * from the deferred bind list.
1238                          */
1239                         if (modules->src == src_module) {
1240                                 list_del(&modules->node);
1241                                 modules->src = NULL;
1242                                 modules->dst = NULL;
1243                                 kfree(modules);
1244                         }
1245                 }
1246         }
1247
1248         list_for_each_entry(w_module, &s_pipe->w_list, node) {
1249                 dst_module = w_module->w->priv;
1250
1251                 if (src_module == NULL) {
1252                         src_module = dst_module;
1253                         continue;
1254                 }
1255
1256                 skl_unbind_modules(skl, src_module, dst_module);
1257                 src_module = dst_module;
1258         }
1259
1260         skl_delete_pipe(skl, mconfig->pipe);
1261
1262         list_for_each_entry(w_module, &s_pipe->w_list, node) {
1263                 src_module = w_module->w->priv;
1264                 src_module->m_state = SKL_MODULE_UNINIT;
1265         }
1266
1267         return skl_tplg_unload_pipe_modules(skl, s_pipe);
1268 }
1269
1270 /*
1271  * in the Post-PMD event of PGA we need to do following:
1272  *   - Stop the pipeline
1273  *   - In source pipe is connected, unbind with source pipelines
1274  */
1275 static int skl_tplg_pga_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
1276                                                         struct skl_dev *skl)
1277 {
1278         struct skl_module_cfg *src_mconfig, *sink_mconfig;
1279         int ret = 0, i;
1280
1281         src_mconfig = w->priv;
1282
1283         /* Stop the pipe since this is a mixin module */
1284         ret = skl_stop_pipe(skl, src_mconfig->pipe);
1285         if (ret)
1286                 return ret;
1287
1288         for (i = 0; i < src_mconfig->module->max_output_pins; i++) {
1289                 if (src_mconfig->m_out_pin[i].pin_state == SKL_PIN_BIND_DONE) {
1290                         sink_mconfig = src_mconfig->m_out_pin[i].tgt_mcfg;
1291                         if (!sink_mconfig)
1292                                 continue;
1293                         /*
1294                          * This is a connecter and if path is found that means
1295                          * unbind between source and sink has not happened yet
1296                          */
1297                         ret = skl_unbind_modules(skl, src_mconfig,
1298                                                         sink_mconfig);
1299                 }
1300         }
1301
1302         return ret;
1303 }
1304
1305 /*
1306  * In modelling, we assume there will be ONLY one mixer in a pipeline. If a
1307  * second one is required that is created as another pipe entity.
1308  * The mixer is responsible for pipe management and represent a pipeline
1309  * instance
1310  */
1311 static int skl_tplg_mixer_event(struct snd_soc_dapm_widget *w,
1312                                 struct snd_kcontrol *k, int event)
1313 {
1314         struct snd_soc_dapm_context *dapm = w->dapm;
1315         struct skl_dev *skl = get_skl_ctx(dapm->dev);
1316
1317         switch (event) {
1318         case SND_SOC_DAPM_PRE_PMU:
1319                 return skl_tplg_mixer_dapm_pre_pmu_event(w, skl);
1320
1321         case SND_SOC_DAPM_POST_PMU:
1322                 return skl_tplg_mixer_dapm_post_pmu_event(w, skl);
1323
1324         case SND_SOC_DAPM_PRE_PMD:
1325                 return skl_tplg_mixer_dapm_pre_pmd_event(w, skl);
1326
1327         case SND_SOC_DAPM_POST_PMD:
1328                 return skl_tplg_mixer_dapm_post_pmd_event(w, skl);
1329         }
1330
1331         return 0;
1332 }
1333
1334 /*
1335  * In modelling, we assumed rest of the modules in pipeline are PGA. But we
1336  * are interested in last PGA (leaf PGA) in a pipeline to disconnect with
1337  * the sink when it is running (two FE to one BE or one FE to two BE)
1338  * scenarios
1339  */
1340 static int skl_tplg_pga_event(struct snd_soc_dapm_widget *w,
1341                         struct snd_kcontrol *k, int event)
1342
1343 {
1344         struct snd_soc_dapm_context *dapm = w->dapm;
1345         struct skl_dev *skl = get_skl_ctx(dapm->dev);
1346
1347         switch (event) {
1348         case SND_SOC_DAPM_PRE_PMU:
1349                 return skl_tplg_pga_dapm_pre_pmu_event(w, skl);
1350
1351         case SND_SOC_DAPM_POST_PMD:
1352                 return skl_tplg_pga_dapm_post_pmd_event(w, skl);
1353         }
1354
1355         return 0;
1356 }
1357
1358 static int skl_tplg_multi_config_set_get(struct snd_kcontrol *kcontrol,
1359                                          struct snd_ctl_elem_value *ucontrol,
1360                                          bool is_set)
1361 {
1362         struct snd_soc_component *component =
1363                 snd_soc_kcontrol_component(kcontrol);
1364         struct hdac_bus *bus = snd_soc_component_get_drvdata(component);
1365         struct skl_dev *skl = bus_to_skl(bus);
1366         struct skl_pipeline *ppl;
1367         struct skl_pipe *pipe = NULL;
1368         struct soc_enum *ec = (struct soc_enum *)kcontrol->private_value;
1369         u32 *pipe_id;
1370
1371         if (!ec)
1372                 return -EINVAL;
1373
1374         if (is_set && ucontrol->value.enumerated.item[0] > ec->items)
1375                 return -EINVAL;
1376
1377         pipe_id = ec->dobj.private;
1378
1379         list_for_each_entry(ppl, &skl->ppl_list, node) {
1380                 if (ppl->pipe->ppl_id == *pipe_id) {
1381                         pipe = ppl->pipe;
1382                         break;
1383                 }
1384         }
1385         if (!pipe)
1386                 return -EIO;
1387
1388         if (is_set)
1389                 pipe->pipe_config_idx = ucontrol->value.enumerated.item[0];
1390         else
1391                 ucontrol->value.enumerated.item[0]  =  pipe->pipe_config_idx;
1392
1393         return 0;
1394 }
1395
1396 static int skl_tplg_multi_config_get(struct snd_kcontrol *kcontrol,
1397                                      struct snd_ctl_elem_value *ucontrol)
1398 {
1399         return skl_tplg_multi_config_set_get(kcontrol, ucontrol, false);
1400 }
1401
1402 static int skl_tplg_multi_config_set(struct snd_kcontrol *kcontrol,
1403                                      struct snd_ctl_elem_value *ucontrol)
1404 {
1405         return skl_tplg_multi_config_set_get(kcontrol, ucontrol, true);
1406 }
1407
1408 static int skl_tplg_multi_config_get_dmic(struct snd_kcontrol *kcontrol,
1409                                           struct snd_ctl_elem_value *ucontrol)
1410 {
1411         return skl_tplg_multi_config_set_get(kcontrol, ucontrol, false);
1412 }
1413
1414 static int skl_tplg_multi_config_set_dmic(struct snd_kcontrol *kcontrol,
1415                                           struct snd_ctl_elem_value *ucontrol)
1416 {
1417         return skl_tplg_multi_config_set_get(kcontrol, ucontrol, true);
1418 }
1419
1420 static int skl_tplg_tlv_control_get(struct snd_kcontrol *kcontrol,
1421                         unsigned int __user *data, unsigned int size)
1422 {
1423         struct soc_bytes_ext *sb =
1424                         (struct soc_bytes_ext *)kcontrol->private_value;
1425         struct skl_algo_data *bc = (struct skl_algo_data *)sb->dobj.private;
1426         struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1427         struct skl_module_cfg *mconfig = w->priv;
1428         struct skl_dev *skl = get_skl_ctx(w->dapm->dev);
1429
1430         if (w->power)
1431                 skl_get_module_params(skl, (u32 *)bc->params,
1432                                       bc->size, bc->param_id, mconfig);
1433
1434         /* decrement size for TLV header */
1435         size -= 2 * sizeof(u32);
1436
1437         /* check size as we don't want to send kernel data */
1438         if (size > bc->max)
1439                 size = bc->max;
1440
1441         if (bc->params) {
1442                 if (copy_to_user(data, &bc->param_id, sizeof(u32)))
1443                         return -EFAULT;
1444                 if (copy_to_user(data + 1, &size, sizeof(u32)))
1445                         return -EFAULT;
1446                 if (copy_to_user(data + 2, bc->params, size))
1447                         return -EFAULT;
1448         }
1449
1450         return 0;
1451 }
1452
1453 #define SKL_PARAM_VENDOR_ID 0xff
1454
1455 static int skl_tplg_tlv_control_set(struct snd_kcontrol *kcontrol,
1456                         const unsigned int __user *data, unsigned int size)
1457 {
1458         struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1459         struct skl_module_cfg *mconfig = w->priv;
1460         struct soc_bytes_ext *sb =
1461                         (struct soc_bytes_ext *)kcontrol->private_value;
1462         struct skl_algo_data *ac = (struct skl_algo_data *)sb->dobj.private;
1463         struct skl_dev *skl = get_skl_ctx(w->dapm->dev);
1464
1465         if (ac->params) {
1466                 /*
1467                  * Widget data is expected to be stripped of T and L
1468                  */
1469                 size -= 2 * sizeof(unsigned int);
1470                 data += 2;
1471
1472                 if (size > ac->max)
1473                         return -EINVAL;
1474                 ac->size = size;
1475
1476                 if (copy_from_user(ac->params, data, size))
1477                         return -EFAULT;
1478
1479                 if (w->power)
1480                         return skl_set_module_params(skl,
1481                                                 (u32 *)ac->params, ac->size,
1482                                                 ac->param_id, mconfig);
1483         }
1484
1485         return 0;
1486 }
1487
1488 static int skl_tplg_mic_control_get(struct snd_kcontrol *kcontrol,
1489                 struct snd_ctl_elem_value *ucontrol)
1490 {
1491         struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1492         struct skl_module_cfg *mconfig = w->priv;
1493         struct soc_enum *ec = (struct soc_enum *)kcontrol->private_value;
1494         u32 ch_type = *((u32 *)ec->dobj.private);
1495
1496         if (mconfig->dmic_ch_type == ch_type)
1497                 ucontrol->value.enumerated.item[0] =
1498                                         mconfig->dmic_ch_combo_index;
1499         else
1500                 ucontrol->value.enumerated.item[0] = 0;
1501
1502         return 0;
1503 }
1504
1505 static int skl_fill_mic_sel_params(struct skl_module_cfg *mconfig,
1506         struct skl_mic_sel_config *mic_cfg, struct device *dev)
1507 {
1508         struct skl_specific_cfg *sp_cfg = &mconfig->formats_config;
1509
1510         sp_cfg->caps_size = sizeof(struct skl_mic_sel_config);
1511         sp_cfg->set_params = SKL_PARAM_SET;
1512         sp_cfg->param_id = 0x00;
1513         if (!sp_cfg->caps) {
1514                 sp_cfg->caps = devm_kzalloc(dev, sp_cfg->caps_size, GFP_KERNEL);
1515                 if (!sp_cfg->caps)
1516                         return -ENOMEM;
1517         }
1518
1519         mic_cfg->mic_switch = SKL_MIC_SEL_SWITCH;
1520         mic_cfg->flags = 0;
1521         memcpy(sp_cfg->caps, mic_cfg, sp_cfg->caps_size);
1522
1523         return 0;
1524 }
1525
1526 static int skl_tplg_mic_control_set(struct snd_kcontrol *kcontrol,
1527                         struct snd_ctl_elem_value *ucontrol)
1528 {
1529         struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1530         struct skl_module_cfg *mconfig = w->priv;
1531         struct skl_mic_sel_config mic_cfg = {0};
1532         struct soc_enum *ec = (struct soc_enum *)kcontrol->private_value;
1533         u32 ch_type = *((u32 *)ec->dobj.private);
1534         const int *list;
1535         u8 in_ch, out_ch, index;
1536
1537         mconfig->dmic_ch_type = ch_type;
1538         mconfig->dmic_ch_combo_index = ucontrol->value.enumerated.item[0];
1539
1540         /* enum control index 0 is INVALID, so no channels to be set */
1541         if (mconfig->dmic_ch_combo_index == 0)
1542                 return 0;
1543
1544         /* No valid channel selection map for index 0, so offset by 1 */
1545         index = mconfig->dmic_ch_combo_index - 1;
1546
1547         switch (ch_type) {
1548         case SKL_CH_MONO:
1549                 if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_mono_list))
1550                         return -EINVAL;
1551
1552                 list = &mic_mono_list[index];
1553                 break;
1554
1555         case SKL_CH_STEREO:
1556                 if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_stereo_list))
1557                         return -EINVAL;
1558
1559                 list = mic_stereo_list[index];
1560                 break;
1561
1562         case SKL_CH_TRIO:
1563                 if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_trio_list))
1564                         return -EINVAL;
1565
1566                 list = mic_trio_list[index];
1567                 break;
1568
1569         case SKL_CH_QUATRO:
1570                 if (mconfig->dmic_ch_combo_index > ARRAY_SIZE(mic_quatro_list))
1571                         return -EINVAL;
1572
1573                 list = mic_quatro_list[index];
1574                 break;
1575
1576         default:
1577                 dev_err(w->dapm->dev,
1578                                 "Invalid channel %d for mic_select module\n",
1579                                 ch_type);
1580                 return -EINVAL;
1581
1582         }
1583
1584         /* channel type enum map to number of chanels for that type */
1585         for (out_ch = 0; out_ch < ch_type; out_ch++) {
1586                 in_ch = list[out_ch];
1587                 mic_cfg.blob[out_ch][in_ch] = SKL_DEFAULT_MIC_SEL_GAIN;
1588         }
1589
1590         return skl_fill_mic_sel_params(mconfig, &mic_cfg, w->dapm->dev);
1591 }
1592
1593 /*
1594  * Fill the dma id for host and link. In case of passthrough
1595  * pipeline, this will both host and link in the same
1596  * pipeline, so need to copy the link and host based on dev_type
1597  */
1598 static void skl_tplg_fill_dma_id(struct skl_module_cfg *mcfg,
1599                                 struct skl_pipe_params *params)
1600 {
1601         struct skl_pipe *pipe = mcfg->pipe;
1602
1603         if (pipe->passthru) {
1604                 switch (mcfg->dev_type) {
1605                 case SKL_DEVICE_HDALINK:
1606                         pipe->p_params->link_dma_id = params->link_dma_id;
1607                         pipe->p_params->link_index = params->link_index;
1608                         pipe->p_params->link_bps = params->link_bps;
1609                         break;
1610
1611                 case SKL_DEVICE_HDAHOST:
1612                         pipe->p_params->host_dma_id = params->host_dma_id;
1613                         pipe->p_params->host_bps = params->host_bps;
1614                         break;
1615
1616                 default:
1617                         break;
1618                 }
1619                 pipe->p_params->s_fmt = params->s_fmt;
1620                 pipe->p_params->ch = params->ch;
1621                 pipe->p_params->s_freq = params->s_freq;
1622                 pipe->p_params->stream = params->stream;
1623                 pipe->p_params->format = params->format;
1624
1625         } else {
1626                 memcpy(pipe->p_params, params, sizeof(*params));
1627         }
1628 }
1629
1630 /*
1631  * The FE params are passed by hw_params of the DAI.
1632  * On hw_params, the params are stored in Gateway module of the FE and we
1633  * need to calculate the format in DSP module configuration, that
1634  * conversion is done here
1635  */
1636 int skl_tplg_update_pipe_params(struct device *dev,
1637                         struct skl_module_cfg *mconfig,
1638                         struct skl_pipe_params *params)
1639 {
1640         struct skl_module_res *res = &mconfig->module->resources[0];
1641         struct skl_dev *skl = get_skl_ctx(dev);
1642         struct skl_module_fmt *format = NULL;
1643         u8 cfg_idx = mconfig->pipe->cur_config_idx;
1644
1645         skl_tplg_fill_dma_id(mconfig, params);
1646         mconfig->fmt_idx = mconfig->mod_cfg[cfg_idx].fmt_idx;
1647         mconfig->res_idx = mconfig->mod_cfg[cfg_idx].res_idx;
1648
1649         if (skl->nr_modules)
1650                 return 0;
1651
1652         if (params->stream == SNDRV_PCM_STREAM_PLAYBACK)
1653                 format = &mconfig->module->formats[0].inputs[0].fmt;
1654         else
1655                 format = &mconfig->module->formats[0].outputs[0].fmt;
1656
1657         /* set the hw_params */
1658         format->s_freq = params->s_freq;
1659         format->channels = params->ch;
1660         format->valid_bit_depth = skl_get_bit_depth(params->s_fmt);
1661
1662         /*
1663          * 16 bit is 16 bit container whereas 24 bit is in 32 bit
1664          * container so update bit depth accordingly
1665          */
1666         switch (format->valid_bit_depth) {
1667         case SKL_DEPTH_16BIT:
1668                 format->bit_depth = format->valid_bit_depth;
1669                 break;
1670
1671         case SKL_DEPTH_24BIT:
1672         case SKL_DEPTH_32BIT:
1673                 format->bit_depth = SKL_DEPTH_32BIT;
1674                 break;
1675
1676         default:
1677                 dev_err(dev, "Invalid bit depth %x for pipe\n",
1678                                 format->valid_bit_depth);
1679                 return -EINVAL;
1680         }
1681
1682         if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1683                 res->ibs = (format->s_freq / 1000) *
1684                                 (format->channels) *
1685                                 (format->bit_depth >> 3);
1686         } else {
1687                 res->obs = (format->s_freq / 1000) *
1688                                 (format->channels) *
1689                                 (format->bit_depth >> 3);
1690         }
1691
1692         return 0;
1693 }
1694
1695 /*
1696  * Query the module config for the FE DAI
1697  * This is used to find the hw_params set for that DAI and apply to FE
1698  * pipeline
1699  */
1700 struct skl_module_cfg *
1701 skl_tplg_fe_get_cpr_module(struct snd_soc_dai *dai, int stream)
1702 {
1703         struct snd_soc_dapm_widget *w;
1704         struct snd_soc_dapm_path *p = NULL;
1705
1706         if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1707                 w = dai->playback_widget;
1708                 snd_soc_dapm_widget_for_each_sink_path(w, p) {
1709                         if (p->connect && p->sink->power &&
1710                                 !is_skl_dsp_widget_type(p->sink, dai->dev))
1711                                 continue;
1712
1713                         if (p->sink->priv) {
1714                                 dev_dbg(dai->dev, "set params for %s\n",
1715                                                 p->sink->name);
1716                                 return p->sink->priv;
1717                         }
1718                 }
1719         } else {
1720                 w = dai->capture_widget;
1721                 snd_soc_dapm_widget_for_each_source_path(w, p) {
1722                         if (p->connect && p->source->power &&
1723                                 !is_skl_dsp_widget_type(p->source, dai->dev))
1724                                 continue;
1725
1726                         if (p->source->priv) {
1727                                 dev_dbg(dai->dev, "set params for %s\n",
1728                                                 p->source->name);
1729                                 return p->source->priv;
1730                         }
1731                 }
1732         }
1733
1734         return NULL;
1735 }
1736
1737 static struct skl_module_cfg *skl_get_mconfig_pb_cpr(
1738                 struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
1739 {
1740         struct snd_soc_dapm_path *p;
1741         struct skl_module_cfg *mconfig = NULL;
1742
1743         snd_soc_dapm_widget_for_each_source_path(w, p) {
1744                 if (w->endpoints[SND_SOC_DAPM_DIR_OUT] > 0) {
1745                         if (p->connect &&
1746                                     (p->sink->id == snd_soc_dapm_aif_out) &&
1747                                     p->source->priv) {
1748                                 mconfig = p->source->priv;
1749                                 return mconfig;
1750                         }
1751                         mconfig = skl_get_mconfig_pb_cpr(dai, p->source);
1752                         if (mconfig)
1753                                 return mconfig;
1754                 }
1755         }
1756         return mconfig;
1757 }
1758
1759 static struct skl_module_cfg *skl_get_mconfig_cap_cpr(
1760                 struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
1761 {
1762         struct snd_soc_dapm_path *p;
1763         struct skl_module_cfg *mconfig = NULL;
1764
1765         snd_soc_dapm_widget_for_each_sink_path(w, p) {
1766                 if (w->endpoints[SND_SOC_DAPM_DIR_IN] > 0) {
1767                         if (p->connect &&
1768                                     (p->source->id == snd_soc_dapm_aif_in) &&
1769                                     p->sink->priv) {
1770                                 mconfig = p->sink->priv;
1771                                 return mconfig;
1772                         }
1773                         mconfig = skl_get_mconfig_cap_cpr(dai, p->sink);
1774                         if (mconfig)
1775                                 return mconfig;
1776                 }
1777         }
1778         return mconfig;
1779 }
1780
1781 struct skl_module_cfg *
1782 skl_tplg_be_get_cpr_module(struct snd_soc_dai *dai, int stream)
1783 {
1784         struct snd_soc_dapm_widget *w;
1785         struct skl_module_cfg *mconfig;
1786
1787         if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1788                 w = dai->playback_widget;
1789                 mconfig = skl_get_mconfig_pb_cpr(dai, w);
1790         } else {
1791                 w = dai->capture_widget;
1792                 mconfig = skl_get_mconfig_cap_cpr(dai, w);
1793         }
1794         return mconfig;
1795 }
1796
1797 static u8 skl_tplg_be_link_type(int dev_type)
1798 {
1799         int ret;
1800
1801         switch (dev_type) {
1802         case SKL_DEVICE_BT:
1803                 ret = NHLT_LINK_SSP;
1804                 break;
1805
1806         case SKL_DEVICE_DMIC:
1807                 ret = NHLT_LINK_DMIC;
1808                 break;
1809
1810         case SKL_DEVICE_I2S:
1811                 ret = NHLT_LINK_SSP;
1812                 break;
1813
1814         case SKL_DEVICE_HDALINK:
1815                 ret = NHLT_LINK_HDA;
1816                 break;
1817
1818         default:
1819                 ret = NHLT_LINK_INVALID;
1820                 break;
1821         }
1822
1823         return ret;
1824 }
1825
1826 /*
1827  * Fill the BE gateway parameters
1828  * The BE gateway expects a blob of parameters which are kept in the ACPI
1829  * NHLT blob, so query the blob for interface type (i2s/pdm) and instance.
1830  * The port can have multiple settings so pick based on the PCM
1831  * parameters
1832  */
1833 static int skl_tplg_be_fill_pipe_params(struct snd_soc_dai *dai,
1834                                 struct skl_module_cfg *mconfig,
1835                                 struct skl_pipe_params *params)
1836 {
1837         struct nhlt_specific_cfg *cfg;
1838         struct skl_dev *skl = get_skl_ctx(dai->dev);
1839         int link_type = skl_tplg_be_link_type(mconfig->dev_type);
1840         u8 dev_type = skl_tplg_be_dev_type(mconfig->dev_type);
1841
1842         skl_tplg_fill_dma_id(mconfig, params);
1843
1844         if (link_type == NHLT_LINK_HDA)
1845                 return 0;
1846
1847         /* update the blob based on virtual bus_id*/
1848         cfg = skl_get_ep_blob(skl, mconfig->vbus_id, link_type,
1849                                         params->s_fmt, params->ch,
1850                                         params->s_freq, params->stream,
1851                                         dev_type);
1852         if (cfg) {
1853                 mconfig->formats_config.caps_size = cfg->size;
1854                 mconfig->formats_config.caps = (u32 *) &cfg->caps;
1855         } else {
1856                 dev_err(dai->dev, "Blob NULL for id %x type %d dirn %d\n",
1857                                         mconfig->vbus_id, link_type,
1858                                         params->stream);
1859                 dev_err(dai->dev, "PCM: ch %d, freq %d, fmt %d\n",
1860                                  params->ch, params->s_freq, params->s_fmt);
1861                 return -EINVAL;
1862         }
1863
1864         return 0;
1865 }
1866
1867 static int skl_tplg_be_set_src_pipe_params(struct snd_soc_dai *dai,
1868                                 struct snd_soc_dapm_widget *w,
1869                                 struct skl_pipe_params *params)
1870 {
1871         struct snd_soc_dapm_path *p;
1872         int ret = -EIO;
1873
1874         snd_soc_dapm_widget_for_each_source_path(w, p) {
1875                 if (p->connect && is_skl_dsp_widget_type(p->source, dai->dev) &&
1876                                                 p->source->priv) {
1877
1878                         ret = skl_tplg_be_fill_pipe_params(dai,
1879                                                 p->source->priv, params);
1880                         if (ret < 0)
1881                                 return ret;
1882                 } else {
1883                         ret = skl_tplg_be_set_src_pipe_params(dai,
1884                                                 p->source, params);
1885                         if (ret < 0)
1886                                 return ret;
1887                 }
1888         }
1889
1890         return ret;
1891 }
1892
1893 static int skl_tplg_be_set_sink_pipe_params(struct snd_soc_dai *dai,
1894         struct snd_soc_dapm_widget *w, struct skl_pipe_params *params)
1895 {
1896         struct snd_soc_dapm_path *p;
1897         int ret = -EIO;
1898
1899         snd_soc_dapm_widget_for_each_sink_path(w, p) {
1900                 if (p->connect && is_skl_dsp_widget_type(p->sink, dai->dev) &&
1901                                                 p->sink->priv) {
1902
1903                         ret = skl_tplg_be_fill_pipe_params(dai,
1904                                                 p->sink->priv, params);
1905                         if (ret < 0)
1906                                 return ret;
1907                 } else {
1908                         ret = skl_tplg_be_set_sink_pipe_params(
1909                                                 dai, p->sink, params);
1910                         if (ret < 0)
1911                                 return ret;
1912                 }
1913         }
1914
1915         return ret;
1916 }
1917
1918 /*
1919  * BE hw_params can be a source parameters (capture) or sink parameters
1920  * (playback). Based on sink and source we need to either find the source
1921  * list or the sink list and set the pipeline parameters
1922  */
1923 int skl_tplg_be_update_params(struct snd_soc_dai *dai,
1924                                 struct skl_pipe_params *params)
1925 {
1926         struct snd_soc_dapm_widget *w;
1927
1928         if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1929                 w = dai->playback_widget;
1930
1931                 return skl_tplg_be_set_src_pipe_params(dai, w, params);
1932
1933         } else {
1934                 w = dai->capture_widget;
1935
1936                 return skl_tplg_be_set_sink_pipe_params(dai, w, params);
1937         }
1938
1939         return 0;
1940 }
1941
1942 static const struct snd_soc_tplg_widget_events skl_tplg_widget_ops[] = {
1943         {SKL_MIXER_EVENT, skl_tplg_mixer_event},
1944         {SKL_VMIXER_EVENT, skl_tplg_mixer_event},
1945         {SKL_PGA_EVENT, skl_tplg_pga_event},
1946 };
1947
1948 static const struct snd_soc_tplg_bytes_ext_ops skl_tlv_ops[] = {
1949         {SKL_CONTROL_TYPE_BYTE_TLV, skl_tplg_tlv_control_get,
1950                                         skl_tplg_tlv_control_set},
1951 };
1952
1953 static const struct snd_soc_tplg_kcontrol_ops skl_tplg_kcontrol_ops[] = {
1954         {
1955                 .id = SKL_CONTROL_TYPE_MIC_SELECT,
1956                 .get = skl_tplg_mic_control_get,
1957                 .put = skl_tplg_mic_control_set,
1958         },
1959         {
1960                 .id = SKL_CONTROL_TYPE_MULTI_IO_SELECT,
1961                 .get = skl_tplg_multi_config_get,
1962                 .put = skl_tplg_multi_config_set,
1963         },
1964         {
1965                 .id = SKL_CONTROL_TYPE_MULTI_IO_SELECT_DMIC,
1966                 .get = skl_tplg_multi_config_get_dmic,
1967                 .put = skl_tplg_multi_config_set_dmic,
1968         }
1969 };
1970
1971 static int skl_tplg_fill_pipe_cfg(struct device *dev,
1972                         struct skl_pipe *pipe, u32 tkn,
1973                         u32 tkn_val, int conf_idx, int dir)
1974 {
1975         struct skl_pipe_fmt *fmt;
1976         struct skl_path_config *config;
1977
1978         switch (dir) {
1979         case SKL_DIR_IN:
1980                 fmt = &pipe->configs[conf_idx].in_fmt;
1981                 break;
1982
1983         case SKL_DIR_OUT:
1984                 fmt = &pipe->configs[conf_idx].out_fmt;
1985                 break;
1986
1987         default:
1988                 dev_err(dev, "Invalid direction: %d\n", dir);
1989                 return -EINVAL;
1990         }
1991
1992         config = &pipe->configs[conf_idx];
1993
1994         switch (tkn) {
1995         case SKL_TKN_U32_CFG_FREQ:
1996                 fmt->freq = tkn_val;
1997                 break;
1998
1999         case SKL_TKN_U8_CFG_CHAN:
2000                 fmt->channels = tkn_val;
2001                 break;
2002
2003         case SKL_TKN_U8_CFG_BPS:
2004                 fmt->bps = tkn_val;
2005                 break;
2006
2007         case SKL_TKN_U32_PATH_MEM_PGS:
2008                 config->mem_pages = tkn_val;
2009                 break;
2010
2011         default:
2012                 dev_err(dev, "Invalid token config: %d\n", tkn);
2013                 return -EINVAL;
2014         }
2015
2016         return 0;
2017 }
2018
2019 static int skl_tplg_fill_pipe_tkn(struct device *dev,
2020                         struct skl_pipe *pipe, u32 tkn,
2021                         u32 tkn_val)
2022 {
2023
2024         switch (tkn) {
2025         case SKL_TKN_U32_PIPE_CONN_TYPE:
2026                 pipe->conn_type = tkn_val;
2027                 break;
2028
2029         case SKL_TKN_U32_PIPE_PRIORITY:
2030                 pipe->pipe_priority = tkn_val;
2031                 break;
2032
2033         case SKL_TKN_U32_PIPE_MEM_PGS:
2034                 pipe->memory_pages = tkn_val;
2035                 break;
2036
2037         case SKL_TKN_U32_PMODE:
2038                 pipe->lp_mode = tkn_val;
2039                 break;
2040
2041         case SKL_TKN_U32_PIPE_DIRECTION:
2042                 pipe->direction = tkn_val;
2043                 break;
2044
2045         case SKL_TKN_U32_NUM_CONFIGS:
2046                 pipe->nr_cfgs = tkn_val;
2047                 break;
2048
2049         default:
2050                 dev_err(dev, "Token not handled %d\n", tkn);
2051                 return -EINVAL;
2052         }
2053
2054         return 0;
2055 }
2056
2057 /*
2058  * Add pipeline by parsing the relevant tokens
2059  * Return an existing pipe if the pipe already exists.
2060  */
2061 static int skl_tplg_add_pipe(struct device *dev,
2062                 struct skl_module_cfg *mconfig, struct skl_dev *skl,
2063                 struct snd_soc_tplg_vendor_value_elem *tkn_elem)
2064 {
2065         struct skl_pipeline *ppl;
2066         struct skl_pipe *pipe;
2067         struct skl_pipe_params *params;
2068
2069         list_for_each_entry(ppl, &skl->ppl_list, node) {
2070                 if (ppl->pipe->ppl_id == tkn_elem->value) {
2071                         mconfig->pipe = ppl->pipe;
2072                         return -EEXIST;
2073                 }
2074         }
2075
2076         ppl = devm_kzalloc(dev, sizeof(*ppl), GFP_KERNEL);
2077         if (!ppl)
2078                 return -ENOMEM;
2079
2080         pipe = devm_kzalloc(dev, sizeof(*pipe), GFP_KERNEL);
2081         if (!pipe)
2082                 return -ENOMEM;
2083
2084         params = devm_kzalloc(dev, sizeof(*params), GFP_KERNEL);
2085         if (!params)
2086                 return -ENOMEM;
2087
2088         pipe->p_params = params;
2089         pipe->ppl_id = tkn_elem->value;
2090         INIT_LIST_HEAD(&pipe->w_list);
2091
2092         ppl->pipe = pipe;
2093         list_add(&ppl->node, &skl->ppl_list);
2094
2095         mconfig->pipe = pipe;
2096         mconfig->pipe->state = SKL_PIPE_INVALID;
2097
2098         return 0;
2099 }
2100
2101 static int skl_tplg_get_uuid(struct device *dev, guid_t *guid,
2102               struct snd_soc_tplg_vendor_uuid_elem *uuid_tkn)
2103 {
2104         if (uuid_tkn->token == SKL_TKN_UUID) {
2105                 import_guid(guid, uuid_tkn->uuid);
2106                 return 0;
2107         }
2108
2109         dev_err(dev, "Not an UUID token %d\n", uuid_tkn->token);
2110
2111         return -EINVAL;
2112 }
2113
2114 static int skl_tplg_fill_pin(struct device *dev,
2115                         struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2116                         struct skl_module_pin *m_pin,
2117                         int pin_index)
2118 {
2119         int ret;
2120
2121         switch (tkn_elem->token) {
2122         case SKL_TKN_U32_PIN_MOD_ID:
2123                 m_pin[pin_index].id.module_id = tkn_elem->value;
2124                 break;
2125
2126         case SKL_TKN_U32_PIN_INST_ID:
2127                 m_pin[pin_index].id.instance_id = tkn_elem->value;
2128                 break;
2129
2130         case SKL_TKN_UUID:
2131                 ret = skl_tplg_get_uuid(dev, &m_pin[pin_index].id.mod_uuid,
2132                         (struct snd_soc_tplg_vendor_uuid_elem *)tkn_elem);
2133                 if (ret < 0)
2134                         return ret;
2135
2136                 break;
2137
2138         default:
2139                 dev_err(dev, "%d Not a pin token\n", tkn_elem->token);
2140                 return -EINVAL;
2141         }
2142
2143         return 0;
2144 }
2145
2146 /*
2147  * Parse for pin config specific tokens to fill up the
2148  * module private data
2149  */
2150 static int skl_tplg_fill_pins_info(struct device *dev,
2151                 struct skl_module_cfg *mconfig,
2152                 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2153                 int dir, int pin_count)
2154 {
2155         int ret;
2156         struct skl_module_pin *m_pin;
2157
2158         switch (dir) {
2159         case SKL_DIR_IN:
2160                 m_pin = mconfig->m_in_pin;
2161                 break;
2162
2163         case SKL_DIR_OUT:
2164                 m_pin = mconfig->m_out_pin;
2165                 break;
2166
2167         default:
2168                 dev_err(dev, "Invalid direction value\n");
2169                 return -EINVAL;
2170         }
2171
2172         ret = skl_tplg_fill_pin(dev, tkn_elem, m_pin, pin_count);
2173         if (ret < 0)
2174                 return ret;
2175
2176         m_pin[pin_count].in_use = false;
2177         m_pin[pin_count].pin_state = SKL_PIN_UNBIND;
2178
2179         return 0;
2180 }
2181
2182 /*
2183  * Fill up input/output module config format based
2184  * on the direction
2185  */
2186 static int skl_tplg_fill_fmt(struct device *dev,
2187                 struct skl_module_fmt *dst_fmt,
2188                 u32 tkn, u32 value)
2189 {
2190         switch (tkn) {
2191         case SKL_TKN_U32_FMT_CH:
2192                 dst_fmt->channels  = value;
2193                 break;
2194
2195         case SKL_TKN_U32_FMT_FREQ:
2196                 dst_fmt->s_freq = value;
2197                 break;
2198
2199         case SKL_TKN_U32_FMT_BIT_DEPTH:
2200                 dst_fmt->bit_depth = value;
2201                 break;
2202
2203         case SKL_TKN_U32_FMT_SAMPLE_SIZE:
2204                 dst_fmt->valid_bit_depth = value;
2205                 break;
2206
2207         case SKL_TKN_U32_FMT_CH_CONFIG:
2208                 dst_fmt->ch_cfg = value;
2209                 break;
2210
2211         case SKL_TKN_U32_FMT_INTERLEAVE:
2212                 dst_fmt->interleaving_style = value;
2213                 break;
2214
2215         case SKL_TKN_U32_FMT_SAMPLE_TYPE:
2216                 dst_fmt->sample_type = value;
2217                 break;
2218
2219         case SKL_TKN_U32_FMT_CH_MAP:
2220                 dst_fmt->ch_map = value;
2221                 break;
2222
2223         default:
2224                 dev_err(dev, "Invalid token %d\n", tkn);
2225                 return -EINVAL;
2226         }
2227
2228         return 0;
2229 }
2230
2231 static int skl_tplg_widget_fill_fmt(struct device *dev,
2232                 struct skl_module_iface *fmt,
2233                 u32 tkn, u32 val, u32 dir, int fmt_idx)
2234 {
2235         struct skl_module_fmt *dst_fmt;
2236
2237         if (!fmt)
2238                 return -EINVAL;
2239
2240         switch (dir) {
2241         case SKL_DIR_IN:
2242                 dst_fmt = &fmt->inputs[fmt_idx].fmt;
2243                 break;
2244
2245         case SKL_DIR_OUT:
2246                 dst_fmt = &fmt->outputs[fmt_idx].fmt;
2247                 break;
2248
2249         default:
2250                 dev_err(dev, "Invalid direction: %d\n", dir);
2251                 return -EINVAL;
2252         }
2253
2254         return skl_tplg_fill_fmt(dev, dst_fmt, tkn, val);
2255 }
2256
2257 static void skl_tplg_fill_pin_dynamic_val(
2258                 struct skl_module_pin *mpin, u32 pin_count, u32 value)
2259 {
2260         int i;
2261
2262         for (i = 0; i < pin_count; i++)
2263                 mpin[i].is_dynamic = value;
2264 }
2265
2266 /*
2267  * Resource table in the manifest has pin specific resources
2268  * like pin and pin buffer size
2269  */
2270 static int skl_tplg_manifest_pin_res_tkn(struct device *dev,
2271                 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2272                 struct skl_module_res *res, int pin_idx, int dir)
2273 {
2274         struct skl_module_pin_resources *m_pin;
2275
2276         switch (dir) {
2277         case SKL_DIR_IN:
2278                 m_pin = &res->input[pin_idx];
2279                 break;
2280
2281         case SKL_DIR_OUT:
2282                 m_pin = &res->output[pin_idx];
2283                 break;
2284
2285         default:
2286                 dev_err(dev, "Invalid pin direction: %d\n", dir);
2287                 return -EINVAL;
2288         }
2289
2290         switch (tkn_elem->token) {
2291         case SKL_TKN_MM_U32_RES_PIN_ID:
2292                 m_pin->pin_index = tkn_elem->value;
2293                 break;
2294
2295         case SKL_TKN_MM_U32_PIN_BUF:
2296                 m_pin->buf_size = tkn_elem->value;
2297                 break;
2298
2299         default:
2300                 dev_err(dev, "Invalid token: %d\n", tkn_elem->token);
2301                 return -EINVAL;
2302         }
2303
2304         return 0;
2305 }
2306
2307 /*
2308  * Fill module specific resources from the manifest's resource
2309  * table like CPS, DMA size, mem_pages.
2310  */
2311 static int skl_tplg_fill_res_tkn(struct device *dev,
2312                 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2313                 struct skl_module_res *res,
2314                 int pin_idx, int dir)
2315 {
2316         int ret, tkn_count = 0;
2317
2318         if (!res)
2319                 return -EINVAL;
2320
2321         switch (tkn_elem->token) {
2322         case SKL_TKN_MM_U32_DMA_SIZE:
2323                 res->dma_buffer_size = tkn_elem->value;
2324                 break;
2325
2326         case SKL_TKN_MM_U32_CPC:
2327                 res->cpc = tkn_elem->value;
2328                 break;
2329
2330         case SKL_TKN_U32_MEM_PAGES:
2331                 res->is_pages = tkn_elem->value;
2332                 break;
2333
2334         case SKL_TKN_U32_OBS:
2335                 res->obs = tkn_elem->value;
2336                 break;
2337
2338         case SKL_TKN_U32_IBS:
2339                 res->ibs = tkn_elem->value;
2340                 break;
2341
2342         case SKL_TKN_MM_U32_RES_PIN_ID:
2343         case SKL_TKN_MM_U32_PIN_BUF:
2344                 ret = skl_tplg_manifest_pin_res_tkn(dev, tkn_elem, res,
2345                                                     pin_idx, dir);
2346                 if (ret < 0)
2347                         return ret;
2348                 break;
2349
2350         case SKL_TKN_MM_U32_CPS:
2351         case SKL_TKN_U32_MAX_MCPS:
2352                 /* ignore unused tokens */
2353                 break;
2354
2355         default:
2356                 dev_err(dev, "Not a res type token: %d", tkn_elem->token);
2357                 return -EINVAL;
2358
2359         }
2360         tkn_count++;
2361
2362         return tkn_count;
2363 }
2364
2365 /*
2366  * Parse tokens to fill up the module private data
2367  */
2368 static int skl_tplg_get_token(struct device *dev,
2369                 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2370                 struct skl_dev *skl, struct skl_module_cfg *mconfig)
2371 {
2372         int tkn_count = 0;
2373         int ret;
2374         static int is_pipe_exists;
2375         static int pin_index, dir, conf_idx;
2376         struct skl_module_iface *iface = NULL;
2377         struct skl_module_res *res = NULL;
2378         int res_idx = mconfig->res_idx;
2379         int fmt_idx = mconfig->fmt_idx;
2380
2381         /*
2382          * If the manifest structure contains no modules, fill all
2383          * the module data to 0th index.
2384          * res_idx and fmt_idx are default set to 0.
2385          */
2386         if (skl->nr_modules == 0) {
2387                 res = &mconfig->module->resources[res_idx];
2388                 iface = &mconfig->module->formats[fmt_idx];
2389         }
2390
2391         if (tkn_elem->token > SKL_TKN_MAX)
2392                 return -EINVAL;
2393
2394         switch (tkn_elem->token) {
2395         case SKL_TKN_U8_IN_QUEUE_COUNT:
2396                 mconfig->module->max_input_pins = tkn_elem->value;
2397                 break;
2398
2399         case SKL_TKN_U8_OUT_QUEUE_COUNT:
2400                 mconfig->module->max_output_pins = tkn_elem->value;
2401                 break;
2402
2403         case SKL_TKN_U8_DYN_IN_PIN:
2404                 if (!mconfig->m_in_pin)
2405                         mconfig->m_in_pin =
2406                                 devm_kcalloc(dev, MAX_IN_QUEUE,
2407                                              sizeof(*mconfig->m_in_pin),
2408                                              GFP_KERNEL);
2409                 if (!mconfig->m_in_pin)
2410                         return -ENOMEM;
2411
2412                 skl_tplg_fill_pin_dynamic_val(mconfig->m_in_pin, MAX_IN_QUEUE,
2413                                               tkn_elem->value);
2414                 break;
2415
2416         case SKL_TKN_U8_DYN_OUT_PIN:
2417                 if (!mconfig->m_out_pin)
2418                         mconfig->m_out_pin =
2419                                 devm_kcalloc(dev, MAX_IN_QUEUE,
2420                                              sizeof(*mconfig->m_in_pin),
2421                                              GFP_KERNEL);
2422                 if (!mconfig->m_out_pin)
2423                         return -ENOMEM;
2424
2425                 skl_tplg_fill_pin_dynamic_val(mconfig->m_out_pin, MAX_OUT_QUEUE,
2426                                               tkn_elem->value);
2427                 break;
2428
2429         case SKL_TKN_U8_TIME_SLOT:
2430                 mconfig->time_slot = tkn_elem->value;
2431                 break;
2432
2433         case SKL_TKN_U8_CORE_ID:
2434                 mconfig->core_id = tkn_elem->value;
2435                 break;
2436
2437         case SKL_TKN_U8_MOD_TYPE:
2438                 mconfig->m_type = tkn_elem->value;
2439                 break;
2440
2441         case SKL_TKN_U8_DEV_TYPE:
2442                 mconfig->dev_type = tkn_elem->value;
2443                 break;
2444
2445         case SKL_TKN_U8_HW_CONN_TYPE:
2446                 mconfig->hw_conn_type = tkn_elem->value;
2447                 break;
2448
2449         case SKL_TKN_U16_MOD_INST_ID:
2450                 mconfig->id.instance_id =
2451                 tkn_elem->value;
2452                 break;
2453
2454         case SKL_TKN_U32_MEM_PAGES:
2455         case SKL_TKN_U32_MAX_MCPS:
2456         case SKL_TKN_U32_OBS:
2457         case SKL_TKN_U32_IBS:
2458                 ret = skl_tplg_fill_res_tkn(dev, tkn_elem, res, pin_index, dir);
2459                 if (ret < 0)
2460                         return ret;
2461
2462                 break;
2463
2464         case SKL_TKN_U32_VBUS_ID:
2465                 mconfig->vbus_id = tkn_elem->value;
2466                 break;
2467
2468         case SKL_TKN_U32_PARAMS_FIXUP:
2469                 mconfig->params_fixup = tkn_elem->value;
2470                 break;
2471
2472         case SKL_TKN_U32_CONVERTER:
2473                 mconfig->converter = tkn_elem->value;
2474                 break;
2475
2476         case SKL_TKN_U32_D0I3_CAPS:
2477                 mconfig->d0i3_caps = tkn_elem->value;
2478                 break;
2479
2480         case SKL_TKN_U32_PIPE_ID:
2481                 ret = skl_tplg_add_pipe(dev,
2482                                 mconfig, skl, tkn_elem);
2483
2484                 if (ret < 0) {
2485                         if (ret == -EEXIST) {
2486                                 is_pipe_exists = 1;
2487                                 break;
2488                         }
2489                         return is_pipe_exists;
2490                 }
2491
2492                 break;
2493
2494         case SKL_TKN_U32_PIPE_CONFIG_ID:
2495                 conf_idx = tkn_elem->value;
2496                 break;
2497
2498         case SKL_TKN_U32_PIPE_CONN_TYPE:
2499         case SKL_TKN_U32_PIPE_PRIORITY:
2500         case SKL_TKN_U32_PIPE_MEM_PGS:
2501         case SKL_TKN_U32_PMODE:
2502         case SKL_TKN_U32_PIPE_DIRECTION:
2503         case SKL_TKN_U32_NUM_CONFIGS:
2504                 if (is_pipe_exists) {
2505                         ret = skl_tplg_fill_pipe_tkn(dev, mconfig->pipe,
2506                                         tkn_elem->token, tkn_elem->value);
2507                         if (ret < 0)
2508                                 return ret;
2509                 }
2510
2511                 break;
2512
2513         case SKL_TKN_U32_PATH_MEM_PGS:
2514         case SKL_TKN_U32_CFG_FREQ:
2515         case SKL_TKN_U8_CFG_CHAN:
2516         case SKL_TKN_U8_CFG_BPS:
2517                 if (mconfig->pipe->nr_cfgs) {
2518                         ret = skl_tplg_fill_pipe_cfg(dev, mconfig->pipe,
2519                                         tkn_elem->token, tkn_elem->value,
2520                                         conf_idx, dir);
2521                         if (ret < 0)
2522                                 return ret;
2523                 }
2524                 break;
2525
2526         case SKL_TKN_CFG_MOD_RES_ID:
2527                 mconfig->mod_cfg[conf_idx].res_idx = tkn_elem->value;
2528                 break;
2529
2530         case SKL_TKN_CFG_MOD_FMT_ID:
2531                 mconfig->mod_cfg[conf_idx].fmt_idx = tkn_elem->value;
2532                 break;
2533
2534         /*
2535          * SKL_TKN_U32_DIR_PIN_COUNT token has the value for both
2536          * direction and the pin count. The first four bits represent
2537          * direction and next four the pin count.
2538          */
2539         case SKL_TKN_U32_DIR_PIN_COUNT:
2540                 dir = tkn_elem->value & SKL_IN_DIR_BIT_MASK;
2541                 pin_index = (tkn_elem->value &
2542                         SKL_PIN_COUNT_MASK) >> 4;
2543
2544                 break;
2545
2546         case SKL_TKN_U32_FMT_CH:
2547         case SKL_TKN_U32_FMT_FREQ:
2548         case SKL_TKN_U32_FMT_BIT_DEPTH:
2549         case SKL_TKN_U32_FMT_SAMPLE_SIZE:
2550         case SKL_TKN_U32_FMT_CH_CONFIG:
2551         case SKL_TKN_U32_FMT_INTERLEAVE:
2552         case SKL_TKN_U32_FMT_SAMPLE_TYPE:
2553         case SKL_TKN_U32_FMT_CH_MAP:
2554                 ret = skl_tplg_widget_fill_fmt(dev, iface, tkn_elem->token,
2555                                 tkn_elem->value, dir, pin_index);
2556
2557                 if (ret < 0)
2558                         return ret;
2559
2560                 break;
2561
2562         case SKL_TKN_U32_PIN_MOD_ID:
2563         case SKL_TKN_U32_PIN_INST_ID:
2564         case SKL_TKN_UUID:
2565                 ret = skl_tplg_fill_pins_info(dev,
2566                                 mconfig, tkn_elem, dir,
2567                                 pin_index);
2568                 if (ret < 0)
2569                         return ret;
2570
2571                 break;
2572
2573         case SKL_TKN_U32_CAPS_SIZE:
2574                 mconfig->formats_config.caps_size =
2575                         tkn_elem->value;
2576
2577                 break;
2578
2579         case SKL_TKN_U32_CAPS_SET_PARAMS:
2580                 mconfig->formats_config.set_params =
2581                                 tkn_elem->value;
2582                 break;
2583
2584         case SKL_TKN_U32_CAPS_PARAMS_ID:
2585                 mconfig->formats_config.param_id =
2586                                 tkn_elem->value;
2587                 break;
2588
2589         case SKL_TKN_U32_PROC_DOMAIN:
2590                 mconfig->domain =
2591                         tkn_elem->value;
2592
2593                 break;
2594
2595         case SKL_TKN_U32_DMA_BUF_SIZE:
2596                 mconfig->dma_buffer_size = tkn_elem->value;
2597                 break;
2598
2599         case SKL_TKN_U8_IN_PIN_TYPE:
2600         case SKL_TKN_U8_OUT_PIN_TYPE:
2601         case SKL_TKN_U8_CONN_TYPE:
2602                 break;
2603
2604         default:
2605                 dev_err(dev, "Token %d not handled\n",
2606                                 tkn_elem->token);
2607                 return -EINVAL;
2608         }
2609
2610         tkn_count++;
2611
2612         return tkn_count;
2613 }
2614
2615 /*
2616  * Parse the vendor array for specific tokens to construct
2617  * module private data
2618  */
2619 static int skl_tplg_get_tokens(struct device *dev,
2620                 char *pvt_data, struct skl_dev *skl,
2621                 struct skl_module_cfg *mconfig, int block_size)
2622 {
2623         struct snd_soc_tplg_vendor_array *array;
2624         struct snd_soc_tplg_vendor_value_elem *tkn_elem;
2625         int tkn_count = 0, ret;
2626         int off = 0, tuple_size = 0;
2627         bool is_module_guid = true;
2628
2629         if (block_size <= 0)
2630                 return -EINVAL;
2631
2632         while (tuple_size < block_size) {
2633                 array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off);
2634
2635                 off += array->size;
2636
2637                 switch (array->type) {
2638                 case SND_SOC_TPLG_TUPLE_TYPE_STRING:
2639                         dev_warn(dev, "no string tokens expected for skl tplg\n");
2640                         continue;
2641
2642                 case SND_SOC_TPLG_TUPLE_TYPE_UUID:
2643                         if (is_module_guid) {
2644                                 ret = skl_tplg_get_uuid(dev, (guid_t *)mconfig->guid,
2645                                                         array->uuid);
2646                                 is_module_guid = false;
2647                         } else {
2648                                 ret = skl_tplg_get_token(dev, array->value, skl,
2649                                                          mconfig);
2650                         }
2651
2652                         if (ret < 0)
2653                                 return ret;
2654
2655                         tuple_size += sizeof(*array->uuid);
2656
2657                         continue;
2658
2659                 default:
2660                         tkn_elem = array->value;
2661                         tkn_count = 0;
2662                         break;
2663                 }
2664
2665                 while (tkn_count <= (array->num_elems - 1)) {
2666                         ret = skl_tplg_get_token(dev, tkn_elem,
2667                                         skl, mconfig);
2668
2669                         if (ret < 0)
2670                                 return ret;
2671
2672                         tkn_count = tkn_count + ret;
2673                         tkn_elem++;
2674                 }
2675
2676                 tuple_size += tkn_count * sizeof(*tkn_elem);
2677         }
2678
2679         return off;
2680 }
2681
2682 /*
2683  * Every data block is preceded by a descriptor to read the number
2684  * of data blocks, they type of the block and it's size
2685  */
2686 static int skl_tplg_get_desc_blocks(struct device *dev,
2687                 struct snd_soc_tplg_vendor_array *array)
2688 {
2689         struct snd_soc_tplg_vendor_value_elem *tkn_elem;
2690
2691         tkn_elem = array->value;
2692
2693         switch (tkn_elem->token) {
2694         case SKL_TKN_U8_NUM_BLOCKS:
2695         case SKL_TKN_U8_BLOCK_TYPE:
2696         case SKL_TKN_U16_BLOCK_SIZE:
2697                 return tkn_elem->value;
2698
2699         default:
2700                 dev_err(dev, "Invalid descriptor token %d\n", tkn_elem->token);
2701                 break;
2702         }
2703
2704         return -EINVAL;
2705 }
2706
2707 /* Functions to parse private data from configuration file format v4 */
2708
2709 /*
2710  * Add pipeline from topology binary into driver pipeline list
2711  *
2712  * If already added we return that instance
2713  * Otherwise we create a new instance and add into driver list
2714  */
2715 static int skl_tplg_add_pipe_v4(struct device *dev,
2716                         struct skl_module_cfg *mconfig, struct skl_dev *skl,
2717                         struct skl_dfw_v4_pipe *dfw_pipe)
2718 {
2719         struct skl_pipeline *ppl;
2720         struct skl_pipe *pipe;
2721         struct skl_pipe_params *params;
2722
2723         list_for_each_entry(ppl, &skl->ppl_list, node) {
2724                 if (ppl->pipe->ppl_id == dfw_pipe->pipe_id) {
2725                         mconfig->pipe = ppl->pipe;
2726                         return 0;
2727                 }
2728         }
2729
2730         ppl = devm_kzalloc(dev, sizeof(*ppl), GFP_KERNEL);
2731         if (!ppl)
2732                 return -ENOMEM;
2733
2734         pipe = devm_kzalloc(dev, sizeof(*pipe), GFP_KERNEL);
2735         if (!pipe)
2736                 return -ENOMEM;
2737
2738         params = devm_kzalloc(dev, sizeof(*params), GFP_KERNEL);
2739         if (!params)
2740                 return -ENOMEM;
2741
2742         pipe->ppl_id = dfw_pipe->pipe_id;
2743         pipe->memory_pages = dfw_pipe->memory_pages;
2744         pipe->pipe_priority = dfw_pipe->pipe_priority;
2745         pipe->conn_type = dfw_pipe->conn_type;
2746         pipe->state = SKL_PIPE_INVALID;
2747         pipe->p_params = params;
2748         INIT_LIST_HEAD(&pipe->w_list);
2749
2750         ppl->pipe = pipe;
2751         list_add(&ppl->node, &skl->ppl_list);
2752
2753         mconfig->pipe = pipe;
2754
2755         return 0;
2756 }
2757
2758 static void skl_fill_module_pin_info_v4(struct skl_dfw_v4_module_pin *dfw_pin,
2759                                         struct skl_module_pin *m_pin,
2760                                         bool is_dynamic, int max_pin)
2761 {
2762         int i;
2763
2764         for (i = 0; i < max_pin; i++) {
2765                 m_pin[i].id.module_id = dfw_pin[i].module_id;
2766                 m_pin[i].id.instance_id = dfw_pin[i].instance_id;
2767                 m_pin[i].in_use = false;
2768                 m_pin[i].is_dynamic = is_dynamic;
2769                 m_pin[i].pin_state = SKL_PIN_UNBIND;
2770         }
2771 }
2772
2773 static void skl_tplg_fill_fmt_v4(struct skl_module_pin_fmt *dst_fmt,
2774                                  struct skl_dfw_v4_module_fmt *src_fmt,
2775                                  int pins)
2776 {
2777         int i;
2778
2779         for (i = 0; i < pins; i++) {
2780                 dst_fmt[i].fmt.channels  = src_fmt[i].channels;
2781                 dst_fmt[i].fmt.s_freq = src_fmt[i].freq;
2782                 dst_fmt[i].fmt.bit_depth = src_fmt[i].bit_depth;
2783                 dst_fmt[i].fmt.valid_bit_depth = src_fmt[i].valid_bit_depth;
2784                 dst_fmt[i].fmt.ch_cfg = src_fmt[i].ch_cfg;
2785                 dst_fmt[i].fmt.ch_map = src_fmt[i].ch_map;
2786                 dst_fmt[i].fmt.interleaving_style =
2787                                                 src_fmt[i].interleaving_style;
2788                 dst_fmt[i].fmt.sample_type = src_fmt[i].sample_type;
2789         }
2790 }
2791
2792 static int skl_tplg_get_pvt_data_v4(struct snd_soc_tplg_dapm_widget *tplg_w,
2793                                     struct skl_dev *skl, struct device *dev,
2794                                     struct skl_module_cfg *mconfig)
2795 {
2796         struct skl_dfw_v4_module *dfw =
2797                                 (struct skl_dfw_v4_module *)tplg_w->priv.data;
2798         int ret;
2799
2800         dev_dbg(dev, "Parsing Skylake v4 widget topology data\n");
2801
2802         ret = guid_parse(dfw->uuid, (guid_t *)mconfig->guid);
2803         if (ret)
2804                 return ret;
2805         mconfig->id.module_id = -1;
2806         mconfig->id.instance_id = dfw->instance_id;
2807         mconfig->module->resources[0].cpc = dfw->max_mcps / 1000;
2808         mconfig->module->resources[0].ibs = dfw->ibs;
2809         mconfig->module->resources[0].obs = dfw->obs;
2810         mconfig->core_id = dfw->core_id;
2811         mconfig->module->max_input_pins = dfw->max_in_queue;
2812         mconfig->module->max_output_pins = dfw->max_out_queue;
2813         mconfig->module->loadable = dfw->is_loadable;
2814         skl_tplg_fill_fmt_v4(mconfig->module->formats[0].inputs, dfw->in_fmt,
2815                              MAX_IN_QUEUE);
2816         skl_tplg_fill_fmt_v4(mconfig->module->formats[0].outputs, dfw->out_fmt,
2817                              MAX_OUT_QUEUE);
2818
2819         mconfig->params_fixup = dfw->params_fixup;
2820         mconfig->converter = dfw->converter;
2821         mconfig->m_type = dfw->module_type;
2822         mconfig->vbus_id = dfw->vbus_id;
2823         mconfig->module->resources[0].is_pages = dfw->mem_pages;
2824
2825         ret = skl_tplg_add_pipe_v4(dev, mconfig, skl, &dfw->pipe);
2826         if (ret)
2827                 return ret;
2828
2829         mconfig->dev_type = dfw->dev_type;
2830         mconfig->hw_conn_type = dfw->hw_conn_type;
2831         mconfig->time_slot = dfw->time_slot;
2832         mconfig->formats_config.caps_size = dfw->caps.caps_size;
2833
2834         mconfig->m_in_pin = devm_kcalloc(dev,
2835                                 MAX_IN_QUEUE, sizeof(*mconfig->m_in_pin),
2836                                 GFP_KERNEL);
2837         if (!mconfig->m_in_pin)
2838                 return -ENOMEM;
2839
2840         mconfig->m_out_pin = devm_kcalloc(dev,
2841                                 MAX_OUT_QUEUE, sizeof(*mconfig->m_out_pin),
2842                                 GFP_KERNEL);
2843         if (!mconfig->m_out_pin)
2844                 return -ENOMEM;
2845
2846         skl_fill_module_pin_info_v4(dfw->in_pin, mconfig->m_in_pin,
2847                                     dfw->is_dynamic_in_pin,
2848                                     mconfig->module->max_input_pins);
2849         skl_fill_module_pin_info_v4(dfw->out_pin, mconfig->m_out_pin,
2850                                     dfw->is_dynamic_out_pin,
2851                                     mconfig->module->max_output_pins);
2852
2853         if (mconfig->formats_config.caps_size) {
2854                 mconfig->formats_config.set_params = dfw->caps.set_params;
2855                 mconfig->formats_config.param_id = dfw->caps.param_id;
2856                 mconfig->formats_config.caps =
2857                 devm_kzalloc(dev, mconfig->formats_config.caps_size,
2858                              GFP_KERNEL);
2859                 if (!mconfig->formats_config.caps)
2860                         return -ENOMEM;
2861                 memcpy(mconfig->formats_config.caps, dfw->caps.caps,
2862                        dfw->caps.caps_size);
2863         }
2864
2865         return 0;
2866 }
2867
2868 /*
2869  * Parse the private data for the token and corresponding value.
2870  * The private data can have multiple data blocks. So, a data block
2871  * is preceded by a descriptor for number of blocks and a descriptor
2872  * for the type and size of the suceeding data block.
2873  */
2874 static int skl_tplg_get_pvt_data(struct snd_soc_tplg_dapm_widget *tplg_w,
2875                                 struct skl_dev *skl, struct device *dev,
2876                                 struct skl_module_cfg *mconfig)
2877 {
2878         struct snd_soc_tplg_vendor_array *array;
2879         int num_blocks, block_size, block_type, off = 0;
2880         char *data;
2881         int ret;
2882
2883         /*
2884          * v4 configuration files have a valid UUID at the start of
2885          * the widget's private data.
2886          */
2887         if (uuid_is_valid((char *)tplg_w->priv.data))
2888                 return skl_tplg_get_pvt_data_v4(tplg_w, skl, dev, mconfig);
2889
2890         /* Read the NUM_DATA_BLOCKS descriptor */
2891         array = (struct snd_soc_tplg_vendor_array *)tplg_w->priv.data;
2892         ret = skl_tplg_get_desc_blocks(dev, array);
2893         if (ret < 0)
2894                 return ret;
2895         num_blocks = ret;
2896
2897         off += array->size;
2898         /* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */
2899         while (num_blocks > 0) {
2900                 array = (struct snd_soc_tplg_vendor_array *)
2901                                 (tplg_w->priv.data + off);
2902
2903                 ret = skl_tplg_get_desc_blocks(dev, array);
2904
2905                 if (ret < 0)
2906                         return ret;
2907                 block_type = ret;
2908                 off += array->size;
2909
2910                 array = (struct snd_soc_tplg_vendor_array *)
2911                         (tplg_w->priv.data + off);
2912
2913                 ret = skl_tplg_get_desc_blocks(dev, array);
2914
2915                 if (ret < 0)
2916                         return ret;
2917                 block_size = ret;
2918                 off += array->size;
2919
2920                 array = (struct snd_soc_tplg_vendor_array *)
2921                         (tplg_w->priv.data + off);
2922
2923                 data = (tplg_w->priv.data + off);
2924
2925                 if (block_type == SKL_TYPE_TUPLE) {
2926                         ret = skl_tplg_get_tokens(dev, data,
2927                                         skl, mconfig, block_size);
2928
2929                         if (ret < 0)
2930                                 return ret;
2931
2932                         --num_blocks;
2933                 } else {
2934                         if (mconfig->formats_config.caps_size > 0)
2935                                 memcpy(mconfig->formats_config.caps, data,
2936                                         mconfig->formats_config.caps_size);
2937                         --num_blocks;
2938                         ret = mconfig->formats_config.caps_size;
2939                 }
2940                 off += ret;
2941         }
2942
2943         return 0;
2944 }
2945
2946 static void skl_clear_pin_config(struct snd_soc_component *component,
2947                                 struct snd_soc_dapm_widget *w)
2948 {
2949         int i;
2950         struct skl_module_cfg *mconfig;
2951         struct skl_pipe *pipe;
2952
2953         if (!strncmp(w->dapm->component->name, component->name,
2954                                         strlen(component->name))) {
2955                 mconfig = w->priv;
2956                 pipe = mconfig->pipe;
2957                 for (i = 0; i < mconfig->module->max_input_pins; i++) {
2958                         mconfig->m_in_pin[i].in_use = false;
2959                         mconfig->m_in_pin[i].pin_state = SKL_PIN_UNBIND;
2960                 }
2961                 for (i = 0; i < mconfig->module->max_output_pins; i++) {
2962                         mconfig->m_out_pin[i].in_use = false;
2963                         mconfig->m_out_pin[i].pin_state = SKL_PIN_UNBIND;
2964                 }
2965                 pipe->state = SKL_PIPE_INVALID;
2966                 mconfig->m_state = SKL_MODULE_UNINIT;
2967         }
2968 }
2969
2970 void skl_cleanup_resources(struct skl_dev *skl)
2971 {
2972         struct snd_soc_component *soc_component = skl->component;
2973         struct snd_soc_dapm_widget *w;
2974         struct snd_soc_card *card;
2975
2976         if (soc_component == NULL)
2977                 return;
2978
2979         card = soc_component->card;
2980         if (!card || !card->instantiated)
2981                 return;
2982
2983         list_for_each_entry(w, &card->widgets, list) {
2984                 if (is_skl_dsp_widget_type(w, skl->dev) && w->priv != NULL)
2985                         skl_clear_pin_config(soc_component, w);
2986         }
2987
2988         skl_clear_module_cnt(skl->dsp);
2989 }
2990
2991 /*
2992  * Topology core widget load callback
2993  *
2994  * This is used to save the private data for each widget which gives
2995  * information to the driver about module and pipeline parameters which DSP
2996  * FW expects like ids, resource values, formats etc
2997  */
2998 static int skl_tplg_widget_load(struct snd_soc_component *cmpnt, int index,
2999                                 struct snd_soc_dapm_widget *w,
3000                                 struct snd_soc_tplg_dapm_widget *tplg_w)
3001 {
3002         int ret;
3003         struct hdac_bus *bus = snd_soc_component_get_drvdata(cmpnt);
3004         struct skl_dev *skl = bus_to_skl(bus);
3005         struct skl_module_cfg *mconfig;
3006
3007         if (!tplg_w->priv.size)
3008                 goto bind_event;
3009
3010         mconfig = devm_kzalloc(bus->dev, sizeof(*mconfig), GFP_KERNEL);
3011
3012         if (!mconfig)
3013                 return -ENOMEM;
3014
3015         if (skl->nr_modules == 0) {
3016                 mconfig->module = devm_kzalloc(bus->dev,
3017                                 sizeof(*mconfig->module), GFP_KERNEL);
3018                 if (!mconfig->module)
3019                         return -ENOMEM;
3020         }
3021
3022         w->priv = mconfig;
3023
3024         /*
3025          * module binary can be loaded later, so set it to query when
3026          * module is load for a use case
3027          */
3028         mconfig->id.module_id = -1;
3029
3030         /* Parse private data for tuples */
3031         ret = skl_tplg_get_pvt_data(tplg_w, skl, bus->dev, mconfig);
3032         if (ret < 0)
3033                 return ret;
3034
3035         skl_debug_init_module(skl->debugfs, w, mconfig);
3036
3037 bind_event:
3038         if (tplg_w->event_type == 0) {
3039                 dev_dbg(bus->dev, "ASoC: No event handler required\n");
3040                 return 0;
3041         }
3042
3043         ret = snd_soc_tplg_widget_bind_event(w, skl_tplg_widget_ops,
3044                                         ARRAY_SIZE(skl_tplg_widget_ops),
3045                                         tplg_w->event_type);
3046
3047         if (ret) {
3048                 dev_err(bus->dev, "%s: No matching event handlers found for %d\n",
3049                                         __func__, tplg_w->event_type);
3050                 return -EINVAL;
3051         }
3052
3053         return 0;
3054 }
3055
3056 static int skl_init_algo_data(struct device *dev, struct soc_bytes_ext *be,
3057                                         struct snd_soc_tplg_bytes_control *bc)
3058 {
3059         struct skl_algo_data *ac;
3060         struct skl_dfw_algo_data *dfw_ac =
3061                                 (struct skl_dfw_algo_data *)bc->priv.data;
3062
3063         ac = devm_kzalloc(dev, sizeof(*ac), GFP_KERNEL);
3064         if (!ac)
3065                 return -ENOMEM;
3066
3067         /* Fill private data */
3068         ac->max = dfw_ac->max;
3069         ac->param_id = dfw_ac->param_id;
3070         ac->set_params = dfw_ac->set_params;
3071         ac->size = dfw_ac->max;
3072
3073         if (ac->max) {
3074                 ac->params = devm_kzalloc(dev, ac->max, GFP_KERNEL);
3075                 if (!ac->params)
3076                         return -ENOMEM;
3077
3078                 memcpy(ac->params, dfw_ac->params, ac->max);
3079         }
3080
3081         be->dobj.private  = ac;
3082         return 0;
3083 }
3084
3085 static int skl_init_enum_data(struct device *dev, struct soc_enum *se,
3086                                 struct snd_soc_tplg_enum_control *ec)
3087 {
3088
3089         void *data;
3090
3091         if (ec->priv.size) {
3092                 data = devm_kzalloc(dev, sizeof(ec->priv.size), GFP_KERNEL);
3093                 if (!data)
3094                         return -ENOMEM;
3095                 memcpy(data, ec->priv.data, ec->priv.size);
3096                 se->dobj.private = data;
3097         }
3098
3099         return 0;
3100
3101 }
3102
3103 static int skl_tplg_control_load(struct snd_soc_component *cmpnt,
3104                                 int index,
3105                                 struct snd_kcontrol_new *kctl,
3106                                 struct snd_soc_tplg_ctl_hdr *hdr)
3107 {
3108         struct soc_bytes_ext *sb;
3109         struct snd_soc_tplg_bytes_control *tplg_bc;
3110         struct snd_soc_tplg_enum_control *tplg_ec;
3111         struct hdac_bus *bus  = snd_soc_component_get_drvdata(cmpnt);
3112         struct soc_enum *se;
3113
3114         switch (hdr->ops.info) {
3115         case SND_SOC_TPLG_CTL_BYTES:
3116                 tplg_bc = container_of(hdr,
3117                                 struct snd_soc_tplg_bytes_control, hdr);
3118                 if (kctl->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
3119                         sb = (struct soc_bytes_ext *)kctl->private_value;
3120                         if (tplg_bc->priv.size)
3121                                 return skl_init_algo_data(
3122                                                 bus->dev, sb, tplg_bc);
3123                 }
3124                 break;
3125
3126         case SND_SOC_TPLG_CTL_ENUM:
3127                 tplg_ec = container_of(hdr,
3128                                 struct snd_soc_tplg_enum_control, hdr);
3129                 if (kctl->access & SNDRV_CTL_ELEM_ACCESS_READ) {
3130                         se = (struct soc_enum *)kctl->private_value;
3131                         if (tplg_ec->priv.size)
3132                                 skl_init_enum_data(bus->dev, se, tplg_ec);
3133                 }
3134
3135                 /*
3136                  * now that the control initializations are done, remove
3137                  * write permission for the DMIC configuration enums to
3138                  * avoid conflicts between NHLT settings and user interaction
3139                  */
3140
3141                 if (hdr->ops.get == SKL_CONTROL_TYPE_MULTI_IO_SELECT_DMIC)
3142                         kctl->access = SNDRV_CTL_ELEM_ACCESS_READ;
3143
3144                 break;
3145
3146         default:
3147                 dev_dbg(bus->dev, "Control load not supported %d:%d:%d\n",
3148                         hdr->ops.get, hdr->ops.put, hdr->ops.info);
3149                 break;
3150         }
3151
3152         return 0;
3153 }
3154
3155 static int skl_tplg_fill_str_mfest_tkn(struct device *dev,
3156                 struct snd_soc_tplg_vendor_string_elem *str_elem,
3157                 struct skl_dev *skl)
3158 {
3159         int tkn_count = 0;
3160         static int ref_count;
3161
3162         switch (str_elem->token) {
3163         case SKL_TKN_STR_LIB_NAME:
3164                 if (ref_count > skl->lib_count - 1) {
3165                         ref_count = 0;
3166                         return -EINVAL;
3167                 }
3168
3169                 strncpy(skl->lib_info[ref_count].name,
3170                         str_elem->string,
3171                         ARRAY_SIZE(skl->lib_info[ref_count].name));
3172                 ref_count++;
3173                 break;
3174
3175         default:
3176                 dev_err(dev, "Not a string token %d\n", str_elem->token);
3177                 break;
3178         }
3179         tkn_count++;
3180
3181         return tkn_count;
3182 }
3183
3184 static int skl_tplg_get_str_tkn(struct device *dev,
3185                 struct snd_soc_tplg_vendor_array *array,
3186                 struct skl_dev *skl)
3187 {
3188         int tkn_count = 0, ret;
3189         struct snd_soc_tplg_vendor_string_elem *str_elem;
3190
3191         str_elem = (struct snd_soc_tplg_vendor_string_elem *)array->value;
3192         while (tkn_count < array->num_elems) {
3193                 ret = skl_tplg_fill_str_mfest_tkn(dev, str_elem, skl);
3194                 str_elem++;
3195
3196                 if (ret < 0)
3197                         return ret;
3198
3199                 tkn_count = tkn_count + ret;
3200         }
3201
3202         return tkn_count;
3203 }
3204
3205 static int skl_tplg_manifest_fill_fmt(struct device *dev,
3206                 struct skl_module_iface *fmt,
3207                 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
3208                 u32 dir, int fmt_idx)
3209 {
3210         struct skl_module_pin_fmt *dst_fmt;
3211         struct skl_module_fmt *mod_fmt;
3212         int ret;
3213
3214         if (!fmt)
3215                 return -EINVAL;
3216
3217         switch (dir) {
3218         case SKL_DIR_IN:
3219                 dst_fmt = &fmt->inputs[fmt_idx];
3220                 break;
3221
3222         case SKL_DIR_OUT:
3223                 dst_fmt = &fmt->outputs[fmt_idx];
3224                 break;
3225
3226         default:
3227                 dev_err(dev, "Invalid direction: %d\n", dir);
3228                 return -EINVAL;
3229         }
3230
3231         mod_fmt = &dst_fmt->fmt;
3232
3233         switch (tkn_elem->token) {
3234         case SKL_TKN_MM_U32_INTF_PIN_ID:
3235                 dst_fmt->id = tkn_elem->value;
3236                 break;
3237
3238         default:
3239                 ret = skl_tplg_fill_fmt(dev, mod_fmt, tkn_elem->token,
3240                                         tkn_elem->value);
3241                 if (ret < 0)
3242                         return ret;
3243                 break;
3244         }
3245
3246         return 0;
3247 }
3248
3249 static int skl_tplg_fill_mod_info(struct device *dev,
3250                 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
3251                 struct skl_module *mod)
3252 {
3253
3254         if (!mod)
3255                 return -EINVAL;
3256
3257         switch (tkn_elem->token) {
3258         case SKL_TKN_U8_IN_PIN_TYPE:
3259                 mod->input_pin_type = tkn_elem->value;
3260                 break;
3261
3262         case SKL_TKN_U8_OUT_PIN_TYPE:
3263                 mod->output_pin_type = tkn_elem->value;
3264                 break;
3265
3266         case SKL_TKN_U8_IN_QUEUE_COUNT:
3267                 mod->max_input_pins = tkn_elem->value;
3268                 break;
3269
3270         case SKL_TKN_U8_OUT_QUEUE_COUNT:
3271                 mod->max_output_pins = tkn_elem->value;
3272                 break;
3273
3274         case SKL_TKN_MM_U8_NUM_RES:
3275                 mod->nr_resources = tkn_elem->value;
3276                 break;
3277
3278         case SKL_TKN_MM_U8_NUM_INTF:
3279                 mod->nr_interfaces = tkn_elem->value;
3280                 break;
3281
3282         default:
3283                 dev_err(dev, "Invalid mod info token %d", tkn_elem->token);
3284                 return -EINVAL;
3285         }
3286
3287         return 0;
3288 }
3289
3290
3291 static int skl_tplg_get_int_tkn(struct device *dev,
3292                 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
3293                 struct skl_dev *skl)
3294 {
3295         int tkn_count = 0, ret;
3296         static int mod_idx, res_val_idx, intf_val_idx, dir, pin_idx;
3297         struct skl_module_res *res = NULL;
3298         struct skl_module_iface *fmt = NULL;
3299         struct skl_module *mod = NULL;
3300         static struct skl_astate_param *astate_table;
3301         static int astate_cfg_idx, count;
3302         int i;
3303         size_t size;
3304
3305         if (skl->modules) {
3306                 mod = skl->modules[mod_idx];
3307                 res = &mod->resources[res_val_idx];
3308                 fmt = &mod->formats[intf_val_idx];
3309         }
3310
3311         switch (tkn_elem->token) {
3312         case SKL_TKN_U32_LIB_COUNT:
3313                 skl->lib_count = tkn_elem->value;
3314                 break;
3315
3316         case SKL_TKN_U8_NUM_MOD:
3317                 skl->nr_modules = tkn_elem->value;
3318                 skl->modules = devm_kcalloc(dev, skl->nr_modules,
3319                                 sizeof(*skl->modules), GFP_KERNEL);
3320                 if (!skl->modules)
3321                         return -ENOMEM;
3322
3323                 for (i = 0; i < skl->nr_modules; i++) {
3324                         skl->modules[i] = devm_kzalloc(dev,
3325                                         sizeof(struct skl_module), GFP_KERNEL);
3326                         if (!skl->modules[i])
3327                                 return -ENOMEM;
3328                 }
3329                 break;
3330
3331         case SKL_TKN_MM_U8_MOD_IDX:
3332                 mod_idx = tkn_elem->value;
3333                 break;
3334
3335         case SKL_TKN_U32_ASTATE_COUNT:
3336                 if (astate_table != NULL) {
3337                         dev_err(dev, "More than one entry for A-State count");
3338                         return -EINVAL;
3339                 }
3340
3341                 if (tkn_elem->value > SKL_MAX_ASTATE_CFG) {
3342                         dev_err(dev, "Invalid A-State count %d\n",
3343                                 tkn_elem->value);
3344                         return -EINVAL;
3345                 }
3346
3347                 size = struct_size(skl->cfg.astate_cfg, astate_table,
3348                                    tkn_elem->value);
3349                 skl->cfg.astate_cfg = devm_kzalloc(dev, size, GFP_KERNEL);
3350                 if (!skl->cfg.astate_cfg)
3351                         return -ENOMEM;
3352
3353                 astate_table = skl->cfg.astate_cfg->astate_table;
3354                 count = skl->cfg.astate_cfg->count = tkn_elem->value;
3355                 break;
3356
3357         case SKL_TKN_U32_ASTATE_IDX:
3358                 if (tkn_elem->value >= count) {
3359                         dev_err(dev, "Invalid A-State index %d\n",
3360                                 tkn_elem->value);
3361                         return -EINVAL;
3362                 }
3363
3364                 astate_cfg_idx = tkn_elem->value;
3365                 break;
3366
3367         case SKL_TKN_U32_ASTATE_KCPS:
3368                 astate_table[astate_cfg_idx].kcps = tkn_elem->value;
3369                 break;
3370
3371         case SKL_TKN_U32_ASTATE_CLK_SRC:
3372                 astate_table[astate_cfg_idx].clk_src = tkn_elem->value;
3373                 break;
3374
3375         case SKL_TKN_U8_IN_PIN_TYPE:
3376         case SKL_TKN_U8_OUT_PIN_TYPE:
3377         case SKL_TKN_U8_IN_QUEUE_COUNT:
3378         case SKL_TKN_U8_OUT_QUEUE_COUNT:
3379         case SKL_TKN_MM_U8_NUM_RES:
3380         case SKL_TKN_MM_U8_NUM_INTF:
3381                 ret = skl_tplg_fill_mod_info(dev, tkn_elem, mod);
3382                 if (ret < 0)
3383                         return ret;
3384                 break;
3385
3386         case SKL_TKN_U32_DIR_PIN_COUNT:
3387                 dir = tkn_elem->value & SKL_IN_DIR_BIT_MASK;
3388                 pin_idx = (tkn_elem->value & SKL_PIN_COUNT_MASK) >> 4;
3389                 break;
3390
3391         case SKL_TKN_MM_U32_RES_ID:
3392                 if (!res)
3393                         return -EINVAL;
3394
3395                 res->id = tkn_elem->value;
3396                 res_val_idx = tkn_elem->value;
3397                 break;
3398
3399         case SKL_TKN_MM_U32_FMT_ID:
3400                 if (!fmt)
3401                         return -EINVAL;
3402
3403                 fmt->fmt_idx = tkn_elem->value;
3404                 intf_val_idx = tkn_elem->value;
3405                 break;
3406
3407         case SKL_TKN_MM_U32_CPS:
3408         case SKL_TKN_MM_U32_DMA_SIZE:
3409         case SKL_TKN_MM_U32_CPC:
3410         case SKL_TKN_U32_MEM_PAGES:
3411         case SKL_TKN_U32_OBS:
3412         case SKL_TKN_U32_IBS:
3413         case SKL_TKN_MM_U32_RES_PIN_ID:
3414         case SKL_TKN_MM_U32_PIN_BUF:
3415                 ret = skl_tplg_fill_res_tkn(dev, tkn_elem, res, pin_idx, dir);
3416                 if (ret < 0)
3417                         return ret;
3418
3419                 break;
3420
3421         case SKL_TKN_MM_U32_NUM_IN_FMT:
3422                 if (!fmt)
3423                         return -EINVAL;
3424
3425                 res->nr_input_pins = tkn_elem->value;
3426                 break;
3427
3428         case SKL_TKN_MM_U32_NUM_OUT_FMT:
3429                 if (!fmt)
3430                         return -EINVAL;
3431
3432                 res->nr_output_pins = tkn_elem->value;
3433                 break;
3434
3435         case SKL_TKN_U32_FMT_CH:
3436         case SKL_TKN_U32_FMT_FREQ:
3437         case SKL_TKN_U32_FMT_BIT_DEPTH:
3438         case SKL_TKN_U32_FMT_SAMPLE_SIZE:
3439         case SKL_TKN_U32_FMT_CH_CONFIG:
3440         case SKL_TKN_U32_FMT_INTERLEAVE:
3441         case SKL_TKN_U32_FMT_SAMPLE_TYPE:
3442         case SKL_TKN_U32_FMT_CH_MAP:
3443         case SKL_TKN_MM_U32_INTF_PIN_ID:
3444                 ret = skl_tplg_manifest_fill_fmt(dev, fmt, tkn_elem,
3445                                                  dir, pin_idx);
3446                 if (ret < 0)
3447                         return ret;
3448                 break;
3449
3450         default:
3451                 dev_err(dev, "Not a manifest token %d\n", tkn_elem->token);
3452                 return -EINVAL;
3453         }
3454         tkn_count++;
3455
3456         return tkn_count;
3457 }
3458
3459 /*
3460  * Fill the manifest structure by parsing the tokens based on the
3461  * type.
3462  */
3463 static int skl_tplg_get_manifest_tkn(struct device *dev,
3464                 char *pvt_data, struct skl_dev *skl,
3465                 int block_size)
3466 {
3467         int tkn_count = 0, ret;
3468         int off = 0, tuple_size = 0;
3469         u8 uuid_index = 0;
3470         struct snd_soc_tplg_vendor_array *array;
3471         struct snd_soc_tplg_vendor_value_elem *tkn_elem;
3472
3473         if (block_size <= 0)
3474                 return -EINVAL;
3475
3476         while (tuple_size < block_size) {
3477                 array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off);
3478                 off += array->size;
3479                 switch (array->type) {
3480                 case SND_SOC_TPLG_TUPLE_TYPE_STRING:
3481                         ret = skl_tplg_get_str_tkn(dev, array, skl);
3482
3483                         if (ret < 0)
3484                                 return ret;
3485                         tkn_count = ret;
3486
3487                         tuple_size += tkn_count *
3488                                 sizeof(struct snd_soc_tplg_vendor_string_elem);
3489                         continue;
3490
3491                 case SND_SOC_TPLG_TUPLE_TYPE_UUID:
3492                         if (array->uuid->token != SKL_TKN_UUID) {
3493                                 dev_err(dev, "Not an UUID token: %d\n",
3494                                         array->uuid->token);
3495                                 return -EINVAL;
3496                         }
3497                         if (uuid_index >= skl->nr_modules) {
3498                                 dev_err(dev, "Too many UUID tokens\n");
3499                                 return -EINVAL;
3500                         }
3501                         import_guid(&skl->modules[uuid_index++]->uuid,
3502                                     array->uuid->uuid);
3503
3504                         tuple_size += sizeof(*array->uuid);
3505                         continue;
3506
3507                 default:
3508                         tkn_elem = array->value;
3509                         tkn_count = 0;
3510                         break;
3511                 }
3512
3513                 while (tkn_count <= array->num_elems - 1) {
3514                         ret = skl_tplg_get_int_tkn(dev,
3515                                         tkn_elem, skl);
3516                         if (ret < 0)
3517                                 return ret;
3518
3519                         tkn_count = tkn_count + ret;
3520                         tkn_elem++;
3521                 }
3522                 tuple_size += (tkn_count * sizeof(*tkn_elem));
3523                 tkn_count = 0;
3524         }
3525
3526         return off;
3527 }
3528
3529 /*
3530  * Parse manifest private data for tokens. The private data block is
3531  * preceded by descriptors for type and size of data block.
3532  */
3533 static int skl_tplg_get_manifest_data(struct snd_soc_tplg_manifest *manifest,
3534                         struct device *dev, struct skl_dev *skl)
3535 {
3536         struct snd_soc_tplg_vendor_array *array;
3537         int num_blocks, block_size = 0, block_type, off = 0;
3538         char *data;
3539         int ret;
3540
3541         /* Read the NUM_DATA_BLOCKS descriptor */
3542         array = (struct snd_soc_tplg_vendor_array *)manifest->priv.data;
3543         ret = skl_tplg_get_desc_blocks(dev, array);
3544         if (ret < 0)
3545                 return ret;
3546         num_blocks = ret;
3547
3548         off += array->size;
3549         /* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */
3550         while (num_blocks > 0) {
3551                 array = (struct snd_soc_tplg_vendor_array *)
3552                                 (manifest->priv.data + off);
3553                 ret = skl_tplg_get_desc_blocks(dev, array);
3554
3555                 if (ret < 0)
3556                         return ret;
3557                 block_type = ret;
3558                 off += array->size;
3559
3560                 array = (struct snd_soc_tplg_vendor_array *)
3561                         (manifest->priv.data + off);
3562
3563                 ret = skl_tplg_get_desc_blocks(dev, array);
3564
3565                 if (ret < 0)
3566                         return ret;
3567                 block_size = ret;
3568                 off += array->size;
3569
3570                 array = (struct snd_soc_tplg_vendor_array *)
3571                         (manifest->priv.data + off);
3572
3573                 data = (manifest->priv.data + off);
3574
3575                 if (block_type == SKL_TYPE_TUPLE) {
3576                         ret = skl_tplg_get_manifest_tkn(dev, data, skl,
3577                                         block_size);
3578
3579                         if (ret < 0)
3580                                 return ret;
3581
3582                         --num_blocks;
3583                 } else {
3584                         return -EINVAL;
3585                 }
3586                 off += ret;
3587         }
3588
3589         return 0;
3590 }
3591
3592 static int skl_manifest_load(struct snd_soc_component *cmpnt, int index,
3593                                 struct snd_soc_tplg_manifest *manifest)
3594 {
3595         struct hdac_bus *bus = snd_soc_component_get_drvdata(cmpnt);
3596         struct skl_dev *skl = bus_to_skl(bus);
3597
3598         /* proceed only if we have private data defined */
3599         if (manifest->priv.size == 0)
3600                 return 0;
3601
3602         skl_tplg_get_manifest_data(manifest, bus->dev, skl);
3603
3604         if (skl->lib_count > SKL_MAX_LIB) {
3605                 dev_err(bus->dev, "Exceeding max Library count. Got:%d\n",
3606                                         skl->lib_count);
3607                 return  -EINVAL;
3608         }
3609
3610         return 0;
3611 }
3612
3613 static void skl_tplg_complete(struct snd_soc_component *component)
3614 {
3615         struct snd_soc_dobj *dobj;
3616         struct snd_soc_acpi_mach *mach =
3617                 dev_get_platdata(component->card->dev);
3618         int i;
3619
3620         list_for_each_entry(dobj, &component->dobj_list, list) {
3621                 struct snd_kcontrol *kcontrol = dobj->control.kcontrol;
3622                 struct soc_enum *se =
3623                         (struct soc_enum *)kcontrol->private_value;
3624                 char **texts = dobj->control.dtexts;
3625                 char chan_text[4];
3626
3627                 if (dobj->type != SND_SOC_DOBJ_ENUM ||
3628                     dobj->control.kcontrol->put !=
3629                     skl_tplg_multi_config_set_dmic)
3630                         continue;
3631                 sprintf(chan_text, "c%d", mach->mach_params.dmic_num);
3632
3633                 for (i = 0; i < se->items; i++) {
3634                         struct snd_ctl_elem_value val;
3635
3636                         if (strstr(texts[i], chan_text)) {
3637                                 val.value.enumerated.item[0] = i;
3638                                 kcontrol->put(kcontrol, &val);
3639                         }
3640                 }
3641         }
3642 }
3643
3644 static struct snd_soc_tplg_ops skl_tplg_ops  = {
3645         .widget_load = skl_tplg_widget_load,
3646         .control_load = skl_tplg_control_load,
3647         .bytes_ext_ops = skl_tlv_ops,
3648         .bytes_ext_ops_count = ARRAY_SIZE(skl_tlv_ops),
3649         .io_ops = skl_tplg_kcontrol_ops,
3650         .io_ops_count = ARRAY_SIZE(skl_tplg_kcontrol_ops),
3651         .manifest = skl_manifest_load,
3652         .dai_load = skl_dai_load,
3653         .complete = skl_tplg_complete,
3654 };
3655
3656 /*
3657  * A pipe can have multiple modules, each of them will be a DAPM widget as
3658  * well. While managing a pipeline we need to get the list of all the
3659  * widgets in a pipelines, so this helper - skl_tplg_create_pipe_widget_list()
3660  * helps to get the SKL type widgets in that pipeline
3661  */
3662 static int skl_tplg_create_pipe_widget_list(struct snd_soc_component *component)
3663 {
3664         struct snd_soc_dapm_widget *w;
3665         struct skl_module_cfg *mcfg = NULL;
3666         struct skl_pipe_module *p_module = NULL;
3667         struct skl_pipe *pipe;
3668
3669         list_for_each_entry(w, &component->card->widgets, list) {
3670                 if (is_skl_dsp_widget_type(w, component->dev) && w->priv) {
3671                         mcfg = w->priv;
3672                         pipe = mcfg->pipe;
3673
3674                         p_module = devm_kzalloc(component->dev,
3675                                                 sizeof(*p_module), GFP_KERNEL);
3676                         if (!p_module)
3677                                 return -ENOMEM;
3678
3679                         p_module->w = w;
3680                         list_add_tail(&p_module->node, &pipe->w_list);
3681                 }
3682         }
3683
3684         return 0;
3685 }
3686
3687 static void skl_tplg_set_pipe_type(struct skl_dev *skl, struct skl_pipe *pipe)
3688 {
3689         struct skl_pipe_module *w_module;
3690         struct snd_soc_dapm_widget *w;
3691         struct skl_module_cfg *mconfig;
3692         bool host_found = false, link_found = false;
3693
3694         list_for_each_entry(w_module, &pipe->w_list, node) {
3695                 w = w_module->w;
3696                 mconfig = w->priv;
3697
3698                 if (mconfig->dev_type == SKL_DEVICE_HDAHOST)
3699                         host_found = true;
3700                 else if (mconfig->dev_type != SKL_DEVICE_NONE)
3701                         link_found = true;
3702         }
3703
3704         if (host_found && link_found)
3705                 pipe->passthru = true;
3706         else
3707                 pipe->passthru = false;
3708 }
3709
3710 /*
3711  * SKL topology init routine
3712  */
3713 int skl_tplg_init(struct snd_soc_component *component, struct hdac_bus *bus)
3714 {
3715         int ret;
3716         const struct firmware *fw;
3717         struct skl_dev *skl = bus_to_skl(bus);
3718         struct skl_pipeline *ppl;
3719
3720         ret = request_firmware(&fw, skl->tplg_name, bus->dev);
3721         if (ret < 0) {
3722                 char alt_tplg_name[64];
3723
3724                 snprintf(alt_tplg_name, sizeof(alt_tplg_name), "%s-tplg.bin",
3725                          skl->mach->drv_name);
3726                 dev_info(bus->dev, "tplg fw %s load failed with %d, trying alternative tplg name %s",
3727                          skl->tplg_name, ret, alt_tplg_name);
3728
3729                 ret = request_firmware(&fw, alt_tplg_name, bus->dev);
3730                 if (!ret)
3731                         goto component_load;
3732
3733                 dev_info(bus->dev, "tplg %s failed with %d, falling back to dfw_sst.bin",
3734                          alt_tplg_name, ret);
3735
3736                 ret = request_firmware(&fw, "dfw_sst.bin", bus->dev);
3737                 if (ret < 0) {
3738                         dev_err(bus->dev, "Fallback tplg fw %s load failed with %d\n",
3739                                         "dfw_sst.bin", ret);
3740                         return ret;
3741                 }
3742         }
3743
3744 component_load:
3745         ret = snd_soc_tplg_component_load(component, &skl_tplg_ops, fw);
3746         if (ret < 0) {
3747                 dev_err(bus->dev, "tplg component load failed%d\n", ret);
3748                 goto err;
3749         }
3750
3751         ret = skl_tplg_create_pipe_widget_list(component);
3752         if (ret < 0) {
3753                 dev_err(bus->dev, "tplg create pipe widget list failed%d\n",
3754                                 ret);
3755                 goto err;
3756         }
3757
3758         list_for_each_entry(ppl, &skl->ppl_list, node)
3759                 skl_tplg_set_pipe_type(skl, ppl->pipe);
3760
3761 err:
3762         release_firmware(fw);
3763         return ret;
3764 }
3765
3766 void skl_tplg_exit(struct snd_soc_component *component, struct hdac_bus *bus)
3767 {
3768         struct skl_dev *skl = bus_to_skl(bus);
3769         struct skl_pipeline *ppl, *tmp;
3770
3771         list_for_each_entry_safe(ppl, tmp, &skl->ppl_list, node)
3772                 list_del(&ppl->node);
3773
3774         /* clean up topology */
3775         snd_soc_tplg_component_remove(component);
3776 }