ALSA: hda: take cmd_mutex in probe_codec()
[safe/jmp/linux-2.6] / sound / pci / hda / patch_via.c
index 6e360d3..9008b4b 100644 (file)
@@ -35,6 +35,7 @@
 /* 2008-04-09  Lydia Wang  Add mute front speaker when HP plugin             */
 /* 2008-04-09  Lydia Wang  Add Independent HP feature                        */
 /* 2008-05-28  Lydia Wang  Add second S/PDIF Out support for VT1702         */
+/* 2008-09-15  Logan Li    Add VT1708S Mic Boost workaround/backdoor        */
 /*                                                                           */
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
 #include <sound/asoundef.h>
 #include "hda_codec.h"
 #include "hda_local.h"
-#include "hda_patch.h"
 
 /* amp values */
 #define AMP_VAL_IDX_SHIFT      19
 #define AMP_VAL_IDX_MASK       (0x0f<<19)
 
-#define NUM_CONTROL_ALLOC      32
-#define NUM_VERB_ALLOC         32
-
 /* Pin Widget NID */
 #define VT1708_HP_NID          0x13
 #define VT1708_DIGOUT_NID      0x14
 #define IS_VT1708S_VENDORID(x)         ((x) >= 0x11060397 && (x) <= 0x11067397)
 #define IS_VT1702_VENDORID(x)          ((x) >= 0x11060398 && (x) <= 0x11067398)
 
+enum VIA_HDA_CODEC {
+       UNKNOWN = -1,
+       VT1708,
+       VT1709_10CH,
+       VT1709_6CH,
+       VT1708B_8CH,
+       VT1708B_4CH,
+       VT1708S,
+       VT1702,
+       CODEC_TYPES,
+};
+
+static enum VIA_HDA_CODEC get_codec_type(u32 vendor_id)
+{
+       u16 ven_id = vendor_id >> 16;
+       u16 dev_id = vendor_id & 0xffff;
+       enum VIA_HDA_CODEC codec_type;
+
+       /* get codec type */
+       if (ven_id != 0x1106)
+               codec_type = UNKNOWN;
+       else if (dev_id >= 0x1708 && dev_id <= 0x170b)
+               codec_type = VT1708;
+       else if (dev_id >= 0xe710 && dev_id <= 0xe713)
+               codec_type = VT1709_10CH;
+       else if (dev_id >= 0xe714 && dev_id <= 0xe717)
+               codec_type = VT1709_6CH;
+       else if (dev_id >= 0xe720 && dev_id <= 0xe723)
+               codec_type = VT1708B_8CH;
+       else if (dev_id >= 0xe724 && dev_id <= 0xe727)
+               codec_type = VT1708B_4CH;
+       else if ((dev_id & 0xfff) == 0x397
+                && (dev_id >> 12) < 8)
+               codec_type = VT1708S;
+       else if ((dev_id & 0xfff) == 0x398
+                && (dev_id >> 12) < 8)
+               codec_type = VT1702;
+       else
+               codec_type = UNKNOWN;
+       return codec_type;
+};
+
 #define VIA_HP_EVENT           0x01
 #define VIA_GPIO_EVENT         0x02
 
@@ -102,6 +141,46 @@ enum {
        AUTO_SEQ_SIDE
 };
 
