ASoC: TWL4030: Add Analog PGA control switch to DAPM
[pandora-kernel.git] / sound / soc / codecs / twl4030.c
index 498c42f..4293ec7 100644 (file)
@@ -46,9 +46,9 @@ static const u8 twl4030_reg[TWL4030_CACHEREGNUM] = {
        0xc3, /* REG_OPTION             (0x2)   */
        0x00, /* REG_UNKNOWN            (0x3)   */
        0x00, /* REG_MICBIAS_CTL        (0x4)   */
-       0x24, /* REG_ANAMICL            (0x5)   */
-       0x04, /* REG_ANAMICR            (0x6)   */
-       0x0a, /* REG_AVADC_CTL          (0x7)   */
+       0x20, /* REG_ANAMICL            (0x5)   */
+       0x00, /* REG_ANAMICR            (0x6)   */
+       0x00, /* REG_AVADC_CTL          (0x7)   */
        0x00, /* REG_ADCMICSEL          (0x8)   */
        0x00, /* REG_DIGMIXING          (0x9)   */
        0x0c, /* REG_ATXL1PGA           (0xA)   */
@@ -190,19 +190,413 @@ static void twl4030_init_chip(struct snd_soc_codec *codec)
 
 }
 
+/*
+ * Some of the gain controls in TWL (mostly those which are associated with
+ * the outputs) are implemented in an interesting way:
+ * 0x0 : Power down (mute)
+ * 0x1 : 6dB
+ * 0x2 : 0 dB
+ * 0x3 : -6 dB
+ * Inverting not going to help with these.
+ * Custom volsw and volsw_2r get/put functions to handle these gain bits.
+ */
+#define SOC_DOUBLE_TLV_TWL4030(xname, xreg, shift_left, shift_right, xmax,\
+                              xinvert, tlv_array) \
+{      .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
+       .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
+                SNDRV_CTL_ELEM_ACCESS_READWRITE,\
+       .tlv.p = (tlv_array), \
+       .info = snd_soc_info_volsw, \
+       .get = snd_soc_get_volsw_twl4030, \
+       .put = snd_soc_put_volsw_twl4030, \
+       .private_value = (unsigned long)&(struct soc_mixer_control) \
+               {.reg = xreg, .shift = shift_left, .rshift = shift_right,\
+                .max = xmax, .invert = xinvert} }
+#define SOC_DOUBLE_R_TLV_TWL4030(xname, reg_left, reg_right, xshift, xmax,\
+                                xinvert, tlv_array) \
+{      .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
+       .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
+                SNDRV_CTL_ELEM_ACCESS_READWRITE,\
+       .tlv.p = (tlv_array), \
+       .info = snd_soc_info_volsw_2r, \
+       .get = snd_soc_get_volsw_r2_twl4030,\
+       .put = snd_soc_put_volsw_r2_twl4030, \
+       .private_value = (unsigned long)&(struct soc_mixer_control) \
+               {.reg = reg_left, .rreg = reg_right, .shift = xshift, \
+               .max = xmax, .invert = xinvert} }
+#define SOC_SINGLE_TLV_TWL4030(xname, xreg, xshift, xmax, xinvert, tlv_array) \
+       SOC_DOUBLE_TLV_TWL4030(xname, xreg, xshift, xshift, xmax, \
+                              xinvert, tlv_array)
+
+static int snd_soc_get_volsw_twl4030(struct snd_kcontrol *kcontrol,
+       struct snd_ctl_elem_value *ucontrol)
+{
+       struct soc_mixer_control *mc =
+               (struct soc_mixer_control *)kcontrol->private_value;
+       struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
+       unsigned int reg = mc->reg;
+       unsigned int shift = mc->shift;
+       unsigned int rshift = mc->rshift;
+       int max = mc->max;
+       int mask = (1 << fls(max)) - 1;
+
+       ucontrol->value.integer.value[0] =
+               (snd_soc_read(codec, reg) >> shift) & mask;
+       if (ucontrol->value.integer.value[0])
+               ucontrol->value.integer.value[0] =
+                       max + 1 - ucontrol->value.integer.value[0];
+
+       if (shift != rshift) {
+               ucontrol->value.integer.value[1] =
+                       (snd_soc_read(codec, reg) >> rshift) & mask;
+               if (ucontrol->value.integer.value[1])
+                       ucontrol->value.integer.value[1] =
+                               max + 1 - ucontrol->value.integer.value[1];
+       }
+
+       return 0;
+}
+
+static int snd_soc_put_volsw_twl4030(struct snd_kcontrol *kcontrol,
+       struct snd_ctl_elem_value *ucontrol)
+{
+       struct soc_mixer_control *mc =
+               (struct soc_mixer_control *)kcontrol->private_value;
+       struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
+       unsigned int reg = mc->reg;
+       unsigned int shift = mc->shift;
+       unsigned int rshift = mc->rshift;
+       int max = mc->max;
+       int mask = (1 << fls(max)) - 1;
+       unsigned short val, val2, val_mask;
+
+       val = (ucontrol->value.integer.value[0] & mask);
+
+       val_mask = mask << shift;
+       if (val)
+               val = max + 1 - val;
+       val = val << shift;
+       if (shift != rshift) {
+               val2 = (ucontrol->value.integer.value[1] & mask);
+               val_mask |= mask << rshift;
+               if (val2)
+                       val2 = max + 1 - val2;
+               val |= val2 << rshift;
+       }
+       return snd_soc_update_bits(codec, reg, val_mask, val);
+}
+
+static int snd_soc_get_volsw_r2_twl4030(struct snd_kcontrol *kcontrol,
+       struct snd_ctl_elem_value *ucontrol)
+{
+       struct soc_mixer_control *mc =
+               (struct soc_mixer_control *)kcontrol->private_value;
+       struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
+       unsigned int reg = mc->reg;
+       unsigned int reg2 = mc->rreg;
+       unsigned int shift = mc->shift;
+       int max = mc->max;
+       int mask = (1<<fls(max))-1;
+
+       ucontrol->value.integer.value[0] =
+               (snd_soc_read(codec, reg) >> shift) & mask;
+       ucontrol->value.integer.value[1] =
+               (snd_soc_read(codec, reg2) >> shift) & mask;
+
+       if (ucontrol->value.integer.value[0])
+               ucontrol->value.integer.value[0] =
+                       max + 1 - ucontrol->value.integer.value[0];
+       if (ucontrol->value.integer.value[1])
+               ucontrol->value.integer.value[1] =
+                       max + 1 - ucontrol->value.integer.value[1];
+
+       return 0;
+}
+
+static int snd_soc_put_volsw_r2_twl4030(struct snd_kcontrol *kcontrol,
+       struct snd_ctl_elem_value *ucontrol)
+{
+       struct soc_mixer_control *mc =
+               (struct soc_mixer_control *)kcontrol->private_value;
+       struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
+       unsigned int reg = mc->reg;
+       unsigned int reg2 = mc->rreg;
+       unsigned int shift = mc->shift;
+       int max = mc->max;
+       int mask = (1 << fls(max)) - 1;
+       int err;
+       unsigned short val, val2, val_mask;
+
+       val_mask = mask << shift;
+       val = (ucontrol->value.integer.value[0] & mask);
+       val2 = (ucontrol->value.integer.value[1] & mask);
+
+       if (val)
+               val = max + 1 - val;
+       if (val2)
+               val2 = max + 1 - val2;
+
+       val = val << shift;
+       val2 = val2 << shift;
+
+       err = snd_soc_update_bits(codec, reg, val_mask, val);
+       if (err < 0)
+               return err;
+
+       err = snd_soc_update_bits(codec, reg2, val_mask, val2);
+       return err;
+}
+
+static int twl4030_get_left_input(struct snd_kcontrol *kcontrol,
+       struct snd_ctl_elem_value *ucontrol)
+{
+       struct snd_soc_codec *codec = kcontrol->private_data;
+       u8 reg = twl4030_read_reg_cache(codec, TWL4030_REG_ANAMICL);
+       int result = 0;
+
+       /* one bit must be set a time */
+       reg &= TWL4030_CKMIC_EN | TWL4030_AUXL_EN | TWL4030_HSMIC_EN
+                       | TWL4030_MAINMIC_EN;
+       if (reg != 0) {
+               result++;
+               while ((reg & 1) == 0) {
+                       result++;
+                       reg >>= 1;
+               }
+       }
+
+       ucontrol->value.integer.value[0] = result;
+       return 0;
+}
+
+static int twl4030_put_left_input(struct snd_kcontrol *kcontrol,
+       struct snd_ctl_elem_value *ucontrol)
+{
+       struct snd_soc_codec *codec = kcontrol->private_data;
+       int value = ucontrol->value.integer.value[0];
+       u8 anamicl, micbias, avadc_ctl;
+
+       anamicl = twl4030_read_reg_cache(codec, TWL4030_REG_ANAMICL);
+       anamicl &= ~(TWL4030_CKMIC_EN | TWL4030_AUXL_EN | TWL4030_HSMIC_EN
+                       | TWL4030_MAINMIC_EN);
+       micbias = twl4030_read_reg_cache(codec, TWL4030_REG_MICBIAS_CTL);
+       micbias &= ~(TWL4030_HSMICBIAS_EN | TWL4030_MICBIAS1_EN);
+       avadc_ctl = twl4030_read_reg_cache(codec, TWL4030_REG_AVADC_CTL);
+
+       switch (value) {
+       case 1:
+               anamicl |= TWL4030_MAINMIC_EN;
+               micbias |= TWL4030_MICBIAS1_EN;
+               break;
+       case 2:
+               anamicl |= TWL4030_HSMIC_EN;
+               micbias |= TWL4030_HSMICBIAS_EN;
+               break;
+       case 3:
+               anamicl |= TWL4030_AUXL_EN;
+               break;
+       case 4:
+               anamicl |= TWL4030_CKMIC_EN;
+               break;
+       default:
+               break;
+       }
+
+       /* If some input is selected, enable amp and ADC */
+       if (value != 0) {
+               anamicl |= TWL4030_MICAMPL_EN;
+               avadc_ctl |= TWL4030_ADCL_EN;
+       } else {
+               anamicl &= ~TWL4030_MICAMPL_EN;
+               avadc_ctl &= ~TWL4030_ADCL_EN;
+       }
+
+       twl4030_write(codec, TWL4030_REG_ANAMICL, anamicl);
+       twl4030_write(codec, TWL4030_REG_MICBIAS_CTL, micbias);
+       twl4030_write(codec, TWL4030_REG_AVADC_CTL, avadc_ctl);
+
+       return 1;
+}
+
+static int twl4030_get_right_input(struct snd_kcontrol *kcontrol,
+       struct snd_ctl_elem_value *ucontrol)
+{
+       struct snd_soc_codec *codec = kcontrol->private_data;
+       u8 reg = twl4030_read_reg_cache(codec, TWL4030_REG_ANAMICR);
+       int value = 0;
+
+       reg &= TWL4030_SUBMIC_EN|TWL4030_AUXR_EN;
+       switch (reg) {
+       case TWL4030_SUBMIC_EN:
+               value = 1;
+               break;
+       case TWL4030_AUXR_EN:
+               value = 2;
+               break;
+       default:
+               break;
+       }
+
+       ucontrol->value.integer.value[0] = value;
+       return 0;
+}
+
+static int twl4030_put_right_input(struct snd_kcontrol *kcontrol,
+       struct snd_ctl_elem_value *ucontrol)
+{
+       struct snd_soc_codec *codec = kcontrol->private_data;
+       int value = ucontrol->value.integer.value[0];
+       u8 anamicr, micbias, avadc_ctl;
+
+       anamicr = twl4030_read_reg_cache(codec, TWL4030_REG_ANAMICR);
+       anamicr &= ~(TWL4030_SUBMIC_EN|TWL4030_AUXR_EN);
+       micbias = twl4030_read_reg_cache(codec, TWL4030_REG_MICBIAS_CTL);
+       micbias &= ~TWL4030_MICBIAS2_EN;
+       avadc_ctl = twl4030_read_reg_cache(codec, TWL4030_REG_AVADC_CTL);
+
+       switch (value) {
+       case 1:
+               anamicr |= TWL4030_SUBMIC_EN;
+               micbias |= TWL4030_MICBIAS2_EN;
+               break;
+       case 2:
+               anamicr |= TWL4030_AUXR_EN;
+               break;
+       default:
+               break;
+       }
+
+       if (value != 0) {
+               anamicr |= TWL4030_MICAMPR_EN;
+               avadc_ctl |= TWL4030_ADCR_EN;
+       } else {
+               anamicr &= ~TWL4030_MICAMPR_EN;
+               avadc_ctl &= ~TWL4030_ADCR_EN;
+       }
+
+       twl4030_write(codec, TWL4030_REG_ANAMICR, anamicr);
+       twl4030_write(codec, TWL4030_REG_MICBIAS_CTL, micbias);
+       twl4030_write(codec, TWL4030_REG_AVADC_CTL, avadc_ctl);
+
+       return 1;
+}
+
+static const char *twl4030_left_in_sel[] = {
+       "None",
+       "Main Mic",
+       "Headset Mic",
+       "Line In",
+       "Carkit Mic",
+};
+
+static const char *twl4030_right_in_sel[] = {
+       "None",
+       "Sub Mic",
+       "Line In",
+};
+
+static const struct soc_enum twl4030_left_input_mux =
+       SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(twl4030_left_in_sel),
+               twl4030_left_in_sel);
+
+static const struct soc_enum twl4030_right_input_mux =
+       SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(twl4030_right_in_sel),
+               twl4030_right_in_sel);
+
 /*
  * FGAIN volume control:
  * from -62 to 0 dB in 1 dB steps (mute instead of -63 dB)
  */
