ALSA: HDA VIA: Modify vt1709_auto_create_multi_out_ctls.
[deliverable/linux.git] / sound / pci / hda / patch_via.c
index ee89db90c9b62913758a480dd2adb228aa69444d..f9702a17fc16f83904d1be330571cc7d072204c8 100644 (file)
 #define VT1702_HP_NID          0x17
 #define VT1702_DIGOUT_NID      0x11
 
-#define IS_VT1708_VENDORID(x)          ((x) >= 0x11061708 && (x) <= 0x1106170b)
-#define IS_VT1709_10CH_VENDORID(x)     ((x) >= 0x1106e710 && (x) <= 0x1106e713)
-#define IS_VT1709_6CH_VENDORID(x)      ((x) >= 0x1106e714 && (x) <= 0x1106e717)
-#define IS_VT1708B_8CH_VENDORID(x)     ((x) >= 0x1106e720 && (x) <= 0x1106e723)
-#define IS_VT1708B_4CH_VENDORID(x)     ((x) >= 0x1106e724 && (x) <= 0x1106e727)
-#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,
@@ -92,12 +84,72 @@ enum VIA_HDA_CODEC {
        VT1708B_8CH,
        VT1708B_4CH,
        VT1708S,
+       VT1708BCE,
        VT1702,
        CODEC_TYPES,
 };
 