+/* Some VT1708S based boards gets the micboost setting wrong, so we have
+ * to apply some brute-force and re-write the TLV's by software. */
+static int mic_boost_tlv(struct snd_kcontrol *kcontrol, int op_flag,
+                        unsigned int size, unsigned int __user *_tlv)
+{
+       struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
+       hda_nid_t nid = get_amp_nid(kcontrol);
+
+       if (get_codec_type(codec->vendor_id) == VT1708S
+           && (nid == 0x1a || nid == 0x1e)) {
+               if (size < 4 * sizeof(unsigned int))
+                       return -ENOMEM;
+               if (put_user(1, _tlv))  /* SNDRV_CTL_TLVT_DB_SCALE */
+                       return -EFAULT;
+               if (put_user(2 * sizeof(unsigned int), _tlv + 1))
+                       return -EFAULT;
+               if (put_user(0, _tlv + 2)) /* offset = 0 */
+                       return -EFAULT;
+               if (put_user(1000, _tlv + 3)) /* step size = 10 dB */
+                       return -EFAULT;
+       }
+       return 0;
+}
+
+static int mic_boost_volume_info(struct snd_kcontrol *kcontrol,
+                                struct snd_ctl_elem_info *uinfo)
+{
+       struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
+       hda_nid_t nid = get_amp_nid(kcontrol);
+
+       if (get_codec_type(codec->vendor_id) == VT1708S
+           && (nid == 0x1a || nid == 0x1e)) {
+               uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
+               uinfo->count = 2;
+               uinfo->value.integer.min = 0;
+               uinfo->value.integer.max = 3;
+       }
+       return 0;
+}
+
 static struct snd_kcontrol_new vt1708_control_templates[] = {
        HDA_CODEC_VOLUME(NULL, 0, 0, 0),
        HDA_CODEC_MUTE(NULL, 0, 0, 0),
@@ -123,16 +202,17 @@ struct via_spec {
        char *stream_name_digital;
        struct hda_pcm_stream *stream_digital_playback;
        struct hda_pcm_stream *stream_digital_capture;
-       struct hda_pcm_stream *stream_extra_digital_playback;
 
        /* playback */
        struct hda_multi_out multiout;
-       hda_nid_t extra_dig_out_nid;
+       hda_nid_t slave_dig_outs[2];
 
        /* capture */
        unsigned int num_adc_nids;
        hda_nid_t *adc_nids;
+       hda_nid_t mux_nids[3];
        hda_nid_t dig_in_nid;
+       hda_nid_t dig_in_pin;
 
        /* capture source */
        const struct hda_input_mux *input_mux;
@@ -143,8 +223,7 @@ struct via_spec {
 
        /* dynamic controls, init_verbs and input_mux */
        struct auto_pin_cfg autocfg;
-       unsigned int num_kctl_alloc, num_kctl_used;
-       struct snd_kcontrol_new *kctl_alloc;
+       struct snd_array kctls;
        struct hda_input_mux private_imux[2];
        hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
 
@@ -188,33 +267,31 @@ static int via_add_control(struct via_spec *spec, int type, const char *name,
 {
        struct snd_kcontrol_new *knew;
 
-       if (spec->num_kctl_used >= spec->num_kctl_alloc) {
-               int num = spec->num_kctl_alloc + NUM_CONTROL_ALLOC;
-
-               /* array + terminator */
-               knew = kcalloc(num + 1, sizeof(*knew), GFP_KERNEL);
-               if (!knew)
-                       return -ENOMEM;
-               if (spec->kctl_alloc) {
-                       memcpy(knew, spec->kctl_alloc,
-                              sizeof(*knew) * spec->num_kctl_alloc);
-                       kfree(spec->kctl_alloc);
-               }
-               spec->kctl_alloc = knew;
-               spec->num_kctl_alloc = num;
-       }
-
-       knew = &spec->kctl_alloc[spec->num_kctl_used];
+       snd_array_init(&spec->kctls, sizeof(*knew), 32);
+       knew = snd_array_new(&spec->kctls);
+       if (!knew)
+               return -ENOMEM;
        *knew = vt1708_control_templates[type];
        knew->name = kstrdup(name, GFP_KERNEL);
-
        if (!knew->name)
                return -ENOMEM;
        knew->private_value = val;
-       spec->num_kctl_used++;
        return 0;
 }
 
+static void via_free_kctls(struct hda_codec *codec)
+{
+       struct via_spec *spec = codec->spec;
+
+       if (spec->kctls.list) {
+               struct snd_kcontrol_new *kctl = spec->kctls.list;
+               int i;
+               for (i = 0; i < spec->kctls.used; i++)
+                       kfree(kctl[i].name);
+       }
+       snd_array_free(&spec->kctls);
+}
+
 /* create input playback/capture controls for the given pin */
 static int via_new_analog_input(struct via_spec *spec, hda_nid_t pin,
                                const char *ctlname, int idx, int mix_nid)
@@ -244,6 +321,9 @@ static void via_auto_set_output_and_unmute(struct hda_codec *codec,
                            pin_type);
        snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
                            AMP_OUT_UNMUTE);
+       if (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)
+               snd_hda_codec_write(codec, nid, 0, 
+                                   AC_VERB_SET_EAPD_BTLENABLE, 0x02);
 }
 
 
@@ -312,27 +392,12 @@ static int via_mux_enum_put(struct snd_kcontrol *kcontrol,
        struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
        struct via_spec *spec = codec->spec;
        unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
-       unsigned int vendor_id = codec->vendor_id;
-
-       /* AIW0  lydia 060801 add for correct sw0 input select */
-       if (IS_VT1708_VENDORID(vendor_id) && (adc_idx == 0))
-               return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
-                                            0x18, &spec->cur_mux[adc_idx]);
-       else if ((IS_VT1709_10CH_VENDORID(vendor_id) ||
-                 IS_VT1709_6CH_VENDORID(vendor_id)) && (adc_idx == 0))
-               return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
-                                            0x19, &spec->cur_mux[adc_idx]);
-       else if ((IS_VT1708B_8CH_VENDORID(vendor_id) ||
-                 IS_VT1708B_4CH_VENDORID(vendor_id)) && (adc_idx == 0))
-               return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
-                                            0x17, &spec->cur_mux[adc_idx]);
-       else if (IS_VT1702_VENDORID(vendor_id) && (adc_idx == 0))
-               return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
-                                            0x13, &spec->cur_mux[adc_idx]);
-       else
-               return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
-                                            spec->adc_nids[adc_idx],
-                                            &spec->cur_mux[adc_idx]);
+
+       if (!spec->mux_nids[adc_idx])
+               return -EINVAL;
+       return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
+                                    spec->mux_nids[adc_idx],
+                                    &spec->cur_mux[adc_idx]);
 }
 
 static int via_independent_hp_info(struct snd_kcontrol *kcontrol,
@@ -667,33 +732,12 @@ static int via_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
                                             stream_tag, format, substream);
 }
 