-static DECLARE_TLV_DB_SCALE(master_tlv, -6300, 100, 1);
+static DECLARE_TLV_DB_SCALE(digital_fine_tlv, -6300, 100, 1);
+
+/*
+ * CGAIN volume control:
+ * 0 dB to 12 dB in 6 dB steps
+ * value 2 and 3 means 12 dB
+ */
+static DECLARE_TLV_DB_SCALE(digital_coarse_tlv, 0, 600, 0);
+
+/*
+ * Analog playback gain
+ * -24 dB to 12 dB in 2 dB steps
+ */
+static DECLARE_TLV_DB_SCALE(analog_tlv, -2400, 200, 0);
+
+/*
+ * Gain controls tied to outputs
+ * -6 dB to 6 dB in 6 dB steps (mute instead of -12)
+ */
+static DECLARE_TLV_DB_SCALE(output_tvl, -1200, 600, 1);
+
+/*
+ * Capture gain after the ADCs
+ * from 0 dB to 31 dB in 1 dB steps
+ */
+static DECLARE_TLV_DB_SCALE(digital_capture_tlv, 0, 100, 0);
+
+/*
+ * Gain control for input amplifiers
+ * 0 dB to 30 dB in 6 dB steps
+ */
+static DECLARE_TLV_DB_SCALE(input_gain_tlv, 0, 600, 0);
 
 static const struct snd_kcontrol_new twl4030_snd_controls[] = {
-       SOC_DOUBLE_R_TLV("Master Playback Volume",
-                TWL4030_REG_ARXL2PGA, TWL4030_REG_ARXR2PGA,
-               0, 0x3f, 0, master_tlv),
-       SOC_DOUBLE_R("Capture Volume",
-                TWL4030_REG_ATXL1PGA, TWL4030_REG_ATXR1PGA,
-               0, 0x1f, 0),
+       /* Common playback gain controls */
+       SOC_DOUBLE_R_TLV("DAC1 Digital Fine Playback Volume",
+               TWL4030_REG_ARXL1PGA, TWL4030_REG_ARXR1PGA,
+               0, 0x3f, 0, digital_fine_tlv),
+       SOC_DOUBLE_R_TLV("DAC2 Digital Fine Playback Volume",
+               TWL4030_REG_ARXL2PGA, TWL4030_REG_ARXR2PGA,
+               0, 0x3f, 0, digital_fine_tlv),
+
+       SOC_DOUBLE_R_TLV("DAC1 Digital Coarse Playback Volume",
+               TWL4030_REG_ARXL1PGA, TWL4030_REG_ARXR1PGA,
+               6, 0x2, 0, digital_coarse_tlv),
+       SOC_DOUBLE_R_TLV("DAC2 Digital Coarse Playback Volume",
+               TWL4030_REG_ARXL2PGA, TWL4030_REG_ARXR2PGA,
+               6, 0x2, 0, digital_coarse_tlv),
+
+       SOC_DOUBLE_R_TLV("DAC1 Analog Playback Volume",
+               TWL4030_REG_ARXL1_APGA_CTL, TWL4030_REG_ARXR1_APGA_CTL,
+               3, 0x12, 1, analog_tlv),
+       SOC_DOUBLE_R_TLV("DAC2 Analog Playback Volume",
+               TWL4030_REG_ARXL2_APGA_CTL, TWL4030_REG_ARXR2_APGA_CTL,
+               3, 0x12, 1, analog_tlv),
+       SOC_DOUBLE_R("DAC1 Analog Playback Switch",
+               TWL4030_REG_ARXL1_APGA_CTL, TWL4030_REG_ARXR1_APGA_CTL,
+               1, 1, 0),
+       SOC_DOUBLE_R("DAC2 Analog Playback Switch",
+               TWL4030_REG_ARXL2_APGA_CTL, TWL4030_REG_ARXR2_APGA_CTL,
+               1, 1, 0),
+
+       /* Separate output gain controls */
+       SOC_DOUBLE_R_TLV_TWL4030("PreDriv Playback Volume",
+               TWL4030_REG_PREDL_CTL, TWL4030_REG_PREDR_CTL,
+               4, 3, 0, output_tvl),
+
+       SOC_DOUBLE_TLV_TWL4030("Headset Playback Volume",
+               TWL4030_REG_HS_GAIN_SET, 0, 2, 3, 0, output_tvl),
+
+       SOC_DOUBLE_R_TLV_TWL4030("Carkit Playback Volume",
+               TWL4030_REG_PRECKL_CTL, TWL4030_REG_PRECKR_CTL,
+               4, 3, 0, output_tvl),
+
+       SOC_SINGLE_TLV_TWL4030("Earpiece Playback Volume",
+               TWL4030_REG_EAR_CTL, 4, 3, 0, output_tvl),
+
+       /* Common capture gain controls */
+       SOC_DOUBLE_R_TLV("Capture Volume",
+               TWL4030_REG_ATXL1PGA, TWL4030_REG_ATXR1PGA,
+               0, 0x1f, 0, digital_capture_tlv),
+
+       SOC_DOUBLE_TLV("Input Boost Volume", TWL4030_REG_ANAMIC_GAIN,
+               0, 3, 5, 0, input_gain_tlv),
+
+       /* Input source controls */
+       SOC_ENUM_EXT("Left Input Source", twl4030_left_input_mux,
+               twl4030_get_left_input, twl4030_put_left_input),
+       SOC_ENUM_EXT("Right Input Source", twl4030_right_input_mux,
+               twl4030_get_right_input, twl4030_put_right_input),
 };
 
 /* add non dapm controls */
