blob: 67b1ab5019189788a5eff34a15a82c6d715957ac [file] [log] [blame]
Jeeja KPe4e2d2f2015-10-07 11:31:52 +01001/*
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"
Dharageswari R6c5768b2015-12-03 23:29:50 +053029#include "../common/sst-dsp.h"
30#include "../common/sst-dsp-priv.h"
Jeeja KPe4e2d2f2015-10-07 11:31:52 +010031
Jeeja KPf7590d42015-10-07 11:31:53 +010032#define SKL_CH_FIXUP_MASK (1 << 0)
33#define SKL_RATE_FIXUP_MASK (1 << 1)
34#define SKL_FMT_FIXUP_MASK (1 << 2)
35
Jeeja KPe4e2d2f2015-10-07 11:31:52 +010036/*
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
Dharageswari.R9ba8ffe2016-02-03 17:59:47 +053057 * from available pool.
Jeeja KPe4e2d2f2015-10-07 11:31:52 +010058 */
Dharageswari.R9ba8ffe2016-02-03 17:59:47 +053059static bool skl_is_pipe_mem_avail(struct skl *skl,
Jeeja KPe4e2d2f2015-10-07 11:31:52 +010060 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;
Dharageswari.R9ba8ffe2016-02-03 17:59:47 +053074 } else {
75 return true;
Jeeja KPe4e2d2f2015-10-07 11:31:52 +010076 }
Dharageswari.R9ba8ffe2016-02-03 17:59:47 +053077}
Jeeja KPe4e2d2f2015-10-07 11:31:52 +010078
Dharageswari.R9ba8ffe2016-02-03 17:59:47 +053079/*
80 * Add the mem to the mem pool. This is freed when pipe is deleted.
81 * Note: DSP does actual memory management we only keep track for complete
82 * pool
83 */
84static void skl_tplg_alloc_pipe_mem(struct skl *skl,
85 struct skl_module_cfg *mconfig)
86{
Jeeja KPe4e2d2f2015-10-07 11:31:52 +010087 skl->resource.mem += mconfig->pipe->memory_pages;
Jeeja KPe4e2d2f2015-10-07 11:31:52 +010088}
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
Dharageswari.R9ba8ffe2016-02-03 17:59:47 +053095 * pipe.
Jeeja KPe4e2d2f2015-10-07 11:31:52 +010096 */
Dharageswari.R9ba8ffe2016-02-03 17:59:47 +053097
98static bool skl_is_pipe_mcps_avail(struct skl *skl,
Jeeja KPe4e2d2f2015-10-07 11:31:52 +010099 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,
Guneshwor Singh7ca42f52016-02-03 17:59:46 +0530108 "exceeds ppl mcps available %d > mem %d\n",
Jeeja KPe4e2d2f2015-10-07 11:31:52 +0100109 skl->resource.max_mcps, skl->resource.mcps);
110 return false;
Dharageswari.R9ba8ffe2016-02-03 17:59:47 +0530111 } else {
112 return true;
Jeeja KPe4e2d2f2015-10-07 11:31:52 +0100113 }
Dharageswari.R9ba8ffe2016-02-03 17:59:47 +0530114}
Jeeja KPe4e2d2f2015-10-07 11:31:52 +0100115
Dharageswari.R9ba8ffe2016-02-03 17:59:47 +0530116static void skl_tplg_alloc_pipe_mcps(struct skl *skl,
117 struct skl_module_cfg *mconfig)
118{
Jeeja KPe4e2d2f2015-10-07 11:31:52 +0100119 skl->resource.mcps += mconfig->mcps;
Jeeja KPe4e2d2f2015-10-07 11:31:52 +0100120}
121
122/*
123 * Free the mcps when tearing down
124 */
125static void
126skl_tplg_free_pipe_mcps(struct skl *skl, struct skl_module_cfg *mconfig)
127{
128 skl->resource.mcps -= mconfig->mcps;
129}
130
131/*
132 * Free the memory when tearing down
133 */
134static void
135skl_tplg_free_pipe_mem(struct skl *skl, struct skl_module_cfg *mconfig)
136{
137 skl->resource.mem -= mconfig->pipe->memory_pages;
138}
139
Jeeja KPf7590d42015-10-07 11:31:53 +0100140
141static void skl_dump_mconfig(struct skl_sst *ctx,
142 struct skl_module_cfg *mcfg)
143{
144 dev_dbg(ctx->dev, "Dumping config\n");
145 dev_dbg(ctx->dev, "Input Format:\n");
Hardik T Shah4cd98992015-10-27 09:22:55 +0900146 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);
Jeeja KPf7590d42015-10-07 11:31:53 +0100150 dev_dbg(ctx->dev, "Output Format:\n");
Hardik T Shah4cd98992015-10-27 09:22:55 +0900151 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);
Jeeja KPf7590d42015-10-07 11:31:53 +0100155}
156
Subhransu S. Prustyea5a1372016-04-14 10:07:36 +0530157static 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
Jeeja KPf7590d42015-10-07 11:31:53 +0100174static 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;
Subhransu S. Prustyea5a1372016-04-14 10:07:36 +0530179 if (fixup & SKL_CH_FIXUP_MASK) {
Jeeja KPf7590d42015-10-07 11:31:53 +0100180 fmt->channels = params->ch;
Subhransu S. Prustyea5a1372016-04-14 10:07:36 +0530181 skl_tplg_update_chmap(fmt, fmt->channels);
182 }
Jeeja KP98256f82015-11-23 22:26:25 +0530183 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
Jeeja KPf7590d42015-10-07 11:31:53 +0100201}
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 */
215static 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
Hardik T Shah4cd98992015-10-27 09:22:55 +0900221 /* Fixups will be applied to pin 0 only */
222 in_fmt = &m_cfg->in_fmt[0];
223 out_fmt = &m_cfg->out_fmt[0];
Jeeja KPf7590d42015-10-07 11:31:53 +0100224
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 */
256static void skl_tplg_update_buffer_size(struct skl_sst *ctx,
257 struct skl_module_cfg *mcfg)
258{
259 int multiplier = 1;
Hardik T Shah4cd98992015-10-27 09:22:55 +0900260 struct skl_module_fmt *in_fmt, *out_fmt;
Subhransu S. Prustyf0c8e1d2016-04-12 10:31:23 +0530261 int in_rate, out_rate;
Hardik T Shah4cd98992015-10-27 09:22:55 +0900262
263
264 /* Since fixups is applied to pin 0 only, ibs, obs needs
265 * change for pin 0 only
266 */
267 in_fmt = &mcfg->in_fmt[0];
268 out_fmt = &mcfg->out_fmt[0];
Jeeja KPf7590d42015-10-07 11:31:53 +0100269
270 if (mcfg->m_type == SKL_MODULE_TYPE_SRCINT)
271 multiplier = 5;
Jeeja KPf7590d42015-10-07 11:31:53 +0100272
Subhransu S. Prustyf0c8e1d2016-04-12 10:31:23 +0530273 if (in_fmt->s_freq % 1000)
274 in_rate = (in_fmt->s_freq / 1000) + 1;
275 else
276 in_rate = (in_fmt->s_freq / 1000);
277
278 mcfg->ibs = in_rate * (mcfg->in_fmt->channels) *
279 (mcfg->in_fmt->bit_depth >> 3) *
280 multiplier;
281
282 if (mcfg->out_fmt->s_freq % 1000)
283 out_rate = (mcfg->out_fmt->s_freq / 1000) + 1;
284 else
285 out_rate = (mcfg->out_fmt->s_freq / 1000);
286
287 mcfg->obs = out_rate * (mcfg->out_fmt->channels) *
288 (mcfg->out_fmt->bit_depth >> 3) *
289 multiplier;
Jeeja KPf7590d42015-10-07 11:31:53 +0100290}
291
Jeeja KP2d1419a2016-02-05 12:19:10 +0530292static int skl_tplg_update_be_blob(struct snd_soc_dapm_widget *w,
293 struct skl_sst *ctx)
294{
295 struct skl_module_cfg *m_cfg = w->priv;
296 int link_type, dir;
297 u32 ch, s_freq, s_fmt;
298 struct nhlt_specific_cfg *cfg;
299 struct skl *skl = get_skl_ctx(ctx->dev);
300
301 /* check if we already have blob */
302 if (m_cfg->formats_config.caps_size > 0)
303 return 0;
304
Jeeja KPc7c6c732016-03-01 07:59:10 +0530305 dev_dbg(ctx->dev, "Applying default cfg blob\n");
Jeeja KP2d1419a2016-02-05 12:19:10 +0530306 switch (m_cfg->dev_type) {
307 case SKL_DEVICE_DMIC:
308 link_type = NHLT_LINK_DMIC;
Jeeja KPc7c6c732016-03-01 07:59:10 +0530309 dir = SNDRV_PCM_STREAM_CAPTURE;
Jeeja KP2d1419a2016-02-05 12:19:10 +0530310 s_freq = m_cfg->in_fmt[0].s_freq;
311 s_fmt = m_cfg->in_fmt[0].bit_depth;
312 ch = m_cfg->in_fmt[0].channels;
313 break;
314
315 case SKL_DEVICE_I2S:
316 link_type = NHLT_LINK_SSP;
317 if (m_cfg->hw_conn_type == SKL_CONN_SOURCE) {
Jeeja KPc7c6c732016-03-01 07:59:10 +0530318 dir = SNDRV_PCM_STREAM_PLAYBACK;
Jeeja KP2d1419a2016-02-05 12:19:10 +0530319 s_freq = m_cfg->out_fmt[0].s_freq;
320 s_fmt = m_cfg->out_fmt[0].bit_depth;
321 ch = m_cfg->out_fmt[0].channels;
Jeeja KPc7c6c732016-03-01 07:59:10 +0530322 } else {
323 dir = SNDRV_PCM_STREAM_CAPTURE;
324 s_freq = m_cfg->in_fmt[0].s_freq;
325 s_fmt = m_cfg->in_fmt[0].bit_depth;
326 ch = m_cfg->in_fmt[0].channels;
Jeeja KP2d1419a2016-02-05 12:19:10 +0530327 }
328 break;
329
330 default:
331 return -EINVAL;
332 }
333
334 /* update the blob based on virtual bus_id and default params */
335 cfg = skl_get_ep_blob(skl, m_cfg->vbus_id, link_type,
336 s_fmt, ch, s_freq, dir);
337 if (cfg) {
338 m_cfg->formats_config.caps_size = cfg->size;
339 m_cfg->formats_config.caps = (u32 *) &cfg->caps;
340 } else {
341 dev_err(ctx->dev, "Blob NULL for id %x type %d dirn %d\n",
342 m_cfg->vbus_id, link_type, dir);
343 dev_err(ctx->dev, "PCM: ch %d, freq %d, fmt %d\n",
344 ch, s_freq, s_fmt);
345 return -EIO;
346 }
347
348 return 0;
349}
350
Jeeja KPf7590d42015-10-07 11:31:53 +0100351static void skl_tplg_update_module_params(struct snd_soc_dapm_widget *w,
352 struct skl_sst *ctx)
353{
354 struct skl_module_cfg *m_cfg = w->priv;
355 struct skl_pipe_params *params = m_cfg->pipe->p_params;
356 int p_conn_type = m_cfg->pipe->conn_type;
357 bool is_fe;
358
359 if (!m_cfg->params_fixup)
360 return;
361
362 dev_dbg(ctx->dev, "Mconfig for widget=%s BEFORE updation\n",
363 w->name);
364
365 skl_dump_mconfig(ctx, m_cfg);
366
367 if (p_conn_type == SKL_PIPE_CONN_TYPE_FE)
368 is_fe = true;
369 else
370 is_fe = false;
371
372 skl_tplg_update_params_fixup(m_cfg, params, is_fe);
373 skl_tplg_update_buffer_size(ctx, m_cfg);
374
375 dev_dbg(ctx->dev, "Mconfig for widget=%s AFTER updation\n",
376 w->name);
377
378 skl_dump_mconfig(ctx, m_cfg);
379}
380
Jeeja KPe4e2d2f2015-10-07 11:31:52 +0100381/*
382 * A pipe can have multiple modules, each of them will be a DAPM widget as
383 * well. While managing a pipeline we need to get the list of all the
384 * widgets in a pipelines, so this helper - skl_tplg_get_pipe_widget() helps
385 * to get the SKL type widgets in that pipeline
386 */
387static int skl_tplg_alloc_pipe_widget(struct device *dev,
388 struct snd_soc_dapm_widget *w, struct skl_pipe *pipe)
389{
390 struct skl_module_cfg *src_module = NULL;
391 struct snd_soc_dapm_path *p = NULL;
392 struct skl_pipe_module *p_module = NULL;
393
394 p_module = devm_kzalloc(dev, sizeof(*p_module), GFP_KERNEL);
395 if (!p_module)
396 return -ENOMEM;
397
398 p_module->w = w;
399 list_add_tail(&p_module->node, &pipe->w_list);
400
401 snd_soc_dapm_widget_for_each_sink_path(w, p) {
402 if ((p->sink->priv == NULL)
403 && (!is_skl_dsp_widget_type(w)))
404 continue;
405
406 if ((p->sink->priv != NULL) && p->connect
407 && is_skl_dsp_widget_type(p->sink)) {
408
409 src_module = p->sink->priv;
410 if (pipe->ppl_id == src_module->pipe->ppl_id)
411 skl_tplg_alloc_pipe_widget(dev,
412 p->sink, pipe);
413 }
414 }
415 return 0;
416}
417
418/*
Jeeja KPabb74002015-11-28 15:01:49 +0530419 * some modules can have multiple params set from user control and
420 * need to be set after module is initialized. If set_param flag is
421 * set module params will be done after module is initialised.
422 */
423static int skl_tplg_set_module_params(struct snd_soc_dapm_widget *w,
424 struct skl_sst *ctx)
425{
426 int i, ret;
427 struct skl_module_cfg *mconfig = w->priv;
428 const struct snd_kcontrol_new *k;
429 struct soc_bytes_ext *sb;
430 struct skl_algo_data *bc;
431 struct skl_specific_cfg *sp_cfg;
432
433 if (mconfig->formats_config.caps_size > 0 &&
Jeeja KP4ced1822015-12-03 23:29:53 +0530434 mconfig->formats_config.set_params == SKL_PARAM_SET) {
Jeeja KPabb74002015-11-28 15:01:49 +0530435 sp_cfg = &mconfig->formats_config;
436 ret = skl_set_module_params(ctx, sp_cfg->caps,
437 sp_cfg->caps_size,
438 sp_cfg->param_id, mconfig);
439 if (ret < 0)
440 return ret;
441 }
442
443 for (i = 0; i < w->num_kcontrols; i++) {
444 k = &w->kcontrol_news[i];
445 if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
446 sb = (void *) k->private_value;
447 bc = (struct skl_algo_data *)sb->dobj.private;
448
Jeeja KP4ced1822015-12-03 23:29:53 +0530449 if (bc->set_params == SKL_PARAM_SET) {
Jeeja KPabb74002015-11-28 15:01:49 +0530450 ret = skl_set_module_params(ctx,
451 (u32 *)bc->params, bc->max,
452 bc->param_id, mconfig);
453 if (ret < 0)
454 return ret;
455 }
456 }
457 }
458
459 return 0;
460}
461
462/*
463 * some module param can set from user control and this is required as
464 * when module is initailzed. if module param is required in init it is
465 * identifed by set_param flag. if set_param flag is not set, then this
466 * parameter needs to set as part of module init.
467 */
468static int skl_tplg_set_module_init_data(struct snd_soc_dapm_widget *w)
469{
470 const struct snd_kcontrol_new *k;
471 struct soc_bytes_ext *sb;
472 struct skl_algo_data *bc;
473 struct skl_module_cfg *mconfig = w->priv;
474 int i;
475
476 for (i = 0; i < w->num_kcontrols; i++) {
477 k = &w->kcontrol_news[i];
478 if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
479 sb = (struct soc_bytes_ext *)k->private_value;
480 bc = (struct skl_algo_data *)sb->dobj.private;
481
Jeeja KP4ced1822015-12-03 23:29:53 +0530482 if (bc->set_params != SKL_PARAM_INIT)
Jeeja KPabb74002015-11-28 15:01:49 +0530483 continue;
484
485 mconfig->formats_config.caps = (u32 *)&bc->params;
486 mconfig->formats_config.caps_size = bc->max;
487
488 break;
489 }
490 }
491
492 return 0;
493}
494
495/*
Jeeja KPe4e2d2f2015-10-07 11:31:52 +0100496 * Inside a pipe instance, we can have various modules. These modules need
497 * to instantiated in DSP by invoking INIT_MODULE IPC, which is achieved by
498 * skl_init_module() routine, so invoke that for all modules in a pipeline
499 */
500static int
501skl_tplg_init_pipe_modules(struct skl *skl, struct skl_pipe *pipe)
502{
503 struct skl_pipe_module *w_module;
504 struct snd_soc_dapm_widget *w;
505 struct skl_module_cfg *mconfig;
506 struct skl_sst *ctx = skl->skl_sst;
507 int ret = 0;
508
509 list_for_each_entry(w_module, &pipe->w_list, node) {
510 w = w_module->w;
511 mconfig = w->priv;
512
513 /* check resource available */
Dharageswari.R9ba8ffe2016-02-03 17:59:47 +0530514 if (!skl_is_pipe_mcps_avail(skl, mconfig))
Jeeja KPe4e2d2f2015-10-07 11:31:52 +0100515 return -ENOMEM;
516
Dharageswari R6c5768b2015-12-03 23:29:50 +0530517 if (mconfig->is_loadable && ctx->dsp->fw_ops.load_mod) {
518 ret = ctx->dsp->fw_ops.load_mod(ctx->dsp,
519 mconfig->id.module_id, mconfig->guid);
520 if (ret < 0)
521 return ret;
Jeeja KPd6436782016-03-28 22:11:30 +0530522
523 mconfig->m_state = SKL_MODULE_LOADED;
Dharageswari R6c5768b2015-12-03 23:29:50 +0530524 }
525
Jeeja KP2d1419a2016-02-05 12:19:10 +0530526 /* update blob if blob is null for be with default value */
527 skl_tplg_update_be_blob(w, ctx);
528
Jeeja KPf7590d42015-10-07 11:31:53 +0100529 /*
530 * apply fix/conversion to module params based on
531 * FE/BE params
532 */
533 skl_tplg_update_module_params(w, ctx);
Jeeja KPabb74002015-11-28 15:01:49 +0530534
535 skl_tplg_set_module_init_data(w);
Jeeja KP9939a9c2015-11-28 15:01:47 +0530536 ret = skl_init_module(ctx, mconfig);
Jeeja KPe4e2d2f2015-10-07 11:31:52 +0100537 if (ret < 0)
538 return ret;
Jeeja KPabb74002015-11-28 15:01:49 +0530539
Dharageswari R260eb732016-06-03 18:29:38 +0530540 skl_tplg_alloc_pipe_mcps(skl, mconfig);
Jeeja KPabb74002015-11-28 15:01:49 +0530541 ret = skl_tplg_set_module_params(w, ctx);
Jeeja KPe4e2d2f2015-10-07 11:31:52 +0100542 if (ret < 0)
543 return ret;
544 }
545
546 return 0;
547}
Vinod Kould93f8e52015-10-07 11:31:54 +0100548
Dharageswari R6c5768b2015-12-03 23:29:50 +0530549static int skl_tplg_unload_pipe_modules(struct skl_sst *ctx,
550 struct skl_pipe *pipe)
551{
552 struct skl_pipe_module *w_module = NULL;
553 struct skl_module_cfg *mconfig = NULL;
554
555 list_for_each_entry(w_module, &pipe->w_list, node) {
556 mconfig = w_module->w->priv;
557
Jeeja KPd6436782016-03-28 22:11:30 +0530558 if (mconfig->is_loadable && ctx->dsp->fw_ops.unload_mod &&
559 mconfig->m_state > SKL_MODULE_UNINIT)
Dharageswari R6c5768b2015-12-03 23:29:50 +0530560 return ctx->dsp->fw_ops.unload_mod(ctx->dsp,
561 mconfig->id.module_id);
562 }
563
564 /* no modules to unload in this path, so return */
565 return 0;
566}
567
Vinod Kould93f8e52015-10-07 11:31:54 +0100568/*
569 * Mixer module represents a pipeline. So in the Pre-PMU event of mixer we
570 * need create the pipeline. So we do following:
571 * - check the resources
572 * - Create the pipeline
573 * - Initialize the modules in pipeline
574 * - finally bind all modules together
575 */
576static int skl_tplg_mixer_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
577 struct skl *skl)
578{
579 int ret;
580 struct skl_module_cfg *mconfig = w->priv;
581 struct skl_pipe_module *w_module;
582 struct skl_pipe *s_pipe = mconfig->pipe;
583 struct skl_module_cfg *src_module = NULL, *dst_module;
584 struct skl_sst *ctx = skl->skl_sst;
585
586 /* check resource available */
Dharageswari.R9ba8ffe2016-02-03 17:59:47 +0530587 if (!skl_is_pipe_mcps_avail(skl, mconfig))
Vinod Kould93f8e52015-10-07 11:31:54 +0100588 return -EBUSY;
589
Dharageswari.R9ba8ffe2016-02-03 17:59:47 +0530590 if (!skl_is_pipe_mem_avail(skl, mconfig))
Vinod Kould93f8e52015-10-07 11:31:54 +0100591 return -ENOMEM;
592
593 /*
594 * Create a list of modules for pipe.
595 * This list contains modules from source to sink
596 */
597 ret = skl_create_pipeline(ctx, mconfig->pipe);
598 if (ret < 0)
599 return ret;
600
Dharageswari R260eb732016-06-03 18:29:38 +0530601 skl_tplg_alloc_pipe_mem(skl, mconfig);
602 skl_tplg_alloc_pipe_mcps(skl, mconfig);
603
Vinod Kould93f8e52015-10-07 11:31:54 +0100604 /*
605 * we create a w_list of all widgets in that pipe. This list is not
606 * freed on PMD event as widgets within a pipe are static. This
607 * saves us cycles to get widgets in pipe every time.
608 *
609 * So if we have already initialized all the widgets of a pipeline
610 * we skip, so check for list_empty and create the list if empty
611 */
612 if (list_empty(&s_pipe->w_list)) {
613 ret = skl_tplg_alloc_pipe_widget(ctx->dev, w, s_pipe);
614 if (ret < 0)
615 return ret;
616 }
617
618 /* Init all pipe modules from source to sink */
619 ret = skl_tplg_init_pipe_modules(skl, s_pipe);
620 if (ret < 0)
621 return ret;
622
623 /* Bind modules from source to sink */
624 list_for_each_entry(w_module, &s_pipe->w_list, node) {
625 dst_module = w_module->w->priv;
626
627 if (src_module == NULL) {
628 src_module = dst_module;
629 continue;
630 }
631
632 ret = skl_bind_modules(ctx, src_module, dst_module);
633 if (ret < 0)
634 return ret;
635
636 src_module = dst_module;
637 }
638
639 return 0;
640}
641
Jeeja KPcc6a4042016-02-05 12:19:08 +0530642/*
643 * Some modules require params to be set after the module is bound to
644 * all pins connected.
645 *
646 * The module provider initializes set_param flag for such modules and we
647 * send params after binding
648 */
649static int skl_tplg_set_module_bind_params(struct snd_soc_dapm_widget *w,
650 struct skl_module_cfg *mcfg, struct skl_sst *ctx)
651{
652 int i, ret;
653 struct skl_module_cfg *mconfig = w->priv;
654 const struct snd_kcontrol_new *k;
655 struct soc_bytes_ext *sb;
656 struct skl_algo_data *bc;
657 struct skl_specific_cfg *sp_cfg;
658
659 /*
660 * check all out/in pins are in bind state.
661 * if so set the module param
662 */
663 for (i = 0; i < mcfg->max_out_queue; i++) {
664 if (mcfg->m_out_pin[i].pin_state != SKL_PIN_BIND_DONE)
665 return 0;
666 }
667
668 for (i = 0; i < mcfg->max_in_queue; i++) {
669 if (mcfg->m_in_pin[i].pin_state != SKL_PIN_BIND_DONE)
670 return 0;
671 }
672
673 if (mconfig->formats_config.caps_size > 0 &&
674 mconfig->formats_config.set_params == SKL_PARAM_BIND) {
675 sp_cfg = &mconfig->formats_config;
676 ret = skl_set_module_params(ctx, sp_cfg->caps,
677 sp_cfg->caps_size,
678 sp_cfg->param_id, mconfig);
679 if (ret < 0)
680 return ret;
681 }
682
683 for (i = 0; i < w->num_kcontrols; i++) {
684 k = &w->kcontrol_news[i];
685 if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
686 sb = (void *) k->private_value;
687 bc = (struct skl_algo_data *)sb->dobj.private;
688
689 if (bc->set_params == SKL_PARAM_BIND) {
690 ret = skl_set_module_params(ctx,
691 (u32 *)bc->params, bc->max,
692 bc->param_id, mconfig);
693 if (ret < 0)
694 return ret;
695 }
696 }
697 }
698
699 return 0;
700}
701
Jeeja KP8724ff12015-10-27 09:22:52 +0900702static int skl_tplg_bind_sinks(struct snd_soc_dapm_widget *w,
703 struct skl *skl,
Jeeja KP6bd4cf82016-02-03 17:59:51 +0530704 struct snd_soc_dapm_widget *src_w,
Jeeja KP8724ff12015-10-27 09:22:52 +0900705 struct skl_module_cfg *src_mconfig)
Vinod Kould93f8e52015-10-07 11:31:54 +0100706{
707 struct snd_soc_dapm_path *p;
Jeeja KP0ed95d72015-11-13 19:22:11 +0530708 struct snd_soc_dapm_widget *sink = NULL, *next_sink = NULL;
Jeeja KP8724ff12015-10-27 09:22:52 +0900709 struct skl_module_cfg *sink_mconfig;
Vinod Kould93f8e52015-10-07 11:31:54 +0100710 struct skl_sst *ctx = skl->skl_sst;
Jeeja KP8724ff12015-10-27 09:22:52 +0900711 int ret;
Vinod Kould93f8e52015-10-07 11:31:54 +0100712
Jeeja KP8724ff12015-10-27 09:22:52 +0900713 snd_soc_dapm_widget_for_each_sink_path(w, p) {
Vinod Kould93f8e52015-10-07 11:31:54 +0100714 if (!p->connect)
715 continue;
716
717 dev_dbg(ctx->dev, "%s: src widget=%s\n", __func__, w->name);
718 dev_dbg(ctx->dev, "%s: sink widget=%s\n", __func__, p->sink->name);
719
Jeeja KP0ed95d72015-11-13 19:22:11 +0530720 next_sink = p->sink;
Jeeja KP6bd4cf82016-02-03 17:59:51 +0530721
722 if (!is_skl_dsp_widget_type(p->sink))
723 return skl_tplg_bind_sinks(p->sink, skl, src_w, src_mconfig);
724
Vinod Kould93f8e52015-10-07 11:31:54 +0100725 /*
726 * here we will check widgets in sink pipelines, so that
727 * can be any widgets type and we are only interested if
728 * they are ones used for SKL so check that first
729 */
730 if ((p->sink->priv != NULL) &&
731 is_skl_dsp_widget_type(p->sink)) {
732
733 sink = p->sink;
Vinod Kould93f8e52015-10-07 11:31:54 +0100734 sink_mconfig = sink->priv;
735
Jeeja KPcc6a4042016-02-05 12:19:08 +0530736 if (src_mconfig->m_state == SKL_MODULE_UNINIT ||
737 sink_mconfig->m_state == SKL_MODULE_UNINIT)
738 continue;
739
Vinod Kould93f8e52015-10-07 11:31:54 +0100740 /* Bind source to sink, mixin is always source */
741 ret = skl_bind_modules(ctx, src_mconfig, sink_mconfig);
742 if (ret)
743 return ret;
744
Jeeja KPcc6a4042016-02-05 12:19:08 +0530745 /* set module params after bind */
746 skl_tplg_set_module_bind_params(src_w, src_mconfig, ctx);
747 skl_tplg_set_module_bind_params(sink, sink_mconfig, ctx);
748
Vinod Kould93f8e52015-10-07 11:31:54 +0100749 /* Start sinks pipe first */
750 if (sink_mconfig->pipe->state != SKL_PIPE_STARTED) {
Jeeja KPd1730c32015-10-27 09:22:53 +0900751 if (sink_mconfig->pipe->conn_type !=
752 SKL_PIPE_CONN_TYPE_FE)
753 ret = skl_run_pipe(ctx,
754 sink_mconfig->pipe);
Vinod Kould93f8e52015-10-07 11:31:54 +0100755 if (ret)
756 return ret;
757 }
Vinod Kould93f8e52015-10-07 11:31:54 +0100758 }
759 }
760
Jeeja KP8724ff12015-10-27 09:22:52 +0900761 if (!sink)
Jeeja KP6bd4cf82016-02-03 17:59:51 +0530762 return skl_tplg_bind_sinks(next_sink, skl, src_w, src_mconfig);
Jeeja KP8724ff12015-10-27 09:22:52 +0900763
764 return 0;
765}
766
Vinod Kould93f8e52015-10-07 11:31:54 +0100767/*
768 * A PGA represents a module in a pipeline. So in the Pre-PMU event of PGA
769 * we need to do following:
770 * - Bind to sink pipeline
771 * Since the sink pipes can be running and we don't get mixer event on
772 * connect for already running mixer, we need to find the sink pipes
773 * here and bind to them. This way dynamic connect works.
774 * - Start sink pipeline, if not running
775 * - Then run current pipe
776 */
777static int skl_tplg_pga_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
Jeeja KP8724ff12015-10-27 09:22:52 +0900778 struct skl *skl)
Vinod Kould93f8e52015-10-07 11:31:54 +0100779{
Jeeja KP8724ff12015-10-27 09:22:52 +0900780 struct skl_module_cfg *src_mconfig;
Vinod Kould93f8e52015-10-07 11:31:54 +0100781 struct skl_sst *ctx = skl->skl_sst;
782 int ret = 0;
783
Jeeja KP8724ff12015-10-27 09:22:52 +0900784 src_mconfig = w->priv;
Vinod Kould93f8e52015-10-07 11:31:54 +0100785
786 /*
787 * find which sink it is connected to, bind with the sink,
788 * if sink is not started, start sink pipe first, then start
789 * this pipe
790 */
Jeeja KP6bd4cf82016-02-03 17:59:51 +0530791 ret = skl_tplg_bind_sinks(w, skl, w, src_mconfig);
Vinod Kould93f8e52015-10-07 11:31:54 +0100792 if (ret)
793 return ret;
794
Vinod Kould93f8e52015-10-07 11:31:54 +0100795 /* Start source pipe last after starting all sinks */
Jeeja KPd1730c32015-10-27 09:22:53 +0900796 if (src_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
797 return skl_run_pipe(ctx, src_mconfig->pipe);
Vinod Kould93f8e52015-10-07 11:31:54 +0100798
799 return 0;
800}
801
Jeeja KP8724ff12015-10-27 09:22:52 +0900802static struct snd_soc_dapm_widget *skl_get_src_dsp_widget(
803 struct snd_soc_dapm_widget *w, struct skl *skl)
804{
805 struct snd_soc_dapm_path *p;
806 struct snd_soc_dapm_widget *src_w = NULL;
807 struct skl_sst *ctx = skl->skl_sst;
808
809 snd_soc_dapm_widget_for_each_source_path(w, p) {
810 src_w = p->source;
811 if (!p->connect)
812 continue;
813
814 dev_dbg(ctx->dev, "sink widget=%s\n", w->name);
815 dev_dbg(ctx->dev, "src widget=%s\n", p->source->name);
816
817 /*
818 * here we will check widgets in sink pipelines, so that can
819 * be any widgets type and we are only interested if they are
820 * ones used for SKL so check that first
821 */
822 if ((p->source->priv != NULL) &&
823 is_skl_dsp_widget_type(p->source)) {
824 return p->source;
825 }
826 }
827
828 if (src_w != NULL)
829 return skl_get_src_dsp_widget(src_w, skl);
830
831 return NULL;
832}
833
Vinod Kould93f8e52015-10-07 11:31:54 +0100834/*
835 * in the Post-PMU event of mixer we need to do following:
836 * - Check if this pipe is running
837 * - if not, then
838 * - bind this pipeline to its source pipeline
839 * if source pipe is already running, this means it is a dynamic
840 * connection and we need to bind only to that pipe
841 * - start this pipeline
842 */
843static int skl_tplg_mixer_dapm_post_pmu_event(struct snd_soc_dapm_widget *w,
844 struct skl *skl)
845{
846 int ret = 0;
Vinod Kould93f8e52015-10-07 11:31:54 +0100847 struct snd_soc_dapm_widget *source, *sink;
848 struct skl_module_cfg *src_mconfig, *sink_mconfig;
849 struct skl_sst *ctx = skl->skl_sst;
850 int src_pipe_started = 0;
851
852 sink = w;
853 sink_mconfig = sink->priv;
854
855 /*
856 * If source pipe is already started, that means source is driving
857 * one more sink before this sink got connected, Since source is
858 * started, bind this sink to source and start this pipe.
859 */
Jeeja KP8724ff12015-10-27 09:22:52 +0900860 source = skl_get_src_dsp_widget(w, skl);
861 if (source != NULL) {
862 src_mconfig = source->priv;
863 sink_mconfig = sink->priv;
864 src_pipe_started = 1;
Vinod Kould93f8e52015-10-07 11:31:54 +0100865
866 /*
Jeeja KP8724ff12015-10-27 09:22:52 +0900867 * check pipe state, then no need to bind or start the
868 * pipe
Vinod Kould93f8e52015-10-07 11:31:54 +0100869 */
Jeeja KP8724ff12015-10-27 09:22:52 +0900870 if (src_mconfig->pipe->state != SKL_PIPE_STARTED)
871 src_pipe_started = 0;
Vinod Kould93f8e52015-10-07 11:31:54 +0100872 }
873
874 if (src_pipe_started) {
875 ret = skl_bind_modules(ctx, src_mconfig, sink_mconfig);
876 if (ret)
877 return ret;
878
Jeeja KPcc6a4042016-02-05 12:19:08 +0530879 /* set module params after bind */
880 skl_tplg_set_module_bind_params(source, src_mconfig, ctx);
881 skl_tplg_set_module_bind_params(sink, sink_mconfig, ctx);
882
Jeeja KPd1730c32015-10-27 09:22:53 +0900883 if (sink_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
884 ret = skl_run_pipe(ctx, sink_mconfig->pipe);
Vinod Kould93f8e52015-10-07 11:31:54 +0100885 }
886
887 return ret;
888}
889
890/*
891 * in the Pre-PMD event of mixer we need to do following:
892 * - Stop the pipe
893 * - find the source connections and remove that from dapm_path_list
894 * - unbind with source pipelines if still connected
895 */
896static int skl_tplg_mixer_dapm_pre_pmd_event(struct snd_soc_dapm_widget *w,
897 struct skl *skl)
898{
Vinod Kould93f8e52015-10-07 11:31:54 +0100899 struct skl_module_cfg *src_mconfig, *sink_mconfig;
Jeeja KPce1b5552015-10-27 09:22:51 +0900900 int ret = 0, i;
Vinod Kould93f8e52015-10-07 11:31:54 +0100901 struct skl_sst *ctx = skl->skl_sst;
902
Jeeja KPce1b5552015-10-27 09:22:51 +0900903 sink_mconfig = w->priv;
Vinod Kould93f8e52015-10-07 11:31:54 +0100904
905 /* Stop the pipe */
906 ret = skl_stop_pipe(ctx, sink_mconfig->pipe);
907 if (ret)
908 return ret;
909
Jeeja KPce1b5552015-10-27 09:22:51 +0900910 for (i = 0; i < sink_mconfig->max_in_queue; i++) {
911 if (sink_mconfig->m_in_pin[i].pin_state == SKL_PIN_BIND_DONE) {
912 src_mconfig = sink_mconfig->m_in_pin[i].tgt_mcfg;
913 if (!src_mconfig)
914 continue;
915 /*
916 * If path_found == 1, that means pmd for source
917 * pipe has not occurred, source is connected to
918 * some other sink. so its responsibility of sink
919 * to unbind itself from source.
920 */
921 ret = skl_stop_pipe(ctx, src_mconfig->pipe);
922 if (ret < 0)
923 return ret;
Vinod Kould93f8e52015-10-07 11:31:54 +0100924
Jeeja KPce1b5552015-10-27 09:22:51 +0900925 ret = skl_unbind_modules(ctx,
926 src_mconfig, sink_mconfig);
Vinod Kould93f8e52015-10-07 11:31:54 +0100927 }
928 }
929
Vinod Kould93f8e52015-10-07 11:31:54 +0100930 return ret;
931}
932
933/*
934 * in the Post-PMD event of mixer we need to do following:
935 * - Free the mcps used
936 * - Free the mem used
937 * - Unbind the modules within the pipeline
938 * - Delete the pipeline (modules are not required to be explicitly
939 * deleted, pipeline delete is enough here
940 */
941static int skl_tplg_mixer_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
942 struct skl *skl)
943{
944 struct skl_module_cfg *mconfig = w->priv;
945 struct skl_pipe_module *w_module;
946 struct skl_module_cfg *src_module = NULL, *dst_module;
947 struct skl_sst *ctx = skl->skl_sst;
948 struct skl_pipe *s_pipe = mconfig->pipe;
949 int ret = 0;
950
Dharageswari R260eb732016-06-03 18:29:38 +0530951 if (s_pipe->state == SKL_PIPE_INVALID)
952 return -EINVAL;
953
Vinod Kould93f8e52015-10-07 11:31:54 +0100954 skl_tplg_free_pipe_mcps(skl, mconfig);
Vinod Koul65976872015-11-23 22:26:29 +0530955 skl_tplg_free_pipe_mem(skl, mconfig);
Vinod Kould93f8e52015-10-07 11:31:54 +0100956
957 list_for_each_entry(w_module, &s_pipe->w_list, node) {
958 dst_module = w_module->w->priv;
959
Dharageswari R260eb732016-06-03 18:29:38 +0530960 if (mconfig->m_state >= SKL_MODULE_INIT_DONE)
961 skl_tplg_free_pipe_mcps(skl, dst_module);
Vinod Kould93f8e52015-10-07 11:31:54 +0100962 if (src_module == NULL) {
963 src_module = dst_module;
964 continue;
965 }
966
Guneshwor Singh7ca42f52016-02-03 17:59:46 +0530967 skl_unbind_modules(ctx, src_module, dst_module);
Vinod Kould93f8e52015-10-07 11:31:54 +0100968 src_module = dst_module;
969 }
970
971 ret = skl_delete_pipe(ctx, mconfig->pipe);
Vinod Kould93f8e52015-10-07 11:31:54 +0100972
Dharageswari R6c5768b2015-12-03 23:29:50 +0530973 return skl_tplg_unload_pipe_modules(ctx, s_pipe);
Vinod Kould93f8e52015-10-07 11:31:54 +0100974}
975
976/*
977 * in the Post-PMD event of PGA we need to do following:
978 * - Free the mcps used
979 * - Stop the pipeline
980 * - In source pipe is connected, unbind with source pipelines
981 */
982static int skl_tplg_pga_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
983 struct skl *skl)
984{
Vinod Kould93f8e52015-10-07 11:31:54 +0100985 struct skl_module_cfg *src_mconfig, *sink_mconfig;
Jeeja KPce1b5552015-10-27 09:22:51 +0900986 int ret = 0, i;
Vinod Kould93f8e52015-10-07 11:31:54 +0100987 struct skl_sst *ctx = skl->skl_sst;
988
Jeeja KPce1b5552015-10-27 09:22:51 +0900989 src_mconfig = w->priv;
Vinod Kould93f8e52015-10-07 11:31:54 +0100990
Vinod Kould93f8e52015-10-07 11:31:54 +0100991 /* Stop the pipe since this is a mixin module */
992 ret = skl_stop_pipe(ctx, src_mconfig->pipe);
993 if (ret)
994 return ret;
995
Jeeja KPce1b5552015-10-27 09:22:51 +0900996 for (i = 0; i < src_mconfig->max_out_queue; i++) {
997 if (src_mconfig->m_out_pin[i].pin_state == SKL_PIN_BIND_DONE) {
998 sink_mconfig = src_mconfig->m_out_pin[i].tgt_mcfg;
999 if (!sink_mconfig)
1000 continue;
1001 /*
1002 * This is a connecter and if path is found that means
1003 * unbind between source and sink has not happened yet
1004 */
Jeeja KPce1b5552015-10-27 09:22:51 +09001005 ret = skl_unbind_modules(ctx, src_mconfig,
1006 sink_mconfig);
Vinod Kould93f8e52015-10-07 11:31:54 +01001007 }
1008 }
1009
Vinod Kould93f8e52015-10-07 11:31:54 +01001010 return ret;
1011}
1012
1013/*
1014 * In modelling, we assume there will be ONLY one mixer in a pipeline. If
1015 * mixer is not required then it is treated as static mixer aka vmixer with
1016 * a hard path to source module
1017 * So we don't need to check if source is started or not as hard path puts
1018 * dependency on each other
1019 */
1020static int skl_tplg_vmixer_event(struct snd_soc_dapm_widget *w,
1021 struct snd_kcontrol *k, int event)
1022{
1023 struct snd_soc_dapm_context *dapm = w->dapm;
1024 struct skl *skl = get_skl_ctx(dapm->dev);
1025
1026 switch (event) {
1027 case SND_SOC_DAPM_PRE_PMU:
1028 return skl_tplg_mixer_dapm_pre_pmu_event(w, skl);
1029
Jeeja KPde1fedf2016-02-03 17:59:52 +05301030 case SND_SOC_DAPM_POST_PMU:
1031 return skl_tplg_mixer_dapm_post_pmu_event(w, skl);
1032
1033 case SND_SOC_DAPM_PRE_PMD:
1034 return skl_tplg_mixer_dapm_pre_pmd_event(w, skl);
1035
Vinod Kould93f8e52015-10-07 11:31:54 +01001036 case SND_SOC_DAPM_POST_PMD:
1037 return skl_tplg_mixer_dapm_post_pmd_event(w, skl);
1038 }
1039
1040 return 0;
1041}
1042
1043/*
1044 * In modelling, we assume there will be ONLY one mixer in a pipeline. If a
1045 * second one is required that is created as another pipe entity.
1046 * The mixer is responsible for pipe management and represent a pipeline
1047 * instance
1048 */
1049static int skl_tplg_mixer_event(struct snd_soc_dapm_widget *w,
1050 struct snd_kcontrol *k, int event)
1051{
1052 struct snd_soc_dapm_context *dapm = w->dapm;
1053 struct skl *skl = get_skl_ctx(dapm->dev);
1054
1055 switch (event) {
1056 case SND_SOC_DAPM_PRE_PMU:
1057 return skl_tplg_mixer_dapm_pre_pmu_event(w, skl);
1058
1059 case SND_SOC_DAPM_POST_PMU:
1060 return skl_tplg_mixer_dapm_post_pmu_event(w, skl);
1061
1062 case SND_SOC_DAPM_PRE_PMD:
1063 return skl_tplg_mixer_dapm_pre_pmd_event(w, skl);
1064
1065 case SND_SOC_DAPM_POST_PMD:
1066 return skl_tplg_mixer_dapm_post_pmd_event(w, skl);
1067 }
1068
1069 return 0;
1070}
1071
1072/*
1073 * In modelling, we assumed rest of the modules in pipeline are PGA. But we
1074 * are interested in last PGA (leaf PGA) in a pipeline to disconnect with
1075 * the sink when it is running (two FE to one BE or one FE to two BE)
1076 * scenarios
1077 */
1078static int skl_tplg_pga_event(struct snd_soc_dapm_widget *w,
1079 struct snd_kcontrol *k, int event)
1080
1081{
1082 struct snd_soc_dapm_context *dapm = w->dapm;
1083 struct skl *skl = get_skl_ctx(dapm->dev);
1084
1085 switch (event) {
1086 case SND_SOC_DAPM_PRE_PMU:
1087 return skl_tplg_pga_dapm_pre_pmu_event(w, skl);
1088
1089 case SND_SOC_DAPM_POST_PMD:
1090 return skl_tplg_pga_dapm_post_pmd_event(w, skl);
1091 }
1092
1093 return 0;
1094}
Vinod Koulcfb0a872015-10-07 11:31:55 +01001095
Jeeja KP140adfb2015-11-28 15:01:50 +05301096static int skl_tplg_tlv_control_get(struct snd_kcontrol *kcontrol,
1097 unsigned int __user *data, unsigned int size)
1098{
1099 struct soc_bytes_ext *sb =
1100 (struct soc_bytes_ext *)kcontrol->private_value;
1101 struct skl_algo_data *bc = (struct skl_algo_data *)sb->dobj.private;
Omair M Abdullah7d9f2912015-12-03 23:29:56 +05301102 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1103 struct skl_module_cfg *mconfig = w->priv;
1104 struct skl *skl = get_skl_ctx(w->dapm->dev);
1105
1106 if (w->power)
1107 skl_get_module_params(skl->skl_sst, (u32 *)bc->params,
1108 bc->max, bc->param_id, mconfig);
Jeeja KP140adfb2015-11-28 15:01:50 +05301109
Vinod Koul41556f62016-02-03 17:59:44 +05301110 /* decrement size for TLV header */
1111 size -= 2 * sizeof(u32);
1112
1113 /* check size as we don't want to send kernel data */
1114 if (size > bc->max)
1115 size = bc->max;
1116
Jeeja KP140adfb2015-11-28 15:01:50 +05301117 if (bc->params) {
1118 if (copy_to_user(data, &bc->param_id, sizeof(u32)))
1119 return -EFAULT;
Dan Carpentere8bc3c92015-12-08 08:53:22 +03001120 if (copy_to_user(data + 1, &size, sizeof(u32)))
Jeeja KP140adfb2015-11-28 15:01:50 +05301121 return -EFAULT;
Dan Carpentere8bc3c92015-12-08 08:53:22 +03001122 if (copy_to_user(data + 2, bc->params, size))
Jeeja KP140adfb2015-11-28 15:01:50 +05301123 return -EFAULT;
1124 }
1125
1126 return 0;
1127}
1128
1129#define SKL_PARAM_VENDOR_ID 0xff
1130
1131static int skl_tplg_tlv_control_set(struct snd_kcontrol *kcontrol,
1132 const unsigned int __user *data, unsigned int size)
1133{
1134 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1135 struct skl_module_cfg *mconfig = w->priv;
1136 struct soc_bytes_ext *sb =
1137 (struct soc_bytes_ext *)kcontrol->private_value;
1138 struct skl_algo_data *ac = (struct skl_algo_data *)sb->dobj.private;
1139 struct skl *skl = get_skl_ctx(w->dapm->dev);
1140
1141 if (ac->params) {
1142 /*
1143 * if the param_is is of type Vendor, firmware expects actual
1144 * parameter id and size from the control.
1145 */
1146 if (ac->param_id == SKL_PARAM_VENDOR_ID) {
1147 if (copy_from_user(ac->params, data, size))
1148 return -EFAULT;
1149 } else {
1150 if (copy_from_user(ac->params,
Alan65b4bcb2016-02-19 11:42:32 +05301151 data + 2, size))
Jeeja KP140adfb2015-11-28 15:01:50 +05301152 return -EFAULT;
1153 }
1154
1155 if (w->power)
1156 return skl_set_module_params(skl->skl_sst,
1157 (u32 *)ac->params, ac->max,
1158 ac->param_id, mconfig);
1159 }
1160
1161 return 0;
1162}
1163
Vinod Koulcfb0a872015-10-07 11:31:55 +01001164/*
1165 * The FE params are passed by hw_params of the DAI.
1166 * On hw_params, the params are stored in Gateway module of the FE and we
1167 * need to calculate the format in DSP module configuration, that
1168 * conversion is done here
1169 */
1170int skl_tplg_update_pipe_params(struct device *dev,
1171 struct skl_module_cfg *mconfig,
1172 struct skl_pipe_params *params)
1173{
1174 struct skl_pipe *pipe = mconfig->pipe;
1175 struct skl_module_fmt *format = NULL;
1176
1177 memcpy(pipe->p_params, params, sizeof(*params));
1178
1179 if (params->stream == SNDRV_PCM_STREAM_PLAYBACK)
Hardik T Shah4cd98992015-10-27 09:22:55 +09001180 format = &mconfig->in_fmt[0];
Vinod Koulcfb0a872015-10-07 11:31:55 +01001181 else
Hardik T Shah4cd98992015-10-27 09:22:55 +09001182 format = &mconfig->out_fmt[0];
Vinod Koulcfb0a872015-10-07 11:31:55 +01001183
1184 /* set the hw_params */
1185 format->s_freq = params->s_freq;
1186 format->channels = params->ch;
1187 format->valid_bit_depth = skl_get_bit_depth(params->s_fmt);
1188
1189 /*
1190 * 16 bit is 16 bit container whereas 24 bit is in 32 bit
1191 * container so update bit depth accordingly
1192 */
1193 switch (format->valid_bit_depth) {
1194 case SKL_DEPTH_16BIT:
1195 format->bit_depth = format->valid_bit_depth;
1196 break;
1197
1198 case SKL_DEPTH_24BIT:
Jeeja KP6654f392015-10-27 09:22:46 +09001199 case SKL_DEPTH_32BIT:
Vinod Koulcfb0a872015-10-07 11:31:55 +01001200 format->bit_depth = SKL_DEPTH_32BIT;
1201 break;
1202
1203 default:
1204 dev_err(dev, "Invalid bit depth %x for pipe\n",
1205 format->valid_bit_depth);
1206 return -EINVAL;
1207 }
1208
1209 if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1210 mconfig->ibs = (format->s_freq / 1000) *
1211 (format->channels) *
1212 (format->bit_depth >> 3);
1213 } else {
1214 mconfig->obs = (format->s_freq / 1000) *
1215 (format->channels) *
1216 (format->bit_depth >> 3);
1217 }
1218
1219 return 0;
1220}
1221
1222/*
1223 * Query the module config for the FE DAI
1224 * This is used to find the hw_params set for that DAI and apply to FE
1225 * pipeline
1226 */
1227struct skl_module_cfg *
1228skl_tplg_fe_get_cpr_module(struct snd_soc_dai *dai, int stream)
1229{
1230 struct snd_soc_dapm_widget *w;
1231 struct snd_soc_dapm_path *p = NULL;
1232
1233 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1234 w = dai->playback_widget;
Subhransu S. Prustyf0900eb2015-10-22 23:22:36 +05301235 snd_soc_dapm_widget_for_each_sink_path(w, p) {
Vinod Koulcfb0a872015-10-07 11:31:55 +01001236 if (p->connect && p->sink->power &&
Jeeja KPa28f51d2015-10-27 09:22:44 +09001237 !is_skl_dsp_widget_type(p->sink))
Vinod Koulcfb0a872015-10-07 11:31:55 +01001238 continue;
1239
1240 if (p->sink->priv) {
1241 dev_dbg(dai->dev, "set params for %s\n",
1242 p->sink->name);
1243 return p->sink->priv;
1244 }
1245 }
1246 } else {
1247 w = dai->capture_widget;
Subhransu S. Prustyf0900eb2015-10-22 23:22:36 +05301248 snd_soc_dapm_widget_for_each_source_path(w, p) {
Vinod Koulcfb0a872015-10-07 11:31:55 +01001249 if (p->connect && p->source->power &&
Jeeja KPa28f51d2015-10-27 09:22:44 +09001250 !is_skl_dsp_widget_type(p->source))
Vinod Koulcfb0a872015-10-07 11:31:55 +01001251 continue;
1252
1253 if (p->source->priv) {
1254 dev_dbg(dai->dev, "set params for %s\n",
1255 p->source->name);
1256 return p->source->priv;
1257 }
1258 }
1259 }
1260
1261 return NULL;
1262}
1263
Dharageswari.R718a42b2016-02-05 12:19:06 +05301264static struct skl_module_cfg *skl_get_mconfig_pb_cpr(
1265 struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
1266{
1267 struct snd_soc_dapm_path *p;
1268 struct skl_module_cfg *mconfig = NULL;
1269
1270 snd_soc_dapm_widget_for_each_source_path(w, p) {
1271 if (w->endpoints[SND_SOC_DAPM_DIR_OUT] > 0) {
1272 if (p->connect &&
1273 (p->sink->id == snd_soc_dapm_aif_out) &&
1274 p->source->priv) {
1275 mconfig = p->source->priv;
1276 return mconfig;
1277 }
1278 mconfig = skl_get_mconfig_pb_cpr(dai, p->source);
1279 if (mconfig)
1280 return mconfig;
1281 }
1282 }
1283 return mconfig;
1284}
1285
1286static struct skl_module_cfg *skl_get_mconfig_cap_cpr(
1287 struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
1288{
1289 struct snd_soc_dapm_path *p;
1290 struct skl_module_cfg *mconfig = NULL;
1291
1292 snd_soc_dapm_widget_for_each_sink_path(w, p) {
1293 if (w->endpoints[SND_SOC_DAPM_DIR_IN] > 0) {
1294 if (p->connect &&
1295 (p->source->id == snd_soc_dapm_aif_in) &&
1296 p->sink->priv) {
1297 mconfig = p->sink->priv;
1298 return mconfig;
1299 }
1300 mconfig = skl_get_mconfig_cap_cpr(dai, p->sink);
1301 if (mconfig)
1302 return mconfig;
1303 }
1304 }
1305 return mconfig;
1306}
1307
1308struct skl_module_cfg *
1309skl_tplg_be_get_cpr_module(struct snd_soc_dai *dai, int stream)
1310{
1311 struct snd_soc_dapm_widget *w;
1312 struct skl_module_cfg *mconfig;
1313
1314 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1315 w = dai->playback_widget;
1316 mconfig = skl_get_mconfig_pb_cpr(dai, w);
1317 } else {
1318 w = dai->capture_widget;
1319 mconfig = skl_get_mconfig_cap_cpr(dai, w);
1320 }
1321 return mconfig;
1322}
1323
Vinod Koulcfb0a872015-10-07 11:31:55 +01001324static u8 skl_tplg_be_link_type(int dev_type)
1325{
1326 int ret;
1327
1328 switch (dev_type) {
1329 case SKL_DEVICE_BT:
1330 ret = NHLT_LINK_SSP;
1331 break;
1332
1333 case SKL_DEVICE_DMIC:
1334 ret = NHLT_LINK_DMIC;
1335 break;
1336
1337 case SKL_DEVICE_I2S:
1338 ret = NHLT_LINK_SSP;
1339 break;
1340
1341 case SKL_DEVICE_HDALINK:
1342 ret = NHLT_LINK_HDA;
1343 break;
1344
1345 default:
1346 ret = NHLT_LINK_INVALID;
1347 break;
1348 }
1349
1350 return ret;
1351}
1352
1353/*
1354 * Fill the BE gateway parameters
1355 * The BE gateway expects a blob of parameters which are kept in the ACPI
1356 * NHLT blob, so query the blob for interface type (i2s/pdm) and instance.
1357 * The port can have multiple settings so pick based on the PCM
1358 * parameters
1359 */
1360static int skl_tplg_be_fill_pipe_params(struct snd_soc_dai *dai,
1361 struct skl_module_cfg *mconfig,
1362 struct skl_pipe_params *params)
1363{
1364 struct skl_pipe *pipe = mconfig->pipe;
1365 struct nhlt_specific_cfg *cfg;
1366 struct skl *skl = get_skl_ctx(dai->dev);
1367 int link_type = skl_tplg_be_link_type(mconfig->dev_type);
1368
1369 memcpy(pipe->p_params, params, sizeof(*params));
1370
Jeeja KPb30c2752015-10-27 09:22:48 +09001371 if (link_type == NHLT_LINK_HDA)
1372 return 0;
1373
Vinod Koulcfb0a872015-10-07 11:31:55 +01001374 /* update the blob based on virtual bus_id*/
1375 cfg = skl_get_ep_blob(skl, mconfig->vbus_id, link_type,
1376 params->s_fmt, params->ch,
1377 params->s_freq, params->stream);
1378 if (cfg) {
1379 mconfig->formats_config.caps_size = cfg->size;
Jeeja KPbc032812015-10-22 23:22:35 +05301380 mconfig->formats_config.caps = (u32 *) &cfg->caps;
Vinod Koulcfb0a872015-10-07 11:31:55 +01001381 } else {
1382 dev_err(dai->dev, "Blob NULL for id %x type %d dirn %d\n",
1383 mconfig->vbus_id, link_type,
1384 params->stream);
1385 dev_err(dai->dev, "PCM: ch %d, freq %d, fmt %d\n",
1386 params->ch, params->s_freq, params->s_fmt);
1387 return -EINVAL;
1388 }
1389
1390 return 0;
1391}
1392
1393static int skl_tplg_be_set_src_pipe_params(struct snd_soc_dai *dai,
1394 struct snd_soc_dapm_widget *w,
1395 struct skl_pipe_params *params)
1396{
1397 struct snd_soc_dapm_path *p;
Subhransu S. Prusty4d8adccb2015-10-22 23:22:37 +05301398 int ret = -EIO;
Vinod Koulcfb0a872015-10-07 11:31:55 +01001399
Subhransu S. Prustyf0900eb2015-10-22 23:22:36 +05301400 snd_soc_dapm_widget_for_each_source_path(w, p) {
Vinod Koulcfb0a872015-10-07 11:31:55 +01001401 if (p->connect && is_skl_dsp_widget_type(p->source) &&
1402 p->source->priv) {
1403
Jeeja KP9a03cb42015-10-27 09:22:54 +09001404 ret = skl_tplg_be_fill_pipe_params(dai,
1405 p->source->priv, params);
1406 if (ret < 0)
1407 return ret;
Vinod Koulcfb0a872015-10-07 11:31:55 +01001408 } else {
Jeeja KP9a03cb42015-10-27 09:22:54 +09001409 ret = skl_tplg_be_set_src_pipe_params(dai,
1410 p->source, params);
Subhransu S. Prusty4d8adccb2015-10-22 23:22:37 +05301411 if (ret < 0)
1412 return ret;
Vinod Koulcfb0a872015-10-07 11:31:55 +01001413 }
1414 }
1415
Subhransu S. Prusty4d8adccb2015-10-22 23:22:37 +05301416 return ret;
Vinod Koulcfb0a872015-10-07 11:31:55 +01001417}
1418
1419static int skl_tplg_be_set_sink_pipe_params(struct snd_soc_dai *dai,
1420 struct snd_soc_dapm_widget *w, struct skl_pipe_params *params)
1421{
1422 struct snd_soc_dapm_path *p = NULL;
Subhransu S. Prusty4d8adccb2015-10-22 23:22:37 +05301423 int ret = -EIO;
Vinod Koulcfb0a872015-10-07 11:31:55 +01001424
Subhransu S. Prustyf0900eb2015-10-22 23:22:36 +05301425 snd_soc_dapm_widget_for_each_sink_path(w, p) {
Vinod Koulcfb0a872015-10-07 11:31:55 +01001426 if (p->connect && is_skl_dsp_widget_type(p->sink) &&
1427 p->sink->priv) {
1428
Jeeja KP9a03cb42015-10-27 09:22:54 +09001429 ret = skl_tplg_be_fill_pipe_params(dai,
1430 p->sink->priv, params);
1431 if (ret < 0)
1432 return ret;
Vinod Koulcfb0a872015-10-07 11:31:55 +01001433 } else {
Subhransu S. Prusty4d8adccb2015-10-22 23:22:37 +05301434 ret = skl_tplg_be_set_sink_pipe_params(
Vinod Koulcfb0a872015-10-07 11:31:55 +01001435 dai, p->sink, params);
Subhransu S. Prusty4d8adccb2015-10-22 23:22:37 +05301436 if (ret < 0)
1437 return ret;
Vinod Koulcfb0a872015-10-07 11:31:55 +01001438 }
1439 }
1440
Subhransu S. Prusty4d8adccb2015-10-22 23:22:37 +05301441 return ret;
Vinod Koulcfb0a872015-10-07 11:31:55 +01001442}
1443
1444/*
1445 * BE hw_params can be a source parameters (capture) or sink parameters
1446 * (playback). Based on sink and source we need to either find the source
1447 * list or the sink list and set the pipeline parameters
1448 */
1449int skl_tplg_be_update_params(struct snd_soc_dai *dai,
1450 struct skl_pipe_params *params)
1451{
1452 struct snd_soc_dapm_widget *w;
1453
1454 if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1455 w = dai->playback_widget;
1456
1457 return skl_tplg_be_set_src_pipe_params(dai, w, params);
1458
1459 } else {
1460 w = dai->capture_widget;
1461
1462 return skl_tplg_be_set_sink_pipe_params(dai, w, params);
1463 }
1464
1465 return 0;
1466}
Vinod Koul3af36702015-10-07 11:31:56 +01001467
1468static const struct snd_soc_tplg_widget_events skl_tplg_widget_ops[] = {
1469 {SKL_MIXER_EVENT, skl_tplg_mixer_event},
1470 {SKL_VMIXER_EVENT, skl_tplg_vmixer_event},
1471 {SKL_PGA_EVENT, skl_tplg_pga_event},
1472};
1473
Jeeja KP140adfb2015-11-28 15:01:50 +05301474static const struct snd_soc_tplg_bytes_ext_ops skl_tlv_ops[] = {
1475 {SKL_CONTROL_TYPE_BYTE_TLV, skl_tplg_tlv_control_get,
1476 skl_tplg_tlv_control_set},
1477};
1478
Vinod Koul3af36702015-10-07 11:31:56 +01001479/*
1480 * The topology binary passes the pin info for a module so initialize the pin
1481 * info passed into module instance
1482 */
Jeeja KP6abca1d2015-10-22 23:22:42 +05301483static void skl_fill_module_pin_info(struct skl_dfw_module_pin *dfw_pin,
1484 struct skl_module_pin *m_pin,
1485 bool is_dynamic, int max_pin)
Vinod Koul3af36702015-10-07 11:31:56 +01001486{
1487 int i;
1488
1489 for (i = 0; i < max_pin; i++) {
Jeeja KP6abca1d2015-10-22 23:22:42 +05301490 m_pin[i].id.module_id = dfw_pin[i].module_id;
1491 m_pin[i].id.instance_id = dfw_pin[i].instance_id;
Vinod Koul3af36702015-10-07 11:31:56 +01001492 m_pin[i].in_use = false;
Jeeja KP6abca1d2015-10-22 23:22:42 +05301493 m_pin[i].is_dynamic = is_dynamic;
Jeeja KP4f745702015-10-27 09:22:49 +09001494 m_pin[i].pin_state = SKL_PIN_UNBIND;
Vinod Koul3af36702015-10-07 11:31:56 +01001495 }
1496}
1497
1498/*
1499 * Add pipeline from topology binary into driver pipeline list
1500 *
1501 * If already added we return that instance
1502 * Otherwise we create a new instance and add into driver list
1503 */
1504static struct skl_pipe *skl_tplg_add_pipe(struct device *dev,
1505 struct skl *skl, struct skl_dfw_pipe *dfw_pipe)
1506{
1507 struct skl_pipeline *ppl;
1508 struct skl_pipe *pipe;
1509 struct skl_pipe_params *params;
1510
1511 list_for_each_entry(ppl, &skl->ppl_list, node) {
1512 if (ppl->pipe->ppl_id == dfw_pipe->pipe_id)
1513 return ppl->pipe;
1514 }
1515
1516 ppl = devm_kzalloc(dev, sizeof(*ppl), GFP_KERNEL);
1517 if (!ppl)
1518 return NULL;
1519
1520 pipe = devm_kzalloc(dev, sizeof(*pipe), GFP_KERNEL);
1521 if (!pipe)
1522 return NULL;
1523
1524 params = devm_kzalloc(dev, sizeof(*params), GFP_KERNEL);
1525 if (!params)
1526 return NULL;
1527
1528 pipe->ppl_id = dfw_pipe->pipe_id;
1529 pipe->memory_pages = dfw_pipe->memory_pages;
1530 pipe->pipe_priority = dfw_pipe->pipe_priority;
1531 pipe->conn_type = dfw_pipe->conn_type;
1532 pipe->state = SKL_PIPE_INVALID;
1533 pipe->p_params = params;
1534 INIT_LIST_HEAD(&pipe->w_list);
1535
1536 ppl->pipe = pipe;
1537 list_add(&ppl->node, &skl->ppl_list);
1538
1539 return ppl->pipe;
1540}
1541
Hardik T Shah4cd98992015-10-27 09:22:55 +09001542static void skl_tplg_fill_fmt(struct skl_module_fmt *dst_fmt,
1543 struct skl_dfw_module_fmt *src_fmt,
1544 int pins)
1545{
1546 int i;
1547
1548 for (i = 0; i < pins; i++) {
1549 dst_fmt[i].channels = src_fmt[i].channels;
1550 dst_fmt[i].s_freq = src_fmt[i].freq;
1551 dst_fmt[i].bit_depth = src_fmt[i].bit_depth;
1552 dst_fmt[i].valid_bit_depth = src_fmt[i].valid_bit_depth;
1553 dst_fmt[i].ch_cfg = src_fmt[i].ch_cfg;
1554 dst_fmt[i].ch_map = src_fmt[i].ch_map;
1555 dst_fmt[i].interleaving_style = src_fmt[i].interleaving_style;
1556 dst_fmt[i].sample_type = src_fmt[i].sample_type;
1557 }
1558}
1559
Vinod Koul3af36702015-10-07 11:31:56 +01001560/*
1561 * Topology core widget load callback
1562 *
1563 * This is used to save the private data for each widget which gives
1564 * information to the driver about module and pipeline parameters which DSP
1565 * FW expects like ids, resource values, formats etc
1566 */
1567static int skl_tplg_widget_load(struct snd_soc_component *cmpnt,
Jeeja KPb663a8c2015-10-07 11:31:57 +01001568 struct snd_soc_dapm_widget *w,
1569 struct snd_soc_tplg_dapm_widget *tplg_w)
Vinod Koul3af36702015-10-07 11:31:56 +01001570{
1571 int ret;
1572 struct hdac_ext_bus *ebus = snd_soc_component_get_drvdata(cmpnt);
1573 struct skl *skl = ebus_to_skl(ebus);
1574 struct hdac_bus *bus = ebus_to_hbus(ebus);
1575 struct skl_module_cfg *mconfig;
1576 struct skl_pipe *pipe;
Jeeja KPb663a8c2015-10-07 11:31:57 +01001577 struct skl_dfw_module *dfw_config =
1578 (struct skl_dfw_module *)tplg_w->priv.data;
Vinod Koul3af36702015-10-07 11:31:56 +01001579
1580 if (!tplg_w->priv.size)
1581 goto bind_event;
1582
1583 mconfig = devm_kzalloc(bus->dev, sizeof(*mconfig), GFP_KERNEL);
1584
1585 if (!mconfig)
1586 return -ENOMEM;
1587
1588 w->priv = mconfig;
Shreyas NC09305da2016-04-21 11:45:22 +05301589 memcpy(&mconfig->guid, &dfw_config->uuid, 16);
1590
Shreyas NCea6b3e92016-05-30 17:42:59 +05301591 ret = snd_skl_get_module_info(skl->skl_sst, mconfig->guid, dfw_config);
1592 if (ret < 0)
1593 return ret;
1594
Vinod Koul3af36702015-10-07 11:31:56 +01001595 mconfig->id.module_id = dfw_config->module_id;
1596 mconfig->id.instance_id = dfw_config->instance_id;
1597 mconfig->mcps = dfw_config->max_mcps;
1598 mconfig->ibs = dfw_config->ibs;
1599 mconfig->obs = dfw_config->obs;
1600 mconfig->core_id = dfw_config->core_id;
1601 mconfig->max_in_queue = dfw_config->max_in_queue;
1602 mconfig->max_out_queue = dfw_config->max_out_queue;
1603 mconfig->is_loadable = dfw_config->is_loadable;
Hardik T Shah4cd98992015-10-27 09:22:55 +09001604 skl_tplg_fill_fmt(mconfig->in_fmt, dfw_config->in_fmt,
1605 MODULE_MAX_IN_PINS);
1606 skl_tplg_fill_fmt(mconfig->out_fmt, dfw_config->out_fmt,
1607 MODULE_MAX_OUT_PINS);
1608
Vinod Koul3af36702015-10-07 11:31:56 +01001609 mconfig->params_fixup = dfw_config->params_fixup;
1610 mconfig->converter = dfw_config->converter;
1611 mconfig->m_type = dfw_config->module_type;
1612 mconfig->vbus_id = dfw_config->vbus_id;
Jeeja KPb18c4582015-12-03 23:29:51 +05301613 mconfig->mem_pages = dfw_config->mem_pages;
Vinod Koul3af36702015-10-07 11:31:56 +01001614
1615 pipe = skl_tplg_add_pipe(bus->dev, skl, &dfw_config->pipe);
1616 if (pipe)
1617 mconfig->pipe = pipe;
1618
1619 mconfig->dev_type = dfw_config->dev_type;
1620 mconfig->hw_conn_type = dfw_config->hw_conn_type;
1621 mconfig->time_slot = dfw_config->time_slot;
1622 mconfig->formats_config.caps_size = dfw_config->caps.caps_size;
1623
Hardik T Shah4cd98992015-10-27 09:22:55 +09001624 mconfig->m_in_pin = devm_kzalloc(bus->dev, (mconfig->max_in_queue) *
1625 sizeof(*mconfig->m_in_pin),
1626 GFP_KERNEL);
Vinod Koul3af36702015-10-07 11:31:56 +01001627 if (!mconfig->m_in_pin)
1628 return -ENOMEM;
1629
Jeeja KP6abca1d2015-10-22 23:22:42 +05301630 mconfig->m_out_pin = devm_kzalloc(bus->dev, (mconfig->max_out_queue) *
1631 sizeof(*mconfig->m_out_pin),
1632 GFP_KERNEL);
Vinod Koul3af36702015-10-07 11:31:56 +01001633 if (!mconfig->m_out_pin)
1634 return -ENOMEM;
1635
Jeeja KP6abca1d2015-10-22 23:22:42 +05301636 skl_fill_module_pin_info(dfw_config->in_pin, mconfig->m_in_pin,
1637 dfw_config->is_dynamic_in_pin,
1638 mconfig->max_in_queue);
1639
1640 skl_fill_module_pin_info(dfw_config->out_pin, mconfig->m_out_pin,
1641 dfw_config->is_dynamic_out_pin,
1642 mconfig->max_out_queue);
1643
Vinod Koul3af36702015-10-07 11:31:56 +01001644
1645 if (mconfig->formats_config.caps_size == 0)
1646 goto bind_event;
1647
1648 mconfig->formats_config.caps = (u32 *)devm_kzalloc(bus->dev,
Jeeja KPb663a8c2015-10-07 11:31:57 +01001649 mconfig->formats_config.caps_size, GFP_KERNEL);
Vinod Koul3af36702015-10-07 11:31:56 +01001650
1651 if (mconfig->formats_config.caps == NULL)
1652 return -ENOMEM;
1653
1654 memcpy(mconfig->formats_config.caps, dfw_config->caps.caps,
Jeeja KPabb74002015-11-28 15:01:49 +05301655 dfw_config->caps.caps_size);
1656 mconfig->formats_config.param_id = dfw_config->caps.param_id;
1657 mconfig->formats_config.set_params = dfw_config->caps.set_params;
Vinod Koul3af36702015-10-07 11:31:56 +01001658
1659bind_event:
1660 if (tplg_w->event_type == 0) {
Vinod Koul3373f712015-10-07 16:39:38 +01001661 dev_dbg(bus->dev, "ASoC: No event handler required\n");
Vinod Koul3af36702015-10-07 11:31:56 +01001662 return 0;
1663 }
1664
1665 ret = snd_soc_tplg_widget_bind_event(w, skl_tplg_widget_ops,
Jeeja KPb663a8c2015-10-07 11:31:57 +01001666 ARRAY_SIZE(skl_tplg_widget_ops),
1667 tplg_w->event_type);
Vinod Koul3af36702015-10-07 11:31:56 +01001668
1669 if (ret) {
1670 dev_err(bus->dev, "%s: No matching event handlers found for %d\n",
1671 __func__, tplg_w->event_type);
1672 return -EINVAL;
1673 }
1674
1675 return 0;
1676}
1677
Jeeja KP140adfb2015-11-28 15:01:50 +05301678static int skl_init_algo_data(struct device *dev, struct soc_bytes_ext *be,
1679 struct snd_soc_tplg_bytes_control *bc)
1680{
1681 struct skl_algo_data *ac;
1682 struct skl_dfw_algo_data *dfw_ac =
1683 (struct skl_dfw_algo_data *)bc->priv.data;
1684
1685 ac = devm_kzalloc(dev, sizeof(*ac), GFP_KERNEL);
1686 if (!ac)
1687 return -ENOMEM;
1688
1689 /* Fill private data */
1690 ac->max = dfw_ac->max;
1691 ac->param_id = dfw_ac->param_id;
1692 ac->set_params = dfw_ac->set_params;
1693
1694 if (ac->max) {
1695 ac->params = (char *) devm_kzalloc(dev, ac->max, GFP_KERNEL);
1696 if (!ac->params)
1697 return -ENOMEM;
1698
Alan Coxedd7ea22016-02-22 09:37:27 +05301699 memcpy(ac->params, dfw_ac->params, ac->max);
Jeeja KP140adfb2015-11-28 15:01:50 +05301700 }
1701
1702 be->dobj.private = ac;
1703 return 0;
1704}
1705
1706static int skl_tplg_control_load(struct snd_soc_component *cmpnt,
1707 struct snd_kcontrol_new *kctl,
1708 struct snd_soc_tplg_ctl_hdr *hdr)
1709{
1710 struct soc_bytes_ext *sb;
1711 struct snd_soc_tplg_bytes_control *tplg_bc;
1712 struct hdac_ext_bus *ebus = snd_soc_component_get_drvdata(cmpnt);
1713 struct hdac_bus *bus = ebus_to_hbus(ebus);
1714
1715 switch (hdr->ops.info) {
1716 case SND_SOC_TPLG_CTL_BYTES:
1717 tplg_bc = container_of(hdr,
1718 struct snd_soc_tplg_bytes_control, hdr);
1719 if (kctl->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
1720 sb = (struct soc_bytes_ext *)kctl->private_value;
1721 if (tplg_bc->priv.size)
1722 return skl_init_algo_data(
1723 bus->dev, sb, tplg_bc);
1724 }
1725 break;
1726
1727 default:
1728 dev_warn(bus->dev, "Control load not supported %d:%d:%d\n",
1729 hdr->ops.get, hdr->ops.put, hdr->ops.info);
1730 break;
1731 }
1732
1733 return 0;
1734}
1735
Vinod Koul3af36702015-10-07 11:31:56 +01001736static struct snd_soc_tplg_ops skl_tplg_ops = {
1737 .widget_load = skl_tplg_widget_load,
Jeeja KP140adfb2015-11-28 15:01:50 +05301738 .control_load = skl_tplg_control_load,
1739 .bytes_ext_ops = skl_tlv_ops,
1740 .bytes_ext_ops_count = ARRAY_SIZE(skl_tlv_ops),
Vinod Koul3af36702015-10-07 11:31:56 +01001741};
1742
1743/* This will be read from topology manifest, currently defined here */
1744#define SKL_MAX_MCPS 30000000
1745#define SKL_FW_MAX_MEM 1000000
1746
1747/*
1748 * SKL topology init routine
1749 */
1750int skl_tplg_init(struct snd_soc_platform *platform, struct hdac_ext_bus *ebus)
1751{
1752 int ret;
1753 const struct firmware *fw;
1754 struct hdac_bus *bus = ebus_to_hbus(ebus);
1755 struct skl *skl = ebus_to_skl(ebus);
1756
Vinod Koul4b235c42016-02-19 11:42:34 +05301757 ret = request_firmware(&fw, skl->tplg_name, bus->dev);
Vinod Koul3af36702015-10-07 11:31:56 +01001758 if (ret < 0) {
Jeeja KPb663a8c2015-10-07 11:31:57 +01001759 dev_err(bus->dev, "tplg fw %s load failed with %d\n",
Vinod Koul4b235c42016-02-19 11:42:34 +05301760 skl->tplg_name, ret);
1761 ret = request_firmware(&fw, "dfw_sst.bin", bus->dev);
1762 if (ret < 0) {
1763 dev_err(bus->dev, "Fallback tplg fw %s load failed with %d\n",
1764 "dfw_sst.bin", ret);
1765 return ret;
1766 }
Vinod Koul3af36702015-10-07 11:31:56 +01001767 }
1768
1769 /*
1770 * The complete tplg for SKL is loaded as index 0, we don't use
1771 * any other index
1772 */
Jeeja KPb663a8c2015-10-07 11:31:57 +01001773 ret = snd_soc_tplg_component_load(&platform->component,
1774 &skl_tplg_ops, fw, 0);
Vinod Koul3af36702015-10-07 11:31:56 +01001775 if (ret < 0) {
1776 dev_err(bus->dev, "tplg component load failed%d\n", ret);
Sudip Mukherjeec14a82c2016-01-21 17:27:59 +05301777 release_firmware(fw);
Vinod Koul3af36702015-10-07 11:31:56 +01001778 return -EINVAL;
1779 }
1780
1781 skl->resource.max_mcps = SKL_MAX_MCPS;
1782 skl->resource.max_mem = SKL_FW_MAX_MEM;
1783
Vinod Kould8018362016-01-05 17:16:04 +05301784 skl->tplg = fw;
1785
Vinod Koul3af36702015-10-07 11:31:56 +01001786 return 0;
1787}