Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002  /*
0003  *  sst-atom-controls.c - Intel MID Platform driver DPCM ALSA controls for Mrfld
0004  *
0005  *  Copyright (C) 2013-14 Intel Corp
0006  *  Author: Omair Mohammed Abdullah <omair.m.abdullah@intel.com>
0007  *  Vinod Koul <vinod.koul@intel.com>
0008  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0009  *
0010  *  In the dpcm driver modelling when a particular FE/BE/Mixer/Pipe is active
0011  *  we forward the settings and parameters, rest we keep the values  in
0012  *  driver and forward when DAPM enables them
0013  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0014  */
0015 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0016 
0017 #include <linux/slab.h>
0018 #include <sound/soc.h>
0019 #include <sound/tlv.h>
0020 #include "sst-mfld-platform.h"
0021 #include "sst-atom-controls.h"
0022 
0023 static int sst_fill_byte_control(struct sst_data *drv,
0024                      u8 ipc_msg, u8 block,
0025                      u8 task_id, u8 pipe_id,
0026                      u16 len, void *cmd_data)
0027 {
0028     struct snd_sst_bytes_v2 *byte_data = drv->byte_stream;
0029 
0030     byte_data->type = SST_CMD_BYTES_SET;
0031     byte_data->ipc_msg = ipc_msg;
0032     byte_data->block = block;
0033     byte_data->task_id = task_id;
0034     byte_data->pipe_id = pipe_id;
0035 
0036     if (len > SST_MAX_BIN_BYTES - sizeof(*byte_data)) {
0037         dev_err(&drv->pdev->dev, "command length too big (%u)", len);
0038         return -EINVAL;
0039     }
0040     byte_data->len = len;
0041     memcpy(byte_data->bytes, cmd_data, len);
0042     print_hex_dump_bytes("writing to lpe: ", DUMP_PREFIX_OFFSET,
0043                  byte_data, len + sizeof(*byte_data));
0044     return 0;
0045 }
0046 
0047 static int sst_fill_and_send_cmd_unlocked(struct sst_data *drv,
0048                  u8 ipc_msg, u8 block, u8 task_id, u8 pipe_id,
0049                  void *cmd_data, u16 len)
0050 {
0051     int ret = 0;
0052 
0053     WARN_ON(!mutex_is_locked(&drv->lock));
0054 
0055     ret = sst_fill_byte_control(drv, ipc_msg,
0056                 block, task_id, pipe_id, len, cmd_data);
0057     if (ret < 0)
0058         return ret;
0059     return sst->ops->send_byte_stream(sst->dev, drv->byte_stream);
0060 }
0061 
0062 /**
0063  * sst_fill_and_send_cmd - generate the IPC message and send it to the FW
0064  * @drv: sst_data
0065  * @ipc_msg: type of IPC (CMD, SET_PARAMS, GET_PARAMS)
0066  * @block: block index
0067  * @task_id: task index
0068  * @pipe_id: pipe index
0069  * @cmd_data: the IPC payload
0070  * @len: length of data to be sent
0071  */
0072 static int sst_fill_and_send_cmd(struct sst_data *drv,
0073                  u8 ipc_msg, u8 block, u8 task_id, u8 pipe_id,
0074                  void *cmd_data, u16 len)
0075 {
0076     int ret;
0077 
0078     mutex_lock(&drv->lock);
0079     ret = sst_fill_and_send_cmd_unlocked(drv, ipc_msg, block,
0080                     task_id, pipe_id, cmd_data, len);
0081     mutex_unlock(&drv->lock);
0082 
0083     return ret;
0084 }
0085 
0086 /*
0087  * tx map value is a bitfield where each bit represents a FW channel
0088  *
0089  *          3 2 1 0     # 0 = codec0, 1 = codec1
0090  *          RLRLRLRL    # 3, 4 = reserved
0091  *
0092  * e.g. slot 0 rx map = 00001100b -> data from slot 0 goes into codec_in1 L,R
0093  */
0094 static u8 sst_ssp_tx_map[SST_MAX_TDM_SLOTS] = {
0095     0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80, /* default rx map */
0096 };
0097 
0098 /*
0099  * rx map value is a bitfield where each bit represents a slot
0100  *
0101  *            76543210  # 0 = slot 0, 1 = slot 1
0102  *
0103  * e.g. codec1_0 tx map = 00000101b -> data from codec_out1_0 goes into slot 0, 2
0104  */
0105 static u8 sst_ssp_rx_map[SST_MAX_TDM_SLOTS] = {
0106     0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80, /* default tx map */
0107 };
0108 
0109 /*
0110  * NOTE: this is invoked with lock held
0111  */
0112 static int sst_send_slot_map(struct sst_data *drv)
0113 {
0114     struct sst_param_sba_ssp_slot_map cmd;
0115 
0116     SST_FILL_DEFAULT_DESTINATION(cmd.header.dst);
0117     cmd.header.command_id = SBA_SET_SSP_SLOT_MAP;
0118     cmd.header.length = sizeof(struct sst_param_sba_ssp_slot_map)
0119                 - sizeof(struct sst_dsp_header);
0120 
0121     cmd.param_id = SBA_SET_SSP_SLOT_MAP;
0122     cmd.param_len = sizeof(cmd.rx_slot_map) + sizeof(cmd.tx_slot_map)
0123                     + sizeof(cmd.ssp_index);
0124     cmd.ssp_index = SSP_CODEC;
0125 
0126     memcpy(cmd.rx_slot_map, &sst_ssp_tx_map[0], sizeof(cmd.rx_slot_map));
0127     memcpy(cmd.tx_slot_map, &sst_ssp_rx_map[0], sizeof(cmd.tx_slot_map));
0128 
0129     return sst_fill_and_send_cmd_unlocked(drv, SST_IPC_IA_SET_PARAMS,
0130             SST_FLAG_BLOCKED, SST_TASK_SBA, 0, &cmd,
0131                   sizeof(cmd.header) + cmd.header.length);
0132 }
0133 
0134 static int sst_slot_enum_info(struct snd_kcontrol *kcontrol,
0135                struct snd_ctl_elem_info *uinfo)
0136 {
0137     struct sst_enum *e = (struct sst_enum *)kcontrol->private_value;
0138 
0139     uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
0140     uinfo->count = 1;
0141     uinfo->value.enumerated.items = e->max;
0142 
0143     if (uinfo->value.enumerated.item > e->max - 1)
0144         uinfo->value.enumerated.item = e->max - 1;
0145     strcpy(uinfo->value.enumerated.name,
0146         e->texts[uinfo->value.enumerated.item]);
0147 
0148     return 0;
0149 }
0150 
0151 /**
0152  * sst_slot_get - get the status of the interleaver/deinterleaver control
0153  * @kcontrol: control pointer
0154  * @ucontrol: User data
0155  * Searches the map where the control status is stored, and gets the
0156  * channel/slot which is currently set for this enumerated control. Since it is
0157  * an enumerated control, there is only one possible value.
0158  */
0159 static int sst_slot_get(struct snd_kcontrol *kcontrol,
0160             struct snd_ctl_elem_value *ucontrol)
0161 {
0162     struct sst_enum *e = (void *)kcontrol->private_value;
0163     struct snd_soc_component *c = snd_kcontrol_chip(kcontrol);
0164     struct sst_data *drv = snd_soc_component_get_drvdata(c);
0165     unsigned int ctl_no = e->reg;
0166     unsigned int is_tx = e->tx;
0167     unsigned int val, mux;
0168     u8 *map = is_tx ? sst_ssp_rx_map : sst_ssp_tx_map;
0169 
0170     mutex_lock(&drv->lock);
0171     val = 1 << ctl_no;
0172     /* search which slot/channel has this bit set - there should be only one */
0173     for (mux = e->max; mux > 0;  mux--)
0174         if (map[mux - 1] & val)
0175             break;
0176 
0177     ucontrol->value.enumerated.item[0] = mux;
0178     mutex_unlock(&drv->lock);
0179 
0180     dev_dbg(c->dev, "%s - %s map = %#x\n",
0181             is_tx ? "tx channel" : "rx slot",
0182              e->texts[mux], mux ? map[mux - 1] : -1);
0183     return 0;
0184 }
0185 
0186 /* sst_check_and_send_slot_map - helper for checking power state and sending
0187  * slot map cmd
0188  *
0189  * called with lock held
0190  */
0191 static int sst_check_and_send_slot_map(struct sst_data *drv, struct snd_kcontrol *kcontrol)
0192 {
0193     struct sst_enum *e = (void *)kcontrol->private_value;
0194     int ret = 0;
0195 
0196     if (e->w && e->w->power)
0197         ret = sst_send_slot_map(drv);
0198     else if (!e->w)
0199         dev_err(&drv->pdev->dev, "Slot control: %s doesn't have DAPM widget!!!\n",
0200                 kcontrol->id.name);
0201     return ret;
0202 }
0203 
0204 /**
0205  * sst_slot_put - set the status of interleaver/deinterleaver control
0206  * @kcontrol: control pointer
0207  * @ucontrol: User data
0208  * (de)interleaver controls are defined in opposite sense to be user-friendly
0209  *
0210  * Instead of the enum value being the value written to the register, it is the
0211  * register address; and the kcontrol number (register num) is the value written
0212  * to the register. This is so that there can be only one value for each
0213  * slot/channel since there is only one control for each slot/channel.
0214  *
0215  * This means that whenever an enum is set, we need to clear the bit
0216  * for that kcontrol_no for all the interleaver OR deinterleaver registers
0217  */
0218 static int sst_slot_put(struct snd_kcontrol *kcontrol,
0219             struct snd_ctl_elem_value *ucontrol)
0220 {
0221     struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol);
0222     struct sst_data *drv = snd_soc_component_get_drvdata(c);
0223     struct sst_enum *e = (void *)kcontrol->private_value;
0224     int i, ret = 0;
0225     unsigned int ctl_no = e->reg;
0226     unsigned int is_tx = e->tx;
0227     unsigned int slot_channel_no;
0228     unsigned int val, mux;
0229     u8 *map;
0230 
0231     map = is_tx ? sst_ssp_rx_map : sst_ssp_tx_map;
0232 
0233     val = 1 << ctl_no;
0234     mux = ucontrol->value.enumerated.item[0];
0235     if (mux > e->max - 1)
0236         return -EINVAL;
0237 
0238     mutex_lock(&drv->lock);
0239     /* first clear all registers of this bit */
0240     for (i = 0; i < e->max; i++)
0241         map[i] &= ~val;
0242 
0243     if (mux == 0) {
0244         /* kctl set to 'none' and we reset the bits so send IPC */
0245         ret = sst_check_and_send_slot_map(drv, kcontrol);
0246 
0247         mutex_unlock(&drv->lock);
0248         return ret;
0249     }
0250 
0251     /* offset by one to take "None" into account */
0252     slot_channel_no = mux - 1;
0253     map[slot_channel_no] |= val;
0254 
0255     dev_dbg(c->dev, "%s %s map = %#x\n",
0256             is_tx ? "tx channel" : "rx slot",
0257             e->texts[mux], map[slot_channel_no]);
0258 
0259     ret = sst_check_and_send_slot_map(drv, kcontrol);
0260 
0261     mutex_unlock(&drv->lock);
0262     return ret;
0263 }
0264 
0265 static int sst_send_algo_cmd(struct sst_data *drv,
0266                   struct sst_algo_control *bc)
0267 {
0268     int len, ret = 0;
0269     struct sst_cmd_set_params *cmd;
0270 
0271     /*bc->max includes sizeof algos + length field*/
0272     len = sizeof(cmd->dst) + sizeof(cmd->command_id) + bc->max;
0273 
0274     cmd = kzalloc(len, GFP_KERNEL);
0275     if (cmd == NULL)
0276         return -ENOMEM;
0277 
0278     SST_FILL_DESTINATION(2, cmd->dst, bc->pipe_id, bc->module_id);
0279     cmd->command_id = bc->cmd_id;
0280     memcpy(cmd->params, bc->params, bc->max);
0281 
0282     ret = sst_fill_and_send_cmd_unlocked(drv, SST_IPC_IA_SET_PARAMS,
0283                 SST_FLAG_BLOCKED, bc->task_id, 0, cmd, len);
0284     kfree(cmd);
0285     return ret;
0286 }
0287 
0288 /**
0289  * sst_find_and_send_pipe_algo - send all the algo parameters for a pipe
0290  * @drv: sst_data
0291  * @pipe: string identifier
0292  * @ids: list of algorithms
0293  * The algos which are in each pipeline are sent to the firmware one by one
0294  *
0295  * Called with lock held
0296  */
0297 static int sst_find_and_send_pipe_algo(struct sst_data *drv,
0298                     const char *pipe, struct sst_ids *ids)
0299 {
0300     int ret = 0;
0301     struct sst_algo_control *bc;
0302     struct sst_module *algo;
0303 
0304     dev_dbg(&drv->pdev->dev, "Enter: widget=%s\n", pipe);
0305 
0306     list_for_each_entry(algo, &ids->algo_list, node) {
0307         bc = (void *)algo->kctl->private_value;
0308 
0309         dev_dbg(&drv->pdev->dev, "Found algo control name=%s pipe=%s\n",
0310                 algo->kctl->id.name, pipe);
0311         ret = sst_send_algo_cmd(drv, bc);
0312         if (ret)
0313             return ret;
0314     }
0315     return ret;
0316 }
0317 
0318 static int sst_algo_bytes_ctl_info(struct snd_kcontrol *kcontrol,
0319                 struct snd_ctl_elem_info *uinfo)
0320 {
0321     struct sst_algo_control *bc = (void *)kcontrol->private_value;
0322 
0323     uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
0324     uinfo->count = bc->max;
0325 
0326     return 0;
0327 }
0328 
0329 static int sst_algo_control_get(struct snd_kcontrol *kcontrol,
0330                 struct snd_ctl_elem_value *ucontrol)
0331 {
0332     struct sst_algo_control *bc = (void *)kcontrol->private_value;
0333     struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
0334 
0335     switch (bc->type) {
0336     case SST_ALGO_PARAMS:
0337         memcpy(ucontrol->value.bytes.data, bc->params, bc->max);
0338         break;
0339     default:
0340         dev_err(component->dev, "Invalid Input- algo type:%d\n",
0341                 bc->type);
0342         return -EINVAL;
0343 
0344     }
0345     return 0;
0346 }
0347 
0348 static int sst_algo_control_set(struct snd_kcontrol *kcontrol,
0349                 struct snd_ctl_elem_value *ucontrol)
0350 {
0351     int ret = 0;
0352     struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
0353     struct sst_data *drv = snd_soc_component_get_drvdata(cmpnt);
0354     struct sst_algo_control *bc = (void *)kcontrol->private_value;
0355 
0356     dev_dbg(cmpnt->dev, "control_name=%s\n", kcontrol->id.name);
0357     mutex_lock(&drv->lock);
0358     switch (bc->type) {
0359     case SST_ALGO_PARAMS:
0360         memcpy(bc->params, ucontrol->value.bytes.data, bc->max);
0361         break;
0362     default:
0363         mutex_unlock(&drv->lock);
0364         dev_err(cmpnt->dev, "Invalid Input- algo type:%d\n",
0365                 bc->type);
0366         return -EINVAL;
0367     }
0368     /*if pipe is enabled, need to send the algo params from here*/
0369     if (bc->w && bc->w->power)
0370         ret = sst_send_algo_cmd(drv, bc);
0371     mutex_unlock(&drv->lock);
0372 
0373     return ret;
0374 }
0375 
0376 static int sst_gain_ctl_info(struct snd_kcontrol *kcontrol,
0377     struct snd_ctl_elem_info *uinfo)
0378 {
0379     struct sst_gain_mixer_control *mc = (void *)kcontrol->private_value;
0380 
0381     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0382     uinfo->count = mc->stereo ? 2 : 1;
0383     uinfo->value.integer.min = mc->min;
0384     uinfo->value.integer.max = mc->max;
0385 
0386     return 0;
0387 }
0388 
0389 /**
0390  * sst_send_gain_cmd - send the gain algorithm IPC to the FW
0391  * @drv: sst_data
0392  * @gv:the stored value of gain (also contains rampduration)
0393  * @task_id: task index
0394  * @loc_id: location/position index
0395  * @module_id: module index
0396  * @mute: flag that indicates whether this was called from the
0397  *  digital_mute callback or directly. If called from the
0398  *  digital_mute callback, module will be muted/unmuted based on this
0399  *  flag. The flag is always 0 if called directly.
0400  *
0401  * Called with sst_data.lock held
0402  *
0403  * The user-set gain value is sent only if the user-controllable 'mute' control
0404  * is OFF (indicated by gv->mute). Otherwise, the mute value (MIN value) is
0405  * sent.
0406  */
0407 static int sst_send_gain_cmd(struct sst_data *drv, struct sst_gain_value *gv,
0408                   u16 task_id, u16 loc_id, u16 module_id, int mute)
0409 {
0410     struct sst_cmd_set_gain_dual cmd;
0411 
0412     dev_dbg(&drv->pdev->dev, "Enter\n");
0413 
0414     cmd.header.command_id = MMX_SET_GAIN;
0415     SST_FILL_DEFAULT_DESTINATION(cmd.header.dst);
0416     cmd.gain_cell_num = 1;
0417 
0418     if (mute || gv->mute) {
0419         cmd.cell_gains[0].cell_gain_left = SST_GAIN_MIN_VALUE;
0420         cmd.cell_gains[0].cell_gain_right = SST_GAIN_MIN_VALUE;
0421     } else {
0422         cmd.cell_gains[0].cell_gain_left = gv->l_gain;
0423         cmd.cell_gains[0].cell_gain_right = gv->r_gain;
0424     }
0425 
0426     SST_FILL_DESTINATION(2, cmd.cell_gains[0].dest,
0427                  loc_id, module_id);
0428     cmd.cell_gains[0].gain_time_constant = gv->ramp_duration;
0429 
0430     cmd.header.length = sizeof(struct sst_cmd_set_gain_dual)
0431                 - sizeof(struct sst_dsp_header);
0432 
0433     /* we are with lock held, so call the unlocked api  to send */
0434     return sst_fill_and_send_cmd_unlocked(drv, SST_IPC_IA_SET_PARAMS,
0435                 SST_FLAG_BLOCKED, task_id, 0, &cmd,
0436                   sizeof(cmd.header) + cmd.header.length);
0437 }
0438 
0439 static int sst_gain_get(struct snd_kcontrol *kcontrol,
0440             struct snd_ctl_elem_value *ucontrol)
0441 {
0442     struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
0443     struct sst_gain_mixer_control *mc = (void *)kcontrol->private_value;
0444     struct sst_gain_value *gv = mc->gain_val;
0445 
0446     switch (mc->type) {
0447     case SST_GAIN_TLV:
0448         ucontrol->value.integer.value[0] = gv->l_gain;
0449         ucontrol->value.integer.value[1] = gv->r_gain;
0450         break;
0451 
0452     case SST_GAIN_MUTE:
0453         ucontrol->value.integer.value[0] = gv->mute ? 0 : 1;
0454         break;
0455 
0456     case SST_GAIN_RAMP_DURATION:
0457         ucontrol->value.integer.value[0] = gv->ramp_duration;
0458         break;
0459 
0460     default:
0461         dev_err(component->dev, "Invalid Input- gain type:%d\n",
0462                 mc->type);
0463         return -EINVAL;
0464     }
0465 
0466     return 0;
0467 }
0468 
0469 static int sst_gain_put(struct snd_kcontrol *kcontrol,
0470             struct snd_ctl_elem_value *ucontrol)
0471 {
0472     int ret = 0;
0473     struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
0474     struct sst_data *drv = snd_soc_component_get_drvdata(cmpnt);
0475     struct sst_gain_mixer_control *mc = (void *)kcontrol->private_value;
0476     struct sst_gain_value *gv = mc->gain_val;
0477 
0478     mutex_lock(&drv->lock);
0479 
0480     switch (mc->type) {
0481     case SST_GAIN_TLV:
0482         gv->l_gain = ucontrol->value.integer.value[0];
0483         gv->r_gain = ucontrol->value.integer.value[1];
0484         dev_dbg(cmpnt->dev, "%s: Volume %d, %d\n",
0485                 mc->pname, gv->l_gain, gv->r_gain);
0486         break;
0487 
0488     case SST_GAIN_MUTE:
0489         gv->mute = !ucontrol->value.integer.value[0];
0490         dev_dbg(cmpnt->dev, "%s: Mute %d\n", mc->pname, gv->mute);
0491         break;
0492 
0493     case SST_GAIN_RAMP_DURATION:
0494         gv->ramp_duration = ucontrol->value.integer.value[0];
0495         dev_dbg(cmpnt->dev, "%s: Ramp Delay%d\n",
0496                     mc->pname, gv->ramp_duration);
0497         break;
0498 
0499     default:
0500         mutex_unlock(&drv->lock);
0501         dev_err(cmpnt->dev, "Invalid Input- gain type:%d\n",
0502                 mc->type);
0503         return -EINVAL;
0504     }
0505 
0506     if (mc->w && mc->w->power)
0507         ret = sst_send_gain_cmd(drv, gv, mc->task_id,
0508             mc->pipe_id | mc->instance_id, mc->module_id, 0);
0509     mutex_unlock(&drv->lock);
0510 
0511     return ret;
0512 }
0513 
0514 static int sst_set_pipe_gain(struct sst_ids *ids,
0515                 struct sst_data *drv, int mute);
0516 
0517 static int sst_send_pipe_module_params(struct snd_soc_dapm_widget *w,
0518         struct snd_kcontrol *kcontrol)
0519 {
0520     struct snd_soc_component *c = snd_soc_dapm_to_component(w->dapm);
0521     struct sst_data *drv = snd_soc_component_get_drvdata(c);
0522     struct sst_ids *ids = w->priv;
0523 
0524     mutex_lock(&drv->lock);
0525     sst_find_and_send_pipe_algo(drv, w->name, ids);
0526     sst_set_pipe_gain(ids, drv, 0);
0527     mutex_unlock(&drv->lock);
0528 
0529     return 0;
0530 }
0531 
0532 static int sst_generic_modules_event(struct snd_soc_dapm_widget *w,
0533                      struct snd_kcontrol *k, int event)
0534 {
0535     if (SND_SOC_DAPM_EVENT_ON(event))
0536         return sst_send_pipe_module_params(w, k);
0537     return 0;
0538 }
0539 
0540 static const DECLARE_TLV_DB_SCALE(sst_gain_tlv_common, SST_GAIN_MIN_VALUE * 10, 10, 0);
0541 
0542 /* Look up table to convert MIXER SW bit regs to SWM inputs */
0543 static const uint swm_mixer_input_ids[SST_SWM_INPUT_COUNT] = {
0544     [SST_IP_MODEM]      = SST_SWM_IN_MODEM,
0545     [SST_IP_CODEC0]     = SST_SWM_IN_CODEC0,
0546     [SST_IP_CODEC1]     = SST_SWM_IN_CODEC1,
0547     [SST_IP_LOOP0]      = SST_SWM_IN_SPROT_LOOP,
0548     [SST_IP_LOOP1]      = SST_SWM_IN_MEDIA_LOOP1,
0549     [SST_IP_LOOP2]      = SST_SWM_IN_MEDIA_LOOP2,
0550     [SST_IP_PCM0]       = SST_SWM_IN_PCM0,
0551     [SST_IP_PCM1]       = SST_SWM_IN_PCM1,
0552     [SST_IP_MEDIA0]     = SST_SWM_IN_MEDIA0,
0553     [SST_IP_MEDIA1]     = SST_SWM_IN_MEDIA1,
0554     [SST_IP_MEDIA2]     = SST_SWM_IN_MEDIA2,
0555     [SST_IP_MEDIA3]     = SST_SWM_IN_MEDIA3,
0556 };
0557 
0558 /**
0559  * fill_swm_input - fill in the SWM input ids given the register
0560  * @cmpnt: ASoC component
0561  * @swm_input: array of swm_input_ids
0562  * @reg: the register value is a bit-field inicated which mixer inputs are ON.
0563  *
0564  * Use the lookup table to get the input-id and fill it in the
0565  * structure.
0566  */
0567 static int fill_swm_input(struct snd_soc_component *cmpnt,
0568         struct swm_input_ids *swm_input, unsigned int reg)
0569 {
0570     uint i, is_set, nb_inputs = 0;
0571     u16 input_loc_id;
0572 
0573     dev_dbg(cmpnt->dev, "reg: %#x\n", reg);
0574     for (i = 0; i < SST_SWM_INPUT_COUNT; i++) {
0575         is_set = reg & BIT(i);
0576         if (!is_set)
0577             continue;
0578 
0579         input_loc_id = swm_mixer_input_ids[i];
0580         SST_FILL_DESTINATION(2, swm_input->input_id,
0581                      input_loc_id, SST_DEFAULT_MODULE_ID);
0582         nb_inputs++;
0583         swm_input++;
0584         dev_dbg(cmpnt->dev, "input id: %#x, nb_inputs: %d\n",
0585                 input_loc_id, nb_inputs);
0586 
0587         if (nb_inputs == SST_CMD_SWM_MAX_INPUTS) {
0588             dev_warn(cmpnt->dev, "SET_SWM cmd max inputs reached");
0589             break;
0590         }
0591     }
0592     return nb_inputs;
0593 }
0594 
0595 
0596 /*
0597  * called with lock held
0598  */
0599 static int sst_set_pipe_gain(struct sst_ids *ids,
0600             struct sst_data *drv, int mute)
0601 {
0602     int ret = 0;
0603     struct sst_gain_mixer_control *mc;
0604     struct sst_gain_value *gv;
0605     struct sst_module *gain;
0606 
0607     list_for_each_entry(gain, &ids->gain_list, node) {
0608         struct snd_kcontrol *kctl = gain->kctl;
0609 
0610         dev_dbg(&drv->pdev->dev, "control name=%s\n", kctl->id.name);
0611         mc = (void *)kctl->private_value;
0612         gv = mc->gain_val;
0613 
0614         ret = sst_send_gain_cmd(drv, gv, mc->task_id,
0615             mc->pipe_id | mc->instance_id, mc->module_id, mute);
0616         if (ret)
0617             return ret;
0618     }
0619     return ret;
0620 }
0621 
0622 static int sst_swm_mixer_event(struct snd_soc_dapm_widget *w,
0623             struct snd_kcontrol *k, int event)
0624 {
0625     struct sst_cmd_set_swm cmd;
0626     struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm);
0627     struct sst_data *drv = snd_soc_component_get_drvdata(cmpnt);
0628     struct sst_ids *ids = w->priv;
0629     bool set_mixer = false;
0630     struct soc_mixer_control *mc;
0631     int val = 0;
0632     int i = 0;
0633 
0634     dev_dbg(cmpnt->dev, "widget = %s\n", w->name);
0635     /*
0636      * Identify which mixer input is on and send the bitmap of the
0637      * inputs as an IPC to the DSP.
0638      */
0639     for (i = 0; i < w->num_kcontrols; i++) {
0640         if (dapm_kcontrol_get_value(w->kcontrols[i])) {
0641             mc = (struct soc_mixer_control *)(w->kcontrols[i])->private_value;
0642             val |= 1 << mc->shift;
0643         }
0644     }
0645     dev_dbg(cmpnt->dev, "val = %#x\n", val);
0646 
0647     switch (event) {
0648     case SND_SOC_DAPM_PRE_PMU:
0649     case SND_SOC_DAPM_POST_PMD:
0650         set_mixer = true;
0651         break;
0652     case SND_SOC_DAPM_POST_REG:
0653         if (w->power)
0654             set_mixer = true;
0655         break;
0656     default:
0657         set_mixer = false;
0658     }
0659 
0660     if (!set_mixer)
0661         return 0;
0662 
0663     if (SND_SOC_DAPM_EVENT_ON(event) ||
0664         event == SND_SOC_DAPM_POST_REG)
0665         cmd.switch_state = SST_SWM_ON;
0666     else
0667         cmd.switch_state = SST_SWM_OFF;
0668 
0669     SST_FILL_DEFAULT_DESTINATION(cmd.header.dst);
0670     /* MMX_SET_SWM == SBA_SET_SWM */
0671     cmd.header.command_id = SBA_SET_SWM;
0672 
0673     SST_FILL_DESTINATION(2, cmd.output_id,
0674                  ids->location_id, SST_DEFAULT_MODULE_ID);
0675     cmd.nb_inputs = fill_swm_input(cmpnt, &cmd.input[0], val);
0676     cmd.header.length = offsetof(struct sst_cmd_set_swm, input)
0677                 - sizeof(struct sst_dsp_header)
0678                 + (cmd.nb_inputs * sizeof(cmd.input[0]));
0679 
0680     return sst_fill_and_send_cmd(drv, SST_IPC_IA_CMD, SST_FLAG_BLOCKED,
0681                   ids->task_id, 0, &cmd,
0682                   sizeof(cmd.header) + cmd.header.length);
0683 }
0684 
0685 /* SBA mixers - 16 inputs */
0686 #define SST_SBA_DECLARE_MIX_CONTROLS(kctl_name)                         \
0687     static const struct snd_kcontrol_new kctl_name[] = {                    \
0688         SOC_DAPM_SINGLE("modem_in Switch", SND_SOC_NOPM, SST_IP_MODEM, 1, 0),       \
0689         SOC_DAPM_SINGLE("codec_in0 Switch", SND_SOC_NOPM, SST_IP_CODEC0, 1, 0),     \
0690         SOC_DAPM_SINGLE("codec_in1 Switch", SND_SOC_NOPM, SST_IP_CODEC1, 1, 0),     \
0691         SOC_DAPM_SINGLE("sprot_loop_in Switch", SND_SOC_NOPM, SST_IP_LOOP0, 1, 0),  \
0692         SOC_DAPM_SINGLE("media_loop1_in Switch", SND_SOC_NOPM, SST_IP_LOOP1, 1, 0), \
0693         SOC_DAPM_SINGLE("media_loop2_in Switch", SND_SOC_NOPM, SST_IP_LOOP2, 1, 0), \
0694         SOC_DAPM_SINGLE("pcm0_in Switch", SND_SOC_NOPM, SST_IP_PCM0, 1, 0),     \
0695         SOC_DAPM_SINGLE("pcm1_in Switch", SND_SOC_NOPM, SST_IP_PCM1, 1, 0),     \
0696     }
0697 
0698 #define SST_SBA_MIXER_GRAPH_MAP(mix_name)           \
0699     { mix_name, "modem_in Switch",  "modem_in" },       \
0700     { mix_name, "codec_in0 Switch", "codec_in0" },      \
0701     { mix_name, "codec_in1 Switch", "codec_in1" },      \
0702     { mix_name, "sprot_loop_in Switch", "sprot_loop_in" },  \
0703     { mix_name, "media_loop1_in Switch",    "media_loop1_in" }, \
0704     { mix_name, "media_loop2_in Switch",    "media_loop2_in" }, \
0705     { mix_name, "pcm0_in Switch",       "pcm0_in" },        \
0706     { mix_name, "pcm1_in Switch",       "pcm1_in" }
0707 
0708 #define SST_MMX_DECLARE_MIX_CONTROLS(kctl_name)                     \
0709     static const struct snd_kcontrol_new kctl_name[] = {                \
0710         SOC_DAPM_SINGLE("media0_in Switch", SND_SOC_NOPM, SST_IP_MEDIA0, 1, 0), \
0711         SOC_DAPM_SINGLE("media1_in Switch", SND_SOC_NOPM, SST_IP_MEDIA1, 1, 0), \
0712         SOC_DAPM_SINGLE("media2_in Switch", SND_SOC_NOPM, SST_IP_MEDIA2, 1, 0), \
0713         SOC_DAPM_SINGLE("media3_in Switch", SND_SOC_NOPM, SST_IP_MEDIA3, 1, 0), \
0714     }
0715 
0716 SST_MMX_DECLARE_MIX_CONTROLS(sst_mix_media0_controls);
0717 SST_MMX_DECLARE_MIX_CONTROLS(sst_mix_media1_controls);
0718 
0719 /* 18 SBA mixers */
0720 SST_SBA_DECLARE_MIX_CONTROLS(sst_mix_pcm0_controls);
0721 SST_SBA_DECLARE_MIX_CONTROLS(sst_mix_pcm1_controls);
0722 SST_SBA_DECLARE_MIX_CONTROLS(sst_mix_pcm2_controls);
0723 SST_SBA_DECLARE_MIX_CONTROLS(sst_mix_sprot_l0_controls);
0724 SST_SBA_DECLARE_MIX_CONTROLS(sst_mix_media_l1_controls);
0725 SST_SBA_DECLARE_MIX_CONTROLS(sst_mix_media_l2_controls);
0726 SST_SBA_DECLARE_MIX_CONTROLS(__maybe_unused sst_mix_voip_controls);
0727 SST_SBA_DECLARE_MIX_CONTROLS(sst_mix_codec0_controls);
0728 SST_SBA_DECLARE_MIX_CONTROLS(sst_mix_codec1_controls);
0729 SST_SBA_DECLARE_MIX_CONTROLS(sst_mix_modem_controls);
0730 
0731 /*
0732  * sst_handle_vb_timer - Start/Stop the DSP scheduler
0733  *
0734  * The DSP expects first cmd to be SBA_VB_START, so at first startup send
0735  * that.
0736  * DSP expects last cmd to be SBA_VB_IDLE, so at last shutdown send that.
0737  *
0738  * Do refcount internally so that we send command only at first start
0739  * and last end. Since SST driver does its own ref count, invoke sst's
0740  * power ops always!
0741  */
0742 int sst_handle_vb_timer(struct snd_soc_dai *dai, bool enable)
0743 {
0744     int ret = 0;
0745     struct sst_cmd_generic cmd;
0746     struct sst_data *drv = snd_soc_dai_get_drvdata(dai);
0747     static int timer_usage;
0748 
0749     if (enable)
0750         cmd.header.command_id = SBA_VB_START;
0751     else
0752         cmd.header.command_id = SBA_IDLE;
0753     dev_dbg(dai->dev, "enable=%u, usage=%d\n", enable, timer_usage);
0754 
0755     SST_FILL_DEFAULT_DESTINATION(cmd.header.dst);
0756     cmd.header.length = 0;
0757 
0758     if (enable) {
0759         ret = sst->ops->power(sst->dev, true);
0760         if (ret < 0)
0761             return ret;
0762     }
0763 
0764     mutex_lock(&drv->lock);
0765     if (enable)
0766         timer_usage++;
0767     else
0768         timer_usage--;
0769 
0770     /*
0771      * Send the command only if this call is the first enable or last
0772      * disable
0773      */
0774     if ((enable && (timer_usage == 1)) ||
0775         (!enable && (timer_usage == 0))) {
0776         ret = sst_fill_and_send_cmd_unlocked(drv, SST_IPC_IA_CMD,
0777                 SST_FLAG_BLOCKED, SST_TASK_SBA, 0, &cmd,
0778                 sizeof(cmd.header) + cmd.header.length);
0779         if (ret && enable) {
0780             timer_usage--;
0781             enable  = false;
0782         }
0783     }
0784     mutex_unlock(&drv->lock);
0785 
0786     if (!enable)
0787         sst->ops->power(sst->dev, false);
0788     return ret;
0789 }
0790 
0791 int sst_fill_ssp_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
0792         unsigned int rx_mask, int slots, int slot_width)
0793 {
0794     struct sst_data *ctx = snd_soc_dai_get_drvdata(dai);
0795 
0796     ctx->ssp_cmd.nb_slots = slots;
0797     ctx->ssp_cmd.active_tx_slot_map = tx_mask;
0798     ctx->ssp_cmd.active_rx_slot_map = rx_mask;
0799     ctx->ssp_cmd.nb_bits_per_slots = slot_width;
0800 
0801     return 0;
0802 }
0803 
0804 static int sst_get_frame_sync_polarity(struct snd_soc_dai *dai,
0805         unsigned int fmt)
0806 {
0807     int format;
0808 
0809     format = fmt & SND_SOC_DAIFMT_INV_MASK;
0810     dev_dbg(dai->dev, "Enter:%s, format=%x\n", __func__, format);
0811 
0812     switch (format) {
0813     case SND_SOC_DAIFMT_NB_NF:
0814     case SND_SOC_DAIFMT_IB_NF:
0815         return SSP_FS_ACTIVE_HIGH;
0816     case SND_SOC_DAIFMT_NB_IF:
0817     case SND_SOC_DAIFMT_IB_IF:
0818         return SSP_FS_ACTIVE_LOW;
0819     default:
0820         dev_err(dai->dev, "Invalid frame sync polarity %d\n", format);
0821     }
0822 
0823     return -EINVAL;
0824 }
0825 
0826 static int sst_get_ssp_mode(struct snd_soc_dai *dai, unsigned int fmt)
0827 {
0828     int format;
0829 
0830     format = (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK);
0831     dev_dbg(dai->dev, "Enter:%s, format=%x\n", __func__, format);
0832 
0833     switch (format) {
0834     case SND_SOC_DAIFMT_BP_FP:
0835         return SSP_MODE_PROVIDER;
0836     case SND_SOC_DAIFMT_BC_FC:
0837         return SSP_MODE_CONSUMER;
0838     default:
0839         dev_err(dai->dev, "Invalid ssp protocol: %d\n", format);
0840     }
0841 
0842     return -EINVAL;
0843 }
0844 
0845 
0846 int sst_fill_ssp_config(struct snd_soc_dai *dai, unsigned int fmt)
0847 {
0848     unsigned int mode;
0849     int fs_polarity;
0850     struct sst_data *ctx = snd_soc_dai_get_drvdata(dai);
0851 
0852     mode = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
0853 
0854     switch (mode) {
0855     case SND_SOC_DAIFMT_DSP_B:
0856         ctx->ssp_cmd.ssp_protocol = SSP_MODE_PCM;
0857         ctx->ssp_cmd.mode = sst_get_ssp_mode(dai, fmt) | (SSP_PCM_MODE_NETWORK << 1);
0858         ctx->ssp_cmd.start_delay = 0;
0859         ctx->ssp_cmd.data_polarity = 1;
0860         ctx->ssp_cmd.frame_sync_width = 1;
0861         break;
0862 
0863     case SND_SOC_DAIFMT_DSP_A:
0864         ctx->ssp_cmd.ssp_protocol = SSP_MODE_PCM;
0865         ctx->ssp_cmd.mode = sst_get_ssp_mode(dai, fmt) | (SSP_PCM_MODE_NETWORK << 1);
0866         ctx->ssp_cmd.start_delay = 1;
0867         ctx->ssp_cmd.data_polarity = 1;
0868         ctx->ssp_cmd.frame_sync_width = 1;
0869         break;
0870 
0871     case SND_SOC_DAIFMT_I2S:
0872         ctx->ssp_cmd.ssp_protocol = SSP_MODE_I2S;
0873         ctx->ssp_cmd.mode = sst_get_ssp_mode(dai, fmt) | (SSP_PCM_MODE_NORMAL << 1);
0874         ctx->ssp_cmd.start_delay = 1;
0875         ctx->ssp_cmd.data_polarity = 0;
0876         ctx->ssp_cmd.frame_sync_width = ctx->ssp_cmd.nb_bits_per_slots;
0877         break;
0878 
0879     case SND_SOC_DAIFMT_LEFT_J:
0880         ctx->ssp_cmd.ssp_protocol = SSP_MODE_I2S;
0881         ctx->ssp_cmd.mode = sst_get_ssp_mode(dai, fmt) | (SSP_PCM_MODE_NORMAL << 1);
0882         ctx->ssp_cmd.start_delay = 0;
0883         ctx->ssp_cmd.data_polarity = 0;
0884         ctx->ssp_cmd.frame_sync_width = ctx->ssp_cmd.nb_bits_per_slots;
0885         break;
0886 
0887     default:
0888         dev_dbg(dai->dev, "using default ssp configs\n");
0889     }
0890 
0891     fs_polarity = sst_get_frame_sync_polarity(dai, fmt);
0892     if (fs_polarity < 0)
0893         return fs_polarity;
0894 
0895     ctx->ssp_cmd.frame_sync_polarity = fs_polarity;
0896 
0897     return 0;
0898 }
0899 
0900 /*
0901  * sst_ssp_config - contains SSP configuration for media UC
0902  * this can be overwritten by set_dai_xxx APIs
0903  */
0904 static const struct sst_ssp_config sst_ssp_configs = {
0905     .ssp_id = SSP_CODEC,
0906     .bits_per_slot = 24,
0907     .slots = 4,
0908     .ssp_mode = SSP_MODE_PROVIDER,
0909     .pcm_mode = SSP_PCM_MODE_NETWORK,
0910     .duplex = SSP_DUPLEX,
0911     .ssp_protocol = SSP_MODE_PCM,
0912     .fs_width = 1,
0913     .fs_frequency = SSP_FS_48_KHZ,
0914     .active_slot_map = 0xF,
0915     .start_delay = 0,
0916     .frame_sync_polarity = SSP_FS_ACTIVE_HIGH,
0917     .data_polarity = 1,
0918 };
0919 
0920 void sst_fill_ssp_defaults(struct snd_soc_dai *dai)
0921 {
0922     const struct sst_ssp_config *config;
0923     struct sst_data *ctx = snd_soc_dai_get_drvdata(dai);
0924 
0925     config = &sst_ssp_configs;
0926 
0927     ctx->ssp_cmd.selection = config->ssp_id;
0928     ctx->ssp_cmd.nb_bits_per_slots = config->bits_per_slot;
0929     ctx->ssp_cmd.nb_slots = config->slots;
0930     ctx->ssp_cmd.mode = config->ssp_mode | (config->pcm_mode << 1);
0931     ctx->ssp_cmd.duplex = config->duplex;
0932     ctx->ssp_cmd.active_tx_slot_map = config->active_slot_map;
0933     ctx->ssp_cmd.active_rx_slot_map = config->active_slot_map;
0934     ctx->ssp_cmd.frame_sync_frequency = config->fs_frequency;
0935     ctx->ssp_cmd.frame_sync_polarity = config->frame_sync_polarity;
0936     ctx->ssp_cmd.data_polarity = config->data_polarity;
0937     ctx->ssp_cmd.frame_sync_width = config->fs_width;
0938     ctx->ssp_cmd.ssp_protocol = config->ssp_protocol;
0939     ctx->ssp_cmd.start_delay = config->start_delay;
0940     ctx->ssp_cmd.reserved1 = ctx->ssp_cmd.reserved2 = 0xFF;
0941 }
0942 
0943 int send_ssp_cmd(struct snd_soc_dai *dai, const char *id, bool enable)
0944 {
0945     struct sst_data *drv = snd_soc_dai_get_drvdata(dai);
0946     int ssp_id;
0947 
0948     dev_dbg(dai->dev, "Enter: enable=%d port_name=%s\n", enable, id);
0949 
0950     if (strcmp(id, "ssp0-port") == 0)
0951         ssp_id = SSP_MODEM;
0952     else if (strcmp(id, "ssp2-port") == 0)
0953         ssp_id = SSP_CODEC;
0954     else {
0955         dev_dbg(dai->dev, "port %s is not supported\n", id);
0956         return -1;
0957     }
0958 
0959     SST_FILL_DEFAULT_DESTINATION(drv->ssp_cmd.header.dst);
0960     drv->ssp_cmd.header.command_id = SBA_HW_SET_SSP;
0961     drv->ssp_cmd.header.length = sizeof(struct sst_cmd_sba_hw_set_ssp)
0962                 - sizeof(struct sst_dsp_header);
0963 
0964     drv->ssp_cmd.selection = ssp_id;
0965     dev_dbg(dai->dev, "ssp_id: %u\n", ssp_id);
0966 
0967     if (enable)
0968         drv->ssp_cmd.switch_state = SST_SWITCH_ON;
0969     else
0970         drv->ssp_cmd.switch_state = SST_SWITCH_OFF;
0971 
0972     return sst_fill_and_send_cmd(drv, SST_IPC_IA_CMD, SST_FLAG_BLOCKED,
0973                 SST_TASK_SBA, 0, &drv->ssp_cmd,
0974                 sizeof(drv->ssp_cmd.header) + drv->ssp_cmd.header.length);
0975 }
0976 
0977 static int sst_set_be_modules(struct snd_soc_dapm_widget *w,
0978              struct snd_kcontrol *k, int event)
0979 {
0980     int ret = 0;
0981     struct snd_soc_component *c = snd_soc_dapm_to_component(w->dapm);
0982     struct sst_data *drv = snd_soc_component_get_drvdata(c);
0983 
0984     dev_dbg(c->dev, "Enter: widget=%s\n", w->name);
0985 
0986     if (SND_SOC_DAPM_EVENT_ON(event)) {
0987         mutex_lock(&drv->lock);
0988         ret = sst_send_slot_map(drv);
0989         mutex_unlock(&drv->lock);
0990         if (ret)
0991             return ret;
0992         ret = sst_send_pipe_module_params(w, k);
0993     }
0994     return ret;
0995 }
0996 
0997 static int sst_set_media_path(struct snd_soc_dapm_widget *w,
0998                   struct snd_kcontrol *k, int event)
0999 {
1000     int ret = 0;
1001     struct sst_cmd_set_media_path cmd;
1002     struct snd_soc_component *c = snd_soc_dapm_to_component(w->dapm);
1003     struct sst_data *drv = snd_soc_component_get_drvdata(c);
1004     struct sst_ids *ids = w->priv;
1005 
1006     dev_dbg(c->dev, "widget=%s\n", w->name);
1007     dev_dbg(c->dev, "task=%u, location=%#x\n",
1008                 ids->task_id, ids->location_id);
1009 
1010     if (SND_SOC_DAPM_EVENT_ON(event))
1011         cmd.switch_state = SST_PATH_ON;
1012     else
1013         cmd.switch_state = SST_PATH_OFF;
1014 
1015     SST_FILL_DESTINATION(2, cmd.header.dst,
1016                  ids->location_id, SST_DEFAULT_MODULE_ID);
1017 
1018     /* MMX_SET_MEDIA_PATH == SBA_SET_MEDIA_PATH */
1019     cmd.header.command_id = MMX_SET_MEDIA_PATH;
1020     cmd.header.length = sizeof(struct sst_cmd_set_media_path)
1021                 - sizeof(struct sst_dsp_header);
1022 
1023     ret = sst_fill_and_send_cmd(drv, SST_IPC_IA_CMD, SST_FLAG_BLOCKED,
1024                   ids->task_id, 0, &cmd,
1025                   sizeof(cmd.header) + cmd.header.length);
1026     if (ret)
1027         return ret;
1028 
1029     if (SND_SOC_DAPM_EVENT_ON(event))
1030         ret = sst_send_pipe_module_params(w, k);
1031     return ret;
1032 }
1033 
1034 static int sst_set_media_loop(struct snd_soc_dapm_widget *w,
1035             struct snd_kcontrol *k, int event)
1036 {
1037     int ret = 0;
1038     struct sst_cmd_sba_set_media_loop_map cmd;
1039     struct snd_soc_component *c = snd_soc_dapm_to_component(w->dapm);
1040     struct sst_data *drv = snd_soc_component_get_drvdata(c);
1041     struct sst_ids *ids = w->priv;
1042 
1043     dev_dbg(c->dev, "Enter:widget=%s\n", w->name);
1044     if (SND_SOC_DAPM_EVENT_ON(event))
1045         cmd.switch_state = SST_SWITCH_ON;
1046     else
1047         cmd.switch_state = SST_SWITCH_OFF;
1048 
1049     SST_FILL_DESTINATION(2, cmd.header.dst,
1050                  ids->location_id, SST_DEFAULT_MODULE_ID);
1051 
1052     cmd.header.command_id = SBA_SET_MEDIA_LOOP_MAP;
1053     cmd.header.length = sizeof(struct sst_cmd_sba_set_media_loop_map)
1054                  - sizeof(struct sst_dsp_header);
1055     cmd.param.part.cfg.rate = 2; /* 48khz */
1056 
1057     cmd.param.part.cfg.format = ids->format; /* stereo/Mono */
1058     cmd.param.part.cfg.s_length = 1; /* 24bit left justified */
1059     cmd.map = 0; /* Algo sequence: Gain - DRP - FIR - IIR */
1060 
1061     ret = sst_fill_and_send_cmd(drv, SST_IPC_IA_CMD, SST_FLAG_BLOCKED,
1062                   SST_TASK_SBA, 0, &cmd,
1063                   sizeof(cmd.header) + cmd.header.length);
1064     if (ret)
1065         return ret;
1066 
1067     if (SND_SOC_DAPM_EVENT_ON(event))
1068         ret = sst_send_pipe_module_params(w, k);
1069     return ret;
1070 }
1071 
1072 static const struct snd_soc_dapm_widget sst_dapm_widgets[] = {
1073     SST_AIF_IN("modem_in", sst_set_be_modules),
1074     SST_AIF_IN("codec_in0", sst_set_be_modules),
1075     SST_AIF_IN("codec_in1", sst_set_be_modules),
1076     SST_AIF_OUT("modem_out", sst_set_be_modules),
1077     SST_AIF_OUT("codec_out0", sst_set_be_modules),
1078     SST_AIF_OUT("codec_out1", sst_set_be_modules),
1079 
1080     /* Media Paths */
1081     /* MediaX IN paths are set via ALLOC, so no SET_MEDIA_PATH command */
1082     SST_PATH_INPUT("media0_in", SST_TASK_MMX, SST_SWM_IN_MEDIA0, sst_generic_modules_event),
1083     SST_PATH_INPUT("media1_in", SST_TASK_MMX, SST_SWM_IN_MEDIA1, NULL),
1084     SST_PATH_INPUT("media2_in", SST_TASK_MMX, SST_SWM_IN_MEDIA2, sst_set_media_path),
1085     SST_PATH_INPUT("media3_in", SST_TASK_MMX, SST_SWM_IN_MEDIA3, NULL),
1086     SST_PATH_OUTPUT("media0_out", SST_TASK_MMX, SST_SWM_OUT_MEDIA0, sst_set_media_path),
1087     SST_PATH_OUTPUT("media1_out", SST_TASK_MMX, SST_SWM_OUT_MEDIA1, sst_set_media_path),
1088 
1089     /* SBA PCM Paths */
1090     SST_PATH_INPUT("pcm0_in", SST_TASK_SBA, SST_SWM_IN_PCM0, sst_set_media_path),
1091     SST_PATH_INPUT("pcm1_in", SST_TASK_SBA, SST_SWM_IN_PCM1, sst_set_media_path),
1092     SST_PATH_OUTPUT("pcm0_out", SST_TASK_SBA, SST_SWM_OUT_PCM0, sst_set_media_path),
1093     SST_PATH_OUTPUT("pcm1_out", SST_TASK_SBA, SST_SWM_OUT_PCM1, sst_set_media_path),
1094     SST_PATH_OUTPUT("pcm2_out", SST_TASK_SBA, SST_SWM_OUT_PCM2, sst_set_media_path),
1095 
1096     /* SBA Loops */
1097     SST_PATH_INPUT("sprot_loop_in", SST_TASK_SBA, SST_SWM_IN_SPROT_LOOP, NULL),
1098     SST_PATH_INPUT("media_loop1_in", SST_TASK_SBA, SST_SWM_IN_MEDIA_LOOP1, NULL),
1099     SST_PATH_INPUT("media_loop2_in", SST_TASK_SBA, SST_SWM_IN_MEDIA_LOOP2, NULL),
1100     SST_PATH_MEDIA_LOOP_OUTPUT("sprot_loop_out", SST_TASK_SBA, SST_SWM_OUT_SPROT_LOOP, SST_FMT_STEREO, sst_set_media_loop),
1101     SST_PATH_MEDIA_LOOP_OUTPUT("media_loop1_out", SST_TASK_SBA, SST_SWM_OUT_MEDIA_LOOP1, SST_FMT_STEREO, sst_set_media_loop),
1102     SST_PATH_MEDIA_LOOP_OUTPUT("media_loop2_out", SST_TASK_SBA, SST_SWM_OUT_MEDIA_LOOP2, SST_FMT_STEREO, sst_set_media_loop),
1103 
1104     /* Media Mixers */
1105     SST_SWM_MIXER("media0_out mix 0", SND_SOC_NOPM, SST_TASK_MMX, SST_SWM_OUT_MEDIA0,
1106               sst_mix_media0_controls, sst_swm_mixer_event),
1107     SST_SWM_MIXER("media1_out mix 0", SND_SOC_NOPM, SST_TASK_MMX, SST_SWM_OUT_MEDIA1,
1108               sst_mix_media1_controls, sst_swm_mixer_event),
1109 
1110     /* SBA PCM mixers */
1111     SST_SWM_MIXER("pcm0_out mix 0", SND_SOC_NOPM, SST_TASK_SBA, SST_SWM_OUT_PCM0,
1112               sst_mix_pcm0_controls, sst_swm_mixer_event),
1113     SST_SWM_MIXER("pcm1_out mix 0", SND_SOC_NOPM, SST_TASK_SBA, SST_SWM_OUT_PCM1,
1114               sst_mix_pcm1_controls, sst_swm_mixer_event),
1115     SST_SWM_MIXER("pcm2_out mix 0", SND_SOC_NOPM, SST_TASK_SBA, SST_SWM_OUT_PCM2,
1116               sst_mix_pcm2_controls, sst_swm_mixer_event),
1117 
1118     /* SBA Loop mixers */
1119     SST_SWM_MIXER("sprot_loop_out mix 0", SND_SOC_NOPM, SST_TASK_SBA, SST_SWM_OUT_SPROT_LOOP,
1120               sst_mix_sprot_l0_controls, sst_swm_mixer_event),
1121     SST_SWM_MIXER("media_loop1_out mix 0", SND_SOC_NOPM, SST_TASK_SBA, SST_SWM_OUT_MEDIA_LOOP1,
1122               sst_mix_media_l1_controls, sst_swm_mixer_event),
1123     SST_SWM_MIXER("media_loop2_out mix 0", SND_SOC_NOPM, SST_TASK_SBA, SST_SWM_OUT_MEDIA_LOOP2,
1124               sst_mix_media_l2_controls, sst_swm_mixer_event),
1125 
1126     /* SBA Backend mixers */
1127     SST_SWM_MIXER("codec_out0 mix 0", SND_SOC_NOPM, SST_TASK_SBA, SST_SWM_OUT_CODEC0,
1128               sst_mix_codec0_controls, sst_swm_mixer_event),
1129     SST_SWM_MIXER("codec_out1 mix 0", SND_SOC_NOPM, SST_TASK_SBA, SST_SWM_OUT_CODEC1,
1130               sst_mix_codec1_controls, sst_swm_mixer_event),
1131     SST_SWM_MIXER("modem_out mix 0", SND_SOC_NOPM, SST_TASK_SBA, SST_SWM_OUT_MODEM,
1132               sst_mix_modem_controls, sst_swm_mixer_event),
1133 
1134 };
1135 
1136 static const struct snd_soc_dapm_route intercon[] = {
1137     {"media0_in", NULL, "Compress Playback"},
1138     {"media1_in", NULL, "Headset Playback"},
1139     {"media2_in", NULL, "pcm0_out"},
1140     {"media3_in", NULL, "Deepbuffer Playback"},
1141 
1142     {"media0_out mix 0", "media0_in Switch", "media0_in"},
1143     {"media0_out mix 0", "media1_in Switch", "media1_in"},
1144     {"media0_out mix 0", "media2_in Switch", "media2_in"},
1145     {"media0_out mix 0", "media3_in Switch", "media3_in"},
1146     {"media1_out mix 0", "media0_in Switch", "media0_in"},
1147     {"media1_out mix 0", "media1_in Switch", "media1_in"},
1148     {"media1_out mix 0", "media2_in Switch", "media2_in"},
1149     {"media1_out mix 0", "media3_in Switch", "media3_in"},
1150 
1151     {"media0_out", NULL, "media0_out mix 0"},
1152     {"media1_out", NULL, "media1_out mix 0"},
1153     {"pcm0_in", NULL, "media0_out"},
1154     {"pcm1_in", NULL, "media1_out"},
1155 
1156     {"Headset Capture", NULL, "pcm1_out"},
1157     {"Headset Capture", NULL, "pcm2_out"},
1158     {"pcm0_out", NULL, "pcm0_out mix 0"},
1159     SST_SBA_MIXER_GRAPH_MAP("pcm0_out mix 0"),
1160     {"pcm1_out", NULL, "pcm1_out mix 0"},
1161     SST_SBA_MIXER_GRAPH_MAP("pcm1_out mix 0"),
1162     {"pcm2_out", NULL, "pcm2_out mix 0"},
1163     SST_SBA_MIXER_GRAPH_MAP("pcm2_out mix 0"),
1164 
1165     {"media_loop1_in", NULL, "media_loop1_out"},
1166     {"media_loop1_out", NULL, "media_loop1_out mix 0"},
1167     SST_SBA_MIXER_GRAPH_MAP("media_loop1_out mix 0"),
1168     {"media_loop2_in", NULL, "media_loop2_out"},
1169     {"media_loop2_out", NULL, "media_loop2_out mix 0"},
1170     SST_SBA_MIXER_GRAPH_MAP("media_loop2_out mix 0"),
1171     {"sprot_loop_in", NULL, "sprot_loop_out"},
1172     {"sprot_loop_out", NULL, "sprot_loop_out mix 0"},
1173     SST_SBA_MIXER_GRAPH_MAP("sprot_loop_out mix 0"),
1174 
1175     {"codec_out0", NULL, "codec_out0 mix 0"},
1176     SST_SBA_MIXER_GRAPH_MAP("codec_out0 mix 0"),
1177     {"codec_out1", NULL, "codec_out1 mix 0"},
1178     SST_SBA_MIXER_GRAPH_MAP("codec_out1 mix 0"),
1179     {"modem_out", NULL, "modem_out mix 0"},
1180     SST_SBA_MIXER_GRAPH_MAP("modem_out mix 0"),
1181 
1182 
1183 };
1184 static const char * const slot_names[] = {
1185     "none",
1186     "slot 0", "slot 1", "slot 2", "slot 3",
1187     "slot 4", "slot 5", "slot 6", "slot 7", /* not supported by FW */
1188 };
1189 
1190 static const char * const channel_names[] = {
1191     "none",
1192     "codec_out0_0", "codec_out0_1", "codec_out1_0", "codec_out1_1",
1193     "codec_out2_0", "codec_out2_1", "codec_out3_0", "codec_out3_1", /* not supported by FW */
1194 };
1195 
1196 #define SST_INTERLEAVER(xpname, slot_name, slotno) \
1197     SST_SSP_SLOT_CTL(xpname, "tx interleaver", slot_name, slotno, true, \
1198              channel_names, sst_slot_get, sst_slot_put)
1199 
1200 #define SST_DEINTERLEAVER(xpname, channel_name, channel_no) \
1201     SST_SSP_SLOT_CTL(xpname, "rx deinterleaver", channel_name, channel_no, false, \
1202              slot_names, sst_slot_get, sst_slot_put)
1203 
1204 static const struct snd_kcontrol_new sst_slot_controls[] = {
1205     SST_INTERLEAVER("codec_out", "slot 0", 0),
1206     SST_INTERLEAVER("codec_out", "slot 1", 1),
1207     SST_INTERLEAVER("codec_out", "slot 2", 2),
1208     SST_INTERLEAVER("codec_out", "slot 3", 3),
1209     SST_DEINTERLEAVER("codec_in", "codec_in0_0", 0),
1210     SST_DEINTERLEAVER("codec_in", "codec_in0_1", 1),
1211     SST_DEINTERLEAVER("codec_in", "codec_in1_0", 2),
1212     SST_DEINTERLEAVER("codec_in", "codec_in1_1", 3),
1213 };
1214 
1215 /* Gain helper with min/max set */
1216 #define SST_GAIN(name, path_id, task_id, instance, gain_var)                \
1217     SST_GAIN_KCONTROLS(name, "Gain", SST_GAIN_MIN_VALUE, SST_GAIN_MAX_VALUE,    \
1218         SST_GAIN_TC_MIN, SST_GAIN_TC_MAX,                   \
1219         sst_gain_get, sst_gain_put,                     \
1220         SST_MODULE_ID_GAIN_CELL, path_id, instance, task_id,            \
1221         sst_gain_tlv_common, gain_var)
1222 
1223 #define SST_VOLUME(name, path_id, task_id, instance, gain_var)              \
1224     SST_GAIN_KCONTROLS(name, "Volume", SST_GAIN_MIN_VALUE, SST_GAIN_MAX_VALUE,  \
1225         SST_GAIN_TC_MIN, SST_GAIN_TC_MAX,                   \
1226         sst_gain_get, sst_gain_put,                     \
1227         SST_MODULE_ID_VOLUME, path_id, instance, task_id,           \
1228         sst_gain_tlv_common, gain_var)
1229 
1230 static struct sst_gain_value sst_gains[];
1231 
1232 static const struct snd_kcontrol_new sst_gain_controls[] = {
1233     SST_GAIN("media0_in", SST_PATH_INDEX_MEDIA0_IN, SST_TASK_MMX, 0, &sst_gains[0]),
1234     SST_GAIN("media1_in", SST_PATH_INDEX_MEDIA1_IN, SST_TASK_MMX, 0, &sst_gains[1]),
1235     SST_GAIN("media2_in", SST_PATH_INDEX_MEDIA2_IN, SST_TASK_MMX, 0, &sst_gains[2]),
1236     SST_GAIN("media3_in", SST_PATH_INDEX_MEDIA3_IN, SST_TASK_MMX, 0, &sst_gains[3]),
1237 
1238     SST_GAIN("pcm0_in", SST_PATH_INDEX_PCM0_IN, SST_TASK_SBA, 0, &sst_gains[4]),
1239     SST_GAIN("pcm1_in", SST_PATH_INDEX_PCM1_IN, SST_TASK_SBA, 0, &sst_gains[5]),
1240     SST_GAIN("pcm1_out", SST_PATH_INDEX_PCM1_OUT, SST_TASK_SBA, 0, &sst_gains[6]),
1241     SST_GAIN("pcm2_out", SST_PATH_INDEX_PCM2_OUT, SST_TASK_SBA, 0, &sst_gains[7]),
1242 
1243     SST_GAIN("codec_in0", SST_PATH_INDEX_CODEC_IN0, SST_TASK_SBA, 0, &sst_gains[8]),
1244     SST_GAIN("codec_in1", SST_PATH_INDEX_CODEC_IN1, SST_TASK_SBA, 0, &sst_gains[9]),
1245     SST_GAIN("codec_out0", SST_PATH_INDEX_CODEC_OUT0, SST_TASK_SBA, 0, &sst_gains[10]),
1246     SST_GAIN("codec_out1", SST_PATH_INDEX_CODEC_OUT1, SST_TASK_SBA, 0, &sst_gains[11]),
1247     SST_GAIN("media_loop1_out", SST_PATH_INDEX_MEDIA_LOOP1_OUT, SST_TASK_SBA, 0, &sst_gains[12]),
1248     SST_GAIN("media_loop2_out", SST_PATH_INDEX_MEDIA_LOOP2_OUT, SST_TASK_SBA, 0, &sst_gains[13]),
1249     SST_GAIN("sprot_loop_out", SST_PATH_INDEX_SPROT_LOOP_OUT, SST_TASK_SBA, 0, &sst_gains[14]),
1250     SST_VOLUME("media0_in", SST_PATH_INDEX_MEDIA0_IN, SST_TASK_MMX, 0, &sst_gains[15]),
1251     SST_GAIN("modem_in", SST_PATH_INDEX_MODEM_IN, SST_TASK_SBA, 0, &sst_gains[16]),
1252     SST_GAIN("modem_out", SST_PATH_INDEX_MODEM_OUT, SST_TASK_SBA, 0, &sst_gains[17]),
1253 
1254 };
1255 
1256 #define SST_GAIN_NUM_CONTROLS 3
1257 /* the SST_GAIN macro above will create three alsa controls for each
1258  * instance invoked, gain, mute and ramp duration, which use the same gain
1259  * cell sst_gain to keep track of data
1260  * To calculate number of gain cell instances we need to device by 3 in
1261  * below caulcation for gain cell memory.
1262  * This gets rid of static number and issues while adding new controls
1263  */
1264 static struct sst_gain_value sst_gains[ARRAY_SIZE(sst_gain_controls)/SST_GAIN_NUM_CONTROLS];
1265 
1266 static const struct snd_kcontrol_new sst_algo_controls[] = {
1267     SST_ALGO_KCONTROL_BYTES("media_loop1_out", "fir", 272, SST_MODULE_ID_FIR_24,
1268          SST_PATH_INDEX_MEDIA_LOOP1_OUT, 0, SST_TASK_SBA, SBA_VB_SET_FIR),
1269     SST_ALGO_KCONTROL_BYTES("media_loop1_out", "iir", 300, SST_MODULE_ID_IIR_24,
1270         SST_PATH_INDEX_MEDIA_LOOP1_OUT, 0, SST_TASK_SBA, SBA_VB_SET_IIR),
1271     SST_ALGO_KCONTROL_BYTES("media_loop1_out", "mdrp", 286, SST_MODULE_ID_MDRP,
1272         SST_PATH_INDEX_MEDIA_LOOP1_OUT, 0, SST_TASK_SBA, SBA_SET_MDRP),
1273     SST_ALGO_KCONTROL_BYTES("media_loop2_out", "fir", 272, SST_MODULE_ID_FIR_24,
1274         SST_PATH_INDEX_MEDIA_LOOP2_OUT, 0, SST_TASK_SBA, SBA_VB_SET_FIR),
1275     SST_ALGO_KCONTROL_BYTES("media_loop2_out", "iir", 300, SST_MODULE_ID_IIR_24,
1276         SST_PATH_INDEX_MEDIA_LOOP2_OUT, 0, SST_TASK_SBA, SBA_VB_SET_IIR),
1277     SST_ALGO_KCONTROL_BYTES("media_loop2_out", "mdrp", 286, SST_MODULE_ID_MDRP,
1278         SST_PATH_INDEX_MEDIA_LOOP2_OUT, 0, SST_TASK_SBA, SBA_SET_MDRP),
1279     SST_ALGO_KCONTROL_BYTES("sprot_loop_out", "lpro", 192, SST_MODULE_ID_SPROT,
1280         SST_PATH_INDEX_SPROT_LOOP_OUT, 0, SST_TASK_SBA, SBA_VB_LPRO),
1281     SST_ALGO_KCONTROL_BYTES("codec_in0", "dcr", 52, SST_MODULE_ID_FILT_DCR,
1282         SST_PATH_INDEX_CODEC_IN0, 0, SST_TASK_SBA, SBA_VB_SET_IIR),
1283     SST_ALGO_KCONTROL_BYTES("codec_in1", "dcr", 52, SST_MODULE_ID_FILT_DCR,
1284         SST_PATH_INDEX_CODEC_IN1, 0, SST_TASK_SBA, SBA_VB_SET_IIR),
1285 
1286 };
1287 
1288 static int sst_algo_control_init(struct device *dev)
1289 {
1290     int i = 0;
1291     struct sst_algo_control *bc;
1292     /*allocate space to cache the algo parameters in the driver*/
1293     for (i = 0; i < ARRAY_SIZE(sst_algo_controls); i++) {
1294         bc = (struct sst_algo_control *)sst_algo_controls[i].private_value;
1295         bc->params = devm_kzalloc(dev, bc->max, GFP_KERNEL);
1296         if (bc->params == NULL)
1297             return -ENOMEM;
1298     }
1299     return 0;
1300 }
1301 
1302 static bool is_sst_dapm_widget(struct snd_soc_dapm_widget *w)
1303 {
1304     switch (w->id) {
1305     case snd_soc_dapm_pga:
1306     case snd_soc_dapm_aif_in:
1307     case snd_soc_dapm_aif_out:
1308     case snd_soc_dapm_input:
1309     case snd_soc_dapm_output:
1310     case snd_soc_dapm_mixer:
1311         return true;
1312     default:
1313         return false;
1314     }
1315 }
1316 
1317 /**
1318  * sst_send_pipe_gains - send gains for the front-end DAIs
1319  * @dai: front-end dai
1320  * @stream: direction
1321  * @mute: boolean indicating mute status
1322  *
1323  * The gains in the pipes connected to the front-ends are muted/unmuted
1324  * automatically via the digital_mute() DAPM callback. This function sends the
1325  * gains for the front-end pipes.
1326  */
1327 int sst_send_pipe_gains(struct snd_soc_dai *dai, int stream, int mute)
1328 {
1329     struct sst_data *drv = snd_soc_dai_get_drvdata(dai);
1330     struct snd_soc_dapm_widget *w;
1331     struct snd_soc_dapm_path *p;
1332 
1333     dev_dbg(dai->dev, "enter, dai-name=%s dir=%d\n", dai->name, stream);
1334 
1335     if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1336         dev_dbg(dai->dev, "Stream name=%s\n",
1337                 dai->playback_widget->name);
1338         w = dai->playback_widget;
1339         snd_soc_dapm_widget_for_each_sink_path(w, p) {
1340             if (p->connected && !p->connected(w, p->sink))
1341                 continue;
1342 
1343             if (p->connect && p->sink->power &&
1344                     is_sst_dapm_widget(p->sink)) {
1345                 struct sst_ids *ids = p->sink->priv;
1346 
1347                 dev_dbg(dai->dev, "send gains for widget=%s\n",
1348                         p->sink->name);
1349                 mutex_lock(&drv->lock);
1350                 sst_set_pipe_gain(ids, drv, mute);
1351                 mutex_unlock(&drv->lock);
1352             }
1353         }
1354     } else {
1355         dev_dbg(dai->dev, "Stream name=%s\n",
1356                 dai->capture_widget->name);
1357         w = dai->capture_widget;
1358         snd_soc_dapm_widget_for_each_source_path(w, p) {
1359             if (p->connected && !p->connected(w, p->source))
1360                 continue;
1361 
1362             if (p->connect &&  p->source->power &&
1363                     is_sst_dapm_widget(p->source)) {
1364                 struct sst_ids *ids = p->source->priv;
1365 
1366                 dev_dbg(dai->dev, "send gain for widget=%s\n",
1367                         p->source->name);
1368                 mutex_lock(&drv->lock);
1369                 sst_set_pipe_gain(ids, drv, mute);
1370                 mutex_unlock(&drv->lock);
1371             }
1372         }
1373     }
1374     return 0;
1375 }
1376 
1377 /**
1378  * sst_fill_module_list - populate the list of modules/gains for a pipe
1379  * @kctl: kcontrol pointer
1380  * @w: dapm widget
1381  * @type: widget type
1382  *
1383  * Fills the widget pointer in the kcontrol private data, and also fills the
1384  * kcontrol pointer in the widget private data.
1385  *
1386  * Widget pointer is used to send the algo/gain in the .put() handler if the
1387  * widget is powerd on.
1388  *
1389  * Kcontrol pointer is used to send the algo/gain in the widget power ON/OFF
1390  * event handler. Each widget (pipe) has multiple algos stored in the algo_list.
1391  */
1392 static int sst_fill_module_list(struct snd_kcontrol *kctl,
1393      struct snd_soc_dapm_widget *w, int type)
1394 {
1395     struct sst_module *module;
1396     struct snd_soc_component *c = snd_soc_dapm_to_component(w->dapm);
1397     struct sst_ids *ids = w->priv;
1398     int ret = 0;
1399 
1400     module = devm_kzalloc(c->dev, sizeof(*module), GFP_KERNEL);
1401     if (!module)
1402         return -ENOMEM;
1403 
1404     if (type == SST_MODULE_GAIN) {
1405         struct sst_gain_mixer_control *mc = (void *)kctl->private_value;
1406 
1407         mc->w = w;
1408         module->kctl = kctl;
1409         list_add_tail(&module->node, &ids->gain_list);
1410     } else if (type == SST_MODULE_ALGO) {
1411         struct sst_algo_control *bc = (void *)kctl->private_value;
1412 
1413         bc->w = w;
1414         module->kctl = kctl;
1415         list_add_tail(&module->node, &ids->algo_list);
1416     } else {
1417         dev_err(c->dev, "invoked for unknown type %d module %s",
1418                 type, kctl->id.name);
1419         ret = -EINVAL;
1420     }
1421 
1422     return ret;
1423 }
1424 
1425 /**
1426  * sst_fill_widget_module_info - fill list of gains/algos for the pipe
1427  * @w: pipe modeled as a DAPM widget
1428  * @component: ASoC component
1429  *
1430  * Fill the list of gains/algos for the widget by looking at all the card
1431  * controls and comparing the name of the widget with the first part of control
1432  * name. First part of control name contains the pipe name (widget name).
1433  */
1434 static int sst_fill_widget_module_info(struct snd_soc_dapm_widget *w,
1435     struct snd_soc_component *component)
1436 {
1437     struct snd_kcontrol *kctl;
1438     int index, ret = 0;
1439     struct snd_card *card = component->card->snd_card;
1440     char *idx;
1441 
1442     down_read(&card->controls_rwsem);
1443 
1444     list_for_each_entry(kctl, &card->controls, list) {
1445         idx = strchr(kctl->id.name, ' ');
1446         if (idx == NULL)
1447             continue;
1448         index = idx - (char*)kctl->id.name;
1449         if (strncmp(kctl->id.name, w->name, index))
1450             continue;
1451 
1452         if (strstr(kctl->id.name, "Volume"))
1453             ret = sst_fill_module_list(kctl, w, SST_MODULE_GAIN);
1454 
1455         else if (strstr(kctl->id.name, "params"))
1456             ret = sst_fill_module_list(kctl, w, SST_MODULE_ALGO);
1457 
1458         else if (strstr(kctl->id.name, "Switch") &&
1459              strstr(kctl->id.name, "Gain")) {
1460             struct sst_gain_mixer_control *mc =
1461                         (void *)kctl->private_value;
1462 
1463             mc->w = w;
1464 
1465         } else if (strstr(kctl->id.name, "interleaver")) {
1466             struct sst_enum *e = (void *)kctl->private_value;
1467 
1468             e->w = w;
1469 
1470         } else if (strstr(kctl->id.name, "deinterleaver")) {
1471             struct sst_enum *e = (void *)kctl->private_value;
1472 
1473             e->w = w;
1474         }
1475 
1476         if (ret < 0) {
1477             up_read(&card->controls_rwsem);
1478             return ret;
1479         }
1480     }
1481 
1482     up_read(&card->controls_rwsem);
1483     return 0;
1484 }
1485 
1486 /**
1487  * sst_fill_linked_widgets - fill the parent pointer for the linked widget
1488  * @component: ASoC component
1489  * @ids: sst_ids array
1490  */
1491 static void sst_fill_linked_widgets(struct snd_soc_component *component,
1492                         struct sst_ids *ids)
1493 {
1494     struct snd_soc_dapm_widget *w;
1495     unsigned int len = strlen(ids->parent_wname);
1496 
1497     list_for_each_entry(w, &component->card->widgets, list) {
1498         if (!strncmp(ids->parent_wname, w->name, len)) {
1499             ids->parent_w = w;
1500             break;
1501         }
1502     }
1503 }
1504 
1505 /**
1506  * sst_map_modules_to_pipe - fill algo/gains list for all pipes
1507  * @component: ASoC component
1508  */
1509 static int sst_map_modules_to_pipe(struct snd_soc_component *component)
1510 {
1511     struct snd_soc_dapm_widget *w;
1512     int ret = 0;
1513 
1514     list_for_each_entry(w, &component->card->widgets, list) {
1515         if (is_sst_dapm_widget(w) && (w->priv)) {
1516             struct sst_ids *ids = w->priv;
1517 
1518             dev_dbg(component->dev, "widget type=%d name=%s\n",
1519                     w->id, w->name);
1520             INIT_LIST_HEAD(&ids->algo_list);
1521             INIT_LIST_HEAD(&ids->gain_list);
1522             ret = sst_fill_widget_module_info(w, component);
1523 
1524             if (ret < 0)
1525                 return ret;
1526 
1527             /* fill linked widgets */
1528             if (ids->parent_wname !=  NULL)
1529                 sst_fill_linked_widgets(component, ids);
1530         }
1531     }
1532     return 0;
1533 }
1534 
1535 int sst_dsp_init_v2_dpcm(struct snd_soc_component *component)
1536 {
1537     int i, ret = 0;
1538     struct snd_soc_dapm_context *dapm =
1539             snd_soc_component_get_dapm(component);
1540     struct sst_data *drv = snd_soc_component_get_drvdata(component);
1541     unsigned int gains = ARRAY_SIZE(sst_gain_controls)/3;
1542 
1543     drv->byte_stream = devm_kzalloc(component->dev,
1544                     SST_MAX_BIN_BYTES, GFP_KERNEL);
1545     if (!drv->byte_stream)
1546         return -ENOMEM;
1547 
1548     snd_soc_dapm_new_controls(dapm, sst_dapm_widgets,
1549             ARRAY_SIZE(sst_dapm_widgets));
1550     snd_soc_dapm_add_routes(dapm, intercon,
1551             ARRAY_SIZE(intercon));
1552     snd_soc_dapm_new_widgets(dapm->card);
1553 
1554     for (i = 0; i < gains; i++) {
1555         sst_gains[i].mute = SST_GAIN_MUTE_DEFAULT;
1556         sst_gains[i].l_gain = SST_GAIN_VOLUME_DEFAULT;
1557         sst_gains[i].r_gain = SST_GAIN_VOLUME_DEFAULT;
1558         sst_gains[i].ramp_duration = SST_GAIN_RAMP_DURATION_DEFAULT;
1559     }
1560 
1561     ret = snd_soc_add_component_controls(component, sst_gain_controls,
1562             ARRAY_SIZE(sst_gain_controls));
1563     if (ret)
1564         return ret;
1565 
1566     /* Initialize algo control params */
1567     ret = sst_algo_control_init(component->dev);
1568     if (ret)
1569         return ret;
1570     ret = snd_soc_add_component_controls(component, sst_algo_controls,
1571             ARRAY_SIZE(sst_algo_controls));
1572     if (ret)
1573         return ret;
1574 
1575     ret = snd_soc_add_component_controls(component, sst_slot_controls,
1576             ARRAY_SIZE(sst_slot_controls));
1577     if (ret)
1578         return ret;
1579 
1580     ret = sst_map_modules_to_pipe(component);
1581 
1582     return ret;
1583 }