@@ -228,17 +622,39 @@ static const struct snd_soc_dapm_widget twl4030_dapm_widgets[] = {
        SND_SOC_DAPM_OUTPUT("OUTL"),
        SND_SOC_DAPM_OUTPUT("OUTR"),
 
-       SND_SOC_DAPM_DAC("DACL", "Left Playback", SND_SOC_NOPM, 0, 0),
-       SND_SOC_DAPM_DAC("DACR", "Right Playback", SND_SOC_NOPM, 0, 0),
+       /* DACs */
+       SND_SOC_DAPM_DAC("DACR1", "Right Front Playback",
+                       TWL4030_REG_AVDAC_CTL, 0, 0),
+       SND_SOC_DAPM_DAC("DACL1", "Left Front Playback",
+                       TWL4030_REG_AVDAC_CTL, 1, 0),
+       SND_SOC_DAPM_DAC("DACR2", "Right Rear Playback",
+                       TWL4030_REG_AVDAC_CTL, 2, 0),
+       SND_SOC_DAPM_DAC("DACL2", "Left Rear Playback",
+                       TWL4030_REG_AVDAC_CTL, 3, 0),
+
+       /* Analog PGAs */
+       SND_SOC_DAPM_PGA("ARXR1_APGA", TWL4030_REG_ARXR1_APGA_CTL,
+                       0, 0, NULL, 0),
+       SND_SOC_DAPM_PGA("ARXL1_APGA", TWL4030_REG_ARXL1_APGA_CTL,
+                       0, 0, NULL, 0),
+       SND_SOC_DAPM_PGA("ARXR2_APGA", TWL4030_REG_ARXR2_APGA_CTL,
+                       0, 0, NULL, 0),
+       SND_SOC_DAPM_PGA("ARXL2_APGA", TWL4030_REG_ARXL2_APGA_CTL,
+                       0, 0, NULL, 0),
 
        SND_SOC_DAPM_ADC("ADCL", "Left Capture", SND_SOC_NOPM, 0, 0),
        SND_SOC_DAPM_ADC("ADCR", "Right Capture", SND_SOC_NOPM, 0, 0),
 };
 
 static const struct snd_soc_dapm_route intercon[] = {
+       {"ARXL1_APGA", NULL, "DACL1"},
+       {"ARXR1_APGA", NULL, "DACR1"},
+       {"ARXL2_APGA", NULL, "DACL2"},
+       {"ARXR2_APGA", NULL, "DACR2"},
+
        /* outputs */
-       {"OUTL", NULL, "DACL"},
-       {"OUTR", NULL, "DACR"},
+       {"OUTL", NULL, "ARXL2_APGA"},
+       {"OUTR", NULL, "ARXR2_APGA"},
 
        /* inputs */
        {"ADCL", NULL, "INL"},
@@ -597,7 +1013,7 @@ static int twl4030_init(struct snd_soc_device *socdev)
        twl4030_add_controls(codec);
        twl4030_add_widgets(codec);
 
-       ret = snd_soc_register_card(socdev);
+       ret = snd_soc_init_card(socdev);
        if (ret < 0) {
                printk(KERN_ERR "twl4030: failed to register card\n");
                goto card_err;