blob: 9a7a008f66892a795f90a1576ea78dcf57208a46 [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>
Shreyas NC6277e832016-08-12 12:29:51 +053024#include <uapi/sound/snd_sst_tokens.h>
Jeeja KPe4e2d2f2015-10-07 11:31:52 +010025#include "skl-sst-dsp.h"
26#include "skl-sst-ipc.h"
27#include "skl-topology.h"
28#include "skl.h"
29#include "skl-tplg-interface.h"
Dharageswari R6c5768b2015-12-03 23:29:50 +053030#include "../common/sst-dsp.h"
31#include "../common/sst-dsp-priv.h"
Jeeja KPe4e2d2f2015-10-07 11:31:52 +010032
Jeeja KPf7590d42015-10-07 11:31:53 +010033#define SKL_CH_FIXUP_MASK (1 << 0)
34#define SKL_RATE_FIXUP_MASK (1 << 1)
35#define SKL_FMT_FIXUP_MASK (1 << 2)
Shreyas NC6277e832016-08-12 12:29:51 +053036#define SKL_IN_DIR_BIT_MASK BIT(0)
37#define SKL_PIN_COUNT_MASK GENMASK(7, 4)
Jeeja KPf7590d42015-10-07 11:31:53 +010038
Jeeja KPe4e2d2f2015-10-07 11:31:52 +010039/*
40 * SKL DSP driver modelling uses only few DAPM widgets so for rest we will
41 * ignore. This helpers checks if the SKL driver handles this widget type
42 */
43static int is_skl_dsp_widget_type(struct snd_soc_dapm_widget *w)
44{
45 switch (w->id) {
46 case snd_soc_dapm_dai_link:
47 case snd_soc_dapm_dai_in:
48 case snd_soc_dapm_aif_in:
49 case snd_soc_dapm_aif_out:
50 case snd_soc_dapm_dai_out:
51 case snd_soc_dapm_switch:
52 return false;
53 default:
54 return true;
55 }
56}
57
58/*
59 * Each pipelines needs memory to be allocated. Check if we have free memory
Dharageswari.R9ba8ffe2016-02-03 17:59:47 +053060 * from available pool.
Jeeja KPe4e2d2f2015-10-07 11:31:52 +010061 */
Dharageswari.R9ba8ffe2016-02-03 17:59:47 +053062static bool skl_is_pipe_mem_avail(struct skl *skl,
Jeeja KPe4e2d2f2015-10-07 11:31:52 +010063 struct skl_module_cfg *mconfig)
64{
65 struct skl_sst *ctx = skl->skl_sst;
66
67 if (skl->resource.mem + mconfig->pipe->memory_pages >
68 skl->resource.max_mem) {
69 dev_err(ctx->dev,
70 "%s: module_id %d instance %d\n", __func__,
71 mconfig->id.module_id,
72 mconfig->id.instance_id);
73 dev_err(ctx->dev,
74 "exceeds ppl memory available %d mem %d\n",
75 skl->resource.max_mem, skl->resource.mem);
76 return false;
Dharageswari.R9ba8ffe2016-02-03 17:59:47 +053077 } else {
78 return true;
Jeeja KPe4e2d2f2015-10-07 11:31:52 +010079 }
Dharageswari.R9ba8ffe2016-02-03 17:59:47 +053080}
Jeeja KPe4e2d2f2015-10-07 11:31:52 +010081
Dharageswari.R9ba8ffe2016-02-03 17:59:47 +053082/*
83 * Add the mem to the mem pool. This is freed when pipe is deleted.
84 * Note: DSP does actual memory management we only keep track for complete
85 * pool
86 */
87static void skl_tplg_alloc_pipe_mem(struct skl *skl,
88 struct skl_module_cfg *mconfig)
89{
Jeeja KPe4e2d2f2015-10-07 11:31:52 +010090 skl->resource.mem += mconfig->pipe->memory_pages;
Jeeja KPe4e2d2f2015-10-07 11:31:52 +010091}
92
93/*
94 * Pipeline needs needs DSP CPU resources for computation, this is
95 * quantified in MCPS (Million Clocks Per Second) required for module/pipe
96 *
97 * Each pipelines needs mcps to be allocated. Check if we have mcps for this
Dharageswari.R9ba8ffe2016-02-03 17:59:47 +053098 * pipe.
Jeeja KPe4e2d2f2015-10-07 11:31:52 +010099 */
Dharageswari.R9ba8ffe2016-02-03 17:59:47 +0530100
101static bool skl_is_pipe_mcps_avail(struct skl *skl,
Jeeja KPe4e2d2f2015-10-07 11:31:52 +0100102 struct skl_module_cfg *mconfig)
103{
104 struct skl_sst *ctx = skl->skl_sst;
105
106 if (skl->resource.mcps + mconfig->mcps > skl->resource.max_mcps) {
107 dev_err(ctx->dev,
108 "%s: module_id %d instance %d\n", __func__,
109 mconfig->id.module_id, mconfig->id.instance_id);
110 dev_err(ctx->dev,
Guneshwor Singh7ca42f52016-02-03 17:59:46 +0530111 "exceeds ppl mcps available %d > mem %d\n",
Jeeja KPe4e2d2f2015-10-07 11:31:52 +0100112 skl->resource.max_mcps, skl->resource.mcps);
113 return false;
Dharageswari.R9ba8ffe2016-02-03 17:59:47 +0530114 } else {
115 return true;
Jeeja KPe4e2d2f2015-10-07 11:31:52 +0100116 }
Dharageswari.R9ba8ffe2016-02-03 17:59:47 +0530117}
Jeeja KPe4e2d2f2015-10-07 11:31:52 +0100118
Dharageswari.R9ba8ffe2016-02-03 17:59:47 +0530119static void skl_tplg_alloc_pipe_mcps(struct skl *skl,
120 struct skl_module_cfg *mconfig)
121{
Jeeja KPe4e2d2f2015-10-07 11:31:52 +0100122 skl->resource.mcps += mconfig->mcps;
Jeeja KPe4e2d2f2015-10-07 11:31:52 +0100123}
124
125/*
126 * Free the mcps when tearing down
127 */
128static void
129skl_tplg_free_pipe_mcps(struct skl *skl, struct skl_module_cfg *mconfig)
130{
131 skl->resource.mcps -= mconfig->mcps;
132}
133
134/*
135 * Free the memory when tearing down
136 */
137static void
138skl_tplg_free_pipe_mem(struct skl *skl, struct skl_module_cfg *mconfig)
139{
140 skl->resource.mem -= mconfig->pipe->memory_pages;
141}
142
Jeeja KPf7590d42015-10-07 11:31:53 +0100143
144static void skl_dump_mconfig(struct skl_sst *ctx,
145 struct skl_module_cfg *mcfg)
146{
147 dev_dbg(ctx->dev, "Dumping config\n");
148 dev_dbg(ctx->dev, "Input Format:\n");
Hardik T Shah4cd98992015-10-27 09:22:55 +0900149 dev_dbg(ctx->dev, "channels = %d\n", mcfg->in_fmt[0].channels);
150 dev_dbg(ctx->dev, "s_freq = %d\n", mcfg->in_fmt[0].s_freq);
151 dev_dbg(ctx->dev, "ch_cfg = %d\n", mcfg->in_fmt[0].ch_cfg);
152 dev_dbg(ctx->dev, "valid bit depth = %d\n", mcfg->in_fmt[0].valid_bit_depth);
Jeeja KPf7590d42015-10-07 11:31:53 +0100153 dev_dbg(ctx->dev, "Output Format:\n");
Hardik T Shah4cd98992015-10-27 09:22:55 +0900154 dev_dbg(ctx->dev, "channels = %d\n", mcfg->out_fmt[0].channels);
155 dev_dbg(ctx->dev, "s_freq = %d\n", mcfg->out_fmt[0].s_freq);
156 dev_dbg(ctx->dev, "valid bit depth = %d\n", mcfg->out_fmt[0].valid_bit_depth);
157 dev_dbg(ctx->dev, "ch_cfg = %d\n", mcfg->out_fmt[0].ch_cfg);
Jeeja KPf7590d42015-10-07 11:31:53 +0100158}
159
Subhransu S. Prustyea5a1372016-04-14 10:07:36 +0530160static void skl_tplg_update_chmap(struct skl_module_fmt *fmt, int chs)
161{
162 int slot_map = 0xFFFFFFFF;
163 int start_slot = 0;
164 int i;
165
166 for (i = 0; i < chs; i++) {
167 /*
168 * For 2 channels with starting slot as 0, slot map will
169 * look like 0xFFFFFF10.
170 */
171 slot_map &= (~(0xF << (4 * i)) | (start_slot << (4 * i)));
172 start_slot++;
173 }
174 fmt->ch_map = slot_map;
175}
176
Jeeja KPf7590d42015-10-07 11:31:53 +0100177static void skl_tplg_update_params(struct skl_module_fmt *fmt,
178 struct skl_pipe_params *params, int fixup)
179{
180 if (fixup & SKL_RATE_FIXUP_MASK)
181 fmt->s_freq = params->s_freq;
Subhransu S. Prustyea5a1372016-04-14 10:07:36 +0530182 if (fixup & SKL_CH_FIXUP_MASK) {
Jeeja KPf7590d42015-10-07 11:31:53 +0100183 fmt->channels = params->ch;
Subhransu S. Prustyea5a1372016-04-14 10:07:36 +0530184 skl_tplg_update_chmap(fmt, fmt->channels);
185 }
Jeeja KP98256f82015-11-23 22:26:25 +0530186 if (fixup & SKL_FMT_FIXUP_MASK) {
187 fmt->valid_bit_depth = skl_get_bit_depth(params->s_fmt);
188
189 /*
190 * 16 bit is 16 bit container whereas 24 bit is in 32 bit
191 * container so update bit depth accordingly
192 */
193 switch (fmt->valid_bit_depth) {
194 case SKL_DEPTH_16BIT:
195 fmt->bit_depth = fmt->valid_bit_depth;
196 break;
197
198 default:
199 fmt->bit_depth = SKL_DEPTH_32BIT;
200 break;
201 }
202 }
203
Jeeja KPf7590d42015-10-07 11:31:53 +0100204}
205
206/*
207 * A pipeline may have modules which impact the pcm parameters, like SRC,
208 * channel converter, format converter.
209 * We need to calculate the output params by applying the 'fixup'
210 * Topology will tell driver which type of fixup is to be applied by
211 * supplying the fixup mask, so based on that we calculate the output
212 *
213 * Now In FE the pcm hw_params is source/target format. Same is applicable
214 * for BE with its hw_params invoked.
215 * here based on FE, BE pipeline and direction we calculate the input and
216 * outfix and then apply that for a module
217 */
218static void skl_tplg_update_params_fixup(struct skl_module_cfg *m_cfg,
219 struct skl_pipe_params *params, bool is_fe)
220{
221 int in_fixup, out_fixup;
222 struct skl_module_fmt *in_fmt, *out_fmt;
223
Hardik T Shah4cd98992015-10-27 09:22:55 +0900224 /* Fixups will be applied to pin 0 only */
225 in_fmt = &m_cfg->in_fmt[0];
226 out_fmt = &m_cfg->out_fmt[0];
Jeeja KPf7590d42015-10-07 11:31:53 +0100227
228 if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
229 if (is_fe) {
230 in_fixup = m_cfg->params_fixup;
231 out_fixup = (~m_cfg->converter) &
232 m_cfg->params_fixup;
233 } else {
234 out_fixup = m_cfg->params_fixup;
235 in_fixup = (~m_cfg->converter) &
236 m_cfg->params_fixup;
237 }
238 } else {
239 if (is_fe) {
240 out_fixup = m_cfg->params_fixup;
241 in_fixup = (~m_cfg->converter) &
242 m_cfg->params_fixup;
243 } else {
244 in_fixup = m_cfg->params_fixup;
245 out_fixup = (~m_cfg->converter) &
246 m_cfg->params_fixup;
247 }
248 }
249
250 skl_tplg_update_params(in_fmt, params, in_fixup);
251 skl_tplg_update_params(out_fmt, params, out_fixup);
252}
253
254/*
255 * A module needs input and output buffers, which are dependent upon pcm
256 * params, so once we have calculate params, we need buffer calculation as
257 * well.
258 */
259static void skl_tplg_update_buffer_size(struct skl_sst *ctx,
260 struct skl_module_cfg *mcfg)
261{
262 int multiplier = 1;
Hardik T Shah4cd98992015-10-27 09:22:55 +0900263 struct skl_module_fmt *in_fmt, *out_fmt;
Subhransu S. Prustyf0c8e1d2016-04-12 10:31:23 +0530264 int in_rate, out_rate;
Hardik T Shah4cd98992015-10-27 09:22:55 +0900265
266
267 /* Since fixups is applied to pin 0 only, ibs, obs needs
268 * change for pin 0 only
269 */
270 in_fmt = &mcfg->in_fmt[0];
271 out_fmt = &mcfg->out_fmt[0];
Jeeja KPf7590d42015-10-07 11:31:53 +0100272
273 if (mcfg->m_type == SKL_MODULE_TYPE_SRCINT)
274 multiplier = 5;
Jeeja KPf7590d42015-10-07 11:31:53 +0100275
Subhransu S. Prustyf0c8e1d2016-04-12 10:31:23 +0530276 if (in_fmt->s_freq % 1000)
277 in_rate = (in_fmt->s_freq / 1000) + 1;
278 else
279 in_rate = (in_fmt->s_freq / 1000);
280
281 mcfg->ibs = in_rate * (mcfg->in_fmt->channels) *
282 (mcfg->in_fmt->bit_depth >> 3) *
283 multiplier;
284
285 if (mcfg->out_fmt->s_freq % 1000)
286 out_rate = (mcfg->out_fmt->s_freq / 1000) + 1;
287 else
288 out_rate = (mcfg->out_fmt->s_freq / 1000);
289
290 mcfg->obs = out_rate * (mcfg->out_fmt->channels) *
291 (mcfg->out_fmt->bit_depth >> 3) *
292 multiplier;
Jeeja KPf7590d42015-10-07 11:31:53 +0100293}
294
Jeeja KP2d1419a2016-02-05 12:19:10 +0530295static int skl_tplg_update_be_blob(struct snd_soc_dapm_widget *w,
296 struct skl_sst *ctx)
297{
298 struct skl_module_cfg *m_cfg = w->priv;
299 int link_type, dir;
300 u32 ch, s_freq, s_fmt;
301 struct nhlt_specific_cfg *cfg;
302 struct skl *skl = get_skl_ctx(ctx->dev);
303
304 /* check if we already have blob */
305 if (m_cfg->formats_config.caps_size > 0)
306 return 0;
307
Jeeja KPc7c6c732016-03-01 07:59:10 +0530308 dev_dbg(ctx->dev, "Applying default cfg blob\n");
Jeeja KP2d1419a2016-02-05 12:19:10 +0530309 switch (m_cfg->dev_type) {
310 case SKL_DEVICE_DMIC:
311 link_type = NHLT_LINK_DMIC;
Jeeja KPc7c6c732016-03-01 07:59:10 +0530312 dir = SNDRV_PCM_STREAM_CAPTURE;
Jeeja KP2d1419a2016-02-05 12:19:10 +0530313 s_freq = m_cfg->in_fmt[0].s_freq;
314 s_fmt = m_cfg->in_fmt[0].bit_depth;
315 ch = m_cfg->in_fmt[0].channels;
316 break;
317
318 case SKL_DEVICE_I2S:
319 link_type = NHLT_LINK_SSP;
320 if (m_cfg->hw_conn_type == SKL_CONN_SOURCE) {
Jeeja KPc7c6c732016-03-01 07:59:10 +0530321 dir = SNDRV_PCM_STREAM_PLAYBACK;
Jeeja KP2d1419a2016-02-05 12:19:10 +0530322 s_freq = m_cfg->out_fmt[0].s_freq;
323 s_fmt = m_cfg->out_fmt[0].bit_depth;
324 ch = m_cfg->out_fmt[0].channels;
Jeeja KPc7c6c732016-03-01 07:59:10 +0530325 } else {
326 dir = SNDRV_PCM_STREAM_CAPTURE;
327 s_freq = m_cfg->in_fmt[0].s_freq;
328 s_fmt = m_cfg->in_fmt[0].bit_depth;
329 ch = m_cfg->in_fmt[0].channels;
Jeeja KP2d1419a2016-02-05 12:19:10 +0530330 }
331 break;
332
333 default:
334 return -EINVAL;
335 }
336
337 /* update the blob based on virtual bus_id and default params */
338 cfg = skl_get_ep_blob(skl, m_cfg->vbus_id, link_type,
339 s_fmt, ch, s_freq, dir);
340 if (cfg) {
341 m_cfg->formats_config.caps_size = cfg->size;
342 m_cfg->formats_config.caps = (u32 *) &cfg->caps;
343 } else {
344 dev_err(ctx->dev, "Blob NULL for id %x type %d dirn %d\n",
345 m_cfg->vbus_id, link_type, dir);
346 dev_err(ctx->dev, "PCM: ch %d, freq %d, fmt %d\n",
347 ch, s_freq, s_fmt);
348 return -EIO;
349 }
350
351 return 0;
352}
353
Jeeja KPf7590d42015-10-07 11:31:53 +0100354static void skl_tplg_update_module_params(struct snd_soc_dapm_widget *w,
355 struct skl_sst *ctx)
356{
357 struct skl_module_cfg *m_cfg = w->priv;
358 struct skl_pipe_params *params = m_cfg->pipe->p_params;
359 int p_conn_type = m_cfg->pipe->conn_type;
360 bool is_fe;
361
362 if (!m_cfg->params_fixup)
363 return;
364
365 dev_dbg(ctx->dev, "Mconfig for widget=%s BEFORE updation\n",
366 w->name);
367
368 skl_dump_mconfig(ctx, m_cfg);
369
370 if (p_conn_type == SKL_PIPE_CONN_TYPE_FE)
371 is_fe = true;
372 else
373 is_fe = false;
374
375 skl_tplg_update_params_fixup(m_cfg, params, is_fe);
376 skl_tplg_update_buffer_size(ctx, m_cfg);
377
378 dev_dbg(ctx->dev, "Mconfig for widget=%s AFTER updation\n",
379 w->name);
380
381 skl_dump_mconfig(ctx, m_cfg);
382}
383
Jeeja KPe4e2d2f2015-10-07 11:31:52 +0100384/*
Jeeja KPabb74002015-11-28 15:01:49 +0530385 * some modules can have multiple params set from user control and
386 * need to be set after module is initialized. If set_param flag is
387 * set module params will be done after module is initialised.
388 */
389static int skl_tplg_set_module_params(struct snd_soc_dapm_widget *w,
390 struct skl_sst *ctx)
391{
392 int i, ret;
393 struct skl_module_cfg *mconfig = w->priv;
394 const struct snd_kcontrol_new *k;
395 struct soc_bytes_ext *sb;
396 struct skl_algo_data *bc;
397 struct skl_specific_cfg *sp_cfg;
398
399 if (mconfig->formats_config.caps_size > 0 &&
Jeeja KP4ced1822015-12-03 23:29:53 +0530400 mconfig->formats_config.set_params == SKL_PARAM_SET) {
Jeeja KPabb74002015-11-28 15:01:49 +0530401 sp_cfg = &mconfig->formats_config;
402 ret = skl_set_module_params(ctx, sp_cfg->caps,
403 sp_cfg->caps_size,
404 sp_cfg->param_id, mconfig);
405 if (ret < 0)
406 return ret;
407 }
408
409 for (i = 0; i < w->num_kcontrols; i++) {
410 k = &w->kcontrol_news[i];
411 if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
412 sb = (void *) k->private_value;
413 bc = (struct skl_algo_data *)sb->dobj.private;
414
Jeeja KP4ced1822015-12-03 23:29:53 +0530415 if (bc->set_params == SKL_PARAM_SET) {
Jeeja KPabb74002015-11-28 15:01:49 +0530416 ret = skl_set_module_params(ctx,
Dharageswari R0d682102016-07-08 18:15:03 +0530417 (u32 *)bc->params, bc->size,
Jeeja KPabb74002015-11-28 15:01:49 +0530418 bc->param_id, mconfig);
419 if (ret < 0)
420 return ret;
421 }
422 }
423 }
424
425 return 0;
426}
427
428/*
429 * some module param can set from user control and this is required as
430 * when module is initailzed. if module param is required in init it is
431 * identifed by set_param flag. if set_param flag is not set, then this
432 * parameter needs to set as part of module init.
433 */
434static int skl_tplg_set_module_init_data(struct snd_soc_dapm_widget *w)
435{
436 const struct snd_kcontrol_new *k;
437 struct soc_bytes_ext *sb;
438 struct skl_algo_data *bc;
439 struct skl_module_cfg *mconfig = w->priv;
440 int i;
441
442 for (i = 0; i < w->num_kcontrols; i++) {
443 k = &w->kcontrol_news[i];
444 if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
445 sb = (struct soc_bytes_ext *)k->private_value;
446 bc = (struct skl_algo_data *)sb->dobj.private;
447
Jeeja KP4ced1822015-12-03 23:29:53 +0530448 if (bc->set_params != SKL_PARAM_INIT)
Jeeja KPabb74002015-11-28 15:01:49 +0530449 continue;
450
451 mconfig->formats_config.caps = (u32 *)&bc->params;
Dharageswari R0d682102016-07-08 18:15:03 +0530452 mconfig->formats_config.caps_size = bc->size;
Jeeja KPabb74002015-11-28 15:01:49 +0530453
454 break;
455 }
456 }
457
458 return 0;
459}
460
461/*
Jeeja KPe4e2d2f2015-10-07 11:31:52 +0100462 * Inside a pipe instance, we can have various modules. These modules need
463 * to instantiated in DSP by invoking INIT_MODULE IPC, which is achieved by
464 * skl_init_module() routine, so invoke that for all modules in a pipeline
465 */
466static int
467skl_tplg_init_pipe_modules(struct skl *skl, struct skl_pipe *pipe)
468{
469 struct skl_pipe_module *w_module;
470 struct snd_soc_dapm_widget *w;
471 struct skl_module_cfg *mconfig;
472 struct skl_sst *ctx = skl->skl_sst;
473 int ret = 0;
474
475 list_for_each_entry(w_module, &pipe->w_list, node) {
476 w = w_module->w;
477 mconfig = w->priv;
478
Vinod Koulb7c50552016-07-26 18:06:40 +0530479 /* check if module ids are populated */
480 if (mconfig->id.module_id < 0) {
Vinod Koula657ae72016-08-10 09:40:50 +0530481 dev_err(skl->skl_sst->dev,
482 "module %pUL id not populated\n",
483 (uuid_le *)mconfig->guid);
484 return -EIO;
Vinod Koulb7c50552016-07-26 18:06:40 +0530485 }
486
Jeeja KPe4e2d2f2015-10-07 11:31:52 +0100487 /* check resource available */
Dharageswari.R9ba8ffe2016-02-03 17:59:47 +0530488 if (!skl_is_pipe_mcps_avail(skl, mconfig))
Jeeja KPe4e2d2f2015-10-07 11:31:52 +0100489 return -ENOMEM;
490
Dharageswari R6c5768b2015-12-03 23:29:50 +0530491 if (mconfig->is_loadable && ctx->dsp->fw_ops.load_mod) {
492 ret = ctx->dsp->fw_ops.load_mod(ctx->dsp,
493 mconfig->id.module_id, mconfig->guid);
494 if (ret < 0)
495 return ret;
Jeeja KPd6436782016-03-28 22:11:30 +0530496
497 mconfig->m_state = SKL_MODULE_LOADED;
Dharageswari R6c5768b2015-12-03 23:29:50 +0530498 }
499
Jeeja KP2d1419a2016-02-05 12:19:10 +0530500 /* update blob if blob is null for be with default value */
501 skl_tplg_update_be_blob(w, ctx);
502
Jeeja KPf7590d42015-10-07 11:31:53 +0100503 /*
504 * apply fix/conversion to module params based on
505 * FE/BE params
506 */
507 skl_tplg_update_module_params(w, ctx);
Dharageswari Ref2a3522016-09-22 14:00:38 +0530508 mconfig->id.pvt_id = skl_get_pvt_id(ctx, mconfig);
509 if (mconfig->id.pvt_id < 0)
510 return ret;
Jeeja KPabb74002015-11-28 15:01:49 +0530511 skl_tplg_set_module_init_data(w);
Jeeja KP9939a9c2015-11-28 15:01:47 +0530512 ret = skl_init_module(ctx, mconfig);
Dharageswari Ref2a3522016-09-22 14:00:38 +0530513 if (ret < 0) {
514 skl_put_pvt_id(ctx, mconfig);
Jeeja KPe4e2d2f2015-10-07 11:31:52 +0100515 return ret;
Dharageswari Ref2a3522016-09-22 14:00:38 +0530516 }
Dharageswari R260eb732016-06-03 18:29:38 +0530517 skl_tplg_alloc_pipe_mcps(skl, mconfig);
Jeeja KPabb74002015-11-28 15:01:49 +0530518 ret = skl_tplg_set_module_params(w, ctx);
Jeeja KPe4e2d2f2015-10-07 11:31:52 +0100519 if (ret < 0)
520 return ret;
521 }
522
523 return 0;
524}
Vinod Kould93f8e52015-10-07 11:31:54 +0100525
Dharageswari R6c5768b2015-12-03 23:29:50 +0530526static int skl_tplg_unload_pipe_modules(struct skl_sst *ctx,
527 struct skl_pipe *pipe)
528{
Dharageswari Rb0fab9c2016-08-24 18:03:16 +0530529 int ret;
Dharageswari R6c5768b2015-12-03 23:29:50 +0530530 struct skl_pipe_module *w_module = NULL;
531 struct skl_module_cfg *mconfig = NULL;
532
533 list_for_each_entry(w_module, &pipe->w_list, node) {
534 mconfig = w_module->w->priv;
535
Jeeja KPd6436782016-03-28 22:11:30 +0530536 if (mconfig->is_loadable && ctx->dsp->fw_ops.unload_mod &&
Dharageswari Rb0fab9c2016-08-24 18:03:16 +0530537 mconfig->m_state > SKL_MODULE_UNINIT) {
538 ret = ctx->dsp->fw_ops.unload_mod(ctx->dsp,
Dharageswari R6c5768b2015-12-03 23:29:50 +0530539 mconfig->id.module_id);
Dharageswari Rb0fab9c2016-08-24 18:03:16 +0530540 if (ret < 0)
541 return -EIO;
542 }
Dharageswari Ref2a3522016-09-22 14:00:38 +0530543 skl_put_pvt_id(ctx, mconfig);
Dharageswari R6c5768b2015-12-03 23:29:50 +0530544 }
545
546 /* no modules to unload in this path, so return */
547 return 0;
548}
549
Vinod Kould93f8e52015-10-07 11:31:54 +0100550/*
551 * Mixer module represents a pipeline. So in the Pre-PMU event of mixer we
552 * need create the pipeline. So we do following:
553 * - check the resources
554 * - Create the pipeline
555 * - Initialize the modules in pipeline
556 * - finally bind all modules together
557 */
558static int skl_tplg_mixer_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
559 struct skl *skl)
560{
561 int ret;
562 struct skl_module_cfg *mconfig = w->priv;
563 struct skl_pipe_module *w_module;
564 struct skl_pipe *s_pipe = mconfig->pipe;
565 struct skl_module_cfg *src_module = NULL, *dst_module;
566 struct skl_sst *ctx = skl->skl_sst;
567
568 /* check resource available */
Dharageswari.R9ba8ffe2016-02-03 17:59:47 +0530569 if (!skl_is_pipe_mcps_avail(skl, mconfig))
Vinod Kould93f8e52015-10-07 11:31:54 +0100570 return -EBUSY;
571
Dharageswari.R9ba8ffe2016-02-03 17:59:47 +0530572 if (!skl_is_pipe_mem_avail(skl, mconfig))
Vinod Kould93f8e52015-10-07 11:31:54 +0100573 return -ENOMEM;
574
575 /*
576 * Create a list of modules for pipe.
577 * This list contains modules from source to sink
578 */
579 ret = skl_create_pipeline(ctx, mconfig->pipe);
580 if (ret < 0)
581 return ret;
582
Dharageswari R260eb732016-06-03 18:29:38 +0530583 skl_tplg_alloc_pipe_mem(skl, mconfig);
584 skl_tplg_alloc_pipe_mcps(skl, mconfig);
Vinod Kould93f8e52015-10-07 11:31:54 +0100585
586 /* Init all pipe modules from source to sink */
587 ret = skl_tplg_init_pipe_modules(skl, s_pipe);
588 if (ret < 0)
589 return ret;
590
591 /* Bind modules from source to sink */
592 list_for_each_entry(w_module, &s_pipe->w_list, node) {
593 dst_module = w_module->w->priv;
594
595 if (src_module == NULL) {
596 src_module = dst_module;
597 continue;
598 }
599
600 ret = skl_bind_modules(ctx, src_module, dst_module);
601 if (ret < 0)
602 return ret;
603
604 src_module = dst_module;
605 }
606
607 return 0;
608}
609
Dharageswari R5e8f0ee2016-09-22 14:00:40 +0530610static int skl_fill_sink_instance_id(struct skl_sst *ctx,
611 struct skl_algo_data *alg_data)
612{
613 struct skl_kpb_params *params = (struct skl_kpb_params *)alg_data->params;
614 struct skl_mod_inst_map *inst;
615 int i, pvt_id;
616
617 inst = params->map;
618
619 for (i = 0; i < params->num_modules; i++) {
620 pvt_id = skl_get_pvt_instance_id_map(ctx,
621 inst->mod_id, inst->inst_id);
622 if (pvt_id < 0)
623 return -EINVAL;
624 inst->inst_id = pvt_id;
625 inst++;
626 }
627 return 0;
628}
629
Jeeja KPcc6a4042016-02-05 12:19:08 +0530630/*
631 * Some modules require params to be set after the module is bound to
632 * all pins connected.
633 *
634 * The module provider initializes set_param flag for such modules and we
635 * send params after binding
636 */
637static int skl_tplg_set_module_bind_params(struct snd_soc_dapm_widget *w,
638 struct skl_module_cfg *mcfg, struct skl_sst *ctx)
639{
640 int i, ret;
641 struct skl_module_cfg *mconfig = w->priv;
642 const struct snd_kcontrol_new *k;
643 struct soc_bytes_ext *sb;
644 struct skl_algo_data *bc;
645 struct skl_specific_cfg *sp_cfg;
646
647 /*
648 * check all out/in pins are in bind state.
649 * if so set the module param
650 */
651 for (i = 0; i < mcfg->max_out_queue; i++) {
652 if (mcfg->m_out_pin[i].pin_state != SKL_PIN_BIND_DONE)
653 return 0;
654 }
655
656 for (i = 0; i < mcfg->max_in_queue; i++) {
657 if (mcfg->m_in_pin[i].pin_state != SKL_PIN_BIND_DONE)
658 return 0;
659 }
660
661 if (mconfig->formats_config.caps_size > 0 &&
662 mconfig->formats_config.set_params == SKL_PARAM_BIND) {
663 sp_cfg = &mconfig->formats_config;
664 ret = skl_set_module_params(ctx, sp_cfg->caps,
665 sp_cfg->caps_size,
666 sp_cfg->param_id, mconfig);
667 if (ret < 0)
668 return ret;
669 }
670
671 for (i = 0; i < w->num_kcontrols; i++) {
672 k = &w->kcontrol_news[i];
673 if (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
674 sb = (void *) k->private_value;
675 bc = (struct skl_algo_data *)sb->dobj.private;
676
677 if (bc->set_params == SKL_PARAM_BIND) {
Dharageswari R5e8f0ee2016-09-22 14:00:40 +0530678 if (mconfig->m_type == SKL_MODULE_TYPE_KPB)
679 skl_fill_sink_instance_id(ctx, bc);
Jeeja KPcc6a4042016-02-05 12:19:08 +0530680 ret = skl_set_module_params(ctx,
681 (u32 *)bc->params, bc->max,
682 bc->param_id, mconfig);
683 if (ret < 0)
684 return ret;
685 }
686 }
687 }
688
689 return 0;
690}
691
Jeeja KP8724ff12015-10-27 09:22:52 +0900692static int skl_tplg_bind_sinks(struct snd_soc_dapm_widget *w,
693 struct skl *skl,
Jeeja KP6bd4cf82016-02-03 17:59:51 +0530694 struct snd_soc_dapm_widget *src_w,
Jeeja KP8724ff12015-10-27 09:22:52 +0900695 struct skl_module_cfg *src_mconfig)
Vinod Kould93f8e52015-10-07 11:31:54 +0100696{
697 struct snd_soc_dapm_path *p;
Jeeja KP0ed95d72015-11-13 19:22:11 +0530698 struct snd_soc_dapm_widget *sink = NULL, *next_sink = NULL;
Jeeja KP8724ff12015-10-27 09:22:52 +0900699 struct skl_module_cfg *sink_mconfig;
Vinod Kould93f8e52015-10-07 11:31:54 +0100700 struct skl_sst *ctx = skl->skl_sst;
Jeeja KP8724ff12015-10-27 09:22:52 +0900701 int ret;
Vinod Kould93f8e52015-10-07 11:31:54 +0100702
Jeeja KP8724ff12015-10-27 09:22:52 +0900703 snd_soc_dapm_widget_for_each_sink_path(w, p) {
Vinod Kould93f8e52015-10-07 11:31:54 +0100704 if (!p->connect)
705 continue;
706
707 dev_dbg(ctx->dev, "%s: src widget=%s\n", __func__, w->name);
708 dev_dbg(ctx->dev, "%s: sink widget=%s\n", __func__, p->sink->name);
709
Jeeja KP0ed95d72015-11-13 19:22:11 +0530710 next_sink = p->sink;
Jeeja KP6bd4cf82016-02-03 17:59:51 +0530711
712 if (!is_skl_dsp_widget_type(p->sink))
713 return skl_tplg_bind_sinks(p->sink, skl, src_w, src_mconfig);
714
Vinod Kould93f8e52015-10-07 11:31:54 +0100715 /*
716 * here we will check widgets in sink pipelines, so that
717 * can be any widgets type and we are only interested if
718 * they are ones used for SKL so check that first
719 */
720 if ((p->sink->priv != NULL) &&
721 is_skl_dsp_widget_type(p->sink)) {
722
723 sink = p->sink;
Vinod Kould93f8e52015-10-07 11:31:54 +0100724 sink_mconfig = sink->priv;
725
Jeeja KPcc6a4042016-02-05 12:19:08 +0530726 if (src_mconfig->m_state == SKL_MODULE_UNINIT ||
727 sink_mconfig->m_state == SKL_MODULE_UNINIT)
728 continue;
729
Vinod Kould93f8e52015-10-07 11:31:54 +0100730 /* Bind source to sink, mixin is always source */
731 ret = skl_bind_modules(ctx, src_mconfig, sink_mconfig);
732 if (ret)
733 return ret;
734
Jeeja KPcc6a4042016-02-05 12:19:08 +0530735 /* set module params after bind */
736 skl_tplg_set_module_bind_params(src_w, src_mconfig, ctx);
737 skl_tplg_set_module_bind_params(sink, sink_mconfig, ctx);
738
Vinod Kould93f8e52015-10-07 11:31:54 +0100739 /* Start sinks pipe first */
740 if (sink_mconfig->pipe->state != SKL_PIPE_STARTED) {
Jeeja KPd1730c32015-10-27 09:22:53 +0900741 if (sink_mconfig->pipe->conn_type !=
742 SKL_PIPE_CONN_TYPE_FE)
743 ret = skl_run_pipe(ctx,
744 sink_mconfig->pipe);
Vinod Kould93f8e52015-10-07 11:31:54 +0100745 if (ret)
746 return ret;
747 }
Vinod Kould93f8e52015-10-07 11:31:54 +0100748 }
749 }
750
Jeeja KP8724ff12015-10-27 09:22:52 +0900751 if (!sink)
Jeeja KP6bd4cf82016-02-03 17:59:51 +0530752 return skl_tplg_bind_sinks(next_sink, skl, src_w, src_mconfig);
Jeeja KP8724ff12015-10-27 09:22:52 +0900753
754 return 0;
755}
756
Vinod Kould93f8e52015-10-07 11:31:54 +0100757/*
758 * A PGA represents a module in a pipeline. So in the Pre-PMU event of PGA
759 * we need to do following:
760 * - Bind to sink pipeline
761 * Since the sink pipes can be running and we don't get mixer event on
762 * connect for already running mixer, we need to find the sink pipes
763 * here and bind to them. This way dynamic connect works.
764 * - Start sink pipeline, if not running
765 * - Then run current pipe
766 */
767static int skl_tplg_pga_dapm_pre_pmu_event(struct snd_soc_dapm_widget *w,
Jeeja KP8724ff12015-10-27 09:22:52 +0900768 struct skl *skl)
Vinod Kould93f8e52015-10-07 11:31:54 +0100769{
Jeeja KP8724ff12015-10-27 09:22:52 +0900770 struct skl_module_cfg *src_mconfig;
Vinod Kould93f8e52015-10-07 11:31:54 +0100771 struct skl_sst *ctx = skl->skl_sst;
772 int ret = 0;
773
Jeeja KP8724ff12015-10-27 09:22:52 +0900774 src_mconfig = w->priv;
Vinod Kould93f8e52015-10-07 11:31:54 +0100775
776 /*
777 * find which sink it is connected to, bind with the sink,
778 * if sink is not started, start sink pipe first, then start
779 * this pipe
780 */
Jeeja KP6bd4cf82016-02-03 17:59:51 +0530781 ret = skl_tplg_bind_sinks(w, skl, w, src_mconfig);
Vinod Kould93f8e52015-10-07 11:31:54 +0100782 if (ret)
783 return ret;
784
Vinod Kould93f8e52015-10-07 11:31:54 +0100785 /* Start source pipe last after starting all sinks */
Jeeja KPd1730c32015-10-27 09:22:53 +0900786 if (src_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
787 return skl_run_pipe(ctx, src_mconfig->pipe);
Vinod Kould93f8e52015-10-07 11:31:54 +0100788
789 return 0;
790}
791
Jeeja KP8724ff12015-10-27 09:22:52 +0900792static struct snd_soc_dapm_widget *skl_get_src_dsp_widget(
793 struct snd_soc_dapm_widget *w, struct skl *skl)
794{
795 struct snd_soc_dapm_path *p;
796 struct snd_soc_dapm_widget *src_w = NULL;
797 struct skl_sst *ctx = skl->skl_sst;
798
799 snd_soc_dapm_widget_for_each_source_path(w, p) {
800 src_w = p->source;
801 if (!p->connect)
802 continue;
803
804 dev_dbg(ctx->dev, "sink widget=%s\n", w->name);
805 dev_dbg(ctx->dev, "src widget=%s\n", p->source->name);
806
807 /*
808 * here we will check widgets in sink pipelines, so that can
809 * be any widgets type and we are only interested if they are
810 * ones used for SKL so check that first
811 */
812 if ((p->source->priv != NULL) &&
813 is_skl_dsp_widget_type(p->source)) {
814 return p->source;
815 }
816 }
817
818 if (src_w != NULL)
819 return skl_get_src_dsp_widget(src_w, skl);
820
821 return NULL;
822}
823
Vinod Kould93f8e52015-10-07 11:31:54 +0100824/*
825 * in the Post-PMU event of mixer we need to do following:
826 * - Check if this pipe is running
827 * - if not, then
828 * - bind this pipeline to its source pipeline
829 * if source pipe is already running, this means it is a dynamic
830 * connection and we need to bind only to that pipe
831 * - start this pipeline
832 */
833static int skl_tplg_mixer_dapm_post_pmu_event(struct snd_soc_dapm_widget *w,
834 struct skl *skl)
835{
836 int ret = 0;
Vinod Kould93f8e52015-10-07 11:31:54 +0100837 struct snd_soc_dapm_widget *source, *sink;
838 struct skl_module_cfg *src_mconfig, *sink_mconfig;
839 struct skl_sst *ctx = skl->skl_sst;
840 int src_pipe_started = 0;
841
842 sink = w;
843 sink_mconfig = sink->priv;
844
845 /*
846 * If source pipe is already started, that means source is driving
847 * one more sink before this sink got connected, Since source is
848 * started, bind this sink to source and start this pipe.
849 */
Jeeja KP8724ff12015-10-27 09:22:52 +0900850 source = skl_get_src_dsp_widget(w, skl);
851 if (source != NULL) {
852 src_mconfig = source->priv;
853 sink_mconfig = sink->priv;
854 src_pipe_started = 1;
Vinod Kould93f8e52015-10-07 11:31:54 +0100855
856 /*
Jeeja KP8724ff12015-10-27 09:22:52 +0900857 * check pipe state, then no need to bind or start the
858 * pipe
Vinod Kould93f8e52015-10-07 11:31:54 +0100859 */
Jeeja KP8724ff12015-10-27 09:22:52 +0900860 if (src_mconfig->pipe->state != SKL_PIPE_STARTED)
861 src_pipe_started = 0;
Vinod Kould93f8e52015-10-07 11:31:54 +0100862 }
863
864 if (src_pipe_started) {
865 ret = skl_bind_modules(ctx, src_mconfig, sink_mconfig);
866 if (ret)
867 return ret;
868
Jeeja KPcc6a4042016-02-05 12:19:08 +0530869 /* set module params after bind */
870 skl_tplg_set_module_bind_params(source, src_mconfig, ctx);
871 skl_tplg_set_module_bind_params(sink, sink_mconfig, ctx);
872
Jeeja KPd1730c32015-10-27 09:22:53 +0900873 if (sink_mconfig->pipe->conn_type != SKL_PIPE_CONN_TYPE_FE)
874 ret = skl_run_pipe(ctx, sink_mconfig->pipe);
Vinod Kould93f8e52015-10-07 11:31:54 +0100875 }
876
877 return ret;
878}
879
880/*
881 * in the Pre-PMD event of mixer we need to do following:
882 * - Stop the pipe
883 * - find the source connections and remove that from dapm_path_list
884 * - unbind with source pipelines if still connected
885 */
886static int skl_tplg_mixer_dapm_pre_pmd_event(struct snd_soc_dapm_widget *w,
887 struct skl *skl)
888{
Vinod Kould93f8e52015-10-07 11:31:54 +0100889 struct skl_module_cfg *src_mconfig, *sink_mconfig;
Jeeja KPce1b5552015-10-27 09:22:51 +0900890 int ret = 0, i;
Vinod Kould93f8e52015-10-07 11:31:54 +0100891 struct skl_sst *ctx = skl->skl_sst;
892
Jeeja KPce1b5552015-10-27 09:22:51 +0900893 sink_mconfig = w->priv;
Vinod Kould93f8e52015-10-07 11:31:54 +0100894
895 /* Stop the pipe */
896 ret = skl_stop_pipe(ctx, sink_mconfig->pipe);
897 if (ret)
898 return ret;
899
Jeeja KPce1b5552015-10-27 09:22:51 +0900900 for (i = 0; i < sink_mconfig->max_in_queue; i++) {
901 if (sink_mconfig->m_in_pin[i].pin_state == SKL_PIN_BIND_DONE) {
902 src_mconfig = sink_mconfig->m_in_pin[i].tgt_mcfg;
903 if (!src_mconfig)
904 continue;
905 /*
906 * If path_found == 1, that means pmd for source
907 * pipe has not occurred, source is connected to
908 * some other sink. so its responsibility of sink
909 * to unbind itself from source.
910 */
911 ret = skl_stop_pipe(ctx, src_mconfig->pipe);
912 if (ret < 0)
913 return ret;
Vinod Kould93f8e52015-10-07 11:31:54 +0100914
Jeeja KPce1b5552015-10-27 09:22:51 +0900915 ret = skl_unbind_modules(ctx,
916 src_mconfig, sink_mconfig);
Vinod Kould93f8e52015-10-07 11:31:54 +0100917 }
918 }
919
Vinod Kould93f8e52015-10-07 11:31:54 +0100920 return ret;
921}
922
923/*
924 * in the Post-PMD event of mixer we need to do following:
925 * - Free the mcps used
926 * - Free the mem used
927 * - Unbind the modules within the pipeline
928 * - Delete the pipeline (modules are not required to be explicitly
929 * deleted, pipeline delete is enough here
930 */
931static int skl_tplg_mixer_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
932 struct skl *skl)
933{
934 struct skl_module_cfg *mconfig = w->priv;
935 struct skl_pipe_module *w_module;
936 struct skl_module_cfg *src_module = NULL, *dst_module;
937 struct skl_sst *ctx = skl->skl_sst;
938 struct skl_pipe *s_pipe = mconfig->pipe;
939 int ret = 0;
940
Dharageswari R260eb732016-06-03 18:29:38 +0530941 if (s_pipe->state == SKL_PIPE_INVALID)
942 return -EINVAL;
943
Vinod Kould93f8e52015-10-07 11:31:54 +0100944 skl_tplg_free_pipe_mcps(skl, mconfig);
Vinod Koul65976872015-11-23 22:26:29 +0530945 skl_tplg_free_pipe_mem(skl, mconfig);
Vinod Kould93f8e52015-10-07 11:31:54 +0100946
947 list_for_each_entry(w_module, &s_pipe->w_list, node) {
948 dst_module = w_module->w->priv;
949
Dharageswari R260eb732016-06-03 18:29:38 +0530950 if (mconfig->m_state >= SKL_MODULE_INIT_DONE)
951 skl_tplg_free_pipe_mcps(skl, dst_module);
Vinod Kould93f8e52015-10-07 11:31:54 +0100952 if (src_module == NULL) {
953 src_module = dst_module;
954 continue;
955 }
956
Guneshwor Singh7ca42f52016-02-03 17:59:46 +0530957 skl_unbind_modules(ctx, src_module, dst_module);
Vinod Kould93f8e52015-10-07 11:31:54 +0100958 src_module = dst_module;
959 }
960
961 ret = skl_delete_pipe(ctx, mconfig->pipe);
Vinod Kould93f8e52015-10-07 11:31:54 +0100962
Dharageswari R6c5768b2015-12-03 23:29:50 +0530963 return skl_tplg_unload_pipe_modules(ctx, s_pipe);
Vinod Kould93f8e52015-10-07 11:31:54 +0100964}
965
966/*
967 * in the Post-PMD event of PGA we need to do following:
968 * - Free the mcps used
969 * - Stop the pipeline
970 * - In source pipe is connected, unbind with source pipelines
971 */
972static int skl_tplg_pga_dapm_post_pmd_event(struct snd_soc_dapm_widget *w,
973 struct skl *skl)
974{
Vinod Kould93f8e52015-10-07 11:31:54 +0100975 struct skl_module_cfg *src_mconfig, *sink_mconfig;
Jeeja KPce1b5552015-10-27 09:22:51 +0900976 int ret = 0, i;
Vinod Kould93f8e52015-10-07 11:31:54 +0100977 struct skl_sst *ctx = skl->skl_sst;
978
Jeeja KPce1b5552015-10-27 09:22:51 +0900979 src_mconfig = w->priv;
Vinod Kould93f8e52015-10-07 11:31:54 +0100980
Vinod Kould93f8e52015-10-07 11:31:54 +0100981 /* Stop the pipe since this is a mixin module */
982 ret = skl_stop_pipe(ctx, src_mconfig->pipe);
983 if (ret)
984 return ret;
985
Jeeja KPce1b5552015-10-27 09:22:51 +0900986 for (i = 0; i < src_mconfig->max_out_queue; i++) {
987 if (src_mconfig->m_out_pin[i].pin_state == SKL_PIN_BIND_DONE) {
988 sink_mconfig = src_mconfig->m_out_pin[i].tgt_mcfg;
989 if (!sink_mconfig)
990 continue;
991 /*
992 * This is a connecter and if path is found that means
993 * unbind between source and sink has not happened yet
994 */
Jeeja KPce1b5552015-10-27 09:22:51 +0900995 ret = skl_unbind_modules(ctx, src_mconfig,
996 sink_mconfig);
Vinod Kould93f8e52015-10-07 11:31:54 +0100997 }
998 }
999
Vinod Kould93f8e52015-10-07 11:31:54 +01001000 return ret;
1001}
1002
1003/*
1004 * In modelling, we assume there will be ONLY one mixer in a pipeline. If
1005 * mixer is not required then it is treated as static mixer aka vmixer with
1006 * a hard path to source module
1007 * So we don't need to check if source is started or not as hard path puts
1008 * dependency on each other
1009 */
1010static int skl_tplg_vmixer_event(struct snd_soc_dapm_widget *w,
1011 struct snd_kcontrol *k, int event)
1012{
1013 struct snd_soc_dapm_context *dapm = w->dapm;
1014 struct skl *skl = get_skl_ctx(dapm->dev);
1015
1016 switch (event) {
1017 case SND_SOC_DAPM_PRE_PMU:
1018 return skl_tplg_mixer_dapm_pre_pmu_event(w, skl);
1019
Jeeja KPde1fedf2016-02-03 17:59:52 +05301020 case SND_SOC_DAPM_POST_PMU:
1021 return skl_tplg_mixer_dapm_post_pmu_event(w, skl);
1022
1023 case SND_SOC_DAPM_PRE_PMD:
1024 return skl_tplg_mixer_dapm_pre_pmd_event(w, skl);
1025
Vinod Kould93f8e52015-10-07 11:31:54 +01001026 case SND_SOC_DAPM_POST_PMD:
1027 return skl_tplg_mixer_dapm_post_pmd_event(w, skl);
1028 }
1029
1030 return 0;
1031}
1032
1033/*
1034 * In modelling, we assume there will be ONLY one mixer in a pipeline. If a
1035 * second one is required that is created as another pipe entity.
1036 * The mixer is responsible for pipe management and represent a pipeline
1037 * instance
1038 */
1039static int skl_tplg_mixer_event(struct snd_soc_dapm_widget *w,
1040 struct snd_kcontrol *k, int event)
1041{
1042 struct snd_soc_dapm_context *dapm = w->dapm;
1043 struct skl *skl = get_skl_ctx(dapm->dev);
1044
1045 switch (event) {
1046 case SND_SOC_DAPM_PRE_PMU:
1047 return skl_tplg_mixer_dapm_pre_pmu_event(w, skl);
1048
1049 case SND_SOC_DAPM_POST_PMU:
1050 return skl_tplg_mixer_dapm_post_pmu_event(w, skl);
1051
1052 case SND_SOC_DAPM_PRE_PMD:
1053 return skl_tplg_mixer_dapm_pre_pmd_event(w, skl);
1054
1055 case SND_SOC_DAPM_POST_PMD:
1056 return skl_tplg_mixer_dapm_post_pmd_event(w, skl);
1057 }
1058
1059 return 0;
1060}
1061
1062/*
1063 * In modelling, we assumed rest of the modules in pipeline are PGA. But we
1064 * are interested in last PGA (leaf PGA) in a pipeline to disconnect with
1065 * the sink when it is running (two FE to one BE or one FE to two BE)
1066 * scenarios
1067 */
1068static int skl_tplg_pga_event(struct snd_soc_dapm_widget *w,
1069 struct snd_kcontrol *k, int event)
1070
1071{
1072 struct snd_soc_dapm_context *dapm = w->dapm;
1073 struct skl *skl = get_skl_ctx(dapm->dev);
1074
1075 switch (event) {
1076 case SND_SOC_DAPM_PRE_PMU:
1077 return skl_tplg_pga_dapm_pre_pmu_event(w, skl);
1078
1079 case SND_SOC_DAPM_POST_PMD:
1080 return skl_tplg_pga_dapm_post_pmd_event(w, skl);
1081 }
1082
1083 return 0;
1084}
Vinod Koulcfb0a872015-10-07 11:31:55 +01001085
Jeeja KP140adfb2015-11-28 15:01:50 +05301086static int skl_tplg_tlv_control_get(struct snd_kcontrol *kcontrol,
1087 unsigned int __user *data, unsigned int size)
1088{
1089 struct soc_bytes_ext *sb =
1090 (struct soc_bytes_ext *)kcontrol->private_value;
1091 struct skl_algo_data *bc = (struct skl_algo_data *)sb->dobj.private;
Omair M Abdullah7d9f2912015-12-03 23:29:56 +05301092 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1093 struct skl_module_cfg *mconfig = w->priv;
1094 struct skl *skl = get_skl_ctx(w->dapm->dev);
1095
1096 if (w->power)
1097 skl_get_module_params(skl->skl_sst, (u32 *)bc->params,
Dharageswari R0d682102016-07-08 18:15:03 +05301098 bc->size, bc->param_id, mconfig);
Jeeja KP140adfb2015-11-28 15:01:50 +05301099
Vinod Koul41556f62016-02-03 17:59:44 +05301100 /* decrement size for TLV header */
1101 size -= 2 * sizeof(u32);
1102
1103 /* check size as we don't want to send kernel data */
1104 if (size > bc->max)
1105 size = bc->max;
1106
Jeeja KP140adfb2015-11-28 15:01:50 +05301107 if (bc->params) {
1108 if (copy_to_user(data, &bc->param_id, sizeof(u32)))
1109 return -EFAULT;
Dan Carpentere8bc3c92015-12-08 08:53:22 +03001110 if (copy_to_user(data + 1, &size, sizeof(u32)))
Jeeja KP140adfb2015-11-28 15:01:50 +05301111 return -EFAULT;
Dan Carpentere8bc3c92015-12-08 08:53:22 +03001112 if (copy_to_user(data + 2, bc->params, size))
Jeeja KP140adfb2015-11-28 15:01:50 +05301113 return -EFAULT;
1114 }
1115
1116 return 0;
1117}
1118
1119#define SKL_PARAM_VENDOR_ID 0xff
1120
1121static int skl_tplg_tlv_control_set(struct snd_kcontrol *kcontrol,
1122 const unsigned int __user *data, unsigned int size)
1123{
1124 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
1125 struct skl_module_cfg *mconfig = w->priv;
1126 struct soc_bytes_ext *sb =
1127 (struct soc_bytes_ext *)kcontrol->private_value;
1128 struct skl_algo_data *ac = (struct skl_algo_data *)sb->dobj.private;
1129 struct skl *skl = get_skl_ctx(w->dapm->dev);
1130
1131 if (ac->params) {
Dharageswari R0d682102016-07-08 18:15:03 +05301132 if (size > ac->max)
1133 return -EINVAL;
1134
1135 ac->size = size;
Jeeja KP140adfb2015-11-28 15:01:50 +05301136 /*
1137 * if the param_is is of type Vendor, firmware expects actual
1138 * parameter id and size from the control.
1139 */
1140 if (ac->param_id == SKL_PARAM_VENDOR_ID) {
1141 if (copy_from_user(ac->params, data, size))
1142 return -EFAULT;
1143 } else {
1144 if (copy_from_user(ac->params,
Alan65b4bcb2016-02-19 11:42:32 +05301145 data + 2, size))
Jeeja KP140adfb2015-11-28 15:01:50 +05301146 return -EFAULT;
1147 }
1148
1149 if (w->power)
1150 return skl_set_module_params(skl->skl_sst,
Dharageswari R0d682102016-07-08 18:15:03 +05301151 (u32 *)ac->params, ac->size,
Jeeja KP140adfb2015-11-28 15:01:50 +05301152 ac->param_id, mconfig);
1153 }
1154
1155 return 0;
1156}
1157
Vinod Koulcfb0a872015-10-07 11:31:55 +01001158/*
Jeeja KP8871dcb2016-06-03 18:29:42 +05301159 * Fill the dma id for host and link. In case of passthrough
1160 * pipeline, this will both host and link in the same
1161 * pipeline, so need to copy the link and host based on dev_type
1162 */
1163static void skl_tplg_fill_dma_id(struct skl_module_cfg *mcfg,
1164 struct skl_pipe_params *params)
1165{
1166 struct skl_pipe *pipe = mcfg->pipe;
1167
1168 if (pipe->passthru) {
1169 switch (mcfg->dev_type) {
1170 case SKL_DEVICE_HDALINK:
1171 pipe->p_params->link_dma_id = params->link_dma_id;
1172 break;
1173
1174 case SKL_DEVICE_HDAHOST:
1175 pipe->p_params->host_dma_id = params->host_dma_id;
1176 break;
1177
1178 default:
1179 break;
1180 }
1181 pipe->p_params->s_fmt = params->s_fmt;
1182 pipe->p_params->ch = params->ch;
1183 pipe->p_params->s_freq = params->s_freq;
1184 pipe->p_params->stream = params->stream;
1185
1186 } else {
1187 memcpy(pipe->p_params, params, sizeof(*params));
1188 }
1189}
1190
1191/*
Vinod Koulcfb0a872015-10-07 11:31:55 +01001192 * The FE params are passed by hw_params of the DAI.
1193 * On hw_params, the params are stored in Gateway module of the FE and we
1194 * need to calculate the format in DSP module configuration, that
1195 * conversion is done here
1196 */
1197int skl_tplg_update_pipe_params(struct device *dev,
1198 struct skl_module_cfg *mconfig,
1199 struct skl_pipe_params *params)
1200{
Vinod Koulcfb0a872015-10-07 11:31:55 +01001201 struct skl_module_fmt *format = NULL;
1202
Jeeja KP8871dcb2016-06-03 18:29:42 +05301203 skl_tplg_fill_dma_id(mconfig, params);
Vinod Koulcfb0a872015-10-07 11:31:55 +01001204
1205 if (params->stream == SNDRV_PCM_STREAM_PLAYBACK)
Hardik T Shah4cd98992015-10-27 09:22:55 +09001206 format = &mconfig->in_fmt[0];
Vinod Koulcfb0a872015-10-07 11:31:55 +01001207 else
Hardik T Shah4cd98992015-10-27 09:22:55 +09001208 format = &mconfig->out_fmt[0];
Vinod Koulcfb0a872015-10-07 11:31:55 +01001209
1210 /* set the hw_params */
1211 format->s_freq = params->s_freq;
1212 format->channels = params->ch;
1213 format->valid_bit_depth = skl_get_bit_depth(params->s_fmt);
1214
1215 /*
1216 * 16 bit is 16 bit container whereas 24 bit is in 32 bit
1217 * container so update bit depth accordingly
1218 */
1219 switch (format->valid_bit_depth) {
1220 case SKL_DEPTH_16BIT:
1221 format->bit_depth = format->valid_bit_depth;
1222 break;
1223
1224 case SKL_DEPTH_24BIT:
Jeeja KP6654f392015-10-27 09:22:46 +09001225 case SKL_DEPTH_32BIT:
Vinod Koulcfb0a872015-10-07 11:31:55 +01001226 format->bit_depth = SKL_DEPTH_32BIT;
1227 break;
1228
1229 default:
1230 dev_err(dev, "Invalid bit depth %x for pipe\n",
1231 format->valid_bit_depth);
1232 return -EINVAL;
1233 }
1234
1235 if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1236 mconfig->ibs = (format->s_freq / 1000) *
1237 (format->channels) *
1238 (format->bit_depth >> 3);
1239 } else {
1240 mconfig->obs = (format->s_freq / 1000) *
1241 (format->channels) *
1242 (format->bit_depth >> 3);
1243 }
1244
1245 return 0;
1246}
1247
1248/*
1249 * Query the module config for the FE DAI
1250 * This is used to find the hw_params set for that DAI and apply to FE
1251 * pipeline
1252 */
1253struct skl_module_cfg *
1254skl_tplg_fe_get_cpr_module(struct snd_soc_dai *dai, int stream)
1255{
1256 struct snd_soc_dapm_widget *w;
1257 struct snd_soc_dapm_path *p = NULL;
1258
1259 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1260 w = dai->playback_widget;
Subhransu S. Prustyf0900eb2015-10-22 23:22:36 +05301261 snd_soc_dapm_widget_for_each_sink_path(w, p) {
Vinod Koulcfb0a872015-10-07 11:31:55 +01001262 if (p->connect && p->sink->power &&
Jeeja KPa28f51d2015-10-27 09:22:44 +09001263 !is_skl_dsp_widget_type(p->sink))
Vinod Koulcfb0a872015-10-07 11:31:55 +01001264 continue;
1265
1266 if (p->sink->priv) {
1267 dev_dbg(dai->dev, "set params for %s\n",
1268 p->sink->name);
1269 return p->sink->priv;
1270 }
1271 }
1272 } else {
1273 w = dai->capture_widget;
Subhransu S. Prustyf0900eb2015-10-22 23:22:36 +05301274 snd_soc_dapm_widget_for_each_source_path(w, p) {
Vinod Koulcfb0a872015-10-07 11:31:55 +01001275 if (p->connect && p->source->power &&
Jeeja KPa28f51d2015-10-27 09:22:44 +09001276 !is_skl_dsp_widget_type(p->source))
Vinod Koulcfb0a872015-10-07 11:31:55 +01001277 continue;
1278
1279 if (p->source->priv) {
1280 dev_dbg(dai->dev, "set params for %s\n",
1281 p->source->name);
1282 return p->source->priv;
1283 }
1284 }
1285 }
1286
1287 return NULL;
1288}
1289
Dharageswari.R718a42b2016-02-05 12:19:06 +05301290static struct skl_module_cfg *skl_get_mconfig_pb_cpr(
1291 struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
1292{
1293 struct snd_soc_dapm_path *p;
1294 struct skl_module_cfg *mconfig = NULL;
1295
1296 snd_soc_dapm_widget_for_each_source_path(w, p) {
1297 if (w->endpoints[SND_SOC_DAPM_DIR_OUT] > 0) {
1298 if (p->connect &&
1299 (p->sink->id == snd_soc_dapm_aif_out) &&
1300 p->source->priv) {
1301 mconfig = p->source->priv;
1302 return mconfig;
1303 }
1304 mconfig = skl_get_mconfig_pb_cpr(dai, p->source);
1305 if (mconfig)
1306 return mconfig;
1307 }
1308 }
1309 return mconfig;
1310}
1311
1312static struct skl_module_cfg *skl_get_mconfig_cap_cpr(
1313 struct snd_soc_dai *dai, struct snd_soc_dapm_widget *w)
1314{
1315 struct snd_soc_dapm_path *p;
1316 struct skl_module_cfg *mconfig = NULL;
1317
1318 snd_soc_dapm_widget_for_each_sink_path(w, p) {
1319 if (w->endpoints[SND_SOC_DAPM_DIR_IN] > 0) {
1320 if (p->connect &&
1321 (p->source->id == snd_soc_dapm_aif_in) &&
1322 p->sink->priv) {
1323 mconfig = p->sink->priv;
1324 return mconfig;
1325 }
1326 mconfig = skl_get_mconfig_cap_cpr(dai, p->sink);
1327 if (mconfig)
1328 return mconfig;
1329 }
1330 }
1331 return mconfig;
1332}
1333
1334struct skl_module_cfg *
1335skl_tplg_be_get_cpr_module(struct snd_soc_dai *dai, int stream)
1336{
1337 struct snd_soc_dapm_widget *w;
1338 struct skl_module_cfg *mconfig;
1339
1340 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1341 w = dai->playback_widget;
1342 mconfig = skl_get_mconfig_pb_cpr(dai, w);
1343 } else {
1344 w = dai->capture_widget;
1345 mconfig = skl_get_mconfig_cap_cpr(dai, w);
1346 }
1347 return mconfig;
1348}
1349
Vinod Koulcfb0a872015-10-07 11:31:55 +01001350static u8 skl_tplg_be_link_type(int dev_type)
1351{
1352 int ret;
1353
1354 switch (dev_type) {
1355 case SKL_DEVICE_BT:
1356 ret = NHLT_LINK_SSP;
1357 break;
1358
1359 case SKL_DEVICE_DMIC:
1360 ret = NHLT_LINK_DMIC;
1361 break;
1362
1363 case SKL_DEVICE_I2S:
1364 ret = NHLT_LINK_SSP;
1365 break;
1366
1367 case SKL_DEVICE_HDALINK:
1368 ret = NHLT_LINK_HDA;
1369 break;
1370
1371 default:
1372 ret = NHLT_LINK_INVALID;
1373 break;
1374 }
1375
1376 return ret;
1377}
1378
1379/*
1380 * Fill the BE gateway parameters
1381 * The BE gateway expects a blob of parameters which are kept in the ACPI
1382 * NHLT blob, so query the blob for interface type (i2s/pdm) and instance.
1383 * The port can have multiple settings so pick based on the PCM
1384 * parameters
1385 */
1386static int skl_tplg_be_fill_pipe_params(struct snd_soc_dai *dai,
1387 struct skl_module_cfg *mconfig,
1388 struct skl_pipe_params *params)
1389{
Vinod Koulcfb0a872015-10-07 11:31:55 +01001390 struct nhlt_specific_cfg *cfg;
1391 struct skl *skl = get_skl_ctx(dai->dev);
1392 int link_type = skl_tplg_be_link_type(mconfig->dev_type);
1393
Jeeja KP8871dcb2016-06-03 18:29:42 +05301394 skl_tplg_fill_dma_id(mconfig, params);
Vinod Koulcfb0a872015-10-07 11:31:55 +01001395
Jeeja KPb30c2752015-10-27 09:22:48 +09001396 if (link_type == NHLT_LINK_HDA)
1397 return 0;
1398
Vinod Koulcfb0a872015-10-07 11:31:55 +01001399 /* update the blob based on virtual bus_id*/
1400 cfg = skl_get_ep_blob(skl, mconfig->vbus_id, link_type,
1401 params->s_fmt, params->ch,
1402 params->s_freq, params->stream);
1403 if (cfg) {
1404 mconfig->formats_config.caps_size = cfg->size;
Jeeja KPbc032812015-10-22 23:22:35 +05301405 mconfig->formats_config.caps = (u32 *) &cfg->caps;
Vinod Koulcfb0a872015-10-07 11:31:55 +01001406 } else {
1407 dev_err(dai->dev, "Blob NULL for id %x type %d dirn %d\n",
1408 mconfig->vbus_id, link_type,
1409 params->stream);
1410 dev_err(dai->dev, "PCM: ch %d, freq %d, fmt %d\n",
1411 params->ch, params->s_freq, params->s_fmt);
1412 return -EINVAL;
1413 }
1414
1415 return 0;
1416}
1417
1418static int skl_tplg_be_set_src_pipe_params(struct snd_soc_dai *dai,
1419 struct snd_soc_dapm_widget *w,
1420 struct skl_pipe_params *params)
1421{
1422 struct snd_soc_dapm_path *p;
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_source_path(w, p) {
Vinod Koulcfb0a872015-10-07 11:31:55 +01001426 if (p->connect && is_skl_dsp_widget_type(p->source) &&
1427 p->source->priv) {
1428
Jeeja KP9a03cb42015-10-27 09:22:54 +09001429 ret = skl_tplg_be_fill_pipe_params(dai,
1430 p->source->priv, params);
1431 if (ret < 0)
1432 return ret;
Vinod Koulcfb0a872015-10-07 11:31:55 +01001433 } else {
Jeeja KP9a03cb42015-10-27 09:22:54 +09001434 ret = skl_tplg_be_set_src_pipe_params(dai,
1435 p->source, 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
1444static int skl_tplg_be_set_sink_pipe_params(struct snd_soc_dai *dai,
1445 struct snd_soc_dapm_widget *w, struct skl_pipe_params *params)
1446{
1447 struct snd_soc_dapm_path *p = NULL;
Subhransu S. Prusty4d8adccb2015-10-22 23:22:37 +05301448 int ret = -EIO;
Vinod Koulcfb0a872015-10-07 11:31:55 +01001449
Subhransu S. Prustyf0900eb2015-10-22 23:22:36 +05301450 snd_soc_dapm_widget_for_each_sink_path(w, p) {
Vinod Koulcfb0a872015-10-07 11:31:55 +01001451 if (p->connect && is_skl_dsp_widget_type(p->sink) &&
1452 p->sink->priv) {
1453
Jeeja KP9a03cb42015-10-27 09:22:54 +09001454 ret = skl_tplg_be_fill_pipe_params(dai,
1455 p->sink->priv, params);
1456 if (ret < 0)
1457 return ret;
Vinod Koulcfb0a872015-10-07 11:31:55 +01001458 } else {
Subhransu S. Prusty4d8adccb2015-10-22 23:22:37 +05301459 ret = skl_tplg_be_set_sink_pipe_params(
Vinod Koulcfb0a872015-10-07 11:31:55 +01001460 dai, p->sink, params);
Subhransu S. Prusty4d8adccb2015-10-22 23:22:37 +05301461 if (ret < 0)
1462 return ret;
Vinod Koulcfb0a872015-10-07 11:31:55 +01001463 }
1464 }
1465
Subhransu S. Prusty4d8adccb2015-10-22 23:22:37 +05301466 return ret;
Vinod Koulcfb0a872015-10-07 11:31:55 +01001467}
1468
1469/*
1470 * BE hw_params can be a source parameters (capture) or sink parameters
1471 * (playback). Based on sink and source we need to either find the source
1472 * list or the sink list and set the pipeline parameters
1473 */
1474int skl_tplg_be_update_params(struct snd_soc_dai *dai,
1475 struct skl_pipe_params *params)
1476{
1477 struct snd_soc_dapm_widget *w;
1478
1479 if (params->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1480 w = dai->playback_widget;
1481
1482 return skl_tplg_be_set_src_pipe_params(dai, w, params);
1483
1484 } else {
1485 w = dai->capture_widget;
1486
1487 return skl_tplg_be_set_sink_pipe_params(dai, w, params);
1488 }
1489
1490 return 0;
1491}
Vinod Koul3af36702015-10-07 11:31:56 +01001492
1493static const struct snd_soc_tplg_widget_events skl_tplg_widget_ops[] = {
1494 {SKL_MIXER_EVENT, skl_tplg_mixer_event},
1495 {SKL_VMIXER_EVENT, skl_tplg_vmixer_event},
1496 {SKL_PGA_EVENT, skl_tplg_pga_event},
1497};
1498
Jeeja KP140adfb2015-11-28 15:01:50 +05301499static const struct snd_soc_tplg_bytes_ext_ops skl_tlv_ops[] = {
1500 {SKL_CONTROL_TYPE_BYTE_TLV, skl_tplg_tlv_control_get,
1501 skl_tplg_tlv_control_set},
1502};
1503
Shreyas NC6277e832016-08-12 12:29:51 +05301504static int skl_tplg_fill_pipe_tkn(struct device *dev,
1505 struct skl_pipe *pipe, u32 tkn,
1506 u32 tkn_val)
Vinod Koul3af36702015-10-07 11:31:56 +01001507{
Vinod Koul3af36702015-10-07 11:31:56 +01001508
Shreyas NC6277e832016-08-12 12:29:51 +05301509 switch (tkn) {
1510 case SKL_TKN_U32_PIPE_CONN_TYPE:
1511 pipe->conn_type = tkn_val;
1512 break;
1513
1514 case SKL_TKN_U32_PIPE_PRIORITY:
1515 pipe->pipe_priority = tkn_val;
1516 break;
1517
1518 case SKL_TKN_U32_PIPE_MEM_PGS:
1519 pipe->memory_pages = tkn_val;
1520 break;
1521
Vinod Koul8a0cb232016-11-03 17:07:18 +05301522 case SKL_TKN_U32_PMODE:
1523 pipe->lp_mode = tkn_val;
1524 break;
1525
Shreyas NC6277e832016-08-12 12:29:51 +05301526 default:
1527 dev_err(dev, "Token not handled %d\n", tkn);
1528 return -EINVAL;
Vinod Koul3af36702015-10-07 11:31:56 +01001529 }
Shreyas NC6277e832016-08-12 12:29:51 +05301530
1531 return 0;
Vinod Koul3af36702015-10-07 11:31:56 +01001532}
1533
1534/*
Shreyas NC6277e832016-08-12 12:29:51 +05301535 * Add pipeline by parsing the relevant tokens
1536 * Return an existing pipe if the pipe already exists.
Vinod Koul3af36702015-10-07 11:31:56 +01001537 */
Shreyas NC6277e832016-08-12 12:29:51 +05301538static int skl_tplg_add_pipe(struct device *dev,
1539 struct skl_module_cfg *mconfig, struct skl *skl,
1540 struct snd_soc_tplg_vendor_value_elem *tkn_elem)
Vinod Koul3af36702015-10-07 11:31:56 +01001541{
1542 struct skl_pipeline *ppl;
1543 struct skl_pipe *pipe;
1544 struct skl_pipe_params *params;
1545
1546 list_for_each_entry(ppl, &skl->ppl_list, node) {
Shreyas NC6277e832016-08-12 12:29:51 +05301547 if (ppl->pipe->ppl_id == tkn_elem->value) {
1548 mconfig->pipe = ppl->pipe;
1549 return EEXIST;
1550 }
Vinod Koul3af36702015-10-07 11:31:56 +01001551 }
1552
1553 ppl = devm_kzalloc(dev, sizeof(*ppl), GFP_KERNEL);
1554 if (!ppl)
Shreyas NC6277e832016-08-12 12:29:51 +05301555 return -ENOMEM;
Vinod Koul3af36702015-10-07 11:31:56 +01001556
1557 pipe = devm_kzalloc(dev, sizeof(*pipe), GFP_KERNEL);
1558 if (!pipe)
Shreyas NC6277e832016-08-12 12:29:51 +05301559 return -ENOMEM;
Vinod Koul3af36702015-10-07 11:31:56 +01001560
1561 params = devm_kzalloc(dev, sizeof(*params), GFP_KERNEL);
1562 if (!params)
Shreyas NC6277e832016-08-12 12:29:51 +05301563 return -ENOMEM;
Vinod Koul3af36702015-10-07 11:31:56 +01001564
Vinod Koul3af36702015-10-07 11:31:56 +01001565 pipe->p_params = params;
Shreyas NC6277e832016-08-12 12:29:51 +05301566 pipe->ppl_id = tkn_elem->value;
Vinod Koul3af36702015-10-07 11:31:56 +01001567 INIT_LIST_HEAD(&pipe->w_list);
1568
1569 ppl->pipe = pipe;
1570 list_add(&ppl->node, &skl->ppl_list);
1571
Shreyas NC6277e832016-08-12 12:29:51 +05301572 mconfig->pipe = pipe;
1573 mconfig->pipe->state = SKL_PIPE_INVALID;
1574
1575 return 0;
Vinod Koul3af36702015-10-07 11:31:56 +01001576}
1577
Shreyas NC6277e832016-08-12 12:29:51 +05301578static int skl_tplg_fill_pin(struct device *dev, u32 tkn,
1579 struct skl_module_pin *m_pin,
1580 int pin_index, u32 value)
1581{
1582 switch (tkn) {
1583 case SKL_TKN_U32_PIN_MOD_ID:
1584 m_pin[pin_index].id.module_id = value;
1585 break;
1586
1587 case SKL_TKN_U32_PIN_INST_ID:
1588 m_pin[pin_index].id.instance_id = value;
1589 break;
1590
1591 default:
1592 dev_err(dev, "%d Not a pin token\n", value);
1593 return -EINVAL;
1594 }
1595
1596 return 0;
1597}
1598
1599/*
1600 * Parse for pin config specific tokens to fill up the
1601 * module private data
1602 */
1603static int skl_tplg_fill_pins_info(struct device *dev,
1604 struct skl_module_cfg *mconfig,
1605 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
1606 int dir, int pin_count)
1607{
1608 int ret;
1609 struct skl_module_pin *m_pin;
1610
1611 switch (dir) {
1612 case SKL_DIR_IN:
1613 m_pin = mconfig->m_in_pin;
1614 break;
1615
1616 case SKL_DIR_OUT:
1617 m_pin = mconfig->m_out_pin;
1618 break;
1619
1620 default:
Colin Ian Kingecd286a2016-09-16 18:51:21 +01001621 dev_err(dev, "Invalid direction value\n");
Shreyas NC6277e832016-08-12 12:29:51 +05301622 return -EINVAL;
1623 }
1624
1625 ret = skl_tplg_fill_pin(dev, tkn_elem->token,
1626 m_pin, pin_count, tkn_elem->value);
1627
1628 if (ret < 0)
1629 return ret;
1630
1631 m_pin[pin_count].in_use = false;
1632 m_pin[pin_count].pin_state = SKL_PIN_UNBIND;
1633
1634 return 0;
1635}
1636
1637/*
1638 * Fill up input/output module config format based
1639 * on the direction
1640 */
1641static int skl_tplg_fill_fmt(struct device *dev,
1642 struct skl_module_cfg *mconfig, u32 tkn,
1643 u32 value, u32 dir, u32 pin_count)
1644{
1645 struct skl_module_fmt *dst_fmt;
1646
1647 switch (dir) {
1648 case SKL_DIR_IN:
1649 dst_fmt = mconfig->in_fmt;
1650 dst_fmt += pin_count;
1651 break;
1652
1653 case SKL_DIR_OUT:
1654 dst_fmt = mconfig->out_fmt;
1655 dst_fmt += pin_count;
1656 break;
1657
1658 default:
Colin Ian Kingecd286a2016-09-16 18:51:21 +01001659 dev_err(dev, "Invalid direction value\n");
Shreyas NC6277e832016-08-12 12:29:51 +05301660 return -EINVAL;
1661 }
1662
1663 switch (tkn) {
1664 case SKL_TKN_U32_FMT_CH:
1665 dst_fmt->channels = value;
1666 break;
1667
1668 case SKL_TKN_U32_FMT_FREQ:
1669 dst_fmt->s_freq = value;
1670 break;
1671
1672 case SKL_TKN_U32_FMT_BIT_DEPTH:
1673 dst_fmt->bit_depth = value;
1674 break;
1675
1676 case SKL_TKN_U32_FMT_SAMPLE_SIZE:
1677 dst_fmt->valid_bit_depth = value;
1678 break;
1679
1680 case SKL_TKN_U32_FMT_CH_CONFIG:
1681 dst_fmt->ch_cfg = value;
1682 break;
1683
1684 case SKL_TKN_U32_FMT_INTERLEAVE:
1685 dst_fmt->interleaving_style = value;
1686 break;
1687
1688 case SKL_TKN_U32_FMT_SAMPLE_TYPE:
1689 dst_fmt->sample_type = value;
1690 break;
1691
1692 case SKL_TKN_U32_FMT_CH_MAP:
1693 dst_fmt->ch_map = value;
1694 break;
1695
1696 default:
Colin Ian Kingecd286a2016-09-16 18:51:21 +01001697 dev_err(dev, "Invalid token %d\n", tkn);
Shreyas NC6277e832016-08-12 12:29:51 +05301698 return -EINVAL;
1699 }
1700
1701 return 0;
1702}
1703
1704static int skl_tplg_get_uuid(struct device *dev, struct skl_module_cfg *mconfig,
1705 struct snd_soc_tplg_vendor_uuid_elem *uuid_tkn)
1706{
1707 if (uuid_tkn->token == SKL_TKN_UUID)
1708 memcpy(&mconfig->guid, &uuid_tkn->uuid, 16);
1709 else {
Colin Ian Kingecd286a2016-09-16 18:51:21 +01001710 dev_err(dev, "Not an UUID token tkn %d\n", uuid_tkn->token);
Shreyas NC6277e832016-08-12 12:29:51 +05301711 return -EINVAL;
1712 }
1713
1714 return 0;
1715}
1716
1717static void skl_tplg_fill_pin_dynamic_val(
1718 struct skl_module_pin *mpin, u32 pin_count, u32 value)
Hardik T Shah4cd98992015-10-27 09:22:55 +09001719{
1720 int i;
1721
Shreyas NC6277e832016-08-12 12:29:51 +05301722 for (i = 0; i < pin_count; i++)
1723 mpin[i].is_dynamic = value;
1724}
1725
1726/*
1727 * Parse tokens to fill up the module private data
1728 */
1729static int skl_tplg_get_token(struct device *dev,
1730 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
1731 struct skl *skl, struct skl_module_cfg *mconfig)
1732{
1733 int tkn_count = 0;
1734 int ret;
1735 static int is_pipe_exists;
1736 static int pin_index, dir;
1737
1738 if (tkn_elem->token > SKL_TKN_MAX)
1739 return -EINVAL;
1740
1741 switch (tkn_elem->token) {
1742 case SKL_TKN_U8_IN_QUEUE_COUNT:
1743 mconfig->max_in_queue = tkn_elem->value;
1744 mconfig->m_in_pin = devm_kzalloc(dev, mconfig->max_in_queue *
1745 sizeof(*mconfig->m_in_pin),
1746 GFP_KERNEL);
1747 if (!mconfig->m_in_pin)
1748 return -ENOMEM;
1749
1750 break;
1751
1752 case SKL_TKN_U8_OUT_QUEUE_COUNT:
1753 mconfig->max_out_queue = tkn_elem->value;
1754 mconfig->m_out_pin = devm_kzalloc(dev, mconfig->max_out_queue *
1755 sizeof(*mconfig->m_out_pin),
1756 GFP_KERNEL);
1757
1758 if (!mconfig->m_out_pin)
1759 return -ENOMEM;
1760
1761 break;
1762
1763 case SKL_TKN_U8_DYN_IN_PIN:
1764 if (!mconfig->m_in_pin)
1765 return -ENOMEM;
1766
1767 skl_tplg_fill_pin_dynamic_val(mconfig->m_in_pin,
1768 mconfig->max_in_queue, tkn_elem->value);
1769
1770 break;
1771
1772 case SKL_TKN_U8_DYN_OUT_PIN:
1773 if (!mconfig->m_out_pin)
1774 return -ENOMEM;
1775
1776 skl_tplg_fill_pin_dynamic_val(mconfig->m_out_pin,
1777 mconfig->max_out_queue, tkn_elem->value);
1778
1779 break;
1780
1781 case SKL_TKN_U8_TIME_SLOT:
1782 mconfig->time_slot = tkn_elem->value;
1783 break;
1784
1785 case SKL_TKN_U8_CORE_ID:
1786 mconfig->core_id = tkn_elem->value;
1787
1788 case SKL_TKN_U8_MOD_TYPE:
1789 mconfig->m_type = tkn_elem->value;
1790 break;
1791
1792 case SKL_TKN_U8_DEV_TYPE:
1793 mconfig->dev_type = tkn_elem->value;
1794 break;
1795
1796 case SKL_TKN_U8_HW_CONN_TYPE:
1797 mconfig->hw_conn_type = tkn_elem->value;
1798 break;
1799
1800 case SKL_TKN_U16_MOD_INST_ID:
1801 mconfig->id.instance_id =
1802 tkn_elem->value;
1803 break;
1804
1805 case SKL_TKN_U32_MEM_PAGES:
1806 mconfig->mem_pages = tkn_elem->value;
1807 break;
1808
1809 case SKL_TKN_U32_MAX_MCPS:
1810 mconfig->mcps = tkn_elem->value;
1811 break;
1812
1813 case SKL_TKN_U32_OBS:
1814 mconfig->obs = tkn_elem->value;
1815 break;
1816
1817 case SKL_TKN_U32_IBS:
1818 mconfig->ibs = tkn_elem->value;
1819 break;
1820
1821 case SKL_TKN_U32_VBUS_ID:
1822 mconfig->vbus_id = tkn_elem->value;
1823 break;
1824
1825 case SKL_TKN_U32_PARAMS_FIXUP:
1826 mconfig->params_fixup = tkn_elem->value;
1827 break;
1828
1829 case SKL_TKN_U32_CONVERTER:
1830 mconfig->converter = tkn_elem->value;
1831 break;
1832
1833 case SKL_TKN_U32_PIPE_ID:
1834 ret = skl_tplg_add_pipe(dev,
1835 mconfig, skl, tkn_elem);
1836
1837 if (ret < 0)
1838 return is_pipe_exists;
1839
1840 if (ret == EEXIST)
1841 is_pipe_exists = 1;
1842
1843 break;
1844
1845 case SKL_TKN_U32_PIPE_CONN_TYPE:
1846 case SKL_TKN_U32_PIPE_PRIORITY:
1847 case SKL_TKN_U32_PIPE_MEM_PGS:
Vinod Koul8a0cb232016-11-03 17:07:18 +05301848 case SKL_TKN_U32_PMODE:
Shreyas NC6277e832016-08-12 12:29:51 +05301849 if (is_pipe_exists) {
1850 ret = skl_tplg_fill_pipe_tkn(dev, mconfig->pipe,
1851 tkn_elem->token, tkn_elem->value);
1852 if (ret < 0)
1853 return ret;
1854 }
1855
1856 break;
1857
1858 /*
1859 * SKL_TKN_U32_DIR_PIN_COUNT token has the value for both
1860 * direction and the pin count. The first four bits represent
1861 * direction and next four the pin count.
1862 */
1863 case SKL_TKN_U32_DIR_PIN_COUNT:
1864 dir = tkn_elem->value & SKL_IN_DIR_BIT_MASK;
1865 pin_index = (tkn_elem->value &
1866 SKL_PIN_COUNT_MASK) >> 4;
1867
1868 break;
1869
1870 case SKL_TKN_U32_FMT_CH:
1871 case SKL_TKN_U32_FMT_FREQ:
1872 case SKL_TKN_U32_FMT_BIT_DEPTH:
1873 case SKL_TKN_U32_FMT_SAMPLE_SIZE:
1874 case SKL_TKN_U32_FMT_CH_CONFIG:
1875 case SKL_TKN_U32_FMT_INTERLEAVE:
1876 case SKL_TKN_U32_FMT_SAMPLE_TYPE:
1877 case SKL_TKN_U32_FMT_CH_MAP:
1878 ret = skl_tplg_fill_fmt(dev, mconfig, tkn_elem->token,
1879 tkn_elem->value, dir, pin_index);
1880
1881 if (ret < 0)
1882 return ret;
1883
1884 break;
1885
1886 case SKL_TKN_U32_PIN_MOD_ID:
1887 case SKL_TKN_U32_PIN_INST_ID:
1888 ret = skl_tplg_fill_pins_info(dev,
1889 mconfig, tkn_elem, dir,
1890 pin_index);
1891 if (ret < 0)
1892 return ret;
1893
1894 break;
1895
1896 case SKL_TKN_U32_CAPS_SIZE:
1897 mconfig->formats_config.caps_size =
1898 tkn_elem->value;
1899
1900 break;
1901
1902 case SKL_TKN_U32_PROC_DOMAIN:
1903 mconfig->domain =
1904 tkn_elem->value;
1905
1906 break;
1907
1908 case SKL_TKN_U8_IN_PIN_TYPE:
1909 case SKL_TKN_U8_OUT_PIN_TYPE:
1910 case SKL_TKN_U8_CONN_TYPE:
1911 break;
1912
1913 default:
1914 dev_err(dev, "Token %d not handled\n",
1915 tkn_elem->token);
1916 return -EINVAL;
Hardik T Shah4cd98992015-10-27 09:22:55 +09001917 }
Shreyas NC6277e832016-08-12 12:29:51 +05301918
1919 tkn_count++;
1920
1921 return tkn_count;
1922}
1923
1924/*
1925 * Parse the vendor array for specific tokens to construct
1926 * module private data
1927 */
1928static int skl_tplg_get_tokens(struct device *dev,
1929 char *pvt_data, struct skl *skl,
1930 struct skl_module_cfg *mconfig, int block_size)
1931{
1932 struct snd_soc_tplg_vendor_array *array;
1933 struct snd_soc_tplg_vendor_value_elem *tkn_elem;
1934 int tkn_count = 0, ret;
1935 int off = 0, tuple_size = 0;
1936
1937 if (block_size <= 0)
1938 return -EINVAL;
1939
1940 while (tuple_size < block_size) {
1941 array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off);
1942
1943 off += array->size;
1944
1945 switch (array->type) {
1946 case SND_SOC_TPLG_TUPLE_TYPE_STRING:
Colin Ian Kingecd286a2016-09-16 18:51:21 +01001947 dev_warn(dev, "no string tokens expected for skl tplg\n");
Shreyas NC6277e832016-08-12 12:29:51 +05301948 continue;
1949
1950 case SND_SOC_TPLG_TUPLE_TYPE_UUID:
1951 ret = skl_tplg_get_uuid(dev, mconfig, array->uuid);
1952 if (ret < 0)
1953 return ret;
1954
1955 tuple_size += sizeof(*array->uuid);
1956
1957 continue;
1958
1959 default:
1960 tkn_elem = array->value;
1961 tkn_count = 0;
1962 break;
1963 }
1964
1965 while (tkn_count <= (array->num_elems - 1)) {
1966 ret = skl_tplg_get_token(dev, tkn_elem,
1967 skl, mconfig);
1968
1969 if (ret < 0)
1970 return ret;
1971
1972 tkn_count = tkn_count + ret;
1973 tkn_elem++;
1974 }
1975
1976 tuple_size += tkn_count * sizeof(*tkn_elem);
1977 }
1978
1979 return 0;
1980}
1981
1982/*
1983 * Every data block is preceded by a descriptor to read the number
1984 * of data blocks, they type of the block and it's size
1985 */
1986static int skl_tplg_get_desc_blocks(struct device *dev,
1987 struct snd_soc_tplg_vendor_array *array)
1988{
1989 struct snd_soc_tplg_vendor_value_elem *tkn_elem;
1990
1991 tkn_elem = array->value;
1992
1993 switch (tkn_elem->token) {
1994 case SKL_TKN_U8_NUM_BLOCKS:
1995 case SKL_TKN_U8_BLOCK_TYPE:
1996 case SKL_TKN_U16_BLOCK_SIZE:
1997 return tkn_elem->value;
1998
1999 default:
Colin Ian Kingecd286a2016-09-16 18:51:21 +01002000 dev_err(dev, "Invalid descriptor token %d\n", tkn_elem->token);
Shreyas NC6277e832016-08-12 12:29:51 +05302001 break;
2002 }
2003
2004 return -EINVAL;
2005}
2006
2007/*
2008 * Parse the private data for the token and corresponding value.
2009 * The private data can have multiple data blocks. So, a data block
2010 * is preceded by a descriptor for number of blocks and a descriptor
2011 * for the type and size of the suceeding data block.
2012 */
2013static int skl_tplg_get_pvt_data(struct snd_soc_tplg_dapm_widget *tplg_w,
2014 struct skl *skl, struct device *dev,
2015 struct skl_module_cfg *mconfig)
2016{
2017 struct snd_soc_tplg_vendor_array *array;
2018 int num_blocks, block_size = 0, block_type, off = 0;
2019 char *data;
2020 int ret;
2021
2022 /* Read the NUM_DATA_BLOCKS descriptor */
2023 array = (struct snd_soc_tplg_vendor_array *)tplg_w->priv.data;
2024 ret = skl_tplg_get_desc_blocks(dev, array);
2025 if (ret < 0)
2026 return ret;
2027 num_blocks = ret;
2028
2029 off += array->size;
2030 array = (struct snd_soc_tplg_vendor_array *)(tplg_w->priv.data + off);
2031
2032 /* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */
2033 while (num_blocks > 0) {
2034 ret = skl_tplg_get_desc_blocks(dev, array);
2035
2036 if (ret < 0)
2037 return ret;
2038 block_type = ret;
2039 off += array->size;
2040
2041 array = (struct snd_soc_tplg_vendor_array *)
2042 (tplg_w->priv.data + off);
2043
2044 ret = skl_tplg_get_desc_blocks(dev, array);
2045
2046 if (ret < 0)
2047 return ret;
2048 block_size = ret;
2049 off += array->size;
2050
2051 array = (struct snd_soc_tplg_vendor_array *)
2052 (tplg_w->priv.data + off);
2053
2054 data = (tplg_w->priv.data + off);
2055
2056 if (block_type == SKL_TYPE_TUPLE) {
2057 ret = skl_tplg_get_tokens(dev, data,
2058 skl, mconfig, block_size);
2059
2060 if (ret < 0)
2061 return ret;
2062
2063 --num_blocks;
2064 } else {
2065 if (mconfig->formats_config.caps_size > 0)
2066 memcpy(mconfig->formats_config.caps, data,
2067 mconfig->formats_config.caps_size);
2068 --num_blocks;
2069 }
2070 }
2071
2072 return 0;
Hardik T Shah4cd98992015-10-27 09:22:55 +09002073}
2074
Dharageswari Rfe3f4442016-06-03 18:29:39 +05302075static void skl_clear_pin_config(struct snd_soc_platform *platform,
2076 struct snd_soc_dapm_widget *w)
2077{
2078 int i;
2079 struct skl_module_cfg *mconfig;
2080 struct skl_pipe *pipe;
2081
2082 if (!strncmp(w->dapm->component->name, platform->component.name,
2083 strlen(platform->component.name))) {
2084 mconfig = w->priv;
2085 pipe = mconfig->pipe;
2086 for (i = 0; i < mconfig->max_in_queue; i++) {
2087 mconfig->m_in_pin[i].in_use = false;
2088 mconfig->m_in_pin[i].pin_state = SKL_PIN_UNBIND;
2089 }
2090 for (i = 0; i < mconfig->max_out_queue; i++) {
2091 mconfig->m_out_pin[i].in_use = false;
2092 mconfig->m_out_pin[i].pin_state = SKL_PIN_UNBIND;
2093 }
2094 pipe->state = SKL_PIPE_INVALID;
2095 mconfig->m_state = SKL_MODULE_UNINIT;
2096 }
2097}
2098
2099void skl_cleanup_resources(struct skl *skl)
2100{
2101 struct skl_sst *ctx = skl->skl_sst;
2102 struct snd_soc_platform *soc_platform = skl->platform;
2103 struct snd_soc_dapm_widget *w;
2104 struct snd_soc_card *card;
2105
2106 if (soc_platform == NULL)
2107 return;
2108
2109 card = soc_platform->component.card;
2110 if (!card || !card->instantiated)
2111 return;
2112
2113 skl->resource.mem = 0;
2114 skl->resource.mcps = 0;
2115
2116 list_for_each_entry(w, &card->widgets, list) {
2117 if (is_skl_dsp_widget_type(w) && (w->priv != NULL))
2118 skl_clear_pin_config(soc_platform, w);
2119 }
2120
2121 skl_clear_module_cnt(ctx->dsp);
2122}
2123
Vinod Koul3af36702015-10-07 11:31:56 +01002124/*
2125 * Topology core widget load callback
2126 *
2127 * This is used to save the private data for each widget which gives
2128 * information to the driver about module and pipeline parameters which DSP
2129 * FW expects like ids, resource values, formats etc
2130 */
2131static int skl_tplg_widget_load(struct snd_soc_component *cmpnt,
Jeeja KPb663a8c2015-10-07 11:31:57 +01002132 struct snd_soc_dapm_widget *w,
2133 struct snd_soc_tplg_dapm_widget *tplg_w)
Vinod Koul3af36702015-10-07 11:31:56 +01002134{
2135 int ret;
2136 struct hdac_ext_bus *ebus = snd_soc_component_get_drvdata(cmpnt);
2137 struct skl *skl = ebus_to_skl(ebus);
2138 struct hdac_bus *bus = ebus_to_hbus(ebus);
2139 struct skl_module_cfg *mconfig;
Vinod Koul3af36702015-10-07 11:31:56 +01002140
2141 if (!tplg_w->priv.size)
2142 goto bind_event;
2143
2144 mconfig = devm_kzalloc(bus->dev, sizeof(*mconfig), GFP_KERNEL);
2145
2146 if (!mconfig)
2147 return -ENOMEM;
2148
2149 w->priv = mconfig;
Shreyas NC09305da2016-04-21 11:45:22 +05302150
Vinod Koulb7c50552016-07-26 18:06:40 +05302151 /*
2152 * module binary can be loaded later, so set it to query when
2153 * module is load for a use case
2154 */
2155 mconfig->id.module_id = -1;
Hardik T Shah4cd98992015-10-27 09:22:55 +09002156
Shreyas NC6277e832016-08-12 12:29:51 +05302157 /* Parse private data for tuples */
2158 ret = skl_tplg_get_pvt_data(tplg_w, skl, bus->dev, mconfig);
2159 if (ret < 0)
2160 return ret;
Vinod Koul3af36702015-10-07 11:31:56 +01002161bind_event:
2162 if (tplg_w->event_type == 0) {
Vinod Koul3373f712015-10-07 16:39:38 +01002163 dev_dbg(bus->dev, "ASoC: No event handler required\n");
Vinod Koul3af36702015-10-07 11:31:56 +01002164 return 0;
2165 }
2166
2167 ret = snd_soc_tplg_widget_bind_event(w, skl_tplg_widget_ops,
Jeeja KPb663a8c2015-10-07 11:31:57 +01002168 ARRAY_SIZE(skl_tplg_widget_ops),
2169 tplg_w->event_type);
Vinod Koul3af36702015-10-07 11:31:56 +01002170
2171 if (ret) {
2172 dev_err(bus->dev, "%s: No matching event handlers found for %d\n",
2173 __func__, tplg_w->event_type);
2174 return -EINVAL;
2175 }
2176
2177 return 0;
2178}
2179
Jeeja KP140adfb2015-11-28 15:01:50 +05302180static int skl_init_algo_data(struct device *dev, struct soc_bytes_ext *be,
2181 struct snd_soc_tplg_bytes_control *bc)
2182{
2183 struct skl_algo_data *ac;
2184 struct skl_dfw_algo_data *dfw_ac =
2185 (struct skl_dfw_algo_data *)bc->priv.data;
2186
2187 ac = devm_kzalloc(dev, sizeof(*ac), GFP_KERNEL);
2188 if (!ac)
2189 return -ENOMEM;
2190
2191 /* Fill private data */
2192 ac->max = dfw_ac->max;
2193 ac->param_id = dfw_ac->param_id;
2194 ac->set_params = dfw_ac->set_params;
Dharageswari R0d682102016-07-08 18:15:03 +05302195 ac->size = dfw_ac->max;
Jeeja KP140adfb2015-11-28 15:01:50 +05302196
2197 if (ac->max) {
2198 ac->params = (char *) devm_kzalloc(dev, ac->max, GFP_KERNEL);
2199 if (!ac->params)
2200 return -ENOMEM;
2201
Alan Coxedd7ea22016-02-22 09:37:27 +05302202 memcpy(ac->params, dfw_ac->params, ac->max);
Jeeja KP140adfb2015-11-28 15:01:50 +05302203 }
2204
2205 be->dobj.private = ac;
2206 return 0;
2207}
2208
2209static int skl_tplg_control_load(struct snd_soc_component *cmpnt,
2210 struct snd_kcontrol_new *kctl,
2211 struct snd_soc_tplg_ctl_hdr *hdr)
2212{
2213 struct soc_bytes_ext *sb;
2214 struct snd_soc_tplg_bytes_control *tplg_bc;
2215 struct hdac_ext_bus *ebus = snd_soc_component_get_drvdata(cmpnt);
2216 struct hdac_bus *bus = ebus_to_hbus(ebus);
2217
2218 switch (hdr->ops.info) {
2219 case SND_SOC_TPLG_CTL_BYTES:
2220 tplg_bc = container_of(hdr,
2221 struct snd_soc_tplg_bytes_control, hdr);
2222 if (kctl->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2223 sb = (struct soc_bytes_ext *)kctl->private_value;
2224 if (tplg_bc->priv.size)
2225 return skl_init_algo_data(
2226 bus->dev, sb, tplg_bc);
2227 }
2228 break;
2229
2230 default:
2231 dev_warn(bus->dev, "Control load not supported %d:%d:%d\n",
2232 hdr->ops.get, hdr->ops.put, hdr->ops.info);
2233 break;
2234 }
2235
2236 return 0;
2237}
2238
Shreyas NC541070c2016-08-23 09:31:03 +05302239static int skl_tplg_fill_str_mfest_tkn(struct device *dev,
2240 struct snd_soc_tplg_vendor_string_elem *str_elem,
2241 struct skl_dfw_manifest *minfo)
2242{
2243 int tkn_count = 0;
2244 static int ref_count;
2245
2246 switch (str_elem->token) {
2247 case SKL_TKN_STR_LIB_NAME:
2248 if (ref_count > minfo->lib_count - 1) {
2249 ref_count = 0;
2250 return -EINVAL;
2251 }
2252
2253 strncpy(minfo->lib[ref_count].name, str_elem->string,
2254 ARRAY_SIZE(minfo->lib[ref_count].name));
2255 ref_count++;
2256 tkn_count++;
2257 break;
2258
2259 default:
Colin Ian Kingecd286a2016-09-16 18:51:21 +01002260 dev_err(dev, "Not a string token %d\n", str_elem->token);
Shreyas NC541070c2016-08-23 09:31:03 +05302261 break;
2262 }
2263
2264 return tkn_count;
2265}
2266
2267static int skl_tplg_get_str_tkn(struct device *dev,
2268 struct snd_soc_tplg_vendor_array *array,
2269 struct skl_dfw_manifest *minfo)
2270{
2271 int tkn_count = 0, ret;
2272 struct snd_soc_tplg_vendor_string_elem *str_elem;
2273
2274 str_elem = (struct snd_soc_tplg_vendor_string_elem *)array->value;
2275 while (tkn_count < array->num_elems) {
2276 ret = skl_tplg_fill_str_mfest_tkn(dev, str_elem, minfo);
2277 str_elem++;
2278
2279 if (ret < 0)
2280 return ret;
2281
2282 tkn_count = tkn_count + ret;
2283 }
2284
2285 return tkn_count;
2286}
2287
2288static int skl_tplg_get_int_tkn(struct device *dev,
2289 struct snd_soc_tplg_vendor_value_elem *tkn_elem,
2290 struct skl_dfw_manifest *minfo)
2291{
2292 int tkn_count = 0;
2293
2294 switch (tkn_elem->token) {
2295 case SKL_TKN_U32_LIB_COUNT:
2296 minfo->lib_count = tkn_elem->value;
2297 tkn_count++;
2298 break;
2299
2300 default:
Colin Ian Kingecd286a2016-09-16 18:51:21 +01002301 dev_err(dev, "Not a manifest token %d\n", tkn_elem->token);
Shreyas NC541070c2016-08-23 09:31:03 +05302302 return -EINVAL;
2303 }
2304
2305 return tkn_count;
2306}
2307
2308/*
2309 * Fill the manifest structure by parsing the tokens based on the
2310 * type.
2311 */
2312static int skl_tplg_get_manifest_tkn(struct device *dev,
2313 char *pvt_data, struct skl_dfw_manifest *minfo,
2314 int block_size)
2315{
2316 int tkn_count = 0, ret;
2317 int off = 0, tuple_size = 0;
2318 struct snd_soc_tplg_vendor_array *array;
2319 struct snd_soc_tplg_vendor_value_elem *tkn_elem;
2320
2321 if (block_size <= 0)
2322 return -EINVAL;
2323
2324 while (tuple_size < block_size) {
2325 array = (struct snd_soc_tplg_vendor_array *)(pvt_data + off);
2326 off += array->size;
2327 switch (array->type) {
2328 case SND_SOC_TPLG_TUPLE_TYPE_STRING:
2329 ret = skl_tplg_get_str_tkn(dev, array, minfo);
2330
2331 if (ret < 0)
2332 return ret;
2333 tkn_count += ret;
2334
2335 tuple_size += tkn_count *
2336 sizeof(struct snd_soc_tplg_vendor_string_elem);
2337 continue;
2338
2339 case SND_SOC_TPLG_TUPLE_TYPE_UUID:
Colin Ian Kingecd286a2016-09-16 18:51:21 +01002340 dev_warn(dev, "no uuid tokens for skl tplf manifest\n");
Shreyas NC541070c2016-08-23 09:31:03 +05302341 continue;
2342
2343 default:
2344 tkn_elem = array->value;
2345 tkn_count = 0;
2346 break;
2347 }
2348
2349 while (tkn_count <= array->num_elems - 1) {
2350 ret = skl_tplg_get_int_tkn(dev,
2351 tkn_elem, minfo);
2352 if (ret < 0)
2353 return ret;
2354
2355 tkn_count = tkn_count + ret;
2356 tkn_elem++;
2357 tuple_size += tkn_count *
2358 sizeof(struct snd_soc_tplg_vendor_value_elem);
2359 break;
2360 }
2361 tkn_count = 0;
2362 }
2363
2364 return 0;
2365}
2366
2367/*
2368 * Parse manifest private data for tokens. The private data block is
2369 * preceded by descriptors for type and size of data block.
2370 */
2371static int skl_tplg_get_manifest_data(struct snd_soc_tplg_manifest *manifest,
2372 struct device *dev, struct skl_dfw_manifest *minfo)
2373{
2374 struct snd_soc_tplg_vendor_array *array;
2375 int num_blocks, block_size = 0, block_type, off = 0;
2376 char *data;
2377 int ret;
2378
2379 /* Read the NUM_DATA_BLOCKS descriptor */
2380 array = (struct snd_soc_tplg_vendor_array *)manifest->priv.data;
2381 ret = skl_tplg_get_desc_blocks(dev, array);
2382 if (ret < 0)
2383 return ret;
2384 num_blocks = ret;
2385
2386 off += array->size;
2387 array = (struct snd_soc_tplg_vendor_array *)
2388 (manifest->priv.data + off);
2389
2390 /* Read the BLOCK_TYPE and BLOCK_SIZE descriptor */
2391 while (num_blocks > 0) {
2392 ret = skl_tplg_get_desc_blocks(dev, array);
2393
2394 if (ret < 0)
2395 return ret;
2396 block_type = ret;
2397 off += array->size;
2398
2399 array = (struct snd_soc_tplg_vendor_array *)
2400 (manifest->priv.data + off);
2401
2402 ret = skl_tplg_get_desc_blocks(dev, array);
2403
2404 if (ret < 0)
2405 return ret;
2406 block_size = ret;
2407 off += array->size;
2408
2409 array = (struct snd_soc_tplg_vendor_array *)
2410 (manifest->priv.data + off);
2411
2412 data = (manifest->priv.data + off);
2413
2414 if (block_type == SKL_TYPE_TUPLE) {
2415 ret = skl_tplg_get_manifest_tkn(dev, data, minfo,
2416 block_size);
2417
2418 if (ret < 0)
2419 return ret;
2420
2421 --num_blocks;
2422 } else {
2423 return -EINVAL;
2424 }
2425 }
2426
2427 return 0;
2428}
2429
Kranthi G15ecaba92016-07-26 18:06:43 +05302430static int skl_manifest_load(struct snd_soc_component *cmpnt,
2431 struct snd_soc_tplg_manifest *manifest)
2432{
2433 struct skl_dfw_manifest *minfo;
2434 struct hdac_ext_bus *ebus = snd_soc_component_get_drvdata(cmpnt);
2435 struct hdac_bus *bus = ebus_to_hbus(ebus);
2436 struct skl *skl = ebus_to_skl(ebus);
2437 int ret = 0;
2438
Vinod Koulc15ad602016-08-24 18:03:13 +05302439 /* proceed only if we have private data defined */
2440 if (manifest->priv.size == 0)
2441 return 0;
2442
Kranthi G15ecaba92016-07-26 18:06:43 +05302443 minfo = &skl->skl_sst->manifest;
Shreyas NC541070c2016-08-23 09:31:03 +05302444
2445 skl_tplg_get_manifest_data(manifest, bus->dev, minfo);
Kranthi G15ecaba92016-07-26 18:06:43 +05302446
2447 if (minfo->lib_count > HDA_MAX_LIB) {
2448 dev_err(bus->dev, "Exceeding max Library count. Got:%d\n",
2449 minfo->lib_count);
2450 ret = -EINVAL;
2451 }
2452
2453 return ret;
2454}
2455
Vinod Koul3af36702015-10-07 11:31:56 +01002456static struct snd_soc_tplg_ops skl_tplg_ops = {
2457 .widget_load = skl_tplg_widget_load,
Jeeja KP140adfb2015-11-28 15:01:50 +05302458 .control_load = skl_tplg_control_load,
2459 .bytes_ext_ops = skl_tlv_ops,
2460 .bytes_ext_ops_count = ARRAY_SIZE(skl_tlv_ops),
Kranthi G15ecaba92016-07-26 18:06:43 +05302461 .manifest = skl_manifest_load,
Vinod Koul3af36702015-10-07 11:31:56 +01002462};
2463
Jeeja KP287af4f2016-06-03 18:29:40 +05302464/*
2465 * A pipe can have multiple modules, each of them will be a DAPM widget as
2466 * well. While managing a pipeline we need to get the list of all the
2467 * widgets in a pipelines, so this helper - skl_tplg_create_pipe_widget_list()
2468 * helps to get the SKL type widgets in that pipeline
2469 */
2470static int skl_tplg_create_pipe_widget_list(struct snd_soc_platform *platform)
2471{
2472 struct snd_soc_dapm_widget *w;
2473 struct skl_module_cfg *mcfg = NULL;
2474 struct skl_pipe_module *p_module = NULL;
2475 struct skl_pipe *pipe;
2476
2477 list_for_each_entry(w, &platform->component.card->widgets, list) {
2478 if (is_skl_dsp_widget_type(w) && w->priv != NULL) {
2479 mcfg = w->priv;
2480 pipe = mcfg->pipe;
2481
2482 p_module = devm_kzalloc(platform->dev,
2483 sizeof(*p_module), GFP_KERNEL);
2484 if (!p_module)
2485 return -ENOMEM;
2486
2487 p_module->w = w;
2488 list_add_tail(&p_module->node, &pipe->w_list);
2489 }
2490 }
2491
2492 return 0;
2493}
2494
Jeeja KPf0aa94f2016-06-03 18:29:41 +05302495static void skl_tplg_set_pipe_type(struct skl *skl, struct skl_pipe *pipe)
2496{
2497 struct skl_pipe_module *w_module;
2498 struct snd_soc_dapm_widget *w;
2499 struct skl_module_cfg *mconfig;
2500 bool host_found = false, link_found = false;
2501
2502 list_for_each_entry(w_module, &pipe->w_list, node) {
2503 w = w_module->w;
2504 mconfig = w->priv;
2505
2506 if (mconfig->dev_type == SKL_DEVICE_HDAHOST)
2507 host_found = true;
2508 else if (mconfig->dev_type != SKL_DEVICE_NONE)
2509 link_found = true;
2510 }
2511
2512 if (host_found && link_found)
2513 pipe->passthru = true;
2514 else
2515 pipe->passthru = false;
2516}
2517
Vinod Koul3af36702015-10-07 11:31:56 +01002518/* This will be read from topology manifest, currently defined here */
2519#define SKL_MAX_MCPS 30000000
2520#define SKL_FW_MAX_MEM 1000000
2521
2522/*
2523 * SKL topology init routine
2524 */
2525int skl_tplg_init(struct snd_soc_platform *platform, struct hdac_ext_bus *ebus)
2526{
2527 int ret;
2528 const struct firmware *fw;
2529 struct hdac_bus *bus = ebus_to_hbus(ebus);
2530 struct skl *skl = ebus_to_skl(ebus);
Jeeja KPf0aa94f2016-06-03 18:29:41 +05302531 struct skl_pipeline *ppl;
Vinod Koul3af36702015-10-07 11:31:56 +01002532
Vinod Koul4b235c42016-02-19 11:42:34 +05302533 ret = request_firmware(&fw, skl->tplg_name, bus->dev);
Vinod Koul3af36702015-10-07 11:31:56 +01002534 if (ret < 0) {
Jeeja KPb663a8c2015-10-07 11:31:57 +01002535 dev_err(bus->dev, "tplg fw %s load failed with %d\n",
Vinod Koul4b235c42016-02-19 11:42:34 +05302536 skl->tplg_name, ret);
2537 ret = request_firmware(&fw, "dfw_sst.bin", bus->dev);
2538 if (ret < 0) {
2539 dev_err(bus->dev, "Fallback tplg fw %s load failed with %d\n",
2540 "dfw_sst.bin", ret);
2541 return ret;
2542 }
Vinod Koul3af36702015-10-07 11:31:56 +01002543 }
2544
2545 /*
2546 * The complete tplg for SKL is loaded as index 0, we don't use
2547 * any other index
2548 */
Jeeja KPb663a8c2015-10-07 11:31:57 +01002549 ret = snd_soc_tplg_component_load(&platform->component,
2550 &skl_tplg_ops, fw, 0);
Vinod Koul3af36702015-10-07 11:31:56 +01002551 if (ret < 0) {
2552 dev_err(bus->dev, "tplg component load failed%d\n", ret);
Sudip Mukherjeec14a82c2016-01-21 17:27:59 +05302553 release_firmware(fw);
Vinod Koul3af36702015-10-07 11:31:56 +01002554 return -EINVAL;
2555 }
2556
2557 skl->resource.max_mcps = SKL_MAX_MCPS;
2558 skl->resource.max_mem = SKL_FW_MAX_MEM;
2559
Vinod Kould8018362016-01-05 17:16:04 +05302560 skl->tplg = fw;
Jeeja KP287af4f2016-06-03 18:29:40 +05302561 ret = skl_tplg_create_pipe_widget_list(platform);
2562 if (ret < 0)
2563 return ret;
Vinod Kould8018362016-01-05 17:16:04 +05302564
Jeeja KPf0aa94f2016-06-03 18:29:41 +05302565 list_for_each_entry(ppl, &skl->ppl_list, node)
2566 skl_tplg_set_pipe_type(skl, ppl->pipe);
Vinod Koul3af36702015-10-07 11:31:56 +01002567
2568 return 0;
Jeeja KPe4e2d2f2015-10-07 11:31:52 +01002569}