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 #define is_haswell(codec) ((codec)->core.vendor_id == 0x80862807)
41 #define is_broadwell(codec) ((codec)->core.vendor_id == 0x80862808)
42 #define is_skylake(codec) ((codec)->core.vendor_id == 0x80862809)
43 #define is_broxton(codec) ((codec)->core.vendor_id == 0x8086280a)
44 #define is_kabylake(codec) ((codec)->core.vendor_id == 0x8086280b)
45 #define is_geminilake(codec) (((codec)->core.vendor_id == 0x8086280d) || \
46 ((codec)->core.vendor_id == 0x80862800))
47 #define is_cannonlake(codec) ((codec)->core.vendor_id == 0x8086280c)
48 #define is_icelake(codec) ((codec)->core.vendor_id == 0x8086280f)
49 #define is_haswell_plus(codec) (is_haswell(codec) || is_broadwell(codec) \
50 || is_skylake(codec) || is_broxton(codec) \
51 || is_kabylake(codec) || is_geminilake(codec) \
52 || is_cannonlake(codec) || is_icelake(codec))
53 #define is_valleyview(codec) ((codec)->core.vendor_id == 0x80862882)
54 #define is_cherryview(codec) ((codec)->core.vendor_id == 0x80862883)
55 #define is_valleyview_plus(codec) (is_valleyview(codec) || is_cherryview(codec))
57 struct hdmi_spec_per_cvt {
60 unsigned int channels_min;
61 unsigned int channels_max;
67 /* max. connections to a widget */
68 #define HDA_MAX_CONNECTIONS 32
70 struct hdmi_spec_per_pin {
73 /* pin idx, different device entries on the same pin use the same idx */
76 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
80 struct hda_codec *codec;
81 struct hdmi_eld sink_eld;
83 struct delayed_work work;
84 struct hdmi_pcm *pcm; /* pointer to spec->pcm_rec[n] dynamically*/
85 int pcm_idx; /* which pcm is attached. -1 means no pcm is attached */
87 bool setup; /* the stream has been set up by prepare callback */
88 int channels; /* current number of channels */
90 bool chmap_set; /* channel-map override by ALSA API? */
91 unsigned char chmap[8]; /* ALSA API channel-map */
92 #ifdef CONFIG_SND_PROC_FS
93 struct snd_info_entry *proc_entry;
97 /* operations used by generic code that can be overridden by patches */
99 int (*pin_get_eld)(struct hda_codec *codec, hda_nid_t pin_nid,
100 unsigned char *buf, int *eld_size);
102 void (*pin_setup_infoframe)(struct hda_codec *codec, hda_nid_t pin_nid,
103 int ca, int active_channels, int conn_type);
105 /* enable/disable HBR (HD passthrough) */
106 int (*pin_hbr_setup)(struct hda_codec *codec, hda_nid_t pin_nid, bool hbr);
108 int (*setup_stream)(struct hda_codec *codec, hda_nid_t cvt_nid,
109 hda_nid_t pin_nid, u32 stream_tag, int format);
111 void (*pin_cvt_fixup)(struct hda_codec *codec,
112 struct hdmi_spec_per_pin *per_pin,
118 struct snd_jack *jack;
119 struct snd_kcontrol *eld_ctl;
123 struct hda_codec *codec;
125 struct snd_array cvts; /* struct hdmi_spec_per_cvt */
126 hda_nid_t cvt_nids[4]; /* only for haswell fix */
129 * num_pins is the number of virtual pins
130 * for example, there are 3 pins, and each pin
131 * has 4 device entries, then the num_pins is 12
135 * num_nids is the number of real pins
136 * In the above example, num_nids is 3
140 * dev_num is the number of device entries
142 * In the above example, dev_num is 4
145 struct snd_array pins; /* struct hdmi_spec_per_pin */
146 struct hdmi_pcm pcm_rec[16];
147 struct mutex pcm_lock;
148 struct mutex bind_lock; /* for audio component binding */
149 /* pcm_bitmap means which pcms have been assigned to pins*/
150 unsigned long pcm_bitmap;
151 int pcm_used; /* counter of pcm_rec[] */
152 /* bitmap shows whether the pcm is opened in user space
153 * bit 0 means the first playback PCM (PCM3);
154 * bit 1 means the second playback PCM, and so on.
156 unsigned long pcm_in_use;
158 struct hdmi_eld temp_eld;
164 * Non-generic VIA/NVIDIA specific
166 struct hda_multi_out multiout;
167 struct hda_pcm_stream pcm_playback;
169 bool use_jack_detect; /* jack detection enabled */
170 bool use_acomp_notifier; /* use eld_notify callback for hotplug */
171 bool acomp_registered; /* audio component registered in this driver */
172 struct drm_audio_component_audio_ops drm_audio_ops;
173 int (*port2pin)(struct hda_codec *, int); /* reverse port/pin mapping */
175 struct hdac_chmap chmap;
176 hda_nid_t vendor_nid;
181 #ifdef CONFIG_SND_HDA_COMPONENT
182 static inline bool codec_has_acomp(struct hda_codec *codec)
184 struct hdmi_spec *spec = codec->spec;
185 return spec->use_acomp_notifier;
188 #define codec_has_acomp(codec) false
191 struct hdmi_audio_infoframe {
198 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
202 u8 LFEPBL01_LSV36_DM_INH7;
205 struct dp_audio_infoframe {
208 u8 ver; /* 0x11 << 2 */
210 u8 CC02_CT47; /* match with HDMI infoframe from this on */
214 u8 LFEPBL01_LSV36_DM_INH7;
217 union audio_infoframe {
218 struct hdmi_audio_infoframe hdmi;
219 struct dp_audio_infoframe dp;
227 #define get_pin(spec, idx) \
228 ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
229 #define get_cvt(spec, idx) \
230 ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx))
231 /* obtain hdmi_pcm object assigned to idx */
232 #define get_hdmi_pcm(spec, idx) (&(spec)->pcm_rec[idx])
233 /* obtain hda_pcm object assigned to idx */
234 #define get_pcm_rec(spec, idx) (get_hdmi_pcm(spec, idx)->pcm)
236 static int pin_id_to_pin_index(struct hda_codec *codec,
237 hda_nid_t pin_nid, int dev_id)
239 struct hdmi_spec *spec = codec->spec;
241 struct hdmi_spec_per_pin *per_pin;
244 * (dev_id == -1) means it is NON-MST pin
245 * return the first virtual pin on this port
250 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
251 per_pin = get_pin(spec, pin_idx);
252 if ((per_pin->pin_nid == pin_nid) &&
253 (per_pin->dev_id == dev_id))
257 codec_warn(codec, "HDMI: pin nid %d not registered\n", pin_nid);
261 static int hinfo_to_pcm_index(struct hda_codec *codec,
262 struct hda_pcm_stream *hinfo)
264 struct hdmi_spec *spec = codec->spec;
267 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++)
268 if (get_pcm_rec(spec, pcm_idx)->stream == hinfo)
271 codec_warn(codec, "HDMI: hinfo %p not registered\n", hinfo);
275 static int hinfo_to_pin_index(struct hda_codec *codec,
276 struct hda_pcm_stream *hinfo)
278 struct hdmi_spec *spec = codec->spec;
279 struct hdmi_spec_per_pin *per_pin;
282 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
283 per_pin = get_pin(spec, pin_idx);
285 per_pin->pcm->pcm->stream == hinfo)
289 codec_dbg(codec, "HDMI: hinfo %p not registered\n", hinfo);
293 static struct hdmi_spec_per_pin *pcm_idx_to_pin(struct hdmi_spec *spec,
297 struct hdmi_spec_per_pin *per_pin;
299 for (i = 0; i < spec->num_pins; i++) {
300 per_pin = get_pin(spec, i);
301 if (per_pin->pcm_idx == pcm_idx)
307 static int cvt_nid_to_cvt_index(struct hda_codec *codec, hda_nid_t cvt_nid)
309 struct hdmi_spec *spec = codec->spec;
312 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
313 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
316 codec_warn(codec, "HDMI: cvt nid %d not registered\n", cvt_nid);
320 static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
321 struct snd_ctl_elem_info *uinfo)
323 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
324 struct hdmi_spec *spec = codec->spec;
325 struct hdmi_spec_per_pin *per_pin;
326 struct hdmi_eld *eld;
329 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
331 pcm_idx = kcontrol->private_value;
332 mutex_lock(&spec->pcm_lock);
333 per_pin = pcm_idx_to_pin(spec, pcm_idx);
335 /* no pin is bound to the pcm */
339 eld = &per_pin->sink_eld;
340 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
343 mutex_unlock(&spec->pcm_lock);
347 static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
348 struct snd_ctl_elem_value *ucontrol)
350 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
351 struct hdmi_spec *spec = codec->spec;
352 struct hdmi_spec_per_pin *per_pin;
353 struct hdmi_eld *eld;
357 pcm_idx = kcontrol->private_value;
358 mutex_lock(&spec->pcm_lock);
359 per_pin = pcm_idx_to_pin(spec, pcm_idx);
361 /* no pin is bound to the pcm */
362 memset(ucontrol->value.bytes.data, 0,
363 ARRAY_SIZE(ucontrol->value.bytes.data));
367 eld = &per_pin->sink_eld;
368 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data) ||
369 eld->eld_size > ELD_MAX_SIZE) {
375 memset(ucontrol->value.bytes.data, 0,
376 ARRAY_SIZE(ucontrol->value.bytes.data));
378 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
382 mutex_unlock(&spec->pcm_lock);
386 static const struct snd_kcontrol_new eld_bytes_ctl = {
387 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
388 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
390 .info = hdmi_eld_ctl_info,
391 .get = hdmi_eld_ctl_get,
394 static int hdmi_create_eld_ctl(struct hda_codec *codec, int pcm_idx,
397 struct snd_kcontrol *kctl;
398 struct hdmi_spec *spec = codec->spec;
401 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
404 kctl->private_value = pcm_idx;
405 kctl->id.device = device;
407 /* no pin nid is associated with the kctl now
408 * tbd: associate pin nid to eld ctl later
410 err = snd_hda_ctl_add(codec, 0, kctl);
414 get_hdmi_pcm(spec, pcm_idx)->eld_ctl = kctl;
419 static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
420 int *packet_index, int *byte_index)
424 val = snd_hda_codec_read(codec, pin_nid, 0,
425 AC_VERB_GET_HDMI_DIP_INDEX, 0);
427 *packet_index = val >> 5;
428 *byte_index = val & 0x1f;
432 static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
433 int packet_index, int byte_index)
437 val = (packet_index << 5) | (byte_index & 0x1f);
439 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
442 static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
445 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
448 static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
450 struct hdmi_spec *spec = codec->spec;
454 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
455 snd_hda_codec_write(codec, pin_nid, 0,
456 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
458 if (spec->dyn_pin_out)
459 /* Disable pin out until stream is active */
462 /* Enable pin out: some machines with GM965 gets broken output
463 * when the pin is disabled or changed while using with HDMI
467 snd_hda_codec_write(codec, pin_nid, 0,
468 AC_VERB_SET_PIN_WIDGET_CONTROL, pin_out);
475 #ifdef CONFIG_SND_PROC_FS
476 static void print_eld_info(struct snd_info_entry *entry,
477 struct snd_info_buffer *buffer)
479 struct hdmi_spec_per_pin *per_pin = entry->private_data;
481 mutex_lock(&per_pin->lock);
482 snd_hdmi_print_eld_info(&per_pin->sink_eld, buffer);
483 mutex_unlock(&per_pin->lock);
486 static void write_eld_info(struct snd_info_entry *entry,
487 struct snd_info_buffer *buffer)
489 struct hdmi_spec_per_pin *per_pin = entry->private_data;
491 mutex_lock(&per_pin->lock);
492 snd_hdmi_write_eld_info(&per_pin->sink_eld, buffer);
493 mutex_unlock(&per_pin->lock);
496 static int eld_proc_new(struct hdmi_spec_per_pin *per_pin, int index)
499 struct hda_codec *codec = per_pin->codec;
500 struct snd_info_entry *entry;
503 snprintf(name, sizeof(name), "eld#%d.%d", codec->addr, index);
504 err = snd_card_proc_new(codec->card, name, &entry);
508 snd_info_set_text_ops(entry, per_pin, print_eld_info);
509 entry->c.text.write = write_eld_info;
511 per_pin->proc_entry = entry;
516 static void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
518 if (!per_pin->codec->bus->shutdown) {
519 snd_info_free_entry(per_pin->proc_entry);
520 per_pin->proc_entry = NULL;
524 static inline int eld_proc_new(struct hdmi_spec_per_pin *per_pin,
529 static inline void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
535 * Audio InfoFrame routines
539 * Enable Audio InfoFrame Transmission
541 static void hdmi_start_infoframe_trans(struct hda_codec *codec,
544 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
545 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
550 * Disable Audio InfoFrame Transmission
552 static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
555 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
556 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
560 static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
562 #ifdef CONFIG_SND_DEBUG_VERBOSE
566 size = snd_hdmi_get_eld_size(codec, pin_nid);
567 codec_dbg(codec, "HDMI: ELD buf size is %d\n", size);
569 for (i = 0; i < 8; i++) {
570 size = snd_hda_codec_read(codec, pin_nid, 0,
571 AC_VERB_GET_HDMI_DIP_SIZE, i);
572 codec_dbg(codec, "HDMI: DIP GP[%d] buf size is %d\n", i, size);
577 static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
583 for (i = 0; i < 8; i++) {
584 size = snd_hda_codec_read(codec, pin_nid, 0,
585 AC_VERB_GET_HDMI_DIP_SIZE, i);
589 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
590 for (j = 1; j < 1000; j++) {
591 hdmi_write_dip_byte(codec, pin_nid, 0x0);
592 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
594 codec_dbg(codec, "dip index %d: %d != %d\n",
596 if (bi == 0) /* byte index wrapped around */
600 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
606 static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
608 u8 *bytes = (u8 *)hdmi_ai;
612 hdmi_ai->checksum = 0;
614 for (i = 0; i < sizeof(*hdmi_ai); i++)
617 hdmi_ai->checksum = -sum;
620 static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
626 hdmi_debug_dip_size(codec, pin_nid);
627 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
629 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
630 for (i = 0; i < size; i++)
631 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
634 static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
640 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
644 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
645 for (i = 0; i < size; i++) {
646 val = snd_hda_codec_read(codec, pin_nid, 0,
647 AC_VERB_GET_HDMI_DIP_DATA, 0);
655 static void hdmi_pin_setup_infoframe(struct hda_codec *codec,
657 int ca, int active_channels,
660 union audio_infoframe ai;
662 memset(&ai, 0, sizeof(ai));
663 if (conn_type == 0) { /* HDMI */
664 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
666 hdmi_ai->type = 0x84;
669 hdmi_ai->CC02_CT47 = active_channels - 1;
671 hdmi_checksum_audio_infoframe(hdmi_ai);
672 } else if (conn_type == 1) { /* DisplayPort */
673 struct dp_audio_infoframe *dp_ai = &ai.dp;
677 dp_ai->ver = 0x11 << 2;
678 dp_ai->CC02_CT47 = active_channels - 1;
681 codec_dbg(codec, "HDMI: unknown connection type at pin %d\n",
687 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
688 * sizeof(*dp_ai) to avoid partial match/update problems when
689 * the user switches between HDMI/DP monitors.
691 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
694 "hdmi_pin_setup_infoframe: pin=%d channels=%d ca=0x%02x\n",
696 active_channels, ca);
697 hdmi_stop_infoframe_trans(codec, pin_nid);
698 hdmi_fill_audio_infoframe(codec, pin_nid,
699 ai.bytes, sizeof(ai));
700 hdmi_start_infoframe_trans(codec, pin_nid);
704 static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
705 struct hdmi_spec_per_pin *per_pin,
708 struct hdmi_spec *spec = codec->spec;
709 struct hdac_chmap *chmap = &spec->chmap;
710 hda_nid_t pin_nid = per_pin->pin_nid;
711 int channels = per_pin->channels;
713 struct hdmi_eld *eld;
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, ca, active_channels,
745 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 /* hda_jack don't support DP MST */
778 check_presence_and_report(codec, jack->nid, 0);
781 static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
783 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
784 struct hda_jack_tbl *jack;
785 int dev_entry = (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
788 * assume DP MST uses dyn_pcm_assign and acomp and
790 * if DP MST supports unsol event, below code need
793 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
796 jack->jack_dirty = 1;
799 "HDMI hot plug event: Codec=%d Pin=%d Device=%d Inactive=%d Presence_Detect=%d ELD_Valid=%d\n",
800 codec->addr, jack->nid, dev_entry, !!(res & AC_UNSOL_RES_IA),
801 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
803 /* hda_jack don't support DP MST */
804 check_presence_and_report(codec, jack->nid, 0);
807 static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
809 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
810 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
811 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
812 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
815 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
830 static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
832 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
833 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
835 if (codec_has_acomp(codec))
838 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
839 codec_dbg(codec, "Unexpected HDMI event tag 0x%x\n", tag);
844 hdmi_intrinsic_event(codec, res);
846 hdmi_non_intrinsic_event(codec, res);
849 static void haswell_verify_D0(struct hda_codec *codec,
850 hda_nid_t cvt_nid, hda_nid_t nid)
854 /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
855 * thus pins could only choose converter 0 for use. Make sure the
856 * converters are in correct power state */
857 if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
858 snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
860 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
861 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
864 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
865 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
866 codec_dbg(codec, "Haswell HDMI audio: Power for pin 0x%x is now D%d\n", nid, pwr);
874 /* HBR should be Non-PCM, 8 channels */
875 #define is_hbr_format(format) \
876 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
878 static int hdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
881 int pinctl, new_pinctl;
883 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
884 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
885 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
888 return hbr ? -EINVAL : 0;
890 new_pinctl = pinctl & ~AC_PINCTL_EPT;
892 new_pinctl |= AC_PINCTL_EPT_HBR;
894 new_pinctl |= AC_PINCTL_EPT_NATIVE;
897 "hdmi_pin_hbr_setup: NID=0x%x, %spinctl=0x%x\n",
899 pinctl == new_pinctl ? "" : "new-",
902 if (pinctl != new_pinctl)
903 snd_hda_codec_write(codec, pin_nid, 0,
904 AC_VERB_SET_PIN_WIDGET_CONTROL,
912 static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
913 hda_nid_t pin_nid, u32 stream_tag, int format)
915 struct hdmi_spec *spec = codec->spec;
919 err = spec->ops.pin_hbr_setup(codec, pin_nid, is_hbr_format(format));
922 codec_dbg(codec, "hdmi_setup_stream: HBR is not supported\n");
926 if (is_haswell_plus(codec)) {
929 * on recent platforms IEC Coding Type is required for HBR
930 * support, read current Digital Converter settings and set
931 * ICT bitfield if needed.
933 param = snd_hda_codec_read(codec, cvt_nid, 0,
934 AC_VERB_GET_DIGI_CONVERT_1, 0);
936 param = (param >> 16) & ~(AC_DIG3_ICT);
938 /* on recent platforms ICT mode is required for HBR support */
939 if (is_hbr_format(format))
942 snd_hda_codec_write(codec, cvt_nid, 0,
943 AC_VERB_SET_DIGI_CONVERT_3, param);
946 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
950 /* Try to find an available converter
951 * If pin_idx is less then zero, just try to find an available converter.
952 * Otherwise, try to find an available converter and get the cvt mux index
955 static int hdmi_choose_cvt(struct hda_codec *codec,
956 int pin_idx, int *cvt_id)
958 struct hdmi_spec *spec = codec->spec;
959 struct hdmi_spec_per_pin *per_pin;
960 struct hdmi_spec_per_cvt *per_cvt = NULL;
961 int cvt_idx, mux_idx = 0;
963 /* pin_idx < 0 means no pin will be bound to the converter */
967 per_pin = get_pin(spec, pin_idx);
969 /* Dynamically assign converter to stream */
970 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
971 per_cvt = get_cvt(spec, cvt_idx);
973 /* Must not already be assigned */
974 if (per_cvt->assigned)
978 /* Must be in pin's mux's list of converters */
979 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
980 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
982 /* Not in mux list */
983 if (mux_idx == per_pin->num_mux_nids)
988 /* No free converters */
989 if (cvt_idx == spec->num_cvts)
993 per_pin->mux_idx = mux_idx;
1001 /* Assure the pin select the right convetor */
1002 static void intel_verify_pin_cvt_connect(struct hda_codec *codec,
1003 struct hdmi_spec_per_pin *per_pin)
1005 hda_nid_t pin_nid = per_pin->pin_nid;
1008 mux_idx = per_pin->mux_idx;
1009 curr = snd_hda_codec_read(codec, pin_nid, 0,
1010 AC_VERB_GET_CONNECT_SEL, 0);
1011 if (curr != mux_idx)
1012 snd_hda_codec_write_cache(codec, pin_nid, 0,
1013 AC_VERB_SET_CONNECT_SEL,
1017 /* get the mux index for the converter of the pins
1018 * converter's mux index is the same for all pins on Intel platform
1020 static int intel_cvt_id_to_mux_idx(struct hdmi_spec *spec,
1025 for (i = 0; i < spec->num_cvts; i++)
1026 if (spec->cvt_nids[i] == cvt_nid)
1031 /* Intel HDMI workaround to fix audio routing issue:
1032 * For some Intel display codecs, pins share the same connection list.
1033 * So a conveter can be selected by multiple pins and playback on any of these
1034 * pins will generate sound on the external display, because audio flows from
1035 * the same converter to the display pipeline. Also muting one pin may make
1036 * other pins have no sound output.
1037 * So this function assures that an assigned converter for a pin is not selected
1038 * by any other pins.
1040 static void intel_not_share_assigned_cvt(struct hda_codec *codec,
1042 int dev_id, int mux_idx)
1044 struct hdmi_spec *spec = codec->spec;
1047 struct hdmi_spec_per_cvt *per_cvt;
1048 struct hdmi_spec_per_pin *per_pin;
1051 /* configure the pins connections */
1052 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1056 per_pin = get_pin(spec, pin_idx);
1058 * pin not connected to monitor
1059 * no need to operate on it
1064 if ((per_pin->pin_nid == pin_nid) &&
1065 (per_pin->dev_id == dev_id))
1069 * if per_pin->dev_id >= dev_num,
1070 * snd_hda_get_dev_select() will fail,
1071 * and the following operation is unpredictable.
1072 * So skip this situation.
1074 dev_num = snd_hda_get_num_devices(codec, per_pin->pin_nid) + 1;
1075 if (per_pin->dev_id >= dev_num)
1078 nid = per_pin->pin_nid;
1081 * Calling this function should not impact
1082 * on the device entry selection
1083 * So let's save the dev id for each pin,
1084 * and restore it when return
1086 dev_id_saved = snd_hda_get_dev_select(codec, nid);
1087 snd_hda_set_dev_select(codec, nid, per_pin->dev_id);
1088 curr = snd_hda_codec_read(codec, nid, 0,
1089 AC_VERB_GET_CONNECT_SEL, 0);
1090 if (curr != mux_idx) {
1091 snd_hda_set_dev_select(codec, nid, dev_id_saved);
1096 /* choose an unassigned converter. The conveters in the
1097 * connection list are in the same order as in the codec.
1099 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1100 per_cvt = get_cvt(spec, cvt_idx);
1101 if (!per_cvt->assigned) {
1103 "choose cvt %d for pin nid %d\n",
1105 snd_hda_codec_write_cache(codec, nid, 0,
1106 AC_VERB_SET_CONNECT_SEL,
1111 snd_hda_set_dev_select(codec, nid, dev_id_saved);
1115 /* A wrapper of intel_not_share_asigned_cvt() */
1116 static void intel_not_share_assigned_cvt_nid(struct hda_codec *codec,
1117 hda_nid_t pin_nid, int dev_id, hda_nid_t cvt_nid)
1120 struct hdmi_spec *spec = codec->spec;
1122 /* On Intel platform, the mapping of converter nid to
1123 * mux index of the pins are always the same.
1124 * The pin nid may be 0, this means all pins will not
1125 * share the converter.
1127 mux_idx = intel_cvt_id_to_mux_idx(spec, cvt_nid);
1129 intel_not_share_assigned_cvt(codec, pin_nid, dev_id, mux_idx);
1132 /* skeleton caller of pin_cvt_fixup ops */
1133 static void pin_cvt_fixup(struct hda_codec *codec,
1134 struct hdmi_spec_per_pin *per_pin,
1137 struct hdmi_spec *spec = codec->spec;
1139 if (spec->ops.pin_cvt_fixup)
1140 spec->ops.pin_cvt_fixup(codec, per_pin, cvt_nid);
1143 /* called in hdmi_pcm_open when no pin is assigned to the PCM
1144 * in dyn_pcm_assign mode.
1146 static int hdmi_pcm_open_no_pin(struct hda_pcm_stream *hinfo,
1147 struct hda_codec *codec,
1148 struct snd_pcm_substream *substream)
1150 struct hdmi_spec *spec = codec->spec;
1151 struct snd_pcm_runtime *runtime = substream->runtime;
1152 int cvt_idx, pcm_idx;
1153 struct hdmi_spec_per_cvt *per_cvt = NULL;
1156 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1160 err = hdmi_choose_cvt(codec, -1, &cvt_idx);
1164 per_cvt = get_cvt(spec, cvt_idx);
1165 per_cvt->assigned = 1;
1166 hinfo->nid = per_cvt->cvt_nid;
1168 pin_cvt_fixup(codec, NULL, per_cvt->cvt_nid);
1170 set_bit(pcm_idx, &spec->pcm_in_use);
1171 /* todo: setup spdif ctls assign */
1173 /* Initially set the converter's capabilities */
1174 hinfo->channels_min = per_cvt->channels_min;
1175 hinfo->channels_max = per_cvt->channels_max;
1176 hinfo->rates = per_cvt->rates;
1177 hinfo->formats = per_cvt->formats;
1178 hinfo->maxbps = per_cvt->maxbps;
1180 /* Store the updated parameters */
1181 runtime->hw.channels_min = hinfo->channels_min;
1182 runtime->hw.channels_max = hinfo->channels_max;
1183 runtime->hw.formats = hinfo->formats;
1184 runtime->hw.rates = hinfo->rates;
1186 snd_pcm_hw_constraint_step(substream->runtime, 0,
1187 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1194 static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1195 struct hda_codec *codec,
1196 struct snd_pcm_substream *substream)
1198 struct hdmi_spec *spec = codec->spec;
1199 struct snd_pcm_runtime *runtime = substream->runtime;
1200 int pin_idx, cvt_idx, pcm_idx;
1201 struct hdmi_spec_per_pin *per_pin;
1202 struct hdmi_eld *eld;
1203 struct hdmi_spec_per_cvt *per_cvt = NULL;
1206 /* Validate hinfo */
1207 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1211 mutex_lock(&spec->pcm_lock);
1212 pin_idx = hinfo_to_pin_index(codec, hinfo);
1213 if (!spec->dyn_pcm_assign) {
1214 if (snd_BUG_ON(pin_idx < 0)) {
1219 /* no pin is assigned to the PCM
1220 * PA need pcm open successfully when probe
1223 err = hdmi_pcm_open_no_pin(hinfo, codec, substream);
1228 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx);
1232 per_cvt = get_cvt(spec, cvt_idx);
1233 /* Claim converter */
1234 per_cvt->assigned = 1;
1236 set_bit(pcm_idx, &spec->pcm_in_use);
1237 per_pin = get_pin(spec, pin_idx);
1238 per_pin->cvt_nid = per_cvt->cvt_nid;
1239 hinfo->nid = per_cvt->cvt_nid;
1241 snd_hda_set_dev_select(codec, per_pin->pin_nid, per_pin->dev_id);
1242 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1243 AC_VERB_SET_CONNECT_SEL,
1246 /* configure unused pins to choose other converters */
1247 pin_cvt_fixup(codec, per_pin, 0);
1249 snd_hda_spdif_ctls_assign(codec, pcm_idx, per_cvt->cvt_nid);
1251 /* Initially set the converter's capabilities */
1252 hinfo->channels_min = per_cvt->channels_min;
1253 hinfo->channels_max = per_cvt->channels_max;
1254 hinfo->rates = per_cvt->rates;
1255 hinfo->formats = per_cvt->formats;
1256 hinfo->maxbps = per_cvt->maxbps;
1258 eld = &per_pin->sink_eld;
1259 /* Restrict capabilities by ELD if this isn't disabled */
1260 if (!static_hdmi_pcm && eld->eld_valid) {
1261 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
1262 if (hinfo->channels_min > hinfo->channels_max ||
1263 !hinfo->rates || !hinfo->formats) {
1264 per_cvt->assigned = 0;
1266 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
1272 /* Store the updated parameters */
1273 runtime->hw.channels_min = hinfo->channels_min;
1274 runtime->hw.channels_max = hinfo->channels_max;
1275 runtime->hw.formats = hinfo->formats;
1276 runtime->hw.rates = hinfo->rates;
1278 snd_pcm_hw_constraint_step(substream->runtime, 0,
1279 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1281 mutex_unlock(&spec->pcm_lock);
1286 * HDA/HDMI auto parsing
1288 static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
1290 struct hdmi_spec *spec = codec->spec;
1291 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1292 hda_nid_t pin_nid = per_pin->pin_nid;
1294 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
1296 "HDMI: pin %d wcaps %#x does not support connection list\n",
1297 pin_nid, get_wcaps(codec, pin_nid));
1301 /* all the device entries on the same pin have the same conn list */
1302 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1304 HDA_MAX_CONNECTIONS);
1309 static int hdmi_find_pcm_slot(struct hdmi_spec *spec,
1310 struct hdmi_spec_per_pin *per_pin)
1314 /* try the prefer PCM */
1315 if (!test_bit(per_pin->pin_nid_idx, &spec->pcm_bitmap))
1316 return per_pin->pin_nid_idx;
1318 /* have a second try; check the "reserved area" over num_pins */
1319 for (i = spec->num_nids; i < spec->pcm_used; i++) {
1320 if (!test_bit(i, &spec->pcm_bitmap))
1324 /* the last try; check the empty slots in pins */
1325 for (i = 0; i < spec->num_nids; i++) {
1326 if (!test_bit(i, &spec->pcm_bitmap))
1332 static void hdmi_attach_hda_pcm(struct hdmi_spec *spec,
1333 struct hdmi_spec_per_pin *per_pin)
1337 /* pcm already be attached to the pin */
1340 idx = hdmi_find_pcm_slot(spec, per_pin);
1343 per_pin->pcm_idx = idx;
1344 per_pin->pcm = get_hdmi_pcm(spec, idx);
1345 set_bit(idx, &spec->pcm_bitmap);
1348 static void hdmi_detach_hda_pcm(struct hdmi_spec *spec,
1349 struct hdmi_spec_per_pin *per_pin)
1353 /* pcm already be detached from the pin */
1356 idx = per_pin->pcm_idx;
1357 per_pin->pcm_idx = -1;
1358 per_pin->pcm = NULL;
1359 if (idx >= 0 && idx < spec->pcm_used)
1360 clear_bit(idx, &spec->pcm_bitmap);
1363 static int hdmi_get_pin_cvt_mux(struct hdmi_spec *spec,
1364 struct hdmi_spec_per_pin *per_pin, hda_nid_t cvt_nid)
1368 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1369 if (per_pin->mux_nids[mux_idx] == cvt_nid)
1374 static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid);
1376 static void hdmi_pcm_setup_pin(struct hdmi_spec *spec,
1377 struct hdmi_spec_per_pin *per_pin)
1379 struct hda_codec *codec = per_pin->codec;
1380 struct hda_pcm *pcm;
1381 struct hda_pcm_stream *hinfo;
1382 struct snd_pcm_substream *substream;
1386 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1387 pcm = get_pcm_rec(spec, per_pin->pcm_idx);
1392 if (!test_bit(per_pin->pcm_idx, &spec->pcm_in_use))
1395 /* hdmi audio only uses playback and one substream */
1396 hinfo = pcm->stream;
1397 substream = pcm->pcm->streams[0].substream;
1399 per_pin->cvt_nid = hinfo->nid;
1401 mux_idx = hdmi_get_pin_cvt_mux(spec, per_pin, hinfo->nid);
1402 if (mux_idx < per_pin->num_mux_nids) {
1403 snd_hda_set_dev_select(codec, per_pin->pin_nid,
1405 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1406 AC_VERB_SET_CONNECT_SEL,
1409 snd_hda_spdif_ctls_assign(codec, per_pin->pcm_idx, hinfo->nid);
1411 non_pcm = check_non_pcm_per_cvt(codec, hinfo->nid);
1412 if (substream->runtime)
1413 per_pin->channels = substream->runtime->channels;
1414 per_pin->setup = true;
1415 per_pin->mux_idx = mux_idx;
1417 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1420 static void hdmi_pcm_reset_pin(struct hdmi_spec *spec,
1421 struct hdmi_spec_per_pin *per_pin)
1423 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1424 snd_hda_spdif_ctls_unassign(per_pin->codec, per_pin->pcm_idx);
1426 per_pin->chmap_set = false;
1427 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
1429 per_pin->setup = false;
1430 per_pin->channels = 0;
1433 /* update per_pin ELD from the given new ELD;
1434 * setup info frame and notification accordingly
1436 static bool update_eld(struct hda_codec *codec,
1437 struct hdmi_spec_per_pin *per_pin,
1438 struct hdmi_eld *eld)
1440 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
1441 struct hdmi_spec *spec = codec->spec;
1442 bool old_eld_valid = pin_eld->eld_valid;
1446 /* for monitor disconnection, save pcm_idx firstly */
1447 pcm_idx = per_pin->pcm_idx;
1448 if (spec->dyn_pcm_assign) {
1449 if (eld->eld_valid) {
1450 hdmi_attach_hda_pcm(spec, per_pin);
1451 hdmi_pcm_setup_pin(spec, per_pin);
1453 hdmi_pcm_reset_pin(spec, per_pin);
1454 hdmi_detach_hda_pcm(spec, per_pin);
1457 /* if pcm_idx == -1, it means this is in monitor connection event
1458 * we can get the correct pcm_idx now.
1461 pcm_idx = per_pin->pcm_idx;
1464 snd_hdmi_show_eld(codec, &eld->info);
1466 eld_changed = (pin_eld->eld_valid != eld->eld_valid);
1467 eld_changed |= (pin_eld->monitor_present != eld->monitor_present);
1468 if (!eld_changed && eld->eld_valid && pin_eld->eld_valid)
1469 if (pin_eld->eld_size != eld->eld_size ||
1470 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
1471 eld->eld_size) != 0)
1475 pin_eld->monitor_present = eld->monitor_present;
1476 pin_eld->eld_valid = eld->eld_valid;
1477 pin_eld->eld_size = eld->eld_size;
1479 memcpy(pin_eld->eld_buffer, eld->eld_buffer,
1481 pin_eld->info = eld->info;
1485 * Re-setup pin and infoframe. This is needed e.g. when
1486 * - sink is first plugged-in
1487 * - transcoder can change during stream playback on Haswell
1488 * and this can make HW reset converter selection on a pin.
1490 if (eld->eld_valid && !old_eld_valid && per_pin->setup) {
1491 pin_cvt_fixup(codec, per_pin, 0);
1492 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1495 if (eld_changed && pcm_idx >= 0)
1496 snd_ctl_notify(codec->card,
1497 SNDRV_CTL_EVENT_MASK_VALUE |
1498 SNDRV_CTL_EVENT_MASK_INFO,
1499 &get_hdmi_pcm(spec, pcm_idx)->eld_ctl->id);
1503 /* update ELD and jack state via HD-audio verbs */
1504 static bool hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin *per_pin,
1507 struct hda_jack_tbl *jack;
1508 struct hda_codec *codec = per_pin->codec;
1509 struct hdmi_spec *spec = codec->spec;
1510 struct hdmi_eld *eld = &spec->temp_eld;
1511 hda_nid_t pin_nid = per_pin->pin_nid;
1513 * Always execute a GetPinSense verb here, even when called from
1514 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1515 * response's PD bit is not the real PD value, but indicates that
1516 * the real PD value changed. An older version of the HD-audio
1517 * specification worked this way. Hence, we just ignore the data in
1518 * the unsolicited response to avoid custom WARs.
1522 bool do_repoll = false;
1524 present = snd_hda_pin_sense(codec, pin_nid);
1526 mutex_lock(&per_pin->lock);
1527 eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1528 if (eld->monitor_present)
1529 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1531 eld->eld_valid = false;
1534 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
1535 codec->addr, pin_nid, eld->monitor_present, eld->eld_valid);
1537 if (eld->eld_valid) {
1538 if (spec->ops.pin_get_eld(codec, pin_nid, eld->eld_buffer,
1539 &eld->eld_size) < 0)
1540 eld->eld_valid = false;
1542 if (snd_hdmi_parse_eld(codec, &eld->info, eld->eld_buffer,
1544 eld->eld_valid = false;
1546 if (!eld->eld_valid && repoll)
1551 schedule_delayed_work(&per_pin->work, msecs_to_jiffies(300));
1553 update_eld(codec, per_pin, eld);
1555 ret = !repoll || !eld->monitor_present || eld->eld_valid;
1557 jack = snd_hda_jack_tbl_get(codec, pin_nid);
1559 jack->block_report = !ret;
1560 jack->pin_sense = (eld->monitor_present && eld->eld_valid) ?
1561 AC_PINSENSE_PRESENCE : 0;
1563 mutex_unlock(&per_pin->lock);
1567 static struct snd_jack *pin_idx_to_jack(struct hda_codec *codec,
1568 struct hdmi_spec_per_pin *per_pin)
1570 struct hdmi_spec *spec = codec->spec;
1571 struct snd_jack *jack = NULL;
1572 struct hda_jack_tbl *jack_tbl;
1574 /* if !dyn_pcm_assign, get jack from hda_jack_tbl
1575 * in !dyn_pcm_assign case, spec->pcm_rec[].jack is not
1576 * NULL even after snd_hda_jack_tbl_clear() is called to
1577 * free snd_jack. This may cause access invalid memory
1578 * when calling snd_jack_report
1580 if (per_pin->pcm_idx >= 0 && spec->dyn_pcm_assign)
1581 jack = spec->pcm_rec[per_pin->pcm_idx].jack;
1582 else if (!spec->dyn_pcm_assign) {
1584 * jack tbl doesn't support DP MST
1585 * DP MST will use dyn_pcm_assign,
1586 * so DP MST will never come here
1588 jack_tbl = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
1590 jack = jack_tbl->jack;
1595 /* update ELD and jack state via audio component */
1596 static void sync_eld_via_acomp(struct hda_codec *codec,
1597 struct hdmi_spec_per_pin *per_pin)
1599 struct hdmi_spec *spec = codec->spec;
1600 struct hdmi_eld *eld = &spec->temp_eld;
1601 struct snd_jack *jack = NULL;
1605 mutex_lock(&per_pin->lock);
1606 eld->monitor_present = false;
1607 size = snd_hdac_acomp_get_eld(&codec->core, per_pin->pin_nid,
1608 per_pin->dev_id, &eld->monitor_present,
1609 eld->eld_buffer, ELD_MAX_SIZE);
1611 size = min(size, ELD_MAX_SIZE);
1612 if (snd_hdmi_parse_eld(codec, &eld->info,
1613 eld->eld_buffer, size) < 0)
1618 eld->eld_valid = true;
1619 eld->eld_size = size;
1621 eld->eld_valid = false;
1625 /* pcm_idx >=0 before update_eld() means it is in monitor
1626 * disconnected event. Jack must be fetched before update_eld()
1628 jack = pin_idx_to_jack(codec, per_pin);
1629 changed = update_eld(codec, per_pin, eld);
1631 jack = pin_idx_to_jack(codec, per_pin);
1632 if (changed && jack)
1633 snd_jack_report(jack,
1634 (eld->monitor_present && eld->eld_valid) ?
1635 SND_JACK_AVOUT : 0);
1636 mutex_unlock(&per_pin->lock);
1639 static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
1641 struct hda_codec *codec = per_pin->codec;
1644 /* no temporary power up/down needed for component notifier */
1645 if (!codec_has_acomp(codec)) {
1646 ret = snd_hda_power_up_pm(codec);
1647 if (ret < 0 && pm_runtime_suspended(hda_codec_dev(codec))) {
1648 snd_hda_power_down_pm(codec);
1651 ret = hdmi_present_sense_via_verbs(per_pin, repoll);
1652 snd_hda_power_down_pm(codec);
1654 sync_eld_via_acomp(codec, per_pin);
1655 ret = false; /* don't call snd_hda_jack_report_sync() */
1661 static void hdmi_repoll_eld(struct work_struct *work)
1663 struct hdmi_spec_per_pin *per_pin =
1664 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1665 struct hda_codec *codec = per_pin->codec;
1666 struct hdmi_spec *spec = codec->spec;
1667 struct hda_jack_tbl *jack;
1669 jack = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
1671 jack->jack_dirty = 1;
1673 if (per_pin->repoll_count++ > 6)
1674 per_pin->repoll_count = 0;
1676 mutex_lock(&spec->pcm_lock);
1677 if (hdmi_present_sense(per_pin, per_pin->repoll_count))
1678 snd_hda_jack_report_sync(per_pin->codec);
1679 mutex_unlock(&spec->pcm_lock);
1682 static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1685 static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1687 struct hdmi_spec *spec = codec->spec;
1688 unsigned int caps, config;
1690 struct hdmi_spec_per_pin *per_pin;
1694 caps = snd_hda_query_pin_caps(codec, pin_nid);
1695 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1699 * For DP MST audio, Configuration Default is the same for
1700 * all device entries on the same pin
1702 config = snd_hda_codec_get_pincfg(codec, pin_nid);
1703 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
1707 * To simplify the implementation, malloc all
1708 * the virtual pins in the initialization statically
1710 if (is_haswell_plus(codec)) {
1712 * On Intel platforms, device entries number is
1713 * changed dynamically. If there is a DP MST
1714 * hub connected, the device entries number is 3.
1715 * Otherwise, it is 1.
1716 * Here we manually set dev_num to 3, so that
1717 * we can initialize all the device entries when
1718 * bootup statically.
1722 } else if (spec->dyn_pcm_assign && codec->dp_mst) {
1723 dev_num = snd_hda_get_num_devices(codec, pin_nid) + 1;
1725 * spec->dev_num is the maxinum number of device entries
1726 * among all the pins
1728 spec->dev_num = (spec->dev_num > dev_num) ?
1729 spec->dev_num : dev_num;
1732 * If the platform doesn't support DP MST,
1733 * manually set dev_num to 1. This means
1734 * the pin has only one device entry.
1740 for (i = 0; i < dev_num; i++) {
1741 pin_idx = spec->num_pins;
1742 per_pin = snd_array_new(&spec->pins);
1747 if (spec->dyn_pcm_assign) {
1748 per_pin->pcm = NULL;
1749 per_pin->pcm_idx = -1;
1751 per_pin->pcm = get_hdmi_pcm(spec, pin_idx);
1752 per_pin->pcm_idx = pin_idx;
1754 per_pin->pin_nid = pin_nid;
1755 per_pin->pin_nid_idx = spec->num_nids;
1756 per_pin->dev_id = i;
1757 per_pin->non_pcm = false;
1758 snd_hda_set_dev_select(codec, pin_nid, i);
1759 if (is_haswell_plus(codec))
1760 intel_haswell_fixup_connect_list(codec, pin_nid);
1761 err = hdmi_read_pin_conn(codec, pin_idx);
1771 static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1773 struct hdmi_spec *spec = codec->spec;
1774 struct hdmi_spec_per_cvt *per_cvt;
1778 chans = get_wcaps(codec, cvt_nid);
1779 chans = get_wcaps_channels(chans);
1781 per_cvt = snd_array_new(&spec->cvts);
1785 per_cvt->cvt_nid = cvt_nid;
1786 per_cvt->channels_min = 2;
1788 per_cvt->channels_max = chans;
1789 if (chans > spec->chmap.channels_max)
1790 spec->chmap.channels_max = chans;
1793 err = snd_hda_query_supported_pcm(codec, cvt_nid,
1800 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
1801 spec->cvt_nids[spec->num_cvts] = cvt_nid;
1807 static int hdmi_parse_codec(struct hda_codec *codec)
1812 nodes = snd_hda_get_sub_nodes(codec, codec->core.afg, &nid);
1813 if (!nid || nodes < 0) {
1814 codec_warn(codec, "HDMI: failed to get afg sub nodes\n");
1818 for (i = 0; i < nodes; i++, nid++) {
1822 caps = get_wcaps(codec, nid);
1823 type = get_wcaps_type(caps);
1825 if (!(caps & AC_WCAP_DIGITAL))
1829 case AC_WID_AUD_OUT:
1830 hdmi_add_cvt(codec, nid);
1833 hdmi_add_pin(codec, nid);
1843 static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1845 struct hda_spdif_out *spdif;
1848 mutex_lock(&codec->spdif_mutex);
1849 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
1850 /* Add sanity check to pass klockwork check.
1851 * This should never happen.
1853 if (WARN_ON(spdif == NULL))
1855 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
1856 mutex_unlock(&codec->spdif_mutex);
1864 static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1865 struct hda_codec *codec,
1866 unsigned int stream_tag,
1867 unsigned int format,
1868 struct snd_pcm_substream *substream)
1870 hda_nid_t cvt_nid = hinfo->nid;
1871 struct hdmi_spec *spec = codec->spec;
1873 struct hdmi_spec_per_pin *per_pin;
1875 struct snd_pcm_runtime *runtime = substream->runtime;
1880 mutex_lock(&spec->pcm_lock);
1881 pin_idx = hinfo_to_pin_index(codec, hinfo);
1882 if (spec->dyn_pcm_assign && pin_idx < 0) {
1883 /* when dyn_pcm_assign and pcm is not bound to a pin
1884 * skip pin setup and return 0 to make audio playback
1887 pin_cvt_fixup(codec, NULL, cvt_nid);
1888 snd_hda_codec_setup_stream(codec, cvt_nid,
1889 stream_tag, 0, format);
1893 if (snd_BUG_ON(pin_idx < 0)) {
1897 per_pin = get_pin(spec, pin_idx);
1898 pin_nid = per_pin->pin_nid;
1900 /* Verify pin:cvt selections to avoid silent audio after S3.
1901 * After S3, the audio driver restores pin:cvt selections
1902 * but this can happen before gfx is ready and such selection
1903 * is overlooked by HW. Thus multiple pins can share a same
1904 * default convertor and mute control will affect each other,
1905 * which can cause a resumed audio playback become silent
1908 pin_cvt_fixup(codec, per_pin, 0);
1910 /* Call sync_audio_rate to set the N/CTS/M manually if necessary */
1911 /* Todo: add DP1.2 MST audio support later */
1912 if (codec_has_acomp(codec))
1913 snd_hdac_sync_audio_rate(&codec->core, pin_nid, per_pin->dev_id,
1916 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
1917 mutex_lock(&per_pin->lock);
1918 per_pin->channels = substream->runtime->channels;
1919 per_pin->setup = true;
1921 if (get_wcaps(codec, cvt_nid) & AC_WCAP_STRIPE) {
1922 stripe = snd_hdac_get_stream_stripe_ctl(&codec->bus->core,
1924 snd_hda_codec_write(codec, cvt_nid, 0,
1925 AC_VERB_SET_STRIPE_CONTROL,
1929 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1930 mutex_unlock(&per_pin->lock);
1931 if (spec->dyn_pin_out) {
1932 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
1933 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1934 snd_hda_codec_write(codec, pin_nid, 0,
1935 AC_VERB_SET_PIN_WIDGET_CONTROL,
1939 /* snd_hda_set_dev_select() has been called before */
1940 err = spec->ops.setup_stream(codec, cvt_nid, pin_nid,
1941 stream_tag, format);
1943 mutex_unlock(&spec->pcm_lock);
1947 static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1948 struct hda_codec *codec,
1949 struct snd_pcm_substream *substream)
1951 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1955 static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
1956 struct hda_codec *codec,
1957 struct snd_pcm_substream *substream)
1959 struct hdmi_spec *spec = codec->spec;
1960 int cvt_idx, pin_idx, pcm_idx;
1961 struct hdmi_spec_per_cvt *per_cvt;
1962 struct hdmi_spec_per_pin *per_pin;
1967 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1968 if (snd_BUG_ON(pcm_idx < 0))
1970 cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid);
1971 if (snd_BUG_ON(cvt_idx < 0))
1973 per_cvt = get_cvt(spec, cvt_idx);
1975 snd_BUG_ON(!per_cvt->assigned);
1976 per_cvt->assigned = 0;
1979 mutex_lock(&spec->pcm_lock);
1980 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
1981 clear_bit(pcm_idx, &spec->pcm_in_use);
1982 pin_idx = hinfo_to_pin_index(codec, hinfo);
1983 if (spec->dyn_pcm_assign && pin_idx < 0)
1986 if (snd_BUG_ON(pin_idx < 0)) {
1990 per_pin = get_pin(spec, pin_idx);
1992 if (spec->dyn_pin_out) {
1993 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
1994 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1995 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
1996 AC_VERB_SET_PIN_WIDGET_CONTROL,
2000 mutex_lock(&per_pin->lock);
2001 per_pin->chmap_set = false;
2002 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
2004 per_pin->setup = false;
2005 per_pin->channels = 0;
2006 mutex_unlock(&per_pin->lock);
2008 mutex_unlock(&spec->pcm_lock);
2014 static const struct hda_pcm_ops generic_ops = {
2015 .open = hdmi_pcm_open,
2016 .close = hdmi_pcm_close,
2017 .prepare = generic_hdmi_playback_pcm_prepare,
2018 .cleanup = generic_hdmi_playback_pcm_cleanup,
2021 static int hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
2023 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2024 struct hdmi_spec *spec = codec->spec;
2025 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2030 return per_pin->sink_eld.info.spk_alloc;
2033 static void hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
2034 unsigned char *chmap)
2036 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2037 struct hdmi_spec *spec = codec->spec;
2038 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2040 /* chmap is already set to 0 in caller */
2044 memcpy(chmap, per_pin->chmap, ARRAY_SIZE(per_pin->chmap));
2047 static void hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
2048 unsigned char *chmap, int prepared)
2050 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2051 struct hdmi_spec *spec = codec->spec;
2052 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2056 mutex_lock(&per_pin->lock);
2057 per_pin->chmap_set = true;
2058 memcpy(per_pin->chmap, chmap, ARRAY_SIZE(per_pin->chmap));
2060 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
2061 mutex_unlock(&per_pin->lock);
2064 static bool is_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
2066 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2067 struct hdmi_spec *spec = codec->spec;
2068 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2070 return per_pin ? true:false;
2073 static int generic_hdmi_build_pcms(struct hda_codec *codec)
2075 struct hdmi_spec *spec = codec->spec;
2079 * for non-mst mode, pcm number is the same as before
2080 * for DP MST mode, pcm number is (nid number + dev_num - 1)
2081 * dev_num is the device entry number in a pin
2084 for (idx = 0; idx < spec->num_nids + spec->dev_num - 1; idx++) {
2085 struct hda_pcm *info;
2086 struct hda_pcm_stream *pstr;
2088 info = snd_hda_codec_pcm_new(codec, "HDMI %d", idx);
2092 spec->pcm_rec[idx].pcm = info;
2094 info->pcm_type = HDA_PCM_TYPE_HDMI;
2095 info->own_chmap = true;
2097 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2098 pstr->substreams = 1;
2099 pstr->ops = generic_ops;
2100 /* pcm number is less than 16 */
2101 if (spec->pcm_used >= 16)
2103 /* other pstr fields are set in open */
2109 static void free_hdmi_jack_priv(struct snd_jack *jack)
2111 struct hdmi_pcm *pcm = jack->private_data;
2116 static int add_hdmi_jack_kctl(struct hda_codec *codec,
2117 struct hdmi_spec *spec,
2121 struct snd_jack *jack;
2124 err = snd_jack_new(codec->card, name, SND_JACK_AVOUT, &jack,
2129 spec->pcm_rec[pcm_idx].jack = jack;
2130 jack->private_data = &spec->pcm_rec[pcm_idx];
2131 jack->private_free = free_hdmi_jack_priv;
2135 static int generic_hdmi_build_jack(struct hda_codec *codec, int pcm_idx)
2137 char hdmi_str[32] = "HDMI/DP";
2138 struct hdmi_spec *spec = codec->spec;
2139 struct hdmi_spec_per_pin *per_pin;
2140 struct hda_jack_tbl *jack;
2141 int pcmdev = get_pcm_rec(spec, pcm_idx)->device;
2146 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
2148 if (spec->dyn_pcm_assign)
2149 return add_hdmi_jack_kctl(codec, spec, pcm_idx, hdmi_str);
2151 /* for !dyn_pcm_assign, we still use hda_jack for compatibility */
2152 /* if !dyn_pcm_assign, it must be non-MST mode.
2153 * This means pcms and pins are statically mapped.
2154 * And pcm_idx is pin_idx.
2156 per_pin = get_pin(spec, pcm_idx);
2157 phantom_jack = !is_jack_detectable(codec, per_pin->pin_nid);
2159 strncat(hdmi_str, " Phantom",
2160 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
2161 ret = snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str,
2162 phantom_jack, 0, NULL);
2165 jack = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
2168 /* assign jack->jack to pcm_rec[].jack to
2169 * align with dyn_pcm_assign mode
2171 spec->pcm_rec[pcm_idx].jack = jack->jack;
2175 static int generic_hdmi_build_controls(struct hda_codec *codec)
2177 struct hdmi_spec *spec = codec->spec;
2179 int pin_idx, pcm_idx;
2181 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2182 if (!get_pcm_rec(spec, pcm_idx)->pcm) {
2183 /* no PCM: mark this for skipping permanently */
2184 set_bit(pcm_idx, &spec->pcm_bitmap);
2188 err = generic_hdmi_build_jack(codec, pcm_idx);
2192 /* create the spdif for each pcm
2193 * pin will be bound when monitor is connected
2195 if (spec->dyn_pcm_assign)
2196 err = snd_hda_create_dig_out_ctls(codec,
2197 0, spec->cvt_nids[0],
2200 struct hdmi_spec_per_pin *per_pin =
2201 get_pin(spec, pcm_idx);
2202 err = snd_hda_create_dig_out_ctls(codec,
2204 per_pin->mux_nids[0],
2209 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
2211 dev = get_pcm_rec(spec, pcm_idx)->device;
2212 if (dev != SNDRV_PCM_INVALID_DEVICE) {
2213 /* add control for ELD Bytes */
2214 err = hdmi_create_eld_ctl(codec, pcm_idx, dev);
2220 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2221 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2223 hdmi_present_sense(per_pin, 0);
2226 /* add channel maps */
2227 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2228 struct hda_pcm *pcm;
2230 pcm = get_pcm_rec(spec, pcm_idx);
2231 if (!pcm || !pcm->pcm)
2233 err = snd_hdac_add_chmap_ctls(pcm->pcm, pcm_idx, &spec->chmap);
2241 static int generic_hdmi_init_per_pins(struct hda_codec *codec)
2243 struct hdmi_spec *spec = codec->spec;
2246 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2247 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2249 per_pin->codec = codec;
2250 mutex_init(&per_pin->lock);
2251 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
2252 eld_proc_new(per_pin, pin_idx);
2257 static int generic_hdmi_init(struct hda_codec *codec)
2259 struct hdmi_spec *spec = codec->spec;
2262 mutex_lock(&spec->bind_lock);
2263 spec->use_jack_detect = !codec->jackpoll_interval;
2264 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2265 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2266 hda_nid_t pin_nid = per_pin->pin_nid;
2267 int dev_id = per_pin->dev_id;
2269 snd_hda_set_dev_select(codec, pin_nid, dev_id);
2270 hdmi_init_pin(codec, pin_nid);
2271 if (codec_has_acomp(codec))
2273 if (spec->use_jack_detect)
2274 snd_hda_jack_detect_enable(codec, pin_nid);
2276 snd_hda_jack_detect_enable_callback(codec, pin_nid,
2279 mutex_unlock(&spec->bind_lock);
2283 static void hdmi_array_init(struct hdmi_spec *spec, int nums)
2285 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
2286 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
2289 static void hdmi_array_free(struct hdmi_spec *spec)
2291 snd_array_free(&spec->pins);
2292 snd_array_free(&spec->cvts);
2295 static void generic_spec_free(struct hda_codec *codec)
2297 struct hdmi_spec *spec = codec->spec;
2300 hdmi_array_free(spec);
2304 codec->dp_mst = false;
2307 static void generic_hdmi_free(struct hda_codec *codec)
2309 struct hdmi_spec *spec = codec->spec;
2310 int pin_idx, pcm_idx;
2312 if (spec->acomp_registered) {
2313 snd_hdac_acomp_exit(&codec->bus->core);
2314 } else if (codec_has_acomp(codec)) {
2315 snd_hdac_acomp_register_notifier(&codec->bus->core, NULL);
2316 codec->relaxed_resume = 0;
2319 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2320 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2321 cancel_delayed_work_sync(&per_pin->work);
2322 eld_proc_free(per_pin);
2325 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2326 if (spec->pcm_rec[pcm_idx].jack == NULL)
2328 if (spec->dyn_pcm_assign)
2329 snd_device_free(codec->card,
2330 spec->pcm_rec[pcm_idx].jack);
2332 spec->pcm_rec[pcm_idx].jack = NULL;
2335 generic_spec_free(codec);
2339 static int generic_hdmi_resume(struct hda_codec *codec)
2341 struct hdmi_spec *spec = codec->spec;
2344 codec->patch_ops.init(codec);
2345 regcache_sync(codec->core.regmap);
2347 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2348 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2349 hdmi_present_sense(per_pin, 1);
2355 static const struct hda_codec_ops generic_hdmi_patch_ops = {
2356 .init = generic_hdmi_init,
2357 .free = generic_hdmi_free,
2358 .build_pcms = generic_hdmi_build_pcms,
2359 .build_controls = generic_hdmi_build_controls,
2360 .unsol_event = hdmi_unsol_event,
2362 .resume = generic_hdmi_resume,
2366 static const struct hdmi_ops generic_standard_hdmi_ops = {
2367 .pin_get_eld = snd_hdmi_get_eld,
2368 .pin_setup_infoframe = hdmi_pin_setup_infoframe,
2369 .pin_hbr_setup = hdmi_pin_hbr_setup,
2370 .setup_stream = hdmi_setup_stream,
2373 /* allocate codec->spec and assign/initialize generic parser ops */
2374 static int alloc_generic_hdmi(struct hda_codec *codec)
2376 struct hdmi_spec *spec;
2378 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2382 spec->codec = codec;
2383 spec->ops = generic_standard_hdmi_ops;
2384 spec->dev_num = 1; /* initialize to 1 */
2385 mutex_init(&spec->pcm_lock);
2386 mutex_init(&spec->bind_lock);
2387 snd_hdac_register_chmap_ops(&codec->core, &spec->chmap);
2389 spec->chmap.ops.get_chmap = hdmi_get_chmap;
2390 spec->chmap.ops.set_chmap = hdmi_set_chmap;
2391 spec->chmap.ops.is_pcm_attached = is_hdmi_pcm_attached;
2392 spec->chmap.ops.get_spk_alloc = hdmi_get_spk_alloc,
2395 hdmi_array_init(spec, 4);
2397 codec->patch_ops = generic_hdmi_patch_ops;
2402 /* generic HDMI parser */
2403 static int patch_generic_hdmi(struct hda_codec *codec)
2407 err = alloc_generic_hdmi(codec);
2411 err = hdmi_parse_codec(codec);
2413 generic_spec_free(codec);
2417 generic_hdmi_init_per_pins(codec);
2422 * generic audio component binding
2425 /* turn on / off the unsol event jack detection dynamically */
2426 static void reprogram_jack_detect(struct hda_codec *codec, hda_nid_t nid,
2429 struct hda_jack_tbl *tbl;
2431 tbl = snd_hda_jack_tbl_get(codec, nid);
2433 /* clear unsol even if component notifier is used, or re-enable
2434 * if notifier is cleared
2436 unsigned int val = use_acomp ? 0 : (AC_USRSP_EN | tbl->tag);
2437 snd_hda_codec_write_cache(codec, nid, 0,
2438 AC_VERB_SET_UNSOLICITED_ENABLE, val);
2440 /* if no jack entry was defined beforehand, create a new one
2441 * at need (i.e. only when notifier is cleared)
2444 snd_hda_jack_detect_enable(codec, nid);
2448 /* set up / clear component notifier dynamically */
2449 static void generic_acomp_notifier_set(struct drm_audio_component *acomp,
2452 struct hdmi_spec *spec;
2455 spec = container_of(acomp->audio_ops, struct hdmi_spec, drm_audio_ops);
2456 mutex_lock(&spec->bind_lock);
2457 spec->use_acomp_notifier = use_acomp;
2458 spec->codec->relaxed_resume = use_acomp;
2459 /* reprogram each jack detection logic depending on the notifier */
2460 if (spec->use_jack_detect) {
2461 for (i = 0; i < spec->num_pins; i++)
2462 reprogram_jack_detect(spec->codec,
2463 get_pin(spec, i)->pin_nid,
2466 mutex_unlock(&spec->bind_lock);
2469 /* enable / disable the notifier via master bind / unbind */
2470 static int generic_acomp_master_bind(struct device *dev,
2471 struct drm_audio_component *acomp)
2473 generic_acomp_notifier_set(acomp, true);
2477 static void generic_acomp_master_unbind(struct device *dev,
2478 struct drm_audio_component *acomp)
2480 generic_acomp_notifier_set(acomp, false);
2483 /* check whether both HD-audio and DRM PCI devices belong to the same bus */
2484 static int match_bound_vga(struct device *dev, int subtype, void *data)
2486 struct hdac_bus *bus = data;
2487 struct pci_dev *pci, *master;
2489 if (!dev_is_pci(dev) || !dev_is_pci(bus->dev))
2491 master = to_pci_dev(bus->dev);
2492 pci = to_pci_dev(dev);
2493 return master->bus == pci->bus;
2496 /* audio component notifier for AMD/Nvidia HDMI codecs */
2497 static void generic_acomp_pin_eld_notify(void *audio_ptr, int port, int dev_id)
2499 struct hda_codec *codec = audio_ptr;
2500 struct hdmi_spec *spec = codec->spec;
2501 hda_nid_t pin_nid = spec->port2pin(codec, port);
2505 if (get_wcaps_type(get_wcaps(codec, pin_nid)) != AC_WID_PIN)
2507 /* skip notification during system suspend (but not in runtime PM);
2508 * the state will be updated at resume
2510 if (snd_power_get_state(codec->card) != SNDRV_CTL_POWER_D0)
2512 /* ditto during suspend/resume process itself */
2513 if (snd_hdac_is_in_pm(&codec->core))
2516 check_presence_and_report(codec, pin_nid, dev_id);
2519 /* set up the private drm_audio_ops from the template */
2520 static void setup_drm_audio_ops(struct hda_codec *codec,
2521 const struct drm_audio_component_audio_ops *ops)
2523 struct hdmi_spec *spec = codec->spec;
2525 spec->drm_audio_ops.audio_ptr = codec;
2526 /* intel_audio_codec_enable() or intel_audio_codec_disable()
2527 * will call pin_eld_notify with using audio_ptr pointer
2528 * We need make sure audio_ptr is really setup
2531 spec->drm_audio_ops.pin2port = ops->pin2port;
2532 spec->drm_audio_ops.pin_eld_notify = ops->pin_eld_notify;
2533 spec->drm_audio_ops.master_bind = ops->master_bind;
2534 spec->drm_audio_ops.master_unbind = ops->master_unbind;
2537 /* initialize the generic HDMI audio component */
2538 static void generic_acomp_init(struct hda_codec *codec,
2539 const struct drm_audio_component_audio_ops *ops,
2540 int (*port2pin)(struct hda_codec *, int))
2542 struct hdmi_spec *spec = codec->spec;
2544 spec->port2pin = port2pin;
2545 setup_drm_audio_ops(codec, ops);
2546 if (!snd_hdac_acomp_init(&codec->bus->core, &spec->drm_audio_ops,
2547 match_bound_vga, 0)) {
2548 spec->acomp_registered = true;
2549 codec->bus->keep_power = 0;
2554 * Intel codec parsers and helpers
2557 static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
2560 struct hdmi_spec *spec = codec->spec;
2564 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
2565 if (nconns == spec->num_cvts &&
2566 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
2569 /* override pins connection list */
2570 codec_dbg(codec, "hdmi: haswell: override pin connection 0x%x\n", nid);
2571 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
2574 #define INTEL_GET_VENDOR_VERB 0xf81
2575 #define INTEL_SET_VENDOR_VERB 0x781
2576 #define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
2577 #define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
2579 static void intel_haswell_enable_all_pins(struct hda_codec *codec,
2582 unsigned int vendor_param;
2583 struct hdmi_spec *spec = codec->spec;
2585 vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0,
2586 INTEL_GET_VENDOR_VERB, 0);
2587 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
2590 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
2591 vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0,
2592 INTEL_SET_VENDOR_VERB, vendor_param);
2593 if (vendor_param == -1)
2597 snd_hda_codec_update_widgets(codec);
2600 static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
2602 unsigned int vendor_param;
2603 struct hdmi_spec *spec = codec->spec;
2605 vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0,
2606 INTEL_GET_VENDOR_VERB, 0);
2607 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
2610 /* enable DP1.2 mode */
2611 vendor_param |= INTEL_EN_DP12;
2612 snd_hdac_regmap_add_vendor_verb(&codec->core, INTEL_SET_VENDOR_VERB);
2613 snd_hda_codec_write_cache(codec, spec->vendor_nid, 0,
2614 INTEL_SET_VENDOR_VERB, vendor_param);
2617 /* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
2618 * Otherwise you may get severe h/w communication errors.
2620 static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2621 unsigned int power_state)
2623 if (power_state == AC_PWRST_D0) {
2624 intel_haswell_enable_all_pins(codec, false);
2625 intel_haswell_fixup_enable_dp12(codec);
2628 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
2629 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2632 /* There is a fixed mapping between audio pin node and display port.
2633 * on SNB, IVY, HSW, BSW, SKL, BXT, KBL:
2634 * Pin Widget 5 - PORT B (port = 1 in i915 driver)
2635 * Pin Widget 6 - PORT C (port = 2 in i915 driver)
2636 * Pin Widget 7 - PORT D (port = 3 in i915 driver)
2639 * Pin Widget 4 - PORT B (port = 1 in i915 driver)
2640 * Pin Widget 5 - PORT C (port = 2 in i915 driver)
2641 * Pin Widget 6 - PORT D (port = 3 in i915 driver)
2643 static int intel_base_nid(struct hda_codec *codec)
2645 switch (codec->core.vendor_id) {
2646 case 0x80860054: /* ILK */
2647 case 0x80862804: /* ILK */
2648 case 0x80862882: /* VLV */
2655 static int intel_pin2port(void *audio_ptr, int pin_nid)
2657 struct hda_codec *codec = audio_ptr;
2658 struct hdmi_spec *spec = codec->spec;
2661 if (!spec->port_num) {
2662 base_nid = intel_base_nid(codec);
2663 if (WARN_ON(pin_nid < base_nid || pin_nid >= base_nid + 3))
2665 return pin_nid - base_nid + 1; /* intel port is 1-based */
2669 * looking for the pin number in the mapping table and return
2670 * the index which indicate the port number
2672 for (i = 0; i < spec->port_num; i++) {
2673 if (pin_nid == spec->port_map[i])
2677 /* return -1 if pin number exceeds our expectation */
2678 codec_info(codec, "Can't find the HDMI/DP port for pin %d\n", pin_nid);
2682 static int intel_port2pin(struct hda_codec *codec, int port)
2684 struct hdmi_spec *spec = codec->spec;
2686 if (!spec->port_num) {
2687 /* we assume only from port-B to port-D */
2688 if (port < 1 || port > 3)
2690 /* intel port is 1-based */
2691 return port + intel_base_nid(codec) - 1;
2694 if (port < 1 || port > spec->port_num)
2696 return spec->port_map[port - 1];
2699 static void intel_pin_eld_notify(void *audio_ptr, int port, int pipe)
2701 struct hda_codec *codec = audio_ptr;
2705 pin_nid = intel_port2pin(codec, port);
2708 /* skip notification during system suspend (but not in runtime PM);
2709 * the state will be updated at resume
2711 if (snd_power_get_state(codec->card) != SNDRV_CTL_POWER_D0)
2713 /* ditto during suspend/resume process itself */
2714 if (snd_hdac_is_in_pm(&codec->core))
2717 snd_hdac_i915_set_bclk(&codec->bus->core);
2718 check_presence_and_report(codec, pin_nid, dev_id);
2721 static const struct drm_audio_component_audio_ops intel_audio_ops = {
2722 .pin2port = intel_pin2port,
2723 .pin_eld_notify = intel_pin_eld_notify,
2726 /* register i915 component pin_eld_notify callback */
2727 static void register_i915_notifier(struct hda_codec *codec)
2729 struct hdmi_spec *spec = codec->spec;
2731 spec->use_acomp_notifier = true;
2732 spec->port2pin = intel_port2pin;
2733 setup_drm_audio_ops(codec, &intel_audio_ops);
2734 snd_hdac_acomp_register_notifier(&codec->bus->core,
2735 &spec->drm_audio_ops);
2736 /* no need for forcible resume for jack check thanks to notifier */
2737 codec->relaxed_resume = 1;
2740 /* setup_stream ops override for HSW+ */
2741 static int i915_hsw_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
2742 hda_nid_t pin_nid, u32 stream_tag, int format)
2744 haswell_verify_D0(codec, cvt_nid, pin_nid);
2745 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
2748 /* pin_cvt_fixup ops override for HSW+ and VLV+ */
2749 static void i915_pin_cvt_fixup(struct hda_codec *codec,
2750 struct hdmi_spec_per_pin *per_pin,
2754 snd_hda_set_dev_select(codec, per_pin->pin_nid,
2756 intel_verify_pin_cvt_connect(codec, per_pin);
2757 intel_not_share_assigned_cvt(codec, per_pin->pin_nid,
2758 per_pin->dev_id, per_pin->mux_idx);
2760 intel_not_share_assigned_cvt_nid(codec, 0, 0, cvt_nid);
2764 /* precondition and allocation for Intel codecs */
2765 static int alloc_intel_hdmi(struct hda_codec *codec)
2769 /* requires i915 binding */
2770 if (!codec->bus->core.audio_component) {
2771 codec_info(codec, "No i915 binding for Intel HDMI/DP codec\n");
2772 /* set probe_id here to prevent generic fallback binding */
2773 codec->probe_id = HDA_CODEC_ID_SKIP_PROBE;
2777 err = alloc_generic_hdmi(codec);
2780 /* no need to handle unsol events */
2781 codec->patch_ops.unsol_event = NULL;
2785 /* parse and post-process for Intel codecs */
2786 static int parse_intel_hdmi(struct hda_codec *codec)
2790 err = hdmi_parse_codec(codec);
2792 generic_spec_free(codec);
2796 generic_hdmi_init_per_pins(codec);
2797 register_i915_notifier(codec);
2801 /* Intel Haswell and onwards; audio component with eld notifier */
2802 static int intel_hsw_common_init(struct hda_codec *codec, hda_nid_t vendor_nid,
2803 const int *port_map, int port_num)
2805 struct hdmi_spec *spec;
2808 err = alloc_intel_hdmi(codec);
2812 codec->dp_mst = true;
2813 spec->dyn_pcm_assign = true;
2814 spec->vendor_nid = vendor_nid;
2815 spec->port_map = port_map;
2816 spec->port_num = port_num;
2818 intel_haswell_enable_all_pins(codec, true);
2819 intel_haswell_fixup_enable_dp12(codec);
2821 codec->display_power_control = 1;
2823 codec->patch_ops.set_power_state = haswell_set_power_state;
2824 codec->depop_delay = 0;
2825 codec->auto_runtime_pm = 1;
2827 spec->ops.setup_stream = i915_hsw_setup_stream;
2828 spec->ops.pin_cvt_fixup = i915_pin_cvt_fixup;
2830 return parse_intel_hdmi(codec);
2833 static int patch_i915_hsw_hdmi(struct hda_codec *codec)
2835 return intel_hsw_common_init(codec, 0x08, NULL, 0);
2838 static int patch_i915_glk_hdmi(struct hda_codec *codec)
2840 return intel_hsw_common_init(codec, 0x0b, NULL, 0);
2843 static int patch_i915_icl_hdmi(struct hda_codec *codec)
2846 * pin to port mapping table where the value indicate the pin number and
2847 * the index indicate the port number with 1 base.
2849 static const int map[] = {0x4, 0x6, 0x8, 0xa, 0xb};
2851 return intel_hsw_common_init(codec, 0x02, map, ARRAY_SIZE(map));
2854 static int patch_i915_tgl_hdmi(struct hda_codec *codec)
2857 * pin to port mapping table where the value indicate the pin number and
2858 * the index indicate the port number with 1 base.
2860 static const int map[] = {0x4, 0x6, 0x8, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
2862 return intel_hsw_common_init(codec, 0x02, map, ARRAY_SIZE(map));
2866 /* Intel Baytrail and Braswell; with eld notifier */
2867 static int patch_i915_byt_hdmi(struct hda_codec *codec)
2869 struct hdmi_spec *spec;
2872 err = alloc_intel_hdmi(codec);
2877 /* For Valleyview/Cherryview, only the display codec is in the display
2878 * power well and can use link_power ops to request/release the power.
2880 codec->display_power_control = 1;
2882 codec->depop_delay = 0;
2883 codec->auto_runtime_pm = 1;
2885 spec->ops.pin_cvt_fixup = i915_pin_cvt_fixup;
2887 return parse_intel_hdmi(codec);
2890 /* Intel IronLake, SandyBridge and IvyBridge; with eld notifier */
2891 static int patch_i915_cpt_hdmi(struct hda_codec *codec)
2895 err = alloc_intel_hdmi(codec);
2898 return parse_intel_hdmi(codec);
2902 * Shared non-generic implementations
2905 static int simple_playback_build_pcms(struct hda_codec *codec)
2907 struct hdmi_spec *spec = codec->spec;
2908 struct hda_pcm *info;
2910 struct hda_pcm_stream *pstr;
2911 struct hdmi_spec_per_cvt *per_cvt;
2913 per_cvt = get_cvt(spec, 0);
2914 chans = get_wcaps(codec, per_cvt->cvt_nid);
2915 chans = get_wcaps_channels(chans);
2917 info = snd_hda_codec_pcm_new(codec, "HDMI 0");
2920 spec->pcm_rec[0].pcm = info;
2921 info->pcm_type = HDA_PCM_TYPE_HDMI;
2922 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2923 *pstr = spec->pcm_playback;
2924 pstr->nid = per_cvt->cvt_nid;
2925 if (pstr->channels_max <= 2 && chans && chans <= 16)
2926 pstr->channels_max = chans;
2931 /* unsolicited event for jack sensing */
2932 static void simple_hdmi_unsol_event(struct hda_codec *codec,
2935 snd_hda_jack_set_dirty_all(codec);
2936 snd_hda_jack_report_sync(codec);
2939 /* generic_hdmi_build_jack can be used for simple_hdmi, too,
2940 * as long as spec->pins[] is set correctly
2942 #define simple_hdmi_build_jack generic_hdmi_build_jack
2944 static int simple_playback_build_controls(struct hda_codec *codec)
2946 struct hdmi_spec *spec = codec->spec;
2947 struct hdmi_spec_per_cvt *per_cvt;
2950 per_cvt = get_cvt(spec, 0);
2951 err = snd_hda_create_dig_out_ctls(codec, per_cvt->cvt_nid,
2956 return simple_hdmi_build_jack(codec, 0);
2959 static int simple_playback_init(struct hda_codec *codec)
2961 struct hdmi_spec *spec = codec->spec;
2962 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
2963 hda_nid_t pin = per_pin->pin_nid;
2965 snd_hda_codec_write(codec, pin, 0,
2966 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
2967 /* some codecs require to unmute the pin */
2968 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
2969 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2971 snd_hda_jack_detect_enable(codec, pin);
2975 static void simple_playback_free(struct hda_codec *codec)
2977 struct hdmi_spec *spec = codec->spec;
2979 hdmi_array_free(spec);
2984 * Nvidia specific implementations
2987 #define Nv_VERB_SET_Channel_Allocation 0xF79
2988 #define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
2989 #define Nv_VERB_SET_Audio_Protection_On 0xF98
2990 #define Nv_VERB_SET_Audio_Protection_Off 0xF99
2992 #define nvhdmi_master_con_nid_7x 0x04
2993 #define nvhdmi_master_pin_nid_7x 0x05
2995 static const hda_nid_t nvhdmi_con_nids_7x[4] = {
2996 /*front, rear, clfe, rear_surr */
3000 static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
3001 /* set audio protect on */
3002 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
3003 /* enable digital output on pin widget */
3004 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3008 static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
3009 /* set audio protect on */
3010 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
3011 /* enable digital output on pin widget */
3012 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3013 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3014 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3015 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3016 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3020 #ifdef LIMITED_RATE_FMT_SUPPORT
3021 /* support only the safe format and rate */
3022 #define SUPPORTED_RATES SNDRV_PCM_RATE_48000
3023 #define SUPPORTED_MAXBPS 16
3024 #define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
3026 /* support all rates and formats */
3027 #define SUPPORTED_RATES \
3028 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
3029 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
3030 SNDRV_PCM_RATE_192000)
3031 #define SUPPORTED_MAXBPS 24
3032 #define SUPPORTED_FORMATS \
3033 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
3036 static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
3038 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
3042 static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
3044 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
3048 static const unsigned int channels_2_6_8[] = {
3052 static const unsigned int channels_2_8[] = {
3056 static const struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
3057 .count = ARRAY_SIZE(channels_2_6_8),
3058 .list = channels_2_6_8,
3062 static const struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
3063 .count = ARRAY_SIZE(channels_2_8),
3064 .list = channels_2_8,
3068 static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
3069 struct hda_codec *codec,
3070 struct snd_pcm_substream *substream)
3072 struct hdmi_spec *spec = codec->spec;
3073 const struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
3075 switch (codec->preset->vendor_id) {
3080 hw_constraints_channels = &hw_constraints_2_8_channels;
3083 hw_constraints_channels = &hw_constraints_2_6_8_channels;
3089 if (hw_constraints_channels != NULL) {
3090 snd_pcm_hw_constraint_list(substream->runtime, 0,
3091 SNDRV_PCM_HW_PARAM_CHANNELS,
3092 hw_constraints_channels);
3094 snd_pcm_hw_constraint_step(substream->runtime, 0,
3095 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3098 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3101 static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
3102 struct hda_codec *codec,
3103 struct snd_pcm_substream *substream)
3105 struct hdmi_spec *spec = codec->spec;
3106 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3109 static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3110 struct hda_codec *codec,
3111 unsigned int stream_tag,
3112 unsigned int format,
3113 struct snd_pcm_substream *substream)
3115 struct hdmi_spec *spec = codec->spec;
3116 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3117 stream_tag, format, substream);
3120 static const struct hda_pcm_stream simple_pcm_playback = {
3125 .open = simple_playback_pcm_open,
3126 .close = simple_playback_pcm_close,
3127 .prepare = simple_playback_pcm_prepare
3131 static const struct hda_codec_ops simple_hdmi_patch_ops = {
3132 .build_controls = simple_playback_build_controls,
3133 .build_pcms = simple_playback_build_pcms,
3134 .init = simple_playback_init,
3135 .free = simple_playback_free,
3136 .unsol_event = simple_hdmi_unsol_event,
3139 static int patch_simple_hdmi(struct hda_codec *codec,
3140 hda_nid_t cvt_nid, hda_nid_t pin_nid)
3142 struct hdmi_spec *spec;
3143 struct hdmi_spec_per_cvt *per_cvt;
3144 struct hdmi_spec_per_pin *per_pin;
3146 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3150 spec->codec = codec;
3152 hdmi_array_init(spec, 1);
3154 spec->multiout.num_dacs = 0; /* no analog */
3155 spec->multiout.max_channels = 2;
3156 spec->multiout.dig_out_nid = cvt_nid;
3159 per_pin = snd_array_new(&spec->pins);
3160 per_cvt = snd_array_new(&spec->cvts);
3161 if (!per_pin || !per_cvt) {
3162 simple_playback_free(codec);
3165 per_cvt->cvt_nid = cvt_nid;
3166 per_pin->pin_nid = pin_nid;
3167 spec->pcm_playback = simple_pcm_playback;
3169 codec->patch_ops = simple_hdmi_patch_ops;
3174 static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
3177 unsigned int chanmask;
3178 int chan = channels ? (channels - 1) : 1;
3197 /* Set the audio infoframe channel allocation and checksum fields. The
3198 * channel count is computed implicitly by the hardware. */
3199 snd_hda_codec_write(codec, 0x1, 0,
3200 Nv_VERB_SET_Channel_Allocation, chanmask);
3202 snd_hda_codec_write(codec, 0x1, 0,
3203 Nv_VERB_SET_Info_Frame_Checksum,
3204 (0x71 - chan - chanmask));
3207 static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
3208 struct hda_codec *codec,
3209 struct snd_pcm_substream *substream)
3211 struct hdmi_spec *spec = codec->spec;
3214 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
3215 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
3216 for (i = 0; i < 4; i++) {
3217 /* set the stream id */
3218 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3219 AC_VERB_SET_CHANNEL_STREAMID, 0);
3220 /* set the stream format */
3221 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3222 AC_VERB_SET_STREAM_FORMAT, 0);
3225 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
3226 * streams are disabled. */
3227 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3229 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3232 static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
3233 struct hda_codec *codec,
3234 unsigned int stream_tag,
3235 unsigned int format,
3236 struct snd_pcm_substream *substream)
3239 unsigned int dataDCC2, channel_id;
3241 struct hdmi_spec *spec = codec->spec;
3242 struct hda_spdif_out *spdif;
3243 struct hdmi_spec_per_cvt *per_cvt;
3245 mutex_lock(&codec->spdif_mutex);
3246 per_cvt = get_cvt(spec, 0);
3247 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
3249 chs = substream->runtime->channels;
3253 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
3254 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
3255 snd_hda_codec_write(codec,
3256 nvhdmi_master_con_nid_7x,
3258 AC_VERB_SET_DIGI_CONVERT_1,
3259 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
3261 /* set the stream id */
3262 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3263 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
3265 /* set the stream format */
3266 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3267 AC_VERB_SET_STREAM_FORMAT, format);
3269 /* turn on again (if needed) */
3270 /* enable and set the channel status audio/data flag */
3271 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
3272 snd_hda_codec_write(codec,
3273 nvhdmi_master_con_nid_7x,
3275 AC_VERB_SET_DIGI_CONVERT_1,
3276 spdif->ctls & 0xff);
3277 snd_hda_codec_write(codec,
3278 nvhdmi_master_con_nid_7x,
3280 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3283 for (i = 0; i < 4; i++) {
3289 /* turn off SPDIF once;
3290 *otherwise the IEC958 bits won't be updated
3292 if (codec->spdif_status_reset &&
3293 (spdif->ctls & AC_DIG1_ENABLE))
3294 snd_hda_codec_write(codec,
3295 nvhdmi_con_nids_7x[i],
3297 AC_VERB_SET_DIGI_CONVERT_1,
3298 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
3299 /* set the stream id */
3300 snd_hda_codec_write(codec,
3301 nvhdmi_con_nids_7x[i],
3303 AC_VERB_SET_CHANNEL_STREAMID,
3304 (stream_tag << 4) | channel_id);
3305 /* set the stream format */
3306 snd_hda_codec_write(codec,
3307 nvhdmi_con_nids_7x[i],
3309 AC_VERB_SET_STREAM_FORMAT,
3311 /* turn on again (if needed) */
3312 /* enable and set the channel status audio/data flag */
3313 if (codec->spdif_status_reset &&
3314 (spdif->ctls & AC_DIG1_ENABLE)) {
3315 snd_hda_codec_write(codec,
3316 nvhdmi_con_nids_7x[i],
3318 AC_VERB_SET_DIGI_CONVERT_1,
3319 spdif->ctls & 0xff);
3320 snd_hda_codec_write(codec,
3321 nvhdmi_con_nids_7x[i],
3323 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3327 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
3329 mutex_unlock(&codec->spdif_mutex);
3333 static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
3337 .nid = nvhdmi_master_con_nid_7x,
3338 .rates = SUPPORTED_RATES,
3339 .maxbps = SUPPORTED_MAXBPS,
3340 .formats = SUPPORTED_FORMATS,
3342 .open = simple_playback_pcm_open,
3343 .close = nvhdmi_8ch_7x_pcm_close,
3344 .prepare = nvhdmi_8ch_7x_pcm_prepare
3348 static int patch_nvhdmi_2ch(struct hda_codec *codec)
3350 struct hdmi_spec *spec;
3351 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
3352 nvhdmi_master_pin_nid_7x);
3356 codec->patch_ops.init = nvhdmi_7x_init_2ch;
3357 /* override the PCM rates, etc, as the codec doesn't give full list */
3359 spec->pcm_playback.rates = SUPPORTED_RATES;
3360 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
3361 spec->pcm_playback.formats = SUPPORTED_FORMATS;
3365 static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
3367 struct hdmi_spec *spec = codec->spec;
3368 int err = simple_playback_build_pcms(codec);
3370 struct hda_pcm *info = get_pcm_rec(spec, 0);
3371 info->own_chmap = true;
3376 static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
3378 struct hdmi_spec *spec = codec->spec;
3379 struct hda_pcm *info;
3380 struct snd_pcm_chmap *chmap;
3383 err = simple_playback_build_controls(codec);
3387 /* add channel maps */
3388 info = get_pcm_rec(spec, 0);
3389 err = snd_pcm_add_chmap_ctls(info->pcm,
3390 SNDRV_PCM_STREAM_PLAYBACK,
3391 snd_pcm_alt_chmaps, 8, 0, &chmap);
3394 switch (codec->preset->vendor_id) {
3399 chmap->channel_mask = (1U << 2) | (1U << 8);
3402 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
3407 static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
3409 struct hdmi_spec *spec;
3410 int err = patch_nvhdmi_2ch(codec);
3414 spec->multiout.max_channels = 8;
3415 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
3416 codec->patch_ops.init = nvhdmi_7x_init_8ch;
3417 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
3418 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
3420 /* Initialize the audio infoframe channel mask and checksum to something
3422 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3428 * NVIDIA codecs ignore ASP mapping for 2ch - confirmed on:
3432 static int nvhdmi_chmap_cea_alloc_validate_get_type(struct hdac_chmap *chmap,
3433 struct hdac_cea_channel_speaker_allocation *cap, int channels)
3435 if (cap->ca_index == 0x00 && channels == 2)
3436 return SNDRV_CTL_TLVT_CHMAP_FIXED;
3438 /* If the speaker allocation matches the channel count, it is OK. */
3439 if (cap->channels != channels)
3442 /* all channels are remappable freely */
3443 return SNDRV_CTL_TLVT_CHMAP_VAR;
3446 static int nvhdmi_chmap_validate(struct hdac_chmap *chmap,
3447 int ca, int chs, unsigned char *map)
3449 if (ca == 0x00 && (map[0] != SNDRV_CHMAP_FL || map[1] != SNDRV_CHMAP_FR))
3455 /* map from pin NID to port; port is 0-based */
3456 /* for Nvidia: assume widget NID starting from 4, with step 1 (4, 5, 6, ...) */
3457 static int nvhdmi_pin2port(void *audio_ptr, int pin_nid)
3462 /* reverse-map from port to pin NID: see above */
3463 static int nvhdmi_port2pin(struct hda_codec *codec, int port)
3468 static const struct drm_audio_component_audio_ops nvhdmi_audio_ops = {
3469 .pin2port = nvhdmi_pin2port,
3470 .pin_eld_notify = generic_acomp_pin_eld_notify,
3471 .master_bind = generic_acomp_master_bind,
3472 .master_unbind = generic_acomp_master_unbind,
3475 static int patch_nvhdmi(struct hda_codec *codec)
3477 struct hdmi_spec *spec;
3480 err = patch_generic_hdmi(codec);
3485 spec->dyn_pin_out = true;
3487 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
3488 nvhdmi_chmap_cea_alloc_validate_get_type;
3489 spec->chmap.ops.chmap_validate = nvhdmi_chmap_validate;
3491 codec->link_down_at_suspend = 1;
3493 generic_acomp_init(codec, &nvhdmi_audio_ops, nvhdmi_port2pin);
3499 * The HDA codec on NVIDIA Tegra contains two scratch registers that are
3500 * accessed using vendor-defined verbs. These registers can be used for
3501 * interoperability between the HDA and HDMI drivers.
3504 /* Audio Function Group node */
3505 #define NVIDIA_AFG_NID 0x01
3508 * The SCRATCH0 register is used to notify the HDMI codec of changes in audio
3509 * format. On Tegra, bit 31 is used as a trigger that causes an interrupt to
3510 * be raised in the HDMI codec. The remainder of the bits is arbitrary. This
3511 * implementation stores the HDA format (see AC_FMT_*) in bits [15:0] and an
3512 * additional bit (at position 30) to signal the validity of the format.
3514 * | 31 | 30 | 29 16 | 15 0 |
3515 * +---------+-------+--------+--------+
3516 * | TRIGGER | VALID | UNUSED | FORMAT |
3517 * +-----------------------------------|
3519 * Note that for the trigger bit to take effect it needs to change value
3520 * (i.e. it needs to be toggled).
3522 #define NVIDIA_GET_SCRATCH0 0xfa6
3523 #define NVIDIA_SET_SCRATCH0_BYTE0 0xfa7
3524 #define NVIDIA_SET_SCRATCH0_BYTE1 0xfa8
3525 #define NVIDIA_SET_SCRATCH0_BYTE2 0xfa9
3526 #define NVIDIA_SET_SCRATCH0_BYTE3 0xfaa
3527 #define NVIDIA_SCRATCH_TRIGGER (1 << 7)
3528 #define NVIDIA_SCRATCH_VALID (1 << 6)
3530 #define NVIDIA_GET_SCRATCH1 0xfab
3531 #define NVIDIA_SET_SCRATCH1_BYTE0 0xfac
3532 #define NVIDIA_SET_SCRATCH1_BYTE1 0xfad
3533 #define NVIDIA_SET_SCRATCH1_BYTE2 0xfae
3534 #define NVIDIA_SET_SCRATCH1_BYTE3 0xfaf
3537 * The format parameter is the HDA audio format (see AC_FMT_*). If set to 0,
3538 * the format is invalidated so that the HDMI codec can be disabled.
3540 static void tegra_hdmi_set_format(struct hda_codec *codec, unsigned int format)
3544 /* bits [31:30] contain the trigger and valid bits */
3545 value = snd_hda_codec_read(codec, NVIDIA_AFG_NID, 0,
3546 NVIDIA_GET_SCRATCH0, 0);
3547 value = (value >> 24) & 0xff;
3549 /* bits [15:0] are used to store the HDA format */
3550 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3551 NVIDIA_SET_SCRATCH0_BYTE0,
3552 (format >> 0) & 0xff);
3553 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3554 NVIDIA_SET_SCRATCH0_BYTE1,
3555 (format >> 8) & 0xff);
3557 /* bits [16:24] are unused */
3558 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3559 NVIDIA_SET_SCRATCH0_BYTE2, 0);
3562 * Bit 30 signals that the data is valid and hence that HDMI audio can
3566 value &= ~NVIDIA_SCRATCH_VALID;
3568 value |= NVIDIA_SCRATCH_VALID;
3571 * Whenever the trigger bit is toggled, an interrupt is raised in the
3572 * HDMI codec. The HDMI driver will use that as trigger to update its
3575 value ^= NVIDIA_SCRATCH_TRIGGER;
3577 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3578 NVIDIA_SET_SCRATCH0_BYTE3, value);
3581 static int tegra_hdmi_pcm_prepare(struct hda_pcm_stream *hinfo,
3582 struct hda_codec *codec,
3583 unsigned int stream_tag,
3584 unsigned int format,
3585 struct snd_pcm_substream *substream)
3589 err = generic_hdmi_playback_pcm_prepare(hinfo, codec, stream_tag,
3594 /* notify the HDMI codec of the format change */
3595 tegra_hdmi_set_format(codec, format);
3600 static int tegra_hdmi_pcm_cleanup(struct hda_pcm_stream *hinfo,
3601 struct hda_codec *codec,
3602 struct snd_pcm_substream *substream)
3604 /* invalidate the format in the HDMI codec */
3605 tegra_hdmi_set_format(codec, 0);
3607 return generic_hdmi_playback_pcm_cleanup(hinfo, codec, substream);
3610 static struct hda_pcm *hda_find_pcm_by_type(struct hda_codec *codec, int type)
3612 struct hdmi_spec *spec = codec->spec;
3615 for (i = 0; i < spec->num_pins; i++) {
3616 struct hda_pcm *pcm = get_pcm_rec(spec, i);
3618 if (pcm->pcm_type == type)
3625 static int tegra_hdmi_build_pcms(struct hda_codec *codec)
3627 struct hda_pcm_stream *stream;
3628 struct hda_pcm *pcm;
3631 err = generic_hdmi_build_pcms(codec);
3635 pcm = hda_find_pcm_by_type(codec, HDA_PCM_TYPE_HDMI);
3640 * Override ->prepare() and ->cleanup() operations to notify the HDMI
3641 * codec about format changes.
3643 stream = &pcm->stream[SNDRV_PCM_STREAM_PLAYBACK];
3644 stream->ops.prepare = tegra_hdmi_pcm_prepare;
3645 stream->ops.cleanup = tegra_hdmi_pcm_cleanup;
3650 static int patch_tegra_hdmi(struct hda_codec *codec)
3654 err = patch_generic_hdmi(codec);
3658 codec->patch_ops.build_pcms = tegra_hdmi_build_pcms;
3664 * ATI/AMD-specific implementations
3667 #define is_amdhdmi_rev3_or_later(codec) \
3668 ((codec)->core.vendor_id == 0x1002aa01 && \
3669 ((codec)->core.revision_id & 0xff00) >= 0x0300)
3670 #define has_amd_full_remap_support(codec) is_amdhdmi_rev3_or_later(codec)
3672 /* ATI/AMD specific HDA pin verbs, see the AMD HDA Verbs specification */
3673 #define ATI_VERB_SET_CHANNEL_ALLOCATION 0x771
3674 #define ATI_VERB_SET_DOWNMIX_INFO 0x772
3675 #define ATI_VERB_SET_MULTICHANNEL_01 0x777
3676 #define ATI_VERB_SET_MULTICHANNEL_23 0x778
3677 #define ATI_VERB_SET_MULTICHANNEL_45 0x779
3678 #define ATI_VERB_SET_MULTICHANNEL_67 0x77a
3679 #define ATI_VERB_SET_HBR_CONTROL 0x77c
3680 #define ATI_VERB_SET_MULTICHANNEL_1 0x785
3681 #define ATI_VERB_SET_MULTICHANNEL_3 0x786
3682 #define ATI_VERB_SET_MULTICHANNEL_5 0x787
3683 #define ATI_VERB_SET_MULTICHANNEL_7 0x788
3684 #define ATI_VERB_SET_MULTICHANNEL_MODE 0x789
3685 #define ATI_VERB_GET_CHANNEL_ALLOCATION 0xf71
3686 #define ATI_VERB_GET_DOWNMIX_INFO 0xf72
3687 #define ATI_VERB_GET_MULTICHANNEL_01 0xf77
3688 #define ATI_VERB_GET_MULTICHANNEL_23 0xf78
3689 #define ATI_VERB_GET_MULTICHANNEL_45 0xf79
3690 #define ATI_VERB_GET_MULTICHANNEL_67 0xf7a
3691 #define ATI_VERB_GET_HBR_CONTROL 0xf7c
3692 #define ATI_VERB_GET_MULTICHANNEL_1 0xf85
3693 #define ATI_VERB_GET_MULTICHANNEL_3 0xf86
3694 #define ATI_VERB_GET_MULTICHANNEL_5 0xf87
3695 #define ATI_VERB_GET_MULTICHANNEL_7 0xf88
3696 #define ATI_VERB_GET_MULTICHANNEL_MODE 0xf89
3698 /* AMD specific HDA cvt verbs */
3699 #define ATI_VERB_SET_RAMP_RATE 0x770
3700 #define ATI_VERB_GET_RAMP_RATE 0xf70
3702 #define ATI_OUT_ENABLE 0x1
3704 #define ATI_MULTICHANNEL_MODE_PAIRED 0
3705 #define ATI_MULTICHANNEL_MODE_SINGLE 1
3707 #define ATI_HBR_CAPABLE 0x01
3708 #define ATI_HBR_ENABLE 0x10
3710 static int atihdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
3711 unsigned char *buf, int *eld_size)
3713 /* call hda_eld.c ATI/AMD-specific function */
3714 return snd_hdmi_get_eld_ati(codec, nid, buf, eld_size,
3715 is_amdhdmi_rev3_or_later(codec));
3718 static void atihdmi_pin_setup_infoframe(struct hda_codec *codec, hda_nid_t pin_nid, int ca,
3719 int active_channels, int conn_type)
3721 snd_hda_codec_write(codec, pin_nid, 0, ATI_VERB_SET_CHANNEL_ALLOCATION, ca);
3724 static int atihdmi_paired_swap_fc_lfe(int pos)
3727 * ATI/AMD have automatic FC/LFE swap built-in
3728 * when in pairwise mapping mode.
3732 /* see channel_allocations[].speakers[] */
3741 static int atihdmi_paired_chmap_validate(struct hdac_chmap *chmap,
3742 int ca, int chs, unsigned char *map)
3744 struct hdac_cea_channel_speaker_allocation *cap;
3747 /* check that only channel pairs need to be remapped on old pre-rev3 ATI/AMD */
3749 cap = snd_hdac_get_ch_alloc_from_ca(ca);
3750 for (i = 0; i < chs; ++i) {
3751 int mask = snd_hdac_chmap_to_spk_mask(map[i]);
3753 bool companion_ok = false;
3758 for (j = 0 + i % 2; j < 8; j += 2) {
3759 int chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j);
3760 if (cap->speakers[chan_idx] == mask) {
3761 /* channel is in a supported position */
3764 if (i % 2 == 0 && i + 1 < chs) {
3765 /* even channel, check the odd companion */
3766 int comp_chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j + 1);
3767 int comp_mask_req = snd_hdac_chmap_to_spk_mask(map[i+1]);
3768 int comp_mask_act = cap->speakers[comp_chan_idx];
3770 if (comp_mask_req == comp_mask_act)
3771 companion_ok = true;
3783 i++; /* companion channel already checked */
3789 static int atihdmi_pin_set_slot_channel(struct hdac_device *hdac,
3790 hda_nid_t pin_nid, int hdmi_slot, int stream_channel)
3792 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
3794 int ati_channel_setup = 0;
3799 if (!has_amd_full_remap_support(codec)) {
3800 hdmi_slot = atihdmi_paired_swap_fc_lfe(hdmi_slot);
3802 /* In case this is an odd slot but without stream channel, do not
3803 * disable the slot since the corresponding even slot could have a
3804 * channel. In case neither have a channel, the slot pair will be
3805 * disabled when this function is called for the even slot. */
3806 if (hdmi_slot % 2 != 0 && stream_channel == 0xf)
3809 hdmi_slot -= hdmi_slot % 2;
3811 if (stream_channel != 0xf)
3812 stream_channel -= stream_channel % 2;
3815 verb = ATI_VERB_SET_MULTICHANNEL_01 + hdmi_slot/2 + (hdmi_slot % 2) * 0x00e;
3817 /* ati_channel_setup format: [7..4] = stream_channel_id, [1] = mute, [0] = enable */
3819 if (stream_channel != 0xf)
3820 ati_channel_setup = (stream_channel << 4) | ATI_OUT_ENABLE;
3822 return snd_hda_codec_write(codec, pin_nid, 0, verb, ati_channel_setup);
3825 static int atihdmi_pin_get_slot_channel(struct hdac_device *hdac,
3826 hda_nid_t pin_nid, int asp_slot)
3828 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
3829 bool was_odd = false;
3830 int ati_asp_slot = asp_slot;
3832 int ati_channel_setup;
3837 if (!has_amd_full_remap_support(codec)) {
3838 ati_asp_slot = atihdmi_paired_swap_fc_lfe(asp_slot);
3839 if (ati_asp_slot % 2 != 0) {
3845 verb = ATI_VERB_GET_MULTICHANNEL_01 + ati_asp_slot/2 + (ati_asp_slot % 2) * 0x00e;
3847 ati_channel_setup = snd_hda_codec_read(codec, pin_nid, 0, verb, 0);
3849 if (!(ati_channel_setup & ATI_OUT_ENABLE))
3852 return ((ati_channel_setup & 0xf0) >> 4) + !!was_odd;
3855 static int atihdmi_paired_chmap_cea_alloc_validate_get_type(
3856 struct hdac_chmap *chmap,
3857 struct hdac_cea_channel_speaker_allocation *cap,
3863 * Pre-rev3 ATI/AMD codecs operate in a paired channel mode, so
3864 * we need to take that into account (a single channel may take 2
3865 * channel slots if we need to carry a silent channel next to it).
3866 * On Rev3+ AMD codecs this function is not used.
3870 /* We only produce even-numbered channel count TLVs */
3871 if ((channels % 2) != 0)
3874 for (c = 0; c < 7; c += 2) {
3875 if (cap->speakers[c] || cap->speakers[c+1])
3879 if (chanpairs * 2 != channels)
3882 return SNDRV_CTL_TLVT_CHMAP_PAIRED;
3885 static void atihdmi_paired_cea_alloc_to_tlv_chmap(struct hdac_chmap *hchmap,
3886 struct hdac_cea_channel_speaker_allocation *cap,
3887 unsigned int *chmap, int channels)
3889 /* produce paired maps for pre-rev3 ATI/AMD codecs */
3893 for (c = 7; c >= 0; c--) {
3894 int chan = 7 - atihdmi_paired_swap_fc_lfe(7 - c);
3895 int spk = cap->speakers[chan];
3897 /* add N/A channel if the companion channel is occupied */
3898 if (cap->speakers[chan + (chan % 2 ? -1 : 1)])
3899 chmap[count++] = SNDRV_CHMAP_NA;
3904 chmap[count++] = snd_hdac_spk_to_chmap(spk);
3907 WARN_ON(count != channels);
3910 static int atihdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
3913 int hbr_ctl, hbr_ctl_new;
3915 hbr_ctl = snd_hda_codec_read(codec, pin_nid, 0, ATI_VERB_GET_HBR_CONTROL, 0);
3916 if (hbr_ctl >= 0 && (hbr_ctl & ATI_HBR_CAPABLE)) {
3918 hbr_ctl_new = hbr_ctl | ATI_HBR_ENABLE;
3920 hbr_ctl_new = hbr_ctl & ~ATI_HBR_ENABLE;
3923 "atihdmi_pin_hbr_setup: NID=0x%x, %shbr-ctl=0x%x\n",
3925 hbr_ctl == hbr_ctl_new ? "" : "new-",
3928 if (hbr_ctl != hbr_ctl_new)
3929 snd_hda_codec_write(codec, pin_nid, 0,
3930 ATI_VERB_SET_HBR_CONTROL,
3939 static int atihdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
3940 hda_nid_t pin_nid, u32 stream_tag, int format)
3943 if (is_amdhdmi_rev3_or_later(codec)) {
3944 int ramp_rate = 180; /* default as per AMD spec */
3945 /* disable ramp-up/down for non-pcm as per AMD spec */
3946 if (format & AC_FMT_TYPE_NON_PCM)
3949 snd_hda_codec_write(codec, cvt_nid, 0, ATI_VERB_SET_RAMP_RATE, ramp_rate);
3952 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
3956 static int atihdmi_init(struct hda_codec *codec)
3958 struct hdmi_spec *spec = codec->spec;
3961 err = generic_hdmi_init(codec);
3966 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
3967 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
3969 /* make sure downmix information in infoframe is zero */
3970 snd_hda_codec_write(codec, per_pin->pin_nid, 0, ATI_VERB_SET_DOWNMIX_INFO, 0);
3972 /* enable channel-wise remap mode if supported */
3973 if (has_amd_full_remap_support(codec))
3974 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
3975 ATI_VERB_SET_MULTICHANNEL_MODE,
3976 ATI_MULTICHANNEL_MODE_SINGLE);
3982 /* map from pin NID to port; port is 0-based */
3983 /* for AMD: assume widget NID starting from 3, with step 2 (3, 5, 7, ...) */
3984 static int atihdmi_pin2port(void *audio_ptr, int pin_nid)
3986 return pin_nid / 2 - 1;
3989 /* reverse-map from port to pin NID: see above */
3990 static int atihdmi_port2pin(struct hda_codec *codec, int port)
3992 return port * 2 + 3;
3995 static const struct drm_audio_component_audio_ops atihdmi_audio_ops = {
3996 .pin2port = atihdmi_pin2port,
3997 .pin_eld_notify = generic_acomp_pin_eld_notify,
3998 .master_bind = generic_acomp_master_bind,
3999 .master_unbind = generic_acomp_master_unbind,
4002 static int patch_atihdmi(struct hda_codec *codec)
4004 struct hdmi_spec *spec;
4005 struct hdmi_spec_per_cvt *per_cvt;
4008 err = patch_generic_hdmi(codec);
4013 codec->patch_ops.init = atihdmi_init;
4017 spec->ops.pin_get_eld = atihdmi_pin_get_eld;
4018 spec->ops.pin_setup_infoframe = atihdmi_pin_setup_infoframe;
4019 spec->ops.pin_hbr_setup = atihdmi_pin_hbr_setup;
4020 spec->ops.setup_stream = atihdmi_setup_stream;
4022 spec->chmap.ops.pin_get_slot_channel = atihdmi_pin_get_slot_channel;
4023 spec->chmap.ops.pin_set_slot_channel = atihdmi_pin_set_slot_channel;
4025 if (!has_amd_full_remap_support(codec)) {
4026 /* override to ATI/AMD-specific versions with pairwise mapping */
4027 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
4028 atihdmi_paired_chmap_cea_alloc_validate_get_type;
4029 spec->chmap.ops.cea_alloc_to_tlv_chmap =
4030 atihdmi_paired_cea_alloc_to_tlv_chmap;
4031 spec->chmap.ops.chmap_validate = atihdmi_paired_chmap_validate;
4034 /* ATI/AMD converters do not advertise all of their capabilities */
4035 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
4036 per_cvt = get_cvt(spec, cvt_idx);
4037 per_cvt->channels_max = max(per_cvt->channels_max, 8u);
4038 per_cvt->rates |= SUPPORTED_RATES;
4039 per_cvt->formats |= SUPPORTED_FORMATS;
4040 per_cvt->maxbps = max(per_cvt->maxbps, 24u);
4043 spec->chmap.channels_max = max(spec->chmap.channels_max, 8u);
4045 /* AMD GPUs have neither EPSS nor CLKSTOP bits, hence preventing
4046 * the link-down as is. Tell the core to allow it.
4048 codec->link_down_at_suspend = 1;
4050 generic_acomp_init(codec, &atihdmi_audio_ops, atihdmi_port2pin);
4055 /* VIA HDMI Implementation */
4056 #define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
4057 #define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
4059 static int patch_via_hdmi(struct hda_codec *codec)
4061 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
4067 static const struct hda_device_id snd_hda_id_hdmi[] = {
4068 HDA_CODEC_ENTRY(0x1002793c, "RS600 HDMI", patch_atihdmi),
4069 HDA_CODEC_ENTRY(0x10027919, "RS600 HDMI", patch_atihdmi),
4070 HDA_CODEC_ENTRY(0x1002791a, "RS690/780 HDMI", patch_atihdmi),
4071 HDA_CODEC_ENTRY(0x1002aa01, "R6xx HDMI", patch_atihdmi),
4072 HDA_CODEC_ENTRY(0x10951390, "SiI1390 HDMI", patch_generic_hdmi),
4073 HDA_CODEC_ENTRY(0x10951392, "SiI1392 HDMI", patch_generic_hdmi),
4074 HDA_CODEC_ENTRY(0x17e80047, "Chrontel HDMI", patch_generic_hdmi),
4075 HDA_CODEC_ENTRY(0x10de0001, "MCP73 HDMI", patch_nvhdmi_2ch),
4076 HDA_CODEC_ENTRY(0x10de0002, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4077 HDA_CODEC_ENTRY(0x10de0003, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4078 HDA_CODEC_ENTRY(0x10de0004, "GPU 04 HDMI", patch_nvhdmi_8ch_7x),
4079 HDA_CODEC_ENTRY(0x10de0005, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4080 HDA_CODEC_ENTRY(0x10de0006, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4081 HDA_CODEC_ENTRY(0x10de0007, "MCP79/7A HDMI", patch_nvhdmi_8ch_7x),
4082 HDA_CODEC_ENTRY(0x10de0008, "GPU 08 HDMI/DP", patch_nvhdmi),
4083 HDA_CODEC_ENTRY(0x10de0009, "GPU 09 HDMI/DP", patch_nvhdmi),
4084 HDA_CODEC_ENTRY(0x10de000a, "GPU 0a HDMI/DP", patch_nvhdmi),
4085 HDA_CODEC_ENTRY(0x10de000b, "GPU 0b HDMI/DP", patch_nvhdmi),
4086 HDA_CODEC_ENTRY(0x10de000c, "MCP89 HDMI", patch_nvhdmi),
4087 HDA_CODEC_ENTRY(0x10de000d, "GPU 0d HDMI/DP", patch_nvhdmi),
4088 HDA_CODEC_ENTRY(0x10de0010, "GPU 10 HDMI/DP", patch_nvhdmi),
4089 HDA_CODEC_ENTRY(0x10de0011, "GPU 11 HDMI/DP", patch_nvhdmi),
4090 HDA_CODEC_ENTRY(0x10de0012, "GPU 12 HDMI/DP", patch_nvhdmi),
4091 HDA_CODEC_ENTRY(0x10de0013, "GPU 13 HDMI/DP", patch_nvhdmi),
4092 HDA_CODEC_ENTRY(0x10de0014, "GPU 14 HDMI/DP", patch_nvhdmi),
4093 HDA_CODEC_ENTRY(0x10de0015, "GPU 15 HDMI/DP", patch_nvhdmi),
4094 HDA_CODEC_ENTRY(0x10de0016, "GPU 16 HDMI/DP", patch_nvhdmi),
4095 /* 17 is known to be absent */
4096 HDA_CODEC_ENTRY(0x10de0018, "GPU 18 HDMI/DP", patch_nvhdmi),
4097 HDA_CODEC_ENTRY(0x10de0019, "GPU 19 HDMI/DP", patch_nvhdmi),
4098 HDA_CODEC_ENTRY(0x10de001a, "GPU 1a HDMI/DP", patch_nvhdmi),
4099 HDA_CODEC_ENTRY(0x10de001b, "GPU 1b HDMI/DP", patch_nvhdmi),
4100 HDA_CODEC_ENTRY(0x10de001c, "GPU 1c HDMI/DP", patch_nvhdmi),
4101 HDA_CODEC_ENTRY(0x10de0020, "Tegra30 HDMI", patch_tegra_hdmi),
4102 HDA_CODEC_ENTRY(0x10de0022, "Tegra114 HDMI", patch_tegra_hdmi),
4103 HDA_CODEC_ENTRY(0x10de0028, "Tegra124 HDMI", patch_tegra_hdmi),
4104 HDA_CODEC_ENTRY(0x10de0029, "Tegra210 HDMI/DP", patch_tegra_hdmi),
4105 HDA_CODEC_ENTRY(0x10de002d, "Tegra186 HDMI/DP0", patch_tegra_hdmi),
4106 HDA_CODEC_ENTRY(0x10de002e, "Tegra186 HDMI/DP1", patch_tegra_hdmi),
4107 HDA_CODEC_ENTRY(0x10de002f, "Tegra194 HDMI/DP2", patch_tegra_hdmi),
4108 HDA_CODEC_ENTRY(0x10de0030, "Tegra194 HDMI/DP3", patch_tegra_hdmi),
4109 HDA_CODEC_ENTRY(0x10de0040, "GPU 40 HDMI/DP", patch_nvhdmi),
4110 HDA_CODEC_ENTRY(0x10de0041, "GPU 41 HDMI/DP", patch_nvhdmi),
4111 HDA_CODEC_ENTRY(0x10de0042, "GPU 42 HDMI/DP", patch_nvhdmi),
4112 HDA_CODEC_ENTRY(0x10de0043, "GPU 43 HDMI/DP", patch_nvhdmi),
4113 HDA_CODEC_ENTRY(0x10de0044, "GPU 44 HDMI/DP", patch_nvhdmi),
4114 HDA_CODEC_ENTRY(0x10de0045, "GPU 45 HDMI/DP", patch_nvhdmi),
4115 HDA_CODEC_ENTRY(0x10de0050, "GPU 50 HDMI/DP", patch_nvhdmi),
4116 HDA_CODEC_ENTRY(0x10de0051, "GPU 51 HDMI/DP", patch_nvhdmi),
4117 HDA_CODEC_ENTRY(0x10de0052, "GPU 52 HDMI/DP", patch_nvhdmi),
4118 HDA_CODEC_ENTRY(0x10de0060, "GPU 60 HDMI/DP", patch_nvhdmi),
4119 HDA_CODEC_ENTRY(0x10de0061, "GPU 61 HDMI/DP", patch_nvhdmi),
4120 HDA_CODEC_ENTRY(0x10de0062, "GPU 62 HDMI/DP", patch_nvhdmi),
4121 HDA_CODEC_ENTRY(0x10de0067, "MCP67 HDMI", patch_nvhdmi_2ch),
4122 HDA_CODEC_ENTRY(0x10de0070, "GPU 70 HDMI/DP", patch_nvhdmi),
4123 HDA_CODEC_ENTRY(0x10de0071, "GPU 71 HDMI/DP", patch_nvhdmi),
4124 HDA_CODEC_ENTRY(0x10de0072, "GPU 72 HDMI/DP", patch_nvhdmi),
4125 HDA_CODEC_ENTRY(0x10de0073, "GPU 73 HDMI/DP", patch_nvhdmi),
4126 HDA_CODEC_ENTRY(0x10de0074, "GPU 74 HDMI/DP", patch_nvhdmi),
4127 HDA_CODEC_ENTRY(0x10de0076, "GPU 76 HDMI/DP", patch_nvhdmi),
4128 HDA_CODEC_ENTRY(0x10de007b, "GPU 7b HDMI/DP", patch_nvhdmi),
4129 HDA_CODEC_ENTRY(0x10de007c, "GPU 7c HDMI/DP", patch_nvhdmi),
4130 HDA_CODEC_ENTRY(0x10de007d, "GPU 7d HDMI/DP", patch_nvhdmi),
4131 HDA_CODEC_ENTRY(0x10de007e, "GPU 7e HDMI/DP", patch_nvhdmi),
4132 HDA_CODEC_ENTRY(0x10de0080, "GPU 80 HDMI/DP", patch_nvhdmi),
4133 HDA_CODEC_ENTRY(0x10de0081, "GPU 81 HDMI/DP", patch_nvhdmi),
4134 HDA_CODEC_ENTRY(0x10de0082, "GPU 82 HDMI/DP", patch_nvhdmi),
4135 HDA_CODEC_ENTRY(0x10de0083, "GPU 83 HDMI/DP", patch_nvhdmi),
4136 HDA_CODEC_ENTRY(0x10de0084, "GPU 84 HDMI/DP", patch_nvhdmi),
4137 HDA_CODEC_ENTRY(0x10de0090, "GPU 90 HDMI/DP", patch_nvhdmi),
4138 HDA_CODEC_ENTRY(0x10de0091, "GPU 91 HDMI/DP", patch_nvhdmi),
4139 HDA_CODEC_ENTRY(0x10de0092, "GPU 92 HDMI/DP", patch_nvhdmi),
4140 HDA_CODEC_ENTRY(0x10de0093, "GPU 93 HDMI/DP", patch_nvhdmi),
4141 HDA_CODEC_ENTRY(0x10de0094, "GPU 94 HDMI/DP", patch_nvhdmi),
4142 HDA_CODEC_ENTRY(0x10de0095, "GPU 95 HDMI/DP", patch_nvhdmi),
4143 HDA_CODEC_ENTRY(0x10de0097, "GPU 97 HDMI/DP", patch_nvhdmi),
4144 HDA_CODEC_ENTRY(0x10de0098, "GPU 98 HDMI/DP", patch_nvhdmi),
4145 HDA_CODEC_ENTRY(0x10de0099, "GPU 99 HDMI/DP", patch_nvhdmi),
4146 HDA_CODEC_ENTRY(0x10de8001, "MCP73 HDMI", patch_nvhdmi_2ch),
4147 HDA_CODEC_ENTRY(0x10de8067, "MCP67/68 HDMI", patch_nvhdmi_2ch),
4148 HDA_CODEC_ENTRY(0x11069f80, "VX900 HDMI/DP", patch_via_hdmi),
4149 HDA_CODEC_ENTRY(0x11069f81, "VX900 HDMI/DP", patch_via_hdmi),
4150 HDA_CODEC_ENTRY(0x11069f84, "VX11 HDMI/DP", patch_generic_hdmi),
4151 HDA_CODEC_ENTRY(0x11069f85, "VX11 HDMI/DP", patch_generic_hdmi),
4152 HDA_CODEC_ENTRY(0x80860054, "IbexPeak HDMI", patch_i915_cpt_hdmi),
4153 HDA_CODEC_ENTRY(0x80862800, "Geminilake HDMI", patch_i915_glk_hdmi),
4154 HDA_CODEC_ENTRY(0x80862801, "Bearlake HDMI", patch_generic_hdmi),
4155 HDA_CODEC_ENTRY(0x80862802, "Cantiga HDMI", patch_generic_hdmi),
4156 HDA_CODEC_ENTRY(0x80862803, "Eaglelake HDMI", patch_generic_hdmi),
4157 HDA_CODEC_ENTRY(0x80862804, "IbexPeak HDMI", patch_i915_cpt_hdmi),
4158 HDA_CODEC_ENTRY(0x80862805, "CougarPoint HDMI", patch_i915_cpt_hdmi),
4159 HDA_CODEC_ENTRY(0x80862806, "PantherPoint HDMI", patch_i915_cpt_hdmi),
4160 HDA_CODEC_ENTRY(0x80862807, "Haswell HDMI", patch_i915_hsw_hdmi),
4161 HDA_CODEC_ENTRY(0x80862808, "Broadwell HDMI", patch_i915_hsw_hdmi),
4162 HDA_CODEC_ENTRY(0x80862809, "Skylake HDMI", patch_i915_hsw_hdmi),
4163 HDA_CODEC_ENTRY(0x8086280a, "Broxton HDMI", patch_i915_hsw_hdmi),
4164 HDA_CODEC_ENTRY(0x8086280b, "Kabylake HDMI", patch_i915_hsw_hdmi),
4165 HDA_CODEC_ENTRY(0x8086280c, "Cannonlake HDMI", patch_i915_glk_hdmi),
4166 HDA_CODEC_ENTRY(0x8086280d, "Geminilake HDMI", patch_i915_glk_hdmi),
4167 HDA_CODEC_ENTRY(0x8086280f, "Icelake HDMI", patch_i915_icl_hdmi),
4168 HDA_CODEC_ENTRY(0x80862812, "Tigerlake HDMI", patch_i915_tgl_hdmi),
4169 HDA_CODEC_ENTRY(0x80862880, "CedarTrail HDMI", patch_generic_hdmi),
4170 HDA_CODEC_ENTRY(0x80862882, "Valleyview2 HDMI", patch_i915_byt_hdmi),
4171 HDA_CODEC_ENTRY(0x80862883, "Braswell HDMI", patch_i915_byt_hdmi),
4172 HDA_CODEC_ENTRY(0x808629fb, "Crestline HDMI", patch_generic_hdmi),
4173 /* special ID for generic HDMI */
4174 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC_HDMI, "Generic HDMI", patch_generic_hdmi),
4177 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_hdmi);
4179 MODULE_LICENSE("GPL");
4180 MODULE_DESCRIPTION("HDMI HD-audio codec");
4181 MODULE_ALIAS("snd-hda-codec-intelhdmi");
4182 MODULE_ALIAS("snd-hda-codec-nvhdmi");
4183 MODULE_ALIAS("snd-hda-codec-atihdmi");
4185 static struct hda_codec_driver hdmi_driver = {
4186 .id = snd_hda_id_hdmi,
4189 module_hda_codec_driver(hdmi_driver);