-static enum VIA_HDA_CODEC get_codec_type(u32 vendor_id)
+struct via_spec {
+       /* codec parameterization */
+       struct snd_kcontrol_new *mixers[4];
+       unsigned int num_mixers;
+
+       struct hda_verb *init_verbs[5];
+       unsigned int num_iverbs;
+
+       char *stream_name_analog;
+       struct hda_pcm_stream *stream_analog_playback;
+       struct hda_pcm_stream *stream_analog_capture;
+
+       char *stream_name_digital;
+       struct hda_pcm_stream *stream_digital_playback;
+       struct hda_pcm_stream *stream_digital_capture;
+
+       /* playback */
+       struct hda_multi_out multiout;
+       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;
+       unsigned int cur_mux[3];
+
+       /* PCM information */
+       struct hda_pcm pcm_rec[3];
+
+       /* dynamic controls, init_verbs and input_mux */
+       struct auto_pin_cfg autocfg;
+       struct snd_array kctls;
+       struct hda_input_mux private_imux[2];
+       hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
+
+       /* HP mode source */
+       const struct hda_input_mux *hp_mux;
+       unsigned int hp_independent_mode;
+       unsigned int hp_independent_mode_index;
+       unsigned int smart51_enabled;
+
+       enum VIA_HDA_CODEC codec_type;
+
+       /* work to check hp jack state */
+       struct hda_codec *codec;
+       struct delayed_work vt1708_hp_work;
+       int vt1708_jack_detectect;
+       int vt1708_hp_present;
+#ifdef CONFIG_SND_HDA_POWER_SAVE
+       struct hda_loopback_check loopback;
+#endif
+};
+
+static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec)
 {
+       u32 vendor_id = codec->vendor_id;
        u16 ven_id = vendor_id >> 16;
        u16 dev_id = vendor_id & 0xffff;
        enum VIA_HDA_CODEC codec_type;
@@ -111,9 +163,11 @@ static enum VIA_HDA_CODEC get_codec_type(u32 vendor_id)
                codec_type = VT1709_10CH;
        else if (dev_id >= 0xe714 && dev_id <= 0xe717)
                codec_type = VT1709_6CH;
-       else if (dev_id >= 0xe720 && dev_id <= 0xe723)
+       else if (dev_id >= 0xe720 && dev_id <= 0xe723) {
                codec_type = VT1708B_8CH;
-       else if (dev_id >= 0xe724 && dev_id <= 0xe727)
+               if (snd_hda_param_read(codec, 0x16, AC_PAR_CONNLIST_LEN) == 0x7)
+                       codec_type = VT1708BCE;
+       } else if (dev_id >= 0xe724 && dev_id <= 0xe727)
                codec_type = VT1708B_4CH;
        else if ((dev_id & 0xfff) == 0x397
                 && (dev_id >> 12) < 8)
@@ -128,10 +182,12 @@ static enum VIA_HDA_CODEC get_codec_type(u32 vendor_id)
 
 #define VIA_HP_EVENT           0x01
 #define VIA_GPIO_EVENT         0x02
+#define VIA_JACK_EVENT         0x04
 
 enum {
        VIA_CTL_WIDGET_VOL,
        VIA_CTL_WIDGET_MUTE,
+       VIA_CTL_WIDGET_ANALOG_MUTE,
 };
 
 enum {
@@ -149,7 +205,7 @@ static int mic_boost_tlv(struct snd_kcontrol *kcontrol, int op_flag,
        struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
        hda_nid_t nid = get_amp_nid(kcontrol);
 
-       if (get_codec_type(codec->vendor_id) == VT1708S
+       if (get_codec_type(codec) == VT1708S
            && (nid == 0x1a || nid == 0x1e)) {
                if (size < 4 * sizeof(unsigned int))
                        return -ENOMEM;
@@ -171,7 +227,7 @@ static int mic_boost_volume_info(struct snd_kcontrol *kcontrol,
        struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
        hda_nid_t nid = get_amp_nid(kcontrol);
 
-       if (get_codec_type(codec->vendor_id) == VT1708S
+       if (get_codec_type(codec) == VT1708S
            && (nid == 0x1a || nid == 0x1e)) {
                uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
                uinfo->count = 2;
@@ -181,61 +237,68 @@ static int mic_boost_volume_info(struct snd_kcontrol *kcontrol,
        return 0;
 }
 
-static struct snd_kcontrol_new vt1708_control_templates[] = {
-       HDA_CODEC_VOLUME(NULL, 0, 0, 0),
-       HDA_CODEC_MUTE(NULL, 0, 0, 0),
-};
-
-
-struct via_spec {
-       /* codec parameterization */
-       struct snd_kcontrol_new *mixers[3];
-       unsigned int num_mixers;
+static void analog_low_current_mode(struct hda_codec *codec, int stream_idle);
+static void set_jack_power_state(struct hda_codec *codec);
+static int is_aa_path_mute(struct hda_codec *codec);
 
-       struct hda_verb *init_verbs[5];
-       unsigned int num_iverbs;
-
-       char *stream_name_analog;
-       struct hda_pcm_stream *stream_analog_playback;
-       struct hda_pcm_stream *stream_analog_capture;
-
-       char *stream_name_digital;
-       struct hda_pcm_stream *stream_digital_playback;
-       struct hda_pcm_stream *stream_digital_capture;
-
-       /* playback */
-       struct hda_multi_out multiout;
-       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;
+static void vt1708_start_hp_work(struct via_spec *spec)
+{
+       if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
+               return;
+       snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81,
+                           !spec->vt1708_jack_detectect);
+       if (!delayed_work_pending(&spec->vt1708_hp_work))
+               schedule_delayed_work(&spec->vt1708_hp_work,
+                                     msecs_to_jiffies(100));
+}
 
-       /* capture source */
-       const struct hda_input_mux *input_mux;
-       unsigned int cur_mux[3];
+static void vt1708_stop_hp_work(struct via_spec *spec)
+{
+       if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
+               return;
+       if (snd_hda_get_bool_hint(spec->codec, "analog_loopback_hp_detect") == 1
+           && !is_aa_path_mute(spec->codec))
+               return;
+       snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81,
+                           !spec->vt1708_jack_detectect);
+       cancel_delayed_work(&spec->vt1708_hp_work);
+       flush_scheduled_work();
+}
 
-       /* PCM information */
-       struct hda_pcm pcm_rec[3];
+static int analog_input_switch_put(struct snd_kcontrol *kcontrol,
+                                  struct snd_ctl_elem_value *ucontrol)
+{
+       int change = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
+       struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
 
-       /* dynamic controls, init_verbs and input_mux */
-       struct auto_pin_cfg autocfg;
-       struct snd_array kctls;
-       struct hda_input_mux private_imux[2];
-       hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
+       set_jack_power_state(codec);
+       analog_low_current_mode(snd_kcontrol_chip(kcontrol), -1);
+       if (snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") == 1) {
+               if (is_aa_path_mute(codec))
+                       vt1708_start_hp_work(codec->spec);
+               else
+                       vt1708_stop_hp_work(codec->spec);
+       }
+       return change;
+}
 
-       /* HP mode source */
-       const struct hda_input_mux *hp_mux;
-       unsigned int hp_independent_mode;
+/* modify .put = snd_hda_mixer_amp_switch_put */
+#define ANALOG_INPUT_MUTE                                              \
+       {               .iface = SNDRV_CTL_ELEM_IFACE_MIXER,            \
+                       .name = NULL,                                   \
+                       .index = 0,                                     \
+                       .info = snd_hda_mixer_amp_switch_info,          \
+                       .get = snd_hda_mixer_amp_switch_get,            \
+                       .put = analog_input_switch_put,                 \
+                       .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0) }
 
-#ifdef CONFIG_SND_HDA_POWER_SAVE
-       struct hda_loopback_check loopback;
-#endif
+static struct snd_kcontrol_new vt1708_control_templates[] = {
+       HDA_CODEC_VOLUME(NULL, 0, 0, 0),
+       HDA_CODEC_MUTE(NULL, 0, 0, 0),
+       ANALOG_INPUT_MUTE,
 };
 
+
 static hda_nid_t vt1708_adc_nids[2] = {
        /* ADC1-2 */
        0x15, 0x27
@@ -293,8 +356,8 @@ static void via_free_kctls(struct hda_codec *codec)
 }
 
 /* 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)
+static int via_new_analog_input(struct via_spec *spec, const char *ctlname,
+                               int idx, int mix_nid)
 {
        char name[32];
        int err;
@@ -305,7 +368,7 @@ static int via_new_analog_input(struct via_spec *spec, hda_nid_t pin,
        if (err < 0)
                return err;
        sprintf(name, "%s Playback Switch", ctlname);
-       err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
+       err = via_add_control(spec, VIA_CTL_WIDGET_ANALOG_MUTE, name,
                              HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
        if (err < 0)
                return err;
@@ -364,6 +427,134 @@ static void via_auto_init_analog_input(struct hda_codec *codec)
 
        }
 }
+
+static int is_smart51_pins(struct via_spec *spec, hda_nid_t pin);
+
+static void set_pin_power_state(struct hda_codec *codec, hda_nid_t nid,
+                               unsigned int *affected_parm)
+{
+       unsigned parm;
+       unsigned def_conf = snd_hda_codec_get_pincfg(codec, nid);
+       unsigned no_presence = (def_conf & AC_DEFCFG_MISC)
+               >> AC_DEFCFG_MISC_SHIFT
+               & AC_DEFCFG_MISC_NO_PRESENCE; /* do not support pin sense */
+       unsigned present = snd_hda_codec_read(codec, nid, 0,
+                                             AC_VERB_GET_PIN_SENSE, 0) >> 31;
+       struct via_spec *spec = codec->spec;
+       if ((spec->smart51_enabled && is_smart51_pins(spec, nid))
+           || ((no_presence || present)
+               && get_defcfg_connect(def_conf) != AC_JACK_PORT_NONE)) {
+               *affected_parm = AC_PWRST_D0; /* if it's connected */
+               parm = AC_PWRST_D0;
+       } else
+               parm = AC_PWRST_D3;
+
+       snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, parm);
+}
+
+static void set_jack_power_state(struct hda_codec *codec)
+{
+       struct via_spec *spec = codec->spec;
+       int imux_is_smixer;
+       unsigned int parm;
+
+       if (spec->codec_type == VT1702) {
+               imux_is_smixer = snd_hda_codec_read(
+                       codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
+               /* inputs */
+               /* PW 1/2/5 (14h/15h/18h) */
+               parm = AC_PWRST_D3;
+               set_pin_power_state(codec, 0x14, &parm);
+               set_pin_power_state(codec, 0x15, &parm);
+               set_pin_power_state(codec, 0x18, &parm);
+               if (imux_is_smixer)
+                       parm = AC_PWRST_D0; /* SW0 = stereo mixer (idx 3) */
+               /* SW0 (13h), AIW 0/1/2 (12h/1fh/20h) */
+               snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE,
+                                   parm);
+               snd_hda_codec_write(codec, 0x12, 0, AC_VERB_SET_POWER_STATE,
+                                   parm);
+               snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE,
+                                   parm);
+               snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_POWER_STATE,
+                                   parm);
+
+               /* outputs */
+               /* PW 3/4 (16h/17h) */
+               parm = AC_PWRST_D3;
+               set_pin_power_state(codec, 0x16, &parm);
+               set_pin_power_state(codec, 0x17, &parm);
+               /* MW0 (1ah), AOW 0/1 (10h/1dh) */
+               snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_POWER_STATE,
+                                   imux_is_smixer ? AC_PWRST_D0 : parm);
+               snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE,
+                                   parm);
+               snd_hda_codec_write(codec, 0x1d, 0, AC_VERB_SET_POWER_STATE,
+                                   parm);
+       } else if (spec->codec_type == VT1708B_8CH
+                  || spec->codec_type == VT1708B_4CH
+                  || spec->codec_type == VT1708S) {
+               /* SW0 (17h) = stereo mixer */
+               int is_8ch = spec->codec_type != VT1708B_4CH;
+               imux_is_smixer = snd_hda_codec_read(
+                       codec, 0x17, 0, AC_VERB_GET_CONNECT_SEL, 0x00)
+                       == ((spec->codec_type == VT1708S)  ? 5 : 0);
+               /* inputs */
+               /* PW 1/2/5 (1ah/1bh/1eh) */
+               parm = AC_PWRST_D3;
+               set_pin_power_state(codec, 0x1a, &parm);
+               set_pin_power_state(codec, 0x1b, &parm);
+               set_pin_power_state(codec, 0x1e, &parm);
+               if (imux_is_smixer)
+                       parm = AC_PWRST_D0;
+               /* SW0 (17h), AIW 0/1 (13h/14h) */
+               snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_POWER_STATE,
+                                   parm);
+               snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE,
+                                   parm);
+               snd_hda_codec_write(codec, 0x14, 0, AC_VERB_SET_POWER_STATE,
+                                   parm);
+
+               /* outputs */
+               /* PW0 (19h), SW1 (18h), AOW1 (11h) */
+               parm = AC_PWRST_D3;
+               set_pin_power_state(codec, 0x19, &parm);
+               snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE,
+                                   parm);
+               snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE,
+                                   parm);
+
+               /* PW6 (22h), SW2 (26h), AOW2 (24h) */
+               if (is_8ch) {
+                       parm = AC_PWRST_D3;
+                       set_pin_power_state(codec, 0x22, &parm);
+                       snd_hda_codec_write(codec, 0x26, 0,
+                                           AC_VERB_SET_POWER_STATE, parm);
+                       snd_hda_codec_write(codec, 0x24, 0,
+                                           AC_VERB_SET_POWER_STATE, parm);
+               }
+
+               /* PW 3/4/7 (1ch/1dh/23h) */
+               parm = AC_PWRST_D3;
+               /* force to D0 for internal Speaker */
+               set_pin_power_state(codec, 0x1c, &parm);
+               set_pin_power_state(codec, 0x1d, &parm);
+               if (is_8ch)
+                       set_pin_power_state(codec, 0x23, &parm);
+               /* MW0 (16h), Sw3 (27h), AOW 0/3 (10h/25h) */
+               snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_POWER_STATE,
+                                   imux_is_smixer ? AC_PWRST_D0 : parm);
+               snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE,
+                                   parm);
+               if (is_8ch) {
+                       snd_hda_codec_write(codec, 0x25, 0,
+                                           AC_VERB_SET_POWER_STATE, parm);
+                       snd_hda_codec_write(codec, 0x27, 0,
+                                           AC_VERB_SET_POWER_STATE, parm);
+               }
+       }
+}
+
 /*
  * input MUX handling
  */