-/* setup SPDIF output stream */
-static void setup_dig_playback_stream(struct hda_codec *codec, hda_nid_t nid,
-                                unsigned int stream_tag, unsigned int format)
-{
-       /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
-       if (codec->spdif_ctls & AC_DIG1_ENABLE)
-               snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
-                                   codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff);
-       snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
-       /* turn on again (if needed) */
-       if (codec->spdif_ctls & AC_DIG1_ENABLE)
-               snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
-                                   codec->spdif_ctls & 0xff);
-}
-
-static int via_extra_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
+static int via_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
                                        struct hda_codec *codec,
-                                       unsigned int stream_tag,
-                                       unsigned int format,
                                        struct snd_pcm_substream *substream)
 {
        struct via_spec *spec = codec->spec;
-
-       mutex_lock(&codec->spdif_mutex);
-       setup_dig_playback_stream(codec, spec->extra_dig_out_nid, stream_tag,
-                                 format);
-       mutex_unlock(&codec->spdif_mutex);
+       snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
        return 0;
 }
 
@@ -770,7 +814,8 @@ static struct hda_pcm_stream vt1708_pcm_digital_playback = {
        .ops = {
                .open = via_dig_playback_pcm_open,
                .close = via_dig_playback_pcm_close,
-               .prepare = via_dig_playback_pcm_prepare
+               .prepare = via_dig_playback_pcm_prepare,
+               .cleanup = via_dig_playback_pcm_cleanup
        },
 };
 
@@ -802,19 +847,13 @@ static int via_build_controls(struct hda_codec *codec)
                if (err < 0)
                        return err;
                spec->multiout.share_spdif = 1;
-
-               if (spec->extra_dig_out_nid) {
-                       err = snd_hda_create_spdif_out_ctls(codec,
-                                                   spec->extra_dig_out_nid);
-                       if (err < 0)
-                               return err;
-               }
        }
        if (spec->dig_in_nid) {
                err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
                if (err < 0)
                        return err;
        }
+       via_free_kctls(codec); /* no longer needed */
        return 0;
 }
 
@@ -854,34 +893,17 @@ static int via_build_pcms(struct hda_codec *codec)
                }
        }
 
-       if (spec->extra_dig_out_nid) {
-               codec->num_pcms++;
-               info++;
-               info->name = spec->stream_name_digital;
-               info->pcm_type = HDA_PCM_TYPE_HDMI;
-               info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
-                               *(spec->stream_extra_digital_playback);
-               info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
-                               spec->extra_dig_out_nid;
-       }
-
        return 0;
 }
 
 static void via_free(struct hda_codec *codec)
 {
        struct via_spec *spec = codec->spec;
-       unsigned int i;
 
        if (!spec)
                return;
 
-       if (spec->kctl_alloc) {
-               for (i = 0; i < spec->num_kctl_used; i++)
-                       kfree(spec->kctl_alloc[i].name);
-               kfree(spec->kctl_alloc);
-       }
-
+       via_free_kctls(codec);
        kfree(codec->spec);
 }
 
