[ALSA] ca0106 - Fix write proc assignment
[safe/jmp/linux-2.6] / sound / pci / ca0106 / ca0106_mixer.c
index b6b8882..3f9b5c5 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *  Copyright (c) 2004 James Courtier-Dutton <James@superbug.demon.co.uk>
  *  Driver CA0106 chips. e.g. Sound Blaster Audigy LS and Live 24bit
- *  Version: 0.0.17
+ *  Version: 0.0.18
  *
  *  FEATURES currently supported:
  *    See ca0106_main.c for features.
@@ -39,6 +39,8 @@
  *    Modified Copyright message.
  *  0.0.17
  *    Implement Mic and Line in Capture.
+ *  0.0.18
+ *    Add support for mute control on SB Live 24bit (cards w/ SPI DAC)
  *
  *  This code was initally based on code from ALSA's emu10k1x.c which is:
  *  Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
@@ -62,7 +64,6 @@
 #include <linux/delay.h>
 #include <linux/init.h>
 #include <linux/interrupt.h>
-#include <linux/pci.h>
 #include <linux/slab.h>
 #include <linux/moduleparam.h>
 #include <sound/core.h>
 #include <sound/pcm.h>
 #include <sound/ac97_codec.h>
 #include <sound/info.h>
+#include <sound/tlv.h>
+#include <asm/io.h>
 
 #include "ca0106.h"
 
-static int snd_ca0106_shared_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
-{
-       uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
-       uinfo->count = 1;
-       uinfo->value.integer.min = 0;
-       uinfo->value.integer.max = 1;
-       return 0;
-}
+static const DECLARE_TLV_DB_SCALE(snd_ca0106_db_scale1, -5175, 25, 1);
+static const DECLARE_TLV_DB_SCALE(snd_ca0106_db_scale2, -10350, 50, 1);
 
-static int snd_ca0106_shared_spdif_get(snd_kcontrol_t * kcontrol,
-                                       snd_ctl_elem_value_t * ucontrol)
+#define snd_ca0106_shared_spdif_info   snd_ctl_boolean_mono_info
+
+static int snd_ca0106_shared_spdif_get(struct snd_kcontrol *kcontrol,
+                                       struct snd_ctl_elem_value *ucontrol)
 {
-       ca0106_t *emu = snd_kcontrol_chip(kcontrol);
+       struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
 
-       ucontrol->value.enumerated.item[0] = emu->spdif_enable;
+       ucontrol->value.integer.value[0] = emu->spdif_enable;
        return 0;
 }
 
