ALSA: HDA VIA: Modify vt1709_auto_create_multi_out_ctls.
[deliverable/linux.git] / sound / pci / hda / patch_via.c
index 0da57db3a69145799fc966b45dde7d8b8ac448a1..f9702a17fc16f83904d1be330571cc7d072204c8 100644 (file)
@@ -89,6 +89,64 @@ enum VIA_HDA_CODEC {
        CODEC_TYPES,
 };
 
+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;
@@ -124,6 +182,7 @@ static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec)
 
 #define VIA_HP_EVENT           0x01
 #define VIA_GPIO_EVENT         0x02
+#define VIA_JACK_EVENT         0x04
 
 enum {
        VIA_CTL_WIDGET_VOL,
@@ -180,6 +239,31 @@ static int mic_boost_volume_info(struct snd_kcontrol *kcontrol,
 
 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);
+
+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));
+}
+
+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();
+}
 
 static int analog_input_switch_put(struct snd_kcontrol *kcontrol,
                                   struct snd_ctl_elem_value *ucontrol)
@@ -189,6 +273,12 @@ static int analog_input_switch_put(struct snd_kcontrol *kcontrol,
 
        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;
 }
 
@@ -209,57 +299,6 @@ static struct snd_kcontrol_new vt1708_control_templates[] = {
 };
 
 
-struct via_spec {
-       /* codec parameterization */
-       struct snd_kcontrol_new *mixers[3];
-       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;
-
-       enum VIA_HDA_CODEC codec_type;
-
-#ifdef CONFIG_SND_HDA_POWER_SAVE
-       struct hda_loopback_check loopback;
-#endif
-};
-
 static hda_nid_t vt1708_adc_nids[2] = {
        /* ADC1-2 */
        0x15, 0x27
@@ -389,6 +428,8 @@ 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)
 {
@@ -399,9 +440,10 @@ static void set_pin_power_state(struct hda_codec *codec, hda_nid_t nid,
                & 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;
-
-       if ((no_presence || present) && get_defcfg_connect(def_conf)
-           != AC_JACK_PORT_NONE) {
+       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
@@ -544,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]);
@@ -572,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)
 {
@@ -579,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;
 }
 
@@ -635,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),
@@ -785,32 +1017,11 @@ static int via_playback_pcm_open(struct hda_pcm_stream *hinfo,
        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,
@@ -898,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;
 }
 
@@ -938,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;
 }
 
@@ -1019,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 */
@@ -1030,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
        },
 };
 
@@ -1149,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)
@@ -1218,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)
@@ -1255,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)
 {
@@ -1270,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
@@ -1316,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++) {
@@ -1325,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 */
@@ -1358,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;
@@ -1427,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",
@@ -1508,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;
@@ -1543,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;
 
@@ -1564,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;
 }
 
@@ -1577,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;
@@ -1653,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;
 }
 
@@ -1681,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},
        { }
 };
 
@@ -1735,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,
        },
 };
 
@@ -1747,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,
        },
 };
 
@@ -1856,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++) {
@@ -1865,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;
@@ -1922,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;
@@ -1962,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",
@@ -2063,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;
 }
 
@@ -2315,7 +2623,15 @@ 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},
        { }
 };
 
@@ -2521,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",
@@ -2624,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;
 }
 
@@ -2808,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},
        { }
 };
 
@@ -2819,8 +3145,8 @@ 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
        },
 };
@@ -2991,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",
@@ -3116,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;
 }
 
@@ -3245,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},
        { }
 };
 
@@ -3342,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",
@@ -3361,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;
 }
 
This page took 0.043081 seconds and 5 git commands to generate.