1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * patch_hdmi.c - routines for HDMI/DisplayPort codecs
6 * Copyright(c) 2008-2010 Intel Corporation. All rights reserved.
7 * Copyright (c) 2006 ATI Technologies Inc.
8 * Copyright (c) 2008 NVIDIA Corp. All rights reserved.
9 * Copyright (c) 2008 Wei Ni <wni@nvidia.com>
10 * Copyright (c) 2013 Anssi Hannula <anssi.hannula@iki.fi>
13 * Wu Fengguang <wfg@linux.intel.com>
16 * Wu Fengguang <wfg@linux.intel.com>
19 #include <linux/init.h>
20 #include <linux/delay.h>
21 #include <linux/pci.h>
22 #include <linux/slab.h>
23 #include <linux/module.h>
24 #include <linux/pm_runtime.h>
25 #include <sound/core.h>
26 #include <sound/jack.h>
27 #include <sound/asoundef.h>
28 #include <sound/tlv.h>
29 #include <sound/hdaudio.h>
30 #include <sound/hda_i915.h>
31 #include <sound/hda_chmap.h>
32 #include <sound/hda_codec.h>
33 #include "hda_local.h"
36 static bool static_hdmi_pcm;
37 module_param(static_hdmi_pcm, bool, 0644);
38 MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info");
40 struct hdmi_spec_per_cvt {
43 unsigned int channels_min;
44 unsigned int channels_max;
50 /* max. connections to a widget */
51 #define HDA_MAX_CONNECTIONS 32
53 struct hdmi_spec_per_pin {
56 /* pin idx, different device entries on the same pin use the same idx */
59 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
63 struct hda_codec *codec;
64 struct hdmi_eld sink_eld;
66 struct delayed_work work;
67 struct hdmi_pcm *pcm; /* pointer to spec->pcm_rec[n] dynamically*/
68 int pcm_idx; /* which pcm is attached. -1 means no pcm is attached */
70 bool setup; /* the stream has been set up by prepare callback */
71 int channels; /* current number of channels */
73 bool chmap_set; /* channel-map override by ALSA API? */
74 unsigned char chmap[8]; /* ALSA API channel-map */
75 #ifdef CONFIG_SND_PROC_FS
76 struct snd_info_entry *proc_entry;
80 /* operations used by generic code that can be overridden by patches */
82 int (*pin_get_eld)(struct hda_codec *codec, hda_nid_t pin_nid,
83 int dev_id, unsigned char *buf, int *eld_size);
85 void (*pin_setup_infoframe)(struct hda_codec *codec, hda_nid_t pin_nid,
87 int ca, int active_channels, int conn_type);
89 /* enable/disable HBR (HD passthrough) */
90 int (*pin_hbr_setup)(struct hda_codec *codec, hda_nid_t pin_nid,
91 int dev_id, bool hbr);
93 int (*setup_stream)(struct hda_codec *codec, hda_nid_t cvt_nid,
94 hda_nid_t pin_nid, int dev_id, u32 stream_tag,
97 void (*pin_cvt_fixup)(struct hda_codec *codec,
98 struct hdmi_spec_per_pin *per_pin,
104 struct snd_jack *jack;
105 struct snd_kcontrol *eld_ctl;
109 struct hda_codec *codec;
111 struct snd_array cvts; /* struct hdmi_spec_per_cvt */
112 hda_nid_t cvt_nids[4]; /* only for haswell fix */
115 * num_pins is the number of virtual pins
116 * for example, there are 3 pins, and each pin
117 * has 4 device entries, then the num_pins is 12
121 * num_nids is the number of real pins
122 * In the above example, num_nids is 3
126 * dev_num is the number of device entries
128 * In the above example, dev_num is 4
131 struct snd_array pins; /* struct hdmi_spec_per_pin */
132 struct hdmi_pcm pcm_rec[16];
133 struct mutex pcm_lock;
134 struct mutex bind_lock; /* for audio component binding */
135 /* pcm_bitmap means which pcms have been assigned to pins*/
136 unsigned long pcm_bitmap;
137 int pcm_used; /* counter of pcm_rec[] */
138 /* bitmap shows whether the pcm is opened in user space
139 * bit 0 means the first playback PCM (PCM3);
140 * bit 1 means the second playback PCM, and so on.
142 unsigned long pcm_in_use;
144 struct hdmi_eld temp_eld;
149 bool intel_hsw_fixup; /* apply Intel platform-specific fixups */
151 * Non-generic VIA/NVIDIA specific
153 struct hda_multi_out multiout;
154 struct hda_pcm_stream pcm_playback;
156 bool use_jack_detect; /* jack detection enabled */
157 bool use_acomp_notifier; /* use eld_notify callback for hotplug */
158 bool acomp_registered; /* audio component registered in this driver */
159 struct drm_audio_component_audio_ops drm_audio_ops;
160 int (*port2pin)(struct hda_codec *, int); /* reverse port/pin mapping */
162 struct hdac_chmap chmap;
163 hda_nid_t vendor_nid;
168 #ifdef CONFIG_SND_HDA_COMPONENT
169 static inline bool codec_has_acomp(struct hda_codec *codec)
171 struct hdmi_spec *spec = codec->spec;
172 return spec->use_acomp_notifier;
175 #define codec_has_acomp(codec) false
178 struct hdmi_audio_infoframe {
185 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
189 u8 LFEPBL01_LSV36_DM_INH7;
192 struct dp_audio_infoframe {
195 u8 ver; /* 0x11 << 2 */
197 u8 CC02_CT47; /* match with HDMI infoframe from this on */
201 u8 LFEPBL01_LSV36_DM_INH7;
204 union audio_infoframe {
205 struct hdmi_audio_infoframe hdmi;
206 struct dp_audio_infoframe dp;
214 #define get_pin(spec, idx) \
215 ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
216 #define get_cvt(spec, idx) \
217 ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx))
218 /* obtain hdmi_pcm object assigned to idx */
219 #define get_hdmi_pcm(spec, idx) (&(spec)->pcm_rec[idx])
220 /* obtain hda_pcm object assigned to idx */
221 #define get_pcm_rec(spec, idx) (get_hdmi_pcm(spec, idx)->pcm)
223 static int pin_id_to_pin_index(struct hda_codec *codec,
224 hda_nid_t pin_nid, int dev_id)
226 struct hdmi_spec *spec = codec->spec;
228 struct hdmi_spec_per_pin *per_pin;
231 * (dev_id == -1) means it is NON-MST pin
232 * return the first virtual pin on this port
237 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
238 per_pin = get_pin(spec, pin_idx);
239 if ((per_pin->pin_nid == pin_nid) &&
240 (per_pin->dev_id == dev_id))
244 codec_warn(codec, "HDMI: pin nid %d not registered\n", pin_nid);
248 static int hinfo_to_pcm_index(struct hda_codec *codec,
249 struct hda_pcm_stream *hinfo)
251 struct hdmi_spec *spec = codec->spec;
254 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++)
255 if (get_pcm_rec(spec, pcm_idx)->stream == hinfo)
258 codec_warn(codec, "HDMI: hinfo %p not registered\n", hinfo);
262 static int hinfo_to_pin_index(struct hda_codec *codec,
263 struct hda_pcm_stream *hinfo)
265 struct hdmi_spec *spec = codec->spec;
266 struct hdmi_spec_per_pin *per_pin;
269 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
270 per_pin = get_pin(spec, pin_idx);
272 per_pin->pcm->pcm->stream == hinfo)
276 codec_dbg(codec, "HDMI: hinfo %p not registered\n", hinfo);
280 static struct hdmi_spec_per_pin *pcm_idx_to_pin(struct hdmi_spec *spec,
284 struct hdmi_spec_per_pin *per_pin;
286 for (i = 0; i < spec->num_pins; i++) {
287 per_pin = get_pin(spec, i);
288 if (per_pin->pcm_idx == pcm_idx)
294 static int cvt_nid_to_cvt_index(struct hda_codec *codec, hda_nid_t cvt_nid)
296 struct hdmi_spec *spec = codec->spec;
299 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
300 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
303 codec_warn(codec, "HDMI: cvt nid %d not registered\n", cvt_nid);
307 static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
308 struct snd_ctl_elem_info *uinfo)
310 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
311 struct hdmi_spec *spec = codec->spec;
312 struct hdmi_spec_per_pin *per_pin;
313 struct hdmi_eld *eld;
316 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
318 pcm_idx = kcontrol->private_value;
319 mutex_lock(&spec->pcm_lock);
320 per_pin = pcm_idx_to_pin(spec, pcm_idx);
322 /* no pin is bound to the pcm */
326 eld = &per_pin->sink_eld;
327 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
330 mutex_unlock(&spec->pcm_lock);
334 static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
335 struct snd_ctl_elem_value *ucontrol)
337 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
338 struct hdmi_spec *spec = codec->spec;
339 struct hdmi_spec_per_pin *per_pin;
340 struct hdmi_eld *eld;
344 pcm_idx = kcontrol->private_value;
345 mutex_lock(&spec->pcm_lock);
346 per_pin = pcm_idx_to_pin(spec, pcm_idx);
348 /* no pin is bound to the pcm */
349 memset(ucontrol->value.bytes.data, 0,
350 ARRAY_SIZE(ucontrol->value.bytes.data));
354 eld = &per_pin->sink_eld;
355 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data) ||
356 eld->eld_size > ELD_MAX_SIZE) {
362 memset(ucontrol->value.bytes.data, 0,
363 ARRAY_SIZE(ucontrol->value.bytes.data));
365 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
369 mutex_unlock(&spec->pcm_lock);
373 static const struct snd_kcontrol_new eld_bytes_ctl = {
374 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
375 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
377 .info = hdmi_eld_ctl_info,
378 .get = hdmi_eld_ctl_get,
381 static int hdmi_create_eld_ctl(struct hda_codec *codec, int pcm_idx,
384 struct snd_kcontrol *kctl;
385 struct hdmi_spec *spec = codec->spec;
388 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
391 kctl->private_value = pcm_idx;
392 kctl->id.device = device;
394 /* no pin nid is associated with the kctl now
395 * tbd: associate pin nid to eld ctl later
397 err = snd_hda_ctl_add(codec, 0, kctl);
401 get_hdmi_pcm(spec, pcm_idx)->eld_ctl = kctl;
406 static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
407 int *packet_index, int *byte_index)
411 val = snd_hda_codec_read(codec, pin_nid, 0,
412 AC_VERB_GET_HDMI_DIP_INDEX, 0);
414 *packet_index = val >> 5;
415 *byte_index = val & 0x1f;
419 static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
420 int packet_index, int byte_index)
424 val = (packet_index << 5) | (byte_index & 0x1f);
426 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
429 static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
432 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
435 static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
437 struct hdmi_spec *spec = codec->spec;
441 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
442 snd_hda_codec_write(codec, pin_nid, 0,
443 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
445 if (spec->dyn_pin_out)
446 /* Disable pin out until stream is active */
449 /* Enable pin out: some machines with GM965 gets broken output
450 * when the pin is disabled or changed while using with HDMI
454 snd_hda_codec_write(codec, pin_nid, 0,
455 AC_VERB_SET_PIN_WIDGET_CONTROL, pin_out);
462 #ifdef CONFIG_SND_PROC_FS
463 static void print_eld_info(struct snd_info_entry *entry,
464 struct snd_info_buffer *buffer)
466 struct hdmi_spec_per_pin *per_pin = entry->private_data;
468 mutex_lock(&per_pin->lock);
469 snd_hdmi_print_eld_info(&per_pin->sink_eld, buffer);
470 mutex_unlock(&per_pin->lock);
473 static void write_eld_info(struct snd_info_entry *entry,
474 struct snd_info_buffer *buffer)
476 struct hdmi_spec_per_pin *per_pin = entry->private_data;
478 mutex_lock(&per_pin->lock);
479 snd_hdmi_write_eld_info(&per_pin->sink_eld, buffer);
480 mutex_unlock(&per_pin->lock);
483 static int eld_proc_new(struct hdmi_spec_per_pin *per_pin, int index)
486 struct hda_codec *codec = per_pin->codec;
487 struct snd_info_entry *entry;
490 snprintf(name, sizeof(name), "eld#%d.%d", codec->addr, index);
491 err = snd_card_proc_new(codec->card, name, &entry);
495 snd_info_set_text_ops(entry, per_pin, print_eld_info);
496 entry->c.text.write = write_eld_info;
498 per_pin->proc_entry = entry;
503 static void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
505 if (!per_pin->codec->bus->shutdown) {
506 snd_info_free_entry(per_pin->proc_entry);
507 per_pin->proc_entry = NULL;
511 static inline int eld_proc_new(struct hdmi_spec_per_pin *per_pin,
516 static inline void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
522 * Audio InfoFrame routines
526 * Enable Audio InfoFrame Transmission
528 static void hdmi_start_infoframe_trans(struct hda_codec *codec,
531 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
532 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
537 * Disable Audio InfoFrame Transmission
539 static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
542 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
543 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
547 static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
549 #ifdef CONFIG_SND_DEBUG_VERBOSE
553 size = snd_hdmi_get_eld_size(codec, pin_nid);
554 codec_dbg(codec, "HDMI: ELD buf size is %d\n", size);
556 for (i = 0; i < 8; i++) {
557 size = snd_hda_codec_read(codec, pin_nid, 0,
558 AC_VERB_GET_HDMI_DIP_SIZE, i);
559 codec_dbg(codec, "HDMI: DIP GP[%d] buf size is %d\n", i, size);
564 static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
570 for (i = 0; i < 8; i++) {
571 size = snd_hda_codec_read(codec, pin_nid, 0,
572 AC_VERB_GET_HDMI_DIP_SIZE, i);
576 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
577 for (j = 1; j < 1000; j++) {
578 hdmi_write_dip_byte(codec, pin_nid, 0x0);
579 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
581 codec_dbg(codec, "dip index %d: %d != %d\n",
583 if (bi == 0) /* byte index wrapped around */
587 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
593 static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
595 u8 *bytes = (u8 *)hdmi_ai;
599 hdmi_ai->checksum = 0;
601 for (i = 0; i < sizeof(*hdmi_ai); i++)
604 hdmi_ai->checksum = -sum;
607 static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
613 hdmi_debug_dip_size(codec, pin_nid);
614 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
616 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
617 for (i = 0; i < size; i++)
618 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
621 static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
627 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
631 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
632 for (i = 0; i < size; i++) {
633 val = snd_hda_codec_read(codec, pin_nid, 0,
634 AC_VERB_GET_HDMI_DIP_DATA, 0);
642 static int hdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
643 int dev_id, unsigned char *buf, int *eld_size)
645 snd_hda_set_dev_select(codec, nid, dev_id);
647 return snd_hdmi_get_eld(codec, nid, buf, eld_size);
650 static void hdmi_pin_setup_infoframe(struct hda_codec *codec,
651 hda_nid_t pin_nid, int dev_id,
652 int ca, int active_channels,
655 union audio_infoframe ai;
657 memset(&ai, 0, sizeof(ai));
658 if (conn_type == 0) { /* HDMI */
659 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
661 hdmi_ai->type = 0x84;
664 hdmi_ai->CC02_CT47 = active_channels - 1;
666 hdmi_checksum_audio_infoframe(hdmi_ai);
667 } else if (conn_type == 1) { /* DisplayPort */
668 struct dp_audio_infoframe *dp_ai = &ai.dp;
672 dp_ai->ver = 0x11 << 2;
673 dp_ai->CC02_CT47 = active_channels - 1;
676 codec_dbg(codec, "HDMI: unknown connection type at pin %d\n",
681 snd_hda_set_dev_select(codec, pin_nid, dev_id);
684 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
685 * sizeof(*dp_ai) to avoid partial match/update problems when
686 * the user switches between HDMI/DP monitors.
688 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
691 "hdmi_pin_setup_infoframe: pin=%d channels=%d ca=0x%02x\n",
693 active_channels, ca);
694 hdmi_stop_infoframe_trans(codec, pin_nid);
695 hdmi_fill_audio_infoframe(codec, pin_nid,
696 ai.bytes, sizeof(ai));
697 hdmi_start_infoframe_trans(codec, pin_nid);
701 static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
702 struct hdmi_spec_per_pin *per_pin,
705 struct hdmi_spec *spec = codec->spec;
706 struct hdac_chmap *chmap = &spec->chmap;
707 hda_nid_t pin_nid = per_pin->pin_nid;
708 int dev_id = per_pin->dev_id;
709 int channels = per_pin->channels;
711 struct hdmi_eld *eld;
717 snd_hda_set_dev_select(codec, pin_nid, dev_id);
719 /* some HW (e.g. HSW+) needs reprogramming the amp at each time */
720 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
721 snd_hda_codec_write(codec, pin_nid, 0,
722 AC_VERB_SET_AMP_GAIN_MUTE,
725 eld = &per_pin->sink_eld;
727 ca = snd_hdac_channel_allocation(&codec->core,
728 eld->info.spk_alloc, channels,
729 per_pin->chmap_set, non_pcm, per_pin->chmap);
731 active_channels = snd_hdac_get_active_channels(ca);
733 chmap->ops.set_channel_count(&codec->core, per_pin->cvt_nid,
737 * always configure channel mapping, it may have been changed by the
738 * user in the meantime
740 snd_hdac_setup_channel_mapping(&spec->chmap,
741 pin_nid, non_pcm, ca, channels,
742 per_pin->chmap, per_pin->chmap_set);
744 spec->ops.pin_setup_infoframe(codec, pin_nid, dev_id,
745 ca, active_channels, eld->info.conn_type);
747 per_pin->non_pcm = non_pcm;
754 static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
756 static void check_presence_and_report(struct hda_codec *codec, hda_nid_t nid,
759 struct hdmi_spec *spec = codec->spec;
760 int pin_idx = pin_id_to_pin_index(codec, nid, dev_id);
764 mutex_lock(&spec->pcm_lock);
765 if (hdmi_present_sense(get_pin(spec, pin_idx), 1))
766 snd_hda_jack_report_sync(codec);
767 mutex_unlock(&spec->pcm_lock);
770 static void jack_callback(struct hda_codec *codec,
771 struct hda_jack_callback *jack)
773 /* stop polling when notification is enabled */
774 if (codec_has_acomp(codec))
777 check_presence_and_report(codec, jack->nid, jack->dev_id);
780 static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
782 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
783 struct hda_jack_tbl *jack;
787 (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
789 jack = snd_hda_jack_tbl_get_from_tag(codec, tag, dev_entry);
791 jack = snd_hda_jack_tbl_get_from_tag(codec, tag, 0);
795 jack->jack_dirty = 1;
798 "HDMI hot plug event: Codec=%d Pin=%d Device=%d Inactive=%d Presence_Detect=%d ELD_Valid=%d\n",
799 codec->addr, jack->nid, jack->dev_id, !!(res & AC_UNSOL_RES_IA),
800 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
802 check_presence_and_report(codec, jack->nid, jack->dev_id);
805 static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
807 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
808 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
809 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
810 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
813 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
828 static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
830 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
831 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
832 struct hda_jack_tbl *jack;
834 if (codec_has_acomp(codec))
839 (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
841 jack = snd_hda_jack_tbl_get_from_tag(codec, tag, dev_entry);
843 jack = snd_hda_jack_tbl_get_from_tag(codec, tag, 0);
847 codec_dbg(codec, "Unexpected HDMI event tag 0x%x\n", tag);
852 hdmi_intrinsic_event(codec, res);
854 hdmi_non_intrinsic_event(codec, res);
857 static void haswell_verify_D0(struct hda_codec *codec,
858 hda_nid_t cvt_nid, hda_nid_t nid)
862 /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
863 * thus pins could only choose converter 0 for use. Make sure the
864 * converters are in correct power state */
865 if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
866 snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
868 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
869 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
872 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
873 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
874 codec_dbg(codec, "Haswell HDMI audio: Power for pin 0x%x is now D%d\n", nid, pwr);
882 /* HBR should be Non-PCM, 8 channels */
883 #define is_hbr_format(format) \
884 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
886 static int hdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
887 int dev_id, bool hbr)
889 int pinctl, new_pinctl;
891 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
892 snd_hda_set_dev_select(codec, pin_nid, dev_id);
893 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
894 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
897 return hbr ? -EINVAL : 0;
899 new_pinctl = pinctl & ~AC_PINCTL_EPT;
901 new_pinctl |= AC_PINCTL_EPT_HBR;
903 new_pinctl |= AC_PINCTL_EPT_NATIVE;
906 "hdmi_pin_hbr_setup: NID=0x%x, %spinctl=0x%x\n",
908 pinctl == new_pinctl ? "" : "new-",
911 if (pinctl != new_pinctl)
912 snd_hda_codec_write(codec, pin_nid, 0,
913 AC_VERB_SET_PIN_WIDGET_CONTROL,
921 static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
922 hda_nid_t pin_nid, int dev_id,
923 u32 stream_tag, int format)
925 struct hdmi_spec *spec = codec->spec;
929 err = spec->ops.pin_hbr_setup(codec, pin_nid, dev_id,
930 is_hbr_format(format));
933 codec_dbg(codec, "hdmi_setup_stream: HBR is not supported\n");
937 if (spec->intel_hsw_fixup) {
940 * on recent platforms IEC Coding Type is required for HBR
941 * support, read current Digital Converter settings and set
942 * ICT bitfield if needed.
944 param = snd_hda_codec_read(codec, cvt_nid, 0,
945 AC_VERB_GET_DIGI_CONVERT_1, 0);
947 param = (param >> 16) & ~(AC_DIG3_ICT);
949 /* on recent platforms ICT mode is required for HBR support */
950 if (is_hbr_format(format))
953 snd_hda_codec_write(codec, cvt_nid, 0,
954 AC_VERB_SET_DIGI_CONVERT_3, param);
957 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
961 /* Try to find an available converter
962 * If pin_idx is less then zero, just try to find an available converter.
963 * Otherwise, try to find an available converter and get the cvt mux index
966 static int hdmi_choose_cvt(struct hda_codec *codec,
967 int pin_idx, int *cvt_id)
969 struct hdmi_spec *spec = codec->spec;
970 struct hdmi_spec_per_pin *per_pin;
971 struct hdmi_spec_per_cvt *per_cvt = NULL;
972 int cvt_idx, mux_idx = 0;
974 /* pin_idx < 0 means no pin will be bound to the converter */
978 per_pin = get_pin(spec, pin_idx);
980 /* Dynamically assign converter to stream */
981 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
982 per_cvt = get_cvt(spec, cvt_idx);
984 /* Must not already be assigned */
985 if (per_cvt->assigned)
989 /* Must be in pin's mux's list of converters */
990 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
991 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
993 /* Not in mux list */
994 if (mux_idx == per_pin->num_mux_nids)
999 /* No free converters */
1000 if (cvt_idx == spec->num_cvts)
1003 if (per_pin != NULL)
1004 per_pin->mux_idx = mux_idx;
1012 /* Assure the pin select the right convetor */
1013 static void intel_verify_pin_cvt_connect(struct hda_codec *codec,
1014 struct hdmi_spec_per_pin *per_pin)
1016 hda_nid_t pin_nid = per_pin->pin_nid;
1019 mux_idx = per_pin->mux_idx;
1020 curr = snd_hda_codec_read(codec, pin_nid, 0,
1021 AC_VERB_GET_CONNECT_SEL, 0);
1022 if (curr != mux_idx)
1023 snd_hda_codec_write_cache(codec, pin_nid, 0,
1024 AC_VERB_SET_CONNECT_SEL,
1028 /* get the mux index for the converter of the pins
1029 * converter's mux index is the same for all pins on Intel platform
1031 static int intel_cvt_id_to_mux_idx(struct hdmi_spec *spec,
1036 for (i = 0; i < spec->num_cvts; i++)
1037 if (spec->cvt_nids[i] == cvt_nid)
1042 /* Intel HDMI workaround to fix audio routing issue:
1043 * For some Intel display codecs, pins share the same connection list.
1044 * So a conveter can be selected by multiple pins and playback on any of these
1045 * pins will generate sound on the external display, because audio flows from
1046 * the same converter to the display pipeline. Also muting one pin may make
1047 * other pins have no sound output.
1048 * So this function assures that an assigned converter for a pin is not selected
1049 * by any other pins.
1051 static void intel_not_share_assigned_cvt(struct hda_codec *codec,
1053 int dev_id, int mux_idx)
1055 struct hdmi_spec *spec = codec->spec;
1058 struct hdmi_spec_per_cvt *per_cvt;
1059 struct hdmi_spec_per_pin *per_pin;
1062 /* configure the pins connections */
1063 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1067 per_pin = get_pin(spec, pin_idx);
1069 * pin not connected to monitor
1070 * no need to operate on it
1075 if ((per_pin->pin_nid == pin_nid) &&
1076 (per_pin->dev_id == dev_id))
1080 * if per_pin->dev_id >= dev_num,
1081 * snd_hda_get_dev_select() will fail,
1082 * and the following operation is unpredictable.
1083 * So skip this situation.
1085 dev_num = snd_hda_get_num_devices(codec, per_pin->pin_nid) + 1;
1086 if (per_pin->dev_id >= dev_num)
1089 nid = per_pin->pin_nid;
1092 * Calling this function should not impact
1093 * on the device entry selection
1094 * So let's save the dev id for each pin,
1095 * and restore it when return
1097 dev_id_saved = snd_hda_get_dev_select(codec, nid);
1098 snd_hda_set_dev_select(codec, nid, per_pin->dev_id);
1099 curr = snd_hda_codec_read(codec, nid, 0,
1100 AC_VERB_GET_CONNECT_SEL, 0);
1101 if (curr != mux_idx) {
1102 snd_hda_set_dev_select(codec, nid, dev_id_saved);
1107 /* choose an unassigned converter. The conveters in the
1108 * connection list are in the same order as in the codec.
1110 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1111 per_cvt = get_cvt(spec, cvt_idx);
1112 if (!per_cvt->assigned) {
1114 "choose cvt %d for pin nid %d\n",
1116 snd_hda_codec_write_cache(codec, nid, 0,
1117 AC_VERB_SET_CONNECT_SEL,
1122 snd_hda_set_dev_select(codec, nid, dev_id_saved);
1126 /* A wrapper of intel_not_share_asigned_cvt() */
1127 static void intel_not_share_assigned_cvt_nid(struct hda_codec *codec,
1128 hda_nid_t pin_nid, int dev_id, hda_nid_t cvt_nid)
1131 struct hdmi_spec *spec = codec->spec;
1133 /* On Intel platform, the mapping of converter nid to
1134 * mux index of the pins are always the same.
1135 * The pin nid may be 0, this means all pins will not
1136 * share the converter.
1138 mux_idx = intel_cvt_id_to_mux_idx(spec, cvt_nid);
1140 intel_not_share_assigned_cvt(codec, pin_nid, dev_id, mux_idx);
1143 /* skeleton caller of pin_cvt_fixup ops */
1144 static void pin_cvt_fixup(struct hda_codec *codec,
1145 struct hdmi_spec_per_pin *per_pin,
1148 struct hdmi_spec *spec = codec->spec;
1150 if (spec->ops.pin_cvt_fixup)
1151 spec->ops.pin_cvt_fixup(codec, per_pin, cvt_nid);
1154 /* called in hdmi_pcm_open when no pin is assigned to the PCM
1155 * in dyn_pcm_assign mode.
1157 static int hdmi_pcm_open_no_pin(struct hda_pcm_stream *hinfo,
1158 struct hda_codec *codec,
1159 struct snd_pcm_substream *substream)
1161 struct hdmi_spec *spec = codec->spec;
1162 struct snd_pcm_runtime *runtime = substream->runtime;
1163 int cvt_idx, pcm_idx;
1164 struct hdmi_spec_per_cvt *per_cvt = NULL;
1167 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1171 err = hdmi_choose_cvt(codec, -1, &cvt_idx);
1175 per_cvt = get_cvt(spec, cvt_idx);
1176 per_cvt->assigned = 1;
1177 hinfo->nid = per_cvt->cvt_nid;
1179 pin_cvt_fixup(codec, NULL, per_cvt->cvt_nid);
1181 set_bit(pcm_idx, &spec->pcm_in_use);
1182 /* todo: setup spdif ctls assign */
1184 /* Initially set the converter's capabilities */
1185 hinfo->channels_min = per_cvt->channels_min;
1186 hinfo->channels_max = per_cvt->channels_max;
1187 hinfo->rates = per_cvt->rates;
1188 hinfo->formats = per_cvt->formats;
1189 hinfo->maxbps = per_cvt->maxbps;
1191 /* Store the updated parameters */
1192 runtime->hw.channels_min = hinfo->channels_min;
1193 runtime->hw.channels_max = hinfo->channels_max;
1194 runtime->hw.formats = hinfo->formats;
1195 runtime->hw.rates = hinfo->rates;
1197 snd_pcm_hw_constraint_step(substream->runtime, 0,
1198 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1205 static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1206 struct hda_codec *codec,
1207 struct snd_pcm_substream *substream)
1209 struct hdmi_spec *spec = codec->spec;
1210 struct snd_pcm_runtime *runtime = substream->runtime;
1211 int pin_idx, cvt_idx, pcm_idx;
1212 struct hdmi_spec_per_pin *per_pin;
1213 struct hdmi_eld *eld;
1214 struct hdmi_spec_per_cvt *per_cvt = NULL;
1217 /* Validate hinfo */
1218 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1222 mutex_lock(&spec->pcm_lock);
1223 pin_idx = hinfo_to_pin_index(codec, hinfo);
1224 if (!spec->dyn_pcm_assign) {
1225 if (snd_BUG_ON(pin_idx < 0)) {
1230 /* no pin is assigned to the PCM
1231 * PA need pcm open successfully when probe
1234 err = hdmi_pcm_open_no_pin(hinfo, codec, substream);
1239 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx);
1243 per_cvt = get_cvt(spec, cvt_idx);
1244 /* Claim converter */
1245 per_cvt->assigned = 1;
1247 set_bit(pcm_idx, &spec->pcm_in_use);
1248 per_pin = get_pin(spec, pin_idx);
1249 per_pin->cvt_nid = per_cvt->cvt_nid;
1250 hinfo->nid = per_cvt->cvt_nid;
1252 snd_hda_set_dev_select(codec, per_pin->pin_nid, per_pin->dev_id);
1253 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1254 AC_VERB_SET_CONNECT_SEL,
1257 /* configure unused pins to choose other converters */
1258 pin_cvt_fixup(codec, per_pin, 0);
1260 snd_hda_spdif_ctls_assign(codec, pcm_idx, per_cvt->cvt_nid);
1262 /* Initially set the converter's capabilities */
1263 hinfo->channels_min = per_cvt->channels_min;
1264 hinfo->channels_max = per_cvt->channels_max;
1265 hinfo->rates = per_cvt->rates;
1266 hinfo->formats = per_cvt->formats;
1267 hinfo->maxbps = per_cvt->maxbps;
1269 eld = &per_pin->sink_eld;
1270 /* Restrict capabilities by ELD if this isn't disabled */
1271 if (!static_hdmi_pcm && eld->eld_valid) {
1272 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
1273 if (hinfo->channels_min > hinfo->channels_max ||
1274 !hinfo->rates || !hinfo->formats) {
1275 per_cvt->assigned = 0;
1277 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
1283 /* Store the updated parameters */
1284 runtime->hw.channels_min = hinfo->channels_min;
1285 runtime->hw.channels_max = hinfo->channels_max;
1286 runtime->hw.formats = hinfo->formats;
1287 runtime->hw.rates = hinfo->rates;
1289 snd_pcm_hw_constraint_step(substream->runtime, 0,
1290 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1292 mutex_unlock(&spec->pcm_lock);
1297 * HDA/HDMI auto parsing
1299 static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
1301 struct hdmi_spec *spec = codec->spec;
1302 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1303 hda_nid_t pin_nid = per_pin->pin_nid;
1304 int dev_id = per_pin->dev_id;
1306 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
1308 "HDMI: pin %d wcaps %#x does not support connection list\n",
1309 pin_nid, get_wcaps(codec, pin_nid));
1313 snd_hda_set_dev_select(codec, pin_nid, dev_id);
1315 /* all the device entries on the same pin have the same conn list */
1316 per_pin->num_mux_nids =
1317 snd_hda_get_raw_connections(codec, pin_nid, per_pin->mux_nids,
1318 HDA_MAX_CONNECTIONS);
1323 static int hdmi_find_pcm_slot(struct hdmi_spec *spec,
1324 struct hdmi_spec_per_pin *per_pin)
1328 /* try the prefer PCM */
1329 if (!test_bit(per_pin->pin_nid_idx, &spec->pcm_bitmap))
1330 return per_pin->pin_nid_idx;
1332 /* have a second try; check the "reserved area" over num_pins */
1333 for (i = spec->num_nids; i < spec->pcm_used; i++) {
1334 if (!test_bit(i, &spec->pcm_bitmap))
1338 /* the last try; check the empty slots in pins */
1339 for (i = 0; i < spec->num_nids; i++) {
1340 if (!test_bit(i, &spec->pcm_bitmap))
1346 static void hdmi_attach_hda_pcm(struct hdmi_spec *spec,
1347 struct hdmi_spec_per_pin *per_pin)
1351 /* pcm already be attached to the pin */
1354 idx = hdmi_find_pcm_slot(spec, per_pin);
1357 per_pin->pcm_idx = idx;
1358 per_pin->pcm = get_hdmi_pcm(spec, idx);
1359 set_bit(idx, &spec->pcm_bitmap);
1362 static void hdmi_detach_hda_pcm(struct hdmi_spec *spec,
1363 struct hdmi_spec_per_pin *per_pin)
1367 /* pcm already be detached from the pin */
1370 idx = per_pin->pcm_idx;
1371 per_pin->pcm_idx = -1;
1372 per_pin->pcm = NULL;
1373 if (idx >= 0 && idx < spec->pcm_used)
1374 clear_bit(idx, &spec->pcm_bitmap);
1377 static int hdmi_get_pin_cvt_mux(struct hdmi_spec *spec,
1378 struct hdmi_spec_per_pin *per_pin, hda_nid_t cvt_nid)
1382 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1383 if (per_pin->mux_nids[mux_idx] == cvt_nid)
1388 static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid);
1390 static void hdmi_pcm_setup_pin(struct hdmi_spec *spec,
1391 struct hdmi_spec_per_pin *per_pin)
1393 struct hda_codec *codec = per_pin->codec;
1394 struct hda_pcm *pcm;
1395 struct hda_pcm_stream *hinfo;
1396 struct snd_pcm_substream *substream;
1400 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1401 pcm = get_pcm_rec(spec, per_pin->pcm_idx);
1406 if (!test_bit(per_pin->pcm_idx, &spec->pcm_in_use))
1409 /* hdmi audio only uses playback and one substream */
1410 hinfo = pcm->stream;
1411 substream = pcm->pcm->streams[0].substream;
1413 per_pin->cvt_nid = hinfo->nid;
1415 mux_idx = hdmi_get_pin_cvt_mux(spec, per_pin, hinfo->nid);
1416 if (mux_idx < per_pin->num_mux_nids) {
1417 snd_hda_set_dev_select(codec, per_pin->pin_nid,
1419 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1420 AC_VERB_SET_CONNECT_SEL,
1423 snd_hda_spdif_ctls_assign(codec, per_pin->pcm_idx, hinfo->nid);
1425 non_pcm = check_non_pcm_per_cvt(codec, hinfo->nid);
1426 if (substream->runtime)
1427 per_pin->channels = substream->runtime->channels;
1428 per_pin->setup = true;
1429 per_pin->mux_idx = mux_idx;
1431 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1434 static void hdmi_pcm_reset_pin(struct hdmi_spec *spec,
1435 struct hdmi_spec_per_pin *per_pin)
1437 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1438 snd_hda_spdif_ctls_unassign(per_pin->codec, per_pin->pcm_idx);
1440 per_pin->chmap_set = false;
1441 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
1443 per_pin->setup = false;
1444 per_pin->channels = 0;
1447 /* update per_pin ELD from the given new ELD;
1448 * setup info frame and notification accordingly
1450 static bool update_eld(struct hda_codec *codec,
1451 struct hdmi_spec_per_pin *per_pin,
1452 struct hdmi_eld *eld)
1454 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
1455 struct hdmi_spec *spec = codec->spec;
1456 bool old_eld_valid = pin_eld->eld_valid;
1460 /* for monitor disconnection, save pcm_idx firstly */
1461 pcm_idx = per_pin->pcm_idx;
1462 if (spec->dyn_pcm_assign) {
1463 if (eld->eld_valid) {
1464 hdmi_attach_hda_pcm(spec, per_pin);
1465 hdmi_pcm_setup_pin(spec, per_pin);
1467 hdmi_pcm_reset_pin(spec, per_pin);
1468 hdmi_detach_hda_pcm(spec, per_pin);
1471 /* if pcm_idx == -1, it means this is in monitor connection event
1472 * we can get the correct pcm_idx now.
1475 pcm_idx = per_pin->pcm_idx;
1478 snd_hdmi_show_eld(codec, &eld->info);
1480 eld_changed = (pin_eld->eld_valid != eld->eld_valid);
1481 eld_changed |= (pin_eld->monitor_present != eld->monitor_present);
1482 if (!eld_changed && eld->eld_valid && pin_eld->eld_valid)
1483 if (pin_eld->eld_size != eld->eld_size ||
1484 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
1485 eld->eld_size) != 0)
1489 pin_eld->monitor_present = eld->monitor_present;
1490 pin_eld->eld_valid = eld->eld_valid;
1491 pin_eld->eld_size = eld->eld_size;
1493 memcpy(pin_eld->eld_buffer, eld->eld_buffer,
1495 pin_eld->info = eld->info;
1499 * Re-setup pin and infoframe. This is needed e.g. when
1500 * - sink is first plugged-in
1501 * - transcoder can change during stream playback on Haswell
1502 * and this can make HW reset converter selection on a pin.
1504 if (eld->eld_valid && !old_eld_valid && per_pin->setup) {
1505 pin_cvt_fixup(codec, per_pin, 0);
1506 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1509 if (eld_changed && pcm_idx >= 0)
1510 snd_ctl_notify(codec->card,
1511 SNDRV_CTL_EVENT_MASK_VALUE |
1512 SNDRV_CTL_EVENT_MASK_INFO,
1513 &get_hdmi_pcm(spec, pcm_idx)->eld_ctl->id);
1517 /* update ELD and jack state via HD-audio verbs */
1518 static bool hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin *per_pin,
1521 struct hda_jack_tbl *jack;
1522 struct hda_codec *codec = per_pin->codec;
1523 struct hdmi_spec *spec = codec->spec;
1524 struct hdmi_eld *eld = &spec->temp_eld;
1525 hda_nid_t pin_nid = per_pin->pin_nid;
1526 int dev_id = per_pin->dev_id;
1528 * Always execute a GetPinSense verb here, even when called from
1529 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1530 * response's PD bit is not the real PD value, but indicates that
1531 * the real PD value changed. An older version of the HD-audio
1532 * specification worked this way. Hence, we just ignore the data in
1533 * the unsolicited response to avoid custom WARs.
1537 bool do_repoll = false;
1539 present = snd_hda_jack_pin_sense(codec, pin_nid, dev_id);
1541 mutex_lock(&per_pin->lock);
1542 eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1543 if (eld->monitor_present)
1544 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1546 eld->eld_valid = false;
1549 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
1550 codec->addr, pin_nid, eld->monitor_present, eld->eld_valid);
1552 if (eld->eld_valid) {
1553 if (spec->ops.pin_get_eld(codec, pin_nid, dev_id,
1554 eld->eld_buffer, &eld->eld_size) < 0)
1555 eld->eld_valid = false;
1557 if (snd_hdmi_parse_eld(codec, &eld->info, eld->eld_buffer,
1559 eld->eld_valid = false;
1561 if (!eld->eld_valid && repoll)
1566 schedule_delayed_work(&per_pin->work, msecs_to_jiffies(300));
1568 update_eld(codec, per_pin, eld);
1570 ret = !repoll || !eld->monitor_present || eld->eld_valid;
1572 jack = snd_hda_jack_tbl_get_mst(codec, pin_nid, per_pin->dev_id);
1574 jack->block_report = !ret;
1575 jack->pin_sense = (eld->monitor_present && eld->eld_valid) ?
1576 AC_PINSENSE_PRESENCE : 0;
1578 mutex_unlock(&per_pin->lock);
1582 static struct snd_jack *pin_idx_to_jack(struct hda_codec *codec,
1583 struct hdmi_spec_per_pin *per_pin)
1585 struct hdmi_spec *spec = codec->spec;
1586 struct snd_jack *jack = NULL;
1587 struct hda_jack_tbl *jack_tbl;
1589 /* if !dyn_pcm_assign, get jack from hda_jack_tbl
1590 * in !dyn_pcm_assign case, spec->pcm_rec[].jack is not
1591 * NULL even after snd_hda_jack_tbl_clear() is called to
1592 * free snd_jack. This may cause access invalid memory
1593 * when calling snd_jack_report
1595 if (per_pin->pcm_idx >= 0 && spec->dyn_pcm_assign)
1596 jack = spec->pcm_rec[per_pin->pcm_idx].jack;
1597 else if (!spec->dyn_pcm_assign) {
1599 * jack tbl doesn't support DP MST
1600 * DP MST will use dyn_pcm_assign,
1601 * so DP MST will never come here
1603 jack_tbl = snd_hda_jack_tbl_get_mst(codec, per_pin->pin_nid,
1606 jack = jack_tbl->jack;
1611 /* update ELD and jack state via audio component */
1612 static void sync_eld_via_acomp(struct hda_codec *codec,
1613 struct hdmi_spec_per_pin *per_pin)
1615 struct hdmi_spec *spec = codec->spec;
1616 struct hdmi_eld *eld = &spec->temp_eld;
1617 struct snd_jack *jack = NULL;
1621 mutex_lock(&per_pin->lock);
1622 eld->monitor_present = false;
1623 size = snd_hdac_acomp_get_eld(&codec->core, per_pin->pin_nid,
1624 per_pin->dev_id, &eld->monitor_present,
1625 eld->eld_buffer, ELD_MAX_SIZE);
1627 size = min(size, ELD_MAX_SIZE);
1628 if (snd_hdmi_parse_eld(codec, &eld->info,
1629 eld->eld_buffer, size) < 0)
1634 eld->eld_valid = true;
1635 eld->eld_size = size;
1637 eld->eld_valid = false;
1641 /* pcm_idx >=0 before update_eld() means it is in monitor
1642 * disconnected event. Jack must be fetched before update_eld()
1644 jack = pin_idx_to_jack(codec, per_pin);
1645 changed = update_eld(codec, per_pin, eld);
1647 jack = pin_idx_to_jack(codec, per_pin);
1648 if (changed && jack)
1649 snd_jack_report(jack,
1650 (eld->monitor_present && eld->eld_valid) ?
1651 SND_JACK_AVOUT : 0);
1652 mutex_unlock(&per_pin->lock);
1655 static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
1657 struct hda_codec *codec = per_pin->codec;
1660 /* no temporary power up/down needed for component notifier */
1661 if (!codec_has_acomp(codec)) {
1662 ret = snd_hda_power_up_pm(codec);
1663 if (ret < 0 && pm_runtime_suspended(hda_codec_dev(codec))) {
1664 snd_hda_power_down_pm(codec);
1667 ret = hdmi_present_sense_via_verbs(per_pin, repoll);
1668 snd_hda_power_down_pm(codec);
1670 sync_eld_via_acomp(codec, per_pin);
1671 ret = false; /* don't call snd_hda_jack_report_sync() */
1677 static void hdmi_repoll_eld(struct work_struct *work)
1679 struct hdmi_spec_per_pin *per_pin =
1680 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1681 struct hda_codec *codec = per_pin->codec;
1682 struct hdmi_spec *spec = codec->spec;
1683 struct hda_jack_tbl *jack;
1685 jack = snd_hda_jack_tbl_get_mst(codec, per_pin->pin_nid,
1688 jack->jack_dirty = 1;
1690 if (per_pin->repoll_count++ > 6)
1691 per_pin->repoll_count = 0;
1693 mutex_lock(&spec->pcm_lock);
1694 if (hdmi_present_sense(per_pin, per_pin->repoll_count))
1695 snd_hda_jack_report_sync(per_pin->codec);
1696 mutex_unlock(&spec->pcm_lock);
1699 static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1702 static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1704 struct hdmi_spec *spec = codec->spec;
1705 unsigned int caps, config;
1707 struct hdmi_spec_per_pin *per_pin;
1711 caps = snd_hda_query_pin_caps(codec, pin_nid);
1712 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1716 * For DP MST audio, Configuration Default is the same for
1717 * all device entries on the same pin
1719 config = snd_hda_codec_get_pincfg(codec, pin_nid);
1720 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
1724 * To simplify the implementation, malloc all
1725 * the virtual pins in the initialization statically
1727 if (spec->intel_hsw_fixup) {
1729 * On Intel platforms, device entries number is
1730 * changed dynamically. If there is a DP MST
1731 * hub connected, the device entries number is 3.
1732 * Otherwise, it is 1.
1733 * Here we manually set dev_num to 3, so that
1734 * we can initialize all the device entries when
1735 * bootup statically.
1739 } else if (spec->dyn_pcm_assign && codec->dp_mst) {
1740 dev_num = snd_hda_get_num_devices(codec, pin_nid) + 1;
1742 * spec->dev_num is the maxinum number of device entries
1743 * among all the pins
1745 spec->dev_num = (spec->dev_num > dev_num) ?
1746 spec->dev_num : dev_num;
1749 * If the platform doesn't support DP MST,
1750 * manually set dev_num to 1. This means
1751 * the pin has only one device entry.
1757 for (i = 0; i < dev_num; i++) {
1758 pin_idx = spec->num_pins;
1759 per_pin = snd_array_new(&spec->pins);
1764 if (spec->dyn_pcm_assign) {
1765 per_pin->pcm = NULL;
1766 per_pin->pcm_idx = -1;
1768 per_pin->pcm = get_hdmi_pcm(spec, pin_idx);
1769 per_pin->pcm_idx = pin_idx;
1771 per_pin->pin_nid = pin_nid;
1772 per_pin->pin_nid_idx = spec->num_nids;
1773 per_pin->dev_id = i;
1774 per_pin->non_pcm = false;
1775 snd_hda_set_dev_select(codec, pin_nid, i);
1776 if (spec->intel_hsw_fixup)
1777 intel_haswell_fixup_connect_list(codec, pin_nid);
1778 err = hdmi_read_pin_conn(codec, pin_idx);
1788 static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1790 struct hdmi_spec *spec = codec->spec;
1791 struct hdmi_spec_per_cvt *per_cvt;
1795 chans = get_wcaps(codec, cvt_nid);
1796 chans = get_wcaps_channels(chans);
1798 per_cvt = snd_array_new(&spec->cvts);
1802 per_cvt->cvt_nid = cvt_nid;
1803 per_cvt->channels_min = 2;
1805 per_cvt->channels_max = chans;
1806 if (chans > spec->chmap.channels_max)
1807 spec->chmap.channels_max = chans;
1810 err = snd_hda_query_supported_pcm(codec, cvt_nid,
1817 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
1818 spec->cvt_nids[spec->num_cvts] = cvt_nid;
1824 static int hdmi_parse_codec(struct hda_codec *codec)
1829 nodes = snd_hda_get_sub_nodes(codec, codec->core.afg, &nid);
1830 if (!nid || nodes < 0) {
1831 codec_warn(codec, "HDMI: failed to get afg sub nodes\n");
1835 for (i = 0; i < nodes; i++, nid++) {
1839 caps = get_wcaps(codec, nid);
1840 type = get_wcaps_type(caps);
1842 if (!(caps & AC_WCAP_DIGITAL))
1846 case AC_WID_AUD_OUT:
1847 hdmi_add_cvt(codec, nid);
1850 hdmi_add_pin(codec, nid);
1860 static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1862 struct hda_spdif_out *spdif;
1865 mutex_lock(&codec->spdif_mutex);
1866 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
1867 /* Add sanity check to pass klockwork check.
1868 * This should never happen.
1870 if (WARN_ON(spdif == NULL))
1872 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
1873 mutex_unlock(&codec->spdif_mutex);
1881 static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1882 struct hda_codec *codec,
1883 unsigned int stream_tag,
1884 unsigned int format,
1885 struct snd_pcm_substream *substream)
1887 hda_nid_t cvt_nid = hinfo->nid;
1888 struct hdmi_spec *spec = codec->spec;
1890 struct hdmi_spec_per_pin *per_pin;
1891 struct snd_pcm_runtime *runtime = substream->runtime;
1896 mutex_lock(&spec->pcm_lock);
1897 pin_idx = hinfo_to_pin_index(codec, hinfo);
1898 if (spec->dyn_pcm_assign && pin_idx < 0) {
1899 /* when dyn_pcm_assign and pcm is not bound to a pin
1900 * skip pin setup and return 0 to make audio playback
1903 pin_cvt_fixup(codec, NULL, cvt_nid);
1904 snd_hda_codec_setup_stream(codec, cvt_nid,
1905 stream_tag, 0, format);
1909 if (snd_BUG_ON(pin_idx < 0)) {
1913 per_pin = get_pin(spec, pin_idx);
1915 /* Verify pin:cvt selections to avoid silent audio after S3.
1916 * After S3, the audio driver restores pin:cvt selections
1917 * but this can happen before gfx is ready and such selection
1918 * is overlooked by HW. Thus multiple pins can share a same
1919 * default convertor and mute control will affect each other,
1920 * which can cause a resumed audio playback become silent
1923 pin_cvt_fixup(codec, per_pin, 0);
1925 /* Call sync_audio_rate to set the N/CTS/M manually if necessary */
1926 /* Todo: add DP1.2 MST audio support later */
1927 if (codec_has_acomp(codec))
1928 snd_hdac_sync_audio_rate(&codec->core, per_pin->pin_nid,
1929 per_pin->dev_id, runtime->rate);
1931 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
1932 mutex_lock(&per_pin->lock);
1933 per_pin->channels = substream->runtime->channels;
1934 per_pin->setup = true;
1936 if (get_wcaps(codec, cvt_nid) & AC_WCAP_STRIPE) {
1937 stripe = snd_hdac_get_stream_stripe_ctl(&codec->bus->core,
1939 snd_hda_codec_write(codec, cvt_nid, 0,
1940 AC_VERB_SET_STRIPE_CONTROL,
1944 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1945 mutex_unlock(&per_pin->lock);
1946 if (spec->dyn_pin_out) {
1947 snd_hda_set_dev_select(codec, per_pin->pin_nid,
1949 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
1950 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1951 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
1952 AC_VERB_SET_PIN_WIDGET_CONTROL,
1956 /* snd_hda_set_dev_select() has been called before */
1957 err = spec->ops.setup_stream(codec, cvt_nid, per_pin->pin_nid,
1958 per_pin->dev_id, stream_tag, format);
1960 mutex_unlock(&spec->pcm_lock);
1964 static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1965 struct hda_codec *codec,
1966 struct snd_pcm_substream *substream)
1968 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1972 static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
1973 struct hda_codec *codec,
1974 struct snd_pcm_substream *substream)
1976 struct hdmi_spec *spec = codec->spec;
1977 int cvt_idx, pin_idx, pcm_idx;
1978 struct hdmi_spec_per_cvt *per_cvt;
1979 struct hdmi_spec_per_pin *per_pin;
1984 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1985 if (snd_BUG_ON(pcm_idx < 0))
1987 cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid);
1988 if (snd_BUG_ON(cvt_idx < 0))
1990 per_cvt = get_cvt(spec, cvt_idx);
1992 snd_BUG_ON(!per_cvt->assigned);
1993 per_cvt->assigned = 0;
1996 mutex_lock(&spec->pcm_lock);
1997 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
1998 clear_bit(pcm_idx, &spec->pcm_in_use);
1999 pin_idx = hinfo_to_pin_index(codec, hinfo);
2000 if (spec->dyn_pcm_assign && pin_idx < 0)
2003 if (snd_BUG_ON(pin_idx < 0)) {
2007 per_pin = get_pin(spec, pin_idx);
2009 if (spec->dyn_pin_out) {
2010 snd_hda_set_dev_select(codec, per_pin->pin_nid,
2012 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
2013 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2014 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
2015 AC_VERB_SET_PIN_WIDGET_CONTROL,
2019 mutex_lock(&per_pin->lock);
2020 per_pin->chmap_set = false;
2021 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
2023 per_pin->setup = false;
2024 per_pin->channels = 0;
2025 mutex_unlock(&per_pin->lock);
2027 mutex_unlock(&spec->pcm_lock);
2033 static const struct hda_pcm_ops generic_ops = {
2034 .open = hdmi_pcm_open,
2035 .close = hdmi_pcm_close,
2036 .prepare = generic_hdmi_playback_pcm_prepare,
2037 .cleanup = generic_hdmi_playback_pcm_cleanup,
2040 static int hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
2042 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2043 struct hdmi_spec *spec = codec->spec;
2044 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2049 return per_pin->sink_eld.info.spk_alloc;
2052 static void hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
2053 unsigned char *chmap)
2055 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2056 struct hdmi_spec *spec = codec->spec;
2057 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2059 /* chmap is already set to 0 in caller */
2063 memcpy(chmap, per_pin->chmap, ARRAY_SIZE(per_pin->chmap));
2066 static void hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
2067 unsigned char *chmap, int prepared)
2069 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2070 struct hdmi_spec *spec = codec->spec;
2071 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2075 mutex_lock(&per_pin->lock);
2076 per_pin->chmap_set = true;
2077 memcpy(per_pin->chmap, chmap, ARRAY_SIZE(per_pin->chmap));
2079 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
2080 mutex_unlock(&per_pin->lock);
2083 static bool is_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
2085 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2086 struct hdmi_spec *spec = codec->spec;
2087 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2089 return per_pin ? true:false;
2092 static int generic_hdmi_build_pcms(struct hda_codec *codec)
2094 struct hdmi_spec *spec = codec->spec;
2098 * for non-mst mode, pcm number is the same as before
2099 * for DP MST mode without extra PCM, pcm number is same
2100 * for DP MST mode with extra PCMs, pcm number is
2101 * (nid number + dev_num - 1)
2102 * dev_num is the device entry number in a pin
2105 if (codec->mst_no_extra_pcms)
2106 pcm_num = spec->num_nids;
2108 pcm_num = spec->num_nids + spec->dev_num - 1;
2110 codec_dbg(codec, "hdmi: pcm_num set to %d\n", pcm_num);
2112 for (idx = 0; idx < pcm_num; idx++) {
2113 struct hda_pcm *info;
2114 struct hda_pcm_stream *pstr;
2116 info = snd_hda_codec_pcm_new(codec, "HDMI %d", idx);
2120 spec->pcm_rec[idx].pcm = info;
2122 info->pcm_type = HDA_PCM_TYPE_HDMI;
2123 info->own_chmap = true;
2125 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2126 pstr->substreams = 1;
2127 pstr->ops = generic_ops;
2128 /* pcm number is less than 16 */
2129 if (spec->pcm_used >= 16)
2131 /* other pstr fields are set in open */
2137 static void free_hdmi_jack_priv(struct snd_jack *jack)
2139 struct hdmi_pcm *pcm = jack->private_data;
2144 static int add_hdmi_jack_kctl(struct hda_codec *codec,
2145 struct hdmi_spec *spec,
2149 struct snd_jack *jack;
2152 err = snd_jack_new(codec->card, name, SND_JACK_AVOUT, &jack,
2157 spec->pcm_rec[pcm_idx].jack = jack;
2158 jack->private_data = &spec->pcm_rec[pcm_idx];
2159 jack->private_free = free_hdmi_jack_priv;
2163 static int generic_hdmi_build_jack(struct hda_codec *codec, int pcm_idx)
2165 char hdmi_str[32] = "HDMI/DP";
2166 struct hdmi_spec *spec = codec->spec;
2167 struct hdmi_spec_per_pin *per_pin;
2168 struct hda_jack_tbl *jack;
2169 int pcmdev = get_pcm_rec(spec, pcm_idx)->device;
2174 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
2176 if (spec->dyn_pcm_assign)
2177 return add_hdmi_jack_kctl(codec, spec, pcm_idx, hdmi_str);
2179 /* for !dyn_pcm_assign, we still use hda_jack for compatibility */
2180 /* if !dyn_pcm_assign, it must be non-MST mode.
2181 * This means pcms and pins are statically mapped.
2182 * And pcm_idx is pin_idx.
2184 per_pin = get_pin(spec, pcm_idx);
2185 phantom_jack = !is_jack_detectable(codec, per_pin->pin_nid);
2187 strncat(hdmi_str, " Phantom",
2188 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
2189 ret = snd_hda_jack_add_kctl_mst(codec, per_pin->pin_nid,
2190 per_pin->dev_id, hdmi_str, phantom_jack,
2194 jack = snd_hda_jack_tbl_get_mst(codec, per_pin->pin_nid,
2198 /* assign jack->jack to pcm_rec[].jack to
2199 * align with dyn_pcm_assign mode
2201 spec->pcm_rec[pcm_idx].jack = jack->jack;
2205 static int generic_hdmi_build_controls(struct hda_codec *codec)
2207 struct hdmi_spec *spec = codec->spec;
2209 int pin_idx, pcm_idx;
2211 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2212 if (!get_pcm_rec(spec, pcm_idx)->pcm) {
2213 /* no PCM: mark this for skipping permanently */
2214 set_bit(pcm_idx, &spec->pcm_bitmap);
2218 err = generic_hdmi_build_jack(codec, pcm_idx);
2222 /* create the spdif for each pcm
2223 * pin will be bound when monitor is connected
2225 if (spec->dyn_pcm_assign)
2226 err = snd_hda_create_dig_out_ctls(codec,
2227 0, spec->cvt_nids[0],
2230 struct hdmi_spec_per_pin *per_pin =
2231 get_pin(spec, pcm_idx);
2232 err = snd_hda_create_dig_out_ctls(codec,
2234 per_pin->mux_nids[0],
2239 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
2241 dev = get_pcm_rec(spec, pcm_idx)->device;
2242 if (dev != SNDRV_PCM_INVALID_DEVICE) {
2243 /* add control for ELD Bytes */
2244 err = hdmi_create_eld_ctl(codec, pcm_idx, dev);
2250 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2251 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2253 hdmi_present_sense(per_pin, 0);
2256 /* add channel maps */
2257 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2258 struct hda_pcm *pcm;
2260 pcm = get_pcm_rec(spec, pcm_idx);
2261 if (!pcm || !pcm->pcm)
2263 err = snd_hdac_add_chmap_ctls(pcm->pcm, pcm_idx, &spec->chmap);
2271 static int generic_hdmi_init_per_pins(struct hda_codec *codec)
2273 struct hdmi_spec *spec = codec->spec;
2276 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2277 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2279 per_pin->codec = codec;
2280 mutex_init(&per_pin->lock);
2281 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
2282 eld_proc_new(per_pin, pin_idx);
2287 static int generic_hdmi_init(struct hda_codec *codec)
2289 struct hdmi_spec *spec = codec->spec;
2292 mutex_lock(&spec->bind_lock);
2293 spec->use_jack_detect = !codec->jackpoll_interval;
2294 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2295 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2296 hda_nid_t pin_nid = per_pin->pin_nid;
2297 int dev_id = per_pin->dev_id;
2299 snd_hda_set_dev_select(codec, pin_nid, dev_id);
2300 hdmi_init_pin(codec, pin_nid);
2301 if (codec_has_acomp(codec))
2303 if (spec->use_jack_detect)
2304 snd_hda_jack_detect_enable(codec, pin_nid, dev_id);
2306 snd_hda_jack_detect_enable_callback_mst(codec, pin_nid,
2310 mutex_unlock(&spec->bind_lock);
2314 static void hdmi_array_init(struct hdmi_spec *spec, int nums)
2316 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
2317 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
2320 static void hdmi_array_free(struct hdmi_spec *spec)
2322 snd_array_free(&spec->pins);
2323 snd_array_free(&spec->cvts);
2326 static void generic_spec_free(struct hda_codec *codec)
2328 struct hdmi_spec *spec = codec->spec;
2331 hdmi_array_free(spec);
2335 codec->dp_mst = false;
2338 static void generic_hdmi_free(struct hda_codec *codec)
2340 struct hdmi_spec *spec = codec->spec;
2341 int pin_idx, pcm_idx;
2343 if (spec->acomp_registered) {
2344 snd_hdac_acomp_exit(&codec->bus->core);
2345 } else if (codec_has_acomp(codec)) {
2346 snd_hdac_acomp_register_notifier(&codec->bus->core, NULL);
2347 codec->relaxed_resume = 0;
2350 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2351 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2352 cancel_delayed_work_sync(&per_pin->work);
2353 eld_proc_free(per_pin);
2356 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2357 if (spec->pcm_rec[pcm_idx].jack == NULL)
2359 if (spec->dyn_pcm_assign)
2360 snd_device_free(codec->card,
2361 spec->pcm_rec[pcm_idx].jack);
2363 spec->pcm_rec[pcm_idx].jack = NULL;
2366 generic_spec_free(codec);
2370 static int generic_hdmi_resume(struct hda_codec *codec)
2372 struct hdmi_spec *spec = codec->spec;
2375 codec->patch_ops.init(codec);
2376 regcache_sync(codec->core.regmap);
2378 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2379 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2380 hdmi_present_sense(per_pin, 1);
2386 static const struct hda_codec_ops generic_hdmi_patch_ops = {
2387 .init = generic_hdmi_init,
2388 .free = generic_hdmi_free,
2389 .build_pcms = generic_hdmi_build_pcms,
2390 .build_controls = generic_hdmi_build_controls,
2391 .unsol_event = hdmi_unsol_event,
2393 .resume = generic_hdmi_resume,
2397 static const struct hdmi_ops generic_standard_hdmi_ops = {
2398 .pin_get_eld = hdmi_pin_get_eld,
2399 .pin_setup_infoframe = hdmi_pin_setup_infoframe,
2400 .pin_hbr_setup = hdmi_pin_hbr_setup,
2401 .setup_stream = hdmi_setup_stream,
2404 /* allocate codec->spec and assign/initialize generic parser ops */
2405 static int alloc_generic_hdmi(struct hda_codec *codec)
2407 struct hdmi_spec *spec;
2409 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2413 spec->codec = codec;
2414 spec->ops = generic_standard_hdmi_ops;
2415 spec->dev_num = 1; /* initialize to 1 */
2416 mutex_init(&spec->pcm_lock);
2417 mutex_init(&spec->bind_lock);
2418 snd_hdac_register_chmap_ops(&codec->core, &spec->chmap);
2420 spec->chmap.ops.get_chmap = hdmi_get_chmap;
2421 spec->chmap.ops.set_chmap = hdmi_set_chmap;
2422 spec->chmap.ops.is_pcm_attached = is_hdmi_pcm_attached;
2423 spec->chmap.ops.get_spk_alloc = hdmi_get_spk_alloc,
2426 hdmi_array_init(spec, 4);
2428 codec->patch_ops = generic_hdmi_patch_ops;
2433 /* generic HDMI parser */
2434 static int patch_generic_hdmi(struct hda_codec *codec)
2438 err = alloc_generic_hdmi(codec);
2442 err = hdmi_parse_codec(codec);
2444 generic_spec_free(codec);
2448 generic_hdmi_init_per_pins(codec);
2453 * generic audio component binding
2456 /* turn on / off the unsol event jack detection dynamically */
2457 static void reprogram_jack_detect(struct hda_codec *codec, hda_nid_t nid,
2458 int dev_id, bool use_acomp)
2460 struct hda_jack_tbl *tbl;
2462 tbl = snd_hda_jack_tbl_get_mst(codec, nid, dev_id);
2464 /* clear unsol even if component notifier is used, or re-enable
2465 * if notifier is cleared
2467 unsigned int val = use_acomp ? 0 : (AC_USRSP_EN | tbl->tag);
2468 snd_hda_codec_write_cache(codec, nid, 0,
2469 AC_VERB_SET_UNSOLICITED_ENABLE, val);
2471 /* if no jack entry was defined beforehand, create a new one
2472 * at need (i.e. only when notifier is cleared)
2475 snd_hda_jack_detect_enable(codec, nid, dev_id);
2479 /* set up / clear component notifier dynamically */
2480 static void generic_acomp_notifier_set(struct drm_audio_component *acomp,
2483 struct hdmi_spec *spec;
2486 spec = container_of(acomp->audio_ops, struct hdmi_spec, drm_audio_ops);
2487 mutex_lock(&spec->bind_lock);
2488 spec->use_acomp_notifier = use_acomp;
2489 spec->codec->relaxed_resume = use_acomp;
2490 /* reprogram each jack detection logic depending on the notifier */
2491 if (spec->use_jack_detect) {
2492 for (i = 0; i < spec->num_pins; i++)
2493 reprogram_jack_detect(spec->codec,
2494 get_pin(spec, i)->pin_nid,
2495 get_pin(spec, i)->dev_id,
2498 mutex_unlock(&spec->bind_lock);
2501 /* enable / disable the notifier via master bind / unbind */
2502 static int generic_acomp_master_bind(struct device *dev,
2503 struct drm_audio_component *acomp)
2505 generic_acomp_notifier_set(acomp, true);
2509 static void generic_acomp_master_unbind(struct device *dev,
2510 struct drm_audio_component *acomp)
2512 generic_acomp_notifier_set(acomp, false);
2515 /* check whether both HD-audio and DRM PCI devices belong to the same bus */
2516 static int match_bound_vga(struct device *dev, int subtype, void *data)
2518 struct hdac_bus *bus = data;
2519 struct pci_dev *pci, *master;
2521 if (!dev_is_pci(dev) || !dev_is_pci(bus->dev))
2523 master = to_pci_dev(bus->dev);
2524 pci = to_pci_dev(dev);
2525 return master->bus == pci->bus;
2528 /* audio component notifier for AMD/Nvidia HDMI codecs */
2529 static void generic_acomp_pin_eld_notify(void *audio_ptr, int port, int dev_id)
2531 struct hda_codec *codec = audio_ptr;
2532 struct hdmi_spec *spec = codec->spec;
2533 hda_nid_t pin_nid = spec->port2pin(codec, port);
2537 if (get_wcaps_type(get_wcaps(codec, pin_nid)) != AC_WID_PIN)
2539 /* skip notification during system suspend (but not in runtime PM);
2540 * the state will be updated at resume
2542 if (snd_power_get_state(codec->card) != SNDRV_CTL_POWER_D0)
2544 /* ditto during suspend/resume process itself */
2545 if (snd_hdac_is_in_pm(&codec->core))
2548 check_presence_and_report(codec, pin_nid, dev_id);
2551 /* set up the private drm_audio_ops from the template */
2552 static void setup_drm_audio_ops(struct hda_codec *codec,
2553 const struct drm_audio_component_audio_ops *ops)
2555 struct hdmi_spec *spec = codec->spec;
2557 spec->drm_audio_ops.audio_ptr = codec;
2558 /* intel_audio_codec_enable() or intel_audio_codec_disable()
2559 * will call pin_eld_notify with using audio_ptr pointer
2560 * We need make sure audio_ptr is really setup
2563 spec->drm_audio_ops.pin2port = ops->pin2port;
2564 spec->drm_audio_ops.pin_eld_notify = ops->pin_eld_notify;
2565 spec->drm_audio_ops.master_bind = ops->master_bind;
2566 spec->drm_audio_ops.master_unbind = ops->master_unbind;
2569 /* initialize the generic HDMI audio component */
2570 static void generic_acomp_init(struct hda_codec *codec,
2571 const struct drm_audio_component_audio_ops *ops,
2572 int (*port2pin)(struct hda_codec *, int))
2574 struct hdmi_spec *spec = codec->spec;
2576 spec->port2pin = port2pin;
2577 setup_drm_audio_ops(codec, ops);
2578 if (!snd_hdac_acomp_init(&codec->bus->core, &spec->drm_audio_ops,
2579 match_bound_vga, 0)) {
2580 spec->acomp_registered = true;
2581 codec->bus->keep_power = 0;
2586 * Intel codec parsers and helpers
2589 static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
2592 struct hdmi_spec *spec = codec->spec;
2596 nconns = snd_hda_get_raw_connections(codec, nid, conns,
2598 if (nconns == spec->num_cvts &&
2599 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
2602 /* override pins connection list */
2603 codec_dbg(codec, "hdmi: haswell: override pin connection 0x%x\n", nid);
2604 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
2607 #define INTEL_GET_VENDOR_VERB 0xf81
2608 #define INTEL_SET_VENDOR_VERB 0x781
2609 #define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
2610 #define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
2612 static void intel_haswell_enable_all_pins(struct hda_codec *codec,
2615 unsigned int vendor_param;
2616 struct hdmi_spec *spec = codec->spec;
2618 vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0,
2619 INTEL_GET_VENDOR_VERB, 0);
2620 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
2623 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
2624 vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0,
2625 INTEL_SET_VENDOR_VERB, vendor_param);
2626 if (vendor_param == -1)
2630 snd_hda_codec_update_widgets(codec);
2633 static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
2635 unsigned int vendor_param;
2636 struct hdmi_spec *spec = codec->spec;
2638 vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0,
2639 INTEL_GET_VENDOR_VERB, 0);
2640 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
2643 /* enable DP1.2 mode */
2644 vendor_param |= INTEL_EN_DP12;
2645 snd_hdac_regmap_add_vendor_verb(&codec->core, INTEL_SET_VENDOR_VERB);
2646 snd_hda_codec_write_cache(codec, spec->vendor_nid, 0,
2647 INTEL_SET_VENDOR_VERB, vendor_param);
2650 /* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
2651 * Otherwise you may get severe h/w communication errors.
2653 static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2654 unsigned int power_state)
2656 if (power_state == AC_PWRST_D0) {
2657 intel_haswell_enable_all_pins(codec, false);
2658 intel_haswell_fixup_enable_dp12(codec);
2661 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
2662 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2665 /* There is a fixed mapping between audio pin node and display port.
2666 * on SNB, IVY, HSW, BSW, SKL, BXT, KBL:
2667 * Pin Widget 5 - PORT B (port = 1 in i915 driver)
2668 * Pin Widget 6 - PORT C (port = 2 in i915 driver)
2669 * Pin Widget 7 - PORT D (port = 3 in i915 driver)
2672 * Pin Widget 4 - PORT B (port = 1 in i915 driver)
2673 * Pin Widget 5 - PORT C (port = 2 in i915 driver)
2674 * Pin Widget 6 - PORT D (port = 3 in i915 driver)
2676 static int intel_base_nid(struct hda_codec *codec)
2678 switch (codec->core.vendor_id) {
2679 case 0x80860054: /* ILK */
2680 case 0x80862804: /* ILK */
2681 case 0x80862882: /* VLV */
2688 static int intel_pin2port(void *audio_ptr, int pin_nid)
2690 struct hda_codec *codec = audio_ptr;
2691 struct hdmi_spec *spec = codec->spec;
2694 if (!spec->port_num) {
2695 base_nid = intel_base_nid(codec);
2696 if (WARN_ON(pin_nid < base_nid || pin_nid >= base_nid + 3))
2698 return pin_nid - base_nid + 1;
2702 * looking for the pin number in the mapping table and return
2703 * the index which indicate the port number
2705 for (i = 0; i < spec->port_num; i++) {
2706 if (pin_nid == spec->port_map[i])
2710 codec_info(codec, "Can't find the HDMI/DP port for pin %d\n", pin_nid);
2714 static int intel_port2pin(struct hda_codec *codec, int port)
2716 struct hdmi_spec *spec = codec->spec;
2718 if (!spec->port_num) {
2719 /* we assume only from port-B to port-D */
2720 if (port < 1 || port > 3)
2722 return port + intel_base_nid(codec) - 1;
2725 if (port < 0 || port >= spec->port_num)
2727 return spec->port_map[port];
2730 static void intel_pin_eld_notify(void *audio_ptr, int port, int pipe)
2732 struct hda_codec *codec = audio_ptr;
2736 pin_nid = intel_port2pin(codec, port);
2739 /* skip notification during system suspend (but not in runtime PM);
2740 * the state will be updated at resume
2742 if (snd_power_get_state(codec->card) != SNDRV_CTL_POWER_D0)
2744 /* ditto during suspend/resume process itself */
2745 if (snd_hdac_is_in_pm(&codec->core))
2748 snd_hdac_i915_set_bclk(&codec->bus->core);
2749 check_presence_and_report(codec, pin_nid, dev_id);
2752 static const struct drm_audio_component_audio_ops intel_audio_ops = {
2753 .pin2port = intel_pin2port,
2754 .pin_eld_notify = intel_pin_eld_notify,
2757 /* register i915 component pin_eld_notify callback */
2758 static void register_i915_notifier(struct hda_codec *codec)
2760 struct hdmi_spec *spec = codec->spec;
2762 spec->use_acomp_notifier = true;
2763 spec->port2pin = intel_port2pin;
2764 setup_drm_audio_ops(codec, &intel_audio_ops);
2765 snd_hdac_acomp_register_notifier(&codec->bus->core,
2766 &spec->drm_audio_ops);
2767 /* no need for forcible resume for jack check thanks to notifier */
2768 codec->relaxed_resume = 1;
2771 /* setup_stream ops override for HSW+ */
2772 static int i915_hsw_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
2773 hda_nid_t pin_nid, int dev_id, u32 stream_tag,
2776 haswell_verify_D0(codec, cvt_nid, pin_nid);
2777 return hdmi_setup_stream(codec, cvt_nid, pin_nid, dev_id,
2778 stream_tag, format);
2781 /* pin_cvt_fixup ops override for HSW+ and VLV+ */
2782 static void i915_pin_cvt_fixup(struct hda_codec *codec,
2783 struct hdmi_spec_per_pin *per_pin,
2787 snd_hda_set_dev_select(codec, per_pin->pin_nid,
2789 intel_verify_pin_cvt_connect(codec, per_pin);
2790 intel_not_share_assigned_cvt(codec, per_pin->pin_nid,
2791 per_pin->dev_id, per_pin->mux_idx);
2793 intel_not_share_assigned_cvt_nid(codec, 0, 0, cvt_nid);
2797 /* precondition and allocation for Intel codecs */
2798 static int alloc_intel_hdmi(struct hda_codec *codec)
2802 /* requires i915 binding */
2803 if (!codec->bus->core.audio_component) {
2804 codec_info(codec, "No i915 binding for Intel HDMI/DP codec\n");
2805 /* set probe_id here to prevent generic fallback binding */
2806 codec->probe_id = HDA_CODEC_ID_SKIP_PROBE;
2810 err = alloc_generic_hdmi(codec);
2813 /* no need to handle unsol events */
2814 codec->patch_ops.unsol_event = NULL;
2818 /* parse and post-process for Intel codecs */
2819 static int parse_intel_hdmi(struct hda_codec *codec)
2823 err = hdmi_parse_codec(codec);
2825 generic_spec_free(codec);
2829 generic_hdmi_init_per_pins(codec);
2830 register_i915_notifier(codec);
2834 /* Intel Haswell and onwards; audio component with eld notifier */
2835 static int intel_hsw_common_init(struct hda_codec *codec, hda_nid_t vendor_nid,
2836 const int *port_map, int port_num)
2838 struct hdmi_spec *spec;
2841 err = alloc_intel_hdmi(codec);
2845 codec->dp_mst = true;
2846 spec->dyn_pcm_assign = true;
2847 spec->vendor_nid = vendor_nid;
2848 spec->port_map = port_map;
2849 spec->port_num = port_num;
2850 spec->intel_hsw_fixup = true;
2852 intel_haswell_enable_all_pins(codec, true);
2853 intel_haswell_fixup_enable_dp12(codec);
2855 codec->display_power_control = 1;
2857 codec->patch_ops.set_power_state = haswell_set_power_state;
2858 codec->depop_delay = 0;
2859 codec->auto_runtime_pm = 1;
2861 spec->ops.setup_stream = i915_hsw_setup_stream;
2862 spec->ops.pin_cvt_fixup = i915_pin_cvt_fixup;
2864 return parse_intel_hdmi(codec);
2867 static int patch_i915_hsw_hdmi(struct hda_codec *codec)
2869 return intel_hsw_common_init(codec, 0x08, NULL, 0);
2872 static int patch_i915_glk_hdmi(struct hda_codec *codec)
2874 return intel_hsw_common_init(codec, 0x0b, NULL, 0);
2877 static int patch_i915_icl_hdmi(struct hda_codec *codec)
2880 * pin to port mapping table where the value indicate the pin number and
2881 * the index indicate the port number.
2883 static const int map[] = {0x0, 0x4, 0x6, 0x8, 0xa, 0xb};
2885 return intel_hsw_common_init(codec, 0x02, map, ARRAY_SIZE(map));
2888 static int patch_i915_tgl_hdmi(struct hda_codec *codec)
2891 * pin to port mapping table where the value indicate the pin number and
2892 * the index indicate the port number.
2894 static const int map[] = {0x4, 0x6, 0x8, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
2896 return intel_hsw_common_init(codec, 0x02, map, ARRAY_SIZE(map));
2899 /* Intel Baytrail and Braswell; with eld notifier */
2900 static int patch_i915_byt_hdmi(struct hda_codec *codec)
2902 struct hdmi_spec *spec;
2905 err = alloc_intel_hdmi(codec);
2910 /* For Valleyview/Cherryview, only the display codec is in the display
2911 * power well and can use link_power ops to request/release the power.
2913 codec->display_power_control = 1;
2915 codec->depop_delay = 0;
2916 codec->auto_runtime_pm = 1;
2918 spec->ops.pin_cvt_fixup = i915_pin_cvt_fixup;
2920 return parse_intel_hdmi(codec);
2923 /* Intel IronLake, SandyBridge and IvyBridge; with eld notifier */
2924 static int patch_i915_cpt_hdmi(struct hda_codec *codec)
2928 err = alloc_intel_hdmi(codec);
2931 return parse_intel_hdmi(codec);
2935 * Shared non-generic implementations
2938 static int simple_playback_build_pcms(struct hda_codec *codec)
2940 struct hdmi_spec *spec = codec->spec;
2941 struct hda_pcm *info;
2943 struct hda_pcm_stream *pstr;
2944 struct hdmi_spec_per_cvt *per_cvt;
2946 per_cvt = get_cvt(spec, 0);
2947 chans = get_wcaps(codec, per_cvt->cvt_nid);
2948 chans = get_wcaps_channels(chans);
2950 info = snd_hda_codec_pcm_new(codec, "HDMI 0");
2953 spec->pcm_rec[0].pcm = info;
2954 info->pcm_type = HDA_PCM_TYPE_HDMI;
2955 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2956 *pstr = spec->pcm_playback;
2957 pstr->nid = per_cvt->cvt_nid;
2958 if (pstr->channels_max <= 2 && chans && chans <= 16)
2959 pstr->channels_max = chans;
2964 /* unsolicited event for jack sensing */
2965 static void simple_hdmi_unsol_event(struct hda_codec *codec,
2968 snd_hda_jack_set_dirty_all(codec);
2969 snd_hda_jack_report_sync(codec);
2972 /* generic_hdmi_build_jack can be used for simple_hdmi, too,
2973 * as long as spec->pins[] is set correctly
2975 #define simple_hdmi_build_jack generic_hdmi_build_jack
2977 static int simple_playback_build_controls(struct hda_codec *codec)
2979 struct hdmi_spec *spec = codec->spec;
2980 struct hdmi_spec_per_cvt *per_cvt;
2983 per_cvt = get_cvt(spec, 0);
2984 err = snd_hda_create_dig_out_ctls(codec, per_cvt->cvt_nid,
2989 return simple_hdmi_build_jack(codec, 0);
2992 static int simple_playback_init(struct hda_codec *codec)
2994 struct hdmi_spec *spec = codec->spec;
2995 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
2996 hda_nid_t pin = per_pin->pin_nid;
2998 snd_hda_codec_write(codec, pin, 0,
2999 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
3000 /* some codecs require to unmute the pin */
3001 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
3002 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3004 snd_hda_jack_detect_enable(codec, pin, per_pin->dev_id);
3008 static void simple_playback_free(struct hda_codec *codec)
3010 struct hdmi_spec *spec = codec->spec;
3012 hdmi_array_free(spec);
3017 * Nvidia specific implementations
3020 #define Nv_VERB_SET_Channel_Allocation 0xF79
3021 #define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
3022 #define Nv_VERB_SET_Audio_Protection_On 0xF98
3023 #define Nv_VERB_SET_Audio_Protection_Off 0xF99
3025 #define nvhdmi_master_con_nid_7x 0x04
3026 #define nvhdmi_master_pin_nid_7x 0x05
3028 static const hda_nid_t nvhdmi_con_nids_7x[4] = {
3029 /*front, rear, clfe, rear_surr */
3033 static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
3034 /* set audio protect on */
3035 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
3036 /* enable digital output on pin widget */
3037 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3041 static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
3042 /* set audio protect on */
3043 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
3044 /* enable digital output on pin widget */
3045 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3046 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3047 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3048 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3049 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3053 #ifdef LIMITED_RATE_FMT_SUPPORT
3054 /* support only the safe format and rate */
3055 #define SUPPORTED_RATES SNDRV_PCM_RATE_48000
3056 #define SUPPORTED_MAXBPS 16
3057 #define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
3059 /* support all rates and formats */
3060 #define SUPPORTED_RATES \
3061 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
3062 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
3063 SNDRV_PCM_RATE_192000)
3064 #define SUPPORTED_MAXBPS 24
3065 #define SUPPORTED_FORMATS \
3066 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
3069 static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
3071 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
3075 static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
3077 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
3081 static const unsigned int channels_2_6_8[] = {
3085 static const unsigned int channels_2_8[] = {
3089 static const struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
3090 .count = ARRAY_SIZE(channels_2_6_8),
3091 .list = channels_2_6_8,
3095 static const struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
3096 .count = ARRAY_SIZE(channels_2_8),
3097 .list = channels_2_8,
3101 static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
3102 struct hda_codec *codec,
3103 struct snd_pcm_substream *substream)
3105 struct hdmi_spec *spec = codec->spec;
3106 const struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
3108 switch (codec->preset->vendor_id) {
3113 hw_constraints_channels = &hw_constraints_2_8_channels;
3116 hw_constraints_channels = &hw_constraints_2_6_8_channels;
3122 if (hw_constraints_channels != NULL) {
3123 snd_pcm_hw_constraint_list(substream->runtime, 0,
3124 SNDRV_PCM_HW_PARAM_CHANNELS,
3125 hw_constraints_channels);
3127 snd_pcm_hw_constraint_step(substream->runtime, 0,
3128 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3131 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3134 static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
3135 struct hda_codec *codec,
3136 struct snd_pcm_substream *substream)
3138 struct hdmi_spec *spec = codec->spec;
3139 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3142 static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3143 struct hda_codec *codec,
3144 unsigned int stream_tag,
3145 unsigned int format,
3146 struct snd_pcm_substream *substream)
3148 struct hdmi_spec *spec = codec->spec;
3149 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3150 stream_tag, format, substream);
3153 static const struct hda_pcm_stream simple_pcm_playback = {
3158 .open = simple_playback_pcm_open,
3159 .close = simple_playback_pcm_close,
3160 .prepare = simple_playback_pcm_prepare
3164 static const struct hda_codec_ops simple_hdmi_patch_ops = {
3165 .build_controls = simple_playback_build_controls,
3166 .build_pcms = simple_playback_build_pcms,
3167 .init = simple_playback_init,
3168 .free = simple_playback_free,
3169 .unsol_event = simple_hdmi_unsol_event,
3172 static int patch_simple_hdmi(struct hda_codec *codec,
3173 hda_nid_t cvt_nid, hda_nid_t pin_nid)
3175 struct hdmi_spec *spec;
3176 struct hdmi_spec_per_cvt *per_cvt;
3177 struct hdmi_spec_per_pin *per_pin;
3179 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3183 spec->codec = codec;
3185 hdmi_array_init(spec, 1);
3187 spec->multiout.num_dacs = 0; /* no analog */
3188 spec->multiout.max_channels = 2;
3189 spec->multiout.dig_out_nid = cvt_nid;
3192 per_pin = snd_array_new(&spec->pins);
3193 per_cvt = snd_array_new(&spec->cvts);
3194 if (!per_pin || !per_cvt) {
3195 simple_playback_free(codec);
3198 per_cvt->cvt_nid = cvt_nid;
3199 per_pin->pin_nid = pin_nid;
3200 spec->pcm_playback = simple_pcm_playback;
3202 codec->patch_ops = simple_hdmi_patch_ops;
3207 static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
3210 unsigned int chanmask;
3211 int chan = channels ? (channels - 1) : 1;
3230 /* Set the audio infoframe channel allocation and checksum fields. The
3231 * channel count is computed implicitly by the hardware. */
3232 snd_hda_codec_write(codec, 0x1, 0,
3233 Nv_VERB_SET_Channel_Allocation, chanmask);
3235 snd_hda_codec_write(codec, 0x1, 0,
3236 Nv_VERB_SET_Info_Frame_Checksum,
3237 (0x71 - chan - chanmask));
3240 static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
3241 struct hda_codec *codec,
3242 struct snd_pcm_substream *substream)
3244 struct hdmi_spec *spec = codec->spec;
3247 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
3248 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
3249 for (i = 0; i < 4; i++) {
3250 /* set the stream id */
3251 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3252 AC_VERB_SET_CHANNEL_STREAMID, 0);
3253 /* set the stream format */
3254 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3255 AC_VERB_SET_STREAM_FORMAT, 0);
3258 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
3259 * streams are disabled. */
3260 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3262 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3265 static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
3266 struct hda_codec *codec,
3267 unsigned int stream_tag,
3268 unsigned int format,
3269 struct snd_pcm_substream *substream)
3272 unsigned int dataDCC2, channel_id;
3274 struct hdmi_spec *spec = codec->spec;
3275 struct hda_spdif_out *spdif;
3276 struct hdmi_spec_per_cvt *per_cvt;
3278 mutex_lock(&codec->spdif_mutex);
3279 per_cvt = get_cvt(spec, 0);
3280 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
3282 chs = substream->runtime->channels;
3286 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
3287 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
3288 snd_hda_codec_write(codec,
3289 nvhdmi_master_con_nid_7x,
3291 AC_VERB_SET_DIGI_CONVERT_1,
3292 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
3294 /* set the stream id */
3295 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3296 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
3298 /* set the stream format */
3299 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3300 AC_VERB_SET_STREAM_FORMAT, format);
3302 /* turn on again (if needed) */
3303 /* enable and set the channel status audio/data flag */
3304 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
3305 snd_hda_codec_write(codec,
3306 nvhdmi_master_con_nid_7x,
3308 AC_VERB_SET_DIGI_CONVERT_1,
3309 spdif->ctls & 0xff);
3310 snd_hda_codec_write(codec,
3311 nvhdmi_master_con_nid_7x,
3313 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3316 for (i = 0; i < 4; i++) {
3322 /* turn off SPDIF once;
3323 *otherwise the IEC958 bits won't be updated
3325 if (codec->spdif_status_reset &&
3326 (spdif->ctls & AC_DIG1_ENABLE))
3327 snd_hda_codec_write(codec,
3328 nvhdmi_con_nids_7x[i],
3330 AC_VERB_SET_DIGI_CONVERT_1,
3331 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
3332 /* set the stream id */
3333 snd_hda_codec_write(codec,
3334 nvhdmi_con_nids_7x[i],
3336 AC_VERB_SET_CHANNEL_STREAMID,
3337 (stream_tag << 4) | channel_id);
3338 /* set the stream format */
3339 snd_hda_codec_write(codec,
3340 nvhdmi_con_nids_7x[i],
3342 AC_VERB_SET_STREAM_FORMAT,
3344 /* turn on again (if needed) */
3345 /* enable and set the channel status audio/data flag */
3346 if (codec->spdif_status_reset &&
3347 (spdif->ctls & AC_DIG1_ENABLE)) {
3348 snd_hda_codec_write(codec,
3349 nvhdmi_con_nids_7x[i],
3351 AC_VERB_SET_DIGI_CONVERT_1,
3352 spdif->ctls & 0xff);
3353 snd_hda_codec_write(codec,
3354 nvhdmi_con_nids_7x[i],
3356 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3360 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
3362 mutex_unlock(&codec->spdif_mutex);
3366 static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
3370 .nid = nvhdmi_master_con_nid_7x,
3371 .rates = SUPPORTED_RATES,
3372 .maxbps = SUPPORTED_MAXBPS,
3373 .formats = SUPPORTED_FORMATS,
3375 .open = simple_playback_pcm_open,
3376 .close = nvhdmi_8ch_7x_pcm_close,
3377 .prepare = nvhdmi_8ch_7x_pcm_prepare
3381 static int patch_nvhdmi_2ch(struct hda_codec *codec)
3383 struct hdmi_spec *spec;
3384 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
3385 nvhdmi_master_pin_nid_7x);
3389 codec->patch_ops.init = nvhdmi_7x_init_2ch;
3390 /* override the PCM rates, etc, as the codec doesn't give full list */
3392 spec->pcm_playback.rates = SUPPORTED_RATES;
3393 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
3394 spec->pcm_playback.formats = SUPPORTED_FORMATS;
3398 static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
3400 struct hdmi_spec *spec = codec->spec;
3401 int err = simple_playback_build_pcms(codec);
3403 struct hda_pcm *info = get_pcm_rec(spec, 0);
3404 info->own_chmap = true;
3409 static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
3411 struct hdmi_spec *spec = codec->spec;
3412 struct hda_pcm *info;
3413 struct snd_pcm_chmap *chmap;
3416 err = simple_playback_build_controls(codec);
3420 /* add channel maps */
3421 info = get_pcm_rec(spec, 0);
3422 err = snd_pcm_add_chmap_ctls(info->pcm,
3423 SNDRV_PCM_STREAM_PLAYBACK,
3424 snd_pcm_alt_chmaps, 8, 0, &chmap);
3427 switch (codec->preset->vendor_id) {
3432 chmap->channel_mask = (1U << 2) | (1U << 8);
3435 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
3440 static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
3442 struct hdmi_spec *spec;
3443 int err = patch_nvhdmi_2ch(codec);
3447 spec->multiout.max_channels = 8;
3448 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
3449 codec->patch_ops.init = nvhdmi_7x_init_8ch;
3450 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
3451 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
3453 /* Initialize the audio infoframe channel mask and checksum to something
3455 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3461 * NVIDIA codecs ignore ASP mapping for 2ch - confirmed on:
3465 static int nvhdmi_chmap_cea_alloc_validate_get_type(struct hdac_chmap *chmap,
3466 struct hdac_cea_channel_speaker_allocation *cap, int channels)
3468 if (cap->ca_index == 0x00 && channels == 2)
3469 return SNDRV_CTL_TLVT_CHMAP_FIXED;
3471 /* If the speaker allocation matches the channel count, it is OK. */
3472 if (cap->channels != channels)
3475 /* all channels are remappable freely */
3476 return SNDRV_CTL_TLVT_CHMAP_VAR;
3479 static int nvhdmi_chmap_validate(struct hdac_chmap *chmap,
3480 int ca, int chs, unsigned char *map)
3482 if (ca == 0x00 && (map[0] != SNDRV_CHMAP_FL || map[1] != SNDRV_CHMAP_FR))
3488 /* map from pin NID to port; port is 0-based */
3489 /* for Nvidia: assume widget NID starting from 4, with step 1 (4, 5, 6, ...) */
3490 static int nvhdmi_pin2port(void *audio_ptr, int pin_nid)
3495 /* reverse-map from port to pin NID: see above */
3496 static int nvhdmi_port2pin(struct hda_codec *codec, int port)
3501 static const struct drm_audio_component_audio_ops nvhdmi_audio_ops = {
3502 .pin2port = nvhdmi_pin2port,
3503 .pin_eld_notify = generic_acomp_pin_eld_notify,
3504 .master_bind = generic_acomp_master_bind,
3505 .master_unbind = generic_acomp_master_unbind,
3508 static int patch_nvhdmi(struct hda_codec *codec)
3510 struct hdmi_spec *spec;
3513 err = patch_generic_hdmi(codec);
3518 spec->dyn_pin_out = true;
3520 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
3521 nvhdmi_chmap_cea_alloc_validate_get_type;
3522 spec->chmap.ops.chmap_validate = nvhdmi_chmap_validate;
3524 codec->link_down_at_suspend = 1;
3526 generic_acomp_init(codec, &nvhdmi_audio_ops, nvhdmi_port2pin);
3532 * The HDA codec on NVIDIA Tegra contains two scratch registers that are
3533 * accessed using vendor-defined verbs. These registers can be used for
3534 * interoperability between the HDA and HDMI drivers.
3537 /* Audio Function Group node */
3538 #define NVIDIA_AFG_NID 0x01
3541 * The SCRATCH0 register is used to notify the HDMI codec of changes in audio
3542 * format. On Tegra, bit 31 is used as a trigger that causes an interrupt to
3543 * be raised in the HDMI codec. The remainder of the bits is arbitrary. This
3544 * implementation stores the HDA format (see AC_FMT_*) in bits [15:0] and an
3545 * additional bit (at position 30) to signal the validity of the format.
3547 * | 31 | 30 | 29 16 | 15 0 |
3548 * +---------+-------+--------+--------+
3549 * | TRIGGER | VALID | UNUSED | FORMAT |
3550 * +-----------------------------------|
3552 * Note that for the trigger bit to take effect it needs to change value
3553 * (i.e. it needs to be toggled).
3555 #define NVIDIA_GET_SCRATCH0 0xfa6
3556 #define NVIDIA_SET_SCRATCH0_BYTE0 0xfa7
3557 #define NVIDIA_SET_SCRATCH0_BYTE1 0xfa8
3558 #define NVIDIA_SET_SCRATCH0_BYTE2 0xfa9
3559 #define NVIDIA_SET_SCRATCH0_BYTE3 0xfaa
3560 #define NVIDIA_SCRATCH_TRIGGER (1 << 7)
3561 #define NVIDIA_SCRATCH_VALID (1 << 6)
3563 #define NVIDIA_GET_SCRATCH1 0xfab
3564 #define NVIDIA_SET_SCRATCH1_BYTE0 0xfac
3565 #define NVIDIA_SET_SCRATCH1_BYTE1 0xfad
3566 #define NVIDIA_SET_SCRATCH1_BYTE2 0xfae
3567 #define NVIDIA_SET_SCRATCH1_BYTE3 0xfaf
3570 * The format parameter is the HDA audio format (see AC_FMT_*). If set to 0,
3571 * the format is invalidated so that the HDMI codec can be disabled.
3573 static void tegra_hdmi_set_format(struct hda_codec *codec, unsigned int format)
3577 /* bits [31:30] contain the trigger and valid bits */
3578 value = snd_hda_codec_read(codec, NVIDIA_AFG_NID, 0,
3579 NVIDIA_GET_SCRATCH0, 0);
3580 value = (value >> 24) & 0xff;
3582 /* bits [15:0] are used to store the HDA format */
3583 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3584 NVIDIA_SET_SCRATCH0_BYTE0,
3585 (format >> 0) & 0xff);
3586 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3587 NVIDIA_SET_SCRATCH0_BYTE1,
3588 (format >> 8) & 0xff);
3590 /* bits [16:24] are unused */
3591 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3592 NVIDIA_SET_SCRATCH0_BYTE2, 0);
3595 * Bit 30 signals that the data is valid and hence that HDMI audio can
3599 value &= ~NVIDIA_SCRATCH_VALID;
3601 value |= NVIDIA_SCRATCH_VALID;
3604 * Whenever the trigger bit is toggled, an interrupt is raised in the
3605 * HDMI codec. The HDMI driver will use that as trigger to update its
3608 value ^= NVIDIA_SCRATCH_TRIGGER;
3610 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3611 NVIDIA_SET_SCRATCH0_BYTE3, value);
3614 static int tegra_hdmi_pcm_prepare(struct hda_pcm_stream *hinfo,
3615 struct hda_codec *codec,
3616 unsigned int stream_tag,
3617 unsigned int format,
3618 struct snd_pcm_substream *substream)
3622 err = generic_hdmi_playback_pcm_prepare(hinfo, codec, stream_tag,
3627 /* notify the HDMI codec of the format change */
3628 tegra_hdmi_set_format(codec, format);
3633 static int tegra_hdmi_pcm_cleanup(struct hda_pcm_stream *hinfo,
3634 struct hda_codec *codec,
3635 struct snd_pcm_substream *substream)
3637 /* invalidate the format in the HDMI codec */
3638 tegra_hdmi_set_format(codec, 0);
3640 return generic_hdmi_playback_pcm_cleanup(hinfo, codec, substream);
3643 static struct hda_pcm *hda_find_pcm_by_type(struct hda_codec *codec, int type)
3645 struct hdmi_spec *spec = codec->spec;
3648 for (i = 0; i < spec->num_pins; i++) {
3649 struct hda_pcm *pcm = get_pcm_rec(spec, i);
3651 if (pcm->pcm_type == type)
3658 static int tegra_hdmi_build_pcms(struct hda_codec *codec)
3660 struct hda_pcm_stream *stream;
3661 struct hda_pcm *pcm;
3664 err = generic_hdmi_build_pcms(codec);
3668 pcm = hda_find_pcm_by_type(codec, HDA_PCM_TYPE_HDMI);
3673 * Override ->prepare() and ->cleanup() operations to notify the HDMI
3674 * codec about format changes.
3676 stream = &pcm->stream[SNDRV_PCM_STREAM_PLAYBACK];
3677 stream->ops.prepare = tegra_hdmi_pcm_prepare;
3678 stream->ops.cleanup = tegra_hdmi_pcm_cleanup;
3683 static int patch_tegra_hdmi(struct hda_codec *codec)
3687 err = patch_generic_hdmi(codec);
3691 codec->patch_ops.build_pcms = tegra_hdmi_build_pcms;
3697 * ATI/AMD-specific implementations
3700 #define is_amdhdmi_rev3_or_later(codec) \
3701 ((codec)->core.vendor_id == 0x1002aa01 && \
3702 ((codec)->core.revision_id & 0xff00) >= 0x0300)
3703 #define has_amd_full_remap_support(codec) is_amdhdmi_rev3_or_later(codec)
3705 /* ATI/AMD specific HDA pin verbs, see the AMD HDA Verbs specification */
3706 #define ATI_VERB_SET_CHANNEL_ALLOCATION 0x771
3707 #define ATI_VERB_SET_DOWNMIX_INFO 0x772
3708 #define ATI_VERB_SET_MULTICHANNEL_01 0x777
3709 #define ATI_VERB_SET_MULTICHANNEL_23 0x778
3710 #define ATI_VERB_SET_MULTICHANNEL_45 0x779
3711 #define ATI_VERB_SET_MULTICHANNEL_67 0x77a
3712 #define ATI_VERB_SET_HBR_CONTROL 0x77c
3713 #define ATI_VERB_SET_MULTICHANNEL_1 0x785
3714 #define ATI_VERB_SET_MULTICHANNEL_3 0x786
3715 #define ATI_VERB_SET_MULTICHANNEL_5 0x787
3716 #define ATI_VERB_SET_MULTICHANNEL_7 0x788
3717 #define ATI_VERB_SET_MULTICHANNEL_MODE 0x789
3718 #define ATI_VERB_GET_CHANNEL_ALLOCATION 0xf71
3719 #define ATI_VERB_GET_DOWNMIX_INFO 0xf72
3720 #define ATI_VERB_GET_MULTICHANNEL_01 0xf77
3721 #define ATI_VERB_GET_MULTICHANNEL_23 0xf78
3722 #define ATI_VERB_GET_MULTICHANNEL_45 0xf79
3723 #define ATI_VERB_GET_MULTICHANNEL_67 0xf7a
3724 #define ATI_VERB_GET_HBR_CONTROL 0xf7c
3725 #define ATI_VERB_GET_MULTICHANNEL_1 0xf85
3726 #define ATI_VERB_GET_MULTICHANNEL_3 0xf86
3727 #define ATI_VERB_GET_MULTICHANNEL_5 0xf87
3728 #define ATI_VERB_GET_MULTICHANNEL_7 0xf88
3729 #define ATI_VERB_GET_MULTICHANNEL_MODE 0xf89
3731 /* AMD specific HDA cvt verbs */
3732 #define ATI_VERB_SET_RAMP_RATE 0x770
3733 #define ATI_VERB_GET_RAMP_RATE 0xf70
3735 #define ATI_OUT_ENABLE 0x1
3737 #define ATI_MULTICHANNEL_MODE_PAIRED 0
3738 #define ATI_MULTICHANNEL_MODE_SINGLE 1
3740 #define ATI_HBR_CAPABLE 0x01
3741 #define ATI_HBR_ENABLE 0x10
3743 static int atihdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
3744 int dev_id, unsigned char *buf, int *eld_size)
3746 WARN_ON(dev_id != 0);
3747 /* call hda_eld.c ATI/AMD-specific function */
3748 return snd_hdmi_get_eld_ati(codec, nid, buf, eld_size,
3749 is_amdhdmi_rev3_or_later(codec));
3752 static void atihdmi_pin_setup_infoframe(struct hda_codec *codec,
3753 hda_nid_t pin_nid, int dev_id, int ca,
3754 int active_channels, int conn_type)
3756 WARN_ON(dev_id != 0);
3757 snd_hda_codec_write(codec, pin_nid, 0, ATI_VERB_SET_CHANNEL_ALLOCATION, ca);
3760 static int atihdmi_paired_swap_fc_lfe(int pos)
3763 * ATI/AMD have automatic FC/LFE swap built-in
3764 * when in pairwise mapping mode.
3768 /* see channel_allocations[].speakers[] */
3777 static int atihdmi_paired_chmap_validate(struct hdac_chmap *chmap,
3778 int ca, int chs, unsigned char *map)
3780 struct hdac_cea_channel_speaker_allocation *cap;
3783 /* check that only channel pairs need to be remapped on old pre-rev3 ATI/AMD */
3785 cap = snd_hdac_get_ch_alloc_from_ca(ca);
3786 for (i = 0; i < chs; ++i) {
3787 int mask = snd_hdac_chmap_to_spk_mask(map[i]);
3789 bool companion_ok = false;
3794 for (j = 0 + i % 2; j < 8; j += 2) {
3795 int chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j);
3796 if (cap->speakers[chan_idx] == mask) {
3797 /* channel is in a supported position */
3800 if (i % 2 == 0 && i + 1 < chs) {
3801 /* even channel, check the odd companion */
3802 int comp_chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j + 1);
3803 int comp_mask_req = snd_hdac_chmap_to_spk_mask(map[i+1]);
3804 int comp_mask_act = cap->speakers[comp_chan_idx];
3806 if (comp_mask_req == comp_mask_act)
3807 companion_ok = true;
3819 i++; /* companion channel already checked */
3825 static int atihdmi_pin_set_slot_channel(struct hdac_device *hdac,
3826 hda_nid_t pin_nid, int hdmi_slot, int stream_channel)
3828 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
3830 int ati_channel_setup = 0;
3835 if (!has_amd_full_remap_support(codec)) {
3836 hdmi_slot = atihdmi_paired_swap_fc_lfe(hdmi_slot);
3838 /* In case this is an odd slot but without stream channel, do not
3839 * disable the slot since the corresponding even slot could have a
3840 * channel. In case neither have a channel, the slot pair will be
3841 * disabled when this function is called for the even slot. */
3842 if (hdmi_slot % 2 != 0 && stream_channel == 0xf)
3845 hdmi_slot -= hdmi_slot % 2;
3847 if (stream_channel != 0xf)
3848 stream_channel -= stream_channel % 2;
3851 verb = ATI_VERB_SET_MULTICHANNEL_01 + hdmi_slot/2 + (hdmi_slot % 2) * 0x00e;
3853 /* ati_channel_setup format: [7..4] = stream_channel_id, [1] = mute, [0] = enable */
3855 if (stream_channel != 0xf)
3856 ati_channel_setup = (stream_channel << 4) | ATI_OUT_ENABLE;
3858 return snd_hda_codec_write(codec, pin_nid, 0, verb, ati_channel_setup);
3861 static int atihdmi_pin_get_slot_channel(struct hdac_device *hdac,
3862 hda_nid_t pin_nid, int asp_slot)
3864 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
3865 bool was_odd = false;
3866 int ati_asp_slot = asp_slot;
3868 int ati_channel_setup;
3873 if (!has_amd_full_remap_support(codec)) {
3874 ati_asp_slot = atihdmi_paired_swap_fc_lfe(asp_slot);
3875 if (ati_asp_slot % 2 != 0) {
3881 verb = ATI_VERB_GET_MULTICHANNEL_01 + ati_asp_slot/2 + (ati_asp_slot % 2) * 0x00e;
3883 ati_channel_setup = snd_hda_codec_read(codec, pin_nid, 0, verb, 0);
3885 if (!(ati_channel_setup & ATI_OUT_ENABLE))
3888 return ((ati_channel_setup & 0xf0) >> 4) + !!was_odd;
3891 static int atihdmi_paired_chmap_cea_alloc_validate_get_type(
3892 struct hdac_chmap *chmap,
3893 struct hdac_cea_channel_speaker_allocation *cap,
3899 * Pre-rev3 ATI/AMD codecs operate in a paired channel mode, so
3900 * we need to take that into account (a single channel may take 2
3901 * channel slots if we need to carry a silent channel next to it).
3902 * On Rev3+ AMD codecs this function is not used.
3906 /* We only produce even-numbered channel count TLVs */
3907 if ((channels % 2) != 0)
3910 for (c = 0; c < 7; c += 2) {
3911 if (cap->speakers[c] || cap->speakers[c+1])
3915 if (chanpairs * 2 != channels)
3918 return SNDRV_CTL_TLVT_CHMAP_PAIRED;
3921 static void atihdmi_paired_cea_alloc_to_tlv_chmap(struct hdac_chmap *hchmap,
3922 struct hdac_cea_channel_speaker_allocation *cap,
3923 unsigned int *chmap, int channels)
3925 /* produce paired maps for pre-rev3 ATI/AMD codecs */
3929 for (c = 7; c >= 0; c--) {
3930 int chan = 7 - atihdmi_paired_swap_fc_lfe(7 - c);
3931 int spk = cap->speakers[chan];
3933 /* add N/A channel if the companion channel is occupied */
3934 if (cap->speakers[chan + (chan % 2 ? -1 : 1)])
3935 chmap[count++] = SNDRV_CHMAP_NA;
3940 chmap[count++] = snd_hdac_spk_to_chmap(spk);
3943 WARN_ON(count != channels);
3946 static int atihdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
3947 int dev_id, bool hbr)
3949 int hbr_ctl, hbr_ctl_new;
3951 WARN_ON(dev_id != 0);
3953 hbr_ctl = snd_hda_codec_read(codec, pin_nid, 0, ATI_VERB_GET_HBR_CONTROL, 0);
3954 if (hbr_ctl >= 0 && (hbr_ctl & ATI_HBR_CAPABLE)) {
3956 hbr_ctl_new = hbr_ctl | ATI_HBR_ENABLE;
3958 hbr_ctl_new = hbr_ctl & ~ATI_HBR_ENABLE;
3961 "atihdmi_pin_hbr_setup: NID=0x%x, %shbr-ctl=0x%x\n",
3963 hbr_ctl == hbr_ctl_new ? "" : "new-",
3966 if (hbr_ctl != hbr_ctl_new)
3967 snd_hda_codec_write(codec, pin_nid, 0,
3968 ATI_VERB_SET_HBR_CONTROL,
3977 static int atihdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
3978 hda_nid_t pin_nid, int dev_id,
3979 u32 stream_tag, int format)
3981 if (is_amdhdmi_rev3_or_later(codec)) {
3982 int ramp_rate = 180; /* default as per AMD spec */
3983 /* disable ramp-up/down for non-pcm as per AMD spec */
3984 if (format & AC_FMT_TYPE_NON_PCM)
3987 snd_hda_codec_write(codec, cvt_nid, 0, ATI_VERB_SET_RAMP_RATE, ramp_rate);
3990 return hdmi_setup_stream(codec, cvt_nid, pin_nid, dev_id,
3991 stream_tag, format);
3995 static int atihdmi_init(struct hda_codec *codec)
3997 struct hdmi_spec *spec = codec->spec;
4000 err = generic_hdmi_init(codec);
4005 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
4006 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
4008 /* make sure downmix information in infoframe is zero */
4009 snd_hda_codec_write(codec, per_pin->pin_nid, 0, ATI_VERB_SET_DOWNMIX_INFO, 0);
4011 /* enable channel-wise remap mode if supported */
4012 if (has_amd_full_remap_support(codec))
4013 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
4014 ATI_VERB_SET_MULTICHANNEL_MODE,
4015 ATI_MULTICHANNEL_MODE_SINGLE);
4021 /* map from pin NID to port; port is 0-based */
4022 /* for AMD: assume widget NID starting from 3, with step 2 (3, 5, 7, ...) */
4023 static int atihdmi_pin2port(void *audio_ptr, int pin_nid)
4025 return pin_nid / 2 - 1;
4028 /* reverse-map from port to pin NID: see above */
4029 static int atihdmi_port2pin(struct hda_codec *codec, int port)
4031 return port * 2 + 3;
4034 static const struct drm_audio_component_audio_ops atihdmi_audio_ops = {
4035 .pin2port = atihdmi_pin2port,
4036 .pin_eld_notify = generic_acomp_pin_eld_notify,
4037 .master_bind = generic_acomp_master_bind,
4038 .master_unbind = generic_acomp_master_unbind,
4041 static int patch_atihdmi(struct hda_codec *codec)
4043 struct hdmi_spec *spec;
4044 struct hdmi_spec_per_cvt *per_cvt;
4047 err = patch_generic_hdmi(codec);
4052 codec->patch_ops.init = atihdmi_init;
4056 spec->ops.pin_get_eld = atihdmi_pin_get_eld;
4057 spec->ops.pin_setup_infoframe = atihdmi_pin_setup_infoframe;
4058 spec->ops.pin_hbr_setup = atihdmi_pin_hbr_setup;
4059 spec->ops.setup_stream = atihdmi_setup_stream;
4061 spec->chmap.ops.pin_get_slot_channel = atihdmi_pin_get_slot_channel;
4062 spec->chmap.ops.pin_set_slot_channel = atihdmi_pin_set_slot_channel;
4064 if (!has_amd_full_remap_support(codec)) {
4065 /* override to ATI/AMD-specific versions with pairwise mapping */
4066 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
4067 atihdmi_paired_chmap_cea_alloc_validate_get_type;
4068 spec->chmap.ops.cea_alloc_to_tlv_chmap =
4069 atihdmi_paired_cea_alloc_to_tlv_chmap;
4070 spec->chmap.ops.chmap_validate = atihdmi_paired_chmap_validate;
4073 /* ATI/AMD converters do not advertise all of their capabilities */
4074 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
4075 per_cvt = get_cvt(spec, cvt_idx);
4076 per_cvt->channels_max = max(per_cvt->channels_max, 8u);
4077 per_cvt->rates |= SUPPORTED_RATES;
4078 per_cvt->formats |= SUPPORTED_FORMATS;
4079 per_cvt->maxbps = max(per_cvt->maxbps, 24u);
4082 spec->chmap.channels_max = max(spec->chmap.channels_max, 8u);
4084 /* AMD GPUs have neither EPSS nor CLKSTOP bits, hence preventing
4085 * the link-down as is. Tell the core to allow it.
4087 codec->link_down_at_suspend = 1;
4089 generic_acomp_init(codec, &atihdmi_audio_ops, atihdmi_port2pin);
4094 /* VIA HDMI Implementation */
4095 #define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
4096 #define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
4098 static int patch_via_hdmi(struct hda_codec *codec)
4100 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
4106 static const struct hda_device_id snd_hda_id_hdmi[] = {
4107 HDA_CODEC_ENTRY(0x1002793c, "RS600 HDMI", patch_atihdmi),
4108 HDA_CODEC_ENTRY(0x10027919, "RS600 HDMI", patch_atihdmi),
4109 HDA_CODEC_ENTRY(0x1002791a, "RS690/780 HDMI", patch_atihdmi),
4110 HDA_CODEC_ENTRY(0x1002aa01, "R6xx HDMI", patch_atihdmi),
4111 HDA_CODEC_ENTRY(0x10951390, "SiI1390 HDMI", patch_generic_hdmi),
4112 HDA_CODEC_ENTRY(0x10951392, "SiI1392 HDMI", patch_generic_hdmi),
4113 HDA_CODEC_ENTRY(0x17e80047, "Chrontel HDMI", patch_generic_hdmi),
4114 HDA_CODEC_ENTRY(0x10de0001, "MCP73 HDMI", patch_nvhdmi_2ch),
4115 HDA_CODEC_ENTRY(0x10de0002, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4116 HDA_CODEC_ENTRY(0x10de0003, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4117 HDA_CODEC_ENTRY(0x10de0004, "GPU 04 HDMI", patch_nvhdmi_8ch_7x),
4118 HDA_CODEC_ENTRY(0x10de0005, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4119 HDA_CODEC_ENTRY(0x10de0006, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4120 HDA_CODEC_ENTRY(0x10de0007, "MCP79/7A HDMI", patch_nvhdmi_8ch_7x),
4121 HDA_CODEC_ENTRY(0x10de0008, "GPU 08 HDMI/DP", patch_nvhdmi),
4122 HDA_CODEC_ENTRY(0x10de0009, "GPU 09 HDMI/DP", patch_nvhdmi),
4123 HDA_CODEC_ENTRY(0x10de000a, "GPU 0a HDMI/DP", patch_nvhdmi),
4124 HDA_CODEC_ENTRY(0x10de000b, "GPU 0b HDMI/DP", patch_nvhdmi),
4125 HDA_CODEC_ENTRY(0x10de000c, "MCP89 HDMI", patch_nvhdmi),
4126 HDA_CODEC_ENTRY(0x10de000d, "GPU 0d HDMI/DP", patch_nvhdmi),
4127 HDA_CODEC_ENTRY(0x10de0010, "GPU 10 HDMI/DP", patch_nvhdmi),
4128 HDA_CODEC_ENTRY(0x10de0011, "GPU 11 HDMI/DP", patch_nvhdmi),
4129 HDA_CODEC_ENTRY(0x10de0012, "GPU 12 HDMI/DP", patch_nvhdmi),
4130 HDA_CODEC_ENTRY(0x10de0013, "GPU 13 HDMI/DP", patch_nvhdmi),
4131 HDA_CODEC_ENTRY(0x10de0014, "GPU 14 HDMI/DP", patch_nvhdmi),
4132 HDA_CODEC_ENTRY(0x10de0015, "GPU 15 HDMI/DP", patch_nvhdmi),
4133 HDA_CODEC_ENTRY(0x10de0016, "GPU 16 HDMI/DP", patch_nvhdmi),
4134 /* 17 is known to be absent */
4135 HDA_CODEC_ENTRY(0x10de0018, "GPU 18 HDMI/DP", patch_nvhdmi),
4136 HDA_CODEC_ENTRY(0x10de0019, "GPU 19 HDMI/DP", patch_nvhdmi),
4137 HDA_CODEC_ENTRY(0x10de001a, "GPU 1a HDMI/DP", patch_nvhdmi),
4138 HDA_CODEC_ENTRY(0x10de001b, "GPU 1b HDMI/DP", patch_nvhdmi),
4139 HDA_CODEC_ENTRY(0x10de001c, "GPU 1c HDMI/DP", patch_nvhdmi),
4140 HDA_CODEC_ENTRY(0x10de0020, "Tegra30 HDMI", patch_tegra_hdmi),
4141 HDA_CODEC_ENTRY(0x10de0022, "Tegra114 HDMI", patch_tegra_hdmi),
4142 HDA_CODEC_ENTRY(0x10de0028, "Tegra124 HDMI", patch_tegra_hdmi),
4143 HDA_CODEC_ENTRY(0x10de0029, "Tegra210 HDMI/DP", patch_tegra_hdmi),
4144 HDA_CODEC_ENTRY(0x10de002d, "Tegra186 HDMI/DP0", patch_tegra_hdmi),
4145 HDA_CODEC_ENTRY(0x10de002e, "Tegra186 HDMI/DP1", patch_tegra_hdmi),
4146 HDA_CODEC_ENTRY(0x10de002f, "Tegra194 HDMI/DP2", patch_tegra_hdmi),
4147 HDA_CODEC_ENTRY(0x10de0030, "Tegra194 HDMI/DP3", patch_tegra_hdmi),
4148 HDA_CODEC_ENTRY(0x10de0040, "GPU 40 HDMI/DP", patch_nvhdmi),
4149 HDA_CODEC_ENTRY(0x10de0041, "GPU 41 HDMI/DP", patch_nvhdmi),
4150 HDA_CODEC_ENTRY(0x10de0042, "GPU 42 HDMI/DP", patch_nvhdmi),
4151 HDA_CODEC_ENTRY(0x10de0043, "GPU 43 HDMI/DP", patch_nvhdmi),
4152 HDA_CODEC_ENTRY(0x10de0044, "GPU 44 HDMI/DP", patch_nvhdmi),
4153 HDA_CODEC_ENTRY(0x10de0045, "GPU 45 HDMI/DP", patch_nvhdmi),
4154 HDA_CODEC_ENTRY(0x10de0050, "GPU 50 HDMI/DP", patch_nvhdmi),
4155 HDA_CODEC_ENTRY(0x10de0051, "GPU 51 HDMI/DP", patch_nvhdmi),
4156 HDA_CODEC_ENTRY(0x10de0052, "GPU 52 HDMI/DP", patch_nvhdmi),
4157 HDA_CODEC_ENTRY(0x10de0060, "GPU 60 HDMI/DP", patch_nvhdmi),
4158 HDA_CODEC_ENTRY(0x10de0061, "GPU 61 HDMI/DP", patch_nvhdmi),
4159 HDA_CODEC_ENTRY(0x10de0062, "GPU 62 HDMI/DP", patch_nvhdmi),
4160 HDA_CODEC_ENTRY(0x10de0067, "MCP67 HDMI", patch_nvhdmi_2ch),
4161 HDA_CODEC_ENTRY(0x10de0070, "GPU 70 HDMI/DP", patch_nvhdmi),
4162 HDA_CODEC_ENTRY(0x10de0071, "GPU 71 HDMI/DP", patch_nvhdmi),
4163 HDA_CODEC_ENTRY(0x10de0072, "GPU 72 HDMI/DP", patch_nvhdmi),
4164 HDA_CODEC_ENTRY(0x10de0073, "GPU 73 HDMI/DP", patch_nvhdmi),
4165 HDA_CODEC_ENTRY(0x10de0074, "GPU 74 HDMI/DP", patch_nvhdmi),
4166 HDA_CODEC_ENTRY(0x10de0076, "GPU 76 HDMI/DP", patch_nvhdmi),
4167 HDA_CODEC_ENTRY(0x10de007b, "GPU 7b HDMI/DP", patch_nvhdmi),
4168 HDA_CODEC_ENTRY(0x10de007c, "GPU 7c HDMI/DP", patch_nvhdmi),
4169 HDA_CODEC_ENTRY(0x10de007d, "GPU 7d HDMI/DP", patch_nvhdmi),
4170 HDA_CODEC_ENTRY(0x10de007e, "GPU 7e HDMI/DP", patch_nvhdmi),
4171 HDA_CODEC_ENTRY(0x10de0080, "GPU 80 HDMI/DP", patch_nvhdmi),
4172 HDA_CODEC_ENTRY(0x10de0081, "GPU 81 HDMI/DP", patch_nvhdmi),
4173 HDA_CODEC_ENTRY(0x10de0082, "GPU 82 HDMI/DP", patch_nvhdmi),
4174 HDA_CODEC_ENTRY(0x10de0083, "GPU 83 HDMI/DP", patch_nvhdmi),
4175 HDA_CODEC_ENTRY(0x10de0084, "GPU 84 HDMI/DP", patch_nvhdmi),
4176 HDA_CODEC_ENTRY(0x10de0090, "GPU 90 HDMI/DP", patch_nvhdmi),
4177 HDA_CODEC_ENTRY(0x10de0091, "GPU 91 HDMI/DP", patch_nvhdmi),
4178 HDA_CODEC_ENTRY(0x10de0092, "GPU 92 HDMI/DP", patch_nvhdmi),
4179 HDA_CODEC_ENTRY(0x10de0093, "GPU 93 HDMI/DP", patch_nvhdmi),
4180 HDA_CODEC_ENTRY(0x10de0094, "GPU 94 HDMI/DP", patch_nvhdmi),
4181 HDA_CODEC_ENTRY(0x10de0095, "GPU 95 HDMI/DP", patch_nvhdmi),
4182 HDA_CODEC_ENTRY(0x10de0097, "GPU 97 HDMI/DP", patch_nvhdmi),
4183 HDA_CODEC_ENTRY(0x10de0098, "GPU 98 HDMI/DP", patch_nvhdmi),
4184 HDA_CODEC_ENTRY(0x10de0099, "GPU 99 HDMI/DP", patch_nvhdmi),
4185 HDA_CODEC_ENTRY(0x10de8001, "MCP73 HDMI", patch_nvhdmi_2ch),
4186 HDA_CODEC_ENTRY(0x10de8067, "MCP67/68 HDMI", patch_nvhdmi_2ch),
4187 HDA_CODEC_ENTRY(0x11069f80, "VX900 HDMI/DP", patch_via_hdmi),
4188 HDA_CODEC_ENTRY(0x11069f81, "VX900 HDMI/DP", patch_via_hdmi),
4189 HDA_CODEC_ENTRY(0x11069f84, "VX11 HDMI/DP", patch_generic_hdmi),
4190 HDA_CODEC_ENTRY(0x11069f85, "VX11 HDMI/DP", patch_generic_hdmi),
4191 HDA_CODEC_ENTRY(0x80860054, "IbexPeak HDMI", patch_i915_cpt_hdmi),
4192 HDA_CODEC_ENTRY(0x80862800, "Geminilake HDMI", patch_i915_glk_hdmi),
4193 HDA_CODEC_ENTRY(0x80862801, "Bearlake HDMI", patch_generic_hdmi),
4194 HDA_CODEC_ENTRY(0x80862802, "Cantiga HDMI", patch_generic_hdmi),
4195 HDA_CODEC_ENTRY(0x80862803, "Eaglelake HDMI", patch_generic_hdmi),
4196 HDA_CODEC_ENTRY(0x80862804, "IbexPeak HDMI", patch_i915_cpt_hdmi),
4197 HDA_CODEC_ENTRY(0x80862805, "CougarPoint HDMI", patch_i915_cpt_hdmi),
4198 HDA_CODEC_ENTRY(0x80862806, "PantherPoint HDMI", patch_i915_cpt_hdmi),
4199 HDA_CODEC_ENTRY(0x80862807, "Haswell HDMI", patch_i915_hsw_hdmi),
4200 HDA_CODEC_ENTRY(0x80862808, "Broadwell HDMI", patch_i915_hsw_hdmi),
4201 HDA_CODEC_ENTRY(0x80862809, "Skylake HDMI", patch_i915_hsw_hdmi),
4202 HDA_CODEC_ENTRY(0x8086280a, "Broxton HDMI", patch_i915_hsw_hdmi),
4203 HDA_CODEC_ENTRY(0x8086280b, "Kabylake HDMI", patch_i915_hsw_hdmi),
4204 HDA_CODEC_ENTRY(0x8086280c, "Cannonlake HDMI", patch_i915_glk_hdmi),
4205 HDA_CODEC_ENTRY(0x8086280d, "Geminilake HDMI", patch_i915_glk_hdmi),
4206 HDA_CODEC_ENTRY(0x8086280f, "Icelake HDMI", patch_i915_icl_hdmi),
4207 HDA_CODEC_ENTRY(0x80862812, "Tigerlake HDMI", patch_i915_tgl_hdmi),
4208 HDA_CODEC_ENTRY(0x80862880, "CedarTrail HDMI", patch_generic_hdmi),
4209 HDA_CODEC_ENTRY(0x80862882, "Valleyview2 HDMI", patch_i915_byt_hdmi),
4210 HDA_CODEC_ENTRY(0x80862883, "Braswell HDMI", patch_i915_byt_hdmi),
4211 HDA_CODEC_ENTRY(0x808629fb, "Crestline HDMI", patch_generic_hdmi),
4212 /* special ID for generic HDMI */
4213 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC_HDMI, "Generic HDMI", patch_generic_hdmi),
4216 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_hdmi);
4218 MODULE_LICENSE("GPL");
4219 MODULE_DESCRIPTION("HDMI HD-audio codec");
4220 MODULE_ALIAS("snd-hda-codec-intelhdmi");
4221 MODULE_ALIAS("snd-hda-codec-nvhdmi");
4222 MODULE_ALIAS("snd-hda-codec-atihdmi");
4224 static struct hda_codec_driver hdmi_driver = {
4225 .id = snd_hda_id_hdmi,
4228 module_hda_codec_driver(hdmi_driver);