@@ -966,31 +988,21 @@ static int via_init(struct hda_codec *codec)
 
        /* Lydia Add for EAPD enable */
        if (!spec->dig_in_nid) { /* No Digital In connection */
-               if (IS_VT1708_VENDORID(codec->vendor_id)) {
-                       snd_hda_codec_write(codec, VT1708_DIGIN_PIN, 0,
-                                           AC_VERB_SET_PIN_WIDGET_CONTROL,
-                                           PIN_OUT);
-                       snd_hda_codec_write(codec, VT1708_DIGIN_PIN, 0,
-                                           AC_VERB_SET_EAPD_BTLENABLE, 0x02);
-               } else if (IS_VT1709_10CH_VENDORID(codec->vendor_id) ||
-                          IS_VT1709_6CH_VENDORID(codec->vendor_id)) {
-                       snd_hda_codec_write(codec, VT1709_DIGIN_PIN, 0,
-                                           AC_VERB_SET_PIN_WIDGET_CONTROL,
-                                           PIN_OUT);
-                       snd_hda_codec_write(codec, VT1709_DIGIN_PIN, 0,
-                                           AC_VERB_SET_EAPD_BTLENABLE, 0x02);
-               } else if (IS_VT1708B_8CH_VENDORID(codec->vendor_id) ||
-                          IS_VT1708B_4CH_VENDORID(codec->vendor_id)) {
-                       snd_hda_codec_write(codec, VT1708B_DIGIN_PIN, 0,
+               if (spec->dig_in_pin) {
+                       snd_hda_codec_write(codec, spec->dig_in_pin, 0,
                                            AC_VERB_SET_PIN_WIDGET_CONTROL,
                                            PIN_OUT);
-                       snd_hda_codec_write(codec, VT1708B_DIGIN_PIN, 0,
+                       snd_hda_codec_write(codec, spec->dig_in_pin, 0,
                                            AC_VERB_SET_EAPD_BTLENABLE, 0x02);
                }
        } else /* enable SPDIF-input pin */
                snd_hda_codec_write(codec, spec->autocfg.dig_in_pin, 0,
                                    AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN);
 
+       /* assign slave outs */
+       if (spec->slave_dig_outs[0])
+               codec->slave_dig_outs = spec->slave_dig_outs;
+
        return 0;
 }
 
@@ -1245,16 +1257,13 @@ static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
        unsigned int def_conf;
        unsigned char seqassoc;
 
-       def_conf = snd_hda_codec_read(codec, nid, 0,
-                                     AC_VERB_GET_CONFIG_DEFAULT, 0);
+       def_conf = snd_hda_codec_get_pincfg(codec, nid);
        seqassoc = (unsigned char) get_defcfg_association(def_conf);
        seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
        if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE) {
                if (seqassoc == 0xff) {
                        def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
-                       snd_hda_codec_write(codec, nid, 0,
-                                           AC_VERB_SET_CONFIG_DEFAULT_BYTES_3,
-                                           def_conf >> 24);
+                       snd_hda_codec_set_pincfg(codec, nid, def_conf);
                }
        }
 
@@ -1291,19 +1300,21 @@ static int vt1708_parse_auto_config(struct hda_codec *codec)
 
        spec->multiout.max_channels = spec->multiout.num_dacs * 2;
 
-       if (spec->autocfg.dig_out_pin)
+       if (spec->autocfg.dig_outs)
                spec->multiout.dig_out_nid = VT1708_DIGOUT_NID;
+       spec->dig_in_pin = VT1708_DIGIN_PIN;
        if (spec->autocfg.dig_in_pin)
                spec->dig_in_nid = VT1708_DIGIN_NID;
 
-       if (spec->kctl_alloc)
-               spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
+       if (spec->kctls.list)
+               spec->mixers[spec->num_mixers++] = spec->kctls.list;
 
        spec->init_verbs[spec->num_iverbs++] = vt1708_volume_init_verbs;
 
        spec->input_mux = &spec->private_imux[0];
 
-       spec->mixers[spec->num_mixers++] = via_hp_mixer;
+       if (spec->hp_mux)
+               spec->mixers[spec->num_mixers++] = via_hp_mixer;
 
        return 1;
 }
@@ -1318,6 +1329,34 @@ static int via_auto_init(struct hda_codec *codec)
        return 0;
 }
 
+static int get_mux_nids(struct hda_codec *codec)
+{
+       struct via_spec *spec = codec->spec;
+       hda_nid_t nid, conn[8];
+       unsigned int type;
+       int i, n;
+
+       for (i = 0; i < spec->num_adc_nids; i++) {
+               nid = spec->adc_nids[i];
+               while (nid) {
+                       type = (get_wcaps(codec, nid) & AC_WCAP_TYPE)
+                               >> AC_WCAP_TYPE_SHIFT;
+                       if (type == AC_WID_PIN)
+                               break;
+                       n = snd_hda_get_connections(codec, nid, conn,
+                                                   ARRAY_SIZE(conn));
+                       if (n <= 0)
+                               break;
+                       if (n > 1) {
+                               spec->mux_nids[i] = nid;
+                               break;
+                       }
+                       nid = conn[0];
+               }
+       }
+       return 0;
+}
+
 static int patch_vt1708(struct hda_codec *codec)
 {
        struct via_spec *spec;
@@ -1763,16 +1802,20 @@ static int vt1709_parse_auto_config(struct hda_codec *codec)
 
        spec->multiout.max_channels = spec->multiout.num_dacs * 2;
 
-       if (spec->autocfg.dig_out_pin)
+       if (spec->autocfg.dig_outs)
                spec->multiout.dig_out_nid = VT1709_DIGOUT_NID;
+       spec->dig_in_pin = VT1709_DIGIN_PIN;
        if (spec->autocfg.dig_in_pin)
                spec->dig_in_nid = VT1709_DIGIN_NID;
 
-       if (spec->kctl_alloc)
-               spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
+       if (spec->kctls.list)
+               spec->mixers[spec->num_mixers++] = spec->kctls.list;
 
        spec->input_mux = &spec->private_imux[0];
 
+       if (spec->hp_mux)
+               spec->mixers[spec->num_mixers++] = via_hp_mixer;
+
        return 1;
 }
 
