1 // SPDX-License-Identifier: GPL-2.0-only
2  /*
3  *  sst-atom-controls.c - Intel MID Platform driver DPCM ALSA controls for Mrfld
4  *
5  *  Copyright (C) 2013-14 Intel Corp
6  *  Author: Omair Mohammed Abdullah <omair.m.abdullah@intel.com>
7  *	Vinod Koul <vinod.koul@intel.com>
8  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9  *
10  *  In the dpcm driver modelling when a particular FE/BE/Mixer/Pipe is active
11  *  we forward the settings and parameters, rest we keep the values  in
12  *  driver and forward when DAPM enables them
13  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14  */
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 
17 #include <linux/slab.h>
18 #include <sound/soc.h>
19 #include <sound/tlv.h>
20 #include "sst-mfld-platform.h"
21 #include "sst-atom-controls.h"
22 
23 static int sst_fill_byte_control(struct sst_data *drv,
24 					 u8 ipc_msg, u8 block,
25 					 u8 task_id, u8 pipe_id,
26 					 u16 len, void *cmd_data)
27 {
28 	struct snd_sst_bytes_v2 *byte_data = drv->byte_stream;
29 
30 	byte_data->type = SST_CMD_BYTES_SET;
31 	byte_data->ipc_msg = ipc_msg;
32 	byte_data->block = block;
33 	byte_data->task_id = task_id;
34 	byte_data->pipe_id = pipe_id;
35 
36 	if (len > SST_MAX_BIN_BYTES - sizeof(*byte_data)) {
37 		dev_err(&drv->pdev->dev, "command length too big (%u)", len);
38 		return -EINVAL;
39 	}
40 	byte_data->len = len;
41 	memcpy(byte_data->bytes, cmd_data, len);
42 	print_hex_dump_bytes("writing to lpe: ", DUMP_PREFIX_OFFSET,
43 			     byte_data, len + sizeof(*byte_data));
44 	return 0;
45 }
46 
47 static int sst_fill_and_send_cmd_unlocked(struct sst_data *drv,
48 				 u8 ipc_msg, u8 block, u8 task_id, u8 pipe_id,
49 				 void *cmd_data, u16 len)
50 {
51 	int ret = 0;
52 
53 	WARN_ON(!mutex_is_locked(&drv->lock));
54 
55 	ret = sst_fill_byte_control(drv, ipc_msg,
56 				block, task_id, pipe_id, len, cmd_data);
57 	if (ret < 0)
58 		return ret;
59 	return sst->ops->send_byte_stream(sst->dev, drv->byte_stream);
60 }
61 
62 /**
63  * sst_fill_and_send_cmd - generate the IPC message and send it to the FW
64  * @ipc_msg:	type of IPC (CMD, SET_PARAMS, GET_PARAMS)
65  * @cmd_data:	the IPC payload
66  */
67 static int sst_fill_and_send_cmd(struct sst_data *drv,
68 				 u8 ipc_msg, u8 block, u8 task_id, u8 pipe_id,
69 				 void *cmd_data, u16 len)
70 {
71 	int ret;
72 
73 	mutex_lock(&drv->lock);
74 	ret = sst_fill_and_send_cmd_unlocked(drv, ipc_msg, block,
75 					task_id, pipe_id, cmd_data, len);
76 	mutex_unlock(&drv->lock);
77 
78 	return ret;
79 }
80 
81 /**
82  * tx map value is a bitfield where each bit represents a FW channel
83  *
84  *			3 2 1 0		# 0 = codec0, 1 = codec1
85  *			RLRLRLRL	# 3, 4 = reserved
86  *
87  * e.g. slot 0 rx map =	00001100b -> data from slot 0 goes into codec_in1 L,R
88  */
89 static u8 sst_ssp_tx_map[SST_MAX_TDM_SLOTS] = {
90 	0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80, /* default rx map */
91 };
92 
93 /**
94  * rx map value is a bitfield where each bit represents a slot
95  *
96  *			  76543210	# 0 = slot 0, 1 = slot 1
97  *
98  * e.g. codec1_0 tx map = 00000101b -> data from codec_out1_0 goes into slot 0, 2
99  */
100 static u8 sst_ssp_rx_map[SST_MAX_TDM_SLOTS] = {
101 	0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80, /* default tx map */
102 };
103 
104 /**
105  * NOTE: this is invoked with lock held
106  */
107 static int sst_send_slot_map(struct sst_data *drv)
108 {
109 	struct sst_param_sba_ssp_slot_map cmd;
110 
111 	SST_FILL_DEFAULT_DESTINATION(cmd.header.dst);
112 	cmd.header.command_id = SBA_SET_SSP_SLOT_MAP;
113 	cmd.header.length = sizeof(struct sst_param_sba_ssp_slot_map)
114 				- sizeof(struct sst_dsp_header);
115 
116 	cmd.param_id = SBA_SET_SSP_SLOT_MAP;
117 	cmd.param_len = sizeof(cmd.rx_slot_map) + sizeof(cmd.tx_slot_map)
118 					+ sizeof(cmd.ssp_index);
119 	cmd.ssp_index = SSP_CODEC;
120 
121 	memcpy(cmd.rx_slot_map, &sst_ssp_tx_map[0], sizeof(cmd.rx_slot_map));
122 	memcpy(cmd.tx_slot_map, &sst_ssp_rx_map[0], sizeof(cmd.tx_slot_map));
123 
124 	return sst_fill_and_send_cmd_unlocked(drv, SST_IPC_IA_SET_PARAMS,
125 			SST_FLAG_BLOCKED, SST_TASK_SBA, 0, &cmd,
126 			      sizeof(cmd.header) + cmd.header.length);
127 }
128 
129 static int sst_slot_enum_info(struct snd_kcontrol *kcontrol,
130 		       struct snd_ctl_elem_info *uinfo)
131 {
132 	struct sst_enum *e = (struct sst_enum *)kcontrol->private_value;
133 
134 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
135 	uinfo->count = 1;
136 	uinfo->value.enumerated.items = e->max;
137 
138 	if (uinfo->value.enumerated.item > e->max - 1)
139 		uinfo->value.enumerated.item = e->max - 1;
140 	strcpy(uinfo->value.enumerated.name,
141 		e->texts[uinfo->value.enumerated.item]);
142 
143 	return 0;
144 }
145 
146 /**
147  * sst_slot_get - get the status of the interleaver/deinterleaver control
148  *
149  * Searches the map where the control status is stored, and gets the
150  * channel/slot which is currently set for this enumerated control. Since it is
151  * an enumerated control, there is only one possible value.
152  */
153 static int sst_slot_get(struct snd_kcontrol *kcontrol,
154 			struct snd_ctl_elem_value *ucontrol)
155 {
156 	struct sst_enum *e = (void *)kcontrol->private_value;
157 	struct snd_soc_component *c = snd_kcontrol_chip(kcontrol);
158 	struct sst_data *drv = snd_soc_component_get_drvdata(c);
159 	unsigned int ctl_no = e->reg;
160 	unsigned int is_tx = e->tx;
161 	unsigned int val, mux;
162 	u8 *map = is_tx ? sst_ssp_rx_map : sst_ssp_tx_map;
163 
164 	mutex_lock(&drv->lock);
165 	val = 1 << ctl_no;
166 	/* search which slot/channel has this bit set - there should be only one */
167 	for (mux = e->max; mux > 0;  mux--)
168 		if (map[mux - 1] & val)
169 			break;
170 
171 	ucontrol->value.enumerated.item[0] = mux;
172 	mutex_unlock(&drv->lock);
173 
174 	dev_dbg(c->dev, "%s - %s map = %#x\n",
175 			is_tx ? "tx channel" : "rx slot",
176 			 e->texts[mux], mux ? map[mux - 1] : -1);
177 	return 0;
178 }
179 
180 /* sst_check_and_send_slot_map - helper for checking power state and sending
181  * slot map cmd
182  *
183  * called with lock held
184  */
185 static int sst_check_and_send_slot_map(struct sst_data *drv, struct snd_kcontrol *kcontrol)
186 {
187 	struct sst_enum *e = (void *)kcontrol->private_value;
188 	int ret = 0;
189 
190 	if (e->w && e->w->power)
191 		ret = sst_send_slot_map(drv);
192 	else if (!e->w)
193 		dev_err(&drv->pdev->dev, "Slot control: %s doesn't have DAPM widget!!!\n",
194 				kcontrol->id.name);
195 	return ret;
196 }
197 
198 /**
199  * sst_slot_put - set the status of interleaver/deinterleaver control
200  *
201  * (de)interleaver controls are defined in opposite sense to be user-friendly
202  *
203  * Instead of the enum value being the value written to the register, it is the
204  * register address; and the kcontrol number (register num) is the value written
205  * to the register. This is so that there can be only one value for each
206  * slot/channel since there is only one control for each slot/channel.
207  *
208  * This means that whenever an enum is set, we need to clear the bit
209  * for that kcontrol_no for all the interleaver OR deinterleaver registers
210  */
211 static int sst_slot_put(struct snd_kcontrol *kcontrol,
212 			struct snd_ctl_elem_value *ucontrol)
213 {
214 	struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol);
215 	struct sst_data *drv = snd_soc_component_get_drvdata(c);
216 	struct sst_enum *e = (void *)kcontrol->private_value;
217 	int i, ret = 0;
218 	unsigned int ctl_no = e->reg;
219 	unsigned int is_tx = e->tx;
220 	unsigned int slot_channel_no;
221 	unsigned int val, mux;
222 	u8 *map;
223 
224 	map = is_tx ? sst_ssp_rx_map : sst_ssp_tx_map;
225 
226 	val = 1 << ctl_no;
227 	mux = ucontrol->value.enumerated.item[0];
228 	if (mux > e->max - 1)
229 		return -EINVAL;
230 
231 	mutex_lock(&drv->lock);
232 	/* first clear all registers of this bit */
233 	for (i = 0; i < e->max; i++)
234 		map[i] &= ~val;
235 
236 	if (mux == 0) {
237 		/* kctl set to 'none' and we reset the bits so send IPC */
238 		ret = sst_check_and_send_slot_map(drv, kcontrol);
239 
240 		mutex_unlock(&drv->lock);
241 		return ret;
242 	}
243 
244 	/* offset by one to take "None" into account */
245 	slot_channel_no = mux - 1;
246 	map[slot_channel_no] |= val;
247 
248 	dev_dbg(c->dev, "%s %s map = %#x\n",
249 			is_tx ? "tx channel" : "rx slot",
250 			e->texts[mux], map[slot_channel_no]);
251 
252 	ret = sst_check_and_send_slot_map(drv, kcontrol);
253 
254 	mutex_unlock(&drv->lock);
255 	return ret;
256 }
257 
258 static int sst_send_algo_cmd(struct sst_data *drv,
259 			      struct sst_algo_control *bc)
260 {
261 	int len, ret = 0;
262 	struct sst_cmd_set_params *cmd;
263 
264 	/*bc->max includes sizeof algos + length field*/
265 	len = sizeof(cmd->dst) + sizeof(cmd->command_id) + bc->max;
266 
267 	cmd = kzalloc(len, GFP_KERNEL);
268 	if (cmd == NULL)
269 		return -ENOMEM;
270 
271 	SST_FILL_DESTINATION(2, cmd->dst, bc->pipe_id, bc->module_id);
272 	cmd->command_id = bc->cmd_id;
273 	memcpy(cmd->params, bc->params, bc->max);
274 
275 	ret = sst_fill_and_send_cmd_unlocked(drv, SST_IPC_IA_SET_PARAMS,
276 				SST_FLAG_BLOCKED, bc->task_id, 0, cmd, len);
277 	kfree(cmd);
278 	return ret;
279 }
280 
281 /**
282  * sst_find_and_send_pipe_algo - send all the algo parameters for a pipe
283  *
284  * The algos which are in each pipeline are sent to the firmware one by one
285  *
286  * Called with lock held
287  */
288 static int sst_find_and_send_pipe_algo(struct sst_data *drv,
289 					const char *pipe, struct sst_ids *ids)
290 {
291 	int ret = 0;
292 	struct sst_algo_control *bc;
293 	struct sst_module *algo = NULL;
294 
295 	dev_dbg(&drv->pdev->dev, "Enter: widget=%s\n", pipe);
296 
297 	list_for_each_entry(algo, &ids->algo_list, node) {
298 		bc = (void *)algo->kctl->private_value;
299 
300 		dev_dbg(&drv->pdev->dev, "Found algo control name=%s pipe=%s\n",
301 				algo->kctl->id.name, pipe);
302 		ret = sst_send_algo_cmd(drv, bc);
303 		if (ret)
304 			return ret;
305 	}
306 	return ret;
307 }
308 
309 static int sst_algo_bytes_ctl_info(struct snd_kcontrol *kcontrol,
310 			    struct snd_ctl_elem_info *uinfo)
311 {
312 	struct sst_algo_control *bc = (void *)kcontrol->private_value;
313 
314 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
315 	uinfo->count = bc->max;
316 
317 	return 0;
318 }
319 
320 static int sst_algo_control_get(struct snd_kcontrol *kcontrol,
321 				struct snd_ctl_elem_value *ucontrol)
322 {
323 	struct sst_algo_control *bc = (void *)kcontrol->private_value;
324 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
325 
326 	switch (bc->type) {
327 	case SST_ALGO_PARAMS:
328 		memcpy(ucontrol->value.bytes.data, bc->params, bc->max);
329 		break;
330 	default:
331 		dev_err(component->dev, "Invalid Input- algo type:%d\n",
332 				bc->type);
333 		return -EINVAL;
334 
335 	}
336 	return 0;
337 }
338 
339 static int sst_algo_control_set(struct snd_kcontrol *kcontrol,
340 				struct snd_ctl_elem_value *ucontrol)
341 {
342 	int ret = 0;
343 	struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
344 	struct sst_data *drv = snd_soc_component_get_drvdata(cmpnt);
345 	struct sst_algo_control *bc = (void *)kcontrol->private_value;
346 
347 	dev_dbg(cmpnt->dev, "control_name=%s\n", kcontrol->id.name);
348 	mutex_lock(&drv->lock);
349 	switch (bc->type) {
350 	case SST_ALGO_PARAMS:
351 		memcpy(bc->params, ucontrol->value.bytes.data, bc->max);
352 		break;
353 	default:
354 		mutex_unlock(&drv->lock);
355 		dev_err(cmpnt->dev, "Invalid Input- algo type:%d\n",
356 				bc->type);
357 		return -EINVAL;
358 	}
359 	/*if pipe is enabled, need to send the algo params from here*/
360 	if (bc->w && bc->w->power)
361 		ret = sst_send_algo_cmd(drv, bc);
362 	mutex_unlock(&drv->lock);
363 
364 	return ret;
365 }
366 
367 static int sst_gain_ctl_info(struct snd_kcontrol *kcontrol,
368 	struct snd_ctl_elem_info *uinfo)
369 {
370 	struct sst_gain_mixer_control *mc = (void *)kcontrol->private_value;
371 
372 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
373 	uinfo->count = mc->stereo ? 2 : 1;
374 	uinfo->value.integer.min = mc->min;
375 	uinfo->value.integer.max = mc->max;
376 
377 	return 0;
378 }
379 
380 /**
381  * sst_send_gain_cmd - send the gain algorithm IPC to the FW
382  * @gv:		the stored value of gain (also contains rampduration)
383  * @mute:	flag that indicates whether this was called from the
384  *		digital_mute callback or directly. If called from the
385  *		digital_mute callback, module will be muted/unmuted based on this
386  *		flag. The flag is always 0 if called directly.
387  *
388  * Called with sst_data.lock held
389  *
390  * The user-set gain value is sent only if the user-controllable 'mute' control
391  * is OFF (indicated by gv->mute). Otherwise, the mute value (MIN value) is
392  * sent.
393  */
394 static int sst_send_gain_cmd(struct sst_data *drv, struct sst_gain_value *gv,
395 			      u16 task_id, u16 loc_id, u16 module_id, int mute)
396 {
397 	struct sst_cmd_set_gain_dual cmd;
398 
399 	dev_dbg(&drv->pdev->dev, "Enter\n");
400 
401 	cmd.header.command_id = MMX_SET_GAIN;
402 	SST_FILL_DEFAULT_DESTINATION(cmd.header.dst);
403 	cmd.gain_cell_num = 1;
404 
405 	if (mute || gv->mute) {
406 		cmd.cell_gains[0].cell_gain_left = SST_GAIN_MIN_VALUE;
407 		cmd.cell_gains[0].cell_gain_right = SST_GAIN_MIN_VALUE;
408 	} else {
409 		cmd.cell_gains[0].cell_gain_left = gv->l_gain;
410 		cmd.cell_gains[0].cell_gain_right = gv->r_gain;
411 	}
412 
413 	SST_FILL_DESTINATION(2, cmd.cell_gains[0].dest,
414 			     loc_id, module_id);
415 	cmd.cell_gains[0].gain_time_constant = gv->ramp_duration;
416 
417 	cmd.header.length = sizeof(struct sst_cmd_set_gain_dual)
418 				- sizeof(struct sst_dsp_header);
419 
420 	/* we are with lock held, so call the unlocked api  to send */
421 	return sst_fill_and_send_cmd_unlocked(drv, SST_IPC_IA_SET_PARAMS,
422 				SST_FLAG_BLOCKED, task_id, 0, &cmd,
423 			      sizeof(cmd.header) + cmd.header.length);
424 }
425 
426 static int sst_gain_get(struct snd_kcontrol *kcontrol,
427 			struct snd_ctl_elem_value *ucontrol)
428 {
429 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
430 	struct sst_gain_mixer_control *mc = (void *)kcontrol->private_value;
431 	struct sst_gain_value *gv = mc->gain_val;
432 
433 	switch (mc->type) {
434 	case SST_GAIN_TLV:
435 		ucontrol->value.integer.value[0] = gv->l_gain;
436 		ucontrol->value.integer.value[1] = gv->r_gain;
437 		break;
438 
439 	case SST_GAIN_MUTE:
440 		ucontrol->value.integer.value[0] = gv->mute ? 0 : 1;
441 		break;
442 
443 	case SST_GAIN_RAMP_DURATION:
444 		ucontrol->value.integer.value[0] = gv->ramp_duration;
445 		break;
446 
447 	default:
448 		dev_err(component->dev, "Invalid Input- gain type:%d\n",
449 				mc->type);
450 		return -EINVAL;
451 	}
452 
453 	return 0;
454 }
455 
456 static int sst_gain_put(struct snd_kcontrol *kcontrol,
457 			struct snd_ctl_elem_value *ucontrol)
458 {
459 	int ret = 0;
460 	struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
461 	struct sst_data *drv = snd_soc_component_get_drvdata(cmpnt);
462 	struct sst_gain_mixer_control *mc = (void *)kcontrol->private_value;
463 	struct sst_gain_value *gv = mc->gain_val;
464 
465 	mutex_lock(&drv->lock);
466 
467 	switch (mc->type) {
468 	case SST_GAIN_TLV:
469 		gv->l_gain = ucontrol->value.integer.value[0];
470 		gv->r_gain = ucontrol->value.integer.value[1];
471 		dev_dbg(cmpnt->dev, "%s: Volume %d, %d\n",
472 				mc->pname, gv->l_gain, gv->r_gain);
473 		break;
474 
475 	case SST_GAIN_MUTE:
476 		gv->mute = !ucontrol->value.integer.value[0];
477 		dev_dbg(cmpnt->dev, "%s: Mute %d\n", mc->pname, gv->mute);
478 		break;
479 
480 	case SST_GAIN_RAMP_DURATION:
481 		gv->ramp_duration = ucontrol->value.integer.value[0];
482 		dev_dbg(cmpnt->dev, "%s: Ramp Delay%d\n",
483 					mc->pname, gv->ramp_duration);
484 		break;
485 
486 	default:
487 		mutex_unlock(&drv->lock);
488 		dev_err(cmpnt->dev, "Invalid Input- gain type:%d\n",
489 				mc->type);
490 		return -EINVAL;
491 	}
492 
493 	if (mc->w && mc->w->power)
494 		ret = sst_send_gain_cmd(drv, gv, mc->task_id,
495 			mc->pipe_id | mc->instance_id, mc->module_id, 0);
496 	mutex_unlock(&drv->lock);
497 
498 	return ret;
499 }
500 
501 static int sst_set_pipe_gain(struct sst_ids *ids,
502 				struct sst_data *drv, int mute);
503 
504 static int sst_send_pipe_module_params(struct snd_soc_dapm_widget *w,
505 		struct snd_kcontrol *kcontrol)
506 {
507 	struct snd_soc_component *c = snd_soc_dapm_to_component(w->dapm);
508 	struct sst_data *drv = snd_soc_component_get_drvdata(c);
509 	struct sst_ids *ids = w->priv;
510 
511 	mutex_lock(&drv->lock);
512 	sst_find_and_send_pipe_algo(drv, w->name, ids);
513 	sst_set_pipe_gain(ids, drv, 0);
514 	mutex_unlock(&drv->lock);
515 
516 	return 0;
517 }
518 
519 static int sst_generic_modules_event(struct snd_soc_dapm_widget *w,
520 				     struct snd_kcontrol *k, int event)
521 {
522 	if (SND_SOC_DAPM_EVENT_ON(event))
523 		return sst_send_pipe_module_params(w, k);
524 	return 0;
525 }
526 
527 static const DECLARE_TLV_DB_SCALE(sst_gain_tlv_common, SST_GAIN_MIN_VALUE * 10, 10, 0);
528 
529 /* Look up table to convert MIXER SW bit regs to SWM inputs */
530 static const uint swm_mixer_input_ids[SST_SWM_INPUT_COUNT] = {
531 	[SST_IP_MODEM]		= SST_SWM_IN_MODEM,
532 	[SST_IP_CODEC0]		= SST_SWM_IN_CODEC0,
533 	[SST_IP_CODEC1]		= SST_SWM_IN_CODEC1,
534 	[SST_IP_LOOP0]		= SST_SWM_IN_SPROT_LOOP,
535 	[SST_IP_LOOP1]		= SST_SWM_IN_MEDIA_LOOP1,
536 	[SST_IP_LOOP2]		= SST_SWM_IN_MEDIA_LOOP2,
537 	[SST_IP_PCM0]		= SST_SWM_IN_PCM0,
538 	[SST_IP_PCM1]		= SST_SWM_IN_PCM1,
539 	[SST_IP_MEDIA0]		= SST_SWM_IN_MEDIA0,
540 	[SST_IP_MEDIA1]		= SST_SWM_IN_MEDIA1,
541 	[SST_IP_MEDIA2]		= SST_SWM_IN_MEDIA2,
542 	[SST_IP_MEDIA3]		= SST_SWM_IN_MEDIA3,
543 };
544 
545 /**
546  * fill_swm_input - fill in the SWM input ids given the register
547  *
548  * The register value is a bit-field inicated which mixer inputs are ON. Use the
549  * lookup table to get the input-id and fill it in the structure.
550  */
551 static int fill_swm_input(struct snd_soc_component *cmpnt,
552 		struct swm_input_ids *swm_input, unsigned int reg)
553 {
554 	uint i, is_set, nb_inputs = 0;
555 	u16 input_loc_id;
556 
557 	dev_dbg(cmpnt->dev, "reg: %#x\n", reg);
558 	for (i = 0; i < SST_SWM_INPUT_COUNT; i++) {
559 		is_set = reg & BIT(i);
560 		if (!is_set)
561 			continue;
562 
563 		input_loc_id = swm_mixer_input_ids[i];
564 		SST_FILL_DESTINATION(2, swm_input->input_id,
565 				     input_loc_id, SST_DEFAULT_MODULE_ID);
566 		nb_inputs++;
567 		swm_input++;
568 		dev_dbg(cmpnt->dev, "input id: %#x, nb_inputs: %d\n",
569 				input_loc_id, nb_inputs);
570 
571 		if (nb_inputs == SST_CMD_SWM_MAX_INPUTS) {
572 			dev_warn(cmpnt->dev, "SET_SWM cmd max inputs reached");
573 			break;
574 		}
575 	}
576 	return nb_inputs;
577 }
578 
579 
580 /**
581  * called with lock held
582  */
583 static int sst_set_pipe_gain(struct sst_ids *ids,
584 			struct sst_data *drv, int mute)
585 {
586 	int ret = 0;
587 	struct sst_gain_mixer_control *mc;
588 	struct sst_gain_value *gv;
589 	struct sst_module *gain = NULL;
590 
591 	list_for_each_entry(gain, &ids->gain_list, node) {
592 		struct snd_kcontrol *kctl = gain->kctl;
593 
594 		dev_dbg(&drv->pdev->dev, "control name=%s\n", kctl->id.name);
595 		mc = (void *)kctl->private_value;
596 		gv = mc->gain_val;
597 
598 		ret = sst_send_gain_cmd(drv, gv, mc->task_id,
599 			mc->pipe_id | mc->instance_id, mc->module_id, mute);
600 		if (ret)
601 			return ret;
602 	}
603 	return ret;
604 }
605 
606 static int sst_swm_mixer_event(struct snd_soc_dapm_widget *w,
607 			struct snd_kcontrol *k, int event)
608 {
609 	struct sst_cmd_set_swm cmd;
610 	struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
611 	struct sst_data *drv = snd_soc_component_get_drvdata(cmpnt);
612 	struct sst_ids *ids = w->priv;
613 	bool set_mixer = false;
614 	struct soc_mixer_control *mc;
615 	int val = 0;
616 	int i = 0;
617 
618 	dev_dbg(cmpnt->dev, "widget = %s\n", w->name);
619 	/*
620 	 * Identify which mixer input is on and send the bitmap of the
621 	 * inputs as an IPC to the DSP.
622 	 */
623 	for (i = 0; i < w->num_kcontrols; i++) {
624 		if (dapm_kcontrol_get_value(w->kcontrols[i])) {
625 			mc = (struct soc_mixer_control *)(w->kcontrols[i])->private_value;
626 			val |= 1 << mc->shift;
627 		}
628 	}
629 	dev_dbg(cmpnt->dev, "val = %#x\n", val);
630 
631 	switch (event) {
632 	case SND_SOC_DAPM_PRE_PMU:
633 	case SND_SOC_DAPM_POST_PMD:
634 		set_mixer = true;
635 		break;
636 	case SND_SOC_DAPM_POST_REG:
637 		if (w->power)
638 			set_mixer = true;
639 		break;
640 	default:
641 		set_mixer = false;
642 	}
643 
644 	if (!set_mixer)
645 		return 0;
646 
647 	if (SND_SOC_DAPM_EVENT_ON(event) ||
648 	    event == SND_SOC_DAPM_POST_REG)
649 		cmd.switch_state = SST_SWM_ON;
650 	else
651 		cmd.switch_state = SST_SWM_OFF;
652 
653 	SST_FILL_DEFAULT_DESTINATION(cmd.header.dst);
654 	/* MMX_SET_SWM == SBA_SET_SWM */
655 	cmd.header.command_id = SBA_SET_SWM;
656 
657 	SST_FILL_DESTINATION(2, cmd.output_id,
658 			     ids->location_id, SST_DEFAULT_MODULE_ID);
659 	cmd.nb_inputs =	fill_swm_input(cmpnt, &cmd.input[0], val);
660 	cmd.header.length = offsetof(struct sst_cmd_set_swm, input)
661 				- sizeof(struct sst_dsp_header)
662 				+ (cmd.nb_inputs * sizeof(cmd.input[0]));
663 
664 	return sst_fill_and_send_cmd(drv, SST_IPC_IA_CMD, SST_FLAG_BLOCKED,
665 			      ids->task_id, 0, &cmd,
666 			      sizeof(cmd.header) + cmd.header.length);
667 }
668 
669 /* SBA mixers - 16 inputs */
670 #define SST_SBA_DECLARE_MIX_CONTROLS(kctl_name)							\
671 	static const struct snd_kcontrol_new kctl_name[] = {					\
672 		SOC_DAPM_SINGLE("modem_in Switch", SND_SOC_NOPM, SST_IP_MODEM, 1, 0),		\
673 		SOC_DAPM_SINGLE("codec_in0 Switch", SND_SOC_NOPM, SST_IP_CODEC0, 1, 0),		\
674 		SOC_DAPM_SINGLE("codec_in1 Switch", SND_SOC_NOPM, SST_IP_CODEC1, 1, 0),		\
675 		SOC_DAPM_SINGLE("sprot_loop_in Switch", SND_SOC_NOPM, SST_IP_LOOP0, 1, 0),	\
676 		SOC_DAPM_SINGLE("media_loop1_in Switch", SND_SOC_NOPM, SST_IP_LOOP1, 1, 0),	\
677 		SOC_DAPM_SINGLE("media_loop2_in Switch", SND_SOC_NOPM, SST_IP_LOOP2, 1, 0),	\
678 		SOC_DAPM_SINGLE("pcm0_in Switch", SND_SOC_NOPM, SST_IP_PCM0, 1, 0),		\
679 		SOC_DAPM_SINGLE("pcm1_in Switch", SND_SOC_NOPM, SST_IP_PCM1, 1, 0),		\
680 	}
681 
682 #define SST_SBA_MIXER_GRAPH_MAP(mix_name)			\
683 	{ mix_name, "modem_in Switch",	"modem_in" },		\
684 	{ mix_name, "codec_in0 Switch",	"codec_in0" },		\
685 	{ mix_name, "codec_in1 Switch",	"codec_in1" },		\
686 	{ mix_name, "sprot_loop_in Switch",	"sprot_loop_in" },	\
687 	{ mix_name, "media_loop1_in Switch",	"media_loop1_in" },	\
688 	{ mix_name, "media_loop2_in Switch",	"media_loop2_in" },	\
689 	{ mix_name, "pcm0_in Switch",		"pcm0_in" },		\
690 	{ mix_name, "pcm1_in Switch",		"pcm1_in" }
691 
692 #define SST_MMX_DECLARE_MIX_CONTROLS(kctl_name)						\
693 	static const struct snd_kcontrol_new kctl_name[] = {				\
694 		SOC_DAPM_SINGLE("media0_in Switch", SND_SOC_NOPM, SST_IP_MEDIA0, 1, 0),	\
695 		SOC_DAPM_SINGLE("media1_in Switch", SND_SOC_NOPM, SST_IP_MEDIA1, 1, 0),	\
696 		SOC_DAPM_SINGLE("media2_in Switch", SND_SOC_NOPM, SST_IP_MEDIA2, 1, 0),	\
697 		SOC_DAPM_SINGLE("media3_in Switch", SND_SOC_NOPM, SST_IP_MEDIA3, 1, 0),	\
698 	}
699 
700 SST_MMX_DECLARE_MIX_CONTROLS(sst_mix_media0_controls);
701 SST_MMX_DECLARE_MIX_CONTROLS(sst_mix_media1_controls);
702 
703 /* 18 SBA mixers */
704 SST_SBA_DECLARE_MIX_CONTROLS(sst_mix_pcm0_controls);
705 SST_SBA_DECLARE_MIX_CONTROLS(sst_mix_pcm1_controls);
706 SST_SBA_DECLARE_MIX_CONTROLS(sst_mix_pcm2_controls);
707 SST_SBA_DECLARE_MIX_CONTROLS(sst_mix_sprot_l0_controls);
708 SST_SBA_DECLARE_MIX_CONTROLS(sst_mix_media_l1_controls);
709 SST_SBA_DECLARE_MIX_CONTROLS(sst_mix_media_l2_controls);
710 SST_SBA_DECLARE_MIX_CONTROLS(sst_mix_voip_controls);
711 SST_SBA_DECLARE_MIX_CONTROLS(sst_mix_codec0_controls);
712 SST_SBA_DECLARE_MIX_CONTROLS(sst_mix_codec1_controls);
713 SST_SBA_DECLARE_MIX_CONTROLS(sst_mix_modem_controls);
714 
715 /*
716  * sst_handle_vb_timer - Start/Stop the DSP scheduler
717  *
718  * The DSP expects first cmd to be SBA_VB_START, so at first startup send
719  * that.
720  * DSP expects last cmd to be SBA_VB_IDLE, so at last shutdown send that.
721  *
722  * Do refcount internally so that we send command only at first start
723  * and last end. Since SST driver does its own ref count, invoke sst's
724  * power ops always!
725  */
726 int sst_handle_vb_timer(struct snd_soc_dai *dai, bool enable)
727 {
728 	int ret = 0;
729 	struct sst_cmd_generic cmd;
730 	struct sst_data *drv = snd_soc_dai_get_drvdata(dai);
731 	static int timer_usage;
732 
733 	if (enable)
734 		cmd.header.command_id = SBA_VB_START;
735 	else
736 		cmd.header.command_id = SBA_IDLE;
737 	dev_dbg(dai->dev, "enable=%u, usage=%d\n", enable, timer_usage);
738 
739 	SST_FILL_DEFAULT_DESTINATION(cmd.header.dst);
740 	cmd.header.length = 0;
741 
742 	if (enable) {
743 		ret = sst->ops->power(sst->dev, true);
744 		if (ret < 0)
745 			return ret;
746 	}
747 
748 	mutex_lock(&drv->lock);
749 	if (enable)
750 		timer_usage++;
751 	else
752 		timer_usage--;
753 
754 	/*
755 	 * Send the command only if this call is the first enable or last
756 	 * disable
757 	 */
758 	if ((enable && (timer_usage == 1)) ||
759 	    (!enable && (timer_usage == 0))) {
760 		ret = sst_fill_and_send_cmd_unlocked(drv, SST_IPC_IA_CMD,
761 				SST_FLAG_BLOCKED, SST_TASK_SBA, 0, &cmd,
762 				sizeof(cmd.header) + cmd.header.length);
763 		if (ret && enable) {
764 			timer_usage--;
765 			enable  = false;
766 		}
767 	}
768 	mutex_unlock(&drv->lock);
769 
770 	if (!enable)
771 		sst->ops->power(sst->dev, false);
772 	return ret;
773 }
774 
775 int sst_fill_ssp_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
776 		unsigned int rx_mask, int slots, int slot_width)
777 {
778 	struct sst_data *ctx = snd_soc_dai_get_drvdata(dai);
779 
780 	ctx->ssp_cmd.nb_slots = slots;
781 	ctx->ssp_cmd.active_tx_slot_map = tx_mask;
782 	ctx->ssp_cmd.active_rx_slot_map = rx_mask;
783 	ctx->ssp_cmd.nb_bits_per_slots = slot_width;
784 
785 	return 0;
786 }
787 
788 static int sst_get_frame_sync_polarity(struct snd_soc_dai *dai,
789 		unsigned int fmt)
790 {
791 	int format;
792 
793 	format = fmt & SND_SOC_DAIFMT_INV_MASK;
794 	dev_dbg(dai->dev, "Enter:%s, format=%x\n", __func__, format);
795 
796 	switch (format) {
797 	case SND_SOC_DAIFMT_NB_NF:
798 	case SND_SOC_DAIFMT_IB_NF:
799 		return SSP_FS_ACTIVE_HIGH;
800 	case SND_SOC_DAIFMT_NB_IF:
801 	case SND_SOC_DAIFMT_IB_IF:
802 		return SSP_FS_ACTIVE_LOW;
803 	default:
804 		dev_err(dai->dev, "Invalid frame sync polarity %d\n", format);
805 	}
806 
807 	return -EINVAL;
808 }
809 
810 static int sst_get_ssp_mode(struct snd_soc_dai *dai, unsigned int fmt)
811 {
812 	int format;
813 
814 	format = (fmt & SND_SOC_DAIFMT_MASTER_MASK);
815 	dev_dbg(dai->dev, "Enter:%s, format=%x\n", __func__, format);
816 
817 	switch (format) {
818 	case SND_SOC_DAIFMT_CBS_CFS:
819 		return SSP_MODE_MASTER;
820 	case SND_SOC_DAIFMT_CBM_CFM:
821 		return SSP_MODE_SLAVE;
822 	default:
823 		dev_err(dai->dev, "Invalid ssp protocol: %d\n", format);
824 	}
825 
826 	return -EINVAL;
827 }
828 
829 
830 int sst_fill_ssp_config(struct snd_soc_dai *dai, unsigned int fmt)
831 {
832 	unsigned int mode;
833 	int fs_polarity;
834 	struct sst_data *ctx = snd_soc_dai_get_drvdata(dai);
835 
836 	mode = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
837 
838 	switch (mode) {
839 	case SND_SOC_DAIFMT_DSP_B:
840 		ctx->ssp_cmd.ssp_protocol = SSP_MODE_PCM;
841 		ctx->ssp_cmd.mode = sst_get_ssp_mode(dai, fmt) | (SSP_PCM_MODE_NETWORK << 1);
842 		ctx->ssp_cmd.start_delay = 0;
843 		ctx->ssp_cmd.data_polarity = 1;
844 		ctx->ssp_cmd.frame_sync_width = 1;
845 		break;
846 
847 	case SND_SOC_DAIFMT_DSP_A:
848 		ctx->ssp_cmd.ssp_protocol = SSP_MODE_PCM;
849 		ctx->ssp_cmd.mode = sst_get_ssp_mode(dai, fmt) | (SSP_PCM_MODE_NETWORK << 1);
850 		ctx->ssp_cmd.start_delay = 1;
851 		ctx->ssp_cmd.data_polarity = 1;
852 		ctx->ssp_cmd.frame_sync_width = 1;
853 		break;
854 
855 	case SND_SOC_DAIFMT_I2S:
856 		ctx->ssp_cmd.ssp_protocol = SSP_MODE_I2S;
857 		ctx->ssp_cmd.mode = sst_get_ssp_mode(dai, fmt) | (SSP_PCM_MODE_NORMAL << 1);
858 		ctx->ssp_cmd.start_delay = 1;
859 		ctx->ssp_cmd.data_polarity = 0;
860 		ctx->ssp_cmd.frame_sync_width = ctx->ssp_cmd.nb_bits_per_slots;
861 		break;
862 
863 	case SND_SOC_DAIFMT_LEFT_J:
864 		ctx->ssp_cmd.ssp_protocol = SSP_MODE_I2S;
865 		ctx->ssp_cmd.mode = sst_get_ssp_mode(dai, fmt) | (SSP_PCM_MODE_NORMAL << 1);
866 		ctx->ssp_cmd.start_delay = 0;
867 		ctx->ssp_cmd.data_polarity = 0;
868 		ctx->ssp_cmd.frame_sync_width = ctx->ssp_cmd.nb_bits_per_slots;
869 		break;
870 
871 	default:
872 		dev_dbg(dai->dev, "using default ssp configs\n");
873 	}
874 
875 	fs_polarity = sst_get_frame_sync_polarity(dai, fmt);
876 	if (fs_polarity < 0)
877 		return fs_polarity;
878 
879 	ctx->ssp_cmd.frame_sync_polarity = fs_polarity;
880 
881 	return 0;
882 }
883 
884 /**
885  * sst_ssp_config - contains SSP configuration for media UC
886  * this can be overwritten by set_dai_xxx APIs
887  */
888 static const struct sst_ssp_config sst_ssp_configs = {
889 	.ssp_id = SSP_CODEC,
890 	.bits_per_slot = 24,
891 	.slots = 4,
892 	.ssp_mode = SSP_MODE_MASTER,
893 	.pcm_mode = SSP_PCM_MODE_NETWORK,
894 	.duplex = SSP_DUPLEX,
895 	.ssp_protocol = SSP_MODE_PCM,
896 	.fs_width = 1,
897 	.fs_frequency = SSP_FS_48_KHZ,
898 	.active_slot_map = 0xF,
899 	.start_delay = 0,
900 	.frame_sync_polarity = SSP_FS_ACTIVE_HIGH,
901 	.data_polarity = 1,
902 };
903 
904 void sst_fill_ssp_defaults(struct snd_soc_dai *dai)
905 {
906 	const struct sst_ssp_config *config;
907 	struct sst_data *ctx = snd_soc_dai_get_drvdata(dai);
908 
909 	config = &sst_ssp_configs;
910 
911 	ctx->ssp_cmd.selection = config->ssp_id;
912 	ctx->ssp_cmd.nb_bits_per_slots = config->bits_per_slot;
913 	ctx->ssp_cmd.nb_slots = config->slots;
914 	ctx->ssp_cmd.mode = config->ssp_mode | (config->pcm_mode << 1);
915 	ctx->ssp_cmd.duplex = config->duplex;
916 	ctx->ssp_cmd.active_tx_slot_map = config->active_slot_map;
917 	ctx->ssp_cmd.active_rx_slot_map = config->active_slot_map;
918 	ctx->ssp_cmd.frame_sync_frequency = config->fs_frequency;
919 	ctx->ssp_cmd.frame_sync_polarity = config->frame_sync_polarity;
920 	ctx->ssp_cmd.data_polarity = config->data_polarity;
921 	ctx->ssp_cmd.frame_sync_width = config->fs_width;
922 	ctx->ssp_cmd.ssp_protocol = config->ssp_protocol;
923 	ctx->ssp_cmd.start_delay = config->start_delay;
924 	ctx->ssp_cmd.reserved1 = ctx->ssp_cmd.reserved2 = 0xFF;
925 }
926 
927 int send_ssp_cmd(struct snd_soc_dai *dai, const char *id, bool enable)
928 {
929 	struct sst_data *drv = snd_soc_dai_get_drvdata(dai);
930 	int ssp_id;
931 
932 	dev_dbg(dai->dev, "Enter: enable=%d port_name=%s\n", enable, id);
933 
934 	if (strcmp(id, "ssp0-port") == 0)
935 		ssp_id = SSP_MODEM;
936 	else if (strcmp(id, "ssp2-port") == 0)
937 		ssp_id = SSP_CODEC;
938 	else {
939 		dev_dbg(dai->dev, "port %s is not supported\n", id);
940 		return -1;
941 	}
942 
943 	SST_FILL_DEFAULT_DESTINATION(drv->ssp_cmd.header.dst);
944 	drv->ssp_cmd.header.command_id = SBA_HW_SET_SSP;
945 	drv->ssp_cmd.header.length = sizeof(struct sst_cmd_sba_hw_set_ssp)
946 				- sizeof(struct sst_dsp_header);
947 
948 	drv->ssp_cmd.selection = ssp_id;
949 	dev_dbg(dai->dev, "ssp_id: %u\n", ssp_id);
950 
951 	if (enable)
952 		drv->ssp_cmd.switch_state = SST_SWITCH_ON;
953 	else
954 		drv->ssp_cmd.switch_state = SST_SWITCH_OFF;
955 
956 	return sst_fill_and_send_cmd(drv, SST_IPC_IA_CMD, SST_FLAG_BLOCKED,
957 				SST_TASK_SBA, 0, &drv->ssp_cmd,
958 				sizeof(drv->ssp_cmd.header) + drv->ssp_cmd.header.length);
959 }
960 
961 static int sst_set_be_modules(struct snd_soc_dapm_widget *w,
962 			 struct snd_kcontrol *k, int event)
963 {
964 	int ret = 0;
965 	struct snd_soc_component *c = snd_soc_dapm_to_component(w->dapm);
966 	struct sst_data *drv = snd_soc_component_get_drvdata(c);
967 
968 	dev_dbg(c->dev, "Enter: widget=%s\n", w->name);
969 
970 	if (SND_SOC_DAPM_EVENT_ON(event)) {
971 		mutex_lock(&drv->lock);
972 		ret = sst_send_slot_map(drv);
973 		mutex_unlock(&drv->lock);
974 		if (ret)
975 			return ret;
976 		ret = sst_send_pipe_module_params(w, k);
977 	}
978 	return ret;
979 }
980 
981 static int sst_set_media_path(struct snd_soc_dapm_widget *w,
982 			      struct snd_kcontrol *k, int event)
983 {
984 	int ret = 0;
985 	struct sst_cmd_set_media_path cmd;
986 	struct snd_soc_component *c = snd_soc_dapm_to_component(w->dapm);
987 	struct sst_data *drv = snd_soc_component_get_drvdata(c);
988 	struct sst_ids *ids = w->priv;
989 
990 	dev_dbg(c->dev, "widget=%s\n", w->name);
991 	dev_dbg(c->dev, "task=%u, location=%#x\n",
992 				ids->task_id, ids->location_id);
993 
994 	if (SND_SOC_DAPM_EVENT_ON(event))
995 		cmd.switch_state = SST_PATH_ON;
996 	else
997 		cmd.switch_state = SST_PATH_OFF;
998 
999 	SST_FILL_DESTINATION(2, cmd.header.dst,
1000 			     ids->location_id, SST_DEFAULT_MODULE_ID);
1001 
1002 	/* MMX_SET_MEDIA_PATH == SBA_SET_MEDIA_PATH */
1003 	cmd.header.command_id = MMX_SET_MEDIA_PATH;
1004 	cmd.header.length = sizeof(struct sst_cmd_set_media_path)
1005 				- sizeof(struct sst_dsp_header);
1006 
1007 	ret = sst_fill_and_send_cmd(drv, SST_IPC_IA_CMD, SST_FLAG_BLOCKED,
1008 			      ids->task_id, 0, &cmd,
1009 			      sizeof(cmd.header) + cmd.header.length);
1010 	if (ret)
1011 		return ret;
1012 
1013 	if (SND_SOC_DAPM_EVENT_ON(event))
1014 		ret = sst_send_pipe_module_params(w, k);
1015 	return ret;
1016 }
1017 
1018 static int sst_set_media_loop(struct snd_soc_dapm_widget *w,
1019 			struct snd_kcontrol *k, int event)
1020 {
1021 	int ret = 0;
1022 	struct sst_cmd_sba_set_media_loop_map cmd;
1023 	struct snd_soc_component *c = snd_soc_dapm_to_component(w->dapm);
1024 	struct sst_data *drv = snd_soc_component_get_drvdata(c);
1025 	struct sst_ids *ids = w->priv;
1026 
1027 	dev_dbg(c->dev, "Enter:widget=%s\n", w->name);
1028 	if (SND_SOC_DAPM_EVENT_ON(event))
1029 		cmd.switch_state = SST_SWITCH_ON;
1030 	else
1031 		cmd.switch_state = SST_SWITCH_OFF;
1032 
1033 	SST_FILL_DESTINATION(2, cmd.header.dst,
1034 			     ids->location_id, SST_DEFAULT_MODULE_ID);
1035 
1036 	cmd.header.command_id = SBA_SET_MEDIA_LOOP_MAP;
1037 	cmd.header.length = sizeof(struct sst_cmd_sba_set_media_loop_map)
1038 				 - sizeof(struct sst_dsp_header);
1039 	cmd.param.part.cfg.rate = 2; /* 48khz */
1040 
1041 	cmd.param.part.cfg.format = ids->format; /* stereo/Mono */
1042 	cmd.param.part.cfg.s_length = 1; /* 24bit left justified */
1043 	cmd.map = 0; /* Algo sequence: Gain - DRP - FIR - IIR */
1044 
1045 	ret = sst_fill_and_send_cmd(drv, SST_IPC_IA_CMD, SST_FLAG_BLOCKED,
1046 			      SST_TASK_SBA, 0, &cmd,
1047 			      sizeof(cmd.header) + cmd.header.length);
1048 	if (ret)
1049 		return ret;
1050 
1051 	if (SND_SOC_DAPM_EVENT_ON(event))
1052 		ret = sst_send_pipe_module_params(w, k);
1053 	return ret;
1054 }
1055 
1056 static const struct snd_soc_dapm_widget sst_dapm_widgets[] = {
1057 	SST_AIF_IN("modem_in", sst_set_be_modules),
1058 	SST_AIF_IN("codec_in0", sst_set_be_modules),
1059 	SST_AIF_IN("codec_in1", sst_set_be_modules),
1060 	SST_AIF_OUT("modem_out", sst_set_be_modules),
1061 	SST_AIF_OUT("codec_out0", sst_set_be_modules),
1062 	SST_AIF_OUT("codec_out1", sst_set_be_modules),
1063 
1064 	/* Media Paths */
1065 	/* MediaX IN paths are set via ALLOC, so no SET_MEDIA_PATH command */
1066 	SST_PATH_INPUT("media0_in", SST_TASK_MMX, SST_SWM_IN_MEDIA0, sst_generic_modules_event),
1067 	SST_PATH_INPUT("media1_in", SST_TASK_MMX, SST_SWM_IN_MEDIA1, NULL),
1068 	SST_PATH_INPUT("media2_in", SST_TASK_MMX, SST_SWM_IN_MEDIA2, sst_set_media_path),
1069 	SST_PATH_INPUT("media3_in", SST_TASK_MMX, SST_SWM_IN_MEDIA3, NULL),
1070 	SST_PATH_OUTPUT("media0_out", SST_TASK_MMX, SST_SWM_OUT_MEDIA0, sst_set_media_path),
1071 	SST_PATH_OUTPUT("media1_out", SST_TASK_MMX, SST_SWM_OUT_MEDIA1, sst_set_media_path),
1072 
1073 	/* SBA PCM Paths */
1074 	SST_PATH_INPUT("pcm0_in", SST_TASK_SBA, SST_SWM_IN_PCM0, sst_set_media_path),
1075 	SST_PATH_INPUT("pcm1_in", SST_TASK_SBA, SST_SWM_IN_PCM1, sst_set_media_path),
1076 	SST_PATH_OUTPUT("pcm0_out", SST_TASK_SBA, SST_SWM_OUT_PCM0, sst_set_media_path),
1077 	SST_PATH_OUTPUT("pcm1_out", SST_TASK_SBA, SST_SWM_OUT_PCM1, sst_set_media_path),
1078 	SST_PATH_OUTPUT("pcm2_out", SST_TASK_SBA, SST_SWM_OUT_PCM2, sst_set_media_path),
1079 
1080 	/* SBA Loops */
1081 	SST_PATH_INPUT("sprot_loop_in", SST_TASK_SBA, SST_SWM_IN_SPROT_LOOP, NULL),
1082 	SST_PATH_INPUT("media_loop1_in", SST_TASK_SBA, SST_SWM_IN_MEDIA_LOOP1, NULL),
1083 	SST_PATH_INPUT("media_loop2_in", SST_TASK_SBA, SST_SWM_IN_MEDIA_LOOP2, NULL),
1084 	SST_PATH_MEDIA_LOOP_OUTPUT("sprot_loop_out", SST_TASK_SBA, SST_SWM_OUT_SPROT_LOOP, SST_FMT_STEREO, sst_set_media_loop),
1085 	SST_PATH_MEDIA_LOOP_OUTPUT("media_loop1_out", SST_TASK_SBA, SST_SWM_OUT_MEDIA_LOOP1, SST_FMT_STEREO, sst_set_media_loop),
1086 	SST_PATH_MEDIA_LOOP_OUTPUT("media_loop2_out", SST_TASK_SBA, SST_SWM_OUT_MEDIA_LOOP2, SST_FMT_STEREO, sst_set_media_loop),
1087 
1088 	/* Media Mixers */
1089 	SST_SWM_MIXER("media0_out mix 0", SND_SOC_NOPM, SST_TASK_MMX, SST_SWM_OUT_MEDIA0,
1090 		      sst_mix_media0_controls, sst_swm_mixer_event),
1091 	SST_SWM_MIXER("media1_out mix 0", SND_SOC_NOPM, SST_TASK_MMX, SST_SWM_OUT_MEDIA1,
1092 		      sst_mix_media1_controls, sst_swm_mixer_event),
1093 
1094 	/* SBA PCM mixers */
1095 	SST_SWM_MIXER("pcm0_out mix 0", SND_SOC_NOPM, SST_TASK_SBA, SST_SWM_OUT_PCM0,
1096 		      sst_mix_pcm0_controls, sst_swm_mixer_event),
1097 	SST_SWM_MIXER("pcm1_out mix 0", SND_SOC_NOPM, SST_TASK_SBA, SST_SWM_OUT_PCM1,
1098 		      sst_mix_pcm1_controls, sst_swm_mixer_event),
1099 	SST_SWM_MIXER("pcm2_out mix 0", SND_SOC_NOPM, SST_TASK_SBA, SST_SWM_OUT_PCM2,
1100 		      sst_mix_pcm2_controls, sst_swm_mixer_event),
1101 
1102 	/* SBA Loop mixers */
1103 	SST_SWM_MIXER("sprot_loop_out mix 0", SND_SOC_NOPM, SST_TASK_SBA, SST_SWM_OUT_SPROT_LOOP,
1104 		      sst_mix_sprot_l0_controls, sst_swm_mixer_event),
1105 	SST_SWM_MIXER("media_loop1_out mix 0", SND_SOC_NOPM, SST_TASK_SBA, SST_SWM_OUT_MEDIA_LOOP1,
1106 		      sst_mix_media_l1_controls, sst_swm_mixer_event),
1107 	SST_SWM_MIXER("media_loop2_out mix 0", SND_SOC_NOPM, SST_TASK_SBA, SST_SWM_OUT_MEDIA_LOOP2,
1108 		      sst_mix_media_l2_controls, sst_swm_mixer_event),
1109 
1110 	/* SBA Backend mixers */
1111 	SST_SWM_MIXER("codec_out0 mix 0", SND_SOC_NOPM, SST_TASK_SBA, SST_SWM_OUT_CODEC0,
1112 		      sst_mix_codec0_controls, sst_swm_mixer_event),
1113 	SST_SWM_MIXER("codec_out1 mix 0", SND_SOC_NOPM, SST_TASK_SBA, SST_SWM_OUT_CODEC1,
1114 		      sst_mix_codec1_controls, sst_swm_mixer_event),
1115 	SST_SWM_MIXER("modem_out mix 0", SND_SOC_NOPM, SST_TASK_SBA, SST_SWM_OUT_MODEM,
1116 		      sst_mix_modem_controls, sst_swm_mixer_event),
1117 
1118 };
1119 
1120 static const struct snd_soc_dapm_route intercon[] = {
1121 	{"media0_in", NULL, "Compress Playback"},
1122 	{"media1_in", NULL, "Headset Playback"},
1123 	{"media2_in", NULL, "pcm0_out"},
1124 	{"media3_in", NULL, "Deepbuffer Playback"},
1125 
1126 	{"media0_out mix 0", "media0_in Switch", "media0_in"},
1127 	{"media0_out mix 0", "media1_in Switch", "media1_in"},
1128 	{"media0_out mix 0", "media2_in Switch", "media2_in"},
1129 	{"media0_out mix 0", "media3_in Switch", "media3_in"},
1130 	{"media1_out mix 0", "media0_in Switch", "media0_in"},
1131 	{"media1_out mix 0", "media1_in Switch", "media1_in"},
1132 	{"media1_out mix 0", "media2_in Switch", "media2_in"},
1133 	{"media1_out mix 0", "media3_in Switch", "media3_in"},
1134 
1135 	{"media0_out", NULL, "media0_out mix 0"},
1136 	{"media1_out", NULL, "media1_out mix 0"},
1137 	{"pcm0_in", NULL, "media0_out"},
1138 	{"pcm1_in", NULL, "media1_out"},
1139 
1140 	{"Headset Capture", NULL, "pcm1_out"},
1141 	{"Headset Capture", NULL, "pcm2_out"},
1142 	{"pcm0_out", NULL, "pcm0_out mix 0"},
1143 	SST_SBA_MIXER_GRAPH_MAP("pcm0_out mix 0"),
1144 	{"pcm1_out", NULL, "pcm1_out mix 0"},
1145 	SST_SBA_MIXER_GRAPH_MAP("pcm1_out mix 0"),
1146 	{"pcm2_out", NULL, "pcm2_out mix 0"},
1147 	SST_SBA_MIXER_GRAPH_MAP("pcm2_out mix 0"),
1148 
1149 	{"media_loop1_in", NULL, "media_loop1_out"},
1150 	{"media_loop1_out", NULL, "media_loop1_out mix 0"},
1151 	SST_SBA_MIXER_GRAPH_MAP("media_loop1_out mix 0"),
1152 	{"media_loop2_in", NULL, "media_loop2_out"},
1153 	{"media_loop2_out", NULL, "media_loop2_out mix 0"},
1154 	SST_SBA_MIXER_GRAPH_MAP("media_loop2_out mix 0"),
1155 	{"sprot_loop_in", NULL, "sprot_loop_out"},
1156 	{"sprot_loop_out", NULL, "sprot_loop_out mix 0"},
1157 	SST_SBA_MIXER_GRAPH_MAP("sprot_loop_out mix 0"),
1158 
1159 	{"codec_out0", NULL, "codec_out0 mix 0"},
1160 	SST_SBA_MIXER_GRAPH_MAP("codec_out0 mix 0"),
1161 	{"codec_out1", NULL, "codec_out1 mix 0"},
1162 	SST_SBA_MIXER_GRAPH_MAP("codec_out1 mix 0"),
1163 	{"modem_out", NULL, "modem_out mix 0"},
1164 	SST_SBA_MIXER_GRAPH_MAP("modem_out mix 0"),
1165 
1166 
1167 };
1168 static const char * const slot_names[] = {
1169 	"none",
1170 	"slot 0", "slot 1", "slot 2", "slot 3",
1171 	"slot 4", "slot 5", "slot 6", "slot 7", /* not supported by FW */
1172 };
1173 
1174 static const char * const channel_names[] = {
1175 	"none",
1176 	"codec_out0_0", "codec_out0_1", "codec_out1_0", "codec_out1_1",
1177 	"codec_out2_0", "codec_out2_1", "codec_out3_0", "codec_out3_1", /* not supported by FW */
1178 };
1179 
1180 #define SST_INTERLEAVER(xpname, slot_name, slotno) \
1181 	SST_SSP_SLOT_CTL(xpname, "tx interleaver", slot_name, slotno, true, \
1182 			 channel_names, sst_slot_get, sst_slot_put)
1183 
1184 #define SST_DEINTERLEAVER(xpname, channel_name, channel_no) \
1185 	SST_SSP_SLOT_CTL(xpname, "rx deinterleaver", channel_name, channel_no, false, \
1186 			 slot_names, sst_slot_get, sst_slot_put)
1187 
1188 static const struct snd_kcontrol_new sst_slot_controls[] = {
1189 	SST_INTERLEAVER("codec_out", "slot 0", 0),
1190 	SST_INTERLEAVER("codec_out", "slot 1", 1),
1191 	SST_INTERLEAVER("codec_out", "slot 2", 2),
1192 	SST_INTERLEAVER("codec_out", "slot 3", 3),
1193 	SST_DEINTERLEAVER("codec_in", "codec_in0_0", 0),
1194 	SST_DEINTERLEAVER("codec_in", "codec_in0_1", 1),
1195 	SST_DEINTERLEAVER("codec_in", "codec_in1_0", 2),
1196 	SST_DEINTERLEAVER("codec_in", "codec_in1_1", 3),
1197 };
1198 
1199 /* Gain helper with min/max set */
1200 #define SST_GAIN(name, path_id, task_id, instance, gain_var)				\
1201 	SST_GAIN_KCONTROLS(name, "Gain", SST_GAIN_MIN_VALUE, SST_GAIN_MAX_VALUE,	\
1202 		SST_GAIN_TC_MIN, SST_GAIN_TC_MAX,					\
1203 		sst_gain_get, sst_gain_put,						\
1204 		SST_MODULE_ID_GAIN_CELL, path_id, instance, task_id,			\
1205 		sst_gain_tlv_common, gain_var)
1206 
1207 #define SST_VOLUME(name, path_id, task_id, instance, gain_var)				\
1208 	SST_GAIN_KCONTROLS(name, "Volume", SST_GAIN_MIN_VALUE, SST_GAIN_MAX_VALUE,	\
1209 		SST_GAIN_TC_MIN, SST_GAIN_TC_MAX,					\
1210 		sst_gain_get, sst_gain_put,						\
1211 		SST_MODULE_ID_VOLUME, path_id, instance, task_id,			\
1212 		sst_gain_tlv_common, gain_var)
1213 
1214 static struct sst_gain_value sst_gains[];
1215 
1216 static const struct snd_kcontrol_new sst_gain_controls[] = {
1217 	SST_GAIN("media0_in", SST_PATH_INDEX_MEDIA0_IN, SST_TASK_MMX, 0, &sst_gains[0]),
1218 	SST_GAIN("media1_in", SST_PATH_INDEX_MEDIA1_IN, SST_TASK_MMX, 0, &sst_gains[1]),
1219 	SST_GAIN("media2_in", SST_PATH_INDEX_MEDIA2_IN, SST_TASK_MMX, 0, &sst_gains[2]),
1220 	SST_GAIN("media3_in", SST_PATH_INDEX_MEDIA3_IN, SST_TASK_MMX, 0, &sst_gains[3]),
1221 
1222 	SST_GAIN("pcm0_in", SST_PATH_INDEX_PCM0_IN, SST_TASK_SBA, 0, &sst_gains[4]),
1223 	SST_GAIN("pcm1_in", SST_PATH_INDEX_PCM1_IN, SST_TASK_SBA, 0, &sst_gains[5]),
1224 	SST_GAIN("pcm1_out", SST_PATH_INDEX_PCM1_OUT, SST_TASK_SBA, 0, &sst_gains[6]),
1225 	SST_GAIN("pcm2_out", SST_PATH_INDEX_PCM2_OUT, SST_TASK_SBA, 0, &sst_gains[7]),
1226 
1227 	SST_GAIN("codec_in0", SST_PATH_INDEX_CODEC_IN0, SST_TASK_SBA, 0, &sst_gains[8]),
1228 	SST_GAIN("codec_in1", SST_PATH_INDEX_CODEC_IN1, SST_TASK_SBA, 0, &sst_gains[9]),
1229 	SST_GAIN("codec_out0", SST_PATH_INDEX_CODEC_OUT0, SST_TASK_SBA, 0, &sst_gains[10]),
1230 	SST_GAIN("codec_out1", SST_PATH_INDEX_CODEC_OUT1, SST_TASK_SBA, 0, &sst_gains[11]),
1231 	SST_GAIN("media_loop1_out", SST_PATH_INDEX_MEDIA_LOOP1_OUT, SST_TASK_SBA, 0, &sst_gains[12]),
1232 	SST_GAIN("media_loop2_out", SST_PATH_INDEX_MEDIA_LOOP2_OUT, SST_TASK_SBA, 0, &sst_gains[13]),
1233 	SST_GAIN("sprot_loop_out", SST_PATH_INDEX_SPROT_LOOP_OUT, SST_TASK_SBA, 0, &sst_gains[14]),
1234 	SST_VOLUME("media0_in", SST_PATH_INDEX_MEDIA0_IN, SST_TASK_MMX, 0, &sst_gains[15]),
1235 	SST_GAIN("modem_in", SST_PATH_INDEX_MODEM_IN, SST_TASK_SBA, 0, &sst_gains[16]),
1236 	SST_GAIN("modem_out", SST_PATH_INDEX_MODEM_OUT, SST_TASK_SBA, 0, &sst_gains[17]),
1237 
1238 };
1239 
1240 #define SST_GAIN_NUM_CONTROLS 3
1241 /* the SST_GAIN macro above will create three alsa controls for each
1242  * instance invoked, gain, mute and ramp duration, which use the same gain
1243  * cell sst_gain to keep track of data
1244  * To calculate number of gain cell instances we need to device by 3 in
1245  * below caulcation for gain cell memory.
1246  * This gets rid of static number and issues while adding new controls
1247  */
1248 static struct sst_gain_value sst_gains[ARRAY_SIZE(sst_gain_controls)/SST_GAIN_NUM_CONTROLS];
1249 
1250 static const struct snd_kcontrol_new sst_algo_controls[] = {
1251 	SST_ALGO_KCONTROL_BYTES("media_loop1_out", "fir", 272, SST_MODULE_ID_FIR_24,
1252 		 SST_PATH_INDEX_MEDIA_LOOP1_OUT, 0, SST_TASK_SBA, SBA_VB_SET_FIR),
1253 	SST_ALGO_KCONTROL_BYTES("media_loop1_out", "iir", 300, SST_MODULE_ID_IIR_24,
1254 		SST_PATH_INDEX_MEDIA_LOOP1_OUT, 0, SST_TASK_SBA, SBA_VB_SET_IIR),
1255 	SST_ALGO_KCONTROL_BYTES("media_loop1_out", "mdrp", 286, SST_MODULE_ID_MDRP,
1256 		SST_PATH_INDEX_MEDIA_LOOP1_OUT, 0, SST_TASK_SBA, SBA_SET_MDRP),
1257 	SST_ALGO_KCONTROL_BYTES("media_loop2_out", "fir", 272, SST_MODULE_ID_FIR_24,
1258 		SST_PATH_INDEX_MEDIA_LOOP2_OUT, 0, SST_TASK_SBA, SBA_VB_SET_FIR),
1259 	SST_ALGO_KCONTROL_BYTES("media_loop2_out", "iir", 300, SST_MODULE_ID_IIR_24,
1260 		SST_PATH_INDEX_MEDIA_LOOP2_OUT, 0, SST_TASK_SBA, SBA_VB_SET_IIR),
1261 	SST_ALGO_KCONTROL_BYTES("media_loop2_out", "mdrp", 286, SST_MODULE_ID_MDRP,
1262 		SST_PATH_INDEX_MEDIA_LOOP2_OUT, 0, SST_TASK_SBA, SBA_SET_MDRP),
1263 	SST_ALGO_KCONTROL_BYTES("sprot_loop_out", "lpro", 192, SST_MODULE_ID_SPROT,
1264 		SST_PATH_INDEX_SPROT_LOOP_OUT, 0, SST_TASK_SBA, SBA_VB_LPRO),
1265 	SST_ALGO_KCONTROL_BYTES("codec_in0", "dcr", 52, SST_MODULE_ID_FILT_DCR,
1266 		SST_PATH_INDEX_CODEC_IN0, 0, SST_TASK_SBA, SBA_VB_SET_IIR),
1267 	SST_ALGO_KCONTROL_BYTES("codec_in1", "dcr", 52, SST_MODULE_ID_FILT_DCR,
1268 		SST_PATH_INDEX_CODEC_IN1, 0, SST_TASK_SBA, SBA_VB_SET_IIR),
1269 
1270 };
1271 
1272 static int sst_algo_control_init(struct device *dev)
1273 {
1274 	int i = 0;
1275 	struct sst_algo_control *bc;
1276 	/*allocate space to cache the algo parameters in the driver*/
1277 	for (i = 0; i < ARRAY_SIZE(sst_algo_controls); i++) {
1278 		bc = (struct sst_algo_control *)sst_algo_controls[i].private_value;
1279 		bc->params = devm_kzalloc(dev, bc->max, GFP_KERNEL);
1280 		if (bc->params == NULL)
1281 			return -ENOMEM;
1282 	}
1283 	return 0;
1284 }
1285 
1286 static bool is_sst_dapm_widget(struct snd_soc_dapm_widget *w)
1287 {
1288 	switch (w->id) {
1289 	case snd_soc_dapm_pga:
1290 	case snd_soc_dapm_aif_in:
1291 	case snd_soc_dapm_aif_out:
1292 	case snd_soc_dapm_input:
1293 	case snd_soc_dapm_output:
1294 	case snd_soc_dapm_mixer:
1295 		return true;
1296 	default:
1297 		return false;
1298 	}
1299 }
1300 
1301 /**
1302  * sst_send_pipe_gains - send gains for the front-end DAIs
1303  *
1304  * The gains in the pipes connected to the front-ends are muted/unmuted
1305  * automatically via the digital_mute() DAPM callback. This function sends the
1306  * gains for the front-end pipes.
1307  */
1308 int sst_send_pipe_gains(struct snd_soc_dai *dai, int stream, int mute)
1309 {
1310 	struct sst_data *drv = snd_soc_dai_get_drvdata(dai);
1311 	struct snd_soc_dapm_widget *w;
1312 	struct snd_soc_dapm_path *p = NULL;
1313 
1314 	dev_dbg(dai->dev, "enter, dai-name=%s dir=%d\n", dai->name, stream);
1315 
1316 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1317 		dev_dbg(dai->dev, "Stream name=%s\n",
1318 				dai->playback_widget->name);
1319 		w = dai->playback_widget;
1320 		snd_soc_dapm_widget_for_each_sink_path(w, p) {
1321 			if (p->connected && !p->connected(w, p->sink))
1322 				continue;
1323 
1324 			if (p->connect && p->sink->power &&
1325 					is_sst_dapm_widget(p->sink)) {
1326 				struct sst_ids *ids = p->sink->priv;
1327 
1328 				dev_dbg(dai->dev, "send gains for widget=%s\n",
1329 						p->sink->name);
1330 				mutex_lock(&drv->lock);
1331 				sst_set_pipe_gain(ids, drv, mute);
1332 				mutex_unlock(&drv->lock);
1333 			}
1334 		}
1335 	} else {
1336 		dev_dbg(dai->dev, "Stream name=%s\n",
1337 				dai->capture_widget->name);
1338 		w = dai->capture_widget;
1339 		snd_soc_dapm_widget_for_each_source_path(w, p) {
1340 			if (p->connected && !p->connected(w, p->source))
1341 				continue;
1342 
1343 			if (p->connect &&  p->source->power &&
1344 					is_sst_dapm_widget(p->source)) {
1345 				struct sst_ids *ids = p->source->priv;
1346 
1347 				dev_dbg(dai->dev, "send gain for widget=%s\n",
1348 						p->source->name);
1349 				mutex_lock(&drv->lock);
1350 				sst_set_pipe_gain(ids, drv, mute);
1351 				mutex_unlock(&drv->lock);
1352 			}
1353 		}
1354 	}
1355 	return 0;
1356 }
1357 
1358 /**
1359  * sst_fill_module_list - populate the list of modules/gains for a pipe
1360  *
1361  *
1362  * Fills the widget pointer in the kcontrol private data, and also fills the
1363  * kcontrol pointer in the widget private data.
1364  *
1365  * Widget pointer is used to send the algo/gain in the .put() handler if the
1366  * widget is powerd on.
1367  *
1368  * Kcontrol pointer is used to send the algo/gain in the widget power ON/OFF
1369  * event handler. Each widget (pipe) has multiple algos stored in the algo_list.
1370  */
1371 static int sst_fill_module_list(struct snd_kcontrol *kctl,
1372 	 struct snd_soc_dapm_widget *w, int type)
1373 {
1374 	struct sst_module *module = NULL;
1375 	struct snd_soc_component *c = snd_soc_dapm_to_component(w->dapm);
1376 	struct sst_ids *ids = w->priv;
1377 	int ret = 0;
1378 
1379 	module = devm_kzalloc(c->dev, sizeof(*module), GFP_KERNEL);
1380 	if (!module)
1381 		return -ENOMEM;
1382 
1383 	if (type == SST_MODULE_GAIN) {
1384 		struct sst_gain_mixer_control *mc = (void *)kctl->private_value;
1385 
1386 		mc->w = w;
1387 		module->kctl = kctl;
1388 		list_add_tail(&module->node, &ids->gain_list);
1389 	} else if (type == SST_MODULE_ALGO) {
1390 		struct sst_algo_control *bc = (void *)kctl->private_value;
1391 
1392 		bc->w = w;
1393 		module->kctl = kctl;
1394 		list_add_tail(&module->node, &ids->algo_list);
1395 	} else {
1396 		dev_err(c->dev, "invoked for unknown type %d module %s",
1397 				type, kctl->id.name);
1398 		ret = -EINVAL;
1399 	}
1400 
1401 	return ret;
1402 }
1403 
1404 /**
1405  * sst_fill_widget_module_info - fill list of gains/algos for the pipe
1406  * @widget:	pipe modelled as a DAPM widget
1407  *
1408  * Fill the list of gains/algos for the widget by looking at all the card
1409  * controls and comparing the name of the widget with the first part of control
1410  * name. First part of control name contains the pipe name (widget name).
1411  */
1412 static int sst_fill_widget_module_info(struct snd_soc_dapm_widget *w,
1413 	struct snd_soc_component *component)
1414 {
1415 	struct snd_kcontrol *kctl;
1416 	int index, ret = 0;
1417 	struct snd_card *card = component->card->snd_card;
1418 	char *idx;
1419 
1420 	down_read(&card->controls_rwsem);
1421 
1422 	list_for_each_entry(kctl, &card->controls, list) {
1423 		idx = strchr(kctl->id.name, ' ');
1424 		if (idx == NULL)
1425 			continue;
1426 		index = idx - (char*)kctl->id.name;
1427 		if (strncmp(kctl->id.name, w->name, index))
1428 			continue;
1429 
1430 		if (strstr(kctl->id.name, "Volume"))
1431 			ret = sst_fill_module_list(kctl, w, SST_MODULE_GAIN);
1432 
1433 		else if (strstr(kctl->id.name, "params"))
1434 			ret = sst_fill_module_list(kctl, w, SST_MODULE_ALGO);
1435 
1436 		else if (strstr(kctl->id.name, "Switch") &&
1437 			 strstr(kctl->id.name, "Gain")) {
1438 			struct sst_gain_mixer_control *mc =
1439 						(void *)kctl->private_value;
1440 
1441 			mc->w = w;
1442 
1443 		} else if (strstr(kctl->id.name, "interleaver")) {
1444 			struct sst_enum *e = (void *)kctl->private_value;
1445 
1446 			e->w = w;
1447 
1448 		} else if (strstr(kctl->id.name, "deinterleaver")) {
1449 			struct sst_enum *e = (void *)kctl->private_value;
1450 
1451 			e->w = w;
1452 		}
1453 
1454 		if (ret < 0) {
1455 			up_read(&card->controls_rwsem);
1456 			return ret;
1457 		}
1458 	}
1459 
1460 	up_read(&card->controls_rwsem);
1461 	return 0;
1462 }
1463 
1464 /**
1465  * sst_fill_linked_widgets - fill the parent pointer for the linked widget
1466  */
1467 static void sst_fill_linked_widgets(struct snd_soc_component *component,
1468 						struct sst_ids *ids)
1469 {
1470 	struct snd_soc_dapm_widget *w;
1471 	unsigned int len = strlen(ids->parent_wname);
1472 
1473 	list_for_each_entry(w, &component->card->widgets, list) {
1474 		if (!strncmp(ids->parent_wname, w->name, len)) {
1475 			ids->parent_w = w;
1476 			break;
1477 		}
1478 	}
1479 }
1480 
1481 /**
1482  * sst_map_modules_to_pipe - fill algo/gains list for all pipes
1483  */
1484 static int sst_map_modules_to_pipe(struct snd_soc_component *component)
1485 {
1486 	struct snd_soc_dapm_widget *w;
1487 	int ret = 0;
1488 
1489 	list_for_each_entry(w, &component->card->widgets, list) {
1490 		if (is_sst_dapm_widget(w) && (w->priv)) {
1491 			struct sst_ids *ids = w->priv;
1492 
1493 			dev_dbg(component->dev, "widget type=%d name=%s\n",
1494 					w->id, w->name);
1495 			INIT_LIST_HEAD(&ids->algo_list);
1496 			INIT_LIST_HEAD(&ids->gain_list);
1497 			ret = sst_fill_widget_module_info(w, component);
1498 
1499 			if (ret < 0)
1500 				return ret;
1501 
1502 			/* fill linked widgets */
1503 			if (ids->parent_wname !=  NULL)
1504 				sst_fill_linked_widgets(component, ids);
1505 		}
1506 	}
1507 	return 0;
1508 }
1509 
1510 int sst_dsp_init_v2_dpcm(struct snd_soc_component *component)
1511 {
1512 	int i, ret = 0;
1513 	struct snd_soc_dapm_context *dapm =
1514 			snd_soc_component_get_dapm(component);
1515 	struct sst_data *drv = snd_soc_component_get_drvdata(component);
1516 	unsigned int gains = ARRAY_SIZE(sst_gain_controls)/3;
1517 
1518 	drv->byte_stream = devm_kzalloc(component->dev,
1519 					SST_MAX_BIN_BYTES, GFP_KERNEL);
1520 	if (!drv->byte_stream)
1521 		return -ENOMEM;
1522 
1523 	snd_soc_dapm_new_controls(dapm, sst_dapm_widgets,
1524 			ARRAY_SIZE(sst_dapm_widgets));
1525 	snd_soc_dapm_add_routes(dapm, intercon,
1526 			ARRAY_SIZE(intercon));
1527 	snd_soc_dapm_new_widgets(dapm->card);
1528 
1529 	for (i = 0; i < gains; i++) {
1530 		sst_gains[i].mute = SST_GAIN_MUTE_DEFAULT;
1531 		sst_gains[i].l_gain = SST_GAIN_VOLUME_DEFAULT;
1532 		sst_gains[i].r_gain = SST_GAIN_VOLUME_DEFAULT;
1533 		sst_gains[i].ramp_duration = SST_GAIN_RAMP_DURATION_DEFAULT;
1534 	}
1535 
1536 	ret = snd_soc_add_component_controls(component, sst_gain_controls,
1537 			ARRAY_SIZE(sst_gain_controls));
1538 	if (ret)
1539 		return ret;
1540 
1541 	/* Initialize algo control params */
1542 	ret = sst_algo_control_init(component->dev);
1543 	if (ret)
1544 		return ret;
1545 	ret = snd_soc_add_component_controls(component, sst_algo_controls,
1546 			ARRAY_SIZE(sst_algo_controls));
1547 	if (ret)
1548 		return ret;
1549 
1550 	ret = snd_soc_add_component_controls(component, sst_slot_controls,
1551 			ARRAY_SIZE(sst_slot_controls));
1552 	if (ret)
1553 		return ret;
1554 
1555 	ret = sst_map_modules_to_pipe(component);
1556 
1557 	return ret;
1558 }
1559