3 * patch_hdmi.c - routines for HDMI/DisplayPort codecs
5 * Copyright(c) 2008-2010 Intel Corporation. All rights reserved.
6 * Copyright (c) 2006 ATI Technologies Inc.
7 * Copyright (c) 2008 NVIDIA Corp. All rights reserved.
8 * Copyright (c) 2008 Wei Ni <wni@nvidia.com>
9 * Copyright (c) 2013 Anssi Hannula <anssi.hannula@iki.fi>
12 * Wu Fengguang <wfg@linux.intel.com>
15 * Wu Fengguang <wfg@linux.intel.com>
17 * This program is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License as published by the Free
19 * Software Foundation; either version 2 of the License, or (at your option)
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software Foundation,
29 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 #include <linux/init.h>
33 #include <linux/delay.h>
34 #include <linux/slab.h>
35 #include <linux/module.h>
36 #include <linux/pm_runtime.h>
37 #include <sound/core.h>
38 #include <sound/jack.h>
39 #include <sound/asoundef.h>
40 #include <sound/tlv.h>
41 #include <sound/hdaudio.h>
42 #include <sound/hda_i915.h>
43 #include <sound/hda_chmap.h>
44 #include <sound/hda_codec.h>
45 #include "hda_local.h"
48 static bool static_hdmi_pcm;
49 module_param(static_hdmi_pcm, bool, 0644);
50 MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info");
52 #define is_haswell(codec) ((codec)->core.vendor_id == 0x80862807)
53 #define is_broadwell(codec) ((codec)->core.vendor_id == 0x80862808)
54 #define is_skylake(codec) ((codec)->core.vendor_id == 0x80862809)
55 #define is_broxton(codec) ((codec)->core.vendor_id == 0x8086280a)
56 #define is_kabylake(codec) ((codec)->core.vendor_id == 0x8086280b)
57 #define is_geminilake(codec) (((codec)->core.vendor_id == 0x8086280d) || \
58 ((codec)->core.vendor_id == 0x80862800))
59 #define is_cannonlake(codec) ((codec)->core.vendor_id == 0x8086280c)
60 #define is_icelake(codec) ((codec)->core.vendor_id == 0x8086280f)
61 #define is_haswell_plus(codec) (is_haswell(codec) || is_broadwell(codec) \
62 || is_skylake(codec) || is_broxton(codec) \
63 || is_kabylake(codec) || is_geminilake(codec) \
64 || is_cannonlake(codec) || is_icelake(codec))
65 #define is_valleyview(codec) ((codec)->core.vendor_id == 0x80862882)
66 #define is_cherryview(codec) ((codec)->core.vendor_id == 0x80862883)
67 #define is_valleyview_plus(codec) (is_valleyview(codec) || is_cherryview(codec))
69 struct hdmi_spec_per_cvt {
72 unsigned int channels_min;
73 unsigned int channels_max;
79 /* max. connections to a widget */
80 #define HDA_MAX_CONNECTIONS 32
82 struct hdmi_spec_per_pin {
85 /* pin idx, different device entries on the same pin use the same idx */
88 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
92 struct hda_codec *codec;
93 struct hdmi_eld sink_eld;
95 struct delayed_work work;
96 struct hdmi_pcm *pcm; /* pointer to spec->pcm_rec[n] dynamically*/
97 int pcm_idx; /* which pcm is attached. -1 means no pcm is attached */
99 bool setup; /* the stream has been set up by prepare callback */
100 int channels; /* current number of channels */
102 bool chmap_set; /* channel-map override by ALSA API? */
103 unsigned char chmap[8]; /* ALSA API channel-map */
104 #ifdef CONFIG_SND_PROC_FS
105 struct snd_info_entry *proc_entry;
109 /* operations used by generic code that can be overridden by patches */
111 int (*pin_get_eld)(struct hda_codec *codec, hda_nid_t pin_nid,
112 unsigned char *buf, int *eld_size);
114 void (*pin_setup_infoframe)(struct hda_codec *codec, hda_nid_t pin_nid,
115 int ca, int active_channels, int conn_type);
117 /* enable/disable HBR (HD passthrough) */
118 int (*pin_hbr_setup)(struct hda_codec *codec, hda_nid_t pin_nid, bool hbr);
120 int (*setup_stream)(struct hda_codec *codec, hda_nid_t cvt_nid,
121 hda_nid_t pin_nid, u32 stream_tag, int format);
123 void (*pin_cvt_fixup)(struct hda_codec *codec,
124 struct hdmi_spec_per_pin *per_pin,
130 struct snd_jack *jack;
131 struct snd_kcontrol *eld_ctl;
136 struct snd_array cvts; /* struct hdmi_spec_per_cvt */
137 hda_nid_t cvt_nids[4]; /* only for haswell fix */
140 * num_pins is the number of virtual pins
141 * for example, there are 3 pins, and each pin
142 * has 4 device entries, then the num_pins is 12
146 * num_nids is the number of real pins
147 * In the above example, num_nids is 3
151 * dev_num is the number of device entries
153 * In the above example, dev_num is 4
156 struct snd_array pins; /* struct hdmi_spec_per_pin */
157 struct hdmi_pcm pcm_rec[16];
158 struct mutex pcm_lock;
159 /* pcm_bitmap means which pcms have been assigned to pins*/
160 unsigned long pcm_bitmap;
161 int pcm_used; /* counter of pcm_rec[] */
162 /* bitmap shows whether the pcm is opened in user space
163 * bit 0 means the first playback PCM (PCM3);
164 * bit 1 means the second playback PCM, and so on.
166 unsigned long pcm_in_use;
168 struct hdmi_eld temp_eld;
174 * Non-generic VIA/NVIDIA specific
176 struct hda_multi_out multiout;
177 struct hda_pcm_stream pcm_playback;
179 /* i915/powerwell (Haswell+/Valleyview+) specific */
180 bool use_acomp_notifier; /* use i915 eld_notify callback for hotplug */
181 struct drm_audio_component_audio_ops drm_audio_ops;
183 struct hdac_chmap chmap;
184 hda_nid_t vendor_nid;
189 #ifdef CONFIG_SND_HDA_COMPONENT
190 static inline bool codec_has_acomp(struct hda_codec *codec)
192 struct hdmi_spec *spec = codec->spec;
193 return spec->use_acomp_notifier;
196 #define codec_has_acomp(codec) false
199 struct hdmi_audio_infoframe {
206 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
210 u8 LFEPBL01_LSV36_DM_INH7;
213 struct dp_audio_infoframe {
216 u8 ver; /* 0x11 << 2 */
218 u8 CC02_CT47; /* match with HDMI infoframe from this on */
222 u8 LFEPBL01_LSV36_DM_INH7;
225 union audio_infoframe {
226 struct hdmi_audio_infoframe hdmi;
227 struct dp_audio_infoframe dp;
235 #define get_pin(spec, idx) \
236 ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
237 #define get_cvt(spec, idx) \
238 ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx))
239 /* obtain hdmi_pcm object assigned to idx */
240 #define get_hdmi_pcm(spec, idx) (&(spec)->pcm_rec[idx])
241 /* obtain hda_pcm object assigned to idx */
242 #define get_pcm_rec(spec, idx) (get_hdmi_pcm(spec, idx)->pcm)
244 static int pin_id_to_pin_index(struct hda_codec *codec,
245 hda_nid_t pin_nid, int dev_id)
247 struct hdmi_spec *spec = codec->spec;
249 struct hdmi_spec_per_pin *per_pin;
252 * (dev_id == -1) means it is NON-MST pin
253 * return the first virtual pin on this port
258 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
259 per_pin = get_pin(spec, pin_idx);
260 if ((per_pin->pin_nid == pin_nid) &&
261 (per_pin->dev_id == dev_id))
265 codec_warn(codec, "HDMI: pin nid %d not registered\n", pin_nid);
269 static int hinfo_to_pcm_index(struct hda_codec *codec,
270 struct hda_pcm_stream *hinfo)
272 struct hdmi_spec *spec = codec->spec;
275 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++)
276 if (get_pcm_rec(spec, pcm_idx)->stream == hinfo)
279 codec_warn(codec, "HDMI: hinfo %p not registered\n", hinfo);
283 static int hinfo_to_pin_index(struct hda_codec *codec,
284 struct hda_pcm_stream *hinfo)
286 struct hdmi_spec *spec = codec->spec;
287 struct hdmi_spec_per_pin *per_pin;
290 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
291 per_pin = get_pin(spec, pin_idx);
293 per_pin->pcm->pcm->stream == hinfo)
297 codec_dbg(codec, "HDMI: hinfo %p not registered\n", hinfo);
301 static struct hdmi_spec_per_pin *pcm_idx_to_pin(struct hdmi_spec *spec,
305 struct hdmi_spec_per_pin *per_pin;
307 for (i = 0; i < spec->num_pins; i++) {
308 per_pin = get_pin(spec, i);
309 if (per_pin->pcm_idx == pcm_idx)
315 static int cvt_nid_to_cvt_index(struct hda_codec *codec, hda_nid_t cvt_nid)
317 struct hdmi_spec *spec = codec->spec;
320 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
321 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
324 codec_warn(codec, "HDMI: cvt nid %d not registered\n", cvt_nid);
328 static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
329 struct snd_ctl_elem_info *uinfo)
331 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
332 struct hdmi_spec *spec = codec->spec;
333 struct hdmi_spec_per_pin *per_pin;
334 struct hdmi_eld *eld;
337 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
339 pcm_idx = kcontrol->private_value;
340 mutex_lock(&spec->pcm_lock);
341 per_pin = pcm_idx_to_pin(spec, pcm_idx);
343 /* no pin is bound to the pcm */
347 eld = &per_pin->sink_eld;
348 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
351 mutex_unlock(&spec->pcm_lock);
355 static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
356 struct snd_ctl_elem_value *ucontrol)
358 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
359 struct hdmi_spec *spec = codec->spec;
360 struct hdmi_spec_per_pin *per_pin;
361 struct hdmi_eld *eld;
365 pcm_idx = kcontrol->private_value;
366 mutex_lock(&spec->pcm_lock);
367 per_pin = pcm_idx_to_pin(spec, pcm_idx);
369 /* no pin is bound to the pcm */
370 memset(ucontrol->value.bytes.data, 0,
371 ARRAY_SIZE(ucontrol->value.bytes.data));
375 eld = &per_pin->sink_eld;
376 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data) ||
377 eld->eld_size > ELD_MAX_SIZE) {
383 memset(ucontrol->value.bytes.data, 0,
384 ARRAY_SIZE(ucontrol->value.bytes.data));
386 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
390 mutex_unlock(&spec->pcm_lock);
394 static const struct snd_kcontrol_new eld_bytes_ctl = {
395 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
396 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
398 .info = hdmi_eld_ctl_info,
399 .get = hdmi_eld_ctl_get,
402 static int hdmi_create_eld_ctl(struct hda_codec *codec, int pcm_idx,
405 struct snd_kcontrol *kctl;
406 struct hdmi_spec *spec = codec->spec;
409 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
412 kctl->private_value = pcm_idx;
413 kctl->id.device = device;
415 /* no pin nid is associated with the kctl now
416 * tbd: associate pin nid to eld ctl later
418 err = snd_hda_ctl_add(codec, 0, kctl);
422 get_hdmi_pcm(spec, pcm_idx)->eld_ctl = kctl;
427 static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
428 int *packet_index, int *byte_index)
432 val = snd_hda_codec_read(codec, pin_nid, 0,
433 AC_VERB_GET_HDMI_DIP_INDEX, 0);
435 *packet_index = val >> 5;
436 *byte_index = val & 0x1f;
440 static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
441 int packet_index, int byte_index)
445 val = (packet_index << 5) | (byte_index & 0x1f);
447 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
450 static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
453 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
456 static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
458 struct hdmi_spec *spec = codec->spec;
462 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
463 snd_hda_codec_write(codec, pin_nid, 0,
464 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
466 if (spec->dyn_pin_out)
467 /* Disable pin out until stream is active */
470 /* Enable pin out: some machines with GM965 gets broken output
471 * when the pin is disabled or changed while using with HDMI
475 snd_hda_codec_write(codec, pin_nid, 0,
476 AC_VERB_SET_PIN_WIDGET_CONTROL, pin_out);
483 #ifdef CONFIG_SND_PROC_FS
484 static void print_eld_info(struct snd_info_entry *entry,
485 struct snd_info_buffer *buffer)
487 struct hdmi_spec_per_pin *per_pin = entry->private_data;
489 mutex_lock(&per_pin->lock);
490 snd_hdmi_print_eld_info(&per_pin->sink_eld, buffer);
491 mutex_unlock(&per_pin->lock);
494 static void write_eld_info(struct snd_info_entry *entry,
495 struct snd_info_buffer *buffer)
497 struct hdmi_spec_per_pin *per_pin = entry->private_data;
499 mutex_lock(&per_pin->lock);
500 snd_hdmi_write_eld_info(&per_pin->sink_eld, buffer);
501 mutex_unlock(&per_pin->lock);
504 static int eld_proc_new(struct hdmi_spec_per_pin *per_pin, int index)
507 struct hda_codec *codec = per_pin->codec;
508 struct snd_info_entry *entry;
511 snprintf(name, sizeof(name), "eld#%d.%d", codec->addr, index);
512 err = snd_card_proc_new(codec->card, name, &entry);
516 snd_info_set_text_ops(entry, per_pin, print_eld_info);
517 entry->c.text.write = write_eld_info;
519 per_pin->proc_entry = entry;
524 static void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
526 if (!per_pin->codec->bus->shutdown) {
527 snd_info_free_entry(per_pin->proc_entry);
528 per_pin->proc_entry = NULL;
532 static inline int eld_proc_new(struct hdmi_spec_per_pin *per_pin,
537 static inline void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
543 * Audio InfoFrame routines
547 * Enable Audio InfoFrame Transmission
549 static void hdmi_start_infoframe_trans(struct hda_codec *codec,
552 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
553 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
558 * Disable Audio InfoFrame Transmission
560 static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
563 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
564 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
568 static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
570 #ifdef CONFIG_SND_DEBUG_VERBOSE
574 size = snd_hdmi_get_eld_size(codec, pin_nid);
575 codec_dbg(codec, "HDMI: ELD buf size is %d\n", size);
577 for (i = 0; i < 8; i++) {
578 size = snd_hda_codec_read(codec, pin_nid, 0,
579 AC_VERB_GET_HDMI_DIP_SIZE, i);
580 codec_dbg(codec, "HDMI: DIP GP[%d] buf size is %d\n", i, size);
585 static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
591 for (i = 0; i < 8; i++) {
592 size = snd_hda_codec_read(codec, pin_nid, 0,
593 AC_VERB_GET_HDMI_DIP_SIZE, i);
597 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
598 for (j = 1; j < 1000; j++) {
599 hdmi_write_dip_byte(codec, pin_nid, 0x0);
600 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
602 codec_dbg(codec, "dip index %d: %d != %d\n",
604 if (bi == 0) /* byte index wrapped around */
608 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
614 static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
616 u8 *bytes = (u8 *)hdmi_ai;
620 hdmi_ai->checksum = 0;
622 for (i = 0; i < sizeof(*hdmi_ai); i++)
625 hdmi_ai->checksum = -sum;
628 static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
634 hdmi_debug_dip_size(codec, pin_nid);
635 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
637 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
638 for (i = 0; i < size; i++)
639 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
642 static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
648 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
652 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
653 for (i = 0; i < size; i++) {
654 val = snd_hda_codec_read(codec, pin_nid, 0,
655 AC_VERB_GET_HDMI_DIP_DATA, 0);
663 static void hdmi_pin_setup_infoframe(struct hda_codec *codec,
665 int ca, int active_channels,
668 union audio_infoframe ai;
670 memset(&ai, 0, sizeof(ai));
671 if (conn_type == 0) { /* HDMI */
672 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
674 hdmi_ai->type = 0x84;
677 hdmi_ai->CC02_CT47 = active_channels - 1;
679 hdmi_checksum_audio_infoframe(hdmi_ai);
680 } else if (conn_type == 1) { /* DisplayPort */
681 struct dp_audio_infoframe *dp_ai = &ai.dp;
685 dp_ai->ver = 0x11 << 2;
686 dp_ai->CC02_CT47 = active_channels - 1;
689 codec_dbg(codec, "HDMI: unknown connection type at pin %d\n",
695 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
696 * sizeof(*dp_ai) to avoid partial match/update problems when
697 * the user switches between HDMI/DP monitors.
699 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
702 "hdmi_pin_setup_infoframe: pin=%d channels=%d ca=0x%02x\n",
704 active_channels, ca);
705 hdmi_stop_infoframe_trans(codec, pin_nid);
706 hdmi_fill_audio_infoframe(codec, pin_nid,
707 ai.bytes, sizeof(ai));
708 hdmi_start_infoframe_trans(codec, pin_nid);
712 static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
713 struct hdmi_spec_per_pin *per_pin,
716 struct hdmi_spec *spec = codec->spec;
717 struct hdac_chmap *chmap = &spec->chmap;
718 hda_nid_t pin_nid = per_pin->pin_nid;
719 int channels = per_pin->channels;
721 struct hdmi_eld *eld;
727 /* some HW (e.g. HSW+) needs reprogramming the amp at each time */
728 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
729 snd_hda_codec_write(codec, pin_nid, 0,
730 AC_VERB_SET_AMP_GAIN_MUTE,
733 eld = &per_pin->sink_eld;
735 ca = snd_hdac_channel_allocation(&codec->core,
736 eld->info.spk_alloc, channels,
737 per_pin->chmap_set, non_pcm, per_pin->chmap);
739 active_channels = snd_hdac_get_active_channels(ca);
741 chmap->ops.set_channel_count(&codec->core, per_pin->cvt_nid,
745 * always configure channel mapping, it may have been changed by the
746 * user in the meantime
748 snd_hdac_setup_channel_mapping(&spec->chmap,
749 pin_nid, non_pcm, ca, channels,
750 per_pin->chmap, per_pin->chmap_set);
752 spec->ops.pin_setup_infoframe(codec, pin_nid, ca, active_channels,
753 eld->info.conn_type);
755 per_pin->non_pcm = non_pcm;
762 static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
764 static void check_presence_and_report(struct hda_codec *codec, hda_nid_t nid,
767 struct hdmi_spec *spec = codec->spec;
768 int pin_idx = pin_id_to_pin_index(codec, nid, dev_id);
772 mutex_lock(&spec->pcm_lock);
773 if (hdmi_present_sense(get_pin(spec, pin_idx), 1))
774 snd_hda_jack_report_sync(codec);
775 mutex_unlock(&spec->pcm_lock);
778 static void jack_callback(struct hda_codec *codec,
779 struct hda_jack_callback *jack)
781 /* hda_jack don't support DP MST */
782 check_presence_and_report(codec, jack->nid, 0);
785 static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
787 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
788 struct hda_jack_tbl *jack;
789 int dev_entry = (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
792 * assume DP MST uses dyn_pcm_assign and acomp and
794 * if DP MST supports unsol event, below code need
797 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
800 jack->jack_dirty = 1;
803 "HDMI hot plug event: Codec=%d Pin=%d Device=%d Inactive=%d Presence_Detect=%d ELD_Valid=%d\n",
804 codec->addr, jack->nid, dev_entry, !!(res & AC_UNSOL_RES_IA),
805 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
807 /* hda_jack don't support DP MST */
808 check_presence_and_report(codec, jack->nid, 0);
811 static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
813 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
814 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
815 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
816 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
819 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
834 static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
836 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
837 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
839 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
840 codec_dbg(codec, "Unexpected HDMI event tag 0x%x\n", tag);
845 hdmi_intrinsic_event(codec, res);
847 hdmi_non_intrinsic_event(codec, res);
850 static void haswell_verify_D0(struct hda_codec *codec,
851 hda_nid_t cvt_nid, hda_nid_t nid)
855 /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
856 * thus pins could only choose converter 0 for use. Make sure the
857 * converters are in correct power state */
858 if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
859 snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
861 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
862 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
865 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
866 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
867 codec_dbg(codec, "Haswell HDMI audio: Power for pin 0x%x is now D%d\n", nid, pwr);
875 /* HBR should be Non-PCM, 8 channels */
876 #define is_hbr_format(format) \
877 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
879 static int hdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
882 int pinctl, new_pinctl;
884 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
885 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
886 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
889 return hbr ? -EINVAL : 0;
891 new_pinctl = pinctl & ~AC_PINCTL_EPT;
893 new_pinctl |= AC_PINCTL_EPT_HBR;
895 new_pinctl |= AC_PINCTL_EPT_NATIVE;
898 "hdmi_pin_hbr_setup: NID=0x%x, %spinctl=0x%x\n",
900 pinctl == new_pinctl ? "" : "new-",
903 if (pinctl != new_pinctl)
904 snd_hda_codec_write(codec, pin_nid, 0,
905 AC_VERB_SET_PIN_WIDGET_CONTROL,
913 static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
914 hda_nid_t pin_nid, u32 stream_tag, int format)
916 struct hdmi_spec *spec = codec->spec;
920 err = spec->ops.pin_hbr_setup(codec, pin_nid, is_hbr_format(format));
923 codec_dbg(codec, "hdmi_setup_stream: HBR is not supported\n");
927 if (is_haswell_plus(codec)) {
930 * on recent platforms IEC Coding Type is required for HBR
931 * support, read current Digital Converter settings and set
932 * ICT bitfield if needed.
934 param = snd_hda_codec_read(codec, cvt_nid, 0,
935 AC_VERB_GET_DIGI_CONVERT_1, 0);
937 param = (param >> 16) & ~(AC_DIG3_ICT);
939 /* on recent platforms ICT mode is required for HBR support */
940 if (is_hbr_format(format))
943 snd_hda_codec_write(codec, cvt_nid, 0,
944 AC_VERB_SET_DIGI_CONVERT_3, param);
947 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
951 /* Try to find an available converter
952 * If pin_idx is less then zero, just try to find an available converter.
953 * Otherwise, try to find an available converter and get the cvt mux index
956 static int hdmi_choose_cvt(struct hda_codec *codec,
957 int pin_idx, int *cvt_id)
959 struct hdmi_spec *spec = codec->spec;
960 struct hdmi_spec_per_pin *per_pin;
961 struct hdmi_spec_per_cvt *per_cvt = NULL;
962 int cvt_idx, mux_idx = 0;
964 /* pin_idx < 0 means no pin will be bound to the converter */
968 per_pin = get_pin(spec, pin_idx);
970 /* Dynamically assign converter to stream */
971 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
972 per_cvt = get_cvt(spec, cvt_idx);
974 /* Must not already be assigned */
975 if (per_cvt->assigned)
979 /* Must be in pin's mux's list of converters */
980 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
981 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
983 /* Not in mux list */
984 if (mux_idx == per_pin->num_mux_nids)
989 /* No free converters */
990 if (cvt_idx == spec->num_cvts)
994 per_pin->mux_idx = mux_idx;
1002 /* Assure the pin select the right convetor */
1003 static void intel_verify_pin_cvt_connect(struct hda_codec *codec,
1004 struct hdmi_spec_per_pin *per_pin)
1006 hda_nid_t pin_nid = per_pin->pin_nid;
1009 mux_idx = per_pin->mux_idx;
1010 curr = snd_hda_codec_read(codec, pin_nid, 0,
1011 AC_VERB_GET_CONNECT_SEL, 0);
1012 if (curr != mux_idx)
1013 snd_hda_codec_write_cache(codec, pin_nid, 0,
1014 AC_VERB_SET_CONNECT_SEL,
1018 /* get the mux index for the converter of the pins
1019 * converter's mux index is the same for all pins on Intel platform
1021 static int intel_cvt_id_to_mux_idx(struct hdmi_spec *spec,
1026 for (i = 0; i < spec->num_cvts; i++)
1027 if (spec->cvt_nids[i] == cvt_nid)
1032 /* Intel HDMI workaround to fix audio routing issue:
1033 * For some Intel display codecs, pins share the same connection list.
1034 * So a conveter can be selected by multiple pins and playback on any of these
1035 * pins will generate sound on the external display, because audio flows from
1036 * the same converter to the display pipeline. Also muting one pin may make
1037 * other pins have no sound output.
1038 * So this function assures that an assigned converter for a pin is not selected
1039 * by any other pins.
1041 static void intel_not_share_assigned_cvt(struct hda_codec *codec,
1043 int dev_id, int mux_idx)
1045 struct hdmi_spec *spec = codec->spec;
1048 struct hdmi_spec_per_cvt *per_cvt;
1049 struct hdmi_spec_per_pin *per_pin;
1052 /* configure the pins connections */
1053 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1057 per_pin = get_pin(spec, pin_idx);
1059 * pin not connected to monitor
1060 * no need to operate on it
1065 if ((per_pin->pin_nid == pin_nid) &&
1066 (per_pin->dev_id == dev_id))
1070 * if per_pin->dev_id >= dev_num,
1071 * snd_hda_get_dev_select() will fail,
1072 * and the following operation is unpredictable.
1073 * So skip this situation.
1075 dev_num = snd_hda_get_num_devices(codec, per_pin->pin_nid) + 1;
1076 if (per_pin->dev_id >= dev_num)
1079 nid = per_pin->pin_nid;
1082 * Calling this function should not impact
1083 * on the device entry selection
1084 * So let's save the dev id for each pin,
1085 * and restore it when return
1087 dev_id_saved = snd_hda_get_dev_select(codec, nid);
1088 snd_hda_set_dev_select(codec, nid, per_pin->dev_id);
1089 curr = snd_hda_codec_read(codec, nid, 0,
1090 AC_VERB_GET_CONNECT_SEL, 0);
1091 if (curr != mux_idx) {
1092 snd_hda_set_dev_select(codec, nid, dev_id_saved);
1097 /* choose an unassigned converter. The conveters in the
1098 * connection list are in the same order as in the codec.
1100 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1101 per_cvt = get_cvt(spec, cvt_idx);
1102 if (!per_cvt->assigned) {
1104 "choose cvt %d for pin nid %d\n",
1106 snd_hda_codec_write_cache(codec, nid, 0,
1107 AC_VERB_SET_CONNECT_SEL,
1112 snd_hda_set_dev_select(codec, nid, dev_id_saved);
1116 /* A wrapper of intel_not_share_asigned_cvt() */
1117 static void intel_not_share_assigned_cvt_nid(struct hda_codec *codec,
1118 hda_nid_t pin_nid, int dev_id, hda_nid_t cvt_nid)
1121 struct hdmi_spec *spec = codec->spec;
1123 /* On Intel platform, the mapping of converter nid to
1124 * mux index of the pins are always the same.
1125 * The pin nid may be 0, this means all pins will not
1126 * share the converter.
1128 mux_idx = intel_cvt_id_to_mux_idx(spec, cvt_nid);
1130 intel_not_share_assigned_cvt(codec, pin_nid, dev_id, mux_idx);
1133 /* skeleton caller of pin_cvt_fixup ops */
1134 static void pin_cvt_fixup(struct hda_codec *codec,
1135 struct hdmi_spec_per_pin *per_pin,
1138 struct hdmi_spec *spec = codec->spec;
1140 if (spec->ops.pin_cvt_fixup)
1141 spec->ops.pin_cvt_fixup(codec, per_pin, cvt_nid);
1144 /* called in hdmi_pcm_open when no pin is assigned to the PCM
1145 * in dyn_pcm_assign mode.
1147 static int hdmi_pcm_open_no_pin(struct hda_pcm_stream *hinfo,
1148 struct hda_codec *codec,
1149 struct snd_pcm_substream *substream)
1151 struct hdmi_spec *spec = codec->spec;
1152 struct snd_pcm_runtime *runtime = substream->runtime;
1153 int cvt_idx, pcm_idx;
1154 struct hdmi_spec_per_cvt *per_cvt = NULL;
1157 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1161 err = hdmi_choose_cvt(codec, -1, &cvt_idx);
1165 per_cvt = get_cvt(spec, cvt_idx);
1166 per_cvt->assigned = 1;
1167 hinfo->nid = per_cvt->cvt_nid;
1169 pin_cvt_fixup(codec, NULL, per_cvt->cvt_nid);
1171 set_bit(pcm_idx, &spec->pcm_in_use);
1172 /* todo: setup spdif ctls assign */
1174 /* Initially set the converter's capabilities */
1175 hinfo->channels_min = per_cvt->channels_min;
1176 hinfo->channels_max = per_cvt->channels_max;
1177 hinfo->rates = per_cvt->rates;
1178 hinfo->formats = per_cvt->formats;
1179 hinfo->maxbps = per_cvt->maxbps;
1181 /* Store the updated parameters */
1182 runtime->hw.channels_min = hinfo->channels_min;
1183 runtime->hw.channels_max = hinfo->channels_max;
1184 runtime->hw.formats = hinfo->formats;
1185 runtime->hw.rates = hinfo->rates;
1187 snd_pcm_hw_constraint_step(substream->runtime, 0,
1188 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1195 static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1196 struct hda_codec *codec,
1197 struct snd_pcm_substream *substream)
1199 struct hdmi_spec *spec = codec->spec;
1200 struct snd_pcm_runtime *runtime = substream->runtime;
1201 int pin_idx, cvt_idx, pcm_idx;
1202 struct hdmi_spec_per_pin *per_pin;
1203 struct hdmi_eld *eld;
1204 struct hdmi_spec_per_cvt *per_cvt = NULL;
1207 /* Validate hinfo */
1208 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1212 mutex_lock(&spec->pcm_lock);
1213 pin_idx = hinfo_to_pin_index(codec, hinfo);
1214 if (!spec->dyn_pcm_assign) {
1215 if (snd_BUG_ON(pin_idx < 0)) {
1220 /* no pin is assigned to the PCM
1221 * PA need pcm open successfully when probe
1224 err = hdmi_pcm_open_no_pin(hinfo, codec, substream);
1229 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx);
1233 per_cvt = get_cvt(spec, cvt_idx);
1234 /* Claim converter */
1235 per_cvt->assigned = 1;
1237 set_bit(pcm_idx, &spec->pcm_in_use);
1238 per_pin = get_pin(spec, pin_idx);
1239 per_pin->cvt_nid = per_cvt->cvt_nid;
1240 hinfo->nid = per_cvt->cvt_nid;
1242 snd_hda_set_dev_select(codec, per_pin->pin_nid, per_pin->dev_id);
1243 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1244 AC_VERB_SET_CONNECT_SEL,
1247 /* configure unused pins to choose other converters */
1248 pin_cvt_fixup(codec, per_pin, 0);
1250 snd_hda_spdif_ctls_assign(codec, pcm_idx, per_cvt->cvt_nid);
1252 /* Initially set the converter's capabilities */
1253 hinfo->channels_min = per_cvt->channels_min;
1254 hinfo->channels_max = per_cvt->channels_max;
1255 hinfo->rates = per_cvt->rates;
1256 hinfo->formats = per_cvt->formats;
1257 hinfo->maxbps = per_cvt->maxbps;
1259 eld = &per_pin->sink_eld;
1260 /* Restrict capabilities by ELD if this isn't disabled */
1261 if (!static_hdmi_pcm && eld->eld_valid) {
1262 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
1263 if (hinfo->channels_min > hinfo->channels_max ||
1264 !hinfo->rates || !hinfo->formats) {
1265 per_cvt->assigned = 0;
1267 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
1273 /* Store the updated parameters */
1274 runtime->hw.channels_min = hinfo->channels_min;
1275 runtime->hw.channels_max = hinfo->channels_max;
1276 runtime->hw.formats = hinfo->formats;
1277 runtime->hw.rates = hinfo->rates;
1279 snd_pcm_hw_constraint_step(substream->runtime, 0,
1280 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1282 mutex_unlock(&spec->pcm_lock);
1287 * HDA/HDMI auto parsing
1289 static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
1291 struct hdmi_spec *spec = codec->spec;
1292 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1293 hda_nid_t pin_nid = per_pin->pin_nid;
1295 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
1297 "HDMI: pin %d wcaps %#x does not support connection list\n",
1298 pin_nid, get_wcaps(codec, pin_nid));
1302 /* all the device entries on the same pin have the same conn list */
1303 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1305 HDA_MAX_CONNECTIONS);
1310 static int hdmi_find_pcm_slot(struct hdmi_spec *spec,
1311 struct hdmi_spec_per_pin *per_pin)
1315 /* try the prefer PCM */
1316 if (!test_bit(per_pin->pin_nid_idx, &spec->pcm_bitmap))
1317 return per_pin->pin_nid_idx;
1319 /* have a second try; check the "reserved area" over num_pins */
1320 for (i = spec->num_nids; i < spec->pcm_used; i++) {
1321 if (!test_bit(i, &spec->pcm_bitmap))
1325 /* the last try; check the empty slots in pins */
1326 for (i = 0; i < spec->num_nids; i++) {
1327 if (!test_bit(i, &spec->pcm_bitmap))
1333 static void hdmi_attach_hda_pcm(struct hdmi_spec *spec,
1334 struct hdmi_spec_per_pin *per_pin)
1338 /* pcm already be attached to the pin */
1341 idx = hdmi_find_pcm_slot(spec, per_pin);
1344 per_pin->pcm_idx = idx;
1345 per_pin->pcm = get_hdmi_pcm(spec, idx);
1346 set_bit(idx, &spec->pcm_bitmap);
1349 static void hdmi_detach_hda_pcm(struct hdmi_spec *spec,
1350 struct hdmi_spec_per_pin *per_pin)
1354 /* pcm already be detached from the pin */
1357 idx = per_pin->pcm_idx;
1358 per_pin->pcm_idx = -1;
1359 per_pin->pcm = NULL;
1360 if (idx >= 0 && idx < spec->pcm_used)
1361 clear_bit(idx, &spec->pcm_bitmap);
1364 static int hdmi_get_pin_cvt_mux(struct hdmi_spec *spec,
1365 struct hdmi_spec_per_pin *per_pin, hda_nid_t cvt_nid)
1369 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1370 if (per_pin->mux_nids[mux_idx] == cvt_nid)
1375 static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid);
1377 static void hdmi_pcm_setup_pin(struct hdmi_spec *spec,
1378 struct hdmi_spec_per_pin *per_pin)
1380 struct hda_codec *codec = per_pin->codec;
1381 struct hda_pcm *pcm;
1382 struct hda_pcm_stream *hinfo;
1383 struct snd_pcm_substream *substream;
1387 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1388 pcm = get_pcm_rec(spec, per_pin->pcm_idx);
1393 if (!test_bit(per_pin->pcm_idx, &spec->pcm_in_use))
1396 /* hdmi audio only uses playback and one substream */
1397 hinfo = pcm->stream;
1398 substream = pcm->pcm->streams[0].substream;
1400 per_pin->cvt_nid = hinfo->nid;
1402 mux_idx = hdmi_get_pin_cvt_mux(spec, per_pin, hinfo->nid);
1403 if (mux_idx < per_pin->num_mux_nids) {
1404 snd_hda_set_dev_select(codec, per_pin->pin_nid,
1406 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1407 AC_VERB_SET_CONNECT_SEL,
1410 snd_hda_spdif_ctls_assign(codec, per_pin->pcm_idx, hinfo->nid);
1412 non_pcm = check_non_pcm_per_cvt(codec, hinfo->nid);
1413 if (substream->runtime)
1414 per_pin->channels = substream->runtime->channels;
1415 per_pin->setup = true;
1416 per_pin->mux_idx = mux_idx;
1418 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1421 static void hdmi_pcm_reset_pin(struct hdmi_spec *spec,
1422 struct hdmi_spec_per_pin *per_pin)
1424 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1425 snd_hda_spdif_ctls_unassign(per_pin->codec, per_pin->pcm_idx);
1427 per_pin->chmap_set = false;
1428 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
1430 per_pin->setup = false;
1431 per_pin->channels = 0;
1434 /* update per_pin ELD from the given new ELD;
1435 * setup info frame and notification accordingly
1437 static void update_eld(struct hda_codec *codec,
1438 struct hdmi_spec_per_pin *per_pin,
1439 struct hdmi_eld *eld)
1441 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
1442 struct hdmi_spec *spec = codec->spec;
1443 bool old_eld_valid = pin_eld->eld_valid;
1447 /* for monitor disconnection, save pcm_idx firstly */
1448 pcm_idx = per_pin->pcm_idx;
1449 if (spec->dyn_pcm_assign) {
1450 if (eld->eld_valid) {
1451 hdmi_attach_hda_pcm(spec, per_pin);
1452 hdmi_pcm_setup_pin(spec, per_pin);
1454 hdmi_pcm_reset_pin(spec, per_pin);
1455 hdmi_detach_hda_pcm(spec, per_pin);
1458 /* if pcm_idx == -1, it means this is in monitor connection event
1459 * we can get the correct pcm_idx now.
1462 pcm_idx = per_pin->pcm_idx;
1465 snd_hdmi_show_eld(codec, &eld->info);
1467 eld_changed = (pin_eld->eld_valid != eld->eld_valid);
1468 if (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)
1474 pin_eld->monitor_present = eld->monitor_present;
1475 pin_eld->eld_valid = eld->eld_valid;
1476 pin_eld->eld_size = eld->eld_size;
1478 memcpy(pin_eld->eld_buffer, eld->eld_buffer, eld->eld_size);
1479 pin_eld->info = eld->info;
1482 * Re-setup pin and infoframe. This is needed e.g. when
1483 * - sink is first plugged-in
1484 * - transcoder can change during stream playback on Haswell
1485 * and this can make HW reset converter selection on a pin.
1487 if (eld->eld_valid && !old_eld_valid && per_pin->setup) {
1488 pin_cvt_fixup(codec, per_pin, 0);
1489 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1492 if (eld_changed && pcm_idx >= 0)
1493 snd_ctl_notify(codec->card,
1494 SNDRV_CTL_EVENT_MASK_VALUE |
1495 SNDRV_CTL_EVENT_MASK_INFO,
1496 &get_hdmi_pcm(spec, pcm_idx)->eld_ctl->id);
1499 /* update ELD and jack state via HD-audio verbs */
1500 static bool hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin *per_pin,
1503 struct hda_jack_tbl *jack;
1504 struct hda_codec *codec = per_pin->codec;
1505 struct hdmi_spec *spec = codec->spec;
1506 struct hdmi_eld *eld = &spec->temp_eld;
1507 hda_nid_t pin_nid = per_pin->pin_nid;
1509 * Always execute a GetPinSense verb here, even when called from
1510 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1511 * response's PD bit is not the real PD value, but indicates that
1512 * the real PD value changed. An older version of the HD-audio
1513 * specification worked this way. Hence, we just ignore the data in
1514 * the unsolicited response to avoid custom WARs.
1518 bool do_repoll = false;
1520 present = snd_hda_pin_sense(codec, pin_nid);
1522 mutex_lock(&per_pin->lock);
1523 eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1524 if (eld->monitor_present)
1525 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1527 eld->eld_valid = false;
1530 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
1531 codec->addr, pin_nid, eld->monitor_present, eld->eld_valid);
1533 if (eld->eld_valid) {
1534 if (spec->ops.pin_get_eld(codec, pin_nid, eld->eld_buffer,
1535 &eld->eld_size) < 0)
1536 eld->eld_valid = false;
1538 if (snd_hdmi_parse_eld(codec, &eld->info, eld->eld_buffer,
1540 eld->eld_valid = false;
1542 if (!eld->eld_valid && repoll)
1547 schedule_delayed_work(&per_pin->work, msecs_to_jiffies(300));
1549 update_eld(codec, per_pin, eld);
1551 ret = !repoll || !eld->monitor_present || eld->eld_valid;
1553 jack = snd_hda_jack_tbl_get(codec, pin_nid);
1555 jack->block_report = !ret;
1556 jack->pin_sense = (eld->monitor_present && eld->eld_valid) ?
1557 AC_PINSENSE_PRESENCE : 0;
1559 mutex_unlock(&per_pin->lock);
1563 static struct snd_jack *pin_idx_to_jack(struct hda_codec *codec,
1564 struct hdmi_spec_per_pin *per_pin)
1566 struct hdmi_spec *spec = codec->spec;
1567 struct snd_jack *jack = NULL;
1568 struct hda_jack_tbl *jack_tbl;
1570 /* if !dyn_pcm_assign, get jack from hda_jack_tbl
1571 * in !dyn_pcm_assign case, spec->pcm_rec[].jack is not
1572 * NULL even after snd_hda_jack_tbl_clear() is called to
1573 * free snd_jack. This may cause access invalid memory
1574 * when calling snd_jack_report
1576 if (per_pin->pcm_idx >= 0 && spec->dyn_pcm_assign)
1577 jack = spec->pcm_rec[per_pin->pcm_idx].jack;
1578 else if (!spec->dyn_pcm_assign) {
1580 * jack tbl doesn't support DP MST
1581 * DP MST will use dyn_pcm_assign,
1582 * so DP MST will never come here
1584 jack_tbl = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
1586 jack = jack_tbl->jack;
1591 /* update ELD and jack state via audio component */
1592 static void sync_eld_via_acomp(struct hda_codec *codec,
1593 struct hdmi_spec_per_pin *per_pin)
1595 struct hdmi_spec *spec = codec->spec;
1596 struct hdmi_eld *eld = &spec->temp_eld;
1597 struct snd_jack *jack = NULL;
1600 mutex_lock(&per_pin->lock);
1601 eld->monitor_present = false;
1602 size = snd_hdac_acomp_get_eld(&codec->core, per_pin->pin_nid,
1603 per_pin->dev_id, &eld->monitor_present,
1604 eld->eld_buffer, ELD_MAX_SIZE);
1606 size = min(size, ELD_MAX_SIZE);
1607 if (snd_hdmi_parse_eld(codec, &eld->info,
1608 eld->eld_buffer, size) < 0)
1613 eld->eld_valid = true;
1614 eld->eld_size = size;
1616 eld->eld_valid = false;
1620 /* pcm_idx >=0 before update_eld() means it is in monitor
1621 * disconnected event. Jack must be fetched before update_eld()
1623 jack = pin_idx_to_jack(codec, per_pin);
1624 update_eld(codec, per_pin, eld);
1626 jack = pin_idx_to_jack(codec, per_pin);
1629 snd_jack_report(jack,
1630 eld->monitor_present ? SND_JACK_AVOUT : 0);
1632 mutex_unlock(&per_pin->lock);
1635 static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
1637 struct hda_codec *codec = per_pin->codec;
1640 /* no temporary power up/down needed for component notifier */
1641 if (!codec_has_acomp(codec)) {
1642 ret = snd_hda_power_up_pm(codec);
1643 if (ret < 0 && pm_runtime_suspended(hda_codec_dev(codec))) {
1644 snd_hda_power_down_pm(codec);
1649 if (codec_has_acomp(codec)) {
1650 sync_eld_via_acomp(codec, per_pin);
1651 ret = false; /* don't call snd_hda_jack_report_sync() */
1653 ret = hdmi_present_sense_via_verbs(per_pin, repoll);
1656 if (!codec_has_acomp(codec))
1657 snd_hda_power_down_pm(codec);
1662 static void hdmi_repoll_eld(struct work_struct *work)
1664 struct hdmi_spec_per_pin *per_pin =
1665 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1666 struct hda_codec *codec = per_pin->codec;
1667 struct hdmi_spec *spec = codec->spec;
1668 struct hda_jack_tbl *jack;
1670 jack = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
1672 jack->jack_dirty = 1;
1674 if (per_pin->repoll_count++ > 6)
1675 per_pin->repoll_count = 0;
1677 mutex_lock(&spec->pcm_lock);
1678 if (hdmi_present_sense(per_pin, per_pin->repoll_count))
1679 snd_hda_jack_report_sync(per_pin->codec);
1680 mutex_unlock(&spec->pcm_lock);
1683 static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1686 static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1688 struct hdmi_spec *spec = codec->spec;
1689 unsigned int caps, config;
1691 struct hdmi_spec_per_pin *per_pin;
1695 caps = snd_hda_query_pin_caps(codec, pin_nid);
1696 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1700 * For DP MST audio, Configuration Default is the same for
1701 * all device entries on the same pin
1703 config = snd_hda_codec_get_pincfg(codec, pin_nid);
1704 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
1708 * To simplify the implementation, malloc all
1709 * the virtual pins in the initialization statically
1711 if (is_haswell_plus(codec)) {
1713 * On Intel platforms, device entries number is
1714 * changed dynamically. If there is a DP MST
1715 * hub connected, the device entries number is 3.
1716 * Otherwise, it is 1.
1717 * Here we manually set dev_num to 3, so that
1718 * we can initialize all the device entries when
1719 * bootup statically.
1723 } else if (spec->dyn_pcm_assign && codec->dp_mst) {
1724 dev_num = snd_hda_get_num_devices(codec, pin_nid) + 1;
1726 * spec->dev_num is the maxinum number of device entries
1727 * among all the pins
1729 spec->dev_num = (spec->dev_num > dev_num) ?
1730 spec->dev_num : dev_num;
1733 * If the platform doesn't support DP MST,
1734 * manually set dev_num to 1. This means
1735 * the pin has only one device entry.
1741 for (i = 0; i < dev_num; i++) {
1742 pin_idx = spec->num_pins;
1743 per_pin = snd_array_new(&spec->pins);
1748 if (spec->dyn_pcm_assign) {
1749 per_pin->pcm = NULL;
1750 per_pin->pcm_idx = -1;
1752 per_pin->pcm = get_hdmi_pcm(spec, pin_idx);
1753 per_pin->pcm_idx = pin_idx;
1755 per_pin->pin_nid = pin_nid;
1756 per_pin->pin_nid_idx = spec->num_nids;
1757 per_pin->dev_id = i;
1758 per_pin->non_pcm = false;
1759 snd_hda_set_dev_select(codec, pin_nid, i);
1760 if (is_haswell_plus(codec))
1761 intel_haswell_fixup_connect_list(codec, pin_nid);
1762 err = hdmi_read_pin_conn(codec, pin_idx);
1772 static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1774 struct hdmi_spec *spec = codec->spec;
1775 struct hdmi_spec_per_cvt *per_cvt;
1779 chans = get_wcaps(codec, cvt_nid);
1780 chans = get_wcaps_channels(chans);
1782 per_cvt = snd_array_new(&spec->cvts);
1786 per_cvt->cvt_nid = cvt_nid;
1787 per_cvt->channels_min = 2;
1789 per_cvt->channels_max = chans;
1790 if (chans > spec->chmap.channels_max)
1791 spec->chmap.channels_max = chans;
1794 err = snd_hda_query_supported_pcm(codec, cvt_nid,
1801 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
1802 spec->cvt_nids[spec->num_cvts] = cvt_nid;
1808 static int hdmi_parse_codec(struct hda_codec *codec)
1813 nodes = snd_hda_get_sub_nodes(codec, codec->core.afg, &nid);
1814 if (!nid || nodes < 0) {
1815 codec_warn(codec, "HDMI: failed to get afg sub nodes\n");
1819 for (i = 0; i < nodes; i++, nid++) {
1823 caps = get_wcaps(codec, nid);
1824 type = get_wcaps_type(caps);
1826 if (!(caps & AC_WCAP_DIGITAL))
1830 case AC_WID_AUD_OUT:
1831 hdmi_add_cvt(codec, nid);
1834 hdmi_add_pin(codec, nid);
1844 static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1846 struct hda_spdif_out *spdif;
1849 mutex_lock(&codec->spdif_mutex);
1850 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
1851 /* Add sanity check to pass klockwork check.
1852 * This should never happen.
1854 if (WARN_ON(spdif == NULL))
1856 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
1857 mutex_unlock(&codec->spdif_mutex);
1865 static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1866 struct hda_codec *codec,
1867 unsigned int stream_tag,
1868 unsigned int format,
1869 struct snd_pcm_substream *substream)
1871 hda_nid_t cvt_nid = hinfo->nid;
1872 struct hdmi_spec *spec = codec->spec;
1874 struct hdmi_spec_per_pin *per_pin;
1876 struct snd_pcm_runtime *runtime = substream->runtime;
1881 mutex_lock(&spec->pcm_lock);
1882 pin_idx = hinfo_to_pin_index(codec, hinfo);
1883 if (spec->dyn_pcm_assign && pin_idx < 0) {
1884 /* when dyn_pcm_assign and pcm is not bound to a pin
1885 * skip pin setup and return 0 to make audio playback
1888 pin_cvt_fixup(codec, NULL, cvt_nid);
1889 snd_hda_codec_setup_stream(codec, cvt_nid,
1890 stream_tag, 0, format);
1894 if (snd_BUG_ON(pin_idx < 0)) {
1898 per_pin = get_pin(spec, pin_idx);
1899 pin_nid = per_pin->pin_nid;
1901 /* Verify pin:cvt selections to avoid silent audio after S3.
1902 * After S3, the audio driver restores pin:cvt selections
1903 * but this can happen before gfx is ready and such selection
1904 * is overlooked by HW. Thus multiple pins can share a same
1905 * default convertor and mute control will affect each other,
1906 * which can cause a resumed audio playback become silent
1909 pin_cvt_fixup(codec, per_pin, 0);
1911 /* Call sync_audio_rate to set the N/CTS/M manually if necessary */
1912 /* Todo: add DP1.2 MST audio support later */
1913 if (codec_has_acomp(codec))
1914 snd_hdac_sync_audio_rate(&codec->core, pin_nid, per_pin->dev_id,
1917 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
1918 mutex_lock(&per_pin->lock);
1919 per_pin->channels = substream->runtime->channels;
1920 per_pin->setup = true;
1922 if (get_wcaps(codec, cvt_nid) & AC_WCAP_STRIPE) {
1923 stripe = snd_hdac_get_stream_stripe_ctl(&codec->bus->core,
1925 snd_hda_codec_write(codec, cvt_nid, 0,
1926 AC_VERB_SET_STRIPE_CONTROL,
1930 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1931 mutex_unlock(&per_pin->lock);
1932 if (spec->dyn_pin_out) {
1933 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
1934 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1935 snd_hda_codec_write(codec, pin_nid, 0,
1936 AC_VERB_SET_PIN_WIDGET_CONTROL,
1940 /* snd_hda_set_dev_select() has been called before */
1941 err = spec->ops.setup_stream(codec, cvt_nid, pin_nid,
1942 stream_tag, format);
1944 mutex_unlock(&spec->pcm_lock);
1948 static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1949 struct hda_codec *codec,
1950 struct snd_pcm_substream *substream)
1952 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1956 static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
1957 struct hda_codec *codec,
1958 struct snd_pcm_substream *substream)
1960 struct hdmi_spec *spec = codec->spec;
1961 int cvt_idx, pin_idx, pcm_idx;
1962 struct hdmi_spec_per_cvt *per_cvt;
1963 struct hdmi_spec_per_pin *per_pin;
1968 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1969 if (snd_BUG_ON(pcm_idx < 0))
1971 cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid);
1972 if (snd_BUG_ON(cvt_idx < 0))
1974 per_cvt = get_cvt(spec, cvt_idx);
1976 snd_BUG_ON(!per_cvt->assigned);
1977 per_cvt->assigned = 0;
1980 mutex_lock(&spec->pcm_lock);
1981 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
1982 clear_bit(pcm_idx, &spec->pcm_in_use);
1983 pin_idx = hinfo_to_pin_index(codec, hinfo);
1984 if (spec->dyn_pcm_assign && pin_idx < 0)
1987 if (snd_BUG_ON(pin_idx < 0)) {
1991 per_pin = get_pin(spec, pin_idx);
1993 if (spec->dyn_pin_out) {
1994 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
1995 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1996 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
1997 AC_VERB_SET_PIN_WIDGET_CONTROL,
2001 mutex_lock(&per_pin->lock);
2002 per_pin->chmap_set = false;
2003 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
2005 per_pin->setup = false;
2006 per_pin->channels = 0;
2007 mutex_unlock(&per_pin->lock);
2009 mutex_unlock(&spec->pcm_lock);
2015 static const struct hda_pcm_ops generic_ops = {
2016 .open = hdmi_pcm_open,
2017 .close = hdmi_pcm_close,
2018 .prepare = generic_hdmi_playback_pcm_prepare,
2019 .cleanup = generic_hdmi_playback_pcm_cleanup,
2022 static int hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
2024 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2025 struct hdmi_spec *spec = codec->spec;
2026 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2031 return per_pin->sink_eld.info.spk_alloc;
2034 static void hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
2035 unsigned char *chmap)
2037 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2038 struct hdmi_spec *spec = codec->spec;
2039 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2041 /* chmap is already set to 0 in caller */
2045 memcpy(chmap, per_pin->chmap, ARRAY_SIZE(per_pin->chmap));
2048 static void hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
2049 unsigned char *chmap, int prepared)
2051 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2052 struct hdmi_spec *spec = codec->spec;
2053 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2057 mutex_lock(&per_pin->lock);
2058 per_pin->chmap_set = true;
2059 memcpy(per_pin->chmap, chmap, ARRAY_SIZE(per_pin->chmap));
2061 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
2062 mutex_unlock(&per_pin->lock);
2065 static bool is_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
2067 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
2068 struct hdmi_spec *spec = codec->spec;
2069 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2071 return per_pin ? true:false;
2074 static int generic_hdmi_build_pcms(struct hda_codec *codec)
2076 struct hdmi_spec *spec = codec->spec;
2080 * for non-mst mode, pcm number is the same as before
2081 * for DP MST mode, pcm number is (nid number + dev_num - 1)
2082 * dev_num is the device entry number in a pin
2085 for (idx = 0; idx < spec->num_nids + spec->dev_num - 1; idx++) {
2086 struct hda_pcm *info;
2087 struct hda_pcm_stream *pstr;
2089 info = snd_hda_codec_pcm_new(codec, "HDMI %d", idx);
2093 spec->pcm_rec[idx].pcm = info;
2095 info->pcm_type = HDA_PCM_TYPE_HDMI;
2096 info->own_chmap = true;
2098 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2099 pstr->substreams = 1;
2100 pstr->ops = generic_ops;
2101 /* pcm number is less than 16 */
2102 if (spec->pcm_used >= 16)
2104 /* other pstr fields are set in open */
2110 static void free_hdmi_jack_priv(struct snd_jack *jack)
2112 struct hdmi_pcm *pcm = jack->private_data;
2117 static int add_hdmi_jack_kctl(struct hda_codec *codec,
2118 struct hdmi_spec *spec,
2122 struct snd_jack *jack;
2125 err = snd_jack_new(codec->card, name, SND_JACK_AVOUT, &jack,
2130 spec->pcm_rec[pcm_idx].jack = jack;
2131 jack->private_data = &spec->pcm_rec[pcm_idx];
2132 jack->private_free = free_hdmi_jack_priv;
2136 static int generic_hdmi_build_jack(struct hda_codec *codec, int pcm_idx)
2138 char hdmi_str[32] = "HDMI/DP";
2139 struct hdmi_spec *spec = codec->spec;
2140 struct hdmi_spec_per_pin *per_pin;
2141 struct hda_jack_tbl *jack;
2142 int pcmdev = get_pcm_rec(spec, pcm_idx)->device;
2147 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
2149 if (spec->dyn_pcm_assign)
2150 return add_hdmi_jack_kctl(codec, spec, pcm_idx, hdmi_str);
2152 /* for !dyn_pcm_assign, we still use hda_jack for compatibility */
2153 /* if !dyn_pcm_assign, it must be non-MST mode.
2154 * This means pcms and pins are statically mapped.
2155 * And pcm_idx is pin_idx.
2157 per_pin = get_pin(spec, pcm_idx);
2158 phantom_jack = !is_jack_detectable(codec, per_pin->pin_nid);
2160 strncat(hdmi_str, " Phantom",
2161 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
2162 ret = snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str,
2163 phantom_jack, 0, NULL);
2166 jack = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
2169 /* assign jack->jack to pcm_rec[].jack to
2170 * align with dyn_pcm_assign mode
2172 spec->pcm_rec[pcm_idx].jack = jack->jack;
2176 static int generic_hdmi_build_controls(struct hda_codec *codec)
2178 struct hdmi_spec *spec = codec->spec;
2180 int pin_idx, pcm_idx;
2182 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2183 if (!get_pcm_rec(spec, pcm_idx)->pcm) {
2184 /* no PCM: mark this for skipping permanently */
2185 set_bit(pcm_idx, &spec->pcm_bitmap);
2189 err = generic_hdmi_build_jack(codec, pcm_idx);
2193 /* create the spdif for each pcm
2194 * pin will be bound when monitor is connected
2196 if (spec->dyn_pcm_assign)
2197 err = snd_hda_create_dig_out_ctls(codec,
2198 0, spec->cvt_nids[0],
2201 struct hdmi_spec_per_pin *per_pin =
2202 get_pin(spec, pcm_idx);
2203 err = snd_hda_create_dig_out_ctls(codec,
2205 per_pin->mux_nids[0],
2210 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
2212 dev = get_pcm_rec(spec, pcm_idx)->device;
2213 if (dev != SNDRV_PCM_INVALID_DEVICE) {
2214 /* add control for ELD Bytes */
2215 err = hdmi_create_eld_ctl(codec, pcm_idx, dev);
2221 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2222 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2224 hdmi_present_sense(per_pin, 0);
2227 /* add channel maps */
2228 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2229 struct hda_pcm *pcm;
2231 pcm = get_pcm_rec(spec, pcm_idx);
2232 if (!pcm || !pcm->pcm)
2234 err = snd_hdac_add_chmap_ctls(pcm->pcm, pcm_idx, &spec->chmap);
2242 static int generic_hdmi_init_per_pins(struct hda_codec *codec)
2244 struct hdmi_spec *spec = codec->spec;
2247 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2248 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2250 per_pin->codec = codec;
2251 mutex_init(&per_pin->lock);
2252 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
2253 eld_proc_new(per_pin, pin_idx);
2258 static int generic_hdmi_init(struct hda_codec *codec)
2260 struct hdmi_spec *spec = codec->spec;
2263 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2264 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2265 hda_nid_t pin_nid = per_pin->pin_nid;
2266 int dev_id = per_pin->dev_id;
2268 snd_hda_set_dev_select(codec, pin_nid, dev_id);
2269 hdmi_init_pin(codec, pin_nid);
2270 if (!codec_has_acomp(codec))
2271 snd_hda_jack_detect_enable_callback(codec, pin_nid,
2272 codec->jackpoll_interval > 0 ?
2273 jack_callback : NULL);
2278 static void hdmi_array_init(struct hdmi_spec *spec, int nums)
2280 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
2281 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
2284 static void hdmi_array_free(struct hdmi_spec *spec)
2286 snd_array_free(&spec->pins);
2287 snd_array_free(&spec->cvts);
2290 static void generic_spec_free(struct hda_codec *codec)
2292 struct hdmi_spec *spec = codec->spec;
2295 hdmi_array_free(spec);
2299 codec->dp_mst = false;
2302 static void generic_hdmi_free(struct hda_codec *codec)
2304 struct hdmi_spec *spec = codec->spec;
2305 int pin_idx, pcm_idx;
2307 if (codec_has_acomp(codec))
2308 snd_hdac_acomp_register_notifier(&codec->bus->core, NULL);
2310 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2311 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2312 cancel_delayed_work_sync(&per_pin->work);
2313 eld_proc_free(per_pin);
2316 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2317 if (spec->pcm_rec[pcm_idx].jack == NULL)
2319 if (spec->dyn_pcm_assign)
2320 snd_device_free(codec->card,
2321 spec->pcm_rec[pcm_idx].jack);
2323 spec->pcm_rec[pcm_idx].jack = NULL;
2326 generic_spec_free(codec);
2330 static int generic_hdmi_resume(struct hda_codec *codec)
2332 struct hdmi_spec *spec = codec->spec;
2335 codec->patch_ops.init(codec);
2336 regcache_sync(codec->core.regmap);
2338 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2339 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2340 hdmi_present_sense(per_pin, 1);
2346 static const struct hda_codec_ops generic_hdmi_patch_ops = {
2347 .init = generic_hdmi_init,
2348 .free = generic_hdmi_free,
2349 .build_pcms = generic_hdmi_build_pcms,
2350 .build_controls = generic_hdmi_build_controls,
2351 .unsol_event = hdmi_unsol_event,
2353 .resume = generic_hdmi_resume,
2357 static const struct hdmi_ops generic_standard_hdmi_ops = {
2358 .pin_get_eld = snd_hdmi_get_eld,
2359 .pin_setup_infoframe = hdmi_pin_setup_infoframe,
2360 .pin_hbr_setup = hdmi_pin_hbr_setup,
2361 .setup_stream = hdmi_setup_stream,
2364 /* allocate codec->spec and assign/initialize generic parser ops */
2365 static int alloc_generic_hdmi(struct hda_codec *codec)
2367 struct hdmi_spec *spec;
2369 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2373 spec->ops = generic_standard_hdmi_ops;
2374 spec->dev_num = 1; /* initialize to 1 */
2375 mutex_init(&spec->pcm_lock);
2376 snd_hdac_register_chmap_ops(&codec->core, &spec->chmap);
2378 spec->chmap.ops.get_chmap = hdmi_get_chmap;
2379 spec->chmap.ops.set_chmap = hdmi_set_chmap;
2380 spec->chmap.ops.is_pcm_attached = is_hdmi_pcm_attached;
2381 spec->chmap.ops.get_spk_alloc = hdmi_get_spk_alloc,
2384 hdmi_array_init(spec, 4);
2386 codec->patch_ops = generic_hdmi_patch_ops;
2391 /* generic HDMI parser */
2392 static int patch_generic_hdmi(struct hda_codec *codec)
2396 err = alloc_generic_hdmi(codec);
2400 err = hdmi_parse_codec(codec);
2402 generic_spec_free(codec);
2406 generic_hdmi_init_per_pins(codec);
2411 * Intel codec parsers and helpers
2414 static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
2417 struct hdmi_spec *spec = codec->spec;
2421 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
2422 if (nconns == spec->num_cvts &&
2423 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
2426 /* override pins connection list */
2427 codec_dbg(codec, "hdmi: haswell: override pin connection 0x%x\n", nid);
2428 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
2431 #define INTEL_GET_VENDOR_VERB 0xf81
2432 #define INTEL_GET_VENDOR_VERB 0xf81
2433 #define INTEL_SET_VENDOR_VERB 0x781
2434 #define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
2435 #define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
2437 static void intel_haswell_enable_all_pins(struct hda_codec *codec,
2440 unsigned int vendor_param;
2441 struct hdmi_spec *spec = codec->spec;
2443 vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0,
2444 INTEL_GET_VENDOR_VERB, 0);
2445 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
2448 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
2449 vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0,
2450 INTEL_SET_VENDOR_VERB, vendor_param);
2451 if (vendor_param == -1)
2455 snd_hda_codec_update_widgets(codec);
2458 static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
2460 unsigned int vendor_param;
2461 struct hdmi_spec *spec = codec->spec;
2463 vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0,
2464 INTEL_GET_VENDOR_VERB, 0);
2465 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
2468 /* enable DP1.2 mode */
2469 vendor_param |= INTEL_EN_DP12;
2470 snd_hdac_regmap_add_vendor_verb(&codec->core, INTEL_SET_VENDOR_VERB);
2471 snd_hda_codec_write_cache(codec, spec->vendor_nid, 0,
2472 INTEL_SET_VENDOR_VERB, vendor_param);
2475 /* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
2476 * Otherwise you may get severe h/w communication errors.
2478 static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2479 unsigned int power_state)
2481 if (power_state == AC_PWRST_D0) {
2482 intel_haswell_enable_all_pins(codec, false);
2483 intel_haswell_fixup_enable_dp12(codec);
2486 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
2487 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2490 /* There is a fixed mapping between audio pin node and display port.
2491 * on SNB, IVY, HSW, BSW, SKL, BXT, KBL:
2492 * Pin Widget 5 - PORT B (port = 1 in i915 driver)
2493 * Pin Widget 6 - PORT C (port = 2 in i915 driver)
2494 * Pin Widget 7 - PORT D (port = 3 in i915 driver)
2497 * Pin Widget 4 - PORT B (port = 1 in i915 driver)
2498 * Pin Widget 5 - PORT C (port = 2 in i915 driver)
2499 * Pin Widget 6 - PORT D (port = 3 in i915 driver)
2501 static int intel_base_nid(struct hda_codec *codec)
2503 switch (codec->core.vendor_id) {
2504 case 0x80860054: /* ILK */
2505 case 0x80862804: /* ILK */
2506 case 0x80862882: /* VLV */
2513 static int intel_pin2port(void *audio_ptr, int pin_nid)
2515 struct hda_codec *codec = audio_ptr;
2516 struct hdmi_spec *spec = codec->spec;
2519 if (!spec->port_num) {
2520 base_nid = intel_base_nid(codec);
2521 if (WARN_ON(pin_nid < base_nid || pin_nid >= base_nid + 3))
2523 return pin_nid - base_nid + 1; /* intel port is 1-based */
2527 * looking for the pin number in the mapping table and return
2528 * the index which indicate the port number
2530 for (i = 0; i < spec->port_num; i++) {
2531 if (pin_nid == spec->port_map[i])
2535 /* return -1 if pin number exceeds our expectation */
2536 codec_info(codec, "Can't find the HDMI/DP port for pin %d\n", pin_nid);
2540 static void intel_pin_eld_notify(void *audio_ptr, int port, int pipe)
2542 struct hda_codec *codec = audio_ptr;
2546 /* we assume only from port-B to port-D */
2547 if (port < 1 || port > 3)
2550 pin_nid = port + intel_base_nid(codec) - 1; /* intel port is 1-based */
2552 /* skip notification during system suspend (but not in runtime PM);
2553 * the state will be updated at resume
2555 if (snd_power_get_state(codec->card) != SNDRV_CTL_POWER_D0)
2557 /* ditto during suspend/resume process itself */
2558 if (snd_hdac_is_in_pm(&codec->core))
2561 snd_hdac_i915_set_bclk(&codec->bus->core);
2562 check_presence_and_report(codec, pin_nid, dev_id);
2565 /* register i915 component pin_eld_notify callback */
2566 static void register_i915_notifier(struct hda_codec *codec)
2568 struct hdmi_spec *spec = codec->spec;
2570 spec->use_acomp_notifier = true;
2571 spec->drm_audio_ops.audio_ptr = codec;
2572 /* intel_audio_codec_enable() or intel_audio_codec_disable()
2573 * will call pin_eld_notify with using audio_ptr pointer
2574 * We need make sure audio_ptr is really setup
2577 spec->drm_audio_ops.pin2port = intel_pin2port;
2578 spec->drm_audio_ops.pin_eld_notify = intel_pin_eld_notify;
2579 snd_hdac_acomp_register_notifier(&codec->bus->core,
2580 &spec->drm_audio_ops);
2583 /* setup_stream ops override for HSW+ */
2584 static int i915_hsw_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
2585 hda_nid_t pin_nid, u32 stream_tag, int format)
2587 haswell_verify_D0(codec, cvt_nid, pin_nid);
2588 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
2591 /* pin_cvt_fixup ops override for HSW+ and VLV+ */
2592 static void i915_pin_cvt_fixup(struct hda_codec *codec,
2593 struct hdmi_spec_per_pin *per_pin,
2597 snd_hda_set_dev_select(codec, per_pin->pin_nid,
2599 intel_verify_pin_cvt_connect(codec, per_pin);
2600 intel_not_share_assigned_cvt(codec, per_pin->pin_nid,
2601 per_pin->dev_id, per_pin->mux_idx);
2603 intel_not_share_assigned_cvt_nid(codec, 0, 0, cvt_nid);
2607 /* precondition and allocation for Intel codecs */
2608 static int alloc_intel_hdmi(struct hda_codec *codec)
2610 /* requires i915 binding */
2611 if (!codec->bus->core.audio_component) {
2612 codec_info(codec, "No i915 binding for Intel HDMI/DP codec\n");
2613 /* set probe_id here to prevent generic fallback binding */
2614 codec->probe_id = HDA_CODEC_ID_SKIP_PROBE;
2618 return alloc_generic_hdmi(codec);
2621 /* parse and post-process for Intel codecs */
2622 static int parse_intel_hdmi(struct hda_codec *codec)
2626 err = hdmi_parse_codec(codec);
2628 generic_spec_free(codec);
2632 generic_hdmi_init_per_pins(codec);
2633 register_i915_notifier(codec);
2637 /* Intel Haswell and onwards; audio component with eld notifier */
2638 static int intel_hsw_common_init(struct hda_codec *codec, hda_nid_t vendor_nid,
2639 const int *port_map, int port_num)
2641 struct hdmi_spec *spec;
2644 err = alloc_intel_hdmi(codec);
2648 codec->dp_mst = true;
2649 spec->dyn_pcm_assign = true;
2650 spec->vendor_nid = vendor_nid;
2651 spec->port_map = port_map;
2652 spec->port_num = port_num;
2654 intel_haswell_enable_all_pins(codec, true);
2655 intel_haswell_fixup_enable_dp12(codec);
2657 codec->display_power_control = 1;
2659 codec->patch_ops.set_power_state = haswell_set_power_state;
2660 codec->depop_delay = 0;
2661 codec->auto_runtime_pm = 1;
2663 spec->ops.setup_stream = i915_hsw_setup_stream;
2664 spec->ops.pin_cvt_fixup = i915_pin_cvt_fixup;
2666 return parse_intel_hdmi(codec);
2669 static int patch_i915_hsw_hdmi(struct hda_codec *codec)
2671 return intel_hsw_common_init(codec, 0x08, NULL, 0);
2674 static int patch_i915_glk_hdmi(struct hda_codec *codec)
2676 return intel_hsw_common_init(codec, 0x0b, NULL, 0);
2679 static int patch_i915_icl_hdmi(struct hda_codec *codec)
2682 * pin to port mapping table where the value indicate the pin number and
2683 * the index indicate the port number with 1 base.
2685 static const int map[] = {0x4, 0x6, 0x8, 0xa, 0xb};
2687 return intel_hsw_common_init(codec, 0x02, map, ARRAY_SIZE(map));
2690 /* Intel Baytrail and Braswell; with eld notifier */
2691 static int patch_i915_byt_hdmi(struct hda_codec *codec)
2693 struct hdmi_spec *spec;
2696 err = alloc_intel_hdmi(codec);
2701 /* For Valleyview/Cherryview, only the display codec is in the display
2702 * power well and can use link_power ops to request/release the power.
2704 codec->display_power_control = 1;
2706 codec->depop_delay = 0;
2707 codec->auto_runtime_pm = 1;
2709 spec->ops.pin_cvt_fixup = i915_pin_cvt_fixup;
2711 return parse_intel_hdmi(codec);
2714 /* Intel IronLake, SandyBridge and IvyBridge; with eld notifier */
2715 static int patch_i915_cpt_hdmi(struct hda_codec *codec)
2719 err = alloc_intel_hdmi(codec);
2722 return parse_intel_hdmi(codec);
2726 * Shared non-generic implementations
2729 static int simple_playback_build_pcms(struct hda_codec *codec)
2731 struct hdmi_spec *spec = codec->spec;
2732 struct hda_pcm *info;
2734 struct hda_pcm_stream *pstr;
2735 struct hdmi_spec_per_cvt *per_cvt;
2737 per_cvt = get_cvt(spec, 0);
2738 chans = get_wcaps(codec, per_cvt->cvt_nid);
2739 chans = get_wcaps_channels(chans);
2741 info = snd_hda_codec_pcm_new(codec, "HDMI 0");
2744 spec->pcm_rec[0].pcm = info;
2745 info->pcm_type = HDA_PCM_TYPE_HDMI;
2746 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2747 *pstr = spec->pcm_playback;
2748 pstr->nid = per_cvt->cvt_nid;
2749 if (pstr->channels_max <= 2 && chans && chans <= 16)
2750 pstr->channels_max = chans;
2755 /* unsolicited event for jack sensing */
2756 static void simple_hdmi_unsol_event(struct hda_codec *codec,
2759 snd_hda_jack_set_dirty_all(codec);
2760 snd_hda_jack_report_sync(codec);
2763 /* generic_hdmi_build_jack can be used for simple_hdmi, too,
2764 * as long as spec->pins[] is set correctly
2766 #define simple_hdmi_build_jack generic_hdmi_build_jack
2768 static int simple_playback_build_controls(struct hda_codec *codec)
2770 struct hdmi_spec *spec = codec->spec;
2771 struct hdmi_spec_per_cvt *per_cvt;
2774 per_cvt = get_cvt(spec, 0);
2775 err = snd_hda_create_dig_out_ctls(codec, per_cvt->cvt_nid,
2780 return simple_hdmi_build_jack(codec, 0);
2783 static int simple_playback_init(struct hda_codec *codec)
2785 struct hdmi_spec *spec = codec->spec;
2786 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
2787 hda_nid_t pin = per_pin->pin_nid;
2789 snd_hda_codec_write(codec, pin, 0,
2790 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
2791 /* some codecs require to unmute the pin */
2792 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
2793 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2795 snd_hda_jack_detect_enable(codec, pin);
2799 static void simple_playback_free(struct hda_codec *codec)
2801 struct hdmi_spec *spec = codec->spec;
2803 hdmi_array_free(spec);
2808 * Nvidia specific implementations
2811 #define Nv_VERB_SET_Channel_Allocation 0xF79
2812 #define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
2813 #define Nv_VERB_SET_Audio_Protection_On 0xF98
2814 #define Nv_VERB_SET_Audio_Protection_Off 0xF99
2816 #define nvhdmi_master_con_nid_7x 0x04
2817 #define nvhdmi_master_pin_nid_7x 0x05
2819 static const hda_nid_t nvhdmi_con_nids_7x[4] = {
2820 /*front, rear, clfe, rear_surr */
2824 static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
2825 /* set audio protect on */
2826 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2827 /* enable digital output on pin widget */
2828 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2832 static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
2833 /* set audio protect on */
2834 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2835 /* enable digital output on pin widget */
2836 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2837 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2838 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2839 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2840 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2844 #ifdef LIMITED_RATE_FMT_SUPPORT
2845 /* support only the safe format and rate */
2846 #define SUPPORTED_RATES SNDRV_PCM_RATE_48000
2847 #define SUPPORTED_MAXBPS 16
2848 #define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
2850 /* support all rates and formats */
2851 #define SUPPORTED_RATES \
2852 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
2853 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
2854 SNDRV_PCM_RATE_192000)
2855 #define SUPPORTED_MAXBPS 24
2856 #define SUPPORTED_FORMATS \
2857 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
2860 static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
2862 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
2866 static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
2868 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
2872 static const unsigned int channels_2_6_8[] = {
2876 static const unsigned int channels_2_8[] = {
2880 static const struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
2881 .count = ARRAY_SIZE(channels_2_6_8),
2882 .list = channels_2_6_8,
2886 static const struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
2887 .count = ARRAY_SIZE(channels_2_8),
2888 .list = channels_2_8,
2892 static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
2893 struct hda_codec *codec,
2894 struct snd_pcm_substream *substream)
2896 struct hdmi_spec *spec = codec->spec;
2897 const struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
2899 switch (codec->preset->vendor_id) {
2904 hw_constraints_channels = &hw_constraints_2_8_channels;
2907 hw_constraints_channels = &hw_constraints_2_6_8_channels;
2913 if (hw_constraints_channels != NULL) {
2914 snd_pcm_hw_constraint_list(substream->runtime, 0,
2915 SNDRV_PCM_HW_PARAM_CHANNELS,
2916 hw_constraints_channels);
2918 snd_pcm_hw_constraint_step(substream->runtime, 0,
2919 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
2922 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2925 static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
2926 struct hda_codec *codec,
2927 struct snd_pcm_substream *substream)
2929 struct hdmi_spec *spec = codec->spec;
2930 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2933 static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2934 struct hda_codec *codec,
2935 unsigned int stream_tag,
2936 unsigned int format,
2937 struct snd_pcm_substream *substream)
2939 struct hdmi_spec *spec = codec->spec;
2940 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2941 stream_tag, format, substream);
2944 static const struct hda_pcm_stream simple_pcm_playback = {
2949 .open = simple_playback_pcm_open,
2950 .close = simple_playback_pcm_close,
2951 .prepare = simple_playback_pcm_prepare
2955 static const struct hda_codec_ops simple_hdmi_patch_ops = {
2956 .build_controls = simple_playback_build_controls,
2957 .build_pcms = simple_playback_build_pcms,
2958 .init = simple_playback_init,
2959 .free = simple_playback_free,
2960 .unsol_event = simple_hdmi_unsol_event,
2963 static int patch_simple_hdmi(struct hda_codec *codec,
2964 hda_nid_t cvt_nid, hda_nid_t pin_nid)
2966 struct hdmi_spec *spec;
2967 struct hdmi_spec_per_cvt *per_cvt;
2968 struct hdmi_spec_per_pin *per_pin;
2970 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2975 hdmi_array_init(spec, 1);
2977 spec->multiout.num_dacs = 0; /* no analog */
2978 spec->multiout.max_channels = 2;
2979 spec->multiout.dig_out_nid = cvt_nid;
2982 per_pin = snd_array_new(&spec->pins);
2983 per_cvt = snd_array_new(&spec->cvts);
2984 if (!per_pin || !per_cvt) {
2985 simple_playback_free(codec);
2988 per_cvt->cvt_nid = cvt_nid;
2989 per_pin->pin_nid = pin_nid;
2990 spec->pcm_playback = simple_pcm_playback;
2992 codec->patch_ops = simple_hdmi_patch_ops;
2997 static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
3000 unsigned int chanmask;
3001 int chan = channels ? (channels - 1) : 1;
3020 /* Set the audio infoframe channel allocation and checksum fields. The
3021 * channel count is computed implicitly by the hardware. */
3022 snd_hda_codec_write(codec, 0x1, 0,
3023 Nv_VERB_SET_Channel_Allocation, chanmask);
3025 snd_hda_codec_write(codec, 0x1, 0,
3026 Nv_VERB_SET_Info_Frame_Checksum,
3027 (0x71 - chan - chanmask));
3030 static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
3031 struct hda_codec *codec,
3032 struct snd_pcm_substream *substream)
3034 struct hdmi_spec *spec = codec->spec;
3037 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
3038 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
3039 for (i = 0; i < 4; i++) {
3040 /* set the stream id */
3041 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3042 AC_VERB_SET_CHANNEL_STREAMID, 0);
3043 /* set the stream format */
3044 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3045 AC_VERB_SET_STREAM_FORMAT, 0);
3048 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
3049 * streams are disabled. */
3050 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3052 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3055 static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
3056 struct hda_codec *codec,
3057 unsigned int stream_tag,
3058 unsigned int format,
3059 struct snd_pcm_substream *substream)
3062 unsigned int dataDCC2, channel_id;
3064 struct hdmi_spec *spec = codec->spec;
3065 struct hda_spdif_out *spdif;
3066 struct hdmi_spec_per_cvt *per_cvt;
3068 mutex_lock(&codec->spdif_mutex);
3069 per_cvt = get_cvt(spec, 0);
3070 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
3072 chs = substream->runtime->channels;
3076 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
3077 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
3078 snd_hda_codec_write(codec,
3079 nvhdmi_master_con_nid_7x,
3081 AC_VERB_SET_DIGI_CONVERT_1,
3082 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
3084 /* set the stream id */
3085 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3086 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
3088 /* set the stream format */
3089 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3090 AC_VERB_SET_STREAM_FORMAT, format);
3092 /* turn on again (if needed) */
3093 /* enable and set the channel status audio/data flag */
3094 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
3095 snd_hda_codec_write(codec,
3096 nvhdmi_master_con_nid_7x,
3098 AC_VERB_SET_DIGI_CONVERT_1,
3099 spdif->ctls & 0xff);
3100 snd_hda_codec_write(codec,
3101 nvhdmi_master_con_nid_7x,
3103 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3106 for (i = 0; i < 4; i++) {
3112 /* turn off SPDIF once;
3113 *otherwise the IEC958 bits won't be updated
3115 if (codec->spdif_status_reset &&
3116 (spdif->ctls & AC_DIG1_ENABLE))
3117 snd_hda_codec_write(codec,
3118 nvhdmi_con_nids_7x[i],
3120 AC_VERB_SET_DIGI_CONVERT_1,
3121 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
3122 /* set the stream id */
3123 snd_hda_codec_write(codec,
3124 nvhdmi_con_nids_7x[i],
3126 AC_VERB_SET_CHANNEL_STREAMID,
3127 (stream_tag << 4) | channel_id);
3128 /* set the stream format */
3129 snd_hda_codec_write(codec,
3130 nvhdmi_con_nids_7x[i],
3132 AC_VERB_SET_STREAM_FORMAT,
3134 /* turn on again (if needed) */
3135 /* enable and set the channel status audio/data flag */
3136 if (codec->spdif_status_reset &&
3137 (spdif->ctls & AC_DIG1_ENABLE)) {
3138 snd_hda_codec_write(codec,
3139 nvhdmi_con_nids_7x[i],
3141 AC_VERB_SET_DIGI_CONVERT_1,
3142 spdif->ctls & 0xff);
3143 snd_hda_codec_write(codec,
3144 nvhdmi_con_nids_7x[i],
3146 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3150 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
3152 mutex_unlock(&codec->spdif_mutex);
3156 static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
3160 .nid = nvhdmi_master_con_nid_7x,
3161 .rates = SUPPORTED_RATES,
3162 .maxbps = SUPPORTED_MAXBPS,
3163 .formats = SUPPORTED_FORMATS,
3165 .open = simple_playback_pcm_open,
3166 .close = nvhdmi_8ch_7x_pcm_close,
3167 .prepare = nvhdmi_8ch_7x_pcm_prepare
3171 static int patch_nvhdmi_2ch(struct hda_codec *codec)
3173 struct hdmi_spec *spec;
3174 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
3175 nvhdmi_master_pin_nid_7x);
3179 codec->patch_ops.init = nvhdmi_7x_init_2ch;
3180 /* override the PCM rates, etc, as the codec doesn't give full list */
3182 spec->pcm_playback.rates = SUPPORTED_RATES;
3183 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
3184 spec->pcm_playback.formats = SUPPORTED_FORMATS;
3188 static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
3190 struct hdmi_spec *spec = codec->spec;
3191 int err = simple_playback_build_pcms(codec);
3193 struct hda_pcm *info = get_pcm_rec(spec, 0);
3194 info->own_chmap = true;
3199 static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
3201 struct hdmi_spec *spec = codec->spec;
3202 struct hda_pcm *info;
3203 struct snd_pcm_chmap *chmap;
3206 err = simple_playback_build_controls(codec);
3210 /* add channel maps */
3211 info = get_pcm_rec(spec, 0);
3212 err = snd_pcm_add_chmap_ctls(info->pcm,
3213 SNDRV_PCM_STREAM_PLAYBACK,
3214 snd_pcm_alt_chmaps, 8, 0, &chmap);
3217 switch (codec->preset->vendor_id) {
3222 chmap->channel_mask = (1U << 2) | (1U << 8);
3225 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
3230 static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
3232 struct hdmi_spec *spec;
3233 int err = patch_nvhdmi_2ch(codec);
3237 spec->multiout.max_channels = 8;
3238 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
3239 codec->patch_ops.init = nvhdmi_7x_init_8ch;
3240 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
3241 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
3243 /* Initialize the audio infoframe channel mask and checksum to something
3245 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3251 * NVIDIA codecs ignore ASP mapping for 2ch - confirmed on:
3255 static int nvhdmi_chmap_cea_alloc_validate_get_type(struct hdac_chmap *chmap,
3256 struct hdac_cea_channel_speaker_allocation *cap, int channels)
3258 if (cap->ca_index == 0x00 && channels == 2)
3259 return SNDRV_CTL_TLVT_CHMAP_FIXED;
3261 /* If the speaker allocation matches the channel count, it is OK. */
3262 if (cap->channels != channels)
3265 /* all channels are remappable freely */
3266 return SNDRV_CTL_TLVT_CHMAP_VAR;
3269 static int nvhdmi_chmap_validate(struct hdac_chmap *chmap,
3270 int ca, int chs, unsigned char *map)
3272 if (ca == 0x00 && (map[0] != SNDRV_CHMAP_FL || map[1] != SNDRV_CHMAP_FR))
3278 static int patch_nvhdmi(struct hda_codec *codec)
3280 struct hdmi_spec *spec;
3283 err = patch_generic_hdmi(codec);
3288 spec->dyn_pin_out = true;
3290 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
3291 nvhdmi_chmap_cea_alloc_validate_get_type;
3292 spec->chmap.ops.chmap_validate = nvhdmi_chmap_validate;
3298 * The HDA codec on NVIDIA Tegra contains two scratch registers that are
3299 * accessed using vendor-defined verbs. These registers can be used for
3300 * interoperability between the HDA and HDMI drivers.
3303 /* Audio Function Group node */
3304 #define NVIDIA_AFG_NID 0x01
3307 * The SCRATCH0 register is used to notify the HDMI codec of changes in audio
3308 * format. On Tegra, bit 31 is used as a trigger that causes an interrupt to
3309 * be raised in the HDMI codec. The remainder of the bits is arbitrary. This
3310 * implementation stores the HDA format (see AC_FMT_*) in bits [15:0] and an
3311 * additional bit (at position 30) to signal the validity of the format.
3313 * | 31 | 30 | 29 16 | 15 0 |
3314 * +---------+-------+--------+--------+
3315 * | TRIGGER | VALID | UNUSED | FORMAT |
3316 * +-----------------------------------|
3318 * Note that for the trigger bit to take effect it needs to change value
3319 * (i.e. it needs to be toggled).
3321 #define NVIDIA_GET_SCRATCH0 0xfa6
3322 #define NVIDIA_SET_SCRATCH0_BYTE0 0xfa7
3323 #define NVIDIA_SET_SCRATCH0_BYTE1 0xfa8
3324 #define NVIDIA_SET_SCRATCH0_BYTE2 0xfa9
3325 #define NVIDIA_SET_SCRATCH0_BYTE3 0xfaa
3326 #define NVIDIA_SCRATCH_TRIGGER (1 << 7)
3327 #define NVIDIA_SCRATCH_VALID (1 << 6)
3329 #define NVIDIA_GET_SCRATCH1 0xfab
3330 #define NVIDIA_SET_SCRATCH1_BYTE0 0xfac
3331 #define NVIDIA_SET_SCRATCH1_BYTE1 0xfad
3332 #define NVIDIA_SET_SCRATCH1_BYTE2 0xfae
3333 #define NVIDIA_SET_SCRATCH1_BYTE3 0xfaf
3336 * The format parameter is the HDA audio format (see AC_FMT_*). If set to 0,
3337 * the format is invalidated so that the HDMI codec can be disabled.
3339 static void tegra_hdmi_set_format(struct hda_codec *codec, unsigned int format)
3343 /* bits [31:30] contain the trigger and valid bits */
3344 value = snd_hda_codec_read(codec, NVIDIA_AFG_NID, 0,
3345 NVIDIA_GET_SCRATCH0, 0);
3346 value = (value >> 24) & 0xff;
3348 /* bits [15:0] are used to store the HDA format */
3349 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3350 NVIDIA_SET_SCRATCH0_BYTE0,
3351 (format >> 0) & 0xff);
3352 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3353 NVIDIA_SET_SCRATCH0_BYTE1,
3354 (format >> 8) & 0xff);
3356 /* bits [16:24] are unused */
3357 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3358 NVIDIA_SET_SCRATCH0_BYTE2, 0);
3361 * Bit 30 signals that the data is valid and hence that HDMI audio can
3365 value &= ~NVIDIA_SCRATCH_VALID;
3367 value |= NVIDIA_SCRATCH_VALID;
3370 * Whenever the trigger bit is toggled, an interrupt is raised in the
3371 * HDMI codec. The HDMI driver will use that as trigger to update its
3374 value ^= NVIDIA_SCRATCH_TRIGGER;
3376 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3377 NVIDIA_SET_SCRATCH0_BYTE3, value);
3380 static int tegra_hdmi_pcm_prepare(struct hda_pcm_stream *hinfo,
3381 struct hda_codec *codec,
3382 unsigned int stream_tag,
3383 unsigned int format,
3384 struct snd_pcm_substream *substream)
3388 err = generic_hdmi_playback_pcm_prepare(hinfo, codec, stream_tag,
3393 /* notify the HDMI codec of the format change */
3394 tegra_hdmi_set_format(codec, format);
3399 static int tegra_hdmi_pcm_cleanup(struct hda_pcm_stream *hinfo,
3400 struct hda_codec *codec,
3401 struct snd_pcm_substream *substream)
3403 /* invalidate the format in the HDMI codec */
3404 tegra_hdmi_set_format(codec, 0);
3406 return generic_hdmi_playback_pcm_cleanup(hinfo, codec, substream);
3409 static struct hda_pcm *hda_find_pcm_by_type(struct hda_codec *codec, int type)
3411 struct hdmi_spec *spec = codec->spec;
3414 for (i = 0; i < spec->num_pins; i++) {
3415 struct hda_pcm *pcm = get_pcm_rec(spec, i);
3417 if (pcm->pcm_type == type)
3424 static int tegra_hdmi_build_pcms(struct hda_codec *codec)
3426 struct hda_pcm_stream *stream;
3427 struct hda_pcm *pcm;
3430 err = generic_hdmi_build_pcms(codec);
3434 pcm = hda_find_pcm_by_type(codec, HDA_PCM_TYPE_HDMI);
3439 * Override ->prepare() and ->cleanup() operations to notify the HDMI
3440 * codec about format changes.
3442 stream = &pcm->stream[SNDRV_PCM_STREAM_PLAYBACK];
3443 stream->ops.prepare = tegra_hdmi_pcm_prepare;
3444 stream->ops.cleanup = tegra_hdmi_pcm_cleanup;
3449 static int patch_tegra_hdmi(struct hda_codec *codec)
3453 err = patch_generic_hdmi(codec);
3457 codec->patch_ops.build_pcms = tegra_hdmi_build_pcms;
3463 * ATI/AMD-specific implementations
3466 #define is_amdhdmi_rev3_or_later(codec) \
3467 ((codec)->core.vendor_id == 0x1002aa01 && \
3468 ((codec)->core.revision_id & 0xff00) >= 0x0300)
3469 #define has_amd_full_remap_support(codec) is_amdhdmi_rev3_or_later(codec)
3471 /* ATI/AMD specific HDA pin verbs, see the AMD HDA Verbs specification */
3472 #define ATI_VERB_SET_CHANNEL_ALLOCATION 0x771
3473 #define ATI_VERB_SET_DOWNMIX_INFO 0x772
3474 #define ATI_VERB_SET_MULTICHANNEL_01 0x777
3475 #define ATI_VERB_SET_MULTICHANNEL_23 0x778
3476 #define ATI_VERB_SET_MULTICHANNEL_45 0x779
3477 #define ATI_VERB_SET_MULTICHANNEL_67 0x77a
3478 #define ATI_VERB_SET_HBR_CONTROL 0x77c
3479 #define ATI_VERB_SET_MULTICHANNEL_1 0x785
3480 #define ATI_VERB_SET_MULTICHANNEL_3 0x786
3481 #define ATI_VERB_SET_MULTICHANNEL_5 0x787
3482 #define ATI_VERB_SET_MULTICHANNEL_7 0x788
3483 #define ATI_VERB_SET_MULTICHANNEL_MODE 0x789
3484 #define ATI_VERB_GET_CHANNEL_ALLOCATION 0xf71
3485 #define ATI_VERB_GET_DOWNMIX_INFO 0xf72
3486 #define ATI_VERB_GET_MULTICHANNEL_01 0xf77
3487 #define ATI_VERB_GET_MULTICHANNEL_23 0xf78
3488 #define ATI_VERB_GET_MULTICHANNEL_45 0xf79
3489 #define ATI_VERB_GET_MULTICHANNEL_67 0xf7a
3490 #define ATI_VERB_GET_HBR_CONTROL 0xf7c
3491 #define ATI_VERB_GET_MULTICHANNEL_1 0xf85
3492 #define ATI_VERB_GET_MULTICHANNEL_3 0xf86
3493 #define ATI_VERB_GET_MULTICHANNEL_5 0xf87
3494 #define ATI_VERB_GET_MULTICHANNEL_7 0xf88
3495 #define ATI_VERB_GET_MULTICHANNEL_MODE 0xf89
3497 /* AMD specific HDA cvt verbs */
3498 #define ATI_VERB_SET_RAMP_RATE 0x770
3499 #define ATI_VERB_GET_RAMP_RATE 0xf70
3501 #define ATI_OUT_ENABLE 0x1
3503 #define ATI_MULTICHANNEL_MODE_PAIRED 0
3504 #define ATI_MULTICHANNEL_MODE_SINGLE 1
3506 #define ATI_HBR_CAPABLE 0x01
3507 #define ATI_HBR_ENABLE 0x10
3509 static int atihdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
3510 unsigned char *buf, int *eld_size)
3512 /* call hda_eld.c ATI/AMD-specific function */
3513 return snd_hdmi_get_eld_ati(codec, nid, buf, eld_size,
3514 is_amdhdmi_rev3_or_later(codec));
3517 static void atihdmi_pin_setup_infoframe(struct hda_codec *codec, hda_nid_t pin_nid, int ca,
3518 int active_channels, int conn_type)
3520 snd_hda_codec_write(codec, pin_nid, 0, ATI_VERB_SET_CHANNEL_ALLOCATION, ca);
3523 static int atihdmi_paired_swap_fc_lfe(int pos)
3526 * ATI/AMD have automatic FC/LFE swap built-in
3527 * when in pairwise mapping mode.
3531 /* see channel_allocations[].speakers[] */
3540 static int atihdmi_paired_chmap_validate(struct hdac_chmap *chmap,
3541 int ca, int chs, unsigned char *map)
3543 struct hdac_cea_channel_speaker_allocation *cap;
3546 /* check that only channel pairs need to be remapped on old pre-rev3 ATI/AMD */
3548 cap = snd_hdac_get_ch_alloc_from_ca(ca);
3549 for (i = 0; i < chs; ++i) {
3550 int mask = snd_hdac_chmap_to_spk_mask(map[i]);
3552 bool companion_ok = false;
3557 for (j = 0 + i % 2; j < 8; j += 2) {
3558 int chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j);
3559 if (cap->speakers[chan_idx] == mask) {
3560 /* channel is in a supported position */
3563 if (i % 2 == 0 && i + 1 < chs) {
3564 /* even channel, check the odd companion */
3565 int comp_chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j + 1);
3566 int comp_mask_req = snd_hdac_chmap_to_spk_mask(map[i+1]);
3567 int comp_mask_act = cap->speakers[comp_chan_idx];
3569 if (comp_mask_req == comp_mask_act)
3570 companion_ok = true;
3582 i++; /* companion channel already checked */
3588 static int atihdmi_pin_set_slot_channel(struct hdac_device *hdac,
3589 hda_nid_t pin_nid, int hdmi_slot, int stream_channel)
3591 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
3593 int ati_channel_setup = 0;
3598 if (!has_amd_full_remap_support(codec)) {
3599 hdmi_slot = atihdmi_paired_swap_fc_lfe(hdmi_slot);
3601 /* In case this is an odd slot but without stream channel, do not
3602 * disable the slot since the corresponding even slot could have a
3603 * channel. In case neither have a channel, the slot pair will be
3604 * disabled when this function is called for the even slot. */
3605 if (hdmi_slot % 2 != 0 && stream_channel == 0xf)
3608 hdmi_slot -= hdmi_slot % 2;
3610 if (stream_channel != 0xf)
3611 stream_channel -= stream_channel % 2;
3614 verb = ATI_VERB_SET_MULTICHANNEL_01 + hdmi_slot/2 + (hdmi_slot % 2) * 0x00e;
3616 /* ati_channel_setup format: [7..4] = stream_channel_id, [1] = mute, [0] = enable */
3618 if (stream_channel != 0xf)
3619 ati_channel_setup = (stream_channel << 4) | ATI_OUT_ENABLE;
3621 return snd_hda_codec_write(codec, pin_nid, 0, verb, ati_channel_setup);
3624 static int atihdmi_pin_get_slot_channel(struct hdac_device *hdac,
3625 hda_nid_t pin_nid, int asp_slot)
3627 struct hda_codec *codec = container_of(hdac, struct hda_codec, core);
3628 bool was_odd = false;
3629 int ati_asp_slot = asp_slot;
3631 int ati_channel_setup;
3636 if (!has_amd_full_remap_support(codec)) {
3637 ati_asp_slot = atihdmi_paired_swap_fc_lfe(asp_slot);
3638 if (ati_asp_slot % 2 != 0) {
3644 verb = ATI_VERB_GET_MULTICHANNEL_01 + ati_asp_slot/2 + (ati_asp_slot % 2) * 0x00e;
3646 ati_channel_setup = snd_hda_codec_read(codec, pin_nid, 0, verb, 0);
3648 if (!(ati_channel_setup & ATI_OUT_ENABLE))
3651 return ((ati_channel_setup & 0xf0) >> 4) + !!was_odd;
3654 static int atihdmi_paired_chmap_cea_alloc_validate_get_type(
3655 struct hdac_chmap *chmap,
3656 struct hdac_cea_channel_speaker_allocation *cap,
3662 * Pre-rev3 ATI/AMD codecs operate in a paired channel mode, so
3663 * we need to take that into account (a single channel may take 2
3664 * channel slots if we need to carry a silent channel next to it).
3665 * On Rev3+ AMD codecs this function is not used.
3669 /* We only produce even-numbered channel count TLVs */
3670 if ((channels % 2) != 0)
3673 for (c = 0; c < 7; c += 2) {
3674 if (cap->speakers[c] || cap->speakers[c+1])
3678 if (chanpairs * 2 != channels)
3681 return SNDRV_CTL_TLVT_CHMAP_PAIRED;
3684 static void atihdmi_paired_cea_alloc_to_tlv_chmap(struct hdac_chmap *hchmap,
3685 struct hdac_cea_channel_speaker_allocation *cap,
3686 unsigned int *chmap, int channels)
3688 /* produce paired maps for pre-rev3 ATI/AMD codecs */
3692 for (c = 7; c >= 0; c--) {
3693 int chan = 7 - atihdmi_paired_swap_fc_lfe(7 - c);
3694 int spk = cap->speakers[chan];
3696 /* add N/A channel if the companion channel is occupied */
3697 if (cap->speakers[chan + (chan % 2 ? -1 : 1)])
3698 chmap[count++] = SNDRV_CHMAP_NA;
3703 chmap[count++] = snd_hdac_spk_to_chmap(spk);
3706 WARN_ON(count != channels);
3709 static int atihdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
3712 int hbr_ctl, hbr_ctl_new;
3714 hbr_ctl = snd_hda_codec_read(codec, pin_nid, 0, ATI_VERB_GET_HBR_CONTROL, 0);
3715 if (hbr_ctl >= 0 && (hbr_ctl & ATI_HBR_CAPABLE)) {
3717 hbr_ctl_new = hbr_ctl | ATI_HBR_ENABLE;
3719 hbr_ctl_new = hbr_ctl & ~ATI_HBR_ENABLE;
3722 "atihdmi_pin_hbr_setup: NID=0x%x, %shbr-ctl=0x%x\n",
3724 hbr_ctl == hbr_ctl_new ? "" : "new-",
3727 if (hbr_ctl != hbr_ctl_new)
3728 snd_hda_codec_write(codec, pin_nid, 0,
3729 ATI_VERB_SET_HBR_CONTROL,
3738 static int atihdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
3739 hda_nid_t pin_nid, u32 stream_tag, int format)
3742 if (is_amdhdmi_rev3_or_later(codec)) {
3743 int ramp_rate = 180; /* default as per AMD spec */
3744 /* disable ramp-up/down for non-pcm as per AMD spec */
3745 if (format & AC_FMT_TYPE_NON_PCM)
3748 snd_hda_codec_write(codec, cvt_nid, 0, ATI_VERB_SET_RAMP_RATE, ramp_rate);
3751 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
3755 static int atihdmi_init(struct hda_codec *codec)
3757 struct hdmi_spec *spec = codec->spec;
3760 err = generic_hdmi_init(codec);
3765 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
3766 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
3768 /* make sure downmix information in infoframe is zero */
3769 snd_hda_codec_write(codec, per_pin->pin_nid, 0, ATI_VERB_SET_DOWNMIX_INFO, 0);
3771 /* enable channel-wise remap mode if supported */
3772 if (has_amd_full_remap_support(codec))
3773 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
3774 ATI_VERB_SET_MULTICHANNEL_MODE,
3775 ATI_MULTICHANNEL_MODE_SINGLE);
3781 static int patch_atihdmi(struct hda_codec *codec)
3783 struct hdmi_spec *spec;
3784 struct hdmi_spec_per_cvt *per_cvt;
3787 err = patch_generic_hdmi(codec);
3792 codec->patch_ops.init = atihdmi_init;
3796 spec->ops.pin_get_eld = atihdmi_pin_get_eld;
3797 spec->ops.pin_setup_infoframe = atihdmi_pin_setup_infoframe;
3798 spec->ops.pin_hbr_setup = atihdmi_pin_hbr_setup;
3799 spec->ops.setup_stream = atihdmi_setup_stream;
3801 spec->chmap.ops.pin_get_slot_channel = atihdmi_pin_get_slot_channel;
3802 spec->chmap.ops.pin_set_slot_channel = atihdmi_pin_set_slot_channel;
3804 if (!has_amd_full_remap_support(codec)) {
3805 /* override to ATI/AMD-specific versions with pairwise mapping */
3806 spec->chmap.ops.chmap_cea_alloc_validate_get_type =
3807 atihdmi_paired_chmap_cea_alloc_validate_get_type;
3808 spec->chmap.ops.cea_alloc_to_tlv_chmap =
3809 atihdmi_paired_cea_alloc_to_tlv_chmap;
3810 spec->chmap.ops.chmap_validate = atihdmi_paired_chmap_validate;
3813 /* ATI/AMD converters do not advertise all of their capabilities */
3814 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
3815 per_cvt = get_cvt(spec, cvt_idx);
3816 per_cvt->channels_max = max(per_cvt->channels_max, 8u);
3817 per_cvt->rates |= SUPPORTED_RATES;
3818 per_cvt->formats |= SUPPORTED_FORMATS;
3819 per_cvt->maxbps = max(per_cvt->maxbps, 24u);
3822 spec->chmap.channels_max = max(spec->chmap.channels_max, 8u);
3824 /* AMD GPUs have neither EPSS nor CLKSTOP bits, hence preventing
3825 * the link-down as is. Tell the core to allow it.
3827 codec->link_down_at_suspend = 1;
3832 /* VIA HDMI Implementation */
3833 #define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
3834 #define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
3836 static int patch_via_hdmi(struct hda_codec *codec)
3838 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
3844 static const struct hda_device_id snd_hda_id_hdmi[] = {
3845 HDA_CODEC_ENTRY(0x1002793c, "RS600 HDMI", patch_atihdmi),
3846 HDA_CODEC_ENTRY(0x10027919, "RS600 HDMI", patch_atihdmi),
3847 HDA_CODEC_ENTRY(0x1002791a, "RS690/780 HDMI", patch_atihdmi),
3848 HDA_CODEC_ENTRY(0x1002aa01, "R6xx HDMI", patch_atihdmi),
3849 HDA_CODEC_ENTRY(0x10951390, "SiI1390 HDMI", patch_generic_hdmi),
3850 HDA_CODEC_ENTRY(0x10951392, "SiI1392 HDMI", patch_generic_hdmi),
3851 HDA_CODEC_ENTRY(0x17e80047, "Chrontel HDMI", patch_generic_hdmi),
3852 HDA_CODEC_ENTRY(0x10de0001, "MCP73 HDMI", patch_nvhdmi_2ch),
3853 HDA_CODEC_ENTRY(0x10de0002, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3854 HDA_CODEC_ENTRY(0x10de0003, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3855 HDA_CODEC_ENTRY(0x10de0004, "GPU 04 HDMI", patch_nvhdmi_8ch_7x),
3856 HDA_CODEC_ENTRY(0x10de0005, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3857 HDA_CODEC_ENTRY(0x10de0006, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3858 HDA_CODEC_ENTRY(0x10de0007, "MCP79/7A HDMI", patch_nvhdmi_8ch_7x),
3859 HDA_CODEC_ENTRY(0x10de0008, "GPU 08 HDMI/DP", patch_nvhdmi),
3860 HDA_CODEC_ENTRY(0x10de0009, "GPU 09 HDMI/DP", patch_nvhdmi),
3861 HDA_CODEC_ENTRY(0x10de000a, "GPU 0a HDMI/DP", patch_nvhdmi),
3862 HDA_CODEC_ENTRY(0x10de000b, "GPU 0b HDMI/DP", patch_nvhdmi),
3863 HDA_CODEC_ENTRY(0x10de000c, "MCP89 HDMI", patch_nvhdmi),
3864 HDA_CODEC_ENTRY(0x10de000d, "GPU 0d HDMI/DP", patch_nvhdmi),
3865 HDA_CODEC_ENTRY(0x10de0010, "GPU 10 HDMI/DP", patch_nvhdmi),
3866 HDA_CODEC_ENTRY(0x10de0011, "GPU 11 HDMI/DP", patch_nvhdmi),
3867 HDA_CODEC_ENTRY(0x10de0012, "GPU 12 HDMI/DP", patch_nvhdmi),
3868 HDA_CODEC_ENTRY(0x10de0013, "GPU 13 HDMI/DP", patch_nvhdmi),
3869 HDA_CODEC_ENTRY(0x10de0014, "GPU 14 HDMI/DP", patch_nvhdmi),
3870 HDA_CODEC_ENTRY(0x10de0015, "GPU 15 HDMI/DP", patch_nvhdmi),
3871 HDA_CODEC_ENTRY(0x10de0016, "GPU 16 HDMI/DP", patch_nvhdmi),
3872 /* 17 is known to be absent */
3873 HDA_CODEC_ENTRY(0x10de0018, "GPU 18 HDMI/DP", patch_nvhdmi),
3874 HDA_CODEC_ENTRY(0x10de0019, "GPU 19 HDMI/DP", patch_nvhdmi),
3875 HDA_CODEC_ENTRY(0x10de001a, "GPU 1a HDMI/DP", patch_nvhdmi),
3876 HDA_CODEC_ENTRY(0x10de001b, "GPU 1b HDMI/DP", patch_nvhdmi),
3877 HDA_CODEC_ENTRY(0x10de001c, "GPU 1c HDMI/DP", patch_nvhdmi),
3878 HDA_CODEC_ENTRY(0x10de0020, "Tegra30 HDMI", patch_tegra_hdmi),
3879 HDA_CODEC_ENTRY(0x10de0022, "Tegra114 HDMI", patch_tegra_hdmi),
3880 HDA_CODEC_ENTRY(0x10de0028, "Tegra124 HDMI", patch_tegra_hdmi),
3881 HDA_CODEC_ENTRY(0x10de0029, "Tegra210 HDMI/DP", patch_tegra_hdmi),
3882 HDA_CODEC_ENTRY(0x10de002d, "Tegra186 HDMI/DP0", patch_tegra_hdmi),
3883 HDA_CODEC_ENTRY(0x10de002e, "Tegra186 HDMI/DP1", patch_tegra_hdmi),
3884 HDA_CODEC_ENTRY(0x10de002f, "Tegra194 HDMI/DP2", patch_tegra_hdmi),
3885 HDA_CODEC_ENTRY(0x10de0030, "Tegra194 HDMI/DP3", patch_tegra_hdmi),
3886 HDA_CODEC_ENTRY(0x10de0040, "GPU 40 HDMI/DP", patch_nvhdmi),
3887 HDA_CODEC_ENTRY(0x10de0041, "GPU 41 HDMI/DP", patch_nvhdmi),
3888 HDA_CODEC_ENTRY(0x10de0042, "GPU 42 HDMI/DP", patch_nvhdmi),
3889 HDA_CODEC_ENTRY(0x10de0043, "GPU 43 HDMI/DP", patch_nvhdmi),
3890 HDA_CODEC_ENTRY(0x10de0044, "GPU 44 HDMI/DP", patch_nvhdmi),
3891 HDA_CODEC_ENTRY(0x10de0045, "GPU 45 HDMI/DP", patch_nvhdmi),
3892 HDA_CODEC_ENTRY(0x10de0050, "GPU 50 HDMI/DP", patch_nvhdmi),
3893 HDA_CODEC_ENTRY(0x10de0051, "GPU 51 HDMI/DP", patch_nvhdmi),
3894 HDA_CODEC_ENTRY(0x10de0052, "GPU 52 HDMI/DP", patch_nvhdmi),
3895 HDA_CODEC_ENTRY(0x10de0060, "GPU 60 HDMI/DP", patch_nvhdmi),
3896 HDA_CODEC_ENTRY(0x10de0061, "GPU 61 HDMI/DP", patch_nvhdmi),
3897 HDA_CODEC_ENTRY(0x10de0062, "GPU 62 HDMI/DP", patch_nvhdmi),
3898 HDA_CODEC_ENTRY(0x10de0067, "MCP67 HDMI", patch_nvhdmi_2ch),
3899 HDA_CODEC_ENTRY(0x10de0070, "GPU 70 HDMI/DP", patch_nvhdmi),
3900 HDA_CODEC_ENTRY(0x10de0071, "GPU 71 HDMI/DP", patch_nvhdmi),
3901 HDA_CODEC_ENTRY(0x10de0072, "GPU 72 HDMI/DP", patch_nvhdmi),
3902 HDA_CODEC_ENTRY(0x10de0073, "GPU 73 HDMI/DP", patch_nvhdmi),
3903 HDA_CODEC_ENTRY(0x10de0074, "GPU 74 HDMI/DP", patch_nvhdmi),
3904 HDA_CODEC_ENTRY(0x10de0076, "GPU 76 HDMI/DP", patch_nvhdmi),
3905 HDA_CODEC_ENTRY(0x10de007b, "GPU 7b HDMI/DP", patch_nvhdmi),
3906 HDA_CODEC_ENTRY(0x10de007c, "GPU 7c HDMI/DP", patch_nvhdmi),
3907 HDA_CODEC_ENTRY(0x10de007d, "GPU 7d HDMI/DP", patch_nvhdmi),
3908 HDA_CODEC_ENTRY(0x10de007e, "GPU 7e HDMI/DP", patch_nvhdmi),
3909 HDA_CODEC_ENTRY(0x10de0080, "GPU 80 HDMI/DP", patch_nvhdmi),
3910 HDA_CODEC_ENTRY(0x10de0081, "GPU 81 HDMI/DP", patch_nvhdmi),
3911 HDA_CODEC_ENTRY(0x10de0082, "GPU 82 HDMI/DP", patch_nvhdmi),
3912 HDA_CODEC_ENTRY(0x10de0083, "GPU 83 HDMI/DP", patch_nvhdmi),
3913 HDA_CODEC_ENTRY(0x10de0084, "GPU 84 HDMI/DP", patch_nvhdmi),
3914 HDA_CODEC_ENTRY(0x10de0090, "GPU 90 HDMI/DP", patch_nvhdmi),
3915 HDA_CODEC_ENTRY(0x10de0091, "GPU 91 HDMI/DP", patch_nvhdmi),
3916 HDA_CODEC_ENTRY(0x10de0092, "GPU 92 HDMI/DP", patch_nvhdmi),
3917 HDA_CODEC_ENTRY(0x10de0093, "GPU 93 HDMI/DP", patch_nvhdmi),
3918 HDA_CODEC_ENTRY(0x10de0094, "GPU 94 HDMI/DP", patch_nvhdmi),
3919 HDA_CODEC_ENTRY(0x10de0095, "GPU 95 HDMI/DP", patch_nvhdmi),
3920 HDA_CODEC_ENTRY(0x10de0097, "GPU 97 HDMI/DP", patch_nvhdmi),
3921 HDA_CODEC_ENTRY(0x10de0098, "GPU 98 HDMI/DP", patch_nvhdmi),
3922 HDA_CODEC_ENTRY(0x10de0099, "GPU 99 HDMI/DP", patch_nvhdmi),
3923 HDA_CODEC_ENTRY(0x10de8001, "MCP73 HDMI", patch_nvhdmi_2ch),
3924 HDA_CODEC_ENTRY(0x10de8067, "MCP67/68 HDMI", patch_nvhdmi_2ch),
3925 HDA_CODEC_ENTRY(0x11069f80, "VX900 HDMI/DP", patch_via_hdmi),
3926 HDA_CODEC_ENTRY(0x11069f81, "VX900 HDMI/DP", patch_via_hdmi),
3927 HDA_CODEC_ENTRY(0x11069f84, "VX11 HDMI/DP", patch_generic_hdmi),
3928 HDA_CODEC_ENTRY(0x11069f85, "VX11 HDMI/DP", patch_generic_hdmi),
3929 HDA_CODEC_ENTRY(0x80860054, "IbexPeak HDMI", patch_i915_cpt_hdmi),
3930 HDA_CODEC_ENTRY(0x80862800, "Geminilake HDMI", patch_i915_glk_hdmi),
3931 HDA_CODEC_ENTRY(0x80862801, "Bearlake HDMI", patch_generic_hdmi),
3932 HDA_CODEC_ENTRY(0x80862802, "Cantiga HDMI", patch_generic_hdmi),
3933 HDA_CODEC_ENTRY(0x80862803, "Eaglelake HDMI", patch_generic_hdmi),
3934 HDA_CODEC_ENTRY(0x80862804, "IbexPeak HDMI", patch_i915_cpt_hdmi),
3935 HDA_CODEC_ENTRY(0x80862805, "CougarPoint HDMI", patch_i915_cpt_hdmi),
3936 HDA_CODEC_ENTRY(0x80862806, "PantherPoint HDMI", patch_i915_cpt_hdmi),
3937 HDA_CODEC_ENTRY(0x80862807, "Haswell HDMI", patch_i915_hsw_hdmi),
3938 HDA_CODEC_ENTRY(0x80862808, "Broadwell HDMI", patch_i915_hsw_hdmi),
3939 HDA_CODEC_ENTRY(0x80862809, "Skylake HDMI", patch_i915_hsw_hdmi),
3940 HDA_CODEC_ENTRY(0x8086280a, "Broxton HDMI", patch_i915_hsw_hdmi),
3941 HDA_CODEC_ENTRY(0x8086280b, "Kabylake HDMI", patch_i915_hsw_hdmi),
3942 HDA_CODEC_ENTRY(0x8086280c, "Cannonlake HDMI", patch_i915_glk_hdmi),
3943 HDA_CODEC_ENTRY(0x8086280d, "Geminilake HDMI", patch_i915_glk_hdmi),
3944 HDA_CODEC_ENTRY(0x8086280f, "Icelake HDMI", patch_i915_icl_hdmi),
3945 HDA_CODEC_ENTRY(0x80862880, "CedarTrail HDMI", patch_generic_hdmi),
3946 HDA_CODEC_ENTRY(0x80862882, "Valleyview2 HDMI", patch_i915_byt_hdmi),
3947 HDA_CODEC_ENTRY(0x80862883, "Braswell HDMI", patch_i915_byt_hdmi),
3948 HDA_CODEC_ENTRY(0x808629fb, "Crestline HDMI", patch_generic_hdmi),
3949 /* special ID for generic HDMI */
3950 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC_HDMI, "Generic HDMI", patch_generic_hdmi),
3953 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_hdmi);
3955 MODULE_LICENSE("GPL");
3956 MODULE_DESCRIPTION("HDMI HD-audio codec");
3957 MODULE_ALIAS("snd-hda-codec-intelhdmi");
3958 MODULE_ALIAS("snd-hda-codec-nvhdmi");
3959 MODULE_ALIAS("snd-hda-codec-atihdmi");
3961 static struct hda_codec_driver hdmi_driver = {
3962 .id = snd_hda_id_hdmi,
3965 module_hda_codec_driver(hdmi_driver);