@@ -1822,6 +1865,7 @@ static int patch_vt1709_10ch(struct hda_codec *codec)
        if (!spec->adc_nids && spec->input_mux) {
                spec->adc_nids = vt1709_adc_nids;
                spec->num_adc_nids = ARRAY_SIZE(vt1709_adc_nids);
+               get_mux_nids(codec);
                spec->mixers[spec->num_mixers] = vt1709_capture_mixer;
                spec->num_mixers++;
        }
@@ -1915,6 +1959,7 @@ static int patch_vt1709_6ch(struct hda_codec *codec)
        if (!spec->adc_nids && spec->input_mux) {
                spec->adc_nids = vt1709_adc_nids;
                spec->num_adc_nids = ARRAY_SIZE(vt1709_adc_nids);
+               get_mux_nids(codec);
                spec->mixers[spec->num_mixers] = vt1709_capture_mixer;
                spec->num_mixers++;
        }
@@ -2070,7 +2115,8 @@ static struct hda_pcm_stream vt1708B_pcm_digital_playback = {
        .ops = {
                .open = via_dig_playback_pcm_open,
                .close = via_dig_playback_pcm_close,
-               .prepare = via_dig_playback_pcm_prepare
+               .prepare = via_dig_playback_pcm_prepare,
+               .cleanup = via_dig_playback_pcm_cleanup
        },
 };
 
@@ -2304,17 +2350,19 @@ static int vt1708B_parse_auto_config(struct hda_codec *codec)
 
        spec->multiout.max_channels = spec->multiout.num_dacs * 2;
 
-       if (spec->autocfg.dig_out_pin)
+       if (spec->autocfg.dig_outs)
                spec->multiout.dig_out_nid = VT1708B_DIGOUT_NID;
+       spec->dig_in_pin = VT1708B_DIGIN_PIN;
        if (spec->autocfg.dig_in_pin)
                spec->dig_in_nid = VT1708B_DIGIN_NID;
 
-       if (spec->kctl_alloc)
-               spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
+       if (spec->kctls.list)
+               spec->mixers[spec->num_mixers++] = spec->kctls.list;
 
        spec->input_mux = &spec->private_imux[0];
 
-       spec->mixers[spec->num_mixers++] = via_hp_mixer;
+       if (spec->hp_mux)
+               spec->mixers[spec->num_mixers++] = via_hp_mixer;
 
        return 1;
 }
@@ -2365,6 +2413,7 @@ static int patch_vt1708B_8ch(struct hda_codec *codec)
        if (!spec->adc_nids && spec->input_mux) {
                spec->adc_nids = vt1708B_adc_nids;
                spec->num_adc_nids = ARRAY_SIZE(vt1708B_adc_nids);
+               get_mux_nids(codec);
                spec->mixers[spec->num_mixers] = vt1708B_capture_mixer;
                spec->num_mixers++;
        }
@@ -2416,6 +2465,7 @@ static int patch_vt1708B_4ch(struct hda_codec *codec)
        if (!spec->adc_nids && spec->input_mux) {
                spec->adc_nids = vt1708B_adc_nids;
                spec->num_adc_nids = ARRAY_SIZE(vt1708B_adc_nids);
+               get_mux_nids(codec);
                spec->mixers[spec->num_mixers] = vt1708B_capture_mixer;
                spec->num_mixers++;
        }
@@ -2433,14 +2483,29 @@ static int patch_vt1708B_4ch(struct hda_codec *codec)
 
 /* Patch for VT1708S */
 
+/* VT1708S software backdoor based override for buggy hardware micboost
+ * setting */
+#define MIC_BOOST_VOLUME(xname, nid) {                         \
+       .iface = SNDRV_CTL_ELEM_IFACE_MIXER,            \
+       .name = xname,                                  \
+       .index = 0,                                     \
+       .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |     \
+       SNDRV_CTL_ELEM_ACCESS_TLV_READ |                \
+       SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,             \
+       .info = mic_boost_volume_info,                  \
+       .get = snd_hda_mixer_amp_volume_get,            \
+       .put = snd_hda_mixer_amp_volume_put,            \
+       .tlv = { .c = mic_boost_tlv },                  \
+       .private_value = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT) }
+
 /* capture mixer elements */
 static struct snd_kcontrol_new vt1708S_capture_mixer[] = {
        HDA_CODEC_VOLUME("Capture Volume", 0x13, 0x0, HDA_INPUT),
        HDA_CODEC_MUTE("Capture Switch", 0x13, 0x0, HDA_INPUT),
        HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x14, 0x0, HDA_INPUT),
        HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x14, 0x0, HDA_INPUT),
