Merge remote-tracking branch 'asoc/fix/dapm' into asoc-linus
[cascardo/linux.git] / sound / soc / intel / skylake / skl-topology.c
1 /*
2  *  skl-topology.c - Implements Platform component ALSA controls/widget
3  *  handlers.
4  *
5  *  Copyright (C) 2014-2015 Intel Corp
6  *  Author: Jeeja KP <jeeja.kp@intel.com>
7  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as version 2, as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  */
18
19 #include <linux/slab.h>
20 #include <linux/types.h>
21 #include <linux/firmware.h>
22 #include <sound/soc.h>
23 #include <sound/soc-topology.h>
24 #include "skl-sst-dsp.h"
25 #include "skl-sst-ipc.h"
26 #include "skl-topology.h"
27 #include "skl.h"
28 #include "skl-tplg-interface.h"
29 #include "../common/sst-dsp.h"
30 #include "../common/sst-dsp-priv.h"
31
32 #define SKL_CH_FIXUP_MASK               (1 << 0)
33 #define SKL_RATE_FIXUP_MASK             (1 << 1)
34 #define SKL_FMT_FIXUP_MASK              (1 << 2)
35
36 /*
37  * SKL DSP driver modelling uses only few DAPM widgets so for rest we will
38  * ignore. This helpers checks if the SKL driver handles this widget type
39  */
40 static int is_skl_dsp_widget_type(struct snd_soc_dapm_widget *w)
41 {
42         switch (w->id) {
43         case snd_soc_dapm_dai_link:
44         case snd_soc_dapm_dai_in:
45         case snd_soc_dapm_aif_in:
46         case snd_soc_dapm_aif_out:
47         case snd_soc_dapm_dai_out:
48         case snd_soc_dapm_switch:
49                 return false;
50         default:
51                 return true;
52         }
53 }
54
55 /*
56  * Each pipelines needs memory to be allocated. Check if we have free memory
57  * from available pool. Then only add this to pool
58  * This is freed when pipe is deleted
59  * Note: DSP does actual memory management we only keep track for complete
60  * pool
61  */
62 static bool skl_tplg_alloc_pipe_mem(struct skl *skl,
63                                 struct skl_module_cfg *mconfig)
64 {
65         struct skl_sst *ctx = skl->skl_sst;
66
67         if (skl->resource.mem + mconfig->pipe->memory_pages >
68                                 skl->resource.max_mem) {
69                 dev_err(ctx->dev,
70                                 "%s: module_id %d instance %d\n", __func__,
71                                 mconfig->id.module_id,
72                                 mconfig->id.instance_id);
73                 dev_err(ctx->dev,
74                                 "exceeds ppl memory available %d mem %d\n",
75                                 skl->resource.max_mem, skl->resource.mem);
76                 return false;
77         }
78
79         skl->resource.mem += mconfig->pipe->memory_pages;
80         return true;
81 }
82
83 /*
84  * Pipeline needs needs DSP CPU resources for computation, this is
85  * quantified in MCPS (Million Clocks Per Second) required for module/pipe
86  *
87  * Each pipelines needs mcps to be allocated. Check if we have mcps for this
88  * pipe. This adds the mcps to driver counter
89  * This is removed on pipeline delete
90  */
91 static bool skl_tplg_alloc_pipe_mcps(struct skl *skl,
92                                 struct skl_module_cfg *mconfig)
93 {
94         struct skl_sst *ctx = skl->skl_sst;
95
96         if (skl->resource.mcps + mconfig->mcps > skl->resource.max_mcps) {
97                 dev_err(ctx->dev,
98                         "%s: module_id %d instance %d\n", __func__,
99                         mconfig->id.module_id, mconfig->id.instance_id);
100                 dev_err(ctx->dev,
101                         "exceeds ppl memory available %d > mem %d\n",
102                         skl->resource.max_mcps, skl->resource.mcps);
103                 return false;
104         }
105
106         skl->resource.mcps += mconfig->mcps;
107         return true;
108 }
109
110 /*
111  * Free the mcps when tearing down
112  */
113 static void
114 skl_tplg_free_pipe_mcps(struct skl *skl, struct skl_module_cfg *mconfig)
115 {
116         skl->resource.mcps -= mconfig->mcps;
117 }
118
119 /*
120  * Free the memory when tearing down
121  */
122 static void
123 skl_tplg_free_pipe_mem(struct skl *skl, struct skl_module_cfg *mconfig)
124 {
125         skl->resource.mem -= mconfig->pipe->memory_pages;
126 }
127
128
129 static void skl_dump_mconfig(struct skl_sst *ctx,
130                                         struct skl_module_cfg *mcfg)
131 {
132         dev_dbg(ctx->dev, "Dumping config\n");
133         dev_dbg(ctx->dev, "Input Format:\n");
134         dev_dbg(ctx->dev, "channels = %d\n", mcfg->in_fmt[0].channels);
135         dev_dbg(ctx->dev, "s_freq = %d\n", mcfg->in_fmt[0].s_freq);
136         dev_dbg(ctx->dev, "ch_cfg = %d\n", mcfg->in_fmt[0].ch_cfg);
137         dev_dbg(ctx->dev, "valid bit depth = %d\n", mcfg->in_fmt[0].valid_bit_depth);
138         dev_dbg(ctx->dev, "Output Format:\n");
139         dev_dbg(ctx->dev, "channels = %d\n", mcfg->out_fmt[0].channels);
140         dev_dbg(ctx->dev, "s_freq = %d\n", mcfg->out_fmt[0].s_freq);
141         dev_dbg(ctx->dev, "valid bit depth = %d\n", mcfg->out_fmt[0].valid_bit_depth);
142         dev_dbg(ctx->dev, "ch_cfg = %d\n", mcfg->out_fmt[0].ch_cfg);
143 }
144
145 static void skl_tplg_update_params(struct skl_module_fmt *fmt,
146                         struct skl_pipe_params *params, int fixup)
147 {
148         if (fixup & SKL_RATE_FIXUP_MASK)
149                 fmt->s_freq = params->s_freq;
150         if (fixup & SKL_CH_FIXUP_MASK)
151                 fmt->channels = params->ch;
152         if (fixup & SKL_FMT_FIXUP_MASK) {
153                 fmt->valid_bit_depth = skl_get_bit_depth(params->s_fmt);
154
155                 /*
156                  * 16 bit is 16 bit container whereas 24 bit is in 32 bit
157                  * container so update bit depth accordingly
158                  */
159                 switch (fmt->valid_bit_depth) {
160                 case SKL_DEPTH_16BIT:
161                         fmt->bit_depth = fmt->valid_bit_depth;
162                         break;
163
164                 default:
165                         fmt->bit_depth = SKL_DEPTH_32BIT;
166                         break;
167                 }
168         }
169
170 }
171
172 /*
173  * A pipeline may have modules which impact the pcm parameters, like SRC,
174  * channel converter, format converter.
175  * We need to calculate the output params by applying the 'fixup'
176  * Topology will tell driver which type of fixup is to be applied by
177  * supplying the fixup mask, so based on that we calculate the output
178  *
179  * Now In FE the pcm hw_params is source/target format. Same is applicable
180  * for BE with its hw_params invoked.
181  * here based on FE, BE pipeline and direction we calculate the input and
182  * outfix and then apply that for a module
183  */
184 static void skl_tplg_update_params_fixup(struct skl_module_cfg *m_cfg,
185                 struct skl_pipe_params *params, bool is_fe)
186 {
187         int in_fixup, out_fixup;
188         struct skl_module_fmt *in_fmt, *out_fmt;
189
190         /* Fixups will be applied to pin 0 only */
191         in_fmt = &m_cfg->in_fmt[0];
192         out_fmt = &m_cfg->out_fmt[0];
193
194         if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
195                 if (is_fe) {
196                         in_fixup = m_cfg->params_fixup;
197                         out_fixup = (~m_cfg->converter) &
198                                         m_cfg->params_fixup;
199                 } else {
200                         out_fixup = m_cfg->params_fixup;
201                         in_fixup = (~m_cfg->converter) &
202                                         m_cfg->params_fixup;
203                 }
204         } else {
205                 if (is_fe) {
206                         out_fixup = m_cfg->params_fixup;
207                         in_fixup = (~m_cfg->converter) &
208                                         m_cfg->params_fixup;
209                 } else {
210                         in_fixup = m_cfg->params_fixup;
211                         out_fixup = (~m_cfg->converter) &
212                                         m_cfg->params_fixup;
213                 }
214         }
215
216         skl_tplg_update_params(in_fmt, params, in_fixup);
217         skl_tplg_update_params(out_fmt, params, out_fixup);
218 }
219
220 /*
221  * A module needs input and output buffers, which are dependent upon pcm
222  * params, so once we have calculate params, we need buffer calculation as
223  * well.
224  */
225 static void skl_tplg_update_buffer_size(struct skl_sst *ctx,
226                                 struct skl_module_cfg *mcfg)
227 {
228         int multiplier = 1;
229         struct skl_module_fmt *in_fmt, *out_fmt;
230
231
232         /* Since fixups is applied to pin 0 only, ibs, obs needs
233          * change for pin 0 only
234          */
235         in_fmt = &mcfg->in_fmt[0];
236         out_fmt = &mcfg->out_fmt[0];
237
238         if (mcfg->m_type == SKL_MODULE_TYPE_SRCINT)
239                 multiplier = 5;
240         mcfg->ibs = (in_fmt->s_freq / 1000) *
241                                 (mcfg->in_fmt->channels) *
242                                 (mcfg->in_fmt->bit_depth >> 3) *
243                                 multiplier;
244
245         mcfg->obs = (mcfg->out_fmt->s_freq / 1000) *
246                                 (mcfg->out_fmt->channels) *
247                                 (mcfg->out_fmt->bit_depth >> 3) *
248                                 multiplier;
249 }
250
251 static void skl_tplg_update_module_params(struct snd_soc_dapm_widget *w,
252                                                         struct skl_sst *ctx)
253 {
254         struct skl_module_cfg *m_cfg = w->priv;
255         struct skl_pipe_params *params = m_cfg->pipe->p_params;
256         int p_conn_type = m_cfg->pipe->conn_type;
257         bool is_fe;
258
259         if (!m_cfg->params_fixup)
260                 return;
261
262         dev_dbg(ctx->dev, "Mconfig for widget=%s BEFORE updation\n",
263                                 w->name);
264
265         skl_dump_mconfig(ctx, m_cfg);
266
267         if (p_conn_type == SKL_PIPE_CONN_TYPE_FE)
268                 is_fe = true;
269         else
270                 is_fe = false;
271
272         skl_tplg_update_params_fixup(m_cfg, params, is_fe);
273         skl_tplg_update_buffer_size(ctx, m_cfg);
274
275         dev_dbg(ctx->dev, "Mconfig for widget=%s AFTER updation\n",
276                                 w->name);
277
278         skl_dump_mconfig(ctx, m_cfg);
279 }
280
281 /*
282  * A pipe can have multiple modules, each of them will be a DAPM widget as
283  * well. While managing a pipeline we need to get the list of all the
284  * widgets in a pipelines, so this helper - skl_tplg_get_pipe_widget() helps
285  * to get the SKL type widgets in that pipeline
286  */
287 static int skl_tplg_alloc_pipe_widget(struct device *dev,
288         struct snd_soc_dapm_widget *w, struct skl_pipe *pipe)
289 {
290         struct skl_module_cfg *src_module = NULL;
291         struct snd_soc_dapm_path *p = NULL;
292         struct skl_pipe_module *p_module = NULL;
293
294         p_module = devm_kzalloc(dev, sizeof(*p_module), GFP_KERNEL);
295         if (!p_module)
296                 return -ENOMEM;
297
298         p_module->w = w;
299         list_add_tail(&p_module->node, &pipe->w_list);
300
301         snd_soc_dapm_widget_for_each_sink_path(w, p) {
302                 if ((p->sink->priv == NULL)
303                                 && (!is_skl_dsp_widget_type(w)))
304                         continue;
305
306                 if ((p->sink->priv != NULL) && p->connect
307                                 && is_skl_dsp_widget_type(p->sink)) {
308
309                         src_module = p->sink->priv;
310                         if (pipe->ppl_id == src_module->pipe->ppl_id)
311                                 skl_tplg_alloc_pipe_widget(dev,
312                                                         p->sink, pipe);
313                 }
314         }
315         return 0;
316 }
317
318 /*
319  * some modules can have multiple params set from user control and
320  * need to be set after module is initialized. If set_param flag is
321  * set module params will be done after module is initialised.
322  */
323 static int skl_tplg_set_module_params(struct snd_soc_dapm_widget *w,
324                                                 struct skl_sst *ctx)
325 {
326         int i, ret;
327         struct skl_module_cfg *mconfig = w->priv;
328         const struct snd_kcontrol_new *k;
329         struct soc_bytes_ext *sb;
330         struct skl_algo_data *bc;
331         struct skl_specific_cfg *sp_cfg;
332
333         if (mconfig->formats_config.caps_size > 0 &&
334                 mconfig->formats_config.set_params == SKL_PARAM_SET) {
335                 sp_cfg = &mconfig->formats_config;
336                 ret = skl_set_module_params(ctx, sp_cfg->caps,
337                                         sp_cfg->caps_size,
338                                         sp_cfg->param_id, mconfig);
339                 if (ret < 0)
340                         return ret;
341         }
342
343         for (i = 0; i < w->num_kcontrols; i++) {
344                 k = &w->kcontrol_news[i];
345                 if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
346                         sb = (void *) k->private_value;
347                         bc = (struct skl_algo_data *)sb->dobj.private;
348
349                         if (bc->set_params == SKL_PARAM_SET) {
350                                 ret = skl_set_module_params(ctx,
351                                                 (u32 *)bc->params, bc->max,
352                                                 bc->param_id, mconfig);
353                                 if (ret < 0)
354                                         return ret;
355                         }
356                 }
357         }
358
359         return 0;
360 }
361
362 /*
363  * some module param can set from user control and this is required as
364  * when module is initailzed. if module param is required in init it is
365  * identifed by set_param flag. if set_param flag is not set, then this
366  * parameter needs to set as part of module init.
367  */
368 static int skl_tplg_set_module_init_data(struct snd_soc_dapm_widget *w)
369 {
370         const struct snd_kcontrol_new *k;
371         struct soc_bytes_ext *sb;
372         struct skl_algo_data *bc;
373         struct skl_module_cfg *mconfig = w->priv;
374         int i;
375
376         for (i = 0; i < w->num_kcontrols; i++) {
377                 k = &w->kcontrol_news[i];
378                 if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
379                         sb = (struct soc_bytes_ext *)k->private_value;
380                         bc = (struct skl_algo_data *)sb->dobj.private;
381
382                         if (bc->set_params != SKL_PARAM_INIT)
383                                 continue;
384
385                         mconfig->formats_config.caps = (u32 *)&bc->params;
386                         mconfig->formats_config.caps_size = bc->max;
387
388                         break;
389                 }
390         }
391
392         return 0;
393 }
394
395 /*
396  * Inside a pipe instance, we can have various modules. These modules need
397  * to instantiated in DSP by invoking INIT_MODULE IPC, which is achieved by
398  * skl_init_module() routine, so invoke that for all modules in a pipeline
399  */
400 static int
401 skl_tplg_init_pipe_modules(struct skl *skl, struct skl_pipe *pipe)
402 {
403         struct skl_pipe_module *w_module;
404         struct snd_soc_dapm_widget *w;
405         struct skl_module_cfg *mconfig;
406         struct skl_sst *ctx = skl->skl_sst;
407         int ret = 0;
408
409         list_for_each_entry(w_module, &pipe->w_list, node) {
410                 w = w_module->w;
411                 mconfig = w->priv;
412
413                 /* check resource available */
414                 if (!skl_tplg_alloc_pipe_mcps(skl, mconfig))
415                         return -ENOMEM;
416
417                 if (mconfig->is_loadable && ctx->dsp->fw_ops.load_mod) {
418                         ret = ctx->dsp->fw_ops.load_mod(ctx->dsp,
419                                 mconfig->id.module_id, mconfig->guid);
420                         if (ret < 0)
421                                 return ret;
422                 }
423
424                 /*
425                  * apply fix/conversion to module params based on
426                  * FE/BE params
427                  */
428                 skl_tplg_update_module_params(w, ctx);
429
430                 skl_tplg_set_module_init_data(w);
431                 ret = skl_init_module(ctx, mconfig);
432                 if (ret < 0)
433                         return ret;
434
435                 ret = skl_tplg_set_module_params(w, ctx);
436                 if (ret < 0)
437                         return ret;
438         }
439
440         return 0;
441 }
442
443 static int skl_tplg_unload_pipe_modules(struct skl_sst *ctx,
444          struct skl_pipe *pipe)
445 {
446         struct skl_pipe_module *w_module = NULL;
447         struct skl_module_cfg *mconfig = NULL;
448
449         list_for_each_entry(w_module, &pipe->w_list, node) {
450                 mconfig  = w_module->w->priv;
451
452                 if (mconfig->is_loadable && ctx->dsp->fw_ops.unload_mod)
453                         return ctx->dsp->fw_ops.unload_mod(ctx->dsp,
454                                                 mconfig->id.module_id);
455         }
456
457         /* no modules to unload in this path, so return */
458         return 0;
459 }
460
461 /*
462  * Mixer module represents a pipeline. So in the Pre-PMU event of mixer we
463  * need create the pipeline. So we do following:
464  *   - check the resources
465  *   - Create the pipeline
466  *   - Initialize the modules in pipeline
467  *   - finally bind all modules together
468  */
469 static int skl_tplg_mixer_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
470                                                         struct skl *skl)
471 {
472         int ret;
473         struct skl_module_cfg *mconfig = w->priv;
474         struct skl_pipe_module *w_module;
475         struct skl_pipe *s_pipe = mconfig->pipe;
476         struct skl_module_cfg *src_module = NULL, *dst_module;
477         struct skl_sst *ctx = skl->skl_sst;
478
479         /* check resource available */
480         if (!skl_tplg_alloc_pipe_mcps(skl, mconfig))
481                 return -EBUSY;
482
483         if (!skl_tplg_alloc_pipe_mem(skl, mconfig))
484                 return -ENOMEM;
485
486         /*
487          * Create a list of modules for pipe.
488          * This list contains modules from source to sink
489          */
490         ret = skl_create_pipeline(ctx, mconfig->pipe);
491         if (ret < 0)
492                 return ret;
493
494         /*
495          * we create a w_list of all widgets in that pipe. This list is not
496          * freed on PMD event as widgets within a pipe are static. This
497          * saves us cycles to get widgets in pipe every time.
498          *
499          * So if we have already initialized all the widgets of a pipeline
500          * we skip, so check for list_empty and create the list if empty
501          */
502         if (list_empty(&s_pipe->w_list)) {
503                 ret = skl_tplg_alloc_pipe_widget(ctx->dev, w, s_pipe);
504                 if (ret < 0)
505                         return ret;
506         }
507
508         /* Init all pipe modules from source to sink */
509         ret = skl_tplg_init_pipe_modules(skl, s_pipe);
510         if (ret < 0)
511                 return ret;
512
513         /* Bind modules from source to sink */
514         list_for_each_entry(w_module, &s_pipe->w_list, node) {
515                 dst_module = w_module->w->priv;
516
517                 if (src_module == NULL) {
518                         src_module = dst_module;
519                         continue;
520                 }
521
522                 ret = skl_bind_modules(ctx, src_module, dst_module);
523                 if (ret < 0)
524                         return ret;
525
526                 src_module = dst_module;
527         }
528
529         return 0;
530 }
531
532 static int skl_tplg_bind_sinks(struct snd_soc_dapm_widget *w,
533                                 struct skl *skl,
534                                 struct skl_module_cfg *src_mconfig)
535 {
536         struct snd_soc_dapm_path *p;
537         struct snd_soc_dapm_widget *sink = NULL, *next_sink = NULL;
538         struct skl_module_cfg *sink_mconfig;
539         struct skl_sst *ctx = skl->skl_sst;
540         int ret;
541
542         snd_soc_dapm_widget_for_each_sink_path(w, p) {
543                 if (!p->connect)
544                         continue;
545
546                 dev_dbg(ctx->dev, "%s: src widget=%s\n", __func__, w->name);
547                 dev_dbg(ctx->dev, "%s: sink widget=%s\n", __func__, p->sink->name);
548
549                 next_sink = p->sink;
550                 /*
551                  * here we will check widgets in sink pipelines, so that
552                  * can be any widgets type and we are only interested if
553                  * they are ones used for SKL so check that first
554                  */
555                 if ((p->sink->priv != NULL) &&
556                                         is_skl_dsp_widget_type(p->sink)) {
557
558                         sink = p->sink;
559                         sink_mconfig = sink->priv;
560
561                         /* Bind source to sink, mixin is always source */
562                         ret = skl_bind_modules(ctx, src_mconfig, sink_mconfig);
563                         if (ret)
564                                 return ret;
565
566                         /* Start sinks pipe first */
567                         if (sink_mconfig->pipe->state != SKL_PIPE_STARTED) {
568                                 if (sink_mconfig->pipe->conn_type !=
569                                                         SKL_PIPE_CONN_TYPE_FE)
570                                         ret = skl_run_pipe(ctx,
571                                                         sink_mconfig->pipe);
572                                 if (ret)
573                                         return ret;
574                         }
575                 }
576         }
577
578         if (!sink)
579                 return skl_tplg_bind_sinks(next_sink, skl, src_mconfig);
580
581         return 0;
582 }
583
584 /*
585  * A PGA represents a module in a pipeline. So in the Pre-PMU event of PGA
586  * we need to do following:
587  *   - Bind to sink pipeline
588  *      Since the sink pipes can be running and we don't get mixer event on
589  *      connect for already running mixer, we need to find the sink pipes
590  *      here and bind to them. This way dynamic connect works.
591  *   - Start sink pipeline, if not running
592  *   - Then run current pipe
593  */
594 static int skl_tplg_pga_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
595                                                                 struct skl *skl)
596 {
597         struct skl_module_cfg *src_mconfig;
598         struct skl_sst *ctx = skl->skl_sst;
599         int ret = 0;
600
601         src_mconfig = w->priv;
602
603         /*
604          * find which sink it is connected to, bind with the sink,
605          * if sink is not started, start sink pipe first, then start
606          * this pipe
607          */
608         ret = skl_tplg_bind_sinks(w, skl, src_mconfig);
609         if (ret)
610                 return ret;
611
612         /* Start source pipe last after starting all sinks */
613         if (src_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
614                 return skl_run_pipe(ctx, src_mconfig->pipe);
615
616         return 0;
617 }
618
619 static struct snd_soc_dapm_widget *skl_get_src_dsp_widget(
620                 struct snd_soc_dapm_widget *w, struct skl *skl)
621 {
622         struct snd_soc_dapm_path *p;
623         struct snd_soc_dapm_widget *src_w = NULL;
624         struct skl_sst *ctx = skl->skl_sst;
625
626         snd_soc_dapm_widget_for_each_source_path(w, p) {
627                 src_w = p->source;
628                 if (!p->connect)
629                         continue;
630
631                 dev_dbg(ctx->dev, "sink widget=%s\n", w->name);
632                 dev_dbg(ctx->dev, "src widget=%s\n", p->source->name);
633
634                 /*
635                  * here we will check widgets in sink pipelines, so that can
636                  * be any widgets type and we are only interested if they are
637                  * ones used for SKL so check that first
638                  */
639                 if ((p->source->priv != NULL) &&
640                                         is_skl_dsp_widget_type(p->source)) {
641                         return p->source;
642                 }
643         }
644
645         if (src_w != NULL)
646                 return skl_get_src_dsp_widget(src_w, skl);
647
648         return NULL;
649 }
650
651 /*
652  * in the Post-PMU event of mixer we need to do following:
653  *   - Check if this pipe is running
654  *   - if not, then
655  *      - bind this pipeline to its source pipeline
656  *        if source pipe is already running, this means it is a dynamic
657  *        connection and we need to bind only to that pipe
658  *      - start this pipeline
659  */
660 static int skl_tplg_mixer_dapm_post_pmu_event(struct snd_soc_dapm_widget *w,
661                                                         struct skl *skl)
662 {
663         int ret = 0;
664         struct snd_soc_dapm_widget *source, *sink;
665         struct skl_module_cfg *src_mconfig, *sink_mconfig;
666         struct skl_sst *ctx = skl->skl_sst;
667         int src_pipe_started = 0;
668
669         sink = w;
670         sink_mconfig = sink->priv;
671
672         /*
673          * If source pipe is already started, that means source is driving
674          * one more sink before this sink got connected, Since source is
675          * started, bind this sink to source and start this pipe.
676          */
677         source = skl_get_src_dsp_widget(w, skl);
678         if (source != NULL) {
679                 src_mconfig = source->priv;
680                 sink_mconfig = sink->priv;
681                 src_pipe_started = 1;
682
683                 /*
684                  * check pipe state, then no need to bind or start the
685                  * pipe
686                  */
687                 if (src_mconfig->pipe->state != SKL_PIPE_STARTED)
688                         src_pipe_started = 0;
689         }
690
691         if (src_pipe_started) {
692                 ret = skl_bind_modules(ctx, src_mconfig, sink_mconfig);
693                 if (ret)
694                         return ret;
695
696                 if (sink_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
697                         ret = skl_run_pipe(ctx, sink_mconfig->pipe);
698         }
699
700         return ret;
701 }
702
703 /*
704  * in the Pre-PMD event of mixer we need to do following:
705  *   - Stop the pipe
706  *   - find the source connections and remove that from dapm_path_list
707  *   - unbind with source pipelines if still connected
708  */
709 static int skl_tplg_mixer_dapm_pre_pmd_event(struct snd_soc_dapm_widget *w,
710                                                         struct skl *skl)
711 {
712         struct skl_module_cfg *src_mconfig, *sink_mconfig;
713         int ret = 0, i;
714         struct skl_sst *ctx = skl->skl_sst;
715
716         sink_mconfig = w->priv;
717
718         /* Stop the pipe */
719         ret = skl_stop_pipe(ctx, sink_mconfig->pipe);
720         if (ret)
721                 return ret;
722
723         for (i = 0; i < sink_mconfig->max_in_queue; i++) {
724                 if (sink_mconfig->m_in_pin[i].pin_state == SKL_PIN_BIND_DONE) {
725                         src_mconfig = sink_mconfig->m_in_pin[i].tgt_mcfg;
726                         if (!src_mconfig)
727                                 continue;
728                         /*
729                          * If path_found == 1, that means pmd for source
730                          * pipe has not occurred, source is connected to
731                          * some other sink. so its responsibility of sink
732                          * to unbind itself from source.
733                          */
734                         ret = skl_stop_pipe(ctx, src_mconfig->pipe);
735                         if (ret < 0)
736                                 return ret;
737
738                         ret = skl_unbind_modules(ctx,
739                                                 src_mconfig, sink_mconfig);
740                 }
741         }
742
743         return ret;
744 }
745
746 /*
747  * in the Post-PMD event of mixer we need to do following:
748  *   - Free the mcps used
749  *   - Free the mem used
750  *   - Unbind the modules within the pipeline
751  *   - Delete the pipeline (modules are not required to be explicitly
752  *     deleted, pipeline delete is enough here
753  */
754 static int skl_tplg_mixer_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
755                                                         struct skl *skl)
756 {
757         struct skl_module_cfg *mconfig = w->priv;
758         struct skl_pipe_module *w_module;
759         struct skl_module_cfg *src_module = NULL, *dst_module;
760         struct skl_sst *ctx = skl->skl_sst;
761         struct skl_pipe *s_pipe = mconfig->pipe;
762         int ret = 0;
763
764         skl_tplg_free_pipe_mcps(skl, mconfig);
765         skl_tplg_free_pipe_mem(skl, mconfig);
766
767         list_for_each_entry(w_module, &s_pipe->w_list, node) {
768                 dst_module = w_module->w->priv;
769
770                 skl_tplg_free_pipe_mcps(skl, dst_module);
771                 if (src_module == NULL) {
772                         src_module = dst_module;
773                         continue;
774                 }
775
776                 ret = skl_unbind_modules(ctx, src_module, dst_module);
777                 if (ret < 0)
778                         return ret;
779
780                 src_module = dst_module;
781         }
782
783         ret = skl_delete_pipe(ctx, mconfig->pipe);
784
785         return skl_tplg_unload_pipe_modules(ctx, s_pipe);
786 }
787
788 /*
789  * in the Post-PMD event of PGA we need to do following:
790  *   - Free the mcps used
791  *   - Stop the pipeline
792  *   - In source pipe is connected, unbind with source pipelines
793  */
794 static int skl_tplg_pga_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
795                                                                 struct skl *skl)
796 {
797         struct skl_module_cfg *src_mconfig, *sink_mconfig;
798         int ret = 0, i;
799         struct skl_sst *ctx = skl->skl_sst;
800
801         src_mconfig = w->priv;
802
803         /* Stop the pipe since this is a mixin module */
804         ret = skl_stop_pipe(ctx, src_mconfig->pipe);
805         if (ret)
806                 return ret;
807
808         for (i = 0; i < src_mconfig->max_out_queue; i++) {
809                 if (src_mconfig->m_out_pin[i].pin_state == SKL_PIN_BIND_DONE) {
810                         sink_mconfig = src_mconfig->m_out_pin[i].tgt_mcfg;
811                         if (!sink_mconfig)
812                                 continue;
813                         /*
814                          * This is a connecter and if path is found that means
815                          * unbind between source and sink has not happened yet
816                          */
817                         ret = skl_stop_pipe(ctx, sink_mconfig->pipe);
818                         if (ret < 0)
819                                 return ret;
820                         ret = skl_unbind_modules(ctx, src_mconfig,
821                                                         sink_mconfig);
822                 }
823         }
824
825         return ret;
826 }
827
828 /*
829  * In modelling, we assume there will be ONLY one mixer in a pipeline.  If
830  * mixer is not required then it is treated as static mixer aka vmixer with
831  * a hard path to source module
832  * So we don't need to check if source is started or not as hard path puts
833  * dependency on each other
834  */
835 static int skl_tplg_vmixer_event(struct snd_soc_dapm_widget *w,
836                                 struct snd_kcontrol *k, int event)
837 {
838         struct snd_soc_dapm_context *dapm = w->dapm;
839         struct skl *skl = get_skl_ctx(dapm->dev);
840
841         switch (event) {
842         case SND_SOC_DAPM_PRE_PMU:
843                 return skl_tplg_mixer_dapm_pre_pmu_event(w, skl);
844
845         case SND_SOC_DAPM_POST_PMD:
846                 return skl_tplg_mixer_dapm_post_pmd_event(w, skl);
847         }
848
849         return 0;
850 }
851
852 /*
853  * In modelling, we assume there will be ONLY one mixer in a pipeline. If a
854  * second one is required that is created as another pipe entity.
855  * The mixer is responsible for pipe management and represent a pipeline
856  * instance
857  */
858 static int skl_tplg_mixer_event(struct snd_soc_dapm_widget *w,
859                                 struct snd_kcontrol *k, int event)
860 {
861         struct snd_soc_dapm_context *dapm = w->dapm;
862         struct skl *skl = get_skl_ctx(dapm->dev);
863
864         switch (event) {
865         case SND_SOC_DAPM_PRE_PMU:
866                 return skl_tplg_mixer_dapm_pre_pmu_event(w, skl);
867
868         case SND_SOC_DAPM_POST_PMU:
869                 return skl_tplg_mixer_dapm_post_pmu_event(w, skl);
870
871         case SND_SOC_DAPM_PRE_PMD:
872                 return skl_tplg_mixer_dapm_pre_pmd_event(w, skl);
873
874         case SND_SOC_DAPM_POST_PMD:
875                 return skl_tplg_mixer_dapm_post_pmd_event(w, skl);
876         }
877
878         return 0;
879 }
880
881 /*
882  * In modelling, we assumed rest of the modules in pipeline are PGA. But we
883  * are interested in last PGA (leaf PGA) in a pipeline to disconnect with
884  * the sink when it is running (two FE to one BE or one FE to two BE)
885  * scenarios
886  */
887 static int skl_tplg_pga_event(struct snd_soc_dapm_widget *w,
888                         struct snd_kcontrol *k, int event)
889
890 {
891         struct snd_soc_dapm_context *dapm = w->dapm;
892         struct skl *skl = get_skl_ctx(dapm->dev);
893
894         switch (event) {
895         case SND_SOC_DAPM_PRE_PMU:
896                 return skl_tplg_pga_dapm_pre_pmu_event(w, skl);
897
898         case SND_SOC_DAPM_POST_PMD:
899                 return skl_tplg_pga_dapm_post_pmd_event(w, skl);
900         }
901
902         return 0;
903 }
904
905 static int skl_tplg_tlv_control_get(struct snd_kcontrol *kcontrol,
906                         unsigned int __user *data, unsigned int size)
907 {
908         struct soc_bytes_ext *sb =
909                         (struct soc_bytes_ext *)kcontrol->private_value;
910         struct skl_algo_data *bc = (struct skl_algo_data *)sb->dobj.private;
911         struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
912         struct skl_module_cfg *mconfig = w->priv;
913         struct skl *skl = get_skl_ctx(w->dapm->dev);
914
915         if (w->power)
916                 skl_get_module_params(skl->skl_sst, (u32 *)bc->params,
917                                       bc->max, bc->param_id, mconfig);
918
919         if (bc->params) {
920                 if (copy_to_user(data, &bc->param_id, sizeof(u32)))
921                         return -EFAULT;
922                 if (copy_to_user(data + 1, &size, sizeof(u32)))
923                         return -EFAULT;
924                 if (copy_to_user(data + 2, bc->params, size))
925                         return -EFAULT;
926         }
927
928         return 0;
929 }
930
931 #define SKL_PARAM_VENDOR_ID 0xff
932
933 static int skl_tplg_tlv_control_set(struct snd_kcontrol *kcontrol,
934                         const unsigned int __user *data, unsigned int size)
935 {
936         struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
937         struct skl_module_cfg *mconfig = w->priv;
938         struct soc_bytes_ext *sb =
939                         (struct soc_bytes_ext *)kcontrol->private_value;
940         struct skl_algo_data *ac = (struct skl_algo_data *)sb->dobj.private;
941         struct skl *skl = get_skl_ctx(w->dapm->dev);
942
943         if (ac->params) {
944                 /*
945                  * if the param_is is of type Vendor, firmware expects actual
946                  * parameter id and size from the control.
947                  */
948                 if (ac->param_id == SKL_PARAM_VENDOR_ID) {
949                         if (copy_from_user(ac->params, data, size))
950                                 return -EFAULT;
951                 } else {
952                         if (copy_from_user(ac->params,
953                                            data + 2 * sizeof(u32), size))
954                                 return -EFAULT;
955                 }
956
957                 if (w->power)
958                         return skl_set_module_params(skl->skl_sst,
959                                                 (u32 *)ac->params, ac->max,
960                                                 ac->param_id, mconfig);
961         }
962
963         return 0;
964 }
965
966 /*
967  * The FE params are passed by hw_params of the DAI.
968  * On hw_params, the params are stored in Gateway module of the FE and we
969  * need to calculate the format in DSP module configuration, that
970  * conversion is done here
971  */
972 int skl_tplg_update_pipe_params(struct device *dev,
973                         struct skl_module_cfg *mconfig,
974                         struct skl_pipe_params *params)
975 {
976         struct skl_pipe *pipe = mconfig->pipe;
977         struct skl_module_fmt *format = NULL;
978
979         memcpy(pipe->p_params, params, sizeof(*params));
980
981         if (params->stream == SNDRV_PCM_STREAM_PLAYBACK)
982                 format = &mconfig->in_fmt[0];
983         else
984                 format = &mconfig->out_fmt[0];
985
986         /* set the hw_params */
987         format->s_freq = params->s_freq;
988         format->channels = params->ch;
989         format->valid_bit_depth = skl_get_bit_depth(params->s_fmt);
990
991         /*
992          * 16 bit is 16 bit container whereas 24 bit is in 32 bit
993          * container so update bit depth accordingly
994          */
995         switch (format->valid_bit_depth) {
996         case SKL_DEPTH_16BIT:
997                 format->bit_depth = format->valid_bit_depth;
998                 break;
999
1000         case SKL_DEPTH_24BIT:
1001         case SKL_DEPTH_32BIT:
1002                 format->bit_depth = SKL_DEPTH_32BIT;
1003                 break;
1004
1005         default:
1006                 dev_err(dev, "Invalid bit depth %x for pipe\n",
1007                                 format->valid_bit_depth);
1008                 return -EINVAL;
1009         }
1010
1011         if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1012                 mconfig->ibs = (format->s_freq / 1000) *
1013                                 (format->channels) *
1014                                 (format->bit_depth >> 3);
1015         } else {
1016                 mconfig->obs = (format->s_freq / 1000) *
1017                                 (format->channels) *
1018                                 (format->bit_depth >> 3);
1019         }
1020
1021         return 0;
1022 }
1023
1024 /*
1025  * Query the module config for the FE DAI
1026  * This is used to find the hw_params set for that DAI and apply to FE
1027  * pipeline
1028  */
1029 struct skl_module_cfg *
1030 skl_tplg_fe_get_cpr_module(struct snd_soc_dai *dai, int stream)
1031 {
1032         struct snd_soc_dapm_widget *w;
1033         struct snd_soc_dapm_path *p = NULL;
1034
1035         if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1036                 w = dai->playback_widget;
1037                 snd_soc_dapm_widget_for_each_sink_path(w, p) {
1038                         if (p->connect && p->sink->power &&
1039                                         !is_skl_dsp_widget_type(p->sink))
1040                                 continue;
1041
1042                         if (p->sink->priv) {
1043                                 dev_dbg(dai->dev, "set params for %s\n",
1044                                                 p->sink->name);
1045                                 return p->sink->priv;
1046                         }
1047                 }
1048         } else {
1049                 w = dai->capture_widget;
1050                 snd_soc_dapm_widget_for_each_source_path(w, p) {
1051                         if (p->connect && p->source->power &&
1052                                         !is_skl_dsp_widget_type(p->source))
1053                                 continue;
1054
1055                         if (p->source->priv) {
1056                                 dev_dbg(dai->dev, "set params for %s\n",
1057                                                 p->source->name);
1058                                 return p->source->priv;
1059                         }
1060                 }
1061         }
1062
1063         return NULL;
1064 }
1065
1066 static u8 skl_tplg_be_link_type(int dev_type)
1067 {
1068         int ret;
1069
1070         switch (dev_type) {
1071         case SKL_DEVICE_BT:
1072                 ret = NHLT_LINK_SSP;
1073                 break;
1074
1075         case SKL_DEVICE_DMIC:
1076                 ret = NHLT_LINK_DMIC;
1077                 break;
1078
1079         case SKL_DEVICE_I2S:
1080                 ret = NHLT_LINK_SSP;
1081                 break;
1082
1083         case SKL_DEVICE_HDALINK:
1084                 ret = NHLT_LINK_HDA;
1085                 break;
1086
1087         default:
1088                 ret = NHLT_LINK_INVALID;
1089                 break;
1090         }
1091
1092         return ret;
1093 }
1094
1095 /*
1096  * Fill the BE gateway parameters
1097  * The BE gateway expects a blob of parameters which are kept in the ACPI
1098  * NHLT blob, so query the blob for interface type (i2s/pdm) and instance.
1099  * The port can have multiple settings so pick based on the PCM
1100  * parameters
1101  */
1102 static int skl_tplg_be_fill_pipe_params(struct snd_soc_dai *dai,
1103                                 struct skl_module_cfg *mconfig,
1104                                 struct skl_pipe_params *params)
1105 {
1106         struct skl_pipe *pipe = mconfig->pipe;
1107         struct nhlt_specific_cfg *cfg;
1108         struct skl *skl = get_skl_ctx(dai->dev);
1109         int link_type = skl_tplg_be_link_type(mconfig->dev_type);
1110
1111         memcpy(pipe->p_params, params, sizeof(*params));
1112
1113         if (link_type == NHLT_LINK_HDA)
1114                 return 0;
1115
1116         /* update the blob based on virtual bus_id*/
1117         cfg = skl_get_ep_blob(skl, mconfig->vbus_id, link_type,
1118                                         params->s_fmt, params->ch,
1119                                         params->s_freq, params->stream);
1120         if (cfg) {
1121                 mconfig->formats_config.caps_size = cfg->size;
1122                 mconfig->formats_config.caps = (u32 *) &cfg->caps;
1123         } else {
1124                 dev_err(dai->dev, "Blob NULL for id %x type %d dirn %d\n",
1125                                         mconfig->vbus_id, link_type,
1126                                         params->stream);
1127                 dev_err(dai->dev, "PCM: ch %d, freq %d, fmt %d\n",
1128                                  params->ch, params->s_freq, params->s_fmt);
1129                 return -EINVAL;
1130         }
1131
1132         return 0;
1133 }
1134
1135 static int skl_tplg_be_set_src_pipe_params(struct snd_soc_dai *dai,
1136                                 struct snd_soc_dapm_widget *w,
1137                                 struct skl_pipe_params *params)
1138 {
1139         struct snd_soc_dapm_path *p;
1140         int ret = -EIO;
1141
1142         snd_soc_dapm_widget_for_each_source_path(w, p) {
1143                 if (p->connect && is_skl_dsp_widget_type(p->source) &&
1144                                                 p->source->priv) {
1145
1146                         ret = skl_tplg_be_fill_pipe_params(dai,
1147                                                 p->source->priv, params);
1148                         if (ret < 0)
1149                                 return ret;
1150                 } else {
1151                         ret = skl_tplg_be_set_src_pipe_params(dai,
1152                                                 p->source, params);
1153                         if (ret < 0)
1154                                 return ret;
1155                 }
1156         }
1157
1158         return ret;
1159 }
1160
1161 static int skl_tplg_be_set_sink_pipe_params(struct snd_soc_dai *dai,
1162         struct snd_soc_dapm_widget *w, struct skl_pipe_params *params)
1163 {
1164         struct snd_soc_dapm_path *p = NULL;
1165         int ret = -EIO;
1166
1167         snd_soc_dapm_widget_for_each_sink_path(w, p) {
1168                 if (p->connect && is_skl_dsp_widget_type(p->sink) &&
1169                                                 p->sink->priv) {
1170
1171                         ret = skl_tplg_be_fill_pipe_params(dai,
1172                                                 p->sink->priv, params);
1173                         if (ret < 0)
1174                                 return ret;
1175                 } else {
1176                         ret = skl_tplg_be_set_sink_pipe_params(
1177                                                 dai, p->sink, params);
1178                         if (ret < 0)
1179                                 return ret;
1180                 }
1181         }
1182
1183         return ret;
1184 }
1185
1186 /*
1187  * BE hw_params can be a source parameters (capture) or sink parameters
1188  * (playback). Based on sink and source we need to either find the source
1189  * list or the sink list and set the pipeline parameters
1190  */
1191 int skl_tplg_be_update_params(struct snd_soc_dai *dai,
1192                                 struct skl_pipe_params *params)
1193 {
1194         struct snd_soc_dapm_widget *w;
1195
1196         if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1197                 w = dai->playback_widget;
1198
1199                 return skl_tplg_be_set_src_pipe_params(dai, w, params);
1200
1201         } else {
1202                 w = dai->capture_widget;
1203
1204                 return skl_tplg_be_set_sink_pipe_params(dai, w, params);
1205         }
1206
1207         return 0;
1208 }
1209
1210 static const struct snd_soc_tplg_widget_events skl_tplg_widget_ops[] = {
1211         {SKL_MIXER_EVENT, skl_tplg_mixer_event},
1212         {SKL_VMIXER_EVENT, skl_tplg_vmixer_event},
1213         {SKL_PGA_EVENT, skl_tplg_pga_event},
1214 };
1215
1216 static const struct snd_soc_tplg_bytes_ext_ops skl_tlv_ops[] = {
1217         {SKL_CONTROL_TYPE_BYTE_TLV, skl_tplg_tlv_control_get,
1218                                         skl_tplg_tlv_control_set},
1219 };
1220
1221 /*
1222  * The topology binary passes the pin info for a module so initialize the pin
1223  * info passed into module instance
1224  */
1225 static void skl_fill_module_pin_info(struct skl_dfw_module_pin *dfw_pin,
1226                                                 struct skl_module_pin *m_pin,
1227                                                 bool is_dynamic, int max_pin)
1228 {
1229         int i;
1230
1231         for (i = 0; i < max_pin; i++) {
1232                 m_pin[i].id.module_id = dfw_pin[i].module_id;
1233                 m_pin[i].id.instance_id = dfw_pin[i].instance_id;
1234                 m_pin[i].in_use = false;
1235                 m_pin[i].is_dynamic = is_dynamic;
1236                 m_pin[i].pin_state = SKL_PIN_UNBIND;
1237         }
1238 }
1239
1240 /*
1241  * Add pipeline from topology binary into driver pipeline list
1242  *
1243  * If already added we return that instance
1244  * Otherwise we create a new instance and add into driver list
1245  */
1246 static struct skl_pipe *skl_tplg_add_pipe(struct device *dev,
1247                         struct skl *skl, struct skl_dfw_pipe *dfw_pipe)
1248 {
1249         struct skl_pipeline *ppl;
1250         struct skl_pipe *pipe;
1251         struct skl_pipe_params *params;
1252
1253         list_for_each_entry(ppl, &skl->ppl_list, node) {
1254                 if (ppl->pipe->ppl_id == dfw_pipe->pipe_id)
1255                         return ppl->pipe;
1256         }
1257
1258         ppl = devm_kzalloc(dev, sizeof(*ppl), GFP_KERNEL);
1259         if (!ppl)
1260                 return NULL;
1261
1262         pipe = devm_kzalloc(dev, sizeof(*pipe), GFP_KERNEL);
1263         if (!pipe)
1264                 return NULL;
1265
1266         params = devm_kzalloc(dev, sizeof(*params), GFP_KERNEL);
1267         if (!params)
1268                 return NULL;
1269
1270         pipe->ppl_id = dfw_pipe->pipe_id;
1271         pipe->memory_pages = dfw_pipe->memory_pages;
1272         pipe->pipe_priority = dfw_pipe->pipe_priority;
1273         pipe->conn_type = dfw_pipe->conn_type;
1274         pipe->state = SKL_PIPE_INVALID;
1275         pipe->p_params = params;
1276         INIT_LIST_HEAD(&pipe->w_list);
1277
1278         ppl->pipe = pipe;
1279         list_add(&ppl->node, &skl->ppl_list);
1280
1281         return ppl->pipe;
1282 }
1283
1284 static void skl_tplg_fill_fmt(struct skl_module_fmt *dst_fmt,
1285                                 struct skl_dfw_module_fmt *src_fmt,
1286                                 int pins)
1287 {
1288         int i;
1289
1290         for (i = 0; i < pins; i++) {
1291                 dst_fmt[i].channels  = src_fmt[i].channels;
1292                 dst_fmt[i].s_freq = src_fmt[i].freq;
1293                 dst_fmt[i].bit_depth = src_fmt[i].bit_depth;
1294                 dst_fmt[i].valid_bit_depth = src_fmt[i].valid_bit_depth;
1295                 dst_fmt[i].ch_cfg = src_fmt[i].ch_cfg;
1296                 dst_fmt[i].ch_map = src_fmt[i].ch_map;
1297                 dst_fmt[i].interleaving_style = src_fmt[i].interleaving_style;
1298                 dst_fmt[i].sample_type = src_fmt[i].sample_type;
1299         }
1300 }
1301
1302 /*
1303  * Topology core widget load callback
1304  *
1305  * This is used to save the private data for each widget which gives
1306  * information to the driver about module and pipeline parameters which DSP
1307  * FW expects like ids, resource values, formats etc
1308  */
1309 static int skl_tplg_widget_load(struct snd_soc_component *cmpnt,
1310                                 struct snd_soc_dapm_widget *w,
1311                                 struct snd_soc_tplg_dapm_widget *tplg_w)
1312 {
1313         int ret;
1314         struct hdac_ext_bus *ebus = snd_soc_component_get_drvdata(cmpnt);
1315         struct skl *skl = ebus_to_skl(ebus);
1316         struct hdac_bus *bus = ebus_to_hbus(ebus);
1317         struct skl_module_cfg *mconfig;
1318         struct skl_pipe *pipe;
1319         struct skl_dfw_module *dfw_config =
1320                                 (struct skl_dfw_module *)tplg_w->priv.data;
1321
1322         if (!tplg_w->priv.size)
1323                 goto bind_event;
1324
1325         mconfig = devm_kzalloc(bus->dev, sizeof(*mconfig), GFP_KERNEL);
1326
1327         if (!mconfig)
1328                 return -ENOMEM;
1329
1330         w->priv = mconfig;
1331         mconfig->id.module_id = dfw_config->module_id;
1332         mconfig->id.instance_id = dfw_config->instance_id;
1333         mconfig->mcps = dfw_config->max_mcps;
1334         mconfig->ibs = dfw_config->ibs;
1335         mconfig->obs = dfw_config->obs;
1336         mconfig->core_id = dfw_config->core_id;
1337         mconfig->max_in_queue = dfw_config->max_in_queue;
1338         mconfig->max_out_queue = dfw_config->max_out_queue;
1339         mconfig->is_loadable = dfw_config->is_loadable;
1340         skl_tplg_fill_fmt(mconfig->in_fmt, dfw_config->in_fmt,
1341                                                 MODULE_MAX_IN_PINS);
1342         skl_tplg_fill_fmt(mconfig->out_fmt, dfw_config->out_fmt,
1343                                                 MODULE_MAX_OUT_PINS);
1344
1345         mconfig->params_fixup = dfw_config->params_fixup;
1346         mconfig->converter = dfw_config->converter;
1347         mconfig->m_type = dfw_config->module_type;
1348         mconfig->vbus_id = dfw_config->vbus_id;
1349         mconfig->mem_pages = dfw_config->mem_pages;
1350
1351         pipe = skl_tplg_add_pipe(bus->dev, skl, &dfw_config->pipe);
1352         if (pipe)
1353                 mconfig->pipe = pipe;
1354
1355         mconfig->dev_type = dfw_config->dev_type;
1356         mconfig->hw_conn_type = dfw_config->hw_conn_type;
1357         mconfig->time_slot = dfw_config->time_slot;
1358         mconfig->formats_config.caps_size = dfw_config->caps.caps_size;
1359
1360         if (dfw_config->is_loadable)
1361                 memcpy(mconfig->guid, dfw_config->uuid,
1362                                         ARRAY_SIZE(dfw_config->uuid));
1363
1364         mconfig->m_in_pin = devm_kzalloc(bus->dev, (mconfig->max_in_queue) *
1365                                                 sizeof(*mconfig->m_in_pin),
1366                                                 GFP_KERNEL);
1367         if (!mconfig->m_in_pin)
1368                 return -ENOMEM;
1369
1370         mconfig->m_out_pin = devm_kzalloc(bus->dev, (mconfig->max_out_queue) *
1371                                                 sizeof(*mconfig->m_out_pin),
1372                                                 GFP_KERNEL);
1373         if (!mconfig->m_out_pin)
1374                 return -ENOMEM;
1375
1376         skl_fill_module_pin_info(dfw_config->in_pin, mconfig->m_in_pin,
1377                                                 dfw_config->is_dynamic_in_pin,
1378                                                 mconfig->max_in_queue);
1379
1380         skl_fill_module_pin_info(dfw_config->out_pin, mconfig->m_out_pin,
1381                                                  dfw_config->is_dynamic_out_pin,
1382                                                         mconfig->max_out_queue);
1383
1384
1385         if (mconfig->formats_config.caps_size == 0)
1386                 goto bind_event;
1387
1388         mconfig->formats_config.caps = (u32 *)devm_kzalloc(bus->dev,
1389                         mconfig->formats_config.caps_size, GFP_KERNEL);
1390
1391         if (mconfig->formats_config.caps == NULL)
1392                 return -ENOMEM;
1393
1394         memcpy(mconfig->formats_config.caps, dfw_config->caps.caps,
1395                                                  dfw_config->caps.caps_size);
1396         mconfig->formats_config.param_id = dfw_config->caps.param_id;
1397         mconfig->formats_config.set_params = dfw_config->caps.set_params;
1398
1399 bind_event:
1400         if (tplg_w->event_type == 0) {
1401                 dev_dbg(bus->dev, "ASoC: No event handler required\n");
1402                 return 0;
1403         }
1404
1405         ret = snd_soc_tplg_widget_bind_event(w, skl_tplg_widget_ops,
1406                                         ARRAY_SIZE(skl_tplg_widget_ops),
1407                                         tplg_w->event_type);
1408
1409         if (ret) {
1410                 dev_err(bus->dev, "%s: No matching event handlers found for %d\n",
1411                                         __func__, tplg_w->event_type);
1412                 return -EINVAL;
1413         }
1414
1415         return 0;
1416 }
1417
1418 static int skl_init_algo_data(struct device *dev, struct soc_bytes_ext *be,
1419                                         struct snd_soc_tplg_bytes_control *bc)
1420 {
1421         struct skl_algo_data *ac;
1422         struct skl_dfw_algo_data *dfw_ac =
1423                                 (struct skl_dfw_algo_data *)bc->priv.data;
1424
1425         ac = devm_kzalloc(dev, sizeof(*ac), GFP_KERNEL);
1426         if (!ac)
1427                 return -ENOMEM;
1428
1429         /* Fill private data */
1430         ac->max = dfw_ac->max;
1431         ac->param_id = dfw_ac->param_id;
1432         ac->set_params = dfw_ac->set_params;
1433
1434         if (ac->max) {
1435                 ac->params = (char *) devm_kzalloc(dev, ac->max, GFP_KERNEL);
1436                 if (!ac->params)
1437                         return -ENOMEM;
1438
1439                 if (dfw_ac->params)
1440                         memcpy(ac->params, dfw_ac->params, ac->max);
1441         }
1442
1443         be->dobj.private  = ac;
1444         return 0;
1445 }
1446
1447 static int skl_tplg_control_load(struct snd_soc_component *cmpnt,
1448                                 struct snd_kcontrol_new *kctl,
1449                                 struct snd_soc_tplg_ctl_hdr *hdr)
1450 {
1451         struct soc_bytes_ext *sb;
1452         struct snd_soc_tplg_bytes_control *tplg_bc;
1453         struct hdac_ext_bus *ebus  = snd_soc_component_get_drvdata(cmpnt);
1454         struct hdac_bus *bus = ebus_to_hbus(ebus);
1455
1456         switch (hdr->ops.info) {
1457         case SND_SOC_TPLG_CTL_BYTES:
1458                 tplg_bc = container_of(hdr,
1459                                 struct snd_soc_tplg_bytes_control, hdr);
1460                 if (kctl->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1461                         sb = (struct soc_bytes_ext *)kctl->private_value;
1462                         if (tplg_bc->priv.size)
1463                                 return skl_init_algo_data(
1464                                                 bus->dev, sb, tplg_bc);
1465                 }
1466                 break;
1467
1468         default:
1469                 dev_warn(bus->dev, "Control load not supported %d:%d:%d\n",
1470                         hdr->ops.get, hdr->ops.put, hdr->ops.info);
1471                 break;
1472         }
1473
1474         return 0;
1475 }
1476
1477 static struct snd_soc_tplg_ops skl_tplg_ops  = {
1478         .widget_load = skl_tplg_widget_load,
1479         .control_load = skl_tplg_control_load,
1480         .bytes_ext_ops = skl_tlv_ops,
1481         .bytes_ext_ops_count = ARRAY_SIZE(skl_tlv_ops),
1482 };
1483
1484 /* This will be read from topology manifest, currently defined here */
1485 #define SKL_MAX_MCPS 30000000
1486 #define SKL_FW_MAX_MEM 1000000
1487
1488 /*
1489  * SKL topology init routine
1490  */
1491 int skl_tplg_init(struct snd_soc_platform *platform, struct hdac_ext_bus *ebus)
1492 {
1493         int ret;
1494         const struct firmware *fw;
1495         struct hdac_bus *bus = ebus_to_hbus(ebus);
1496         struct skl *skl = ebus_to_skl(ebus);
1497
1498         ret = request_firmware(&fw, "dfw_sst.bin", bus->dev);
1499         if (ret < 0) {
1500                 dev_err(bus->dev, "tplg fw %s load failed with %d\n",
1501                                 "dfw_sst.bin", ret);
1502                 return ret;
1503         }
1504
1505         /*
1506          * The complete tplg for SKL is loaded as index 0, we don't use
1507          * any other index
1508          */
1509         ret = snd_soc_tplg_component_load(&platform->component,
1510                                         &skl_tplg_ops, fw, 0);
1511         if (ret < 0) {
1512                 dev_err(bus->dev, "tplg component load failed%d\n", ret);
1513                 return -EINVAL;
1514         }
1515
1516         skl->resource.max_mcps = SKL_MAX_MCPS;
1517         skl->resource.max_mem = SKL_FW_MAX_MEM;
1518
1519         skl->tplg = fw;
1520
1521         return 0;
1522 }