]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - sound/pci/hda/patch_hdmi.c
ALSA: hda - hdmi: Fix unused slots being enabled in manual and non-PCM mappings
[karo-tx-linux.git] / sound / pci / hda / patch_hdmi.c
index 9f35862768713899cad3ba865bb9d13b3cee796a..8bf526ea8cf27919f0c3fd9fe35ffe3aa2247eee 100644 (file)
@@ -44,6 +44,8 @@ static bool static_hdmi_pcm;
 module_param(static_hdmi_pcm, bool, 0644);
 MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info");
 
+#define is_haswell(codec)  ((codec)->vendor_id == 0x80862807)
+
 struct hdmi_spec_per_cvt {
        hda_nid_t cvt_nid;
        int assigned;
@@ -61,12 +63,15 @@ struct hdmi_spec_per_pin {
        hda_nid_t pin_nid;
        int num_mux_nids;
        hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
+       hda_nid_t cvt_nid;
 
        struct hda_codec *codec;
        struct hdmi_eld sink_eld;
        struct delayed_work work;
        struct snd_kcontrol *eld_ctl;
        int repoll_count;
+       bool setup; /* the stream has been set up by prepare callback */
+       int channels; /* current number of channels */
        bool non_pcm;
        bool chmap_set;         /* channel-map override by ALSA API? */
        unsigned char chmap[8]; /* ALSA API channel-map */
@@ -551,6 +556,17 @@ static int hdmi_channel_allocation(struct hdmi_eld *eld, int channels)
                }
        }
 
+       if (!ca) {
+               /* if there was no match, select the regular ALSA channel
+                * allocation with the matching number of channels */
+               for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
+                       if (channels == channel_allocations[i].channels) {
+                               ca = channel_allocations[i].ca_index;
+                               break;
+                       }
+               }
+       }
+
        snd_print_channel_allocation(eld->info.spk_alloc, buf, sizeof(buf));
        snd_printdd("HDMI: select CA 0x%x for %d-channel allocation: %s\n",
                    ca, channels, buf);
@@ -580,25 +596,35 @@ static void hdmi_std_setup_channel_mapping(struct hda_codec *codec,
                                       bool non_pcm,
                                       int ca)
 {
+       struct cea_channel_speaker_allocation *ch_alloc;
        int i;
        int err;
        int order;
        int non_pcm_mapping[8];
 
        order = get_channel_allocation_order(ca);
+       ch_alloc = &channel_allocations[order];
 
        if (hdmi_channel_mapping[ca][1] == 0) {
-               for (i = 0; i < channel_allocations[order].channels; i++)
-                       hdmi_channel_mapping[ca][i] = i | (i << 4);
-               for (; i < 8; i++)
-                       hdmi_channel_mapping[ca][i] = 0xf | (i << 4);
+               int hdmi_slot = 0;
+               /* fill actual channel mappings in ALSA channel (i) order */
+               for (i = 0; i < ch_alloc->channels; i++) {
+                       while (!ch_alloc->speakers[7 - hdmi_slot] && !WARN_ON(hdmi_slot >= 8))
+                               hdmi_slot++; /* skip zero slots */
+
+                       hdmi_channel_mapping[ca][i] = (i << 4) | hdmi_slot++;
+               }
+               /* fill the rest of the slots with ALSA channel 0xf */
+               for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++)
+                       if (!ch_alloc->speakers[7 - hdmi_slot])
+                               hdmi_channel_mapping[ca][i++] = (0xf << 4) | hdmi_slot;
        }
 
        if (non_pcm) {
-               for (i = 0; i < channel_allocations[order].channels; i++)
-                       non_pcm_mapping[i] = i | (i << 4);
+               for (i = 0; i < ch_alloc->channels; i++)
+                       non_pcm_mapping[i] = (i << 4) | i;
                for (; i < 8; i++)
-                       non_pcm_mapping[i] = 0xf | (i << 4);
+                       non_pcm_mapping[i] = (0xf << 4) | i;
        }
 
        for (i = 0; i < 8; i++) {
@@ -652,7 +678,7 @@ static int to_cea_slot(unsigned char c)
                if (t->map == c)
                        return t->cea_slot;
        }
-       return 0x0f;
+       return -1;
 }
 
 /* from CEA slot to ALSA API channel position */
