ASoC: TWL4030: Add volume controls for outputs
[safe/jmp/linux-2.6] / sound / soc / codecs / twl4030.c
index c1893d2..ffd5120 100644 (file)
@@ -33,6 +33,7 @@
 #include <sound/soc.h>
 #include <sound/soc-dapm.h>
 #include <sound/initval.h>
+#include <sound/tlv.h>
 
 #include "twl4030.h"
 
@@ -87,7 +88,7 @@ static const u8 twl4030_reg[TWL4030_CACHEREGNUM] = {
        0x00, /* REG_ALC_SET1           (0x2C)  */
        0x00, /* REG_ALC_SET2           (0x2D)  */
        0x00, /* REG_BOOST_CTL          (0x2E)  */
-       0x01, /* REG_SOFTVOL_CTL        (0x2F)  */
+       0x00, /* REG_SOFTVOL_CTL        (0x2F)  */
        0x00, /* REG_DTMF_FREQSEL       (0x30)  */
        0x00, /* REG_DTMF_TONEXT1H      (0x31)  */
        0x00, /* REG_DTMF_TONEXT1L      (0x32)  */
@@ -189,13 +190,236 @@ 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;
+}
+
+/*
+ * FGAIN volume control:
+ * from -62 to 0 dB in 1 dB steps (mute instead of -63 dB)
+ */
+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);
+
 static const struct snd_kcontrol_new twl4030_snd_controls[] = {
-       SOC_DOUBLE_R("Master Playback Volume",
-                TWL4030_REG_ARXL2PGA, TWL4030_REG_ARXR2PGA,
-               0, 127, 0),
-       SOC_DOUBLE_R("Capture Volume",
-                TWL4030_REG_ATXL1PGA, TWL4030_REG_ATXR1PGA,
-               0, 127, 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),
+
+       /* 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),
 };
 
 /* add non dapm controls */
@@ -343,7 +567,8 @@ static int twl4030_set_bias_level(struct snd_soc_codec *codec,
 }
 
 static int twl4030_hw_params(struct snd_pcm_substream *substream,
-                          struct snd_pcm_hw_params *params)
+                          struct snd_pcm_hw_params *params,
+                          struct snd_soc_dai *dai)
 {
        struct snd_soc_pcm_runtime *rtd = substream->private_data;
        struct snd_soc_device *socdev = rtd->socdev;
@@ -523,8 +748,6 @@ struct snd_soc_dai twl4030_dai = {
                .formats = TWL4030_FORMATS,},
        .ops = {
                .hw_params = twl4030_hw_params,
-       },
-       .dai_ops = {
                .set_sysclk = twl4030_set_dai_sysclk,
                .set_fmt = twl4030_set_dai_fmt,
        }
@@ -591,7 +814,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;