-static int snd_ca0106_shared_spdif_put(snd_kcontrol_t * kcontrol,
-                                       snd_ctl_elem_value_t * ucontrol)
+static int snd_ca0106_shared_spdif_put(struct snd_kcontrol *kcontrol,
+                                       struct snd_ctl_elem_value *ucontrol)
 {
-       ca0106_t *emu = snd_kcontrol_chip(kcontrol);
+       struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
        unsigned int val;
        int change = 0;
        u32 mask;
 
-       val = ucontrol->value.enumerated.item[0] ;
+       val = !!ucontrol->value.integer.value[0];
        change = (emu->spdif_enable != val);
        if (change) {
                emu->spdif_enable = val;
-               if (val == 1) {
+               if (val) {
                        /* Digital */
                        snd_ca0106_ptr_write(emu, SPDIF_SELECT1, 0, 0xf);
                        snd_ca0106_ptr_write(emu, SPDIF_SELECT2, 0, 0x0b000000);
@@ -125,18 +124,12 @@ static int snd_ca0106_shared_spdif_put(snd_kcontrol_t * kcontrol,
         return change;
 }
 
-static snd_kcontrol_new_t snd_ca0106_shared_spdif __devinitdata =
-{
-       .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
-       .name =         "SPDIF Out",
-       .info =         snd_ca0106_shared_spdif_info,
-       .get =          snd_ca0106_shared_spdif_get,
-       .put =          snd_ca0106_shared_spdif_put
-};
-
-static int snd_ca0106_capture_source_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
+static int snd_ca0106_capture_source_info(struct snd_kcontrol *kcontrol,
+                                         struct snd_ctl_elem_info *uinfo)
 {
-       static char *texts[6] = { "SPDIF out", "i2s mixer out", "SPDIF in", "i2s in", "AC97 in", "SRC out" };
+       static char *texts[6] = {
+               "IEC958 out", "i2s mixer out", "IEC958 in", "i2s in", "AC97 in", "SRC out"
+       };
 
        uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
        uinfo->count = 1;
@@ -147,25 +140,27 @@ static int snd_ca0106_capture_source_info(snd_kcontrol_t *kcontrol, snd_ctl_elem
        return 0;
 }
 
-static int snd_ca0106_capture_source_get(snd_kcontrol_t * kcontrol,
-                                       snd_ctl_elem_value_t * ucontrol)
+static int snd_ca0106_capture_source_get(struct snd_kcontrol *kcontrol,
+                                       struct snd_ctl_elem_value *ucontrol)
 {
-       ca0106_t *emu = snd_kcontrol_chip(kcontrol);
+       struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
 
        ucontrol->value.enumerated.item[0] = emu->capture_source;
        return 0;
 }
 
-static int snd_ca0106_capture_source_put(snd_kcontrol_t * kcontrol,
-                                       snd_ctl_elem_value_t * ucontrol)
+static int snd_ca0106_capture_source_put(struct snd_kcontrol *kcontrol,
+                                       struct snd_ctl_elem_value *ucontrol)
 {
-       ca0106_t *emu = snd_kcontrol_chip(kcontrol);
+       struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
        unsigned int val;
        int change = 0;
        u32 mask;
        u32 source;
 
        val = ucontrol->value.enumerated.item[0] ;
+       if (val >= 6)
+               return -EINVAL;
        change = (emu->capture_source != val);
        if (change) {
                emu->capture_source = val;
@@ -176,16 +171,80 @@ static int snd_ca0106_capture_source_put(snd_kcontrol_t * kcontrol,
         return change;
 }
 
-static snd_kcontrol_new_t snd_ca0106_capture_source __devinitdata =
+static int snd_ca0106_i2c_capture_source_info(struct snd_kcontrol *kcontrol,
+                                         struct snd_ctl_elem_info *uinfo)
 {
-       .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
-       .name =         "Capture Source",
-       .info =         snd_ca0106_capture_source_info,
-       .get =          snd_ca0106_capture_source_get,
-       .put =          snd_ca0106_capture_source_put
-};
+       static char *texts[6] = {
+               "Phone", "Mic", "Line in", "Aux"
+       };
+
+       uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
+       uinfo->count = 1;
+       uinfo->value.enumerated.items = 4;
+       if (uinfo->value.enumerated.item > 3)
+                uinfo->value.enumerated.item = 3;
+       strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
+       return 0;
+}
+
+static int snd_ca0106_i2c_capture_source_get(struct snd_kcontrol *kcontrol,
+                                       struct snd_ctl_elem_value *ucontrol)
+{
+       struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
+
+       ucontrol->value.enumerated.item[0] = emu->i2c_capture_source;
+       return 0;
+}
+
+static int snd_ca0106_i2c_capture_source_put(struct snd_kcontrol *kcontrol,
+                                       struct snd_ctl_elem_value *ucontrol)
+{
+       struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
+       unsigned int source_id;
+       unsigned int ngain, ogain;
+       int change = 0;
+       u32 source;
+       /* If the capture source has changed,
+        * update the capture volume from the cached value
+        * for the particular source.
+        */
+       source_id = ucontrol->value.enumerated.item[0] ;
+       if (source_id >= 4)
+               return -EINVAL;
+       change = (emu->i2c_capture_source != source_id);
+       if (change) {
+               snd_ca0106_i2c_write(emu, ADC_MUX, 0); /* Mute input */
+               ngain = emu->i2c_capture_volume[source_id][0]; /* Left */
+               ogain = emu->i2c_capture_volume[emu->i2c_capture_source][0]; /* Left */
+               if (ngain != ogain)
+                       snd_ca0106_i2c_write(emu, ADC_ATTEN_ADCL, ((ngain) & 0xff));
+               ngain = emu->i2c_capture_volume[source_id][1]; /* Left */
+               ogain = emu->i2c_capture_volume[emu->i2c_capture_source][1]; /* Left */
+               if (ngain != ogain)
+                       snd_ca0106_i2c_write(emu, ADC_ATTEN_ADCR, ((ngain) & 0xff));
+               source = 1 << source_id;
+               snd_ca0106_i2c_write(emu, ADC_MUX, source); /* Set source */
+               emu->i2c_capture_source = source_id;
+       }
+        return change;
+}
+
+static int snd_ca0106_capture_line_in_side_out_info(struct snd_kcontrol *kcontrol,
+                                              struct snd_ctl_elem_info *uinfo)
+{
+       static char *texts[2] = { "Side out", "Line in" };
+
+       uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
+       uinfo->count = 1;
+       uinfo->value.enumerated.items = 2;
+       if (uinfo->value.enumerated.item > 1)
+                uinfo->value.enumerated.item = 1;
+       strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
+       return 0;
+}
 
-static int snd_ca0106_capture_mic_line_in_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
+static int snd_ca0106_capture_mic_line_in_info(struct snd_kcontrol *kcontrol,
+                                              struct snd_ctl_elem_info *uinfo)
 {
        static char *texts[2] = { "Line in", "Mic in" };
 
@@ -198,63 +257,76 @@ static int snd_ca0106_capture_mic_line_in_info(snd_kcontrol_t *kcontrol, snd_ctl
        return 0;
 }
 
-static int snd_ca0106_capture_mic_line_in_get(snd_kcontrol_t * kcontrol,
-                                       snd_ctl_elem_value_t * ucontrol)
+static int snd_ca0106_capture_mic_line_in_get(struct snd_kcontrol *kcontrol,
+                                       struct snd_ctl_elem_value *ucontrol)
 {
-       ca0106_t *emu = snd_kcontrol_chip(kcontrol);
+       struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
 
        ucontrol->value.enumerated.item[0] = emu->capture_mic_line_in;
        return 0;
 }
 
-static int snd_ca0106_capture_mic_line_in_put(snd_kcontrol_t * kcontrol,
-                                       snd_ctl_elem_value_t * ucontrol)
+static int snd_ca0106_capture_mic_line_in_put(struct snd_kcontrol *kcontrol,
+                                       struct snd_ctl_elem_value *ucontrol)
 {
-       ca0106_t *emu = snd_kcontrol_chip(kcontrol);
+       struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
        unsigned int val;
        int change = 0;
        u32 tmp;
 
        val = ucontrol->value.enumerated.item[0] ;
+       if (val > 1)
+               return -EINVAL;
        change = (emu->capture_mic_line_in != val);
        if (change) {
                emu->capture_mic_line_in = val;
                if (val) {
-                       snd_ca0106_i2c_write(emu, ADC_MUX, ADC_MUX_PHONE); /* Mute input */
+                       //snd_ca0106_i2c_write(emu, ADC_MUX, 0); /* Mute input */
                        tmp = inl(emu->port+GPIO) & ~0x400;
                        tmp = tmp | 0x400;
                        outl(tmp, emu->port+GPIO);
-                       snd_ca0106_i2c_write(emu, ADC_MUX, ADC_MUX_MIC);
+                       //snd_ca0106_i2c_write(emu, ADC_MUX, ADC_MUX_MIC);
                } else {
-                       snd_ca0106_i2c_write(emu, ADC_MUX, ADC_MUX_PHONE); /* Mute input */
+                       //snd_ca0106_i2c_write(emu, ADC_MUX, 0); /* Mute input */
                        tmp = inl(emu->port+GPIO) & ~0x400;
                        outl(tmp, emu->port+GPIO);
-                       snd_ca0106_i2c_write(emu, ADC_MUX, ADC_MUX_LINEIN);
+                       //snd_ca0106_i2c_write(emu, ADC_MUX, ADC_MUX_LINEIN);
                }
        }
         return change;
 }
 
-static snd_kcontrol_new_t snd_ca0106_capture_mic_line_in __devinitdata =
+static struct snd_kcontrol_new snd_ca0106_capture_mic_line_in __devinitdata =
 {
        .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
-       .name =         "Mic/Line in Capture",
+       .name =         "Shared Mic/Line in Capture Switch",
        .info =         snd_ca0106_capture_mic_line_in_info,
        .get =          snd_ca0106_capture_mic_line_in_get,
        .put =          snd_ca0106_capture_mic_line_in_put
 };
 
-static int snd_ca0106_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
+static struct snd_kcontrol_new snd_ca0106_capture_line_in_side_out __devinitdata =
+{
+       .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
+       .name =         "Shared Line in/Side out Capture Switch",
+       .info =         snd_ca0106_capture_line_in_side_out_info,
+       .get =          snd_ca0106_capture_mic_line_in_get,
+       .put =          snd_ca0106_capture_mic_line_in_put
+};
+
+
+static int snd_ca0106_spdif_info(struct snd_kcontrol *kcontrol,
+                                struct snd_ctl_elem_info *uinfo)
 {
        uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
        uinfo->count = 1;
        return 0;
 }
 
-static int snd_ca0106_spdif_get(snd_kcontrol_t * kcontrol,
-                                 snd_ctl_elem_value_t * ucontrol)
+static int snd_ca0106_spdif_get(struct snd_kcontrol *kcontrol,
+                                 struct snd_ctl_elem_value *ucontrol)
 {
-       ca0106_t *emu = snd_kcontrol_chip(kcontrol);
+       struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
        unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
 
        ucontrol->value.iec958.status[0] = (emu->spdif_bits[idx] >> 0) & 0xff;
@@ -264,8 +336,8 @@ static int snd_ca0106_spdif_get(snd_kcontrol_t * kcontrol,
         return 0;
 }
 
-static int snd_ca0106_spdif_get_mask(snd_kcontrol_t * kcontrol,
-                                     snd_ctl_elem_value_t * ucontrol)
+static int snd_ca0106_spdif_get_mask(struct snd_kcontrol *kcontrol,
+                                     struct snd_ctl_elem_value *ucontrol)
 {
        ucontrol->value.iec958.status[0] = 0xff;
        ucontrol->value.iec958.status[1] = 0xff;
@@ -274,10 +346,10 @@ static int snd_ca0106_spdif_get_mask(snd_kcontrol_t * kcontrol,
         return 0;
 }
 
-static int snd_ca0106_spdif_put(snd_kcontrol_t * kcontrol,
-                                 snd_ctl_elem_value_t * ucontrol)
+static int snd_ca0106_spdif_put(struct snd_kcontrol *kcontrol,
+                                 struct snd_ctl_elem_value *ucontrol)
 {
-       ca0106_t *emu = snd_kcontrol_chip(kcontrol);
+       struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
        unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
        int change;
        unsigned int val;
@@ -294,27 +366,8 @@ static int snd_ca0106_spdif_put(snd_kcontrol_t * kcontrol,
         return change;
 }
 
-static snd_kcontrol_new_t snd_ca0106_spdif_mask_control =
-{
-       .access =       SNDRV_CTL_ELEM_ACCESS_READ,
-        .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
-        .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
-       .count =        4,
-        .info =         snd_ca0106_spdif_info,
-        .get =          snd_ca0106_spdif_get_mask
-};
-
-static snd_kcontrol_new_t snd_ca0106_spdif_control =
-{
-        .iface =       SNDRV_CTL_ELEM_IFACE_PCM,
-        .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
-       .count =        4,
-        .info =         snd_ca0106_spdif_info,
-        .get =          snd_ca0106_spdif_get,
-        .put =          snd_ca0106_spdif_put
-};
-
-static int snd_ca0106_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
+static int snd_ca0106_volume_info(struct snd_kcontrol *kcontrol,
+                                 struct snd_ctl_elem_info *uinfo)
 {
         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
         uinfo->count = 2;
@@ -323,11 +376,15 @@ static int snd_ca0106_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t
         return 0;
 }
 
-static int snd_ca0106_volume_get(snd_kcontrol_t * kcontrol,
-                                       snd_ctl_elem_value_t * ucontrol, int reg, int channel_id)
+static int snd_ca0106_volume_get(struct snd_kcontrol *kcontrol,
+                                struct snd_ctl_elem_value *ucontrol)
 {
-        ca0106_t *emu = snd_kcontrol_chip(kcontrol);
+        struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
         unsigned int value;
+       int channel_id, reg;
+
+       channel_id = (kcontrol->private_value >> 8) & 0xff;
+       reg = kcontrol->private_value & 0xff;
 
         value = snd_ca0106_ptr_read(emu, reg, channel_id);
         ucontrol->value.integer.value[0] = 0xff - ((value >> 24) & 0xff); /* Left */
@@ -335,237 +392,246 @@ static int snd_ca0106_volume_get(snd_kcontrol_t * kcontrol,
         return 0;
 }
 
-static int snd_ca0106_volume_get_spdif_front(snd_kcontrol_t * kcontrol,
-                                       snd_ctl_elem_value_t * ucontrol)
+static int snd_ca0106_volume_put(struct snd_kcontrol *kcontrol,
+                                struct snd_ctl_elem_value *ucontrol)
 {
-       int channel_id = CONTROL_FRONT_CHANNEL;
-       int reg = PLAYBACK_VOLUME1;
-        return snd_ca0106_volume_get(kcontrol, ucontrol, reg, channel_id);
-}
+        struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
+        unsigned int oval, nval;
+       int channel_id, reg;
 
-static int snd_ca0106_volume_get_spdif_center_lfe(snd_kcontrol_t * kcontrol,
-                                       snd_ctl_elem_value_t * ucontrol)
-{
-       int channel_id = CONTROL_CENTER_LFE_CHANNEL;
-       int reg = PLAYBACK_VOLUME1;
-        return snd_ca0106_volume_get(kcontrol, ucontrol, reg, channel_id);
-}
-static int snd_ca0106_volume_get_spdif_unknown(snd_kcontrol_t * kcontrol,
-                                       snd_ctl_elem_value_t * ucontrol)
-{
-       int channel_id = CONTROL_UNKNOWN_CHANNEL;
-       int reg = PLAYBACK_VOLUME1;
-        return snd_ca0106_volume_get(kcontrol, ucontrol, reg, channel_id);
-}
-static int snd_ca0106_volume_get_spdif_rear(snd_kcontrol_t * kcontrol,
-                                       snd_ctl_elem_value_t * ucontrol)
-{
-       int channel_id = CONTROL_REAR_CHANNEL;
-       int reg = PLAYBACK_VOLUME1;
-        return snd_ca0106_volume_get(kcontrol, ucontrol, reg, channel_id);
-}
-static int snd_ca0106_volume_get_analog_front(snd_kcontrol_t * kcontrol,
-                                       snd_ctl_elem_value_t * ucontrol)
-{
-       int channel_id = CONTROL_FRONT_CHANNEL;
-       int reg = PLAYBACK_VOLUME2;
-        return snd_ca0106_volume_get(kcontrol, ucontrol, reg, channel_id);
-}
+       channel_id = (kcontrol->private_value >> 8) & 0xff;
+       reg = kcontrol->private_value & 0xff;
 
-static int snd_ca0106_volume_get_analog_center_lfe(snd_kcontrol_t * kcontrol,
-                                       snd_ctl_elem_value_t * ucontrol)
-{
-       int channel_id = CONTROL_CENTER_LFE_CHANNEL;
-       int reg = PLAYBACK_VOLUME2;
-        return snd_ca0106_volume_get(kcontrol, ucontrol, reg, channel_id);
-}
-static int snd_ca0106_volume_get_analog_unknown(snd_kcontrol_t * kcontrol,
-                                       snd_ctl_elem_value_t * ucontrol)
-{
-       int channel_id = CONTROL_UNKNOWN_CHANNEL;
-       int reg = PLAYBACK_VOLUME2;
-        return snd_ca0106_volume_get(kcontrol, ucontrol, reg, channel_id);
-}
-static int snd_ca0106_volume_get_analog_rear(snd_kcontrol_t * kcontrol,
-                                       snd_ctl_elem_value_t * ucontrol)
-{
-       int channel_id = CONTROL_REAR_CHANNEL;
-       int reg = PLAYBACK_VOLUME2;
-        return snd_ca0106_volume_get(kcontrol, ucontrol, reg, channel_id);
+       oval = snd_ca0106_ptr_read(emu, reg, channel_id);
+       nval = ((0xff - ucontrol->value.integer.value[0]) << 24) |
+               ((0xff - ucontrol->value.integer.value[1]) << 16);
+        nval |= ((0xff - ucontrol->value.integer.value[0]) << 8) |
+               ((0xff - ucontrol->value.integer.value[1]) );
+       if (oval == nval)
+               return 0;
+       snd_ca0106_ptr_write(emu, reg, channel_id, nval);
+       return 1;
 }
 
-static int snd_ca0106_volume_get_feedback(snd_kcontrol_t * kcontrol,
-                                       snd_ctl_elem_value_t * ucontrol)
-{
-       int channel_id = 1;
-       int reg = CAPTURE_CONTROL;
-        return snd_ca0106_volume_get(kcontrol, ucontrol, reg, channel_id);
-}
-                                                                                                                           
-static int snd_ca0106_volume_put(snd_kcontrol_t * kcontrol,
-                                       snd_ctl_elem_value_t * ucontrol, int reg, int channel_id)
-{
-        ca0106_t *emu = snd_kcontrol_chip(kcontrol);
-        unsigned int value;
-        //value = snd_ca0106_ptr_read(emu, reg, channel_id);
-        //value = value & 0xffff;
-        value = ((0xff - ucontrol->value.integer.value[0]) << 24) | ((0xff - ucontrol->value.integer.value[1]) << 16);
-        value = value | ((0xff - ucontrol->value.integer.value[0]) << 8) | ((0xff - ucontrol->value.integer.value[1]) );
-        snd_ca0106_ptr_write(emu, reg, channel_id, value);
-        return 1;
-}
-static int snd_ca0106_volume_put_spdif_front(snd_kcontrol_t * kcontrol,
-                                       snd_ctl_elem_value_t * ucontrol)
-{
-       int channel_id = CONTROL_FRONT_CHANNEL;
-       int reg = PLAYBACK_VOLUME1;
-        return snd_ca0106_volume_put(kcontrol, ucontrol, reg, channel_id);
-}
-static int snd_ca0106_volume_put_spdif_center_lfe(snd_kcontrol_t * kcontrol,
-                                       snd_ctl_elem_value_t * ucontrol)
-{
-       int channel_id = CONTROL_CENTER_LFE_CHANNEL;
-       int reg = PLAYBACK_VOLUME1;
-        return snd_ca0106_volume_put(kcontrol, ucontrol, reg, channel_id);
-}
-static int snd_ca0106_volume_put_spdif_unknown(snd_kcontrol_t * kcontrol,
-                                       snd_ctl_elem_value_t * ucontrol)
-{
-       int channel_id = CONTROL_UNKNOWN_CHANNEL;
-       int reg = PLAYBACK_VOLUME1;
-        return snd_ca0106_volume_put(kcontrol, ucontrol, reg, channel_id);
-}
-static int snd_ca0106_volume_put_spdif_rear(snd_kcontrol_t * kcontrol,
-                                       snd_ctl_elem_value_t * ucontrol)
-{
-       int channel_id = CONTROL_REAR_CHANNEL;
-       int reg = PLAYBACK_VOLUME1;
-        return snd_ca0106_volume_put(kcontrol, ucontrol, reg, channel_id);
-}
-static int snd_ca0106_volume_put_analog_front(snd_kcontrol_t * kcontrol,
-                                       snd_ctl_elem_value_t * ucontrol)
-{
-       int channel_id = CONTROL_FRONT_CHANNEL;
-       int reg = PLAYBACK_VOLUME2;
-        return snd_ca0106_volume_put(kcontrol, ucontrol, reg, channel_id);
-}
-static int snd_ca0106_volume_put_analog_center_lfe(snd_kcontrol_t * kcontrol,
-                                       snd_ctl_elem_value_t * ucontrol)
+static int snd_ca0106_i2c_volume_info(struct snd_kcontrol *kcontrol,
+                                 struct snd_ctl_elem_info *uinfo)
 {
-       int channel_id = CONTROL_CENTER_LFE_CHANNEL;
-       int reg = PLAYBACK_VOLUME2;
-        return snd_ca0106_volume_put(kcontrol, ucontrol, reg, channel_id);
+        uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
+        uinfo->count = 2;
+        uinfo->value.integer.min = 0;
+        uinfo->value.integer.max = 255;
+        return 0;
 }
-static int snd_ca0106_volume_put_analog_unknown(snd_kcontrol_t * kcontrol,
-                                       snd_ctl_elem_value_t * ucontrol)
+
+static int snd_ca0106_i2c_volume_get(struct snd_kcontrol *kcontrol,
+                                struct snd_ctl_elem_value *ucontrol)
 {
-       int channel_id = CONTROL_UNKNOWN_CHANNEL;
-       int reg = PLAYBACK_VOLUME2;
-        return snd_ca0106_volume_put(kcontrol, ucontrol, reg, channel_id);
+        struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
+       int source_id;
+
+       source_id = kcontrol->private_value;
+
+        ucontrol->value.integer.value[0] = emu->i2c_capture_volume[source_id][0];
+        ucontrol->value.integer.value[1] = emu->i2c_capture_volume[source_id][1];
+        return 0;
 }
-static int snd_ca0106_volume_put_analog_rear(snd_kcontrol_t * kcontrol,
-                                       snd_ctl_elem_value_t * ucontrol)
+
+static int snd_ca0106_i2c_volume_put(struct snd_kcontrol *kcontrol,
+                                struct snd_ctl_elem_value *ucontrol)
 {
-       int channel_id = CONTROL_REAR_CHANNEL;
-       int reg = PLAYBACK_VOLUME2;
-        return snd_ca0106_volume_put(kcontrol, ucontrol, reg, channel_id);
+        struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
+        unsigned int ogain;
+        unsigned int ngain;
+       int source_id;
+       int change = 0;
+
+       source_id = kcontrol->private_value;
+       ogain = emu->i2c_capture_volume[source_id][0]; /* Left */
+       ngain = ucontrol->value.integer.value[0];
+       if (ngain > 0xff)
+               return -EINVAL;
+       if (ogain != ngain) {
+               if (emu->i2c_capture_source == source_id)
+                       snd_ca0106_i2c_write(emu, ADC_ATTEN_ADCL, ((ngain) & 0xff) );
+               emu->i2c_capture_volume[source_id][0] = ucontrol->value.integer.value[0];
+               change = 1;
+       }
+       ogain = emu->i2c_capture_volume[source_id][1]; /* Right */
+       ngain = ucontrol->value.integer.value[1];
+       if (ngain > 0xff)
+               return -EINVAL;
+       if (ogain != ngain) {
+               if (emu->i2c_capture_source == source_id)
+                       snd_ca0106_i2c_write(emu, ADC_ATTEN_ADCR, ((ngain) & 0xff));
+               emu->i2c_capture_volume[source_id][1] = ucontrol->value.integer.value[1];
+               change = 1;
+       }
+
+       return change;
 }
 
-static int snd_ca0106_volume_put_feedback(snd_kcontrol_t * kcontrol,
-                                       snd_ctl_elem_value_t * ucontrol)
+#define spi_mute_info  snd_ctl_boolean_mono_info
+
+static int spi_mute_get(struct snd_kcontrol *kcontrol,
+                       struct snd_ctl_elem_value *ucontrol)
 {
-       int channel_id = 1;
-       int reg = CAPTURE_CONTROL;
-        return snd_ca0106_volume_put(kcontrol, ucontrol, reg, channel_id);
+       struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
+       unsigned int reg = kcontrol->private_value >> SPI_REG_SHIFT;
+       unsigned int bit = kcontrol->private_value & SPI_REG_MASK;
+
+       ucontrol->value.integer.value[0] = !(emu->spi_dac_reg[reg] & bit);
+       return 0;
 }
 
-static snd_kcontrol_new_t snd_ca0106_volume_control_analog_front =
-{
-        .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
-        .name =         "Analog Front Volume",
-        .info =         snd_ca0106_volume_info,
-        .get =          snd_ca0106_volume_get_analog_front,
-        .put =          snd_ca0106_volume_put_analog_front
-};
-static snd_kcontrol_new_t snd_ca0106_volume_control_analog_center_lfe =
-{
-        .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
-        .name =         "Analog Center/LFE Volume",
-        .info =         snd_ca0106_volume_info,
-        .get =          snd_ca0106_volume_get_analog_center_lfe,
-        .put =          snd_ca0106_volume_put_analog_center_lfe
-};
-static snd_kcontrol_new_t snd_ca0106_volume_control_analog_unknown =
+static int spi_mute_put(struct snd_kcontrol *kcontrol,
+                       struct snd_ctl_elem_value *ucontrol)
 {
-        .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
-        .name =         "Analog Side Volume",
-        .info =         snd_ca0106_volume_info,
-        .get =          snd_ca0106_volume_get_analog_unknown,
-        .put =          snd_ca0106_volume_put_analog_unknown
-};
-static snd_kcontrol_new_t snd_ca0106_volume_control_analog_rear =
-{
-        .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
-        .name =         "Analog Rear Volume",
-        .info =         snd_ca0106_volume_info,
-        .get =          snd_ca0106_volume_get_analog_rear,
-        .put =          snd_ca0106_volume_put_analog_rear
-};
-static snd_kcontrol_new_t snd_ca0106_volume_control_spdif_front =
-{
-        .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
-        .name =         "SPDIF Front Volume",
-        .info =         snd_ca0106_volume_info,
-        .get =          snd_ca0106_volume_get_spdif_front,
-        .put =          snd_ca0106_volume_put_spdif_front
-};
-static snd_kcontrol_new_t snd_ca0106_volume_control_spdif_center_lfe =
-{
-        .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
-        .name =         "SPDIF Center/LFE Volume",
-        .info =         snd_ca0106_volume_info,
-        .get =          snd_ca0106_volume_get_spdif_center_lfe,
-        .put =          snd_ca0106_volume_put_spdif_center_lfe
-};
-static snd_kcontrol_new_t snd_ca0106_volume_control_spdif_unknown =
-{
-        .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
-        .name =         "SPDIF Unknown Volume",
-        .info =         snd_ca0106_volume_info,
-        .get =          snd_ca0106_volume_get_spdif_unknown,
-        .put =          snd_ca0106_volume_put_spdif_unknown
-};
-static snd_kcontrol_new_t snd_ca0106_volume_control_spdif_rear =
-{
-        .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
-        .name =         "SPDIF Rear Volume",
-        .info =         snd_ca0106_volume_info,
-        .get =          snd_ca0106_volume_get_spdif_rear,
-        .put =          snd_ca0106_volume_put_spdif_rear
+       struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
+       unsigned int reg = kcontrol->private_value >> SPI_REG_SHIFT;
+       unsigned int bit = kcontrol->private_value & SPI_REG_MASK;
+       int ret;
+
+       ret = emu->spi_dac_reg[reg] & bit;
+       if (ucontrol->value.integer.value[0]) {
+               if (!ret)       /* bit already cleared, do nothing */
+                       return 0;
+               emu->spi_dac_reg[reg] &= ~bit;
+       } else {
+               if (ret)        /* bit already set, do nothing */
+                       return 0;
+               emu->spi_dac_reg[reg] |= bit;
+       }
+
+       ret = snd_ca0106_spi_write(emu, emu->spi_dac_reg[reg]);
+       return ret ? -EINVAL : 1;
+}
+
+#define CA_VOLUME(xname,chid,reg) \
+{                                                              \
+       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname,     \
+       .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |             \
+                 SNDRV_CTL_ELEM_ACCESS_TLV_READ,               \
+       .info =  snd_ca0106_volume_info,                        \
+       .get =   snd_ca0106_volume_get,                         \
+       .put =   snd_ca0106_volume_put,                         \
+       .tlv = { .p = snd_ca0106_db_scale1 },                   \
+       .private_value = ((chid) << 8) | (reg)                  \
+}
+
+static struct snd_kcontrol_new snd_ca0106_volume_ctls[] __devinitdata = {
+       CA_VOLUME("Analog Front Playback Volume",
+                 CONTROL_FRONT_CHANNEL, PLAYBACK_VOLUME2),
+        CA_VOLUME("Analog Rear Playback Volume",
+                 CONTROL_REAR_CHANNEL, PLAYBACK_VOLUME2),
+       CA_VOLUME("Analog Center/LFE Playback Volume",
+                 CONTROL_CENTER_LFE_CHANNEL, PLAYBACK_VOLUME2),
+        CA_VOLUME("Analog Side Playback Volume",
+                 CONTROL_UNKNOWN_CHANNEL, PLAYBACK_VOLUME2),
+
+        CA_VOLUME("IEC958 Front Playback Volume",
+                 CONTROL_FRONT_CHANNEL, PLAYBACK_VOLUME1),
+       CA_VOLUME("IEC958 Rear Playback Volume",
+                 CONTROL_REAR_CHANNEL, PLAYBACK_VOLUME1),
+       CA_VOLUME("IEC958 Center/LFE Playback Volume",
+                 CONTROL_CENTER_LFE_CHANNEL, PLAYBACK_VOLUME1),
+       CA_VOLUME("IEC958 Unknown Playback Volume",
+                 CONTROL_UNKNOWN_CHANNEL, PLAYBACK_VOLUME1),
+
+        CA_VOLUME("CAPTURE feedback Playback Volume",
+                 1, CAPTURE_CONTROL),
+
+       {
+               .access =       SNDRV_CTL_ELEM_ACCESS_READ,
+               .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
+               .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
+               .count =        4,
+               .info =         snd_ca0106_spdif_info,
+               .get =          snd_ca0106_spdif_get_mask
+       },
+       {
+               .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
+               .name =         "IEC958 Playback Switch",
+               .info =         snd_ca0106_shared_spdif_info,
+               .get =          snd_ca0106_shared_spdif_get,
+               .put =          snd_ca0106_shared_spdif_put
+       },
+       {
+               .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
+               .name =         "Digital Source Capture Enum",
+               .info =         snd_ca0106_capture_source_info,
+               .get =          snd_ca0106_capture_source_get,
+               .put =          snd_ca0106_capture_source_put
+       },
+       {
+               .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
+               .name =         "Analog Source Capture Enum",
+               .info =         snd_ca0106_i2c_capture_source_info,
+               .get =          snd_ca0106_i2c_capture_source_get,
+               .put =          snd_ca0106_i2c_capture_source_put
+       },
+       {
+               .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
+               .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
+               .count =        4,
+               .info =         snd_ca0106_spdif_info,
+               .get =          snd_ca0106_spdif_get,
+               .put =          snd_ca0106_spdif_put
+       },
 };
 
-static snd_kcontrol_new_t snd_ca0106_volume_control_feedback =
-{
-        .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
-        .name =         "CAPTURE feedback into PLAYBACK",
-        .info =         snd_ca0106_volume_info,
-        .get =          snd_ca0106_volume_get_feedback,
-        .put =          snd_ca0106_volume_put_feedback
+#define I2C_VOLUME(xname,chid) \
+{                                                              \
+       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname,     \
+       .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |             \
+                 SNDRV_CTL_ELEM_ACCESS_TLV_READ,               \
+       .info =  snd_ca0106_i2c_volume_info,                    \
+       .get =   snd_ca0106_i2c_volume_get,                     \
+       .put =   snd_ca0106_i2c_volume_put,                     \
+       .tlv = { .p = snd_ca0106_db_scale2 },                   \
+       .private_value = chid                                   \
+}
+
+static struct snd_kcontrol_new snd_ca0106_volume_i2c_adc_ctls[] __devinitdata = {
+        I2C_VOLUME("Phone Capture Volume", 0),
+        I2C_VOLUME("Mic Capture Volume", 1),
+        I2C_VOLUME("Line in Capture Volume", 2),
+        I2C_VOLUME("Aux Capture Volume", 3),
 };
 
+#define SPI_SWITCH(xname,reg,bit) \
+{                                                              \
+       .iface  = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname,    \
+       .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,              \
+       .info   = spi_mute_info,                                \
+       .get    = spi_mute_get,                                 \
+       .put    = spi_mute_put,                                 \
+       .private_value = (reg<<SPI_REG_SHIFT) | (bit)           \
+}
+
+static struct snd_kcontrol_new snd_ca0106_volume_spi_dac_ctls[]
+__devinitdata = {
+       SPI_SWITCH("Analog Front Playback Switch",
+                  SPI_DMUTE4_REG, SPI_DMUTE4_BIT),
+       SPI_SWITCH("Analog Rear Playback Switch",
+                  SPI_DMUTE0_REG, SPI_DMUTE0_BIT),
+       SPI_SWITCH("Analog Center/LFE Playback Switch",
+                  SPI_DMUTE2_REG, SPI_DMUTE2_BIT),
+       SPI_SWITCH("Analog Side Playback Switch",
+                  SPI_DMUTE1_REG, SPI_DMUTE1_BIT),
+};
 
-static int remove_ctl(snd_card_t *card, const char *name)
+static int __devinit remove_ctl(struct snd_card *card, const char *name)
 {
-       snd_ctl_elem_id_t id;
+       struct snd_ctl_elem_id id;
        memset(&id, 0, sizeof(id));
        strcpy(id.name, name);
        id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
        return snd_ctl_remove_id(card, &id);
 }
 
-static snd_kcontrol_t *ctl_find(snd_card_t *card, const char *name)
+static struct snd_kcontrol __devinit *ctl_find(struct snd_card *card, const char *name)
 {
-       snd_ctl_elem_id_t sid;
+       struct snd_ctl_elem_id sid;
        memset(&sid, 0, sizeof(sid));
        /* FIXME: strcpy is bad. */
        strcpy(sid.name, name);
@@ -573,9 +639,9 @@ static snd_kcontrol_t *ctl_find(snd_card_t *card, const char *name)
        return snd_ctl_find_id(card, &sid);
 }
 
-static int rename_ctl(snd_card_t *card, const char *src, const char *dst)
+static int __devinit rename_ctl(struct snd_card *card, const char *src, const char *dst)
 {
-       snd_kcontrol_t *kctl = ctl_find(card, src);
+       struct snd_kcontrol *kctl = ctl_find(card, src);
        if (kctl) {
                strcpy(kctl->id.name, dst);
                return 0;
@@ -583,11 +649,20 @@ static int rename_ctl(snd_card_t *card, const char *src, const char *dst)
        return -ENOENT;
 }
 
-int __devinit snd_ca0106_mixer(ca0106_t *emu)
+#define ADD_CTLS(emu, ctls)                                            \
+       do {                                                            \
+               int i, err;                                             \
+               for (i = 0; i < ARRAY_SIZE(ctls); i++) {                \
+                       err = snd_ctl_add(card, snd_ctl_new1(&ctls[i], emu)); \
+                       if (err < 0)                                    \
+                               return err;                             \
+               }                                                       \
+       } while (0)
+
+int __devinit snd_ca0106_mixer(struct snd_ca0106 *emu)
 {
-        int err;
-        snd_kcontrol_t *kctl;
-        snd_card_t *card = emu->card;
+       int err;
+        struct snd_card *card = emu->card;
        char **c;
        static char *ca0106_remove_ctls[] = {
                "Master Mono Playback Switch",
@@ -627,70 +702,24 @@ int __devinit snd_ca0106_mixer(ca0106_t *emu)
                NULL
        };
 #if 1
-       for (c=ca0106_remove_ctls; *c; c++)
+       for (c = ca0106_remove_ctls; *c; c++)
                remove_ctl(card, *c);
-       for (c=ca0106_rename_ctls; *c; c += 2)
+       for (c = ca0106_rename_ctls; *c; c += 2)
                rename_ctl(card, c[0], c[1]);
 #endif
 
-        if ((kctl = snd_ctl_new1(&snd_ca0106_volume_control_analog_front, emu)) == NULL)
-                return -ENOMEM;
-        if ((err = snd_ctl_add(card, kctl)))
-                return err;
-        if ((kctl = snd_ctl_new1(&snd_ca0106_volume_control_analog_rear, emu)) == NULL)
-                return -ENOMEM;
-        if ((err = snd_ctl_add(card, kctl)))
-                return err;
-        if ((kctl = snd_ctl_new1(&snd_ca0106_volume_control_analog_center_lfe, emu)) == NULL)
-                return -ENOMEM;
-        if ((err = snd_ctl_add(card, kctl)))
-                return err;
-        if ((kctl = snd_ctl_new1(&snd_ca0106_volume_control_analog_unknown, emu)) == NULL)
-                return -ENOMEM;
-        if ((err = snd_ctl_add(card, kctl)))
-                return err;
-        if ((kctl = snd_ctl_new1(&snd_ca0106_volume_control_spdif_front, emu)) == NULL)
-                return -ENOMEM;
-        if ((err = snd_ctl_add(card, kctl)))
-                return err;
-        if ((kctl = snd_ctl_new1(&snd_ca0106_volume_control_spdif_rear, emu)) == NULL)
-                return -ENOMEM;
-        if ((err = snd_ctl_add(card, kctl)))
-                return err;
-        if ((kctl = snd_ctl_new1(&snd_ca0106_volume_control_spdif_center_lfe, emu)) == NULL)
-                return -ENOMEM;
-        if ((err = snd_ctl_add(card, kctl)))
-                return err;
-        if ((kctl = snd_ctl_new1(&snd_ca0106_volume_control_spdif_unknown, emu)) == NULL)
-                return -ENOMEM;
-        if ((err = snd_ctl_add(card, kctl)))
-                return err;
-        if ((kctl = snd_ctl_new1(&snd_ca0106_volume_control_feedback, emu)) == NULL)
-                return -ENOMEM;
-        if ((err = snd_ctl_add(card, kctl)))
-                return err;
-       if ((kctl = snd_ctl_new1(&snd_ca0106_spdif_mask_control, emu)) == NULL)
-               return -ENOMEM;
-       if ((err = snd_ctl_add(card, kctl)))
-               return err;
-       if ((kctl = snd_ctl_new1(&snd_ca0106_shared_spdif, emu)) == NULL)
-               return -ENOMEM;
-       if ((err = snd_ctl_add(card, kctl)))
-               return err;
-       if ((kctl = snd_ctl_new1(&snd_ca0106_capture_source, emu)) == NULL)
-               return -ENOMEM;
-       if ((err = snd_ctl_add(card, kctl)))
-               return err;
+       ADD_CTLS(emu, snd_ca0106_volume_ctls);
        if (emu->details->i2c_adc == 1) {
-               if ((kctl = snd_ctl_new1(&snd_ca0106_capture_mic_line_in, emu)) == NULL)
-                       return -ENOMEM;
-               if ((err = snd_ctl_add(card, kctl)))
+               ADD_CTLS(emu, snd_ca0106_volume_i2c_adc_ctls);
+               if (emu->details->gpio_type == 1)
+                       err = snd_ctl_add(card, snd_ctl_new1(&snd_ca0106_capture_mic_line_in, emu));
+               else  /* gpio_type == 2 */
+                       err = snd_ctl_add(card, snd_ctl_new1(&snd_ca0106_capture_line_in_side_out, emu));
+               if (err < 0)
                        return err;
        }
-       if ((kctl = snd_ctl_new1(&snd_ca0106_spdif_control, emu)) == NULL)
-               return -ENOMEM;
-       if ((err = snd_ctl_add(card, kctl)))
-               return err;
+       if (emu->details->spi_dac == 1)
+               ADD_CTLS(emu, snd_ca0106_volume_spi_dac_ctls);
         return 0;
 }