@@ -705,14 +731,23 @@ static int hdmi_manual_setup_channel_mapping(struct hda_codec *codec,
                                             hda_nid_t pin_nid,
                                             int chs, unsigned char *map)
 {
-       int i;
-       for (i = 0; i < 8; i++) {
+       int alsa_pos, hdmi_slot;
+       int assignments[8] = {[0 ... 7] = 0xf};
+
+       for (alsa_pos = 0; alsa_pos < chs; alsa_pos++) {
+
+               hdmi_slot = to_cea_slot(map[alsa_pos]);
+
+               if (hdmi_slot < 0)
+                       continue; /* unassigned channel */
+
+               assignments[hdmi_slot] = alsa_pos;
+       }
+
+       for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++) {
                int val, err;
-               if (i < chs)
-                       val = to_cea_slot(map[i]);
-               else
-                       val = 0xf;
-               val |= (i << 4);
+
+               val = (assignments[hdmi_slot] << 4) | hdmi_slot;
                err = snd_hda_codec_write(codec, pin_nid, 0,
                                          AC_VERB_SET_HDMI_CHAN_SLOT, val);
                if (err)
@@ -725,9 +760,10 @@ static int hdmi_manual_setup_channel_mapping(struct hda_codec *codec,
 static void hdmi_setup_fake_chmap(unsigned char *map, int ca)
 {
        int i;
+       int ordered_ca = get_channel_allocation_order(ca);
        for (i = 0; i < 8; i++) {
-               if (i < channel_allocations[ca].channels)
-                       map[i] = from_cea_slot((hdmi_channel_mapping[ca][i] >> 4) & 0x0f);
+               if (i < channel_allocations[ordered_ca].channels)
+                       map[i] = from_cea_slot(hdmi_channel_mapping[ca][i] & 0x0f);
                else
                        map[i] = 0;
        }
@@ -868,18 +904,25 @@ static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
        return true;
 }
 
-static void hdmi_setup_audio_infoframe(struct hda_codec *codec, int pin_idx,
-                                      bool non_pcm,
-                                      struct snd_pcm_substream *substream)
+static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
+                                      struct hdmi_spec_per_pin *per_pin,
+                                      bool non_pcm)
 {
-       struct hdmi_spec *spec = codec->spec;
-       struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
        hda_nid_t pin_nid = per_pin->pin_nid;
-       int channels = substream->runtime->channels;
+       int channels = per_pin->channels;
+       int active_channels;
        struct hdmi_eld *eld;
-       int ca;
+       int ca, ordered_ca;
        union audio_infoframe ai;
 
+       if (!channels)
+               return;
+
+       if (is_haswell(codec))
+               snd_hda_codec_write(codec, pin_nid, 0,
+                                           AC_VERB_SET_AMP_GAIN_MUTE,
+                                           AMP_OUT_UNMUTE);
+
        eld = &per_pin->sink_eld;
        if (!eld->monitor_present)
                return;
@@ -891,6 +934,11 @@ static void hdmi_setup_audio_infoframe(struct hda_codec *codec, int pin_idx,
        if (ca < 0)
                ca = 0;
 
+       ordered_ca = get_channel_allocation_order(ca);
+       active_channels = channel_allocations[ordered_ca].channels;
+
+       hdmi_set_channel_count(codec, per_pin->cvt_nid, active_channels);
+
        memset(&ai, 0, sizeof(ai));
        if (eld->info.conn_type == 0) { /* HDMI */
                struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
@@ -898,7 +946,7 @@ static void hdmi_setup_audio_infoframe(struct hda_codec *codec, int pin_idx,
                hdmi_ai->type           = 0x84;
                hdmi_ai->ver            = 0x01;
                hdmi_ai->len            = 0x0a;
-               hdmi_ai->CC02_CT47      = channels - 1;
+               hdmi_ai->CC02_CT47      = active_channels - 1;
                hdmi_ai->CA             = ca;
                hdmi_checksum_audio_infoframe(hdmi_ai);
        } else if (eld->info.conn_type == 1) { /* DisplayPort */
@@ -907,7 +955,7 @@ static void hdmi_setup_audio_infoframe(struct hda_codec *codec, int pin_idx,
                dp_ai->type             = 0x84;
                dp_ai->len              = 0x1b;
                dp_ai->ver              = 0x11 << 2;
-               dp_ai->CC02_CT47        = channels - 1;
+               dp_ai->CC02_CT47        = active_channels - 1;
                dp_ai->CA               = ca;
        } else {
                snd_printd("HDMI: unknown connection type at pin %d\n",
@@ -925,7 +973,7 @@ static void hdmi_setup_audio_infoframe(struct hda_codec *codec, int pin_idx,
                snd_printdd("hdmi_setup_audio_infoframe: "
                            "pin=%d channels=%d\n",
                            pin_nid,
-                           channels);
+                           active_channels);
                hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
                                           channels, per_pin->chmap,
                                           per_pin->chmap_set);
@@ -959,6 +1007,7 @@ static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
        int pin_nid;
        int pin_idx;
        struct hda_jack_tbl *jack;
+       int dev_entry = (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
 
        jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
        if (!jack)
@@ -967,8 +1016,8 @@ static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
        jack->jack_dirty = 1;
 
        _snd_printd(SND_PR_VERBOSE,
-               "HDMI hot plug event: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
-               codec->addr, pin_nid,
+               "HDMI hot plug event: Codec=%d Pin=%d Device=%d Inactive=%d Presence_Detect=%d ELD_Valid=%d\n",
+               codec->addr, pin_nid, dev_entry, !!(res & AC_UNSOL_RES_IA),
                !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
 
        pin_idx = pin_nid_to_pin_index(spec, pin_nid);
@@ -1018,10 +1067,10 @@ static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
                hdmi_non_intrinsic_event(codec, res);
 }
 
-static void haswell_verify_pin_D0(struct hda_codec *codec,
+static void haswell_verify_D0(struct hda_codec *codec,
                hda_nid_t cvt_nid, hda_nid_t nid)
 {
-       int pwr, lamp, ramp;
+       int pwr;
 
        /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
         * thus pins could only choose converter 0 for use. Make sure the
@@ -1037,25 +1086,6 @@ static void haswell_verify_pin_D0(struct hda_codec *codec,
                pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
                snd_printd("Haswell HDMI audio: Power for pin 0x%x is now D%d\n", nid, pwr);
        }
-
-       lamp = snd_hda_codec_read(codec, nid, 0,
-                                 AC_VERB_GET_AMP_GAIN_MUTE,
-                                 AC_AMP_GET_LEFT | AC_AMP_GET_OUTPUT);
-       ramp = snd_hda_codec_read(codec, nid, 0,
-                                 AC_VERB_GET_AMP_GAIN_MUTE,
-                                 AC_AMP_GET_RIGHT | AC_AMP_GET_OUTPUT);
-       if (lamp != ramp) {
-               snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
-                                   AC_AMP_SET_RIGHT | AC_AMP_SET_OUTPUT | lamp);
-
-               lamp = snd_hda_codec_read(codec, nid, 0,
-                                 AC_VERB_GET_AMP_GAIN_MUTE,
-                                 AC_AMP_GET_LEFT | AC_AMP_GET_OUTPUT);
-               ramp = snd_hda_codec_read(codec, nid, 0,
-                                 AC_VERB_GET_AMP_GAIN_MUTE,
-                                 AC_AMP_GET_RIGHT | AC_AMP_GET_OUTPUT);
-               snd_printd("Haswell HDMI audio: Mute after set on pin 0x%x: [0x%x 0x%x]\n", nid, lamp, ramp);
-       }
 }
 
 /*
@@ -1072,8 +1102,8 @@ static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
        int pinctl;
        int new_pinctl = 0;
 
-       if (codec->vendor_id == 0x80862807)
-               haswell_verify_pin_D0(codec, cvt_nid, pin_nid);
+       if (is_haswell(codec))
+               haswell_verify_D0(codec, cvt_nid, pin_nid);
 
        if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
                pinctl = snd_hda_codec_read(codec, pin_nid, 0,
@@ -1146,32 +1176,43 @@ static int hdmi_choose_cvt(struct hda_codec *codec,
 }
 
 static void haswell_config_cvts(struct hda_codec *codec,
-                       int pin_id, int mux_id)
+                       hda_nid_t pin_nid, int mux_idx)
 {
        struct hdmi_spec *spec = codec->spec;
-       struct hdmi_spec_per_pin *per_pin;
-       int pin_idx, mux_idx;
-       int curr;
-       int err;
+       hda_nid_t nid, end_nid;
+       int cvt_idx, curr;
+       struct hdmi_spec_per_cvt *per_cvt;
 
-       for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
-               per_pin = get_pin(spec, pin_idx);
+       /* configure all pins, including "no physical connection" ones */
+       end_nid = codec->start_nid + codec->num_nodes;
+       for (nid = codec->start_nid; nid < end_nid; nid++) {
+               unsigned int wid_caps = get_wcaps(codec, nid);
+               unsigned int wid_type = get_wcaps_type(wid_caps);
 
-               if (pin_idx == pin_id)
+               if (wid_type != AC_WID_PIN)
                        continue;
 
-               curr = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
+               if (nid == pin_nid)
+                       continue;
+
+               curr = snd_hda_codec_read(codec, nid, 0,
                                          AC_VERB_GET_CONNECT_SEL, 0);
+               if (curr != mux_idx)
+                       continue;
 
-               /* Choose another unused converter */
-               if (curr == mux_id) {
-                       err = hdmi_choose_cvt(codec, pin_idx, NULL, &mux_idx);
-                       if (err < 0)
-                               return;
-                       snd_printdd("HDMI: choose converter %d for pin %d\n", mux_idx, pin_idx);
-                       snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
+               /* choose an unassigned converter. The conveters in the
+                * connection list are in the same order as in the codec.
+                */
+               for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
+                       per_cvt = get_cvt(spec, cvt_idx);
+                       if (!per_cvt->assigned) {
+                               snd_printdd("choose cvt %d for pin nid %d\n",
+                                       cvt_idx, nid);
+                               snd_hda_codec_write_cache(codec, nid, 0,
                                            AC_VERB_SET_CONNECT_SEL,
-                                           mux_idx);
+                                           cvt_idx);
+                               break;
+                       }
                }
        }
 }
@@ -1205,6 +1246,7 @@ static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
        per_cvt = get_cvt(spec, cvt_idx);
        /* Claim converter */
        per_cvt->assigned = 1;
+       per_pin->cvt_nid = per_cvt->cvt_nid;
        hinfo->nid = per_cvt->cvt_nid;
 
        snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
@@ -1212,8 +1254,8 @@ static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
                            mux_idx);
 
        /* configure unused pins to choose other converters */
-       if (codec->vendor_id == 0x80862807)
-               haswell_config_cvts(codec, pin_idx, mux_idx);
+       if (is_haswell(codec))
+               haswell_config_cvts(codec, per_pin->pin_nid, mux_idx);
 
        snd_hda_spdif_ctls_assign(codec, pin_idx, per_cvt->cvt_nid);
 
@@ -1329,6 +1371,7 @@ static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
                eld_changed = true;
        }
        if (update_eld) {
+               bool old_eld_valid = pin_eld->eld_valid;
                pin_eld->eld_valid = eld->eld_valid;
                eld_changed = pin_eld->eld_size != eld->eld_size ||
                              memcmp(pin_eld->eld_buffer, eld->eld_buffer,
@@ -1338,6 +1381,14 @@ static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
                               eld->eld_size);
                pin_eld->eld_size = eld->eld_size;
                pin_eld->info = eld->info;
+
+               /* Haswell-specific workaround: re-setup when the transcoder is
+                * changed during the stream playback
+                */
+               if (is_haswell(codec) &&
+                   eld->eld_valid && !old_eld_valid && per_pin->setup)
+                       hdmi_setup_audio_infoframe(codec, per_pin,
+                                                  per_pin->non_pcm);
        }
        mutex_unlock(&pin_eld->lock);
 
@@ -1377,7 +1428,7 @@ static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
        if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
                return 0;
 
-       if (codec->vendor_id == 0x80862807)
+       if (is_haswell(codec))
                intel_haswell_fixup_connect_list(codec, pin_nid);
 
        pin_idx = spec->num_pins;
@@ -1510,14 +1561,15 @@ static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
        hda_nid_t cvt_nid = hinfo->nid;
        struct hdmi_spec *spec = codec->spec;
        int pin_idx = hinfo_to_pin_index(spec, hinfo);
-       hda_nid_t pin_nid = get_pin(spec, pin_idx)->pin_nid;
+       struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
+       hda_nid_t pin_nid = per_pin->pin_nid;
        bool non_pcm;
 
        non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
+       per_pin->channels = substream->runtime->channels;
+       per_pin->setup = true;
 
-       hdmi_set_channel_count(codec, cvt_nid, substream->runtime->channels);
-
-       hdmi_setup_audio_infoframe(codec, pin_idx, non_pcm, substream);
+       hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
 
        return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
 }
@@ -1557,6 +1609,9 @@ static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
                snd_hda_spdif_ctls_unassign(codec, pin_idx);
                per_pin->chmap_set = false;
                memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
+
+               per_pin->setup = false;
+               per_pin->channels = 0;
        }
 
        return 0;
@@ -1692,8 +1747,7 @@ static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
        per_pin->chmap_set = true;
        memcpy(per_pin->chmap, chmap, sizeof(chmap));
        if (prepared)
-               hdmi_setup_audio_infoframe(codec, pin_idx, per_pin->non_pcm,
-                                          substream);
+               hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
 
        return 0;
 }
@@ -1981,7 +2035,7 @@ static int patch_generic_hdmi(struct hda_codec *codec)
        codec->spec = spec;
        hdmi_array_init(spec, 4);
 
-       if (codec->vendor_id == 0x80862807) {
+       if (is_haswell(codec)) {
                intel_haswell_enable_all_pins(codec, true);
                intel_haswell_fixup_enable_dp12(codec);
        }
@@ -1992,8 +2046,10 @@ static int patch_generic_hdmi(struct hda_codec *codec)
                return -EINVAL;
        }
        codec->patch_ops = generic_hdmi_patch_ops;
-       if (codec->vendor_id == 0x80862807)
+       if (is_haswell(codec)) {
                codec->patch_ops.set_power_state = haswell_set_power_state;
+               codec->dp_mst = true;
+       }
 
        generic_hdmi_init_per_pins(codec);