@@ -395,6 +586,14 @@ static int via_mux_enum_put(struct snd_kcontrol *kcontrol,
 
        if (!spec->mux_nids[adc_idx])
                return -EINVAL;
+       /* switch to D0 beofre change index */
+       if (snd_hda_codec_read(codec, spec->mux_nids[adc_idx], 0,
+                              AC_VERB_GET_POWER_STATE, 0x00) != AC_PWRST_D0)
+               snd_hda_codec_write(codec, spec->mux_nids[adc_idx], 0,
+                                   AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
+       /* update jack power state */
+       set_jack_power_state(codec);
+
        return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
                                     spec->mux_nids[adc_idx],
                                     &spec->cur_mux[adc_idx]);
@@ -423,6 +622,48 @@ static int via_independent_hp_get(struct snd_kcontrol *kcontrol,
        return 0;
 }
 
+static void activate_ctl(struct hda_codec *codec, const char *name, int active)
+{
+       struct snd_kcontrol *ctl = snd_hda_find_mixer_ctl(codec, name);
+       if (ctl) {
+               ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
+               ctl->vd[0].access |= active
+                       ? 0 : SNDRV_CTL_ELEM_ACCESS_INACTIVE;
+               snd_ctl_notify(codec->bus->card,
+                              SNDRV_CTL_EVENT_MASK_VALUE, &ctl->id);
+       }
+}
+
+static int update_side_mute_status(struct hda_codec *codec)
+{
+       /* mute side channel */
+       struct via_spec *spec = codec->spec;
+       unsigned int parm = spec->hp_independent_mode
+               ? AMP_OUT_MUTE : AMP_OUT_UNMUTE;
+       hda_nid_t sw3;
+
+       switch (spec->codec_type) {
+       case VT1708:
+               sw3 = 0x1b;
+               break;
+       case VT1709_10CH:
+               sw3 = 0x29;
+               break;
+       case VT1708B_8CH:
+       case VT1708S:
+               sw3 = 0x27;
+               break;
+       default:
+               sw3 = 0;
+               break;
+       }
+
+       if (sw3)
+               snd_hda_codec_write(codec, sw3, 0, AC_VERB_SET_AMP_GAIN_MUTE,
+                                   parm);
+       return 0;
+}
+
 static int via_independent_hp_put(struct snd_kcontrol *kcontrol,
                                  struct snd_ctl_elem_value *ucontrol)
 {
@@ -430,47 +671,26 @@ static int via_independent_hp_put(struct snd_kcontrol *kcontrol,
        struct via_spec *spec = codec->spec;
        hda_nid_t nid = spec->autocfg.hp_pins[0];
        unsigned int pinsel = ucontrol->value.enumerated.item[0];
-       unsigned int con_nid = snd_hda_codec_read(codec, nid, 0,
-                                        AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
-
-       if (con_nid == spec->multiout.hp_nid) {
-               if (pinsel == 0) {
-                       if (!spec->hp_independent_mode) {
-                               if (spec->multiout.num_dacs > 1)
-                                       spec->multiout.num_dacs -= 1;
-                               spec->hp_independent_mode = 1;
-                       }
-               } else if (pinsel == 1) {
-                      if (spec->hp_independent_mode) {
-                               if (spec->multiout.num_dacs > 1)
-                                       spec->multiout.num_dacs += 1;
-                               spec->hp_independent_mode = 0;
-                      }
-               }
-       } else {
-               if (pinsel == 0) {
-                       if (spec->hp_independent_mode) {
-                               if (spec->multiout.num_dacs > 1)
-                                       spec->multiout.num_dacs += 1;
-                               spec->hp_independent_mode = 0;
-                       }
-               } else if (pinsel == 1) {
-                      if (!spec->hp_independent_mode) {
-                               if (spec->multiout.num_dacs > 1)
-                                       spec->multiout.num_dacs -= 1;
-                               spec->hp_independent_mode = 1;
-                      }
-               }
+       /* Get Independent Mode index of headphone pin widget */
+       spec->hp_independent_mode = spec->hp_independent_mode_index == pinsel
+               ? 1 : 0;
+
+       snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, pinsel);
+
+       if (spec->multiout.hp_nid && spec->multiout.hp_nid
+           != spec->multiout.dac_nids[HDA_FRONT])
+               snd_hda_codec_setup_stream(codec, spec->multiout.hp_nid,
+                                          0, 0, 0);
+
+       update_side_mute_status(codec);
+       /* update HP volume/swtich active state */
+       if (spec->codec_type == VT1708S
+           || spec->codec_type == VT1702) {
+               activate_ctl(codec, "Headphone Playback Volume",
+                            spec->hp_independent_mode);
+               activate_ctl(codec, "Headphone Playback Switch",
+                            spec->hp_independent_mode);
        }
-       snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
-                           pinsel);
-
-       if (spec->multiout.hp_nid &&
-           spec->multiout.hp_nid != spec->multiout.dac_nids[HDA_FRONT])
-                       snd_hda_codec_setup_stream(codec,
-                                                  spec->multiout.hp_nid,
-                                                  0, 0, 0);
-
        return 0;
 }
 
@@ -486,6 +706,167 @@ static struct snd_kcontrol_new via_hp_mixer[] = {
        { } /* end */
 };
 
+static void notify_aa_path_ctls(struct hda_codec *codec)
+{
+       int i;
+       struct snd_ctl_elem_id id;
+       const char *labels[] = {"Mic", "Front Mic", "Line"};
+
+       memset(&id, 0, sizeof(id));
+       id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
+       for (i = 0; i < ARRAY_SIZE(labels); i++) {
+               sprintf(id.name, "%s Playback Volume", labels[i]);
+               snd_ctl_notify(codec->bus->card, SNDRV_CTL_EVENT_MASK_VALUE,
+                              &id);
+       }
+}
+
+static void mute_aa_path(struct hda_codec *codec, int mute)
+{
+       struct via_spec *spec = codec->spec;
+       hda_nid_t  nid_mixer;
+       int start_idx;
+       int end_idx;
+       int i;
+       /* get nid of MW0 and start & end index */
+       switch (spec->codec_type) {
+       case VT1708:
+               nid_mixer = 0x17;
+               start_idx = 2;
+               end_idx = 4;
+               break;
+       case VT1709_10CH:
+       case VT1709_6CH:
+               nid_mixer = 0x18;
+               start_idx = 2;
+               end_idx = 4;
+               break;
+       case VT1708B_8CH:
+       case VT1708B_4CH:
+       case VT1708S:
+               nid_mixer = 0x16;
+               start_idx = 2;
+               end_idx = 4;
+               break;
+       default:
+               return;
+       }
+       /* check AA path's mute status */
+       for (i = start_idx; i <= end_idx; i++) {
+               int val = mute ? HDA_AMP_MUTE : HDA_AMP_UNMUTE;
+               snd_hda_codec_amp_stereo(codec, nid_mixer, HDA_INPUT, i,
+                                        HDA_AMP_MUTE, val);
+       }
+}
+static int is_smart51_pins(struct via_spec *spec, hda_nid_t pin)
+{
+       int res = 0;
+       int index;
+       for (index = AUTO_PIN_MIC; index < AUTO_PIN_FRONT_LINE; index++) {
+               if (pin == spec->autocfg.input_pins[index]) {
+                       res = 1;
+                       break;
+               }
+       }
+       return res;
+}
+
+static int via_smart51_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 via_smart51_get(struct snd_kcontrol *kcontrol,
+                          struct snd_ctl_elem_value *ucontrol)
+{
+       struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
+       struct via_spec *spec = codec->spec;
+       int index[] = { AUTO_PIN_MIC, AUTO_PIN_FRONT_MIC, AUTO_PIN_LINE };
+       int on = 1;
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(index); i++) {
+               hda_nid_t nid = spec->autocfg.input_pins[index[i]];
+               if (nid) {
+                       int ctl =
+                           snd_hda_codec_read(codec, nid, 0,
+                                              AC_VERB_GET_PIN_WIDGET_CONTROL,
+                                              0);
+                       if (i == AUTO_PIN_FRONT_MIC
+                           && spec->hp_independent_mode)
+                               continue; /* ignore FMic for independent HP */
+                       if (ctl & AC_PINCTL_IN_EN
+                           && !(ctl & AC_PINCTL_OUT_EN))
+                               on = 0;
+               }
+       }
+       *ucontrol->value.integer.value = on;
+       return 0;
+}
+
+static int via_smart51_put(struct snd_kcontrol *kcontrol,
+                          struct snd_ctl_elem_value *ucontrol)
+{
+       struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
+       struct via_spec *spec = codec->spec;
+       int out_in = *ucontrol->value.integer.value
+               ? AC_PINCTL_OUT_EN : AC_PINCTL_IN_EN;
+       int index[] = { AUTO_PIN_MIC, AUTO_PIN_FRONT_MIC, AUTO_PIN_LINE };
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(index); i++) {
+               hda_nid_t nid = spec->autocfg.input_pins[index[i]];
+               if (i == AUTO_PIN_FRONT_MIC
+                   && spec->hp_independent_mode)
+                       continue; /* don't retask FMic for independent HP */
+               if (nid) {
+                       unsigned int parm = snd_hda_codec_read(
+                               codec, nid, 0,
+                               AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
+                       parm &= ~(AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN);
+                       parm |= out_in;
+                       snd_hda_codec_write(codec, nid, 0,
+                                           AC_VERB_SET_PIN_WIDGET_CONTROL,
+                                           parm);
+                       if (out_in == AC_PINCTL_OUT_EN) {
+                               mute_aa_path(codec, 1);
+                               notify_aa_path_ctls(codec);
+                       }
+               }
+               if (i == AUTO_PIN_FRONT_MIC) {
+                       if (spec->codec_type == VT1708S) {
+                               /* input = index 1 (AOW3) */
+                               snd_hda_codec_write(
+                                       codec, nid, 0,
+                                       AC_VERB_SET_CONNECT_SEL, 1);
+                               snd_hda_codec_amp_stereo(
+                                       codec, nid, HDA_OUTPUT,
+                                       0, HDA_AMP_MUTE, HDA_AMP_UNMUTE);
+                       }
+               }
+       }
+       spec->smart51_enabled = *ucontrol->value.integer.value;
+       set_jack_power_state(codec);
+       return 1;
+}
+
+static struct snd_kcontrol_new via_smart51_mixer[] = {
+       {
+        .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
+        .name = "Smart 5.1",
+        .count = 1,
+        .info = via_smart51_info,
+        .get = via_smart51_get,
+        .put = via_smart51_put,
+        },
+       {}                      /* end */
+};
+
 /* capture mixer elements */
 static struct snd_kcontrol_new vt1708_capture_mixer[] = {
        HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_INPUT),
@@ -506,6 +887,93 @@ static struct snd_kcontrol_new vt1708_capture_mixer[] = {
        },
        { } /* end */
 };
