fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / sound / i2c / other / ak4xxx-adda.c
index dc7cc20..5da49e2 100644 (file)
 #include <linux/init.h>
 #include <sound/core.h>
 #include <sound/control.h>
+#include <sound/tlv.h>
 #include <sound/ak4xxx-adda.h>
 
 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Takashi Iwai <tiwai@suse.de>");
 MODULE_DESCRIPTION("Routines for control of AK452x / AK43xx  AD/DA converters");
 MODULE_LICENSE("GPL");
 
+/* write the given register and save the data to the cache */
 void snd_akm4xxx_write(struct snd_akm4xxx *ak, int chip, unsigned char reg,
                       unsigned char val)
 {
@@ -41,15 +43,7 @@ void snd_akm4xxx_write(struct snd_akm4xxx *ak, int chip, unsigned char reg,
        ak->ops.write(ak, chip, reg, val);
 
        /* save the data */
-       if (ak->type == SND_AK4524 || ak->type == SND_AK4528) {
-               if ((reg != 0x04 && reg != 0x05) || (val & 0x80) == 0)
-                       snd_akm4xxx_set(ak, chip, reg, val);
-               else
-                       snd_akm4xxx_set_ipga(ak, chip, reg, val);
-       } else {
-               /* AK4529, or else */
-               snd_akm4xxx_set(ak, chip, reg, val);
-       }
+       snd_akm4xxx_set(ak, chip, reg, val);
        ak->ops.unlock(ak, chip);
 }
 
@@ -73,12 +67,6 @@ static void ak4524_reset(struct snd_akm4xxx *ak, int state)
                for (reg = 0x04; reg < maxreg; reg++)
                        snd_akm4xxx_write(ak, chip, reg,
                                          snd_akm4xxx_get(ak, chip, reg));
-               if (ak->type == SND_AK4528)
-                       continue;
-               /* IPGA */
-               for (reg = 0x04; reg < 0x06; reg++)
-                       snd_akm4xxx_write(ak, chip, reg,
-                                         snd_akm4xxx_get_ipga(ak, chip, reg));
        }
 }
 
@@ -137,11 +125,48 @@ void snd_akm4xxx_reset(struct snd_akm4xxx *ak, int state)
        case SND_AK4381:
                ak4381_reset(ak, state);
                break;
+       default:
+               break;
        }
 }
 
 EXPORT_SYMBOL(snd_akm4xxx_reset);
 
+
+/*
+ * Volume conversion table for non-linear volumes
+ * from -63.5dB (mute) to 0dB step 0.5dB
+ *
+ * Used for AK4524 input/ouput attenuation, AK4528, and
+ * AK5365 input attenuation
+ */
+static unsigned char vol_cvt_datt[128] = {
+       0x00, 0x01, 0x01, 0x02, 0x02, 0x03, 0x03, 0x04,
+       0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x06, 0x06,
+       0x06, 0x07, 0x07, 0x08, 0x08, 0x08, 0x09, 0x0a,
+       0x0a, 0x0b, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x0f,
+       0x10, 0x10, 0x11, 0x12, 0x12, 0x13, 0x13, 0x14,
+       0x15, 0x16, 0x17, 0x17, 0x18, 0x19, 0x1a, 0x1c,
+       0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x23,
+       0x24, 0x25, 0x26, 0x28, 0x29, 0x2a, 0x2b, 0x2d,
+       0x2e, 0x30, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
+       0x37, 0x38, 0x39, 0x3b, 0x3c, 0x3e, 0x3f, 0x40,
+       0x41, 0x42, 0x43, 0x44, 0x46, 0x47, 0x48, 0x4a,
+       0x4b, 0x4d, 0x4e, 0x50, 0x51, 0x52, 0x53, 0x54,
+       0x55, 0x56, 0x58, 0x59, 0x5b, 0x5c, 0x5e, 0x5f,
+       0x60, 0x61, 0x62, 0x64, 0x65, 0x66, 0x67, 0x69,
+       0x6a, 0x6c, 0x6d, 0x6f, 0x70, 0x71, 0x72, 0x73,
+       0x75, 0x76, 0x77, 0x79, 0x7a, 0x7c, 0x7d, 0x7f,
+};
+
+/*
+ * dB tables
+ */
+static DECLARE_TLV_DB_SCALE(db_scale_vol_datt, -6350, 50, 1);
+static DECLARE_TLV_DB_SCALE(db_scale_8bit, -12750, 50, 1);
+static DECLARE_TLV_DB_SCALE(db_scale_7bit, -6350, 50, 1);
+static DECLARE_TLV_DB_LINEAR(db_scale_linear, TLV_DB_GAIN_MUTE, 0);
+
 /*
  * initialize all the ak4xxx chips
  */
