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