+
+/* check AA path's mute statue */
+static int is_aa_path_mute(struct hda_codec *codec)
+{
+       int mute = 1;
+       hda_nid_t  nid_mixer;
+       int start_idx;
+       int end_idx;
+       int i;
+       struct via_spec *spec = codec->spec;
+       /* get nid of MW0 and start & end index */
+       switch (spec->codec_type) {
+       case VT1708B_8CH:
+       case VT1708B_4CH:
+       case VT1708S:
+               nid_mixer = 0x16;
+               start_idx = 2;
+               end_idx = 4;
+               break;
+       case VT1702:
+               nid_mixer = 0x1a;
+               start_idx = 1;
+               end_idx = 3;
+               break;
+       default:
+               return 0;
+       }
+       /* check AA path's mute status */
+       for (i = start_idx; i <= end_idx; i++) {
+               unsigned int con_list = snd_hda_codec_read(
+                       codec, nid_mixer, 0, AC_VERB_GET_CONNECT_LIST, i/4*4);
+               int shift = 8 * (i % 4);
+               hda_nid_t nid_pin = (con_list & (0xff << shift)) >> shift;
+               unsigned int defconf = snd_hda_codec_get_pincfg(codec, nid_pin);
+               if (get_defcfg_connect(defconf) == AC_JACK_PORT_COMPLEX) {
+                       /* check mute status while the pin is connected */
+                       int mute_l = snd_hda_codec_amp_read(codec, nid_mixer, 0,
+                                                           HDA_INPUT, i) >> 7;
+                       int mute_r = snd_hda_codec_amp_read(codec, nid_mixer, 1,
+                                                           HDA_INPUT, i) >> 7;
+                       if (!mute_l || !mute_r) {
+                               mute = 0;
+                               break;
+                       }
+               }
+       }
+       return mute;
+}
+
+/* enter/exit analog low-current mode */
+static void analog_low_current_mode(struct hda_codec *codec, int stream_idle)
+{
+       struct via_spec *spec = codec->spec;
+       static int saved_stream_idle = 1; /* saved stream idle status */
+       int enable = is_aa_path_mute(codec);
+       unsigned int verb = 0;
+       unsigned int parm = 0;
+
+       if (stream_idle == -1)  /* stream status did not change */
+               enable = enable && saved_stream_idle;
+       else {
+               enable = enable && stream_idle;
+               saved_stream_idle = stream_idle;
+       }
+
+       /* decide low current mode's verb & parameter */
+       switch (spec->codec_type) {
+       case VT1708B_8CH:
+       case VT1708B_4CH:
+               verb = 0xf70;
+               parm = enable ? 0x02 : 0x00; /* 0x02: 2/3x, 0x00: 1x */
+               break;
+       case VT1708S:
+               verb = 0xf73;
+               parm = enable ? 0x51 : 0xe1; /* 0x51: 4/28x, 0xe1: 1x */
+               break;
+       case VT1702:
+               verb = 0xf73;
+               parm = enable ? 0x01 : 0x1d; /* 0x01: 4/40x, 0x1d: 1x */
+               break;
+       default:
+               return;         /* other codecs are not supported */
+       }
+       /* send verb */
+       snd_hda_codec_write(codec, codec->afg, 0, verb, parm);
+}
+
 /*
  * generic initialization of ADC, input mixers and output mixers
  */
