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