ASoC: Intel: Skylake: Add support for Mic Select module
[deliverable/linux.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
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 */
62static 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 */
91static 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 */
113static void
114skl_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 */
122static void
123skl_tplg_free_pipe_mem(struct skl *skl, struct skl_module_cfg *mconfig)
124{
125 skl->resource.mem -= mconfig->pipe->memory_pages;
126}
127
f7590d4f
JK
128
129static 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");
4cd9899f
HS
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);
f7590d4f 138 dev_dbg(ctx->dev, "Output Format:\n");
4cd9899f
HS
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);
f7590d4f
JK
143}
144
145static 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;
98256f83
JK
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
f7590d4f
JK
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 */
184static 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
4cd9899f
HS
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];
f7590d4f
JK
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 */
225static void skl_tplg_update_buffer_size(struct skl_sst *ctx,
226 struct skl_module_cfg *mcfg)
227{
228 int multiplier = 1;
4cd9899f
HS
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];
f7590d4f
JK
237
238 if (mcfg->m_type == SKL_MODULE_TYPE_SRCINT)
239 multiplier = 5;
4cd9899f
HS
240 mcfg->ibs = (in_fmt->s_freq / 1000) *
241 (mcfg->in_fmt->channels) *
242 (mcfg->in_fmt->bit_depth >> 3) *
f7590d4f
JK
243 multiplier;
244
4cd9899f
HS
245 mcfg->obs = (mcfg->out_fmt->s_freq / 1000) *
246 (mcfg->out_fmt->channels) *
247 (mcfg->out_fmt->bit_depth >> 3) *
f7590d4f
JK
248 multiplier;
249}
250
251static 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
e4e2d2f4
JK
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 */
287static 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
abb74003
JK
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 */
323static 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) {
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) {
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 */
368static 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)
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
e4e2d2f4
JK
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 */
400static int
401skl_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
6c5768b3
D
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
f7590d4f
JK
424 /*
425 * apply fix/conversion to module params based on
426 * FE/BE params
427 */
428 skl_tplg_update_module_params(w, ctx);
abb74003
JK
429
430 skl_tplg_set_module_init_data(w);
9939a9c3 431 ret = skl_init_module(ctx, mconfig);
e4e2d2f4
JK
432 if (ret < 0)
433 return ret;
abb74003
JK
434
435 ret = skl_tplg_set_module_params(w, ctx);
436 if (ret < 0)
437 return ret;
e4e2d2f4
JK
438 }
439
440 return 0;
441}
d93f8e55 442
6c5768b3
D
443static 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
d93f8e55
VK
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 */
469static 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
8724ff17
JK
532static int skl_tplg_bind_sinks(struct snd_soc_dapm_widget *w,
533 struct skl *skl,
534 struct skl_module_cfg *src_mconfig)
d93f8e55
VK
535{
536 struct snd_soc_dapm_path *p;
0ed95d76 537 struct snd_soc_dapm_widget *sink = NULL, *next_sink = NULL;
8724ff17 538 struct skl_module_cfg *sink_mconfig;
d93f8e55 539 struct skl_sst *ctx = skl->skl_sst;
8724ff17 540 int ret;
d93f8e55 541
8724ff17 542 snd_soc_dapm_widget_for_each_sink_path(w, p) {
d93f8e55
VK
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
0ed95d76 549 next_sink = p->sink;
d93f8e55
VK
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;
d93f8e55
VK
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) {
d1730c3d
JK
568 if (sink_mconfig->pipe->conn_type !=
569 SKL_PIPE_CONN_TYPE_FE)
570 ret = skl_run_pipe(ctx,
571 sink_mconfig->pipe);
d93f8e55
VK
572 if (ret)
573 return ret;
574 }
d93f8e55
VK
575 }
576 }
577
8724ff17 578 if (!sink)
0ed95d76 579 return skl_tplg_bind_sinks(next_sink, skl, src_mconfig);
8724ff17
JK
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 */
594static 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
d93f8e55 612 /* Start source pipe last after starting all sinks */
d1730c3d
JK
613 if (src_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
614 return skl_run_pipe(ctx, src_mconfig->pipe);
d93f8e55
VK
615
616 return 0;
617}
618
8724ff17
JK
619static 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
d93f8e55
VK
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 */
660static int skl_tplg_mixer_dapm_post_pmu_event(struct snd_soc_dapm_widget *w,
661 struct skl *skl)
662{
663 int ret = 0;
d93f8e55
VK
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 */
8724ff17
JK
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;
d93f8e55
VK
682
683 /*
8724ff17
JK
684 * check pipe state, then no need to bind or start the
685 * pipe
d93f8e55 686 */
8724ff17
JK
687 if (src_mconfig->pipe->state != SKL_PIPE_STARTED)
688 src_pipe_started = 0;
d93f8e55
VK
689 }
690
691 if (src_pipe_started) {
692 ret = skl_bind_modules(ctx, src_mconfig, sink_mconfig);
693 if (ret)
694 return ret;
695
d1730c3d
JK
696 if (sink_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
697 ret = skl_run_pipe(ctx, sink_mconfig->pipe);
d93f8e55
VK
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 */
709static int skl_tplg_mixer_dapm_pre_pmd_event(struct snd_soc_dapm_widget *w,
710 struct skl *skl)
711{
d93f8e55 712 struct skl_module_cfg *src_mconfig, *sink_mconfig;
ce1b5551 713 int ret = 0, i;
d93f8e55
VK
714 struct skl_sst *ctx = skl->skl_sst;
715
ce1b5551 716 sink_mconfig = w->priv;
d93f8e55
VK
717
718 /* Stop the pipe */
719 ret = skl_stop_pipe(ctx, sink_mconfig->pipe);
720 if (ret)
721 return ret;
722
ce1b5551
JK
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;
d93f8e55 737
ce1b5551
JK
738 ret = skl_unbind_modules(ctx,
739 src_mconfig, sink_mconfig);
d93f8e55
VK
740 }
741 }
742
d93f8e55
VK
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 */
754static 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);
65976878 765 skl_tplg_free_pipe_mem(skl, mconfig);
d93f8e55
VK
766
767 list_for_each_entry(w_module, &s_pipe->w_list, node) {
768 dst_module = w_module->w->priv;
769
7ae3cb15 770 skl_tplg_free_pipe_mcps(skl, dst_module);
d93f8e55
VK
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);
d93f8e55 784
6c5768b3 785 return skl_tplg_unload_pipe_modules(ctx, s_pipe);
d93f8e55
VK
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 */
794static int skl_tplg_pga_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
795 struct skl *skl)
796{
d93f8e55 797 struct skl_module_cfg *src_mconfig, *sink_mconfig;
ce1b5551 798 int ret = 0, i;
d93f8e55
VK
799 struct skl_sst *ctx = skl->skl_sst;
800
ce1b5551 801 src_mconfig = w->priv;
d93f8e55 802
d93f8e55
VK
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
ce1b5551
JK
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);
d93f8e55
VK
822 }
823 }
824
d93f8e55
VK
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 */
835static 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 */
858static 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 */
887static 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}
cfb0a873 904
140adfba
JK
905static 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
912 if (bc->params) {
913 if (copy_to_user(data, &bc->param_id, sizeof(u32)))
914 return -EFAULT;
915 if (copy_to_user(data + sizeof(u32), &size, sizeof(u32)))
916 return -EFAULT;
917 if (copy_to_user(data + 2 * sizeof(u32), bc->params, size))
918 return -EFAULT;
919 }
920
921 return 0;
922}
923
924#define SKL_PARAM_VENDOR_ID 0xff
925
926static int skl_tplg_tlv_control_set(struct snd_kcontrol *kcontrol,
927 const unsigned int __user *data, unsigned int size)
928{
929 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
930 struct skl_module_cfg *mconfig = w->priv;
931 struct soc_bytes_ext *sb =
932 (struct soc_bytes_ext *)kcontrol->private_value;
933 struct skl_algo_data *ac = (struct skl_algo_data *)sb->dobj.private;
934 struct skl *skl = get_skl_ctx(w->dapm->dev);
935
936 if (ac->params) {
937 /*
938 * if the param_is is of type Vendor, firmware expects actual
939 * parameter id and size from the control.
940 */
941 if (ac->param_id == SKL_PARAM_VENDOR_ID) {
942 if (copy_from_user(ac->params, data, size))
943 return -EFAULT;
944 } else {
945 if (copy_from_user(ac->params,
946 data + 2 * sizeof(u32), size))
947 return -EFAULT;
948 }
949
950 if (w->power)
951 return skl_set_module_params(skl->skl_sst,
952 (u32 *)ac->params, ac->max,
953 ac->param_id, mconfig);
954 }
955
956 return 0;
957}
958
cfb0a873
VK
959/*
960 * The FE params are passed by hw_params of the DAI.
961 * On hw_params, the params are stored in Gateway module of the FE and we
962 * need to calculate the format in DSP module configuration, that
963 * conversion is done here
964 */
965int skl_tplg_update_pipe_params(struct device *dev,
966 struct skl_module_cfg *mconfig,
967 struct skl_pipe_params *params)
968{
969 struct skl_pipe *pipe = mconfig->pipe;
970 struct skl_module_fmt *format = NULL;
971
972 memcpy(pipe->p_params, params, sizeof(*params));
973
974 if (params->stream == SNDRV_PCM_STREAM_PLAYBACK)
4cd9899f 975 format = &mconfig->in_fmt[0];
cfb0a873 976 else
4cd9899f 977 format = &mconfig->out_fmt[0];
cfb0a873
VK
978
979 /* set the hw_params */
980 format->s_freq = params->s_freq;
981 format->channels = params->ch;
982 format->valid_bit_depth = skl_get_bit_depth(params->s_fmt);
983
984 /*
985 * 16 bit is 16 bit container whereas 24 bit is in 32 bit
986 * container so update bit depth accordingly
987 */
988 switch (format->valid_bit_depth) {
989 case SKL_DEPTH_16BIT:
990 format->bit_depth = format->valid_bit_depth;
991 break;
992
993 case SKL_DEPTH_24BIT:
6654f39e 994 case SKL_DEPTH_32BIT:
cfb0a873
VK
995 format->bit_depth = SKL_DEPTH_32BIT;
996 break;
997
998 default:
999 dev_err(dev, "Invalid bit depth %x for pipe\n",
1000 format->valid_bit_depth);
1001 return -EINVAL;
1002 }
1003
1004 if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1005 mconfig->ibs = (format->s_freq / 1000) *
1006 (format->channels) *
1007 (format->bit_depth >> 3);
1008 } else {
1009 mconfig->obs = (format->s_freq / 1000) *
1010 (format->channels) *
1011 (format->bit_depth >> 3);
1012 }
1013
1014 return 0;
1015}
1016
1017/*
1018 * Query the module config for the FE DAI
1019 * This is used to find the hw_params set for that DAI and apply to FE
1020 * pipeline
1021 */
1022struct skl_module_cfg *
1023skl_tplg_fe_get_cpr_module(struct snd_soc_dai *dai, int stream)
1024{
1025 struct snd_soc_dapm_widget *w;
1026 struct snd_soc_dapm_path *p = NULL;
1027
1028 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1029 w = dai->playback_widget;
f0900eb2 1030 snd_soc_dapm_widget_for_each_sink_path(w, p) {
cfb0a873 1031 if (p->connect && p->sink->power &&
a28f51db 1032 !is_skl_dsp_widget_type(p->sink))
cfb0a873
VK
1033 continue;
1034
1035 if (p->sink->priv) {
1036 dev_dbg(dai->dev, "set params for %s\n",
1037 p->sink->name);
1038 return p->sink->priv;
1039 }
1040 }
1041 } else {
1042 w = dai->capture_widget;
f0900eb2 1043 snd_soc_dapm_widget_for_each_source_path(w, p) {
cfb0a873 1044 if (p->connect && p->source->power &&
a28f51db 1045 !is_skl_dsp_widget_type(p->source))
cfb0a873
VK
1046 continue;
1047
1048 if (p->source->priv) {
1049 dev_dbg(dai->dev, "set params for %s\n",
1050 p->source->name);
1051 return p->source->priv;
1052 }
1053 }
1054 }
1055
1056 return NULL;
1057}
1058
1059static u8 skl_tplg_be_link_type(int dev_type)
1060{
1061 int ret;
1062
1063 switch (dev_type) {
1064 case SKL_DEVICE_BT:
1065 ret = NHLT_LINK_SSP;
1066 break;
1067
1068 case SKL_DEVICE_DMIC:
1069 ret = NHLT_LINK_DMIC;
1070 break;
1071
1072 case SKL_DEVICE_I2S:
1073 ret = NHLT_LINK_SSP;
1074 break;
1075
1076 case SKL_DEVICE_HDALINK:
1077 ret = NHLT_LINK_HDA;
1078 break;
1079
1080 default:
1081 ret = NHLT_LINK_INVALID;
1082 break;
1083 }
1084
1085 return ret;
1086}
1087
1088/*
1089 * Fill the BE gateway parameters
1090 * The BE gateway expects a blob of parameters which are kept in the ACPI
1091 * NHLT blob, so query the blob for interface type (i2s/pdm) and instance.
1092 * The port can have multiple settings so pick based on the PCM
1093 * parameters
1094 */
1095static int skl_tplg_be_fill_pipe_params(struct snd_soc_dai *dai,
1096 struct skl_module_cfg *mconfig,
1097 struct skl_pipe_params *params)
1098{
1099 struct skl_pipe *pipe = mconfig->pipe;
1100 struct nhlt_specific_cfg *cfg;
1101 struct skl *skl = get_skl_ctx(dai->dev);
1102 int link_type = skl_tplg_be_link_type(mconfig->dev_type);
1103
1104 memcpy(pipe->p_params, params, sizeof(*params));
1105
b30c275e
JK
1106 if (link_type == NHLT_LINK_HDA)
1107 return 0;
1108
cfb0a873
VK
1109 /* update the blob based on virtual bus_id*/
1110 cfg = skl_get_ep_blob(skl, mconfig->vbus_id, link_type,
1111 params->s_fmt, params->ch,
1112 params->s_freq, params->stream);
1113 if (cfg) {
1114 mconfig->formats_config.caps_size = cfg->size;
bc03281a 1115 mconfig->formats_config.caps = (u32 *) &cfg->caps;
cfb0a873
VK
1116 } else {
1117 dev_err(dai->dev, "Blob NULL for id %x type %d dirn %d\n",
1118 mconfig->vbus_id, link_type,
1119 params->stream);
1120 dev_err(dai->dev, "PCM: ch %d, freq %d, fmt %d\n",
1121 params->ch, params->s_freq, params->s_fmt);
1122 return -EINVAL;
1123 }
1124
1125 return 0;
1126}
1127
1128static int skl_tplg_be_set_src_pipe_params(struct snd_soc_dai *dai,
1129 struct snd_soc_dapm_widget *w,
1130 struct skl_pipe_params *params)
1131{
1132 struct snd_soc_dapm_path *p;
4d8adccb 1133 int ret = -EIO;
cfb0a873 1134
f0900eb2 1135 snd_soc_dapm_widget_for_each_source_path(w, p) {
cfb0a873
VK
1136 if (p->connect && is_skl_dsp_widget_type(p->source) &&
1137 p->source->priv) {
1138
9a03cb49
JK
1139 ret = skl_tplg_be_fill_pipe_params(dai,
1140 p->source->priv, params);
1141 if (ret < 0)
1142 return ret;
cfb0a873 1143 } else {
9a03cb49
JK
1144 ret = skl_tplg_be_set_src_pipe_params(dai,
1145 p->source, params);
4d8adccb
SP
1146 if (ret < 0)
1147 return ret;
cfb0a873
VK
1148 }
1149 }
1150
4d8adccb 1151 return ret;
cfb0a873
VK
1152}
1153
1154static int skl_tplg_be_set_sink_pipe_params(struct snd_soc_dai *dai,
1155 struct snd_soc_dapm_widget *w, struct skl_pipe_params *params)
1156{
1157 struct snd_soc_dapm_path *p = NULL;
4d8adccb 1158 int ret = -EIO;
cfb0a873 1159
f0900eb2 1160 snd_soc_dapm_widget_for_each_sink_path(w, p) {
cfb0a873
VK
1161 if (p->connect && is_skl_dsp_widget_type(p->sink) &&
1162 p->sink->priv) {
1163
9a03cb49
JK
1164 ret = skl_tplg_be_fill_pipe_params(dai,
1165 p->sink->priv, params);
1166 if (ret < 0)
1167 return ret;
cfb0a873 1168 } else {
4d8adccb 1169 ret = skl_tplg_be_set_sink_pipe_params(
cfb0a873 1170 dai, p->sink, params);
4d8adccb
SP
1171 if (ret < 0)
1172 return ret;
cfb0a873
VK
1173 }
1174 }
1175
4d8adccb 1176 return ret;
cfb0a873
VK
1177}
1178
1179/*
1180 * BE hw_params can be a source parameters (capture) or sink parameters
1181 * (playback). Based on sink and source we need to either find the source
1182 * list or the sink list and set the pipeline parameters
1183 */
1184int skl_tplg_be_update_params(struct snd_soc_dai *dai,
1185 struct skl_pipe_params *params)
1186{
1187 struct snd_soc_dapm_widget *w;
1188
1189 if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1190 w = dai->playback_widget;
1191
1192 return skl_tplg_be_set_src_pipe_params(dai, w, params);
1193
1194 } else {
1195 w = dai->capture_widget;
1196
1197 return skl_tplg_be_set_sink_pipe_params(dai, w, params);
1198 }
1199
1200 return 0;
1201}
3af36706
VK
1202
1203static const struct snd_soc_tplg_widget_events skl_tplg_widget_ops[] = {
1204 {SKL_MIXER_EVENT, skl_tplg_mixer_event},
1205 {SKL_VMIXER_EVENT, skl_tplg_vmixer_event},
1206 {SKL_PGA_EVENT, skl_tplg_pga_event},
1207};
1208
140adfba
JK
1209static const struct snd_soc_tplg_bytes_ext_ops skl_tlv_ops[] = {
1210 {SKL_CONTROL_TYPE_BYTE_TLV, skl_tplg_tlv_control_get,
1211 skl_tplg_tlv_control_set},
1212};
1213
3af36706
VK
1214/*
1215 * The topology binary passes the pin info for a module so initialize the pin
1216 * info passed into module instance
1217 */
6abca1d7
JK
1218static void skl_fill_module_pin_info(struct skl_dfw_module_pin *dfw_pin,
1219 struct skl_module_pin *m_pin,
1220 bool is_dynamic, int max_pin)
3af36706
VK
1221{
1222 int i;
1223
1224 for (i = 0; i < max_pin; i++) {
6abca1d7
JK
1225 m_pin[i].id.module_id = dfw_pin[i].module_id;
1226 m_pin[i].id.instance_id = dfw_pin[i].instance_id;
3af36706 1227 m_pin[i].in_use = false;
6abca1d7 1228 m_pin[i].is_dynamic = is_dynamic;
4f745708 1229 m_pin[i].pin_state = SKL_PIN_UNBIND;
3af36706
VK
1230 }
1231}
1232
1233/*
1234 * Add pipeline from topology binary into driver pipeline list
1235 *
1236 * If already added we return that instance
1237 * Otherwise we create a new instance and add into driver list
1238 */
1239static struct skl_pipe *skl_tplg_add_pipe(struct device *dev,
1240 struct skl *skl, struct skl_dfw_pipe *dfw_pipe)
1241{
1242 struct skl_pipeline *ppl;
1243 struct skl_pipe *pipe;
1244 struct skl_pipe_params *params;
1245
1246 list_for_each_entry(ppl, &skl->ppl_list, node) {
1247 if (ppl->pipe->ppl_id == dfw_pipe->pipe_id)
1248 return ppl->pipe;
1249 }
1250
1251 ppl = devm_kzalloc(dev, sizeof(*ppl), GFP_KERNEL);
1252 if (!ppl)
1253 return NULL;
1254
1255 pipe = devm_kzalloc(dev, sizeof(*pipe), GFP_KERNEL);
1256 if (!pipe)
1257 return NULL;
1258
1259 params = devm_kzalloc(dev, sizeof(*params), GFP_KERNEL);
1260 if (!params)
1261 return NULL;
1262
1263 pipe->ppl_id = dfw_pipe->pipe_id;
1264 pipe->memory_pages = dfw_pipe->memory_pages;
1265 pipe->pipe_priority = dfw_pipe->pipe_priority;
1266 pipe->conn_type = dfw_pipe->conn_type;
1267 pipe->state = SKL_PIPE_INVALID;
1268 pipe->p_params = params;
1269 INIT_LIST_HEAD(&pipe->w_list);
1270
1271 ppl->pipe = pipe;
1272 list_add(&ppl->node, &skl->ppl_list);
1273
1274 return ppl->pipe;
1275}
1276
4cd9899f
HS
1277static void skl_tplg_fill_fmt(struct skl_module_fmt *dst_fmt,
1278 struct skl_dfw_module_fmt *src_fmt,
1279 int pins)
1280{
1281 int i;
1282
1283 for (i = 0; i < pins; i++) {
1284 dst_fmt[i].channels = src_fmt[i].channels;
1285 dst_fmt[i].s_freq = src_fmt[i].freq;
1286 dst_fmt[i].bit_depth = src_fmt[i].bit_depth;
1287 dst_fmt[i].valid_bit_depth = src_fmt[i].valid_bit_depth;
1288 dst_fmt[i].ch_cfg = src_fmt[i].ch_cfg;
1289 dst_fmt[i].ch_map = src_fmt[i].ch_map;
1290 dst_fmt[i].interleaving_style = src_fmt[i].interleaving_style;
1291 dst_fmt[i].sample_type = src_fmt[i].sample_type;
1292 }
1293}
1294
3af36706
VK
1295/*
1296 * Topology core widget load callback
1297 *
1298 * This is used to save the private data for each widget which gives
1299 * information to the driver about module and pipeline parameters which DSP
1300 * FW expects like ids, resource values, formats etc
1301 */
1302static int skl_tplg_widget_load(struct snd_soc_component *cmpnt,
b663a8c5
JK
1303 struct snd_soc_dapm_widget *w,
1304 struct snd_soc_tplg_dapm_widget *tplg_w)
3af36706
VK
1305{
1306 int ret;
1307 struct hdac_ext_bus *ebus = snd_soc_component_get_drvdata(cmpnt);
1308 struct skl *skl = ebus_to_skl(ebus);
1309 struct hdac_bus *bus = ebus_to_hbus(ebus);
1310 struct skl_module_cfg *mconfig;
1311 struct skl_pipe *pipe;
b663a8c5
JK
1312 struct skl_dfw_module *dfw_config =
1313 (struct skl_dfw_module *)tplg_w->priv.data;
3af36706
VK
1314
1315 if (!tplg_w->priv.size)
1316 goto bind_event;
1317
1318 mconfig = devm_kzalloc(bus->dev, sizeof(*mconfig), GFP_KERNEL);
1319
1320 if (!mconfig)
1321 return -ENOMEM;
1322
1323 w->priv = mconfig;
1324 mconfig->id.module_id = dfw_config->module_id;
1325 mconfig->id.instance_id = dfw_config->instance_id;
1326 mconfig->mcps = dfw_config->max_mcps;
1327 mconfig->ibs = dfw_config->ibs;
1328 mconfig->obs = dfw_config->obs;
1329 mconfig->core_id = dfw_config->core_id;
1330 mconfig->max_in_queue = dfw_config->max_in_queue;
1331 mconfig->max_out_queue = dfw_config->max_out_queue;
1332 mconfig->is_loadable = dfw_config->is_loadable;
4cd9899f
HS
1333 skl_tplg_fill_fmt(mconfig->in_fmt, dfw_config->in_fmt,
1334 MODULE_MAX_IN_PINS);
1335 skl_tplg_fill_fmt(mconfig->out_fmt, dfw_config->out_fmt,
1336 MODULE_MAX_OUT_PINS);
1337
3af36706
VK
1338 mconfig->params_fixup = dfw_config->params_fixup;
1339 mconfig->converter = dfw_config->converter;
1340 mconfig->m_type = dfw_config->module_type;
1341 mconfig->vbus_id = dfw_config->vbus_id;
b18c458d 1342 mconfig->mem_pages = dfw_config->mem_pages;
3af36706
VK
1343
1344 pipe = skl_tplg_add_pipe(bus->dev, skl, &dfw_config->pipe);
1345 if (pipe)
1346 mconfig->pipe = pipe;
1347
1348 mconfig->dev_type = dfw_config->dev_type;
1349 mconfig->hw_conn_type = dfw_config->hw_conn_type;
1350 mconfig->time_slot = dfw_config->time_slot;
1351 mconfig->formats_config.caps_size = dfw_config->caps.caps_size;
1352
65aecfa8
HS
1353 if (dfw_config->is_loadable)
1354 memcpy(mconfig->guid, dfw_config->uuid,
1355 ARRAY_SIZE(dfw_config->uuid));
1356
4cd9899f
HS
1357 mconfig->m_in_pin = devm_kzalloc(bus->dev, (mconfig->max_in_queue) *
1358 sizeof(*mconfig->m_in_pin),
1359 GFP_KERNEL);
3af36706
VK
1360 if (!mconfig->m_in_pin)
1361 return -ENOMEM;
1362
6abca1d7
JK
1363 mconfig->m_out_pin = devm_kzalloc(bus->dev, (mconfig->max_out_queue) *
1364 sizeof(*mconfig->m_out_pin),
1365 GFP_KERNEL);
3af36706
VK
1366 if (!mconfig->m_out_pin)
1367 return -ENOMEM;
1368
6abca1d7
JK
1369 skl_fill_module_pin_info(dfw_config->in_pin, mconfig->m_in_pin,
1370 dfw_config->is_dynamic_in_pin,
1371 mconfig->max_in_queue);
1372
1373 skl_fill_module_pin_info(dfw_config->out_pin, mconfig->m_out_pin,
1374 dfw_config->is_dynamic_out_pin,
1375 mconfig->max_out_queue);
1376
3af36706
VK
1377
1378 if (mconfig->formats_config.caps_size == 0)
1379 goto bind_event;
1380
1381 mconfig->formats_config.caps = (u32 *)devm_kzalloc(bus->dev,
b663a8c5 1382 mconfig->formats_config.caps_size, GFP_KERNEL);
3af36706
VK
1383
1384 if (mconfig->formats_config.caps == NULL)
1385 return -ENOMEM;
1386
1387 memcpy(mconfig->formats_config.caps, dfw_config->caps.caps,
abb74003
JK
1388 dfw_config->caps.caps_size);
1389 mconfig->formats_config.param_id = dfw_config->caps.param_id;
1390 mconfig->formats_config.set_params = dfw_config->caps.set_params;
3af36706
VK
1391
1392bind_event:
1393 if (tplg_w->event_type == 0) {
3373f716 1394 dev_dbg(bus->dev, "ASoC: No event handler required\n");
3af36706
VK
1395 return 0;
1396 }
1397
1398 ret = snd_soc_tplg_widget_bind_event(w, skl_tplg_widget_ops,
b663a8c5
JK
1399 ARRAY_SIZE(skl_tplg_widget_ops),
1400 tplg_w->event_type);
3af36706
VK
1401
1402 if (ret) {
1403 dev_err(bus->dev, "%s: No matching event handlers found for %d\n",
1404 __func__, tplg_w->event_type);
1405 return -EINVAL;
1406 }
1407
1408 return 0;
1409}
1410
140adfba
JK
1411static int skl_init_algo_data(struct device *dev, struct soc_bytes_ext *be,
1412 struct snd_soc_tplg_bytes_control *bc)
1413{
1414 struct skl_algo_data *ac;
1415 struct skl_dfw_algo_data *dfw_ac =
1416 (struct skl_dfw_algo_data *)bc->priv.data;
1417
1418 ac = devm_kzalloc(dev, sizeof(*ac), GFP_KERNEL);
1419 if (!ac)
1420 return -ENOMEM;
1421
1422 /* Fill private data */
1423 ac->max = dfw_ac->max;
1424 ac->param_id = dfw_ac->param_id;
1425 ac->set_params = dfw_ac->set_params;
1426
1427 if (ac->max) {
1428 ac->params = (char *) devm_kzalloc(dev, ac->max, GFP_KERNEL);
1429 if (!ac->params)
1430 return -ENOMEM;
1431
1432 if (dfw_ac->params)
1433 memcpy(ac->params, dfw_ac->params, ac->max);
1434 }
1435
1436 be->dobj.private = ac;
1437 return 0;
1438}
1439
1440static int skl_tplg_control_load(struct snd_soc_component *cmpnt,
1441 struct snd_kcontrol_new *kctl,
1442 struct snd_soc_tplg_ctl_hdr *hdr)
1443{
1444 struct soc_bytes_ext *sb;
1445 struct snd_soc_tplg_bytes_control *tplg_bc;
1446 struct hdac_ext_bus *ebus = snd_soc_component_get_drvdata(cmpnt);
1447 struct hdac_bus *bus = ebus_to_hbus(ebus);
1448
1449 switch (hdr->ops.info) {
1450 case SND_SOC_TPLG_CTL_BYTES:
1451 tplg_bc = container_of(hdr,
1452 struct snd_soc_tplg_bytes_control, hdr);
1453 if (kctl->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1454 sb = (struct soc_bytes_ext *)kctl->private_value;
1455 if (tplg_bc->priv.size)
1456 return skl_init_algo_data(
1457 bus->dev, sb, tplg_bc);
1458 }
1459 break;
1460
1461 default:
1462 dev_warn(bus->dev, "Control load not supported %d:%d:%d\n",
1463 hdr->ops.get, hdr->ops.put, hdr->ops.info);
1464 break;
1465 }
1466
1467 return 0;
1468}
1469
3af36706
VK
1470static struct snd_soc_tplg_ops skl_tplg_ops = {
1471 .widget_load = skl_tplg_widget_load,
140adfba
JK
1472 .control_load = skl_tplg_control_load,
1473 .bytes_ext_ops = skl_tlv_ops,
1474 .bytes_ext_ops_count = ARRAY_SIZE(skl_tlv_ops),
3af36706
VK
1475};
1476
1477/* This will be read from topology manifest, currently defined here */
1478#define SKL_MAX_MCPS 30000000
1479#define SKL_FW_MAX_MEM 1000000
1480
1481/*
1482 * SKL topology init routine
1483 */
1484int skl_tplg_init(struct snd_soc_platform *platform, struct hdac_ext_bus *ebus)
1485{
1486 int ret;
1487 const struct firmware *fw;
1488 struct hdac_bus *bus = ebus_to_hbus(ebus);
1489 struct skl *skl = ebus_to_skl(ebus);
1490
1491 ret = request_firmware(&fw, "dfw_sst.bin", bus->dev);
1492 if (ret < 0) {
b663a8c5 1493 dev_err(bus->dev, "tplg fw %s load failed with %d\n",
3af36706
VK
1494 "dfw_sst.bin", ret);
1495 return ret;
1496 }
1497
1498 /*
1499 * The complete tplg for SKL is loaded as index 0, we don't use
1500 * any other index
1501 */
b663a8c5
JK
1502 ret = snd_soc_tplg_component_load(&platform->component,
1503 &skl_tplg_ops, fw, 0);
3af36706
VK
1504 if (ret < 0) {
1505 dev_err(bus->dev, "tplg component load failed%d\n", ret);
1506 return -EINVAL;
1507 }
1508
1509 skl->resource.max_mcps = SKL_MAX_MCPS;
1510 skl->resource.max_mem = SKL_FW_MAX_MEM;
1511
1512 return 0;
1513}
This page took 0.097084 seconds and 5 git commands to generate.