@@ -547,30 +1015,13 @@ static int via_playback_pcm_open(struct hda_pcm_stream *hinfo,
                                 struct snd_pcm_substream *substream)
 {
        struct via_spec *spec = codec->spec;
+       int idle = substream->pstr->substream_opened == 1
+               && substream->ref_count == 0;
+       analog_low_current_mode(codec, idle);
        return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
                                             hinfo);
 }
 
-static int via_playback_pcm_prepare(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;
-       return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
-                                               stream_tag, format, substream);
-}
-
-static int via_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
-                                   struct hda_codec *codec,
-                                   struct snd_pcm_substream *substream)
-{
-       struct via_spec *spec = codec->spec;
-       return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
-}
-
-
 static void playback_multi_pcm_prep_0(struct hda_codec *codec,
                                      unsigned int stream_tag,
                                      unsigned int format,
@@ -658,7 +1109,7 @@ static int via_playback_multi_pcm_prepare(struct hda_pcm_stream *hinfo,
                        snd_hda_codec_setup_stream(codec, mout->hp_nid,
                                                   stream_tag, 0, format);
        }
-
+       vt1708_start_hp_work(spec);
        return 0;
 }
 
@@ -698,7 +1149,7 @@ static int via_playback_multi_pcm_cleanup(struct hda_pcm_stream *hinfo,
                        snd_hda_codec_setup_stream(codec, mout->hp_nid,
                                                   0, 0, 0);
        }
-
+       vt1708_stop_hp_work(spec);
        return 0;
 }
 
@@ -779,7 +1230,7 @@ static struct hda_pcm_stream vt1708_pcm_analog_playback = {
 };
 
 static struct hda_pcm_stream vt1708_pcm_analog_s16_playback = {
-       .substreams = 1,
+       .substreams = 2,
        .channels_min = 2,
        .channels_max = 8,
        .nid = 0x10, /* NID to query formats and rates */
@@ -790,8 +1241,8 @@ static struct hda_pcm_stream vt1708_pcm_analog_s16_playback = {
        .formats = SNDRV_PCM_FMTBIT_S16_LE,
        .ops = {
                .open = via_playback_pcm_open,
-               .prepare = via_playback_pcm_prepare,
-               .cleanup = via_playback_pcm_cleanup
+               .prepare = via_playback_multi_pcm_prepare,
+               .cleanup = via_playback_multi_pcm_cleanup
        },
 };
 
@@ -853,6 +1304,11 @@ static int via_build_controls(struct hda_codec *codec)
                if (err < 0)
                        return err;
        }
+
+       /* init power states */
+       set_jack_power_state(codec);
+       analog_low_current_mode(codec, 1);
+
        via_free_kctls(codec); /* no longer needed */
        return 0;
 }
@@ -904,20 +1360,32 @@ static void via_free(struct hda_codec *codec)
                return;
 
        via_free_kctls(codec);