-       HDA_CODEC_VOLUME("Mic Boost", 0x1A, 0x0, HDA_INPUT),
-       HDA_CODEC_VOLUME("Front Mic Boost", 0x1E, 0x0, HDA_INPUT),
+       MIC_BOOST_VOLUME("Mic Boost Capture Volume", 0x1A),
+       MIC_BOOST_VOLUME("Front Mic Boost Capture Volume", 0x1E),
        {
                .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
                /* The multiple "Capture Source" controls confuse alsamixer
@@ -2472,8 +2537,11 @@ static struct hda_verb vt1708S_volume_init_verbs[] = {
 
        /* Setup default input of PW4 to MW0 */
        {0x1d, AC_VERB_SET_CONNECT_SEL, 0x0},
-       /* PW9 Output enable */
+       /* PW9, PW10  Output enable */
        {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
+       {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
+       /* Enable Mic Boost Volume backdoor */
+       {0x1, 0xf98, 0x1},
        { }
 };
 
@@ -2513,17 +2581,8 @@ static struct hda_pcm_stream vt1708S_pcm_digital_playback = {
        .ops = {
                .open = via_dig_playback_pcm_open,
                .close = via_dig_playback_pcm_close,
-               .prepare = via_dig_playback_pcm_prepare
-       },
-};
-
-static struct hda_pcm_stream vt1708S_pcm_extra_digital_playback = {
-       .substreams = 1,
-       .channels_min = 2,
-       .channels_max = 2,
-       /* NID is set in via_build_pcms */
-       .ops = {
-               .prepare = via_extra_dig_playback_pcm_prepare
+               .prepare = via_dig_playback_pcm_prepare,
+               .cleanup = via_dig_playback_pcm_cleanup
        },
 };
 
@@ -2732,14 +2791,37 @@ static int vt1708S_auto_create_analog_input_ctls(struct via_spec *spec,
        return 0;
 }
 
+/* fill out digital output widgets; one for master and one for slave outputs */
+static void fill_dig_outs(struct hda_codec *codec)
+{
+       struct via_spec *spec = codec->spec;
+       int i;
+
+       for (i = 0; i < spec->autocfg.dig_outs; i++) {
+               hda_nid_t nid;
+               int conn;
+
+               nid = spec->autocfg.dig_out_pins[i];
+               if (!nid)
+                       continue;
+               conn = snd_hda_get_connections(codec, nid, &nid, 1);
+               if (conn < 1)
+                       continue;
+               if (!spec->multiout.dig_out_nid)
+                       spec->multiout.dig_out_nid = nid;
+               else {
+                       spec->slave_dig_outs[0] = nid;
+                       break; /* at most two dig outs */
+               }
+       }
+}
+
 static int vt1708S_parse_auto_config(struct hda_codec *codec)
 {
        struct via_spec *spec = codec->spec;
        int err;
-       static hda_nid_t vt1708s_ignore[] = {0x21, 0};
 
-       err = snd_hda_parse_pin_def_config(codec, &spec->autocfg,
-                                          vt1708s_ignore);
+       err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
        if (err < 0)
                return err;
        err = vt1708S_auto_fill_dac_nids(spec, &spec->autocfg);
@@ -2760,17 +2842,15 @@ static int vt1708S_parse_auto_config(struct hda_codec *codec)
 
        spec->multiout.max_channels = spec->multiout.num_dacs * 2;
 
-       if (spec->autocfg.dig_out_pin)
-               spec->multiout.dig_out_nid = VT1708S_DIGOUT_NID;
+       fill_dig_outs(codec);
 
-       spec->extra_dig_out_nid = 0x15;
-
-       if (spec->kctl_alloc)
-               spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
+       if (spec->kctls.list)
+               spec->mixers[spec->num_mixers++] = spec->kctls.list;
 
        spec->input_mux = &spec->private_imux[0];
 
-       spec->mixers[spec->num_mixers++] = via_hp_mixer;
+       if (spec->hp_mux)
+               spec->mixers[spec->num_mixers++] = via_hp_mixer;
 
        return 1;
 }
@@ -2816,12 +2896,11 @@ static int patch_vt1708S(struct hda_codec *codec)
 
        spec->stream_name_digital = "VT1708S Digital";
        spec->stream_digital_playback = &vt1708S_pcm_digital_playback;
-       spec->stream_extra_digital_playback =
-                                       &vt1708S_pcm_extra_digital_playback;
 
        if (!spec->adc_nids && spec->input_mux) {
                spec->adc_nids = vt1708S_adc_nids;
                spec->num_adc_nids = ARRAY_SIZE(vt1708S_adc_nids);
+               get_mux_nids(codec);
                spec->mixers[spec->num_mixers] = vt1708S_capture_mixer;
                spec->num_mixers++;
        }
@@ -2921,24 +3000,15 @@ static struct hda_pcm_stream vt1702_pcm_analog_capture = {
 };
 
 static struct hda_pcm_stream vt1702_pcm_digital_playback = {
-       .substreams = 1,
+       .substreams = 2,
        .channels_min = 2,
        .channels_max = 2,
        /* NID is set in via_build_pcms */
        .ops = {
                .open = via_dig_playback_pcm_open,
                .close = via_dig_playback_pcm_close,
-               .prepare = via_dig_playback_pcm_prepare
-       },
-};
-
-static struct hda_pcm_stream vt1702_pcm_extra_digital_playback = {
-       .substreams = 1,
-       .channels_min = 2,
-       .channels_max = 2,
-       /* NID is set in via_build_pcms */
-       .ops = {
-               .prepare = via_extra_dig_playback_pcm_prepare
+               .prepare = via_dig_playback_pcm_prepare,
+               .cleanup = via_dig_playback_pcm_cleanup
        },
 };
 
@@ -3066,10 +3136,8 @@ static int vt1702_parse_auto_config(struct hda_codec *codec)
 {
        struct via_spec *spec = codec->spec;
        int err;
-       static hda_nid_t vt1702_ignore[] = {0x1C, 0};
 
-       err = snd_hda_parse_pin_def_config(codec, &spec->autocfg,
-                                          vt1702_ignore);
+       err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
        if (err < 0)
                return err;
        err = vt1702_auto_fill_dac_nids(spec, &spec->autocfg);
@@ -3090,17 +3158,15 @@ static int vt1702_parse_auto_config(struct hda_codec *codec)
 
        spec->multiout.max_channels = spec->multiout.num_dacs * 2;
 
-       if (spec->autocfg.dig_out_pin)
-               spec->multiout.dig_out_nid = VT1702_DIGOUT_NID;
-
-       spec->extra_dig_out_nid = 0x1B;
+       fill_dig_outs(codec);
 
-       if (spec->kctl_alloc)
-               spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
+       if (spec->kctls.list)
+               spec->mixers[spec->num_mixers++] = spec->kctls.list;
 
        spec->input_mux = &spec->private_imux[0];
 
-       spec->mixers[spec->num_mixers++] = via_hp_mixer;
+       if (spec->hp_mux)
+               spec->mixers[spec->num_mixers++] = via_hp_mixer;
 
        return 1;
 }
@@ -3148,12 +3214,11 @@ static int patch_vt1702(struct hda_codec *codec)
 
        spec->stream_name_digital = "VT1702 Digital";
        spec->stream_digital_playback = &vt1702_pcm_digital_playback;
-       spec->stream_extra_digital_playback =
-                                       &vt1702_pcm_extra_digital_playback;
 
        if (!spec->adc_nids && spec->input_mux) {
                spec->adc_nids = vt1702_adc_nids;
                spec->num_adc_nids = ARRAY_SIZE(vt1702_adc_nids);
+               get_mux_nids(codec);
                spec->mixers[spec->num_mixers] = vt1702_capture_mixer;
                spec->num_mixers++;
        }
@@ -3184,74 +3249,97 @@ static int patch_vt1702(struct hda_codec *codec)
 /*
  * patch entries
  */
-struct hda_codec_preset snd_hda_preset_via[] = {
-       { .id = 0x11061708, .name = "VIA VT1708", .patch = patch_vt1708},
-       { .id = 0x11061709, .name = "VIA VT1708", .patch = patch_vt1708},
-       { .id = 0x1106170A, .name = "VIA VT1708", .patch = patch_vt1708},
-       { .id = 0x1106170B, .name = "VIA VT1708", .patch = patch_vt1708},
-       { .id = 0x1106E710, .name = "VIA VT1709 10-Ch",
+static struct hda_codec_preset snd_hda_preset_via[] = {
+       { .id = 0x11061708, .name = "VT1708", .patch = patch_vt1708},
+       { .id = 0x11061709, .name = "VT1708", .patch = patch_vt1708},
+       { .id = 0x1106170a, .name = "VT1708", .patch = patch_vt1708},
+       { .id = 0x1106170b, .name = "VT1708", .patch = patch_vt1708},
+       { .id = 0x1106e710, .name = "VT1709 10-Ch",
          .patch = patch_vt1709_10ch},
-       { .id = 0x1106E711, .name = "VIA VT1709 10-Ch",
+       { .id = 0x1106e711, .name = "VT1709 10-Ch",
          .patch = patch_vt1709_10ch},
-       { .id = 0x1106E712, .name = "VIA VT1709 10-Ch",
+       { .id = 0x1106e712, .name = "VT1709 10-Ch",
          .patch = patch_vt1709_10ch},
-       { .id = 0x1106E713, .name = "VIA VT1709 10-Ch",
+       { .id = 0x1106e713, .name = "VT1709 10-Ch",
          .patch = patch_vt1709_10ch},
-       { .id = 0x1106E714, .name = "VIA VT1709 6-Ch",
+       { .id = 0x1106e714, .name = "VT1709 6-Ch",
          .patch = patch_vt1709_6ch},
-       { .id = 0x1106E715, .name = "VIA VT1709 6-Ch",
+       { .id = 0x1106e715, .name = "VT1709 6-Ch",
          .patch = patch_vt1709_6ch},
-       { .id = 0x1106E716, .name = "VIA VT1709 6-Ch",
+       { .id = 0x1106e716, .name = "VT1709 6-Ch",
          .patch = patch_vt1709_6ch},
-       { .id = 0x1106E717, .name = "VIA VT1709 6-Ch",
+       { .id = 0x1106e717, .name = "VT1709 6-Ch",
          .patch = patch_vt1709_6ch},
-       { .id = 0x1106E720, .name = "VIA VT1708B 8-Ch",
+       { .id = 0x1106e720, .name = "VT1708B 8-Ch",
          .patch = patch_vt1708B_8ch},
-       { .id = 0x1106E721, .name = "VIA VT1708B 8-Ch",
+       { .id = 0x1106e721, .name = "VT1708B 8-Ch",
          .patch = patch_vt1708B_8ch},
-       { .id = 0x1106E722, .name = "VIA VT1708B 8-Ch",
+       { .id = 0x1106e722, .name = "VT1708B 8-Ch",
          .patch = patch_vt1708B_8ch},
-       { .id = 0x1106E723, .name = "VIA VT1708B 8-Ch",
+       { .id = 0x1106e723, .name = "VT1708B 8-Ch",
          .patch = patch_vt1708B_8ch},
-       { .id = 0x1106E724, .name = "VIA VT1708B 4-Ch",
+       { .id = 0x1106e724, .name = "VT1708B 4-Ch",
          .patch = patch_vt1708B_4ch},
-       { .id = 0x1106E725, .name = "VIA VT1708B 4-Ch",
+       { .id = 0x1106e725, .name = "VT1708B 4-Ch",
          .patch = patch_vt1708B_4ch},
-       { .id = 0x1106E726, .name = "VIA VT1708B 4-Ch",
+       { .id = 0x1106e726, .name = "VT1708B 4-Ch",
          .patch = patch_vt1708B_4ch},
-       { .id = 0x1106E727, .name = "VIA VT1708B 4-Ch",
+       { .id = 0x1106e727, .name = "VT1708B 4-Ch",
          .patch = patch_vt1708B_4ch},
-       { .id = 0x11060397, .name = "VIA VT1708S",
+       { .id = 0x11060397, .name = "VT1708S",
          .patch = patch_vt1708S},
-       { .id = 0x11061397, .name = "VIA VT1708S",
+       { .id = 0x11061397, .name = "VT1708S",
          .patch = patch_vt1708S},
-       { .id = 0x11062397, .name = "VIA VT1708S",
+       { .id = 0x11062397, .name = "VT1708S",
          .patch = patch_vt1708S},
-       { .id = 0x11063397, .name = "VIA VT1708S",
+       { .id = 0x11063397, .name = "VT1708S",
          .patch = patch_vt1708S},
-       { .id = 0x11064397, .name = "VIA VT1708S",
+       { .id = 0x11064397, .name = "VT1708S",
          .patch = patch_vt1708S},
-       { .id = 0x11065397, .name = "VIA VT1708S",
+       { .id = 0x11065397, .name = "VT1708S",
          .patch = patch_vt1708S},
-       { .id = 0x11066397, .name = "VIA VT1708S",
+       { .id = 0x11066397, .name = "VT1708S",
          .patch = patch_vt1708S},
-       { .id = 0x11067397, .name = "VIA VT1708S",
+       { .id = 0x11067397, .name = "VT1708S",
          .patch = patch_vt1708S},
-       { .id = 0x11060398, .name = "VIA VT1702",
+       { .id = 0x11060398, .name = "VT1702",
          .patch = patch_vt1702},
-       { .id = 0x11061398, .name = "VIA VT1702",
+       { .id = 0x11061398, .name = "VT1702",
          .patch = patch_vt1702},
-       { .id = 0x11062398, .name = "VIA VT1702",
+       { .id = 0x11062398, .name = "VT1702",
          .patch = patch_vt1702},
-       { .id = 0x11063398, .name = "VIA VT1702",
+       { .id = 0x11063398, .name = "VT1702",
          .patch = patch_vt1702},
-       { .id = 0x11064398, .name = "VIA VT1702",
+       { .id = 0x11064398, .name = "VT1702",
          .patch = patch_vt1702},
-       { .id = 0x11065398, .name = "VIA VT1702",
+       { .id = 0x11065398, .name = "VT1702",
          .patch = patch_vt1702},
-       { .id = 0x11066398, .name = "VIA VT1702",
+       { .id = 0x11066398, .name = "VT1702",
          .patch = patch_vt1702},
-       { .id = 0x11067398, .name = "VIA VT1702",
+       { .id = 0x11067398, .name = "VT1702",
          .patch = patch_vt1702},
        {} /* terminator */
 };
+
+MODULE_ALIAS("snd-hda-codec-id:1106*");
+
+static struct hda_codec_preset_list via_list = {
+       .preset = snd_hda_preset_via,
+       .owner = THIS_MODULE,
+};
+
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("VIA HD-audio codec");
+
+static int __init patch_via_init(void)
+{
+       return snd_hda_add_codec_preset(&via_list);
+}
+
+static void __exit patch_via_exit(void)
+{
+       snd_hda_delete_codec_preset(&via_list);
+}
+
+module_init(patch_via_init)
+module_exit(patch_via_exit)