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