+       vt1708_stop_hp_work(spec);
        kfree(codec->spec);
 }
 
 /* mute internal speaker if HP is plugged */
 static void via_hp_automute(struct hda_codec *codec)
 {
-       unsigned int present;
+       unsigned int present = 0;
        struct via_spec *spec = codec->spec;
 
        present = snd_hda_codec_read(codec, spec->autocfg.hp_pins[0], 0,
                                     AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
-       snd_hda_codec_amp_stereo(codec, spec->autocfg.line_out_pins[0],
-                                HDA_OUTPUT, 0, HDA_AMP_MUTE,
-                                present ? HDA_AMP_MUTE : 0);
+
+       if (!spec->hp_independent_mode) {
+               struct snd_ctl_elem_id id;
+               /* auto mute */
+               snd_hda_codec_amp_stereo(
+                       codec, spec->autocfg.line_out_pins[0], HDA_OUTPUT, 0,
+                       HDA_AMP_MUTE, present ? HDA_AMP_MUTE : 0);
+               /* notify change */
+               memset(&id, 0, sizeof(id));
+               id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
+               strcpy(id.name, "Front Playback Switch");
+               snd_ctl_notify(codec->bus->card, SNDRV_CTL_EVENT_MASK_VALUE,
+                              &id);
+       }
 }
 
 static void via_gpio_control(struct hda_codec *codec)
@@ -973,10 +1441,12 @@ static void via_unsol_event(struct hda_codec *codec,
                                  unsigned int res)
 {
        res >>= 26;
-       if (res == VIA_HP_EVENT)
+       if (res & VIA_HP_EVENT)
                via_hp_automute(codec);
-       else if (res == VIA_GPIO_EVENT)
+       if (res & VIA_GPIO_EVENT)
                via_gpio_control(codec);
+       if (res & VIA_JACK_EVENT)
+               set_jack_power_state(codec);
 }
 
 static int via_init(struct hda_codec *codec)
@@ -986,6 +1456,10 @@ static int via_init(struct hda_codec *codec)
        for (i = 0; i < spec->num_iverbs; i++)
                snd_hda_sequence_write(codec, spec->init_verbs[i]);
 
+       spec->codec_type = get_codec_type(codec);
+       if (spec->codec_type == VT1708BCE)
+               spec->codec_type = VT1708S; /* VT1708BCE & VT1708S are almost
+                                              same */
        /* Lydia Add for EAPD enable */
        if (!spec->dig_in_nid) { /* No Digital In connection */
                if (spec->dig_in_pin) {
@@ -1006,6 +1480,15 @@ static int via_init(struct hda_codec *codec)
        return 0;
 }
 
+#ifdef SND_HDA_NEEDS_RESUME
+static int via_suspend(struct hda_codec *codec, pm_message_t state)
+{
+       struct via_spec *spec = codec->spec;
+       vt1708_stop_hp_work(spec);
+       return 0;
+}
+#endif
+
 #ifdef CONFIG_SND_HDA_POWER_SAVE
 static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
 {
@@ -1021,6 +1504,9 @@ static struct hda_codec_ops via_patch_ops = {
        .build_pcms = via_build_pcms,
        .init = via_init,
        .free = via_free,
+#ifdef SND_HDA_NEEDS_RESUME
+       .suspend = via_suspend,
+#endif
 #ifdef CONFIG_SND_HDA_POWER_SAVE
        .check_power_status = via_check_power_status,
 #endif
@@ -1067,7 +1553,7 @@ static int vt1708_auto_create_multi_out_ctls(struct via_spec *spec,
 {
        char name[32];
        static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
-       hda_nid_t nid, nid_vol = 0;
+       hda_nid_t nid, nid_vol, nid_vols[] = {0x17, 0x19, 0x1a, 0x1b};
        int i, err;
 
        for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
@@ -1076,8 +1562,7 @@ static int vt1708_auto_create_multi_out_ctls(struct via_spec *spec,
                if (!nid)
                        continue;
                
-               if (i != AUTO_SEQ_FRONT)
-                       nid_vol = 0x18 + i;
+               nid_vol = nid_vols[i];
 
                if (i == AUTO_SEQ_CENLFE) {
                        /* Center/LFE */
@@ -1109,13 +1594,13 @@ static int vt1708_auto_create_multi_out_ctls(struct via_spec *spec,
                        /* add control to mixer index 0 */
                        err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
                                              "Master Front Playback Volume",
-                                             HDA_COMPOSE_AMP_VAL(0x17, 3, 0,
+                                             HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
                                                                  HDA_INPUT));
                        if (err < 0)
                                return err;
                        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
                                              "Master Front Playback Switch",
-                                             HDA_COMPOSE_AMP_VAL(0x17, 3, 0,
+                                             HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
                                                                  HDA_INPUT));
                        if (err < 0)
                                return err;
@@ -1178,6 +1663,7 @@ static int vt1708_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
                return 0;
 
        spec->multiout.hp_nid = VT1708_HP_NID; /* AOW3 */
+       spec->hp_independent_mode_index = 1;
 
        err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
                              "Headphone Playback Volume",
@@ -1231,8 +1717,7 @@ static int vt1708_auto_create_analog_input_ctls(struct via_spec *spec,
                        idx = 1;
                        break;
                }
-               err = via_new_analog_input(spec, cfg->input_pins[i], labels[i],
-                                          idx, 0x17);
+               err = via_new_analog_input(spec, labels[i], idx, 0x17);
                if (err < 0)
                        return err;
                imux->items[imux->num_items].label = labels[i];
@@ -1260,16 +1745,60 @@ static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
        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_set_pincfg(codec, nid, def_conf);
-               }
+       if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE
+           && (seqassoc == 0xf0 || seqassoc == 0xff)) {
+               def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
+               snd_hda_codec_set_pincfg(codec, nid, def_conf);
        }
 
        return;
 }
 
+static int vt1708_jack_detectect_get(struct snd_kcontrol *kcontrol,
+                                    struct snd_ctl_elem_value *ucontrol)
+{
+       struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
+       struct via_spec *spec = codec->spec;
+
+       if (spec->codec_type != VT1708)
+               return 0;
+       spec->vt1708_jack_detectect =
+               !((snd_hda_codec_read(codec, 0x1, 0, 0xf84, 0) >> 8) & 0x1);
+       ucontrol->value.integer.value[0] = spec->vt1708_jack_detectect;
+       return 0;
+}
+
+static int vt1708_jack_detectect_put(struct snd_kcontrol *kcontrol,
+                                    struct snd_ctl_elem_value *ucontrol)
+{
+       struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
+       struct via_spec *spec = codec->spec;
+       int change;
+
+       if (spec->codec_type != VT1708)
+               return 0;
+       spec->vt1708_jack_detectect = ucontrol->value.integer.value[0];
+       change = (0x1 & (snd_hda_codec_read(codec, 0x1, 0, 0xf84, 0) >> 8))
+               == !spec->vt1708_jack_detectect;
+       if (spec->vt1708_jack_detectect) {
+               mute_aa_path(codec, 1);
+               notify_aa_path_ctls(codec);
+       }
+       return change;
+}
+
+static struct snd_kcontrol_new vt1708_jack_detectect[] = {
+       {
+               .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
+               .name = "Jack Detect",
+               .count = 1,
+               .info = snd_ctl_boolean_mono_info,
+               .get = vt1708_jack_detectect_get,
+               .put = vt1708_jack_detectect_put,
+       },
+       {} /* end */
+};
+
 static int vt1708_parse_auto_config(struct hda_codec *codec)
 {
        struct via_spec *spec = codec->spec;
@@ -1295,6 +1824,10 @@ static int vt1708_parse_auto_config(struct hda_codec *codec)
        if (err < 0)
                return err;
        err = vt1708_auto_create_analog_input_ctls(spec, &spec->autocfg);
+       if (err < 0)
+               return err;
+       /* add jack detect on/off control */
+       err = snd_hda_add_new_ctls(codec, vt1708_jack_detectect);
        if (err < 0)
                return err;
 
@@ -1316,6 +1849,7 @@ static int vt1708_parse_auto_config(struct hda_codec *codec)
        if (spec->hp_mux)
                spec->mixers[spec->num_mixers++] = via_hp_mixer;
 
+       spec->mixers[spec->num_mixers++] = via_smart51_mixer;
        return 1;
 }
 
@@ -1329,6 +1863,22 @@ static int via_auto_init(struct hda_codec *codec)
        return 0;
 }
 
+static void vt1708_update_hp_jack_state(struct work_struct *work)
+{
+       struct via_spec *spec = container_of(work, struct via_spec,
+                                            vt1708_hp_work.work);
+       if (spec->codec_type != VT1708)
+               return;
+       /* if jack state toggled */
+       if (spec->vt1708_hp_present
+           != (snd_hda_codec_read(spec->codec, spec->autocfg.hp_pins[0], 0,
+                                  AC_VERB_GET_PIN_SENSE, 0) >> 31)) {
+               spec->vt1708_hp_present ^= 1;
+               via_hp_automute(spec->codec);
+       }
+       vt1708_start_hp_work(spec);
+}
+
 static int get_mux_nids(struct hda_codec *codec)
 {
        struct via_spec *spec = codec->spec;
@@ -1405,7 +1955,8 @@ static int patch_vt1708(struct hda_codec *codec)
 #ifdef CONFIG_SND_HDA_POWER_SAVE
        spec->loopback.amplist = vt1708_loopbacks;
 #endif
-
+       spec->codec = codec;
+       INIT_DELAYED_WORK(&spec->vt1708_hp_work, vt1708_update_hp_jack_state);
        return 0;
 }
 
@@ -1433,7 +1984,8 @@ static struct snd_kcontrol_new vt1709_capture_mixer[] = {
 };
 
 static struct hda_verb vt1709_uniwill_init_verbs[] = {
-       {0x20, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_HP_EVENT},
+       {0x20, AC_VERB_SET_UNSOLICITED_ENABLE,
+        AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
        { }
 };
 
@@ -1487,8 +2039,8 @@ static struct hda_pcm_stream vt1709_10ch_pcm_analog_playback = {
        .nid = 0x10, /* NID to query formats and rates */
        .ops = {
                .open = via_playback_pcm_open,
-               .prepare = via_playback_pcm_prepare,
-               .cleanup = via_playback_pcm_cleanup
+               .prepare = via_playback_multi_pcm_prepare,
+               .cleanup = via_playback_multi_pcm_cleanup,
        },
 };
 
@@ -1499,8 +2051,8 @@ static struct hda_pcm_stream vt1709_6ch_pcm_analog_playback = {
        .nid = 0x10, /* NID to query formats and rates */
        .ops = {
                .open = via_playback_pcm_open,
-               .prepare = via_playback_pcm_prepare,
-               .cleanup = via_playback_pcm_cleanup
+               .prepare = via_playback_multi_pcm_prepare,
+               .cleanup = via_playback_multi_pcm_cleanup,
        },
 };
 
@@ -1608,7 +2160,7 @@ static int vt1709_auto_create_multi_out_ctls(struct via_spec *spec,
 {
        char name[32];
        static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
-       hda_nid_t nid = 0;
+       hda_nid_t nid, nid_vol, nid_vols[] = {0x18, 0x1a, 0x1b, 0x29};
        int i, err;
 
        for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
@@ -1617,43 +2169,45 @@ static int vt1709_auto_create_multi_out_ctls(struct via_spec *spec,
                if (!nid)       
                        continue;
 
+               nid_vol = nid_vols[i];
+
                if (i == AUTO_SEQ_CENLFE) {
                        /* Center/LFE */
                        err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
                                              "Center Playback Volume",
-                                             HDA_COMPOSE_AMP_VAL(0x1b, 1, 0,
+                                             HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
                                                                  HDA_OUTPUT));
                        if (err < 0)
                                return err;
                        err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
                                              "LFE Playback Volume",
-                                             HDA_COMPOSE_AMP_VAL(0x1b, 2, 0,
+                                             HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
                                                                  HDA_OUTPUT));
                        if (err < 0)
                                return err;
                        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
                                              "Center Playback Switch",
-                                             HDA_COMPOSE_AMP_VAL(0x1b, 1, 0,
+                                             HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
                                                                  HDA_OUTPUT));
                        if (err < 0)
                                return err;
                        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
                                              "LFE Playback Switch",
-                                             HDA_COMPOSE_AMP_VAL(0x1b, 2, 0,
+                                             HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
                                                                  HDA_OUTPUT));
                        if (err < 0)
                                return err;
                } else if (i == AUTO_SEQ_FRONT){
-                       /* add control to mixer index 0 */
+                       /* ADD control to mixer index 0 */
                        err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
                                              "Master Front Playback Volume",
-                                             HDA_COMPOSE_AMP_VAL(0x18, 3, 0,
+                                             HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
                                                                  HDA_INPUT));
                        if (err < 0)
                                return err;
                        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
                                              "Master Front Playback Switch",
-                                             HDA_COMPOSE_AMP_VAL(0x18, 3, 0,
+                                             HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
                                                                  HDA_INPUT));
                        if (err < 0)
                                return err;
@@ -1674,26 +2228,26 @@ static int vt1709_auto_create_multi_out_ctls(struct via_spec *spec,
                } else if (i == AUTO_SEQ_SURROUND) {
                        sprintf(name, "%s Playback Volume", chname[i]);
                        err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
-                                             HDA_COMPOSE_AMP_VAL(0x1a, 3, 0,
+                                             HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
                                                                  HDA_OUTPUT));
                        if (err < 0)
                                return err;
                        sprintf(name, "%s Playback Switch", chname[i]);
                        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
-                                             HDA_COMPOSE_AMP_VAL(0x1a, 3, 0,
+                                             HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
                                                                  HDA_OUTPUT));
                        if (err < 0)
                                return err;
                } else if (i == AUTO_SEQ_SIDE) {
                        sprintf(name, "%s Playback Volume", chname[i]);
                        err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
-                                             HDA_COMPOSE_AMP_VAL(0x29, 3, 0,
+                                             HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
                                                                  HDA_OUTPUT));
                        if (err < 0)
                                return err;
                        sprintf(name, "%s Playback Switch", chname[i]);
                        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