@@ -155,8 +180,6 @@ void snd_akm4xxx_init(struct snd_akm4xxx *ak)
                0x01, 0x03, /* 1: ADC/DAC enable */
                0x04, 0x00, /* 4: ADC left muted */
                0x05, 0x00, /* 5: ADC right muted */
-               0x04, 0x80, /* 4: ADC IPGA gain 0dB */
-               0x05, 0x80, /* 5: ADC IPGA gain 0dB */
                0x06, 0x00, /* 6: DAC left muted */
                0x07, 0x00, /* 7: DAC right muted */
                0xff, 0xff
@@ -238,6 +261,9 @@ void snd_akm4xxx_init(struct snd_akm4xxx *ak)
        int chip, num_chips;
        unsigned char *ptr, reg, data, *inits;
 
+       memset(ak->images, 0, sizeof(ak->images));
+       memset(ak->volumes, 0, sizeof(ak->volumes));
+
        switch (ak->type) {
        case SND_AK4524:
                inits = inits_ak4524;
@@ -263,6 +289,9 @@ void snd_akm4xxx_init(struct snd_akm4xxx *ak)
                inits = inits_ak4381;
                num_chips = ak->num_dacs / 2;
                break;
+       case SND_AK5365:
+               /* FIXME: any init sequence? */
+               return;
        default:
                snd_BUG();
                return;
@@ -280,14 +309,23 @@ void snd_akm4xxx_init(struct snd_akm4xxx *ak)
 
 EXPORT_SYMBOL(snd_akm4xxx_init);
 
+/*
+ * Mixer callbacks
+ */
+#define AK_IPGA                        (1<<20) /* including IPGA */
+#define AK_VOL_CVT                     (1<<21) /* need dB conversion */
+#define AK_NEEDSMSB                    (1<<22) /* need MSB update bit */
+#define AK_INVERT                      (1<<23) /* data is inverted */
 #define AK_GET_CHIP(val)               (((val) >> 8) & 0xff)
 #define AK_GET_ADDR(val)               ((val) & 0xff)
-#define AK_GET_SHIFT(val)              (((val) >> 16) & 0x7f)
+#define AK_GET_SHIFT(val)              (((val) >> 16) & 0x0f)
+#define AK_GET_VOL_CVT(val)            (((val) >> 21) & 1)
+#define AK_GET_IPGA(val)               (((val) >> 20) & 1)
+#define AK_GET_NEEDSMSB(val)           (((val) >> 22) & 1)
 #define AK_GET_INVERT(val)             (((val) >> 23) & 1)
 #define AK_GET_MASK(val)               (((val) >> 24) & 0xff)
 #define AK_COMPOSE(chip,addr,shift,mask) \
        (((chip) << 8) | (addr) | ((shift) << 16) | ((mask) << 24))
-#define AK_INVERT                      (1<<23)
 
 static int snd_akm4xxx_volume_info(struct snd_kcontrol *kcontrol,
                                   struct snd_ctl_elem_info *uinfo)
@@ -307,31 +345,39 @@ static int snd_akm4xxx_volume_get(struct snd_kcontrol *kcontrol,
        struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
        int chip = AK_GET_CHIP(kcontrol->private_value);
        int addr = AK_GET_ADDR(kcontrol->private_value);
-       int invert = AK_GET_INVERT(kcontrol->private_value);
-       unsigned int mask = AK_GET_MASK(kcontrol->private_value);
-       unsigned char val = snd_akm4xxx_get(ak, chip, addr);
-       
-       ucontrol->value.integer.value[0] = invert ? mask - val : val;
+
+       ucontrol->value.integer.value[0] = snd_akm4xxx_get_vol(ak, chip, addr);
        return 0;
 }
 
-static int snd_akm4xxx_volume_put(struct snd_kcontrol *kcontrol,
-                                 struct snd_ctl_elem_value *ucontrol)
+static int put_ak_reg(struct snd_kcontrol *kcontrol, int addr,
+                     unsigned char nval)
 {
        struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
-       int chip = AK_GET_CHIP(kcontrol->private_value);
-       int addr = AK_GET_ADDR(kcontrol->private_value);
-       int invert = AK_GET_INVERT(kcontrol->private_value);
        unsigned int mask = AK_GET_MASK(kcontrol->private_value);
-       unsigned char nval = ucontrol->value.integer.value[0] % (mask+1);
-       int change;
+       int chip = AK_GET_CHIP(kcontrol->private_value);
 
-       if (invert)
+       if (snd_akm4xxx_get_vol(ak, chip, addr) == nval)
+               return 0;
+
+       snd_akm4xxx_set_vol(ak, chip, addr, nval);
+       if (AK_GET_VOL_CVT(kcontrol->private_value) && nval < 128)
+               nval = vol_cvt_datt[nval];
+       if (AK_GET_IPGA(kcontrol->private_value) && nval >= 128)
+               nval++; /* need to correct + 1 since both 127 and 128 are 0dB */
+       if (AK_GET_INVERT(kcontrol->private_value))
                nval = mask - nval;
-       change = snd_akm4xxx_get(ak, chip, addr) != nval;
-       if (change)
-               snd_akm4xxx_write(ak, chip, addr, nval);
-       return change;
+       if (AK_GET_NEEDSMSB(kcontrol->private_value))
+               nval |= 0x80;
+       snd_akm4xxx_write(ak, chip, addr, nval);
+       return 1;
+}
+
+static int snd_akm4xxx_volume_put(struct snd_kcontrol *kcontrol,
+                                 struct snd_ctl_elem_value *ucontrol)
+{
+       return put_ak_reg(kcontrol, AK_GET_ADDR(kcontrol->private_value),
+                         ucontrol->value.integer.value[0]);
 }
 
 static int snd_akm4xxx_stereo_volume_info(struct snd_kcontrol *kcontrol,
@@ -352,77 +398,21 @@ static int snd_akm4xxx_stereo_volume_get(struct snd_kcontrol *kcontrol,
        struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
        int chip = AK_GET_CHIP(kcontrol->private_value);
        int addr = AK_GET_ADDR(kcontrol->private_value);
-       int invert = AK_GET_INVERT(kcontrol->private_value);
-       unsigned int mask = AK_GET_MASK(kcontrol->private_value);
-       unsigned char val = snd_akm4xxx_get(ak, chip, addr);
-       
-       ucontrol->value.integer.value[0] = invert ? mask - val : val;
-
-       val = snd_akm4xxx_get(ak, chip, addr+1);
-       ucontrol->value.integer.value[1] = invert ? mask - val : val;
 
+       ucontrol->value.integer.value[0] = snd_akm4xxx_get_vol(ak, chip, addr);
+       ucontrol->value.integer.value[1] = snd_akm4xxx_get_vol(ak, chip, addr+1);
        return 0;
 }
 
 static int snd_akm4xxx_stereo_volume_put(struct snd_kcontrol *kcontrol,
                                         struct snd_ctl_elem_value *ucontrol)
 {
-       struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
-       int chip = AK_GET_CHIP(kcontrol->private_value);
-       int addr = AK_GET_ADDR(kcontrol->private_value);
-       int invert = AK_GET_INVERT(kcontrol->private_value);
-       unsigned int mask = AK_GET_MASK(kcontrol->private_value);
-       unsigned char nval = ucontrol->value.integer.value[0] % (mask+1);
-       int change0, change1;
-
-       if (invert)
-               nval = mask - nval;
-       change0 = snd_akm4xxx_get(ak, chip, addr) != nval;
-       if (change0)
-               snd_akm4xxx_write(ak, chip, addr, nval);
-
-       nval = ucontrol->value.integer.value[1] % (mask+1);
-       if (invert)
-               nval = mask - nval;
-       change1 = snd_akm4xxx_get(ak, chip, addr+1) != nval;
-       if (change1)
-               snd_akm4xxx_write(ak, chip, addr+1, nval);
-
-
-       return change0 || change1;
-}
-
-static int snd_akm4xxx_ipga_gain_info(struct snd_kcontrol *kcontrol,
-                                     struct snd_ctl_elem_info *uinfo)
-{
-       uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
-       uinfo->count = 1;
-       uinfo->value.integer.min = 0;
-       uinfo->value.integer.max = 36;
-       return 0;
-}
-
-static int snd_akm4xxx_ipga_gain_get(struct snd_kcontrol *kcontrol,
-                                    struct snd_ctl_elem_value *ucontrol)
-{
-       struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
-       int chip = AK_GET_CHIP(kcontrol->private_value);
        int addr = AK_GET_ADDR(kcontrol->private_value);
-       ucontrol->value.integer.value[0] =
-               snd_akm4xxx_get_ipga(ak, chip, addr) & 0x7f;
-       return 0;
-}
+       int change;
 
-static int snd_akm4xxx_ipga_gain_put(struct snd_kcontrol *kcontrol,
-                                    struct snd_ctl_elem_value *ucontrol)
-{
-       struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
-       int chip = AK_GET_CHIP(kcontrol->private_value);
-       int addr = AK_GET_ADDR(kcontrol->private_value);
-       unsigned char nval = (ucontrol->value.integer.value[0] % 37) | 0x80;
-       int change = snd_akm4xxx_get_ipga(ak, chip, addr) != nval;
-       if (change)
-               snd_akm4xxx_write(ak, chip, addr, nval);
+       change = put_ak_reg(kcontrol, addr, ucontrol->value.integer.value[0]);
+       change |= put_ak_reg(kcontrol, addr + 1,
+                            ucontrol->value.integer.value[1]);
        return change;
 }
 
@@ -472,179 +462,280 @@ static int snd_akm4xxx_deemphasis_put(struct snd_kcontrol *kcontrol,
        return change;
 }
 
+static int ak4xxx_switch_info(struct snd_kcontrol *kcontrol,
+                             struct snd_ctl_elem_info *uinfo)
+{
+       uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
+       uinfo->count = 1;
+       uinfo->value.integer.min = 0;
+       uinfo->value.integer.max = 1;
+       return 0;
+}
+
+static int ak4xxx_switch_get(struct snd_kcontrol *kcontrol,
+                            struct snd_ctl_elem_value *ucontrol)
+{
+       struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
+       int chip = AK_GET_CHIP(kcontrol->private_value);
+       int addr = AK_GET_ADDR(kcontrol->private_value);
+       int shift = AK_GET_SHIFT(kcontrol->private_value);
+       int invert = AK_GET_INVERT(kcontrol->private_value);
+       unsigned char val = snd_akm4xxx_get(ak, chip, addr);
+
+       if (invert)
+               val = ! val;
+       ucontrol->value.integer.value[0] = (val & (1<<shift)) != 0;
+       return 0;
+}
+
+static int ak4xxx_switch_put(struct snd_kcontrol *kcontrol,
+                            struct snd_ctl_elem_value *ucontrol)
+{
+       struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
+       int chip = AK_GET_CHIP(kcontrol->private_value);
+       int addr = AK_GET_ADDR(kcontrol->private_value);
+       int shift = AK_GET_SHIFT(kcontrol->private_value);
+       int invert = AK_GET_INVERT(kcontrol->private_value);
+       long flag = ucontrol->value.integer.value[0];
+       unsigned char val, oval;
+       int change;
+
+       if (invert)
+               flag = ! flag;
+       oval = snd_akm4xxx_get(ak, chip, addr);
+       if (flag)
+               val = oval | (1<<shift);
+       else
+               val = oval & ~(1<<shift);
+       change = (oval != val);
+       if (change)
+               snd_akm4xxx_write(ak, chip, addr, val);
+       return change;
+}
+
 /*
  * build AK4xxx controls
  */
 
-int snd_akm4xxx_build_controls(struct snd_akm4xxx *ak)
+static int build_dac_controls(struct snd_akm4xxx *ak)
 {
-       unsigned int idx, num_emphs;
-       struct snd_kcontrol *ctl;
-       int err;
-       int mixer_ch = 0;
-       int num_stereo;
-
-       ctl = kmalloc(sizeof(*ctl), GFP_KERNEL);
-       if (! ctl)
-               return -ENOMEM;
+       int idx, err, mixer_ch, num_stereo;
+       struct snd_kcontrol_new knew;
 
+       mixer_ch = 0;
        for (idx = 0; idx < ak->num_dacs; ) {
-               memset(ctl, 0, sizeof(*ctl));
-               if (ak->channel_names == NULL) {
-                       strcpy(ctl->id.name, "DAC Volume");
+               memset(&knew, 0, sizeof(knew));
+               if (! ak->dac_info || ! ak->dac_info[mixer_ch].name) {
+                       knew.name = "DAC Volume";
+                       knew.index = mixer_ch + ak->idx_offset * 2;
                        num_stereo = 1;
-                       ctl->id.index = mixer_ch + ak->idx_offset * 2;
                } else {
-                       strcpy(ctl->id.name, ak->channel_names[mixer_ch]);
-                       num_stereo = ak->num_stereo[mixer_ch];
-                       ctl->id.index = 0;
+                       knew.name = ak->dac_info[mixer_ch].name;
+                       num_stereo = ak->dac_info[mixer_ch].num_channels;
                }
-               ctl->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
-               ctl->count = 1;
+               knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
+               knew.count = 1;
+               knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
+                       SNDRV_CTL_ELEM_ACCESS_TLV_READ;
                if (num_stereo == 2) {
-                       ctl->info = snd_akm4xxx_stereo_volume_info;
-                       ctl->get = snd_akm4xxx_stereo_volume_get;
-                       ctl->put = snd_akm4xxx_stereo_volume_put;
+                       knew.info = snd_akm4xxx_stereo_volume_info;
+                       knew.get = snd_akm4xxx_stereo_volume_get;
+                       knew.put = snd_akm4xxx_stereo_volume_put;
                } else {
-                       ctl->info = snd_akm4xxx_volume_info;
-                       ctl->get = snd_akm4xxx_volume_get;
-                       ctl->put = snd_akm4xxx_volume_put;
+                       knew.info = snd_akm4xxx_volume_info;
+                       knew.get = snd_akm4xxx_volume_get;
+                       knew.put = snd_akm4xxx_volume_put;
                }
                switch (ak->type) {
                case SND_AK4524:
                        /* register 6 & 7 */
-                       ctl->private_value =
-                               AK_COMPOSE(idx/2, (idx%2) + 6, 0, 127);
+                       knew.private_value =
+                               AK_COMPOSE(idx/2, (idx%2) + 6, 0, 127) |
+                               AK_VOL_CVT;
+                       knew.tlv.p = db_scale_vol_datt;
                        break;
                case SND_AK4528:
                        /* register 4 & 5 */
-                       ctl->private_value =
-                               AK_COMPOSE(idx/2, (idx%2) + 4, 0, 127);
+                       knew.private_value =
+                               AK_COMPOSE(idx/2, (idx%2) + 4, 0, 127) |
+                               AK_VOL_CVT;
+                       knew.tlv.p = db_scale_vol_datt;
                        break;
                case SND_AK4529: {
                        /* registers 2-7 and b,c */
                        int val = idx < 6 ? idx + 2 : (idx - 6) + 0xb;
-                       ctl->private_value =
+                       knew.private_value =
                                AK_COMPOSE(0, val, 0, 255) | AK_INVERT;
+                       knew.tlv.p = db_scale_8bit;
                        break;
                }
                case SND_AK4355:
                        /* register 4-9, chip #0 only */
-                       ctl->private_value = AK_COMPOSE(0, idx + 4, 0, 255);
+                       knew.private_value = AK_COMPOSE(0, idx + 4, 0, 255);
+                       knew.tlv.p = db_scale_8bit;
                        break;
-               case SND_AK4358:
-                       if (idx >= 6)
-                               /* register 4-9, chip #0 only */
-                               ctl->private_value =
-                                       AK_COMPOSE(0, idx + 5, 0, 255);
-                       else
-                               /* register 4-9, chip #0 only */
-                               ctl->private_value =
-                                       AK_COMPOSE(0, idx + 4, 0, 255);
+               case SND_AK4358: {
+                       /* register 4-9 and 11-12, chip #0 only */
+                       int  addr = idx < 6 ? idx + 4 : idx + 5;
+                       knew.private_value =
+                               AK_COMPOSE(0, addr, 0, 127) | AK_NEEDSMSB;
+                       knew.tlv.p = db_scale_7bit;
                        break;
+               }
                case SND_AK4381:
                        /* register 3 & 4 */
-                       ctl->private_value =
+                       knew.private_value =
                                AK_COMPOSE(idx/2, (idx%2) + 3, 0, 255);
+                       knew.tlv.p = db_scale_linear;
                        break;
                default:
-                       err = -EINVAL;
-                       goto __error;
+                       return -EINVAL;
                }
 
-               ctl->private_data = ak;
-               err = snd_ctl_add(ak->card,
-                                 snd_ctl_new(ctl, SNDRV_CTL_ELEM_ACCESS_READ|
-                                             SNDRV_CTL_ELEM_ACCESS_WRITE));
+               err = snd_ctl_add(ak->card, snd_ctl_new1(&knew, ak));
                if (err < 0)
-                       goto __error;
+                       return err;
 
                idx += num_stereo;
                mixer_ch++;
        }
-       for (idx = 0; idx < ak->num_adcs && ak->type == SND_AK4524; ++idx) {
-               memset(ctl, 0, sizeof(*ctl));
-               strcpy(ctl->id.name, "ADC Volume");
-               ctl->id.index = idx + ak->idx_offset * 2;
-               ctl->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
-               ctl->count = 1;
-               ctl->info = snd_akm4xxx_volume_info;
-               ctl->get = snd_akm4xxx_volume_get;
-               ctl->put = snd_akm4xxx_volume_put;
-               /* register 4 & 5 */
-               ctl->private_value =
-                       AK_COMPOSE(idx/2, (idx%2) + 4, 0, 127);
-               ctl->private_data = ak;
-               err = snd_ctl_add(ak->card,
-                                 snd_ctl_new(ctl, SNDRV_CTL_ELEM_ACCESS_READ|
-                                             SNDRV_CTL_ELEM_ACCESS_WRITE));
-               if (err < 0)
-                       goto __error;
-
-               memset(ctl, 0, sizeof(*ctl));
-               strcpy(ctl->id.name, "IPGA Analog Capture Volume");
-               ctl->id.index = idx + ak->idx_offset * 2;
-               ctl->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
-               ctl->count = 1;
-               ctl->info = snd_akm4xxx_ipga_gain_info;
-               ctl->get = snd_akm4xxx_ipga_gain_get;
-               ctl->put = snd_akm4xxx_ipga_gain_put;
+       return 0;
+}
+
+static int build_adc_controls(struct snd_akm4xxx *ak)
+{
+       int idx, err, mixer_ch, num_stereo;
+       struct snd_kcontrol_new knew;
+
+       mixer_ch = 0;
+       for (idx = 0; idx < ak->num_adcs;) {
+               memset(&knew, 0, sizeof(knew));
+               if (! ak->adc_info || ! ak->adc_info[mixer_ch].name) {
+                       knew.name = "ADC Volume";
+                       knew.index = mixer_ch + ak->idx_offset * 2;
+                       num_stereo = 1;
+               } else {
+                       knew.name = ak->adc_info[mixer_ch].name;
+                       num_stereo = ak->adc_info[mixer_ch].num_channels;
+               }
+               knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
+               knew.count = 1;
+               knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
+                       SNDRV_CTL_ELEM_ACCESS_TLV_READ;
+               if (num_stereo == 2) {
+                       knew.info = snd_akm4xxx_stereo_volume_info;
+                       knew.get = snd_akm4xxx_stereo_volume_get;
+                       knew.put = snd_akm4xxx_stereo_volume_put;
+               } else {
+                       knew.info = snd_akm4xxx_volume_info;
+                       knew.get = snd_akm4xxx_volume_get;
+                       knew.put = snd_akm4xxx_volume_put;
+               }
                /* register 4 & 5 */
-               ctl->private_value = AK_COMPOSE(idx/2, (idx%2) + 4, 0, 0);
-               ctl->private_data = ak;
-               err = snd_ctl_add(ak->card,
-                                 snd_ctl_new(ctl, SNDRV_CTL_ELEM_ACCESS_READ|
-                                             SNDRV_CTL_ELEM_ACCESS_WRITE));
+               if (ak->type == SND_AK5365)
+                       knew.private_value =
+                               AK_COMPOSE(idx/2, (idx%2) + 4, 0, 151) |
+                               AK_VOL_CVT | AK_IPGA;
+               else
+                       knew.private_value =
+                               AK_COMPOSE(idx/2, (idx%2) + 4, 0, 163) |
+                               AK_VOL_CVT | AK_IPGA;
+               knew.tlv.p = db_scale_vol_datt;
+               err = snd_ctl_add(ak->card, snd_ctl_new1(&knew, ak));
                if (err < 0)
-                       goto __error;
+                       return err;
+
+               if (ak->type == SND_AK5365 && (idx % 2) == 0) {
+                       if (! ak->adc_info || 
+                           ! ak->adc_info[mixer_ch].switch_name)
+                               knew.name = "Capture Switch";
+                       else
+                               knew.name = ak->adc_info[mixer_ch].switch_name;
+                       knew.info = ak4xxx_switch_info;
+                       knew.get = ak4xxx_switch_get;
+                       knew.put = ak4xxx_switch_put;
+                       knew.access = 0;
+                       /* register 2, bit 0 (SMUTE): 0 = normal operation,
+                          1 = mute */
+                       knew.private_value =
+                               AK_COMPOSE(idx/2, 2, 0, 0) | AK_INVERT;
+                       err = snd_ctl_add(ak->card, snd_ctl_new1(&knew, ak));
+                       if (err < 0)
+                               return err;
+               }
+
+               idx += num_stereo;
+               mixer_ch++;
        }
-       if (ak->type == SND_AK4355 || ak->type == SND_AK4358)
-               num_emphs = 1;
-       else
-               num_emphs = ak->num_dacs / 2;
+       return 0;
+}
+
+static int build_deemphasis(struct snd_akm4xxx *ak, int num_emphs)
+{
+       int idx, err;
+       struct snd_kcontrol_new knew;
+
        for (idx = 0; idx < num_emphs; idx++) {
-               memset(ctl, 0, sizeof(*ctl));
-               strcpy(ctl->id.name, "Deemphasis");
-               ctl->id.index = idx + ak->idx_offset;
-               ctl->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
-               ctl->count = 1;
-               ctl->info = snd_akm4xxx_deemphasis_info;
-               ctl->get = snd_akm4xxx_deemphasis_get;
-               ctl->put = snd_akm4xxx_deemphasis_put;
+               memset(&knew, 0, sizeof(knew));
+               knew.name = "Deemphasis";
+               knew.index = idx + ak->idx_offset;
+               knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
+               knew.count = 1;
+               knew.info = snd_akm4xxx_deemphasis_info;
+               knew.get = snd_akm4xxx_deemphasis_get;
+               knew.put = snd_akm4xxx_deemphasis_put;
                switch (ak->type) {
                case SND_AK4524:
                case SND_AK4528:
                        /* register 3 */
-                       ctl->private_value = AK_COMPOSE(idx, 3, 0, 0);
+                       knew.private_value = AK_COMPOSE(idx, 3, 0, 0);
                        break;
                case SND_AK4529: {
                        int shift = idx == 3 ? 6 : (2 - idx) * 2;
                        /* register 8 with shift */
-                       ctl->private_value = AK_COMPOSE(0, 8, shift, 0);
+                       knew.private_value = AK_COMPOSE(0, 8, shift, 0);
                        break;
                }
                case SND_AK4355:
                case SND_AK4358:
-                       ctl->private_value = AK_COMPOSE(idx, 3, 0, 0);
+                       knew.private_value = AK_COMPOSE(idx, 3, 0, 0);
                        break;
                case SND_AK4381:
-                       ctl->private_value = AK_COMPOSE(idx, 1, 1, 0);
+                       knew.private_value = AK_COMPOSE(idx, 1, 1, 0);
                        break;
+               default:
+                       return -EINVAL;
                }
-               ctl->private_data = ak;
-               err = snd_ctl_add(ak->card,
-                                 snd_ctl_new(ctl, SNDRV_CTL_ELEM_ACCESS_READ|
-                                             SNDRV_CTL_ELEM_ACCESS_WRITE));
+               err = snd_ctl_add(ak->card, snd_ctl_new1(&knew, ak));
                if (err < 0)
-                       goto __error;
+                       return err;
        }
-       err = 0;
-
- __error:
-       kfree(ctl);
-       return err;
+       return 0;
 }
 
+int snd_akm4xxx_build_controls(struct snd_akm4xxx *ak)
+{
+       int err, num_emphs;
+
+       err = build_dac_controls(ak);
+       if (err < 0)
+               return err;
+
+       err = build_adc_controls(ak);
+       if (err < 0)
+               return err;
+
+       if (ak->type == SND_AK4355 || ak->type == SND_AK4358)
+               num_emphs = 1;
+       else
+               num_emphs = ak->num_dacs / 2;
+       err = build_deemphasis(ak, num_emphs);
+       if (err < 0)
+               return err;
+
+       return 0;
+}
+       
 EXPORT_SYMBOL(snd_akm4xxx_build_controls);
 
 static int __init alsa_akm4xxx_module_init(void)