Support bitwidth change in IV data
[tas2770sw-android/tas2770sw-android.git] / tas2770-codec.c
index ce05bc2172cb77d994b2350ed5e6cb115ea8b954..1e4e852969d68c5ef37128e5396c0a8574de207f 100644 (file)
@@ -137,27 +137,58 @@ static SOC_ENUM_SINGLE_DECL(
 static const struct snd_kcontrol_new tas2770_asi1_mux =
        SOC_DAPM_ENUM("ASI1 Source", tas2770_ASI1_src_enum);
 
-
-static int tas2770_dac_event(struct snd_soc_dapm_widget *w,
-                       struct snd_kcontrol *kcontrol, int event)
+static int tas2770_set_power_state(struct tas2770_priv *pTAS2770, int state)
 {
-       struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
+       struct snd_soc_codec *codec = pTAS2770->codec;
 
-       switch (event) {
-       case SND_SOC_DAPM_POST_PMU:
+       switch (state) {
+       case TAS2770_POWER_ACTIVE:
+               snd_soc_update_bits(codec, TAS2770_PowerControl,
+                       TAS2770_PowerControl_OperationalMode10_Mask,
+                       TAS2770_PowerControl_OperationalMode10_Active);
+               break;
+
+       case TAS2770_POWER_MUTE:
                snd_soc_update_bits(codec, TAS2770_PowerControl,
                        TAS2770_PowerControl_OperationalMode10_Mask,
                        TAS2770_PowerControl_OperationalMode10_Mute);
                break;
-       case SND_SOC_DAPM_PRE_PMD:
+
+       case TAS2770_POWER_SHUTDOWN:
                snd_soc_update_bits(codec, TAS2770_PowerControl,
                        TAS2770_PowerControl_OperationalMode10_Mask,
                        TAS2770_PowerControl_OperationalMode10_Shutdown);
                break;
 
+       default:
+               dev_err(pTAS2770->dev, "wrong power state setting %d\n", state);
+
        }
+
+       pTAS2770->mnPowerState = state;
        return 0;
+}
+
+static int tas2770_dac_event(struct snd_soc_dapm_widget *w,
+                       struct snd_kcontrol *kcontrol, int event)
+{
+       struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
+       struct tas2770_priv *pTAS2770 = snd_soc_codec_get_drvdata(codec);
+
+       mutex_lock(&pTAS2770->codec_lock);
+
+       switch (event) {
+       case SND_SOC_DAPM_POST_PMU:
+               tas2770_set_power_state(pTAS2770, TAS2770_POWER_MUTE);
+               break;
+       case SND_SOC_DAPM_PRE_PMD:
+               tas2770_set_power_state(pTAS2770, TAS2770_POWER_SHUTDOWN);
+               break;
+
+       }
 
+       mutex_unlock(&pTAS2770->codec_lock);
+       return 0;
 }
 
 static const struct snd_kcontrol_new isense_switch =
@@ -200,57 +231,44 @@ static int tas2770_mute(struct snd_soc_dai *dai, int mute)
 
        mutex_lock(&pTAS2770->codec_lock);
        if (mute) {
-               snd_soc_update_bits(codec, TAS2770_PowerControl,
-                       TAS2770_PowerControl_OperationalMode10_Mask,
-                       TAS2770_PowerControl_OperationalMode10_Mute);
+               tas2770_set_power_state(pTAS2770, TAS2770_POWER_MUTE);
        } else {
-               snd_soc_update_bits(codec, TAS2770_PowerControl,
-                       TAS2770_PowerControl_OperationalMode10_Mask,
-                       TAS2770_PowerControl_OperationalMode10_Active);
+               tas2770_set_power_state(pTAS2770, TAS2770_POWER_ACTIVE);
        }
        mutex_unlock(&pTAS2770->codec_lock);
        return 0;
 }
 
-static int tas2770_hw_params(struct snd_pcm_substream *substream,
-               struct snd_pcm_hw_params *params,
-               struct snd_soc_dai *dai)
+static int tas2770_set_bitwidth(struct tas2770_priv *pTAS2770, int bitwidth)
 {
-       struct snd_soc_codec *codec = dai->codec;
-       struct tas2770_priv *pTAS2770 = snd_soc_codec_get_drvdata(codec);
-       int ret = 0;
-       int slot_width_tmp = 16;
-
-       dev_dbg(pTAS2770->dev, "%s, format: %d\n", __func__,
-               params_format(params));
-
-       switch (params_format(params)) {
+       switch (bitwidth) {
        case SNDRV_PCM_FORMAT_S16_LE:
-               snd_soc_update_bits(codec,
+               snd_soc_update_bits(pTAS2770->codec,
                        TAS2770_TDMConfigurationReg2,
                        TAS2770_TDMConfigurationReg2_RXWLEN32_Mask,
                        TAS2770_TDMConfigurationReg2_RXWLEN32_16Bits);
-                       pTAS2770->mnCh_size = 16;
+                       /* If machine driver did not call set slot width */
                        if (pTAS2770->mnSlot_width == 0)
-                               slot_width_tmp = 16;
+                               tas2770_set_slot(pTAS2770->codec, 16);
+               pTAS2770->mnVmon_slot_no = pTAS2770->mnImon_slot_no + 2;
                break;
        case SNDRV_PCM_FORMAT_S24_LE:
-                       snd_soc_update_bits(codec,
+                       snd_soc_update_bits(pTAS2770->codec,
                        TAS2770_TDMConfigurationReg2,
                        TAS2770_TDMConfigurationReg2_RXWLEN32_Mask,
                        TAS2770_TDMConfigurationReg2_RXWLEN32_24Bits);
-                       pTAS2770->mnCh_size = 24;
                        if (pTAS2770->mnSlot_width == 0)
-                               slot_width_tmp = 32;
+                               tas2770_set_slot(pTAS2770->codec, 32);
+               pTAS2770->mnVmon_slot_no = pTAS2770->mnImon_slot_no + 4;
                break;
        case SNDRV_PCM_FORMAT_S32_LE:
-                       snd_soc_update_bits(codec,
+                       snd_soc_update_bits(pTAS2770->codec,
                        TAS2770_TDMConfigurationReg2,
                        TAS2770_TDMConfigurationReg2_RXWLEN32_Mask,
                        TAS2770_TDMConfigurationReg2_RXWLEN32_32Bits);
-                       pTAS2770->mnCh_size = 32;
                        if (pTAS2770->mnSlot_width == 0)
-                               slot_width_tmp = 32;
+                               tas2770_set_slot(pTAS2770->codec, 32);
+               pTAS2770->mnVmon_slot_no = pTAS2770->mnImon_slot_no + 4;
                break;
 
        default:
@@ -258,63 +276,84 @@ static int tas2770_hw_params(struct snd_pcm_substream *substream,
                return -EINVAL;
        }
 
-       /* If machine driver did not call set slot width */
-       if (pTAS2770->mnSlot_width == 0)
-               tas2770_set_slot(codec, slot_width_tmp);
-
+       pTAS2770->mnCh_size = bitwidth;
        dev_dbg(pTAS2770->dev, "mnCh_size: %d\n", pTAS2770->mnCh_size);
-       snd_soc_update_bits(codec,
+
+       snd_soc_update_bits(pTAS2770->codec,
                TAS2770_TDMConfigurationReg5,
                TAS2770_TDMConfigurationReg5_VSNSTX_Mask |
                TAS2770_TDMConfigurationReg5_VSNSSLOT50_Mask,
                TAS2770_TDMConfigurationReg5_VSNSTX_Enable |
                pTAS2770->mnVmon_slot_no);
-       snd_soc_update_bits(codec,
+       snd_soc_update_bits(pTAS2770->codec,
                TAS2770_TDMConfigurationReg6,
                TAS2770_TDMConfigurationReg6_ISNSTX_Mask |
                TAS2770_TDMConfigurationReg6_ISNSSLOT50_Mask,
                TAS2770_TDMConfigurationReg6_ISNSTX_Enable |
                pTAS2770->mnImon_slot_no);
 
-       dev_dbg(pTAS2770->dev, "%s, sample rate: %d\n", __func__,
-               params_rate(params));
-       switch (params_rate(params)) {
+       return 0;
+}
+
+static int tas2770_set_samplerate(struct tas2770_priv *pTAS2770, int samplerate)
+{
+       switch (samplerate) {
        case 48000:
-                       snd_soc_update_bits(codec,
+                       snd_soc_update_bits(pTAS2770->codec,
                                TAS2770_TDMConfigurationReg0,
                                TAS2770_TDMConfigurationReg0_SAMPRATERAMP_Mask,
                                TAS2770_TDMConfigurationReg0_SAMPRATERAMP_48KHz);
-                       snd_soc_update_bits(codec,
+                       snd_soc_update_bits(pTAS2770->codec,
                                TAS2770_TDMConfigurationReg0,
                                TAS2770_TDMConfigurationReg0_SAMPRATE31_Mask,
                                TAS2770_TDMConfigurationReg0_SAMPRATE31_44_1_48kHz);
                        break;
        case 44100:
-                       snd_soc_update_bits(codec,
+                       snd_soc_update_bits(pTAS2770->codec,
                                TAS2770_TDMConfigurationReg0,
                                TAS2770_TDMConfigurationReg0_SAMPRATERAMP_Mask,
                                TAS2770_TDMConfigurationReg0_SAMPRATERAMP_44_1KHz);
-                       snd_soc_update_bits(codec,
+                       snd_soc_update_bits(pTAS2770->codec,
                                TAS2770_TDMConfigurationReg0,
                                TAS2770_TDMConfigurationReg0_SAMPRATE31_Mask,
                                TAS2770_TDMConfigurationReg0_SAMPRATE31_44_1_48kHz);
                        break;
        case 96000:
-                       snd_soc_update_bits(codec,
+                       snd_soc_update_bits(pTAS2770->codec,
                                TAS2770_TDMConfigurationReg0,
                                TAS2770_TDMConfigurationReg0_SAMPRATERAMP_Mask,
                                TAS2770_TDMConfigurationReg0_SAMPRATERAMP_48KHz);
-                       snd_soc_update_bits(codec,
+                       snd_soc_update_bits(pTAS2770->codec,
+                               TAS2770_TDMConfigurationReg0,
+                               TAS2770_TDMConfigurationReg0_SAMPRATE31_Mask,
+                               TAS2770_TDMConfigurationReg0_SAMPRATE31_88_2_96kHz);
+                       break;
+       case 88200:
+                       snd_soc_update_bits(pTAS2770->codec,
+                               TAS2770_TDMConfigurationReg0,
+                               TAS2770_TDMConfigurationReg0_SAMPRATERAMP_Mask,
+                               TAS2770_TDMConfigurationReg0_SAMPRATERAMP_44_1KHz);
+                       snd_soc_update_bits(pTAS2770->codec,
                                TAS2770_TDMConfigurationReg0,
                                TAS2770_TDMConfigurationReg0_SAMPRATE31_Mask,
                                TAS2770_TDMConfigurationReg0_SAMPRATE31_88_2_96kHz);
                        break;
        case 19200:
-                       snd_soc_update_bits(codec,
+                       snd_soc_update_bits(pTAS2770->codec,
                                TAS2770_TDMConfigurationReg0,
                                TAS2770_TDMConfigurationReg0_SAMPRATERAMP_Mask,
                                TAS2770_TDMConfigurationReg0_SAMPRATERAMP_48KHz);
-                       snd_soc_update_bits(codec,
+                       snd_soc_update_bits(pTAS2770->codec,
+                               TAS2770_TDMConfigurationReg0,
+                               TAS2770_TDMConfigurationReg0_SAMPRATE31_Mask,
+                               TAS2770_TDMConfigurationReg0_SAMPRATE31_176_4_192kHz);
+                       break;
+       case 17640:
+                       snd_soc_update_bits(pTAS2770->codec,
+                               TAS2770_TDMConfigurationReg0,
+                               TAS2770_TDMConfigurationReg0_SAMPRATERAMP_Mask,
+                               TAS2770_TDMConfigurationReg0_SAMPRATERAMP_44_1KHz);
+                       snd_soc_update_bits(pTAS2770->codec,
                                TAS2770_TDMConfigurationReg0,
                                TAS2770_TDMConfigurationReg0_SAMPRATE31_Mask,
                                TAS2770_TDMConfigurationReg0_SAMPRATE31_176_4_192kHz);
@@ -323,18 +362,42 @@ static int tas2770_hw_params(struct snd_pcm_substream *substream,
                        dev_dbg(pTAS2770->dev, "%s, unsupported sample rate\n", __func__);
 
        }
-       return ret;
+
+       pTAS2770->mnSamplingRate = samplerate;
+       return 0;
 }
 
-static int tas2770_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
+static int tas2770_hw_params(struct snd_pcm_substream *substream,
+               struct snd_pcm_hw_params *params,
+               struct snd_soc_dai *dai)
 {
-       u8 tdm_rx_start_slot = 0, asi_cfg_1 = 0;
        struct snd_soc_codec *codec = dai->codec;
        struct tas2770_priv *pTAS2770 = snd_soc_codec_get_drvdata(codec);
        int ret = 0;
-       int value = 0;
 
-       dev_dbg(pTAS2770->dev, "%s, format=0x%x\n", __func__, fmt);
+       dev_dbg(pTAS2770->dev, "%s, format: %d\n", __func__,
+               params_format(params));
+
+       mutex_lock(&pTAS2770->codec_lock);
+
+       ret = tas2770_set_bitwidth(pTAS2770, params_format(params));
+       if(ret < 0)
+               return ret;
+
+       dev_dbg(pTAS2770->dev, "%s, sample rate: %d\n", __func__,
+               params_rate(params));
+
+       ret = tas2770_set_samplerate(pTAS2770, params_rate(params));
+
+       mutex_unlock(&pTAS2770->codec_lock);
+       return ret;
+}
+
+static int tas2770_set_fmt(struct tas2770_priv *pTAS2770, unsigned int fmt)
+{
+       u8 tdm_rx_start_slot = 0, asi_cfg_1 = 0;
+       int ret = 0;
+       int value = 0;
 
        switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
        case SND_SOC_DAIFMT_CBS_CFS:
@@ -359,7 +422,7 @@ static int tas2770_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
                ret = -EINVAL;
        }
 
-       snd_soc_update_bits(codec, TAS2770_TDMConfigurationReg1,
+       snd_soc_update_bits(pTAS2770->codec, TAS2770_TDMConfigurationReg1,
                TAS2770_TDMConfigurationReg1_RXEDGE_Mask,
                asi_cfg_1);
 
@@ -380,20 +443,37 @@ static int tas2770_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
                break;
        }
 
-       snd_soc_update_bits(codec, TAS2770_TDMConfigurationReg1,
+       snd_soc_update_bits(pTAS2770->codec, TAS2770_TDMConfigurationReg1,
                TAS2770_TDMConfigurationReg1_RXOFFSET51_Mask,
        (tdm_rx_start_slot << TAS2770_TDMConfigurationReg1_RXOFFSET51_Shift));
 
-       snd_soc_update_bits(codec, TAS2770_TDMConfigurationReg3,
+       snd_soc_update_bits(pTAS2770->codec, TAS2770_TDMConfigurationReg3,
                TAS2770_TDMConfigurationReg3_RXSLOTLeft30_Mask,
                (pTAS2770->mnLeftSlot << TAS2770_TDMConfigurationReg3_RXSLOTLeft30_Shift));
-       snd_soc_update_bits(codec, TAS2770_TDMConfigurationReg3,
+       snd_soc_update_bits(pTAS2770->codec, TAS2770_TDMConfigurationReg3,
                TAS2770_TDMConfigurationReg3_RXSLOTRight74_Mask,
        (pTAS2770->mnRightSlot << TAS2770_TDMConfigurationReg3_RXSLOTRight74_Shift));
 
-       value = snd_soc_read(codec, TAS2770_TDMConfigurationReg3);
+       value = snd_soc_read(pTAS2770->codec, TAS2770_TDMConfigurationReg3);
        dev_dbg(pTAS2770->dev, "slot value: 0x%x", value);
 
+       pTAS2770->mnASIFormat = fmt;
+
+       return 0;
+}
+
+static int tas2770_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
+{
+       struct snd_soc_codec *codec = dai->codec;
+       struct tas2770_priv *pTAS2770 = snd_soc_codec_get_drvdata(codec);
+       int ret = 0;
+
+       dev_dbg(pTAS2770->dev, "%s, format=0x%x\n", __func__, fmt);
+       mutex_lock(&pTAS2770->codec_lock);
+
+       ret = tas2770_set_fmt(pTAS2770, fmt);
+
+       mutex_unlock(&pTAS2770->codec_lock);
        return ret;
 }
 
@@ -450,7 +530,9 @@ static int tas2770_set_dai_tdm_slot(struct snd_soc_dai *dai,
        dev_dbg(pTAS2770->dev, "%s, tx_mask:%d, rx_mask:%d, slots:%d, slot_width:%d",
                        __func__, tx_mask, rx_mask, slots, slot_width);
 
+       mutex_lock(&pTAS2770->codec_lock);
        ret = tas2770_set_slot(codec, slot_width);
+       mutex_unlock(&pTAS2770->codec_lock);
 
        return ret;
 }
@@ -498,7 +580,7 @@ static int tas2770_codec_probe(struct snd_soc_codec *codec)
        struct tas2770_priv *pTAS2770 = snd_soc_codec_get_drvdata(codec);
 
        dev_err(pTAS2770->dev, "%s\n", __func__);
-       snd_soc_codec_init_regmap(codec, pTAS2770->regmap);
+       pTAS2770->codec = codec;
 
        return 0;
 }
@@ -509,14 +591,14 @@ static int tas2770_codec_remove(struct snd_soc_codec *codec)
 }
 
 static DECLARE_TLV_DB_SCALE(tas2770_digital_tlv, 1100, 50, 0);
-static DECLARE_TLV_DB_SCALE(tas2770_playback_volume, -10000, 50, 0);
+static DECLARE_TLV_DB_SCALE(tas2770_playback_volume, -12750, 50, 0);
 
 static const struct snd_kcontrol_new tas2770_snd_controls[] = {
        SOC_SINGLE_TLV("Amp Output Level", TAS2770_PlaybackConfigurationReg0,
-               0, TAS2770_PlaybackConfigurationReg0_AmplifierLevel40_Mask, 0,
+               0, 0x14, 0,
                tas2770_digital_tlv),
        SOC_SINGLE_TLV("Playback Volume", TAS2770_PlaybackConfigurationReg2,
-               0, TAS2770_PlaybackConfigurationReg2_DVCPCM70_Mask, 1,
+               0, TAS2770_PlaybackConfigurationReg2_VOLMAX, 1,
                tas2770_playback_volume),
 };
 
@@ -556,6 +638,39 @@ int tas2770_deregister_codec(struct tas2770_priv *pTAS2770)
        return 0;
 }
 
+void tas2770_LoadConfig(struct tas2770_priv *pTAS2770)
+{
+       int ret = 0;
+
+       pTAS2770->hw_reset(pTAS2770);
+       snd_soc_write(pTAS2770->codec, TAS2770_SoftwareReset,
+                       TAS2770_SoftwareReset_SoftwareReset_Reset);
+
+       ret = tas2770_set_slot(pTAS2770->codec, pTAS2770->mnSlot_width);
+       if (ret < 0)
+               goto end;
+
+       ret = tas2770_set_fmt(pTAS2770, pTAS2770->mnASIFormat);
+       if (ret < 0)
+               goto end;
+
+       ret = tas2770_set_bitwidth(pTAS2770, pTAS2770->mnCh_size);
+       if (ret < 0)
+               goto end;
+
+       ret = tas2770_set_samplerate(pTAS2770, pTAS2770->mnSamplingRate);
+       if (ret < 0)
+               goto end;
+
+       ret = tas2770_set_power_state(pTAS2770, pTAS2770->mnPowerState);
+
+end:
+/* power up failed, restart later */
+       if (ret < 0)
+               schedule_delayed_work(&pTAS2770->irq_work,
+                               msecs_to_jiffies(1000));
+}
+
 MODULE_AUTHOR("Texas Instruments Inc.");
 MODULE_DESCRIPTION("TAS2770 ALSA SOC Smart Amplifier driver");
 MODULE_LICENSE("GPL v2");