-                                             HDA_COMPOSE_AMP_VAL(0x29, 3, 0,
+                                             HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
                                                                  HDA_OUTPUT));
                        if (err < 0)
                                return err;
@@ -1714,6 +2268,7 @@ static int vt1709_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
                spec->multiout.hp_nid = VT1709_HP_DAC_NID;
        else if (spec->multiout.num_dacs == 3) /* 6 channels */
                spec->multiout.hp_nid = 0;
+       spec->hp_independent_mode_index = 1;
 
        err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
                              "Headphone Playback Volume",
@@ -1765,8 +2320,7 @@ static int vt1709_auto_create_analog_input_ctls(struct via_spec *spec,
                        idx = 1;
                        break;
                }
-               err = via_new_analog_input(spec, cfg->input_pins[i], labels[i],
-                                          idx, 0x18);
+               err = via_new_analog_input(spec, labels[i], idx, 0x18);
                if (err < 0)
                        return err;
                imux->items[imux->num_items].label = labels[i];
@@ -1816,6 +2370,7 @@ static int vt1709_parse_auto_config(struct hda_codec *codec)
        if (spec->hp_mux)
                spec->mixers[spec->num_mixers++] = via_hp_mixer;
 
+       spec->mixers[spec->num_mixers++] = via_smart51_mixer;
        return 1;
 }
 
