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