Correct format
[tas2770sw-android/tas2770sw-android.git] / tas2770-codec.c
index 5ba3efe1e58909a7e4642593a530c3724d29a49b..6efab7a14d7f67c411510bc1afe67c99fc57be96 100644 (file)
@@ -8,19 +8,19 @@
 **
 ** This program is distributed in the hope that it will be useful, but WITHOUT
 ** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-** FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+** FOR A PARTICULAR PURPOSE.See the GNU General Public License for more details.
 **
 ** File:
 **     tas2770-codec.c
 **
 ** Description:
-**     ALSA SoC driver for Texas Instruments TAS2770 High Performance 4W Smart Amplifier
+**     ALSA SoC driver for TAS2770 20-W Digital Input Mono Class-D Audio Amplifier
+**     with Speaker I/V Sense
 **
 ** =============================================================================
 */
 
 #ifdef CONFIG_TAS2770_CODEC
-
 #define DEBUG
 #include <linux/module.h>
 #include <linux/moduleparam.h>
 
 
 #define TAS2770_MDELAY 0xFFFFFFFE
-//#define KCONTROL_CODEC
 
-#define DAPM_TO_CODEC
-static unsigned int tas2770_codec_read(struct snd_soc_codec *codec,  unsigned int reg)
+static unsigned int tas2770_codec_read(struct snd_soc_codec *codec,
+               unsigned int reg)
 {
        struct tas2770_priv *pTAS2770 = snd_soc_codec_get_drvdata(codec);
        int nResult = 0;
@@ -63,10 +62,12 @@ static unsigned int tas2770_codec_read(struct snd_soc_codec *codec,  unsigned in
                dev_err(pTAS2770->dev, "%s, ERROR, reg=0x%x, E=%d\n",
                        __func__, reg, nResult);
        else
-               dev_dbg(pTAS2770->dev, "%s, reg: 0x%x, value: 0x%x\n", __func__, reg, value);
+               dev_dbg(pTAS2770->dev, "%s, reg: 0x%x, value: 0x%x\n",
+                               __func__, reg, value);
 
        mutex_unlock(&pTAS2770->dev_lock);
-       if(nResult >= 0)
+
+       if (nResult >= 0)
                return value;
        else
                return nResult;
@@ -83,11 +84,12 @@ static int tas2770_codec_write(struct snd_soc_codec *codec, unsigned int reg,
        mutex_lock(&pTAS2770->dev_lock);
 
        nResult = regmap_write(pTAS2770->regmap, reg, value);
-       if(nResult < 0)
+       if (nResult < 0)
                dev_err(pTAS2770->dev, "%s, ERROR, reg=0x%x, E=%d\n",
                        __func__, reg, nResult);
        else
-               dev_dbg(pTAS2770->dev, "%s, reg: 0x%x, 0x%x\n", __func__, reg, value);
+               dev_dbg(pTAS2770->dev, "%s, reg: 0x%x, 0x%x\n",
+                       __func__, reg, value);
 
        mutex_unlock(&pTAS2770->dev_lock);
 
@@ -104,7 +106,6 @@ static int tas2770_codec_suspend(struct snd_soc_codec *codec)
        mutex_lock(&pTAS2770->codec_lock);
 
        dev_dbg(pTAS2770->dev, "%s\n", __func__);
-//     pTAS2770->runtime_suspend(pTAS2770);
 
        mutex_unlock(&pTAS2770->codec_lock);
        return ret;
@@ -118,7 +119,6 @@ static int tas2770_codec_resume(struct snd_soc_codec *codec)
        mutex_lock(&pTAS2770->codec_lock);
 
        dev_dbg(pTAS2770->dev, "%s\n", __func__);
-//     pTAS2770->runtime_resume(pTAS2770);
 
        mutex_unlock(&pTAS2770->codec_lock);
        return ret;
@@ -135,15 +135,10 @@ static const struct snd_kcontrol_new tas2770_asi_controls[] = {
 
 
 static int tas2770_dac_event(struct snd_soc_dapm_widget *w,
-                       struct snd_kcontrol *kcontrol, int event) 
+                       struct snd_kcontrol *kcontrol, int event)
 {
-#ifdef DAPM_TO_CODEC
        struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
-#else
-       struct snd_soc_codec *codec = w->codec;
-#endif
-//     struct tas2770_priv *pTAS2770 = snd_soc_codec_get_drvdata(codec);
-       pr_err("%s",__func__);
+
        switch (event) {
        case SND_SOC_DAPM_POST_PMU:
                snd_soc_update_bits(codec, TAS2770_PowerControl,
@@ -155,22 +150,32 @@ static int tas2770_dac_event(struct snd_soc_dapm_widget *w,
                        TAS2770_PowerControl_OperationalMode10_Mask,
                        TAS2770_PowerControl_OperationalMode10_Shutdown);
                break;
-       
+
        }
        return 0;
 
 }
 
+static const struct snd_kcontrol_new isense_switch =
+       SOC_DAPM_SINGLE("Switch", TAS2770_PowerControl, 3, 1, 1);
+static const struct snd_kcontrol_new vsense_switch =
+       SOC_DAPM_SINGLE("Switch", TAS2770_PowerControl, 2, 1, 1);
+
 static const struct snd_soc_dapm_widget tas2770_dapm_widgets[] = {
        SND_SOC_DAPM_AIF_IN("ASI1", "ASI1 Playback", 0, SND_SOC_NOPM, 0, 0),
        SND_SOC_DAPM_MIXER("ASI1 Sel",
                TAS2770_TDMConfigurationReg2, 4, 0,
                &tas2770_asi_controls[0],
                ARRAY_SIZE(tas2770_asi_controls)),
-       SND_SOC_DAPM_DAC_E("DAC", NULL, SND_SOC_NOPM, 0, 0,
-               tas2770_dac_event, SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
-       SND_SOC_DAPM_OUTPUT("OUT")
-
+       SND_SOC_DAPM_SWITCH("ISENSE", TAS2770_PowerControl, 3, 1,
+                       &isense_switch),
+       SND_SOC_DAPM_SWITCH("VSENSE", TAS2770_PowerControl, 2, 1,
+                       &vsense_switch),
+       SND_SOC_DAPM_DAC_E("DAC", NULL, SND_SOC_NOPM, 0, 0, tas2770_dac_event,
+       SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
+       SND_SOC_DAPM_OUTPUT("OUT"),
+       SND_SOC_DAPM_SIGGEN("VMON"),
+       SND_SOC_DAPM_SIGGEN("IMON")
 };
 
 static const struct snd_soc_dapm_route tas2770_audio_map[] = {
@@ -179,6 +184,8 @@ static const struct snd_soc_dapm_route tas2770_audio_map[] = {
        {"ASI1 Sel", "LeftRightDiv2", "ASI1"},
        {"DAC", NULL, "ASI1 Sel"},
        {"OUT", NULL, "DAC"},
+       {"ISENSE", "Switch", "IMON"},
+       {"VSENSE", "Switch", "VMON"},
 };
 
 
@@ -186,25 +193,22 @@ static int tas2770_mute(struct snd_soc_dai *dai, int mute)
 {
        struct snd_soc_codec *codec = dai->codec;
        struct tas2770_priv *pTAS2770 = snd_soc_codec_get_drvdata(codec);
-
-       dev_err(pTAS2770->dev, "%s mute %d",__func__, mute); 
+       dev_dbg(pTAS2770->dev, "%s\n", __func__);
 
        mutex_lock(&pTAS2770->codec_lock);
-       if(mute) {
+       if (mute) {
                snd_soc_update_bits(codec, TAS2770_PowerControl,
                        TAS2770_PowerControl_OperationalMode10_Mask,
-                       TAS2770_PowerControl_OperationalMode10_Mute); 
+                       TAS2770_PowerControl_OperationalMode10_Mute);
        } else {
                snd_soc_update_bits(codec, TAS2770_PowerControl,
                        TAS2770_PowerControl_OperationalMode10_Mask,
-                       TAS2770_PowerControl_OperationalMode10_Active); 
+                       TAS2770_PowerControl_OperationalMode10_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)
@@ -212,17 +216,18 @@ static int tas2770_hw_params(struct snd_pcm_substream *substream,
        struct snd_soc_codec *codec = dai->codec;
        struct tas2770_priv *pTAS2770 = snd_soc_codec_get_drvdata(codec);
        int ret = 0;
-       dev_err(pTAS2770->dev, "%s",__func__);
-       dev_dbg(pTAS2770->dev, "%s, format: %d\n", __func__, params_format(params));
 
-       switch(params_format(params)) {
+       dev_dbg(pTAS2770->dev, "%s, format: %d\n", __func__,
+               params_format(params));
+
+       switch (params_format(params)) {
        case SNDRV_PCM_FORMAT_S16_LE:
                snd_soc_update_bits(codec,
                        TAS2770_TDMConfigurationReg2,
                        TAS2770_TDMConfigurationReg2_RXWLEN32_Mask |
                        TAS2770_TDMConfigurationReg2_RXSLEN10_Mask,
                        TAS2770_TDMConfigurationReg2_RXWLEN32_16Bits |
-                       TAS2770_TDMConfigurationReg2_RXWLEN32_16Bits );
+                       TAS2770_TDMConfigurationReg2_RXSLEN10_16Bits);
                        pTAS2770->ch_size = 16;
                break;
        case SNDRV_PCM_FORMAT_S24_LE:
@@ -231,8 +236,8 @@ static int tas2770_hw_params(struct snd_pcm_substream *substream,
                        TAS2770_TDMConfigurationReg2_RXWLEN32_Mask |
                        TAS2770_TDMConfigurationReg2_RXSLEN10_Mask,
                        TAS2770_TDMConfigurationReg2_RXWLEN32_24Bits  |
-                       TAS2770_TDMConfigurationReg2_RXWLEN32_24Bits );
-                       pTAS2770->ch_size = 24;
+                       TAS2770_TDMConfigurationReg2_RXSLEN10_32Bits);
+                       pTAS2770->ch_size = 32;
                break;
        case SNDRV_PCM_FORMAT_S32_LE:
                        snd_soc_update_bits(codec,
@@ -240,16 +245,29 @@ static int tas2770_hw_params(struct snd_pcm_substream *substream,
                        TAS2770_TDMConfigurationReg2_RXWLEN32_Mask |
                        TAS2770_TDMConfigurationReg2_RXSLEN10_Mask,
                        TAS2770_TDMConfigurationReg2_RXWLEN32_32Bits |
-                       TAS2770_TDMConfigurationReg2_RXWLEN32_32Bits );
+                       TAS2770_TDMConfigurationReg2_RXSLEN10_32Bits);
                        pTAS2770->ch_size = 32;
                break;
 
        }
 
-       dev_dbg(pTAS2770->dev, "%s, sample rate: %d\n", __func__, params_rate(params));
-       switch(params_rate(params))
-       {
-               case 48000:
+       dev_dbg(pTAS2770->dev, "ch_size: %d\n", pTAS2770->ch_size);
+       snd_soc_update_bits(codec,
+               TAS2770_TDMConfigurationReg5,
+               TAS2770_TDMConfigurationReg5_VSNSTX_Mask |
+               TAS2770_TDMConfigurationReg5_VSNSSLOT50_Mask,
+               TAS2770_TDMConfigurationReg5_VSNSTX_Enable |
+               (pTAS2770->ch_size >> 3));
+       snd_soc_update_bits(codec,
+               TAS2770_TDMConfigurationReg6,
+               TAS2770_TDMConfigurationReg6_ISNSTX_Mask |
+               TAS2770_TDMConfigurationReg6_ISNSSLOT50_Mask,
+               TAS2770_TDMConfigurationReg6_ISNSTX_Enable);
+
+       dev_dbg(pTAS2770->dev, "%s, sample rate: %d\n", __func__,
+               params_rate(params));
+       switch (params_rate(params)) {
+       case 48000:
                        snd_soc_update_bits(codec,
                                TAS2770_TDMConfigurationReg0,
                                TAS2770_TDMConfigurationReg0_SAMPRATERAMP_Mask,
@@ -259,7 +277,7 @@ static int tas2770_hw_params(struct snd_pcm_substream *substream,
                                TAS2770_TDMConfigurationReg0_SAMPRATE31_Mask,
                                TAS2770_TDMConfigurationReg0_SAMPRATE31_44_1_48kHz);
                        break;
-               case 44100:
+       case 44100:
                        snd_soc_update_bits(codec,
                                TAS2770_TDMConfigurationReg0,
                                TAS2770_TDMConfigurationReg0_SAMPRATERAMP_Mask,
@@ -269,7 +287,7 @@ static int tas2770_hw_params(struct snd_pcm_substream *substream,
                                TAS2770_TDMConfigurationReg0_SAMPRATE31_Mask,
                                TAS2770_TDMConfigurationReg0_SAMPRATE31_44_1_48kHz);
                        break;
-               case 96000:
+       case 96000:
                        snd_soc_update_bits(codec,
                                TAS2770_TDMConfigurationReg0,
                                TAS2770_TDMConfigurationReg0_SAMPRATERAMP_Mask,
@@ -279,17 +297,7 @@ static int tas2770_hw_params(struct snd_pcm_substream *substream,
                                TAS2770_TDMConfigurationReg0_SAMPRATE31_Mask,
                                TAS2770_TDMConfigurationReg0_SAMPRATE31_88_2_96kHz);
                        break;
-               case 88200:
-                       snd_soc_update_bits(codec,
-                               TAS2770_TDMConfigurationReg0,
-                               TAS2770_TDMConfigurationReg0_SAMPRATERAMP_Mask,
-                               TAS2770_TDMConfigurationReg0_SAMPRATERAMP_44_1KHz);
-                       snd_soc_update_bits(codec,
-                               TAS2770_TDMConfigurationReg0,
-                               TAS2770_TDMConfigurationReg0_SAMPRATE31_Mask,
-                               TAS2770_TDMConfigurationReg0_SAMPRATE31_88_2_96kHz);
-                       break;
-               case 19200:
+       case 19200:
                        snd_soc_update_bits(codec,
                                TAS2770_TDMConfigurationReg0,
                                TAS2770_TDMConfigurationReg0_SAMPRATERAMP_Mask,
@@ -299,19 +307,9 @@ static int tas2770_hw_params(struct snd_pcm_substream *substream,
                                TAS2770_TDMConfigurationReg0_SAMPRATE31_Mask,
                                TAS2770_TDMConfigurationReg0_SAMPRATE31_176_4_192kHz);
                        break;
-               case 17640:
-                       snd_soc_update_bits(codec,
-                               TAS2770_TDMConfigurationReg0,
-                               TAS2770_TDMConfigurationReg0_SAMPRATERAMP_Mask,
-                               TAS2770_TDMConfigurationReg0_SAMPRATERAMP_44_1KHz);
-                       snd_soc_update_bits(codec,
-                               TAS2770_TDMConfigurationReg0,
-                               TAS2770_TDMConfigurationReg0_SAMPRATE31_Mask,
-                               TAS2770_TDMConfigurationReg0_SAMPRATE31_176_4_192kHz);
-                       break;
-               default:
+       default:
                        dev_dbg(pTAS2770->dev, "%s, unsupported sample rate\n", __func__);
-                       
+
        }
        return ret;
 }
@@ -325,9 +323,8 @@ static int tas2770_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
 
        dev_dbg(pTAS2770->dev, "%s, format=0x%x\n", __func__, fmt);
 
-#if 1
        switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
-       case SND_SOC_DAIFMT_CBS_CFS: 
+       case SND_SOC_DAIFMT_CBS_CFS:
                asi_cfg_1 = 0x00;
                break;
        default:
@@ -349,7 +346,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(codec, TAS2770_TDMConfigurationReg1,
                TAS2770_TDMConfigurationReg1_RXEDGE_Mask,
                asi_cfg_1);
 
@@ -365,15 +362,14 @@ static int tas2770_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
                tdm_rx_start_slot = 0;
                break;
        default:
-               dev_err(pTAS2770->dev, "DAI Format is not found, fmt=0x%x\n", fmt);
-               ret = -EINVAL;
+       dev_err(pTAS2770->dev, "DAI Format is not found, fmt=0x%x\n", fmt);
+       ret = -EINVAL;
                break;
        }
 
-       snd_soc_update_bits(codec, TAS2770_TDMConfigurationReg1, 
+       snd_soc_update_bits(codec, TAS2770_TDMConfigurationReg1,
                TAS2770_TDMConfigurationReg1_RXOFFSET51_Mask,
-               (tdm_rx_start_slot << TAS2770_TDMConfigurationReg1_RXOFFSET51_Shift));
-#endif
+       (tdm_rx_start_slot << TAS2770_TDMConfigurationReg1_RXOFFSET51_Shift));
        return ret;
 }
 
@@ -386,10 +382,8 @@ static struct snd_soc_dai_ops tas2770_dai_ops = {
 #define TAS2770_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
                SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
 
-#define TAS2770_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 \
-                                               SNDRV_PCM_RATE_88200 |\
+#define TAS2770_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
                                                SNDRV_PCM_RATE_96000 |\
-                                               SNDRV_PCM_RATE_176400 |\
                                                SNDRV_PCM_RATE_192000\
                                                )
 
@@ -401,15 +395,15 @@ static struct snd_soc_dai_driver tas2770_dai_driver[] = {
                        .stream_name    = "ASI1 Playback",
                        .channels_min   = 2,
                        .channels_max   = 2,
-                       .rates      = SNDRV_PCM_RATE_8000_192000,
+                       .rates      = TAS2770_RATES,
                        .formats    = TAS2770_FORMATS,
                },
                .capture = {
                        .stream_name    = "ASI1 Capture",
                        .channels_min   = 0,
                        .channels_max   = 2,
-                       .rates          = SNDRV_PCM_RATE_8000_192000,
-                       .formats    = TAS2770_FORMATS,  
+                       .rates          = TAS2770_RATES,
+                       .formats    = TAS2770_FORMATS,
                },
                .ops = &tas2770_dai_ops,
                .symmetric_rates = 1,
@@ -430,20 +424,13 @@ static int tas2770_codec_remove(struct snd_soc_codec *codec)
        return 0;
 }
 
-#if 0
-
-/*
- * DAC digital volumes. From 0 to 31 dB in 1 dB steps
- */
-static DECLARE_TLV_DB_SCALE(dac_tlv, 0, 100, 0);
+static DECLARE_TLV_DB_SCALE(tas2770_digital_tlv, 1100, 50, 0);
 
 static const struct snd_kcontrol_new tas2770_snd_controls[] = {
-       SOC_SINGLE_TLV("DAC Playback Volume", TAS2770_PlaybackConfigurationReg0, 0, 0x1f, 0,
-                       dac_tlv),
-//     SOC_SINGLE_EXT("TAS2770 PowerCtrl", SND_SOC_NOPM, 0, 0x0001, 0,
-//                     tas2770_power_ctrl_get, tas2770_power_ctrl_put),
+       SOC_SINGLE_TLV("Amp Output Level", TAS2770_PlaybackConfigurationReg0,
+               0, TAS2770_PlaybackConfigurationReg0_AmplifierLevel40_Mask, 0,
+               tas2770_digital_tlv),
 };
-#endif
 
 static struct snd_soc_codec_driver soc_codec_driver_tas2770 = {
        .probe                  = tas2770_codec_probe,
@@ -453,8 +440,8 @@ static struct snd_soc_codec_driver soc_codec_driver_tas2770 = {
        .suspend                = tas2770_codec_suspend,
        .resume                 = tas2770_codec_resume,
        .component_driver = {
-//             .controls               = tas2770_snd_controls,
-//             .num_controls           = ARRAY_SIZE(tas2770_snd_controls),
+               .controls               = tas2770_snd_controls,
+               .num_controls           = ARRAY_SIZE(tas2770_snd_controls),
                .dapm_widgets           = tas2770_dapm_widgets,
                .num_dapm_widgets       = ARRAY_SIZE(tas2770_dapm_widgets),
                .dapm_routes            = tas2770_audio_map,
@@ -470,7 +457,7 @@ int tas2770_register_codec(struct tas2770_priv *pTAS2770)
        nResult = snd_soc_register_codec(pTAS2770->dev,
                &soc_codec_driver_tas2770,
                tas2770_dai_driver, ARRAY_SIZE(tas2770_dai_driver));
-        dev_info(pTAS2770->dev, "%s, enter result %d\n", __func__,nResult);
+
        return nResult;
 }