@@ -2068,10 +2623,29 @@ static struct hda_verb vt1708B_4ch_volume_init_verbs[] = {
 };
 
 static struct hda_verb vt1708B_uniwill_init_verbs[] = {
-       {0x1D, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_HP_EVENT},
+       {0x1d, AC_VERB_SET_UNSOLICITED_ENABLE,
+        AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
+       {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
+       {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
+       {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
+       {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
+       {0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
+       {0x22, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
+       {0x23, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
        { }
 };
 
+static int via_pcm_open_close(struct hda_pcm_stream *hinfo,
+                             struct hda_codec *codec,
+                             struct snd_pcm_substream *substream)
+{
+       int idle = substream->pstr->substream_opened == 1
+               && substream->ref_count == 0;
+
+       analog_low_current_mode(codec, idle);
+       return 0;
+}
+
 static struct hda_pcm_stream vt1708B_8ch_pcm_analog_playback = {
        .substreams = 2,
        .channels_min = 2,
@@ -2080,7 +2654,8 @@ static struct hda_pcm_stream vt1708B_8ch_pcm_analog_playback = {
        .ops = {
                .open = via_playback_pcm_open,
                .prepare = via_playback_multi_pcm_prepare,
-               .cleanup = via_playback_multi_pcm_cleanup
+               .cleanup = via_playback_multi_pcm_cleanup,
+               .close = via_pcm_open_close
        },
 };
 
@@ -2102,8 +2677,10 @@ static struct hda_pcm_stream vt1708B_pcm_analog_capture = {
        .channels_max = 2,
        .nid = 0x13, /* NID to query formats and rates */
        .ops = {
+               .open = via_pcm_open_close,
                .prepare = via_capture_pcm_prepare,
-               .cleanup = via_capture_pcm_cleanup
+               .cleanup = via_capture_pcm_cleanup,
+               .close = via_pcm_open_close
        },
 };
 
@@ -2260,6 +2837,7 @@ static int vt1708B_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
                return 0;
 
        spec->multiout.hp_nid = VT1708B_HP_NID; /* AOW3 */
+       spec->hp_independent_mode_index = 1;
 
        err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
                              "Headphone Playback Volume",
@@ -2313,8 +2891,7 @@ static int vt1708B_auto_create_analog_input_ctls(struct via_spec *spec,
                        idx = 1;
                        break;
                }
-               err = via_new_analog_input(spec, cfg->input_pins[i], labels[i],
-                                          idx, 0x16);
+               err = via_new_analog_input(spec, labels[i], idx, 0x16);
                if (err < 0)
                        return err;
                imux->items[imux->num_items].label = labels[i];
@@ -2364,6 +2941,7 @@ static int vt1708B_parse_auto_config(struct hda_codec *codec)
        if (spec->hp_mux)
                spec->mixers[spec->num_mixers++] = via_hp_mixer;
 
+       spec->mixers[spec->num_mixers++] = via_smart51_mixer;
        return 1;
 }
 
@@ -2376,12 +2954,14 @@ static struct hda_amp_list vt1708B_loopbacks[] = {
        { } /* end */
 };
 #endif
-
+static int patch_vt1708S(struct hda_codec *codec);
 static int patch_vt1708B_8ch(struct hda_codec *codec)
 {
        struct via_spec *spec;
        int err;
 
+       if (get_codec_type(codec) == VT1708BCE)
+               return patch_vt1708S(codec);
        /* create a codec specific record */
        spec = kzalloc(sizeof(*spec), GFP_KERNEL);
        if (spec == NULL)
@@ -2546,7 +3126,15 @@ static struct hda_verb vt1708S_volume_init_verbs[] = {
 };
 
 static struct hda_verb vt1708S_uniwill_init_verbs[] = {
-       {0x1D, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_HP_EVENT},
+       {0x1d, AC_VERB_SET_UNSOLICITED_ENABLE,
+        AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
+       {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
+       {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
+       {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
+       {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
+       {0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
+       {0x22, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
+       {0x23, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
        { }
 };
 
@@ -2557,8 +3145,9 @@ static struct hda_pcm_stream vt1708S_pcm_analog_playback = {
        .nid = 0x10, /* NID to query formats and rates */
        .ops = {
                .open = via_playback_pcm_open,
-               .prepare = via_playback_pcm_prepare,
-               .cleanup = via_playback_pcm_cleanup
+               .prepare = via_playback_multi_pcm_prepare,
+               .cleanup = via_playback_multi_pcm_cleanup,
+               .close = via_pcm_open_close
        },
 };
 
@@ -2568,8 +3157,10 @@ static struct hda_pcm_stream vt1708S_pcm_analog_capture = {
        .channels_max = 2,
        .nid = 0x13, /* NID to query formats and rates */
        .ops = {
+               .open = via_pcm_open_close,
                .prepare = via_capture_pcm_prepare,
-               .cleanup = via_capture_pcm_cleanup
+               .cleanup = via_capture_pcm_cleanup,
+               .close = via_pcm_open_close
        },
 };
 
@@ -2726,6 +3317,7 @@ static int vt1708S_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
                return 0;
 
        spec->multiout.hp_nid = VT1708S_HP_NID; /* AOW3 */
+       spec->hp_independent_mode_index = 1;
 
        err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
                              "Headphone Playback Volume",
@@ -2780,8 +3372,7 @@ static int vt1708S_auto_create_analog_input_ctls(struct via_spec *spec,
                        idx = 1;
                        break;
                }
-               err = via_new_analog_input(spec, cfg->input_pins[i], labels[i],
-                                          idx, 0x16);
+               err = via_new_analog_input(spec, labels[i], idx, 0x16);
                if (err < 0)
                        return err;
                imux->items[imux->num_items].label = labels[i];
@@ -2852,6 +3443,7 @@ static int vt1708S_parse_auto_config(struct hda_codec *codec)
        if (spec->hp_mux)
                spec->mixers[spec->num_mixers++] = via_hp_mixer;
 
+       spec->mixers[spec->num_mixers++] = via_smart51_mixer;
        return 1;
 }
 
@@ -2913,6 +3505,16 @@ static int patch_vt1708S(struct hda_codec *codec)
        spec->loopback.amplist = vt1708S_loopbacks;
 #endif
 
+       /* correct names for VT1708BCE */
+       if (get_codec_type(codec) == VT1708BCE) {
+               kfree(codec->chip_name);
+               codec->chip_name = kstrdup("VT1708BCE", GFP_KERNEL);
+               snprintf(codec->bus->card->mixername,
+                        sizeof(codec->bus->card->mixername),
+                        "%s %s", codec->vendor_name, codec->chip_name);
+               spec->stream_name_analog = "VT1708BCE Analog";
+               spec->stream_name_digital = "VT1708BCE Digital";
+       }
        return 0;
 }
 
@@ -2971,8 +3573,12 @@ static struct hda_verb vt1702_volume_init_verbs[] = {
 };
 
 static struct hda_verb vt1702_uniwill_init_verbs[] = {
-       {0x01, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_GPIO_EVENT},
-       {0x17, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_HP_EVENT},
+       {0x17, AC_VERB_SET_UNSOLICITED_ENABLE,
+        AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT},
+       {0x14, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
+       {0x15, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
+       {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
+       {0x18, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_JACK_EVENT},
        { }
 };
 
@@ -2984,7 +3590,8 @@ static struct hda_pcm_stream vt1702_pcm_analog_playback = {
        .ops = {
                .open = via_playback_pcm_open,
                .prepare = via_playback_multi_pcm_prepare,
-               .cleanup = via_playback_multi_pcm_cleanup
+               .cleanup = via_playback_multi_pcm_cleanup,
+               .close = via_pcm_open_close
        },
 };
 
@@ -2994,8 +3601,10 @@ static struct hda_pcm_stream vt1702_pcm_analog_capture = {
        .channels_max = 2,
        .nid = 0x12, /* NID to query formats and rates */
        .ops = {
+               .open = via_pcm_open_close,
                .prepare = via_capture_pcm_prepare,
-               .cleanup = via_capture_pcm_cleanup
+               .cleanup = via_capture_pcm_cleanup,
+               .close = via_pcm_open_close
        },
 };
 
@@ -3065,12 +3674,13 @@ static int vt1702_auto_create_line_out_ctls(struct via_spec *spec,
 
 static int vt1702_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
 {
-       int err;
-
+       int err, i;
+       struct hda_input_mux *imux;
+       static const char *texts[] = { "ON", "OFF", NULL};
        if (!pin)
                return 0;
-
        spec->multiout.hp_nid = 0x1D;
+       spec->hp_independent_mode_index = 0;
 
        err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
                              "Headphone Playback Volume",
@@ -3084,8 +3694,18 @@ static int vt1702_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
        if (err < 0)
                return err;
 
-       create_hp_imux(spec);
+       imux = &spec->private_imux[1];
+
+       /* for hp mode select */
+       i = 0;
+       while (texts[i] != NULL)        {
+               imux->items[imux->num_items].label =  texts[i];
+               imux->items[imux->num_items].index = i;
+               imux->num_items++;
+               i++;
+       }
 
+       spec->hp_mux = &spec->private_imux[1];
        return 0;
 }
 
@@ -3121,8 +3741,7 @@ static int vt1702_auto_create_analog_input_ctls(struct via_spec *spec,
                        idx = 3;
                        break;
                }
-               err = via_new_analog_input(spec, cfg->input_pins[i],
-                                          labels[i], idx, 0x1A);
+               err = via_new_analog_input(spec, labels[i], idx, 0x1A);
                if (err < 0)
                        return err;
                imux->items[imux->num_items].label = labels[i];
@@ -3152,6 +3771,12 @@ static int vt1702_parse_auto_config(struct hda_codec *codec)
        err = vt1702_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
        if (err < 0)
                return err;
+       /* limit AA path volume to 0 dB */
+       snd_hda_override_amp_caps(codec, 0x1A, HDA_INPUT,
+                                 (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
+                                 (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
+                                 (0x5 << AC_AMPCAP_STEP_SIZE_SHIFT) |
+                                 (1 << AC_AMPCAP_MUTE_SHIFT));
        err = vt1702_auto_create_analog_input_ctls(spec, &spec->autocfg);
        if (err < 0)
                return err;
This page took 0.048829 seconds and 5 git commands to generate.